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>
65 #include <sys/malloc.h>
66 #include <sys/systm.h>
67 #include <sys/endian.h>
69 #include <machine/stdarg.h>
71 #include <dev/ofw/ofwvar.h>
72 #include <dev/ofw/openfirm.h>
76 static void OF_putchar(int c, void *arg);
78 MALLOC_DEFINE(M_OFWPROP, "openfirm", "Open Firmware properties");
80 static ihandle_t stdout;
82 static ofw_def_t *ofw_def_impl = NULL;
84 static struct ofw_kobj ofw_kernel_obj;
85 static struct kobj_ops ofw_kernel_kops;
88 * OFW install routines. Highest priority wins, equal priority also
89 * overrides allowing last-set to win.
91 SET_DECLARE(ofw_set, ofw_def_t);
94 OF_install(char *name, int prio)
96 ofw_def_t *ofwp, **ofwpp;
97 static int curr_prio = 0;
100 * Try and locate the OFW kobj corresponding to the name.
102 SET_FOREACH(ofwpp, ofw_set) {
106 !strcmp(ofwp->name, name) &&
119 OF_init(void *cookie)
124 if (ofw_def_impl == NULL)
127 ofw_obj = &ofw_kernel_obj;
129 * Take care of compiling the selected class, and
130 * then statically initialize the OFW object.
132 kobj_class_compile_static(ofw_def_impl, &ofw_kernel_kops);
133 kobj_init_static((kobj_t)ofw_obj, ofw_def_impl);
135 rv = OFW_INIT(ofw_obj, cookie);
137 if ((chosen = OF_finddevice("/chosen")) != -1)
138 if (OF_getencprop(chosen, "stdout", &stdout,
139 sizeof(stdout)) == -1)
146 OF_putchar(int c, void *arg __unused)
152 OF_write(stdout, &cbuf, 1);
156 OF_write(stdout, &cbuf, 1);
160 OF_printf(const char *fmt, ...)
165 (void)kvprintf(fmt, OF_putchar, NULL, 10, va);
173 /* Test to see if a service exists. */
175 OF_test(const char *name)
178 if (ofw_def_impl == NULL)
181 return (OFW_TEST(ofw_obj, name));
185 OF_interpret(const char *cmd, int nreturns, ...)
192 if (ofw_def_impl == NULL)
195 status = OFW_INTERPRET(ofw_obj, cmd, nreturns, slots);
199 va_start(ap, nreturns);
201 *va_arg(ap, cell_t *) = slots[i++];
208 * Device tree functions
211 /* Return the next sibling of this node or 0. */
213 OF_peer(phandle_t node)
216 if (ofw_def_impl == NULL)
219 return (OFW_PEER(ofw_obj, node));
222 /* Return the first child of this node or 0. */
224 OF_child(phandle_t node)
227 if (ofw_def_impl == NULL)
230 return (OFW_CHILD(ofw_obj, node));
233 /* Return the parent of this node or 0. */
235 OF_parent(phandle_t node)
238 if (ofw_def_impl == NULL)
241 return (OFW_PARENT(ofw_obj, node));
244 /* Return the package handle that corresponds to an instance handle. */
246 OF_instance_to_package(ihandle_t instance)
249 if (ofw_def_impl == NULL)
252 return (OFW_INSTANCE_TO_PACKAGE(ofw_obj, instance));
255 /* Get the length of a property of a package. */
257 OF_getproplen(phandle_t package, const char *propname)
260 if (ofw_def_impl == NULL)
263 return (OFW_GETPROPLEN(ofw_obj, package, propname));
266 /* Check existence of a property of a package. */
268 OF_hasprop(phandle_t package, const char *propname)
271 return (OF_getproplen(package, propname) >= 0 ? 1 : 0);
274 /* Get the value of a property of a package. */
276 OF_getprop(phandle_t package, const char *propname, void *buf, size_t buflen)
279 if (ofw_def_impl == NULL)
282 return (OFW_GETPROP(ofw_obj, package, propname, buf, buflen));
286 OF_getencprop(phandle_t node, const char *propname, pcell_t *buf, size_t len)
291 KASSERT(len % 4 == 0, ("Need a multiple of 4 bytes"));
293 retval = OF_getprop(node, propname, buf, len);
294 for (i = 0; i < len/4; i++)
295 buf[i] = be32toh(buf[i]);
301 * Recursively search the node and its parent for the given property, working
302 * downward from the node to the device tree root. Returns the value of the
306 OF_searchprop(phandle_t node, const char *propname, void *buf, size_t len)
310 for (; node != 0; node = OF_parent(node))
311 if ((rv = OF_getprop(node, propname, buf, len)) != -1)
317 OF_searchencprop(phandle_t node, const char *propname, void *buf, size_t len)
321 for (; node != 0; node = OF_parent(node))
322 if ((rv = OF_getencprop(node, propname, buf, len)) != -1)
328 * Store the value of a property of a package into newly allocated memory
329 * (using the M_OFWPROP malloc pool and M_WAITOK). elsz is the size of a
330 * single element, the number of elements is return in number.
333 OF_getprop_alloc(phandle_t package, const char *propname, int elsz, void **buf)
338 if ((len = OF_getproplen(package, propname)) == -1 ||
342 *buf = malloc(len, M_OFWPROP, M_WAITOK);
343 if (OF_getprop(package, propname, *buf, len) == -1) {
344 free(*buf, M_OFWPROP);
352 OF_getencprop_alloc(phandle_t package, const char *name, int elsz, void **buf)
358 retval = OF_getprop_alloc(package, name, elsz, buf);
359 if (retval == -1 || retval*elsz % 4 != 0)
363 for (i = 0; i < retval*elsz/4; i++)
364 cell[i] = be32toh(cell[i]);
369 /* Get the next property of a package. */
371 OF_nextprop(phandle_t package, const char *previous, char *buf, size_t size)
374 if (ofw_def_impl == NULL)
377 return (OFW_NEXTPROP(ofw_obj, package, previous, buf, size));
380 /* Set the value of a property of a package. */
382 OF_setprop(phandle_t package, const char *propname, const void *buf, size_t len)
385 if (ofw_def_impl == NULL)
388 return (OFW_SETPROP(ofw_obj, package, propname, buf,len));
391 /* Convert a device specifier to a fully qualified pathname. */
393 OF_canon(const char *device, char *buf, size_t len)
396 if (ofw_def_impl == NULL)
399 return (OFW_CANON(ofw_obj, device, buf, len));
402 /* Return a package handle for the specified device. */
404 OF_finddevice(const char *device)
407 if (ofw_def_impl == NULL)
410 return (OFW_FINDDEVICE(ofw_obj, device));
413 /* Return the fully qualified pathname corresponding to an instance. */
415 OF_instance_to_path(ihandle_t instance, char *buf, size_t len)
418 if (ofw_def_impl == NULL)
421 return (OFW_INSTANCE_TO_PATH(ofw_obj, instance, buf, len));
424 /* Return the fully qualified pathname corresponding to a package. */
426 OF_package_to_path(phandle_t package, char *buf, size_t len)
429 if (ofw_def_impl == NULL)
432 return (OFW_PACKAGE_TO_PATH(ofw_obj, package, buf, len));
435 /* Look up effective phandle (see FDT/PAPR spec) */
437 OF_child_xref_phandle(phandle_t parent, phandle_t xref)
439 phandle_t child, rxref;
442 * Recursively descend from parent, looking for a node with a property
443 * named either "phandle", "ibm,phandle", or "linux,phandle" that
444 * matches the xref we are looking for.
447 for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
448 rxref = OF_child_xref_phandle(child, xref);
452 if (OF_getencprop(child, "phandle", &rxref, sizeof(rxref)) ==
453 -1 && OF_getencprop(child, "ibm,phandle", &rxref,
454 sizeof(rxref)) == -1 && OF_getencprop(child,
455 "linux,phandle", &rxref, sizeof(rxref)) == -1)
466 OF_xref_phandle(phandle_t xref)
470 node = OF_child_xref_phandle(OF_peer(0), xref);
477 /* Call the method in the scope of a given instance. */
479 OF_call_method(const char *method, ihandle_t instance, int nargs, int nreturns,
483 cell_t args_n_results[12];
486 if (nargs > 6 || ofw_def_impl == NULL)
488 va_start(ap, nreturns);
489 for (n = 0; n < nargs; n++)
490 args_n_results[n] = va_arg(ap, cell_t);
492 status = OFW_CALL_METHOD(ofw_obj, instance, method, nargs, nreturns,
497 for (; n < nargs + nreturns; n++)
498 *va_arg(ap, cell_t *) = args_n_results[n];
504 * Device I/O functions
507 /* Open an instance for a device. */
509 OF_open(const char *device)
512 if (ofw_def_impl == NULL)
515 return (OFW_OPEN(ofw_obj, device));
518 /* Close an instance. */
520 OF_close(ihandle_t instance)
523 if (ofw_def_impl == NULL)
526 OFW_CLOSE(ofw_obj, instance);
529 /* Read from an instance. */
531 OF_read(ihandle_t instance, void *addr, size_t len)
534 if (ofw_def_impl == NULL)
537 return (OFW_READ(ofw_obj, instance, addr, len));
540 /* Write to an instance. */
542 OF_write(ihandle_t instance, const void *addr, size_t len)
545 if (ofw_def_impl == NULL)
548 return (OFW_WRITE(ofw_obj, instance, addr, len));
551 /* Seek to a position. */
553 OF_seek(ihandle_t instance, uint64_t pos)
556 if (ofw_def_impl == NULL)
559 return (OFW_SEEK(ofw_obj, instance, pos));
566 /* Claim an area of memory. */
568 OF_claim(void *virt, size_t size, u_int align)
571 if (ofw_def_impl == NULL)
574 return (OFW_CLAIM(ofw_obj, virt, size, align));
577 /* Release an area of memory. */
579 OF_release(void *virt, size_t size)
582 if (ofw_def_impl == NULL)
585 OFW_RELEASE(ofw_obj, virt, size);
589 * Control transfer functions
592 /* Suspend and drop back to the Open Firmware interface. */
597 if (ofw_def_impl == NULL)
603 /* Shut down and drop back to the Open Firmware interface. */
608 if (ofw_def_impl == NULL)
609 panic("OF_exit: Open Firmware not available");
611 /* Should not return */
614 for (;;) /* just in case */