1 /* $NetBSD: Locore.c,v 1.7 2000/08/20 07:04:59 tsubai Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
7 * Copyright (C) 1995, 1996 TooLs GmbH.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by TooLs GmbH.
21 * 4. The name of TooLs GmbH may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35 /*-
36 * Copyright (C) 2000 Benno Rice.
37 * All rights reserved.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 *
48 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
52 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
53 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
54 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
55 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
56 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
57 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 */
59
60 #include <sys/cdefs.h>
61 #include "opt_platform.h"
62
63 #include <sys/param.h>
64 #include <sys/kernel.h>
65 #include <sys/lock.h>
66 #include <sys/malloc.h>
67 #include <sys/mutex.h>
68 #include <sys/queue.h>
69 #include <sys/systm.h>
70 #include <sys/endian.h>
71
72 #include <machine/stdarg.h>
73
74 #include <dev/ofw/ofwvar.h>
75 #include <dev/ofw/openfirm.h>
76
77 #include "ofw_if.h"
78
79 static void OF_putchar(int c, void *arg);
80
81 MALLOC_DEFINE(M_OFWPROP, "openfirm", "Open Firmware properties");
82
83 static ihandle_t stdout;
84
85 static ofw_def_t *ofw_def_impl = NULL;
86 static ofw_t ofw_obj;
87 static struct ofw_kobj ofw_kernel_obj;
88 static struct kobj_ops ofw_kernel_kops;
89
90 struct xrefinfo {
91 phandle_t xref;
92 phandle_t node;
93 device_t dev;
94 SLIST_ENTRY(xrefinfo) next_entry;
95 };
96
97 static SLIST_HEAD(, xrefinfo) xreflist = SLIST_HEAD_INITIALIZER(xreflist);
98 static struct mtx xreflist_lock;
99 static bool xref_init_done;
100
101 #define FIND_BY_XREF 0
102 #define FIND_BY_NODE 1
103 #define FIND_BY_DEV 2
104
105 /*
106 * xref-phandle-device lookup helper routines.
107 *
108 * As soon as we are able to use malloc(), walk the node tree and build a list
109 * of info that cross-references node handles, xref handles, and device_t
110 * instances. This list exists primarily to allow association of a device_t
111 * with an xref handle, but it is also used to speed up translation between xref
112 * and node handles. Before malloc() is available we have to recursively search
113 * the node tree each time we want to translate between a node and xref handle.
114 * Afterwards we can do the translations by searching this much shorter list.
115 */
116 static void
xrefinfo_create(phandle_t node)117 xrefinfo_create(phandle_t node)
118 {
119 struct xrefinfo * xi;
120 phandle_t child, xref;
121
122 /*
123 * Recursively descend from parent, looking for nodes with a property
124 * named either "phandle", "ibm,phandle", or "linux,phandle". For each
125 * such node found create an entry in the xreflist.
126 */
127 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
128 xrefinfo_create(child);
129 if (OF_getencprop(child, "phandle", &xref, sizeof(xref)) ==
130 -1 && OF_getencprop(child, "ibm,phandle", &xref,
131 sizeof(xref)) == -1 && OF_getencprop(child,
132 "linux,phandle", &xref, sizeof(xref)) == -1)
133 continue;
134 xi = malloc(sizeof(*xi), M_OFWPROP, M_WAITOK | M_ZERO);
135 xi->node = child;
136 xi->xref = xref;
137 SLIST_INSERT_HEAD(&xreflist, xi, next_entry);
138 }
139 }
140
141 static void
xrefinfo_init(void * unsed)142 xrefinfo_init(void *unsed)
143 {
144
145 /*
146 * There is no locking during this init because it runs much earlier
147 * than any of the clients/consumers of the xref list data, but we do
148 * initialize the mutex that will be used for access later.
149 */
150 mtx_init(&xreflist_lock, "OF xreflist lock", NULL, MTX_DEF);
151 xrefinfo_create(OF_peer(0));
152 xref_init_done = true;
153 }
154 SYSINIT(xrefinfo, SI_SUB_KMEM, SI_ORDER_ANY, xrefinfo_init, NULL);
155
156 static struct xrefinfo *
xrefinfo_find(uintptr_t key,int find_by)157 xrefinfo_find(uintptr_t key, int find_by)
158 {
159 struct xrefinfo *rv, *xi;
160
161 rv = NULL;
162 mtx_lock(&xreflist_lock);
163 SLIST_FOREACH(xi, &xreflist, next_entry) {
164 if ((find_by == FIND_BY_XREF && (phandle_t)key == xi->xref) ||
165 (find_by == FIND_BY_NODE && (phandle_t)key == xi->node) ||
166 (find_by == FIND_BY_DEV && key == (uintptr_t)xi->dev)) {
167 rv = xi;
168 break;
169 }
170 }
171 mtx_unlock(&xreflist_lock);
172 return (rv);
173 }
174
175 static struct xrefinfo *
xrefinfo_add(phandle_t node,phandle_t xref,device_t dev)176 xrefinfo_add(phandle_t node, phandle_t xref, device_t dev)
177 {
178 struct xrefinfo *xi;
179
180 xi = malloc(sizeof(*xi), M_OFWPROP, M_WAITOK);
181 xi->node = node;
182 xi->xref = xref;
183 xi->dev = dev;
184 mtx_lock(&xreflist_lock);
185 SLIST_INSERT_HEAD(&xreflist, xi, next_entry);
186 mtx_unlock(&xreflist_lock);
187 return (xi);
188 }
189
190 static void
xrefinfo_remove(struct xrefinfo * xi)191 xrefinfo_remove(struct xrefinfo *xi)
192 {
193
194 mtx_lock(&xreflist_lock);
195 SLIST_REMOVE(&xreflist, xi, xrefinfo, next_entry);
196 mtx_unlock(&xreflist_lock);
197 }
198
199 /*
200 * OFW install routines. Highest priority wins, equal priority also
201 * overrides allowing last-set to win.
202 */
203 SET_DECLARE(ofw_set, ofw_def_t);
204
205 bool
OF_install(char * name,int prio)206 OF_install(char *name, int prio)
207 {
208 ofw_def_t *ofwp, **ofwpp;
209 static int curr_prio = 0;
210
211 /* Allow OF layer to be uninstalled */
212 if (name == NULL) {
213 ofw_def_impl = NULL;
214 return (false);
215 }
216
217 /*
218 * Try and locate the OFW kobj corresponding to the name.
219 */
220 SET_FOREACH(ofwpp, ofw_set) {
221 ofwp = *ofwpp;
222
223 if (ofwp->name &&
224 !strcmp(ofwp->name, name) &&
225 prio >= curr_prio) {
226 curr_prio = prio;
227 ofw_def_impl = ofwp;
228 return (true);
229 }
230 }
231
232 return (false);
233 }
234
235 /* Initializer */
236 int
OF_init(void * cookie)237 OF_init(void *cookie)
238 {
239 phandle_t chosen;
240 int rv;
241
242 if (ofw_def_impl == NULL)
243 return (-1);
244
245 ofw_obj = &ofw_kernel_obj;
246 /*
247 * Take care of compiling the selected class, and
248 * then statically initialize the OFW object.
249 */
250 kobj_class_compile_static(ofw_def_impl, &ofw_kernel_kops);
251 kobj_init_static((kobj_t)ofw_obj, ofw_def_impl);
252
253 rv = OFW_INIT(ofw_obj, cookie);
254
255 if ((chosen = OF_finddevice("/chosen")) != -1)
256 if (OF_getencprop(chosen, "stdout", &stdout,
257 sizeof(stdout)) == -1)
258 stdout = -1;
259
260 return (rv);
261 }
262
263 static void
OF_putchar(int c,void * arg __unused)264 OF_putchar(int c, void *arg __unused)
265 {
266 char cbuf;
267
268 if (c == '\n') {
269 cbuf = '\r';
270 OF_write(stdout, &cbuf, 1);
271 }
272
273 cbuf = c;
274 OF_write(stdout, &cbuf, 1);
275 }
276
277 void
OF_printf(const char * fmt,...)278 OF_printf(const char *fmt, ...)
279 {
280 va_list va;
281
282 va_start(va, fmt);
283 (void)kvprintf(fmt, OF_putchar, NULL, 10, va);
284 va_end(va);
285 }
286
287 /*
288 * Generic functions
289 */
290
291 /* Test to see if a service exists. */
292 int
OF_test(const char * name)293 OF_test(const char *name)
294 {
295
296 if (ofw_def_impl == NULL)
297 return (-1);
298
299 return (OFW_TEST(ofw_obj, name));
300 }
301
302 int
OF_interpret(const char * cmd,int nreturns,...)303 OF_interpret(const char *cmd, int nreturns, ...)
304 {
305 va_list ap;
306 cell_t slots[16];
307 int i = 0;
308 int status;
309
310 if (ofw_def_impl == NULL)
311 return (-1);
312
313 status = OFW_INTERPRET(ofw_obj, cmd, nreturns, slots);
314 if (status == -1)
315 return (status);
316
317 va_start(ap, nreturns);
318 while (i < nreturns)
319 *va_arg(ap, cell_t *) = slots[i++];
320 va_end(ap);
321
322 return (status);
323 }
324
325 /*
326 * Device tree functions
327 */
328
329 /* Return the next sibling of this node or 0. */
330 phandle_t
OF_peer(phandle_t node)331 OF_peer(phandle_t node)
332 {
333
334 if (ofw_def_impl == NULL)
335 return (0);
336
337 return (OFW_PEER(ofw_obj, node));
338 }
339
340 /* Return the first child of this node or 0. */
341 phandle_t
OF_child(phandle_t node)342 OF_child(phandle_t node)
343 {
344
345 if (ofw_def_impl == NULL)
346 return (0);
347
348 return (OFW_CHILD(ofw_obj, node));
349 }
350
351 /* Return the parent of this node or 0. */
352 phandle_t
OF_parent(phandle_t node)353 OF_parent(phandle_t node)
354 {
355
356 if (ofw_def_impl == NULL)
357 return (0);
358
359 return (OFW_PARENT(ofw_obj, node));
360 }
361
362 /* Return the package handle that corresponds to an instance handle. */
363 phandle_t
OF_instance_to_package(ihandle_t instance)364 OF_instance_to_package(ihandle_t instance)
365 {
366
367 if (ofw_def_impl == NULL)
368 return (-1);
369
370 return (OFW_INSTANCE_TO_PACKAGE(ofw_obj, instance));
371 }
372
373 /* Get the length of a property of a package. */
374 ssize_t
OF_getproplen(phandle_t package,const char * propname)375 OF_getproplen(phandle_t package, const char *propname)
376 {
377
378 if (ofw_def_impl == NULL)
379 return (-1);
380
381 return (OFW_GETPROPLEN(ofw_obj, package, propname));
382 }
383
384 /* Check existence of a property of a package. */
385 int
OF_hasprop(phandle_t package,const char * propname)386 OF_hasprop(phandle_t package, const char *propname)
387 {
388
389 return (OF_getproplen(package, propname) >= 0 ? 1 : 0);
390 }
391
392 /* Get the value of a property of a package. */
393 ssize_t
OF_getprop(phandle_t package,const char * propname,void * buf,size_t buflen)394 OF_getprop(phandle_t package, const char *propname, void *buf, size_t buflen)
395 {
396
397 if (ofw_def_impl == NULL)
398 return (-1);
399
400 return (OFW_GETPROP(ofw_obj, package, propname, buf, buflen));
401 }
402
403 ssize_t
OF_getencprop(phandle_t node,const char * propname,pcell_t * buf,size_t len)404 OF_getencprop(phandle_t node, const char *propname, pcell_t *buf, size_t len)
405 {
406 ssize_t retval;
407 int i;
408
409 KASSERT(len % 4 == 0, ("Need a multiple of 4 bytes"));
410
411 retval = OF_getprop(node, propname, buf, len);
412 if (retval <= 0)
413 return (retval);
414
415 for (i = 0; i < len/4; i++)
416 buf[i] = be32toh(buf[i]);
417
418 return (retval);
419 }
420
421 /*
422 * Recursively search the node and its parent for the given property, working
423 * downward from the node to the device tree root. Returns the value of the
424 * first match.
425 */
426 ssize_t
OF_searchprop(phandle_t node,const char * propname,void * buf,size_t len)427 OF_searchprop(phandle_t node, const char *propname, void *buf, size_t len)
428 {
429 ssize_t rv;
430
431 for (; node != 0; node = OF_parent(node))
432 if ((rv = OF_getprop(node, propname, buf, len)) != -1)
433 return (rv);
434 return (-1);
435 }
436
437 ssize_t
OF_searchencprop(phandle_t node,const char * propname,pcell_t * buf,size_t len)438 OF_searchencprop(phandle_t node, const char *propname, pcell_t *buf, size_t len)
439 {
440 ssize_t rv;
441
442 for (; node != 0; node = OF_parent(node))
443 if ((rv = OF_getencprop(node, propname, buf, len)) != -1)
444 return (rv);
445 return (-1);
446 }
447
448 /*
449 * Store the value of a property of a package into newly allocated memory
450 * (using the M_OFWPROP malloc pool and M_WAITOK).
451 */
452 ssize_t
OF_getprop_alloc(phandle_t package,const char * propname,void ** buf)453 OF_getprop_alloc(phandle_t package, const char *propname, void **buf)
454 {
455 int len;
456
457 *buf = NULL;
458 if ((len = OF_getproplen(package, propname)) == -1)
459 return (-1);
460
461 if (len > 0) {
462 *buf = malloc(len, M_OFWPROP, M_WAITOK);
463 if (OF_getprop(package, propname, *buf, len) == -1) {
464 free(*buf, M_OFWPROP);
465 *buf = NULL;
466 return (-1);
467 }
468 }
469 return (len);
470 }
471
472 /*
473 * Store the value of a property of a package into newly allocated memory
474 * (using the M_OFWPROP malloc pool and M_WAITOK). elsz is the size of a
475 * single element, the number of elements is return in number.
476 */
477 ssize_t
OF_getprop_alloc_multi(phandle_t package,const char * propname,int elsz,void ** buf)478 OF_getprop_alloc_multi(phandle_t package, const char *propname, int elsz, void **buf)
479 {
480 int len;
481
482 *buf = NULL;
483 if ((len = OF_getproplen(package, propname)) == -1 ||
484 len % elsz != 0)
485 return (-1);
486
487 if (len > 0) {
488 *buf = malloc(len, M_OFWPROP, M_WAITOK);
489 if (OF_getprop(package, propname, *buf, len) == -1) {
490 free(*buf, M_OFWPROP);
491 *buf = NULL;
492 return (-1);
493 }
494 }
495 return (len / elsz);
496 }
497
498 ssize_t
OF_getencprop_alloc(phandle_t package,const char * name,void ** buf)499 OF_getencprop_alloc(phandle_t package, const char *name, void **buf)
500 {
501 ssize_t ret;
502
503 ret = OF_getencprop_alloc_multi(package, name, sizeof(pcell_t),
504 buf);
505 if (ret < 0)
506 return (ret);
507 else
508 return (ret * sizeof(pcell_t));
509 }
510
511 ssize_t
OF_getencprop_alloc_multi(phandle_t package,const char * name,int elsz,void ** buf)512 OF_getencprop_alloc_multi(phandle_t package, const char *name, int elsz,
513 void **buf)
514 {
515 ssize_t retval;
516 pcell_t *cell;
517 int i;
518
519 retval = OF_getprop_alloc_multi(package, name, elsz, buf);
520 if (retval == -1)
521 return (-1);
522
523 cell = *buf;
524 for (i = 0; i < retval * elsz / 4; i++)
525 cell[i] = be32toh(cell[i]);
526
527 return (retval);
528 }
529
530 /* Free buffer allocated by OF_getencprop_alloc or OF_getprop_alloc */
OF_prop_free(void * buf)531 void OF_prop_free(void *buf)
532 {
533
534 free(buf, M_OFWPROP);
535 }
536
537 /* Get the next property of a package. */
538 int
OF_nextprop(phandle_t package,const char * previous,char * buf,size_t size)539 OF_nextprop(phandle_t package, const char *previous, char *buf, size_t size)
540 {
541
542 if (ofw_def_impl == NULL)
543 return (-1);
544
545 return (OFW_NEXTPROP(ofw_obj, package, previous, buf, size));
546 }
547
548 /* Set the value of a property of a package. */
549 int
OF_setprop(phandle_t package,const char * propname,const void * buf,size_t len)550 OF_setprop(phandle_t package, const char *propname, const void *buf, size_t len)
551 {
552
553 if (ofw_def_impl == NULL)
554 return (-1);
555
556 return (OFW_SETPROP(ofw_obj, package, propname, buf,len));
557 }
558
559 /* Convert a device specifier to a fully qualified pathname. */
560 ssize_t
OF_canon(const char * device,char * buf,size_t len)561 OF_canon(const char *device, char *buf, size_t len)
562 {
563
564 if (ofw_def_impl == NULL)
565 return (-1);
566
567 return (OFW_CANON(ofw_obj, device, buf, len));
568 }
569
570 /* Return a package handle for the specified device. */
571 phandle_t
OF_finddevice(const char * device)572 OF_finddevice(const char *device)
573 {
574
575 if (ofw_def_impl == NULL)
576 return (-1);
577
578 return (OFW_FINDDEVICE(ofw_obj, device));
579 }
580
581 /* Return the fully qualified pathname corresponding to an instance. */
582 ssize_t
OF_instance_to_path(ihandle_t instance,char * buf,size_t len)583 OF_instance_to_path(ihandle_t instance, char *buf, size_t len)
584 {
585
586 if (ofw_def_impl == NULL)
587 return (-1);
588
589 return (OFW_INSTANCE_TO_PATH(ofw_obj, instance, buf, len));
590 }
591
592 /* Return the fully qualified pathname corresponding to a package. */
593 ssize_t
OF_package_to_path(phandle_t package,char * buf,size_t len)594 OF_package_to_path(phandle_t package, char *buf, size_t len)
595 {
596
597 if (ofw_def_impl == NULL)
598 return (-1);
599
600 return (OFW_PACKAGE_TO_PATH(ofw_obj, package, buf, len));
601 }
602
603 /* Look up effective phandle (see FDT/PAPR spec) */
604 static phandle_t
OF_child_xref_phandle(phandle_t parent,phandle_t xref)605 OF_child_xref_phandle(phandle_t parent, phandle_t xref)
606 {
607 phandle_t child, rxref;
608
609 /*
610 * Recursively descend from parent, looking for a node with a property
611 * named either "phandle", "ibm,phandle", or "linux,phandle" that
612 * matches the xref we are looking for.
613 */
614
615 for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
616 rxref = OF_child_xref_phandle(child, xref);
617 if (rxref != -1)
618 return (rxref);
619
620 if (OF_getencprop(child, "phandle", &rxref, sizeof(rxref)) ==
621 -1 && OF_getencprop(child, "ibm,phandle", &rxref,
622 sizeof(rxref)) == -1 && OF_getencprop(child,
623 "linux,phandle", &rxref, sizeof(rxref)) == -1)
624 continue;
625
626 if (rxref == xref)
627 return (child);
628 }
629
630 return (-1);
631 }
632
633 phandle_t
OF_node_from_xref(phandle_t xref)634 OF_node_from_xref(phandle_t xref)
635 {
636 struct xrefinfo *xi;
637 phandle_t node;
638
639 if (xref_init_done) {
640 if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
641 return (xref);
642 return (xi->node);
643 }
644
645 if ((node = OF_child_xref_phandle(OF_peer(0), xref)) == -1)
646 return (xref);
647 return (node);
648 }
649
650 phandle_t
OF_xref_from_node(phandle_t node)651 OF_xref_from_node(phandle_t node)
652 {
653 struct xrefinfo *xi;
654 phandle_t xref;
655
656 if (xref_init_done) {
657 if ((xi = xrefinfo_find(node, FIND_BY_NODE)) == NULL)
658 return (node);
659 return (xi->xref);
660 }
661
662 if (OF_getencprop(node, "phandle", &xref, sizeof(xref)) == -1 &&
663 OF_getencprop(node, "ibm,phandle", &xref, sizeof(xref)) == -1 &&
664 OF_getencprop(node, "linux,phandle", &xref, sizeof(xref)) == -1)
665 return (node);
666 return (xref);
667 }
668
669 device_t
OF_device_from_xref(phandle_t xref)670 OF_device_from_xref(phandle_t xref)
671 {
672 struct xrefinfo *xi;
673
674 if (xref_init_done) {
675 if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
676 return (NULL);
677 return (xi->dev);
678 }
679 panic("Attempt to find device before xreflist_init");
680 }
681
682 phandle_t
OF_xref_from_device(device_t dev)683 OF_xref_from_device(device_t dev)
684 {
685 struct xrefinfo *xi;
686
687 if (xref_init_done) {
688 if ((xi = xrefinfo_find((uintptr_t)dev, FIND_BY_DEV)) == NULL)
689 return (0);
690 return (xi->xref);
691 }
692 panic("Attempt to find xref before xreflist_init");
693 }
694
695 int
OF_device_register_xref(phandle_t xref,device_t dev)696 OF_device_register_xref(phandle_t xref, device_t dev)
697 {
698 struct xrefinfo *xi;
699
700 /*
701 * If the given xref handle doesn't already exist in the list then we
702 * add a list entry. In theory this can only happen on a system where
703 * nodes don't contain phandle properties and xref and node handles are
704 * synonymous, so the xref handle is added as the node handle as well.
705 */
706 if (xref_init_done) {
707 if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
708 xrefinfo_add(xref, xref, dev);
709 else
710 xi->dev = dev;
711 return (0);
712 }
713 panic("Attempt to register device before xreflist_init");
714 }
715
716 void
OF_device_unregister_xref(phandle_t xref,device_t dev)717 OF_device_unregister_xref(phandle_t xref, device_t dev)
718 {
719 struct xrefinfo *xi;
720
721 if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
722 return;
723 xrefinfo_remove(xi);
724 }
725
726 /* Call the method in the scope of a given instance. */
727 int
OF_call_method(const char * method,ihandle_t instance,int nargs,int nreturns,...)728 OF_call_method(const char *method, ihandle_t instance, int nargs, int nreturns,
729 ...)
730 {
731 va_list ap;
732 cell_t args_n_results[12];
733 int n, status;
734
735 if (nargs > 6 || ofw_def_impl == NULL)
736 return (-1);
737 va_start(ap, nreturns);
738 for (n = 0; n < nargs; n++)
739 args_n_results[n] = va_arg(ap, cell_t);
740
741 status = OFW_CALL_METHOD(ofw_obj, instance, method, nargs, nreturns,
742 args_n_results);
743 if (status != 0)
744 return (status);
745
746 for (; n < nargs + nreturns; n++)
747 *va_arg(ap, cell_t *) = args_n_results[n];
748 va_end(ap);
749 return (0);
750 }
751
752 /*
753 * Device I/O functions
754 */
755
756 /* Open an instance for a device. */
757 ihandle_t
OF_open(const char * device)758 OF_open(const char *device)
759 {
760
761 if (ofw_def_impl == NULL)
762 return (0);
763
764 return (OFW_OPEN(ofw_obj, device));
765 }
766
767 /* Close an instance. */
768 void
OF_close(ihandle_t instance)769 OF_close(ihandle_t instance)
770 {
771
772 if (ofw_def_impl == NULL)
773 return;
774
775 OFW_CLOSE(ofw_obj, instance);
776 }
777
778 /* Read from an instance. */
779 ssize_t
OF_read(ihandle_t instance,void * addr,size_t len)780 OF_read(ihandle_t instance, void *addr, size_t len)
781 {
782
783 if (ofw_def_impl == NULL)
784 return (-1);
785
786 return (OFW_READ(ofw_obj, instance, addr, len));
787 }
788
789 /* Write to an instance. */
790 ssize_t
OF_write(ihandle_t instance,const void * addr,size_t len)791 OF_write(ihandle_t instance, const void *addr, size_t len)
792 {
793
794 if (ofw_def_impl == NULL)
795 return (-1);
796
797 return (OFW_WRITE(ofw_obj, instance, addr, len));
798 }
799
800 /* Seek to a position. */
801 int
OF_seek(ihandle_t instance,uint64_t pos)802 OF_seek(ihandle_t instance, uint64_t pos)
803 {
804
805 if (ofw_def_impl == NULL)
806 return (-1);
807
808 return (OFW_SEEK(ofw_obj, instance, pos));
809 }
810
811 /*
812 * Memory functions
813 */
814
815 /* Claim an area of memory. */
816 void *
OF_claim(void * virt,size_t size,u_int align)817 OF_claim(void *virt, size_t size, u_int align)
818 {
819
820 if (ofw_def_impl == NULL)
821 return ((void *)-1);
822
823 return (OFW_CLAIM(ofw_obj, virt, size, align));
824 }
825
826 /* Release an area of memory. */
827 void
OF_release(void * virt,size_t size)828 OF_release(void *virt, size_t size)
829 {
830
831 if (ofw_def_impl == NULL)
832 return;
833
834 OFW_RELEASE(ofw_obj, virt, size);
835 }
836
837 /*
838 * Control transfer functions
839 */
840
841 /* Suspend and drop back to the Open Firmware interface. */
842 void
OF_enter(void)843 OF_enter(void)
844 {
845
846 if (ofw_def_impl == NULL)
847 return;
848
849 OFW_ENTER(ofw_obj);
850 }
851
852 /* Shut down and drop back to the Open Firmware interface. */
853 void
OF_exit(void)854 OF_exit(void)
855 {
856
857 if (ofw_def_impl == NULL)
858 panic("OF_exit: Open Firmware not available");
859
860 /* Should not return */
861 OFW_EXIT(ofw_obj);
862
863 for (;;) /* just in case */
864 ;
865 }
866