openfirm.c revision 230631
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: head/sys/dev/ofw/openfirm.c 230631 2012-01-27 22:29:29Z marius $");
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
75static void OF_putchar(int c, void *arg);
76
77MALLOC_DEFINE(M_OFWPROP, "openfirm", "Open Firmware properties");
78
79static ihandle_t stdout;
80
81static ofw_def_t	*ofw_def_impl = NULL;
82static ofw_t		ofw_obj;
83static struct ofw_kobj	ofw_kernel_obj;
84static 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 */
90SET_DECLARE(ofw_set, ofw_def_t);
91
92boolean_t
93OF_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 */
117int
118OF_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
143static void
144OF_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
157void
158OF_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. */
172int
173OF_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
182int
183OF_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. */
210phandle_t
211OF_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. */
221phandle_t
222OF_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. */
232phandle_t
233OF_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. */
243phandle_t
244OF_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. */
254ssize_t
255OF_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/* Get the value of a property of a package. */
265ssize_t
266OF_getprop(phandle_t package, const char *propname, void *buf, size_t buflen)
267{
268
269	if (ofw_def_impl == NULL)
270		return (-1);
271
272	return (OFW_GETPROP(ofw_obj, package, propname, buf, buflen));
273}
274
275/*
276 * Recursively search the node and its parent for the given property, working
277 * downward from the node to the device tree root.  Returns the value of the
278 * first match.
279 */
280ssize_t
281OF_searchprop(phandle_t node, const char *propname, void *buf, size_t len)
282{
283	ssize_t rv;
284
285	for (; node != 0; node = OF_parent(node))
286		if ((rv = OF_getprop(node, propname, buf, len)) != -1)
287			return (rv);
288	return (-1);
289}
290
291/*
292 * Store the value of a property of a package into newly allocated memory
293 * (using the M_OFWPROP malloc pool and M_WAITOK).  elsz is the size of a
294 * single element, the number of elements is return in number.
295 */
296ssize_t
297OF_getprop_alloc(phandle_t package, const char *propname, int elsz, void **buf)
298{
299	int len;
300
301	*buf = NULL;
302	if ((len = OF_getproplen(package, propname)) == -1 ||
303	    len % elsz != 0)
304		return (-1);
305
306	*buf = malloc(len, M_OFWPROP, M_WAITOK);
307	if (OF_getprop(package, propname, *buf, len) == -1) {
308		free(*buf, M_OFWPROP);
309		*buf = NULL;
310		return (-1);
311	}
312	return (len / elsz);
313}
314
315/* Get the next property of a package. */
316int
317OF_nextprop(phandle_t package, const char *previous, char *buf, size_t size)
318{
319
320	if (ofw_def_impl == NULL)
321		return (-1);
322
323	return (OFW_NEXTPROP(ofw_obj, package, previous, buf, size));
324}
325
326/* Set the value of a property of a package. */
327int
328OF_setprop(phandle_t package, const char *propname, const void *buf, size_t len)
329{
330
331	if (ofw_def_impl == NULL)
332		return (-1);
333
334	return (OFW_SETPROP(ofw_obj, package, propname, buf,len));
335}
336
337/* Convert a device specifier to a fully qualified pathname. */
338ssize_t
339OF_canon(const char *device, char *buf, size_t len)
340{
341
342	if (ofw_def_impl == NULL)
343		return (-1);
344
345	return (OFW_CANON(ofw_obj, device, buf, len));
346}
347
348/* Return a package handle for the specified device. */
349phandle_t
350OF_finddevice(const char *device)
351{
352
353	if (ofw_def_impl == NULL)
354		return (-1);
355
356	return (OFW_FINDDEVICE(ofw_obj, device));
357}
358
359/* Return the fully qualified pathname corresponding to an instance. */
360ssize_t
361OF_instance_to_path(ihandle_t instance, char *buf, size_t len)
362{
363
364	if (ofw_def_impl == NULL)
365		return (-1);
366
367	return (OFW_INSTANCE_TO_PATH(ofw_obj, instance, buf, len));
368}
369
370/* Return the fully qualified pathname corresponding to a package. */
371ssize_t
372OF_package_to_path(phandle_t package, char *buf, size_t len)
373{
374
375	if (ofw_def_impl == NULL)
376		return (-1);
377
378	return (OFW_PACKAGE_TO_PATH(ofw_obj, package, buf, len));
379}
380
381/*  Call the method in the scope of a given instance. */
382int
383OF_call_method(const char *method, ihandle_t instance, int nargs, int nreturns,
384    ...)
385{
386	va_list ap;
387	cell_t args_n_results[12];
388	int n, status;
389
390	if (nargs > 6 || ofw_def_impl == NULL)
391		return (-1);
392	va_start(ap, nreturns);
393	for (n = 0; n < nargs; n++)
394		args_n_results[n] = va_arg(ap, cell_t);
395
396	status = OFW_CALL_METHOD(ofw_obj, instance, method, nargs, nreturns,
397	    args_n_results);
398	if (status != 0)
399		return (status);
400
401	for (; n < nargs + nreturns; n++)
402		*va_arg(ap, cell_t *) = args_n_results[n];
403	va_end(ap);
404	return (0);
405}
406
407/*
408 * Device I/O functions
409 */
410
411/* Open an instance for a device. */
412ihandle_t
413OF_open(const char *device)
414{
415
416	if (ofw_def_impl == NULL)
417		return (0);
418
419	return (OFW_OPEN(ofw_obj, device));
420}
421
422/* Close an instance. */
423void
424OF_close(ihandle_t instance)
425{
426
427	if (ofw_def_impl == NULL)
428		return;
429
430	OFW_CLOSE(ofw_obj, instance);
431}
432
433/* Read from an instance. */
434ssize_t
435OF_read(ihandle_t instance, void *addr, size_t len)
436{
437
438	if (ofw_def_impl == NULL)
439		return (-1);
440
441	return (OFW_READ(ofw_obj, instance, addr, len));
442}
443
444/* Write to an instance. */
445ssize_t
446OF_write(ihandle_t instance, const void *addr, size_t len)
447{
448
449	if (ofw_def_impl == NULL)
450		return (-1);
451
452	return (OFW_WRITE(ofw_obj, instance, addr, len));
453}
454
455/* Seek to a position. */
456int
457OF_seek(ihandle_t instance, uint64_t pos)
458{
459
460	if (ofw_def_impl == NULL)
461		return (-1);
462
463	return (OFW_SEEK(ofw_obj, instance, pos));
464}
465
466/*
467 * Memory functions
468 */
469
470/* Claim an area of memory. */
471void *
472OF_claim(void *virt, size_t size, u_int align)
473{
474
475	if (ofw_def_impl == NULL)
476		return ((void *)-1);
477
478	return (OFW_CLAIM(ofw_obj, virt, size, align));
479}
480
481/* Release an area of memory. */
482void
483OF_release(void *virt, size_t size)
484{
485
486	if (ofw_def_impl == NULL)
487		return;
488
489	OFW_RELEASE(ofw_obj, virt, size);
490}
491
492/*
493 * Control transfer functions
494 */
495
496/* Suspend and drop back to the Open Firmware interface. */
497void
498OF_enter()
499{
500
501	if (ofw_def_impl == NULL)
502		return;
503
504	OFW_ENTER(ofw_obj);
505}
506
507/* Shut down and drop back to the Open Firmware interface. */
508void
509OF_exit()
510{
511
512	if (ofw_def_impl == NULL)
513		panic("OF_exit: Open Firmware not available");
514
515	/* Should not return */
516	OFW_EXIT(ofw_obj);
517
518	for (;;)			/* just in case */
519		;
520}
521