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>
42 #include <machine/resource.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/ofw/openfirm.h>
48 #include "ofw_bus_if.h"
50 #define OFW_COMPAT_LEN 255
51 #define OFW_STATUS_LEN 16
54 ofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *obd, phandle_t node)
59 /* The 'name' property is considered mandatory. */
60 if ((OF_getprop_alloc(node, "name", 1, (void **)&obd->obd_name)) == -1)
62 OF_getprop_alloc(node, "compatible", 1, (void **)&obd->obd_compat);
63 OF_getprop_alloc(node, "device_type", 1, (void **)&obd->obd_type);
64 OF_getprop_alloc(node, "model", 1, (void **)&obd->obd_model);
65 OF_getprop_alloc(node, "status", 1, (void **)&obd->obd_status);
71 ofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *obd)
76 if (obd->obd_compat != NULL)
77 free(obd->obd_compat, M_OFWPROP);
78 if (obd->obd_model != NULL)
79 free(obd->obd_model, M_OFWPROP);
80 if (obd->obd_name != NULL)
81 free(obd->obd_name, M_OFWPROP);
82 if (obd->obd_type != NULL)
83 free(obd->obd_type, M_OFWPROP);
84 if (obd->obd_status != NULL)
85 free(obd->obd_status, M_OFWPROP);
89 ofw_bus_gen_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
93 if (ofw_bus_get_name(child) != NULL) {
94 strlcat(buf, "name=", buflen);
95 strlcat(buf, ofw_bus_get_name(child), buflen);
98 if (ofw_bus_get_compat(child) != NULL) {
99 strlcat(buf, " compat=", buflen);
100 strlcat(buf, ofw_bus_get_compat(child), buflen);
106 ofw_bus_gen_get_compat(device_t bus, device_t dev)
108 const struct ofw_bus_devinfo *obd;
110 obd = OFW_BUS_GET_DEVINFO(bus, dev);
113 return (obd->obd_compat);
117 ofw_bus_gen_get_model(device_t bus, device_t dev)
119 const struct ofw_bus_devinfo *obd;
121 obd = OFW_BUS_GET_DEVINFO(bus, dev);
124 return (obd->obd_model);
128 ofw_bus_gen_get_name(device_t bus, device_t dev)
130 const struct ofw_bus_devinfo *obd;
132 obd = OFW_BUS_GET_DEVINFO(bus, dev);
135 return (obd->obd_name);
139 ofw_bus_gen_get_node(device_t bus, device_t dev)
141 const struct ofw_bus_devinfo *obd;
143 obd = OFW_BUS_GET_DEVINFO(bus, dev);
146 return (obd->obd_node);
150 ofw_bus_gen_get_type(device_t bus, device_t dev)
152 const struct ofw_bus_devinfo *obd;
154 obd = OFW_BUS_GET_DEVINFO(bus, dev);
157 return (obd->obd_type);
161 ofw_bus_get_status(device_t dev)
163 const struct ofw_bus_devinfo *obd;
165 obd = OFW_BUS_GET_DEVINFO(device_get_parent(dev), dev);
169 return (obd->obd_status);
173 ofw_bus_status_okay(device_t dev)
177 status = ofw_bus_get_status(dev);
178 if (status == NULL || strcmp(status, "okay") == 0 ||
179 strcmp(status, "ok") == 0)
186 ofw_bus_node_status_okay(phandle_t node)
188 char status[OFW_STATUS_LEN];
191 len = OF_getproplen(node, "status");
195 OF_getprop(node, "status", status, OFW_STATUS_LEN);
196 if ((len == 5 && (bcmp(status, "okay", len) == 0)) ||
197 (len == 3 && (bcmp(status, "ok", len))))
204 ofw_bus_node_is_compatible_int(const char *compat, int len,
205 const char *onecompat)
209 onelen = strlen(onecompat);
213 if (strlen(compat) == onelen &&
214 strncasecmp(compat, onecompat, onelen) == 0) {
220 /* Slide to the next sub-string. */
221 l = strlen(compat) + 1;
230 ofw_bus_node_is_compatible(phandle_t node, const char *compatstr)
232 char compat[OFW_COMPAT_LEN];
235 if ((len = OF_getproplen(node, "compatible")) <= 0)
238 bzero(compat, OFW_COMPAT_LEN);
240 if (OF_getprop(node, "compatible", compat, OFW_COMPAT_LEN) < 0)
243 rv = ofw_bus_node_is_compatible_int(compat, len, compatstr);
249 ofw_bus_is_compatible(device_t dev, const char *onecompat)
255 if ((compat = ofw_bus_get_compat(dev)) == NULL)
258 if ((node = ofw_bus_get_node(dev)) == -1)
261 /* Get total 'compatible' prop len */
262 if ((len = OF_getproplen(node, "compatible")) <= 0)
265 return (ofw_bus_node_is_compatible_int(compat, len, onecompat));
269 ofw_bus_is_compatible_strict(device_t dev, const char *compatible)
274 if ((compat = ofw_bus_get_compat(dev)) == NULL)
277 len = strlen(compatible);
278 if (strlen(compat) == len &&
279 strncasecmp(compat, compatible, len) == 0)
285 const struct ofw_compat_data *
286 ofw_bus_search_compatible(device_t dev, const struct ofw_compat_data *compat)
292 for (; compat->ocd_str != NULL; ++compat) {
293 if (ofw_bus_is_compatible(dev, compat->ocd_str))
301 ofw_bus_has_prop(device_t dev, const char *propname)
305 if ((node = ofw_bus_get_node(dev)) == -1)
308 return (OF_hasprop(node, propname));
312 ofw_bus_setup_iinfo(phandle_t node, struct ofw_bus_iinfo *ii, int intrsz)
317 if (OF_getencprop(node, "#address-cells", &addrc, sizeof(addrc)) == -1)
319 ii->opi_addrc = addrc * sizeof(pcell_t);
321 ii->opi_imapsz = OF_getencprop_alloc(node, "interrupt-map", 1,
322 (void **)&ii->opi_imap);
323 if (ii->opi_imapsz > 0) {
324 msksz = OF_getencprop_alloc(node, "interrupt-map-mask", 1,
325 (void **)&ii->opi_imapmsk);
327 * Failure to get the mask is ignored; a full mask is used
328 * then. We barf on bad mask sizes, however.
330 if (msksz != -1 && msksz != ii->opi_addrc + intrsz)
331 panic("ofw_bus_setup_iinfo: bad interrupt-map-mask "
337 ofw_bus_lookup_imap(phandle_t node, struct ofw_bus_iinfo *ii, void *reg,
338 int regsz, void *pintr, int pintrsz, void *mintr, int mintrsz,
341 uint8_t maskbuf[regsz + pintrsz];
344 if (ii->opi_imapsz <= 0)
346 KASSERT(regsz >= ii->opi_addrc,
347 ("ofw_bus_lookup_imap: register size too small: %d < %d",
348 regsz, ii->opi_addrc));
350 rv = OF_getencprop(node, "reg", reg, regsz);
352 panic("ofw_bus_lookup_imap: cannot get reg property");
354 return (ofw_bus_search_intrmap(pintr, pintrsz, reg, ii->opi_addrc,
355 ii->opi_imap, ii->opi_imapsz, ii->opi_imapmsk, maskbuf, mintr,
360 * Map an interrupt using the firmware reg, interrupt-map and
361 * interrupt-map-mask properties.
362 * The interrupt property to be mapped must be of size intrsz, and pointed to
363 * by intr. The regs property of the node for which the mapping is done must
364 * be passed as regs. This property is an array of register specifications;
365 * the size of the address part of such a specification must be passed as
366 * physsz. Only the first element of the property is used.
367 * imap and imapsz hold the interrupt mask and it's size.
368 * imapmsk is a pointer to the interrupt-map-mask property, which must have
369 * a size of physsz + intrsz; it may be NULL, in which case a full mask is
371 * maskbuf must point to a buffer of length physsz + intrsz.
372 * The interrupt is returned in result, which must point to a buffer of length
373 * rintrsz (which gives the expected size of the mapped interrupt).
374 * Returns number of cells in the interrupt if a mapping was found, 0 otherwise.
377 ofw_bus_search_intrmap(void *intr, int intrsz, void *regs, int physsz,
378 void *imap, int imapsz, void *imapmsk, void *maskbuf, void *result,
379 int rintrsz, phandle_t *iparent)
382 uint8_t *ref = maskbuf;
383 uint8_t *uiintr = intr;
384 uint8_t *uiregs = regs;
385 uint8_t *uiimapmsk = imapmsk;
391 if (imapmsk != NULL) {
392 for (i = 0; i < physsz; i++)
393 ref[i] = uiregs[i] & uiimapmsk[i];
394 for (i = 0; i < intrsz; i++)
395 ref[physsz + i] = uiintr[i] & uiimapmsk[physsz + i];
397 bcopy(regs, ref, physsz);
398 bcopy(intr, ref + physsz, intrsz);
405 bcopy(mptr + physsz + intrsz, &parent, sizeof(parent));
406 #ifndef OFW_IMAP_NO_IPARENT_ADDR_CELLS
408 * Find if we need to read the parent address data.
409 * CHRP-derived OF bindings, including ePAPR-compliant FDTs,
410 * use this as an optional part of the specifier.
412 if (OF_getencprop(OF_node_from_xref(parent),
413 "#address-cells", &paddrsz, sizeof(paddrsz)) == -1)
414 paddrsz = 0; /* default */
415 paddrsz *= sizeof(pcell_t);
418 if (OF_searchencprop(OF_node_from_xref(parent),
419 "#interrupt-cells", &pintrsz, sizeof(pintrsz)) == -1)
420 pintrsz = 1; /* default */
421 pintrsz *= sizeof(pcell_t);
423 /* Compute the map stride size. */
424 tsz = physsz + intrsz + sizeof(phandle_t) + paddrsz + pintrsz;
425 KASSERT(i >= tsz, ("ofw_bus_search_intrmap: truncated map"));
427 if (bcmp(ref, mptr, physsz + intrsz) == 0) {
428 bcopy(mptr + physsz + intrsz + sizeof(parent) + paddrsz,
429 result, MIN(rintrsz, pintrsz));
433 return (pintrsz/sizeof(pcell_t));
442 ofw_bus_msimap(phandle_t node, uint16_t pci_rid, phandle_t *msi_parent,
445 pcell_t *map, mask, msi_base, rid_base, rid_length;
450 /* TODO: This should be OF_searchprop_alloc if we had it */
451 len = OF_getencprop_alloc(node, "msi-map", sizeof(*map), (void **)&map);
453 if (msi_parent != NULL) {
455 OF_getencprop(node, "msi-parent", msi_parent,
456 sizeof(*msi_parent));
465 OF_getencprop(node, "msi-map-mask", &mask, sizeof(mask));
467 masked_rid = pci_rid & mask;
468 for (i = 0; i < len; i += 4) {
469 rid_base = map[i + 0];
470 rid_length = map[i + 3];
472 if (masked_rid < rid_base ||
473 masked_rid >= (rid_base + rid_length))
476 msi_base = map[i + 2];
478 if (msi_parent != NULL)
479 *msi_parent = map[i + 1];
481 *msi_rid = masked_rid - rid_base + msi_base;
486 free(map, M_OFWPROP);
492 ofw_bus_reg_to_rl(device_t dev, phandle_t node, pcell_t acells, pcell_t scells,
493 struct resource_list *rl)
496 ssize_t i, j, rid, nreg, ret;
501 * This may be just redundant when having ofw_bus_devinfo
502 * but makes this routine independent of it.
504 ret = OF_getprop_alloc(node, "name", sizeof(*name), (void **)&name);
508 ret = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)®);
509 nreg = (ret == -1) ? 0 : ret;
511 if (nreg % (acells + scells) != 0) {
513 device_printf(dev, "Malformed reg property on <%s>\n",
514 (name == NULL) ? "unknown" : name);
518 for (i = 0, rid = 0; i < nreg; i += acells + scells, rid++) {
520 for (j = 0; j < acells; j++) {
524 for (j = 0; j < scells; j++) {
526 size |= reg[i + acells + j];
528 /* Skip the dummy reg property of glue devices like ssm(4). */
530 resource_list_add(rl, SYS_RES_MEMORY, rid,
531 phys, phys + size - 1, size);
533 free(name, M_OFWPROP);
534 free(reg, M_OFWPROP);
540 * Get interrupt parent for given node.
541 * Returns 0 if interrupt parent doesn't exist.
544 ofw_bus_find_iparent(phandle_t node)
548 if (OF_searchencprop(node, "interrupt-parent", &iparent,
549 sizeof(iparent)) == -1) {
550 for (iparent = node; iparent != 0;
551 iparent = OF_parent(iparent)) {
552 if (OF_hasprop(iparent, "interrupt-controller"))
555 iparent = OF_xref_from_node(iparent);
561 ofw_bus_intr_to_rl(device_t dev, phandle_t node,
562 struct resource_list *rl, int *rlen)
565 uint32_t icells, *intr;
566 int err, i, irqnum, nintr, rid;
569 nintr = OF_getencprop_alloc(node, "interrupts", sizeof(*intr),
572 iparent = ofw_bus_find_iparent(node);
574 device_printf(dev, "No interrupt-parent found, "
575 "assuming direct parent\n");
576 iparent = OF_parent(node);
577 iparent = OF_xref_from_node(iparent);
579 if (OF_searchencprop(OF_node_from_xref(iparent),
580 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
581 device_printf(dev, "Missing #interrupt-cells "
582 "property, assuming <1>\n");
585 if (icells < 1 || icells > nintr) {
586 device_printf(dev, "Invalid #interrupt-cells property "
587 "value <%d>, assuming <1>\n", icells);
592 nintr = OF_getencprop_alloc(node, "interrupts-extended",
593 sizeof(*intr), (void **)&intr);
600 for (i = 0; i < nintr; i += icells) {
603 if (OF_searchencprop(OF_node_from_xref(iparent),
604 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
605 device_printf(dev, "Missing #interrupt-cells "
610 if (icells < 1 || (i + icells) > nintr) {
611 device_printf(dev, "Invalid #interrupt-cells "
612 "property value <%d>\n", icells);
617 irqnum = ofw_bus_map_intr(dev, iparent, icells, &intr[i]);
618 resource_list_add(rl, SYS_RES_IRQ, rid++, irqnum, irqnum, 1);
622 free(intr, M_OFWPROP);
627 ofw_bus_intr_by_rid(device_t dev, phandle_t node, int wanted_rid,
628 phandle_t *producer, int *ncells, pcell_t **cells)
631 uint32_t icells, *intr;
632 int err, i, nintr, rid;
635 nintr = OF_getencprop_alloc(node, "interrupts", sizeof(*intr),
638 iparent = ofw_bus_find_iparent(node);
640 device_printf(dev, "No interrupt-parent found, "
641 "assuming direct parent\n");
642 iparent = OF_parent(node);
643 iparent = OF_xref_from_node(iparent);
645 if (OF_searchencprop(OF_node_from_xref(iparent),
646 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
647 device_printf(dev, "Missing #interrupt-cells "
648 "property, assuming <1>\n");
651 if (icells < 1 || icells > nintr) {
652 device_printf(dev, "Invalid #interrupt-cells property "
653 "value <%d>, assuming <1>\n", icells);
658 nintr = OF_getencprop_alloc(node, "interrupts-extended",
659 sizeof(*intr), (void **)&intr);
666 for (i = 0; i < nintr; i += icells, rid++) {
669 if (OF_searchencprop(OF_node_from_xref(iparent),
670 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
671 device_printf(dev, "Missing #interrupt-cells "
676 if (icells < 1 || (i + icells) > nintr) {
677 device_printf(dev, "Invalid #interrupt-cells "
678 "property value <%d>\n", icells);
683 if (rid == wanted_rid) {
684 *cells = malloc(icells * sizeof(**cells), M_OFWPROP,
688 memcpy(*cells, intr + i, icells * sizeof(**cells));
693 free(intr, M_OFWPROP);
698 ofw_bus_find_child(phandle_t start, const char *child_name)
704 for (child = OF_child(start); child != 0; child = OF_peer(child)) {
705 ret = OF_getprop_alloc(child, "name", sizeof(*name), (void **)&name);
708 if (strcmp(name, child_name) == 0) {
709 free(name, M_OFWPROP);
713 free(name, M_OFWPROP);
720 ofw_bus_find_compatible(phandle_t node, const char *onecompat)
722 phandle_t child, ret;
725 * Traverse all children of 'start' node, and find first with
726 * matching 'compatible' property.
728 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
729 if (ofw_bus_node_is_compatible(child, onecompat) != 0)
732 ret = ofw_bus_find_compatible(child, onecompat);
740 * @brief Return child of bus whose phandle is node
742 * A direct child of @p will be returned if it its phandle in the
743 * OFW tree is @p node. Otherwise, NULL is returned.
745 * @param bus The bus to examine
746 * @param node The phandle_t to look for.
749 ofw_bus_find_child_device_by_phandle(device_t bus, phandle_t node)
751 device_t *children, retval, child;
755 * Nothing can match the flag value for no node.
761 * Search the children for a match. We microoptimize
762 * a bit by not using ofw_bus_get since we already know
763 * the parent. We do not recurse.
765 if (device_get_children(bus, &children, &nkid) != 0)
768 for (i = 0; i < nkid; i++) {
770 if (OFW_BUS_GET_NODE(bus, child) == node) {
775 free(children, M_TEMP);
781 * Parse property that contain list of xrefs and values
782 * (like standard "clocks" and "resets" properties)
784 * node - consumers device node
785 * list_name - name of parsed list - "clocks"
786 * cells_name - name of size property - "#clock-cells"
787 * idx - the index of the requested list entry, or, if -1, an indication
788 * to return the number of entries in the parsed list.
790 * producer - handle of producer
791 * ncells - number of cells in result or the number of items in the list when
793 * cells - array of decoded cells
796 ofw_bus_parse_xref_list_internal(phandle_t node, const char *list_name,
797 const char *cells_name, int idx, phandle_t *producer, int *ncells,
803 int rv, i, j, nelems, cnt;
806 nelems = OF_getencprop_alloc(node, list_name, sizeof(*elems),
810 rv = (idx == -1) ? 0 : ENOENT;
811 for (i = 0, cnt = 0; i < nelems; i += pcells, cnt++) {
813 if (OF_getencprop(OF_node_from_xref(pnode),
814 cells_name, &pcells, sizeof(pcells)) == -1) {
815 printf("Missing %s property\n", cells_name);
820 if ((i + pcells) > nelems) {
821 printf("Invalid %s property value <%d>\n", cells_name,
827 *cells= malloc(pcells * sizeof(**cells), M_OFWPROP,
831 for (j = 0; j < pcells; j++)
832 (*cells)[j] = elems[i + j];
838 free(elems, M_OFWPROP);
839 if (idx == -1 && rv == 0)
845 * Parse property that contain list of xrefs and values
846 * (like standard "clocks" and "resets" properties)
848 * node - consumers device node
849 * list_name - name of parsed list - "clocks"
850 * cells_name - name of size property - "#clock-cells"
851 * idx - the index of the requested list entry (>= 0)
853 * producer - handle of producer
854 * ncells - number of cells in result
855 * cells - array of decoded cells
858 ofw_bus_parse_xref_list_alloc(phandle_t node, const char *list_name,
859 const char *cells_name, int idx, phandle_t *producer, int *ncells,
864 ("ofw_bus_parse_xref_list_alloc: negative index supplied"));
866 return (ofw_bus_parse_xref_list_internal(node, list_name, cells_name,
867 idx, producer, ncells, cells));
871 * Parse property that contain list of xrefs and values
872 * (like standard "clocks" and "resets" properties)
873 * and determine the number of items in the list
875 * node - consumers device node
876 * list_name - name of parsed list - "clocks"
877 * cells_name - name of size property - "#clock-cells"
879 * count - number of items in list
882 ofw_bus_parse_xref_list_get_length(phandle_t node, const char *list_name,
883 const char *cells_name, int *count)
886 return (ofw_bus_parse_xref_list_internal(node, list_name, cells_name,
887 -1, NULL, count, NULL));
891 * Find index of string in string list property (case sensitive).
894 ofw_bus_find_string_index(phandle_t node, const char *list_name,
895 const char *name, int *idx)
898 int rv, i, cnt, nelems;
901 nelems = OF_getprop_alloc(node, list_name, 1, (void **)&elems);
906 for (i = 0, cnt = 0; i < nelems; cnt++) {
907 if (strcmp(elems + i, name) == 0) {
912 i += strlen(elems + i) + 1;
916 free(elems, M_OFWPROP);
921 * Create zero terminated array of strings from string list property.
924 ofw_bus_string_list_to_array(phandle_t node, const char *list_name,
925 const char ***out_array)
929 int i, cnt, nelems, len;
932 nelems = OF_getprop_alloc(node, list_name, 1, (void **)&elems);
936 /* Count number of strings. */
937 for (i = 0, cnt = 0; i < nelems; cnt++)
938 i += strlen(elems + i) + 1;
940 /* Allocate space for arrays and all strings. */
941 array = malloc((cnt + 1) * sizeof(char *) + nelems, M_OFWPROP,
944 /* Get address of first string. */
945 tptr = (char *)(array + cnt + 1);
948 memcpy(tptr, elems, nelems);
949 free(elems, M_OFWPROP);
951 /* Fill string pointers. */
952 for (i = 0, cnt = 0; i < nelems; cnt++) {
953 len = strlen(tptr) + 1;