xref: /freebsd/sys/dev/pci/pci_pci.c (revision 6f33eaa5f0cecb963ddce2095d365ede3c4c2980)
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 
3882cb5c3bSJohn Baldwin #include "opt_pci.h"
3982cb5c3bSJohn Baldwin 
40bb0d0a8eSMike Smith #include <sys/param.h>
41bb0d0a8eSMike Smith #include <sys/bus.h>
4283c41143SJohn Baldwin #include <sys/kernel.h>
4383c41143SJohn Baldwin #include <sys/malloc.h>
4483c41143SJohn Baldwin #include <sys/module.h>
45a8b354a8SWarner Losh #include <sys/rman.h>
461c54ff33SMatthew N. Dodd #include <sys/sysctl.h>
4783c41143SJohn Baldwin #include <sys/systm.h>
4882cb5c3bSJohn Baldwin #include <sys/taskqueue.h>
49bb0d0a8eSMike Smith 
5038d8c994SWarner Losh #include <dev/pci/pcivar.h>
5138d8c994SWarner Losh #include <dev/pci/pcireg.h>
5262508c53SJohn Baldwin #include <dev/pci/pci_private.h>
5338d8c994SWarner Losh #include <dev/pci/pcib_private.h>
54bb0d0a8eSMike Smith 
55bb0d0a8eSMike Smith #include "pcib_if.h"
56bb0d0a8eSMike Smith 
57bb0d0a8eSMike Smith static int		pcib_probe(device_t dev);
58e36af292SJung-uk Kim static int		pcib_suspend(device_t dev);
59e36af292SJung-uk Kim static int		pcib_resume(device_t dev);
6062508c53SJohn Baldwin static int		pcib_power_for_sleep(device_t pcib, device_t dev,
6162508c53SJohn Baldwin 			    int *pstate);
62d7be980dSAndrew Turner static int		pcib_ari_get_id(device_t pcib, device_t dev,
63d7be980dSAndrew Turner     enum pci_id_type type, uintptr_t *id);
6455d3ea17SRyan Stone static uint32_t		pcib_read_config(device_t dev, u_int b, u_int s,
6555d3ea17SRyan Stone     u_int f, u_int reg, int width);
6655d3ea17SRyan Stone static void		pcib_write_config(device_t dev, u_int b, u_int s,
6755d3ea17SRyan Stone     u_int f, u_int reg, uint32_t val, int width);
6855d3ea17SRyan Stone static int		pcib_ari_maxslots(device_t dev);
6955d3ea17SRyan Stone static int		pcib_ari_maxfuncs(device_t dev);
7055d3ea17SRyan Stone static int		pcib_try_enable_ari(device_t pcib, device_t dev);
712397d2d8SRyan Stone static int		pcib_ari_enabled(device_t pcib);
722397d2d8SRyan Stone static void		pcib_ari_decode_rid(device_t pcib, uint16_t rid,
732397d2d8SRyan Stone 			    int *bus, int *slot, int *func);
7482cb5c3bSJohn Baldwin #ifdef PCI_HP
7582cb5c3bSJohn Baldwin static void		pcib_pcie_ab_timeout(void *arg);
7682cb5c3bSJohn Baldwin static void		pcib_pcie_cc_timeout(void *arg);
7782cb5c3bSJohn Baldwin static void		pcib_pcie_dll_timeout(void *arg);
7882cb5c3bSJohn Baldwin #endif
79bb0d0a8eSMike Smith 
80bb0d0a8eSMike Smith static device_method_t pcib_methods[] = {
81bb0d0a8eSMike Smith     /* Device interface */
82bb0d0a8eSMike Smith     DEVMETHOD(device_probe,		pcib_probe),
83bb0d0a8eSMike Smith     DEVMETHOD(device_attach,		pcib_attach),
84*6f33eaa5SJohn Baldwin     DEVMETHOD(device_detach,		pcib_detach),
85bb0d0a8eSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
86e36af292SJung-uk Kim     DEVMETHOD(device_suspend,		pcib_suspend),
87e36af292SJung-uk Kim     DEVMETHOD(device_resume,		pcib_resume),
88bb0d0a8eSMike Smith 
89bb0d0a8eSMike Smith     /* Bus interface */
9082cb5c3bSJohn Baldwin     DEVMETHOD(bus_child_present,	pcib_child_present),
91bb0d0a8eSMike Smith     DEVMETHOD(bus_read_ivar,		pcib_read_ivar),
92bb0d0a8eSMike Smith     DEVMETHOD(bus_write_ivar,		pcib_write_ivar),
93bb0d0a8eSMike Smith     DEVMETHOD(bus_alloc_resource,	pcib_alloc_resource),
9483c41143SJohn Baldwin #ifdef NEW_PCIB
9583c41143SJohn Baldwin     DEVMETHOD(bus_adjust_resource,	pcib_adjust_resource),
9683c41143SJohn Baldwin     DEVMETHOD(bus_release_resource,	pcib_release_resource),
9783c41143SJohn Baldwin #else
98d2c9344fSJohn Baldwin     DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
99bb0d0a8eSMike Smith     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
10083c41143SJohn Baldwin #endif
101bb0d0a8eSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
102bb0d0a8eSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
103bb0d0a8eSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
104bb0d0a8eSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
105bb0d0a8eSMike Smith 
106bb0d0a8eSMike Smith     /* pcib interface */
10755d3ea17SRyan Stone     DEVMETHOD(pcib_maxslots,		pcib_ari_maxslots),
10855d3ea17SRyan Stone     DEVMETHOD(pcib_maxfuncs,		pcib_ari_maxfuncs),
109bb0d0a8eSMike Smith     DEVMETHOD(pcib_read_config,		pcib_read_config),
110bb0d0a8eSMike Smith     DEVMETHOD(pcib_write_config,	pcib_write_config),
111bb0d0a8eSMike Smith     DEVMETHOD(pcib_route_interrupt,	pcib_route_interrupt),
1129bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_alloc_msi,		pcib_alloc_msi),
1139bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msi,		pcib_release_msi),
1149bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_alloc_msix,		pcib_alloc_msix),
1159bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msix,	pcib_release_msix),
116e706f7f0SJohn Baldwin     DEVMETHOD(pcib_map_msi,		pcib_map_msi),
11762508c53SJohn Baldwin     DEVMETHOD(pcib_power_for_sleep,	pcib_power_for_sleep),
118d7be980dSAndrew Turner     DEVMETHOD(pcib_get_id,		pcib_ari_get_id),
11955d3ea17SRyan Stone     DEVMETHOD(pcib_try_enable_ari,	pcib_try_enable_ari),
1202397d2d8SRyan Stone     DEVMETHOD(pcib_ari_enabled,		pcib_ari_enabled),
1212397d2d8SRyan Stone     DEVMETHOD(pcib_decode_rid,		pcib_ari_decode_rid),
122bb0d0a8eSMike Smith 
1234b7ec270SMarius Strobl     DEVMETHOD_END
124bb0d0a8eSMike Smith };
125bb0d0a8eSMike Smith 
12604dda605SJohn Baldwin static devclass_t pcib_devclass;
127bb0d0a8eSMike Smith 
12804dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc));
12968e9cbd3SMarius Strobl DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, NULL, NULL);
130bb0d0a8eSMike Smith 
13183c41143SJohn Baldwin #ifdef NEW_PCIB
1320070c94bSJohn Baldwin SYSCTL_DECL(_hw_pci);
1330070c94bSJohn Baldwin 
1340070c94bSJohn Baldwin static int pci_clear_pcib;
1350070c94bSJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0,
1360070c94bSJohn Baldwin     "Clear firmware-assigned resources for PCI-PCI bridge I/O windows.");
13783c41143SJohn Baldwin 
13883c41143SJohn Baldwin /*
13983c41143SJohn Baldwin  * Is a resource from a child device sub-allocated from one of our
14083c41143SJohn Baldwin  * resource managers?
14183c41143SJohn Baldwin  */
14283c41143SJohn Baldwin static int
14383c41143SJohn Baldwin pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r)
14483c41143SJohn Baldwin {
14583c41143SJohn Baldwin 
14683c41143SJohn Baldwin 	switch (type) {
1474edef187SJohn Baldwin #ifdef PCI_RES_BUS
1484edef187SJohn Baldwin 	case PCI_RES_BUS:
1494edef187SJohn Baldwin 		return (rman_is_region_manager(r, &sc->bus.rman));
1504edef187SJohn Baldwin #endif
15183c41143SJohn Baldwin 	case SYS_RES_IOPORT:
15283c41143SJohn Baldwin 		return (rman_is_region_manager(r, &sc->io.rman));
15383c41143SJohn Baldwin 	case SYS_RES_MEMORY:
15483c41143SJohn Baldwin 		/* Prefetchable resources may live in either memory rman. */
15583c41143SJohn Baldwin 		if (rman_get_flags(r) & RF_PREFETCHABLE &&
15683c41143SJohn Baldwin 		    rman_is_region_manager(r, &sc->pmem.rman))
15783c41143SJohn Baldwin 			return (1);
15883c41143SJohn Baldwin 		return (rman_is_region_manager(r, &sc->mem.rman));
15983c41143SJohn Baldwin 	}
16083c41143SJohn Baldwin 	return (0);
16183c41143SJohn Baldwin }
16283c41143SJohn Baldwin 
16383c41143SJohn Baldwin static int
16483c41143SJohn Baldwin pcib_is_window_open(struct pcib_window *pw)
16583c41143SJohn Baldwin {
16683c41143SJohn Baldwin 
16783c41143SJohn Baldwin 	return (pw->valid && pw->base < pw->limit);
16883c41143SJohn Baldwin }
16983c41143SJohn Baldwin 
17083c41143SJohn Baldwin /*
17183c41143SJohn Baldwin  * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and
17283c41143SJohn Baldwin  * handle for the resource, we could pass RF_ACTIVE up to the PCI bus
17383c41143SJohn Baldwin  * when allocating the resource windows and rely on the PCI bus driver
17483c41143SJohn Baldwin  * to do this for us.
17583c41143SJohn Baldwin  */
17683c41143SJohn Baldwin static void
17783c41143SJohn Baldwin pcib_activate_window(struct pcib_softc *sc, int type)
17883c41143SJohn Baldwin {
17983c41143SJohn Baldwin 
18083c41143SJohn Baldwin 	PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type);
18183c41143SJohn Baldwin }
18283c41143SJohn Baldwin 
18383c41143SJohn Baldwin static void
18483c41143SJohn Baldwin pcib_write_windows(struct pcib_softc *sc, int mask)
18583c41143SJohn Baldwin {
18683c41143SJohn Baldwin 	device_t dev;
18783c41143SJohn Baldwin 	uint32_t val;
18883c41143SJohn Baldwin 
18983c41143SJohn Baldwin 	dev = sc->dev;
19083c41143SJohn Baldwin 	if (sc->io.valid && mask & WIN_IO) {
19183c41143SJohn Baldwin 		val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
19283c41143SJohn Baldwin 		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
19383c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOBASEH_1,
19483c41143SJohn Baldwin 			    sc->io.base >> 16, 2);
19583c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOLIMITH_1,
19683c41143SJohn Baldwin 			    sc->io.limit >> 16, 2);
19783c41143SJohn Baldwin 		}
19883c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1);
19983c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1);
20083c41143SJohn Baldwin 	}
20183c41143SJohn Baldwin 
20283c41143SJohn Baldwin 	if (mask & WIN_MEM) {
20383c41143SJohn Baldwin 		pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2);
20483c41143SJohn Baldwin 		pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2);
20583c41143SJohn Baldwin 	}
20683c41143SJohn Baldwin 
20783c41143SJohn Baldwin 	if (sc->pmem.valid && mask & WIN_PMEM) {
20883c41143SJohn Baldwin 		val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
20983c41143SJohn Baldwin 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
21083c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMBASEH_1,
21183c41143SJohn Baldwin 			    sc->pmem.base >> 32, 4);
21283c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMLIMITH_1,
21383c41143SJohn Baldwin 			    sc->pmem.limit >> 32, 4);
21483c41143SJohn Baldwin 		}
21583c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2);
21683c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2);
21783c41143SJohn Baldwin 	}
21883c41143SJohn Baldwin }
21983c41143SJohn Baldwin 
220c825d4dcSJohn Baldwin /*
221c825d4dcSJohn Baldwin  * This is used to reject I/O port allocations that conflict with an
222c825d4dcSJohn Baldwin  * ISA alias range.
223c825d4dcSJohn Baldwin  */
224c825d4dcSJohn Baldwin static int
2252dd1bdf1SJustin Hibbits pcib_is_isa_range(struct pcib_softc *sc, rman_res_t start, rman_res_t end,
2262dd1bdf1SJustin Hibbits     rman_res_t count)
227c825d4dcSJohn Baldwin {
2282dd1bdf1SJustin Hibbits 	rman_res_t next_alias;
229c825d4dcSJohn Baldwin 
230c825d4dcSJohn Baldwin 	if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE))
231c825d4dcSJohn Baldwin 		return (0);
232c825d4dcSJohn Baldwin 
233c825d4dcSJohn Baldwin 	/* Only check fixed ranges for overlap. */
234c825d4dcSJohn Baldwin 	if (start + count - 1 != end)
235c825d4dcSJohn Baldwin 		return (0);
236c825d4dcSJohn Baldwin 
237c825d4dcSJohn Baldwin 	/* ISA aliases are only in the lower 64KB of I/O space. */
238c825d4dcSJohn Baldwin 	if (start >= 65536)
239c825d4dcSJohn Baldwin 		return (0);
240c825d4dcSJohn Baldwin 
241c825d4dcSJohn Baldwin 	/* Check for overlap with 0x000 - 0x0ff as a special case. */
242c825d4dcSJohn Baldwin 	if (start < 0x100)
243c825d4dcSJohn Baldwin 		goto alias;
244c825d4dcSJohn Baldwin 
245c825d4dcSJohn Baldwin 	/*
246c825d4dcSJohn Baldwin 	 * If the start address is an alias, the range is an alias.
247c825d4dcSJohn Baldwin 	 * Otherwise, compute the start of the next alias range and
248c825d4dcSJohn Baldwin 	 * check if it is before the end of the candidate range.
249c825d4dcSJohn Baldwin 	 */
250c825d4dcSJohn Baldwin 	if ((start & 0x300) != 0)
251c825d4dcSJohn Baldwin 		goto alias;
252c825d4dcSJohn Baldwin 	next_alias = (start & ~0x3fful) | 0x100;
253c825d4dcSJohn Baldwin 	if (next_alias <= end)
254c825d4dcSJohn Baldwin 		goto alias;
255c825d4dcSJohn Baldwin 	return (0);
256c825d4dcSJohn Baldwin 
257c825d4dcSJohn Baldwin alias:
258c825d4dcSJohn Baldwin 	if (bootverbose)
259c825d4dcSJohn Baldwin 		device_printf(sc->dev,
260da1b038aSJustin Hibbits 		    "I/O range %#jx-%#jx overlaps with an ISA alias\n", start,
261c825d4dcSJohn Baldwin 		    end);
262c825d4dcSJohn Baldwin 	return (1);
263c825d4dcSJohn Baldwin }
264c825d4dcSJohn Baldwin 
265c825d4dcSJohn Baldwin static void
266c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res,
267c825d4dcSJohn Baldwin     int count)
268c825d4dcSJohn Baldwin {
269c825d4dcSJohn Baldwin 	struct resource **newarray;
270c825d4dcSJohn Baldwin 	int error, i;
271c825d4dcSJohn Baldwin 
272c825d4dcSJohn Baldwin 	newarray = malloc(sizeof(struct resource *) * (w->count + count),
273c825d4dcSJohn Baldwin 	    M_DEVBUF, M_WAITOK);
274c825d4dcSJohn Baldwin 	if (w->res != NULL)
275c825d4dcSJohn Baldwin 		bcopy(w->res, newarray, sizeof(struct resource *) * w->count);
276c825d4dcSJohn Baldwin 	bcopy(res, newarray + w->count, sizeof(struct resource *) * count);
277c825d4dcSJohn Baldwin 	free(w->res, M_DEVBUF);
278c825d4dcSJohn Baldwin 	w->res = newarray;
279c825d4dcSJohn Baldwin 	w->count += count;
280c825d4dcSJohn Baldwin 
281c825d4dcSJohn Baldwin 	for (i = 0; i < count; i++) {
282c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, rman_get_start(res[i]),
283c825d4dcSJohn Baldwin 		    rman_get_end(res[i]));
284c825d4dcSJohn Baldwin 		if (error)
285c825d4dcSJohn Baldwin 			panic("Failed to add resource to rman");
286c825d4dcSJohn Baldwin 	}
287c825d4dcSJohn Baldwin }
288c825d4dcSJohn Baldwin 
2892dd1bdf1SJustin Hibbits typedef void (nonisa_callback)(rman_res_t start, rman_res_t end, void *arg);
290c825d4dcSJohn Baldwin 
291c825d4dcSJohn Baldwin static void
2922dd1bdf1SJustin Hibbits pcib_walk_nonisa_ranges(rman_res_t start, rman_res_t end, nonisa_callback *cb,
293c825d4dcSJohn Baldwin     void *arg)
294c825d4dcSJohn Baldwin {
2952dd1bdf1SJustin Hibbits 	rman_res_t next_end;
296c825d4dcSJohn Baldwin 
297c825d4dcSJohn Baldwin 	/*
298c825d4dcSJohn Baldwin 	 * If start is within an ISA alias range, move up to the start
299c825d4dcSJohn Baldwin 	 * of the next non-alias range.  As a special case, addresses
300c825d4dcSJohn Baldwin 	 * in the range 0x000 - 0x0ff should also be skipped since
301c825d4dcSJohn Baldwin 	 * those are used for various system I/O devices in ISA
302c825d4dcSJohn Baldwin 	 * systems.
303c825d4dcSJohn Baldwin 	 */
304c825d4dcSJohn Baldwin 	if (start <= 65535) {
305c825d4dcSJohn Baldwin 		if (start < 0x100 || (start & 0x300) != 0) {
306c825d4dcSJohn Baldwin 			start &= ~0x3ff;
307c825d4dcSJohn Baldwin 			start += 0x400;
308c825d4dcSJohn Baldwin 		}
309c825d4dcSJohn Baldwin 	}
310c825d4dcSJohn Baldwin 
311c825d4dcSJohn Baldwin 	/* ISA aliases are only in the lower 64KB of I/O space. */
312c825d4dcSJohn Baldwin 	while (start <= MIN(end, 65535)) {
313c825d4dcSJohn Baldwin 		next_end = MIN(start | 0xff, end);
314c825d4dcSJohn Baldwin 		cb(start, next_end, arg);
315c825d4dcSJohn Baldwin 		start += 0x400;
316c825d4dcSJohn Baldwin 	}
317c825d4dcSJohn Baldwin 
318c825d4dcSJohn Baldwin 	if (start <= end)
319c825d4dcSJohn Baldwin 		cb(start, end, arg);
320c825d4dcSJohn Baldwin }
321c825d4dcSJohn Baldwin 
322c825d4dcSJohn Baldwin static void
3232dd1bdf1SJustin Hibbits count_ranges(rman_res_t start, rman_res_t end, void *arg)
324c825d4dcSJohn Baldwin {
325c825d4dcSJohn Baldwin 	int *countp;
326c825d4dcSJohn Baldwin 
327c825d4dcSJohn Baldwin 	countp = arg;
328c825d4dcSJohn Baldwin 	(*countp)++;
329c825d4dcSJohn Baldwin }
330c825d4dcSJohn Baldwin 
331c825d4dcSJohn Baldwin struct alloc_state {
332c825d4dcSJohn Baldwin 	struct resource **res;
333c825d4dcSJohn Baldwin 	struct pcib_softc *sc;
334c825d4dcSJohn Baldwin 	int count, error;
335c825d4dcSJohn Baldwin };
336c825d4dcSJohn Baldwin 
337c825d4dcSJohn Baldwin static void
3382dd1bdf1SJustin Hibbits alloc_ranges(rman_res_t start, rman_res_t end, void *arg)
339c825d4dcSJohn Baldwin {
340c825d4dcSJohn Baldwin 	struct alloc_state *as;
341c825d4dcSJohn Baldwin 	struct pcib_window *w;
342c825d4dcSJohn Baldwin 	int rid;
343c825d4dcSJohn Baldwin 
344c825d4dcSJohn Baldwin 	as = arg;
345c825d4dcSJohn Baldwin 	if (as->error != 0)
346c825d4dcSJohn Baldwin 		return;
347c825d4dcSJohn Baldwin 
348c825d4dcSJohn Baldwin 	w = &as->sc->io;
349c825d4dcSJohn Baldwin 	rid = w->reg;
350c825d4dcSJohn Baldwin 	if (bootverbose)
351c825d4dcSJohn Baldwin 		device_printf(as->sc->dev,
352da1b038aSJustin Hibbits 		    "allocating non-ISA range %#jx-%#jx\n", start, end);
353c825d4dcSJohn Baldwin 	as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT,
354c825d4dcSJohn Baldwin 	    &rid, start, end, end - start + 1, 0);
355c825d4dcSJohn Baldwin 	if (as->res[as->count] == NULL)
356c825d4dcSJohn Baldwin 		as->error = ENXIO;
357c825d4dcSJohn Baldwin 	else
358c825d4dcSJohn Baldwin 		as->count++;
359c825d4dcSJohn Baldwin }
360c825d4dcSJohn Baldwin 
361c825d4dcSJohn Baldwin static int
3622dd1bdf1SJustin Hibbits pcib_alloc_nonisa_ranges(struct pcib_softc *sc, rman_res_t start, rman_res_t end)
363c825d4dcSJohn Baldwin {
364c825d4dcSJohn Baldwin 	struct alloc_state as;
365c825d4dcSJohn Baldwin 	int i, new_count;
366c825d4dcSJohn Baldwin 
367c825d4dcSJohn Baldwin 	/* First, see how many ranges we need. */
368c825d4dcSJohn Baldwin 	new_count = 0;
369c825d4dcSJohn Baldwin 	pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count);
370c825d4dcSJohn Baldwin 
371c825d4dcSJohn Baldwin 	/* Second, allocate the ranges. */
372c825d4dcSJohn Baldwin 	as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF,
373c825d4dcSJohn Baldwin 	    M_WAITOK);
374c825d4dcSJohn Baldwin 	as.sc = sc;
375c825d4dcSJohn Baldwin 	as.count = 0;
376c825d4dcSJohn Baldwin 	as.error = 0;
377c825d4dcSJohn Baldwin 	pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as);
378c825d4dcSJohn Baldwin 	if (as.error != 0) {
379c825d4dcSJohn Baldwin 		for (i = 0; i < as.count; i++)
380c825d4dcSJohn Baldwin 			bus_release_resource(sc->dev, SYS_RES_IOPORT,
381c825d4dcSJohn Baldwin 			    sc->io.reg, as.res[i]);
382c825d4dcSJohn Baldwin 		free(as.res, M_DEVBUF);
383c825d4dcSJohn Baldwin 		return (as.error);
384c825d4dcSJohn Baldwin 	}
385c825d4dcSJohn Baldwin 	KASSERT(as.count == new_count, ("%s: count mismatch", __func__));
386c825d4dcSJohn Baldwin 
387c825d4dcSJohn Baldwin 	/* Third, add the ranges to the window. */
388c825d4dcSJohn Baldwin 	pcib_add_window_resources(&sc->io, as.res, as.count);
389c825d4dcSJohn Baldwin 	free(as.res, M_DEVBUF);
390c825d4dcSJohn Baldwin 	return (0);
391c825d4dcSJohn Baldwin }
392c825d4dcSJohn Baldwin 
39383c41143SJohn Baldwin static void
39483c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type,
39583c41143SJohn Baldwin     int flags, pci_addr_t max_address)
39683c41143SJohn Baldwin {
397c825d4dcSJohn Baldwin 	struct resource *res;
39883c41143SJohn Baldwin 	char buf[64];
39983c41143SJohn Baldwin 	int error, rid;
40083c41143SJohn Baldwin 
40189977ce2SJustin Hibbits 	if (max_address != (rman_res_t)max_address)
402534ccd7bSJustin Hibbits 		max_address = ~0;
40383c41143SJohn Baldwin 	w->rman.rm_start = 0;
40483c41143SJohn Baldwin 	w->rman.rm_end = max_address;
40583c41143SJohn Baldwin 	w->rman.rm_type = RMAN_ARRAY;
40683c41143SJohn Baldwin 	snprintf(buf, sizeof(buf), "%s %s window",
40783c41143SJohn Baldwin 	    device_get_nameunit(sc->dev), w->name);
40883c41143SJohn Baldwin 	w->rman.rm_descr = strdup(buf, M_DEVBUF);
40983c41143SJohn Baldwin 	error = rman_init(&w->rman);
41083c41143SJohn Baldwin 	if (error)
41183c41143SJohn Baldwin 		panic("Failed to initialize %s %s rman",
41283c41143SJohn Baldwin 		    device_get_nameunit(sc->dev), w->name);
41383c41143SJohn Baldwin 
41483c41143SJohn Baldwin 	if (!pcib_is_window_open(w))
41583c41143SJohn Baldwin 		return;
41683c41143SJohn Baldwin 
41783c41143SJohn Baldwin 	if (w->base > max_address || w->limit > max_address) {
41883c41143SJohn Baldwin 		device_printf(sc->dev,
41983c41143SJohn Baldwin 		    "initial %s window has too many bits, ignoring\n", w->name);
42083c41143SJohn Baldwin 		return;
42183c41143SJohn Baldwin 	}
422c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE)
423c825d4dcSJohn Baldwin 		(void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit);
424c825d4dcSJohn Baldwin 	else {
42583c41143SJohn Baldwin 		rid = w->reg;
426c825d4dcSJohn Baldwin 		res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit,
42783c41143SJohn Baldwin 		    w->limit - w->base + 1, flags);
428c825d4dcSJohn Baldwin 		if (res != NULL)
429c825d4dcSJohn Baldwin 			pcib_add_window_resources(w, &res, 1);
430c825d4dcSJohn Baldwin 	}
43183c41143SJohn Baldwin 	if (w->res == NULL) {
43283c41143SJohn Baldwin 		device_printf(sc->dev,
43383c41143SJohn Baldwin 		    "failed to allocate initial %s window: %#jx-%#jx\n",
43483c41143SJohn Baldwin 		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
43583c41143SJohn Baldwin 		w->base = max_address;
43683c41143SJohn Baldwin 		w->limit = 0;
43783c41143SJohn Baldwin 		pcib_write_windows(sc, w->mask);
43883c41143SJohn Baldwin 		return;
43983c41143SJohn Baldwin 	}
44083c41143SJohn Baldwin 	pcib_activate_window(sc, type);
44183c41143SJohn Baldwin }
44283c41143SJohn Baldwin 
44383c41143SJohn Baldwin /*
44483c41143SJohn Baldwin  * Initialize I/O windows.
44583c41143SJohn Baldwin  */
44683c41143SJohn Baldwin static void
44783c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc)
44883c41143SJohn Baldwin {
44983c41143SJohn Baldwin 	pci_addr_t max;
45083c41143SJohn Baldwin 	device_t dev;
45183c41143SJohn Baldwin 	uint32_t val;
45283c41143SJohn Baldwin 
45383c41143SJohn Baldwin 	dev = sc->dev;
45483c41143SJohn Baldwin 
4550070c94bSJohn Baldwin 	if (pci_clear_pcib) {
456809923caSJustin Hibbits 		pcib_bridge_init(dev);
4570070c94bSJohn Baldwin 	}
4580070c94bSJohn Baldwin 
45983c41143SJohn Baldwin 	/* Determine if the I/O port window is implemented. */
46083c41143SJohn Baldwin 	val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
46183c41143SJohn Baldwin 	if (val == 0) {
46283c41143SJohn Baldwin 		/*
46383c41143SJohn Baldwin 		 * If 'val' is zero, then only 16-bits of I/O space
46483c41143SJohn Baldwin 		 * are supported.
46583c41143SJohn Baldwin 		 */
46683c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
46783c41143SJohn Baldwin 		if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) {
46883c41143SJohn Baldwin 			sc->io.valid = 1;
46983c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOBASEL_1, 0, 1);
47083c41143SJohn Baldwin 		}
47183c41143SJohn Baldwin 	} else
47283c41143SJohn Baldwin 		sc->io.valid = 1;
47383c41143SJohn Baldwin 
47483c41143SJohn Baldwin 	/* Read the existing I/O port window. */
47583c41143SJohn Baldwin 	if (sc->io.valid) {
47683c41143SJohn Baldwin 		sc->io.reg = PCIR_IOBASEL_1;
47783c41143SJohn Baldwin 		sc->io.step = 12;
47883c41143SJohn Baldwin 		sc->io.mask = WIN_IO;
47983c41143SJohn Baldwin 		sc->io.name = "I/O port";
48083c41143SJohn Baldwin 		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
48183c41143SJohn Baldwin 			sc->io.base = PCI_PPBIOBASE(
48283c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOBASEH_1, 2), val);
48383c41143SJohn Baldwin 			sc->io.limit = PCI_PPBIOLIMIT(
48483c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITH_1, 2),
48583c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
48683c41143SJohn Baldwin 			max = 0xffffffff;
48783c41143SJohn Baldwin 		} else {
48883c41143SJohn Baldwin 			sc->io.base = PCI_PPBIOBASE(0, val);
48983c41143SJohn Baldwin 			sc->io.limit = PCI_PPBIOLIMIT(0,
49083c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
49183c41143SJohn Baldwin 			max = 0xffff;
49283c41143SJohn Baldwin 		}
49383c41143SJohn Baldwin 		pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max);
49483c41143SJohn Baldwin 	}
49583c41143SJohn Baldwin 
49683c41143SJohn Baldwin 	/* Read the existing memory window. */
49783c41143SJohn Baldwin 	sc->mem.valid = 1;
49883c41143SJohn Baldwin 	sc->mem.reg = PCIR_MEMBASE_1;
49983c41143SJohn Baldwin 	sc->mem.step = 20;
50083c41143SJohn Baldwin 	sc->mem.mask = WIN_MEM;
50183c41143SJohn Baldwin 	sc->mem.name = "memory";
50283c41143SJohn Baldwin 	sc->mem.base = PCI_PPBMEMBASE(0,
50383c41143SJohn Baldwin 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
50483c41143SJohn Baldwin 	sc->mem.limit = PCI_PPBMEMLIMIT(0,
50583c41143SJohn Baldwin 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
50683c41143SJohn Baldwin 	pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff);
50783c41143SJohn Baldwin 
50883c41143SJohn Baldwin 	/* Determine if the prefetchable memory window is implemented. */
50983c41143SJohn Baldwin 	val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
51083c41143SJohn Baldwin 	if (val == 0) {
51183c41143SJohn Baldwin 		/*
51283c41143SJohn Baldwin 		 * If 'val' is zero, then only 32-bits of memory space
51383c41143SJohn Baldwin 		 * are supported.
51483c41143SJohn Baldwin 		 */
51583c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
51683c41143SJohn Baldwin 		if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) {
51783c41143SJohn Baldwin 			sc->pmem.valid = 1;
51883c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMBASEL_1, 0, 2);
51983c41143SJohn Baldwin 		}
52083c41143SJohn Baldwin 	} else
52183c41143SJohn Baldwin 		sc->pmem.valid = 1;
52283c41143SJohn Baldwin 
52383c41143SJohn Baldwin 	/* Read the existing prefetchable memory window. */
52483c41143SJohn Baldwin 	if (sc->pmem.valid) {
52583c41143SJohn Baldwin 		sc->pmem.reg = PCIR_PMBASEL_1;
52683c41143SJohn Baldwin 		sc->pmem.step = 20;
52783c41143SJohn Baldwin 		sc->pmem.mask = WIN_PMEM;
52883c41143SJohn Baldwin 		sc->pmem.name = "prefetch";
52983c41143SJohn Baldwin 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
53083c41143SJohn Baldwin 			sc->pmem.base = PCI_PPBMEMBASE(
53183c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMBASEH_1, 4), val);
53283c41143SJohn Baldwin 			sc->pmem.limit = PCI_PPBMEMLIMIT(
53383c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITH_1, 4),
53483c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
53583c41143SJohn Baldwin 			max = 0xffffffffffffffff;
53683c41143SJohn Baldwin 		} else {
53783c41143SJohn Baldwin 			sc->pmem.base = PCI_PPBMEMBASE(0, val);
53883c41143SJohn Baldwin 			sc->pmem.limit = PCI_PPBMEMLIMIT(0,
53983c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
54083c41143SJohn Baldwin 			max = 0xffffffff;
54183c41143SJohn Baldwin 		}
54283c41143SJohn Baldwin 		pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY,
54383c41143SJohn Baldwin 		    RF_PREFETCHABLE, max);
54483c41143SJohn Baldwin 	}
54583c41143SJohn Baldwin }
54683c41143SJohn Baldwin 
547*6f33eaa5SJohn Baldwin static void
548*6f33eaa5SJohn Baldwin pcib_release_window(struct pcib_softc *sc, struct pcib_window *w, int type)
549*6f33eaa5SJohn Baldwin {
550*6f33eaa5SJohn Baldwin 	device_t dev;
551*6f33eaa5SJohn Baldwin 	int error, i;
552*6f33eaa5SJohn Baldwin 
553*6f33eaa5SJohn Baldwin 	if (!w->valid)
554*6f33eaa5SJohn Baldwin 		return;
555*6f33eaa5SJohn Baldwin 
556*6f33eaa5SJohn Baldwin 	dev = sc->dev;
557*6f33eaa5SJohn Baldwin 	error = rman_fini(&w->rman);
558*6f33eaa5SJohn Baldwin 	if (error) {
559*6f33eaa5SJohn Baldwin 		device_printf(dev, "failed to release %s rman\n", w->name);
560*6f33eaa5SJohn Baldwin 		return;
561*6f33eaa5SJohn Baldwin 	}
562*6f33eaa5SJohn Baldwin 	free(__DECONST(char *, w->rman.rm_descr), M_DEVBUF);
563*6f33eaa5SJohn Baldwin 
564*6f33eaa5SJohn Baldwin 	for (i = 0; i < w->count; i++) {
565*6f33eaa5SJohn Baldwin 		error = bus_free_resource(dev, type, w->res[i]);
566*6f33eaa5SJohn Baldwin 		if (error)
567*6f33eaa5SJohn Baldwin 			device_printf(dev,
568*6f33eaa5SJohn Baldwin 			    "failed to release %s resource: %d\n", w->name,
569*6f33eaa5SJohn Baldwin 			    error);
570*6f33eaa5SJohn Baldwin 	}
571*6f33eaa5SJohn Baldwin 	free(w->res, M_DEVBUF);
572*6f33eaa5SJohn Baldwin }
573*6f33eaa5SJohn Baldwin 
574*6f33eaa5SJohn Baldwin static void
575*6f33eaa5SJohn Baldwin pcib_free_windows(struct pcib_softc *sc)
576*6f33eaa5SJohn Baldwin {
577*6f33eaa5SJohn Baldwin 
578*6f33eaa5SJohn Baldwin 	pcib_release_window(sc, &sc->pmem, SYS_RES_MEMORY);
579*6f33eaa5SJohn Baldwin 	pcib_release_window(sc, &sc->mem, SYS_RES_MEMORY);
580*6f33eaa5SJohn Baldwin 	pcib_release_window(sc, &sc->io, SYS_RES_IOPORT);
581*6f33eaa5SJohn Baldwin }
582*6f33eaa5SJohn Baldwin 
5834edef187SJohn Baldwin #ifdef PCI_RES_BUS
5844edef187SJohn Baldwin /*
5854edef187SJohn Baldwin  * Allocate a suitable secondary bus for this bridge if needed and
5864edef187SJohn Baldwin  * initialize the resource manager for the secondary bus range.  Note
5874edef187SJohn Baldwin  * that the minimum count is a desired value and this may allocate a
5884edef187SJohn Baldwin  * smaller range.
5894edef187SJohn Baldwin  */
5904edef187SJohn Baldwin void
5914edef187SJohn Baldwin pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count)
5924edef187SJohn Baldwin {
5934edef187SJohn Baldwin 	char buf[64];
594ad6f36f8SJohn Baldwin 	int error, rid, sec_reg;
5954edef187SJohn Baldwin 
5964edef187SJohn Baldwin 	switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) {
5974edef187SJohn Baldwin 	case PCIM_HDRTYPE_BRIDGE:
598ad6f36f8SJohn Baldwin 		sec_reg = PCIR_SECBUS_1;
5994edef187SJohn Baldwin 		bus->sub_reg = PCIR_SUBBUS_1;
6004edef187SJohn Baldwin 		break;
6014edef187SJohn Baldwin 	case PCIM_HDRTYPE_CARDBUS:
602ad6f36f8SJohn Baldwin 		sec_reg = PCIR_SECBUS_2;
6034edef187SJohn Baldwin 		bus->sub_reg = PCIR_SUBBUS_2;
6044edef187SJohn Baldwin 		break;
6054edef187SJohn Baldwin 	default:
6064edef187SJohn Baldwin 		panic("not a PCI bridge");
6074edef187SJohn Baldwin 	}
608ad6f36f8SJohn Baldwin 	bus->sec = pci_read_config(dev, sec_reg, 1);
609ad6f36f8SJohn Baldwin 	bus->sub = pci_read_config(dev, bus->sub_reg, 1);
6104edef187SJohn Baldwin 	bus->dev = dev;
6114edef187SJohn Baldwin 	bus->rman.rm_start = 0;
6124edef187SJohn Baldwin 	bus->rman.rm_end = PCI_BUSMAX;
6134edef187SJohn Baldwin 	bus->rman.rm_type = RMAN_ARRAY;
6144edef187SJohn Baldwin 	snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev));
6154edef187SJohn Baldwin 	bus->rman.rm_descr = strdup(buf, M_DEVBUF);
6164edef187SJohn Baldwin 	error = rman_init(&bus->rman);
6174edef187SJohn Baldwin 	if (error)
6184edef187SJohn Baldwin 		panic("Failed to initialize %s bus number rman",
6194edef187SJohn Baldwin 		    device_get_nameunit(dev));
6204edef187SJohn Baldwin 
6214edef187SJohn Baldwin 	/*
6224edef187SJohn Baldwin 	 * Allocate a bus range.  This will return an existing bus range
6234edef187SJohn Baldwin 	 * if one exists, or a new bus range if one does not.
6244edef187SJohn Baldwin 	 */
6254edef187SJohn Baldwin 	rid = 0;
626c47476d7SJustin Hibbits 	bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid,
6274edef187SJohn Baldwin 	    min_count, 0);
6284edef187SJohn Baldwin 	if (bus->res == NULL) {
6294edef187SJohn Baldwin 		/*
6304edef187SJohn Baldwin 		 * Fall back to just allocating a range of a single bus
6314edef187SJohn Baldwin 		 * number.
6324edef187SJohn Baldwin 		 */
633c47476d7SJustin Hibbits 		bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid,
6344edef187SJohn Baldwin 		    1, 0);
6354edef187SJohn Baldwin 	} else if (rman_get_size(bus->res) < min_count)
6364edef187SJohn Baldwin 		/*
6374edef187SJohn Baldwin 		 * Attempt to grow the existing range to satisfy the
6384edef187SJohn Baldwin 		 * minimum desired count.
6394edef187SJohn Baldwin 		 */
6404edef187SJohn Baldwin 		(void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res,
6414edef187SJohn Baldwin 		    rman_get_start(bus->res), rman_get_start(bus->res) +
6424edef187SJohn Baldwin 		    min_count - 1);
6434edef187SJohn Baldwin 
6444edef187SJohn Baldwin 	/*
6454edef187SJohn Baldwin 	 * Add the initial resource to the rman.
6464edef187SJohn Baldwin 	 */
6474edef187SJohn Baldwin 	if (bus->res != NULL) {
6484edef187SJohn Baldwin 		error = rman_manage_region(&bus->rman, rman_get_start(bus->res),
6494edef187SJohn Baldwin 		    rman_get_end(bus->res));
6504edef187SJohn Baldwin 		if (error)
6514edef187SJohn Baldwin 			panic("Failed to add resource to rman");
6524edef187SJohn Baldwin 		bus->sec = rman_get_start(bus->res);
6534edef187SJohn Baldwin 		bus->sub = rman_get_end(bus->res);
6544edef187SJohn Baldwin 	}
6554edef187SJohn Baldwin }
6564edef187SJohn Baldwin 
657*6f33eaa5SJohn Baldwin void
658*6f33eaa5SJohn Baldwin pcib_free_secbus(device_t dev, struct pcib_secbus *bus)
659*6f33eaa5SJohn Baldwin {
660*6f33eaa5SJohn Baldwin 	int error;
661*6f33eaa5SJohn Baldwin 
662*6f33eaa5SJohn Baldwin 	error = rman_fini(&bus->rman);
663*6f33eaa5SJohn Baldwin 	if (error) {
664*6f33eaa5SJohn Baldwin 		device_printf(dev, "failed to release bus number rman\n");
665*6f33eaa5SJohn Baldwin 		return;
666*6f33eaa5SJohn Baldwin 	}
667*6f33eaa5SJohn Baldwin 	free(__DECONST(char *, bus->rman.rm_descr), M_DEVBUF);
668*6f33eaa5SJohn Baldwin 
669*6f33eaa5SJohn Baldwin 	error = bus_free_resource(dev, PCI_RES_BUS, bus->res);
670*6f33eaa5SJohn Baldwin 	if (error)
671*6f33eaa5SJohn Baldwin 		device_printf(dev,
672*6f33eaa5SJohn Baldwin 		    "failed to release bus numbers resource: %d\n", error);
673*6f33eaa5SJohn Baldwin }
674*6f33eaa5SJohn Baldwin 
6754edef187SJohn Baldwin static struct resource *
6764edef187SJohn Baldwin pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid,
6772dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
6784edef187SJohn Baldwin {
6794edef187SJohn Baldwin 	struct resource *res;
6804edef187SJohn Baldwin 
6814edef187SJohn Baldwin 	res = rman_reserve_resource(&bus->rman, start, end, count, flags,
6824edef187SJohn Baldwin 	    child);
6834edef187SJohn Baldwin 	if (res == NULL)
6844edef187SJohn Baldwin 		return (NULL);
6854edef187SJohn Baldwin 
6864edef187SJohn Baldwin 	if (bootverbose)
6874edef187SJohn Baldwin 		device_printf(bus->dev,
688da1b038aSJustin Hibbits 		    "allocated bus range (%ju-%ju) for rid %d of %s\n",
6894edef187SJohn Baldwin 		    rman_get_start(res), rman_get_end(res), *rid,
6904edef187SJohn Baldwin 		    pcib_child_name(child));
6914edef187SJohn Baldwin 	rman_set_rid(res, *rid);
6924edef187SJohn Baldwin 	return (res);
6934edef187SJohn Baldwin }
6944edef187SJohn Baldwin 
6954edef187SJohn Baldwin /*
6964edef187SJohn Baldwin  * Attempt to grow the secondary bus range.  This is much simpler than
6974edef187SJohn Baldwin  * for I/O windows as the range can only be grown by increasing
6984edef187SJohn Baldwin  * subbus.
6994edef187SJohn Baldwin  */
7004edef187SJohn Baldwin static int
7012dd1bdf1SJustin Hibbits pcib_grow_subbus(struct pcib_secbus *bus, rman_res_t new_end)
7024edef187SJohn Baldwin {
7032dd1bdf1SJustin Hibbits 	rman_res_t old_end;
7044edef187SJohn Baldwin 	int error;
7054edef187SJohn Baldwin 
7064edef187SJohn Baldwin 	old_end = rman_get_end(bus->res);
7074edef187SJohn Baldwin 	KASSERT(new_end > old_end, ("attempt to shrink subbus"));
7084edef187SJohn Baldwin 	error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res,
7094edef187SJohn Baldwin 	    rman_get_start(bus->res), new_end);
7104edef187SJohn Baldwin 	if (error)
7114edef187SJohn Baldwin 		return (error);
7124edef187SJohn Baldwin 	if (bootverbose)
713da1b038aSJustin Hibbits 		device_printf(bus->dev, "grew bus range to %ju-%ju\n",
7144edef187SJohn Baldwin 		    rman_get_start(bus->res), rman_get_end(bus->res));
7154edef187SJohn Baldwin 	error = rman_manage_region(&bus->rman, old_end + 1,
7164edef187SJohn Baldwin 	    rman_get_end(bus->res));
7174edef187SJohn Baldwin 	if (error)
7184edef187SJohn Baldwin 		panic("Failed to add resource to rman");
7194edef187SJohn Baldwin 	bus->sub = rman_get_end(bus->res);
7204edef187SJohn Baldwin 	pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1);
7214edef187SJohn Baldwin 	return (0);
7224edef187SJohn Baldwin }
7234edef187SJohn Baldwin 
7244edef187SJohn Baldwin struct resource *
7254edef187SJohn Baldwin pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid,
7262dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
7274edef187SJohn Baldwin {
7284edef187SJohn Baldwin 	struct resource *res;
7292dd1bdf1SJustin Hibbits 	rman_res_t start_free, end_free, new_end;
7304edef187SJohn Baldwin 
7314edef187SJohn Baldwin 	/*
7324edef187SJohn Baldwin 	 * First, see if the request can be satisified by the existing
7334edef187SJohn Baldwin 	 * bus range.
7344edef187SJohn Baldwin 	 */
7354edef187SJohn Baldwin 	res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags);
7364edef187SJohn Baldwin 	if (res != NULL)
7374edef187SJohn Baldwin 		return (res);
7384edef187SJohn Baldwin 
7394edef187SJohn Baldwin 	/*
7404edef187SJohn Baldwin 	 * Figure out a range to grow the bus range.  First, find the
7414edef187SJohn Baldwin 	 * first bus number after the last allocated bus in the rman and
7424edef187SJohn Baldwin 	 * enforce that as a minimum starting point for the range.
7434edef187SJohn Baldwin 	 */
7444edef187SJohn Baldwin 	if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 ||
7454edef187SJohn Baldwin 	    end_free != bus->sub)
7464edef187SJohn Baldwin 		start_free = bus->sub + 1;
7474edef187SJohn Baldwin 	if (start_free < start)
7484edef187SJohn Baldwin 		start_free = start;
7494edef187SJohn Baldwin 	new_end = start_free + count - 1;
7504edef187SJohn Baldwin 
7514edef187SJohn Baldwin 	/*
7524edef187SJohn Baldwin 	 * See if this new range would satisfy the request if it
7534edef187SJohn Baldwin 	 * succeeds.
7544edef187SJohn Baldwin 	 */
7554edef187SJohn Baldwin 	if (new_end > end)
7564edef187SJohn Baldwin 		return (NULL);
7574edef187SJohn Baldwin 
7584edef187SJohn Baldwin 	/* Finally, attempt to grow the existing resource. */
7594edef187SJohn Baldwin 	if (bootverbose) {
7604edef187SJohn Baldwin 		device_printf(bus->dev,
761da1b038aSJustin Hibbits 		    "attempting to grow bus range for %ju buses\n", count);
762da1b038aSJustin Hibbits 		printf("\tback candidate range: %ju-%ju\n", start_free,
7634edef187SJohn Baldwin 		    new_end);
7644edef187SJohn Baldwin 	}
7654edef187SJohn Baldwin 	if (pcib_grow_subbus(bus, new_end) == 0)
7664edef187SJohn Baldwin 		return (pcib_suballoc_bus(bus, child, rid, start, end, count,
7674edef187SJohn Baldwin 		    flags));
7684edef187SJohn Baldwin 	return (NULL);
7694edef187SJohn Baldwin }
7704edef187SJohn Baldwin #endif
7714edef187SJohn Baldwin 
77283c41143SJohn Baldwin #else
77383c41143SJohn Baldwin 
774bb0d0a8eSMike Smith /*
775b0a2d4b8SWarner Losh  * Is the prefetch window open (eg, can we allocate memory in it?)
776b0a2d4b8SWarner Losh  */
777b0a2d4b8SWarner Losh static int
778b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc)
779b0a2d4b8SWarner Losh {
780b0a2d4b8SWarner Losh 	return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
781b0a2d4b8SWarner Losh }
782b0a2d4b8SWarner Losh 
783b0a2d4b8SWarner Losh /*
784b0a2d4b8SWarner Losh  * Is the nonprefetch window open (eg, can we allocate memory in it?)
785b0a2d4b8SWarner Losh  */
786b0a2d4b8SWarner Losh static int
787b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc)
788b0a2d4b8SWarner Losh {
789b0a2d4b8SWarner Losh 	return (sc->membase > 0 && sc->membase < sc->memlimit);
790b0a2d4b8SWarner Losh }
791b0a2d4b8SWarner Losh 
792b0a2d4b8SWarner Losh /*
793b0a2d4b8SWarner Losh  * Is the io window open (eg, can we allocate ports in it?)
794b0a2d4b8SWarner Losh  */
795b0a2d4b8SWarner Losh static int
796b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc)
797b0a2d4b8SWarner Losh {
798b0a2d4b8SWarner Losh 	return (sc->iobase > 0 && sc->iobase < sc->iolimit);
799b0a2d4b8SWarner Losh }
800b0a2d4b8SWarner Losh 
801b0a2d4b8SWarner Losh /*
802e36af292SJung-uk Kim  * Get current I/O decode.
803e36af292SJung-uk Kim  */
804e36af292SJung-uk Kim static void
805e36af292SJung-uk Kim pcib_get_io_decode(struct pcib_softc *sc)
806e36af292SJung-uk Kim {
807e36af292SJung-uk Kim 	device_t	dev;
808e36af292SJung-uk Kim 	uint32_t	iolow;
809e36af292SJung-uk Kim 
810e36af292SJung-uk Kim 	dev = sc->dev;
811e36af292SJung-uk Kim 
812e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
813e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
814e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(
815e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow);
816e36af292SJung-uk Kim 	else
817e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(0, iolow);
818e36af292SJung-uk Kim 
819e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
820e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
821e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(
822e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow);
823e36af292SJung-uk Kim 	else
824e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(0, iolow);
825e36af292SJung-uk Kim }
826e36af292SJung-uk Kim 
827e36af292SJung-uk Kim /*
828e36af292SJung-uk Kim  * Get current memory decode.
829e36af292SJung-uk Kim  */
830e36af292SJung-uk Kim static void
831e36af292SJung-uk Kim pcib_get_mem_decode(struct pcib_softc *sc)
832e36af292SJung-uk Kim {
833e36af292SJung-uk Kim 	device_t	dev;
834e36af292SJung-uk Kim 	pci_addr_t	pmemlow;
835e36af292SJung-uk Kim 
836e36af292SJung-uk Kim 	dev = sc->dev;
837e36af292SJung-uk Kim 
838e36af292SJung-uk Kim 	sc->membase = PCI_PPBMEMBASE(0,
839e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
840e36af292SJung-uk Kim 	sc->memlimit = PCI_PPBMEMLIMIT(0,
841e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
842e36af292SJung-uk Kim 
843e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2);
844e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
845e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(
846e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow);
847e36af292SJung-uk Kim 	else
848e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(0, pmemlow);
849e36af292SJung-uk Kim 
850e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2);
851e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
852e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(
853e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow);
854e36af292SJung-uk Kim 	else
855e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow);
856e36af292SJung-uk Kim }
857e36af292SJung-uk Kim 
858e36af292SJung-uk Kim /*
859e36af292SJung-uk Kim  * Restore previous I/O decode.
860e36af292SJung-uk Kim  */
861e36af292SJung-uk Kim static void
862e36af292SJung-uk Kim pcib_set_io_decode(struct pcib_softc *sc)
863e36af292SJung-uk Kim {
864e36af292SJung-uk Kim 	device_t	dev;
865e36af292SJung-uk Kim 	uint32_t	iohi;
866e36af292SJung-uk Kim 
867e36af292SJung-uk Kim 	dev = sc->dev;
868e36af292SJung-uk Kim 
869e36af292SJung-uk Kim 	iohi = sc->iobase >> 16;
870e36af292SJung-uk Kim 	if (iohi > 0)
871e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2);
872e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1);
873e36af292SJung-uk Kim 
874e36af292SJung-uk Kim 	iohi = sc->iolimit >> 16;
875e36af292SJung-uk Kim 	if (iohi > 0)
876e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2);
877e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1);
878e36af292SJung-uk Kim }
879e36af292SJung-uk Kim 
880e36af292SJung-uk Kim /*
881e36af292SJung-uk Kim  * Restore previous memory decode.
882e36af292SJung-uk Kim  */
883e36af292SJung-uk Kim static void
884e36af292SJung-uk Kim pcib_set_mem_decode(struct pcib_softc *sc)
885e36af292SJung-uk Kim {
886e36af292SJung-uk Kim 	device_t	dev;
887e36af292SJung-uk Kim 	pci_addr_t	pmemhi;
888e36af292SJung-uk Kim 
889e36af292SJung-uk Kim 	dev = sc->dev;
890e36af292SJung-uk Kim 
891e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2);
892e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2);
893e36af292SJung-uk Kim 
894e36af292SJung-uk Kim 	pmemhi = sc->pmembase >> 32;
895e36af292SJung-uk Kim 	if (pmemhi > 0)
896e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4);
897e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2);
898e36af292SJung-uk Kim 
899e36af292SJung-uk Kim 	pmemhi = sc->pmemlimit >> 32;
900e36af292SJung-uk Kim 	if (pmemhi > 0)
901e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4);
902e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2);
903e36af292SJung-uk Kim }
90483c41143SJohn Baldwin #endif
905e36af292SJung-uk Kim 
90682cb5c3bSJohn Baldwin #ifdef PCI_HP
90782cb5c3bSJohn Baldwin /*
90882cb5c3bSJohn Baldwin  * PCI-express HotPlug support.
90982cb5c3bSJohn Baldwin  */
91082cb5c3bSJohn Baldwin static void
91182cb5c3bSJohn Baldwin pcib_probe_hotplug(struct pcib_softc *sc)
91282cb5c3bSJohn Baldwin {
91382cb5c3bSJohn Baldwin 	device_t dev;
91482cb5c3bSJohn Baldwin 
91582cb5c3bSJohn Baldwin 	dev = sc->dev;
91682cb5c3bSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, NULL) != 0)
91782cb5c3bSJohn Baldwin 		return;
91882cb5c3bSJohn Baldwin 
91982cb5c3bSJohn Baldwin 	if (!(pcie_read_config(dev, PCIER_FLAGS, 2) & PCIEM_FLAGS_SLOT))
92082cb5c3bSJohn Baldwin 		return;
92182cb5c3bSJohn Baldwin 
92282cb5c3bSJohn Baldwin 	sc->pcie_link_cap = pcie_read_config(dev, PCIER_LINK_CAP, 4);
92382cb5c3bSJohn Baldwin 	sc->pcie_slot_cap = pcie_read_config(dev, PCIER_SLOT_CAP, 4);
92482cb5c3bSJohn Baldwin 
92582cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_HPC)
92682cb5c3bSJohn Baldwin 		sc->flags |= PCIB_HOTPLUG;
92782cb5c3bSJohn Baldwin }
92882cb5c3bSJohn Baldwin 
92982cb5c3bSJohn Baldwin /*
93082cb5c3bSJohn Baldwin  * Send a HotPlug command to the slot control register.  If this slot
93107454911SJohn Baldwin  * uses command completion interrupts and a previous command is still
93207454911SJohn Baldwin  * in progress, then the command is dropped.  Once the previous
93307454911SJohn Baldwin  * command completes or times out, pcib_pcie_hotplug_update() will be
93407454911SJohn Baldwin  * invoked to post a new command based on the slot's state at that
93507454911SJohn Baldwin  * time.
93682cb5c3bSJohn Baldwin  */
93782cb5c3bSJohn Baldwin static void
93882cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(struct pcib_softc *sc, uint16_t val, uint16_t mask)
93982cb5c3bSJohn Baldwin {
94082cb5c3bSJohn Baldwin 	device_t dev;
94182cb5c3bSJohn Baldwin 	uint16_t ctl, new;
94282cb5c3bSJohn Baldwin 
94382cb5c3bSJohn Baldwin 	dev = sc->dev;
94482cb5c3bSJohn Baldwin 
94507454911SJohn Baldwin 	if (sc->flags & PCIB_HOTPLUG_CMD_PENDING)
94607454911SJohn Baldwin 		return;
94707454911SJohn Baldwin 
94882cb5c3bSJohn Baldwin 	ctl = pcie_read_config(dev, PCIER_SLOT_CTL, 2);
94982cb5c3bSJohn Baldwin 	new = (ctl & ~mask) | val;
95007454911SJohn Baldwin 	if (new == ctl)
95107454911SJohn Baldwin 		return;
95207454911SJohn Baldwin 	pcie_write_config(dev, PCIER_SLOT_CTL, new, 2);
953*6f33eaa5SJohn Baldwin 	if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS) &&
954*6f33eaa5SJohn Baldwin 	    (ctl & new) & PCIEM_SLOT_CTL_CCIE) {
95582cb5c3bSJohn Baldwin 		sc->flags |= PCIB_HOTPLUG_CMD_PENDING;
95682cb5c3bSJohn Baldwin 		if (!cold)
95782cb5c3bSJohn Baldwin 			callout_reset(&sc->pcie_cc_timer, hz,
95882cb5c3bSJohn Baldwin 			    pcib_pcie_cc_timeout, sc);
95982cb5c3bSJohn Baldwin 	}
96082cb5c3bSJohn Baldwin }
96182cb5c3bSJohn Baldwin 
96282cb5c3bSJohn Baldwin static void
96382cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(struct pcib_softc *sc)
96482cb5c3bSJohn Baldwin {
96582cb5c3bSJohn Baldwin 	device_t dev;
96682cb5c3bSJohn Baldwin 
96782cb5c3bSJohn Baldwin 	dev = sc->dev;
96882cb5c3bSJohn Baldwin 
96982cb5c3bSJohn Baldwin 	if (bootverbose)
97082cb5c3bSJohn Baldwin 		device_printf(dev, "Command Completed\n");
97182cb5c3bSJohn Baldwin 	if (!(sc->flags & PCIB_HOTPLUG_CMD_PENDING))
97282cb5c3bSJohn Baldwin 		return;
97382cb5c3bSJohn Baldwin 	callout_stop(&sc->pcie_cc_timer);
97482cb5c3bSJohn Baldwin 	sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING;
975*6f33eaa5SJohn Baldwin 	wakeup(sc);
97682cb5c3bSJohn Baldwin }
97782cb5c3bSJohn Baldwin 
97882cb5c3bSJohn Baldwin /*
97982cb5c3bSJohn Baldwin  * Returns true if a card is fully inserted from the user's
98082cb5c3bSJohn Baldwin  * perspective.  It may not yet be ready for access, but the driver
98182cb5c3bSJohn Baldwin  * can now start enabling access if necessary.
98282cb5c3bSJohn Baldwin  */
98382cb5c3bSJohn Baldwin static bool
98482cb5c3bSJohn Baldwin pcib_hotplug_inserted(struct pcib_softc *sc)
98582cb5c3bSJohn Baldwin {
98682cb5c3bSJohn Baldwin 
98782cb5c3bSJohn Baldwin 	/* Pretend the card isn't present if a detach is forced. */
98882cb5c3bSJohn Baldwin 	if (sc->flags & PCIB_DETACHING)
98982cb5c3bSJohn Baldwin 		return (false);
99082cb5c3bSJohn Baldwin 
99182cb5c3bSJohn Baldwin 	/* Card must be present in the slot. */
99282cb5c3bSJohn Baldwin 	if ((sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS) == 0)
99382cb5c3bSJohn Baldwin 		return (false);
99482cb5c3bSJohn Baldwin 
99582cb5c3bSJohn Baldwin 	/* A power fault implicitly turns off power to the slot. */
99682cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD)
99782cb5c3bSJohn Baldwin 		return (false);
99882cb5c3bSJohn Baldwin 
99982cb5c3bSJohn Baldwin 	/* If the MRL is disengaged, the slot is powered off. */
100082cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP &&
100182cb5c3bSJohn Baldwin 	    (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS) != 0)
100282cb5c3bSJohn Baldwin 		return (false);
100382cb5c3bSJohn Baldwin 
100482cb5c3bSJohn Baldwin 	return (true);
100582cb5c3bSJohn Baldwin }
100682cb5c3bSJohn Baldwin 
100782cb5c3bSJohn Baldwin /*
100882cb5c3bSJohn Baldwin  * Returns -1 if the card is fully inserted, powered, and ready for
100982cb5c3bSJohn Baldwin  * access.  Otherwise, returns 0.
101082cb5c3bSJohn Baldwin  */
101182cb5c3bSJohn Baldwin static int
101282cb5c3bSJohn Baldwin pcib_hotplug_present(struct pcib_softc *sc)
101382cb5c3bSJohn Baldwin {
101482cb5c3bSJohn Baldwin 	device_t dev;
101582cb5c3bSJohn Baldwin 
101682cb5c3bSJohn Baldwin 	dev = sc->dev;
101782cb5c3bSJohn Baldwin 
101882cb5c3bSJohn Baldwin 	/* Card must be inserted. */
101982cb5c3bSJohn Baldwin 	if (!pcib_hotplug_inserted(sc))
102082cb5c3bSJohn Baldwin 		return (0);
102182cb5c3bSJohn Baldwin 
102282cb5c3bSJohn Baldwin 	/*
102382cb5c3bSJohn Baldwin 	 * Require the Electromechanical Interlock to be engaged if
102482cb5c3bSJohn Baldwin 	 * present.
102582cb5c3bSJohn Baldwin 	 */
102682cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP &&
102782cb5c3bSJohn Baldwin 	    (sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS) == 0)
102882cb5c3bSJohn Baldwin 		return (0);
102982cb5c3bSJohn Baldwin 
103082cb5c3bSJohn Baldwin 	/* Require the Data Link Layer to be active. */
103182cb5c3bSJohn Baldwin 	if (sc->pcie_link_cap & PCIEM_LINK_CAP_DL_ACTIVE) {
103282cb5c3bSJohn Baldwin 		if (!(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE))
103382cb5c3bSJohn Baldwin 			return (0);
103482cb5c3bSJohn Baldwin 	}
103582cb5c3bSJohn Baldwin 
103682cb5c3bSJohn Baldwin 	return (-1);
103782cb5c3bSJohn Baldwin }
103882cb5c3bSJohn Baldwin 
103982cb5c3bSJohn Baldwin static void
104082cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(struct pcib_softc *sc, uint16_t val, uint16_t mask,
104182cb5c3bSJohn Baldwin     bool schedule_task)
104282cb5c3bSJohn Baldwin {
104382cb5c3bSJohn Baldwin 	bool card_inserted;
104482cb5c3bSJohn Baldwin 
104582cb5c3bSJohn Baldwin 	/* Clear DETACHING if Present Detect has cleared. */
104682cb5c3bSJohn Baldwin 	if ((sc->pcie_slot_sta & (PCIEM_SLOT_STA_PDC | PCIEM_SLOT_STA_PDS)) ==
104782cb5c3bSJohn Baldwin 	    PCIEM_SLOT_STA_PDC)
104882cb5c3bSJohn Baldwin 		sc->flags &= ~PCIB_DETACHING;
104982cb5c3bSJohn Baldwin 
105082cb5c3bSJohn Baldwin 	card_inserted = pcib_hotplug_inserted(sc);
105182cb5c3bSJohn Baldwin 
105282cb5c3bSJohn Baldwin 	/* Turn the power indicator on if a card is inserted. */
105382cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PIP) {
105482cb5c3bSJohn Baldwin 		mask |= PCIEM_SLOT_CTL_PIC;
105582cb5c3bSJohn Baldwin 		if (card_inserted)
105682cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PI_ON;
105782cb5c3bSJohn Baldwin 		else if (sc->flags & PCIB_DETACH_PENDING)
105882cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PI_BLINK;
105982cb5c3bSJohn Baldwin 		else
106082cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PI_OFF;
106182cb5c3bSJohn Baldwin 	}
106282cb5c3bSJohn Baldwin 
106382cb5c3bSJohn Baldwin 	/* Turn the power on via the Power Controller if a card is inserted. */
106482cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) {
106582cb5c3bSJohn Baldwin 		mask |= PCIEM_SLOT_CTL_PCC;
106682cb5c3bSJohn Baldwin 		if (card_inserted)
106782cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PC_ON;
106882cb5c3bSJohn Baldwin 		else
106982cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PC_OFF;
107082cb5c3bSJohn Baldwin 	}
107182cb5c3bSJohn Baldwin 
107282cb5c3bSJohn Baldwin 	/*
107382cb5c3bSJohn Baldwin 	 * If a card is inserted, enable the Electromechanical
107482cb5c3bSJohn Baldwin 	 * Interlock.  If a card is not inserted (or we are in the
107582cb5c3bSJohn Baldwin 	 * process of detaching), disable the Electromechanical
107682cb5c3bSJohn Baldwin 	 * Interlock.
107782cb5c3bSJohn Baldwin 	 */
107882cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP) {
107982cb5c3bSJohn Baldwin 		mask |= PCIEM_SLOT_CTL_EIC;
108007454911SJohn Baldwin 		if (card_inserted !=
108107454911SJohn Baldwin 		    !(sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS))
108282cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_EIC;
108382cb5c3bSJohn Baldwin 	}
108482cb5c3bSJohn Baldwin 
108582cb5c3bSJohn Baldwin 	/*
108682cb5c3bSJohn Baldwin 	 * Start a timer to see if the Data Link Layer times out.
108782cb5c3bSJohn Baldwin 	 * Note that we only start the timer if Presence Detect
108882cb5c3bSJohn Baldwin 	 * changed on this interrupt.  Stop any scheduled timer if
108982cb5c3bSJohn Baldwin 	 * the Data Link Layer is active.
109082cb5c3bSJohn Baldwin 	 */
109182cb5c3bSJohn Baldwin 	if (sc->pcie_link_cap & PCIEM_LINK_CAP_DL_ACTIVE) {
109282cb5c3bSJohn Baldwin 		if (card_inserted &&
109382cb5c3bSJohn Baldwin 		    !(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) &&
109482cb5c3bSJohn Baldwin 		    sc->pcie_slot_sta & PCIEM_SLOT_STA_PDC) {
109582cb5c3bSJohn Baldwin 			if (cold)
109682cb5c3bSJohn Baldwin 				device_printf(sc->dev,
109782cb5c3bSJohn Baldwin 				    "Data Link Layer inactive\n");
109882cb5c3bSJohn Baldwin 			else
109982cb5c3bSJohn Baldwin 				callout_reset(&sc->pcie_dll_timer, hz,
110082cb5c3bSJohn Baldwin 				    pcib_pcie_dll_timeout, sc);
110182cb5c3bSJohn Baldwin 		} else if (sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE)
110282cb5c3bSJohn Baldwin 			callout_stop(&sc->pcie_dll_timer);
110382cb5c3bSJohn Baldwin 	}
110482cb5c3bSJohn Baldwin 
110582cb5c3bSJohn Baldwin 	pcib_pcie_hotplug_command(sc, val, mask);
110682cb5c3bSJohn Baldwin 
110782cb5c3bSJohn Baldwin 	/*
110882cb5c3bSJohn Baldwin 	 * During attach the child "pci" device is added sychronously;
110982cb5c3bSJohn Baldwin 	 * otherwise, the task is scheduled to manage the child
111082cb5c3bSJohn Baldwin 	 * device.
111182cb5c3bSJohn Baldwin 	 */
111282cb5c3bSJohn Baldwin 	if (schedule_task &&
111382cb5c3bSJohn Baldwin 	    (pcib_hotplug_present(sc) != 0) != (sc->child != NULL))
111482cb5c3bSJohn Baldwin 		taskqueue_enqueue(taskqueue_thread, &sc->pcie_hp_task);
111582cb5c3bSJohn Baldwin }
111682cb5c3bSJohn Baldwin 
111782cb5c3bSJohn Baldwin static void
111882cb5c3bSJohn Baldwin pcib_pcie_intr(void *arg)
111982cb5c3bSJohn Baldwin {
112082cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
112182cb5c3bSJohn Baldwin 	device_t dev;
112282cb5c3bSJohn Baldwin 
112382cb5c3bSJohn Baldwin 	sc = arg;
112482cb5c3bSJohn Baldwin 	dev = sc->dev;
112582cb5c3bSJohn Baldwin 	sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
112682cb5c3bSJohn Baldwin 
112782cb5c3bSJohn Baldwin 	/* Clear the events just reported. */
112882cb5c3bSJohn Baldwin 	pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2);
112982cb5c3bSJohn Baldwin 
113082cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_ABP) {
113182cb5c3bSJohn Baldwin 		if (sc->flags & PCIB_DETACH_PENDING) {
113282cb5c3bSJohn Baldwin 			device_printf(dev,
113382cb5c3bSJohn Baldwin 			    "Attention Button Pressed: Detach Cancelled\n");
113482cb5c3bSJohn Baldwin 			sc->flags &= ~PCIB_DETACH_PENDING;
113582cb5c3bSJohn Baldwin 			callout_stop(&sc->pcie_ab_timer);
113682cb5c3bSJohn Baldwin 		} else {
113782cb5c3bSJohn Baldwin 			device_printf(dev,
113882cb5c3bSJohn Baldwin 		    "Attention Button Pressed: Detaching in 5 seconds\n");
113982cb5c3bSJohn Baldwin 			sc->flags |= PCIB_DETACH_PENDING;
114082cb5c3bSJohn Baldwin 			callout_reset(&sc->pcie_ab_timer, 5 * hz,
114182cb5c3bSJohn Baldwin 			    pcib_pcie_ab_timeout, sc);
114282cb5c3bSJohn Baldwin 		}
114382cb5c3bSJohn Baldwin 	}
114482cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD)
114582cb5c3bSJohn Baldwin 		device_printf(dev, "Power Fault Detected\n");
114682cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSC)
114782cb5c3bSJohn Baldwin 		device_printf(dev, "MRL Sensor Changed to %s\n",
114882cb5c3bSJohn Baldwin 		    sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS ? "open" :
114982cb5c3bSJohn Baldwin 		    "closed");
115082cb5c3bSJohn Baldwin 	if (bootverbose && sc->pcie_slot_sta & PCIEM_SLOT_STA_PDC)
115182cb5c3bSJohn Baldwin 		device_printf(dev, "Present Detect Changed to %s\n",
115282cb5c3bSJohn Baldwin 		    sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS ? "card present" :
115382cb5c3bSJohn Baldwin 		    "empty");
115482cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_CC)
115582cb5c3bSJohn Baldwin 		pcib_pcie_hotplug_command_completed(sc);
115682cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_DLLSC) {
115782cb5c3bSJohn Baldwin 		sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
115882cb5c3bSJohn Baldwin 		if (bootverbose)
115982cb5c3bSJohn Baldwin 			device_printf(dev,
116082cb5c3bSJohn Baldwin 			    "Data Link Layer State Changed to %s\n",
116182cb5c3bSJohn Baldwin 			    sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE ?
116282cb5c3bSJohn Baldwin 			    "active" : "inactive");
116382cb5c3bSJohn Baldwin 	}
116482cb5c3bSJohn Baldwin 
116582cb5c3bSJohn Baldwin 	pcib_pcie_hotplug_update(sc, 0, 0, true);
116682cb5c3bSJohn Baldwin }
116782cb5c3bSJohn Baldwin 
116882cb5c3bSJohn Baldwin static void
116982cb5c3bSJohn Baldwin pcib_pcie_hotplug_task(void *context, int pending)
117082cb5c3bSJohn Baldwin {
117182cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
117282cb5c3bSJohn Baldwin 	device_t dev;
117382cb5c3bSJohn Baldwin 
117482cb5c3bSJohn Baldwin 	sc = context;
117582cb5c3bSJohn Baldwin 	mtx_lock(&Giant);
117682cb5c3bSJohn Baldwin 	dev = sc->dev;
117782cb5c3bSJohn Baldwin 	if (pcib_hotplug_present(sc) != 0) {
117882cb5c3bSJohn Baldwin 		if (sc->child == NULL) {
117982cb5c3bSJohn Baldwin 			sc->child = device_add_child(dev, "pci", -1);
118082cb5c3bSJohn Baldwin 			bus_generic_attach(dev);
118182cb5c3bSJohn Baldwin 		}
118282cb5c3bSJohn Baldwin 	} else {
118382cb5c3bSJohn Baldwin 		if (sc->child != NULL) {
118482cb5c3bSJohn Baldwin 			if (device_delete_child(dev, sc->child) == 0)
118582cb5c3bSJohn Baldwin 				sc->child = NULL;
118682cb5c3bSJohn Baldwin 		}
118782cb5c3bSJohn Baldwin 	}
118882cb5c3bSJohn Baldwin 	mtx_unlock(&Giant);
118982cb5c3bSJohn Baldwin }
119082cb5c3bSJohn Baldwin 
119182cb5c3bSJohn Baldwin static void
119282cb5c3bSJohn Baldwin pcib_pcie_ab_timeout(void *arg)
119382cb5c3bSJohn Baldwin {
119482cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
119582cb5c3bSJohn Baldwin 	device_t dev;
119682cb5c3bSJohn Baldwin 
119782cb5c3bSJohn Baldwin 	sc = arg;
119882cb5c3bSJohn Baldwin 	dev = sc->dev;
119982cb5c3bSJohn Baldwin 	mtx_assert(&Giant, MA_OWNED);
120082cb5c3bSJohn Baldwin 	if (sc->flags & PCIB_DETACH_PENDING) {
120182cb5c3bSJohn Baldwin 		sc->flags |= PCIB_DETACHING;
120282cb5c3bSJohn Baldwin 		sc->flags &= ~PCIB_DETACH_PENDING;
120382cb5c3bSJohn Baldwin 		pcib_pcie_hotplug_update(sc, 0, 0, true);
120482cb5c3bSJohn Baldwin 	}
120582cb5c3bSJohn Baldwin }
120682cb5c3bSJohn Baldwin 
120782cb5c3bSJohn Baldwin static void
120882cb5c3bSJohn Baldwin pcib_pcie_cc_timeout(void *arg)
120982cb5c3bSJohn Baldwin {
121082cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
121182cb5c3bSJohn Baldwin 	device_t dev;
1212*6f33eaa5SJohn Baldwin 	uint16_t sta;
121382cb5c3bSJohn Baldwin 
121482cb5c3bSJohn Baldwin 	sc = arg;
121582cb5c3bSJohn Baldwin 	dev = sc->dev;
121682cb5c3bSJohn Baldwin 	mtx_assert(&Giant, MA_OWNED);
1217*6f33eaa5SJohn Baldwin 	sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
1218*6f33eaa5SJohn Baldwin 	if (!(sta & PCIEM_SLOT_STA_CC)) {
121982cb5c3bSJohn Baldwin 		device_printf(dev,
122082cb5c3bSJohn Baldwin 		    "Hotplug Command Timed Out - forcing detach\n");
122182cb5c3bSJohn Baldwin 		sc->flags &= ~(PCIB_HOTPLUG_CMD_PENDING | PCIB_DETACH_PENDING);
122282cb5c3bSJohn Baldwin 		sc->flags |= PCIB_DETACHING;
122382cb5c3bSJohn Baldwin 		pcib_pcie_hotplug_update(sc, 0, 0, true);
1224*6f33eaa5SJohn Baldwin 	} else {
1225*6f33eaa5SJohn Baldwin 		device_printf(dev,
1226*6f33eaa5SJohn Baldwin 	    "Missed HotPlug interrupt waiting for Command Completion\n");
1227*6f33eaa5SJohn Baldwin 		pcib_pcie_intr(sc);
122882cb5c3bSJohn Baldwin 	}
122982cb5c3bSJohn Baldwin }
123082cb5c3bSJohn Baldwin 
123182cb5c3bSJohn Baldwin static void
123282cb5c3bSJohn Baldwin pcib_pcie_dll_timeout(void *arg)
123382cb5c3bSJohn Baldwin {
123482cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
123582cb5c3bSJohn Baldwin 	device_t dev;
123682cb5c3bSJohn Baldwin 	uint16_t sta;
123782cb5c3bSJohn Baldwin 
123882cb5c3bSJohn Baldwin 	sc = arg;
123982cb5c3bSJohn Baldwin 	dev = sc->dev;
124082cb5c3bSJohn Baldwin 	mtx_assert(&Giant, MA_OWNED);
124182cb5c3bSJohn Baldwin 	sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
124282cb5c3bSJohn Baldwin 	if (!(sta & PCIEM_LINK_STA_DL_ACTIVE)) {
124382cb5c3bSJohn Baldwin 		device_printf(dev,
124482cb5c3bSJohn Baldwin 		    "Timed out waiting for Data Link Layer Active\n");
124582cb5c3bSJohn Baldwin 		sc->flags |= PCIB_DETACHING;
124682cb5c3bSJohn Baldwin 		pcib_pcie_hotplug_update(sc, 0, 0, true);
124782cb5c3bSJohn Baldwin 	} else if (sta != sc->pcie_link_sta) {
124882cb5c3bSJohn Baldwin 		device_printf(dev,
124982cb5c3bSJohn Baldwin 		    "Missed HotPlug interrupt waiting for DLL Active\n");
125082cb5c3bSJohn Baldwin 		pcib_pcie_intr(sc);
125182cb5c3bSJohn Baldwin 	}
125282cb5c3bSJohn Baldwin }
125382cb5c3bSJohn Baldwin 
125482cb5c3bSJohn Baldwin static int
125582cb5c3bSJohn Baldwin pcib_alloc_pcie_irq(struct pcib_softc *sc)
125682cb5c3bSJohn Baldwin {
125782cb5c3bSJohn Baldwin 	device_t dev;
125882cb5c3bSJohn Baldwin 	int count, error, rid;
125982cb5c3bSJohn Baldwin 
126082cb5c3bSJohn Baldwin 	rid = -1;
126182cb5c3bSJohn Baldwin 	dev = sc->dev;
126282cb5c3bSJohn Baldwin 
126382cb5c3bSJohn Baldwin 	/*
126482cb5c3bSJohn Baldwin 	 * For simplicity, only use MSI-X if there is a single message.
126582cb5c3bSJohn Baldwin 	 * To support a device with multiple messages we would have to
126682cb5c3bSJohn Baldwin 	 * use remap intr if the MSI number is not 0.
126782cb5c3bSJohn Baldwin 	 */
126882cb5c3bSJohn Baldwin 	count = pci_msix_count(dev);
126982cb5c3bSJohn Baldwin 	if (count == 1) {
127082cb5c3bSJohn Baldwin 		error = pci_alloc_msix(dev, &count);
127182cb5c3bSJohn Baldwin 		if (error == 0)
127282cb5c3bSJohn Baldwin 			rid = 1;
127382cb5c3bSJohn Baldwin 	}
127482cb5c3bSJohn Baldwin 
127582cb5c3bSJohn Baldwin 	if (rid < 0 && pci_msi_count(dev) > 0) {
127682cb5c3bSJohn Baldwin 		count = 1;
127782cb5c3bSJohn Baldwin 		error = pci_alloc_msi(dev, &count);
127882cb5c3bSJohn Baldwin 		if (error == 0)
127982cb5c3bSJohn Baldwin 			rid = 1;
128082cb5c3bSJohn Baldwin 	}
128182cb5c3bSJohn Baldwin 
128282cb5c3bSJohn Baldwin 	if (rid < 0)
128382cb5c3bSJohn Baldwin 		rid = 0;
128482cb5c3bSJohn Baldwin 
128582cb5c3bSJohn Baldwin 	sc->pcie_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
128682cb5c3bSJohn Baldwin 	    RF_ACTIVE);
128782cb5c3bSJohn Baldwin 	if (sc->pcie_irq == NULL) {
128882cb5c3bSJohn Baldwin 		device_printf(dev,
128982cb5c3bSJohn Baldwin 		    "Failed to allocate interrupt for PCI-e events\n");
129082cb5c3bSJohn Baldwin 		if (rid > 0)
129182cb5c3bSJohn Baldwin 			pci_release_msi(dev);
129282cb5c3bSJohn Baldwin 		return (ENXIO);
129382cb5c3bSJohn Baldwin 	}
129482cb5c3bSJohn Baldwin 
129582cb5c3bSJohn Baldwin 	error = bus_setup_intr(dev, sc->pcie_irq, INTR_TYPE_MISC,
129682cb5c3bSJohn Baldwin 	    NULL, pcib_pcie_intr, sc, &sc->pcie_ihand);
129782cb5c3bSJohn Baldwin 	if (error) {
129882cb5c3bSJohn Baldwin 		device_printf(dev, "Failed to setup PCI-e interrupt handler\n");
129982cb5c3bSJohn Baldwin 		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->pcie_irq);
130082cb5c3bSJohn Baldwin 		if (rid > 0)
130182cb5c3bSJohn Baldwin 			pci_release_msi(dev);
130282cb5c3bSJohn Baldwin 		return (error);
130382cb5c3bSJohn Baldwin 	}
130482cb5c3bSJohn Baldwin 	return (0);
130582cb5c3bSJohn Baldwin }
130682cb5c3bSJohn Baldwin 
1307*6f33eaa5SJohn Baldwin static int
1308*6f33eaa5SJohn Baldwin pcib_release_pcie_irq(struct pcib_softc *sc)
1309*6f33eaa5SJohn Baldwin {
1310*6f33eaa5SJohn Baldwin 	device_t dev;
1311*6f33eaa5SJohn Baldwin 	int error;
1312*6f33eaa5SJohn Baldwin 
1313*6f33eaa5SJohn Baldwin 	dev = sc->dev;
1314*6f33eaa5SJohn Baldwin 	error = bus_teardown_intr(dev, sc->pcie_irq, sc->pcie_ihand);
1315*6f33eaa5SJohn Baldwin 	if (error)
1316*6f33eaa5SJohn Baldwin 		return (error);
1317*6f33eaa5SJohn Baldwin 	error = bus_free_resource(dev, SYS_RES_IRQ, sc->pcie_irq);
1318*6f33eaa5SJohn Baldwin 	if (error)
1319*6f33eaa5SJohn Baldwin 		return (error);
1320*6f33eaa5SJohn Baldwin 	return (pci_release_msi(dev));
1321*6f33eaa5SJohn Baldwin }
1322*6f33eaa5SJohn Baldwin 
132382cb5c3bSJohn Baldwin static void
132482cb5c3bSJohn Baldwin pcib_setup_hotplug(struct pcib_softc *sc)
132582cb5c3bSJohn Baldwin {
132682cb5c3bSJohn Baldwin 	device_t dev;
132782cb5c3bSJohn Baldwin 	uint16_t mask, val;
132882cb5c3bSJohn Baldwin 
132982cb5c3bSJohn Baldwin 	dev = sc->dev;
133082cb5c3bSJohn Baldwin 	callout_init(&sc->pcie_ab_timer, 0);
133182cb5c3bSJohn Baldwin 	callout_init(&sc->pcie_cc_timer, 0);
133282cb5c3bSJohn Baldwin 	callout_init(&sc->pcie_dll_timer, 0);
133382cb5c3bSJohn Baldwin 	TASK_INIT(&sc->pcie_hp_task, 0, pcib_pcie_hotplug_task, sc);
133482cb5c3bSJohn Baldwin 
133582cb5c3bSJohn Baldwin 	/* Allocate IRQ. */
133682cb5c3bSJohn Baldwin 	if (pcib_alloc_pcie_irq(sc) != 0)
133782cb5c3bSJohn Baldwin 		return;
133882cb5c3bSJohn Baldwin 
133982cb5c3bSJohn Baldwin 	sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
134082cb5c3bSJohn Baldwin 	sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
134182cb5c3bSJohn Baldwin 
1342*6f33eaa5SJohn Baldwin 	/* Clear any events previously pending. */
1343*6f33eaa5SJohn Baldwin 	pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2);
1344*6f33eaa5SJohn Baldwin 
134582cb5c3bSJohn Baldwin 	/* Enable HotPlug events. */
134682cb5c3bSJohn Baldwin 	mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE |
134782cb5c3bSJohn Baldwin 	    PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE |
134882cb5c3bSJohn Baldwin 	    PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE;
134982cb5c3bSJohn Baldwin 	val = PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_HPIE;
135082cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_APB)
135182cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_ABPE;
135282cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP)
135382cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_PFDE;
135482cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP)
135582cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_MRLSCE;
135682cb5c3bSJohn Baldwin 	if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS))
135782cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_CCIE;
135882cb5c3bSJohn Baldwin 	if (sc->pcie_link_cap & PCIEM_LINK_CAP_DL_ACTIVE)
135982cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_DLLSCE;
136082cb5c3bSJohn Baldwin 
136182cb5c3bSJohn Baldwin 	/* Turn the attention indicator off. */
136282cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) {
136382cb5c3bSJohn Baldwin 		mask |= PCIEM_SLOT_CTL_AIC;
136482cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_AI_OFF;
136582cb5c3bSJohn Baldwin 	}
136682cb5c3bSJohn Baldwin 
136782cb5c3bSJohn Baldwin 	pcib_pcie_hotplug_update(sc, val, mask, false);
136882cb5c3bSJohn Baldwin }
1369*6f33eaa5SJohn Baldwin 
1370*6f33eaa5SJohn Baldwin static int
1371*6f33eaa5SJohn Baldwin pcib_detach_hotplug(struct pcib_softc *sc)
1372*6f33eaa5SJohn Baldwin {
1373*6f33eaa5SJohn Baldwin 	uint16_t mask, val;
1374*6f33eaa5SJohn Baldwin 	int error;
1375*6f33eaa5SJohn Baldwin 
1376*6f33eaa5SJohn Baldwin 	/* Disable the card in the slot and force it to detach. */
1377*6f33eaa5SJohn Baldwin 	if (sc->flags & PCIB_DETACH_PENDING) {
1378*6f33eaa5SJohn Baldwin 		sc->flags &= ~PCIB_DETACH_PENDING;
1379*6f33eaa5SJohn Baldwin 		callout_stop(&sc->pcie_ab_timer);
1380*6f33eaa5SJohn Baldwin 	}
1381*6f33eaa5SJohn Baldwin 	sc->flags |= PCIB_DETACHING;
1382*6f33eaa5SJohn Baldwin 
1383*6f33eaa5SJohn Baldwin 	if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) {
1384*6f33eaa5SJohn Baldwin 		callout_stop(&sc->pcie_cc_timer);
1385*6f33eaa5SJohn Baldwin 		tsleep(sc, 0, "hpcmd", hz);
1386*6f33eaa5SJohn Baldwin 		sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING;
1387*6f33eaa5SJohn Baldwin 	}
1388*6f33eaa5SJohn Baldwin 
1389*6f33eaa5SJohn Baldwin 	/* Disable HotPlug events. */
1390*6f33eaa5SJohn Baldwin 	mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE |
1391*6f33eaa5SJohn Baldwin 	    PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE |
1392*6f33eaa5SJohn Baldwin 	    PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE;
1393*6f33eaa5SJohn Baldwin 	val = 0;
1394*6f33eaa5SJohn Baldwin 
1395*6f33eaa5SJohn Baldwin 	/* Turn the attention indicator off. */
1396*6f33eaa5SJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) {
1397*6f33eaa5SJohn Baldwin 		mask |= PCIEM_SLOT_CTL_AIC;
1398*6f33eaa5SJohn Baldwin 		val |= PCIEM_SLOT_CTL_AI_OFF;
1399*6f33eaa5SJohn Baldwin 	}
1400*6f33eaa5SJohn Baldwin 
1401*6f33eaa5SJohn Baldwin 	pcib_pcie_hotplug_update(sc, val, mask, false);
1402*6f33eaa5SJohn Baldwin 
1403*6f33eaa5SJohn Baldwin 	error = pcib_release_pcie_irq(sc);
1404*6f33eaa5SJohn Baldwin 	if (error)
1405*6f33eaa5SJohn Baldwin 		return (error);
1406*6f33eaa5SJohn Baldwin 	taskqueue_drain(taskqueue_thread, &sc->pcie_hp_task);
1407*6f33eaa5SJohn Baldwin 	callout_drain(&sc->pcie_ab_timer);
1408*6f33eaa5SJohn Baldwin 	callout_drain(&sc->pcie_cc_timer);
1409*6f33eaa5SJohn Baldwin 	callout_drain(&sc->pcie_dll_timer);
1410*6f33eaa5SJohn Baldwin 	return (0);
1411*6f33eaa5SJohn Baldwin }
141282cb5c3bSJohn Baldwin #endif
141382cb5c3bSJohn Baldwin 
1414e36af292SJung-uk Kim /*
1415e36af292SJung-uk Kim  * Get current bridge configuration.
1416e36af292SJung-uk Kim  */
1417e36af292SJung-uk Kim static void
1418e36af292SJung-uk Kim pcib_cfg_save(struct pcib_softc *sc)
1419e36af292SJung-uk Kim {
1420ad6f36f8SJohn Baldwin #ifndef NEW_PCIB
1421e36af292SJung-uk Kim 	device_t	dev;
1422ad6f36f8SJohn Baldwin 	uint16_t command;
1423e36af292SJung-uk Kim 
1424e36af292SJung-uk Kim 	dev = sc->dev;
1425e36af292SJung-uk Kim 
1426ad6f36f8SJohn Baldwin 	command = pci_read_config(dev, PCIR_COMMAND, 2);
1427ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_PORTEN)
1428e36af292SJung-uk Kim 		pcib_get_io_decode(sc);
1429ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_MEMEN)
1430e36af292SJung-uk Kim 		pcib_get_mem_decode(sc);
143183c41143SJohn Baldwin #endif
1432e36af292SJung-uk Kim }
1433e36af292SJung-uk Kim 
1434e36af292SJung-uk Kim /*
1435e36af292SJung-uk Kim  * Restore previous bridge configuration.
1436e36af292SJung-uk Kim  */
1437e36af292SJung-uk Kim static void
1438e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc)
1439e36af292SJung-uk Kim {
1440e36af292SJung-uk Kim 	device_t	dev;
1441ad6f36f8SJohn Baldwin #ifndef NEW_PCIB
1442ad6f36f8SJohn Baldwin 	uint16_t command;
1443ad6f36f8SJohn Baldwin #endif
1444e36af292SJung-uk Kim 	dev = sc->dev;
1445e36af292SJung-uk Kim 
144683c41143SJohn Baldwin #ifdef NEW_PCIB
144783c41143SJohn Baldwin 	pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM);
144883c41143SJohn Baldwin #else
1449ad6f36f8SJohn Baldwin 	command = pci_read_config(dev, PCIR_COMMAND, 2);
1450ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_PORTEN)
1451e36af292SJung-uk Kim 		pcib_set_io_decode(sc);
1452ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_MEMEN)
1453e36af292SJung-uk Kim 		pcib_set_mem_decode(sc);
145483c41143SJohn Baldwin #endif
1455e36af292SJung-uk Kim }
1456e36af292SJung-uk Kim 
1457e36af292SJung-uk Kim /*
1458bb0d0a8eSMike Smith  * Generic device interface
1459bb0d0a8eSMike Smith  */
1460bb0d0a8eSMike Smith static int
1461bb0d0a8eSMike Smith pcib_probe(device_t dev)
1462bb0d0a8eSMike Smith {
1463bb0d0a8eSMike Smith     if ((pci_get_class(dev) == PCIC_BRIDGE) &&
1464bb0d0a8eSMike Smith 	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
1465bb0d0a8eSMike Smith 	device_set_desc(dev, "PCI-PCI bridge");
1466b7cbd25bSMarcel Moolenaar 	return(-10000);
1467bb0d0a8eSMike Smith     }
1468bb0d0a8eSMike Smith     return(ENXIO);
1469bb0d0a8eSMike Smith }
1470bb0d0a8eSMike Smith 
14716f0d5884SJohn Baldwin void
14726f0d5884SJohn Baldwin pcib_attach_common(device_t dev)
1473bb0d0a8eSMike Smith {
1474bb0d0a8eSMike Smith     struct pcib_softc	*sc;
1475abf07f13SWarner Losh     struct sysctl_ctx_list *sctx;
1476abf07f13SWarner Losh     struct sysctl_oid	*soid;
1477c825d4dcSJohn Baldwin     int comma;
1478bb0d0a8eSMike Smith 
1479bb0d0a8eSMike Smith     sc = device_get_softc(dev);
1480bb0d0a8eSMike Smith     sc->dev = dev;
1481bb0d0a8eSMike Smith 
14824fa59183SMike Smith     /*
14834fa59183SMike Smith      * Get current bridge configuration.
14844fa59183SMike Smith      */
148555aaf894SMarius Strobl     sc->domain = pci_get_domain(dev);
1486ad6f36f8SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
1487ad6f36f8SJohn Baldwin     sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1);
1488ad6f36f8SJohn Baldwin     sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
1489ad6f36f8SJohn Baldwin #endif
1490ad6f36f8SJohn Baldwin     sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
1491e36af292SJung-uk Kim     pcib_cfg_save(sc);
14924fa59183SMike Smith 
14934fa59183SMike Smith     /*
14944edef187SJohn Baldwin      * The primary bus register should always be the bus of the
14954edef187SJohn Baldwin      * parent.
14964edef187SJohn Baldwin      */
14974edef187SJohn Baldwin     sc->pribus = pci_get_bus(dev);
14984edef187SJohn Baldwin     pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
14994edef187SJohn Baldwin 
15004edef187SJohn Baldwin     /*
1501abf07f13SWarner Losh      * Setup sysctl reporting nodes
1502abf07f13SWarner Losh      */
1503abf07f13SWarner Losh     sctx = device_get_sysctl_ctx(dev);
1504abf07f13SWarner Losh     soid = device_get_sysctl_tree(dev);
1505abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
1506abf07f13SWarner Losh       CTLFLAG_RD, &sc->domain, 0, "Domain number");
1507abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
1508abf07f13SWarner Losh       CTLFLAG_RD, &sc->pribus, 0, "Primary bus number");
1509abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
15104edef187SJohn Baldwin       CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number");
1511abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
15124edef187SJohn Baldwin       CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number");
1513abf07f13SWarner Losh 
1514abf07f13SWarner Losh     /*
15154fa59183SMike Smith      * Quirk handling.
15164fa59183SMike Smith      */
15174fa59183SMike Smith     switch (pci_get_devid(dev)) {
15182ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
15194fa59183SMike Smith     case 0x12258086:		/* Intel 82454KX/GX (Orion) */
15204fa59183SMike Smith 	{
1521b0cb115fSWarner Losh 	    uint8_t	supbus;
15224fa59183SMike Smith 
15234fa59183SMike Smith 	    supbus = pci_read_config(dev, 0x41, 1);
15244fa59183SMike Smith 	    if (supbus != 0xff) {
15254edef187SJohn Baldwin 		sc->bus.sec = supbus + 1;
15264edef187SJohn Baldwin 		sc->bus.sub = supbus + 1;
15274fa59183SMike Smith 	    }
15284fa59183SMike Smith 	    break;
15294fa59183SMike Smith 	}
15304edef187SJohn Baldwin #endif
15314fa59183SMike Smith 
1532e4b59fc5SWarner Losh     /*
1533e4b59fc5SWarner Losh      * The i82380FB mobile docking controller is a PCI-PCI bridge,
1534e4b59fc5SWarner Losh      * and it is a subtractive bridge.  However, the ProgIf is wrong
1535e4b59fc5SWarner Losh      * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
15364718610dSZbigniew Bodek      * happen.  There are also Toshiba and Cavium ThunderX bridges
15374718610dSZbigniew Bodek      * that behave this way.
1538e4b59fc5SWarner Losh      */
15394718610dSZbigniew Bodek     case 0xa002177d:		/* Cavium ThunderX */
1540e4b59fc5SWarner Losh     case 0x124b8086:		/* Intel 82380FB Mobile */
1541e4b59fc5SWarner Losh     case 0x060513d7:		/* Toshiba ???? */
1542e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
1543e4b59fc5SWarner Losh 	break;
1544c94d6dbeSJung-uk Kim 
15452ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
1546c94d6dbeSJung-uk Kim     /* Compaq R3000 BIOS sets wrong subordinate bus number. */
1547c94d6dbeSJung-uk Kim     case 0x00dd10de:
1548c94d6dbeSJung-uk Kim 	{
1549c94d6dbeSJung-uk Kim 	    char *cp;
1550c94d6dbeSJung-uk Kim 
15512be111bfSDavide Italiano 	    if ((cp = kern_getenv("smbios.planar.maker")) == NULL)
1552c94d6dbeSJung-uk Kim 		break;
15531def0ca6SJung-uk Kim 	    if (strncmp(cp, "Compal", 6) != 0) {
15541def0ca6SJung-uk Kim 		freeenv(cp);
1555c94d6dbeSJung-uk Kim 		break;
15561def0ca6SJung-uk Kim 	    }
15571def0ca6SJung-uk Kim 	    freeenv(cp);
15582be111bfSDavide Italiano 	    if ((cp = kern_getenv("smbios.planar.product")) == NULL)
15591def0ca6SJung-uk Kim 		break;
15601def0ca6SJung-uk Kim 	    if (strncmp(cp, "08A0", 4) != 0) {
15611def0ca6SJung-uk Kim 		freeenv(cp);
15621def0ca6SJung-uk Kim 		break;
15631def0ca6SJung-uk Kim 	    }
15641def0ca6SJung-uk Kim 	    freeenv(cp);
15654edef187SJohn Baldwin 	    if (sc->bus.sub < 0xa) {
1566c94d6dbeSJung-uk Kim 		pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
15674edef187SJohn Baldwin 		sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
1568c94d6dbeSJung-uk Kim 	    }
1569c94d6dbeSJung-uk Kim 	    break;
1570c94d6dbeSJung-uk Kim 	}
15714edef187SJohn Baldwin #endif
1572e4b59fc5SWarner Losh     }
1573e4b59fc5SWarner Losh 
157422bf1c7fSJohn Baldwin     if (pci_msi_device_blacklisted(dev))
157522bf1c7fSJohn Baldwin 	sc->flags |= PCIB_DISABLE_MSI;
157622bf1c7fSJohn Baldwin 
157768e9cbd3SMarius Strobl     if (pci_msix_device_blacklisted(dev))
157868e9cbd3SMarius Strobl 	sc->flags |= PCIB_DISABLE_MSIX;
157968e9cbd3SMarius Strobl 
1580e4b59fc5SWarner Losh     /*
1581e4b59fc5SWarner Losh      * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
1582e4b59fc5SWarner Losh      * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
1583e4b59fc5SWarner Losh      * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
1584e4b59fc5SWarner Losh      * This means they act as if they were subtractively decoding
1585e4b59fc5SWarner Losh      * bridges and pass all transactions.  Mark them and real ProgIf 1
1586e4b59fc5SWarner Losh      * parts as subtractive.
1587e4b59fc5SWarner Losh      */
1588e4b59fc5SWarner Losh     if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
1589657d9f9fSJohn Baldwin       pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
1590e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
1591e4b59fc5SWarner Losh 
159282cb5c3bSJohn Baldwin #ifdef PCI_HP
159382cb5c3bSJohn Baldwin     pcib_probe_hotplug(sc);
159482cb5c3bSJohn Baldwin #endif
159583c41143SJohn Baldwin #ifdef NEW_PCIB
15964edef187SJohn Baldwin #ifdef PCI_RES_BUS
15974edef187SJohn Baldwin     pcib_setup_secbus(dev, &sc->bus, 1);
15984edef187SJohn Baldwin #endif
159983c41143SJohn Baldwin     pcib_probe_windows(sc);
160083c41143SJohn Baldwin #endif
160182cb5c3bSJohn Baldwin #ifdef PCI_HP
160282cb5c3bSJohn Baldwin     if (sc->flags & PCIB_HOTPLUG)
160382cb5c3bSJohn Baldwin 	    pcib_setup_hotplug(sc);
160482cb5c3bSJohn Baldwin #endif
1605bb0d0a8eSMike Smith     if (bootverbose) {
160655aaf894SMarius Strobl 	device_printf(dev, "  domain            %d\n", sc->domain);
16074edef187SJohn Baldwin 	device_printf(dev, "  secondary bus     %d\n", sc->bus.sec);
16084edef187SJohn Baldwin 	device_printf(dev, "  subordinate bus   %d\n", sc->bus.sub);
160983c41143SJohn Baldwin #ifdef NEW_PCIB
161083c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->io))
161183c41143SJohn Baldwin 	    device_printf(dev, "  I/O decode        0x%jx-0x%jx\n",
161283c41143SJohn Baldwin 	      (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit);
161383c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->mem))
161483c41143SJohn Baldwin 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
161583c41143SJohn Baldwin 	      (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit);
161683c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->pmem))
161783c41143SJohn Baldwin 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
161883c41143SJohn Baldwin 	      (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit);
161983c41143SJohn Baldwin #else
162083c41143SJohn Baldwin 	if (pcib_is_io_open(sc))
162183c41143SJohn Baldwin 	    device_printf(dev, "  I/O decode        0x%x-0x%x\n",
162283c41143SJohn Baldwin 	      sc->iobase, sc->iolimit);
1623b0a2d4b8SWarner Losh 	if (pcib_is_nonprefetch_open(sc))
1624b0a2d4b8SWarner Losh 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
1625b0a2d4b8SWarner Losh 	      (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
1626b0a2d4b8SWarner Losh 	if (pcib_is_prefetch_open(sc))
1627b0a2d4b8SWarner Losh 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
1628b0a2d4b8SWarner Losh 	      (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
162983c41143SJohn Baldwin #endif
1630c825d4dcSJohn Baldwin 	if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) ||
1631c825d4dcSJohn Baldwin 	    sc->flags & PCIB_SUBTRACTIVE) {
1632c825d4dcSJohn Baldwin 		device_printf(dev, "  special decode    ");
1633c825d4dcSJohn Baldwin 		comma = 0;
1634c825d4dcSJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) {
1635c825d4dcSJohn Baldwin 			printf("ISA");
1636c825d4dcSJohn Baldwin 			comma = 1;
1637c825d4dcSJohn Baldwin 		}
1638c825d4dcSJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) {
1639c825d4dcSJohn Baldwin 			printf("%sVGA", comma ? ", " : "");
1640c825d4dcSJohn Baldwin 			comma = 1;
1641c825d4dcSJohn Baldwin 		}
1642e4b59fc5SWarner Losh 		if (sc->flags & PCIB_SUBTRACTIVE)
1643c825d4dcSJohn Baldwin 			printf("%ssubtractive", comma ? ", " : "");
1644c825d4dcSJohn Baldwin 		printf("\n");
1645c825d4dcSJohn Baldwin 	}
1646bb0d0a8eSMike Smith     }
1647bb0d0a8eSMike Smith 
1648bb0d0a8eSMike Smith     /*
1649ef888152SJohn Baldwin      * Always enable busmastering on bridges so that transactions
1650ef888152SJohn Baldwin      * initiated on the secondary bus are passed through to the
1651ef888152SJohn Baldwin      * primary bus.
1652ef888152SJohn Baldwin      */
1653ef888152SJohn Baldwin     pci_enable_busmaster(dev);
16546f0d5884SJohn Baldwin }
1655bb0d0a8eSMike Smith 
165682cb5c3bSJohn Baldwin #ifdef PCI_HP
165782cb5c3bSJohn Baldwin static int
165882cb5c3bSJohn Baldwin pcib_present(struct pcib_softc *sc)
165982cb5c3bSJohn Baldwin {
166082cb5c3bSJohn Baldwin 
166182cb5c3bSJohn Baldwin 	if (sc->flags & PCIB_HOTPLUG)
166282cb5c3bSJohn Baldwin 		return (pcib_hotplug_present(sc) != 0);
166382cb5c3bSJohn Baldwin 	return (1);
166482cb5c3bSJohn Baldwin }
166582cb5c3bSJohn Baldwin #endif
166682cb5c3bSJohn Baldwin 
166738906aedSJohn Baldwin int
166867e7d085SJohn Baldwin pcib_attach_child(device_t dev)
16696f0d5884SJohn Baldwin {
16706f0d5884SJohn Baldwin 	struct pcib_softc *sc;
16716f0d5884SJohn Baldwin 
16726f0d5884SJohn Baldwin 	sc = device_get_softc(dev);
167367e7d085SJohn Baldwin 	if (sc->bus.sec == 0) {
167467e7d085SJohn Baldwin 		/* no secondary bus; we should have fixed this */
167567e7d085SJohn Baldwin 		return(0);
167667e7d085SJohn Baldwin 	}
167767e7d085SJohn Baldwin 
167882cb5c3bSJohn Baldwin #ifdef PCI_HP
167982cb5c3bSJohn Baldwin 	if (!pcib_present(sc)) {
168082cb5c3bSJohn Baldwin 		/* An empty HotPlug slot, so don't add a PCI bus yet. */
168182cb5c3bSJohn Baldwin 		return (0);
168282cb5c3bSJohn Baldwin 	}
168382cb5c3bSJohn Baldwin #endif
168482cb5c3bSJohn Baldwin 
168567e7d085SJohn Baldwin 	sc->child = device_add_child(dev, "pci", -1);
1686bb0d0a8eSMike Smith 	return (bus_generic_attach(dev));
1687bb0d0a8eSMike Smith }
1688bb0d0a8eSMike Smith 
168967e7d085SJohn Baldwin int
169067e7d085SJohn Baldwin pcib_attach(device_t dev)
169167e7d085SJohn Baldwin {
169267e7d085SJohn Baldwin 
169367e7d085SJohn Baldwin     pcib_attach_common(dev);
169467e7d085SJohn Baldwin     return (pcib_attach_child(dev));
1695bb0d0a8eSMike Smith }
1696bb0d0a8eSMike Smith 
16976f0d5884SJohn Baldwin int
1698*6f33eaa5SJohn Baldwin pcib_detach(device_t dev)
1699*6f33eaa5SJohn Baldwin {
1700*6f33eaa5SJohn Baldwin #if defined(PCI_HP) || defined(NEW_PCIB)
1701*6f33eaa5SJohn Baldwin 	struct pcib_softc *sc;
1702*6f33eaa5SJohn Baldwin #endif
1703*6f33eaa5SJohn Baldwin 	int error;
1704*6f33eaa5SJohn Baldwin 
1705*6f33eaa5SJohn Baldwin #if defined(PCI_HP) || defined(NEW_PCIB)
1706*6f33eaa5SJohn Baldwin 	sc = device_get_softc(dev);
1707*6f33eaa5SJohn Baldwin #endif
1708*6f33eaa5SJohn Baldwin 	error = bus_generic_detach(dev);
1709*6f33eaa5SJohn Baldwin 	if (error)
1710*6f33eaa5SJohn Baldwin 		return (error);
1711*6f33eaa5SJohn Baldwin #ifdef PCI_HP
1712*6f33eaa5SJohn Baldwin 	if (sc->flags & PCIB_HOTPLUG) {
1713*6f33eaa5SJohn Baldwin 		error = pcib_detach_hotplug(sc);
1714*6f33eaa5SJohn Baldwin 		if (error)
1715*6f33eaa5SJohn Baldwin 			return (error);
1716*6f33eaa5SJohn Baldwin 	}
1717*6f33eaa5SJohn Baldwin #endif
1718*6f33eaa5SJohn Baldwin 	error = device_delete_children(dev);
1719*6f33eaa5SJohn Baldwin 	if (error)
1720*6f33eaa5SJohn Baldwin 		return (error);
1721*6f33eaa5SJohn Baldwin #ifdef NEW_PCIB
1722*6f33eaa5SJohn Baldwin 	pcib_free_windows(sc);
1723*6f33eaa5SJohn Baldwin #ifdef PCI_RES_BUS
1724*6f33eaa5SJohn Baldwin 	pcib_free_secbus(dev, &sc->bus);
1725*6f33eaa5SJohn Baldwin #endif
1726*6f33eaa5SJohn Baldwin #endif
1727*6f33eaa5SJohn Baldwin 	return (0);
1728*6f33eaa5SJohn Baldwin }
1729*6f33eaa5SJohn Baldwin 
1730*6f33eaa5SJohn Baldwin int
1731e36af292SJung-uk Kim pcib_suspend(device_t dev)
1732e36af292SJung-uk Kim {
1733e36af292SJung-uk Kim 
1734e36af292SJung-uk Kim 	pcib_cfg_save(device_get_softc(dev));
17357212fc6aSJohn Baldwin 	return (bus_generic_suspend(dev));
1736e36af292SJung-uk Kim }
1737e36af292SJung-uk Kim 
1738e36af292SJung-uk Kim int
1739e36af292SJung-uk Kim pcib_resume(device_t dev)
1740e36af292SJung-uk Kim {
1741e36af292SJung-uk Kim 
1742e36af292SJung-uk Kim 	pcib_cfg_restore(device_get_softc(dev));
1743e36af292SJung-uk Kim 	return (bus_generic_resume(dev));
1744e36af292SJung-uk Kim }
1745e36af292SJung-uk Kim 
1746809923caSJustin Hibbits void
1747809923caSJustin Hibbits pcib_bridge_init(device_t dev)
1748809923caSJustin Hibbits {
1749809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
1750809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2);
1751809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1);
1752809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2);
1753809923caSJustin Hibbits 	pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2);
1754809923caSJustin Hibbits 	pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2);
1755809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
1756809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4);
1757809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2);
1758809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4);
1759809923caSJustin Hibbits }
1760809923caSJustin Hibbits 
1761e36af292SJung-uk Kim int
176282cb5c3bSJohn Baldwin pcib_child_present(device_t dev, device_t child)
176382cb5c3bSJohn Baldwin {
176482cb5c3bSJohn Baldwin #ifdef PCI_HP
176582cb5c3bSJohn Baldwin 	struct pcib_softc *sc = device_get_softc(dev);
176682cb5c3bSJohn Baldwin 	int retval;
176782cb5c3bSJohn Baldwin 
176882cb5c3bSJohn Baldwin 	retval = bus_child_present(dev);
176982cb5c3bSJohn Baldwin 	if (retval != 0 && sc->flags & PCIB_HOTPLUG)
177082cb5c3bSJohn Baldwin 		retval = pcib_hotplug_present(sc);
177182cb5c3bSJohn Baldwin 	return (retval);
177282cb5c3bSJohn Baldwin #else
177382cb5c3bSJohn Baldwin 	return (bus_child_present(dev));
177482cb5c3bSJohn Baldwin #endif
177582cb5c3bSJohn Baldwin }
177682cb5c3bSJohn Baldwin 
177782cb5c3bSJohn Baldwin int
1778bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1779bb0d0a8eSMike Smith {
1780bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
1781bb0d0a8eSMike Smith 
1782bb0d0a8eSMike Smith     switch (which) {
178355aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
178455aaf894SMarius Strobl 	*result = sc->domain;
178555aaf894SMarius Strobl 	return(0);
1786bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
17874edef187SJohn Baldwin 	*result = sc->bus.sec;
1788bb0d0a8eSMike Smith 	return(0);
1789bb0d0a8eSMike Smith     }
1790bb0d0a8eSMike Smith     return(ENOENT);
1791bb0d0a8eSMike Smith }
1792bb0d0a8eSMike Smith 
17936f0d5884SJohn Baldwin int
1794bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1795bb0d0a8eSMike Smith {
1796bb0d0a8eSMike Smith 
1797bb0d0a8eSMike Smith     switch (which) {
179855aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
179955aaf894SMarius Strobl 	return(EINVAL);
1800bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
18014edef187SJohn Baldwin 	return(EINVAL);
1802bb0d0a8eSMike Smith     }
1803bb0d0a8eSMike Smith     return(ENOENT);
1804bb0d0a8eSMike Smith }
1805bb0d0a8eSMike Smith 
180683c41143SJohn Baldwin #ifdef NEW_PCIB
180783c41143SJohn Baldwin /*
180883c41143SJohn Baldwin  * Attempt to allocate a resource from the existing resources assigned
180983c41143SJohn Baldwin  * to a window.
181083c41143SJohn Baldwin  */
181183c41143SJohn Baldwin static struct resource *
181283c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w,
18132dd1bdf1SJustin Hibbits     device_t child, int type, int *rid, rman_res_t start, rman_res_t end,
18142dd1bdf1SJustin Hibbits     rman_res_t count, u_int flags)
181583c41143SJohn Baldwin {
181683c41143SJohn Baldwin 	struct resource *res;
181783c41143SJohn Baldwin 
181883c41143SJohn Baldwin 	if (!pcib_is_window_open(w))
181983c41143SJohn Baldwin 		return (NULL);
182083c41143SJohn Baldwin 
182183c41143SJohn Baldwin 	res = rman_reserve_resource(&w->rman, start, end, count,
182283c41143SJohn Baldwin 	    flags & ~RF_ACTIVE, child);
182383c41143SJohn Baldwin 	if (res == NULL)
182483c41143SJohn Baldwin 		return (NULL);
182583c41143SJohn Baldwin 
182683c41143SJohn Baldwin 	if (bootverbose)
182783c41143SJohn Baldwin 		device_printf(sc->dev,
1828da1b038aSJustin Hibbits 		    "allocated %s range (%#jx-%#jx) for rid %x of %s\n",
182983c41143SJohn Baldwin 		    w->name, rman_get_start(res), rman_get_end(res), *rid,
183083c41143SJohn Baldwin 		    pcib_child_name(child));
183183c41143SJohn Baldwin 	rman_set_rid(res, *rid);
183283c41143SJohn Baldwin 
183383c41143SJohn Baldwin 	/*
183483c41143SJohn Baldwin 	 * If the resource should be active, pass that request up the
183583c41143SJohn Baldwin 	 * tree.  This assumes the parent drivers can handle
183683c41143SJohn Baldwin 	 * activating sub-allocated resources.
183783c41143SJohn Baldwin 	 */
183883c41143SJohn Baldwin 	if (flags & RF_ACTIVE) {
183983c41143SJohn Baldwin 		if (bus_activate_resource(child, type, *rid, res) != 0) {
184083c41143SJohn Baldwin 			rman_release_resource(res);
184183c41143SJohn Baldwin 			return (NULL);
184283c41143SJohn Baldwin 		}
184383c41143SJohn Baldwin 	}
184483c41143SJohn Baldwin 
184583c41143SJohn Baldwin 	return (res);
184683c41143SJohn Baldwin }
184783c41143SJohn Baldwin 
1848c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */
1849c825d4dcSJohn Baldwin static int
1850c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type,
18512dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1852c825d4dcSJohn Baldwin {
1853c825d4dcSJohn Baldwin 	struct resource *res;
18542dd1bdf1SJustin Hibbits 	rman_res_t base, limit, wmask;
1855c825d4dcSJohn Baldwin 	int rid;
1856c825d4dcSJohn Baldwin 
1857c825d4dcSJohn Baldwin 	/*
1858c825d4dcSJohn Baldwin 	 * If this is an I/O window on a bridge with ISA enable set
1859c825d4dcSJohn Baldwin 	 * and the start address is below 64k, then try to allocate an
1860c825d4dcSJohn Baldwin 	 * initial window of 0x1000 bytes long starting at address
1861c825d4dcSJohn Baldwin 	 * 0xf000 and walking down.  Note that if the original request
1862c825d4dcSJohn Baldwin 	 * was larger than the non-aliased range size of 0x100 our
1863c825d4dcSJohn Baldwin 	 * caller would have raised the start address up to 64k
1864c825d4dcSJohn Baldwin 	 * already.
1865c825d4dcSJohn Baldwin 	 */
1866c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1867c825d4dcSJohn Baldwin 	    start < 65536) {
1868c825d4dcSJohn Baldwin 		for (base = 0xf000; (long)base >= 0; base -= 0x1000) {
1869c825d4dcSJohn Baldwin 			limit = base + 0xfff;
1870c825d4dcSJohn Baldwin 
1871c825d4dcSJohn Baldwin 			/*
1872c825d4dcSJohn Baldwin 			 * Skip ranges that wouldn't work for the
1873c825d4dcSJohn Baldwin 			 * original request.  Note that the actual
1874c825d4dcSJohn Baldwin 			 * window that overlaps are the non-alias
1875c825d4dcSJohn Baldwin 			 * ranges within [base, limit], so this isn't
1876c825d4dcSJohn Baldwin 			 * quite a simple comparison.
1877c825d4dcSJohn Baldwin 			 */
1878c825d4dcSJohn Baldwin 			if (start + count > limit - 0x400)
1879c825d4dcSJohn Baldwin 				continue;
1880c825d4dcSJohn Baldwin 			if (base == 0) {
1881c825d4dcSJohn Baldwin 				/*
1882c825d4dcSJohn Baldwin 				 * The first open region for the window at
1883c825d4dcSJohn Baldwin 				 * 0 is 0x400-0x4ff.
1884c825d4dcSJohn Baldwin 				 */
1885c825d4dcSJohn Baldwin 				if (end - count + 1 < 0x400)
1886c825d4dcSJohn Baldwin 					continue;
1887c825d4dcSJohn Baldwin 			} else {
1888c825d4dcSJohn Baldwin 				if (end - count + 1 < base)
1889c825d4dcSJohn Baldwin 					continue;
1890c825d4dcSJohn Baldwin 			}
1891c825d4dcSJohn Baldwin 
1892c825d4dcSJohn Baldwin 			if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) {
1893c825d4dcSJohn Baldwin 				w->base = base;
1894c825d4dcSJohn Baldwin 				w->limit = limit;
1895c825d4dcSJohn Baldwin 				return (0);
1896c825d4dcSJohn Baldwin 			}
1897c825d4dcSJohn Baldwin 		}
1898c825d4dcSJohn Baldwin 		return (ENOSPC);
1899c825d4dcSJohn Baldwin 	}
1900c825d4dcSJohn Baldwin 
190189977ce2SJustin Hibbits 	wmask = ((rman_res_t)1 << w->step) - 1;
1902c825d4dcSJohn Baldwin 	if (RF_ALIGNMENT(flags) < w->step) {
1903c825d4dcSJohn Baldwin 		flags &= ~RF_ALIGNMENT_MASK;
1904c825d4dcSJohn Baldwin 		flags |= RF_ALIGNMENT_LOG2(w->step);
1905c825d4dcSJohn Baldwin 	}
1906c825d4dcSJohn Baldwin 	start &= ~wmask;
1907c825d4dcSJohn Baldwin 	end |= wmask;
190889977ce2SJustin Hibbits 	count = roundup2(count, (rman_res_t)1 << w->step);
1909c825d4dcSJohn Baldwin 	rid = w->reg;
1910c825d4dcSJohn Baldwin 	res = bus_alloc_resource(sc->dev, type, &rid, start, end, count,
1911c825d4dcSJohn Baldwin 	    flags & ~RF_ACTIVE);
1912c825d4dcSJohn Baldwin 	if (res == NULL)
1913c825d4dcSJohn Baldwin 		return (ENOSPC);
1914c825d4dcSJohn Baldwin 	pcib_add_window_resources(w, &res, 1);
1915c825d4dcSJohn Baldwin 	pcib_activate_window(sc, type);
1916c825d4dcSJohn Baldwin 	w->base = rman_get_start(res);
1917c825d4dcSJohn Baldwin 	w->limit = rman_get_end(res);
1918c825d4dcSJohn Baldwin 	return (0);
1919c825d4dcSJohn Baldwin }
1920c825d4dcSJohn Baldwin 
1921c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */
1922c825d4dcSJohn Baldwin static int
1923c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type,
19242dd1bdf1SJustin Hibbits     rman_res_t base, rman_res_t limit)
1925c825d4dcSJohn Baldwin {
1926c825d4dcSJohn Baldwin 	struct resource *res;
1927c825d4dcSJohn Baldwin 	int error, i, force_64k_base;
1928c825d4dcSJohn Baldwin 
1929c825d4dcSJohn Baldwin 	KASSERT(base <= w->base && limit >= w->limit,
1930c825d4dcSJohn Baldwin 	    ("attempting to shrink window"));
1931c825d4dcSJohn Baldwin 
1932c825d4dcSJohn Baldwin 	/*
1933c825d4dcSJohn Baldwin 	 * XXX: pcib_grow_window() doesn't try to do this anyway and
1934c825d4dcSJohn Baldwin 	 * the error handling for all the edge cases would be tedious.
1935c825d4dcSJohn Baldwin 	 */
1936c825d4dcSJohn Baldwin 	KASSERT(limit == w->limit || base == w->base,
1937c825d4dcSJohn Baldwin 	    ("attempting to grow both ends of a window"));
1938c825d4dcSJohn Baldwin 
1939c825d4dcSJohn Baldwin 	/*
1940c825d4dcSJohn Baldwin 	 * Yet more special handling for requests to expand an I/O
1941c825d4dcSJohn Baldwin 	 * window behind an ISA-enabled bridge.  Since I/O windows
1942c825d4dcSJohn Baldwin 	 * have to grow in 0x1000 increments and the end of the 0xffff
1943c825d4dcSJohn Baldwin 	 * range is an alias, growing a window below 64k will always
1944c825d4dcSJohn Baldwin 	 * result in allocating new resources and never adjusting an
1945c825d4dcSJohn Baldwin 	 * existing resource.
1946c825d4dcSJohn Baldwin 	 */
1947c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1948c825d4dcSJohn Baldwin 	    (limit <= 65535 || (base <= 65535 && base != w->base))) {
1949c825d4dcSJohn Baldwin 		KASSERT(limit == w->limit || limit <= 65535,
1950c825d4dcSJohn Baldwin 		    ("attempting to grow both ends across 64k ISA alias"));
1951c825d4dcSJohn Baldwin 
1952c825d4dcSJohn Baldwin 		if (base != w->base)
1953c825d4dcSJohn Baldwin 			error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1);
1954c825d4dcSJohn Baldwin 		else
1955c825d4dcSJohn Baldwin 			error = pcib_alloc_nonisa_ranges(sc, w->limit + 1,
1956c825d4dcSJohn Baldwin 			    limit);
1957c825d4dcSJohn Baldwin 		if (error == 0) {
1958c825d4dcSJohn Baldwin 			w->base = base;
1959c825d4dcSJohn Baldwin 			w->limit = limit;
1960c825d4dcSJohn Baldwin 		}
1961c825d4dcSJohn Baldwin 		return (error);
1962c825d4dcSJohn Baldwin 	}
1963c825d4dcSJohn Baldwin 
1964c825d4dcSJohn Baldwin 	/*
1965c825d4dcSJohn Baldwin 	 * Find the existing resource to adjust.  Usually there is only one,
1966c825d4dcSJohn Baldwin 	 * but for an ISA-enabled bridge we might be growing the I/O window
1967c825d4dcSJohn Baldwin 	 * above 64k and need to find the existing resource that maps all
1968c825d4dcSJohn Baldwin 	 * of the area above 64k.
1969c825d4dcSJohn Baldwin 	 */
1970c825d4dcSJohn Baldwin 	for (i = 0; i < w->count; i++) {
1971c825d4dcSJohn Baldwin 		if (rman_get_end(w->res[i]) == w->limit)
1972c825d4dcSJohn Baldwin 			break;
1973c825d4dcSJohn Baldwin 	}
1974c825d4dcSJohn Baldwin 	KASSERT(i != w->count, ("did not find existing resource"));
1975c825d4dcSJohn Baldwin 	res = w->res[i];
1976c825d4dcSJohn Baldwin 
1977c825d4dcSJohn Baldwin 	/*
1978c825d4dcSJohn Baldwin 	 * Usually the resource we found should match the window's
1979c825d4dcSJohn Baldwin 	 * existing range.  The one exception is the ISA-enabled case
1980c825d4dcSJohn Baldwin 	 * mentioned above in which case the resource should start at
1981c825d4dcSJohn Baldwin 	 * 64k.
1982c825d4dcSJohn Baldwin 	 */
1983c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1984c825d4dcSJohn Baldwin 	    w->base <= 65535) {
1985c825d4dcSJohn Baldwin 		KASSERT(rman_get_start(res) == 65536,
1986c825d4dcSJohn Baldwin 		    ("existing resource mismatch"));
1987c825d4dcSJohn Baldwin 		force_64k_base = 1;
1988c825d4dcSJohn Baldwin 	} else {
1989c825d4dcSJohn Baldwin 		KASSERT(w->base == rman_get_start(res),
1990c825d4dcSJohn Baldwin 		    ("existing resource mismatch"));
1991c825d4dcSJohn Baldwin 		force_64k_base = 0;
1992c825d4dcSJohn Baldwin 	}
1993c825d4dcSJohn Baldwin 
1994c825d4dcSJohn Baldwin 	error = bus_adjust_resource(sc->dev, type, res, force_64k_base ?
1995c825d4dcSJohn Baldwin 	    rman_get_start(res) : base, limit);
1996c825d4dcSJohn Baldwin 	if (error)
1997c825d4dcSJohn Baldwin 		return (error);
1998c825d4dcSJohn Baldwin 
1999c825d4dcSJohn Baldwin 	/* Add the newly allocated region to the resource manager. */
2000c825d4dcSJohn Baldwin 	if (w->base != base) {
2001c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, base, w->base - 1);
2002c825d4dcSJohn Baldwin 		w->base = base;
2003c825d4dcSJohn Baldwin 	} else {
2004c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, w->limit + 1, limit);
2005c825d4dcSJohn Baldwin 		w->limit = limit;
2006c825d4dcSJohn Baldwin 	}
2007c825d4dcSJohn Baldwin 	if (error) {
2008c825d4dcSJohn Baldwin 		if (bootverbose)
2009c825d4dcSJohn Baldwin 			device_printf(sc->dev,
2010c825d4dcSJohn Baldwin 			    "failed to expand %s resource manager\n", w->name);
2011c825d4dcSJohn Baldwin 		(void)bus_adjust_resource(sc->dev, type, res, force_64k_base ?
2012c825d4dcSJohn Baldwin 		    rman_get_start(res) : w->base, w->limit);
2013c825d4dcSJohn Baldwin 	}
2014c825d4dcSJohn Baldwin 	return (error);
2015c825d4dcSJohn Baldwin }
2016c825d4dcSJohn Baldwin 
201783c41143SJohn Baldwin /*
201883c41143SJohn Baldwin  * Attempt to grow a window to make room for a given resource request.
201983c41143SJohn Baldwin  */
202083c41143SJohn Baldwin static int
202183c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type,
20222dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
202383c41143SJohn Baldwin {
20242dd1bdf1SJustin Hibbits 	rman_res_t align, start_free, end_free, front, back, wmask;
2025c825d4dcSJohn Baldwin 	int error;
202683c41143SJohn Baldwin 
202783c41143SJohn Baldwin 	/*
202883c41143SJohn Baldwin 	 * Clamp the desired resource range to the maximum address
202983c41143SJohn Baldwin 	 * this window supports.  Reject impossible requests.
2030c825d4dcSJohn Baldwin 	 *
2031c825d4dcSJohn Baldwin 	 * For I/O port requests behind a bridge with the ISA enable
2032c825d4dcSJohn Baldwin 	 * bit set, force large allocations to start above 64k.
203383c41143SJohn Baldwin 	 */
203483c41143SJohn Baldwin 	if (!w->valid)
203583c41143SJohn Baldwin 		return (EINVAL);
2036c825d4dcSJohn Baldwin 	if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 &&
2037c825d4dcSJohn Baldwin 	    start < 65536)
2038c825d4dcSJohn Baldwin 		start = 65536;
203983c41143SJohn Baldwin 	if (end > w->rman.rm_end)
204083c41143SJohn Baldwin 		end = w->rman.rm_end;
204183c41143SJohn Baldwin 	if (start + count - 1 > end || start + count < start)
204283c41143SJohn Baldwin 		return (EINVAL);
204389977ce2SJustin Hibbits 	wmask = ((rman_res_t)1 << w->step) - 1;
204483c41143SJohn Baldwin 
204583c41143SJohn Baldwin 	/*
204683c41143SJohn Baldwin 	 * If there is no resource at all, just try to allocate enough
204783c41143SJohn Baldwin 	 * aligned space for this resource.
204883c41143SJohn Baldwin 	 */
204983c41143SJohn Baldwin 	if (w->res == NULL) {
2050c825d4dcSJohn Baldwin 		error = pcib_alloc_new_window(sc, w, type, start, end, count,
2051c825d4dcSJohn Baldwin 		    flags);
2052c825d4dcSJohn Baldwin 		if (error) {
205383c41143SJohn Baldwin 			if (bootverbose)
205483c41143SJohn Baldwin 				device_printf(sc->dev,
2055da1b038aSJustin Hibbits 		    "failed to allocate initial %s window (%#jx-%#jx,%#jx)\n",
205683c41143SJohn Baldwin 				    w->name, start, end, count);
205783c41143SJohn Baldwin 			return (error);
205883c41143SJohn Baldwin 		}
2059c825d4dcSJohn Baldwin 		if (bootverbose)
2060c825d4dcSJohn Baldwin 			device_printf(sc->dev,
2061c825d4dcSJohn Baldwin 			    "allocated initial %s window of %#jx-%#jx\n",
2062c825d4dcSJohn Baldwin 			    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
206383c41143SJohn Baldwin 		goto updatewin;
206483c41143SJohn Baldwin 	}
206583c41143SJohn Baldwin 
206683c41143SJohn Baldwin 	/*
206783c41143SJohn Baldwin 	 * See if growing the window would help.  Compute the minimum
206883c41143SJohn Baldwin 	 * amount of address space needed on both the front and back
206983c41143SJohn Baldwin 	 * ends of the existing window to satisfy the allocation.
207083c41143SJohn Baldwin 	 *
207183c41143SJohn Baldwin 	 * For each end, build a candidate region adjusting for the
207283c41143SJohn Baldwin 	 * required alignment, etc.  If there is a free region at the
207383c41143SJohn Baldwin 	 * edge of the window, grow from the inner edge of the free
207483c41143SJohn Baldwin 	 * region.  Otherwise grow from the window boundary.
207583c41143SJohn Baldwin 	 *
2076c825d4dcSJohn Baldwin 	 * Growing an I/O window below 64k for a bridge with the ISA
2077c825d4dcSJohn Baldwin 	 * enable bit doesn't require any special magic as the step
2078c825d4dcSJohn Baldwin 	 * size of an I/O window (1k) always includes multiple
2079c825d4dcSJohn Baldwin 	 * non-alias ranges when it is grown in either direction.
2080c825d4dcSJohn Baldwin 	 *
208183c41143SJohn Baldwin 	 * XXX: Special case: if w->res is completely empty and the
208283c41143SJohn Baldwin 	 * request size is larger than w->res, we should find the
208383c41143SJohn Baldwin 	 * optimal aligned buffer containing w->res and allocate that.
208483c41143SJohn Baldwin 	 */
208583c41143SJohn Baldwin 	if (bootverbose)
208683c41143SJohn Baldwin 		device_printf(sc->dev,
2087da1b038aSJustin Hibbits 		    "attempting to grow %s window for (%#jx-%#jx,%#jx)\n",
208883c41143SJohn Baldwin 		    w->name, start, end, count);
208989977ce2SJustin Hibbits 	align = (rman_res_t)1 << RF_ALIGNMENT(flags);
2090c825d4dcSJohn Baldwin 	if (start < w->base) {
209183c41143SJohn Baldwin 		if (rman_first_free_region(&w->rman, &start_free, &end_free) !=
2092c825d4dcSJohn Baldwin 		    0 || start_free != w->base)
2093c825d4dcSJohn Baldwin 			end_free = w->base;
209483c41143SJohn Baldwin 		if (end_free > end)
2095ddac8cc9SJohn Baldwin 			end_free = end + 1;
209683c41143SJohn Baldwin 
209783c41143SJohn Baldwin 		/* Move end_free down until it is properly aligned. */
209883c41143SJohn Baldwin 		end_free &= ~(align - 1);
2099a49dcb46SJohn Baldwin 		end_free--;
2100a49dcb46SJohn Baldwin 		front = end_free - (count - 1);
210183c41143SJohn Baldwin 
210283c41143SJohn Baldwin 		/*
210383c41143SJohn Baldwin 		 * The resource would now be allocated at (front,
210483c41143SJohn Baldwin 		 * end_free).  Ensure that fits in the (start, end)
210583c41143SJohn Baldwin 		 * bounds.  end_free is checked above.  If 'front' is
210683c41143SJohn Baldwin 		 * ok, ensure it is properly aligned for this window.
210783c41143SJohn Baldwin 		 * Also check for underflow.
210883c41143SJohn Baldwin 		 */
210983c41143SJohn Baldwin 		if (front >= start && front <= end_free) {
211083c41143SJohn Baldwin 			if (bootverbose)
2111da1b038aSJustin Hibbits 				printf("\tfront candidate range: %#jx-%#jx\n",
211283c41143SJohn Baldwin 				    front, end_free);
2113a7b5acacSJohn Baldwin 			front &= ~wmask;
2114c825d4dcSJohn Baldwin 			front = w->base - front;
211583c41143SJohn Baldwin 		} else
211683c41143SJohn Baldwin 			front = 0;
211783c41143SJohn Baldwin 	} else
211883c41143SJohn Baldwin 		front = 0;
2119c825d4dcSJohn Baldwin 	if (end > w->limit) {
212083c41143SJohn Baldwin 		if (rman_last_free_region(&w->rman, &start_free, &end_free) !=
2121c825d4dcSJohn Baldwin 		    0 || end_free != w->limit)
2122c825d4dcSJohn Baldwin 			start_free = w->limit + 1;
212383c41143SJohn Baldwin 		if (start_free < start)
212483c41143SJohn Baldwin 			start_free = start;
212583c41143SJohn Baldwin 
212683c41143SJohn Baldwin 		/* Move start_free up until it is properly aligned. */
212783c41143SJohn Baldwin 		start_free = roundup2(start_free, align);
2128a49dcb46SJohn Baldwin 		back = start_free + count - 1;
212983c41143SJohn Baldwin 
213083c41143SJohn Baldwin 		/*
213183c41143SJohn Baldwin 		 * The resource would now be allocated at (start_free,
213283c41143SJohn Baldwin 		 * back).  Ensure that fits in the (start, end)
213383c41143SJohn Baldwin 		 * bounds.  start_free is checked above.  If 'back' is
213483c41143SJohn Baldwin 		 * ok, ensure it is properly aligned for this window.
213583c41143SJohn Baldwin 		 * Also check for overflow.
213683c41143SJohn Baldwin 		 */
213783c41143SJohn Baldwin 		if (back <= end && start_free <= back) {
213883c41143SJohn Baldwin 			if (bootverbose)
2139da1b038aSJustin Hibbits 				printf("\tback candidate range: %#jx-%#jx\n",
214083c41143SJohn Baldwin 				    start_free, back);
2141a7b5acacSJohn Baldwin 			back |= wmask;
2142c825d4dcSJohn Baldwin 			back -= w->limit;
214383c41143SJohn Baldwin 		} else
214483c41143SJohn Baldwin 			back = 0;
214583c41143SJohn Baldwin 	} else
214683c41143SJohn Baldwin 		back = 0;
214783c41143SJohn Baldwin 
214883c41143SJohn Baldwin 	/*
214983c41143SJohn Baldwin 	 * Try to allocate the smallest needed region first.
215083c41143SJohn Baldwin 	 * If that fails, fall back to the other region.
215183c41143SJohn Baldwin 	 */
215283c41143SJohn Baldwin 	error = ENOSPC;
215383c41143SJohn Baldwin 	while (front != 0 || back != 0) {
215483c41143SJohn Baldwin 		if (front != 0 && (front <= back || back == 0)) {
2155c825d4dcSJohn Baldwin 			error = pcib_expand_window(sc, w, type, w->base - front,
2156c825d4dcSJohn Baldwin 			    w->limit);
215783c41143SJohn Baldwin 			if (error == 0)
215883c41143SJohn Baldwin 				break;
215983c41143SJohn Baldwin 			front = 0;
216083c41143SJohn Baldwin 		} else {
2161c825d4dcSJohn Baldwin 			error = pcib_expand_window(sc, w, type, w->base,
2162c825d4dcSJohn Baldwin 			    w->limit + back);
216383c41143SJohn Baldwin 			if (error == 0)
216483c41143SJohn Baldwin 				break;
216583c41143SJohn Baldwin 			back = 0;
216683c41143SJohn Baldwin 		}
216783c41143SJohn Baldwin 	}
216883c41143SJohn Baldwin 
216983c41143SJohn Baldwin 	if (error)
217083c41143SJohn Baldwin 		return (error);
217183c41143SJohn Baldwin 	if (bootverbose)
2172c825d4dcSJohn Baldwin 		device_printf(sc->dev, "grew %s window to %#jx-%#jx\n",
2173c825d4dcSJohn Baldwin 		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
217483c41143SJohn Baldwin 
217583c41143SJohn Baldwin updatewin:
2176c825d4dcSJohn Baldwin 	/* Write the new window. */
2177a7b5acacSJohn Baldwin 	KASSERT((w->base & wmask) == 0, ("start address is not aligned"));
2178a7b5acacSJohn Baldwin 	KASSERT((w->limit & wmask) == wmask, ("end address is not aligned"));
217983c41143SJohn Baldwin 	pcib_write_windows(sc, w->mask);
218083c41143SJohn Baldwin 	return (0);
218183c41143SJohn Baldwin }
218283c41143SJohn Baldwin 
218383c41143SJohn Baldwin /*
218483c41143SJohn Baldwin  * We have to trap resource allocation requests and ensure that the bridge
218583c41143SJohn Baldwin  * is set up to, or capable of handling them.
218683c41143SJohn Baldwin  */
218783c41143SJohn Baldwin struct resource *
218883c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
21892dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
219083c41143SJohn Baldwin {
219183c41143SJohn Baldwin 	struct pcib_softc *sc;
219283c41143SJohn Baldwin 	struct resource *r;
219383c41143SJohn Baldwin 
219483c41143SJohn Baldwin 	sc = device_get_softc(dev);
219583c41143SJohn Baldwin 
219683c41143SJohn Baldwin 	/*
219783c41143SJohn Baldwin 	 * VGA resources are decoded iff the VGA enable bit is set in
219883c41143SJohn Baldwin 	 * the bridge control register.  VGA resources do not fall into
219983c41143SJohn Baldwin 	 * the resource windows and are passed up to the parent.
220083c41143SJohn Baldwin 	 */
220183c41143SJohn Baldwin 	if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) ||
220283c41143SJohn Baldwin 	    (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) {
220383c41143SJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE)
220483c41143SJohn Baldwin 			return (bus_generic_alloc_resource(dev, child, type,
220583c41143SJohn Baldwin 			    rid, start, end, count, flags));
220683c41143SJohn Baldwin 		else
220783c41143SJohn Baldwin 			return (NULL);
220883c41143SJohn Baldwin 	}
220983c41143SJohn Baldwin 
221083c41143SJohn Baldwin 	switch (type) {
22114edef187SJohn Baldwin #ifdef PCI_RES_BUS
22124edef187SJohn Baldwin 	case PCI_RES_BUS:
22134edef187SJohn Baldwin 		return (pcib_alloc_subbus(&sc->bus, child, rid, start, end,
22144edef187SJohn Baldwin 		    count, flags));
22154edef187SJohn Baldwin #endif
221683c41143SJohn Baldwin 	case SYS_RES_IOPORT:
2217c825d4dcSJohn Baldwin 		if (pcib_is_isa_range(sc, start, end, count))
2218c825d4dcSJohn Baldwin 			return (NULL);
221983c41143SJohn Baldwin 		r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start,
222083c41143SJohn Baldwin 		    end, count, flags);
2221a6c82265SMarius Strobl 		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
222283c41143SJohn Baldwin 			break;
222383c41143SJohn Baldwin 		if (pcib_grow_window(sc, &sc->io, type, start, end, count,
222483c41143SJohn Baldwin 		    flags) == 0)
222583c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->io, child, type,
222683c41143SJohn Baldwin 			    rid, start, end, count, flags);
222783c41143SJohn Baldwin 		break;
222883c41143SJohn Baldwin 	case SYS_RES_MEMORY:
222983c41143SJohn Baldwin 		/*
223083c41143SJohn Baldwin 		 * For prefetchable resources, prefer the prefetchable
223183c41143SJohn Baldwin 		 * memory window, but fall back to the regular memory
223283c41143SJohn Baldwin 		 * window if that fails.  Try both windows before
223383c41143SJohn Baldwin 		 * attempting to grow a window in case the firmware
223483c41143SJohn Baldwin 		 * has used a range in the regular memory window to
223583c41143SJohn Baldwin 		 * map a prefetchable BAR.
223683c41143SJohn Baldwin 		 */
223783c41143SJohn Baldwin 		if (flags & RF_PREFETCHABLE) {
223883c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->pmem, child, type,
223983c41143SJohn Baldwin 			    rid, start, end, count, flags);
224083c41143SJohn Baldwin 			if (r != NULL)
224183c41143SJohn Baldwin 				break;
224283c41143SJohn Baldwin 		}
224383c41143SJohn Baldwin 		r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid,
224483c41143SJohn Baldwin 		    start, end, count, flags);
2245a6c82265SMarius Strobl 		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
224683c41143SJohn Baldwin 			break;
224783c41143SJohn Baldwin 		if (flags & RF_PREFETCHABLE) {
224883c41143SJohn Baldwin 			if (pcib_grow_window(sc, &sc->pmem, type, start, end,
224983c41143SJohn Baldwin 			    count, flags) == 0) {
225083c41143SJohn Baldwin 				r = pcib_suballoc_resource(sc, &sc->pmem, child,
225183c41143SJohn Baldwin 				    type, rid, start, end, count, flags);
225283c41143SJohn Baldwin 				if (r != NULL)
225383c41143SJohn Baldwin 					break;
225483c41143SJohn Baldwin 			}
225583c41143SJohn Baldwin 		}
225683c41143SJohn Baldwin 		if (pcib_grow_window(sc, &sc->mem, type, start, end, count,
225783c41143SJohn Baldwin 		    flags & ~RF_PREFETCHABLE) == 0)
225883c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->mem, child, type,
225983c41143SJohn Baldwin 			    rid, start, end, count, flags);
226083c41143SJohn Baldwin 		break;
226183c41143SJohn Baldwin 	default:
226283c41143SJohn Baldwin 		return (bus_generic_alloc_resource(dev, child, type, rid,
226383c41143SJohn Baldwin 		    start, end, count, flags));
226483c41143SJohn Baldwin 	}
226583c41143SJohn Baldwin 
226683c41143SJohn Baldwin 	/*
226783c41143SJohn Baldwin 	 * If attempts to suballocate from the window fail but this is a
226883c41143SJohn Baldwin 	 * subtractive bridge, pass the request up the tree.
226983c41143SJohn Baldwin 	 */
227083c41143SJohn Baldwin 	if (sc->flags & PCIB_SUBTRACTIVE && r == NULL)
227183c41143SJohn Baldwin 		return (bus_generic_alloc_resource(dev, child, type, rid,
227283c41143SJohn Baldwin 		    start, end, count, flags));
227383c41143SJohn Baldwin 	return (r);
227483c41143SJohn Baldwin }
227583c41143SJohn Baldwin 
227683c41143SJohn Baldwin int
227783c41143SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
22782dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end)
227983c41143SJohn Baldwin {
228083c41143SJohn Baldwin 	struct pcib_softc *sc;
228183c41143SJohn Baldwin 
228283c41143SJohn Baldwin 	sc = device_get_softc(bus);
228383c41143SJohn Baldwin 	if (pcib_is_resource_managed(sc, type, r))
228483c41143SJohn Baldwin 		return (rman_adjust_resource(r, start, end));
228583c41143SJohn Baldwin 	return (bus_generic_adjust_resource(bus, child, type, r, start, end));
228683c41143SJohn Baldwin }
228783c41143SJohn Baldwin 
228883c41143SJohn Baldwin int
228983c41143SJohn Baldwin pcib_release_resource(device_t dev, device_t child, int type, int rid,
229083c41143SJohn Baldwin     struct resource *r)
229183c41143SJohn Baldwin {
229283c41143SJohn Baldwin 	struct pcib_softc *sc;
229383c41143SJohn Baldwin 	int error;
229483c41143SJohn Baldwin 
229583c41143SJohn Baldwin 	sc = device_get_softc(dev);
229683c41143SJohn Baldwin 	if (pcib_is_resource_managed(sc, type, r)) {
229783c41143SJohn Baldwin 		if (rman_get_flags(r) & RF_ACTIVE) {
229883c41143SJohn Baldwin 			error = bus_deactivate_resource(child, type, rid, r);
229983c41143SJohn Baldwin 			if (error)
230083c41143SJohn Baldwin 				return (error);
230183c41143SJohn Baldwin 		}
230283c41143SJohn Baldwin 		return (rman_release_resource(r));
230383c41143SJohn Baldwin 	}
230483c41143SJohn Baldwin 	return (bus_generic_release_resource(dev, child, type, rid, r));
230583c41143SJohn Baldwin }
230683c41143SJohn Baldwin #else
2307bb0d0a8eSMike Smith /*
2308bb0d0a8eSMike Smith  * We have to trap resource allocation requests and ensure that the bridge
2309bb0d0a8eSMike Smith  * is set up to, or capable of handling them.
2310bb0d0a8eSMike Smith  */
23116f0d5884SJohn Baldwin struct resource *
2312bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
23132dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
2314bb0d0a8eSMike Smith {
2315bb0d0a8eSMike Smith 	struct pcib_softc	*sc = device_get_softc(dev);
231626043836SJohn Baldwin 	const char *name, *suffix;
2317a8b354a8SWarner Losh 	int ok;
2318bb0d0a8eSMike Smith 
2319bb0d0a8eSMike Smith 	/*
2320bb0d0a8eSMike Smith 	 * Fail the allocation for this range if it's not supported.
2321bb0d0a8eSMike Smith 	 */
232226043836SJohn Baldwin 	name = device_get_nameunit(child);
232326043836SJohn Baldwin 	if (name == NULL) {
232426043836SJohn Baldwin 		name = "";
232526043836SJohn Baldwin 		suffix = "";
232626043836SJohn Baldwin 	} else
232726043836SJohn Baldwin 		suffix = " ";
2328bb0d0a8eSMike Smith 	switch (type) {
2329bb0d0a8eSMike Smith 	case SYS_RES_IOPORT:
2330a8b354a8SWarner Losh 		ok = 0;
2331e4b59fc5SWarner Losh 		if (!pcib_is_io_open(sc))
2332e4b59fc5SWarner Losh 			break;
2333a8b354a8SWarner Losh 		ok = (start >= sc->iobase && end <= sc->iolimit);
2334d98d9b12SMarcel Moolenaar 
2335d98d9b12SMarcel Moolenaar 		/*
2336d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA I/O addresses when the
2337d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
2338d98d9b12SMarcel Moolenaar 		 */
2339d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_ioport_range(start, end))
2340d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
2341d98d9b12SMarcel Moolenaar 
2342e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
2343a8b354a8SWarner Losh 			if (!ok) {
234412b8c86eSWarner Losh 				if (start < sc->iobase)
234512b8c86eSWarner Losh 					start = sc->iobase;
234612b8c86eSWarner Losh 				if (end > sc->iolimit)
234712b8c86eSWarner Losh 					end = sc->iolimit;
23482daa7a07SWarner Losh 				if (start < end)
23492daa7a07SWarner Losh 					ok = 1;
2350a8b354a8SWarner Losh 			}
23511c54ff33SMatthew N. Dodd 		} else {
2352e4b59fc5SWarner Losh 			ok = 1;
23539dffe835SWarner Losh #if 0
2354795dceffSWarner Losh 			/*
2355795dceffSWarner Losh 			 * If we overlap with the subtractive range, then
2356795dceffSWarner Losh 			 * pick the upper range to use.
2357795dceffSWarner Losh 			 */
2358795dceffSWarner Losh 			if (start < sc->iolimit && end > sc->iobase)
2359795dceffSWarner Losh 				start = sc->iolimit + 1;
23609dffe835SWarner Losh #endif
236112b8c86eSWarner Losh 		}
2362a8b354a8SWarner Losh 		if (end < start) {
2363da1b038aSJustin Hibbits 			device_printf(dev, "ioport: end (%jx) < start (%jx)\n",
23642daa7a07SWarner Losh 			    end, start);
2365a8b354a8SWarner Losh 			start = 0;
2366a8b354a8SWarner Losh 			end = 0;
2367a8b354a8SWarner Losh 			ok = 0;
2368a8b354a8SWarner Losh 		}
2369a8b354a8SWarner Losh 		if (!ok) {
237026043836SJohn Baldwin 			device_printf(dev, "%s%srequested unsupported I/O "
2371da1b038aSJustin Hibbits 			    "range 0x%jx-0x%jx (decoding 0x%x-0x%x)\n",
237226043836SJohn Baldwin 			    name, suffix, start, end, sc->iobase, sc->iolimit);
2373bb0d0a8eSMike Smith 			return (NULL);
2374bb0d0a8eSMike Smith 		}
23754fa59183SMike Smith 		if (bootverbose)
23762daa7a07SWarner Losh 			device_printf(dev,
2377da1b038aSJustin Hibbits 			    "%s%srequested I/O range 0x%jx-0x%jx: in range\n",
237826043836SJohn Baldwin 			    name, suffix, start, end);
2379bb0d0a8eSMike Smith 		break;
2380bb0d0a8eSMike Smith 
2381bb0d0a8eSMike Smith 	case SYS_RES_MEMORY:
2382a8b354a8SWarner Losh 		ok = 0;
2383a8b354a8SWarner Losh 		if (pcib_is_nonprefetch_open(sc))
2384a8b354a8SWarner Losh 			ok = ok || (start >= sc->membase && end <= sc->memlimit);
2385a8b354a8SWarner Losh 		if (pcib_is_prefetch_open(sc))
2386a8b354a8SWarner Losh 			ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
2387d98d9b12SMarcel Moolenaar 
2388d98d9b12SMarcel Moolenaar 		/*
2389d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA memory addresses when the
2390d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
2391d98d9b12SMarcel Moolenaar 		 */
2392d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_memory_range(start, end))
2393d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
2394d98d9b12SMarcel Moolenaar 
2395e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
2396a8b354a8SWarner Losh 			if (!ok) {
2397a8b354a8SWarner Losh 				ok = 1;
2398a8b354a8SWarner Losh 				if (flags & RF_PREFETCHABLE) {
2399a8b354a8SWarner Losh 					if (pcib_is_prefetch_open(sc)) {
2400a8b354a8SWarner Losh 						if (start < sc->pmembase)
2401a8b354a8SWarner Losh 							start = sc->pmembase;
2402a8b354a8SWarner Losh 						if (end > sc->pmemlimit)
2403a8b354a8SWarner Losh 							end = sc->pmemlimit;
2404a8b354a8SWarner Losh 					} else {
2405a8b354a8SWarner Losh 						ok = 0;
2406a8b354a8SWarner Losh 					}
2407a8b354a8SWarner Losh 				} else {	/* non-prefetchable */
2408a8b354a8SWarner Losh 					if (pcib_is_nonprefetch_open(sc)) {
2409a8b354a8SWarner Losh 						if (start < sc->membase)
241012b8c86eSWarner Losh 							start = sc->membase;
241112b8c86eSWarner Losh 						if (end > sc->memlimit)
241212b8c86eSWarner Losh 							end = sc->memlimit;
24131c54ff33SMatthew N. Dodd 					} else {
2414a8b354a8SWarner Losh 						ok = 0;
2415a8b354a8SWarner Losh 					}
2416a8b354a8SWarner Losh 				}
2417a8b354a8SWarner Losh 			}
2418a8b354a8SWarner Losh 		} else if (!ok) {
2419e4b59fc5SWarner Losh 			ok = 1;	/* subtractive bridge: always ok */
24209dffe835SWarner Losh #if 0
2421a8b354a8SWarner Losh 			if (pcib_is_nonprefetch_open(sc)) {
2422795dceffSWarner Losh 				if (start < sc->memlimit && end > sc->membase)
2423795dceffSWarner Losh 					start = sc->memlimit + 1;
2424a8b354a8SWarner Losh 			}
2425a8b354a8SWarner Losh 			if (pcib_is_prefetch_open(sc)) {
2426795dceffSWarner Losh 				if (start < sc->pmemlimit && end > sc->pmembase)
2427795dceffSWarner Losh 					start = sc->pmemlimit + 1;
24281c54ff33SMatthew N. Dodd 			}
24299dffe835SWarner Losh #endif
243012b8c86eSWarner Losh 		}
2431a8b354a8SWarner Losh 		if (end < start) {
2432da1b038aSJustin Hibbits 			device_printf(dev, "memory: end (%jx) < start (%jx)\n",
24332daa7a07SWarner Losh 			    end, start);
2434a8b354a8SWarner Losh 			start = 0;
2435a8b354a8SWarner Losh 			end = 0;
2436a8b354a8SWarner Losh 			ok = 0;
2437a8b354a8SWarner Losh 		}
2438a8b354a8SWarner Losh 		if (!ok && bootverbose)
243934428485SWarner Losh 			device_printf(dev,
2440da1b038aSJustin Hibbits 			    "%s%srequested unsupported memory range %#jx-%#jx "
2441b0a2d4b8SWarner Losh 			    "(decoding %#jx-%#jx, %#jx-%#jx)\n",
244226043836SJohn Baldwin 			    name, suffix, start, end,
2443b0a2d4b8SWarner Losh 			    (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
2444b0a2d4b8SWarner Losh 			    (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
2445a8b354a8SWarner Losh 		if (!ok)
2446bb0d0a8eSMike Smith 			return (NULL);
24474fa59183SMike Smith 		if (bootverbose)
244826043836SJohn Baldwin 			device_printf(dev,"%s%srequested memory range "
2449da1b038aSJustin Hibbits 			    "0x%jx-0x%jx: good\n",
245026043836SJohn Baldwin 			    name, suffix, start, end);
24514fa59183SMike Smith 		break;
24524fa59183SMike Smith 
2453bb0d0a8eSMike Smith 	default:
24544fa59183SMike Smith 		break;
2455bb0d0a8eSMike Smith 	}
2456bb0d0a8eSMike Smith 	/*
2457bb0d0a8eSMike Smith 	 * Bridge is OK decoding this resource, so pass it up.
2458bb0d0a8eSMike Smith 	 */
24592daa7a07SWarner Losh 	return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
24602daa7a07SWarner Losh 	    count, flags));
2461bb0d0a8eSMike Smith }
246283c41143SJohn Baldwin #endif
2463bb0d0a8eSMike Smith 
2464bb0d0a8eSMike Smith /*
246555d3ea17SRyan Stone  * If ARI is enabled on this downstream port, translate the function number
246655d3ea17SRyan Stone  * to the non-ARI slot/function.  The downstream port will convert it back in
246755d3ea17SRyan Stone  * hardware.  If ARI is not enabled slot and func are not modified.
246855d3ea17SRyan Stone  */
246955d3ea17SRyan Stone static __inline void
247055d3ea17SRyan Stone pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func)
247155d3ea17SRyan Stone {
247255d3ea17SRyan Stone 	struct pcib_softc *sc;
247355d3ea17SRyan Stone 	int ari_func;
247455d3ea17SRyan Stone 
247555d3ea17SRyan Stone 	sc = device_get_softc(pcib);
247655d3ea17SRyan Stone 	ari_func = *func;
247755d3ea17SRyan Stone 
247855d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
247955d3ea17SRyan Stone 		KASSERT(*slot == 0,
248055d3ea17SRyan Stone 		    ("Non-zero slot number with ARI enabled!"));
248155d3ea17SRyan Stone 		*slot = PCIE_ARI_SLOT(ari_func);
248255d3ea17SRyan Stone 		*func = PCIE_ARI_FUNC(ari_func);
248355d3ea17SRyan Stone 	}
248455d3ea17SRyan Stone }
248555d3ea17SRyan Stone 
248655d3ea17SRyan Stone 
248755d3ea17SRyan Stone static void
248855d3ea17SRyan Stone pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos)
248955d3ea17SRyan Stone {
249055d3ea17SRyan Stone 	uint32_t ctl2;
249155d3ea17SRyan Stone 
249255d3ea17SRyan Stone 	ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4);
249355d3ea17SRyan Stone 	ctl2 |= PCIEM_CTL2_ARI;
249455d3ea17SRyan Stone 	pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4);
249555d3ea17SRyan Stone 
249655d3ea17SRyan Stone 	sc->flags |= PCIB_ENABLE_ARI;
249755d3ea17SRyan Stone }
249855d3ea17SRyan Stone 
249955d3ea17SRyan Stone /*
2500bb0d0a8eSMike Smith  * PCIB interface.
2501bb0d0a8eSMike Smith  */
25026f0d5884SJohn Baldwin int
2503bb0d0a8eSMike Smith pcib_maxslots(device_t dev)
2504bb0d0a8eSMike Smith {
25054fa59183SMike Smith 	return (PCI_SLOTMAX);
2506bb0d0a8eSMike Smith }
2507bb0d0a8eSMike Smith 
250855d3ea17SRyan Stone static int
250955d3ea17SRyan Stone pcib_ari_maxslots(device_t dev)
251055d3ea17SRyan Stone {
251155d3ea17SRyan Stone 	struct pcib_softc *sc;
251255d3ea17SRyan Stone 
251355d3ea17SRyan Stone 	sc = device_get_softc(dev);
251455d3ea17SRyan Stone 
251555d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI)
251655d3ea17SRyan Stone 		return (PCIE_ARI_SLOTMAX);
251755d3ea17SRyan Stone 	else
251855d3ea17SRyan Stone 		return (PCI_SLOTMAX);
251955d3ea17SRyan Stone }
252055d3ea17SRyan Stone 
252155d3ea17SRyan Stone static int
252255d3ea17SRyan Stone pcib_ari_maxfuncs(device_t dev)
252355d3ea17SRyan Stone {
252455d3ea17SRyan Stone 	struct pcib_softc *sc;
252555d3ea17SRyan Stone 
252655d3ea17SRyan Stone 	sc = device_get_softc(dev);
252755d3ea17SRyan Stone 
252855d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI)
252955d3ea17SRyan Stone 		return (PCIE_ARI_FUNCMAX);
253055d3ea17SRyan Stone 	else
253155d3ea17SRyan Stone 		return (PCI_FUNCMAX);
253255d3ea17SRyan Stone }
253355d3ea17SRyan Stone 
25342397d2d8SRyan Stone static void
25352397d2d8SRyan Stone pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot,
25362397d2d8SRyan Stone     int *func)
25372397d2d8SRyan Stone {
25382397d2d8SRyan Stone 	struct pcib_softc *sc;
25392397d2d8SRyan Stone 
25402397d2d8SRyan Stone 	sc = device_get_softc(pcib);
25412397d2d8SRyan Stone 
25422397d2d8SRyan Stone 	*bus = PCI_RID2BUS(rid);
25432397d2d8SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
25442397d2d8SRyan Stone 		*slot = PCIE_ARI_RID2SLOT(rid);
25452397d2d8SRyan Stone 		*func = PCIE_ARI_RID2FUNC(rid);
25462397d2d8SRyan Stone 	} else {
25472397d2d8SRyan Stone 		*slot = PCI_RID2SLOT(rid);
25482397d2d8SRyan Stone 		*func = PCI_RID2FUNC(rid);
25492397d2d8SRyan Stone 	}
25502397d2d8SRyan Stone }
25512397d2d8SRyan Stone 
2552bb0d0a8eSMike Smith /*
2553bb0d0a8eSMike Smith  * Since we are a child of a PCI bus, its parent must support the pcib interface.
2554bb0d0a8eSMike Smith  */
255555d3ea17SRyan Stone static uint32_t
2556795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
2557bb0d0a8eSMike Smith {
255882cb5c3bSJohn Baldwin #ifdef PCI_HP
255982cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
256055d3ea17SRyan Stone 
256182cb5c3bSJohn Baldwin 	sc = device_get_softc(dev);
256282cb5c3bSJohn Baldwin 	if (!pcib_present(sc)) {
256382cb5c3bSJohn Baldwin 		switch (width) {
256482cb5c3bSJohn Baldwin 		case 2:
256582cb5c3bSJohn Baldwin 			return (0xffff);
256682cb5c3bSJohn Baldwin 		case 1:
256782cb5c3bSJohn Baldwin 			return (0xff);
256882cb5c3bSJohn Baldwin 		default:
256982cb5c3bSJohn Baldwin 			return (0xffffffff);
257082cb5c3bSJohn Baldwin 		}
257182cb5c3bSJohn Baldwin 	}
257282cb5c3bSJohn Baldwin #endif
257355d3ea17SRyan Stone 	pcib_xlate_ari(dev, b, &s, &f);
257455d3ea17SRyan Stone 	return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s,
257555d3ea17SRyan Stone 	    f, reg, width));
2576bb0d0a8eSMike Smith }
2577bb0d0a8eSMike Smith 
257855d3ea17SRyan Stone static void
2579795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
2580bb0d0a8eSMike Smith {
258182cb5c3bSJohn Baldwin #ifdef PCI_HP
258282cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
258355d3ea17SRyan Stone 
258482cb5c3bSJohn Baldwin 	sc = device_get_softc(dev);
258582cb5c3bSJohn Baldwin 	if (!pcib_present(sc))
258682cb5c3bSJohn Baldwin 		return;
258782cb5c3bSJohn Baldwin #endif
258855d3ea17SRyan Stone 	pcib_xlate_ari(dev, b, &s, &f);
258955d3ea17SRyan Stone 	PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f,
259055d3ea17SRyan Stone 	    reg, val, width);
2591bb0d0a8eSMike Smith }
2592bb0d0a8eSMike Smith 
2593bb0d0a8eSMike Smith /*
2594bb0d0a8eSMike Smith  * Route an interrupt across a PCI bridge.
2595bb0d0a8eSMike Smith  */
25962c2d1d07SBenno Rice int
2597bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin)
2598bb0d0a8eSMike Smith {
2599bb0d0a8eSMike Smith     device_t	bus;
2600bb0d0a8eSMike Smith     int		parent_intpin;
2601bb0d0a8eSMike Smith     int		intnum;
2602bb0d0a8eSMike Smith 
2603bb0d0a8eSMike Smith     /*
2604bb0d0a8eSMike Smith      *
2605bb0d0a8eSMike Smith      * The PCI standard defines a swizzle of the child-side device/intpin to
2606bb0d0a8eSMike Smith      * the parent-side intpin as follows.
2607bb0d0a8eSMike Smith      *
2608bb0d0a8eSMike Smith      * device = device on child bus
2609bb0d0a8eSMike Smith      * child_intpin = intpin on child bus slot (0-3)
2610bb0d0a8eSMike Smith      * parent_intpin = intpin on parent bus slot (0-3)
2611bb0d0a8eSMike Smith      *
2612bb0d0a8eSMike Smith      * parent_intpin = (device + child_intpin) % 4
2613bb0d0a8eSMike Smith      */
2614cdc95e1bSBernd Walter     parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
2615bb0d0a8eSMike Smith 
2616bb0d0a8eSMike Smith     /*
2617bb0d0a8eSMike Smith      * Our parent is a PCI bus.  Its parent must export the pcib interface
2618bb0d0a8eSMike Smith      * which includes the ability to route interrupts.
2619bb0d0a8eSMike Smith      */
2620bb0d0a8eSMike Smith     bus = device_get_parent(pcib);
2621bb0d0a8eSMike Smith     intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
262239981fedSJohn Baldwin     if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
2623c6a121abSJohn Baldwin 	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
2624c6a121abSJohn Baldwin 	    pci_get_slot(dev), 'A' + pin - 1, intnum);
26258046c4b9SMike Smith     }
2626bb0d0a8eSMike Smith     return(intnum);
2627bb0d0a8eSMike Smith }
2628b173edafSJohn Baldwin 
2629e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
26309bf4c9c1SJohn Baldwin int
26319bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
26329bf4c9c1SJohn Baldwin {
2633bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
26349bf4c9c1SJohn Baldwin 	device_t bus;
26359bf4c9c1SJohn Baldwin 
263622bf1c7fSJohn Baldwin 	if (sc->flags & PCIB_DISABLE_MSI)
263722bf1c7fSJohn Baldwin 		return (ENXIO);
26389bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
26399bf4c9c1SJohn Baldwin 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
26409bf4c9c1SJohn Baldwin 	    irqs));
26419bf4c9c1SJohn Baldwin }
26429bf4c9c1SJohn Baldwin 
2643e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */
26449bf4c9c1SJohn Baldwin int
26459bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
26469bf4c9c1SJohn Baldwin {
26479bf4c9c1SJohn Baldwin 	device_t bus;
26489bf4c9c1SJohn Baldwin 
26499bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
26509bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
26519bf4c9c1SJohn Baldwin }
26529bf4c9c1SJohn Baldwin 
26539bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */
26549bf4c9c1SJohn Baldwin int
2655e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
26569bf4c9c1SJohn Baldwin {
2657bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
26589bf4c9c1SJohn Baldwin 	device_t bus;
26599bf4c9c1SJohn Baldwin 
266068e9cbd3SMarius Strobl 	if (sc->flags & PCIB_DISABLE_MSIX)
266122bf1c7fSJohn Baldwin 		return (ENXIO);
26629bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
2663e706f7f0SJohn Baldwin 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
26645fe82bcaSJohn Baldwin }
26655fe82bcaSJohn Baldwin 
26669bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */
26679bf4c9c1SJohn Baldwin int
26689bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq)
26699bf4c9c1SJohn Baldwin {
26709bf4c9c1SJohn Baldwin 	device_t bus;
26719bf4c9c1SJohn Baldwin 
26729bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
26739bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
26749bf4c9c1SJohn Baldwin }
26759bf4c9c1SJohn Baldwin 
2676e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */
2677e706f7f0SJohn Baldwin int
2678e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
2679e706f7f0SJohn Baldwin     uint32_t *data)
2680e706f7f0SJohn Baldwin {
2681e706f7f0SJohn Baldwin 	device_t bus;
26824522ac77SLuoqi Chen 	int error;
2683e706f7f0SJohn Baldwin 
2684e706f7f0SJohn Baldwin 	bus = device_get_parent(pcib);
26854522ac77SLuoqi Chen 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
26864522ac77SLuoqi Chen 	if (error)
26874522ac77SLuoqi Chen 		return (error);
26884522ac77SLuoqi Chen 
26894522ac77SLuoqi Chen 	pci_ht_map_msi(pcib, *addr);
26904522ac77SLuoqi Chen 	return (0);
2691e706f7f0SJohn Baldwin }
2692e706f7f0SJohn Baldwin 
269362508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */
269462508c53SJohn Baldwin int
269562508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
269662508c53SJohn Baldwin {
269762508c53SJohn Baldwin 	device_t bus;
269862508c53SJohn Baldwin 
269962508c53SJohn Baldwin 	bus = device_get_parent(pcib);
270062508c53SJohn Baldwin 	return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate));
270162508c53SJohn Baldwin }
27025605a99eSRyan Stone 
27032397d2d8SRyan Stone static int
27042397d2d8SRyan Stone pcib_ari_enabled(device_t pcib)
27052397d2d8SRyan Stone {
27062397d2d8SRyan Stone 	struct pcib_softc *sc;
27072397d2d8SRyan Stone 
27082397d2d8SRyan Stone 	sc = device_get_softc(pcib);
27092397d2d8SRyan Stone 
27102397d2d8SRyan Stone 	return ((sc->flags & PCIB_ENABLE_ARI) != 0);
27112397d2d8SRyan Stone }
27122397d2d8SRyan Stone 
2713d7be980dSAndrew Turner static int
2714d7be980dSAndrew Turner pcib_ari_get_id(device_t pcib, device_t dev, enum pci_id_type type,
2715d7be980dSAndrew Turner     uintptr_t *id)
271655d3ea17SRyan Stone {
271755d3ea17SRyan Stone 	struct pcib_softc *sc;
27181e43b18cSAndrew Turner 	device_t bus_dev;
271955d3ea17SRyan Stone 	uint8_t bus, slot, func;
272055d3ea17SRyan Stone 
27211e43b18cSAndrew Turner 	if (type != PCI_ID_RID) {
27221e43b18cSAndrew Turner 		bus_dev = device_get_parent(pcib);
27231e43b18cSAndrew Turner 		return (PCIB_GET_ID(device_get_parent(bus_dev), dev, type, id));
27241e43b18cSAndrew Turner 	}
2725d7be980dSAndrew Turner 
272655d3ea17SRyan Stone 	sc = device_get_softc(pcib);
272755d3ea17SRyan Stone 
272855d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
272955d3ea17SRyan Stone 		bus = pci_get_bus(dev);
273055d3ea17SRyan Stone 		func = pci_get_function(dev);
273155d3ea17SRyan Stone 
2732d7be980dSAndrew Turner 		*id = (PCI_ARI_RID(bus, func));
273355d3ea17SRyan Stone 	} else {
273455d3ea17SRyan Stone 		bus = pci_get_bus(dev);
273555d3ea17SRyan Stone 		slot = pci_get_slot(dev);
273655d3ea17SRyan Stone 		func = pci_get_function(dev);
273755d3ea17SRyan Stone 
2738d7be980dSAndrew Turner 		*id = (PCI_RID(bus, slot, func));
273955d3ea17SRyan Stone 	}
2740d7be980dSAndrew Turner 
2741d7be980dSAndrew Turner 	return (0);
274255d3ea17SRyan Stone }
274355d3ea17SRyan Stone 
274455d3ea17SRyan Stone /*
274555d3ea17SRyan Stone  * Check that the downstream port (pcib) and the endpoint device (dev) both
274655d3ea17SRyan Stone  * support ARI.  If so, enable it and return 0, otherwise return an error.
274755d3ea17SRyan Stone  */
274855d3ea17SRyan Stone static int
274955d3ea17SRyan Stone pcib_try_enable_ari(device_t pcib, device_t dev)
275055d3ea17SRyan Stone {
275155d3ea17SRyan Stone 	struct pcib_softc *sc;
275255d3ea17SRyan Stone 	int error;
275355d3ea17SRyan Stone 	uint32_t cap2;
275455d3ea17SRyan Stone 	int ari_cap_off;
275555d3ea17SRyan Stone 	uint32_t ari_ver;
275655d3ea17SRyan Stone 	uint32_t pcie_pos;
275755d3ea17SRyan Stone 
275855d3ea17SRyan Stone 	sc = device_get_softc(pcib);
275955d3ea17SRyan Stone 
276055d3ea17SRyan Stone 	/*
276155d3ea17SRyan Stone 	 * ARI is controlled in a register in the PCIe capability structure.
276255d3ea17SRyan Stone 	 * If the downstream port does not have the PCIe capability structure
276355d3ea17SRyan Stone 	 * then it does not support ARI.
276455d3ea17SRyan Stone 	 */
276555d3ea17SRyan Stone 	error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos);
276655d3ea17SRyan Stone 	if (error != 0)
276755d3ea17SRyan Stone 		return (ENODEV);
276855d3ea17SRyan Stone 
276955d3ea17SRyan Stone 	/* Check that the PCIe port advertises ARI support. */
277055d3ea17SRyan Stone 	cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4);
277155d3ea17SRyan Stone 	if (!(cap2 & PCIEM_CAP2_ARI))
277255d3ea17SRyan Stone 		return (ENODEV);
277355d3ea17SRyan Stone 
277455d3ea17SRyan Stone 	/*
277555d3ea17SRyan Stone 	 * Check that the endpoint device advertises ARI support via the ARI
277655d3ea17SRyan Stone 	 * extended capability structure.
277755d3ea17SRyan Stone 	 */
277855d3ea17SRyan Stone 	error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off);
277955d3ea17SRyan Stone 	if (error != 0)
278055d3ea17SRyan Stone 		return (ENODEV);
278155d3ea17SRyan Stone 
278255d3ea17SRyan Stone 	/*
278355d3ea17SRyan Stone 	 * Finally, check that the endpoint device supports the same version
278455d3ea17SRyan Stone 	 * of ARI that we do.
278555d3ea17SRyan Stone 	 */
278655d3ea17SRyan Stone 	ari_ver = pci_read_config(dev, ari_cap_off, 4);
278755d3ea17SRyan Stone 	if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) {
278855d3ea17SRyan Stone 		if (bootverbose)
278955d3ea17SRyan Stone 			device_printf(pcib,
279055d3ea17SRyan Stone 			    "Unsupported version of ARI (%d) detected\n",
279155d3ea17SRyan Stone 			    PCI_EXTCAP_VER(ari_ver));
279255d3ea17SRyan Stone 
279355d3ea17SRyan Stone 		return (ENXIO);
279455d3ea17SRyan Stone 	}
279555d3ea17SRyan Stone 
279655d3ea17SRyan Stone 	pcib_enable_ari(sc, pcie_pos);
279755d3ea17SRyan Stone 
279855d3ea17SRyan Stone 	return (0);
279955d3ea17SRyan Stone }
2800