xref: /freebsd/sys/dev/pci/pci_pci.c (revision 4cb6772936ebacb24b373822591bb2d3ec55e25d)
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
79*4cb67729SWarner Losh static int		pcib_request_feature(device_t pcib, device_t dev,
80*4cb67729SWarner Losh 			    enum pci_feature feature);
81bb0d0a8eSMike Smith 
82bb0d0a8eSMike Smith static device_method_t pcib_methods[] = {
83bb0d0a8eSMike Smith     /* Device interface */
84bb0d0a8eSMike Smith     DEVMETHOD(device_probe,		pcib_probe),
85bb0d0a8eSMike Smith     DEVMETHOD(device_attach,		pcib_attach),
866f33eaa5SJohn Baldwin     DEVMETHOD(device_detach,		pcib_detach),
87bb0d0a8eSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
88e36af292SJung-uk Kim     DEVMETHOD(device_suspend,		pcib_suspend),
89e36af292SJung-uk Kim     DEVMETHOD(device_resume,		pcib_resume),
90bb0d0a8eSMike Smith 
91bb0d0a8eSMike Smith     /* Bus interface */
9282cb5c3bSJohn Baldwin     DEVMETHOD(bus_child_present,	pcib_child_present),
93bb0d0a8eSMike Smith     DEVMETHOD(bus_read_ivar,		pcib_read_ivar),
94bb0d0a8eSMike Smith     DEVMETHOD(bus_write_ivar,		pcib_write_ivar),
95bb0d0a8eSMike Smith     DEVMETHOD(bus_alloc_resource,	pcib_alloc_resource),
9683c41143SJohn Baldwin #ifdef NEW_PCIB
9783c41143SJohn Baldwin     DEVMETHOD(bus_adjust_resource,	pcib_adjust_resource),
9883c41143SJohn Baldwin     DEVMETHOD(bus_release_resource,	pcib_release_resource),
9983c41143SJohn Baldwin #else
100d2c9344fSJohn Baldwin     DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
101bb0d0a8eSMike Smith     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
10283c41143SJohn Baldwin #endif
103bb0d0a8eSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
104bb0d0a8eSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
105bb0d0a8eSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
106bb0d0a8eSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
107bb0d0a8eSMike Smith 
108bb0d0a8eSMike Smith     /* pcib interface */
10955d3ea17SRyan Stone     DEVMETHOD(pcib_maxslots,		pcib_ari_maxslots),
11055d3ea17SRyan Stone     DEVMETHOD(pcib_maxfuncs,		pcib_ari_maxfuncs),
111bb0d0a8eSMike Smith     DEVMETHOD(pcib_read_config,		pcib_read_config),
112bb0d0a8eSMike Smith     DEVMETHOD(pcib_write_config,	pcib_write_config),
113bb0d0a8eSMike Smith     DEVMETHOD(pcib_route_interrupt,	pcib_route_interrupt),
1149bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_alloc_msi,		pcib_alloc_msi),
1159bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msi,		pcib_release_msi),
1169bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_alloc_msix,		pcib_alloc_msix),
1179bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msix,	pcib_release_msix),
118e706f7f0SJohn Baldwin     DEVMETHOD(pcib_map_msi,		pcib_map_msi),
11962508c53SJohn Baldwin     DEVMETHOD(pcib_power_for_sleep,	pcib_power_for_sleep),
120d7be980dSAndrew Turner     DEVMETHOD(pcib_get_id,		pcib_ari_get_id),
12155d3ea17SRyan Stone     DEVMETHOD(pcib_try_enable_ari,	pcib_try_enable_ari),
1222397d2d8SRyan Stone     DEVMETHOD(pcib_ari_enabled,		pcib_ari_enabled),
1232397d2d8SRyan Stone     DEVMETHOD(pcib_decode_rid,		pcib_ari_decode_rid),
124*4cb67729SWarner Losh     DEVMETHOD(pcib_request_feature,	pcib_request_feature),
125bb0d0a8eSMike Smith 
1264b7ec270SMarius Strobl     DEVMETHOD_END
127bb0d0a8eSMike Smith };
128bb0d0a8eSMike Smith 
12904dda605SJohn Baldwin static devclass_t pcib_devclass;
130bb0d0a8eSMike Smith 
13104dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc));
13268e9cbd3SMarius Strobl DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, NULL, NULL);
133bb0d0a8eSMike Smith 
1346ca2d094SBjoern A. Zeeb #if defined(NEW_PCIB) || defined(PCI_HP)
1350070c94bSJohn Baldwin SYSCTL_DECL(_hw_pci);
1366ca2d094SBjoern A. Zeeb #endif
1370070c94bSJohn Baldwin 
1386ca2d094SBjoern A. Zeeb #ifdef NEW_PCIB
1390070c94bSJohn Baldwin static int pci_clear_pcib;
1400070c94bSJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0,
1410070c94bSJohn Baldwin     "Clear firmware-assigned resources for PCI-PCI bridge I/O windows.");
14283c41143SJohn Baldwin 
14383c41143SJohn Baldwin /*
14483c41143SJohn Baldwin  * Is a resource from a child device sub-allocated from one of our
14583c41143SJohn Baldwin  * resource managers?
14683c41143SJohn Baldwin  */
14783c41143SJohn Baldwin static int
14883c41143SJohn Baldwin pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r)
14983c41143SJohn Baldwin {
15083c41143SJohn Baldwin 
15183c41143SJohn Baldwin 	switch (type) {
1524edef187SJohn Baldwin #ifdef PCI_RES_BUS
1534edef187SJohn Baldwin 	case PCI_RES_BUS:
1544edef187SJohn Baldwin 		return (rman_is_region_manager(r, &sc->bus.rman));
1554edef187SJohn Baldwin #endif
15683c41143SJohn Baldwin 	case SYS_RES_IOPORT:
15783c41143SJohn Baldwin 		return (rman_is_region_manager(r, &sc->io.rman));
15883c41143SJohn Baldwin 	case SYS_RES_MEMORY:
15983c41143SJohn Baldwin 		/* Prefetchable resources may live in either memory rman. */
16083c41143SJohn Baldwin 		if (rman_get_flags(r) & RF_PREFETCHABLE &&
16183c41143SJohn Baldwin 		    rman_is_region_manager(r, &sc->pmem.rman))
16283c41143SJohn Baldwin 			return (1);
16383c41143SJohn Baldwin 		return (rman_is_region_manager(r, &sc->mem.rman));
16483c41143SJohn Baldwin 	}
16583c41143SJohn Baldwin 	return (0);
16683c41143SJohn Baldwin }
16783c41143SJohn Baldwin 
16883c41143SJohn Baldwin static int
16983c41143SJohn Baldwin pcib_is_window_open(struct pcib_window *pw)
17083c41143SJohn Baldwin {
17183c41143SJohn Baldwin 
17283c41143SJohn Baldwin 	return (pw->valid && pw->base < pw->limit);
17383c41143SJohn Baldwin }
17483c41143SJohn Baldwin 
17583c41143SJohn Baldwin /*
17683c41143SJohn Baldwin  * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and
17783c41143SJohn Baldwin  * handle for the resource, we could pass RF_ACTIVE up to the PCI bus
17883c41143SJohn Baldwin  * when allocating the resource windows and rely on the PCI bus driver
17983c41143SJohn Baldwin  * to do this for us.
18083c41143SJohn Baldwin  */
18183c41143SJohn Baldwin static void
18283c41143SJohn Baldwin pcib_activate_window(struct pcib_softc *sc, int type)
18383c41143SJohn Baldwin {
18483c41143SJohn Baldwin 
18583c41143SJohn Baldwin 	PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type);
18683c41143SJohn Baldwin }
18783c41143SJohn Baldwin 
18883c41143SJohn Baldwin static void
18983c41143SJohn Baldwin pcib_write_windows(struct pcib_softc *sc, int mask)
19083c41143SJohn Baldwin {
19183c41143SJohn Baldwin 	device_t dev;
19283c41143SJohn Baldwin 	uint32_t val;
19383c41143SJohn Baldwin 
19483c41143SJohn Baldwin 	dev = sc->dev;
19583c41143SJohn Baldwin 	if (sc->io.valid && mask & WIN_IO) {
19683c41143SJohn Baldwin 		val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
19783c41143SJohn Baldwin 		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
19883c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOBASEH_1,
19983c41143SJohn Baldwin 			    sc->io.base >> 16, 2);
20083c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOLIMITH_1,
20183c41143SJohn Baldwin 			    sc->io.limit >> 16, 2);
20283c41143SJohn Baldwin 		}
20383c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1);
20483c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1);
20583c41143SJohn Baldwin 	}
20683c41143SJohn Baldwin 
20783c41143SJohn Baldwin 	if (mask & WIN_MEM) {
20883c41143SJohn Baldwin 		pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2);
20983c41143SJohn Baldwin 		pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2);
21083c41143SJohn Baldwin 	}
21183c41143SJohn Baldwin 
21283c41143SJohn Baldwin 	if (sc->pmem.valid && mask & WIN_PMEM) {
21383c41143SJohn Baldwin 		val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
21483c41143SJohn Baldwin 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
21583c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMBASEH_1,
21683c41143SJohn Baldwin 			    sc->pmem.base >> 32, 4);
21783c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMLIMITH_1,
21883c41143SJohn Baldwin 			    sc->pmem.limit >> 32, 4);
21983c41143SJohn Baldwin 		}
22083c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2);
22183c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2);
22283c41143SJohn Baldwin 	}
22383c41143SJohn Baldwin }
22483c41143SJohn Baldwin 
225c825d4dcSJohn Baldwin /*
226c825d4dcSJohn Baldwin  * This is used to reject I/O port allocations that conflict with an
227c825d4dcSJohn Baldwin  * ISA alias range.
228c825d4dcSJohn Baldwin  */
229c825d4dcSJohn Baldwin static int
2302dd1bdf1SJustin Hibbits pcib_is_isa_range(struct pcib_softc *sc, rman_res_t start, rman_res_t end,
2312dd1bdf1SJustin Hibbits     rman_res_t count)
232c825d4dcSJohn Baldwin {
2332dd1bdf1SJustin Hibbits 	rman_res_t next_alias;
234c825d4dcSJohn Baldwin 
235c825d4dcSJohn Baldwin 	if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE))
236c825d4dcSJohn Baldwin 		return (0);
237c825d4dcSJohn Baldwin 
238c825d4dcSJohn Baldwin 	/* Only check fixed ranges for overlap. */
239c825d4dcSJohn Baldwin 	if (start + count - 1 != end)
240c825d4dcSJohn Baldwin 		return (0);
241c825d4dcSJohn Baldwin 
242c825d4dcSJohn Baldwin 	/* ISA aliases are only in the lower 64KB of I/O space. */
243c825d4dcSJohn Baldwin 	if (start >= 65536)
244c825d4dcSJohn Baldwin 		return (0);
245c825d4dcSJohn Baldwin 
246c825d4dcSJohn Baldwin 	/* Check for overlap with 0x000 - 0x0ff as a special case. */
247c825d4dcSJohn Baldwin 	if (start < 0x100)
248c825d4dcSJohn Baldwin 		goto alias;
249c825d4dcSJohn Baldwin 
250c825d4dcSJohn Baldwin 	/*
251c825d4dcSJohn Baldwin 	 * If the start address is an alias, the range is an alias.
252c825d4dcSJohn Baldwin 	 * Otherwise, compute the start of the next alias range and
253c825d4dcSJohn Baldwin 	 * check if it is before the end of the candidate range.
254c825d4dcSJohn Baldwin 	 */
255c825d4dcSJohn Baldwin 	if ((start & 0x300) != 0)
256c825d4dcSJohn Baldwin 		goto alias;
257c825d4dcSJohn Baldwin 	next_alias = (start & ~0x3fful) | 0x100;
258c825d4dcSJohn Baldwin 	if (next_alias <= end)
259c825d4dcSJohn Baldwin 		goto alias;
260c825d4dcSJohn Baldwin 	return (0);
261c825d4dcSJohn Baldwin 
262c825d4dcSJohn Baldwin alias:
263c825d4dcSJohn Baldwin 	if (bootverbose)
264c825d4dcSJohn Baldwin 		device_printf(sc->dev,
265da1b038aSJustin Hibbits 		    "I/O range %#jx-%#jx overlaps with an ISA alias\n", start,
266c825d4dcSJohn Baldwin 		    end);
267c825d4dcSJohn Baldwin 	return (1);
268c825d4dcSJohn Baldwin }
269c825d4dcSJohn Baldwin 
270c825d4dcSJohn Baldwin static void
271c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res,
272c825d4dcSJohn Baldwin     int count)
273c825d4dcSJohn Baldwin {
274c825d4dcSJohn Baldwin 	struct resource **newarray;
275c825d4dcSJohn Baldwin 	int error, i;
276c825d4dcSJohn Baldwin 
277c825d4dcSJohn Baldwin 	newarray = malloc(sizeof(struct resource *) * (w->count + count),
278c825d4dcSJohn Baldwin 	    M_DEVBUF, M_WAITOK);
279c825d4dcSJohn Baldwin 	if (w->res != NULL)
280c825d4dcSJohn Baldwin 		bcopy(w->res, newarray, sizeof(struct resource *) * w->count);
281c825d4dcSJohn Baldwin 	bcopy(res, newarray + w->count, sizeof(struct resource *) * count);
282c825d4dcSJohn Baldwin 	free(w->res, M_DEVBUF);
283c825d4dcSJohn Baldwin 	w->res = newarray;
284c825d4dcSJohn Baldwin 	w->count += count;
285c825d4dcSJohn Baldwin 
286c825d4dcSJohn Baldwin 	for (i = 0; i < count; i++) {
287c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, rman_get_start(res[i]),
288c825d4dcSJohn Baldwin 		    rman_get_end(res[i]));
289c825d4dcSJohn Baldwin 		if (error)
290c825d4dcSJohn Baldwin 			panic("Failed to add resource to rman");
291c825d4dcSJohn Baldwin 	}
292c825d4dcSJohn Baldwin }
293c825d4dcSJohn Baldwin 
2942dd1bdf1SJustin Hibbits typedef void (nonisa_callback)(rman_res_t start, rman_res_t end, void *arg);
295c825d4dcSJohn Baldwin 
296c825d4dcSJohn Baldwin static void
2972dd1bdf1SJustin Hibbits pcib_walk_nonisa_ranges(rman_res_t start, rman_res_t end, nonisa_callback *cb,
298c825d4dcSJohn Baldwin     void *arg)
299c825d4dcSJohn Baldwin {
3002dd1bdf1SJustin Hibbits 	rman_res_t next_end;
301c825d4dcSJohn Baldwin 
302c825d4dcSJohn Baldwin 	/*
303c825d4dcSJohn Baldwin 	 * If start is within an ISA alias range, move up to the start
304c825d4dcSJohn Baldwin 	 * of the next non-alias range.  As a special case, addresses
305c825d4dcSJohn Baldwin 	 * in the range 0x000 - 0x0ff should also be skipped since
306c825d4dcSJohn Baldwin 	 * those are used for various system I/O devices in ISA
307c825d4dcSJohn Baldwin 	 * systems.
308c825d4dcSJohn Baldwin 	 */
309c825d4dcSJohn Baldwin 	if (start <= 65535) {
310c825d4dcSJohn Baldwin 		if (start < 0x100 || (start & 0x300) != 0) {
311c825d4dcSJohn Baldwin 			start &= ~0x3ff;
312c825d4dcSJohn Baldwin 			start += 0x400;
313c825d4dcSJohn Baldwin 		}
314c825d4dcSJohn Baldwin 	}
315c825d4dcSJohn Baldwin 
316c825d4dcSJohn Baldwin 	/* ISA aliases are only in the lower 64KB of I/O space. */
317c825d4dcSJohn Baldwin 	while (start <= MIN(end, 65535)) {
318c825d4dcSJohn Baldwin 		next_end = MIN(start | 0xff, end);
319c825d4dcSJohn Baldwin 		cb(start, next_end, arg);
320c825d4dcSJohn Baldwin 		start += 0x400;
321c825d4dcSJohn Baldwin 	}
322c825d4dcSJohn Baldwin 
323c825d4dcSJohn Baldwin 	if (start <= end)
324c825d4dcSJohn Baldwin 		cb(start, end, arg);
325c825d4dcSJohn Baldwin }
326c825d4dcSJohn Baldwin 
327c825d4dcSJohn Baldwin static void
3282dd1bdf1SJustin Hibbits count_ranges(rman_res_t start, rman_res_t end, void *arg)
329c825d4dcSJohn Baldwin {
330c825d4dcSJohn Baldwin 	int *countp;
331c825d4dcSJohn Baldwin 
332c825d4dcSJohn Baldwin 	countp = arg;
333c825d4dcSJohn Baldwin 	(*countp)++;
334c825d4dcSJohn Baldwin }
335c825d4dcSJohn Baldwin 
336c825d4dcSJohn Baldwin struct alloc_state {
337c825d4dcSJohn Baldwin 	struct resource **res;
338c825d4dcSJohn Baldwin 	struct pcib_softc *sc;
339c825d4dcSJohn Baldwin 	int count, error;
340c825d4dcSJohn Baldwin };
341c825d4dcSJohn Baldwin 
342c825d4dcSJohn Baldwin static void
3432dd1bdf1SJustin Hibbits alloc_ranges(rman_res_t start, rman_res_t end, void *arg)
344c825d4dcSJohn Baldwin {
345c825d4dcSJohn Baldwin 	struct alloc_state *as;
346c825d4dcSJohn Baldwin 	struct pcib_window *w;
347c825d4dcSJohn Baldwin 	int rid;
348c825d4dcSJohn Baldwin 
349c825d4dcSJohn Baldwin 	as = arg;
350c825d4dcSJohn Baldwin 	if (as->error != 0)
351c825d4dcSJohn Baldwin 		return;
352c825d4dcSJohn Baldwin 
353c825d4dcSJohn Baldwin 	w = &as->sc->io;
354c825d4dcSJohn Baldwin 	rid = w->reg;
355c825d4dcSJohn Baldwin 	if (bootverbose)
356c825d4dcSJohn Baldwin 		device_printf(as->sc->dev,
357da1b038aSJustin Hibbits 		    "allocating non-ISA range %#jx-%#jx\n", start, end);
358c825d4dcSJohn Baldwin 	as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT,
359c825d4dcSJohn Baldwin 	    &rid, start, end, end - start + 1, 0);
360c825d4dcSJohn Baldwin 	if (as->res[as->count] == NULL)
361c825d4dcSJohn Baldwin 		as->error = ENXIO;
362c825d4dcSJohn Baldwin 	else
363c825d4dcSJohn Baldwin 		as->count++;
364c825d4dcSJohn Baldwin }
365c825d4dcSJohn Baldwin 
366c825d4dcSJohn Baldwin static int
3672dd1bdf1SJustin Hibbits pcib_alloc_nonisa_ranges(struct pcib_softc *sc, rman_res_t start, rman_res_t end)
368c825d4dcSJohn Baldwin {
369c825d4dcSJohn Baldwin 	struct alloc_state as;
370c825d4dcSJohn Baldwin 	int i, new_count;
371c825d4dcSJohn Baldwin 
372c825d4dcSJohn Baldwin 	/* First, see how many ranges we need. */
373c825d4dcSJohn Baldwin 	new_count = 0;
374c825d4dcSJohn Baldwin 	pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count);
375c825d4dcSJohn Baldwin 
376c825d4dcSJohn Baldwin 	/* Second, allocate the ranges. */
377c825d4dcSJohn Baldwin 	as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF,
378c825d4dcSJohn Baldwin 	    M_WAITOK);
379c825d4dcSJohn Baldwin 	as.sc = sc;
380c825d4dcSJohn Baldwin 	as.count = 0;
381c825d4dcSJohn Baldwin 	as.error = 0;
382c825d4dcSJohn Baldwin 	pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as);
383c825d4dcSJohn Baldwin 	if (as.error != 0) {
384c825d4dcSJohn Baldwin 		for (i = 0; i < as.count; i++)
385c825d4dcSJohn Baldwin 			bus_release_resource(sc->dev, SYS_RES_IOPORT,
386c825d4dcSJohn Baldwin 			    sc->io.reg, as.res[i]);
387c825d4dcSJohn Baldwin 		free(as.res, M_DEVBUF);
388c825d4dcSJohn Baldwin 		return (as.error);
389c825d4dcSJohn Baldwin 	}
390c825d4dcSJohn Baldwin 	KASSERT(as.count == new_count, ("%s: count mismatch", __func__));
391c825d4dcSJohn Baldwin 
392c825d4dcSJohn Baldwin 	/* Third, add the ranges to the window. */
393c825d4dcSJohn Baldwin 	pcib_add_window_resources(&sc->io, as.res, as.count);
394c825d4dcSJohn Baldwin 	free(as.res, M_DEVBUF);
395c825d4dcSJohn Baldwin 	return (0);
396c825d4dcSJohn Baldwin }
397c825d4dcSJohn Baldwin 
39883c41143SJohn Baldwin static void
39983c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type,
40083c41143SJohn Baldwin     int flags, pci_addr_t max_address)
40183c41143SJohn Baldwin {
402c825d4dcSJohn Baldwin 	struct resource *res;
40383c41143SJohn Baldwin 	char buf[64];
40483c41143SJohn Baldwin 	int error, rid;
40583c41143SJohn Baldwin 
40689977ce2SJustin Hibbits 	if (max_address != (rman_res_t)max_address)
407534ccd7bSJustin Hibbits 		max_address = ~0;
40883c41143SJohn Baldwin 	w->rman.rm_start = 0;
40983c41143SJohn Baldwin 	w->rman.rm_end = max_address;
41083c41143SJohn Baldwin 	w->rman.rm_type = RMAN_ARRAY;
41183c41143SJohn Baldwin 	snprintf(buf, sizeof(buf), "%s %s window",
41283c41143SJohn Baldwin 	    device_get_nameunit(sc->dev), w->name);
41383c41143SJohn Baldwin 	w->rman.rm_descr = strdup(buf, M_DEVBUF);
41483c41143SJohn Baldwin 	error = rman_init(&w->rman);
41583c41143SJohn Baldwin 	if (error)
41683c41143SJohn Baldwin 		panic("Failed to initialize %s %s rman",
41783c41143SJohn Baldwin 		    device_get_nameunit(sc->dev), w->name);
41883c41143SJohn Baldwin 
41983c41143SJohn Baldwin 	if (!pcib_is_window_open(w))
42083c41143SJohn Baldwin 		return;
42183c41143SJohn Baldwin 
42283c41143SJohn Baldwin 	if (w->base > max_address || w->limit > max_address) {
42383c41143SJohn Baldwin 		device_printf(sc->dev,
42483c41143SJohn Baldwin 		    "initial %s window has too many bits, ignoring\n", w->name);
42583c41143SJohn Baldwin 		return;
42683c41143SJohn Baldwin 	}
427c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE)
428c825d4dcSJohn Baldwin 		(void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit);
429c825d4dcSJohn Baldwin 	else {
43083c41143SJohn Baldwin 		rid = w->reg;
431c825d4dcSJohn Baldwin 		res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit,
43283c41143SJohn Baldwin 		    w->limit - w->base + 1, flags);
433c825d4dcSJohn Baldwin 		if (res != NULL)
434c825d4dcSJohn Baldwin 			pcib_add_window_resources(w, &res, 1);
435c825d4dcSJohn Baldwin 	}
43683c41143SJohn Baldwin 	if (w->res == NULL) {
43783c41143SJohn Baldwin 		device_printf(sc->dev,
43883c41143SJohn Baldwin 		    "failed to allocate initial %s window: %#jx-%#jx\n",
43983c41143SJohn Baldwin 		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
44083c41143SJohn Baldwin 		w->base = max_address;
44183c41143SJohn Baldwin 		w->limit = 0;
44283c41143SJohn Baldwin 		pcib_write_windows(sc, w->mask);
44383c41143SJohn Baldwin 		return;
44483c41143SJohn Baldwin 	}
44583c41143SJohn Baldwin 	pcib_activate_window(sc, type);
44683c41143SJohn Baldwin }
44783c41143SJohn Baldwin 
44883c41143SJohn Baldwin /*
44983c41143SJohn Baldwin  * Initialize I/O windows.
45083c41143SJohn Baldwin  */
45183c41143SJohn Baldwin static void
45283c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc)
45383c41143SJohn Baldwin {
45483c41143SJohn Baldwin 	pci_addr_t max;
45583c41143SJohn Baldwin 	device_t dev;
45683c41143SJohn Baldwin 	uint32_t val;
45783c41143SJohn Baldwin 
45883c41143SJohn Baldwin 	dev = sc->dev;
45983c41143SJohn Baldwin 
4600070c94bSJohn Baldwin 	if (pci_clear_pcib) {
461809923caSJustin Hibbits 		pcib_bridge_init(dev);
4620070c94bSJohn Baldwin 	}
4630070c94bSJohn Baldwin 
46483c41143SJohn Baldwin 	/* Determine if the I/O port window is implemented. */
46583c41143SJohn Baldwin 	val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
46683c41143SJohn Baldwin 	if (val == 0) {
46783c41143SJohn Baldwin 		/*
46883c41143SJohn Baldwin 		 * If 'val' is zero, then only 16-bits of I/O space
46983c41143SJohn Baldwin 		 * are supported.
47083c41143SJohn Baldwin 		 */
47183c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
47283c41143SJohn Baldwin 		if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) {
47383c41143SJohn Baldwin 			sc->io.valid = 1;
47483c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOBASEL_1, 0, 1);
47583c41143SJohn Baldwin 		}
47683c41143SJohn Baldwin 	} else
47783c41143SJohn Baldwin 		sc->io.valid = 1;
47883c41143SJohn Baldwin 
47983c41143SJohn Baldwin 	/* Read the existing I/O port window. */
48083c41143SJohn Baldwin 	if (sc->io.valid) {
48183c41143SJohn Baldwin 		sc->io.reg = PCIR_IOBASEL_1;
48283c41143SJohn Baldwin 		sc->io.step = 12;
48383c41143SJohn Baldwin 		sc->io.mask = WIN_IO;
48483c41143SJohn Baldwin 		sc->io.name = "I/O port";
48583c41143SJohn Baldwin 		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
48683c41143SJohn Baldwin 			sc->io.base = PCI_PPBIOBASE(
48783c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOBASEH_1, 2), val);
48883c41143SJohn Baldwin 			sc->io.limit = PCI_PPBIOLIMIT(
48983c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITH_1, 2),
49083c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
49183c41143SJohn Baldwin 			max = 0xffffffff;
49283c41143SJohn Baldwin 		} else {
49383c41143SJohn Baldwin 			sc->io.base = PCI_PPBIOBASE(0, val);
49483c41143SJohn Baldwin 			sc->io.limit = PCI_PPBIOLIMIT(0,
49583c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
49683c41143SJohn Baldwin 			max = 0xffff;
49783c41143SJohn Baldwin 		}
49883c41143SJohn Baldwin 		pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max);
49983c41143SJohn Baldwin 	}
50083c41143SJohn Baldwin 
50183c41143SJohn Baldwin 	/* Read the existing memory window. */
50283c41143SJohn Baldwin 	sc->mem.valid = 1;
50383c41143SJohn Baldwin 	sc->mem.reg = PCIR_MEMBASE_1;
50483c41143SJohn Baldwin 	sc->mem.step = 20;
50583c41143SJohn Baldwin 	sc->mem.mask = WIN_MEM;
50683c41143SJohn Baldwin 	sc->mem.name = "memory";
50783c41143SJohn Baldwin 	sc->mem.base = PCI_PPBMEMBASE(0,
50883c41143SJohn Baldwin 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
50983c41143SJohn Baldwin 	sc->mem.limit = PCI_PPBMEMLIMIT(0,
51083c41143SJohn Baldwin 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
51183c41143SJohn Baldwin 	pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff);
51283c41143SJohn Baldwin 
51383c41143SJohn Baldwin 	/* Determine if the prefetchable memory window is implemented. */
51483c41143SJohn Baldwin 	val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
51583c41143SJohn Baldwin 	if (val == 0) {
51683c41143SJohn Baldwin 		/*
51783c41143SJohn Baldwin 		 * If 'val' is zero, then only 32-bits of memory space
51883c41143SJohn Baldwin 		 * are supported.
51983c41143SJohn Baldwin 		 */
52083c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
52183c41143SJohn Baldwin 		if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) {
52283c41143SJohn Baldwin 			sc->pmem.valid = 1;
52383c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMBASEL_1, 0, 2);
52483c41143SJohn Baldwin 		}
52583c41143SJohn Baldwin 	} else
52683c41143SJohn Baldwin 		sc->pmem.valid = 1;
52783c41143SJohn Baldwin 
52883c41143SJohn Baldwin 	/* Read the existing prefetchable memory window. */
52983c41143SJohn Baldwin 	if (sc->pmem.valid) {
53083c41143SJohn Baldwin 		sc->pmem.reg = PCIR_PMBASEL_1;
53183c41143SJohn Baldwin 		sc->pmem.step = 20;
53283c41143SJohn Baldwin 		sc->pmem.mask = WIN_PMEM;
53383c41143SJohn Baldwin 		sc->pmem.name = "prefetch";
53483c41143SJohn Baldwin 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
53583c41143SJohn Baldwin 			sc->pmem.base = PCI_PPBMEMBASE(
53683c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMBASEH_1, 4), val);
53783c41143SJohn Baldwin 			sc->pmem.limit = PCI_PPBMEMLIMIT(
53883c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITH_1, 4),
53983c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
54083c41143SJohn Baldwin 			max = 0xffffffffffffffff;
54183c41143SJohn Baldwin 		} else {
54283c41143SJohn Baldwin 			sc->pmem.base = PCI_PPBMEMBASE(0, val);
54383c41143SJohn Baldwin 			sc->pmem.limit = PCI_PPBMEMLIMIT(0,
54483c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
54583c41143SJohn Baldwin 			max = 0xffffffff;
54683c41143SJohn Baldwin 		}
54783c41143SJohn Baldwin 		pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY,
54883c41143SJohn Baldwin 		    RF_PREFETCHABLE, max);
54983c41143SJohn Baldwin 	}
55083c41143SJohn Baldwin }
55183c41143SJohn Baldwin 
5526f33eaa5SJohn Baldwin static void
5536f33eaa5SJohn Baldwin pcib_release_window(struct pcib_softc *sc, struct pcib_window *w, int type)
5546f33eaa5SJohn Baldwin {
5556f33eaa5SJohn Baldwin 	device_t dev;
5566f33eaa5SJohn Baldwin 	int error, i;
5576f33eaa5SJohn Baldwin 
5586f33eaa5SJohn Baldwin 	if (!w->valid)
5596f33eaa5SJohn Baldwin 		return;
5606f33eaa5SJohn Baldwin 
5616f33eaa5SJohn Baldwin 	dev = sc->dev;
5626f33eaa5SJohn Baldwin 	error = rman_fini(&w->rman);
5636f33eaa5SJohn Baldwin 	if (error) {
5646f33eaa5SJohn Baldwin 		device_printf(dev, "failed to release %s rman\n", w->name);
5656f33eaa5SJohn Baldwin 		return;
5666f33eaa5SJohn Baldwin 	}
5676f33eaa5SJohn Baldwin 	free(__DECONST(char *, w->rman.rm_descr), M_DEVBUF);
5686f33eaa5SJohn Baldwin 
5696f33eaa5SJohn Baldwin 	for (i = 0; i < w->count; i++) {
5706f33eaa5SJohn Baldwin 		error = bus_free_resource(dev, type, w->res[i]);
5716f33eaa5SJohn Baldwin 		if (error)
5726f33eaa5SJohn Baldwin 			device_printf(dev,
5736f33eaa5SJohn Baldwin 			    "failed to release %s resource: %d\n", w->name,
5746f33eaa5SJohn Baldwin 			    error);
5756f33eaa5SJohn Baldwin 	}
5766f33eaa5SJohn Baldwin 	free(w->res, M_DEVBUF);
5776f33eaa5SJohn Baldwin }
5786f33eaa5SJohn Baldwin 
5796f33eaa5SJohn Baldwin static void
5806f33eaa5SJohn Baldwin pcib_free_windows(struct pcib_softc *sc)
5816f33eaa5SJohn Baldwin {
5826f33eaa5SJohn Baldwin 
5836f33eaa5SJohn Baldwin 	pcib_release_window(sc, &sc->pmem, SYS_RES_MEMORY);
5846f33eaa5SJohn Baldwin 	pcib_release_window(sc, &sc->mem, SYS_RES_MEMORY);
5856f33eaa5SJohn Baldwin 	pcib_release_window(sc, &sc->io, SYS_RES_IOPORT);
5866f33eaa5SJohn Baldwin }
5876f33eaa5SJohn Baldwin 
5884edef187SJohn Baldwin #ifdef PCI_RES_BUS
5894edef187SJohn Baldwin /*
5904edef187SJohn Baldwin  * Allocate a suitable secondary bus for this bridge if needed and
5914edef187SJohn Baldwin  * initialize the resource manager for the secondary bus range.  Note
5924edef187SJohn Baldwin  * that the minimum count is a desired value and this may allocate a
5934edef187SJohn Baldwin  * smaller range.
5944edef187SJohn Baldwin  */
5954edef187SJohn Baldwin void
5964edef187SJohn Baldwin pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count)
5974edef187SJohn Baldwin {
5984edef187SJohn Baldwin 	char buf[64];
599ad6f36f8SJohn Baldwin 	int error, rid, sec_reg;
6004edef187SJohn Baldwin 
6014edef187SJohn Baldwin 	switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) {
6024edef187SJohn Baldwin 	case PCIM_HDRTYPE_BRIDGE:
603ad6f36f8SJohn Baldwin 		sec_reg = PCIR_SECBUS_1;
6044edef187SJohn Baldwin 		bus->sub_reg = PCIR_SUBBUS_1;
6054edef187SJohn Baldwin 		break;
6064edef187SJohn Baldwin 	case PCIM_HDRTYPE_CARDBUS:
607ad6f36f8SJohn Baldwin 		sec_reg = PCIR_SECBUS_2;
6084edef187SJohn Baldwin 		bus->sub_reg = PCIR_SUBBUS_2;
6094edef187SJohn Baldwin 		break;
6104edef187SJohn Baldwin 	default:
6114edef187SJohn Baldwin 		panic("not a PCI bridge");
6124edef187SJohn Baldwin 	}
613ad6f36f8SJohn Baldwin 	bus->sec = pci_read_config(dev, sec_reg, 1);
614ad6f36f8SJohn Baldwin 	bus->sub = pci_read_config(dev, bus->sub_reg, 1);
6154edef187SJohn Baldwin 	bus->dev = dev;
6164edef187SJohn Baldwin 	bus->rman.rm_start = 0;
6174edef187SJohn Baldwin 	bus->rman.rm_end = PCI_BUSMAX;
6184edef187SJohn Baldwin 	bus->rman.rm_type = RMAN_ARRAY;
6194edef187SJohn Baldwin 	snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev));
6204edef187SJohn Baldwin 	bus->rman.rm_descr = strdup(buf, M_DEVBUF);
6214edef187SJohn Baldwin 	error = rman_init(&bus->rman);
6224edef187SJohn Baldwin 	if (error)
6234edef187SJohn Baldwin 		panic("Failed to initialize %s bus number rman",
6244edef187SJohn Baldwin 		    device_get_nameunit(dev));
6254edef187SJohn Baldwin 
6264edef187SJohn Baldwin 	/*
6274edef187SJohn Baldwin 	 * Allocate a bus range.  This will return an existing bus range
6284edef187SJohn Baldwin 	 * if one exists, or a new bus range if one does not.
6294edef187SJohn Baldwin 	 */
6304edef187SJohn Baldwin 	rid = 0;
631c47476d7SJustin Hibbits 	bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid,
6324edef187SJohn Baldwin 	    min_count, 0);
6334edef187SJohn Baldwin 	if (bus->res == NULL) {
6344edef187SJohn Baldwin 		/*
6354edef187SJohn Baldwin 		 * Fall back to just allocating a range of a single bus
6364edef187SJohn Baldwin 		 * number.
6374edef187SJohn Baldwin 		 */
638c47476d7SJustin Hibbits 		bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid,
6394edef187SJohn Baldwin 		    1, 0);
6404edef187SJohn Baldwin 	} else if (rman_get_size(bus->res) < min_count)
6414edef187SJohn Baldwin 		/*
6424edef187SJohn Baldwin 		 * Attempt to grow the existing range to satisfy the
6434edef187SJohn Baldwin 		 * minimum desired count.
6444edef187SJohn Baldwin 		 */
6454edef187SJohn Baldwin 		(void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res,
6464edef187SJohn Baldwin 		    rman_get_start(bus->res), rman_get_start(bus->res) +
6474edef187SJohn Baldwin 		    min_count - 1);
6484edef187SJohn Baldwin 
6494edef187SJohn Baldwin 	/*
6504edef187SJohn Baldwin 	 * Add the initial resource to the rman.
6514edef187SJohn Baldwin 	 */
6524edef187SJohn Baldwin 	if (bus->res != NULL) {
6534edef187SJohn Baldwin 		error = rman_manage_region(&bus->rman, rman_get_start(bus->res),
6544edef187SJohn Baldwin 		    rman_get_end(bus->res));
6554edef187SJohn Baldwin 		if (error)
6564edef187SJohn Baldwin 			panic("Failed to add resource to rman");
6574edef187SJohn Baldwin 		bus->sec = rman_get_start(bus->res);
6584edef187SJohn Baldwin 		bus->sub = rman_get_end(bus->res);
6594edef187SJohn Baldwin 	}
6604edef187SJohn Baldwin }
6614edef187SJohn Baldwin 
6626f33eaa5SJohn Baldwin void
6636f33eaa5SJohn Baldwin pcib_free_secbus(device_t dev, struct pcib_secbus *bus)
6646f33eaa5SJohn Baldwin {
6656f33eaa5SJohn Baldwin 	int error;
6666f33eaa5SJohn Baldwin 
6676f33eaa5SJohn Baldwin 	error = rman_fini(&bus->rman);
6686f33eaa5SJohn Baldwin 	if (error) {
6696f33eaa5SJohn Baldwin 		device_printf(dev, "failed to release bus number rman\n");
6706f33eaa5SJohn Baldwin 		return;
6716f33eaa5SJohn Baldwin 	}
6726f33eaa5SJohn Baldwin 	free(__DECONST(char *, bus->rman.rm_descr), M_DEVBUF);
6736f33eaa5SJohn Baldwin 
6746f33eaa5SJohn Baldwin 	error = bus_free_resource(dev, PCI_RES_BUS, bus->res);
6756f33eaa5SJohn Baldwin 	if (error)
6766f33eaa5SJohn Baldwin 		device_printf(dev,
6776f33eaa5SJohn Baldwin 		    "failed to release bus numbers resource: %d\n", error);
6786f33eaa5SJohn Baldwin }
6796f33eaa5SJohn Baldwin 
6804edef187SJohn Baldwin static struct resource *
6814edef187SJohn Baldwin pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid,
6822dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
6834edef187SJohn Baldwin {
6844edef187SJohn Baldwin 	struct resource *res;
6854edef187SJohn Baldwin 
6864edef187SJohn Baldwin 	res = rman_reserve_resource(&bus->rman, start, end, count, flags,
6874edef187SJohn Baldwin 	    child);
6884edef187SJohn Baldwin 	if (res == NULL)
6894edef187SJohn Baldwin 		return (NULL);
6904edef187SJohn Baldwin 
6914edef187SJohn Baldwin 	if (bootverbose)
6924edef187SJohn Baldwin 		device_printf(bus->dev,
693da1b038aSJustin Hibbits 		    "allocated bus range (%ju-%ju) for rid %d of %s\n",
6944edef187SJohn Baldwin 		    rman_get_start(res), rman_get_end(res), *rid,
6954edef187SJohn Baldwin 		    pcib_child_name(child));
6964edef187SJohn Baldwin 	rman_set_rid(res, *rid);
6974edef187SJohn Baldwin 	return (res);
6984edef187SJohn Baldwin }
6994edef187SJohn Baldwin 
7004edef187SJohn Baldwin /*
7014edef187SJohn Baldwin  * Attempt to grow the secondary bus range.  This is much simpler than
7024edef187SJohn Baldwin  * for I/O windows as the range can only be grown by increasing
7034edef187SJohn Baldwin  * subbus.
7044edef187SJohn Baldwin  */
7054edef187SJohn Baldwin static int
7062dd1bdf1SJustin Hibbits pcib_grow_subbus(struct pcib_secbus *bus, rman_res_t new_end)
7074edef187SJohn Baldwin {
7082dd1bdf1SJustin Hibbits 	rman_res_t old_end;
7094edef187SJohn Baldwin 	int error;
7104edef187SJohn Baldwin 
7114edef187SJohn Baldwin 	old_end = rman_get_end(bus->res);
7124edef187SJohn Baldwin 	KASSERT(new_end > old_end, ("attempt to shrink subbus"));
7134edef187SJohn Baldwin 	error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res,
7144edef187SJohn Baldwin 	    rman_get_start(bus->res), new_end);
7154edef187SJohn Baldwin 	if (error)
7164edef187SJohn Baldwin 		return (error);
7174edef187SJohn Baldwin 	if (bootverbose)
718da1b038aSJustin Hibbits 		device_printf(bus->dev, "grew bus range to %ju-%ju\n",
7194edef187SJohn Baldwin 		    rman_get_start(bus->res), rman_get_end(bus->res));
7204edef187SJohn Baldwin 	error = rman_manage_region(&bus->rman, old_end + 1,
7214edef187SJohn Baldwin 	    rman_get_end(bus->res));
7224edef187SJohn Baldwin 	if (error)
7234edef187SJohn Baldwin 		panic("Failed to add resource to rman");
7244edef187SJohn Baldwin 	bus->sub = rman_get_end(bus->res);
7254edef187SJohn Baldwin 	pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1);
7264edef187SJohn Baldwin 	return (0);
7274edef187SJohn Baldwin }
7284edef187SJohn Baldwin 
7294edef187SJohn Baldwin struct resource *
7304edef187SJohn Baldwin pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid,
7312dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
7324edef187SJohn Baldwin {
7334edef187SJohn Baldwin 	struct resource *res;
7342dd1bdf1SJustin Hibbits 	rman_res_t start_free, end_free, new_end;
7354edef187SJohn Baldwin 
7364edef187SJohn Baldwin 	/*
7374edef187SJohn Baldwin 	 * First, see if the request can be satisified by the existing
7384edef187SJohn Baldwin 	 * bus range.
7394edef187SJohn Baldwin 	 */
7404edef187SJohn Baldwin 	res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags);
7414edef187SJohn Baldwin 	if (res != NULL)
7424edef187SJohn Baldwin 		return (res);
7434edef187SJohn Baldwin 
7444edef187SJohn Baldwin 	/*
7454edef187SJohn Baldwin 	 * Figure out a range to grow the bus range.  First, find the
7464edef187SJohn Baldwin 	 * first bus number after the last allocated bus in the rman and
7474edef187SJohn Baldwin 	 * enforce that as a minimum starting point for the range.
7484edef187SJohn Baldwin 	 */
7494edef187SJohn Baldwin 	if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 ||
7504edef187SJohn Baldwin 	    end_free != bus->sub)
7514edef187SJohn Baldwin 		start_free = bus->sub + 1;
7524edef187SJohn Baldwin 	if (start_free < start)
7534edef187SJohn Baldwin 		start_free = start;
7544edef187SJohn Baldwin 	new_end = start_free + count - 1;
7554edef187SJohn Baldwin 
7564edef187SJohn Baldwin 	/*
7574edef187SJohn Baldwin 	 * See if this new range would satisfy the request if it
7584edef187SJohn Baldwin 	 * succeeds.
7594edef187SJohn Baldwin 	 */
7604edef187SJohn Baldwin 	if (new_end > end)
7614edef187SJohn Baldwin 		return (NULL);
7624edef187SJohn Baldwin 
7634edef187SJohn Baldwin 	/* Finally, attempt to grow the existing resource. */
7644edef187SJohn Baldwin 	if (bootverbose) {
7654edef187SJohn Baldwin 		device_printf(bus->dev,
766da1b038aSJustin Hibbits 		    "attempting to grow bus range for %ju buses\n", count);
767da1b038aSJustin Hibbits 		printf("\tback candidate range: %ju-%ju\n", start_free,
7684edef187SJohn Baldwin 		    new_end);
7694edef187SJohn Baldwin 	}
7704edef187SJohn Baldwin 	if (pcib_grow_subbus(bus, new_end) == 0)
7714edef187SJohn Baldwin 		return (pcib_suballoc_bus(bus, child, rid, start, end, count,
7724edef187SJohn Baldwin 		    flags));
7734edef187SJohn Baldwin 	return (NULL);
7744edef187SJohn Baldwin }
7754edef187SJohn Baldwin #endif
7764edef187SJohn Baldwin 
77783c41143SJohn Baldwin #else
77883c41143SJohn Baldwin 
779bb0d0a8eSMike Smith /*
780b0a2d4b8SWarner Losh  * Is the prefetch window open (eg, can we allocate memory in it?)
781b0a2d4b8SWarner Losh  */
782b0a2d4b8SWarner Losh static int
783b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc)
784b0a2d4b8SWarner Losh {
785b0a2d4b8SWarner Losh 	return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
786b0a2d4b8SWarner Losh }
787b0a2d4b8SWarner Losh 
788b0a2d4b8SWarner Losh /*
789b0a2d4b8SWarner Losh  * Is the nonprefetch window open (eg, can we allocate memory in it?)
790b0a2d4b8SWarner Losh  */
791b0a2d4b8SWarner Losh static int
792b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc)
793b0a2d4b8SWarner Losh {
794b0a2d4b8SWarner Losh 	return (sc->membase > 0 && sc->membase < sc->memlimit);
795b0a2d4b8SWarner Losh }
796b0a2d4b8SWarner Losh 
797b0a2d4b8SWarner Losh /*
798b0a2d4b8SWarner Losh  * Is the io window open (eg, can we allocate ports in it?)
799b0a2d4b8SWarner Losh  */
800b0a2d4b8SWarner Losh static int
801b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc)
802b0a2d4b8SWarner Losh {
803b0a2d4b8SWarner Losh 	return (sc->iobase > 0 && sc->iobase < sc->iolimit);
804b0a2d4b8SWarner Losh }
805b0a2d4b8SWarner Losh 
806b0a2d4b8SWarner Losh /*
807e36af292SJung-uk Kim  * Get current I/O decode.
808e36af292SJung-uk Kim  */
809e36af292SJung-uk Kim static void
810e36af292SJung-uk Kim pcib_get_io_decode(struct pcib_softc *sc)
811e36af292SJung-uk Kim {
812e36af292SJung-uk Kim 	device_t	dev;
813e36af292SJung-uk Kim 	uint32_t	iolow;
814e36af292SJung-uk Kim 
815e36af292SJung-uk Kim 	dev = sc->dev;
816e36af292SJung-uk Kim 
817e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
818e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
819e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(
820e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow);
821e36af292SJung-uk Kim 	else
822e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(0, iolow);
823e36af292SJung-uk Kim 
824e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
825e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
826e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(
827e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow);
828e36af292SJung-uk Kim 	else
829e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(0, iolow);
830e36af292SJung-uk Kim }
831e36af292SJung-uk Kim 
832e36af292SJung-uk Kim /*
833e36af292SJung-uk Kim  * Get current memory decode.
834e36af292SJung-uk Kim  */
835e36af292SJung-uk Kim static void
836e36af292SJung-uk Kim pcib_get_mem_decode(struct pcib_softc *sc)
837e36af292SJung-uk Kim {
838e36af292SJung-uk Kim 	device_t	dev;
839e36af292SJung-uk Kim 	pci_addr_t	pmemlow;
840e36af292SJung-uk Kim 
841e36af292SJung-uk Kim 	dev = sc->dev;
842e36af292SJung-uk Kim 
843e36af292SJung-uk Kim 	sc->membase = PCI_PPBMEMBASE(0,
844e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
845e36af292SJung-uk Kim 	sc->memlimit = PCI_PPBMEMLIMIT(0,
846e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
847e36af292SJung-uk Kim 
848e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2);
849e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
850e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(
851e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow);
852e36af292SJung-uk Kim 	else
853e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(0, pmemlow);
854e36af292SJung-uk Kim 
855e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2);
856e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
857e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(
858e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow);
859e36af292SJung-uk Kim 	else
860e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow);
861e36af292SJung-uk Kim }
862e36af292SJung-uk Kim 
863e36af292SJung-uk Kim /*
864e36af292SJung-uk Kim  * Restore previous I/O decode.
865e36af292SJung-uk Kim  */
866e36af292SJung-uk Kim static void
867e36af292SJung-uk Kim pcib_set_io_decode(struct pcib_softc *sc)
868e36af292SJung-uk Kim {
869e36af292SJung-uk Kim 	device_t	dev;
870e36af292SJung-uk Kim 	uint32_t	iohi;
871e36af292SJung-uk Kim 
872e36af292SJung-uk Kim 	dev = sc->dev;
873e36af292SJung-uk Kim 
874e36af292SJung-uk Kim 	iohi = sc->iobase >> 16;
875e36af292SJung-uk Kim 	if (iohi > 0)
876e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2);
877e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1);
878e36af292SJung-uk Kim 
879e36af292SJung-uk Kim 	iohi = sc->iolimit >> 16;
880e36af292SJung-uk Kim 	if (iohi > 0)
881e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2);
882e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1);
883e36af292SJung-uk Kim }
884e36af292SJung-uk Kim 
885e36af292SJung-uk Kim /*
886e36af292SJung-uk Kim  * Restore previous memory decode.
887e36af292SJung-uk Kim  */
888e36af292SJung-uk Kim static void
889e36af292SJung-uk Kim pcib_set_mem_decode(struct pcib_softc *sc)
890e36af292SJung-uk Kim {
891e36af292SJung-uk Kim 	device_t	dev;
892e36af292SJung-uk Kim 	pci_addr_t	pmemhi;
893e36af292SJung-uk Kim 
894e36af292SJung-uk Kim 	dev = sc->dev;
895e36af292SJung-uk Kim 
896e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2);
897e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2);
898e36af292SJung-uk Kim 
899e36af292SJung-uk Kim 	pmemhi = sc->pmembase >> 32;
900e36af292SJung-uk Kim 	if (pmemhi > 0)
901e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4);
902e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2);
903e36af292SJung-uk Kim 
904e36af292SJung-uk Kim 	pmemhi = sc->pmemlimit >> 32;
905e36af292SJung-uk Kim 	if (pmemhi > 0)
906e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4);
907e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2);
908e36af292SJung-uk Kim }
90983c41143SJohn Baldwin #endif
910e36af292SJung-uk Kim 
91182cb5c3bSJohn Baldwin #ifdef PCI_HP
91282cb5c3bSJohn Baldwin /*
91382cb5c3bSJohn Baldwin  * PCI-express HotPlug support.
91482cb5c3bSJohn Baldwin  */
91525a57bd6SJohn Baldwin static int pci_enable_pcie_hp = 1;
91625a57bd6SJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_hp, CTLFLAG_RDTUN,
91725a57bd6SJohn Baldwin     &pci_enable_pcie_hp, 0,
91825a57bd6SJohn Baldwin     "Enable support for native PCI-express HotPlug.");
91925a57bd6SJohn Baldwin 
92082cb5c3bSJohn Baldwin static void
92182cb5c3bSJohn Baldwin pcib_probe_hotplug(struct pcib_softc *sc)
92282cb5c3bSJohn Baldwin {
92382cb5c3bSJohn Baldwin 	device_t dev;
92437290148SEric van Gyzen 	uint32_t link_cap;
925991d431fSEric van Gyzen 	uint16_t link_sta, slot_sta;
92682cb5c3bSJohn Baldwin 
92725a57bd6SJohn Baldwin 	if (!pci_enable_pcie_hp)
92825a57bd6SJohn Baldwin 		return;
92925a57bd6SJohn Baldwin 
93082cb5c3bSJohn Baldwin 	dev = sc->dev;
93182cb5c3bSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, NULL) != 0)
93282cb5c3bSJohn Baldwin 		return;
93382cb5c3bSJohn Baldwin 
93482cb5c3bSJohn Baldwin 	if (!(pcie_read_config(dev, PCIER_FLAGS, 2) & PCIEM_FLAGS_SLOT))
93582cb5c3bSJohn Baldwin 		return;
93682cb5c3bSJohn Baldwin 
93782cb5c3bSJohn Baldwin 	sc->pcie_slot_cap = pcie_read_config(dev, PCIER_SLOT_CAP, 4);
93882cb5c3bSJohn Baldwin 
939991d431fSEric van Gyzen 	if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_HPC) == 0)
9402611037cSJohn Baldwin 		return;
94137290148SEric van Gyzen 	link_cap = pcie_read_config(dev, PCIER_LINK_CAP, 4);
94237290148SEric van Gyzen 	if ((link_cap & PCIEM_LINK_CAP_DL_ACTIVE) == 0)
9432ffb582aSJohn Baldwin 		return;
9442611037cSJohn Baldwin 
945991d431fSEric van Gyzen 	/*
946991d431fSEric van Gyzen 	 * Some devices report that they have an MRL when they actually
947991d431fSEric van Gyzen 	 * do not.  Since they always report that the MRL is open, child
948991d431fSEric van Gyzen 	 * devices would be ignored.  Try to detect these devices and
949991d431fSEric van Gyzen 	 * ignore their claim of HotPlug support.
950991d431fSEric van Gyzen 	 *
951991d431fSEric van Gyzen 	 * If there is an open MRL but the Data Link Layer is active,
952991d431fSEric van Gyzen 	 * the MRL is not real.
953991d431fSEric van Gyzen 	 */
95437290148SEric van Gyzen 	if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) != 0) {
955991d431fSEric van Gyzen 		link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
956991d431fSEric van Gyzen 		slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
957991d431fSEric van Gyzen 		if ((slot_sta & PCIEM_SLOT_STA_MRLSS) != 0 &&
958991d431fSEric van Gyzen 		    (link_sta & PCIEM_LINK_STA_DL_ACTIVE) != 0) {
959991d431fSEric van Gyzen 			return;
960991d431fSEric van Gyzen 		}
961991d431fSEric van Gyzen 	}
962991d431fSEric van Gyzen 
96382cb5c3bSJohn Baldwin 	sc->flags |= PCIB_HOTPLUG;
96482cb5c3bSJohn Baldwin }
96582cb5c3bSJohn Baldwin 
96682cb5c3bSJohn Baldwin /*
96782cb5c3bSJohn Baldwin  * Send a HotPlug command to the slot control register.  If this slot
96807454911SJohn Baldwin  * uses command completion interrupts and a previous command is still
96907454911SJohn Baldwin  * in progress, then the command is dropped.  Once the previous
97007454911SJohn Baldwin  * command completes or times out, pcib_pcie_hotplug_update() will be
97107454911SJohn Baldwin  * invoked to post a new command based on the slot's state at that
97207454911SJohn Baldwin  * time.
97382cb5c3bSJohn Baldwin  */
97482cb5c3bSJohn Baldwin static void
97582cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(struct pcib_softc *sc, uint16_t val, uint16_t mask)
97682cb5c3bSJohn Baldwin {
97782cb5c3bSJohn Baldwin 	device_t dev;
97882cb5c3bSJohn Baldwin 	uint16_t ctl, new;
97982cb5c3bSJohn Baldwin 
98082cb5c3bSJohn Baldwin 	dev = sc->dev;
98182cb5c3bSJohn Baldwin 
98207454911SJohn Baldwin 	if (sc->flags & PCIB_HOTPLUG_CMD_PENDING)
98307454911SJohn Baldwin 		return;
98407454911SJohn Baldwin 
98582cb5c3bSJohn Baldwin 	ctl = pcie_read_config(dev, PCIER_SLOT_CTL, 2);
98682cb5c3bSJohn Baldwin 	new = (ctl & ~mask) | val;
98707454911SJohn Baldwin 	if (new == ctl)
98807454911SJohn Baldwin 		return;
989991d431fSEric van Gyzen 	if (bootverbose)
990991d431fSEric van Gyzen 		device_printf(dev, "HotPlug command: %04x -> %04x\n", ctl, new);
99107454911SJohn Baldwin 	pcie_write_config(dev, PCIER_SLOT_CTL, new, 2);
9926f33eaa5SJohn Baldwin 	if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS) &&
9936f33eaa5SJohn Baldwin 	    (ctl & new) & PCIEM_SLOT_CTL_CCIE) {
99482cb5c3bSJohn Baldwin 		sc->flags |= PCIB_HOTPLUG_CMD_PENDING;
99582cb5c3bSJohn Baldwin 		if (!cold)
99682cb5c3bSJohn Baldwin 			callout_reset(&sc->pcie_cc_timer, hz,
99782cb5c3bSJohn Baldwin 			    pcib_pcie_cc_timeout, sc);
99882cb5c3bSJohn Baldwin 	}
99982cb5c3bSJohn Baldwin }
100082cb5c3bSJohn Baldwin 
100182cb5c3bSJohn Baldwin static void
100282cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(struct pcib_softc *sc)
100382cb5c3bSJohn Baldwin {
100482cb5c3bSJohn Baldwin 	device_t dev;
100582cb5c3bSJohn Baldwin 
100682cb5c3bSJohn Baldwin 	dev = sc->dev;
100782cb5c3bSJohn Baldwin 
100882cb5c3bSJohn Baldwin 	if (bootverbose)
100982cb5c3bSJohn Baldwin 		device_printf(dev, "Command Completed\n");
101082cb5c3bSJohn Baldwin 	if (!(sc->flags & PCIB_HOTPLUG_CMD_PENDING))
101182cb5c3bSJohn Baldwin 		return;
101282cb5c3bSJohn Baldwin 	callout_stop(&sc->pcie_cc_timer);
101382cb5c3bSJohn Baldwin 	sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING;
10146f33eaa5SJohn Baldwin 	wakeup(sc);
101582cb5c3bSJohn Baldwin }
101682cb5c3bSJohn Baldwin 
101782cb5c3bSJohn Baldwin /*
101882cb5c3bSJohn Baldwin  * Returns true if a card is fully inserted from the user's
101982cb5c3bSJohn Baldwin  * perspective.  It may not yet be ready for access, but the driver
102082cb5c3bSJohn Baldwin  * can now start enabling access if necessary.
102182cb5c3bSJohn Baldwin  */
102282cb5c3bSJohn Baldwin static bool
102382cb5c3bSJohn Baldwin pcib_hotplug_inserted(struct pcib_softc *sc)
102482cb5c3bSJohn Baldwin {
102582cb5c3bSJohn Baldwin 
102682cb5c3bSJohn Baldwin 	/* Pretend the card isn't present if a detach is forced. */
102782cb5c3bSJohn Baldwin 	if (sc->flags & PCIB_DETACHING)
102882cb5c3bSJohn Baldwin 		return (false);
102982cb5c3bSJohn Baldwin 
103082cb5c3bSJohn Baldwin 	/* Card must be present in the slot. */
103182cb5c3bSJohn Baldwin 	if ((sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS) == 0)
103282cb5c3bSJohn Baldwin 		return (false);
103382cb5c3bSJohn Baldwin 
103482cb5c3bSJohn Baldwin 	/* A power fault implicitly turns off power to the slot. */
103582cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD)
103682cb5c3bSJohn Baldwin 		return (false);
103782cb5c3bSJohn Baldwin 
103882cb5c3bSJohn Baldwin 	/* If the MRL is disengaged, the slot is powered off. */
103982cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP &&
104082cb5c3bSJohn Baldwin 	    (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS) != 0)
104182cb5c3bSJohn Baldwin 		return (false);
104282cb5c3bSJohn Baldwin 
104382cb5c3bSJohn Baldwin 	return (true);
104482cb5c3bSJohn Baldwin }
104582cb5c3bSJohn Baldwin 
104682cb5c3bSJohn Baldwin /*
104782cb5c3bSJohn Baldwin  * Returns -1 if the card is fully inserted, powered, and ready for
104882cb5c3bSJohn Baldwin  * access.  Otherwise, returns 0.
104982cb5c3bSJohn Baldwin  */
105082cb5c3bSJohn Baldwin static int
105182cb5c3bSJohn Baldwin pcib_hotplug_present(struct pcib_softc *sc)
105282cb5c3bSJohn Baldwin {
105382cb5c3bSJohn Baldwin 
105482cb5c3bSJohn Baldwin 	/* Card must be inserted. */
105582cb5c3bSJohn Baldwin 	if (!pcib_hotplug_inserted(sc))
105682cb5c3bSJohn Baldwin 		return (0);
105782cb5c3bSJohn Baldwin 
105882cb5c3bSJohn Baldwin 	/*
105982cb5c3bSJohn Baldwin 	 * Require the Electromechanical Interlock to be engaged if
106082cb5c3bSJohn Baldwin 	 * present.
106182cb5c3bSJohn Baldwin 	 */
106282cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP &&
106382cb5c3bSJohn Baldwin 	    (sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS) == 0)
106482cb5c3bSJohn Baldwin 		return (0);
106582cb5c3bSJohn Baldwin 
106682cb5c3bSJohn Baldwin 	/* Require the Data Link Layer to be active. */
106782cb5c3bSJohn Baldwin 	if (!(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE))
106882cb5c3bSJohn Baldwin 		return (0);
106982cb5c3bSJohn Baldwin 
107082cb5c3bSJohn Baldwin 	return (-1);
107182cb5c3bSJohn Baldwin }
107282cb5c3bSJohn Baldwin 
107382cb5c3bSJohn Baldwin static void
107482cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(struct pcib_softc *sc, uint16_t val, uint16_t mask,
107582cb5c3bSJohn Baldwin     bool schedule_task)
107682cb5c3bSJohn Baldwin {
1077a1566487SEric van Gyzen 	bool card_inserted, ei_engaged;
107882cb5c3bSJohn Baldwin 
1079991d431fSEric van Gyzen 	/* Clear DETACHING if Presence Detect has cleared. */
108082cb5c3bSJohn Baldwin 	if ((sc->pcie_slot_sta & (PCIEM_SLOT_STA_PDC | PCIEM_SLOT_STA_PDS)) ==
108182cb5c3bSJohn Baldwin 	    PCIEM_SLOT_STA_PDC)
108282cb5c3bSJohn Baldwin 		sc->flags &= ~PCIB_DETACHING;
108382cb5c3bSJohn Baldwin 
108482cb5c3bSJohn Baldwin 	card_inserted = pcib_hotplug_inserted(sc);
108582cb5c3bSJohn Baldwin 
108682cb5c3bSJohn Baldwin 	/* Turn the power indicator on if a card is inserted. */
108782cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PIP) {
108882cb5c3bSJohn Baldwin 		mask |= PCIEM_SLOT_CTL_PIC;
108982cb5c3bSJohn Baldwin 		if (card_inserted)
109082cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PI_ON;
109182cb5c3bSJohn Baldwin 		else if (sc->flags & PCIB_DETACH_PENDING)
109282cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PI_BLINK;
109382cb5c3bSJohn Baldwin 		else
109482cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PI_OFF;
109582cb5c3bSJohn Baldwin 	}
109682cb5c3bSJohn Baldwin 
109782cb5c3bSJohn Baldwin 	/* Turn the power on via the Power Controller if a card is inserted. */
109882cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) {
109982cb5c3bSJohn Baldwin 		mask |= PCIEM_SLOT_CTL_PCC;
110082cb5c3bSJohn Baldwin 		if (card_inserted)
110182cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PC_ON;
110282cb5c3bSJohn Baldwin 		else
110382cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PC_OFF;
110482cb5c3bSJohn Baldwin 	}
110582cb5c3bSJohn Baldwin 
110682cb5c3bSJohn Baldwin 	/*
110782cb5c3bSJohn Baldwin 	 * If a card is inserted, enable the Electromechanical
110882cb5c3bSJohn Baldwin 	 * Interlock.  If a card is not inserted (or we are in the
110982cb5c3bSJohn Baldwin 	 * process of detaching), disable the Electromechanical
111082cb5c3bSJohn Baldwin 	 * Interlock.
111182cb5c3bSJohn Baldwin 	 */
111282cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP) {
111382cb5c3bSJohn Baldwin 		mask |= PCIEM_SLOT_CTL_EIC;
1114a1566487SEric van Gyzen 		ei_engaged = (sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS) != 0;
1115a1566487SEric van Gyzen 		if (card_inserted != ei_engaged)
111682cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_EIC;
111782cb5c3bSJohn Baldwin 	}
111882cb5c3bSJohn Baldwin 
111982cb5c3bSJohn Baldwin 	/*
112082cb5c3bSJohn Baldwin 	 * Start a timer to see if the Data Link Layer times out.
1121991d431fSEric van Gyzen 	 * Note that we only start the timer if Presence Detect or MRL Sensor
112282cb5c3bSJohn Baldwin 	 * changed on this interrupt.  Stop any scheduled timer if
112382cb5c3bSJohn Baldwin 	 * the Data Link Layer is active.
112482cb5c3bSJohn Baldwin 	 */
112582cb5c3bSJohn Baldwin 	if (card_inserted &&
112682cb5c3bSJohn Baldwin 	    !(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) &&
1127991d431fSEric van Gyzen 	    sc->pcie_slot_sta &
1128991d431fSEric van Gyzen 	    (PCIEM_SLOT_STA_MRLSC | PCIEM_SLOT_STA_PDC)) {
112982cb5c3bSJohn Baldwin 		if (cold)
113082cb5c3bSJohn Baldwin 			device_printf(sc->dev,
113182cb5c3bSJohn Baldwin 			    "Data Link Layer inactive\n");
113282cb5c3bSJohn Baldwin 		else
113382cb5c3bSJohn Baldwin 			callout_reset(&sc->pcie_dll_timer, hz,
113482cb5c3bSJohn Baldwin 			    pcib_pcie_dll_timeout, sc);
113582cb5c3bSJohn Baldwin 	} else if (sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE)
113682cb5c3bSJohn Baldwin 		callout_stop(&sc->pcie_dll_timer);
113782cb5c3bSJohn Baldwin 
113882cb5c3bSJohn Baldwin 	pcib_pcie_hotplug_command(sc, val, mask);
113982cb5c3bSJohn Baldwin 
114082cb5c3bSJohn Baldwin 	/*
1141a1566487SEric van Gyzen 	 * During attach the child "pci" device is added synchronously;
114282cb5c3bSJohn Baldwin 	 * otherwise, the task is scheduled to manage the child
114382cb5c3bSJohn Baldwin 	 * device.
114482cb5c3bSJohn Baldwin 	 */
114582cb5c3bSJohn Baldwin 	if (schedule_task &&
114682cb5c3bSJohn Baldwin 	    (pcib_hotplug_present(sc) != 0) != (sc->child != NULL))
114782cb5c3bSJohn Baldwin 		taskqueue_enqueue(taskqueue_thread, &sc->pcie_hp_task);
114882cb5c3bSJohn Baldwin }
114982cb5c3bSJohn Baldwin 
115082cb5c3bSJohn Baldwin static void
115182cb5c3bSJohn Baldwin pcib_pcie_intr(void *arg)
115282cb5c3bSJohn Baldwin {
115382cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
115482cb5c3bSJohn Baldwin 	device_t dev;
115582cb5c3bSJohn Baldwin 
115682cb5c3bSJohn Baldwin 	sc = arg;
115782cb5c3bSJohn Baldwin 	dev = sc->dev;
115882cb5c3bSJohn Baldwin 	sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
115982cb5c3bSJohn Baldwin 
116082cb5c3bSJohn Baldwin 	/* Clear the events just reported. */
116182cb5c3bSJohn Baldwin 	pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2);
116282cb5c3bSJohn Baldwin 
1163991d431fSEric van Gyzen 	if (bootverbose)
1164991d431fSEric van Gyzen 		device_printf(dev, "HotPlug interrupt: %#x\n",
1165991d431fSEric van Gyzen 		    sc->pcie_slot_sta);
1166991d431fSEric van Gyzen 
116782cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_ABP) {
116882cb5c3bSJohn Baldwin 		if (sc->flags & PCIB_DETACH_PENDING) {
116982cb5c3bSJohn Baldwin 			device_printf(dev,
117082cb5c3bSJohn Baldwin 			    "Attention Button Pressed: Detach Cancelled\n");
117182cb5c3bSJohn Baldwin 			sc->flags &= ~PCIB_DETACH_PENDING;
117282cb5c3bSJohn Baldwin 			callout_stop(&sc->pcie_ab_timer);
117382cb5c3bSJohn Baldwin 		} else {
117482cb5c3bSJohn Baldwin 			device_printf(dev,
117582cb5c3bSJohn Baldwin 		    "Attention Button Pressed: Detaching in 5 seconds\n");
117682cb5c3bSJohn Baldwin 			sc->flags |= PCIB_DETACH_PENDING;
117782cb5c3bSJohn Baldwin 			callout_reset(&sc->pcie_ab_timer, 5 * hz,
117882cb5c3bSJohn Baldwin 			    pcib_pcie_ab_timeout, sc);
117982cb5c3bSJohn Baldwin 		}
118082cb5c3bSJohn Baldwin 	}
118182cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD)
118282cb5c3bSJohn Baldwin 		device_printf(dev, "Power Fault Detected\n");
118382cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSC)
118482cb5c3bSJohn Baldwin 		device_printf(dev, "MRL Sensor Changed to %s\n",
118582cb5c3bSJohn Baldwin 		    sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS ? "open" :
118682cb5c3bSJohn Baldwin 		    "closed");
118782cb5c3bSJohn Baldwin 	if (bootverbose && sc->pcie_slot_sta & PCIEM_SLOT_STA_PDC)
1188991d431fSEric van Gyzen 		device_printf(dev, "Presence Detect Changed to %s\n",
118982cb5c3bSJohn Baldwin 		    sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS ? "card present" :
119082cb5c3bSJohn Baldwin 		    "empty");
119182cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_CC)
119282cb5c3bSJohn Baldwin 		pcib_pcie_hotplug_command_completed(sc);
119382cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_DLLSC) {
119482cb5c3bSJohn Baldwin 		sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
119582cb5c3bSJohn Baldwin 		if (bootverbose)
119682cb5c3bSJohn Baldwin 			device_printf(dev,
119782cb5c3bSJohn Baldwin 			    "Data Link Layer State Changed to %s\n",
119882cb5c3bSJohn Baldwin 			    sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE ?
119982cb5c3bSJohn Baldwin 			    "active" : "inactive");
120082cb5c3bSJohn Baldwin 	}
120182cb5c3bSJohn Baldwin 
120282cb5c3bSJohn Baldwin 	pcib_pcie_hotplug_update(sc, 0, 0, true);
120382cb5c3bSJohn Baldwin }
120482cb5c3bSJohn Baldwin 
120582cb5c3bSJohn Baldwin static void
120682cb5c3bSJohn Baldwin pcib_pcie_hotplug_task(void *context, int pending)
120782cb5c3bSJohn Baldwin {
120882cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
120982cb5c3bSJohn Baldwin 	device_t dev;
121082cb5c3bSJohn Baldwin 
121182cb5c3bSJohn Baldwin 	sc = context;
121282cb5c3bSJohn Baldwin 	mtx_lock(&Giant);
121382cb5c3bSJohn Baldwin 	dev = sc->dev;
121482cb5c3bSJohn Baldwin 	if (pcib_hotplug_present(sc) != 0) {
121582cb5c3bSJohn Baldwin 		if (sc->child == NULL) {
121682cb5c3bSJohn Baldwin 			sc->child = device_add_child(dev, "pci", -1);
121782cb5c3bSJohn Baldwin 			bus_generic_attach(dev);
121882cb5c3bSJohn Baldwin 		}
121982cb5c3bSJohn Baldwin 	} else {
122082cb5c3bSJohn Baldwin 		if (sc->child != NULL) {
122182cb5c3bSJohn Baldwin 			if (device_delete_child(dev, sc->child) == 0)
122282cb5c3bSJohn Baldwin 				sc->child = NULL;
122382cb5c3bSJohn Baldwin 		}
122482cb5c3bSJohn Baldwin 	}
122582cb5c3bSJohn Baldwin 	mtx_unlock(&Giant);
122682cb5c3bSJohn Baldwin }
122782cb5c3bSJohn Baldwin 
122882cb5c3bSJohn Baldwin static void
122982cb5c3bSJohn Baldwin pcib_pcie_ab_timeout(void *arg)
123082cb5c3bSJohn Baldwin {
123182cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
123282cb5c3bSJohn Baldwin 	device_t dev;
123382cb5c3bSJohn Baldwin 
123482cb5c3bSJohn Baldwin 	sc = arg;
123582cb5c3bSJohn Baldwin 	dev = sc->dev;
123682cb5c3bSJohn Baldwin 	mtx_assert(&Giant, MA_OWNED);
123782cb5c3bSJohn Baldwin 	if (sc->flags & PCIB_DETACH_PENDING) {
123882cb5c3bSJohn Baldwin 		sc->flags |= PCIB_DETACHING;
123982cb5c3bSJohn Baldwin 		sc->flags &= ~PCIB_DETACH_PENDING;
124082cb5c3bSJohn Baldwin 		pcib_pcie_hotplug_update(sc, 0, 0, true);
124182cb5c3bSJohn Baldwin 	}
124282cb5c3bSJohn Baldwin }
124382cb5c3bSJohn Baldwin 
124482cb5c3bSJohn Baldwin static void
124582cb5c3bSJohn Baldwin pcib_pcie_cc_timeout(void *arg)
124682cb5c3bSJohn Baldwin {
124782cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
124882cb5c3bSJohn Baldwin 	device_t dev;
12496f33eaa5SJohn Baldwin 	uint16_t sta;
125082cb5c3bSJohn Baldwin 
125182cb5c3bSJohn Baldwin 	sc = arg;
125282cb5c3bSJohn Baldwin 	dev = sc->dev;
125382cb5c3bSJohn Baldwin 	mtx_assert(&Giant, MA_OWNED);
12546f33eaa5SJohn Baldwin 	sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
12556f33eaa5SJohn Baldwin 	if (!(sta & PCIEM_SLOT_STA_CC)) {
125682cb5c3bSJohn Baldwin 		device_printf(dev,
1257991d431fSEric van Gyzen 		    "HotPlug Command Timed Out - forcing detach\n");
125882cb5c3bSJohn Baldwin 		sc->flags &= ~(PCIB_HOTPLUG_CMD_PENDING | PCIB_DETACH_PENDING);
125982cb5c3bSJohn Baldwin 		sc->flags |= PCIB_DETACHING;
126082cb5c3bSJohn Baldwin 		pcib_pcie_hotplug_update(sc, 0, 0, true);
12616f33eaa5SJohn Baldwin 	} else {
12626f33eaa5SJohn Baldwin 		device_printf(dev,
12636f33eaa5SJohn Baldwin 	    "Missed HotPlug interrupt waiting for Command Completion\n");
12646f33eaa5SJohn Baldwin 		pcib_pcie_intr(sc);
126582cb5c3bSJohn Baldwin 	}
126682cb5c3bSJohn Baldwin }
126782cb5c3bSJohn Baldwin 
126882cb5c3bSJohn Baldwin static void
126982cb5c3bSJohn Baldwin pcib_pcie_dll_timeout(void *arg)
127082cb5c3bSJohn Baldwin {
127182cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
127282cb5c3bSJohn Baldwin 	device_t dev;
127382cb5c3bSJohn Baldwin 	uint16_t sta;
127482cb5c3bSJohn Baldwin 
127582cb5c3bSJohn Baldwin 	sc = arg;
127682cb5c3bSJohn Baldwin 	dev = sc->dev;
127782cb5c3bSJohn Baldwin 	mtx_assert(&Giant, MA_OWNED);
127882cb5c3bSJohn Baldwin 	sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
127982cb5c3bSJohn Baldwin 	if (!(sta & PCIEM_LINK_STA_DL_ACTIVE)) {
128082cb5c3bSJohn Baldwin 		device_printf(dev,
128182cb5c3bSJohn Baldwin 		    "Timed out waiting for Data Link Layer Active\n");
128282cb5c3bSJohn Baldwin 		sc->flags |= PCIB_DETACHING;
128382cb5c3bSJohn Baldwin 		pcib_pcie_hotplug_update(sc, 0, 0, true);
128482cb5c3bSJohn Baldwin 	} else if (sta != sc->pcie_link_sta) {
128582cb5c3bSJohn Baldwin 		device_printf(dev,
128682cb5c3bSJohn Baldwin 		    "Missed HotPlug interrupt waiting for DLL Active\n");
128782cb5c3bSJohn Baldwin 		pcib_pcie_intr(sc);
128882cb5c3bSJohn Baldwin 	}
128982cb5c3bSJohn Baldwin }
129082cb5c3bSJohn Baldwin 
129182cb5c3bSJohn Baldwin static int
129282cb5c3bSJohn Baldwin pcib_alloc_pcie_irq(struct pcib_softc *sc)
129382cb5c3bSJohn Baldwin {
129482cb5c3bSJohn Baldwin 	device_t dev;
129582cb5c3bSJohn Baldwin 	int count, error, rid;
129682cb5c3bSJohn Baldwin 
129782cb5c3bSJohn Baldwin 	rid = -1;
129882cb5c3bSJohn Baldwin 	dev = sc->dev;
129982cb5c3bSJohn Baldwin 
130082cb5c3bSJohn Baldwin 	/*
130182cb5c3bSJohn Baldwin 	 * For simplicity, only use MSI-X if there is a single message.
130282cb5c3bSJohn Baldwin 	 * To support a device with multiple messages we would have to
130382cb5c3bSJohn Baldwin 	 * use remap intr if the MSI number is not 0.
130482cb5c3bSJohn Baldwin 	 */
130582cb5c3bSJohn Baldwin 	count = pci_msix_count(dev);
130682cb5c3bSJohn Baldwin 	if (count == 1) {
130782cb5c3bSJohn Baldwin 		error = pci_alloc_msix(dev, &count);
130882cb5c3bSJohn Baldwin 		if (error == 0)
130982cb5c3bSJohn Baldwin 			rid = 1;
131082cb5c3bSJohn Baldwin 	}
131182cb5c3bSJohn Baldwin 
131282cb5c3bSJohn Baldwin 	if (rid < 0 && pci_msi_count(dev) > 0) {
131382cb5c3bSJohn Baldwin 		count = 1;
131482cb5c3bSJohn Baldwin 		error = pci_alloc_msi(dev, &count);
131582cb5c3bSJohn Baldwin 		if (error == 0)
131682cb5c3bSJohn Baldwin 			rid = 1;
131782cb5c3bSJohn Baldwin 	}
131882cb5c3bSJohn Baldwin 
131982cb5c3bSJohn Baldwin 	if (rid < 0)
132082cb5c3bSJohn Baldwin 		rid = 0;
132182cb5c3bSJohn Baldwin 
132282cb5c3bSJohn Baldwin 	sc->pcie_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
132382cb5c3bSJohn Baldwin 	    RF_ACTIVE);
132482cb5c3bSJohn Baldwin 	if (sc->pcie_irq == NULL) {
132582cb5c3bSJohn Baldwin 		device_printf(dev,
132682cb5c3bSJohn Baldwin 		    "Failed to allocate interrupt for PCI-e events\n");
132782cb5c3bSJohn Baldwin 		if (rid > 0)
132882cb5c3bSJohn Baldwin 			pci_release_msi(dev);
132982cb5c3bSJohn Baldwin 		return (ENXIO);
133082cb5c3bSJohn Baldwin 	}
133182cb5c3bSJohn Baldwin 
133282cb5c3bSJohn Baldwin 	error = bus_setup_intr(dev, sc->pcie_irq, INTR_TYPE_MISC,
133382cb5c3bSJohn Baldwin 	    NULL, pcib_pcie_intr, sc, &sc->pcie_ihand);
133482cb5c3bSJohn Baldwin 	if (error) {
133582cb5c3bSJohn Baldwin 		device_printf(dev, "Failed to setup PCI-e interrupt handler\n");
133682cb5c3bSJohn Baldwin 		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->pcie_irq);
133782cb5c3bSJohn Baldwin 		if (rid > 0)
133882cb5c3bSJohn Baldwin 			pci_release_msi(dev);
133982cb5c3bSJohn Baldwin 		return (error);
134082cb5c3bSJohn Baldwin 	}
134182cb5c3bSJohn Baldwin 	return (0);
134282cb5c3bSJohn Baldwin }
134382cb5c3bSJohn Baldwin 
13446f33eaa5SJohn Baldwin static int
13456f33eaa5SJohn Baldwin pcib_release_pcie_irq(struct pcib_softc *sc)
13466f33eaa5SJohn Baldwin {
13476f33eaa5SJohn Baldwin 	device_t dev;
13486f33eaa5SJohn Baldwin 	int error;
13496f33eaa5SJohn Baldwin 
13506f33eaa5SJohn Baldwin 	dev = sc->dev;
13516f33eaa5SJohn Baldwin 	error = bus_teardown_intr(dev, sc->pcie_irq, sc->pcie_ihand);
13526f33eaa5SJohn Baldwin 	if (error)
13536f33eaa5SJohn Baldwin 		return (error);
13546f33eaa5SJohn Baldwin 	error = bus_free_resource(dev, SYS_RES_IRQ, sc->pcie_irq);
13556f33eaa5SJohn Baldwin 	if (error)
13566f33eaa5SJohn Baldwin 		return (error);
13576f33eaa5SJohn Baldwin 	return (pci_release_msi(dev));
13586f33eaa5SJohn Baldwin }
13596f33eaa5SJohn Baldwin 
136082cb5c3bSJohn Baldwin static void
136182cb5c3bSJohn Baldwin pcib_setup_hotplug(struct pcib_softc *sc)
136282cb5c3bSJohn Baldwin {
136382cb5c3bSJohn Baldwin 	device_t dev;
136482cb5c3bSJohn Baldwin 	uint16_t mask, val;
136582cb5c3bSJohn Baldwin 
136682cb5c3bSJohn Baldwin 	dev = sc->dev;
136782cb5c3bSJohn Baldwin 	callout_init(&sc->pcie_ab_timer, 0);
136882cb5c3bSJohn Baldwin 	callout_init(&sc->pcie_cc_timer, 0);
136982cb5c3bSJohn Baldwin 	callout_init(&sc->pcie_dll_timer, 0);
137082cb5c3bSJohn Baldwin 	TASK_INIT(&sc->pcie_hp_task, 0, pcib_pcie_hotplug_task, sc);
137182cb5c3bSJohn Baldwin 
137282cb5c3bSJohn Baldwin 	/* Allocate IRQ. */
137382cb5c3bSJohn Baldwin 	if (pcib_alloc_pcie_irq(sc) != 0)
137482cb5c3bSJohn Baldwin 		return;
137582cb5c3bSJohn Baldwin 
137682cb5c3bSJohn Baldwin 	sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
137782cb5c3bSJohn Baldwin 	sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
137882cb5c3bSJohn Baldwin 
13796f33eaa5SJohn Baldwin 	/* Clear any events previously pending. */
13806f33eaa5SJohn Baldwin 	pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2);
13816f33eaa5SJohn Baldwin 
138282cb5c3bSJohn Baldwin 	/* Enable HotPlug events. */
138382cb5c3bSJohn Baldwin 	mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE |
138482cb5c3bSJohn Baldwin 	    PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE |
138582cb5c3bSJohn Baldwin 	    PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE;
138637290148SEric van Gyzen 	val = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | PCIEM_SLOT_CTL_PDCE;
138782cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_APB)
138882cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_ABPE;
138982cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP)
139082cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_PFDE;
139182cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP)
139282cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_MRLSCE;
139382cb5c3bSJohn Baldwin 	if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS))
139482cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_CCIE;
139582cb5c3bSJohn Baldwin 
139682cb5c3bSJohn Baldwin 	/* Turn the attention indicator off. */
139782cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) {
139882cb5c3bSJohn Baldwin 		mask |= PCIEM_SLOT_CTL_AIC;
139982cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_AI_OFF;
140082cb5c3bSJohn Baldwin 	}
140182cb5c3bSJohn Baldwin 
140282cb5c3bSJohn Baldwin 	pcib_pcie_hotplug_update(sc, val, mask, false);
140382cb5c3bSJohn Baldwin }
14046f33eaa5SJohn Baldwin 
14056f33eaa5SJohn Baldwin static int
14066f33eaa5SJohn Baldwin pcib_detach_hotplug(struct pcib_softc *sc)
14076f33eaa5SJohn Baldwin {
14086f33eaa5SJohn Baldwin 	uint16_t mask, val;
14096f33eaa5SJohn Baldwin 	int error;
14106f33eaa5SJohn Baldwin 
14116f33eaa5SJohn Baldwin 	/* Disable the card in the slot and force it to detach. */
14126f33eaa5SJohn Baldwin 	if (sc->flags & PCIB_DETACH_PENDING) {
14136f33eaa5SJohn Baldwin 		sc->flags &= ~PCIB_DETACH_PENDING;
14146f33eaa5SJohn Baldwin 		callout_stop(&sc->pcie_ab_timer);
14156f33eaa5SJohn Baldwin 	}
14166f33eaa5SJohn Baldwin 	sc->flags |= PCIB_DETACHING;
14176f33eaa5SJohn Baldwin 
14186f33eaa5SJohn Baldwin 	if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) {
14196f33eaa5SJohn Baldwin 		callout_stop(&sc->pcie_cc_timer);
14206f33eaa5SJohn Baldwin 		tsleep(sc, 0, "hpcmd", hz);
14216f33eaa5SJohn Baldwin 		sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING;
14226f33eaa5SJohn Baldwin 	}
14236f33eaa5SJohn Baldwin 
14246f33eaa5SJohn Baldwin 	/* Disable HotPlug events. */
14256f33eaa5SJohn Baldwin 	mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE |
14266f33eaa5SJohn Baldwin 	    PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE |
14276f33eaa5SJohn Baldwin 	    PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE;
14286f33eaa5SJohn Baldwin 	val = 0;
14296f33eaa5SJohn Baldwin 
14306f33eaa5SJohn Baldwin 	/* Turn the attention indicator off. */
14316f33eaa5SJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) {
14326f33eaa5SJohn Baldwin 		mask |= PCIEM_SLOT_CTL_AIC;
14336f33eaa5SJohn Baldwin 		val |= PCIEM_SLOT_CTL_AI_OFF;
14346f33eaa5SJohn Baldwin 	}
14356f33eaa5SJohn Baldwin 
14366f33eaa5SJohn Baldwin 	pcib_pcie_hotplug_update(sc, val, mask, false);
14376f33eaa5SJohn Baldwin 
14386f33eaa5SJohn Baldwin 	error = pcib_release_pcie_irq(sc);
14396f33eaa5SJohn Baldwin 	if (error)
14406f33eaa5SJohn Baldwin 		return (error);
14416f33eaa5SJohn Baldwin 	taskqueue_drain(taskqueue_thread, &sc->pcie_hp_task);
14426f33eaa5SJohn Baldwin 	callout_drain(&sc->pcie_ab_timer);
14436f33eaa5SJohn Baldwin 	callout_drain(&sc->pcie_cc_timer);
14446f33eaa5SJohn Baldwin 	callout_drain(&sc->pcie_dll_timer);
14456f33eaa5SJohn Baldwin 	return (0);
14466f33eaa5SJohn Baldwin }
144782cb5c3bSJohn Baldwin #endif
144882cb5c3bSJohn Baldwin 
1449e36af292SJung-uk Kim /*
1450e36af292SJung-uk Kim  * Get current bridge configuration.
1451e36af292SJung-uk Kim  */
1452e36af292SJung-uk Kim static void
1453e36af292SJung-uk Kim pcib_cfg_save(struct pcib_softc *sc)
1454e36af292SJung-uk Kim {
1455ad6f36f8SJohn Baldwin #ifndef NEW_PCIB
1456e36af292SJung-uk Kim 	device_t	dev;
1457ad6f36f8SJohn Baldwin 	uint16_t command;
1458e36af292SJung-uk Kim 
1459e36af292SJung-uk Kim 	dev = sc->dev;
1460e36af292SJung-uk Kim 
1461ad6f36f8SJohn Baldwin 	command = pci_read_config(dev, PCIR_COMMAND, 2);
1462ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_PORTEN)
1463e36af292SJung-uk Kim 		pcib_get_io_decode(sc);
1464ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_MEMEN)
1465e36af292SJung-uk Kim 		pcib_get_mem_decode(sc);
146683c41143SJohn Baldwin #endif
1467e36af292SJung-uk Kim }
1468e36af292SJung-uk Kim 
1469e36af292SJung-uk Kim /*
1470e36af292SJung-uk Kim  * Restore previous bridge configuration.
1471e36af292SJung-uk Kim  */
1472e36af292SJung-uk Kim static void
1473e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc)
1474e36af292SJung-uk Kim {
1475e36af292SJung-uk Kim 	device_t	dev;
1476ad6f36f8SJohn Baldwin #ifndef NEW_PCIB
1477ad6f36f8SJohn Baldwin 	uint16_t command;
1478ad6f36f8SJohn Baldwin #endif
1479e36af292SJung-uk Kim 	dev = sc->dev;
1480e36af292SJung-uk Kim 
148183c41143SJohn Baldwin #ifdef NEW_PCIB
148283c41143SJohn Baldwin 	pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM);
148383c41143SJohn Baldwin #else
1484ad6f36f8SJohn Baldwin 	command = pci_read_config(dev, PCIR_COMMAND, 2);
1485ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_PORTEN)
1486e36af292SJung-uk Kim 		pcib_set_io_decode(sc);
1487ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_MEMEN)
1488e36af292SJung-uk Kim 		pcib_set_mem_decode(sc);
148983c41143SJohn Baldwin #endif
1490e36af292SJung-uk Kim }
1491e36af292SJung-uk Kim 
1492e36af292SJung-uk Kim /*
1493bb0d0a8eSMike Smith  * Generic device interface
1494bb0d0a8eSMike Smith  */
1495bb0d0a8eSMike Smith static int
1496bb0d0a8eSMike Smith pcib_probe(device_t dev)
1497bb0d0a8eSMike Smith {
1498bb0d0a8eSMike Smith     if ((pci_get_class(dev) == PCIC_BRIDGE) &&
1499bb0d0a8eSMike Smith 	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
1500bb0d0a8eSMike Smith 	device_set_desc(dev, "PCI-PCI bridge");
1501b7cbd25bSMarcel Moolenaar 	return(-10000);
1502bb0d0a8eSMike Smith     }
1503bb0d0a8eSMike Smith     return(ENXIO);
1504bb0d0a8eSMike Smith }
1505bb0d0a8eSMike Smith 
15066f0d5884SJohn Baldwin void
15076f0d5884SJohn Baldwin pcib_attach_common(device_t dev)
1508bb0d0a8eSMike Smith {
1509bb0d0a8eSMike Smith     struct pcib_softc	*sc;
1510abf07f13SWarner Losh     struct sysctl_ctx_list *sctx;
1511abf07f13SWarner Losh     struct sysctl_oid	*soid;
1512c825d4dcSJohn Baldwin     int comma;
1513bb0d0a8eSMike Smith 
1514bb0d0a8eSMike Smith     sc = device_get_softc(dev);
1515bb0d0a8eSMike Smith     sc->dev = dev;
1516bb0d0a8eSMike Smith 
15174fa59183SMike Smith     /*
15184fa59183SMike Smith      * Get current bridge configuration.
15194fa59183SMike Smith      */
152055aaf894SMarius Strobl     sc->domain = pci_get_domain(dev);
1521ad6f36f8SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
1522ad6f36f8SJohn Baldwin     sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1);
1523ad6f36f8SJohn Baldwin     sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
1524ad6f36f8SJohn Baldwin #endif
1525ad6f36f8SJohn Baldwin     sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
1526e36af292SJung-uk Kim     pcib_cfg_save(sc);
15274fa59183SMike Smith 
15284fa59183SMike Smith     /*
15294edef187SJohn Baldwin      * The primary bus register should always be the bus of the
15304edef187SJohn Baldwin      * parent.
15314edef187SJohn Baldwin      */
15324edef187SJohn Baldwin     sc->pribus = pci_get_bus(dev);
15334edef187SJohn Baldwin     pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
15344edef187SJohn Baldwin 
15354edef187SJohn Baldwin     /*
1536abf07f13SWarner Losh      * Setup sysctl reporting nodes
1537abf07f13SWarner Losh      */
1538abf07f13SWarner Losh     sctx = device_get_sysctl_ctx(dev);
1539abf07f13SWarner Losh     soid = device_get_sysctl_tree(dev);
1540abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
1541abf07f13SWarner Losh       CTLFLAG_RD, &sc->domain, 0, "Domain number");
1542abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
1543abf07f13SWarner Losh       CTLFLAG_RD, &sc->pribus, 0, "Primary bus number");
1544abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
15454edef187SJohn Baldwin       CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number");
1546abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
15474edef187SJohn Baldwin       CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number");
1548abf07f13SWarner Losh 
1549abf07f13SWarner Losh     /*
15504fa59183SMike Smith      * Quirk handling.
15514fa59183SMike Smith      */
15524fa59183SMike Smith     switch (pci_get_devid(dev)) {
15532ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
15544fa59183SMike Smith     case 0x12258086:		/* Intel 82454KX/GX (Orion) */
15554fa59183SMike Smith 	{
1556b0cb115fSWarner Losh 	    uint8_t	supbus;
15574fa59183SMike Smith 
15584fa59183SMike Smith 	    supbus = pci_read_config(dev, 0x41, 1);
15594fa59183SMike Smith 	    if (supbus != 0xff) {
15604edef187SJohn Baldwin 		sc->bus.sec = supbus + 1;
15614edef187SJohn Baldwin 		sc->bus.sub = supbus + 1;
15624fa59183SMike Smith 	    }
15634fa59183SMike Smith 	    break;
15644fa59183SMike Smith 	}
15654edef187SJohn Baldwin #endif
15664fa59183SMike Smith 
1567e4b59fc5SWarner Losh     /*
1568e4b59fc5SWarner Losh      * The i82380FB mobile docking controller is a PCI-PCI bridge,
1569e4b59fc5SWarner Losh      * and it is a subtractive bridge.  However, the ProgIf is wrong
1570e4b59fc5SWarner Losh      * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
15714718610dSZbigniew Bodek      * happen.  There are also Toshiba and Cavium ThunderX bridges
15724718610dSZbigniew Bodek      * that behave this way.
1573e4b59fc5SWarner Losh      */
15744718610dSZbigniew Bodek     case 0xa002177d:		/* Cavium ThunderX */
1575e4b59fc5SWarner Losh     case 0x124b8086:		/* Intel 82380FB Mobile */
1576e4b59fc5SWarner Losh     case 0x060513d7:		/* Toshiba ???? */
1577e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
1578e4b59fc5SWarner Losh 	break;
1579c94d6dbeSJung-uk Kim 
15802ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
1581c94d6dbeSJung-uk Kim     /* Compaq R3000 BIOS sets wrong subordinate bus number. */
1582c94d6dbeSJung-uk Kim     case 0x00dd10de:
1583c94d6dbeSJung-uk Kim 	{
1584c94d6dbeSJung-uk Kim 	    char *cp;
1585c94d6dbeSJung-uk Kim 
15862be111bfSDavide Italiano 	    if ((cp = kern_getenv("smbios.planar.maker")) == NULL)
1587c94d6dbeSJung-uk Kim 		break;
15881def0ca6SJung-uk Kim 	    if (strncmp(cp, "Compal", 6) != 0) {
15891def0ca6SJung-uk Kim 		freeenv(cp);
1590c94d6dbeSJung-uk Kim 		break;
15911def0ca6SJung-uk Kim 	    }
15921def0ca6SJung-uk Kim 	    freeenv(cp);
15932be111bfSDavide Italiano 	    if ((cp = kern_getenv("smbios.planar.product")) == NULL)
15941def0ca6SJung-uk Kim 		break;
15951def0ca6SJung-uk Kim 	    if (strncmp(cp, "08A0", 4) != 0) {
15961def0ca6SJung-uk Kim 		freeenv(cp);
15971def0ca6SJung-uk Kim 		break;
15981def0ca6SJung-uk Kim 	    }
15991def0ca6SJung-uk Kim 	    freeenv(cp);
16004edef187SJohn Baldwin 	    if (sc->bus.sub < 0xa) {
1601c94d6dbeSJung-uk Kim 		pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
16024edef187SJohn Baldwin 		sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
1603c94d6dbeSJung-uk Kim 	    }
1604c94d6dbeSJung-uk Kim 	    break;
1605c94d6dbeSJung-uk Kim 	}
16064edef187SJohn Baldwin #endif
1607e4b59fc5SWarner Losh     }
1608e4b59fc5SWarner Losh 
160922bf1c7fSJohn Baldwin     if (pci_msi_device_blacklisted(dev))
161022bf1c7fSJohn Baldwin 	sc->flags |= PCIB_DISABLE_MSI;
161122bf1c7fSJohn Baldwin 
161268e9cbd3SMarius Strobl     if (pci_msix_device_blacklisted(dev))
161368e9cbd3SMarius Strobl 	sc->flags |= PCIB_DISABLE_MSIX;
161468e9cbd3SMarius Strobl 
1615e4b59fc5SWarner Losh     /*
1616e4b59fc5SWarner Losh      * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
1617e4b59fc5SWarner Losh      * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
1618e4b59fc5SWarner Losh      * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
1619e4b59fc5SWarner Losh      * This means they act as if they were subtractively decoding
1620e4b59fc5SWarner Losh      * bridges and pass all transactions.  Mark them and real ProgIf 1
1621e4b59fc5SWarner Losh      * parts as subtractive.
1622e4b59fc5SWarner Losh      */
1623e4b59fc5SWarner Losh     if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
1624657d9f9fSJohn Baldwin       pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
1625e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
1626e4b59fc5SWarner Losh 
162782cb5c3bSJohn Baldwin #ifdef PCI_HP
162882cb5c3bSJohn Baldwin     pcib_probe_hotplug(sc);
162982cb5c3bSJohn Baldwin #endif
163083c41143SJohn Baldwin #ifdef NEW_PCIB
16314edef187SJohn Baldwin #ifdef PCI_RES_BUS
16324edef187SJohn Baldwin     pcib_setup_secbus(dev, &sc->bus, 1);
16334edef187SJohn Baldwin #endif
163483c41143SJohn Baldwin     pcib_probe_windows(sc);
163583c41143SJohn Baldwin #endif
163682cb5c3bSJohn Baldwin #ifdef PCI_HP
163782cb5c3bSJohn Baldwin     if (sc->flags & PCIB_HOTPLUG)
163882cb5c3bSJohn Baldwin 	    pcib_setup_hotplug(sc);
163982cb5c3bSJohn Baldwin #endif
1640bb0d0a8eSMike Smith     if (bootverbose) {
164155aaf894SMarius Strobl 	device_printf(dev, "  domain            %d\n", sc->domain);
16424edef187SJohn Baldwin 	device_printf(dev, "  secondary bus     %d\n", sc->bus.sec);
16434edef187SJohn Baldwin 	device_printf(dev, "  subordinate bus   %d\n", sc->bus.sub);
164483c41143SJohn Baldwin #ifdef NEW_PCIB
164583c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->io))
164683c41143SJohn Baldwin 	    device_printf(dev, "  I/O decode        0x%jx-0x%jx\n",
164783c41143SJohn Baldwin 	      (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit);
164883c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->mem))
164983c41143SJohn Baldwin 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
165083c41143SJohn Baldwin 	      (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit);
165183c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->pmem))
165283c41143SJohn Baldwin 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
165383c41143SJohn Baldwin 	      (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit);
165483c41143SJohn Baldwin #else
165583c41143SJohn Baldwin 	if (pcib_is_io_open(sc))
165683c41143SJohn Baldwin 	    device_printf(dev, "  I/O decode        0x%x-0x%x\n",
165783c41143SJohn Baldwin 	      sc->iobase, sc->iolimit);
1658b0a2d4b8SWarner Losh 	if (pcib_is_nonprefetch_open(sc))
1659b0a2d4b8SWarner Losh 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
1660b0a2d4b8SWarner Losh 	      (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
1661b0a2d4b8SWarner Losh 	if (pcib_is_prefetch_open(sc))
1662b0a2d4b8SWarner Losh 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
1663b0a2d4b8SWarner Losh 	      (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
166483c41143SJohn Baldwin #endif
1665c825d4dcSJohn Baldwin 	if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) ||
1666c825d4dcSJohn Baldwin 	    sc->flags & PCIB_SUBTRACTIVE) {
1667c825d4dcSJohn Baldwin 		device_printf(dev, "  special decode    ");
1668c825d4dcSJohn Baldwin 		comma = 0;
1669c825d4dcSJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) {
1670c825d4dcSJohn Baldwin 			printf("ISA");
1671c825d4dcSJohn Baldwin 			comma = 1;
1672c825d4dcSJohn Baldwin 		}
1673c825d4dcSJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) {
1674c825d4dcSJohn Baldwin 			printf("%sVGA", comma ? ", " : "");
1675c825d4dcSJohn Baldwin 			comma = 1;
1676c825d4dcSJohn Baldwin 		}
1677e4b59fc5SWarner Losh 		if (sc->flags & PCIB_SUBTRACTIVE)
1678c825d4dcSJohn Baldwin 			printf("%ssubtractive", comma ? ", " : "");
1679c825d4dcSJohn Baldwin 		printf("\n");
1680c825d4dcSJohn Baldwin 	}
1681bb0d0a8eSMike Smith     }
1682bb0d0a8eSMike Smith 
1683bb0d0a8eSMike Smith     /*
1684ef888152SJohn Baldwin      * Always enable busmastering on bridges so that transactions
1685ef888152SJohn Baldwin      * initiated on the secondary bus are passed through to the
1686ef888152SJohn Baldwin      * primary bus.
1687ef888152SJohn Baldwin      */
1688ef888152SJohn Baldwin     pci_enable_busmaster(dev);
16896f0d5884SJohn Baldwin }
1690bb0d0a8eSMike Smith 
169182cb5c3bSJohn Baldwin #ifdef PCI_HP
169282cb5c3bSJohn Baldwin static int
169382cb5c3bSJohn Baldwin pcib_present(struct pcib_softc *sc)
169482cb5c3bSJohn Baldwin {
169582cb5c3bSJohn Baldwin 
169682cb5c3bSJohn Baldwin 	if (sc->flags & PCIB_HOTPLUG)
169782cb5c3bSJohn Baldwin 		return (pcib_hotplug_present(sc) != 0);
169882cb5c3bSJohn Baldwin 	return (1);
169982cb5c3bSJohn Baldwin }
170082cb5c3bSJohn Baldwin #endif
170182cb5c3bSJohn Baldwin 
170238906aedSJohn Baldwin int
170367e7d085SJohn Baldwin pcib_attach_child(device_t dev)
17046f0d5884SJohn Baldwin {
17056f0d5884SJohn Baldwin 	struct pcib_softc *sc;
17066f0d5884SJohn Baldwin 
17076f0d5884SJohn Baldwin 	sc = device_get_softc(dev);
170867e7d085SJohn Baldwin 	if (sc->bus.sec == 0) {
170967e7d085SJohn Baldwin 		/* no secondary bus; we should have fixed this */
171067e7d085SJohn Baldwin 		return(0);
171167e7d085SJohn Baldwin 	}
171267e7d085SJohn Baldwin 
171382cb5c3bSJohn Baldwin #ifdef PCI_HP
171482cb5c3bSJohn Baldwin 	if (!pcib_present(sc)) {
171582cb5c3bSJohn Baldwin 		/* An empty HotPlug slot, so don't add a PCI bus yet. */
171682cb5c3bSJohn Baldwin 		return (0);
171782cb5c3bSJohn Baldwin 	}
171882cb5c3bSJohn Baldwin #endif
171982cb5c3bSJohn Baldwin 
172067e7d085SJohn Baldwin 	sc->child = device_add_child(dev, "pci", -1);
1721bb0d0a8eSMike Smith 	return (bus_generic_attach(dev));
1722bb0d0a8eSMike Smith }
1723bb0d0a8eSMike Smith 
172467e7d085SJohn Baldwin int
172567e7d085SJohn Baldwin pcib_attach(device_t dev)
172667e7d085SJohn Baldwin {
172767e7d085SJohn Baldwin 
172867e7d085SJohn Baldwin     pcib_attach_common(dev);
172967e7d085SJohn Baldwin     return (pcib_attach_child(dev));
1730bb0d0a8eSMike Smith }
1731bb0d0a8eSMike Smith 
17326f0d5884SJohn Baldwin int
17336f33eaa5SJohn Baldwin pcib_detach(device_t dev)
17346f33eaa5SJohn Baldwin {
17356f33eaa5SJohn Baldwin #if defined(PCI_HP) || defined(NEW_PCIB)
17366f33eaa5SJohn Baldwin 	struct pcib_softc *sc;
17376f33eaa5SJohn Baldwin #endif
17386f33eaa5SJohn Baldwin 	int error;
17396f33eaa5SJohn Baldwin 
17406f33eaa5SJohn Baldwin #if defined(PCI_HP) || defined(NEW_PCIB)
17416f33eaa5SJohn Baldwin 	sc = device_get_softc(dev);
17426f33eaa5SJohn Baldwin #endif
17436f33eaa5SJohn Baldwin 	error = bus_generic_detach(dev);
17446f33eaa5SJohn Baldwin 	if (error)
17456f33eaa5SJohn Baldwin 		return (error);
17466f33eaa5SJohn Baldwin #ifdef PCI_HP
17476f33eaa5SJohn Baldwin 	if (sc->flags & PCIB_HOTPLUG) {
17486f33eaa5SJohn Baldwin 		error = pcib_detach_hotplug(sc);
17496f33eaa5SJohn Baldwin 		if (error)
17506f33eaa5SJohn Baldwin 			return (error);
17516f33eaa5SJohn Baldwin 	}
17526f33eaa5SJohn Baldwin #endif
17536f33eaa5SJohn Baldwin 	error = device_delete_children(dev);
17546f33eaa5SJohn Baldwin 	if (error)
17556f33eaa5SJohn Baldwin 		return (error);
17566f33eaa5SJohn Baldwin #ifdef NEW_PCIB
17576f33eaa5SJohn Baldwin 	pcib_free_windows(sc);
17586f33eaa5SJohn Baldwin #ifdef PCI_RES_BUS
17596f33eaa5SJohn Baldwin 	pcib_free_secbus(dev, &sc->bus);
17606f33eaa5SJohn Baldwin #endif
17616f33eaa5SJohn Baldwin #endif
17626f33eaa5SJohn Baldwin 	return (0);
17636f33eaa5SJohn Baldwin }
17646f33eaa5SJohn Baldwin 
17656f33eaa5SJohn Baldwin int
1766e36af292SJung-uk Kim pcib_suspend(device_t dev)
1767e36af292SJung-uk Kim {
1768e36af292SJung-uk Kim 
1769e36af292SJung-uk Kim 	pcib_cfg_save(device_get_softc(dev));
17707212fc6aSJohn Baldwin 	return (bus_generic_suspend(dev));
1771e36af292SJung-uk Kim }
1772e36af292SJung-uk Kim 
1773e36af292SJung-uk Kim int
1774e36af292SJung-uk Kim pcib_resume(device_t dev)
1775e36af292SJung-uk Kim {
1776e36af292SJung-uk Kim 
1777e36af292SJung-uk Kim 	pcib_cfg_restore(device_get_softc(dev));
1778e36af292SJung-uk Kim 	return (bus_generic_resume(dev));
1779e36af292SJung-uk Kim }
1780e36af292SJung-uk Kim 
1781809923caSJustin Hibbits void
1782809923caSJustin Hibbits pcib_bridge_init(device_t dev)
1783809923caSJustin Hibbits {
1784809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
1785809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2);
1786809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1);
1787809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2);
1788809923caSJustin Hibbits 	pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2);
1789809923caSJustin Hibbits 	pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2);
1790809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
1791809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4);
1792809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2);
1793809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4);
1794809923caSJustin Hibbits }
1795809923caSJustin Hibbits 
1796e36af292SJung-uk Kim int
179782cb5c3bSJohn Baldwin pcib_child_present(device_t dev, device_t child)
179882cb5c3bSJohn Baldwin {
179982cb5c3bSJohn Baldwin #ifdef PCI_HP
180082cb5c3bSJohn Baldwin 	struct pcib_softc *sc = device_get_softc(dev);
180182cb5c3bSJohn Baldwin 	int retval;
180282cb5c3bSJohn Baldwin 
180382cb5c3bSJohn Baldwin 	retval = bus_child_present(dev);
180482cb5c3bSJohn Baldwin 	if (retval != 0 && sc->flags & PCIB_HOTPLUG)
180582cb5c3bSJohn Baldwin 		retval = pcib_hotplug_present(sc);
180682cb5c3bSJohn Baldwin 	return (retval);
180782cb5c3bSJohn Baldwin #else
180882cb5c3bSJohn Baldwin 	return (bus_child_present(dev));
180982cb5c3bSJohn Baldwin #endif
181082cb5c3bSJohn Baldwin }
181182cb5c3bSJohn Baldwin 
181282cb5c3bSJohn Baldwin int
1813bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1814bb0d0a8eSMike Smith {
1815bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
1816bb0d0a8eSMike Smith 
1817bb0d0a8eSMike Smith     switch (which) {
181855aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
181955aaf894SMarius Strobl 	*result = sc->domain;
182055aaf894SMarius Strobl 	return(0);
1821bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
18224edef187SJohn Baldwin 	*result = sc->bus.sec;
1823bb0d0a8eSMike Smith 	return(0);
1824bb0d0a8eSMike Smith     }
1825bb0d0a8eSMike Smith     return(ENOENT);
1826bb0d0a8eSMike Smith }
1827bb0d0a8eSMike Smith 
18286f0d5884SJohn Baldwin int
1829bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1830bb0d0a8eSMike Smith {
1831bb0d0a8eSMike Smith 
1832bb0d0a8eSMike Smith     switch (which) {
183355aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
183455aaf894SMarius Strobl 	return(EINVAL);
1835bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
18364edef187SJohn Baldwin 	return(EINVAL);
1837bb0d0a8eSMike Smith     }
1838bb0d0a8eSMike Smith     return(ENOENT);
1839bb0d0a8eSMike Smith }
1840bb0d0a8eSMike Smith 
184183c41143SJohn Baldwin #ifdef NEW_PCIB
184283c41143SJohn Baldwin /*
184383c41143SJohn Baldwin  * Attempt to allocate a resource from the existing resources assigned
184483c41143SJohn Baldwin  * to a window.
184583c41143SJohn Baldwin  */
184683c41143SJohn Baldwin static struct resource *
184783c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w,
18482dd1bdf1SJustin Hibbits     device_t child, int type, int *rid, rman_res_t start, rman_res_t end,
18492dd1bdf1SJustin Hibbits     rman_res_t count, u_int flags)
185083c41143SJohn Baldwin {
185183c41143SJohn Baldwin 	struct resource *res;
185283c41143SJohn Baldwin 
185383c41143SJohn Baldwin 	if (!pcib_is_window_open(w))
185483c41143SJohn Baldwin 		return (NULL);
185583c41143SJohn Baldwin 
185683c41143SJohn Baldwin 	res = rman_reserve_resource(&w->rman, start, end, count,
185783c41143SJohn Baldwin 	    flags & ~RF_ACTIVE, child);
185883c41143SJohn Baldwin 	if (res == NULL)
185983c41143SJohn Baldwin 		return (NULL);
186083c41143SJohn Baldwin 
186183c41143SJohn Baldwin 	if (bootverbose)
186283c41143SJohn Baldwin 		device_printf(sc->dev,
1863da1b038aSJustin Hibbits 		    "allocated %s range (%#jx-%#jx) for rid %x of %s\n",
186483c41143SJohn Baldwin 		    w->name, rman_get_start(res), rman_get_end(res), *rid,
186583c41143SJohn Baldwin 		    pcib_child_name(child));
186683c41143SJohn Baldwin 	rman_set_rid(res, *rid);
186783c41143SJohn Baldwin 
186883c41143SJohn Baldwin 	/*
186983c41143SJohn Baldwin 	 * If the resource should be active, pass that request up the
187083c41143SJohn Baldwin 	 * tree.  This assumes the parent drivers can handle
187183c41143SJohn Baldwin 	 * activating sub-allocated resources.
187283c41143SJohn Baldwin 	 */
187383c41143SJohn Baldwin 	if (flags & RF_ACTIVE) {
187483c41143SJohn Baldwin 		if (bus_activate_resource(child, type, *rid, res) != 0) {
187583c41143SJohn Baldwin 			rman_release_resource(res);
187683c41143SJohn Baldwin 			return (NULL);
187783c41143SJohn Baldwin 		}
187883c41143SJohn Baldwin 	}
187983c41143SJohn Baldwin 
188083c41143SJohn Baldwin 	return (res);
188183c41143SJohn Baldwin }
188283c41143SJohn Baldwin 
1883c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */
1884c825d4dcSJohn Baldwin static int
1885c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type,
18862dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1887c825d4dcSJohn Baldwin {
1888c825d4dcSJohn Baldwin 	struct resource *res;
18892dd1bdf1SJustin Hibbits 	rman_res_t base, limit, wmask;
1890c825d4dcSJohn Baldwin 	int rid;
1891c825d4dcSJohn Baldwin 
1892c825d4dcSJohn Baldwin 	/*
1893c825d4dcSJohn Baldwin 	 * If this is an I/O window on a bridge with ISA enable set
1894c825d4dcSJohn Baldwin 	 * and the start address is below 64k, then try to allocate an
1895c825d4dcSJohn Baldwin 	 * initial window of 0x1000 bytes long starting at address
1896c825d4dcSJohn Baldwin 	 * 0xf000 and walking down.  Note that if the original request
1897c825d4dcSJohn Baldwin 	 * was larger than the non-aliased range size of 0x100 our
1898c825d4dcSJohn Baldwin 	 * caller would have raised the start address up to 64k
1899c825d4dcSJohn Baldwin 	 * already.
1900c825d4dcSJohn Baldwin 	 */
1901c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1902c825d4dcSJohn Baldwin 	    start < 65536) {
1903c825d4dcSJohn Baldwin 		for (base = 0xf000; (long)base >= 0; base -= 0x1000) {
1904c825d4dcSJohn Baldwin 			limit = base + 0xfff;
1905c825d4dcSJohn Baldwin 
1906c825d4dcSJohn Baldwin 			/*
1907c825d4dcSJohn Baldwin 			 * Skip ranges that wouldn't work for the
1908c825d4dcSJohn Baldwin 			 * original request.  Note that the actual
1909c825d4dcSJohn Baldwin 			 * window that overlaps are the non-alias
1910c825d4dcSJohn Baldwin 			 * ranges within [base, limit], so this isn't
1911c825d4dcSJohn Baldwin 			 * quite a simple comparison.
1912c825d4dcSJohn Baldwin 			 */
1913c825d4dcSJohn Baldwin 			if (start + count > limit - 0x400)
1914c825d4dcSJohn Baldwin 				continue;
1915c825d4dcSJohn Baldwin 			if (base == 0) {
1916c825d4dcSJohn Baldwin 				/*
1917c825d4dcSJohn Baldwin 				 * The first open region for the window at
1918c825d4dcSJohn Baldwin 				 * 0 is 0x400-0x4ff.
1919c825d4dcSJohn Baldwin 				 */
1920c825d4dcSJohn Baldwin 				if (end - count + 1 < 0x400)
1921c825d4dcSJohn Baldwin 					continue;
1922c825d4dcSJohn Baldwin 			} else {
1923c825d4dcSJohn Baldwin 				if (end - count + 1 < base)
1924c825d4dcSJohn Baldwin 					continue;
1925c825d4dcSJohn Baldwin 			}
1926c825d4dcSJohn Baldwin 
1927c825d4dcSJohn Baldwin 			if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) {
1928c825d4dcSJohn Baldwin 				w->base = base;
1929c825d4dcSJohn Baldwin 				w->limit = limit;
1930c825d4dcSJohn Baldwin 				return (0);
1931c825d4dcSJohn Baldwin 			}
1932c825d4dcSJohn Baldwin 		}
1933c825d4dcSJohn Baldwin 		return (ENOSPC);
1934c825d4dcSJohn Baldwin 	}
1935c825d4dcSJohn Baldwin 
193689977ce2SJustin Hibbits 	wmask = ((rman_res_t)1 << w->step) - 1;
1937c825d4dcSJohn Baldwin 	if (RF_ALIGNMENT(flags) < w->step) {
1938c825d4dcSJohn Baldwin 		flags &= ~RF_ALIGNMENT_MASK;
1939c825d4dcSJohn Baldwin 		flags |= RF_ALIGNMENT_LOG2(w->step);
1940c825d4dcSJohn Baldwin 	}
1941c825d4dcSJohn Baldwin 	start &= ~wmask;
1942c825d4dcSJohn Baldwin 	end |= wmask;
194389977ce2SJustin Hibbits 	count = roundup2(count, (rman_res_t)1 << w->step);
1944c825d4dcSJohn Baldwin 	rid = w->reg;
1945c825d4dcSJohn Baldwin 	res = bus_alloc_resource(sc->dev, type, &rid, start, end, count,
1946c825d4dcSJohn Baldwin 	    flags & ~RF_ACTIVE);
1947c825d4dcSJohn Baldwin 	if (res == NULL)
1948c825d4dcSJohn Baldwin 		return (ENOSPC);
1949c825d4dcSJohn Baldwin 	pcib_add_window_resources(w, &res, 1);
1950c825d4dcSJohn Baldwin 	pcib_activate_window(sc, type);
1951c825d4dcSJohn Baldwin 	w->base = rman_get_start(res);
1952c825d4dcSJohn Baldwin 	w->limit = rman_get_end(res);
1953c825d4dcSJohn Baldwin 	return (0);
1954c825d4dcSJohn Baldwin }
1955c825d4dcSJohn Baldwin 
1956c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */
1957c825d4dcSJohn Baldwin static int
1958c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type,
19592dd1bdf1SJustin Hibbits     rman_res_t base, rman_res_t limit)
1960c825d4dcSJohn Baldwin {
1961c825d4dcSJohn Baldwin 	struct resource *res;
1962c825d4dcSJohn Baldwin 	int error, i, force_64k_base;
1963c825d4dcSJohn Baldwin 
1964c825d4dcSJohn Baldwin 	KASSERT(base <= w->base && limit >= w->limit,
1965c825d4dcSJohn Baldwin 	    ("attempting to shrink window"));
1966c825d4dcSJohn Baldwin 
1967c825d4dcSJohn Baldwin 	/*
1968c825d4dcSJohn Baldwin 	 * XXX: pcib_grow_window() doesn't try to do this anyway and
1969c825d4dcSJohn Baldwin 	 * the error handling for all the edge cases would be tedious.
1970c825d4dcSJohn Baldwin 	 */
1971c825d4dcSJohn Baldwin 	KASSERT(limit == w->limit || base == w->base,
1972c825d4dcSJohn Baldwin 	    ("attempting to grow both ends of a window"));
1973c825d4dcSJohn Baldwin 
1974c825d4dcSJohn Baldwin 	/*
1975c825d4dcSJohn Baldwin 	 * Yet more special handling for requests to expand an I/O
1976c825d4dcSJohn Baldwin 	 * window behind an ISA-enabled bridge.  Since I/O windows
1977c825d4dcSJohn Baldwin 	 * have to grow in 0x1000 increments and the end of the 0xffff
1978c825d4dcSJohn Baldwin 	 * range is an alias, growing a window below 64k will always
1979c825d4dcSJohn Baldwin 	 * result in allocating new resources and never adjusting an
1980c825d4dcSJohn Baldwin 	 * existing resource.
1981c825d4dcSJohn Baldwin 	 */
1982c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1983c825d4dcSJohn Baldwin 	    (limit <= 65535 || (base <= 65535 && base != w->base))) {
1984c825d4dcSJohn Baldwin 		KASSERT(limit == w->limit || limit <= 65535,
1985c825d4dcSJohn Baldwin 		    ("attempting to grow both ends across 64k ISA alias"));
1986c825d4dcSJohn Baldwin 
1987c825d4dcSJohn Baldwin 		if (base != w->base)
1988c825d4dcSJohn Baldwin 			error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1);
1989c825d4dcSJohn Baldwin 		else
1990c825d4dcSJohn Baldwin 			error = pcib_alloc_nonisa_ranges(sc, w->limit + 1,
1991c825d4dcSJohn Baldwin 			    limit);
1992c825d4dcSJohn Baldwin 		if (error == 0) {
1993c825d4dcSJohn Baldwin 			w->base = base;
1994c825d4dcSJohn Baldwin 			w->limit = limit;
1995c825d4dcSJohn Baldwin 		}
1996c825d4dcSJohn Baldwin 		return (error);
1997c825d4dcSJohn Baldwin 	}
1998c825d4dcSJohn Baldwin 
1999c825d4dcSJohn Baldwin 	/*
2000c825d4dcSJohn Baldwin 	 * Find the existing resource to adjust.  Usually there is only one,
2001c825d4dcSJohn Baldwin 	 * but for an ISA-enabled bridge we might be growing the I/O window
2002c825d4dcSJohn Baldwin 	 * above 64k and need to find the existing resource that maps all
2003c825d4dcSJohn Baldwin 	 * of the area above 64k.
2004c825d4dcSJohn Baldwin 	 */
2005c825d4dcSJohn Baldwin 	for (i = 0; i < w->count; i++) {
2006c825d4dcSJohn Baldwin 		if (rman_get_end(w->res[i]) == w->limit)
2007c825d4dcSJohn Baldwin 			break;
2008c825d4dcSJohn Baldwin 	}
2009c825d4dcSJohn Baldwin 	KASSERT(i != w->count, ("did not find existing resource"));
2010c825d4dcSJohn Baldwin 	res = w->res[i];
2011c825d4dcSJohn Baldwin 
2012c825d4dcSJohn Baldwin 	/*
2013c825d4dcSJohn Baldwin 	 * Usually the resource we found should match the window's
2014c825d4dcSJohn Baldwin 	 * existing range.  The one exception is the ISA-enabled case
2015c825d4dcSJohn Baldwin 	 * mentioned above in which case the resource should start at
2016c825d4dcSJohn Baldwin 	 * 64k.
2017c825d4dcSJohn Baldwin 	 */
2018c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
2019c825d4dcSJohn Baldwin 	    w->base <= 65535) {
2020c825d4dcSJohn Baldwin 		KASSERT(rman_get_start(res) == 65536,
2021c825d4dcSJohn Baldwin 		    ("existing resource mismatch"));
2022c825d4dcSJohn Baldwin 		force_64k_base = 1;
2023c825d4dcSJohn Baldwin 	} else {
2024c825d4dcSJohn Baldwin 		KASSERT(w->base == rman_get_start(res),
2025c825d4dcSJohn Baldwin 		    ("existing resource mismatch"));
2026c825d4dcSJohn Baldwin 		force_64k_base = 0;
2027c825d4dcSJohn Baldwin 	}
2028c825d4dcSJohn Baldwin 
2029c825d4dcSJohn Baldwin 	error = bus_adjust_resource(sc->dev, type, res, force_64k_base ?
2030c825d4dcSJohn Baldwin 	    rman_get_start(res) : base, limit);
2031c825d4dcSJohn Baldwin 	if (error)
2032c825d4dcSJohn Baldwin 		return (error);
2033c825d4dcSJohn Baldwin 
2034c825d4dcSJohn Baldwin 	/* Add the newly allocated region to the resource manager. */
2035c825d4dcSJohn Baldwin 	if (w->base != base) {
2036c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, base, w->base - 1);
2037c825d4dcSJohn Baldwin 		w->base = base;
2038c825d4dcSJohn Baldwin 	} else {
2039c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, w->limit + 1, limit);
2040c825d4dcSJohn Baldwin 		w->limit = limit;
2041c825d4dcSJohn Baldwin 	}
2042c825d4dcSJohn Baldwin 	if (error) {
2043c825d4dcSJohn Baldwin 		if (bootverbose)
2044c825d4dcSJohn Baldwin 			device_printf(sc->dev,
2045c825d4dcSJohn Baldwin 			    "failed to expand %s resource manager\n", w->name);
2046c825d4dcSJohn Baldwin 		(void)bus_adjust_resource(sc->dev, type, res, force_64k_base ?
2047c825d4dcSJohn Baldwin 		    rman_get_start(res) : w->base, w->limit);
2048c825d4dcSJohn Baldwin 	}
2049c825d4dcSJohn Baldwin 	return (error);
2050c825d4dcSJohn Baldwin }
2051c825d4dcSJohn Baldwin 
205283c41143SJohn Baldwin /*
205383c41143SJohn Baldwin  * Attempt to grow a window to make room for a given resource request.
205483c41143SJohn Baldwin  */
205583c41143SJohn Baldwin static int
205683c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type,
20572dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
205883c41143SJohn Baldwin {
20592dd1bdf1SJustin Hibbits 	rman_res_t align, start_free, end_free, front, back, wmask;
2060c825d4dcSJohn Baldwin 	int error;
206183c41143SJohn Baldwin 
206283c41143SJohn Baldwin 	/*
206383c41143SJohn Baldwin 	 * Clamp the desired resource range to the maximum address
206483c41143SJohn Baldwin 	 * this window supports.  Reject impossible requests.
2065c825d4dcSJohn Baldwin 	 *
2066c825d4dcSJohn Baldwin 	 * For I/O port requests behind a bridge with the ISA enable
2067c825d4dcSJohn Baldwin 	 * bit set, force large allocations to start above 64k.
206883c41143SJohn Baldwin 	 */
206983c41143SJohn Baldwin 	if (!w->valid)
207083c41143SJohn Baldwin 		return (EINVAL);
2071c825d4dcSJohn Baldwin 	if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 &&
2072c825d4dcSJohn Baldwin 	    start < 65536)
2073c825d4dcSJohn Baldwin 		start = 65536;
207483c41143SJohn Baldwin 	if (end > w->rman.rm_end)
207583c41143SJohn Baldwin 		end = w->rman.rm_end;
207683c41143SJohn Baldwin 	if (start + count - 1 > end || start + count < start)
207783c41143SJohn Baldwin 		return (EINVAL);
207889977ce2SJustin Hibbits 	wmask = ((rman_res_t)1 << w->step) - 1;
207983c41143SJohn Baldwin 
208083c41143SJohn Baldwin 	/*
208183c41143SJohn Baldwin 	 * If there is no resource at all, just try to allocate enough
208283c41143SJohn Baldwin 	 * aligned space for this resource.
208383c41143SJohn Baldwin 	 */
208483c41143SJohn Baldwin 	if (w->res == NULL) {
2085c825d4dcSJohn Baldwin 		error = pcib_alloc_new_window(sc, w, type, start, end, count,
2086c825d4dcSJohn Baldwin 		    flags);
2087c825d4dcSJohn Baldwin 		if (error) {
208883c41143SJohn Baldwin 			if (bootverbose)
208983c41143SJohn Baldwin 				device_printf(sc->dev,
2090da1b038aSJustin Hibbits 		    "failed to allocate initial %s window (%#jx-%#jx,%#jx)\n",
209183c41143SJohn Baldwin 				    w->name, start, end, count);
209283c41143SJohn Baldwin 			return (error);
209383c41143SJohn Baldwin 		}
2094c825d4dcSJohn Baldwin 		if (bootverbose)
2095c825d4dcSJohn Baldwin 			device_printf(sc->dev,
2096c825d4dcSJohn Baldwin 			    "allocated initial %s window of %#jx-%#jx\n",
2097c825d4dcSJohn Baldwin 			    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
209883c41143SJohn Baldwin 		goto updatewin;
209983c41143SJohn Baldwin 	}
210083c41143SJohn Baldwin 
210183c41143SJohn Baldwin 	/*
210283c41143SJohn Baldwin 	 * See if growing the window would help.  Compute the minimum
210383c41143SJohn Baldwin 	 * amount of address space needed on both the front and back
210483c41143SJohn Baldwin 	 * ends of the existing window to satisfy the allocation.
210583c41143SJohn Baldwin 	 *
210683c41143SJohn Baldwin 	 * For each end, build a candidate region adjusting for the
210783c41143SJohn Baldwin 	 * required alignment, etc.  If there is a free region at the
210883c41143SJohn Baldwin 	 * edge of the window, grow from the inner edge of the free
210983c41143SJohn Baldwin 	 * region.  Otherwise grow from the window boundary.
211083c41143SJohn Baldwin 	 *
2111c825d4dcSJohn Baldwin 	 * Growing an I/O window below 64k for a bridge with the ISA
2112c825d4dcSJohn Baldwin 	 * enable bit doesn't require any special magic as the step
2113c825d4dcSJohn Baldwin 	 * size of an I/O window (1k) always includes multiple
2114c825d4dcSJohn Baldwin 	 * non-alias ranges when it is grown in either direction.
2115c825d4dcSJohn Baldwin 	 *
211683c41143SJohn Baldwin 	 * XXX: Special case: if w->res is completely empty and the
211783c41143SJohn Baldwin 	 * request size is larger than w->res, we should find the
211883c41143SJohn Baldwin 	 * optimal aligned buffer containing w->res and allocate that.
211983c41143SJohn Baldwin 	 */
212083c41143SJohn Baldwin 	if (bootverbose)
212183c41143SJohn Baldwin 		device_printf(sc->dev,
2122da1b038aSJustin Hibbits 		    "attempting to grow %s window for (%#jx-%#jx,%#jx)\n",
212383c41143SJohn Baldwin 		    w->name, start, end, count);
212489977ce2SJustin Hibbits 	align = (rman_res_t)1 << RF_ALIGNMENT(flags);
2125c825d4dcSJohn Baldwin 	if (start < w->base) {
212683c41143SJohn Baldwin 		if (rman_first_free_region(&w->rman, &start_free, &end_free) !=
2127c825d4dcSJohn Baldwin 		    0 || start_free != w->base)
2128c825d4dcSJohn Baldwin 			end_free = w->base;
212983c41143SJohn Baldwin 		if (end_free > end)
2130ddac8cc9SJohn Baldwin 			end_free = end + 1;
213183c41143SJohn Baldwin 
213283c41143SJohn Baldwin 		/* Move end_free down until it is properly aligned. */
213383c41143SJohn Baldwin 		end_free &= ~(align - 1);
2134a49dcb46SJohn Baldwin 		end_free--;
2135a49dcb46SJohn Baldwin 		front = end_free - (count - 1);
213683c41143SJohn Baldwin 
213783c41143SJohn Baldwin 		/*
213883c41143SJohn Baldwin 		 * The resource would now be allocated at (front,
213983c41143SJohn Baldwin 		 * end_free).  Ensure that fits in the (start, end)
214083c41143SJohn Baldwin 		 * bounds.  end_free is checked above.  If 'front' is
214183c41143SJohn Baldwin 		 * ok, ensure it is properly aligned for this window.
214283c41143SJohn Baldwin 		 * Also check for underflow.
214383c41143SJohn Baldwin 		 */
214483c41143SJohn Baldwin 		if (front >= start && front <= end_free) {
214583c41143SJohn Baldwin 			if (bootverbose)
2146da1b038aSJustin Hibbits 				printf("\tfront candidate range: %#jx-%#jx\n",
214783c41143SJohn Baldwin 				    front, end_free);
2148a7b5acacSJohn Baldwin 			front &= ~wmask;
2149c825d4dcSJohn Baldwin 			front = w->base - front;
215083c41143SJohn Baldwin 		} else
215183c41143SJohn Baldwin 			front = 0;
215283c41143SJohn Baldwin 	} else
215383c41143SJohn Baldwin 		front = 0;
2154c825d4dcSJohn Baldwin 	if (end > w->limit) {
215583c41143SJohn Baldwin 		if (rman_last_free_region(&w->rman, &start_free, &end_free) !=
2156c825d4dcSJohn Baldwin 		    0 || end_free != w->limit)
2157c825d4dcSJohn Baldwin 			start_free = w->limit + 1;
215883c41143SJohn Baldwin 		if (start_free < start)
215983c41143SJohn Baldwin 			start_free = start;
216083c41143SJohn Baldwin 
216183c41143SJohn Baldwin 		/* Move start_free up until it is properly aligned. */
216283c41143SJohn Baldwin 		start_free = roundup2(start_free, align);
2163a49dcb46SJohn Baldwin 		back = start_free + count - 1;
216483c41143SJohn Baldwin 
216583c41143SJohn Baldwin 		/*
216683c41143SJohn Baldwin 		 * The resource would now be allocated at (start_free,
216783c41143SJohn Baldwin 		 * back).  Ensure that fits in the (start, end)
216883c41143SJohn Baldwin 		 * bounds.  start_free is checked above.  If 'back' is
216983c41143SJohn Baldwin 		 * ok, ensure it is properly aligned for this window.
217083c41143SJohn Baldwin 		 * Also check for overflow.
217183c41143SJohn Baldwin 		 */
217283c41143SJohn Baldwin 		if (back <= end && start_free <= back) {
217383c41143SJohn Baldwin 			if (bootverbose)
2174da1b038aSJustin Hibbits 				printf("\tback candidate range: %#jx-%#jx\n",
217583c41143SJohn Baldwin 				    start_free, back);
2176a7b5acacSJohn Baldwin 			back |= wmask;
2177c825d4dcSJohn Baldwin 			back -= w->limit;
217883c41143SJohn Baldwin 		} else
217983c41143SJohn Baldwin 			back = 0;
218083c41143SJohn Baldwin 	} else
218183c41143SJohn Baldwin 		back = 0;
218283c41143SJohn Baldwin 
218383c41143SJohn Baldwin 	/*
218483c41143SJohn Baldwin 	 * Try to allocate the smallest needed region first.
218583c41143SJohn Baldwin 	 * If that fails, fall back to the other region.
218683c41143SJohn Baldwin 	 */
218783c41143SJohn Baldwin 	error = ENOSPC;
218883c41143SJohn Baldwin 	while (front != 0 || back != 0) {
218983c41143SJohn Baldwin 		if (front != 0 && (front <= back || back == 0)) {
2190c825d4dcSJohn Baldwin 			error = pcib_expand_window(sc, w, type, w->base - front,
2191c825d4dcSJohn Baldwin 			    w->limit);
219283c41143SJohn Baldwin 			if (error == 0)
219383c41143SJohn Baldwin 				break;
219483c41143SJohn Baldwin 			front = 0;
219583c41143SJohn Baldwin 		} else {
2196c825d4dcSJohn Baldwin 			error = pcib_expand_window(sc, w, type, w->base,
2197c825d4dcSJohn Baldwin 			    w->limit + back);
219883c41143SJohn Baldwin 			if (error == 0)
219983c41143SJohn Baldwin 				break;
220083c41143SJohn Baldwin 			back = 0;
220183c41143SJohn Baldwin 		}
220283c41143SJohn Baldwin 	}
220383c41143SJohn Baldwin 
220483c41143SJohn Baldwin 	if (error)
220583c41143SJohn Baldwin 		return (error);
220683c41143SJohn Baldwin 	if (bootverbose)
2207c825d4dcSJohn Baldwin 		device_printf(sc->dev, "grew %s window to %#jx-%#jx\n",
2208c825d4dcSJohn Baldwin 		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
220983c41143SJohn Baldwin 
221083c41143SJohn Baldwin updatewin:
2211c825d4dcSJohn Baldwin 	/* Write the new window. */
2212a7b5acacSJohn Baldwin 	KASSERT((w->base & wmask) == 0, ("start address is not aligned"));
2213a7b5acacSJohn Baldwin 	KASSERT((w->limit & wmask) == wmask, ("end address is not aligned"));
221483c41143SJohn Baldwin 	pcib_write_windows(sc, w->mask);
221583c41143SJohn Baldwin 	return (0);
221683c41143SJohn Baldwin }
221783c41143SJohn Baldwin 
221883c41143SJohn Baldwin /*
221983c41143SJohn Baldwin  * We have to trap resource allocation requests and ensure that the bridge
222083c41143SJohn Baldwin  * is set up to, or capable of handling them.
222183c41143SJohn Baldwin  */
222283c41143SJohn Baldwin struct resource *
222383c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
22242dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
222583c41143SJohn Baldwin {
222683c41143SJohn Baldwin 	struct pcib_softc *sc;
222783c41143SJohn Baldwin 	struct resource *r;
222883c41143SJohn Baldwin 
222983c41143SJohn Baldwin 	sc = device_get_softc(dev);
223083c41143SJohn Baldwin 
223183c41143SJohn Baldwin 	/*
223283c41143SJohn Baldwin 	 * VGA resources are decoded iff the VGA enable bit is set in
223383c41143SJohn Baldwin 	 * the bridge control register.  VGA resources do not fall into
223483c41143SJohn Baldwin 	 * the resource windows and are passed up to the parent.
223583c41143SJohn Baldwin 	 */
223683c41143SJohn Baldwin 	if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) ||
223783c41143SJohn Baldwin 	    (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) {
223883c41143SJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE)
223983c41143SJohn Baldwin 			return (bus_generic_alloc_resource(dev, child, type,
224083c41143SJohn Baldwin 			    rid, start, end, count, flags));
224183c41143SJohn Baldwin 		else
224283c41143SJohn Baldwin 			return (NULL);
224383c41143SJohn Baldwin 	}
224483c41143SJohn Baldwin 
224583c41143SJohn Baldwin 	switch (type) {
22464edef187SJohn Baldwin #ifdef PCI_RES_BUS
22474edef187SJohn Baldwin 	case PCI_RES_BUS:
22484edef187SJohn Baldwin 		return (pcib_alloc_subbus(&sc->bus, child, rid, start, end,
22494edef187SJohn Baldwin 		    count, flags));
22504edef187SJohn Baldwin #endif
225183c41143SJohn Baldwin 	case SYS_RES_IOPORT:
2252c825d4dcSJohn Baldwin 		if (pcib_is_isa_range(sc, start, end, count))
2253c825d4dcSJohn Baldwin 			return (NULL);
225483c41143SJohn Baldwin 		r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start,
225583c41143SJohn Baldwin 		    end, count, flags);
2256a6c82265SMarius Strobl 		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
225783c41143SJohn Baldwin 			break;
225883c41143SJohn Baldwin 		if (pcib_grow_window(sc, &sc->io, type, start, end, count,
225983c41143SJohn Baldwin 		    flags) == 0)
226083c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->io, child, type,
226183c41143SJohn Baldwin 			    rid, start, end, count, flags);
226283c41143SJohn Baldwin 		break;
226383c41143SJohn Baldwin 	case SYS_RES_MEMORY:
226483c41143SJohn Baldwin 		/*
226583c41143SJohn Baldwin 		 * For prefetchable resources, prefer the prefetchable
226683c41143SJohn Baldwin 		 * memory window, but fall back to the regular memory
226783c41143SJohn Baldwin 		 * window if that fails.  Try both windows before
226883c41143SJohn Baldwin 		 * attempting to grow a window in case the firmware
226983c41143SJohn Baldwin 		 * has used a range in the regular memory window to
227083c41143SJohn Baldwin 		 * map a prefetchable BAR.
227183c41143SJohn Baldwin 		 */
227283c41143SJohn Baldwin 		if (flags & RF_PREFETCHABLE) {
227383c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->pmem, child, type,
227483c41143SJohn Baldwin 			    rid, start, end, count, flags);
227583c41143SJohn Baldwin 			if (r != NULL)
227683c41143SJohn Baldwin 				break;
227783c41143SJohn Baldwin 		}
227883c41143SJohn Baldwin 		r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid,
227983c41143SJohn Baldwin 		    start, end, count, flags);
2280a6c82265SMarius Strobl 		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
228183c41143SJohn Baldwin 			break;
228283c41143SJohn Baldwin 		if (flags & RF_PREFETCHABLE) {
228383c41143SJohn Baldwin 			if (pcib_grow_window(sc, &sc->pmem, type, start, end,
228483c41143SJohn Baldwin 			    count, flags) == 0) {
228583c41143SJohn Baldwin 				r = pcib_suballoc_resource(sc, &sc->pmem, child,
228683c41143SJohn Baldwin 				    type, rid, start, end, count, flags);
228783c41143SJohn Baldwin 				if (r != NULL)
228883c41143SJohn Baldwin 					break;
228983c41143SJohn Baldwin 			}
229083c41143SJohn Baldwin 		}
229183c41143SJohn Baldwin 		if (pcib_grow_window(sc, &sc->mem, type, start, end, count,
229283c41143SJohn Baldwin 		    flags & ~RF_PREFETCHABLE) == 0)
229383c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->mem, child, type,
229483c41143SJohn Baldwin 			    rid, start, end, count, flags);
229583c41143SJohn Baldwin 		break;
229683c41143SJohn Baldwin 	default:
229783c41143SJohn Baldwin 		return (bus_generic_alloc_resource(dev, child, type, rid,
229883c41143SJohn Baldwin 		    start, end, count, flags));
229983c41143SJohn Baldwin 	}
230083c41143SJohn Baldwin 
230183c41143SJohn Baldwin 	/*
230283c41143SJohn Baldwin 	 * If attempts to suballocate from the window fail but this is a
230383c41143SJohn Baldwin 	 * subtractive bridge, pass the request up the tree.
230483c41143SJohn Baldwin 	 */
230583c41143SJohn Baldwin 	if (sc->flags & PCIB_SUBTRACTIVE && r == NULL)
230683c41143SJohn Baldwin 		return (bus_generic_alloc_resource(dev, child, type, rid,
230783c41143SJohn Baldwin 		    start, end, count, flags));
230883c41143SJohn Baldwin 	return (r);
230983c41143SJohn Baldwin }
231083c41143SJohn Baldwin 
231183c41143SJohn Baldwin int
231283c41143SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
23132dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end)
231483c41143SJohn Baldwin {
231583c41143SJohn Baldwin 	struct pcib_softc *sc;
231683c41143SJohn Baldwin 
231783c41143SJohn Baldwin 	sc = device_get_softc(bus);
231883c41143SJohn Baldwin 	if (pcib_is_resource_managed(sc, type, r))
231983c41143SJohn Baldwin 		return (rman_adjust_resource(r, start, end));
232083c41143SJohn Baldwin 	return (bus_generic_adjust_resource(bus, child, type, r, start, end));
232183c41143SJohn Baldwin }
232283c41143SJohn Baldwin 
232383c41143SJohn Baldwin int
232483c41143SJohn Baldwin pcib_release_resource(device_t dev, device_t child, int type, int rid,
232583c41143SJohn Baldwin     struct resource *r)
232683c41143SJohn Baldwin {
232783c41143SJohn Baldwin 	struct pcib_softc *sc;
232883c41143SJohn Baldwin 	int error;
232983c41143SJohn Baldwin 
233083c41143SJohn Baldwin 	sc = device_get_softc(dev);
233183c41143SJohn Baldwin 	if (pcib_is_resource_managed(sc, type, r)) {
233283c41143SJohn Baldwin 		if (rman_get_flags(r) & RF_ACTIVE) {
233383c41143SJohn Baldwin 			error = bus_deactivate_resource(child, type, rid, r);
233483c41143SJohn Baldwin 			if (error)
233583c41143SJohn Baldwin 				return (error);
233683c41143SJohn Baldwin 		}
233783c41143SJohn Baldwin 		return (rman_release_resource(r));
233883c41143SJohn Baldwin 	}
233983c41143SJohn Baldwin 	return (bus_generic_release_resource(dev, child, type, rid, r));
234083c41143SJohn Baldwin }
234183c41143SJohn Baldwin #else
2342bb0d0a8eSMike Smith /*
2343bb0d0a8eSMike Smith  * We have to trap resource allocation requests and ensure that the bridge
2344bb0d0a8eSMike Smith  * is set up to, or capable of handling them.
2345bb0d0a8eSMike Smith  */
23466f0d5884SJohn Baldwin struct resource *
2347bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
23482dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
2349bb0d0a8eSMike Smith {
2350bb0d0a8eSMike Smith 	struct pcib_softc	*sc = device_get_softc(dev);
235126043836SJohn Baldwin 	const char *name, *suffix;
2352a8b354a8SWarner Losh 	int ok;
2353bb0d0a8eSMike Smith 
2354bb0d0a8eSMike Smith 	/*
2355bb0d0a8eSMike Smith 	 * Fail the allocation for this range if it's not supported.
2356bb0d0a8eSMike Smith 	 */
235726043836SJohn Baldwin 	name = device_get_nameunit(child);
235826043836SJohn Baldwin 	if (name == NULL) {
235926043836SJohn Baldwin 		name = "";
236026043836SJohn Baldwin 		suffix = "";
236126043836SJohn Baldwin 	} else
236226043836SJohn Baldwin 		suffix = " ";
2363bb0d0a8eSMike Smith 	switch (type) {
2364bb0d0a8eSMike Smith 	case SYS_RES_IOPORT:
2365a8b354a8SWarner Losh 		ok = 0;
2366e4b59fc5SWarner Losh 		if (!pcib_is_io_open(sc))
2367e4b59fc5SWarner Losh 			break;
2368a8b354a8SWarner Losh 		ok = (start >= sc->iobase && end <= sc->iolimit);
2369d98d9b12SMarcel Moolenaar 
2370d98d9b12SMarcel Moolenaar 		/*
2371d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA I/O addresses when the
2372d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
2373d98d9b12SMarcel Moolenaar 		 */
2374d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_ioport_range(start, end))
2375d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
2376d98d9b12SMarcel Moolenaar 
2377e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
2378a8b354a8SWarner Losh 			if (!ok) {
237912b8c86eSWarner Losh 				if (start < sc->iobase)
238012b8c86eSWarner Losh 					start = sc->iobase;
238112b8c86eSWarner Losh 				if (end > sc->iolimit)
238212b8c86eSWarner Losh 					end = sc->iolimit;
23832daa7a07SWarner Losh 				if (start < end)
23842daa7a07SWarner Losh 					ok = 1;
2385a8b354a8SWarner Losh 			}
23861c54ff33SMatthew N. Dodd 		} else {
2387e4b59fc5SWarner Losh 			ok = 1;
23889dffe835SWarner Losh #if 0
2389795dceffSWarner Losh 			/*
2390795dceffSWarner Losh 			 * If we overlap with the subtractive range, then
2391795dceffSWarner Losh 			 * pick the upper range to use.
2392795dceffSWarner Losh 			 */
2393795dceffSWarner Losh 			if (start < sc->iolimit && end > sc->iobase)
2394795dceffSWarner Losh 				start = sc->iolimit + 1;
23959dffe835SWarner Losh #endif
239612b8c86eSWarner Losh 		}
2397a8b354a8SWarner Losh 		if (end < start) {
2398da1b038aSJustin Hibbits 			device_printf(dev, "ioport: end (%jx) < start (%jx)\n",
23992daa7a07SWarner Losh 			    end, start);
2400a8b354a8SWarner Losh 			start = 0;
2401a8b354a8SWarner Losh 			end = 0;
2402a8b354a8SWarner Losh 			ok = 0;
2403a8b354a8SWarner Losh 		}
2404a8b354a8SWarner Losh 		if (!ok) {
240526043836SJohn Baldwin 			device_printf(dev, "%s%srequested unsupported I/O "
2406da1b038aSJustin Hibbits 			    "range 0x%jx-0x%jx (decoding 0x%x-0x%x)\n",
240726043836SJohn Baldwin 			    name, suffix, start, end, sc->iobase, sc->iolimit);
2408bb0d0a8eSMike Smith 			return (NULL);
2409bb0d0a8eSMike Smith 		}
24104fa59183SMike Smith 		if (bootverbose)
24112daa7a07SWarner Losh 			device_printf(dev,
2412da1b038aSJustin Hibbits 			    "%s%srequested I/O range 0x%jx-0x%jx: in range\n",
241326043836SJohn Baldwin 			    name, suffix, start, end);
2414bb0d0a8eSMike Smith 		break;
2415bb0d0a8eSMike Smith 
2416bb0d0a8eSMike Smith 	case SYS_RES_MEMORY:
2417a8b354a8SWarner Losh 		ok = 0;
2418a8b354a8SWarner Losh 		if (pcib_is_nonprefetch_open(sc))
2419a8b354a8SWarner Losh 			ok = ok || (start >= sc->membase && end <= sc->memlimit);
2420a8b354a8SWarner Losh 		if (pcib_is_prefetch_open(sc))
2421a8b354a8SWarner Losh 			ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
2422d98d9b12SMarcel Moolenaar 
2423d98d9b12SMarcel Moolenaar 		/*
2424d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA memory addresses when the
2425d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
2426d98d9b12SMarcel Moolenaar 		 */
2427d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_memory_range(start, end))
2428d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
2429d98d9b12SMarcel Moolenaar 
2430e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
2431a8b354a8SWarner Losh 			if (!ok) {
2432a8b354a8SWarner Losh 				ok = 1;
2433a8b354a8SWarner Losh 				if (flags & RF_PREFETCHABLE) {
2434a8b354a8SWarner Losh 					if (pcib_is_prefetch_open(sc)) {
2435a8b354a8SWarner Losh 						if (start < sc->pmembase)
2436a8b354a8SWarner Losh 							start = sc->pmembase;
2437a8b354a8SWarner Losh 						if (end > sc->pmemlimit)
2438a8b354a8SWarner Losh 							end = sc->pmemlimit;
2439a8b354a8SWarner Losh 					} else {
2440a8b354a8SWarner Losh 						ok = 0;
2441a8b354a8SWarner Losh 					}
2442a8b354a8SWarner Losh 				} else {	/* non-prefetchable */
2443a8b354a8SWarner Losh 					if (pcib_is_nonprefetch_open(sc)) {
2444a8b354a8SWarner Losh 						if (start < sc->membase)
244512b8c86eSWarner Losh 							start = sc->membase;
244612b8c86eSWarner Losh 						if (end > sc->memlimit)
244712b8c86eSWarner Losh 							end = sc->memlimit;
24481c54ff33SMatthew N. Dodd 					} else {
2449a8b354a8SWarner Losh 						ok = 0;
2450a8b354a8SWarner Losh 					}
2451a8b354a8SWarner Losh 				}
2452a8b354a8SWarner Losh 			}
2453a8b354a8SWarner Losh 		} else if (!ok) {
2454e4b59fc5SWarner Losh 			ok = 1;	/* subtractive bridge: always ok */
24559dffe835SWarner Losh #if 0
2456a8b354a8SWarner Losh 			if (pcib_is_nonprefetch_open(sc)) {
2457795dceffSWarner Losh 				if (start < sc->memlimit && end > sc->membase)
2458795dceffSWarner Losh 					start = sc->memlimit + 1;
2459a8b354a8SWarner Losh 			}
2460a8b354a8SWarner Losh 			if (pcib_is_prefetch_open(sc)) {
2461795dceffSWarner Losh 				if (start < sc->pmemlimit && end > sc->pmembase)
2462795dceffSWarner Losh 					start = sc->pmemlimit + 1;
24631c54ff33SMatthew N. Dodd 			}
24649dffe835SWarner Losh #endif
246512b8c86eSWarner Losh 		}
2466a8b354a8SWarner Losh 		if (end < start) {
2467da1b038aSJustin Hibbits 			device_printf(dev, "memory: end (%jx) < start (%jx)\n",
24682daa7a07SWarner Losh 			    end, start);
2469a8b354a8SWarner Losh 			start = 0;
2470a8b354a8SWarner Losh 			end = 0;
2471a8b354a8SWarner Losh 			ok = 0;
2472a8b354a8SWarner Losh 		}
2473a8b354a8SWarner Losh 		if (!ok && bootverbose)
247434428485SWarner Losh 			device_printf(dev,
2475da1b038aSJustin Hibbits 			    "%s%srequested unsupported memory range %#jx-%#jx "
2476b0a2d4b8SWarner Losh 			    "(decoding %#jx-%#jx, %#jx-%#jx)\n",
247726043836SJohn Baldwin 			    name, suffix, start, end,
2478b0a2d4b8SWarner Losh 			    (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
2479b0a2d4b8SWarner Losh 			    (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
2480a8b354a8SWarner Losh 		if (!ok)
2481bb0d0a8eSMike Smith 			return (NULL);
24824fa59183SMike Smith 		if (bootverbose)
248326043836SJohn Baldwin 			device_printf(dev,"%s%srequested memory range "
2484da1b038aSJustin Hibbits 			    "0x%jx-0x%jx: good\n",
248526043836SJohn Baldwin 			    name, suffix, start, end);
24864fa59183SMike Smith 		break;
24874fa59183SMike Smith 
2488bb0d0a8eSMike Smith 	default:
24894fa59183SMike Smith 		break;
2490bb0d0a8eSMike Smith 	}
2491bb0d0a8eSMike Smith 	/*
2492bb0d0a8eSMike Smith 	 * Bridge is OK decoding this resource, so pass it up.
2493bb0d0a8eSMike Smith 	 */
24942daa7a07SWarner Losh 	return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
24952daa7a07SWarner Losh 	    count, flags));
2496bb0d0a8eSMike Smith }
249783c41143SJohn Baldwin #endif
2498bb0d0a8eSMike Smith 
2499bb0d0a8eSMike Smith /*
250055d3ea17SRyan Stone  * If ARI is enabled on this downstream port, translate the function number
250155d3ea17SRyan Stone  * to the non-ARI slot/function.  The downstream port will convert it back in
250255d3ea17SRyan Stone  * hardware.  If ARI is not enabled slot and func are not modified.
250355d3ea17SRyan Stone  */
250455d3ea17SRyan Stone static __inline void
250555d3ea17SRyan Stone pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func)
250655d3ea17SRyan Stone {
250755d3ea17SRyan Stone 	struct pcib_softc *sc;
250855d3ea17SRyan Stone 	int ari_func;
250955d3ea17SRyan Stone 
251055d3ea17SRyan Stone 	sc = device_get_softc(pcib);
251155d3ea17SRyan Stone 	ari_func = *func;
251255d3ea17SRyan Stone 
251355d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
251455d3ea17SRyan Stone 		KASSERT(*slot == 0,
251555d3ea17SRyan Stone 		    ("Non-zero slot number with ARI enabled!"));
251655d3ea17SRyan Stone 		*slot = PCIE_ARI_SLOT(ari_func);
251755d3ea17SRyan Stone 		*func = PCIE_ARI_FUNC(ari_func);
251855d3ea17SRyan Stone 	}
251955d3ea17SRyan Stone }
252055d3ea17SRyan Stone 
252155d3ea17SRyan Stone 
252255d3ea17SRyan Stone static void
252355d3ea17SRyan Stone pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos)
252455d3ea17SRyan Stone {
252555d3ea17SRyan Stone 	uint32_t ctl2;
252655d3ea17SRyan Stone 
252755d3ea17SRyan Stone 	ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4);
252855d3ea17SRyan Stone 	ctl2 |= PCIEM_CTL2_ARI;
252955d3ea17SRyan Stone 	pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4);
253055d3ea17SRyan Stone 
253155d3ea17SRyan Stone 	sc->flags |= PCIB_ENABLE_ARI;
253255d3ea17SRyan Stone }
253355d3ea17SRyan Stone 
253455d3ea17SRyan Stone /*
2535bb0d0a8eSMike Smith  * PCIB interface.
2536bb0d0a8eSMike Smith  */
25376f0d5884SJohn Baldwin int
2538bb0d0a8eSMike Smith pcib_maxslots(device_t dev)
2539bb0d0a8eSMike Smith {
25404fa59183SMike Smith 	return (PCI_SLOTMAX);
2541bb0d0a8eSMike Smith }
2542bb0d0a8eSMike Smith 
254355d3ea17SRyan Stone static int
254455d3ea17SRyan Stone pcib_ari_maxslots(device_t dev)
254555d3ea17SRyan Stone {
254655d3ea17SRyan Stone 	struct pcib_softc *sc;
254755d3ea17SRyan Stone 
254855d3ea17SRyan Stone 	sc = device_get_softc(dev);
254955d3ea17SRyan Stone 
255055d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI)
255155d3ea17SRyan Stone 		return (PCIE_ARI_SLOTMAX);
255255d3ea17SRyan Stone 	else
255355d3ea17SRyan Stone 		return (PCI_SLOTMAX);
255455d3ea17SRyan Stone }
255555d3ea17SRyan Stone 
255655d3ea17SRyan Stone static int
255755d3ea17SRyan Stone pcib_ari_maxfuncs(device_t dev)
255855d3ea17SRyan Stone {
255955d3ea17SRyan Stone 	struct pcib_softc *sc;
256055d3ea17SRyan Stone 
256155d3ea17SRyan Stone 	sc = device_get_softc(dev);
256255d3ea17SRyan Stone 
256355d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI)
256455d3ea17SRyan Stone 		return (PCIE_ARI_FUNCMAX);
256555d3ea17SRyan Stone 	else
256655d3ea17SRyan Stone 		return (PCI_FUNCMAX);
256755d3ea17SRyan Stone }
256855d3ea17SRyan Stone 
25692397d2d8SRyan Stone static void
25702397d2d8SRyan Stone pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot,
25712397d2d8SRyan Stone     int *func)
25722397d2d8SRyan Stone {
25732397d2d8SRyan Stone 	struct pcib_softc *sc;
25742397d2d8SRyan Stone 
25752397d2d8SRyan Stone 	sc = device_get_softc(pcib);
25762397d2d8SRyan Stone 
25772397d2d8SRyan Stone 	*bus = PCI_RID2BUS(rid);
25782397d2d8SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
25792397d2d8SRyan Stone 		*slot = PCIE_ARI_RID2SLOT(rid);
25802397d2d8SRyan Stone 		*func = PCIE_ARI_RID2FUNC(rid);
25812397d2d8SRyan Stone 	} else {
25822397d2d8SRyan Stone 		*slot = PCI_RID2SLOT(rid);
25832397d2d8SRyan Stone 		*func = PCI_RID2FUNC(rid);
25842397d2d8SRyan Stone 	}
25852397d2d8SRyan Stone }
25862397d2d8SRyan Stone 
2587bb0d0a8eSMike Smith /*
2588bb0d0a8eSMike Smith  * Since we are a child of a PCI bus, its parent must support the pcib interface.
2589bb0d0a8eSMike Smith  */
259055d3ea17SRyan Stone static uint32_t
2591795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
2592bb0d0a8eSMike Smith {
259382cb5c3bSJohn Baldwin #ifdef PCI_HP
259482cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
259555d3ea17SRyan Stone 
259682cb5c3bSJohn Baldwin 	sc = device_get_softc(dev);
259782cb5c3bSJohn Baldwin 	if (!pcib_present(sc)) {
259882cb5c3bSJohn Baldwin 		switch (width) {
259982cb5c3bSJohn Baldwin 		case 2:
260082cb5c3bSJohn Baldwin 			return (0xffff);
260182cb5c3bSJohn Baldwin 		case 1:
260282cb5c3bSJohn Baldwin 			return (0xff);
260382cb5c3bSJohn Baldwin 		default:
260482cb5c3bSJohn Baldwin 			return (0xffffffff);
260582cb5c3bSJohn Baldwin 		}
260682cb5c3bSJohn Baldwin 	}
260782cb5c3bSJohn Baldwin #endif
260855d3ea17SRyan Stone 	pcib_xlate_ari(dev, b, &s, &f);
260955d3ea17SRyan Stone 	return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s,
261055d3ea17SRyan Stone 	    f, reg, width));
2611bb0d0a8eSMike Smith }
2612bb0d0a8eSMike Smith 
261355d3ea17SRyan Stone static void
2614795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
2615bb0d0a8eSMike Smith {
261682cb5c3bSJohn Baldwin #ifdef PCI_HP
261782cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
261855d3ea17SRyan Stone 
261982cb5c3bSJohn Baldwin 	sc = device_get_softc(dev);
262082cb5c3bSJohn Baldwin 	if (!pcib_present(sc))
262182cb5c3bSJohn Baldwin 		return;
262282cb5c3bSJohn Baldwin #endif
262355d3ea17SRyan Stone 	pcib_xlate_ari(dev, b, &s, &f);
262455d3ea17SRyan Stone 	PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f,
262555d3ea17SRyan Stone 	    reg, val, width);
2626bb0d0a8eSMike Smith }
2627bb0d0a8eSMike Smith 
2628bb0d0a8eSMike Smith /*
2629bb0d0a8eSMike Smith  * Route an interrupt across a PCI bridge.
2630bb0d0a8eSMike Smith  */
26312c2d1d07SBenno Rice int
2632bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin)
2633bb0d0a8eSMike Smith {
2634bb0d0a8eSMike Smith     device_t	bus;
2635bb0d0a8eSMike Smith     int		parent_intpin;
2636bb0d0a8eSMike Smith     int		intnum;
2637bb0d0a8eSMike Smith 
2638bb0d0a8eSMike Smith     /*
2639bb0d0a8eSMike Smith      *
2640bb0d0a8eSMike Smith      * The PCI standard defines a swizzle of the child-side device/intpin to
2641bb0d0a8eSMike Smith      * the parent-side intpin as follows.
2642bb0d0a8eSMike Smith      *
2643bb0d0a8eSMike Smith      * device = device on child bus
2644bb0d0a8eSMike Smith      * child_intpin = intpin on child bus slot (0-3)
2645bb0d0a8eSMike Smith      * parent_intpin = intpin on parent bus slot (0-3)
2646bb0d0a8eSMike Smith      *
2647bb0d0a8eSMike Smith      * parent_intpin = (device + child_intpin) % 4
2648bb0d0a8eSMike Smith      */
2649cdc95e1bSBernd Walter     parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
2650bb0d0a8eSMike Smith 
2651bb0d0a8eSMike Smith     /*
2652bb0d0a8eSMike Smith      * Our parent is a PCI bus.  Its parent must export the pcib interface
2653bb0d0a8eSMike Smith      * which includes the ability to route interrupts.
2654bb0d0a8eSMike Smith      */
2655bb0d0a8eSMike Smith     bus = device_get_parent(pcib);
2656bb0d0a8eSMike Smith     intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
265739981fedSJohn Baldwin     if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
2658c6a121abSJohn Baldwin 	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
2659c6a121abSJohn Baldwin 	    pci_get_slot(dev), 'A' + pin - 1, intnum);
26608046c4b9SMike Smith     }
2661bb0d0a8eSMike Smith     return(intnum);
2662bb0d0a8eSMike Smith }
2663b173edafSJohn Baldwin 
2664e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
26659bf4c9c1SJohn Baldwin int
26669bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
26679bf4c9c1SJohn Baldwin {
2668bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
26699bf4c9c1SJohn Baldwin 	device_t bus;
26709bf4c9c1SJohn Baldwin 
267122bf1c7fSJohn Baldwin 	if (sc->flags & PCIB_DISABLE_MSI)
267222bf1c7fSJohn Baldwin 		return (ENXIO);
26739bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
26749bf4c9c1SJohn Baldwin 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
26759bf4c9c1SJohn Baldwin 	    irqs));
26769bf4c9c1SJohn Baldwin }
26779bf4c9c1SJohn Baldwin 
2678e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */
26799bf4c9c1SJohn Baldwin int
26809bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
26819bf4c9c1SJohn Baldwin {
26829bf4c9c1SJohn Baldwin 	device_t bus;
26839bf4c9c1SJohn Baldwin 
26849bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
26859bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
26869bf4c9c1SJohn Baldwin }
26879bf4c9c1SJohn Baldwin 
26889bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */
26899bf4c9c1SJohn Baldwin int
2690e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
26919bf4c9c1SJohn Baldwin {
2692bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
26939bf4c9c1SJohn Baldwin 	device_t bus;
26949bf4c9c1SJohn Baldwin 
269568e9cbd3SMarius Strobl 	if (sc->flags & PCIB_DISABLE_MSIX)
269622bf1c7fSJohn Baldwin 		return (ENXIO);
26979bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
2698e706f7f0SJohn Baldwin 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
26995fe82bcaSJohn Baldwin }
27005fe82bcaSJohn Baldwin 
27019bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */
27029bf4c9c1SJohn Baldwin int
27039bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq)
27049bf4c9c1SJohn Baldwin {
27059bf4c9c1SJohn Baldwin 	device_t bus;
27069bf4c9c1SJohn Baldwin 
27079bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
27089bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
27099bf4c9c1SJohn Baldwin }
27109bf4c9c1SJohn Baldwin 
2711e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */
2712e706f7f0SJohn Baldwin int
2713e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
2714e706f7f0SJohn Baldwin     uint32_t *data)
2715e706f7f0SJohn Baldwin {
2716e706f7f0SJohn Baldwin 	device_t bus;
27174522ac77SLuoqi Chen 	int error;
2718e706f7f0SJohn Baldwin 
2719e706f7f0SJohn Baldwin 	bus = device_get_parent(pcib);
27204522ac77SLuoqi Chen 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
27214522ac77SLuoqi Chen 	if (error)
27224522ac77SLuoqi Chen 		return (error);
27234522ac77SLuoqi Chen 
27244522ac77SLuoqi Chen 	pci_ht_map_msi(pcib, *addr);
27254522ac77SLuoqi Chen 	return (0);
2726e706f7f0SJohn Baldwin }
2727e706f7f0SJohn Baldwin 
272862508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */
272962508c53SJohn Baldwin int
273062508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
273162508c53SJohn Baldwin {
273262508c53SJohn Baldwin 	device_t bus;
273362508c53SJohn Baldwin 
273462508c53SJohn Baldwin 	bus = device_get_parent(pcib);
273562508c53SJohn Baldwin 	return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate));
273662508c53SJohn Baldwin }
27375605a99eSRyan Stone 
27382397d2d8SRyan Stone static int
27392397d2d8SRyan Stone pcib_ari_enabled(device_t pcib)
27402397d2d8SRyan Stone {
27412397d2d8SRyan Stone 	struct pcib_softc *sc;
27422397d2d8SRyan Stone 
27432397d2d8SRyan Stone 	sc = device_get_softc(pcib);
27442397d2d8SRyan Stone 
27452397d2d8SRyan Stone 	return ((sc->flags & PCIB_ENABLE_ARI) != 0);
27462397d2d8SRyan Stone }
27472397d2d8SRyan Stone 
2748d7be980dSAndrew Turner static int
2749d7be980dSAndrew Turner pcib_ari_get_id(device_t pcib, device_t dev, enum pci_id_type type,
2750d7be980dSAndrew Turner     uintptr_t *id)
275155d3ea17SRyan Stone {
275255d3ea17SRyan Stone 	struct pcib_softc *sc;
27531e43b18cSAndrew Turner 	device_t bus_dev;
275455d3ea17SRyan Stone 	uint8_t bus, slot, func;
275555d3ea17SRyan Stone 
27561e43b18cSAndrew Turner 	if (type != PCI_ID_RID) {
27571e43b18cSAndrew Turner 		bus_dev = device_get_parent(pcib);
27581e43b18cSAndrew Turner 		return (PCIB_GET_ID(device_get_parent(bus_dev), dev, type, id));
27591e43b18cSAndrew Turner 	}
2760d7be980dSAndrew Turner 
276155d3ea17SRyan Stone 	sc = device_get_softc(pcib);
276255d3ea17SRyan Stone 
276355d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
276455d3ea17SRyan Stone 		bus = pci_get_bus(dev);
276555d3ea17SRyan Stone 		func = pci_get_function(dev);
276655d3ea17SRyan Stone 
2767d7be980dSAndrew Turner 		*id = (PCI_ARI_RID(bus, func));
276855d3ea17SRyan Stone 	} else {
276955d3ea17SRyan Stone 		bus = pci_get_bus(dev);
277055d3ea17SRyan Stone 		slot = pci_get_slot(dev);
277155d3ea17SRyan Stone 		func = pci_get_function(dev);
277255d3ea17SRyan Stone 
2773d7be980dSAndrew Turner 		*id = (PCI_RID(bus, slot, func));
277455d3ea17SRyan Stone 	}
2775d7be980dSAndrew Turner 
2776d7be980dSAndrew Turner 	return (0);
277755d3ea17SRyan Stone }
277855d3ea17SRyan Stone 
277955d3ea17SRyan Stone /*
278055d3ea17SRyan Stone  * Check that the downstream port (pcib) and the endpoint device (dev) both
278155d3ea17SRyan Stone  * support ARI.  If so, enable it and return 0, otherwise return an error.
278255d3ea17SRyan Stone  */
278355d3ea17SRyan Stone static int
278455d3ea17SRyan Stone pcib_try_enable_ari(device_t pcib, device_t dev)
278555d3ea17SRyan Stone {
278655d3ea17SRyan Stone 	struct pcib_softc *sc;
278755d3ea17SRyan Stone 	int error;
278855d3ea17SRyan Stone 	uint32_t cap2;
278955d3ea17SRyan Stone 	int ari_cap_off;
279055d3ea17SRyan Stone 	uint32_t ari_ver;
279155d3ea17SRyan Stone 	uint32_t pcie_pos;
279255d3ea17SRyan Stone 
279355d3ea17SRyan Stone 	sc = device_get_softc(pcib);
279455d3ea17SRyan Stone 
279555d3ea17SRyan Stone 	/*
279655d3ea17SRyan Stone 	 * ARI is controlled in a register in the PCIe capability structure.
279755d3ea17SRyan Stone 	 * If the downstream port does not have the PCIe capability structure
279855d3ea17SRyan Stone 	 * then it does not support ARI.
279955d3ea17SRyan Stone 	 */
280055d3ea17SRyan Stone 	error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos);
280155d3ea17SRyan Stone 	if (error != 0)
280255d3ea17SRyan Stone 		return (ENODEV);
280355d3ea17SRyan Stone 
280455d3ea17SRyan Stone 	/* Check that the PCIe port advertises ARI support. */
280555d3ea17SRyan Stone 	cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4);
280655d3ea17SRyan Stone 	if (!(cap2 & PCIEM_CAP2_ARI))
280755d3ea17SRyan Stone 		return (ENODEV);
280855d3ea17SRyan Stone 
280955d3ea17SRyan Stone 	/*
281055d3ea17SRyan Stone 	 * Check that the endpoint device advertises ARI support via the ARI
281155d3ea17SRyan Stone 	 * extended capability structure.
281255d3ea17SRyan Stone 	 */
281355d3ea17SRyan Stone 	error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off);
281455d3ea17SRyan Stone 	if (error != 0)
281555d3ea17SRyan Stone 		return (ENODEV);
281655d3ea17SRyan Stone 
281755d3ea17SRyan Stone 	/*
281855d3ea17SRyan Stone 	 * Finally, check that the endpoint device supports the same version
281955d3ea17SRyan Stone 	 * of ARI that we do.
282055d3ea17SRyan Stone 	 */
282155d3ea17SRyan Stone 	ari_ver = pci_read_config(dev, ari_cap_off, 4);
282255d3ea17SRyan Stone 	if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) {
282355d3ea17SRyan Stone 		if (bootverbose)
282455d3ea17SRyan Stone 			device_printf(pcib,
282555d3ea17SRyan Stone 			    "Unsupported version of ARI (%d) detected\n",
282655d3ea17SRyan Stone 			    PCI_EXTCAP_VER(ari_ver));
282755d3ea17SRyan Stone 
282855d3ea17SRyan Stone 		return (ENXIO);
282955d3ea17SRyan Stone 	}
283055d3ea17SRyan Stone 
283155d3ea17SRyan Stone 	pcib_enable_ari(sc, pcie_pos);
283255d3ea17SRyan Stone 
283355d3ea17SRyan Stone 	return (0);
283455d3ea17SRyan Stone }
2835*4cb67729SWarner Losh 
2836*4cb67729SWarner Losh /*
2837*4cb67729SWarner Losh  * Pass the request to use this PCI feature up the tree. Either there's a
2838*4cb67729SWarner Losh  * firmware like ACPI that's using this feature that will approve (or deny) the
2839*4cb67729SWarner Losh  * request to take it over, or the platform has no such firmware, in which case
2840*4cb67729SWarner Losh  * the request will be approved. If the request is approved, the OS is expected
2841*4cb67729SWarner Losh  * to make use of the feature or render it harmless.
2842*4cb67729SWarner Losh  */
2843*4cb67729SWarner Losh static int
2844*4cb67729SWarner Losh pcib_request_feature(device_t pcib, device_t dev, enum pci_feature feature)
2845*4cb67729SWarner Losh {
2846*4cb67729SWarner Losh 	device_t bus;
2847*4cb67729SWarner Losh 
2848*4cb67729SWarner Losh 	/*
2849*4cb67729SWarner Losh 	 * Our parent is necessarily a pci bus. Its parent will either be
2850*4cb67729SWarner Losh 	 * another pci bridge (which passes it up) or a host bridge that can
2851*4cb67729SWarner Losh 	 * approve or reject the request.
2852*4cb67729SWarner Losh 	 */
2853*4cb67729SWarner Losh 	bus = device_get_parent(pcib);
2854*4cb67729SWarner Losh 	return (PCIB_REQUEST_FEATURE(device_get_parent(bus), dev, feature));
2855*4cb67729SWarner Losh }
2856