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 <sys/param.h>
62 #include <sys/kernel.h>
64 #include <sys/mutex.h>
65 #include <sys/systm.h>
68 #include <vm/vm_page.h>
71 #include <machine/bus.h>
72 #include <machine/md_var.h>
73 #include <machine/ofw_machdep.h>
74 #include <machine/pmap.h>
75 #include <machine/stdarg.h>
77 #include <dev/ofw/openfirm.h>
78 #include <dev/ofw/ofwvar.h>
81 static int ofw_real_init(ofw_t, void *openfirm);
82 static int ofw_real_test(ofw_t, const char *name);
83 static phandle_t ofw_real_peer(ofw_t, phandle_t node);
84 static phandle_t ofw_real_child(ofw_t, phandle_t node);
85 static phandle_t ofw_real_parent(ofw_t, phandle_t node);
86 static phandle_t ofw_real_instance_to_package(ofw_t, ihandle_t instance);
87 static ssize_t ofw_real_getproplen(ofw_t, phandle_t package,
88 const char *propname);
89 static ssize_t ofw_real_getprop(ofw_t, phandle_t package, const char *propname,
90 void *buf, size_t buflen);
91 static int ofw_real_nextprop(ofw_t, phandle_t package, const char *previous,
93 static int ofw_real_setprop(ofw_t, phandle_t package, const char *propname,
94 const void *buf, size_t len);
95 static ssize_t ofw_real_canon(ofw_t, const char *device, char *buf, size_t len);
96 static phandle_t ofw_real_finddevice(ofw_t, const char *device);
97 static ssize_t ofw_real_instance_to_path(ofw_t, ihandle_t instance, char *buf,
99 static ssize_t ofw_real_package_to_path(ofw_t, phandle_t package, char *buf,
101 static int ofw_real_call_method(ofw_t, ihandle_t instance, const char *method,
102 int nargs, int nreturns, cell_t *args_and_returns);
103 static int ofw_real_interpret(ofw_t ofw, const char *cmd, int nreturns,
105 static ihandle_t ofw_real_open(ofw_t, const char *device);
106 static void ofw_real_close(ofw_t, ihandle_t instance);
107 static ssize_t ofw_real_read(ofw_t, ihandle_t instance, void *addr, size_t len);
108 static ssize_t ofw_real_write(ofw_t, ihandle_t instance, const void *addr,
110 static int ofw_real_seek(ofw_t, ihandle_t instance, u_int64_t pos);
111 static caddr_t ofw_real_claim(ofw_t, void *virt, size_t size, u_int align);
112 static void ofw_real_release(ofw_t, void *virt, size_t size);
113 static void ofw_real_enter(ofw_t);
114 static void ofw_real_exit(ofw_t);
116 static ofw_method_t ofw_real_methods[] = {
117 OFWMETHOD(ofw_init, ofw_real_init),
118 OFWMETHOD(ofw_peer, ofw_real_peer),
119 OFWMETHOD(ofw_child, ofw_real_child),
120 OFWMETHOD(ofw_parent, ofw_real_parent),
121 OFWMETHOD(ofw_instance_to_package, ofw_real_instance_to_package),
122 OFWMETHOD(ofw_getproplen, ofw_real_getproplen),
123 OFWMETHOD(ofw_getprop, ofw_real_getprop),
124 OFWMETHOD(ofw_nextprop, ofw_real_nextprop),
125 OFWMETHOD(ofw_setprop, ofw_real_setprop),
126 OFWMETHOD(ofw_canon, ofw_real_canon),
127 OFWMETHOD(ofw_finddevice, ofw_real_finddevice),
128 OFWMETHOD(ofw_instance_to_path, ofw_real_instance_to_path),
129 OFWMETHOD(ofw_package_to_path, ofw_real_package_to_path),
131 OFWMETHOD(ofw_test, ofw_real_test),
132 OFWMETHOD(ofw_call_method, ofw_real_call_method),
133 OFWMETHOD(ofw_interpret, ofw_real_interpret),
134 OFWMETHOD(ofw_open, ofw_real_open),
135 OFWMETHOD(ofw_close, ofw_real_close),
136 OFWMETHOD(ofw_read, ofw_real_read),
137 OFWMETHOD(ofw_write, ofw_real_write),
138 OFWMETHOD(ofw_seek, ofw_real_seek),
139 OFWMETHOD(ofw_claim, ofw_real_claim),
140 OFWMETHOD(ofw_release, ofw_real_release),
141 OFWMETHOD(ofw_enter, ofw_real_enter),
142 OFWMETHOD(ofw_exit, ofw_real_exit),
147 static ofw_def_t ofw_real = {
154 static ofw_def_t ofw_32bit = {
161 static MALLOC_DEFINE(M_OFWREAL, "ofwreal",
162 "Open Firmware Real Mode Bounce Page");
164 static int (*openfirmware)(void *);
166 static vm_offset_t of_bounce_phys;
167 static caddr_t of_bounce_virt;
168 static off_t of_bounce_offset;
169 static size_t of_bounce_size;
170 static struct mtx of_bounce_mtx;
172 extern int ofw_real_mode;
173 extern struct pmap ofw_pmap;
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)
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;
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;
257 of_bounce_size = sizeof(emergency_buffer);
261 * Make sure the bounce page offset satisfies any reasonable
262 * alignment constraint.
264 of_bounce_offset += sizeof(register_t) - (of_bounce_offset % sizeof(register_t));
266 if (of_bounce_offset + len > of_bounce_size) {
267 panic("Oversize Open Firmware call!");
272 memcpy(of_bounce_virt + of_bounce_offset, buf, len);
276 phys = of_bounce_phys + of_bounce_offset;
278 of_bounce_offset += len;
284 ofw_real_unmap(cell_t physaddr, void *buf, size_t len)
286 mtx_assert(&of_bounce_mtx, MA_OWNED);
288 if (of_bounce_virt == NULL)
294 memcpy(buf,of_bounce_virt + (physaddr - of_bounce_phys),len);
300 ofw_real_init(ofw_t ofw, void *openfirm)
302 openfirmware = (int (*)(void *))openfirm;
304 mtx_init(&of_bounce_mtx, "OF Bounce Page", MTX_DEF, 0);
305 of_bounce_virt = NULL;
313 /* Test to see if a service exists. */
315 ofw_real_test(ofw_t ofw, const char *name)
326 args.name = (cell_t)(uintptr_t)"test";
332 args.service = ofw_real_map(name, strlen(name) + 1);
333 argsptr = ofw_real_map(&args, sizeof(args));
334 if (args.service == 0 || openfirmware((void *)argsptr) == -1) {
338 ofw_real_unmap(argsptr, &args, sizeof(args));
340 return (args.missing);
344 * Device tree functions
347 /* Return the next sibling of this node or 0. */
349 ofw_real_peer(ofw_t ofw, phandle_t node)
360 args.name = (cell_t)(uintptr_t)"peer";
366 argsptr = ofw_real_map(&args, sizeof(args));
367 if (openfirmware((void *)argsptr) == -1) {
371 ofw_real_unmap(argsptr, &args, sizeof(args));
376 /* Return the first child of this node or 0. */
378 ofw_real_child(ofw_t ofw, phandle_t node)
389 args.name = (cell_t)(uintptr_t)"child";
395 argsptr = ofw_real_map(&args, sizeof(args));
396 if (openfirmware((void *)argsptr) == -1) {
400 ofw_real_unmap(argsptr, &args, sizeof(args));
405 /* Return the parent of this node or 0. */
407 ofw_real_parent(ofw_t ofw, phandle_t node)
418 args.name = (cell_t)(uintptr_t)"parent";
424 argsptr = ofw_real_map(&args, sizeof(args));
425 if (openfirmware((void *)argsptr) == -1) {
429 ofw_real_unmap(argsptr, &args, sizeof(args));
431 return (args.parent);
434 /* Return the package handle that corresponds to an instance handle. */
436 ofw_real_instance_to_package(ofw_t ofw, ihandle_t instance)
447 args.name = (cell_t)(uintptr_t)"instance-to-package";
451 args.instance = instance;
453 argsptr = ofw_real_map(&args, sizeof(args));
454 if (openfirmware((void *)argsptr) == -1) {
458 ofw_real_unmap(argsptr, &args, sizeof(args));
460 return (args.package);
463 /* Get the length of a property of a package. */
465 ofw_real_getproplen(ofw_t ofw, phandle_t package, const char *propname)
477 args.name = (cell_t)(uintptr_t)"getproplen";
483 args.package = package;
484 args.propname = ofw_real_map(propname, strlen(propname) + 1);
485 argsptr = ofw_real_map(&args, sizeof(args));
486 if (args.propname == 0 || openfirmware((void *)argsptr) == -1) {
490 ofw_real_unmap(argsptr, &args, sizeof(args));
492 return (args.proplen);
495 /* Get the value of a property of a package. */
497 ofw_real_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf,
512 args.name = (cell_t)(uintptr_t)"getprop";
518 args.package = package;
519 args.propname = ofw_real_map(propname, strlen(propname) + 1);
520 args.buf = ofw_real_map(buf, buflen);
521 args.buflen = buflen;
522 argsptr = ofw_real_map(&args, sizeof(args));
523 if (args.propname == 0 || args.buf == 0 ||
524 openfirmware((void *)argsptr) == -1) {
528 ofw_real_unmap(argsptr, &args, sizeof(args));
529 ofw_real_unmap(args.buf, buf, buflen);
535 /* Get the next property of a package. */
537 ofw_real_nextprop(ofw_t ofw, phandle_t package, const char *previous,
538 char *buf, size_t size)
551 args.name = (cell_t)(uintptr_t)"nextprop";
557 args.package = package;
558 args.previous = ofw_real_map(previous, (previous != NULL) ? (strlen(previous) + 1) : 0);
559 args.buf = ofw_real_map(buf, size);
560 argsptr = ofw_real_map(&args, sizeof(args));
561 if (args.buf == 0 || openfirmware((void *)argsptr) == -1) {
565 ofw_real_unmap(argsptr, &args, sizeof(args));
566 ofw_real_unmap(args.buf, buf, size);
572 /* Set the value of a property of a package. */
573 /* XXX Has a bug on FirePower */
575 ofw_real_setprop(ofw_t ofw, phandle_t package, const char *propname,
576 const void *buf, size_t len)
590 args.name = (cell_t)(uintptr_t)"setprop";
596 args.package = package;
597 args.propname = ofw_real_map(propname, strlen(propname) + 1);
598 args.buf = ofw_real_map(buf, len);
600 argsptr = ofw_real_map(&args, sizeof(args));
601 if (args.propname == 0 || args.buf == 0 ||
602 openfirmware((void *)argsptr) == -1) {
606 ofw_real_unmap(argsptr, &args, sizeof(args));
611 /* Convert a device specifier to a fully qualified pathname. */
613 ofw_real_canon(ofw_t ofw, const char *device, char *buf, size_t len)
626 args.name = (cell_t)(uintptr_t)"canon";
632 args.device = ofw_real_map(device, strlen(device) + 1);
633 args.buf = ofw_real_map(buf, len);
635 argsptr = ofw_real_map(&args, sizeof(args));
636 if (args.device == 0 || args.buf == 0 ||
637 openfirmware((void *)argsptr) == -1) {
641 ofw_real_unmap(argsptr, &args, sizeof(args));
642 ofw_real_unmap(args.buf, buf, len);
648 /* Return a package handle for the specified device. */
650 ofw_real_finddevice(ofw_t ofw, const char *device)
661 args.name = (cell_t)(uintptr_t)"finddevice";
667 args.device = ofw_real_map(device, strlen(device) + 1);
668 argsptr = ofw_real_map(&args, sizeof(args));
669 if (args.device == 0 ||
670 openfirmware((void *)argsptr) == -1) {
674 ofw_real_unmap(argsptr, &args, sizeof(args));
676 return (args.package);
679 /* Return the fully qualified pathname corresponding to an instance. */
681 ofw_real_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len)
694 args.name = (cell_t)(uintptr_t)"instance-to-path";
700 args.instance = instance;
701 args.buf = ofw_real_map(buf, len);
703 argsptr = ofw_real_map(&args, sizeof(args));
705 openfirmware((void *)argsptr) == -1) {
709 ofw_real_unmap(argsptr, &args, sizeof(args));
710 ofw_real_unmap(args.buf, buf, len);
716 /* Return the fully qualified pathname corresponding to a package. */
718 ofw_real_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len)
731 args.name = (cell_t)(uintptr_t)"package-to-path";
737 args.package = package;
738 args.buf = ofw_real_map(buf, len);
740 argsptr = ofw_real_map(&args, sizeof(args));
742 openfirmware((void *)argsptr) == -1) {
746 ofw_real_unmap(argsptr, &args, sizeof(args));
747 ofw_real_unmap(args.buf, buf, len);
753 /* Call the method in the scope of a given instance. */
755 ofw_real_call_method(ofw_t ofw, ihandle_t instance, const char *method,
756 int nargs, int nreturns, cell_t *args_and_returns)
765 cell_t args_n_results[12];
770 args.name = (cell_t)(uintptr_t)"call-method";
778 args.nargs = nargs + 2;
779 args.nreturns = nreturns + 1;
780 args.method = ofw_real_map(method, strlen(method) + 1);
781 args.instance = instance;
783 ap = args_and_returns;
784 for (cp = args.args_n_results + (n = nargs); --n >= 0;)
786 argsptr = ofw_real_map(&args, sizeof(args));
787 if (args.method == 0 ||
788 openfirmware((void *)argsptr) == -1) {
792 ofw_real_unmap(argsptr, &args, sizeof(args));
794 if (args.args_n_results[nargs])
795 return (args.args_n_results[nargs]);
796 for (cp = args.args_n_results + nargs + (n = args.nreturns); --n > 0;)
802 ofw_real_interpret(ofw_t ofw, const char *cmd, int nreturns, cell_t *returns)
814 args.name = (cell_t)(uintptr_t)"interpret";
818 args.nreturns = ++nreturns;
819 args.slot[i++] = ofw_real_map(cmd, strlen(cmd) + 1);
820 argsptr = ofw_real_map(&args, sizeof(args));
821 if (openfirmware((void *)argsptr) == -1) {
825 ofw_real_unmap(argsptr, &args, sizeof(args));
827 status = args.slot[i++];
828 while (i < 1 + nreturns)
829 returns[j++] = args.slot[i++];
834 * Device I/O functions
837 /* Open an instance for a device. */
839 ofw_real_open(ofw_t ofw, const char *device)
850 args.name = (cell_t)(uintptr_t)"open";
856 args.device = ofw_real_map(device, strlen(device) + 1);
857 argsptr = ofw_real_map(&args, sizeof(args));
858 if (args.device == 0 || openfirmware((void *)argsptr) == -1
859 || args.instance == 0) {
863 ofw_real_unmap(argsptr, &args, sizeof(args));
865 return (args.instance);
868 /* Close an instance. */
870 ofw_real_close(ofw_t ofw, ihandle_t instance)
880 args.name = (cell_t)(uintptr_t)"close";
883 args.instance = instance;
885 argsptr = ofw_real_map(&args, sizeof(args));
886 openfirmware((void *)argsptr);
890 /* Read from an instance. */
892 ofw_real_read(ofw_t ofw, ihandle_t instance, void *addr, size_t len)
905 args.name = (cell_t)(uintptr_t)"read";
911 args.instance = instance;
912 args.addr = ofw_real_map(addr, len);
914 argsptr = ofw_real_map(&args, sizeof(args));
915 if (args.addr == 0 || openfirmware((void *)argsptr) == -1) {
919 ofw_real_unmap(argsptr, &args, sizeof(args));
920 ofw_real_unmap(args.addr, addr, len);
923 return (args.actual);
926 /* Write to an instance. */
928 ofw_real_write(ofw_t ofw, ihandle_t instance, const void *addr, size_t len)
941 args.name = (cell_t)(uintptr_t)"write";
947 args.instance = instance;
948 args.addr = ofw_real_map(addr, len);
950 argsptr = ofw_real_map(&args, sizeof(args));
951 if (args.addr == 0 || openfirmware((void *)argsptr) == -1) {
955 ofw_real_unmap(argsptr, &args, sizeof(args));
957 return (args.actual);
960 /* Seek to a position. */
962 ofw_real_seek(ofw_t ofw, ihandle_t instance, u_int64_t pos)
975 args.name = (cell_t)(uintptr_t)"seek";
979 args.instance = instance;
980 args.poshi = pos >> 32;
983 argsptr = ofw_real_map(&args, sizeof(args));
984 if (openfirmware((void *)argsptr) == -1) {
988 ofw_real_unmap(argsptr, &args, sizeof(args));
990 return (args.status);
997 /* Claim an area of memory. */
999 ofw_real_claim(ofw_t ofw, void *virt, size_t size, u_int align)
1001 vm_offset_t argsptr;
1012 args.name = (cell_t)(uintptr_t)"claim";
1016 args.virt = (cell_t)(uintptr_t)virt;
1020 argsptr = ofw_real_map(&args, sizeof(args));
1021 if (openfirmware((void *)argsptr) == -1) {
1023 return ((void *)-1);
1025 ofw_real_unmap(argsptr, &args, sizeof(args));
1027 return ((void *)(uintptr_t)args.baseaddr);
1030 /* Release an area of memory. */
1032 ofw_real_release(ofw_t ofw, void *virt, size_t size)
1034 vm_offset_t argsptr;
1043 args.name = (cell_t)(uintptr_t)"release";
1047 args.virt = (cell_t)(uintptr_t)virt;
1050 argsptr = ofw_real_map(&args, sizeof(args));
1051 openfirmware((void *)argsptr);
1056 * Control transfer functions
1059 /* Suspend and drop back to the Open Firmware interface. */
1061 ofw_real_enter(ofw_t ofw)
1063 vm_offset_t argsptr;
1070 args.name = (cell_t)(uintptr_t)"enter";
1075 argsptr = ofw_real_map(&args, sizeof(args));
1076 openfirmware((void *)argsptr);
1077 /* We may come back. */
1081 /* Shut down and drop back to the Open Firmware interface. */
1083 ofw_real_exit(ofw_t ofw)
1085 vm_offset_t argsptr;
1092 args.name = (cell_t)(uintptr_t)"exit";
1097 argsptr = ofw_real_map(&args, sizeof(args));
1098 openfirmware((void *)argsptr);
1099 for (;;) /* just in case */