2 * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/limits.h>
37 #include "bhndb_private.h"
41 * Attach a BHND bridge device to @p parent.
43 * @param parent A parent PCI device.
44 * @param[out] bhndb On success, the probed and attached bhndb bridge device.
45 * @param unit The device unit number, or -1 to select the next available unit
49 * @retval non-zero Failed to attach the bhndb device.
52 bhndb_attach_bridge(device_t parent, device_t *bhndb, int unit)
56 *bhndb = device_add_child(parent, "bhndb", unit);
60 if (!(error = device_probe_and_attach(*bhndb)))
63 if ((device_delete_child(parent, *bhndb)))
64 device_printf(parent, "failed to detach bhndb child\n");
70 * Call BHNDB_SUSPEND_RESOURCE() for all resources in @p rl.
73 bhndb_do_suspend_resources(device_t dev, struct resource_list *rl)
75 struct resource_list_entry *rle;
77 /* Suspend all child resources. */
78 STAILQ_FOREACH(rle, rl, link) {
79 /* Skip non-allocated resources */
83 BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev, rle->type,
89 * Helper function for implementing BUS_RESUME_CHILD() on bridged
92 * This implementation of BUS_RESUME_CHILD() uses BUS_GET_RESOURCE_LIST()
93 * to find the child's resources and call BHNDB_SUSPEND_RESOURCE() for all
94 * child resources, ensuring that the device's allocated bridge resources
95 * will be available to other devices during bus resumption.
97 * Before suspending any resources, @p child is suspended by
98 * calling bhnd_generic_suspend_child().
100 * If @p child is not a direct child of @p dev, suspension is delegated to
104 bhnd_generic_br_suspend_child(device_t dev, device_t child)
106 struct resource_list *rl;
109 if (device_get_parent(child) != dev)
110 BUS_SUSPEND_CHILD(device_get_parent(dev), child);
112 if (device_is_suspended(child))
115 /* Suspend the child device */
116 if ((error = bhnd_generic_suspend_child(dev, child)))
119 /* Fetch the resource list. If none, there's nothing else to do */
120 rl = BUS_GET_RESOURCE_LIST(device_get_parent(child), child);
124 /* Suspend all child resources. */
125 bhndb_do_suspend_resources(dev, rl);
131 * Helper function for implementing BUS_RESUME_CHILD() on bridged
132 * bhnd(4) bus devices.
134 * This implementation of BUS_RESUME_CHILD() uses BUS_GET_RESOURCE_LIST()
135 * to find the child's resources and call BHNDB_RESUME_RESOURCE() for all
136 * child resources, before delegating to bhnd_generic_resume_child().
138 * If resource resumption fails, @p child will not be resumed.
140 * If @p child is not a direct child of @p dev, suspension is delegated to
144 bhnd_generic_br_resume_child(device_t dev, device_t child)
146 struct resource_list *rl;
147 struct resource_list_entry *rle;
150 if (device_get_parent(child) != dev)
151 BUS_RESUME_CHILD(device_get_parent(dev), child);
153 if (!device_is_suspended(child))
156 /* Fetch the resource list. If none, there's nothing else to do */
157 rl = BUS_GET_RESOURCE_LIST(device_get_parent(child), child);
159 return (bhnd_generic_resume_child(dev, child));
161 /* Resume all resources */
162 STAILQ_FOREACH(rle, rl, link) {
163 /* Skip non-allocated resources */
164 if (rle->res == NULL)
167 error = BHNDB_RESUME_RESOURCE(device_get_parent(dev), dev,
168 rle->type, rle->res);
170 /* Put all resources back into a suspend state */
171 bhndb_do_suspend_resources(dev, rl);
176 /* Now that all resources are resumed, resume child */
177 if ((error = bhnd_generic_resume_child(dev, child))) {
178 /* Put all resources back into a suspend state */
179 bhndb_do_suspend_resources(dev, rl);
186 * Find a SYS_RES_MEMORY resource containing the given address range.
188 * @param br The bhndb resource state to search.
189 * @param start The start address of the range to search for.
190 * @param count The size of the range to search for.
192 * @retval resource the host resource containing the requested range.
193 * @retval NULL if no resource containing the requested range can be found.
196 bhndb_find_resource_range(struct bhndb_resources *br, rman_res_t start,
199 KASSERT(br->res_avail, ("no host resources allocated"));
201 for (u_int i = 0; br->res_spec[i].type != -1; i++) {
202 struct resource *r = br->res[i];
204 if (br->res_spec->type != SYS_RES_MEMORY)
208 if (rman_get_start(r) > start)
211 if (rman_get_end(r) < (start + count - 1))
221 * Find the resource containing @p win.
223 * @param br The bhndb resource state to search.
224 * @param win A register window.
226 * @retval resource the resource containing @p win.
227 * @retval NULL if no resource containing @p win can be found.
230 bhndb_find_regwin_resource(struct bhndb_resources *br,
231 const struct bhndb_regwin *win)
233 const struct resource_spec *rspecs;
235 KASSERT(br->res_avail, ("no host resources allocated"));
237 rspecs = br->cfg->resource_specs;
238 for (u_int i = 0; rspecs[i].type != -1; i++) {
239 if (win->res.type != rspecs[i].type)
242 if (win->res.rid != rspecs[i].rid)
245 /* Found declared resource */
249 device_printf(br->dev,
250 "missing regwin resource spec (type=%d, rid=%d)\n",
251 win->res.type, win->res.rid);
257 * Allocate and initialize a new resource state structure.
259 * @param dev The bridge device.
260 * @param parent_dev The parent device from which host resources should be
262 * @param cfg The hardware configuration to be used.
264 struct bhndb_resources *
265 bhndb_alloc_resources(device_t dev, device_t parent_dev,
266 const struct bhndb_hwcfg *cfg)
268 struct bhndb_resources *r;
269 const struct bhndb_regwin *win;
270 bus_size_t last_window_size;
273 bool free_ht_mem, free_br_mem;
278 r = malloc(sizeof(*r), M_BHND, M_NOWAIT|M_ZERO);
282 /* Basic initialization */
284 r->parent_dev = parent_dev;
286 r->min_prio = BHNDB_PRIORITY_NONE;
287 STAILQ_INIT(&r->bus_regions);
289 /* Initialize host address space resource manager. */
290 r->ht_mem_rman.rm_start = 0;
291 r->ht_mem_rman.rm_end = ~0;
292 r->ht_mem_rman.rm_type = RMAN_ARRAY;
293 r->ht_mem_rman.rm_descr = "BHNDB host memory";
294 if ((error = rman_init(&r->ht_mem_rman))) {
295 device_printf(r->dev, "could not initialize ht_mem_rman\n");
301 /* Initialize resource manager for the bridged address space. */
302 r->br_mem_rman.rm_start = 0;
303 r->br_mem_rman.rm_end = BUS_SPACE_MAXADDR_32BIT;
304 r->br_mem_rman.rm_type = RMAN_ARRAY;
305 r->br_mem_rman.rm_descr = "BHNDB bridged memory";
307 if ((error = rman_init(&r->br_mem_rman))) {
308 device_printf(r->dev, "could not initialize br_mem_rman\n");
313 error = rman_manage_region(&r->br_mem_rman, 0, BUS_SPACE_MAXADDR_32BIT);
315 device_printf(r->dev, "could not configure br_mem_rman\n");
319 /* Fetch the dynamic regwin count and verify that it does not exceed
320 * what is representable via our freelist bitstring. */
321 r->dwa_count = bhndb_regwin_count(cfg->register_windows,
323 if (r->dwa_count >= INT_MAX) {
324 device_printf(r->dev, "max dynamic regwin count exceeded\n");
328 /* Allocate the dynamic window allocation table. */
329 r->dw_alloc = malloc(sizeof(r->dw_alloc[0]) * r->dwa_count, M_BHND,
331 if (r->dw_alloc == NULL)
334 /* Allocate the dynamic window allocation freelist */
335 r->dwa_freelist = bit_alloc(r->dwa_count, M_BHND, M_NOWAIT);
336 if (r->dwa_freelist == NULL)
339 /* Initialize the dynamic window table */
341 last_window_size = 0;
342 for (win = cfg->register_windows;
343 win->win_type != BHNDB_REGWIN_T_INVALID; win++)
345 struct bhndb_dw_alloc *dwa;
347 /* Skip non-DYN windows */
348 if (win->win_type != BHNDB_REGWIN_T_DYN)
351 /* Validate the window size */
352 if (win->win_size == 0) {
353 device_printf(r->dev, "ignoring zero-length dynamic "
354 "register window\n");
356 } else if (last_window_size == 0) {
357 last_window_size = win->win_size;
358 } else if (last_window_size != win->win_size) {
360 * No existing hardware should trigger this.
362 * If you run into this in the future, the dynamic
363 * window allocator and the resource priority system
364 * will need to be extended to support multiple register
365 * window allocation pools.
367 device_printf(r->dev, "devices that vend multiple "
368 "dynamic register window sizes are not currently "
373 dwa = &r->dw_alloc[rnid];
375 dwa->parent_res = NULL;
379 LIST_INIT(&dwa->refs);
387 rman_fini(&r->ht_mem_rman);
390 rman_fini(&r->br_mem_rman);
392 if (r->dw_alloc != NULL)
393 free(r->dw_alloc, M_BHND);
395 if (r->dwa_freelist != NULL)
396 free(r->dwa_freelist, M_BHND);
404 * Allocate host resources required by @p br, and initialize
405 * internal BHNDB_ADDRSPACE_NATIVE resource manager state.
407 * @param br Resource state.
410 bhndb_alloc_host_resources(struct bhndb_resources *br)
415 KASSERT(!br->res_avail, ("host resources already allocated"));
417 /* Determine our bridge resource count from the hardware config. */
419 for (size_t i = 0; br->cfg->resource_specs[i].type != -1; i++)
422 /* Allocate space for a non-const copy of our resource_spec
423 * table; this will be updated with the RIDs assigned by
424 * bus_alloc_resources. */
425 br->res_spec = malloc(sizeof(br->res_spec[0]) * (res_num + 1), M_BHND,
427 if (br->res_spec == NULL) {
432 /* Initialize and terminate the table */
433 for (size_t i = 0; i < res_num; i++)
434 br->res_spec[i] = br->cfg->resource_specs[i];
436 br->res_spec[res_num].type = -1;
438 /* Allocate space for our resource references */
439 br->res = malloc(sizeof(br->res[0]) * res_num, M_BHND, M_NOWAIT);
440 if (br->res == NULL) {
445 /* Allocate host resources */
446 error = bus_alloc_resources(br->parent_dev, br->res_spec, br->res);
448 device_printf(br->dev,
449 "could not allocate bridge resources on %s: %d\n",
450 device_get_nameunit(br->parent_dev), error);
453 br->res_avail = true;
456 /* Populate (and validate) parent resource references for all
458 for (size_t i = 0; i < br->dwa_count; i++) {
459 struct bhndb_dw_alloc *dwa;
460 const struct bhndb_regwin *win;
462 dwa = &br->dw_alloc[i];
465 /* Find and validate corresponding resource. */
466 dwa->parent_res = bhndb_find_regwin_resource(br, win);
467 if (dwa->parent_res == NULL) {
468 device_printf(br->dev, "no host resource found for %u "
469 "register window with offset %#jx and "
472 (uintmax_t)win->win_offset,
473 (uintmax_t)win->win_size);
479 if (rman_get_size(dwa->parent_res) < win->win_offset +
482 device_printf(br->dev, "resource %d too small for "
483 "register window with offset %llx and size %llx\n",
484 rman_get_rid(dwa->parent_res),
485 (unsigned long long) win->win_offset,
486 (unsigned long long) win->win_size);
493 /* Add allocated memory resources to our host memory resource manager */
494 for (u_int i = 0; br->res_spec[i].type != -1; i++) {
495 struct resource *res;
497 /* skip non-memory resources */
498 if (br->res_spec[i].type != SYS_RES_MEMORY)
501 /* add host resource to set of managed regions */
503 error = rman_manage_region(&br->ht_mem_rman,
504 rman_get_start(res), rman_get_end(res));
506 device_printf(br->dev,
507 "could not register host memory region with "
508 "ht_mem_rman: %d\n", error);
517 bus_release_resources(br->parent_dev, br->res_spec, br->res);
520 free(br->res, M_BHND);
522 if (br->res_spec != NULL)
523 free(br->res_spec, M_BHND);
529 * Deallocate the given bridge resource structure and any associated resources.
531 * @param br Resource state to be deallocated.
534 bhndb_free_resources(struct bhndb_resources *br)
536 struct bhndb_region *region, *r_next;
537 struct bhndb_dw_alloc *dwa;
538 struct bhndb_dw_rentry *dwr, *dwr_next;
540 /* No window regions may still be held */
541 if (!bhndb_dw_all_free(br)) {
542 for (int i = 0; i < br->dwa_count; i++) {
543 dwa = &br->dw_alloc[i];
545 /* Skip free dynamic windows */
546 if (bhndb_dw_is_free(br, dwa))
549 device_printf(br->dev,
550 "leaked dynamic register window %d\n", dwa->rnid);
554 /* Release resources allocated through our parent. */
556 bus_release_resources(br->parent_dev, br->res_spec, br->res);
558 /* Clean up resource reservations */
559 for (size_t i = 0; i < br->dwa_count; i++) {
560 dwa = &br->dw_alloc[i];
562 LIST_FOREACH_SAFE(dwr, &dwa->refs, dw_link, dwr_next) {
563 LIST_REMOVE(dwr, dw_link);
568 /* Release bus regions */
569 STAILQ_FOREACH_SAFE(region, &br->bus_regions, link, r_next) {
570 STAILQ_REMOVE(&br->bus_regions, region, bhndb_region, link);
571 free(region, M_BHND);
574 /* Release our resource managers */
575 rman_fini(&br->ht_mem_rman);
576 rman_fini(&br->br_mem_rman);
578 /* Free backing resource state structures */
580 free(br->res, M_BHND);
582 if (br->res_spec != NULL)
583 free(br->res_spec, M_BHND);
585 free(br->dw_alloc, M_BHND);
586 free(br->dwa_freelist, M_BHND);
590 * Add a bus region entry to @p r for the given base @p addr and @p size.
592 * @param br The resource state to which the bus region entry will be added.
593 * @param addr The base address of this region.
594 * @param size The size of this region.
595 * @param priority The resource priority to be assigned to allocations
596 * made within this bus region.
597 * @param static_regwin If available, a static register window mapping this
598 * bus region entry. If not available, NULL.
601 * @retval non-zero if adding the bus region fails.
604 bhndb_add_resource_region(struct bhndb_resources *br, bhnd_addr_t addr,
605 bhnd_size_t size, bhndb_priority_t priority,
606 const struct bhndb_regwin *static_regwin)
608 struct bhndb_region *reg;
610 /* Insert in the bus resource list */
611 reg = malloc(sizeof(*reg), M_BHND, M_NOWAIT);
615 *reg = (struct bhndb_region) {
618 .priority = priority,
619 .static_regwin = static_regwin
622 STAILQ_INSERT_HEAD(&br->bus_regions, reg, link);
629 * Find the maximum start and end limits of the register window mapping
632 * If the memory range is not mapped by an existing dynamic or static register
633 * window, ENOENT will be returned.
635 * @param br The resource state to search.
636 * @param r The resource to search for in @p br.
637 * @param addr The requested starting address.
638 * @param size The requested size.
640 * @retval bhndb_region A region that fully contains the requested range.
641 * @retval NULL If no mapping region can be found.
644 bhndb_find_resource_limits(struct bhndb_resources *br, struct resource *r,
645 rman_res_t *start, rman_res_t *end)
647 struct bhndb_dw_alloc *dynamic;
648 struct bhndb_region *sregion;
650 /* Check for an enclosing dynamic register window */
651 if ((dynamic = bhndb_dw_find_resource(br, r))) {
652 *start = dynamic->target;
653 *end = dynamic->target + dynamic->win->win_size - 1;
657 /* Check for a static region */
658 sregion = bhndb_find_resource_region(br, rman_get_start(r),
660 if (sregion != NULL && sregion->static_regwin != NULL) {
661 *start = sregion->addr;
662 *end = sregion->addr + sregion->size - 1;
672 * Find the bus region that maps @p size bytes at @p addr.
674 * @param br The resource state to search.
675 * @param addr The requested starting address.
676 * @param size The requested size.
678 * @retval bhndb_region A region that fully contains the requested range.
679 * @retval NULL If no mapping region can be found.
681 struct bhndb_region *
682 bhndb_find_resource_region(struct bhndb_resources *br, bhnd_addr_t addr,
685 struct bhndb_region *region;
687 STAILQ_FOREACH(region, &br->bus_regions, link) {
688 /* Request must fit within the region's mapping */
689 if (addr < region->addr)
692 if (addr + size > region->addr + region->size)
703 * Find the entry matching @p r in @p dwa's references, if any.
705 * @param dwa The dynamic window allocation to search
706 * @param r The resource to search for in @p dwa.
708 static struct bhndb_dw_rentry *
709 bhndb_dw_find_resource_entry(struct bhndb_dw_alloc *dwa, struct resource *r)
711 struct bhndb_dw_rentry *rentry;
713 LIST_FOREACH(rentry, &dwa->refs, dw_link) {
714 struct resource *dw_res = rentry->dw_res;
716 /* Match dev/rid/addr/size */
717 if (rman_get_device(dw_res) != rman_get_device(r) ||
718 rman_get_rid(dw_res) != rman_get_rid(r) ||
719 rman_get_start(dw_res) != rman_get_start(r) ||
720 rman_get_size(dw_res) != rman_get_size(r))
725 /* Matching allocation found */
733 * Find the dynamic region allocated for @p r, if any.
735 * @param br The resource state to search.
736 * @param r The resource to search for.
738 * @retval bhndb_dw_alloc The allocation record for @p r.
739 * @retval NULL if no dynamic window is allocated for @p r.
741 struct bhndb_dw_alloc *
742 bhndb_dw_find_resource(struct bhndb_resources *br, struct resource *r)
744 struct bhndb_dw_alloc *dwa;
746 for (size_t i = 0; i < br->dwa_count; i++) {
747 dwa = &br->dw_alloc[i];
749 /* Skip free dynamic windows */
750 if (bhndb_dw_is_free(br, dwa))
753 /* Matching allocation found? */
754 if (bhndb_dw_find_resource_entry(dwa, r) != NULL)
762 * Find an existing dynamic window mapping @p size bytes
763 * at @p addr. The window may or may not be free.
765 * @param br The resource state to search.
766 * @param addr The requested starting address.
767 * @param size The requested size.
769 * @retval bhndb_dw_alloc A window allocation that fully contains the requested
771 * @retval NULL If no mapping region can be found.
773 struct bhndb_dw_alloc *
774 bhndb_dw_find_mapping(struct bhndb_resources *br, bhnd_addr_t addr,
777 struct bhndb_dw_alloc *dwr;
778 const struct bhndb_regwin *win;
780 /* Search for an existing dynamic mapping of this address range. */
781 for (size_t i = 0; i < br->dwa_count; i++) {
782 dwr = &br->dw_alloc[i];
785 /* Verify the range */
786 if (addr < dwr->target)
789 if (addr + size > dwr->target + win->win_size)
792 /* Found a usable mapping */
801 * Retain a reference to @p dwa for use by @p res.
803 * @param br The resource state owning @p dwa.
804 * @param dwa The allocation record to be retained.
805 * @param res The resource that will own a reference to @p dwa.
808 * @retval ENOMEM Failed to allocate a new reference structure.
811 bhndb_dw_retain(struct bhndb_resources *br, struct bhndb_dw_alloc *dwa,
812 struct resource *res)
814 struct bhndb_dw_rentry *rentry;
816 KASSERT(bhndb_dw_find_resource_entry(dwa, res) == NULL,
817 ("double-retain of dynamic window for same resource"));
819 /* Insert a reference entry; we use M_NOWAIT to allow use from
820 * within a non-sleepable lock */
821 rentry = malloc(sizeof(*rentry), M_BHND, M_NOWAIT);
825 rentry->dw_res = res;
826 LIST_INSERT_HEAD(&dwa->refs, rentry, dw_link);
828 /* Update the free list */
829 bit_set(br->dwa_freelist, dwa->rnid);
835 * Release a reference to @p dwa previously retained by @p res. If the
836 * reference count of @p dwa reaches zero, it will be added to the
839 * @param br The resource state owning @p dwa.
840 * @param dwa The allocation record to be released.
841 * @param res The resource that currently owns a reference to @p dwa.
844 bhndb_dw_release(struct bhndb_resources *br, struct bhndb_dw_alloc *dwa,
847 struct bhndb_dw_rentry *rentry;
849 /* Find the rentry */
850 rentry = bhndb_dw_find_resource_entry(dwa, r);
851 KASSERT(rentry != NULL, ("over release of resource entry"));
853 LIST_REMOVE(rentry, dw_link);
854 free(rentry, M_BHND);
856 /* If this was the last reference, update the free list */
857 if (LIST_EMPTY(&dwa->refs))
858 bit_clear(br->dwa_freelist, dwa->rnid);
862 * Attempt to set (or reset) the target address of @p dwa to map @p size bytes
865 * This will apply any necessary window alignment and verify that
866 * the window is capable of mapping the requested range prior to modifying
869 * @param dev The device on which to issue the BHNDB_SET_WINDOW_ADDR() request.
870 * @param br The resource state owning @p dwa.
871 * @param dwa The allocation record to be configured.
872 * @param addr The address to be mapped via @p dwa.
873 * @param size The number of bytes to be mapped at @p addr.
876 * @retval non-zero no usable register window available.
879 bhndb_dw_set_addr(device_t dev, struct bhndb_resources *br,
880 struct bhndb_dw_alloc *dwa, bus_addr_t addr, bus_size_t size)
882 const struct bhndb_regwin *rw;
888 KASSERT(bhndb_dw_is_free(br, dwa),
889 ("attempting to set the target address on an in-use window"));
891 /* Page-align the target address */
892 offset = addr % rw->win_size;
893 dwa->target = addr - offset;
895 /* Verify that the window is large enough for the full target */
896 if (rw->win_size - offset < size)
899 /* Update the window target */
900 error = BHNDB_SET_WINDOW_ADDR(dev, dwa->win, dwa->target);
910 * Return the count of @p type register windows in @p table.
912 * @param table The table to search.
913 * @param type The required window type, or BHNDB_REGWIN_T_INVALID to
914 * count all register window types.
917 bhndb_regwin_count(const struct bhndb_regwin *table,
918 bhndb_regwin_type_t type)
920 const struct bhndb_regwin *rw;
924 for (rw = table; rw->win_type != BHNDB_REGWIN_T_INVALID; rw++) {
925 if (type == BHNDB_REGWIN_T_INVALID || rw->win_type == type)
933 * Search @p table for the first window with the given @p type.
935 * @param table The table to search.
936 * @param type The required window type.
937 * @param min_size The minimum window size.
939 * @retval bhndb_regwin The first matching window.
940 * @retval NULL If no window of the requested type could be found.
942 const struct bhndb_regwin *
943 bhndb_regwin_find_type(const struct bhndb_regwin *table,
944 bhndb_regwin_type_t type, bus_size_t min_size)
946 const struct bhndb_regwin *rw;
948 for (rw = table; rw->win_type != BHNDB_REGWIN_T_INVALID; rw++)
950 if (rw->win_type == type && rw->win_size >= min_size)
958 * Search @p windows for the first matching core window.
960 * @param table The table to search.
961 * @param class The required core class.
962 * @param unit The required core unit, or -1.
963 * @param port_type The required port type.
964 * @param port The required port.
965 * @param region The required region.
967 * @retval bhndb_regwin The first matching window.
968 * @retval NULL If no matching window was found.
970 const struct bhndb_regwin *
971 bhndb_regwin_find_core(const struct bhndb_regwin *table, bhnd_devclass_t class,
972 int unit, bhnd_port_type port_type, u_int port, u_int region)
974 const struct bhndb_regwin *rw;
976 for (rw = table; rw->win_type != BHNDB_REGWIN_T_INVALID; rw++)
978 if (rw->win_type != BHNDB_REGWIN_T_CORE)
981 if (rw->d.core.class != class)
984 if (unit != -1 && rw->d.core.unit != unit)
987 if (rw->d.core.port_type != port_type)
990 if (rw->d.core.port != port)
993 if (rw->d.core.region != region)
1003 * Search @p windows for the best available window of at least @p min_size.
1006 * - BHND_REGWIN_T_CORE
1007 * - BHND_REGWIN_T_DYN
1009 * @param table The table to search.
1010 * @param class The required core class.
1011 * @param unit The required core unit, or -1.
1012 * @param port_type The required port type.
1013 * @param port The required port.
1014 * @param region The required region.
1015 * @param min_size The minimum window size.
1017 * @retval bhndb_regwin The first matching window.
1018 * @retval NULL If no matching window was found.
1020 const struct bhndb_regwin *
1021 bhndb_regwin_find_best(const struct bhndb_regwin *table,
1022 bhnd_devclass_t class, int unit, bhnd_port_type port_type, u_int port,
1023 u_int region, bus_size_t min_size)
1025 const struct bhndb_regwin *rw;
1027 /* Prefer a fixed core mapping */
1028 rw = bhndb_regwin_find_core(table, class, unit, port_type,
1033 /* Fall back on a generic dynamic window */
1034 return (bhndb_regwin_find_type(table, BHNDB_REGWIN_T_DYN, min_size));
1038 * Return true if @p regw defines a BHNDB_REGWIN_T_CORE register window
1039 * that matches against @p core.
1041 * @param regw A register window to match against.
1042 * @param core The bhnd(4) core info to match against @p regw.
1045 bhndb_regwin_match_core(const struct bhndb_regwin *regw,
1046 struct bhnd_core_info *core)
1048 /* Only core windows are supported */
1049 if (regw->win_type != BHNDB_REGWIN_T_CORE)
1052 /* Device class must match */
1053 if (bhnd_core_class(core) != regw->d.core.class)
1056 /* Device unit must match */
1057 if (core->unit != regw->d.core.unit)
1065 * Search for a core resource priority descriptor in @p table that matches
1068 * @param table The table to search.
1069 * @param core The core to match against @p table.
1071 const struct bhndb_hw_priority *
1072 bhndb_hw_priority_find_core(const struct bhndb_hw_priority *table,
1073 struct bhnd_core_info *core)
1075 const struct bhndb_hw_priority *hp;
1077 for (hp = table; hp->ports != NULL; hp++) {
1078 if (bhnd_core_matches(core, &hp->match))