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