2 * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * Abstract BHND Bridge Device Driver
36 * Provides generic support for bridging from a parent bus (such as PCI) to
37 * a BHND-compatible bus (e.g. bcma or siba).
40 #include <sys/param.h>
41 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/systm.h>
46 #include <machine/bus.h>
48 #include <machine/resource.h>
50 #include <dev/bhnd/bhndvar.h>
51 #include <dev/bhnd/bhndreg.h>
53 #include <dev/bhnd/bhnd_erom.h>
55 #include <dev/bhnd/cores/chipc/chipcreg.h>
56 #include <dev/bhnd/nvram/bhnd_nvram.h>
58 #include "bhnd_chipc_if.h"
59 #include "bhnd_nvram_if.h"
62 #include "bhndb_bus_if.h"
63 #include "bhndb_hwdata.h"
64 #include "bhndb_private.h"
67 static u_long bhndb_debug = 0;
68 TUNABLE_ULONG("hw.bhndb.debug", &bhndb_debug);
71 BHNDB_DEBUG_PRIO = 1 << 0,
74 #define BHNDB_DEBUG(_type) (BHNDB_DEBUG_ ## _type & bhndb_debug)
76 static int bhndb_find_hostb_core(struct bhndb_softc *sc,
78 struct bhnd_core_info *core);
80 static bhnd_erom_class_t *bhndb_probe_erom_class(struct bhndb_softc *sc,
81 struct bhnd_chipid *cid);
83 static int bhndb_init_full_config(struct bhndb_softc *sc,
84 bhnd_erom_class_t *eromcls);
86 static struct bhnd_core_info *bhndb_get_bridge_core(struct bhndb_softc *sc);
88 static bool bhndb_hw_matches(struct bhndb_softc *sc,
89 struct bhnd_core_info *cores, u_int ncores,
90 const struct bhndb_hw *hw);
92 static int bhndb_init_region_cfg(struct bhndb_softc *sc,
94 struct bhndb_resources *r,
95 struct bhnd_core_info *cores, u_int ncores,
96 const struct bhndb_hw_priority *table);
98 static int bhndb_find_hwspec(struct bhndb_softc *sc,
99 struct bhnd_core_info *cores, u_int ncores,
100 const struct bhndb_hw **hw);
102 bhndb_addrspace bhndb_get_addrspace(struct bhndb_softc *sc,
105 static struct rman *bhndb_get_rman(struct bhndb_softc *sc,
106 device_t child, int type);
108 static int bhndb_init_child_resource(struct resource *r,
109 struct resource *parent,
113 static int bhndb_activate_static_region(
114 struct bhndb_softc *sc,
115 struct bhndb_region *region,
116 device_t child, int type, int rid,
119 static int bhndb_try_activate_resource(
120 struct bhndb_softc *sc, device_t child,
121 int type, int rid, struct resource *r,
124 static inline struct bhndb_dw_alloc *bhndb_io_resource(struct bhndb_softc *sc,
125 bus_addr_t addr, bus_size_t size,
130 * Default bhndb(4) implementation of DEVICE_PROBE().
132 * This function provides the default bhndb implementation of DEVICE_PROBE(),
133 * and is compatible with bhndb(4) bridges attached via bhndb_attach_bridge().
136 bhndb_generic_probe(device_t dev)
138 return (BUS_PROBE_NOWILDCARD);
142 bhndb_probe_nomatch(device_t dev, device_t child)
146 name = device_get_name(child);
148 name = "unknown device";
150 device_printf(dev, "<%s> (no driver attached)\n", name);
154 bhndb_print_child(device_t dev, device_t child)
156 struct bhndb_softc *sc;
157 struct resource_list *rl;
160 sc = device_get_softc(dev);
162 retval += bus_print_child_header(dev, child);
164 rl = BUS_GET_RESOURCE_LIST(dev, child);
166 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY,
168 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ,
172 retval += bus_print_child_domain(dev, child);
173 retval += bus_print_child_footer(dev, child);
179 bhndb_child_pnpinfo_str(device_t bus, device_t child, char *buf,
187 bhndb_child_location_str(device_t dev, device_t child, char *buf,
190 struct bhndb_softc *sc;
192 sc = device_get_softc(dev);
194 snprintf(buf, buflen, "base=0x%llx",
195 (unsigned long long) sc->chipid.enum_addr);
200 * Return the bridge core info. Will panic if the bridge core info has not yet
201 * been populated during full bridge configuration.
203 * @param sc BHNDB device state.
205 static struct bhnd_core_info *
206 bhndb_get_bridge_core(struct bhndb_softc *sc)
208 if (!sc->have_br_core)
209 panic("bridge not yet fully configured; no bridge core!");
211 return (&sc->bridge_core);
215 * Return true if @p cores matches the @p hw specification.
217 * @param sc BHNDB device state.
218 * @param cores A device table to match against.
219 * @param ncores The number of cores in @p cores.
220 * @param hw The hardware description to be matched against.
223 bhndb_hw_matches(struct bhndb_softc *sc, struct bhnd_core_info *cores,
224 u_int ncores, const struct bhndb_hw *hw)
226 for (u_int i = 0; i < hw->num_hw_reqs; i++) {
227 const struct bhnd_core_match *match;
230 match = &hw->hw_reqs[i];
233 for (u_int d = 0; d < ncores; d++) {
234 struct bhnd_core_info *core = &cores[d];
236 if (BHNDB_IS_CORE_DISABLED(sc->dev, sc->bus_dev, core))
239 if (!bhnd_core_matches(core, match))
254 * Initialize the region maps and priority configuration in @p br using
255 * the priority @p table and the set of cores enumerated by @p erom.
257 * @param sc The bhndb device state.
258 * @param br The resource state to be configured.
259 * @param erom EROM parser used to enumerate @p cores.
260 * @param cores All cores enumerated on the bridged bhnd bus.
261 * @param ncores The length of @p cores.
262 * @param table Hardware priority table to be used to determine the relative
263 * priorities of per-core port resources.
266 bhndb_init_region_cfg(struct bhndb_softc *sc, bhnd_erom_t *erom,
267 struct bhndb_resources *br, struct bhnd_core_info *cores, u_int ncores,
268 const struct bhndb_hw_priority *table)
270 const struct bhndb_hw_priority *hp;
273 size_t prio_low, prio_default, prio_high;
276 /* The number of port regions per priority band that must be accessible
277 * via dynamic register windows */
283 * Register bridge regions covering all statically mapped ports.
285 for (u_int i = 0; i < ncores; i++) {
286 const struct bhndb_regwin *regw;
287 struct bhnd_core_info *core;
288 struct bhnd_core_match md;
291 md = bhnd_core_get_match_desc(core);
293 for (regw = br->cfg->register_windows;
294 regw->win_type != BHNDB_REGWIN_T_INVALID; regw++)
296 /* Only core windows are supported */
297 if (regw->win_type != BHNDB_REGWIN_T_CORE)
300 /* Skip non-matching cores. */
301 if (!bhndb_regwin_match_core(regw, core))
304 /* Fetch the base address of the mapped port */
305 error = bhnd_erom_lookup_core_addr(erom, &md,
306 regw->d.core.port_type,
313 /* Skip non-applicable register windows */
321 * Always defer to the register window's size.
323 * If the port size is smaller than the window size,
324 * this ensures that we fully utilize register windows
325 * larger than the referenced port.
327 * If the port size is larger than the window size, this
328 * ensures that we do not directly map the allocations
329 * within the region to a too-small window.
331 size = regw->win_size;
334 * Add to the bus region list.
336 * The window priority for a statically mapped
337 * region is always HIGH.
339 error = bhndb_add_resource_region(br, addr, size,
340 BHNDB_PRIORITY_HIGH, regw);
347 * Perform priority accounting and register bridge regions for all
348 * ports defined in the priority table
350 for (u_int i = 0; i < ncores; i++) {
351 struct bhndb_region *region;
352 struct bhnd_core_info *core;
353 struct bhnd_core_match md;
356 md = bhnd_core_get_match_desc(core);
359 * Skip priority accounting for cores that ...
362 /* ... do not require bridge resources */
363 if (BHNDB_IS_CORE_DISABLED(sc->dev, sc->bus_dev, core))
366 /* ... do not have a priority table entry */
367 hp = bhndb_hw_priority_find_core(table, core);
371 /* ... are explicitly disabled in the priority table. */
372 if (hp->priority == BHNDB_PRIORITY_NONE)
375 /* Determine the number of dynamic windows required and
376 * register their bus_region entries. */
377 for (u_int i = 0; i < hp->num_ports; i++) {
378 const struct bhndb_port_priority *pp;
382 /* Fetch the address+size of the mapped port. */
383 error = bhnd_erom_lookup_core_addr(erom, &md,
384 pp->type, pp->port, pp->region,
387 /* Skip ports not defined on this device */
394 /* Skip ports with an existing static mapping */
395 region = bhndb_find_resource_region(br, addr, size);
396 if (region != NULL && region->static_regwin != NULL)
399 /* Define a dynamic region for this port */
400 error = bhndb_add_resource_region(br, addr, size,
405 /* Update port mapping counts */
406 switch (pp->priority) {
407 case BHNDB_PRIORITY_NONE:
409 case BHNDB_PRIORITY_LOW:
412 case BHNDB_PRIORITY_DEFAULT:
415 case BHNDB_PRIORITY_HIGH:
422 /* Determine the minimum priority at which we'll allocate direct
423 * register windows from our dynamic pool */
424 size_t prio_total = prio_low + prio_default + prio_high;
425 if (prio_total <= br->dwa_count) {
426 /* low+default+high priority regions get windows */
427 br->min_prio = BHNDB_PRIORITY_LOW;
429 } else if (prio_default + prio_high <= br->dwa_count) {
430 /* default+high priority regions get windows */
431 br->min_prio = BHNDB_PRIORITY_DEFAULT;
434 /* high priority regions get windows */
435 br->min_prio = BHNDB_PRIORITY_HIGH;
438 if (BHNDB_DEBUG(PRIO)) {
439 struct bhndb_region *region;
440 const char *direct_msg, *type_msg;
441 bhndb_priority_t prio, prio_min;
443 prio_min = br->min_prio;
444 device_printf(sc->dev, "min_prio: %d\n", prio_min);
446 STAILQ_FOREACH(region, &br->bus_regions, link) {
447 prio = region->priority;
449 direct_msg = prio >= prio_min ? "direct" : "indirect";
450 type_msg = region->static_regwin ? "static" : "dynamic";
452 device_printf(sc->dev, "region 0x%llx+0x%llx priority "
454 (unsigned long long) region->addr,
455 (unsigned long long) region->size,
457 direct_msg, type_msg);
465 * Find a hardware specification for @p dev.
467 * @param sc The bhndb device state.
468 * @param cores All cores enumerated on the bridged bhnd bus.
469 * @param ncores The length of @p cores.
470 * @param[out] hw On success, the matched hardware specification.
474 * @retval non-zero if an error occurs fetching device info for comparison.
477 bhndb_find_hwspec(struct bhndb_softc *sc, struct bhnd_core_info *cores,
478 u_int ncores, const struct bhndb_hw **hw)
480 const struct bhndb_hw *next, *hw_table;
482 /* Search for the first matching hardware config. */
483 hw_table = BHNDB_BUS_GET_HARDWARE_TABLE(sc->parent_dev, sc->dev);
484 for (next = hw_table; next->hw_reqs != NULL; next++) {
485 if (!bhndb_hw_matches(sc, cores, ncores, next))
497 * Helper function that must be called by subclass bhndb(4) drivers
498 * when implementing DEVICE_ATTACH() before calling any bhnd(4) or bhndb(4)
499 * APIs on the bridge device.
501 * This function will add a bridged bhnd(4) child device with a device order of
502 * BHND_PROBE_BUS. Any subclass bhndb(4) driver may use the BHND_PROBE_*
503 * priority bands to add additional devices that will be attached in
504 * their preferred order relative to the bridged bhnd(4) bus.
506 * @param dev The bridge device to attach.
507 * @param bridge_devclass The device class of the bridging core. This is used
508 * to automatically detect the bridge core, and to disable additional bridge
509 * cores (e.g. PCMCIA on a PCIe device).
512 bhndb_attach(device_t dev, bhnd_devclass_t bridge_devclass)
514 struct bhndb_devinfo *dinfo;
515 struct bhndb_softc *sc;
516 const struct bhndb_hwcfg *cfg;
517 bhnd_erom_class_t *eromcls;
520 sc = device_get_softc(dev);
522 sc->parent_dev = device_get_parent(dev);
523 sc->bridge_class = bridge_devclass;
527 /* Populate generic resource allocation state. */
528 cfg = BHNDB_BUS_GET_GENERIC_HWCFG(sc->parent_dev, sc->dev);
529 sc->bus_res = bhndb_alloc_resources(dev, sc->parent_dev, cfg);
530 if (sc->bus_res == NULL) {
534 /* Allocate our host resources */
535 if ((error = bhndb_alloc_host_resources(sc->bus_res)))
538 /* Probe for a usable EROM class for our bridged bhnd(4) bus and
539 * populate our chip identifier. */
541 if ((eromcls = bhndb_probe_erom_class(sc, &sc->chipid)) == NULL) {
544 device_printf(sc->dev, "device enumeration unsupported; no "
545 "compatible driver found\n");
550 /* Add our bridged bus device */
551 sc->bus_dev = BUS_ADD_CHILD(dev, BHND_PROBE_BUS, "bhnd", -1);
552 if (sc->bus_dev == NULL) {
557 dinfo = device_get_ivars(sc->bus_dev);
558 dinfo->addrspace = BHNDB_ADDRSPACE_BRIDGED;
560 /* Enumerate the bridged device and fully initialize our bridged
561 * resource configuration */
562 if ((error = bhndb_init_full_config(sc, eromcls))) {
563 device_printf(sc->dev, "initializing full bridge "
564 "configuration failed: %d\n", error);
571 BHNDB_LOCK_DESTROY(sc);
573 if (sc->bus_res != NULL)
574 bhndb_free_resources(sc->bus_res);
581 * Return a borrowed reference to the host resource mapping at least
582 * BHND_DEFAULT_CORE_SIZE bytes at the first bus core, for use with
585 * This may return a borrowed reference to a bhndb_dw_alloc-managed
586 * resource; any additional resource mapping requests may invalidate this
587 * borrowed reference.
589 * @param sc BHNDB driver state.
590 * @param[out] offset On success, the offset within the returned resource
591 * at which the first bus core can be found.
593 * @retval non-NULL success.
594 * @retval NULL If no usable mapping could be found.
596 static struct resource *
597 bhndb_erom_chipc_resource(struct bhndb_softc *sc, bus_size_t *offset)
599 const struct bhndb_hwcfg *cfg;
600 struct bhndb_dw_alloc *dwa;
601 struct resource *res;
602 const struct bhndb_regwin *win;
604 BHNDB_LOCK_ASSERT(sc, MA_OWNED);
606 cfg = sc->bus_res->cfg;
608 /* Find a static register window mapping ChipCommon. */
609 win = bhndb_regwin_find_core(cfg->register_windows, BHND_DEVCLASS_CC,
610 0, BHND_PORT_DEVICE, 0, 0);
612 if (win->win_size < BHND_DEFAULT_CORE_SIZE) {
613 device_printf(sc->dev,
614 "chipcommon register window too small\n");
618 res = bhndb_find_regwin_resource(sc->bus_res, win);
620 device_printf(sc->dev,
621 "chipcommon register window not allocated\n");
625 *offset = win->win_offset;
629 /* We'll need to fetch and configure a dynamic window. We can assume a
630 * device without a static ChipCommon mapping uses the default siba(4)
632 dwa = bhndb_io_resource(sc, BHND_DEFAULT_CHIPC_ADDR,
633 BHND_DEFAULT_CORE_SIZE, offset);
635 return (dwa->parent_res);
637 device_printf(sc->dev, "unable to map chipcommon registers; no usable "
638 "register window found\n");
643 * Probe all supported EROM classes, returning the best matching class
644 * (or NULL if not found), writing the probed chip identifier to @p cid.
646 * @param sc BHNDB driver state.
647 * @param cid On success, the bridged chipset's chip identifier.
649 static bhnd_erom_class_t *
650 bhndb_probe_erom_class(struct bhndb_softc *sc, struct bhnd_chipid *cid)
652 devclass_t bhndb_devclass;
653 const struct bhnd_chipid *hint;
654 struct resource *res;
655 bus_size_t res_offset;
658 bhnd_erom_class_t *erom_cls;
661 BHNDB_LOCK_ASSERT(sc, MA_OWNED);
666 /* Let our parent device provide a chipid hint */
667 hint = BHNDB_BUS_GET_CHIPID(sc->parent_dev, sc->dev);
669 /* Fetch a borrowed reference to the resource mapping ChipCommon. */
670 res = bhndb_erom_chipc_resource(sc, &res_offset);
674 /* Fetch all available drivers */
675 bhndb_devclass = device_get_devclass(sc->dev);
676 if (devclass_get_drivers(bhndb_devclass, &drivers, &drv_count) != 0)
679 /* Enumerate the drivers looking for the best available EROM class */
680 for (int i = 0; i < drv_count; i++) {
681 struct bhnd_chipid pcid;
682 bhnd_erom_class_t *cls;
684 cls = bhnd_driver_get_erom_class(drivers[i]);
688 kobj_class_compile(cls);
691 result = bhnd_erom_probe(cls, &BHND_DIRECT_RESOURCE(res),
692 res_offset, hint, &pcid);
694 /* The parser did not match if an error was returned */
698 /* Check for a new highest priority match */
699 if (erom_cls == NULL || result > prio) {
706 /* Terminate immediately on BUS_PROBE_SPECIFIC */
707 if (result == BUS_PROBE_SPECIFIC)
714 /* ascending core index comparison used by bhndb_find_hostb_core() */
716 compare_core_index(const void *lhs, const void *rhs)
718 u_int left = ((const struct bhnd_core_info *)lhs)->core_idx;
719 u_int right = ((const struct bhnd_core_info *)rhs)->core_idx;
723 else if (left > right)
730 * Search @p erom for the core serving as the bhnd host bridge.
732 * This function uses a heuristic valid on all known PCI/PCIe/PCMCIA-bridged
733 * bhnd(4) devices to determine the hostb core:
735 * - The core must have a Broadcom vendor ID.
736 * - The core devclass must match the bridge type.
737 * - The core must be the first device on the bus with the bridged device
740 * @param sc BHNDB device state.
741 * @param erom The device enumeration table parser to be used to fetch
743 * @param[out] core If found, the matching core info.
746 * @retval ENOENT not found
747 * @retval non-zero if an error occured fetching core info.
750 bhndb_find_hostb_core(struct bhndb_softc *sc, bhnd_erom_t *erom,
751 struct bhnd_core_info *core)
753 struct bhnd_core_match md;
754 struct bhnd_core_info *cores;
758 if ((error = bhnd_erom_get_core_table(erom, &cores, &ncores)))
761 /* Set up a match descriptor for the required device class. */
762 md = (struct bhnd_core_match) {
763 BHND_MATCH_CORE_CLASS(sc->bridge_class),
764 BHND_MATCH_CORE_UNIT(0)
767 /* Ensure the table is sorted by core index value, ascending;
768 * the host bridge must be the absolute first matching device on the
770 qsort(cores, ncores, sizeof(*cores), compare_core_index);
772 /* Find the hostb core */
774 for (u_int i = 0; i < ncores; i++) {
775 if (bhnd_core_matches(&cores[i], &md)) {
784 bhnd_erom_free_core_table(erom, cores);
790 * Identify the bridged device and perform final bridge resource configuration
791 * based on capabilities of the enumerated device.
793 * Any bridged resources allocated using the generic brige hardware
794 * configuration must be released prior to calling this function.
797 bhndb_init_full_config(struct bhndb_softc *sc, bhnd_erom_class_t *eromcls)
799 struct bhnd_core_info *cores;
800 struct bhndb_resources *br;
801 const struct bhndb_hw_priority *hwprio;
803 const struct bhndb_hw *hw;
811 /* Allocate EROM parser instance */
812 erom = bhnd_erom_alloc(eromcls, &sc->chipid, sc->bus_dev, 0);
814 device_printf(sc->dev, "failed to allocate device enumeration "
819 /* Look for our host bridge core */
820 if ((error = bhndb_find_hostb_core(sc, erom, &sc->bridge_core))) {
821 device_printf(sc->dev, "no host bridge core found\n");
824 sc->have_br_core = true;
827 /* Fetch the bridged device's core table */
828 if ((error = bhnd_erom_get_core_table(erom, &cores, &ncores))) {
829 device_printf(sc->dev, "error fetching core table: %d\n",
834 /* Find our full register window configuration */
835 if ((error = bhndb_find_hwspec(sc, cores, ncores, &hw))) {
836 device_printf(sc->dev, "unable to identify device, "
837 " using generic bridge resource definitions\n");
842 if (bootverbose || BHNDB_DEBUG(PRIO))
843 device_printf(sc->dev, "%s resource configuration\n", hw->name);
845 /* Allocate new bridge resource state using the discovered hardware
847 br = bhndb_alloc_resources(sc->dev, sc->parent_dev, hw->cfg);
849 device_printf(sc->dev,
850 "failed to allocate new resource state\n");
855 /* Populate our resource priority configuration */
856 hwprio = BHNDB_BUS_GET_HARDWARE_PRIO(sc->parent_dev, sc->dev);
857 error = bhndb_init_region_cfg(sc, erom, br, cores, ncores, hwprio);
859 device_printf(sc->dev, "failed to initialize resource "
860 "priority configuration: %d\n", error);
864 /* The EROM parser holds a reference to the resource state we're
865 * about to invalidate */
866 bhnd_erom_free_core_table(erom, cores);
867 bhnd_erom_free(erom);
872 /* Replace existing resource state */
873 bhndb_free_resources(sc->bus_res);
876 /* Pointer is now owned by sc->bus_res */
879 /* Re-allocate host resources */
880 if ((error = bhndb_alloc_host_resources(sc->bus_res))) {
881 device_printf(sc->dev, "failed to reallocate bridge host "
882 "resources: %d\n", error);
890 bhnd_erom_free_core_table(erom, cores);
893 bhnd_erom_free(erom);
896 bhndb_free_resources(br);
902 * Default bhndb(4) implementation of DEVICE_DETACH().
904 * This function detaches any child devices, and if successful, releases all
905 * resources held by the bridge device.
908 bhndb_generic_detach(device_t dev)
910 struct bhndb_softc *sc;
913 sc = device_get_softc(dev);
915 /* Detach children */
916 if ((error = bus_generic_detach(dev)))
919 /* Clean up our driver state. */
920 bhndb_free_resources(sc->bus_res);
922 BHNDB_LOCK_DESTROY(sc);
928 * Default bhndb(4) implementation of DEVICE_SUSPEND().
930 * This function calls bus_generic_suspend() (or implements equivalent
934 bhndb_generic_suspend(device_t dev)
936 return (bus_generic_suspend(dev));
940 * Default bhndb(4) implementation of DEVICE_RESUME().
942 * This function calls bus_generic_resume() (or implements equivalent
946 bhndb_generic_resume(device_t dev)
948 struct bhndb_softc *sc;
949 struct bhndb_resources *bus_res;
950 struct bhndb_dw_alloc *dwa;
953 sc = device_get_softc(dev);
954 bus_res = sc->bus_res;
956 /* Guarantee that all in-use dynamic register windows are mapped to
957 * their previously configured target address. */
959 for (size_t i = 0; i < bus_res->dwa_count; i++) {
960 dwa = &bus_res->dw_alloc[i];
962 /* Skip regions that were not previously used */
963 if (bhndb_dw_is_free(bus_res, dwa) && dwa->target == 0x0)
966 /* Otherwise, ensure the register window is correct before
967 * any children attempt MMIO */
968 error = BHNDB_SET_WINDOW_ADDR(dev, dwa->win, dwa->target);
974 /* Error restoring hardware state; children cannot be safely resumed */
976 device_printf(dev, "Unable to restore hardware configuration; "
977 "cannot resume: %d\n", error);
981 return (bus_generic_resume(dev));
985 * Default implementation of BHNDB_SUSPEND_RESOURCE.
988 bhndb_suspend_resource(device_t dev, device_t child, int type,
991 struct bhndb_softc *sc;
992 struct bhndb_dw_alloc *dwa;
994 sc = device_get_softc(dev);
996 /* Non-MMIO resources (e.g. IRQs) are handled solely by our parent */
997 if (type != SYS_RES_MEMORY)
1001 dwa = bhndb_dw_find_resource(sc->bus_res, r);
1007 if (BHNDB_DEBUG(PRIO))
1008 device_printf(child, "suspend resource type=%d 0x%jx+0x%jx\n",
1009 type, rman_get_start(r), rman_get_size(r));
1011 /* Release the resource's window reference */
1012 bhndb_dw_release(sc->bus_res, dwa, r);
1017 * Default implementation of BHNDB_RESUME_RESOURCE.
1020 bhndb_resume_resource(device_t dev, device_t child, int type,
1023 struct bhndb_softc *sc;
1025 sc = device_get_softc(dev);
1027 /* Non-MMIO resources (e.g. IRQs) are handled solely by our parent */
1028 if (type != SYS_RES_MEMORY)
1031 /* Inactive resources don't require reallocation of bridge resources */
1032 if (!(rman_get_flags(r) & RF_ACTIVE))
1035 if (BHNDB_DEBUG(PRIO))
1036 device_printf(child, "resume resource type=%d 0x%jx+0x%jx\n",
1037 type, rman_get_start(r), rman_get_size(r));
1039 return (bhndb_try_activate_resource(sc, rman_get_device(r), type,
1040 rman_get_rid(r), r, NULL));
1044 * Default bhndb(4) implementation of BUS_READ_IVAR().
1047 bhndb_read_ivar(device_t dev, device_t child, int index,
1054 * Default bhndb(4) implementation of BUS_WRITE_IVAR().
1057 bhndb_write_ivar(device_t dev, device_t child, int index,
1064 * Return the address space for the given @p child device.
1067 bhndb_get_addrspace(struct bhndb_softc *sc, device_t child)
1069 struct bhndb_devinfo *dinfo;
1072 /* Find the directly attached parent of the requesting device */
1074 while (imd_dev != NULL && device_get_parent(imd_dev) != sc->dev)
1075 imd_dev = device_get_parent(imd_dev);
1077 if (imd_dev == NULL)
1078 panic("bhndb address space request for non-child device %s\n",
1079 device_get_nameunit(child));
1081 dinfo = device_get_ivars(imd_dev);
1082 return (dinfo->addrspace);
1086 * Return the rman instance for a given resource @p type, if any.
1088 * @param sc The bhndb device state.
1089 * @param child The requesting child.
1090 * @param type The resource type (e.g. SYS_RES_MEMORY, SYS_RES_IRQ, ...)
1092 static struct rman *
1093 bhndb_get_rman(struct bhndb_softc *sc, device_t child, int type)
1095 switch (bhndb_get_addrspace(sc, child)) {
1096 case BHNDB_ADDRSPACE_NATIVE:
1098 case SYS_RES_MEMORY:
1099 return (&sc->bus_res->ht_mem_rman);
1106 case BHNDB_ADDRSPACE_BRIDGED:
1108 case SYS_RES_MEMORY:
1109 return (&sc->bus_res->br_mem_rman);
1122 * Default implementation of BUS_ADD_CHILD()
1125 bhndb_add_child(device_t dev, u_int order, const char *name, int unit)
1127 struct bhndb_devinfo *dinfo;
1130 child = device_add_child_ordered(dev, order, name, unit);
1134 dinfo = malloc(sizeof(struct bhndb_devinfo), M_BHND, M_NOWAIT);
1135 if (dinfo == NULL) {
1136 device_delete_child(dev, child);
1140 dinfo->addrspace = BHNDB_ADDRSPACE_NATIVE;
1141 resource_list_init(&dinfo->resources);
1143 device_set_ivars(child, dinfo);
1149 * Default implementation of BUS_CHILD_DELETED().
1152 bhndb_child_deleted(device_t dev, device_t child)
1154 struct bhndb_devinfo *dinfo = device_get_ivars(child);
1155 if (dinfo != NULL) {
1156 resource_list_free(&dinfo->resources);
1157 free(dinfo, M_BHND);
1160 device_set_ivars(child, NULL);
1164 * Default implementation of BHNDB_GET_CHIPID().
1166 static const struct bhnd_chipid *
1167 bhndb_get_chipid(device_t dev, device_t child)
1169 struct bhndb_softc *sc = device_get_softc(dev);
1170 return (&sc->chipid);
1174 * Default implementation of BHNDB_IS_CORE_DISABLED().
1177 bhndb_is_core_disabled(device_t dev, device_t child,
1178 struct bhnd_core_info *core)
1180 struct bhndb_softc *sc;
1181 struct bhnd_core_info *bridge_core;
1183 sc = device_get_softc(dev);
1185 /* Try to defer to the bhndb bus parent */
1186 if (BHNDB_BUS_IS_CORE_DISABLED(sc->parent_dev, dev, core))
1189 /* Otherwise, we treat bridge-capable cores as unpopulated if they're
1190 * not the configured host bridge */
1191 bridge_core = bhndb_get_bridge_core(sc);
1192 if (BHND_DEVCLASS_SUPPORTS_HOSTB(bhnd_core_class(core)))
1193 return (!bhnd_cores_equal(core, bridge_core));
1195 /* Assume the core is populated */
1200 * Default bhndb(4) implementation of BHNDB_GET_HOSTB_CORE().
1202 * This function uses a heuristic valid on all known PCI/PCIe/PCMCIA-bridged
1206 bhndb_get_hostb_core(device_t dev, device_t child, struct bhnd_core_info *core)
1208 struct bhndb_softc *sc = device_get_softc(dev);
1210 *core = *bhndb_get_bridge_core(sc);
1215 * Default bhndb(4) implementation of BUS_ALLOC_RESOURCE().
1217 static struct resource *
1218 bhndb_alloc_resource(device_t dev, device_t child, int type,
1219 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1221 struct bhndb_softc *sc;
1222 struct resource_list_entry *rle;
1223 struct resource *rv;
1226 bool passthrough, isdefault;
1228 sc = device_get_softc(dev);
1229 passthrough = (device_get_parent(child) != dev);
1230 isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
1233 /* Fetch the resource manager */
1234 rm = bhndb_get_rman(sc, child, type);
1236 /* Delegate to our parent device's bus; the requested
1237 * resource type isn't handled locally. */
1238 return (BUS_ALLOC_RESOURCE(device_get_parent(sc->parent_dev),
1239 child, type, rid, start, end, count, flags));
1242 /* Populate defaults */
1243 if (!passthrough && isdefault) {
1244 /* Fetch the resource list entry. */
1245 rle = resource_list_find(BUS_GET_RESOURCE_LIST(dev, child),
1249 "default resource %#x type %d for child %s "
1250 "not found\n", *rid, type,
1251 device_get_nameunit(child));
1256 if (rle->res != NULL) {
1258 "resource entry %#x type %d for child %s is busy\n",
1259 *rid, type, device_get_nameunit(child));
1266 count = ulmax(count, rle->count);
1269 /* Validate resource addresses */
1270 if (start > end || count > ((end - start) + 1))
1273 /* Make our reservation */
1274 rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
1279 rman_set_rid(rv, *rid);
1282 if (flags & RF_ACTIVE) {
1283 error = bus_activate_resource(child, type, *rid, rv);
1286 "failed to activate entry %#x type %d for "
1288 *rid, type, device_get_nameunit(child), error);
1290 rman_release_resource(rv);
1296 /* Update child's resource list entry */
1299 rle->start = rman_get_start(rv);
1300 rle->end = rman_get_end(rv);
1301 rle->count = rman_get_size(rv);
1308 * Default bhndb(4) implementation of BUS_RELEASE_RESOURCE().
1311 bhndb_release_resource(device_t dev, device_t child, int type, int rid,
1314 struct bhndb_softc *sc;
1315 struct resource_list_entry *rle;
1319 sc = device_get_softc(dev);
1320 passthrough = (device_get_parent(child) != dev);
1322 /* Delegate to our parent device's bus if the requested resource type
1323 * isn't handled locally. */
1324 if (bhndb_get_rman(sc, child, type) == NULL) {
1325 return (BUS_RELEASE_RESOURCE(device_get_parent(sc->parent_dev),
1326 child, type, rid, r));
1329 /* Deactivate resources */
1330 if (rman_get_flags(r) & RF_ACTIVE) {
1331 error = BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, r);
1336 if ((error = rman_release_resource(r)))
1340 /* Clean resource list entry */
1341 rle = resource_list_find(BUS_GET_RESOURCE_LIST(dev, child),
1351 * Default bhndb(4) implementation of BUS_ADJUST_RESOURCE().
1354 bhndb_adjust_resource(device_t dev, device_t child, int type,
1355 struct resource *r, rman_res_t start, rman_res_t end)
1357 struct bhndb_softc *sc;
1359 rman_res_t mstart, mend;
1362 sc = device_get_softc(dev);
1365 /* Delegate to our parent device's bus if the requested resource type
1366 * isn't handled locally. */
1367 rm = bhndb_get_rman(sc, child, type);
1369 return (BUS_ADJUST_RESOURCE(device_get_parent(sc->parent_dev),
1370 child, type, r, start, end));
1373 /* Verify basic constraints */
1377 if (!rman_is_region_manager(r, rm))
1382 /* If not active, allow any range permitted by the resource manager */
1383 if (!(rman_get_flags(r) & RF_ACTIVE))
1386 /* Otherwise, the range is limited to the existing register window
1388 error = bhndb_find_resource_limits(sc->bus_res, r, &mstart, &mend);
1392 if (start < mstart || end > mend) {
1400 error = rman_adjust_resource(r, start, end);
1407 * Initialize child resource @p r with a virtual address, tag, and handle
1408 * copied from @p parent, adjusted to contain only the range defined by
1409 * @p offsize and @p size.
1411 * @param r The register to be initialized.
1412 * @param parent The parent bus resource that fully contains the subregion.
1413 * @param offset The subregion offset within @p parent.
1414 * @param size The subregion size.
1418 bhndb_init_child_resource(struct resource *r,
1419 struct resource *parent, bhnd_size_t offset, bhnd_size_t size)
1421 bus_space_handle_t bh, child_bh;
1426 /* Fetch the parent resource's real bus values */
1427 vaddr = (uintptr_t) rman_get_virtual(parent);
1428 bt = rman_get_bustag(parent);
1429 bh = rman_get_bushandle(parent);
1431 /* Configure child resource with window-adjusted real bus values */
1433 error = bus_space_subregion(bt, bh, offset, size, &child_bh);
1437 rman_set_virtual(r, (void *) vaddr);
1438 rman_set_bustag(r, bt);
1439 rman_set_bushandle(r, child_bh);
1445 * Attempt activation of a fixed register window mapping for @p child.
1447 * @param sc BHNDB device state.
1448 * @param region The static region definition capable of mapping @p r.
1449 * @param child A child requesting resource activation.
1450 * @param type Resource type.
1451 * @param rid Resource identifier.
1452 * @param r Resource to be activated.
1454 * @retval 0 if @p r was activated successfully
1455 * @retval ENOENT if no fixed register window was found.
1456 * @retval non-zero if @p r could not be activated.
1459 bhndb_activate_static_region(struct bhndb_softc *sc,
1460 struct bhndb_region *region, device_t child, int type, int rid,
1463 struct resource *bridge_res;
1464 const struct bhndb_regwin *win;
1465 bhnd_size_t parent_offset;
1466 rman_res_t r_start, r_size;
1469 win = region->static_regwin;
1471 KASSERT(win != NULL && BHNDB_REGWIN_T_IS_STATIC(win->win_type),
1472 ("can't activate non-static region"));
1474 r_start = rman_get_start(r);
1475 r_size = rman_get_size(r);
1477 /* Find the corresponding bridge resource */
1478 bridge_res = bhndb_find_regwin_resource(sc->bus_res, win);
1479 if (bridge_res == NULL)
1482 /* Calculate subregion offset within the parent resource */
1483 parent_offset = r_start - region->addr;
1484 parent_offset += win->win_offset;
1486 /* Configure resource with its real bus values. */
1487 error = bhndb_init_child_resource(r, bridge_res, parent_offset, r_size);
1492 if ((error = rman_activate_resource(r)))
1499 * Attempt to allocate/retain a dynamic register window for @p r, returning
1500 * the retained window.
1502 * @param sc The bhndb driver state.
1503 * @param r The resource for which a window will be retained.
1505 static struct bhndb_dw_alloc *
1506 bhndb_retain_dynamic_window(struct bhndb_softc *sc, struct resource *r)
1508 struct bhndb_dw_alloc *dwa;
1509 rman_res_t r_start, r_size;
1512 BHNDB_LOCK_ASSERT(sc, MA_OWNED);
1514 r_start = rman_get_start(r);
1515 r_size = rman_get_size(r);
1517 /* Look for an existing dynamic window we can reference */
1518 dwa = bhndb_dw_find_mapping(sc->bus_res, r_start, r_size);
1520 if (bhndb_dw_retain(sc->bus_res, dwa, r) == 0)
1526 /* Otherwise, try to reserve a free window */
1527 dwa = bhndb_dw_next_free(sc->bus_res);
1529 /* No free windows */
1533 /* Window must be large enough to map the entire resource */
1534 if (dwa->win->win_size < rman_get_size(r))
1537 /* Set the window target */
1538 error = bhndb_dw_set_addr(sc->dev, sc->bus_res, dwa, rman_get_start(r),
1541 device_printf(sc->dev, "dynamic window initialization "
1542 "for 0x%llx-0x%llx failed: %d\n",
1543 (unsigned long long) r_start,
1544 (unsigned long long) r_start + r_size - 1,
1549 /* Add our reservation */
1550 if (bhndb_dw_retain(sc->bus_res, dwa, r))
1557 * Activate a resource using any viable static or dynamic register window.
1559 * @param sc The bhndb driver state.
1560 * @param child The child holding ownership of @p r.
1561 * @param type The type of the resource to be activated.
1562 * @param rid The resource ID of @p r.
1563 * @param r The resource to be activated
1564 * @param[out] indirect On error and if not NULL, will be set to 'true' if
1565 * the caller should instead use an indirect resource mapping.
1568 * @retval non-zero activation failed.
1571 bhndb_try_activate_resource(struct bhndb_softc *sc, device_t child, int type,
1572 int rid, struct resource *r, bool *indirect)
1574 struct bhndb_region *region;
1575 struct bhndb_dw_alloc *dwa;
1576 bhndb_priority_t dw_priority;
1577 rman_res_t r_start, r_size;
1578 rman_res_t parent_offset;
1581 BHNDB_LOCK_ASSERT(sc, MA_NOTOWNED);
1583 /* Only MMIO resources can be mapped via register windows */
1584 if (type != SYS_RES_MEMORY)
1590 r_start = rman_get_start(r);
1591 r_size = rman_get_size(r);
1593 /* Activate native addrspace resources using the host address space */
1594 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_NATIVE) {
1595 struct resource *parent;
1597 /* Find the bridge resource referenced by the child */
1598 parent = bhndb_find_resource_range(sc->bus_res, r_start,
1600 if (parent == NULL) {
1601 device_printf(sc->dev, "host resource not found "
1602 "for 0x%llx-0x%llx\n",
1603 (unsigned long long) r_start,
1604 (unsigned long long) r_start + r_size - 1);
1608 /* Initialize child resource with the real bus values */
1609 error = bhndb_init_child_resource(r, parent,
1610 r_start - rman_get_start(parent), r_size);
1614 /* Try to activate child resource */
1615 return (rman_activate_resource(r));
1618 /* Default to low priority */
1619 dw_priority = BHNDB_PRIORITY_LOW;
1621 /* Look for a bus region matching the resource's address range */
1622 region = bhndb_find_resource_region(sc->bus_res, r_start, r_size);
1624 dw_priority = region->priority;
1626 /* Prefer static mappings over consuming a dynamic windows. */
1627 if (region && region->static_regwin) {
1628 error = bhndb_activate_static_region(sc, region, child, type,
1631 device_printf(sc->dev, "static window allocation "
1632 "for 0x%llx-0x%llx failed\n",
1633 (unsigned long long) r_start,
1634 (unsigned long long) r_start + r_size - 1);
1638 /* A dynamic window will be required; is this resource high enough
1639 * priority to be reserved a dynamic window? */
1640 if (dw_priority < sc->bus_res->min_prio) {
1647 /* Find and retain a usable window */
1649 dwa = bhndb_retain_dynamic_window(sc, r);
1658 /* Configure resource with its real bus values. */
1659 parent_offset = dwa->win->win_offset;
1660 parent_offset += r_start - dwa->target;
1662 error = bhndb_init_child_resource(r, dwa->parent_res, parent_offset,
1663 dwa->win->win_size);
1668 if ((error = rman_activate_resource(r)))
1674 /* Release our region allocation. */
1676 bhndb_dw_release(sc->bus_res, dwa, r);
1683 * Default bhndb(4) implementation of BUS_ACTIVATE_RESOURCE().
1685 * Maps resource activation requests to a viable static or dynamic
1686 * register window, if any.
1689 bhndb_activate_resource(device_t dev, device_t child, int type, int rid,
1692 struct bhndb_softc *sc = device_get_softc(dev);
1694 /* Delegate directly to our parent device's bus if the requested
1695 * resource type isn't handled locally. */
1696 if (bhndb_get_rman(sc, child, type) == NULL) {
1697 return (BUS_ACTIVATE_RESOURCE(device_get_parent(sc->parent_dev),
1698 child, type, rid, r));
1701 return (bhndb_try_activate_resource(sc, child, type, rid, r, NULL));
1705 * Default bhndb(4) implementation of BUS_DEACTIVATE_RESOURCE().
1708 bhndb_deactivate_resource(device_t dev, device_t child, int type,
1709 int rid, struct resource *r)
1711 struct bhndb_dw_alloc *dwa;
1712 struct bhndb_softc *sc;
1716 sc = device_get_softc(dev);
1718 /* Delegate directly to our parent device's bus if the requested
1719 * resource type isn't handled locally. */
1720 rm = bhndb_get_rman(sc, child, type);
1722 return (BUS_DEACTIVATE_RESOURCE(
1723 device_get_parent(sc->parent_dev), child, type, rid, r));
1727 if ((error = rman_deactivate_resource(r)))
1730 /* Free any dynamic window allocation. */
1731 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) {
1733 dwa = bhndb_dw_find_resource(sc->bus_res, r);
1735 bhndb_dw_release(sc->bus_res, dwa, r);
1743 * Default bhndb(4) implementation of BUS_GET_RESOURCE_LIST().
1745 static struct resource_list *
1746 bhndb_get_resource_list(device_t dev, device_t child)
1748 struct bhndb_devinfo *dinfo = device_get_ivars(child);
1749 return (&dinfo->resources);
1753 * Default bhndb(4) implementation of BHND_BUS_ACTIVATE_RESOURCE().
1755 * For BHNDB_ADDRSPACE_NATIVE children, all resources may be assumed to
1756 * be activated by the bridge.
1758 * For BHNDB_ADDRSPACE_BRIDGED children, attempts to activate a static register
1759 * window, a dynamic register window, or configures @p r as an indirect
1760 * resource -- in that order.
1763 bhndb_activate_bhnd_resource(device_t dev, device_t child,
1764 int type, int rid, struct bhnd_resource *r)
1766 struct bhndb_softc *sc;
1767 struct bhndb_region *region;
1768 rman_res_t r_start, r_size;
1773 ("direct flag set on inactive resource"));
1775 KASSERT(!(rman_get_flags(r->res) & RF_ACTIVE),
1776 ("RF_ACTIVE set on inactive resource"));
1778 sc = device_get_softc(dev);
1780 /* Delegate directly to BUS_ACTIVATE_RESOURCE() if the requested
1781 * resource type isn't handled locally. */
1782 if (bhndb_get_rman(sc, child, type) == NULL) {
1783 error = BUS_ACTIVATE_RESOURCE(dev, child, type, rid, r->res);
1789 r_start = rman_get_start(r->res);
1790 r_size = rman_get_size(r->res);
1792 /* Verify bridged address range's resource priority, and skip direct
1793 * allocation if the priority is too low. */
1794 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) {
1795 bhndb_priority_t r_prio;
1797 region = bhndb_find_resource_region(sc->bus_res, r_start,
1800 r_prio = region->priority;
1802 r_prio = BHNDB_PRIORITY_NONE;
1804 /* If less than the minimum dynamic window priority, this
1805 * resource should always be indirect. */
1806 if (r_prio < sc->bus_res->min_prio)
1810 /* Attempt direct activation */
1811 error = bhndb_try_activate_resource(sc, child, type, rid, r->res,
1815 } else if (indirect) {
1816 /* The request was valid, but no viable register window is
1817 * available; indirection must be employed. */
1822 if (BHNDB_DEBUG(PRIO) &&
1823 bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED)
1825 device_printf(child, "activated 0x%llx-0x%llx as %s "
1827 (unsigned long long) r_start,
1828 (unsigned long long) r_start + r_size - 1,
1829 r->direct ? "direct" : "indirect");
1836 * Default bhndb(4) implementation of BHND_BUS_DEACTIVATE_RESOURCE().
1839 bhndb_deactivate_bhnd_resource(device_t dev, device_t child,
1840 int type, int rid, struct bhnd_resource *r)
1844 /* Indirect resources don't require activation */
1848 KASSERT(rman_get_flags(r->res) & RF_ACTIVE,
1849 ("RF_ACTIVE not set on direct resource"));
1851 /* Perform deactivation */
1852 error = BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, r->res);
1860 * Slow path for bhndb_io_resource().
1862 * Iterates over the existing allocated dynamic windows looking for a viable
1863 * in-use region; the first matching region is returned.
1865 static struct bhndb_dw_alloc *
1866 bhndb_io_resource_slow(struct bhndb_softc *sc, bus_addr_t addr,
1867 bus_size_t size, bus_size_t *offset)
1869 struct bhndb_resources *br;
1870 struct bhndb_dw_alloc *dwa;
1872 BHNDB_LOCK_ASSERT(sc, MA_OWNED);
1876 /* Search for an existing dynamic mapping of this address range.
1877 * Static regions are not searched, as a statically mapped
1878 * region would never be allocated as an indirect resource. */
1879 for (size_t i = 0; i < br->dwa_count; i++) {
1880 const struct bhndb_regwin *win;
1882 dwa = &br->dw_alloc[i];
1885 KASSERT(win->win_type == BHNDB_REGWIN_T_DYN,
1886 ("invalid register window type"));
1888 /* Verify the range */
1889 if (addr < dwa->target)
1892 if (addr + size > dwa->target + win->win_size)
1896 *offset = dwa->win->win_offset;
1897 *offset += addr - dwa->target;
1907 * Return a borrowed reference to a bridge resource allocation record capable
1908 * of handling bus I/O requests of @p size at @p addr.
1910 * This will either return a reference to an existing allocation
1911 * record mapping the requested space, or will configure and return a free
1912 * allocation record.
1914 * Will panic if a usable record cannot be found.
1916 * @param sc Bridge driver state.
1917 * @param addr The I/O target address.
1918 * @param size The size of the I/O operation to be performed at @p addr.
1919 * @param[out] offset The offset within the returned resource at which
1920 * to perform the I/O request.
1922 static inline struct bhndb_dw_alloc *
1923 bhndb_io_resource(struct bhndb_softc *sc, bus_addr_t addr, bus_size_t size,
1926 struct bhndb_resources *br;
1927 struct bhndb_dw_alloc *dwa;
1930 BHNDB_LOCK_ASSERT(sc, MA_OWNED);
1934 /* Try to fetch a free window */
1935 dwa = bhndb_dw_next_free(br);
1938 * If no dynamic windows are available, look for an existing
1939 * region that maps the target range.
1941 * If none are found, this is a child driver bug -- our window
1942 * over-commit should only fail in the case where a child driver leaks
1943 * resources, or perform operations out-of-order.
1945 * Broadcom HND chipsets are designed to not require register window
1946 * swapping during execution; as long as the child devices are
1947 * attached/detached correctly, using the hardware's required order
1948 * of operations, there should always be a window available for the
1949 * current operation.
1952 dwa = bhndb_io_resource_slow(sc, addr, size, offset);
1954 panic("register windows exhausted attempting to map "
1956 (unsigned long long) addr,
1957 (unsigned long long) addr+size-1);
1963 /* Adjust the window if the I/O request won't fit in the current
1965 if (addr < dwa->target ||
1966 addr > dwa->target + dwa->win->win_size ||
1967 (dwa->target + dwa->win->win_size) - addr < size)
1969 error = bhndb_dw_set_addr(sc->dev, sc->bus_res, dwa, addr,
1972 panic("failed to set register window target mapping "
1974 (unsigned long long) addr,
1975 (unsigned long long) addr+size-1);
1979 /* Calculate the offset and return */
1980 *offset = (addr - dwa->target) + dwa->win->win_offset;
1985 * BHND_BUS_(READ|WRITE_* implementations
1988 /* bhndb_bus_(read|write) common implementation */
1989 #define BHNDB_IO_COMMON_SETUP(_io_size) \
1990 struct bhndb_softc *sc; \
1991 struct bhndb_dw_alloc *dwa; \
1992 struct resource *io_res; \
1993 bus_size_t io_offset; \
1995 sc = device_get_softc(dev); \
1998 dwa = bhndb_io_resource(sc, rman_get_start(r->res) + \
1999 offset, _io_size, &io_offset); \
2000 io_res = dwa->parent_res; \
2002 KASSERT(!r->direct, \
2003 ("bhnd_bus slow path used for direct resource")); \
2005 KASSERT(rman_get_flags(io_res) & RF_ACTIVE, \
2006 ("i/o resource is not active"));
2008 #define BHNDB_IO_COMMON_TEARDOWN() \
2011 /* Defines a bhndb_bus_read_* method implementation */
2012 #define BHNDB_IO_READ(_type, _name) \
2014 bhndb_bus_read_ ## _name (device_t dev, device_t child, \
2015 struct bhnd_resource *r, bus_size_t offset) \
2018 BHNDB_IO_COMMON_SETUP(sizeof(_type)); \
2019 v = bus_read_ ## _name (io_res, io_offset); \
2020 BHNDB_IO_COMMON_TEARDOWN(); \
2025 /* Defines a bhndb_bus_write_* method implementation */
2026 #define BHNDB_IO_WRITE(_type, _name) \
2028 bhndb_bus_write_ ## _name (device_t dev, device_t child, \
2029 struct bhnd_resource *r, bus_size_t offset, _type value) \
2031 BHNDB_IO_COMMON_SETUP(sizeof(_type)); \
2032 bus_write_ ## _name (io_res, io_offset, value); \
2033 BHNDB_IO_COMMON_TEARDOWN(); \
2036 /* Defines a bhndb_bus_(read|write|set)_(multi|region)_* method */
2037 #define BHNDB_IO_MISC(_type, _ptr, _op, _size) \
2039 bhndb_bus_ ## _op ## _ ## _size (device_t dev, \
2040 device_t child, struct bhnd_resource *r, bus_size_t offset, \
2041 _type _ptr datap, bus_size_t count) \
2043 BHNDB_IO_COMMON_SETUP(sizeof(_type) * count); \
2044 bus_ ## _op ## _ ## _size (io_res, io_offset, \
2046 BHNDB_IO_COMMON_TEARDOWN(); \
2049 /* Defines a complete set of read/write methods */
2050 #define BHNDB_IO_METHODS(_type, _size) \
2051 BHNDB_IO_READ(_type, _size) \
2052 BHNDB_IO_WRITE(_type, _size) \
2054 BHNDB_IO_READ(_type, stream_ ## _size) \
2055 BHNDB_IO_WRITE(_type, stream_ ## _size) \
2057 BHNDB_IO_MISC(_type, *, read_multi, _size) \
2058 BHNDB_IO_MISC(_type, *, write_multi, _size) \
2060 BHNDB_IO_MISC(_type, *, read_multi_stream, _size) \
2061 BHNDB_IO_MISC(_type, *, write_multi_stream, _size) \
2063 BHNDB_IO_MISC(_type, , set_multi, _size) \
2064 BHNDB_IO_MISC(_type, , set_region, _size) \
2065 BHNDB_IO_MISC(_type, *, read_region, _size) \
2066 BHNDB_IO_MISC(_type, *, write_region, _size) \
2068 BHNDB_IO_MISC(_type, *, read_region_stream, _size) \
2069 BHNDB_IO_MISC(_type, *, write_region_stream, _size)
2071 BHNDB_IO_METHODS(uint8_t, 1);
2072 BHNDB_IO_METHODS(uint16_t, 2);
2073 BHNDB_IO_METHODS(uint32_t, 4);
2076 * Default bhndb(4) implementation of BHND_BUS_BARRIER().
2079 bhndb_bus_barrier(device_t dev, device_t child, struct bhnd_resource *r,
2080 bus_size_t offset, bus_size_t length, int flags)
2082 BHNDB_IO_COMMON_SETUP(length);
2084 bus_barrier(io_res, io_offset + offset, length, flags);
2086 BHNDB_IO_COMMON_TEARDOWN();
2090 * Default bhndb(4) implementation of BUS_GET_DMA_TAG().
2092 static bus_dma_tag_t
2093 bhndb_get_dma_tag(device_t dev, device_t child)
2099 static device_method_t bhndb_methods[] = {
2100 /* Device interface */ \
2101 DEVMETHOD(device_probe, bhndb_generic_probe),
2102 DEVMETHOD(device_detach, bhndb_generic_detach),
2103 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2104 DEVMETHOD(device_suspend, bhndb_generic_suspend),
2105 DEVMETHOD(device_resume, bhndb_generic_resume),
2108 DEVMETHOD(bus_probe_nomatch, bhndb_probe_nomatch),
2109 DEVMETHOD(bus_print_child, bhndb_print_child),
2110 DEVMETHOD(bus_child_pnpinfo_str, bhndb_child_pnpinfo_str),
2111 DEVMETHOD(bus_child_location_str, bhndb_child_location_str),
2112 DEVMETHOD(bus_add_child, bhndb_add_child),
2113 DEVMETHOD(bus_child_deleted, bhndb_child_deleted),
2115 DEVMETHOD(bus_alloc_resource, bhndb_alloc_resource),
2116 DEVMETHOD(bus_release_resource, bhndb_release_resource),
2117 DEVMETHOD(bus_activate_resource, bhndb_activate_resource),
2118 DEVMETHOD(bus_deactivate_resource, bhndb_deactivate_resource),
2120 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
2121 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
2122 DEVMETHOD(bus_config_intr, bus_generic_config_intr),
2123 DEVMETHOD(bus_bind_intr, bus_generic_bind_intr),
2124 DEVMETHOD(bus_describe_intr, bus_generic_describe_intr),
2126 DEVMETHOD(bus_get_dma_tag, bhndb_get_dma_tag),
2128 DEVMETHOD(bus_adjust_resource, bhndb_adjust_resource),
2129 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
2130 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
2131 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
2132 DEVMETHOD(bus_get_resource_list, bhndb_get_resource_list),
2134 DEVMETHOD(bus_read_ivar, bhndb_read_ivar),
2135 DEVMETHOD(bus_write_ivar, bhndb_write_ivar),
2137 /* BHNDB interface */
2138 DEVMETHOD(bhndb_get_chipid, bhndb_get_chipid),
2139 DEVMETHOD(bhndb_is_core_disabled, bhndb_is_core_disabled),
2140 DEVMETHOD(bhndb_get_hostb_core, bhndb_get_hostb_core),
2141 DEVMETHOD(bhndb_suspend_resource, bhndb_suspend_resource),
2142 DEVMETHOD(bhndb_resume_resource, bhndb_resume_resource),
2144 /* BHND interface */
2145 DEVMETHOD(bhnd_bus_get_chipid, bhndb_get_chipid),
2146 DEVMETHOD(bhnd_bus_activate_resource, bhndb_activate_bhnd_resource),
2147 DEVMETHOD(bhnd_bus_deactivate_resource, bhndb_deactivate_bhnd_resource),
2148 DEVMETHOD(bhnd_bus_get_nvram_var, bhnd_bus_generic_get_nvram_var),
2149 DEVMETHOD(bhnd_bus_read_1, bhndb_bus_read_1),
2150 DEVMETHOD(bhnd_bus_read_2, bhndb_bus_read_2),
2151 DEVMETHOD(bhnd_bus_read_4, bhndb_bus_read_4),
2152 DEVMETHOD(bhnd_bus_write_1, bhndb_bus_write_1),
2153 DEVMETHOD(bhnd_bus_write_2, bhndb_bus_write_2),
2154 DEVMETHOD(bhnd_bus_write_4, bhndb_bus_write_4),
2156 DEVMETHOD(bhnd_bus_read_stream_1, bhndb_bus_read_stream_1),
2157 DEVMETHOD(bhnd_bus_read_stream_2, bhndb_bus_read_stream_2),
2158 DEVMETHOD(bhnd_bus_read_stream_4, bhndb_bus_read_stream_4),
2159 DEVMETHOD(bhnd_bus_write_stream_1, bhndb_bus_write_stream_1),
2160 DEVMETHOD(bhnd_bus_write_stream_2, bhndb_bus_write_stream_2),
2161 DEVMETHOD(bhnd_bus_write_stream_4, bhndb_bus_write_stream_4),
2163 DEVMETHOD(bhnd_bus_read_multi_1, bhndb_bus_read_multi_1),
2164 DEVMETHOD(bhnd_bus_read_multi_2, bhndb_bus_read_multi_2),
2165 DEVMETHOD(bhnd_bus_read_multi_4, bhndb_bus_read_multi_4),
2166 DEVMETHOD(bhnd_bus_write_multi_1, bhndb_bus_write_multi_1),
2167 DEVMETHOD(bhnd_bus_write_multi_2, bhndb_bus_write_multi_2),
2168 DEVMETHOD(bhnd_bus_write_multi_4, bhndb_bus_write_multi_4),
2170 DEVMETHOD(bhnd_bus_read_multi_stream_1, bhndb_bus_read_multi_stream_1),
2171 DEVMETHOD(bhnd_bus_read_multi_stream_2, bhndb_bus_read_multi_stream_2),
2172 DEVMETHOD(bhnd_bus_read_multi_stream_4, bhndb_bus_read_multi_stream_4),
2173 DEVMETHOD(bhnd_bus_write_multi_stream_1,bhndb_bus_write_multi_stream_1),
2174 DEVMETHOD(bhnd_bus_write_multi_stream_2,bhndb_bus_write_multi_stream_2),
2175 DEVMETHOD(bhnd_bus_write_multi_stream_4,bhndb_bus_write_multi_stream_4),
2177 DEVMETHOD(bhnd_bus_set_multi_1, bhndb_bus_set_multi_1),
2178 DEVMETHOD(bhnd_bus_set_multi_2, bhndb_bus_set_multi_2),
2179 DEVMETHOD(bhnd_bus_set_multi_4, bhndb_bus_set_multi_4),
2180 DEVMETHOD(bhnd_bus_set_region_1, bhndb_bus_set_region_1),
2181 DEVMETHOD(bhnd_bus_set_region_2, bhndb_bus_set_region_2),
2182 DEVMETHOD(bhnd_bus_set_region_4, bhndb_bus_set_region_4),
2184 DEVMETHOD(bhnd_bus_read_region_1, bhndb_bus_read_region_1),
2185 DEVMETHOD(bhnd_bus_read_region_2, bhndb_bus_read_region_2),
2186 DEVMETHOD(bhnd_bus_read_region_4, bhndb_bus_read_region_4),
2187 DEVMETHOD(bhnd_bus_write_region_1, bhndb_bus_write_region_1),
2188 DEVMETHOD(bhnd_bus_write_region_2, bhndb_bus_write_region_2),
2189 DEVMETHOD(bhnd_bus_write_region_4, bhndb_bus_write_region_4),
2191 DEVMETHOD(bhnd_bus_read_region_stream_1,bhndb_bus_read_region_stream_1),
2192 DEVMETHOD(bhnd_bus_read_region_stream_2,bhndb_bus_read_region_stream_2),
2193 DEVMETHOD(bhnd_bus_read_region_stream_4,bhndb_bus_read_region_stream_4),
2194 DEVMETHOD(bhnd_bus_write_region_stream_1,bhndb_bus_write_region_stream_1),
2195 DEVMETHOD(bhnd_bus_write_region_stream_2,bhndb_bus_write_region_stream_2),
2196 DEVMETHOD(bhnd_bus_write_region_stream_4,bhndb_bus_write_region_stream_4),
2198 DEVMETHOD(bhnd_bus_barrier, bhndb_bus_barrier),
2203 devclass_t bhndb_devclass;
2205 DEFINE_CLASS_0(bhndb, bhndb_driver, bhndb_methods, sizeof(struct bhndb_softc));
2207 MODULE_VERSION(bhndb, 1);
2208 MODULE_DEPEND(bhndb, bhnd, 1, 1, 1);