xref: /freebsd/sys/dev/ofw/openfirm.c (revision febdb468801f35e51c6c5c22221cfce9197c6f3b)
1 /*	$NetBSD: Locore.c,v 1.7 2000/08/20 07:04:59 tsubai Exp $	*/
2 
3 /*-
4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5  * Copyright (C) 1995, 1996 TooLs GmbH.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
21  *
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.
32  */
33 /*-
34  * Copyright (C) 2000 Benno Rice.
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
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.
45  *
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.
56  */
57 
58 #include <sys/cdefs.h>
59 __FBSDID("$FreeBSD$");
60 
61 #include "opt_platform.h"
62 
63 #include <sys/param.h>
64 #include <sys/kernel.h>
65 #include <sys/malloc.h>
66 #include <sys/systm.h>
67 
68 #include <machine/stdarg.h>
69 
70 #include <dev/ofw/ofwvar.h>
71 #include <dev/ofw/openfirm.h>
72 
73 #include "ofw_if.h"
74 
75 static void OF_putchar(int c, void *arg);
76 
77 MALLOC_DEFINE(M_OFWPROP, "openfirm", "Open Firmware properties");
78 
79 static ihandle_t stdout;
80 
81 static ofw_def_t	*ofw_def_impl = NULL;
82 static ofw_t		ofw_obj;
83 static struct ofw_kobj	ofw_kernel_obj;
84 static struct kobj_ops	ofw_kernel_kops;
85 
86 /*
87  * OFW install routines.  Highest priority wins, equal priority also
88  * overrides allowing last-set to win.
89  */
90 SET_DECLARE(ofw_set, ofw_def_t);
91 
92 boolean_t
93 OF_install(char *name, int prio)
94 {
95 	ofw_def_t *ofwp, **ofwpp;
96 	static int curr_prio = 0;
97 
98 	/*
99 	 * Try and locate the OFW kobj corresponding to the name.
100 	 */
101 	SET_FOREACH(ofwpp, ofw_set) {
102 		ofwp = *ofwpp;
103 
104 		if (ofwp->name &&
105 		    !strcmp(ofwp->name, name) &&
106 		    prio >= curr_prio) {
107 			curr_prio = prio;
108 			ofw_def_impl = ofwp;
109 			return (TRUE);
110 		}
111 	}
112 
113 	return (FALSE);
114 }
115 
116 /* Initializer */
117 int
118 OF_init(void *cookie)
119 {
120 	phandle_t chosen;
121 	int rv;
122 
123 	if (ofw_def_impl == NULL)
124 		return (-1);
125 
126 	ofw_obj = &ofw_kernel_obj;
127 	/*
128 	 * Take care of compiling the selected class, and
129 	 * then statically initialize the OFW object.
130 	 */
131 	kobj_class_compile_static(ofw_def_impl, &ofw_kernel_kops);
132 	kobj_init_static((kobj_t)ofw_obj, ofw_def_impl);
133 
134 	rv = OFW_INIT(ofw_obj, cookie);
135 
136 	if ((chosen = OF_finddevice("/chosen")) != -1)
137 		if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
138 			stdout = -1;
139 
140 	return (rv);
141 }
142 
143 static void
144 OF_putchar(int c, void *arg __unused)
145 {
146 	char cbuf;
147 
148 	if (c == '\n') {
149 		cbuf = '\r';
150 		OF_write(stdout, &cbuf, 1);
151 	}
152 
153 	cbuf = c;
154 	OF_write(stdout, &cbuf, 1);
155 }
156 
157 void
158 OF_printf(const char *fmt, ...)
159 {
160 	va_list	va;
161 
162 	va_start(va, fmt);
163 	(void)kvprintf(fmt, OF_putchar, NULL, 10, va);
164 	va_end(va);
165 }
166 
167 /*
168  * Generic functions
169  */
170 
171 /* Test to see if a service exists. */
172 int
173 OF_test(const char *name)
174 {
175 
176 	if (ofw_def_impl == NULL)
177 		return (-1);
178 
179 	return (OFW_TEST(ofw_obj, name));
180 }
181 
182 int
183 OF_interpret(const char *cmd, int nreturns, ...)
184 {
185 	va_list ap;
186 	cell_t slots[16];
187 	int i = 0;
188 	int status;
189 
190 	if (ofw_def_impl == NULL)
191 		return (-1);
192 
193 	status = OFW_INTERPRET(ofw_obj, cmd, nreturns, slots);
194 	if (status == -1)
195 		return (status);
196 
197 	va_start(ap, nreturns);
198 	while (i < nreturns)
199 		*va_arg(ap, cell_t *) = slots[i++];
200 	va_end(ap);
201 
202 	return (status);
203 }
204 
205 /*
206  * Device tree functions
207  */
208 
209 /* Return the next sibling of this node or 0. */
210 phandle_t
211 OF_peer(phandle_t node)
212 {
213 
214 	if (ofw_def_impl == NULL)
215 		return (0);
216 
217 	return (OFW_PEER(ofw_obj, node));
218 }
219 
220 /* Return the first child of this node or 0. */
221 phandle_t
222 OF_child(phandle_t node)
223 {
224 
225 	if (ofw_def_impl == NULL)
226 		return (0);
227 
228 	return (OFW_CHILD(ofw_obj, node));
229 }
230 
231 /* Return the parent of this node or 0. */
232 phandle_t
233 OF_parent(phandle_t node)
234 {
235 
236 	if (ofw_def_impl == NULL)
237 		return (0);
238 
239 	return (OFW_PARENT(ofw_obj, node));
240 }
241 
242 /* Return the package handle that corresponds to an instance handle. */
243 phandle_t
244 OF_instance_to_package(ihandle_t instance)
245 {
246 
247 	if (ofw_def_impl == NULL)
248 		return (-1);
249 
250 	return (OFW_INSTANCE_TO_PACKAGE(ofw_obj, instance));
251 }
252 
253 /* Get the length of a property of a package. */
254 ssize_t
255 OF_getproplen(phandle_t package, const char *propname)
256 {
257 
258 	if (ofw_def_impl == NULL)
259 		return (-1);
260 
261 	return (OFW_GETPROPLEN(ofw_obj, package, propname));
262 }
263 
264 /* Check existence of a property of a package. */
265 int
266 OF_hasprop(phandle_t package, const char *propname)
267 {
268 
269 	return (OF_getproplen(package, propname) >= 0 ? 1 : 0);
270 }
271 
272 /* Get the value of a property of a package. */
273 ssize_t
274 OF_getprop(phandle_t package, const char *propname, void *buf, size_t buflen)
275 {
276 
277 	if (ofw_def_impl == NULL)
278 		return (-1);
279 
280 	return (OFW_GETPROP(ofw_obj, package, propname, buf, buflen));
281 }
282 
283 /*
284  * Recursively search the node and its parent for the given property, working
285  * downward from the node to the device tree root.  Returns the value of the
286  * first match.
287  */
288 ssize_t
289 OF_searchprop(phandle_t node, const char *propname, void *buf, size_t len)
290 {
291 	ssize_t rv;
292 
293 	for (; node != 0; node = OF_parent(node))
294 		if ((rv = OF_getprop(node, propname, buf, len)) != -1)
295 			return (rv);
296 	return (-1);
297 }
298 
299 /*
300  * Store the value of a property of a package into newly allocated memory
301  * (using the M_OFWPROP malloc pool and M_WAITOK).  elsz is the size of a
302  * single element, the number of elements is return in number.
303  */
304 ssize_t
305 OF_getprop_alloc(phandle_t package, const char *propname, int elsz, void **buf)
306 {
307 	int len;
308 
309 	*buf = NULL;
310 	if ((len = OF_getproplen(package, propname)) == -1 ||
311 	    len % elsz != 0)
312 		return (-1);
313 
314 	*buf = malloc(len, M_OFWPROP, M_WAITOK);
315 	if (OF_getprop(package, propname, *buf, len) == -1) {
316 		free(*buf, M_OFWPROP);
317 		*buf = NULL;
318 		return (-1);
319 	}
320 	return (len / elsz);
321 }
322 
323 /* Get the next property of a package. */
324 int
325 OF_nextprop(phandle_t package, const char *previous, char *buf, size_t size)
326 {
327 
328 	if (ofw_def_impl == NULL)
329 		return (-1);
330 
331 	return (OFW_NEXTPROP(ofw_obj, package, previous, buf, size));
332 }
333 
334 /* Set the value of a property of a package. */
335 int
336 OF_setprop(phandle_t package, const char *propname, const void *buf, size_t len)
337 {
338 
339 	if (ofw_def_impl == NULL)
340 		return (-1);
341 
342 	return (OFW_SETPROP(ofw_obj, package, propname, buf,len));
343 }
344 
345 /* Convert a device specifier to a fully qualified pathname. */
346 ssize_t
347 OF_canon(const char *device, char *buf, size_t len)
348 {
349 
350 	if (ofw_def_impl == NULL)
351 		return (-1);
352 
353 	return (OFW_CANON(ofw_obj, device, buf, len));
354 }
355 
356 /* Return a package handle for the specified device. */
357 phandle_t
358 OF_finddevice(const char *device)
359 {
360 
361 	if (ofw_def_impl == NULL)
362 		return (-1);
363 
364 	return (OFW_FINDDEVICE(ofw_obj, device));
365 }
366 
367 /* Return the fully qualified pathname corresponding to an instance. */
368 ssize_t
369 OF_instance_to_path(ihandle_t instance, char *buf, size_t len)
370 {
371 
372 	if (ofw_def_impl == NULL)
373 		return (-1);
374 
375 	return (OFW_INSTANCE_TO_PATH(ofw_obj, instance, buf, len));
376 }
377 
378 /* Return the fully qualified pathname corresponding to a package. */
379 ssize_t
380 OF_package_to_path(phandle_t package, char *buf, size_t len)
381 {
382 
383 	if (ofw_def_impl == NULL)
384 		return (-1);
385 
386 	return (OFW_PACKAGE_TO_PATH(ofw_obj, package, buf, len));
387 }
388 
389 /* Look up effective phandle (see FDT/PAPR spec) */
390 static phandle_t
391 OF_child_xref_phandle(phandle_t parent, phandle_t xref)
392 {
393 	phandle_t child, rxref;
394 
395 	/*
396 	 * Recursively descend from parent, looking for a node with a property
397 	 * named either "phandle", "ibm,phandle", or "linux,phandle" that
398 	 * matches the xref we are looking for.
399 	 */
400 
401 	for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
402 		rxref = OF_child_xref_phandle(child, xref);
403 		if (rxref != -1)
404 			return (rxref);
405 
406 		if (OF_getprop(child, "phandle", &rxref, sizeof(rxref)) == -1 &&
407 		    OF_getprop(child, "ibm,phandle", &rxref,
408 		    sizeof(rxref)) == -1 && OF_getprop(child,
409 		    "linux,phandle", &rxref, sizeof(rxref)) == -1)
410 			continue;
411 
412 		if (rxref == xref)
413 			return (child);
414 	}
415 
416 	return (-1);
417 }
418 
419 phandle_t
420 OF_xref_phandle(phandle_t xref)
421 {
422 	phandle_t node;
423 
424 	node = OF_child_xref_phandle(OF_peer(0), xref);
425 	if (node == -1)
426 		return (xref);
427 
428 	return (node);
429 }
430 
431 /*  Call the method in the scope of a given instance. */
432 int
433 OF_call_method(const char *method, ihandle_t instance, int nargs, int nreturns,
434     ...)
435 {
436 	va_list ap;
437 	cell_t args_n_results[12];
438 	int n, status;
439 
440 	if (nargs > 6 || ofw_def_impl == NULL)
441 		return (-1);
442 	va_start(ap, nreturns);
443 	for (n = 0; n < nargs; n++)
444 		args_n_results[n] = va_arg(ap, cell_t);
445 
446 	status = OFW_CALL_METHOD(ofw_obj, instance, method, nargs, nreturns,
447 	    args_n_results);
448 	if (status != 0)
449 		return (status);
450 
451 	for (; n < nargs + nreturns; n++)
452 		*va_arg(ap, cell_t *) = args_n_results[n];
453 	va_end(ap);
454 	return (0);
455 }
456 
457 /*
458  * Device I/O functions
459  */
460 
461 /* Open an instance for a device. */
462 ihandle_t
463 OF_open(const char *device)
464 {
465 
466 	if (ofw_def_impl == NULL)
467 		return (0);
468 
469 	return (OFW_OPEN(ofw_obj, device));
470 }
471 
472 /* Close an instance. */
473 void
474 OF_close(ihandle_t instance)
475 {
476 
477 	if (ofw_def_impl == NULL)
478 		return;
479 
480 	OFW_CLOSE(ofw_obj, instance);
481 }
482 
483 /* Read from an instance. */
484 ssize_t
485 OF_read(ihandle_t instance, void *addr, size_t len)
486 {
487 
488 	if (ofw_def_impl == NULL)
489 		return (-1);
490 
491 	return (OFW_READ(ofw_obj, instance, addr, len));
492 }
493 
494 /* Write to an instance. */
495 ssize_t
496 OF_write(ihandle_t instance, const void *addr, size_t len)
497 {
498 
499 	if (ofw_def_impl == NULL)
500 		return (-1);
501 
502 	return (OFW_WRITE(ofw_obj, instance, addr, len));
503 }
504 
505 /* Seek to a position. */
506 int
507 OF_seek(ihandle_t instance, uint64_t pos)
508 {
509 
510 	if (ofw_def_impl == NULL)
511 		return (-1);
512 
513 	return (OFW_SEEK(ofw_obj, instance, pos));
514 }
515 
516 /*
517  * Memory functions
518  */
519 
520 /* Claim an area of memory. */
521 void *
522 OF_claim(void *virt, size_t size, u_int align)
523 {
524 
525 	if (ofw_def_impl == NULL)
526 		return ((void *)-1);
527 
528 	return (OFW_CLAIM(ofw_obj, virt, size, align));
529 }
530 
531 /* Release an area of memory. */
532 void
533 OF_release(void *virt, size_t size)
534 {
535 
536 	if (ofw_def_impl == NULL)
537 		return;
538 
539 	OFW_RELEASE(ofw_obj, virt, size);
540 }
541 
542 /*
543  * Control transfer functions
544  */
545 
546 /* Suspend and drop back to the Open Firmware interface. */
547 void
548 OF_enter()
549 {
550 
551 	if (ofw_def_impl == NULL)
552 		return;
553 
554 	OFW_ENTER(ofw_obj);
555 }
556 
557 /* Shut down and drop back to the Open Firmware interface. */
558 void
559 OF_exit()
560 {
561 
562 	if (ofw_def_impl == NULL)
563 		panic("OF_exit: Open Firmware not available");
564 
565 	/* Should not return */
566 	OFW_EXIT(ofw_obj);
567 
568 	for (;;)			/* just in case */
569 		;
570 }
571