xref: /titanic_52/usr/src/psm/promif/ieee1275/sun4/prom_mem.c (revision 986fd29a0dc13f7608ef7f508f6e700bd7bc2720)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*986fd29aSsetje  * Common Development and Distribution License (the "License").
6*986fd29aSsetje  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*986fd29aSsetje  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23*986fd29aSsetje  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * This file contains platform-dependent memory support routines,
307c478bd9Sstevel@tonic-gate  * suitable for memory methods with 2-cell physical addresses.
317c478bd9Sstevel@tonic-gate  * Use of these routines makes the caller platform-dependent,
327c478bd9Sstevel@tonic-gate  * since the caller assumes knowledge of the physical layout of
337c478bd9Sstevel@tonic-gate  * the machines address space.  Generic programs should use the
347c478bd9Sstevel@tonic-gate  * standard client interface memory allocators.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <sys/promif.h>
387c478bd9Sstevel@tonic-gate #include <sys/promimpl.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate ihandle_t
417c478bd9Sstevel@tonic-gate prom_memory_ihandle(void)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	static ihandle_t imemory;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	if (imemory != (ihandle_t)0)
467c478bd9Sstevel@tonic-gate 		return (imemory);
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate 	if (prom_getproplen(prom_chosennode(), "memory") != sizeof (ihandle_t))
497c478bd9Sstevel@tonic-gate 		return (imemory = (ihandle_t)-1);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	(void) prom_getprop(prom_chosennode(), "memory", (caddr_t)(&imemory));
527c478bd9Sstevel@tonic-gate 	return (imemory);
537c478bd9Sstevel@tonic-gate }
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Allocate physical memory, unmapped and possibly aligned.
577c478bd9Sstevel@tonic-gate  * Returns 0: Success; Non-zero: failure.
587c478bd9Sstevel@tonic-gate  * Returns *physaddr only if successful.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  * This routine is suitable for platforms with 2-cell physical addresses
617c478bd9Sstevel@tonic-gate  * and a single size cell in the "memory" node.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate int
64*986fd29aSsetje prom_allocate_phys(size_t size, uint_t align, unsigned long long *physaddr)
657c478bd9Sstevel@tonic-gate {
667c478bd9Sstevel@tonic-gate 	cell_t ci[10];
677c478bd9Sstevel@tonic-gate 	int rv;
687c478bd9Sstevel@tonic-gate 	ihandle_t imemory = prom_memory_ihandle();
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	if ((imemory == (ihandle_t)-1))
717c478bd9Sstevel@tonic-gate 		return (-1);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	if (align == 0)
74*986fd29aSsetje 		align = (uint_t)1;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");	/* Service name */
777c478bd9Sstevel@tonic-gate 	ci[1] = (cell_t)4;			/* #argument cells */
787c478bd9Sstevel@tonic-gate 	ci[2] = (cell_t)3;			/* #result cells */
797c478bd9Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("claim");	/* Arg1: Method name */
807c478bd9Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(imemory);	/* Arg2: memory ihandle */
817c478bd9Sstevel@tonic-gate 	ci[5] = p1275_uint2cell(align);		/* Arg3: SA1: align */
827c478bd9Sstevel@tonic-gate 	ci[6] = p1275_size2cell(size);		/* Arg4: SA2: size */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	promif_preprom();
857c478bd9Sstevel@tonic-gate 	rv = p1275_cif_handler(&ci);
867c478bd9Sstevel@tonic-gate 	promif_postprom();
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (rv != 0)
897c478bd9Sstevel@tonic-gate 		return (rv);
907c478bd9Sstevel@tonic-gate 	if (p1275_cell2int(ci[7]) != 0)		/* Res1: Catch result */
917c478bd9Sstevel@tonic-gate 		return (-1);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	*physaddr = p1275_cells2ull(ci[8], ci[9]);
947c478bd9Sstevel@tonic-gate 				/* Res2: SR1: phys.hi ... Res3: SR2: phys.lo */
957c478bd9Sstevel@tonic-gate 	return (0);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate  * Claim a region of physical memory, unmapped.
1007c478bd9Sstevel@tonic-gate  * Returns 0: Success; Non-zero: failure.
1017c478bd9Sstevel@tonic-gate  *
1027c478bd9Sstevel@tonic-gate  * This routine is suitable for platforms with 2-cell physical addresses
1037c478bd9Sstevel@tonic-gate  * and a single size cell in the "memory" node.
1047c478bd9Sstevel@tonic-gate  */
1057c478bd9Sstevel@tonic-gate int
1067c478bd9Sstevel@tonic-gate prom_claim_phys(size_t size, unsigned long long physaddr)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	cell_t ci[10];
1097c478bd9Sstevel@tonic-gate 	int rv;
1107c478bd9Sstevel@tonic-gate 	ihandle_t imemory = prom_memory_ihandle();
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if ((imemory == (ihandle_t)-1))
1137c478bd9Sstevel@tonic-gate 		return (-1);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");	/* Service name */
1167c478bd9Sstevel@tonic-gate 	ci[1] = (cell_t)6;			/* #argument cells */
1177c478bd9Sstevel@tonic-gate 	ci[2] = (cell_t)1;			/* #result cells */
1187c478bd9Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("claim");	/* Arg1: Method name */
1197c478bd9Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(imemory);	/* Arg2: mmu ihandle */
1207c478bd9Sstevel@tonic-gate 	ci[5] = 0;				/* Arg3: SA1: align */
1217c478bd9Sstevel@tonic-gate 	ci[6] = p1275_size2cell(size);		/* Arg4: SA2: len */
1227c478bd9Sstevel@tonic-gate 	ci[7] = p1275_ull2cell_high(physaddr);	/* Arg5: SA3: phys.hi */
1237c478bd9Sstevel@tonic-gate 	ci[8] = p1275_ull2cell_low(physaddr);	/* Arg6: SA4: phys.lo */
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	promif_preprom();
1267c478bd9Sstevel@tonic-gate 	rv = p1275_cif_handler(&ci);
1277c478bd9Sstevel@tonic-gate 	promif_postprom();
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (rv != 0)
1307c478bd9Sstevel@tonic-gate 		return (rv);
1317c478bd9Sstevel@tonic-gate 	if (p1275_cell2int(ci[9]) != 0)		/* Res1: Catch result */
1327c478bd9Sstevel@tonic-gate 		return (-1);
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	return (0);
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate  * Free physical memory (no unmapping is done).
1397c478bd9Sstevel@tonic-gate  * This routine is suitable for platforms with 2-cell physical addresses
1407c478bd9Sstevel@tonic-gate  * with a single size cell.
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate void
1437c478bd9Sstevel@tonic-gate prom_free_phys(size_t size, unsigned long long physaddr)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	cell_t ci[8];
1467c478bd9Sstevel@tonic-gate 	ihandle_t imemory = prom_memory_ihandle();
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if ((imemory == (ihandle_t)-1))
1497c478bd9Sstevel@tonic-gate 		return;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("call-method");	/* Service name */
1527c478bd9Sstevel@tonic-gate 	ci[1] = (cell_t)5;			/* #argument cells */
1537c478bd9Sstevel@tonic-gate 	ci[2] = (cell_t)0;			/* #return cells */
1547c478bd9Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell("release");	/* Arg1: Method name */
1557c478bd9Sstevel@tonic-gate 	ci[4] = p1275_ihandle2cell(imemory);	/* Arg2: memory ihandle */
1567c478bd9Sstevel@tonic-gate 	ci[5] = p1275_size2cell(size);		/* Arg3: SA1: size */
1577c478bd9Sstevel@tonic-gate 	ci[6] = p1275_ull2cell_high(physaddr);	/* Arg4: SA2: phys.hi */
1587c478bd9Sstevel@tonic-gate 	ci[7] = p1275_ull2cell_low(physaddr);	/* Arg5: SA3: phys.lo */
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	promif_preprom();
1617c478bd9Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
1627c478bd9Sstevel@tonic-gate 	promif_postprom();
1637c478bd9Sstevel@tonic-gate }
164*986fd29aSsetje 
165*986fd29aSsetje static pnode_t
166*986fd29aSsetje prom_mem_phandle(void)
167*986fd29aSsetje {
168*986fd29aSsetje 	static pnode_t pmem = 0;
169*986fd29aSsetje 
170*986fd29aSsetje 	if (pmem == (pnode_t)0)  {
171*986fd29aSsetje 		ihandle_t ih;
172*986fd29aSsetje 
173*986fd29aSsetje 		if ((ih = prom_memory_ihandle()) == (ihandle_t)-1)
174*986fd29aSsetje 			prom_panic("Can't get memory ihandle");
175*986fd29aSsetje 		pmem = prom_getphandle(ih);
176*986fd29aSsetje 	}
177*986fd29aSsetje 	return (pmem);
178*986fd29aSsetje }
179*986fd29aSsetje 
180*986fd29aSsetje 
181*986fd29aSsetje int
182*986fd29aSsetje prom_phys_installed_len(void)
183*986fd29aSsetje {
184*986fd29aSsetje 	return (prom_getproplen(prom_mem_phandle(), "reg"));
185*986fd29aSsetje }
186*986fd29aSsetje 
187*986fd29aSsetje int
188*986fd29aSsetje prom_phys_avail_len(void)
189*986fd29aSsetje {
190*986fd29aSsetje 	return (prom_getproplen(prom_mem_phandle(), "available"));
191*986fd29aSsetje }
192*986fd29aSsetje 
193*986fd29aSsetje int
194*986fd29aSsetje prom_phys_installed(caddr_t prop)
195*986fd29aSsetje {
196*986fd29aSsetje 	return (prom_getprop(prom_mem_phandle(), "reg", prop));
197*986fd29aSsetje }
198*986fd29aSsetje 
199*986fd29aSsetje int
200*986fd29aSsetje prom_phys_avail(caddr_t prop)
201*986fd29aSsetje {
202*986fd29aSsetje 	return (prom_getprop(prom_mem_phandle(), "available", prop));
203*986fd29aSsetje }
204