xref: /illumos-gate/usr/src/stand/lib/sa/standalloc.c (revision 71815ce76261aa773c97600750fdce92334d1990)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <sys/types.h>
28 #include <sys/saio.h>
29 #include <sys/sysmacros.h>
30 #include <sys/promif.h>
31 #include <sys/bootconf.h>
32 #include <sys/salib.h>
33 
34 #ifdef DEBUG
35 static int	resalloc_debug = 1;
36 #else /* DEBUG */
37 static int	resalloc_debug = 0;
38 #endif /* DEBUG */
39 #define	dprintf	if (resalloc_debug) printf
40 
41 extern	caddr_t		resalloc(enum RESOURCES type,
42 				size_t bytes, caddr_t virthint, int align);
43 extern	void		resfree(enum RESOURCES type,
44 				caddr_t virtaddr, size_t bytes);
45 
46 extern int	pagesize;
47 
48 /*
49  *  This routine should be called get_a_page().
50  *  It allocates from the appropriate entity one or
51  *  more pages and maps them in.
52  */
53 
54 caddr_t
55 kern_resalloc(caddr_t virthint, size_t size, int align)
56 {
57 	if (virthint != 0)
58 		return (resalloc(RES_CHILDVIRT, size, virthint, align));
59 	else {
60 		return (resalloc(RES_BOOTSCRATCH, size, NULL, NULL));
61 	}
62 }
63 
64 /*
65  * This is called only on sparcv9 for freeing scratch memory.
66  * The standalone allocator cannot free other types of memory.
67  */
68 void
69 kern_resfree(caddr_t virtaddr, size_t size)
70 {
71 	resfree(RES_BOOTSCRATCH, virtaddr, size);
72 }
73 
74 int
75 get_progmemory(caddr_t vaddr, size_t size, int align)
76 {
77 	uintptr_t n;
78 
79 	/*
80 	 * if the vaddr given is not a mult of PAGESIZE,
81 	 * then we rounddown to a page, but keep the same
82 	 * ending addr.
83 	 */
84 	n = (uintptr_t)vaddr & (pagesize - 1);
85 	if (n) {
86 		vaddr -= n;
87 		size += n;
88 	}
89 
90 	dprintf("get_progmemory: requesting %lx bytes at %p\n", size,
91 	    (void *)vaddr);
92 	if (resalloc(RES_CHILDVIRT, size, vaddr, align) != vaddr)
93 		return (-1);
94 	return (0);
95 }
96