xref: /titanic_51/usr/src/stand/lib/sa/standalloc.c (revision 6a72db4a7fa12c3e0d1c1cf91a07390739fa0fbf)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/saio.h>
31 #include <sys/sysmacros.h>
32 #include <sys/promif.h>
33 #include <sys/bootconf.h>
34 #include <sys/salib.h>
35 
36 #ifdef DEBUG
37 static int	resalloc_debug = 1;
38 #else /* DEBUG */
39 static int	resalloc_debug = 0;
40 #endif /* DEBUG */
41 #define	dprintf	if (resalloc_debug) printf
42 
43 extern	caddr_t		resalloc(enum RESOURCES type,
44 				size_t bytes, caddr_t virthint, int align);
45 extern	void		resfree(enum RESOURCES type,
46 				caddr_t virtaddr, size_t bytes);
47 
48 caddr_t		memlistpage;
49 extern int	pagesize;
50 
51 /*
52  *  This routine should be called get_a_page().
53  *  It allocates from the appropriate entity one or
54  *  more pages and maps them in.
55  */
56 
57 caddr_t
58 kern_resalloc(caddr_t virthint, size_t size, int align)
59 {
60 	if (virthint != 0)
61 		return (resalloc(RES_CHILDVIRT, size, virthint, align));
62 	else {
63 		return (resalloc(RES_BOOTSCRATCH, size, NULL, NULL));
64 	}
65 }
66 
67 /*
68  * This is called only on sparcv9 for freeing scratch memory.
69  * The standalone allocator cannot free other types of memory.
70  */
71 void
72 kern_resfree(caddr_t virtaddr, size_t size)
73 {
74 	resfree(RES_BOOTSCRATCH, virtaddr, size);
75 }
76 
77 int
78 get_progmemory(caddr_t vaddr, size_t size, int align)
79 {
80 	uintptr_t n;
81 
82 	/*
83 	 * if the vaddr given is not a mult of PAGESIZE,
84 	 * then we rounddown to a page, but keep the same
85 	 * ending addr.
86 	 */
87 	n = (uintptr_t)vaddr & (pagesize - 1);
88 	if (n) {
89 		vaddr -= n;
90 		size += n;
91 	}
92 
93 	dprintf("get_progmemory: requesting %lx bytes at %p\n", size,
94 	    (void *)vaddr);
95 	if (resalloc(RES_CHILDVIRT, size, vaddr, align) != vaddr)
96 		return (-1);
97 	return (0);
98 }
99