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" },
174 static const struct bhnd_device_quirk bhnd_chipc_clkctl_quirks[];
175 static const struct bhnd_device_quirk bhnd_pcmcia_clkctl_quirks[];
178 * Device table entries for core-specific CLKCTL quirk lookup.
180 static const struct bhnd_device bhnd_clkctl_devices[] = {
181 BHND_DEVICE(BCM, CC, NULL, bhnd_chipc_clkctl_quirks),
182 BHND_DEVICE(BCM, PCMCIA, NULL, bhnd_pcmcia_clkctl_quirks),
186 /** ChipCommon CLKCTL quirks */
187 static const struct bhnd_device_quirk bhnd_chipc_clkctl_quirks[] = {
188 /* HTAVAIL/ALPAVAIL are bitswapped in chipc's CLKCTL */
189 BHND_CHIP_QUIRK(4328, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),
190 BHND_CHIP_QUIRK(5354, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),
191 BHND_DEVICE_QUIRK_END
194 /** PCMCIA CLKCTL quirks */
195 static const struct bhnd_device_quirk bhnd_pcmcia_clkctl_quirks[] = {
196 /* HTAVAIL/ALPAVAIL are bitswapped in pcmcia's CLKCTL */
197 BHND_CHIP_QUIRK(4328, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),
198 BHND_CHIP_QUIRK(5354, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),
199 BHND_DEVICE_QUIRK_END
203 * Return the name for a given JEP106 manufacturer ID.
205 * @param vendor A JEP106 Manufacturer ID, including the non-standard ARM 4-bit
206 * JEP106 continuation code.
209 bhnd_vendor_name(uint16_t vendor)
216 case BHND_MFGID_MIPS:
224 * Return the name of a port type.
226 * @param port_type The port type to look up.
229 bhnd_port_type_name(bhnd_port_type port_type)
232 case BHND_PORT_DEVICE:
234 case BHND_PORT_BRIDGE:
236 case BHND_PORT_AGENT:
244 * Return the name of an NVRAM source.
246 * @param nvram_src The NVRAM source type to look up.
249 bhnd_nvram_src_name(bhnd_nvram_src nvram_src)
252 case BHND_NVRAM_SRC_FLASH:
254 case BHND_NVRAM_SRC_OTP:
256 case BHND_NVRAM_SRC_SPROM:
258 case BHND_NVRAM_SRC_UNKNOWN:
265 static const struct bhnd_core_desc *
266 bhnd_find_core_desc(uint16_t vendor, uint16_t device)
268 for (u_int i = 0; bhnd_core_descs[i].desc != NULL; i++) {
269 if (bhnd_core_descs[i].vendor != vendor)
272 if (bhnd_core_descs[i].device != device)
275 return (&bhnd_core_descs[i]);
282 * Return a human-readable name for a BHND core.
284 * @param vendor The core designer's JEDEC-106 Manufacturer ID.
285 * @param device The core identifier.
288 bhnd_find_core_name(uint16_t vendor, uint16_t device)
290 const struct bhnd_core_desc *desc;
292 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)
299 * Return the device class for a BHND core.
301 * @param vendor The core designer's JEDEC-106 Manufacturer ID.
302 * @param device The core identifier.
305 bhnd_find_core_class(uint16_t vendor, uint16_t device)
307 const struct bhnd_core_desc *desc;
309 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)
310 return (BHND_DEVCLASS_OTHER);
316 * Return a human-readable name for a BHND core.
318 * @param ci The core's info record.
321 bhnd_core_name(const struct bhnd_core_info *ci)
323 return bhnd_find_core_name(ci->vendor, ci->device);
327 * Return the device class for a BHND core.
329 * @param ci The core's info record.
332 bhnd_core_class(const struct bhnd_core_info *ci)
334 return bhnd_find_core_class(ci->vendor, ci->device);
338 * Write a human readable name representation of the given
339 * BHND_CHIPID_* constant to @p buffer.
341 * @param buffer Output buffer, or NULL to compute the required size.
342 * @param size Capacity of @p buffer, in bytes.
343 * @param chip_id Chip ID to be formatted.
345 * @return The required number of bytes on success, or a negative integer on
346 * failure. No more than @p size-1 characters be written, with the @p size'th
349 * @sa BHND_CHIPID_MAX_NAMELEN
352 bhnd_format_chip_id(char *buffer, size_t size, uint16_t chip_id)
354 /* All hex formatted IDs are within the range of 0x4000-0x9C3F (40000-1) */
355 if (chip_id >= 0x4000 && chip_id <= 0x9C3F)
356 return (snprintf(buffer, size, "BCM%hX", chip_id));
358 return (snprintf(buffer, size, "BCM%hu", chip_id));
362 * Return a core info record populated from a bhnd-attached @p dev.
364 * @param dev A bhnd device.
366 * @return A core info record for @p dev.
368 struct bhnd_core_info
369 bhnd_get_core_info(device_t dev) {
370 return (struct bhnd_core_info) {
371 .vendor = bhnd_get_vendor(dev),
372 .device = bhnd_get_device(dev),
373 .hwrev = bhnd_get_hwrev(dev),
374 .core_idx = bhnd_get_core_index(dev),
375 .unit = bhnd_get_core_unit(dev)
380 * Find a @p class child device with @p unit on @p bus.
382 * @param bus The bhnd-compatible bus to be searched.
383 * @param class The device class to match on.
384 * @param unit The core unit number; specify -1 to return the first match
385 * regardless of unit number.
387 * @retval device_t if a matching child device is found.
388 * @retval NULL if no matching child device is found.
391 bhnd_bus_find_child(device_t bus, bhnd_devclass_t class, int unit)
393 struct bhnd_core_match md = {
394 BHND_MATCH_CORE_CLASS(class),
395 BHND_MATCH_CORE_UNIT(unit)
399 md.m.match.core_unit = 0;
401 return bhnd_bus_match_child(bus, &md);
405 * Find the first child device on @p bus that matches @p desc.
407 * @param bus The bhnd-compatible bus to be searched.
408 * @param desc A match descriptor.
410 * @retval device_t if a matching child device is found.
411 * @retval NULL if no matching child device is found.
414 bhnd_bus_match_child(device_t bus, const struct bhnd_core_match *desc)
421 error = device_get_children(bus, &devlistp, &devcnt);
426 for (int i = 0; i < devcnt; i++) {
427 struct bhnd_core_info ci = bhnd_get_core_info(devlistp[i]);
429 if (bhnd_core_matches(&ci, desc)) {
436 free(devlistp, M_TEMP);
441 * Retrieve an ordered list of all device instances currently connected to
442 * @p bus, returning a pointer to the array in @p devlistp and the count
445 * The memory allocated for the table must be freed via
446 * bhnd_bus_free_children().
448 * @param bus The bhnd-compatible bus to be queried.
449 * @param[out] devlist The array of devices.
450 * @param[out] devcount The number of devices in @p devlistp
451 * @param order The order in which devices will be returned
455 * @retval non-zero if an error occurs, a regular unix error code will
459 bhnd_bus_get_children(device_t bus, device_t **devlist, int *devcount,
460 bhnd_device_order order)
464 /* Fetch device array */
465 if ((error = device_get_children(bus, devlist, devcount)))
468 /* Perform requested sorting */
469 if ((error = bhnd_sort_devices(*devlist, *devcount, order))) {
470 bhnd_bus_free_children(*devlist);
478 * Free any memory allocated in a previous call to bhnd_bus_get_children().
480 * @param devlist The device array returned by bhnd_bus_get_children().
483 bhnd_bus_free_children(device_t *devlist)
485 free(devlist, M_TEMP);
489 * Perform in-place sorting of an array of bhnd device instances.
491 * @param devlist An array of bhnd devices.
492 * @param devcount The number of devices in @p devs.
493 * @param order The sort order to be used.
496 * @retval EINVAL if the sort order is unknown.
499 bhnd_sort_devices(device_t *devlist, size_t devcount, bhnd_device_order order)
501 int (*compare)(const void *, const void *);
504 case BHND_DEVICE_ORDER_ATTACH:
505 compare = compare_ascending_probe_order;
507 case BHND_DEVICE_ORDER_DETACH:
508 compare = compare_descending_probe_order;
511 printf("unknown sort order: %d\n", order);
515 qsort(devlist, devcount, sizeof(*devlist), compare);
520 * Ascending comparison of bhnd device's probe order.
523 compare_ascending_probe_order(const void *lhs, const void *rhs)
528 ldev = (*(const device_t *) lhs);
529 rdev = (*(const device_t *) rhs);
531 lorder = BHND_BUS_GET_PROBE_ORDER(device_get_parent(ldev), ldev);
532 rorder = BHND_BUS_GET_PROBE_ORDER(device_get_parent(rdev), rdev);
534 if (lorder < rorder) {
536 } else if (lorder > rorder) {
544 * Descending comparison of bhnd device's probe order.
547 compare_descending_probe_order(const void *lhs, const void *rhs)
549 return (compare_ascending_probe_order(rhs, lhs));
553 * Call device_probe_and_attach() for each of the bhnd bus device's
554 * children, in bhnd attach order.
556 * @param bus The bhnd-compatible bus for which all children should be probed
560 bhnd_bus_probe_children(device_t bus)
566 /* Fetch children in attach order */
567 error = bhnd_bus_get_children(bus, &devs, &ndevs,
568 BHND_DEVICE_ORDER_ATTACH);
572 /* Probe and attach all children */
573 for (int i = 0; i < ndevs; i++) {
574 device_t child = devs[i];
575 device_probe_and_attach(child);
578 bhnd_bus_free_children(devs);
584 * Walk up the bhnd device hierarchy to locate the root device
585 * to which the bhndb bridge is attached.
587 * This can be used from within bhnd host bridge drivers to locate the
588 * actual upstream host device.
590 * @param dev A bhnd device.
591 * @param bus_class The expected bus (e.g. "pci") to which the bridge root
592 * should be attached.
594 * @retval device_t if a matching parent device is found.
595 * @retval NULL if @p dev is not attached via a bhndb bus.
596 * @retval NULL if no parent device is attached via @p bus_class.
599 bhnd_find_bridge_root(device_t dev, devclass_t bus_class)
601 devclass_t bhndb_class;
604 KASSERT(device_get_devclass(device_get_parent(dev)) == bhnd_devclass,
605 ("%s not a bhnd device", device_get_nameunit(dev)));
607 bhndb_class = devclass_find("bhndb");
609 /* Walk the device tree until we hit a bridge */
611 while ((parent = device_get_parent(parent)) != NULL) {
612 if (device_get_devclass(parent) == bhndb_class)
620 /* Search for a parent attached to the expected bus class */
621 while ((parent = device_get_parent(parent)) != NULL) {
624 bus = device_get_parent(parent);
625 if (bus != NULL && device_get_devclass(bus) == bus_class)
634 * Find the first core in @p cores that matches @p desc.
636 * @param cores The table to search.
637 * @param num_cores The length of @p cores.
638 * @param desc A match descriptor.
640 * @retval bhnd_core_info if a matching core is found.
641 * @retval NULL if no matching core is found.
643 const struct bhnd_core_info *
644 bhnd_match_core(const struct bhnd_core_info *cores, u_int num_cores,
645 const struct bhnd_core_match *desc)
647 for (u_int i = 0; i < num_cores; i++) {
648 if (bhnd_core_matches(&cores[i], desc))
657 * Find the first core in @p cores with the given @p class.
659 * @param cores The table to search.
660 * @param num_cores The length of @p cores.
661 * @param class The device class to match on.
663 * @retval non-NULL if a matching core is found.
664 * @retval NULL if no matching core is found.
666 const struct bhnd_core_info *
667 bhnd_find_core(const struct bhnd_core_info *cores, u_int num_cores,
668 bhnd_devclass_t class)
670 struct bhnd_core_match md = {
671 BHND_MATCH_CORE_CLASS(class)
674 return bhnd_match_core(cores, num_cores, &md);
679 * Create an equality match descriptor for @p core.
681 * @param core The core info to be matched on.
683 * @return an equality match descriptor for @p core.
685 struct bhnd_core_match
686 bhnd_core_get_match_desc(const struct bhnd_core_info *core)
688 return ((struct bhnd_core_match) {
689 BHND_MATCH_CORE_VENDOR(core->vendor),
690 BHND_MATCH_CORE_ID(core->device),
691 BHND_MATCH_CORE_REV(HWREV_EQ(core->hwrev)),
692 BHND_MATCH_CORE_CLASS(bhnd_core_class(core)),
693 BHND_MATCH_CORE_IDX(core->core_idx),
694 BHND_MATCH_CORE_UNIT(core->unit)
700 * Return true if the @p lhs is equal to @p rhs.
702 * @param lhs The first bhnd core descriptor to compare.
703 * @param rhs The second bhnd core descriptor to compare.
705 * @retval true if @p lhs is equal to @p rhs
706 * @retval false if @p lhs is not equal to @p rhs
709 bhnd_cores_equal(const struct bhnd_core_info *lhs,
710 const struct bhnd_core_info *rhs)
712 struct bhnd_core_match md;
714 /* Use an equality match descriptor to perform the comparison */
715 md = bhnd_core_get_match_desc(rhs);
716 return (bhnd_core_matches(lhs, &md));
720 * Return true if the @p core matches @p desc.
722 * @param core A bhnd core descriptor.
723 * @param desc A match descriptor to compare against @p core.
725 * @retval true if @p core matches @p match.
726 * @retval false if @p core does not match @p match.
729 bhnd_core_matches(const struct bhnd_core_info *core,
730 const struct bhnd_core_match *desc)
732 if (desc->m.match.core_vendor && desc->core_vendor != core->vendor)
735 if (desc->m.match.core_id && desc->core_id != core->device)
738 if (desc->m.match.core_unit && desc->core_unit != core->unit)
741 if (desc->m.match.core_rev &&
742 !bhnd_hwrev_matches(core->hwrev, &desc->core_rev))
745 if (desc->m.match.core_idx && desc->core_idx != core->core_idx)
748 if (desc->m.match.core_class &&
749 desc->core_class != bhnd_core_class(core))
756 * Return true if the @p chip matches @p desc.
758 * @param chip A bhnd chip identifier.
759 * @param desc A match descriptor to compare against @p chip.
761 * @retval true if @p chip matches @p match.
762 * @retval false if @p chip does not match @p match.
765 bhnd_chip_matches(const struct bhnd_chipid *chip,
766 const struct bhnd_chip_match *desc)
768 if (desc->m.match.chip_id && chip->chip_id != desc->chip_id)
771 if (desc->m.match.chip_pkg && chip->chip_pkg != desc->chip_pkg)
774 if (desc->m.match.chip_rev &&
775 !bhnd_hwrev_matches(chip->chip_rev, &desc->chip_rev))
778 if (desc->m.match.chip_type && chip->chip_type != desc->chip_type)
785 * Return true if the @p board matches @p desc.
787 * @param board The bhnd board info.
788 * @param desc A match descriptor to compare against @p board.
790 * @retval true if @p chip matches @p match.
791 * @retval false if @p chip does not match @p match.
794 bhnd_board_matches(const struct bhnd_board_info *board,
795 const struct bhnd_board_match *desc)
797 if (desc->m.match.board_srom_rev &&
798 !bhnd_hwrev_matches(board->board_srom_rev, &desc->board_srom_rev))
801 if (desc->m.match.board_vendor &&
802 board->board_vendor != desc->board_vendor)
805 if (desc->m.match.board_type && board->board_type != desc->board_type)
808 if (desc->m.match.board_rev &&
809 !bhnd_hwrev_matches(board->board_rev, &desc->board_rev))
816 * Return true if the @p hwrev matches @p desc.
818 * @param hwrev A bhnd hardware revision.
819 * @param desc A match descriptor to compare against @p core.
821 * @retval true if @p hwrev matches @p match.
822 * @retval false if @p hwrev does not match @p match.
825 bhnd_hwrev_matches(uint16_t hwrev, const struct bhnd_hwrev_match *desc)
827 if (desc->start != BHND_HWREV_INVALID &&
831 if (desc->end != BHND_HWREV_INVALID &&
839 * Return true if the @p dev matches @p desc.
841 * @param dev A bhnd device.
842 * @param desc A match descriptor to compare against @p dev.
844 * @retval true if @p dev matches @p match.
845 * @retval false if @p dev does not match @p match.
848 bhnd_device_matches(device_t dev, const struct bhnd_device_match *desc)
850 struct bhnd_core_info core;
851 const struct bhnd_chipid *chip;
852 struct bhnd_board_info board;
856 /* Construct individual match descriptors */
857 struct bhnd_core_match m_core = { _BHND_CORE_MATCH_COPY(desc) };
858 struct bhnd_chip_match m_chip = { _BHND_CHIP_MATCH_COPY(desc) };
859 struct bhnd_board_match m_board = { _BHND_BOARD_MATCH_COPY(desc) };
861 /* Fetch and match core info */
862 if (m_core.m.match_flags) {
863 /* Only applicable to bhnd-attached cores */
864 parent = device_get_parent(dev);
865 if (device_get_devclass(parent) != bhnd_devclass) {
866 device_printf(dev, "attempting to match core "
867 "attributes against non-core device\n");
871 core = bhnd_get_core_info(dev);
872 if (!bhnd_core_matches(&core, &m_core))
876 /* Fetch and match chip info */
877 if (m_chip.m.match_flags) {
878 chip = bhnd_get_chipid(dev);
880 if (!bhnd_chip_matches(chip, &m_chip))
884 /* Fetch and match board info.
886 * This is not available until after NVRAM is up; earlier device
887 * matches should not include board requirements */
888 if (m_board.m.match_flags) {
889 if ((error = bhnd_read_board_info(dev, &board))) {
890 device_printf(dev, "failed to read required board info "
891 "during device matching: %d\n", error);
895 if (!bhnd_board_matches(&board, &m_board))
904 * Search @p table for an entry matching @p dev.
906 * @param dev A bhnd device to match against @p table.
907 * @param table The device table to search.
908 * @param entry_size The @p table entry size, in bytes.
910 * @retval non-NULL the first matching device, if any.
911 * @retval NULL if no matching device is found in @p table.
913 const struct bhnd_device *
914 bhnd_device_lookup(device_t dev, const struct bhnd_device *table,
917 const struct bhnd_device *entry;
918 device_t hostb, parent;
919 bhnd_attach_type attach_type;
922 parent = device_get_parent(dev);
923 hostb = bhnd_bus_find_hostb_device(parent);
924 attach_type = bhnd_get_attach_type(dev);
926 for (entry = table; !BHND_DEVICE_IS_END(entry); entry =
927 (const struct bhnd_device *) ((const char *) entry + entry_size))
929 /* match core info */
930 if (!bhnd_device_matches(dev, &entry->core))
933 /* match device flags */
934 dflags = entry->device_flags;
936 /* hostb implies BHND_ATTACH_ADAPTER requirement */
937 if (dflags & BHND_DF_HOSTB)
938 dflags |= BHND_DF_ADAPTER;
940 if (dflags & BHND_DF_ADAPTER)
941 if (attach_type != BHND_ATTACH_ADAPTER)
944 if (dflags & BHND_DF_HOSTB)
948 if (dflags & BHND_DF_SOC)
949 if (attach_type != BHND_ATTACH_NATIVE)
961 * Scan the device @p table for all quirk flags applicable to @p dev.
963 * @param dev A bhnd device to match against @p table.
964 * @param table The device table to search.
965 * @param entry_size The @p table entry size, in bytes.
967 * @return all matching quirk flags.
970 bhnd_device_quirks(device_t dev, const struct bhnd_device *table,
973 const struct bhnd_device *dent;
974 const struct bhnd_device_quirk *qent, *qtable;
977 /* Locate the device entry */
978 if ((dent = bhnd_device_lookup(dev, table, entry_size)) == NULL)
981 /* Quirks table is optional */
982 qtable = dent->quirks_table;
986 /* Collect matching device quirk entries */
988 for (qent = qtable; !BHND_DEVICE_QUIRK_IS_END(qent); qent++) {
989 if (bhnd_device_matches(dev, &qent->desc))
990 quirks |= qent->quirks;
998 * Allocate bhnd(4) resources defined in @p rs from a parent bus.
1000 * @param dev The device requesting ownership of the resources.
1001 * @param rs A standard bus resource specification. This will be updated
1002 * with the allocated resource's RIDs.
1003 * @param res On success, the allocated bhnd resources.
1006 * @retval non-zero if allocation of any non-RF_OPTIONAL resource fails,
1007 * all allocated resources will be released and a regular
1008 * unix error code will be returned.
1011 bhnd_alloc_resources(device_t dev, struct resource_spec *rs,
1012 struct bhnd_resource **res)
1014 /* Initialize output array */
1015 for (u_int i = 0; rs[i].type != -1; i++)
1018 for (u_int i = 0; rs[i].type != -1; i++) {
1019 res[i] = bhnd_alloc_resource_any(dev, rs[i].type, &rs[i].rid,
1022 /* Clean up all allocations on failure */
1023 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
1024 bhnd_release_resources(dev, rs, res);
1033 * Release bhnd(4) resources defined in @p rs from a parent bus.
1035 * @param dev The device that owns the resources.
1036 * @param rs A standard bus resource specification previously initialized
1037 * by @p bhnd_alloc_resources.
1038 * @param res The bhnd resources to be released.
1041 bhnd_release_resources(device_t dev, const struct resource_spec *rs,
1042 struct bhnd_resource **res)
1044 for (u_int i = 0; rs[i].type != -1; i++) {
1048 bhnd_release_resource(dev, rs[i].type, rs[i].rid, res[i]);
1054 * Parse the CHIPC_ID_* fields from the ChipCommon CHIPC_ID
1055 * register, returning its bhnd_chipid representation.
1057 * @param idreg The CHIPC_ID register value.
1058 * @param enum_addr The enumeration address to include in the result.
1061 * On early siba(4) devices, the ChipCommon core does not provide
1062 * a valid CHIPC_ID_NUMCORE field. On these ChipCommon revisions
1063 * (see CHIPC_NCORES_MIN_HWREV()), this function will parse and return
1064 * an invalid `ncores` value.
1067 bhnd_parse_chipid(uint32_t idreg, bhnd_addr_t enum_addr)
1069 struct bhnd_chipid result;
1071 /* Fetch the basic chip info */
1072 result.chip_id = CHIPC_GET_BITS(idreg, CHIPC_ID_CHIP);
1073 result.chip_pkg = CHIPC_GET_BITS(idreg, CHIPC_ID_PKG);
1074 result.chip_rev = CHIPC_GET_BITS(idreg, CHIPC_ID_REV);
1075 result.chip_type = CHIPC_GET_BITS(idreg, CHIPC_ID_BUS);
1076 result.ncores = CHIPC_GET_BITS(idreg, CHIPC_ID_NUMCORE);
1078 result.enum_addr = enum_addr;
1085 * Determine the correct core count for a chip identification value that
1086 * may contain an invalid core count.
1088 * On some early siba(4) devices (see CHIPC_NCORES_MIN_HWREV()), the ChipCommon
1089 * core does not provide a valid CHIPC_ID_NUMCORE field.
1091 * @param cid The chip identification to be queried.
1092 * @param chipc_hwrev The hardware revision of the ChipCommon core from which
1093 * @p cid was parsed.
1094 * @param[out] ncores On success, will be set to the correct core count.
1096 * @retval 0 If the core count is already correct, or was mapped to a
1098 * @retval EINVAL If the core count is incorrect, but the chip was not
1102 bhnd_chipid_fixed_ncores(const struct bhnd_chipid *cid, uint16_t chipc_hwrev,
1105 /* bcma(4), and most siba(4) devices */
1106 if (CHIPC_NCORES_MIN_HWREV(chipc_hwrev)) {
1107 *ncores = cid->ncores;
1111 /* broken siba(4) chipsets */
1112 switch (cid->chip_id) {
1113 case BHND_CHIPID_BCM4306:
1116 case BHND_CHIPID_BCM4704:
1119 case BHND_CHIPID_BCM5365:
1121 * BCM5365 does support ID_NUMCORE in at least
1122 * some of its revisions, but for unknown
1123 * reasons, Broadcom's drivers always exclude
1124 * the ChipCommon revision (0x5) used by BCM5365
1125 * from the set of revisions supporting
1126 * ID_NUMCORE, and instead supply a fixed value.
1128 * Presumably, at least some of these devices
1129 * shipped with a broken ID_NUMCORE value.
1141 * Allocate the resource defined by @p rs via @p dev, use it
1142 * to read the ChipCommon ID register relative to @p chipc_offset,
1143 * then release the resource.
1145 * @param dev The device owning @p rs.
1146 * @param rs A resource spec that encompasses the ChipCommon register block.
1147 * @param chipc_offset The offset of the ChipCommon registers within @p rs.
1148 * @param[out] result The chip identification data.
1151 * @retval non-zero if the ChipCommon identification data could not be read.
1154 bhnd_read_chipid(device_t dev, struct resource_spec *rs,
1155 bus_size_t chipc_offset, struct bhnd_chipid *result)
1157 struct resource *res;
1158 bhnd_addr_t enum_addr;
1161 int error, rid, rtype;
1167 /* Allocate the ChipCommon window resource and fetch the chipid data */
1168 res = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
1171 "failed to allocate bhnd chipc resource\n");
1175 /* Fetch the basic chip info */
1176 reg = bus_read_4(res, chipc_offset + CHIPC_ID);
1177 chip_type = CHIPC_GET_BITS(reg, CHIPC_ID_BUS);
1179 /* Fetch the EROMPTR */
1180 if (BHND_CHIPTYPE_HAS_EROM(chip_type)) {
1181 enum_addr = bus_read_4(res, chipc_offset + CHIPC_EROMPTR);
1182 } else if (chip_type == BHND_CHIPTYPE_SIBA) {
1183 /* siba(4) uses the ChipCommon base address as the enumeration
1185 enum_addr = BHND_DEFAULT_CHIPC_ADDR;
1187 device_printf(dev, "unknown chip type %hhu\n", chip_type);
1192 *result = bhnd_parse_chipid(reg, enum_addr);
1194 /* Fix the core count on early siba(4) devices */
1195 if (chip_type == BHND_CHIPTYPE_SIBA) {
1197 uint16_t chipc_hwrev;
1200 * We need the ChipCommon revision to determine whether
1201 * the ncore field is valid.
1203 * We can safely assume the siba IDHIGH register is mapped
1204 * within the chipc register block.
1206 idh = bus_read_4(res, SB0_REG_ABS(SIBA_CFG0_IDHIGH));
1207 chipc_hwrev = SIBA_IDH_CORE_REV(idh);
1209 error = bhnd_chipid_fixed_ncores(result, chipc_hwrev,
1217 bus_release_resource(dev, rtype, rid, res);
1222 * Allocate and return a new per-core PMU clock control/status (clkctl)
1223 * instance for @p dev.
1225 * @param dev The bhnd(4) core device mapped by @p r.
1226 * @param pmu_dev The bhnd(4) PMU device, implmenting the bhnd_pmu_if
1227 * interface. The caller is responsible for ensuring that
1228 * this reference remains valid for the lifetime of the
1229 * returned clkctl instance.
1230 * @param r A resource mapping the core's clock control register
1231 * (see BHND_CLK_CTL_ST). The caller is responsible for
1232 * ensuring that this resource remains valid for the
1233 * lifetime of the returned clkctl instance.
1234 * @param offset The offset to the clock control register within @p r.
1235 * @param max_latency The PMU's maximum state transition latency in
1236 * microseconds; this upper bound will be used to busy-wait
1237 * on PMU state transitions.
1239 * @retval non-NULL success
1240 * @retval NULL if allocation fails.
1243 struct bhnd_core_clkctl *
1244 bhnd_alloc_core_clkctl(device_t dev, device_t pmu_dev, struct bhnd_resource *r,
1245 bus_size_t offset, u_int max_latency)
1247 struct bhnd_core_clkctl *clkctl;
1249 clkctl = malloc(sizeof(*clkctl), M_BHND, M_ZERO | M_NOWAIT);
1253 clkctl->cc_dev = dev;
1254 clkctl->cc_pmu_dev = pmu_dev;
1256 clkctl->cc_res_offset = offset;
1257 clkctl->cc_max_latency = max_latency;
1258 clkctl->cc_quirks = bhnd_device_quirks(dev, bhnd_clkctl_devices,
1259 sizeof(bhnd_clkctl_devices[0]));
1261 BHND_CLKCTL_LOCK_INIT(clkctl);
1267 * Free a clkctl instance previously allocated via bhnd_alloc_core_clkctl().
1269 * @param clkctl The clkctl instance to be freed.
1272 bhnd_free_core_clkctl(struct bhnd_core_clkctl *clkctl)
1274 BHND_CLKCTL_LOCK_DESTROY(clkctl);
1276 free(clkctl, M_BHND);
1280 * Wait for the per-core clock status to be equal to @p value after
1281 * applying @p mask, timing out after the maximum transition latency is reached.
1283 * @param clkctl Per-core clkctl state to be queryied.
1284 * @param value Value to wait for.
1285 * @param mask Mask to apply prior to value comparison.
1288 * @retval ETIMEDOUT if the PMU's maximum transition delay is reached before
1289 * the clock status matches @p value and @p mask.
1292 bhnd_core_clkctl_wait(struct bhnd_core_clkctl *clkctl, uint32_t value,
1297 BHND_CLKCTL_LOCK_ASSERT(clkctl, MA_OWNED);
1299 /* Bitswapped HTAVAIL/ALPAVAIL work-around */
1300 if (clkctl->cc_quirks & BHND_CLKCTL_QUIRK_CCS0) {
1301 uint32_t fmask, fval;
1303 fmask = mask & ~(BHND_CCS_HTAVAIL | BHND_CCS_ALPAVAIL);
1304 fval = value & ~(BHND_CCS_HTAVAIL | BHND_CCS_ALPAVAIL);
1306 if (mask & BHND_CCS_HTAVAIL)
1307 fmask |= BHND_CCS0_HTAVAIL;
1308 if (value & BHND_CCS_HTAVAIL)
1309 fval |= BHND_CCS0_HTAVAIL;
1311 if (mask & BHND_CCS_ALPAVAIL)
1312 fmask |= BHND_CCS0_ALPAVAIL;
1313 if (value & BHND_CCS_ALPAVAIL)
1314 fval |= BHND_CCS0_ALPAVAIL;
1320 for (u_int i = 0; i < clkctl->cc_max_latency; i += 10) {
1321 clkst = bhnd_bus_read_4(clkctl->cc_res, clkctl->cc_res_offset);
1322 if ((clkst & mask) == (value & mask))
1328 device_printf(clkctl->cc_dev, "clkst wait timeout (value=%#x, "
1329 "mask=%#x)\n", value, mask);
1335 * Read an NVRAM variable's NUL-terminated string value.
1337 * @param dev A bhnd bus child device.
1338 * @param name The NVRAM variable name.
1339 * @param[out] buf A buffer large enough to hold @p len bytes. On
1340 * success, the NUL-terminated string value will be
1341 * written to this buffer. This argment may be NULL if
1342 * the value is not desired.
1343 * @param len The maximum capacity of @p buf.
1344 * @param[out] rlen On success, will be set to the actual size of
1345 * the requested value (including NUL termination). This
1346 * argment may be NULL if the size is not desired.
1349 * @retval ENOENT The requested variable was not found.
1350 * @retval ENODEV No valid NVRAM source could be found.
1351 * @retval ENOMEM If @p buf is non-NULL and a buffer of @p len is too
1352 * small to hold the requested value.
1353 * @retval EFTYPE If the variable data cannot be coerced to a valid
1354 * string representation.
1355 * @retval ERANGE If value coercion would overflow @p type.
1356 * @retval non-zero If reading @p name otherwise fails, a regular unix
1357 * error code will be returned.
1360 bhnd_nvram_getvar_str(device_t dev, const char *name, char *buf, size_t len,
1367 error = bhnd_nvram_getvar(dev, name, buf, &larg,
1368 BHND_NVRAM_TYPE_STRING);
1376 * Read an NVRAM variable's unsigned integer value.
1378 * @param dev A bhnd bus child device.
1379 * @param name The NVRAM variable name.
1380 * @param[out] value On success, the requested value will be written
1382 * @param width The output integer type width (1, 2, or
1386 * @retval ENOENT The requested variable was not found.
1387 * @retval ENODEV No valid NVRAM source could be found.
1388 * @retval EFTYPE If the variable data cannot be coerced to a
1389 * a valid unsigned integer representation.
1390 * @retval ERANGE If value coercion would overflow (or underflow) an
1391 * unsigned representation of the given @p width.
1392 * @retval non-zero If reading @p name otherwise fails, a regular unix
1393 * error code will be returned.
1396 bhnd_nvram_getvar_uint(device_t dev, const char *name, void *value, int width)
1398 bhnd_nvram_type type;
1403 type = BHND_NVRAM_TYPE_UINT8;
1406 type = BHND_NVRAM_TYPE_UINT16;
1409 type = BHND_NVRAM_TYPE_UINT32;
1412 device_printf(dev, "unsupported NVRAM integer width: %d\n",
1418 return (bhnd_nvram_getvar(dev, name, value, &len, type));
1422 * Read an NVRAM variable's unsigned 8-bit integer value.
1424 * @param dev A bhnd bus child device.
1425 * @param name The NVRAM variable name.
1426 * @param[out] value On success, the requested value will be written
1430 * @retval ENOENT The requested variable was not found.
1431 * @retval ENODEV No valid NVRAM source could be found.
1432 * @retval EFTYPE If the variable data cannot be coerced to a
1433 * a valid unsigned integer representation.
1434 * @retval ERANGE If value coercion would overflow (or underflow) uint8_t.
1435 * @retval non-zero If reading @p name otherwise fails, a regular unix
1436 * error code will be returned.
1439 bhnd_nvram_getvar_uint8(device_t dev, const char *name, uint8_t *value)
1441 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1445 * Read an NVRAM variable's unsigned 16-bit integer value.
1447 * @param dev A bhnd bus child device.
1448 * @param name The NVRAM variable name.
1449 * @param[out] value On success, the requested value will be written
1453 * @retval ENOENT The requested variable was not found.
1454 * @retval ENODEV No valid NVRAM source could be found.
1455 * @retval EFTYPE If the variable data cannot be coerced to a
1456 * a valid unsigned integer representation.
1457 * @retval ERANGE If value coercion would overflow (or underflow)
1459 * @retval non-zero If reading @p name otherwise fails, a regular unix
1460 * error code will be returned.
1463 bhnd_nvram_getvar_uint16(device_t dev, const char *name, uint16_t *value)
1465 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1469 * Read an NVRAM variable's unsigned 32-bit integer value.
1471 * @param dev A bhnd bus child device.
1472 * @param name The NVRAM variable name.
1473 * @param[out] value On success, the requested value will be written
1477 * @retval ENOENT The requested variable was not found.
1478 * @retval ENODEV No valid NVRAM source could be found.
1479 * @retval EFTYPE If the variable data cannot be coerced to a
1480 * a valid unsigned integer representation.
1481 * @retval ERANGE If value coercion would overflow (or underflow)
1483 * @retval non-zero If reading @p name otherwise fails, a regular unix
1484 * error code will be returned.
1487 bhnd_nvram_getvar_uint32(device_t dev, const char *name, uint32_t *value)
1489 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1493 * Read an NVRAM variable's signed integer value.
1495 * @param dev A bhnd bus child device.
1496 * @param name The NVRAM variable name.
1497 * @param[out] value On success, the requested value will be written
1499 * @param width The output integer type width (1, 2, or
1503 * @retval ENOENT The requested variable was not found.
1504 * @retval ENODEV No valid NVRAM source could be found.
1505 * @retval EFTYPE If the variable data cannot be coerced to a
1506 * a valid integer representation.
1507 * @retval ERANGE If value coercion would overflow (or underflow) an
1508 * signed representation of the given @p width.
1509 * @retval non-zero If reading @p name otherwise fails, a regular unix
1510 * error code will be returned.
1513 bhnd_nvram_getvar_int(device_t dev, const char *name, void *value, int width)
1515 bhnd_nvram_type type;
1520 type = BHND_NVRAM_TYPE_INT8;
1523 type = BHND_NVRAM_TYPE_INT16;
1526 type = BHND_NVRAM_TYPE_INT32;
1529 device_printf(dev, "unsupported NVRAM integer width: %d\n",
1535 return (bhnd_nvram_getvar(dev, name, value, &len, type));
1539 * Read an NVRAM variable's signed 8-bit integer value.
1541 * @param dev A bhnd bus child device.
1542 * @param name The NVRAM variable name.
1543 * @param[out] value On success, the requested value will be written
1547 * @retval ENOENT The requested variable was not found.
1548 * @retval ENODEV No valid NVRAM source could be found.
1549 * @retval EFTYPE If the variable data cannot be coerced to a
1550 * a valid integer representation.
1551 * @retval ERANGE If value coercion would overflow (or underflow) int8_t.
1552 * @retval non-zero If reading @p name otherwise fails, a regular unix
1553 * error code will be returned.
1556 bhnd_nvram_getvar_int8(device_t dev, const char *name, int8_t *value)
1558 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1562 * Read an NVRAM variable's signed 16-bit integer value.
1564 * @param dev A bhnd bus child device.
1565 * @param name The NVRAM variable name.
1566 * @param[out] value On success, the requested value will be written
1570 * @retval ENOENT The requested variable was not found.
1571 * @retval ENODEV No valid NVRAM source could be found.
1572 * @retval EFTYPE If the variable data cannot be coerced to a
1573 * a valid integer representation.
1574 * @retval ERANGE If value coercion would overflow (or underflow)
1576 * @retval non-zero If reading @p name otherwise fails, a regular unix
1577 * error code will be returned.
1580 bhnd_nvram_getvar_int16(device_t dev, const char *name, int16_t *value)
1582 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1586 * Read an NVRAM variable's signed 32-bit integer value.
1588 * @param dev A bhnd bus child device.
1589 * @param name The NVRAM variable name.
1590 * @param[out] value On success, the requested value will be written
1594 * @retval ENOENT The requested variable was not found.
1595 * @retval ENODEV No valid NVRAM source could be found.
1596 * @retval EFTYPE If the variable data cannot be coerced to a
1597 * a valid integer representation.
1598 * @retval ERANGE If value coercion would overflow (or underflow)
1600 * @retval non-zero If reading @p name otherwise fails, a regular unix
1601 * error code will be returned.
1604 bhnd_nvram_getvar_int32(device_t dev, const char *name, int32_t *value)
1606 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1611 * Read an NVRAM variable's array value.
1613 * @param dev A bhnd bus child device.
1614 * @param name The NVRAM variable name.
1615 * @param[out] buf A buffer large enough to hold @p size bytes.
1616 * On success, the requested value will be written
1618 * @param[in,out] size The required number of bytes to write to
1620 * @param type The desired array element data representation.
1623 * @retval ENOENT The requested variable was not found.
1624 * @retval ENODEV No valid NVRAM source could be found.
1625 * @retval ENXIO If less than @p size bytes are available.
1626 * @retval ENOMEM If a buffer of @p size is too small to hold the
1628 * @retval EFTYPE If the variable data cannot be coerced to a
1629 * a valid instance of @p type.
1630 * @retval ERANGE If value coercion would overflow (or underflow) a
1631 * representation of @p type.
1632 * @retval non-zero If reading @p name otherwise fails, a regular unix
1633 * error code will be returned.
1636 bhnd_nvram_getvar_array(device_t dev, const char *name, void *buf, size_t size,
1637 bhnd_nvram_type type)
1644 if ((error = bhnd_nvram_getvar(dev, name, buf, &nbytes, type)))
1647 /* Verify that the expected number of bytes were fetched */
1655 * Initialize a service provider registry.
1657 * @param bsr The service registry to initialize.
1660 * @retval non-zero if an error occurs initializing the service registry,
1661 * a regular unix error code will be returned.
1665 bhnd_service_registry_init(struct bhnd_service_registry *bsr)
1667 STAILQ_INIT(&bsr->entries);
1668 mtx_init(&bsr->lock, "bhnd_service_registry lock", NULL, MTX_DEF);
1674 * Release all resources held by @p bsr.
1676 * @param bsr A service registry instance previously successfully
1677 * initialized via bhnd_service_registry_init().
1680 * @retval EBUSY if active references to service providers registered
1681 * with @p bsr exist.
1684 bhnd_service_registry_fini(struct bhnd_service_registry *bsr)
1686 struct bhnd_service_entry *entry, *enext;
1688 /* Remove everthing we can */
1689 mtx_lock(&bsr->lock);
1690 STAILQ_FOREACH_SAFE(entry, &bsr->entries, link, enext) {
1691 if (entry->refs > 0)
1694 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry, link);
1695 free(entry, M_BHND);
1698 if (!STAILQ_EMPTY(&bsr->entries)) {
1699 mtx_unlock(&bsr->lock);
1702 mtx_unlock(&bsr->lock);
1704 mtx_destroy(&bsr->lock);
1709 * Register a @p provider for the given @p service.
1711 * @param bsr Service registry to be modified.
1712 * @param provider Service provider to register.
1713 * @param service Service for which @p provider will be registered.
1714 * @param flags Service provider flags (see BHND_SPF_*).
1717 * @retval EEXIST if an entry for @p service already exists.
1718 * @retval EINVAL if @p service is BHND_SERVICE_ANY.
1719 * @retval non-zero if registering @p provider otherwise fails, a regular
1720 * unix error code will be returned.
1723 bhnd_service_registry_add(struct bhnd_service_registry *bsr, device_t provider,
1724 bhnd_service_t service, uint32_t flags)
1726 struct bhnd_service_entry *entry;
1728 if (service == BHND_SERVICE_ANY)
1731 mtx_lock(&bsr->lock);
1733 /* Is a service provider already registered? */
1734 STAILQ_FOREACH(entry, &bsr->entries, link) {
1735 if (entry->service == service) {
1736 mtx_unlock(&bsr->lock);
1741 /* Initialize and insert our new entry */
1742 entry = malloc(sizeof(*entry), M_BHND, M_NOWAIT);
1743 if (entry == NULL) {
1744 mtx_unlock(&bsr->lock);
1748 entry->provider = provider;
1749 entry->service = service;
1750 entry->flags = flags;
1751 refcount_init(&entry->refs, 0);
1753 STAILQ_INSERT_HEAD(&bsr->entries, entry, link);
1755 mtx_unlock(&bsr->lock);
1760 * Free an unreferenced registry entry.
1762 * @param entry The entry to be deallocated.
1765 bhnd_service_registry_free_entry(struct bhnd_service_entry *entry)
1767 KASSERT(entry->refs == 0, ("provider has active references"));
1768 free(entry, M_BHND);
1772 * Attempt to remove the @p service provider registration for @p provider.
1774 * @param bsr The service registry to be modified.
1775 * @param provider The service provider to be deregistered.
1776 * @param service The service for which @p provider will be deregistered,
1777 * or BHND_SERVICE_ANY to remove all service
1778 * registrations for @p provider.
1781 * @retval EBUSY if active references to @p provider exist; see
1782 * bhnd_service_registry_retain() and
1783 * bhnd_service_registry_release().
1786 bhnd_service_registry_remove(struct bhnd_service_registry *bsr,
1787 device_t provider, bhnd_service_t service)
1789 struct bhnd_service_entry *entry, *enext;
1791 mtx_lock(&bsr->lock);
1793 #define BHND_PROV_MATCH(_e) \
1794 ((_e)->provider == provider && \
1795 (service == BHND_SERVICE_ANY || (_e)->service == service))
1797 /* Validate matching provider entries before making any
1799 STAILQ_FOREACH(entry, &bsr->entries, link) {
1800 /* Skip non-matching entries */
1801 if (!BHND_PROV_MATCH(entry))
1804 /* Entry is in use? */
1805 if (entry->refs > 0) {
1806 mtx_unlock(&bsr->lock);
1811 /* We can now safely remove matching entries */
1812 STAILQ_FOREACH_SAFE(entry, &bsr->entries, link, enext) {
1813 /* Skip non-matching entries */
1814 if (!BHND_PROV_MATCH(entry))
1817 /* Remove from list */
1818 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry, link);
1820 /* Free provider entry */
1821 bhnd_service_registry_free_entry(entry);
1823 #undef BHND_PROV_MATCH
1825 mtx_unlock(&bsr->lock);
1830 * Retain and return a reference to a registered @p service provider, if any.
1832 * @param bsr The service registry to be queried.
1833 * @param service The service for which a provider should be returned.
1835 * On success, the caller assumes ownership the returned provider, and
1836 * is responsible for releasing this reference via
1837 * bhnd_service_registry_release().
1839 * @retval device_t success
1840 * @retval NULL if no provider is registered for @p service.
1843 bhnd_service_registry_retain(struct bhnd_service_registry *bsr,
1844 bhnd_service_t service)
1846 struct bhnd_service_entry *entry;
1848 mtx_lock(&bsr->lock);
1849 STAILQ_FOREACH(entry, &bsr->entries, link) {
1850 if (entry->service != service)
1853 /* With a live refcount, entry is gauranteed to remain alive
1854 * after we release our lock */
1855 refcount_acquire(&entry->refs);
1857 mtx_unlock(&bsr->lock);
1858 return (entry->provider);
1860 mtx_unlock(&bsr->lock);
1867 * Release a reference to a service provider previously returned by
1868 * bhnd_service_registry_retain().
1870 * If this is the last reference to an inherited service provider registration
1871 * (see BHND_SPF_INHERITED), the registration will also be removed, and
1872 * true will be returned.
1874 * @param bsr The service registry from which @p provider
1876 * @param provider The provider to be released.
1877 * @param service The service for which @p provider was previously
1879 * @retval true The inherited service provider registration was removed;
1880 * the caller should release its own reference to the
1882 * @retval false The service provider was not inherited, or active
1883 * references to the provider remain.
1885 * @see BHND_SPF_INHERITED
1888 bhnd_service_registry_release(struct bhnd_service_registry *bsr,
1889 device_t provider, bhnd_service_t service)
1891 struct bhnd_service_entry *entry;
1893 /* Exclusive lock, as we need to prevent any new references to the
1894 * entry from being taken if it's to be removed */
1895 mtx_lock(&bsr->lock);
1896 STAILQ_FOREACH(entry, &bsr->entries, link) {
1899 if (entry->provider != provider)
1902 if (entry->service != service)
1905 if (refcount_release(&entry->refs) &&
1906 (entry->flags & BHND_SPF_INHERITED))
1908 /* If an inherited entry is no longer actively
1909 * referenced, remove the local registration and inform
1911 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry,
1913 bhnd_service_registry_free_entry(entry);
1919 mtx_unlock(&bsr->lock);
1923 /* Caller owns a reference, but no such provider is registered? */
1924 panic("invalid service provider reference");
1928 * Using the bhnd(4) bus-level core information and a custom core name,
1929 * populate @p dev's device description.
1931 * @param dev A bhnd-bus attached device.
1932 * @param dev_name The core's name (e.g. "SDIO Device Core").
1935 bhnd_set_custom_core_desc(device_t dev, const char *dev_name)
1937 const char *vendor_name;
1940 vendor_name = bhnd_get_vendor_name(dev);
1941 asprintf(&desc, M_BHND, "%s %s, rev %hhu", vendor_name, dev_name,
1942 bhnd_get_hwrev(dev));
1945 device_set_desc_copy(dev, desc);
1948 device_set_desc(dev, dev_name);
1953 * Using the bhnd(4) bus-level core information, populate @p dev's device
1956 * @param dev A bhnd-bus attached device.
1959 bhnd_set_default_core_desc(device_t dev)
1961 bhnd_set_custom_core_desc(dev, bhnd_get_device_name(dev));
1966 * Using the bhnd @p chip_id, populate the bhnd(4) bus @p dev's device
1969 * @param dev A bhnd-bus attached device.
1970 * @param chip_id The chip identification.
1973 bhnd_set_default_bus_desc(device_t dev, const struct bhnd_chipid *chip_id)
1975 const char *bus_name;
1977 char chip_name[BHND_CHIPID_MAX_NAMELEN];
1979 /* Determine chip type's bus name */
1980 switch (chip_id->chip_type) {
1981 case BHND_CHIPTYPE_SIBA:
1982 bus_name = "SIBA bus";
1984 case BHND_CHIPTYPE_BCMA:
1985 case BHND_CHIPTYPE_BCMA_ALT:
1986 bus_name = "BCMA bus";
1988 case BHND_CHIPTYPE_UBUS:
1989 bus_name = "UBUS bus";
1992 bus_name = "Unknown Type";
1996 /* Format chip name */
1997 bhnd_format_chip_id(chip_name, sizeof(chip_name),
2000 /* Format and set device description */
2001 asprintf(&desc, M_BHND, "%s %s", chip_name, bus_name);
2003 device_set_desc_copy(dev, desc);
2006 device_set_desc(dev, bus_name);
2012 * Helper function for implementing BHND_BUS_REGISTER_PROVIDER().
2014 * This implementation delegates the request to the BHND_BUS_REGISTER_PROVIDER()
2015 * method on the parent of @p dev. If no parent exists, the implementation
2016 * will return an error.
2019 bhnd_bus_generic_register_provider(device_t dev, device_t child,
2020 device_t provider, bhnd_service_t service)
2022 device_t parent = device_get_parent(dev);
2024 if (parent != NULL) {
2025 return (BHND_BUS_REGISTER_PROVIDER(parent, child,
2026 provider, service));
2033 * Helper function for implementing BHND_BUS_DEREGISTER_PROVIDER().
2035 * This implementation delegates the request to the
2036 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
2037 * exists, the implementation will panic.
2040 bhnd_bus_generic_deregister_provider(device_t dev, device_t child,
2041 device_t provider, bhnd_service_t service)
2043 device_t parent = device_get_parent(dev);
2045 if (parent != NULL) {
2046 return (BHND_BUS_DEREGISTER_PROVIDER(parent, child,
2047 provider, service));
2050 panic("missing BHND_BUS_DEREGISTER_PROVIDER()");
2054 * Helper function for implementing BHND_BUS_RETAIN_PROVIDER().
2056 * This implementation delegates the request to the
2057 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
2058 * exists, the implementation will return NULL.
2061 bhnd_bus_generic_retain_provider(device_t dev, device_t child,
2062 bhnd_service_t service)
2064 device_t parent = device_get_parent(dev);
2066 if (parent != NULL) {
2067 return (BHND_BUS_RETAIN_PROVIDER(parent, child,
2075 * Helper function for implementing BHND_BUS_RELEASE_PROVIDER().
2077 * This implementation delegates the request to the
2078 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
2079 * exists, the implementation will panic.
2082 bhnd_bus_generic_release_provider(device_t dev, device_t child,
2083 device_t provider, bhnd_service_t service)
2085 device_t parent = device_get_parent(dev);
2087 if (parent != NULL) {
2088 return (BHND_BUS_RELEASE_PROVIDER(parent, child,
2089 provider, service));
2092 panic("missing BHND_BUS_RELEASE_PROVIDER()");
2096 * Helper function for implementing BHND_BUS_REGISTER_PROVIDER().
2098 * This implementation uses the bhnd_service_registry_add() function to
2099 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
2100 * a suitable service registry to edit.
2103 bhnd_bus_generic_sr_register_provider(device_t dev, device_t child,
2104 device_t provider, bhnd_service_t service)
2106 struct bhnd_service_registry *bsr;
2108 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
2110 KASSERT(bsr != NULL, ("NULL service registry"));
2112 return (bhnd_service_registry_add(bsr, provider, service, 0));
2116 * Helper function for implementing BHND_BUS_DEREGISTER_PROVIDER().
2118 * This implementation uses the bhnd_service_registry_remove() function to
2119 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
2120 * a suitable service registry to edit.
2123 bhnd_bus_generic_sr_deregister_provider(device_t dev, device_t child,
2124 device_t provider, bhnd_service_t service)
2126 struct bhnd_service_registry *bsr;
2128 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
2130 KASSERT(bsr != NULL, ("NULL service registry"));
2132 return (bhnd_service_registry_remove(bsr, provider, service));
2136 * Helper function for implementing BHND_BUS_RETAIN_PROVIDER().
2138 * This implementation uses the bhnd_service_registry_retain() function to
2139 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
2140 * a suitable service registry.
2142 * If a local provider for the service is not available, and a parent device is
2143 * available, this implementation will attempt to fetch and locally register
2144 * a service provider reference from the parent of @p dev.
2147 bhnd_bus_generic_sr_retain_provider(device_t dev, device_t child,
2148 bhnd_service_t service)
2150 struct bhnd_service_registry *bsr;
2151 device_t parent, provider;
2154 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
2155 KASSERT(bsr != NULL, ("NULL service registry"));
2158 * Attempt to fetch a service provider reference from either the local
2159 * service registry, or if not found, from our parent.
2161 * If we fetch a provider from our parent, we register the provider
2162 * with the local service registry to prevent conflicting local
2163 * registrations from being added.
2166 /* Check the local service registry first */
2167 provider = bhnd_service_registry_retain(bsr, service);
2168 if (provider != NULL)
2171 /* Otherwise, try to delegate to our parent (if any) */
2172 if ((parent = device_get_parent(dev)) == NULL)
2175 provider = BHND_BUS_RETAIN_PROVIDER(parent, dev, service);
2176 if (provider == NULL)
2179 /* Register the inherited service registration with the local
2181 error = bhnd_service_registry_add(bsr, provider, service,
2182 BHND_SPF_INHERITED);
2184 BHND_BUS_RELEASE_PROVIDER(parent, dev, provider,
2186 if (error == EEXIST) {
2187 /* A valid service provider was registered
2188 * concurrently; retry fetching from the local
2193 device_printf(dev, "failed to register service "
2194 "provider: %d\n", error);
2201 * Helper function for implementing BHND_BUS_RELEASE_PROVIDER().
2203 * This implementation uses the bhnd_service_registry_release() function to
2204 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
2205 * a suitable service registry.
2208 bhnd_bus_generic_sr_release_provider(device_t dev, device_t child,
2209 device_t provider, bhnd_service_t service)
2211 struct bhnd_service_registry *bsr;
2213 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
2214 KASSERT(bsr != NULL, ("NULL service registry"));
2216 /* Release the provider reference; if the refcount hits zero on an
2217 * inherited reference, true will be returned, and we need to drop
2218 * our own bus reference to the provider */
2219 if (!bhnd_service_registry_release(bsr, provider, service))
2222 /* Drop our reference to the borrowed provider */
2223 BHND_BUS_RELEASE_PROVIDER(device_get_parent(dev), dev, provider,
2228 * Helper function for implementing BHND_BUS_IS_HW_DISABLED().
2230 * If a parent device is available, this implementation delegates the
2231 * request to the BHND_BUS_IS_HW_DISABLED() method on the parent of @p dev.
2233 * If no parent device is available (i.e. on a the bus root), the hardware
2234 * is assumed to be usable and false is returned.
2237 bhnd_bus_generic_is_hw_disabled(device_t dev, device_t child)
2239 if (device_get_parent(dev) != NULL)
2240 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child));
2246 * Helper function for implementing BHND_BUS_GET_CHIPID().
2248 * This implementation delegates the request to the BHND_BUS_GET_CHIPID()
2249 * method on the parent of @p dev. If no parent exists, the implementation
2252 const struct bhnd_chipid *
2253 bhnd_bus_generic_get_chipid(device_t dev, device_t child)
2255 if (device_get_parent(dev) != NULL)
2256 return (BHND_BUS_GET_CHIPID(device_get_parent(dev), child));
2258 panic("missing BHND_BUS_GET_CHIPID()");
2262 * Helper function for implementing BHND_BUS_GET_DMA_TRANSLATION().
2264 * If a parent device is available, this implementation delegates the
2265 * request to the BHND_BUS_GET_DMA_TRANSLATION() method on the parent of @p dev.
2267 * If no parent device is available, this implementation will panic.
2270 bhnd_bus_generic_get_dma_translation(device_t dev, device_t child, u_int width,
2271 uint32_t flags, bus_dma_tag_t *dmat,
2272 struct bhnd_dma_translation *translation)
2274 if (device_get_parent(dev) != NULL) {
2275 return (BHND_BUS_GET_DMA_TRANSLATION(device_get_parent(dev),
2276 child, width, flags, dmat, translation));
2279 panic("missing BHND_BUS_GET_DMA_TRANSLATION()");
2282 /* nvram board_info population macros for bhnd_bus_generic_read_board_info() */
2283 #define BHND_GV(_dest, _name) \
2284 bhnd_nvram_getvar_uint(child, BHND_NVAR_ ## _name, &_dest, \
2287 #define REQ_BHND_GV(_dest, _name) do { \
2288 if ((error = BHND_GV(_dest, _name))) { \
2289 device_printf(dev, \
2290 "error reading " __STRING(_name) ": %d\n", error); \
2295 #define OPT_BHND_GV(_dest, _name, _default) do { \
2296 if ((error = BHND_GV(_dest, _name))) { \
2297 if (error != ENOENT) { \
2298 device_printf(dev, \
2300 __STRING(_name) ": %d\n", error); \
2308 * Helper function for implementing BHND_BUS_READ_BOARDINFO().
2310 * This implementation populates @p info with information from NVRAM,
2311 * defaulting board_vendor and board_type fields to 0 if the
2312 * requested variables cannot be found.
2314 * This behavior is correct for most SoCs, but must be overridden on
2315 * bridged (PCI, PCMCIA, etc) devices to produce a complete bhnd_board_info
2319 bhnd_bus_generic_read_board_info(device_t dev, device_t child,
2320 struct bhnd_board_info *info)
2324 OPT_BHND_GV(info->board_vendor, BOARDVENDOR, 0);
2325 OPT_BHND_GV(info->board_type, BOARDTYPE, 0); /* srom >= 2 */
2326 REQ_BHND_GV(info->board_rev, BOARDREV);
2327 OPT_BHND_GV(info->board_srom_rev,SROMREV, 0); /* missing in
2330 REQ_BHND_GV(info->board_flags, BOARDFLAGS);
2331 OPT_BHND_GV(info->board_flags2, BOARDFLAGS2, 0); /* srom >= 4 */
2332 OPT_BHND_GV(info->board_flags3, BOARDFLAGS3, 0); /* srom >= 11 */
2342 * Helper function for implementing BHND_BUS_GET_NVRAM_VAR().
2344 * This implementation searches @p dev for a usable NVRAM child device.
2346 * If no usable child device is found on @p dev, the request is delegated to
2347 * the BHND_BUS_GET_NVRAM_VAR() method on the parent of @p dev.
2350 bhnd_bus_generic_get_nvram_var(device_t dev, device_t child, const char *name,
2351 void *buf, size_t *size, bhnd_nvram_type type)
2356 /* Make sure we're holding Giant for newbus */
2359 /* Look for a directly-attached NVRAM child */
2360 if ((nvram = device_find_child(dev, "bhnd_nvram", -1)) != NULL)
2361 return BHND_NVRAM_GETVAR(nvram, name, buf, size, type);
2363 /* Try to delegate to parent */
2364 if ((parent = device_get_parent(dev)) == NULL)
2367 return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), child,
2368 name, buf, size, type));
2372 * Helper function for implementing BHND_BUS_ALLOC_RESOURCE().
2374 * This implementation of BHND_BUS_ALLOC_RESOURCE() delegates allocation
2375 * of the underlying resource to BUS_ALLOC_RESOURCE(), and activation
2376 * to @p dev's BHND_BUS_ACTIVATE_RESOURCE().
2378 struct bhnd_resource *
2379 bhnd_bus_generic_alloc_resource(device_t dev, device_t child, int type,
2380 int *rid, rman_res_t start, rman_res_t end, rman_res_t count,
2383 struct bhnd_resource *br;
2384 struct resource *res;
2390 /* Allocate the real bus resource (without activating it) */
2391 res = BUS_ALLOC_RESOURCE(dev, child, type, rid, start, end, count,
2392 (flags & ~RF_ACTIVE));
2396 /* Allocate our bhnd resource wrapper. */
2397 br = malloc(sizeof(struct bhnd_resource), M_BHND, M_NOWAIT);
2404 /* Attempt activation */
2405 if (flags & RF_ACTIVE) {
2406 error = BHND_BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, br);
2415 BUS_RELEASE_RESOURCE(dev, child, type, *rid, res);
2422 * Helper function for implementing BHND_BUS_RELEASE_RESOURCE().
2424 * This implementation of BHND_BUS_RELEASE_RESOURCE() delegates release of
2425 * the backing resource to BUS_RELEASE_RESOURCE().
2428 bhnd_bus_generic_release_resource(device_t dev, device_t child, int type,
2429 int rid, struct bhnd_resource *r)
2433 if ((error = BUS_RELEASE_RESOURCE(dev, child, type, rid, r->res)))
2442 * Helper function for implementing BHND_BUS_ACTIVATE_RESOURCE().
2444 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() first calls the
2445 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
2447 * If this fails, and if @p dev is the direct parent of @p child, standard
2448 * resource activation is attempted via bus_activate_resource(). This enables
2449 * direct use of the bhnd(4) resource APIs on devices that may not be attached
2450 * to a parent bhnd bus or bridge.
2453 bhnd_bus_generic_activate_resource(device_t dev, device_t child, int type,
2454 int rid, struct bhnd_resource *r)
2459 passthrough = (device_get_parent(child) != dev);
2461 /* Try to delegate to the parent */
2462 if (device_get_parent(dev) != NULL) {
2463 error = BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev),
2464 child, type, rid, r);
2469 /* If bhnd(4) activation has failed and we're the child's direct
2470 * parent, try falling back on standard resource activation.
2472 if (error && !passthrough) {
2473 error = bus_activate_resource(child, type, rid, r->res);
2482 * Helper function for implementing BHND_BUS_DEACTIVATE_RESOURCE().
2484 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the
2485 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
2488 bhnd_bus_generic_deactivate_resource(device_t dev, device_t child,
2489 int type, int rid, struct bhnd_resource *r)
2491 if (device_get_parent(dev) != NULL)
2492 return (BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev),
2493 child, type, rid, r));
2499 * Helper function for implementing BHND_BUS_GET_INTR_DOMAIN().
2501 * This implementation simply returns the address of nearest bhnd(4) bus,
2502 * which may be @p dev; this behavior may be incompatible with FDT/OFW targets.
2505 bhnd_bus_generic_get_intr_domain(device_t dev, device_t child, bool self)
2507 return ((uintptr_t)dev);