2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2001 - 2003 by Thomas Moestl <tmm@FreeBSD.org>.
5 * Copyright (c) 2005 Marius Strobl <marius@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_platform.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/errno.h>
40 #include <sys/libkern.h>
43 #include <machine/resource.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/ofw/openfirm.h>
49 #include "ofw_bus_if.h"
51 #define OFW_COMPAT_LEN 255
52 #define OFW_STATUS_LEN 16
55 ofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *obd, phandle_t node)
60 /* The 'name' property is considered mandatory. */
61 if ((OF_getprop_alloc(node, "name", (void **)&obd->obd_name)) == -1)
63 OF_getprop_alloc(node, "compatible", (void **)&obd->obd_compat);
64 OF_getprop_alloc(node, "device_type", (void **)&obd->obd_type);
65 OF_getprop_alloc(node, "model", (void **)&obd->obd_model);
66 OF_getprop_alloc(node, "status", (void **)&obd->obd_status);
72 ofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *obd)
77 if (obd->obd_compat != NULL)
78 free(obd->obd_compat, M_OFWPROP);
79 if (obd->obd_model != NULL)
80 free(obd->obd_model, M_OFWPROP);
81 if (obd->obd_name != NULL)
82 free(obd->obd_name, M_OFWPROP);
83 if (obd->obd_type != NULL)
84 free(obd->obd_type, M_OFWPROP);
85 if (obd->obd_status != NULL)
86 free(obd->obd_status, M_OFWPROP);
90 ofw_bus_gen_child_pnpinfo(device_t cbdev, device_t child, struct sbuf *sb)
93 if (!ofw_bus_status_okay(child))
96 if (ofw_bus_get_name(child) != NULL) {
97 sbuf_printf(sb, "name=%s ", ofw_bus_get_name(child));
100 if (ofw_bus_get_compat(child) != NULL) {
101 sbuf_printf(sb, "compat=%s ", ofw_bus_get_compat(child));
108 ofw_bus_gen_get_compat(device_t bus, device_t dev)
110 const struct ofw_bus_devinfo *obd;
112 obd = OFW_BUS_GET_DEVINFO(bus, dev);
115 return (obd->obd_compat);
119 ofw_bus_gen_get_model(device_t bus, device_t dev)
121 const struct ofw_bus_devinfo *obd;
123 obd = OFW_BUS_GET_DEVINFO(bus, dev);
126 return (obd->obd_model);
130 ofw_bus_gen_get_name(device_t bus, device_t dev)
132 const struct ofw_bus_devinfo *obd;
134 obd = OFW_BUS_GET_DEVINFO(bus, dev);
137 return (obd->obd_name);
141 ofw_bus_gen_get_node(device_t bus, device_t dev)
143 const struct ofw_bus_devinfo *obd;
145 obd = OFW_BUS_GET_DEVINFO(bus, dev);
147 return ((phandle_t)-1);
148 return (obd->obd_node);
152 ofw_bus_gen_get_type(device_t bus, device_t dev)
154 const struct ofw_bus_devinfo *obd;
156 obd = OFW_BUS_GET_DEVINFO(bus, dev);
159 return (obd->obd_type);
163 ofw_bus_get_status(device_t dev)
165 const struct ofw_bus_devinfo *obd;
167 obd = OFW_BUS_GET_DEVINFO(device_get_parent(dev), dev);
171 return (obd->obd_status);
175 ofw_bus_status_okay(device_t dev)
179 status = ofw_bus_get_status(dev);
180 if (status == NULL || strcmp(status, "okay") == 0 ||
181 strcmp(status, "ok") == 0)
188 ofw_bus_node_status_okay(phandle_t node)
190 char status[OFW_STATUS_LEN];
193 len = OF_getproplen(node, "status");
197 OF_getprop(node, "status", status, OFW_STATUS_LEN);
198 if ((len == 5 && (bcmp(status, "okay", len) == 0)) ||
199 (len == 3 && (bcmp(status, "ok", len))))
206 ofw_bus_node_is_compatible_int(const char *compat, int len,
207 const char *onecompat)
211 onelen = strlen(onecompat);
215 if (strlen(compat) == onelen &&
216 strncasecmp(compat, onecompat, onelen) == 0) {
222 /* Slide to the next sub-string. */
223 l = strlen(compat) + 1;
232 ofw_bus_node_is_compatible(phandle_t node, const char *compatstr)
234 char compat[OFW_COMPAT_LEN];
237 if ((len = OF_getproplen(node, "compatible")) <= 0)
240 bzero(compat, OFW_COMPAT_LEN);
242 if (OF_getprop(node, "compatible", compat, OFW_COMPAT_LEN) < 0)
245 rv = ofw_bus_node_is_compatible_int(compat, len, compatstr);
251 ofw_bus_is_compatible(device_t dev, const char *onecompat)
257 if ((compat = ofw_bus_get_compat(dev)) == NULL)
260 if ((node = ofw_bus_get_node(dev)) == -1)
263 /* Get total 'compatible' prop len */
264 if ((len = OF_getproplen(node, "compatible")) <= 0)
267 return (ofw_bus_node_is_compatible_int(compat, len, onecompat));
271 ofw_bus_is_compatible_strict(device_t dev, const char *compatible)
276 if ((compat = ofw_bus_get_compat(dev)) == NULL)
279 len = strlen(compatible);
280 if (strlen(compat) == len &&
281 strncasecmp(compat, compatible, len) == 0)
287 const struct ofw_compat_data *
288 ofw_bus_search_compatible(device_t dev, const struct ofw_compat_data *compat)
294 for (; compat->ocd_str != NULL; ++compat) {
295 if (ofw_bus_is_compatible(dev, compat->ocd_str))
303 ofw_bus_has_prop(device_t dev, const char *propname)
307 if ((node = ofw_bus_get_node(dev)) == -1)
310 return (OF_hasprop(node, propname));
314 ofw_bus_setup_iinfo(phandle_t node, struct ofw_bus_iinfo *ii, int intrsz)
319 if (OF_getencprop(node, "#address-cells", &addrc, sizeof(addrc)) == -1)
321 ii->opi_addrc = addrc * sizeof(pcell_t);
323 ii->opi_imapsz = OF_getencprop_alloc(node, "interrupt-map",
324 (void **)&ii->opi_imap);
325 if (ii->opi_imapsz > 0) {
326 msksz = OF_getencprop_alloc(node, "interrupt-map-mask",
327 (void **)&ii->opi_imapmsk);
329 * Failure to get the mask is ignored; a full mask is used
330 * then. We barf on bad mask sizes, however.
332 if (msksz != -1 && msksz != ii->opi_addrc + intrsz)
333 panic("ofw_bus_setup_iinfo: bad interrupt-map-mask "
339 ofw_bus_lookup_imap(phandle_t node, struct ofw_bus_iinfo *ii, void *reg,
340 int regsz, void *pintr, int pintrsz, void *mintr, int mintrsz,
343 uint8_t maskbuf[regsz + pintrsz];
346 if (ii->opi_imapsz <= 0)
348 KASSERT(regsz >= ii->opi_addrc,
349 ("ofw_bus_lookup_imap: register size too small: %d < %d",
350 regsz, ii->opi_addrc));
352 rv = OF_getencprop(node, "reg", reg, regsz);
354 panic("ofw_bus_lookup_imap: cannot get reg property");
356 return (ofw_bus_search_intrmap(pintr, pintrsz, reg, ii->opi_addrc,
357 ii->opi_imap, ii->opi_imapsz, ii->opi_imapmsk, maskbuf, mintr,
362 * Map an interrupt using the firmware reg, interrupt-map and
363 * interrupt-map-mask properties.
364 * The interrupt property to be mapped must be of size intrsz, and pointed to
365 * by intr. The regs property of the node for which the mapping is done must
366 * be passed as regs. This property is an array of register specifications;
367 * the size of the address part of such a specification must be passed as
368 * physsz. Only the first element of the property is used.
369 * imap and imapsz hold the interrupt mask and it's size.
370 * imapmsk is a pointer to the interrupt-map-mask property, which must have
371 * a size of physsz + intrsz; it may be NULL, in which case a full mask is
373 * maskbuf must point to a buffer of length physsz + intrsz.
374 * The interrupt is returned in result, which must point to a buffer of length
375 * rintrsz (which gives the expected size of the mapped interrupt).
376 * Returns number of cells in the interrupt if a mapping was found, 0 otherwise.
379 ofw_bus_search_intrmap(void *intr, int intrsz, void *regs, int physsz,
380 void *imap, int imapsz, void *imapmsk, void *maskbuf, void *result,
381 int rintrsz, phandle_t *iparent)
384 uint8_t *ref = maskbuf;
385 uint8_t *uiintr = intr;
386 uint8_t *uiregs = regs;
387 uint8_t *uiimapmsk = imapmsk;
393 if (imapmsk != NULL) {
394 for (i = 0; i < physsz; i++)
395 ref[i] = uiregs[i] & uiimapmsk[i];
396 for (i = 0; i < intrsz; i++)
397 ref[physsz + i] = uiintr[i] & uiimapmsk[physsz + i];
399 bcopy(regs, ref, physsz);
400 bcopy(intr, ref + physsz, intrsz);
407 bcopy(mptr + physsz + intrsz, &parent, sizeof(parent));
408 #ifndef OFW_IMAP_NO_IPARENT_ADDR_CELLS
410 * Find if we need to read the parent address data.
411 * CHRP-derived OF bindings, including ePAPR-compliant FDTs,
412 * use this as an optional part of the specifier.
414 if (OF_getencprop(OF_node_from_xref(parent),
415 "#address-cells", &paddrsz, sizeof(paddrsz)) == -1)
416 paddrsz = 0; /* default */
417 paddrsz *= sizeof(pcell_t);
420 if (OF_searchencprop(OF_node_from_xref(parent),
421 "#interrupt-cells", &pintrsz, sizeof(pintrsz)) == -1)
422 pintrsz = 1; /* default */
423 pintrsz *= sizeof(pcell_t);
425 /* Compute the map stride size. */
426 tsz = physsz + intrsz + sizeof(phandle_t) + paddrsz + pintrsz;
427 KASSERT(i >= tsz, ("ofw_bus_search_intrmap: truncated map"));
429 if (bcmp(ref, mptr, physsz + intrsz) == 0) {
430 bcopy(mptr + physsz + intrsz + sizeof(parent) + paddrsz,
431 result, MIN(rintrsz, pintrsz));
435 return (pintrsz/sizeof(pcell_t));
444 ofw_bus_msimap(phandle_t node, uint16_t pci_rid, phandle_t *msi_parent,
447 pcell_t *map, mask, msi_base, rid_base, rid_length;
452 /* TODO: This should be OF_searchprop_alloc if we had it */
453 len = OF_getencprop_alloc_multi(node, "msi-map", sizeof(*map),
456 if (msi_parent != NULL) {
458 OF_getencprop(node, "msi-parent", msi_parent,
459 sizeof(*msi_parent));
468 OF_getencprop(node, "msi-map-mask", &mask, sizeof(mask));
470 masked_rid = pci_rid & mask;
471 for (i = 0; i < len; i += 4) {
472 rid_base = map[i + 0];
473 rid_length = map[i + 3];
475 if (masked_rid < rid_base ||
476 masked_rid >= (rid_base + rid_length))
479 msi_base = map[i + 2];
481 if (msi_parent != NULL)
482 *msi_parent = map[i + 1];
484 *msi_rid = masked_rid - rid_base + msi_base;
489 free(map, M_OFWPROP);
495 ofw_bus_reg_to_rl_helper(device_t dev, phandle_t node, pcell_t acells, pcell_t scells,
496 struct resource_list *rl, const char *reg_source)
499 ssize_t i, j, rid, nreg, ret;
504 * This may be just redundant when having ofw_bus_devinfo
505 * but makes this routine independent of it.
507 ret = OF_getprop_alloc(node, "name", (void **)&name);
511 ret = OF_getencprop_alloc_multi(node, reg_source, sizeof(*reg),
513 nreg = (ret == -1) ? 0 : ret;
515 if (nreg % (acells + scells) != 0) {
517 device_printf(dev, "Malformed reg property on <%s>\n",
518 (name == NULL) ? "unknown" : name);
522 for (i = 0, rid = 0; i < nreg; i += acells + scells, rid++) {
524 for (j = 0; j < acells; j++) {
528 for (j = 0; j < scells; j++) {
530 size |= reg[i + acells + j];
532 /* Skip the dummy reg property of glue devices like ssm(4). */
534 resource_list_add(rl, SYS_RES_MEMORY, rid,
535 phys, phys + size - 1, size);
537 free(name, M_OFWPROP);
538 free(reg, M_OFWPROP);
544 ofw_bus_reg_to_rl(device_t dev, phandle_t node, pcell_t acells, pcell_t scells,
545 struct resource_list *rl)
548 return (ofw_bus_reg_to_rl_helper(dev, node, acells, scells, rl, "reg"));
552 ofw_bus_assigned_addresses_to_rl(device_t dev, phandle_t node, pcell_t acells,
553 pcell_t scells, struct resource_list *rl)
556 return (ofw_bus_reg_to_rl_helper(dev, node, acells, scells,
557 rl, "assigned-addresses"));
561 * Get interrupt parent for given node.
562 * Returns 0 if interrupt parent doesn't exist.
565 ofw_bus_find_iparent(phandle_t node)
569 if (OF_searchencprop(node, "interrupt-parent", &iparent,
570 sizeof(iparent)) == -1) {
571 for (iparent = node; iparent != 0;
572 iparent = OF_parent(iparent)) {
573 if (OF_hasprop(iparent, "interrupt-controller"))
576 iparent = OF_xref_from_node(iparent);
582 ofw_bus_intr_to_rl(device_t dev, phandle_t node,
583 struct resource_list *rl, int *rlen)
586 uint32_t icells, *intr;
587 int err, i, irqnum, nintr, rid;
590 nintr = OF_getencprop_alloc_multi(node, "interrupts", sizeof(*intr),
593 iparent = ofw_bus_find_iparent(node);
595 device_printf(dev, "No interrupt-parent found, "
596 "assuming direct parent\n");
597 iparent = OF_parent(node);
598 iparent = OF_xref_from_node(iparent);
600 if (OF_searchencprop(OF_node_from_xref(iparent),
601 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
602 device_printf(dev, "Missing #interrupt-cells "
603 "property, assuming <1>\n");
606 if (icells < 1 || icells > nintr) {
607 device_printf(dev, "Invalid #interrupt-cells property "
608 "value <%d>, assuming <1>\n", icells);
613 nintr = OF_getencprop_alloc_multi(node, "interrupts-extended",
614 sizeof(*intr), (void **)&intr);
621 for (i = 0; i < nintr; i += icells) {
624 if (OF_searchencprop(OF_node_from_xref(iparent),
625 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
626 device_printf(dev, "Missing #interrupt-cells "
631 if (icells < 1 || (i + icells) > nintr) {
632 device_printf(dev, "Invalid #interrupt-cells "
633 "property value <%d>\n", icells);
638 irqnum = ofw_bus_map_intr(dev, iparent, icells, &intr[i]);
639 resource_list_add(rl, SYS_RES_IRQ, rid++, irqnum, irqnum, 1);
643 free(intr, M_OFWPROP);
648 ofw_bus_intr_by_rid(device_t dev, phandle_t node, int wanted_rid,
649 phandle_t *producer, int *ncells, pcell_t **cells)
652 uint32_t icells, *intr;
653 int err, i, nintr, rid;
656 nintr = OF_getencprop_alloc_multi(node, "interrupts", sizeof(*intr),
659 iparent = ofw_bus_find_iparent(node);
661 device_printf(dev, "No interrupt-parent found, "
662 "assuming direct parent\n");
663 iparent = OF_parent(node);
664 iparent = OF_xref_from_node(iparent);
666 if (OF_searchencprop(OF_node_from_xref(iparent),
667 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
668 device_printf(dev, "Missing #interrupt-cells "
669 "property, assuming <1>\n");
672 if (icells < 1 || icells > nintr) {
673 device_printf(dev, "Invalid #interrupt-cells property "
674 "value <%d>, assuming <1>\n", icells);
679 nintr = OF_getencprop_alloc_multi(node, "interrupts-extended",
680 sizeof(*intr), (void **)&intr);
687 for (i = 0; i < nintr; i += icells, rid++) {
690 if (OF_searchencprop(OF_node_from_xref(iparent),
691 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
692 device_printf(dev, "Missing #interrupt-cells "
697 if (icells < 1 || (i + icells) > nintr) {
698 device_printf(dev, "Invalid #interrupt-cells "
699 "property value <%d>\n", icells);
704 if (rid == wanted_rid) {
705 *cells = malloc(icells * sizeof(**cells), M_OFWPROP,
709 memcpy(*cells, intr + i, icells * sizeof(**cells));
714 free(intr, M_OFWPROP);
719 ofw_bus_find_child(phandle_t start, const char *child_name)
725 for (child = OF_child(start); child != 0; child = OF_peer(child)) {
726 ret = OF_getprop_alloc(child, "name", (void **)&name);
729 if (strcmp(name, child_name) == 0) {
730 free(name, M_OFWPROP);
734 free(name, M_OFWPROP);
741 ofw_bus_find_compatible(phandle_t node, const char *onecompat)
743 phandle_t child, ret;
746 * Traverse all children of 'start' node, and find first with
747 * matching 'compatible' property.
749 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
750 if (ofw_bus_node_is_compatible(child, onecompat) != 0)
753 ret = ofw_bus_find_compatible(child, onecompat);
761 * @brief Return child of bus whose phandle is node
763 * A direct child of @p will be returned if it its phandle in the
764 * OFW tree is @p node. Otherwise, NULL is returned.
766 * @param bus The bus to examine
767 * @param node The phandle_t to look for.
770 ofw_bus_find_child_device_by_phandle(device_t bus, phandle_t node)
772 device_t *children, retval, child;
776 * Nothing can match the flag value for no node.
782 * Search the children for a match. We microoptimize
783 * a bit by not using ofw_bus_get since we already know
784 * the parent. We do not recurse.
786 if (device_get_children(bus, &children, &nkid) != 0)
789 for (i = 0; i < nkid; i++) {
791 if (OFW_BUS_GET_NODE(bus, child) == node) {
796 free(children, M_TEMP);
802 * Parse property that contain list of xrefs and values
803 * (like standard "clocks" and "resets" properties)
805 * node - consumers device node
806 * list_name - name of parsed list - "clocks"
807 * cells_name - name of size property - "#clock-cells"
808 * idx - the index of the requested list entry, or, if -1, an indication
809 * to return the number of entries in the parsed list.
811 * producer - handle of producer
812 * ncells - number of cells in result or the number of items in the list when
814 * cells - array of decoded cells
817 ofw_bus_parse_xref_list_internal(phandle_t node, const char *list_name,
818 const char *cells_name, int idx, phandle_t *producer, int *ncells,
824 int rv, i, j, nelems, cnt;
827 nelems = OF_getencprop_alloc_multi(node, list_name, sizeof(*elems),
831 rv = (idx == -1) ? 0 : ENOENT;
832 for (i = 0, cnt = 0; i < nelems; i += pcells, cnt++) {
834 if (OF_getencprop(OF_node_from_xref(pnode),
835 cells_name, &pcells, sizeof(pcells)) == -1) {
836 printf("Missing %s property\n", cells_name);
841 if ((i + pcells) > nelems) {
842 printf("Invalid %s property value <%d>\n", cells_name,
848 *cells= malloc(pcells * sizeof(**cells), M_OFWPROP,
852 for (j = 0; j < pcells; j++)
853 (*cells)[j] = elems[i + j];
859 free(elems, M_OFWPROP);
860 if (idx == -1 && rv == 0)
866 * Parse property that contain list of xrefs and values
867 * (like standard "clocks" and "resets" properties)
869 * node - consumers device node
870 * list_name - name of parsed list - "clocks"
871 * cells_name - name of size property - "#clock-cells"
872 * idx - the index of the requested list entry (>= 0)
874 * producer - handle of producer
875 * ncells - number of cells in result
876 * cells - array of decoded cells
879 ofw_bus_parse_xref_list_alloc(phandle_t node, const char *list_name,
880 const char *cells_name, int idx, phandle_t *producer, int *ncells,
885 ("ofw_bus_parse_xref_list_alloc: negative index supplied"));
887 return (ofw_bus_parse_xref_list_internal(node, list_name, cells_name,
888 idx, producer, ncells, cells));
892 * Parse property that contain list of xrefs and values
893 * (like standard "clocks" and "resets" properties)
894 * and determine the number of items in the list
896 * node - consumers device node
897 * list_name - name of parsed list - "clocks"
898 * cells_name - name of size property - "#clock-cells"
900 * count - number of items in list
903 ofw_bus_parse_xref_list_get_length(phandle_t node, const char *list_name,
904 const char *cells_name, int *count)
907 return (ofw_bus_parse_xref_list_internal(node, list_name, cells_name,
908 -1, NULL, count, NULL));
912 * Find index of string in string list property (case sensitive).
915 ofw_bus_find_string_index(phandle_t node, const char *list_name,
916 const char *name, int *idx)
919 int rv, i, cnt, nelems;
922 nelems = OF_getprop_alloc(node, list_name, (void **)&elems);
927 for (i = 0, cnt = 0; i < nelems; cnt++) {
928 if (strcmp(elems + i, name) == 0) {
933 i += strlen(elems + i) + 1;
937 free(elems, M_OFWPROP);
942 * Create zero terminated array of strings from string list property.
945 ofw_bus_string_list_to_array(phandle_t node, const char *list_name,
946 const char ***out_array)
950 int i, cnt, nelems, len;
953 nelems = OF_getprop_alloc(node, list_name, (void **)&elems);
957 /* Count number of strings. */
958 for (i = 0, cnt = 0; i < nelems; cnt++)
959 i += strlen(elems + i) + 1;
961 /* Allocate space for arrays and all strings. */
962 array = malloc((cnt + 1) * sizeof(char *) + nelems, M_OFWPROP,
965 /* Get address of first string. */
966 tptr = (char *)(array + cnt + 1);
969 memcpy(tptr, elems, nelems);
970 free(elems, M_OFWPROP);
972 /* Fill string pointers. */
973 for (i = 0, cnt = 0; i < nelems; cnt++) {
974 len = strlen(tptr) + 1;