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