1 /* $NetBSD: Locore.c,v 1.7 2000/08/20 07:04:59 tsubai Exp $ */
4 * SPDX-License-Identifier:BSD-4-Clause AND BSD-2-Clause-FreeBSD
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 <sys/param.h>
64 #include <sys/kernel.h>
66 #include <sys/mutex.h>
67 #include <sys/systm.h>
70 #include <vm/vm_page.h>
73 #include <machine/bus.h>
74 #include <machine/md_var.h>
75 #include <machine/ofw_machdep.h>
76 #include <machine/stdarg.h>
78 #include <dev/ofw/openfirm.h>
79 #include <dev/ofw/ofwvar.h>
82 static int ofw_real_init(ofw_t, void *openfirm);
83 static int ofw_real_test(ofw_t, const char *name);
84 static phandle_t ofw_real_peer(ofw_t, phandle_t node);
85 static phandle_t ofw_real_child(ofw_t, phandle_t node);
86 static phandle_t ofw_real_parent(ofw_t, phandle_t node);
87 static phandle_t ofw_real_instance_to_package(ofw_t, ihandle_t instance);
88 static ssize_t ofw_real_getproplen(ofw_t, phandle_t package,
89 const char *propname);
90 static ssize_t ofw_real_getprop(ofw_t, phandle_t package, const char *propname,
91 void *buf, size_t buflen);
92 static int ofw_real_nextprop(ofw_t, phandle_t package, const char *previous,
94 static int ofw_real_setprop(ofw_t, phandle_t package, const char *propname,
95 const void *buf, size_t len);
96 static ssize_t ofw_real_canon(ofw_t, const char *device, char *buf, size_t len);
97 static phandle_t ofw_real_finddevice(ofw_t, const char *device);
98 static ssize_t ofw_real_instance_to_path(ofw_t, ihandle_t instance, char *buf,
100 static ssize_t ofw_real_package_to_path(ofw_t, phandle_t package, char *buf,
102 static int ofw_real_call_method(ofw_t, ihandle_t instance, const char *method,
103 int nargs, int nreturns, cell_t *args_and_returns);
104 static int ofw_real_interpret(ofw_t ofw, const char *cmd, int nreturns,
106 static ihandle_t ofw_real_open(ofw_t, const char *device);
107 static void ofw_real_close(ofw_t, ihandle_t instance);
108 static ssize_t ofw_real_read(ofw_t, ihandle_t instance, void *addr, size_t len);
109 static ssize_t ofw_real_write(ofw_t, ihandle_t instance, const void *addr,
111 static int ofw_real_seek(ofw_t, ihandle_t instance, u_int64_t pos);
112 static caddr_t ofw_real_claim(ofw_t, void *virt, size_t size, u_int align);
113 static void ofw_real_release(ofw_t, void *virt, size_t size);
114 static void ofw_real_enter(ofw_t);
115 static void ofw_real_exit(ofw_t);
117 static ofw_method_t ofw_real_methods[] = {
118 OFWMETHOD(ofw_init, ofw_real_init),
119 OFWMETHOD(ofw_peer, ofw_real_peer),
120 OFWMETHOD(ofw_child, ofw_real_child),
121 OFWMETHOD(ofw_parent, ofw_real_parent),
122 OFWMETHOD(ofw_instance_to_package, ofw_real_instance_to_package),
123 OFWMETHOD(ofw_getproplen, ofw_real_getproplen),
124 OFWMETHOD(ofw_getprop, ofw_real_getprop),
125 OFWMETHOD(ofw_nextprop, ofw_real_nextprop),
126 OFWMETHOD(ofw_setprop, ofw_real_setprop),
127 OFWMETHOD(ofw_canon, ofw_real_canon),
128 OFWMETHOD(ofw_finddevice, ofw_real_finddevice),
129 OFWMETHOD(ofw_instance_to_path, ofw_real_instance_to_path),
130 OFWMETHOD(ofw_package_to_path, ofw_real_package_to_path),
132 OFWMETHOD(ofw_test, ofw_real_test),
133 OFWMETHOD(ofw_call_method, ofw_real_call_method),
134 OFWMETHOD(ofw_interpret, ofw_real_interpret),
135 OFWMETHOD(ofw_open, ofw_real_open),
136 OFWMETHOD(ofw_close, ofw_real_close),
137 OFWMETHOD(ofw_read, ofw_real_read),
138 OFWMETHOD(ofw_write, ofw_real_write),
139 OFWMETHOD(ofw_seek, ofw_real_seek),
140 OFWMETHOD(ofw_claim, ofw_real_claim),
141 OFWMETHOD(ofw_release, ofw_real_release),
142 OFWMETHOD(ofw_enter, ofw_real_enter),
143 OFWMETHOD(ofw_exit, ofw_real_exit),
148 static ofw_def_t ofw_real = {
155 static ofw_def_t ofw_32bit = {
162 static MALLOC_DEFINE(M_OFWREAL, "ofwreal",
163 "Open Firmware Real Mode Bounce Page");
165 static int (*openfirmware)(void *);
167 static vm_offset_t of_bounce_phys;
168 static caddr_t of_bounce_virt;
169 static off_t of_bounce_offset;
170 static size_t of_bounce_size;
171 static struct mtx of_bounce_mtx;
173 extern int ofw_real_mode;
176 * After the VM is up, allocate a wired, low memory bounce page.
179 static void ofw_real_bounce_alloc(void *);
181 SYSINIT(ofw_real_bounce_alloc, SI_SUB_KMEM, SI_ORDER_ANY,
182 ofw_real_bounce_alloc, NULL);
187 mtx_lock(&of_bounce_mtx);
188 of_bounce_offset = 0;
194 mtx_unlock(&of_bounce_mtx);
198 ofw_real_bounce_alloc(void *junk)
201 * Check that ofw_real is actually in use before allocating wads
202 * of memory. Do this by checking if our mutex has been set up.
204 if (!mtx_initialized(&of_bounce_mtx))
208 * Allocate a page of contiguous, wired physical memory that can
209 * fit into a 32-bit address space and accessed from real mode.
212 mtx_lock(&of_bounce_mtx);
214 of_bounce_virt = contigmalloc(4 * PAGE_SIZE, M_OFWREAL, 0, 0,
215 ulmin(platform_real_maxaddr(), BUS_SPACE_MAXADDR_32BIT), PAGE_SIZE,
218 of_bounce_phys = vtophys(of_bounce_virt);
219 of_bounce_size = 4 * PAGE_SIZE;
222 * For virtual-mode OF, direct map this physical address so that
223 * we have a 32-bit virtual address to give OF.
226 if (!ofw_real_mode && (!hw_direct_map || DMAP_BASE_ADDRESS != 0))
227 pmap_kenter(of_bounce_phys, of_bounce_phys);
229 mtx_unlock(&of_bounce_mtx);
233 ofw_real_map(const void *buf, size_t len)
235 static char emergency_buffer[255];
238 mtx_assert(&of_bounce_mtx, MA_OWNED);
240 if (of_bounce_virt == NULL) {
242 * If we haven't set up the MMU, then buf is guaranteed
243 * to be accessible to OF, because the only memory we
244 * can use right now is memory mapped by firmware.
246 if (!pmap_bootstrapped)
247 return (cell_t)((uintptr_t)buf & ~DMAP_BASE_ADDRESS);
250 * XXX: It is possible for us to get called before the VM has
251 * come online, but after the MMU is up. We don't have the
252 * bounce buffer yet, but can no longer presume a 1:1 mapping.
253 * Copy into the emergency buffer, and reset at the end.
255 of_bounce_virt = emergency_buffer;
256 of_bounce_phys = (vm_offset_t)of_bounce_virt &
258 of_bounce_size = sizeof(emergency_buffer);
262 * Make sure the bounce page offset satisfies any reasonable
263 * alignment constraint.
265 of_bounce_offset += sizeof(register_t) -
266 (of_bounce_offset % sizeof(register_t));
268 if (of_bounce_offset + len > of_bounce_size) {
269 panic("Oversize Open Firmware call!");
274 memcpy(of_bounce_virt + of_bounce_offset, buf, len);
278 phys = of_bounce_phys + of_bounce_offset;
280 of_bounce_offset += len;
286 ofw_real_unmap(cell_t physaddr, void *buf, size_t len)
288 mtx_assert(&of_bounce_mtx, MA_OWNED);
290 if (of_bounce_virt == NULL)
296 memcpy(buf,of_bounce_virt + (physaddr - of_bounce_phys),len);
302 ofw_real_init(ofw_t ofw, void *openfirm)
304 openfirmware = (int (*)(void *))openfirm;
306 mtx_init(&of_bounce_mtx, "OF Bounce Page", NULL, MTX_DEF);
307 of_bounce_virt = NULL;
315 /* Test to see if a service exists. */
317 ofw_real_test(ofw_t ofw, const char *name)
328 args.name = (cell_t)(uintptr_t)"test";
334 args.service = ofw_real_map(name, strlen(name) + 1);
335 argsptr = ofw_real_map(&args, sizeof(args));
336 if (args.service == 0 || openfirmware((void *)argsptr) == -1) {
340 ofw_real_unmap(argsptr, &args, sizeof(args));
342 return (args.missing);
346 * Device tree functions
349 /* Return the next sibling of this node or 0. */
351 ofw_real_peer(ofw_t ofw, phandle_t node)
362 args.name = (cell_t)(uintptr_t)"peer";
368 argsptr = ofw_real_map(&args, sizeof(args));
369 if (openfirmware((void *)argsptr) == -1) {
373 ofw_real_unmap(argsptr, &args, sizeof(args));
378 /* Return the first child of this node or 0. */
380 ofw_real_child(ofw_t ofw, phandle_t node)
391 args.name = (cell_t)(uintptr_t)"child";
397 argsptr = ofw_real_map(&args, sizeof(args));
398 if (openfirmware((void *)argsptr) == -1) {
402 ofw_real_unmap(argsptr, &args, sizeof(args));
407 /* Return the parent of this node or 0. */
409 ofw_real_parent(ofw_t ofw, phandle_t node)
420 args.name = (cell_t)(uintptr_t)"parent";
426 argsptr = ofw_real_map(&args, sizeof(args));
427 if (openfirmware((void *)argsptr) == -1) {
431 ofw_real_unmap(argsptr, &args, sizeof(args));
433 return (args.parent);
436 /* Return the package handle that corresponds to an instance handle. */
438 ofw_real_instance_to_package(ofw_t ofw, ihandle_t instance)
449 args.name = (cell_t)(uintptr_t)"instance-to-package";
453 args.instance = instance;
455 argsptr = ofw_real_map(&args, sizeof(args));
456 if (openfirmware((void *)argsptr) == -1) {
460 ofw_real_unmap(argsptr, &args, sizeof(args));
462 return (args.package);
465 /* Get the length of a property of a package. */
467 ofw_real_getproplen(ofw_t ofw, phandle_t package, const char *propname)
479 args.name = (cell_t)(uintptr_t)"getproplen";
485 args.package = package;
486 args.propname = ofw_real_map(propname, strlen(propname) + 1);
487 argsptr = ofw_real_map(&args, sizeof(args));
488 if (args.propname == 0 || openfirmware((void *)argsptr) == -1) {
492 ofw_real_unmap(argsptr, &args, sizeof(args));
494 return (args.proplen);
497 /* Get the value of a property of a package. */
499 ofw_real_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,
514 args.name = (cell_t)(uintptr_t)"getprop";
520 args.package = package;
521 args.propname = ofw_real_map(propname, strlen(propname) + 1);
522 args.buf = ofw_real_map(buf, buflen);
523 args.buflen = buflen;
524 argsptr = ofw_real_map(&args, sizeof(args));
525 if (args.propname == 0 || args.buf == 0 ||
526 openfirmware((void *)argsptr) == -1) {
530 ofw_real_unmap(argsptr, &args, sizeof(args));
531 ofw_real_unmap(args.buf, buf, buflen);
537 /* Get the next property of a package. */
539 ofw_real_nextprop(ofw_t ofw, phandle_t package, const char *previous,
540 char *buf, size_t size)
553 args.name = (cell_t)(uintptr_t)"nextprop";
559 args.package = package;
560 args.previous = ofw_real_map(previous, (previous != NULL) ? (strlen(previous) + 1) : 0);
561 args.buf = ofw_real_map(buf, size);
562 argsptr = ofw_real_map(&args, sizeof(args));
563 if (args.buf == 0 || openfirmware((void *)argsptr) == -1) {
567 ofw_real_unmap(argsptr, &args, sizeof(args));
568 ofw_real_unmap(args.buf, buf, size);
574 /* Set the value of a property of a package. */
575 /* XXX Has a bug on FirePower */
577 ofw_real_setprop(ofw_t ofw, phandle_t package, const char *propname,
578 const void *buf, size_t len)
592 args.name = (cell_t)(uintptr_t)"setprop";
598 args.package = package;
599 args.propname = ofw_real_map(propname, strlen(propname) + 1);
600 args.buf = ofw_real_map(buf, len);
602 argsptr = ofw_real_map(&args, sizeof(args));
603 if (args.propname == 0 || args.buf == 0 ||
604 openfirmware((void *)argsptr) == -1) {
608 ofw_real_unmap(argsptr, &args, sizeof(args));
613 /* Convert a device specifier to a fully qualified pathname. */
615 ofw_real_canon(ofw_t ofw, const char *device, char *buf, size_t len)
628 args.name = (cell_t)(uintptr_t)"canon";
634 args.device = ofw_real_map(device, strlen(device) + 1);
635 args.buf = ofw_real_map(buf, len);
637 argsptr = ofw_real_map(&args, sizeof(args));
638 if (args.device == 0 || args.buf == 0 ||
639 openfirmware((void *)argsptr) == -1) {
643 ofw_real_unmap(argsptr, &args, sizeof(args));
644 ofw_real_unmap(args.buf, buf, len);
650 /* Return a package handle for the specified device. */
652 ofw_real_finddevice(ofw_t ofw, const char *device)
663 args.name = (cell_t)(uintptr_t)"finddevice";
669 args.device = ofw_real_map(device, strlen(device) + 1);
670 argsptr = ofw_real_map(&args, sizeof(args));
671 if (args.device == 0 ||
672 openfirmware((void *)argsptr) == -1) {
676 ofw_real_unmap(argsptr, &args, sizeof(args));
678 return (args.package);
681 /* Return the fully qualified pathname corresponding to an instance. */
683 ofw_real_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
696 args.name = (cell_t)(uintptr_t)"instance-to-path";
702 args.instance = instance;
703 args.buf = ofw_real_map(buf, len);
705 argsptr = ofw_real_map(&args, sizeof(args));
707 openfirmware((void *)argsptr) == -1) {
711 ofw_real_unmap(argsptr, &args, sizeof(args));
712 ofw_real_unmap(args.buf, buf, len);
718 /* Return the fully qualified pathname corresponding to a package. */
720 ofw_real_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
733 args.name = (cell_t)(uintptr_t)"package-to-path";
739 args.package = package;
740 args.buf = ofw_real_map(buf, len);
742 argsptr = ofw_real_map(&args, sizeof(args));
744 openfirmware((void *)argsptr) == -1) {
748 ofw_real_unmap(argsptr, &args, sizeof(args));
749 ofw_real_unmap(args.buf, buf, len);
755 /* Call the method in the scope of a given instance. */
757 ofw_real_call_method(ofw_t ofw, ihandle_t instance, const char *method,
758 int nargs, int nreturns, cell_t *args_and_returns)
767 cell_t args_n_results[12];
772 args.name = (cell_t)(uintptr_t)"call-method";
780 args.nargs = nargs + 2;
781 args.nreturns = nreturns + 1;
782 args.method = ofw_real_map(method, strlen(method) + 1);
783 args.instance = instance;
785 ap = args_and_returns;
786 for (cp = args.args_n_results + (n = nargs); --n >= 0;)
788 argsptr = ofw_real_map(&args, sizeof(args));
789 if (args.method == 0 ||
790 openfirmware((void *)argsptr) == -1) {
794 ofw_real_unmap(argsptr, &args, sizeof(args));
796 if (args.args_n_results[nargs])
797 return (args.args_n_results[nargs]);
798 for (cp = args.args_n_results + nargs + (n = args.nreturns); --n > 0;)
804 ofw_real_interpret(ofw_t ofw, const char *cmd, int nreturns, cell_t *returns)
816 args.name = (cell_t)(uintptr_t)"interpret";
820 args.nreturns = ++nreturns;
821 args.slot[i++] = ofw_real_map(cmd, strlen(cmd) + 1);
822 argsptr = ofw_real_map(&args, sizeof(args));
823 if (openfirmware((void *)argsptr) == -1) {
827 ofw_real_unmap(argsptr, &args, sizeof(args));
829 status = args.slot[i++];
830 while (i < 1 + nreturns)
831 returns[j++] = args.slot[i++];
836 * Device I/O functions
839 /* Open an instance for a device. */
841 ofw_real_open(ofw_t ofw, const char *device)
852 args.name = (cell_t)(uintptr_t)"open";
858 args.device = ofw_real_map(device, strlen(device) + 1);
859 argsptr = ofw_real_map(&args, sizeof(args));
860 if (args.device == 0 || openfirmware((void *)argsptr) == -1
861 || args.instance == 0) {
865 ofw_real_unmap(argsptr, &args, sizeof(args));
867 return (args.instance);
870 /* Close an instance. */
872 ofw_real_close(ofw_t ofw, ihandle_t instance)
882 args.name = (cell_t)(uintptr_t)"close";
885 args.instance = instance;
887 argsptr = ofw_real_map(&args, sizeof(args));
888 openfirmware((void *)argsptr);
892 /* Read from an instance. */
894 ofw_real_read(ofw_t ofw, ihandle_t instance, void *addr, size_t len)
907 args.name = (cell_t)(uintptr_t)"read";
913 args.instance = instance;
914 args.addr = ofw_real_map(addr, len);
916 argsptr = ofw_real_map(&args, sizeof(args));
917 if (args.addr == 0 || openfirmware((void *)argsptr) == -1) {
921 ofw_real_unmap(argsptr, &args, sizeof(args));
922 ofw_real_unmap(args.addr, addr, len);
925 return (args.actual);
928 /* Write to an instance. */
930 ofw_real_write(ofw_t ofw, ihandle_t instance, const void *addr, size_t len)
943 args.name = (cell_t)(uintptr_t)"write";
949 args.instance = instance;
950 args.addr = ofw_real_map(addr, len);
952 argsptr = ofw_real_map(&args, sizeof(args));
953 if (args.addr == 0 || openfirmware((void *)argsptr) == -1) {
957 ofw_real_unmap(argsptr, &args, sizeof(args));
959 return (args.actual);
962 /* Seek to a position. */
964 ofw_real_seek(ofw_t ofw, ihandle_t instance, u_int64_t pos)
977 args.name = (cell_t)(uintptr_t)"seek";
981 args.instance = instance;
982 args.poshi = pos >> 32;
985 argsptr = ofw_real_map(&args, sizeof(args));
986 if (openfirmware((void *)argsptr) == -1) {
990 ofw_real_unmap(argsptr, &args, sizeof(args));
992 return (args.status);
999 /* Claim an area of memory. */
1001 ofw_real_claim(ofw_t ofw, void *virt, size_t size, u_int align)
1003 vm_offset_t argsptr;
1014 args.name = (cell_t)(uintptr_t)"claim";
1018 args.virt = (cell_t)(uintptr_t)virt;
1022 argsptr = ofw_real_map(&args, sizeof(args));
1023 if (openfirmware((void *)argsptr) == -1) {
1025 return ((void *)-1);
1027 ofw_real_unmap(argsptr, &args, sizeof(args));
1029 return ((void *)(uintptr_t)args.baseaddr);
1032 /* Release an area of memory. */
1034 ofw_real_release(ofw_t ofw, void *virt, size_t size)
1036 vm_offset_t argsptr;
1045 args.name = (cell_t)(uintptr_t)"release";
1049 args.virt = (cell_t)(uintptr_t)virt;
1052 argsptr = ofw_real_map(&args, sizeof(args));
1053 openfirmware((void *)argsptr);
1058 * Control transfer functions
1061 /* Suspend and drop back to the Open Firmware interface. */
1063 ofw_real_enter(ofw_t ofw)
1065 vm_offset_t argsptr;
1072 args.name = (cell_t)(uintptr_t)"enter";
1077 argsptr = ofw_real_map(&args, sizeof(args));
1078 openfirmware((void *)argsptr);
1079 /* We may come back. */
1083 /* Shut down and drop back to the Open Firmware interface. */
1085 ofw_real_exit(ofw_t ofw)
1087 vm_offset_t argsptr;
1094 args.name = (cell_t)(uintptr_t)"exit";
1099 argsptr = ofw_real_map(&args, sizeof(args));
1100 openfirmware((void *)argsptr);
1101 for (;;) /* just in case */