xref: /onnv-gate/usr/src/psm/promif/ieee1275/sun4/prom_mem.c (revision 5648:161f8007cab9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*5648Ssetje  * Common Development and Distribution License (the "License").
6*5648Ssetje  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*5648Ssetje  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*5648Ssetje  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * This file contains platform-dependent memory support routines,
300Sstevel@tonic-gate  * suitable for memory methods with 2-cell physical addresses.
310Sstevel@tonic-gate  * Use of these routines makes the caller platform-dependent,
320Sstevel@tonic-gate  * since the caller assumes knowledge of the physical layout of
330Sstevel@tonic-gate  * the machines address space.  Generic programs should use the
340Sstevel@tonic-gate  * standard client interface memory allocators.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <sys/promif.h>
380Sstevel@tonic-gate #include <sys/promimpl.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate ihandle_t
prom_memory_ihandle(void)410Sstevel@tonic-gate prom_memory_ihandle(void)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate 	static ihandle_t imemory;
440Sstevel@tonic-gate 
450Sstevel@tonic-gate 	if (imemory != (ihandle_t)0)
460Sstevel@tonic-gate 		return (imemory);
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 	if (prom_getproplen(prom_chosennode(), "memory") != sizeof (ihandle_t))
490Sstevel@tonic-gate 		return (imemory = (ihandle_t)-1);
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 	(void) prom_getprop(prom_chosennode(), "memory", (caddr_t)(&imemory));
520Sstevel@tonic-gate 	return (imemory);
530Sstevel@tonic-gate }
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * Allocate physical memory, unmapped and possibly aligned.
570Sstevel@tonic-gate  * Returns 0: Success; Non-zero: failure.
580Sstevel@tonic-gate  * Returns *physaddr only if successful.
590Sstevel@tonic-gate  *
600Sstevel@tonic-gate  * This routine is suitable for platforms with 2-cell physical addresses
610Sstevel@tonic-gate  * and a single size cell in the "memory" node.
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate int
prom_allocate_phys(size_t size,uint_t align,unsigned long long * physaddr)64*5648Ssetje prom_allocate_phys(size_t size, uint_t align, unsigned long long *physaddr)
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	cell_t ci[10];
670Sstevel@tonic-gate 	int rv;
680Sstevel@tonic-gate 	ihandle_t imemory = prom_memory_ihandle();
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	if ((imemory == (ihandle_t)-1))
710Sstevel@tonic-gate 		return (-1);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if (align == 0)
74*5648Ssetje 		align = (uint_t)1;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");	/* Service name */
770Sstevel@tonic-gate 	ci[1] = (cell_t)4;			/* #argument cells */
780Sstevel@tonic-gate 	ci[2] = (cell_t)3;			/* #result cells */
790Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("claim");	/* Arg1: Method name */
800Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(imemory);	/* Arg2: memory ihandle */
810Sstevel@tonic-gate 	ci[5] = p1275_uint2cell(align);		/* Arg3: SA1: align */
820Sstevel@tonic-gate 	ci[6] = p1275_size2cell(size);		/* Arg4: SA2: size */
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	promif_preprom();
850Sstevel@tonic-gate 	rv = p1275_cif_handler(&ci);
860Sstevel@tonic-gate 	promif_postprom();
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	if (rv != 0)
890Sstevel@tonic-gate 		return (rv);
900Sstevel@tonic-gate 	if (p1275_cell2int(ci[7]) != 0)		/* Res1: Catch result */
910Sstevel@tonic-gate 		return (-1);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	*physaddr = p1275_cells2ull(ci[8], ci[9]);
940Sstevel@tonic-gate 				/* Res2: SR1: phys.hi ... Res3: SR2: phys.lo */
950Sstevel@tonic-gate 	return (0);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * Claim a region of physical memory, unmapped.
1000Sstevel@tonic-gate  * Returns 0: Success; Non-zero: failure.
1010Sstevel@tonic-gate  *
1020Sstevel@tonic-gate  * This routine is suitable for platforms with 2-cell physical addresses
1030Sstevel@tonic-gate  * and a single size cell in the "memory" node.
1040Sstevel@tonic-gate  */
1050Sstevel@tonic-gate int
prom_claim_phys(size_t size,unsigned long long physaddr)1060Sstevel@tonic-gate prom_claim_phys(size_t size, unsigned long long physaddr)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	cell_t ci[10];
1090Sstevel@tonic-gate 	int rv;
1100Sstevel@tonic-gate 	ihandle_t imemory = prom_memory_ihandle();
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if ((imemory == (ihandle_t)-1))
1130Sstevel@tonic-gate 		return (-1);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");	/* Service name */
1160Sstevel@tonic-gate 	ci[1] = (cell_t)6;			/* #argument cells */
1170Sstevel@tonic-gate 	ci[2] = (cell_t)1;			/* #result cells */
1180Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("claim");	/* Arg1: Method name */
1190Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(imemory);	/* Arg2: mmu ihandle */
1200Sstevel@tonic-gate 	ci[5] = 0;				/* Arg3: SA1: align */
1210Sstevel@tonic-gate 	ci[6] = p1275_size2cell(size);		/* Arg4: SA2: len */
1220Sstevel@tonic-gate 	ci[7] = p1275_ull2cell_high(physaddr);	/* Arg5: SA3: phys.hi */
1230Sstevel@tonic-gate 	ci[8] = p1275_ull2cell_low(physaddr);	/* Arg6: SA4: phys.lo */
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	promif_preprom();
1260Sstevel@tonic-gate 	rv = p1275_cif_handler(&ci);
1270Sstevel@tonic-gate 	promif_postprom();
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	if (rv != 0)
1300Sstevel@tonic-gate 		return (rv);
1310Sstevel@tonic-gate 	if (p1275_cell2int(ci[9]) != 0)		/* Res1: Catch result */
1320Sstevel@tonic-gate 		return (-1);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	return (0);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate  * Free physical memory (no unmapping is done).
1390Sstevel@tonic-gate  * This routine is suitable for platforms with 2-cell physical addresses
1400Sstevel@tonic-gate  * with a single size cell.
1410Sstevel@tonic-gate  */
1420Sstevel@tonic-gate void
prom_free_phys(size_t size,unsigned long long physaddr)1430Sstevel@tonic-gate prom_free_phys(size_t size, unsigned long long physaddr)
1440Sstevel@tonic-gate {
1450Sstevel@tonic-gate 	cell_t ci[8];
1460Sstevel@tonic-gate 	ihandle_t imemory = prom_memory_ihandle();
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if ((imemory == (ihandle_t)-1))
1490Sstevel@tonic-gate 		return;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");	/* Service name */
1520Sstevel@tonic-gate 	ci[1] = (cell_t)5;			/* #argument cells */
1530Sstevel@tonic-gate 	ci[2] = (cell_t)0;			/* #return cells */
1540Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("release");	/* Arg1: Method name */
1550Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(imemory);	/* Arg2: memory ihandle */
1560Sstevel@tonic-gate 	ci[5] = p1275_size2cell(size);		/* Arg3: SA1: size */
1570Sstevel@tonic-gate 	ci[6] = p1275_ull2cell_high(physaddr);	/* Arg4: SA2: phys.hi */
1580Sstevel@tonic-gate 	ci[7] = p1275_ull2cell_low(physaddr);	/* Arg5: SA3: phys.lo */
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	promif_preprom();
1610Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
1620Sstevel@tonic-gate 	promif_postprom();
1630Sstevel@tonic-gate }
164*5648Ssetje 
165*5648Ssetje static pnode_t
prom_mem_phandle(void)166*5648Ssetje prom_mem_phandle(void)
167*5648Ssetje {
168*5648Ssetje 	static pnode_t pmem = 0;
169*5648Ssetje 
170*5648Ssetje 	if (pmem == (pnode_t)0)  {
171*5648Ssetje 		ihandle_t ih;
172*5648Ssetje 
173*5648Ssetje 		if ((ih = prom_memory_ihandle()) == (ihandle_t)-1)
174*5648Ssetje 			prom_panic("Can't get memory ihandle");
175*5648Ssetje 		pmem = prom_getphandle(ih);
176*5648Ssetje 	}
177*5648Ssetje 	return (pmem);
178*5648Ssetje }
179*5648Ssetje 
180*5648Ssetje 
181*5648Ssetje int
prom_phys_installed_len(void)182*5648Ssetje prom_phys_installed_len(void)
183*5648Ssetje {
184*5648Ssetje 	return (prom_getproplen(prom_mem_phandle(), "reg"));
185*5648Ssetje }
186*5648Ssetje 
187*5648Ssetje int
prom_phys_avail_len(void)188*5648Ssetje prom_phys_avail_len(void)
189*5648Ssetje {
190*5648Ssetje 	return (prom_getproplen(prom_mem_phandle(), "available"));
191*5648Ssetje }
192*5648Ssetje 
193*5648Ssetje int
prom_phys_installed(caddr_t prop)194*5648Ssetje prom_phys_installed(caddr_t prop)
195*5648Ssetje {
196*5648Ssetje 	return (prom_getprop(prom_mem_phandle(), "reg", prop));
197*5648Ssetje }
198*5648Ssetje 
199*5648Ssetje int
prom_phys_avail(caddr_t prop)200*5648Ssetje prom_phys_avail(caddr_t prop)
201*5648Ssetje {
202*5648Ssetje 	return (prom_getprop(prom_mem_phandle(), "available", prop));
203*5648Ssetje }
204