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)) ==
606 devclass_find("bhnd"),
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 * Find the first core in @p cores with the given @p class.
659 *
660 * @param cores The table to search.
661 * @param num_cores The length of @p cores.
662 * @param class The device class to match on.
663 *
664 * @retval non-NULL if a matching core is found.
665 * @retval NULL if no matching core is found.
666 */
667 const struct bhnd_core_info *
668 bhnd_find_core(const struct bhnd_core_info *cores, u_int num_cores,
669 bhnd_devclass_t class)
670 {
671 struct bhnd_core_match md = {
672 BHND_MATCH_CORE_CLASS(class)
673 };
674
675 return bhnd_match_core(cores, num_cores, &md);
676 }
677
678 /**
679 * Create an equality match descriptor for @p core.
680 *
681 * @param core The core info to be matched on.
682 *
683 * @return an equality match descriptor for @p core.
684 */
685 struct bhnd_core_match
686 bhnd_core_get_match_desc(const struct bhnd_core_info *core)
687 {
688 return ((struct bhnd_core_match) {
689 BHND_MATCH_CORE_VENDOR(core->vendor),
690 BHND_MATCH_CORE_ID(core->device),
691 BHND_MATCH_CORE_REV(HWREV_EQ(core->hwrev)),
692 BHND_MATCH_CORE_CLASS(bhnd_core_class(core)),
693 BHND_MATCH_CORE_IDX(core->core_idx),
694 BHND_MATCH_CORE_UNIT(core->unit)
695 });
696 }
697
698 /**
699 * Return true if the @p lhs is equal to @p rhs.
700 *
701 * @param lhs The first bhnd core descriptor to compare.
702 * @param rhs The second bhnd core descriptor to compare.
703 *
704 * @retval true if @p lhs is equal to @p rhs
705 * @retval false if @p lhs is not equal to @p rhs
706 */
707 bool
708 bhnd_cores_equal(const struct bhnd_core_info *lhs,
709 const struct bhnd_core_info *rhs)
710 {
711 struct bhnd_core_match md;
712
713 /* Use an equality match descriptor to perform the comparison */
714 md = bhnd_core_get_match_desc(rhs);
715 return (bhnd_core_matches(lhs, &md));
716 }
717
718 /**
719 * Return true if the @p core matches @p desc.
720 *
721 * @param core A bhnd core descriptor.
722 * @param desc A match descriptor to compare against @p core.
723 *
724 * @retval true if @p core matches @p match.
725 * @retval false if @p core does not match @p match.
726 */
727 bool
728 bhnd_core_matches(const struct bhnd_core_info *core,
729 const struct bhnd_core_match *desc)
730 {
731 if (desc->m.match.core_vendor && desc->core_vendor != core->vendor)
732 return (false);
733
734 if (desc->m.match.core_id && desc->core_id != core->device)
735 return (false);
736
737 if (desc->m.match.core_unit && desc->core_unit != core->unit)
738 return (false);
739
740 if (desc->m.match.core_rev &&
741 !bhnd_hwrev_matches(core->hwrev, &desc->core_rev))
742 return (false);
743
744 if (desc->m.match.core_idx && desc->core_idx != core->core_idx)
745 return (false);
746
747 if (desc->m.match.core_class &&
748 desc->core_class != bhnd_core_class(core))
749 return (false);
750
751 return true;
752 }
753
754 /**
755 * Return true if the @p chip matches @p desc.
756 *
757 * @param chip A bhnd chip identifier.
758 * @param desc A match descriptor to compare against @p chip.
759 *
760 * @retval true if @p chip matches @p match.
761 * @retval false if @p chip does not match @p match.
762 */
763 bool
764 bhnd_chip_matches(const struct bhnd_chipid *chip,
765 const struct bhnd_chip_match *desc)
766 {
767 if (desc->m.match.chip_id && chip->chip_id != desc->chip_id)
768 return (false);
769
770 if (desc->m.match.chip_pkg && chip->chip_pkg != desc->chip_pkg)
771 return (false);
772
773 if (desc->m.match.chip_rev &&
774 !bhnd_hwrev_matches(chip->chip_rev, &desc->chip_rev))
775 return (false);
776
777 if (desc->m.match.chip_type && chip->chip_type != desc->chip_type)
778 return (false);
779
780 return (true);
781 }
782
783 /**
784 * Return true if the @p board matches @p desc.
785 *
786 * @param board The bhnd board info.
787 * @param desc A match descriptor to compare against @p board.
788 *
789 * @retval true if @p chip matches @p match.
790 * @retval false if @p chip does not match @p match.
791 */
792 bool
793 bhnd_board_matches(const struct bhnd_board_info *board,
794 const struct bhnd_board_match *desc)
795 {
796 if (desc->m.match.board_srom_rev &&
797 !bhnd_hwrev_matches(board->board_srom_rev, &desc->board_srom_rev))
798 return (false);
799
800 if (desc->m.match.board_vendor &&
801 board->board_vendor != desc->board_vendor)
802 return (false);
803
804 if (desc->m.match.board_type && board->board_type != desc->board_type)
805 return (false);
806
807 if (desc->m.match.board_devid &&
808 board->board_devid != desc->board_devid)
809 return (false);
810
811 if (desc->m.match.board_rev &&
812 !bhnd_hwrev_matches(board->board_rev, &desc->board_rev))
813 return (false);
814
815 return (true);
816 }
817
818 /**
819 * Return true if the @p hwrev matches @p desc.
820 *
821 * @param hwrev A bhnd hardware revision.
822 * @param desc A match descriptor to compare against @p core.
823 *
824 * @retval true if @p hwrev matches @p match.
825 * @retval false if @p hwrev does not match @p match.
826 */
827 bool
828 bhnd_hwrev_matches(uint16_t hwrev, const struct bhnd_hwrev_match *desc)
829 {
830 if (desc->start != BHND_HWREV_INVALID &&
831 desc->start > hwrev)
832 return false;
833
834 if (desc->end != BHND_HWREV_INVALID &&
835 desc->end < hwrev)
836 return false;
837
838 return true;
839 }
840
841 /**
842 * Return true if the @p dev matches @p desc.
843 *
844 * @param dev A bhnd device.
845 * @param desc A match descriptor to compare against @p dev.
846 *
847 * @retval true if @p dev matches @p match.
848 * @retval false if @p dev does not match @p match.
849 */
850 bool
851 bhnd_device_matches(device_t dev, const struct bhnd_device_match *desc)
852 {
853 struct bhnd_core_info core;
854 const struct bhnd_chipid *chip;
855 struct bhnd_board_info board;
856 device_t parent;
857 int error;
858
859 /* Construct individual match descriptors */
860 struct bhnd_core_match m_core = { _BHND_CORE_MATCH_COPY(desc) };
861 struct bhnd_chip_match m_chip = { _BHND_CHIP_MATCH_COPY(desc) };
862 struct bhnd_board_match m_board = { _BHND_BOARD_MATCH_COPY(desc) };
863
864 /* Fetch and match core info */
865 if (m_core.m.match_flags) {
866 /* Only applicable to bhnd-attached cores */
867 parent = device_get_parent(dev);
868 if (device_get_devclass(parent) != devclass_find("bhnd")) {
869 device_printf(dev, "attempting to match core "
870 "attributes against non-core device\n");
871 return (false);
872 }
873
874 core = bhnd_get_core_info(dev);
875 if (!bhnd_core_matches(&core, &m_core))
876 return (false);
877 }
878
879 /* Fetch and match chip info */
880 if (m_chip.m.match_flags) {
881 chip = bhnd_get_chipid(dev);
882
883 if (!bhnd_chip_matches(chip, &m_chip))
884 return (false);
885 }
886
887 /* Fetch and match board info.
888 *
889 * This is not available until after NVRAM is up; earlier device
890 * matches should not include board requirements */
891 if (m_board.m.match_flags) {
892 if ((error = bhnd_read_board_info(dev, &board))) {
893 device_printf(dev, "failed to read required board info "
894 "during device matching: %d\n", error);
895 return (false);
896 }
897
898 if (!bhnd_board_matches(&board, &m_board))
899 return (false);
900 }
901
902 /* All matched */
903 return (true);
904 }
905
906 /**
907 * Search @p table for an entry matching @p dev.
908 *
909 * @param dev A bhnd device to match against @p table.
910 * @param table The device table to search.
911 * @param entry_size The @p table entry size, in bytes.
912 *
913 * @retval non-NULL the first matching device, if any.
914 * @retval NULL if no matching device is found in @p table.
915 */
916 const struct bhnd_device *
917 bhnd_device_lookup(device_t dev, const struct bhnd_device *table,
918 size_t entry_size)
919 {
920 const struct bhnd_device *entry;
921 device_t hostb, parent;
922 bhnd_attach_type attach_type;
923 uint32_t dflags;
924
925 parent = device_get_parent(dev);
926 hostb = bhnd_bus_find_hostb_device(parent);
927 attach_type = bhnd_get_attach_type(dev);
928
929 for (entry = table; !BHND_DEVICE_IS_END(entry); entry =
930 (const struct bhnd_device *) ((const char *) entry + entry_size))
931 {
932 /* match core info */
933 if (!bhnd_device_matches(dev, &entry->core))
934 continue;
935
936 /* match device flags */
937 dflags = entry->device_flags;
938
939 /* hostb implies BHND_ATTACH_ADAPTER requirement */
940 if (dflags & BHND_DF_HOSTB)
941 dflags |= BHND_DF_ADAPTER;
942
943 if (dflags & BHND_DF_ADAPTER)
944 if (attach_type != BHND_ATTACH_ADAPTER)
945 continue;
946
947 if (dflags & BHND_DF_HOSTB)
948 if (dev != hostb)
949 continue;
950
951 if (dflags & BHND_DF_SOC)
952 if (attach_type != BHND_ATTACH_NATIVE)
953 continue;
954
955 /* device found */
956 return (entry);
957 }
958
959 /* not found */
960 return (NULL);
961 }
962
963 /**
964 * Scan the device @p table for all quirk flags applicable to @p dev.
965 *
966 * @param dev A bhnd device to match against @p table.
967 * @param table The device table to search.
968 * @param entry_size The @p table entry size, in bytes.
969 *
970 * @return all matching quirk flags.
971 */
972 uint32_t
973 bhnd_device_quirks(device_t dev, const struct bhnd_device *table,
974 size_t entry_size)
975 {
976 const struct bhnd_device *dent;
977 const struct bhnd_device_quirk *qent, *qtable;
978 uint32_t quirks;
979
980 /* Locate the device entry */
981 if ((dent = bhnd_device_lookup(dev, table, entry_size)) == NULL)
982 return (0);
983
984 /* Quirks table is optional */
985 qtable = dent->quirks_table;
986 if (qtable == NULL)
987 return (0);
988
989 /* Collect matching device quirk entries */
990 quirks = 0;
991 for (qent = qtable; !BHND_DEVICE_QUIRK_IS_END(qent); qent++) {
992 if (bhnd_device_matches(dev, &qent->desc))
993 quirks |= qent->quirks;
994 }
995
996 return (quirks);
997 }
998
999 /**
1000 * Allocate bhnd(4) resources defined in @p rs from a parent bus.
1001 *
1002 * @param dev The device requesting ownership of the resources.
1003 * @param rs A standard bus resource specification. This will be updated
1004 * with the allocated resource's RIDs.
1005 * @param res On success, the allocated bhnd resources.
1006 *
1007 * @retval 0 success
1008 * @retval non-zero if allocation of any non-RF_OPTIONAL resource fails,
1009 * all allocated resources will be released and a regular
1010 * unix error code will be returned.
1011 */
1012 int
1013 bhnd_alloc_resources(device_t dev, struct resource_spec *rs,
1014 struct bhnd_resource **res)
1015 {
1016 /* Initialize output array */
1017 for (u_int i = 0; rs[i].type != -1; i++)
1018 res[i] = NULL;
1019
1020 for (u_int i = 0; rs[i].type != -1; i++) {
1021 res[i] = bhnd_alloc_resource_any(dev, rs[i].type, &rs[i].rid,
1022 rs[i].flags);
1023
1024 /* Clean up all allocations on failure */
1025 if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {
1026 bhnd_release_resources(dev, rs, res);
1027 return (ENXIO);
1028 }
1029 }
1030
1031 return (0);
1032 }
1033
1034 /**
1035 * Release bhnd(4) resources defined in @p rs from a parent bus.
1036 *
1037 * @param dev The device that owns the resources.
1038 * @param rs A standard bus resource specification previously initialized
1039 * by @p bhnd_alloc_resources.
1040 * @param res The bhnd resources to be released.
1041 */
1042 void
1043 bhnd_release_resources(device_t dev, const struct resource_spec *rs,
1044 struct bhnd_resource **res)
1045 {
1046 for (u_int i = 0; rs[i].type != -1; i++) {
1047 if (res[i] == NULL)
1048 continue;
1049
1050 bhnd_release_resource(dev, rs[i].type, rs[i].rid, res[i]);
1051 res[i] = NULL;
1052 }
1053 }
1054
1055 /**
1056 * Allocate and return a new per-core PMU clock control/status (clkctl)
1057 * instance for @p dev.
1058 *
1059 * @param dev The bhnd(4) core device mapped by @p r.
1060 * @param pmu_dev The bhnd(4) PMU device, implmenting the bhnd_pmu_if
1061 * interface. The caller is responsible for ensuring that
1062 * this reference remains valid for the lifetime of the
1063 * returned clkctl instance.
1064 * @param r A resource mapping the core's clock control register
1065 * (see BHND_CLK_CTL_ST). The caller is responsible for
1066 * ensuring that this resource remains valid for the
1067 * lifetime of the returned clkctl instance.
1068 * @param offset The offset to the clock control register within @p r.
1069 * @param max_latency The PMU's maximum state transition latency in
1070 * microseconds; this upper bound will be used to busy-wait
1071 * on PMU state transitions.
1072 *
1073 * @retval non-NULL success
1074 * @retval NULL if allocation fails.
1075 *
1076 */
1077 struct bhnd_core_clkctl *
1078 bhnd_alloc_core_clkctl(device_t dev, device_t pmu_dev, struct bhnd_resource *r,
1079 bus_size_t offset, u_int max_latency)
1080 {
1081 struct bhnd_core_clkctl *clkctl;
1082
1083 clkctl = malloc(sizeof(*clkctl), M_BHND, M_ZERO | M_NOWAIT);
1084 if (clkctl == NULL)
1085 return (NULL);
1086
1087 clkctl->cc_dev = dev;
1088 clkctl->cc_pmu_dev = pmu_dev;
1089 clkctl->cc_res = r;
1090 clkctl->cc_res_offset = offset;
1091 clkctl->cc_max_latency = max_latency;
1092 clkctl->cc_quirks = bhnd_device_quirks(dev, bhnd_clkctl_devices,
1093 sizeof(bhnd_clkctl_devices[0]));
1094
1095 BHND_CLKCTL_LOCK_INIT(clkctl);
1096
1097 return (clkctl);
1098 }
1099
1100 /**
1101 * Free a clkctl instance previously allocated via bhnd_alloc_core_clkctl().
1102 *
1103 * @param clkctl The clkctl instance to be freed.
1104 */
1105 void
1106 bhnd_free_core_clkctl(struct bhnd_core_clkctl *clkctl)
1107 {
1108 BHND_CLKCTL_LOCK_DESTROY(clkctl);
1109
1110 free(clkctl, M_BHND);
1111 }
1112
1113 /**
1114 * Wait for the per-core clock status to be equal to @p value after
1115 * applying @p mask, timing out after the maximum transition latency is reached.
1116 *
1117 * @param clkctl Per-core clkctl state to be queryied.
1118 * @param value Value to wait for.
1119 * @param mask Mask to apply prior to value comparison.
1120 *
1121 * @retval 0 success
1122 * @retval ETIMEDOUT if the PMU's maximum transition delay is reached before
1123 * the clock status matches @p value and @p mask.
1124 */
1125 int
1126 bhnd_core_clkctl_wait(struct bhnd_core_clkctl *clkctl, uint32_t value,
1127 uint32_t mask)
1128 {
1129 uint32_t clkst;
1130
1131 BHND_CLKCTL_LOCK_ASSERT(clkctl, MA_OWNED);
1132
1133 /* Bitswapped HTAVAIL/ALPAVAIL work-around */
1134 if (clkctl->cc_quirks & BHND_CLKCTL_QUIRK_CCS0) {
1135 uint32_t fmask, fval;
1136
1137 fmask = mask & ~(BHND_CCS_HTAVAIL | BHND_CCS_ALPAVAIL);
1138 fval = value & ~(BHND_CCS_HTAVAIL | BHND_CCS_ALPAVAIL);
1139
1140 if (mask & BHND_CCS_HTAVAIL)
1141 fmask |= BHND_CCS0_HTAVAIL;
1142 if (value & BHND_CCS_HTAVAIL)
1143 fval |= BHND_CCS0_HTAVAIL;
1144
1145 if (mask & BHND_CCS_ALPAVAIL)
1146 fmask |= BHND_CCS0_ALPAVAIL;
1147 if (value & BHND_CCS_ALPAVAIL)
1148 fval |= BHND_CCS0_ALPAVAIL;
1149
1150 mask = fmask;
1151 value = fval;
1152 }
1153
1154 for (u_int i = 0; i < clkctl->cc_max_latency; i += 10) {
1155 clkst = bhnd_bus_read_4(clkctl->cc_res, clkctl->cc_res_offset);
1156 if ((clkst & mask) == (value & mask))
1157 return (0);
1158
1159 DELAY(10);
1160 }
1161
1162 device_printf(clkctl->cc_dev, "clkst wait timeout (value=%#x, "
1163 "mask=%#x)\n", value, mask);
1164
1165 return (ETIMEDOUT);
1166 }
1167
1168 /**
1169 * Read an NVRAM variable's NUL-terminated string value.
1170 *
1171 * @param dev A bhnd bus child device.
1172 * @param name The NVRAM variable name.
1173 * @param[out] buf A buffer large enough to hold @p len bytes. On
1174 * success, the NUL-terminated string value will be
1175 * written to this buffer. This argment may be NULL if
1176 * the value is not desired.
1177 * @param len The maximum capacity of @p buf.
1178 * @param[out] rlen On success, will be set to the actual size of
1179 * the requested value (including NUL termination). This
1180 * argment may be NULL if the size is not desired.
1181 *
1182 * @retval 0 success
1183 * @retval ENOENT The requested variable was not found.
1184 * @retval ENODEV No valid NVRAM source could be found.
1185 * @retval ENOMEM If @p buf is non-NULL and a buffer of @p len is too
1186 * small to hold the requested value.
1187 * @retval EFTYPE If the variable data cannot be coerced to a valid
1188 * string representation.
1189 * @retval ERANGE If value coercion would overflow @p type.
1190 * @retval non-zero If reading @p name otherwise fails, a regular unix
1191 * error code will be returned.
1192 */
1193 int
1194 bhnd_nvram_getvar_str(device_t dev, const char *name, char *buf, size_t len,
1195 size_t *rlen)
1196 {
1197 size_t larg;
1198 int error;
1199
1200 larg = len;
1201 error = bhnd_nvram_getvar(dev, name, buf, &larg,
1202 BHND_NVRAM_TYPE_STRING);
1203 if (rlen != NULL)
1204 *rlen = larg;
1205
1206 return (error);
1207 }
1208
1209 /**
1210 * Read an NVRAM variable's unsigned integer value.
1211 *
1212 * @param dev A bhnd bus child device.
1213 * @param name The NVRAM variable name.
1214 * @param[out] value On success, the requested value will be written
1215 * to this pointer.
1216 * @param width The output integer type width (1, 2, or
1217 * 4 bytes).
1218 *
1219 * @retval 0 success
1220 * @retval ENOENT The requested variable was not found.
1221 * @retval ENODEV No valid NVRAM source could be found.
1222 * @retval EFTYPE If the variable data cannot be coerced to a
1223 * a valid unsigned integer representation.
1224 * @retval ERANGE If value coercion would overflow (or underflow) an
1225 * unsigned representation of the given @p width.
1226 * @retval non-zero If reading @p name otherwise fails, a regular unix
1227 * error code will be returned.
1228 */
1229 int
1230 bhnd_nvram_getvar_uint(device_t dev, const char *name, void *value, int width)
1231 {
1232 bhnd_nvram_type type;
1233 size_t len;
1234
1235 switch (width) {
1236 case 1:
1237 type = BHND_NVRAM_TYPE_UINT8;
1238 break;
1239 case 2:
1240 type = BHND_NVRAM_TYPE_UINT16;
1241 break;
1242 case 4:
1243 type = BHND_NVRAM_TYPE_UINT32;
1244 break;
1245 default:
1246 device_printf(dev, "unsupported NVRAM integer width: %d\n",
1247 width);
1248 return (EINVAL);
1249 }
1250
1251 len = width;
1252 return (bhnd_nvram_getvar(dev, name, value, &len, type));
1253 }
1254
1255 /**
1256 * Read an NVRAM variable's unsigned 8-bit integer value.
1257 *
1258 * @param dev A bhnd bus child device.
1259 * @param name The NVRAM variable name.
1260 * @param[out] value On success, the requested value will be written
1261 * to this pointer.
1262 *
1263 * @retval 0 success
1264 * @retval ENOENT The requested variable was not found.
1265 * @retval ENODEV No valid NVRAM source could be found.
1266 * @retval EFTYPE If the variable data cannot be coerced to a
1267 * a valid unsigned integer representation.
1268 * @retval ERANGE If value coercion would overflow (or underflow) uint8_t.
1269 * @retval non-zero If reading @p name otherwise fails, a regular unix
1270 * error code will be returned.
1271 */
1272 int
1273 bhnd_nvram_getvar_uint8(device_t dev, const char *name, uint8_t *value)
1274 {
1275 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1276 }
1277
1278 /**
1279 * Read an NVRAM variable's unsigned 16-bit integer value.
1280 *
1281 * @param dev A bhnd bus child device.
1282 * @param name The NVRAM variable name.
1283 * @param[out] value On success, the requested value will be written
1284 * to this pointer.
1285 *
1286 * @retval 0 success
1287 * @retval ENOENT The requested variable was not found.
1288 * @retval ENODEV No valid NVRAM source could be found.
1289 * @retval EFTYPE If the variable data cannot be coerced to a
1290 * a valid unsigned integer representation.
1291 * @retval ERANGE If value coercion would overflow (or underflow)
1292 * uint16_t.
1293 * @retval non-zero If reading @p name otherwise fails, a regular unix
1294 * error code will be returned.
1295 */
1296 int
1297 bhnd_nvram_getvar_uint16(device_t dev, const char *name, uint16_t *value)
1298 {
1299 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1300 }
1301
1302 /**
1303 * Read an NVRAM variable's unsigned 32-bit integer value.
1304 *
1305 * @param dev A bhnd bus child device.
1306 * @param name The NVRAM variable name.
1307 * @param[out] value On success, the requested value will be written
1308 * to this pointer.
1309 *
1310 * @retval 0 success
1311 * @retval ENOENT The requested variable was not found.
1312 * @retval ENODEV No valid NVRAM source could be found.
1313 * @retval EFTYPE If the variable data cannot be coerced to a
1314 * a valid unsigned integer representation.
1315 * @retval ERANGE If value coercion would overflow (or underflow)
1316 * uint32_t.
1317 * @retval non-zero If reading @p name otherwise fails, a regular unix
1318 * error code will be returned.
1319 */
1320 int
1321 bhnd_nvram_getvar_uint32(device_t dev, const char *name, uint32_t *value)
1322 {
1323 return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));
1324 }
1325
1326 /**
1327 * Read an NVRAM variable's signed integer value.
1328 *
1329 * @param dev A bhnd bus child device.
1330 * @param name The NVRAM variable name.
1331 * @param[out] value On success, the requested value will be written
1332 * to this pointer.
1333 * @param width The output integer type width (1, 2, or
1334 * 4 bytes).
1335 *
1336 * @retval 0 success
1337 * @retval ENOENT The requested variable was not found.
1338 * @retval ENODEV No valid NVRAM source could be found.
1339 * @retval EFTYPE If the variable data cannot be coerced to a
1340 * a valid integer representation.
1341 * @retval ERANGE If value coercion would overflow (or underflow) an
1342 * signed representation of the given @p width.
1343 * @retval non-zero If reading @p name otherwise fails, a regular unix
1344 * error code will be returned.
1345 */
1346 int
1347 bhnd_nvram_getvar_int(device_t dev, const char *name, void *value, int width)
1348 {
1349 bhnd_nvram_type type;
1350 size_t len;
1351
1352 switch (width) {
1353 case 1:
1354 type = BHND_NVRAM_TYPE_INT8;
1355 break;
1356 case 2:
1357 type = BHND_NVRAM_TYPE_INT16;
1358 break;
1359 case 4:
1360 type = BHND_NVRAM_TYPE_INT32;
1361 break;
1362 default:
1363 device_printf(dev, "unsupported NVRAM integer width: %d\n",
1364 width);
1365 return (EINVAL);
1366 }
1367
1368 len = width;
1369 return (bhnd_nvram_getvar(dev, name, value, &len, type));
1370 }
1371
1372 /**
1373 * Read an NVRAM variable's signed 8-bit integer value.
1374 *
1375 * @param dev A bhnd bus child device.
1376 * @param name The NVRAM variable name.
1377 * @param[out] value On success, the requested value will be written
1378 * to this pointer.
1379 *
1380 * @retval 0 success
1381 * @retval ENOENT The requested variable was not found.
1382 * @retval ENODEV No valid NVRAM source could be found.
1383 * @retval EFTYPE If the variable data cannot be coerced to a
1384 * a valid integer representation.
1385 * @retval ERANGE If value coercion would overflow (or underflow) int8_t.
1386 * @retval non-zero If reading @p name otherwise fails, a regular unix
1387 * error code will be returned.
1388 */
1389 int
1390 bhnd_nvram_getvar_int8(device_t dev, const char *name, int8_t *value)
1391 {
1392 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1393 }
1394
1395 /**
1396 * Read an NVRAM variable's signed 16-bit integer value.
1397 *
1398 * @param dev A bhnd bus child device.
1399 * @param name The NVRAM variable name.
1400 * @param[out] value On success, the requested value will be written
1401 * to this pointer.
1402 *
1403 * @retval 0 success
1404 * @retval ENOENT The requested variable was not found.
1405 * @retval ENODEV No valid NVRAM source could be found.
1406 * @retval EFTYPE If the variable data cannot be coerced to a
1407 * a valid integer representation.
1408 * @retval ERANGE If value coercion would overflow (or underflow)
1409 * int16_t.
1410 * @retval non-zero If reading @p name otherwise fails, a regular unix
1411 * error code will be returned.
1412 */
1413 int
1414 bhnd_nvram_getvar_int16(device_t dev, const char *name, int16_t *value)
1415 {
1416 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1417 }
1418
1419 /**
1420 * Read an NVRAM variable's signed 32-bit integer value.
1421 *
1422 * @param dev A bhnd bus child device.
1423 * @param name The NVRAM variable name.
1424 * @param[out] value On success, the requested value will be written
1425 * to this pointer.
1426 *
1427 * @retval 0 success
1428 * @retval ENOENT The requested variable was not found.
1429 * @retval ENODEV No valid NVRAM source could be found.
1430 * @retval EFTYPE If the variable data cannot be coerced to a
1431 * a valid integer representation.
1432 * @retval ERANGE If value coercion would overflow (or underflow)
1433 * int32_t.
1434 * @retval non-zero If reading @p name otherwise fails, a regular unix
1435 * error code will be returned.
1436 */
1437 int
1438 bhnd_nvram_getvar_int32(device_t dev, const char *name, int32_t *value)
1439 {
1440 return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));
1441 }
1442
1443 /**
1444 * Read an NVRAM variable's array value.
1445 *
1446 * @param dev A bhnd bus child device.
1447 * @param name The NVRAM variable name.
1448 * @param[out] buf A buffer large enough to hold @p size bytes.
1449 * On success, the requested value will be written
1450 * to this buffer.
1451 * @param[in,out] size The required number of bytes to write to
1452 * @p buf.
1453 * @param type The desired array element data representation.
1454 *
1455 * @retval 0 success
1456 * @retval ENOENT The requested variable was not found.
1457 * @retval ENODEV No valid NVRAM source could be found.
1458 * @retval ENXIO If less than @p size bytes are available.
1459 * @retval ENOMEM If a buffer of @p size is too small to hold the
1460 * requested value.
1461 * @retval EFTYPE If the variable data cannot be coerced to a
1462 * a valid instance of @p type.
1463 * @retval ERANGE If value coercion would overflow (or underflow) a
1464 * representation of @p type.
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_array(device_t dev, const char *name, void *buf, size_t size,
1470 bhnd_nvram_type type)
1471 {
1472 size_t nbytes;
1473 int error;
1474
1475 /* Attempt read */
1476 nbytes = size;
1477 if ((error = bhnd_nvram_getvar(dev, name, buf, &nbytes, type)))
1478 return (error);
1479
1480 /* Verify that the expected number of bytes were fetched */
1481 if (nbytes < size)
1482 return (ENXIO);
1483
1484 return (0);
1485 }
1486
1487 /**
1488 * Initialize a service provider registry.
1489 *
1490 * @param bsr The service registry to initialize.
1491 *
1492 * @retval 0 success
1493 * @retval non-zero if an error occurs initializing the service registry,
1494 * a regular unix error code will be returned.
1495
1496 */
1497 int
1498 bhnd_service_registry_init(struct bhnd_service_registry *bsr)
1499 {
1500 STAILQ_INIT(&bsr->entries);
1501 mtx_init(&bsr->lock, "bhnd_service_registry lock", NULL, MTX_DEF);
1502
1503 return (0);
1504 }
1505
1506 /**
1507 * Release all resources held by @p bsr.
1508 *
1509 * @param bsr A service registry instance previously successfully
1510 * initialized via bhnd_service_registry_init().
1511 *
1512 * @retval 0 success
1513 * @retval EBUSY if active references to service providers registered
1514 * with @p bsr exist.
1515 */
1516 int
1517 bhnd_service_registry_fini(struct bhnd_service_registry *bsr)
1518 {
1519 struct bhnd_service_entry *entry, *enext;
1520
1521 /* Remove everthing we can */
1522 mtx_lock(&bsr->lock);
1523 STAILQ_FOREACH_SAFE(entry, &bsr->entries, link, enext) {
1524 if (entry->refs > 0)
1525 continue;
1526
1527 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry, link);
1528 free(entry, M_BHND);
1529 }
1530
1531 if (!STAILQ_EMPTY(&bsr->entries)) {
1532 mtx_unlock(&bsr->lock);
1533 return (EBUSY);
1534 }
1535 mtx_unlock(&bsr->lock);
1536
1537 mtx_destroy(&bsr->lock);
1538 return (0);
1539 }
1540
1541 /**
1542 * Register a @p provider for the given @p service.
1543 *
1544 * @param bsr Service registry to be modified.
1545 * @param provider Service provider to register.
1546 * @param service Service for which @p provider will be registered.
1547 * @param flags Service provider flags (see BHND_SPF_*).
1548 *
1549 * @retval 0 success
1550 * @retval EEXIST if an entry for @p service already exists.
1551 * @retval EINVAL if @p service is BHND_SERVICE_ANY.
1552 * @retval non-zero if registering @p provider otherwise fails, a regular
1553 * unix error code will be returned.
1554 */
1555 int
1556 bhnd_service_registry_add(struct bhnd_service_registry *bsr, device_t provider,
1557 bhnd_service_t service, uint32_t flags)
1558 {
1559 struct bhnd_service_entry *entry;
1560
1561 if (service == BHND_SERVICE_ANY)
1562 return (EINVAL);
1563
1564 mtx_lock(&bsr->lock);
1565
1566 /* Is a service provider already registered? */
1567 STAILQ_FOREACH(entry, &bsr->entries, link) {
1568 if (entry->service == service) {
1569 mtx_unlock(&bsr->lock);
1570 return (EEXIST);
1571 }
1572 }
1573
1574 /* Initialize and insert our new entry */
1575 entry = malloc(sizeof(*entry), M_BHND, M_NOWAIT);
1576 if (entry == NULL) {
1577 mtx_unlock(&bsr->lock);
1578 return (ENOMEM);
1579 }
1580
1581 entry->provider = provider;
1582 entry->service = service;
1583 entry->flags = flags;
1584 refcount_init(&entry->refs, 0);
1585
1586 STAILQ_INSERT_HEAD(&bsr->entries, entry, link);
1587
1588 mtx_unlock(&bsr->lock);
1589 return (0);
1590 }
1591
1592 /**
1593 * Free an unreferenced registry entry.
1594 *
1595 * @param entry The entry to be deallocated.
1596 */
1597 static void
1598 bhnd_service_registry_free_entry(struct bhnd_service_entry *entry)
1599 {
1600 KASSERT(entry->refs == 0, ("provider has active references"));
1601 free(entry, M_BHND);
1602 }
1603
1604 /**
1605 * Attempt to remove the @p service provider registration for @p provider.
1606 *
1607 * @param bsr The service registry to be modified.
1608 * @param provider The service provider to be deregistered.
1609 * @param service The service for which @p provider will be deregistered,
1610 * or BHND_SERVICE_ANY to remove all service
1611 * registrations for @p provider.
1612 *
1613 * @retval 0 success
1614 * @retval EBUSY if active references to @p provider exist; see
1615 * bhnd_service_registry_retain() and
1616 * bhnd_service_registry_release().
1617 */
1618 int
1619 bhnd_service_registry_remove(struct bhnd_service_registry *bsr,
1620 device_t provider, bhnd_service_t service)
1621 {
1622 struct bhnd_service_entry *entry, *enext;
1623
1624 mtx_lock(&bsr->lock);
1625
1626 #define BHND_PROV_MATCH(_e) \
1627 ((_e)->provider == provider && \
1628 (service == BHND_SERVICE_ANY || (_e)->service == service))
1629
1630 /* Validate matching provider entries before making any
1631 * modifications */
1632 STAILQ_FOREACH(entry, &bsr->entries, link) {
1633 /* Skip non-matching entries */
1634 if (!BHND_PROV_MATCH(entry))
1635 continue;
1636
1637 /* Entry is in use? */
1638 if (entry->refs > 0) {
1639 mtx_unlock(&bsr->lock);
1640 return (EBUSY);
1641 }
1642 }
1643
1644 /* We can now safely remove matching entries */
1645 STAILQ_FOREACH_SAFE(entry, &bsr->entries, link, enext) {
1646 /* Skip non-matching entries */
1647 if (!BHND_PROV_MATCH(entry))
1648 continue;
1649
1650 /* Remove from list */
1651 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry, link);
1652
1653 /* Free provider entry */
1654 bhnd_service_registry_free_entry(entry);
1655 }
1656 #undef BHND_PROV_MATCH
1657
1658 mtx_unlock(&bsr->lock);
1659 return (0);
1660 }
1661
1662 /**
1663 * Retain and return a reference to a registered @p service provider, if any.
1664 *
1665 * @param bsr The service registry to be queried.
1666 * @param service The service for which a provider should be returned.
1667 *
1668 * On success, the caller assumes ownership the returned provider, and
1669 * is responsible for releasing this reference via
1670 * bhnd_service_registry_release().
1671 *
1672 * @retval device_t success
1673 * @retval NULL if no provider is registered for @p service.
1674 */
1675 device_t
1676 bhnd_service_registry_retain(struct bhnd_service_registry *bsr,
1677 bhnd_service_t service)
1678 {
1679 struct bhnd_service_entry *entry;
1680
1681 mtx_lock(&bsr->lock);
1682 STAILQ_FOREACH(entry, &bsr->entries, link) {
1683 if (entry->service != service)
1684 continue;
1685
1686 /* With a live refcount, entry is gauranteed to remain alive
1687 * after we release our lock */
1688 refcount_acquire(&entry->refs);
1689
1690 mtx_unlock(&bsr->lock);
1691 return (entry->provider);
1692 }
1693 mtx_unlock(&bsr->lock);
1694
1695 /* Not found */
1696 return (NULL);
1697 }
1698
1699 /**
1700 * Release a reference to a service provider previously returned by
1701 * bhnd_service_registry_retain().
1702 *
1703 * If this is the last reference to an inherited service provider registration
1704 * (see BHND_SPF_INHERITED), the registration will also be removed, and
1705 * true will be returned.
1706 *
1707 * @param bsr The service registry from which @p provider
1708 * was returned.
1709 * @param provider The provider to be released.
1710 * @param service The service for which @p provider was previously
1711 * retained.
1712 * @retval true The inherited service provider registration was removed;
1713 * the caller should release its own reference to the
1714 * provider.
1715 * @retval false The service provider was not inherited, or active
1716 * references to the provider remain.
1717 *
1718 * @see BHND_SPF_INHERITED
1719 */
1720 bool
1721 bhnd_service_registry_release(struct bhnd_service_registry *bsr,
1722 device_t provider, bhnd_service_t service)
1723 {
1724 struct bhnd_service_entry *entry;
1725
1726 /* Exclusive lock, as we need to prevent any new references to the
1727 * entry from being taken if it's to be removed */
1728 mtx_lock(&bsr->lock);
1729 STAILQ_FOREACH(entry, &bsr->entries, link) {
1730 bool removed;
1731
1732 if (entry->provider != provider)
1733 continue;
1734
1735 if (entry->service != service)
1736 continue;
1737
1738 if (refcount_release(&entry->refs) &&
1739 (entry->flags & BHND_SPF_INHERITED))
1740 {
1741 /* If an inherited entry is no longer actively
1742 * referenced, remove the local registration and inform
1743 * the caller. */
1744 STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry,
1745 link);
1746 bhnd_service_registry_free_entry(entry);
1747 removed = true;
1748 } else {
1749 removed = false;
1750 }
1751
1752 mtx_unlock(&bsr->lock);
1753 return (removed);
1754 }
1755
1756 /* Caller owns a reference, but no such provider is registered? */
1757 panic("invalid service provider reference");
1758 }
1759
1760 /**
1761 * Using the bhnd(4) bus-level core information and a custom core name,
1762 * populate @p dev's device description.
1763 *
1764 * @param dev A bhnd-bus attached device.
1765 * @param dev_name The core's name (e.g. "SDIO Device Core").
1766 */
1767 void
1768 bhnd_set_custom_core_desc(device_t dev, const char *dev_name)
1769 {
1770 const char *vendor_name;
1771 char *desc;
1772
1773 vendor_name = bhnd_get_vendor_name(dev);
1774 asprintf(&desc, M_BHND, "%s %s, rev %hhu", vendor_name, dev_name,
1775 bhnd_get_hwrev(dev));
1776
1777 if (desc != NULL) {
1778 device_set_desc_copy(dev, desc);
1779 free(desc, M_BHND);
1780 } else {
1781 device_set_desc(dev, dev_name);
1782 }
1783 }
1784
1785 /**
1786 * Using the bhnd(4) bus-level core information, populate @p dev's device
1787 * description.
1788 *
1789 * @param dev A bhnd-bus attached device.
1790 */
1791 void
1792 bhnd_set_default_core_desc(device_t dev)
1793 {
1794 bhnd_set_custom_core_desc(dev, bhnd_get_device_name(dev));
1795 }
1796
1797 /**
1798 * Using the bhnd @p chip_id, populate the bhnd(4) bus @p dev's device
1799 * description.
1800 *
1801 * @param dev A bhnd-bus attached device.
1802 * @param chip_id The chip identification.
1803 */
1804 void
1805 bhnd_set_default_bus_desc(device_t dev, const struct bhnd_chipid *chip_id)
1806 {
1807 const char *bus_name;
1808 char *desc;
1809 char chip_name[BHND_CHIPID_MAX_NAMELEN];
1810
1811 /* Determine chip type's bus name */
1812 switch (chip_id->chip_type) {
1813 case BHND_CHIPTYPE_SIBA:
1814 bus_name = "SIBA bus";
1815 break;
1816 case BHND_CHIPTYPE_BCMA:
1817 case BHND_CHIPTYPE_BCMA_ALT:
1818 bus_name = "BCMA bus";
1819 break;
1820 case BHND_CHIPTYPE_UBUS:
1821 bus_name = "UBUS bus";
1822 break;
1823 default:
1824 bus_name = "Unknown Type";
1825 break;
1826 }
1827
1828 /* Format chip name */
1829 bhnd_format_chip_id(chip_name, sizeof(chip_name),
1830 chip_id->chip_id);
1831
1832 /* Format and set device description */
1833 asprintf(&desc, M_BHND, "%s %s", chip_name, bus_name);
1834 if (desc != NULL) {
1835 device_set_desc_copy(dev, desc);
1836 free(desc, M_BHND);
1837 } else {
1838 device_set_desc(dev, bus_name);
1839 }
1840
1841 }
1842
1843 /**
1844 * Helper function for implementing BHND_BUS_REGISTER_PROVIDER().
1845 *
1846 * This implementation delegates the request to the BHND_BUS_REGISTER_PROVIDER()
1847 * method on the parent of @p dev. If no parent exists, the implementation
1848 * will return an error.
1849 */
1850 int
1851 bhnd_bus_generic_register_provider(device_t dev, device_t child,
1852 device_t provider, bhnd_service_t service)
1853 {
1854 device_t parent = device_get_parent(dev);
1855
1856 if (parent != NULL) {
1857 return (BHND_BUS_REGISTER_PROVIDER(parent, child,
1858 provider, service));
1859 }
1860
1861 return (ENXIO);
1862 }
1863
1864 /**
1865 * Helper function for implementing BHND_BUS_DEREGISTER_PROVIDER().
1866 *
1867 * This implementation delegates the request to the
1868 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
1869 * exists, the implementation will panic.
1870 */
1871 int
1872 bhnd_bus_generic_deregister_provider(device_t dev, device_t child,
1873 device_t provider, bhnd_service_t service)
1874 {
1875 device_t parent = device_get_parent(dev);
1876
1877 if (parent != NULL) {
1878 return (BHND_BUS_DEREGISTER_PROVIDER(parent, child,
1879 provider, service));
1880 }
1881
1882 panic("missing BHND_BUS_DEREGISTER_PROVIDER()");
1883 }
1884
1885 /**
1886 * Helper function for implementing BHND_BUS_RETAIN_PROVIDER().
1887 *
1888 * This implementation delegates the request to the
1889 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
1890 * exists, the implementation will return NULL.
1891 */
1892 device_t
1893 bhnd_bus_generic_retain_provider(device_t dev, device_t child,
1894 bhnd_service_t service)
1895 {
1896 device_t parent = device_get_parent(dev);
1897
1898 if (parent != NULL) {
1899 return (BHND_BUS_RETAIN_PROVIDER(parent, child,
1900 service));
1901 }
1902
1903 return (NULL);
1904 }
1905
1906 /**
1907 * Helper function for implementing BHND_BUS_RELEASE_PROVIDER().
1908 *
1909 * This implementation delegates the request to the
1910 * BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent
1911 * exists, the implementation will panic.
1912 */
1913 void
1914 bhnd_bus_generic_release_provider(device_t dev, device_t child,
1915 device_t provider, bhnd_service_t service)
1916 {
1917 device_t parent = device_get_parent(dev);
1918
1919 if (parent != NULL) {
1920 return (BHND_BUS_RELEASE_PROVIDER(parent, child,
1921 provider, service));
1922 }
1923
1924 panic("missing BHND_BUS_RELEASE_PROVIDER()");
1925 }
1926
1927 /**
1928 * Helper function for implementing BHND_BUS_REGISTER_PROVIDER().
1929 *
1930 * This implementation uses the bhnd_service_registry_add() function to
1931 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
1932 * a suitable service registry to edit.
1933 */
1934 int
1935 bhnd_bus_generic_sr_register_provider(device_t dev, device_t child,
1936 device_t provider, bhnd_service_t service)
1937 {
1938 struct bhnd_service_registry *bsr;
1939
1940 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
1941
1942 KASSERT(bsr != NULL, ("NULL service registry"));
1943
1944 return (bhnd_service_registry_add(bsr, provider, service, 0));
1945 }
1946
1947 /**
1948 * Helper function for implementing BHND_BUS_DEREGISTER_PROVIDER().
1949 *
1950 * This implementation uses the bhnd_service_registry_remove() function to
1951 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
1952 * a suitable service registry to edit.
1953 */
1954 int
1955 bhnd_bus_generic_sr_deregister_provider(device_t dev, device_t child,
1956 device_t provider, bhnd_service_t service)
1957 {
1958 struct bhnd_service_registry *bsr;
1959
1960 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
1961
1962 KASSERT(bsr != NULL, ("NULL service registry"));
1963
1964 return (bhnd_service_registry_remove(bsr, provider, service));
1965 }
1966
1967 /**
1968 * Helper function for implementing BHND_BUS_RETAIN_PROVIDER().
1969 *
1970 * This implementation uses the bhnd_service_registry_retain() function to
1971 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
1972 * a suitable service registry.
1973 *
1974 * If a local provider for the service is not available, and a parent device is
1975 * available, this implementation will attempt to fetch and locally register
1976 * a service provider reference from the parent of @p dev.
1977 */
1978 device_t
1979 bhnd_bus_generic_sr_retain_provider(device_t dev, device_t child,
1980 bhnd_service_t service)
1981 {
1982 struct bhnd_service_registry *bsr;
1983 device_t parent, provider;
1984 int error;
1985
1986 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
1987 KASSERT(bsr != NULL, ("NULL service registry"));
1988
1989 /*
1990 * Attempt to fetch a service provider reference from either the local
1991 * service registry, or if not found, from our parent.
1992 *
1993 * If we fetch a provider from our parent, we register the provider
1994 * with the local service registry to prevent conflicting local
1995 * registrations from being added.
1996 */
1997 while (1) {
1998 /* Check the local service registry first */
1999 provider = bhnd_service_registry_retain(bsr, service);
2000 if (provider != NULL)
2001 return (provider);
2002
2003 /* Otherwise, try to delegate to our parent (if any) */
2004 if ((parent = device_get_parent(dev)) == NULL)
2005 return (NULL);
2006
2007 provider = BHND_BUS_RETAIN_PROVIDER(parent, dev, service);
2008 if (provider == NULL)
2009 return (NULL);
2010
2011 /* Register the inherited service registration with the local
2012 * registry */
2013 error = bhnd_service_registry_add(bsr, provider, service,
2014 BHND_SPF_INHERITED);
2015 if (error) {
2016 BHND_BUS_RELEASE_PROVIDER(parent, dev, provider,
2017 service);
2018 if (error == EEXIST) {
2019 /* A valid service provider was registered
2020 * concurrently; retry fetching from the local
2021 * registry */
2022 continue;
2023 }
2024
2025 device_printf(dev, "failed to register service "
2026 "provider: %d\n", error);
2027 return (NULL);
2028 }
2029 }
2030 }
2031
2032 /**
2033 * Helper function for implementing BHND_BUS_RELEASE_PROVIDER().
2034 *
2035 * This implementation uses the bhnd_service_registry_release() function to
2036 * do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find
2037 * a suitable service registry.
2038 */
2039 void
2040 bhnd_bus_generic_sr_release_provider(device_t dev, device_t child,
2041 device_t provider, bhnd_service_t service)
2042 {
2043 struct bhnd_service_registry *bsr;
2044
2045 bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);
2046 KASSERT(bsr != NULL, ("NULL service registry"));
2047
2048 /* Release the provider reference; if the refcount hits zero on an
2049 * inherited reference, true will be returned, and we need to drop
2050 * our own bus reference to the provider */
2051 if (!bhnd_service_registry_release(bsr, provider, service))
2052 return;
2053
2054 /* Drop our reference to the borrowed provider */
2055 BHND_BUS_RELEASE_PROVIDER(device_get_parent(dev), dev, provider,
2056 service);
2057 }
2058
2059 /**
2060 * Helper function for implementing BHND_BUS_IS_HW_DISABLED().
2061 *
2062 * If a parent device is available, this implementation delegates the
2063 * request to the BHND_BUS_IS_HW_DISABLED() method on the parent of @p dev.
2064 *
2065 * If no parent device is available (i.e. on a the bus root), the hardware
2066 * is assumed to be usable and false is returned.
2067 */
2068 bool
2069 bhnd_bus_generic_is_hw_disabled(device_t dev, device_t child)
2070 {
2071 if (device_get_parent(dev) != NULL)
2072 return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child));
2073
2074 return (false);
2075 }
2076
2077 /**
2078 * Helper function for implementing BHND_BUS_GET_CHIPID().
2079 *
2080 * This implementation delegates the request to the BHND_BUS_GET_CHIPID()
2081 * method on the parent of @p dev. If no parent exists, the implementation
2082 * will panic.
2083 */
2084 const struct bhnd_chipid *
2085 bhnd_bus_generic_get_chipid(device_t dev, device_t child)
2086 {
2087 if (device_get_parent(dev) != NULL)
2088 return (BHND_BUS_GET_CHIPID(device_get_parent(dev), child));
2089
2090 panic("missing BHND_BUS_GET_CHIPID()");
2091 }
2092
2093 /**
2094 * Helper function for implementing BHND_BUS_GET_DMA_TRANSLATION().
2095 *
2096 * If a parent device is available, this implementation delegates the
2097 * request to the BHND_BUS_GET_DMA_TRANSLATION() method on the parent of @p dev.
2098 *
2099 * If no parent device is available, this implementation will panic.
2100 */
2101 int
2102 bhnd_bus_generic_get_dma_translation(device_t dev, device_t child, u_int width,
2103 uint32_t flags, bus_dma_tag_t *dmat,
2104 struct bhnd_dma_translation *translation)
2105 {
2106 if (device_get_parent(dev) != NULL) {
2107 return (BHND_BUS_GET_DMA_TRANSLATION(device_get_parent(dev),
2108 child, width, flags, dmat, translation));
2109 }
2110
2111 panic("missing BHND_BUS_GET_DMA_TRANSLATION()");
2112 }
2113
2114 /* nvram board_info population macros for bhnd_bus_generic_read_board_info() */
2115 #define BHND_GV(_dest, _name) \
2116 bhnd_nvram_getvar_uint(child, BHND_NVAR_ ## _name, &_dest, \
2117 sizeof(_dest))
2118
2119 #define REQ_BHND_GV(_dest, _name) do { \
2120 if ((error = BHND_GV(_dest, _name))) { \
2121 device_printf(dev, \
2122 "error reading " __STRING(_name) ": %d\n", error); \
2123 return (error); \
2124 } \
2125 } while(0)
2126
2127 #define OPT_BHND_GV(_dest, _name, _default) do { \
2128 if ((error = BHND_GV(_dest, _name))) { \
2129 if (error != ENOENT) { \
2130 device_printf(dev, \
2131 "error reading " \
2132 __STRING(_name) ": %d\n", error); \
2133 return (error); \
2134 } \
2135 _dest = _default; \
2136 } \
2137 } while(0)
2138
2139 /**
2140 * Helper function for implementing BHND_BUS_READ_BOARDINFO().
2141 *
2142 * This implementation populates @p info with information from NVRAM,
2143 * defaulting board_vendor and board_type fields to 0 if the
2144 * requested variables cannot be found.
2145 *
2146 * This behavior is correct for most SoCs, but must be overridden on
2147 * bridged (PCI, PCMCIA, etc) devices to produce a complete bhnd_board_info
2148 * result.
2149 */
2150 int
2151 bhnd_bus_generic_read_board_info(device_t dev, device_t child,
2152 struct bhnd_board_info *info)
2153 {
2154 int error;
2155
2156 OPT_BHND_GV(info->board_vendor, BOARDVENDOR, 0);
2157 OPT_BHND_GV(info->board_type, BOARDTYPE, 0); /* srom >= 2 */
2158 OPT_BHND_GV(info->board_devid, DEVID, 0); /* srom >= 8 */
2159 REQ_BHND_GV(info->board_rev, BOARDREV);
2160 OPT_BHND_GV(info->board_srom_rev,SROMREV, 0); /* missing in
2161 some SoC
2162 NVRAM */
2163 REQ_BHND_GV(info->board_flags, BOARDFLAGS);
2164 OPT_BHND_GV(info->board_flags2, BOARDFLAGS2, 0); /* srom >= 4 */
2165 OPT_BHND_GV(info->board_flags3, BOARDFLAGS3, 0); /* srom >= 11 */
2166
2167 return (0);
2168 }
2169
2170 #undef BHND_GV
2171 #undef BHND_GV_REQ
2172 #undef BHND_GV_OPT
2173
2174 /**
2175 * Helper function for implementing BHND_BUS_GET_NVRAM_VAR().
2176 *
2177 * This implementation searches @p dev for a usable NVRAM child device.
2178 *
2179 * If no usable child device is found on @p dev, the request is delegated to
2180 * the BHND_BUS_GET_NVRAM_VAR() method on the parent of @p dev.
2181 */
2182 int
2183 bhnd_bus_generic_get_nvram_var(device_t dev, device_t child, const char *name,
2184 void *buf, size_t *size, bhnd_nvram_type type)
2185 {
2186 device_t nvram;
2187 device_t parent;
2188
2189 bus_topo_assert();
2190
2191 /* Look for a directly-attached NVRAM child */
2192 if ((nvram = device_find_child(dev, "bhnd_nvram", -1)) != NULL)
2193 return BHND_NVRAM_GETVAR(nvram, name, buf, size, type);
2194
2195 /* Try to delegate to parent */
2196 if ((parent = device_get_parent(dev)) == NULL)
2197 return (ENODEV);
2198
2199 return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), child,
2200 name, buf, size, type));
2201 }
2202
2203 /**
2204 * Helper function for implementing BHND_BUS_ALLOC_RESOURCE().
2205 *
2206 * This implementation of BHND_BUS_ALLOC_RESOURCE() delegates allocation
2207 * of the underlying resource to BUS_ALLOC_RESOURCE(), and activation
2208 * to @p dev's BHND_BUS_ACTIVATE_RESOURCE().
2209 */
2210 struct bhnd_resource *
2211 bhnd_bus_generic_alloc_resource(device_t dev, device_t child, int type,
2212 int *rid, rman_res_t start, rman_res_t end, rman_res_t count,
2213 u_int flags)
2214 {
2215 struct bhnd_resource *br;
2216 struct resource *res;
2217 int error;
2218
2219 br = NULL;
2220 res = NULL;
2221
2222 /* Allocate the real bus resource (without activating it) */
2223 res = BUS_ALLOC_RESOURCE(dev, child, type, rid, start, end, count,
2224 (flags & ~RF_ACTIVE));
2225 if (res == NULL)
2226 return (NULL);
2227
2228 /* Allocate our bhnd resource wrapper. */
2229 br = malloc(sizeof(struct bhnd_resource), M_BHND, M_NOWAIT);
2230 if (br == NULL)
2231 goto failed;
2232
2233 br->direct = false;
2234 br->res = res;
2235
2236 /* Attempt activation */
2237 if (flags & RF_ACTIVE) {
2238 error = BHND_BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, br);
2239 if (error)
2240 goto failed;
2241 }
2242
2243 return (br);
2244
2245 failed:
2246 if (res != NULL)
2247 BUS_RELEASE_RESOURCE(dev, child, type, *rid, res);
2248
2249 free(br, M_BHND);
2250 return (NULL);
2251 }
2252
2253 /**
2254 * Helper function for implementing BHND_BUS_RELEASE_RESOURCE().
2255 *
2256 * This implementation of BHND_BUS_RELEASE_RESOURCE() delegates release of
2257 * the backing resource to BUS_RELEASE_RESOURCE().
2258 */
2259 int
2260 bhnd_bus_generic_release_resource(device_t dev, device_t child, int type,
2261 int rid, struct bhnd_resource *r)
2262 {
2263 int error;
2264
2265 if ((error = BUS_RELEASE_RESOURCE(dev, child, type, rid, r->res)))
2266 return (error);
2267
2268 free(r, M_BHND);
2269 return (0);
2270 }
2271
2272 /**
2273 * Helper function for implementing BHND_BUS_ACTIVATE_RESOURCE().
2274 *
2275 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() first calls the
2276 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
2277 *
2278 * If this fails, and if @p dev is the direct parent of @p child, standard
2279 * resource activation is attempted via bus_activate_resource(). This enables
2280 * direct use of the bhnd(4) resource APIs on devices that may not be attached
2281 * to a parent bhnd bus or bridge.
2282 */
2283 int
2284 bhnd_bus_generic_activate_resource(device_t dev, device_t child, int type,
2285 int rid, struct bhnd_resource *r)
2286 {
2287 int error;
2288 bool passthrough;
2289
2290 passthrough = (device_get_parent(child) != dev);
2291
2292 /* Try to delegate to the parent */
2293 if (device_get_parent(dev) != NULL) {
2294 error = BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev),
2295 child, type, rid, r);
2296 } else {
2297 error = ENODEV;
2298 }
2299
2300 /* If bhnd(4) activation has failed and we're the child's direct
2301 * parent, try falling back on standard resource activation.
2302 */
2303 if (error && !passthrough) {
2304 error = bus_activate_resource(child, type, rid, r->res);
2305 if (!error)
2306 r->direct = true;
2307 }
2308
2309 return (error);
2310 }
2311
2312 /**
2313 * Helper function for implementing BHND_BUS_DEACTIVATE_RESOURCE().
2314 *
2315 * This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the
2316 * BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.
2317 */
2318 int
2319 bhnd_bus_generic_deactivate_resource(device_t dev, device_t child,
2320 int type, int rid, struct bhnd_resource *r)
2321 {
2322 if (device_get_parent(dev) != NULL)
2323 return (BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev),
2324 child, type, rid, r));
2325
2326 return (EINVAL);
2327 }
2328
2329 /**
2330 * Helper function for implementing BHND_BUS_GET_INTR_DOMAIN().
2331 *
2332 * This implementation simply returns the address of nearest bhnd(4) bus,
2333 * which may be @p dev; this behavior may be incompatible with FDT/OFW targets.
2334 */
2335 uintptr_t
2336 bhnd_bus_generic_get_intr_domain(device_t dev, device_t child, bool self)
2337 {
2338 return ((uintptr_t)dev);
2339 }
Cache object: 38e053c7fc31b6d0840220eef671ffc2
|