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