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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
2353391bafSeota  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/promif.h>
307c478bd9Sstevel@tonic-gate #include <sys/promimpl.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Routines for walking the PROMs devinfo tree
347c478bd9Sstevel@tonic-gate  */
35*fa9e4066Sahrens pnode_t
prom_nextnode(pnode_t nodeid)36*fa9e4066Sahrens prom_nextnode(pnode_t nodeid)
377c478bd9Sstevel@tonic-gate {
387c478bd9Sstevel@tonic-gate 	cell_t ci[5];
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("peer");		/* Service name */
417c478bd9Sstevel@tonic-gate 	ci[1] = (cell_t)1;			/* #argument cells */
427c478bd9Sstevel@tonic-gate 	ci[2] = (cell_t)1;			/* #result cells */
437c478bd9Sstevel@tonic-gate 	ci[3] = p1275_dnode2cell(nodeid);	/* Arg1: input phandle */
447c478bd9Sstevel@tonic-gate 	ci[4] = p1275_dnode2cell(OBP_NONODE);	/* Res1: Prime result */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	promif_preprom();
477c478bd9Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
487c478bd9Sstevel@tonic-gate 	promif_postprom();
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	return (p1275_cell2dnode(ci[4]));	/* Res1: peer phandle */
517c478bd9Sstevel@tonic-gate }
527c478bd9Sstevel@tonic-gate 
53*fa9e4066Sahrens pnode_t
prom_childnode(pnode_t nodeid)54*fa9e4066Sahrens prom_childnode(pnode_t nodeid)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	cell_t ci[5];
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("child");	/* Service name */
597c478bd9Sstevel@tonic-gate 	ci[1] = (cell_t)1;			/* #argument cells */
607c478bd9Sstevel@tonic-gate 	ci[2] = (cell_t)1;			/* #result cells */
617c478bd9Sstevel@tonic-gate 	ci[3] = p1275_dnode2cell(nodeid);	/* Arg1: input phandle */
627c478bd9Sstevel@tonic-gate 	ci[4] = p1275_dnode2cell(OBP_NONODE);	/* Res1: Prime result */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	promif_preprom();
657c478bd9Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
667c478bd9Sstevel@tonic-gate 	promif_postprom();
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	return (p1275_cell2dnode(ci[4]));	/* Res1: child phandle */
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * prom_walk_devs() implements a generic walker for the OBP tree; this
737c478bd9Sstevel@tonic-gate  * implementation uses an explicitly managed stack in order to save the
747c478bd9Sstevel@tonic-gate  * overhead of a recursive implementation.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate void
prom_walk_devs(pnode_t node,int (* cb)(pnode_t,void *,void *),void * arg,void * result)77*fa9e4066Sahrens prom_walk_devs(pnode_t node, int (*cb)(pnode_t, void *, void *), void *arg,
787c478bd9Sstevel@tonic-gate     void *result)
797c478bd9Sstevel@tonic-gate {
80*fa9e4066Sahrens 	pnode_t stack[OBP_STACKDEPTH];
817c478bd9Sstevel@tonic-gate 	int stackidx = 0;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (node == OBP_NONODE || node == OBP_BADNODE) {
847c478bd9Sstevel@tonic-gate 		prom_panic("Invalid node specified as root of prom tree walk");
857c478bd9Sstevel@tonic-gate 	}
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	stack[0] = node;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	for (;;) {
90*fa9e4066Sahrens 		pnode_t curnode = stack[stackidx];
91*fa9e4066Sahrens 		pnode_t child;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 		/*
947c478bd9Sstevel@tonic-gate 		 * We're out of stuff to do at this level, bump back up a level
957c478bd9Sstevel@tonic-gate 		 * in the tree, and move to the next node;  if the new level
967c478bd9Sstevel@tonic-gate 		 * will be level -1, we're done.
977c478bd9Sstevel@tonic-gate 		 */
987c478bd9Sstevel@tonic-gate 		if (curnode == OBP_NONODE || curnode == OBP_BADNODE) {
997c478bd9Sstevel@tonic-gate 			stackidx--;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 			if (stackidx < 0)
1027c478bd9Sstevel@tonic-gate 				return;
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 			stack[stackidx] = prom_nextnode(stack[stackidx]);
1057c478bd9Sstevel@tonic-gate 			continue;
1067c478bd9Sstevel@tonic-gate 		}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 		switch ((*cb)(curnode, arg, result)) {
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		case PROM_WALK_TERMINATE:
1117c478bd9Sstevel@tonic-gate 			return;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 		case PROM_WALK_CONTINUE:
1147c478bd9Sstevel@tonic-gate 			/*
1157c478bd9Sstevel@tonic-gate 			 * If curnode has a child, traverse to it,
1167c478bd9Sstevel@tonic-gate 			 * otherwise move to curnode's sibling.
1177c478bd9Sstevel@tonic-gate 			 */
1187c478bd9Sstevel@tonic-gate 			child = prom_childnode(curnode);
1197c478bd9Sstevel@tonic-gate 			if (child != OBP_NONODE && child != OBP_BADNODE) {
1207c478bd9Sstevel@tonic-gate 				stackidx++;
1217c478bd9Sstevel@tonic-gate 				stack[stackidx] = child;
1227c478bd9Sstevel@tonic-gate 			} else {
1237c478bd9Sstevel@tonic-gate 				stack[stackidx] =
1247c478bd9Sstevel@tonic-gate 				    prom_nextnode(stack[stackidx]);
1257c478bd9Sstevel@tonic-gate 			}
1267c478bd9Sstevel@tonic-gate 			break;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		default:
1297c478bd9Sstevel@tonic-gate 			prom_panic("unrecognized walk directive");
1307c478bd9Sstevel@tonic-gate 		}
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * prom_findnode_bydevtype() searches the prom device subtree rooted at 'node'
1367c478bd9Sstevel@tonic-gate  * and returns the first node whose device type property matches the type
1377c478bd9Sstevel@tonic-gate  * supplied in 'devtype'.
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate static int
bytype_cb(pnode_t node,void * arg,void * result)140*fa9e4066Sahrens bytype_cb(pnode_t node, void *arg, void *result)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	if (prom_devicetype(node, (char *)arg)) {
143*fa9e4066Sahrens 		*((pnode_t *)result) = node;
1447c478bd9Sstevel@tonic-gate 		return (PROM_WALK_TERMINATE);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	return (PROM_WALK_CONTINUE);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate 
149*fa9e4066Sahrens pnode_t
prom_findnode_bydevtype(pnode_t node,char * devtype)150*fa9e4066Sahrens prom_findnode_bydevtype(pnode_t node, char *devtype)
1517c478bd9Sstevel@tonic-gate {
152*fa9e4066Sahrens 	pnode_t result = OBP_NONODE;
1537c478bd9Sstevel@tonic-gate 	prom_walk_devs(node, bytype_cb, devtype, &result);
1547c478bd9Sstevel@tonic-gate 	return (result);
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate  * prom_findnode_byname() searches the prom device subtree rooted at 'node' and
1607c478bd9Sstevel@tonic-gate  * returns the first node whose name matches the name supplied in 'name'.
1617c478bd9Sstevel@tonic-gate  */
1627c478bd9Sstevel@tonic-gate static int
byname_cb(pnode_t node,void * arg,void * result)163*fa9e4066Sahrens byname_cb(pnode_t node, void *arg, void *result)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	if (prom_getnode_byname(node, (char *)arg)) {
166*fa9e4066Sahrens 		*((pnode_t *)result) = node;
1677c478bd9Sstevel@tonic-gate 		return (PROM_WALK_TERMINATE);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 	return (PROM_WALK_CONTINUE);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate 
172*fa9e4066Sahrens pnode_t
prom_findnode_byname(pnode_t node,char * name)173*fa9e4066Sahrens prom_findnode_byname(pnode_t node, char *name)
1747c478bd9Sstevel@tonic-gate {
175*fa9e4066Sahrens 	pnode_t result = OBP_NONODE;
1767c478bd9Sstevel@tonic-gate 	prom_walk_devs(node, byname_cb, name, &result);
1777c478bd9Sstevel@tonic-gate 	return (result);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /*
1817c478bd9Sstevel@tonic-gate  * Return the root nodeid.
1827c478bd9Sstevel@tonic-gate  * Calling prom_nextnode(0) returns the root nodeid.
1837c478bd9Sstevel@tonic-gate  */
184*fa9e4066Sahrens pnode_t
prom_rootnode(void)1857c478bd9Sstevel@tonic-gate prom_rootnode(void)
1867c478bd9Sstevel@tonic-gate {
187*fa9e4066Sahrens 	static pnode_t rootnode;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	return (rootnode ? rootnode : (rootnode = prom_nextnode(OBP_NONODE)));
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
192*fa9e4066Sahrens pnode_t
prom_parentnode(pnode_t nodeid)193*fa9e4066Sahrens prom_parentnode(pnode_t nodeid)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	cell_t ci[5];
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("parent");	/* Service name */
1987c478bd9Sstevel@tonic-gate 	ci[1] = (cell_t)1;			/* #argument cells */
1997c478bd9Sstevel@tonic-gate 	ci[2] = (cell_t)1;			/* #result cells */
2007c478bd9Sstevel@tonic-gate 	ci[3] = p1275_dnode2cell(nodeid);	/* Arg1: input phandle */
2017c478bd9Sstevel@tonic-gate 	ci[4] = p1275_dnode2cell(OBP_NONODE);	/* Res1: Prime result */
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	promif_preprom();
2047c478bd9Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
2057c478bd9Sstevel@tonic-gate 	promif_postprom();
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	return (p1275_cell2dnode(ci[4]));	/* Res1: parent phandle */
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
210*fa9e4066Sahrens pnode_t
prom_finddevice(char * path)2117c478bd9Sstevel@tonic-gate prom_finddevice(char *path)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	cell_t ci[5];
2147c478bd9Sstevel@tonic-gate #ifdef PROM_32BIT_ADDRS
2157c478bd9Sstevel@tonic-gate 	char *opath = NULL;
2167c478bd9Sstevel@tonic-gate 	size_t len;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if ((uintptr_t)path > (uint32_t)-1) {
2197c478bd9Sstevel@tonic-gate 		opath = path;
2207c478bd9Sstevel@tonic-gate 		len = prom_strlen(opath) + 1; /* include terminating NUL */
2217c478bd9Sstevel@tonic-gate 		path = promplat_alloc(len);
2227c478bd9Sstevel@tonic-gate 		if (path == NULL) {
2237c478bd9Sstevel@tonic-gate 			return (OBP_BADNODE);
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 		(void) prom_strcpy(path, opath);
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate #endif
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	promif_preprom();
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	ci[0] = p1275_ptr2cell("finddevice");	/* Service name */
2327c478bd9Sstevel@tonic-gate 	ci[1] = (cell_t)1;			/* #argument cells */
2337c478bd9Sstevel@tonic-gate 	ci[2] = (cell_t)1;			/* #result cells */
2347c478bd9Sstevel@tonic-gate 	ci[3] = p1275_ptr2cell(path);		/* Arg1: pathname */
2357c478bd9Sstevel@tonic-gate 	ci[4] = p1275_dnode2cell(OBP_BADNODE);	/* Res1: Prime result */
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	(void) p1275_cif_handler(&ci);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	promif_postprom();
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate #ifdef PROM_32BIT_ADDRS
2427c478bd9Sstevel@tonic-gate 	if (opath != NULL)
2437c478bd9Sstevel@tonic-gate 		promplat_free(path, len);
2447c478bd9Sstevel@tonic-gate #endif
2457c478bd9Sstevel@tonic-gate 
246*fa9e4066Sahrens 	return ((pnode_t)p1275_cell2dnode(ci[4])); /* Res1: phandle */
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
249*fa9e4066Sahrens pnode_t
prom_chosennode(void)2507c478bd9Sstevel@tonic-gate prom_chosennode(void)
2517c478bd9Sstevel@tonic-gate {
252*fa9e4066Sahrens 	static pnode_t chosen;
253*fa9e4066Sahrens 	pnode_t	node;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	if (chosen)
2567c478bd9Sstevel@tonic-gate 		return (chosen);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	node = prom_finddevice("/chosen");
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	if (node != OBP_BADNODE)
2617c478bd9Sstevel@tonic-gate 		return (chosen = node);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	prom_fatal_error("prom_chosennode: Can't find </chosen>\n");
2647c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
26553391bafSeota 
26653391bafSeota 	/*
26753391bafSeota 	 * gcc doesn't recognize "NOTREACHED" and puts the warning.
26853391bafSeota 	 * To surpress it, returning an integer value is required.
26953391bafSeota 	 */
270*fa9e4066Sahrens 	return ((pnode_t)0);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate /*
2747c478bd9Sstevel@tonic-gate  * Returns the nodeid of /aliases.
2757c478bd9Sstevel@tonic-gate  * /aliases exists in OBP >= 2.4 and in Open Firmware.
2767c478bd9Sstevel@tonic-gate  * Returns OBP_BADNODE if it doesn't exist.
2777c478bd9Sstevel@tonic-gate  */
278*fa9e4066Sahrens pnode_t
prom_alias_node(void)2797c478bd9Sstevel@tonic-gate prom_alias_node(void)
2807c478bd9Sstevel@tonic-gate {
281*fa9e4066Sahrens 	static pnode_t node;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (node == 0)
2847c478bd9Sstevel@tonic-gate 		node = prom_finddevice("/aliases");
2857c478bd9Sstevel@tonic-gate 	return (node);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate /*
2897c478bd9Sstevel@tonic-gate  * Returns the nodeid of /options.
2907c478bd9Sstevel@tonic-gate  * Returns OBP_BADNODE if it doesn't exist.
2917c478bd9Sstevel@tonic-gate  */
292*fa9e4066Sahrens pnode_t
prom_optionsnode(void)2937c478bd9Sstevel@tonic-gate prom_optionsnode(void)
2947c478bd9Sstevel@tonic-gate {
295*fa9e4066Sahrens 	static pnode_t node;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (node == 0)
2987c478bd9Sstevel@tonic-gate 		node = prom_finddevice("/options");
2997c478bd9Sstevel@tonic-gate 	return (node);
3007c478bd9Sstevel@tonic-gate }
301