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