1 /* $NetBSD: Locore.c,v 1.7 2000/08/20 07:04:59 tsubai Exp $ */
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
34 * Copyright (C) 2000 Benno Rice.
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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.
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
61 #include "opt_platform.h"
63 #include <sys/param.h>
64 #include <sys/kernel.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>
72 #include <machine/stdarg.h>
74 #include <dev/ofw/ofwvar.h>
75 #include <dev/ofw/openfirm.h>
79 static void OF_putchar(int c, void *arg);
81 MALLOC_DEFINE(M_OFWPROP, "openfirm", "Open Firmware properties");
83 static ihandle_t stdout;
85 static ofw_def_t *ofw_def_impl = NULL;
87 static struct ofw_kobj ofw_kernel_obj;
88 static struct kobj_ops ofw_kernel_kops;
94 SLIST_ENTRY(xrefinfo) next_entry;
97 static SLIST_HEAD(, xrefinfo) xreflist = SLIST_HEAD_INITIALIZER(xreflist);
98 static struct mtx xreflist_lock;
99 static boolean_t xref_init_done;
101 #define FIND_BY_XREF 0
102 #define FIND_BY_NODE 1
103 #define FIND_BY_DEV 2
106 * xref-phandle-device lookup helper routines.
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.
117 xrefinfo_create(phandle_t node)
119 struct xrefinfo * xi;
120 phandle_t child, xref;
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.
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)
134 xi = malloc(sizeof(*xi), M_OFWPROP, M_WAITOK | M_ZERO);
137 SLIST_INSERT_HEAD(&xreflist, xi, next_entry);
142 xrefinfo_init(void *unsed)
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.
150 mtx_init(&xreflist_lock, "OF xreflist lock", NULL, MTX_DEF);
151 xrefinfo_create(OF_peer(0));
152 xref_init_done = true;
154 SYSINIT(xrefinfo, SI_SUB_KMEM, SI_ORDER_ANY, xrefinfo_init, NULL);
156 static struct xrefinfo *
157 xrefinfo_find(uintptr_t key, int find_by)
159 struct xrefinfo *rv, *xi;
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)) {
171 mtx_unlock(&xreflist_lock);
175 static struct xrefinfo *
176 xrefinfo_add(phandle_t node, phandle_t xref, device_t dev)
180 xi = malloc(sizeof(*xi), M_OFWPROP, M_WAITOK);
184 mtx_lock(&xreflist_lock);
185 SLIST_INSERT_HEAD(&xreflist, xi, next_entry);
186 mtx_unlock(&xreflist_lock);
191 * OFW install routines. Highest priority wins, equal priority also
192 * overrides allowing last-set to win.
194 SET_DECLARE(ofw_set, ofw_def_t);
197 OF_install(char *name, int prio)
199 ofw_def_t *ofwp, **ofwpp;
200 static int curr_prio = 0;
203 * Try and locate the OFW kobj corresponding to the name.
205 SET_FOREACH(ofwpp, ofw_set) {
209 !strcmp(ofwp->name, name) &&
222 OF_init(void *cookie)
227 if (ofw_def_impl == NULL)
230 ofw_obj = &ofw_kernel_obj;
232 * Take care of compiling the selected class, and
233 * then statically initialize the OFW object.
235 kobj_class_compile_static(ofw_def_impl, &ofw_kernel_kops);
236 kobj_init_static((kobj_t)ofw_obj, ofw_def_impl);
238 rv = OFW_INIT(ofw_obj, cookie);
240 if ((chosen = OF_finddevice("/chosen")) != -1)
241 if (OF_getencprop(chosen, "stdout", &stdout,
242 sizeof(stdout)) == -1)
249 OF_putchar(int c, void *arg __unused)
255 OF_write(stdout, &cbuf, 1);
259 OF_write(stdout, &cbuf, 1);
263 OF_printf(const char *fmt, ...)
268 (void)kvprintf(fmt, OF_putchar, NULL, 10, va);
276 /* Test to see if a service exists. */
278 OF_test(const char *name)
281 if (ofw_def_impl == NULL)
284 return (OFW_TEST(ofw_obj, name));
288 OF_interpret(const char *cmd, int nreturns, ...)
295 if (ofw_def_impl == NULL)
298 status = OFW_INTERPRET(ofw_obj, cmd, nreturns, slots);
302 va_start(ap, nreturns);
304 *va_arg(ap, cell_t *) = slots[i++];
311 * Device tree functions
314 /* Return the next sibling of this node or 0. */
316 OF_peer(phandle_t node)
319 if (ofw_def_impl == NULL)
322 return (OFW_PEER(ofw_obj, node));
325 /* Return the first child of this node or 0. */
327 OF_child(phandle_t node)
330 if (ofw_def_impl == NULL)
333 return (OFW_CHILD(ofw_obj, node));
336 /* Return the parent of this node or 0. */
338 OF_parent(phandle_t node)
341 if (ofw_def_impl == NULL)
344 return (OFW_PARENT(ofw_obj, node));
347 /* Return the package handle that corresponds to an instance handle. */
349 OF_instance_to_package(ihandle_t instance)
352 if (ofw_def_impl == NULL)
355 return (OFW_INSTANCE_TO_PACKAGE(ofw_obj, instance));
358 /* Get the length of a property of a package. */
360 OF_getproplen(phandle_t package, const char *propname)
363 if (ofw_def_impl == NULL)
366 return (OFW_GETPROPLEN(ofw_obj, package, propname));
369 /* Check existence of a property of a package. */
371 OF_hasprop(phandle_t package, const char *propname)
374 return (OF_getproplen(package, propname) >= 0 ? 1 : 0);
377 /* Get the value of a property of a package. */
379 OF_getprop(phandle_t package, const char *propname, void *buf, size_t buflen)
382 if (ofw_def_impl == NULL)
385 return (OFW_GETPROP(ofw_obj, package, propname, buf, buflen));
389 OF_getencprop(phandle_t node, const char *propname, pcell_t *buf, size_t len)
394 KASSERT(len % 4 == 0, ("Need a multiple of 4 bytes"));
396 retval = OF_getprop(node, propname, buf, len);
400 for (i = 0; i < len/4; i++)
401 buf[i] = be32toh(buf[i]);
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
412 OF_searchprop(phandle_t node, const char *propname, void *buf, size_t len)
416 for (; node != 0; node = OF_parent(node))
417 if ((rv = OF_getprop(node, propname, buf, len)) != -1)
423 OF_searchencprop(phandle_t node, const char *propname, void *buf, size_t len)
427 for (; node != 0; node = OF_parent(node))
428 if ((rv = OF_getencprop(node, propname, buf, len)) != -1)
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.
439 OF_getprop_alloc(phandle_t package, const char *propname, int elsz, void **buf)
444 if ((len = OF_getproplen(package, propname)) == -1 ||
448 *buf = malloc(len, M_OFWPROP, M_WAITOK);
449 if (OF_getprop(package, propname, *buf, len) == -1) {
450 free(*buf, M_OFWPROP);
458 OF_getencprop_alloc(phandle_t package, const char *name, int elsz, void **buf)
464 retval = OF_getprop_alloc(package, name, elsz, buf);
467 if (retval * elsz % 4 != 0) {
468 free(*buf, M_OFWPROP);
474 for (i = 0; i < retval * elsz / 4; i++)
475 cell[i] = be32toh(cell[i]);
480 /* Get the next property of a package. */
482 OF_nextprop(phandle_t package, const char *previous, char *buf, size_t size)
485 if (ofw_def_impl == NULL)
488 return (OFW_NEXTPROP(ofw_obj, package, previous, buf, size));
491 /* Set the value of a property of a package. */
493 OF_setprop(phandle_t package, const char *propname, const void *buf, size_t len)
496 if (ofw_def_impl == NULL)
499 return (OFW_SETPROP(ofw_obj, package, propname, buf,len));
502 /* Convert a device specifier to a fully qualified pathname. */
504 OF_canon(const char *device, char *buf, size_t len)
507 if (ofw_def_impl == NULL)
510 return (OFW_CANON(ofw_obj, device, buf, len));
513 /* Return a package handle for the specified device. */
515 OF_finddevice(const char *device)
518 if (ofw_def_impl == NULL)
521 return (OFW_FINDDEVICE(ofw_obj, device));
524 /* Return the fully qualified pathname corresponding to an instance. */
526 OF_instance_to_path(ihandle_t instance, char *buf, size_t len)
529 if (ofw_def_impl == NULL)
532 return (OFW_INSTANCE_TO_PATH(ofw_obj, instance, buf, len));
535 /* Return the fully qualified pathname corresponding to a package. */
537 OF_package_to_path(phandle_t package, char *buf, size_t len)
540 if (ofw_def_impl == NULL)
543 return (OFW_PACKAGE_TO_PATH(ofw_obj, package, buf, len));
546 /* Look up effective phandle (see FDT/PAPR spec) */
548 OF_child_xref_phandle(phandle_t parent, phandle_t xref)
550 phandle_t child, rxref;
553 * Recursively descend from parent, looking for a node with a property
554 * named either "phandle", "ibm,phandle", or "linux,phandle" that
555 * matches the xref we are looking for.
558 for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
559 rxref = OF_child_xref_phandle(child, xref);
563 if (OF_getencprop(child, "phandle", &rxref, sizeof(rxref)) ==
564 -1 && OF_getencprop(child, "ibm,phandle", &rxref,
565 sizeof(rxref)) == -1 && OF_getencprop(child,
566 "linux,phandle", &rxref, sizeof(rxref)) == -1)
577 OF_node_from_xref(phandle_t xref)
582 if (xref_init_done) {
583 if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
588 if ((node = OF_child_xref_phandle(OF_peer(0), xref)) == -1)
594 OF_xref_from_node(phandle_t node)
599 if (xref_init_done) {
600 if ((xi = xrefinfo_find(node, FIND_BY_NODE)) == NULL)
605 if (OF_getencprop(node, "phandle", &xref, sizeof(xref)) == -1 &&
606 OF_getencprop(node, "ibm,phandle", &xref, sizeof(xref)) == -1 &&
607 OF_getencprop(node, "linux,phandle", &xref, sizeof(xref)) == -1)
613 OF_device_from_xref(phandle_t xref)
617 if (xref_init_done) {
618 if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
622 panic("Attempt to find device before xreflist_init");
626 OF_xref_from_device(device_t dev)
630 if (xref_init_done) {
631 if ((xi = xrefinfo_find((uintptr_t)dev, FIND_BY_DEV)) == NULL)
635 panic("Attempt to find xref before xreflist_init");
639 OF_device_register_xref(phandle_t xref, device_t dev)
644 * If the given xref handle doesn't already exist in the list then we
645 * add a list entry. In theory this can only happen on a system where
646 * nodes don't contain phandle properties and xref and node handles are
647 * synonymous, so the xref handle is added as the node handle as well.
649 if (xref_init_done) {
650 if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
651 xrefinfo_add(xref, xref, dev);
656 panic("Attempt to register device before xreflist_init");
659 /* Call the method in the scope of a given instance. */
661 OF_call_method(const char *method, ihandle_t instance, int nargs, int nreturns,
665 cell_t args_n_results[12];
668 if (nargs > 6 || ofw_def_impl == NULL)
670 va_start(ap, nreturns);
671 for (n = 0; n < nargs; n++)
672 args_n_results[n] = va_arg(ap, cell_t);
674 status = OFW_CALL_METHOD(ofw_obj, instance, method, nargs, nreturns,
679 for (; n < nargs + nreturns; n++)
680 *va_arg(ap, cell_t *) = args_n_results[n];
686 * Device I/O functions
689 /* Open an instance for a device. */
691 OF_open(const char *device)
694 if (ofw_def_impl == NULL)
697 return (OFW_OPEN(ofw_obj, device));
700 /* Close an instance. */
702 OF_close(ihandle_t instance)
705 if (ofw_def_impl == NULL)
708 OFW_CLOSE(ofw_obj, instance);
711 /* Read from an instance. */
713 OF_read(ihandle_t instance, void *addr, size_t len)
716 if (ofw_def_impl == NULL)
719 return (OFW_READ(ofw_obj, instance, addr, len));
722 /* Write to an instance. */
724 OF_write(ihandle_t instance, const void *addr, size_t len)
727 if (ofw_def_impl == NULL)
730 return (OFW_WRITE(ofw_obj, instance, addr, len));
733 /* Seek to a position. */
735 OF_seek(ihandle_t instance, uint64_t pos)
738 if (ofw_def_impl == NULL)
741 return (OFW_SEEK(ofw_obj, instance, pos));
748 /* Claim an area of memory. */
750 OF_claim(void *virt, size_t size, u_int align)
753 if (ofw_def_impl == NULL)
756 return (OFW_CLAIM(ofw_obj, virt, size, align));
759 /* Release an area of memory. */
761 OF_release(void *virt, size_t size)
764 if (ofw_def_impl == NULL)
767 OFW_RELEASE(ofw_obj, virt, size);
771 * Control transfer functions
774 /* Suspend and drop back to the Open Firmware interface. */
779 if (ofw_def_impl == NULL)
785 /* Shut down and drop back to the Open Firmware interface. */
790 if (ofw_def_impl == NULL)
791 panic("OF_exit: Open Firmware not available");
793 /* Should not return */
796 for (;;) /* just in case */