xref: /freebsd/sys/dev/ofw/openfirm.c (revision 273c26a3c3bea87a241d6879abd4f991db180bf0)
1 /*	$NetBSD: Locore.c,v 1.7 2000/08/20 07:04:59 tsubai Exp $	*/
2 
3 /*-
4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5  * Copyright (C) 1995, 1996 TooLs GmbH.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by TooLs GmbH.
19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 /*-
34  * Copyright (C) 2000 Benno Rice.
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  *
46  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
47  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
52  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
53  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
54  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
55  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  */
57 
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
60 
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 boolean_t 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
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
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 *
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 *
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 /*
191  * OFW install routines.  Highest priority wins, equal priority also
192  * overrides allowing last-set to win.
193  */
194 SET_DECLARE(ofw_set, ofw_def_t);
195 
196 boolean_t
197 OF_install(char *name, int prio)
198 {
199 	ofw_def_t *ofwp, **ofwpp;
200 	static int curr_prio = 0;
201 
202 	/*
203 	 * Try and locate the OFW kobj corresponding to the name.
204 	 */
205 	SET_FOREACH(ofwpp, ofw_set) {
206 		ofwp = *ofwpp;
207 
208 		if (ofwp->name &&
209 		    !strcmp(ofwp->name, name) &&
210 		    prio >= curr_prio) {
211 			curr_prio = prio;
212 			ofw_def_impl = ofwp;
213 			return (TRUE);
214 		}
215 	}
216 
217 	return (FALSE);
218 }
219 
220 /* Initializer */
221 int
222 OF_init(void *cookie)
223 {
224 	phandle_t chosen;
225 	int rv;
226 
227 	if (ofw_def_impl == NULL)
228 		return (-1);
229 
230 	ofw_obj = &ofw_kernel_obj;
231 	/*
232 	 * Take care of compiling the selected class, and
233 	 * then statically initialize the OFW object.
234 	 */
235 	kobj_class_compile_static(ofw_def_impl, &ofw_kernel_kops);
236 	kobj_init_static((kobj_t)ofw_obj, ofw_def_impl);
237 
238 	rv = OFW_INIT(ofw_obj, cookie);
239 
240 	if ((chosen = OF_finddevice("/chosen")) != -1)
241 		if (OF_getencprop(chosen, "stdout", &stdout,
242 		    sizeof(stdout)) == -1)
243 			stdout = -1;
244 
245 	return (rv);
246 }
247 
248 static void
249 OF_putchar(int c, void *arg __unused)
250 {
251 	char cbuf;
252 
253 	if (c == '\n') {
254 		cbuf = '\r';
255 		OF_write(stdout, &cbuf, 1);
256 	}
257 
258 	cbuf = c;
259 	OF_write(stdout, &cbuf, 1);
260 }
261 
262 void
263 OF_printf(const char *fmt, ...)
264 {
265 	va_list	va;
266 
267 	va_start(va, fmt);
268 	(void)kvprintf(fmt, OF_putchar, NULL, 10, va);
269 	va_end(va);
270 }
271 
272 /*
273  * Generic functions
274  */
275 
276 /* Test to see if a service exists. */
277 int
278 OF_test(const char *name)
279 {
280 
281 	if (ofw_def_impl == NULL)
282 		return (-1);
283 
284 	return (OFW_TEST(ofw_obj, name));
285 }
286 
287 int
288 OF_interpret(const char *cmd, int nreturns, ...)
289 {
290 	va_list ap;
291 	cell_t slots[16];
292 	int i = 0;
293 	int status;
294 
295 	if (ofw_def_impl == NULL)
296 		return (-1);
297 
298 	status = OFW_INTERPRET(ofw_obj, cmd, nreturns, slots);
299 	if (status == -1)
300 		return (status);
301 
302 	va_start(ap, nreturns);
303 	while (i < nreturns)
304 		*va_arg(ap, cell_t *) = slots[i++];
305 	va_end(ap);
306 
307 	return (status);
308 }
309 
310 /*
311  * Device tree functions
312  */
313 
314 /* Return the next sibling of this node or 0. */
315 phandle_t
316 OF_peer(phandle_t node)
317 {
318 
319 	if (ofw_def_impl == NULL)
320 		return (0);
321 
322 	return (OFW_PEER(ofw_obj, node));
323 }
324 
325 /* Return the first child of this node or 0. */
326 phandle_t
327 OF_child(phandle_t node)
328 {
329 
330 	if (ofw_def_impl == NULL)
331 		return (0);
332 
333 	return (OFW_CHILD(ofw_obj, node));
334 }
335 
336 /* Return the parent of this node or 0. */
337 phandle_t
338 OF_parent(phandle_t node)
339 {
340 
341 	if (ofw_def_impl == NULL)
342 		return (0);
343 
344 	return (OFW_PARENT(ofw_obj, node));
345 }
346 
347 /* Return the package handle that corresponds to an instance handle. */
348 phandle_t
349 OF_instance_to_package(ihandle_t instance)
350 {
351 
352 	if (ofw_def_impl == NULL)
353 		return (-1);
354 
355 	return (OFW_INSTANCE_TO_PACKAGE(ofw_obj, instance));
356 }
357 
358 /* Get the length of a property of a package. */
359 ssize_t
360 OF_getproplen(phandle_t package, const char *propname)
361 {
362 
363 	if (ofw_def_impl == NULL)
364 		return (-1);
365 
366 	return (OFW_GETPROPLEN(ofw_obj, package, propname));
367 }
368 
369 /* Check existence of a property of a package. */
370 int
371 OF_hasprop(phandle_t package, const char *propname)
372 {
373 
374 	return (OF_getproplen(package, propname) >= 0 ? 1 : 0);
375 }
376 
377 /* Get the value of a property of a package. */
378 ssize_t
379 OF_getprop(phandle_t package, const char *propname, void *buf, size_t buflen)
380 {
381 
382 	if (ofw_def_impl == NULL)
383 		return (-1);
384 
385 	return (OFW_GETPROP(ofw_obj, package, propname, buf, buflen));
386 }
387 
388 ssize_t
389 OF_getencprop(phandle_t node, const char *propname, pcell_t *buf, size_t len)
390 {
391 	ssize_t retval;
392 	int i;
393 
394 	KASSERT(len % 4 == 0, ("Need a multiple of 4 bytes"));
395 
396 	retval = OF_getprop(node, propname, buf, len);
397 	if (retval <= 0)
398 		return (retval);
399 
400 	for (i = 0; i < len/4; i++)
401 		buf[i] = be32toh(buf[i]);
402 
403 	return (retval);
404 }
405 
406 /*
407  * Recursively search the node and its parent for the given property, working
408  * downward from the node to the device tree root.  Returns the value of the
409  * first match.
410  */
411 ssize_t
412 OF_searchprop(phandle_t node, const char *propname, void *buf, size_t len)
413 {
414 	ssize_t rv;
415 
416 	for (; node != 0; node = OF_parent(node))
417 		if ((rv = OF_getprop(node, propname, buf, len)) != -1)
418 			return (rv);
419 	return (-1);
420 }
421 
422 ssize_t
423 OF_searchencprop(phandle_t node, const char *propname, void *buf, size_t len)
424 {
425 	ssize_t rv;
426 
427 	for (; node != 0; node = OF_parent(node))
428 		if ((rv = OF_getencprop(node, propname, buf, len)) != -1)
429 			return (rv);
430 	return (-1);
431 }
432 
433 /*
434  * Store the value of a property of a package into newly allocated memory
435  * (using the M_OFWPROP malloc pool and M_WAITOK).  elsz is the size of a
436  * single element, the number of elements is return in number.
437  */
438 ssize_t
439 OF_getprop_alloc(phandle_t package, const char *propname, int elsz, void **buf)
440 {
441 	int len;
442 
443 	*buf = NULL;
444 	if ((len = OF_getproplen(package, propname)) == -1 ||
445 	    len % elsz != 0)
446 		return (-1);
447 
448 	*buf = malloc(len, M_OFWPROP, M_WAITOK);
449 	if (OF_getprop(package, propname, *buf, len) == -1) {
450 		free(*buf, M_OFWPROP);
451 		*buf = NULL;
452 		return (-1);
453 	}
454 	return (len / elsz);
455 }
456 
457 ssize_t
458 OF_getencprop_alloc(phandle_t package, const char *name, int elsz, void **buf)
459 {
460 	ssize_t retval;
461 	pcell_t *cell;
462 	int i;
463 
464 	retval = OF_getprop_alloc(package, name, elsz, buf);
465 	if (retval == -1)
466 		return (-1);
467  	if (retval * elsz % 4 != 0) {
468 		free(*buf, M_OFWPROP);
469 		*buf = NULL;
470 		return (-1);
471 	}
472 
473 	cell = *buf;
474 	for (i = 0; i < retval * elsz / 4; i++)
475 		cell[i] = be32toh(cell[i]);
476 
477 	return (retval);
478 }
479 
480 /* Free buffer allocated by OF_getencprop_alloc or OF_getprop_alloc */
481 void OF_prop_free(void *buf)
482 {
483 
484 	free(buf, M_OFWPROP);
485 }
486 
487 /* Get the next property of a package. */
488 int
489 OF_nextprop(phandle_t package, const char *previous, char *buf, size_t size)
490 {
491 
492 	if (ofw_def_impl == NULL)
493 		return (-1);
494 
495 	return (OFW_NEXTPROP(ofw_obj, package, previous, buf, size));
496 }
497 
498 /* Set the value of a property of a package. */
499 int
500 OF_setprop(phandle_t package, const char *propname, const void *buf, size_t len)
501 {
502 
503 	if (ofw_def_impl == NULL)
504 		return (-1);
505 
506 	return (OFW_SETPROP(ofw_obj, package, propname, buf,len));
507 }
508 
509 /* Convert a device specifier to a fully qualified pathname. */
510 ssize_t
511 OF_canon(const char *device, char *buf, size_t len)
512 {
513 
514 	if (ofw_def_impl == NULL)
515 		return (-1);
516 
517 	return (OFW_CANON(ofw_obj, device, buf, len));
518 }
519 
520 /* Return a package handle for the specified device. */
521 phandle_t
522 OF_finddevice(const char *device)
523 {
524 
525 	if (ofw_def_impl == NULL)
526 		return (-1);
527 
528 	return (OFW_FINDDEVICE(ofw_obj, device));
529 }
530 
531 /* Return the fully qualified pathname corresponding to an instance. */
532 ssize_t
533 OF_instance_to_path(ihandle_t instance, char *buf, size_t len)
534 {
535 
536 	if (ofw_def_impl == NULL)
537 		return (-1);
538 
539 	return (OFW_INSTANCE_TO_PATH(ofw_obj, instance, buf, len));
540 }
541 
542 /* Return the fully qualified pathname corresponding to a package. */
543 ssize_t
544 OF_package_to_path(phandle_t package, char *buf, size_t len)
545 {
546 
547 	if (ofw_def_impl == NULL)
548 		return (-1);
549 
550 	return (OFW_PACKAGE_TO_PATH(ofw_obj, package, buf, len));
551 }
552 
553 /* Look up effective phandle (see FDT/PAPR spec) */
554 static phandle_t
555 OF_child_xref_phandle(phandle_t parent, phandle_t xref)
556 {
557 	phandle_t child, rxref;
558 
559 	/*
560 	 * Recursively descend from parent, looking for a node with a property
561 	 * named either "phandle", "ibm,phandle", or "linux,phandle" that
562 	 * matches the xref we are looking for.
563 	 */
564 
565 	for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
566 		rxref = OF_child_xref_phandle(child, xref);
567 		if (rxref != -1)
568 			return (rxref);
569 
570 		if (OF_getencprop(child, "phandle", &rxref, sizeof(rxref)) ==
571 		    -1 && OF_getencprop(child, "ibm,phandle", &rxref,
572 		    sizeof(rxref)) == -1 && OF_getencprop(child,
573 		    "linux,phandle", &rxref, sizeof(rxref)) == -1)
574 			continue;
575 
576 		if (rxref == xref)
577 			return (child);
578 	}
579 
580 	return (-1);
581 }
582 
583 phandle_t
584 OF_node_from_xref(phandle_t xref)
585 {
586 	struct xrefinfo *xi;
587 	phandle_t node;
588 
589 	if (xref_init_done) {
590 		if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
591 			return (xref);
592 		return (xi->node);
593 	}
594 
595 	if ((node = OF_child_xref_phandle(OF_peer(0), xref)) == -1)
596 		return (xref);
597 	return (node);
598 }
599 
600 phandle_t
601 OF_xref_from_node(phandle_t node)
602 {
603 	struct xrefinfo *xi;
604 	phandle_t xref;
605 
606 	if (xref_init_done) {
607 		if ((xi = xrefinfo_find(node, FIND_BY_NODE)) == NULL)
608 			return (node);
609 		return (xi->xref);
610 	}
611 
612 	if (OF_getencprop(node, "phandle", &xref, sizeof(xref)) == -1 &&
613 	    OF_getencprop(node, "ibm,phandle", &xref, sizeof(xref)) == -1 &&
614 	    OF_getencprop(node, "linux,phandle", &xref, sizeof(xref)) == -1)
615 		return (node);
616 	return (xref);
617 }
618 
619 device_t
620 OF_device_from_xref(phandle_t xref)
621 {
622 	struct xrefinfo *xi;
623 
624 	if (xref_init_done) {
625 		if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
626 			return (NULL);
627 		return (xi->dev);
628 	}
629 	panic("Attempt to find device before xreflist_init");
630 }
631 
632 phandle_t
633 OF_xref_from_device(device_t dev)
634 {
635 	struct xrefinfo *xi;
636 
637 	if (xref_init_done) {
638 		if ((xi = xrefinfo_find((uintptr_t)dev, FIND_BY_DEV)) == NULL)
639 			return (0);
640 		return (xi->xref);
641 	}
642 	panic("Attempt to find xref before xreflist_init");
643 }
644 
645 int
646 OF_device_register_xref(phandle_t xref, device_t dev)
647 {
648 	struct xrefinfo *xi;
649 
650 	/*
651 	 * If the given xref handle doesn't already exist in the list then we
652 	 * add a list entry.  In theory this can only happen on a system where
653 	 * nodes don't contain phandle properties and xref and node handles are
654 	 * synonymous, so the xref handle is added as the node handle as well.
655 	 */
656 	if (xref_init_done) {
657 		if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
658 			xrefinfo_add(xref, xref, dev);
659 		else
660 			xi->dev = dev;
661 		return (0);
662 	}
663 	panic("Attempt to register device before xreflist_init");
664 }
665 
666 /*  Call the method in the scope of a given instance. */
667 int
668 OF_call_method(const char *method, ihandle_t instance, int nargs, int nreturns,
669     ...)
670 {
671 	va_list ap;
672 	cell_t args_n_results[12];
673 	int n, status;
674 
675 	if (nargs > 6 || ofw_def_impl == NULL)
676 		return (-1);
677 	va_start(ap, nreturns);
678 	for (n = 0; n < nargs; n++)
679 		args_n_results[n] = va_arg(ap, cell_t);
680 
681 	status = OFW_CALL_METHOD(ofw_obj, instance, method, nargs, nreturns,
682 	    args_n_results);
683 	if (status != 0)
684 		return (status);
685 
686 	for (; n < nargs + nreturns; n++)
687 		*va_arg(ap, cell_t *) = args_n_results[n];
688 	va_end(ap);
689 	return (0);
690 }
691 
692 /*
693  * Device I/O functions
694  */
695 
696 /* Open an instance for a device. */
697 ihandle_t
698 OF_open(const char *device)
699 {
700 
701 	if (ofw_def_impl == NULL)
702 		return (0);
703 
704 	return (OFW_OPEN(ofw_obj, device));
705 }
706 
707 /* Close an instance. */
708 void
709 OF_close(ihandle_t instance)
710 {
711 
712 	if (ofw_def_impl == NULL)
713 		return;
714 
715 	OFW_CLOSE(ofw_obj, instance);
716 }
717 
718 /* Read from an instance. */
719 ssize_t
720 OF_read(ihandle_t instance, void *addr, size_t len)
721 {
722 
723 	if (ofw_def_impl == NULL)
724 		return (-1);
725 
726 	return (OFW_READ(ofw_obj, instance, addr, len));
727 }
728 
729 /* Write to an instance. */
730 ssize_t
731 OF_write(ihandle_t instance, const void *addr, size_t len)
732 {
733 
734 	if (ofw_def_impl == NULL)
735 		return (-1);
736 
737 	return (OFW_WRITE(ofw_obj, instance, addr, len));
738 }
739 
740 /* Seek to a position. */
741 int
742 OF_seek(ihandle_t instance, uint64_t pos)
743 {
744 
745 	if (ofw_def_impl == NULL)
746 		return (-1);
747 
748 	return (OFW_SEEK(ofw_obj, instance, pos));
749 }
750 
751 /*
752  * Memory functions
753  */
754 
755 /* Claim an area of memory. */
756 void *
757 OF_claim(void *virt, size_t size, u_int align)
758 {
759 
760 	if (ofw_def_impl == NULL)
761 		return ((void *)-1);
762 
763 	return (OFW_CLAIM(ofw_obj, virt, size, align));
764 }
765 
766 /* Release an area of memory. */
767 void
768 OF_release(void *virt, size_t size)
769 {
770 
771 	if (ofw_def_impl == NULL)
772 		return;
773 
774 	OFW_RELEASE(ofw_obj, virt, size);
775 }
776 
777 /*
778  * Control transfer functions
779  */
780 
781 /* Suspend and drop back to the Open Firmware interface. */
782 void
783 OF_enter()
784 {
785 
786 	if (ofw_def_impl == NULL)
787 		return;
788 
789 	OFW_ENTER(ofw_obj);
790 }
791 
792 /* Shut down and drop back to the Open Firmware interface. */
793 void
794 OF_exit()
795 {
796 
797 	if (ofw_def_impl == NULL)
798 		panic("OF_exit: Open Firmware not available");
799 
800 	/* Should not return */
801 	OFW_EXIT(ofw_obj);
802 
803 	for (;;)			/* just in case */
804 		;
805 }
806