Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / tools / promif / sun4u / prom_alloc.c
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* Hypervisor Software File: prom_alloc.c
5*
6* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
7*
8* - Do no alter or remove copyright notices
9*
10* - Redistribution and use of this software in source and binary forms, with
11* or without modification, are permitted provided that the following
12* conditions are met:
13*
14* - Redistribution of source code must retain the above copyright notice,
15* this list of conditions and the following disclaimer.
16*
17* - Redistribution in binary form must reproduce the above copyright notice,
18* this list of conditions and the following disclaimer in the
19* documentation and/or other materials provided with the distribution.
20*
21* Neither the name of Sun Microsystems, Inc. or the names of contributors
22* may be used to endorse or promote products derived from this software
23* without specific prior written permission.
24*
25* This software is provided "AS IS," without a warranty of any kind.
26* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
27* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
28* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
29* MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
30* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
31* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
32* OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
33* FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
34* DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
35* ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
36* SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37*
38* You acknowledge that this software is not designed, licensed or
39* intended for use in the design, construction, operation or maintenance of
40* any nuclear facility.
41*
42* ========== Copyright Header End ============================================
43*/
44/*
45 * Copyright (c) 2000-2003 Sun Microsystems, Inc.
46 * All rights reserved.
47 * Use is subject to license terms.
48 */
49
50#pragma ident "@(#)prom_alloc.c 1.1 00/07/22 SMI"
51
52#include <sys/promif.h>
53#include <sys/promimpl.h>
54
55/*
56 * This allocator has OBP-like semantics associated with it.
57 * Specifically, the alignment value specifies both a physical
58 * and virtual alignment. If virthint is zero, a suitable virt
59 * is chosen. In either case, align is not ignored.
60 *
61 * This routine returns NULL on failure.
62 *
63 * Memory allocated with prom_alloc can be freed with prom_free.
64 *
65 * The generic allocator is prom_malloc.
66 *
67 */
68
69caddr_t
70prom_alloc(caddr_t virthint, size_t size, uint32_t align)
71{
72
73 caddr_t virt = virthint;
74 u_longlong_t physaddr;
75
76 if (align == 0)
77 align = (uint32_t)1;
78
79 /*
80 * First, allocate or claim the virtual address space.
81 * In either case, after this code, "virt" is the chosen address.
82 */
83 if (virthint == 0) {
84 virt = prom_allocate_virt(align, size);
85 if (virt == (caddr_t)-1)
86 return ((caddr_t)0);
87 } else {
88 if (prom_claim_virt(size, virthint) == (caddr_t)-1)
89 return ((caddr_t)0);
90 }
91
92 /*
93 * Next, allocate the physical address space, at the specified
94 * physical alignment (or 1 byte alignment, if none specified)
95 */
96
97 if (prom_allocate_phys(size, align, &physaddr) == -1) {
98
99 /*
100 * Request failed, free virtual address space and return.
101 */
102 prom_free_virt(size, virt);
103 return ((caddr_t)0);
104 }
105
106 /*
107 * Next, create a mapping from the physical to virtual address,
108 * using a default "mode".
109 */
110
111 if (prom_map_phys(-1, size, virt, physaddr) == -1) {
112
113 /*
114 * The call failed; release the physical and virtual
115 * addresses allocated or claimed, and return.
116 */
117
118 prom_free_virt(size, virt);
119 prom_free_phys(size, physaddr);
120 return ((caddr_t)0);
121 }
122 return (virt);
123}
124
125/*
126 * This is the generic client interface to "claim" memory.
127 * These two routines belong in the common directory.
128 */
129caddr_t
130prom_malloc(caddr_t virt, size_t size, uint32_t align)
131{
132 cell_t ci[7];
133 int32_t rv;
134
135 ci[0] = p1275_ptr2cell("claim"); /* Service name */
136 ci[1] = (cell_t)3; /* #argument cells */
137 ci[2] = (cell_t)1; /* #result cells */
138 ci[3] = p1275_ptr2cell(virt); /* Arg1: virt */
139 ci[4] = p1275_size2cell(size); /* Arg2: size */
140 ci[5] = p1275_uint2cell(align); /* Arg3: align */
141
142 rv = p1275_cif_handler(&ci);
143
144 if (rv == 0)
145 return ((caddr_t)p1275_cell2ptr(ci[6])); /* Res1: base */
146 return ((caddr_t)-1);
147}
148
149
150void
151prom_free(caddr_t virt, size_t size)
152{
153 cell_t ci[5];
154
155 ci[0] = p1275_ptr2cell("release"); /* Service name */
156 ci[1] = (cell_t)2; /* #argument cells */
157 ci[2] = (cell_t)0; /* #result cells */
158 ci[3] = p1275_ptr2cell(virt); /* Arg1: virt */
159 ci[4] = p1275_size2cell(size); /* Arg2: size */
160
161 (void) p1275_cif_handler(&ci);
162}