/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2015-2016 Landon Fuller <[email protected]>4* Copyright (c) 2017 The FreeBSD Foundation5* All rights reserved.6*7* Portions of this software were developed by Landon Fuller8* under sponsorship from the FreeBSD Foundation.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer,15* without modification.16* 2. Redistributions in binary form must reproduce at minimum a disclaimer17* similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any18* redistribution must be conditioned upon including a substantially19* similar Disclaimer requirement for further binary redistribution.20*21* NO WARRANTY22* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS23* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT24* LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY25* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL26* THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,27* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF28* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS29* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER30* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)31* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF32* THE POSSIBILITY OF SUCH DAMAGES.33*/3435#include <sys/param.h>36#include <sys/bus.h>37#include <sys/refcount.h>38#include <sys/systm.h>3940#include <machine/bus.h>41#include <sys/rman.h>42#include <machine/resource.h>4344#include <dev/bhnd/siba/sibareg.h>4546#include <dev/bhnd/cores/chipc/chipcreg.h>4748#include "nvram/bhnd_nvram.h"4950#include "bhnd_chipc_if.h"5152#include "bhnd_nvram_if.h"53#include "bhnd_nvram_map.h"5455#include "bhndreg.h"56#include "bhndvar.h"57#include "bhnd_private.h"5859static void bhnd_service_registry_free_entry(60struct bhnd_service_entry *entry);6162static int compare_ascending_probe_order(const void *lhs, const void *rhs);63static int compare_descending_probe_order(const void *lhs,64const void *rhs);6566/* BHND core device description table. */67static const struct bhnd_core_desc {68uint16_t vendor;69uint16_t device;70bhnd_devclass_t class;71const char *desc;72} bhnd_core_descs[] = {73#define BHND_CDESC(_mfg, _cid, _cls, _desc) \74{ BHND_MFGID_ ## _mfg, BHND_COREID_ ## _cid, \75BHND_DEVCLASS_ ## _cls, _desc }7677BHND_CDESC(BCM, CC, CC, "ChipCommon I/O Controller"),78BHND_CDESC(BCM, ILINE20, OTHER, "iLine20 HPNA"),79BHND_CDESC(BCM, SRAM, RAM, "SRAM"),80BHND_CDESC(BCM, SDRAM, RAM, "SDRAM"),81BHND_CDESC(BCM, PCI, PCI, "PCI Bridge"),82BHND_CDESC(BCM, MIPS, CPU, "BMIPS CPU"),83BHND_CDESC(BCM, ENET, ENET_MAC, "Fast Ethernet MAC"),84BHND_CDESC(BCM, V90_CODEC, SOFTMODEM, "V.90 SoftModem Codec"),85BHND_CDESC(BCM, USB, USB_DUAL, "USB 1.1 Device/Host Controller"),86BHND_CDESC(BCM, ADSL, OTHER, "ADSL Core"),87BHND_CDESC(BCM, ILINE100, OTHER, "iLine100 HPNA"),88BHND_CDESC(BCM, IPSEC, OTHER, "IPsec Accelerator"),89BHND_CDESC(BCM, UTOPIA, OTHER, "UTOPIA ATM Core"),90BHND_CDESC(BCM, PCMCIA, PCCARD, "PCMCIA Bridge"),91BHND_CDESC(BCM, SOCRAM, RAM, "Internal Memory"),92BHND_CDESC(BCM, MEMC, MEMC, "MEMC SDRAM Controller"),93BHND_CDESC(BCM, OFDM, OTHER, "OFDM PHY"),94BHND_CDESC(BCM, EXTIF, OTHER, "External Interface"),95BHND_CDESC(BCM, D11, WLAN, "802.11 MAC/PHY/Radio"),96BHND_CDESC(BCM, APHY, WLAN_PHY, "802.11a PHY"),97BHND_CDESC(BCM, BPHY, WLAN_PHY, "802.11b PHY"),98BHND_CDESC(BCM, GPHY, WLAN_PHY, "802.11g PHY"),99BHND_CDESC(BCM, MIPS33, CPU, "BMIPS33 CPU"),100BHND_CDESC(BCM, USB11H, USB_HOST, "USB 1.1 Host Controller"),101BHND_CDESC(BCM, USB11D, USB_DEV, "USB 1.1 Device Controller"),102BHND_CDESC(BCM, USB20H, USB_HOST, "USB 2.0 Host Controller"),103BHND_CDESC(BCM, USB20D, USB_DEV, "USB 2.0 Device Controller"),104BHND_CDESC(BCM, SDIOH, OTHER, "SDIO Host Controller"),105BHND_CDESC(BCM, ROBO, OTHER, "RoboSwitch"),106BHND_CDESC(BCM, ATA100, OTHER, "Parallel ATA Controller"),107BHND_CDESC(BCM, SATAXOR, OTHER, "SATA DMA/XOR Controller"),108BHND_CDESC(BCM, GIGETH, ENET_MAC, "Gigabit Ethernet MAC"),109BHND_CDESC(BCM, PCIE, PCIE, "PCIe Bridge"),110BHND_CDESC(BCM, NPHY, WLAN_PHY, "802.11n 2x2 PHY"),111BHND_CDESC(BCM, SRAMC, MEMC, "SRAM Controller"),112BHND_CDESC(BCM, MINIMAC, OTHER, "MINI MAC/PHY"),113BHND_CDESC(BCM, ARM11, CPU, "ARM1176 CPU"),114BHND_CDESC(BCM, ARM7S, CPU, "ARM7TDMI-S CPU"),115BHND_CDESC(BCM, LPPHY, WLAN_PHY, "802.11a/b/g PHY"),116BHND_CDESC(BCM, PMU, PMU, "PMU"),117BHND_CDESC(BCM, SSNPHY, WLAN_PHY, "802.11n Single-Stream PHY"),118BHND_CDESC(BCM, SDIOD, OTHER, "SDIO Device Core"),119BHND_CDESC(BCM, ARMCM3, CPU, "ARM Cortex-M3 CPU"),120BHND_CDESC(BCM, HTPHY, WLAN_PHY, "802.11n 4x4 PHY"),121BHND_CDESC(MIPS,MIPS74K, CPU, "MIPS74k CPU"),122BHND_CDESC(BCM, GMAC, ENET_MAC, "Gigabit MAC core"),123BHND_CDESC(BCM, DMEMC, MEMC, "DDR1/DDR2 Memory Controller"),124BHND_CDESC(BCM, PCIERC, OTHER, "PCIe Root Complex"),125BHND_CDESC(BCM, OCP, SOC_BRIDGE, "OCP to OCP Bridge"),126BHND_CDESC(BCM, SC, OTHER, "Shared Common Core"),127BHND_CDESC(BCM, AHB, SOC_BRIDGE, "OCP to AHB Bridge"),128BHND_CDESC(BCM, SPIH, OTHER, "SPI Host Controller"),129BHND_CDESC(BCM, I2S, OTHER, "I2S Digital Audio Interface"),130BHND_CDESC(BCM, DMEMS, MEMC, "SDR/DDR1 Memory Controller"),131BHND_CDESC(BCM, UBUS_SHIM, OTHER, "BCM6362/UBUS WLAN SHIM"),132BHND_CDESC(BCM, PCIE2, PCIE, "PCIe Bridge (Gen2)"),133134BHND_CDESC(ARM, APB_BRIDGE, SOC_BRIDGE, "BP135 AMBA3 AXI to APB Bridge"),135BHND_CDESC(ARM, PL301, SOC_ROUTER, "PL301 AMBA3 Interconnect"),136BHND_CDESC(ARM, EROM, EROM, "PL366 Device Enumeration ROM"),137BHND_CDESC(ARM, OOB_ROUTER, OTHER, "PL367 OOB Interrupt Router"),138BHND_CDESC(ARM, AXI_UNMAPPED, OTHER, "Unmapped Address Ranges"),139140BHND_CDESC(BCM, 4706_CC, CC, "ChipCommon I/O Controller"),141BHND_CDESC(BCM, NS_PCIE2, PCIE, "PCIe Bridge (Gen2)"),142BHND_CDESC(BCM, NS_DMA, OTHER, "DMA engine"),143BHND_CDESC(BCM, NS_SDIO, OTHER, "SDIO 3.0 Host Controller"),144BHND_CDESC(BCM, NS_USB20H, USB_HOST, "USB 2.0 Host Controller"),145BHND_CDESC(BCM, NS_USB30H, USB_HOST, "USB 3.0 Host Controller"),146BHND_CDESC(BCM, NS_A9JTAG, OTHER, "ARM Cortex A9 JTAG Interface"),147BHND_CDESC(BCM, NS_DDR23_MEMC, MEMC, "Denali DDR2/DD3 Memory Controller"),148BHND_CDESC(BCM, NS_ROM, NVRAM, "System ROM"),149BHND_CDESC(BCM, NS_NAND, NVRAM, "NAND Flash Controller"),150BHND_CDESC(BCM, NS_QSPI, NVRAM, "QSPI Flash Controller"),151BHND_CDESC(BCM, NS_CC_B, CC_B, "ChipCommon B Auxiliary I/O Controller"),152BHND_CDESC(BCM, 4706_SOCRAM, RAM, "Internal Memory"),153BHND_CDESC(BCM, IHOST_ARMCA9, CPU, "ARM Cortex A9 CPU"),154BHND_CDESC(BCM, 4706_GMAC_CMN, ENET, "Gigabit MAC (Common)"),155BHND_CDESC(BCM, 4706_GMAC, ENET_MAC, "Gigabit MAC"),156BHND_CDESC(BCM, AMEMC, MEMC, "Denali DDR1/DDR2 Memory Controller"),157#undef BHND_CDESC158159/* Derived from inspection of the BCM4331 cores that provide PrimeCell160* IDs. Due to lack of documentation, the surmised device name/purpose161* provided here may be incorrect. */162{ BHND_MFGID_ARM, BHND_PRIMEID_EROM, BHND_DEVCLASS_OTHER,163"PL364 Device Enumeration ROM" },164{ BHND_MFGID_ARM, BHND_PRIMEID_SWRAP, BHND_DEVCLASS_OTHER,165"PL368 Device Management Interface" },166{ BHND_MFGID_ARM, BHND_PRIMEID_MWRAP, BHND_DEVCLASS_OTHER,167"PL369 Device Management Interface" },168{ 0, 0, 0, NULL }169};170171static const struct bhnd_device_quirk bhnd_chipc_clkctl_quirks[];172static const struct bhnd_device_quirk bhnd_pcmcia_clkctl_quirks[];173174/**175* Device table entries for core-specific CLKCTL quirk lookup.176*/177static const struct bhnd_device bhnd_clkctl_devices[] = {178BHND_DEVICE(BCM, CC, NULL, bhnd_chipc_clkctl_quirks),179BHND_DEVICE(BCM, PCMCIA, NULL, bhnd_pcmcia_clkctl_quirks),180BHND_DEVICE_END,181};182183/** ChipCommon CLKCTL quirks */184static const struct bhnd_device_quirk bhnd_chipc_clkctl_quirks[] = {185/* HTAVAIL/ALPAVAIL are bitswapped in chipc's CLKCTL */186BHND_CHIP_QUIRK(4328, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),187BHND_CHIP_QUIRK(5354, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),188BHND_DEVICE_QUIRK_END189};190191/** PCMCIA CLKCTL quirks */192static const struct bhnd_device_quirk bhnd_pcmcia_clkctl_quirks[] = {193/* HTAVAIL/ALPAVAIL are bitswapped in pcmcia's CLKCTL */194BHND_CHIP_QUIRK(4328, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),195BHND_CHIP_QUIRK(5354, HWREV_ANY, BHND_CLKCTL_QUIRK_CCS0),196BHND_DEVICE_QUIRK_END197};198199/**200* Return the name for a given JEP106 manufacturer ID.201*202* @param vendor A JEP106 Manufacturer ID, including the non-standard ARM 4-bit203* JEP106 continuation code.204*/205const char *206bhnd_vendor_name(uint16_t vendor)207{208switch (vendor) {209case BHND_MFGID_ARM:210return "ARM";211case BHND_MFGID_BCM:212return "Broadcom";213case BHND_MFGID_MIPS:214return "MIPS";215default:216return "unknown";217}218}219220/**221* Return the name of a port type.222*223* @param port_type The port type to look up.224*/225const char *226bhnd_port_type_name(bhnd_port_type port_type)227{228switch (port_type) {229case BHND_PORT_DEVICE:230return ("device");231case BHND_PORT_BRIDGE:232return ("bridge");233case BHND_PORT_AGENT:234return ("agent");235default:236return "unknown";237}238}239240/**241* Return the name of an NVRAM source.242*243* @param nvram_src The NVRAM source type to look up.244*/245const char *246bhnd_nvram_src_name(bhnd_nvram_src nvram_src)247{248switch (nvram_src) {249case BHND_NVRAM_SRC_FLASH:250return ("flash");251case BHND_NVRAM_SRC_OTP:252return ("OTP");253case BHND_NVRAM_SRC_SPROM:254return ("SPROM");255case BHND_NVRAM_SRC_UNKNOWN:256return ("none");257default:258return ("unknown");259}260}261262static const struct bhnd_core_desc *263bhnd_find_core_desc(uint16_t vendor, uint16_t device)264{265for (u_int i = 0; bhnd_core_descs[i].desc != NULL; i++) {266if (bhnd_core_descs[i].vendor != vendor)267continue;268269if (bhnd_core_descs[i].device != device)270continue;271272return (&bhnd_core_descs[i]);273}274275return (NULL);276}277278/**279* Return a human-readable name for a BHND core.280*281* @param vendor The core designer's JEDEC-106 Manufacturer ID.282* @param device The core identifier.283*/284const char *285bhnd_find_core_name(uint16_t vendor, uint16_t device)286{287const struct bhnd_core_desc *desc;288289if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)290return ("unknown");291292return desc->desc;293}294295/**296* Return the device class for a BHND core.297*298* @param vendor The core designer's JEDEC-106 Manufacturer ID.299* @param device The core identifier.300*/301bhnd_devclass_t302bhnd_find_core_class(uint16_t vendor, uint16_t device)303{304const struct bhnd_core_desc *desc;305306if ((desc = bhnd_find_core_desc(vendor, device)) == NULL)307return (BHND_DEVCLASS_OTHER);308309return desc->class;310}311312/**313* Return a human-readable name for a BHND core.314*315* @param ci The core's info record.316*/317const char *318bhnd_core_name(const struct bhnd_core_info *ci)319{320return bhnd_find_core_name(ci->vendor, ci->device);321}322323/**324* Return the device class for a BHND core.325*326* @param ci The core's info record.327*/328bhnd_devclass_t329bhnd_core_class(const struct bhnd_core_info *ci)330{331return bhnd_find_core_class(ci->vendor, ci->device);332}333334/**335* Write a human readable name representation of the given336* BHND_CHIPID_* constant to @p buffer.337*338* @param buffer Output buffer, or NULL to compute the required size.339* @param size Capacity of @p buffer, in bytes.340* @param chip_id Chip ID to be formatted.341*342* @return The required number of bytes on success, or a negative integer on343* failure. No more than @p size-1 characters be written, with the @p size'th344* set to '\0'.345*346* @sa BHND_CHIPID_MAX_NAMELEN347*/348int349bhnd_format_chip_id(char *buffer, size_t size, uint16_t chip_id)350{351/* All hex formatted IDs are within the range of 0x4000-0x9C3F (40000-1) */352if (chip_id >= 0x4000 && chip_id <= 0x9C3F)353return (snprintf(buffer, size, "BCM%hX", chip_id));354else355return (snprintf(buffer, size, "BCM%hu", chip_id));356}357358/**359* Return a core info record populated from a bhnd-attached @p dev.360*361* @param dev A bhnd device.362*363* @return A core info record for @p dev.364*/365struct bhnd_core_info366bhnd_get_core_info(device_t dev) {367return (struct bhnd_core_info) {368.vendor = bhnd_get_vendor(dev),369.device = bhnd_get_device(dev),370.hwrev = bhnd_get_hwrev(dev),371.core_idx = bhnd_get_core_index(dev),372.unit = bhnd_get_core_unit(dev)373};374}375376/**377* Find a @p class child device with @p unit on @p bus.378*379* @param bus The bhnd-compatible bus to be searched.380* @param class The device class to match on.381* @param unit The core unit number; specify -1 to return the first match382* regardless of unit number.383*384* @retval device_t if a matching child device is found.385* @retval NULL if no matching child device is found.386*/387device_t388bhnd_bus_find_child(device_t bus, bhnd_devclass_t class, int unit)389{390struct bhnd_core_match md = {391BHND_MATCH_CORE_CLASS(class),392BHND_MATCH_CORE_UNIT(unit)393};394395if (unit == -1)396md.m.match.core_unit = 0;397398return bhnd_bus_match_child(bus, &md);399}400401/**402* Find the first child device on @p bus that matches @p desc.403*404* @param bus The bhnd-compatible bus to be searched.405* @param desc A match descriptor.406*407* @retval device_t if a matching child device is found.408* @retval NULL if no matching child device is found.409*/410device_t411bhnd_bus_match_child(device_t bus, const struct bhnd_core_match *desc)412{413device_t *devlistp;414device_t match;415int devcnt;416int error;417418error = device_get_children(bus, &devlistp, &devcnt);419if (error != 0)420return (NULL);421422match = NULL;423for (int i = 0; i < devcnt; i++) {424struct bhnd_core_info ci = bhnd_get_core_info(devlistp[i]);425426if (bhnd_core_matches(&ci, desc)) {427match = devlistp[i];428goto done;429}430}431432done:433free(devlistp, M_TEMP);434return match;435}436437/**438* Retrieve an ordered list of all device instances currently connected to439* @p bus, returning a pointer to the array in @p devlistp and the count440* in @p ndevs.441*442* The memory allocated for the table must be freed via443* bhnd_bus_free_children().444*445* @param bus The bhnd-compatible bus to be queried.446* @param[out] devlist The array of devices.447* @param[out] devcount The number of devices in @p devlistp448* @param order The order in which devices will be returned449* in @p devlist.450*451* @retval 0 success452* @retval non-zero if an error occurs, a regular unix error code will453* be returned.454*/455int456bhnd_bus_get_children(device_t bus, device_t **devlist, int *devcount,457bhnd_device_order order)458{459int error;460461/* Fetch device array */462if ((error = device_get_children(bus, devlist, devcount)))463return (error);464465/* Perform requested sorting */466if ((error = bhnd_sort_devices(*devlist, *devcount, order))) {467bhnd_bus_free_children(*devlist);468return (error);469}470471return (0);472}473474/**475* Free any memory allocated in a previous call to bhnd_bus_get_children().476*477* @param devlist The device array returned by bhnd_bus_get_children().478*/479void480bhnd_bus_free_children(device_t *devlist)481{482free(devlist, M_TEMP);483}484485/**486* Perform in-place sorting of an array of bhnd device instances.487*488* @param devlist An array of bhnd devices.489* @param devcount The number of devices in @p devs.490* @param order The sort order to be used.491*492* @retval 0 success493* @retval EINVAL if the sort order is unknown.494*/495int496bhnd_sort_devices(device_t *devlist, size_t devcount, bhnd_device_order order)497{498int (*compare)(const void *, const void *);499500switch (order) {501case BHND_DEVICE_ORDER_ATTACH:502compare = compare_ascending_probe_order;503break;504case BHND_DEVICE_ORDER_DETACH:505compare = compare_descending_probe_order;506break;507default:508printf("unknown sort order: %d\n", order);509return (EINVAL);510}511512qsort(devlist, devcount, sizeof(*devlist), compare);513return (0);514}515516/*517* Ascending comparison of bhnd device's probe order.518*/519static int520compare_ascending_probe_order(const void *lhs, const void *rhs)521{522device_t ldev, rdev;523int lorder, rorder;524525ldev = (*(const device_t *) lhs);526rdev = (*(const device_t *) rhs);527528lorder = BHND_BUS_GET_PROBE_ORDER(device_get_parent(ldev), ldev);529rorder = BHND_BUS_GET_PROBE_ORDER(device_get_parent(rdev), rdev);530531if (lorder < rorder) {532return (-1);533} else if (lorder > rorder) {534return (1);535} else {536return (0);537}538}539540/*541* Descending comparison of bhnd device's probe order.542*/543static int544compare_descending_probe_order(const void *lhs, const void *rhs)545{546return (compare_ascending_probe_order(rhs, lhs));547}548549/**550* Call device_probe_and_attach() for each of the bhnd bus device's551* children, in bhnd attach order.552*553* @param bus The bhnd-compatible bus for which all children should be probed554* and attached.555*/556int557bhnd_bus_probe_children(device_t bus)558{559device_t *devs;560int ndevs;561int error;562563/* Fetch children in attach order */564error = bhnd_bus_get_children(bus, &devs, &ndevs,565BHND_DEVICE_ORDER_ATTACH);566if (error)567return (error);568569/* Probe and attach all children */570for (int i = 0; i < ndevs; i++) {571device_t child = devs[i];572device_probe_and_attach(child);573}574575bhnd_bus_free_children(devs);576577return (0);578}579580/**581* Walk up the bhnd device hierarchy to locate the root device582* to which the bhndb bridge is attached.583*584* This can be used from within bhnd host bridge drivers to locate the585* actual upstream host device.586*587* @param dev A bhnd device.588* @param bus_class The expected bus (e.g. "pci") to which the bridge root589* should be attached.590*591* @retval device_t if a matching parent device is found.592* @retval NULL if @p dev is not attached via a bhndb bus.593* @retval NULL if no parent device is attached via @p bus_class.594*/595device_t596bhnd_find_bridge_root(device_t dev, devclass_t bus_class)597{598devclass_t bhndb_class;599device_t parent;600601KASSERT(device_get_devclass(device_get_parent(dev)) ==602devclass_find("bhnd"),603("%s not a bhnd device", device_get_nameunit(dev)));604605bhndb_class = devclass_find("bhndb");606607/* Walk the device tree until we hit a bridge */608parent = dev;609while ((parent = device_get_parent(parent)) != NULL) {610if (device_get_devclass(parent) == bhndb_class)611break;612}613614/* No bridge? */615if (parent == NULL)616return (NULL);617618/* Search for a parent attached to the expected bus class */619while ((parent = device_get_parent(parent)) != NULL) {620device_t bus;621622bus = device_get_parent(parent);623if (bus != NULL && device_get_devclass(bus) == bus_class)624return (parent);625}626627/* Not found */628return (NULL);629}630631/**632* Find the first core in @p cores that matches @p desc.633*634* @param cores The table to search.635* @param num_cores The length of @p cores.636* @param desc A match descriptor.637*638* @retval bhnd_core_info if a matching core is found.639* @retval NULL if no matching core is found.640*/641const struct bhnd_core_info *642bhnd_match_core(const struct bhnd_core_info *cores, u_int num_cores,643const struct bhnd_core_match *desc)644{645for (u_int i = 0; i < num_cores; i++) {646if (bhnd_core_matches(&cores[i], desc))647return &cores[i];648}649650return (NULL);651}652653/**654* Find the first core in @p cores with the given @p class.655*656* @param cores The table to search.657* @param num_cores The length of @p cores.658* @param class The device class to match on.659*660* @retval non-NULL if a matching core is found.661* @retval NULL if no matching core is found.662*/663const struct bhnd_core_info *664bhnd_find_core(const struct bhnd_core_info *cores, u_int num_cores,665bhnd_devclass_t class)666{667struct bhnd_core_match md = {668BHND_MATCH_CORE_CLASS(class)669};670671return bhnd_match_core(cores, num_cores, &md);672}673674/**675* Create an equality match descriptor for @p core.676*677* @param core The core info to be matched on.678*679* @return an equality match descriptor for @p core.680*/681struct bhnd_core_match682bhnd_core_get_match_desc(const struct bhnd_core_info *core)683{684return ((struct bhnd_core_match) {685BHND_MATCH_CORE_VENDOR(core->vendor),686BHND_MATCH_CORE_ID(core->device),687BHND_MATCH_CORE_REV(HWREV_EQ(core->hwrev)),688BHND_MATCH_CORE_CLASS(bhnd_core_class(core)),689BHND_MATCH_CORE_IDX(core->core_idx),690BHND_MATCH_CORE_UNIT(core->unit)691});692}693694/**695* Return true if the @p lhs is equal to @p rhs.696*697* @param lhs The first bhnd core descriptor to compare.698* @param rhs The second bhnd core descriptor to compare.699*700* @retval true if @p lhs is equal to @p rhs701* @retval false if @p lhs is not equal to @p rhs702*/703bool704bhnd_cores_equal(const struct bhnd_core_info *lhs,705const struct bhnd_core_info *rhs)706{707struct bhnd_core_match md;708709/* Use an equality match descriptor to perform the comparison */710md = bhnd_core_get_match_desc(rhs);711return (bhnd_core_matches(lhs, &md));712}713714/**715* Return true if the @p core matches @p desc.716*717* @param core A bhnd core descriptor.718* @param desc A match descriptor to compare against @p core.719*720* @retval true if @p core matches @p match.721* @retval false if @p core does not match @p match.722*/723bool724bhnd_core_matches(const struct bhnd_core_info *core,725const struct bhnd_core_match *desc)726{727if (desc->m.match.core_vendor && desc->core_vendor != core->vendor)728return (false);729730if (desc->m.match.core_id && desc->core_id != core->device)731return (false);732733if (desc->m.match.core_unit && desc->core_unit != core->unit)734return (false);735736if (desc->m.match.core_rev &&737!bhnd_hwrev_matches(core->hwrev, &desc->core_rev))738return (false);739740if (desc->m.match.core_idx && desc->core_idx != core->core_idx)741return (false);742743if (desc->m.match.core_class &&744desc->core_class != bhnd_core_class(core))745return (false);746747return true;748}749750/**751* Return true if the @p chip matches @p desc.752*753* @param chip A bhnd chip identifier.754* @param desc A match descriptor to compare against @p chip.755*756* @retval true if @p chip matches @p match.757* @retval false if @p chip does not match @p match.758*/759bool760bhnd_chip_matches(const struct bhnd_chipid *chip,761const struct bhnd_chip_match *desc)762{763if (desc->m.match.chip_id && chip->chip_id != desc->chip_id)764return (false);765766if (desc->m.match.chip_pkg && chip->chip_pkg != desc->chip_pkg)767return (false);768769if (desc->m.match.chip_rev &&770!bhnd_hwrev_matches(chip->chip_rev, &desc->chip_rev))771return (false);772773if (desc->m.match.chip_type && chip->chip_type != desc->chip_type)774return (false);775776return (true);777}778779/**780* Return true if the @p board matches @p desc.781*782* @param board The bhnd board info.783* @param desc A match descriptor to compare against @p board.784*785* @retval true if @p chip matches @p match.786* @retval false if @p chip does not match @p match.787*/788bool789bhnd_board_matches(const struct bhnd_board_info *board,790const struct bhnd_board_match *desc)791{792if (desc->m.match.board_srom_rev &&793!bhnd_hwrev_matches(board->board_srom_rev, &desc->board_srom_rev))794return (false);795796if (desc->m.match.board_vendor &&797board->board_vendor != desc->board_vendor)798return (false);799800if (desc->m.match.board_type && board->board_type != desc->board_type)801return (false);802803if (desc->m.match.board_devid &&804board->board_devid != desc->board_devid)805return (false);806807if (desc->m.match.board_rev &&808!bhnd_hwrev_matches(board->board_rev, &desc->board_rev))809return (false);810811return (true);812}813814/**815* Return true if the @p hwrev matches @p desc.816*817* @param hwrev A bhnd hardware revision.818* @param desc A match descriptor to compare against @p core.819*820* @retval true if @p hwrev matches @p match.821* @retval false if @p hwrev does not match @p match.822*/823bool824bhnd_hwrev_matches(uint16_t hwrev, const struct bhnd_hwrev_match *desc)825{826if (desc->start != BHND_HWREV_INVALID &&827desc->start > hwrev)828return false;829830if (desc->end != BHND_HWREV_INVALID &&831desc->end < hwrev)832return false;833834return true;835}836837/**838* Return true if the @p dev matches @p desc.839*840* @param dev A bhnd device.841* @param desc A match descriptor to compare against @p dev.842*843* @retval true if @p dev matches @p match.844* @retval false if @p dev does not match @p match.845*/846bool847bhnd_device_matches(device_t dev, const struct bhnd_device_match *desc)848{849struct bhnd_core_info core;850const struct bhnd_chipid *chip;851struct bhnd_board_info board;852device_t parent;853int error;854855/* Construct individual match descriptors */856struct bhnd_core_match m_core = { _BHND_CORE_MATCH_COPY(desc) };857struct bhnd_chip_match m_chip = { _BHND_CHIP_MATCH_COPY(desc) };858struct bhnd_board_match m_board = { _BHND_BOARD_MATCH_COPY(desc) };859860/* Fetch and match core info */861if (m_core.m.match_flags) {862/* Only applicable to bhnd-attached cores */863parent = device_get_parent(dev);864if (device_get_devclass(parent) != devclass_find("bhnd")) {865device_printf(dev, "attempting to match core "866"attributes against non-core device\n");867return (false);868}869870core = bhnd_get_core_info(dev);871if (!bhnd_core_matches(&core, &m_core))872return (false);873}874875/* Fetch and match chip info */876if (m_chip.m.match_flags) {877chip = bhnd_get_chipid(dev);878879if (!bhnd_chip_matches(chip, &m_chip))880return (false);881}882883/* Fetch and match board info.884*885* This is not available until after NVRAM is up; earlier device886* matches should not include board requirements */887if (m_board.m.match_flags) {888if ((error = bhnd_read_board_info(dev, &board))) {889device_printf(dev, "failed to read required board info "890"during device matching: %d\n", error);891return (false);892}893894if (!bhnd_board_matches(&board, &m_board))895return (false);896}897898/* All matched */899return (true);900}901902/**903* Search @p table for an entry matching @p dev.904*905* @param dev A bhnd device to match against @p table.906* @param table The device table to search.907* @param entry_size The @p table entry size, in bytes.908*909* @retval non-NULL the first matching device, if any.910* @retval NULL if no matching device is found in @p table.911*/912const struct bhnd_device *913bhnd_device_lookup(device_t dev, const struct bhnd_device *table,914size_t entry_size)915{916const struct bhnd_device *entry;917device_t hostb, parent;918bhnd_attach_type attach_type;919uint32_t dflags;920921parent = device_get_parent(dev);922hostb = bhnd_bus_find_hostb_device(parent);923attach_type = bhnd_get_attach_type(dev);924925for (entry = table; !BHND_DEVICE_IS_END(entry); entry =926(const struct bhnd_device *) ((const char *) entry + entry_size))927{928/* match core info */929if (!bhnd_device_matches(dev, &entry->core))930continue;931932/* match device flags */933dflags = entry->device_flags;934935/* hostb implies BHND_ATTACH_ADAPTER requirement */936if (dflags & BHND_DF_HOSTB)937dflags |= BHND_DF_ADAPTER;938939if (dflags & BHND_DF_ADAPTER)940if (attach_type != BHND_ATTACH_ADAPTER)941continue;942943if (dflags & BHND_DF_HOSTB)944if (dev != hostb)945continue;946947if (dflags & BHND_DF_SOC)948if (attach_type != BHND_ATTACH_NATIVE)949continue;950951/* device found */952return (entry);953}954955/* not found */956return (NULL);957}958959/**960* Scan the device @p table for all quirk flags applicable to @p dev.961*962* @param dev A bhnd device to match against @p table.963* @param table The device table to search.964* @param entry_size The @p table entry size, in bytes.965*966* @return all matching quirk flags.967*/968uint32_t969bhnd_device_quirks(device_t dev, const struct bhnd_device *table,970size_t entry_size)971{972const struct bhnd_device *dent;973const struct bhnd_device_quirk *qent, *qtable;974uint32_t quirks;975976/* Locate the device entry */977if ((dent = bhnd_device_lookup(dev, table, entry_size)) == NULL)978return (0);979980/* Quirks table is optional */981qtable = dent->quirks_table;982if (qtable == NULL)983return (0);984985/* Collect matching device quirk entries */986quirks = 0;987for (qent = qtable; !BHND_DEVICE_QUIRK_IS_END(qent); qent++) {988if (bhnd_device_matches(dev, &qent->desc))989quirks |= qent->quirks;990}991992return (quirks);993}994995/**996* Allocate bhnd(4) resources defined in @p rs from a parent bus.997*998* @param dev The device requesting ownership of the resources.999* @param rs A standard bus resource specification. This will be updated1000* with the allocated resource's RIDs.1001* @param res On success, the allocated bhnd resources.1002*1003* @retval 0 success1004* @retval non-zero if allocation of any non-RF_OPTIONAL resource fails,1005* all allocated resources will be released and a regular1006* unix error code will be returned.1007*/1008int1009bhnd_alloc_resources(device_t dev, struct resource_spec *rs,1010struct bhnd_resource **res)1011{1012/* Initialize output array */1013for (u_int i = 0; rs[i].type != -1; i++)1014res[i] = NULL;10151016for (u_int i = 0; rs[i].type != -1; i++) {1017res[i] = bhnd_alloc_resource_any(dev, rs[i].type, &rs[i].rid,1018rs[i].flags);10191020/* Clean up all allocations on failure */1021if (res[i] == NULL && !(rs[i].flags & RF_OPTIONAL)) {1022bhnd_release_resources(dev, rs, res);1023return (ENXIO);1024}1025}10261027return (0);1028}10291030/**1031* Release bhnd(4) resources defined in @p rs from a parent bus.1032*1033* @param dev The device that owns the resources.1034* @param rs A standard bus resource specification previously initialized1035* by @p bhnd_alloc_resources.1036* @param res The bhnd resources to be released.1037*/1038void1039bhnd_release_resources(device_t dev, const struct resource_spec *rs,1040struct bhnd_resource **res)1041{1042for (u_int i = 0; rs[i].type != -1; i++) {1043if (res[i] == NULL)1044continue;10451046bhnd_release_resource(dev, rs[i].type, rs[i].rid, res[i]);1047res[i] = NULL;1048}1049}10501051/**1052* Allocate and return a new per-core PMU clock control/status (clkctl)1053* instance for @p dev.1054*1055* @param dev The bhnd(4) core device mapped by @p r.1056* @param pmu_dev The bhnd(4) PMU device, implmenting the bhnd_pmu_if1057* interface. The caller is responsible for ensuring that1058* this reference remains valid for the lifetime of the1059* returned clkctl instance.1060* @param r A resource mapping the core's clock control register1061* (see BHND_CLK_CTL_ST). The caller is responsible for1062* ensuring that this resource remains valid for the1063* lifetime of the returned clkctl instance.1064* @param offset The offset to the clock control register within @p r.1065* @param max_latency The PMU's maximum state transition latency in1066* microseconds; this upper bound will be used to busy-wait1067* on PMU state transitions.1068*1069* @retval non-NULL success1070* @retval NULL if allocation fails.1071*1072*/1073struct bhnd_core_clkctl *1074bhnd_alloc_core_clkctl(device_t dev, device_t pmu_dev, struct bhnd_resource *r,1075bus_size_t offset, u_int max_latency)1076{1077struct bhnd_core_clkctl *clkctl;10781079clkctl = malloc(sizeof(*clkctl), M_BHND, M_ZERO | M_NOWAIT);1080if (clkctl == NULL)1081return (NULL);10821083clkctl->cc_dev = dev;1084clkctl->cc_pmu_dev = pmu_dev;1085clkctl->cc_res = r;1086clkctl->cc_res_offset = offset;1087clkctl->cc_max_latency = max_latency;1088clkctl->cc_quirks = bhnd_device_quirks(dev, bhnd_clkctl_devices,1089sizeof(bhnd_clkctl_devices[0]));10901091BHND_CLKCTL_LOCK_INIT(clkctl);10921093return (clkctl);1094}10951096/**1097* Free a clkctl instance previously allocated via bhnd_alloc_core_clkctl().1098*1099* @param clkctl The clkctl instance to be freed.1100*/1101void1102bhnd_free_core_clkctl(struct bhnd_core_clkctl *clkctl)1103{1104BHND_CLKCTL_LOCK_DESTROY(clkctl);11051106free(clkctl, M_BHND);1107}11081109/**1110* Wait for the per-core clock status to be equal to @p value after1111* applying @p mask, timing out after the maximum transition latency is reached.1112*1113* @param clkctl Per-core clkctl state to be queryied.1114* @param value Value to wait for.1115* @param mask Mask to apply prior to value comparison.1116*1117* @retval 0 success1118* @retval ETIMEDOUT if the PMU's maximum transition delay is reached before1119* the clock status matches @p value and @p mask.1120*/1121int1122bhnd_core_clkctl_wait(struct bhnd_core_clkctl *clkctl, uint32_t value,1123uint32_t mask)1124{1125uint32_t clkst;11261127BHND_CLKCTL_LOCK_ASSERT(clkctl, MA_OWNED);11281129/* Bitswapped HTAVAIL/ALPAVAIL work-around */1130if (clkctl->cc_quirks & BHND_CLKCTL_QUIRK_CCS0) {1131uint32_t fmask, fval;11321133fmask = mask & ~(BHND_CCS_HTAVAIL | BHND_CCS_ALPAVAIL);1134fval = value & ~(BHND_CCS_HTAVAIL | BHND_CCS_ALPAVAIL);11351136if (mask & BHND_CCS_HTAVAIL)1137fmask |= BHND_CCS0_HTAVAIL;1138if (value & BHND_CCS_HTAVAIL)1139fval |= BHND_CCS0_HTAVAIL;11401141if (mask & BHND_CCS_ALPAVAIL)1142fmask |= BHND_CCS0_ALPAVAIL;1143if (value & BHND_CCS_ALPAVAIL)1144fval |= BHND_CCS0_ALPAVAIL;11451146mask = fmask;1147value = fval;1148}11491150for (u_int i = 0; i < clkctl->cc_max_latency; i += 10) {1151clkst = bhnd_bus_read_4(clkctl->cc_res, clkctl->cc_res_offset);1152if ((clkst & mask) == (value & mask))1153return (0);11541155DELAY(10);1156}11571158device_printf(clkctl->cc_dev, "clkst wait timeout (value=%#x, "1159"mask=%#x)\n", value, mask);11601161return (ETIMEDOUT);1162}11631164/**1165* Read an NVRAM variable's NUL-terminated string value.1166*1167* @param dev A bhnd bus child device.1168* @param name The NVRAM variable name.1169* @param[out] buf A buffer large enough to hold @p len bytes. On1170* success, the NUL-terminated string value will be1171* written to this buffer. This argment may be NULL if1172* the value is not desired.1173* @param len The maximum capacity of @p buf.1174* @param[out] rlen On success, will be set to the actual size of1175* the requested value (including NUL termination). This1176* argment may be NULL if the size is not desired.1177*1178* @retval 0 success1179* @retval ENOENT The requested variable was not found.1180* @retval ENODEV No valid NVRAM source could be found.1181* @retval ENOMEM If @p buf is non-NULL and a buffer of @p len is too1182* small to hold the requested value.1183* @retval EFTYPE If the variable data cannot be coerced to a valid1184* string representation.1185* @retval ERANGE If value coercion would overflow @p type.1186* @retval non-zero If reading @p name otherwise fails, a regular unix1187* error code will be returned.1188*/1189int1190bhnd_nvram_getvar_str(device_t dev, const char *name, char *buf, size_t len,1191size_t *rlen)1192{1193size_t larg;1194int error;11951196larg = len;1197error = bhnd_nvram_getvar(dev, name, buf, &larg,1198BHND_NVRAM_TYPE_STRING);1199if (rlen != NULL)1200*rlen = larg;12011202return (error);1203}12041205/**1206* Read an NVRAM variable's unsigned integer value.1207*1208* @param dev A bhnd bus child device.1209* @param name The NVRAM variable name.1210* @param[out] value On success, the requested value will be written1211* to this pointer.1212* @param width The output integer type width (1, 2, or1213* 4 bytes).1214*1215* @retval 0 success1216* @retval ENOENT The requested variable was not found.1217* @retval ENODEV No valid NVRAM source could be found.1218* @retval EFTYPE If the variable data cannot be coerced to a1219* a valid unsigned integer representation.1220* @retval ERANGE If value coercion would overflow (or underflow) an1221* unsigned representation of the given @p width.1222* @retval non-zero If reading @p name otherwise fails, a regular unix1223* error code will be returned.1224*/1225int1226bhnd_nvram_getvar_uint(device_t dev, const char *name, void *value, int width)1227{1228bhnd_nvram_type type;1229size_t len;12301231switch (width) {1232case 1:1233type = BHND_NVRAM_TYPE_UINT8;1234break;1235case 2:1236type = BHND_NVRAM_TYPE_UINT16;1237break;1238case 4:1239type = BHND_NVRAM_TYPE_UINT32;1240break;1241default:1242device_printf(dev, "unsupported NVRAM integer width: %d\n",1243width);1244return (EINVAL);1245}12461247len = width;1248return (bhnd_nvram_getvar(dev, name, value, &len, type));1249}12501251/**1252* Read an NVRAM variable's unsigned 8-bit integer value.1253*1254* @param dev A bhnd bus child device.1255* @param name The NVRAM variable name.1256* @param[out] value On success, the requested value will be written1257* to this pointer.1258*1259* @retval 0 success1260* @retval ENOENT The requested variable was not found.1261* @retval ENODEV No valid NVRAM source could be found.1262* @retval EFTYPE If the variable data cannot be coerced to a1263* a valid unsigned integer representation.1264* @retval ERANGE If value coercion would overflow (or underflow) uint8_t.1265* @retval non-zero If reading @p name otherwise fails, a regular unix1266* error code will be returned.1267*/1268int1269bhnd_nvram_getvar_uint8(device_t dev, const char *name, uint8_t *value)1270{1271return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));1272}12731274/**1275* Read an NVRAM variable's unsigned 16-bit integer value.1276*1277* @param dev A bhnd bus child device.1278* @param name The NVRAM variable name.1279* @param[out] value On success, the requested value will be written1280* to this pointer.1281*1282* @retval 0 success1283* @retval ENOENT The requested variable was not found.1284* @retval ENODEV No valid NVRAM source could be found.1285* @retval EFTYPE If the variable data cannot be coerced to a1286* a valid unsigned integer representation.1287* @retval ERANGE If value coercion would overflow (or underflow)1288* uint16_t.1289* @retval non-zero If reading @p name otherwise fails, a regular unix1290* error code will be returned.1291*/1292int1293bhnd_nvram_getvar_uint16(device_t dev, const char *name, uint16_t *value)1294{1295return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));1296}12971298/**1299* Read an NVRAM variable's unsigned 32-bit integer value.1300*1301* @param dev A bhnd bus child device.1302* @param name The NVRAM variable name.1303* @param[out] value On success, the requested value will be written1304* to this pointer.1305*1306* @retval 0 success1307* @retval ENOENT The requested variable was not found.1308* @retval ENODEV No valid NVRAM source could be found.1309* @retval EFTYPE If the variable data cannot be coerced to a1310* a valid unsigned integer representation.1311* @retval ERANGE If value coercion would overflow (or underflow)1312* uint32_t.1313* @retval non-zero If reading @p name otherwise fails, a regular unix1314* error code will be returned.1315*/1316int1317bhnd_nvram_getvar_uint32(device_t dev, const char *name, uint32_t *value)1318{1319return (bhnd_nvram_getvar_uint(dev, name, value, sizeof(*value)));1320}13211322/**1323* Read an NVRAM variable's signed integer value.1324*1325* @param dev A bhnd bus child device.1326* @param name The NVRAM variable name.1327* @param[out] value On success, the requested value will be written1328* to this pointer.1329* @param width The output integer type width (1, 2, or1330* 4 bytes).1331*1332* @retval 0 success1333* @retval ENOENT The requested variable was not found.1334* @retval ENODEV No valid NVRAM source could be found.1335* @retval EFTYPE If the variable data cannot be coerced to a1336* a valid integer representation.1337* @retval ERANGE If value coercion would overflow (or underflow) an1338* signed representation of the given @p width.1339* @retval non-zero If reading @p name otherwise fails, a regular unix1340* error code will be returned.1341*/1342int1343bhnd_nvram_getvar_int(device_t dev, const char *name, void *value, int width)1344{1345bhnd_nvram_type type;1346size_t len;13471348switch (width) {1349case 1:1350type = BHND_NVRAM_TYPE_INT8;1351break;1352case 2:1353type = BHND_NVRAM_TYPE_INT16;1354break;1355case 4:1356type = BHND_NVRAM_TYPE_INT32;1357break;1358default:1359device_printf(dev, "unsupported NVRAM integer width: %d\n",1360width);1361return (EINVAL);1362}13631364len = width;1365return (bhnd_nvram_getvar(dev, name, value, &len, type));1366}13671368/**1369* Read an NVRAM variable's signed 8-bit integer value.1370*1371* @param dev A bhnd bus child device.1372* @param name The NVRAM variable name.1373* @param[out] value On success, the requested value will be written1374* to this pointer.1375*1376* @retval 0 success1377* @retval ENOENT The requested variable was not found.1378* @retval ENODEV No valid NVRAM source could be found.1379* @retval EFTYPE If the variable data cannot be coerced to a1380* a valid integer representation.1381* @retval ERANGE If value coercion would overflow (or underflow) int8_t.1382* @retval non-zero If reading @p name otherwise fails, a regular unix1383* error code will be returned.1384*/1385int1386bhnd_nvram_getvar_int8(device_t dev, const char *name, int8_t *value)1387{1388return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));1389}13901391/**1392* Read an NVRAM variable's signed 16-bit integer value.1393*1394* @param dev A bhnd bus child device.1395* @param name The NVRAM variable name.1396* @param[out] value On success, the requested value will be written1397* to this pointer.1398*1399* @retval 0 success1400* @retval ENOENT The requested variable was not found.1401* @retval ENODEV No valid NVRAM source could be found.1402* @retval EFTYPE If the variable data cannot be coerced to a1403* a valid integer representation.1404* @retval ERANGE If value coercion would overflow (or underflow)1405* int16_t.1406* @retval non-zero If reading @p name otherwise fails, a regular unix1407* error code will be returned.1408*/1409int1410bhnd_nvram_getvar_int16(device_t dev, const char *name, int16_t *value)1411{1412return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));1413}14141415/**1416* Read an NVRAM variable's signed 32-bit integer value.1417*1418* @param dev A bhnd bus child device.1419* @param name The NVRAM variable name.1420* @param[out] value On success, the requested value will be written1421* to this pointer.1422*1423* @retval 0 success1424* @retval ENOENT The requested variable was not found.1425* @retval ENODEV No valid NVRAM source could be found.1426* @retval EFTYPE If the variable data cannot be coerced to a1427* a valid integer representation.1428* @retval ERANGE If value coercion would overflow (or underflow)1429* int32_t.1430* @retval non-zero If reading @p name otherwise fails, a regular unix1431* error code will be returned.1432*/1433int1434bhnd_nvram_getvar_int32(device_t dev, const char *name, int32_t *value)1435{1436return (bhnd_nvram_getvar_int(dev, name, value, sizeof(*value)));1437}14381439/**1440* Read an NVRAM variable's array value.1441*1442* @param dev A bhnd bus child device.1443* @param name The NVRAM variable name.1444* @param[out] buf A buffer large enough to hold @p size bytes.1445* On success, the requested value will be written1446* to this buffer.1447* @param[in,out] size The required number of bytes to write to1448* @p buf.1449* @param type The desired array element data representation.1450*1451* @retval 0 success1452* @retval ENOENT The requested variable was not found.1453* @retval ENODEV No valid NVRAM source could be found.1454* @retval ENXIO If less than @p size bytes are available.1455* @retval ENOMEM If a buffer of @p size is too small to hold the1456* requested value.1457* @retval EFTYPE If the variable data cannot be coerced to a1458* a valid instance of @p type.1459* @retval ERANGE If value coercion would overflow (or underflow) a1460* representation of @p type.1461* @retval non-zero If reading @p name otherwise fails, a regular unix1462* error code will be returned.1463*/1464int1465bhnd_nvram_getvar_array(device_t dev, const char *name, void *buf, size_t size,1466bhnd_nvram_type type)1467{1468size_t nbytes;1469int error;14701471/* Attempt read */1472nbytes = size;1473if ((error = bhnd_nvram_getvar(dev, name, buf, &nbytes, type)))1474return (error);14751476/* Verify that the expected number of bytes were fetched */1477if (nbytes < size)1478return (ENXIO);14791480return (0);1481}14821483/**1484* Initialize a service provider registry.1485*1486* @param bsr The service registry to initialize.1487*1488* @retval 0 success1489* @retval non-zero if an error occurs initializing the service registry,1490* a regular unix error code will be returned.14911492*/1493int1494bhnd_service_registry_init(struct bhnd_service_registry *bsr)1495{1496STAILQ_INIT(&bsr->entries);1497mtx_init(&bsr->lock, "bhnd_service_registry lock", NULL, MTX_DEF);14981499return (0);1500}15011502/**1503* Release all resources held by @p bsr.1504*1505* @param bsr A service registry instance previously successfully1506* initialized via bhnd_service_registry_init().1507*1508* @retval 0 success1509* @retval EBUSY if active references to service providers registered1510* with @p bsr exist.1511*/1512int1513bhnd_service_registry_fini(struct bhnd_service_registry *bsr)1514{1515struct bhnd_service_entry *entry, *enext;15161517/* Remove everthing we can */1518mtx_lock(&bsr->lock);1519STAILQ_FOREACH_SAFE(entry, &bsr->entries, link, enext) {1520if (entry->refs > 0)1521continue;15221523STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry, link);1524free(entry, M_BHND);1525}15261527if (!STAILQ_EMPTY(&bsr->entries)) {1528mtx_unlock(&bsr->lock);1529return (EBUSY);1530}1531mtx_unlock(&bsr->lock);15321533mtx_destroy(&bsr->lock);1534return (0);1535}15361537/**1538* Register a @p provider for the given @p service.1539*1540* @param bsr Service registry to be modified.1541* @param provider Service provider to register.1542* @param service Service for which @p provider will be registered.1543* @param flags Service provider flags (see BHND_SPF_*).1544*1545* @retval 0 success1546* @retval EEXIST if an entry for @p service already exists.1547* @retval EINVAL if @p service is BHND_SERVICE_ANY.1548* @retval non-zero if registering @p provider otherwise fails, a regular1549* unix error code will be returned.1550*/1551int1552bhnd_service_registry_add(struct bhnd_service_registry *bsr, device_t provider,1553bhnd_service_t service, uint32_t flags)1554{1555struct bhnd_service_entry *entry;15561557if (service == BHND_SERVICE_ANY)1558return (EINVAL);15591560mtx_lock(&bsr->lock);15611562/* Is a service provider already registered? */1563STAILQ_FOREACH(entry, &bsr->entries, link) {1564if (entry->service == service) {1565mtx_unlock(&bsr->lock);1566return (EEXIST);1567}1568}15691570/* Initialize and insert our new entry */1571entry = malloc(sizeof(*entry), M_BHND, M_NOWAIT);1572if (entry == NULL) {1573mtx_unlock(&bsr->lock);1574return (ENOMEM);1575}15761577entry->provider = provider;1578entry->service = service;1579entry->flags = flags;1580refcount_init(&entry->refs, 0);15811582STAILQ_INSERT_HEAD(&bsr->entries, entry, link);15831584mtx_unlock(&bsr->lock);1585return (0);1586}15871588/**1589* Free an unreferenced registry entry.1590*1591* @param entry The entry to be deallocated.1592*/1593static void1594bhnd_service_registry_free_entry(struct bhnd_service_entry *entry)1595{1596KASSERT(entry->refs == 0, ("provider has active references"));1597free(entry, M_BHND);1598}15991600/**1601* Attempt to remove the @p service provider registration for @p provider.1602*1603* @param bsr The service registry to be modified.1604* @param provider The service provider to be deregistered.1605* @param service The service for which @p provider will be deregistered,1606* or BHND_SERVICE_ANY to remove all service1607* registrations for @p provider.1608*1609* @retval 0 success1610* @retval EBUSY if active references to @p provider exist; see1611* bhnd_service_registry_retain() and1612* bhnd_service_registry_release().1613*/1614int1615bhnd_service_registry_remove(struct bhnd_service_registry *bsr,1616device_t provider, bhnd_service_t service)1617{1618struct bhnd_service_entry *entry, *enext;16191620mtx_lock(&bsr->lock);16211622#define BHND_PROV_MATCH(_e) \1623((_e)->provider == provider && \1624(service == BHND_SERVICE_ANY || (_e)->service == service))16251626/* Validate matching provider entries before making any1627* modifications */1628STAILQ_FOREACH(entry, &bsr->entries, link) {1629/* Skip non-matching entries */1630if (!BHND_PROV_MATCH(entry))1631continue;16321633/* Entry is in use? */1634if (entry->refs > 0) {1635mtx_unlock(&bsr->lock);1636return (EBUSY);1637}1638}16391640/* We can now safely remove matching entries */1641STAILQ_FOREACH_SAFE(entry, &bsr->entries, link, enext) {1642/* Skip non-matching entries */1643if (!BHND_PROV_MATCH(entry))1644continue;16451646/* Remove from list */1647STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry, link);16481649/* Free provider entry */1650bhnd_service_registry_free_entry(entry);1651}1652#undef BHND_PROV_MATCH16531654mtx_unlock(&bsr->lock);1655return (0);1656}16571658/**1659* Retain and return a reference to a registered @p service provider, if any.1660*1661* @param bsr The service registry to be queried.1662* @param service The service for which a provider should be returned.1663*1664* On success, the caller assumes ownership the returned provider, and1665* is responsible for releasing this reference via1666* bhnd_service_registry_release().1667*1668* @retval device_t success1669* @retval NULL if no provider is registered for @p service.1670*/1671device_t1672bhnd_service_registry_retain(struct bhnd_service_registry *bsr,1673bhnd_service_t service)1674{1675struct bhnd_service_entry *entry;16761677mtx_lock(&bsr->lock);1678STAILQ_FOREACH(entry, &bsr->entries, link) {1679if (entry->service != service)1680continue;16811682/* With a live refcount, entry is gauranteed to remain alive1683* after we release our lock */1684refcount_acquire(&entry->refs);16851686mtx_unlock(&bsr->lock);1687return (entry->provider);1688}1689mtx_unlock(&bsr->lock);16901691/* Not found */1692return (NULL);1693}16941695/**1696* Release a reference to a service provider previously returned by1697* bhnd_service_registry_retain().1698*1699* If this is the last reference to an inherited service provider registration1700* (see BHND_SPF_INHERITED), the registration will also be removed, and1701* true will be returned.1702*1703* @param bsr The service registry from which @p provider1704* was returned.1705* @param provider The provider to be released.1706* @param service The service for which @p provider was previously1707* retained.1708* @retval true The inherited service provider registration was removed;1709* the caller should release its own reference to the1710* provider.1711* @retval false The service provider was not inherited, or active1712* references to the provider remain.1713*1714* @see BHND_SPF_INHERITED1715*/1716bool1717bhnd_service_registry_release(struct bhnd_service_registry *bsr,1718device_t provider, bhnd_service_t service)1719{1720struct bhnd_service_entry *entry;17211722/* Exclusive lock, as we need to prevent any new references to the1723* entry from being taken if it's to be removed */1724mtx_lock(&bsr->lock);1725STAILQ_FOREACH(entry, &bsr->entries, link) {1726bool removed;17271728if (entry->provider != provider)1729continue;17301731if (entry->service != service)1732continue;17331734if (refcount_release(&entry->refs) &&1735(entry->flags & BHND_SPF_INHERITED))1736{1737/* If an inherited entry is no longer actively1738* referenced, remove the local registration and inform1739* the caller. */1740STAILQ_REMOVE(&bsr->entries, entry, bhnd_service_entry,1741link);1742bhnd_service_registry_free_entry(entry);1743removed = true;1744} else {1745removed = false;1746}17471748mtx_unlock(&bsr->lock);1749return (removed);1750}17511752/* Caller owns a reference, but no such provider is registered? */1753panic("invalid service provider reference");1754}17551756/**1757* Using the bhnd(4) bus-level core information and a custom core name,1758* populate @p dev's device description.1759*1760* @param dev A bhnd-bus attached device.1761* @param dev_name The core's name (e.g. "SDIO Device Core").1762*/1763void1764bhnd_set_custom_core_desc(device_t dev, const char *dev_name)1765{1766const char *vendor_name;17671768vendor_name = bhnd_get_vendor_name(dev);1769device_set_descf(dev, "%s %s, rev %hhu", vendor_name, dev_name,1770bhnd_get_hwrev(dev));1771}17721773/**1774* Using the bhnd(4) bus-level core information, populate @p dev's device1775* description.1776*1777* @param dev A bhnd-bus attached device.1778*/1779void1780bhnd_set_default_core_desc(device_t dev)1781{1782bhnd_set_custom_core_desc(dev, bhnd_get_device_name(dev));1783}17841785/**1786* Using the bhnd @p chip_id, populate the bhnd(4) bus @p dev's device1787* description.1788*1789* @param dev A bhnd-bus attached device.1790* @param chip_id The chip identification.1791*/1792void1793bhnd_set_default_bus_desc(device_t dev, const struct bhnd_chipid *chip_id)1794{1795const char *bus_name;1796char chip_name[BHND_CHIPID_MAX_NAMELEN];17971798/* Determine chip type's bus name */1799switch (chip_id->chip_type) {1800case BHND_CHIPTYPE_SIBA:1801bus_name = "SIBA bus";1802break;1803case BHND_CHIPTYPE_BCMA:1804case BHND_CHIPTYPE_BCMA_ALT:1805bus_name = "BCMA bus";1806break;1807case BHND_CHIPTYPE_UBUS:1808bus_name = "UBUS bus";1809break;1810default:1811bus_name = "Unknown Type";1812break;1813}18141815/* Format chip name */1816bhnd_format_chip_id(chip_name, sizeof(chip_name),1817chip_id->chip_id);18181819/* Format and set device description */1820device_set_descf(dev, "%s %s", chip_name, bus_name);1821}18221823/**1824* Helper function for implementing BHND_BUS_REGISTER_PROVIDER().1825*1826* This implementation delegates the request to the BHND_BUS_REGISTER_PROVIDER()1827* method on the parent of @p dev. If no parent exists, the implementation1828* will return an error.1829*/1830int1831bhnd_bus_generic_register_provider(device_t dev, device_t child,1832device_t provider, bhnd_service_t service)1833{1834device_t parent = device_get_parent(dev);18351836if (parent != NULL) {1837return (BHND_BUS_REGISTER_PROVIDER(parent, child,1838provider, service));1839}18401841return (ENXIO);1842}18431844/**1845* Helper function for implementing BHND_BUS_DEREGISTER_PROVIDER().1846*1847* This implementation delegates the request to the1848* BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent1849* exists, the implementation will panic.1850*/1851int1852bhnd_bus_generic_deregister_provider(device_t dev, device_t child,1853device_t provider, bhnd_service_t service)1854{1855device_t parent = device_get_parent(dev);18561857if (parent != NULL) {1858return (BHND_BUS_DEREGISTER_PROVIDER(parent, child,1859provider, service));1860}18611862panic("missing BHND_BUS_DEREGISTER_PROVIDER()");1863}18641865/**1866* Helper function for implementing BHND_BUS_RETAIN_PROVIDER().1867*1868* This implementation delegates the request to the1869* BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent1870* exists, the implementation will return NULL.1871*/1872device_t1873bhnd_bus_generic_retain_provider(device_t dev, device_t child,1874bhnd_service_t service)1875{1876device_t parent = device_get_parent(dev);18771878if (parent != NULL) {1879return (BHND_BUS_RETAIN_PROVIDER(parent, child,1880service));1881}18821883return (NULL);1884}18851886/**1887* Helper function for implementing BHND_BUS_RELEASE_PROVIDER().1888*1889* This implementation delegates the request to the1890* BHND_BUS_DEREGISTER_PROVIDER() method on the parent of @p dev. If no parent1891* exists, the implementation will panic.1892*/1893void1894bhnd_bus_generic_release_provider(device_t dev, device_t child,1895device_t provider, bhnd_service_t service)1896{1897device_t parent = device_get_parent(dev);18981899if (parent != NULL) {1900return (BHND_BUS_RELEASE_PROVIDER(parent, child,1901provider, service));1902}19031904panic("missing BHND_BUS_RELEASE_PROVIDER()");1905}19061907/**1908* Helper function for implementing BHND_BUS_REGISTER_PROVIDER().1909*1910* This implementation uses the bhnd_service_registry_add() function to1911* do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find1912* a suitable service registry to edit.1913*/1914int1915bhnd_bus_generic_sr_register_provider(device_t dev, device_t child,1916device_t provider, bhnd_service_t service)1917{1918struct bhnd_service_registry *bsr;19191920bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);19211922KASSERT(bsr != NULL, ("NULL service registry"));19231924return (bhnd_service_registry_add(bsr, provider, service, 0));1925}19261927/**1928* Helper function for implementing BHND_BUS_DEREGISTER_PROVIDER().1929*1930* This implementation uses the bhnd_service_registry_remove() function to1931* do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find1932* a suitable service registry to edit.1933*/1934int1935bhnd_bus_generic_sr_deregister_provider(device_t dev, device_t child,1936device_t provider, bhnd_service_t service)1937{1938struct bhnd_service_registry *bsr;19391940bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);19411942KASSERT(bsr != NULL, ("NULL service registry"));19431944return (bhnd_service_registry_remove(bsr, provider, service));1945}19461947/**1948* Helper function for implementing BHND_BUS_RETAIN_PROVIDER().1949*1950* This implementation uses the bhnd_service_registry_retain() function to1951* do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find1952* a suitable service registry.1953*1954* If a local provider for the service is not available, and a parent device is1955* available, this implementation will attempt to fetch and locally register1956* a service provider reference from the parent of @p dev.1957*/1958device_t1959bhnd_bus_generic_sr_retain_provider(device_t dev, device_t child,1960bhnd_service_t service)1961{1962struct bhnd_service_registry *bsr;1963device_t parent, provider;1964int error;19651966bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);1967KASSERT(bsr != NULL, ("NULL service registry"));19681969/*1970* Attempt to fetch a service provider reference from either the local1971* service registry, or if not found, from our parent.1972*1973* If we fetch a provider from our parent, we register the provider1974* with the local service registry to prevent conflicting local1975* registrations from being added.1976*/1977while (1) {1978/* Check the local service registry first */1979provider = bhnd_service_registry_retain(bsr, service);1980if (provider != NULL)1981return (provider);19821983/* Otherwise, try to delegate to our parent (if any) */1984if ((parent = device_get_parent(dev)) == NULL)1985return (NULL);19861987provider = BHND_BUS_RETAIN_PROVIDER(parent, dev, service);1988if (provider == NULL)1989return (NULL);19901991/* Register the inherited service registration with the local1992* registry */1993error = bhnd_service_registry_add(bsr, provider, service,1994BHND_SPF_INHERITED);1995if (error) {1996BHND_BUS_RELEASE_PROVIDER(parent, dev, provider,1997service);1998if (error == EEXIST) {1999/* A valid service provider was registered2000* concurrently; retry fetching from the local2001* registry */2002continue;2003}20042005device_printf(dev, "failed to register service "2006"provider: %d\n", error);2007return (NULL);2008}2009}2010}20112012/**2013* Helper function for implementing BHND_BUS_RELEASE_PROVIDER().2014*2015* This implementation uses the bhnd_service_registry_release() function to2016* do most of the work. It calls BHND_BUS_GET_SERVICE_REGISTRY() to find2017* a suitable service registry.2018*/2019void2020bhnd_bus_generic_sr_release_provider(device_t dev, device_t child,2021device_t provider, bhnd_service_t service)2022{2023struct bhnd_service_registry *bsr;20242025bsr = BHND_BUS_GET_SERVICE_REGISTRY(dev, child);2026KASSERT(bsr != NULL, ("NULL service registry"));20272028/* Release the provider reference; if the refcount hits zero on an2029* inherited reference, true will be returned, and we need to drop2030* our own bus reference to the provider */2031if (!bhnd_service_registry_release(bsr, provider, service))2032return;20332034/* Drop our reference to the borrowed provider */2035BHND_BUS_RELEASE_PROVIDER(device_get_parent(dev), dev, provider,2036service);2037}20382039/**2040* Helper function for implementing BHND_BUS_IS_HW_DISABLED().2041*2042* If a parent device is available, this implementation delegates the2043* request to the BHND_BUS_IS_HW_DISABLED() method on the parent of @p dev.2044*2045* If no parent device is available (i.e. on a the bus root), the hardware2046* is assumed to be usable and false is returned.2047*/2048bool2049bhnd_bus_generic_is_hw_disabled(device_t dev, device_t child)2050{2051if (device_get_parent(dev) != NULL)2052return (BHND_BUS_IS_HW_DISABLED(device_get_parent(dev), child));20532054return (false);2055}20562057/**2058* Helper function for implementing BHND_BUS_GET_CHIPID().2059*2060* This implementation delegates the request to the BHND_BUS_GET_CHIPID()2061* method on the parent of @p dev. If no parent exists, the implementation2062* will panic.2063*/2064const struct bhnd_chipid *2065bhnd_bus_generic_get_chipid(device_t dev, device_t child)2066{2067if (device_get_parent(dev) != NULL)2068return (BHND_BUS_GET_CHIPID(device_get_parent(dev), child));20692070panic("missing BHND_BUS_GET_CHIPID()");2071}20722073/**2074* Helper function for implementing BHND_BUS_GET_DMA_TRANSLATION().2075*2076* If a parent device is available, this implementation delegates the2077* request to the BHND_BUS_GET_DMA_TRANSLATION() method on the parent of @p dev.2078*2079* If no parent device is available, this implementation will panic.2080*/2081int2082bhnd_bus_generic_get_dma_translation(device_t dev, device_t child, u_int width,2083uint32_t flags, bus_dma_tag_t *dmat,2084struct bhnd_dma_translation *translation)2085{2086if (device_get_parent(dev) != NULL) {2087return (BHND_BUS_GET_DMA_TRANSLATION(device_get_parent(dev),2088child, width, flags, dmat, translation));2089}20902091panic("missing BHND_BUS_GET_DMA_TRANSLATION()");2092}20932094/* nvram board_info population macros for bhnd_bus_generic_read_board_info() */2095#define BHND_GV(_dest, _name) \2096bhnd_nvram_getvar_uint(child, BHND_NVAR_ ## _name, &_dest, \2097sizeof(_dest))20982099#define REQ_BHND_GV(_dest, _name) do { \2100if ((error = BHND_GV(_dest, _name))) { \2101device_printf(dev, \2102"error reading " __STRING(_name) ": %d\n", error); \2103return (error); \2104} \2105} while(0)21062107#define OPT_BHND_GV(_dest, _name, _default) do { \2108if ((error = BHND_GV(_dest, _name))) { \2109if (error != ENOENT) { \2110device_printf(dev, \2111"error reading " \2112__STRING(_name) ": %d\n", error); \2113return (error); \2114} \2115_dest = _default; \2116} \2117} while(0)21182119/**2120* Helper function for implementing BHND_BUS_READ_BOARDINFO().2121*2122* This implementation populates @p info with information from NVRAM,2123* defaulting board_vendor and board_type fields to 0 if the2124* requested variables cannot be found.2125*2126* This behavior is correct for most SoCs, but must be overridden on2127* bridged (PCI, PCMCIA, etc) devices to produce a complete bhnd_board_info2128* result.2129*/2130int2131bhnd_bus_generic_read_board_info(device_t dev, device_t child,2132struct bhnd_board_info *info)2133{2134int error;21352136OPT_BHND_GV(info->board_vendor, BOARDVENDOR, 0);2137OPT_BHND_GV(info->board_type, BOARDTYPE, 0); /* srom >= 2 */2138OPT_BHND_GV(info->board_devid, DEVID, 0); /* srom >= 8 */2139REQ_BHND_GV(info->board_rev, BOARDREV);2140OPT_BHND_GV(info->board_srom_rev,SROMREV, 0); /* missing in2141some SoC2142NVRAM */2143REQ_BHND_GV(info->board_flags, BOARDFLAGS);2144OPT_BHND_GV(info->board_flags2, BOARDFLAGS2, 0); /* srom >= 4 */2145OPT_BHND_GV(info->board_flags3, BOARDFLAGS3, 0); /* srom >= 11 */21462147return (0);2148}21492150#undef BHND_GV2151#undef BHND_GV_REQ2152#undef BHND_GV_OPT21532154/**2155* Helper function for implementing BHND_BUS_GET_NVRAM_VAR().2156*2157* This implementation searches @p dev for a usable NVRAM child device.2158*2159* If no usable child device is found on @p dev, the request is delegated to2160* the BHND_BUS_GET_NVRAM_VAR() method on the parent of @p dev.2161*/2162int2163bhnd_bus_generic_get_nvram_var(device_t dev, device_t child, const char *name,2164void *buf, size_t *size, bhnd_nvram_type type)2165{2166device_t nvram;2167device_t parent;21682169bus_topo_assert();21702171/* Look for a directly-attached NVRAM child */2172if ((nvram = device_find_child(dev, "bhnd_nvram", DEVICE_UNIT_ANY)) != NULL)2173return BHND_NVRAM_GETVAR(nvram, name, buf, size, type);21742175/* Try to delegate to parent */2176if ((parent = device_get_parent(dev)) == NULL)2177return (ENODEV);21782179return (BHND_BUS_GET_NVRAM_VAR(device_get_parent(dev), child,2180name, buf, size, type));2181}21822183/**2184* Helper function for implementing BHND_BUS_ALLOC_RESOURCE().2185*2186* This implementation of BHND_BUS_ALLOC_RESOURCE() delegates allocation2187* of the underlying resource to BUS_ALLOC_RESOURCE(), and activation2188* to @p dev's BHND_BUS_ACTIVATE_RESOURCE().2189*/2190struct bhnd_resource *2191bhnd_bus_generic_alloc_resource(device_t dev, device_t child, int type,2192int *rid, rman_res_t start, rman_res_t end, rman_res_t count,2193u_int flags)2194{2195struct bhnd_resource *br;2196struct resource *res;2197int error;21982199br = NULL;2200res = NULL;22012202/* Allocate the real bus resource (without activating it) */2203res = BUS_ALLOC_RESOURCE(dev, child, type, rid, start, end, count,2204(flags & ~RF_ACTIVE));2205if (res == NULL)2206return (NULL);22072208/* Allocate our bhnd resource wrapper. */2209br = malloc(sizeof(struct bhnd_resource), M_BHND, M_NOWAIT);2210if (br == NULL)2211goto failed;22122213br->direct = false;2214br->res = res;22152216/* Attempt activation */2217if (flags & RF_ACTIVE) {2218error = BHND_BUS_ACTIVATE_RESOURCE(dev, child, type, *rid, br);2219if (error)2220goto failed;2221}22222223return (br);22242225failed:2226if (res != NULL)2227BUS_RELEASE_RESOURCE(dev, child, res);22282229free(br, M_BHND);2230return (NULL);2231}22322233/**2234* Helper function for implementing BHND_BUS_RELEASE_RESOURCE().2235*2236* This implementation of BHND_BUS_RELEASE_RESOURCE() delegates release of2237* the backing resource to BUS_RELEASE_RESOURCE().2238*/2239int2240bhnd_bus_generic_release_resource(device_t dev, device_t child, int type,2241int rid, struct bhnd_resource *r)2242{2243int error;22442245if ((error = BUS_RELEASE_RESOURCE(dev, child, r->res)))2246return (error);22472248free(r, M_BHND);2249return (0);2250}22512252/**2253* Helper function for implementing BHND_BUS_ACTIVATE_RESOURCE().2254*2255* This implementation of BHND_BUS_ACTIVATE_RESOURCE() first calls the2256* BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.2257*2258* If this fails, and if @p dev is the direct parent of @p child, standard2259* resource activation is attempted via bus_activate_resource(). This enables2260* direct use of the bhnd(4) resource APIs on devices that may not be attached2261* to a parent bhnd bus or bridge.2262*/2263int2264bhnd_bus_generic_activate_resource(device_t dev, device_t child, int type,2265int rid, struct bhnd_resource *r)2266{2267int error;2268bool passthrough;22692270passthrough = (device_get_parent(child) != dev);22712272/* Try to delegate to the parent */2273if (device_get_parent(dev) != NULL) {2274error = BHND_BUS_ACTIVATE_RESOURCE(device_get_parent(dev),2275child, type, rid, r);2276} else {2277error = ENODEV;2278}22792280/* If bhnd(4) activation has failed and we're the child's direct2281* parent, try falling back on standard resource activation.2282*/2283if (error && !passthrough) {2284error = bus_activate_resource(child, type, rid, r->res);2285if (!error)2286r->direct = true;2287}22882289return (error);2290}22912292/**2293* Helper function for implementing BHND_BUS_DEACTIVATE_RESOURCE().2294*2295* This implementation of BHND_BUS_ACTIVATE_RESOURCE() simply calls the2296* BHND_BUS_ACTIVATE_RESOURCE() method of the parent of @p dev.2297*/2298int2299bhnd_bus_generic_deactivate_resource(device_t dev, device_t child,2300int type, int rid, struct bhnd_resource *r)2301{2302if (device_get_parent(dev) != NULL)2303return (BHND_BUS_DEACTIVATE_RESOURCE(device_get_parent(dev),2304child, type, rid, r));23052306return (EINVAL);2307}23082309/**2310* Helper function for implementing BHND_BUS_GET_INTR_DOMAIN().2311*2312* This implementation simply returns the address of nearest bhnd(4) bus,2313* which may be @p dev; this behavior may be incompatible with FDT/OFW targets.2314*/2315uintptr_t2316bhnd_bus_generic_get_intr_domain(device_t dev, device_t child, bool self)2317{2318return ((uintptr_t)dev);2319}232023212322