xref: /freebsd/sys/dev/pci/pci_pci.c (revision 4edef187b8acc2ca09a38cb6bb6969ff5ead938b)
1bb0d0a8eSMike Smith /*-
2bb0d0a8eSMike Smith  * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
3bb0d0a8eSMike Smith  * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
4bb0d0a8eSMike Smith  * Copyright (c) 2000 BSDi
5bb0d0a8eSMike Smith  * All rights reserved.
6bb0d0a8eSMike Smith  *
7bb0d0a8eSMike Smith  * Redistribution and use in source and binary forms, with or without
8bb0d0a8eSMike Smith  * modification, are permitted provided that the following conditions
9bb0d0a8eSMike Smith  * are met:
10bb0d0a8eSMike Smith  * 1. Redistributions of source code must retain the above copyright
11bb0d0a8eSMike Smith  *    notice, this list of conditions and the following disclaimer.
12bb0d0a8eSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
13bb0d0a8eSMike Smith  *    notice, this list of conditions and the following disclaimer in the
14bb0d0a8eSMike Smith  *    documentation and/or other materials provided with the distribution.
15bb0d0a8eSMike Smith  * 3. The name of the author may not be used to endorse or promote products
16bb0d0a8eSMike Smith  *    derived from this software without specific prior written permission.
17bb0d0a8eSMike Smith  *
18bb0d0a8eSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19bb0d0a8eSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20bb0d0a8eSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21bb0d0a8eSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22bb0d0a8eSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23bb0d0a8eSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24bb0d0a8eSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25bb0d0a8eSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26bb0d0a8eSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27bb0d0a8eSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28bb0d0a8eSMike Smith  * SUCH DAMAGE.
29bb0d0a8eSMike Smith  */
30bb0d0a8eSMike Smith 
31aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
32aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
33aad970f1SDavid E. O'Brien 
34bb0d0a8eSMike Smith /*
35bb0d0a8eSMike Smith  * PCI:PCI bridge support.
36bb0d0a8eSMike Smith  */
37bb0d0a8eSMike Smith 
38bb0d0a8eSMike Smith #include <sys/param.h>
39bb0d0a8eSMike Smith #include <sys/bus.h>
4083c41143SJohn Baldwin #include <sys/kernel.h>
4183c41143SJohn Baldwin #include <sys/malloc.h>
4283c41143SJohn Baldwin #include <sys/module.h>
43a8b354a8SWarner Losh #include <sys/rman.h>
441c54ff33SMatthew N. Dodd #include <sys/sysctl.h>
4583c41143SJohn Baldwin #include <sys/systm.h>
46bb0d0a8eSMike Smith 
4738d8c994SWarner Losh #include <dev/pci/pcivar.h>
4838d8c994SWarner Losh #include <dev/pci/pcireg.h>
4962508c53SJohn Baldwin #include <dev/pci/pci_private.h>
5038d8c994SWarner Losh #include <dev/pci/pcib_private.h>
51bb0d0a8eSMike Smith 
52bb0d0a8eSMike Smith #include "pcib_if.h"
53bb0d0a8eSMike Smith 
54bb0d0a8eSMike Smith static int		pcib_probe(device_t dev);
55e36af292SJung-uk Kim static int		pcib_suspend(device_t dev);
56e36af292SJung-uk Kim static int		pcib_resume(device_t dev);
5762508c53SJohn Baldwin static int		pcib_power_for_sleep(device_t pcib, device_t dev,
5862508c53SJohn Baldwin 			    int *pstate);
59bb0d0a8eSMike Smith 
60bb0d0a8eSMike Smith static device_method_t pcib_methods[] = {
61bb0d0a8eSMike Smith     /* Device interface */
62bb0d0a8eSMike Smith     DEVMETHOD(device_probe,		pcib_probe),
63bb0d0a8eSMike Smith     DEVMETHOD(device_attach,		pcib_attach),
644e30440dSWarner Losh     DEVMETHOD(device_detach,		bus_generic_detach),
65bb0d0a8eSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
66e36af292SJung-uk Kim     DEVMETHOD(device_suspend,		pcib_suspend),
67e36af292SJung-uk Kim     DEVMETHOD(device_resume,		pcib_resume),
68bb0d0a8eSMike Smith 
69bb0d0a8eSMike Smith     /* Bus interface */
70bb0d0a8eSMike Smith     DEVMETHOD(bus_read_ivar,		pcib_read_ivar),
71bb0d0a8eSMike Smith     DEVMETHOD(bus_write_ivar,		pcib_write_ivar),
72bb0d0a8eSMike Smith     DEVMETHOD(bus_alloc_resource,	pcib_alloc_resource),
7383c41143SJohn Baldwin #ifdef NEW_PCIB
7483c41143SJohn Baldwin     DEVMETHOD(bus_adjust_resource,	pcib_adjust_resource),
7583c41143SJohn Baldwin     DEVMETHOD(bus_release_resource,	pcib_release_resource),
7683c41143SJohn Baldwin #else
77d2c9344fSJohn Baldwin     DEVMETHOD(bus_adjust_resource,	bus_generic_adjust_resource),
78bb0d0a8eSMike Smith     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
7983c41143SJohn Baldwin #endif
80bb0d0a8eSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
81bb0d0a8eSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
82bb0d0a8eSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
83bb0d0a8eSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
84bb0d0a8eSMike Smith 
85bb0d0a8eSMike Smith     /* pcib interface */
86bb0d0a8eSMike Smith     DEVMETHOD(pcib_maxslots,		pcib_maxslots),
87bb0d0a8eSMike Smith     DEVMETHOD(pcib_read_config,		pcib_read_config),
88bb0d0a8eSMike Smith     DEVMETHOD(pcib_write_config,	pcib_write_config),
89bb0d0a8eSMike Smith     DEVMETHOD(pcib_route_interrupt,	pcib_route_interrupt),
909bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_alloc_msi,		pcib_alloc_msi),
919bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msi,		pcib_release_msi),
929bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_alloc_msix,		pcib_alloc_msix),
939bf4c9c1SJohn Baldwin     DEVMETHOD(pcib_release_msix,	pcib_release_msix),
94e706f7f0SJohn Baldwin     DEVMETHOD(pcib_map_msi,		pcib_map_msi),
9562508c53SJohn Baldwin     DEVMETHOD(pcib_power_for_sleep,	pcib_power_for_sleep),
96bb0d0a8eSMike Smith 
974b7ec270SMarius Strobl     DEVMETHOD_END
98bb0d0a8eSMike Smith };
99bb0d0a8eSMike Smith 
10004dda605SJohn Baldwin static devclass_t pcib_devclass;
101bb0d0a8eSMike Smith 
10204dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc));
10368e9cbd3SMarius Strobl DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, NULL, NULL);
104bb0d0a8eSMike Smith 
10583c41143SJohn Baldwin #ifdef NEW_PCIB
1060070c94bSJohn Baldwin SYSCTL_DECL(_hw_pci);
1070070c94bSJohn Baldwin 
1080070c94bSJohn Baldwin static int pci_clear_pcib;
1090070c94bSJohn Baldwin TUNABLE_INT("hw.pci.clear_pcib", &pci_clear_pcib);
1100070c94bSJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0,
1110070c94bSJohn Baldwin     "Clear firmware-assigned resources for PCI-PCI bridge I/O windows.");
11283c41143SJohn Baldwin 
11383c41143SJohn Baldwin /*
11483c41143SJohn Baldwin  * Is a resource from a child device sub-allocated from one of our
11583c41143SJohn Baldwin  * resource managers?
11683c41143SJohn Baldwin  */
11783c41143SJohn Baldwin static int
11883c41143SJohn Baldwin pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r)
11983c41143SJohn Baldwin {
12083c41143SJohn Baldwin 
12183c41143SJohn Baldwin 	switch (type) {
122*4edef187SJohn Baldwin #ifdef PCI_RES_BUS
123*4edef187SJohn Baldwin 	case PCI_RES_BUS:
124*4edef187SJohn Baldwin 		return (rman_is_region_manager(r, &sc->bus.rman));
125*4edef187SJohn Baldwin #endif
12683c41143SJohn Baldwin 	case SYS_RES_IOPORT:
12783c41143SJohn Baldwin 		return (rman_is_region_manager(r, &sc->io.rman));
12883c41143SJohn Baldwin 	case SYS_RES_MEMORY:
12983c41143SJohn Baldwin 		/* Prefetchable resources may live in either memory rman. */
13083c41143SJohn Baldwin 		if (rman_get_flags(r) & RF_PREFETCHABLE &&
13183c41143SJohn Baldwin 		    rman_is_region_manager(r, &sc->pmem.rman))
13283c41143SJohn Baldwin 			return (1);
13383c41143SJohn Baldwin 		return (rman_is_region_manager(r, &sc->mem.rman));
13483c41143SJohn Baldwin 	}
13583c41143SJohn Baldwin 	return (0);
13683c41143SJohn Baldwin }
13783c41143SJohn Baldwin 
13883c41143SJohn Baldwin static int
13983c41143SJohn Baldwin pcib_is_window_open(struct pcib_window *pw)
14083c41143SJohn Baldwin {
14183c41143SJohn Baldwin 
14283c41143SJohn Baldwin 	return (pw->valid && pw->base < pw->limit);
14383c41143SJohn Baldwin }
14483c41143SJohn Baldwin 
14583c41143SJohn Baldwin /*
14683c41143SJohn Baldwin  * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and
14783c41143SJohn Baldwin  * handle for the resource, we could pass RF_ACTIVE up to the PCI bus
14883c41143SJohn Baldwin  * when allocating the resource windows and rely on the PCI bus driver
14983c41143SJohn Baldwin  * to do this for us.
15083c41143SJohn Baldwin  */
15183c41143SJohn Baldwin static void
15283c41143SJohn Baldwin pcib_activate_window(struct pcib_softc *sc, int type)
15383c41143SJohn Baldwin {
15483c41143SJohn Baldwin 
15583c41143SJohn Baldwin 	PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type);
15683c41143SJohn Baldwin }
15783c41143SJohn Baldwin 
15883c41143SJohn Baldwin static void
15983c41143SJohn Baldwin pcib_write_windows(struct pcib_softc *sc, int mask)
16083c41143SJohn Baldwin {
16183c41143SJohn Baldwin 	device_t dev;
16283c41143SJohn Baldwin 	uint32_t val;
16383c41143SJohn Baldwin 
16483c41143SJohn Baldwin 	dev = sc->dev;
16583c41143SJohn Baldwin 	if (sc->io.valid && mask & WIN_IO) {
16683c41143SJohn Baldwin 		val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
16783c41143SJohn Baldwin 		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
16883c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOBASEH_1,
16983c41143SJohn Baldwin 			    sc->io.base >> 16, 2);
17083c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOLIMITH_1,
17183c41143SJohn Baldwin 			    sc->io.limit >> 16, 2);
17283c41143SJohn Baldwin 		}
17383c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1);
17483c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1);
17583c41143SJohn Baldwin 	}
17683c41143SJohn Baldwin 
17783c41143SJohn Baldwin 	if (mask & WIN_MEM) {
17883c41143SJohn Baldwin 		pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2);
17983c41143SJohn Baldwin 		pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2);
18083c41143SJohn Baldwin 	}
18183c41143SJohn Baldwin 
18283c41143SJohn Baldwin 	if (sc->pmem.valid && mask & WIN_PMEM) {
18383c41143SJohn Baldwin 		val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
18483c41143SJohn Baldwin 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
18583c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMBASEH_1,
18683c41143SJohn Baldwin 			    sc->pmem.base >> 32, 4);
18783c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMLIMITH_1,
18883c41143SJohn Baldwin 			    sc->pmem.limit >> 32, 4);
18983c41143SJohn Baldwin 		}
19083c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2);
19183c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2);
19283c41143SJohn Baldwin 	}
19383c41143SJohn Baldwin }
19483c41143SJohn Baldwin 
195c825d4dcSJohn Baldwin /*
196c825d4dcSJohn Baldwin  * This is used to reject I/O port allocations that conflict with an
197c825d4dcSJohn Baldwin  * ISA alias range.
198c825d4dcSJohn Baldwin  */
199c825d4dcSJohn Baldwin static int
200c825d4dcSJohn Baldwin pcib_is_isa_range(struct pcib_softc *sc, u_long start, u_long end, u_long count)
201c825d4dcSJohn Baldwin {
202c825d4dcSJohn Baldwin 	u_long next_alias;
203c825d4dcSJohn Baldwin 
204c825d4dcSJohn Baldwin 	if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE))
205c825d4dcSJohn Baldwin 		return (0);
206c825d4dcSJohn Baldwin 
207c825d4dcSJohn Baldwin 	/* Only check fixed ranges for overlap. */
208c825d4dcSJohn Baldwin 	if (start + count - 1 != end)
209c825d4dcSJohn Baldwin 		return (0);
210c825d4dcSJohn Baldwin 
211c825d4dcSJohn Baldwin 	/* ISA aliases are only in the lower 64KB of I/O space. */
212c825d4dcSJohn Baldwin 	if (start >= 65536)
213c825d4dcSJohn Baldwin 		return (0);
214c825d4dcSJohn Baldwin 
215c825d4dcSJohn Baldwin 	/* Check for overlap with 0x000 - 0x0ff as a special case. */
216c825d4dcSJohn Baldwin 	if (start < 0x100)
217c825d4dcSJohn Baldwin 		goto alias;
218c825d4dcSJohn Baldwin 
219c825d4dcSJohn Baldwin 	/*
220c825d4dcSJohn Baldwin 	 * If the start address is an alias, the range is an alias.
221c825d4dcSJohn Baldwin 	 * Otherwise, compute the start of the next alias range and
222c825d4dcSJohn Baldwin 	 * check if it is before the end of the candidate range.
223c825d4dcSJohn Baldwin 	 */
224c825d4dcSJohn Baldwin 	if ((start & 0x300) != 0)
225c825d4dcSJohn Baldwin 		goto alias;
226c825d4dcSJohn Baldwin 	next_alias = (start & ~0x3fful) | 0x100;
227c825d4dcSJohn Baldwin 	if (next_alias <= end)
228c825d4dcSJohn Baldwin 		goto alias;
229c825d4dcSJohn Baldwin 	return (0);
230c825d4dcSJohn Baldwin 
231c825d4dcSJohn Baldwin alias:
232c825d4dcSJohn Baldwin 	if (bootverbose)
233c825d4dcSJohn Baldwin 		device_printf(sc->dev,
234c825d4dcSJohn Baldwin 		    "I/O range %#lx-%#lx overlaps with an ISA alias\n", start,
235c825d4dcSJohn Baldwin 		    end);
236c825d4dcSJohn Baldwin 	return (1);
237c825d4dcSJohn Baldwin }
238c825d4dcSJohn Baldwin 
239c825d4dcSJohn Baldwin static void
240c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res,
241c825d4dcSJohn Baldwin     int count)
242c825d4dcSJohn Baldwin {
243c825d4dcSJohn Baldwin 	struct resource **newarray;
244c825d4dcSJohn Baldwin 	int error, i;
245c825d4dcSJohn Baldwin 
246c825d4dcSJohn Baldwin 	newarray = malloc(sizeof(struct resource *) * (w->count + count),
247c825d4dcSJohn Baldwin 	    M_DEVBUF, M_WAITOK);
248c825d4dcSJohn Baldwin 	if (w->res != NULL)
249c825d4dcSJohn Baldwin 		bcopy(w->res, newarray, sizeof(struct resource *) * w->count);
250c825d4dcSJohn Baldwin 	bcopy(res, newarray + w->count, sizeof(struct resource *) * count);
251c825d4dcSJohn Baldwin 	free(w->res, M_DEVBUF);
252c825d4dcSJohn Baldwin 	w->res = newarray;
253c825d4dcSJohn Baldwin 	w->count += count;
254c825d4dcSJohn Baldwin 
255c825d4dcSJohn Baldwin 	for (i = 0; i < count; i++) {
256c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, rman_get_start(res[i]),
257c825d4dcSJohn Baldwin 		    rman_get_end(res[i]));
258c825d4dcSJohn Baldwin 		if (error)
259c825d4dcSJohn Baldwin 			panic("Failed to add resource to rman");
260c825d4dcSJohn Baldwin 	}
261c825d4dcSJohn Baldwin }
262c825d4dcSJohn Baldwin 
263c825d4dcSJohn Baldwin typedef void (nonisa_callback)(u_long start, u_long end, void *arg);
264c825d4dcSJohn Baldwin 
265c825d4dcSJohn Baldwin static void
266c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(u_long start, u_long end, nonisa_callback *cb,
267c825d4dcSJohn Baldwin     void *arg)
268c825d4dcSJohn Baldwin {
269c825d4dcSJohn Baldwin 	u_long next_end;
270c825d4dcSJohn Baldwin 
271c825d4dcSJohn Baldwin 	/*
272c825d4dcSJohn Baldwin 	 * If start is within an ISA alias range, move up to the start
273c825d4dcSJohn Baldwin 	 * of the next non-alias range.  As a special case, addresses
274c825d4dcSJohn Baldwin 	 * in the range 0x000 - 0x0ff should also be skipped since
275c825d4dcSJohn Baldwin 	 * those are used for various system I/O devices in ISA
276c825d4dcSJohn Baldwin 	 * systems.
277c825d4dcSJohn Baldwin 	 */
278c825d4dcSJohn Baldwin 	if (start <= 65535) {
279c825d4dcSJohn Baldwin 		if (start < 0x100 || (start & 0x300) != 0) {
280c825d4dcSJohn Baldwin 			start &= ~0x3ff;
281c825d4dcSJohn Baldwin 			start += 0x400;
282c825d4dcSJohn Baldwin 		}
283c825d4dcSJohn Baldwin 	}
284c825d4dcSJohn Baldwin 
285c825d4dcSJohn Baldwin 	/* ISA aliases are only in the lower 64KB of I/O space. */
286c825d4dcSJohn Baldwin 	while (start <= MIN(end, 65535)) {
287c825d4dcSJohn Baldwin 		next_end = MIN(start | 0xff, end);
288c825d4dcSJohn Baldwin 		cb(start, next_end, arg);
289c825d4dcSJohn Baldwin 		start += 0x400;
290c825d4dcSJohn Baldwin 	}
291c825d4dcSJohn Baldwin 
292c825d4dcSJohn Baldwin 	if (start <= end)
293c825d4dcSJohn Baldwin 		cb(start, end, arg);
294c825d4dcSJohn Baldwin }
295c825d4dcSJohn Baldwin 
296c825d4dcSJohn Baldwin static void
297c825d4dcSJohn Baldwin count_ranges(u_long start, u_long end, void *arg)
298c825d4dcSJohn Baldwin {
299c825d4dcSJohn Baldwin 	int *countp;
300c825d4dcSJohn Baldwin 
301c825d4dcSJohn Baldwin 	countp = arg;
302c825d4dcSJohn Baldwin 	(*countp)++;
303c825d4dcSJohn Baldwin }
304c825d4dcSJohn Baldwin 
305c825d4dcSJohn Baldwin struct alloc_state {
306c825d4dcSJohn Baldwin 	struct resource **res;
307c825d4dcSJohn Baldwin 	struct pcib_softc *sc;
308c825d4dcSJohn Baldwin 	int count, error;
309c825d4dcSJohn Baldwin };
310c825d4dcSJohn Baldwin 
311c825d4dcSJohn Baldwin static void
312c825d4dcSJohn Baldwin alloc_ranges(u_long start, u_long end, void *arg)
313c825d4dcSJohn Baldwin {
314c825d4dcSJohn Baldwin 	struct alloc_state *as;
315c825d4dcSJohn Baldwin 	struct pcib_window *w;
316c825d4dcSJohn Baldwin 	int rid;
317c825d4dcSJohn Baldwin 
318c825d4dcSJohn Baldwin 	as = arg;
319c825d4dcSJohn Baldwin 	if (as->error != 0)
320c825d4dcSJohn Baldwin 		return;
321c825d4dcSJohn Baldwin 
322c825d4dcSJohn Baldwin 	w = &as->sc->io;
323c825d4dcSJohn Baldwin 	rid = w->reg;
324c825d4dcSJohn Baldwin 	if (bootverbose)
325c825d4dcSJohn Baldwin 		device_printf(as->sc->dev,
326c825d4dcSJohn Baldwin 		    "allocating non-ISA range %#lx-%#lx\n", start, end);
327c825d4dcSJohn Baldwin 	as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT,
328c825d4dcSJohn Baldwin 	    &rid, start, end, end - start + 1, 0);
329c825d4dcSJohn Baldwin 	if (as->res[as->count] == NULL)
330c825d4dcSJohn Baldwin 		as->error = ENXIO;
331c825d4dcSJohn Baldwin 	else
332c825d4dcSJohn Baldwin 		as->count++;
333c825d4dcSJohn Baldwin }
334c825d4dcSJohn Baldwin 
335c825d4dcSJohn Baldwin static int
336c825d4dcSJohn Baldwin pcib_alloc_nonisa_ranges(struct pcib_softc *sc, u_long start, u_long end)
337c825d4dcSJohn Baldwin {
338c825d4dcSJohn Baldwin 	struct alloc_state as;
339c825d4dcSJohn Baldwin 	int i, new_count;
340c825d4dcSJohn Baldwin 
341c825d4dcSJohn Baldwin 	/* First, see how many ranges we need. */
342c825d4dcSJohn Baldwin 	new_count = 0;
343c825d4dcSJohn Baldwin 	pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count);
344c825d4dcSJohn Baldwin 
345c825d4dcSJohn Baldwin 	/* Second, allocate the ranges. */
346c825d4dcSJohn Baldwin 	as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF,
347c825d4dcSJohn Baldwin 	    M_WAITOK);
348c825d4dcSJohn Baldwin 	as.sc = sc;
349c825d4dcSJohn Baldwin 	as.count = 0;
350c825d4dcSJohn Baldwin 	as.error = 0;
351c825d4dcSJohn Baldwin 	pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as);
352c825d4dcSJohn Baldwin 	if (as.error != 0) {
353c825d4dcSJohn Baldwin 		for (i = 0; i < as.count; i++)
354c825d4dcSJohn Baldwin 			bus_release_resource(sc->dev, SYS_RES_IOPORT,
355c825d4dcSJohn Baldwin 			    sc->io.reg, as.res[i]);
356c825d4dcSJohn Baldwin 		free(as.res, M_DEVBUF);
357c825d4dcSJohn Baldwin 		return (as.error);
358c825d4dcSJohn Baldwin 	}
359c825d4dcSJohn Baldwin 	KASSERT(as.count == new_count, ("%s: count mismatch", __func__));
360c825d4dcSJohn Baldwin 
361c825d4dcSJohn Baldwin 	/* Third, add the ranges to the window. */
362c825d4dcSJohn Baldwin 	pcib_add_window_resources(&sc->io, as.res, as.count);
363c825d4dcSJohn Baldwin 	free(as.res, M_DEVBUF);
364c825d4dcSJohn Baldwin 	return (0);
365c825d4dcSJohn Baldwin }
366c825d4dcSJohn Baldwin 
36783c41143SJohn Baldwin static void
36883c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type,
36983c41143SJohn Baldwin     int flags, pci_addr_t max_address)
37083c41143SJohn Baldwin {
371c825d4dcSJohn Baldwin 	struct resource *res;
37283c41143SJohn Baldwin 	char buf[64];
37383c41143SJohn Baldwin 	int error, rid;
37483c41143SJohn Baldwin 
37583c41143SJohn Baldwin 	if (max_address != (u_long)max_address)
37683c41143SJohn Baldwin 		max_address = ~0ul;
37783c41143SJohn Baldwin 	w->rman.rm_start = 0;
37883c41143SJohn Baldwin 	w->rman.rm_end = max_address;
37983c41143SJohn Baldwin 	w->rman.rm_type = RMAN_ARRAY;
38083c41143SJohn Baldwin 	snprintf(buf, sizeof(buf), "%s %s window",
38183c41143SJohn Baldwin 	    device_get_nameunit(sc->dev), w->name);
38283c41143SJohn Baldwin 	w->rman.rm_descr = strdup(buf, M_DEVBUF);
38383c41143SJohn Baldwin 	error = rman_init(&w->rman);
38483c41143SJohn Baldwin 	if (error)
38583c41143SJohn Baldwin 		panic("Failed to initialize %s %s rman",
38683c41143SJohn Baldwin 		    device_get_nameunit(sc->dev), w->name);
38783c41143SJohn Baldwin 
38883c41143SJohn Baldwin 	if (!pcib_is_window_open(w))
38983c41143SJohn Baldwin 		return;
39083c41143SJohn Baldwin 
39183c41143SJohn Baldwin 	if (w->base > max_address || w->limit > max_address) {
39283c41143SJohn Baldwin 		device_printf(sc->dev,
39383c41143SJohn Baldwin 		    "initial %s window has too many bits, ignoring\n", w->name);
39483c41143SJohn Baldwin 		return;
39583c41143SJohn Baldwin 	}
396c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE)
397c825d4dcSJohn Baldwin 		(void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit);
398c825d4dcSJohn Baldwin 	else {
39983c41143SJohn Baldwin 		rid = w->reg;
400c825d4dcSJohn Baldwin 		res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit,
40183c41143SJohn Baldwin 		    w->limit - w->base + 1, flags);
402c825d4dcSJohn Baldwin 		if (res != NULL)
403c825d4dcSJohn Baldwin 			pcib_add_window_resources(w, &res, 1);
404c825d4dcSJohn Baldwin 	}
40583c41143SJohn Baldwin 	if (w->res == NULL) {
40683c41143SJohn Baldwin 		device_printf(sc->dev,
40783c41143SJohn Baldwin 		    "failed to allocate initial %s window: %#jx-%#jx\n",
40883c41143SJohn Baldwin 		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
40983c41143SJohn Baldwin 		w->base = max_address;
41083c41143SJohn Baldwin 		w->limit = 0;
41183c41143SJohn Baldwin 		pcib_write_windows(sc, w->mask);
41283c41143SJohn Baldwin 		return;
41383c41143SJohn Baldwin 	}
41483c41143SJohn Baldwin 	pcib_activate_window(sc, type);
41583c41143SJohn Baldwin }
41683c41143SJohn Baldwin 
41783c41143SJohn Baldwin /*
41883c41143SJohn Baldwin  * Initialize I/O windows.
41983c41143SJohn Baldwin  */
42083c41143SJohn Baldwin static void
42183c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc)
42283c41143SJohn Baldwin {
42383c41143SJohn Baldwin 	pci_addr_t max;
42483c41143SJohn Baldwin 	device_t dev;
42583c41143SJohn Baldwin 	uint32_t val;
42683c41143SJohn Baldwin 
42783c41143SJohn Baldwin 	dev = sc->dev;
42883c41143SJohn Baldwin 
4290070c94bSJohn Baldwin 	if (pci_clear_pcib) {
4300070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
4310070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2);
4320070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1);
4330070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2);
4340070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2);
4350070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2);
4360070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
4370070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4);
4380070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2);
4390070c94bSJohn Baldwin 		pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4);
4400070c94bSJohn Baldwin 	}
4410070c94bSJohn Baldwin 
44283c41143SJohn Baldwin 	/* Determine if the I/O port window is implemented. */
44383c41143SJohn Baldwin 	val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
44483c41143SJohn Baldwin 	if (val == 0) {
44583c41143SJohn Baldwin 		/*
44683c41143SJohn Baldwin 		 * If 'val' is zero, then only 16-bits of I/O space
44783c41143SJohn Baldwin 		 * are supported.
44883c41143SJohn Baldwin 		 */
44983c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
45083c41143SJohn Baldwin 		if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) {
45183c41143SJohn Baldwin 			sc->io.valid = 1;
45283c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOBASEL_1, 0, 1);
45383c41143SJohn Baldwin 		}
45483c41143SJohn Baldwin 	} else
45583c41143SJohn Baldwin 		sc->io.valid = 1;
45683c41143SJohn Baldwin 
45783c41143SJohn Baldwin 	/* Read the existing I/O port window. */
45883c41143SJohn Baldwin 	if (sc->io.valid) {
45983c41143SJohn Baldwin 		sc->io.reg = PCIR_IOBASEL_1;
46083c41143SJohn Baldwin 		sc->io.step = 12;
46183c41143SJohn Baldwin 		sc->io.mask = WIN_IO;
46283c41143SJohn Baldwin 		sc->io.name = "I/O port";
46383c41143SJohn Baldwin 		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
46483c41143SJohn Baldwin 			sc->io.base = PCI_PPBIOBASE(
46583c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOBASEH_1, 2), val);
46683c41143SJohn Baldwin 			sc->io.limit = PCI_PPBIOLIMIT(
46783c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITH_1, 2),
46883c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
46983c41143SJohn Baldwin 			max = 0xffffffff;
47083c41143SJohn Baldwin 		} else {
47183c41143SJohn Baldwin 			sc->io.base = PCI_PPBIOBASE(0, val);
47283c41143SJohn Baldwin 			sc->io.limit = PCI_PPBIOLIMIT(0,
47383c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
47483c41143SJohn Baldwin 			max = 0xffff;
47583c41143SJohn Baldwin 		}
47683c41143SJohn Baldwin 		pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max);
47783c41143SJohn Baldwin 	}
47883c41143SJohn Baldwin 
47983c41143SJohn Baldwin 	/* Read the existing memory window. */
48083c41143SJohn Baldwin 	sc->mem.valid = 1;
48183c41143SJohn Baldwin 	sc->mem.reg = PCIR_MEMBASE_1;
48283c41143SJohn Baldwin 	sc->mem.step = 20;
48383c41143SJohn Baldwin 	sc->mem.mask = WIN_MEM;
48483c41143SJohn Baldwin 	sc->mem.name = "memory";
48583c41143SJohn Baldwin 	sc->mem.base = PCI_PPBMEMBASE(0,
48683c41143SJohn Baldwin 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
48783c41143SJohn Baldwin 	sc->mem.limit = PCI_PPBMEMLIMIT(0,
48883c41143SJohn Baldwin 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
48983c41143SJohn Baldwin 	pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff);
49083c41143SJohn Baldwin 
49183c41143SJohn Baldwin 	/* Determine if the prefetchable memory window is implemented. */
49283c41143SJohn Baldwin 	val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
49383c41143SJohn Baldwin 	if (val == 0) {
49483c41143SJohn Baldwin 		/*
49583c41143SJohn Baldwin 		 * If 'val' is zero, then only 32-bits of memory space
49683c41143SJohn Baldwin 		 * are supported.
49783c41143SJohn Baldwin 		 */
49883c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
49983c41143SJohn Baldwin 		if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) {
50083c41143SJohn Baldwin 			sc->pmem.valid = 1;
50183c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMBASEL_1, 0, 2);
50283c41143SJohn Baldwin 		}
50383c41143SJohn Baldwin 	} else
50483c41143SJohn Baldwin 		sc->pmem.valid = 1;
50583c41143SJohn Baldwin 
50683c41143SJohn Baldwin 	/* Read the existing prefetchable memory window. */
50783c41143SJohn Baldwin 	if (sc->pmem.valid) {
50883c41143SJohn Baldwin 		sc->pmem.reg = PCIR_PMBASEL_1;
50983c41143SJohn Baldwin 		sc->pmem.step = 20;
51083c41143SJohn Baldwin 		sc->pmem.mask = WIN_PMEM;
51183c41143SJohn Baldwin 		sc->pmem.name = "prefetch";
51283c41143SJohn Baldwin 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
51383c41143SJohn Baldwin 			sc->pmem.base = PCI_PPBMEMBASE(
51483c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMBASEH_1, 4), val);
51583c41143SJohn Baldwin 			sc->pmem.limit = PCI_PPBMEMLIMIT(
51683c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITH_1, 4),
51783c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
51883c41143SJohn Baldwin 			max = 0xffffffffffffffff;
51983c41143SJohn Baldwin 		} else {
52083c41143SJohn Baldwin 			sc->pmem.base = PCI_PPBMEMBASE(0, val);
52183c41143SJohn Baldwin 			sc->pmem.limit = PCI_PPBMEMLIMIT(0,
52283c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
52383c41143SJohn Baldwin 			max = 0xffffffff;
52483c41143SJohn Baldwin 		}
52583c41143SJohn Baldwin 		pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY,
52683c41143SJohn Baldwin 		    RF_PREFETCHABLE, max);
52783c41143SJohn Baldwin 	}
52883c41143SJohn Baldwin }
52983c41143SJohn Baldwin 
530*4edef187SJohn Baldwin #ifdef PCI_RES_BUS
531*4edef187SJohn Baldwin /*
532*4edef187SJohn Baldwin  * Allocate a suitable secondary bus for this bridge if needed and
533*4edef187SJohn Baldwin  * initialize the resource manager for the secondary bus range.  Note
534*4edef187SJohn Baldwin  * that the minimum count is a desired value and this may allocate a
535*4edef187SJohn Baldwin  * smaller range.
536*4edef187SJohn Baldwin  */
537*4edef187SJohn Baldwin void
538*4edef187SJohn Baldwin pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count)
539*4edef187SJohn Baldwin {
540*4edef187SJohn Baldwin 	char buf[64];
541*4edef187SJohn Baldwin 	int error, rid;
542*4edef187SJohn Baldwin 
543*4edef187SJohn Baldwin 	switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) {
544*4edef187SJohn Baldwin 	case PCIM_HDRTYPE_BRIDGE:
545*4edef187SJohn Baldwin 		bus->sub_reg = PCIR_SUBBUS_1;
546*4edef187SJohn Baldwin 		break;
547*4edef187SJohn Baldwin 	case PCIM_HDRTYPE_CARDBUS:
548*4edef187SJohn Baldwin 		bus->sub_reg = PCIR_SUBBUS_2;
549*4edef187SJohn Baldwin 		break;
550*4edef187SJohn Baldwin 	default:
551*4edef187SJohn Baldwin 		panic("not a PCI bridge");
552*4edef187SJohn Baldwin 	}
553*4edef187SJohn Baldwin 	bus->dev = dev;
554*4edef187SJohn Baldwin 	bus->rman.rm_start = 0;
555*4edef187SJohn Baldwin 	bus->rman.rm_end = PCI_BUSMAX;
556*4edef187SJohn Baldwin 	bus->rman.rm_type = RMAN_ARRAY;
557*4edef187SJohn Baldwin 	snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev));
558*4edef187SJohn Baldwin 	bus->rman.rm_descr = strdup(buf, M_DEVBUF);
559*4edef187SJohn Baldwin 	error = rman_init(&bus->rman);
560*4edef187SJohn Baldwin 	if (error)
561*4edef187SJohn Baldwin 		panic("Failed to initialize %s bus number rman",
562*4edef187SJohn Baldwin 		    device_get_nameunit(dev));
563*4edef187SJohn Baldwin 
564*4edef187SJohn Baldwin 	/*
565*4edef187SJohn Baldwin 	 * Allocate a bus range.  This will return an existing bus range
566*4edef187SJohn Baldwin 	 * if one exists, or a new bus range if one does not.
567*4edef187SJohn Baldwin 	 */
568*4edef187SJohn Baldwin 	rid = 0;
569*4edef187SJohn Baldwin 	bus->res = bus_alloc_resource(dev, PCI_RES_BUS, &rid, 0ul, ~0ul,
570*4edef187SJohn Baldwin 	    min_count, 0);
571*4edef187SJohn Baldwin 	if (bus->res == NULL) {
572*4edef187SJohn Baldwin 		/*
573*4edef187SJohn Baldwin 		 * Fall back to just allocating a range of a single bus
574*4edef187SJohn Baldwin 		 * number.
575*4edef187SJohn Baldwin 		 */
576*4edef187SJohn Baldwin 		bus->res = bus_alloc_resource(dev, PCI_RES_BUS, &rid, 0ul, ~0ul,
577*4edef187SJohn Baldwin 		    1, 0);
578*4edef187SJohn Baldwin 	} else if (rman_get_size(bus->res) < min_count)
579*4edef187SJohn Baldwin 		/*
580*4edef187SJohn Baldwin 		 * Attempt to grow the existing range to satisfy the
581*4edef187SJohn Baldwin 		 * minimum desired count.
582*4edef187SJohn Baldwin 		 */
583*4edef187SJohn Baldwin 		(void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res,
584*4edef187SJohn Baldwin 		    rman_get_start(bus->res), rman_get_start(bus->res) +
585*4edef187SJohn Baldwin 		    min_count - 1);
586*4edef187SJohn Baldwin 
587*4edef187SJohn Baldwin 	/*
588*4edef187SJohn Baldwin 	 * Add the initial resource to the rman.
589*4edef187SJohn Baldwin 	 */
590*4edef187SJohn Baldwin 	if (bus->res != NULL) {
591*4edef187SJohn Baldwin 		error = rman_manage_region(&bus->rman, rman_get_start(bus->res),
592*4edef187SJohn Baldwin 		    rman_get_end(bus->res));
593*4edef187SJohn Baldwin 		if (error)
594*4edef187SJohn Baldwin 			panic("Failed to add resource to rman");
595*4edef187SJohn Baldwin 		bus->sec = rman_get_start(bus->res);
596*4edef187SJohn Baldwin 		bus->sub = rman_get_end(bus->res);
597*4edef187SJohn Baldwin 	}
598*4edef187SJohn Baldwin }
599*4edef187SJohn Baldwin 
600*4edef187SJohn Baldwin static struct resource *
601*4edef187SJohn Baldwin pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid,
602*4edef187SJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
603*4edef187SJohn Baldwin {
604*4edef187SJohn Baldwin 	struct resource *res;
605*4edef187SJohn Baldwin 
606*4edef187SJohn Baldwin 	res = rman_reserve_resource(&bus->rman, start, end, count, flags,
607*4edef187SJohn Baldwin 	    child);
608*4edef187SJohn Baldwin 	if (res == NULL)
609*4edef187SJohn Baldwin 		return (NULL);
610*4edef187SJohn Baldwin 
611*4edef187SJohn Baldwin 	if (bootverbose)
612*4edef187SJohn Baldwin 		device_printf(bus->dev,
613*4edef187SJohn Baldwin 		    "allocated bus range (%lu-%lu) for rid %d of %s\n",
614*4edef187SJohn Baldwin 		    rman_get_start(res), rman_get_end(res), *rid,
615*4edef187SJohn Baldwin 		    pcib_child_name(child));
616*4edef187SJohn Baldwin 	rman_set_rid(res, *rid);
617*4edef187SJohn Baldwin 	return (res);
618*4edef187SJohn Baldwin }
619*4edef187SJohn Baldwin 
620*4edef187SJohn Baldwin /*
621*4edef187SJohn Baldwin  * Attempt to grow the secondary bus range.  This is much simpler than
622*4edef187SJohn Baldwin  * for I/O windows as the range can only be grown by increasing
623*4edef187SJohn Baldwin  * subbus.
624*4edef187SJohn Baldwin  */
625*4edef187SJohn Baldwin static int
626*4edef187SJohn Baldwin pcib_grow_subbus(struct pcib_secbus *bus, u_long new_end)
627*4edef187SJohn Baldwin {
628*4edef187SJohn Baldwin 	u_long old_end;
629*4edef187SJohn Baldwin 	int error;
630*4edef187SJohn Baldwin 
631*4edef187SJohn Baldwin 	old_end = rman_get_end(bus->res);
632*4edef187SJohn Baldwin 	KASSERT(new_end > old_end, ("attempt to shrink subbus"));
633*4edef187SJohn Baldwin 	error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res,
634*4edef187SJohn Baldwin 	    rman_get_start(bus->res), new_end);
635*4edef187SJohn Baldwin 	if (error)
636*4edef187SJohn Baldwin 		return (error);
637*4edef187SJohn Baldwin 	if (bootverbose)
638*4edef187SJohn Baldwin 		device_printf(bus->dev, "grew bus range to %lu-%lu\n",
639*4edef187SJohn Baldwin 		    rman_get_start(bus->res), rman_get_end(bus->res));
640*4edef187SJohn Baldwin 	error = rman_manage_region(&bus->rman, old_end + 1,
641*4edef187SJohn Baldwin 	    rman_get_end(bus->res));
642*4edef187SJohn Baldwin 	if (error)
643*4edef187SJohn Baldwin 		panic("Failed to add resource to rman");
644*4edef187SJohn Baldwin 	bus->sub = rman_get_end(bus->res);
645*4edef187SJohn Baldwin 	pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1);
646*4edef187SJohn Baldwin 	return (0);
647*4edef187SJohn Baldwin }
648*4edef187SJohn Baldwin 
649*4edef187SJohn Baldwin struct resource *
650*4edef187SJohn Baldwin pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid,
651*4edef187SJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
652*4edef187SJohn Baldwin {
653*4edef187SJohn Baldwin 	struct resource *res;
654*4edef187SJohn Baldwin 	u_long start_free, end_free, new_end;
655*4edef187SJohn Baldwin 
656*4edef187SJohn Baldwin 	/*
657*4edef187SJohn Baldwin 	 * First, see if the request can be satisified by the existing
658*4edef187SJohn Baldwin 	 * bus range.
659*4edef187SJohn Baldwin 	 */
660*4edef187SJohn Baldwin 	res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags);
661*4edef187SJohn Baldwin 	if (res != NULL)
662*4edef187SJohn Baldwin 		return (res);
663*4edef187SJohn Baldwin 
664*4edef187SJohn Baldwin 	/*
665*4edef187SJohn Baldwin 	 * Figure out a range to grow the bus range.  First, find the
666*4edef187SJohn Baldwin 	 * first bus number after the last allocated bus in the rman and
667*4edef187SJohn Baldwin 	 * enforce that as a minimum starting point for the range.
668*4edef187SJohn Baldwin 	 */
669*4edef187SJohn Baldwin 	if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 ||
670*4edef187SJohn Baldwin 	    end_free != bus->sub)
671*4edef187SJohn Baldwin 		start_free = bus->sub + 1;
672*4edef187SJohn Baldwin 	if (start_free < start)
673*4edef187SJohn Baldwin 		start_free = start;
674*4edef187SJohn Baldwin 	new_end = start_free + count - 1;
675*4edef187SJohn Baldwin 
676*4edef187SJohn Baldwin 	/*
677*4edef187SJohn Baldwin 	 * See if this new range would satisfy the request if it
678*4edef187SJohn Baldwin 	 * succeeds.
679*4edef187SJohn Baldwin 	 */
680*4edef187SJohn Baldwin 	if (new_end > end)
681*4edef187SJohn Baldwin 		return (NULL);
682*4edef187SJohn Baldwin 
683*4edef187SJohn Baldwin 	/* Finally, attempt to grow the existing resource. */
684*4edef187SJohn Baldwin 	if (bootverbose) {
685*4edef187SJohn Baldwin 		device_printf(bus->dev,
686*4edef187SJohn Baldwin 		    "attempting to grow bus range for %lu buses\n", count);
687*4edef187SJohn Baldwin 		printf("\tback candidate range: %lu-%lu\n", start_free,
688*4edef187SJohn Baldwin 		    new_end);
689*4edef187SJohn Baldwin 	}
690*4edef187SJohn Baldwin 	if (pcib_grow_subbus(bus, new_end) == 0)
691*4edef187SJohn Baldwin 		return (pcib_suballoc_bus(bus, child, rid, start, end, count,
692*4edef187SJohn Baldwin 		    flags));
693*4edef187SJohn Baldwin 	return (NULL);
694*4edef187SJohn Baldwin }
695*4edef187SJohn Baldwin #endif
696*4edef187SJohn Baldwin 
69783c41143SJohn Baldwin #else
69883c41143SJohn Baldwin 
699bb0d0a8eSMike Smith /*
700b0a2d4b8SWarner Losh  * Is the prefetch window open (eg, can we allocate memory in it?)
701b0a2d4b8SWarner Losh  */
702b0a2d4b8SWarner Losh static int
703b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc)
704b0a2d4b8SWarner Losh {
705b0a2d4b8SWarner Losh 	return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
706b0a2d4b8SWarner Losh }
707b0a2d4b8SWarner Losh 
708b0a2d4b8SWarner Losh /*
709b0a2d4b8SWarner Losh  * Is the nonprefetch window open (eg, can we allocate memory in it?)
710b0a2d4b8SWarner Losh  */
711b0a2d4b8SWarner Losh static int
712b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc)
713b0a2d4b8SWarner Losh {
714b0a2d4b8SWarner Losh 	return (sc->membase > 0 && sc->membase < sc->memlimit);
715b0a2d4b8SWarner Losh }
716b0a2d4b8SWarner Losh 
717b0a2d4b8SWarner Losh /*
718b0a2d4b8SWarner Losh  * Is the io window open (eg, can we allocate ports in it?)
719b0a2d4b8SWarner Losh  */
720b0a2d4b8SWarner Losh static int
721b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc)
722b0a2d4b8SWarner Losh {
723b0a2d4b8SWarner Losh 	return (sc->iobase > 0 && sc->iobase < sc->iolimit);
724b0a2d4b8SWarner Losh }
725b0a2d4b8SWarner Losh 
726b0a2d4b8SWarner Losh /*
727e36af292SJung-uk Kim  * Get current I/O decode.
728e36af292SJung-uk Kim  */
729e36af292SJung-uk Kim static void
730e36af292SJung-uk Kim pcib_get_io_decode(struct pcib_softc *sc)
731e36af292SJung-uk Kim {
732e36af292SJung-uk Kim 	device_t	dev;
733e36af292SJung-uk Kim 	uint32_t	iolow;
734e36af292SJung-uk Kim 
735e36af292SJung-uk Kim 	dev = sc->dev;
736e36af292SJung-uk Kim 
737e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
738e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
739e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(
740e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow);
741e36af292SJung-uk Kim 	else
742e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(0, iolow);
743e36af292SJung-uk Kim 
744e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
745e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
746e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(
747e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow);
748e36af292SJung-uk Kim 	else
749e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(0, iolow);
750e36af292SJung-uk Kim }
751e36af292SJung-uk Kim 
752e36af292SJung-uk Kim /*
753e36af292SJung-uk Kim  * Get current memory decode.
754e36af292SJung-uk Kim  */
755e36af292SJung-uk Kim static void
756e36af292SJung-uk Kim pcib_get_mem_decode(struct pcib_softc *sc)
757e36af292SJung-uk Kim {
758e36af292SJung-uk Kim 	device_t	dev;
759e36af292SJung-uk Kim 	pci_addr_t	pmemlow;
760e36af292SJung-uk Kim 
761e36af292SJung-uk Kim 	dev = sc->dev;
762e36af292SJung-uk Kim 
763e36af292SJung-uk Kim 	sc->membase = PCI_PPBMEMBASE(0,
764e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
765e36af292SJung-uk Kim 	sc->memlimit = PCI_PPBMEMLIMIT(0,
766e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
767e36af292SJung-uk Kim 
768e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2);
769e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
770e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(
771e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow);
772e36af292SJung-uk Kim 	else
773e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(0, pmemlow);
774e36af292SJung-uk Kim 
775e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2);
776e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
777e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(
778e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow);
779e36af292SJung-uk Kim 	else
780e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow);
781e36af292SJung-uk Kim }
782e36af292SJung-uk Kim 
783e36af292SJung-uk Kim /*
784e36af292SJung-uk Kim  * Restore previous I/O decode.
785e36af292SJung-uk Kim  */
786e36af292SJung-uk Kim static void
787e36af292SJung-uk Kim pcib_set_io_decode(struct pcib_softc *sc)
788e36af292SJung-uk Kim {
789e36af292SJung-uk Kim 	device_t	dev;
790e36af292SJung-uk Kim 	uint32_t	iohi;
791e36af292SJung-uk Kim 
792e36af292SJung-uk Kim 	dev = sc->dev;
793e36af292SJung-uk Kim 
794e36af292SJung-uk Kim 	iohi = sc->iobase >> 16;
795e36af292SJung-uk Kim 	if (iohi > 0)
796e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2);
797e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1);
798e36af292SJung-uk Kim 
799e36af292SJung-uk Kim 	iohi = sc->iolimit >> 16;
800e36af292SJung-uk Kim 	if (iohi > 0)
801e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2);
802e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1);
803e36af292SJung-uk Kim }
804e36af292SJung-uk Kim 
805e36af292SJung-uk Kim /*
806e36af292SJung-uk Kim  * Restore previous memory decode.
807e36af292SJung-uk Kim  */
808e36af292SJung-uk Kim static void
809e36af292SJung-uk Kim pcib_set_mem_decode(struct pcib_softc *sc)
810e36af292SJung-uk Kim {
811e36af292SJung-uk Kim 	device_t	dev;
812e36af292SJung-uk Kim 	pci_addr_t	pmemhi;
813e36af292SJung-uk Kim 
814e36af292SJung-uk Kim 	dev = sc->dev;
815e36af292SJung-uk Kim 
816e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2);
817e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2);
818e36af292SJung-uk Kim 
819e36af292SJung-uk Kim 	pmemhi = sc->pmembase >> 32;
820e36af292SJung-uk Kim 	if (pmemhi > 0)
821e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4);
822e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2);
823e36af292SJung-uk Kim 
824e36af292SJung-uk Kim 	pmemhi = sc->pmemlimit >> 32;
825e36af292SJung-uk Kim 	if (pmemhi > 0)
826e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4);
827e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2);
828e36af292SJung-uk Kim }
82983c41143SJohn Baldwin #endif
830e36af292SJung-uk Kim 
831e36af292SJung-uk Kim /*
832e36af292SJung-uk Kim  * Get current bridge configuration.
833e36af292SJung-uk Kim  */
834e36af292SJung-uk Kim static void
835e36af292SJung-uk Kim pcib_cfg_save(struct pcib_softc *sc)
836e36af292SJung-uk Kim {
837e36af292SJung-uk Kim 	device_t	dev;
838e36af292SJung-uk Kim 
839e36af292SJung-uk Kim 	dev = sc->dev;
840e36af292SJung-uk Kim 
841e36af292SJung-uk Kim 	sc->command = pci_read_config(dev, PCIR_COMMAND, 2);
842e36af292SJung-uk Kim 	sc->pribus = pci_read_config(dev, PCIR_PRIBUS_1, 1);
843*4edef187SJohn Baldwin 	sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1);
844*4edef187SJohn Baldwin 	sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
845e36af292SJung-uk Kim 	sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
846e36af292SJung-uk Kim 	sc->seclat = pci_read_config(dev, PCIR_SECLAT_1, 1);
84783c41143SJohn Baldwin #ifndef NEW_PCIB
848e36af292SJung-uk Kim 	if (sc->command & PCIM_CMD_PORTEN)
849e36af292SJung-uk Kim 		pcib_get_io_decode(sc);
850e36af292SJung-uk Kim 	if (sc->command & PCIM_CMD_MEMEN)
851e36af292SJung-uk Kim 		pcib_get_mem_decode(sc);
85283c41143SJohn Baldwin #endif
853e36af292SJung-uk Kim }
854e36af292SJung-uk Kim 
855e36af292SJung-uk Kim /*
856e36af292SJung-uk Kim  * Restore previous bridge configuration.
857e36af292SJung-uk Kim  */
858e36af292SJung-uk Kim static void
859e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc)
860e36af292SJung-uk Kim {
861e36af292SJung-uk Kim 	device_t	dev;
862e36af292SJung-uk Kim 
863e36af292SJung-uk Kim 	dev = sc->dev;
864e36af292SJung-uk Kim 
865e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_COMMAND, sc->command, 2);
866e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
867*4edef187SJohn Baldwin 	pci_write_config(dev, PCIR_SECBUS_1, sc->bus.sec, 1);
868*4edef187SJohn Baldwin 	pci_write_config(dev, PCIR_SUBBUS_1, sc->bus.sub, 1);
869e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_BRIDGECTL_1, sc->bridgectl, 2);
870e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_SECLAT_1, sc->seclat, 1);
87183c41143SJohn Baldwin #ifdef NEW_PCIB
87283c41143SJohn Baldwin 	pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM);
87383c41143SJohn Baldwin #else
874e36af292SJung-uk Kim 	if (sc->command & PCIM_CMD_PORTEN)
875e36af292SJung-uk Kim 		pcib_set_io_decode(sc);
876e36af292SJung-uk Kim 	if (sc->command & PCIM_CMD_MEMEN)
877e36af292SJung-uk Kim 		pcib_set_mem_decode(sc);
87883c41143SJohn Baldwin #endif
879e36af292SJung-uk Kim }
880e36af292SJung-uk Kim 
881e36af292SJung-uk Kim /*
882bb0d0a8eSMike Smith  * Generic device interface
883bb0d0a8eSMike Smith  */
884bb0d0a8eSMike Smith static int
885bb0d0a8eSMike Smith pcib_probe(device_t dev)
886bb0d0a8eSMike Smith {
887bb0d0a8eSMike Smith     if ((pci_get_class(dev) == PCIC_BRIDGE) &&
888bb0d0a8eSMike Smith 	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
889bb0d0a8eSMike Smith 	device_set_desc(dev, "PCI-PCI bridge");
890bb0d0a8eSMike Smith 	return(-10000);
891bb0d0a8eSMike Smith     }
892bb0d0a8eSMike Smith     return(ENXIO);
893bb0d0a8eSMike Smith }
894bb0d0a8eSMike Smith 
8956f0d5884SJohn Baldwin void
8966f0d5884SJohn Baldwin pcib_attach_common(device_t dev)
897bb0d0a8eSMike Smith {
898bb0d0a8eSMike Smith     struct pcib_softc	*sc;
899abf07f13SWarner Losh     struct sysctl_ctx_list *sctx;
900abf07f13SWarner Losh     struct sysctl_oid	*soid;
901c825d4dcSJohn Baldwin     int comma;
902bb0d0a8eSMike Smith 
903bb0d0a8eSMike Smith     sc = device_get_softc(dev);
904bb0d0a8eSMike Smith     sc->dev = dev;
905bb0d0a8eSMike Smith 
9064fa59183SMike Smith     /*
9074fa59183SMike Smith      * Get current bridge configuration.
9084fa59183SMike Smith      */
90955aaf894SMarius Strobl     sc->domain = pci_get_domain(dev);
9104fa59183SMike Smith     sc->secstat = pci_read_config(dev, PCIR_SECSTAT_1, 2);
911e36af292SJung-uk Kim     pcib_cfg_save(sc);
9124fa59183SMike Smith 
9134fa59183SMike Smith     /*
914*4edef187SJohn Baldwin      * The primary bus register should always be the bus of the
915*4edef187SJohn Baldwin      * parent.
916*4edef187SJohn Baldwin      */
917*4edef187SJohn Baldwin     sc->pribus = pci_get_bus(dev);
918*4edef187SJohn Baldwin     pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
919*4edef187SJohn Baldwin 
920*4edef187SJohn Baldwin     /*
921abf07f13SWarner Losh      * Setup sysctl reporting nodes
922abf07f13SWarner Losh      */
923abf07f13SWarner Losh     sctx = device_get_sysctl_ctx(dev);
924abf07f13SWarner Losh     soid = device_get_sysctl_tree(dev);
925abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
926abf07f13SWarner Losh       CTLFLAG_RD, &sc->domain, 0, "Domain number");
927abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
928abf07f13SWarner Losh       CTLFLAG_RD, &sc->pribus, 0, "Primary bus number");
929abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
930*4edef187SJohn Baldwin       CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number");
931abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
932*4edef187SJohn Baldwin       CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number");
933abf07f13SWarner Losh 
934abf07f13SWarner Losh     /*
9354fa59183SMike Smith      * Quirk handling.
9364fa59183SMike Smith      */
9374fa59183SMike Smith     switch (pci_get_devid(dev)) {
938*4edef187SJohn Baldwin #if !defined(NEW_PCIB) && !defined(PCI_RES_BUS)
9394fa59183SMike Smith     case 0x12258086:		/* Intel 82454KX/GX (Orion) */
9404fa59183SMike Smith 	{
941b0cb115fSWarner Losh 	    uint8_t	supbus;
9424fa59183SMike Smith 
9434fa59183SMike Smith 	    supbus = pci_read_config(dev, 0x41, 1);
9444fa59183SMike Smith 	    if (supbus != 0xff) {
945*4edef187SJohn Baldwin 		sc->bus.sec = supbus + 1;
946*4edef187SJohn Baldwin 		sc->bus.sub = supbus + 1;
9474fa59183SMike Smith 	    }
9484fa59183SMike Smith 	    break;
9494fa59183SMike Smith 	}
950*4edef187SJohn Baldwin #endif
9514fa59183SMike Smith 
952e4b59fc5SWarner Losh     /*
953e4b59fc5SWarner Losh      * The i82380FB mobile docking controller is a PCI-PCI bridge,
954e4b59fc5SWarner Losh      * and it is a subtractive bridge.  However, the ProgIf is wrong
955e4b59fc5SWarner Losh      * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
956e4b59fc5SWarner Losh      * happen.  There's also a Toshiba bridge that behaves this
957e4b59fc5SWarner Losh      * way.
958e4b59fc5SWarner Losh      */
959e4b59fc5SWarner Losh     case 0x124b8086:		/* Intel 82380FB Mobile */
960e4b59fc5SWarner Losh     case 0x060513d7:		/* Toshiba ???? */
961e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
962e4b59fc5SWarner Losh 	break;
963c94d6dbeSJung-uk Kim 
964*4edef187SJohn Baldwin #if !defined(NEW_PCIB) && !defined(PCI_RES_BUS)
965c94d6dbeSJung-uk Kim     /* Compaq R3000 BIOS sets wrong subordinate bus number. */
966c94d6dbeSJung-uk Kim     case 0x00dd10de:
967c94d6dbeSJung-uk Kim 	{
968c94d6dbeSJung-uk Kim 	    char *cp;
969c94d6dbeSJung-uk Kim 
9701def0ca6SJung-uk Kim 	    if ((cp = getenv("smbios.planar.maker")) == NULL)
971c94d6dbeSJung-uk Kim 		break;
9721def0ca6SJung-uk Kim 	    if (strncmp(cp, "Compal", 6) != 0) {
9731def0ca6SJung-uk Kim 		freeenv(cp);
974c94d6dbeSJung-uk Kim 		break;
9751def0ca6SJung-uk Kim 	    }
9761def0ca6SJung-uk Kim 	    freeenv(cp);
9771def0ca6SJung-uk Kim 	    if ((cp = getenv("smbios.planar.product")) == NULL)
9781def0ca6SJung-uk Kim 		break;
9791def0ca6SJung-uk Kim 	    if (strncmp(cp, "08A0", 4) != 0) {
9801def0ca6SJung-uk Kim 		freeenv(cp);
9811def0ca6SJung-uk Kim 		break;
9821def0ca6SJung-uk Kim 	    }
9831def0ca6SJung-uk Kim 	    freeenv(cp);
984*4edef187SJohn Baldwin 	    if (sc->bus.sub < 0xa) {
985c94d6dbeSJung-uk Kim 		pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
986*4edef187SJohn Baldwin 		sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
987c94d6dbeSJung-uk Kim 	    }
988c94d6dbeSJung-uk Kim 	    break;
989c94d6dbeSJung-uk Kim 	}
990*4edef187SJohn Baldwin #endif
991e4b59fc5SWarner Losh     }
992e4b59fc5SWarner Losh 
99322bf1c7fSJohn Baldwin     if (pci_msi_device_blacklisted(dev))
99422bf1c7fSJohn Baldwin 	sc->flags |= PCIB_DISABLE_MSI;
99522bf1c7fSJohn Baldwin 
99668e9cbd3SMarius Strobl     if (pci_msix_device_blacklisted(dev))
99768e9cbd3SMarius Strobl 	sc->flags |= PCIB_DISABLE_MSIX;
99868e9cbd3SMarius Strobl 
999e4b59fc5SWarner Losh     /*
1000e4b59fc5SWarner Losh      * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
1001e4b59fc5SWarner Losh      * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
1002e4b59fc5SWarner Losh      * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
1003e4b59fc5SWarner Losh      * This means they act as if they were subtractively decoding
1004e4b59fc5SWarner Losh      * bridges and pass all transactions.  Mark them and real ProgIf 1
1005e4b59fc5SWarner Losh      * parts as subtractive.
1006e4b59fc5SWarner Losh      */
1007e4b59fc5SWarner Losh     if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
1008657d9f9fSJohn Baldwin       pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
1009e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
1010e4b59fc5SWarner Losh 
101183c41143SJohn Baldwin #ifdef NEW_PCIB
1012*4edef187SJohn Baldwin #ifdef PCI_RES_BUS
1013*4edef187SJohn Baldwin     pcib_setup_secbus(dev, &sc->bus, 1);
1014*4edef187SJohn Baldwin #endif
101583c41143SJohn Baldwin     pcib_probe_windows(sc);
101683c41143SJohn Baldwin #endif
1017bb0d0a8eSMike Smith     if (bootverbose) {
101855aaf894SMarius Strobl 	device_printf(dev, "  domain            %d\n", sc->domain);
1019*4edef187SJohn Baldwin 	device_printf(dev, "  secondary bus     %d\n", sc->bus.sec);
1020*4edef187SJohn Baldwin 	device_printf(dev, "  subordinate bus   %d\n", sc->bus.sub);
102183c41143SJohn Baldwin #ifdef NEW_PCIB
102283c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->io))
102383c41143SJohn Baldwin 	    device_printf(dev, "  I/O decode        0x%jx-0x%jx\n",
102483c41143SJohn Baldwin 	      (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit);
102583c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->mem))
102683c41143SJohn Baldwin 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
102783c41143SJohn Baldwin 	      (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit);
102883c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->pmem))
102983c41143SJohn Baldwin 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
103083c41143SJohn Baldwin 	      (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit);
103183c41143SJohn Baldwin #else
103283c41143SJohn Baldwin 	if (pcib_is_io_open(sc))
103383c41143SJohn Baldwin 	    device_printf(dev, "  I/O decode        0x%x-0x%x\n",
103483c41143SJohn Baldwin 	      sc->iobase, sc->iolimit);
1035b0a2d4b8SWarner Losh 	if (pcib_is_nonprefetch_open(sc))
1036b0a2d4b8SWarner Losh 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
1037b0a2d4b8SWarner Losh 	      (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
1038b0a2d4b8SWarner Losh 	if (pcib_is_prefetch_open(sc))
1039b0a2d4b8SWarner Losh 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
1040b0a2d4b8SWarner Losh 	      (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
104183c41143SJohn Baldwin #endif
1042c825d4dcSJohn Baldwin 	if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) ||
1043c825d4dcSJohn Baldwin 	    sc->flags & PCIB_SUBTRACTIVE) {
1044c825d4dcSJohn Baldwin 		device_printf(dev, "  special decode    ");
1045c825d4dcSJohn Baldwin 		comma = 0;
1046c825d4dcSJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) {
1047c825d4dcSJohn Baldwin 			printf("ISA");
1048c825d4dcSJohn Baldwin 			comma = 1;
1049c825d4dcSJohn Baldwin 		}
1050c825d4dcSJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) {
1051c825d4dcSJohn Baldwin 			printf("%sVGA", comma ? ", " : "");
1052c825d4dcSJohn Baldwin 			comma = 1;
1053c825d4dcSJohn Baldwin 		}
1054e4b59fc5SWarner Losh 		if (sc->flags & PCIB_SUBTRACTIVE)
1055c825d4dcSJohn Baldwin 			printf("%ssubtractive", comma ? ", " : "");
1056c825d4dcSJohn Baldwin 		printf("\n");
1057c825d4dcSJohn Baldwin 	}
1058bb0d0a8eSMike Smith     }
1059bb0d0a8eSMike Smith 
1060bb0d0a8eSMike Smith     /*
1061ef888152SJohn Baldwin      * Always enable busmastering on bridges so that transactions
1062ef888152SJohn Baldwin      * initiated on the secondary bus are passed through to the
1063ef888152SJohn Baldwin      * primary bus.
1064ef888152SJohn Baldwin      */
1065ef888152SJohn Baldwin     pci_enable_busmaster(dev);
10666f0d5884SJohn Baldwin }
1067bb0d0a8eSMike Smith 
106838906aedSJohn Baldwin int
10696f0d5884SJohn Baldwin pcib_attach(device_t dev)
10706f0d5884SJohn Baldwin {
10716f0d5884SJohn Baldwin     struct pcib_softc	*sc;
10726f0d5884SJohn Baldwin     device_t		child;
10736f0d5884SJohn Baldwin 
10746f0d5884SJohn Baldwin     pcib_attach_common(dev);
10756f0d5884SJohn Baldwin     sc = device_get_softc(dev);
1076*4edef187SJohn Baldwin     if (sc->bus.sec != 0) {
1077*4edef187SJohn Baldwin 	child = device_add_child(dev, "pci", sc->bus.sec);
1078bb0d0a8eSMike Smith 	if (child != NULL)
1079bb0d0a8eSMike Smith 	    return(bus_generic_attach(dev));
1080bb0d0a8eSMike Smith     }
1081bb0d0a8eSMike Smith 
1082bb0d0a8eSMike Smith     /* no secondary bus; we should have fixed this */
1083bb0d0a8eSMike Smith     return(0);
1084bb0d0a8eSMike Smith }
1085bb0d0a8eSMike Smith 
10866f0d5884SJohn Baldwin int
1087e36af292SJung-uk Kim pcib_suspend(device_t dev)
1088e36af292SJung-uk Kim {
108962508c53SJohn Baldwin 	device_t	pcib;
1090e36af292SJung-uk Kim 	int		dstate, error;
1091e36af292SJung-uk Kim 
1092e36af292SJung-uk Kim 	pcib_cfg_save(device_get_softc(dev));
1093e36af292SJung-uk Kim 	error = bus_generic_suspend(dev);
1094f3e0b109SJung-uk Kim 	if (error == 0 && pci_do_power_suspend) {
1095e36af292SJung-uk Kim 		dstate = PCI_POWERSTATE_D3;
109662508c53SJohn Baldwin 		pcib = device_get_parent(device_get_parent(dev));
109762508c53SJohn Baldwin 		if (PCIB_POWER_FOR_SLEEP(pcib, dev, &dstate) == 0)
1098e36af292SJung-uk Kim 			pci_set_powerstate(dev, dstate);
1099e36af292SJung-uk Kim 	}
1100e36af292SJung-uk Kim 	return (error);
1101e36af292SJung-uk Kim }
1102e36af292SJung-uk Kim 
1103e36af292SJung-uk Kim int
1104e36af292SJung-uk Kim pcib_resume(device_t dev)
1105e36af292SJung-uk Kim {
110662508c53SJohn Baldwin 	device_t	pcib;
1107e36af292SJung-uk Kim 
1108e36af292SJung-uk Kim 	if (pci_do_power_resume) {
110962508c53SJohn Baldwin 		pcib = device_get_parent(device_get_parent(dev));
111062508c53SJohn Baldwin 		if (PCIB_POWER_FOR_SLEEP(pcib, dev, NULL) == 0)
1111e36af292SJung-uk Kim 			pci_set_powerstate(dev, PCI_POWERSTATE_D0);
1112e36af292SJung-uk Kim 	}
1113e36af292SJung-uk Kim 	pcib_cfg_restore(device_get_softc(dev));
1114e36af292SJung-uk Kim 	return (bus_generic_resume(dev));
1115e36af292SJung-uk Kim }
1116e36af292SJung-uk Kim 
1117e36af292SJung-uk Kim int
1118bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1119bb0d0a8eSMike Smith {
1120bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
1121bb0d0a8eSMike Smith 
1122bb0d0a8eSMike Smith     switch (which) {
112355aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
112455aaf894SMarius Strobl 	*result = sc->domain;
112555aaf894SMarius Strobl 	return(0);
1126bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
1127*4edef187SJohn Baldwin 	*result = sc->bus.sec;
1128bb0d0a8eSMike Smith 	return(0);
1129bb0d0a8eSMike Smith     }
1130bb0d0a8eSMike Smith     return(ENOENT);
1131bb0d0a8eSMike Smith }
1132bb0d0a8eSMike Smith 
11336f0d5884SJohn Baldwin int
1134bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1135bb0d0a8eSMike Smith {
1136bb0d0a8eSMike Smith 
1137bb0d0a8eSMike Smith     switch (which) {
113855aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
113955aaf894SMarius Strobl 	return(EINVAL);
1140bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
1141*4edef187SJohn Baldwin 	return(EINVAL);
1142bb0d0a8eSMike Smith     }
1143bb0d0a8eSMike Smith     return(ENOENT);
1144bb0d0a8eSMike Smith }
1145bb0d0a8eSMike Smith 
114683c41143SJohn Baldwin #ifdef NEW_PCIB
114783c41143SJohn Baldwin /*
114883c41143SJohn Baldwin  * Attempt to allocate a resource from the existing resources assigned
114983c41143SJohn Baldwin  * to a window.
115083c41143SJohn Baldwin  */
115183c41143SJohn Baldwin static struct resource *
115283c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w,
115383c41143SJohn Baldwin     device_t child, int type, int *rid, u_long start, u_long end, u_long count,
115483c41143SJohn Baldwin     u_int flags)
115583c41143SJohn Baldwin {
115683c41143SJohn Baldwin 	struct resource *res;
115783c41143SJohn Baldwin 
115883c41143SJohn Baldwin 	if (!pcib_is_window_open(w))
115983c41143SJohn Baldwin 		return (NULL);
116083c41143SJohn Baldwin 
116183c41143SJohn Baldwin 	res = rman_reserve_resource(&w->rman, start, end, count,
116283c41143SJohn Baldwin 	    flags & ~RF_ACTIVE, child);
116383c41143SJohn Baldwin 	if (res == NULL)
116483c41143SJohn Baldwin 		return (NULL);
116583c41143SJohn Baldwin 
116683c41143SJohn Baldwin 	if (bootverbose)
116783c41143SJohn Baldwin 		device_printf(sc->dev,
116883c41143SJohn Baldwin 		    "allocated %s range (%#lx-%#lx) for rid %x of %s\n",
116983c41143SJohn Baldwin 		    w->name, rman_get_start(res), rman_get_end(res), *rid,
117083c41143SJohn Baldwin 		    pcib_child_name(child));
117183c41143SJohn Baldwin 	rman_set_rid(res, *rid);
117283c41143SJohn Baldwin 
117383c41143SJohn Baldwin 	/*
117483c41143SJohn Baldwin 	 * If the resource should be active, pass that request up the
117583c41143SJohn Baldwin 	 * tree.  This assumes the parent drivers can handle
117683c41143SJohn Baldwin 	 * activating sub-allocated resources.
117783c41143SJohn Baldwin 	 */
117883c41143SJohn Baldwin 	if (flags & RF_ACTIVE) {
117983c41143SJohn Baldwin 		if (bus_activate_resource(child, type, *rid, res) != 0) {
118083c41143SJohn Baldwin 			rman_release_resource(res);
118183c41143SJohn Baldwin 			return (NULL);
118283c41143SJohn Baldwin 		}
118383c41143SJohn Baldwin 	}
118483c41143SJohn Baldwin 
118583c41143SJohn Baldwin 	return (res);
118683c41143SJohn Baldwin }
118783c41143SJohn Baldwin 
1188c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */
1189c825d4dcSJohn Baldwin static int
1190c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type,
1191c825d4dcSJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
1192c825d4dcSJohn Baldwin {
1193c825d4dcSJohn Baldwin 	struct resource *res;
1194c825d4dcSJohn Baldwin 	u_long base, limit, wmask;
1195c825d4dcSJohn Baldwin 	int rid;
1196c825d4dcSJohn Baldwin 
1197c825d4dcSJohn Baldwin 	/*
1198c825d4dcSJohn Baldwin 	 * If this is an I/O window on a bridge with ISA enable set
1199c825d4dcSJohn Baldwin 	 * and the start address is below 64k, then try to allocate an
1200c825d4dcSJohn Baldwin 	 * initial window of 0x1000 bytes long starting at address
1201c825d4dcSJohn Baldwin 	 * 0xf000 and walking down.  Note that if the original request
1202c825d4dcSJohn Baldwin 	 * was larger than the non-aliased range size of 0x100 our
1203c825d4dcSJohn Baldwin 	 * caller would have raised the start address up to 64k
1204c825d4dcSJohn Baldwin 	 * already.
1205c825d4dcSJohn Baldwin 	 */
1206c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1207c825d4dcSJohn Baldwin 	    start < 65536) {
1208c825d4dcSJohn Baldwin 		for (base = 0xf000; (long)base >= 0; base -= 0x1000) {
1209c825d4dcSJohn Baldwin 			limit = base + 0xfff;
1210c825d4dcSJohn Baldwin 
1211c825d4dcSJohn Baldwin 			/*
1212c825d4dcSJohn Baldwin 			 * Skip ranges that wouldn't work for the
1213c825d4dcSJohn Baldwin 			 * original request.  Note that the actual
1214c825d4dcSJohn Baldwin 			 * window that overlaps are the non-alias
1215c825d4dcSJohn Baldwin 			 * ranges within [base, limit], so this isn't
1216c825d4dcSJohn Baldwin 			 * quite a simple comparison.
1217c825d4dcSJohn Baldwin 			 */
1218c825d4dcSJohn Baldwin 			if (start + count > limit - 0x400)
1219c825d4dcSJohn Baldwin 				continue;
1220c825d4dcSJohn Baldwin 			if (base == 0) {
1221c825d4dcSJohn Baldwin 				/*
1222c825d4dcSJohn Baldwin 				 * The first open region for the window at
1223c825d4dcSJohn Baldwin 				 * 0 is 0x400-0x4ff.
1224c825d4dcSJohn Baldwin 				 */
1225c825d4dcSJohn Baldwin 				if (end - count + 1 < 0x400)
1226c825d4dcSJohn Baldwin 					continue;
1227c825d4dcSJohn Baldwin 			} else {
1228c825d4dcSJohn Baldwin 				if (end - count + 1 < base)
1229c825d4dcSJohn Baldwin 					continue;
1230c825d4dcSJohn Baldwin 			}
1231c825d4dcSJohn Baldwin 
1232c825d4dcSJohn Baldwin 			if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) {
1233c825d4dcSJohn Baldwin 				w->base = base;
1234c825d4dcSJohn Baldwin 				w->limit = limit;
1235c825d4dcSJohn Baldwin 				return (0);
1236c825d4dcSJohn Baldwin 			}
1237c825d4dcSJohn Baldwin 		}
1238c825d4dcSJohn Baldwin 		return (ENOSPC);
1239c825d4dcSJohn Baldwin 	}
1240c825d4dcSJohn Baldwin 
1241c825d4dcSJohn Baldwin 	wmask = (1ul << w->step) - 1;
1242c825d4dcSJohn Baldwin 	if (RF_ALIGNMENT(flags) < w->step) {
1243c825d4dcSJohn Baldwin 		flags &= ~RF_ALIGNMENT_MASK;
1244c825d4dcSJohn Baldwin 		flags |= RF_ALIGNMENT_LOG2(w->step);
1245c825d4dcSJohn Baldwin 	}
1246c825d4dcSJohn Baldwin 	start &= ~wmask;
1247c825d4dcSJohn Baldwin 	end |= wmask;
1248c825d4dcSJohn Baldwin 	count = roundup2(count, 1ul << w->step);
1249c825d4dcSJohn Baldwin 	rid = w->reg;
1250c825d4dcSJohn Baldwin 	res = bus_alloc_resource(sc->dev, type, &rid, start, end, count,
1251c825d4dcSJohn Baldwin 	    flags & ~RF_ACTIVE);
1252c825d4dcSJohn Baldwin 	if (res == NULL)
1253c825d4dcSJohn Baldwin 		return (ENOSPC);
1254c825d4dcSJohn Baldwin 	pcib_add_window_resources(w, &res, 1);
1255c825d4dcSJohn Baldwin 	pcib_activate_window(sc, type);
1256c825d4dcSJohn Baldwin 	w->base = rman_get_start(res);
1257c825d4dcSJohn Baldwin 	w->limit = rman_get_end(res);
1258c825d4dcSJohn Baldwin 	return (0);
1259c825d4dcSJohn Baldwin }
1260c825d4dcSJohn Baldwin 
1261c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */
1262c825d4dcSJohn Baldwin static int
1263c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type,
1264c825d4dcSJohn Baldwin     u_long base, u_long limit)
1265c825d4dcSJohn Baldwin {
1266c825d4dcSJohn Baldwin 	struct resource *res;
1267c825d4dcSJohn Baldwin 	int error, i, force_64k_base;
1268c825d4dcSJohn Baldwin 
1269c825d4dcSJohn Baldwin 	KASSERT(base <= w->base && limit >= w->limit,
1270c825d4dcSJohn Baldwin 	    ("attempting to shrink window"));
1271c825d4dcSJohn Baldwin 
1272c825d4dcSJohn Baldwin 	/*
1273c825d4dcSJohn Baldwin 	 * XXX: pcib_grow_window() doesn't try to do this anyway and
1274c825d4dcSJohn Baldwin 	 * the error handling for all the edge cases would be tedious.
1275c825d4dcSJohn Baldwin 	 */
1276c825d4dcSJohn Baldwin 	KASSERT(limit == w->limit || base == w->base,
1277c825d4dcSJohn Baldwin 	    ("attempting to grow both ends of a window"));
1278c825d4dcSJohn Baldwin 
1279c825d4dcSJohn Baldwin 	/*
1280c825d4dcSJohn Baldwin 	 * Yet more special handling for requests to expand an I/O
1281c825d4dcSJohn Baldwin 	 * window behind an ISA-enabled bridge.  Since I/O windows
1282c825d4dcSJohn Baldwin 	 * have to grow in 0x1000 increments and the end of the 0xffff
1283c825d4dcSJohn Baldwin 	 * range is an alias, growing a window below 64k will always
1284c825d4dcSJohn Baldwin 	 * result in allocating new resources and never adjusting an
1285c825d4dcSJohn Baldwin 	 * existing resource.
1286c825d4dcSJohn Baldwin 	 */
1287c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1288c825d4dcSJohn Baldwin 	    (limit <= 65535 || (base <= 65535 && base != w->base))) {
1289c825d4dcSJohn Baldwin 		KASSERT(limit == w->limit || limit <= 65535,
1290c825d4dcSJohn Baldwin 		    ("attempting to grow both ends across 64k ISA alias"));
1291c825d4dcSJohn Baldwin 
1292c825d4dcSJohn Baldwin 		if (base != w->base)
1293c825d4dcSJohn Baldwin 			error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1);
1294c825d4dcSJohn Baldwin 		else
1295c825d4dcSJohn Baldwin 			error = pcib_alloc_nonisa_ranges(sc, w->limit + 1,
1296c825d4dcSJohn Baldwin 			    limit);
1297c825d4dcSJohn Baldwin 		if (error == 0) {
1298c825d4dcSJohn Baldwin 			w->base = base;
1299c825d4dcSJohn Baldwin 			w->limit = limit;
1300c825d4dcSJohn Baldwin 		}
1301c825d4dcSJohn Baldwin 		return (error);
1302c825d4dcSJohn Baldwin 	}
1303c825d4dcSJohn Baldwin 
1304c825d4dcSJohn Baldwin 	/*
1305c825d4dcSJohn Baldwin 	 * Find the existing resource to adjust.  Usually there is only one,
1306c825d4dcSJohn Baldwin 	 * but for an ISA-enabled bridge we might be growing the I/O window
1307c825d4dcSJohn Baldwin 	 * above 64k and need to find the existing resource that maps all
1308c825d4dcSJohn Baldwin 	 * of the area above 64k.
1309c825d4dcSJohn Baldwin 	 */
1310c825d4dcSJohn Baldwin 	for (i = 0; i < w->count; i++) {
1311c825d4dcSJohn Baldwin 		if (rman_get_end(w->res[i]) == w->limit)
1312c825d4dcSJohn Baldwin 			break;
1313c825d4dcSJohn Baldwin 	}
1314c825d4dcSJohn Baldwin 	KASSERT(i != w->count, ("did not find existing resource"));
1315c825d4dcSJohn Baldwin 	res = w->res[i];
1316c825d4dcSJohn Baldwin 
1317c825d4dcSJohn Baldwin 	/*
1318c825d4dcSJohn Baldwin 	 * Usually the resource we found should match the window's
1319c825d4dcSJohn Baldwin 	 * existing range.  The one exception is the ISA-enabled case
1320c825d4dcSJohn Baldwin 	 * mentioned above in which case the resource should start at
1321c825d4dcSJohn Baldwin 	 * 64k.
1322c825d4dcSJohn Baldwin 	 */
1323c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1324c825d4dcSJohn Baldwin 	    w->base <= 65535) {
1325c825d4dcSJohn Baldwin 		KASSERT(rman_get_start(res) == 65536,
1326c825d4dcSJohn Baldwin 		    ("existing resource mismatch"));
1327c825d4dcSJohn Baldwin 		force_64k_base = 1;
1328c825d4dcSJohn Baldwin 	} else {
1329c825d4dcSJohn Baldwin 		KASSERT(w->base == rman_get_start(res),
1330c825d4dcSJohn Baldwin 		    ("existing resource mismatch"));
1331c825d4dcSJohn Baldwin 		force_64k_base = 0;
1332c825d4dcSJohn Baldwin 	}
1333c825d4dcSJohn Baldwin 
1334c825d4dcSJohn Baldwin 	error = bus_adjust_resource(sc->dev, type, res, force_64k_base ?
1335c825d4dcSJohn Baldwin 	    rman_get_start(res) : base, limit);
1336c825d4dcSJohn Baldwin 	if (error)
1337c825d4dcSJohn Baldwin 		return (error);
1338c825d4dcSJohn Baldwin 
1339c825d4dcSJohn Baldwin 	/* Add the newly allocated region to the resource manager. */
1340c825d4dcSJohn Baldwin 	if (w->base != base) {
1341c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, base, w->base - 1);
1342c825d4dcSJohn Baldwin 		w->base = base;
1343c825d4dcSJohn Baldwin 	} else {
1344c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, w->limit + 1, limit);
1345c825d4dcSJohn Baldwin 		w->limit = limit;
1346c825d4dcSJohn Baldwin 	}
1347c825d4dcSJohn Baldwin 	if (error) {
1348c825d4dcSJohn Baldwin 		if (bootverbose)
1349c825d4dcSJohn Baldwin 			device_printf(sc->dev,
1350c825d4dcSJohn Baldwin 			    "failed to expand %s resource manager\n", w->name);
1351c825d4dcSJohn Baldwin 		(void)bus_adjust_resource(sc->dev, type, res, force_64k_base ?
1352c825d4dcSJohn Baldwin 		    rman_get_start(res) : w->base, w->limit);
1353c825d4dcSJohn Baldwin 	}
1354c825d4dcSJohn Baldwin 	return (error);
1355c825d4dcSJohn Baldwin }
1356c825d4dcSJohn Baldwin 
135783c41143SJohn Baldwin /*
135883c41143SJohn Baldwin  * Attempt to grow a window to make room for a given resource request.
135983c41143SJohn Baldwin  */
136083c41143SJohn Baldwin static int
136183c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type,
136283c41143SJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
136383c41143SJohn Baldwin {
1364a7b5acacSJohn Baldwin 	u_long align, start_free, end_free, front, back, wmask;
1365c825d4dcSJohn Baldwin 	int error;
136683c41143SJohn Baldwin 
136783c41143SJohn Baldwin 	/*
136883c41143SJohn Baldwin 	 * Clamp the desired resource range to the maximum address
136983c41143SJohn Baldwin 	 * this window supports.  Reject impossible requests.
1370c825d4dcSJohn Baldwin 	 *
1371c825d4dcSJohn Baldwin 	 * For I/O port requests behind a bridge with the ISA enable
1372c825d4dcSJohn Baldwin 	 * bit set, force large allocations to start above 64k.
137383c41143SJohn Baldwin 	 */
137483c41143SJohn Baldwin 	if (!w->valid)
137583c41143SJohn Baldwin 		return (EINVAL);
1376c825d4dcSJohn Baldwin 	if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 &&
1377c825d4dcSJohn Baldwin 	    start < 65536)
1378c825d4dcSJohn Baldwin 		start = 65536;
137983c41143SJohn Baldwin 	if (end > w->rman.rm_end)
138083c41143SJohn Baldwin 		end = w->rman.rm_end;
138183c41143SJohn Baldwin 	if (start + count - 1 > end || start + count < start)
138283c41143SJohn Baldwin 		return (EINVAL);
1383a7b5acacSJohn Baldwin 	wmask = (1ul << w->step) - 1;
138483c41143SJohn Baldwin 
138583c41143SJohn Baldwin 	/*
138683c41143SJohn Baldwin 	 * If there is no resource at all, just try to allocate enough
138783c41143SJohn Baldwin 	 * aligned space for this resource.
138883c41143SJohn Baldwin 	 */
138983c41143SJohn Baldwin 	if (w->res == NULL) {
1390c825d4dcSJohn Baldwin 		error = pcib_alloc_new_window(sc, w, type, start, end, count,
1391c825d4dcSJohn Baldwin 		    flags);
1392c825d4dcSJohn Baldwin 		if (error) {
139383c41143SJohn Baldwin 			if (bootverbose)
139483c41143SJohn Baldwin 				device_printf(sc->dev,
139583c41143SJohn Baldwin 		    "failed to allocate initial %s window (%#lx-%#lx,%#lx)\n",
139683c41143SJohn Baldwin 				    w->name, start, end, count);
139783c41143SJohn Baldwin 			return (error);
139883c41143SJohn Baldwin 		}
1399c825d4dcSJohn Baldwin 		if (bootverbose)
1400c825d4dcSJohn Baldwin 			device_printf(sc->dev,
1401c825d4dcSJohn Baldwin 			    "allocated initial %s window of %#jx-%#jx\n",
1402c825d4dcSJohn Baldwin 			    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
140383c41143SJohn Baldwin 		goto updatewin;
140483c41143SJohn Baldwin 	}
140583c41143SJohn Baldwin 
140683c41143SJohn Baldwin 	/*
140783c41143SJohn Baldwin 	 * See if growing the window would help.  Compute the minimum
140883c41143SJohn Baldwin 	 * amount of address space needed on both the front and back
140983c41143SJohn Baldwin 	 * ends of the existing window to satisfy the allocation.
141083c41143SJohn Baldwin 	 *
141183c41143SJohn Baldwin 	 * For each end, build a candidate region adjusting for the
141283c41143SJohn Baldwin 	 * required alignment, etc.  If there is a free region at the
141383c41143SJohn Baldwin 	 * edge of the window, grow from the inner edge of the free
141483c41143SJohn Baldwin 	 * region.  Otherwise grow from the window boundary.
141583c41143SJohn Baldwin 	 *
1416c825d4dcSJohn Baldwin 	 * Growing an I/O window below 64k for a bridge with the ISA
1417c825d4dcSJohn Baldwin 	 * enable bit doesn't require any special magic as the step
1418c825d4dcSJohn Baldwin 	 * size of an I/O window (1k) always includes multiple
1419c825d4dcSJohn Baldwin 	 * non-alias ranges when it is grown in either direction.
1420c825d4dcSJohn Baldwin 	 *
142183c41143SJohn Baldwin 	 * XXX: Special case: if w->res is completely empty and the
142283c41143SJohn Baldwin 	 * request size is larger than w->res, we should find the
142383c41143SJohn Baldwin 	 * optimal aligned buffer containing w->res and allocate that.
142483c41143SJohn Baldwin 	 */
142583c41143SJohn Baldwin 	if (bootverbose)
142683c41143SJohn Baldwin 		device_printf(sc->dev,
142783c41143SJohn Baldwin 		    "attempting to grow %s window for (%#lx-%#lx,%#lx)\n",
142883c41143SJohn Baldwin 		    w->name, start, end, count);
142983c41143SJohn Baldwin 	align = 1ul << RF_ALIGNMENT(flags);
1430c825d4dcSJohn Baldwin 	if (start < w->base) {
143183c41143SJohn Baldwin 		if (rman_first_free_region(&w->rman, &start_free, &end_free) !=
1432c825d4dcSJohn Baldwin 		    0 || start_free != w->base)
1433c825d4dcSJohn Baldwin 			end_free = w->base;
143483c41143SJohn Baldwin 		if (end_free > end)
1435ddac8cc9SJohn Baldwin 			end_free = end + 1;
143683c41143SJohn Baldwin 
143783c41143SJohn Baldwin 		/* Move end_free down until it is properly aligned. */
143883c41143SJohn Baldwin 		end_free &= ~(align - 1);
1439a49dcb46SJohn Baldwin 		end_free--;
1440a49dcb46SJohn Baldwin 		front = end_free - (count - 1);
144183c41143SJohn Baldwin 
144283c41143SJohn Baldwin 		/*
144383c41143SJohn Baldwin 		 * The resource would now be allocated at (front,
144483c41143SJohn Baldwin 		 * end_free).  Ensure that fits in the (start, end)
144583c41143SJohn Baldwin 		 * bounds.  end_free is checked above.  If 'front' is
144683c41143SJohn Baldwin 		 * ok, ensure it is properly aligned for this window.
144783c41143SJohn Baldwin 		 * Also check for underflow.
144883c41143SJohn Baldwin 		 */
144983c41143SJohn Baldwin 		if (front >= start && front <= end_free) {
145083c41143SJohn Baldwin 			if (bootverbose)
145183c41143SJohn Baldwin 				printf("\tfront candidate range: %#lx-%#lx\n",
145283c41143SJohn Baldwin 				    front, end_free);
1453a7b5acacSJohn Baldwin 			front &= ~wmask;
1454c825d4dcSJohn Baldwin 			front = w->base - front;
145583c41143SJohn Baldwin 		} else
145683c41143SJohn Baldwin 			front = 0;
145783c41143SJohn Baldwin 	} else
145883c41143SJohn Baldwin 		front = 0;
1459c825d4dcSJohn Baldwin 	if (end > w->limit) {
146083c41143SJohn Baldwin 		if (rman_last_free_region(&w->rman, &start_free, &end_free) !=
1461c825d4dcSJohn Baldwin 		    0 || end_free != w->limit)
1462c825d4dcSJohn Baldwin 			start_free = w->limit + 1;
146383c41143SJohn Baldwin 		if (start_free < start)
146483c41143SJohn Baldwin 			start_free = start;
146583c41143SJohn Baldwin 
146683c41143SJohn Baldwin 		/* Move start_free up until it is properly aligned. */
146783c41143SJohn Baldwin 		start_free = roundup2(start_free, align);
1468a49dcb46SJohn Baldwin 		back = start_free + count - 1;
146983c41143SJohn Baldwin 
147083c41143SJohn Baldwin 		/*
147183c41143SJohn Baldwin 		 * The resource would now be allocated at (start_free,
147283c41143SJohn Baldwin 		 * back).  Ensure that fits in the (start, end)
147383c41143SJohn Baldwin 		 * bounds.  start_free is checked above.  If 'back' is
147483c41143SJohn Baldwin 		 * ok, ensure it is properly aligned for this window.
147583c41143SJohn Baldwin 		 * Also check for overflow.
147683c41143SJohn Baldwin 		 */
147783c41143SJohn Baldwin 		if (back <= end && start_free <= back) {
147883c41143SJohn Baldwin 			if (bootverbose)
147983c41143SJohn Baldwin 				printf("\tback candidate range: %#lx-%#lx\n",
148083c41143SJohn Baldwin 				    start_free, back);
1481a7b5acacSJohn Baldwin 			back |= wmask;
1482c825d4dcSJohn Baldwin 			back -= w->limit;
148383c41143SJohn Baldwin 		} else
148483c41143SJohn Baldwin 			back = 0;
148583c41143SJohn Baldwin 	} else
148683c41143SJohn Baldwin 		back = 0;
148783c41143SJohn Baldwin 
148883c41143SJohn Baldwin 	/*
148983c41143SJohn Baldwin 	 * Try to allocate the smallest needed region first.
149083c41143SJohn Baldwin 	 * If that fails, fall back to the other region.
149183c41143SJohn Baldwin 	 */
149283c41143SJohn Baldwin 	error = ENOSPC;
149383c41143SJohn Baldwin 	while (front != 0 || back != 0) {
149483c41143SJohn Baldwin 		if (front != 0 && (front <= back || back == 0)) {
1495c825d4dcSJohn Baldwin 			error = pcib_expand_window(sc, w, type, w->base - front,
1496c825d4dcSJohn Baldwin 			    w->limit);
149783c41143SJohn Baldwin 			if (error == 0)
149883c41143SJohn Baldwin 				break;
149983c41143SJohn Baldwin 			front = 0;
150083c41143SJohn Baldwin 		} else {
1501c825d4dcSJohn Baldwin 			error = pcib_expand_window(sc, w, type, w->base,
1502c825d4dcSJohn Baldwin 			    w->limit + back);
150383c41143SJohn Baldwin 			if (error == 0)
150483c41143SJohn Baldwin 				break;
150583c41143SJohn Baldwin 			back = 0;
150683c41143SJohn Baldwin 		}
150783c41143SJohn Baldwin 	}
150883c41143SJohn Baldwin 
150983c41143SJohn Baldwin 	if (error)
151083c41143SJohn Baldwin 		return (error);
151183c41143SJohn Baldwin 	if (bootverbose)
1512c825d4dcSJohn Baldwin 		device_printf(sc->dev, "grew %s window to %#jx-%#jx\n",
1513c825d4dcSJohn Baldwin 		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
151483c41143SJohn Baldwin 
151583c41143SJohn Baldwin updatewin:
1516c825d4dcSJohn Baldwin 	/* Write the new window. */
1517a7b5acacSJohn Baldwin 	KASSERT((w->base & wmask) == 0, ("start address is not aligned"));
1518a7b5acacSJohn Baldwin 	KASSERT((w->limit & wmask) == wmask, ("end address is not aligned"));
151983c41143SJohn Baldwin 	pcib_write_windows(sc, w->mask);
152083c41143SJohn Baldwin 	return (0);
152183c41143SJohn Baldwin }
152283c41143SJohn Baldwin 
152383c41143SJohn Baldwin /*
152483c41143SJohn Baldwin  * We have to trap resource allocation requests and ensure that the bridge
152583c41143SJohn Baldwin  * is set up to, or capable of handling them.
152683c41143SJohn Baldwin  */
152783c41143SJohn Baldwin struct resource *
152883c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
152983c41143SJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
153083c41143SJohn Baldwin {
153183c41143SJohn Baldwin 	struct pcib_softc *sc;
153283c41143SJohn Baldwin 	struct resource *r;
153383c41143SJohn Baldwin 
153483c41143SJohn Baldwin 	sc = device_get_softc(dev);
153583c41143SJohn Baldwin 
153683c41143SJohn Baldwin 	/*
153783c41143SJohn Baldwin 	 * VGA resources are decoded iff the VGA enable bit is set in
153883c41143SJohn Baldwin 	 * the bridge control register.  VGA resources do not fall into
153983c41143SJohn Baldwin 	 * the resource windows and are passed up to the parent.
154083c41143SJohn Baldwin 	 */
154183c41143SJohn Baldwin 	if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) ||
154283c41143SJohn Baldwin 	    (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) {
154383c41143SJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE)
154483c41143SJohn Baldwin 			return (bus_generic_alloc_resource(dev, child, type,
154583c41143SJohn Baldwin 			    rid, start, end, count, flags));
154683c41143SJohn Baldwin 		else
154783c41143SJohn Baldwin 			return (NULL);
154883c41143SJohn Baldwin 	}
154983c41143SJohn Baldwin 
155083c41143SJohn Baldwin 	switch (type) {
1551*4edef187SJohn Baldwin #ifdef PCI_RES_BUS
1552*4edef187SJohn Baldwin 	case PCI_RES_BUS:
1553*4edef187SJohn Baldwin 		return (pcib_alloc_subbus(&sc->bus, child, rid, start, end,
1554*4edef187SJohn Baldwin 		    count, flags));
1555*4edef187SJohn Baldwin #endif
155683c41143SJohn Baldwin 	case SYS_RES_IOPORT:
1557c825d4dcSJohn Baldwin 		if (pcib_is_isa_range(sc, start, end, count))
1558c825d4dcSJohn Baldwin 			return (NULL);
155983c41143SJohn Baldwin 		r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start,
156083c41143SJohn Baldwin 		    end, count, flags);
1561a6c82265SMarius Strobl 		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
156283c41143SJohn Baldwin 			break;
156383c41143SJohn Baldwin 		if (pcib_grow_window(sc, &sc->io, type, start, end, count,
156483c41143SJohn Baldwin 		    flags) == 0)
156583c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->io, child, type,
156683c41143SJohn Baldwin 			    rid, start, end, count, flags);
156783c41143SJohn Baldwin 		break;
156883c41143SJohn Baldwin 	case SYS_RES_MEMORY:
156983c41143SJohn Baldwin 		/*
157083c41143SJohn Baldwin 		 * For prefetchable resources, prefer the prefetchable
157183c41143SJohn Baldwin 		 * memory window, but fall back to the regular memory
157283c41143SJohn Baldwin 		 * window if that fails.  Try both windows before
157383c41143SJohn Baldwin 		 * attempting to grow a window in case the firmware
157483c41143SJohn Baldwin 		 * has used a range in the regular memory window to
157583c41143SJohn Baldwin 		 * map a prefetchable BAR.
157683c41143SJohn Baldwin 		 */
157783c41143SJohn Baldwin 		if (flags & RF_PREFETCHABLE) {
157883c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->pmem, child, type,
157983c41143SJohn Baldwin 			    rid, start, end, count, flags);
158083c41143SJohn Baldwin 			if (r != NULL)
158183c41143SJohn Baldwin 				break;
158283c41143SJohn Baldwin 		}
158383c41143SJohn Baldwin 		r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid,
158483c41143SJohn Baldwin 		    start, end, count, flags);
1585a6c82265SMarius Strobl 		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
158683c41143SJohn Baldwin 			break;
158783c41143SJohn Baldwin 		if (flags & RF_PREFETCHABLE) {
158883c41143SJohn Baldwin 			if (pcib_grow_window(sc, &sc->pmem, type, start, end,
158983c41143SJohn Baldwin 			    count, flags) == 0) {
159083c41143SJohn Baldwin 				r = pcib_suballoc_resource(sc, &sc->pmem, child,
159183c41143SJohn Baldwin 				    type, rid, start, end, count, flags);
159283c41143SJohn Baldwin 				if (r != NULL)
159383c41143SJohn Baldwin 					break;
159483c41143SJohn Baldwin 			}
159583c41143SJohn Baldwin 		}
159683c41143SJohn Baldwin 		if (pcib_grow_window(sc, &sc->mem, type, start, end, count,
159783c41143SJohn Baldwin 		    flags & ~RF_PREFETCHABLE) == 0)
159883c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->mem, child, type,
159983c41143SJohn Baldwin 			    rid, start, end, count, flags);
160083c41143SJohn Baldwin 		break;
160183c41143SJohn Baldwin 	default:
160283c41143SJohn Baldwin 		return (bus_generic_alloc_resource(dev, child, type, rid,
160383c41143SJohn Baldwin 		    start, end, count, flags));
160483c41143SJohn Baldwin 	}
160583c41143SJohn Baldwin 
160683c41143SJohn Baldwin 	/*
160783c41143SJohn Baldwin 	 * If attempts to suballocate from the window fail but this is a
160883c41143SJohn Baldwin 	 * subtractive bridge, pass the request up the tree.
160983c41143SJohn Baldwin 	 */
161083c41143SJohn Baldwin 	if (sc->flags & PCIB_SUBTRACTIVE && r == NULL)
161183c41143SJohn Baldwin 		return (bus_generic_alloc_resource(dev, child, type, rid,
161283c41143SJohn Baldwin 		    start, end, count, flags));
161383c41143SJohn Baldwin 	return (r);
161483c41143SJohn Baldwin }
161583c41143SJohn Baldwin 
161683c41143SJohn Baldwin int
161783c41143SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
161883c41143SJohn Baldwin     u_long start, u_long end)
161983c41143SJohn Baldwin {
162083c41143SJohn Baldwin 	struct pcib_softc *sc;
162183c41143SJohn Baldwin 
162283c41143SJohn Baldwin 	sc = device_get_softc(bus);
162383c41143SJohn Baldwin 	if (pcib_is_resource_managed(sc, type, r))
162483c41143SJohn Baldwin 		return (rman_adjust_resource(r, start, end));
162583c41143SJohn Baldwin 	return (bus_generic_adjust_resource(bus, child, type, r, start, end));
162683c41143SJohn Baldwin }
162783c41143SJohn Baldwin 
162883c41143SJohn Baldwin int
162983c41143SJohn Baldwin pcib_release_resource(device_t dev, device_t child, int type, int rid,
163083c41143SJohn Baldwin     struct resource *r)
163183c41143SJohn Baldwin {
163283c41143SJohn Baldwin 	struct pcib_softc *sc;
163383c41143SJohn Baldwin 	int error;
163483c41143SJohn Baldwin 
163583c41143SJohn Baldwin 	sc = device_get_softc(dev);
163683c41143SJohn Baldwin 	if (pcib_is_resource_managed(sc, type, r)) {
163783c41143SJohn Baldwin 		if (rman_get_flags(r) & RF_ACTIVE) {
163883c41143SJohn Baldwin 			error = bus_deactivate_resource(child, type, rid, r);
163983c41143SJohn Baldwin 			if (error)
164083c41143SJohn Baldwin 				return (error);
164183c41143SJohn Baldwin 		}
164283c41143SJohn Baldwin 		return (rman_release_resource(r));
164383c41143SJohn Baldwin 	}
164483c41143SJohn Baldwin 	return (bus_generic_release_resource(dev, child, type, rid, r));
164583c41143SJohn Baldwin }
164683c41143SJohn Baldwin #else
1647bb0d0a8eSMike Smith /*
1648bb0d0a8eSMike Smith  * We have to trap resource allocation requests and ensure that the bridge
1649bb0d0a8eSMike Smith  * is set up to, or capable of handling them.
1650bb0d0a8eSMike Smith  */
16516f0d5884SJohn Baldwin struct resource *
1652bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
1653bb0d0a8eSMike Smith     u_long start, u_long end, u_long count, u_int flags)
1654bb0d0a8eSMike Smith {
1655bb0d0a8eSMike Smith 	struct pcib_softc	*sc = device_get_softc(dev);
165626043836SJohn Baldwin 	const char *name, *suffix;
1657a8b354a8SWarner Losh 	int ok;
1658bb0d0a8eSMike Smith 
1659bb0d0a8eSMike Smith 	/*
1660bb0d0a8eSMike Smith 	 * Fail the allocation for this range if it's not supported.
1661bb0d0a8eSMike Smith 	 */
166226043836SJohn Baldwin 	name = device_get_nameunit(child);
166326043836SJohn Baldwin 	if (name == NULL) {
166426043836SJohn Baldwin 		name = "";
166526043836SJohn Baldwin 		suffix = "";
166626043836SJohn Baldwin 	} else
166726043836SJohn Baldwin 		suffix = " ";
1668bb0d0a8eSMike Smith 	switch (type) {
1669bb0d0a8eSMike Smith 	case SYS_RES_IOPORT:
1670a8b354a8SWarner Losh 		ok = 0;
1671e4b59fc5SWarner Losh 		if (!pcib_is_io_open(sc))
1672e4b59fc5SWarner Losh 			break;
1673a8b354a8SWarner Losh 		ok = (start >= sc->iobase && end <= sc->iolimit);
1674d98d9b12SMarcel Moolenaar 
1675d98d9b12SMarcel Moolenaar 		/*
1676d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA I/O addresses when the
1677d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
1678d98d9b12SMarcel Moolenaar 		 */
1679d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_ioport_range(start, end))
1680d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1681d98d9b12SMarcel Moolenaar 
1682e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1683a8b354a8SWarner Losh 			if (!ok) {
168412b8c86eSWarner Losh 				if (start < sc->iobase)
168512b8c86eSWarner Losh 					start = sc->iobase;
168612b8c86eSWarner Losh 				if (end > sc->iolimit)
168712b8c86eSWarner Losh 					end = sc->iolimit;
16882daa7a07SWarner Losh 				if (start < end)
16892daa7a07SWarner Losh 					ok = 1;
1690a8b354a8SWarner Losh 			}
16911c54ff33SMatthew N. Dodd 		} else {
1692e4b59fc5SWarner Losh 			ok = 1;
16939dffe835SWarner Losh #if 0
1694795dceffSWarner Losh 			/*
1695795dceffSWarner Losh 			 * If we overlap with the subtractive range, then
1696795dceffSWarner Losh 			 * pick the upper range to use.
1697795dceffSWarner Losh 			 */
1698795dceffSWarner Losh 			if (start < sc->iolimit && end > sc->iobase)
1699795dceffSWarner Losh 				start = sc->iolimit + 1;
17009dffe835SWarner Losh #endif
170112b8c86eSWarner Losh 		}
1702a8b354a8SWarner Losh 		if (end < start) {
17032daa7a07SWarner Losh 			device_printf(dev, "ioport: end (%lx) < start (%lx)\n",
17042daa7a07SWarner Losh 			    end, start);
1705a8b354a8SWarner Losh 			start = 0;
1706a8b354a8SWarner Losh 			end = 0;
1707a8b354a8SWarner Losh 			ok = 0;
1708a8b354a8SWarner Losh 		}
1709a8b354a8SWarner Losh 		if (!ok) {
171026043836SJohn Baldwin 			device_printf(dev, "%s%srequested unsupported I/O "
1711a8b354a8SWarner Losh 			    "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
171226043836SJohn Baldwin 			    name, suffix, start, end, sc->iobase, sc->iolimit);
1713bb0d0a8eSMike Smith 			return (NULL);
1714bb0d0a8eSMike Smith 		}
17154fa59183SMike Smith 		if (bootverbose)
17162daa7a07SWarner Losh 			device_printf(dev,
171726043836SJohn Baldwin 			    "%s%srequested I/O range 0x%lx-0x%lx: in range\n",
171826043836SJohn Baldwin 			    name, suffix, start, end);
1719bb0d0a8eSMike Smith 		break;
1720bb0d0a8eSMike Smith 
1721bb0d0a8eSMike Smith 	case SYS_RES_MEMORY:
1722a8b354a8SWarner Losh 		ok = 0;
1723a8b354a8SWarner Losh 		if (pcib_is_nonprefetch_open(sc))
1724a8b354a8SWarner Losh 			ok = ok || (start >= sc->membase && end <= sc->memlimit);
1725a8b354a8SWarner Losh 		if (pcib_is_prefetch_open(sc))
1726a8b354a8SWarner Losh 			ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
1727d98d9b12SMarcel Moolenaar 
1728d98d9b12SMarcel Moolenaar 		/*
1729d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA memory addresses when the
1730d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
1731d98d9b12SMarcel Moolenaar 		 */
1732d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_memory_range(start, end))
1733d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1734d98d9b12SMarcel Moolenaar 
1735e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1736a8b354a8SWarner Losh 			if (!ok) {
1737a8b354a8SWarner Losh 				ok = 1;
1738a8b354a8SWarner Losh 				if (flags & RF_PREFETCHABLE) {
1739a8b354a8SWarner Losh 					if (pcib_is_prefetch_open(sc)) {
1740a8b354a8SWarner Losh 						if (start < sc->pmembase)
1741a8b354a8SWarner Losh 							start = sc->pmembase;
1742a8b354a8SWarner Losh 						if (end > sc->pmemlimit)
1743a8b354a8SWarner Losh 							end = sc->pmemlimit;
1744a8b354a8SWarner Losh 					} else {
1745a8b354a8SWarner Losh 						ok = 0;
1746a8b354a8SWarner Losh 					}
1747a8b354a8SWarner Losh 				} else {	/* non-prefetchable */
1748a8b354a8SWarner Losh 					if (pcib_is_nonprefetch_open(sc)) {
1749a8b354a8SWarner Losh 						if (start < sc->membase)
175012b8c86eSWarner Losh 							start = sc->membase;
175112b8c86eSWarner Losh 						if (end > sc->memlimit)
175212b8c86eSWarner Losh 							end = sc->memlimit;
17531c54ff33SMatthew N. Dodd 					} else {
1754a8b354a8SWarner Losh 						ok = 0;
1755a8b354a8SWarner Losh 					}
1756a8b354a8SWarner Losh 				}
1757a8b354a8SWarner Losh 			}
1758a8b354a8SWarner Losh 		} else if (!ok) {
1759e4b59fc5SWarner Losh 			ok = 1;	/* subtractive bridge: always ok */
17609dffe835SWarner Losh #if 0
1761a8b354a8SWarner Losh 			if (pcib_is_nonprefetch_open(sc)) {
1762795dceffSWarner Losh 				if (start < sc->memlimit && end > sc->membase)
1763795dceffSWarner Losh 					start = sc->memlimit + 1;
1764a8b354a8SWarner Losh 			}
1765a8b354a8SWarner Losh 			if (pcib_is_prefetch_open(sc)) {
1766795dceffSWarner Losh 				if (start < sc->pmemlimit && end > sc->pmembase)
1767795dceffSWarner Losh 					start = sc->pmemlimit + 1;
17681c54ff33SMatthew N. Dodd 			}
17699dffe835SWarner Losh #endif
177012b8c86eSWarner Losh 		}
1771a8b354a8SWarner Losh 		if (end < start) {
17722daa7a07SWarner Losh 			device_printf(dev, "memory: end (%lx) < start (%lx)\n",
17732daa7a07SWarner Losh 			    end, start);
1774a8b354a8SWarner Losh 			start = 0;
1775a8b354a8SWarner Losh 			end = 0;
1776a8b354a8SWarner Losh 			ok = 0;
1777a8b354a8SWarner Losh 		}
1778a8b354a8SWarner Losh 		if (!ok && bootverbose)
177934428485SWarner Losh 			device_printf(dev,
178026043836SJohn Baldwin 			    "%s%srequested unsupported memory range %#lx-%#lx "
1781b0a2d4b8SWarner Losh 			    "(decoding %#jx-%#jx, %#jx-%#jx)\n",
178226043836SJohn Baldwin 			    name, suffix, start, end,
1783b0a2d4b8SWarner Losh 			    (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
1784b0a2d4b8SWarner Losh 			    (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
1785a8b354a8SWarner Losh 		if (!ok)
1786bb0d0a8eSMike Smith 			return (NULL);
17874fa59183SMike Smith 		if (bootverbose)
178826043836SJohn Baldwin 			device_printf(dev,"%s%srequested memory range "
17892daa7a07SWarner Losh 			    "0x%lx-0x%lx: good\n",
179026043836SJohn Baldwin 			    name, suffix, start, end);
17914fa59183SMike Smith 		break;
17924fa59183SMike Smith 
1793bb0d0a8eSMike Smith 	default:
17944fa59183SMike Smith 		break;
1795bb0d0a8eSMike Smith 	}
1796bb0d0a8eSMike Smith 	/*
1797bb0d0a8eSMike Smith 	 * Bridge is OK decoding this resource, so pass it up.
1798bb0d0a8eSMike Smith 	 */
17992daa7a07SWarner Losh 	return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
18002daa7a07SWarner Losh 	    count, flags));
1801bb0d0a8eSMike Smith }
180283c41143SJohn Baldwin #endif
1803bb0d0a8eSMike Smith 
1804bb0d0a8eSMike Smith /*
1805bb0d0a8eSMike Smith  * PCIB interface.
1806bb0d0a8eSMike Smith  */
18076f0d5884SJohn Baldwin int
1808bb0d0a8eSMike Smith pcib_maxslots(device_t dev)
1809bb0d0a8eSMike Smith {
18104fa59183SMike Smith     return(PCI_SLOTMAX);
1811bb0d0a8eSMike Smith }
1812bb0d0a8eSMike Smith 
1813bb0d0a8eSMike Smith /*
1814bb0d0a8eSMike Smith  * Since we are a child of a PCI bus, its parent must support the pcib interface.
1815bb0d0a8eSMike Smith  */
1816b0cb115fSWarner Losh uint32_t
1817795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
1818bb0d0a8eSMike Smith {
1819bb0d0a8eSMike Smith     return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width));
1820bb0d0a8eSMike Smith }
1821bb0d0a8eSMike Smith 
18226f0d5884SJohn Baldwin void
1823795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
1824bb0d0a8eSMike Smith {
1825bb0d0a8eSMike Smith     PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width);
1826bb0d0a8eSMike Smith }
1827bb0d0a8eSMike Smith 
1828bb0d0a8eSMike Smith /*
1829bb0d0a8eSMike Smith  * Route an interrupt across a PCI bridge.
1830bb0d0a8eSMike Smith  */
18312c2d1d07SBenno Rice int
1832bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin)
1833bb0d0a8eSMike Smith {
1834bb0d0a8eSMike Smith     device_t	bus;
1835bb0d0a8eSMike Smith     int		parent_intpin;
1836bb0d0a8eSMike Smith     int		intnum;
1837bb0d0a8eSMike Smith 
1838bb0d0a8eSMike Smith     /*
1839bb0d0a8eSMike Smith      *
1840bb0d0a8eSMike Smith      * The PCI standard defines a swizzle of the child-side device/intpin to
1841bb0d0a8eSMike Smith      * the parent-side intpin as follows.
1842bb0d0a8eSMike Smith      *
1843bb0d0a8eSMike Smith      * device = device on child bus
1844bb0d0a8eSMike Smith      * child_intpin = intpin on child bus slot (0-3)
1845bb0d0a8eSMike Smith      * parent_intpin = intpin on parent bus slot (0-3)
1846bb0d0a8eSMike Smith      *
1847bb0d0a8eSMike Smith      * parent_intpin = (device + child_intpin) % 4
1848bb0d0a8eSMike Smith      */
1849cdc95e1bSBernd Walter     parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
1850bb0d0a8eSMike Smith 
1851bb0d0a8eSMike Smith     /*
1852bb0d0a8eSMike Smith      * Our parent is a PCI bus.  Its parent must export the pcib interface
1853bb0d0a8eSMike Smith      * which includes the ability to route interrupts.
1854bb0d0a8eSMike Smith      */
1855bb0d0a8eSMike Smith     bus = device_get_parent(pcib);
1856bb0d0a8eSMike Smith     intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
185739981fedSJohn Baldwin     if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
1858c6a121abSJohn Baldwin 	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
1859c6a121abSJohn Baldwin 	    pci_get_slot(dev), 'A' + pin - 1, intnum);
18608046c4b9SMike Smith     }
1861bb0d0a8eSMike Smith     return(intnum);
1862bb0d0a8eSMike Smith }
1863b173edafSJohn Baldwin 
1864e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
18659bf4c9c1SJohn Baldwin int
18669bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
18679bf4c9c1SJohn Baldwin {
1868bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
18699bf4c9c1SJohn Baldwin 	device_t bus;
18709bf4c9c1SJohn Baldwin 
187122bf1c7fSJohn Baldwin 	if (sc->flags & PCIB_DISABLE_MSI)
187222bf1c7fSJohn Baldwin 		return (ENXIO);
18739bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
18749bf4c9c1SJohn Baldwin 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
18759bf4c9c1SJohn Baldwin 	    irqs));
18769bf4c9c1SJohn Baldwin }
18779bf4c9c1SJohn Baldwin 
1878e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */
18799bf4c9c1SJohn Baldwin int
18809bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
18819bf4c9c1SJohn Baldwin {
18829bf4c9c1SJohn Baldwin 	device_t bus;
18839bf4c9c1SJohn Baldwin 
18849bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
18859bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
18869bf4c9c1SJohn Baldwin }
18879bf4c9c1SJohn Baldwin 
18889bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */
18899bf4c9c1SJohn Baldwin int
1890e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
18919bf4c9c1SJohn Baldwin {
1892bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
18939bf4c9c1SJohn Baldwin 	device_t bus;
18949bf4c9c1SJohn Baldwin 
189568e9cbd3SMarius Strobl 	if (sc->flags & PCIB_DISABLE_MSIX)
189622bf1c7fSJohn Baldwin 		return (ENXIO);
18979bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
1898e706f7f0SJohn Baldwin 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
18995fe82bcaSJohn Baldwin }
19005fe82bcaSJohn Baldwin 
19019bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */
19029bf4c9c1SJohn Baldwin int
19039bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq)
19049bf4c9c1SJohn Baldwin {
19059bf4c9c1SJohn Baldwin 	device_t bus;
19069bf4c9c1SJohn Baldwin 
19079bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
19089bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
19099bf4c9c1SJohn Baldwin }
19109bf4c9c1SJohn Baldwin 
1911e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */
1912e706f7f0SJohn Baldwin int
1913e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
1914e706f7f0SJohn Baldwin     uint32_t *data)
1915e706f7f0SJohn Baldwin {
1916e706f7f0SJohn Baldwin 	device_t bus;
19174522ac77SLuoqi Chen 	int error;
1918e706f7f0SJohn Baldwin 
1919e706f7f0SJohn Baldwin 	bus = device_get_parent(pcib);
19204522ac77SLuoqi Chen 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
19214522ac77SLuoqi Chen 	if (error)
19224522ac77SLuoqi Chen 		return (error);
19234522ac77SLuoqi Chen 
19244522ac77SLuoqi Chen 	pci_ht_map_msi(pcib, *addr);
19254522ac77SLuoqi Chen 	return (0);
1926e706f7f0SJohn Baldwin }
1927e706f7f0SJohn Baldwin 
192862508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */
192962508c53SJohn Baldwin int
193062508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
193162508c53SJohn Baldwin {
193262508c53SJohn Baldwin 	device_t bus;
193362508c53SJohn Baldwin 
193462508c53SJohn Baldwin 	bus = device_get_parent(pcib);
193562508c53SJohn Baldwin 	return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate));
193662508c53SJohn Baldwin }
1937