2 * Copyright (c) 2001 - 2003 by Thomas Moestl <tmm@FreeBSD.org>.
3 * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_platform.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/errno.h>
38 #include <sys/libkern.h>
40 #include <machine/resource.h>
42 #include <dev/ofw/ofw_bus.h>
43 #include <dev/ofw/ofw_bus_subr.h>
44 #include <dev/ofw/openfirm.h>
46 #include "ofw_bus_if.h"
49 ofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *obd, phandle_t node)
54 /* The 'name' property is considered mandatory. */
55 if ((OF_getprop_alloc(node, "name", 1, (void **)&obd->obd_name)) == -1)
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);
66 ofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *obd)
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);
84 ofw_bus_gen_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
88 if (ofw_bus_get_name(child) != NULL) {
89 strlcat(buf, "name=", buflen);
90 strlcat(buf, ofw_bus_get_name(child), buflen);
93 if (ofw_bus_get_compat(child) != NULL) {
94 strlcat(buf, " compat=", buflen);
95 strlcat(buf, ofw_bus_get_compat(child), buflen);
101 ofw_bus_gen_get_compat(device_t bus, device_t dev)
103 const struct ofw_bus_devinfo *obd;
105 obd = OFW_BUS_GET_DEVINFO(bus, dev);
108 return (obd->obd_compat);
112 ofw_bus_gen_get_model(device_t bus, device_t dev)
114 const struct ofw_bus_devinfo *obd;
116 obd = OFW_BUS_GET_DEVINFO(bus, dev);
119 return (obd->obd_model);
123 ofw_bus_gen_get_name(device_t bus, device_t dev)
125 const struct ofw_bus_devinfo *obd;
127 obd = OFW_BUS_GET_DEVINFO(bus, dev);
130 return (obd->obd_name);
134 ofw_bus_gen_get_node(device_t bus, device_t dev)
136 const struct ofw_bus_devinfo *obd;
138 obd = OFW_BUS_GET_DEVINFO(bus, dev);
141 return (obd->obd_node);
145 ofw_bus_gen_get_type(device_t bus, device_t dev)
147 const struct ofw_bus_devinfo *obd;
149 obd = OFW_BUS_GET_DEVINFO(bus, dev);
152 return (obd->obd_type);
156 ofw_bus_get_status(device_t dev)
158 const struct ofw_bus_devinfo *obd;
160 obd = OFW_BUS_GET_DEVINFO(device_get_parent(dev), dev);
164 return (obd->obd_status);
168 ofw_bus_status_okay(device_t dev)
172 status = ofw_bus_get_status(dev);
173 if (status == NULL || strcmp(status, "okay") == 0 ||
174 strcmp(status, "ok") == 0)
181 ofw_bus_node_is_compatible(const char *compat, int len, const char *onecompat)
185 onelen = strlen(onecompat);
189 if (strlen(compat) == onelen &&
190 strncasecmp(compat, onecompat, onelen) == 0) {
196 /* Slide to the next sub-string. */
197 l = strlen(compat) + 1;
206 ofw_bus_is_compatible(device_t dev, const char *onecompat)
212 if ((compat = ofw_bus_get_compat(dev)) == NULL)
215 if ((node = ofw_bus_get_node(dev)) == -1)
218 /* Get total 'compatible' prop len */
219 if ((len = OF_getproplen(node, "compatible")) <= 0)
222 return (ofw_bus_node_is_compatible(compat, len, onecompat));
226 ofw_bus_is_compatible_strict(device_t dev, const char *compatible)
231 if ((compat = ofw_bus_get_compat(dev)) == NULL)
234 len = strlen(compatible);
235 if (strlen(compat) == len &&
236 strncasecmp(compat, compatible, len) == 0)
242 const struct ofw_compat_data *
243 ofw_bus_search_compatible(device_t dev, const struct ofw_compat_data *compat)
249 for (; compat->ocd_str != NULL; ++compat) {
250 if (ofw_bus_is_compatible(dev, compat->ocd_str))
258 ofw_bus_has_prop(device_t dev, const char *propname)
262 if ((node = ofw_bus_get_node(dev)) == -1)
265 return (OF_hasprop(node, propname));
269 ofw_bus_setup_iinfo(phandle_t node, struct ofw_bus_iinfo *ii, int intrsz)
274 if (OF_getencprop(node, "#address-cells", &addrc, sizeof(addrc)) == -1)
276 ii->opi_addrc = addrc * sizeof(pcell_t);
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);
284 * Failure to get the mask is ignored; a full mask is used
285 * then. We barf on bad mask sizes, however.
287 if (msksz != -1 && msksz != ii->opi_addrc + intrsz)
288 panic("ofw_bus_setup_iinfo: bad interrupt-map-mask "
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,
298 uint8_t maskbuf[regsz + pintrsz];
301 if (ii->opi_imapsz <= 0)
303 KASSERT(regsz >= ii->opi_addrc,
304 ("ofw_bus_lookup_imap: register size too small: %d < %d",
305 regsz, ii->opi_addrc));
307 rv = OF_getencprop(node, "reg", reg, regsz);
309 panic("ofw_bus_lookup_imap: cannot get reg property");
311 return (ofw_bus_search_intrmap(pintr, pintrsz, reg, ii->opi_addrc,
312 ii->opi_imap, ii->opi_imapsz, ii->opi_imapmsk, maskbuf, mintr,
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
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.
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)
339 uint8_t *ref = maskbuf;
340 uint8_t *uiintr = intr;
341 uint8_t *uiregs = regs;
342 uint8_t *uiimapmsk = imapmsk;
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];
355 bcopy(regs, ref, physsz);
356 bcopy(intr, ref + physsz, intrsz);
363 bcopy(mptr + physsz + intrsz, &parent, sizeof(parent));
364 #ifndef OFW_IMAP_NO_IPARENT_ADDR_CELLS
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.
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);
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);
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"));
385 if (bcmp(ref, mptr, physsz + intrsz) == 0) {
386 bcopy(mptr + physsz + intrsz + sizeof(parent) + paddrsz,
387 result, MIN(rintrsz, pintrsz));
391 return (pintrsz/sizeof(pcell_t));
400 ofw_bus_msimap(phandle_t node, uint16_t pci_rid, phandle_t *msi_parent,
403 pcell_t *map, mask, msi_base, rid_base, rid_length;
405 uint32_t masked_rid, rid;
408 /* TODO: This should be OF_searchprop_alloc if we had it */
409 len = OF_getencprop_alloc(node, "msi-map", sizeof(*map), (void **)&map);
411 if (msi_parent != NULL) {
413 OF_getencprop(node, "msi-parent", msi_parent,
414 sizeof(*msi_parent));
424 OF_getencprop(node, "msi-map-mask", &mask, sizeof(mask));
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];
431 if (masked_rid < rid_base ||
432 masked_rid >= (rid_base + rid_length))
435 msi_base = map[i + 2];
437 if (msi_parent != NULL)
438 *msi_parent = map[i + 1];
440 *msi_rid = masked_rid - rid_base + msi_base;
445 free(map, M_OFWPROP);
451 ofw_bus_reg_to_rl(device_t dev, phandle_t node, pcell_t acells, pcell_t scells,
452 struct resource_list *rl)
455 ssize_t i, j, rid, nreg, ret;
460 * This may be just redundant when having ofw_bus_devinfo
461 * but makes this routine independent of it.
463 ret = OF_getprop_alloc(node, "name", sizeof(*name), (void **)&name);
467 ret = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)®);
468 nreg = (ret == -1) ? 0 : ret;
470 if (nreg % (acells + scells) != 0) {
472 device_printf(dev, "Malformed reg property on <%s>\n",
473 (name == NULL) ? "unknown" : name);
477 for (i = 0, rid = 0; i < nreg; i += acells + scells, rid++) {
479 for (j = 0; j < acells; j++) {
483 for (j = 0; j < scells; j++) {
485 size |= reg[i + acells + j];
487 /* Skip the dummy reg property of glue devices like ssm(4). */
489 resource_list_add(rl, SYS_RES_MEMORY, rid,
490 phys, phys + size - 1, size);
492 free(name, M_OFWPROP);
493 free(reg, M_OFWPROP);
499 * Get interrupt parent for given node.
500 * Returns 0 if interrupt parent doesn't exist.
503 ofw_bus_find_iparent(phandle_t node)
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"))
514 iparent = OF_xref_from_node(iparent);
521 ofw_bus_intr_to_rl(device_t dev, phandle_t node,
522 struct resource_list *rl, int *rlen)
525 uint32_t icells, *intr;
526 int err, i, irqnum, nintr, rid;
529 nintr = OF_getencprop_alloc(node, "interrupts", sizeof(*intr),
532 iparent = ofw_bus_find_iparent(node);
534 device_printf(dev, "No interrupt-parent found, "
535 "assuming direct parent\n");
536 iparent = OF_parent(node);
537 iparent = OF_xref_from_node(iparent);
539 if (OF_searchencprop(OF_node_from_xref(iparent),
540 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
541 device_printf(dev, "Missing #interrupt-cells "
542 "property, assuming <1>\n");
545 if (icells < 1 || icells > nintr) {
546 device_printf(dev, "Invalid #interrupt-cells property "
547 "value <%d>, assuming <1>\n", icells);
552 nintr = OF_getencprop_alloc(node, "interrupts-extended",
553 sizeof(*intr), (void **)&intr);
560 for (i = 0; i < nintr; i += icells) {
563 if (OF_searchencprop(OF_node_from_xref(iparent),
564 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
565 device_printf(dev, "Missing #interrupt-cells "
570 if (icells < 1 || (i + icells) > nintr) {
571 device_printf(dev, "Invalid #interrupt-cells "
572 "property value <%d>\n", icells);
577 irqnum = ofw_bus_map_intr(dev, iparent, icells, &intr[i]);
578 resource_list_add(rl, SYS_RES_IRQ, rid++, irqnum, irqnum, 1);
582 free(intr, M_OFWPROP);
588 ofw_bus_intr_by_rid(device_t dev, phandle_t node, int wanted_rid,
589 phandle_t *producer, int *ncells, pcell_t **cells)
592 uint32_t icells, *intr;
593 int err, i, nintr, rid;
596 nintr = OF_getencprop_alloc(node, "interrupts", sizeof(*intr),
599 iparent = ofw_bus_find_iparent(node);
601 device_printf(dev, "No interrupt-parent found, "
602 "assuming direct parent\n");
603 iparent = OF_parent(node);
604 iparent = OF_xref_from_node(iparent);
606 if (OF_searchencprop(OF_node_from_xref(iparent),
607 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
608 device_printf(dev, "Missing #interrupt-cells "
609 "property, assuming <1>\n");
612 if (icells < 1 || icells > nintr) {
613 device_printf(dev, "Invalid #interrupt-cells property "
614 "value <%d>, assuming <1>\n", icells);
619 nintr = OF_getencprop_alloc(node, "interrupts-extended",
620 sizeof(*intr), (void **)&intr);
627 for (i = 0; i < nintr; i += icells, rid++) {
630 if (OF_searchencprop(OF_node_from_xref(iparent),
631 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
632 device_printf(dev, "Missing #interrupt-cells "
637 if (icells < 1 || (i + icells) > nintr) {
638 device_printf(dev, "Invalid #interrupt-cells "
639 "property value <%d>\n", icells);
644 if (rid == wanted_rid) {
645 *cells = malloc(icells * sizeof(**cells), M_OFWPROP,
649 memcpy(*cells, intr + i, icells * sizeof(**cells));
654 free(intr, M_OFWPROP);
659 ofw_bus_find_child(phandle_t start, const char *child_name)
665 for (child = OF_child(start); child != 0; child = OF_peer(child)) {
666 ret = OF_getprop_alloc(child, "name", sizeof(*name), (void **)&name);
669 if (strcmp(name, child_name) == 0) {
670 free(name, M_OFWPROP);
674 free(name, M_OFWPROP);
681 ofw_bus_find_compatible(phandle_t node, const char *onecompat)
683 phandle_t child, ret;
688 * Traverse all children of 'start' node, and find first with
689 * matching 'compatible' property.
691 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
692 len = OF_getprop_alloc(child, "compatible", 1, &compat);
694 ret = ofw_bus_node_is_compatible(compat, len,
696 free(compat, M_OFWPROP);
701 ret = ofw_bus_find_compatible(child, onecompat);
709 * @brief Return child of bus whose phandle is node
711 * A direct child of @p will be returned if it its phandle in the
712 * OFW tree is @p node. Otherwise, NULL is returned.
714 * @param bus The bus to examine
715 * @param node The phandle_t to look for.
718 ofw_bus_find_child_device_by_phandle(device_t bus, phandle_t node)
720 device_t *children, retval, child;
724 * Nothing can match the flag value for no node.
730 * Search the children for a match. We microoptimize
731 * a bit by not using ofw_bus_get since we already know
732 * the parent. We do not recurse.
734 if (device_get_children(bus, &children, &nkid) != 0)
737 for (i = 0; i < nkid; i++) {
739 if (OFW_BUS_GET_NODE(bus, child) == node) {
744 free(children, M_TEMP);
750 * Parse property that contain list of xrefs and values
751 * (like standard "clocks" and "resets" properties)
753 * node - consumers device node
754 * list_name - name of parsed list - "clocks"
755 * cells_name - name of size property - "#clock-cells"
756 * idx - the index of the requested list entry, or, if -1, an indication
757 * to return the number of entries in the parsed list.
759 * producer - handle of producer
760 * ncells - number of cells in result or the number of items in the list when
762 * cells - array of decoded cells
765 ofw_bus_parse_xref_list_internal(phandle_t node, const char *list_name,
766 const char *cells_name, int idx, phandle_t *producer, int *ncells,
772 int rv, i, j, nelems, cnt;
775 nelems = OF_getencprop_alloc(node, list_name, sizeof(*elems),
779 rv = (idx == -1) ? 0 : ENOENT;
780 for (i = 0, cnt = 0; i < nelems; i += pcells, cnt++) {
782 if (OF_getencprop(OF_node_from_xref(pnode),
783 cells_name, &pcells, sizeof(pcells)) == -1) {
784 printf("Missing %s property\n", cells_name);
789 if ((i + pcells) > nelems) {
790 printf("Invalid %s property value <%d>\n", cells_name,
796 *cells= malloc(pcells * sizeof(**cells), M_OFWPROP,
800 for (j = 0; j < pcells; j++)
801 (*cells)[j] = elems[i + j];
807 free(elems, M_OFWPROP);
808 if (idx == -1 && rv == 0)
814 * Parse property that contain list of xrefs and values
815 * (like standard "clocks" and "resets" properties)
817 * node - consumers device node
818 * list_name - name of parsed list - "clocks"
819 * cells_name - name of size property - "#clock-cells"
820 * idx - the index of the requested list entry (>= 0)
822 * producer - handle of producer
823 * ncells - number of cells in result
824 * cells - array of decoded cells
827 ofw_bus_parse_xref_list_alloc(phandle_t node, const char *list_name,
828 const char *cells_name, int idx, phandle_t *producer, int *ncells,
833 ("ofw_bus_parse_xref_list_alloc: negative index supplied"));
835 return (ofw_bus_parse_xref_list_internal(node, list_name, cells_name,
836 idx, producer, ncells, cells));
840 * Parse property that contain list of xrefs and values
841 * (like standard "clocks" and "resets" properties)
842 * and determine the number of items in the list
844 * node - consumers device node
845 * list_name - name of parsed list - "clocks"
846 * cells_name - name of size property - "#clock-cells"
848 * count - number of items in list
851 ofw_bus_parse_xref_list_get_length(phandle_t node, const char *list_name,
852 const char *cells_name, int *count)
855 return (ofw_bus_parse_xref_list_internal(node, list_name, cells_name,
856 -1, NULL, count, NULL));
860 * Find index of string in string list property (case sensitive).
863 ofw_bus_find_string_index(phandle_t node, const char *list_name,
864 const char *name, int *idx)
867 int rv, i, cnt, nelems;
870 nelems = OF_getprop_alloc(node, list_name, 1, (void **)&elems);
875 for (i = 0, cnt = 0; i < nelems; cnt++) {
876 if (strcmp(elems + i, name) == 0) {
881 i += strlen(elems + i) + 1;
885 free(elems, M_OFWPROP);
890 * Create zero terminated array of strings from string list property.
893 ofw_bus_string_list_to_array(phandle_t node, const char *list_name,
894 const char ***out_array)
898 int i, cnt, nelems, len;
901 nelems = OF_getprop_alloc(node, list_name, 1, (void **)&elems);
905 /* Count number of strings. */
906 for (i = 0, cnt = 0; i < nelems; cnt++)
907 i += strlen(elems + i) + 1;
909 /* Allocate space for arrays and all strings. */
910 array = malloc((cnt + 1) * sizeof(char *) + nelems, M_OFWPROP,
913 /* Get address of first string. */
914 tptr = (char *)(array + cnt + 1);
917 memcpy(tptr, elems, nelems);
918 free(elems, M_OFWPROP);
920 /* Fill string pointers. */
921 for (i = 0, cnt = 0; i < nelems; cnt++) {
922 len = strlen(tptr) + 1;