xref: /freebsd/sys/dev/pci/pci_pci.c (revision ad6f36f84564256469d3abc10db3110eddcc7462)
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
215c825d4dcSJohn Baldwin pcib_is_isa_range(struct pcib_softc *sc, u_long start, u_long end, u_long count)
216c825d4dcSJohn Baldwin {
217c825d4dcSJohn Baldwin 	u_long next_alias;
218c825d4dcSJohn Baldwin 
219c825d4dcSJohn Baldwin 	if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE))
220c825d4dcSJohn Baldwin 		return (0);
221c825d4dcSJohn Baldwin 
222c825d4dcSJohn Baldwin 	/* Only check fixed ranges for overlap. */
223c825d4dcSJohn Baldwin 	if (start + count - 1 != end)
224c825d4dcSJohn Baldwin 		return (0);
225c825d4dcSJohn Baldwin 
226c825d4dcSJohn Baldwin 	/* ISA aliases are only in the lower 64KB of I/O space. */
227c825d4dcSJohn Baldwin 	if (start >= 65536)
228c825d4dcSJohn Baldwin 		return (0);
229c825d4dcSJohn Baldwin 
230c825d4dcSJohn Baldwin 	/* Check for overlap with 0x000 - 0x0ff as a special case. */
231c825d4dcSJohn Baldwin 	if (start < 0x100)
232c825d4dcSJohn Baldwin 		goto alias;
233c825d4dcSJohn Baldwin 
234c825d4dcSJohn Baldwin 	/*
235c825d4dcSJohn Baldwin 	 * If the start address is an alias, the range is an alias.
236c825d4dcSJohn Baldwin 	 * Otherwise, compute the start of the next alias range and
237c825d4dcSJohn Baldwin 	 * check if it is before the end of the candidate range.
238c825d4dcSJohn Baldwin 	 */
239c825d4dcSJohn Baldwin 	if ((start & 0x300) != 0)
240c825d4dcSJohn Baldwin 		goto alias;
241c825d4dcSJohn Baldwin 	next_alias = (start & ~0x3fful) | 0x100;
242c825d4dcSJohn Baldwin 	if (next_alias <= end)
243c825d4dcSJohn Baldwin 		goto alias;
244c825d4dcSJohn Baldwin 	return (0);
245c825d4dcSJohn Baldwin 
246c825d4dcSJohn Baldwin alias:
247c825d4dcSJohn Baldwin 	if (bootverbose)
248c825d4dcSJohn Baldwin 		device_printf(sc->dev,
249c825d4dcSJohn Baldwin 		    "I/O range %#lx-%#lx overlaps with an ISA alias\n", start,
250c825d4dcSJohn Baldwin 		    end);
251c825d4dcSJohn Baldwin 	return (1);
252c825d4dcSJohn Baldwin }
253c825d4dcSJohn Baldwin 
254c825d4dcSJohn Baldwin static void
255c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res,
256c825d4dcSJohn Baldwin     int count)
257c825d4dcSJohn Baldwin {
258c825d4dcSJohn Baldwin 	struct resource **newarray;
259c825d4dcSJohn Baldwin 	int error, i;
260c825d4dcSJohn Baldwin 
261c825d4dcSJohn Baldwin 	newarray = malloc(sizeof(struct resource *) * (w->count + count),
262c825d4dcSJohn Baldwin 	    M_DEVBUF, M_WAITOK);
263c825d4dcSJohn Baldwin 	if (w->res != NULL)
264c825d4dcSJohn Baldwin 		bcopy(w->res, newarray, sizeof(struct resource *) * w->count);
265c825d4dcSJohn Baldwin 	bcopy(res, newarray + w->count, sizeof(struct resource *) * count);
266c825d4dcSJohn Baldwin 	free(w->res, M_DEVBUF);
267c825d4dcSJohn Baldwin 	w->res = newarray;
268c825d4dcSJohn Baldwin 	w->count += count;
269c825d4dcSJohn Baldwin 
270c825d4dcSJohn Baldwin 	for (i = 0; i < count; i++) {
271c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, rman_get_start(res[i]),
272c825d4dcSJohn Baldwin 		    rman_get_end(res[i]));
273c825d4dcSJohn Baldwin 		if (error)
274c825d4dcSJohn Baldwin 			panic("Failed to add resource to rman");
275c825d4dcSJohn Baldwin 	}
276c825d4dcSJohn Baldwin }
277c825d4dcSJohn Baldwin 
278c825d4dcSJohn Baldwin typedef void (nonisa_callback)(u_long start, u_long end, void *arg);
279c825d4dcSJohn Baldwin 
280c825d4dcSJohn Baldwin static void
281c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(u_long start, u_long end, nonisa_callback *cb,
282c825d4dcSJohn Baldwin     void *arg)
283c825d4dcSJohn Baldwin {
284c825d4dcSJohn Baldwin 	u_long next_end;
285c825d4dcSJohn Baldwin 
286c825d4dcSJohn Baldwin 	/*
287c825d4dcSJohn Baldwin 	 * If start is within an ISA alias range, move up to the start
288c825d4dcSJohn Baldwin 	 * of the next non-alias range.  As a special case, addresses
289c825d4dcSJohn Baldwin 	 * in the range 0x000 - 0x0ff should also be skipped since
290c825d4dcSJohn Baldwin 	 * those are used for various system I/O devices in ISA
291c825d4dcSJohn Baldwin 	 * systems.
292c825d4dcSJohn Baldwin 	 */
293c825d4dcSJohn Baldwin 	if (start <= 65535) {
294c825d4dcSJohn Baldwin 		if (start < 0x100 || (start & 0x300) != 0) {
295c825d4dcSJohn Baldwin 			start &= ~0x3ff;
296c825d4dcSJohn Baldwin 			start += 0x400;
297c825d4dcSJohn Baldwin 		}
298c825d4dcSJohn Baldwin 	}
299c825d4dcSJohn Baldwin 
300c825d4dcSJohn Baldwin 	/* ISA aliases are only in the lower 64KB of I/O space. */
301c825d4dcSJohn Baldwin 	while (start <= MIN(end, 65535)) {
302c825d4dcSJohn Baldwin 		next_end = MIN(start | 0xff, end);
303c825d4dcSJohn Baldwin 		cb(start, next_end, arg);
304c825d4dcSJohn Baldwin 		start += 0x400;
305c825d4dcSJohn Baldwin 	}
306c825d4dcSJohn Baldwin 
307c825d4dcSJohn Baldwin 	if (start <= end)
308c825d4dcSJohn Baldwin 		cb(start, end, arg);
309c825d4dcSJohn Baldwin }
310c825d4dcSJohn Baldwin 
311c825d4dcSJohn Baldwin static void
312c825d4dcSJohn Baldwin count_ranges(u_long start, u_long end, void *arg)
313c825d4dcSJohn Baldwin {
314c825d4dcSJohn Baldwin 	int *countp;
315c825d4dcSJohn Baldwin 
316c825d4dcSJohn Baldwin 	countp = arg;
317c825d4dcSJohn Baldwin 	(*countp)++;
318c825d4dcSJohn Baldwin }
319c825d4dcSJohn Baldwin 
320c825d4dcSJohn Baldwin struct alloc_state {
321c825d4dcSJohn Baldwin 	struct resource **res;
322c825d4dcSJohn Baldwin 	struct pcib_softc *sc;
323c825d4dcSJohn Baldwin 	int count, error;
324c825d4dcSJohn Baldwin };
325c825d4dcSJohn Baldwin 
326c825d4dcSJohn Baldwin static void
327c825d4dcSJohn Baldwin alloc_ranges(u_long start, u_long end, void *arg)
328c825d4dcSJohn Baldwin {
329c825d4dcSJohn Baldwin 	struct alloc_state *as;
330c825d4dcSJohn Baldwin 	struct pcib_window *w;
331c825d4dcSJohn Baldwin 	int rid;
332c825d4dcSJohn Baldwin 
333c825d4dcSJohn Baldwin 	as = arg;
334c825d4dcSJohn Baldwin 	if (as->error != 0)
335c825d4dcSJohn Baldwin 		return;
336c825d4dcSJohn Baldwin 
337c825d4dcSJohn Baldwin 	w = &as->sc->io;
338c825d4dcSJohn Baldwin 	rid = w->reg;
339c825d4dcSJohn Baldwin 	if (bootverbose)
340c825d4dcSJohn Baldwin 		device_printf(as->sc->dev,
341c825d4dcSJohn Baldwin 		    "allocating non-ISA range %#lx-%#lx\n", start, end);
342c825d4dcSJohn Baldwin 	as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT,
343c825d4dcSJohn Baldwin 	    &rid, start, end, end - start + 1, 0);
344c825d4dcSJohn Baldwin 	if (as->res[as->count] == NULL)
345c825d4dcSJohn Baldwin 		as->error = ENXIO;
346c825d4dcSJohn Baldwin 	else
347c825d4dcSJohn Baldwin 		as->count++;
348c825d4dcSJohn Baldwin }
349c825d4dcSJohn Baldwin 
350c825d4dcSJohn Baldwin static int
351c825d4dcSJohn Baldwin pcib_alloc_nonisa_ranges(struct pcib_softc *sc, u_long start, u_long end)
352c825d4dcSJohn Baldwin {
353c825d4dcSJohn Baldwin 	struct alloc_state as;
354c825d4dcSJohn Baldwin 	int i, new_count;
355c825d4dcSJohn Baldwin 
356c825d4dcSJohn Baldwin 	/* First, see how many ranges we need. */
357c825d4dcSJohn Baldwin 	new_count = 0;
358c825d4dcSJohn Baldwin 	pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count);
359c825d4dcSJohn Baldwin 
360c825d4dcSJohn Baldwin 	/* Second, allocate the ranges. */
361c825d4dcSJohn Baldwin 	as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF,
362c825d4dcSJohn Baldwin 	    M_WAITOK);
363c825d4dcSJohn Baldwin 	as.sc = sc;
364c825d4dcSJohn Baldwin 	as.count = 0;
365c825d4dcSJohn Baldwin 	as.error = 0;
366c825d4dcSJohn Baldwin 	pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as);
367c825d4dcSJohn Baldwin 	if (as.error != 0) {
368c825d4dcSJohn Baldwin 		for (i = 0; i < as.count; i++)
369c825d4dcSJohn Baldwin 			bus_release_resource(sc->dev, SYS_RES_IOPORT,
370c825d4dcSJohn Baldwin 			    sc->io.reg, as.res[i]);
371c825d4dcSJohn Baldwin 		free(as.res, M_DEVBUF);
372c825d4dcSJohn Baldwin 		return (as.error);
373c825d4dcSJohn Baldwin 	}
374c825d4dcSJohn Baldwin 	KASSERT(as.count == new_count, ("%s: count mismatch", __func__));
375c825d4dcSJohn Baldwin 
376c825d4dcSJohn Baldwin 	/* Third, add the ranges to the window. */
377c825d4dcSJohn Baldwin 	pcib_add_window_resources(&sc->io, as.res, as.count);
378c825d4dcSJohn Baldwin 	free(as.res, M_DEVBUF);
379c825d4dcSJohn Baldwin 	return (0);
380c825d4dcSJohn Baldwin }
381c825d4dcSJohn Baldwin 
38283c41143SJohn Baldwin static void
38383c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type,
38483c41143SJohn Baldwin     int flags, pci_addr_t max_address)
38583c41143SJohn Baldwin {
386c825d4dcSJohn Baldwin 	struct resource *res;
38783c41143SJohn Baldwin 	char buf[64];
38883c41143SJohn Baldwin 	int error, rid;
38983c41143SJohn Baldwin 
39083c41143SJohn Baldwin 	if (max_address != (u_long)max_address)
39183c41143SJohn Baldwin 		max_address = ~0ul;
39283c41143SJohn Baldwin 	w->rman.rm_start = 0;
39383c41143SJohn Baldwin 	w->rman.rm_end = max_address;
39483c41143SJohn Baldwin 	w->rman.rm_type = RMAN_ARRAY;
39583c41143SJohn Baldwin 	snprintf(buf, sizeof(buf), "%s %s window",
39683c41143SJohn Baldwin 	    device_get_nameunit(sc->dev), w->name);
39783c41143SJohn Baldwin 	w->rman.rm_descr = strdup(buf, M_DEVBUF);
39883c41143SJohn Baldwin 	error = rman_init(&w->rman);
39983c41143SJohn Baldwin 	if (error)
40083c41143SJohn Baldwin 		panic("Failed to initialize %s %s rman",
40183c41143SJohn Baldwin 		    device_get_nameunit(sc->dev), w->name);
40283c41143SJohn Baldwin 
40383c41143SJohn Baldwin 	if (!pcib_is_window_open(w))
40483c41143SJohn Baldwin 		return;
40583c41143SJohn Baldwin 
40683c41143SJohn Baldwin 	if (w->base > max_address || w->limit > max_address) {
40783c41143SJohn Baldwin 		device_printf(sc->dev,
40883c41143SJohn Baldwin 		    "initial %s window has too many bits, ignoring\n", w->name);
40983c41143SJohn Baldwin 		return;
41083c41143SJohn Baldwin 	}
411c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE)
412c825d4dcSJohn Baldwin 		(void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit);
413c825d4dcSJohn Baldwin 	else {
41483c41143SJohn Baldwin 		rid = w->reg;
415c825d4dcSJohn Baldwin 		res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit,
41683c41143SJohn Baldwin 		    w->limit - w->base + 1, flags);
417c825d4dcSJohn Baldwin 		if (res != NULL)
418c825d4dcSJohn Baldwin 			pcib_add_window_resources(w, &res, 1);
419c825d4dcSJohn Baldwin 	}
42083c41143SJohn Baldwin 	if (w->res == NULL) {
42183c41143SJohn Baldwin 		device_printf(sc->dev,
42283c41143SJohn Baldwin 		    "failed to allocate initial %s window: %#jx-%#jx\n",
42383c41143SJohn Baldwin 		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
42483c41143SJohn Baldwin 		w->base = max_address;
42583c41143SJohn Baldwin 		w->limit = 0;
42683c41143SJohn Baldwin 		pcib_write_windows(sc, w->mask);
42783c41143SJohn Baldwin 		return;
42883c41143SJohn Baldwin 	}
42983c41143SJohn Baldwin 	pcib_activate_window(sc, type);
43083c41143SJohn Baldwin }
43183c41143SJohn Baldwin 
43283c41143SJohn Baldwin /*
43383c41143SJohn Baldwin  * Initialize I/O windows.
43483c41143SJohn Baldwin  */
43583c41143SJohn Baldwin static void
43683c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc)
43783c41143SJohn Baldwin {
43883c41143SJohn Baldwin 	pci_addr_t max;
43983c41143SJohn Baldwin 	device_t dev;
44083c41143SJohn Baldwin 	uint32_t val;
44183c41143SJohn Baldwin 
44283c41143SJohn Baldwin 	dev = sc->dev;
44383c41143SJohn Baldwin 
4440070c94bSJohn Baldwin 	if (pci_clear_pcib) {
4450070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
4460070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2);
4470070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1);
4480070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2);
4490070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2);
4500070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2);
4510070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
4520070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4);
4530070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2);
4540070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4);
4550070c94bSJohn Baldwin 	}
4560070c94bSJohn Baldwin 
45783c41143SJohn Baldwin 	/* Determine if the I/O port window is implemented. */
45883c41143SJohn Baldwin 	val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
45983c41143SJohn Baldwin 	if (val == 0) {
46083c41143SJohn Baldwin 		/*
46183c41143SJohn Baldwin 		 * If 'val' is zero, then only 16-bits of I/O space
46283c41143SJohn Baldwin 		 * are supported.
46383c41143SJohn Baldwin 		 */
46483c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
46583c41143SJohn Baldwin 		if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) {
46683c41143SJohn Baldwin 			sc->io.valid = 1;
46783c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOBASEL_1, 0, 1);
46883c41143SJohn Baldwin 		}
46983c41143SJohn Baldwin 	} else
47083c41143SJohn Baldwin 		sc->io.valid = 1;
47183c41143SJohn Baldwin 
47283c41143SJohn Baldwin 	/* Read the existing I/O port window. */
47383c41143SJohn Baldwin 	if (sc->io.valid) {
47483c41143SJohn Baldwin 		sc->io.reg = PCIR_IOBASEL_1;
47583c41143SJohn Baldwin 		sc->io.step = 12;
47683c41143SJohn Baldwin 		sc->io.mask = WIN_IO;
47783c41143SJohn Baldwin 		sc->io.name = "I/O port";
47883c41143SJohn Baldwin 		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
47983c41143SJohn Baldwin 			sc->io.base = PCI_PPBIOBASE(
48083c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOBASEH_1, 2), val);
48183c41143SJohn Baldwin 			sc->io.limit = PCI_PPBIOLIMIT(
48283c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITH_1, 2),
48383c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
48483c41143SJohn Baldwin 			max = 0xffffffff;
48583c41143SJohn Baldwin 		} else {
48683c41143SJohn Baldwin 			sc->io.base = PCI_PPBIOBASE(0, val);
48783c41143SJohn Baldwin 			sc->io.limit = PCI_PPBIOLIMIT(0,
48883c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
48983c41143SJohn Baldwin 			max = 0xffff;
49083c41143SJohn Baldwin 		}
49183c41143SJohn Baldwin 		pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max);
49283c41143SJohn Baldwin 	}
49383c41143SJohn Baldwin 
49483c41143SJohn Baldwin 	/* Read the existing memory window. */
49583c41143SJohn Baldwin 	sc->mem.valid = 1;
49683c41143SJohn Baldwin 	sc->mem.reg = PCIR_MEMBASE_1;
49783c41143SJohn Baldwin 	sc->mem.step = 20;
49883c41143SJohn Baldwin 	sc->mem.mask = WIN_MEM;
49983c41143SJohn Baldwin 	sc->mem.name = "memory";
50083c41143SJohn Baldwin 	sc->mem.base = PCI_PPBMEMBASE(0,
50183c41143SJohn Baldwin 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
50283c41143SJohn Baldwin 	sc->mem.limit = PCI_PPBMEMLIMIT(0,
50383c41143SJohn Baldwin 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
50483c41143SJohn Baldwin 	pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff);
50583c41143SJohn Baldwin 
50683c41143SJohn Baldwin 	/* Determine if the prefetchable memory window is implemented. */
50783c41143SJohn Baldwin 	val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
50883c41143SJohn Baldwin 	if (val == 0) {
50983c41143SJohn Baldwin 		/*
51083c41143SJohn Baldwin 		 * If 'val' is zero, then only 32-bits of memory space
51183c41143SJohn Baldwin 		 * are supported.
51283c41143SJohn Baldwin 		 */
51383c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
51483c41143SJohn Baldwin 		if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) {
51583c41143SJohn Baldwin 			sc->pmem.valid = 1;
51683c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMBASEL_1, 0, 2);
51783c41143SJohn Baldwin 		}
51883c41143SJohn Baldwin 	} else
51983c41143SJohn Baldwin 		sc->pmem.valid = 1;
52083c41143SJohn Baldwin 
52183c41143SJohn Baldwin 	/* Read the existing prefetchable memory window. */
52283c41143SJohn Baldwin 	if (sc->pmem.valid) {
52383c41143SJohn Baldwin 		sc->pmem.reg = PCIR_PMBASEL_1;
52483c41143SJohn Baldwin 		sc->pmem.step = 20;
52583c41143SJohn Baldwin 		sc->pmem.mask = WIN_PMEM;
52683c41143SJohn Baldwin 		sc->pmem.name = "prefetch";
52783c41143SJohn Baldwin 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
52883c41143SJohn Baldwin 			sc->pmem.base = PCI_PPBMEMBASE(
52983c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMBASEH_1, 4), val);
53083c41143SJohn Baldwin 			sc->pmem.limit = PCI_PPBMEMLIMIT(
53183c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITH_1, 4),
53283c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
53383c41143SJohn Baldwin 			max = 0xffffffffffffffff;
53483c41143SJohn Baldwin 		} else {
53583c41143SJohn Baldwin 			sc->pmem.base = PCI_PPBMEMBASE(0, val);
53683c41143SJohn Baldwin 			sc->pmem.limit = PCI_PPBMEMLIMIT(0,
53783c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
53883c41143SJohn Baldwin 			max = 0xffffffff;
53983c41143SJohn Baldwin 		}
54083c41143SJohn Baldwin 		pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY,
54183c41143SJohn Baldwin 		    RF_PREFETCHABLE, max);
54283c41143SJohn Baldwin 	}
54383c41143SJohn Baldwin }
54483c41143SJohn Baldwin 
5454edef187SJohn Baldwin #ifdef PCI_RES_BUS
5464edef187SJohn Baldwin /*
5474edef187SJohn Baldwin  * Allocate a suitable secondary bus for this bridge if needed and
5484edef187SJohn Baldwin  * initialize the resource manager for the secondary bus range.  Note
5494edef187SJohn Baldwin  * that the minimum count is a desired value and this may allocate a
5504edef187SJohn Baldwin  * smaller range.
5514edef187SJohn Baldwin  */
5524edef187SJohn Baldwin void
5534edef187SJohn Baldwin pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count)
5544edef187SJohn Baldwin {
5554edef187SJohn Baldwin 	char buf[64];
556*ad6f36f8SJohn Baldwin 	int error, rid, sec_reg;
5574edef187SJohn Baldwin 
5584edef187SJohn Baldwin 	switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) {
5594edef187SJohn Baldwin 	case PCIM_HDRTYPE_BRIDGE:
560*ad6f36f8SJohn Baldwin 		sec_reg = PCIR_SECBUS_1;
5614edef187SJohn Baldwin 		bus->sub_reg = PCIR_SUBBUS_1;
5624edef187SJohn Baldwin 		break;
5634edef187SJohn Baldwin 	case PCIM_HDRTYPE_CARDBUS:
564*ad6f36f8SJohn Baldwin 		sec_reg = PCIR_SECBUS_2;
5654edef187SJohn Baldwin 		bus->sub_reg = PCIR_SUBBUS_2;
5664edef187SJohn Baldwin 		break;
5674edef187SJohn Baldwin 	default:
5684edef187SJohn Baldwin 		panic("not a PCI bridge");
5694edef187SJohn Baldwin 	}
570*ad6f36f8SJohn Baldwin 	bus->sec = pci_read_config(dev, sec_reg, 1);
571*ad6f36f8SJohn Baldwin 	bus->sub = pci_read_config(dev, bus->sub_reg, 1);
5724edef187SJohn Baldwin 	bus->dev = dev;
5734edef187SJohn Baldwin 	bus->rman.rm_start = 0;
5744edef187SJohn Baldwin 	bus->rman.rm_end = PCI_BUSMAX;
5754edef187SJohn Baldwin 	bus->rman.rm_type = RMAN_ARRAY;
5764edef187SJohn Baldwin 	snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev));
5774edef187SJohn Baldwin 	bus->rman.rm_descr = strdup(buf, M_DEVBUF);
5784edef187SJohn Baldwin 	error = rman_init(&bus->rman);
5794edef187SJohn Baldwin 	if (error)
5804edef187SJohn Baldwin 		panic("Failed to initialize %s bus number rman",
5814edef187SJohn Baldwin 		    device_get_nameunit(dev));
5824edef187SJohn Baldwin 
5834edef187SJohn Baldwin 	/*
5844edef187SJohn Baldwin 	 * Allocate a bus range.  This will return an existing bus range
5854edef187SJohn Baldwin 	 * if one exists, or a new bus range if one does not.
5864edef187SJohn Baldwin 	 */
5874edef187SJohn Baldwin 	rid = 0;
5884edef187SJohn Baldwin 	bus->res = bus_alloc_resource(dev, PCI_RES_BUS, &rid, 0ul, ~0ul,
5894edef187SJohn Baldwin 	    min_count, 0);
5904edef187SJohn Baldwin 	if (bus->res == NULL) {
5914edef187SJohn Baldwin 		/*
5924edef187SJohn Baldwin 		 * Fall back to just allocating a range of a single bus
5934edef187SJohn Baldwin 		 * number.
5944edef187SJohn Baldwin 		 */
5954edef187SJohn Baldwin 		bus->res = bus_alloc_resource(dev, PCI_RES_BUS, &rid, 0ul, ~0ul,
5964edef187SJohn Baldwin 		    1, 0);
5974edef187SJohn Baldwin 	} else if (rman_get_size(bus->res) < min_count)
5984edef187SJohn Baldwin 		/*
5994edef187SJohn Baldwin 		 * Attempt to grow the existing range to satisfy the
6004edef187SJohn Baldwin 		 * minimum desired count.
6014edef187SJohn Baldwin 		 */
6024edef187SJohn Baldwin 		(void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res,
6034edef187SJohn Baldwin 		    rman_get_start(bus->res), rman_get_start(bus->res) +
6044edef187SJohn Baldwin 		    min_count - 1);
6054edef187SJohn Baldwin 
6064edef187SJohn Baldwin 	/*
6074edef187SJohn Baldwin 	 * Add the initial resource to the rman.
6084edef187SJohn Baldwin 	 */
6094edef187SJohn Baldwin 	if (bus->res != NULL) {
6104edef187SJohn Baldwin 		error = rman_manage_region(&bus->rman, rman_get_start(bus->res),
6114edef187SJohn Baldwin 		    rman_get_end(bus->res));
6124edef187SJohn Baldwin 		if (error)
6134edef187SJohn Baldwin 			panic("Failed to add resource to rman");
6144edef187SJohn Baldwin 		bus->sec = rman_get_start(bus->res);
6154edef187SJohn Baldwin 		bus->sub = rman_get_end(bus->res);
6164edef187SJohn Baldwin 	}
6174edef187SJohn Baldwin }
6184edef187SJohn Baldwin 
6194edef187SJohn Baldwin static struct resource *
6204edef187SJohn Baldwin pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid,
6214edef187SJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
6224edef187SJohn Baldwin {
6234edef187SJohn Baldwin 	struct resource *res;
6244edef187SJohn Baldwin 
6254edef187SJohn Baldwin 	res = rman_reserve_resource(&bus->rman, start, end, count, flags,
6264edef187SJohn Baldwin 	    child);
6274edef187SJohn Baldwin 	if (res == NULL)
6284edef187SJohn Baldwin 		return (NULL);
6294edef187SJohn Baldwin 
6304edef187SJohn Baldwin 	if (bootverbose)
6314edef187SJohn Baldwin 		device_printf(bus->dev,
6324edef187SJohn Baldwin 		    "allocated bus range (%lu-%lu) for rid %d of %s\n",
6334edef187SJohn Baldwin 		    rman_get_start(res), rman_get_end(res), *rid,
6344edef187SJohn Baldwin 		    pcib_child_name(child));
6354edef187SJohn Baldwin 	rman_set_rid(res, *rid);
6364edef187SJohn Baldwin 	return (res);
6374edef187SJohn Baldwin }
6384edef187SJohn Baldwin 
6394edef187SJohn Baldwin /*
6404edef187SJohn Baldwin  * Attempt to grow the secondary bus range.  This is much simpler than
6414edef187SJohn Baldwin  * for I/O windows as the range can only be grown by increasing
6424edef187SJohn Baldwin  * subbus.
6434edef187SJohn Baldwin  */
6444edef187SJohn Baldwin static int
6454edef187SJohn Baldwin pcib_grow_subbus(struct pcib_secbus *bus, u_long new_end)
6464edef187SJohn Baldwin {
6474edef187SJohn Baldwin 	u_long old_end;
6484edef187SJohn Baldwin 	int error;
6494edef187SJohn Baldwin 
6504edef187SJohn Baldwin 	old_end = rman_get_end(bus->res);
6514edef187SJohn Baldwin 	KASSERT(new_end > old_end, ("attempt to shrink subbus"));
6524edef187SJohn Baldwin 	error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res,
6534edef187SJohn Baldwin 	    rman_get_start(bus->res), new_end);
6544edef187SJohn Baldwin 	if (error)
6554edef187SJohn Baldwin 		return (error);
6564edef187SJohn Baldwin 	if (bootverbose)
6574edef187SJohn Baldwin 		device_printf(bus->dev, "grew bus range to %lu-%lu\n",
6584edef187SJohn Baldwin 		    rman_get_start(bus->res), rman_get_end(bus->res));
6594edef187SJohn Baldwin 	error = rman_manage_region(&bus->rman, old_end + 1,
6604edef187SJohn Baldwin 	    rman_get_end(bus->res));
6614edef187SJohn Baldwin 	if (error)
6624edef187SJohn Baldwin 		panic("Failed to add resource to rman");
6634edef187SJohn Baldwin 	bus->sub = rman_get_end(bus->res);
6644edef187SJohn Baldwin 	pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1);
6654edef187SJohn Baldwin 	return (0);
6664edef187SJohn Baldwin }
6674edef187SJohn Baldwin 
6684edef187SJohn Baldwin struct resource *
6694edef187SJohn Baldwin pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid,
6704edef187SJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
6714edef187SJohn Baldwin {
6724edef187SJohn Baldwin 	struct resource *res;
6734edef187SJohn Baldwin 	u_long start_free, end_free, new_end;
6744edef187SJohn Baldwin 
6754edef187SJohn Baldwin 	/*
6764edef187SJohn Baldwin 	 * First, see if the request can be satisified by the existing
6774edef187SJohn Baldwin 	 * bus range.
6784edef187SJohn Baldwin 	 */
6794edef187SJohn Baldwin 	res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags);
6804edef187SJohn Baldwin 	if (res != NULL)
6814edef187SJohn Baldwin 		return (res);
6824edef187SJohn Baldwin 
6834edef187SJohn Baldwin 	/*
6844edef187SJohn Baldwin 	 * Figure out a range to grow the bus range.  First, find the
6854edef187SJohn Baldwin 	 * first bus number after the last allocated bus in the rman and
6864edef187SJohn Baldwin 	 * enforce that as a minimum starting point for the range.
6874edef187SJohn Baldwin 	 */
6884edef187SJohn Baldwin 	if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 ||
6894edef187SJohn Baldwin 	    end_free != bus->sub)
6904edef187SJohn Baldwin 		start_free = bus->sub + 1;
6914edef187SJohn Baldwin 	if (start_free < start)
6924edef187SJohn Baldwin 		start_free = start;
6934edef187SJohn Baldwin 	new_end = start_free + count - 1;
6944edef187SJohn Baldwin 
6954edef187SJohn Baldwin 	/*
6964edef187SJohn Baldwin 	 * See if this new range would satisfy the request if it
6974edef187SJohn Baldwin 	 * succeeds.
6984edef187SJohn Baldwin 	 */
6994edef187SJohn Baldwin 	if (new_end > end)
7004edef187SJohn Baldwin 		return (NULL);
7014edef187SJohn Baldwin 
7024edef187SJohn Baldwin 	/* Finally, attempt to grow the existing resource. */
7034edef187SJohn Baldwin 	if (bootverbose) {
7044edef187SJohn Baldwin 		device_printf(bus->dev,
7054edef187SJohn Baldwin 		    "attempting to grow bus range for %lu buses\n", count);
7064edef187SJohn Baldwin 		printf("\tback candidate range: %lu-%lu\n", start_free,
7074edef187SJohn Baldwin 		    new_end);
7084edef187SJohn Baldwin 	}
7094edef187SJohn Baldwin 	if (pcib_grow_subbus(bus, new_end) == 0)
7104edef187SJohn Baldwin 		return (pcib_suballoc_bus(bus, child, rid, start, end, count,
7114edef187SJohn Baldwin 		    flags));
7124edef187SJohn Baldwin 	return (NULL);
7134edef187SJohn Baldwin }
7144edef187SJohn Baldwin #endif
7154edef187SJohn Baldwin 
71683c41143SJohn Baldwin #else
71783c41143SJohn Baldwin 
718bb0d0a8eSMike Smith /*
719b0a2d4b8SWarner Losh  * Is the prefetch window open (eg, can we allocate memory in it?)
720b0a2d4b8SWarner Losh  */
721b0a2d4b8SWarner Losh static int
722b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc)
723b0a2d4b8SWarner Losh {
724b0a2d4b8SWarner Losh 	return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
725b0a2d4b8SWarner Losh }
726b0a2d4b8SWarner Losh 
727b0a2d4b8SWarner Losh /*
728b0a2d4b8SWarner Losh  * Is the nonprefetch window open (eg, can we allocate memory in it?)
729b0a2d4b8SWarner Losh  */
730b0a2d4b8SWarner Losh static int
731b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc)
732b0a2d4b8SWarner Losh {
733b0a2d4b8SWarner Losh 	return (sc->membase > 0 && sc->membase < sc->memlimit);
734b0a2d4b8SWarner Losh }
735b0a2d4b8SWarner Losh 
736b0a2d4b8SWarner Losh /*
737b0a2d4b8SWarner Losh  * Is the io window open (eg, can we allocate ports in it?)
738b0a2d4b8SWarner Losh  */
739b0a2d4b8SWarner Losh static int
740b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc)
741b0a2d4b8SWarner Losh {
742b0a2d4b8SWarner Losh 	return (sc->iobase > 0 && sc->iobase < sc->iolimit);
743b0a2d4b8SWarner Losh }
744b0a2d4b8SWarner Losh 
745b0a2d4b8SWarner Losh /*
746e36af292SJung-uk Kim  * Get current I/O decode.
747e36af292SJung-uk Kim  */
748e36af292SJung-uk Kim static void
749e36af292SJung-uk Kim pcib_get_io_decode(struct pcib_softc *sc)
750e36af292SJung-uk Kim {
751e36af292SJung-uk Kim 	device_t	dev;
752e36af292SJung-uk Kim 	uint32_t	iolow;
753e36af292SJung-uk Kim 
754e36af292SJung-uk Kim 	dev = sc->dev;
755e36af292SJung-uk Kim 
756e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
757e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
758e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(
759e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow);
760e36af292SJung-uk Kim 	else
761e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(0, iolow);
762e36af292SJung-uk Kim 
763e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
764e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
765e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(
766e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow);
767e36af292SJung-uk Kim 	else
768e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(0, iolow);
769e36af292SJung-uk Kim }
770e36af292SJung-uk Kim 
771e36af292SJung-uk Kim /*
772e36af292SJung-uk Kim  * Get current memory decode.
773e36af292SJung-uk Kim  */
774e36af292SJung-uk Kim static void
775e36af292SJung-uk Kim pcib_get_mem_decode(struct pcib_softc *sc)
776e36af292SJung-uk Kim {
777e36af292SJung-uk Kim 	device_t	dev;
778e36af292SJung-uk Kim 	pci_addr_t	pmemlow;
779e36af292SJung-uk Kim 
780e36af292SJung-uk Kim 	dev = sc->dev;
781e36af292SJung-uk Kim 
782e36af292SJung-uk Kim 	sc->membase = PCI_PPBMEMBASE(0,
783e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
784e36af292SJung-uk Kim 	sc->memlimit = PCI_PPBMEMLIMIT(0,
785e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
786e36af292SJung-uk Kim 
787e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2);
788e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
789e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(
790e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow);
791e36af292SJung-uk Kim 	else
792e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(0, pmemlow);
793e36af292SJung-uk Kim 
794e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2);
795e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
796e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(
797e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow);
798e36af292SJung-uk Kim 	else
799e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow);
800e36af292SJung-uk Kim }
801e36af292SJung-uk Kim 
802e36af292SJung-uk Kim /*
803e36af292SJung-uk Kim  * Restore previous I/O decode.
804e36af292SJung-uk Kim  */
805e36af292SJung-uk Kim static void
806e36af292SJung-uk Kim pcib_set_io_decode(struct pcib_softc *sc)
807e36af292SJung-uk Kim {
808e36af292SJung-uk Kim 	device_t	dev;
809e36af292SJung-uk Kim 	uint32_t	iohi;
810e36af292SJung-uk Kim 
811e36af292SJung-uk Kim 	dev = sc->dev;
812e36af292SJung-uk Kim 
813e36af292SJung-uk Kim 	iohi = sc->iobase >> 16;
814e36af292SJung-uk Kim 	if (iohi > 0)
815e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2);
816e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1);
817e36af292SJung-uk Kim 
818e36af292SJung-uk Kim 	iohi = sc->iolimit >> 16;
819e36af292SJung-uk Kim 	if (iohi > 0)
820e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2);
821e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1);
822e36af292SJung-uk Kim }
823e36af292SJung-uk Kim 
824e36af292SJung-uk Kim /*
825e36af292SJung-uk Kim  * Restore previous memory decode.
826e36af292SJung-uk Kim  */
827e36af292SJung-uk Kim static void
828e36af292SJung-uk Kim pcib_set_mem_decode(struct pcib_softc *sc)
829e36af292SJung-uk Kim {
830e36af292SJung-uk Kim 	device_t	dev;
831e36af292SJung-uk Kim 	pci_addr_t	pmemhi;
832e36af292SJung-uk Kim 
833e36af292SJung-uk Kim 	dev = sc->dev;
834e36af292SJung-uk Kim 
835e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2);
836e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2);
837e36af292SJung-uk Kim 
838e36af292SJung-uk Kim 	pmemhi = sc->pmembase >> 32;
839e36af292SJung-uk Kim 	if (pmemhi > 0)
840e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4);
841e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2);
842e36af292SJung-uk Kim 
843e36af292SJung-uk Kim 	pmemhi = sc->pmemlimit >> 32;
844e36af292SJung-uk Kim 	if (pmemhi > 0)
845e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4);
846e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2);
847e36af292SJung-uk Kim }
84883c41143SJohn Baldwin #endif
849e36af292SJung-uk Kim 
850e36af292SJung-uk Kim /*
851e36af292SJung-uk Kim  * Get current bridge configuration.
852e36af292SJung-uk Kim  */
853e36af292SJung-uk Kim static void
854e36af292SJung-uk Kim pcib_cfg_save(struct pcib_softc *sc)
855e36af292SJung-uk Kim {
856*ad6f36f8SJohn Baldwin #ifndef NEW_PCIB
857e36af292SJung-uk Kim 	device_t	dev;
858*ad6f36f8SJohn Baldwin 	uint16_t command;
859e36af292SJung-uk Kim 
860e36af292SJung-uk Kim 	dev = sc->dev;
861e36af292SJung-uk Kim 
862*ad6f36f8SJohn Baldwin 	command = pci_read_config(dev, PCIR_COMMAND, 2);
863*ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_PORTEN)
864e36af292SJung-uk Kim 		pcib_get_io_decode(sc);
865*ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_MEMEN)
866e36af292SJung-uk Kim 		pcib_get_mem_decode(sc);
86783c41143SJohn Baldwin #endif
868e36af292SJung-uk Kim }
869e36af292SJung-uk Kim 
870e36af292SJung-uk Kim /*
871e36af292SJung-uk Kim  * Restore previous bridge configuration.
872e36af292SJung-uk Kim  */
873e36af292SJung-uk Kim static void
874e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc)
875e36af292SJung-uk Kim {
876e36af292SJung-uk Kim 	device_t	dev;
877*ad6f36f8SJohn Baldwin #ifndef NEW_PCIB
878*ad6f36f8SJohn Baldwin 	uint16_t command;
879*ad6f36f8SJohn Baldwin #endif
880e36af292SJung-uk Kim 	dev = sc->dev;
881e36af292SJung-uk Kim 
88283c41143SJohn Baldwin #ifdef NEW_PCIB
88383c41143SJohn Baldwin 	pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM);
88483c41143SJohn Baldwin #else
885*ad6f36f8SJohn Baldwin 	command = pci_read_config(dev, PCIR_COMMAND, 2);
886*ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_PORTEN)
887e36af292SJung-uk Kim 		pcib_set_io_decode(sc);
888*ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_MEMEN)
889e36af292SJung-uk Kim 		pcib_set_mem_decode(sc);
89083c41143SJohn Baldwin #endif
891e36af292SJung-uk Kim }
892e36af292SJung-uk Kim 
893e36af292SJung-uk Kim /*
894bb0d0a8eSMike Smith  * Generic device interface
895bb0d0a8eSMike Smith  */
896bb0d0a8eSMike Smith static int
897bb0d0a8eSMike Smith pcib_probe(device_t dev)
898bb0d0a8eSMike Smith {
899bb0d0a8eSMike Smith     if ((pci_get_class(dev) == PCIC_BRIDGE) &&
900bb0d0a8eSMike Smith 	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
901bb0d0a8eSMike Smith 	device_set_desc(dev, "PCI-PCI bridge");
902bb0d0a8eSMike Smith 	return(-10000);
903bb0d0a8eSMike Smith     }
904bb0d0a8eSMike Smith     return(ENXIO);
905bb0d0a8eSMike Smith }
906bb0d0a8eSMike Smith 
9076f0d5884SJohn Baldwin void
9086f0d5884SJohn Baldwin pcib_attach_common(device_t dev)
909bb0d0a8eSMike Smith {
910bb0d0a8eSMike Smith     struct pcib_softc	*sc;
911abf07f13SWarner Losh     struct sysctl_ctx_list *sctx;
912abf07f13SWarner Losh     struct sysctl_oid	*soid;
913c825d4dcSJohn Baldwin     int comma;
914bb0d0a8eSMike Smith 
915bb0d0a8eSMike Smith     sc = device_get_softc(dev);
916bb0d0a8eSMike Smith     sc->dev = dev;
917bb0d0a8eSMike Smith 
9184fa59183SMike Smith     /*
9194fa59183SMike Smith      * Get current bridge configuration.
9204fa59183SMike Smith      */
92155aaf894SMarius Strobl     sc->domain = pci_get_domain(dev);
922*ad6f36f8SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
923*ad6f36f8SJohn Baldwin     sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1);
924*ad6f36f8SJohn Baldwin     sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
925*ad6f36f8SJohn Baldwin #endif
926*ad6f36f8SJohn Baldwin     sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
927e36af292SJung-uk Kim     pcib_cfg_save(sc);
9284fa59183SMike Smith 
9294fa59183SMike Smith     /*
9304edef187SJohn Baldwin      * The primary bus register should always be the bus of the
9314edef187SJohn Baldwin      * parent.
9324edef187SJohn Baldwin      */
9334edef187SJohn Baldwin     sc->pribus = pci_get_bus(dev);
9344edef187SJohn Baldwin     pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
9354edef187SJohn Baldwin 
9364edef187SJohn Baldwin     /*
937abf07f13SWarner Losh      * Setup sysctl reporting nodes
938abf07f13SWarner Losh      */
939abf07f13SWarner Losh     sctx = device_get_sysctl_ctx(dev);
940abf07f13SWarner Losh     soid = device_get_sysctl_tree(dev);
941abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
942abf07f13SWarner Losh       CTLFLAG_RD, &sc->domain, 0, "Domain number");
943abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
944abf07f13SWarner Losh       CTLFLAG_RD, &sc->pribus, 0, "Primary bus number");
945abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
9464edef187SJohn Baldwin       CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number");
947abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
9484edef187SJohn Baldwin       CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number");
949abf07f13SWarner Losh 
950abf07f13SWarner Losh     /*
9514fa59183SMike Smith      * Quirk handling.
9524fa59183SMike Smith      */
9534fa59183SMike Smith     switch (pci_get_devid(dev)) {
9542ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
9554fa59183SMike Smith     case 0x12258086:		/* Intel 82454KX/GX (Orion) */
9564fa59183SMike Smith 	{
957b0cb115fSWarner Losh 	    uint8_t	supbus;
9584fa59183SMike Smith 
9594fa59183SMike Smith 	    supbus = pci_read_config(dev, 0x41, 1);
9604fa59183SMike Smith 	    if (supbus != 0xff) {
9614edef187SJohn Baldwin 		sc->bus.sec = supbus + 1;
9624edef187SJohn Baldwin 		sc->bus.sub = supbus + 1;
9634fa59183SMike Smith 	    }
9644fa59183SMike Smith 	    break;
9654fa59183SMike Smith 	}
9664edef187SJohn Baldwin #endif
9674fa59183SMike Smith 
968e4b59fc5SWarner Losh     /*
969e4b59fc5SWarner Losh      * The i82380FB mobile docking controller is a PCI-PCI bridge,
970e4b59fc5SWarner Losh      * and it is a subtractive bridge.  However, the ProgIf is wrong
971e4b59fc5SWarner Losh      * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
972e4b59fc5SWarner Losh      * happen.  There's also a Toshiba bridge that behaves this
973e4b59fc5SWarner Losh      * way.
974e4b59fc5SWarner Losh      */
975e4b59fc5SWarner Losh     case 0x124b8086:		/* Intel 82380FB Mobile */
976e4b59fc5SWarner Losh     case 0x060513d7:		/* Toshiba ???? */
977e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
978e4b59fc5SWarner Losh 	break;
979c94d6dbeSJung-uk Kim 
9802ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
981c94d6dbeSJung-uk Kim     /* Compaq R3000 BIOS sets wrong subordinate bus number. */
982c94d6dbeSJung-uk Kim     case 0x00dd10de:
983c94d6dbeSJung-uk Kim 	{
984c94d6dbeSJung-uk Kim 	    char *cp;
985c94d6dbeSJung-uk Kim 
9862be111bfSDavide Italiano 	    if ((cp = kern_getenv("smbios.planar.maker")) == NULL)
987c94d6dbeSJung-uk Kim 		break;
9881def0ca6SJung-uk Kim 	    if (strncmp(cp, "Compal", 6) != 0) {
9891def0ca6SJung-uk Kim 		freeenv(cp);
990c94d6dbeSJung-uk Kim 		break;
9911def0ca6SJung-uk Kim 	    }
9921def0ca6SJung-uk Kim 	    freeenv(cp);
9932be111bfSDavide Italiano 	    if ((cp = kern_getenv("smbios.planar.product")) == NULL)
9941def0ca6SJung-uk Kim 		break;
9951def0ca6SJung-uk Kim 	    if (strncmp(cp, "08A0", 4) != 0) {
9961def0ca6SJung-uk Kim 		freeenv(cp);
9971def0ca6SJung-uk Kim 		break;
9981def0ca6SJung-uk Kim 	    }
9991def0ca6SJung-uk Kim 	    freeenv(cp);
10004edef187SJohn Baldwin 	    if (sc->bus.sub < 0xa) {
1001c94d6dbeSJung-uk Kim 		pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
10024edef187SJohn Baldwin 		sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
1003c94d6dbeSJung-uk Kim 	    }
1004c94d6dbeSJung-uk Kim 	    break;
1005c94d6dbeSJung-uk Kim 	}
10064edef187SJohn Baldwin #endif
1007e4b59fc5SWarner Losh     }
1008e4b59fc5SWarner Losh 
100922bf1c7fSJohn Baldwin     if (pci_msi_device_blacklisted(dev))
101022bf1c7fSJohn Baldwin 	sc->flags |= PCIB_DISABLE_MSI;
101122bf1c7fSJohn Baldwin 
101268e9cbd3SMarius Strobl     if (pci_msix_device_blacklisted(dev))
101368e9cbd3SMarius Strobl 	sc->flags |= PCIB_DISABLE_MSIX;
101468e9cbd3SMarius Strobl 
1015e4b59fc5SWarner Losh     /*
1016e4b59fc5SWarner Losh      * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
1017e4b59fc5SWarner Losh      * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
1018e4b59fc5SWarner Losh      * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
1019e4b59fc5SWarner Losh      * This means they act as if they were subtractively decoding
1020e4b59fc5SWarner Losh      * bridges and pass all transactions.  Mark them and real ProgIf 1
1021e4b59fc5SWarner Losh      * parts as subtractive.
1022e4b59fc5SWarner Losh      */
1023e4b59fc5SWarner Losh     if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
1024657d9f9fSJohn Baldwin       pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
1025e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
1026e4b59fc5SWarner Losh 
102783c41143SJohn Baldwin #ifdef NEW_PCIB
10284edef187SJohn Baldwin #ifdef PCI_RES_BUS
10294edef187SJohn Baldwin     pcib_setup_secbus(dev, &sc->bus, 1);
10304edef187SJohn Baldwin #endif
103183c41143SJohn Baldwin     pcib_probe_windows(sc);
103283c41143SJohn Baldwin #endif
1033bb0d0a8eSMike Smith     if (bootverbose) {
103455aaf894SMarius Strobl 	device_printf(dev, "  domain            %d\n", sc->domain);
10354edef187SJohn Baldwin 	device_printf(dev, "  secondary bus     %d\n", sc->bus.sec);
10364edef187SJohn Baldwin 	device_printf(dev, "  subordinate bus   %d\n", sc->bus.sub);
103783c41143SJohn Baldwin #ifdef NEW_PCIB
103883c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->io))
103983c41143SJohn Baldwin 	    device_printf(dev, "  I/O decode        0x%jx-0x%jx\n",
104083c41143SJohn Baldwin 	      (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit);
104183c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->mem))
104283c41143SJohn Baldwin 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
104383c41143SJohn Baldwin 	      (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit);
104483c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->pmem))
104583c41143SJohn Baldwin 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
104683c41143SJohn Baldwin 	      (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit);
104783c41143SJohn Baldwin #else
104883c41143SJohn Baldwin 	if (pcib_is_io_open(sc))
104983c41143SJohn Baldwin 	    device_printf(dev, "  I/O decode        0x%x-0x%x\n",
105083c41143SJohn Baldwin 	      sc->iobase, sc->iolimit);
1051b0a2d4b8SWarner Losh 	if (pcib_is_nonprefetch_open(sc))
1052b0a2d4b8SWarner Losh 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
1053b0a2d4b8SWarner Losh 	      (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
1054b0a2d4b8SWarner Losh 	if (pcib_is_prefetch_open(sc))
1055b0a2d4b8SWarner Losh 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
1056b0a2d4b8SWarner Losh 	      (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
105783c41143SJohn Baldwin #endif
1058c825d4dcSJohn Baldwin 	if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) ||
1059c825d4dcSJohn Baldwin 	    sc->flags & PCIB_SUBTRACTIVE) {
1060c825d4dcSJohn Baldwin 		device_printf(dev, "  special decode    ");
1061c825d4dcSJohn Baldwin 		comma = 0;
1062c825d4dcSJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) {
1063c825d4dcSJohn Baldwin 			printf("ISA");
1064c825d4dcSJohn Baldwin 			comma = 1;
1065c825d4dcSJohn Baldwin 		}
1066c825d4dcSJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) {
1067c825d4dcSJohn Baldwin 			printf("%sVGA", comma ? ", " : "");
1068c825d4dcSJohn Baldwin 			comma = 1;
1069c825d4dcSJohn Baldwin 		}
1070e4b59fc5SWarner Losh 		if (sc->flags & PCIB_SUBTRACTIVE)
1071c825d4dcSJohn Baldwin 			printf("%ssubtractive", comma ? ", " : "");
1072c825d4dcSJohn Baldwin 		printf("\n");
1073c825d4dcSJohn Baldwin 	}
1074bb0d0a8eSMike Smith     }
1075bb0d0a8eSMike Smith 
1076bb0d0a8eSMike Smith     /*
1077ef888152SJohn Baldwin      * Always enable busmastering on bridges so that transactions
1078ef888152SJohn Baldwin      * initiated on the secondary bus are passed through to the
1079ef888152SJohn Baldwin      * primary bus.
1080ef888152SJohn Baldwin      */
1081ef888152SJohn Baldwin     pci_enable_busmaster(dev);
10826f0d5884SJohn Baldwin }
1083bb0d0a8eSMike Smith 
108438906aedSJohn Baldwin int
10856f0d5884SJohn Baldwin pcib_attach(device_t dev)
10866f0d5884SJohn Baldwin {
10876f0d5884SJohn Baldwin     struct pcib_softc	*sc;
10886f0d5884SJohn Baldwin     device_t		child;
10896f0d5884SJohn Baldwin 
10906f0d5884SJohn Baldwin     pcib_attach_common(dev);
10916f0d5884SJohn Baldwin     sc = device_get_softc(dev);
10924edef187SJohn Baldwin     if (sc->bus.sec != 0) {
10934edef187SJohn Baldwin 	child = device_add_child(dev, "pci", sc->bus.sec);
1094bb0d0a8eSMike Smith 	if (child != NULL)
1095bb0d0a8eSMike Smith 	    return(bus_generic_attach(dev));
1096bb0d0a8eSMike Smith     }
1097bb0d0a8eSMike Smith 
1098bb0d0a8eSMike Smith     /* no secondary bus; we should have fixed this */
1099bb0d0a8eSMike Smith     return(0);
1100bb0d0a8eSMike Smith }
1101bb0d0a8eSMike Smith 
11026f0d5884SJohn Baldwin int
1103e36af292SJung-uk Kim pcib_suspend(device_t dev)
1104e36af292SJung-uk Kim {
1105e36af292SJung-uk Kim 
1106e36af292SJung-uk Kim 	pcib_cfg_save(device_get_softc(dev));
11077212fc6aSJohn Baldwin 	return (bus_generic_suspend(dev));
1108e36af292SJung-uk Kim }
1109e36af292SJung-uk Kim 
1110e36af292SJung-uk Kim int
1111e36af292SJung-uk Kim pcib_resume(device_t dev)
1112e36af292SJung-uk Kim {
1113e36af292SJung-uk Kim 
1114e36af292SJung-uk Kim 	pcib_cfg_restore(device_get_softc(dev));
1115e36af292SJung-uk Kim 	return (bus_generic_resume(dev));
1116e36af292SJung-uk Kim }
1117e36af292SJung-uk Kim 
1118e36af292SJung-uk Kim int
1119bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1120bb0d0a8eSMike Smith {
1121bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
1122bb0d0a8eSMike Smith 
1123bb0d0a8eSMike Smith     switch (which) {
112455aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
112555aaf894SMarius Strobl 	*result = sc->domain;
112655aaf894SMarius Strobl 	return(0);
1127bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
11284edef187SJohn Baldwin 	*result = sc->bus.sec;
1129bb0d0a8eSMike Smith 	return(0);
1130bb0d0a8eSMike Smith     }
1131bb0d0a8eSMike Smith     return(ENOENT);
1132bb0d0a8eSMike Smith }
1133bb0d0a8eSMike Smith 
11346f0d5884SJohn Baldwin int
1135bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1136bb0d0a8eSMike Smith {
1137bb0d0a8eSMike Smith 
1138bb0d0a8eSMike Smith     switch (which) {
113955aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
114055aaf894SMarius Strobl 	return(EINVAL);
1141bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
11424edef187SJohn Baldwin 	return(EINVAL);
1143bb0d0a8eSMike Smith     }
1144bb0d0a8eSMike Smith     return(ENOENT);
1145bb0d0a8eSMike Smith }
1146bb0d0a8eSMike Smith 
114783c41143SJohn Baldwin #ifdef NEW_PCIB
114883c41143SJohn Baldwin /*
114983c41143SJohn Baldwin  * Attempt to allocate a resource from the existing resources assigned
115083c41143SJohn Baldwin  * to a window.
115183c41143SJohn Baldwin  */
115283c41143SJohn Baldwin static struct resource *
115383c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w,
115483c41143SJohn Baldwin     device_t child, int type, int *rid, u_long start, u_long end, u_long count,
115583c41143SJohn Baldwin     u_int flags)
115683c41143SJohn Baldwin {
115783c41143SJohn Baldwin 	struct resource *res;
115883c41143SJohn Baldwin 
115983c41143SJohn Baldwin 	if (!pcib_is_window_open(w))
116083c41143SJohn Baldwin 		return (NULL);
116183c41143SJohn Baldwin 
116283c41143SJohn Baldwin 	res = rman_reserve_resource(&w->rman, start, end, count,
116383c41143SJohn Baldwin 	    flags & ~RF_ACTIVE, child);
116483c41143SJohn Baldwin 	if (res == NULL)
116583c41143SJohn Baldwin 		return (NULL);
116683c41143SJohn Baldwin 
116783c41143SJohn Baldwin 	if (bootverbose)
116883c41143SJohn Baldwin 		device_printf(sc->dev,
116983c41143SJohn Baldwin 		    "allocated %s range (%#lx-%#lx) for rid %x of %s\n",
117083c41143SJohn Baldwin 		    w->name, rman_get_start(res), rman_get_end(res), *rid,
117183c41143SJohn Baldwin 		    pcib_child_name(child));
117283c41143SJohn Baldwin 	rman_set_rid(res, *rid);
117383c41143SJohn Baldwin 
117483c41143SJohn Baldwin 	/*
117583c41143SJohn Baldwin 	 * If the resource should be active, pass that request up the
117683c41143SJohn Baldwin 	 * tree.  This assumes the parent drivers can handle
117783c41143SJohn Baldwin 	 * activating sub-allocated resources.
117883c41143SJohn Baldwin 	 */
117983c41143SJohn Baldwin 	if (flags & RF_ACTIVE) {
118083c41143SJohn Baldwin 		if (bus_activate_resource(child, type, *rid, res) != 0) {
118183c41143SJohn Baldwin 			rman_release_resource(res);
118283c41143SJohn Baldwin 			return (NULL);
118383c41143SJohn Baldwin 		}
118483c41143SJohn Baldwin 	}
118583c41143SJohn Baldwin 
118683c41143SJohn Baldwin 	return (res);
118783c41143SJohn Baldwin }
118883c41143SJohn Baldwin 
1189c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */
1190c825d4dcSJohn Baldwin static int
1191c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type,
1192c825d4dcSJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
1193c825d4dcSJohn Baldwin {
1194c825d4dcSJohn Baldwin 	struct resource *res;
1195c825d4dcSJohn Baldwin 	u_long base, limit, wmask;
1196c825d4dcSJohn Baldwin 	int rid;
1197c825d4dcSJohn Baldwin 
1198c825d4dcSJohn Baldwin 	/*
1199c825d4dcSJohn Baldwin 	 * If this is an I/O window on a bridge with ISA enable set
1200c825d4dcSJohn Baldwin 	 * and the start address is below 64k, then try to allocate an
1201c825d4dcSJohn Baldwin 	 * initial window of 0x1000 bytes long starting at address
1202c825d4dcSJohn Baldwin 	 * 0xf000 and walking down.  Note that if the original request
1203c825d4dcSJohn Baldwin 	 * was larger than the non-aliased range size of 0x100 our
1204c825d4dcSJohn Baldwin 	 * caller would have raised the start address up to 64k
1205c825d4dcSJohn Baldwin 	 * already.
1206c825d4dcSJohn Baldwin 	 */
1207c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1208c825d4dcSJohn Baldwin 	    start < 65536) {
1209c825d4dcSJohn Baldwin 		for (base = 0xf000; (long)base >= 0; base -= 0x1000) {
1210c825d4dcSJohn Baldwin 			limit = base + 0xfff;
1211c825d4dcSJohn Baldwin 
1212c825d4dcSJohn Baldwin 			/*
1213c825d4dcSJohn Baldwin 			 * Skip ranges that wouldn't work for the
1214c825d4dcSJohn Baldwin 			 * original request.  Note that the actual
1215c825d4dcSJohn Baldwin 			 * window that overlaps are the non-alias
1216c825d4dcSJohn Baldwin 			 * ranges within [base, limit], so this isn't
1217c825d4dcSJohn Baldwin 			 * quite a simple comparison.
1218c825d4dcSJohn Baldwin 			 */
1219c825d4dcSJohn Baldwin 			if (start + count > limit - 0x400)
1220c825d4dcSJohn Baldwin 				continue;
1221c825d4dcSJohn Baldwin 			if (base == 0) {
1222c825d4dcSJohn Baldwin 				/*
1223c825d4dcSJohn Baldwin 				 * The first open region for the window at
1224c825d4dcSJohn Baldwin 				 * 0 is 0x400-0x4ff.
1225c825d4dcSJohn Baldwin 				 */
1226c825d4dcSJohn Baldwin 				if (end - count + 1 < 0x400)
1227c825d4dcSJohn Baldwin 					continue;
1228c825d4dcSJohn Baldwin 			} else {
1229c825d4dcSJohn Baldwin 				if (end - count + 1 < base)
1230c825d4dcSJohn Baldwin 					continue;
1231c825d4dcSJohn Baldwin 			}
1232c825d4dcSJohn Baldwin 
1233c825d4dcSJohn Baldwin 			if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) {
1234c825d4dcSJohn Baldwin 				w->base = base;
1235c825d4dcSJohn Baldwin 				w->limit = limit;
1236c825d4dcSJohn Baldwin 				return (0);
1237c825d4dcSJohn Baldwin 			}
1238c825d4dcSJohn Baldwin 		}
1239c825d4dcSJohn Baldwin 		return (ENOSPC);
1240c825d4dcSJohn Baldwin 	}
1241c825d4dcSJohn Baldwin 
1242c825d4dcSJohn Baldwin 	wmask = (1ul << w->step) - 1;
1243c825d4dcSJohn Baldwin 	if (RF_ALIGNMENT(flags) < w->step) {
1244c825d4dcSJohn Baldwin 		flags &= ~RF_ALIGNMENT_MASK;
1245c825d4dcSJohn Baldwin 		flags |= RF_ALIGNMENT_LOG2(w->step);
1246c825d4dcSJohn Baldwin 	}
1247c825d4dcSJohn Baldwin 	start &= ~wmask;
1248c825d4dcSJohn Baldwin 	end |= wmask;
1249c825d4dcSJohn Baldwin 	count = roundup2(count, 1ul << w->step);
1250c825d4dcSJohn Baldwin 	rid = w->reg;
1251c825d4dcSJohn Baldwin 	res = bus_alloc_resource(sc->dev, type, &rid, start, end, count,
1252c825d4dcSJohn Baldwin 	    flags & ~RF_ACTIVE);
1253c825d4dcSJohn Baldwin 	if (res == NULL)
1254c825d4dcSJohn Baldwin 		return (ENOSPC);
1255c825d4dcSJohn Baldwin 	pcib_add_window_resources(w, &res, 1);
1256c825d4dcSJohn Baldwin 	pcib_activate_window(sc, type);
1257c825d4dcSJohn Baldwin 	w->base = rman_get_start(res);
1258c825d4dcSJohn Baldwin 	w->limit = rman_get_end(res);
1259c825d4dcSJohn Baldwin 	return (0);
1260c825d4dcSJohn Baldwin }
1261c825d4dcSJohn Baldwin 
1262c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */
1263c825d4dcSJohn Baldwin static int
1264c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type,
1265c825d4dcSJohn Baldwin     u_long base, u_long limit)
1266c825d4dcSJohn Baldwin {
1267c825d4dcSJohn Baldwin 	struct resource *res;
1268c825d4dcSJohn Baldwin 	int error, i, force_64k_base;
1269c825d4dcSJohn Baldwin 
1270c825d4dcSJohn Baldwin 	KASSERT(base <= w->base && limit >= w->limit,
1271c825d4dcSJohn Baldwin 	    ("attempting to shrink window"));
1272c825d4dcSJohn Baldwin 
1273c825d4dcSJohn Baldwin 	/*
1274c825d4dcSJohn Baldwin 	 * XXX: pcib_grow_window() doesn't try to do this anyway and
1275c825d4dcSJohn Baldwin 	 * the error handling for all the edge cases would be tedious.
1276c825d4dcSJohn Baldwin 	 */
1277c825d4dcSJohn Baldwin 	KASSERT(limit == w->limit || base == w->base,
1278c825d4dcSJohn Baldwin 	    ("attempting to grow both ends of a window"));
1279c825d4dcSJohn Baldwin 
1280c825d4dcSJohn Baldwin 	/*
1281c825d4dcSJohn Baldwin 	 * Yet more special handling for requests to expand an I/O
1282c825d4dcSJohn Baldwin 	 * window behind an ISA-enabled bridge.  Since I/O windows
1283c825d4dcSJohn Baldwin 	 * have to grow in 0x1000 increments and the end of the 0xffff
1284c825d4dcSJohn Baldwin 	 * range is an alias, growing a window below 64k will always
1285c825d4dcSJohn Baldwin 	 * result in allocating new resources and never adjusting an
1286c825d4dcSJohn Baldwin 	 * existing resource.
1287c825d4dcSJohn Baldwin 	 */
1288c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1289c825d4dcSJohn Baldwin 	    (limit <= 65535 || (base <= 65535 && base != w->base))) {
1290c825d4dcSJohn Baldwin 		KASSERT(limit == w->limit || limit <= 65535,
1291c825d4dcSJohn Baldwin 		    ("attempting to grow both ends across 64k ISA alias"));
1292c825d4dcSJohn Baldwin 
1293c825d4dcSJohn Baldwin 		if (base != w->base)
1294c825d4dcSJohn Baldwin 			error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1);
1295c825d4dcSJohn Baldwin 		else
1296c825d4dcSJohn Baldwin 			error = pcib_alloc_nonisa_ranges(sc, w->limit + 1,
1297c825d4dcSJohn Baldwin 			    limit);
1298c825d4dcSJohn Baldwin 		if (error == 0) {
1299c825d4dcSJohn Baldwin 			w->base = base;
1300c825d4dcSJohn Baldwin 			w->limit = limit;
1301c825d4dcSJohn Baldwin 		}
1302c825d4dcSJohn Baldwin 		return (error);
1303c825d4dcSJohn Baldwin 	}
1304c825d4dcSJohn Baldwin 
1305c825d4dcSJohn Baldwin 	/*
1306c825d4dcSJohn Baldwin 	 * Find the existing resource to adjust.  Usually there is only one,
1307c825d4dcSJohn Baldwin 	 * but for an ISA-enabled bridge we might be growing the I/O window
1308c825d4dcSJohn Baldwin 	 * above 64k and need to find the existing resource that maps all
1309c825d4dcSJohn Baldwin 	 * of the area above 64k.
1310c825d4dcSJohn Baldwin 	 */
1311c825d4dcSJohn Baldwin 	for (i = 0; i < w->count; i++) {
1312c825d4dcSJohn Baldwin 		if (rman_get_end(w->res[i]) == w->limit)
1313c825d4dcSJohn Baldwin 			break;
1314c825d4dcSJohn Baldwin 	}
1315c825d4dcSJohn Baldwin 	KASSERT(i != w->count, ("did not find existing resource"));
1316c825d4dcSJohn Baldwin 	res = w->res[i];
1317c825d4dcSJohn Baldwin 
1318c825d4dcSJohn Baldwin 	/*
1319c825d4dcSJohn Baldwin 	 * Usually the resource we found should match the window's
1320c825d4dcSJohn Baldwin 	 * existing range.  The one exception is the ISA-enabled case
1321c825d4dcSJohn Baldwin 	 * mentioned above in which case the resource should start at
1322c825d4dcSJohn Baldwin 	 * 64k.
1323c825d4dcSJohn Baldwin 	 */
1324c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1325c825d4dcSJohn Baldwin 	    w->base <= 65535) {
1326c825d4dcSJohn Baldwin 		KASSERT(rman_get_start(res) == 65536,
1327c825d4dcSJohn Baldwin 		    ("existing resource mismatch"));
1328c825d4dcSJohn Baldwin 		force_64k_base = 1;
1329c825d4dcSJohn Baldwin 	} else {
1330c825d4dcSJohn Baldwin 		KASSERT(w->base == rman_get_start(res),
1331c825d4dcSJohn Baldwin 		    ("existing resource mismatch"));
1332c825d4dcSJohn Baldwin 		force_64k_base = 0;
1333c825d4dcSJohn Baldwin 	}
1334c825d4dcSJohn Baldwin 
1335c825d4dcSJohn Baldwin 	error = bus_adjust_resource(sc->dev, type, res, force_64k_base ?
1336c825d4dcSJohn Baldwin 	    rman_get_start(res) : base, limit);
1337c825d4dcSJohn Baldwin 	if (error)
1338c825d4dcSJohn Baldwin 		return (error);
1339c825d4dcSJohn Baldwin 
1340c825d4dcSJohn Baldwin 	/* Add the newly allocated region to the resource manager. */
1341c825d4dcSJohn Baldwin 	if (w->base != base) {
1342c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, base, w->base - 1);
1343c825d4dcSJohn Baldwin 		w->base = base;
1344c825d4dcSJohn Baldwin 	} else {
1345c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, w->limit + 1, limit);
1346c825d4dcSJohn Baldwin 		w->limit = limit;
1347c825d4dcSJohn Baldwin 	}
1348c825d4dcSJohn Baldwin 	if (error) {
1349c825d4dcSJohn Baldwin 		if (bootverbose)
1350c825d4dcSJohn Baldwin 			device_printf(sc->dev,
1351c825d4dcSJohn Baldwin 			    "failed to expand %s resource manager\n", w->name);
1352c825d4dcSJohn Baldwin 		(void)bus_adjust_resource(sc->dev, type, res, force_64k_base ?
1353c825d4dcSJohn Baldwin 		    rman_get_start(res) : w->base, w->limit);
1354c825d4dcSJohn Baldwin 	}
1355c825d4dcSJohn Baldwin 	return (error);
1356c825d4dcSJohn Baldwin }
1357c825d4dcSJohn Baldwin 
135883c41143SJohn Baldwin /*
135983c41143SJohn Baldwin  * Attempt to grow a window to make room for a given resource request.
136083c41143SJohn Baldwin  */
136183c41143SJohn Baldwin static int
136283c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type,
136383c41143SJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
136483c41143SJohn Baldwin {
1365a7b5acacSJohn Baldwin 	u_long align, start_free, end_free, front, back, wmask;
1366c825d4dcSJohn Baldwin 	int error;
136783c41143SJohn Baldwin 
136883c41143SJohn Baldwin 	/*
136983c41143SJohn Baldwin 	 * Clamp the desired resource range to the maximum address
137083c41143SJohn Baldwin 	 * this window supports.  Reject impossible requests.
1371c825d4dcSJohn Baldwin 	 *
1372c825d4dcSJohn Baldwin 	 * For I/O port requests behind a bridge with the ISA enable
1373c825d4dcSJohn Baldwin 	 * bit set, force large allocations to start above 64k.
137483c41143SJohn Baldwin 	 */
137583c41143SJohn Baldwin 	if (!w->valid)
137683c41143SJohn Baldwin 		return (EINVAL);
1377c825d4dcSJohn Baldwin 	if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 &&
1378c825d4dcSJohn Baldwin 	    start < 65536)
1379c825d4dcSJohn Baldwin 		start = 65536;
138083c41143SJohn Baldwin 	if (end > w->rman.rm_end)
138183c41143SJohn Baldwin 		end = w->rman.rm_end;
138283c41143SJohn Baldwin 	if (start + count - 1 > end || start + count < start)
138383c41143SJohn Baldwin 		return (EINVAL);
1384a7b5acacSJohn Baldwin 	wmask = (1ul << w->step) - 1;
138583c41143SJohn Baldwin 
138683c41143SJohn Baldwin 	/*
138783c41143SJohn Baldwin 	 * If there is no resource at all, just try to allocate enough
138883c41143SJohn Baldwin 	 * aligned space for this resource.
138983c41143SJohn Baldwin 	 */
139083c41143SJohn Baldwin 	if (w->res == NULL) {
1391c825d4dcSJohn Baldwin 		error = pcib_alloc_new_window(sc, w, type, start, end, count,
1392c825d4dcSJohn Baldwin 		    flags);
1393c825d4dcSJohn Baldwin 		if (error) {
139483c41143SJohn Baldwin 			if (bootverbose)
139583c41143SJohn Baldwin 				device_printf(sc->dev,
139683c41143SJohn Baldwin 		    "failed to allocate initial %s window (%#lx-%#lx,%#lx)\n",
139783c41143SJohn Baldwin 				    w->name, start, end, count);
139883c41143SJohn Baldwin 			return (error);
139983c41143SJohn Baldwin 		}
1400c825d4dcSJohn Baldwin 		if (bootverbose)
1401c825d4dcSJohn Baldwin 			device_printf(sc->dev,
1402c825d4dcSJohn Baldwin 			    "allocated initial %s window of %#jx-%#jx\n",
1403c825d4dcSJohn Baldwin 			    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
140483c41143SJohn Baldwin 		goto updatewin;
140583c41143SJohn Baldwin 	}
140683c41143SJohn Baldwin 
140783c41143SJohn Baldwin 	/*
140883c41143SJohn Baldwin 	 * See if growing the window would help.  Compute the minimum
140983c41143SJohn Baldwin 	 * amount of address space needed on both the front and back
141083c41143SJohn Baldwin 	 * ends of the existing window to satisfy the allocation.
141183c41143SJohn Baldwin 	 *
141283c41143SJohn Baldwin 	 * For each end, build a candidate region adjusting for the
141383c41143SJohn Baldwin 	 * required alignment, etc.  If there is a free region at the
141483c41143SJohn Baldwin 	 * edge of the window, grow from the inner edge of the free
141583c41143SJohn Baldwin 	 * region.  Otherwise grow from the window boundary.
141683c41143SJohn Baldwin 	 *
1417c825d4dcSJohn Baldwin 	 * Growing an I/O window below 64k for a bridge with the ISA
1418c825d4dcSJohn Baldwin 	 * enable bit doesn't require any special magic as the step
1419c825d4dcSJohn Baldwin 	 * size of an I/O window (1k) always includes multiple
1420c825d4dcSJohn Baldwin 	 * non-alias ranges when it is grown in either direction.
1421c825d4dcSJohn Baldwin 	 *
142283c41143SJohn Baldwin 	 * XXX: Special case: if w->res is completely empty and the
142383c41143SJohn Baldwin 	 * request size is larger than w->res, we should find the
142483c41143SJohn Baldwin 	 * optimal aligned buffer containing w->res and allocate that.
142583c41143SJohn Baldwin 	 */
142683c41143SJohn Baldwin 	if (bootverbose)
142783c41143SJohn Baldwin 		device_printf(sc->dev,
142883c41143SJohn Baldwin 		    "attempting to grow %s window for (%#lx-%#lx,%#lx)\n",
142983c41143SJohn Baldwin 		    w->name, start, end, count);
143083c41143SJohn Baldwin 	align = 1ul << RF_ALIGNMENT(flags);
1431c825d4dcSJohn Baldwin 	if (start < w->base) {
143283c41143SJohn Baldwin 		if (rman_first_free_region(&w->rman, &start_free, &end_free) !=
1433c825d4dcSJohn Baldwin 		    0 || start_free != w->base)
1434c825d4dcSJohn Baldwin 			end_free = w->base;
143583c41143SJohn Baldwin 		if (end_free > end)
1436ddac8cc9SJohn Baldwin 			end_free = end + 1;
143783c41143SJohn Baldwin 
143883c41143SJohn Baldwin 		/* Move end_free down until it is properly aligned. */
143983c41143SJohn Baldwin 		end_free &= ~(align - 1);
1440a49dcb46SJohn Baldwin 		end_free--;
1441a49dcb46SJohn Baldwin 		front = end_free - (count - 1);
144283c41143SJohn Baldwin 
144383c41143SJohn Baldwin 		/*
144483c41143SJohn Baldwin 		 * The resource would now be allocated at (front,
144583c41143SJohn Baldwin 		 * end_free).  Ensure that fits in the (start, end)
144683c41143SJohn Baldwin 		 * bounds.  end_free is checked above.  If 'front' is
144783c41143SJohn Baldwin 		 * ok, ensure it is properly aligned for this window.
144883c41143SJohn Baldwin 		 * Also check for underflow.
144983c41143SJohn Baldwin 		 */
145083c41143SJohn Baldwin 		if (front >= start && front <= end_free) {
145183c41143SJohn Baldwin 			if (bootverbose)
145283c41143SJohn Baldwin 				printf("\tfront candidate range: %#lx-%#lx\n",
145383c41143SJohn Baldwin 				    front, end_free);
1454a7b5acacSJohn Baldwin 			front &= ~wmask;
1455c825d4dcSJohn Baldwin 			front = w->base - front;
145683c41143SJohn Baldwin 		} else
145783c41143SJohn Baldwin 			front = 0;
145883c41143SJohn Baldwin 	} else
145983c41143SJohn Baldwin 		front = 0;
1460c825d4dcSJohn Baldwin 	if (end > w->limit) {
146183c41143SJohn Baldwin 		if (rman_last_free_region(&w->rman, &start_free, &end_free) !=
1462c825d4dcSJohn Baldwin 		    0 || end_free != w->limit)
1463c825d4dcSJohn Baldwin 			start_free = w->limit + 1;
146483c41143SJohn Baldwin 		if (start_free < start)
146583c41143SJohn Baldwin 			start_free = start;
146683c41143SJohn Baldwin 
146783c41143SJohn Baldwin 		/* Move start_free up until it is properly aligned. */
146883c41143SJohn Baldwin 		start_free = roundup2(start_free, align);
1469a49dcb46SJohn Baldwin 		back = start_free + count - 1;
147083c41143SJohn Baldwin 
147183c41143SJohn Baldwin 		/*
147283c41143SJohn Baldwin 		 * The resource would now be allocated at (start_free,
147383c41143SJohn Baldwin 		 * back).  Ensure that fits in the (start, end)
147483c41143SJohn Baldwin 		 * bounds.  start_free is checked above.  If 'back' is
147583c41143SJohn Baldwin 		 * ok, ensure it is properly aligned for this window.
147683c41143SJohn Baldwin 		 * Also check for overflow.
147783c41143SJohn Baldwin 		 */
147883c41143SJohn Baldwin 		if (back <= end && start_free <= back) {
147983c41143SJohn Baldwin 			if (bootverbose)
148083c41143SJohn Baldwin 				printf("\tback candidate range: %#lx-%#lx\n",
148183c41143SJohn Baldwin 				    start_free, back);
1482a7b5acacSJohn Baldwin 			back |= wmask;
1483c825d4dcSJohn Baldwin 			back -= w->limit;
148483c41143SJohn Baldwin 		} else
148583c41143SJohn Baldwin 			back = 0;
148683c41143SJohn Baldwin 	} else
148783c41143SJohn Baldwin 		back = 0;
148883c41143SJohn Baldwin 
148983c41143SJohn Baldwin 	/*
149083c41143SJohn Baldwin 	 * Try to allocate the smallest needed region first.
149183c41143SJohn Baldwin 	 * If that fails, fall back to the other region.
149283c41143SJohn Baldwin 	 */
149383c41143SJohn Baldwin 	error = ENOSPC;
149483c41143SJohn Baldwin 	while (front != 0 || back != 0) {
149583c41143SJohn Baldwin 		if (front != 0 && (front <= back || back == 0)) {
1496c825d4dcSJohn Baldwin 			error = pcib_expand_window(sc, w, type, w->base - front,
1497c825d4dcSJohn Baldwin 			    w->limit);
149883c41143SJohn Baldwin 			if (error == 0)
149983c41143SJohn Baldwin 				break;
150083c41143SJohn Baldwin 			front = 0;
150183c41143SJohn Baldwin 		} else {
1502c825d4dcSJohn Baldwin 			error = pcib_expand_window(sc, w, type, w->base,
1503c825d4dcSJohn Baldwin 			    w->limit + back);
150483c41143SJohn Baldwin 			if (error == 0)
150583c41143SJohn Baldwin 				break;
150683c41143SJohn Baldwin 			back = 0;
150783c41143SJohn Baldwin 		}
150883c41143SJohn Baldwin 	}
150983c41143SJohn Baldwin 
151083c41143SJohn Baldwin 	if (error)
151183c41143SJohn Baldwin 		return (error);
151283c41143SJohn Baldwin 	if (bootverbose)
1513c825d4dcSJohn Baldwin 		device_printf(sc->dev, "grew %s window to %#jx-%#jx\n",
1514c825d4dcSJohn Baldwin 		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
151583c41143SJohn Baldwin 
151683c41143SJohn Baldwin updatewin:
1517c825d4dcSJohn Baldwin 	/* Write the new window. */
1518a7b5acacSJohn Baldwin 	KASSERT((w->base & wmask) == 0, ("start address is not aligned"));
1519a7b5acacSJohn Baldwin 	KASSERT((w->limit & wmask) == wmask, ("end address is not aligned"));
152083c41143SJohn Baldwin 	pcib_write_windows(sc, w->mask);
152183c41143SJohn Baldwin 	return (0);
152283c41143SJohn Baldwin }
152383c41143SJohn Baldwin 
152483c41143SJohn Baldwin /*
152583c41143SJohn Baldwin  * We have to trap resource allocation requests and ensure that the bridge
152683c41143SJohn Baldwin  * is set up to, or capable of handling them.
152783c41143SJohn Baldwin  */
152883c41143SJohn Baldwin struct resource *
152983c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
153083c41143SJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
153183c41143SJohn Baldwin {
153283c41143SJohn Baldwin 	struct pcib_softc *sc;
153383c41143SJohn Baldwin 	struct resource *r;
153483c41143SJohn Baldwin 
153583c41143SJohn Baldwin 	sc = device_get_softc(dev);
153683c41143SJohn Baldwin 
153783c41143SJohn Baldwin 	/*
153883c41143SJohn Baldwin 	 * VGA resources are decoded iff the VGA enable bit is set in
153983c41143SJohn Baldwin 	 * the bridge control register.  VGA resources do not fall into
154083c41143SJohn Baldwin 	 * the resource windows and are passed up to the parent.
154183c41143SJohn Baldwin 	 */
154283c41143SJohn Baldwin 	if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) ||
154383c41143SJohn Baldwin 	    (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) {
154483c41143SJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE)
154583c41143SJohn Baldwin 			return (bus_generic_alloc_resource(dev, child, type,
154683c41143SJohn Baldwin 			    rid, start, end, count, flags));
154783c41143SJohn Baldwin 		else
154883c41143SJohn Baldwin 			return (NULL);
154983c41143SJohn Baldwin 	}
155083c41143SJohn Baldwin 
155183c41143SJohn Baldwin 	switch (type) {
15524edef187SJohn Baldwin #ifdef PCI_RES_BUS
15534edef187SJohn Baldwin 	case PCI_RES_BUS:
15544edef187SJohn Baldwin 		return (pcib_alloc_subbus(&sc->bus, child, rid, start, end,
15554edef187SJohn Baldwin 		    count, flags));
15564edef187SJohn Baldwin #endif
155783c41143SJohn Baldwin 	case SYS_RES_IOPORT:
1558c825d4dcSJohn Baldwin 		if (pcib_is_isa_range(sc, start, end, count))
1559c825d4dcSJohn Baldwin 			return (NULL);
156083c41143SJohn Baldwin 		r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start,
156183c41143SJohn Baldwin 		    end, count, flags);
1562a6c82265SMarius Strobl 		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
156383c41143SJohn Baldwin 			break;
156483c41143SJohn Baldwin 		if (pcib_grow_window(sc, &sc->io, type, start, end, count,
156583c41143SJohn Baldwin 		    flags) == 0)
156683c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->io, child, type,
156783c41143SJohn Baldwin 			    rid, start, end, count, flags);
156883c41143SJohn Baldwin 		break;
156983c41143SJohn Baldwin 	case SYS_RES_MEMORY:
157083c41143SJohn Baldwin 		/*
157183c41143SJohn Baldwin 		 * For prefetchable resources, prefer the prefetchable
157283c41143SJohn Baldwin 		 * memory window, but fall back to the regular memory
157383c41143SJohn Baldwin 		 * window if that fails.  Try both windows before
157483c41143SJohn Baldwin 		 * attempting to grow a window in case the firmware
157583c41143SJohn Baldwin 		 * has used a range in the regular memory window to
157683c41143SJohn Baldwin 		 * map a prefetchable BAR.
157783c41143SJohn Baldwin 		 */
157883c41143SJohn Baldwin 		if (flags & RF_PREFETCHABLE) {
157983c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->pmem, child, type,
158083c41143SJohn Baldwin 			    rid, start, end, count, flags);
158183c41143SJohn Baldwin 			if (r != NULL)
158283c41143SJohn Baldwin 				break;
158383c41143SJohn Baldwin 		}
158483c41143SJohn Baldwin 		r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid,
158583c41143SJohn Baldwin 		    start, end, count, flags);
1586a6c82265SMarius Strobl 		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
158783c41143SJohn Baldwin 			break;
158883c41143SJohn Baldwin 		if (flags & RF_PREFETCHABLE) {
158983c41143SJohn Baldwin 			if (pcib_grow_window(sc, &sc->pmem, type, start, end,
159083c41143SJohn Baldwin 			    count, flags) == 0) {
159183c41143SJohn Baldwin 				r = pcib_suballoc_resource(sc, &sc->pmem, child,
159283c41143SJohn Baldwin 				    type, rid, start, end, count, flags);
159383c41143SJohn Baldwin 				if (r != NULL)
159483c41143SJohn Baldwin 					break;
159583c41143SJohn Baldwin 			}
159683c41143SJohn Baldwin 		}
159783c41143SJohn Baldwin 		if (pcib_grow_window(sc, &sc->mem, type, start, end, count,
159883c41143SJohn Baldwin 		    flags & ~RF_PREFETCHABLE) == 0)
159983c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->mem, child, type,
160083c41143SJohn Baldwin 			    rid, start, end, count, flags);
160183c41143SJohn Baldwin 		break;
160283c41143SJohn Baldwin 	default:
160383c41143SJohn Baldwin 		return (bus_generic_alloc_resource(dev, child, type, rid,
160483c41143SJohn Baldwin 		    start, end, count, flags));
160583c41143SJohn Baldwin 	}
160683c41143SJohn Baldwin 
160783c41143SJohn Baldwin 	/*
160883c41143SJohn Baldwin 	 * If attempts to suballocate from the window fail but this is a
160983c41143SJohn Baldwin 	 * subtractive bridge, pass the request up the tree.
161083c41143SJohn Baldwin 	 */
161183c41143SJohn Baldwin 	if (sc->flags & PCIB_SUBTRACTIVE && r == NULL)
161283c41143SJohn Baldwin 		return (bus_generic_alloc_resource(dev, child, type, rid,
161383c41143SJohn Baldwin 		    start, end, count, flags));
161483c41143SJohn Baldwin 	return (r);
161583c41143SJohn Baldwin }
161683c41143SJohn Baldwin 
161783c41143SJohn Baldwin int
161883c41143SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
161983c41143SJohn Baldwin     u_long start, u_long end)
162083c41143SJohn Baldwin {
162183c41143SJohn Baldwin 	struct pcib_softc *sc;
162283c41143SJohn Baldwin 
162383c41143SJohn Baldwin 	sc = device_get_softc(bus);
162483c41143SJohn Baldwin 	if (pcib_is_resource_managed(sc, type, r))
162583c41143SJohn Baldwin 		return (rman_adjust_resource(r, start, end));
162683c41143SJohn Baldwin 	return (bus_generic_adjust_resource(bus, child, type, r, start, end));
162783c41143SJohn Baldwin }
162883c41143SJohn Baldwin 
162983c41143SJohn Baldwin int
163083c41143SJohn Baldwin pcib_release_resource(device_t dev, device_t child, int type, int rid,
163183c41143SJohn Baldwin     struct resource *r)
163283c41143SJohn Baldwin {
163383c41143SJohn Baldwin 	struct pcib_softc *sc;
163483c41143SJohn Baldwin 	int error;
163583c41143SJohn Baldwin 
163683c41143SJohn Baldwin 	sc = device_get_softc(dev);
163783c41143SJohn Baldwin 	if (pcib_is_resource_managed(sc, type, r)) {
163883c41143SJohn Baldwin 		if (rman_get_flags(r) & RF_ACTIVE) {
163983c41143SJohn Baldwin 			error = bus_deactivate_resource(child, type, rid, r);
164083c41143SJohn Baldwin 			if (error)
164183c41143SJohn Baldwin 				return (error);
164283c41143SJohn Baldwin 		}
164383c41143SJohn Baldwin 		return (rman_release_resource(r));
164483c41143SJohn Baldwin 	}
164583c41143SJohn Baldwin 	return (bus_generic_release_resource(dev, child, type, rid, r));
164683c41143SJohn Baldwin }
164783c41143SJohn Baldwin #else
1648bb0d0a8eSMike Smith /*
1649bb0d0a8eSMike Smith  * We have to trap resource allocation requests and ensure that the bridge
1650bb0d0a8eSMike Smith  * is set up to, or capable of handling them.
1651bb0d0a8eSMike Smith  */
16526f0d5884SJohn Baldwin struct resource *
1653bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
1654bb0d0a8eSMike Smith     u_long start, u_long end, u_long count, u_int flags)
1655bb0d0a8eSMike Smith {
1656bb0d0a8eSMike Smith 	struct pcib_softc	*sc = device_get_softc(dev);
165726043836SJohn Baldwin 	const char *name, *suffix;
1658a8b354a8SWarner Losh 	int ok;
1659bb0d0a8eSMike Smith 
1660bb0d0a8eSMike Smith 	/*
1661bb0d0a8eSMike Smith 	 * Fail the allocation for this range if it's not supported.
1662bb0d0a8eSMike Smith 	 */
166326043836SJohn Baldwin 	name = device_get_nameunit(child);
166426043836SJohn Baldwin 	if (name == NULL) {
166526043836SJohn Baldwin 		name = "";
166626043836SJohn Baldwin 		suffix = "";
166726043836SJohn Baldwin 	} else
166826043836SJohn Baldwin 		suffix = " ";
1669bb0d0a8eSMike Smith 	switch (type) {
1670bb0d0a8eSMike Smith 	case SYS_RES_IOPORT:
1671a8b354a8SWarner Losh 		ok = 0;
1672e4b59fc5SWarner Losh 		if (!pcib_is_io_open(sc))
1673e4b59fc5SWarner Losh 			break;
1674a8b354a8SWarner Losh 		ok = (start >= sc->iobase && end <= sc->iolimit);
1675d98d9b12SMarcel Moolenaar 
1676d98d9b12SMarcel Moolenaar 		/*
1677d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA I/O addresses when the
1678d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
1679d98d9b12SMarcel Moolenaar 		 */
1680d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_ioport_range(start, end))
1681d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1682d98d9b12SMarcel Moolenaar 
1683e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1684a8b354a8SWarner Losh 			if (!ok) {
168512b8c86eSWarner Losh 				if (start < sc->iobase)
168612b8c86eSWarner Losh 					start = sc->iobase;
168712b8c86eSWarner Losh 				if (end > sc->iolimit)
168812b8c86eSWarner Losh 					end = sc->iolimit;
16892daa7a07SWarner Losh 				if (start < end)
16902daa7a07SWarner Losh 					ok = 1;
1691a8b354a8SWarner Losh 			}
16921c54ff33SMatthew N. Dodd 		} else {
1693e4b59fc5SWarner Losh 			ok = 1;
16949dffe835SWarner Losh #if 0
1695795dceffSWarner Losh 			/*
1696795dceffSWarner Losh 			 * If we overlap with the subtractive range, then
1697795dceffSWarner Losh 			 * pick the upper range to use.
1698795dceffSWarner Losh 			 */
1699795dceffSWarner Losh 			if (start < sc->iolimit && end > sc->iobase)
1700795dceffSWarner Losh 				start = sc->iolimit + 1;
17019dffe835SWarner Losh #endif
170212b8c86eSWarner Losh 		}
1703a8b354a8SWarner Losh 		if (end < start) {
17042daa7a07SWarner Losh 			device_printf(dev, "ioport: end (%lx) < start (%lx)\n",
17052daa7a07SWarner Losh 			    end, start);
1706a8b354a8SWarner Losh 			start = 0;
1707a8b354a8SWarner Losh 			end = 0;
1708a8b354a8SWarner Losh 			ok = 0;
1709a8b354a8SWarner Losh 		}
1710a8b354a8SWarner Losh 		if (!ok) {
171126043836SJohn Baldwin 			device_printf(dev, "%s%srequested unsupported I/O "
1712a8b354a8SWarner Losh 			    "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
171326043836SJohn Baldwin 			    name, suffix, start, end, sc->iobase, sc->iolimit);
1714bb0d0a8eSMike Smith 			return (NULL);
1715bb0d0a8eSMike Smith 		}
17164fa59183SMike Smith 		if (bootverbose)
17172daa7a07SWarner Losh 			device_printf(dev,
171826043836SJohn Baldwin 			    "%s%srequested I/O range 0x%lx-0x%lx: in range\n",
171926043836SJohn Baldwin 			    name, suffix, start, end);
1720bb0d0a8eSMike Smith 		break;
1721bb0d0a8eSMike Smith 
1722bb0d0a8eSMike Smith 	case SYS_RES_MEMORY:
1723a8b354a8SWarner Losh 		ok = 0;
1724a8b354a8SWarner Losh 		if (pcib_is_nonprefetch_open(sc))
1725a8b354a8SWarner Losh 			ok = ok || (start >= sc->membase && end <= sc->memlimit);
1726a8b354a8SWarner Losh 		if (pcib_is_prefetch_open(sc))
1727a8b354a8SWarner Losh 			ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
1728d98d9b12SMarcel Moolenaar 
1729d98d9b12SMarcel Moolenaar 		/*
1730d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA memory addresses when the
1731d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
1732d98d9b12SMarcel Moolenaar 		 */
1733d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_memory_range(start, end))
1734d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1735d98d9b12SMarcel Moolenaar 
1736e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1737a8b354a8SWarner Losh 			if (!ok) {
1738a8b354a8SWarner Losh 				ok = 1;
1739a8b354a8SWarner Losh 				if (flags & RF_PREFETCHABLE) {
1740a8b354a8SWarner Losh 					if (pcib_is_prefetch_open(sc)) {
1741a8b354a8SWarner Losh 						if (start < sc->pmembase)
1742a8b354a8SWarner Losh 							start = sc->pmembase;
1743a8b354a8SWarner Losh 						if (end > sc->pmemlimit)
1744a8b354a8SWarner Losh 							end = sc->pmemlimit;
1745a8b354a8SWarner Losh 					} else {
1746a8b354a8SWarner Losh 						ok = 0;
1747a8b354a8SWarner Losh 					}
1748a8b354a8SWarner Losh 				} else {	/* non-prefetchable */
1749a8b354a8SWarner Losh 					if (pcib_is_nonprefetch_open(sc)) {
1750a8b354a8SWarner Losh 						if (start < sc->membase)
175112b8c86eSWarner Losh 							start = sc->membase;
175212b8c86eSWarner Losh 						if (end > sc->memlimit)
175312b8c86eSWarner Losh 							end = sc->memlimit;
17541c54ff33SMatthew N. Dodd 					} else {
1755a8b354a8SWarner Losh 						ok = 0;
1756a8b354a8SWarner Losh 					}
1757a8b354a8SWarner Losh 				}
1758a8b354a8SWarner Losh 			}
1759a8b354a8SWarner Losh 		} else if (!ok) {
1760e4b59fc5SWarner Losh 			ok = 1;	/* subtractive bridge: always ok */
17619dffe835SWarner Losh #if 0
1762a8b354a8SWarner Losh 			if (pcib_is_nonprefetch_open(sc)) {
1763795dceffSWarner Losh 				if (start < sc->memlimit && end > sc->membase)
1764795dceffSWarner Losh 					start = sc->memlimit + 1;
1765a8b354a8SWarner Losh 			}
1766a8b354a8SWarner Losh 			if (pcib_is_prefetch_open(sc)) {
1767795dceffSWarner Losh 				if (start < sc->pmemlimit && end > sc->pmembase)
1768795dceffSWarner Losh 					start = sc->pmemlimit + 1;
17691c54ff33SMatthew N. Dodd 			}
17709dffe835SWarner Losh #endif
177112b8c86eSWarner Losh 		}
1772a8b354a8SWarner Losh 		if (end < start) {
17732daa7a07SWarner Losh 			device_printf(dev, "memory: end (%lx) < start (%lx)\n",
17742daa7a07SWarner Losh 			    end, start);
1775a8b354a8SWarner Losh 			start = 0;
1776a8b354a8SWarner Losh 			end = 0;
1777a8b354a8SWarner Losh 			ok = 0;
1778a8b354a8SWarner Losh 		}
1779a8b354a8SWarner Losh 		if (!ok && bootverbose)
178034428485SWarner Losh 			device_printf(dev,
178126043836SJohn Baldwin 			    "%s%srequested unsupported memory range %#lx-%#lx "
1782b0a2d4b8SWarner Losh 			    "(decoding %#jx-%#jx, %#jx-%#jx)\n",
178326043836SJohn Baldwin 			    name, suffix, start, end,
1784b0a2d4b8SWarner Losh 			    (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
1785b0a2d4b8SWarner Losh 			    (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
1786a8b354a8SWarner Losh 		if (!ok)
1787bb0d0a8eSMike Smith 			return (NULL);
17884fa59183SMike Smith 		if (bootverbose)
178926043836SJohn Baldwin 			device_printf(dev,"%s%srequested memory range "
17902daa7a07SWarner Losh 			    "0x%lx-0x%lx: good\n",
179126043836SJohn Baldwin 			    name, suffix, start, end);
17924fa59183SMike Smith 		break;
17934fa59183SMike Smith 
1794bb0d0a8eSMike Smith 	default:
17954fa59183SMike Smith 		break;
1796bb0d0a8eSMike Smith 	}
1797bb0d0a8eSMike Smith 	/*
1798bb0d0a8eSMike Smith 	 * Bridge is OK decoding this resource, so pass it up.
1799bb0d0a8eSMike Smith 	 */
18002daa7a07SWarner Losh 	return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
18012daa7a07SWarner Losh 	    count, flags));
1802bb0d0a8eSMike Smith }
180383c41143SJohn Baldwin #endif
1804bb0d0a8eSMike Smith 
1805bb0d0a8eSMike Smith /*
180655d3ea17SRyan Stone  * If ARI is enabled on this downstream port, translate the function number
180755d3ea17SRyan Stone  * to the non-ARI slot/function.  The downstream port will convert it back in
180855d3ea17SRyan Stone  * hardware.  If ARI is not enabled slot and func are not modified.
180955d3ea17SRyan Stone  */
181055d3ea17SRyan Stone static __inline void
181155d3ea17SRyan Stone pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func)
181255d3ea17SRyan Stone {
181355d3ea17SRyan Stone 	struct pcib_softc *sc;
181455d3ea17SRyan Stone 	int ari_func;
181555d3ea17SRyan Stone 
181655d3ea17SRyan Stone 	sc = device_get_softc(pcib);
181755d3ea17SRyan Stone 	ari_func = *func;
181855d3ea17SRyan Stone 
181955d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
182055d3ea17SRyan Stone 		KASSERT(*slot == 0,
182155d3ea17SRyan Stone 		    ("Non-zero slot number with ARI enabled!"));
182255d3ea17SRyan Stone 		*slot = PCIE_ARI_SLOT(ari_func);
182355d3ea17SRyan Stone 		*func = PCIE_ARI_FUNC(ari_func);
182455d3ea17SRyan Stone 	}
182555d3ea17SRyan Stone }
182655d3ea17SRyan Stone 
182755d3ea17SRyan Stone 
182855d3ea17SRyan Stone static void
182955d3ea17SRyan Stone pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos)
183055d3ea17SRyan Stone {
183155d3ea17SRyan Stone 	uint32_t ctl2;
183255d3ea17SRyan Stone 
183355d3ea17SRyan Stone 	ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4);
183455d3ea17SRyan Stone 	ctl2 |= PCIEM_CTL2_ARI;
183555d3ea17SRyan Stone 	pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4);
183655d3ea17SRyan Stone 
183755d3ea17SRyan Stone 	sc->flags |= PCIB_ENABLE_ARI;
183855d3ea17SRyan Stone }
183955d3ea17SRyan Stone 
184055d3ea17SRyan Stone /*
1841bb0d0a8eSMike Smith  * PCIB interface.
1842bb0d0a8eSMike Smith  */
18436f0d5884SJohn Baldwin int
1844bb0d0a8eSMike Smith pcib_maxslots(device_t dev)
1845bb0d0a8eSMike Smith {
18464fa59183SMike Smith 	return (PCI_SLOTMAX);
1847bb0d0a8eSMike Smith }
1848bb0d0a8eSMike Smith 
184955d3ea17SRyan Stone static int
185055d3ea17SRyan Stone pcib_ari_maxslots(device_t dev)
185155d3ea17SRyan Stone {
185255d3ea17SRyan Stone 	struct pcib_softc *sc;
185355d3ea17SRyan Stone 
185455d3ea17SRyan Stone 	sc = device_get_softc(dev);
185555d3ea17SRyan Stone 
185655d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI)
185755d3ea17SRyan Stone 		return (PCIE_ARI_SLOTMAX);
185855d3ea17SRyan Stone 	else
185955d3ea17SRyan Stone 		return (PCI_SLOTMAX);
186055d3ea17SRyan Stone }
186155d3ea17SRyan Stone 
186255d3ea17SRyan Stone static int
186355d3ea17SRyan Stone pcib_ari_maxfuncs(device_t dev)
186455d3ea17SRyan Stone {
186555d3ea17SRyan Stone 	struct pcib_softc *sc;
186655d3ea17SRyan Stone 
186755d3ea17SRyan Stone 	sc = device_get_softc(dev);
186855d3ea17SRyan Stone 
186955d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI)
187055d3ea17SRyan Stone 		return (PCIE_ARI_FUNCMAX);
187155d3ea17SRyan Stone 	else
187255d3ea17SRyan Stone 		return (PCI_FUNCMAX);
187355d3ea17SRyan Stone }
187455d3ea17SRyan Stone 
18752397d2d8SRyan Stone static void
18762397d2d8SRyan Stone pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot,
18772397d2d8SRyan Stone     int *func)
18782397d2d8SRyan Stone {
18792397d2d8SRyan Stone 	struct pcib_softc *sc;
18802397d2d8SRyan Stone 
18812397d2d8SRyan Stone 	sc = device_get_softc(pcib);
18822397d2d8SRyan Stone 
18832397d2d8SRyan Stone 	*bus = PCI_RID2BUS(rid);
18842397d2d8SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
18852397d2d8SRyan Stone 		*slot = PCIE_ARI_RID2SLOT(rid);
18862397d2d8SRyan Stone 		*func = PCIE_ARI_RID2FUNC(rid);
18872397d2d8SRyan Stone 	} else {
18882397d2d8SRyan Stone 		*slot = PCI_RID2SLOT(rid);
18892397d2d8SRyan Stone 		*func = PCI_RID2FUNC(rid);
18902397d2d8SRyan Stone 	}
18912397d2d8SRyan Stone }
18922397d2d8SRyan Stone 
1893bb0d0a8eSMike Smith /*
1894bb0d0a8eSMike Smith  * Since we are a child of a PCI bus, its parent must support the pcib interface.
1895bb0d0a8eSMike Smith  */
189655d3ea17SRyan Stone static uint32_t
1897795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
1898bb0d0a8eSMike Smith {
189955d3ea17SRyan Stone 
190055d3ea17SRyan Stone 	pcib_xlate_ari(dev, b, &s, &f);
190155d3ea17SRyan Stone 	return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s,
190255d3ea17SRyan Stone 	    f, reg, width));
1903bb0d0a8eSMike Smith }
1904bb0d0a8eSMike Smith 
190555d3ea17SRyan Stone static void
1906795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
1907bb0d0a8eSMike Smith {
190855d3ea17SRyan Stone 
190955d3ea17SRyan Stone 	pcib_xlate_ari(dev, b, &s, &f);
191055d3ea17SRyan Stone 	PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f,
191155d3ea17SRyan Stone 	    reg, val, width);
1912bb0d0a8eSMike Smith }
1913bb0d0a8eSMike Smith 
1914bb0d0a8eSMike Smith /*
1915bb0d0a8eSMike Smith  * Route an interrupt across a PCI bridge.
1916bb0d0a8eSMike Smith  */
19172c2d1d07SBenno Rice int
1918bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin)
1919bb0d0a8eSMike Smith {
1920bb0d0a8eSMike Smith     device_t	bus;
1921bb0d0a8eSMike Smith     int		parent_intpin;
1922bb0d0a8eSMike Smith     int		intnum;
1923bb0d0a8eSMike Smith 
1924bb0d0a8eSMike Smith     /*
1925bb0d0a8eSMike Smith      *
1926bb0d0a8eSMike Smith      * The PCI standard defines a swizzle of the child-side device/intpin to
1927bb0d0a8eSMike Smith      * the parent-side intpin as follows.
1928bb0d0a8eSMike Smith      *
1929bb0d0a8eSMike Smith      * device = device on child bus
1930bb0d0a8eSMike Smith      * child_intpin = intpin on child bus slot (0-3)
1931bb0d0a8eSMike Smith      * parent_intpin = intpin on parent bus slot (0-3)
1932bb0d0a8eSMike Smith      *
1933bb0d0a8eSMike Smith      * parent_intpin = (device + child_intpin) % 4
1934bb0d0a8eSMike Smith      */
1935cdc95e1bSBernd Walter     parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
1936bb0d0a8eSMike Smith 
1937bb0d0a8eSMike Smith     /*
1938bb0d0a8eSMike Smith      * Our parent is a PCI bus.  Its parent must export the pcib interface
1939bb0d0a8eSMike Smith      * which includes the ability to route interrupts.
1940bb0d0a8eSMike Smith      */
1941bb0d0a8eSMike Smith     bus = device_get_parent(pcib);
1942bb0d0a8eSMike Smith     intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
194339981fedSJohn Baldwin     if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
1944c6a121abSJohn Baldwin 	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
1945c6a121abSJohn Baldwin 	    pci_get_slot(dev), 'A' + pin - 1, intnum);
19468046c4b9SMike Smith     }
1947bb0d0a8eSMike Smith     return(intnum);
1948bb0d0a8eSMike Smith }
1949b173edafSJohn Baldwin 
1950e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
19519bf4c9c1SJohn Baldwin int
19529bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
19539bf4c9c1SJohn Baldwin {
1954bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
19559bf4c9c1SJohn Baldwin 	device_t bus;
19569bf4c9c1SJohn Baldwin 
195722bf1c7fSJohn Baldwin 	if (sc->flags & PCIB_DISABLE_MSI)
195822bf1c7fSJohn Baldwin 		return (ENXIO);
19599bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
19609bf4c9c1SJohn Baldwin 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
19619bf4c9c1SJohn Baldwin 	    irqs));
19629bf4c9c1SJohn Baldwin }
19639bf4c9c1SJohn Baldwin 
1964e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */
19659bf4c9c1SJohn Baldwin int
19669bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
19679bf4c9c1SJohn Baldwin {
19689bf4c9c1SJohn Baldwin 	device_t bus;
19699bf4c9c1SJohn Baldwin 
19709bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
19719bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
19729bf4c9c1SJohn Baldwin }
19739bf4c9c1SJohn Baldwin 
19749bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */
19759bf4c9c1SJohn Baldwin int
1976e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
19779bf4c9c1SJohn Baldwin {
1978bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
19799bf4c9c1SJohn Baldwin 	device_t bus;
19809bf4c9c1SJohn Baldwin 
198168e9cbd3SMarius Strobl 	if (sc->flags & PCIB_DISABLE_MSIX)
198222bf1c7fSJohn Baldwin 		return (ENXIO);
19839bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
1984e706f7f0SJohn Baldwin 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
19855fe82bcaSJohn Baldwin }
19865fe82bcaSJohn Baldwin 
19879bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */
19889bf4c9c1SJohn Baldwin int
19899bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq)
19909bf4c9c1SJohn Baldwin {
19919bf4c9c1SJohn Baldwin 	device_t bus;
19929bf4c9c1SJohn Baldwin 
19939bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
19949bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
19959bf4c9c1SJohn Baldwin }
19969bf4c9c1SJohn Baldwin 
1997e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */
1998e706f7f0SJohn Baldwin int
1999e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
2000e706f7f0SJohn Baldwin     uint32_t *data)
2001e706f7f0SJohn Baldwin {
2002e706f7f0SJohn Baldwin 	device_t bus;
20034522ac77SLuoqi Chen 	int error;
2004e706f7f0SJohn Baldwin 
2005e706f7f0SJohn Baldwin 	bus = device_get_parent(pcib);
20064522ac77SLuoqi Chen 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
20074522ac77SLuoqi Chen 	if (error)
20084522ac77SLuoqi Chen 		return (error);
20094522ac77SLuoqi Chen 
20104522ac77SLuoqi Chen 	pci_ht_map_msi(pcib, *addr);
20114522ac77SLuoqi Chen 	return (0);
2012e706f7f0SJohn Baldwin }
2013e706f7f0SJohn Baldwin 
201462508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */
201562508c53SJohn Baldwin int
201662508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
201762508c53SJohn Baldwin {
201862508c53SJohn Baldwin 	device_t bus;
201962508c53SJohn Baldwin 
202062508c53SJohn Baldwin 	bus = device_get_parent(pcib);
202162508c53SJohn Baldwin 	return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate));
202262508c53SJohn Baldwin }
20235605a99eSRyan Stone 
20242397d2d8SRyan Stone static int
20252397d2d8SRyan Stone pcib_ari_enabled(device_t pcib)
20262397d2d8SRyan Stone {
20272397d2d8SRyan Stone 	struct pcib_softc *sc;
20282397d2d8SRyan Stone 
20292397d2d8SRyan Stone 	sc = device_get_softc(pcib);
20302397d2d8SRyan Stone 
20312397d2d8SRyan Stone 	return ((sc->flags & PCIB_ENABLE_ARI) != 0);
20322397d2d8SRyan Stone }
20332397d2d8SRyan Stone 
203455d3ea17SRyan Stone static uint16_t
203555d3ea17SRyan Stone pcib_ari_get_rid(device_t pcib, device_t dev)
203655d3ea17SRyan Stone {
203755d3ea17SRyan Stone 	struct pcib_softc *sc;
203855d3ea17SRyan Stone 	uint8_t bus, slot, func;
203955d3ea17SRyan Stone 
204055d3ea17SRyan Stone 	sc = device_get_softc(pcib);
204155d3ea17SRyan Stone 
204255d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
204355d3ea17SRyan Stone 		bus = pci_get_bus(dev);
204455d3ea17SRyan Stone 		func = pci_get_function(dev);
204555d3ea17SRyan Stone 
204655d3ea17SRyan Stone 		return (PCI_ARI_RID(bus, func));
204755d3ea17SRyan Stone 	} else {
204855d3ea17SRyan Stone 		bus = pci_get_bus(dev);
204955d3ea17SRyan Stone 		slot = pci_get_slot(dev);
205055d3ea17SRyan Stone 		func = pci_get_function(dev);
205155d3ea17SRyan Stone 
205255d3ea17SRyan Stone 		return (PCI_RID(bus, slot, func));
205355d3ea17SRyan Stone 	}
205455d3ea17SRyan Stone }
205555d3ea17SRyan Stone 
205655d3ea17SRyan Stone /*
205755d3ea17SRyan Stone  * Check that the downstream port (pcib) and the endpoint device (dev) both
205855d3ea17SRyan Stone  * support ARI.  If so, enable it and return 0, otherwise return an error.
205955d3ea17SRyan Stone  */
206055d3ea17SRyan Stone static int
206155d3ea17SRyan Stone pcib_try_enable_ari(device_t pcib, device_t dev)
206255d3ea17SRyan Stone {
206355d3ea17SRyan Stone 	struct pcib_softc *sc;
206455d3ea17SRyan Stone 	int error;
206555d3ea17SRyan Stone 	uint32_t cap2;
206655d3ea17SRyan Stone 	int ari_cap_off;
206755d3ea17SRyan Stone 	uint32_t ari_ver;
206855d3ea17SRyan Stone 	uint32_t pcie_pos;
206955d3ea17SRyan Stone 
207055d3ea17SRyan Stone 	sc = device_get_softc(pcib);
207155d3ea17SRyan Stone 
207255d3ea17SRyan Stone 	/*
207355d3ea17SRyan Stone 	 * ARI is controlled in a register in the PCIe capability structure.
207455d3ea17SRyan Stone 	 * If the downstream port does not have the PCIe capability structure
207555d3ea17SRyan Stone 	 * then it does not support ARI.
207655d3ea17SRyan Stone 	 */
207755d3ea17SRyan Stone 	error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos);
207855d3ea17SRyan Stone 	if (error != 0)
207955d3ea17SRyan Stone 		return (ENODEV);
208055d3ea17SRyan Stone 
208155d3ea17SRyan Stone 	/* Check that the PCIe port advertises ARI support. */
208255d3ea17SRyan Stone 	cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4);
208355d3ea17SRyan Stone 	if (!(cap2 & PCIEM_CAP2_ARI))
208455d3ea17SRyan Stone 		return (ENODEV);
208555d3ea17SRyan Stone 
208655d3ea17SRyan Stone 	/*
208755d3ea17SRyan Stone 	 * Check that the endpoint device advertises ARI support via the ARI
208855d3ea17SRyan Stone 	 * extended capability structure.
208955d3ea17SRyan Stone 	 */
209055d3ea17SRyan Stone 	error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off);
209155d3ea17SRyan Stone 	if (error != 0)
209255d3ea17SRyan Stone 		return (ENODEV);
209355d3ea17SRyan Stone 
209455d3ea17SRyan Stone 	/*
209555d3ea17SRyan Stone 	 * Finally, check that the endpoint device supports the same version
209655d3ea17SRyan Stone 	 * of ARI that we do.
209755d3ea17SRyan Stone 	 */
209855d3ea17SRyan Stone 	ari_ver = pci_read_config(dev, ari_cap_off, 4);
209955d3ea17SRyan Stone 	if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) {
210055d3ea17SRyan Stone 		if (bootverbose)
210155d3ea17SRyan Stone 			device_printf(pcib,
210255d3ea17SRyan Stone 			    "Unsupported version of ARI (%d) detected\n",
210355d3ea17SRyan Stone 			    PCI_EXTCAP_VER(ari_ver));
210455d3ea17SRyan Stone 
210555d3ea17SRyan Stone 		return (ENXIO);
210655d3ea17SRyan Stone 	}
210755d3ea17SRyan Stone 
210855d3ea17SRyan Stone 	pcib_enable_ari(sc, pcie_pos);
210955d3ea17SRyan Stone 
211055d3ea17SRyan Stone 	return (0);
211155d3ea17SRyan Stone }
211255d3ea17SRyan Stone 
2113