2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
5 * Copyright (c) 2017 The FreeBSD Foundation
8 * Portions of this software were developed by Landon Fuller
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
19 * redistribution must be conditioned upon including a substantially
20 * similar Disclaimer requirement for further binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
26 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
28 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
31 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGES.
36 #include <sys/cdefs.h>
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, V90_CODEC, SOFTMODEM, "V.90 SoftModem 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" },
173 static const struct bhnd_device_quirk bhnd_chipc_clkctl_quirks[];
174 static const struct bhnd_device_quirk bhnd_pcmcia_clkctl_quirks[];
177 * Device table entries for core-specific CLKCTL quirk lookup.
179 static const struct bhnd_device bhnd_clkctl_devices[] = {
180 BHND_DEVICE(BCM, CC, NULL, bhnd_chipc_clkctl_quirks),
181 BHND_DEVICE(BCM, PCMCIA, NULL, bhnd_pcmcia_clkctl_quirks),
185 /** ChipCommon CLKCTL quirks */
186 static const struct bhnd_device_quirk bhnd_chipc_clkctl_quirks[] = {
187 /* HTAVAIL/ALPAVAIL are bitswapped in chipc's CLKCTL */
188 BHND_CHIP_QUIRK(4328, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),
189 BHND_CHIP_QUIRK(5354, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),
190 BHND_DEVICE_QUIRK_END
193 /** PCMCIA CLKCTL quirks */
194 static const struct bhnd_device_quirk bhnd_pcmcia_clkctl_quirks[] = {
195 /* HTAVAIL/ALPAVAIL are bitswapped in pcmcia's CLKCTL */
196 BHND_CHIP_QUIRK(4328, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),
197 BHND_CHIP_QUIRK(5354, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),
198 BHND_DEVICE_QUIRK_END
202 * Return the name for a given JEP106 manufacturer ID.
204 * @param vendor A JEP106 Manufacturer ID, including the non-standard ARM 4-bit
205 * JEP106 continuation code.
208 bhnd_vendor_name(uint16_t vendor)
215 case BHND_MFGID_MIPS:
223 * Return the name of a port type.
225 * @param port_type The port type to look up.
228 bhnd_port_type_name(bhnd_port_type port_type)
231 case BHND_PORT_DEVICE:
233 case BHND_PORT_BRIDGE:
235 case BHND_PORT_AGENT:
243 * Return the name of an NVRAM source.
245 * @param nvram_src The NVRAM source type to look up.
248 bhnd_nvram_src_name(bhnd_nvram_src nvram_src)
251 case BHND_NVRAM_SRC_FLASH:
253 case BHND_NVRAM_SRC_OTP:
255 case BHND_NVRAM_SRC_SPROM:
257 case BHND_NVRAM_SRC_UNKNOWN:
264 static const struct bhnd_core_desc *
265 bhnd_find_core_desc(uint16_t vendor, uint16_t device)
267 for (u_int i = 0; bhnd_core_descs[i].desc != NULL; i++) {
268 if (bhnd_core_descs[i].vendor != vendor)
271 if (bhnd_core_descs[i].device != device)
274 return (&bhnd_core_descs[i]);
281 * Return a human-readable name for a BHND core.
283 * @param vendor The core designer's JEDEC-106 Manufacturer ID.
284 * @param device The core identifier.
287 bhnd_find_core_name(uint16_t vendor, uint16_t device)
289 const struct bhnd_core_desc *desc;
291 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)
298 * Return the device class for a BHND core.
300 * @param vendor The core designer's JEDEC-106 Manufacturer ID.
301 * @param device The core identifier.
304 bhnd_find_core_class(uint16_t vendor, uint16_t device)
306 const struct bhnd_core_desc *desc;
308 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)
309 return (BHND_DEVCLASS_OTHER);
315 * Return a human-readable name for a BHND core.
317 * @param ci The core's info record.
320 bhnd_core_name(const struct bhnd_core_info *ci)
322 return bhnd_find_core_name(ci->vendor, ci->device);
326 * Return the device class for a BHND core.
328 * @param ci The core's info record.
331 bhnd_core_class(const struct bhnd_core_info *ci)
333 return bhnd_find_core_class(ci->vendor, ci->device);
337 * Write a human readable name representation of the given
338 * BHND_CHIPID_* constant to @p buffer.
340 * @param buffer Output buffer, or NULL to compute the required size.
341 * @param size Capacity of @p buffer, in bytes.
342 * @param chip_id Chip ID to be formatted.
344 * @return The required number of bytes on success, or a negative integer on
345 * failure. No more than @p size-1 characters be written, with the @p size'th
348 * @sa BHND_CHIPID_MAX_NAMELEN
351 bhnd_format_chip_id(char *buffer, size_t size, uint16_t chip_id)
353 /* All hex formatted IDs are within the range of 0x4000-0x9C3F (40000-1) */
354 if (chip_id >= 0x4000 && chip_id <= 0x9C3F)
355 return (snprintf(buffer, size, "BCM%hX", chip_id));
357 return (snprintf(buffer, size, "BCM%hu", chip_id));
361 * Return a core info record populated from a bhnd-attached @p dev.
363 * @param dev A bhnd device.
365 * @return A core info record for @p dev.
367 struct bhnd_core_info
368 bhnd_get_core_info(device_t dev) {
369 return (struct bhnd_core_info) {
370 .vendor = bhnd_get_vendor(dev),
371 .device = bhnd_get_device(dev),
372 .hwrev = bhnd_get_hwrev(dev),
373 .core_idx = bhnd_get_core_index(dev),
374 .unit = bhnd_get_core_unit(dev)
379 * Find a @p class child device with @p unit on @p bus.
381 * @param bus The bhnd-compatible bus to be searched.
382 * @param class The device class to match on.
383 * @param unit The core unit number; specify -1 to return the first match
384 * regardless of unit number.
386 * @retval device_t if a matching child device is found.
387 * @retval NULL if no matching child device is found.
390 bhnd_bus_find_child(device_t bus, bhnd_devclass_t class, int unit)
392 struct bhnd_core_match md = {
393 BHND_MATCH_CORE_CLASS(class),
394 BHND_MATCH_CORE_UNIT(unit)
398 md.m.match.core_unit = 0;
400 return bhnd_bus_match_child(bus, &md);
404 * Find the first child device on @p bus that matches @p desc.
406 * @param bus The bhnd-compatible bus to be searched.
407 * @param desc A match descriptor.
409 * @retval device_t if a matching child device is found.
410 * @retval NULL if no matching child device is found.
413 bhnd_bus_match_child(device_t bus, const struct bhnd_core_match *desc)
420 error = device_get_children(bus, &devlistp, &devcnt);
425 for (int i = 0; i < devcnt; i++) {
426 struct bhnd_core_info ci = bhnd_get_core_info(devlistp[i]);
428 if (bhnd_core_matches(&ci, desc)) {
435 free(devlistp, M_TEMP);
440 * Retrieve an ordered list of all device instances currently connected to
441 * @p bus, returning a pointer to the array in @p devlistp and the count
444 * The memory allocated for the table must be freed via
445 * bhnd_bus_free_children().
447 * @param bus The bhnd-compatible bus to be queried.
448 * @param[out] devlist The array of devices.
449 * @param[out] devcount The number of devices in @p devlistp
450 * @param order The order in which devices will be returned
454 * @retval non-zero if an error occurs, a regular unix error code will
458 bhnd_bus_get_children(device_t bus, device_t **devlist, int *devcount,
459 bhnd_device_order order)
463 /* Fetch device array */
464 if ((error = device_get_children(bus, devlist, devcount)))
467 /* Perform requested sorting */
468 if ((error = bhnd_sort_devices(*devlist, *devcount, order))) {
469 bhnd_bus_free_children(*devlist);
477 * Free any memory allocated in a previous call to bhnd_bus_get_children().
479 * @param devlist The device array returned by bhnd_bus_get_children().
482 bhnd_bus_free_children(device_t *devlist)
484 free(devlist, M_TEMP);
488 * Perform in-place sorting of an array of bhnd device instances.
490 * @param devlist An array of bhnd devices.
491 * @param devcount The number of devices in @p devs.
492 * @param order The sort order to be used.
495 * @retval EINVAL if the sort order is unknown.
498 bhnd_sort_devices(device_t *devlist, size_t devcount, bhnd_device_order order)
500 int (*compare)(const void *, const void *);
503 case BHND_DEVICE_ORDER_ATTACH:
504 compare = compare_ascending_probe_order;
506 case BHND_DEVICE_ORDER_DETACH:
507 compare = compare_descending_probe_order;
510 printf("unknown sort order: %d\n", order);
514 qsort(devlist, devcount, sizeof(*devlist), compare);
519 * Ascending comparison of bhnd device's probe order.
522 compare_ascending_probe_order(const void *lhs, const void *rhs)
527 ldev = (*(const device_t *) lhs);
528 rdev = (*(const device_t *) rhs);
530 lorder = BHND_BUS_GET_PROBE_ORDER(device_get_parent(ldev), ldev);
531 rorder = BHND_BUS_GET_PROBE_ORDER(device_get_parent(rdev), rdev);
533 if (lorder < rorder) {
535 } else if (lorder > rorder) {
543 * Descending comparison of bhnd device's probe order.
546 compare_descending_probe_order(const void *lhs, const void *rhs)
548 return (compare_ascending_probe_order(rhs, lhs));
552 * Call device_probe_and_attach() for each of the bhnd bus device's
553 * children, in bhnd attach order.
555 * @param bus The bhnd-compatible bus for which all children should be probed
559 bhnd_bus_probe_children(device_t bus)
565 /* Fetch children in attach order */
566 error = bhnd_bus_get_children(bus, &devs, &ndevs,
567 BHND_DEVICE_ORDER_ATTACH);
571 /* Probe and attach all children */
572 for (int i = 0; i < ndevs; i++) {
573 device_t child = devs[i];
574 device_probe_and_attach(child);
577 bhnd_bus_free_children(devs);
583 * Walk up the bhnd device hierarchy to locate the root device
584 * to which the bhndb bridge is attached.
586 * This can be used from within bhnd host bridge drivers to locate the
587 * actual upstream host device.
589 * @param dev A bhnd device.
590 * @param bus_class The expected bus (e.g. "pci") to which the bridge root
591 * should be attached.
593 * @retval device_t if a matching parent device is found.
594 * @retval NULL if @p dev is not attached via a bhndb bus.
595 * @retval NULL if no parent device is attached via @p bus_class.
598 bhnd_find_bridge_root(device_t dev, devclass_t bus_class)
600 devclass_t bhndb_class;
603 KASSERT(device_get_devclass(device_get_parent(dev)) ==
604 devclass_find("bhnd"),
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))
656 * Find the first core in @p cores with the given @p class.
658 * @param cores The table to search.
659 * @param num_cores The length of @p cores.
660 * @param class The device class to match on.
662 * @retval non-NULL if a matching core is found.
663 * @retval NULL if no matching core is found.
665 const struct bhnd_core_info *
666 bhnd_find_core(const struct bhnd_core_info *cores, u_int num_cores,
667 bhnd_devclass_t class)
669 struct bhnd_core_match md = {
670 BHND_MATCH_CORE_CLASS(class)
673 return bhnd_match_core(cores, num_cores, &md);
677 * Create an equality match descriptor for @p core.
679 * @param core The core info to be matched on.
681 * @return an equality match descriptor for @p core.
683 struct bhnd_core_match
684 bhnd_core_get_match_desc(const struct bhnd_core_info *core)
686 return ((struct bhnd_core_match) {
687 BHND_MATCH_CORE_VENDOR(core->vendor),
688 BHND_MATCH_CORE_ID(core->device),
689 BHND_MATCH_CORE_REV(HWREV_EQ(core->hwrev)),
690 BHND_MATCH_CORE_CLASS(bhnd_core_class(core)),
691 BHND_MATCH_CORE_IDX(core->core_idx),
692 BHND_MATCH_CORE_UNIT(core->unit)
697 * Return true if the @p lhs is equal to @p rhs.
699 * @param lhs The first bhnd core descriptor to compare.
700 * @param rhs The second bhnd core descriptor to compare.
702 * @retval true if @p lhs is equal to @p rhs
703 * @retval false if @p lhs is not equal to @p rhs
706 bhnd_cores_equal(const struct bhnd_core_info *lhs,
707 const struct bhnd_core_info *rhs)
709 struct bhnd_core_match md;
711 /* Use an equality match descriptor to perform the comparison */
712 md = bhnd_core_get_match_desc(rhs);
713 return (bhnd_core_matches(lhs, &md));
717 * Return true if the @p core matches @p desc.
719 * @param core A bhnd core descriptor.
720 * @param desc A match descriptor to compare against @p core.
722 * @retval true if @p core matches @p match.
723 * @retval false if @p core does not match @p match.
726 bhnd_core_matches(const struct bhnd_core_info *core,
727 const struct bhnd_core_match *desc)
729 if (desc->m.match.core_vendor && desc->core_vendor != core->vendor)
732 if (desc->m.match.core_id && desc->core_id != core->device)
735 if (desc->m.match.core_unit && desc->core_unit != core->unit)
738 if (desc->m.match.core_rev &&
739 !bhnd_hwrev_matches(core->hwrev, &desc->core_rev))
742 if (desc->m.match.core_idx && desc->core_idx != core->core_idx)
745 if (desc->m.match.core_class &&
746 desc->core_class != bhnd_core_class(core))
753 * Return true if the @p chip matches @p desc.
755 * @param chip A bhnd chip identifier.
756 * @param desc A match descriptor to compare against @p chip.
758 * @retval true if @p chip matches @p match.
759 * @retval false if @p chip does not match @p match.
762 bhnd_chip_matches(const struct bhnd_chipid *chip,
763 const struct bhnd_chip_match *desc)
765 if (desc->m.match.chip_id && chip->chip_id != desc->chip_id)
768 if (desc->m.match.chip_pkg && chip->chip_pkg != desc->chip_pkg)
771 if (desc->m.match.chip_rev &&
772 !bhnd_hwrev_matches(chip->chip_rev, &desc->chip_rev))
775 if (desc->m.match.chip_type && chip->chip_type != desc->chip_type)
782 * Return true if the @p board matches @p desc.
784 * @param board The bhnd board info.
785 * @param desc A match descriptor to compare against @p board.
787 * @retval true if @p chip matches @p match.
788 * @retval false if @p chip does not match @p match.
791 bhnd_board_matches(const struct bhnd_board_info *board,
792 const struct bhnd_board_match *desc)
794 if (desc->m.match.board_srom_rev &&
795 !bhnd_hwrev_matches(board->board_srom_rev, &desc->board_srom_rev))
798 if (desc->m.match.board_vendor &&
799 board->board_vendor != desc->board_vendor)
802 if (desc->m.match.board_type && board->board_type != desc->board_type)
805 if (desc->m.match.board_devid &&
806 board->board_devid != desc->board_devid)
809 if (desc->m.match.board_rev &&
810 !bhnd_hwrev_matches(board->board_rev, &desc->board_rev))
817 * Return true if the @p hwrev matches @p desc.
819 * @param hwrev A bhnd hardware revision.
820 * @param desc A match descriptor to compare against @p core.
822 * @retval true if @p hwrev matches @p match.
823 * @retval false if @p hwrev does not match @p match.
826 bhnd_hwrev_matches(uint16_t hwrev, const struct bhnd_hwrev_match *desc)
828 if (desc->start != BHND_HWREV_INVALID &&
832 if (desc->end != BHND_HWREV_INVALID &&
840 * Return true if the @p dev matches @p desc.
842 * @param dev A bhnd device.
843 * @param desc A match descriptor to compare against @p dev.
845 * @retval true if @p dev matches @p match.
846 * @retval false if @p dev does not match @p match.
849 bhnd_device_matches(device_t dev, const struct bhnd_device_match *desc)
851 struct bhnd_core_info core;
852 const struct bhnd_chipid *chip;
853 struct bhnd_board_info board;
857 /* Construct individual match descriptors */
858 struct bhnd_core_match m_core = { _BHND_CORE_MATCH_COPY(desc) };
859 struct bhnd_chip_match m_chip = { _BHND_CHIP_MATCH_COPY(desc) };
860 struct bhnd_board_match m_board = { _BHND_BOARD_MATCH_COPY(desc) };
862 /* Fetch and match core info */
863 if (m_core.m.match_flags) {
864 /* Only applicable to bhnd-attached cores */
865 parent = device_get_parent(dev);
866 if (device_get_devclass(parent) != devclass_find("bhnd")) {
867 device_printf(dev, "attempting to match core "
868 "attributes against non-core device\n");
872 core = bhnd_get_core_info(dev);
873 if (!bhnd_core_matches(&core, &m_core))
877 /* Fetch and match chip info */
878 if (m_chip.m.match_flags) {
879 chip = bhnd_get_chipid(dev);
881 if (!bhnd_chip_matches(chip, &m_chip))
885 /* Fetch and match board info.
887 * This is not available until after NVRAM is up; earlier device
888 * matches should not include board requirements */
889 if (m_board.m.match_flags) {
890 if ((error = bhnd_read_board_info(dev, &board))) {
891 device_printf(dev, "failed to read required board info "
892 "during device matching: %d\n", error);
896 if (!bhnd_board_matches(&board, &m_board))
905 * Search @p table for an entry matching @p dev.
907 * @param dev A bhnd device to match against @p table.
908 * @param table The device table to search.
909 * @param entry_size The @p table entry size, in bytes.
911 * @retval non-NULL the first matching device, if any.
912 * @retval NULL if no matching device is found in @p table.
914 const struct bhnd_device *
915 bhnd_device_lookup(device_t dev, const struct bhnd_device *table,
918 const struct bhnd_device *entry;
919 device_t hostb, parent;
920 bhnd_attach_type attach_type;
923 parent = device_get_parent(dev);
924 hostb = bhnd_bus_find_hostb_device(parent);
925 attach_type = bhnd_get_attach_type(dev);
927 for (entry = table; !BHND_DEVICE_IS_END(entry); entry =
928 (const struct bhnd_device *) ((const char *) entry + entry_size))
930 /* match core info */
931 if (!bhnd_device_matches(dev, &entry->core))
934 /* match device flags */
935 dflags = entry->device_flags;
937 /* hostb implies BHND_ATTACH_ADAPTER requirement */
938 if (dflags & BHND_DF_HOSTB)
939 dflags |= BHND_DF_ADAPTER;
941 if (dflags & BHND_DF_ADAPTER)
942 if (attach_type != BHND_ATTACH_ADAPTER)
945 if (dflags & BHND_DF_HOSTB)
949 if (dflags & BHND_DF_SOC)
950 if (attach_type != BHND_ATTACH_NATIVE)
962 * Scan the device @p table for all quirk flags applicable to @p dev.
964 * @param dev A bhnd device to match against @p table.
965 * @param table The device table to search.
966 * @param entry_size The @p table entry size, in bytes.
968 * @return all matching quirk flags.
971 bhnd_device_quirks(device_t dev, const struct bhnd_device *table,
974 const struct bhnd_device *dent;
975 const struct bhnd_device_quirk *qent, *qtable;
978 /* Locate the device entry */
979 if ((dent = bhnd_device_lookup(dev, table, entry_size)) == NULL)
982 /* Quirks table is optional */
983 qtable = dent->quirks_table;
987 /* Collect matching device quirk entries */
989 for (qent = qtable; !BHND_DEVICE_QUIRK_IS_END(qent); qent++) {
990 if (bhnd_device_matches(dev, &qent->desc))
991 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 * Allocate and return a new per-core PMU clock control/status (clkctl)
1055 * instance for @p dev.
1057 * @param dev The bhnd(4) core device mapped by @p r.
1058 * @param pmu_dev The bhnd(4) PMU device, implmenting the bhnd_pmu_if
1059 * interface. The caller is responsible for ensuring that
1060 * this reference remains valid for the lifetime of the
1061 * returned clkctl instance.
1062 * @param r A resource mapping the core's clock control register
1063 * (see BHND_CLK_CTL_ST). The caller is responsible for
1064 * ensuring that this resource remains valid for the
1065 * lifetime of the returned clkctl instance.
1066 * @param offset The offset to the clock control register within @p r.
1067 * @param max_latency The PMU's maximum state transition latency in
1068 * microseconds; this upper bound will be used to busy-wait
1069 * on PMU state transitions.
1071 * @retval non-NULL success
1072 * @retval NULL if allocation fails.
1075 struct bhnd_core_clkctl *
1076 bhnd_alloc_core_clkctl(device_t dev, device_t pmu_dev, struct bhnd_resource *r,
1077 bus_size_t offset, u_int max_latency)
1079 struct bhnd_core_clkctl *clkctl;
1081 clkctl = malloc(sizeof(*clkctl), M_BHND, M_ZERO | M_NOWAIT);
1085 clkctl->cc_dev = dev;
1086 clkctl->cc_pmu_dev = pmu_dev;
1088 clkctl->cc_res_offset = offset;
1089 clkctl->cc_max_latency = max_latency;
1090 clkctl->cc_quirks = bhnd_device_quirks(dev, bhnd_clkctl_devices,
1091 sizeof(bhnd_clkctl_devices[0]));
1093 BHND_CLKCTL_LOCK_INIT(clkctl);
1099 * Free a clkctl instance previously allocated via bhnd_alloc_core_clkctl().
1101 * @param clkctl The clkctl instance to be freed.
1104 bhnd_free_core_clkctl(struct bhnd_core_clkctl *clkctl)
1106 BHND_CLKCTL_LOCK_DESTROY(clkctl);
1108 free(clkctl, M_BHND);
1112 * Wait for the per-core clock status to be equal to @p value after
1113 * applying @p mask, timing out after the maximum transition latency is reached.
1115 * @param clkctl Per-core clkctl state to be queryied.
1116 * @param value Value to wait for.
1117 * @param mask Mask to apply prior to value comparison.
1120 * @retval ETIMEDOUT if the PMU's maximum transition delay is reached before
1121 * the clock status matches @p value and @p mask.
1124 bhnd_core_clkctl_wait(struct bhnd_core_clkctl *clkctl, uint32_t value,
1129 BHND_CLKCTL_LOCK_ASSERT(clkctl, MA_OWNED);
1131 /* Bitswapped HTAVAIL/ALPAVAIL work-around */
1132 if (clkctl->cc_quirks & BHND_CLKCTL_QUIRK_CCS0) {
1133 uint32_t fmask, fval;
1135 fmask = mask & ~(BHND_CCS_HTAVAIL | BHND_CCS_ALPAVAIL);
1136 fval = value & ~(BHND_CCS_HTAVAIL | BHND_CCS_ALPAVAIL);
1138 if (mask & BHND_CCS_HTAVAIL)
1139 fmask |= BHND_CCS0_HTAVAIL;
1140 if (value & BHND_CCS_HTAVAIL)
1141 fval |= BHND_CCS0_HTAVAIL;
1143 if (mask & BHND_CCS_ALPAVAIL)
1144 fmask |= BHND_CCS0_ALPAVAIL;
1145 if (value & BHND_CCS_ALPAVAIL)
1146 fval |= BHND_CCS0_ALPAVAIL;
1152 for (u_int i = 0; i < clkctl->cc_max_latency; i += 10) {
1153 clkst = bhnd_bus_read_4(clkctl->cc_res, clkctl->cc_res_offset);
1154 if ((clkst & mask) == (value & mask))
1160 device_printf(clkctl->cc_dev, "clkst wait timeout (value=%#x, "
1161 "mask=%#x)\n", value, mask);
1167 * Read an NVRAM variable's NUL-terminated string value.
1169 * @param dev A bhnd bus child device.
1170 * @param name The NVRAM variable name.
1171 * @param[out] buf A buffer large enough to hold @p len bytes. On
1172 * success, the NUL-terminated string value will be
1173 * written to this buffer. This argment may be NULL if
1174 * the value is not desired.
1175 * @param len The maximum capacity of @p buf.
1176 * @param[out] rlen On success, will be set to the actual size of
1177 * the requested value (including NUL termination). This
1178 * argment may be NULL if the size is not desired.
1181 * @retval ENOENT The requested variable was not found.
1182 * @retval ENODEV No valid NVRAM source could be found.
1183 * @retval ENOMEM If @p buf is non-NULL and a buffer of @p len is too
1184 * small to hold the requested value.
1185 * @retval EFTYPE If the variable data cannot be coerced to a valid
1186 * string representation.
1187 * @retval ERANGE If value coercion would overflow @p type.
1188 * @retval non-zero If reading @p name otherwise fails, a regular unix
1189 * error code will be returned.
1192 bhnd_nvram_getvar_str(device_t dev, const char *name, char *buf, size_t len,
1199 error = bhnd_nvram_getvar(dev, name, buf, &larg,
1200 BHND_NVRAM_TYPE_STRING);
1208 * Read an NVRAM variable's unsigned integer value.
1210 * @param dev A bhnd bus child device.
1211 * @param name The NVRAM variable name.
1212 * @param[out] value On success, the requested value will be written
1214 * @param width The output integer type width (1, 2, or
1218 * @retval ENOENT The requested variable was not found.
1219 * @retval ENODEV No valid NVRAM source could be found.
1220 * @retval EFTYPE If the variable data cannot be coerced to a
1221 * a valid unsigned integer representation.
1222 * @retval ERANGE If value coercion would overflow (or underflow) an
1223 * unsigned representation of the given @p width.
1224 * @retval non-zero If reading @p name otherwise fails, a regular unix
1225 * error code will be returned.
1228 bhnd_nvram_getvar_uint(device_t dev, const char *name, void *value, int width)
1230 bhnd_nvram_type type;
1235 type = BHND_NVRAM_TYPE_UINT8;
1238 type = BHND_NVRAM_TYPE_UINT16;
1241 type = BHND_NVRAM_TYPE_UINT32;
1244 device_printf(dev, "unsupported NVRAM integer width: %d\n",
1250 return (bhnd_nvram_getvar(dev, name, value, &len, type));
1254 * Read an NVRAM variable's unsigned 8-bit integer value.
1256 * @param dev A bhnd bus child device.
1257 * @param name The NVRAM variable name.
1258 * @param[out] value On success, the requested value will be written
1262 * @retval ENOENT The requested variable was not found.
1263 * @retval ENODEV No valid NVRAM source could be found.
1264 * @retval EFTYPE If the variable data cannot be coerced to a
1265 * a valid unsigned integer representation.
1266 * @retval ERANGE If value coercion would overflow (or underflow) uint8_t.
1267 * @retval non-zero If reading @p name otherwise fails, a regular unix
1268 * error code will be returned.
1271 bhnd_nvram_getvar_uint8(device_t dev, const char *name, uint8_t *value)
1273 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1277 * Read an NVRAM variable's unsigned 16-bit integer value.
1279 * @param dev A bhnd bus child device.
1280 * @param name The NVRAM variable name.
1281 * @param[out] value On success, the requested value will be written
1285 * @retval ENOENT The requested variable was not found.
1286 * @retval ENODEV No valid NVRAM source could be found.
1287 * @retval EFTYPE If the variable data cannot be coerced to a
1288 * a valid unsigned integer representation.
1289 * @retval ERANGE If value coercion would overflow (or underflow)
1291 * @retval non-zero If reading @p name otherwise fails, a regular unix
1292 * error code will be returned.
1295 bhnd_nvram_getvar_uint16(device_t dev, const char *name, uint16_t *value)
1297 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1301 * Read an NVRAM variable's unsigned 32-bit integer value.
1303 * @param dev A bhnd bus child device.
1304 * @param name The NVRAM variable name.
1305 * @param[out] value On success, the requested value will be written
1309 * @retval ENOENT The requested variable was not found.
1310 * @retval ENODEV No valid NVRAM source could be found.
1311 * @retval EFTYPE If the variable data cannot be coerced to a
1312 * a valid unsigned integer representation.
1313 * @retval ERANGE If value coercion would overflow (or underflow)
1315 * @retval non-zero If reading @p name otherwise fails, a regular unix
1316 * error code will be returned.
1319 bhnd_nvram_getvar_uint32(device_t dev, const char *name, uint32_t *value)
1321 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1325 * Read an NVRAM variable's signed integer value.
1327 * @param dev A bhnd bus child device.
1328 * @param name The NVRAM variable name.
1329 * @param[out] value On success, the requested value will be written
1331 * @param width The output integer type width (1, 2, or
1335 * @retval ENOENT The requested variable was not found.
1336 * @retval ENODEV No valid NVRAM source could be found.
1337 * @retval EFTYPE If the variable data cannot be coerced to a
1338 * a valid integer representation.
1339 * @retval ERANGE If value coercion would overflow (or underflow) an
1340 * signed representation of the given @p width.
1341 * @retval non-zero If reading @p name otherwise fails, a regular unix
1342 * error code will be returned.
1345 bhnd_nvram_getvar_int(device_t dev, const char *name, void *value, int width)
1347 bhnd_nvram_type type;
1352 type = BHND_NVRAM_TYPE_INT8;
1355 type = BHND_NVRAM_TYPE_INT16;
1358 type = BHND_NVRAM_TYPE_INT32;
1361 device_printf(dev, "unsupported NVRAM integer width: %d\n",
1367 return (bhnd_nvram_getvar(dev, name, value, &len, type));
1371 * Read an NVRAM variable's signed 8-bit integer value.
1373 * @param dev A bhnd bus child device.
1374 * @param name The NVRAM variable name.
1375 * @param[out] value On success, the requested value will be written
1379 * @retval ENOENT The requested variable was not found.
1380 * @retval ENODEV No valid NVRAM source could be found.
1381 * @retval EFTYPE If the variable data cannot be coerced to a
1382 * a valid integer representation.
1383 * @retval ERANGE If value coercion would overflow (or underflow) int8_t.
1384 * @retval non-zero If reading @p name otherwise fails, a regular unix
1385 * error code will be returned.
1388 bhnd_nvram_getvar_int8(device_t dev, const char *name, int8_t *value)
1390 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1394 * Read an NVRAM variable's signed 16-bit integer value.
1396 * @param dev A bhnd bus child device.
1397 * @param name The NVRAM variable name.
1398 * @param[out] value On success, the requested value will be written
1402 * @retval ENOENT The requested variable was not found.
1403 * @retval ENODEV No valid NVRAM source could be found.
1404 * @retval EFTYPE If the variable data cannot be coerced to a
1405 * a valid integer representation.
1406 * @retval ERANGE If value coercion would overflow (or underflow)
1408 * @retval non-zero If reading @p name otherwise fails, a regular unix
1409 * error code will be returned.
1412 bhnd_nvram_getvar_int16(device_t dev, const char *name, int16_t *value)
1414 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1418 * Read an NVRAM variable's signed 32-bit integer value.
1420 * @param dev A bhnd bus child device.
1421 * @param name The NVRAM variable name.
1422 * @param[out] value On success, the requested value will be written
1426 * @retval ENOENT The requested variable was not found.
1427 * @retval ENODEV No valid NVRAM source could be found.
1428 * @retval EFTYPE If the variable data cannot be coerced to a
1429 * a valid integer representation.
1430 * @retval ERANGE If value coercion would overflow (or underflow)
1432 * @retval non-zero If reading @p name otherwise fails, a regular unix
1433 * error code will be returned.
1436 bhnd_nvram_getvar_int32(device_t dev, const char *name, int32_t *value)
1438 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1442 * Read an NVRAM variable's array value.
1444 * @param dev A bhnd bus child device.
1445 * @param name The NVRAM variable name.
1446 * @param[out] buf A buffer large enough to hold @p size bytes.
1447 * On success, the requested value will be written
1449 * @param[in,out] size The required number of bytes to write to
1451 * @param type The desired array element data representation.
1454 * @retval ENOENT The requested variable was not found.
1455 * @retval ENODEV No valid NVRAM source could be found.
1456 * @retval ENXIO If less than @p size bytes are available.
1457 * @retval ENOMEM If a buffer of @p size is too small to hold the
1459 * @retval EFTYPE If the variable data cannot be coerced to a
1460 * a valid instance of @p type.
1461 * @retval ERANGE If value coercion would overflow (or underflow) a
1462 * representation of @p type.
1463 * @retval non-zero If reading @p name otherwise fails, a regular unix
1464 * error code will be returned.
1467 bhnd_nvram_getvar_array(device_t dev, const char *name, void *buf, size_t size,
1468 bhnd_nvram_type type)
1475 if ((error = bhnd_nvram_getvar(dev, name, buf, &nbytes, type)))
1478 /* Verify that the expected number of bytes were fetched */
1486 * Initialize a service provider registry.
1488 * @param bsr The service registry to initialize.
1491 * @retval non-zero if an error occurs initializing the service registry,
1492 * a regular unix error code will be returned.
1496 bhnd_service_registry_init(struct bhnd_service_registry *bsr)
1498 STAILQ_INIT(&bsr->entries);
1499 mtx_init(&bsr->lock, "bhnd_service_registry lock", NULL, MTX_DEF);
1505 * Release all resources held by @p bsr.
1507 * @param bsr A service registry instance previously successfully
1508 * initialized via bhnd_service_registry_init().
1511 * @retval EBUSY if active references to service providers registered
1512 * with @p bsr exist.
1515 bhnd_service_registry_fini(struct bhnd_service_registry *bsr)
1517 struct bhnd_service_entry *entry, *enext;
1519 /* Remove everthing we can */
1520 mtx_lock(&bsr->lock);
1521 STAILQ_FOREACH_SAFE(entry, &bsr->entries, link, enext) {
1522 if (entry->refs > 0)
1525 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry, link);
1526 free(entry, M_BHND);
1529 if (!STAILQ_EMPTY(&bsr->entries)) {
1530 mtx_unlock(&bsr->lock);
1533 mtx_unlock(&bsr->lock);
1535 mtx_destroy(&bsr->lock);
1540 * Register a @p provider for the given @p service.
1542 * @param bsr Service registry to be modified.
1543 * @param provider Service provider to register.
1544 * @param service Service for which @p provider will be registered.
1545 * @param flags Service provider flags (see BHND_SPF_*).
1548 * @retval EEXIST if an entry for @p service already exists.
1549 * @retval EINVAL if @p service is BHND_SERVICE_ANY.
1550 * @retval non-zero if registering @p provider otherwise fails, a regular
1551 * unix error code will be returned.
1554 bhnd_service_registry_add(struct bhnd_service_registry *bsr, device_t provider,
1555 bhnd_service_t service, uint32_t flags)
1557 struct bhnd_service_entry *entry;
1559 if (service == BHND_SERVICE_ANY)
1562 mtx_lock(&bsr->lock);
1564 /* Is a service provider already registered? */
1565 STAILQ_FOREACH(entry, &bsr->entries, link) {
1566 if (entry->service == service) {
1567 mtx_unlock(&bsr->lock);
1572 /* Initialize and insert our new entry */
1573 entry = malloc(sizeof(*entry), M_BHND, M_NOWAIT);
1574 if (entry == NULL) {
1575 mtx_unlock(&bsr->lock);
1579 entry->provider = provider;
1580 entry->service = service;
1581 entry->flags = flags;
1582 refcount_init(&entry->refs, 0);
1584 STAILQ_INSERT_HEAD(&bsr->entries, entry, link);
1586 mtx_unlock(&bsr->lock);
1591 * Free an unreferenced registry entry.
1593 * @param entry The entry to be deallocated.
1596 bhnd_service_registry_free_entry(struct bhnd_service_entry *entry)
1598 KASSERT(entry->refs == 0, ("provider has active references"));
1599 free(entry, M_BHND);
1603 * Attempt to remove the @p service provider registration for @p provider.
1605 * @param bsr The service registry to be modified.
1606 * @param provider The service provider to be deregistered.
1607 * @param service The service for which @p provider will be deregistered,
1608 * or BHND_SERVICE_ANY to remove all service
1609 * registrations for @p provider.
1612 * @retval EBUSY if active references to @p provider exist; see
1613 * bhnd_service_registry_retain() and
1614 * bhnd_service_registry_release().
1617 bhnd_service_registry_remove(struct bhnd_service_registry *bsr,
1618 device_t provider, bhnd_service_t service)
1620 struct bhnd_service_entry *entry, *enext;
1622 mtx_lock(&bsr->lock);
1624 #define BHND_PROV_MATCH(_e) \
1625 ((_e)->provider == provider && \
1626 (service == BHND_SERVICE_ANY || (_e)->service == service))
1628 /* Validate matching provider entries before making any
1630 STAILQ_FOREACH(entry, &bsr->entries, link) {
1631 /* Skip non-matching entries */
1632 if (!BHND_PROV_MATCH(entry))
1635 /* Entry is in use? */
1636 if (entry->refs > 0) {
1637 mtx_unlock(&bsr->lock);
1642 /* We can now safely remove matching entries */
1643 STAILQ_FOREACH_SAFE(entry, &bsr->entries, link, enext) {
1644 /* Skip non-matching entries */
1645 if (!BHND_PROV_MATCH(entry))
1648 /* Remove from list */
1649 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry, link);
1651 /* Free provider entry */
1652 bhnd_service_registry_free_entry(entry);
1654 #undef BHND_PROV_MATCH
1656 mtx_unlock(&bsr->lock);
1661 * Retain and return a reference to a registered @p service provider, if any.
1663 * @param bsr The service registry to be queried.
1664 * @param service The service for which a provider should be returned.
1666 * On success, the caller assumes ownership the returned provider, and
1667 * is responsible for releasing this reference via
1668 * bhnd_service_registry_release().
1670 * @retval device_t success
1671 * @retval NULL if no provider is registered for @p service.
1674 bhnd_service_registry_retain(struct bhnd_service_registry *bsr,
1675 bhnd_service_t service)
1677 struct bhnd_service_entry *entry;
1679 mtx_lock(&bsr->lock);
1680 STAILQ_FOREACH(entry, &bsr->entries, link) {
1681 if (entry->service != service)
1684 /* With a live refcount, entry is gauranteed to remain alive
1685 * after we release our lock */
1686 refcount_acquire(&entry->refs);
1688 mtx_unlock(&bsr->lock);
1689 return (entry->provider);
1691 mtx_unlock(&bsr->lock);
1698 * Release a reference to a service provider previously returned by
1699 * bhnd_service_registry_retain().
1701 * If this is the last reference to an inherited service provider registration
1702 * (see BHND_SPF_INHERITED), the registration will also be removed, and
1703 * true will be returned.
1705 * @param bsr The service registry from which @p provider
1707 * @param provider The provider to be released.
1708 * @param service The service for which @p provider was previously
1710 * @retval true The inherited service provider registration was removed;
1711 * the caller should release its own reference to the
1713 * @retval false The service provider was not inherited, or active
1714 * references to the provider remain.
1716 * @see BHND_SPF_INHERITED
1719 bhnd_service_registry_release(struct bhnd_service_registry *bsr,
1720 device_t provider, bhnd_service_t service)
1722 struct bhnd_service_entry *entry;
1724 /* Exclusive lock, as we need to prevent any new references to the
1725 * entry from being taken if it's to be removed */
1726 mtx_lock(&bsr->lock);
1727 STAILQ_FOREACH(entry, &bsr->entries, link) {
1730 if (entry->provider != provider)
1733 if (entry->service != service)
1736 if (refcount_release(&entry->refs) &&
1737 (entry->flags & BHND_SPF_INHERITED))
1739 /* If an inherited entry is no longer actively
1740 * referenced, remove the local registration and inform
1742 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry,
1744 bhnd_service_registry_free_entry(entry);
1750 mtx_unlock(&bsr->lock);
1754 /* Caller owns a reference, but no such provider is registered? */
1755 panic("invalid service provider reference");
1759 * Using the bhnd(4) bus-level core information and a custom core name,
1760 * populate @p dev's device description.
1762 * @param dev A bhnd-bus attached device.
1763 * @param dev_name The core's name (e.g. "SDIO Device Core").
1766 bhnd_set_custom_core_desc(device_t dev, const char *dev_name)
1768 const char *vendor_name;
1771 vendor_name = bhnd_get_vendor_name(dev);
1772 asprintf(&desc, M_BHND, "%s %s, rev %hhu", vendor_name, dev_name,
1773 bhnd_get_hwrev(dev));
1776 device_set_desc_copy(dev, desc);
1779 device_set_desc(dev, dev_name);
1784 * Using the bhnd(4) bus-level core information, populate @p dev's device
1787 * @param dev A bhnd-bus attached device.
1790 bhnd_set_default_core_desc(device_t dev)
1792 bhnd_set_custom_core_desc(dev, bhnd_get_device_name(dev));
1796 * Using the bhnd @p chip_id, populate the bhnd(4) bus @p dev's device
1799 * @param dev A bhnd-bus attached device.
1800 * @param chip_id The chip identification.
1803 bhnd_set_default_bus_desc(device_t dev, const struct bhnd_chipid *chip_id)
1805 const char *bus_name;
1807 char chip_name[BHND_CHIPID_MAX_NAMELEN];
1809 /* Determine chip type's bus name */
1810 switch (chip_id->chip_type) {
1811 case BHND_CHIPTYPE_SIBA:
1812 bus_name = "SIBA bus";
1814 case BHND_CHIPTYPE_BCMA:
1815 case BHND_CHIPTYPE_BCMA_ALT:
1816 bus_name = "BCMA bus";
1818 case BHND_CHIPTYPE_UBUS:
1819 bus_name = "UBUS bus";
1822 bus_name = "Unknown Type";
1826 /* Format chip name */
1827 bhnd_format_chip_id(chip_name, sizeof(chip_name),
1830 /* Format and set device description */
1831 asprintf(&desc, M_BHND, "%s %s", chip_name, bus_name);
1833 device_set_desc_copy(dev, desc);
1836 device_set_desc(dev, bus_name);
1842 * Helper function for implementing BHND_BUS_REGISTER_PROVIDER().
1844 * This implementation delegates the request to the BHND_BUS_REGISTER_PROVIDER()
1845 * method on the parent of @p dev. If no parent exists, the implementation
1846 * will return an error.
1849 bhnd_bus_generic_register_provider(device_t dev, device_t child,
1850 device_t provider, bhnd_service_t service)
1852 device_t parent = device_get_parent(dev);
1854 if (parent != NULL) {
1855 return (BHND_BUS_REGISTER_PROVIDER(parent, child,
1856 provider, service));
1863 * Helper function for implementing BHND_BUS_DEREGISTER_PROVIDER().
1865 * This implementation delegates the request to the
1866 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
1867 * exists, the implementation will panic.
1870 bhnd_bus_generic_deregister_provider(device_t dev, device_t child,
1871 device_t provider, bhnd_service_t service)
1873 device_t parent = device_get_parent(dev);
1875 if (parent != NULL) {
1876 return (BHND_BUS_DEREGISTER_PROVIDER(parent, child,
1877 provider, service));
1880 panic("missing BHND_BUS_DEREGISTER_PROVIDER()");
1884 * Helper function for implementing BHND_BUS_RETAIN_PROVIDER().
1886 * This implementation delegates the request to the
1887 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
1888 * exists, the implementation will return NULL.
1891 bhnd_bus_generic_retain_provider(device_t dev, device_t child,
1892 bhnd_service_t service)
1894 device_t parent = device_get_parent(dev);
1896 if (parent != NULL) {
1897 return (BHND_BUS_RETAIN_PROVIDER(parent, child,
1905 * Helper function for implementing BHND_BUS_RELEASE_PROVIDER().
1907 * This implementation delegates the request to the
1908 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
1909 * exists, the implementation will panic.
1912 bhnd_bus_generic_release_provider(device_t dev, device_t child,
1913 device_t provider, bhnd_service_t service)
1915 device_t parent = device_get_parent(dev);
1917 if (parent != NULL) {
1918 return (BHND_BUS_RELEASE_PROVIDER(parent, child,
1919 provider, service));
1922 panic("missing BHND_BUS_RELEASE_PROVIDER()");
1926 * Helper function for implementing BHND_BUS_REGISTER_PROVIDER().
1928 * This implementation uses the bhnd_service_registry_add() function to
1929 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
1930 * a suitable service registry to edit.
1933 bhnd_bus_generic_sr_register_provider(device_t dev, device_t child,
1934 device_t provider, bhnd_service_t service)
1936 struct bhnd_service_registry *bsr;
1938 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
1940 KASSERT(bsr != NULL, ("NULL service registry"));
1942 return (bhnd_service_registry_add(bsr, provider, service, 0));
1946 * Helper function for implementing BHND_BUS_DEREGISTER_PROVIDER().
1948 * This implementation uses the bhnd_service_registry_remove() function to
1949 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
1950 * a suitable service registry to edit.
1953 bhnd_bus_generic_sr_deregister_provider(device_t dev, device_t child,
1954 device_t provider, bhnd_service_t service)
1956 struct bhnd_service_registry *bsr;
1958 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
1960 KASSERT(bsr != NULL, ("NULL service registry"));
1962 return (bhnd_service_registry_remove(bsr, provider, service));
1966 * Helper function for implementing BHND_BUS_RETAIN_PROVIDER().
1968 * This implementation uses the bhnd_service_registry_retain() function to
1969 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
1970 * a suitable service registry.
1972 * If a local provider for the service is not available, and a parent device is
1973 * available, this implementation will attempt to fetch and locally register
1974 * a service provider reference from the parent of @p dev.
1977 bhnd_bus_generic_sr_retain_provider(device_t dev, device_t child,
1978 bhnd_service_t service)
1980 struct bhnd_service_registry *bsr;
1981 device_t parent, provider;
1984 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
1985 KASSERT(bsr != NULL, ("NULL service registry"));
1988 * Attempt to fetch a service provider reference from either the local
1989 * service registry, or if not found, from our parent.
1991 * If we fetch a provider from our parent, we register the provider
1992 * with the local service registry to prevent conflicting local
1993 * registrations from being added.
1996 /* Check the local service registry first */
1997 provider = bhnd_service_registry_retain(bsr, service);
1998 if (provider != NULL)
2001 /* Otherwise, try to delegate to our parent (if any) */
2002 if ((parent = device_get_parent(dev)) == NULL)
2005 provider = BHND_BUS_RETAIN_PROVIDER(parent, dev, service);
2006 if (provider == NULL)
2009 /* Register the inherited service registration with the local
2011 error = bhnd_service_registry_add(bsr, provider, service,
2012 BHND_SPF_INHERITED);
2014 BHND_BUS_RELEASE_PROVIDER(parent, dev, provider,
2016 if (error == EEXIST) {
2017 /* A valid service provider was registered
2018 * concurrently; retry fetching from the local
2023 device_printf(dev, "failed to register service "
2024 "provider: %d\n", error);
2031 * Helper function for implementing BHND_BUS_RELEASE_PROVIDER().
2033 * This implementation uses the bhnd_service_registry_release() function to
2034 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
2035 * a suitable service registry.
2038 bhnd_bus_generic_sr_release_provider(device_t dev, device_t child,
2039 device_t provider, bhnd_service_t service)
2041 struct bhnd_service_registry *bsr;
2043 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
2044 KASSERT(bsr != NULL, ("NULL service registry"));
2046 /* Release the provider reference; if the refcount hits zero on an
2047 * inherited reference, true will be returned, and we need to drop
2048 * our own bus reference to the provider */
2049 if (!bhnd_service_registry_release(bsr, provider, service))
2052 /* Drop our reference to the borrowed provider */
2053 BHND_BUS_RELEASE_PROVIDER(device_get_parent(dev), dev, provider,
2058 * Helper function for implementing BHND_BUS_IS_HW_DISABLED().
2060 * If a parent device is available, this implementation delegates the
2061 * request to the BHND_BUS_IS_HW_DISABLED() method on the parent of @p dev.
2063 * If no parent device is available (i.e. on a the bus root), the hardware
2064 * is assumed to be usable and false is returned.
2067 bhnd_bus_generic_is_hw_disabled(device_t dev, device_t child)
2069 if (device_get_parent(dev) != NULL)
2070 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child));
2076 * Helper function for implementing BHND_BUS_GET_CHIPID().
2078 * This implementation delegates the request to the BHND_BUS_GET_CHIPID()
2079 * method on the parent of @p dev. If no parent exists, the implementation
2082 const struct bhnd_chipid *
2083 bhnd_bus_generic_get_chipid(device_t dev, device_t child)
2085 if (device_get_parent(dev) != NULL)
2086 return (BHND_BUS_GET_CHIPID(device_get_parent(dev), child));
2088 panic("missing BHND_BUS_GET_CHIPID()");
2092 * Helper function for implementing BHND_BUS_GET_DMA_TRANSLATION().
2094 * If a parent device is available, this implementation delegates the
2095 * request to the BHND_BUS_GET_DMA_TRANSLATION() method on the parent of @p dev.
2097 * If no parent device is available, this implementation will panic.
2100 bhnd_bus_generic_get_dma_translation(device_t dev, device_t child, u_int width,
2101 uint32_t flags, bus_dma_tag_t *dmat,
2102 struct bhnd_dma_translation *translation)
2104 if (device_get_parent(dev) != NULL) {
2105 return (BHND_BUS_GET_DMA_TRANSLATION(device_get_parent(dev),
2106 child, width, flags, dmat, translation));
2109 panic("missing BHND_BUS_GET_DMA_TRANSLATION()");
2112 /* nvram board_info population macros for bhnd_bus_generic_read_board_info() */
2113 #define BHND_GV(_dest, _name) \
2114 bhnd_nvram_getvar_uint(child, BHND_NVAR_ ## _name, &_dest, \
2117 #define REQ_BHND_GV(_dest, _name) do { \
2118 if ((error = BHND_GV(_dest, _name))) { \
2119 device_printf(dev, \
2120 "error reading " __STRING(_name) ": %d\n", error); \
2125 #define OPT_BHND_GV(_dest, _name, _default) do { \
2126 if ((error = BHND_GV(_dest, _name))) { \
2127 if (error != ENOENT) { \
2128 device_printf(dev, \
2130 __STRING(_name) ": %d\n", error); \
2138 * Helper function for implementing BHND_BUS_READ_BOARDINFO().
2140 * This implementation populates @p info with information from NVRAM,
2141 * defaulting board_vendor and board_type fields to 0 if the
2142 * requested variables cannot be found.
2144 * This behavior is correct for most SoCs, but must be overridden on
2145 * bridged (PCI, PCMCIA, etc) devices to produce a complete bhnd_board_info
2149 bhnd_bus_generic_read_board_info(device_t dev, device_t child,
2150 struct bhnd_board_info *info)
2154 OPT_BHND_GV(info->board_vendor, BOARDVENDOR, 0);
2155 OPT_BHND_GV(info->board_type, BOARDTYPE, 0); /* srom >= 2 */
2156 OPT_BHND_GV(info->board_devid, DEVID, 0); /* srom >= 8 */
2157 REQ_BHND_GV(info->board_rev, BOARDREV);
2158 OPT_BHND_GV(info->board_srom_rev,SROMREV, 0); /* missing in
2161 REQ_BHND_GV(info->board_flags, BOARDFLAGS);
2162 OPT_BHND_GV(info->board_flags2, BOARDFLAGS2, 0); /* srom >= 4 */
2163 OPT_BHND_GV(info->board_flags3, BOARDFLAGS3, 0); /* srom >= 11 */
2173 * Helper function for implementing BHND_BUS_GET_NVRAM_VAR().
2175 * This implementation searches @p dev for a usable NVRAM child device.
2177 * If no usable child device is found on @p dev, the request is delegated to
2178 * the BHND_BUS_GET_NVRAM_VAR() method on the parent of @p dev.
2181 bhnd_bus_generic_get_nvram_var(device_t dev, device_t child, const char *name,
2182 void *buf, size_t *size, bhnd_nvram_type type)
2189 /* Look for a directly-attached NVRAM child */
2190 if ((nvram = device_find_child(dev, "bhnd_nvram", -1)) != NULL)
2191 return BHND_NVRAM_GETVAR(nvram, name, buf, size, type);
2193 /* Try to delegate to parent */
2194 if ((parent = device_get_parent(dev)) == NULL)
2197 return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), child,
2198 name, buf, size, type));
2202 * Helper function for implementing BHND_BUS_ALLOC_RESOURCE().
2204 * This implementation of BHND_BUS_ALLOC_RESOURCE() delegates allocation
2205 * of the underlying resource to BUS_ALLOC_RESOURCE(), and activation
2206 * to @p dev's BHND_BUS_ACTIVATE_RESOURCE().
2208 struct bhnd_resource *
2209 bhnd_bus_generic_alloc_resource(device_t dev, device_t child, int type,
2210 int *rid, rman_res_t start, rman_res_t end, rman_res_t count,
2213 struct bhnd_resource *br;
2214 struct resource *res;
2220 /* Allocate the real bus resource (without activating it) */
2221 res = BUS_ALLOC_RESOURCE(dev, child, type, rid, start, end, count,
2222 (flags & ~RF_ACTIVE));
2226 /* Allocate our bhnd resource wrapper. */
2227 br = malloc(sizeof(struct bhnd_resource), M_BHND, M_NOWAIT);
2234 /* Attempt activation */
2235 if (flags & RF_ACTIVE) {
2236 error = BHND_BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, br);
2245 BUS_RELEASE_RESOURCE(dev, child, type, *rid, res);
2252 * Helper function for implementing BHND_BUS_RELEASE_RESOURCE().
2254 * This implementation of BHND_BUS_RELEASE_RESOURCE() delegates release of
2255 * the backing resource to BUS_RELEASE_RESOURCE().
2258 bhnd_bus_generic_release_resource(device_t dev, device_t child, int type,
2259 int rid, struct bhnd_resource *r)
2263 if ((error = BUS_RELEASE_RESOURCE(dev, child, type, rid, r->res)))
2271 * Helper function for implementing BHND_BUS_ACTIVATE_RESOURCE().
2273 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() first calls the
2274 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
2276 * If this fails, and if @p dev is the direct parent of @p child, standard
2277 * resource activation is attempted via bus_activate_resource(). This enables
2278 * direct use of the bhnd(4) resource APIs on devices that may not be attached
2279 * to a parent bhnd bus or bridge.
2282 bhnd_bus_generic_activate_resource(device_t dev, device_t child, int type,
2283 int rid, struct bhnd_resource *r)
2288 passthrough = (device_get_parent(child) != dev);
2290 /* Try to delegate to the parent */
2291 if (device_get_parent(dev) != NULL) {
2292 error = BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev),
2293 child, type, rid, r);
2298 /* If bhnd(4) activation has failed and we're the child's direct
2299 * parent, try falling back on standard resource activation.
2301 if (error && !passthrough) {
2302 error = bus_activate_resource(child, type, rid, r->res);
2311 * Helper function for implementing BHND_BUS_DEACTIVATE_RESOURCE().
2313 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the
2314 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
2317 bhnd_bus_generic_deactivate_resource(device_t dev, device_t child,
2318 int type, int rid, struct bhnd_resource *r)
2320 if (device_get_parent(dev) != NULL)
2321 return (BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev),
2322 child, type, rid, r));
2328 * Helper function for implementing BHND_BUS_GET_INTR_DOMAIN().
2330 * This implementation simply returns the address of nearest bhnd(4) bus,
2331 * which may be @p dev; this behavior may be incompatible with FDT/OFW targets.
2334 bhnd_bus_generic_get_intr_domain(device_t dev, device_t child, bool self)
2336 return ((uintptr_t)dev);