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