xref: /freebsd/sys/dev/pci/pci_pci.c (revision 534ccd7bbfadde557eeffe986e5462455c8111ea)
1bb0d0a8eSMike Smith /*-
2bb0d0a8eSMike Smith  * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
3bb0d0a8eSMike Smith  * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
4bb0d0a8eSMike Smith  * Copyright (c) 2000 BSDi
5bb0d0a8eSMike Smith  * All rights reserved.
6bb0d0a8eSMike Smith  *
7bb0d0a8eSMike Smith  * Redistribution and use in source and binary forms, with or without
8bb0d0a8eSMike Smith  * modification, are permitted provided that the following conditions
9bb0d0a8eSMike Smith  * are met:
10bb0d0a8eSMike Smith  * 1. Redistributions of source code must retain the above copyright
11bb0d0a8eSMike Smith  *    notice, this list of conditions and the following disclaimer.
12bb0d0a8eSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
13bb0d0a8eSMike Smith  *    notice, this list of conditions and the following disclaimer in the
14bb0d0a8eSMike Smith  *    documentation and/or other materials provided with the distribution.
15bb0d0a8eSMike Smith  * 3. The name of the author may not be used to endorse or promote products
16bb0d0a8eSMike Smith  *    derived from this software without specific prior written permission.
17bb0d0a8eSMike Smith  *
18bb0d0a8eSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19bb0d0a8eSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20bb0d0a8eSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21bb0d0a8eSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22bb0d0a8eSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23bb0d0a8eSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24bb0d0a8eSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25bb0d0a8eSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26bb0d0a8eSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27bb0d0a8eSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28bb0d0a8eSMike Smith  * SUCH DAMAGE.
29bb0d0a8eSMike Smith  */
30bb0d0a8eSMike Smith 
31aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
32aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
33aad970f1SDavid E. O'Brien 
34bb0d0a8eSMike Smith /*
35bb0d0a8eSMike Smith  * PCI:PCI bridge support.
36bb0d0a8eSMike Smith  */
37bb0d0a8eSMike Smith 
38bb0d0a8eSMike Smith #include <sys/param.h>
39bb0d0a8eSMike Smith #include <sys/bus.h>
4083c41143SJohn Baldwin #include <sys/kernel.h>
4183c41143SJohn Baldwin #include <sys/malloc.h>
4283c41143SJohn Baldwin #include <sys/module.h>
43a8b354a8SWarner Losh #include <sys/rman.h>
441c54ff33SMatthew N. Dodd #include <sys/sysctl.h>
4583c41143SJohn Baldwin #include <sys/systm.h>
46bb0d0a8eSMike Smith 
4738d8c994SWarner Losh #include <dev/pci/pcivar.h>
4838d8c994SWarner Losh #include <dev/pci/pcireg.h>
4962508c53SJohn Baldwin #include <dev/pci/pci_private.h>
5038d8c994SWarner Losh #include <dev/pci/pcib_private.h>
51bb0d0a8eSMike Smith 
52bb0d0a8eSMike Smith #include "pcib_if.h"
53bb0d0a8eSMike Smith 
54bb0d0a8eSMike Smith static int		pcib_probe(device_t dev);
55e36af292SJung-uk Kim static int		pcib_suspend(device_t dev);
56e36af292SJung-uk Kim static int		pcib_resume(device_t dev);
5762508c53SJohn Baldwin static int		pcib_power_for_sleep(device_t pcib, device_t dev,
5862508c53SJohn Baldwin 			    int *pstate);
5955d3ea17SRyan Stone static uint16_t		pcib_ari_get_rid(device_t pcib, device_t dev);
6055d3ea17SRyan Stone static uint32_t		pcib_read_config(device_t dev, u_int b, u_int s,
6155d3ea17SRyan Stone     u_int f, u_int reg, int width);
6255d3ea17SRyan Stone static void		pcib_write_config(device_t dev, u_int b, u_int s,
6355d3ea17SRyan Stone     u_int f, u_int reg, uint32_t val, int width);
6455d3ea17SRyan Stone static int		pcib_ari_maxslots(device_t dev);
6555d3ea17SRyan Stone static int		pcib_ari_maxfuncs(device_t dev);
6655d3ea17SRyan Stone static int		pcib_try_enable_ari(device_t pcib, device_t dev);
672397d2d8SRyan Stone static int		pcib_ari_enabled(device_t pcib);
682397d2d8SRyan Stone static void		pcib_ari_decode_rid(device_t pcib, uint16_t rid,
692397d2d8SRyan Stone 			    int *bus, int *slot, int *func);
70bb0d0a8eSMike Smith 
71bb0d0a8eSMike Smith static device_method_t pcib_methods[] = {
72bb0d0a8eSMike Smith     /* Device interface */
73bb0d0a8eSMike Smith     DEVMETHOD(device_probe,		pcib_probe),
74bb0d0a8eSMike Smith     DEVMETHOD(device_attach,		pcib_attach),
754e30440dSWarner Losh     DEVMETHOD(device_detach,		bus_generic_detach),
76bb0d0a8eSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
77e36af292SJung-uk Kim     DEVMETHOD(device_suspend,		pcib_suspend),
78e36af292SJung-uk Kim     DEVMETHOD(device_resume,		pcib_resume),
79bb0d0a8eSMike Smith 
80bb0d0a8eSMike Smith     /* Bus interface */
81bb0d0a8eSMike Smith     DEVMETHOD(bus_read_ivar,		pcib_read_ivar),
82bb0d0a8eSMike Smith     DEVMETHOD(bus_write_ivar,		pcib_write_ivar),
83bb0d0a8eSMike Smith     DEVMETHOD(bus_alloc_resource,	pcib_alloc_resource),
8483c41143SJohn Baldwin #ifdef NEW_PCIB
8583c41143SJohn Baldwin     DEVMETHOD(bus_adjust_resource,	pcib_adjust_resource),
8683c41143SJohn Baldwin     DEVMETHOD(bus_release_resource,	pcib_release_resource),
8783c41143SJohn Baldwin #else
88d2c9344fSJohn Baldwin     DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
89bb0d0a8eSMike Smith     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
9083c41143SJohn Baldwin #endif
91bb0d0a8eSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
92bb0d0a8eSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
93bb0d0a8eSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
94bb0d0a8eSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
95bb0d0a8eSMike Smith 
96bb0d0a8eSMike Smith     /* pcib interface */
9755d3ea17SRyan Stone     DEVMETHOD(pcib_maxslots,		pcib_ari_maxslots),
9855d3ea17SRyan Stone     DEVMETHOD(pcib_maxfuncs,		pcib_ari_maxfuncs),
99bb0d0a8eSMike Smith     DEVMETHOD(pcib_read_config,		pcib_read_config),
100bb0d0a8eSMike Smith     DEVMETHOD(pcib_write_config,	pcib_write_config),
101bb0d0a8eSMike Smith     DEVMETHOD(pcib_route_interrupt,	pcib_route_interrupt),
1029bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_alloc_msi,		pcib_alloc_msi),
1039bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msi,		pcib_release_msi),
1049bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_alloc_msix,		pcib_alloc_msix),
1059bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msix,	pcib_release_msix),
106e706f7f0SJohn Baldwin     DEVMETHOD(pcib_map_msi,		pcib_map_msi),
10762508c53SJohn Baldwin     DEVMETHOD(pcib_power_for_sleep,	pcib_power_for_sleep),
10855d3ea17SRyan Stone     DEVMETHOD(pcib_get_rid,		pcib_ari_get_rid),
10955d3ea17SRyan Stone     DEVMETHOD(pcib_try_enable_ari,	pcib_try_enable_ari),
1102397d2d8SRyan Stone     DEVMETHOD(pcib_ari_enabled,		pcib_ari_enabled),
1112397d2d8SRyan Stone     DEVMETHOD(pcib_decode_rid,		pcib_ari_decode_rid),
112bb0d0a8eSMike Smith 
1134b7ec270SMarius Strobl     DEVMETHOD_END
114bb0d0a8eSMike Smith };
115bb0d0a8eSMike Smith 
11604dda605SJohn Baldwin static devclass_t pcib_devclass;
117bb0d0a8eSMike Smith 
11804dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc));
11968e9cbd3SMarius Strobl DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, NULL, NULL);
120bb0d0a8eSMike Smith 
12183c41143SJohn Baldwin #ifdef NEW_PCIB
1220070c94bSJohn Baldwin SYSCTL_DECL(_hw_pci);
1230070c94bSJohn Baldwin 
1240070c94bSJohn Baldwin static int pci_clear_pcib;
1250070c94bSJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0,
1260070c94bSJohn Baldwin     "Clear firmware-assigned resources for PCI-PCI bridge I/O windows.");
12783c41143SJohn Baldwin 
12883c41143SJohn Baldwin /*
12983c41143SJohn Baldwin  * Is a resource from a child device sub-allocated from one of our
13083c41143SJohn Baldwin  * resource managers?
13183c41143SJohn Baldwin  */
13283c41143SJohn Baldwin static int
13383c41143SJohn Baldwin pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r)
13483c41143SJohn Baldwin {
13583c41143SJohn Baldwin 
13683c41143SJohn Baldwin 	switch (type) {
1374edef187SJohn Baldwin #ifdef PCI_RES_BUS
1384edef187SJohn Baldwin 	case PCI_RES_BUS:
1394edef187SJohn Baldwin 		return (rman_is_region_manager(r, &sc->bus.rman));
1404edef187SJohn Baldwin #endif
14183c41143SJohn Baldwin 	case SYS_RES_IOPORT:
14283c41143SJohn Baldwin 		return (rman_is_region_manager(r, &sc->io.rman));
14383c41143SJohn Baldwin 	case SYS_RES_MEMORY:
14483c41143SJohn Baldwin 		/* Prefetchable resources may live in either memory rman. */
14583c41143SJohn Baldwin 		if (rman_get_flags(r) & RF_PREFETCHABLE &&
14683c41143SJohn Baldwin 		    rman_is_region_manager(r, &sc->pmem.rman))
14783c41143SJohn Baldwin 			return (1);
14883c41143SJohn Baldwin 		return (rman_is_region_manager(r, &sc->mem.rman));
14983c41143SJohn Baldwin 	}
15083c41143SJohn Baldwin 	return (0);
15183c41143SJohn Baldwin }
15283c41143SJohn Baldwin 
15383c41143SJohn Baldwin static int
15483c41143SJohn Baldwin pcib_is_window_open(struct pcib_window *pw)
15583c41143SJohn Baldwin {
15683c41143SJohn Baldwin 
15783c41143SJohn Baldwin 	return (pw->valid && pw->base < pw->limit);
15883c41143SJohn Baldwin }
15983c41143SJohn Baldwin 
16083c41143SJohn Baldwin /*
16183c41143SJohn Baldwin  * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and
16283c41143SJohn Baldwin  * handle for the resource, we could pass RF_ACTIVE up to the PCI bus
16383c41143SJohn Baldwin  * when allocating the resource windows and rely on the PCI bus driver
16483c41143SJohn Baldwin  * to do this for us.
16583c41143SJohn Baldwin  */
16683c41143SJohn Baldwin static void
16783c41143SJohn Baldwin pcib_activate_window(struct pcib_softc *sc, int type)
16883c41143SJohn Baldwin {
16983c41143SJohn Baldwin 
17083c41143SJohn Baldwin 	PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type);
17183c41143SJohn Baldwin }
17283c41143SJohn Baldwin 
17383c41143SJohn Baldwin static void
17483c41143SJohn Baldwin pcib_write_windows(struct pcib_softc *sc, int mask)
17583c41143SJohn Baldwin {
17683c41143SJohn Baldwin 	device_t dev;
17783c41143SJohn Baldwin 	uint32_t val;
17883c41143SJohn Baldwin 
17983c41143SJohn Baldwin 	dev = sc->dev;
18083c41143SJohn Baldwin 	if (sc->io.valid && mask & WIN_IO) {
18183c41143SJohn Baldwin 		val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
18283c41143SJohn Baldwin 		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
18383c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOBASEH_1,
18483c41143SJohn Baldwin 			    sc->io.base >> 16, 2);
18583c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOLIMITH_1,
18683c41143SJohn Baldwin 			    sc->io.limit >> 16, 2);
18783c41143SJohn Baldwin 		}
18883c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1);
18983c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1);
19083c41143SJohn Baldwin 	}
19183c41143SJohn Baldwin 
19283c41143SJohn Baldwin 	if (mask & WIN_MEM) {
19383c41143SJohn Baldwin 		pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2);
19483c41143SJohn Baldwin 		pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2);
19583c41143SJohn Baldwin 	}
19683c41143SJohn Baldwin 
19783c41143SJohn Baldwin 	if (sc->pmem.valid && mask & WIN_PMEM) {
19883c41143SJohn Baldwin 		val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
19983c41143SJohn Baldwin 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
20083c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMBASEH_1,
20183c41143SJohn Baldwin 			    sc->pmem.base >> 32, 4);
20283c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMLIMITH_1,
20383c41143SJohn Baldwin 			    sc->pmem.limit >> 32, 4);
20483c41143SJohn Baldwin 		}
20583c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2);
20683c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2);
20783c41143SJohn Baldwin 	}
20883c41143SJohn Baldwin }
20983c41143SJohn Baldwin 
210c825d4dcSJohn Baldwin /*
211c825d4dcSJohn Baldwin  * This is used to reject I/O port allocations that conflict with an
212c825d4dcSJohn Baldwin  * ISA alias range.
213c825d4dcSJohn Baldwin  */
214c825d4dcSJohn Baldwin static int
2152dd1bdf1SJustin Hibbits pcib_is_isa_range(struct pcib_softc *sc, rman_res_t start, rman_res_t end,
2162dd1bdf1SJustin Hibbits     rman_res_t count)
217c825d4dcSJohn Baldwin {
2182dd1bdf1SJustin Hibbits 	rman_res_t next_alias;
219c825d4dcSJohn Baldwin 
220c825d4dcSJohn Baldwin 	if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE))
221c825d4dcSJohn Baldwin 		return (0);
222c825d4dcSJohn Baldwin 
223c825d4dcSJohn Baldwin 	/* Only check fixed ranges for overlap. */
224c825d4dcSJohn Baldwin 	if (start + count - 1 != end)
225c825d4dcSJohn Baldwin 		return (0);
226c825d4dcSJohn Baldwin 
227c825d4dcSJohn Baldwin 	/* ISA aliases are only in the lower 64KB of I/O space. */
228c825d4dcSJohn Baldwin 	if (start >= 65536)
229c825d4dcSJohn Baldwin 		return (0);
230c825d4dcSJohn Baldwin 
231c825d4dcSJohn Baldwin 	/* Check for overlap with 0x000 - 0x0ff as a special case. */
232c825d4dcSJohn Baldwin 	if (start < 0x100)
233c825d4dcSJohn Baldwin 		goto alias;
234c825d4dcSJohn Baldwin 
235c825d4dcSJohn Baldwin 	/*
236c825d4dcSJohn Baldwin 	 * If the start address is an alias, the range is an alias.
237c825d4dcSJohn Baldwin 	 * Otherwise, compute the start of the next alias range and
238c825d4dcSJohn Baldwin 	 * check if it is before the end of the candidate range.
239c825d4dcSJohn Baldwin 	 */
240c825d4dcSJohn Baldwin 	if ((start & 0x300) != 0)
241c825d4dcSJohn Baldwin 		goto alias;
242c825d4dcSJohn Baldwin 	next_alias = (start & ~0x3fful) | 0x100;
243c825d4dcSJohn Baldwin 	if (next_alias <= end)
244c825d4dcSJohn Baldwin 		goto alias;
245c825d4dcSJohn Baldwin 	return (0);
246c825d4dcSJohn Baldwin 
247c825d4dcSJohn Baldwin alias:
248c825d4dcSJohn Baldwin 	if (bootverbose)
249c825d4dcSJohn Baldwin 		device_printf(sc->dev,
250c825d4dcSJohn Baldwin 		    "I/O range %#lx-%#lx overlaps with an ISA alias\n", start,
251c825d4dcSJohn Baldwin 		    end);
252c825d4dcSJohn Baldwin 	return (1);
253c825d4dcSJohn Baldwin }
254c825d4dcSJohn Baldwin 
255c825d4dcSJohn Baldwin static void
256c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res,
257c825d4dcSJohn Baldwin     int count)
258c825d4dcSJohn Baldwin {
259c825d4dcSJohn Baldwin 	struct resource **newarray;
260c825d4dcSJohn Baldwin 	int error, i;
261c825d4dcSJohn Baldwin 
262c825d4dcSJohn Baldwin 	newarray = malloc(sizeof(struct resource *) * (w->count + count),
263c825d4dcSJohn Baldwin 	    M_DEVBUF, M_WAITOK);
264c825d4dcSJohn Baldwin 	if (w->res != NULL)
265c825d4dcSJohn Baldwin 		bcopy(w->res, newarray, sizeof(struct resource *) * w->count);
266c825d4dcSJohn Baldwin 	bcopy(res, newarray + w->count, sizeof(struct resource *) * count);
267c825d4dcSJohn Baldwin 	free(w->res, M_DEVBUF);
268c825d4dcSJohn Baldwin 	w->res = newarray;
269c825d4dcSJohn Baldwin 	w->count += count;
270c825d4dcSJohn Baldwin 
271c825d4dcSJohn Baldwin 	for (i = 0; i < count; i++) {
272c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, rman_get_start(res[i]),
273c825d4dcSJohn Baldwin 		    rman_get_end(res[i]));
274c825d4dcSJohn Baldwin 		if (error)
275c825d4dcSJohn Baldwin 			panic("Failed to add resource to rman");
276c825d4dcSJohn Baldwin 	}
277c825d4dcSJohn Baldwin }
278c825d4dcSJohn Baldwin 
2792dd1bdf1SJustin Hibbits typedef void (nonisa_callback)(rman_res_t start, rman_res_t end, void *arg);
280c825d4dcSJohn Baldwin 
281c825d4dcSJohn Baldwin static void
2822dd1bdf1SJustin Hibbits pcib_walk_nonisa_ranges(rman_res_t start, rman_res_t end, nonisa_callback *cb,
283c825d4dcSJohn Baldwin     void *arg)
284c825d4dcSJohn Baldwin {
2852dd1bdf1SJustin Hibbits 	rman_res_t next_end;
286c825d4dcSJohn Baldwin 
287c825d4dcSJohn Baldwin 	/*
288c825d4dcSJohn Baldwin 	 * If start is within an ISA alias range, move up to the start
289c825d4dcSJohn Baldwin 	 * of the next non-alias range.  As a special case, addresses
290c825d4dcSJohn Baldwin 	 * in the range 0x000 - 0x0ff should also be skipped since
291c825d4dcSJohn Baldwin 	 * those are used for various system I/O devices in ISA
292c825d4dcSJohn Baldwin 	 * systems.
293c825d4dcSJohn Baldwin 	 */
294c825d4dcSJohn Baldwin 	if (start <= 65535) {
295c825d4dcSJohn Baldwin 		if (start < 0x100 || (start & 0x300) != 0) {
296c825d4dcSJohn Baldwin 			start &= ~0x3ff;
297c825d4dcSJohn Baldwin 			start += 0x400;
298c825d4dcSJohn Baldwin 		}
299c825d4dcSJohn Baldwin 	}
300c825d4dcSJohn Baldwin 
301c825d4dcSJohn Baldwin 	/* ISA aliases are only in the lower 64KB of I/O space. */
302c825d4dcSJohn Baldwin 	while (start <= MIN(end, 65535)) {
303c825d4dcSJohn Baldwin 		next_end = MIN(start | 0xff, end);
304c825d4dcSJohn Baldwin 		cb(start, next_end, arg);
305c825d4dcSJohn Baldwin 		start += 0x400;
306c825d4dcSJohn Baldwin 	}
307c825d4dcSJohn Baldwin 
308c825d4dcSJohn Baldwin 	if (start <= end)
309c825d4dcSJohn Baldwin 		cb(start, end, arg);
310c825d4dcSJohn Baldwin }
311c825d4dcSJohn Baldwin 
312c825d4dcSJohn Baldwin static void
3132dd1bdf1SJustin Hibbits count_ranges(rman_res_t start, rman_res_t end, void *arg)
314c825d4dcSJohn Baldwin {
315c825d4dcSJohn Baldwin 	int *countp;
316c825d4dcSJohn Baldwin 
317c825d4dcSJohn Baldwin 	countp = arg;
318c825d4dcSJohn Baldwin 	(*countp)++;
319c825d4dcSJohn Baldwin }
320c825d4dcSJohn Baldwin 
321c825d4dcSJohn Baldwin struct alloc_state {
322c825d4dcSJohn Baldwin 	struct resource **res;
323c825d4dcSJohn Baldwin 	struct pcib_softc *sc;
324c825d4dcSJohn Baldwin 	int count, error;
325c825d4dcSJohn Baldwin };
326c825d4dcSJohn Baldwin 
327c825d4dcSJohn Baldwin static void
3282dd1bdf1SJustin Hibbits alloc_ranges(rman_res_t start, rman_res_t end, void *arg)
329c825d4dcSJohn Baldwin {
330c825d4dcSJohn Baldwin 	struct alloc_state *as;
331c825d4dcSJohn Baldwin 	struct pcib_window *w;
332c825d4dcSJohn Baldwin 	int rid;
333c825d4dcSJohn Baldwin 
334c825d4dcSJohn Baldwin 	as = arg;
335c825d4dcSJohn Baldwin 	if (as->error != 0)
336c825d4dcSJohn Baldwin 		return;
337c825d4dcSJohn Baldwin 
338c825d4dcSJohn Baldwin 	w = &as->sc->io;
339c825d4dcSJohn Baldwin 	rid = w->reg;
340c825d4dcSJohn Baldwin 	if (bootverbose)
341c825d4dcSJohn Baldwin 		device_printf(as->sc->dev,
342c825d4dcSJohn Baldwin 		    "allocating non-ISA range %#lx-%#lx\n", start, end);
343c825d4dcSJohn Baldwin 	as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT,
344c825d4dcSJohn Baldwin 	    &rid, start, end, end - start + 1, 0);
345c825d4dcSJohn Baldwin 	if (as->res[as->count] == NULL)
346c825d4dcSJohn Baldwin 		as->error = ENXIO;
347c825d4dcSJohn Baldwin 	else
348c825d4dcSJohn Baldwin 		as->count++;
349c825d4dcSJohn Baldwin }
350c825d4dcSJohn Baldwin 
351c825d4dcSJohn Baldwin static int
3522dd1bdf1SJustin Hibbits pcib_alloc_nonisa_ranges(struct pcib_softc *sc, rman_res_t start, rman_res_t end)
353c825d4dcSJohn Baldwin {
354c825d4dcSJohn Baldwin 	struct alloc_state as;
355c825d4dcSJohn Baldwin 	int i, new_count;
356c825d4dcSJohn Baldwin 
357c825d4dcSJohn Baldwin 	/* First, see how many ranges we need. */
358c825d4dcSJohn Baldwin 	new_count = 0;
359c825d4dcSJohn Baldwin 	pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count);
360c825d4dcSJohn Baldwin 
361c825d4dcSJohn Baldwin 	/* Second, allocate the ranges. */
362c825d4dcSJohn Baldwin 	as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF,
363c825d4dcSJohn Baldwin 	    M_WAITOK);
364c825d4dcSJohn Baldwin 	as.sc = sc;
365c825d4dcSJohn Baldwin 	as.count = 0;
366c825d4dcSJohn Baldwin 	as.error = 0;
367c825d4dcSJohn Baldwin 	pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as);
368c825d4dcSJohn Baldwin 	if (as.error != 0) {
369c825d4dcSJohn Baldwin 		for (i = 0; i < as.count; i++)
370c825d4dcSJohn Baldwin 			bus_release_resource(sc->dev, SYS_RES_IOPORT,
371c825d4dcSJohn Baldwin 			    sc->io.reg, as.res[i]);
372c825d4dcSJohn Baldwin 		free(as.res, M_DEVBUF);
373c825d4dcSJohn Baldwin 		return (as.error);
374c825d4dcSJohn Baldwin 	}
375c825d4dcSJohn Baldwin 	KASSERT(as.count == new_count, ("%s: count mismatch", __func__));
376c825d4dcSJohn Baldwin 
377c825d4dcSJohn Baldwin 	/* Third, add the ranges to the window. */
378c825d4dcSJohn Baldwin 	pcib_add_window_resources(&sc->io, as.res, as.count);
379c825d4dcSJohn Baldwin 	free(as.res, M_DEVBUF);
380c825d4dcSJohn Baldwin 	return (0);
381c825d4dcSJohn Baldwin }
382c825d4dcSJohn Baldwin 
38383c41143SJohn Baldwin static void
38483c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type,
38583c41143SJohn Baldwin     int flags, pci_addr_t max_address)
38683c41143SJohn Baldwin {
387c825d4dcSJohn Baldwin 	struct resource *res;
38883c41143SJohn Baldwin 	char buf[64];
38983c41143SJohn Baldwin 	int error, rid;
39083c41143SJohn Baldwin 
39189977ce2SJustin Hibbits 	if (max_address != (rman_res_t)max_address)
392*534ccd7bSJustin Hibbits 		max_address = ~0;
39383c41143SJohn Baldwin 	w->rman.rm_start = 0;
39483c41143SJohn Baldwin 	w->rman.rm_end = max_address;
39583c41143SJohn Baldwin 	w->rman.rm_type = RMAN_ARRAY;
39683c41143SJohn Baldwin 	snprintf(buf, sizeof(buf), "%s %s window",
39783c41143SJohn Baldwin 	    device_get_nameunit(sc->dev), w->name);
39883c41143SJohn Baldwin 	w->rman.rm_descr = strdup(buf, M_DEVBUF);
39983c41143SJohn Baldwin 	error = rman_init(&w->rman);
40083c41143SJohn Baldwin 	if (error)
40183c41143SJohn Baldwin 		panic("Failed to initialize %s %s rman",
40283c41143SJohn Baldwin 		    device_get_nameunit(sc->dev), w->name);
40383c41143SJohn Baldwin 
40483c41143SJohn Baldwin 	if (!pcib_is_window_open(w))
40583c41143SJohn Baldwin 		return;
40683c41143SJohn Baldwin 
40783c41143SJohn Baldwin 	if (w->base > max_address || w->limit > max_address) {
40883c41143SJohn Baldwin 		device_printf(sc->dev,
40983c41143SJohn Baldwin 		    "initial %s window has too many bits, ignoring\n", w->name);
41083c41143SJohn Baldwin 		return;
41183c41143SJohn Baldwin 	}
412c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE)
413c825d4dcSJohn Baldwin 		(void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit);
414c825d4dcSJohn Baldwin 	else {
41583c41143SJohn Baldwin 		rid = w->reg;
416c825d4dcSJohn Baldwin 		res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit,
41783c41143SJohn Baldwin 		    w->limit - w->base + 1, flags);
418c825d4dcSJohn Baldwin 		if (res != NULL)
419c825d4dcSJohn Baldwin 			pcib_add_window_resources(w, &res, 1);
420c825d4dcSJohn Baldwin 	}
42183c41143SJohn Baldwin 	if (w->res == NULL) {
42283c41143SJohn Baldwin 		device_printf(sc->dev,
42383c41143SJohn Baldwin 		    "failed to allocate initial %s window: %#jx-%#jx\n",
42483c41143SJohn Baldwin 		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
42583c41143SJohn Baldwin 		w->base = max_address;
42683c41143SJohn Baldwin 		w->limit = 0;
42783c41143SJohn Baldwin 		pcib_write_windows(sc, w->mask);
42883c41143SJohn Baldwin 		return;
42983c41143SJohn Baldwin 	}
43083c41143SJohn Baldwin 	pcib_activate_window(sc, type);
43183c41143SJohn Baldwin }
43283c41143SJohn Baldwin 
43383c41143SJohn Baldwin /*
43483c41143SJohn Baldwin  * Initialize I/O windows.
43583c41143SJohn Baldwin  */
43683c41143SJohn Baldwin static void
43783c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc)
43883c41143SJohn Baldwin {
43983c41143SJohn Baldwin 	pci_addr_t max;
44083c41143SJohn Baldwin 	device_t dev;
44183c41143SJohn Baldwin 	uint32_t val;
44283c41143SJohn Baldwin 
44383c41143SJohn Baldwin 	dev = sc->dev;
44483c41143SJohn Baldwin 
4450070c94bSJohn Baldwin 	if (pci_clear_pcib) {
446809923caSJustin Hibbits 		pcib_bridge_init(dev);
4470070c94bSJohn Baldwin 	}
4480070c94bSJohn Baldwin 
44983c41143SJohn Baldwin 	/* Determine if the I/O port window is implemented. */
45083c41143SJohn Baldwin 	val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
45183c41143SJohn Baldwin 	if (val == 0) {
45283c41143SJohn Baldwin 		/*
45383c41143SJohn Baldwin 		 * If 'val' is zero, then only 16-bits of I/O space
45483c41143SJohn Baldwin 		 * are supported.
45583c41143SJohn Baldwin 		 */
45683c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
45783c41143SJohn Baldwin 		if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) {
45883c41143SJohn Baldwin 			sc->io.valid = 1;
45983c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOBASEL_1, 0, 1);
46083c41143SJohn Baldwin 		}
46183c41143SJohn Baldwin 	} else
46283c41143SJohn Baldwin 		sc->io.valid = 1;
46383c41143SJohn Baldwin 
46483c41143SJohn Baldwin 	/* Read the existing I/O port window. */
46583c41143SJohn Baldwin 	if (sc->io.valid) {
46683c41143SJohn Baldwin 		sc->io.reg = PCIR_IOBASEL_1;
46783c41143SJohn Baldwin 		sc->io.step = 12;
46883c41143SJohn Baldwin 		sc->io.mask = WIN_IO;
46983c41143SJohn Baldwin 		sc->io.name = "I/O port";
47083c41143SJohn Baldwin 		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
47183c41143SJohn Baldwin 			sc->io.base = PCI_PPBIOBASE(
47283c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOBASEH_1, 2), val);
47383c41143SJohn Baldwin 			sc->io.limit = PCI_PPBIOLIMIT(
47483c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITH_1, 2),
47583c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
47683c41143SJohn Baldwin 			max = 0xffffffff;
47783c41143SJohn Baldwin 		} else {
47883c41143SJohn Baldwin 			sc->io.base = PCI_PPBIOBASE(0, val);
47983c41143SJohn Baldwin 			sc->io.limit = PCI_PPBIOLIMIT(0,
48083c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
48183c41143SJohn Baldwin 			max = 0xffff;
48283c41143SJohn Baldwin 		}
48383c41143SJohn Baldwin 		pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max);
48483c41143SJohn Baldwin 	}
48583c41143SJohn Baldwin 
48683c41143SJohn Baldwin 	/* Read the existing memory window. */
48783c41143SJohn Baldwin 	sc->mem.valid = 1;
48883c41143SJohn Baldwin 	sc->mem.reg = PCIR_MEMBASE_1;
48983c41143SJohn Baldwin 	sc->mem.step = 20;
49083c41143SJohn Baldwin 	sc->mem.mask = WIN_MEM;
49183c41143SJohn Baldwin 	sc->mem.name = "memory";
49283c41143SJohn Baldwin 	sc->mem.base = PCI_PPBMEMBASE(0,
49383c41143SJohn Baldwin 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
49483c41143SJohn Baldwin 	sc->mem.limit = PCI_PPBMEMLIMIT(0,
49583c41143SJohn Baldwin 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
49683c41143SJohn Baldwin 	pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff);
49783c41143SJohn Baldwin 
49883c41143SJohn Baldwin 	/* Determine if the prefetchable memory window is implemented. */
49983c41143SJohn Baldwin 	val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
50083c41143SJohn Baldwin 	if (val == 0) {
50183c41143SJohn Baldwin 		/*
50283c41143SJohn Baldwin 		 * If 'val' is zero, then only 32-bits of memory space
50383c41143SJohn Baldwin 		 * are supported.
50483c41143SJohn Baldwin 		 */
50583c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
50683c41143SJohn Baldwin 		if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) {
50783c41143SJohn Baldwin 			sc->pmem.valid = 1;
50883c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMBASEL_1, 0, 2);
50983c41143SJohn Baldwin 		}
51083c41143SJohn Baldwin 	} else
51183c41143SJohn Baldwin 		sc->pmem.valid = 1;
51283c41143SJohn Baldwin 
51383c41143SJohn Baldwin 	/* Read the existing prefetchable memory window. */
51483c41143SJohn Baldwin 	if (sc->pmem.valid) {
51583c41143SJohn Baldwin 		sc->pmem.reg = PCIR_PMBASEL_1;
51683c41143SJohn Baldwin 		sc->pmem.step = 20;
51783c41143SJohn Baldwin 		sc->pmem.mask = WIN_PMEM;
51883c41143SJohn Baldwin 		sc->pmem.name = "prefetch";
51983c41143SJohn Baldwin 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
52083c41143SJohn Baldwin 			sc->pmem.base = PCI_PPBMEMBASE(
52183c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMBASEH_1, 4), val);
52283c41143SJohn Baldwin 			sc->pmem.limit = PCI_PPBMEMLIMIT(
52383c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITH_1, 4),
52483c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
52583c41143SJohn Baldwin 			max = 0xffffffffffffffff;
52683c41143SJohn Baldwin 		} else {
52783c41143SJohn Baldwin 			sc->pmem.base = PCI_PPBMEMBASE(0, val);
52883c41143SJohn Baldwin 			sc->pmem.limit = PCI_PPBMEMLIMIT(0,
52983c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
53083c41143SJohn Baldwin 			max = 0xffffffff;
53183c41143SJohn Baldwin 		}
53283c41143SJohn Baldwin 		pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY,
53383c41143SJohn Baldwin 		    RF_PREFETCHABLE, max);
53483c41143SJohn Baldwin 	}
53583c41143SJohn Baldwin }
53683c41143SJohn Baldwin 
5374edef187SJohn Baldwin #ifdef PCI_RES_BUS
5384edef187SJohn Baldwin /*
5394edef187SJohn Baldwin  * Allocate a suitable secondary bus for this bridge if needed and
5404edef187SJohn Baldwin  * initialize the resource manager for the secondary bus range.  Note
5414edef187SJohn Baldwin  * that the minimum count is a desired value and this may allocate a
5424edef187SJohn Baldwin  * smaller range.
5434edef187SJohn Baldwin  */
5444edef187SJohn Baldwin void
5454edef187SJohn Baldwin pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count)
5464edef187SJohn Baldwin {
5474edef187SJohn Baldwin 	char buf[64];
548ad6f36f8SJohn Baldwin 	int error, rid, sec_reg;
5494edef187SJohn Baldwin 
5504edef187SJohn Baldwin 	switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) {
5514edef187SJohn Baldwin 	case PCIM_HDRTYPE_BRIDGE:
552ad6f36f8SJohn Baldwin 		sec_reg = PCIR_SECBUS_1;
5534edef187SJohn Baldwin 		bus->sub_reg = PCIR_SUBBUS_1;
5544edef187SJohn Baldwin 		break;
5554edef187SJohn Baldwin 	case PCIM_HDRTYPE_CARDBUS:
556ad6f36f8SJohn Baldwin 		sec_reg = PCIR_SECBUS_2;
5574edef187SJohn Baldwin 		bus->sub_reg = PCIR_SUBBUS_2;
5584edef187SJohn Baldwin 		break;
5594edef187SJohn Baldwin 	default:
5604edef187SJohn Baldwin 		panic("not a PCI bridge");
5614edef187SJohn Baldwin 	}
562ad6f36f8SJohn Baldwin 	bus->sec = pci_read_config(dev, sec_reg, 1);
563ad6f36f8SJohn Baldwin 	bus->sub = pci_read_config(dev, bus->sub_reg, 1);
5644edef187SJohn Baldwin 	bus->dev = dev;
5654edef187SJohn Baldwin 	bus->rman.rm_start = 0;
5664edef187SJohn Baldwin 	bus->rman.rm_end = PCI_BUSMAX;
5674edef187SJohn Baldwin 	bus->rman.rm_type = RMAN_ARRAY;
5684edef187SJohn Baldwin 	snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev));
5694edef187SJohn Baldwin 	bus->rman.rm_descr = strdup(buf, M_DEVBUF);
5704edef187SJohn Baldwin 	error = rman_init(&bus->rman);
5714edef187SJohn Baldwin 	if (error)
5724edef187SJohn Baldwin 		panic("Failed to initialize %s bus number rman",
5734edef187SJohn Baldwin 		    device_get_nameunit(dev));
5744edef187SJohn Baldwin 
5754edef187SJohn Baldwin 	/*
5764edef187SJohn Baldwin 	 * Allocate a bus range.  This will return an existing bus range
5774edef187SJohn Baldwin 	 * if one exists, or a new bus range if one does not.
5784edef187SJohn Baldwin 	 */
5794edef187SJohn Baldwin 	rid = 0;
580c47476d7SJustin Hibbits 	bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid,
5814edef187SJohn Baldwin 	    min_count, 0);
5824edef187SJohn Baldwin 	if (bus->res == NULL) {
5834edef187SJohn Baldwin 		/*
5844edef187SJohn Baldwin 		 * Fall back to just allocating a range of a single bus
5854edef187SJohn Baldwin 		 * number.
5864edef187SJohn Baldwin 		 */
587c47476d7SJustin Hibbits 		bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid,
5884edef187SJohn Baldwin 		    1, 0);
5894edef187SJohn Baldwin 	} else if (rman_get_size(bus->res) < min_count)
5904edef187SJohn Baldwin 		/*
5914edef187SJohn Baldwin 		 * Attempt to grow the existing range to satisfy the
5924edef187SJohn Baldwin 		 * minimum desired count.
5934edef187SJohn Baldwin 		 */
5944edef187SJohn Baldwin 		(void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res,
5954edef187SJohn Baldwin 		    rman_get_start(bus->res), rman_get_start(bus->res) +
5964edef187SJohn Baldwin 		    min_count - 1);
5974edef187SJohn Baldwin 
5984edef187SJohn Baldwin 	/*
5994edef187SJohn Baldwin 	 * Add the initial resource to the rman.
6004edef187SJohn Baldwin 	 */
6014edef187SJohn Baldwin 	if (bus->res != NULL) {
6024edef187SJohn Baldwin 		error = rman_manage_region(&bus->rman, rman_get_start(bus->res),
6034edef187SJohn Baldwin 		    rman_get_end(bus->res));
6044edef187SJohn Baldwin 		if (error)
6054edef187SJohn Baldwin 			panic("Failed to add resource to rman");
6064edef187SJohn Baldwin 		bus->sec = rman_get_start(bus->res);
6074edef187SJohn Baldwin 		bus->sub = rman_get_end(bus->res);
6084edef187SJohn Baldwin 	}
6094edef187SJohn Baldwin }
6104edef187SJohn Baldwin 
6114edef187SJohn Baldwin static struct resource *
6124edef187SJohn Baldwin pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid,
6132dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
6144edef187SJohn Baldwin {
6154edef187SJohn Baldwin 	struct resource *res;
6164edef187SJohn Baldwin 
6174edef187SJohn Baldwin 	res = rman_reserve_resource(&bus->rman, start, end, count, flags,
6184edef187SJohn Baldwin 	    child);
6194edef187SJohn Baldwin 	if (res == NULL)
6204edef187SJohn Baldwin 		return (NULL);
6214edef187SJohn Baldwin 
6224edef187SJohn Baldwin 	if (bootverbose)
6234edef187SJohn Baldwin 		device_printf(bus->dev,
6244edef187SJohn Baldwin 		    "allocated bus range (%lu-%lu) for rid %d of %s\n",
6254edef187SJohn Baldwin 		    rman_get_start(res), rman_get_end(res), *rid,
6264edef187SJohn Baldwin 		    pcib_child_name(child));
6274edef187SJohn Baldwin 	rman_set_rid(res, *rid);
6284edef187SJohn Baldwin 	return (res);
6294edef187SJohn Baldwin }
6304edef187SJohn Baldwin 
6314edef187SJohn Baldwin /*
6324edef187SJohn Baldwin  * Attempt to grow the secondary bus range.  This is much simpler than
6334edef187SJohn Baldwin  * for I/O windows as the range can only be grown by increasing
6344edef187SJohn Baldwin  * subbus.
6354edef187SJohn Baldwin  */
6364edef187SJohn Baldwin static int
6372dd1bdf1SJustin Hibbits pcib_grow_subbus(struct pcib_secbus *bus, rman_res_t new_end)
6384edef187SJohn Baldwin {
6392dd1bdf1SJustin Hibbits 	rman_res_t old_end;
6404edef187SJohn Baldwin 	int error;
6414edef187SJohn Baldwin 
6424edef187SJohn Baldwin 	old_end = rman_get_end(bus->res);
6434edef187SJohn Baldwin 	KASSERT(new_end > old_end, ("attempt to shrink subbus"));
6444edef187SJohn Baldwin 	error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res,
6454edef187SJohn Baldwin 	    rman_get_start(bus->res), new_end);
6464edef187SJohn Baldwin 	if (error)
6474edef187SJohn Baldwin 		return (error);
6484edef187SJohn Baldwin 	if (bootverbose)
6494edef187SJohn Baldwin 		device_printf(bus->dev, "grew bus range to %lu-%lu\n",
6504edef187SJohn Baldwin 		    rman_get_start(bus->res), rman_get_end(bus->res));
6514edef187SJohn Baldwin 	error = rman_manage_region(&bus->rman, old_end + 1,
6524edef187SJohn Baldwin 	    rman_get_end(bus->res));
6534edef187SJohn Baldwin 	if (error)
6544edef187SJohn Baldwin 		panic("Failed to add resource to rman");
6554edef187SJohn Baldwin 	bus->sub = rman_get_end(bus->res);
6564edef187SJohn Baldwin 	pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1);
6574edef187SJohn Baldwin 	return (0);
6584edef187SJohn Baldwin }
6594edef187SJohn Baldwin 
6604edef187SJohn Baldwin struct resource *
6614edef187SJohn Baldwin pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid,
6622dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
6634edef187SJohn Baldwin {
6644edef187SJohn Baldwin 	struct resource *res;
6652dd1bdf1SJustin Hibbits 	rman_res_t start_free, end_free, new_end;
6664edef187SJohn Baldwin 
6674edef187SJohn Baldwin 	/*
6684edef187SJohn Baldwin 	 * First, see if the request can be satisified by the existing
6694edef187SJohn Baldwin 	 * bus range.
6704edef187SJohn Baldwin 	 */
6714edef187SJohn Baldwin 	res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags);
6724edef187SJohn Baldwin 	if (res != NULL)
6734edef187SJohn Baldwin 		return (res);
6744edef187SJohn Baldwin 
6754edef187SJohn Baldwin 	/*
6764edef187SJohn Baldwin 	 * Figure out a range to grow the bus range.  First, find the
6774edef187SJohn Baldwin 	 * first bus number after the last allocated bus in the rman and
6784edef187SJohn Baldwin 	 * enforce that as a minimum starting point for the range.
6794edef187SJohn Baldwin 	 */
6804edef187SJohn Baldwin 	if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 ||
6814edef187SJohn Baldwin 	    end_free != bus->sub)
6824edef187SJohn Baldwin 		start_free = bus->sub + 1;
6834edef187SJohn Baldwin 	if (start_free < start)
6844edef187SJohn Baldwin 		start_free = start;
6854edef187SJohn Baldwin 	new_end = start_free + count - 1;
6864edef187SJohn Baldwin 
6874edef187SJohn Baldwin 	/*
6884edef187SJohn Baldwin 	 * See if this new range would satisfy the request if it
6894edef187SJohn Baldwin 	 * succeeds.
6904edef187SJohn Baldwin 	 */
6914edef187SJohn Baldwin 	if (new_end > end)
6924edef187SJohn Baldwin 		return (NULL);
6934edef187SJohn Baldwin 
6944edef187SJohn Baldwin 	/* Finally, attempt to grow the existing resource. */
6954edef187SJohn Baldwin 	if (bootverbose) {
6964edef187SJohn Baldwin 		device_printf(bus->dev,
6974edef187SJohn Baldwin 		    "attempting to grow bus range for %lu buses\n", count);
6984edef187SJohn Baldwin 		printf("\tback candidate range: %lu-%lu\n", start_free,
6994edef187SJohn Baldwin 		    new_end);
7004edef187SJohn Baldwin 	}
7014edef187SJohn Baldwin 	if (pcib_grow_subbus(bus, new_end) == 0)
7024edef187SJohn Baldwin 		return (pcib_suballoc_bus(bus, child, rid, start, end, count,
7034edef187SJohn Baldwin 		    flags));
7044edef187SJohn Baldwin 	return (NULL);
7054edef187SJohn Baldwin }
7064edef187SJohn Baldwin #endif
7074edef187SJohn Baldwin 
70883c41143SJohn Baldwin #else
70983c41143SJohn Baldwin 
710bb0d0a8eSMike Smith /*
711b0a2d4b8SWarner Losh  * Is the prefetch window open (eg, can we allocate memory in it?)
712b0a2d4b8SWarner Losh  */
713b0a2d4b8SWarner Losh static int
714b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc)
715b0a2d4b8SWarner Losh {
716b0a2d4b8SWarner Losh 	return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
717b0a2d4b8SWarner Losh }
718b0a2d4b8SWarner Losh 
719b0a2d4b8SWarner Losh /*
720b0a2d4b8SWarner Losh  * Is the nonprefetch window open (eg, can we allocate memory in it?)
721b0a2d4b8SWarner Losh  */
722b0a2d4b8SWarner Losh static int
723b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc)
724b0a2d4b8SWarner Losh {
725b0a2d4b8SWarner Losh 	return (sc->membase > 0 && sc->membase < sc->memlimit);
726b0a2d4b8SWarner Losh }
727b0a2d4b8SWarner Losh 
728b0a2d4b8SWarner Losh /*
729b0a2d4b8SWarner Losh  * Is the io window open (eg, can we allocate ports in it?)
730b0a2d4b8SWarner Losh  */
731b0a2d4b8SWarner Losh static int
732b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc)
733b0a2d4b8SWarner Losh {
734b0a2d4b8SWarner Losh 	return (sc->iobase > 0 && sc->iobase < sc->iolimit);
735b0a2d4b8SWarner Losh }
736b0a2d4b8SWarner Losh 
737b0a2d4b8SWarner Losh /*
738e36af292SJung-uk Kim  * Get current I/O decode.
739e36af292SJung-uk Kim  */
740e36af292SJung-uk Kim static void
741e36af292SJung-uk Kim pcib_get_io_decode(struct pcib_softc *sc)
742e36af292SJung-uk Kim {
743e36af292SJung-uk Kim 	device_t	dev;
744e36af292SJung-uk Kim 	uint32_t	iolow;
745e36af292SJung-uk Kim 
746e36af292SJung-uk Kim 	dev = sc->dev;
747e36af292SJung-uk Kim 
748e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
749e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
750e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(
751e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow);
752e36af292SJung-uk Kim 	else
753e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(0, iolow);
754e36af292SJung-uk Kim 
755e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
756e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
757e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(
758e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow);
759e36af292SJung-uk Kim 	else
760e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(0, iolow);
761e36af292SJung-uk Kim }
762e36af292SJung-uk Kim 
763e36af292SJung-uk Kim /*
764e36af292SJung-uk Kim  * Get current memory decode.
765e36af292SJung-uk Kim  */
766e36af292SJung-uk Kim static void
767e36af292SJung-uk Kim pcib_get_mem_decode(struct pcib_softc *sc)
768e36af292SJung-uk Kim {
769e36af292SJung-uk Kim 	device_t	dev;
770e36af292SJung-uk Kim 	pci_addr_t	pmemlow;
771e36af292SJung-uk Kim 
772e36af292SJung-uk Kim 	dev = sc->dev;
773e36af292SJung-uk Kim 
774e36af292SJung-uk Kim 	sc->membase = PCI_PPBMEMBASE(0,
775e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
776e36af292SJung-uk Kim 	sc->memlimit = PCI_PPBMEMLIMIT(0,
777e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
778e36af292SJung-uk Kim 
779e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2);
780e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
781e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(
782e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow);
783e36af292SJung-uk Kim 	else
784e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(0, pmemlow);
785e36af292SJung-uk Kim 
786e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2);
787e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
788e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(
789e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow);
790e36af292SJung-uk Kim 	else
791e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow);
792e36af292SJung-uk Kim }
793e36af292SJung-uk Kim 
794e36af292SJung-uk Kim /*
795e36af292SJung-uk Kim  * Restore previous I/O decode.
796e36af292SJung-uk Kim  */
797e36af292SJung-uk Kim static void
798e36af292SJung-uk Kim pcib_set_io_decode(struct pcib_softc *sc)
799e36af292SJung-uk Kim {
800e36af292SJung-uk Kim 	device_t	dev;
801e36af292SJung-uk Kim 	uint32_t	iohi;
802e36af292SJung-uk Kim 
803e36af292SJung-uk Kim 	dev = sc->dev;
804e36af292SJung-uk Kim 
805e36af292SJung-uk Kim 	iohi = sc->iobase >> 16;
806e36af292SJung-uk Kim 	if (iohi > 0)
807e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2);
808e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1);
809e36af292SJung-uk Kim 
810e36af292SJung-uk Kim 	iohi = sc->iolimit >> 16;
811e36af292SJung-uk Kim 	if (iohi > 0)
812e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2);
813e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1);
814e36af292SJung-uk Kim }
815e36af292SJung-uk Kim 
816e36af292SJung-uk Kim /*
817e36af292SJung-uk Kim  * Restore previous memory decode.
818e36af292SJung-uk Kim  */
819e36af292SJung-uk Kim static void
820e36af292SJung-uk Kim pcib_set_mem_decode(struct pcib_softc *sc)
821e36af292SJung-uk Kim {
822e36af292SJung-uk Kim 	device_t	dev;
823e36af292SJung-uk Kim 	pci_addr_t	pmemhi;
824e36af292SJung-uk Kim 
825e36af292SJung-uk Kim 	dev = sc->dev;
826e36af292SJung-uk Kim 
827e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2);
828e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2);
829e36af292SJung-uk Kim 
830e36af292SJung-uk Kim 	pmemhi = sc->pmembase >> 32;
831e36af292SJung-uk Kim 	if (pmemhi > 0)
832e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4);
833e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2);
834e36af292SJung-uk Kim 
835e36af292SJung-uk Kim 	pmemhi = sc->pmemlimit >> 32;
836e36af292SJung-uk Kim 	if (pmemhi > 0)
837e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4);
838e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2);
839e36af292SJung-uk Kim }
84083c41143SJohn Baldwin #endif
841e36af292SJung-uk Kim 
842e36af292SJung-uk Kim /*
843e36af292SJung-uk Kim  * Get current bridge configuration.
844e36af292SJung-uk Kim  */
845e36af292SJung-uk Kim static void
846e36af292SJung-uk Kim pcib_cfg_save(struct pcib_softc *sc)
847e36af292SJung-uk Kim {
848ad6f36f8SJohn Baldwin #ifndef NEW_PCIB
849e36af292SJung-uk Kim 	device_t	dev;
850ad6f36f8SJohn Baldwin 	uint16_t command;
851e36af292SJung-uk Kim 
852e36af292SJung-uk Kim 	dev = sc->dev;
853e36af292SJung-uk Kim 
854ad6f36f8SJohn Baldwin 	command = pci_read_config(dev, PCIR_COMMAND, 2);
855ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_PORTEN)
856e36af292SJung-uk Kim 		pcib_get_io_decode(sc);
857ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_MEMEN)
858e36af292SJung-uk Kim 		pcib_get_mem_decode(sc);
85983c41143SJohn Baldwin #endif
860e36af292SJung-uk Kim }
861e36af292SJung-uk Kim 
862e36af292SJung-uk Kim /*
863e36af292SJung-uk Kim  * Restore previous bridge configuration.
864e36af292SJung-uk Kim  */
865e36af292SJung-uk Kim static void
866e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc)
867e36af292SJung-uk Kim {
868e36af292SJung-uk Kim 	device_t	dev;
869ad6f36f8SJohn Baldwin #ifndef NEW_PCIB
870ad6f36f8SJohn Baldwin 	uint16_t command;
871ad6f36f8SJohn Baldwin #endif
872e36af292SJung-uk Kim 	dev = sc->dev;
873e36af292SJung-uk Kim 
87483c41143SJohn Baldwin #ifdef NEW_PCIB
87583c41143SJohn Baldwin 	pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM);
87683c41143SJohn Baldwin #else
877ad6f36f8SJohn Baldwin 	command = pci_read_config(dev, PCIR_COMMAND, 2);
878ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_PORTEN)
879e36af292SJung-uk Kim 		pcib_set_io_decode(sc);
880ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_MEMEN)
881e36af292SJung-uk Kim 		pcib_set_mem_decode(sc);
88283c41143SJohn Baldwin #endif
883e36af292SJung-uk Kim }
884e36af292SJung-uk Kim 
885e36af292SJung-uk Kim /*
886bb0d0a8eSMike Smith  * Generic device interface
887bb0d0a8eSMike Smith  */
888bb0d0a8eSMike Smith static int
889bb0d0a8eSMike Smith pcib_probe(device_t dev)
890bb0d0a8eSMike Smith {
891bb0d0a8eSMike Smith     if ((pci_get_class(dev) == PCIC_BRIDGE) &&
892bb0d0a8eSMike Smith 	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
893bb0d0a8eSMike Smith 	device_set_desc(dev, "PCI-PCI bridge");
894b7cbd25bSMarcel Moolenaar 	return(-10000);
895bb0d0a8eSMike Smith     }
896bb0d0a8eSMike Smith     return(ENXIO);
897bb0d0a8eSMike Smith }
898bb0d0a8eSMike Smith 
8996f0d5884SJohn Baldwin void
9006f0d5884SJohn Baldwin pcib_attach_common(device_t dev)
901bb0d0a8eSMike Smith {
902bb0d0a8eSMike Smith     struct pcib_softc	*sc;
903abf07f13SWarner Losh     struct sysctl_ctx_list *sctx;
904abf07f13SWarner Losh     struct sysctl_oid	*soid;
905c825d4dcSJohn Baldwin     int comma;
906bb0d0a8eSMike Smith 
907bb0d0a8eSMike Smith     sc = device_get_softc(dev);
908bb0d0a8eSMike Smith     sc->dev = dev;
909bb0d0a8eSMike Smith 
9104fa59183SMike Smith     /*
9114fa59183SMike Smith      * Get current bridge configuration.
9124fa59183SMike Smith      */
91355aaf894SMarius Strobl     sc->domain = pci_get_domain(dev);
914ad6f36f8SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
915ad6f36f8SJohn Baldwin     sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1);
916ad6f36f8SJohn Baldwin     sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
917ad6f36f8SJohn Baldwin #endif
918ad6f36f8SJohn Baldwin     sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
919e36af292SJung-uk Kim     pcib_cfg_save(sc);
9204fa59183SMike Smith 
9214fa59183SMike Smith     /*
9224edef187SJohn Baldwin      * The primary bus register should always be the bus of the
9234edef187SJohn Baldwin      * parent.
9244edef187SJohn Baldwin      */
9254edef187SJohn Baldwin     sc->pribus = pci_get_bus(dev);
9264edef187SJohn Baldwin     pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
9274edef187SJohn Baldwin 
9284edef187SJohn Baldwin     /*
929abf07f13SWarner Losh      * Setup sysctl reporting nodes
930abf07f13SWarner Losh      */
931abf07f13SWarner Losh     sctx = device_get_sysctl_ctx(dev);
932abf07f13SWarner Losh     soid = device_get_sysctl_tree(dev);
933abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
934abf07f13SWarner Losh       CTLFLAG_RD, &sc->domain, 0, "Domain number");
935abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
936abf07f13SWarner Losh       CTLFLAG_RD, &sc->pribus, 0, "Primary bus number");
937abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
9384edef187SJohn Baldwin       CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number");
939abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
9404edef187SJohn Baldwin       CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number");
941abf07f13SWarner Losh 
942abf07f13SWarner Losh     /*
9434fa59183SMike Smith      * Quirk handling.
9444fa59183SMike Smith      */
9454fa59183SMike Smith     switch (pci_get_devid(dev)) {
9462ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
9474fa59183SMike Smith     case 0x12258086:		/* Intel 82454KX/GX (Orion) */
9484fa59183SMike Smith 	{
949b0cb115fSWarner Losh 	    uint8_t	supbus;
9504fa59183SMike Smith 
9514fa59183SMike Smith 	    supbus = pci_read_config(dev, 0x41, 1);
9524fa59183SMike Smith 	    if (supbus != 0xff) {
9534edef187SJohn Baldwin 		sc->bus.sec = supbus + 1;
9544edef187SJohn Baldwin 		sc->bus.sub = supbus + 1;
9554fa59183SMike Smith 	    }
9564fa59183SMike Smith 	    break;
9574fa59183SMike Smith 	}
9584edef187SJohn Baldwin #endif
9594fa59183SMike Smith 
960e4b59fc5SWarner Losh     /*
961e4b59fc5SWarner Losh      * The i82380FB mobile docking controller is a PCI-PCI bridge,
962e4b59fc5SWarner Losh      * and it is a subtractive bridge.  However, the ProgIf is wrong
963e4b59fc5SWarner Losh      * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
9644718610dSZbigniew Bodek      * happen.  There are also Toshiba and Cavium ThunderX bridges
9654718610dSZbigniew Bodek      * that behave this way.
966e4b59fc5SWarner Losh      */
9674718610dSZbigniew Bodek     case 0xa002177d:		/* Cavium ThunderX */
968e4b59fc5SWarner Losh     case 0x124b8086:		/* Intel 82380FB Mobile */
969e4b59fc5SWarner Losh     case 0x060513d7:		/* Toshiba ???? */
970e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
971e4b59fc5SWarner Losh 	break;
972c94d6dbeSJung-uk Kim 
9732ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
974c94d6dbeSJung-uk Kim     /* Compaq R3000 BIOS sets wrong subordinate bus number. */
975c94d6dbeSJung-uk Kim     case 0x00dd10de:
976c94d6dbeSJung-uk Kim 	{
977c94d6dbeSJung-uk Kim 	    char *cp;
978c94d6dbeSJung-uk Kim 
9792be111bfSDavide Italiano 	    if ((cp = kern_getenv("smbios.planar.maker")) == NULL)
980c94d6dbeSJung-uk Kim 		break;
9811def0ca6SJung-uk Kim 	    if (strncmp(cp, "Compal", 6) != 0) {
9821def0ca6SJung-uk Kim 		freeenv(cp);
983c94d6dbeSJung-uk Kim 		break;
9841def0ca6SJung-uk Kim 	    }
9851def0ca6SJung-uk Kim 	    freeenv(cp);
9862be111bfSDavide Italiano 	    if ((cp = kern_getenv("smbios.planar.product")) == NULL)
9871def0ca6SJung-uk Kim 		break;
9881def0ca6SJung-uk Kim 	    if (strncmp(cp, "08A0", 4) != 0) {
9891def0ca6SJung-uk Kim 		freeenv(cp);
9901def0ca6SJung-uk Kim 		break;
9911def0ca6SJung-uk Kim 	    }
9921def0ca6SJung-uk Kim 	    freeenv(cp);
9934edef187SJohn Baldwin 	    if (sc->bus.sub < 0xa) {
994c94d6dbeSJung-uk Kim 		pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
9954edef187SJohn Baldwin 		sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
996c94d6dbeSJung-uk Kim 	    }
997c94d6dbeSJung-uk Kim 	    break;
998c94d6dbeSJung-uk Kim 	}
9994edef187SJohn Baldwin #endif
1000e4b59fc5SWarner Losh     }
1001e4b59fc5SWarner Losh 
100222bf1c7fSJohn Baldwin     if (pci_msi_device_blacklisted(dev))
100322bf1c7fSJohn Baldwin 	sc->flags |= PCIB_DISABLE_MSI;
100422bf1c7fSJohn Baldwin 
100568e9cbd3SMarius Strobl     if (pci_msix_device_blacklisted(dev))
100668e9cbd3SMarius Strobl 	sc->flags |= PCIB_DISABLE_MSIX;
100768e9cbd3SMarius Strobl 
1008e4b59fc5SWarner Losh     /*
1009e4b59fc5SWarner Losh      * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
1010e4b59fc5SWarner Losh      * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
1011e4b59fc5SWarner Losh      * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
1012e4b59fc5SWarner Losh      * This means they act as if they were subtractively decoding
1013e4b59fc5SWarner Losh      * bridges and pass all transactions.  Mark them and real ProgIf 1
1014e4b59fc5SWarner Losh      * parts as subtractive.
1015e4b59fc5SWarner Losh      */
1016e4b59fc5SWarner Losh     if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
1017657d9f9fSJohn Baldwin       pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
1018e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
1019e4b59fc5SWarner Losh 
102083c41143SJohn Baldwin #ifdef NEW_PCIB
10214edef187SJohn Baldwin #ifdef PCI_RES_BUS
10224edef187SJohn Baldwin     pcib_setup_secbus(dev, &sc->bus, 1);
10234edef187SJohn Baldwin #endif
102483c41143SJohn Baldwin     pcib_probe_windows(sc);
102583c41143SJohn Baldwin #endif
1026bb0d0a8eSMike Smith     if (bootverbose) {
102755aaf894SMarius Strobl 	device_printf(dev, "  domain            %d\n", sc->domain);
10284edef187SJohn Baldwin 	device_printf(dev, "  secondary bus     %d\n", sc->bus.sec);
10294edef187SJohn Baldwin 	device_printf(dev, "  subordinate bus   %d\n", sc->bus.sub);
103083c41143SJohn Baldwin #ifdef NEW_PCIB
103183c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->io))
103283c41143SJohn Baldwin 	    device_printf(dev, "  I/O decode        0x%jx-0x%jx\n",
103383c41143SJohn Baldwin 	      (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit);
103483c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->mem))
103583c41143SJohn Baldwin 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
103683c41143SJohn Baldwin 	      (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit);
103783c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->pmem))
103883c41143SJohn Baldwin 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
103983c41143SJohn Baldwin 	      (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit);
104083c41143SJohn Baldwin #else
104183c41143SJohn Baldwin 	if (pcib_is_io_open(sc))
104283c41143SJohn Baldwin 	    device_printf(dev, "  I/O decode        0x%x-0x%x\n",
104383c41143SJohn Baldwin 	      sc->iobase, sc->iolimit);
1044b0a2d4b8SWarner Losh 	if (pcib_is_nonprefetch_open(sc))
1045b0a2d4b8SWarner Losh 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
1046b0a2d4b8SWarner Losh 	      (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
1047b0a2d4b8SWarner Losh 	if (pcib_is_prefetch_open(sc))
1048b0a2d4b8SWarner Losh 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
1049b0a2d4b8SWarner Losh 	      (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
105083c41143SJohn Baldwin #endif
1051c825d4dcSJohn Baldwin 	if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) ||
1052c825d4dcSJohn Baldwin 	    sc->flags & PCIB_SUBTRACTIVE) {
1053c825d4dcSJohn Baldwin 		device_printf(dev, "  special decode    ");
1054c825d4dcSJohn Baldwin 		comma = 0;
1055c825d4dcSJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) {
1056c825d4dcSJohn Baldwin 			printf("ISA");
1057c825d4dcSJohn Baldwin 			comma = 1;
1058c825d4dcSJohn Baldwin 		}
1059c825d4dcSJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) {
1060c825d4dcSJohn Baldwin 			printf("%sVGA", comma ? ", " : "");
1061c825d4dcSJohn Baldwin 			comma = 1;
1062c825d4dcSJohn Baldwin 		}
1063e4b59fc5SWarner Losh 		if (sc->flags & PCIB_SUBTRACTIVE)
1064c825d4dcSJohn Baldwin 			printf("%ssubtractive", comma ? ", " : "");
1065c825d4dcSJohn Baldwin 		printf("\n");
1066c825d4dcSJohn Baldwin 	}
1067bb0d0a8eSMike Smith     }
1068bb0d0a8eSMike Smith 
1069bb0d0a8eSMike Smith     /*
1070ef888152SJohn Baldwin      * Always enable busmastering on bridges so that transactions
1071ef888152SJohn Baldwin      * initiated on the secondary bus are passed through to the
1072ef888152SJohn Baldwin      * primary bus.
1073ef888152SJohn Baldwin      */
1074ef888152SJohn Baldwin     pci_enable_busmaster(dev);
10756f0d5884SJohn Baldwin }
1076bb0d0a8eSMike Smith 
107738906aedSJohn Baldwin int
10786f0d5884SJohn Baldwin pcib_attach(device_t dev)
10796f0d5884SJohn Baldwin {
10806f0d5884SJohn Baldwin     struct pcib_softc	*sc;
10816f0d5884SJohn Baldwin     device_t		child;
10826f0d5884SJohn Baldwin 
10836f0d5884SJohn Baldwin     pcib_attach_common(dev);
10846f0d5884SJohn Baldwin     sc = device_get_softc(dev);
10854edef187SJohn Baldwin     if (sc->bus.sec != 0) {
108618c72666SZbigniew Bodek 	child = device_add_child(dev, "pci", -1);
1087bb0d0a8eSMike Smith 	if (child != NULL)
1088bb0d0a8eSMike Smith 	    return(bus_generic_attach(dev));
1089bb0d0a8eSMike Smith     }
1090bb0d0a8eSMike Smith 
1091bb0d0a8eSMike Smith     /* no secondary bus; we should have fixed this */
1092bb0d0a8eSMike Smith     return(0);
1093bb0d0a8eSMike Smith }
1094bb0d0a8eSMike Smith 
10956f0d5884SJohn Baldwin int
1096e36af292SJung-uk Kim pcib_suspend(device_t dev)
1097e36af292SJung-uk Kim {
1098e36af292SJung-uk Kim 
1099e36af292SJung-uk Kim 	pcib_cfg_save(device_get_softc(dev));
11007212fc6aSJohn Baldwin 	return (bus_generic_suspend(dev));
1101e36af292SJung-uk Kim }
1102e36af292SJung-uk Kim 
1103e36af292SJung-uk Kim int
1104e36af292SJung-uk Kim pcib_resume(device_t dev)
1105e36af292SJung-uk Kim {
1106e36af292SJung-uk Kim 
1107e36af292SJung-uk Kim 	pcib_cfg_restore(device_get_softc(dev));
1108e36af292SJung-uk Kim 	return (bus_generic_resume(dev));
1109e36af292SJung-uk Kim }
1110e36af292SJung-uk Kim 
1111809923caSJustin Hibbits void
1112809923caSJustin Hibbits pcib_bridge_init(device_t dev)
1113809923caSJustin Hibbits {
1114809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
1115809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2);
1116809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1);
1117809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2);
1118809923caSJustin Hibbits 	pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2);
1119809923caSJustin Hibbits 	pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2);
1120809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
1121809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4);
1122809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2);
1123809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4);
1124809923caSJustin Hibbits }
1125809923caSJustin Hibbits 
1126e36af292SJung-uk Kim int
1127bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1128bb0d0a8eSMike Smith {
1129bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
1130bb0d0a8eSMike Smith 
1131bb0d0a8eSMike Smith     switch (which) {
113255aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
113355aaf894SMarius Strobl 	*result = sc->domain;
113455aaf894SMarius Strobl 	return(0);
1135bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
11364edef187SJohn Baldwin 	*result = sc->bus.sec;
1137bb0d0a8eSMike Smith 	return(0);
1138bb0d0a8eSMike Smith     }
1139bb0d0a8eSMike Smith     return(ENOENT);
1140bb0d0a8eSMike Smith }
1141bb0d0a8eSMike Smith 
11426f0d5884SJohn Baldwin int
1143bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1144bb0d0a8eSMike Smith {
1145bb0d0a8eSMike Smith 
1146bb0d0a8eSMike Smith     switch (which) {
114755aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
114855aaf894SMarius Strobl 	return(EINVAL);
1149bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
11504edef187SJohn Baldwin 	return(EINVAL);
1151bb0d0a8eSMike Smith     }
1152bb0d0a8eSMike Smith     return(ENOENT);
1153bb0d0a8eSMike Smith }
1154bb0d0a8eSMike Smith 
115583c41143SJohn Baldwin #ifdef NEW_PCIB
115683c41143SJohn Baldwin /*
115783c41143SJohn Baldwin  * Attempt to allocate a resource from the existing resources assigned
115883c41143SJohn Baldwin  * to a window.
115983c41143SJohn Baldwin  */
116083c41143SJohn Baldwin static struct resource *
116183c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w,
11622dd1bdf1SJustin Hibbits     device_t child, int type, int *rid, rman_res_t start, rman_res_t end,
11632dd1bdf1SJustin Hibbits     rman_res_t count, u_int flags)
116483c41143SJohn Baldwin {
116583c41143SJohn Baldwin 	struct resource *res;
116683c41143SJohn Baldwin 
116783c41143SJohn Baldwin 	if (!pcib_is_window_open(w))
116883c41143SJohn Baldwin 		return (NULL);
116983c41143SJohn Baldwin 
117083c41143SJohn Baldwin 	res = rman_reserve_resource(&w->rman, start, end, count,
117183c41143SJohn Baldwin 	    flags & ~RF_ACTIVE, child);
117283c41143SJohn Baldwin 	if (res == NULL)
117383c41143SJohn Baldwin 		return (NULL);
117483c41143SJohn Baldwin 
117583c41143SJohn Baldwin 	if (bootverbose)
117683c41143SJohn Baldwin 		device_printf(sc->dev,
117783c41143SJohn Baldwin 		    "allocated %s range (%#lx-%#lx) for rid %x of %s\n",
117883c41143SJohn Baldwin 		    w->name, rman_get_start(res), rman_get_end(res), *rid,
117983c41143SJohn Baldwin 		    pcib_child_name(child));
118083c41143SJohn Baldwin 	rman_set_rid(res, *rid);
118183c41143SJohn Baldwin 
118283c41143SJohn Baldwin 	/*
118383c41143SJohn Baldwin 	 * If the resource should be active, pass that request up the
118483c41143SJohn Baldwin 	 * tree.  This assumes the parent drivers can handle
118583c41143SJohn Baldwin 	 * activating sub-allocated resources.
118683c41143SJohn Baldwin 	 */
118783c41143SJohn Baldwin 	if (flags & RF_ACTIVE) {
118883c41143SJohn Baldwin 		if (bus_activate_resource(child, type, *rid, res) != 0) {
118983c41143SJohn Baldwin 			rman_release_resource(res);
119083c41143SJohn Baldwin 			return (NULL);
119183c41143SJohn Baldwin 		}
119283c41143SJohn Baldwin 	}
119383c41143SJohn Baldwin 
119483c41143SJohn Baldwin 	return (res);
119583c41143SJohn Baldwin }
119683c41143SJohn Baldwin 
1197c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */
1198c825d4dcSJohn Baldwin static int
1199c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type,
12002dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1201c825d4dcSJohn Baldwin {
1202c825d4dcSJohn Baldwin 	struct resource *res;
12032dd1bdf1SJustin Hibbits 	rman_res_t base, limit, wmask;
1204c825d4dcSJohn Baldwin 	int rid;
1205c825d4dcSJohn Baldwin 
1206c825d4dcSJohn Baldwin 	/*
1207c825d4dcSJohn Baldwin 	 * If this is an I/O window on a bridge with ISA enable set
1208c825d4dcSJohn Baldwin 	 * and the start address is below 64k, then try to allocate an
1209c825d4dcSJohn Baldwin 	 * initial window of 0x1000 bytes long starting at address
1210c825d4dcSJohn Baldwin 	 * 0xf000 and walking down.  Note that if the original request
1211c825d4dcSJohn Baldwin 	 * was larger than the non-aliased range size of 0x100 our
1212c825d4dcSJohn Baldwin 	 * caller would have raised the start address up to 64k
1213c825d4dcSJohn Baldwin 	 * already.
1214c825d4dcSJohn Baldwin 	 */
1215c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1216c825d4dcSJohn Baldwin 	    start < 65536) {
1217c825d4dcSJohn Baldwin 		for (base = 0xf000; (long)base >= 0; base -= 0x1000) {
1218c825d4dcSJohn Baldwin 			limit = base + 0xfff;
1219c825d4dcSJohn Baldwin 
1220c825d4dcSJohn Baldwin 			/*
1221c825d4dcSJohn Baldwin 			 * Skip ranges that wouldn't work for the
1222c825d4dcSJohn Baldwin 			 * original request.  Note that the actual
1223c825d4dcSJohn Baldwin 			 * window that overlaps are the non-alias
1224c825d4dcSJohn Baldwin 			 * ranges within [base, limit], so this isn't
1225c825d4dcSJohn Baldwin 			 * quite a simple comparison.
1226c825d4dcSJohn Baldwin 			 */
1227c825d4dcSJohn Baldwin 			if (start + count > limit - 0x400)
1228c825d4dcSJohn Baldwin 				continue;
1229c825d4dcSJohn Baldwin 			if (base == 0) {
1230c825d4dcSJohn Baldwin 				/*
1231c825d4dcSJohn Baldwin 				 * The first open region for the window at
1232c825d4dcSJohn Baldwin 				 * 0 is 0x400-0x4ff.
1233c825d4dcSJohn Baldwin 				 */
1234c825d4dcSJohn Baldwin 				if (end - count + 1 < 0x400)
1235c825d4dcSJohn Baldwin 					continue;
1236c825d4dcSJohn Baldwin 			} else {
1237c825d4dcSJohn Baldwin 				if (end - count + 1 < base)
1238c825d4dcSJohn Baldwin 					continue;
1239c825d4dcSJohn Baldwin 			}
1240c825d4dcSJohn Baldwin 
1241c825d4dcSJohn Baldwin 			if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) {
1242c825d4dcSJohn Baldwin 				w->base = base;
1243c825d4dcSJohn Baldwin 				w->limit = limit;
1244c825d4dcSJohn Baldwin 				return (0);
1245c825d4dcSJohn Baldwin 			}
1246c825d4dcSJohn Baldwin 		}
1247c825d4dcSJohn Baldwin 		return (ENOSPC);
1248c825d4dcSJohn Baldwin 	}
1249c825d4dcSJohn Baldwin 
125089977ce2SJustin Hibbits 	wmask = ((rman_res_t)1 << w->step) - 1;
1251c825d4dcSJohn Baldwin 	if (RF_ALIGNMENT(flags) < w->step) {
1252c825d4dcSJohn Baldwin 		flags &= ~RF_ALIGNMENT_MASK;
1253c825d4dcSJohn Baldwin 		flags |= RF_ALIGNMENT_LOG2(w->step);
1254c825d4dcSJohn Baldwin 	}
1255c825d4dcSJohn Baldwin 	start &= ~wmask;
1256c825d4dcSJohn Baldwin 	end |= wmask;
125789977ce2SJustin Hibbits 	count = roundup2(count, (rman_res_t)1 << w->step);
1258c825d4dcSJohn Baldwin 	rid = w->reg;
1259c825d4dcSJohn Baldwin 	res = bus_alloc_resource(sc->dev, type, &rid, start, end, count,
1260c825d4dcSJohn Baldwin 	    flags & ~RF_ACTIVE);
1261c825d4dcSJohn Baldwin 	if (res == NULL)
1262c825d4dcSJohn Baldwin 		return (ENOSPC);
1263c825d4dcSJohn Baldwin 	pcib_add_window_resources(w, &res, 1);
1264c825d4dcSJohn Baldwin 	pcib_activate_window(sc, type);
1265c825d4dcSJohn Baldwin 	w->base = rman_get_start(res);
1266c825d4dcSJohn Baldwin 	w->limit = rman_get_end(res);
1267c825d4dcSJohn Baldwin 	return (0);
1268c825d4dcSJohn Baldwin }
1269c825d4dcSJohn Baldwin 
1270c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */
1271c825d4dcSJohn Baldwin static int
1272c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type,
12732dd1bdf1SJustin Hibbits     rman_res_t base, rman_res_t limit)
1274c825d4dcSJohn Baldwin {
1275c825d4dcSJohn Baldwin 	struct resource *res;
1276c825d4dcSJohn Baldwin 	int error, i, force_64k_base;
1277c825d4dcSJohn Baldwin 
1278c825d4dcSJohn Baldwin 	KASSERT(base <= w->base && limit >= w->limit,
1279c825d4dcSJohn Baldwin 	    ("attempting to shrink window"));
1280c825d4dcSJohn Baldwin 
1281c825d4dcSJohn Baldwin 	/*
1282c825d4dcSJohn Baldwin 	 * XXX: pcib_grow_window() doesn't try to do this anyway and
1283c825d4dcSJohn Baldwin 	 * the error handling for all the edge cases would be tedious.
1284c825d4dcSJohn Baldwin 	 */
1285c825d4dcSJohn Baldwin 	KASSERT(limit == w->limit || base == w->base,
1286c825d4dcSJohn Baldwin 	    ("attempting to grow both ends of a window"));
1287c825d4dcSJohn Baldwin 
1288c825d4dcSJohn Baldwin 	/*
1289c825d4dcSJohn Baldwin 	 * Yet more special handling for requests to expand an I/O
1290c825d4dcSJohn Baldwin 	 * window behind an ISA-enabled bridge.  Since I/O windows
1291c825d4dcSJohn Baldwin 	 * have to grow in 0x1000 increments and the end of the 0xffff
1292c825d4dcSJohn Baldwin 	 * range is an alias, growing a window below 64k will always
1293c825d4dcSJohn Baldwin 	 * result in allocating new resources and never adjusting an
1294c825d4dcSJohn Baldwin 	 * existing resource.
1295c825d4dcSJohn Baldwin 	 */
1296c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1297c825d4dcSJohn Baldwin 	    (limit <= 65535 || (base <= 65535 && base != w->base))) {
1298c825d4dcSJohn Baldwin 		KASSERT(limit == w->limit || limit <= 65535,
1299c825d4dcSJohn Baldwin 		    ("attempting to grow both ends across 64k ISA alias"));
1300c825d4dcSJohn Baldwin 
1301c825d4dcSJohn Baldwin 		if (base != w->base)
1302c825d4dcSJohn Baldwin 			error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1);
1303c825d4dcSJohn Baldwin 		else
1304c825d4dcSJohn Baldwin 			error = pcib_alloc_nonisa_ranges(sc, w->limit + 1,
1305c825d4dcSJohn Baldwin 			    limit);
1306c825d4dcSJohn Baldwin 		if (error == 0) {
1307c825d4dcSJohn Baldwin 			w->base = base;
1308c825d4dcSJohn Baldwin 			w->limit = limit;
1309c825d4dcSJohn Baldwin 		}
1310c825d4dcSJohn Baldwin 		return (error);
1311c825d4dcSJohn Baldwin 	}
1312c825d4dcSJohn Baldwin 
1313c825d4dcSJohn Baldwin 	/*
1314c825d4dcSJohn Baldwin 	 * Find the existing resource to adjust.  Usually there is only one,
1315c825d4dcSJohn Baldwin 	 * but for an ISA-enabled bridge we might be growing the I/O window
1316c825d4dcSJohn Baldwin 	 * above 64k and need to find the existing resource that maps all
1317c825d4dcSJohn Baldwin 	 * of the area above 64k.
1318c825d4dcSJohn Baldwin 	 */
1319c825d4dcSJohn Baldwin 	for (i = 0; i < w->count; i++) {
1320c825d4dcSJohn Baldwin 		if (rman_get_end(w->res[i]) == w->limit)
1321c825d4dcSJohn Baldwin 			break;
1322c825d4dcSJohn Baldwin 	}
1323c825d4dcSJohn Baldwin 	KASSERT(i != w->count, ("did not find existing resource"));
1324c825d4dcSJohn Baldwin 	res = w->res[i];
1325c825d4dcSJohn Baldwin 
1326c825d4dcSJohn Baldwin 	/*
1327c825d4dcSJohn Baldwin 	 * Usually the resource we found should match the window's
1328c825d4dcSJohn Baldwin 	 * existing range.  The one exception is the ISA-enabled case
1329c825d4dcSJohn Baldwin 	 * mentioned above in which case the resource should start at
1330c825d4dcSJohn Baldwin 	 * 64k.
1331c825d4dcSJohn Baldwin 	 */
1332c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1333c825d4dcSJohn Baldwin 	    w->base <= 65535) {
1334c825d4dcSJohn Baldwin 		KASSERT(rman_get_start(res) == 65536,
1335c825d4dcSJohn Baldwin 		    ("existing resource mismatch"));
1336c825d4dcSJohn Baldwin 		force_64k_base = 1;
1337c825d4dcSJohn Baldwin 	} else {
1338c825d4dcSJohn Baldwin 		KASSERT(w->base == rman_get_start(res),
1339c825d4dcSJohn Baldwin 		    ("existing resource mismatch"));
1340c825d4dcSJohn Baldwin 		force_64k_base = 0;
1341c825d4dcSJohn Baldwin 	}
1342c825d4dcSJohn Baldwin 
1343c825d4dcSJohn Baldwin 	error = bus_adjust_resource(sc->dev, type, res, force_64k_base ?
1344c825d4dcSJohn Baldwin 	    rman_get_start(res) : base, limit);
1345c825d4dcSJohn Baldwin 	if (error)
1346c825d4dcSJohn Baldwin 		return (error);
1347c825d4dcSJohn Baldwin 
1348c825d4dcSJohn Baldwin 	/* Add the newly allocated region to the resource manager. */
1349c825d4dcSJohn Baldwin 	if (w->base != base) {
1350c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, base, w->base - 1);
1351c825d4dcSJohn Baldwin 		w->base = base;
1352c825d4dcSJohn Baldwin 	} else {
1353c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, w->limit + 1, limit);
1354c825d4dcSJohn Baldwin 		w->limit = limit;
1355c825d4dcSJohn Baldwin 	}
1356c825d4dcSJohn Baldwin 	if (error) {
1357c825d4dcSJohn Baldwin 		if (bootverbose)
1358c825d4dcSJohn Baldwin 			device_printf(sc->dev,
1359c825d4dcSJohn Baldwin 			    "failed to expand %s resource manager\n", w->name);
1360c825d4dcSJohn Baldwin 		(void)bus_adjust_resource(sc->dev, type, res, force_64k_base ?
1361c825d4dcSJohn Baldwin 		    rman_get_start(res) : w->base, w->limit);
1362c825d4dcSJohn Baldwin 	}
1363c825d4dcSJohn Baldwin 	return (error);
1364c825d4dcSJohn Baldwin }
1365c825d4dcSJohn Baldwin 
136683c41143SJohn Baldwin /*
136783c41143SJohn Baldwin  * Attempt to grow a window to make room for a given resource request.
136883c41143SJohn Baldwin  */
136983c41143SJohn Baldwin static int
137083c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type,
13712dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
137283c41143SJohn Baldwin {
13732dd1bdf1SJustin Hibbits 	rman_res_t align, start_free, end_free, front, back, wmask;
1374c825d4dcSJohn Baldwin 	int error;
137583c41143SJohn Baldwin 
137683c41143SJohn Baldwin 	/*
137783c41143SJohn Baldwin 	 * Clamp the desired resource range to the maximum address
137883c41143SJohn Baldwin 	 * this window supports.  Reject impossible requests.
1379c825d4dcSJohn Baldwin 	 *
1380c825d4dcSJohn Baldwin 	 * For I/O port requests behind a bridge with the ISA enable
1381c825d4dcSJohn Baldwin 	 * bit set, force large allocations to start above 64k.
138283c41143SJohn Baldwin 	 */
138383c41143SJohn Baldwin 	if (!w->valid)
138483c41143SJohn Baldwin 		return (EINVAL);
1385c825d4dcSJohn Baldwin 	if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 &&
1386c825d4dcSJohn Baldwin 	    start < 65536)
1387c825d4dcSJohn Baldwin 		start = 65536;
138883c41143SJohn Baldwin 	if (end > w->rman.rm_end)
138983c41143SJohn Baldwin 		end = w->rman.rm_end;
139083c41143SJohn Baldwin 	if (start + count - 1 > end || start + count < start)
139183c41143SJohn Baldwin 		return (EINVAL);
139289977ce2SJustin Hibbits 	wmask = ((rman_res_t)1 << w->step) - 1;
139383c41143SJohn Baldwin 
139483c41143SJohn Baldwin 	/*
139583c41143SJohn Baldwin 	 * If there is no resource at all, just try to allocate enough
139683c41143SJohn Baldwin 	 * aligned space for this resource.
139783c41143SJohn Baldwin 	 */
139883c41143SJohn Baldwin 	if (w->res == NULL) {
1399c825d4dcSJohn Baldwin 		error = pcib_alloc_new_window(sc, w, type, start, end, count,
1400c825d4dcSJohn Baldwin 		    flags);
1401c825d4dcSJohn Baldwin 		if (error) {
140283c41143SJohn Baldwin 			if (bootverbose)
140383c41143SJohn Baldwin 				device_printf(sc->dev,
140483c41143SJohn Baldwin 		    "failed to allocate initial %s window (%#lx-%#lx,%#lx)\n",
140583c41143SJohn Baldwin 				    w->name, start, end, count);
140683c41143SJohn Baldwin 			return (error);
140783c41143SJohn Baldwin 		}
1408c825d4dcSJohn Baldwin 		if (bootverbose)
1409c825d4dcSJohn Baldwin 			device_printf(sc->dev,
1410c825d4dcSJohn Baldwin 			    "allocated initial %s window of %#jx-%#jx\n",
1411c825d4dcSJohn Baldwin 			    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
141283c41143SJohn Baldwin 		goto updatewin;
141383c41143SJohn Baldwin 	}
141483c41143SJohn Baldwin 
141583c41143SJohn Baldwin 	/*
141683c41143SJohn Baldwin 	 * See if growing the window would help.  Compute the minimum
141783c41143SJohn Baldwin 	 * amount of address space needed on both the front and back
141883c41143SJohn Baldwin 	 * ends of the existing window to satisfy the allocation.
141983c41143SJohn Baldwin 	 *
142083c41143SJohn Baldwin 	 * For each end, build a candidate region adjusting for the
142183c41143SJohn Baldwin 	 * required alignment, etc.  If there is a free region at the
142283c41143SJohn Baldwin 	 * edge of the window, grow from the inner edge of the free
142383c41143SJohn Baldwin 	 * region.  Otherwise grow from the window boundary.
142483c41143SJohn Baldwin 	 *
1425c825d4dcSJohn Baldwin 	 * Growing an I/O window below 64k for a bridge with the ISA
1426c825d4dcSJohn Baldwin 	 * enable bit doesn't require any special magic as the step
1427c825d4dcSJohn Baldwin 	 * size of an I/O window (1k) always includes multiple
1428c825d4dcSJohn Baldwin 	 * non-alias ranges when it is grown in either direction.
1429c825d4dcSJohn Baldwin 	 *
143083c41143SJohn Baldwin 	 * XXX: Special case: if w->res is completely empty and the
143183c41143SJohn Baldwin 	 * request size is larger than w->res, we should find the
143283c41143SJohn Baldwin 	 * optimal aligned buffer containing w->res and allocate that.
143383c41143SJohn Baldwin 	 */
143483c41143SJohn Baldwin 	if (bootverbose)
143583c41143SJohn Baldwin 		device_printf(sc->dev,
143683c41143SJohn Baldwin 		    "attempting to grow %s window for (%#lx-%#lx,%#lx)\n",
143783c41143SJohn Baldwin 		    w->name, start, end, count);
143889977ce2SJustin Hibbits 	align = (rman_res_t)1 << RF_ALIGNMENT(flags);
1439c825d4dcSJohn Baldwin 	if (start < w->base) {
144083c41143SJohn Baldwin 		if (rman_first_free_region(&w->rman, &start_free, &end_free) !=
1441c825d4dcSJohn Baldwin 		    0 || start_free != w->base)
1442c825d4dcSJohn Baldwin 			end_free = w->base;
144383c41143SJohn Baldwin 		if (end_free > end)
1444ddac8cc9SJohn Baldwin 			end_free = end + 1;
144583c41143SJohn Baldwin 
144683c41143SJohn Baldwin 		/* Move end_free down until it is properly aligned. */
144783c41143SJohn Baldwin 		end_free &= ~(align - 1);
1448a49dcb46SJohn Baldwin 		end_free--;
1449a49dcb46SJohn Baldwin 		front = end_free - (count - 1);
145083c41143SJohn Baldwin 
145183c41143SJohn Baldwin 		/*
145283c41143SJohn Baldwin 		 * The resource would now be allocated at (front,
145383c41143SJohn Baldwin 		 * end_free).  Ensure that fits in the (start, end)
145483c41143SJohn Baldwin 		 * bounds.  end_free is checked above.  If 'front' is
145583c41143SJohn Baldwin 		 * ok, ensure it is properly aligned for this window.
145683c41143SJohn Baldwin 		 * Also check for underflow.
145783c41143SJohn Baldwin 		 */
145883c41143SJohn Baldwin 		if (front >= start && front <= end_free) {
145983c41143SJohn Baldwin 			if (bootverbose)
146083c41143SJohn Baldwin 				printf("\tfront candidate range: %#lx-%#lx\n",
146183c41143SJohn Baldwin 				    front, end_free);
1462a7b5acacSJohn Baldwin 			front &= ~wmask;
1463c825d4dcSJohn Baldwin 			front = w->base - front;
146483c41143SJohn Baldwin 		} else
146583c41143SJohn Baldwin 			front = 0;
146683c41143SJohn Baldwin 	} else
146783c41143SJohn Baldwin 		front = 0;
1468c825d4dcSJohn Baldwin 	if (end > w->limit) {
146983c41143SJohn Baldwin 		if (rman_last_free_region(&w->rman, &start_free, &end_free) !=
1470c825d4dcSJohn Baldwin 		    0 || end_free != w->limit)
1471c825d4dcSJohn Baldwin 			start_free = w->limit + 1;
147283c41143SJohn Baldwin 		if (start_free < start)
147383c41143SJohn Baldwin 			start_free = start;
147483c41143SJohn Baldwin 
147583c41143SJohn Baldwin 		/* Move start_free up until it is properly aligned. */
147683c41143SJohn Baldwin 		start_free = roundup2(start_free, align);
1477a49dcb46SJohn Baldwin 		back = start_free + count - 1;
147883c41143SJohn Baldwin 
147983c41143SJohn Baldwin 		/*
148083c41143SJohn Baldwin 		 * The resource would now be allocated at (start_free,
148183c41143SJohn Baldwin 		 * back).  Ensure that fits in the (start, end)
148283c41143SJohn Baldwin 		 * bounds.  start_free is checked above.  If 'back' is
148383c41143SJohn Baldwin 		 * ok, ensure it is properly aligned for this window.
148483c41143SJohn Baldwin 		 * Also check for overflow.
148583c41143SJohn Baldwin 		 */
148683c41143SJohn Baldwin 		if (back <= end && start_free <= back) {
148783c41143SJohn Baldwin 			if (bootverbose)
148883c41143SJohn Baldwin 				printf("\tback candidate range: %#lx-%#lx\n",
148983c41143SJohn Baldwin 				    start_free, back);
1490a7b5acacSJohn Baldwin 			back |= wmask;
1491c825d4dcSJohn Baldwin 			back -= w->limit;
149283c41143SJohn Baldwin 		} else
149383c41143SJohn Baldwin 			back = 0;
149483c41143SJohn Baldwin 	} else
149583c41143SJohn Baldwin 		back = 0;
149683c41143SJohn Baldwin 
149783c41143SJohn Baldwin 	/*
149883c41143SJohn Baldwin 	 * Try to allocate the smallest needed region first.
149983c41143SJohn Baldwin 	 * If that fails, fall back to the other region.
150083c41143SJohn Baldwin 	 */
150183c41143SJohn Baldwin 	error = ENOSPC;
150283c41143SJohn Baldwin 	while (front != 0 || back != 0) {
150383c41143SJohn Baldwin 		if (front != 0 && (front <= back || back == 0)) {
1504c825d4dcSJohn Baldwin 			error = pcib_expand_window(sc, w, type, w->base - front,
1505c825d4dcSJohn Baldwin 			    w->limit);
150683c41143SJohn Baldwin 			if (error == 0)
150783c41143SJohn Baldwin 				break;
150883c41143SJohn Baldwin 			front = 0;
150983c41143SJohn Baldwin 		} else {
1510c825d4dcSJohn Baldwin 			error = pcib_expand_window(sc, w, type, w->base,
1511c825d4dcSJohn Baldwin 			    w->limit + back);
151283c41143SJohn Baldwin 			if (error == 0)
151383c41143SJohn Baldwin 				break;
151483c41143SJohn Baldwin 			back = 0;
151583c41143SJohn Baldwin 		}
151683c41143SJohn Baldwin 	}
151783c41143SJohn Baldwin 
151883c41143SJohn Baldwin 	if (error)
151983c41143SJohn Baldwin 		return (error);
152083c41143SJohn Baldwin 	if (bootverbose)
1521c825d4dcSJohn Baldwin 		device_printf(sc->dev, "grew %s window to %#jx-%#jx\n",
1522c825d4dcSJohn Baldwin 		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
152383c41143SJohn Baldwin 
152483c41143SJohn Baldwin updatewin:
1525c825d4dcSJohn Baldwin 	/* Write the new window. */
1526a7b5acacSJohn Baldwin 	KASSERT((w->base & wmask) == 0, ("start address is not aligned"));
1527a7b5acacSJohn Baldwin 	KASSERT((w->limit & wmask) == wmask, ("end address is not aligned"));
152883c41143SJohn Baldwin 	pcib_write_windows(sc, w->mask);
152983c41143SJohn Baldwin 	return (0);
153083c41143SJohn Baldwin }
153183c41143SJohn Baldwin 
153283c41143SJohn Baldwin /*
153383c41143SJohn Baldwin  * We have to trap resource allocation requests and ensure that the bridge
153483c41143SJohn Baldwin  * is set up to, or capable of handling them.
153583c41143SJohn Baldwin  */
153683c41143SJohn Baldwin struct resource *
153783c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
15382dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
153983c41143SJohn Baldwin {
154083c41143SJohn Baldwin 	struct pcib_softc *sc;
154183c41143SJohn Baldwin 	struct resource *r;
154283c41143SJohn Baldwin 
154383c41143SJohn Baldwin 	sc = device_get_softc(dev);
154483c41143SJohn Baldwin 
154583c41143SJohn Baldwin 	/*
154683c41143SJohn Baldwin 	 * VGA resources are decoded iff the VGA enable bit is set in
154783c41143SJohn Baldwin 	 * the bridge control register.  VGA resources do not fall into
154883c41143SJohn Baldwin 	 * the resource windows and are passed up to the parent.
154983c41143SJohn Baldwin 	 */
155083c41143SJohn Baldwin 	if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) ||
155183c41143SJohn Baldwin 	    (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) {
155283c41143SJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE)
155383c41143SJohn Baldwin 			return (bus_generic_alloc_resource(dev, child, type,
155483c41143SJohn Baldwin 			    rid, start, end, count, flags));
155583c41143SJohn Baldwin 		else
155683c41143SJohn Baldwin 			return (NULL);
155783c41143SJohn Baldwin 	}
155883c41143SJohn Baldwin 
155983c41143SJohn Baldwin 	switch (type) {
15604edef187SJohn Baldwin #ifdef PCI_RES_BUS
15614edef187SJohn Baldwin 	case PCI_RES_BUS:
15624edef187SJohn Baldwin 		return (pcib_alloc_subbus(&sc->bus, child, rid, start, end,
15634edef187SJohn Baldwin 		    count, flags));
15644edef187SJohn Baldwin #endif
156583c41143SJohn Baldwin 	case SYS_RES_IOPORT:
1566c825d4dcSJohn Baldwin 		if (pcib_is_isa_range(sc, start, end, count))
1567c825d4dcSJohn Baldwin 			return (NULL);
156883c41143SJohn Baldwin 		r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start,
156983c41143SJohn Baldwin 		    end, count, flags);
1570a6c82265SMarius Strobl 		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
157183c41143SJohn Baldwin 			break;
157283c41143SJohn Baldwin 		if (pcib_grow_window(sc, &sc->io, type, start, end, count,
157383c41143SJohn Baldwin 		    flags) == 0)
157483c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->io, child, type,
157583c41143SJohn Baldwin 			    rid, start, end, count, flags);
157683c41143SJohn Baldwin 		break;
157783c41143SJohn Baldwin 	case SYS_RES_MEMORY:
157883c41143SJohn Baldwin 		/*
157983c41143SJohn Baldwin 		 * For prefetchable resources, prefer the prefetchable
158083c41143SJohn Baldwin 		 * memory window, but fall back to the regular memory
158183c41143SJohn Baldwin 		 * window if that fails.  Try both windows before
158283c41143SJohn Baldwin 		 * attempting to grow a window in case the firmware
158383c41143SJohn Baldwin 		 * has used a range in the regular memory window to
158483c41143SJohn Baldwin 		 * map a prefetchable BAR.
158583c41143SJohn Baldwin 		 */
158683c41143SJohn Baldwin 		if (flags & RF_PREFETCHABLE) {
158783c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->pmem, child, type,
158883c41143SJohn Baldwin 			    rid, start, end, count, flags);
158983c41143SJohn Baldwin 			if (r != NULL)
159083c41143SJohn Baldwin 				break;
159183c41143SJohn Baldwin 		}
159283c41143SJohn Baldwin 		r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid,
159383c41143SJohn Baldwin 		    start, end, count, flags);
1594a6c82265SMarius Strobl 		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
159583c41143SJohn Baldwin 			break;
159683c41143SJohn Baldwin 		if (flags & RF_PREFETCHABLE) {
159783c41143SJohn Baldwin 			if (pcib_grow_window(sc, &sc->pmem, type, start, end,
159883c41143SJohn Baldwin 			    count, flags) == 0) {
159983c41143SJohn Baldwin 				r = pcib_suballoc_resource(sc, &sc->pmem, child,
160083c41143SJohn Baldwin 				    type, rid, start, end, count, flags);
160183c41143SJohn Baldwin 				if (r != NULL)
160283c41143SJohn Baldwin 					break;
160383c41143SJohn Baldwin 			}
160483c41143SJohn Baldwin 		}
160583c41143SJohn Baldwin 		if (pcib_grow_window(sc, &sc->mem, type, start, end, count,
160683c41143SJohn Baldwin 		    flags & ~RF_PREFETCHABLE) == 0)
160783c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->mem, child, type,
160883c41143SJohn Baldwin 			    rid, start, end, count, flags);
160983c41143SJohn Baldwin 		break;
161083c41143SJohn Baldwin 	default:
161183c41143SJohn Baldwin 		return (bus_generic_alloc_resource(dev, child, type, rid,
161283c41143SJohn Baldwin 		    start, end, count, flags));
161383c41143SJohn Baldwin 	}
161483c41143SJohn Baldwin 
161583c41143SJohn Baldwin 	/*
161683c41143SJohn Baldwin 	 * If attempts to suballocate from the window fail but this is a
161783c41143SJohn Baldwin 	 * subtractive bridge, pass the request up the tree.
161883c41143SJohn Baldwin 	 */
161983c41143SJohn Baldwin 	if (sc->flags & PCIB_SUBTRACTIVE && r == NULL)
162083c41143SJohn Baldwin 		return (bus_generic_alloc_resource(dev, child, type, rid,
162183c41143SJohn Baldwin 		    start, end, count, flags));
162283c41143SJohn Baldwin 	return (r);
162383c41143SJohn Baldwin }
162483c41143SJohn Baldwin 
162583c41143SJohn Baldwin int
162683c41143SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
16272dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end)
162883c41143SJohn Baldwin {
162983c41143SJohn Baldwin 	struct pcib_softc *sc;
163083c41143SJohn Baldwin 
163183c41143SJohn Baldwin 	sc = device_get_softc(bus);
163283c41143SJohn Baldwin 	if (pcib_is_resource_managed(sc, type, r))
163383c41143SJohn Baldwin 		return (rman_adjust_resource(r, start, end));
163483c41143SJohn Baldwin 	return (bus_generic_adjust_resource(bus, child, type, r, start, end));
163583c41143SJohn Baldwin }
163683c41143SJohn Baldwin 
163783c41143SJohn Baldwin int
163883c41143SJohn Baldwin pcib_release_resource(device_t dev, device_t child, int type, int rid,
163983c41143SJohn Baldwin     struct resource *r)
164083c41143SJohn Baldwin {
164183c41143SJohn Baldwin 	struct pcib_softc *sc;
164283c41143SJohn Baldwin 	int error;
164383c41143SJohn Baldwin 
164483c41143SJohn Baldwin 	sc = device_get_softc(dev);
164583c41143SJohn Baldwin 	if (pcib_is_resource_managed(sc, type, r)) {
164683c41143SJohn Baldwin 		if (rman_get_flags(r) & RF_ACTIVE) {
164783c41143SJohn Baldwin 			error = bus_deactivate_resource(child, type, rid, r);
164883c41143SJohn Baldwin 			if (error)
164983c41143SJohn Baldwin 				return (error);
165083c41143SJohn Baldwin 		}
165183c41143SJohn Baldwin 		return (rman_release_resource(r));
165283c41143SJohn Baldwin 	}
165383c41143SJohn Baldwin 	return (bus_generic_release_resource(dev, child, type, rid, r));
165483c41143SJohn Baldwin }
165583c41143SJohn Baldwin #else
1656bb0d0a8eSMike Smith /*
1657bb0d0a8eSMike Smith  * We have to trap resource allocation requests and ensure that the bridge
1658bb0d0a8eSMike Smith  * is set up to, or capable of handling them.
1659bb0d0a8eSMike Smith  */
16606f0d5884SJohn Baldwin struct resource *
1661bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
16622dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1663bb0d0a8eSMike Smith {
1664bb0d0a8eSMike Smith 	struct pcib_softc	*sc = device_get_softc(dev);
166526043836SJohn Baldwin 	const char *name, *suffix;
1666a8b354a8SWarner Losh 	int ok;
1667bb0d0a8eSMike Smith 
1668bb0d0a8eSMike Smith 	/*
1669bb0d0a8eSMike Smith 	 * Fail the allocation for this range if it's not supported.
1670bb0d0a8eSMike Smith 	 */
167126043836SJohn Baldwin 	name = device_get_nameunit(child);
167226043836SJohn Baldwin 	if (name == NULL) {
167326043836SJohn Baldwin 		name = "";
167426043836SJohn Baldwin 		suffix = "";
167526043836SJohn Baldwin 	} else
167626043836SJohn Baldwin 		suffix = " ";
1677bb0d0a8eSMike Smith 	switch (type) {
1678bb0d0a8eSMike Smith 	case SYS_RES_IOPORT:
1679a8b354a8SWarner Losh 		ok = 0;
1680e4b59fc5SWarner Losh 		if (!pcib_is_io_open(sc))
1681e4b59fc5SWarner Losh 			break;
1682a8b354a8SWarner Losh 		ok = (start >= sc->iobase && end <= sc->iolimit);
1683d98d9b12SMarcel Moolenaar 
1684d98d9b12SMarcel Moolenaar 		/*
1685d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA I/O addresses when the
1686d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
1687d98d9b12SMarcel Moolenaar 		 */
1688d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_ioport_range(start, end))
1689d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1690d98d9b12SMarcel Moolenaar 
1691e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1692a8b354a8SWarner Losh 			if (!ok) {
169312b8c86eSWarner Losh 				if (start < sc->iobase)
169412b8c86eSWarner Losh 					start = sc->iobase;
169512b8c86eSWarner Losh 				if (end > sc->iolimit)
169612b8c86eSWarner Losh 					end = sc->iolimit;
16972daa7a07SWarner Losh 				if (start < end)
16982daa7a07SWarner Losh 					ok = 1;
1699a8b354a8SWarner Losh 			}
17001c54ff33SMatthew N. Dodd 		} else {
1701e4b59fc5SWarner Losh 			ok = 1;
17029dffe835SWarner Losh #if 0
1703795dceffSWarner Losh 			/*
1704795dceffSWarner Losh 			 * If we overlap with the subtractive range, then
1705795dceffSWarner Losh 			 * pick the upper range to use.
1706795dceffSWarner Losh 			 */
1707795dceffSWarner Losh 			if (start < sc->iolimit && end > sc->iobase)
1708795dceffSWarner Losh 				start = sc->iolimit + 1;
17099dffe835SWarner Losh #endif
171012b8c86eSWarner Losh 		}
1711a8b354a8SWarner Losh 		if (end < start) {
17122daa7a07SWarner Losh 			device_printf(dev, "ioport: end (%lx) < start (%lx)\n",
17132daa7a07SWarner Losh 			    end, start);
1714a8b354a8SWarner Losh 			start = 0;
1715a8b354a8SWarner Losh 			end = 0;
1716a8b354a8SWarner Losh 			ok = 0;
1717a8b354a8SWarner Losh 		}
1718a8b354a8SWarner Losh 		if (!ok) {
171926043836SJohn Baldwin 			device_printf(dev, "%s%srequested unsupported I/O "
1720a8b354a8SWarner Losh 			    "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
172126043836SJohn Baldwin 			    name, suffix, start, end, sc->iobase, sc->iolimit);
1722bb0d0a8eSMike Smith 			return (NULL);
1723bb0d0a8eSMike Smith 		}
17244fa59183SMike Smith 		if (bootverbose)
17252daa7a07SWarner Losh 			device_printf(dev,
172626043836SJohn Baldwin 			    "%s%srequested I/O range 0x%lx-0x%lx: in range\n",
172726043836SJohn Baldwin 			    name, suffix, start, end);
1728bb0d0a8eSMike Smith 		break;
1729bb0d0a8eSMike Smith 
1730bb0d0a8eSMike Smith 	case SYS_RES_MEMORY:
1731a8b354a8SWarner Losh 		ok = 0;
1732a8b354a8SWarner Losh 		if (pcib_is_nonprefetch_open(sc))
1733a8b354a8SWarner Losh 			ok = ok || (start >= sc->membase && end <= sc->memlimit);
1734a8b354a8SWarner Losh 		if (pcib_is_prefetch_open(sc))
1735a8b354a8SWarner Losh 			ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
1736d98d9b12SMarcel Moolenaar 
1737d98d9b12SMarcel Moolenaar 		/*
1738d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA memory addresses when the
1739d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
1740d98d9b12SMarcel Moolenaar 		 */
1741d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_memory_range(start, end))
1742d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1743d98d9b12SMarcel Moolenaar 
1744e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1745a8b354a8SWarner Losh 			if (!ok) {
1746a8b354a8SWarner Losh 				ok = 1;
1747a8b354a8SWarner Losh 				if (flags & RF_PREFETCHABLE) {
1748a8b354a8SWarner Losh 					if (pcib_is_prefetch_open(sc)) {
1749a8b354a8SWarner Losh 						if (start < sc->pmembase)
1750a8b354a8SWarner Losh 							start = sc->pmembase;
1751a8b354a8SWarner Losh 						if (end > sc->pmemlimit)
1752a8b354a8SWarner Losh 							end = sc->pmemlimit;
1753a8b354a8SWarner Losh 					} else {
1754a8b354a8SWarner Losh 						ok = 0;
1755a8b354a8SWarner Losh 					}
1756a8b354a8SWarner Losh 				} else {	/* non-prefetchable */
1757a8b354a8SWarner Losh 					if (pcib_is_nonprefetch_open(sc)) {
1758a8b354a8SWarner Losh 						if (start < sc->membase)
175912b8c86eSWarner Losh 							start = sc->membase;
176012b8c86eSWarner Losh 						if (end > sc->memlimit)
176112b8c86eSWarner Losh 							end = sc->memlimit;
17621c54ff33SMatthew N. Dodd 					} else {
1763a8b354a8SWarner Losh 						ok = 0;
1764a8b354a8SWarner Losh 					}
1765a8b354a8SWarner Losh 				}
1766a8b354a8SWarner Losh 			}
1767a8b354a8SWarner Losh 		} else if (!ok) {
1768e4b59fc5SWarner Losh 			ok = 1;	/* subtractive bridge: always ok */
17699dffe835SWarner Losh #if 0
1770a8b354a8SWarner Losh 			if (pcib_is_nonprefetch_open(sc)) {
1771795dceffSWarner Losh 				if (start < sc->memlimit && end > sc->membase)
1772795dceffSWarner Losh 					start = sc->memlimit + 1;
1773a8b354a8SWarner Losh 			}
1774a8b354a8SWarner Losh 			if (pcib_is_prefetch_open(sc)) {
1775795dceffSWarner Losh 				if (start < sc->pmemlimit && end > sc->pmembase)
1776795dceffSWarner Losh 					start = sc->pmemlimit + 1;
17771c54ff33SMatthew N. Dodd 			}
17789dffe835SWarner Losh #endif
177912b8c86eSWarner Losh 		}
1780a8b354a8SWarner Losh 		if (end < start) {
17812daa7a07SWarner Losh 			device_printf(dev, "memory: end (%lx) < start (%lx)\n",
17822daa7a07SWarner Losh 			    end, start);
1783a8b354a8SWarner Losh 			start = 0;
1784a8b354a8SWarner Losh 			end = 0;
1785a8b354a8SWarner Losh 			ok = 0;
1786a8b354a8SWarner Losh 		}
1787a8b354a8SWarner Losh 		if (!ok && bootverbose)
178834428485SWarner Losh 			device_printf(dev,
178926043836SJohn Baldwin 			    "%s%srequested unsupported memory range %#lx-%#lx "
1790b0a2d4b8SWarner Losh 			    "(decoding %#jx-%#jx, %#jx-%#jx)\n",
179126043836SJohn Baldwin 			    name, suffix, start, end,
1792b0a2d4b8SWarner Losh 			    (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
1793b0a2d4b8SWarner Losh 			    (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
1794a8b354a8SWarner Losh 		if (!ok)
1795bb0d0a8eSMike Smith 			return (NULL);
17964fa59183SMike Smith 		if (bootverbose)
179726043836SJohn Baldwin 			device_printf(dev,"%s%srequested memory range "
17982daa7a07SWarner Losh 			    "0x%lx-0x%lx: good\n",
179926043836SJohn Baldwin 			    name, suffix, start, end);
18004fa59183SMike Smith 		break;
18014fa59183SMike Smith 
1802bb0d0a8eSMike Smith 	default:
18034fa59183SMike Smith 		break;
1804bb0d0a8eSMike Smith 	}
1805bb0d0a8eSMike Smith 	/*
1806bb0d0a8eSMike Smith 	 * Bridge is OK decoding this resource, so pass it up.
1807bb0d0a8eSMike Smith 	 */
18082daa7a07SWarner Losh 	return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
18092daa7a07SWarner Losh 	    count, flags));
1810bb0d0a8eSMike Smith }
181183c41143SJohn Baldwin #endif
1812bb0d0a8eSMike Smith 
1813bb0d0a8eSMike Smith /*
181455d3ea17SRyan Stone  * If ARI is enabled on this downstream port, translate the function number
181555d3ea17SRyan Stone  * to the non-ARI slot/function.  The downstream port will convert it back in
181655d3ea17SRyan Stone  * hardware.  If ARI is not enabled slot and func are not modified.
181755d3ea17SRyan Stone  */
181855d3ea17SRyan Stone static __inline void
181955d3ea17SRyan Stone pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func)
182055d3ea17SRyan Stone {
182155d3ea17SRyan Stone 	struct pcib_softc *sc;
182255d3ea17SRyan Stone 	int ari_func;
182355d3ea17SRyan Stone 
182455d3ea17SRyan Stone 	sc = device_get_softc(pcib);
182555d3ea17SRyan Stone 	ari_func = *func;
182655d3ea17SRyan Stone 
182755d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
182855d3ea17SRyan Stone 		KASSERT(*slot == 0,
182955d3ea17SRyan Stone 		    ("Non-zero slot number with ARI enabled!"));
183055d3ea17SRyan Stone 		*slot = PCIE_ARI_SLOT(ari_func);
183155d3ea17SRyan Stone 		*func = PCIE_ARI_FUNC(ari_func);
183255d3ea17SRyan Stone 	}
183355d3ea17SRyan Stone }
183455d3ea17SRyan Stone 
183555d3ea17SRyan Stone 
183655d3ea17SRyan Stone static void
183755d3ea17SRyan Stone pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos)
183855d3ea17SRyan Stone {
183955d3ea17SRyan Stone 	uint32_t ctl2;
184055d3ea17SRyan Stone 
184155d3ea17SRyan Stone 	ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4);
184255d3ea17SRyan Stone 	ctl2 |= PCIEM_CTL2_ARI;
184355d3ea17SRyan Stone 	pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4);
184455d3ea17SRyan Stone 
184555d3ea17SRyan Stone 	sc->flags |= PCIB_ENABLE_ARI;
184655d3ea17SRyan Stone }
184755d3ea17SRyan Stone 
184855d3ea17SRyan Stone /*
1849bb0d0a8eSMike Smith  * PCIB interface.
1850bb0d0a8eSMike Smith  */
18516f0d5884SJohn Baldwin int
1852bb0d0a8eSMike Smith pcib_maxslots(device_t dev)
1853bb0d0a8eSMike Smith {
18544fa59183SMike Smith 	return (PCI_SLOTMAX);
1855bb0d0a8eSMike Smith }
1856bb0d0a8eSMike Smith 
185755d3ea17SRyan Stone static int
185855d3ea17SRyan Stone pcib_ari_maxslots(device_t dev)
185955d3ea17SRyan Stone {
186055d3ea17SRyan Stone 	struct pcib_softc *sc;
186155d3ea17SRyan Stone 
186255d3ea17SRyan Stone 	sc = device_get_softc(dev);
186355d3ea17SRyan Stone 
186455d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI)
186555d3ea17SRyan Stone 		return (PCIE_ARI_SLOTMAX);
186655d3ea17SRyan Stone 	else
186755d3ea17SRyan Stone 		return (PCI_SLOTMAX);
186855d3ea17SRyan Stone }
186955d3ea17SRyan Stone 
187055d3ea17SRyan Stone static int
187155d3ea17SRyan Stone pcib_ari_maxfuncs(device_t dev)
187255d3ea17SRyan Stone {
187355d3ea17SRyan Stone 	struct pcib_softc *sc;
187455d3ea17SRyan Stone 
187555d3ea17SRyan Stone 	sc = device_get_softc(dev);
187655d3ea17SRyan Stone 
187755d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI)
187855d3ea17SRyan Stone 		return (PCIE_ARI_FUNCMAX);
187955d3ea17SRyan Stone 	else
188055d3ea17SRyan Stone 		return (PCI_FUNCMAX);
188155d3ea17SRyan Stone }
188255d3ea17SRyan Stone 
18832397d2d8SRyan Stone static void
18842397d2d8SRyan Stone pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot,
18852397d2d8SRyan Stone     int *func)
18862397d2d8SRyan Stone {
18872397d2d8SRyan Stone 	struct pcib_softc *sc;
18882397d2d8SRyan Stone 
18892397d2d8SRyan Stone 	sc = device_get_softc(pcib);
18902397d2d8SRyan Stone 
18912397d2d8SRyan Stone 	*bus = PCI_RID2BUS(rid);
18922397d2d8SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
18932397d2d8SRyan Stone 		*slot = PCIE_ARI_RID2SLOT(rid);
18942397d2d8SRyan Stone 		*func = PCIE_ARI_RID2FUNC(rid);
18952397d2d8SRyan Stone 	} else {
18962397d2d8SRyan Stone 		*slot = PCI_RID2SLOT(rid);
18972397d2d8SRyan Stone 		*func = PCI_RID2FUNC(rid);
18982397d2d8SRyan Stone 	}
18992397d2d8SRyan Stone }
19002397d2d8SRyan Stone 
1901bb0d0a8eSMike Smith /*
1902bb0d0a8eSMike Smith  * Since we are a child of a PCI bus, its parent must support the pcib interface.
1903bb0d0a8eSMike Smith  */
190455d3ea17SRyan Stone static uint32_t
1905795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
1906bb0d0a8eSMike Smith {
190755d3ea17SRyan Stone 
190855d3ea17SRyan Stone 	pcib_xlate_ari(dev, b, &s, &f);
190955d3ea17SRyan Stone 	return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s,
191055d3ea17SRyan Stone 	    f, reg, width));
1911bb0d0a8eSMike Smith }
1912bb0d0a8eSMike Smith 
191355d3ea17SRyan Stone static void
1914795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
1915bb0d0a8eSMike Smith {
191655d3ea17SRyan Stone 
191755d3ea17SRyan Stone 	pcib_xlate_ari(dev, b, &s, &f);
191855d3ea17SRyan Stone 	PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f,
191955d3ea17SRyan Stone 	    reg, val, width);
1920bb0d0a8eSMike Smith }
1921bb0d0a8eSMike Smith 
1922bb0d0a8eSMike Smith /*
1923bb0d0a8eSMike Smith  * Route an interrupt across a PCI bridge.
1924bb0d0a8eSMike Smith  */
19252c2d1d07SBenno Rice int
1926bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin)
1927bb0d0a8eSMike Smith {
1928bb0d0a8eSMike Smith     device_t	bus;
1929bb0d0a8eSMike Smith     int		parent_intpin;
1930bb0d0a8eSMike Smith     int		intnum;
1931bb0d0a8eSMike Smith 
1932bb0d0a8eSMike Smith     /*
1933bb0d0a8eSMike Smith      *
1934bb0d0a8eSMike Smith      * The PCI standard defines a swizzle of the child-side device/intpin to
1935bb0d0a8eSMike Smith      * the parent-side intpin as follows.
1936bb0d0a8eSMike Smith      *
1937bb0d0a8eSMike Smith      * device = device on child bus
1938bb0d0a8eSMike Smith      * child_intpin = intpin on child bus slot (0-3)
1939bb0d0a8eSMike Smith      * parent_intpin = intpin on parent bus slot (0-3)
1940bb0d0a8eSMike Smith      *
1941bb0d0a8eSMike Smith      * parent_intpin = (device + child_intpin) % 4
1942bb0d0a8eSMike Smith      */
1943cdc95e1bSBernd Walter     parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
1944bb0d0a8eSMike Smith 
1945bb0d0a8eSMike Smith     /*
1946bb0d0a8eSMike Smith      * Our parent is a PCI bus.  Its parent must export the pcib interface
1947bb0d0a8eSMike Smith      * which includes the ability to route interrupts.
1948bb0d0a8eSMike Smith      */
1949bb0d0a8eSMike Smith     bus = device_get_parent(pcib);
1950bb0d0a8eSMike Smith     intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
195139981fedSJohn Baldwin     if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
1952c6a121abSJohn Baldwin 	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
1953c6a121abSJohn Baldwin 	    pci_get_slot(dev), 'A' + pin - 1, intnum);
19548046c4b9SMike Smith     }
1955bb0d0a8eSMike Smith     return(intnum);
1956bb0d0a8eSMike Smith }
1957b173edafSJohn Baldwin 
1958e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
19599bf4c9c1SJohn Baldwin int
19609bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
19619bf4c9c1SJohn Baldwin {
1962bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
19639bf4c9c1SJohn Baldwin 	device_t bus;
19649bf4c9c1SJohn Baldwin 
196522bf1c7fSJohn Baldwin 	if (sc->flags & PCIB_DISABLE_MSI)
196622bf1c7fSJohn Baldwin 		return (ENXIO);
19679bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
19689bf4c9c1SJohn Baldwin 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
19699bf4c9c1SJohn Baldwin 	    irqs));
19709bf4c9c1SJohn Baldwin }
19719bf4c9c1SJohn Baldwin 
1972e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */
19739bf4c9c1SJohn Baldwin int
19749bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
19759bf4c9c1SJohn Baldwin {
19769bf4c9c1SJohn Baldwin 	device_t bus;
19779bf4c9c1SJohn Baldwin 
19789bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
19799bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
19809bf4c9c1SJohn Baldwin }
19819bf4c9c1SJohn Baldwin 
19829bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */
19839bf4c9c1SJohn Baldwin int
1984e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
19859bf4c9c1SJohn Baldwin {
1986bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
19879bf4c9c1SJohn Baldwin 	device_t bus;
19889bf4c9c1SJohn Baldwin 
198968e9cbd3SMarius Strobl 	if (sc->flags & PCIB_DISABLE_MSIX)
199022bf1c7fSJohn Baldwin 		return (ENXIO);
19919bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
1992e706f7f0SJohn Baldwin 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
19935fe82bcaSJohn Baldwin }
19945fe82bcaSJohn Baldwin 
19959bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */
19969bf4c9c1SJohn Baldwin int
19979bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq)
19989bf4c9c1SJohn Baldwin {
19999bf4c9c1SJohn Baldwin 	device_t bus;
20009bf4c9c1SJohn Baldwin 
20019bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
20029bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
20039bf4c9c1SJohn Baldwin }
20049bf4c9c1SJohn Baldwin 
2005e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */
2006e706f7f0SJohn Baldwin int
2007e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
2008e706f7f0SJohn Baldwin     uint32_t *data)
2009e706f7f0SJohn Baldwin {
2010e706f7f0SJohn Baldwin 	device_t bus;
20114522ac77SLuoqi Chen 	int error;
2012e706f7f0SJohn Baldwin 
2013e706f7f0SJohn Baldwin 	bus = device_get_parent(pcib);
20144522ac77SLuoqi Chen 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
20154522ac77SLuoqi Chen 	if (error)
20164522ac77SLuoqi Chen 		return (error);
20174522ac77SLuoqi Chen 
20184522ac77SLuoqi Chen 	pci_ht_map_msi(pcib, *addr);
20194522ac77SLuoqi Chen 	return (0);
2020e706f7f0SJohn Baldwin }
2021e706f7f0SJohn Baldwin 
202262508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */
202362508c53SJohn Baldwin int
202462508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
202562508c53SJohn Baldwin {
202662508c53SJohn Baldwin 	device_t bus;
202762508c53SJohn Baldwin 
202862508c53SJohn Baldwin 	bus = device_get_parent(pcib);
202962508c53SJohn Baldwin 	return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate));
203062508c53SJohn Baldwin }
20315605a99eSRyan Stone 
20322397d2d8SRyan Stone static int
20332397d2d8SRyan Stone pcib_ari_enabled(device_t pcib)
20342397d2d8SRyan Stone {
20352397d2d8SRyan Stone 	struct pcib_softc *sc;
20362397d2d8SRyan Stone 
20372397d2d8SRyan Stone 	sc = device_get_softc(pcib);
20382397d2d8SRyan Stone 
20392397d2d8SRyan Stone 	return ((sc->flags & PCIB_ENABLE_ARI) != 0);
20402397d2d8SRyan Stone }
20412397d2d8SRyan Stone 
204255d3ea17SRyan Stone static uint16_t
204355d3ea17SRyan Stone pcib_ari_get_rid(device_t pcib, device_t dev)
204455d3ea17SRyan Stone {
204555d3ea17SRyan Stone 	struct pcib_softc *sc;
204655d3ea17SRyan Stone 	uint8_t bus, slot, func;
204755d3ea17SRyan Stone 
204855d3ea17SRyan Stone 	sc = device_get_softc(pcib);
204955d3ea17SRyan Stone 
205055d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
205155d3ea17SRyan Stone 		bus = pci_get_bus(dev);
205255d3ea17SRyan Stone 		func = pci_get_function(dev);
205355d3ea17SRyan Stone 
205455d3ea17SRyan Stone 		return (PCI_ARI_RID(bus, func));
205555d3ea17SRyan Stone 	} else {
205655d3ea17SRyan Stone 		bus = pci_get_bus(dev);
205755d3ea17SRyan Stone 		slot = pci_get_slot(dev);
205855d3ea17SRyan Stone 		func = pci_get_function(dev);
205955d3ea17SRyan Stone 
206055d3ea17SRyan Stone 		return (PCI_RID(bus, slot, func));
206155d3ea17SRyan Stone 	}
206255d3ea17SRyan Stone }
206355d3ea17SRyan Stone 
206455d3ea17SRyan Stone /*
206555d3ea17SRyan Stone  * Check that the downstream port (pcib) and the endpoint device (dev) both
206655d3ea17SRyan Stone  * support ARI.  If so, enable it and return 0, otherwise return an error.
206755d3ea17SRyan Stone  */
206855d3ea17SRyan Stone static int
206955d3ea17SRyan Stone pcib_try_enable_ari(device_t pcib, device_t dev)
207055d3ea17SRyan Stone {
207155d3ea17SRyan Stone 	struct pcib_softc *sc;
207255d3ea17SRyan Stone 	int error;
207355d3ea17SRyan Stone 	uint32_t cap2;
207455d3ea17SRyan Stone 	int ari_cap_off;
207555d3ea17SRyan Stone 	uint32_t ari_ver;
207655d3ea17SRyan Stone 	uint32_t pcie_pos;
207755d3ea17SRyan Stone 
207855d3ea17SRyan Stone 	sc = device_get_softc(pcib);
207955d3ea17SRyan Stone 
208055d3ea17SRyan Stone 	/*
208155d3ea17SRyan Stone 	 * ARI is controlled in a register in the PCIe capability structure.
208255d3ea17SRyan Stone 	 * If the downstream port does not have the PCIe capability structure
208355d3ea17SRyan Stone 	 * then it does not support ARI.
208455d3ea17SRyan Stone 	 */
208555d3ea17SRyan Stone 	error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos);
208655d3ea17SRyan Stone 	if (error != 0)
208755d3ea17SRyan Stone 		return (ENODEV);
208855d3ea17SRyan Stone 
208955d3ea17SRyan Stone 	/* Check that the PCIe port advertises ARI support. */
209055d3ea17SRyan Stone 	cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4);
209155d3ea17SRyan Stone 	if (!(cap2 & PCIEM_CAP2_ARI))
209255d3ea17SRyan Stone 		return (ENODEV);
209355d3ea17SRyan Stone 
209455d3ea17SRyan Stone 	/*
209555d3ea17SRyan Stone 	 * Check that the endpoint device advertises ARI support via the ARI
209655d3ea17SRyan Stone 	 * extended capability structure.
209755d3ea17SRyan Stone 	 */
209855d3ea17SRyan Stone 	error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off);
209955d3ea17SRyan Stone 	if (error != 0)
210055d3ea17SRyan Stone 		return (ENODEV);
210155d3ea17SRyan Stone 
210255d3ea17SRyan Stone 	/*
210355d3ea17SRyan Stone 	 * Finally, check that the endpoint device supports the same version
210455d3ea17SRyan Stone 	 * of ARI that we do.
210555d3ea17SRyan Stone 	 */
210655d3ea17SRyan Stone 	ari_ver = pci_read_config(dev, ari_cap_off, 4);
210755d3ea17SRyan Stone 	if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) {
210855d3ea17SRyan Stone 		if (bootverbose)
210955d3ea17SRyan Stone 			device_printf(pcib,
211055d3ea17SRyan Stone 			    "Unsupported version of ARI (%d) detected\n",
211155d3ea17SRyan Stone 			    PCI_EXTCAP_VER(ari_ver));
211255d3ea17SRyan Stone 
211355d3ea17SRyan Stone 		return (ENXIO);
211455d3ea17SRyan Stone 	}
211555d3ea17SRyan Stone 
211655d3ea17SRyan Stone 	pcib_enable_ari(sc, pcie_pos);
211755d3ea17SRyan Stone 
211855d3ea17SRyan Stone 	return (0);
211955d3ea17SRyan Stone }
2120