ofw_subr.c revision 1.59 1 /* $NetBSD: ofw_subr.c,v 1.59 2021/09/15 17:33:08 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Copyright 1998
31 * Digital Equipment Corporation. All rights reserved.
32 *
33 * This software is furnished under license and may be used and
34 * copied only in accordance with the following terms and conditions.
35 * Subject to these conditions, you may download, copy, install,
36 * use, modify and distribute this software in source and/or binary
37 * form. No title or ownership is transferred hereby.
38 *
39 * 1) Any source code used, modified or distributed must reproduce
40 * and retain this copyright notice and list of conditions as
41 * they appear in the source file.
42 *
43 * 2) No right is granted to use any trade name, trademark, or logo of
44 * Digital Equipment Corporation. Neither the "Digital Equipment
45 * Corporation" name nor any trademark or logo of Digital Equipment
46 * Corporation may be used to endorse or promote products derived
47 * from this software without the prior written permission of
48 * Digital Equipment Corporation.
49 *
50 * 3) This software is provided "AS-IS" and any express or implied
51 * warranties, including but not limited to, any implied warranties
52 * of merchantability, fitness for a particular purpose, or
53 * non-infringement are disclaimed. In no event shall DIGITAL be
54 * liable for any damages whatsoever, and in particular, DIGITAL
55 * shall not be liable for special, indirect, consequential, or
56 * incidental damages or damages for lost profits, loss of
57 * revenue or loss of use, whether such damages arise in contract,
58 * negligence, tort, under statute, in equity, at law or otherwise,
59 * even if advised of the possibility of such damage.
60 */
61
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: ofw_subr.c,v 1.59 2021/09/15 17:33:08 thorpej Exp $");
64
65 #include <sys/param.h>
66 #include <sys/device.h>
67 #include <sys/kmem.h>
68 #include <sys/systm.h>
69
70 #include <sys/device_calls.h>
71
72 #include <dev/ofw/openfirm.h>
73
74 #define OFW_MAX_STACK_BUF_SIZE 256
75 #define OFW_PATH_BUF_SIZE 512
76
77 /*
78 * OpenFirmware device handle support.
79 */
80
81 static device_call_t
82 of_devhandle_lookup_device_call(devhandle_t handle, const char *name,
83 devhandle_t *call_handlep)
84 {
85 __link_set_decl(of_device_calls, struct device_call_descriptor);
86 struct device_call_descriptor * const *desc;
87
88 __link_set_foreach(desc, of_device_calls) {
89 if (strcmp((*desc)->name, name) == 0) {
90 return (*desc)->call;
91 }
92 }
93 return NULL;
94 }
95
96 static const struct devhandle_impl of_devhandle_impl = {
97 .type = DEVHANDLE_TYPE_OF,
98 .lookup_device_call = of_devhandle_lookup_device_call,
99 };
100
101 devhandle_t
102 devhandle_from_of(int phandle)
103 {
104 devhandle_t handle = {
105 .impl = &of_devhandle_impl,
106 .integer = phandle,
107 };
108
109 return handle;
110 }
111
112 int
113 devhandle_to_of(devhandle_t const handle)
114 {
115 KASSERT(devhandle_type(handle) == DEVHANDLE_TYPE_OF);
116
117 return handle.integer;
118 }
119
120 static int
121 of_device_enumerate_children(device_t dev, devhandle_t call_handle, void *v)
122 {
123 struct device_enumerate_children_args *args = v;
124 int phandle = devhandle_to_of(call_handle);
125 int child;
126
127 for (child = OF_child(phandle); child != 0; child = OF_peer(child)) {
128 if (!args->callback(dev, devhandle_from_of(child),
129 args->callback_arg)) {
130 break;
131 }
132 }
133
134 return 0;
135 }
136 OF_DEVICE_CALL_REGISTER(DEVICE_ENUMERATE_CHILDREN_STR,
137 of_device_enumerate_children)
138
139 /*
140 * int of_decode_int(p)
141 *
142 * This routine converts OFW encoded-int datums
143 * into the integer format of the host machine.
144 *
145 * It is primarily used to convert integer properties
146 * returned by the OF_getprop routine.
147 *
148 * Arguments:
149 * p pointer to unsigned char array which is an
150 * OFW-encoded integer.
151 *
152 * Return Value:
153 * Decoded integer value of argument p.
154 *
155 * Side Effects:
156 * None.
157 */
158 int
159 of_decode_int(const unsigned char *p)
160 {
161 unsigned int i = *p++ << 8;
162 i = (i + *p++) << 8;
163 i = (i + *p++) << 8;
164 return (i + *p);
165 }
166
167 /*
168 * int of_compatible(phandle, strings)
169 *
170 * This routine checks an OFW node's "compatible" entry to see if
171 * it matches any of the provided strings.
172 *
173 * of_compatible_match() is the preferred way to perform driver
174 * compatibility match. However, this routine that deals with
175 * only strings is useful in some situations and is provided for
176 * convenience.
177 *
178 * Arguments:
179 * phandle OFW phandle of device to be checked for
180 * compatibility.
181 * strings Array of containing expected "compatibility"
182 * property values, presence of any of which
183 * indicates compatibility.
184 *
185 * Return Value:
186 * 0 if none of the strings are found in phandle's "compatibility"
187 * property, or the reverse index of the matching string in the
188 * phandle's "compatibility" property plus 1.
189 *
190 * Side Effects:
191 * None.
192 */
193 int
194 of_compatible(int phandle, const char * const *strings)
195 {
196 char *prop, propbuf[OFW_MAX_STACK_BUF_SIZE];
197 const char *cp;
198 int proplen, match = 0;
199
200 proplen = OF_getproplen(phandle, "compatible");
201 if (proplen <= 0) {
202 return 0;
203 }
204
205 prop = kmem_tmpbuf_alloc(proplen, propbuf, sizeof(propbuf), KM_SLEEP);
206
207 if (OF_getprop(phandle, "compatible", prop, proplen) != proplen) {
208 goto out;
209 }
210
211 for (; (cp = *strings) != NULL; strings++) {
212 if ((match = strlist_match(prop, proplen, cp)) != 0) {
213 break;
214 }
215 }
216
217 out:
218 kmem_tmpbuf_free(prop, proplen, propbuf);
219 return match;
220 }
221
222 /*
223 * int of_compatible_match(phandle, compat_data)
224 *
225 * This routine searches an array of device_compatible_entry structures
226 * for a matching "compatible" entry matching the supplied OFW node,
227 * and returns a weighted match value corresponding to which string
228 * from the "compatible" property was matched, which more weight given
229 * to the first string than the last.
230 *
231 * It should be used when determining whether a driver can drive
232 * a particular device.
233 *
234 * Arguments:
235 * phandle OFW phandle of device to be checked for
236 * compatibility.
237 * compat_data Array of possible compat entry strings and
238 * associated metadata. The last entry in the
239 * list should have a "compat" of NULL to terminate
240 * the list.
241 *
242 * Return Value:
243 * 0 if none of the strings are found in phandle's "compatibility"
244 * property, or a positive number based on the reverse index of the
245 * matching string in the phandle's "compatibility" property, plus 1.
246 *
247 * Side Effects:
248 * None.
249 */
250 int
251 of_compatible_match(int phandle,
252 const struct device_compatible_entry *compat_data)
253 {
254 char *prop, propbuf[OFW_MAX_STACK_BUF_SIZE];
255 int proplen, match = 0;
256
257 proplen = OF_getproplen(phandle, "compatible");
258 if (proplen <= 0) {
259 return 0;
260 }
261
262 prop = kmem_tmpbuf_alloc(proplen, propbuf, sizeof(propbuf), KM_SLEEP);
263
264 if (OF_getprop(phandle, "compatible", prop, proplen) != proplen) {
265 goto out;
266 }
267
268 match = device_compatible_match_strlist(prop, proplen, compat_data);
269
270 out:
271 kmem_tmpbuf_free(prop, proplen, propbuf);
272 return match;
273 }
274
275 /*
276 * const struct device_compatible_entry *of_compatible_lookup(phandle,
277 * compat_data)
278 *
279 * This routine searches an array of device_compatible_entry structures
280 * for a "compatible" entry matching the supplied OFW node.
281 *
282 * Arguments:
283 * phandle OFW phandle of device to be checked for
284 * compatibility.
285 * compat_data Array of possible compat entry strings and
286 * associated metadata. The last entry in the
287 * list should have a "compat" of NULL to terminate
288 * the list.
289 *
290 * Return Value:
291 * The first matching compat_data entry in the array. If no matches
292 * are found, NULL is returned.
293 *
294 * Side Effects:
295 * None.
296 */
297 const struct device_compatible_entry *
298 of_compatible_lookup(int phandle,
299 const struct device_compatible_entry *compat_data)
300 {
301 char *prop, propbuf[OFW_MAX_STACK_BUF_SIZE];
302 const struct device_compatible_entry *match = NULL;
303 int proplen;
304
305 proplen = OF_getproplen(phandle, "compatible");
306 if (proplen <= 0) {
307 return 0;
308 }
309
310 prop = kmem_tmpbuf_alloc(proplen, propbuf, sizeof(propbuf), KM_SLEEP);
311
312 if (OF_getprop(phandle, "compatible", prop, proplen) != proplen) {
313 goto out;
314 }
315
316 match = device_compatible_lookup_strlist(prop, proplen, compat_data);
317
318 out:
319 kmem_tmpbuf_free(prop, proplen, propbuf);
320 return match;
321 }
322
323 /*
324 * int of_packagename(phandle, buf, bufsize)
325 *
326 * This routine places the last component of an OFW node's name
327 * into a user-provided buffer.
328 *
329 * It can be used during autoconfiguration to make printing of
330 * device names more informative.
331 *
332 * Arguments:
333 * phandle OFW phandle of device whose name name is
334 * desired.
335 * buf Buffer to contain device name, provided by
336 * caller. (For now, must be at least 4
337 * bytes long.)
338 * bufsize Length of buffer referenced by 'buf', in
339 * bytes.
340 *
341 * Return Value:
342 * -1 if the device path name could not be obtained or would
343 * not fit in the allocated temporary buffer, or zero otherwise
344 * (meaning that the leaf node name was successfully extracted).
345 *
346 * Side Effects:
347 * If the leaf node name was successfully extracted, 'buf' is
348 * filled in with at most 'bufsize' bytes of the leaf node
349 * name. If the leaf node was not successfully extracted, a
350 * somewhat meaningful string is placed in the buffer. In
351 * either case, the contents of 'buf' will be NUL-terminated.
352 */
353 int
354 of_packagename(int phandle, char *buf, int bufsize)
355 {
356 char *pbuf;
357 const char *lastslash;
358 int l, rv;
359
360 pbuf = kmem_alloc(OFW_PATH_BUF_SIZE, KM_SLEEP);
361 l = OF_package_to_path(phandle, pbuf, OFW_PATH_BUF_SIZE);
362
363 /* check that we could get the name, and that it's not too long. */
364 if (l < 0 ||
365 (l == OFW_PATH_BUF_SIZE && pbuf[OFW_PATH_BUF_SIZE - 1] != '\0')) {
366 if (bufsize >= 25)
367 snprintf(buf, bufsize, "??? (phandle 0x%x)", phandle);
368 else if (bufsize >= 4)
369 strlcpy(buf, "???", bufsize);
370 else
371 panic("of_packagename: bufsize = %d is silly",
372 bufsize);
373 rv = -1;
374 } else {
375 pbuf[l] = '\0';
376 lastslash = strrchr(pbuf, '/');
377 strlcpy(buf, (lastslash == NULL) ? pbuf : (lastslash + 1),
378 bufsize);
379 rv = 0;
380 }
381
382 kmem_free(pbuf, OFW_PATH_BUF_SIZE);
383 return (rv);
384 }
385
386 /*
387 * Find the first child of a given node that matches name. Does not recurse.
388 */
389 int
390 of_find_firstchild_byname(int node, const char *name)
391 {
392 char namex[32];
393 int nn;
394
395 for (nn = OF_child(node); nn; nn = OF_peer(nn)) {
396 memset(namex, 0, sizeof(namex));
397 if (OF_getprop(nn, "name", namex, sizeof(namex)) == -1)
398 continue;
399 if (strcmp(name, namex) == 0)
400 return nn;
401 }
402 return -1;
403 }
404
405 /*
406 * Find a child node that is compatible with str. Recurses, starting at node.
407 */
408 int
409 of_find_bycompat(int node, const char *str)
410 {
411 const char * compatible[] = { str, NULL };
412 int child, ret;
413
414 for (child = OF_child(node); child; child = OF_peer(child)) {
415 if (of_compatible(child, compatible))
416 return child;
417 ret = of_find_bycompat(child, str);
418 if (ret != -1)
419 return ret;
420 }
421
422 return -1;
423 }
424
425 /*
426 * Find a give node by name. Recurses, and seems to walk upwards too.
427 */
428
429 int
430 of_getnode_byname(int start, const char *target)
431 {
432 int node, next;
433 char name[64];
434
435 if (start == 0)
436 start = OF_peer(0);
437
438 for (node = start; node; node = next) {
439 memset(name, 0, sizeof name);
440 OF_getprop(node, "name", name, sizeof name - 1);
441 if (strcmp(name, target) == 0)
442 break;
443
444 if ((next = OF_child(node)) != 0)
445 continue;
446
447 while (node) {
448 if ((next = OF_peer(node)) != 0)
449 break;
450 node = OF_parent(node);
451 }
452 }
453
454 /* XXX is this correct? */
455 return node;
456 }
457
458 /*
459 * Create a uint32_t integer property from an OFW node property.
460 */
461
462 bool
463 of_to_uint32_prop(prop_dictionary_t dict, int node, const char *ofname,
464 const char *propname)
465 {
466 uint32_t prop;
467
468 if (OF_getprop(node, ofname, &prop, sizeof(prop)) != sizeof(prop))
469 return FALSE;
470
471 return(prop_dictionary_set_uint32(dict, propname, prop));
472 }
473
474 /*
475 * Create a data property from an OFW node property. Max size of 256bytes.
476 */
477
478 bool
479 of_to_dataprop(prop_dictionary_t dict, int node, const char *ofname,
480 const char *propname)
481 {
482 int len;
483 uint8_t prop[256];
484
485 len = OF_getprop(node, ofname, prop, 256);
486 if (len < 1)
487 return FALSE;
488
489 return prop_dictionary_set_data(dict, propname, prop, len);
490 }
491
492 /*
493 * look at output-device, see if there's a Sun-typical video mode specifier as
494 * in screen:r1024x768x60 attached. If found copy it into *buffer, otherwise
495 * return NULL
496 */
497
498 char *
499 of_get_mode_string(char *buffer, int len)
500 {
501 int options;
502 char *pos, output_device[256];
503
504 /*
505 * finally, let's see if there's a video mode specified in
506 * output-device and pass it on so there's at least some way
507 * to program video modes
508 */
509 options = OF_finddevice("/options");
510 if ((options == 0) || (options == -1))
511 return NULL;
512 if (OF_getprop(options, "output-device", output_device, 256) == 0)
513 return NULL;
514
515 /* find the mode string if there is one */
516 pos = strstr(output_device, ":r");
517 if (pos == NULL)
518 return NULL;
519 strncpy(buffer, pos + 2, len);
520 return buffer;
521 }
522
523 /*
524 * of_device_from_phandle --
525 *
526 * Return a device_t associated with the specified phandle.
527 *
528 * This is expected to be used rarely, so we don't care if
529 * it's fast. Also, it can only find devices that have
530 * gone through of_device_register() (obviously).
531 */
532 device_t
533 of_device_from_phandle(int phandle)
534 {
535 devhandle_t devhandle;
536 deviter_t di;
537 device_t dev;
538
539 for (dev = deviter_first(&di, DEVITER_F_ROOT_FIRST);
540 dev != NULL;
541 dev = deviter_next(&di)) {
542 devhandle = device_handle(dev);
543 if (devhandle_type(devhandle) == DEVHANDLE_TYPE_OF) {
544 if (devhandle_to_of(devhandle) == phandle) {
545 /* Found it! */
546 break;
547 }
548 }
549 }
550 deviter_release(&di);
551 return dev;
552 }
553
554 /*
555 * Returns true if the specified property is present.
556 */
557 bool
558 of_hasprop(int node, const char *prop)
559 {
560 return OF_getproplen(node, prop) >= 0;
561 }
562
563 /*
564 * Get the value of a uint32 property, compensating for host byte order.
565 * Returns 0 on success, non-zero on failure.
566 */
567 int
568 of_getprop_uint32(int node, const char *prop, uint32_t *val)
569 {
570 uint32_t v;
571 int len;
572
573 len = OF_getprop(node, prop, &v, sizeof(v));
574 if (len != sizeof(v))
575 return -1;
576
577 *val = be32toh(v);
578 return 0;
579 }
580
581 int
582 of_getprop_uint32_array(int node, const char *prop, uint32_t *array, int n)
583 {
584 uint32_t *v = array;
585 int len;
586
587 len = OF_getprop(node, prop, array, n * sizeof(*v));
588 if (len < (int)(n * sizeof(*v)))
589 return -1;
590
591 for (; n > 0; n--) {
592 BE32TOH(*v);
593 v++;
594 }
595
596 return 0;
597 }
598 /*
599 * Get the value of a uint64 property, compensating for host byte order.
600 * Returns 0 on success, non-zero on failure.
601 */
602 int
603 of_getprop_uint64(int node, const char *prop, uint64_t *val)
604 {
605 uint64_t v;
606 int len;
607
608 len = OF_getprop(node, prop, &v, sizeof(v));
609 if (len != sizeof(v))
610 return -1;
611
612 *val = be64toh(v);
613 return 0;
614 }
615