xref: /freebsd/sys/dev/bhnd/bhndb/bhndb.c (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
1 /*-
2  * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3  * Copyright (c) 2017 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Landon Fuller
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
19  *
20  * NO WARRANTY
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.
32  */
33 
34 #include <sys/cdefs.h>
35 /*
36  * Abstract BHND Bridge Device Driver
37  *
38  * Provides generic support for bridging from a parent bus (such as PCI) to
39  * a BHND-compatible bus (e.g. bcma or siba).
40  */
41 
42 #include <sys/param.h>
43 #include <sys/kernel.h>
44 #include <sys/bus.h>
45 #include <sys/module.h>
46 #include <sys/sbuf.h>
47 #include <sys/systm.h>
48 
49 #include <machine/bus.h>
50 #include <sys/rman.h>
51 #include <machine/resource.h>
52 
53 #include <dev/bhnd/bhndvar.h>
54 #include <dev/bhnd/bhndreg.h>
55 
56 #include <dev/bhnd/bhnd_erom.h>
57 
58 #include <dev/bhnd/cores/chipc/chipcreg.h>
59 #include <dev/bhnd/nvram/bhnd_nvram.h>
60 
61 #include "bhnd_chipc_if.h"
62 #include "bhnd_nvram_if.h"
63 
64 #include "bhndbvar.h"
65 #include "bhndb_bus_if.h"
66 #include "bhndb_hwdata.h"
67 #include "bhndb_private.h"
68 
69 /* Debugging flags */
70 static u_long bhndb_debug = 0;
71 TUNABLE_ULONG("hw.bhndb.debug", &bhndb_debug);
72 
73 enum {
74 	BHNDB_DEBUG_PRIO = 1 << 0,
75 };
76 
77 #define	BHNDB_DEBUG(_type)	(BHNDB_DEBUG_ ## _type & bhndb_debug)
78 
79 static bool			 bhndb_hw_matches(struct bhndb_softc *sc,
80 				     struct bhnd_core_info *cores, u_int ncores,
81 				     const struct bhndb_hw *hw);
82 
83 static int			 bhndb_init_region_cfg(struct bhndb_softc *sc,
84 				     bhnd_erom_t *erom,
85 				     struct bhndb_resources *r,
86 				     struct bhnd_core_info *cores, u_int ncores,
87 				     const struct bhndb_hw_priority *table);
88 
89 static int			 bhndb_find_hwspec(struct bhndb_softc *sc,
90 				     struct bhnd_core_info *cores, u_int ncores,
91 				     const struct bhndb_hw **hw);
92 
93 bhndb_addrspace			 bhndb_get_addrspace(struct bhndb_softc *sc,
94 				     device_t child);
95 
96 static struct rman		*bhndb_get_rman(struct bhndb_softc *sc,
97 				     device_t child, int type);
98 
99 static int			 bhndb_init_child_resource(struct resource *r,
100 				     struct resource *parent,
101 				     bhnd_size_t offset,
102 				     bhnd_size_t size);
103 
104 static int			 bhndb_activate_static_region(
105 				     struct bhndb_softc *sc,
106 				     struct bhndb_region *region,
107 				     device_t child, struct resource *r);
108 
109 static int			 bhndb_try_activate_resource(
110 				     struct bhndb_softc *sc, device_t child,
111 				     struct resource *r, bool *indirect);
112 
113 static inline struct bhndb_dw_alloc *bhndb_io_resource(struct bhndb_softc *sc,
114 					bus_addr_t addr, bus_size_t size,
115 					bus_size_t *offset, bool *stolen,
116 					bus_addr_t *restore);
117 
118 /**
119  * Default bhndb(4) implementation of DEVICE_PROBE().
120  *
121  * This function provides the default bhndb implementation of DEVICE_PROBE(),
122  * and is compatible with bhndb(4) bridges attached via bhndb_attach_bridge().
123  */
124 int
125 bhndb_generic_probe(device_t dev)
126 {
127 	return (BUS_PROBE_NOWILDCARD);
128 }
129 
130 static void
131 bhndb_probe_nomatch(device_t dev, device_t child)
132 {
133 	const char *name;
134 
135 	name = device_get_name(child);
136 	if (name == NULL)
137 		name = "unknown device";
138 
139 	device_printf(dev, "<%s> (no driver attached)\n", name);
140 }
141 
142 static int
143 bhndb_print_child(device_t dev, device_t child)
144 {
145 	struct resource_list	*rl;
146 	int			 retval = 0;
147 
148 	retval += bus_print_child_header(dev, child);
149 
150 	rl = BUS_GET_RESOURCE_LIST(dev, child);
151 	if (rl != NULL) {
152 		retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY,
153 		    "%#jx");
154 		retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ,
155 		    "%jd");
156 	}
157 
158 	retval += bus_print_child_domain(dev, child);
159 	retval += bus_print_child_footer(dev, child);
160 
161 	return (retval);
162 }
163 
164 static int
165 bhndb_child_location(device_t dev, device_t child, struct sbuf *sb)
166 {
167 	struct bhndb_softc *sc;
168 
169 	sc = device_get_softc(dev);
170 
171 	sbuf_printf(sb, "base=0x%llx",
172 	    (unsigned long long) sc->chipid.enum_addr);
173 	return (0);
174 }
175 
176 /**
177  * Return true if @p cores matches the @p hw specification.
178  *
179  * @param sc BHNDB device state.
180  * @param cores A device table to match against.
181  * @param ncores The number of cores in @p cores.
182  * @param hw The hardware description to be matched against.
183  */
184 static bool
185 bhndb_hw_matches(struct bhndb_softc *sc, struct bhnd_core_info *cores,
186     u_int ncores, const struct bhndb_hw *hw)
187 {
188 	for (u_int i = 0; i < hw->num_hw_reqs; i++) {
189 		const struct bhnd_core_match	*match;
190 		bool				 found;
191 
192 		match =  &hw->hw_reqs[i];
193 		found = false;
194 
195 		for (u_int d = 0; d < ncores; d++) {
196 			struct bhnd_core_info *core = &cores[d];
197 
198 			if (BHNDB_IS_CORE_DISABLED(sc->dev, sc->bus_dev, core))
199 				continue;
200 
201 			if (!bhnd_core_matches(core, match))
202 				continue;
203 
204 			found = true;
205 			break;
206 		}
207 
208 		if (!found)
209 			return (false);
210 	}
211 
212 	return (true);
213 }
214 
215 /**
216  * Initialize the region maps and priority configuration in @p br using
217  * the priority @p table and the set of cores enumerated by @p erom.
218  *
219  * @param sc The bhndb device state.
220  * @param br The resource state to be configured.
221  * @param erom EROM parser used to enumerate @p cores.
222  * @param cores All cores enumerated on the bridged bhnd bus.
223  * @param ncores The length of @p cores.
224  * @param table Hardware priority table to be used to determine the relative
225  * priorities of per-core port resources.
226  */
227 static int
228 bhndb_init_region_cfg(struct bhndb_softc *sc, bhnd_erom_t *erom,
229     struct bhndb_resources *br, struct bhnd_core_info *cores, u_int ncores,
230     const struct bhndb_hw_priority *table)
231 {
232 	const struct bhndb_hw_priority	*hp;
233 	bhnd_addr_t			 addr;
234 	bhnd_size_t			 size;
235 	size_t				 prio_low, prio_default, prio_high;
236 	int				 error;
237 
238 	/* The number of port regions per priority band that must be accessible
239 	 * via dynamic register windows */
240 	prio_low = 0;
241 	prio_default = 0;
242 	prio_high = 0;
243 
244 	/*
245 	 * Register bridge regions covering all statically mapped ports.
246 	 */
247 	for (u_int i = 0; i < ncores; i++) {
248 		const struct bhndb_regwin	*regw;
249 		struct bhnd_core_info		*core;
250 		struct bhnd_core_match		 md;
251 
252 		core = &cores[i];
253 		md = bhnd_core_get_match_desc(core);
254 
255 		for (regw = br->cfg->register_windows;
256 		    regw->win_type != BHNDB_REGWIN_T_INVALID; regw++)
257 		{
258 			const struct bhndb_port_priority	*pp;
259 			uint32_t				 alloc_flags;
260 
261 			/* Only core windows are supported */
262 			if (regw->win_type != BHNDB_REGWIN_T_CORE)
263 				continue;
264 
265 			/* Skip non-matching cores. */
266 			if (!bhndb_regwin_match_core(regw, core))
267 				continue;
268 
269 			/* Fetch the base address of the mapped port */
270 			error = bhnd_erom_lookup_core_addr(erom, &md,
271 			    regw->d.core.port_type,
272 			    regw->d.core.port,
273 			    regw->d.core.region,
274 			    NULL,
275 			    &addr,
276 			    &size);
277 			if (error) {
278 				/* Skip non-applicable register windows */
279 				if (error == ENOENT)
280 					continue;
281 
282 				return (error);
283 			}
284 
285 			/*
286 			 * Apply the register window's region offset, if any.
287 			 */
288 			if (regw->d.core.offset > size) {
289 				device_printf(sc->dev, "invalid register "
290 				    "window offset %#jx for region %#jx+%#jx\n",
291 				    regw->d.core.offset, addr, size);
292 				return (EINVAL);
293 			}
294 
295 			addr += regw->d.core.offset;
296 
297 			/*
298 			 * Always defer to the register window's size.
299 			 *
300 			 * If the port size is smaller than the window size,
301 			 * this ensures that we fully utilize register windows
302 			 * larger than the referenced port.
303 			 *
304 			 * If the port size is larger than the window size, this
305 			 * ensures that we do not directly map the allocations
306 			 * within the region to a too-small window.
307 			 */
308 			size = regw->win_size;
309 
310 			/* Fetch allocation flags from the corresponding port
311 			 * priority entry, if any */
312 			pp = bhndb_hw_priorty_find_port(table, core,
313 			    regw->d.core.port_type, regw->d.core.port,
314 			    regw->d.core.region);
315 			if (pp != NULL) {
316 				alloc_flags = pp->alloc_flags;
317 			} else {
318 				alloc_flags = 0;
319 			}
320 
321 			/*
322 			 * Add to the bus region list.
323 			 *
324 			 * The window priority for a statically mapped region is
325 			 * always HIGH.
326 			 */
327 			error = bhndb_add_resource_region(br, addr, size,
328 			    BHNDB_PRIORITY_HIGH, alloc_flags, regw);
329 			if (error)
330 				return (error);
331 		}
332 	}
333 
334 	/*
335 	 * Perform priority accounting and register bridge regions for all
336 	 * ports defined in the priority table
337 	 */
338 	for (u_int i = 0; i < ncores; i++) {
339 		struct bhnd_core_info	*core;
340 		struct bhnd_core_match	 md;
341 
342 		core = &cores[i];
343 		md = bhnd_core_get_match_desc(core);
344 
345 		/*
346 		 * Skip priority accounting for cores that ...
347 		 */
348 
349 		/* ... do not require bridge resources */
350 		if (BHNDB_IS_CORE_DISABLED(sc->dev, sc->bus_dev, core))
351 			continue;
352 
353 		/* ... do not have a priority table entry */
354 		hp = bhndb_hw_priority_find_core(table, core);
355 		if (hp == NULL)
356 			continue;
357 
358 		/* ... are explicitly disabled in the priority table. */
359 		if (hp->priority == BHNDB_PRIORITY_NONE)
360 			continue;
361 
362 		/* Determine the number of dynamic windows required and
363 		 * register their bus_region entries. */
364 		for (u_int i = 0; i < hp->num_ports; i++) {
365 			const struct bhndb_port_priority *pp;
366 
367 			pp = &hp->ports[i];
368 
369 			/* Fetch the address+size of the mapped port. */
370 			error = bhnd_erom_lookup_core_addr(erom, &md,
371 			    pp->type, pp->port, pp->region,
372 			    NULL, &addr, &size);
373 			if (error) {
374 				/* Skip ports not defined on this device */
375 				if (error == ENOENT)
376 					continue;
377 
378 				return (error);
379 			}
380 
381 			/* Skip ports with an existing static mapping */
382 			if (bhndb_has_static_region_mapping(br, addr, size))
383 				continue;
384 
385 			/* Define a dynamic region for this port */
386 			error = bhndb_add_resource_region(br, addr, size,
387 			    pp->priority, pp->alloc_flags, NULL);
388 			if (error)
389 				return (error);
390 
391 			/* Update port mapping counts */
392 			switch (pp->priority) {
393 			case BHNDB_PRIORITY_NONE:
394 				break;
395 			case BHNDB_PRIORITY_LOW:
396 				prio_low++;
397 				break;
398 			case BHNDB_PRIORITY_DEFAULT:
399 				prio_default++;
400 				break;
401 			case BHNDB_PRIORITY_HIGH:
402 				prio_high++;
403 				break;
404 			}
405 		}
406 	}
407 
408 	/* Determine the minimum priority at which we'll allocate direct
409 	 * register windows from our dynamic pool */
410 	size_t prio_total = prio_low + prio_default + prio_high;
411 	if (prio_total <= br->dwa_count) {
412 		/* low+default+high priority regions get windows */
413 		br->min_prio = BHNDB_PRIORITY_LOW;
414 
415 	} else if (prio_default + prio_high <= br->dwa_count) {
416 		/* default+high priority regions get windows */
417 		br->min_prio = BHNDB_PRIORITY_DEFAULT;
418 
419 	} else {
420 		/* high priority regions get windows */
421 		br->min_prio = BHNDB_PRIORITY_HIGH;
422 	}
423 
424 	if (BHNDB_DEBUG(PRIO)) {
425 		struct bhndb_region	*region;
426 		const char		*direct_msg, *type_msg;
427 		bhndb_priority_t	 prio, prio_min;
428 		uint32_t		 flags;
429 
430 		prio_min = br->min_prio;
431 		device_printf(sc->dev, "min_prio: %d\n", prio_min);
432 
433 		STAILQ_FOREACH(region, &br->bus_regions, link) {
434 			prio = region->priority;
435 			flags = region->alloc_flags;
436 
437 			direct_msg = prio >= prio_min ? "direct" : "indirect";
438 			type_msg = region->static_regwin ? "static" : "dynamic";
439 
440 			device_printf(sc->dev, "region 0x%llx+0x%llx priority "
441 			    "%u %s/%s",
442 			    (unsigned long long) region->addr,
443 			    (unsigned long long) region->size,
444 			    region->priority,
445 			    direct_msg, type_msg);
446 
447 			if (flags & BHNDB_ALLOC_FULFILL_ON_OVERCOMMIT)
448 				printf(" [overcommit]\n");
449 			else
450 				printf("\n");
451 		}
452 	}
453 
454 	return (0);
455 }
456 
457 /**
458  * Find a hardware specification for @p dev.
459  *
460  * @param sc The bhndb device state.
461  * @param cores All cores enumerated on the bridged bhnd bus.
462  * @param ncores The length of @p cores.
463  * @param[out] hw On success, the matched hardware specification.
464  * with @p dev.
465  *
466  * @retval 0 success
467  * @retval non-zero if an error occurs fetching device info for comparison.
468  */
469 static int
470 bhndb_find_hwspec(struct bhndb_softc *sc, struct bhnd_core_info *cores,
471     u_int ncores, const struct bhndb_hw **hw)
472 {
473 	const struct bhndb_hw	*next, *hw_table;
474 
475 	/* Search for the first matching hardware config. */
476 	hw_table = BHNDB_BUS_GET_HARDWARE_TABLE(sc->parent_dev, sc->dev);
477 	for (next = hw_table; next->hw_reqs != NULL; next++) {
478 		if (!bhndb_hw_matches(sc, cores, ncores, next))
479 			continue;
480 
481 		/* Found */
482 		*hw = next;
483 		return (0);
484 	}
485 
486 	return (ENOENT);
487 }
488 
489 /**
490  * Helper function that must be called by subclass bhndb(4) drivers
491  * when implementing DEVICE_ATTACH() before calling any bhnd(4) or bhndb(4)
492  * APIs on the bridge device.
493  *
494  * This function will add a bridged bhnd(4) child device with a device order of
495  * BHND_PROBE_BUS. Any subclass bhndb(4) driver may use the BHND_PROBE_*
496  * priority bands to add additional devices that will be attached in
497  * their preferred order relative to the bridged bhnd(4) bus.
498  *
499  * @param dev		The bridge device to attach.
500  * @param cid		The bridged device's chip identification.
501  * @param cores		The bridged device's core table.
502  * @param ncores	The number of cores in @p cores.
503  * @param bridge_core	Core info for the bhnd(4) core serving as the host
504  *			bridge.
505  * @param erom_class	An erom parser class that may be used to parse
506  *			the bridged device's device enumeration table.
507  */
508 int
509 bhndb_attach(device_t dev, struct bhnd_chipid *cid,
510     struct bhnd_core_info *cores, u_int ncores,
511     struct bhnd_core_info *bridge_core, bhnd_erom_class_t *erom_class)
512 {
513 	struct bhndb_devinfo		*dinfo;
514 	struct bhndb_softc		*sc;
515 	const struct bhndb_hw		*hw;
516 	const struct bhndb_hwcfg	*hwcfg;
517 	const struct bhndb_hw_priority	*hwprio;
518 	struct bhnd_erom_io		*eio;
519 	bhnd_erom_t			*erom;
520 	int				 error;
521 
522 	sc = device_get_softc(dev);
523 	sc->dev = dev;
524 	sc->parent_dev = device_get_parent(dev);
525 	sc->bridge_core = *bridge_core;
526 	sc->chipid = *cid;
527 
528 	if ((error = bhnd_service_registry_init(&sc->services)))
529 		return (error);
530 
531 	BHNDB_LOCK_INIT(sc);
532 
533 	erom = NULL;
534 
535 	/* Find a matching bridge hardware configuration */
536 	if ((error = bhndb_find_hwspec(sc, cores, ncores, &hw))) {
537 		device_printf(sc->dev, "unable to identify device, "
538 		    " using generic bridge resource definitions\n");
539 
540 		hwcfg = BHNDB_BUS_GET_GENERIC_HWCFG(sc->parent_dev, dev);
541 		hw = NULL;
542 	} else {
543 		hwcfg = hw->cfg;
544 	}
545 
546 	if (hw != NULL && (bootverbose || BHNDB_DEBUG(PRIO))) {
547 		device_printf(sc->dev, "%s resource configuration\n", hw->name);
548 	}
549 
550 	/* Allocate bridge resource state using the discovered hardware
551 	 * configuration */
552 	sc->bus_res = bhndb_alloc_resources(sc->dev, sc->parent_dev, hwcfg);
553 	if (sc->bus_res == NULL) {
554 		device_printf(sc->dev, "failed to allocate bridge resource "
555 		    "state\n");
556 		error = ENOMEM;
557 		goto failed;
558 	}
559 
560 	/* Add our bridged bus device */
561 	sc->bus_dev = BUS_ADD_CHILD(dev, BHND_PROBE_BUS, "bhnd", DEVICE_UNIT_ANY);
562 	if (sc->bus_dev == NULL) {
563 		error = ENXIO;
564 		goto failed;
565 	}
566 
567 	dinfo = device_get_ivars(sc->bus_dev);
568 	dinfo->addrspace = BHNDB_ADDRSPACE_BRIDGED;
569 
570 	/* We can now use bhndb to perform bridging of SYS_RES_MEMORY resources;
571 	 * we use this to instantiate an erom parser instance */
572 	eio = bhnd_erom_iores_new(sc->bus_dev, 0);
573 	if ((erom = bhnd_erom_alloc(erom_class, cid, eio)) == NULL) {
574 		bhnd_erom_io_fini(eio);
575 		error = ENXIO;
576 		goto failed;
577 	}
578 
579 	/* Populate our resource priority configuration */
580 	hwprio = BHNDB_BUS_GET_HARDWARE_PRIO(sc->parent_dev, sc->dev);
581 	error = bhndb_init_region_cfg(sc, erom, sc->bus_res, cores, ncores,
582 	    hwprio);
583 	if (error) {
584 		device_printf(sc->dev, "failed to initialize resource "
585 		    "priority configuration: %d\n", error);
586 		goto failed;
587 	}
588 
589 	/* Free our erom instance */
590 	bhnd_erom_free(erom);
591 	erom = NULL;
592 
593 	return (0);
594 
595 failed:
596 	BHNDB_LOCK_DESTROY(sc);
597 
598 	if (sc->bus_res != NULL)
599 		bhndb_free_resources(sc->bus_res);
600 
601 	if (erom != NULL)
602 		bhnd_erom_free(erom);
603 
604 	bhnd_service_registry_fini(&sc->services);
605 
606 	return (error);
607 }
608 
609 /**
610  * Default bhndb(4) implementation of DEVICE_DETACH().
611  *
612  * This function detaches any child devices, and if successful, releases all
613  * resources held by the bridge device.
614  */
615 int
616 bhndb_generic_detach(device_t dev)
617 {
618 	struct bhndb_softc	*sc;
619 	int			 error;
620 
621 	sc = device_get_softc(dev);
622 
623 	/* Detach children */
624 	if ((error = bus_generic_detach(dev)))
625 		return (error);
626 
627 	/* Delete children */
628 	if ((error = device_delete_children(dev)))
629 		return (error);
630 
631 	/* Clean up our service registry */
632 	if ((error = bhnd_service_registry_fini(&sc->services)))
633 		return (error);
634 
635 	/* Clean up our driver state. */
636 	bhndb_free_resources(sc->bus_res);
637 
638 	BHNDB_LOCK_DESTROY(sc);
639 
640 	return (0);
641 }
642 
643 /**
644  * Default bhndb(4) implementation of DEVICE_SUSPEND().
645  *
646  * This function calls bus_generic_suspend() (or implements equivalent
647  * behavior).
648  */
649 int
650 bhndb_generic_suspend(device_t dev)
651 {
652 	return (bus_generic_suspend(dev));
653 }
654 
655 /**
656  * Default bhndb(4) implementation of DEVICE_RESUME().
657  *
658  * This function calls bus_generic_resume() (or implements equivalent
659  * behavior).
660  */
661 int
662 bhndb_generic_resume(device_t dev)
663 {
664 	struct bhndb_softc	*sc;
665 	struct bhndb_resources	*bus_res;
666 	struct bhndb_dw_alloc	*dwa;
667 	int			 error;
668 
669 	sc = device_get_softc(dev);
670 	bus_res = sc->bus_res;
671 
672 	/* Guarantee that all in-use dynamic register windows are mapped to
673 	 * their previously configured target address. */
674 	BHNDB_LOCK(sc);
675 	error = 0;
676 	for (size_t i = 0; i < bus_res->dwa_count; i++) {
677 		dwa = &bus_res->dw_alloc[i];
678 
679 		/* Skip regions that were not previously used */
680 		if (bhndb_dw_is_free(bus_res, dwa) && dwa->target == 0x0)
681 			continue;
682 
683 		/* Otherwise, ensure the register window is correct before
684 		 * any children attempt MMIO */
685 		error = BHNDB_SET_WINDOW_ADDR(dev, dwa->win, dwa->target);
686 		if (error)
687 			break;
688 	}
689 	BHNDB_UNLOCK(sc);
690 
691 	/* Error restoring hardware state; children cannot be safely resumed */
692 	if (error) {
693 		device_printf(dev, "Unable to restore hardware configuration; "
694 		    "cannot resume: %d\n", error);
695 		return (error);
696 	}
697 
698 	return (bus_generic_resume(dev));
699 }
700 
701 /**
702  * Default implementation of BHNDB_SUSPEND_RESOURCE.
703  */
704 static void
705 bhndb_suspend_resource(device_t dev, device_t child, int type,
706     struct resource *r)
707 {
708 	struct bhndb_softc	*sc;
709 	struct bhndb_dw_alloc	*dwa;
710 
711 	sc = device_get_softc(dev);
712 
713 	/* Non-MMIO resources (e.g. IRQs) are handled solely by our parent */
714 	if (type != SYS_RES_MEMORY)
715 		return;
716 
717 	BHNDB_LOCK(sc);
718 	dwa = bhndb_dw_find_resource(sc->bus_res, r);
719 	if (dwa == NULL) {
720 		BHNDB_UNLOCK(sc);
721 		return;
722 	}
723 
724 	if (BHNDB_DEBUG(PRIO))
725 		device_printf(child, "suspend resource type=%d 0x%jx+0x%jx\n",
726 		    type, rman_get_start(r), rman_get_size(r));
727 
728 	/* Release the resource's window reference */
729 	bhndb_dw_release(sc->bus_res, dwa, r);
730 	BHNDB_UNLOCK(sc);
731 }
732 
733 /**
734  * Default implementation of BHNDB_RESUME_RESOURCE.
735  */
736 static int
737 bhndb_resume_resource(device_t dev, device_t child, int type,
738     struct resource *r)
739 {
740 	struct bhndb_softc	*sc;
741 
742 	sc = device_get_softc(dev);
743 
744 	/* Non-MMIO resources (e.g. IRQs) are handled solely by our parent */
745 	if (type != SYS_RES_MEMORY)
746 		return (0);
747 
748 	/* Inactive resources don't require reallocation of bridge resources */
749 	if (!(rman_get_flags(r) & RF_ACTIVE))
750 		return (0);
751 
752 	if (BHNDB_DEBUG(PRIO))
753 		device_printf(child, "resume resource type=%d 0x%jx+0x%jx\n",
754 		    type, rman_get_start(r), rman_get_size(r));
755 
756 	return (bhndb_try_activate_resource(sc, rman_get_device(r), r, NULL));
757 }
758 
759 /**
760  * Default bhndb(4) implementation of BUS_READ_IVAR().
761  */
762 static int
763 bhndb_read_ivar(device_t dev, device_t child, int index,
764     uintptr_t *result)
765 {
766 	return (ENOENT);
767 }
768 
769 /**
770  * Default bhndb(4) implementation of BUS_WRITE_IVAR().
771  */
772 static int
773 bhndb_write_ivar(device_t dev, device_t child, int index,
774     uintptr_t value)
775 {
776 	return (ENOENT);
777 }
778 
779 /**
780  * Return the address space for the given @p child device.
781  */
782 bhndb_addrspace
783 bhndb_get_addrspace(struct bhndb_softc *sc, device_t child)
784 {
785 	struct bhndb_devinfo	*dinfo;
786 	device_t		 imd_dev;
787 
788 	/* Find the directly attached parent of the requesting device */
789 	imd_dev = child;
790 	while (imd_dev != NULL && device_get_parent(imd_dev) != sc->dev)
791 		imd_dev = device_get_parent(imd_dev);
792 
793 	if (imd_dev == NULL)
794 		panic("bhndb address space request for non-child device %s\n",
795 		     device_get_nameunit(child));
796 
797 	dinfo = device_get_ivars(imd_dev);
798 	return (dinfo->addrspace);
799 }
800 
801 /**
802  * Return the rman instance for a given resource @p type, if any.
803  *
804  * @param sc The bhndb device state.
805  * @param child The requesting child.
806  * @param type The resource type (e.g. SYS_RES_MEMORY, SYS_RES_IRQ, ...)
807  */
808 static struct rman *
809 bhndb_get_rman(struct bhndb_softc *sc, device_t child, int type)
810 {
811 	switch (bhndb_get_addrspace(sc, child)) {
812 	case BHNDB_ADDRSPACE_NATIVE:
813 		switch (type) {
814 		case SYS_RES_MEMORY:
815 			return (&sc->bus_res->ht_mem_rman);
816 		case SYS_RES_IRQ:
817 			return (NULL);
818 		default:
819 			return (NULL);
820 		}
821 
822 	case BHNDB_ADDRSPACE_BRIDGED:
823 		switch (type) {
824 		case SYS_RES_MEMORY:
825 			return (&sc->bus_res->br_mem_rman);
826 		case SYS_RES_IRQ:
827 			return (&sc->bus_res->br_irq_rman);
828 		default:
829 			return (NULL);
830 		}
831 	}
832 
833 	/* Quieten gcc */
834 	return (NULL);
835 }
836 
837 /**
838  * Default implementation of BUS_ADD_CHILD()
839  */
840 static device_t
841 bhndb_add_child(device_t dev, u_int order, const char *name, int unit)
842 {
843 	struct bhndb_devinfo	*dinfo;
844 	device_t		 child;
845 
846 	child = device_add_child_ordered(dev, order, name, unit);
847 	if (child == NULL)
848 		return (NULL);
849 
850 	dinfo = malloc(sizeof(struct bhndb_devinfo), M_BHND, M_NOWAIT);
851 	if (dinfo == NULL) {
852 		device_delete_child(dev, child);
853 		return (NULL);
854 	}
855 
856 	dinfo->addrspace = BHNDB_ADDRSPACE_NATIVE;
857 	resource_list_init(&dinfo->resources);
858 
859 	device_set_ivars(child, dinfo);
860 
861 	return (child);
862 }
863 
864 /**
865  * Default implementation of BUS_CHILD_DELETED().
866  */
867 static void
868 bhndb_child_deleted(device_t dev, device_t child)
869 {
870 	struct bhndb_devinfo *dinfo = device_get_ivars(child);
871 	if (dinfo != NULL) {
872 		resource_list_free(&dinfo->resources);
873 		free(dinfo, M_BHND);
874 	}
875 
876 	device_set_ivars(child, NULL);
877 }
878 
879 /**
880  * Default implementation of BHNDB_GET_CHIPID().
881  */
882 static const struct bhnd_chipid *
883 bhndb_get_chipid(device_t dev, device_t child)
884 {
885 	struct bhndb_softc *sc = device_get_softc(dev);
886 	return (&sc->chipid);
887 }
888 
889 /**
890  * Default implementation of BHNDB_IS_CORE_DISABLED().
891  */
892 static bool
893 bhndb_is_core_disabled(device_t dev, device_t child,
894     struct bhnd_core_info *core)
895 {
896 	struct bhndb_softc	*sc;
897 
898 	sc = device_get_softc(dev);
899 
900 	/* Try to defer to the bhndb bus parent */
901 	if (BHNDB_BUS_IS_CORE_DISABLED(sc->parent_dev, dev, core))
902 		return (true);
903 
904 	/* Otherwise, we treat bridge-capable cores as unpopulated if they're
905 	 * not the configured host bridge */
906 	if (BHND_DEVCLASS_SUPPORTS_HOSTB(bhnd_core_class(core)))
907 		return (!bhnd_cores_equal(core, &sc->bridge_core));
908 
909 	/* Assume the core is populated */
910 	return (false);
911 }
912 
913 /**
914  * Default bhndb(4) implementation of BHNDB_GET_HOSTB_CORE().
915  *
916  * This function uses a heuristic valid on all known PCI/PCIe/PCMCIA-bridged
917  * bhnd(4) devices.
918  */
919 static int
920 bhndb_get_hostb_core(device_t dev, device_t child, struct bhnd_core_info *core)
921 {
922 	struct bhndb_softc *sc = device_get_softc(dev);
923 
924 	*core = sc->bridge_core;
925 	return (0);
926 }
927 
928 /**
929  * Default bhndb(4) implementation of BHND_BUS_GET_SERVICE_REGISTRY().
930  */
931 static struct bhnd_service_registry *
932 bhndb_get_service_registry(device_t dev, device_t child)
933 {
934 	struct bhndb_softc *sc = device_get_softc(dev);
935 
936 	return (&sc->services);
937 }
938 
939 /**
940  * Default bhndb(4) implementation of BUS_ALLOC_RESOURCE().
941  */
942 static struct resource *
943 bhndb_alloc_resource(device_t dev, device_t child, int type,
944     int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
945 {
946 	struct bhndb_softc		*sc;
947 	struct resource_list_entry	*rle;
948 	struct resource			*rv;
949 	struct rman			*rm;
950 	int				 error;
951 	bool				 passthrough, isdefault;
952 
953 	sc = device_get_softc(dev);
954 	passthrough = (device_get_parent(child) != dev);
955 	isdefault = RMAN_IS_DEFAULT_RANGE(start, end);
956 	rle = NULL;
957 
958 	/* Fetch the resource manager */
959 	rm = bhndb_get_rman(sc, child, type);
960 	if (rm == NULL) {
961 		/* Delegate to our parent device's bus; the requested
962 		 * resource type isn't handled locally. */
963 		return (BUS_ALLOC_RESOURCE(device_get_parent(sc->parent_dev),
964 		    child, type, rid,  start, end, count, flags));
965 	}
966 
967 	/* Populate defaults */
968 	if (!passthrough && isdefault) {
969 		/* Fetch the resource list entry. */
970 		rle = resource_list_find(BUS_GET_RESOURCE_LIST(dev, child),
971 		    type, *rid);
972 		if (rle == NULL) {
973 			device_printf(dev,
974 			    "default resource %#x type %d for child %s "
975 			    "not found\n", *rid, type,
976 			    device_get_nameunit(child));
977 
978 			return (NULL);
979 		}
980 
981 		if (rle->res != NULL) {
982 			device_printf(dev,
983 			    "resource entry %#x type %d for child %s is busy\n",
984 			    *rid, type, device_get_nameunit(child));
985 
986 			return (NULL);
987 		}
988 
989 		start = rle->start;
990 		end = rle->end;
991 		count = ulmax(count, rle->count);
992 	}
993 
994 	/* Validate resource addresses */
995 	if (start > end || count > ((end - start) + 1))
996 		return (NULL);
997 
998 	/* Make our reservation */
999 	rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
1000 	    child);
1001 	if (rv == NULL)
1002 		return (NULL);
1003 
1004 	rman_set_rid(rv, *rid);
1005 	rman_set_type(rv, type);
1006 
1007 	/* Activate */
1008 	if (flags & RF_ACTIVE) {
1009 		error = bus_activate_resource(child, type, *rid, rv);
1010 		if (error) {
1011 			device_printf(dev,
1012 			    "failed to activate entry %#x type %d for "
1013 				"child %s: %d\n",
1014 			     *rid, type, device_get_nameunit(child), error);
1015 
1016 			rman_release_resource(rv);
1017 
1018 			return (NULL);
1019 		}
1020 	}
1021 
1022 	/* Update child's resource list entry */
1023 	if (rle != NULL) {
1024 		rle->res = rv;
1025 		rle->start = rman_get_start(rv);
1026 		rle->end = rman_get_end(rv);
1027 		rle->count = rman_get_size(rv);
1028 	}
1029 
1030 	return (rv);
1031 }
1032 
1033 /**
1034  * Default bhndb(4) implementation of BUS_RELEASE_RESOURCE().
1035  */
1036 static int
1037 bhndb_release_resource(device_t dev, device_t child, struct resource *r)
1038 {
1039 	struct bhndb_softc		*sc;
1040 	struct resource_list_entry	*rle;
1041 	bool				 passthrough;
1042 	int				 error;
1043 
1044 	sc = device_get_softc(dev);
1045 	passthrough = (device_get_parent(child) != dev);
1046 
1047 	/* Delegate to our parent device's bus if the requested resource type
1048 	 * isn't handled locally. */
1049 	if (bhndb_get_rman(sc, child, rman_get_type(r)) == NULL) {
1050 		return (BUS_RELEASE_RESOURCE(device_get_parent(sc->parent_dev),
1051 		    child, r));
1052 	}
1053 
1054 	/* Deactivate resources */
1055 	if (rman_get_flags(r) & RF_ACTIVE) {
1056 		error = BUS_DEACTIVATE_RESOURCE(dev, child, r);
1057 		if (error)
1058 			return (error);
1059 	}
1060 
1061 	if ((error = rman_release_resource(r)))
1062 		return (error);
1063 
1064 	if (!passthrough) {
1065 		/* Clean resource list entry */
1066 		rle = resource_list_find(BUS_GET_RESOURCE_LIST(dev, child),
1067 		    rman_get_type(r), rman_get_rid(r));
1068 		if (rle != NULL)
1069 			rle->res = NULL;
1070 	}
1071 
1072 	return (0);
1073 }
1074 
1075 /**
1076  * Default bhndb(4) implementation of BUS_ADJUST_RESOURCE().
1077  */
1078 static int
1079 bhndb_adjust_resource(device_t dev, device_t child,
1080     struct resource *r, rman_res_t start, rman_res_t end)
1081 {
1082 	struct bhndb_softc		*sc;
1083 	struct rman			*rm;
1084 	rman_res_t			 mstart, mend;
1085 	int				 error;
1086 
1087 	sc = device_get_softc(dev);
1088 	error = 0;
1089 
1090 	/* Delegate to our parent device's bus if the requested resource type
1091 	 * isn't handled locally. */
1092 	rm = bhndb_get_rman(sc, child, rman_get_type(r));
1093 	if (rm == NULL) {
1094 		return (BUS_ADJUST_RESOURCE(device_get_parent(sc->parent_dev),
1095 		    child, r, start, end));
1096 	}
1097 
1098 	/* Verify basic constraints */
1099 	if (end <= start)
1100 		return (EINVAL);
1101 
1102 	if (!rman_is_region_manager(r, rm))
1103 		return (ENXIO);
1104 
1105 	BHNDB_LOCK(sc);
1106 
1107 	/* If not active, allow any range permitted by the resource manager */
1108 	if (!(rman_get_flags(r) & RF_ACTIVE))
1109 		goto done;
1110 
1111 	/* Otherwise, the range is limited by the bridged resource mapping */
1112 	error = bhndb_find_resource_limits(sc->bus_res, r, &mstart,
1113 	    &mend);
1114 	if (error)
1115 		goto done;
1116 
1117 	if (start < mstart || end > mend) {
1118 		error = EINVAL;
1119 		goto done;
1120 	}
1121 
1122 	/* Fall through */
1123 done:
1124 	if (!error)
1125 		error = rman_adjust_resource(r, start, end);
1126 
1127 	BHNDB_UNLOCK(sc);
1128 	return (error);
1129 }
1130 
1131 /**
1132  * Initialize child resource @p r with a virtual address, tag, and handle
1133  * copied from @p parent, adjusted to contain only the range defined by
1134  * @p offsize and @p size.
1135  *
1136  * @param r The register to be initialized.
1137  * @param parent The parent bus resource that fully contains the subregion.
1138  * @param offset The subregion offset within @p parent.
1139  * @param size The subregion size.
1140  * @p r.
1141  */
1142 static int
1143 bhndb_init_child_resource(struct resource *r,
1144     struct resource *parent, bhnd_size_t offset, bhnd_size_t size)
1145 {
1146 	bus_space_handle_t	bh, child_bh;
1147 	bus_space_tag_t		bt;
1148 	uintptr_t		vaddr;
1149 	int			error;
1150 
1151 	/* Fetch the parent resource's real bus values */
1152 	vaddr = (uintptr_t) rman_get_virtual(parent);
1153 	bt = rman_get_bustag(parent);
1154 	bh = rman_get_bushandle(parent);
1155 
1156 	/* Configure child resource with window-adjusted real bus values */
1157 	vaddr += offset;
1158 	error = bus_space_subregion(bt, bh, offset, size, &child_bh);
1159 	if (error)
1160 		return (error);
1161 
1162 	rman_set_virtual(r, (void *) vaddr);
1163 	rman_set_bustag(r, bt);
1164 	rman_set_bushandle(r, child_bh);
1165 
1166 	return (0);
1167 }
1168 
1169 /**
1170  * Attempt activation of a fixed register window mapping for @p child.
1171  *
1172  * @param sc BHNDB device state.
1173  * @param region The static region definition capable of mapping @p r.
1174  * @param child A child requesting resource activation.
1175  * @param type Resource type.
1176  * @param rid Resource identifier.
1177  * @param r Resource to be activated.
1178  *
1179  * @retval 0 if @p r was activated successfully
1180  * @retval ENOENT if no fixed register window was found.
1181  * @retval non-zero if @p r could not be activated.
1182  */
1183 static int
1184 bhndb_activate_static_region(struct bhndb_softc *sc,
1185     struct bhndb_region *region, device_t child, struct resource *r)
1186 {
1187 	struct resource			*bridge_res;
1188 	const struct bhndb_regwin	*win;
1189 	bhnd_size_t			 parent_offset;
1190 	rman_res_t			 r_start, r_size;
1191 	int				 error;
1192 
1193 	win = region->static_regwin;
1194 
1195 	KASSERT(win != NULL && BHNDB_REGWIN_T_IS_STATIC(win->win_type),
1196 	    ("can't activate non-static region"));
1197 
1198 	r_start = rman_get_start(r);
1199 	r_size = rman_get_size(r);
1200 
1201 	/* Find the corresponding bridge resource */
1202 	bridge_res = bhndb_host_resource_for_regwin(sc->bus_res->res, win);
1203 	if (bridge_res == NULL)
1204 		return (ENXIO);
1205 
1206 	/* Calculate subregion offset within the parent resource */
1207 	parent_offset = r_start - region->addr;
1208 	parent_offset += win->win_offset;
1209 
1210 	/* Configure resource with its real bus values. */
1211 	error = bhndb_init_child_resource(r, bridge_res, parent_offset, r_size);
1212 	if (error)
1213 		return (error);
1214 
1215 	/* Mark active */
1216 	if ((error = rman_activate_resource(r)))
1217 		return (error);
1218 
1219 	return (0);
1220 }
1221 
1222 /**
1223  * Attempt to allocate/retain a dynamic register window for @p r, returning
1224  * the retained window.
1225  *
1226  * @param sc The bhndb driver state.
1227  * @param r The resource for which a window will be retained.
1228  */
1229 static struct bhndb_dw_alloc *
1230 bhndb_retain_dynamic_window(struct bhndb_softc *sc, struct resource *r)
1231 {
1232 	struct bhndb_dw_alloc	*dwa;
1233 	rman_res_t		 r_start, r_size;
1234 	int			 error;
1235 
1236 	BHNDB_LOCK_ASSERT(sc, MA_OWNED);
1237 
1238 	r_start = rman_get_start(r);
1239 	r_size = rman_get_size(r);
1240 
1241 	/* Look for an existing dynamic window we can reference */
1242 	dwa = bhndb_dw_find_mapping(sc->bus_res, r_start, r_size);
1243 	if (dwa != NULL) {
1244 		if (bhndb_dw_retain(sc->bus_res, dwa, r) == 0)
1245 			return (dwa);
1246 
1247 		return (NULL);
1248 	}
1249 
1250 	/* Otherwise, try to reserve a free window */
1251 	dwa = bhndb_dw_next_free(sc->bus_res);
1252 	if (dwa == NULL) {
1253 		/* No free windows */
1254 		return (NULL);
1255 	}
1256 
1257 	/* Window must be large enough to map the entire resource */
1258 	if (dwa->win->win_size < rman_get_size(r))
1259 		return (NULL);
1260 
1261 	/* Set the window target */
1262 	error = bhndb_dw_set_addr(sc->dev, sc->bus_res, dwa, rman_get_start(r),
1263 	    rman_get_size(r));
1264 	if (error) {
1265 		device_printf(sc->dev, "dynamic window initialization "
1266 			"for 0x%llx-0x%llx failed: %d\n",
1267 			(unsigned long long) r_start,
1268 			(unsigned long long) r_start + r_size - 1,
1269 			error);
1270 		return (NULL);
1271 	}
1272 
1273 	/* Add our reservation */
1274 	if (bhndb_dw_retain(sc->bus_res, dwa, r))
1275 		return (NULL);
1276 
1277 	return (dwa);
1278 }
1279 
1280 /**
1281  * Activate a resource using any viable static or dynamic register window.
1282  *
1283  * @param sc The bhndb driver state.
1284  * @param child The child holding ownership of @p r.
1285  * @param r The resource to be activated
1286  * @param[out] indirect On error and if not NULL, will be set to 'true' if
1287  * the caller should instead use an indirect resource mapping.
1288  *
1289  * @retval 0 success
1290  * @retval non-zero activation failed.
1291  */
1292 static int
1293 bhndb_try_activate_resource(struct bhndb_softc *sc, device_t child,
1294     struct resource *r, bool *indirect)
1295 {
1296 	struct bhndb_region	*region;
1297 	struct bhndb_dw_alloc	*dwa;
1298 	bhndb_priority_t	 dw_priority;
1299 	rman_res_t		 r_start, r_size;
1300 	rman_res_t		 parent_offset;
1301 	int			 error, type;
1302 
1303 	BHNDB_LOCK_ASSERT(sc, MA_NOTOWNED);
1304 
1305 	if (indirect != NULL)
1306 		*indirect = false;
1307 
1308 	type = rman_get_type(r);
1309 	switch (type) {
1310 	case SYS_RES_IRQ:
1311 		/* IRQ resources are always directly mapped */
1312 		return (rman_activate_resource(r));
1313 
1314 	case SYS_RES_MEMORY:
1315 		/* Handled below */
1316 		break;
1317 
1318 	default:
1319 		device_printf(sc->dev, "unsupported resource type %d\n", type);
1320 		return (ENXIO);
1321 	}
1322 
1323 	/* Only MMIO resources can be mapped via register windows */
1324 	KASSERT(type == SYS_RES_MEMORY, ("invalid type: %d", type));
1325 
1326 	r_start = rman_get_start(r);
1327 	r_size = rman_get_size(r);
1328 
1329 	/* Activate native addrspace resources using the host address space */
1330 	if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_NATIVE) {
1331 		struct resource *parent;
1332 
1333 		/* Find the bridge resource referenced by the child */
1334 		parent = bhndb_host_resource_for_range(sc->bus_res->res,
1335 		    type, r_start, r_size);
1336 		if (parent == NULL) {
1337 			device_printf(sc->dev, "host resource not found "
1338 			     "for 0x%llx-0x%llx\n",
1339 			     (unsigned long long) r_start,
1340 			     (unsigned long long) r_start + r_size - 1);
1341 			return (ENOENT);
1342 		}
1343 
1344 		/* Initialize child resource with the real bus values */
1345 		error = bhndb_init_child_resource(r, parent,
1346 		    r_start - rman_get_start(parent), r_size);
1347 		if (error)
1348 			return (error);
1349 
1350 		/* Try to activate child resource */
1351 		return (rman_activate_resource(r));
1352 	}
1353 
1354 	/* Default to low priority */
1355 	dw_priority = BHNDB_PRIORITY_LOW;
1356 
1357 	/* Look for a bus region matching the resource's address range */
1358 	region = bhndb_find_resource_region(sc->bus_res, r_start, r_size);
1359 	if (region != NULL)
1360 		dw_priority = region->priority;
1361 
1362 	/* Prefer static mappings over consuming a dynamic windows. */
1363 	if (region && region->static_regwin) {
1364 		error = bhndb_activate_static_region(sc, region, child, r);
1365 		if (error)
1366 			device_printf(sc->dev, "static window allocation "
1367 			     "for 0x%llx-0x%llx failed\n",
1368 			     (unsigned long long) r_start,
1369 			     (unsigned long long) r_start + r_size - 1);
1370 		return (error);
1371 	}
1372 
1373 	/* A dynamic window will be required; is this resource high enough
1374 	 * priority to be reserved a dynamic window? */
1375 	if (dw_priority < sc->bus_res->min_prio) {
1376 		if (indirect)
1377 			*indirect = true;
1378 
1379 		return (ENOMEM);
1380 	}
1381 
1382 	/* Find and retain a usable window */
1383 	BHNDB_LOCK(sc); {
1384 		dwa = bhndb_retain_dynamic_window(sc, r);
1385 	} BHNDB_UNLOCK(sc);
1386 
1387 	if (dwa == NULL) {
1388 		if (indirect)
1389 			*indirect = true;
1390 		return (ENOMEM);
1391 	}
1392 
1393 	/* Configure resource with its real bus values. */
1394 	parent_offset = dwa->win->win_offset;
1395 	parent_offset += r_start - dwa->target;
1396 
1397 	error = bhndb_init_child_resource(r, dwa->parent_res, parent_offset,
1398 	    dwa->win->win_size);
1399 	if (error)
1400 		goto failed;
1401 
1402 	/* Mark active */
1403 	if ((error = rman_activate_resource(r)))
1404 		goto failed;
1405 
1406 	return (0);
1407 
1408 failed:
1409 	/* Release our region allocation. */
1410 	BHNDB_LOCK(sc);
1411 	bhndb_dw_release(sc->bus_res, dwa, r);
1412 	BHNDB_UNLOCK(sc);
1413 
1414 	return (error);
1415 }
1416 
1417 /**
1418  * Default bhndb(4) implementation of BUS_ACTIVATE_RESOURCE().
1419  */
1420 static int
1421 bhndb_activate_resource(device_t dev, device_t child, struct resource *r)
1422 {
1423 	struct bhndb_softc *sc = device_get_softc(dev);
1424 
1425 	/* Delegate directly to our parent device's bus if the requested
1426 	 * resource type isn't handled locally. */
1427 	if (bhndb_get_rman(sc, child, rman_get_type(r)) == NULL) {
1428 		return (BUS_ACTIVATE_RESOURCE(device_get_parent(sc->parent_dev),
1429 		    child, r));
1430 	}
1431 
1432 	return (bhndb_try_activate_resource(sc, child, r, NULL));
1433 }
1434 
1435 /**
1436  * Default bhndb(4) implementation of BUS_DEACTIVATE_RESOURCE().
1437  */
1438 static int
1439 bhndb_deactivate_resource(device_t dev, device_t child, struct resource *r)
1440 {
1441 	struct bhndb_dw_alloc	*dwa;
1442 	struct bhndb_softc	*sc;
1443 	struct rman		*rm;
1444 	int			 error, type;
1445 
1446 	sc = device_get_softc(dev);
1447 	type = rman_get_type(r);
1448 
1449 	/* Delegate directly to our parent device's bus if the requested
1450 	 * resource type isn't handled locally. */
1451 	rm = bhndb_get_rman(sc, child, type);
1452 	if (rm == NULL) {
1453 		return (BUS_DEACTIVATE_RESOURCE(
1454 		    device_get_parent(sc->parent_dev), child, r));
1455 	}
1456 
1457 	/* Mark inactive */
1458 	if ((error = rman_deactivate_resource(r)))
1459 		return (error);
1460 
1461 	switch (type) {
1462 	case SYS_RES_IRQ:
1463 		/* No bridge-level state to be freed */
1464 		return (0);
1465 
1466 	case SYS_RES_MEMORY:
1467 		/* Free any dynamic window allocation. */
1468 		if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) {
1469 			BHNDB_LOCK(sc);
1470 			dwa = bhndb_dw_find_resource(sc->bus_res, r);
1471 			if (dwa != NULL)
1472 				bhndb_dw_release(sc->bus_res, dwa, r);
1473 			BHNDB_UNLOCK(sc);
1474 		}
1475 
1476 		return (0);
1477 
1478 	default:
1479 		device_printf(dev, "unsupported resource type %d\n", type);
1480 		return (ENXIO);
1481 	}
1482 }
1483 
1484 /**
1485  * Default bhndb(4) implementation of BUS_GET_RESOURCE_LIST().
1486  */
1487 static struct resource_list *
1488 bhndb_get_resource_list(device_t dev, device_t child)
1489 {
1490 	struct bhndb_devinfo *dinfo = device_get_ivars(child);
1491 	return (&dinfo->resources);
1492 }
1493 
1494 /**
1495  * Default bhndb(4) implementation of BHND_BUS_ACTIVATE_RESOURCE().
1496  *
1497  * For BHNDB_ADDRSPACE_NATIVE children, all resources are activated as direct
1498  * resources via BUS_ACTIVATE_RESOURCE().
1499  *
1500  * For BHNDB_ADDRSPACE_BRIDGED children, the resource priority is determined,
1501  * and if possible, the resource is activated as a direct resource. For example,
1502  * depending on resource priority and bridge resource availability, this
1503  * function will attempt to activate SYS_RES_MEMORY resources using either a
1504  * static register window, a dynamic register window, or it will configure @p r
1505  * as an indirect resource -- in that order.
1506  */
1507 static int
1508 bhndb_activate_bhnd_resource(device_t dev, device_t child,
1509     int type, int rid, struct bhnd_resource *r)
1510 {
1511 	struct bhndb_softc	*sc;
1512 	struct bhndb_region	*region;
1513 	bhndb_priority_t	 r_prio;
1514 	rman_res_t		 r_start, r_size;
1515 	int 			 error;
1516 	bool			 indirect;
1517 
1518 	KASSERT(!r->direct,
1519 	    ("direct flag set on inactive resource"));
1520 
1521 	KASSERT(!(rman_get_flags(r->res) & RF_ACTIVE),
1522 	    ("RF_ACTIVE set on inactive resource"));
1523 
1524 	sc = device_get_softc(dev);
1525 
1526 	/* Delegate directly to BUS_ACTIVATE_RESOURCE() if the requested
1527 	 * resource type isn't handled locally. */
1528 	if (bhndb_get_rman(sc, child, type) == NULL) {
1529 		error = BUS_ACTIVATE_RESOURCE(dev, child, r->res);
1530 		if (error == 0)
1531 			r->direct = true;
1532 		return (error);
1533 	}
1534 
1535 	r_start = rman_get_start(r->res);
1536 	r_size = rman_get_size(r->res);
1537 
1538 	/* Determine the resource priority of bridged resources, and skip direct
1539 	 * allocation if the priority is too low. */
1540 	if (bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED) {
1541 		switch (type) {
1542 		case SYS_RES_IRQ:
1543 			/* IRQ resources are always direct */
1544 			break;
1545 
1546 		case SYS_RES_MEMORY:
1547 			region = bhndb_find_resource_region(sc->bus_res,
1548 				r_start, r_size);
1549 			if (region != NULL)
1550 				r_prio = region->priority;
1551 			else
1552 				r_prio = BHNDB_PRIORITY_NONE;
1553 
1554 			/* If less than the minimum dynamic window priority,
1555 			 * this resource should always be indirect. */
1556 			if (r_prio < sc->bus_res->min_prio)
1557 				return (0);
1558 
1559 			break;
1560 
1561 		default:
1562 			device_printf(dev, "unsupported resource type %d\n",
1563 			    type);
1564 			return (ENXIO);
1565 		}
1566 	}
1567 
1568 	/* Attempt direct activation */
1569 	error = bhndb_try_activate_resource(sc, child, r->res, &indirect);
1570 	if (!error) {
1571 		r->direct = true;
1572 	} else if (indirect) {
1573 		/* The request was valid, but no viable register window is
1574 		 * available; indirection must be employed. */
1575 		error = 0;
1576 		r->direct = false;
1577 	}
1578 
1579 	if (BHNDB_DEBUG(PRIO) &&
1580 	    bhndb_get_addrspace(sc, child) == BHNDB_ADDRSPACE_BRIDGED)
1581 	{
1582 		device_printf(child, "activated 0x%llx-0x%llx as %s "
1583 		    "resource\n",
1584 		    (unsigned long long) r_start,
1585 		    (unsigned long long) r_start + r_size - 1,
1586 		    r->direct ? "direct" : "indirect");
1587 	}
1588 
1589 	return (error);
1590 }
1591 
1592 /**
1593  * Default bhndb(4) implementation of BHND_BUS_DEACTIVATE_RESOURCE().
1594  */
1595 static int
1596 bhndb_deactivate_bhnd_resource(device_t dev, device_t child,
1597     int type, int rid, struct bhnd_resource *r)
1598 {
1599 	int error;
1600 
1601 	/* Indirect resources don't require activation */
1602 	if (!r->direct)
1603 		return (0);
1604 
1605 	KASSERT(rman_get_flags(r->res) & RF_ACTIVE,
1606 	    ("RF_ACTIVE not set on direct resource"));
1607 
1608 	/* Perform deactivation */
1609 	error = BUS_DEACTIVATE_RESOURCE(dev, child, r->res);
1610 	if (!error)
1611 		r->direct = false;
1612 
1613 	return (error);
1614 }
1615 
1616 /**
1617  * Find the best available bridge resource allocation record capable of handling
1618  * bus I/O requests of @p size at @p addr.
1619  *
1620  * In order of preference, this function will either:
1621  *
1622  * - Configure and return a free allocation record
1623  * - Return an existing allocation record mapping the requested space, or
1624  * - Steal, configure, and return an in-use allocation record.
1625  *
1626  * Will panic if a usable record cannot be found.
1627  *
1628  * @param sc Bridge driver state.
1629  * @param addr The I/O target address.
1630  * @param size The size of the I/O operation to be performed at @p addr.
1631  * @param[out] borrowed Set to true if the allocation record was borrowed to
1632  * fulfill this request; the borrowed record maps the target address range,
1633  * and must not be modified.
1634  * @param[out] stolen Set to true if the allocation record was stolen to fulfill
1635  * this request. If a stolen allocation record is returned,
1636  * bhndb_io_resource_restore() must be called upon completion of the bus I/O
1637  * request.
1638  * @param[out] restore If the allocation record was stolen, this will be set
1639  * to the target that must be restored.
1640  */
1641 static struct bhndb_dw_alloc *
1642 bhndb_io_resource_get_window(struct bhndb_softc *sc, bus_addr_t addr,
1643     bus_size_t size, bool *borrowed, bool *stolen, bus_addr_t *restore)
1644 {
1645 	struct bhndb_resources	*br;
1646 	struct bhndb_dw_alloc	*dwa;
1647 	struct bhndb_region	*region;
1648 
1649 	BHNDB_LOCK_ASSERT(sc, MA_OWNED);
1650 
1651 	br = sc->bus_res;
1652 	*borrowed = false;
1653 	*stolen = false;
1654 
1655 	/* Try to fetch a free window */
1656 	if ((dwa = bhndb_dw_next_free(br)) != NULL)
1657 		return (dwa);
1658 
1659 	/* Search for an existing dynamic mapping of this address range.
1660 	 * Static regions are not searched, as a statically mapped
1661 	 * region would never be allocated as an indirect resource. */
1662 	for (size_t i = 0; i < br->dwa_count; i++) {
1663 		const struct bhndb_regwin *win;
1664 
1665 		dwa = &br->dw_alloc[i];
1666 		win = dwa->win;
1667 
1668 		KASSERT(win->win_type == BHNDB_REGWIN_T_DYN,
1669 			("invalid register window type"));
1670 
1671 		/* Verify the range */
1672 		if (addr < dwa->target)
1673 			continue;
1674 
1675 		if (addr + size > dwa->target + win->win_size)
1676 			continue;
1677 
1678 		/* Found */
1679 		*borrowed = true;
1680 		return (dwa);
1681 	}
1682 
1683 	/* Try to steal a window; this should only be required on very early
1684 	 * PCI_V0 (BCM4318, etc) Wi-Fi chipsets */
1685 	region = bhndb_find_resource_region(br, addr, size);
1686 	if (region == NULL)
1687 		return (NULL);
1688 
1689 	if ((region->alloc_flags & BHNDB_ALLOC_FULFILL_ON_OVERCOMMIT) == 0)
1690 		return (NULL);
1691 
1692 	/* Steal a window. This acquires our backing spinlock, disabling
1693 	 * interrupts; the spinlock will be released by
1694 	 * bhndb_dw_return_stolen() */
1695 	if ((dwa = bhndb_dw_steal(br, restore)) != NULL) {
1696 		*stolen = true;
1697 		return (dwa);
1698 	}
1699 
1700 	panic("register windows exhausted attempting to map 0x%llx-0x%llx\n",
1701 	    (unsigned long long) addr, (unsigned long long) addr+size-1);
1702 }
1703 
1704 /**
1705  * Return a borrowed reference to a bridge resource allocation record capable
1706  * of handling bus I/O requests of @p size at @p addr.
1707  *
1708  * This will either return a reference to an existing allocation record mapping
1709  * the requested space, or will configure and return a free allocation record.
1710  *
1711  * Will panic if a usable record cannot be found.
1712  *
1713  * @param sc Bridge driver state.
1714  * @param addr The I/O target address.
1715  * @param size The size of the I/O operation to be performed at @p addr.
1716  * @param[out] offset The offset within the returned resource at which
1717  * to perform the I/O request.
1718  * @param[out] stolen Set to true if the allocation record was stolen to fulfill
1719  * this request. If a stolen allocation record is returned,
1720  * bhndb_io_resource_restore() must be called upon completion of the bus I/O
1721  * request.
1722  * @param[out] restore If the allocation record was stolen, this will be set
1723  * to the target that must be restored.
1724  */
1725 static inline struct bhndb_dw_alloc *
1726 bhndb_io_resource(struct bhndb_softc *sc, bus_addr_t addr, bus_size_t size,
1727     bus_size_t *offset, bool *stolen, bus_addr_t *restore)
1728 {
1729 	struct bhndb_dw_alloc	*dwa;
1730 	bool			 borrowed;
1731 	int			 error;
1732 
1733 	BHNDB_LOCK_ASSERT(sc, MA_OWNED);
1734 
1735 	dwa = bhndb_io_resource_get_window(sc, addr, size, &borrowed, stolen,
1736 	    restore);
1737 
1738 	/* Adjust the window if the I/O request won't fit in the current
1739 	 * target range. */
1740 	if (addr < dwa->target ||
1741 	    addr > dwa->target + dwa->win->win_size ||
1742 	    (dwa->target + dwa->win->win_size) - addr < size)
1743 	{
1744 		/* Cannot modify target of borrowed windows */
1745 		if (borrowed) {
1746 			panic("borrowed register window does not map expected "
1747 			    "range 0x%llx-0x%llx\n",
1748 			    (unsigned long long) addr,
1749 			    (unsigned long long) addr+size-1);
1750 		}
1751 
1752 		error = bhndb_dw_set_addr(sc->dev, sc->bus_res, dwa, addr,
1753 		    size);
1754 		if (error) {
1755 		    panic("failed to set register window target mapping "
1756 			    "0x%llx-0x%llx\n",
1757 			    (unsigned long long) addr,
1758 			    (unsigned long long) addr+size-1);
1759 		}
1760 	}
1761 
1762 	/* Calculate the offset and return */
1763 	*offset = (addr - dwa->target) + dwa->win->win_offset;
1764 	return (dwa);
1765 }
1766 
1767 /*
1768  * BHND_BUS_(READ|WRITE_* implementations
1769  */
1770 
1771 /* bhndb_bus_(read|write) common implementation */
1772 #define	BHNDB_IO_COMMON_SETUP(_io_size)				\
1773 	struct bhndb_softc	*sc;				\
1774 	struct bhndb_dw_alloc	*dwa;				\
1775 	struct resource		*io_res;			\
1776 	bus_size_t		 io_offset;			\
1777 	bus_addr_t		 restore;		\
1778 	bool			 stolen;			\
1779 								\
1780 	sc = device_get_softc(dev);				\
1781 								\
1782 	BHNDB_LOCK(sc);						\
1783 	dwa = bhndb_io_resource(sc, rman_get_start(r->res) +	\
1784 	    offset, _io_size, &io_offset, &stolen, &restore);	\
1785 	io_res = dwa->parent_res;				\
1786 								\
1787 	KASSERT(!r->direct,					\
1788 	    ("bhnd_bus slow path used for direct resource"));	\
1789 								\
1790 	KASSERT(rman_get_flags(io_res) & RF_ACTIVE,		\
1791 	    ("i/o resource is not active"));
1792 
1793 #define	BHNDB_IO_COMMON_TEARDOWN()				\
1794 	if (stolen) {						\
1795 		bhndb_dw_return_stolen(sc->dev, sc->bus_res,	\
1796 		    dwa, restore);				\
1797 	}							\
1798 	BHNDB_UNLOCK(sc);
1799 
1800 /* Defines a bhndb_bus_read_* method implementation */
1801 #define	BHNDB_IO_READ(_type, _name)				\
1802 static _type							\
1803 bhndb_bus_read_ ## _name (device_t dev, device_t child,		\
1804     struct bhnd_resource *r, bus_size_t offset)			\
1805 {								\
1806 	_type v;						\
1807 	BHNDB_IO_COMMON_SETUP(sizeof(_type));			\
1808 	v = bus_read_ ## _name (io_res, io_offset);		\
1809 	BHNDB_IO_COMMON_TEARDOWN();				\
1810 								\
1811 	return (v);						\
1812 }
1813 
1814 /* Defines a bhndb_bus_write_* method implementation */
1815 #define	BHNDB_IO_WRITE(_type, _name)				\
1816 static void							\
1817 bhndb_bus_write_ ## _name (device_t dev, device_t child,	\
1818     struct bhnd_resource *r, bus_size_t offset, _type value)	\
1819 {								\
1820 	BHNDB_IO_COMMON_SETUP(sizeof(_type));			\
1821 	bus_write_ ## _name (io_res, io_offset, value);		\
1822 	BHNDB_IO_COMMON_TEARDOWN();				\
1823 }
1824 
1825 /* Defines a bhndb_bus_(read|write|set)_(multi|region)_* method */
1826 #define	BHNDB_IO_MISC(_type, _ptr, _op, _size)			\
1827 static void							\
1828 bhndb_bus_ ## _op ## _ ## _size (device_t dev,			\
1829     device_t child, struct bhnd_resource *r, bus_size_t offset,	\
1830     _type _ptr datap, bus_size_t count)				\
1831 {								\
1832 	BHNDB_IO_COMMON_SETUP(sizeof(_type) * count);		\
1833 	bus_ ## _op ## _ ## _size (io_res, io_offset,		\
1834 	    datap, count);					\
1835 	BHNDB_IO_COMMON_TEARDOWN();				\
1836 }
1837 
1838 /* Defines a complete set of read/write methods */
1839 #define	BHNDB_IO_METHODS(_type, _size)				\
1840 	BHNDB_IO_READ(_type, _size)				\
1841 	BHNDB_IO_WRITE(_type, _size)				\
1842 								\
1843 	BHNDB_IO_READ(_type, stream_ ## _size)			\
1844 	BHNDB_IO_WRITE(_type, stream_ ## _size)			\
1845 								\
1846 	BHNDB_IO_MISC(_type, *, read_multi, _size)		\
1847 	BHNDB_IO_MISC(_type, *, write_multi, _size)		\
1848 								\
1849 	BHNDB_IO_MISC(_type, *, read_multi_stream, _size)	\
1850 	BHNDB_IO_MISC(_type, *, write_multi_stream, _size)	\
1851 								\
1852 	BHNDB_IO_MISC(_type,  , set_multi, _size)		\
1853 	BHNDB_IO_MISC(_type,  , set_region, _size)		\
1854 	BHNDB_IO_MISC(_type, *, read_region, _size)		\
1855 	BHNDB_IO_MISC(_type, *, write_region, _size)		\
1856 								\
1857 	BHNDB_IO_MISC(_type, *, read_region_stream, _size)	\
1858 	BHNDB_IO_MISC(_type, *, write_region_stream, _size)
1859 
1860 BHNDB_IO_METHODS(uint8_t, 1);
1861 BHNDB_IO_METHODS(uint16_t, 2);
1862 BHNDB_IO_METHODS(uint32_t, 4);
1863 
1864 /**
1865  * Default bhndb(4) implementation of BHND_BUS_BARRIER().
1866  */
1867 static void
1868 bhndb_bus_barrier(device_t dev, device_t child, struct bhnd_resource *r,
1869     bus_size_t offset, bus_size_t length, int flags)
1870 {
1871 	BHNDB_IO_COMMON_SETUP(length);
1872 
1873 	bus_barrier(io_res, io_offset + offset, length, flags);
1874 
1875 	BHNDB_IO_COMMON_TEARDOWN();
1876 }
1877 
1878 /**
1879  * Default bhndb(4) implementation of BHND_MAP_INTR().
1880  */
1881 static int
1882 bhndb_bhnd_map_intr(device_t dev, device_t child, u_int intr, rman_res_t *irq)
1883 {
1884 	u_int			 ivec;
1885 	int			 error;
1886 
1887 	/* Is the intr valid? */
1888 	if (intr >= bhnd_get_intr_count(child))
1889 		return (EINVAL);
1890 
1891 	/* Fetch the interrupt vector */
1892 	if ((error = bhnd_get_intr_ivec(child, intr, &ivec)))
1893 		return (error);
1894 
1895 	/* Map directly to the actual backplane interrupt vector */
1896 	*irq = ivec;
1897 
1898 	return (0);
1899 }
1900 
1901 /**
1902  * Default bhndb(4) implementation of BHND_UNMAP_INTR().
1903  */
1904 static void
1905 bhndb_bhnd_unmap_intr(device_t dev, device_t child, rman_res_t irq)
1906 {
1907 	/* No state to clean up */
1908 }
1909 
1910 /**
1911  * Default bhndb(4) implementation of BUS_SETUP_INTR().
1912  */
1913 static int
1914 bhndb_setup_intr(device_t dev, device_t child, struct resource *r,
1915     int flags, driver_filter_t filter, driver_intr_t handler, void *arg,
1916     void **cookiep)
1917 {
1918 	struct bhndb_softc		*sc;
1919 	struct bhndb_intr_isrc		*isrc;
1920 	struct bhndb_intr_handler	*ih;
1921 	int				 error;
1922 
1923 	sc = device_get_softc(dev);
1924 
1925 	/* Fetch the isrc */
1926 	if ((error = BHNDB_MAP_INTR_ISRC(dev, r, &isrc))) {
1927 		device_printf(dev, "failed to fetch isrc: %d\n", error);
1928 		return (error);
1929 	}
1930 
1931 	/* Allocate new ihandler entry  */
1932 	ih = bhndb_alloc_intr_handler(child, r, isrc);
1933 	if (ih == NULL)
1934 		return (ENOMEM);
1935 
1936 	/* Perform actual interrupt setup via the host isrc */
1937 	error = bus_setup_intr(isrc->is_owner, isrc->is_res, flags, filter,
1938 	    handler, arg, &ih->ih_cookiep);
1939 	if (error) {
1940 		bhndb_free_intr_handler(ih);
1941 		return (error);
1942 	}
1943 
1944 	/* Add to our interrupt handler list */
1945 	BHNDB_LOCK(sc);
1946 	bhndb_register_intr_handler(sc->bus_res, ih);
1947 	BHNDB_UNLOCK(sc);
1948 
1949 	/* Provide the interrupt handler entry as our cookiep value */
1950 	*cookiep = ih;
1951 	return (0);
1952 }
1953 
1954 /**
1955  * Default bhndb(4) implementation of BUS_TEARDOWN_INTR().
1956  */
1957 static int
1958 bhndb_teardown_intr(device_t dev, device_t child, struct resource *r,
1959     void *cookiep)
1960 {
1961 	struct bhndb_softc		*sc;
1962 	struct bhndb_intr_handler	*ih;
1963 	struct bhndb_intr_isrc		*isrc;
1964 	int				 error;
1965 
1966 	sc = device_get_softc(dev);
1967 
1968 	/* Locate and claim ownership of the interrupt handler entry */
1969 	BHNDB_LOCK(sc);
1970 
1971 	ih = bhndb_find_intr_handler(sc->bus_res, cookiep);
1972 	if (ih == NULL) {
1973 		panic("%s requested teardown of invalid cookiep %p",
1974 		    device_get_nameunit(child), cookiep);
1975 	}
1976 
1977 	bhndb_deregister_intr_handler(sc->bus_res, ih);
1978 
1979 	BHNDB_UNLOCK(sc);
1980 
1981 	/* Perform actual interrupt teardown via the host isrc */
1982 	isrc = ih->ih_isrc;
1983 	error = bus_teardown_intr(isrc->is_owner, isrc->is_res, ih->ih_cookiep);
1984 	if (error) {
1985 		/* If teardown fails, we need to reinsert the handler entry
1986 		 * to allow later teardown */
1987 		BHNDB_LOCK(sc);
1988 		bhndb_register_intr_handler(sc->bus_res, ih);
1989 		BHNDB_UNLOCK(sc);
1990 
1991 		return (error);
1992 	}
1993 
1994 	/* Free the entry */
1995 	bhndb_free_intr_handler(ih);
1996 	return (0);
1997 }
1998 
1999 /**
2000  * Default bhndb(4) implementation of BUS_BIND_INTR().
2001  */
2002 static int
2003 bhndb_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
2004 {
2005 	struct bhndb_softc		*sc;
2006 	struct bhndb_intr_handler	*ih;
2007 	struct bhndb_intr_isrc		*isrc;
2008 
2009 	sc = device_get_softc(dev);
2010 	isrc = NULL;
2011 
2012 	/* Fetch the isrc corresponding to the child IRQ resource */
2013 	BHNDB_LOCK(sc);
2014 	STAILQ_FOREACH(ih, &sc->bus_res->bus_intrs, ih_link) {
2015 		if (ih->ih_res == irq) {
2016 			isrc = ih->ih_isrc;
2017 			break;
2018 		}
2019 	}
2020 	BHNDB_UNLOCK(sc);
2021 
2022 	if (isrc == NULL) {
2023 		panic("%s requested bind of invalid irq %#jx-%#jx",
2024 		    device_get_nameunit(child), rman_get_start(irq),
2025 		    rman_get_end(irq));
2026 	}
2027 
2028 	/* Perform actual bind via the host isrc */
2029 	return (bus_bind_intr(isrc->is_owner, isrc->is_res, cpu));
2030 }
2031 
2032 /**
2033  * Default bhndb(4) implementation of BUS_DESCRIBE_INTR().
2034  */
2035 static int
2036 bhndb_describe_intr(device_t dev, device_t child, struct resource *irq,
2037     void *cookie, const char *descr)
2038 {
2039 	struct bhndb_softc		*sc;
2040 	struct bhndb_intr_handler	*ih;
2041 	struct bhndb_intr_isrc		*isrc;
2042 
2043 	sc = device_get_softc(dev);
2044 
2045 	/* Locate the interrupt handler entry; the caller owns the handler
2046 	 * reference, and thus our entry is guaranteed to remain valid after
2047 	 * we drop out lock below. */
2048 	BHNDB_LOCK(sc);
2049 
2050 	ih = bhndb_find_intr_handler(sc->bus_res, cookie);
2051 	if (ih == NULL) {
2052 		panic("%s requested invalid cookiep %p",
2053 		    device_get_nameunit(child), cookie);
2054 	}
2055 
2056 	isrc = ih->ih_isrc;
2057 
2058 	BHNDB_UNLOCK(sc);
2059 
2060 	/* Perform the actual request via the host isrc */
2061 	return (BUS_DESCRIBE_INTR(device_get_parent(isrc->is_owner),
2062 	    isrc->is_owner, isrc->is_res, ih->ih_cookiep, descr));
2063 }
2064 
2065 /**
2066  * Default bhndb(4) implementation of BUS_CONFIG_INTR().
2067  */
2068 static int
2069 bhndb_config_intr(device_t dev, int irq, enum intr_trigger trig,
2070     enum intr_polarity pol)
2071 {
2072 	/* Unsupported */
2073 	return (ENXIO);
2074 }
2075 
2076 /**
2077  * Default bhndb(4) implementation of BUS_REMAP_INTR().
2078  */
2079 static int
2080 bhndb_remap_intr(device_t dev, device_t child, u_int irq)
2081 {
2082 	/* Unsupported */
2083 	return (ENXIO);
2084 }
2085 
2086 /**
2087  * Default bhndb(4) implementation of BHND_BUS_GET_DMA_TRANSLATION().
2088  */
2089 static inline int
2090 bhndb_get_dma_translation(device_t dev, device_t child, u_int width,
2091     uint32_t flags, bus_dma_tag_t *dmat,
2092     struct bhnd_dma_translation *translation)
2093 {
2094 	struct bhndb_softc			*sc;
2095 	const struct bhndb_hwcfg		*hwcfg;
2096 	const struct bhnd_dma_translation	*match;
2097 	bus_dma_tag_t				 match_dmat;
2098 	bhnd_addr_t				 addr_mask, match_addr_mask;
2099 
2100 	sc = device_get_softc(dev);
2101 	hwcfg = sc->bus_res->cfg;
2102 
2103 	/* Is DMA supported? */
2104 	if (sc->bus_res->res->dma_tags == NULL)
2105 		return (ENODEV);
2106 
2107 	/* Is the requested width supported? */
2108 	if (width > BHND_DMA_ADDR_32BIT) {
2109 		/* Backplane must support 64-bit addressing */
2110 		if (!(sc->chipid.chip_caps & BHND_CAP_BP64))
2111 			width = BHND_DMA_ADDR_32BIT;
2112 	}
2113 
2114 	/* Find the best matching descriptor for the requested width */
2115 	addr_mask = BHND_DMA_ADDR_BITMASK(width);
2116 
2117 	match = NULL;
2118 	match_addr_mask = 0x0;
2119 	match_dmat = NULL;
2120 
2121 	for (size_t i = 0; i < sc->bus_res->res->num_dma_tags; i++) {
2122 		const struct bhnd_dma_translation	*dwin;
2123 		bhnd_addr_t				 masked;
2124 
2125 		dwin = &hwcfg->dma_translations[i];
2126 
2127 		/* The base address must be device addressable */
2128 		if ((dwin->base_addr & addr_mask) != dwin->base_addr)
2129 			continue;
2130 
2131 		/* The flags must match */
2132 		if ((dwin->flags & flags) != flags)
2133 			continue;
2134 
2135 		/* The window must cover at least part of our addressable
2136 		 * range */
2137 		masked = (dwin->addr_mask | dwin->addrext_mask) & addr_mask;
2138 		if (masked == 0)
2139 			continue;
2140 
2141 		/* Is this a better match? */
2142 		if (match == NULL || masked > match_addr_mask) {
2143 			match = dwin;
2144 			match_addr_mask = masked;
2145 			match_dmat = sc->bus_res->res->dma_tags[i];
2146 		}
2147 	}
2148 
2149 	if (match == NULL || match_addr_mask == 0)
2150 		return (ENOENT);
2151 
2152 	if (dmat != NULL)
2153 		*dmat = match_dmat;
2154 
2155 	if (translation != NULL)
2156 		*translation = *match;
2157 
2158 	return (0);
2159 }
2160 
2161 /**
2162  * Default bhndb(4) implementation of BUS_GET_DMA_TAG().
2163  */
2164 static bus_dma_tag_t
2165 bhndb_get_dma_tag(device_t dev, device_t child)
2166 {
2167 	struct bhndb_softc *sc = device_get_softc(dev);
2168 
2169 	/*
2170 	 * A bridge may have multiple DMA translation descriptors, each with
2171 	 * their own incompatible restrictions; drivers should in general call
2172 	 * BHND_BUS_GET_DMA_TRANSLATION() to fetch both the best available DMA
2173 	 * translation, and its corresponding DMA tag.
2174 	 *
2175 	 * Child drivers that do not use BHND_BUS_GET_DMA_TRANSLATION() are
2176 	 * responsible for creating their own restricted DMA tag; since we
2177 	 * cannot do this for them in BUS_GET_DMA_TAG(), we simply return the
2178 	 * bridge parent's DMA tag directly;
2179 	 */
2180 	return (bus_get_dma_tag(sc->parent_dev));
2181 }
2182 
2183 static device_method_t bhndb_methods[] = {
2184 	/* Device interface */ \
2185 	DEVMETHOD(device_probe,			bhndb_generic_probe),
2186 	DEVMETHOD(device_detach,		bhndb_generic_detach),
2187 	DEVMETHOD(device_shutdown,		bus_generic_shutdown),
2188 	DEVMETHOD(device_suspend,		bhndb_generic_suspend),
2189 	DEVMETHOD(device_resume,		bhndb_generic_resume),
2190 
2191 	/* Bus interface */
2192 	DEVMETHOD(bus_probe_nomatch,		bhndb_probe_nomatch),
2193 	DEVMETHOD(bus_print_child,		bhndb_print_child),
2194 	DEVMETHOD(bus_child_location,		bhndb_child_location),
2195 	DEVMETHOD(bus_add_child,		bhndb_add_child),
2196 	DEVMETHOD(bus_child_deleted,		bhndb_child_deleted),
2197 
2198 	DEVMETHOD(bus_alloc_resource,		bhndb_alloc_resource),
2199 	DEVMETHOD(bus_release_resource,		bhndb_release_resource),
2200 	DEVMETHOD(bus_activate_resource,	bhndb_activate_resource),
2201 	DEVMETHOD(bus_deactivate_resource,	bhndb_deactivate_resource),
2202 
2203 	DEVMETHOD(bus_setup_intr,		bhndb_setup_intr),
2204 	DEVMETHOD(bus_teardown_intr,		bhndb_teardown_intr),
2205 	DEVMETHOD(bus_config_intr,		bhndb_config_intr),
2206 	DEVMETHOD(bus_bind_intr,		bhndb_bind_intr),
2207 	DEVMETHOD(bus_describe_intr,		bhndb_describe_intr),
2208 	DEVMETHOD(bus_remap_intr,		bhndb_remap_intr),
2209 
2210 	DEVMETHOD(bus_get_dma_tag,		bhndb_get_dma_tag),
2211 
2212 	DEVMETHOD(bus_adjust_resource,		bhndb_adjust_resource),
2213 	DEVMETHOD(bus_set_resource,		bus_generic_rl_set_resource),
2214 	DEVMETHOD(bus_get_resource,		bus_generic_rl_get_resource),
2215 	DEVMETHOD(bus_delete_resource,		bus_generic_rl_delete_resource),
2216 	DEVMETHOD(bus_get_resource_list,	bhndb_get_resource_list),
2217 
2218 	DEVMETHOD(bus_read_ivar,		bhndb_read_ivar),
2219 	DEVMETHOD(bus_write_ivar,		bhndb_write_ivar),
2220 
2221 	/* BHNDB interface */
2222 	DEVMETHOD(bhndb_get_chipid,		bhndb_get_chipid),
2223 	DEVMETHOD(bhndb_is_core_disabled,	bhndb_is_core_disabled),
2224 	DEVMETHOD(bhndb_get_hostb_core,		bhndb_get_hostb_core),
2225 	DEVMETHOD(bhndb_suspend_resource,	bhndb_suspend_resource),
2226 	DEVMETHOD(bhndb_resume_resource,	bhndb_resume_resource),
2227 
2228 	/* BHND interface */
2229 	DEVMETHOD(bhnd_bus_get_chipid,		bhndb_get_chipid),
2230 	DEVMETHOD(bhnd_bus_activate_resource,	bhndb_activate_bhnd_resource),
2231 	DEVMETHOD(bhnd_bus_deactivate_resource,	bhndb_deactivate_bhnd_resource),
2232 	DEVMETHOD(bhnd_bus_get_nvram_var,	bhnd_bus_generic_get_nvram_var),
2233 	DEVMETHOD(bhnd_bus_map_intr,		bhndb_bhnd_map_intr),
2234 	DEVMETHOD(bhnd_bus_unmap_intr,		bhndb_bhnd_unmap_intr),
2235 	DEVMETHOD(bhnd_bus_get_dma_translation,	bhndb_get_dma_translation),
2236 
2237 	DEVMETHOD(bhnd_bus_get_service_registry,bhndb_get_service_registry),
2238 	DEVMETHOD(bhnd_bus_register_provider,	bhnd_bus_generic_sr_register_provider),
2239 	DEVMETHOD(bhnd_bus_deregister_provider,	bhnd_bus_generic_sr_deregister_provider),
2240 	DEVMETHOD(bhnd_bus_retain_provider,	bhnd_bus_generic_sr_retain_provider),
2241 	DEVMETHOD(bhnd_bus_release_provider,	bhnd_bus_generic_sr_release_provider),
2242 
2243 	DEVMETHOD(bhnd_bus_read_1,		bhndb_bus_read_1),
2244 	DEVMETHOD(bhnd_bus_read_2,		bhndb_bus_read_2),
2245 	DEVMETHOD(bhnd_bus_read_4,		bhndb_bus_read_4),
2246 	DEVMETHOD(bhnd_bus_write_1,		bhndb_bus_write_1),
2247 	DEVMETHOD(bhnd_bus_write_2,		bhndb_bus_write_2),
2248 	DEVMETHOD(bhnd_bus_write_4,		bhndb_bus_write_4),
2249 
2250 	DEVMETHOD(bhnd_bus_read_stream_1,	bhndb_bus_read_stream_1),
2251 	DEVMETHOD(bhnd_bus_read_stream_2,	bhndb_bus_read_stream_2),
2252 	DEVMETHOD(bhnd_bus_read_stream_4,	bhndb_bus_read_stream_4),
2253 	DEVMETHOD(bhnd_bus_write_stream_1,	bhndb_bus_write_stream_1),
2254 	DEVMETHOD(bhnd_bus_write_stream_2,	bhndb_bus_write_stream_2),
2255 	DEVMETHOD(bhnd_bus_write_stream_4,	bhndb_bus_write_stream_4),
2256 
2257 	DEVMETHOD(bhnd_bus_read_multi_1,	bhndb_bus_read_multi_1),
2258 	DEVMETHOD(bhnd_bus_read_multi_2,	bhndb_bus_read_multi_2),
2259 	DEVMETHOD(bhnd_bus_read_multi_4,	bhndb_bus_read_multi_4),
2260 	DEVMETHOD(bhnd_bus_write_multi_1,	bhndb_bus_write_multi_1),
2261 	DEVMETHOD(bhnd_bus_write_multi_2,	bhndb_bus_write_multi_2),
2262 	DEVMETHOD(bhnd_bus_write_multi_4,	bhndb_bus_write_multi_4),
2263 
2264 	DEVMETHOD(bhnd_bus_read_multi_stream_1,	bhndb_bus_read_multi_stream_1),
2265 	DEVMETHOD(bhnd_bus_read_multi_stream_2,	bhndb_bus_read_multi_stream_2),
2266 	DEVMETHOD(bhnd_bus_read_multi_stream_4,	bhndb_bus_read_multi_stream_4),
2267 	DEVMETHOD(bhnd_bus_write_multi_stream_1,bhndb_bus_write_multi_stream_1),
2268 	DEVMETHOD(bhnd_bus_write_multi_stream_2,bhndb_bus_write_multi_stream_2),
2269 	DEVMETHOD(bhnd_bus_write_multi_stream_4,bhndb_bus_write_multi_stream_4),
2270 
2271 	DEVMETHOD(bhnd_bus_set_multi_1,		bhndb_bus_set_multi_1),
2272 	DEVMETHOD(bhnd_bus_set_multi_2,		bhndb_bus_set_multi_2),
2273 	DEVMETHOD(bhnd_bus_set_multi_4,		bhndb_bus_set_multi_4),
2274 	DEVMETHOD(bhnd_bus_set_region_1,	bhndb_bus_set_region_1),
2275 	DEVMETHOD(bhnd_bus_set_region_2,	bhndb_bus_set_region_2),
2276 	DEVMETHOD(bhnd_bus_set_region_4,	bhndb_bus_set_region_4),
2277 
2278 	DEVMETHOD(bhnd_bus_read_region_1,	bhndb_bus_read_region_1),
2279 	DEVMETHOD(bhnd_bus_read_region_2,	bhndb_bus_read_region_2),
2280 	DEVMETHOD(bhnd_bus_read_region_4,	bhndb_bus_read_region_4),
2281 	DEVMETHOD(bhnd_bus_write_region_1,	bhndb_bus_write_region_1),
2282 	DEVMETHOD(bhnd_bus_write_region_2,	bhndb_bus_write_region_2),
2283 	DEVMETHOD(bhnd_bus_write_region_4,	bhndb_bus_write_region_4),
2284 
2285 	DEVMETHOD(bhnd_bus_read_region_stream_1,bhndb_bus_read_region_stream_1),
2286 	DEVMETHOD(bhnd_bus_read_region_stream_2,bhndb_bus_read_region_stream_2),
2287 	DEVMETHOD(bhnd_bus_read_region_stream_4,bhndb_bus_read_region_stream_4),
2288 	DEVMETHOD(bhnd_bus_write_region_stream_1,bhndb_bus_write_region_stream_1),
2289 	DEVMETHOD(bhnd_bus_write_region_stream_2,bhndb_bus_write_region_stream_2),
2290 	DEVMETHOD(bhnd_bus_write_region_stream_4,bhndb_bus_write_region_stream_4),
2291 
2292 	DEVMETHOD(bhnd_bus_barrier,		bhndb_bus_barrier),
2293 
2294 	DEVMETHOD_END
2295 };
2296 
2297 DEFINE_CLASS_0(bhndb, bhndb_driver, bhndb_methods, sizeof(struct bhndb_softc));
2298 
2299 MODULE_VERSION(bhndb, 1);
2300 MODULE_DEPEND(bhndb, bhnd, 1, 1, 1);
2301