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 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 * Copyright (C) 2000 Benno Rice.
38 * All rights reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
49 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
54 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
55 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
56 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
57 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
58 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 #include <sys/param.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
65 #include <sys/systm.h>
67 #include <machine/stdarg.h>
69 #include <dev/ofw/openfirm.h>
71 MALLOC_DEFINE(M_OFWPROP, "openfirm", "OpenFirmware properties");
73 static ihandle_t stdin;
74 static ihandle_t stdout;
78 void ofbcopy(const void *, void *, size_t);
83 OF_init(int (*openfirm)(void *))
87 set_openfirm_callback(openfirm);
88 if ((chosen = OF_finddevice("/chosen")) == -1)
90 OF_getprop(chosen, "stdin", &stdin, sizeof(stdin));
91 OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
95 OF_printf(const char *fmt, ...)
101 vsprintf(buf, fmt, va);
102 OF_write(stdout, buf, strlen(buf));
110 /* Test to see if a service exists. */
126 args.service = (cell_t)name;
127 if (openfirmware(&args) == -1)
133 OF_interpret(char *cmd, int nreturns, ...)
148 args.nreturns = ++nreturns;
149 args.slot[i++] = (cell_t)cmd;
150 va_start(ap, nreturns);
152 args.slot[i++] = va_arg(ap, cell_t);
153 if (openfirmware(&args) == -1) {
157 status = args.slot[i++];
158 while (i < 1 + nreturns)
159 *va_arg(ap, cell_t *) = args.slot[i++];
164 /* Return firmware millisecond count. */
174 (cell_t)"milliseconds",
184 * Device tree functions
187 /* Return the next sibling of this node or 0. */
189 OF_peer(phandle_t node)
203 args.node = (cell_t)node;
204 if (openfirmware(&args) == -1)
209 /* Return the first child of this node or 0. */
211 OF_child(phandle_t node)
225 args.node = (cell_t)node;
226 if (openfirmware(&args) == -1)
231 /* Return the parent of this node or 0. */
233 OF_parent(phandle_t node)
247 args.node = (cell_t)node;
248 if (openfirmware(&args) == -1)
253 /* Return the package handle that corresponds to an instance handle. */
255 OF_instance_to_package(ihandle_t instance)
264 (cell_t)"instance-to-package",
269 args.instance = (cell_t)instance;
270 if (openfirmware(&args) == -1)
275 /* Get the length of a property of a package. */
277 OF_getproplen(phandle_t package, char *propname)
287 (cell_t)"getproplen",
292 args.package = (cell_t)package;
293 args.propname = (cell_t)propname;
294 if (openfirmware(&args) == -1)
299 /* Get the value of a property of a package. */
301 OF_getprop(phandle_t package, char *propname, void *buf, int buflen)
318 args.package = (cell_t)package;
319 args.propname = (cell_t)propname;
320 args.buf = (cell_t)buf;
321 args.buflen = (cell_t)buflen;
322 if (openfirmware(&args) == -1)
328 * Store the value of a property of a package into newly allocated memory (using
329 * the M_OFWPROP malloc pool and M_WAITOK). elsz is the size of a single element,
330 * the number of elements is return in number.
333 OF_getprop_alloc(phandle_t package, 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);
351 /* Get the next property of a package. */
353 OF_nextprop(phandle_t package, char *previous, char *buf)
369 args.package = (cell_t)package;
370 args.previous = (cell_t)previous;
371 args.buf = (cell_t)buf;
372 if (openfirmware(&args) == -1)
377 /* Set the value of a property of a package. */
378 /* XXX Has a bug on FirePower */
380 OF_setprop(phandle_t package, char *propname, void *buf, int len)
397 args.package = (cell_t)package;
398 args.propname = (cell_t)propname;
399 args.buf = (cell_t)buf;
400 args.len = (cell_t)len;
401 if (openfirmware(&args) == -1)
406 /* Convert a device specifier to a fully qualified pathname. */
408 OF_canon(const char *device, char *buf, int len)
424 args.device = (cell_t)device;
425 args.buf = (cell_t)buf;
426 args.len = (cell_t)len;
427 if (openfirmware(&args) == -1)
432 /* Return a package handle for the specified device. */
434 OF_finddevice(const char *device)
443 (cell_t)"finddevice",
448 args.device = (cell_t)device;
449 if (openfirmware(&args) == -1)
454 /* Return the fully qualified pathname corresponding to an instance. */
456 OF_instance_to_path(ihandle_t instance, char *buf, int len)
467 (cell_t)"instance-to-path",
472 args.instance = (cell_t)instance;
473 args.buf = (cell_t)buf;
474 args.len = (cell_t)len;
475 if (openfirmware(&args) == -1)
480 /* Return the fully qualified pathname corresponding to a package. */
482 OF_package_to_path(phandle_t package, char *buf, int len)
493 (cell_t)"package-to-path",
498 args.package = (cell_t)package;
499 args.buf = (cell_t)buf;
500 args.len = (cell_t)len;
501 if (openfirmware(&args) == -1)
506 /* Call the method in the scope of a given instance. */
508 OF_call_method(char *method, ihandle_t instance, int nargs, int nreturns, ...)
517 cell_t args_n_results[12];
519 (cell_t)"call-method",
528 args.nargs = (cell_t)nargs + 2;
529 args.nreturns = (cell_t)nreturns + 1;
530 args.method = (cell_t)method;
531 args.instance = (cell_t)instance;
532 va_start(ap, nreturns);
533 for (ip = args.args_n_results + (n = (cell_t)nargs); --n >= 0;)
534 *--ip = va_arg(ap, cell_t);
536 if (openfirmware(&args) == -1)
538 if (args.args_n_results[nargs])
539 return args.args_n_results[nargs];
540 for (ip = args.args_n_results + nargs + (n = args.nreturns); --n > 0;)
541 *va_arg(ap, cell_t *) = *--ip;
547 * Device I/O functions.
550 /* Open an instance for a device. */
552 OF_open(char *device)
566 args.device = (cell_t)device;
567 if (openfirmware(&args) == -1 || args.instance == 0) {
570 return args.instance;
573 /* Close an instance. */
575 OF_close(ihandle_t instance)
588 args.instance = (cell_t)instance;
592 /* Read from an instance. */
594 OF_read(ihandle_t instance, void *addr, int len)
610 args.instance = (cell_t)instance;
611 args.addr = (cell_t)addr;
612 args.len = (cell_t)len;
613 if (openfirmware(&args) == -1)
619 /* Write to an instance. */
621 OF_write(ihandle_t instance, void *addr, int len)
637 args.instance = (cell_t)instance;
638 args.addr = (cell_t)addr;
639 args.len = (cell_t)len;
640 if (openfirmware(&args) == -1)
645 /* Seek to a position. */
647 OF_seek(ihandle_t instance, u_int64_t pos)
663 args.instance = (cell_t)instance;
664 args.poshi = (cell_t)(pos >> 32);
665 args.poslo = (cell_t)pos;
666 if (openfirmware(&args) == -1)
675 /* Claim an area of memory. */
677 OF_claim(void *virt, u_int size, u_int align)
693 args.virt = (cell_t)virt;
694 args.size = (cell_t)size;
695 args.align = (cell_t)align;
696 if (openfirmware(&args) == -1)
698 return (void *)args.baseaddr;
701 /* Release an area of memory. */
703 OF_release(void *virt, u_int size)
717 args.virt = (cell_t)virt;
718 args.size = (cell_t)size;
723 * Control transfer functions.
726 /* Reset the system and call "boot <bootspec>". */
728 OF_boot(char *bootspec)
741 args.bootspec = (cell_t)bootspec;
743 for (;;); /* just in case */
746 /* Suspend and drop back to the OpenFirmware interface. */
761 return; /* We may come back. */
764 /* Shut down and drop back to the OpenFirmware interface. */
779 for (;;); /* just in case */
782 /* Free <size> bytes starting at <virt>, then call <entry> with <arg>. */
785 OF_chain(void *virt, u_int size, void (*entry)(), void *arg, u_int len)
802 args.virt = (cell_t)virt;
803 args.size = (cell_t)size;
804 args.entry = (cell_t)entry;
805 args.arg = (cell_t)arg;
806 args.len = (cell_t)len;
811 OF_chain(void *virt, u_int size,
812 void (*entry)(void *, u_int, void *, void *, u_int), void *arg, u_int len)
815 * This is a REALLY dirty hack till the firmware gets this going
818 OF_release(virt, size);
820 entry(0, 0, openfirmware, arg, len);
825 ofbcopy(const void *src, void *dst, size_t len)
827 const char *sp = src;