2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
41 #include <sys/refcount.h>
42 #include <sys/systm.h>
44 #include <machine/bus.h>
46 #include <machine/resource.h>
48 #include <dev/bhnd/siba/sibareg.h>
50 #include <dev/bhnd/cores/chipc/chipcreg.h>
52 #include "nvram/bhnd_nvram.h"
54 #include "bhnd_chipc_if.h"
56 #include "bhnd_nvram_if.h"
57 #include "bhnd_nvram_map.h"
61 #include "bhnd_private.h"
63 static void bhnd_service_registry_free_entry(
64 struct bhnd_service_entry *entry);
66 static int compare_ascending_probe_order(const void *lhs, const void *rhs);
67 static int compare_descending_probe_order(const void *lhs,
70 /* BHND core device description table. */
71 static const struct bhnd_core_desc {
74 bhnd_devclass_t class;
76 } bhnd_core_descs[] = {
77 #define BHND_CDESC(_mfg, _cid, _cls, _desc) \
78 { BHND_MFGID_ ## _mfg, BHND_COREID_ ## _cid, \
79 BHND_DEVCLASS_ ## _cls, _desc }
81 BHND_CDESC(BCM, CC, CC, "ChipCommon I/O Controller"),
82 BHND_CDESC(BCM, ILINE20, OTHER, "iLine20 HPNA"),
83 BHND_CDESC(BCM, SRAM, RAM, "SRAM"),
84 BHND_CDESC(BCM, SDRAM, RAM, "SDRAM"),
85 BHND_CDESC(BCM, PCI, PCI, "PCI Bridge"),
86 BHND_CDESC(BCM, MIPS, CPU, "BMIPS CPU"),
87 BHND_CDESC(BCM, ENET, ENET_MAC, "Fast Ethernet MAC"),
88 BHND_CDESC(BCM, V90_CODEC, SOFTMODEM, "V.90 SoftModem Codec"),
89 BHND_CDESC(BCM, USB, USB_DUAL, "USB 1.1 Device/Host Controller"),
90 BHND_CDESC(BCM, ADSL, OTHER, "ADSL Core"),
91 BHND_CDESC(BCM, ILINE100, OTHER, "iLine100 HPNA"),
92 BHND_CDESC(BCM, IPSEC, OTHER, "IPsec Accelerator"),
93 BHND_CDESC(BCM, UTOPIA, OTHER, "UTOPIA ATM Core"),
94 BHND_CDESC(BCM, PCMCIA, PCCARD, "PCMCIA Bridge"),
95 BHND_CDESC(BCM, SOCRAM, RAM, "Internal Memory"),
96 BHND_CDESC(BCM, MEMC, MEMC, "MEMC SDRAM Controller"),
97 BHND_CDESC(BCM, OFDM, OTHER, "OFDM PHY"),
98 BHND_CDESC(BCM, EXTIF, OTHER, "External Interface"),
99 BHND_CDESC(BCM, D11, WLAN, "802.11 MAC/PHY/Radio"),
100 BHND_CDESC(BCM, APHY, WLAN_PHY, "802.11a PHY"),
101 BHND_CDESC(BCM, BPHY, WLAN_PHY, "802.11b PHY"),
102 BHND_CDESC(BCM, GPHY, WLAN_PHY, "802.11g PHY"),
103 BHND_CDESC(BCM, MIPS33, CPU, "BMIPS33 CPU"),
104 BHND_CDESC(BCM, USB11H, USB_HOST, "USB 1.1 Host Controller"),
105 BHND_CDESC(BCM, USB11D, USB_DEV, "USB 1.1 Device Controller"),
106 BHND_CDESC(BCM, USB20H, USB_HOST, "USB 2.0 Host Controller"),
107 BHND_CDESC(BCM, USB20D, USB_DEV, "USB 2.0 Device Controller"),
108 BHND_CDESC(BCM, SDIOH, OTHER, "SDIO Host Controller"),
109 BHND_CDESC(BCM, ROBO, OTHER, "RoboSwitch"),
110 BHND_CDESC(BCM, ATA100, OTHER, "Parallel ATA Controller"),
111 BHND_CDESC(BCM, SATAXOR, OTHER, "SATA DMA/XOR Controller"),
112 BHND_CDESC(BCM, GIGETH, ENET_MAC, "Gigabit Ethernet MAC"),
113 BHND_CDESC(BCM, PCIE, PCIE, "PCIe Bridge"),
114 BHND_CDESC(BCM, NPHY, WLAN_PHY, "802.11n 2x2 PHY"),
115 BHND_CDESC(BCM, SRAMC, MEMC, "SRAM Controller"),
116 BHND_CDESC(BCM, MINIMAC, OTHER, "MINI MAC/PHY"),
117 BHND_CDESC(BCM, ARM11, CPU, "ARM1176 CPU"),
118 BHND_CDESC(BCM, ARM7S, CPU, "ARM7TDMI-S CPU"),
119 BHND_CDESC(BCM, LPPHY, WLAN_PHY, "802.11a/b/g PHY"),
120 BHND_CDESC(BCM, PMU, PMU, "PMU"),
121 BHND_CDESC(BCM, SSNPHY, WLAN_PHY, "802.11n Single-Stream PHY"),
122 BHND_CDESC(BCM, SDIOD, OTHER, "SDIO Device Core"),
123 BHND_CDESC(BCM, ARMCM3, CPU, "ARM Cortex-M3 CPU"),
124 BHND_CDESC(BCM, HTPHY, WLAN_PHY, "802.11n 4x4 PHY"),
125 BHND_CDESC(MIPS,MIPS74K, CPU, "MIPS74k CPU"),
126 BHND_CDESC(BCM, GMAC, ENET_MAC, "Gigabit MAC core"),
127 BHND_CDESC(BCM, DMEMC, MEMC, "DDR1/DDR2 Memory Controller"),
128 BHND_CDESC(BCM, PCIERC, OTHER, "PCIe Root Complex"),
129 BHND_CDESC(BCM, OCP, SOC_BRIDGE, "OCP to OCP Bridge"),
130 BHND_CDESC(BCM, SC, OTHER, "Shared Common Core"),
131 BHND_CDESC(BCM, AHB, SOC_BRIDGE, "OCP to AHB Bridge"),
132 BHND_CDESC(BCM, SPIH, OTHER, "SPI Host Controller"),
133 BHND_CDESC(BCM, I2S, OTHER, "I2S Digital Audio Interface"),
134 BHND_CDESC(BCM, DMEMS, MEMC, "SDR/DDR1 Memory Controller"),
135 BHND_CDESC(BCM, UBUS_SHIM, OTHER, "BCM6362/UBUS WLAN SHIM"),
136 BHND_CDESC(BCM, PCIE2, PCIE, "PCIe Bridge (Gen2)"),
138 BHND_CDESC(ARM, APB_BRIDGE, SOC_BRIDGE, "BP135 AMBA3 AXI to APB Bridge"),
139 BHND_CDESC(ARM, PL301, SOC_ROUTER, "PL301 AMBA3 Interconnect"),
140 BHND_CDESC(ARM, EROM, EROM, "PL366 Device Enumeration ROM"),
141 BHND_CDESC(ARM, OOB_ROUTER, OTHER, "PL367 OOB Interrupt Router"),
142 BHND_CDESC(ARM, AXI_UNMAPPED, OTHER, "Unmapped Address Ranges"),
144 BHND_CDESC(BCM, 4706_CC, CC, "ChipCommon I/O Controller"),
145 BHND_CDESC(BCM, NS_PCIE2, PCIE, "PCIe Bridge (Gen2)"),
146 BHND_CDESC(BCM, NS_DMA, OTHER, "DMA engine"),
147 BHND_CDESC(BCM, NS_SDIO, OTHER, "SDIO 3.0 Host Controller"),
148 BHND_CDESC(BCM, NS_USB20H, USB_HOST, "USB 2.0 Host Controller"),
149 BHND_CDESC(BCM, NS_USB30H, USB_HOST, "USB 3.0 Host Controller"),
150 BHND_CDESC(BCM, NS_A9JTAG, OTHER, "ARM Cortex A9 JTAG Interface"),
151 BHND_CDESC(BCM, NS_DDR23_MEMC, MEMC, "Denali DDR2/DD3 Memory Controller"),
152 BHND_CDESC(BCM, NS_ROM, NVRAM, "System ROM"),
153 BHND_CDESC(BCM, NS_NAND, NVRAM, "NAND Flash Controller"),
154 BHND_CDESC(BCM, NS_QSPI, NVRAM, "QSPI Flash Controller"),
155 BHND_CDESC(BCM, NS_CC_B, CC_B, "ChipCommon B Auxiliary I/O Controller"),
156 BHND_CDESC(BCM, 4706_SOCRAM, RAM, "Internal Memory"),
157 BHND_CDESC(BCM, IHOST_ARMCA9, CPU, "ARM Cortex A9 CPU"),
158 BHND_CDESC(BCM, 4706_GMAC_CMN, ENET, "Gigabit MAC (Common)"),
159 BHND_CDESC(BCM, 4706_GMAC, ENET_MAC, "Gigabit MAC"),
160 BHND_CDESC(BCM, AMEMC, MEMC, "Denali DDR1/DDR2 Memory Controller"),
163 /* Derived from inspection of the BCM4331 cores that provide PrimeCell
164 * IDs. Due to lack of documentation, the surmised device name/purpose
165 * provided here may be incorrect. */
166 { BHND_MFGID_ARM, BHND_PRIMEID_EROM, BHND_DEVCLASS_OTHER,
167 "PL364 Device Enumeration ROM" },
168 { BHND_MFGID_ARM, BHND_PRIMEID_SWRAP, BHND_DEVCLASS_OTHER,
169 "PL368 Device Management Interface" },
170 { BHND_MFGID_ARM, BHND_PRIMEID_MWRAP, BHND_DEVCLASS_OTHER,
171 "PL369 Device Management Interface" },
176 static const struct bhnd_device_quirk bhnd_chipc_clkctl_quirks[];
177 static const struct bhnd_device_quirk bhnd_pcmcia_clkctl_quirks[];
180 * Device table entries for core-specific CLKCTL quirk lookup.
182 static const struct bhnd_device bhnd_clkctl_devices[] = {
183 BHND_DEVICE(BCM, CC, NULL, bhnd_chipc_clkctl_quirks),
184 BHND_DEVICE(BCM, PCMCIA, NULL, bhnd_pcmcia_clkctl_quirks),
188 /** ChipCommon CLKCTL quirks */
189 static const struct bhnd_device_quirk bhnd_chipc_clkctl_quirks[] = {
190 /* HTAVAIL/ALPAVAIL are bitswapped in chipc's CLKCTL */
191 BHND_CHIP_QUIRK(4328, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),
192 BHND_CHIP_QUIRK(5354, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),
193 BHND_DEVICE_QUIRK_END
196 /** PCMCIA CLKCTL quirks */
197 static const struct bhnd_device_quirk bhnd_pcmcia_clkctl_quirks[] = {
198 /* HTAVAIL/ALPAVAIL are bitswapped in pcmcia's CLKCTL */
199 BHND_CHIP_QUIRK(4328, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),
200 BHND_CHIP_QUIRK(5354, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),
201 BHND_DEVICE_QUIRK_END
205 * Return the name for a given JEP106 manufacturer ID.
207 * @param vendor A JEP106 Manufacturer ID, including the non-standard ARM 4-bit
208 * JEP106 continuation code.
211 bhnd_vendor_name(uint16_t vendor)
218 case BHND_MFGID_MIPS:
226 * Return the name of a port type.
228 * @param port_type The port type to look up.
231 bhnd_port_type_name(bhnd_port_type port_type)
234 case BHND_PORT_DEVICE:
236 case BHND_PORT_BRIDGE:
238 case BHND_PORT_AGENT:
246 * Return the name of an NVRAM source.
248 * @param nvram_src The NVRAM source type to look up.
251 bhnd_nvram_src_name(bhnd_nvram_src nvram_src)
254 case BHND_NVRAM_SRC_FLASH:
256 case BHND_NVRAM_SRC_OTP:
258 case BHND_NVRAM_SRC_SPROM:
260 case BHND_NVRAM_SRC_UNKNOWN:
267 static const struct bhnd_core_desc *
268 bhnd_find_core_desc(uint16_t vendor, uint16_t device)
270 for (u_int i = 0; bhnd_core_descs[i].desc != NULL; i++) {
271 if (bhnd_core_descs[i].vendor != vendor)
274 if (bhnd_core_descs[i].device != device)
277 return (&bhnd_core_descs[i]);
284 * Return a human-readable name for a BHND core.
286 * @param vendor The core designer's JEDEC-106 Manufacturer ID.
287 * @param device The core identifier.
290 bhnd_find_core_name(uint16_t vendor, uint16_t device)
292 const struct bhnd_core_desc *desc;
294 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)
301 * Return the device class for a BHND core.
303 * @param vendor The core designer's JEDEC-106 Manufacturer ID.
304 * @param device The core identifier.
307 bhnd_find_core_class(uint16_t vendor, uint16_t device)
309 const struct bhnd_core_desc *desc;
311 if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)
312 return (BHND_DEVCLASS_OTHER);
318 * Return a human-readable name for a BHND core.
320 * @param ci The core's info record.
323 bhnd_core_name(const struct bhnd_core_info *ci)
325 return bhnd_find_core_name(ci->vendor, ci->device);
329 * Return the device class for a BHND core.
331 * @param ci The core's info record.
334 bhnd_core_class(const struct bhnd_core_info *ci)
336 return bhnd_find_core_class(ci->vendor, ci->device);
340 * Write a human readable name representation of the given
341 * BHND_CHIPID_* constant to @p buffer.
343 * @param buffer Output buffer, or NULL to compute the required size.
344 * @param size Capacity of @p buffer, in bytes.
345 * @param chip_id Chip ID to be formatted.
347 * @return The required number of bytes on success, or a negative integer on
348 * failure. No more than @p size-1 characters be written, with the @p size'th
351 * @sa BHND_CHIPID_MAX_NAMELEN
354 bhnd_format_chip_id(char *buffer, size_t size, uint16_t chip_id)
356 /* All hex formatted IDs are within the range of 0x4000-0x9C3F (40000-1) */
357 if (chip_id >= 0x4000 && chip_id <= 0x9C3F)
358 return (snprintf(buffer, size, "BCM%hX", chip_id));
360 return (snprintf(buffer, size, "BCM%hu", chip_id));
364 * Return a core info record populated from a bhnd-attached @p dev.
366 * @param dev A bhnd device.
368 * @return A core info record for @p dev.
370 struct bhnd_core_info
371 bhnd_get_core_info(device_t dev) {
372 return (struct bhnd_core_info) {
373 .vendor = bhnd_get_vendor(dev),
374 .device = bhnd_get_device(dev),
375 .hwrev = bhnd_get_hwrev(dev),
376 .core_idx = bhnd_get_core_index(dev),
377 .unit = bhnd_get_core_unit(dev)
382 * Find a @p class child device with @p unit on @p bus.
384 * @param bus The bhnd-compatible bus to be searched.
385 * @param class The device class to match on.
386 * @param unit The core unit number; specify -1 to return the first match
387 * regardless of unit number.
389 * @retval device_t if a matching child device is found.
390 * @retval NULL if no matching child device is found.
393 bhnd_bus_find_child(device_t bus, bhnd_devclass_t class, int unit)
395 struct bhnd_core_match md = {
396 BHND_MATCH_CORE_CLASS(class),
397 BHND_MATCH_CORE_UNIT(unit)
401 md.m.match.core_unit = 0;
403 return bhnd_bus_match_child(bus, &md);
407 * Find the first child device on @p bus that matches @p desc.
409 * @param bus The bhnd-compatible bus to be searched.
410 * @param desc A match descriptor.
412 * @retval device_t if a matching child device is found.
413 * @retval NULL if no matching child device is found.
416 bhnd_bus_match_child(device_t bus, const struct bhnd_core_match *desc)
423 error = device_get_children(bus, &devlistp, &devcnt);
428 for (int i = 0; i < devcnt; i++) {
429 struct bhnd_core_info ci = bhnd_get_core_info(devlistp[i]);
431 if (bhnd_core_matches(&ci, desc)) {
438 free(devlistp, M_TEMP);
443 * Retrieve an ordered list of all device instances currently connected to
444 * @p bus, returning a pointer to the array in @p devlistp and the count
447 * The memory allocated for the table must be freed via
448 * bhnd_bus_free_children().
450 * @param bus The bhnd-compatible bus to be queried.
451 * @param[out] devlist The array of devices.
452 * @param[out] devcount The number of devices in @p devlistp
453 * @param order The order in which devices will be returned
457 * @retval non-zero if an error occurs, a regular unix error code will
461 bhnd_bus_get_children(device_t bus, device_t **devlist, int *devcount,
462 bhnd_device_order order)
466 /* Fetch device array */
467 if ((error = device_get_children(bus, devlist, devcount)))
470 /* Perform requested sorting */
471 if ((error = bhnd_sort_devices(*devlist, *devcount, order))) {
472 bhnd_bus_free_children(*devlist);
480 * Free any memory allocated in a previous call to bhnd_bus_get_children().
482 * @param devlist The device array returned by bhnd_bus_get_children().
485 bhnd_bus_free_children(device_t *devlist)
487 free(devlist, M_TEMP);
491 * Perform in-place sorting of an array of bhnd device instances.
493 * @param devlist An array of bhnd devices.
494 * @param devcount The number of devices in @p devs.
495 * @param order The sort order to be used.
498 * @retval EINVAL if the sort order is unknown.
501 bhnd_sort_devices(device_t *devlist, size_t devcount, bhnd_device_order order)
503 int (*compare)(const void *, const void *);
506 case BHND_DEVICE_ORDER_ATTACH:
507 compare = compare_ascending_probe_order;
509 case BHND_DEVICE_ORDER_DETACH:
510 compare = compare_descending_probe_order;
513 printf("unknown sort order: %d\n", order);
517 qsort(devlist, devcount, sizeof(*devlist), compare);
522 * Ascending comparison of bhnd device's probe order.
525 compare_ascending_probe_order(const void *lhs, const void *rhs)
530 ldev = (*(const device_t *) lhs);
531 rdev = (*(const device_t *) rhs);
533 lorder = BHND_BUS_GET_PROBE_ORDER(device_get_parent(ldev), ldev);
534 rorder = BHND_BUS_GET_PROBE_ORDER(device_get_parent(rdev), rdev);
536 if (lorder < rorder) {
538 } else if (lorder > rorder) {
546 * Descending comparison of bhnd device's probe order.
549 compare_descending_probe_order(const void *lhs, const void *rhs)
551 return (compare_ascending_probe_order(rhs, lhs));
555 * Call device_probe_and_attach() for each of the bhnd bus device's
556 * children, in bhnd attach order.
558 * @param bus The bhnd-compatible bus for which all children should be probed
562 bhnd_bus_probe_children(device_t bus)
568 /* Fetch children in attach order */
569 error = bhnd_bus_get_children(bus, &devs, &ndevs,
570 BHND_DEVICE_ORDER_ATTACH);
574 /* Probe and attach all children */
575 for (int i = 0; i < ndevs; i++) {
576 device_t child = devs[i];
577 device_probe_and_attach(child);
580 bhnd_bus_free_children(devs);
586 * Walk up the bhnd device hierarchy to locate the root device
587 * to which the bhndb bridge is attached.
589 * This can be used from within bhnd host bridge drivers to locate the
590 * actual upstream host device.
592 * @param dev A bhnd device.
593 * @param bus_class The expected bus (e.g. "pci") to which the bridge root
594 * should be attached.
596 * @retval device_t if a matching parent device is found.
597 * @retval NULL if @p dev is not attached via a bhndb bus.
598 * @retval NULL if no parent device is attached via @p bus_class.
601 bhnd_find_bridge_root(device_t dev, devclass_t bus_class)
603 devclass_t bhndb_class;
606 KASSERT(device_get_devclass(device_get_parent(dev)) == bhnd_devclass,
607 ("%s not a bhnd device", device_get_nameunit(dev)));
609 bhndb_class = devclass_find("bhndb");
611 /* Walk the device tree until we hit a bridge */
613 while ((parent = device_get_parent(parent)) != NULL) {
614 if (device_get_devclass(parent) == bhndb_class)
622 /* Search for a parent attached to the expected bus class */
623 while ((parent = device_get_parent(parent)) != NULL) {
626 bus = device_get_parent(parent);
627 if (bus != NULL && device_get_devclass(bus) == bus_class)
636 * Find the first core in @p cores that matches @p desc.
638 * @param cores The table to search.
639 * @param num_cores The length of @p cores.
640 * @param desc A match descriptor.
642 * @retval bhnd_core_info if a matching core is found.
643 * @retval NULL if no matching core is found.
645 const struct bhnd_core_info *
646 bhnd_match_core(const struct bhnd_core_info *cores, u_int num_cores,
647 const struct bhnd_core_match *desc)
649 for (u_int i = 0; i < num_cores; i++) {
650 if (bhnd_core_matches(&cores[i], desc))
659 * Find the first core in @p cores with the given @p class.
661 * @param cores The table to search.
662 * @param num_cores The length of @p cores.
663 * @param class The device class to match on.
665 * @retval non-NULL if a matching core is found.
666 * @retval NULL if no matching core is found.
668 const struct bhnd_core_info *
669 bhnd_find_core(const struct bhnd_core_info *cores, u_int num_cores,
670 bhnd_devclass_t class)
672 struct bhnd_core_match md = {
673 BHND_MATCH_CORE_CLASS(class)
676 return bhnd_match_core(cores, num_cores, &md);
681 * Create an equality match descriptor for @p core.
683 * @param core The core info to be matched on.
685 * @return an equality match descriptor for @p core.
687 struct bhnd_core_match
688 bhnd_core_get_match_desc(const struct bhnd_core_info *core)
690 return ((struct bhnd_core_match) {
691 BHND_MATCH_CORE_VENDOR(core->vendor),
692 BHND_MATCH_CORE_ID(core->device),
693 BHND_MATCH_CORE_REV(HWREV_EQ(core->hwrev)),
694 BHND_MATCH_CORE_CLASS(bhnd_core_class(core)),
695 BHND_MATCH_CORE_IDX(core->core_idx),
696 BHND_MATCH_CORE_UNIT(core->unit)
702 * Return true if the @p lhs is equal to @p rhs.
704 * @param lhs The first bhnd core descriptor to compare.
705 * @param rhs The second bhnd core descriptor to compare.
707 * @retval true if @p lhs is equal to @p rhs
708 * @retval false if @p lhs is not equal to @p rhs
711 bhnd_cores_equal(const struct bhnd_core_info *lhs,
712 const struct bhnd_core_info *rhs)
714 struct bhnd_core_match md;
716 /* Use an equality match descriptor to perform the comparison */
717 md = bhnd_core_get_match_desc(rhs);
718 return (bhnd_core_matches(lhs, &md));
722 * Return true if the @p core matches @p desc.
724 * @param core A bhnd core descriptor.
725 * @param desc A match descriptor to compare against @p core.
727 * @retval true if @p core matches @p match.
728 * @retval false if @p core does not match @p match.
731 bhnd_core_matches(const struct bhnd_core_info *core,
732 const struct bhnd_core_match *desc)
734 if (desc->m.match.core_vendor && desc->core_vendor != core->vendor)
737 if (desc->m.match.core_id && desc->core_id != core->device)
740 if (desc->m.match.core_unit && desc->core_unit != core->unit)
743 if (desc->m.match.core_rev &&
744 !bhnd_hwrev_matches(core->hwrev, &desc->core_rev))
747 if (desc->m.match.core_idx && desc->core_idx != core->core_idx)
750 if (desc->m.match.core_class &&
751 desc->core_class != bhnd_core_class(core))
758 * Return true if the @p chip matches @p desc.
760 * @param chip A bhnd chip identifier.
761 * @param desc A match descriptor to compare against @p chip.
763 * @retval true if @p chip matches @p match.
764 * @retval false if @p chip does not match @p match.
767 bhnd_chip_matches(const struct bhnd_chipid *chip,
768 const struct bhnd_chip_match *desc)
770 if (desc->m.match.chip_id && chip->chip_id != desc->chip_id)
773 if (desc->m.match.chip_pkg && chip->chip_pkg != desc->chip_pkg)
776 if (desc->m.match.chip_rev &&
777 !bhnd_hwrev_matches(chip->chip_rev, &desc->chip_rev))
780 if (desc->m.match.chip_type && chip->chip_type != desc->chip_type)
787 * Return true if the @p board matches @p desc.
789 * @param board The bhnd board info.
790 * @param desc A match descriptor to compare against @p board.
792 * @retval true if @p chip matches @p match.
793 * @retval false if @p chip does not match @p match.
796 bhnd_board_matches(const struct bhnd_board_info *board,
797 const struct bhnd_board_match *desc)
799 if (desc->m.match.board_srom_rev &&
800 !bhnd_hwrev_matches(board->board_srom_rev, &desc->board_srom_rev))
803 if (desc->m.match.board_vendor &&
804 board->board_vendor != desc->board_vendor)
807 if (desc->m.match.board_type && board->board_type != desc->board_type)
810 if (desc->m.match.board_devid &&
811 board->board_devid != desc->board_devid)
814 if (desc->m.match.board_rev &&
815 !bhnd_hwrev_matches(board->board_rev, &desc->board_rev))
822 * Return true if the @p hwrev matches @p desc.
824 * @param hwrev A bhnd hardware revision.
825 * @param desc A match descriptor to compare against @p core.
827 * @retval true if @p hwrev matches @p match.
828 * @retval false if @p hwrev does not match @p match.
831 bhnd_hwrev_matches(uint16_t hwrev, const struct bhnd_hwrev_match *desc)
833 if (desc->start != BHND_HWREV_INVALID &&
837 if (desc->end != BHND_HWREV_INVALID &&
845 * Return true if the @p dev matches @p desc.
847 * @param dev A bhnd device.
848 * @param desc A match descriptor to compare against @p dev.
850 * @retval true if @p dev matches @p match.
851 * @retval false if @p dev does not match @p match.
854 bhnd_device_matches(device_t dev, const struct bhnd_device_match *desc)
856 struct bhnd_core_info core;
857 const struct bhnd_chipid *chip;
858 struct bhnd_board_info board;
862 /* Construct individual match descriptors */
863 struct bhnd_core_match m_core = { _BHND_CORE_MATCH_COPY(desc) };
864 struct bhnd_chip_match m_chip = { _BHND_CHIP_MATCH_COPY(desc) };
865 struct bhnd_board_match m_board = { _BHND_BOARD_MATCH_COPY(desc) };
867 /* Fetch and match core info */
868 if (m_core.m.match_flags) {
869 /* Only applicable to bhnd-attached cores */
870 parent = device_get_parent(dev);
871 if (device_get_devclass(parent) != bhnd_devclass) {
872 device_printf(dev, "attempting to match core "
873 "attributes against non-core device\n");
877 core = bhnd_get_core_info(dev);
878 if (!bhnd_core_matches(&core, &m_core))
882 /* Fetch and match chip info */
883 if (m_chip.m.match_flags) {
884 chip = bhnd_get_chipid(dev);
886 if (!bhnd_chip_matches(chip, &m_chip))
890 /* Fetch and match board info.
892 * This is not available until after NVRAM is up; earlier device
893 * matches should not include board requirements */
894 if (m_board.m.match_flags) {
895 if ((error = bhnd_read_board_info(dev, &board))) {
896 device_printf(dev, "failed to read required board info "
897 "during device matching: %d\n", error);
901 if (!bhnd_board_matches(&board, &m_board))
910 * Search @p table for an entry matching @p dev.
912 * @param dev A bhnd device to match against @p table.
913 * @param table The device table to search.
914 * @param entry_size The @p table entry size, in bytes.
916 * @retval non-NULL the first matching device, if any.
917 * @retval NULL if no matching device is found in @p table.
919 const struct bhnd_device *
920 bhnd_device_lookup(device_t dev, const struct bhnd_device *table,
923 const struct bhnd_device *entry;
924 device_t hostb, parent;
925 bhnd_attach_type attach_type;
928 parent = device_get_parent(dev);
929 hostb = bhnd_bus_find_hostb_device(parent);
930 attach_type = bhnd_get_attach_type(dev);
932 for (entry = table; !BHND_DEVICE_IS_END(entry); entry =
933 (const struct bhnd_device *) ((const char *) entry + entry_size))
935 /* match core info */
936 if (!bhnd_device_matches(dev, &entry->core))
939 /* match device flags */
940 dflags = entry->device_flags;
942 /* hostb implies BHND_ATTACH_ADAPTER requirement */
943 if (dflags & BHND_DF_HOSTB)
944 dflags |= BHND_DF_ADAPTER;
946 if (dflags & BHND_DF_ADAPTER)
947 if (attach_type != BHND_ATTACH_ADAPTER)
950 if (dflags & BHND_DF_HOSTB)
954 if (dflags & BHND_DF_SOC)
955 if (attach_type != BHND_ATTACH_NATIVE)
967 * Scan the device @p table for all quirk flags applicable to @p dev.
969 * @param dev A bhnd device to match against @p table.
970 * @param table The device table to search.
971 * @param entry_size The @p table entry size, in bytes.
973 * @return all matching quirk flags.
976 bhnd_device_quirks(device_t dev, const struct bhnd_device *table,
979 const struct bhnd_device *dent;
980 const struct bhnd_device_quirk *qent, *qtable;
983 /* Locate the device entry */
984 if ((dent = bhnd_device_lookup(dev, table, entry_size)) == NULL)
987 /* Quirks table is optional */
988 qtable = dent->quirks_table;
992 /* Collect matching device quirk entries */
994 for (qent = qtable; !BHND_DEVICE_QUIRK_IS_END(qent); qent++) {
995 if (bhnd_device_matches(dev, &qent->desc))
996 quirks |= qent->quirks;
1004 * Allocate bhnd(4) resources defined in @p rs from a parent bus.
1006 * @param dev The device requesting ownership of the resources.
1007 * @param rs A standard bus resource specification. This will be updated
1008 * with the allocated resource's RIDs.
1009 * @param res On success, the allocated bhnd resources.
1012 * @retval non-zero if allocation of any non-RF_OPTIONAL resource fails,
1013 * all allocated resources will be released and a regular
1014 * unix error code will be returned.
1017 bhnd_alloc_resources(device_t dev, struct resource_spec *rs,
1018 struct bhnd_resource **res)
1020 /* Initialize output array */
1021 for (u_int i = 0; rs[i].type != -1; i++)
1024 for (u_int i = 0; rs[i].type != -1; i++) {
1025 res[i] = bhnd_alloc_resource_any(dev, rs[i].type, &rs[i].rid,
1028 /* Clean up all allocations on failure */
1029 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
1030 bhnd_release_resources(dev, rs, res);
1039 * Release bhnd(4) resources defined in @p rs from a parent bus.
1041 * @param dev The device that owns the resources.
1042 * @param rs A standard bus resource specification previously initialized
1043 * by @p bhnd_alloc_resources.
1044 * @param res The bhnd resources to be released.
1047 bhnd_release_resources(device_t dev, const struct resource_spec *rs,
1048 struct bhnd_resource **res)
1050 for (u_int i = 0; rs[i].type != -1; i++) {
1054 bhnd_release_resource(dev, rs[i].type, rs[i].rid, res[i]);
1060 * Allocate and return a new per-core PMU clock control/status (clkctl)
1061 * instance for @p dev.
1063 * @param dev The bhnd(4) core device mapped by @p r.
1064 * @param pmu_dev The bhnd(4) PMU device, implmenting the bhnd_pmu_if
1065 * interface. The caller is responsible for ensuring that
1066 * this reference remains valid for the lifetime of the
1067 * returned clkctl instance.
1068 * @param r A resource mapping the core's clock control register
1069 * (see BHND_CLK_CTL_ST). The caller is responsible for
1070 * ensuring that this resource remains valid for the
1071 * lifetime of the returned clkctl instance.
1072 * @param offset The offset to the clock control register within @p r.
1073 * @param max_latency The PMU's maximum state transition latency in
1074 * microseconds; this upper bound will be used to busy-wait
1075 * on PMU state transitions.
1077 * @retval non-NULL success
1078 * @retval NULL if allocation fails.
1081 struct bhnd_core_clkctl *
1082 bhnd_alloc_core_clkctl(device_t dev, device_t pmu_dev, struct bhnd_resource *r,
1083 bus_size_t offset, u_int max_latency)
1085 struct bhnd_core_clkctl *clkctl;
1087 clkctl = malloc(sizeof(*clkctl), M_BHND, M_ZERO | M_NOWAIT);
1091 clkctl->cc_dev = dev;
1092 clkctl->cc_pmu_dev = pmu_dev;
1094 clkctl->cc_res_offset = offset;
1095 clkctl->cc_max_latency = max_latency;
1096 clkctl->cc_quirks = bhnd_device_quirks(dev, bhnd_clkctl_devices,
1097 sizeof(bhnd_clkctl_devices[0]));
1099 BHND_CLKCTL_LOCK_INIT(clkctl);
1105 * Free a clkctl instance previously allocated via bhnd_alloc_core_clkctl().
1107 * @param clkctl The clkctl instance to be freed.
1110 bhnd_free_core_clkctl(struct bhnd_core_clkctl *clkctl)
1112 BHND_CLKCTL_LOCK_DESTROY(clkctl);
1114 free(clkctl, M_BHND);
1118 * Wait for the per-core clock status to be equal to @p value after
1119 * applying @p mask, timing out after the maximum transition latency is reached.
1121 * @param clkctl Per-core clkctl state to be queryied.
1122 * @param value Value to wait for.
1123 * @param mask Mask to apply prior to value comparison.
1126 * @retval ETIMEDOUT if the PMU's maximum transition delay is reached before
1127 * the clock status matches @p value and @p mask.
1130 bhnd_core_clkctl_wait(struct bhnd_core_clkctl *clkctl, uint32_t value,
1135 BHND_CLKCTL_LOCK_ASSERT(clkctl, MA_OWNED);
1137 /* Bitswapped HTAVAIL/ALPAVAIL work-around */
1138 if (clkctl->cc_quirks & BHND_CLKCTL_QUIRK_CCS0) {
1139 uint32_t fmask, fval;
1141 fmask = mask & ~(BHND_CCS_HTAVAIL | BHND_CCS_ALPAVAIL);
1142 fval = value & ~(BHND_CCS_HTAVAIL | BHND_CCS_ALPAVAIL);
1144 if (mask & BHND_CCS_HTAVAIL)
1145 fmask |= BHND_CCS0_HTAVAIL;
1146 if (value & BHND_CCS_HTAVAIL)
1147 fval |= BHND_CCS0_HTAVAIL;
1149 if (mask & BHND_CCS_ALPAVAIL)
1150 fmask |= BHND_CCS0_ALPAVAIL;
1151 if (value & BHND_CCS_ALPAVAIL)
1152 fval |= BHND_CCS0_ALPAVAIL;
1158 for (u_int i = 0; i < clkctl->cc_max_latency; i += 10) {
1159 clkst = bhnd_bus_read_4(clkctl->cc_res, clkctl->cc_res_offset);
1160 if ((clkst & mask) == (value & mask))
1166 device_printf(clkctl->cc_dev, "clkst wait timeout (value=%#x, "
1167 "mask=%#x)\n", value, mask);
1173 * Read an NVRAM variable's NUL-terminated string value.
1175 * @param dev A bhnd bus child device.
1176 * @param name The NVRAM variable name.
1177 * @param[out] buf A buffer large enough to hold @p len bytes. On
1178 * success, the NUL-terminated string value will be
1179 * written to this buffer. This argment may be NULL if
1180 * the value is not desired.
1181 * @param len The maximum capacity of @p buf.
1182 * @param[out] rlen On success, will be set to the actual size of
1183 * the requested value (including NUL termination). This
1184 * argment may be NULL if the size is not desired.
1187 * @retval ENOENT The requested variable was not found.
1188 * @retval ENODEV No valid NVRAM source could be found.
1189 * @retval ENOMEM If @p buf is non-NULL and a buffer of @p len is too
1190 * small to hold the requested value.
1191 * @retval EFTYPE If the variable data cannot be coerced to a valid
1192 * string representation.
1193 * @retval ERANGE If value coercion would overflow @p type.
1194 * @retval non-zero If reading @p name otherwise fails, a regular unix
1195 * error code will be returned.
1198 bhnd_nvram_getvar_str(device_t dev, const char *name, char *buf, size_t len,
1205 error = bhnd_nvram_getvar(dev, name, buf, &larg,
1206 BHND_NVRAM_TYPE_STRING);
1214 * Read an NVRAM variable's unsigned integer value.
1216 * @param dev A bhnd bus child device.
1217 * @param name The NVRAM variable name.
1218 * @param[out] value On success, the requested value will be written
1220 * @param width The output integer type width (1, 2, or
1224 * @retval ENOENT The requested variable was not found.
1225 * @retval ENODEV No valid NVRAM source could be found.
1226 * @retval EFTYPE If the variable data cannot be coerced to a
1227 * a valid unsigned integer representation.
1228 * @retval ERANGE If value coercion would overflow (or underflow) an
1229 * unsigned representation of the given @p width.
1230 * @retval non-zero If reading @p name otherwise fails, a regular unix
1231 * error code will be returned.
1234 bhnd_nvram_getvar_uint(device_t dev, const char *name, void *value, int width)
1236 bhnd_nvram_type type;
1241 type = BHND_NVRAM_TYPE_UINT8;
1244 type = BHND_NVRAM_TYPE_UINT16;
1247 type = BHND_NVRAM_TYPE_UINT32;
1250 device_printf(dev, "unsupported NVRAM integer width: %d\n",
1256 return (bhnd_nvram_getvar(dev, name, value, &len, type));
1260 * Read an NVRAM variable's unsigned 8-bit integer value.
1262 * @param dev A bhnd bus child device.
1263 * @param name The NVRAM variable name.
1264 * @param[out] value On success, the requested value will be written
1268 * @retval ENOENT The requested variable was not found.
1269 * @retval ENODEV No valid NVRAM source could be found.
1270 * @retval EFTYPE If the variable data cannot be coerced to a
1271 * a valid unsigned integer representation.
1272 * @retval ERANGE If value coercion would overflow (or underflow) uint8_t.
1273 * @retval non-zero If reading @p name otherwise fails, a regular unix
1274 * error code will be returned.
1277 bhnd_nvram_getvar_uint8(device_t dev, const char *name, uint8_t *value)
1279 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1283 * Read an NVRAM variable's unsigned 16-bit integer value.
1285 * @param dev A bhnd bus child device.
1286 * @param name The NVRAM variable name.
1287 * @param[out] value On success, the requested value will be written
1291 * @retval ENOENT The requested variable was not found.
1292 * @retval ENODEV No valid NVRAM source could be found.
1293 * @retval EFTYPE If the variable data cannot be coerced to a
1294 * a valid unsigned integer representation.
1295 * @retval ERANGE If value coercion would overflow (or underflow)
1297 * @retval non-zero If reading @p name otherwise fails, a regular unix
1298 * error code will be returned.
1301 bhnd_nvram_getvar_uint16(device_t dev, const char *name, uint16_t *value)
1303 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1307 * Read an NVRAM variable's unsigned 32-bit integer value.
1309 * @param dev A bhnd bus child device.
1310 * @param name The NVRAM variable name.
1311 * @param[out] value On success, the requested value will be written
1315 * @retval ENOENT The requested variable was not found.
1316 * @retval ENODEV No valid NVRAM source could be found.
1317 * @retval EFTYPE If the variable data cannot be coerced to a
1318 * a valid unsigned integer representation.
1319 * @retval ERANGE If value coercion would overflow (or underflow)
1321 * @retval non-zero If reading @p name otherwise fails, a regular unix
1322 * error code will be returned.
1325 bhnd_nvram_getvar_uint32(device_t dev, const char *name, uint32_t *value)
1327 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1331 * Read an NVRAM variable's signed integer value.
1333 * @param dev A bhnd bus child device.
1334 * @param name The NVRAM variable name.
1335 * @param[out] value On success, the requested value will be written
1337 * @param width The output integer type width (1, 2, or
1341 * @retval ENOENT The requested variable was not found.
1342 * @retval ENODEV No valid NVRAM source could be found.
1343 * @retval EFTYPE If the variable data cannot be coerced to a
1344 * a valid integer representation.
1345 * @retval ERANGE If value coercion would overflow (or underflow) an
1346 * signed representation of the given @p width.
1347 * @retval non-zero If reading @p name otherwise fails, a regular unix
1348 * error code will be returned.
1351 bhnd_nvram_getvar_int(device_t dev, const char *name, void *value, int width)
1353 bhnd_nvram_type type;
1358 type = BHND_NVRAM_TYPE_INT8;
1361 type = BHND_NVRAM_TYPE_INT16;
1364 type = BHND_NVRAM_TYPE_INT32;
1367 device_printf(dev, "unsupported NVRAM integer width: %d\n",
1373 return (bhnd_nvram_getvar(dev, name, value, &len, type));
1377 * Read an NVRAM variable's signed 8-bit integer value.
1379 * @param dev A bhnd bus child device.
1380 * @param name The NVRAM variable name.
1381 * @param[out] value On success, the requested value will be written
1385 * @retval ENOENT The requested variable was not found.
1386 * @retval ENODEV No valid NVRAM source could be found.
1387 * @retval EFTYPE If the variable data cannot be coerced to a
1388 * a valid integer representation.
1389 * @retval ERANGE If value coercion would overflow (or underflow) int8_t.
1390 * @retval non-zero If reading @p name otherwise fails, a regular unix
1391 * error code will be returned.
1394 bhnd_nvram_getvar_int8(device_t dev, const char *name, int8_t *value)
1396 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1400 * Read an NVRAM variable's signed 16-bit integer value.
1402 * @param dev A bhnd bus child device.
1403 * @param name The NVRAM variable name.
1404 * @param[out] value On success, the requested value will be written
1408 * @retval ENOENT The requested variable was not found.
1409 * @retval ENODEV No valid NVRAM source could be found.
1410 * @retval EFTYPE If the variable data cannot be coerced to a
1411 * a valid integer representation.
1412 * @retval ERANGE If value coercion would overflow (or underflow)
1414 * @retval non-zero If reading @p name otherwise fails, a regular unix
1415 * error code will be returned.
1418 bhnd_nvram_getvar_int16(device_t dev, const char *name, int16_t *value)
1420 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1424 * Read an NVRAM variable's signed 32-bit integer value.
1426 * @param dev A bhnd bus child device.
1427 * @param name The NVRAM variable name.
1428 * @param[out] value On success, the requested value will be written
1432 * @retval ENOENT The requested variable was not found.
1433 * @retval ENODEV No valid NVRAM source could be found.
1434 * @retval EFTYPE If the variable data cannot be coerced to a
1435 * a valid integer representation.
1436 * @retval ERANGE If value coercion would overflow (or underflow)
1438 * @retval non-zero If reading @p name otherwise fails, a regular unix
1439 * error code will be returned.
1442 bhnd_nvram_getvar_int32(device_t dev, const char *name, int32_t *value)
1444 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1449 * Read an NVRAM variable's array value.
1451 * @param dev A bhnd bus child device.
1452 * @param name The NVRAM variable name.
1453 * @param[out] buf A buffer large enough to hold @p size bytes.
1454 * On success, the requested value will be written
1456 * @param[in,out] size The required number of bytes to write to
1458 * @param type The desired array element data representation.
1461 * @retval ENOENT The requested variable was not found.
1462 * @retval ENODEV No valid NVRAM source could be found.
1463 * @retval ENXIO If less than @p size bytes are available.
1464 * @retval ENOMEM If a buffer of @p size is too small to hold the
1466 * @retval EFTYPE If the variable data cannot be coerced to a
1467 * a valid instance of @p type.
1468 * @retval ERANGE If value coercion would overflow (or underflow) a
1469 * representation of @p type.
1470 * @retval non-zero If reading @p name otherwise fails, a regular unix
1471 * error code will be returned.
1474 bhnd_nvram_getvar_array(device_t dev, const char *name, void *buf, size_t size,
1475 bhnd_nvram_type type)
1482 if ((error = bhnd_nvram_getvar(dev, name, buf, &nbytes, type)))
1485 /* Verify that the expected number of bytes were fetched */
1493 * Initialize a service provider registry.
1495 * @param bsr The service registry to initialize.
1498 * @retval non-zero if an error occurs initializing the service registry,
1499 * a regular unix error code will be returned.
1503 bhnd_service_registry_init(struct bhnd_service_registry *bsr)
1505 STAILQ_INIT(&bsr->entries);
1506 mtx_init(&bsr->lock, "bhnd_service_registry lock", NULL, MTX_DEF);
1512 * Release all resources held by @p bsr.
1514 * @param bsr A service registry instance previously successfully
1515 * initialized via bhnd_service_registry_init().
1518 * @retval EBUSY if active references to service providers registered
1519 * with @p bsr exist.
1522 bhnd_service_registry_fini(struct bhnd_service_registry *bsr)
1524 struct bhnd_service_entry *entry, *enext;
1526 /* Remove everthing we can */
1527 mtx_lock(&bsr->lock);
1528 STAILQ_FOREACH_SAFE(entry, &bsr->entries, link, enext) {
1529 if (entry->refs > 0)
1532 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry, link);
1533 free(entry, M_BHND);
1536 if (!STAILQ_EMPTY(&bsr->entries)) {
1537 mtx_unlock(&bsr->lock);
1540 mtx_unlock(&bsr->lock);
1542 mtx_destroy(&bsr->lock);
1547 * Register a @p provider for the given @p service.
1549 * @param bsr Service registry to be modified.
1550 * @param provider Service provider to register.
1551 * @param service Service for which @p provider will be registered.
1552 * @param flags Service provider flags (see BHND_SPF_*).
1555 * @retval EEXIST if an entry for @p service already exists.
1556 * @retval EINVAL if @p service is BHND_SERVICE_ANY.
1557 * @retval non-zero if registering @p provider otherwise fails, a regular
1558 * unix error code will be returned.
1561 bhnd_service_registry_add(struct bhnd_service_registry *bsr, device_t provider,
1562 bhnd_service_t service, uint32_t flags)
1564 struct bhnd_service_entry *entry;
1566 if (service == BHND_SERVICE_ANY)
1569 mtx_lock(&bsr->lock);
1571 /* Is a service provider already registered? */
1572 STAILQ_FOREACH(entry, &bsr->entries, link) {
1573 if (entry->service == service) {
1574 mtx_unlock(&bsr->lock);
1579 /* Initialize and insert our new entry */
1580 entry = malloc(sizeof(*entry), M_BHND, M_NOWAIT);
1581 if (entry == NULL) {
1582 mtx_unlock(&bsr->lock);
1586 entry->provider = provider;
1587 entry->service = service;
1588 entry->flags = flags;
1589 refcount_init(&entry->refs, 0);
1591 STAILQ_INSERT_HEAD(&bsr->entries, entry, link);
1593 mtx_unlock(&bsr->lock);
1598 * Free an unreferenced registry entry.
1600 * @param entry The entry to be deallocated.
1603 bhnd_service_registry_free_entry(struct bhnd_service_entry *entry)
1605 KASSERT(entry->refs == 0, ("provider has active references"));
1606 free(entry, M_BHND);
1610 * Attempt to remove the @p service provider registration for @p provider.
1612 * @param bsr The service registry to be modified.
1613 * @param provider The service provider to be deregistered.
1614 * @param service The service for which @p provider will be deregistered,
1615 * or BHND_SERVICE_ANY to remove all service
1616 * registrations for @p provider.
1619 * @retval EBUSY if active references to @p provider exist; see
1620 * bhnd_service_registry_retain() and
1621 * bhnd_service_registry_release().
1624 bhnd_service_registry_remove(struct bhnd_service_registry *bsr,
1625 device_t provider, bhnd_service_t service)
1627 struct bhnd_service_entry *entry, *enext;
1629 mtx_lock(&bsr->lock);
1631 #define BHND_PROV_MATCH(_e) \
1632 ((_e)->provider == provider && \
1633 (service == BHND_SERVICE_ANY || (_e)->service == service))
1635 /* Validate matching provider entries before making any
1637 STAILQ_FOREACH(entry, &bsr->entries, link) {
1638 /* Skip non-matching entries */
1639 if (!BHND_PROV_MATCH(entry))
1642 /* Entry is in use? */
1643 if (entry->refs > 0) {
1644 mtx_unlock(&bsr->lock);
1649 /* We can now safely remove matching entries */
1650 STAILQ_FOREACH_SAFE(entry, &bsr->entries, link, enext) {
1651 /* Skip non-matching entries */
1652 if (!BHND_PROV_MATCH(entry))
1655 /* Remove from list */
1656 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry, link);
1658 /* Free provider entry */
1659 bhnd_service_registry_free_entry(entry);
1661 #undef BHND_PROV_MATCH
1663 mtx_unlock(&bsr->lock);
1668 * Retain and return a reference to a registered @p service provider, if any.
1670 * @param bsr The service registry to be queried.
1671 * @param service The service for which a provider should be returned.
1673 * On success, the caller assumes ownership the returned provider, and
1674 * is responsible for releasing this reference via
1675 * bhnd_service_registry_release().
1677 * @retval device_t success
1678 * @retval NULL if no provider is registered for @p service.
1681 bhnd_service_registry_retain(struct bhnd_service_registry *bsr,
1682 bhnd_service_t service)
1684 struct bhnd_service_entry *entry;
1686 mtx_lock(&bsr->lock);
1687 STAILQ_FOREACH(entry, &bsr->entries, link) {
1688 if (entry->service != service)
1691 /* With a live refcount, entry is gauranteed to remain alive
1692 * after we release our lock */
1693 refcount_acquire(&entry->refs);
1695 mtx_unlock(&bsr->lock);
1696 return (entry->provider);
1698 mtx_unlock(&bsr->lock);
1705 * Release a reference to a service provider previously returned by
1706 * bhnd_service_registry_retain().
1708 * If this is the last reference to an inherited service provider registration
1709 * (see BHND_SPF_INHERITED), the registration will also be removed, and
1710 * true will be returned.
1712 * @param bsr The service registry from which @p provider
1714 * @param provider The provider to be released.
1715 * @param service The service for which @p provider was previously
1717 * @retval true The inherited service provider registration was removed;
1718 * the caller should release its own reference to the
1720 * @retval false The service provider was not inherited, or active
1721 * references to the provider remain.
1723 * @see BHND_SPF_INHERITED
1726 bhnd_service_registry_release(struct bhnd_service_registry *bsr,
1727 device_t provider, bhnd_service_t service)
1729 struct bhnd_service_entry *entry;
1731 /* Exclusive lock, as we need to prevent any new references to the
1732 * entry from being taken if it's to be removed */
1733 mtx_lock(&bsr->lock);
1734 STAILQ_FOREACH(entry, &bsr->entries, link) {
1737 if (entry->provider != provider)
1740 if (entry->service != service)
1743 if (refcount_release(&entry->refs) &&
1744 (entry->flags & BHND_SPF_INHERITED))
1746 /* If an inherited entry is no longer actively
1747 * referenced, remove the local registration and inform
1749 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry,
1751 bhnd_service_registry_free_entry(entry);
1757 mtx_unlock(&bsr->lock);
1761 /* Caller owns a reference, but no such provider is registered? */
1762 panic("invalid service provider reference");
1766 * Using the bhnd(4) bus-level core information and a custom core name,
1767 * populate @p dev's device description.
1769 * @param dev A bhnd-bus attached device.
1770 * @param dev_name The core's name (e.g. "SDIO Device Core").
1773 bhnd_set_custom_core_desc(device_t dev, const char *dev_name)
1775 const char *vendor_name;
1778 vendor_name = bhnd_get_vendor_name(dev);
1779 asprintf(&desc, M_BHND, "%s %s, rev %hhu", vendor_name, dev_name,
1780 bhnd_get_hwrev(dev));
1783 device_set_desc_copy(dev, desc);
1786 device_set_desc(dev, dev_name);
1791 * Using the bhnd(4) bus-level core information, populate @p dev's device
1794 * @param dev A bhnd-bus attached device.
1797 bhnd_set_default_core_desc(device_t dev)
1799 bhnd_set_custom_core_desc(dev, bhnd_get_device_name(dev));
1804 * Using the bhnd @p chip_id, populate the bhnd(4) bus @p dev's device
1807 * @param dev A bhnd-bus attached device.
1808 * @param chip_id The chip identification.
1811 bhnd_set_default_bus_desc(device_t dev, const struct bhnd_chipid *chip_id)
1813 const char *bus_name;
1815 char chip_name[BHND_CHIPID_MAX_NAMELEN];
1817 /* Determine chip type's bus name */
1818 switch (chip_id->chip_type) {
1819 case BHND_CHIPTYPE_SIBA:
1820 bus_name = "SIBA bus";
1822 case BHND_CHIPTYPE_BCMA:
1823 case BHND_CHIPTYPE_BCMA_ALT:
1824 bus_name = "BCMA bus";
1826 case BHND_CHIPTYPE_UBUS:
1827 bus_name = "UBUS bus";
1830 bus_name = "Unknown Type";
1834 /* Format chip name */
1835 bhnd_format_chip_id(chip_name, sizeof(chip_name),
1838 /* Format and set device description */
1839 asprintf(&desc, M_BHND, "%s %s", chip_name, bus_name);
1841 device_set_desc_copy(dev, desc);
1844 device_set_desc(dev, bus_name);
1850 * Helper function for implementing BHND_BUS_REGISTER_PROVIDER().
1852 * This implementation delegates the request to the BHND_BUS_REGISTER_PROVIDER()
1853 * method on the parent of @p dev. If no parent exists, the implementation
1854 * will return an error.
1857 bhnd_bus_generic_register_provider(device_t dev, device_t child,
1858 device_t provider, bhnd_service_t service)
1860 device_t parent = device_get_parent(dev);
1862 if (parent != NULL) {
1863 return (BHND_BUS_REGISTER_PROVIDER(parent, child,
1864 provider, service));
1871 * Helper function for implementing BHND_BUS_DEREGISTER_PROVIDER().
1873 * This implementation delegates the request to the
1874 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
1875 * exists, the implementation will panic.
1878 bhnd_bus_generic_deregister_provider(device_t dev, device_t child,
1879 device_t provider, bhnd_service_t service)
1881 device_t parent = device_get_parent(dev);
1883 if (parent != NULL) {
1884 return (BHND_BUS_DEREGISTER_PROVIDER(parent, child,
1885 provider, service));
1888 panic("missing BHND_BUS_DEREGISTER_PROVIDER()");
1892 * Helper function for implementing BHND_BUS_RETAIN_PROVIDER().
1894 * This implementation delegates the request to the
1895 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
1896 * exists, the implementation will return NULL.
1899 bhnd_bus_generic_retain_provider(device_t dev, device_t child,
1900 bhnd_service_t service)
1902 device_t parent = device_get_parent(dev);
1904 if (parent != NULL) {
1905 return (BHND_BUS_RETAIN_PROVIDER(parent, child,
1913 * Helper function for implementing BHND_BUS_RELEASE_PROVIDER().
1915 * This implementation delegates the request to the
1916 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
1917 * exists, the implementation will panic.
1920 bhnd_bus_generic_release_provider(device_t dev, device_t child,
1921 device_t provider, bhnd_service_t service)
1923 device_t parent = device_get_parent(dev);
1925 if (parent != NULL) {
1926 return (BHND_BUS_RELEASE_PROVIDER(parent, child,
1927 provider, service));
1930 panic("missing BHND_BUS_RELEASE_PROVIDER()");
1934 * Helper function for implementing BHND_BUS_REGISTER_PROVIDER().
1936 * This implementation uses the bhnd_service_registry_add() function to
1937 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
1938 * a suitable service registry to edit.
1941 bhnd_bus_generic_sr_register_provider(device_t dev, device_t child,
1942 device_t provider, bhnd_service_t service)
1944 struct bhnd_service_registry *bsr;
1946 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
1948 KASSERT(bsr != NULL, ("NULL service registry"));
1950 return (bhnd_service_registry_add(bsr, provider, service, 0));
1954 * Helper function for implementing BHND_BUS_DEREGISTER_PROVIDER().
1956 * This implementation uses the bhnd_service_registry_remove() function to
1957 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
1958 * a suitable service registry to edit.
1961 bhnd_bus_generic_sr_deregister_provider(device_t dev, device_t child,
1962 device_t provider, bhnd_service_t service)
1964 struct bhnd_service_registry *bsr;
1966 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
1968 KASSERT(bsr != NULL, ("NULL service registry"));
1970 return (bhnd_service_registry_remove(bsr, provider, service));
1974 * Helper function for implementing BHND_BUS_RETAIN_PROVIDER().
1976 * This implementation uses the bhnd_service_registry_retain() function to
1977 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
1978 * a suitable service registry.
1980 * If a local provider for the service is not available, and a parent device is
1981 * available, this implementation will attempt to fetch and locally register
1982 * a service provider reference from the parent of @p dev.
1985 bhnd_bus_generic_sr_retain_provider(device_t dev, device_t child,
1986 bhnd_service_t service)
1988 struct bhnd_service_registry *bsr;
1989 device_t parent, provider;
1992 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
1993 KASSERT(bsr != NULL, ("NULL service registry"));
1996 * Attempt to fetch a service provider reference from either the local
1997 * service registry, or if not found, from our parent.
1999 * If we fetch a provider from our parent, we register the provider
2000 * with the local service registry to prevent conflicting local
2001 * registrations from being added.
2004 /* Check the local service registry first */
2005 provider = bhnd_service_registry_retain(bsr, service);
2006 if (provider != NULL)
2009 /* Otherwise, try to delegate to our parent (if any) */
2010 if ((parent = device_get_parent(dev)) == NULL)
2013 provider = BHND_BUS_RETAIN_PROVIDER(parent, dev, service);
2014 if (provider == NULL)
2017 /* Register the inherited service registration with the local
2019 error = bhnd_service_registry_add(bsr, provider, service,
2020 BHND_SPF_INHERITED);
2022 BHND_BUS_RELEASE_PROVIDER(parent, dev, provider,
2024 if (error == EEXIST) {
2025 /* A valid service provider was registered
2026 * concurrently; retry fetching from the local
2031 device_printf(dev, "failed to register service "
2032 "provider: %d\n", error);
2039 * Helper function for implementing BHND_BUS_RELEASE_PROVIDER().
2041 * This implementation uses the bhnd_service_registry_release() function to
2042 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
2043 * a suitable service registry.
2046 bhnd_bus_generic_sr_release_provider(device_t dev, device_t child,
2047 device_t provider, bhnd_service_t service)
2049 struct bhnd_service_registry *bsr;
2051 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
2052 KASSERT(bsr != NULL, ("NULL service registry"));
2054 /* Release the provider reference; if the refcount hits zero on an
2055 * inherited reference, true will be returned, and we need to drop
2056 * our own bus reference to the provider */
2057 if (!bhnd_service_registry_release(bsr, provider, service))
2060 /* Drop our reference to the borrowed provider */
2061 BHND_BUS_RELEASE_PROVIDER(device_get_parent(dev), dev, provider,
2066 * Helper function for implementing BHND_BUS_IS_HW_DISABLED().
2068 * If a parent device is available, this implementation delegates the
2069 * request to the BHND_BUS_IS_HW_DISABLED() method on the parent of @p dev.
2071 * If no parent device is available (i.e. on a the bus root), the hardware
2072 * is assumed to be usable and false is returned.
2075 bhnd_bus_generic_is_hw_disabled(device_t dev, device_t child)
2077 if (device_get_parent(dev) != NULL)
2078 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child));
2084 * Helper function for implementing BHND_BUS_GET_CHIPID().
2086 * This implementation delegates the request to the BHND_BUS_GET_CHIPID()
2087 * method on the parent of @p dev. If no parent exists, the implementation
2090 const struct bhnd_chipid *
2091 bhnd_bus_generic_get_chipid(device_t dev, device_t child)
2093 if (device_get_parent(dev) != NULL)
2094 return (BHND_BUS_GET_CHIPID(device_get_parent(dev), child));
2096 panic("missing BHND_BUS_GET_CHIPID()");
2100 * Helper function for implementing BHND_BUS_GET_DMA_TRANSLATION().
2102 * If a parent device is available, this implementation delegates the
2103 * request to the BHND_BUS_GET_DMA_TRANSLATION() method on the parent of @p dev.
2105 * If no parent device is available, this implementation will panic.
2108 bhnd_bus_generic_get_dma_translation(device_t dev, device_t child, u_int width,
2109 uint32_t flags, bus_dma_tag_t *dmat,
2110 struct bhnd_dma_translation *translation)
2112 if (device_get_parent(dev) != NULL) {
2113 return (BHND_BUS_GET_DMA_TRANSLATION(device_get_parent(dev),
2114 child, width, flags, dmat, translation));
2117 panic("missing BHND_BUS_GET_DMA_TRANSLATION()");
2120 /* nvram board_info population macros for bhnd_bus_generic_read_board_info() */
2121 #define BHND_GV(_dest, _name) \
2122 bhnd_nvram_getvar_uint(child, BHND_NVAR_ ## _name, &_dest, \
2125 #define REQ_BHND_GV(_dest, _name) do { \
2126 if ((error = BHND_GV(_dest, _name))) { \
2127 device_printf(dev, \
2128 "error reading " __STRING(_name) ": %d\n", error); \
2133 #define OPT_BHND_GV(_dest, _name, _default) do { \
2134 if ((error = BHND_GV(_dest, _name))) { \
2135 if (error != ENOENT) { \
2136 device_printf(dev, \
2138 __STRING(_name) ": %d\n", error); \
2146 * Helper function for implementing BHND_BUS_READ_BOARDINFO().
2148 * This implementation populates @p info with information from NVRAM,
2149 * defaulting board_vendor and board_type fields to 0 if the
2150 * requested variables cannot be found.
2152 * This behavior is correct for most SoCs, but must be overridden on
2153 * bridged (PCI, PCMCIA, etc) devices to produce a complete bhnd_board_info
2157 bhnd_bus_generic_read_board_info(device_t dev, device_t child,
2158 struct bhnd_board_info *info)
2162 OPT_BHND_GV(info->board_vendor, BOARDVENDOR, 0);
2163 OPT_BHND_GV(info->board_type, BOARDTYPE, 0); /* srom >= 2 */
2164 OPT_BHND_GV(info->board_devid, DEVID, 0); /* srom >= 8 */
2165 REQ_BHND_GV(info->board_rev, BOARDREV);
2166 OPT_BHND_GV(info->board_srom_rev,SROMREV, 0); /* missing in
2169 REQ_BHND_GV(info->board_flags, BOARDFLAGS);
2170 OPT_BHND_GV(info->board_flags2, BOARDFLAGS2, 0); /* srom >= 4 */
2171 OPT_BHND_GV(info->board_flags3, BOARDFLAGS3, 0); /* srom >= 11 */
2181 * Helper function for implementing BHND_BUS_GET_NVRAM_VAR().
2183 * This implementation searches @p dev for a usable NVRAM child device.
2185 * If no usable child device is found on @p dev, the request is delegated to
2186 * the BHND_BUS_GET_NVRAM_VAR() method on the parent of @p dev.
2189 bhnd_bus_generic_get_nvram_var(device_t dev, device_t child, const char *name,
2190 void *buf, size_t *size, bhnd_nvram_type type)
2195 /* Make sure we're holding Giant for newbus */
2198 /* Look for a directly-attached NVRAM child */
2199 if ((nvram = device_find_child(dev, "bhnd_nvram", -1)) != NULL)
2200 return BHND_NVRAM_GETVAR(nvram, name, buf, size, type);
2202 /* Try to delegate to parent */
2203 if ((parent = device_get_parent(dev)) == NULL)
2206 return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), child,
2207 name, buf, size, type));
2211 * Helper function for implementing BHND_BUS_ALLOC_RESOURCE().
2213 * This implementation of BHND_BUS_ALLOC_RESOURCE() delegates allocation
2214 * of the underlying resource to BUS_ALLOC_RESOURCE(), and activation
2215 * to @p dev's BHND_BUS_ACTIVATE_RESOURCE().
2217 struct bhnd_resource *
2218 bhnd_bus_generic_alloc_resource(device_t dev, device_t child, int type,
2219 int *rid, rman_res_t start, rman_res_t end, rman_res_t count,
2222 struct bhnd_resource *br;
2223 struct resource *res;
2229 /* Allocate the real bus resource (without activating it) */
2230 res = BUS_ALLOC_RESOURCE(dev, child, type, rid, start, end, count,
2231 (flags & ~RF_ACTIVE));
2235 /* Allocate our bhnd resource wrapper. */
2236 br = malloc(sizeof(struct bhnd_resource), M_BHND, M_NOWAIT);
2243 /* Attempt activation */
2244 if (flags & RF_ACTIVE) {
2245 error = BHND_BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, br);
2254 BUS_RELEASE_RESOURCE(dev, child, type, *rid, res);
2261 * Helper function for implementing BHND_BUS_RELEASE_RESOURCE().
2263 * This implementation of BHND_BUS_RELEASE_RESOURCE() delegates release of
2264 * the backing resource to BUS_RELEASE_RESOURCE().
2267 bhnd_bus_generic_release_resource(device_t dev, device_t child, int type,
2268 int rid, struct bhnd_resource *r)
2272 if ((error = BUS_RELEASE_RESOURCE(dev, child, type, rid, r->res)))
2281 * Helper function for implementing BHND_BUS_ACTIVATE_RESOURCE().
2283 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() first calls the
2284 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
2286 * If this fails, and if @p dev is the direct parent of @p child, standard
2287 * resource activation is attempted via bus_activate_resource(). This enables
2288 * direct use of the bhnd(4) resource APIs on devices that may not be attached
2289 * to a parent bhnd bus or bridge.
2292 bhnd_bus_generic_activate_resource(device_t dev, device_t child, int type,
2293 int rid, struct bhnd_resource *r)
2298 passthrough = (device_get_parent(child) != dev);
2300 /* Try to delegate to the parent */
2301 if (device_get_parent(dev) != NULL) {
2302 error = BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev),
2303 child, type, rid, r);
2308 /* If bhnd(4) activation has failed and we're the child's direct
2309 * parent, try falling back on standard resource activation.
2311 if (error && !passthrough) {
2312 error = bus_activate_resource(child, type, rid, r->res);
2321 * Helper function for implementing BHND_BUS_DEACTIVATE_RESOURCE().
2323 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the
2324 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
2327 bhnd_bus_generic_deactivate_resource(device_t dev, device_t child,
2328 int type, int rid, struct bhnd_resource *r)
2330 if (device_get_parent(dev) != NULL)
2331 return (BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev),
2332 child, type, rid, r));
2338 * Helper function for implementing BHND_BUS_GET_INTR_DOMAIN().
2340 * This implementation simply returns the address of nearest bhnd(4) bus,
2341 * which may be @p dev; this behavior may be incompatible with FDT/OFW targets.
2344 bhnd_bus_generic_get_intr_domain(device_t dev, device_t child, bool self)
2346 return ((uintptr_t)dev);