]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ofw/ofw_bus_subr.c
MFV r304732.
[FreeBSD/FreeBSD.git] / sys / dev / ofw / ofw_bus_subr.c
1 /*-
2  * Copyright (c) 2001 - 2003 by Thomas Moestl <tmm@FreeBSD.org>.
3  * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions, and the following disclaimer,
11  *    without modification, immediately at the beginning of the file.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_platform.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/errno.h>
38 #include <sys/libkern.h>
39
40 #include <machine/resource.h>
41
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 #include <dev/ofw/openfirm.h>
45
46 #include "ofw_bus_if.h"
47
48 int
49 ofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *obd, phandle_t node)
50 {
51
52         if (obd == NULL)
53                 return (ENOMEM);
54         /* The 'name' property is considered mandatory. */
55         if ((OF_getprop_alloc(node, "name", 1, (void **)&obd->obd_name)) == -1)
56                 return (EINVAL);
57         OF_getprop_alloc(node, "compatible", 1, (void **)&obd->obd_compat);
58         OF_getprop_alloc(node, "device_type", 1, (void **)&obd->obd_type);
59         OF_getprop_alloc(node, "model", 1, (void **)&obd->obd_model);
60         OF_getprop_alloc(node, "status", 1, (void **)&obd->obd_status);
61         obd->obd_node = node;
62         return (0);
63 }
64
65 void
66 ofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *obd)
67 {
68
69         if (obd == NULL)
70                 return;
71         if (obd->obd_compat != NULL)
72                 free(obd->obd_compat, M_OFWPROP);
73         if (obd->obd_model != NULL)
74                 free(obd->obd_model, M_OFWPROP);
75         if (obd->obd_name != NULL)
76                 free(obd->obd_name, M_OFWPROP);
77         if (obd->obd_type != NULL)
78                 free(obd->obd_type, M_OFWPROP);
79         if (obd->obd_status != NULL)
80                 free(obd->obd_status, M_OFWPROP);
81 }
82
83 int
84 ofw_bus_gen_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
85     size_t buflen)
86 {
87
88         if (ofw_bus_get_name(child) != NULL) {
89                 strlcat(buf, "name=", buflen);
90                 strlcat(buf, ofw_bus_get_name(child), buflen);
91         }
92
93         if (ofw_bus_get_compat(child) != NULL) {
94                 strlcat(buf, " compat=", buflen);
95                 strlcat(buf, ofw_bus_get_compat(child), buflen);
96         }
97         return (0);
98 };
99
100 const char *
101 ofw_bus_gen_get_compat(device_t bus, device_t dev)
102 {
103         const struct ofw_bus_devinfo *obd;
104
105         obd = OFW_BUS_GET_DEVINFO(bus, dev);
106         if (obd == NULL)
107                 return (NULL);
108         return (obd->obd_compat);
109 }
110
111 const char *
112 ofw_bus_gen_get_model(device_t bus, device_t dev)
113 {
114         const struct ofw_bus_devinfo *obd;
115
116         obd = OFW_BUS_GET_DEVINFO(bus, dev);
117         if (obd == NULL)
118                 return (NULL);
119         return (obd->obd_model);
120 }
121
122 const char *
123 ofw_bus_gen_get_name(device_t bus, device_t dev)
124 {
125         const struct ofw_bus_devinfo *obd;
126
127         obd = OFW_BUS_GET_DEVINFO(bus, dev);
128         if (obd == NULL)
129                 return (NULL);
130         return (obd->obd_name);
131 }
132
133 phandle_t
134 ofw_bus_gen_get_node(device_t bus, device_t dev)
135 {
136         const struct ofw_bus_devinfo *obd;
137
138         obd = OFW_BUS_GET_DEVINFO(bus, dev);
139         if (obd == NULL)
140                 return (0);
141         return (obd->obd_node);
142 }
143
144 const char *
145 ofw_bus_gen_get_type(device_t bus, device_t dev)
146 {
147         const struct ofw_bus_devinfo *obd;
148
149         obd = OFW_BUS_GET_DEVINFO(bus, dev);
150         if (obd == NULL)
151                 return (NULL);
152         return (obd->obd_type);
153 }
154
155 const char *
156 ofw_bus_get_status(device_t dev)
157 {
158         const struct ofw_bus_devinfo *obd;
159
160         obd = OFW_BUS_GET_DEVINFO(device_get_parent(dev), dev);
161         if (obd == NULL)
162                 return (NULL);
163
164         return (obd->obd_status);
165 }
166
167 int
168 ofw_bus_status_okay(device_t dev)
169 {
170         const char *status;
171
172         status = ofw_bus_get_status(dev);
173         if (status == NULL || strcmp(status, "okay") == 0 ||
174             strcmp(status, "ok") == 0)
175                 return (1);
176         
177         return (0);
178 }
179
180 static int
181 ofw_bus_node_is_compatible(const char *compat, int len, const char *onecompat)
182 {
183         int onelen, l, ret;
184
185         onelen = strlen(onecompat);
186
187         ret = 0;
188         while (len > 0) {
189                 if (strlen(compat) == onelen &&
190                     strncasecmp(compat, onecompat, onelen) == 0) {
191                         /* Found it. */
192                         ret = 1;
193                         break;
194                 }
195
196                 /* Slide to the next sub-string. */
197                 l = strlen(compat) + 1;
198                 compat += l;
199                 len -= l;
200         }
201
202         return (ret);
203 }
204
205 int
206 ofw_bus_is_compatible(device_t dev, const char *onecompat)
207 {
208         phandle_t node;
209         const char *compat;
210         int len;
211
212         if ((compat = ofw_bus_get_compat(dev)) == NULL)
213                 return (0);
214
215         if ((node = ofw_bus_get_node(dev)) == -1)
216                 return (0);
217
218         /* Get total 'compatible' prop len */
219         if ((len = OF_getproplen(node, "compatible")) <= 0)
220                 return (0);
221
222         return (ofw_bus_node_is_compatible(compat, len, onecompat));
223 }
224
225 int
226 ofw_bus_is_compatible_strict(device_t dev, const char *compatible)
227 {
228         const char *compat;
229         size_t len;
230
231         if ((compat = ofw_bus_get_compat(dev)) == NULL)
232                 return (0);
233
234         len = strlen(compatible);
235         if (strlen(compat) == len &&
236             strncasecmp(compat, compatible, len) == 0)
237                 return (1);
238
239         return (0);
240 }
241
242 const struct ofw_compat_data *
243 ofw_bus_search_compatible(device_t dev, const struct ofw_compat_data *compat)
244 {
245
246         if (compat == NULL)
247                 return NULL;
248
249         for (; compat->ocd_str != NULL; ++compat) {
250                 if (ofw_bus_is_compatible(dev, compat->ocd_str))
251                         break;
252         }
253
254         return (compat);
255 }
256
257 int
258 ofw_bus_has_prop(device_t dev, const char *propname)
259 {
260         phandle_t node;
261
262         if ((node = ofw_bus_get_node(dev)) == -1)
263                 return (0);
264
265         return (OF_hasprop(node, propname));
266 }
267
268 void
269 ofw_bus_setup_iinfo(phandle_t node, struct ofw_bus_iinfo *ii, int intrsz)
270 {
271         pcell_t addrc;
272         int msksz;
273
274         if (OF_getencprop(node, "#address-cells", &addrc, sizeof(addrc)) == -1)
275                 addrc = 2;
276         ii->opi_addrc = addrc * sizeof(pcell_t);
277
278         ii->opi_imapsz = OF_getencprop_alloc(node, "interrupt-map", 1,
279             (void **)&ii->opi_imap);
280         if (ii->opi_imapsz > 0) {
281                 msksz = OF_getencprop_alloc(node, "interrupt-map-mask", 1,
282                     (void **)&ii->opi_imapmsk);
283                 /*
284                  * Failure to get the mask is ignored; a full mask is used
285                  * then.  We barf on bad mask sizes, however.
286                  */
287                 if (msksz != -1 && msksz != ii->opi_addrc + intrsz)
288                         panic("ofw_bus_setup_iinfo: bad interrupt-map-mask "
289                             "property!");
290         }
291 }
292
293 int
294 ofw_bus_lookup_imap(phandle_t node, struct ofw_bus_iinfo *ii, void *reg,
295     int regsz, void *pintr, int pintrsz, void *mintr, int mintrsz,
296     phandle_t *iparent)
297 {
298         uint8_t maskbuf[regsz + pintrsz];
299         int rv;
300
301         if (ii->opi_imapsz <= 0)
302                 return (0);
303         KASSERT(regsz >= ii->opi_addrc,
304             ("ofw_bus_lookup_imap: register size too small: %d < %d",
305                 regsz, ii->opi_addrc));
306         if (node != -1) {
307                 rv = OF_getencprop(node, "reg", reg, regsz);
308                 if (rv < regsz)
309                         panic("ofw_bus_lookup_imap: cannot get reg property");
310         }
311         return (ofw_bus_search_intrmap(pintr, pintrsz, reg, ii->opi_addrc,
312             ii->opi_imap, ii->opi_imapsz, ii->opi_imapmsk, maskbuf, mintr,
313             mintrsz, iparent));
314 }
315
316 /*
317  * Map an interrupt using the firmware reg, interrupt-map and
318  * interrupt-map-mask properties.
319  * The interrupt property to be mapped must be of size intrsz, and pointed to
320  * by intr.  The regs property of the node for which the mapping is done must
321  * be passed as regs. This property is an array of register specifications;
322  * the size of the address part of such a specification must be passed as
323  * physsz.  Only the first element of the property is used.
324  * imap and imapsz hold the interrupt mask and it's size.
325  * imapmsk is a pointer to the interrupt-map-mask property, which must have
326  * a size of physsz + intrsz; it may be NULL, in which case a full mask is
327  * assumed.
328  * maskbuf must point to a buffer of length physsz + intrsz.
329  * The interrupt is returned in result, which must point to a buffer of length
330  * rintrsz (which gives the expected size of the mapped interrupt).
331  * Returns number of cells in the interrupt if a mapping was found, 0 otherwise.
332  */
333 int
334 ofw_bus_search_intrmap(void *intr, int intrsz, void *regs, int physsz,
335     void *imap, int imapsz, void *imapmsk, void *maskbuf, void *result,
336     int rintrsz, phandle_t *iparent)
337 {
338         phandle_t parent;
339         uint8_t *ref = maskbuf;
340         uint8_t *uiintr = intr;
341         uint8_t *uiregs = regs;
342         uint8_t *uiimapmsk = imapmsk;
343         uint8_t *mptr;
344         pcell_t paddrsz;
345         pcell_t pintrsz;
346         int i, rsz, tsz;
347
348         rsz = -1;
349         if (imapmsk != NULL) {
350                 for (i = 0; i < physsz; i++)
351                         ref[i] = uiregs[i] & uiimapmsk[i];
352                 for (i = 0; i < intrsz; i++)
353                         ref[physsz + i] = uiintr[i] & uiimapmsk[physsz + i];
354         } else {
355                 bcopy(regs, ref, physsz);
356                 bcopy(intr, ref + physsz, intrsz);
357         }
358
359         mptr = imap;
360         i = imapsz;
361         paddrsz = 0;
362         while (i > 0) {
363                 bcopy(mptr + physsz + intrsz, &parent, sizeof(parent));
364 #ifndef OFW_IMAP_NO_IPARENT_ADDR_CELLS
365                 /*
366                  * Find if we need to read the parent address data.
367                  * CHRP-derived OF bindings, including ePAPR-compliant FDTs,
368                  * use this as an optional part of the specifier.
369                  */
370                 if (OF_getencprop(OF_node_from_xref(parent),
371                     "#address-cells", &paddrsz, sizeof(paddrsz)) == -1)
372                         paddrsz = 0;    /* default */
373                 paddrsz *= sizeof(pcell_t);
374 #endif
375
376                 if (OF_searchencprop(OF_node_from_xref(parent),
377                     "#interrupt-cells", &pintrsz, sizeof(pintrsz)) == -1)
378                         pintrsz = 1;    /* default */
379                 pintrsz *= sizeof(pcell_t);
380
381                 /* Compute the map stride size. */
382                 tsz = physsz + intrsz + sizeof(phandle_t) + paddrsz + pintrsz;
383                 KASSERT(i >= tsz, ("ofw_bus_search_intrmap: truncated map"));
384
385                 if (bcmp(ref, mptr, physsz + intrsz) == 0) {
386                         bcopy(mptr + physsz + intrsz + sizeof(parent) + paddrsz,
387                             result, MIN(rintrsz, pintrsz));
388
389                         if (iparent != NULL)
390                                 *iparent = parent;
391                         return (pintrsz/sizeof(pcell_t));
392                 }
393                 mptr += tsz;
394                 i -= tsz;
395         }
396         return (0);
397 }
398
399 int
400 ofw_bus_msimap(phandle_t node, uint16_t pci_rid, phandle_t *msi_parent,
401     uint32_t *msi_rid)
402 {
403         pcell_t *map, mask, msi_base, rid_base, rid_length;
404         ssize_t len;
405         uint32_t masked_rid, rid;
406         int err, i;
407
408         /* TODO: This should be OF_searchprop_alloc if we had it */
409         len = OF_getencprop_alloc(node, "msi-map", sizeof(*map), (void **)&map);
410         if (len < 0) {
411                 if (msi_parent != NULL) {
412                         *msi_parent = 0;
413                         OF_getencprop(node, "msi-parent", msi_parent,
414                             sizeof(*msi_parent));
415                 }
416                 if (msi_rid != NULL)
417                         *msi_rid = pci_rid;
418                 return (0);
419         }
420
421         err = ENOENT;
422         rid = 0;
423         mask = 0xffffffff;
424         OF_getencprop(node, "msi-map-mask", &mask, sizeof(mask));
425
426         masked_rid = pci_rid & mask;
427         for (i = 0; i < len; i += 4) {
428                 rid_base = map[i + 0];
429                 rid_length = map[i + 3];
430
431                 if (masked_rid < rid_base ||
432                     masked_rid >= (rid_base + rid_length))
433                         continue;
434
435                 msi_base = map[i + 2];
436
437                 if (msi_parent != NULL)
438                         *msi_parent = map[i + 1];
439                 if (msi_rid != NULL)
440                         *msi_rid = masked_rid - rid_base + msi_base;
441                 err = 0;
442                 break;
443         }
444
445         free(map, M_OFWPROP);
446
447         return (err);
448 }
449
450 int
451 ofw_bus_reg_to_rl(device_t dev, phandle_t node, pcell_t acells, pcell_t scells,
452     struct resource_list *rl)
453 {
454         uint64_t phys, size;
455         ssize_t i, j, rid, nreg, ret;
456         uint32_t *reg;
457         char *name;
458
459         /*
460          * This may be just redundant when having ofw_bus_devinfo
461          * but makes this routine independent of it.
462          */
463         ret = OF_getprop_alloc(node, "name", sizeof(*name), (void **)&name);
464         if (ret == -1)
465                 name = NULL;
466
467         ret = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
468         nreg = (ret == -1) ? 0 : ret;
469
470         if (nreg % (acells + scells) != 0) {
471                 if (bootverbose)
472                         device_printf(dev, "Malformed reg property on <%s>\n",
473                             (name == NULL) ? "unknown" : name);
474                 nreg = 0;
475         }
476
477         for (i = 0, rid = 0; i < nreg; i += acells + scells, rid++) {
478                 phys = size = 0;
479                 for (j = 0; j < acells; j++) {
480                         phys <<= 32;
481                         phys |= reg[i + j];
482                 }
483                 for (j = 0; j < scells; j++) {
484                         size <<= 32;
485                         size |= reg[i + acells + j];
486                 }
487                 /* Skip the dummy reg property of glue devices like ssm(4). */
488                 if (size != 0)
489                         resource_list_add(rl, SYS_RES_MEMORY, rid,
490                             phys, phys + size - 1, size);
491         }
492         free(name, M_OFWPROP);
493         free(reg, M_OFWPROP);
494
495         return (0);
496 }
497
498 /*
499  * Get interrupt parent for given node.
500  * Returns 0 if interrupt parent doesn't exist.
501  */
502 phandle_t
503 ofw_bus_find_iparent(phandle_t node)
504 {
505         phandle_t iparent;
506
507         if (OF_searchencprop(node, "interrupt-parent", &iparent,
508                     sizeof(iparent)) == -1) {
509                 for (iparent = node; iparent != 0;
510                     iparent = OF_parent(iparent)) {
511                         if (OF_hasprop(iparent, "interrupt-controller"))
512                                 break;
513                 }
514                 iparent = OF_xref_from_node(iparent);
515         }
516         return (iparent);
517 }
518
519 int
520 ofw_bus_intr_to_rl(device_t dev, phandle_t node,
521     struct resource_list *rl, int *rlen)
522 {
523         phandle_t iparent;
524         uint32_t icells, *intr;
525         int err, i, irqnum, nintr, rid;
526         boolean_t extended;
527
528         nintr = OF_getencprop_alloc(node, "interrupts",  sizeof(*intr),
529             (void **)&intr);
530         if (nintr > 0) {
531                 iparent = ofw_bus_find_iparent(node);
532                 if (iparent == 0) {
533                         device_printf(dev, "No interrupt-parent found, "
534                             "assuming direct parent\n");
535                         iparent = OF_parent(node);
536                         iparent = OF_xref_from_node(iparent);
537                 }
538                 if (OF_searchencprop(OF_node_from_xref(iparent), 
539                     "#interrupt-cells", &icells, sizeof(icells)) == -1) {
540                         device_printf(dev, "Missing #interrupt-cells "
541                             "property, assuming <1>\n");
542                         icells = 1;
543                 }
544                 if (icells < 1 || icells > nintr) {
545                         device_printf(dev, "Invalid #interrupt-cells property "
546                             "value <%d>, assuming <1>\n", icells);
547                         icells = 1;
548                 }
549                 extended = false;
550         } else {
551                 nintr = OF_getencprop_alloc(node, "interrupts-extended",
552                     sizeof(*intr), (void **)&intr);
553                 if (nintr <= 0)
554                         return (0);
555                 extended = true;
556         }
557         err = 0;
558         rid = 0;
559         for (i = 0; i < nintr; i += icells) {
560                 if (extended) {
561                         iparent = intr[i++];
562                         if (OF_searchencprop(OF_node_from_xref(iparent), 
563                             "#interrupt-cells", &icells, sizeof(icells)) == -1) {
564                                 device_printf(dev, "Missing #interrupt-cells "
565                                     "property\n");
566                                 err = ENOENT;
567                                 break;
568                         }
569                         if (icells < 1 || (i + icells) > nintr) {
570                                 device_printf(dev, "Invalid #interrupt-cells "
571                                     "property value <%d>\n", icells);
572                                 err = ERANGE;
573                                 break;
574                         }
575                 }
576                 irqnum = ofw_bus_map_intr(dev, iparent, icells, &intr[i]);
577                 resource_list_add(rl, SYS_RES_IRQ, rid++, irqnum, irqnum, 1);
578         }
579         if (rlen != NULL)
580                 *rlen = rid;
581         free(intr, M_OFWPROP);
582         return (err);
583 }
584
585 int
586 ofw_bus_intr_by_rid(device_t dev, phandle_t node, int wanted_rid,
587     phandle_t *producer, int *ncells, pcell_t **cells)
588 {
589         phandle_t iparent;
590         uint32_t icells, *intr;
591         int err, i, nintr, rid;
592         boolean_t extended;
593
594         nintr = OF_getencprop_alloc(node, "interrupts",  sizeof(*intr),
595             (void **)&intr);
596         if (nintr > 0) {
597                 iparent = ofw_bus_find_iparent(node);
598                 if (iparent == 0) {
599                         device_printf(dev, "No interrupt-parent found, "
600                             "assuming direct parent\n");
601                         iparent = OF_parent(node);
602                         iparent = OF_xref_from_node(iparent);
603                 }
604                 if (OF_searchencprop(OF_node_from_xref(iparent),
605                     "#interrupt-cells", &icells, sizeof(icells)) == -1) {
606                         device_printf(dev, "Missing #interrupt-cells "
607                             "property, assuming <1>\n");
608                         icells = 1;
609                 }
610                 if (icells < 1 || icells > nintr) {
611                         device_printf(dev, "Invalid #interrupt-cells property "
612                             "value <%d>, assuming <1>\n", icells);
613                         icells = 1;
614                 }
615                 extended = false;
616         } else {
617                 nintr = OF_getencprop_alloc(node, "interrupts-extended",
618                     sizeof(*intr), (void **)&intr);
619                 if (nintr <= 0)
620                         return (ESRCH);
621                 extended = true;
622         }
623         err = ESRCH;
624         rid = 0;
625         for (i = 0; i < nintr; i += icells, rid++) {
626                 if (extended) {
627                         iparent = intr[i++];
628                         if (OF_searchencprop(OF_node_from_xref(iparent),
629                             "#interrupt-cells", &icells, sizeof(icells)) == -1) {
630                                 device_printf(dev, "Missing #interrupt-cells "
631                                     "property\n");
632                                 err = ENOENT;
633                                 break;
634                         }
635                         if (icells < 1 || (i + icells) > nintr) {
636                                 device_printf(dev, "Invalid #interrupt-cells "
637                                     "property value <%d>\n", icells);
638                                 err = ERANGE;
639                                 break;
640                         }
641                 }
642                 if (rid == wanted_rid) {
643                         *cells = malloc(icells * sizeof(**cells), M_OFWPROP,
644                             M_WAITOK);
645                         *producer = iparent;
646                         *ncells= icells;
647                         memcpy(*cells, intr + i, icells * sizeof(**cells));
648                         err = 0;
649                         break;
650                 }
651         }
652         free(intr, M_OFWPROP);
653         return (err);
654 }
655
656 phandle_t
657 ofw_bus_find_child(phandle_t start, const char *child_name)
658 {
659         char *name;
660         int ret;
661         phandle_t child;
662
663         for (child = OF_child(start); child != 0; child = OF_peer(child)) {
664                 ret = OF_getprop_alloc(child, "name", sizeof(*name), (void **)&name);
665                 if (ret == -1)
666                         continue;
667                 if (strcmp(name, child_name) == 0) {
668                         free(name, M_OFWPROP);
669                         return (child);
670                 }
671
672                 free(name, M_OFWPROP);
673         }
674
675         return (0);
676 }
677
678 phandle_t
679 ofw_bus_find_compatible(phandle_t node, const char *onecompat)
680 {
681         phandle_t child, ret;
682         void *compat;
683         int len;
684
685         /*
686          * Traverse all children of 'start' node, and find first with
687          * matching 'compatible' property.
688          */
689         for (child = OF_child(node); child != 0; child = OF_peer(child)) {
690                 len = OF_getprop_alloc(child, "compatible", 1, &compat);
691                 if (len >= 0) {
692                         ret = ofw_bus_node_is_compatible(compat, len,
693                             onecompat);
694                         free(compat, M_OFWPROP);
695                         if (ret != 0)
696                                 return (child);
697                 }
698
699                 ret = ofw_bus_find_compatible(child, onecompat);
700                 if (ret != 0)
701                         return (ret);
702         }
703         return (0);
704 }
705
706 /**
707  * @brief Return child of bus whose phandle is node
708  *
709  * A direct child of @p will be returned if it its phandle in the
710  * OFW tree is @p node. Otherwise, NULL is returned.
711  *
712  * @param bus           The bus to examine
713  * @param node          The phandle_t to look for.
714  */
715 device_t
716 ofw_bus_find_child_device_by_phandle(device_t bus, phandle_t node)
717 {
718         device_t *children, retval, child;
719         int nkid, i;
720
721         /*
722          * Nothing can match the flag value for no node.
723          */
724         if (node == -1)
725                 return (NULL);
726
727         /*
728          * Search the children for a match. We microoptimize
729          * a bit by not using ofw_bus_get since we already know
730          * the parent. We do not recurse.
731          */
732         if (device_get_children(bus, &children, &nkid) != 0)
733                 return (NULL);
734         retval = NULL;
735         for (i = 0; i < nkid; i++) {
736                 child = children[i];
737                 if (OFW_BUS_GET_NODE(bus, child) == node) {
738                         retval = child;
739                         break;
740                 }
741         }
742         free(children, M_TEMP);
743
744         return (retval);
745 }
746
747 /*
748  * Parse property that contain list of xrefs and values
749  * (like standard "clocks" and "resets" properties)
750  * Input arguments:
751  *  node - consumers device node
752  *  list_name  - name of parsed list - "clocks"
753  *  cells_name - name of size property - "#clock-cells"
754  *  idx - the index of the requested list entry, or, if -1, an indication
755  *        to return the number of entries in the parsed list.
756  * Output arguments:
757  *  producer - handle of producer
758  *  ncells   - number of cells in result or the number of items in the list when
759  *             idx == -1.
760  *  cells    - array of decoded cells
761  */
762 static int
763 ofw_bus_parse_xref_list_internal(phandle_t node, const char *list_name,
764     const char *cells_name, int idx, phandle_t *producer, int *ncells,
765     pcell_t **cells)
766 {
767         phandle_t pnode;
768         phandle_t *elems;
769         uint32_t  pcells;
770         int rv, i, j, nelems, cnt;
771
772         elems = NULL;
773         nelems = OF_getencprop_alloc(node, list_name,  sizeof(*elems),
774             (void **)&elems);
775         if (nelems <= 0)
776                 return (ENOENT);
777         rv = (idx == -1) ? 0 : ENOENT;
778         for (i = 0, cnt = 0; i < nelems; i += pcells, cnt++) {
779                 pnode = elems[i++];
780                 if (OF_getencprop(OF_node_from_xref(pnode),
781                     cells_name, &pcells, sizeof(pcells)) == -1) {
782                         printf("Missing %s property\n", cells_name);
783                         rv = ENOENT;
784                         break;
785                 }
786
787                 if ((i + pcells) > nelems) {
788                         printf("Invalid %s property value <%d>\n", cells_name,
789                             pcells);
790                         rv = ERANGE;
791                         break;
792                 }
793                 if (cnt == idx) {
794                         *cells= malloc(pcells * sizeof(**cells), M_OFWPROP,
795                             M_WAITOK);
796                         *producer = pnode;
797                         *ncells = pcells;
798                         for (j = 0; j < pcells; j++)
799                                 (*cells)[j] = elems[i + j];
800                         rv = 0;
801                         break;
802                 }
803         }
804         if (elems != NULL)
805                 free(elems, M_OFWPROP);
806         if (idx == -1 && rv == 0)
807                 *ncells = cnt;
808         return (rv);
809 }
810
811 /*
812  * Parse property that contain list of xrefs and values
813  * (like standard "clocks" and "resets" properties)
814  * Input arguments:
815  *  node - consumers device node
816  *  list_name  - name of parsed list - "clocks"
817  *  cells_name - name of size property - "#clock-cells"
818  *  idx - the index of the requested list entry (>= 0)
819  * Output arguments:
820  *  producer - handle of producer
821  *  ncells   - number of cells in result
822  *  cells    - array of decoded cells
823  */
824 int
825 ofw_bus_parse_xref_list_alloc(phandle_t node, const char *list_name,
826     const char *cells_name, int idx, phandle_t *producer, int *ncells,
827     pcell_t **cells)
828 {
829
830         KASSERT(idx >= 0,
831             ("ofw_bus_parse_xref_list_alloc: negative index supplied"));
832
833         return (ofw_bus_parse_xref_list_internal(node, list_name, cells_name,
834                     idx, producer, ncells, cells));
835 }
836
837 /*
838  * Parse property that contain list of xrefs and values
839  * (like standard "clocks" and "resets" properties)
840  * and determine the number of items in the list
841  * Input arguments:
842  *  node - consumers device node
843  *  list_name  - name of parsed list - "clocks"
844  *  cells_name - name of size property - "#clock-cells"
845  * Output arguments:
846  *  count - number of items in list
847  */
848 int
849 ofw_bus_parse_xref_list_get_length(phandle_t node, const char *list_name,
850     const char *cells_name, int *count)
851 {
852
853         return (ofw_bus_parse_xref_list_internal(node, list_name, cells_name,
854                     -1, NULL, count, NULL));
855 }
856
857 /*
858  * Find index of string in string list property (case sensitive).
859  */
860 int
861 ofw_bus_find_string_index(phandle_t node, const char *list_name,
862     const char *name, int *idx)
863 {
864         char *elems;
865         int rv, i, cnt, nelems;
866
867         elems = NULL;
868         nelems = OF_getprop_alloc(node, list_name, 1, (void **)&elems);
869         if (nelems <= 0)
870                 return (ENOENT);
871
872         rv = ENOENT;
873         for (i = 0, cnt = 0; i < nelems; cnt++) {
874                 if (strcmp(elems + i, name) == 0) {
875                         *idx = cnt;
876                         rv = 0;
877                         break;
878                 }
879                 i += strlen(elems + i) + 1;
880         }
881
882         if (elems != NULL)
883                 free(elems, M_OFWPROP);
884         return (rv);
885 }
886
887 /*
888  * Create zero terminated array of strings from string list property.
889  */
890 int
891 ofw_bus_string_list_to_array(phandle_t node, const char *list_name,
892    const char ***out_array)
893 {
894         char *elems, *tptr;
895         const char **array;
896         int i, cnt, nelems, len;
897
898         elems = NULL;
899         nelems = OF_getprop_alloc(node, list_name, 1, (void **)&elems);
900         if (nelems <= 0)
901                 return (nelems);
902
903         /* Count number of strings. */
904         for (i = 0, cnt = 0; i < nelems; cnt++)
905                 i += strlen(elems + i) + 1;
906
907         /* Allocate space for arrays and all strings. */
908         array = malloc((cnt + 1) * sizeof(char *) + nelems, M_OFWPROP,
909             M_WAITOK);
910
911         /* Get address of first string. */
912         tptr = (char *)(array + cnt + 1);
913
914         /* Copy strings. */
915         memcpy(tptr, elems, nelems);
916         free(elems, M_OFWPROP);
917
918         /* Fill string pointers. */
919         for (i = 0, cnt = 0; i < nelems; cnt++) {
920                 len = strlen(tptr) + 1;
921                 array[cnt] = tptr;
922                 i += len;
923                 tptr += len;
924         }
925         array[cnt] = 0;
926         *out_array = array;
927
928         return (cnt);
929 }