Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / obp / tools / promif / sun4u / prom_mem.c
CommitLineData
920dae64
AT
1/*
2* ========== Copyright Header Begin ==========================================
3*
4* Hypervisor Software File: prom_mem.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_mem.c 1.1 00/08/07 SMI"
51
52/*
53 * This file contains platform-dependent memory support routines,
54 * suitable for memory methods with 2-cell physical addresses.
55 * Use of these routines makes the caller platform-dependent,
56 * since the caller assumes knowledge of the physical layout of
57 * the machines address space. Generic programs should use the
58 * standard client interface memory allocators.
59 */
60
61#include <sys/promif.h>
62#include <sys/promimpl.h>
63
64ihandle_t
65prom_memory_ihandle(void)
66{
67 static ihandle_t imemory;
68
69 if (imemory != (ihandle_t)0)
70 return (imemory);
71
72 if (prom_getproplen(prom_chosennode(), "memory") != sizeof (ihandle_t))
73 return (imemory = (ihandle_t)-1);
74
75 (void) prom_getprop(prom_chosennode(), "memory", (caddr_t)(&imemory));
76 imemory = (ihandle_t)prom_decode_int(imemory);
77 return (imemory);
78}
79
80/*
81 * Allocate physical memory, unmapped and possibly aligned.
82 * Returns 0: Success; Non-zero: failure.
83 * Returns *physaddr only if successful.
84 *
85 * This routine is suitable for platforms with 2-cell physical addresses
86 * and a single size cell in the "memory" node.
87 */
88int
89prom_allocate_phys(size_t size, uint32_t align, u_longlong_t *physaddr)
90{
91 cell_t ci[10];
92 int32_t rv;
93 ihandle_t imemory = prom_memory_ihandle();
94
95 if ((imemory == (ihandle_t)-1))
96 return (-1);
97
98 if (align == 0)
99 align = (uint32_t)1;
100
101 ci[0] = p1275_ptr2cell("call-method"); /* Service name */
102 ci[1] = (cell_t)4; /* #argument cells */
103 ci[2] = (cell_t)3; /* #result cells */
104 ci[3] = p1275_ptr2cell("claim"); /* Arg1: Method name */
105 ci[4] = p1275_ihandle2cell(imemory); /* Arg2: memory ihandle */
106 ci[5] = p1275_uint2cell(align); /* Arg3: SA1: align */
107 ci[6] = p1275_size2cell(size); /* Arg4: SA2: size */
108
109 rv = p1275_cif_handler(&ci);
110
111 if (rv != 0)
112 return (rv);
113 if (p1275_cell2int(ci[7]) != 0) /* Res1: Catch result */
114 return (-1);
115
116 *physaddr = p1275_cells2ull(ci[8], ci[9]);
117 /* Res2: SR1: phys.hi ... Res3: SR2: phys.lo */
118 return (0);
119}
120
121/*
122 * Claim a region of physical memory, unmapped.
123 * Returns 0: Success; Non-zero: failure.
124 *
125 * This routine is suitable for platforms with 2-cell physical addresses
126 * and a single size cell in the "memory" node.
127 */
128int
129prom_claim_phys(size_t size, u_longlong_t physaddr)
130{
131 cell_t ci[10];
132 int32_t rv;
133 ihandle_t imemory = prom_memory_ihandle();
134
135 if ((imemory == (ihandle_t)-1))
136 return (-1);
137
138 ci[0] = p1275_ptr2cell("call-method"); /* Service name */
139 ci[1] = (cell_t)6; /* #argument cells */
140 ci[2] = (cell_t)1; /* #result cells */
141 ci[3] = p1275_ptr2cell("claim"); /* Arg1: Method name */
142 ci[4] = p1275_ihandle2cell(imemory); /* Arg2: mmu ihandle */
143 ci[5] = 0; /* Arg3: SA1: align */
144 ci[6] = p1275_size2cell(size); /* Arg4: SA2: len */
145 ci[7] = p1275_ull2cell_high(physaddr); /* Arg5: SA3: phys.hi */
146 ci[8] = p1275_ull2cell_low(physaddr); /* Arg6: SA4: phys.lo */
147
148 rv = p1275_cif_handler(&ci);
149
150 if (rv != 0)
151 return (rv);
152 if (p1275_cell2int(ci[9]) != 0) /* Res1: Catch result */
153 return (-1);
154
155 return (0);
156}
157
158/*
159 * Free physical memory (no unmapping is done).
160 * This routine is suitable for platforms with 2-cell physical addresses
161 * with a single size cell.
162 */
163void
164prom_free_phys(size_t size, u_longlong_t physaddr)
165{
166 cell_t ci[8];
167 ihandle_t imemory = prom_memory_ihandle();
168
169 if ((imemory == (ihandle_t)-1))
170 return;
171
172 ci[0] = p1275_ptr2cell("call-method"); /* Service name */
173 ci[1] = (cell_t)5; /* #argument cells */
174 ci[2] = (cell_t)0; /* #return cells */
175 ci[3] = p1275_ptr2cell("release"); /* Arg1: Method name */
176 ci[4] = p1275_ihandle2cell(imemory); /* Arg2: memory ihandle */
177 ci[5] = p1275_size2cell(size); /* Arg3: SA1: size */
178 ci[6] = p1275_ull2cell_high(physaddr); /* Arg4: SA2: phys.hi */
179 ci[7] = p1275_ull2cell_low(physaddr); /* Arg5: SA3: phys.lo */
180
181 (void) p1275_cif_handler(&ci);
182}