2 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3 * Copyright (c) 2017 The FreeBSD Foundation
6 * Portions of this software were developed by Landon Fuller
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
17 * redistribution must be conditioned upon including a substantially
18 * similar Disclaimer requirement for further binary redistribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
24 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
26 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGES.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
39 #include <sys/refcount.h>
40 #include <sys/systm.h>
42 #include <machine/bus.h>
44 #include <machine/resource.h>
46 #include <dev/bhnd/siba/sibareg.h>
48 #include <dev/bhnd/cores/chipc/chipcreg.h>
50 #include "nvram/bhnd_nvram.h"
52 #include "bhnd_chipc_if.h"
54 #include "bhnd_nvram_if.h"
55 #include "bhnd_nvram_map.h"
59 #include "bhnd_private.h"
61 static void bhnd_service_registry_free_entry(
62 struct bhnd_service_entry *entry);
64 static int compare_ascending_probe_order(const void *lhs, const void *rhs);
65 static int compare_descending_probe_order(const void *lhs,
68 /* BHND core device description table. */
69 static const struct bhnd_core_desc {
72 bhnd_devclass_t class;
74 } bhnd_core_descs[] = {
75 #define BHND_CDESC(_mfg, _cid, _cls, _desc) \
76 { BHND_MFGID_ ## _mfg, BHND_COREID_ ## _cid, \
77 BHND_DEVCLASS_ ## _cls, _desc }
79 BHND_CDESC(BCM, CC, CC, "ChipCommon I/O Controller"),
80 BHND_CDESC(BCM, ILINE20, OTHER, "iLine20 HPNA"),
81 BHND_CDESC(BCM, SRAM, RAM, "SRAM"),
82 BHND_CDESC(BCM, SDRAM, RAM, "SDRAM"),
83 BHND_CDESC(BCM, PCI, PCI, "PCI Bridge"),
84 BHND_CDESC(BCM, MIPS, CPU, "BMIPS CPU"),
85 BHND_CDESC(BCM, ENET, ENET_MAC, "Fast Ethernet MAC"),
86 BHND_CDESC(BCM, CODEC, OTHER, "V.90 Modem Codec"),
87 BHND_CDESC(BCM, USB, USB_DUAL, "USB 1.1 Device/Host Controller"),
88 BHND_CDESC(BCM, ADSL, OTHER, "ADSL Core"),
89 BHND_CDESC(BCM, ILINE100, OTHER, "iLine100 HPNA"),
90 BHND_CDESC(BCM, IPSEC, OTHER, "IPsec Accelerator"),
91 BHND_CDESC(BCM, UTOPIA, OTHER, "UTOPIA ATM Core"),
92 BHND_CDESC(BCM, PCMCIA, PCCARD, "PCMCIA Bridge"),
93 BHND_CDESC(BCM, SOCRAM, RAM, "Internal Memory"),
94 BHND_CDESC(BCM, MEMC, MEMC, "MEMC SDRAM Controller"),
95 BHND_CDESC(BCM, OFDM, OTHER, "OFDM PHY"),
96 BHND_CDESC(BCM, EXTIF, OTHER, "External Interface"),
97 BHND_CDESC(BCM, D11, WLAN, "802.11 MAC/PHY/Radio"),
98 BHND_CDESC(BCM, APHY, WLAN_PHY, "802.11a PHY"),
99 BHND_CDESC(BCM, BPHY, WLAN_PHY, "802.11b PHY"),
100 BHND_CDESC(BCM, GPHY, WLAN_PHY, "802.11g PHY"),
101 BHND_CDESC(BCM, MIPS33, CPU, "BMIPS33 CPU"),
102 BHND_CDESC(BCM, USB11H, USB_HOST, "USB 1.1 Host Controller"),
103 BHND_CDESC(BCM, USB11D, USB_DEV, "USB 1.1 Device Controller"),
104 BHND_CDESC(BCM, USB20H, USB_HOST, "USB 2.0 Host Controller"),
105 BHND_CDESC(BCM, USB20D, USB_DEV, "USB 2.0 Device Controller"),
106 BHND_CDESC(BCM, SDIOH, OTHER, "SDIO Host Controller"),
107 BHND_CDESC(BCM, ROBO, OTHER, "RoboSwitch"),
108 BHND_CDESC(BCM, ATA100, OTHER, "Parallel ATA Controller"),
109 BHND_CDESC(BCM, SATAXOR, OTHER, "SATA DMA/XOR Controller"),
110 BHND_CDESC(BCM, GIGETH, ENET_MAC, "Gigabit Ethernet MAC"),
111 BHND_CDESC(BCM, PCIE, PCIE, "PCIe Bridge"),
112 BHND_CDESC(BCM, NPHY, WLAN_PHY, "802.11n 2x2 PHY"),
113 BHND_CDESC(BCM, SRAMC, MEMC, "SRAM Controller"),
114 BHND_CDESC(BCM, MINIMAC, OTHER, "MINI MAC/PHY"),
115 BHND_CDESC(BCM, ARM11, CPU, "ARM1176 CPU"),
116 BHND_CDESC(BCM, ARM7S, CPU, "ARM7TDMI-S CPU"),
117 BHND_CDESC(BCM, LPPHY, WLAN_PHY, "802.11a/b/g PHY"),
118 BHND_CDESC(BCM, PMU, PMU, "PMU"),
119 BHND_CDESC(BCM, SSNPHY, WLAN_PHY, "802.11n Single-Stream PHY"),
120 BHND_CDESC(BCM, SDIOD, OTHER, "SDIO Device Core"),
121 BHND_CDESC(BCM, ARMCM3, CPU, "ARM Cortex-M3 CPU"),
122 BHND_CDESC(BCM, HTPHY, WLAN_PHY, "802.11n 4x4 PHY"),
123 BHND_CDESC(MIPS,MIPS74K, CPU, "MIPS74k CPU"),
124 BHND_CDESC(BCM, GMAC, ENET_MAC, "Gigabit MAC core"),
125 BHND_CDESC(BCM, DMEMC, MEMC, "DDR1/DDR2 Memory Controller"),
126 BHND_CDESC(BCM, PCIERC, OTHER, "PCIe Root Complex"),
127 BHND_CDESC(BCM, OCP, SOC_BRIDGE, "OCP to OCP Bridge"),
128 BHND_CDESC(BCM, SC, OTHER, "Shared Common Core"),
129 BHND_CDESC(BCM, AHB, SOC_BRIDGE, "OCP to AHB Bridge"),
130 BHND_CDESC(BCM, SPIH, OTHER, "SPI Host Controller"),
131 BHND_CDESC(BCM, I2S, OTHER, "I2S Digital Audio Interface"),
132 BHND_CDESC(BCM, DMEMS, MEMC, "SDR/DDR1 Memory Controller"),
133 BHND_CDESC(BCM, UBUS_SHIM, OTHER, "BCM6362/UBUS WLAN SHIM"),
134 BHND_CDESC(BCM, PCIE2, PCIE, "PCIe Bridge (Gen2)"),
136 BHND_CDESC(ARM, APB_BRIDGE, SOC_BRIDGE, "BP135 AMBA3 AXI to APB Bridge"),
137 BHND_CDESC(ARM, PL301, SOC_ROUTER, "PL301 AMBA3 Interconnect"),
138 BHND_CDESC(ARM, EROM, EROM, "PL366 Device Enumeration ROM"),
139 BHND_CDESC(ARM, OOB_ROUTER, OTHER, "PL367 OOB Interrupt Router"),
140 BHND_CDESC(ARM, AXI_UNMAPPED, OTHER, "Unmapped Address Ranges"),
142 BHND_CDESC(BCM, 4706_CC, CC, "ChipCommon I/O Controller"),
143 BHND_CDESC(BCM, NS_PCIE2, PCIE, "PCIe Bridge (Gen2)"),
144 BHND_CDESC(BCM, NS_DMA, OTHER, "DMA engine"),
145 BHND_CDESC(BCM, NS_SDIO, OTHER, "SDIO 3.0 Host Controller"),
146 BHND_CDESC(BCM, NS_USB20H, USB_HOST, "USB 2.0 Host Controller"),
147 BHND_CDESC(BCM, NS_USB30H, USB_HOST, "USB 3.0 Host Controller"),
148 BHND_CDESC(BCM, NS_A9JTAG, OTHER, "ARM Cortex A9 JTAG Interface"),
149 BHND_CDESC(BCM, NS_DDR23_MEMC, MEMC, "Denali DDR2/DD3 Memory Controller"),
150 BHND_CDESC(BCM, NS_ROM, NVRAM, "System ROM"),
151 BHND_CDESC(BCM, NS_NAND, NVRAM, "NAND Flash Controller"),
152 BHND_CDESC(BCM, NS_QSPI, NVRAM, "QSPI Flash Controller"),
153 BHND_CDESC(BCM, NS_CC_B, CC_B, "ChipCommon B Auxiliary I/O Controller"),
154 BHND_CDESC(BCM, 4706_SOCRAM, RAM, "Internal Memory"),
155 BHND_CDESC(BCM, IHOST_ARMCA9, CPU, "ARM Cortex A9 CPU"),
156 BHND_CDESC(BCM, 4706_GMAC_CMN, ENET, "Gigabit MAC (Common)"),
157 BHND_CDESC(BCM, 4706_GMAC, ENET_MAC, "Gigabit MAC"),
158 BHND_CDESC(BCM, AMEMC, MEMC, "Denali DDR1/DDR2 Memory Controller"),
161 /* Derived from inspection of the BCM4331 cores that provide PrimeCell
162 * IDs. Due to lack of documentation, the surmised device name/purpose
163 * provided here may be incorrect. */
164 { BHND_MFGID_ARM, BHND_PRIMEID_EROM, BHND_DEVCLASS_OTHER,
165 "PL364 Device Enumeration ROM" },
166 { BHND_MFGID_ARM, BHND_PRIMEID_SWRAP, BHND_DEVCLASS_OTHER,
167 "PL368 Device Management Interface" },
168 { BHND_MFGID_ARM, BHND_PRIMEID_MWRAP, BHND_DEVCLASS_OTHER,
169 "PL369 Device Management Interface" },
175 * Return the name for a given JEP106 manufacturer ID.
177 * @param vendor A JEP106 Manufacturer ID, including the non-standard ARM 4-bit
178 * JEP106 continuation code.
181 bhnd_vendor_name(uint16_t vendor)
188 case BHND_MFGID_MIPS:
196 * Return the name of a port type.
199 bhnd_port_type_name(bhnd_port_type port_type)
202 case BHND_PORT_DEVICE:
204 case BHND_PORT_BRIDGE:
206 case BHND_PORT_AGENT:
214 * Return the name of an NVRAM source.
217 bhnd_nvram_src_name(bhnd_nvram_src nvram_src)
220 case BHND_NVRAM_SRC_FLASH:
222 case BHND_NVRAM_SRC_OTP:
224 case BHND_NVRAM_SRC_SPROM:
226 case BHND_NVRAM_SRC_UNKNOWN:
233 static const struct bhnd_core_desc *
234 bhnd_find_core_desc(uint16_t vendor, uint16_t device)
236 for (u_int i = 0; bhnd_core_descs[i].desc != NULL; i++) {
237 if (bhnd_core_descs[i].vendor != vendor)
240 if (bhnd_core_descs[i].device != device)
243 return (&bhnd_core_descs[i]);
250 * Return a human-readable name for a BHND core.
252 * @param vendor The core designer's JEDEC-106 Manufacturer ID
253 * @param device The core identifier.
256 bhnd_find_core_name(uint16_t vendor, uint16_t device)
258 const struct bhnd_core_desc *desc;
260 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)
267 * Return the device class for a BHND core.
269 * @param vendor The core designer's JEDEC-106 Manufacturer ID
270 * @param device The core identifier.
273 bhnd_find_core_class(uint16_t vendor, uint16_t device)
275 const struct bhnd_core_desc *desc;
277 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)
278 return (BHND_DEVCLASS_OTHER);
284 * Return a human-readable name for a BHND core.
286 * @param ci The core's info record.
289 bhnd_core_name(const struct bhnd_core_info *ci)
291 return bhnd_find_core_name(ci->vendor, ci->device);
295 * Return the device class for a BHND core.
297 * @param ci The core's info record.
300 bhnd_core_class(const struct bhnd_core_info *ci)
302 return bhnd_find_core_class(ci->vendor, ci->device);
306 * Write a human readable name representation of the given
307 * BHND_CHIPID_* constant to @p buffer.
309 * @param buffer Output buffer, or NULL to compute the required size.
310 * @param size Capacity of @p buffer, in bytes.
311 * @param chip_id Chip ID to be formatted.
313 * @return Returns the required number of bytes on success, or a negative
314 * integer on failure. No more than @p size-1 characters be written, with
315 * the @p size'th set to '\0'.
317 * @sa BHND_CHIPID_MAX_NAMELEN
320 bhnd_format_chip_id(char *buffer, size_t size, uint16_t chip_id)
322 /* All hex formatted IDs are within the range of 0x4000-0x9C3F (40000-1) */
323 if (chip_id >= 0x4000 && chip_id <= 0x9C3F)
324 return (snprintf(buffer, size, "BCM%hX", chip_id));
326 return (snprintf(buffer, size, "BCM%hu", chip_id));
330 * Initialize a core info record with data from from a bhnd-attached @p dev.
332 * @param dev A bhnd device.
333 * @param core The record to be initialized.
335 struct bhnd_core_info
336 bhnd_get_core_info(device_t dev) {
337 return (struct bhnd_core_info) {
338 .vendor = bhnd_get_vendor(dev),
339 .device = bhnd_get_device(dev),
340 .hwrev = bhnd_get_hwrev(dev),
341 .core_idx = bhnd_get_core_index(dev),
342 .unit = bhnd_get_core_unit(dev)
347 * Find a @p class child device with @p unit on @p bus.
349 * @param bus The bhnd-compatible bus to be searched.
350 * @param class The device class to match on.
351 * @param unit The core unit number; specify -1 to return the first match
352 * regardless of unit number.
354 * @retval device_t if a matching child device is found.
355 * @retval NULL if no matching child device is found.
358 bhnd_bus_find_child(device_t bus, bhnd_devclass_t class, int unit)
360 struct bhnd_core_match md = {
361 BHND_MATCH_CORE_CLASS(class),
362 BHND_MATCH_CORE_UNIT(unit)
366 md.m.match.core_unit = 0;
368 return bhnd_bus_match_child(bus, &md);
372 * Find the first child device on @p bus that matches @p desc.
374 * @param bus The bhnd-compatible bus to be searched.
375 * @param desc A match descriptor.
377 * @retval device_t if a matching child device is found.
378 * @retval NULL if no matching child device is found.
381 bhnd_bus_match_child(device_t bus, const struct bhnd_core_match *desc)
388 error = device_get_children(bus, &devlistp, &devcnt);
393 for (int i = 0; i < devcnt; i++) {
394 struct bhnd_core_info ci = bhnd_get_core_info(devlistp[i]);
396 if (bhnd_core_matches(&ci, desc)) {
403 free(devlistp, M_TEMP);
408 * Retrieve an ordered list of all device instances currently connected to
409 * @p bus, returning a pointer to the array in @p devlistp and the count
412 * The memory allocated for the table must be freed via
413 * bhnd_bus_free_children().
415 * @param bus The bhnd-compatible bus to be queried.
416 * @param[out] devlist The array of devices.
417 * @param[out] devcount The number of devices in @p devlistp
418 * @param order The order in which devices will be returned
422 * @retval non-zero if an error occurs, a regular unix error code will
426 bhnd_bus_get_children(device_t bus, device_t **devlist, int *devcount,
427 bhnd_device_order order)
431 /* Fetch device array */
432 if ((error = device_get_children(bus, devlist, devcount)))
435 /* Perform requested sorting */
436 if ((error = bhnd_sort_devices(*devlist, *devcount, order))) {
437 bhnd_bus_free_children(*devlist);
445 * Free any memory allocated in a previous call to bhnd_bus_get_children().
447 * @param devlist The device array returned by bhnd_bus_get_children().
450 bhnd_bus_free_children(device_t *devlist)
452 free(devlist, M_TEMP);
456 * Perform in-place sorting of an array of bhnd device instances.
458 * @param devlist An array of bhnd devices.
459 * @param devcount The number of devices in @p devs.
460 * @param order The sort order to be used.
463 bhnd_sort_devices(device_t *devlist, size_t devcount, bhnd_device_order order)
465 int (*compare)(const void *, const void *);
468 case BHND_DEVICE_ORDER_ATTACH:
469 compare = compare_ascending_probe_order;
471 case BHND_DEVICE_ORDER_DETACH:
472 compare = compare_descending_probe_order;
475 printf("unknown sort order: %d\n", order);
479 qsort(devlist, devcount, sizeof(*devlist), compare);
484 * Ascending comparison of bhnd device's probe order.
487 compare_ascending_probe_order(const void *lhs, const void *rhs)
492 ldev = (*(const device_t *) lhs);
493 rdev = (*(const device_t *) rhs);
495 lorder = BHND_BUS_GET_PROBE_ORDER(device_get_parent(ldev), ldev);
496 rorder = BHND_BUS_GET_PROBE_ORDER(device_get_parent(rdev), rdev);
498 if (lorder < rorder) {
500 } else if (lorder > rorder) {
508 * Descending comparison of bhnd device's probe order.
511 compare_descending_probe_order(const void *lhs, const void *rhs)
513 return (compare_ascending_probe_order(rhs, lhs));
517 * Call device_probe_and_attach() for each of the bhnd bus device's
518 * children, in bhnd attach order.
520 * @param bus The bhnd-compatible bus for which all children should be probed
524 bhnd_bus_probe_children(device_t bus)
530 /* Fetch children in attach order */
531 error = bhnd_bus_get_children(bus, &devs, &ndevs,
532 BHND_DEVICE_ORDER_ATTACH);
536 /* Probe and attach all children */
537 for (int i = 0; i < ndevs; i++) {
538 device_t child = devs[i];
539 device_probe_and_attach(child);
542 bhnd_bus_free_children(devs);
548 * Walk up the bhnd device hierarchy to locate the root device
549 * to which the bhndb bridge is attached.
551 * This can be used from within bhnd host bridge drivers to locate the
552 * actual upstream host device.
554 * @param dev A bhnd device.
555 * @param bus_class The expected bus (e.g. "pci") to which the bridge root
556 * should be attached.
558 * @retval device_t if a matching parent device is found.
559 * @retval NULL @p dev is not attached via a bhndb bus
560 * @retval NULL no parent device is attached via @p bus_class.
563 bhnd_find_bridge_root(device_t dev, devclass_t bus_class)
565 devclass_t bhndb_class;
568 KASSERT(device_get_devclass(device_get_parent(dev)) == bhnd_devclass,
569 ("%s not a bhnd device", device_get_nameunit(dev)));
571 bhndb_class = devclass_find("bhndb");
573 /* Walk the device tree until we hit a bridge */
575 while ((parent = device_get_parent(parent)) != NULL) {
576 if (device_get_devclass(parent) == bhndb_class)
584 /* Search for a parent attached to the expected bus class */
585 while ((parent = device_get_parent(parent)) != NULL) {
588 bus = device_get_parent(parent);
589 if (bus != NULL && device_get_devclass(bus) == bus_class)
598 * Find the first core in @p cores that matches @p desc.
600 * @param cores The table to search.
601 * @param num_cores The length of @p cores.
602 * @param desc A match descriptor.
604 * @retval bhnd_core_info if a matching core is found.
605 * @retval NULL if no matching core is found.
607 const struct bhnd_core_info *
608 bhnd_match_core(const struct bhnd_core_info *cores, u_int num_cores,
609 const struct bhnd_core_match *desc)
611 for (u_int i = 0; i < num_cores; i++) {
612 if (bhnd_core_matches(&cores[i], desc))
621 * Find the first core in @p cores with the given @p class.
623 * @param cores The table to search.
624 * @param num_cores The length of @p cores.
625 * @param desc A match descriptor.
627 * @retval bhnd_core_info if a matching core is found.
628 * @retval NULL if no matching core is found.
630 const struct bhnd_core_info *
631 bhnd_find_core(const struct bhnd_core_info *cores, u_int num_cores,
632 bhnd_devclass_t class)
634 struct bhnd_core_match md = {
635 BHND_MATCH_CORE_CLASS(class)
638 return bhnd_match_core(cores, num_cores, &md);
643 * Create an equality match descriptor for @p core.
645 * @param core The core info to be matched on.
646 * @param desc On return, will be populated with a match descriptor for @p core.
648 struct bhnd_core_match
649 bhnd_core_get_match_desc(const struct bhnd_core_info *core)
651 return ((struct bhnd_core_match) {
652 BHND_MATCH_CORE_VENDOR(core->vendor),
653 BHND_MATCH_CORE_ID(core->device),
654 BHND_MATCH_CORE_REV(HWREV_EQ(core->hwrev)),
655 BHND_MATCH_CORE_CLASS(bhnd_core_class(core)),
656 BHND_MATCH_CORE_IDX(core->core_idx),
657 BHND_MATCH_CORE_UNIT(core->unit)
663 * Return true if the @p lhs is equal to @p rhs
665 * @param lhs The first bhnd core descriptor to compare.
666 * @param rhs The second bhnd core descriptor to compare.
668 * @retval true if @p lhs is equal to @p rhs
669 * @retval false if @p lhs is not equal to @p rhs
672 bhnd_cores_equal(const struct bhnd_core_info *lhs,
673 const struct bhnd_core_info *rhs)
675 struct bhnd_core_match md;
677 /* Use an equality match descriptor to perform the comparison */
678 md = bhnd_core_get_match_desc(rhs);
679 return (bhnd_core_matches(lhs, &md));
683 * Return true if the @p core matches @p desc.
685 * @param core A bhnd core descriptor.
686 * @param desc A match descriptor to compare against @p core.
688 * @retval true if @p core matches @p match
689 * @retval false if @p core does not match @p match.
692 bhnd_core_matches(const struct bhnd_core_info *core,
693 const struct bhnd_core_match *desc)
695 if (desc->m.match.core_vendor && desc->core_vendor != core->vendor)
698 if (desc->m.match.core_id && desc->core_id != core->device)
701 if (desc->m.match.core_unit && desc->core_unit != core->unit)
704 if (desc->m.match.core_rev &&
705 !bhnd_hwrev_matches(core->hwrev, &desc->core_rev))
708 if (desc->m.match.core_idx && desc->core_idx != core->core_idx)
711 if (desc->m.match.core_class &&
712 desc->core_class != bhnd_core_class(core))
719 * Return true if the @p chip matches @p desc.
721 * @param chip A bhnd chip identifier.
722 * @param desc A match descriptor to compare against @p chip.
724 * @retval true if @p chip matches @p match
725 * @retval false if @p chip does not match @p match.
728 bhnd_chip_matches(const struct bhnd_chipid *chip,
729 const struct bhnd_chip_match *desc)
731 if (desc->m.match.chip_id && chip->chip_id != desc->chip_id)
734 if (desc->m.match.chip_pkg && chip->chip_pkg != desc->chip_pkg)
737 if (desc->m.match.chip_rev &&
738 !bhnd_hwrev_matches(chip->chip_rev, &desc->chip_rev))
745 * Return true if the @p board matches @p desc.
747 * @param board The bhnd board info.
748 * @param desc A match descriptor to compare against @p board.
750 * @retval true if @p chip matches @p match
751 * @retval false if @p chip does not match @p match.
754 bhnd_board_matches(const struct bhnd_board_info *board,
755 const struct bhnd_board_match *desc)
757 if (desc->m.match.board_srom_rev &&
758 !bhnd_hwrev_matches(board->board_srom_rev, &desc->board_srom_rev))
761 if (desc->m.match.board_vendor &&
762 board->board_vendor != desc->board_vendor)
765 if (desc->m.match.board_type && board->board_type != desc->board_type)
768 if (desc->m.match.board_rev &&
769 !bhnd_hwrev_matches(board->board_rev, &desc->board_rev))
776 * Return true if the @p hwrev matches @p desc.
778 * @param hwrev A bhnd hardware revision.
779 * @param desc A match descriptor to compare against @p core.
781 * @retval true if @p hwrev matches @p match
782 * @retval false if @p hwrev does not match @p match.
785 bhnd_hwrev_matches(uint16_t hwrev, const struct bhnd_hwrev_match *desc)
787 if (desc->start != BHND_HWREV_INVALID &&
791 if (desc->end != BHND_HWREV_INVALID &&
799 * Return true if the @p dev matches @p desc.
801 * @param dev A bhnd device.
802 * @param desc A match descriptor to compare against @p dev.
804 * @retval true if @p dev matches @p match
805 * @retval false if @p dev does not match @p match.
808 bhnd_device_matches(device_t dev, const struct bhnd_device_match *desc)
810 struct bhnd_core_info core;
811 const struct bhnd_chipid *chip;
812 struct bhnd_board_info board;
816 /* Construct individual match descriptors */
817 struct bhnd_core_match m_core = { _BHND_CORE_MATCH_COPY(desc) };
818 struct bhnd_chip_match m_chip = { _BHND_CHIP_MATCH_COPY(desc) };
819 struct bhnd_board_match m_board = { _BHND_BOARD_MATCH_COPY(desc) };
821 /* Fetch and match core info */
822 if (m_core.m.match_flags) {
823 /* Only applicable to bhnd-attached cores */
824 parent = device_get_parent(dev);
825 if (device_get_devclass(parent) != bhnd_devclass) {
826 device_printf(dev, "attempting to match core "
827 "attributes against non-core device\n");
831 core = bhnd_get_core_info(dev);
832 if (!bhnd_core_matches(&core, &m_core))
836 /* Fetch and match chip info */
837 if (m_chip.m.match_flags) {
838 chip = bhnd_get_chipid(dev);
840 if (!bhnd_chip_matches(chip, &m_chip))
844 /* Fetch and match board info.
846 * This is not available until after NVRAM is up; earlier device
847 * matches should not include board requirements */
848 if (m_board.m.match_flags) {
849 if ((error = bhnd_read_board_info(dev, &board))) {
850 device_printf(dev, "failed to read required board info "
851 "during device matching: %d\n", error);
855 if (!bhnd_board_matches(&board, &m_board))
864 * Search @p table for an entry matching @p dev.
866 * @param dev A bhnd device to match against @p table.
867 * @param table The device table to search.
868 * @param entry_size The @p table entry size, in bytes.
870 * @retval bhnd_device the first matching device, if any.
871 * @retval NULL if no matching device is found in @p table.
873 const struct bhnd_device *
874 bhnd_device_lookup(device_t dev, const struct bhnd_device *table,
877 const struct bhnd_device *entry;
878 device_t hostb, parent;
879 bhnd_attach_type attach_type;
882 parent = device_get_parent(dev);
883 hostb = bhnd_bus_find_hostb_device(parent);
884 attach_type = bhnd_get_attach_type(dev);
886 for (entry = table; !BHND_DEVICE_IS_END(entry); entry =
887 (const struct bhnd_device *) ((const char *) entry + entry_size))
889 /* match core info */
890 if (!bhnd_device_matches(dev, &entry->core))
893 /* match device flags */
894 dflags = entry->device_flags;
896 /* hostb implies BHND_ATTACH_ADAPTER requirement */
897 if (dflags & BHND_DF_HOSTB)
898 dflags |= BHND_DF_ADAPTER;
900 if (dflags & BHND_DF_ADAPTER)
901 if (attach_type != BHND_ATTACH_ADAPTER)
904 if (dflags & BHND_DF_HOSTB)
908 if (dflags & BHND_DF_SOC)
909 if (attach_type != BHND_ATTACH_NATIVE)
921 * Scan the device @p table for all quirk flags applicable to @p dev.
923 * @param dev A bhnd device to match against @p table.
924 * @param table The device table to search.
926 * @return returns all matching quirk flags.
929 bhnd_device_quirks(device_t dev, const struct bhnd_device *table,
932 const struct bhnd_device *dent;
933 const struct bhnd_device_quirk *qent, *qtable;
936 /* Locate the device entry */
937 if ((dent = bhnd_device_lookup(dev, table, entry_size)) == NULL)
940 /* Quirks table is optional */
941 qtable = dent->quirks_table;
945 /* Collect matching device quirk entries */
947 for (qent = qtable; !BHND_DEVICE_QUIRK_IS_END(qent); qent++) {
948 if (bhnd_device_matches(dev, &qent->desc))
949 quirks |= qent->quirks;
957 * Allocate bhnd(4) resources defined in @p rs from a parent bus.
959 * @param dev The device requesting ownership of the resources.
960 * @param rs A standard bus resource specification. This will be updated
961 * with the allocated resource's RIDs.
962 * @param res On success, the allocated bhnd resources.
965 * @retval non-zero if allocation of any non-RF_OPTIONAL resource fails,
966 * all allocated resources will be released and a regular
967 * unix error code will be returned.
970 bhnd_alloc_resources(device_t dev, struct resource_spec *rs,
971 struct bhnd_resource **res)
973 /* Initialize output array */
974 for (u_int i = 0; rs[i].type != -1; i++)
977 for (u_int i = 0; rs[i].type != -1; i++) {
978 res[i] = bhnd_alloc_resource_any(dev, rs[i].type, &rs[i].rid,
981 /* Clean up all allocations on failure */
982 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
983 bhnd_release_resources(dev, rs, res);
992 * Release bhnd(4) resources defined in @p rs from a parent bus.
994 * @param dev The device that owns the resources.
995 * @param rs A standard bus resource specification previously initialized
996 * by @p bhnd_alloc_resources.
997 * @param res The bhnd resources to be released.
1000 bhnd_release_resources(device_t dev, const struct resource_spec *rs,
1001 struct bhnd_resource **res)
1003 for (u_int i = 0; rs[i].type != -1; i++) {
1007 bhnd_release_resource(dev, rs[i].type, rs[i].rid, res[i]);
1013 * Parse the CHIPC_ID_* fields from the ChipCommon CHIPC_ID
1014 * register, returning its bhnd_chipid representation.
1016 * @param idreg The CHIPC_ID register value.
1017 * @param enum_addr The enumeration address to include in the result.
1020 * On early siba(4) devices, the ChipCommon core does not provide
1021 * a valid CHIPC_ID_NUMCORE field. On these ChipCommon revisions
1022 * (see CHIPC_NCORES_MIN_HWREV()), this function will parse and return
1023 * an invalid `ncores` value.
1026 bhnd_parse_chipid(uint32_t idreg, bhnd_addr_t enum_addr)
1028 struct bhnd_chipid result;
1030 /* Fetch the basic chip info */
1031 result.chip_id = CHIPC_GET_BITS(idreg, CHIPC_ID_CHIP);
1032 result.chip_pkg = CHIPC_GET_BITS(idreg, CHIPC_ID_PKG);
1033 result.chip_rev = CHIPC_GET_BITS(idreg, CHIPC_ID_REV);
1034 result.chip_type = CHIPC_GET_BITS(idreg, CHIPC_ID_BUS);
1035 result.ncores = CHIPC_GET_BITS(idreg, CHIPC_ID_NUMCORE);
1037 result.enum_addr = enum_addr;
1044 * Determine the correct core count for a chip identification value that
1045 * may contain an invalid core count.
1047 * On some early siba(4) devices (see CHIPC_NCORES_MIN_HWREV()), the ChipCommon
1048 * core does not provide a valid CHIPC_ID_NUMCORE field.
1050 * @param cid The chip identification to be queried.
1051 * @param chipc_hwrev The hardware revision of the ChipCommon core from which
1052 * @p cid was parsed.
1053 * @param[out] ncores On success, will be set to the correct core count.
1055 * @retval 0 If the core count is already correct, or was mapped to a
1057 * @retval EINVAL If the core count is incorrect, but the chip was not
1061 bhnd_chipid_fixed_ncores(const struct bhnd_chipid *cid, uint16_t chipc_hwrev,
1064 /* bcma(4), and most siba(4) devices */
1065 if (CHIPC_NCORES_MIN_HWREV(chipc_hwrev)) {
1066 *ncores = cid->ncores;
1070 /* broken siba(4) chipsets */
1071 switch (cid->chip_id) {
1072 case BHND_CHIPID_BCM4306:
1075 case BHND_CHIPID_BCM4704:
1078 case BHND_CHIPID_BCM5365:
1080 * BCM5365 does support ID_NUMCORE in at least
1081 * some of its revisions, but for unknown
1082 * reasons, Broadcom's drivers always exclude
1083 * the ChipCommon revision (0x5) used by BCM5365
1084 * from the set of revisions supporting
1085 * ID_NUMCORE, and instead supply a fixed value.
1087 * Presumably, at least some of these devices
1088 * shipped with a broken ID_NUMCORE value.
1100 * Allocate the resource defined by @p rs via @p dev, use it
1101 * to read the ChipCommon ID register relative to @p chipc_offset,
1102 * then release the resource.
1104 * @param dev The device owning @p rs.
1105 * @param rs A resource spec that encompasses the ChipCommon register block.
1106 * @param chipc_offset The offset of the ChipCommon registers within @p rs.
1107 * @param[out] result the chip identification data.
1110 * @retval non-zero if the ChipCommon identification data could not be read.
1113 bhnd_read_chipid(device_t dev, struct resource_spec *rs,
1114 bus_size_t chipc_offset, struct bhnd_chipid *result)
1116 struct resource *res;
1117 bhnd_addr_t enum_addr;
1120 int error, rid, rtype;
1126 /* Allocate the ChipCommon window resource and fetch the chipid data */
1127 res = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
1130 "failed to allocate bhnd chipc resource\n");
1134 /* Fetch the basic chip info */
1135 reg = bus_read_4(res, chipc_offset + CHIPC_ID);
1136 chip_type = CHIPC_GET_BITS(reg, CHIPC_ID_BUS);
1138 /* Fetch the EROMPTR */
1139 if (BHND_CHIPTYPE_HAS_EROM(chip_type)) {
1140 enum_addr = bus_read_4(res, chipc_offset + CHIPC_EROMPTR);
1141 } else if (chip_type == BHND_CHIPTYPE_SIBA) {
1142 /* siba(4) uses the ChipCommon base address as the enumeration
1144 enum_addr = BHND_DEFAULT_CHIPC_ADDR;
1146 device_printf(dev, "unknown chip type %hhu\n", chip_type);
1151 *result = bhnd_parse_chipid(reg, enum_addr);
1153 /* Fix the core count on early siba(4) devices */
1154 if (chip_type == BHND_CHIPTYPE_SIBA) {
1156 uint16_t chipc_hwrev;
1159 * We need the ChipCommon revision to determine whether
1160 * the ncore field is valid.
1162 * We can safely assume the siba IDHIGH register is mapped
1163 * within the chipc register block.
1165 idh = bus_read_4(res, SB0_REG_ABS(SIBA_CFG0_IDHIGH));
1166 chipc_hwrev = SIBA_IDH_CORE_REV(idh);
1168 error = bhnd_chipid_fixed_ncores(result, chipc_hwrev,
1176 bus_release_resource(dev, rtype, rid, res);
1181 * Read an NVRAM variable's NUL-terminated string value.
1183 * @param dev A bhnd bus child device.
1184 * @param name The NVRAM variable name.
1185 * @param[out] buf A buffer large enough to hold @p len bytes. On
1186 * success, the NUL-terminated string value will be
1187 * written to this buffer. This argment may be NULL if
1188 * the value is not desired.
1189 * @param len The maximum capacity of @p buf.
1190 * @param[out] rlen On success, will be set to the actual size of
1191 * the requested value (including NUL termination). This
1192 * argment may be NULL if the size is not desired.
1195 * @retval ENOENT The requested variable was not found.
1196 * @retval ENODEV No valid NVRAM source could be found.
1197 * @retval ENOMEM If @p buf is non-NULL and a buffer of @p len is too
1198 * small to hold the requested value.
1199 * @retval EFTYPE If the variable data cannot be coerced to a valid
1200 * string representation.
1201 * @retval ERANGE If value coercion would overflow @p type.
1202 * @retval non-zero If reading @p name otherwise fails, a regular unix
1203 * error code will be returned.
1206 bhnd_nvram_getvar_str(device_t dev, const char *name, char *buf, size_t len,
1213 error = bhnd_nvram_getvar(dev, name, buf, &larg,
1214 BHND_NVRAM_TYPE_STRING);
1222 * Read an NVRAM variable's unsigned integer value.
1224 * @param dev A bhnd bus child device.
1225 * @param name The NVRAM variable name.
1226 * @param[out] value On success, the requested value will be written
1228 * @param width The output integer type width (1, 2, or
1232 * @retval ENOENT The requested variable was not found.
1233 * @retval ENODEV No valid NVRAM source could be found.
1234 * @retval EFTYPE If the variable data cannot be coerced to a
1235 * a valid unsigned integer representation.
1236 * @retval ERANGE If value coercion would overflow (or underflow) an
1237 * unsigned representation of the given @p width.
1238 * @retval non-zero If reading @p name otherwise fails, a regular unix
1239 * error code will be returned.
1242 bhnd_nvram_getvar_uint(device_t dev, const char *name, void *value, int width)
1244 bhnd_nvram_type type;
1249 type = BHND_NVRAM_TYPE_UINT8;
1252 type = BHND_NVRAM_TYPE_UINT16;
1255 type = BHND_NVRAM_TYPE_UINT32;
1258 device_printf(dev, "unsupported NVRAM integer width: %d\n",
1264 return (bhnd_nvram_getvar(dev, name, value, &len, type));
1268 * Read an NVRAM variable's unsigned 8-bit integer value.
1270 * @param dev A bhnd bus child device.
1271 * @param name The NVRAM variable name.
1272 * @param[out] value On success, the requested value will be written
1276 * @retval ENOENT The requested variable was not found.
1277 * @retval ENODEV No valid NVRAM source could be found.
1278 * @retval EFTYPE If the variable data cannot be coerced to a
1279 * a valid unsigned integer representation.
1280 * @retval ERANGE If value coercion would overflow (or underflow) uint8_t.
1281 * @retval non-zero If reading @p name otherwise fails, a regular unix
1282 * error code will be returned.
1285 bhnd_nvram_getvar_uint8(device_t dev, const char *name, uint8_t *value)
1287 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1291 * Read an NVRAM variable's unsigned 16-bit integer value.
1293 * @param dev A bhnd bus child device.
1294 * @param name The NVRAM variable name.
1295 * @param[out] value On success, the requested value will be written
1299 * @retval ENOENT The requested variable was not found.
1300 * @retval ENODEV No valid NVRAM source could be found.
1301 * @retval EFTYPE If the variable data cannot be coerced to a
1302 * a valid unsigned integer representation.
1303 * @retval ERANGE If value coercion would overflow (or underflow)
1305 * @retval non-zero If reading @p name otherwise fails, a regular unix
1306 * error code will be returned.
1309 bhnd_nvram_getvar_uint16(device_t dev, const char *name, uint16_t *value)
1311 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1315 * Read an NVRAM variable's unsigned 32-bit integer value.
1317 * @param dev A bhnd bus child device.
1318 * @param name The NVRAM variable name.
1319 * @param[out] value On success, the requested value will be written
1323 * @retval ENOENT The requested variable was not found.
1324 * @retval ENODEV No valid NVRAM source could be found.
1325 * @retval EFTYPE If the variable data cannot be coerced to a
1326 * a valid unsigned integer representation.
1327 * @retval ERANGE If value coercion would overflow (or underflow)
1329 * @retval non-zero If reading @p name otherwise fails, a regular unix
1330 * error code will be returned.
1333 bhnd_nvram_getvar_uint32(device_t dev, const char *name, uint32_t *value)
1335 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1339 * Read an NVRAM variable's signed integer value.
1341 * @param dev A bhnd bus child device.
1342 * @param name The NVRAM variable name.
1343 * @param[out] value On success, the requested value will be written
1345 * @param width The output integer type width (1, 2, or
1349 * @retval ENOENT The requested variable was not found.
1350 * @retval ENODEV No valid NVRAM source could be found.
1351 * @retval EFTYPE If the variable data cannot be coerced to a
1352 * a valid integer representation.
1353 * @retval ERANGE If value coercion would overflow (or underflow) an
1354 * signed representation of the given @p width.
1355 * @retval non-zero If reading @p name otherwise fails, a regular unix
1356 * error code will be returned.
1359 bhnd_nvram_getvar_int(device_t dev, const char *name, void *value, int width)
1361 bhnd_nvram_type type;
1366 type = BHND_NVRAM_TYPE_INT8;
1369 type = BHND_NVRAM_TYPE_INT16;
1372 type = BHND_NVRAM_TYPE_INT32;
1375 device_printf(dev, "unsupported NVRAM integer width: %d\n",
1381 return (bhnd_nvram_getvar(dev, name, value, &len, type));
1385 * Read an NVRAM variable's signed 8-bit integer value.
1387 * @param dev A bhnd bus child device.
1388 * @param name The NVRAM variable name.
1389 * @param[out] value On success, the requested value will be written
1393 * @retval ENOENT The requested variable was not found.
1394 * @retval ENODEV No valid NVRAM source could be found.
1395 * @retval EFTYPE If the variable data cannot be coerced to a
1396 * a valid integer representation.
1397 * @retval ERANGE If value coercion would overflow (or underflow) int8_t.
1398 * @retval non-zero If reading @p name otherwise fails, a regular unix
1399 * error code will be returned.
1402 bhnd_nvram_getvar_int8(device_t dev, const char *name, int8_t *value)
1404 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1408 * Read an NVRAM variable's signed 16-bit integer value.
1410 * @param dev A bhnd bus child device.
1411 * @param name The NVRAM variable name.
1412 * @param[out] value On success, the requested value will be written
1416 * @retval ENOENT The requested variable was not found.
1417 * @retval ENODEV No valid NVRAM source could be found.
1418 * @retval EFTYPE If the variable data cannot be coerced to a
1419 * a valid integer representation.
1420 * @retval ERANGE If value coercion would overflow (or underflow)
1422 * @retval non-zero If reading @p name otherwise fails, a regular unix
1423 * error code will be returned.
1426 bhnd_nvram_getvar_int16(device_t dev, const char *name, int16_t *value)
1428 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1432 * Read an NVRAM variable's signed 32-bit integer value.
1434 * @param dev A bhnd bus child device.
1435 * @param name The NVRAM variable name.
1436 * @param[out] value On success, the requested value will be written
1440 * @retval ENOENT The requested variable was not found.
1441 * @retval ENODEV No valid NVRAM source could be found.
1442 * @retval EFTYPE If the variable data cannot be coerced to a
1443 * a valid integer representation.
1444 * @retval ERANGE If value coercion would overflow (or underflow)
1446 * @retval non-zero If reading @p name otherwise fails, a regular unix
1447 * error code will be returned.
1450 bhnd_nvram_getvar_int32(device_t dev, const char *name, int32_t *value)
1452 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1457 * Read an NVRAM variable's array value.
1459 * @param dev A bhnd bus child device.
1460 * @param name The NVRAM variable name.
1461 * @param[out] buf A buffer large enough to hold @p size bytes.
1462 * On success, the requested value will be written
1464 * @param[in,out] size The required number of bytes to write to
1466 * @param type The desired array element data representation.
1469 * @retval ENOENT The requested variable was not found.
1470 * @retval ENODEV No valid NVRAM source could be found.
1471 * @retval ENXIO If less than @p size bytes are available.
1472 * @retval ENOMEM If a buffer of @p size is too small to hold the
1474 * @retval EFTYPE If the variable data cannot be coerced to a
1475 * a valid instance of @p type.
1476 * @retval ERANGE If value coercion would overflow (or underflow) a
1477 * representation of @p type.
1478 * @retval non-zero If reading @p name otherwise fails, a regular unix
1479 * error code will be returned.
1482 bhnd_nvram_getvar_array(device_t dev, const char *name, void *buf, size_t size,
1483 bhnd_nvram_type type)
1490 if ((error = bhnd_nvram_getvar(dev, name, buf, &nbytes, type)))
1493 /* Verify that the expected number of bytes were fetched */
1501 * Initialize a service provider registry.
1503 * @param bsr The service registry to initialize.
1506 * @retval non-zero if an error occurs initializing the service registry,
1507 * a regular unix error code will be returned.
1511 bhnd_service_registry_init(struct bhnd_service_registry *bsr)
1513 STAILQ_INIT(&bsr->entries);
1514 mtx_init(&bsr->lock, "bhnd_service_registry lock", NULL, MTX_DEF);
1520 * Release all resources held by @p bsr.
1522 * @param bsr A service registry instance previously successfully
1523 * initialized via bhnd_service_registry_init().
1526 * @retval EBUSY if active references to service providers registered
1527 * with @p bsr exist.
1530 bhnd_service_registry_fini(struct bhnd_service_registry *bsr)
1532 struct bhnd_service_entry *entry, *enext;
1534 /* Remove everthing we can */
1535 mtx_lock(&bsr->lock);
1536 STAILQ_FOREACH_SAFE(entry, &bsr->entries, link, enext) {
1537 if (entry->refs > 0)
1540 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry, link);
1541 free(entry, M_BHND);
1544 if (!STAILQ_EMPTY(&bsr->entries)) {
1545 mtx_unlock(&bsr->lock);
1548 mtx_unlock(&bsr->lock);
1550 mtx_destroy(&bsr->lock);
1555 * Register a @p provider for the given @p service.
1557 * @param bsr Service registry to be modified.
1558 * @param provider Service provider to register.
1559 * @param service Service for which @p provider will be registered.
1560 * @param flags Service provider flags (see BHND_SPF_*).
1563 * @retval EEXIST if an entry for @p service already exists.
1564 * @retval EINVAL if @p service is BHND_SERVICE_ANY.
1565 * @retval non-zero if registering @p provider otherwise fails, a regular
1566 * unix error code will be returned.
1569 bhnd_service_registry_add(struct bhnd_service_registry *bsr, device_t provider,
1570 bhnd_service_t service, uint32_t flags)
1572 struct bhnd_service_entry *entry;
1574 if (service == BHND_SERVICE_ANY)
1577 mtx_lock(&bsr->lock);
1579 /* Is a service provider already registered? */
1580 STAILQ_FOREACH(entry, &bsr->entries, link) {
1581 if (entry->service == service) {
1582 mtx_unlock(&bsr->lock);
1587 /* Initialize and insert our new entry */
1588 entry = malloc(sizeof(*entry), M_BHND, M_NOWAIT);
1589 if (entry == NULL) {
1590 mtx_unlock(&bsr->lock);
1594 entry->provider = provider;
1595 entry->service = service;
1596 entry->flags = flags;
1597 refcount_init(&entry->refs, 0);
1599 STAILQ_INSERT_HEAD(&bsr->entries, entry, link);
1601 mtx_unlock(&bsr->lock);
1606 * Free an unreferenced registry entry.
1608 * @param entry The entry to be deallocated.
1611 bhnd_service_registry_free_entry(struct bhnd_service_entry *entry)
1613 KASSERT(entry->refs == 0, ("provider has active references"));
1614 free(entry, M_BHND);
1618 * Attempt to remove the @p service provider registration for @p provider.
1620 * @param bsr The service registry to be modified.
1621 * @param provider The service provider to be deregistered.
1622 * @param service The service for which @p provider will be deregistered,
1623 * or BHND_SERVICE_ANY to remove all service
1624 * registrations for @p provider.
1627 * @retval EBUSY if active references to @p provider exist; @see
1628 * bhnd_service_registry_retain() and
1629 * bhnd_service_registry_release().
1632 bhnd_service_registry_remove(struct bhnd_service_registry *bsr,
1633 device_t provider, bhnd_service_t service)
1635 struct bhnd_service_entry *entry, *enext;
1637 mtx_lock(&bsr->lock);
1639 #define BHND_PROV_MATCH(_e) \
1640 ((_e)->provider == provider && \
1641 (service == BHND_SERVICE_ANY || (_e)->service == service))
1643 /* Validate matching provider entries before making any
1645 STAILQ_FOREACH(entry, &bsr->entries, link) {
1646 /* Skip non-matching entries */
1647 if (!BHND_PROV_MATCH(entry))
1650 /* Entry is in use? */
1651 if (entry->refs > 0) {
1652 mtx_unlock(&bsr->lock);
1657 /* We can now safely remove matching entries */
1658 STAILQ_FOREACH_SAFE(entry, &bsr->entries, link, enext) {
1659 /* Skip non-matching entries */
1660 if (!BHND_PROV_MATCH(entry))
1663 /* Remove from list */
1664 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry, link);
1666 /* Free provider entry */
1667 bhnd_service_registry_free_entry(entry);
1669 #undef BHND_PROV_MATCH
1671 mtx_unlock(&bsr->lock);
1676 * Retain and return a reference to a registered @p service provider, if any.
1678 * @param bsr The service registry to be queried.
1679 * @param service The service for which a provider should be returned.
1681 * On success, the caller assumes ownership the returned provider, and
1682 * is responsible for releasing this reference via
1683 * bhnd_service_registry_release().
1685 * @retval device_t success
1686 * @retval NULL if no provider is registered for @p service.
1689 bhnd_service_registry_retain(struct bhnd_service_registry *bsr,
1690 bhnd_service_t service)
1692 struct bhnd_service_entry *entry;
1694 mtx_lock(&bsr->lock);
1695 STAILQ_FOREACH(entry, &bsr->entries, link) {
1696 if (entry->service != service)
1699 /* With a live refcount, entry is gauranteed to remain alive
1700 * after we release our lock */
1701 refcount_acquire(&entry->refs);
1703 mtx_unlock(&bsr->lock);
1704 return (entry->provider);
1706 mtx_unlock(&bsr->lock);
1713 * Release a reference to a service provider previously returned by
1714 * bhnd_service_registry_retain().
1716 * If this is the last reference to an inherited service provider registration
1717 * (@see BHND_SPF_INHERITED), the registration will also be removed, and
1718 * true will be returned.
1720 * @param bsr The service registry from which @p provider
1722 * @param provider The provider to be released.
1723 * @param service The service for which @p provider was previously
1725 * @retval true The inherited service provider registration was removed;
1726 * the caller should release its own reference to the
1728 * @retval false The service provider was not inherited, or active
1729 * references to the provider remain.
1732 bhnd_service_registry_release(struct bhnd_service_registry *bsr,
1733 device_t provider, bhnd_service_t service)
1735 struct bhnd_service_entry *entry;
1737 /* Exclusive lock, as we need to prevent any new references to the
1738 * entry from being taken if it's to be removed */
1739 mtx_lock(&bsr->lock);
1740 STAILQ_FOREACH(entry, &bsr->entries, link) {
1743 if (entry->provider != provider)
1746 if (entry->service != service)
1749 if (refcount_release(&entry->refs) &&
1750 (entry->flags & BHND_SPF_INHERITED))
1752 /* If an inherited entry is no longer actively
1753 * referenced, remove the local registration and inform
1755 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry,
1757 bhnd_service_registry_free_entry(entry);
1763 mtx_unlock(&bsr->lock);
1767 /* Caller owns a reference, but no such provider is registered? */
1768 panic("invalid service provider reference");
1772 * Using the bhnd(4) bus-level core information and a custom core name,
1773 * populate @p dev's device description.
1775 * @param dev A bhnd-bus attached device.
1776 * @param dev_name The core's name (e.g. "SDIO Device Core")
1779 bhnd_set_custom_core_desc(device_t dev, const char *dev_name)
1781 const char *vendor_name;
1784 vendor_name = bhnd_get_vendor_name(dev);
1785 asprintf(&desc, M_BHND, "%s %s, rev %hhu", vendor_name, dev_name,
1786 bhnd_get_hwrev(dev));
1789 device_set_desc_copy(dev, desc);
1792 device_set_desc(dev, dev_name);
1797 * Using the bhnd(4) bus-level core information, populate @p dev's device
1800 * @param dev A bhnd-bus attached device.
1803 bhnd_set_default_core_desc(device_t dev)
1805 bhnd_set_custom_core_desc(dev, bhnd_get_device_name(dev));
1810 * Using the bhnd @p chip_id, populate the bhnd(4) bus @p dev's device
1813 * @param dev A bhnd-bus attached device.
1816 bhnd_set_default_bus_desc(device_t dev, const struct bhnd_chipid *chip_id)
1818 const char *bus_name;
1820 char chip_name[BHND_CHIPID_MAX_NAMELEN];
1822 /* Determine chip type's bus name */
1823 switch (chip_id->chip_type) {
1824 case BHND_CHIPTYPE_SIBA:
1825 bus_name = "SIBA bus";
1827 case BHND_CHIPTYPE_BCMA:
1828 case BHND_CHIPTYPE_BCMA_ALT:
1829 bus_name = "BCMA bus";
1831 case BHND_CHIPTYPE_UBUS:
1832 bus_name = "UBUS bus";
1835 bus_name = "Unknown Type";
1839 /* Format chip name */
1840 bhnd_format_chip_id(chip_name, sizeof(chip_name),
1843 /* Format and set device description */
1844 asprintf(&desc, M_BHND, "%s %s", chip_name, bus_name);
1846 device_set_desc_copy(dev, desc);
1849 device_set_desc(dev, bus_name);
1855 * Helper function for implementing BHND_BUS_REGISTER_PROVIDER().
1857 * This implementation delegates the request to the BHND_BUS_REGISTER_PROVIDER()
1858 * method on the parent of @p dev. If no parent exists, the implementation
1859 * will return an error.
1862 bhnd_bus_generic_register_provider(device_t dev, device_t child,
1863 device_t provider, bhnd_service_t service)
1865 device_t parent = device_get_parent(dev);
1867 if (parent != NULL) {
1868 return (BHND_BUS_REGISTER_PROVIDER(parent, child,
1869 provider, service));
1876 * Helper function for implementing BHND_BUS_DEREGISTER_PROVIDER().
1878 * This implementation delegates the request to the
1879 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
1880 * exists, the implementation will panic.
1883 bhnd_bus_generic_deregister_provider(device_t dev, device_t child,
1884 device_t provider, bhnd_service_t service)
1886 device_t parent = device_get_parent(dev);
1888 if (parent != NULL) {
1889 return (BHND_BUS_DEREGISTER_PROVIDER(parent, child,
1890 provider, service));
1893 panic("missing BHND_BUS_DEREGISTER_PROVIDER()");
1897 * Helper function for implementing BHND_BUS_RETAIN_PROVIDER().
1899 * This implementation delegates the request to the
1900 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
1901 * exists, the implementation will return NULL.
1904 bhnd_bus_generic_retain_provider(device_t dev, device_t child,
1905 bhnd_service_t service)
1907 device_t parent = device_get_parent(dev);
1909 if (parent != NULL) {
1910 return (BHND_BUS_RETAIN_PROVIDER(parent, child,
1918 * Helper function for implementing BHND_BUS_RELEASE_PROVIDER().
1920 * This implementation delegates the request to the
1921 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
1922 * exists, the implementation will panic.
1925 bhnd_bus_generic_release_provider(device_t dev, device_t child,
1926 device_t provider, bhnd_service_t service)
1928 device_t parent = device_get_parent(dev);
1930 if (parent != NULL) {
1931 return (BHND_BUS_RELEASE_PROVIDER(parent, child,
1932 provider, service));
1935 panic("missing BHND_BUS_RELEASE_PROVIDER()");
1939 * Helper function for implementing BHND_BUS_REGISTER_PROVIDER().
1941 * This implementation uses the bhnd_service_registry_add() function to
1942 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
1943 * a suitable service registry to edit.
1946 bhnd_bus_generic_sr_register_provider(device_t dev, device_t child,
1947 device_t provider, bhnd_service_t service)
1949 struct bhnd_service_registry *bsr;
1951 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
1953 KASSERT(bsr != NULL, ("NULL service registry"));
1955 return (bhnd_service_registry_add(bsr, provider, service, 0));
1959 * Helper function for implementing BHND_BUS_DEREGISTER_PROVIDER().
1961 * This implementation uses the bhnd_service_registry_remove() function to
1962 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
1963 * a suitable service registry to edit.
1966 bhnd_bus_generic_sr_deregister_provider(device_t dev, device_t child,
1967 device_t provider, bhnd_service_t service)
1969 struct bhnd_service_registry *bsr;
1971 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
1973 KASSERT(bsr != NULL, ("NULL service registry"));
1975 return (bhnd_service_registry_remove(bsr, provider, service));
1979 * Helper function for implementing BHND_BUS_RETAIN_PROVIDER().
1981 * This implementation uses the bhnd_service_registry_retain() function to
1982 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
1983 * a suitable service registry.
1985 * If a local provider for the service is not available, and a parent device is
1986 * available, this implementation will attempt to fetch and locally register
1987 * a service provider reference from the parent of @p dev.
1990 bhnd_bus_generic_sr_retain_provider(device_t dev, device_t child,
1991 bhnd_service_t service)
1993 struct bhnd_service_registry *bsr;
1994 device_t parent, provider;
1997 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
1998 KASSERT(bsr != NULL, ("NULL service registry"));
2001 * Attempt to fetch a service provider reference from either the local
2002 * service registry, or if not found, from our parent.
2004 * If we fetch a provider from our parent, we register the provider
2005 * with the local service registry to prevent conflicting local
2006 * registrations from being added.
2009 /* Check the local service registry first */
2010 provider = bhnd_service_registry_retain(bsr, service);
2011 if (provider != NULL)
2014 /* Otherwise, try to delegate to our parent (if any) */
2015 if ((parent = device_get_parent(dev)) == NULL)
2018 provider = BHND_BUS_RETAIN_PROVIDER(parent, dev, service);
2019 if (provider == NULL)
2022 /* Register the inherited service registration with the local
2024 error = bhnd_service_registry_add(bsr, provider, service,
2025 BHND_SPF_INHERITED);
2027 BHND_BUS_RELEASE_PROVIDER(parent, dev, provider,
2029 if (error == EEXIST) {
2030 /* A valid service provider was registered
2031 * concurrently; retry fetching from the local
2036 device_printf(dev, "failed to register service "
2037 "provider: %d\n", error);
2044 * Helper function for implementing BHND_BUS_RELEASE_PROVIDER().
2046 * This implementation uses the bhnd_service_registry_release() function to
2047 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
2048 * a suitable service registry.
2051 bhnd_bus_generic_sr_release_provider(device_t dev, device_t child,
2052 device_t provider, bhnd_service_t service)
2054 struct bhnd_service_registry *bsr;
2056 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
2057 KASSERT(bsr != NULL, ("NULL service registry"));
2059 /* Release the provider reference; if the refcount hits zero on an
2060 * inherited reference, true will be returned, and we need to drop
2061 * our own bus reference to the provider */
2062 if (!bhnd_service_registry_release(bsr, provider, service))
2065 /* Drop our reference to the borrowed provider */
2066 BHND_BUS_RELEASE_PROVIDER(device_get_parent(dev), dev, provider,
2071 * Helper function for implementing BHND_BUS_IS_HW_DISABLED().
2073 * If a parent device is available, this implementation delegates the
2074 * request to the BHND_BUS_IS_HW_DISABLED() method on the parent of @p dev.
2076 * If no parent device is available (i.e. on a the bus root), the hardware
2077 * is assumed to be usable and false is returned.
2080 bhnd_bus_generic_is_hw_disabled(device_t dev, device_t child)
2082 if (device_get_parent(dev) != NULL)
2083 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child));
2089 * Helper function for implementing BHND_BUS_GET_CHIPID().
2091 * This implementation delegates the request to the BHND_BUS_GET_CHIPID()
2092 * method on the parent of @p dev. If no parent exists, the implementation
2095 const struct bhnd_chipid *
2096 bhnd_bus_generic_get_chipid(device_t dev, device_t child)
2098 if (device_get_parent(dev) != NULL)
2099 return (BHND_BUS_GET_CHIPID(device_get_parent(dev), child));
2101 panic("missing BHND_BUS_GET_CHIPID()");
2104 /* nvram board_info population macros for bhnd_bus_generic_read_board_info() */
2105 #define BHND_GV(_dest, _name) \
2106 bhnd_nvram_getvar_uint(child, BHND_NVAR_ ## _name, &_dest, \
2109 #define REQ_BHND_GV(_dest, _name) do { \
2110 if ((error = BHND_GV(_dest, _name))) { \
2111 device_printf(dev, \
2112 "error reading " __STRING(_name) ": %d\n", error); \
2117 #define OPT_BHND_GV(_dest, _name, _default) do { \
2118 if ((error = BHND_GV(_dest, _name))) { \
2119 if (error != ENOENT) { \
2120 device_printf(dev, \
2122 __STRING(_name) ": %d\n", error); \
2130 * Helper function for implementing BHND_BUS_READ_BOARDINFO().
2132 * This implementation populates @p info with information from NVRAM,
2133 * defaulting board_vendor and board_type fields to 0 if the
2134 * requested variables cannot be found.
2136 * This behavior is correct for most SoCs, but must be overridden on
2137 * bridged (PCI, PCMCIA, etc) devices to produce a complete bhnd_board_info
2141 bhnd_bus_generic_read_board_info(device_t dev, device_t child,
2142 struct bhnd_board_info *info)
2146 OPT_BHND_GV(info->board_vendor, BOARDVENDOR, 0);
2147 OPT_BHND_GV(info->board_type, BOARDTYPE, 0); /* srom >= 2 */
2148 REQ_BHND_GV(info->board_rev, BOARDREV);
2149 OPT_BHND_GV(info->board_srom_rev,SROMREV, 0); /* missing in
2152 REQ_BHND_GV(info->board_flags, BOARDFLAGS);
2153 OPT_BHND_GV(info->board_flags2, BOARDFLAGS2, 0); /* srom >= 4 */
2154 OPT_BHND_GV(info->board_flags3, BOARDFLAGS3, 0); /* srom >= 11 */
2164 * Helper function for implementing BHND_BUS_GET_NVRAM_VAR().
2166 * This implementation searches @p dev for a usable NVRAM child device.
2168 * If no usable child device is found on @p dev, the request is delegated to
2169 * the BHND_BUS_GET_NVRAM_VAR() method on the parent of @p dev.
2172 bhnd_bus_generic_get_nvram_var(device_t dev, device_t child, const char *name,
2173 void *buf, size_t *size, bhnd_nvram_type type)
2178 /* Make sure we're holding Giant for newbus */
2181 /* Look for a directly-attached NVRAM child */
2182 if ((nvram = device_find_child(dev, "bhnd_nvram", -1)) != NULL)
2183 return BHND_NVRAM_GETVAR(nvram, name, buf, size, type);
2185 /* Try to delegate to parent */
2186 if ((parent = device_get_parent(dev)) == NULL)
2189 return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), child,
2190 name, buf, size, type));
2194 * Helper function for implementing BHND_BUS_ALLOC_RESOURCE().
2196 * This implementation of BHND_BUS_ALLOC_RESOURCE() delegates allocation
2197 * of the underlying resource to BUS_ALLOC_RESOURCE(), and activation
2198 * to @p dev's BHND_BUS_ACTIVATE_RESOURCE().
2200 struct bhnd_resource *
2201 bhnd_bus_generic_alloc_resource(device_t dev, device_t child, int type,
2202 int *rid, rman_res_t start, rman_res_t end, rman_res_t count,
2205 struct bhnd_resource *br;
2206 struct resource *res;
2212 /* Allocate the real bus resource (without activating it) */
2213 res = BUS_ALLOC_RESOURCE(dev, child, type, rid, start, end, count,
2214 (flags & ~RF_ACTIVE));
2218 /* Allocate our bhnd resource wrapper. */
2219 br = malloc(sizeof(struct bhnd_resource), M_BHND, M_NOWAIT);
2226 /* Attempt activation */
2227 if (flags & RF_ACTIVE) {
2228 error = BHND_BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, br);
2237 BUS_RELEASE_RESOURCE(dev, child, type, *rid, res);
2244 * Helper function for implementing BHND_BUS_RELEASE_RESOURCE().
2246 * This implementation of BHND_BUS_RELEASE_RESOURCE() delegates release of
2247 * the backing resource to BUS_RELEASE_RESOURCE().
2250 bhnd_bus_generic_release_resource(device_t dev, device_t child, int type,
2251 int rid, struct bhnd_resource *r)
2255 if ((error = BUS_RELEASE_RESOURCE(dev, child, type, rid, r->res)))
2264 * Helper function for implementing BHND_BUS_ACTIVATE_RESOURCE().
2266 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() first calls the
2267 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
2269 * If this fails, and if @p dev is the direct parent of @p child, standard
2270 * resource activation is attempted via bus_activate_resource(). This enables
2271 * direct use of the bhnd(4) resource APIs on devices that may not be attached
2272 * to a parent bhnd bus or bridge.
2275 bhnd_bus_generic_activate_resource(device_t dev, device_t child, int type,
2276 int rid, struct bhnd_resource *r)
2281 passthrough = (device_get_parent(child) != dev);
2283 /* Try to delegate to the parent */
2284 if (device_get_parent(dev) != NULL) {
2285 error = BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev),
2286 child, type, rid, r);
2291 /* If bhnd(4) activation has failed and we're the child's direct
2292 * parent, try falling back on standard resource activation.
2294 if (error && !passthrough) {
2295 error = bus_activate_resource(child, type, rid, r->res);
2304 * Helper function for implementing BHND_BUS_DEACTIVATE_RESOURCE().
2306 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the
2307 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
2310 bhnd_bus_generic_deactivate_resource(device_t dev, device_t child,
2311 int type, int rid, struct bhnd_resource *r)
2313 if (device_get_parent(dev) != NULL)
2314 return (BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev),
2315 child, type, rid, r));