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
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate #include <sys/promif.h>
300Sstevel@tonic-gate #include <sys/promimpl.h>
310Sstevel@tonic-gate #include <sys/prom_emul.h>
320Sstevel@tonic-gate #include <sys/obpdefs.h>
330Sstevel@tonic-gate #include <sys/sunddi.h>
340Sstevel@tonic-gate
350Sstevel@tonic-gate static prom_node_t *promif_top;
360Sstevel@tonic-gate
37*789Sahrens static prom_node_t *promif_find_node(pnode_t nodeid);
380Sstevel@tonic-gate static int getproplen(prom_node_t *pnp, char *name);
390Sstevel@tonic-gate static void *getprop(prom_node_t *pnp, char *name);
400Sstevel@tonic-gate
410Sstevel@tonic-gate static void
promif_create_prop(prom_node_t * pnp,char * name,void * val,int len,int flags)420Sstevel@tonic-gate promif_create_prop(prom_node_t *pnp, char *name, void *val, int len, int flags)
430Sstevel@tonic-gate {
440Sstevel@tonic-gate struct prom_prop *p, *q;
450Sstevel@tonic-gate
460Sstevel@tonic-gate q = kmem_zalloc(sizeof (*q), KM_SLEEP);
470Sstevel@tonic-gate q->pp_name = kmem_zalloc(strlen(name) + 1, KM_SLEEP);
480Sstevel@tonic-gate (void) strcpy(q->pp_name, name);
490Sstevel@tonic-gate q->pp_val = kmem_alloc(len, KM_SLEEP);
500Sstevel@tonic-gate q->pp_len = len;
510Sstevel@tonic-gate switch (flags) {
520Sstevel@tonic-gate case DDI_PROP_TYPE_INT:
530Sstevel@tonic-gate case DDI_PROP_TYPE_INT64:
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate * Technically, we need byte-swapping to conform to 1275.
560Sstevel@tonic-gate * However, the old x86 prom simulator used little endian
570Sstevel@tonic-gate * representation, so we don't swap here either.
580Sstevel@tonic-gate *
590Sstevel@tonic-gate * NOTE: this is inconsistent with ddi_prop_lookup_*()
600Sstevel@tonic-gate * which does byte-swapping when looking up prom properties.
610Sstevel@tonic-gate * Since all kernel nodes are SID nodes, drivers no longer
620Sstevel@tonic-gate * access PROM properties on x86.
630Sstevel@tonic-gate */
640Sstevel@tonic-gate default: /* no byte swapping */
650Sstevel@tonic-gate (void) bcopy(val, q->pp_val, len);
660Sstevel@tonic-gate break;
670Sstevel@tonic-gate }
680Sstevel@tonic-gate
690Sstevel@tonic-gate if (pnp->pn_propp == NULL) {
700Sstevel@tonic-gate pnp->pn_propp = q;
710Sstevel@tonic-gate return;
720Sstevel@tonic-gate }
730Sstevel@tonic-gate
740Sstevel@tonic-gate for (p = pnp->pn_propp; p->pp_next != NULL; p = p->pp_next)
750Sstevel@tonic-gate /* empty */;
760Sstevel@tonic-gate
770Sstevel@tonic-gate p->pp_next = q;
780Sstevel@tonic-gate }
790Sstevel@tonic-gate
800Sstevel@tonic-gate static prom_node_t *
promif_create_node(dev_info_t * dip)810Sstevel@tonic-gate promif_create_node(dev_info_t *dip)
820Sstevel@tonic-gate {
830Sstevel@tonic-gate prom_node_t *pnp;
840Sstevel@tonic-gate ddi_prop_t *hwprop;
850Sstevel@tonic-gate char *nodename;
860Sstevel@tonic-gate
870Sstevel@tonic-gate pnp = kmem_zalloc(sizeof (prom_node_t), KM_SLEEP);
880Sstevel@tonic-gate pnp->pn_nodeid = DEVI(dip)->devi_nodeid;
890Sstevel@tonic-gate
900Sstevel@tonic-gate hwprop = DEVI(dip)->devi_hw_prop_ptr;
910Sstevel@tonic-gate while (hwprop != NULL) {
920Sstevel@tonic-gate /* need to encode to proper endianness */
930Sstevel@tonic-gate promif_create_prop(pnp, hwprop->prop_name, hwprop->prop_val,
940Sstevel@tonic-gate hwprop->prop_len, hwprop->prop_flags & DDI_PROP_TYPE_MASK);
950Sstevel@tonic-gate hwprop = hwprop->prop_next;
960Sstevel@tonic-gate }
970Sstevel@tonic-gate nodename = ddi_node_name(dip);
980Sstevel@tonic-gate promif_create_prop(pnp, "name", nodename, strlen(nodename) + 1,
990Sstevel@tonic-gate DDI_PROP_TYPE_STRING);
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate return (pnp);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate static void promif_create_children(prom_node_t *, dev_info_t *);
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate static void
promif_create_peers(prom_node_t * pnp,dev_info_t * dip)1070Sstevel@tonic-gate promif_create_peers(prom_node_t *pnp, dev_info_t *dip)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate dev_info_t *ndip = ddi_get_next_sibling(dip);
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate while (ndip) {
1120Sstevel@tonic-gate pnp->pn_sibling = promif_create_node(ndip);
1130Sstevel@tonic-gate promif_create_children(pnp->pn_sibling, ndip);
1140Sstevel@tonic-gate pnp = pnp->pn_sibling;
1150Sstevel@tonic-gate ndip = ddi_get_next_sibling(ndip);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate static void
promif_create_children(prom_node_t * pnp,dev_info_t * dip)1200Sstevel@tonic-gate promif_create_children(prom_node_t *pnp, dev_info_t *dip)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate dev_info_t *cdip = ddi_get_child(dip);
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate while (cdip) {
1250Sstevel@tonic-gate pnp->pn_child = promif_create_node(cdip);
1260Sstevel@tonic-gate promif_create_peers(pnp->pn_child, cdip);
1270Sstevel@tonic-gate pnp = pnp->pn_child;
1280Sstevel@tonic-gate cdip = ddi_get_child(cdip);
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate void
promif_create_device_tree(void)1330Sstevel@tonic-gate promif_create_device_tree(void)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate promif_top = promif_create_node(ddi_root_node());
1360Sstevel@tonic-gate promif_create_children(promif_top, ddi_root_node());
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
1390Sstevel@tonic-gate static prom_node_t *
find_node_work(prom_node_t * pnp,pnode_t n)140*789Sahrens find_node_work(prom_node_t *pnp, pnode_t n)
1410Sstevel@tonic-gate {
1420Sstevel@tonic-gate prom_node_t *qnp;
1430Sstevel@tonic-gate
1440Sstevel@tonic-gate if (pnp->pn_nodeid == n)
1450Sstevel@tonic-gate return (pnp);
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate if (pnp->pn_child)
1480Sstevel@tonic-gate if ((qnp = find_node_work(pnp->pn_child, n)) != NULL)
1490Sstevel@tonic-gate return (qnp);
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate if (pnp->pn_sibling)
1520Sstevel@tonic-gate if ((qnp = find_node_work(pnp->pn_sibling, n)) != NULL)
1530Sstevel@tonic-gate return (qnp);
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate return (NULL);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate static prom_node_t *
promif_find_node(pnode_t nodeid)159*789Sahrens promif_find_node(pnode_t nodeid)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate if (nodeid == OBP_NONODE)
1620Sstevel@tonic-gate return (promif_top);
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate if (promif_top == NULL)
1650Sstevel@tonic-gate return (NULL);
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate return (find_node_work(promif_top, nodeid));
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
170*789Sahrens pnode_t
promif_nextnode(pnode_t nodeid)171*789Sahrens promif_nextnode(pnode_t nodeid)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate prom_node_t *pnp;
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate /*
1760Sstevel@tonic-gate * Note: next(0) returns the root node
1770Sstevel@tonic-gate */
1780Sstevel@tonic-gate pnp = promif_find_node(nodeid);
1790Sstevel@tonic-gate if (pnp && (nodeid == OBP_NONODE))
1800Sstevel@tonic-gate return (pnp->pn_nodeid);
1810Sstevel@tonic-gate if (pnp && pnp->pn_sibling)
1820Sstevel@tonic-gate return (pnp->pn_sibling->pn_nodeid);
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate return (OBP_NONODE);
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
187*789Sahrens pnode_t
promif_childnode(pnode_t nodeid)188*789Sahrens promif_childnode(pnode_t nodeid)
1890Sstevel@tonic-gate {
1900Sstevel@tonic-gate prom_node_t *pnp;
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate pnp = promif_find_node(nodeid);
1930Sstevel@tonic-gate if (pnp && pnp->pn_child)
1940Sstevel@tonic-gate return (pnp->pn_child->pn_nodeid);
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate return (OBP_NONODE);
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate * Retrieve a PROM property (len and value)
2010Sstevel@tonic-gate */
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate static int
getproplen(prom_node_t * pnp,char * name)2040Sstevel@tonic-gate getproplen(prom_node_t *pnp, char *name)
2050Sstevel@tonic-gate {
2060Sstevel@tonic-gate struct prom_prop *propp;
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate for (propp = pnp->pn_propp; propp != NULL; propp = propp->pp_next)
2090Sstevel@tonic-gate if (strcmp(propp->pp_name, name) == 0)
2100Sstevel@tonic-gate return (propp->pp_len);
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate return (-1);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate int
promif_getproplen(pnode_t nodeid,char * name)216*789Sahrens promif_getproplen(pnode_t nodeid, char *name)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate prom_node_t *pnp;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate pnp = promif_find_node(nodeid);
2210Sstevel@tonic-gate if (pnp == NULL)
2220Sstevel@tonic-gate return (-1);
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate return (getproplen(pnp, name));
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate static void *
getprop(prom_node_t * pnp,char * name)2280Sstevel@tonic-gate getprop(prom_node_t *pnp, char *name)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate struct prom_prop *propp;
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate for (propp = pnp->pn_propp; propp != NULL; propp = propp->pp_next)
2330Sstevel@tonic-gate if (strcmp(propp->pp_name, name) == 0)
2340Sstevel@tonic-gate return (propp->pp_val);
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate return (NULL);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate int
promif_getprop(pnode_t nodeid,char * name,void * value)240*789Sahrens promif_getprop(pnode_t nodeid, char *name, void *value)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate prom_node_t *pnp;
2430Sstevel@tonic-gate void *v;
2440Sstevel@tonic-gate int len;
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate pnp = promif_find_node(nodeid);
2470Sstevel@tonic-gate if (pnp == NULL)
2480Sstevel@tonic-gate return (-1);
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate len = getproplen(pnp, name);
2510Sstevel@tonic-gate if (len > 0) {
2520Sstevel@tonic-gate v = getprop(pnp, name);
2530Sstevel@tonic-gate bcopy(v, value, len);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate return (len);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate static char *
nextprop(prom_node_t * pnp,char * name)2590Sstevel@tonic-gate nextprop(prom_node_t *pnp, char *name)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate struct prom_prop *propp;
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate * getting next of NULL or a null string returns the first prop name
2650Sstevel@tonic-gate */
2660Sstevel@tonic-gate if (name == NULL || *name == '\0')
2670Sstevel@tonic-gate if (pnp->pn_propp)
2680Sstevel@tonic-gate return (pnp->pn_propp->pp_name);
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate for (propp = pnp->pn_propp; propp != NULL; propp = propp->pp_next)
2710Sstevel@tonic-gate if (strcmp(propp->pp_name, name) == 0)
2720Sstevel@tonic-gate if (propp->pp_next)
2730Sstevel@tonic-gate return (propp->pp_next->pp_name);
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate return (NULL);
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate char *
promif_nextprop(pnode_t nodeid,char * name,char * next)279*789Sahrens promif_nextprop(pnode_t nodeid, char *name, char *next)
2800Sstevel@tonic-gate {
2810Sstevel@tonic-gate prom_node_t *pnp;
2820Sstevel@tonic-gate char *s;
2830Sstevel@tonic-gate
2840Sstevel@tonic-gate next[0] = '\0';
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate pnp = promif_find_node(nodeid);
2870Sstevel@tonic-gate if (pnp == NULL)
2880Sstevel@tonic-gate return (NULL);
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate s = nextprop(pnp, name);
2910Sstevel@tonic-gate if (s == NULL)
2920Sstevel@tonic-gate return (next);
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate (void) strcpy(next, s);
2950Sstevel@tonic-gate return (next);
2960Sstevel@tonic-gate }
297