]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ofw/openfirm.c
Upgrade to OpenSSH 6.2p1. The most important new features are support
[FreeBSD/FreeBSD.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 "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 /*  Call the method in the scope of a given instance. */
390 int
391 OF_call_method(const char *method, ihandle_t instance, int nargs, int nreturns,
392     ...)
393 {
394         va_list ap;
395         cell_t args_n_results[12];
396         int n, status;
397
398         if (nargs > 6 || ofw_def_impl == NULL)
399                 return (-1);
400         va_start(ap, nreturns);
401         for (n = 0; n < nargs; n++)
402                 args_n_results[n] = va_arg(ap, cell_t);
403
404         status = OFW_CALL_METHOD(ofw_obj, instance, method, nargs, nreturns,
405             args_n_results);
406         if (status != 0)
407                 return (status);
408
409         for (; n < nargs + nreturns; n++)
410                 *va_arg(ap, cell_t *) = args_n_results[n];
411         va_end(ap);
412         return (0);
413 }
414
415 /*
416  * Device I/O functions
417  */
418
419 /* Open an instance for a device. */
420 ihandle_t
421 OF_open(const char *device)
422 {
423
424         if (ofw_def_impl == NULL)
425                 return (0);
426
427         return (OFW_OPEN(ofw_obj, device));
428 }
429
430 /* Close an instance. */
431 void
432 OF_close(ihandle_t instance)
433 {
434
435         if (ofw_def_impl == NULL)
436                 return;
437
438         OFW_CLOSE(ofw_obj, instance);
439 }
440
441 /* Read from an instance. */
442 ssize_t
443 OF_read(ihandle_t instance, void *addr, size_t len)
444 {
445
446         if (ofw_def_impl == NULL)
447                 return (-1);
448
449         return (OFW_READ(ofw_obj, instance, addr, len));
450 }
451
452 /* Write to an instance. */
453 ssize_t
454 OF_write(ihandle_t instance, const void *addr, size_t len)
455 {
456
457         if (ofw_def_impl == NULL)
458                 return (-1);
459
460         return (OFW_WRITE(ofw_obj, instance, addr, len));
461 }
462
463 /* Seek to a position. */
464 int
465 OF_seek(ihandle_t instance, uint64_t pos)
466 {
467
468         if (ofw_def_impl == NULL)
469                 return (-1);
470
471         return (OFW_SEEK(ofw_obj, instance, pos));
472 }
473
474 /*
475  * Memory functions
476  */
477
478 /* Claim an area of memory. */
479 void *
480 OF_claim(void *virt, size_t size, u_int align)
481 {
482
483         if (ofw_def_impl == NULL)
484                 return ((void *)-1);
485
486         return (OFW_CLAIM(ofw_obj, virt, size, align));
487 }
488
489 /* Release an area of memory. */
490 void
491 OF_release(void *virt, size_t size)
492 {
493
494         if (ofw_def_impl == NULL)
495                 return;
496
497         OFW_RELEASE(ofw_obj, virt, size);
498 }
499
500 /*
501  * Control transfer functions
502  */
503
504 /* Suspend and drop back to the Open Firmware interface. */
505 void
506 OF_enter()
507 {
508
509         if (ofw_def_impl == NULL)
510                 return;
511
512         OFW_ENTER(ofw_obj);
513 }
514
515 /* Shut down and drop back to the Open Firmware interface. */
516 void
517 OF_exit()
518 {
519
520         if (ofw_def_impl == NULL)
521                 panic("OF_exit: Open Firmware not available");
522
523         /* Should not return */
524         OFW_EXIT(ofw_obj);
525
526         for (;;)                        /* just in case */
527                 ;
528 }