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