xref: /freebsd/sys/dev/ofw/openfirm.c (revision 7a0a89d2cb29ee2c383600fa59e42d714a6dcbcb)
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 <sys/param.h>
62 #include <sys/kernel.h>
63 #include <sys/malloc.h>
64 #include <sys/systm.h>
65 
66 #include <machine/stdarg.h>
67 
68 #include <dev/ofw/openfirm.h>
69 #include <dev/ofw/ofwvar.h>
70 
71 #include "ofw_if.h"
72 
73 MALLOC_DEFINE(M_OFWPROP, "openfirm", "Open Firmware properties");
74 
75 static ihandle_t stdout;
76 
77 static ofw_def_t	*ofw_def_impl;
78 static ofw_t		ofw_obj;
79 static struct ofw_kobj	ofw_kernel_obj;
80 static struct kobj_ops	ofw_kernel_kops;
81 
82 /*
83  * OFW install routines. Highest priority wins, equal priority also
84  * overrides allowing last-set to win.
85  */
86 SET_DECLARE(ofw_set, ofw_def_t);
87 
88 boolean_t
89 OF_install(char *name, int prio)
90 {
91 	ofw_def_t  **ofwpp, *ofwp;
92 	static int curr_prio = 0;
93 
94 	/*
95 	* Try and locate the OFW kobj corresponding to the name
96 	*/
97 	SET_FOREACH(ofwpp, ofw_set) {
98 		ofwp = *ofwpp;
99 
100 		if (ofwp->name &&
101 		    !strcmp(ofwp->name, name) &&
102 		    prio >= curr_prio) {
103 			curr_prio = prio;
104 			ofw_def_impl = ofwp;
105 			return (TRUE);
106 		}
107 	}
108 
109 	return (FALSE);
110 }
111 
112 /* Initialiser */
113 
114 void
115 OF_init(void *cookie)
116 {
117 	phandle_t chosen;
118 
119 	ofw_obj = &ofw_kernel_obj;
120 	/*
121 	 * Take care of compiling the selected class, and
122 	 * then statically initialize the OFW object
123 	 */
124 	kobj_class_compile_static(ofw_def_impl, &ofw_kernel_kops);
125 	kobj_init((kobj_t)ofw_obj, ofw_def_impl);
126 
127 	OFW_INIT(ofw_obj, cookie);
128 
129 	if ((chosen = OF_finddevice("/chosen")) == -1)
130 		OF_exit();
131 	if (OF_getprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1)
132 		stdout = -1;
133 }
134 
135 void
136 OF_printf(const char *fmt, ...)
137 {
138 	va_list	va;
139 	char buf[1024];
140 
141 	va_start(va, fmt);
142 	vsprintf(buf, fmt, va);
143 	OF_write(stdout, buf, strlen(buf));
144 	va_end(va);
145 }
146 
147 /*
148  * Generic functions
149  */
150 
151 /* Test to see if a service exists. */
152 int
153 OF_test(const char *name)
154 {
155 	return (OFW_TEST(ofw_obj, name));
156 }
157 
158 int
159 OF_interpret(const char *cmd, int nreturns, ...)
160 {
161 	va_list ap;
162 	unsigned long slots[16];
163 	int i = 0;
164 	int status;
165 
166 	status = OFW_INTERPRET(ofw_obj, cmd, nreturns, slots);
167 
168 	va_start(ap, nreturns);
169 	while (i < 1 + nreturns)
170 		*va_arg(ap, cell_t *) = slots[i++];
171 	va_end(ap);
172 
173 	return (status);
174 }
175 
176 /*
177  * Device tree functions
178  */
179 
180 /* Return the next sibling of this node or 0. */
181 phandle_t
182 OF_peer(phandle_t node)
183 {
184 	return (OFW_PEER(ofw_obj, node));
185 }
186 
187 /* Return the first child of this node or 0. */
188 phandle_t
189 OF_child(phandle_t node)
190 {
191 	return (OFW_CHILD(ofw_obj, node));
192 }
193 
194 /* Return the parent of this node or 0. */
195 phandle_t
196 OF_parent(phandle_t node)
197 {
198 	return (OFW_PARENT(ofw_obj, node));
199 }
200 
201 /* Return the package handle that corresponds to an instance handle. */
202 phandle_t
203 OF_instance_to_package(ihandle_t instance)
204 {
205 	return (OFW_INSTANCE_TO_PACKAGE(ofw_obj, instance));
206 }
207 
208 /* Get the length of a property of a package. */
209 ssize_t
210 OF_getproplen(phandle_t package, const char *propname)
211 {
212 	return (OFW_GETPROPLEN(ofw_obj, package, propname));
213 }
214 
215 /* Get the value of a property of a package. */
216 ssize_t
217 OF_getprop(phandle_t package, const char *propname, void *buf, size_t buflen)
218 {
219 	return (OFW_GETPROP(ofw_obj, package, propname, buf, buflen));
220 }
221 
222 /*
223  * Store the value of a property of a package into newly allocated memory
224  * (using the M_OFWPROP malloc pool and M_WAITOK). elsz is the size of a
225  * single element, the number of elements is return in number.
226  */
227 ssize_t
228 OF_getprop_alloc(phandle_t package, const char *propname, int elsz, void **buf)
229 {
230 	int len;
231 
232 	*buf = NULL;
233 	if ((len = OF_getproplen(package, propname)) == -1 ||
234 	    len % elsz != 0)
235 		return (-1);
236 
237 	*buf = malloc(len, M_OFWPROP, M_WAITOK);
238 	if (OF_getprop(package, propname, *buf, len) == -1) {
239 		free(*buf, M_OFWPROP);
240 		*buf = NULL;
241 		return (-1);
242 	}
243 	return (len / elsz);
244 }
245 
246 /* Get the next property of a package. */
247 int
248 OF_nextprop(phandle_t package, const char *previous, char *buf, size_t size)
249 {
250 	return (OFW_NEXTPROP(ofw_obj, package, previous, buf, size));
251 }
252 
253 /* Set the value of a property of a package. */
254 int
255 OF_setprop(phandle_t package, const char *propname, const void *buf, size_t len)
256 {
257 	return (OFW_SETPROP(ofw_obj, package, propname, buf,len));
258 }
259 
260 /* Convert a device specifier to a fully qualified pathname. */
261 ssize_t
262 OF_canon(const char *device, char *buf, size_t len)
263 {
264 	return (OFW_CANON(ofw_obj, device, buf, len));
265 }
266 
267 /* Return a package handle for the specified device. */
268 phandle_t
269 OF_finddevice(const char *device)
270 {
271 	return (OFW_FINDDEVICE(ofw_obj, device));
272 }
273 
274 /* Return the fully qualified pathname corresponding to an instance. */
275 ssize_t
276 OF_instance_to_path(ihandle_t instance, char *buf, size_t len)
277 {
278 	return (OFW_INSTANCE_TO_PATH(ofw_obj, instance, buf, len));
279 }
280 
281 /* Return the fully qualified pathname corresponding to a package. */
282 ssize_t
283 OF_package_to_path(phandle_t package, char *buf, size_t len)
284 {
285 	return (OFW_PACKAGE_TO_PATH(ofw_obj, package, buf, len));
286 }
287 
288 /*  Call the method in the scope of a given instance. */
289 int
290 OF_call_method(const char *method, ihandle_t instance, int nargs, int nreturns,
291     ...)
292 {
293 	va_list ap;
294 	unsigned long args_n_results[12];
295 	int n, status;
296 
297 	if (nargs > 6)
298 		return (-1);
299 	va_start(ap, nreturns);
300 	for (n = 0; n < nargs; n++)
301 		args_n_results[n] = va_arg(ap, unsigned long);
302 
303 	status = OFW_CALL_METHOD(ofw_obj, instance, method, nargs, nreturns,
304 	    args_n_results);
305 
306 	if (status != 0)
307 		return (status);
308 
309 	for (; n < nargs + nreturns; n++)
310 		*va_arg(ap, unsigned long *) = args_n_results[n];
311 	va_end(ap);
312 	return (0);
313 }
314 
315 /*
316  * Device I/O functions
317  */
318 
319 /* Open an instance for a device. */
320 ihandle_t
321 OF_open(const char *device)
322 {
323 	return (OFW_OPEN(ofw_obj, device));
324 }
325 
326 /* Close an instance. */
327 void
328 OF_close(ihandle_t instance)
329 {
330 	OFW_CLOSE(ofw_obj, instance);
331 }
332 
333 /* Read from an instance. */
334 ssize_t
335 OF_read(ihandle_t instance, void *addr, size_t len)
336 {
337 	return (OFW_READ(ofw_obj, instance, addr, len));
338 }
339 
340 /* Write to an instance. */
341 ssize_t
342 OF_write(ihandle_t instance, const void *addr, size_t len)
343 {
344 	return (OFW_WRITE(ofw_obj, instance, addr, len));
345 }
346 
347 /* Seek to a position. */
348 int
349 OF_seek(ihandle_t instance, uint64_t pos)
350 {
351 	return (OFW_SEEK(ofw_obj, instance, pos));
352 }
353 
354 /*
355  * Memory functions
356  */
357 
358 /* Claim an area of memory. */
359 void *
360 OF_claim(void *virt, size_t size, u_int align)
361 {
362 	return (OFW_CLAIM(ofw_obj, virt, size, align));
363 }
364 
365 /* Release an area of memory. */
366 void
367 OF_release(void *virt, size_t size)
368 {
369 	OFW_RELEASE(ofw_obj, virt, size);
370 }
371 
372 /*
373  * Control transfer functions
374  */
375 
376 /* Suspend and drop back to the Open Firmware interface. */
377 void
378 OF_enter()
379 {
380 	OFW_ENTER(ofw_obj);
381 }
382 
383 /* Shut down and drop back to the Open Firmware interface. */
384 void
385 OF_exit()
386 {
387 	/* Should not return */
388 	OFW_EXIT(ofw_obj);
389 
390 	for (;;)			/* just in case */
391 		;
392 }
393 
394