xref: /freebsd/sys/dev/bhnd/bhnd_subr.c (revision 5bf5ca772c6de2d53344a78cf461447cc322ccea)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
5  * Copyright (c) 2017 The FreeBSD Foundation
6  * All rights reserved.
7  *
8  * Portions of this software were developed by Landon Fuller
9  * under sponsorship from the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
21  *
22  * NO WARRANTY
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.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/refcount.h>
42 #include <sys/systm.h>
43 
44 #include <machine/bus.h>
45 #include <sys/rman.h>
46 #include <machine/resource.h>
47 
48 #include <dev/bhnd/siba/sibareg.h>
49 
50 #include <dev/bhnd/cores/chipc/chipcreg.h>
51 
52 #include "nvram/bhnd_nvram.h"
53 
54 #include "bhnd_chipc_if.h"
55 
56 #include "bhnd_nvram_if.h"
57 #include "bhnd_nvram_map.h"
58 
59 #include "bhndreg.h"
60 #include "bhndvar.h"
61 #include "bhnd_private.h"
62 
63 static void	bhnd_service_registry_free_entry(
64 		    struct bhnd_service_entry *entry);
65 
66 static int	compare_ascending_probe_order(const void *lhs, const void *rhs);
67 static int	compare_descending_probe_order(const void *lhs,
68 		    const void *rhs);
69 
70 /* BHND core device description table. */
71 static const struct bhnd_core_desc {
72 	uint16_t	 vendor;
73 	uint16_t	 device;
74 	bhnd_devclass_t	 class;
75 	const char	*desc;
76 } bhnd_core_descs[] = {
77 	#define	BHND_CDESC(_mfg, _cid, _cls, _desc)		\
78 	    { BHND_MFGID_ ## _mfg, BHND_COREID_ ## _cid,	\
79 		BHND_DEVCLASS_ ## _cls, _desc }
80 
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)"),
137 
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"),
143 
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"),
161 #undef	BHND_CDESC
162 
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" },
172 
173 	{ 0, 0, 0, NULL }
174 };
175 
176 static const struct bhnd_device_quirk bhnd_chipc_clkctl_quirks[];
177 static const struct bhnd_device_quirk bhnd_pcmcia_clkctl_quirks[];
178 
179 /**
180  * Device table entries for core-specific CLKCTL quirk lookup.
181  */
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),
185 	BHND_DEVICE_END,
186 };
187 
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
194 };
195 
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
202 };
203 
204 /**
205  * Return the name for a given JEP106 manufacturer ID.
206  *
207  * @param vendor A JEP106 Manufacturer ID, including the non-standard ARM 4-bit
208  * JEP106 continuation code.
209  */
210 const char *
211 bhnd_vendor_name(uint16_t vendor)
212 {
213 	switch (vendor) {
214 	case BHND_MFGID_ARM:
215 		return "ARM";
216 	case BHND_MFGID_BCM:
217 		return "Broadcom";
218 	case BHND_MFGID_MIPS:
219 		return "MIPS";
220 	default:
221 		return "unknown";
222 	}
223 }
224 
225 /**
226  * Return the name of a port type.
227  *
228  * @param port_type The port type to look up.
229  */
230 const char *
231 bhnd_port_type_name(bhnd_port_type port_type)
232 {
233 	switch (port_type) {
234 	case BHND_PORT_DEVICE:
235 		return ("device");
236 	case BHND_PORT_BRIDGE:
237 		return ("bridge");
238 	case BHND_PORT_AGENT:
239 		return ("agent");
240 	default:
241 		return "unknown";
242 	}
243 }
244 
245 /**
246  * Return the name of an NVRAM source.
247  *
248  * @param nvram_src The NVRAM source type to look up.
249  */
250 const char *
251 bhnd_nvram_src_name(bhnd_nvram_src nvram_src)
252 {
253 	switch (nvram_src) {
254 	case BHND_NVRAM_SRC_FLASH:
255 		return ("flash");
256 	case BHND_NVRAM_SRC_OTP:
257 		return ("OTP");
258 	case BHND_NVRAM_SRC_SPROM:
259 		return ("SPROM");
260 	case BHND_NVRAM_SRC_UNKNOWN:
261 		return ("none");
262 	default:
263 		return ("unknown");
264 	}
265 }
266 
267 static const struct bhnd_core_desc *
268 bhnd_find_core_desc(uint16_t vendor, uint16_t device)
269 {
270 	for (u_int i = 0; bhnd_core_descs[i].desc != NULL; i++) {
271 		if (bhnd_core_descs[i].vendor != vendor)
272 			continue;
273 
274 		if (bhnd_core_descs[i].device != device)
275 			continue;
276 
277 		return (&bhnd_core_descs[i]);
278 	}
279 
280 	return (NULL);
281 }
282 
283 /**
284  * Return a human-readable name for a BHND core.
285  *
286  * @param vendor The core designer's JEDEC-106 Manufacturer ID.
287  * @param device The core identifier.
288  */
289 const char *
290 bhnd_find_core_name(uint16_t vendor, uint16_t device)
291 {
292 	const struct bhnd_core_desc *desc;
293 
294 	if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)
295 		return ("unknown");
296 
297 	return desc->desc;
298 }
299 
300 /**
301  * Return the device class for a BHND core.
302  *
303  * @param vendor The core designer's JEDEC-106 Manufacturer ID.
304  * @param device The core identifier.
305  */
306 bhnd_devclass_t
307 bhnd_find_core_class(uint16_t vendor, uint16_t device)
308 {
309 	const struct bhnd_core_desc *desc;
310 
311 	if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)
312 		return (BHND_DEVCLASS_OTHER);
313 
314 	return desc->class;
315 }
316 
317 /**
318  * Return a human-readable name for a BHND core.
319  *
320  * @param ci The core's info record.
321  */
322 const char *
323 bhnd_core_name(const struct bhnd_core_info *ci)
324 {
325 	return bhnd_find_core_name(ci->vendor, ci->device);
326 }
327 
328 /**
329  * Return the device class for a BHND core.
330  *
331  * @param ci The core's info record.
332  */
333 bhnd_devclass_t
334 bhnd_core_class(const struct bhnd_core_info *ci)
335 {
336 	return bhnd_find_core_class(ci->vendor, ci->device);
337 }
338 
339 /**
340  * Write a human readable name representation of the given
341  * BHND_CHIPID_* constant to @p buffer.
342  *
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.
346  *
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
349  * set to '\0'.
350  *
351  * @sa BHND_CHIPID_MAX_NAMELEN
352  */
353 int
354 bhnd_format_chip_id(char *buffer, size_t size, uint16_t chip_id)
355 {
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));
359 	else
360 		return (snprintf(buffer, size, "BCM%hu", chip_id));
361 }
362 
363 /**
364  * Return a core info record populated from a bhnd-attached @p dev.
365  *
366  * @param dev A bhnd device.
367  *
368  * @return A core info record for @p dev.
369  */
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)
378 	};
379 }
380 
381 /**
382  * Find a @p class child device with @p unit on @p bus.
383  *
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.
388  *
389  * @retval device_t if a matching child device is found.
390  * @retval NULL if no matching child device is found.
391  */
392 device_t
393 bhnd_bus_find_child(device_t bus, bhnd_devclass_t class, int unit)
394 {
395 	struct bhnd_core_match md = {
396 		BHND_MATCH_CORE_CLASS(class),
397 		BHND_MATCH_CORE_UNIT(unit)
398 	};
399 
400 	if (unit == -1)
401 		md.m.match.core_unit = 0;
402 
403 	return bhnd_bus_match_child(bus, &md);
404 }
405 
406 /**
407  * Find the first child device on @p bus that matches @p desc.
408  *
409  * @param bus The bhnd-compatible bus to be searched.
410  * @param desc A match descriptor.
411  *
412  * @retval device_t if a matching child device is found.
413  * @retval NULL if no matching child device is found.
414  */
415 device_t
416 bhnd_bus_match_child(device_t bus, const struct bhnd_core_match *desc)
417 {
418 	device_t	*devlistp;
419 	device_t	 match;
420 	int		 devcnt;
421 	int		 error;
422 
423 	error = device_get_children(bus, &devlistp, &devcnt);
424 	if (error != 0)
425 		return (NULL);
426 
427 	match = NULL;
428 	for (int i = 0; i < devcnt; i++) {
429 		struct bhnd_core_info ci = bhnd_get_core_info(devlistp[i]);
430 
431 		if (bhnd_core_matches(&ci, desc)) {
432 			match = devlistp[i];
433 			goto done;
434 		}
435 	}
436 
437 done:
438 	free(devlistp, M_TEMP);
439 	return match;
440 }
441 
442 /**
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
445  * in @p ndevs.
446  *
447  * The memory allocated for the table must be freed via
448  * bhnd_bus_free_children().
449  *
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
454  *				in @p devlist.
455  *
456  * @retval 0		success
457  * @retval non-zero	if an error occurs, a regular unix error code will
458  *			be returned.
459  */
460 int
461 bhnd_bus_get_children(device_t bus, device_t **devlist, int *devcount,
462     bhnd_device_order order)
463 {
464 	int error;
465 
466 	/* Fetch device array */
467 	if ((error = device_get_children(bus, devlist, devcount)))
468 		return (error);
469 
470 	/* Perform requested sorting */
471 	if ((error = bhnd_sort_devices(*devlist, *devcount, order))) {
472 		bhnd_bus_free_children(*devlist);
473 		return (error);
474 	}
475 
476 	return (0);
477 }
478 
479 /**
480  * Free any memory allocated in a previous call to bhnd_bus_get_children().
481  *
482  * @param devlist The device array returned by bhnd_bus_get_children().
483  */
484 void
485 bhnd_bus_free_children(device_t *devlist)
486 {
487 	free(devlist, M_TEMP);
488 }
489 
490 /**
491  * Perform in-place sorting of an array of bhnd device instances.
492  *
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.
496  *
497  * @retval 0		success
498  * @retval EINVAL	if the sort order is unknown.
499  */
500 int
501 bhnd_sort_devices(device_t *devlist, size_t devcount, bhnd_device_order order)
502 {
503 	int (*compare)(const void *, const void *);
504 
505 	switch (order) {
506 	case BHND_DEVICE_ORDER_ATTACH:
507 		compare = compare_ascending_probe_order;
508 		break;
509 	case BHND_DEVICE_ORDER_DETACH:
510 		compare = compare_descending_probe_order;
511 		break;
512 	default:
513 		printf("unknown sort order: %d\n", order);
514 		return (EINVAL);
515 	}
516 
517 	qsort(devlist, devcount, sizeof(*devlist), compare);
518 	return (0);
519 }
520 
521 /*
522  * Ascending comparison of bhnd device's probe order.
523  */
524 static int
525 compare_ascending_probe_order(const void *lhs, const void *rhs)
526 {
527 	device_t	ldev, rdev;
528 	int		lorder, rorder;
529 
530 	ldev = (*(const device_t *) lhs);
531 	rdev = (*(const device_t *) rhs);
532 
533 	lorder = BHND_BUS_GET_PROBE_ORDER(device_get_parent(ldev), ldev);
534 	rorder = BHND_BUS_GET_PROBE_ORDER(device_get_parent(rdev), rdev);
535 
536 	if (lorder < rorder) {
537 		return (-1);
538 	} else if (lorder > rorder) {
539 		return (1);
540 	} else {
541 		return (0);
542 	}
543 }
544 
545 /*
546  * Descending comparison of bhnd device's probe order.
547  */
548 static int
549 compare_descending_probe_order(const void *lhs, const void *rhs)
550 {
551 	return (compare_ascending_probe_order(rhs, lhs));
552 }
553 
554 /**
555  * Call device_probe_and_attach() for each of the bhnd bus device's
556  * children, in bhnd attach order.
557  *
558  * @param bus The bhnd-compatible bus for which all children should be probed
559  * and attached.
560  */
561 int
562 bhnd_bus_probe_children(device_t bus)
563 {
564 	device_t	*devs;
565 	int		 ndevs;
566 	int		 error;
567 
568 	/* Fetch children in attach order */
569 	error = bhnd_bus_get_children(bus, &devs, &ndevs,
570 	    BHND_DEVICE_ORDER_ATTACH);
571 	if (error)
572 		return (error);
573 
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);
578 	}
579 
580 	bhnd_bus_free_children(devs);
581 
582 	return (0);
583 }
584 
585 /**
586  * Walk up the bhnd device hierarchy to locate the root device
587  * to which the bhndb bridge is attached.
588  *
589  * This can be used from within bhnd host bridge drivers to locate the
590  * actual upstream host device.
591  *
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.
595  *
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.
599  */
600 device_t
601 bhnd_find_bridge_root(device_t dev, devclass_t bus_class)
602 {
603 	devclass_t	bhndb_class;
604 	device_t	parent;
605 
606 	KASSERT(device_get_devclass(device_get_parent(dev)) == bhnd_devclass,
607 	   ("%s not a bhnd device", device_get_nameunit(dev)));
608 
609 	bhndb_class = devclass_find("bhndb");
610 
611 	/* Walk the device tree until we hit a bridge */
612 	parent = dev;
613 	while ((parent = device_get_parent(parent)) != NULL) {
614 		if (device_get_devclass(parent) == bhndb_class)
615 			break;
616 	}
617 
618 	/* No bridge? */
619 	if (parent == NULL)
620 		return (NULL);
621 
622 	/* Search for a parent attached to the expected bus class */
623 	while ((parent = device_get_parent(parent)) != NULL) {
624 		device_t bus;
625 
626 		bus = device_get_parent(parent);
627 		if (bus != NULL && device_get_devclass(bus) == bus_class)
628 			return (parent);
629 	}
630 
631 	/* Not found */
632 	return (NULL);
633 }
634 
635 /**
636  * Find the first core in @p cores that matches @p desc.
637  *
638  * @param cores The table to search.
639  * @param num_cores The length of @p cores.
640  * @param desc A match descriptor.
641  *
642  * @retval bhnd_core_info if a matching core is found.
643  * @retval NULL if no matching core is found.
644  */
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)
648 {
649 	for (u_int i = 0; i < num_cores; i++) {
650 		if (bhnd_core_matches(&cores[i], desc))
651 			return &cores[i];
652 	}
653 
654 	return (NULL);
655 }
656 
657 
658 /**
659  * Find the first core in @p cores with the given @p class.
660  *
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.
664  *
665  * @retval non-NULL if a matching core is found.
666  * @retval NULL if no matching core is found.
667  */
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)
671 {
672 	struct bhnd_core_match md = {
673 		BHND_MATCH_CORE_CLASS(class)
674 	};
675 
676 	return bhnd_match_core(cores, num_cores, &md);
677 }
678 
679 
680 /**
681  * Create an equality match descriptor for @p core.
682  *
683  * @param core The core info to be matched on.
684  *
685  * @return an equality match descriptor for @p core.
686  */
687 struct bhnd_core_match
688 bhnd_core_get_match_desc(const struct bhnd_core_info *core)
689 {
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)
697 	});
698 }
699 
700 
701 /**
702  * Return true if the @p lhs is equal to @p rhs.
703  *
704  * @param lhs The first bhnd core descriptor to compare.
705  * @param rhs The second bhnd core descriptor to compare.
706  *
707  * @retval true if @p lhs is equal to @p rhs
708  * @retval false if @p lhs is not equal to @p rhs
709  */
710 bool
711 bhnd_cores_equal(const struct bhnd_core_info *lhs,
712     const struct bhnd_core_info *rhs)
713 {
714 	struct bhnd_core_match md;
715 
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));
719 }
720 
721 /**
722  * Return true if the @p core matches @p desc.
723  *
724  * @param core A bhnd core descriptor.
725  * @param desc A match descriptor to compare against @p core.
726  *
727  * @retval true if @p core matches @p match.
728  * @retval false if @p core does not match @p match.
729  */
730 bool
731 bhnd_core_matches(const struct bhnd_core_info *core,
732     const struct bhnd_core_match *desc)
733 {
734 	if (desc->m.match.core_vendor && desc->core_vendor != core->vendor)
735 		return (false);
736 
737 	if (desc->m.match.core_id && desc->core_id != core->device)
738 		return (false);
739 
740 	if (desc->m.match.core_unit && desc->core_unit != core->unit)
741 		return (false);
742 
743 	if (desc->m.match.core_rev &&
744 	    !bhnd_hwrev_matches(core->hwrev, &desc->core_rev))
745 		return (false);
746 
747 	if (desc->m.match.core_idx && desc->core_idx != core->core_idx)
748 		return (false);
749 
750 	if (desc->m.match.core_class &&
751 	    desc->core_class != bhnd_core_class(core))
752 		return (false);
753 
754 	return true;
755 }
756 
757 /**
758  * Return true if the @p chip matches @p desc.
759  *
760  * @param chip A bhnd chip identifier.
761  * @param desc A match descriptor to compare against @p chip.
762  *
763  * @retval true if @p chip matches @p match.
764  * @retval false if @p chip does not match @p match.
765  */
766 bool
767 bhnd_chip_matches(const struct bhnd_chipid *chip,
768     const struct bhnd_chip_match *desc)
769 {
770 	if (desc->m.match.chip_id && chip->chip_id != desc->chip_id)
771 		return (false);
772 
773 	if (desc->m.match.chip_pkg && chip->chip_pkg != desc->chip_pkg)
774 		return (false);
775 
776 	if (desc->m.match.chip_rev &&
777 	    !bhnd_hwrev_matches(chip->chip_rev, &desc->chip_rev))
778 		return (false);
779 
780 	if (desc->m.match.chip_type && chip->chip_type != desc->chip_type)
781 		return (false);
782 
783 	return (true);
784 }
785 
786 /**
787  * Return true if the @p board matches @p desc.
788  *
789  * @param board The bhnd board info.
790  * @param desc A match descriptor to compare against @p board.
791  *
792  * @retval true if @p chip matches @p match.
793  * @retval false if @p chip does not match @p match.
794  */
795 bool
796 bhnd_board_matches(const struct bhnd_board_info *board,
797     const struct bhnd_board_match *desc)
798 {
799 	if (desc->m.match.board_srom_rev &&
800 	    !bhnd_hwrev_matches(board->board_srom_rev, &desc->board_srom_rev))
801 		return (false);
802 
803 	if (desc->m.match.board_vendor &&
804 	    board->board_vendor != desc->board_vendor)
805 		return (false);
806 
807 	if (desc->m.match.board_type && board->board_type != desc->board_type)
808 		return (false);
809 
810 	if (desc->m.match.board_devid &&
811 	    board->board_devid != desc->board_devid)
812 		return (false);
813 
814 	if (desc->m.match.board_rev &&
815 	    !bhnd_hwrev_matches(board->board_rev, &desc->board_rev))
816 		return (false);
817 
818 	return (true);
819 }
820 
821 /**
822  * Return true if the @p hwrev matches @p desc.
823  *
824  * @param hwrev A bhnd hardware revision.
825  * @param desc A match descriptor to compare against @p core.
826  *
827  * @retval true if @p hwrev matches @p match.
828  * @retval false if @p hwrev does not match @p match.
829  */
830 bool
831 bhnd_hwrev_matches(uint16_t hwrev, const struct bhnd_hwrev_match *desc)
832 {
833 	if (desc->start != BHND_HWREV_INVALID &&
834 	    desc->start > hwrev)
835 		return false;
836 
837 	if (desc->end != BHND_HWREV_INVALID &&
838 	    desc->end < hwrev)
839 		return false;
840 
841 	return true;
842 }
843 
844 /**
845  * Return true if the @p dev matches @p desc.
846  *
847  * @param dev A bhnd device.
848  * @param desc A match descriptor to compare against @p dev.
849  *
850  * @retval true if @p dev matches @p match.
851  * @retval false if @p dev does not match @p match.
852  */
853 bool
854 bhnd_device_matches(device_t dev, const struct bhnd_device_match *desc)
855 {
856 	struct bhnd_core_info		 core;
857 	const struct bhnd_chipid	*chip;
858 	struct bhnd_board_info		 board;
859 	device_t			 parent;
860 	int				 error;
861 
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) };
866 
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");
874 			return (false);
875 		}
876 
877 		core = bhnd_get_core_info(dev);
878 		if (!bhnd_core_matches(&core, &m_core))
879 			return (false);
880 	}
881 
882 	/* Fetch and match chip info */
883 	if (m_chip.m.match_flags) {
884 		chip = bhnd_get_chipid(dev);
885 
886 		if (!bhnd_chip_matches(chip, &m_chip))
887 			return (false);
888 	}
889 
890 	/* Fetch and match board info.
891 	 *
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);
898 			return (false);
899 		}
900 
901 		if (!bhnd_board_matches(&board, &m_board))
902 			return (false);
903 	}
904 
905 	/* All matched */
906 	return (true);
907 }
908 
909 /**
910  * Search @p table for an entry matching @p dev.
911  *
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.
915  *
916  * @retval non-NULL the first matching device, if any.
917  * @retval NULL if no matching device is found in @p table.
918  */
919 const struct bhnd_device *
920 bhnd_device_lookup(device_t dev, const struct bhnd_device *table,
921     size_t entry_size)
922 {
923 	const struct bhnd_device	*entry;
924 	device_t			 hostb, parent;
925 	bhnd_attach_type		 attach_type;
926 	uint32_t			 dflags;
927 
928 	parent = device_get_parent(dev);
929 	hostb = bhnd_bus_find_hostb_device(parent);
930 	attach_type = bhnd_get_attach_type(dev);
931 
932 	for (entry = table; !BHND_DEVICE_IS_END(entry); entry =
933 	    (const struct bhnd_device *) ((const char *) entry + entry_size))
934 	{
935 		/* match core info */
936 		if (!bhnd_device_matches(dev, &entry->core))
937 			continue;
938 
939 		/* match device flags */
940 		dflags = entry->device_flags;
941 
942 		/* hostb implies BHND_ATTACH_ADAPTER requirement */
943 		if (dflags & BHND_DF_HOSTB)
944 			dflags |= BHND_DF_ADAPTER;
945 
946 		if (dflags & BHND_DF_ADAPTER)
947 			if (attach_type != BHND_ATTACH_ADAPTER)
948 				continue;
949 
950 		if (dflags & BHND_DF_HOSTB)
951 			if (dev != hostb)
952 				continue;
953 
954 		if (dflags & BHND_DF_SOC)
955 			if (attach_type != BHND_ATTACH_NATIVE)
956 				continue;
957 
958 		/* device found */
959 		return (entry);
960 	}
961 
962 	/* not found */
963 	return (NULL);
964 }
965 
966 /**
967  * Scan the device @p table for all quirk flags applicable to @p dev.
968  *
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.
972  *
973  * @return all matching quirk flags.
974  */
975 uint32_t
976 bhnd_device_quirks(device_t dev, const struct bhnd_device *table,
977     size_t entry_size)
978 {
979 	const struct bhnd_device	*dent;
980 	const struct bhnd_device_quirk	*qent, *qtable;
981 	uint32_t			 quirks;
982 
983 	/* Locate the device entry */
984 	if ((dent = bhnd_device_lookup(dev, table, entry_size)) == NULL)
985 		return (0);
986 
987 	/* Quirks table is optional */
988 	qtable = dent->quirks_table;
989 	if (qtable == NULL)
990 		return (0);
991 
992 	/* Collect matching device quirk entries */
993 	quirks = 0;
994 	for (qent = qtable; !BHND_DEVICE_QUIRK_IS_END(qent); qent++) {
995 		if (bhnd_device_matches(dev, &qent->desc))
996 			quirks |= qent->quirks;
997 	}
998 
999 	return (quirks);
1000 }
1001 
1002 
1003 /**
1004  * Allocate bhnd(4) resources defined in @p rs from a parent bus.
1005  *
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.
1010  *
1011  * @retval 0 success
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.
1015  */
1016 int
1017 bhnd_alloc_resources(device_t dev, struct resource_spec *rs,
1018     struct bhnd_resource **res)
1019 {
1020 	/* Initialize output array */
1021 	for (u_int i = 0; rs[i].type != -1; i++)
1022 		res[i] = NULL;
1023 
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,
1026 		    rs[i].flags);
1027 
1028 		/* Clean up all allocations on failure */
1029 		if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
1030 			bhnd_release_resources(dev, rs, res);
1031 			return (ENXIO);
1032 		}
1033 	}
1034 
1035 	return (0);
1036 }
1037 
1038 /**
1039  * Release bhnd(4) resources defined in @p rs from a parent bus.
1040  *
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.
1045  */
1046 void
1047 bhnd_release_resources(device_t dev, const struct resource_spec *rs,
1048     struct bhnd_resource **res)
1049 {
1050 	for (u_int i = 0; rs[i].type != -1; i++) {
1051 		if (res[i] == NULL)
1052 			continue;
1053 
1054 		bhnd_release_resource(dev, rs[i].type, rs[i].rid, res[i]);
1055 		res[i] = NULL;
1056 	}
1057 }
1058 
1059 /**
1060  * Parse the CHIPC_ID_* fields from the ChipCommon CHIPC_ID
1061  * register, returning its bhnd_chipid representation.
1062  *
1063  * @param idreg The CHIPC_ID register value.
1064  * @param enum_addr The enumeration address to include in the result.
1065  *
1066  * @warning
1067  * On early siba(4) devices, the ChipCommon core does not provide
1068  * a valid CHIPC_ID_NUMCORE field. On these ChipCommon revisions
1069  * (see CHIPC_NCORES_MIN_HWREV()), this function will parse and return
1070  * an invalid `ncores` value.
1071  */
1072 struct bhnd_chipid
1073 bhnd_parse_chipid(uint32_t idreg, bhnd_addr_t enum_addr)
1074 {
1075 	struct bhnd_chipid result;
1076 
1077 	/* Fetch the basic chip info */
1078 	result.chip_id = CHIPC_GET_BITS(idreg, CHIPC_ID_CHIP);
1079 	result.chip_pkg = CHIPC_GET_BITS(idreg, CHIPC_ID_PKG);
1080 	result.chip_rev = CHIPC_GET_BITS(idreg, CHIPC_ID_REV);
1081 	result.chip_type = CHIPC_GET_BITS(idreg, CHIPC_ID_BUS);
1082 	result.ncores = CHIPC_GET_BITS(idreg, CHIPC_ID_NUMCORE);
1083 
1084 	result.enum_addr = enum_addr;
1085 
1086 	return (result);
1087 }
1088 
1089 
1090 /**
1091  * Determine the correct core count for a chip identification value that
1092  * may contain an invalid core count.
1093  *
1094  * On some early siba(4) devices (see CHIPC_NCORES_MIN_HWREV()), the ChipCommon
1095  * core does not provide a valid CHIPC_ID_NUMCORE field.
1096  *
1097  * @param cid The chip identification to be queried.
1098  * @param chipc_hwrev The hardware revision of the ChipCommon core from which
1099  * @p cid was parsed.
1100  * @param[out] ncores On success, will be set to the correct core count.
1101  *
1102  * @retval 0 If the core count is already correct, or was mapped to a
1103  * a correct value.
1104  * @retval EINVAL If the core count is incorrect, but the chip was not
1105  * recognized.
1106  */
1107 int
1108 bhnd_chipid_fixed_ncores(const struct bhnd_chipid *cid, uint16_t chipc_hwrev,
1109     uint8_t *ncores)
1110 {
1111 	/* bcma(4), and most siba(4) devices */
1112 	if (CHIPC_NCORES_MIN_HWREV(chipc_hwrev)) {
1113 		*ncores = cid->ncores;
1114 		return (0);
1115 	}
1116 
1117 	/* broken siba(4) chipsets */
1118 	switch (cid->chip_id) {
1119 	case BHND_CHIPID_BCM4306:
1120 		*ncores = 6;
1121 		break;
1122 	case BHND_CHIPID_BCM4704:
1123 		*ncores = 9;
1124 		break;
1125 	case BHND_CHIPID_BCM5365:
1126 		/*
1127 		* BCM5365 does support ID_NUMCORE in at least
1128 		* some of its revisions, but for unknown
1129 		* reasons, Broadcom's drivers always exclude
1130 		* the ChipCommon revision (0x5) used by BCM5365
1131 		* from the set of revisions supporting
1132 		* ID_NUMCORE, and instead supply a fixed value.
1133 		*
1134 		* Presumably, at least some of these devices
1135 		* shipped with a broken ID_NUMCORE value.
1136 		*/
1137 		*ncores = 7;
1138 		break;
1139 	default:
1140 		return (EINVAL);
1141 	}
1142 
1143 	return (0);
1144 }
1145 
1146 /**
1147  * Allocate the resource defined by @p rs via @p dev, use it
1148  * to read the ChipCommon ID register relative to @p chipc_offset,
1149  * then release the resource.
1150  *
1151  * @param dev The device owning @p rs.
1152  * @param rs A resource spec that encompasses the ChipCommon register block.
1153  * @param chipc_offset The offset of the ChipCommon registers within @p rs.
1154  * @param[out] result The chip identification data.
1155  *
1156  * @retval 0 success
1157  * @retval non-zero if the ChipCommon identification data could not be read.
1158  */
1159 int
1160 bhnd_read_chipid(device_t dev, struct resource_spec *rs,
1161     bus_size_t chipc_offset, struct bhnd_chipid *result)
1162 {
1163 	struct resource			*res;
1164 	bhnd_addr_t			 enum_addr;
1165 	uint32_t			 reg;
1166 	uint8_t				 chip_type;
1167 	int				 error, rid, rtype;
1168 
1169 	rid = rs->rid;
1170 	rtype = rs->type;
1171 	error = 0;
1172 
1173 	/* Allocate the ChipCommon window resource and fetch the chipid data */
1174 	res = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
1175 	if (res == NULL) {
1176 		device_printf(dev,
1177 		    "failed to allocate bhnd chipc resource\n");
1178 		return (ENXIO);
1179 	}
1180 
1181 	/* Fetch the basic chip info */
1182 	reg = bus_read_4(res, chipc_offset + CHIPC_ID);
1183 	chip_type = CHIPC_GET_BITS(reg, CHIPC_ID_BUS);
1184 
1185 	/* Fetch the EROMPTR */
1186 	if (BHND_CHIPTYPE_HAS_EROM(chip_type)) {
1187 		enum_addr = bus_read_4(res, chipc_offset + CHIPC_EROMPTR);
1188 	} else if (chip_type == BHND_CHIPTYPE_SIBA) {
1189 		/* siba(4) uses the ChipCommon base address as the enumeration
1190 		 * address */
1191 		enum_addr = BHND_DEFAULT_CHIPC_ADDR;
1192 	} else {
1193 		device_printf(dev, "unknown chip type %hhu\n", chip_type);
1194 		error = ENODEV;
1195 		goto cleanup;
1196 	}
1197 
1198 	*result = bhnd_parse_chipid(reg, enum_addr);
1199 
1200 	/* Fix the core count on early siba(4) devices */
1201 	if (chip_type == BHND_CHIPTYPE_SIBA) {
1202 		uint32_t	idh;
1203 		uint16_t	chipc_hwrev;
1204 
1205 		/*
1206 		 * We need the ChipCommon revision to determine whether
1207 		 * the ncore field is valid.
1208 		 *
1209 		 * We can safely assume the siba IDHIGH register is mapped
1210 		 * within the chipc register block.
1211 		 */
1212 		idh = bus_read_4(res, SB0_REG_ABS(SIBA_CFG0_IDHIGH));
1213 		chipc_hwrev = SIBA_IDH_CORE_REV(idh);
1214 
1215 		error = bhnd_chipid_fixed_ncores(result, chipc_hwrev,
1216 		    &result->ncores);
1217 		if (error)
1218 			goto cleanup;
1219 	}
1220 
1221 cleanup:
1222 	/* Clean up */
1223 	bus_release_resource(dev, rtype, rid, res);
1224 	return (error);
1225 }
1226 
1227 /**
1228  * Allocate and return a new per-core PMU clock control/status (clkctl)
1229  * instance for @p dev.
1230  *
1231  * @param dev		The bhnd(4) core device mapped by @p r.
1232  * @param pmu_dev	The bhnd(4) PMU device, implmenting the bhnd_pmu_if
1233  *			interface. The caller is responsible for ensuring that
1234  *			this reference remains valid for the lifetime of the
1235  *			returned clkctl instance.
1236  * @param r		A resource mapping the core's clock control register
1237  * 			(see BHND_CLK_CTL_ST). The caller is responsible for
1238  *			ensuring that this resource remains valid for the
1239  *			lifetime of the returned clkctl instance.
1240  * @param offset	The offset to the clock control register within @p r.
1241  * @param max_latency	The PMU's maximum state transition latency in
1242  *			microseconds; this upper bound will be used to busy-wait
1243  *			on PMU state transitions.
1244  *
1245  * @retval non-NULL	success
1246  * @retval NULL		if allocation fails.
1247  *
1248  */
1249 struct bhnd_core_clkctl *
1250 bhnd_alloc_core_clkctl(device_t dev, device_t pmu_dev, struct bhnd_resource *r,
1251     bus_size_t offset, u_int max_latency)
1252 {
1253 	struct bhnd_core_clkctl	*clkctl;
1254 
1255 	clkctl = malloc(sizeof(*clkctl), M_BHND, M_ZERO | M_NOWAIT);
1256 	if (clkctl == NULL)
1257 		return (NULL);
1258 
1259 	clkctl->cc_dev = dev;
1260 	clkctl->cc_pmu_dev = pmu_dev;
1261 	clkctl->cc_res = r;
1262 	clkctl->cc_res_offset = offset;
1263 	clkctl->cc_max_latency = max_latency;
1264 	clkctl->cc_quirks = bhnd_device_quirks(dev, bhnd_clkctl_devices,
1265 	    sizeof(bhnd_clkctl_devices[0]));
1266 
1267 	BHND_CLKCTL_LOCK_INIT(clkctl);
1268 
1269 	return (clkctl);
1270 }
1271 
1272 /**
1273  * Free a clkctl instance previously allocated via bhnd_alloc_core_clkctl().
1274  *
1275  * @param clkctl	The clkctl instance to be freed.
1276  */
1277 void
1278 bhnd_free_core_clkctl(struct bhnd_core_clkctl *clkctl)
1279 {
1280 	BHND_CLKCTL_LOCK_DESTROY(clkctl);
1281 
1282 	free(clkctl, M_BHND);
1283 }
1284 
1285 /**
1286  * Wait for the per-core clock status to be equal to @p value after
1287  * applying @p mask, timing out after the maximum transition latency is reached.
1288  *
1289  * @param clkctl	Per-core clkctl state to be queryied.
1290  * @param value		Value to wait for.
1291  * @param mask		Mask to apply prior to value comparison.
1292  *
1293  * @retval 0		success
1294  * @retval ETIMEDOUT	if the PMU's maximum transition delay is reached before
1295  *			the clock status matches @p value and @p mask.
1296  */
1297 int
1298 bhnd_core_clkctl_wait(struct bhnd_core_clkctl *clkctl, uint32_t value,
1299     uint32_t mask)
1300 {
1301 	uint32_t	clkst;
1302 
1303 	BHND_CLKCTL_LOCK_ASSERT(clkctl, MA_OWNED);
1304 
1305 	/* Bitswapped HTAVAIL/ALPAVAIL work-around */
1306 	if (clkctl->cc_quirks & BHND_CLKCTL_QUIRK_CCS0) {
1307 		uint32_t fmask, fval;
1308 
1309 		fmask = mask & ~(BHND_CCS_HTAVAIL | BHND_CCS_ALPAVAIL);
1310 		fval = value & ~(BHND_CCS_HTAVAIL | BHND_CCS_ALPAVAIL);
1311 
1312 		if (mask & BHND_CCS_HTAVAIL)
1313 			fmask |= BHND_CCS0_HTAVAIL;
1314 		if (value & BHND_CCS_HTAVAIL)
1315 			fval |= BHND_CCS0_HTAVAIL;
1316 
1317 		if (mask & BHND_CCS_ALPAVAIL)
1318 			fmask |= BHND_CCS0_ALPAVAIL;
1319 		if (value & BHND_CCS_ALPAVAIL)
1320 			fval |= BHND_CCS0_ALPAVAIL;
1321 
1322 		mask = fmask;
1323 		value = fval;
1324 	}
1325 
1326 	for (u_int i = 0; i < clkctl->cc_max_latency; i += 10) {
1327 		clkst = bhnd_bus_read_4(clkctl->cc_res, clkctl->cc_res_offset);
1328 		if ((clkst & mask) == (value & mask))
1329 			return (0);
1330 
1331 		DELAY(10);
1332 	}
1333 
1334 	device_printf(clkctl->cc_dev, "clkst wait timeout (value=%#x, "
1335 	    "mask=%#x)\n", value, mask);
1336 
1337 	return (ETIMEDOUT);
1338 }
1339 
1340 /**
1341  * Read an NVRAM variable's NUL-terminated string value.
1342  *
1343  * @param 	dev	A bhnd bus child device.
1344  * @param	name	The NVRAM variable name.
1345  * @param[out]	buf	A buffer large enough to hold @p len bytes. On
1346  *			success, the NUL-terminated string value will be
1347  *			written to this buffer. This argment may be NULL if
1348  *			the value is not desired.
1349  * @param	len	The maximum capacity of @p buf.
1350  * @param[out]	rlen	On success, will be set to the actual size of
1351  *			the requested value (including NUL termination). This
1352  *			argment may be NULL if the size is not desired.
1353  *
1354  * @retval 0		success
1355  * @retval ENOENT	The requested variable was not found.
1356  * @retval ENODEV	No valid NVRAM source could be found.
1357  * @retval ENOMEM	If @p buf is non-NULL and a buffer of @p len is too
1358  *			small to hold the requested value.
1359  * @retval EFTYPE	If the variable data cannot be coerced to a valid
1360  *			string representation.
1361  * @retval ERANGE	If value coercion would overflow @p type.
1362  * @retval non-zero	If reading @p name otherwise fails, a regular unix
1363  *			error code will be returned.
1364  */
1365 int
1366 bhnd_nvram_getvar_str(device_t dev, const char *name, char *buf, size_t len,
1367     size_t *rlen)
1368 {
1369 	size_t	larg;
1370 	int	error;
1371 
1372 	larg = len;
1373 	error = bhnd_nvram_getvar(dev, name, buf, &larg,
1374 	    BHND_NVRAM_TYPE_STRING);
1375 	if (rlen != NULL)
1376 		*rlen = larg;
1377 
1378 	return (error);
1379 }
1380 
1381 /**
1382  * Read an NVRAM variable's unsigned integer value.
1383  *
1384  * @param 		dev	A bhnd bus child device.
1385  * @param		name	The NVRAM variable name.
1386  * @param[out]		value	On success, the requested value will be written
1387  *				to this pointer.
1388  * @param		width	The output integer type width (1, 2, or
1389  *				4 bytes).
1390  *
1391  * @retval 0		success
1392  * @retval ENOENT	The requested variable was not found.
1393  * @retval ENODEV	No valid NVRAM source could be found.
1394  * @retval EFTYPE	If the variable data cannot be coerced to a
1395  *			a valid unsigned integer representation.
1396  * @retval ERANGE	If value coercion would overflow (or underflow) an
1397  *			unsigned representation of the given @p width.
1398  * @retval non-zero	If reading @p name otherwise fails, a regular unix
1399  *			error code will be returned.
1400  */
1401 int
1402 bhnd_nvram_getvar_uint(device_t dev, const char *name, void *value, int width)
1403 {
1404 	bhnd_nvram_type	type;
1405 	size_t		len;
1406 
1407 	switch (width) {
1408 	case 1:
1409 		type = BHND_NVRAM_TYPE_UINT8;
1410 		break;
1411 	case 2:
1412 		type = BHND_NVRAM_TYPE_UINT16;
1413 		break;
1414 	case 4:
1415 		type = BHND_NVRAM_TYPE_UINT32;
1416 		break;
1417 	default:
1418 		device_printf(dev, "unsupported NVRAM integer width: %d\n",
1419 		    width);
1420 		return (EINVAL);
1421 	}
1422 
1423 	len = width;
1424 	return (bhnd_nvram_getvar(dev, name, value, &len, type));
1425 }
1426 
1427 /**
1428  * Read an NVRAM variable's unsigned 8-bit integer value.
1429  *
1430  * @param 		dev	A bhnd bus child device.
1431  * @param		name	The NVRAM variable name.
1432  * @param[out]		value	On success, the requested value will be written
1433  *				to this pointer.
1434  *
1435  * @retval 0		success
1436  * @retval ENOENT	The requested variable was not found.
1437  * @retval ENODEV	No valid NVRAM source could be found.
1438  * @retval EFTYPE	If the variable data cannot be coerced to a
1439  *			a valid unsigned integer representation.
1440  * @retval ERANGE	If value coercion would overflow (or underflow) uint8_t.
1441  * @retval non-zero	If reading @p name otherwise fails, a regular unix
1442  *			error code will be returned.
1443  */
1444 int
1445 bhnd_nvram_getvar_uint8(device_t dev, const char *name, uint8_t *value)
1446 {
1447 	return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1448 }
1449 
1450 /**
1451  * Read an NVRAM variable's unsigned 16-bit integer value.
1452  *
1453  * @param 		dev	A bhnd bus child device.
1454  * @param		name	The NVRAM variable name.
1455  * @param[out]		value	On success, the requested value will be written
1456  *				to this pointer.
1457  *
1458  * @retval 0		success
1459  * @retval ENOENT	The requested variable was not found.
1460  * @retval ENODEV	No valid NVRAM source could be found.
1461  * @retval EFTYPE	If the variable data cannot be coerced to a
1462  *			a valid unsigned integer representation.
1463  * @retval ERANGE	If value coercion would overflow (or underflow)
1464  *			uint16_t.
1465  * @retval non-zero	If reading @p name otherwise fails, a regular unix
1466  *			error code will be returned.
1467  */
1468 int
1469 bhnd_nvram_getvar_uint16(device_t dev, const char *name, uint16_t *value)
1470 {
1471 	return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1472 }
1473 
1474 /**
1475  * Read an NVRAM variable's unsigned 32-bit integer value.
1476  *
1477  * @param 		dev	A bhnd bus child device.
1478  * @param		name	The NVRAM variable name.
1479  * @param[out]		value	On success, the requested value will be written
1480  *				to this pointer.
1481  *
1482  * @retval 0		success
1483  * @retval ENOENT	The requested variable was not found.
1484  * @retval ENODEV	No valid NVRAM source could be found.
1485  * @retval EFTYPE	If the variable data cannot be coerced to a
1486  *			a valid unsigned integer representation.
1487  * @retval ERANGE	If value coercion would overflow (or underflow)
1488  *			uint32_t.
1489  * @retval non-zero	If reading @p name otherwise fails, a regular unix
1490  *			error code will be returned.
1491  */
1492 int
1493 bhnd_nvram_getvar_uint32(device_t dev, const char *name, uint32_t *value)
1494 {
1495 	return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1496 }
1497 
1498 /**
1499  * Read an NVRAM variable's signed integer value.
1500  *
1501  * @param 		dev	A bhnd bus child device.
1502  * @param		name	The NVRAM variable name.
1503  * @param[out]		value	On success, the requested value will be written
1504  *				to this pointer.
1505  * @param		width	The output integer type width (1, 2, or
1506  *				4 bytes).
1507  *
1508  * @retval 0		success
1509  * @retval ENOENT	The requested variable was not found.
1510  * @retval ENODEV	No valid NVRAM source could be found.
1511  * @retval EFTYPE	If the variable data cannot be coerced to a
1512  *			a valid integer representation.
1513  * @retval ERANGE	If value coercion would overflow (or underflow) an
1514  *			signed representation of the given @p width.
1515  * @retval non-zero	If reading @p name otherwise fails, a regular unix
1516  *			error code will be returned.
1517  */
1518 int
1519 bhnd_nvram_getvar_int(device_t dev, const char *name, void *value, int width)
1520 {
1521 	bhnd_nvram_type	type;
1522 	size_t		len;
1523 
1524 	switch (width) {
1525 	case 1:
1526 		type = BHND_NVRAM_TYPE_INT8;
1527 		break;
1528 	case 2:
1529 		type = BHND_NVRAM_TYPE_INT16;
1530 		break;
1531 	case 4:
1532 		type = BHND_NVRAM_TYPE_INT32;
1533 		break;
1534 	default:
1535 		device_printf(dev, "unsupported NVRAM integer width: %d\n",
1536 		    width);
1537 		return (EINVAL);
1538 	}
1539 
1540 	len = width;
1541 	return (bhnd_nvram_getvar(dev, name, value, &len, type));
1542 }
1543 
1544 /**
1545  * Read an NVRAM variable's signed 8-bit integer value.
1546  *
1547  * @param 		dev	A bhnd bus child device.
1548  * @param		name	The NVRAM variable name.
1549  * @param[out]		value	On success, the requested value will be written
1550  *				to this pointer.
1551  *
1552  * @retval 0		success
1553  * @retval ENOENT	The requested variable was not found.
1554  * @retval ENODEV	No valid NVRAM source could be found.
1555  * @retval EFTYPE	If the variable data cannot be coerced to a
1556  *			a valid integer representation.
1557  * @retval ERANGE	If value coercion would overflow (or underflow) int8_t.
1558  * @retval non-zero	If reading @p name otherwise fails, a regular unix
1559  *			error code will be returned.
1560  */
1561 int
1562 bhnd_nvram_getvar_int8(device_t dev, const char *name, int8_t *value)
1563 {
1564 	return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1565 }
1566 
1567 /**
1568  * Read an NVRAM variable's signed 16-bit integer value.
1569  *
1570  * @param 		dev	A bhnd bus child device.
1571  * @param		name	The NVRAM variable name.
1572  * @param[out]		value	On success, the requested value will be written
1573  *				to this pointer.
1574  *
1575  * @retval 0		success
1576  * @retval ENOENT	The requested variable was not found.
1577  * @retval ENODEV	No valid NVRAM source could be found.
1578  * @retval EFTYPE	If the variable data cannot be coerced to a
1579  *			a valid integer representation.
1580  * @retval ERANGE	If value coercion would overflow (or underflow)
1581  *			int16_t.
1582  * @retval non-zero	If reading @p name otherwise fails, a regular unix
1583  *			error code will be returned.
1584  */
1585 int
1586 bhnd_nvram_getvar_int16(device_t dev, const char *name, int16_t *value)
1587 {
1588 	return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1589 }
1590 
1591 /**
1592  * Read an NVRAM variable's signed 32-bit integer value.
1593  *
1594  * @param 		dev	A bhnd bus child device.
1595  * @param		name	The NVRAM variable name.
1596  * @param[out]		value	On success, the requested value will be written
1597  *				to this pointer.
1598  *
1599  * @retval 0		success
1600  * @retval ENOENT	The requested variable was not found.
1601  * @retval ENODEV	No valid NVRAM source could be found.
1602  * @retval EFTYPE	If the variable data cannot be coerced to a
1603  *			a valid integer representation.
1604  * @retval ERANGE	If value coercion would overflow (or underflow)
1605  *			int32_t.
1606  * @retval non-zero	If reading @p name otherwise fails, a regular unix
1607  *			error code will be returned.
1608  */
1609 int
1610 bhnd_nvram_getvar_int32(device_t dev, const char *name, int32_t *value)
1611 {
1612 	return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1613 }
1614 
1615 
1616 /**
1617  * Read an NVRAM variable's array value.
1618  *
1619  * @param 		dev	A bhnd bus child device.
1620  * @param		name	The NVRAM variable name.
1621  * @param[out]		buf	A buffer large enough to hold @p size bytes.
1622  *				On success, the requested value will be written
1623  *				to this buffer.
1624  * @param[in,out]	size	The required number of bytes to write to
1625  *				@p buf.
1626  * @param		type	The desired array element data representation.
1627  *
1628  * @retval 0		success
1629  * @retval ENOENT	The requested variable was not found.
1630  * @retval ENODEV	No valid NVRAM source could be found.
1631  * @retval ENXIO	If less than @p size bytes are available.
1632  * @retval ENOMEM	If a buffer of @p size is too small to hold the
1633  *			requested value.
1634  * @retval EFTYPE	If the variable data cannot be coerced to a
1635  *			a valid instance of @p type.
1636  * @retval ERANGE	If value coercion would overflow (or underflow) a
1637  *			representation of @p type.
1638  * @retval non-zero	If reading @p name otherwise fails, a regular unix
1639  *			error code will be returned.
1640  */
1641 int
1642 bhnd_nvram_getvar_array(device_t dev, const char *name, void *buf, size_t size,
1643     bhnd_nvram_type type)
1644 {
1645 	size_t	nbytes;
1646 	int	error;
1647 
1648 	/* Attempt read */
1649 	nbytes = size;
1650 	if ((error = bhnd_nvram_getvar(dev, name, buf, &nbytes, type)))
1651 		return (error);
1652 
1653 	/* Verify that the expected number of bytes were fetched */
1654 	if (nbytes < size)
1655 		return (ENXIO);
1656 
1657 	return (0);
1658 }
1659 
1660 /**
1661  * Initialize a service provider registry.
1662  *
1663  * @param bsr		The service registry to initialize.
1664  *
1665  * @retval 0            success
1666  * @retval non-zero     if an error occurs initializing the service registry,
1667  *                      a regular unix error code will be returned.
1668 
1669  */
1670 int
1671 bhnd_service_registry_init(struct bhnd_service_registry *bsr)
1672 {
1673 	STAILQ_INIT(&bsr->entries);
1674 	mtx_init(&bsr->lock, "bhnd_service_registry lock", NULL, MTX_DEF);
1675 
1676 	return (0);
1677 }
1678 
1679 /**
1680  * Release all resources held by @p bsr.
1681  *
1682  * @param bsr		A service registry instance previously successfully
1683  *			initialized via bhnd_service_registry_init().
1684  *
1685  * @retval 0		success
1686  * @retval EBUSY	if active references to service providers registered
1687  *			with @p bsr exist.
1688  */
1689 int
1690 bhnd_service_registry_fini(struct bhnd_service_registry *bsr)
1691 {
1692 	struct bhnd_service_entry *entry, *enext;
1693 
1694 	/* Remove everthing we can */
1695 	mtx_lock(&bsr->lock);
1696 	STAILQ_FOREACH_SAFE(entry, &bsr->entries, link, enext) {
1697 		if (entry->refs > 0)
1698 			continue;
1699 
1700 		STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry, link);
1701 		free(entry, M_BHND);
1702 	}
1703 
1704 	if (!STAILQ_EMPTY(&bsr->entries)) {
1705 		mtx_unlock(&bsr->lock);
1706 		return (EBUSY);
1707 	}
1708 	mtx_unlock(&bsr->lock);
1709 
1710 	mtx_destroy(&bsr->lock);
1711 	return (0);
1712 }
1713 
1714 /**
1715  * Register a @p provider for the given @p service.
1716  *
1717  * @param bsr		Service registry to be modified.
1718  * @param provider	Service provider to register.
1719  * @param service	Service for which @p provider will be registered.
1720  * @param flags		Service provider flags (see BHND_SPF_*).
1721  *
1722  * @retval 0		success
1723  * @retval EEXIST	if an entry for @p service already exists.
1724  * @retval EINVAL	if @p service is BHND_SERVICE_ANY.
1725  * @retval non-zero	if registering @p provider otherwise fails, a regular
1726  *			unix error code will be returned.
1727  */
1728 int
1729 bhnd_service_registry_add(struct bhnd_service_registry *bsr, device_t provider,
1730     bhnd_service_t service, uint32_t flags)
1731 {
1732 	struct bhnd_service_entry *entry;
1733 
1734 	if (service == BHND_SERVICE_ANY)
1735 		return (EINVAL);
1736 
1737 	mtx_lock(&bsr->lock);
1738 
1739 	/* Is a service provider already registered? */
1740 	STAILQ_FOREACH(entry, &bsr->entries, link) {
1741 		if (entry->service == service) {
1742 			mtx_unlock(&bsr->lock);
1743 			return (EEXIST);
1744 		}
1745 	}
1746 
1747 	/* Initialize and insert our new entry */
1748 	entry = malloc(sizeof(*entry), M_BHND, M_NOWAIT);
1749 	if (entry == NULL) {
1750 		mtx_unlock(&bsr->lock);
1751 		return (ENOMEM);
1752 	}
1753 
1754 	entry->provider = provider;
1755 	entry->service = service;
1756 	entry->flags = flags;
1757 	refcount_init(&entry->refs, 0);
1758 
1759 	STAILQ_INSERT_HEAD(&bsr->entries, entry, link);
1760 
1761 	mtx_unlock(&bsr->lock);
1762 	return (0);
1763 }
1764 
1765 /**
1766  * Free an unreferenced registry entry.
1767  *
1768  * @param entry	The entry to be deallocated.
1769  */
1770 static void
1771 bhnd_service_registry_free_entry(struct bhnd_service_entry *entry)
1772 {
1773 	KASSERT(entry->refs == 0, ("provider has active references"));
1774 	free(entry, M_BHND);
1775 }
1776 
1777 /**
1778  * Attempt to remove the @p service provider registration for @p provider.
1779  *
1780  * @param bsr		The service registry to be modified.
1781  * @param provider	The service provider to be deregistered.
1782  * @param service	The service for which @p provider will be deregistered,
1783  *			or BHND_SERVICE_ANY to remove all service
1784  *			registrations for @p provider.
1785  *
1786  * @retval 0		success
1787  * @retval EBUSY	if active references to @p provider exist; see
1788  *			bhnd_service_registry_retain() and
1789  *			bhnd_service_registry_release().
1790  */
1791 int
1792 bhnd_service_registry_remove(struct bhnd_service_registry *bsr,
1793     device_t provider, bhnd_service_t service)
1794 {
1795 	struct bhnd_service_entry *entry, *enext;
1796 
1797 	mtx_lock(&bsr->lock);
1798 
1799 #define	BHND_PROV_MATCH(_e)	\
1800 	((_e)->provider == provider &&	\
1801 	 (service == BHND_SERVICE_ANY || (_e)->service == service))
1802 
1803 	/* Validate matching provider entries before making any
1804 	 * modifications */
1805 	STAILQ_FOREACH(entry, &bsr->entries, link) {
1806 		/* Skip non-matching entries */
1807 		if (!BHND_PROV_MATCH(entry))
1808 			continue;
1809 
1810 		/* Entry is in use? */
1811 		if (entry->refs > 0) {
1812 			mtx_unlock(&bsr->lock);
1813 			return (EBUSY);
1814 		}
1815 	}
1816 
1817 	/* We can now safely remove matching entries */
1818 	STAILQ_FOREACH_SAFE(entry, &bsr->entries, link, enext) {
1819 		/* Skip non-matching entries */
1820 		if (!BHND_PROV_MATCH(entry))
1821 			continue;
1822 
1823 		/* Remove from list */
1824 		STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry, link);
1825 
1826 		/* Free provider entry */
1827 		bhnd_service_registry_free_entry(entry);
1828 	}
1829 #undef	BHND_PROV_MATCH
1830 
1831 	mtx_unlock(&bsr->lock);
1832 	return (0);
1833 }
1834 
1835 /**
1836  * Retain and return a reference to a registered @p service provider, if any.
1837  *
1838  * @param bsr		The service registry to be queried.
1839  * @param service	The service for which a provider should be returned.
1840  *
1841  * On success, the caller assumes ownership the returned provider, and
1842  * is responsible for releasing this reference via
1843  * bhnd_service_registry_release().
1844  *
1845  * @retval device_t	success
1846  * @retval NULL		if no provider is registered for @p service.
1847  */
1848 device_t
1849 bhnd_service_registry_retain(struct bhnd_service_registry *bsr,
1850     bhnd_service_t service)
1851 {
1852 	struct bhnd_service_entry *entry;
1853 
1854 	mtx_lock(&bsr->lock);
1855 	STAILQ_FOREACH(entry, &bsr->entries, link) {
1856 		if (entry->service != service)
1857 			continue;
1858 
1859 		/* With a live refcount, entry is gauranteed to remain alive
1860 		 * after we release our lock */
1861 		refcount_acquire(&entry->refs);
1862 
1863 		mtx_unlock(&bsr->lock);
1864 		return (entry->provider);
1865 	}
1866 	mtx_unlock(&bsr->lock);
1867 
1868 	/* Not found */
1869 	return (NULL);
1870 }
1871 
1872 /**
1873  * Release a reference to a service provider previously returned by
1874  * bhnd_service_registry_retain().
1875  *
1876  * If this is the last reference to an inherited service provider registration
1877  * (see BHND_SPF_INHERITED), the registration will also be removed, and
1878  * true will be returned.
1879  *
1880  * @param bsr		The service registry from which @p provider
1881  *			was returned.
1882  * @param provider	The provider to be released.
1883  * @param service	The service for which @p provider was previously
1884  *			retained.
1885  * @retval true		The inherited service provider registration was removed;
1886  *			the caller should release its own reference to the
1887  *			provider.
1888  * @retval false	The service provider was not inherited, or active
1889  *			references to the provider remain.
1890  *
1891  * @see BHND_SPF_INHERITED
1892  */
1893 bool
1894 bhnd_service_registry_release(struct bhnd_service_registry *bsr,
1895     device_t provider, bhnd_service_t service)
1896 {
1897 	struct bhnd_service_entry *entry;
1898 
1899 	/* Exclusive lock, as we need to prevent any new references to the
1900 	 * entry from being taken if it's to be removed */
1901 	mtx_lock(&bsr->lock);
1902 	STAILQ_FOREACH(entry, &bsr->entries, link) {
1903 		bool removed;
1904 
1905 		if (entry->provider != provider)
1906 			continue;
1907 
1908 		if (entry->service != service)
1909 			continue;
1910 
1911 		if (refcount_release(&entry->refs) &&
1912 		    (entry->flags & BHND_SPF_INHERITED))
1913 		{
1914 			/* If an inherited entry is no longer actively
1915 			 * referenced, remove the local registration and inform
1916 			 * the caller. */
1917 			STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry,
1918 			    link);
1919 			bhnd_service_registry_free_entry(entry);
1920 			removed = true;
1921 		} else {
1922 			removed = false;
1923 		}
1924 
1925 		mtx_unlock(&bsr->lock);
1926 		return (removed);
1927 	}
1928 
1929 	/* Caller owns a reference, but no such provider is registered? */
1930 	panic("invalid service provider reference");
1931 }
1932 
1933 /**
1934  * Using the bhnd(4) bus-level core information and a custom core name,
1935  * populate @p dev's device description.
1936  *
1937  * @param dev A bhnd-bus attached device.
1938  * @param dev_name The core's name (e.g. "SDIO Device Core").
1939  */
1940 void
1941 bhnd_set_custom_core_desc(device_t dev, const char *dev_name)
1942 {
1943 	const char *vendor_name;
1944 	char *desc;
1945 
1946 	vendor_name = bhnd_get_vendor_name(dev);
1947 	asprintf(&desc, M_BHND, "%s %s, rev %hhu", vendor_name, dev_name,
1948 	    bhnd_get_hwrev(dev));
1949 
1950 	if (desc != NULL) {
1951 		device_set_desc_copy(dev, desc);
1952 		free(desc, M_BHND);
1953 	} else {
1954 		device_set_desc(dev, dev_name);
1955 	}
1956 }
1957 
1958 /**
1959  * Using the bhnd(4) bus-level core information, populate @p dev's device
1960  * description.
1961  *
1962  * @param dev A bhnd-bus attached device.
1963  */
1964 void
1965 bhnd_set_default_core_desc(device_t dev)
1966 {
1967 	bhnd_set_custom_core_desc(dev, bhnd_get_device_name(dev));
1968 }
1969 
1970 
1971 /**
1972  * Using the bhnd @p chip_id, populate the bhnd(4) bus @p dev's device
1973  * description.
1974  *
1975  * @param dev A bhnd-bus attached device.
1976  * @param chip_id The chip identification.
1977  */
1978 void
1979 bhnd_set_default_bus_desc(device_t dev, const struct bhnd_chipid *chip_id)
1980 {
1981 	const char	*bus_name;
1982 	char		*desc;
1983 	char		 chip_name[BHND_CHIPID_MAX_NAMELEN];
1984 
1985 	/* Determine chip type's bus name */
1986 	switch (chip_id->chip_type) {
1987 	case BHND_CHIPTYPE_SIBA:
1988 		bus_name = "SIBA bus";
1989 		break;
1990 	case BHND_CHIPTYPE_BCMA:
1991 	case BHND_CHIPTYPE_BCMA_ALT:
1992 		bus_name = "BCMA bus";
1993 		break;
1994 	case BHND_CHIPTYPE_UBUS:
1995 		bus_name = "UBUS bus";
1996 		break;
1997 	default:
1998 		bus_name = "Unknown Type";
1999 		break;
2000 	}
2001 
2002 	/* Format chip name */
2003 	bhnd_format_chip_id(chip_name, sizeof(chip_name),
2004 	     chip_id->chip_id);
2005 
2006 	/* Format and set device description */
2007 	asprintf(&desc, M_BHND, "%s %s", chip_name, bus_name);
2008 	if (desc != NULL) {
2009 		device_set_desc_copy(dev, desc);
2010 		free(desc, M_BHND);
2011 	} else {
2012 		device_set_desc(dev, bus_name);
2013 	}
2014 
2015 }
2016 
2017 /**
2018  * Helper function for implementing BHND_BUS_REGISTER_PROVIDER().
2019  *
2020  * This implementation delegates the request to the BHND_BUS_REGISTER_PROVIDER()
2021  * method on the parent of @p dev. If no parent exists, the implementation
2022  * will return an error.
2023  */
2024 int
2025 bhnd_bus_generic_register_provider(device_t dev, device_t child,
2026     device_t provider, bhnd_service_t service)
2027 {
2028 	device_t parent = device_get_parent(dev);
2029 
2030 	if (parent != NULL) {
2031 		return (BHND_BUS_REGISTER_PROVIDER(parent, child,
2032 		    provider, service));
2033 	}
2034 
2035 	return (ENXIO);
2036 }
2037 
2038 /**
2039  * Helper function for implementing BHND_BUS_DEREGISTER_PROVIDER().
2040  *
2041  * This implementation delegates the request to the
2042  * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
2043  * exists, the implementation will panic.
2044  */
2045 int
2046 bhnd_bus_generic_deregister_provider(device_t dev, device_t child,
2047     device_t provider, bhnd_service_t service)
2048 {
2049 	device_t parent = device_get_parent(dev);
2050 
2051 	if (parent != NULL) {
2052 		return (BHND_BUS_DEREGISTER_PROVIDER(parent, child,
2053 		    provider, service));
2054 	}
2055 
2056 	panic("missing BHND_BUS_DEREGISTER_PROVIDER()");
2057 }
2058 
2059 /**
2060  * Helper function for implementing BHND_BUS_RETAIN_PROVIDER().
2061  *
2062  * This implementation delegates the request to the
2063  * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
2064  * exists, the implementation will return NULL.
2065  */
2066 device_t
2067 bhnd_bus_generic_retain_provider(device_t dev, device_t child,
2068     bhnd_service_t service)
2069 {
2070 	device_t parent = device_get_parent(dev);
2071 
2072 	if (parent != NULL) {
2073 		return (BHND_BUS_RETAIN_PROVIDER(parent, child,
2074 		    service));
2075 	}
2076 
2077 	return (NULL);
2078 }
2079 
2080 /**
2081  * Helper function for implementing BHND_BUS_RELEASE_PROVIDER().
2082  *
2083  * This implementation delegates the request to the
2084  * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
2085  * exists, the implementation will panic.
2086  */
2087 void
2088 bhnd_bus_generic_release_provider(device_t dev, device_t child,
2089     device_t provider, bhnd_service_t service)
2090 {
2091 	device_t parent = device_get_parent(dev);
2092 
2093 	if (parent != NULL) {
2094 		return (BHND_BUS_RELEASE_PROVIDER(parent, child,
2095 		    provider, service));
2096 	}
2097 
2098 	panic("missing BHND_BUS_RELEASE_PROVIDER()");
2099 }
2100 
2101 /**
2102  * Helper function for implementing BHND_BUS_REGISTER_PROVIDER().
2103  *
2104  * This implementation uses the bhnd_service_registry_add() function to
2105  * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
2106  * a suitable service registry to edit.
2107  */
2108 int
2109 bhnd_bus_generic_sr_register_provider(device_t dev, device_t child,
2110     device_t provider, bhnd_service_t service)
2111 {
2112 	struct bhnd_service_registry *bsr;
2113 
2114 	bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
2115 
2116 	KASSERT(bsr != NULL, ("NULL service registry"));
2117 
2118 	return (bhnd_service_registry_add(bsr, provider, service, 0));
2119 }
2120 
2121 /**
2122  * Helper function for implementing BHND_BUS_DEREGISTER_PROVIDER().
2123  *
2124  * This implementation uses the bhnd_service_registry_remove() function to
2125  * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
2126  * a suitable service registry to edit.
2127  */
2128 int
2129 bhnd_bus_generic_sr_deregister_provider(device_t dev, device_t child,
2130     device_t provider, bhnd_service_t service)
2131 {
2132 	struct bhnd_service_registry *bsr;
2133 
2134 	bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
2135 
2136 	KASSERT(bsr != NULL, ("NULL service registry"));
2137 
2138 	return (bhnd_service_registry_remove(bsr, provider, service));
2139 }
2140 
2141 /**
2142  * Helper function for implementing BHND_BUS_RETAIN_PROVIDER().
2143  *
2144  * This implementation uses the bhnd_service_registry_retain() function to
2145  * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
2146  * a suitable service registry.
2147  *
2148  * If a local provider for the service is not available, and a parent device is
2149  * available, this implementation will attempt to fetch and locally register
2150  * a service provider reference from the parent of @p dev.
2151  */
2152 device_t
2153 bhnd_bus_generic_sr_retain_provider(device_t dev, device_t child,
2154     bhnd_service_t service)
2155 {
2156 	struct bhnd_service_registry	*bsr;
2157 	device_t			 parent, provider;
2158 	int				 error;
2159 
2160 	bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
2161 	KASSERT(bsr != NULL, ("NULL service registry"));
2162 
2163 	/*
2164 	 * Attempt to fetch a service provider reference from either the local
2165 	 * service registry, or if not found, from our parent.
2166 	 *
2167 	 * If we fetch a provider from our parent, we register the provider
2168 	 * with the local service registry to prevent conflicting local
2169 	 * registrations from being added.
2170 	 */
2171 	while (1) {
2172 		/* Check the local service registry first */
2173 		provider = bhnd_service_registry_retain(bsr, service);
2174 		if (provider != NULL)
2175 			return (provider);
2176 
2177 		/* Otherwise, try to delegate to our parent (if any) */
2178 		if ((parent = device_get_parent(dev)) == NULL)
2179 			return (NULL);
2180 
2181 		provider = BHND_BUS_RETAIN_PROVIDER(parent, dev, service);
2182 		if (provider == NULL)
2183 			return (NULL);
2184 
2185 		/* Register the inherited service registration with the local
2186 		 * registry */
2187 		error = bhnd_service_registry_add(bsr, provider, service,
2188 		    BHND_SPF_INHERITED);
2189 		if (error) {
2190 			BHND_BUS_RELEASE_PROVIDER(parent, dev, provider,
2191 			    service);
2192 			if (error == EEXIST) {
2193 				/* A valid service provider was registered
2194 				 * concurrently; retry fetching from the local
2195 				 * registry */
2196 				continue;
2197 			}
2198 
2199 			device_printf(dev, "failed to register service "
2200 			    "provider: %d\n", error);
2201 			return (NULL);
2202 		}
2203 	}
2204 }
2205 
2206 /**
2207  * Helper function for implementing BHND_BUS_RELEASE_PROVIDER().
2208  *
2209  * This implementation uses the bhnd_service_registry_release() function to
2210  * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
2211  * a suitable service registry.
2212  */
2213 void
2214 bhnd_bus_generic_sr_release_provider(device_t dev, device_t child,
2215     device_t provider, bhnd_service_t service)
2216 {
2217 	struct bhnd_service_registry	*bsr;
2218 
2219 	bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
2220 	KASSERT(bsr != NULL, ("NULL service registry"));
2221 
2222 	/* Release the provider reference; if the refcount hits zero on an
2223 	 * inherited reference, true will be returned, and we need to drop
2224 	 * our own bus reference to the provider */
2225 	if (!bhnd_service_registry_release(bsr, provider, service))
2226 		return;
2227 
2228 	/* Drop our reference to the borrowed provider */
2229 	BHND_BUS_RELEASE_PROVIDER(device_get_parent(dev), dev, provider,
2230 	    service);
2231 }
2232 
2233 /**
2234  * Helper function for implementing BHND_BUS_IS_HW_DISABLED().
2235  *
2236  * If a parent device is available, this implementation delegates the
2237  * request to the BHND_BUS_IS_HW_DISABLED() method on the parent of @p dev.
2238  *
2239  * If no parent device is available (i.e. on a the bus root), the hardware
2240  * is assumed to be usable and false is returned.
2241  */
2242 bool
2243 bhnd_bus_generic_is_hw_disabled(device_t dev, device_t child)
2244 {
2245 	if (device_get_parent(dev) != NULL)
2246 		return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child));
2247 
2248 	return (false);
2249 }
2250 
2251 /**
2252  * Helper function for implementing BHND_BUS_GET_CHIPID().
2253  *
2254  * This implementation delegates the request to the BHND_BUS_GET_CHIPID()
2255  * method on the parent of @p dev. If no parent exists, the implementation
2256  * will panic.
2257  */
2258 const struct bhnd_chipid *
2259 bhnd_bus_generic_get_chipid(device_t dev, device_t child)
2260 {
2261 	if (device_get_parent(dev) != NULL)
2262 		return (BHND_BUS_GET_CHIPID(device_get_parent(dev), child));
2263 
2264 	panic("missing BHND_BUS_GET_CHIPID()");
2265 }
2266 
2267 /**
2268  * Helper function for implementing BHND_BUS_GET_DMA_TRANSLATION().
2269  *
2270  * If a parent device is available, this implementation delegates the
2271  * request to the BHND_BUS_GET_DMA_TRANSLATION() method on the parent of @p dev.
2272  *
2273  * If no parent device is available, this implementation will panic.
2274  */
2275 int
2276 bhnd_bus_generic_get_dma_translation(device_t dev, device_t child, u_int width,
2277     uint32_t flags, bus_dma_tag_t *dmat,
2278     struct bhnd_dma_translation *translation)
2279 {
2280 	if (device_get_parent(dev) != NULL) {
2281 		return (BHND_BUS_GET_DMA_TRANSLATION(device_get_parent(dev),
2282 		    child, width, flags, dmat, translation));
2283 	}
2284 
2285 	panic("missing BHND_BUS_GET_DMA_TRANSLATION()");
2286 }
2287 
2288 /* nvram board_info population macros for bhnd_bus_generic_read_board_info() */
2289 #define	BHND_GV(_dest, _name)	\
2290 	bhnd_nvram_getvar_uint(child, BHND_NVAR_ ## _name, &_dest,	\
2291 	    sizeof(_dest))
2292 
2293 #define	REQ_BHND_GV(_dest, _name)		do {			\
2294 	if ((error = BHND_GV(_dest, _name))) {				\
2295 		device_printf(dev,					\
2296 		    "error reading " __STRING(_name) ": %d\n", error);	\
2297 		return (error);						\
2298 	}								\
2299 } while(0)
2300 
2301 #define	OPT_BHND_GV(_dest, _name, _default)	do {			\
2302 	if ((error = BHND_GV(_dest, _name))) {				\
2303 		if (error != ENOENT) {					\
2304 			device_printf(dev,				\
2305 			    "error reading "				\
2306 			       __STRING(_name) ": %d\n", error);	\
2307 			return (error);					\
2308 		}							\
2309 		_dest = _default;					\
2310 	}								\
2311 } while(0)
2312 
2313 /**
2314  * Helper function for implementing BHND_BUS_READ_BOARDINFO().
2315  *
2316  * This implementation populates @p info with information from NVRAM,
2317  * defaulting board_vendor and board_type fields to 0 if the
2318  * requested variables cannot be found.
2319  *
2320  * This behavior is correct for most SoCs, but must be overridden on
2321  * bridged (PCI, PCMCIA, etc) devices to produce a complete bhnd_board_info
2322  * result.
2323  */
2324 int
2325 bhnd_bus_generic_read_board_info(device_t dev, device_t child,
2326     struct bhnd_board_info *info)
2327 {
2328 	int	error;
2329 
2330 	OPT_BHND_GV(info->board_vendor,	BOARDVENDOR,	0);
2331 	OPT_BHND_GV(info->board_type,	BOARDTYPE,	0);	/* srom >= 2 */
2332 	OPT_BHND_GV(info->board_devid,	DEVID,		0);	/* srom >= 8 */
2333 	REQ_BHND_GV(info->board_rev,	BOARDREV);
2334 	OPT_BHND_GV(info->board_srom_rev,SROMREV,	0);	/* missing in
2335 								   some SoC
2336 								   NVRAM */
2337 	REQ_BHND_GV(info->board_flags,	BOARDFLAGS);
2338 	OPT_BHND_GV(info->board_flags2,	BOARDFLAGS2,	0);	/* srom >= 4 */
2339 	OPT_BHND_GV(info->board_flags3,	BOARDFLAGS3,	0);	/* srom >= 11 */
2340 
2341 	return (0);
2342 }
2343 
2344 #undef	BHND_GV
2345 #undef	BHND_GV_REQ
2346 #undef	BHND_GV_OPT
2347 
2348 /**
2349  * Helper function for implementing BHND_BUS_GET_NVRAM_VAR().
2350  *
2351  * This implementation searches @p dev for a usable NVRAM child device.
2352  *
2353  * If no usable child device is found on @p dev, the request is delegated to
2354  * the BHND_BUS_GET_NVRAM_VAR() method on the parent of @p dev.
2355  */
2356 int
2357 bhnd_bus_generic_get_nvram_var(device_t dev, device_t child, const char *name,
2358     void *buf, size_t *size, bhnd_nvram_type type)
2359 {
2360 	device_t	nvram;
2361 	device_t	parent;
2362 
2363         /* Make sure we're holding Giant for newbus */
2364 	GIANT_REQUIRED;
2365 
2366 	/* Look for a directly-attached NVRAM child */
2367 	if ((nvram = device_find_child(dev, "bhnd_nvram", -1)) != NULL)
2368 		return BHND_NVRAM_GETVAR(nvram, name, buf, size, type);
2369 
2370 	/* Try to delegate to parent */
2371 	if ((parent = device_get_parent(dev)) == NULL)
2372 		return (ENODEV);
2373 
2374 	return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), child,
2375 	    name, buf, size, type));
2376 }
2377 
2378 /**
2379  * Helper function for implementing BHND_BUS_ALLOC_RESOURCE().
2380  *
2381  * This implementation of BHND_BUS_ALLOC_RESOURCE() delegates allocation
2382  * of the underlying resource to BUS_ALLOC_RESOURCE(), and activation
2383  * to @p dev's BHND_BUS_ACTIVATE_RESOURCE().
2384  */
2385 struct bhnd_resource *
2386 bhnd_bus_generic_alloc_resource(device_t dev, device_t child, int type,
2387 	int *rid, rman_res_t start, rman_res_t end, rman_res_t count,
2388 	u_int flags)
2389 {
2390 	struct bhnd_resource	*br;
2391 	struct resource		*res;
2392 	int			 error;
2393 
2394 	br = NULL;
2395 	res = NULL;
2396 
2397 	/* Allocate the real bus resource (without activating it) */
2398 	res = BUS_ALLOC_RESOURCE(dev, child, type, rid, start, end, count,
2399 	    (flags & ~RF_ACTIVE));
2400 	if (res == NULL)
2401 		return (NULL);
2402 
2403 	/* Allocate our bhnd resource wrapper. */
2404 	br = malloc(sizeof(struct bhnd_resource), M_BHND, M_NOWAIT);
2405 	if (br == NULL)
2406 		goto failed;
2407 
2408 	br->direct = false;
2409 	br->res = res;
2410 
2411 	/* Attempt activation */
2412 	if (flags & RF_ACTIVE) {
2413 		error = BHND_BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, br);
2414 		if (error)
2415 			goto failed;
2416 	}
2417 
2418 	return (br);
2419 
2420 failed:
2421 	if (res != NULL)
2422 		BUS_RELEASE_RESOURCE(dev, child, type, *rid, res);
2423 
2424 	free(br, M_BHND);
2425 	return (NULL);
2426 }
2427 
2428 /**
2429  * Helper function for implementing BHND_BUS_RELEASE_RESOURCE().
2430  *
2431  * This implementation of BHND_BUS_RELEASE_RESOURCE() delegates release of
2432  * the backing resource to BUS_RELEASE_RESOURCE().
2433  */
2434 int
2435 bhnd_bus_generic_release_resource(device_t dev, device_t child, int type,
2436     int rid, struct bhnd_resource *r)
2437 {
2438 	int error;
2439 
2440 	if ((error = BUS_RELEASE_RESOURCE(dev, child, type, rid, r->res)))
2441 		return (error);
2442 
2443 	free(r, M_BHND);
2444 	return (0);
2445 }
2446 
2447 
2448 /**
2449  * Helper function for implementing BHND_BUS_ACTIVATE_RESOURCE().
2450  *
2451  * This implementation of BHND_BUS_ACTIVATE_RESOURCE() first calls the
2452  * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
2453  *
2454  * If this fails, and if @p dev is the direct parent of @p child, standard
2455  * resource activation is attempted via bus_activate_resource(). This enables
2456  * direct use of the bhnd(4) resource APIs on devices that may not be attached
2457  * to a parent bhnd bus or bridge.
2458  */
2459 int
2460 bhnd_bus_generic_activate_resource(device_t dev, device_t child, int type,
2461     int rid, struct bhnd_resource *r)
2462 {
2463 	int	error;
2464 	bool	passthrough;
2465 
2466 	passthrough = (device_get_parent(child) != dev);
2467 
2468 	/* Try to delegate to the parent */
2469 	if (device_get_parent(dev) != NULL) {
2470 		error = BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev),
2471 		    child, type, rid, r);
2472 	} else {
2473 		error = ENODEV;
2474 	}
2475 
2476 	/* If bhnd(4) activation has failed and we're the child's direct
2477 	 * parent, try falling back on standard resource activation.
2478 	 */
2479 	if (error && !passthrough) {
2480 		error = bus_activate_resource(child, type, rid, r->res);
2481 		if (!error)
2482 			r->direct = true;
2483 	}
2484 
2485 	return (error);
2486 }
2487 
2488 /**
2489  * Helper function for implementing BHND_BUS_DEACTIVATE_RESOURCE().
2490  *
2491  * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the
2492  * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
2493  */
2494 int
2495 bhnd_bus_generic_deactivate_resource(device_t dev, device_t child,
2496     int type, int rid, struct bhnd_resource *r)
2497 {
2498 	if (device_get_parent(dev) != NULL)
2499 		return (BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev),
2500 		    child, type, rid, r));
2501 
2502 	return (EINVAL);
2503 }
2504 
2505 /**
2506  * Helper function for implementing BHND_BUS_GET_INTR_DOMAIN().
2507  *
2508  * This implementation simply returns the address of nearest bhnd(4) bus,
2509  * which may be @p dev; this behavior may be incompatible with FDT/OFW targets.
2510  */
2511 uintptr_t
2512 bhnd_bus_generic_get_intr_domain(device_t dev, device_t child, bool self)
2513 {
2514 	return ((uintptr_t)dev);
2515 }
2516