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"
48 #define OFW_COMPAT_LEN 255
51 ofw_bus_gen_setup_devinfo(struct ofw_bus_devinfo *obd, phandle_t node)
56 /* The 'name' property is considered mandatory. */
57 if ((OF_getprop_alloc(node, "name", 1, (void **)&obd->obd_name)) == -1)
59 OF_getprop_alloc(node, "compatible", 1, (void **)&obd->obd_compat);
60 OF_getprop_alloc(node, "device_type", 1, (void **)&obd->obd_type);
61 OF_getprop_alloc(node, "model", 1, (void **)&obd->obd_model);
62 OF_getprop_alloc(node, "status", 1, (void **)&obd->obd_status);
68 ofw_bus_gen_destroy_devinfo(struct ofw_bus_devinfo *obd)
73 if (obd->obd_compat != NULL)
74 free(obd->obd_compat, M_OFWPROP);
75 if (obd->obd_model != NULL)
76 free(obd->obd_model, M_OFWPROP);
77 if (obd->obd_name != NULL)
78 free(obd->obd_name, M_OFWPROP);
79 if (obd->obd_type != NULL)
80 free(obd->obd_type, M_OFWPROP);
81 if (obd->obd_status != NULL)
82 free(obd->obd_status, M_OFWPROP);
86 ofw_bus_gen_child_pnpinfo_str(device_t cbdev, device_t child, char *buf,
90 if (ofw_bus_get_name(child) != NULL) {
91 strlcat(buf, "name=", buflen);
92 strlcat(buf, ofw_bus_get_name(child), buflen);
95 if (ofw_bus_get_compat(child) != NULL) {
96 strlcat(buf, " compat=", buflen);
97 strlcat(buf, ofw_bus_get_compat(child), buflen);
103 ofw_bus_gen_get_compat(device_t bus, device_t dev)
105 const struct ofw_bus_devinfo *obd;
107 obd = OFW_BUS_GET_DEVINFO(bus, dev);
110 return (obd->obd_compat);
114 ofw_bus_gen_get_model(device_t bus, device_t dev)
116 const struct ofw_bus_devinfo *obd;
118 obd = OFW_BUS_GET_DEVINFO(bus, dev);
121 return (obd->obd_model);
125 ofw_bus_gen_get_name(device_t bus, device_t dev)
127 const struct ofw_bus_devinfo *obd;
129 obd = OFW_BUS_GET_DEVINFO(bus, dev);
132 return (obd->obd_name);
136 ofw_bus_gen_get_node(device_t bus, device_t dev)
138 const struct ofw_bus_devinfo *obd;
140 obd = OFW_BUS_GET_DEVINFO(bus, dev);
143 return (obd->obd_node);
147 ofw_bus_gen_get_type(device_t bus, device_t dev)
149 const struct ofw_bus_devinfo *obd;
151 obd = OFW_BUS_GET_DEVINFO(bus, dev);
154 return (obd->obd_type);
158 ofw_bus_get_status(device_t dev)
160 const struct ofw_bus_devinfo *obd;
162 obd = OFW_BUS_GET_DEVINFO(device_get_parent(dev), dev);
166 return (obd->obd_status);
170 ofw_bus_status_okay(device_t dev)
174 status = ofw_bus_get_status(dev);
175 if (status == NULL || strcmp(status, "okay") == 0 ||
176 strcmp(status, "ok") == 0)
183 ofw_bus_node_is_compatible_int(const char *compat, int len,
184 const char *onecompat)
188 onelen = strlen(onecompat);
192 if (strlen(compat) == onelen &&
193 strncasecmp(compat, onecompat, onelen) == 0) {
199 /* Slide to the next sub-string. */
200 l = strlen(compat) + 1;
209 ofw_bus_node_is_compatible(phandle_t node, const char *compatstr)
211 char compat[OFW_COMPAT_LEN];
214 if ((len = OF_getproplen(node, "compatible")) <= 0)
217 bzero(compat, OFW_COMPAT_LEN);
219 if (OF_getprop(node, "compatible", compat, OFW_COMPAT_LEN) < 0)
222 rv = ofw_bus_node_is_compatible_int(compat, len, compatstr);
228 ofw_bus_is_compatible(device_t dev, const char *onecompat)
234 if ((compat = ofw_bus_get_compat(dev)) == NULL)
237 if ((node = ofw_bus_get_node(dev)) == -1)
240 /* Get total 'compatible' prop len */
241 if ((len = OF_getproplen(node, "compatible")) <= 0)
244 return (ofw_bus_node_is_compatible_int(compat, len, onecompat));
248 ofw_bus_is_compatible_strict(device_t dev, const char *compatible)
253 if ((compat = ofw_bus_get_compat(dev)) == NULL)
256 len = strlen(compatible);
257 if (strlen(compat) == len &&
258 strncasecmp(compat, compatible, len) == 0)
264 const struct ofw_compat_data *
265 ofw_bus_search_compatible(device_t dev, const struct ofw_compat_data *compat)
271 for (; compat->ocd_str != NULL; ++compat) {
272 if (ofw_bus_is_compatible(dev, compat->ocd_str))
280 ofw_bus_has_prop(device_t dev, const char *propname)
284 if ((node = ofw_bus_get_node(dev)) == -1)
287 return (OF_hasprop(node, propname));
291 ofw_bus_setup_iinfo(phandle_t node, struct ofw_bus_iinfo *ii, int intrsz)
296 if (OF_getencprop(node, "#address-cells", &addrc, sizeof(addrc)) == -1)
298 ii->opi_addrc = addrc * sizeof(pcell_t);
300 ii->opi_imapsz = OF_getencprop_alloc(node, "interrupt-map", 1,
301 (void **)&ii->opi_imap);
302 if (ii->opi_imapsz > 0) {
303 msksz = OF_getencprop_alloc(node, "interrupt-map-mask", 1,
304 (void **)&ii->opi_imapmsk);
306 * Failure to get the mask is ignored; a full mask is used
307 * then. We barf on bad mask sizes, however.
309 if (msksz != -1 && msksz != ii->opi_addrc + intrsz)
310 panic("ofw_bus_setup_iinfo: bad interrupt-map-mask "
316 ofw_bus_lookup_imap(phandle_t node, struct ofw_bus_iinfo *ii, void *reg,
317 int regsz, void *pintr, int pintrsz, void *mintr, int mintrsz,
320 uint8_t maskbuf[regsz + pintrsz];
323 if (ii->opi_imapsz <= 0)
325 KASSERT(regsz >= ii->opi_addrc,
326 ("ofw_bus_lookup_imap: register size too small: %d < %d",
327 regsz, ii->opi_addrc));
329 rv = OF_getencprop(node, "reg", reg, regsz);
331 panic("ofw_bus_lookup_imap: cannot get reg property");
333 return (ofw_bus_search_intrmap(pintr, pintrsz, reg, ii->opi_addrc,
334 ii->opi_imap, ii->opi_imapsz, ii->opi_imapmsk, maskbuf, mintr,
339 * Map an interrupt using the firmware reg, interrupt-map and
340 * interrupt-map-mask properties.
341 * The interrupt property to be mapped must be of size intrsz, and pointed to
342 * by intr. The regs property of the node for which the mapping is done must
343 * be passed as regs. This property is an array of register specifications;
344 * the size of the address part of such a specification must be passed as
345 * physsz. Only the first element of the property is used.
346 * imap and imapsz hold the interrupt mask and it's size.
347 * imapmsk is a pointer to the interrupt-map-mask property, which must have
348 * a size of physsz + intrsz; it may be NULL, in which case a full mask is
350 * maskbuf must point to a buffer of length physsz + intrsz.
351 * The interrupt is returned in result, which must point to a buffer of length
352 * rintrsz (which gives the expected size of the mapped interrupt).
353 * Returns number of cells in the interrupt if a mapping was found, 0 otherwise.
356 ofw_bus_search_intrmap(void *intr, int intrsz, void *regs, int physsz,
357 void *imap, int imapsz, void *imapmsk, void *maskbuf, void *result,
358 int rintrsz, phandle_t *iparent)
361 uint8_t *ref = maskbuf;
362 uint8_t *uiintr = intr;
363 uint8_t *uiregs = regs;
364 uint8_t *uiimapmsk = imapmsk;
371 if (imapmsk != NULL) {
372 for (i = 0; i < physsz; i++)
373 ref[i] = uiregs[i] & uiimapmsk[i];
374 for (i = 0; i < intrsz; i++)
375 ref[physsz + i] = uiintr[i] & uiimapmsk[physsz + i];
377 bcopy(regs, ref, physsz);
378 bcopy(intr, ref + physsz, intrsz);
385 bcopy(mptr + physsz + intrsz, &parent, sizeof(parent));
386 #ifndef OFW_IMAP_NO_IPARENT_ADDR_CELLS
388 * Find if we need to read the parent address data.
389 * CHRP-derived OF bindings, including ePAPR-compliant FDTs,
390 * use this as an optional part of the specifier.
392 if (OF_getencprop(OF_node_from_xref(parent),
393 "#address-cells", &paddrsz, sizeof(paddrsz)) == -1)
394 paddrsz = 0; /* default */
395 paddrsz *= sizeof(pcell_t);
398 if (OF_searchencprop(OF_node_from_xref(parent),
399 "#interrupt-cells", &pintrsz, sizeof(pintrsz)) == -1)
400 pintrsz = 1; /* default */
401 pintrsz *= sizeof(pcell_t);
403 /* Compute the map stride size. */
404 tsz = physsz + intrsz + sizeof(phandle_t) + paddrsz + pintrsz;
405 KASSERT(i >= tsz, ("ofw_bus_search_intrmap: truncated map"));
407 if (bcmp(ref, mptr, physsz + intrsz) == 0) {
408 bcopy(mptr + physsz + intrsz + sizeof(parent) + paddrsz,
409 result, MIN(rintrsz, pintrsz));
413 return (pintrsz/sizeof(pcell_t));
422 ofw_bus_msimap(phandle_t node, uint16_t pci_rid, phandle_t *msi_parent,
425 pcell_t *map, mask, msi_base, rid_base, rid_length;
427 uint32_t masked_rid, rid;
430 /* TODO: This should be OF_searchprop_alloc if we had it */
431 len = OF_getencprop_alloc(node, "msi-map", sizeof(*map), (void **)&map);
433 if (msi_parent != NULL) {
435 OF_getencprop(node, "msi-parent", msi_parent,
436 sizeof(*msi_parent));
446 OF_getencprop(node, "msi-map-mask", &mask, sizeof(mask));
448 masked_rid = pci_rid & mask;
449 for (i = 0; i < len; i += 4) {
450 rid_base = map[i + 0];
451 rid_length = map[i + 3];
453 if (masked_rid < rid_base ||
454 masked_rid >= (rid_base + rid_length))
457 msi_base = map[i + 2];
459 if (msi_parent != NULL)
460 *msi_parent = map[i + 1];
462 *msi_rid = masked_rid - rid_base + msi_base;
467 free(map, M_OFWPROP);
473 ofw_bus_reg_to_rl(device_t dev, phandle_t node, pcell_t acells, pcell_t scells,
474 struct resource_list *rl)
477 ssize_t i, j, rid, nreg, ret;
482 * This may be just redundant when having ofw_bus_devinfo
483 * but makes this routine independent of it.
485 ret = OF_getprop_alloc(node, "name", sizeof(*name), (void **)&name);
489 ret = OF_getencprop_alloc(node, "reg", sizeof(*reg), (void **)®);
490 nreg = (ret == -1) ? 0 : ret;
492 if (nreg % (acells + scells) != 0) {
494 device_printf(dev, "Malformed reg property on <%s>\n",
495 (name == NULL) ? "unknown" : name);
499 for (i = 0, rid = 0; i < nreg; i += acells + scells, rid++) {
501 for (j = 0; j < acells; j++) {
505 for (j = 0; j < scells; j++) {
507 size |= reg[i + acells + j];
509 /* Skip the dummy reg property of glue devices like ssm(4). */
511 resource_list_add(rl, SYS_RES_MEMORY, rid,
512 phys, phys + size - 1, size);
514 free(name, M_OFWPROP);
515 free(reg, M_OFWPROP);
521 * Get interrupt parent for given node.
522 * Returns 0 if interrupt parent doesn't exist.
525 ofw_bus_find_iparent(phandle_t node)
529 if (OF_searchencprop(node, "interrupt-parent", &iparent,
530 sizeof(iparent)) == -1) {
531 for (iparent = node; iparent != 0;
532 iparent = OF_parent(iparent)) {
533 if (OF_hasprop(iparent, "interrupt-controller"))
536 iparent = OF_xref_from_node(iparent);
542 ofw_bus_intr_to_rl(device_t dev, phandle_t node,
543 struct resource_list *rl, int *rlen)
546 uint32_t icells, *intr;
547 int err, i, irqnum, nintr, rid;
550 nintr = OF_getencprop_alloc(node, "interrupts", sizeof(*intr),
553 iparent = ofw_bus_find_iparent(node);
555 device_printf(dev, "No interrupt-parent found, "
556 "assuming direct parent\n");
557 iparent = OF_parent(node);
558 iparent = OF_xref_from_node(iparent);
560 if (OF_searchencprop(OF_node_from_xref(iparent),
561 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
562 device_printf(dev, "Missing #interrupt-cells "
563 "property, assuming <1>\n");
566 if (icells < 1 || icells > nintr) {
567 device_printf(dev, "Invalid #interrupt-cells property "
568 "value <%d>, assuming <1>\n", icells);
573 nintr = OF_getencprop_alloc(node, "interrupts-extended",
574 sizeof(*intr), (void **)&intr);
581 for (i = 0; i < nintr; i += icells) {
584 if (OF_searchencprop(OF_node_from_xref(iparent),
585 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
586 device_printf(dev, "Missing #interrupt-cells "
591 if (icells < 1 || (i + icells) > nintr) {
592 device_printf(dev, "Invalid #interrupt-cells "
593 "property value <%d>\n", icells);
598 irqnum = ofw_bus_map_intr(dev, iparent, icells, &intr[i]);
599 resource_list_add(rl, SYS_RES_IRQ, rid++, irqnum, irqnum, 1);
603 free(intr, M_OFWPROP);
608 ofw_bus_intr_by_rid(device_t dev, phandle_t node, int wanted_rid,
609 phandle_t *producer, int *ncells, pcell_t **cells)
612 uint32_t icells, *intr;
613 int err, i, nintr, rid;
616 nintr = OF_getencprop_alloc(node, "interrupts", sizeof(*intr),
619 iparent = ofw_bus_find_iparent(node);
621 device_printf(dev, "No interrupt-parent found, "
622 "assuming direct parent\n");
623 iparent = OF_parent(node);
624 iparent = OF_xref_from_node(iparent);
626 if (OF_searchencprop(OF_node_from_xref(iparent),
627 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
628 device_printf(dev, "Missing #interrupt-cells "
629 "property, assuming <1>\n");
632 if (icells < 1 || icells > nintr) {
633 device_printf(dev, "Invalid #interrupt-cells property "
634 "value <%d>, assuming <1>\n", icells);
639 nintr = OF_getencprop_alloc(node, "interrupts-extended",
640 sizeof(*intr), (void **)&intr);
647 for (i = 0; i < nintr; i += icells, rid++) {
650 if (OF_searchencprop(OF_node_from_xref(iparent),
651 "#interrupt-cells", &icells, sizeof(icells)) == -1) {
652 device_printf(dev, "Missing #interrupt-cells "
657 if (icells < 1 || (i + icells) > nintr) {
658 device_printf(dev, "Invalid #interrupt-cells "
659 "property value <%d>\n", icells);
664 if (rid == wanted_rid) {
665 *cells = malloc(icells * sizeof(**cells), M_OFWPROP,
669 memcpy(*cells, intr + i, icells * sizeof(**cells));
674 free(intr, M_OFWPROP);
679 ofw_bus_find_child(phandle_t start, const char *child_name)
685 for (child = OF_child(start); child != 0; child = OF_peer(child)) {
686 ret = OF_getprop_alloc(child, "name", sizeof(*name), (void **)&name);
689 if (strcmp(name, child_name) == 0) {
690 free(name, M_OFWPROP);
694 free(name, M_OFWPROP);
701 ofw_bus_find_compatible(phandle_t node, const char *onecompat)
703 phandle_t child, ret;
708 * Traverse all children of 'start' node, and find first with
709 * matching 'compatible' property.
711 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
712 len = OF_getprop_alloc(child, "compatible", 1, &compat);
714 ret = ofw_bus_node_is_compatible_int(compat, len,
716 free(compat, M_OFWPROP);
721 ret = ofw_bus_find_compatible(child, onecompat);
729 * @brief Return child of bus whose phandle is node
731 * A direct child of @p will be returned if it its phandle in the
732 * OFW tree is @p node. Otherwise, NULL is returned.
734 * @param bus The bus to examine
735 * @param node The phandle_t to look for.
738 ofw_bus_find_child_device_by_phandle(device_t bus, phandle_t node)
740 device_t *children, retval, child;
744 * Nothing can match the flag value for no node.
750 * Search the children for a match. We microoptimize
751 * a bit by not using ofw_bus_get since we already know
752 * the parent. We do not recurse.
754 if (device_get_children(bus, &children, &nkid) != 0)
757 for (i = 0; i < nkid; i++) {
759 if (OFW_BUS_GET_NODE(bus, child) == node) {
764 free(children, M_TEMP);
770 * Parse property that contain list of xrefs and values
771 * (like standard "clocks" and "resets" properties)
773 * node - consumers device node
774 * list_name - name of parsed list - "clocks"
775 * cells_name - name of size property - "#clock-cells"
776 * idx - the index of the requested list entry, or, if -1, an indication
777 * to return the number of entries in the parsed list.
779 * producer - handle of producer
780 * ncells - number of cells in result or the number of items in the list when
782 * cells - array of decoded cells
785 ofw_bus_parse_xref_list_internal(phandle_t node, const char *list_name,
786 const char *cells_name, int idx, phandle_t *producer, int *ncells,
792 int rv, i, j, nelems, cnt;
795 nelems = OF_getencprop_alloc(node, list_name, sizeof(*elems),
799 rv = (idx == -1) ? 0 : ENOENT;
800 for (i = 0, cnt = 0; i < nelems; i += pcells, cnt++) {
802 if (OF_getencprop(OF_node_from_xref(pnode),
803 cells_name, &pcells, sizeof(pcells)) == -1) {
804 printf("Missing %s property\n", cells_name);
809 if ((i + pcells) > nelems) {
810 printf("Invalid %s property value <%d>\n", cells_name,
816 *cells= malloc(pcells * sizeof(**cells), M_OFWPROP,
820 for (j = 0; j < pcells; j++)
821 (*cells)[j] = elems[i + j];
827 free(elems, M_OFWPROP);
828 if (idx == -1 && rv == 0)
834 * Parse property that contain list of xrefs and values
835 * (like standard "clocks" and "resets" properties)
837 * node - consumers device node
838 * list_name - name of parsed list - "clocks"
839 * cells_name - name of size property - "#clock-cells"
840 * idx - the index of the requested list entry (>= 0)
842 * producer - handle of producer
843 * ncells - number of cells in result
844 * cells - array of decoded cells
847 ofw_bus_parse_xref_list_alloc(phandle_t node, const char *list_name,
848 const char *cells_name, int idx, phandle_t *producer, int *ncells,
853 ("ofw_bus_parse_xref_list_alloc: negative index supplied"));
855 return (ofw_bus_parse_xref_list_internal(node, list_name, cells_name,
856 idx, producer, ncells, cells));
860 * Parse property that contain list of xrefs and values
861 * (like standard "clocks" and "resets" properties)
862 * and determine the number of items in the list
864 * node - consumers device node
865 * list_name - name of parsed list - "clocks"
866 * cells_name - name of size property - "#clock-cells"
868 * count - number of items in list
871 ofw_bus_parse_xref_list_get_length(phandle_t node, const char *list_name,
872 const char *cells_name, int *count)
875 return (ofw_bus_parse_xref_list_internal(node, list_name, cells_name,
876 -1, NULL, count, NULL));
880 * Find index of string in string list property (case sensitive).
883 ofw_bus_find_string_index(phandle_t node, const char *list_name,
884 const char *name, int *idx)
887 int rv, i, cnt, nelems;
890 nelems = OF_getprop_alloc(node, list_name, 1, (void **)&elems);
895 for (i = 0, cnt = 0; i < nelems; cnt++) {
896 if (strcmp(elems + i, name) == 0) {
901 i += strlen(elems + i) + 1;
905 free(elems, M_OFWPROP);
910 * Create zero terminated array of strings from string list property.
913 ofw_bus_string_list_to_array(phandle_t node, const char *list_name,
914 const char ***out_array)
918 int i, cnt, nelems, len;
921 nelems = OF_getprop_alloc(node, list_name, 1, (void **)&elems);
925 /* Count number of strings. */
926 for (i = 0, cnt = 0; i < nelems; cnt++)
927 i += strlen(elems + i) + 1;
929 /* Allocate space for arrays and all strings. */
930 array = malloc((cnt + 1) * sizeof(char *) + nelems, M_OFWPROP,
933 /* Get address of first string. */
934 tptr = (char *)(array + cnt + 1);
937 memcpy(tptr, elems, nelems);
938 free(elems, M_OFWPROP);
940 /* Fill string pointers. */
941 for (i = 0, cnt = 0; i < nelems; cnt++) {
942 len = strlen(tptr) + 1;