xref: /freebsd/sys/dev/pci/pci_pci.c (revision a58536b91ae3931d222c3e4f1a949ff4a4927fb2)
1bb0d0a8eSMike Smith /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3718cf2ccSPedro F. Giffuni  *
4bb0d0a8eSMike Smith  * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
5bb0d0a8eSMike Smith  * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
6bb0d0a8eSMike Smith  * Copyright (c) 2000 BSDi
7bb0d0a8eSMike Smith  * All rights reserved.
8bb0d0a8eSMike Smith  *
9bb0d0a8eSMike Smith  * Redistribution and use in source and binary forms, with or without
10bb0d0a8eSMike Smith  * modification, are permitted provided that the following conditions
11bb0d0a8eSMike Smith  * are met:
12bb0d0a8eSMike Smith  * 1. Redistributions of source code must retain the above copyright
13bb0d0a8eSMike Smith  *    notice, this list of conditions and the following disclaimer.
14bb0d0a8eSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
15bb0d0a8eSMike Smith  *    notice, this list of conditions and the following disclaimer in the
16bb0d0a8eSMike Smith  *    documentation and/or other materials provided with the distribution.
17bb0d0a8eSMike Smith  * 3. The name of the author may not be used to endorse or promote products
18bb0d0a8eSMike Smith  *    derived from this software without specific prior written permission.
19bb0d0a8eSMike Smith  *
20bb0d0a8eSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21bb0d0a8eSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22bb0d0a8eSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23bb0d0a8eSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24bb0d0a8eSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25bb0d0a8eSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26bb0d0a8eSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27bb0d0a8eSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28bb0d0a8eSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29bb0d0a8eSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30bb0d0a8eSMike Smith  * SUCH DAMAGE.
31bb0d0a8eSMike Smith  */
32bb0d0a8eSMike Smith 
33aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
34aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
35aad970f1SDavid E. O'Brien 
36bb0d0a8eSMike Smith /*
37bb0d0a8eSMike Smith  * PCI:PCI bridge support.
38bb0d0a8eSMike Smith  */
39bb0d0a8eSMike Smith 
4082cb5c3bSJohn Baldwin #include "opt_pci.h"
4182cb5c3bSJohn Baldwin 
42bb0d0a8eSMike Smith #include <sys/param.h>
43bb0d0a8eSMike Smith #include <sys/bus.h>
4483c41143SJohn Baldwin #include <sys/kernel.h>
45e2e050c8SConrad Meyer #include <sys/lock.h>
4683c41143SJohn Baldwin #include <sys/malloc.h>
4783c41143SJohn Baldwin #include <sys/module.h>
48e2e050c8SConrad Meyer #include <sys/mutex.h>
495db2a4a8SKonstantin Belousov #include <sys/pciio.h>
50a8b354a8SWarner Losh #include <sys/rman.h>
511c54ff33SMatthew N. Dodd #include <sys/sysctl.h>
5283c41143SJohn Baldwin #include <sys/systm.h>
5382cb5c3bSJohn Baldwin #include <sys/taskqueue.h>
54bb0d0a8eSMike Smith 
5538d8c994SWarner Losh #include <dev/pci/pcivar.h>
5638d8c994SWarner Losh #include <dev/pci/pcireg.h>
5762508c53SJohn Baldwin #include <dev/pci/pci_private.h>
5838d8c994SWarner Losh #include <dev/pci/pcib_private.h>
59bb0d0a8eSMike Smith 
60bb0d0a8eSMike Smith #include "pcib_if.h"
61bb0d0a8eSMike Smith 
62bb0d0a8eSMike Smith static int		pcib_probe(device_t dev);
63e36af292SJung-uk Kim static int		pcib_suspend(device_t dev);
64e36af292SJung-uk Kim static int		pcib_resume(device_t dev);
6562508c53SJohn Baldwin static int		pcib_power_for_sleep(device_t pcib, device_t dev,
6662508c53SJohn Baldwin 			    int *pstate);
67d7be980dSAndrew Turner static int		pcib_ari_get_id(device_t pcib, device_t dev,
68d7be980dSAndrew Turner     enum pci_id_type type, uintptr_t *id);
6955d3ea17SRyan Stone static uint32_t		pcib_read_config(device_t dev, u_int b, u_int s,
7055d3ea17SRyan Stone     u_int f, u_int reg, int width);
7155d3ea17SRyan Stone static void		pcib_write_config(device_t dev, u_int b, u_int s,
7255d3ea17SRyan Stone     u_int f, u_int reg, uint32_t val, int width);
7355d3ea17SRyan Stone static int		pcib_ari_maxslots(device_t dev);
7455d3ea17SRyan Stone static int		pcib_ari_maxfuncs(device_t dev);
7555d3ea17SRyan Stone static int		pcib_try_enable_ari(device_t pcib, device_t dev);
762397d2d8SRyan Stone static int		pcib_ari_enabled(device_t pcib);
772397d2d8SRyan Stone static void		pcib_ari_decode_rid(device_t pcib, uint16_t rid,
782397d2d8SRyan Stone 			    int *bus, int *slot, int *func);
7982cb5c3bSJohn Baldwin #ifdef PCI_HP
80fa3b03d3SAlexander Motin static void		pcib_pcie_ab_timeout(void *arg, int pending);
81fa3b03d3SAlexander Motin static void		pcib_pcie_cc_timeout(void *arg, int pending);
82fa3b03d3SAlexander Motin static void		pcib_pcie_dll_timeout(void *arg, int pending);
8382cb5c3bSJohn Baldwin #endif
841ffd07bdSJohn Baldwin static int		pcib_request_feature_default(device_t pcib, device_t dev,
854cb67729SWarner Losh 			    enum pci_feature feature);
865db2a4a8SKonstantin Belousov static int		pcib_reset_child(device_t dev, device_t child, int flags);
87bb0d0a8eSMike Smith 
88bb0d0a8eSMike Smith static device_method_t pcib_methods[] = {
89bb0d0a8eSMike Smith     /* Device interface */
90bb0d0a8eSMike Smith     DEVMETHOD(device_probe,		pcib_probe),
91bb0d0a8eSMike Smith     DEVMETHOD(device_attach,		pcib_attach),
926f33eaa5SJohn Baldwin     DEVMETHOD(device_detach,		pcib_detach),
93bb0d0a8eSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
94e36af292SJung-uk Kim     DEVMETHOD(device_suspend,		pcib_suspend),
95e36af292SJung-uk Kim     DEVMETHOD(device_resume,		pcib_resume),
96bb0d0a8eSMike Smith 
97bb0d0a8eSMike Smith     /* Bus interface */
9882cb5c3bSJohn Baldwin     DEVMETHOD(bus_child_present,	pcib_child_present),
99bb0d0a8eSMike Smith     DEVMETHOD(bus_read_ivar,		pcib_read_ivar),
100bb0d0a8eSMike Smith     DEVMETHOD(bus_write_ivar,		pcib_write_ivar),
101bb0d0a8eSMike Smith     DEVMETHOD(bus_alloc_resource,	pcib_alloc_resource),
10283c41143SJohn Baldwin #ifdef NEW_PCIB
10383c41143SJohn Baldwin     DEVMETHOD(bus_adjust_resource,	pcib_adjust_resource),
10483c41143SJohn Baldwin     DEVMETHOD(bus_release_resource,	pcib_release_resource),
10583c41143SJohn Baldwin #else
106d2c9344fSJohn Baldwin     DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
107bb0d0a8eSMike Smith     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
10883c41143SJohn Baldwin #endif
109bb0d0a8eSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
110bb0d0a8eSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
111bb0d0a8eSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
112bb0d0a8eSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
1135db2a4a8SKonstantin Belousov     DEVMETHOD(bus_reset_child,		pcib_reset_child),
114bb0d0a8eSMike Smith 
115bb0d0a8eSMike Smith     /* pcib interface */
11655d3ea17SRyan Stone     DEVMETHOD(pcib_maxslots,		pcib_ari_maxslots),
11755d3ea17SRyan Stone     DEVMETHOD(pcib_maxfuncs,		pcib_ari_maxfuncs),
118bb0d0a8eSMike Smith     DEVMETHOD(pcib_read_config,		pcib_read_config),
119bb0d0a8eSMike Smith     DEVMETHOD(pcib_write_config,	pcib_write_config),
120bb0d0a8eSMike Smith     DEVMETHOD(pcib_route_interrupt,	pcib_route_interrupt),
1219bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_alloc_msi,		pcib_alloc_msi),
1229bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msi,		pcib_release_msi),
1239bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_alloc_msix,		pcib_alloc_msix),
1249bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msix,	pcib_release_msix),
125e706f7f0SJohn Baldwin     DEVMETHOD(pcib_map_msi,		pcib_map_msi),
12662508c53SJohn Baldwin     DEVMETHOD(pcib_power_for_sleep,	pcib_power_for_sleep),
127d7be980dSAndrew Turner     DEVMETHOD(pcib_get_id,		pcib_ari_get_id),
12855d3ea17SRyan Stone     DEVMETHOD(pcib_try_enable_ari,	pcib_try_enable_ari),
1292397d2d8SRyan Stone     DEVMETHOD(pcib_ari_enabled,		pcib_ari_enabled),
1302397d2d8SRyan Stone     DEVMETHOD(pcib_decode_rid,		pcib_ari_decode_rid),
1311ffd07bdSJohn Baldwin     DEVMETHOD(pcib_request_feature,	pcib_request_feature_default),
132bb0d0a8eSMike Smith 
1334b7ec270SMarius Strobl     DEVMETHOD_END
134bb0d0a8eSMike Smith };
135bb0d0a8eSMike Smith 
13604dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc));
13797a41013SJohn Baldwin EARLY_DRIVER_MODULE(pcib, pci, pcib_driver, NULL, NULL, BUS_PASS_BUS);
138bb0d0a8eSMike Smith 
1396ca2d094SBjoern A. Zeeb #if defined(NEW_PCIB) || defined(PCI_HP)
1400070c94bSJohn Baldwin SYSCTL_DECL(_hw_pci);
1416ca2d094SBjoern A. Zeeb #endif
1420070c94bSJohn Baldwin 
1436ca2d094SBjoern A. Zeeb #ifdef NEW_PCIB
1440070c94bSJohn Baldwin static int pci_clear_pcib;
1450070c94bSJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0,
1460070c94bSJohn Baldwin     "Clear firmware-assigned resources for PCI-PCI bridge I/O windows.");
14783c41143SJohn Baldwin 
14883c41143SJohn Baldwin /*
14926245980SJessica Clarke  * Get the corresponding window if this resource from a child device was
15026245980SJessica Clarke  * sub-allocated from one of our window resource managers.
15126245980SJessica Clarke  */
15226245980SJessica Clarke static struct pcib_window *
15326245980SJessica Clarke pcib_get_resource_window(struct pcib_softc *sc, int type, struct resource *r)
15426245980SJessica Clarke {
15526245980SJessica Clarke 	switch (type) {
15626245980SJessica Clarke 	case SYS_RES_IOPORT:
15726245980SJessica Clarke 		if (rman_is_region_manager(r, &sc->io.rman))
15826245980SJessica Clarke 			return (&sc->io);
15926245980SJessica Clarke 		break;
16026245980SJessica Clarke 	case SYS_RES_MEMORY:
16126245980SJessica Clarke 		/* Prefetchable resources may live in either memory rman. */
16226245980SJessica Clarke 		if (rman_get_flags(r) & RF_PREFETCHABLE &&
16326245980SJessica Clarke 		    rman_is_region_manager(r, &sc->pmem.rman))
16426245980SJessica Clarke 			return (&sc->pmem);
16526245980SJessica Clarke 		if (rman_is_region_manager(r, &sc->mem.rman))
16626245980SJessica Clarke 			return (&sc->mem);
16726245980SJessica Clarke 		break;
16826245980SJessica Clarke 	}
16926245980SJessica Clarke 	return (NULL);
17026245980SJessica Clarke }
17126245980SJessica Clarke 
17226245980SJessica Clarke /*
17383c41143SJohn Baldwin  * Is a resource from a child device sub-allocated from one of our
17483c41143SJohn Baldwin  * resource managers?
17583c41143SJohn Baldwin  */
17683c41143SJohn Baldwin static int
17783c41143SJohn Baldwin pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r)
17883c41143SJohn Baldwin {
17983c41143SJohn Baldwin 
1804edef187SJohn Baldwin #ifdef PCI_RES_BUS
18126245980SJessica Clarke 	if (type == PCI_RES_BUS)
1824edef187SJohn Baldwin 		return (rman_is_region_manager(r, &sc->bus.rman));
1834edef187SJohn Baldwin #endif
18426245980SJessica Clarke 	return (pcib_get_resource_window(sc, type, r) != NULL);
18583c41143SJohn Baldwin }
18683c41143SJohn Baldwin 
18783c41143SJohn Baldwin static int
18883c41143SJohn Baldwin pcib_is_window_open(struct pcib_window *pw)
18983c41143SJohn Baldwin {
19083c41143SJohn Baldwin 
19183c41143SJohn Baldwin 	return (pw->valid && pw->base < pw->limit);
19283c41143SJohn Baldwin }
19383c41143SJohn Baldwin 
19483c41143SJohn Baldwin /*
19583c41143SJohn Baldwin  * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and
19683c41143SJohn Baldwin  * handle for the resource, we could pass RF_ACTIVE up to the PCI bus
19783c41143SJohn Baldwin  * when allocating the resource windows and rely on the PCI bus driver
19883c41143SJohn Baldwin  * to do this for us.
19983c41143SJohn Baldwin  */
20083c41143SJohn Baldwin static void
20183c41143SJohn Baldwin pcib_activate_window(struct pcib_softc *sc, int type)
20283c41143SJohn Baldwin {
20383c41143SJohn Baldwin 
20483c41143SJohn Baldwin 	PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type);
20583c41143SJohn Baldwin }
20683c41143SJohn Baldwin 
20783c41143SJohn Baldwin static void
20883c41143SJohn Baldwin pcib_write_windows(struct pcib_softc *sc, int mask)
20983c41143SJohn Baldwin {
21083c41143SJohn Baldwin 	device_t dev;
21183c41143SJohn Baldwin 	uint32_t val;
21283c41143SJohn Baldwin 
21383c41143SJohn Baldwin 	dev = sc->dev;
21483c41143SJohn Baldwin 	if (sc->io.valid && mask & WIN_IO) {
21583c41143SJohn Baldwin 		val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
21683c41143SJohn Baldwin 		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
21783c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOBASEH_1,
21883c41143SJohn Baldwin 			    sc->io.base >> 16, 2);
21983c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOLIMITH_1,
22083c41143SJohn Baldwin 			    sc->io.limit >> 16, 2);
22183c41143SJohn Baldwin 		}
22283c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1);
22383c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1);
22483c41143SJohn Baldwin 	}
22583c41143SJohn Baldwin 
22683c41143SJohn Baldwin 	if (mask & WIN_MEM) {
22783c41143SJohn Baldwin 		pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2);
22883c41143SJohn Baldwin 		pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2);
22983c41143SJohn Baldwin 	}
23083c41143SJohn Baldwin 
23183c41143SJohn Baldwin 	if (sc->pmem.valid && mask & WIN_PMEM) {
23283c41143SJohn Baldwin 		val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
23383c41143SJohn Baldwin 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
23483c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMBASEH_1,
23583c41143SJohn Baldwin 			    sc->pmem.base >> 32, 4);
23683c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMLIMITH_1,
23783c41143SJohn Baldwin 			    sc->pmem.limit >> 32, 4);
23883c41143SJohn Baldwin 		}
23983c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2);
24083c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2);
24183c41143SJohn Baldwin 	}
24283c41143SJohn Baldwin }
24383c41143SJohn Baldwin 
244c825d4dcSJohn Baldwin /*
245c825d4dcSJohn Baldwin  * This is used to reject I/O port allocations that conflict with an
246c825d4dcSJohn Baldwin  * ISA alias range.
247c825d4dcSJohn Baldwin  */
248c825d4dcSJohn Baldwin static int
2492dd1bdf1SJustin Hibbits pcib_is_isa_range(struct pcib_softc *sc, rman_res_t start, rman_res_t end,
2502dd1bdf1SJustin Hibbits     rman_res_t count)
251c825d4dcSJohn Baldwin {
2522dd1bdf1SJustin Hibbits 	rman_res_t next_alias;
253c825d4dcSJohn Baldwin 
254c825d4dcSJohn Baldwin 	if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE))
255c825d4dcSJohn Baldwin 		return (0);
256c825d4dcSJohn Baldwin 
257c825d4dcSJohn Baldwin 	/* Only check fixed ranges for overlap. */
258c825d4dcSJohn Baldwin 	if (start + count - 1 != end)
259c825d4dcSJohn Baldwin 		return (0);
260c825d4dcSJohn Baldwin 
261c825d4dcSJohn Baldwin 	/* ISA aliases are only in the lower 64KB of I/O space. */
262c825d4dcSJohn Baldwin 	if (start >= 65536)
263c825d4dcSJohn Baldwin 		return (0);
264c825d4dcSJohn Baldwin 
265c825d4dcSJohn Baldwin 	/* Check for overlap with 0x000 - 0x0ff as a special case. */
266c825d4dcSJohn Baldwin 	if (start < 0x100)
267c825d4dcSJohn Baldwin 		goto alias;
268c825d4dcSJohn Baldwin 
269c825d4dcSJohn Baldwin 	/*
270c825d4dcSJohn Baldwin 	 * If the start address is an alias, the range is an alias.
271c825d4dcSJohn Baldwin 	 * Otherwise, compute the start of the next alias range and
272c825d4dcSJohn Baldwin 	 * check if it is before the end of the candidate range.
273c825d4dcSJohn Baldwin 	 */
274c825d4dcSJohn Baldwin 	if ((start & 0x300) != 0)
275c825d4dcSJohn Baldwin 		goto alias;
276c825d4dcSJohn Baldwin 	next_alias = (start & ~0x3fful) | 0x100;
277c825d4dcSJohn Baldwin 	if (next_alias <= end)
278c825d4dcSJohn Baldwin 		goto alias;
279c825d4dcSJohn Baldwin 	return (0);
280c825d4dcSJohn Baldwin 
281c825d4dcSJohn Baldwin alias:
282c825d4dcSJohn Baldwin 	if (bootverbose)
283c825d4dcSJohn Baldwin 		device_printf(sc->dev,
284da1b038aSJustin Hibbits 		    "I/O range %#jx-%#jx overlaps with an ISA alias\n", start,
285c825d4dcSJohn Baldwin 		    end);
286c825d4dcSJohn Baldwin 	return (1);
287c825d4dcSJohn Baldwin }
288c825d4dcSJohn Baldwin 
289c825d4dcSJohn Baldwin static void
290c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res,
291c825d4dcSJohn Baldwin     int count)
292c825d4dcSJohn Baldwin {
293c825d4dcSJohn Baldwin 	struct resource **newarray;
294c825d4dcSJohn Baldwin 	int error, i;
295c825d4dcSJohn Baldwin 
296c825d4dcSJohn Baldwin 	newarray = malloc(sizeof(struct resource *) * (w->count + count),
297c825d4dcSJohn Baldwin 	    M_DEVBUF, M_WAITOK);
298c825d4dcSJohn Baldwin 	if (w->res != NULL)
299c825d4dcSJohn Baldwin 		bcopy(w->res, newarray, sizeof(struct resource *) * w->count);
300c825d4dcSJohn Baldwin 	bcopy(res, newarray + w->count, sizeof(struct resource *) * count);
301c825d4dcSJohn Baldwin 	free(w->res, M_DEVBUF);
302c825d4dcSJohn Baldwin 	w->res = newarray;
303c825d4dcSJohn Baldwin 	w->count += count;
304c825d4dcSJohn Baldwin 
305c825d4dcSJohn Baldwin 	for (i = 0; i < count; i++) {
306c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, rman_get_start(res[i]),
307c825d4dcSJohn Baldwin 		    rman_get_end(res[i]));
308c825d4dcSJohn Baldwin 		if (error)
309c825d4dcSJohn Baldwin 			panic("Failed to add resource to rman");
310c825d4dcSJohn Baldwin 	}
311c825d4dcSJohn Baldwin }
312c825d4dcSJohn Baldwin 
3132dd1bdf1SJustin Hibbits typedef void (nonisa_callback)(rman_res_t start, rman_res_t end, void *arg);
314c825d4dcSJohn Baldwin 
315c825d4dcSJohn Baldwin static void
3162dd1bdf1SJustin Hibbits pcib_walk_nonisa_ranges(rman_res_t start, rman_res_t end, nonisa_callback *cb,
317c825d4dcSJohn Baldwin     void *arg)
318c825d4dcSJohn Baldwin {
3192dd1bdf1SJustin Hibbits 	rman_res_t next_end;
320c825d4dcSJohn Baldwin 
321c825d4dcSJohn Baldwin 	/*
322c825d4dcSJohn Baldwin 	 * If start is within an ISA alias range, move up to the start
323c825d4dcSJohn Baldwin 	 * of the next non-alias range.  As a special case, addresses
324c825d4dcSJohn Baldwin 	 * in the range 0x000 - 0x0ff should also be skipped since
325c825d4dcSJohn Baldwin 	 * those are used for various system I/O devices in ISA
326c825d4dcSJohn Baldwin 	 * systems.
327c825d4dcSJohn Baldwin 	 */
328c825d4dcSJohn Baldwin 	if (start <= 65535) {
329c825d4dcSJohn Baldwin 		if (start < 0x100 || (start & 0x300) != 0) {
330c825d4dcSJohn Baldwin 			start &= ~0x3ff;
331c825d4dcSJohn Baldwin 			start += 0x400;
332c825d4dcSJohn Baldwin 		}
333c825d4dcSJohn Baldwin 	}
334c825d4dcSJohn Baldwin 
335c825d4dcSJohn Baldwin 	/* ISA aliases are only in the lower 64KB of I/O space. */
336c825d4dcSJohn Baldwin 	while (start <= MIN(end, 65535)) {
337c825d4dcSJohn Baldwin 		next_end = MIN(start | 0xff, end);
338c825d4dcSJohn Baldwin 		cb(start, next_end, arg);
339c825d4dcSJohn Baldwin 		start += 0x400;
340c825d4dcSJohn Baldwin 	}
341c825d4dcSJohn Baldwin 
342c825d4dcSJohn Baldwin 	if (start <= end)
343c825d4dcSJohn Baldwin 		cb(start, end, arg);
344c825d4dcSJohn Baldwin }
345c825d4dcSJohn Baldwin 
346c825d4dcSJohn Baldwin static void
3472dd1bdf1SJustin Hibbits count_ranges(rman_res_t start, rman_res_t end, void *arg)
348c825d4dcSJohn Baldwin {
349c825d4dcSJohn Baldwin 	int *countp;
350c825d4dcSJohn Baldwin 
351c825d4dcSJohn Baldwin 	countp = arg;
352c825d4dcSJohn Baldwin 	(*countp)++;
353c825d4dcSJohn Baldwin }
354c825d4dcSJohn Baldwin 
355c825d4dcSJohn Baldwin struct alloc_state {
356c825d4dcSJohn Baldwin 	struct resource **res;
357c825d4dcSJohn Baldwin 	struct pcib_softc *sc;
358c825d4dcSJohn Baldwin 	int count, error;
359c825d4dcSJohn Baldwin };
360c825d4dcSJohn Baldwin 
361c825d4dcSJohn Baldwin static void
3622dd1bdf1SJustin Hibbits alloc_ranges(rman_res_t start, rman_res_t end, void *arg)
363c825d4dcSJohn Baldwin {
364c825d4dcSJohn Baldwin 	struct alloc_state *as;
365c825d4dcSJohn Baldwin 	struct pcib_window *w;
366c825d4dcSJohn Baldwin 	int rid;
367c825d4dcSJohn Baldwin 
368c825d4dcSJohn Baldwin 	as = arg;
369c825d4dcSJohn Baldwin 	if (as->error != 0)
370c825d4dcSJohn Baldwin 		return;
371c825d4dcSJohn Baldwin 
372c825d4dcSJohn Baldwin 	w = &as->sc->io;
373c825d4dcSJohn Baldwin 	rid = w->reg;
374c825d4dcSJohn Baldwin 	if (bootverbose)
375c825d4dcSJohn Baldwin 		device_printf(as->sc->dev,
376da1b038aSJustin Hibbits 		    "allocating non-ISA range %#jx-%#jx\n", start, end);
377c825d4dcSJohn Baldwin 	as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT,
378c825d4dcSJohn Baldwin 	    &rid, start, end, end - start + 1, 0);
379c825d4dcSJohn Baldwin 	if (as->res[as->count] == NULL)
380c825d4dcSJohn Baldwin 		as->error = ENXIO;
381c825d4dcSJohn Baldwin 	else
382c825d4dcSJohn Baldwin 		as->count++;
383c825d4dcSJohn Baldwin }
384c825d4dcSJohn Baldwin 
385c825d4dcSJohn Baldwin static int
3862dd1bdf1SJustin Hibbits pcib_alloc_nonisa_ranges(struct pcib_softc *sc, rman_res_t start, rman_res_t end)
387c825d4dcSJohn Baldwin {
388c825d4dcSJohn Baldwin 	struct alloc_state as;
389c825d4dcSJohn Baldwin 	int i, new_count;
390c825d4dcSJohn Baldwin 
391c825d4dcSJohn Baldwin 	/* First, see how many ranges we need. */
392c825d4dcSJohn Baldwin 	new_count = 0;
393c825d4dcSJohn Baldwin 	pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count);
394c825d4dcSJohn Baldwin 
395c825d4dcSJohn Baldwin 	/* Second, allocate the ranges. */
396c825d4dcSJohn Baldwin 	as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF,
397c825d4dcSJohn Baldwin 	    M_WAITOK);
398c825d4dcSJohn Baldwin 	as.sc = sc;
399c825d4dcSJohn Baldwin 	as.count = 0;
400c825d4dcSJohn Baldwin 	as.error = 0;
401c825d4dcSJohn Baldwin 	pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as);
402c825d4dcSJohn Baldwin 	if (as.error != 0) {
403c825d4dcSJohn Baldwin 		for (i = 0; i < as.count; i++)
404c825d4dcSJohn Baldwin 			bus_release_resource(sc->dev, SYS_RES_IOPORT,
405c825d4dcSJohn Baldwin 			    sc->io.reg, as.res[i]);
406c825d4dcSJohn Baldwin 		free(as.res, M_DEVBUF);
407c825d4dcSJohn Baldwin 		return (as.error);
408c825d4dcSJohn Baldwin 	}
409c825d4dcSJohn Baldwin 	KASSERT(as.count == new_count, ("%s: count mismatch", __func__));
410c825d4dcSJohn Baldwin 
411c825d4dcSJohn Baldwin 	/* Third, add the ranges to the window. */
412c825d4dcSJohn Baldwin 	pcib_add_window_resources(&sc->io, as.res, as.count);
413c825d4dcSJohn Baldwin 	free(as.res, M_DEVBUF);
414c825d4dcSJohn Baldwin 	return (0);
415c825d4dcSJohn Baldwin }
416c825d4dcSJohn Baldwin 
41783c41143SJohn Baldwin static void
41883c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type,
41983c41143SJohn Baldwin     int flags, pci_addr_t max_address)
42083c41143SJohn Baldwin {
421c825d4dcSJohn Baldwin 	struct resource *res;
42283c41143SJohn Baldwin 	char buf[64];
42383c41143SJohn Baldwin 	int error, rid;
42483c41143SJohn Baldwin 
42589977ce2SJustin Hibbits 	if (max_address != (rman_res_t)max_address)
426534ccd7bSJustin Hibbits 		max_address = ~0;
42783c41143SJohn Baldwin 	w->rman.rm_start = 0;
42883c41143SJohn Baldwin 	w->rman.rm_end = max_address;
42983c41143SJohn Baldwin 	w->rman.rm_type = RMAN_ARRAY;
43083c41143SJohn Baldwin 	snprintf(buf, sizeof(buf), "%s %s window",
43183c41143SJohn Baldwin 	    device_get_nameunit(sc->dev), w->name);
43283c41143SJohn Baldwin 	w->rman.rm_descr = strdup(buf, M_DEVBUF);
43383c41143SJohn Baldwin 	error = rman_init(&w->rman);
43483c41143SJohn Baldwin 	if (error)
43583c41143SJohn Baldwin 		panic("Failed to initialize %s %s rman",
43683c41143SJohn Baldwin 		    device_get_nameunit(sc->dev), w->name);
43783c41143SJohn Baldwin 
43883c41143SJohn Baldwin 	if (!pcib_is_window_open(w))
43983c41143SJohn Baldwin 		return;
44083c41143SJohn Baldwin 
44183c41143SJohn Baldwin 	if (w->base > max_address || w->limit > max_address) {
44283c41143SJohn Baldwin 		device_printf(sc->dev,
44383c41143SJohn Baldwin 		    "initial %s window has too many bits, ignoring\n", w->name);
44483c41143SJohn Baldwin 		return;
44583c41143SJohn Baldwin 	}
446c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE)
447c825d4dcSJohn Baldwin 		(void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit);
448c825d4dcSJohn Baldwin 	else {
44983c41143SJohn Baldwin 		rid = w->reg;
450c825d4dcSJohn Baldwin 		res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit,
45183c41143SJohn Baldwin 		    w->limit - w->base + 1, flags);
452c825d4dcSJohn Baldwin 		if (res != NULL)
453c825d4dcSJohn Baldwin 			pcib_add_window_resources(w, &res, 1);
454c825d4dcSJohn Baldwin 	}
45583c41143SJohn Baldwin 	if (w->res == NULL) {
45683c41143SJohn Baldwin 		device_printf(sc->dev,
45783c41143SJohn Baldwin 		    "failed to allocate initial %s window: %#jx-%#jx\n",
45883c41143SJohn Baldwin 		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
45983c41143SJohn Baldwin 		w->base = max_address;
46083c41143SJohn Baldwin 		w->limit = 0;
46183c41143SJohn Baldwin 		pcib_write_windows(sc, w->mask);
46283c41143SJohn Baldwin 		return;
46383c41143SJohn Baldwin 	}
46483c41143SJohn Baldwin 	pcib_activate_window(sc, type);
46583c41143SJohn Baldwin }
46683c41143SJohn Baldwin 
46783c41143SJohn Baldwin /*
46883c41143SJohn Baldwin  * Initialize I/O windows.
46983c41143SJohn Baldwin  */
47083c41143SJohn Baldwin static void
47183c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc)
47283c41143SJohn Baldwin {
47383c41143SJohn Baldwin 	pci_addr_t max;
47483c41143SJohn Baldwin 	device_t dev;
47583c41143SJohn Baldwin 	uint32_t val;
47683c41143SJohn Baldwin 
47783c41143SJohn Baldwin 	dev = sc->dev;
47883c41143SJohn Baldwin 
4790070c94bSJohn Baldwin 	if (pci_clear_pcib) {
480809923caSJustin Hibbits 		pcib_bridge_init(dev);
4810070c94bSJohn Baldwin 	}
4820070c94bSJohn Baldwin 
48383c41143SJohn Baldwin 	/* Determine if the I/O port window is implemented. */
48483c41143SJohn Baldwin 	val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
48583c41143SJohn Baldwin 	if (val == 0) {
48683c41143SJohn Baldwin 		/*
48783c41143SJohn Baldwin 		 * If 'val' is zero, then only 16-bits of I/O space
48883c41143SJohn Baldwin 		 * are supported.
48983c41143SJohn Baldwin 		 */
49083c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
49183c41143SJohn Baldwin 		if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) {
49283c41143SJohn Baldwin 			sc->io.valid = 1;
49383c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOBASEL_1, 0, 1);
49483c41143SJohn Baldwin 		}
49583c41143SJohn Baldwin 	} else
49683c41143SJohn Baldwin 		sc->io.valid = 1;
49783c41143SJohn Baldwin 
49883c41143SJohn Baldwin 	/* Read the existing I/O port window. */
49983c41143SJohn Baldwin 	if (sc->io.valid) {
50083c41143SJohn Baldwin 		sc->io.reg = PCIR_IOBASEL_1;
50183c41143SJohn Baldwin 		sc->io.step = 12;
50283c41143SJohn Baldwin 		sc->io.mask = WIN_IO;
50383c41143SJohn Baldwin 		sc->io.name = "I/O port";
50483c41143SJohn Baldwin 		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
50583c41143SJohn Baldwin 			sc->io.base = PCI_PPBIOBASE(
50683c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOBASEH_1, 2), val);
50783c41143SJohn Baldwin 			sc->io.limit = PCI_PPBIOLIMIT(
50883c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITH_1, 2),
50983c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
51083c41143SJohn Baldwin 			max = 0xffffffff;
51183c41143SJohn Baldwin 		} else {
51283c41143SJohn Baldwin 			sc->io.base = PCI_PPBIOBASE(0, val);
51383c41143SJohn Baldwin 			sc->io.limit = PCI_PPBIOLIMIT(0,
51483c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
51583c41143SJohn Baldwin 			max = 0xffff;
51683c41143SJohn Baldwin 		}
51783c41143SJohn Baldwin 		pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max);
51883c41143SJohn Baldwin 	}
51983c41143SJohn Baldwin 
52083c41143SJohn Baldwin 	/* Read the existing memory window. */
52183c41143SJohn Baldwin 	sc->mem.valid = 1;
52283c41143SJohn Baldwin 	sc->mem.reg = PCIR_MEMBASE_1;
52383c41143SJohn Baldwin 	sc->mem.step = 20;
52483c41143SJohn Baldwin 	sc->mem.mask = WIN_MEM;
52583c41143SJohn Baldwin 	sc->mem.name = "memory";
52683c41143SJohn Baldwin 	sc->mem.base = PCI_PPBMEMBASE(0,
52783c41143SJohn Baldwin 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
52883c41143SJohn Baldwin 	sc->mem.limit = PCI_PPBMEMLIMIT(0,
52983c41143SJohn Baldwin 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
53083c41143SJohn Baldwin 	pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff);
53183c41143SJohn Baldwin 
53283c41143SJohn Baldwin 	/* Determine if the prefetchable memory window is implemented. */
53383c41143SJohn Baldwin 	val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
53483c41143SJohn Baldwin 	if (val == 0) {
53583c41143SJohn Baldwin 		/*
53683c41143SJohn Baldwin 		 * If 'val' is zero, then only 32-bits of memory space
53783c41143SJohn Baldwin 		 * are supported.
53883c41143SJohn Baldwin 		 */
53983c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
54083c41143SJohn Baldwin 		if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) {
54183c41143SJohn Baldwin 			sc->pmem.valid = 1;
54283c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMBASEL_1, 0, 2);
54383c41143SJohn Baldwin 		}
54483c41143SJohn Baldwin 	} else
54583c41143SJohn Baldwin 		sc->pmem.valid = 1;
54683c41143SJohn Baldwin 
54783c41143SJohn Baldwin 	/* Read the existing prefetchable memory window. */
54883c41143SJohn Baldwin 	if (sc->pmem.valid) {
54983c41143SJohn Baldwin 		sc->pmem.reg = PCIR_PMBASEL_1;
55083c41143SJohn Baldwin 		sc->pmem.step = 20;
55183c41143SJohn Baldwin 		sc->pmem.mask = WIN_PMEM;
55283c41143SJohn Baldwin 		sc->pmem.name = "prefetch";
55383c41143SJohn Baldwin 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
55483c41143SJohn Baldwin 			sc->pmem.base = PCI_PPBMEMBASE(
55583c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMBASEH_1, 4), val);
55683c41143SJohn Baldwin 			sc->pmem.limit = PCI_PPBMEMLIMIT(
55783c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITH_1, 4),
55883c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
55983c41143SJohn Baldwin 			max = 0xffffffffffffffff;
56083c41143SJohn Baldwin 		} else {
56183c41143SJohn Baldwin 			sc->pmem.base = PCI_PPBMEMBASE(0, val);
56283c41143SJohn Baldwin 			sc->pmem.limit = PCI_PPBMEMLIMIT(0,
56383c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
56483c41143SJohn Baldwin 			max = 0xffffffff;
56583c41143SJohn Baldwin 		}
56683c41143SJohn Baldwin 		pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY,
56783c41143SJohn Baldwin 		    RF_PREFETCHABLE, max);
56883c41143SJohn Baldwin 	}
56983c41143SJohn Baldwin }
57083c41143SJohn Baldwin 
5716f33eaa5SJohn Baldwin static void
5726f33eaa5SJohn Baldwin pcib_release_window(struct pcib_softc *sc, struct pcib_window *w, int type)
5736f33eaa5SJohn Baldwin {
5746f33eaa5SJohn Baldwin 	device_t dev;
5756f33eaa5SJohn Baldwin 	int error, i;
5766f33eaa5SJohn Baldwin 
5776f33eaa5SJohn Baldwin 	if (!w->valid)
5786f33eaa5SJohn Baldwin 		return;
5796f33eaa5SJohn Baldwin 
5806f33eaa5SJohn Baldwin 	dev = sc->dev;
5816f33eaa5SJohn Baldwin 	error = rman_fini(&w->rman);
5826f33eaa5SJohn Baldwin 	if (error) {
5836f33eaa5SJohn Baldwin 		device_printf(dev, "failed to release %s rman\n", w->name);
5846f33eaa5SJohn Baldwin 		return;
5856f33eaa5SJohn Baldwin 	}
5866f33eaa5SJohn Baldwin 	free(__DECONST(char *, w->rman.rm_descr), M_DEVBUF);
5876f33eaa5SJohn Baldwin 
5886f33eaa5SJohn Baldwin 	for (i = 0; i < w->count; i++) {
5896f33eaa5SJohn Baldwin 		error = bus_free_resource(dev, type, w->res[i]);
5906f33eaa5SJohn Baldwin 		if (error)
5916f33eaa5SJohn Baldwin 			device_printf(dev,
5926f33eaa5SJohn Baldwin 			    "failed to release %s resource: %d\n", w->name,
5936f33eaa5SJohn Baldwin 			    error);
5946f33eaa5SJohn Baldwin 	}
5956f33eaa5SJohn Baldwin 	free(w->res, M_DEVBUF);
5966f33eaa5SJohn Baldwin }
5976f33eaa5SJohn Baldwin 
5986f33eaa5SJohn Baldwin static void
5996f33eaa5SJohn Baldwin pcib_free_windows(struct pcib_softc *sc)
6006f33eaa5SJohn Baldwin {
6016f33eaa5SJohn Baldwin 
6026f33eaa5SJohn Baldwin 	pcib_release_window(sc, &sc->pmem, SYS_RES_MEMORY);
6036f33eaa5SJohn Baldwin 	pcib_release_window(sc, &sc->mem, SYS_RES_MEMORY);
6046f33eaa5SJohn Baldwin 	pcib_release_window(sc, &sc->io, SYS_RES_IOPORT);
6056f33eaa5SJohn Baldwin }
6066f33eaa5SJohn Baldwin 
6074edef187SJohn Baldwin #ifdef PCI_RES_BUS
6084edef187SJohn Baldwin /*
6094edef187SJohn Baldwin  * Allocate a suitable secondary bus for this bridge if needed and
6104edef187SJohn Baldwin  * initialize the resource manager for the secondary bus range.  Note
6114edef187SJohn Baldwin  * that the minimum count is a desired value and this may allocate a
6124edef187SJohn Baldwin  * smaller range.
6134edef187SJohn Baldwin  */
6144edef187SJohn Baldwin void
6154edef187SJohn Baldwin pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count)
6164edef187SJohn Baldwin {
6174edef187SJohn Baldwin 	char buf[64];
618ad6f36f8SJohn Baldwin 	int error, rid, sec_reg;
6194edef187SJohn Baldwin 
6204edef187SJohn Baldwin 	switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) {
6214edef187SJohn Baldwin 	case PCIM_HDRTYPE_BRIDGE:
622ad6f36f8SJohn Baldwin 		sec_reg = PCIR_SECBUS_1;
6234edef187SJohn Baldwin 		bus->sub_reg = PCIR_SUBBUS_1;
6244edef187SJohn Baldwin 		break;
6254edef187SJohn Baldwin 	case PCIM_HDRTYPE_CARDBUS:
626ad6f36f8SJohn Baldwin 		sec_reg = PCIR_SECBUS_2;
6274edef187SJohn Baldwin 		bus->sub_reg = PCIR_SUBBUS_2;
6284edef187SJohn Baldwin 		break;
6294edef187SJohn Baldwin 	default:
6304edef187SJohn Baldwin 		panic("not a PCI bridge");
6314edef187SJohn Baldwin 	}
632ad6f36f8SJohn Baldwin 	bus->sec = pci_read_config(dev, sec_reg, 1);
633ad6f36f8SJohn Baldwin 	bus->sub = pci_read_config(dev, bus->sub_reg, 1);
6344edef187SJohn Baldwin 	bus->dev = dev;
6354edef187SJohn Baldwin 	bus->rman.rm_start = 0;
6364edef187SJohn Baldwin 	bus->rman.rm_end = PCI_BUSMAX;
6374edef187SJohn Baldwin 	bus->rman.rm_type = RMAN_ARRAY;
6384edef187SJohn Baldwin 	snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev));
6394edef187SJohn Baldwin 	bus->rman.rm_descr = strdup(buf, M_DEVBUF);
6404edef187SJohn Baldwin 	error = rman_init(&bus->rman);
6414edef187SJohn Baldwin 	if (error)
6424edef187SJohn Baldwin 		panic("Failed to initialize %s bus number rman",
6434edef187SJohn Baldwin 		    device_get_nameunit(dev));
6444edef187SJohn Baldwin 
6454edef187SJohn Baldwin 	/*
6464edef187SJohn Baldwin 	 * Allocate a bus range.  This will return an existing bus range
6474edef187SJohn Baldwin 	 * if one exists, or a new bus range if one does not.
6484edef187SJohn Baldwin 	 */
6494edef187SJohn Baldwin 	rid = 0;
650c47476d7SJustin Hibbits 	bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid,
6514edef187SJohn Baldwin 	    min_count, 0);
6524edef187SJohn Baldwin 	if (bus->res == NULL) {
6534edef187SJohn Baldwin 		/*
6544edef187SJohn Baldwin 		 * Fall back to just allocating a range of a single bus
6554edef187SJohn Baldwin 		 * number.
6564edef187SJohn Baldwin 		 */
657c47476d7SJustin Hibbits 		bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid,
6584edef187SJohn Baldwin 		    1, 0);
6594edef187SJohn Baldwin 	} else if (rman_get_size(bus->res) < min_count)
6604edef187SJohn Baldwin 		/*
6614edef187SJohn Baldwin 		 * Attempt to grow the existing range to satisfy the
6624edef187SJohn Baldwin 		 * minimum desired count.
6634edef187SJohn Baldwin 		 */
6644edef187SJohn Baldwin 		(void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res,
6654edef187SJohn Baldwin 		    rman_get_start(bus->res), rman_get_start(bus->res) +
6664edef187SJohn Baldwin 		    min_count - 1);
6674edef187SJohn Baldwin 
6684edef187SJohn Baldwin 	/*
6694edef187SJohn Baldwin 	 * Add the initial resource to the rman.
6704edef187SJohn Baldwin 	 */
6714edef187SJohn Baldwin 	if (bus->res != NULL) {
6724edef187SJohn Baldwin 		error = rman_manage_region(&bus->rman, rman_get_start(bus->res),
6734edef187SJohn Baldwin 		    rman_get_end(bus->res));
6744edef187SJohn Baldwin 		if (error)
6754edef187SJohn Baldwin 			panic("Failed to add resource to rman");
6764edef187SJohn Baldwin 		bus->sec = rman_get_start(bus->res);
6774edef187SJohn Baldwin 		bus->sub = rman_get_end(bus->res);
6784edef187SJohn Baldwin 	}
6794edef187SJohn Baldwin }
6804edef187SJohn Baldwin 
6816f33eaa5SJohn Baldwin void
6826f33eaa5SJohn Baldwin pcib_free_secbus(device_t dev, struct pcib_secbus *bus)
6836f33eaa5SJohn Baldwin {
6846f33eaa5SJohn Baldwin 	int error;
6856f33eaa5SJohn Baldwin 
6866f33eaa5SJohn Baldwin 	error = rman_fini(&bus->rman);
6876f33eaa5SJohn Baldwin 	if (error) {
6886f33eaa5SJohn Baldwin 		device_printf(dev, "failed to release bus number rman\n");
6896f33eaa5SJohn Baldwin 		return;
6906f33eaa5SJohn Baldwin 	}
6916f33eaa5SJohn Baldwin 	free(__DECONST(char *, bus->rman.rm_descr), M_DEVBUF);
6926f33eaa5SJohn Baldwin 
6936f33eaa5SJohn Baldwin 	error = bus_free_resource(dev, PCI_RES_BUS, bus->res);
6946f33eaa5SJohn Baldwin 	if (error)
6956f33eaa5SJohn Baldwin 		device_printf(dev,
6966f33eaa5SJohn Baldwin 		    "failed to release bus numbers resource: %d\n", error);
6976f33eaa5SJohn Baldwin }
6986f33eaa5SJohn Baldwin 
6994edef187SJohn Baldwin static struct resource *
7004edef187SJohn Baldwin pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid,
7012dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
7024edef187SJohn Baldwin {
7034edef187SJohn Baldwin 	struct resource *res;
7044edef187SJohn Baldwin 
7054edef187SJohn Baldwin 	res = rman_reserve_resource(&bus->rman, start, end, count, flags,
7064edef187SJohn Baldwin 	    child);
7074edef187SJohn Baldwin 	if (res == NULL)
7084edef187SJohn Baldwin 		return (NULL);
7094edef187SJohn Baldwin 
7104edef187SJohn Baldwin 	if (bootverbose)
7114edef187SJohn Baldwin 		device_printf(bus->dev,
712da1b038aSJustin Hibbits 		    "allocated bus range (%ju-%ju) for rid %d of %s\n",
7134edef187SJohn Baldwin 		    rman_get_start(res), rman_get_end(res), *rid,
7144edef187SJohn Baldwin 		    pcib_child_name(child));
7154edef187SJohn Baldwin 	rman_set_rid(res, *rid);
7164edef187SJohn Baldwin 	return (res);
7174edef187SJohn Baldwin }
7184edef187SJohn Baldwin 
7194edef187SJohn Baldwin /*
7204edef187SJohn Baldwin  * Attempt to grow the secondary bus range.  This is much simpler than
7214edef187SJohn Baldwin  * for I/O windows as the range can only be grown by increasing
7224edef187SJohn Baldwin  * subbus.
7234edef187SJohn Baldwin  */
7244edef187SJohn Baldwin static int
7252dd1bdf1SJustin Hibbits pcib_grow_subbus(struct pcib_secbus *bus, rman_res_t new_end)
7264edef187SJohn Baldwin {
7272dd1bdf1SJustin Hibbits 	rman_res_t old_end;
7284edef187SJohn Baldwin 	int error;
7294edef187SJohn Baldwin 
7304edef187SJohn Baldwin 	old_end = rman_get_end(bus->res);
7314edef187SJohn Baldwin 	KASSERT(new_end > old_end, ("attempt to shrink subbus"));
7324edef187SJohn Baldwin 	error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res,
7334edef187SJohn Baldwin 	    rman_get_start(bus->res), new_end);
7344edef187SJohn Baldwin 	if (error)
7354edef187SJohn Baldwin 		return (error);
7364edef187SJohn Baldwin 	if (bootverbose)
737da1b038aSJustin Hibbits 		device_printf(bus->dev, "grew bus range to %ju-%ju\n",
7384edef187SJohn Baldwin 		    rman_get_start(bus->res), rman_get_end(bus->res));
7394edef187SJohn Baldwin 	error = rman_manage_region(&bus->rman, old_end + 1,
7404edef187SJohn Baldwin 	    rman_get_end(bus->res));
7414edef187SJohn Baldwin 	if (error)
7424edef187SJohn Baldwin 		panic("Failed to add resource to rman");
7434edef187SJohn Baldwin 	bus->sub = rman_get_end(bus->res);
7444edef187SJohn Baldwin 	pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1);
7454edef187SJohn Baldwin 	return (0);
7464edef187SJohn Baldwin }
7474edef187SJohn Baldwin 
7484edef187SJohn Baldwin struct resource *
7494edef187SJohn Baldwin pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid,
7502dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
7514edef187SJohn Baldwin {
7524edef187SJohn Baldwin 	struct resource *res;
7532dd1bdf1SJustin Hibbits 	rman_res_t start_free, end_free, new_end;
7544edef187SJohn Baldwin 
7554edef187SJohn Baldwin 	/*
7564edef187SJohn Baldwin 	 * First, see if the request can be satisified by the existing
7574edef187SJohn Baldwin 	 * bus range.
7584edef187SJohn Baldwin 	 */
7594edef187SJohn Baldwin 	res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags);
7604edef187SJohn Baldwin 	if (res != NULL)
7614edef187SJohn Baldwin 		return (res);
7624edef187SJohn Baldwin 
7634edef187SJohn Baldwin 	/*
7644edef187SJohn Baldwin 	 * Figure out a range to grow the bus range.  First, find the
7654edef187SJohn Baldwin 	 * first bus number after the last allocated bus in the rman and
7664edef187SJohn Baldwin 	 * enforce that as a minimum starting point for the range.
7674edef187SJohn Baldwin 	 */
7684edef187SJohn Baldwin 	if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 ||
7694edef187SJohn Baldwin 	    end_free != bus->sub)
7704edef187SJohn Baldwin 		start_free = bus->sub + 1;
7714edef187SJohn Baldwin 	if (start_free < start)
7724edef187SJohn Baldwin 		start_free = start;
7734edef187SJohn Baldwin 	new_end = start_free + count - 1;
7744edef187SJohn Baldwin 
7754edef187SJohn Baldwin 	/*
7764edef187SJohn Baldwin 	 * See if this new range would satisfy the request if it
7774edef187SJohn Baldwin 	 * succeeds.
7784edef187SJohn Baldwin 	 */
7794edef187SJohn Baldwin 	if (new_end > end)
7804edef187SJohn Baldwin 		return (NULL);
7814edef187SJohn Baldwin 
7824edef187SJohn Baldwin 	/* Finally, attempt to grow the existing resource. */
7834edef187SJohn Baldwin 	if (bootverbose) {
7844edef187SJohn Baldwin 		device_printf(bus->dev,
785da1b038aSJustin Hibbits 		    "attempting to grow bus range for %ju buses\n", count);
786da1b038aSJustin Hibbits 		printf("\tback candidate range: %ju-%ju\n", start_free,
7874edef187SJohn Baldwin 		    new_end);
7884edef187SJohn Baldwin 	}
7894edef187SJohn Baldwin 	if (pcib_grow_subbus(bus, new_end) == 0)
7904edef187SJohn Baldwin 		return (pcib_suballoc_bus(bus, child, rid, start, end, count,
7914edef187SJohn Baldwin 		    flags));
7924edef187SJohn Baldwin 	return (NULL);
7934edef187SJohn Baldwin }
7944edef187SJohn Baldwin #endif
7954edef187SJohn Baldwin 
79683c41143SJohn Baldwin #else
79783c41143SJohn Baldwin 
798bb0d0a8eSMike Smith /*
799b0a2d4b8SWarner Losh  * Is the prefetch window open (eg, can we allocate memory in it?)
800b0a2d4b8SWarner Losh  */
801b0a2d4b8SWarner Losh static int
802b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc)
803b0a2d4b8SWarner Losh {
804b0a2d4b8SWarner Losh 	return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
805b0a2d4b8SWarner Losh }
806b0a2d4b8SWarner Losh 
807b0a2d4b8SWarner Losh /*
808b0a2d4b8SWarner Losh  * Is the nonprefetch window open (eg, can we allocate memory in it?)
809b0a2d4b8SWarner Losh  */
810b0a2d4b8SWarner Losh static int
811b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc)
812b0a2d4b8SWarner Losh {
813b0a2d4b8SWarner Losh 	return (sc->membase > 0 && sc->membase < sc->memlimit);
814b0a2d4b8SWarner Losh }
815b0a2d4b8SWarner Losh 
816b0a2d4b8SWarner Losh /*
817b0a2d4b8SWarner Losh  * Is the io window open (eg, can we allocate ports in it?)
818b0a2d4b8SWarner Losh  */
819b0a2d4b8SWarner Losh static int
820b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc)
821b0a2d4b8SWarner Losh {
822b0a2d4b8SWarner Losh 	return (sc->iobase > 0 && sc->iobase < sc->iolimit);
823b0a2d4b8SWarner Losh }
824b0a2d4b8SWarner Losh 
825b0a2d4b8SWarner Losh /*
826e36af292SJung-uk Kim  * Get current I/O decode.
827e36af292SJung-uk Kim  */
828e36af292SJung-uk Kim static void
829e36af292SJung-uk Kim pcib_get_io_decode(struct pcib_softc *sc)
830e36af292SJung-uk Kim {
831e36af292SJung-uk Kim 	device_t	dev;
832e36af292SJung-uk Kim 	uint32_t	iolow;
833e36af292SJung-uk Kim 
834e36af292SJung-uk Kim 	dev = sc->dev;
835e36af292SJung-uk Kim 
836e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
837e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
838e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(
839e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow);
840e36af292SJung-uk Kim 	else
841e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(0, iolow);
842e36af292SJung-uk Kim 
843e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
844e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
845e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(
846e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow);
847e36af292SJung-uk Kim 	else
848e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(0, iolow);
849e36af292SJung-uk Kim }
850e36af292SJung-uk Kim 
851e36af292SJung-uk Kim /*
852e36af292SJung-uk Kim  * Get current memory decode.
853e36af292SJung-uk Kim  */
854e36af292SJung-uk Kim static void
855e36af292SJung-uk Kim pcib_get_mem_decode(struct pcib_softc *sc)
856e36af292SJung-uk Kim {
857e36af292SJung-uk Kim 	device_t	dev;
858e36af292SJung-uk Kim 	pci_addr_t	pmemlow;
859e36af292SJung-uk Kim 
860e36af292SJung-uk Kim 	dev = sc->dev;
861e36af292SJung-uk Kim 
862e36af292SJung-uk Kim 	sc->membase = PCI_PPBMEMBASE(0,
863e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
864e36af292SJung-uk Kim 	sc->memlimit = PCI_PPBMEMLIMIT(0,
865e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
866e36af292SJung-uk Kim 
867e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2);
868e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
869e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(
870e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow);
871e36af292SJung-uk Kim 	else
872e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(0, pmemlow);
873e36af292SJung-uk Kim 
874e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2);
875e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
876e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(
877e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow);
878e36af292SJung-uk Kim 	else
879e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow);
880e36af292SJung-uk Kim }
881e36af292SJung-uk Kim 
882e36af292SJung-uk Kim /*
883e36af292SJung-uk Kim  * Restore previous I/O decode.
884e36af292SJung-uk Kim  */
885e36af292SJung-uk Kim static void
886e36af292SJung-uk Kim pcib_set_io_decode(struct pcib_softc *sc)
887e36af292SJung-uk Kim {
888e36af292SJung-uk Kim 	device_t	dev;
889e36af292SJung-uk Kim 	uint32_t	iohi;
890e36af292SJung-uk Kim 
891e36af292SJung-uk Kim 	dev = sc->dev;
892e36af292SJung-uk Kim 
893e36af292SJung-uk Kim 	iohi = sc->iobase >> 16;
894e36af292SJung-uk Kim 	if (iohi > 0)
895e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2);
896e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1);
897e36af292SJung-uk Kim 
898e36af292SJung-uk Kim 	iohi = sc->iolimit >> 16;
899e36af292SJung-uk Kim 	if (iohi > 0)
900e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2);
901e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1);
902e36af292SJung-uk Kim }
903e36af292SJung-uk Kim 
904e36af292SJung-uk Kim /*
905e36af292SJung-uk Kim  * Restore previous memory decode.
906e36af292SJung-uk Kim  */
907e36af292SJung-uk Kim static void
908e36af292SJung-uk Kim pcib_set_mem_decode(struct pcib_softc *sc)
909e36af292SJung-uk Kim {
910e36af292SJung-uk Kim 	device_t	dev;
911e36af292SJung-uk Kim 	pci_addr_t	pmemhi;
912e36af292SJung-uk Kim 
913e36af292SJung-uk Kim 	dev = sc->dev;
914e36af292SJung-uk Kim 
915e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2);
916e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2);
917e36af292SJung-uk Kim 
918e36af292SJung-uk Kim 	pmemhi = sc->pmembase >> 32;
919e36af292SJung-uk Kim 	if (pmemhi > 0)
920e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4);
921e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2);
922e36af292SJung-uk Kim 
923e36af292SJung-uk Kim 	pmemhi = sc->pmemlimit >> 32;
924e36af292SJung-uk Kim 	if (pmemhi > 0)
925e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4);
926e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2);
927e36af292SJung-uk Kim }
92883c41143SJohn Baldwin #endif
929e36af292SJung-uk Kim 
93082cb5c3bSJohn Baldwin #ifdef PCI_HP
93182cb5c3bSJohn Baldwin /*
93282cb5c3bSJohn Baldwin  * PCI-express HotPlug support.
93382cb5c3bSJohn Baldwin  */
93425a57bd6SJohn Baldwin static int pci_enable_pcie_hp = 1;
93525a57bd6SJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_hp, CTLFLAG_RDTUN,
93625a57bd6SJohn Baldwin     &pci_enable_pcie_hp, 0,
93725a57bd6SJohn Baldwin     "Enable support for native PCI-express HotPlug.");
93825a57bd6SJohn Baldwin 
93912588ce0SAndriy Gapon TASKQUEUE_DEFINE_THREAD(pci_hp);
94012588ce0SAndriy Gapon 
94182cb5c3bSJohn Baldwin static void
94282cb5c3bSJohn Baldwin pcib_probe_hotplug(struct pcib_softc *sc)
94382cb5c3bSJohn Baldwin {
94482cb5c3bSJohn Baldwin 	device_t dev;
94537290148SEric van Gyzen 	uint32_t link_cap;
946991d431fSEric van Gyzen 	uint16_t link_sta, slot_sta;
94782cb5c3bSJohn Baldwin 
94825a57bd6SJohn Baldwin 	if (!pci_enable_pcie_hp)
94925a57bd6SJohn Baldwin 		return;
95025a57bd6SJohn Baldwin 
95182cb5c3bSJohn Baldwin 	dev = sc->dev;
95282cb5c3bSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, NULL) != 0)
95382cb5c3bSJohn Baldwin 		return;
95482cb5c3bSJohn Baldwin 
95582cb5c3bSJohn Baldwin 	if (!(pcie_read_config(dev, PCIER_FLAGS, 2) & PCIEM_FLAGS_SLOT))
95682cb5c3bSJohn Baldwin 		return;
95782cb5c3bSJohn Baldwin 
95882cb5c3bSJohn Baldwin 	sc->pcie_slot_cap = pcie_read_config(dev, PCIER_SLOT_CAP, 4);
95982cb5c3bSJohn Baldwin 
960991d431fSEric van Gyzen 	if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_HPC) == 0)
9612611037cSJohn Baldwin 		return;
96237290148SEric van Gyzen 	link_cap = pcie_read_config(dev, PCIER_LINK_CAP, 4);
96337290148SEric van Gyzen 	if ((link_cap & PCIEM_LINK_CAP_DL_ACTIVE) == 0)
9642ffb582aSJohn Baldwin 		return;
9652611037cSJohn Baldwin 
966991d431fSEric van Gyzen 	/*
967991d431fSEric van Gyzen 	 * Some devices report that they have an MRL when they actually
968991d431fSEric van Gyzen 	 * do not.  Since they always report that the MRL is open, child
969991d431fSEric van Gyzen 	 * devices would be ignored.  Try to detect these devices and
970991d431fSEric van Gyzen 	 * ignore their claim of HotPlug support.
971991d431fSEric van Gyzen 	 *
972991d431fSEric van Gyzen 	 * If there is an open MRL but the Data Link Layer is active,
973991d431fSEric van Gyzen 	 * the MRL is not real.
974991d431fSEric van Gyzen 	 */
97537290148SEric van Gyzen 	if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) != 0) {
976991d431fSEric van Gyzen 		link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
977991d431fSEric van Gyzen 		slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
978991d431fSEric van Gyzen 		if ((slot_sta & PCIEM_SLOT_STA_MRLSS) != 0 &&
979991d431fSEric van Gyzen 		    (link_sta & PCIEM_LINK_STA_DL_ACTIVE) != 0) {
980991d431fSEric van Gyzen 			return;
981991d431fSEric van Gyzen 		}
982991d431fSEric van Gyzen 	}
983991d431fSEric van Gyzen 
98428586889SWarner Losh 	/*
98528586889SWarner Losh 	 * Now that we're sure we want to do hot plug, ask the
98628586889SWarner Losh 	 * firmware, if any, if that's OK.
98728586889SWarner Losh 	 */
9881ffd07bdSJohn Baldwin 	if (pcib_request_feature(dev, PCI_FEATURE_HP) != 0) {
98928586889SWarner Losh 		if (bootverbose)
99028586889SWarner Losh 			device_printf(dev, "Unable to activate hot plug feature.\n");
99128586889SWarner Losh 		return;
99228586889SWarner Losh 	}
99328586889SWarner Losh 
99482cb5c3bSJohn Baldwin 	sc->flags |= PCIB_HOTPLUG;
99582cb5c3bSJohn Baldwin }
99682cb5c3bSJohn Baldwin 
99782cb5c3bSJohn Baldwin /*
99882cb5c3bSJohn Baldwin  * Send a HotPlug command to the slot control register.  If this slot
99907454911SJohn Baldwin  * uses command completion interrupts and a previous command is still
100007454911SJohn Baldwin  * in progress, then the command is dropped.  Once the previous
100107454911SJohn Baldwin  * command completes or times out, pcib_pcie_hotplug_update() will be
100207454911SJohn Baldwin  * invoked to post a new command based on the slot's state at that
100307454911SJohn Baldwin  * time.
100482cb5c3bSJohn Baldwin  */
100582cb5c3bSJohn Baldwin static void
100682cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(struct pcib_softc *sc, uint16_t val, uint16_t mask)
100782cb5c3bSJohn Baldwin {
100882cb5c3bSJohn Baldwin 	device_t dev;
100982cb5c3bSJohn Baldwin 	uint16_t ctl, new;
101082cb5c3bSJohn Baldwin 
101182cb5c3bSJohn Baldwin 	dev = sc->dev;
101282cb5c3bSJohn Baldwin 
101307454911SJohn Baldwin 	if (sc->flags & PCIB_HOTPLUG_CMD_PENDING)
101407454911SJohn Baldwin 		return;
101507454911SJohn Baldwin 
101682cb5c3bSJohn Baldwin 	ctl = pcie_read_config(dev, PCIER_SLOT_CTL, 2);
101782cb5c3bSJohn Baldwin 	new = (ctl & ~mask) | val;
101807454911SJohn Baldwin 	if (new == ctl)
101907454911SJohn Baldwin 		return;
1020991d431fSEric van Gyzen 	if (bootverbose)
1021991d431fSEric van Gyzen 		device_printf(dev, "HotPlug command: %04x -> %04x\n", ctl, new);
102207454911SJohn Baldwin 	pcie_write_config(dev, PCIER_SLOT_CTL, new, 2);
10236f33eaa5SJohn Baldwin 	if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS) &&
10246f33eaa5SJohn Baldwin 	    (ctl & new) & PCIEM_SLOT_CTL_CCIE) {
102582cb5c3bSJohn Baldwin 		sc->flags |= PCIB_HOTPLUG_CMD_PENDING;
102682cb5c3bSJohn Baldwin 		if (!cold)
1027fa3b03d3SAlexander Motin 			taskqueue_enqueue_timeout(taskqueue_pci_hp,
1028fa3b03d3SAlexander Motin 			    &sc->pcie_cc_task, hz);
102982cb5c3bSJohn Baldwin 	}
103082cb5c3bSJohn Baldwin }
103182cb5c3bSJohn Baldwin 
103282cb5c3bSJohn Baldwin static void
103382cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(struct pcib_softc *sc)
103482cb5c3bSJohn Baldwin {
103582cb5c3bSJohn Baldwin 	device_t dev;
103682cb5c3bSJohn Baldwin 
103782cb5c3bSJohn Baldwin 	dev = sc->dev;
103882cb5c3bSJohn Baldwin 
103982cb5c3bSJohn Baldwin 	if (bootverbose)
104082cb5c3bSJohn Baldwin 		device_printf(dev, "Command Completed\n");
104182cb5c3bSJohn Baldwin 	if (!(sc->flags & PCIB_HOTPLUG_CMD_PENDING))
104282cb5c3bSJohn Baldwin 		return;
1043fa3b03d3SAlexander Motin 	taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_cc_task, NULL);
104482cb5c3bSJohn Baldwin 	sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING;
10456f33eaa5SJohn Baldwin 	wakeup(sc);
104682cb5c3bSJohn Baldwin }
104782cb5c3bSJohn Baldwin 
104882cb5c3bSJohn Baldwin /*
104982cb5c3bSJohn Baldwin  * Returns true if a card is fully inserted from the user's
105082cb5c3bSJohn Baldwin  * perspective.  It may not yet be ready for access, but the driver
105182cb5c3bSJohn Baldwin  * can now start enabling access if necessary.
105282cb5c3bSJohn Baldwin  */
105382cb5c3bSJohn Baldwin static bool
105482cb5c3bSJohn Baldwin pcib_hotplug_inserted(struct pcib_softc *sc)
105582cb5c3bSJohn Baldwin {
105682cb5c3bSJohn Baldwin 
105782cb5c3bSJohn Baldwin 	/* Pretend the card isn't present if a detach is forced. */
105882cb5c3bSJohn Baldwin 	if (sc->flags & PCIB_DETACHING)
105982cb5c3bSJohn Baldwin 		return (false);
106082cb5c3bSJohn Baldwin 
106182cb5c3bSJohn Baldwin 	/* Card must be present in the slot. */
106282cb5c3bSJohn Baldwin 	if ((sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS) == 0)
106382cb5c3bSJohn Baldwin 		return (false);
106482cb5c3bSJohn Baldwin 
106582cb5c3bSJohn Baldwin 	/* A power fault implicitly turns off power to the slot. */
106682cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD)
106782cb5c3bSJohn Baldwin 		return (false);
106882cb5c3bSJohn Baldwin 
106982cb5c3bSJohn Baldwin 	/* If the MRL is disengaged, the slot is powered off. */
107082cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP &&
107182cb5c3bSJohn Baldwin 	    (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS) != 0)
107282cb5c3bSJohn Baldwin 		return (false);
107382cb5c3bSJohn Baldwin 
107482cb5c3bSJohn Baldwin 	return (true);
107582cb5c3bSJohn Baldwin }
107682cb5c3bSJohn Baldwin 
107782cb5c3bSJohn Baldwin /*
107882cb5c3bSJohn Baldwin  * Returns -1 if the card is fully inserted, powered, and ready for
107982cb5c3bSJohn Baldwin  * access.  Otherwise, returns 0.
108082cb5c3bSJohn Baldwin  */
108182cb5c3bSJohn Baldwin static int
108282cb5c3bSJohn Baldwin pcib_hotplug_present(struct pcib_softc *sc)
108382cb5c3bSJohn Baldwin {
108482cb5c3bSJohn Baldwin 
108582cb5c3bSJohn Baldwin 	/* Card must be inserted. */
108682cb5c3bSJohn Baldwin 	if (!pcib_hotplug_inserted(sc))
108782cb5c3bSJohn Baldwin 		return (0);
108882cb5c3bSJohn Baldwin 
108982cb5c3bSJohn Baldwin 	/* Require the Data Link Layer to be active. */
109082cb5c3bSJohn Baldwin 	if (!(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE))
109182cb5c3bSJohn Baldwin 		return (0);
109282cb5c3bSJohn Baldwin 
109382cb5c3bSJohn Baldwin 	return (-1);
109482cb5c3bSJohn Baldwin }
109582cb5c3bSJohn Baldwin 
1096*a58536b9SAlexander Motin static int pci_enable_pcie_ei = 0;
1097*a58536b9SAlexander Motin SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_ei, CTLFLAG_RWTUN,
1098*a58536b9SAlexander Motin     &pci_enable_pcie_ei, 0,
1099*a58536b9SAlexander Motin     "Enable support for PCI-express Electromechanical Interlock.");
1100*a58536b9SAlexander Motin 
110182cb5c3bSJohn Baldwin static void
110282cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(struct pcib_softc *sc, uint16_t val, uint16_t mask,
110382cb5c3bSJohn Baldwin     bool schedule_task)
110482cb5c3bSJohn Baldwin {
1105a1566487SEric van Gyzen 	bool card_inserted, ei_engaged;
110682cb5c3bSJohn Baldwin 
1107991d431fSEric van Gyzen 	/* Clear DETACHING if Presence Detect has cleared. */
110882cb5c3bSJohn Baldwin 	if ((sc->pcie_slot_sta & (PCIEM_SLOT_STA_PDC | PCIEM_SLOT_STA_PDS)) ==
110982cb5c3bSJohn Baldwin 	    PCIEM_SLOT_STA_PDC)
111082cb5c3bSJohn Baldwin 		sc->flags &= ~PCIB_DETACHING;
111182cb5c3bSJohn Baldwin 
111282cb5c3bSJohn Baldwin 	card_inserted = pcib_hotplug_inserted(sc);
111382cb5c3bSJohn Baldwin 
111482cb5c3bSJohn Baldwin 	/* Turn the power indicator on if a card is inserted. */
111582cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PIP) {
111682cb5c3bSJohn Baldwin 		mask |= PCIEM_SLOT_CTL_PIC;
111782cb5c3bSJohn Baldwin 		if (card_inserted)
111882cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PI_ON;
111982cb5c3bSJohn Baldwin 		else if (sc->flags & PCIB_DETACH_PENDING)
112082cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PI_BLINK;
112182cb5c3bSJohn Baldwin 		else
112282cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PI_OFF;
112382cb5c3bSJohn Baldwin 	}
112482cb5c3bSJohn Baldwin 
112582cb5c3bSJohn Baldwin 	/* Turn the power on via the Power Controller if a card is inserted. */
112682cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) {
112782cb5c3bSJohn Baldwin 		mask |= PCIEM_SLOT_CTL_PCC;
112882cb5c3bSJohn Baldwin 		if (card_inserted)
112982cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PC_ON;
113082cb5c3bSJohn Baldwin 		else
113182cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_PC_OFF;
113282cb5c3bSJohn Baldwin 	}
113382cb5c3bSJohn Baldwin 
113482cb5c3bSJohn Baldwin 	/*
113582cb5c3bSJohn Baldwin 	 * If a card is inserted, enable the Electromechanical
113682cb5c3bSJohn Baldwin 	 * Interlock.  If a card is not inserted (or we are in the
113782cb5c3bSJohn Baldwin 	 * process of detaching), disable the Electromechanical
113882cb5c3bSJohn Baldwin 	 * Interlock.
113982cb5c3bSJohn Baldwin 	 */
1140*a58536b9SAlexander Motin 	if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP) &&
1141*a58536b9SAlexander Motin 	    pci_enable_pcie_ei) {
114282cb5c3bSJohn Baldwin 		mask |= PCIEM_SLOT_CTL_EIC;
1143a1566487SEric van Gyzen 		ei_engaged = (sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS) != 0;
1144a1566487SEric van Gyzen 		if (card_inserted != ei_engaged)
114582cb5c3bSJohn Baldwin 			val |= PCIEM_SLOT_CTL_EIC;
114682cb5c3bSJohn Baldwin 	}
114782cb5c3bSJohn Baldwin 
114882cb5c3bSJohn Baldwin 	/*
114982cb5c3bSJohn Baldwin 	 * Start a timer to see if the Data Link Layer times out.
1150991d431fSEric van Gyzen 	 * Note that we only start the timer if Presence Detect or MRL Sensor
115182cb5c3bSJohn Baldwin 	 * changed on this interrupt.  Stop any scheduled timer if
115282cb5c3bSJohn Baldwin 	 * the Data Link Layer is active.
115382cb5c3bSJohn Baldwin 	 */
115482cb5c3bSJohn Baldwin 	if (card_inserted &&
115582cb5c3bSJohn Baldwin 	    !(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) &&
1156991d431fSEric van Gyzen 	    sc->pcie_slot_sta &
1157991d431fSEric van Gyzen 	    (PCIEM_SLOT_STA_MRLSC | PCIEM_SLOT_STA_PDC)) {
115882cb5c3bSJohn Baldwin 		if (cold)
115982cb5c3bSJohn Baldwin 			device_printf(sc->dev,
116082cb5c3bSJohn Baldwin 			    "Data Link Layer inactive\n");
116182cb5c3bSJohn Baldwin 		else
1162fa3b03d3SAlexander Motin 			taskqueue_enqueue_timeout(taskqueue_pci_hp,
1163fa3b03d3SAlexander Motin 			    &sc->pcie_dll_task, hz);
116482cb5c3bSJohn Baldwin 	} else if (sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE)
1165fa3b03d3SAlexander Motin 		taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_dll_task,
1166fa3b03d3SAlexander Motin 		    NULL);
116782cb5c3bSJohn Baldwin 
116882cb5c3bSJohn Baldwin 	pcib_pcie_hotplug_command(sc, val, mask);
116982cb5c3bSJohn Baldwin 
117082cb5c3bSJohn Baldwin 	/*
1171a1566487SEric van Gyzen 	 * During attach the child "pci" device is added synchronously;
117282cb5c3bSJohn Baldwin 	 * otherwise, the task is scheduled to manage the child
117382cb5c3bSJohn Baldwin 	 * device.
117482cb5c3bSJohn Baldwin 	 */
117582cb5c3bSJohn Baldwin 	if (schedule_task &&
117682cb5c3bSJohn Baldwin 	    (pcib_hotplug_present(sc) != 0) != (sc->child != NULL))
117712588ce0SAndriy Gapon 		taskqueue_enqueue(taskqueue_pci_hp, &sc->pcie_hp_task);
117882cb5c3bSJohn Baldwin }
117982cb5c3bSJohn Baldwin 
118082cb5c3bSJohn Baldwin static void
11818a1926c5SWarner Losh pcib_pcie_intr_hotplug(void *arg)
118282cb5c3bSJohn Baldwin {
118382cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
118482cb5c3bSJohn Baldwin 	device_t dev;
1185e0235fd3SColin Percival 	uint16_t old_slot_sta;
118682cb5c3bSJohn Baldwin 
118782cb5c3bSJohn Baldwin 	sc = arg;
118882cb5c3bSJohn Baldwin 	dev = sc->dev;
118913d700adSScott Long 	PCIB_HP_LOCK(sc);
1190e0235fd3SColin Percival 	old_slot_sta = sc->pcie_slot_sta;
119182cb5c3bSJohn Baldwin 	sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
119282cb5c3bSJohn Baldwin 
119382cb5c3bSJohn Baldwin 	/* Clear the events just reported. */
119482cb5c3bSJohn Baldwin 	pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2);
119582cb5c3bSJohn Baldwin 
1196991d431fSEric van Gyzen 	if (bootverbose)
1197991d431fSEric van Gyzen 		device_printf(dev, "HotPlug interrupt: %#x\n",
1198991d431fSEric van Gyzen 		    sc->pcie_slot_sta);
1199991d431fSEric van Gyzen 
120082cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_ABP) {
120182cb5c3bSJohn Baldwin 		if (sc->flags & PCIB_DETACH_PENDING) {
120282cb5c3bSJohn Baldwin 			device_printf(dev,
120382cb5c3bSJohn Baldwin 			    "Attention Button Pressed: Detach Cancelled\n");
120482cb5c3bSJohn Baldwin 			sc->flags &= ~PCIB_DETACH_PENDING;
1205fa3b03d3SAlexander Motin 			taskqueue_cancel_timeout(taskqueue_pci_hp,
1206fa3b03d3SAlexander Motin 			    &sc->pcie_ab_task, NULL);
1207e0235fd3SColin Percival 		} else if (old_slot_sta & PCIEM_SLOT_STA_PDS) {
1208e0235fd3SColin Percival 			/* Only initiate detach sequence if device present. */
120982cb5c3bSJohn Baldwin 			device_printf(dev,
121082cb5c3bSJohn Baldwin 		    "Attention Button Pressed: Detaching in 5 seconds\n");
121182cb5c3bSJohn Baldwin 			sc->flags |= PCIB_DETACH_PENDING;
1212fa3b03d3SAlexander Motin 			taskqueue_enqueue_timeout(taskqueue_pci_hp,
1213fa3b03d3SAlexander Motin 			    &sc->pcie_ab_task, 5 * hz);
121482cb5c3bSJohn Baldwin 		}
121582cb5c3bSJohn Baldwin 	}
121682cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD)
121782cb5c3bSJohn Baldwin 		device_printf(dev, "Power Fault Detected\n");
121882cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSC)
121982cb5c3bSJohn Baldwin 		device_printf(dev, "MRL Sensor Changed to %s\n",
122082cb5c3bSJohn Baldwin 		    sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS ? "open" :
122182cb5c3bSJohn Baldwin 		    "closed");
122282cb5c3bSJohn Baldwin 	if (bootverbose && sc->pcie_slot_sta & PCIEM_SLOT_STA_PDC)
1223991d431fSEric van Gyzen 		device_printf(dev, "Presence Detect Changed to %s\n",
122482cb5c3bSJohn Baldwin 		    sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS ? "card present" :
122582cb5c3bSJohn Baldwin 		    "empty");
122682cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_CC)
122782cb5c3bSJohn Baldwin 		pcib_pcie_hotplug_command_completed(sc);
122882cb5c3bSJohn Baldwin 	if (sc->pcie_slot_sta & PCIEM_SLOT_STA_DLLSC) {
122982cb5c3bSJohn Baldwin 		sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
123082cb5c3bSJohn Baldwin 		if (bootverbose)
123182cb5c3bSJohn Baldwin 			device_printf(dev,
123282cb5c3bSJohn Baldwin 			    "Data Link Layer State Changed to %s\n",
123382cb5c3bSJohn Baldwin 			    sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE ?
123482cb5c3bSJohn Baldwin 			    "active" : "inactive");
123582cb5c3bSJohn Baldwin 	}
123682cb5c3bSJohn Baldwin 
123782cb5c3bSJohn Baldwin 	pcib_pcie_hotplug_update(sc, 0, 0, true);
123813d700adSScott Long 	PCIB_HP_UNLOCK(sc);
123982cb5c3bSJohn Baldwin }
124082cb5c3bSJohn Baldwin 
124182cb5c3bSJohn Baldwin static void
124282cb5c3bSJohn Baldwin pcib_pcie_hotplug_task(void *context, int pending)
124382cb5c3bSJohn Baldwin {
124482cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
124582cb5c3bSJohn Baldwin 	device_t dev;
124682cb5c3bSJohn Baldwin 
124782cb5c3bSJohn Baldwin 	sc = context;
124813d700adSScott Long 	PCIB_HP_LOCK(sc);
124982cb5c3bSJohn Baldwin 	dev = sc->dev;
125082cb5c3bSJohn Baldwin 	if (pcib_hotplug_present(sc) != 0) {
125182cb5c3bSJohn Baldwin 		if (sc->child == NULL) {
125282cb5c3bSJohn Baldwin 			sc->child = device_add_child(dev, "pci", -1);
125382cb5c3bSJohn Baldwin 			bus_generic_attach(dev);
125482cb5c3bSJohn Baldwin 		}
125582cb5c3bSJohn Baldwin 	} else {
125682cb5c3bSJohn Baldwin 		if (sc->child != NULL) {
125782cb5c3bSJohn Baldwin 			if (device_delete_child(dev, sc->child) == 0)
125882cb5c3bSJohn Baldwin 				sc->child = NULL;
125982cb5c3bSJohn Baldwin 		}
126082cb5c3bSJohn Baldwin 	}
126113d700adSScott Long 	PCIB_HP_UNLOCK(sc);
126282cb5c3bSJohn Baldwin }
126382cb5c3bSJohn Baldwin 
126482cb5c3bSJohn Baldwin static void
1265fa3b03d3SAlexander Motin pcib_pcie_ab_timeout(void *arg, int pending)
126682cb5c3bSJohn Baldwin {
1267fa3b03d3SAlexander Motin 	struct pcib_softc *sc = arg;
126882cb5c3bSJohn Baldwin 
1269fa3b03d3SAlexander Motin 	PCIB_HP_LOCK(sc);
127082cb5c3bSJohn Baldwin 	if (sc->flags & PCIB_DETACH_PENDING) {
127182cb5c3bSJohn Baldwin 		sc->flags |= PCIB_DETACHING;
127282cb5c3bSJohn Baldwin 		sc->flags &= ~PCIB_DETACH_PENDING;
127382cb5c3bSJohn Baldwin 		pcib_pcie_hotplug_update(sc, 0, 0, true);
127482cb5c3bSJohn Baldwin 	}
1275fa3b03d3SAlexander Motin 	PCIB_HP_UNLOCK(sc);
127682cb5c3bSJohn Baldwin }
127782cb5c3bSJohn Baldwin 
127882cb5c3bSJohn Baldwin static void
1279fa3b03d3SAlexander Motin pcib_pcie_cc_timeout(void *arg, int pending)
128082cb5c3bSJohn Baldwin {
1281fa3b03d3SAlexander Motin 	struct pcib_softc *sc = arg;
1282fa3b03d3SAlexander Motin 	device_t dev = sc->dev;
12836f33eaa5SJohn Baldwin 	uint16_t sta;
128482cb5c3bSJohn Baldwin 
1285fa3b03d3SAlexander Motin 	PCIB_HP_LOCK(sc);
12866f33eaa5SJohn Baldwin 	sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
12876f33eaa5SJohn Baldwin 	if (!(sta & PCIEM_SLOT_STA_CC)) {
128821e51c82SAlexander Motin 		device_printf(dev, "HotPlug Command Timed Out\n");
128921e51c82SAlexander Motin 		sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING;
12906f33eaa5SJohn Baldwin 	} else {
12916f33eaa5SJohn Baldwin 		device_printf(dev,
12926f33eaa5SJohn Baldwin 	    "Missed HotPlug interrupt waiting for Command Completion\n");
12938a1926c5SWarner Losh 		pcib_pcie_intr_hotplug(sc);
129482cb5c3bSJohn Baldwin 	}
1295fa3b03d3SAlexander Motin 	PCIB_HP_UNLOCK(sc);
129682cb5c3bSJohn Baldwin }
129782cb5c3bSJohn Baldwin 
129882cb5c3bSJohn Baldwin static void
1299fa3b03d3SAlexander Motin pcib_pcie_dll_timeout(void *arg, int pending)
130082cb5c3bSJohn Baldwin {
1301fa3b03d3SAlexander Motin 	struct pcib_softc *sc = arg;
1302fa3b03d3SAlexander Motin 	device_t dev = sc->dev;
130382cb5c3bSJohn Baldwin 	uint16_t sta;
130482cb5c3bSJohn Baldwin 
1305fa3b03d3SAlexander Motin 	PCIB_HP_LOCK(sc);
130682cb5c3bSJohn Baldwin 	sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
130782cb5c3bSJohn Baldwin 	if (!(sta & PCIEM_LINK_STA_DL_ACTIVE)) {
130882cb5c3bSJohn Baldwin 		device_printf(dev,
130982cb5c3bSJohn Baldwin 		    "Timed out waiting for Data Link Layer Active\n");
131082cb5c3bSJohn Baldwin 		sc->flags |= PCIB_DETACHING;
131182cb5c3bSJohn Baldwin 		pcib_pcie_hotplug_update(sc, 0, 0, true);
131282cb5c3bSJohn Baldwin 	} else if (sta != sc->pcie_link_sta) {
131382cb5c3bSJohn Baldwin 		device_printf(dev,
131482cb5c3bSJohn Baldwin 		    "Missed HotPlug interrupt waiting for DLL Active\n");
13158a1926c5SWarner Losh 		pcib_pcie_intr_hotplug(sc);
131682cb5c3bSJohn Baldwin 	}
1317fa3b03d3SAlexander Motin 	PCIB_HP_UNLOCK(sc);
131882cb5c3bSJohn Baldwin }
131982cb5c3bSJohn Baldwin 
132082cb5c3bSJohn Baldwin static int
132182cb5c3bSJohn Baldwin pcib_alloc_pcie_irq(struct pcib_softc *sc)
132282cb5c3bSJohn Baldwin {
132382cb5c3bSJohn Baldwin 	device_t dev;
132482cb5c3bSJohn Baldwin 	int count, error, rid;
132582cb5c3bSJohn Baldwin 
132682cb5c3bSJohn Baldwin 	rid = -1;
132782cb5c3bSJohn Baldwin 	dev = sc->dev;
132882cb5c3bSJohn Baldwin 
132982cb5c3bSJohn Baldwin 	/*
133082cb5c3bSJohn Baldwin 	 * For simplicity, only use MSI-X if there is a single message.
133182cb5c3bSJohn Baldwin 	 * To support a device with multiple messages we would have to
133282cb5c3bSJohn Baldwin 	 * use remap intr if the MSI number is not 0.
133382cb5c3bSJohn Baldwin 	 */
133482cb5c3bSJohn Baldwin 	count = pci_msix_count(dev);
133582cb5c3bSJohn Baldwin 	if (count == 1) {
133682cb5c3bSJohn Baldwin 		error = pci_alloc_msix(dev, &count);
133782cb5c3bSJohn Baldwin 		if (error == 0)
133882cb5c3bSJohn Baldwin 			rid = 1;
133982cb5c3bSJohn Baldwin 	}
134082cb5c3bSJohn Baldwin 
134182cb5c3bSJohn Baldwin 	if (rid < 0 && pci_msi_count(dev) > 0) {
134282cb5c3bSJohn Baldwin 		count = 1;
134382cb5c3bSJohn Baldwin 		error = pci_alloc_msi(dev, &count);
134482cb5c3bSJohn Baldwin 		if (error == 0)
134582cb5c3bSJohn Baldwin 			rid = 1;
134682cb5c3bSJohn Baldwin 	}
134782cb5c3bSJohn Baldwin 
134882cb5c3bSJohn Baldwin 	if (rid < 0)
134982cb5c3bSJohn Baldwin 		rid = 0;
135082cb5c3bSJohn Baldwin 
135182cb5c3bSJohn Baldwin 	sc->pcie_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1352f14f0051SChuck Tuffli 	    RF_ACTIVE | RF_SHAREABLE);
135382cb5c3bSJohn Baldwin 	if (sc->pcie_irq == NULL) {
135482cb5c3bSJohn Baldwin 		device_printf(dev,
135582cb5c3bSJohn Baldwin 		    "Failed to allocate interrupt for PCI-e events\n");
135682cb5c3bSJohn Baldwin 		if (rid > 0)
135782cb5c3bSJohn Baldwin 			pci_release_msi(dev);
135882cb5c3bSJohn Baldwin 		return (ENXIO);
135982cb5c3bSJohn Baldwin 	}
136082cb5c3bSJohn Baldwin 
136113d700adSScott Long 	error = bus_setup_intr(dev, sc->pcie_irq, INTR_TYPE_MISC|INTR_MPSAFE,
13628a1926c5SWarner Losh 	    NULL, pcib_pcie_intr_hotplug, sc, &sc->pcie_ihand);
136382cb5c3bSJohn Baldwin 	if (error) {
136482cb5c3bSJohn Baldwin 		device_printf(dev, "Failed to setup PCI-e interrupt handler\n");
136582cb5c3bSJohn Baldwin 		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->pcie_irq);
136682cb5c3bSJohn Baldwin 		if (rid > 0)
136782cb5c3bSJohn Baldwin 			pci_release_msi(dev);
136882cb5c3bSJohn Baldwin 		return (error);
136982cb5c3bSJohn Baldwin 	}
137082cb5c3bSJohn Baldwin 	return (0);
137182cb5c3bSJohn Baldwin }
137282cb5c3bSJohn Baldwin 
13736f33eaa5SJohn Baldwin static int
13746f33eaa5SJohn Baldwin pcib_release_pcie_irq(struct pcib_softc *sc)
13756f33eaa5SJohn Baldwin {
13766f33eaa5SJohn Baldwin 	device_t dev;
13776f33eaa5SJohn Baldwin 	int error;
13786f33eaa5SJohn Baldwin 
13796f33eaa5SJohn Baldwin 	dev = sc->dev;
13806f33eaa5SJohn Baldwin 	error = bus_teardown_intr(dev, sc->pcie_irq, sc->pcie_ihand);
13816f33eaa5SJohn Baldwin 	if (error)
13826f33eaa5SJohn Baldwin 		return (error);
13836f33eaa5SJohn Baldwin 	error = bus_free_resource(dev, SYS_RES_IRQ, sc->pcie_irq);
13846f33eaa5SJohn Baldwin 	if (error)
13856f33eaa5SJohn Baldwin 		return (error);
13866f33eaa5SJohn Baldwin 	return (pci_release_msi(dev));
13876f33eaa5SJohn Baldwin }
13886f33eaa5SJohn Baldwin 
138982cb5c3bSJohn Baldwin static void
139082cb5c3bSJohn Baldwin pcib_setup_hotplug(struct pcib_softc *sc)
139182cb5c3bSJohn Baldwin {
139282cb5c3bSJohn Baldwin 	device_t dev;
139382cb5c3bSJohn Baldwin 	uint16_t mask, val;
139482cb5c3bSJohn Baldwin 
139582cb5c3bSJohn Baldwin 	dev = sc->dev;
139682cb5c3bSJohn Baldwin 	TASK_INIT(&sc->pcie_hp_task, 0, pcib_pcie_hotplug_task, sc);
1397fa3b03d3SAlexander Motin 	TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_ab_task, 0,
1398fa3b03d3SAlexander Motin 	    pcib_pcie_ab_timeout, sc);
1399fa3b03d3SAlexander Motin 	TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_cc_task, 0,
1400fa3b03d3SAlexander Motin 	    pcib_pcie_cc_timeout, sc);
1401fa3b03d3SAlexander Motin 	TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_dll_task, 0,
1402fa3b03d3SAlexander Motin 	    pcib_pcie_dll_timeout, sc);
1403c6df6f53SWarner Losh 	sc->pcie_hp_lock = bus_topo_mtx();
140482cb5c3bSJohn Baldwin 
140582cb5c3bSJohn Baldwin 	/* Allocate IRQ. */
140682cb5c3bSJohn Baldwin 	if (pcib_alloc_pcie_irq(sc) != 0)
140782cb5c3bSJohn Baldwin 		return;
140882cb5c3bSJohn Baldwin 
140982cb5c3bSJohn Baldwin 	sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
141082cb5c3bSJohn Baldwin 	sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
141182cb5c3bSJohn Baldwin 
14126f33eaa5SJohn Baldwin 	/* Clear any events previously pending. */
14136f33eaa5SJohn Baldwin 	pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2);
14146f33eaa5SJohn Baldwin 
141582cb5c3bSJohn Baldwin 	/* Enable HotPlug events. */
141682cb5c3bSJohn Baldwin 	mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE |
141782cb5c3bSJohn Baldwin 	    PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE |
141882cb5c3bSJohn Baldwin 	    PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE;
141937290148SEric van Gyzen 	val = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | PCIEM_SLOT_CTL_PDCE;
142082cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_APB)
142182cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_ABPE;
142282cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP)
142382cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_PFDE;
142482cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP)
142582cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_MRLSCE;
142682cb5c3bSJohn Baldwin 	if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS))
142782cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_CCIE;
142882cb5c3bSJohn Baldwin 
142982cb5c3bSJohn Baldwin 	/* Turn the attention indicator off. */
143082cb5c3bSJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) {
143182cb5c3bSJohn Baldwin 		mask |= PCIEM_SLOT_CTL_AIC;
143282cb5c3bSJohn Baldwin 		val |= PCIEM_SLOT_CTL_AI_OFF;
143382cb5c3bSJohn Baldwin 	}
143482cb5c3bSJohn Baldwin 
143582cb5c3bSJohn Baldwin 	pcib_pcie_hotplug_update(sc, val, mask, false);
143682cb5c3bSJohn Baldwin }
14376f33eaa5SJohn Baldwin 
14386f33eaa5SJohn Baldwin static int
14396f33eaa5SJohn Baldwin pcib_detach_hotplug(struct pcib_softc *sc)
14406f33eaa5SJohn Baldwin {
14416f33eaa5SJohn Baldwin 	uint16_t mask, val;
14426f33eaa5SJohn Baldwin 	int error;
14436f33eaa5SJohn Baldwin 
14446f33eaa5SJohn Baldwin 	/* Disable the card in the slot and force it to detach. */
14456f33eaa5SJohn Baldwin 	if (sc->flags & PCIB_DETACH_PENDING) {
14466f33eaa5SJohn Baldwin 		sc->flags &= ~PCIB_DETACH_PENDING;
1447fa3b03d3SAlexander Motin 		taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_ab_task,
1448fa3b03d3SAlexander Motin 		    NULL);
14496f33eaa5SJohn Baldwin 	}
14506f33eaa5SJohn Baldwin 	sc->flags |= PCIB_DETACHING;
14516f33eaa5SJohn Baldwin 
14526f33eaa5SJohn Baldwin 	if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) {
1453fa3b03d3SAlexander Motin 		taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_cc_task,
1454fa3b03d3SAlexander Motin 		    NULL);
14556f33eaa5SJohn Baldwin 		tsleep(sc, 0, "hpcmd", hz);
14566f33eaa5SJohn Baldwin 		sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING;
14576f33eaa5SJohn Baldwin 	}
14586f33eaa5SJohn Baldwin 
14596f33eaa5SJohn Baldwin 	/* Disable HotPlug events. */
14606f33eaa5SJohn Baldwin 	mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE |
14616f33eaa5SJohn Baldwin 	    PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE |
14626f33eaa5SJohn Baldwin 	    PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE;
14636f33eaa5SJohn Baldwin 	val = 0;
14646f33eaa5SJohn Baldwin 
14656f33eaa5SJohn Baldwin 	/* Turn the attention indicator off. */
14666f33eaa5SJohn Baldwin 	if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) {
14676f33eaa5SJohn Baldwin 		mask |= PCIEM_SLOT_CTL_AIC;
14686f33eaa5SJohn Baldwin 		val |= PCIEM_SLOT_CTL_AI_OFF;
14696f33eaa5SJohn Baldwin 	}
14706f33eaa5SJohn Baldwin 
14716f33eaa5SJohn Baldwin 	pcib_pcie_hotplug_update(sc, val, mask, false);
14726f33eaa5SJohn Baldwin 
14736f33eaa5SJohn Baldwin 	error = pcib_release_pcie_irq(sc);
14746f33eaa5SJohn Baldwin 	if (error)
14756f33eaa5SJohn Baldwin 		return (error);
147612588ce0SAndriy Gapon 	taskqueue_drain(taskqueue_pci_hp, &sc->pcie_hp_task);
1477fa3b03d3SAlexander Motin 	taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_ab_task);
1478fa3b03d3SAlexander Motin 	taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_cc_task);
1479fa3b03d3SAlexander Motin 	taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_dll_task);
14806f33eaa5SJohn Baldwin 	return (0);
14816f33eaa5SJohn Baldwin }
148282cb5c3bSJohn Baldwin #endif
148382cb5c3bSJohn Baldwin 
1484e36af292SJung-uk Kim /*
1485e36af292SJung-uk Kim  * Get current bridge configuration.
1486e36af292SJung-uk Kim  */
1487e36af292SJung-uk Kim static void
1488e36af292SJung-uk Kim pcib_cfg_save(struct pcib_softc *sc)
1489e36af292SJung-uk Kim {
1490ad6f36f8SJohn Baldwin #ifndef NEW_PCIB
1491e36af292SJung-uk Kim 	device_t	dev;
1492ad6f36f8SJohn Baldwin 	uint16_t command;
1493e36af292SJung-uk Kim 
1494e36af292SJung-uk Kim 	dev = sc->dev;
1495e36af292SJung-uk Kim 
1496ad6f36f8SJohn Baldwin 	command = pci_read_config(dev, PCIR_COMMAND, 2);
1497ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_PORTEN)
1498e36af292SJung-uk Kim 		pcib_get_io_decode(sc);
1499ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_MEMEN)
1500e36af292SJung-uk Kim 		pcib_get_mem_decode(sc);
150183c41143SJohn Baldwin #endif
1502e36af292SJung-uk Kim }
1503e36af292SJung-uk Kim 
1504e36af292SJung-uk Kim /*
1505e36af292SJung-uk Kim  * Restore previous bridge configuration.
1506e36af292SJung-uk Kim  */
1507e36af292SJung-uk Kim static void
1508e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc)
1509e36af292SJung-uk Kim {
1510ad6f36f8SJohn Baldwin #ifndef NEW_PCIB
1511ad6f36f8SJohn Baldwin 	uint16_t command;
1512ad6f36f8SJohn Baldwin #endif
1513e36af292SJung-uk Kim 
151483c41143SJohn Baldwin #ifdef NEW_PCIB
151583c41143SJohn Baldwin 	pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM);
151683c41143SJohn Baldwin #else
1517151ba793SAlexander Kabaev 	command = pci_read_config(sc->dev, PCIR_COMMAND, 2);
1518ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_PORTEN)
1519e36af292SJung-uk Kim 		pcib_set_io_decode(sc);
1520ad6f36f8SJohn Baldwin 	if (command & PCIM_CMD_MEMEN)
1521e36af292SJung-uk Kim 		pcib_set_mem_decode(sc);
152283c41143SJohn Baldwin #endif
1523e36af292SJung-uk Kim }
1524e36af292SJung-uk Kim 
1525e36af292SJung-uk Kim /*
1526bb0d0a8eSMike Smith  * Generic device interface
1527bb0d0a8eSMike Smith  */
1528bb0d0a8eSMike Smith static int
1529bb0d0a8eSMike Smith pcib_probe(device_t dev)
1530bb0d0a8eSMike Smith {
1531bb0d0a8eSMike Smith     if ((pci_get_class(dev) == PCIC_BRIDGE) &&
1532bb0d0a8eSMike Smith 	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
1533bb0d0a8eSMike Smith 	device_set_desc(dev, "PCI-PCI bridge");
1534b7cbd25bSMarcel Moolenaar 	return(-10000);
1535bb0d0a8eSMike Smith     }
1536bb0d0a8eSMike Smith     return(ENXIO);
1537bb0d0a8eSMike Smith }
1538bb0d0a8eSMike Smith 
15396f0d5884SJohn Baldwin void
15406f0d5884SJohn Baldwin pcib_attach_common(device_t dev)
1541bb0d0a8eSMike Smith {
1542bb0d0a8eSMike Smith     struct pcib_softc	*sc;
1543abf07f13SWarner Losh     struct sysctl_ctx_list *sctx;
1544abf07f13SWarner Losh     struct sysctl_oid	*soid;
1545c825d4dcSJohn Baldwin     int comma;
1546bb0d0a8eSMike Smith 
1547bb0d0a8eSMike Smith     sc = device_get_softc(dev);
1548bb0d0a8eSMike Smith     sc->dev = dev;
1549bb0d0a8eSMike Smith 
15504fa59183SMike Smith     /*
15514fa59183SMike Smith      * Get current bridge configuration.
15524fa59183SMike Smith      */
155355aaf894SMarius Strobl     sc->domain = pci_get_domain(dev);
1554ad6f36f8SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
1555ad6f36f8SJohn Baldwin     sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1);
1556ad6f36f8SJohn Baldwin     sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
1557ad6f36f8SJohn Baldwin #endif
1558ad6f36f8SJohn Baldwin     sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
1559e36af292SJung-uk Kim     pcib_cfg_save(sc);
15604fa59183SMike Smith 
15614fa59183SMike Smith     /*
15624edef187SJohn Baldwin      * The primary bus register should always be the bus of the
15634edef187SJohn Baldwin      * parent.
15644edef187SJohn Baldwin      */
15654edef187SJohn Baldwin     sc->pribus = pci_get_bus(dev);
15664edef187SJohn Baldwin     pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
15674edef187SJohn Baldwin 
15684edef187SJohn Baldwin     /*
1569abf07f13SWarner Losh      * Setup sysctl reporting nodes
1570abf07f13SWarner Losh      */
1571abf07f13SWarner Losh     sctx = device_get_sysctl_ctx(dev);
1572abf07f13SWarner Losh     soid = device_get_sysctl_tree(dev);
1573abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
1574abf07f13SWarner Losh       CTLFLAG_RD, &sc->domain, 0, "Domain number");
1575abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
1576abf07f13SWarner Losh       CTLFLAG_RD, &sc->pribus, 0, "Primary bus number");
1577abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
15784edef187SJohn Baldwin       CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number");
1579abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
15804edef187SJohn Baldwin       CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number");
1581abf07f13SWarner Losh 
1582abf07f13SWarner Losh     /*
15834fa59183SMike Smith      * Quirk handling.
15844fa59183SMike Smith      */
15854fa59183SMike Smith     switch (pci_get_devid(dev)) {
15862ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
15874fa59183SMike Smith     case 0x12258086:		/* Intel 82454KX/GX (Orion) */
15884fa59183SMike Smith 	{
1589b0cb115fSWarner Losh 	    uint8_t	supbus;
15904fa59183SMike Smith 
15914fa59183SMike Smith 	    supbus = pci_read_config(dev, 0x41, 1);
15924fa59183SMike Smith 	    if (supbus != 0xff) {
15934edef187SJohn Baldwin 		sc->bus.sec = supbus + 1;
15944edef187SJohn Baldwin 		sc->bus.sub = supbus + 1;
15954fa59183SMike Smith 	    }
15964fa59183SMike Smith 	    break;
15974fa59183SMike Smith 	}
15984edef187SJohn Baldwin #endif
15994fa59183SMike Smith 
1600e4b59fc5SWarner Losh     /*
1601e4b59fc5SWarner Losh      * The i82380FB mobile docking controller is a PCI-PCI bridge,
1602e4b59fc5SWarner Losh      * and it is a subtractive bridge.  However, the ProgIf is wrong
1603e4b59fc5SWarner Losh      * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
16044718610dSZbigniew Bodek      * happen.  There are also Toshiba and Cavium ThunderX bridges
16054718610dSZbigniew Bodek      * that behave this way.
1606e4b59fc5SWarner Losh      */
16074718610dSZbigniew Bodek     case 0xa002177d:		/* Cavium ThunderX */
1608e4b59fc5SWarner Losh     case 0x124b8086:		/* Intel 82380FB Mobile */
1609e4b59fc5SWarner Losh     case 0x060513d7:		/* Toshiba ???? */
1610e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
1611e4b59fc5SWarner Losh 	break;
1612c94d6dbeSJung-uk Kim 
16132ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
1614c94d6dbeSJung-uk Kim     /* Compaq R3000 BIOS sets wrong subordinate bus number. */
1615c94d6dbeSJung-uk Kim     case 0x00dd10de:
1616c94d6dbeSJung-uk Kim 	{
1617c94d6dbeSJung-uk Kim 	    char *cp;
1618c94d6dbeSJung-uk Kim 
16192be111bfSDavide Italiano 	    if ((cp = kern_getenv("smbios.planar.maker")) == NULL)
1620c94d6dbeSJung-uk Kim 		break;
16211def0ca6SJung-uk Kim 	    if (strncmp(cp, "Compal", 6) != 0) {
16221def0ca6SJung-uk Kim 		freeenv(cp);
1623c94d6dbeSJung-uk Kim 		break;
16241def0ca6SJung-uk Kim 	    }
16251def0ca6SJung-uk Kim 	    freeenv(cp);
16262be111bfSDavide Italiano 	    if ((cp = kern_getenv("smbios.planar.product")) == NULL)
16271def0ca6SJung-uk Kim 		break;
16281def0ca6SJung-uk Kim 	    if (strncmp(cp, "08A0", 4) != 0) {
16291def0ca6SJung-uk Kim 		freeenv(cp);
16301def0ca6SJung-uk Kim 		break;
16311def0ca6SJung-uk Kim 	    }
16321def0ca6SJung-uk Kim 	    freeenv(cp);
16334edef187SJohn Baldwin 	    if (sc->bus.sub < 0xa) {
1634c94d6dbeSJung-uk Kim 		pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
16354edef187SJohn Baldwin 		sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
1636c94d6dbeSJung-uk Kim 	    }
1637c94d6dbeSJung-uk Kim 	    break;
1638c94d6dbeSJung-uk Kim 	}
16394edef187SJohn Baldwin #endif
1640e4b59fc5SWarner Losh     }
1641e4b59fc5SWarner Losh 
164222bf1c7fSJohn Baldwin     if (pci_msi_device_blacklisted(dev))
164322bf1c7fSJohn Baldwin 	sc->flags |= PCIB_DISABLE_MSI;
164422bf1c7fSJohn Baldwin 
164568e9cbd3SMarius Strobl     if (pci_msix_device_blacklisted(dev))
164668e9cbd3SMarius Strobl 	sc->flags |= PCIB_DISABLE_MSIX;
164768e9cbd3SMarius Strobl 
1648e4b59fc5SWarner Losh     /*
1649e4b59fc5SWarner Losh      * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
1650e4b59fc5SWarner Losh      * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
1651e4b59fc5SWarner Losh      * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
1652e4b59fc5SWarner Losh      * This means they act as if they were subtractively decoding
1653e4b59fc5SWarner Losh      * bridges and pass all transactions.  Mark them and real ProgIf 1
1654e4b59fc5SWarner Losh      * parts as subtractive.
1655e4b59fc5SWarner Losh      */
1656e4b59fc5SWarner Losh     if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
1657657d9f9fSJohn Baldwin       pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
1658e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
1659e4b59fc5SWarner Losh 
166082cb5c3bSJohn Baldwin #ifdef PCI_HP
166182cb5c3bSJohn Baldwin     pcib_probe_hotplug(sc);
166282cb5c3bSJohn Baldwin #endif
166383c41143SJohn Baldwin #ifdef NEW_PCIB
16644edef187SJohn Baldwin #ifdef PCI_RES_BUS
16654edef187SJohn Baldwin     pcib_setup_secbus(dev, &sc->bus, 1);
16664edef187SJohn Baldwin #endif
166783c41143SJohn Baldwin     pcib_probe_windows(sc);
166883c41143SJohn Baldwin #endif
166982cb5c3bSJohn Baldwin #ifdef PCI_HP
167082cb5c3bSJohn Baldwin     if (sc->flags & PCIB_HOTPLUG)
167182cb5c3bSJohn Baldwin 	    pcib_setup_hotplug(sc);
167282cb5c3bSJohn Baldwin #endif
1673bb0d0a8eSMike Smith     if (bootverbose) {
167455aaf894SMarius Strobl 	device_printf(dev, "  domain            %d\n", sc->domain);
16754edef187SJohn Baldwin 	device_printf(dev, "  secondary bus     %d\n", sc->bus.sec);
16764edef187SJohn Baldwin 	device_printf(dev, "  subordinate bus   %d\n", sc->bus.sub);
167783c41143SJohn Baldwin #ifdef NEW_PCIB
167883c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->io))
167983c41143SJohn Baldwin 	    device_printf(dev, "  I/O decode        0x%jx-0x%jx\n",
168083c41143SJohn Baldwin 	      (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit);
168183c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->mem))
168283c41143SJohn Baldwin 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
168383c41143SJohn Baldwin 	      (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit);
168483c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->pmem))
168583c41143SJohn Baldwin 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
168683c41143SJohn Baldwin 	      (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit);
168783c41143SJohn Baldwin #else
168883c41143SJohn Baldwin 	if (pcib_is_io_open(sc))
168983c41143SJohn Baldwin 	    device_printf(dev, "  I/O decode        0x%x-0x%x\n",
169083c41143SJohn Baldwin 	      sc->iobase, sc->iolimit);
1691b0a2d4b8SWarner Losh 	if (pcib_is_nonprefetch_open(sc))
1692b0a2d4b8SWarner Losh 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
1693b0a2d4b8SWarner Losh 	      (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
1694b0a2d4b8SWarner Losh 	if (pcib_is_prefetch_open(sc))
1695b0a2d4b8SWarner Losh 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
1696b0a2d4b8SWarner Losh 	      (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
169783c41143SJohn Baldwin #endif
1698c825d4dcSJohn Baldwin 	if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) ||
1699c825d4dcSJohn Baldwin 	    sc->flags & PCIB_SUBTRACTIVE) {
1700c825d4dcSJohn Baldwin 		device_printf(dev, "  special decode    ");
1701c825d4dcSJohn Baldwin 		comma = 0;
1702c825d4dcSJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) {
1703c825d4dcSJohn Baldwin 			printf("ISA");
1704c825d4dcSJohn Baldwin 			comma = 1;
1705c825d4dcSJohn Baldwin 		}
1706c825d4dcSJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) {
1707c825d4dcSJohn Baldwin 			printf("%sVGA", comma ? ", " : "");
1708c825d4dcSJohn Baldwin 			comma = 1;
1709c825d4dcSJohn Baldwin 		}
1710e4b59fc5SWarner Losh 		if (sc->flags & PCIB_SUBTRACTIVE)
1711c825d4dcSJohn Baldwin 			printf("%ssubtractive", comma ? ", " : "");
1712c825d4dcSJohn Baldwin 		printf("\n");
1713c825d4dcSJohn Baldwin 	}
1714bb0d0a8eSMike Smith     }
1715bb0d0a8eSMike Smith 
1716bb0d0a8eSMike Smith     /*
1717ef888152SJohn Baldwin      * Always enable busmastering on bridges so that transactions
1718ef888152SJohn Baldwin      * initiated on the secondary bus are passed through to the
1719ef888152SJohn Baldwin      * primary bus.
1720ef888152SJohn Baldwin      */
1721ef888152SJohn Baldwin     pci_enable_busmaster(dev);
17226f0d5884SJohn Baldwin }
1723bb0d0a8eSMike Smith 
172482cb5c3bSJohn Baldwin #ifdef PCI_HP
172582cb5c3bSJohn Baldwin static int
172682cb5c3bSJohn Baldwin pcib_present(struct pcib_softc *sc)
172782cb5c3bSJohn Baldwin {
172882cb5c3bSJohn Baldwin 
172982cb5c3bSJohn Baldwin 	if (sc->flags & PCIB_HOTPLUG)
173082cb5c3bSJohn Baldwin 		return (pcib_hotplug_present(sc) != 0);
173182cb5c3bSJohn Baldwin 	return (1);
173282cb5c3bSJohn Baldwin }
173382cb5c3bSJohn Baldwin #endif
173482cb5c3bSJohn Baldwin 
173538906aedSJohn Baldwin int
173667e7d085SJohn Baldwin pcib_attach_child(device_t dev)
17376f0d5884SJohn Baldwin {
17386f0d5884SJohn Baldwin 	struct pcib_softc *sc;
17396f0d5884SJohn Baldwin 
17406f0d5884SJohn Baldwin 	sc = device_get_softc(dev);
174167e7d085SJohn Baldwin 	if (sc->bus.sec == 0) {
174267e7d085SJohn Baldwin 		/* no secondary bus; we should have fixed this */
174367e7d085SJohn Baldwin 		return(0);
174467e7d085SJohn Baldwin 	}
174567e7d085SJohn Baldwin 
174682cb5c3bSJohn Baldwin #ifdef PCI_HP
174782cb5c3bSJohn Baldwin 	if (!pcib_present(sc)) {
174882cb5c3bSJohn Baldwin 		/* An empty HotPlug slot, so don't add a PCI bus yet. */
174982cb5c3bSJohn Baldwin 		return (0);
175082cb5c3bSJohn Baldwin 	}
175182cb5c3bSJohn Baldwin #endif
175282cb5c3bSJohn Baldwin 
175367e7d085SJohn Baldwin 	sc->child = device_add_child(dev, "pci", -1);
1754bb0d0a8eSMike Smith 	return (bus_generic_attach(dev));
1755bb0d0a8eSMike Smith }
1756bb0d0a8eSMike Smith 
175767e7d085SJohn Baldwin int
175867e7d085SJohn Baldwin pcib_attach(device_t dev)
175967e7d085SJohn Baldwin {
176067e7d085SJohn Baldwin 
176167e7d085SJohn Baldwin     pcib_attach_common(dev);
176267e7d085SJohn Baldwin     return (pcib_attach_child(dev));
1763bb0d0a8eSMike Smith }
1764bb0d0a8eSMike Smith 
17656f0d5884SJohn Baldwin int
17666f33eaa5SJohn Baldwin pcib_detach(device_t dev)
17676f33eaa5SJohn Baldwin {
17686f33eaa5SJohn Baldwin #if defined(PCI_HP) || defined(NEW_PCIB)
17696f33eaa5SJohn Baldwin 	struct pcib_softc *sc;
17706f33eaa5SJohn Baldwin #endif
17716f33eaa5SJohn Baldwin 	int error;
17726f33eaa5SJohn Baldwin 
17736f33eaa5SJohn Baldwin #if defined(PCI_HP) || defined(NEW_PCIB)
17746f33eaa5SJohn Baldwin 	sc = device_get_softc(dev);
17756f33eaa5SJohn Baldwin #endif
17766f33eaa5SJohn Baldwin 	error = bus_generic_detach(dev);
17776f33eaa5SJohn Baldwin 	if (error)
17786f33eaa5SJohn Baldwin 		return (error);
17796f33eaa5SJohn Baldwin #ifdef PCI_HP
17806f33eaa5SJohn Baldwin 	if (sc->flags & PCIB_HOTPLUG) {
17816f33eaa5SJohn Baldwin 		error = pcib_detach_hotplug(sc);
17826f33eaa5SJohn Baldwin 		if (error)
17836f33eaa5SJohn Baldwin 			return (error);
17846f33eaa5SJohn Baldwin 	}
17856f33eaa5SJohn Baldwin #endif
17866f33eaa5SJohn Baldwin 	error = device_delete_children(dev);
17876f33eaa5SJohn Baldwin 	if (error)
17886f33eaa5SJohn Baldwin 		return (error);
17896f33eaa5SJohn Baldwin #ifdef NEW_PCIB
17906f33eaa5SJohn Baldwin 	pcib_free_windows(sc);
17916f33eaa5SJohn Baldwin #ifdef PCI_RES_BUS
17926f33eaa5SJohn Baldwin 	pcib_free_secbus(dev, &sc->bus);
17936f33eaa5SJohn Baldwin #endif
17946f33eaa5SJohn Baldwin #endif
17956f33eaa5SJohn Baldwin 	return (0);
17966f33eaa5SJohn Baldwin }
17976f33eaa5SJohn Baldwin 
17986f33eaa5SJohn Baldwin int
1799e36af292SJung-uk Kim pcib_suspend(device_t dev)
1800e36af292SJung-uk Kim {
1801e36af292SJung-uk Kim 
1802e36af292SJung-uk Kim 	pcib_cfg_save(device_get_softc(dev));
18037212fc6aSJohn Baldwin 	return (bus_generic_suspend(dev));
1804e36af292SJung-uk Kim }
1805e36af292SJung-uk Kim 
1806e36af292SJung-uk Kim int
1807e36af292SJung-uk Kim pcib_resume(device_t dev)
1808e36af292SJung-uk Kim {
1809e36af292SJung-uk Kim 
1810e36af292SJung-uk Kim 	pcib_cfg_restore(device_get_softc(dev));
1811cffd37daSAndriy Gapon 
1812cffd37daSAndriy Gapon 	/*
1813cffd37daSAndriy Gapon 	 * Restore the Command register only after restoring the windows.
1814cffd37daSAndriy Gapon 	 * The bridge should not be claiming random windows.
1815cffd37daSAndriy Gapon 	 */
1816cffd37daSAndriy Gapon 	pci_write_config(dev, PCIR_COMMAND, pci_get_cmdreg(dev), 2);
1817e36af292SJung-uk Kim 	return (bus_generic_resume(dev));
1818e36af292SJung-uk Kim }
1819e36af292SJung-uk Kim 
1820809923caSJustin Hibbits void
1821809923caSJustin Hibbits pcib_bridge_init(device_t dev)
1822809923caSJustin Hibbits {
1823809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
1824809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2);
1825809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1);
1826809923caSJustin Hibbits 	pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2);
1827809923caSJustin Hibbits 	pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2);
1828809923caSJustin Hibbits 	pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2);
1829809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
1830809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4);
1831809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2);
1832809923caSJustin Hibbits 	pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4);
1833809923caSJustin Hibbits }
1834809923caSJustin Hibbits 
1835e36af292SJung-uk Kim int
183682cb5c3bSJohn Baldwin pcib_child_present(device_t dev, device_t child)
183782cb5c3bSJohn Baldwin {
183882cb5c3bSJohn Baldwin #ifdef PCI_HP
183982cb5c3bSJohn Baldwin 	struct pcib_softc *sc = device_get_softc(dev);
184082cb5c3bSJohn Baldwin 	int retval;
184182cb5c3bSJohn Baldwin 
184282cb5c3bSJohn Baldwin 	retval = bus_child_present(dev);
184382cb5c3bSJohn Baldwin 	if (retval != 0 && sc->flags & PCIB_HOTPLUG)
184482cb5c3bSJohn Baldwin 		retval = pcib_hotplug_present(sc);
184582cb5c3bSJohn Baldwin 	return (retval);
184682cb5c3bSJohn Baldwin #else
184782cb5c3bSJohn Baldwin 	return (bus_child_present(dev));
184882cb5c3bSJohn Baldwin #endif
184982cb5c3bSJohn Baldwin }
185082cb5c3bSJohn Baldwin 
185182cb5c3bSJohn Baldwin int
1852bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1853bb0d0a8eSMike Smith {
1854bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
1855bb0d0a8eSMike Smith 
1856bb0d0a8eSMike Smith     switch (which) {
185755aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
185855aaf894SMarius Strobl 	*result = sc->domain;
185955aaf894SMarius Strobl 	return(0);
1860bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
18614edef187SJohn Baldwin 	*result = sc->bus.sec;
1862bb0d0a8eSMike Smith 	return(0);
1863bb0d0a8eSMike Smith     }
1864bb0d0a8eSMike Smith     return(ENOENT);
1865bb0d0a8eSMike Smith }
1866bb0d0a8eSMike Smith 
18676f0d5884SJohn Baldwin int
1868bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1869bb0d0a8eSMike Smith {
1870bb0d0a8eSMike Smith 
1871bb0d0a8eSMike Smith     switch (which) {
187255aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
187355aaf894SMarius Strobl 	return(EINVAL);
1874bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
18754edef187SJohn Baldwin 	return(EINVAL);
1876bb0d0a8eSMike Smith     }
1877bb0d0a8eSMike Smith     return(ENOENT);
1878bb0d0a8eSMike Smith }
1879bb0d0a8eSMike Smith 
188083c41143SJohn Baldwin #ifdef NEW_PCIB
188183c41143SJohn Baldwin /*
188283c41143SJohn Baldwin  * Attempt to allocate a resource from the existing resources assigned
188383c41143SJohn Baldwin  * to a window.
188483c41143SJohn Baldwin  */
188583c41143SJohn Baldwin static struct resource *
188683c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w,
18872dd1bdf1SJustin Hibbits     device_t child, int type, int *rid, rman_res_t start, rman_res_t end,
18882dd1bdf1SJustin Hibbits     rman_res_t count, u_int flags)
188983c41143SJohn Baldwin {
189083c41143SJohn Baldwin 	struct resource *res;
189183c41143SJohn Baldwin 
189283c41143SJohn Baldwin 	if (!pcib_is_window_open(w))
189383c41143SJohn Baldwin 		return (NULL);
189483c41143SJohn Baldwin 
189583c41143SJohn Baldwin 	res = rman_reserve_resource(&w->rman, start, end, count,
189683c41143SJohn Baldwin 	    flags & ~RF_ACTIVE, child);
189783c41143SJohn Baldwin 	if (res == NULL)
189883c41143SJohn Baldwin 		return (NULL);
189983c41143SJohn Baldwin 
190083c41143SJohn Baldwin 	if (bootverbose)
190183c41143SJohn Baldwin 		device_printf(sc->dev,
1902da1b038aSJustin Hibbits 		    "allocated %s range (%#jx-%#jx) for rid %x of %s\n",
190383c41143SJohn Baldwin 		    w->name, rman_get_start(res), rman_get_end(res), *rid,
190483c41143SJohn Baldwin 		    pcib_child_name(child));
190583c41143SJohn Baldwin 	rman_set_rid(res, *rid);
190683c41143SJohn Baldwin 
190783c41143SJohn Baldwin 	/*
190883c41143SJohn Baldwin 	 * If the resource should be active, pass that request up the
190983c41143SJohn Baldwin 	 * tree.  This assumes the parent drivers can handle
191083c41143SJohn Baldwin 	 * activating sub-allocated resources.
191183c41143SJohn Baldwin 	 */
191283c41143SJohn Baldwin 	if (flags & RF_ACTIVE) {
191383c41143SJohn Baldwin 		if (bus_activate_resource(child, type, *rid, res) != 0) {
191483c41143SJohn Baldwin 			rman_release_resource(res);
191583c41143SJohn Baldwin 			return (NULL);
191683c41143SJohn Baldwin 		}
191783c41143SJohn Baldwin 	}
191883c41143SJohn Baldwin 
191983c41143SJohn Baldwin 	return (res);
192083c41143SJohn Baldwin }
192183c41143SJohn Baldwin 
1922c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */
1923c825d4dcSJohn Baldwin static int
1924c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type,
19252dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1926c825d4dcSJohn Baldwin {
1927c825d4dcSJohn Baldwin 	struct resource *res;
19282dd1bdf1SJustin Hibbits 	rman_res_t base, limit, wmask;
1929c825d4dcSJohn Baldwin 	int rid;
1930c825d4dcSJohn Baldwin 
1931c825d4dcSJohn Baldwin 	/*
1932c825d4dcSJohn Baldwin 	 * If this is an I/O window on a bridge with ISA enable set
1933c825d4dcSJohn Baldwin 	 * and the start address is below 64k, then try to allocate an
1934c825d4dcSJohn Baldwin 	 * initial window of 0x1000 bytes long starting at address
1935c825d4dcSJohn Baldwin 	 * 0xf000 and walking down.  Note that if the original request
1936c825d4dcSJohn Baldwin 	 * was larger than the non-aliased range size of 0x100 our
1937c825d4dcSJohn Baldwin 	 * caller would have raised the start address up to 64k
1938c825d4dcSJohn Baldwin 	 * already.
1939c825d4dcSJohn Baldwin 	 */
1940c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1941c825d4dcSJohn Baldwin 	    start < 65536) {
1942c825d4dcSJohn Baldwin 		for (base = 0xf000; (long)base >= 0; base -= 0x1000) {
1943c825d4dcSJohn Baldwin 			limit = base + 0xfff;
1944c825d4dcSJohn Baldwin 
1945c825d4dcSJohn Baldwin 			/*
1946c825d4dcSJohn Baldwin 			 * Skip ranges that wouldn't work for the
1947c825d4dcSJohn Baldwin 			 * original request.  Note that the actual
1948c825d4dcSJohn Baldwin 			 * window that overlaps are the non-alias
1949c825d4dcSJohn Baldwin 			 * ranges within [base, limit], so this isn't
1950c825d4dcSJohn Baldwin 			 * quite a simple comparison.
1951c825d4dcSJohn Baldwin 			 */
1952c825d4dcSJohn Baldwin 			if (start + count > limit - 0x400)
1953c825d4dcSJohn Baldwin 				continue;
1954c825d4dcSJohn Baldwin 			if (base == 0) {
1955c825d4dcSJohn Baldwin 				/*
1956c825d4dcSJohn Baldwin 				 * The first open region for the window at
1957c825d4dcSJohn Baldwin 				 * 0 is 0x400-0x4ff.
1958c825d4dcSJohn Baldwin 				 */
1959c825d4dcSJohn Baldwin 				if (end - count + 1 < 0x400)
1960c825d4dcSJohn Baldwin 					continue;
1961c825d4dcSJohn Baldwin 			} else {
1962c825d4dcSJohn Baldwin 				if (end - count + 1 < base)
1963c825d4dcSJohn Baldwin 					continue;
1964c825d4dcSJohn Baldwin 			}
1965c825d4dcSJohn Baldwin 
1966c825d4dcSJohn Baldwin 			if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) {
1967c825d4dcSJohn Baldwin 				w->base = base;
1968c825d4dcSJohn Baldwin 				w->limit = limit;
1969c825d4dcSJohn Baldwin 				return (0);
1970c825d4dcSJohn Baldwin 			}
1971c825d4dcSJohn Baldwin 		}
1972c825d4dcSJohn Baldwin 		return (ENOSPC);
1973c825d4dcSJohn Baldwin 	}
1974c825d4dcSJohn Baldwin 
197589977ce2SJustin Hibbits 	wmask = ((rman_res_t)1 << w->step) - 1;
1976c825d4dcSJohn Baldwin 	if (RF_ALIGNMENT(flags) < w->step) {
1977c825d4dcSJohn Baldwin 		flags &= ~RF_ALIGNMENT_MASK;
1978c825d4dcSJohn Baldwin 		flags |= RF_ALIGNMENT_LOG2(w->step);
1979c825d4dcSJohn Baldwin 	}
1980c825d4dcSJohn Baldwin 	start &= ~wmask;
1981c825d4dcSJohn Baldwin 	end |= wmask;
198289977ce2SJustin Hibbits 	count = roundup2(count, (rman_res_t)1 << w->step);
1983c825d4dcSJohn Baldwin 	rid = w->reg;
1984c825d4dcSJohn Baldwin 	res = bus_alloc_resource(sc->dev, type, &rid, start, end, count,
1985c825d4dcSJohn Baldwin 	    flags & ~RF_ACTIVE);
1986c825d4dcSJohn Baldwin 	if (res == NULL)
1987c825d4dcSJohn Baldwin 		return (ENOSPC);
1988c825d4dcSJohn Baldwin 	pcib_add_window_resources(w, &res, 1);
1989c825d4dcSJohn Baldwin 	pcib_activate_window(sc, type);
1990c825d4dcSJohn Baldwin 	w->base = rman_get_start(res);
1991c825d4dcSJohn Baldwin 	w->limit = rman_get_end(res);
1992c825d4dcSJohn Baldwin 	return (0);
1993c825d4dcSJohn Baldwin }
1994c825d4dcSJohn Baldwin 
1995c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */
1996c825d4dcSJohn Baldwin static int
1997c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type,
19982dd1bdf1SJustin Hibbits     rman_res_t base, rman_res_t limit)
1999c825d4dcSJohn Baldwin {
2000c825d4dcSJohn Baldwin 	struct resource *res;
2001c825d4dcSJohn Baldwin 	int error, i, force_64k_base;
2002c825d4dcSJohn Baldwin 
2003c825d4dcSJohn Baldwin 	KASSERT(base <= w->base && limit >= w->limit,
2004c825d4dcSJohn Baldwin 	    ("attempting to shrink window"));
2005c825d4dcSJohn Baldwin 
2006c825d4dcSJohn Baldwin 	/*
2007c825d4dcSJohn Baldwin 	 * XXX: pcib_grow_window() doesn't try to do this anyway and
2008c825d4dcSJohn Baldwin 	 * the error handling for all the edge cases would be tedious.
2009c825d4dcSJohn Baldwin 	 */
2010c825d4dcSJohn Baldwin 	KASSERT(limit == w->limit || base == w->base,
2011c825d4dcSJohn Baldwin 	    ("attempting to grow both ends of a window"));
2012c825d4dcSJohn Baldwin 
2013c825d4dcSJohn Baldwin 	/*
2014c825d4dcSJohn Baldwin 	 * Yet more special handling for requests to expand an I/O
2015c825d4dcSJohn Baldwin 	 * window behind an ISA-enabled bridge.  Since I/O windows
2016c825d4dcSJohn Baldwin 	 * have to grow in 0x1000 increments and the end of the 0xffff
2017c825d4dcSJohn Baldwin 	 * range is an alias, growing a window below 64k will always
2018c825d4dcSJohn Baldwin 	 * result in allocating new resources and never adjusting an
2019c825d4dcSJohn Baldwin 	 * existing resource.
2020c825d4dcSJohn Baldwin 	 */
2021c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
2022c825d4dcSJohn Baldwin 	    (limit <= 65535 || (base <= 65535 && base != w->base))) {
2023c825d4dcSJohn Baldwin 		KASSERT(limit == w->limit || limit <= 65535,
2024c825d4dcSJohn Baldwin 		    ("attempting to grow both ends across 64k ISA alias"));
2025c825d4dcSJohn Baldwin 
2026c825d4dcSJohn Baldwin 		if (base != w->base)
2027c825d4dcSJohn Baldwin 			error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1);
2028c825d4dcSJohn Baldwin 		else
2029c825d4dcSJohn Baldwin 			error = pcib_alloc_nonisa_ranges(sc, w->limit + 1,
2030c825d4dcSJohn Baldwin 			    limit);
2031c825d4dcSJohn Baldwin 		if (error == 0) {
2032c825d4dcSJohn Baldwin 			w->base = base;
2033c825d4dcSJohn Baldwin 			w->limit = limit;
2034c825d4dcSJohn Baldwin 		}
2035c825d4dcSJohn Baldwin 		return (error);
2036c825d4dcSJohn Baldwin 	}
2037c825d4dcSJohn Baldwin 
2038c825d4dcSJohn Baldwin 	/*
2039c825d4dcSJohn Baldwin 	 * Find the existing resource to adjust.  Usually there is only one,
2040c825d4dcSJohn Baldwin 	 * but for an ISA-enabled bridge we might be growing the I/O window
2041c825d4dcSJohn Baldwin 	 * above 64k and need to find the existing resource that maps all
2042c825d4dcSJohn Baldwin 	 * of the area above 64k.
2043c825d4dcSJohn Baldwin 	 */
2044c825d4dcSJohn Baldwin 	for (i = 0; i < w->count; i++) {
2045c825d4dcSJohn Baldwin 		if (rman_get_end(w->res[i]) == w->limit)
2046c825d4dcSJohn Baldwin 			break;
2047c825d4dcSJohn Baldwin 	}
2048c825d4dcSJohn Baldwin 	KASSERT(i != w->count, ("did not find existing resource"));
2049c825d4dcSJohn Baldwin 	res = w->res[i];
2050c825d4dcSJohn Baldwin 
2051c825d4dcSJohn Baldwin 	/*
2052c825d4dcSJohn Baldwin 	 * Usually the resource we found should match the window's
2053c825d4dcSJohn Baldwin 	 * existing range.  The one exception is the ISA-enabled case
2054c825d4dcSJohn Baldwin 	 * mentioned above in which case the resource should start at
2055c825d4dcSJohn Baldwin 	 * 64k.
2056c825d4dcSJohn Baldwin 	 */
2057c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
2058c825d4dcSJohn Baldwin 	    w->base <= 65535) {
2059c825d4dcSJohn Baldwin 		KASSERT(rman_get_start(res) == 65536,
2060c825d4dcSJohn Baldwin 		    ("existing resource mismatch"));
2061c825d4dcSJohn Baldwin 		force_64k_base = 1;
2062c825d4dcSJohn Baldwin 	} else {
2063c825d4dcSJohn Baldwin 		KASSERT(w->base == rman_get_start(res),
2064c825d4dcSJohn Baldwin 		    ("existing resource mismatch"));
2065c825d4dcSJohn Baldwin 		force_64k_base = 0;
2066c825d4dcSJohn Baldwin 	}
2067c825d4dcSJohn Baldwin 
2068c825d4dcSJohn Baldwin 	error = bus_adjust_resource(sc->dev, type, res, force_64k_base ?
2069c825d4dcSJohn Baldwin 	    rman_get_start(res) : base, limit);
2070c825d4dcSJohn Baldwin 	if (error)
2071c825d4dcSJohn Baldwin 		return (error);
2072c825d4dcSJohn Baldwin 
2073c825d4dcSJohn Baldwin 	/* Add the newly allocated region to the resource manager. */
2074c825d4dcSJohn Baldwin 	if (w->base != base) {
2075c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, base, w->base - 1);
2076c825d4dcSJohn Baldwin 		w->base = base;
2077c825d4dcSJohn Baldwin 	} else {
2078c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, w->limit + 1, limit);
2079c825d4dcSJohn Baldwin 		w->limit = limit;
2080c825d4dcSJohn Baldwin 	}
2081c825d4dcSJohn Baldwin 	if (error) {
2082c825d4dcSJohn Baldwin 		if (bootverbose)
2083c825d4dcSJohn Baldwin 			device_printf(sc->dev,
2084c825d4dcSJohn Baldwin 			    "failed to expand %s resource manager\n", w->name);
2085c825d4dcSJohn Baldwin 		(void)bus_adjust_resource(sc->dev, type, res, force_64k_base ?
2086c825d4dcSJohn Baldwin 		    rman_get_start(res) : w->base, w->limit);
2087c825d4dcSJohn Baldwin 	}
2088c825d4dcSJohn Baldwin 	return (error);
2089c825d4dcSJohn Baldwin }
2090c825d4dcSJohn Baldwin 
209183c41143SJohn Baldwin /*
209283c41143SJohn Baldwin  * Attempt to grow a window to make room for a given resource request.
209383c41143SJohn Baldwin  */
209483c41143SJohn Baldwin static int
209583c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type,
20962dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
209783c41143SJohn Baldwin {
20982dd1bdf1SJustin Hibbits 	rman_res_t align, start_free, end_free, front, back, wmask;
2099c825d4dcSJohn Baldwin 	int error;
210083c41143SJohn Baldwin 
210183c41143SJohn Baldwin 	/*
210283c41143SJohn Baldwin 	 * Clamp the desired resource range to the maximum address
210383c41143SJohn Baldwin 	 * this window supports.  Reject impossible requests.
2104c825d4dcSJohn Baldwin 	 *
2105c825d4dcSJohn Baldwin 	 * For I/O port requests behind a bridge with the ISA enable
2106c825d4dcSJohn Baldwin 	 * bit set, force large allocations to start above 64k.
210783c41143SJohn Baldwin 	 */
210883c41143SJohn Baldwin 	if (!w->valid)
210983c41143SJohn Baldwin 		return (EINVAL);
2110c825d4dcSJohn Baldwin 	if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 &&
2111c825d4dcSJohn Baldwin 	    start < 65536)
2112c825d4dcSJohn Baldwin 		start = 65536;
211383c41143SJohn Baldwin 	if (end > w->rman.rm_end)
211483c41143SJohn Baldwin 		end = w->rman.rm_end;
211583c41143SJohn Baldwin 	if (start + count - 1 > end || start + count < start)
211683c41143SJohn Baldwin 		return (EINVAL);
211789977ce2SJustin Hibbits 	wmask = ((rman_res_t)1 << w->step) - 1;
211883c41143SJohn Baldwin 
211983c41143SJohn Baldwin 	/*
212083c41143SJohn Baldwin 	 * If there is no resource at all, just try to allocate enough
212183c41143SJohn Baldwin 	 * aligned space for this resource.
212283c41143SJohn Baldwin 	 */
212383c41143SJohn Baldwin 	if (w->res == NULL) {
2124c825d4dcSJohn Baldwin 		error = pcib_alloc_new_window(sc, w, type, start, end, count,
2125c825d4dcSJohn Baldwin 		    flags);
2126c825d4dcSJohn Baldwin 		if (error) {
212783c41143SJohn Baldwin 			if (bootverbose)
212883c41143SJohn Baldwin 				device_printf(sc->dev,
2129da1b038aSJustin Hibbits 		    "failed to allocate initial %s window (%#jx-%#jx,%#jx)\n",
213083c41143SJohn Baldwin 				    w->name, start, end, count);
213183c41143SJohn Baldwin 			return (error);
213283c41143SJohn Baldwin 		}
2133c825d4dcSJohn Baldwin 		if (bootverbose)
2134c825d4dcSJohn Baldwin 			device_printf(sc->dev,
2135c825d4dcSJohn Baldwin 			    "allocated initial %s window of %#jx-%#jx\n",
2136c825d4dcSJohn Baldwin 			    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
213783c41143SJohn Baldwin 		goto updatewin;
213883c41143SJohn Baldwin 	}
213983c41143SJohn Baldwin 
214083c41143SJohn Baldwin 	/*
214183c41143SJohn Baldwin 	 * See if growing the window would help.  Compute the minimum
214283c41143SJohn Baldwin 	 * amount of address space needed on both the front and back
214383c41143SJohn Baldwin 	 * ends of the existing window to satisfy the allocation.
214483c41143SJohn Baldwin 	 *
214583c41143SJohn Baldwin 	 * For each end, build a candidate region adjusting for the
214683c41143SJohn Baldwin 	 * required alignment, etc.  If there is a free region at the
214783c41143SJohn Baldwin 	 * edge of the window, grow from the inner edge of the free
214883c41143SJohn Baldwin 	 * region.  Otherwise grow from the window boundary.
214983c41143SJohn Baldwin 	 *
2150c825d4dcSJohn Baldwin 	 * Growing an I/O window below 64k for a bridge with the ISA
2151c825d4dcSJohn Baldwin 	 * enable bit doesn't require any special magic as the step
2152c825d4dcSJohn Baldwin 	 * size of an I/O window (1k) always includes multiple
2153c825d4dcSJohn Baldwin 	 * non-alias ranges when it is grown in either direction.
2154c825d4dcSJohn Baldwin 	 *
215583c41143SJohn Baldwin 	 * XXX: Special case: if w->res is completely empty and the
215683c41143SJohn Baldwin 	 * request size is larger than w->res, we should find the
215783c41143SJohn Baldwin 	 * optimal aligned buffer containing w->res and allocate that.
215883c41143SJohn Baldwin 	 */
215983c41143SJohn Baldwin 	if (bootverbose)
216083c41143SJohn Baldwin 		device_printf(sc->dev,
2161da1b038aSJustin Hibbits 		    "attempting to grow %s window for (%#jx-%#jx,%#jx)\n",
216283c41143SJohn Baldwin 		    w->name, start, end, count);
216389977ce2SJustin Hibbits 	align = (rman_res_t)1 << RF_ALIGNMENT(flags);
2164c825d4dcSJohn Baldwin 	if (start < w->base) {
216583c41143SJohn Baldwin 		if (rman_first_free_region(&w->rman, &start_free, &end_free) !=
2166c825d4dcSJohn Baldwin 		    0 || start_free != w->base)
2167c825d4dcSJohn Baldwin 			end_free = w->base;
216883c41143SJohn Baldwin 		if (end_free > end)
2169ddac8cc9SJohn Baldwin 			end_free = end + 1;
217083c41143SJohn Baldwin 
217183c41143SJohn Baldwin 		/* Move end_free down until it is properly aligned. */
217283c41143SJohn Baldwin 		end_free &= ~(align - 1);
2173a49dcb46SJohn Baldwin 		end_free--;
2174a49dcb46SJohn Baldwin 		front = end_free - (count - 1);
217583c41143SJohn Baldwin 
217683c41143SJohn Baldwin 		/*
217783c41143SJohn Baldwin 		 * The resource would now be allocated at (front,
217883c41143SJohn Baldwin 		 * end_free).  Ensure that fits in the (start, end)
217983c41143SJohn Baldwin 		 * bounds.  end_free is checked above.  If 'front' is
218083c41143SJohn Baldwin 		 * ok, ensure it is properly aligned for this window.
218183c41143SJohn Baldwin 		 * Also check for underflow.
218283c41143SJohn Baldwin 		 */
218383c41143SJohn Baldwin 		if (front >= start && front <= end_free) {
218483c41143SJohn Baldwin 			if (bootverbose)
2185da1b038aSJustin Hibbits 				printf("\tfront candidate range: %#jx-%#jx\n",
218683c41143SJohn Baldwin 				    front, end_free);
2187a7b5acacSJohn Baldwin 			front &= ~wmask;
2188c825d4dcSJohn Baldwin 			front = w->base - front;
218983c41143SJohn Baldwin 		} else
219083c41143SJohn Baldwin 			front = 0;
219183c41143SJohn Baldwin 	} else
219283c41143SJohn Baldwin 		front = 0;
2193c825d4dcSJohn Baldwin 	if (end > w->limit) {
219483c41143SJohn Baldwin 		if (rman_last_free_region(&w->rman, &start_free, &end_free) !=
2195c825d4dcSJohn Baldwin 		    0 || end_free != w->limit)
2196c825d4dcSJohn Baldwin 			start_free = w->limit + 1;
219783c41143SJohn Baldwin 		if (start_free < start)
219883c41143SJohn Baldwin 			start_free = start;
219983c41143SJohn Baldwin 
220083c41143SJohn Baldwin 		/* Move start_free up until it is properly aligned. */
220183c41143SJohn Baldwin 		start_free = roundup2(start_free, align);
2202a49dcb46SJohn Baldwin 		back = start_free + count - 1;
220383c41143SJohn Baldwin 
220483c41143SJohn Baldwin 		/*
220583c41143SJohn Baldwin 		 * The resource would now be allocated at (start_free,
220683c41143SJohn Baldwin 		 * back).  Ensure that fits in the (start, end)
220783c41143SJohn Baldwin 		 * bounds.  start_free is checked above.  If 'back' is
220883c41143SJohn Baldwin 		 * ok, ensure it is properly aligned for this window.
220983c41143SJohn Baldwin 		 * Also check for overflow.
221083c41143SJohn Baldwin 		 */
221183c41143SJohn Baldwin 		if (back <= end && start_free <= back) {
221283c41143SJohn Baldwin 			if (bootverbose)
2213da1b038aSJustin Hibbits 				printf("\tback candidate range: %#jx-%#jx\n",
221483c41143SJohn Baldwin 				    start_free, back);
2215a7b5acacSJohn Baldwin 			back |= wmask;
2216c825d4dcSJohn Baldwin 			back -= w->limit;
221783c41143SJohn Baldwin 		} else
221883c41143SJohn Baldwin 			back = 0;
221983c41143SJohn Baldwin 	} else
222083c41143SJohn Baldwin 		back = 0;
222183c41143SJohn Baldwin 
222283c41143SJohn Baldwin 	/*
222383c41143SJohn Baldwin 	 * Try to allocate the smallest needed region first.
222483c41143SJohn Baldwin 	 * If that fails, fall back to the other region.
222583c41143SJohn Baldwin 	 */
222683c41143SJohn Baldwin 	error = ENOSPC;
222783c41143SJohn Baldwin 	while (front != 0 || back != 0) {
222883c41143SJohn Baldwin 		if (front != 0 && (front <= back || back == 0)) {
2229c825d4dcSJohn Baldwin 			error = pcib_expand_window(sc, w, type, w->base - front,
2230c825d4dcSJohn Baldwin 			    w->limit);
223183c41143SJohn Baldwin 			if (error == 0)
223283c41143SJohn Baldwin 				break;
223383c41143SJohn Baldwin 			front = 0;
223483c41143SJohn Baldwin 		} else {
2235c825d4dcSJohn Baldwin 			error = pcib_expand_window(sc, w, type, w->base,
2236c825d4dcSJohn Baldwin 			    w->limit + back);
223783c41143SJohn Baldwin 			if (error == 0)
223883c41143SJohn Baldwin 				break;
223983c41143SJohn Baldwin 			back = 0;
224083c41143SJohn Baldwin 		}
224183c41143SJohn Baldwin 	}
224283c41143SJohn Baldwin 
224383c41143SJohn Baldwin 	if (error)
224483c41143SJohn Baldwin 		return (error);
224583c41143SJohn Baldwin 	if (bootverbose)
2246c825d4dcSJohn Baldwin 		device_printf(sc->dev, "grew %s window to %#jx-%#jx\n",
2247c825d4dcSJohn Baldwin 		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
224883c41143SJohn Baldwin 
224983c41143SJohn Baldwin updatewin:
2250c825d4dcSJohn Baldwin 	/* Write the new window. */
2251a7b5acacSJohn Baldwin 	KASSERT((w->base & wmask) == 0, ("start address is not aligned"));
2252a7b5acacSJohn Baldwin 	KASSERT((w->limit & wmask) == wmask, ("end address is not aligned"));
225383c41143SJohn Baldwin 	pcib_write_windows(sc, w->mask);
225483c41143SJohn Baldwin 	return (0);
225583c41143SJohn Baldwin }
225683c41143SJohn Baldwin 
225783c41143SJohn Baldwin /*
225883c41143SJohn Baldwin  * We have to trap resource allocation requests and ensure that the bridge
225983c41143SJohn Baldwin  * is set up to, or capable of handling them.
226083c41143SJohn Baldwin  */
226183c41143SJohn Baldwin struct resource *
226283c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
22632dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
226483c41143SJohn Baldwin {
226583c41143SJohn Baldwin 	struct pcib_softc *sc;
226683c41143SJohn Baldwin 	struct resource *r;
226783c41143SJohn Baldwin 
226883c41143SJohn Baldwin 	sc = device_get_softc(dev);
226983c41143SJohn Baldwin 
227083c41143SJohn Baldwin 	/*
227183c41143SJohn Baldwin 	 * VGA resources are decoded iff the VGA enable bit is set in
227283c41143SJohn Baldwin 	 * the bridge control register.  VGA resources do not fall into
227383c41143SJohn Baldwin 	 * the resource windows and are passed up to the parent.
227483c41143SJohn Baldwin 	 */
227583c41143SJohn Baldwin 	if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) ||
227683c41143SJohn Baldwin 	    (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) {
227783c41143SJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE)
227883c41143SJohn Baldwin 			return (bus_generic_alloc_resource(dev, child, type,
227983c41143SJohn Baldwin 			    rid, start, end, count, flags));
228083c41143SJohn Baldwin 		else
228183c41143SJohn Baldwin 			return (NULL);
228283c41143SJohn Baldwin 	}
228383c41143SJohn Baldwin 
228483c41143SJohn Baldwin 	switch (type) {
22854edef187SJohn Baldwin #ifdef PCI_RES_BUS
22864edef187SJohn Baldwin 	case PCI_RES_BUS:
22874edef187SJohn Baldwin 		return (pcib_alloc_subbus(&sc->bus, child, rid, start, end,
22884edef187SJohn Baldwin 		    count, flags));
22894edef187SJohn Baldwin #endif
229083c41143SJohn Baldwin 	case SYS_RES_IOPORT:
2291c825d4dcSJohn Baldwin 		if (pcib_is_isa_range(sc, start, end, count))
2292c825d4dcSJohn Baldwin 			return (NULL);
229383c41143SJohn Baldwin 		r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start,
229483c41143SJohn Baldwin 		    end, count, flags);
2295a6c82265SMarius Strobl 		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
229683c41143SJohn Baldwin 			break;
229783c41143SJohn Baldwin 		if (pcib_grow_window(sc, &sc->io, type, start, end, count,
229883c41143SJohn Baldwin 		    flags) == 0)
229983c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->io, child, type,
230083c41143SJohn Baldwin 			    rid, start, end, count, flags);
230183c41143SJohn Baldwin 		break;
230283c41143SJohn Baldwin 	case SYS_RES_MEMORY:
230383c41143SJohn Baldwin 		/*
230483c41143SJohn Baldwin 		 * For prefetchable resources, prefer the prefetchable
230583c41143SJohn Baldwin 		 * memory window, but fall back to the regular memory
230683c41143SJohn Baldwin 		 * window if that fails.  Try both windows before
230783c41143SJohn Baldwin 		 * attempting to grow a window in case the firmware
230883c41143SJohn Baldwin 		 * has used a range in the regular memory window to
230983c41143SJohn Baldwin 		 * map a prefetchable BAR.
231083c41143SJohn Baldwin 		 */
231183c41143SJohn Baldwin 		if (flags & RF_PREFETCHABLE) {
231283c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->pmem, child, type,
231383c41143SJohn Baldwin 			    rid, start, end, count, flags);
231483c41143SJohn Baldwin 			if (r != NULL)
231583c41143SJohn Baldwin 				break;
231683c41143SJohn Baldwin 		}
231783c41143SJohn Baldwin 		r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid,
231883c41143SJohn Baldwin 		    start, end, count, flags);
2319a6c82265SMarius Strobl 		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
232083c41143SJohn Baldwin 			break;
232183c41143SJohn Baldwin 		if (flags & RF_PREFETCHABLE) {
232283c41143SJohn Baldwin 			if (pcib_grow_window(sc, &sc->pmem, type, start, end,
232383c41143SJohn Baldwin 			    count, flags) == 0) {
232483c41143SJohn Baldwin 				r = pcib_suballoc_resource(sc, &sc->pmem, child,
232583c41143SJohn Baldwin 				    type, rid, start, end, count, flags);
232683c41143SJohn Baldwin 				if (r != NULL)
232783c41143SJohn Baldwin 					break;
232883c41143SJohn Baldwin 			}
232983c41143SJohn Baldwin 		}
233083c41143SJohn Baldwin 		if (pcib_grow_window(sc, &sc->mem, type, start, end, count,
233183c41143SJohn Baldwin 		    flags & ~RF_PREFETCHABLE) == 0)
233283c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->mem, child, type,
233383c41143SJohn Baldwin 			    rid, start, end, count, flags);
233483c41143SJohn Baldwin 		break;
233583c41143SJohn Baldwin 	default:
233683c41143SJohn Baldwin 		return (bus_generic_alloc_resource(dev, child, type, rid,
233783c41143SJohn Baldwin 		    start, end, count, flags));
233883c41143SJohn Baldwin 	}
233983c41143SJohn Baldwin 
234083c41143SJohn Baldwin 	/*
234183c41143SJohn Baldwin 	 * If attempts to suballocate from the window fail but this is a
234283c41143SJohn Baldwin 	 * subtractive bridge, pass the request up the tree.
234383c41143SJohn Baldwin 	 */
234483c41143SJohn Baldwin 	if (sc->flags & PCIB_SUBTRACTIVE && r == NULL)
234583c41143SJohn Baldwin 		return (bus_generic_alloc_resource(dev, child, type, rid,
234683c41143SJohn Baldwin 		    start, end, count, flags));
234783c41143SJohn Baldwin 	return (r);
234883c41143SJohn Baldwin }
234983c41143SJohn Baldwin 
235083c41143SJohn Baldwin int
235183c41143SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
23522dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end)
235383c41143SJohn Baldwin {
235483c41143SJohn Baldwin 	struct pcib_softc *sc;
235526245980SJessica Clarke 	struct pcib_window *w;
235615cb3b54SAlexander Motin 	rman_res_t wmask;
235726245980SJessica Clarke 	int error;
235883c41143SJohn Baldwin 
235983c41143SJohn Baldwin 	sc = device_get_softc(bus);
236026245980SJessica Clarke 
236126245980SJessica Clarke 	/*
236226245980SJessica Clarke 	 * If the resource wasn't sub-allocated from one of our region
236326245980SJessica Clarke 	 * managers then just pass the request up.
236426245980SJessica Clarke 	 */
236526245980SJessica Clarke 	if (!pcib_is_resource_managed(sc, type, r))
236626245980SJessica Clarke 		return (bus_generic_adjust_resource(bus, child, type, r,
236726245980SJessica Clarke 		    start, end));
236826245980SJessica Clarke 
236926245980SJessica Clarke #ifdef PCI_RES_BUS
237031776afdSJessica Clarke 	if (type == PCI_RES_BUS) {
237131776afdSJessica Clarke 		/*
237231776afdSJessica Clarke 		 * If our bus range isn't big enough to grow the sub-allocation
237331776afdSJessica Clarke 		 * then we need to grow our bus range. Any request that would
237431776afdSJessica Clarke 		 * require us to decrease the start of our own bus range is
237531776afdSJessica Clarke 		 * invalid, we can only extend the end; ignore such requests
237631776afdSJessica Clarke 		 * and let rman_adjust_resource fail below.
237731776afdSJessica Clarke 		 */
237831776afdSJessica Clarke 		if (start >= sc->bus.sec && end > sc->bus.sub) {
237931776afdSJessica Clarke 			error = pcib_grow_subbus(&sc->bus, end);
238031776afdSJessica Clarke 			if (error != 0)
238131776afdSJessica Clarke 				return (error);
238231776afdSJessica Clarke 		}
238331776afdSJessica Clarke 	} else
238426245980SJessica Clarke #endif
238526245980SJessica Clarke 	{
238626245980SJessica Clarke 		/*
238726245980SJessica Clarke 		 * Resource is managed and not a secondary bus number, must
238826245980SJessica Clarke 		 * be from one of our windows.
238926245980SJessica Clarke 		 */
239026245980SJessica Clarke 		w = pcib_get_resource_window(sc, type, r);
239126245980SJessica Clarke 		KASSERT(w != NULL,
239226245980SJessica Clarke 		    ("%s: no window for resource (%#jx-%#jx) type %d",
239326245980SJessica Clarke 		    __func__, rman_get_start(r), rman_get_end(r), type));
239426245980SJessica Clarke 
239526245980SJessica Clarke 		/*
239626245980SJessica Clarke 		 * If our window isn't big enough to grow the sub-allocation
239726245980SJessica Clarke 		 * then we need to expand the window.
239826245980SJessica Clarke 		 */
239926245980SJessica Clarke 		if (start < w->base || end > w->limit) {
240015cb3b54SAlexander Motin 			wmask = ((rman_res_t)1 << w->step) - 1;
240115cb3b54SAlexander Motin 			error = pcib_expand_window(sc, w, type,
240215cb3b54SAlexander Motin 			    MIN(start & ~wmask, w->base),
240315cb3b54SAlexander Motin 			    MAX(end | wmask, w->limit));
240426245980SJessica Clarke 			if (error != 0)
240526245980SJessica Clarke 				return (error);
240615cb3b54SAlexander Motin 			if (bootverbose)
240715cb3b54SAlexander Motin 				device_printf(sc->dev,
240815cb3b54SAlexander Motin 				    "grew %s window to %#jx-%#jx\n",
240915cb3b54SAlexander Motin 				    w->name, (uintmax_t)w->base,
241015cb3b54SAlexander Motin 				    (uintmax_t)w->limit);
241115cb3b54SAlexander Motin 			pcib_write_windows(sc, w->mask);
241226245980SJessica Clarke 		}
241326245980SJessica Clarke 	}
241426245980SJessica Clarke 
241583c41143SJohn Baldwin 	return (rman_adjust_resource(r, start, end));
241683c41143SJohn Baldwin }
241783c41143SJohn Baldwin 
241883c41143SJohn Baldwin int
241983c41143SJohn Baldwin pcib_release_resource(device_t dev, device_t child, int type, int rid,
242083c41143SJohn Baldwin     struct resource *r)
242183c41143SJohn Baldwin {
242283c41143SJohn Baldwin 	struct pcib_softc *sc;
242383c41143SJohn Baldwin 	int error;
242483c41143SJohn Baldwin 
242583c41143SJohn Baldwin 	sc = device_get_softc(dev);
242683c41143SJohn Baldwin 	if (pcib_is_resource_managed(sc, type, r)) {
242783c41143SJohn Baldwin 		if (rman_get_flags(r) & RF_ACTIVE) {
242883c41143SJohn Baldwin 			error = bus_deactivate_resource(child, type, rid, r);
242983c41143SJohn Baldwin 			if (error)
243083c41143SJohn Baldwin 				return (error);
243183c41143SJohn Baldwin 		}
243283c41143SJohn Baldwin 		return (rman_release_resource(r));
243383c41143SJohn Baldwin 	}
243483c41143SJohn Baldwin 	return (bus_generic_release_resource(dev, child, type, rid, r));
243583c41143SJohn Baldwin }
243683c41143SJohn Baldwin #else
2437bb0d0a8eSMike Smith /*
2438bb0d0a8eSMike Smith  * We have to trap resource allocation requests and ensure that the bridge
2439bb0d0a8eSMike Smith  * is set up to, or capable of handling them.
2440bb0d0a8eSMike Smith  */
24416f0d5884SJohn Baldwin struct resource *
2442bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
24432dd1bdf1SJustin Hibbits     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
2444bb0d0a8eSMike Smith {
2445bb0d0a8eSMike Smith 	struct pcib_softc	*sc = device_get_softc(dev);
244626043836SJohn Baldwin 	const char *name, *suffix;
2447a8b354a8SWarner Losh 	int ok;
2448bb0d0a8eSMike Smith 
2449bb0d0a8eSMike Smith 	/*
2450bb0d0a8eSMike Smith 	 * Fail the allocation for this range if it's not supported.
2451bb0d0a8eSMike Smith 	 */
245226043836SJohn Baldwin 	name = device_get_nameunit(child);
245326043836SJohn Baldwin 	if (name == NULL) {
245426043836SJohn Baldwin 		name = "";
245526043836SJohn Baldwin 		suffix = "";
245626043836SJohn Baldwin 	} else
245726043836SJohn Baldwin 		suffix = " ";
2458bb0d0a8eSMike Smith 	switch (type) {
2459bb0d0a8eSMike Smith 	case SYS_RES_IOPORT:
2460a8b354a8SWarner Losh 		ok = 0;
2461e4b59fc5SWarner Losh 		if (!pcib_is_io_open(sc))
2462e4b59fc5SWarner Losh 			break;
2463a8b354a8SWarner Losh 		ok = (start >= sc->iobase && end <= sc->iolimit);
2464d98d9b12SMarcel Moolenaar 
2465d98d9b12SMarcel Moolenaar 		/*
2466d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA I/O addresses when the
2467d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
2468d98d9b12SMarcel Moolenaar 		 */
2469d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_ioport_range(start, end))
2470d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
2471d98d9b12SMarcel Moolenaar 
2472e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
2473a8b354a8SWarner Losh 			if (!ok) {
247412b8c86eSWarner Losh 				if (start < sc->iobase)
247512b8c86eSWarner Losh 					start = sc->iobase;
247612b8c86eSWarner Losh 				if (end > sc->iolimit)
247712b8c86eSWarner Losh 					end = sc->iolimit;
24782daa7a07SWarner Losh 				if (start < end)
24792daa7a07SWarner Losh 					ok = 1;
2480a8b354a8SWarner Losh 			}
24811c54ff33SMatthew N. Dodd 		} else {
2482e4b59fc5SWarner Losh 			ok = 1;
24839dffe835SWarner Losh #if 0
2484795dceffSWarner Losh 			/*
2485795dceffSWarner Losh 			 * If we overlap with the subtractive range, then
2486795dceffSWarner Losh 			 * pick the upper range to use.
2487795dceffSWarner Losh 			 */
2488795dceffSWarner Losh 			if (start < sc->iolimit && end > sc->iobase)
2489795dceffSWarner Losh 				start = sc->iolimit + 1;
24909dffe835SWarner Losh #endif
249112b8c86eSWarner Losh 		}
2492a8b354a8SWarner Losh 		if (end < start) {
2493da1b038aSJustin Hibbits 			device_printf(dev, "ioport: end (%jx) < start (%jx)\n",
24942daa7a07SWarner Losh 			    end, start);
2495a8b354a8SWarner Losh 			start = 0;
2496a8b354a8SWarner Losh 			end = 0;
2497a8b354a8SWarner Losh 			ok = 0;
2498a8b354a8SWarner Losh 		}
2499a8b354a8SWarner Losh 		if (!ok) {
250026043836SJohn Baldwin 			device_printf(dev, "%s%srequested unsupported I/O "
2501da1b038aSJustin Hibbits 			    "range 0x%jx-0x%jx (decoding 0x%x-0x%x)\n",
250226043836SJohn Baldwin 			    name, suffix, start, end, sc->iobase, sc->iolimit);
2503bb0d0a8eSMike Smith 			return (NULL);
2504bb0d0a8eSMike Smith 		}
25054fa59183SMike Smith 		if (bootverbose)
25062daa7a07SWarner Losh 			device_printf(dev,
2507da1b038aSJustin Hibbits 			    "%s%srequested I/O range 0x%jx-0x%jx: in range\n",
250826043836SJohn Baldwin 			    name, suffix, start, end);
2509bb0d0a8eSMike Smith 		break;
2510bb0d0a8eSMike Smith 
2511bb0d0a8eSMike Smith 	case SYS_RES_MEMORY:
2512a8b354a8SWarner Losh 		ok = 0;
2513a8b354a8SWarner Losh 		if (pcib_is_nonprefetch_open(sc))
2514a8b354a8SWarner Losh 			ok = ok || (start >= sc->membase && end <= sc->memlimit);
2515a8b354a8SWarner Losh 		if (pcib_is_prefetch_open(sc))
2516a8b354a8SWarner Losh 			ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
2517d98d9b12SMarcel Moolenaar 
2518d98d9b12SMarcel Moolenaar 		/*
2519d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA memory addresses when the
2520d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
2521d98d9b12SMarcel Moolenaar 		 */
2522d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_memory_range(start, end))
2523d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
2524d98d9b12SMarcel Moolenaar 
2525e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
2526a8b354a8SWarner Losh 			if (!ok) {
2527a8b354a8SWarner Losh 				ok = 1;
2528a8b354a8SWarner Losh 				if (flags & RF_PREFETCHABLE) {
2529a8b354a8SWarner Losh 					if (pcib_is_prefetch_open(sc)) {
2530a8b354a8SWarner Losh 						if (start < sc->pmembase)
2531a8b354a8SWarner Losh 							start = sc->pmembase;
2532a8b354a8SWarner Losh 						if (end > sc->pmemlimit)
2533a8b354a8SWarner Losh 							end = sc->pmemlimit;
2534a8b354a8SWarner Losh 					} else {
2535a8b354a8SWarner Losh 						ok = 0;
2536a8b354a8SWarner Losh 					}
2537a8b354a8SWarner Losh 				} else {	/* non-prefetchable */
2538a8b354a8SWarner Losh 					if (pcib_is_nonprefetch_open(sc)) {
2539a8b354a8SWarner Losh 						if (start < sc->membase)
254012b8c86eSWarner Losh 							start = sc->membase;
254112b8c86eSWarner Losh 						if (end > sc->memlimit)
254212b8c86eSWarner Losh 							end = sc->memlimit;
25431c54ff33SMatthew N. Dodd 					} else {
2544a8b354a8SWarner Losh 						ok = 0;
2545a8b354a8SWarner Losh 					}
2546a8b354a8SWarner Losh 				}
2547a8b354a8SWarner Losh 			}
2548a8b354a8SWarner Losh 		} else if (!ok) {
2549e4b59fc5SWarner Losh 			ok = 1;	/* subtractive bridge: always ok */
25509dffe835SWarner Losh #if 0
2551a8b354a8SWarner Losh 			if (pcib_is_nonprefetch_open(sc)) {
2552795dceffSWarner Losh 				if (start < sc->memlimit && end > sc->membase)
2553795dceffSWarner Losh 					start = sc->memlimit + 1;
2554a8b354a8SWarner Losh 			}
2555a8b354a8SWarner Losh 			if (pcib_is_prefetch_open(sc)) {
2556795dceffSWarner Losh 				if (start < sc->pmemlimit && end > sc->pmembase)
2557795dceffSWarner Losh 					start = sc->pmemlimit + 1;
25581c54ff33SMatthew N. Dodd 			}
25599dffe835SWarner Losh #endif
256012b8c86eSWarner Losh 		}
2561a8b354a8SWarner Losh 		if (end < start) {
2562da1b038aSJustin Hibbits 			device_printf(dev, "memory: end (%jx) < start (%jx)\n",
25632daa7a07SWarner Losh 			    end, start);
2564a8b354a8SWarner Losh 			start = 0;
2565a8b354a8SWarner Losh 			end = 0;
2566a8b354a8SWarner Losh 			ok = 0;
2567a8b354a8SWarner Losh 		}
2568a8b354a8SWarner Losh 		if (!ok && bootverbose)
256934428485SWarner Losh 			device_printf(dev,
2570da1b038aSJustin Hibbits 			    "%s%srequested unsupported memory range %#jx-%#jx "
2571b0a2d4b8SWarner Losh 			    "(decoding %#jx-%#jx, %#jx-%#jx)\n",
257226043836SJohn Baldwin 			    name, suffix, start, end,
2573b0a2d4b8SWarner Losh 			    (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
2574b0a2d4b8SWarner Losh 			    (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
2575a8b354a8SWarner Losh 		if (!ok)
2576bb0d0a8eSMike Smith 			return (NULL);
25774fa59183SMike Smith 		if (bootverbose)
257826043836SJohn Baldwin 			device_printf(dev,"%s%srequested memory range "
2579da1b038aSJustin Hibbits 			    "0x%jx-0x%jx: good\n",
258026043836SJohn Baldwin 			    name, suffix, start, end);
25814fa59183SMike Smith 		break;
25824fa59183SMike Smith 
2583bb0d0a8eSMike Smith 	default:
25844fa59183SMike Smith 		break;
2585bb0d0a8eSMike Smith 	}
2586bb0d0a8eSMike Smith 	/*
2587bb0d0a8eSMike Smith 	 * Bridge is OK decoding this resource, so pass it up.
2588bb0d0a8eSMike Smith 	 */
25892daa7a07SWarner Losh 	return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
25902daa7a07SWarner Losh 	    count, flags));
2591bb0d0a8eSMike Smith }
259283c41143SJohn Baldwin #endif
2593bb0d0a8eSMike Smith 
2594bb0d0a8eSMike Smith /*
259555d3ea17SRyan Stone  * If ARI is enabled on this downstream port, translate the function number
259655d3ea17SRyan Stone  * to the non-ARI slot/function.  The downstream port will convert it back in
259755d3ea17SRyan Stone  * hardware.  If ARI is not enabled slot and func are not modified.
259855d3ea17SRyan Stone  */
259955d3ea17SRyan Stone static __inline void
260055d3ea17SRyan Stone pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func)
260155d3ea17SRyan Stone {
260255d3ea17SRyan Stone 	struct pcib_softc *sc;
260355d3ea17SRyan Stone 	int ari_func;
260455d3ea17SRyan Stone 
260555d3ea17SRyan Stone 	sc = device_get_softc(pcib);
260655d3ea17SRyan Stone 	ari_func = *func;
260755d3ea17SRyan Stone 
260855d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
260955d3ea17SRyan Stone 		KASSERT(*slot == 0,
261055d3ea17SRyan Stone 		    ("Non-zero slot number with ARI enabled!"));
261155d3ea17SRyan Stone 		*slot = PCIE_ARI_SLOT(ari_func);
261255d3ea17SRyan Stone 		*func = PCIE_ARI_FUNC(ari_func);
261355d3ea17SRyan Stone 	}
261455d3ea17SRyan Stone }
261555d3ea17SRyan Stone 
261655d3ea17SRyan Stone static void
261755d3ea17SRyan Stone pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos)
261855d3ea17SRyan Stone {
261955d3ea17SRyan Stone 	uint32_t ctl2;
262055d3ea17SRyan Stone 
262155d3ea17SRyan Stone 	ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4);
262255d3ea17SRyan Stone 	ctl2 |= PCIEM_CTL2_ARI;
262355d3ea17SRyan Stone 	pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4);
262455d3ea17SRyan Stone 
262555d3ea17SRyan Stone 	sc->flags |= PCIB_ENABLE_ARI;
262655d3ea17SRyan Stone }
262755d3ea17SRyan Stone 
262855d3ea17SRyan Stone /*
2629bb0d0a8eSMike Smith  * PCIB interface.
2630bb0d0a8eSMike Smith  */
26316f0d5884SJohn Baldwin int
2632bb0d0a8eSMike Smith pcib_maxslots(device_t dev)
2633bb0d0a8eSMike Smith {
26345502348dSJustin Hibbits #if !defined(__amd64__) && !defined(__i386__)
26358b92ad43SJustin Hibbits 	uint32_t pcie_pos;
26368b92ad43SJustin Hibbits 	uint16_t val;
26378b92ad43SJustin Hibbits 
26388b92ad43SJustin Hibbits 	/*
26398b92ad43SJustin Hibbits 	 * If this is a PCIe rootport or downstream switch port, there's only
26408b92ad43SJustin Hibbits 	 * one slot permitted.
26418b92ad43SJustin Hibbits 	 */
26428b92ad43SJustin Hibbits 	if (pci_find_cap(dev, PCIY_EXPRESS, &pcie_pos) == 0) {
26438b92ad43SJustin Hibbits 		val = pci_read_config(dev, pcie_pos + PCIER_FLAGS, 2);
26448b92ad43SJustin Hibbits 		val &= PCIEM_FLAGS_TYPE;
26458b92ad43SJustin Hibbits 		if (val == PCIEM_TYPE_ROOT_PORT ||
26468b92ad43SJustin Hibbits 		    val == PCIEM_TYPE_DOWNSTREAM_PORT)
26478b92ad43SJustin Hibbits 			return (0);
26488b92ad43SJustin Hibbits 	}
26495502348dSJustin Hibbits #endif
26504fa59183SMike Smith 	return (PCI_SLOTMAX);
2651bb0d0a8eSMike Smith }
2652bb0d0a8eSMike Smith 
265355d3ea17SRyan Stone static int
265455d3ea17SRyan Stone pcib_ari_maxslots(device_t dev)
265555d3ea17SRyan Stone {
265655d3ea17SRyan Stone 	struct pcib_softc *sc;
265755d3ea17SRyan Stone 
265855d3ea17SRyan Stone 	sc = device_get_softc(dev);
265955d3ea17SRyan Stone 
266055d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI)
266155d3ea17SRyan Stone 		return (PCIE_ARI_SLOTMAX);
266255d3ea17SRyan Stone 	else
26638b92ad43SJustin Hibbits 		return (pcib_maxslots(dev));
266455d3ea17SRyan Stone }
266555d3ea17SRyan Stone 
266655d3ea17SRyan Stone static int
266755d3ea17SRyan Stone pcib_ari_maxfuncs(device_t dev)
266855d3ea17SRyan Stone {
266955d3ea17SRyan Stone 	struct pcib_softc *sc;
267055d3ea17SRyan Stone 
267155d3ea17SRyan Stone 	sc = device_get_softc(dev);
267255d3ea17SRyan Stone 
267355d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI)
267455d3ea17SRyan Stone 		return (PCIE_ARI_FUNCMAX);
267555d3ea17SRyan Stone 	else
267655d3ea17SRyan Stone 		return (PCI_FUNCMAX);
267755d3ea17SRyan Stone }
267855d3ea17SRyan Stone 
26792397d2d8SRyan Stone static void
26802397d2d8SRyan Stone pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot,
26812397d2d8SRyan Stone     int *func)
26822397d2d8SRyan Stone {
26832397d2d8SRyan Stone 	struct pcib_softc *sc;
26842397d2d8SRyan Stone 
26852397d2d8SRyan Stone 	sc = device_get_softc(pcib);
26862397d2d8SRyan Stone 
26872397d2d8SRyan Stone 	*bus = PCI_RID2BUS(rid);
26882397d2d8SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
26892397d2d8SRyan Stone 		*slot = PCIE_ARI_RID2SLOT(rid);
26902397d2d8SRyan Stone 		*func = PCIE_ARI_RID2FUNC(rid);
26912397d2d8SRyan Stone 	} else {
26922397d2d8SRyan Stone 		*slot = PCI_RID2SLOT(rid);
26932397d2d8SRyan Stone 		*func = PCI_RID2FUNC(rid);
26942397d2d8SRyan Stone 	}
26952397d2d8SRyan Stone }
26962397d2d8SRyan Stone 
2697bb0d0a8eSMike Smith /*
2698bb0d0a8eSMike Smith  * Since we are a child of a PCI bus, its parent must support the pcib interface.
2699bb0d0a8eSMike Smith  */
270055d3ea17SRyan Stone static uint32_t
2701795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
2702bb0d0a8eSMike Smith {
270382cb5c3bSJohn Baldwin #ifdef PCI_HP
270482cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
270555d3ea17SRyan Stone 
270682cb5c3bSJohn Baldwin 	sc = device_get_softc(dev);
270782cb5c3bSJohn Baldwin 	if (!pcib_present(sc)) {
270882cb5c3bSJohn Baldwin 		switch (width) {
270982cb5c3bSJohn Baldwin 		case 2:
271082cb5c3bSJohn Baldwin 			return (0xffff);
271182cb5c3bSJohn Baldwin 		case 1:
271282cb5c3bSJohn Baldwin 			return (0xff);
271382cb5c3bSJohn Baldwin 		default:
271482cb5c3bSJohn Baldwin 			return (0xffffffff);
271582cb5c3bSJohn Baldwin 		}
271682cb5c3bSJohn Baldwin 	}
271782cb5c3bSJohn Baldwin #endif
271855d3ea17SRyan Stone 	pcib_xlate_ari(dev, b, &s, &f);
271955d3ea17SRyan Stone 	return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s,
272055d3ea17SRyan Stone 	    f, reg, width));
2721bb0d0a8eSMike Smith }
2722bb0d0a8eSMike Smith 
272355d3ea17SRyan Stone static void
2724795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
2725bb0d0a8eSMike Smith {
272682cb5c3bSJohn Baldwin #ifdef PCI_HP
272782cb5c3bSJohn Baldwin 	struct pcib_softc *sc;
272855d3ea17SRyan Stone 
272982cb5c3bSJohn Baldwin 	sc = device_get_softc(dev);
273082cb5c3bSJohn Baldwin 	if (!pcib_present(sc))
273182cb5c3bSJohn Baldwin 		return;
273282cb5c3bSJohn Baldwin #endif
273355d3ea17SRyan Stone 	pcib_xlate_ari(dev, b, &s, &f);
273455d3ea17SRyan Stone 	PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f,
273555d3ea17SRyan Stone 	    reg, val, width);
2736bb0d0a8eSMike Smith }
2737bb0d0a8eSMike Smith 
2738bb0d0a8eSMike Smith /*
2739bb0d0a8eSMike Smith  * Route an interrupt across a PCI bridge.
2740bb0d0a8eSMike Smith  */
27412c2d1d07SBenno Rice int
2742bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin)
2743bb0d0a8eSMike Smith {
2744bb0d0a8eSMike Smith     device_t	bus;
2745bb0d0a8eSMike Smith     int		parent_intpin;
2746bb0d0a8eSMike Smith     int		intnum;
2747bb0d0a8eSMike Smith 
2748bb0d0a8eSMike Smith     /*
2749bb0d0a8eSMike Smith      *
2750bb0d0a8eSMike Smith      * The PCI standard defines a swizzle of the child-side device/intpin to
2751bb0d0a8eSMike Smith      * the parent-side intpin as follows.
2752bb0d0a8eSMike Smith      *
2753bb0d0a8eSMike Smith      * device = device on child bus
2754bb0d0a8eSMike Smith      * child_intpin = intpin on child bus slot (0-3)
2755bb0d0a8eSMike Smith      * parent_intpin = intpin on parent bus slot (0-3)
2756bb0d0a8eSMike Smith      *
2757bb0d0a8eSMike Smith      * parent_intpin = (device + child_intpin) % 4
2758bb0d0a8eSMike Smith      */
2759cdc95e1bSBernd Walter     parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
2760bb0d0a8eSMike Smith 
2761bb0d0a8eSMike Smith     /*
2762bb0d0a8eSMike Smith      * Our parent is a PCI bus.  Its parent must export the pcib interface
2763bb0d0a8eSMike Smith      * which includes the ability to route interrupts.
2764bb0d0a8eSMike Smith      */
2765bb0d0a8eSMike Smith     bus = device_get_parent(pcib);
2766bb0d0a8eSMike Smith     intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
276739981fedSJohn Baldwin     if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
2768c6a121abSJohn Baldwin 	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
2769c6a121abSJohn Baldwin 	    pci_get_slot(dev), 'A' + pin - 1, intnum);
27708046c4b9SMike Smith     }
2771bb0d0a8eSMike Smith     return(intnum);
2772bb0d0a8eSMike Smith }
2773b173edafSJohn Baldwin 
2774e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
27759bf4c9c1SJohn Baldwin int
27769bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
27779bf4c9c1SJohn Baldwin {
2778bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
27799bf4c9c1SJohn Baldwin 	device_t bus;
27809bf4c9c1SJohn Baldwin 
278122bf1c7fSJohn Baldwin 	if (sc->flags & PCIB_DISABLE_MSI)
278222bf1c7fSJohn Baldwin 		return (ENXIO);
27839bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
27849bf4c9c1SJohn Baldwin 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
27859bf4c9c1SJohn Baldwin 	    irqs));
27869bf4c9c1SJohn Baldwin }
27879bf4c9c1SJohn Baldwin 
2788e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */
27899bf4c9c1SJohn Baldwin int
27909bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
27919bf4c9c1SJohn Baldwin {
27929bf4c9c1SJohn Baldwin 	device_t bus;
27939bf4c9c1SJohn Baldwin 
27949bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
27959bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
27969bf4c9c1SJohn Baldwin }
27979bf4c9c1SJohn Baldwin 
27989bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */
27999bf4c9c1SJohn Baldwin int
2800e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
28019bf4c9c1SJohn Baldwin {
2802bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
28039bf4c9c1SJohn Baldwin 	device_t bus;
28049bf4c9c1SJohn Baldwin 
280568e9cbd3SMarius Strobl 	if (sc->flags & PCIB_DISABLE_MSIX)
280622bf1c7fSJohn Baldwin 		return (ENXIO);
28079bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
2808e706f7f0SJohn Baldwin 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
28095fe82bcaSJohn Baldwin }
28105fe82bcaSJohn Baldwin 
28119bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */
28129bf4c9c1SJohn Baldwin int
28139bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq)
28149bf4c9c1SJohn Baldwin {
28159bf4c9c1SJohn Baldwin 	device_t bus;
28169bf4c9c1SJohn Baldwin 
28179bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
28189bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
28199bf4c9c1SJohn Baldwin }
28209bf4c9c1SJohn Baldwin 
2821e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */
2822e706f7f0SJohn Baldwin int
2823e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
2824e706f7f0SJohn Baldwin     uint32_t *data)
2825e706f7f0SJohn Baldwin {
2826e706f7f0SJohn Baldwin 	device_t bus;
28274522ac77SLuoqi Chen 	int error;
2828e706f7f0SJohn Baldwin 
2829e706f7f0SJohn Baldwin 	bus = device_get_parent(pcib);
28304522ac77SLuoqi Chen 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
28314522ac77SLuoqi Chen 	if (error)
28324522ac77SLuoqi Chen 		return (error);
28334522ac77SLuoqi Chen 
28344522ac77SLuoqi Chen 	pci_ht_map_msi(pcib, *addr);
28354522ac77SLuoqi Chen 	return (0);
2836e706f7f0SJohn Baldwin }
2837e706f7f0SJohn Baldwin 
283862508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */
283962508c53SJohn Baldwin int
284062508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
284162508c53SJohn Baldwin {
284262508c53SJohn Baldwin 	device_t bus;
284362508c53SJohn Baldwin 
284462508c53SJohn Baldwin 	bus = device_get_parent(pcib);
284562508c53SJohn Baldwin 	return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate));
284662508c53SJohn Baldwin }
28475605a99eSRyan Stone 
28482397d2d8SRyan Stone static int
28492397d2d8SRyan Stone pcib_ari_enabled(device_t pcib)
28502397d2d8SRyan Stone {
28512397d2d8SRyan Stone 	struct pcib_softc *sc;
28522397d2d8SRyan Stone 
28532397d2d8SRyan Stone 	sc = device_get_softc(pcib);
28542397d2d8SRyan Stone 
28552397d2d8SRyan Stone 	return ((sc->flags & PCIB_ENABLE_ARI) != 0);
28562397d2d8SRyan Stone }
28572397d2d8SRyan Stone 
2858d7be980dSAndrew Turner static int
2859d7be980dSAndrew Turner pcib_ari_get_id(device_t pcib, device_t dev, enum pci_id_type type,
2860d7be980dSAndrew Turner     uintptr_t *id)
286155d3ea17SRyan Stone {
286255d3ea17SRyan Stone 	struct pcib_softc *sc;
28631e43b18cSAndrew Turner 	device_t bus_dev;
286455d3ea17SRyan Stone 	uint8_t bus, slot, func;
286555d3ea17SRyan Stone 
28661e43b18cSAndrew Turner 	if (type != PCI_ID_RID) {
28671e43b18cSAndrew Turner 		bus_dev = device_get_parent(pcib);
28681e43b18cSAndrew Turner 		return (PCIB_GET_ID(device_get_parent(bus_dev), dev, type, id));
28691e43b18cSAndrew Turner 	}
2870d7be980dSAndrew Turner 
287155d3ea17SRyan Stone 	sc = device_get_softc(pcib);
287255d3ea17SRyan Stone 
287355d3ea17SRyan Stone 	if (sc->flags & PCIB_ENABLE_ARI) {
287455d3ea17SRyan Stone 		bus = pci_get_bus(dev);
287555d3ea17SRyan Stone 		func = pci_get_function(dev);
287655d3ea17SRyan Stone 
2877d7be980dSAndrew Turner 		*id = (PCI_ARI_RID(bus, func));
287855d3ea17SRyan Stone 	} else {
287955d3ea17SRyan Stone 		bus = pci_get_bus(dev);
288055d3ea17SRyan Stone 		slot = pci_get_slot(dev);
288155d3ea17SRyan Stone 		func = pci_get_function(dev);
288255d3ea17SRyan Stone 
2883d7be980dSAndrew Turner 		*id = (PCI_RID(bus, slot, func));
288455d3ea17SRyan Stone 	}
2885d7be980dSAndrew Turner 
2886d7be980dSAndrew Turner 	return (0);
288755d3ea17SRyan Stone }
288855d3ea17SRyan Stone 
288955d3ea17SRyan Stone /*
289055d3ea17SRyan Stone  * Check that the downstream port (pcib) and the endpoint device (dev) both
289155d3ea17SRyan Stone  * support ARI.  If so, enable it and return 0, otherwise return an error.
289255d3ea17SRyan Stone  */
289355d3ea17SRyan Stone static int
289455d3ea17SRyan Stone pcib_try_enable_ari(device_t pcib, device_t dev)
289555d3ea17SRyan Stone {
289655d3ea17SRyan Stone 	struct pcib_softc *sc;
289755d3ea17SRyan Stone 	int error;
289855d3ea17SRyan Stone 	uint32_t cap2;
289955d3ea17SRyan Stone 	int ari_cap_off;
290055d3ea17SRyan Stone 	uint32_t ari_ver;
290155d3ea17SRyan Stone 	uint32_t pcie_pos;
290255d3ea17SRyan Stone 
290355d3ea17SRyan Stone 	sc = device_get_softc(pcib);
290455d3ea17SRyan Stone 
290555d3ea17SRyan Stone 	/*
290655d3ea17SRyan Stone 	 * ARI is controlled in a register in the PCIe capability structure.
290755d3ea17SRyan Stone 	 * If the downstream port does not have the PCIe capability structure
290855d3ea17SRyan Stone 	 * then it does not support ARI.
290955d3ea17SRyan Stone 	 */
291055d3ea17SRyan Stone 	error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos);
291155d3ea17SRyan Stone 	if (error != 0)
291255d3ea17SRyan Stone 		return (ENODEV);
291355d3ea17SRyan Stone 
291455d3ea17SRyan Stone 	/* Check that the PCIe port advertises ARI support. */
291555d3ea17SRyan Stone 	cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4);
291655d3ea17SRyan Stone 	if (!(cap2 & PCIEM_CAP2_ARI))
291755d3ea17SRyan Stone 		return (ENODEV);
291855d3ea17SRyan Stone 
291955d3ea17SRyan Stone 	/*
292055d3ea17SRyan Stone 	 * Check that the endpoint device advertises ARI support via the ARI
292155d3ea17SRyan Stone 	 * extended capability structure.
292255d3ea17SRyan Stone 	 */
292355d3ea17SRyan Stone 	error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off);
292455d3ea17SRyan Stone 	if (error != 0)
292555d3ea17SRyan Stone 		return (ENODEV);
292655d3ea17SRyan Stone 
292755d3ea17SRyan Stone 	/*
292855d3ea17SRyan Stone 	 * Finally, check that the endpoint device supports the same version
292955d3ea17SRyan Stone 	 * of ARI that we do.
293055d3ea17SRyan Stone 	 */
293155d3ea17SRyan Stone 	ari_ver = pci_read_config(dev, ari_cap_off, 4);
293255d3ea17SRyan Stone 	if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) {
293355d3ea17SRyan Stone 		if (bootverbose)
293455d3ea17SRyan Stone 			device_printf(pcib,
293555d3ea17SRyan Stone 			    "Unsupported version of ARI (%d) detected\n",
293655d3ea17SRyan Stone 			    PCI_EXTCAP_VER(ari_ver));
293755d3ea17SRyan Stone 
293855d3ea17SRyan Stone 		return (ENXIO);
293955d3ea17SRyan Stone 	}
294055d3ea17SRyan Stone 
294155d3ea17SRyan Stone 	pcib_enable_ari(sc, pcie_pos);
294255d3ea17SRyan Stone 
294355d3ea17SRyan Stone 	return (0);
294455d3ea17SRyan Stone }
29454cb67729SWarner Losh 
294628586889SWarner Losh int
294728586889SWarner Losh pcib_request_feature_allow(device_t pcib, device_t dev,
294828586889SWarner Losh     enum pci_feature feature)
294928586889SWarner Losh {
295028586889SWarner Losh 	/*
29515914c62eSGavin Atkinson 	 * No host firmware we have to negotiate with, so we allow
295228586889SWarner Losh 	 * every valid feature requested.
295328586889SWarner Losh 	 */
295428586889SWarner Losh 	switch (feature) {
295528586889SWarner Losh 	case PCI_FEATURE_AER:
295628586889SWarner Losh 	case PCI_FEATURE_HP:
295728586889SWarner Losh 		break;
295828586889SWarner Losh 	default:
295928586889SWarner Losh 		return (EINVAL);
296028586889SWarner Losh 	}
296128586889SWarner Losh 
296228586889SWarner Losh 	return (0);
296328586889SWarner Losh }
296428586889SWarner Losh 
29651ffd07bdSJohn Baldwin int
29661ffd07bdSJohn Baldwin pcib_request_feature(device_t dev, enum pci_feature feature)
29671ffd07bdSJohn Baldwin {
29681ffd07bdSJohn Baldwin 
29691ffd07bdSJohn Baldwin 	/*
29701ffd07bdSJohn Baldwin 	 * Invoke PCIB_REQUEST_FEATURE of this bridge first in case
29711ffd07bdSJohn Baldwin 	 * the firmware overrides the method of PCI-PCI bridges.
29721ffd07bdSJohn Baldwin 	 */
29731ffd07bdSJohn Baldwin 	return (PCIB_REQUEST_FEATURE(dev, dev, feature));
29741ffd07bdSJohn Baldwin }
29751ffd07bdSJohn Baldwin 
29764cb67729SWarner Losh /*
29774cb67729SWarner Losh  * Pass the request to use this PCI feature up the tree. Either there's a
29784cb67729SWarner Losh  * firmware like ACPI that's using this feature that will approve (or deny) the
29794cb67729SWarner Losh  * request to take it over, or the platform has no such firmware, in which case
29804cb67729SWarner Losh  * the request will be approved. If the request is approved, the OS is expected
29814cb67729SWarner Losh  * to make use of the feature or render it harmless.
29824cb67729SWarner Losh  */
29834cb67729SWarner Losh static int
29841ffd07bdSJohn Baldwin pcib_request_feature_default(device_t pcib, device_t dev,
29851ffd07bdSJohn Baldwin     enum pci_feature feature)
29864cb67729SWarner Losh {
29874cb67729SWarner Losh 	device_t bus;
29884cb67729SWarner Losh 
29894cb67729SWarner Losh 	/*
29904cb67729SWarner Losh 	 * Our parent is necessarily a pci bus. Its parent will either be
29914cb67729SWarner Losh 	 * another pci bridge (which passes it up) or a host bridge that can
29924cb67729SWarner Losh 	 * approve or reject the request.
29934cb67729SWarner Losh 	 */
29944cb67729SWarner Losh 	bus = device_get_parent(pcib);
29954cb67729SWarner Losh 	return (PCIB_REQUEST_FEATURE(device_get_parent(bus), dev, feature));
29964cb67729SWarner Losh }
29975db2a4a8SKonstantin Belousov 
29985db2a4a8SKonstantin Belousov static int
29995db2a4a8SKonstantin Belousov pcib_reset_child(device_t dev, device_t child, int flags)
30005db2a4a8SKonstantin Belousov {
30015db2a4a8SKonstantin Belousov 	struct pci_devinfo *pdinfo;
30025db2a4a8SKonstantin Belousov 	int error;
30035db2a4a8SKonstantin Belousov 
30045db2a4a8SKonstantin Belousov 	error = 0;
30055db2a4a8SKonstantin Belousov 	if (dev == NULL || device_get_parent(child) != dev)
30065db2a4a8SKonstantin Belousov 		goto out;
30075db2a4a8SKonstantin Belousov 	error = ENXIO;
30085db2a4a8SKonstantin Belousov 	if (device_get_devclass(child) != devclass_find("pci"))
30095db2a4a8SKonstantin Belousov 		goto out;
30105db2a4a8SKonstantin Belousov 	pdinfo = device_get_ivars(dev);
30115db2a4a8SKonstantin Belousov 	if (pdinfo->cfg.pcie.pcie_location != 0 &&
30125db2a4a8SKonstantin Belousov 	    (pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_DOWNSTREAM_PORT ||
30135db2a4a8SKonstantin Belousov 	    pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_ROOT_PORT)) {
30145db2a4a8SKonstantin Belousov 		error = bus_helper_reset_prepare(child, flags);
30155db2a4a8SKonstantin Belousov 		if (error == 0) {
30165db2a4a8SKonstantin Belousov 			error = pcie_link_reset(dev,
30175db2a4a8SKonstantin Belousov 			    pdinfo->cfg.pcie.pcie_location);
30185db2a4a8SKonstantin Belousov 			/* XXXKIB call _post even if error != 0 ? */
30195db2a4a8SKonstantin Belousov 			bus_helper_reset_post(child, flags);
30205db2a4a8SKonstantin Belousov 		}
30215db2a4a8SKonstantin Belousov 	}
30225db2a4a8SKonstantin Belousov out:
30235db2a4a8SKonstantin Belousov 	return (error);
30245db2a4a8SKonstantin Belousov }
3025