1 /* $NetBSD: Locore.c,v 1.7 2000/08/20 07:04:59 tsubai Exp $ */
4 * SPDX-License-Identifier: BSD-4-Clause
6 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
7 * Copyright (C) 1995, 1996 TooLs GmbH.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
36 * Copyright (C) 2000 Benno Rice.
37 * All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
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.
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.
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
63 #include "opt_platform.h"
65 #include <sys/param.h>
66 #include <sys/kernel.h>
68 #include <sys/malloc.h>
69 #include <sys/mutex.h>
70 #include <sys/queue.h>
71 #include <sys/systm.h>
72 #include <sys/endian.h>
74 #include <machine/stdarg.h>
76 #include <dev/ofw/ofwvar.h>
77 #include <dev/ofw/openfirm.h>
81 static void OF_putchar(int c, void *arg);
83 MALLOC_DEFINE(M_OFWPROP, "openfirm", "Open Firmware properties");
85 static ihandle_t stdout;
87 static ofw_def_t *ofw_def_impl = NULL;
89 static struct ofw_kobj ofw_kernel_obj;
90 static struct kobj_ops ofw_kernel_kops;
96 SLIST_ENTRY(xrefinfo) next_entry;
99 static SLIST_HEAD(, xrefinfo) xreflist = SLIST_HEAD_INITIALIZER(xreflist);
100 static struct mtx xreflist_lock;
101 static boolean_t xref_init_done;
103 #define FIND_BY_XREF 0
104 #define FIND_BY_NODE 1
105 #define FIND_BY_DEV 2
108 * xref-phandle-device lookup helper routines.
110 * As soon as we are able to use malloc(), walk the node tree and build a list
111 * of info that cross-references node handles, xref handles, and device_t
112 * instances. This list exists primarily to allow association of a device_t
113 * with an xref handle, but it is also used to speed up translation between xref
114 * and node handles. Before malloc() is available we have to recursively search
115 * the node tree each time we want to translate between a node and xref handle.
116 * Afterwards we can do the translations by searching this much shorter list.
119 xrefinfo_create(phandle_t node)
121 struct xrefinfo * xi;
122 phandle_t child, xref;
125 * Recursively descend from parent, looking for nodes with a property
126 * named either "phandle", "ibm,phandle", or "linux,phandle". For each
127 * such node found create an entry in the xreflist.
129 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
130 xrefinfo_create(child);
131 if (OF_getencprop(child, "phandle", &xref, sizeof(xref)) ==
132 -1 && OF_getencprop(child, "ibm,phandle", &xref,
133 sizeof(xref)) == -1 && OF_getencprop(child,
134 "linux,phandle", &xref, sizeof(xref)) == -1)
136 xi = malloc(sizeof(*xi), M_OFWPROP, M_WAITOK | M_ZERO);
139 SLIST_INSERT_HEAD(&xreflist, xi, next_entry);
144 xrefinfo_init(void *unsed)
148 * There is no locking during this init because it runs much earlier
149 * than any of the clients/consumers of the xref list data, but we do
150 * initialize the mutex that will be used for access later.
152 mtx_init(&xreflist_lock, "OF xreflist lock", NULL, MTX_DEF);
153 xrefinfo_create(OF_peer(0));
154 xref_init_done = true;
156 SYSINIT(xrefinfo, SI_SUB_KMEM, SI_ORDER_ANY, xrefinfo_init, NULL);
158 static struct xrefinfo *
159 xrefinfo_find(uintptr_t key, int find_by)
161 struct xrefinfo *rv, *xi;
164 mtx_lock(&xreflist_lock);
165 SLIST_FOREACH(xi, &xreflist, next_entry) {
166 if ((find_by == FIND_BY_XREF && (phandle_t)key == xi->xref) ||
167 (find_by == FIND_BY_NODE && (phandle_t)key == xi->node) ||
168 (find_by == FIND_BY_DEV && key == (uintptr_t)xi->dev)) {
173 mtx_unlock(&xreflist_lock);
177 static struct xrefinfo *
178 xrefinfo_add(phandle_t node, phandle_t xref, device_t dev)
182 xi = malloc(sizeof(*xi), M_OFWPROP, M_WAITOK);
186 mtx_lock(&xreflist_lock);
187 SLIST_INSERT_HEAD(&xreflist, xi, next_entry);
188 mtx_unlock(&xreflist_lock);
193 * OFW install routines. Highest priority wins, equal priority also
194 * overrides allowing last-set to win.
196 SET_DECLARE(ofw_set, ofw_def_t);
199 OF_install(char *name, int prio)
201 ofw_def_t *ofwp, **ofwpp;
202 static int curr_prio = 0;
204 /* Allow OF layer to be uninstalled */
211 * Try and locate the OFW kobj corresponding to the name.
213 SET_FOREACH(ofwpp, ofw_set) {
217 !strcmp(ofwp->name, name) &&
230 OF_init(void *cookie)
235 if (ofw_def_impl == NULL)
238 ofw_obj = &ofw_kernel_obj;
240 * Take care of compiling the selected class, and
241 * then statically initialize the OFW object.
243 kobj_class_compile_static(ofw_def_impl, &ofw_kernel_kops);
244 kobj_init_static((kobj_t)ofw_obj, ofw_def_impl);
246 rv = OFW_INIT(ofw_obj, cookie);
248 if ((chosen = OF_finddevice("/chosen")) != -1)
249 if (OF_getencprop(chosen, "stdout", &stdout,
250 sizeof(stdout)) == -1)
257 OF_putchar(int c, void *arg __unused)
263 OF_write(stdout, &cbuf, 1);
267 OF_write(stdout, &cbuf, 1);
271 OF_printf(const char *fmt, ...)
276 (void)kvprintf(fmt, OF_putchar, NULL, 10, va);
284 /* Test to see if a service exists. */
286 OF_test(const char *name)
289 if (ofw_def_impl == NULL)
292 return (OFW_TEST(ofw_obj, name));
296 OF_interpret(const char *cmd, int nreturns, ...)
303 if (ofw_def_impl == NULL)
306 status = OFW_INTERPRET(ofw_obj, cmd, nreturns, slots);
310 va_start(ap, nreturns);
312 *va_arg(ap, cell_t *) = slots[i++];
319 * Device tree functions
322 /* Return the next sibling of this node or 0. */
324 OF_peer(phandle_t node)
327 if (ofw_def_impl == NULL)
330 return (OFW_PEER(ofw_obj, node));
333 /* Return the first child of this node or 0. */
335 OF_child(phandle_t node)
338 if (ofw_def_impl == NULL)
341 return (OFW_CHILD(ofw_obj, node));
344 /* Return the parent of this node or 0. */
346 OF_parent(phandle_t node)
349 if (ofw_def_impl == NULL)
352 return (OFW_PARENT(ofw_obj, node));
355 /* Return the package handle that corresponds to an instance handle. */
357 OF_instance_to_package(ihandle_t instance)
360 if (ofw_def_impl == NULL)
363 return (OFW_INSTANCE_TO_PACKAGE(ofw_obj, instance));
366 /* Get the length of a property of a package. */
368 OF_getproplen(phandle_t package, const char *propname)
371 if (ofw_def_impl == NULL)
374 return (OFW_GETPROPLEN(ofw_obj, package, propname));
377 /* Check existence of a property of a package. */
379 OF_hasprop(phandle_t package, const char *propname)
382 return (OF_getproplen(package, propname) >= 0 ? 1 : 0);
385 /* Get the value of a property of a package. */
387 OF_getprop(phandle_t package, const char *propname, void *buf, size_t buflen)
390 if (ofw_def_impl == NULL)
393 return (OFW_GETPROP(ofw_obj, package, propname, buf, buflen));
397 OF_getencprop(phandle_t node, const char *propname, pcell_t *buf, size_t len)
402 KASSERT(len % 4 == 0, ("Need a multiple of 4 bytes"));
404 retval = OF_getprop(node, propname, buf, len);
408 for (i = 0; i < len/4; i++)
409 buf[i] = be32toh(buf[i]);
415 * Recursively search the node and its parent for the given property, working
416 * downward from the node to the device tree root. Returns the value of the
420 OF_searchprop(phandle_t node, const char *propname, void *buf, size_t len)
424 for (; node != 0; node = OF_parent(node))
425 if ((rv = OF_getprop(node, propname, buf, len)) != -1)
431 OF_searchencprop(phandle_t node, const char *propname, pcell_t *buf, size_t len)
435 for (; node != 0; node = OF_parent(node))
436 if ((rv = OF_getencprop(node, propname, buf, len)) != -1)
442 * Store the value of a property of a package into newly allocated memory
443 * (using the M_OFWPROP malloc pool and M_WAITOK).
446 OF_getprop_alloc(phandle_t package, const char *propname, void **buf)
451 if ((len = OF_getproplen(package, propname)) == -1)
455 *buf = malloc(len, M_OFWPROP, M_WAITOK);
456 if (OF_getprop(package, propname, *buf, len) == -1) {
457 free(*buf, M_OFWPROP);
466 * Store the value of a property of a package into newly allocated memory
467 * (using the M_OFWPROP malloc pool and M_WAITOK). elsz is the size of a
468 * single element, the number of elements is return in number.
471 OF_getprop_alloc_multi(phandle_t package, const char *propname, int elsz, void **buf)
476 if ((len = OF_getproplen(package, propname)) == -1 ||
481 *buf = malloc(len, M_OFWPROP, M_WAITOK);
482 if (OF_getprop(package, propname, *buf, len) == -1) {
483 free(*buf, M_OFWPROP);
492 OF_getencprop_alloc(phandle_t package, const char *name, void **buf)
496 ret = OF_getencprop_alloc_multi(package, name, sizeof(pcell_t),
501 return (ret * sizeof(pcell_t));
505 OF_getencprop_alloc_multi(phandle_t package, const char *name, int elsz,
512 retval = OF_getprop_alloc_multi(package, name, elsz, buf);
517 for (i = 0; i < retval * elsz / 4; i++)
518 cell[i] = be32toh(cell[i]);
523 /* Free buffer allocated by OF_getencprop_alloc or OF_getprop_alloc */
524 void OF_prop_free(void *buf)
527 free(buf, M_OFWPROP);
530 /* Get the next property of a package. */
532 OF_nextprop(phandle_t package, const char *previous, char *buf, size_t size)
535 if (ofw_def_impl == NULL)
538 return (OFW_NEXTPROP(ofw_obj, package, previous, buf, size));
541 /* Set the value of a property of a package. */
543 OF_setprop(phandle_t package, const char *propname, const void *buf, size_t len)
546 if (ofw_def_impl == NULL)
549 return (OFW_SETPROP(ofw_obj, package, propname, buf,len));
552 /* Convert a device specifier to a fully qualified pathname. */
554 OF_canon(const char *device, char *buf, size_t len)
557 if (ofw_def_impl == NULL)
560 return (OFW_CANON(ofw_obj, device, buf, len));
563 /* Return a package handle for the specified device. */
565 OF_finddevice(const char *device)
568 if (ofw_def_impl == NULL)
571 return (OFW_FINDDEVICE(ofw_obj, device));
574 /* Return the fully qualified pathname corresponding to an instance. */
576 OF_instance_to_path(ihandle_t instance, char *buf, size_t len)
579 if (ofw_def_impl == NULL)
582 return (OFW_INSTANCE_TO_PATH(ofw_obj, instance, buf, len));
585 /* Return the fully qualified pathname corresponding to a package. */
587 OF_package_to_path(phandle_t package, char *buf, size_t len)
590 if (ofw_def_impl == NULL)
593 return (OFW_PACKAGE_TO_PATH(ofw_obj, package, buf, len));
596 /* Look up effective phandle (see FDT/PAPR spec) */
598 OF_child_xref_phandle(phandle_t parent, phandle_t xref)
600 phandle_t child, rxref;
603 * Recursively descend from parent, looking for a node with a property
604 * named either "phandle", "ibm,phandle", or "linux,phandle" that
605 * matches the xref we are looking for.
608 for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
609 rxref = OF_child_xref_phandle(child, xref);
613 if (OF_getencprop(child, "phandle", &rxref, sizeof(rxref)) ==
614 -1 && OF_getencprop(child, "ibm,phandle", &rxref,
615 sizeof(rxref)) == -1 && OF_getencprop(child,
616 "linux,phandle", &rxref, sizeof(rxref)) == -1)
627 OF_node_from_xref(phandle_t xref)
632 if (xref_init_done) {
633 if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
638 if ((node = OF_child_xref_phandle(OF_peer(0), xref)) == -1)
644 OF_xref_from_node(phandle_t node)
649 if (xref_init_done) {
650 if ((xi = xrefinfo_find(node, FIND_BY_NODE)) == NULL)
655 if (OF_getencprop(node, "phandle", &xref, sizeof(xref)) == -1 &&
656 OF_getencprop(node, "ibm,phandle", &xref, sizeof(xref)) == -1 &&
657 OF_getencprop(node, "linux,phandle", &xref, sizeof(xref)) == -1)
663 OF_device_from_xref(phandle_t xref)
667 if (xref_init_done) {
668 if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
672 panic("Attempt to find device before xreflist_init");
676 OF_xref_from_device(device_t dev)
680 if (xref_init_done) {
681 if ((xi = xrefinfo_find((uintptr_t)dev, FIND_BY_DEV)) == NULL)
685 panic("Attempt to find xref before xreflist_init");
689 OF_device_register_xref(phandle_t xref, device_t dev)
694 * If the given xref handle doesn't already exist in the list then we
695 * add a list entry. In theory this can only happen on a system where
696 * nodes don't contain phandle properties and xref and node handles are
697 * synonymous, so the xref handle is added as the node handle as well.
699 if (xref_init_done) {
700 if ((xi = xrefinfo_find(xref, FIND_BY_XREF)) == NULL)
701 xrefinfo_add(xref, xref, dev);
706 panic("Attempt to register device before xreflist_init");
709 /* Call the method in the scope of a given instance. */
711 OF_call_method(const char *method, ihandle_t instance, int nargs, int nreturns,
715 cell_t args_n_results[12];
718 if (nargs > 6 || ofw_def_impl == NULL)
720 va_start(ap, nreturns);
721 for (n = 0; n < nargs; n++)
722 args_n_results[n] = va_arg(ap, cell_t);
724 status = OFW_CALL_METHOD(ofw_obj, instance, method, nargs, nreturns,
729 for (; n < nargs + nreturns; n++)
730 *va_arg(ap, cell_t *) = args_n_results[n];
736 * Device I/O functions
739 /* Open an instance for a device. */
741 OF_open(const char *device)
744 if (ofw_def_impl == NULL)
747 return (OFW_OPEN(ofw_obj, device));
750 /* Close an instance. */
752 OF_close(ihandle_t instance)
755 if (ofw_def_impl == NULL)
758 OFW_CLOSE(ofw_obj, instance);
761 /* Read from an instance. */
763 OF_read(ihandle_t instance, void *addr, size_t len)
766 if (ofw_def_impl == NULL)
769 return (OFW_READ(ofw_obj, instance, addr, len));
772 /* Write to an instance. */
774 OF_write(ihandle_t instance, const void *addr, size_t len)
777 if (ofw_def_impl == NULL)
780 return (OFW_WRITE(ofw_obj, instance, addr, len));
783 /* Seek to a position. */
785 OF_seek(ihandle_t instance, uint64_t pos)
788 if (ofw_def_impl == NULL)
791 return (OFW_SEEK(ofw_obj, instance, pos));
798 /* Claim an area of memory. */
800 OF_claim(void *virt, size_t size, u_int align)
803 if (ofw_def_impl == NULL)
806 return (OFW_CLAIM(ofw_obj, virt, size, align));
809 /* Release an area of memory. */
811 OF_release(void *virt, size_t size)
814 if (ofw_def_impl == NULL)
817 OFW_RELEASE(ofw_obj, virt, size);
821 * Control transfer functions
824 /* Suspend and drop back to the Open Firmware interface. */
829 if (ofw_def_impl == NULL)
835 /* Shut down and drop back to the Open Firmware interface. */
840 if (ofw_def_impl == NULL)
841 panic("OF_exit: Open Firmware not available");
843 /* Should not return */
846 for (;;) /* just in case */