2 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3 * Copyright (c) 2017 The FreeBSD Foundation
6 * Portions of this software were developed by Landon Fuller
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
17 * redistribution must be conditioned upon including a substantially
18 * similar Disclaimer requirement for further binary redistribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
24 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
26 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGES.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 * Abstract BHND Bridge Device Driver
40 * Provides generic support for bridging from a parent bus (such as PCI) to
41 * a BHND-compatible bus (e.g. bcma or siba).
44 #include <sys/param.h>
45 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/systm.h>
50 #include <machine/bus.h>
52 #include <machine/resource.h>
54 #include <dev/bhnd/bhndvar.h>
55 #include <dev/bhnd/bhndreg.h>
57 #include <dev/bhnd/bhnd_erom.h>
59 #include <dev/bhnd/cores/chipc/chipcreg.h>
60 #include <dev/bhnd/nvram/bhnd_nvram.h>
62 #include "bhnd_chipc_if.h"
63 #include "bhnd_nvram_if.h"
66 #include "bhndb_bus_if.h"
67 #include "bhndb_hwdata.h"
68 #include "bhndb_private.h"
71 static u_long bhndb_debug = 0;
72 TUNABLE_ULONG("hw.bhndb.debug", &bhndb_debug);
75 BHNDB_DEBUG_PRIO = 1 << 0,
78 #define BHNDB_DEBUG(_type) (BHNDB_DEBUG_ ## _type & bhndb_debug)
80 static bool bhndb_hw_matches(struct bhndb_softc *sc,
81 struct bhnd_core_info *cores, u_int ncores,
82 const struct bhndb_hw *hw);
84 static int bhndb_init_region_cfg(struct bhndb_softc *sc,
86 struct bhndb_resources *r,
87 struct bhnd_core_info *cores, u_int ncores,
88 const struct bhndb_hw_priority *table);
90 static int bhndb_find_hwspec(struct bhndb_softc *sc,
91 struct bhnd_core_info *cores, u_int ncores,
92 const struct bhndb_hw **hw);
94 bhndb_addrspace bhndb_get_addrspace(struct bhndb_softc *sc,
97 static struct rman *bhndb_get_rman(struct bhndb_softc *sc,
98 device_t child, int type);
100 static int bhndb_init_child_resource(struct resource *r,
101 struct resource *parent,
105 static int bhndb_activate_static_region(
106 struct bhndb_softc *sc,
107 struct bhndb_region *region,
108 device_t child, int type, int rid,
111 static int bhndb_try_activate_resource(
112 struct bhndb_softc *sc, device_t child,
113 int type, int rid, struct resource *r,
116 static inline struct bhndb_dw_alloc *bhndb_io_resource(struct bhndb_softc *sc,
117 bus_addr_t addr, bus_size_t size,
118 bus_size_t *offset, bool *stolen,
119 bus_addr_t *restore);
122 * Default bhndb(4) implementation of DEVICE_PROBE().
124 * This function provides the default bhndb implementation of DEVICE_PROBE(),
125 * and is compatible with bhndb(4) bridges attached via bhndb_attach_bridge().
128 bhndb_generic_probe(device_t dev)
130 return (BUS_PROBE_NOWILDCARD);
134 bhndb_probe_nomatch(device_t dev, device_t child)
138 name = device_get_name(child);
140 name = "unknown device";
142 device_printf(dev, "<%s> (no driver attached)\n", name);
146 bhndb_print_child(device_t dev, device_t child)
148 struct bhndb_softc *sc;
149 struct resource_list *rl;
152 sc = device_get_softc(dev);
154 retval += bus_print_child_header(dev, child);
156 rl = BUS_GET_RESOURCE_LIST(dev, child);
158 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY,
160 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ,
164 retval += bus_print_child_domain(dev, child);
165 retval += bus_print_child_footer(dev, child);
171 bhndb_child_pnpinfo_str(device_t bus, device_t child, char *buf,
179 bhndb_child_location_str(device_t dev, device_t child, char *buf,
182 struct bhndb_softc *sc;
184 sc = device_get_softc(dev);
186 snprintf(buf, buflen, "base=0x%llx",
187 (unsigned long long) sc->chipid.enum_addr);
192 * Return true if @p cores matches the @p hw specification.
194 * @param sc BHNDB device state.
195 * @param cores A device table to match against.
196 * @param ncores The number of cores in @p cores.
197 * @param hw The hardware description to be matched against.
200 bhndb_hw_matches(struct bhndb_softc *sc, struct bhnd_core_info *cores,
201 u_int ncores, const struct bhndb_hw *hw)
203 for (u_int i = 0; i < hw->num_hw_reqs; i++) {
204 const struct bhnd_core_match *match;
207 match = &hw->hw_reqs[i];
210 for (u_int d = 0; d < ncores; d++) {
211 struct bhnd_core_info *core = &cores[d];
213 if (BHNDB_IS_CORE_DISABLED(sc->dev, sc->bus_dev, core))
216 if (!bhnd_core_matches(core, match))
231 * Initialize the region maps and priority configuration in @p br using
232 * the priority @p table and the set of cores enumerated by @p erom.
234 * @param sc The bhndb device state.
235 * @param br The resource state to be configured.
236 * @param erom EROM parser used to enumerate @p cores.
237 * @param cores All cores enumerated on the bridged bhnd bus.
238 * @param ncores The length of @p cores.
239 * @param table Hardware priority table to be used to determine the relative
240 * priorities of per-core port resources.
243 bhndb_init_region_cfg(struct bhndb_softc *sc, bhnd_erom_t *erom,
244 struct bhndb_resources *br, struct bhnd_core_info *cores, u_int ncores,
245 const struct bhndb_hw_priority *table)
247 const struct bhndb_hw_priority *hp;
250 size_t prio_low, prio_default, prio_high;
253 /* The number of port regions per priority band that must be accessible
254 * via dynamic register windows */
260 * Register bridge regions covering all statically mapped ports.
262 for (u_int i = 0; i < ncores; i++) {
263 const struct bhndb_regwin *regw;
264 struct bhnd_core_info *core;
265 struct bhnd_core_match md;
268 md = bhnd_core_get_match_desc(core);
270 for (regw = br->cfg->register_windows;
271 regw->win_type != BHNDB_REGWIN_T_INVALID; regw++)
273 const struct bhndb_port_priority *pp;
274 uint32_t alloc_flags;
276 /* Only core windows are supported */
277 if (regw->win_type != BHNDB_REGWIN_T_CORE)
280 /* Skip non-matching cores. */
281 if (!bhndb_regwin_match_core(regw, core))
284 /* Fetch the base address of the mapped port */
285 error = bhnd_erom_lookup_core_addr(erom, &md,
286 regw->d.core.port_type,
293 /* Skip non-applicable register windows */
301 * Apply the register window's region offset, if any.
303 if (regw->d.core.offset > size) {
304 device_printf(sc->dev, "invalid register "
305 "window offset %#jx for region %#jx+%#jx\n",
306 regw->d.core.offset, addr, size);
310 addr += regw->d.core.offset;
313 * Always defer to the register window's size.
315 * If the port size is smaller than the window size,
316 * this ensures that we fully utilize register windows
317 * larger than the referenced port.
319 * If the port size is larger than the window size, this
320 * ensures that we do not directly map the allocations
321 * within the region to a too-small window.
323 size = regw->win_size;
325 /* Fetch allocation flags from the corresponding port
326 * priority entry, if any */
327 pp = bhndb_hw_priorty_find_port(table, core,
328 regw->d.core.port_type, regw->d.core.port,
329 regw->d.core.region);
331 alloc_flags = pp->alloc_flags;
337 * Add to the bus region list.
339 * The window priority for a statically mapped region is
342 error = bhndb_add_resource_region(br, addr, size,
343 BHNDB_PRIORITY_HIGH, alloc_flags, regw);
350 * Perform priority accounting and register bridge regions for all
351 * ports defined in the priority table
353 for (u_int i = 0; i < ncores; i++) {
354 struct bhnd_core_info *core;
355 struct bhnd_core_match md;
358 md = bhnd_core_get_match_desc(core);
361 * Skip priority accounting for cores that ...
364 /* ... do not require bridge resources */
365 if (BHNDB_IS_CORE_DISABLED(sc->dev, sc->bus_dev, core))
368 /* ... do not have a priority table entry */
369 hp = bhndb_hw_priority_find_core(table, core);
373 /* ... are explicitly disabled in the priority table. */
374 if (hp->priority == BHNDB_PRIORITY_NONE)
377 /* Determine the number of dynamic windows required and
378 * register their bus_region entries. */
379 for (u_int i = 0; i < hp->num_ports; i++) {
380 const struct bhndb_port_priority *pp;
384 /* Fetch the address+size of the mapped port. */
385 error = bhnd_erom_lookup_core_addr(erom, &md,
386 pp->type, pp->port, pp->region,
389 /* Skip ports not defined on this device */
396 /* Skip ports with an existing static mapping */
397 if (bhndb_has_static_region_mapping(br, addr, size))
400 /* Define a dynamic region for this port */
401 error = bhndb_add_resource_region(br, addr, size,
402 pp->priority, pp->alloc_flags, NULL);
406 /* Update port mapping counts */
407 switch (pp->priority) {
408 case BHNDB_PRIORITY_NONE:
410 case BHNDB_PRIORITY_LOW:
413 case BHNDB_PRIORITY_DEFAULT:
416 case BHNDB_PRIORITY_HIGH:
423 /* Determine the minimum priority at which we'll allocate direct
424 * register windows from our dynamic pool */
425 size_t prio_total = prio_low + prio_default + prio_high;
426 if (prio_total <= br->dwa_count) {
427 /* low+default+high priority regions get windows */
428 br->min_prio = BHNDB_PRIORITY_LOW;
430 } else if (prio_default + prio_high <= br->dwa_count) {
431 /* default+high priority regions get windows */
432 br->min_prio = BHNDB_PRIORITY_DEFAULT;
435 /* high priority regions get windows */
436 br->min_prio = BHNDB_PRIORITY_HIGH;
439 if (BHNDB_DEBUG(PRIO)) {
440 struct bhndb_region *region;
441 const char *direct_msg, *type_msg;
442 bhndb_priority_t prio, prio_min;
445 prio_min = br->min_prio;
446 device_printf(sc->dev, "min_prio: %d\n", prio_min);
448 STAILQ_FOREACH(region, &br->bus_regions, link) {
449 prio = region->priority;
450 flags = region->alloc_flags;
452 direct_msg = prio >= prio_min ? "direct" : "indirect";
453 type_msg = region->static_regwin ? "static" : "dynamic";
455 device_printf(sc->dev, "region 0x%llx+0x%llx priority "
457 (unsigned long long) region->addr,
458 (unsigned long long) region->size,
460 direct_msg, type_msg);
462 if (flags & BHNDB_ALLOC_FULFILL_ON_OVERCOMMIT)
463 printf(" [overcommit]\n");
473 * Find a hardware specification for @p dev.
475 * @param sc The bhndb device state.
476 * @param cores All cores enumerated on the bridged bhnd bus.
477 * @param ncores The length of @p cores.
478 * @param[out] hw On success, the matched hardware specification.
482 * @retval non-zero if an error occurs fetching device info for comparison.
485 bhndb_find_hwspec(struct bhndb_softc *sc, struct bhnd_core_info *cores,
486 u_int ncores, const struct bhndb_hw **hw)
488 const struct bhndb_hw *next, *hw_table;
490 /* Search for the first matching hardware config. */
491 hw_table = BHNDB_BUS_GET_HARDWARE_TABLE(sc->parent_dev, sc->dev);
492 for (next = hw_table; next->hw_reqs != NULL; next++) {
493 if (!bhndb_hw_matches(sc, cores, ncores, next))
505 * Helper function that must be called by subclass bhndb(4) drivers
506 * when implementing DEVICE_ATTACH() before calling any bhnd(4) or bhndb(4)
507 * APIs on the bridge device.
509 * This function will add a bridged bhnd(4) child device with a device order of
510 * BHND_PROBE_BUS. Any subclass bhndb(4) driver may use the BHND_PROBE_*
511 * priority bands to add additional devices that will be attached in
512 * their preferred order relative to the bridged bhnd(4) bus.
514 * @param dev The bridge device to attach.
515 * @param cid The bridged device's chip identification.
516 * @param cores The bridged device's core table.
517 * @param ncores The number of cores in @p cores.
518 * @param bridge_core Core info for the bhnd(4) core serving as the host
520 * @param erom_class An erom parser class that may be used to parse
521 * the bridged device's device enumeration table.
524 bhndb_attach(device_t dev, struct bhnd_chipid *cid,
525 struct bhnd_core_info *cores, u_int ncores,
526 struct bhnd_core_info *bridge_core, bhnd_erom_class_t *erom_class)
528 struct bhndb_devinfo *dinfo;
529 struct bhndb_softc *sc;
530 const struct bhndb_hw *hw;
531 const struct bhndb_hwcfg *hwcfg;
532 const struct bhndb_hw_priority *hwprio;
533 struct bhnd_erom_io *eio;
537 sc = device_get_softc(dev);
539 sc->parent_dev = device_get_parent(dev);
540 sc->bridge_core = *bridge_core;
543 if ((error = bhnd_service_registry_init(&sc->services)))
550 /* Find a matching bridge hardware configuration */
551 if ((error = bhndb_find_hwspec(sc, cores, ncores, &hw))) {
552 device_printf(sc->dev, "unable to identify device, "
553 " using generic bridge resource definitions\n");
555 hwcfg = BHNDB_BUS_GET_GENERIC_HWCFG(sc->parent_dev, dev);
561 if (hw != NULL && (bootverbose || BHNDB_DEBUG(PRIO))) {
562 device_printf(sc->dev, "%s resource configuration\n", hw->name);
565 /* Allocate bridge resource state using the discovered hardware
567 sc->bus_res = bhndb_alloc_resources(sc->dev, sc->parent_dev, hwcfg);
568 if (sc->bus_res == NULL) {
569 device_printf(sc->dev, "failed to allocate bridge resource "
575 /* Add our bridged bus device */
576 sc->bus_dev = BUS_ADD_CHILD(dev, BHND_PROBE_BUS, "bhnd", -1);
577 if (sc->bus_dev == NULL) {
582 dinfo = device_get_ivars(sc->bus_dev);
583 dinfo->addrspace = BHNDB_ADDRSPACE_BRIDGED;
585 /* We can now use bhndb to perform bridging of SYS_RES_MEMORY resources;
586 * we use this to instantiate an erom parser instance */
587 eio = bhnd_erom_iores_new(sc->bus_dev, 0);
588 if ((erom = bhnd_erom_alloc(erom_class, cid, eio)) == NULL) {
589 bhnd_erom_io_fini(eio);
594 /* Populate our resource priority configuration */
595 hwprio = BHNDB_BUS_GET_HARDWARE_PRIO(sc->parent_dev, sc->dev);
596 error = bhndb_init_region_cfg(sc, erom, sc->bus_res, cores, ncores,
599 device_printf(sc->dev, "failed to initialize resource "
600 "priority configuration: %d\n", error);
604 /* Free our erom instance */
605 bhnd_erom_free(erom);
611 BHNDB_LOCK_DESTROY(sc);
613 if (sc->bus_res != NULL)
614 bhndb_free_resources(sc->bus_res);
617 bhnd_erom_free(erom);
619 bhnd_service_registry_fini(&sc->services);
625 * Default bhndb(4) implementation of DEVICE_DETACH().
627 * This function detaches any child devices, and if successful, releases all
628 * resources held by the bridge device.
631 bhndb_generic_detach(device_t dev)
633 struct bhndb_softc *sc;
636 sc = device_get_softc(dev);
638 /* Detach children */
639 if ((error = bus_generic_detach(dev)))
642 /* Delete children */
643 if ((error = device_delete_children(dev)))
646 /* Clean up our service registry */
647 if ((error = bhnd_service_registry_fini(&sc->services)))
650 /* Clean up our driver state. */
651 bhndb_free_resources(sc->bus_res);
653 BHNDB_LOCK_DESTROY(sc);
659 * Default bhndb(4) implementation of DEVICE_SUSPEND().
661 * This function calls bus_generic_suspend() (or implements equivalent
665 bhndb_generic_suspend(device_t dev)
667 return (bus_generic_suspend(dev));
671 * Default bhndb(4) implementation of DEVICE_RESUME().
673 * This function calls bus_generic_resume() (or implements equivalent
677 bhndb_generic_resume(device_t dev)
679 struct bhndb_softc *sc;
680 struct bhndb_resources *bus_res;
681 struct bhndb_dw_alloc *dwa;
684 sc = device_get_softc(dev);
685 bus_res = sc->bus_res;
687 /* Guarantee that all in-use dynamic register windows are mapped to
688 * their previously configured target address. */
691 for (size_t i = 0; i < bus_res->dwa_count; i++) {
692 dwa = &bus_res->dw_alloc[i];
694 /* Skip regions that were not previously used */
695 if (bhndb_dw_is_free(bus_res, dwa) && dwa->target == 0x0)
698 /* Otherwise, ensure the register window is correct before
699 * any children attempt MMIO */
700 error = BHNDB_SET_WINDOW_ADDR(dev, dwa->win, dwa->target);
706 /* Error restoring hardware state; children cannot be safely resumed */
708 device_printf(dev, "Unable to restore hardware configuration; "
709 "cannot resume: %d\n", error);
713 return (bus_generic_resume(dev));
717 * Default implementation of BHNDB_SUSPEND_RESOURCE.
720 bhndb_suspend_resource(device_t dev, device_t child, int type,
723 struct bhndb_softc *sc;
724 struct bhndb_dw_alloc *dwa;
726 sc = device_get_softc(dev);
728 /* Non-MMIO resources (e.g. IRQs) are handled solely by our parent */
729 if (type != SYS_RES_MEMORY)
733 dwa = bhndb_dw_find_resource(sc->bus_res, r);
739 if (BHNDB_DEBUG(PRIO))
740 device_printf(child, "suspend resource type=%d 0x%jx+0x%jx\n",
741 type, rman_get_start(r), rman_get_size(r));
743 /* Release the resource's window reference */
744 bhndb_dw_release(sc->bus_res, dwa, r);
749 * Default implementation of BHNDB_RESUME_RESOURCE.
752 bhndb_resume_resource(device_t dev, device_t child, int type,
755 struct bhndb_softc *sc;
757 sc = device_get_softc(dev);
759 /* Non-MMIO resources (e.g. IRQs) are handled solely by our parent */
760 if (type != SYS_RES_MEMORY)
763 /* Inactive resources don't require reallocation of bridge resources */
764 if (!(rman_get_flags(r) & RF_ACTIVE))
767 if (BHNDB_DEBUG(PRIO))
768 device_printf(child, "resume resource type=%d 0x%jx+0x%jx\n",
769 type, rman_get_start(r), rman_get_size(r));
771 return (bhndb_try_activate_resource(sc, rman_get_device(r), type,
772 rman_get_rid(r), r, NULL));
776 * Default bhndb(4) implementation of BUS_READ_IVAR().
779 bhndb_read_ivar(device_t dev, device_t child, int index,
786 * Default bhndb(4) implementation of BUS_WRITE_IVAR().
789 bhndb_write_ivar(device_t dev, device_t child, int index,
796 * Return the address space for the given @p child device.
799 bhndb_get_addrspace(struct bhndb_softc *sc, device_t child)
801 struct bhndb_devinfo *dinfo;
804 /* Find the directly attached parent of the requesting device */
806 while (imd_dev != NULL && device_get_parent(imd_dev) != sc->dev)
807 imd_dev = device_get_parent(imd_dev);
810 panic("bhndb address space request for non-child device %s\n",
811 device_get_nameunit(child));
813 dinfo = device_get_ivars(imd_dev);
814 return (dinfo->addrspace);
818 * Return the rman instance for a given resource @p type, if any.
820 * @param sc The bhndb device state.
821 * @param child The requesting child.
822 * @param type The resource type (e.g. SYS_RES_MEMORY, SYS_RES_IRQ, ...)
825 bhndb_get_rman(struct bhndb_softc *sc, device_t child, int type)
827 switch (bhndb_get_addrspace(sc, child)) {
828 case BHNDB_ADDRSPACE_NATIVE:
831 return (&sc->bus_res->ht_mem_rman);
838 case BHNDB_ADDRSPACE_BRIDGED:
841 return (&sc->bus_res->br_mem_rman);
843 return (&sc->bus_res->br_irq_rman);
854 * Default implementation of BUS_ADD_CHILD()
857 bhndb_add_child(device_t dev, u_int order, const char *name, int unit)
859 struct bhndb_devinfo *dinfo;
862 child = device_add_child_ordered(dev, order, name, unit);
866 dinfo = malloc(sizeof(struct bhndb_devinfo), M_BHND, M_NOWAIT);
868 device_delete_child(dev, child);
872 dinfo->addrspace = BHNDB_ADDRSPACE_NATIVE;
873 resource_list_init(&dinfo->resources);
875 device_set_ivars(child, dinfo);
881 * Default implementation of BUS_CHILD_DELETED().
884 bhndb_child_deleted(device_t dev, device_t child)
886 struct bhndb_devinfo *dinfo = device_get_ivars(child);
888 resource_list_free(&dinfo->resources);
892 device_set_ivars(child, NULL);
896 * Default implementation of BHNDB_GET_CHIPID().
898 static const struct bhnd_chipid *
899 bhndb_get_chipid(device_t dev, device_t child)
901 struct bhndb_softc *sc = device_get_softc(dev);
902 return (&sc->chipid);
906 * Default implementation of BHNDB_IS_CORE_DISABLED().
909 bhndb_is_core_disabled(device_t dev, device_t child,
910 struct bhnd_core_info *core)
912 struct bhndb_softc *sc;
914 sc = device_get_softc(dev);
916 /* Try to defer to the bhndb bus parent */
917 if (BHNDB_BUS_IS_CORE_DISABLED(sc->parent_dev, dev, core))
920 /* Otherwise, we treat bridge-capable cores as unpopulated if they're
921 * not the configured host bridge */
922 if (BHND_DEVCLASS_SUPPORTS_HOSTB(bhnd_core_class(core)))
923 return (!bhnd_cores_equal(core, &sc->bridge_core));
925 /* Assume the core is populated */
930 * Default bhndb(4) implementation of BHNDB_GET_HOSTB_CORE().
932 * This function uses a heuristic valid on all known PCI/PCIe/PCMCIA-bridged
936 bhndb_get_hostb_core(device_t dev, device_t child, struct bhnd_core_info *core)
938 struct bhndb_softc *sc = device_get_softc(dev);
940 *core = sc->bridge_core;
945 * Default bhndb(4) implementation of BHND_BUS_GET_SERVICE_REGISTRY().
947 static struct bhnd_service_registry *
948 bhndb_get_service_registry(device_t dev, device_t child)
950 struct bhndb_softc *sc = device_get_softc(dev);
952 return (&sc->services);
956 * Default bhndb(4) implementation of BUS_ALLOC_RESOURCE().
958 static struct resource *
959 bhndb_alloc_resource(device_t dev, device_t child, int type,
960 int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
962 struct bhndb_softc *sc;
963 struct resource_list_entry *rle;
967 bool passthrough, isdefault;
969 sc = device_get_softc(dev);
970 passthrough = (device_get_parent(child) != dev);
971 isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
974 /* Fetch the resource manager */
975 rm = bhndb_get_rman(sc, child, type);
977 /* Delegate to our parent device's bus; the requested
978 * resource type isn't handled locally. */
979 return (BUS_ALLOC_RESOURCE(device_get_parent(sc->parent_dev),
980 child, type, rid, start, end, count, flags));
983 /* Populate defaults */
984 if (!passthrough && isdefault) {
985 /* Fetch the resource list entry. */
986 rle = resource_list_find(BUS_GET_RESOURCE_LIST(dev, child),
990 "default resource %#x type %d for child %s "
991 "not found\n", *rid, type,
992 device_get_nameunit(child));
997 if (rle->res != NULL) {
999 "resource entry %#x type %d for child %s is busy\n",
1000 *rid, type, device_get_nameunit(child));
1007 count = ulmax(count, rle->count);
1010 /* Validate resource addresses */
1011 if (start > end || count > ((end - start) + 1))
1014 /* Make our reservation */
1015 rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
1020 rman_set_rid(rv, *rid);
1023 if (flags & RF_ACTIVE) {
1024 error = bus_activate_resource(child, type, *rid, rv);
1027 "failed to activate entry %#x type %d for "
1029 *rid, type, device_get_nameunit(child), error);
1031 rman_release_resource(rv);
1037 /* Update child's resource list entry */
1040 rle->start = rman_get_start(rv);
1041 rle->end = rman_get_end(rv);
1042 rle->count = rman_get_size(rv);
1049 * Default bhndb(4) implementation of BUS_RELEASE_RESOURCE().
1052 bhndb_release_resource(device_t dev, device_t child, int type, int rid,
1055 struct bhndb_softc *sc;
1056 struct resource_list_entry *rle;
1060 sc = device_get_softc(dev);
1061 passthrough = (device_get_parent(child) != dev);
1063 /* Delegate to our parent device's bus if the requested resource type
1064 * isn't handled locally. */
1065 if (bhndb_get_rman(sc, child, type) == NULL) {
1066 return (BUS_RELEASE_RESOURCE(device_get_parent(sc->parent_dev),
1067 child, type, rid, r));
1070 /* Deactivate resources */
1071 if (rman_get_flags(r) & RF_ACTIVE) {
1072 error = BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, r);
1077 if ((error = rman_release_resource(r)))
1081 /* Clean resource list entry */
1082 rle = resource_list_find(BUS_GET_RESOURCE_LIST(dev, child),
1092 * Default bhndb(4) implementation of BUS_ADJUST_RESOURCE().
1095 bhndb_adjust_resource(device_t dev, device_t child, int type,
1096 struct resource *r, rman_res_t start, rman_res_t end)
1098 struct bhndb_softc *sc;
1100 rman_res_t mstart, mend;
1103 sc = device_get_softc(dev);
1106 /* Delegate to our parent device's bus if the requested resource type
1107 * isn't handled locally. */
1108 rm = bhndb_get_rman(sc, child, type);
1110 return (BUS_ADJUST_RESOURCE(device_get_parent(sc->parent_dev),
1111 child, type, r, start, end));
1114 /* Verify basic constraints */
1118 if (!rman_is_region_manager(r, rm))
1123 /* If not active, allow any range permitted by the resource manager */
1124 if (!(rman_get_flags(r) & RF_ACTIVE))
1127 /* Otherwise, the range is limited by the bridged resource mapping */
1128 error = bhndb_find_resource_limits(sc->bus_res, type, r, &mstart,
1133 if (start < mstart || end > mend) {
1141 error = rman_adjust_resource(r, start, end);
1148 * Initialize child resource @p r with a virtual address, tag, and handle
1149 * copied from @p parent, adjusted to contain only the range defined by
1150 * @p offsize and @p size.
1152 * @param r The register to be initialized.
1153 * @param parent The parent bus resource that fully contains the subregion.
1154 * @param offset The subregion offset within @p parent.
1155 * @param size The subregion size.
1159 bhndb_init_child_resource(struct resource *r,
1160 struct resource *parent, bhnd_size_t offset, bhnd_size_t size)
1162 bus_space_handle_t bh, child_bh;
1167 /* Fetch the parent resource's real bus values */
1168 vaddr = (uintptr_t) rman_get_virtual(parent);
1169 bt = rman_get_bustag(parent);
1170 bh = rman_get_bushandle(parent);
1172 /* Configure child resource with window-adjusted real bus values */
1174 error = bus_space_subregion(bt, bh, offset, size, &child_bh);
1178 rman_set_virtual(r, (void *) vaddr);
1179 rman_set_bustag(r, bt);
1180 rman_set_bushandle(r, child_bh);
1186 * Attempt activation of a fixed register window mapping for @p child.
1188 * @param sc BHNDB device state.
1189 * @param region The static region definition capable of mapping @p r.
1190 * @param child A child requesting resource activation.
1191 * @param type Resource type.
1192 * @param rid Resource identifier.
1193 * @param r Resource to be activated.
1195 * @retval 0 if @p r was activated successfully
1196 * @retval ENOENT if no fixed register window was found.
1197 * @retval non-zero if @p r could not be activated.
1200 bhndb_activate_static_region(struct bhndb_softc *sc,
1201 struct bhndb_region *region, device_t child, int type, int rid,
1204 struct resource *bridge_res;
1205 const struct bhndb_regwin *win;
1206 bhnd_size_t parent_offset;
1207 rman_res_t r_start, r_size;
1210 win = region->static_regwin;
1212 KASSERT(win != NULL && BHNDB_REGWIN_T_IS_STATIC(win->win_type),
1213 ("can't activate non-static region"));
1215 r_start = rman_get_start(r);
1216 r_size = rman_get_size(r);
1218 /* Find the corresponding bridge resource */
1219 bridge_res = bhndb_host_resource_for_regwin(sc->bus_res->res, win);
1220 if (bridge_res == NULL)
1223 /* Calculate subregion offset within the parent resource */
1224 parent_offset = r_start - region->addr;
1225 parent_offset += win->win_offset;
1227 /* Configure resource with its real bus values. */
1228 error = bhndb_init_child_resource(r, bridge_res, parent_offset, r_size);
1233 if ((error = rman_activate_resource(r)))
1240 * Attempt to allocate/retain a dynamic register window for @p r, returning
1241 * the retained window.
1243 * @param sc The bhndb driver state.
1244 * @param r The resource for which a window will be retained.
1246 static struct bhndb_dw_alloc *
1247 bhndb_retain_dynamic_window(struct bhndb_softc *sc, struct resource *r)
1249 struct bhndb_dw_alloc *dwa;
1250 rman_res_t r_start, r_size;
1253 BHNDB_LOCK_ASSERT(sc, MA_OWNED);
1255 r_start = rman_get_start(r);
1256 r_size = rman_get_size(r);
1258 /* Look for an existing dynamic window we can reference */
1259 dwa = bhndb_dw_find_mapping(sc->bus_res, r_start, r_size);
1261 if (bhndb_dw_retain(sc->bus_res, dwa, r) == 0)
1267 /* Otherwise, try to reserve a free window */
1268 dwa = bhndb_dw_next_free(sc->bus_res);
1270 /* No free windows */
1274 /* Window must be large enough to map the entire resource */
1275 if (dwa->win->win_size < rman_get_size(r))
1278 /* Set the window target */
1279 error = bhndb_dw_set_addr(sc->dev, sc->bus_res, dwa, rman_get_start(r),
1282 device_printf(sc->dev, "dynamic window initialization "
1283 "for 0x%llx-0x%llx failed: %d\n",
1284 (unsigned long long) r_start,
1285 (unsigned long long) r_start + r_size - 1,
1290 /* Add our reservation */
1291 if (bhndb_dw_retain(sc->bus_res, dwa, r))
1298 * Activate a resource using any viable static or dynamic register window.
1300 * @param sc The bhndb driver state.
1301 * @param child The child holding ownership of @p r.
1302 * @param type The type of the resource to be activated.
1303 * @param rid The resource ID of @p r.
1304 * @param r The resource to be activated
1305 * @param[out] indirect On error and if not NULL, will be set to 'true' if
1306 * the caller should instead use an indirect resource mapping.
1309 * @retval non-zero activation failed.
1312 bhndb_try_activate_resource(struct bhndb_softc *sc, device_t child, int type,
1313 int rid, struct resource *r, bool *indirect)
1315 struct bhndb_region *region;
1316 struct bhndb_dw_alloc *dwa;
1317 bhndb_priority_t dw_priority;
1318 rman_res_t r_start, r_size;
1319 rman_res_t parent_offset;
1322 BHNDB_LOCK_ASSERT(sc, MA_NOTOWNED);
1324 if (indirect != NULL)
1329 /* IRQ resources are always directly mapped */
1330 return (rman_activate_resource(r));
1332 case SYS_RES_MEMORY:
1337 device_printf(sc->dev, "unsupported resource type %d\n", type);
1341 /* Only MMIO resources can be mapped via register windows */
1342 KASSERT(type == SYS_RES_MEMORY, ("invalid type: %d", type));
1344 r_start = rman_get_start(r);
1345 r_size = rman_get_size(r);
1347 /* Activate native addrspace resources using the host address space */
1348 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_NATIVE) {
1349 struct resource *parent;
1351 /* Find the bridge resource referenced by the child */
1352 parent = bhndb_host_resource_for_range(sc->bus_res->res,
1353 type, r_start, r_size);
1354 if (parent == NULL) {
1355 device_printf(sc->dev, "host resource not found "
1356 "for 0x%llx-0x%llx\n",
1357 (unsigned long long) r_start,
1358 (unsigned long long) r_start + r_size - 1);
1362 /* Initialize child resource with the real bus values */
1363 error = bhndb_init_child_resource(r, parent,
1364 r_start - rman_get_start(parent), r_size);
1368 /* Try to activate child resource */
1369 return (rman_activate_resource(r));
1372 /* Default to low priority */
1373 dw_priority = BHNDB_PRIORITY_LOW;
1375 /* Look for a bus region matching the resource's address range */
1376 region = bhndb_find_resource_region(sc->bus_res, r_start, r_size);
1378 dw_priority = region->priority;
1380 /* Prefer static mappings over consuming a dynamic windows. */
1381 if (region && region->static_regwin) {
1382 error = bhndb_activate_static_region(sc, region, child, type,
1385 device_printf(sc->dev, "static window allocation "
1386 "for 0x%llx-0x%llx failed\n",
1387 (unsigned long long) r_start,
1388 (unsigned long long) r_start + r_size - 1);
1392 /* A dynamic window will be required; is this resource high enough
1393 * priority to be reserved a dynamic window? */
1394 if (dw_priority < sc->bus_res->min_prio) {
1401 /* Find and retain a usable window */
1403 dwa = bhndb_retain_dynamic_window(sc, r);
1412 /* Configure resource with its real bus values. */
1413 parent_offset = dwa->win->win_offset;
1414 parent_offset += r_start - dwa->target;
1416 error = bhndb_init_child_resource(r, dwa->parent_res, parent_offset,
1417 dwa->win->win_size);
1422 if ((error = rman_activate_resource(r)))
1428 /* Release our region allocation. */
1430 bhndb_dw_release(sc->bus_res, dwa, r);
1437 * Default bhndb(4) implementation of BUS_ACTIVATE_RESOURCE().
1440 bhndb_activate_resource(device_t dev, device_t child, int type, int rid,
1443 struct bhndb_softc *sc = device_get_softc(dev);
1445 /* Delegate directly to our parent device's bus if the requested
1446 * resource type isn't handled locally. */
1447 if (bhndb_get_rman(sc, child, type) == NULL) {
1448 return (BUS_ACTIVATE_RESOURCE(device_get_parent(sc->parent_dev),
1449 child, type, rid, r));
1452 return (bhndb_try_activate_resource(sc, child, type, rid, r, NULL));
1456 * Default bhndb(4) implementation of BUS_DEACTIVATE_RESOURCE().
1459 bhndb_deactivate_resource(device_t dev, device_t child, int type,
1460 int rid, struct resource *r)
1462 struct bhndb_dw_alloc *dwa;
1463 struct bhndb_softc *sc;
1467 sc = device_get_softc(dev);
1469 /* Delegate directly to our parent device's bus if the requested
1470 * resource type isn't handled locally. */
1471 rm = bhndb_get_rman(sc, child, type);
1473 return (BUS_DEACTIVATE_RESOURCE(
1474 device_get_parent(sc->parent_dev), child, type, rid, r));
1478 if ((error = rman_deactivate_resource(r)))
1483 /* No bridge-level state to be freed */
1486 case SYS_RES_MEMORY:
1487 /* Free any dynamic window allocation. */
1488 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) {
1490 dwa = bhndb_dw_find_resource(sc->bus_res, r);
1492 bhndb_dw_release(sc->bus_res, dwa, r);
1499 device_printf(dev, "unsupported resource type %d\n", type);
1505 * Default bhndb(4) implementation of BUS_GET_RESOURCE_LIST().
1507 static struct resource_list *
1508 bhndb_get_resource_list(device_t dev, device_t child)
1510 struct bhndb_devinfo *dinfo = device_get_ivars(child);
1511 return (&dinfo->resources);
1515 * Default bhndb(4) implementation of BHND_BUS_ACTIVATE_RESOURCE().
1517 * For BHNDB_ADDRSPACE_NATIVE children, all resources are activated as direct
1518 * resources via BUS_ACTIVATE_RESOURCE().
1520 * For BHNDB_ADDRSPACE_BRIDGED children, the resource priority is determined,
1521 * and if possible, the resource is activated as a direct resource. For example,
1522 * depending on resource priority and bridge resource availability, this
1523 * function will attempt to activate SYS_RES_MEMORY resources using either a
1524 * static register window, a dynamic register window, or it will configure @p r
1525 * as an indirect resource -- in that order.
1528 bhndb_activate_bhnd_resource(device_t dev, device_t child,
1529 int type, int rid, struct bhnd_resource *r)
1531 struct bhndb_softc *sc;
1532 struct bhndb_region *region;
1533 bhndb_priority_t r_prio;
1534 rman_res_t r_start, r_size;
1539 ("direct flag set on inactive resource"));
1541 KASSERT(!(rman_get_flags(r->res) & RF_ACTIVE),
1542 ("RF_ACTIVE set on inactive resource"));
1544 sc = device_get_softc(dev);
1546 /* Delegate directly to BUS_ACTIVATE_RESOURCE() if the requested
1547 * resource type isn't handled locally. */
1548 if (bhndb_get_rman(sc, child, type) == NULL) {
1549 error = BUS_ACTIVATE_RESOURCE(dev, child, type, rid, r->res);
1555 r_start = rman_get_start(r->res);
1556 r_size = rman_get_size(r->res);
1558 /* Determine the resource priority of bridged resources, and skip direct
1559 * allocation if the priority is too low. */
1560 if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) {
1563 /* IRQ resources are always direct */
1566 case SYS_RES_MEMORY:
1567 region = bhndb_find_resource_region(sc->bus_res,
1570 r_prio = region->priority;
1572 r_prio = BHNDB_PRIORITY_NONE;
1574 /* If less than the minimum dynamic window priority,
1575 * this resource should always be indirect. */
1576 if (r_prio < sc->bus_res->min_prio)
1582 device_printf(dev, "unsupported resource type %d\n",
1588 /* Attempt direct activation */
1589 error = bhndb_try_activate_resource(sc, child, type, rid, r->res,
1593 } else if (indirect) {
1594 /* The request was valid, but no viable register window is
1595 * available; indirection must be employed. */
1600 if (BHNDB_DEBUG(PRIO) &&
1601 bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED)
1603 device_printf(child, "activated 0x%llx-0x%llx as %s "
1605 (unsigned long long) r_start,
1606 (unsigned long long) r_start + r_size - 1,
1607 r->direct ? "direct" : "indirect");
1614 * Default bhndb(4) implementation of BHND_BUS_DEACTIVATE_RESOURCE().
1617 bhndb_deactivate_bhnd_resource(device_t dev, device_t child,
1618 int type, int rid, struct bhnd_resource *r)
1622 /* Indirect resources don't require activation */
1626 KASSERT(rman_get_flags(r->res) & RF_ACTIVE,
1627 ("RF_ACTIVE not set on direct resource"));
1629 /* Perform deactivation */
1630 error = BUS_DEACTIVATE_RESOURCE(dev, child, type, rid, r->res);
1638 * Find the best available bridge resource allocation record capable of handling
1639 * bus I/O requests of @p size at @p addr.
1641 * In order of preference, this function will either:
1643 * - Configure and return a free allocation record
1644 * - Return an existing allocation record mapping the requested space, or
1645 * - Steal, configure, and return an in-use allocation record.
1647 * Will panic if a usable record cannot be found.
1649 * @param sc Bridge driver state.
1650 * @param addr The I/O target address.
1651 * @param size The size of the I/O operation to be performed at @p addr.
1652 * @param[out] borrowed Set to true if the allocation record was borrowed to
1653 * fulfill this request; the borrowed record maps the target address range,
1654 * and must not be modified.
1655 * @param[out] stolen Set to true if the allocation record was stolen to fulfill
1656 * this request. If a stolen allocation record is returned,
1657 * bhndb_io_resource_restore() must be called upon completion of the bus I/O
1659 * @param[out] restore If the allocation record was stolen, this will be set
1660 * to the target that must be restored.
1662 static struct bhndb_dw_alloc *
1663 bhndb_io_resource_get_window(struct bhndb_softc *sc, bus_addr_t addr,
1664 bus_size_t size, bool *borrowed, bool *stolen, bus_addr_t *restore)
1666 struct bhndb_resources *br;
1667 struct bhndb_dw_alloc *dwa;
1668 struct bhndb_region *region;
1670 BHNDB_LOCK_ASSERT(sc, MA_OWNED);
1676 /* Try to fetch a free window */
1677 if ((dwa = bhndb_dw_next_free(br)) != NULL)
1680 /* Search for an existing dynamic mapping of this address range.
1681 * Static regions are not searched, as a statically mapped
1682 * region would never be allocated as an indirect resource. */
1683 for (size_t i = 0; i < br->dwa_count; i++) {
1684 const struct bhndb_regwin *win;
1686 dwa = &br->dw_alloc[i];
1689 KASSERT(win->win_type == BHNDB_REGWIN_T_DYN,
1690 ("invalid register window type"));
1692 /* Verify the range */
1693 if (addr < dwa->target)
1696 if (addr + size > dwa->target + win->win_size)
1704 /* Try to steal a window; this should only be required on very early
1705 * PCI_V0 (BCM4318, etc) Wi-Fi chipsets */
1706 region = bhndb_find_resource_region(br, addr, size);
1710 if ((region->alloc_flags & BHNDB_ALLOC_FULFILL_ON_OVERCOMMIT) == 0)
1713 /* Steal a window. This acquires our backing spinlock, disabling
1714 * interrupts; the spinlock will be released by
1715 * bhndb_dw_return_stolen() */
1716 if ((dwa = bhndb_dw_steal(br, restore)) != NULL) {
1721 panic("register windows exhausted attempting to map 0x%llx-0x%llx\n",
1722 (unsigned long long) addr, (unsigned long long) addr+size-1);
1726 * Return a borrowed reference to a bridge resource allocation record capable
1727 * of handling bus I/O requests of @p size at @p addr.
1729 * This will either return a reference to an existing allocation record mapping
1730 * the requested space, or will configure and return a free allocation record.
1732 * Will panic if a usable record cannot be found.
1734 * @param sc Bridge driver state.
1735 * @param addr The I/O target address.
1736 * @param size The size of the I/O operation to be performed at @p addr.
1737 * @param[out] offset The offset within the returned resource at which
1738 * to perform the I/O request.
1739 * @param[out] stolen Set to true if the allocation record was stolen to fulfill
1740 * this request. If a stolen allocation record is returned,
1741 * bhndb_io_resource_restore() must be called upon completion of the bus I/O
1743 * @param[out] restore If the allocation record was stolen, this will be set
1744 * to the target that must be restored.
1746 static inline struct bhndb_dw_alloc *
1747 bhndb_io_resource(struct bhndb_softc *sc, bus_addr_t addr, bus_size_t size,
1748 bus_size_t *offset, bool *stolen, bus_addr_t *restore)
1750 struct bhndb_dw_alloc *dwa;
1754 BHNDB_LOCK_ASSERT(sc, MA_OWNED);
1756 dwa = bhndb_io_resource_get_window(sc, addr, size, &borrowed, stolen,
1759 /* Adjust the window if the I/O request won't fit in the current
1761 if (addr < dwa->target ||
1762 addr > dwa->target + dwa->win->win_size ||
1763 (dwa->target + dwa->win->win_size) - addr < size)
1765 /* Cannot modify target of borrowed windows */
1767 panic("borrowed register window does not map expected "
1768 "range 0x%llx-0x%llx\n",
1769 (unsigned long long) addr,
1770 (unsigned long long) addr+size-1);
1773 error = bhndb_dw_set_addr(sc->dev, sc->bus_res, dwa, addr,
1776 panic("failed to set register window target mapping "
1778 (unsigned long long) addr,
1779 (unsigned long long) addr+size-1);
1783 /* Calculate the offset and return */
1784 *offset = (addr - dwa->target) + dwa->win->win_offset;
1789 * BHND_BUS_(READ|WRITE_* implementations
1792 /* bhndb_bus_(read|write) common implementation */
1793 #define BHNDB_IO_COMMON_SETUP(_io_size) \
1794 struct bhndb_softc *sc; \
1795 struct bhndb_dw_alloc *dwa; \
1796 struct resource *io_res; \
1797 bus_size_t io_offset; \
1798 bus_addr_t restore; \
1801 sc = device_get_softc(dev); \
1804 dwa = bhndb_io_resource(sc, rman_get_start(r->res) + \
1805 offset, _io_size, &io_offset, &stolen, &restore); \
1806 io_res = dwa->parent_res; \
1808 KASSERT(!r->direct, \
1809 ("bhnd_bus slow path used for direct resource")); \
1811 KASSERT(rman_get_flags(io_res) & RF_ACTIVE, \
1812 ("i/o resource is not active"));
1814 #define BHNDB_IO_COMMON_TEARDOWN() \
1816 bhndb_dw_return_stolen(sc->dev, sc->bus_res, \
1821 /* Defines a bhndb_bus_read_* method implementation */
1822 #define BHNDB_IO_READ(_type, _name) \
1824 bhndb_bus_read_ ## _name (device_t dev, device_t child, \
1825 struct bhnd_resource *r, bus_size_t offset) \
1828 BHNDB_IO_COMMON_SETUP(sizeof(_type)); \
1829 v = bus_read_ ## _name (io_res, io_offset); \
1830 BHNDB_IO_COMMON_TEARDOWN(); \
1835 /* Defines a bhndb_bus_write_* method implementation */
1836 #define BHNDB_IO_WRITE(_type, _name) \
1838 bhndb_bus_write_ ## _name (device_t dev, device_t child, \
1839 struct bhnd_resource *r, bus_size_t offset, _type value) \
1841 BHNDB_IO_COMMON_SETUP(sizeof(_type)); \
1842 bus_write_ ## _name (io_res, io_offset, value); \
1843 BHNDB_IO_COMMON_TEARDOWN(); \
1846 /* Defines a bhndb_bus_(read|write|set)_(multi|region)_* method */
1847 #define BHNDB_IO_MISC(_type, _ptr, _op, _size) \
1849 bhndb_bus_ ## _op ## _ ## _size (device_t dev, \
1850 device_t child, struct bhnd_resource *r, bus_size_t offset, \
1851 _type _ptr datap, bus_size_t count) \
1853 BHNDB_IO_COMMON_SETUP(sizeof(_type) * count); \
1854 bus_ ## _op ## _ ## _size (io_res, io_offset, \
1856 BHNDB_IO_COMMON_TEARDOWN(); \
1859 /* Defines a complete set of read/write methods */
1860 #define BHNDB_IO_METHODS(_type, _size) \
1861 BHNDB_IO_READ(_type, _size) \
1862 BHNDB_IO_WRITE(_type, _size) \
1864 BHNDB_IO_READ(_type, stream_ ## _size) \
1865 BHNDB_IO_WRITE(_type, stream_ ## _size) \
1867 BHNDB_IO_MISC(_type, *, read_multi, _size) \
1868 BHNDB_IO_MISC(_type, *, write_multi, _size) \
1870 BHNDB_IO_MISC(_type, *, read_multi_stream, _size) \
1871 BHNDB_IO_MISC(_type, *, write_multi_stream, _size) \
1873 BHNDB_IO_MISC(_type, , set_multi, _size) \
1874 BHNDB_IO_MISC(_type, , set_region, _size) \
1875 BHNDB_IO_MISC(_type, *, read_region, _size) \
1876 BHNDB_IO_MISC(_type, *, write_region, _size) \
1878 BHNDB_IO_MISC(_type, *, read_region_stream, _size) \
1879 BHNDB_IO_MISC(_type, *, write_region_stream, _size)
1881 BHNDB_IO_METHODS(uint8_t, 1);
1882 BHNDB_IO_METHODS(uint16_t, 2);
1883 BHNDB_IO_METHODS(uint32_t, 4);
1886 * Default bhndb(4) implementation of BHND_BUS_BARRIER().
1889 bhndb_bus_barrier(device_t dev, device_t child, struct bhnd_resource *r,
1890 bus_size_t offset, bus_size_t length, int flags)
1892 BHNDB_IO_COMMON_SETUP(length);
1894 bus_barrier(io_res, io_offset + offset, length, flags);
1896 BHNDB_IO_COMMON_TEARDOWN();
1900 * Default bhndb(4) implementation of BHND_MAP_INTR().
1903 bhndb_bhnd_map_intr(device_t dev, device_t child, u_int intr, rman_res_t *irq)
1905 struct bhndb_softc *sc;
1909 sc = device_get_softc(dev);
1911 /* Is the intr valid? */
1912 if (intr >= bhnd_get_intr_count(child))
1915 /* Fetch the interrupt vector */
1916 if ((error = bhnd_get_intr_ivec(child, intr, &ivec)))
1919 /* Map directly to the actual backplane interrupt vector */
1926 * Default bhndb(4) implementation of BHND_UNMAP_INTR().
1929 bhndb_bhnd_unmap_intr(device_t dev, device_t child, rman_res_t irq)
1931 /* No state to clean up */
1935 * Default bhndb(4) implementation of BUS_SETUP_INTR().
1938 bhndb_setup_intr(device_t dev, device_t child, struct resource *r,
1939 int flags, driver_filter_t filter, driver_intr_t handler, void *arg,
1942 struct bhndb_softc *sc;
1943 struct bhndb_intr_isrc *isrc;
1944 struct bhndb_intr_handler *ih;
1947 sc = device_get_softc(dev);
1949 /* Fetch the isrc */
1950 if ((error = BHNDB_MAP_INTR_ISRC(dev, r, &isrc))) {
1951 device_printf(dev, "failed to fetch isrc: %d\n", error);
1955 /* Allocate new ihandler entry */
1956 ih = bhndb_alloc_intr_handler(child, r, isrc);
1960 /* Perform actual interrupt setup via the host isrc */
1961 error = bus_setup_intr(isrc->is_owner, isrc->is_res, flags, filter,
1962 handler, arg, &ih->ih_cookiep);
1964 bhndb_free_intr_handler(ih);
1968 /* Add to our interrupt handler list */
1970 bhndb_register_intr_handler(sc->bus_res, ih);
1973 /* Provide the interrupt handler entry as our cookiep value */
1979 * Default bhndb(4) implementation of BUS_TEARDOWN_INTR().
1982 bhndb_teardown_intr(device_t dev, device_t child, struct resource *r,
1985 struct bhndb_softc *sc;
1986 struct bhndb_intr_handler *ih;
1987 struct bhndb_intr_isrc *isrc;
1990 sc = device_get_softc(dev);
1992 /* Locate and claim ownership of the interrupt handler entry */
1995 ih = bhndb_find_intr_handler(sc->bus_res, cookiep);
1997 panic("%s requested teardown of invalid cookiep %p",
1998 device_get_nameunit(child), cookiep);
2001 bhndb_deregister_intr_handler(sc->bus_res, ih);
2005 /* Perform actual interrupt teardown via the host isrc */
2007 error = bus_teardown_intr(isrc->is_owner, isrc->is_res, ih->ih_cookiep);
2009 /* If teardown fails, we need to reinsert the handler entry
2010 * to allow later teardown */
2012 bhndb_register_intr_handler(sc->bus_res, ih);
2018 /* Free the entry */
2019 bhndb_free_intr_handler(ih);
2024 * Default bhndb(4) implementation of BUS_BIND_INTR().
2027 bhndb_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
2029 struct bhndb_softc *sc;
2030 struct bhndb_intr_handler *ih;
2031 struct bhndb_intr_isrc *isrc;
2033 sc = device_get_softc(dev);
2036 /* Fetch the isrc corresponding to the child IRQ resource */
2038 STAILQ_FOREACH(ih, &sc->bus_res->bus_intrs, ih_link) {
2039 if (ih->ih_res == irq) {
2047 panic("%s requested bind of invalid irq %#jx-%#jx",
2048 device_get_nameunit(child), rman_get_start(irq),
2052 /* Perform actual bind via the host isrc */
2053 return (bus_bind_intr(isrc->is_owner, isrc->is_res, cpu));
2057 * Default bhndb(4) implementation of BUS_DESCRIBE_INTR().
2060 bhndb_describe_intr(device_t dev, device_t child, struct resource *irq,
2061 void *cookie, const char *descr)
2063 struct bhndb_softc *sc;
2064 struct bhndb_intr_handler *ih;
2065 struct bhndb_intr_isrc *isrc;
2067 sc = device_get_softc(dev);
2069 /* Locate the interrupt handler entry; the caller owns the handler
2070 * reference, and thus our entry is guaranteed to remain valid after
2071 * we drop out lock below. */
2074 ih = bhndb_find_intr_handler(sc->bus_res, cookie);
2076 panic("%s requested invalid cookiep %p",
2077 device_get_nameunit(child), cookie);
2084 /* Perform the actual request via the host isrc */
2085 return (BUS_DESCRIBE_INTR(device_get_parent(isrc->is_owner),
2086 isrc->is_owner, isrc->is_res, ih->ih_cookiep, descr));
2090 * Default bhndb(4) implementation of BUS_CONFIG_INTR().
2093 bhndb_config_intr(device_t dev, int irq, enum intr_trigger trig,
2094 enum intr_polarity pol)
2101 * Default bhndb(4) implementation of BUS_REMAP_INTR().
2104 bhndb_remap_intr(device_t dev, device_t child, u_int irq)
2111 * Default bhndb(4) implementation of BHND_BUS_GET_DMA_TRANSLATION().
2114 bhndb_get_dma_translation(device_t dev, device_t child, u_int width,
2115 uint32_t flags, bus_dma_tag_t *dmat,
2116 struct bhnd_dma_translation *translation)
2118 struct bhndb_softc *sc;
2119 const struct bhndb_hwcfg *hwcfg;
2120 const struct bhnd_dma_translation *match;
2121 bus_dma_tag_t match_dmat;
2122 bhnd_addr_t addr_mask, match_addr_mask;
2124 sc = device_get_softc(dev);
2125 hwcfg = sc->bus_res->cfg;
2127 /* Is DMA supported? */
2128 if (sc->bus_res->res->dma_tags == NULL)
2131 /* Is the requested width supported? */
2132 if (width > BHND_DMA_ADDR_32BIT) {
2133 /* Backplane must support 64-bit addressing */
2134 if (!(sc->chipid.chip_caps & BHND_CAP_BP64))
2135 width = BHND_DMA_ADDR_32BIT;
2138 /* Find the best matching descriptor for the requested width */
2139 addr_mask = BHND_DMA_ADDR_BITMASK(width);
2142 match_addr_mask = 0x0;
2145 for (size_t i = 0; i < sc->bus_res->res->num_dma_tags; i++) {
2146 const struct bhnd_dma_translation *dwin;
2149 dwin = &hwcfg->dma_translations[i];
2151 /* The base address must be device addressable */
2152 if ((dwin->base_addr & addr_mask) != dwin->base_addr)
2155 /* The flags must match */
2156 if ((dwin->flags & flags) != flags)
2159 /* The window must cover at least part of our addressable
2161 masked = (dwin->addr_mask | dwin->addrext_mask) & addr_mask;
2165 /* Is this a better match? */
2166 if (match == NULL || masked > match_addr_mask) {
2168 match_addr_mask = masked;
2169 match_dmat = sc->bus_res->res->dma_tags[i];
2173 if (match == NULL || match_addr_mask == 0)
2179 if (translation != NULL)
2180 *translation = *match;
2186 * Default bhndb(4) implementation of BUS_GET_DMA_TAG().
2188 static bus_dma_tag_t
2189 bhndb_get_dma_tag(device_t dev, device_t child)
2191 struct bhndb_softc *sc = device_get_softc(dev);
2194 * A bridge may have multiple DMA translation descriptors, each with
2195 * their own incompatible restrictions; drivers should in general call
2196 * BHND_BUS_GET_DMA_TRANSLATION() to fetch both the best available DMA
2197 * translation, and its corresponding DMA tag.
2199 * Child drivers that do not use BHND_BUS_GET_DMA_TRANSLATION() are
2200 * responsible for creating their own restricted DMA tag; since we
2201 * cannot do this for them in BUS_GET_DMA_TAG(), we simply return the
2202 * bridge parent's DMA tag directly;
2204 return (bus_get_dma_tag(sc->parent_dev));
2207 static device_method_t bhndb_methods[] = {
2208 /* Device interface */ \
2209 DEVMETHOD(device_probe, bhndb_generic_probe),
2210 DEVMETHOD(device_detach, bhndb_generic_detach),
2211 DEVMETHOD(device_shutdown, bus_generic_shutdown),
2212 DEVMETHOD(device_suspend, bhndb_generic_suspend),
2213 DEVMETHOD(device_resume, bhndb_generic_resume),
2216 DEVMETHOD(bus_probe_nomatch, bhndb_probe_nomatch),
2217 DEVMETHOD(bus_print_child, bhndb_print_child),
2218 DEVMETHOD(bus_child_pnpinfo_str, bhndb_child_pnpinfo_str),
2219 DEVMETHOD(bus_child_location_str, bhndb_child_location_str),
2220 DEVMETHOD(bus_add_child, bhndb_add_child),
2221 DEVMETHOD(bus_child_deleted, bhndb_child_deleted),
2223 DEVMETHOD(bus_alloc_resource, bhndb_alloc_resource),
2224 DEVMETHOD(bus_release_resource, bhndb_release_resource),
2225 DEVMETHOD(bus_activate_resource, bhndb_activate_resource),
2226 DEVMETHOD(bus_deactivate_resource, bhndb_deactivate_resource),
2228 DEVMETHOD(bus_setup_intr, bhndb_setup_intr),
2229 DEVMETHOD(bus_teardown_intr, bhndb_teardown_intr),
2230 DEVMETHOD(bus_config_intr, bhndb_config_intr),
2231 DEVMETHOD(bus_bind_intr, bhndb_bind_intr),
2232 DEVMETHOD(bus_describe_intr, bhndb_describe_intr),
2233 DEVMETHOD(bus_remap_intr, bhndb_remap_intr),
2235 DEVMETHOD(bus_get_dma_tag, bhndb_get_dma_tag),
2237 DEVMETHOD(bus_adjust_resource, bhndb_adjust_resource),
2238 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
2239 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
2240 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
2241 DEVMETHOD(bus_get_resource_list, bhndb_get_resource_list),
2243 DEVMETHOD(bus_read_ivar, bhndb_read_ivar),
2244 DEVMETHOD(bus_write_ivar, bhndb_write_ivar),
2246 /* BHNDB interface */
2247 DEVMETHOD(bhndb_get_chipid, bhndb_get_chipid),
2248 DEVMETHOD(bhndb_is_core_disabled, bhndb_is_core_disabled),
2249 DEVMETHOD(bhndb_get_hostb_core, bhndb_get_hostb_core),
2250 DEVMETHOD(bhndb_suspend_resource, bhndb_suspend_resource),
2251 DEVMETHOD(bhndb_resume_resource, bhndb_resume_resource),
2253 /* BHND interface */
2254 DEVMETHOD(bhnd_bus_get_chipid, bhndb_get_chipid),
2255 DEVMETHOD(bhnd_bus_activate_resource, bhndb_activate_bhnd_resource),
2256 DEVMETHOD(bhnd_bus_deactivate_resource, bhndb_deactivate_bhnd_resource),
2257 DEVMETHOD(bhnd_bus_get_nvram_var, bhnd_bus_generic_get_nvram_var),
2258 DEVMETHOD(bhnd_bus_map_intr, bhndb_bhnd_map_intr),
2259 DEVMETHOD(bhnd_bus_unmap_intr, bhndb_bhnd_unmap_intr),
2260 DEVMETHOD(bhnd_bus_get_dma_translation, bhndb_get_dma_translation),
2262 DEVMETHOD(bhnd_bus_get_service_registry,bhndb_get_service_registry),
2263 DEVMETHOD(bhnd_bus_register_provider, bhnd_bus_generic_sr_register_provider),
2264 DEVMETHOD(bhnd_bus_deregister_provider, bhnd_bus_generic_sr_deregister_provider),
2265 DEVMETHOD(bhnd_bus_retain_provider, bhnd_bus_generic_sr_retain_provider),
2266 DEVMETHOD(bhnd_bus_release_provider, bhnd_bus_generic_sr_release_provider),
2268 DEVMETHOD(bhnd_bus_read_1, bhndb_bus_read_1),
2269 DEVMETHOD(bhnd_bus_read_2, bhndb_bus_read_2),
2270 DEVMETHOD(bhnd_bus_read_4, bhndb_bus_read_4),
2271 DEVMETHOD(bhnd_bus_write_1, bhndb_bus_write_1),
2272 DEVMETHOD(bhnd_bus_write_2, bhndb_bus_write_2),
2273 DEVMETHOD(bhnd_bus_write_4, bhndb_bus_write_4),
2275 DEVMETHOD(bhnd_bus_read_stream_1, bhndb_bus_read_stream_1),
2276 DEVMETHOD(bhnd_bus_read_stream_2, bhndb_bus_read_stream_2),
2277 DEVMETHOD(bhnd_bus_read_stream_4, bhndb_bus_read_stream_4),
2278 DEVMETHOD(bhnd_bus_write_stream_1, bhndb_bus_write_stream_1),
2279 DEVMETHOD(bhnd_bus_write_stream_2, bhndb_bus_write_stream_2),
2280 DEVMETHOD(bhnd_bus_write_stream_4, bhndb_bus_write_stream_4),
2282 DEVMETHOD(bhnd_bus_read_multi_1, bhndb_bus_read_multi_1),
2283 DEVMETHOD(bhnd_bus_read_multi_2, bhndb_bus_read_multi_2),
2284 DEVMETHOD(bhnd_bus_read_multi_4, bhndb_bus_read_multi_4),
2285 DEVMETHOD(bhnd_bus_write_multi_1, bhndb_bus_write_multi_1),
2286 DEVMETHOD(bhnd_bus_write_multi_2, bhndb_bus_write_multi_2),
2287 DEVMETHOD(bhnd_bus_write_multi_4, bhndb_bus_write_multi_4),
2289 DEVMETHOD(bhnd_bus_read_multi_stream_1, bhndb_bus_read_multi_stream_1),
2290 DEVMETHOD(bhnd_bus_read_multi_stream_2, bhndb_bus_read_multi_stream_2),
2291 DEVMETHOD(bhnd_bus_read_multi_stream_4, bhndb_bus_read_multi_stream_4),
2292 DEVMETHOD(bhnd_bus_write_multi_stream_1,bhndb_bus_write_multi_stream_1),
2293 DEVMETHOD(bhnd_bus_write_multi_stream_2,bhndb_bus_write_multi_stream_2),
2294 DEVMETHOD(bhnd_bus_write_multi_stream_4,bhndb_bus_write_multi_stream_4),
2296 DEVMETHOD(bhnd_bus_set_multi_1, bhndb_bus_set_multi_1),
2297 DEVMETHOD(bhnd_bus_set_multi_2, bhndb_bus_set_multi_2),
2298 DEVMETHOD(bhnd_bus_set_multi_4, bhndb_bus_set_multi_4),
2299 DEVMETHOD(bhnd_bus_set_region_1, bhndb_bus_set_region_1),
2300 DEVMETHOD(bhnd_bus_set_region_2, bhndb_bus_set_region_2),
2301 DEVMETHOD(bhnd_bus_set_region_4, bhndb_bus_set_region_4),
2303 DEVMETHOD(bhnd_bus_read_region_1, bhndb_bus_read_region_1),
2304 DEVMETHOD(bhnd_bus_read_region_2, bhndb_bus_read_region_2),
2305 DEVMETHOD(bhnd_bus_read_region_4, bhndb_bus_read_region_4),
2306 DEVMETHOD(bhnd_bus_write_region_1, bhndb_bus_write_region_1),
2307 DEVMETHOD(bhnd_bus_write_region_2, bhndb_bus_write_region_2),
2308 DEVMETHOD(bhnd_bus_write_region_4, bhndb_bus_write_region_4),
2310 DEVMETHOD(bhnd_bus_read_region_stream_1,bhndb_bus_read_region_stream_1),
2311 DEVMETHOD(bhnd_bus_read_region_stream_2,bhndb_bus_read_region_stream_2),
2312 DEVMETHOD(bhnd_bus_read_region_stream_4,bhndb_bus_read_region_stream_4),
2313 DEVMETHOD(bhnd_bus_write_region_stream_1,bhndb_bus_write_region_stream_1),
2314 DEVMETHOD(bhnd_bus_write_region_stream_2,bhndb_bus_write_region_stream_2),
2315 DEVMETHOD(bhnd_bus_write_region_stream_4,bhndb_bus_write_region_stream_4),
2317 DEVMETHOD(bhnd_bus_barrier, bhndb_bus_barrier),
2322 devclass_t bhndb_devclass;
2324 DEFINE_CLASS_0(bhndb, bhndb_driver, bhndb_methods, sizeof(struct bhndb_softc));
2326 MODULE_VERSION(bhndb, 1);
2327 MODULE_DEPEND(bhndb, bhnd, 1, 1, 1);