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$");
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/limits.h>
41 #include "bhndb_private.h"
44 static int bhndb_dma_tag_create(device_t dev, bus_dma_tag_t parent_dmat,
45 const struct bhnd_dma_translation *translation,
49 * Attach a BHND bridge device to @p parent.
51 * @param parent A parent PCI device.
52 * @param[out] bhndb On success, the probed and attached bhndb bridge device.
53 * @param unit The device unit number, or -1 to select the next available unit
57 * @retval non-zero Failed to attach the bhndb device.
60 bhndb_attach_bridge(device_t parent, device_t *bhndb, int unit)
64 *bhndb = device_add_child(parent, "bhndb", unit);
68 if (!(error = device_probe_and_attach(*bhndb)))
71 if ((device_delete_child(parent, *bhndb)))
72 device_printf(parent, "failed to detach bhndb child\n");
78 * Call BHNDB_SUSPEND_RESOURCE() for all resources in @p rl.
81 bhndb_do_suspend_resources(device_t dev, struct resource_list *rl)
83 struct resource_list_entry *rle;
85 /* Suspend all child resources. */
86 STAILQ_FOREACH(rle, rl, link) {
87 /* Skip non-allocated resources */
91 BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev, rle->type,
97 * Helper function for implementing BUS_RESUME_CHILD() on bridged
100 * This implementation of BUS_RESUME_CHILD() uses BUS_GET_RESOURCE_LIST()
101 * to find the child's resources and call BHNDB_SUSPEND_RESOURCE() for all
102 * child resources, ensuring that the device's allocated bridge resources
103 * will be available to other devices during bus resumption.
105 * Before suspending any resources, @p child is suspended by
106 * calling bhnd_generic_suspend_child().
108 * If @p child is not a direct child of @p dev, suspension is delegated to
112 bhnd_generic_br_suspend_child(device_t dev, device_t child)
114 struct resource_list *rl;
117 if (device_get_parent(child) != dev)
118 BUS_SUSPEND_CHILD(device_get_parent(dev), child);
120 if (device_is_suspended(child))
123 /* Suspend the child device */
124 if ((error = bhnd_generic_suspend_child(dev, child)))
127 /* Fetch the resource list. If none, there's nothing else to do */
128 rl = BUS_GET_RESOURCE_LIST(device_get_parent(child), child);
132 /* Suspend all child resources. */
133 bhndb_do_suspend_resources(dev, rl);
139 * Helper function for implementing BUS_RESUME_CHILD() on bridged
140 * bhnd(4) bus devices.
142 * This implementation of BUS_RESUME_CHILD() uses BUS_GET_RESOURCE_LIST()
143 * to find the child's resources and call BHNDB_RESUME_RESOURCE() for all
144 * child resources, before delegating to bhnd_generic_resume_child().
146 * If resource resumption fails, @p child will not be resumed.
148 * If @p child is not a direct child of @p dev, suspension is delegated to
152 bhnd_generic_br_resume_child(device_t dev, device_t child)
154 struct resource_list *rl;
155 struct resource_list_entry *rle;
158 if (device_get_parent(child) != dev)
159 BUS_RESUME_CHILD(device_get_parent(dev), child);
161 if (!device_is_suspended(child))
164 /* Fetch the resource list. If none, there's nothing else to do */
165 rl = BUS_GET_RESOURCE_LIST(device_get_parent(child), child);
167 return (bhnd_generic_resume_child(dev, child));
169 /* Resume all resources */
170 STAILQ_FOREACH(rle, rl, link) {
171 /* Skip non-allocated resources */
172 if (rle->res == NULL)
175 error = BHNDB_RESUME_RESOURCE(device_get_parent(dev), dev,
176 rle->type, rle->res);
178 /* Put all resources back into a suspend state */
179 bhndb_do_suspend_resources(dev, rl);
184 /* Now that all resources are resumed, resume child */
185 if ((error = bhnd_generic_resume_child(dev, child))) {
186 /* Put all resources back into a suspend state */
187 bhndb_do_suspend_resources(dev, rl);
194 * Find a host resource of @p type that maps the given range.
196 * @param hr The resource state to search.
197 * @param type The resource type to search for (see SYS_RES_*).
198 * @param start The start address of the range to search for.
199 * @param count The size of the range to search for.
201 * @retval resource the host resource containing the requested range.
202 * @retval NULL if no resource containing the requested range can be found.
205 bhndb_host_resource_for_range(struct bhndb_host_resources *hr, int type,
206 rman_res_t start, rman_res_t count)
208 for (u_int i = 0; hr->resource_specs[i].type != -1; i++) {
209 struct resource *r = hr->resources[i];
211 if (hr->resource_specs[i].type != type)
215 if (rman_get_start(r) > start)
218 if (rman_get_end(r) < (start + count - 1))
228 * Find a host resource of that matches the given register window definition.
230 * @param hr The resource state to search.
231 * @param win A register window definition.
233 * @retval resource the host resource corresponding to @p win.
234 * @retval NULL if no resource corresponding to @p win can be found.
237 bhndb_host_resource_for_regwin(struct bhndb_host_resources *hr,
238 const struct bhndb_regwin *win)
240 const struct resource_spec *rspecs;
242 rspecs = hr->resource_specs;
243 for (u_int i = 0; rspecs[i].type != -1; i++) {
244 if (win->res.type != rspecs[i].type)
247 if (win->res.rid != rspecs[i].rid)
250 /* Found declared resource */
251 return (hr->resources[i]);
254 device_printf(hr->owner, "missing regwin resource spec "
255 "(type=%d, rid=%d)\n", win->res.type, win->res.rid);
261 * Allocate and initialize a new resource state structure.
263 * @param dev The bridge device.
264 * @param parent_dev The parent device from which host resources should be
266 * @param cfg The hardware configuration to be used.
268 struct bhndb_resources *
269 bhndb_alloc_resources(device_t dev, device_t parent_dev,
270 const struct bhndb_hwcfg *cfg)
272 struct bhndb_resources *r;
273 const struct bhndb_regwin *win;
274 bus_size_t last_window_size;
277 bool free_ht_mem, free_br_mem, free_br_irq;
283 r = malloc(sizeof(*r), M_BHND, M_NOWAIT|M_ZERO);
287 /* Basic initialization */
291 r->min_prio = BHNDB_PRIORITY_NONE;
292 STAILQ_INIT(&r->bus_regions);
293 STAILQ_INIT(&r->bus_intrs);
295 mtx_init(&r->dw_steal_mtx, device_get_nameunit(dev),
296 "bhndb dwa_steal lock", MTX_SPIN);
298 /* Initialize host address space resource manager. */
299 r->ht_mem_rman.rm_start = 0;
300 r->ht_mem_rman.rm_end = ~0;
301 r->ht_mem_rman.rm_type = RMAN_ARRAY;
302 r->ht_mem_rman.rm_descr = "BHNDB host memory";
303 if ((error = rman_init(&r->ht_mem_rman))) {
304 device_printf(r->dev, "could not initialize ht_mem_rman\n");
310 /* Initialize resource manager for the bridged address space. */
311 r->br_mem_rman.rm_start = 0;
312 r->br_mem_rman.rm_end = BUS_SPACE_MAXADDR_32BIT;
313 r->br_mem_rman.rm_type = RMAN_ARRAY;
314 r->br_mem_rman.rm_descr = "BHNDB bridged memory";
316 if ((error = rman_init(&r->br_mem_rman))) {
317 device_printf(r->dev, "could not initialize br_mem_rman\n");
322 error = rman_manage_region(&r->br_mem_rman, 0, BUS_SPACE_MAXADDR_32BIT);
324 device_printf(r->dev, "could not configure br_mem_rman\n");
329 /* Initialize resource manager for the bridged interrupt controller. */
330 r->br_irq_rman.rm_start = 0;
331 r->br_irq_rman.rm_end = RM_MAX_END;
332 r->br_irq_rman.rm_type = RMAN_ARRAY;
333 r->br_irq_rman.rm_descr = "BHNDB bridged interrupts";
335 if ((error = rman_init(&r->br_irq_rman))) {
336 device_printf(r->dev, "could not initialize br_irq_rman\n");
341 error = rman_manage_region(&r->br_irq_rman, 0, RM_MAX_END);
343 device_printf(r->dev, "could not configure br_irq_rman\n");
347 /* Fetch the dynamic regwin count and verify that it does not exceed
348 * what is representable via our freelist bitstring. */
349 r->dwa_count = bhndb_regwin_count(cfg->register_windows,
351 if (r->dwa_count >= INT_MAX) {
352 device_printf(r->dev, "max dynamic regwin count exceeded\n");
356 /* Allocate the dynamic window allocation table. */
357 r->dw_alloc = malloc(sizeof(r->dw_alloc[0]) * r->dwa_count, M_BHND,
359 if (r->dw_alloc == NULL)
362 /* Allocate the dynamic window allocation freelist */
363 r->dwa_freelist = bit_alloc(r->dwa_count, M_BHND, M_NOWAIT);
364 if (r->dwa_freelist == NULL)
367 /* Initialize the dynamic window table */
369 last_window_size = 0;
370 for (win = cfg->register_windows;
371 win->win_type != BHNDB_REGWIN_T_INVALID; win++)
373 struct bhndb_dw_alloc *dwa;
375 /* Skip non-DYN windows */
376 if (win->win_type != BHNDB_REGWIN_T_DYN)
379 /* Validate the window size */
380 if (win->win_size == 0) {
381 device_printf(r->dev, "ignoring zero-length dynamic "
382 "register window\n");
384 } else if (last_window_size == 0) {
385 last_window_size = win->win_size;
386 } else if (last_window_size != win->win_size) {
388 * No existing hardware should trigger this.
390 * If you run into this in the future, the dynamic
391 * window allocator and the resource priority system
392 * will need to be extended to support multiple register
393 * window allocation pools.
395 device_printf(r->dev, "devices that vend multiple "
396 "dynamic register window sizes are not currently "
401 dwa = &r->dw_alloc[rnid];
403 dwa->parent_res = NULL;
407 LIST_INIT(&dwa->refs);
411 /* Allocate host resources */
412 error = bhndb_alloc_host_resources(&r->res, dev, parent_dev, r->cfg);
414 device_printf(r->dev,
415 "could not allocate host resources on %s: %d\n",
416 device_get_nameunit(parent_dev), error);
420 /* Populate (and validate) parent resource references for all
422 for (size_t i = 0; i < r->dwa_count; i++) {
423 struct bhndb_dw_alloc *dwa;
424 const struct bhndb_regwin *win;
426 dwa = &r->dw_alloc[i];
429 /* Find and validate corresponding resource. */
430 dwa->parent_res = bhndb_host_resource_for_regwin(r->res, win);
431 if (dwa->parent_res == NULL) {
432 device_printf(r->dev, "no host resource found for %u "
433 "register window with offset %#jx and "
436 (uintmax_t)win->win_offset,
437 (uintmax_t)win->win_size);
443 if (rman_get_size(dwa->parent_res) < win->win_offset +
446 device_printf(r->dev, "resource %d too small for "
447 "register window with offset %llx and size %llx\n",
448 rman_get_rid(dwa->parent_res),
449 (unsigned long long) win->win_offset,
450 (unsigned long long) win->win_size);
457 /* Add allocated memory resources to our host memory resource manager */
458 for (u_int i = 0; r->res->resource_specs[i].type != -1; i++) {
459 struct resource *res;
461 /* skip non-memory resources */
462 if (r->res->resource_specs[i].type != SYS_RES_MEMORY)
465 /* add host resource to set of managed regions */
466 res = r->res->resources[i];
467 error = rman_manage_region(&r->ht_mem_rman,
468 rman_get_start(res), rman_get_end(res));
470 device_printf(r->dev,
471 "could not register host memory region with "
472 "ht_mem_rman: %d\n", error);
481 rman_fini(&r->ht_mem_rman);
484 rman_fini(&r->br_mem_rman);
487 rman_fini(&r->br_irq_rman);
489 if (r->dw_alloc != NULL)
490 free(r->dw_alloc, M_BHND);
492 if (r->dwa_freelist != NULL)
493 free(r->dwa_freelist, M_BHND);
496 bhndb_release_host_resources(r->res);
498 mtx_destroy(&r->dw_steal_mtx);
506 * Create a new DMA tag for the given @p translation.
508 * @param dev The bridge device.
509 * @param parent_dmat The parent DMA tag, or NULL if none.
510 * @param translation The DMA translation for which a DMA tag will
512 * @param[out] dmat On success, the newly created DMA tag.
515 * @retval non-zero if creating the new DMA tag otherwise fails, a regular
516 * unix error code will be returned.
519 bhndb_dma_tag_create(device_t dev, bus_dma_tag_t parent_dmat,
520 const struct bhnd_dma_translation *translation, bus_dma_tag_t *dmat)
522 bus_dma_tag_t translation_tag;
524 bus_addr_t lowaddr, highaddr;
528 highaddr = BUS_SPACE_MAXADDR;
529 maxsegsz = BUS_SPACE_MAXSIZE;
531 /* Determine full addressable mask */
532 dt_mask = (translation->addr_mask | translation->addrext_mask);
533 KASSERT(dt_mask != 0, ("DMA addr_mask invalid: %#jx",
534 (uintmax_t)dt_mask));
536 /* (addr_mask|addrext_mask) is our maximum supported address */
537 lowaddr = MIN(dt_mask, BUS_SPACE_MAXADDR);
539 /* Constrain to translation window size */
540 if (translation->addr_mask < maxsegsz)
541 maxsegsz = translation->addr_mask;
543 /* Create our DMA tag */
544 error = bus_dma_tag_create(parent_dmat,
545 1, 0, /* alignment, boundary */
547 NULL, NULL, /* filter, filterarg */
548 BUS_SPACE_MAXSIZE, 0, /* maxsize, nsegments */
549 maxsegsz, 0, /* maxsegsize, flags */
550 NULL, NULL, /* lockfunc, lockarg */
553 device_printf(dev, "failed to create bridge DMA tag: %d\n",
558 *dmat = translation_tag;
563 * Deallocate the given bridge resource structure and any associated resources.
565 * @param br Resource state to be deallocated.
568 bhndb_free_resources(struct bhndb_resources *br)
570 struct bhndb_region *region, *r_next;
571 struct bhndb_dw_alloc *dwa;
572 struct bhndb_dw_rentry *dwr, *dwr_next;
573 struct bhndb_intr_handler *ih;
574 bool leaked_regions, leaked_intrs;
576 leaked_regions = false;
577 leaked_intrs = false;
579 /* No window regions may still be held */
580 if (!bhndb_dw_all_free(br)) {
581 for (int i = 0; i < br->dwa_count; i++) {
582 dwa = &br->dw_alloc[i];
584 /* Skip free dynamic windows */
585 if (bhndb_dw_is_free(br, dwa))
588 device_printf(br->dev,
589 "leaked dynamic register window %d\n", dwa->rnid);
590 leaked_regions = true;
594 /* There should be no interrupt handlers still registered */
595 STAILQ_FOREACH(ih, &br->bus_intrs, ih_link) {
596 device_printf(br->dev, "interrupt handler leaked %p\n",
600 if (leaked_intrs || leaked_regions) {
601 panic("leaked%s%s", leaked_intrs ? " active interrupts" : "",
602 leaked_regions ? " active register windows" : "");
605 /* Release host resources allocated through our parent. */
607 bhndb_release_host_resources(br->res);
609 /* Clean up resource reservations */
610 for (size_t i = 0; i < br->dwa_count; i++) {
611 dwa = &br->dw_alloc[i];
613 LIST_FOREACH_SAFE(dwr, &dwa->refs, dw_link, dwr_next) {
614 LIST_REMOVE(dwr, dw_link);
619 /* Release bus regions */
620 STAILQ_FOREACH_SAFE(region, &br->bus_regions, link, r_next) {
621 STAILQ_REMOVE(&br->bus_regions, region, bhndb_region, link);
622 free(region, M_BHND);
625 /* Release our resource managers */
626 rman_fini(&br->ht_mem_rman);
627 rman_fini(&br->br_mem_rman);
628 rman_fini(&br->br_irq_rman);
630 free(br->dw_alloc, M_BHND);
631 free(br->dwa_freelist, M_BHND);
633 mtx_destroy(&br->dw_steal_mtx);
639 * Allocate host bus resources defined by @p hwcfg.
641 * On success, the caller assumes ownership of the allocated host resources,
642 * which must be freed via bhndb_release_host_resources().
644 * @param[out] resources On success, the allocated host resources.
645 * @param dev The bridge device.
646 * @param parent_dev The parent device from which host resources
647 * should be allocated (e.g. via
648 * bus_alloc_resources()).
649 * @param hwcfg The hardware configuration defining the host
650 * resources to be allocated
653 bhndb_alloc_host_resources(struct bhndb_host_resources **resources,
654 device_t dev, device_t parent_dev, const struct bhndb_hwcfg *hwcfg)
656 struct bhndb_host_resources *hr;
657 const struct bhnd_dma_translation *dt;
658 bus_dma_tag_t parent_dmat;
662 parent_dmat = bus_get_dma_tag(parent_dev);
664 hr = malloc(sizeof(*hr), M_BHND, M_WAITOK);
665 hr->owner = parent_dev;
667 hr->resource_specs = NULL;
668 hr->resources = NULL;
670 hr->num_dma_tags = 0;
672 /* Determine our bridge resource count from the hardware config. */
674 for (size_t i = 0; hwcfg->resource_specs[i].type != -1; i++)
677 /* Determine the total count and validate our DMA translation table. */
679 for (dt = hwcfg->dma_translations; dt != NULL &&
680 !BHND_DMA_IS_TRANSLATION_TABLE_END(dt); dt++)
682 /* Validate the defined translation */
683 if ((dt->base_addr & dt->addr_mask) != 0) {
684 device_printf(dev, "invalid DMA translation; base "
685 "address %#jx overlaps address mask %#jx",
686 (uintmax_t)dt->base_addr, (uintmax_t)dt->addr_mask);
692 if ((dt->addrext_mask & dt->addr_mask) != 0) {
693 device_printf(dev, "invalid DMA translation; addrext "
694 "mask %#jx overlaps address mask %#jx",
695 (uintmax_t)dt->addrext_mask,
696 (uintmax_t)dt->addr_mask);
702 /* Increment our entry count */
706 /* Allocate our DMA tags */
707 hr->dma_tags = malloc(sizeof(*hr->dma_tags) * ndt, M_BHND,
709 for (size_t i = 0; i < ndt; i++) {
710 error = bhndb_dma_tag_create(dev, parent_dmat,
711 &hwcfg->dma_translations[i], &hr->dma_tags[i]);
718 /* Allocate space for a non-const copy of our resource_spec
719 * table; this will be updated with the RIDs assigned by
720 * bus_alloc_resources. */
721 hr->resource_specs = malloc(sizeof(hr->resource_specs[0]) * (nres + 1),
724 /* Initialize and terminate the table */
725 for (size_t i = 0; i < nres; i++)
726 hr->resource_specs[i] = hwcfg->resource_specs[i];
728 hr->resource_specs[nres].type = -1;
730 /* Allocate space for our resource references */
731 hr->resources = malloc(sizeof(hr->resources[0]) * nres, M_BHND,
734 /* Allocate host resources */
735 error = bus_alloc_resources(hr->owner, hr->resource_specs,
738 device_printf(dev, "could not allocate bridge resources via "
739 "%s: %d\n", device_get_nameunit(parent_dev), error);
747 if (hr->resource_specs != NULL)
748 free(hr->resource_specs, M_BHND);
750 if (hr->resources != NULL)
751 free(hr->resources, M_BHND);
753 for (size_t i = 0; i < hr->num_dma_tags; i++)
754 bus_dma_tag_destroy(hr->dma_tags[i]);
756 if (hr->dma_tags != NULL)
757 free(hr->dma_tags, M_BHND);
765 * Deallocate a set of bridge host resources.
767 * @param hr The resources to be freed.
770 bhndb_release_host_resources(struct bhndb_host_resources *hr)
772 bus_release_resources(hr->owner, hr->resource_specs, hr->resources);
774 for (size_t i = 0; i < hr->num_dma_tags; i++)
775 bus_dma_tag_destroy(hr->dma_tags[i]);
777 free(hr->resources, M_BHND);
778 free(hr->resource_specs, M_BHND);
779 free(hr->dma_tags, M_BHND);
785 * Search @p cores for the core serving as the bhnd host bridge.
787 * This function uses a heuristic valid on all known PCI/PCIe/PCMCIA-bridged
788 * bhnd(4) devices to determine the hostb core:
790 * - The core must have a Broadcom vendor ID.
791 * - The core devclass must match the bridge type.
792 * - The core must be the first device on the bus with the bridged device
795 * @param cores The core table to search.
796 * @param ncores The number of cores in @p cores.
797 * @param bridge_devclass The expected device class of the bridge core.
798 * @param[out] core If found, the matching host bridge core info.
801 * @retval ENOENT not found
804 bhndb_find_hostb_core(struct bhnd_core_info *cores, u_int ncores,
805 bhnd_devclass_t bridge_devclass, struct bhnd_core_info *core)
807 struct bhnd_core_match md;
808 struct bhnd_core_info *match;
809 u_int match_core_idx;
811 /* Set up a match descriptor for the required device class. */
812 md = (struct bhnd_core_match) {
813 BHND_MATCH_CORE_CLASS(bridge_devclass),
814 BHND_MATCH_CORE_UNIT(0)
817 /* Find the matching core with the lowest core index */
819 match_core_idx = UINT_MAX;
821 for (u_int i = 0; i < ncores; i++) {
822 if (!bhnd_core_matches(&cores[i], &md))
825 /* Lower core indices take precedence */
826 if (match != NULL && match_core_idx < match->core_idx)
830 match_core_idx = match->core_idx;
841 * Allocate a host interrupt source and its backing SYS_RES_IRQ host resource.
843 * @param owner The device to be used to allocate a SYS_RES_IRQ
844 * resource with @p rid.
845 * @param rid The resource ID of the IRQ to be allocated.
846 * @param start The start value to be passed to bus_alloc_resource().
847 * @param end The end value to be passed to bus_alloc_resource().
848 * @param count The count to be passed to bus_alloc_resource().
849 * @param flags The flags to be passed to bus_alloc_resource().
851 * @retval non-NULL success
852 * @retval NULL if allocation fails.
854 struct bhndb_intr_isrc *
855 bhndb_alloc_intr_isrc(device_t owner, int rid, rman_res_t start, rman_res_t end,
856 rman_res_t count, u_int flags)
858 struct bhndb_intr_isrc *isrc;
860 isrc = malloc(sizeof(*isrc), M_BHND, M_NOWAIT);
864 isrc->is_owner = owner;
866 isrc->is_res = bus_alloc_resource(owner, SYS_RES_IRQ, &isrc->is_rid,
867 start, end, count, flags);
868 if (isrc->is_res == NULL) {
877 * Free a host interrupt source and its backing host resource.
879 * @param isrc The interrupt source to be freed.
882 bhndb_free_intr_isrc(struct bhndb_intr_isrc *isrc)
884 bus_release_resource(isrc->is_owner, SYS_RES_IRQ, isrc->is_rid,
890 * Allocate and initialize a new interrupt handler entry.
892 * @param owner The child device that owns this entry.
893 * @param r The child's interrupt resource.
894 * @param isrc The isrc mapped for this entry.
896 * @retval non-NULL success
897 * @retval NULL if allocation fails.
899 struct bhndb_intr_handler *
900 bhndb_alloc_intr_handler(device_t owner, struct resource *r,
901 struct bhndb_intr_isrc *isrc)
903 struct bhndb_intr_handler *ih;
905 ih = malloc(sizeof(*ih), M_BHND, M_NOWAIT | M_ZERO);
906 ih->ih_owner = owner;
909 ih->ih_cookiep = NULL;
910 ih->ih_active = false;
916 * Free an interrupt handler entry.
918 * @param br The resource state owning @p ih.
919 * @param ih The interrupt handler entry to be removed.
922 bhndb_free_intr_handler(struct bhndb_intr_handler *ih)
924 KASSERT(!ih->ih_active, ("free of active interrupt handler %p",
931 * Add an active interrupt handler to the given resource state.
933 * @param br The resource state to be modified.
934 * @param ih The interrupt handler entry to be added.
937 bhndb_register_intr_handler(struct bhndb_resources *br,
938 struct bhndb_intr_handler *ih)
940 KASSERT(!ih->ih_active, ("duplicate registration of interrupt "
941 "handler %p", ih->ih_cookiep));
942 KASSERT(ih->ih_cookiep != NULL, ("missing cookiep"));
944 ih->ih_active = true;
945 STAILQ_INSERT_HEAD(&br->bus_intrs, ih, ih_link);
949 * Remove an interrupt handler from the given resource state.
951 * @param br The resource state containing @p ih.
952 * @param ih The interrupt handler entry to be removed.
955 bhndb_deregister_intr_handler(struct bhndb_resources *br,
956 struct bhndb_intr_handler *ih)
958 KASSERT(ih->ih_active, ("duplicate deregistration of interrupt "
959 "handler %p", ih->ih_cookiep));
961 KASSERT(bhndb_find_intr_handler(br, ih) == ih,
962 ("unknown interrupt handler %p", ih));
964 STAILQ_REMOVE(&br->bus_intrs, ih, bhndb_intr_handler, ih_link);
965 ih->ih_active = false;
969 * Return the interrupt handler entry corresponding to @p cookiep, or NULL
970 * if no entry is found.
972 * @param br The resource state to search for the given @p cookiep.
973 * @param cookiep The interrupt handler's bus-assigned cookiep value.
975 struct bhndb_intr_handler *
976 bhndb_find_intr_handler(struct bhndb_resources *br, void *cookiep)
978 struct bhndb_intr_handler *ih;
980 STAILQ_FOREACH(ih, &br->bus_intrs, ih_link) {
990 * Find the maximum start and end limits of the bridged resource @p r.
992 * If the resource is not currently mapped by the bridge, ENOENT will be
995 * @param br The resource state to search.
996 * @param type The resource type (see SYS_RES_*).
997 * @param r The resource to search for in @p br.
998 * @param[out] start On success, the minimum supported start address.
999 * @param[out] end On success, the maximum supported end address.
1002 * @retval ENOENT no active mapping found for @p r of @p type
1005 bhndb_find_resource_limits(struct bhndb_resources *br, int type,
1006 struct resource *r, rman_res_t *start, rman_res_t *end)
1008 struct bhndb_dw_alloc *dynamic;
1009 struct bhndb_region *sregion;
1010 struct bhndb_intr_handler *ih;
1014 /* Is this one of ours? */
1015 STAILQ_FOREACH(ih, &br->bus_intrs, ih_link) {
1016 if (ih->ih_res == r)
1019 /* We don't support adjusting IRQ resource limits */
1020 *start = rman_get_start(r);
1021 *end = rman_get_end(r);
1028 case SYS_RES_MEMORY: {
1029 /* Check for an enclosing dynamic register window */
1030 if ((dynamic = bhndb_dw_find_resource(br, r))) {
1031 *start = dynamic->target;
1032 *end = dynamic->target + dynamic->win->win_size - 1;
1036 /* Check for a static region */
1037 sregion = bhndb_find_resource_region(br, rman_get_start(r),
1039 if (sregion != NULL && sregion->static_regwin != NULL) {
1040 *start = sregion->addr;
1041 *end = sregion->addr + sregion->size - 1;
1051 device_printf(br->dev, "unknown resource type: %d\n", type);
1057 * Add a bus region entry to @p r for the given base @p addr and @p size.
1059 * @param br The resource state to which the bus region entry will be added.
1060 * @param addr The base address of this region.
1061 * @param size The size of this region.
1062 * @param priority The resource priority to be assigned to allocations
1063 * made within this bus region.
1064 * @param alloc_flags resource allocation flags (@see bhndb_alloc_flags)
1065 * @param static_regwin If available, a static register window mapping this
1066 * bus region entry. If not available, NULL.
1069 * @retval non-zero if adding the bus region fails.
1072 bhndb_add_resource_region(struct bhndb_resources *br, bhnd_addr_t addr,
1073 bhnd_size_t size, bhndb_priority_t priority, uint32_t alloc_flags,
1074 const struct bhndb_regwin *static_regwin)
1076 struct bhndb_region *reg;
1078 /* Insert in the bus resource list */
1079 reg = malloc(sizeof(*reg), M_BHND, M_NOWAIT);
1083 *reg = (struct bhndb_region) {
1086 .priority = priority,
1087 .alloc_flags = alloc_flags,
1088 .static_regwin = static_regwin
1091 STAILQ_INSERT_HEAD(&br->bus_regions, reg, link);
1097 * Return true if a mapping of @p size bytes at @p addr is provided by either
1098 * one contiguous bus region, or by multiple discontiguous regions.
1100 * @param br The resource state to query.
1101 * @param addr The requested starting address.
1102 * @param size The requested size.
1105 bhndb_has_static_region_mapping(struct bhndb_resources *br,
1106 bhnd_addr_t addr, bhnd_size_t size)
1108 struct bhndb_region *region;
1112 while ((region = bhndb_find_resource_region(br, r_addr, 1)) != NULL) {
1113 /* Must be backed by a static register window */
1114 if (region->static_regwin == NULL)
1117 /* Adjust the search offset */
1118 r_addr += region->size;
1120 /* Have we traversed a complete (if discontiguous) mapping? */
1121 if (r_addr == addr + size)
1126 /* No complete mapping found */
1131 * Find the bus region that maps @p size bytes at @p addr.
1133 * @param br The resource state to search.
1134 * @param addr The requested starting address.
1135 * @param size The requested size.
1137 * @retval bhndb_region A region that fully contains the requested range.
1138 * @retval NULL If no mapping region can be found.
1140 struct bhndb_region *
1141 bhndb_find_resource_region(struct bhndb_resources *br, bhnd_addr_t addr,
1144 struct bhndb_region *region;
1146 STAILQ_FOREACH(region, &br->bus_regions, link) {
1147 /* Request must fit within the region's mapping */
1148 if (addr < region->addr)
1151 if (addr + size > region->addr + region->size)
1162 * Find the entry matching @p r in @p dwa's references, if any.
1164 * @param dwa The dynamic window allocation to search
1165 * @param r The resource to search for in @p dwa.
1167 static struct bhndb_dw_rentry *
1168 bhndb_dw_find_resource_entry(struct bhndb_dw_alloc *dwa, struct resource *r)
1170 struct bhndb_dw_rentry *rentry;
1172 LIST_FOREACH(rentry, &dwa->refs, dw_link) {
1173 struct resource *dw_res = rentry->dw_res;
1175 /* Match dev/rid/addr/size */
1176 if (rman_get_device(dw_res) != rman_get_device(r) ||
1177 rman_get_rid(dw_res) != rman_get_rid(r) ||
1178 rman_get_start(dw_res) != rman_get_start(r) ||
1179 rman_get_size(dw_res) != rman_get_size(r))
1184 /* Matching allocation found */
1192 * Find the dynamic region allocated for @p r, if any.
1194 * @param br The resource state to search.
1195 * @param r The resource to search for.
1197 * @retval bhndb_dw_alloc The allocation record for @p r.
1198 * @retval NULL if no dynamic window is allocated for @p r.
1200 struct bhndb_dw_alloc *
1201 bhndb_dw_find_resource(struct bhndb_resources *br, struct resource *r)
1203 struct bhndb_dw_alloc *dwa;
1205 for (size_t i = 0; i < br->dwa_count; i++) {
1206 dwa = &br->dw_alloc[i];
1208 /* Skip free dynamic windows */
1209 if (bhndb_dw_is_free(br, dwa))
1212 /* Matching allocation found? */
1213 if (bhndb_dw_find_resource_entry(dwa, r) != NULL)
1221 * Find an existing dynamic window mapping @p size bytes
1222 * at @p addr. The window may or may not be free.
1224 * @param br The resource state to search.
1225 * @param addr The requested starting address.
1226 * @param size The requested size.
1228 * @retval bhndb_dw_alloc A window allocation that fully contains the requested
1230 * @retval NULL If no mapping region can be found.
1232 struct bhndb_dw_alloc *
1233 bhndb_dw_find_mapping(struct bhndb_resources *br, bhnd_addr_t addr,
1236 struct bhndb_dw_alloc *dwr;
1237 const struct bhndb_regwin *win;
1239 /* Search for an existing dynamic mapping of this address range. */
1240 for (size_t i = 0; i < br->dwa_count; i++) {
1241 dwr = &br->dw_alloc[i];
1244 /* Verify the range */
1245 if (addr < dwr->target)
1248 if (addr + size > dwr->target + win->win_size)
1251 /* Found a usable mapping */
1260 * Retain a reference to @p dwa for use by @p res.
1262 * @param br The resource state owning @p dwa.
1263 * @param dwa The allocation record to be retained.
1264 * @param res The resource that will own a reference to @p dwa.
1267 * @retval ENOMEM Failed to allocate a new reference structure.
1270 bhndb_dw_retain(struct bhndb_resources *br, struct bhndb_dw_alloc *dwa,
1271 struct resource *res)
1273 struct bhndb_dw_rentry *rentry;
1275 KASSERT(bhndb_dw_find_resource_entry(dwa, res) == NULL,
1276 ("double-retain of dynamic window for same resource"));
1278 /* Insert a reference entry; we use M_NOWAIT to allow use from
1279 * within a non-sleepable lock */
1280 rentry = malloc(sizeof(*rentry), M_BHND, M_NOWAIT);
1284 rentry->dw_res = res;
1285 LIST_INSERT_HEAD(&dwa->refs, rentry, dw_link);
1287 /* Update the free list */
1288 bit_set(br->dwa_freelist, dwa->rnid);
1294 * Release a reference to @p dwa previously retained by @p res. If the
1295 * reference count of @p dwa reaches zero, it will be added to the
1298 * @param br The resource state owning @p dwa.
1299 * @param dwa The allocation record to be released.
1300 * @param res The resource that currently owns a reference to @p dwa.
1303 bhndb_dw_release(struct bhndb_resources *br, struct bhndb_dw_alloc *dwa,
1306 struct bhndb_dw_rentry *rentry;
1308 /* Find the rentry */
1309 rentry = bhndb_dw_find_resource_entry(dwa, r);
1310 KASSERT(rentry != NULL, ("over release of resource entry"));
1312 LIST_REMOVE(rentry, dw_link);
1313 free(rentry, M_BHND);
1315 /* If this was the last reference, update the free list */
1316 if (LIST_EMPTY(&dwa->refs))
1317 bit_clear(br->dwa_freelist, dwa->rnid);
1321 * Attempt to set (or reset) the target address of @p dwa to map @p size bytes
1324 * This will apply any necessary window alignment and verify that
1325 * the window is capable of mapping the requested range prior to modifying
1328 * @param dev The device on which to issue the BHNDB_SET_WINDOW_ADDR() request.
1329 * @param br The resource state owning @p dwa.
1330 * @param dwa The allocation record to be configured.
1331 * @param addr The address to be mapped via @p dwa.
1332 * @param size The number of bytes to be mapped at @p addr.
1335 * @retval non-zero no usable register window available.
1338 bhndb_dw_set_addr(device_t dev, struct bhndb_resources *br,
1339 struct bhndb_dw_alloc *dwa, bus_addr_t addr, bus_size_t size)
1341 const struct bhndb_regwin *rw;
1347 KASSERT(bhndb_dw_is_free(br, dwa) || mtx_owned(&br->dw_steal_mtx),
1348 ("attempting to set the target address on an in-use window"));
1350 /* Page-align the target address */
1351 offset = addr % rw->win_size;
1352 dwa->target = addr - offset;
1354 /* Verify that the window is large enough for the full target */
1355 if (rw->win_size - offset < size)
1358 /* Update the window target */
1359 error = BHNDB_SET_WINDOW_ADDR(dev, dwa->win, dwa->target);
1369 * Steal an in-use allocation record from @p br, returning the record's current
1370 * target in @p saved on success.
1372 * This function acquires a mutex and disables interrupts; callers should
1373 * avoid holding a stolen window longer than required to issue an I/O
1376 * A successful call to bhndb_dw_steal() must be balanced with a call to
1377 * bhndb_dw_return_stolen().
1379 * @param br The resource state from which a window should be stolen.
1380 * @param saved The stolen window's saved target address.
1382 * @retval non-NULL success
1383 * @retval NULL no dynamic window regions are defined.
1385 struct bhndb_dw_alloc *
1386 bhndb_dw_steal(struct bhndb_resources *br, bus_addr_t *saved)
1388 struct bhndb_dw_alloc *dw_stolen;
1390 KASSERT(bhndb_dw_next_free(br) == NULL,
1391 ("attempting to steal an in-use window while free windows remain"));
1393 /* Nothing to steal from? */
1394 if (br->dwa_count == 0)
1398 * Acquire our steal spinlock; this will be released in
1399 * bhndb_dw_return_stolen().
1401 * Acquiring also disables interrupts, which is required when one is
1402 * stealing an in-use existing register window.
1404 mtx_lock_spin(&br->dw_steal_mtx);
1406 dw_stolen = &br->dw_alloc[0];
1407 *saved = dw_stolen->target;
1412 * Return an allocation record previously stolen using bhndb_dw_steal().
1414 * @param dev The device on which to issue a BHNDB_SET_WINDOW_ADDR() request.
1415 * @param br The resource state owning @p dwa.
1416 * @param dwa The allocation record to be returned.
1417 * @param saved The original target address provided by bhndb_dw_steal().
1420 bhndb_dw_return_stolen(device_t dev, struct bhndb_resources *br,
1421 struct bhndb_dw_alloc *dwa, bus_addr_t saved)
1425 mtx_assert(&br->dw_steal_mtx, MA_OWNED);
1427 error = bhndb_dw_set_addr(dev, br, dwa, saved, 0);
1429 panic("failed to restore register window target %#jx: %d\n",
1430 (uintmax_t)saved, error);
1433 mtx_unlock_spin(&br->dw_steal_mtx);
1437 * Return the count of @p type register windows in @p table.
1439 * @param table The table to search.
1440 * @param type The required window type, or BHNDB_REGWIN_T_INVALID to
1441 * count all register window types.
1444 bhndb_regwin_count(const struct bhndb_regwin *table,
1445 bhndb_regwin_type_t type)
1447 const struct bhndb_regwin *rw;
1451 for (rw = table; rw->win_type != BHNDB_REGWIN_T_INVALID; rw++) {
1452 if (type == BHNDB_REGWIN_T_INVALID || rw->win_type == type)
1460 * Search @p table for the first window with the given @p type.
1462 * @param table The table to search.
1463 * @param type The required window type.
1464 * @param min_size The minimum window size.
1466 * @retval bhndb_regwin The first matching window.
1467 * @retval NULL If no window of the requested type could be found.
1469 const struct bhndb_regwin *
1470 bhndb_regwin_find_type(const struct bhndb_regwin *table,
1471 bhndb_regwin_type_t type, bus_size_t min_size)
1473 const struct bhndb_regwin *rw;
1475 for (rw = table; rw->win_type != BHNDB_REGWIN_T_INVALID; rw++)
1477 if (rw->win_type == type && rw->win_size >= min_size)
1485 * Search @p windows for the first matching core window.
1487 * @param table The table to search.
1488 * @param class The required core class.
1489 * @param unit The required core unit, or -1.
1490 * @param port_type The required port type.
1491 * @param port The required port.
1492 * @param region The required region.
1493 * @param offset The required readable core register block offset.
1494 * @param min_size The required minimum readable size at @p offset.
1496 * @retval bhndb_regwin The first matching window.
1497 * @retval NULL If no matching window was found.
1499 const struct bhndb_regwin *
1500 bhndb_regwin_find_core(const struct bhndb_regwin *table, bhnd_devclass_t class,
1501 int unit, bhnd_port_type port_type, u_int port, u_int region,
1502 bus_size_t offset, bus_size_t min_size)
1504 const struct bhndb_regwin *rw;
1506 for (rw = table; rw->win_type != BHNDB_REGWIN_T_INVALID; rw++)
1508 bus_size_t rw_offset;
1510 /* Match on core, port, and region attributes */
1511 if (rw->win_type != BHNDB_REGWIN_T_CORE)
1514 if (rw->d.core.class != class)
1517 if (unit != -1 && rw->d.core.unit != unit)
1520 if (rw->d.core.port_type != port_type)
1523 if (rw->d.core.port != port)
1526 if (rw->d.core.region != region)
1529 /* Verify that the requested range is mapped within
1530 * this register window */
1531 if (rw->d.core.offset > offset)
1534 rw_offset = offset - rw->d.core.offset;
1536 if (rw->win_size < rw_offset)
1539 if (rw->win_size - rw_offset < min_size)
1549 * Search @p windows for the best available window of at least @p min_size.
1552 * - BHND_REGWIN_T_CORE
1553 * - BHND_REGWIN_T_DYN
1555 * @param table The table to search.
1556 * @param class The required core class.
1557 * @param unit The required core unit, or -1.
1558 * @param port_type The required port type.
1559 * @param port The required port.
1560 * @param region The required region.
1561 * @param offset The required readable core register block offset.
1562 * @param min_size The required minimum readable size at @p offset.
1564 * @retval bhndb_regwin The first matching window.
1565 * @retval NULL If no matching window was found.
1567 const struct bhndb_regwin *
1568 bhndb_regwin_find_best(const struct bhndb_regwin *table,
1569 bhnd_devclass_t class, int unit, bhnd_port_type port_type, u_int port,
1570 u_int region, bus_size_t offset, bus_size_t min_size)
1572 const struct bhndb_regwin *rw;
1574 /* Prefer a fixed core mapping */
1575 rw = bhndb_regwin_find_core(table, class, unit, port_type,
1576 port, region, offset, min_size);
1580 /* Fall back on a generic dynamic window */
1581 return (bhndb_regwin_find_type(table, BHNDB_REGWIN_T_DYN, min_size));
1585 * Return true if @p regw defines a BHNDB_REGWIN_T_CORE register window
1586 * that matches against @p core.
1588 * @param regw A register window to match against.
1589 * @param core The bhnd(4) core info to match against @p regw.
1592 bhndb_regwin_match_core(const struct bhndb_regwin *regw,
1593 struct bhnd_core_info *core)
1595 /* Only core windows are supported */
1596 if (regw->win_type != BHNDB_REGWIN_T_CORE)
1599 /* Device class must match */
1600 if (bhnd_core_class(core) != regw->d.core.class)
1603 /* Device unit must match */
1604 if (core->unit != regw->d.core.unit)
1612 * Search for a core resource priority descriptor in @p table that matches
1615 * @param table The table to search.
1616 * @param core The core to match against @p table.
1618 const struct bhndb_hw_priority *
1619 bhndb_hw_priority_find_core(const struct bhndb_hw_priority *table,
1620 struct bhnd_core_info *core)
1622 const struct bhndb_hw_priority *hp;
1624 for (hp = table; hp->ports != NULL; hp++) {
1625 if (bhnd_core_matches(core, &hp->match))
1635 * Search for a port resource priority descriptor in @p table.
1637 * @param table The table to search.
1638 * @param core The core to match against @p table.
1639 * @param port_type The required port type.
1640 * @param port The required port.
1641 * @param region The required region.
1643 const struct bhndb_port_priority *
1644 bhndb_hw_priorty_find_port(const struct bhndb_hw_priority *table,
1645 struct bhnd_core_info *core, bhnd_port_type port_type, u_int port,
1648 const struct bhndb_hw_priority *hp;
1650 if ((hp = bhndb_hw_priority_find_core(table, core)) == NULL)
1653 for (u_int i = 0; i < hp->num_ports; i++) {
1654 const struct bhndb_port_priority *pp = &hp->ports[i];
1656 if (pp->type != port_type)
1659 if (pp->port != port)
1662 if (pp->region != region)