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