xref: /freebsd/sys/dev/pci/pci_pci.c (revision c825d4dc50733e783ae8af5d4335210ca8b9f173)
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
10683c41143SJohn Baldwin 
10783c41143SJohn Baldwin /*
10883c41143SJohn Baldwin  * Is a resource from a child device sub-allocated from one of our
10983c41143SJohn Baldwin  * resource managers?
11083c41143SJohn Baldwin  */
11183c41143SJohn Baldwin static int
11283c41143SJohn Baldwin pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r)
11383c41143SJohn Baldwin {
11483c41143SJohn Baldwin 
11583c41143SJohn Baldwin 	switch (type) {
11683c41143SJohn Baldwin 	case SYS_RES_IOPORT:
11783c41143SJohn Baldwin 		return (rman_is_region_manager(r, &sc->io.rman));
11883c41143SJohn Baldwin 	case SYS_RES_MEMORY:
11983c41143SJohn Baldwin 		/* Prefetchable resources may live in either memory rman. */
12083c41143SJohn Baldwin 		if (rman_get_flags(r) & RF_PREFETCHABLE &&
12183c41143SJohn Baldwin 		    rman_is_region_manager(r, &sc->pmem.rman))
12283c41143SJohn Baldwin 			return (1);
12383c41143SJohn Baldwin 		return (rman_is_region_manager(r, &sc->mem.rman));
12483c41143SJohn Baldwin 	}
12583c41143SJohn Baldwin 	return (0);
12683c41143SJohn Baldwin }
12783c41143SJohn Baldwin 
12883c41143SJohn Baldwin static int
12983c41143SJohn Baldwin pcib_is_window_open(struct pcib_window *pw)
13083c41143SJohn Baldwin {
13183c41143SJohn Baldwin 
13283c41143SJohn Baldwin 	return (pw->valid && pw->base < pw->limit);
13383c41143SJohn Baldwin }
13483c41143SJohn Baldwin 
13583c41143SJohn Baldwin /*
13683c41143SJohn Baldwin  * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and
13783c41143SJohn Baldwin  * handle for the resource, we could pass RF_ACTIVE up to the PCI bus
13883c41143SJohn Baldwin  * when allocating the resource windows and rely on the PCI bus driver
13983c41143SJohn Baldwin  * to do this for us.
14083c41143SJohn Baldwin  */
14183c41143SJohn Baldwin static void
14283c41143SJohn Baldwin pcib_activate_window(struct pcib_softc *sc, int type)
14383c41143SJohn Baldwin {
14483c41143SJohn Baldwin 
14583c41143SJohn Baldwin 	PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type);
14683c41143SJohn Baldwin }
14783c41143SJohn Baldwin 
14883c41143SJohn Baldwin static void
14983c41143SJohn Baldwin pcib_write_windows(struct pcib_softc *sc, int mask)
15083c41143SJohn Baldwin {
15183c41143SJohn Baldwin 	device_t dev;
15283c41143SJohn Baldwin 	uint32_t val;
15383c41143SJohn Baldwin 
15483c41143SJohn Baldwin 	dev = sc->dev;
15583c41143SJohn Baldwin 	if (sc->io.valid && mask & WIN_IO) {
15683c41143SJohn Baldwin 		val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
15783c41143SJohn Baldwin 		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
15883c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOBASEH_1,
15983c41143SJohn Baldwin 			    sc->io.base >> 16, 2);
16083c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOLIMITH_1,
16183c41143SJohn Baldwin 			    sc->io.limit >> 16, 2);
16283c41143SJohn Baldwin 		}
16383c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1);
16483c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1);
16583c41143SJohn Baldwin 	}
16683c41143SJohn Baldwin 
16783c41143SJohn Baldwin 	if (mask & WIN_MEM) {
16883c41143SJohn Baldwin 		pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2);
16983c41143SJohn Baldwin 		pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2);
17083c41143SJohn Baldwin 	}
17183c41143SJohn Baldwin 
17283c41143SJohn Baldwin 	if (sc->pmem.valid && mask & WIN_PMEM) {
17383c41143SJohn Baldwin 		val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
17483c41143SJohn Baldwin 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
17583c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMBASEH_1,
17683c41143SJohn Baldwin 			    sc->pmem.base >> 32, 4);
17783c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMLIMITH_1,
17883c41143SJohn Baldwin 			    sc->pmem.limit >> 32, 4);
17983c41143SJohn Baldwin 		}
18083c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2);
18183c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2);
18283c41143SJohn Baldwin 	}
18383c41143SJohn Baldwin }
18483c41143SJohn Baldwin 
185*c825d4dcSJohn Baldwin /*
186*c825d4dcSJohn Baldwin  * This is used to reject I/O port allocations that conflict with an
187*c825d4dcSJohn Baldwin  * ISA alias range.
188*c825d4dcSJohn Baldwin  */
189*c825d4dcSJohn Baldwin static int
190*c825d4dcSJohn Baldwin pcib_is_isa_range(struct pcib_softc *sc, u_long start, u_long end, u_long count)
191*c825d4dcSJohn Baldwin {
192*c825d4dcSJohn Baldwin 	u_long next_alias;
193*c825d4dcSJohn Baldwin 
194*c825d4dcSJohn Baldwin 	if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE))
195*c825d4dcSJohn Baldwin 		return (0);
196*c825d4dcSJohn Baldwin 
197*c825d4dcSJohn Baldwin 	/* Only check fixed ranges for overlap. */
198*c825d4dcSJohn Baldwin 	if (start + count - 1 != end)
199*c825d4dcSJohn Baldwin 		return (0);
200*c825d4dcSJohn Baldwin 
201*c825d4dcSJohn Baldwin 	/* ISA aliases are only in the lower 64KB of I/O space. */
202*c825d4dcSJohn Baldwin 	if (start >= 65536)
203*c825d4dcSJohn Baldwin 		return (0);
204*c825d4dcSJohn Baldwin 
205*c825d4dcSJohn Baldwin 	/* Check for overlap with 0x000 - 0x0ff as a special case. */
206*c825d4dcSJohn Baldwin 	if (start < 0x100)
207*c825d4dcSJohn Baldwin 		goto alias;
208*c825d4dcSJohn Baldwin 
209*c825d4dcSJohn Baldwin 	/*
210*c825d4dcSJohn Baldwin 	 * If the start address is an alias, the range is an alias.
211*c825d4dcSJohn Baldwin 	 * Otherwise, compute the start of the next alias range and
212*c825d4dcSJohn Baldwin 	 * check if it is before the end of the candidate range.
213*c825d4dcSJohn Baldwin 	 */
214*c825d4dcSJohn Baldwin 	if ((start & 0x300) != 0)
215*c825d4dcSJohn Baldwin 		goto alias;
216*c825d4dcSJohn Baldwin 	next_alias = (start & ~0x3fful) | 0x100;
217*c825d4dcSJohn Baldwin 	if (next_alias <= end)
218*c825d4dcSJohn Baldwin 		goto alias;
219*c825d4dcSJohn Baldwin 	return (0);
220*c825d4dcSJohn Baldwin 
221*c825d4dcSJohn Baldwin alias:
222*c825d4dcSJohn Baldwin 	if (bootverbose)
223*c825d4dcSJohn Baldwin 		device_printf(sc->dev,
224*c825d4dcSJohn Baldwin 		    "I/O range %#lx-%#lx overlaps with an ISA alias\n", start,
225*c825d4dcSJohn Baldwin 		    end);
226*c825d4dcSJohn Baldwin 	return (1);
227*c825d4dcSJohn Baldwin }
228*c825d4dcSJohn Baldwin 
229*c825d4dcSJohn Baldwin static void
230*c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res,
231*c825d4dcSJohn Baldwin     int count)
232*c825d4dcSJohn Baldwin {
233*c825d4dcSJohn Baldwin 	struct resource **newarray;
234*c825d4dcSJohn Baldwin 	int error, i;
235*c825d4dcSJohn Baldwin 
236*c825d4dcSJohn Baldwin 	newarray = malloc(sizeof(struct resource *) * (w->count + count),
237*c825d4dcSJohn Baldwin 	    M_DEVBUF, M_WAITOK);
238*c825d4dcSJohn Baldwin 	if (w->res != NULL)
239*c825d4dcSJohn Baldwin 		bcopy(w->res, newarray, sizeof(struct resource *) * w->count);
240*c825d4dcSJohn Baldwin 	bcopy(res, newarray + w->count, sizeof(struct resource *) * count);
241*c825d4dcSJohn Baldwin 	free(w->res, M_DEVBUF);
242*c825d4dcSJohn Baldwin 	w->res = newarray;
243*c825d4dcSJohn Baldwin 	w->count += count;
244*c825d4dcSJohn Baldwin 
245*c825d4dcSJohn Baldwin 	for (i = 0; i < count; i++) {
246*c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, rman_get_start(res[i]),
247*c825d4dcSJohn Baldwin 		    rman_get_end(res[i]));
248*c825d4dcSJohn Baldwin 		if (error)
249*c825d4dcSJohn Baldwin 			panic("Failed to add resource to rman");
250*c825d4dcSJohn Baldwin 	}
251*c825d4dcSJohn Baldwin }
252*c825d4dcSJohn Baldwin 
253*c825d4dcSJohn Baldwin typedef void (nonisa_callback)(u_long start, u_long end, void *arg);
254*c825d4dcSJohn Baldwin 
255*c825d4dcSJohn Baldwin static void
256*c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(u_long start, u_long end, nonisa_callback *cb,
257*c825d4dcSJohn Baldwin     void *arg)
258*c825d4dcSJohn Baldwin {
259*c825d4dcSJohn Baldwin 	u_long next_end;
260*c825d4dcSJohn Baldwin 
261*c825d4dcSJohn Baldwin 	/*
262*c825d4dcSJohn Baldwin 	 * If start is within an ISA alias range, move up to the start
263*c825d4dcSJohn Baldwin 	 * of the next non-alias range.  As a special case, addresses
264*c825d4dcSJohn Baldwin 	 * in the range 0x000 - 0x0ff should also be skipped since
265*c825d4dcSJohn Baldwin 	 * those are used for various system I/O devices in ISA
266*c825d4dcSJohn Baldwin 	 * systems.
267*c825d4dcSJohn Baldwin 	 */
268*c825d4dcSJohn Baldwin 	if (start <= 65535) {
269*c825d4dcSJohn Baldwin 		if (start < 0x100 || (start & 0x300) != 0) {
270*c825d4dcSJohn Baldwin 			start &= ~0x3ff;
271*c825d4dcSJohn Baldwin 			start += 0x400;
272*c825d4dcSJohn Baldwin 		}
273*c825d4dcSJohn Baldwin 	}
274*c825d4dcSJohn Baldwin 
275*c825d4dcSJohn Baldwin 	/* ISA aliases are only in the lower 64KB of I/O space. */
276*c825d4dcSJohn Baldwin 	while (start <= MIN(end, 65535)) {
277*c825d4dcSJohn Baldwin 		next_end = MIN(start | 0xff, end);
278*c825d4dcSJohn Baldwin 		cb(start, next_end, arg);
279*c825d4dcSJohn Baldwin 		start += 0x400;
280*c825d4dcSJohn Baldwin 	}
281*c825d4dcSJohn Baldwin 
282*c825d4dcSJohn Baldwin 	if (start <= end)
283*c825d4dcSJohn Baldwin 		cb(start, end, arg);
284*c825d4dcSJohn Baldwin }
285*c825d4dcSJohn Baldwin 
286*c825d4dcSJohn Baldwin static void
287*c825d4dcSJohn Baldwin count_ranges(u_long start, u_long end, void *arg)
288*c825d4dcSJohn Baldwin {
289*c825d4dcSJohn Baldwin 	int *countp;
290*c825d4dcSJohn Baldwin 
291*c825d4dcSJohn Baldwin 	countp = arg;
292*c825d4dcSJohn Baldwin 	(*countp)++;
293*c825d4dcSJohn Baldwin }
294*c825d4dcSJohn Baldwin 
295*c825d4dcSJohn Baldwin struct alloc_state {
296*c825d4dcSJohn Baldwin 	struct resource **res;
297*c825d4dcSJohn Baldwin 	struct pcib_softc *sc;
298*c825d4dcSJohn Baldwin 	int count, error;
299*c825d4dcSJohn Baldwin };
300*c825d4dcSJohn Baldwin 
301*c825d4dcSJohn Baldwin static void
302*c825d4dcSJohn Baldwin alloc_ranges(u_long start, u_long end, void *arg)
303*c825d4dcSJohn Baldwin {
304*c825d4dcSJohn Baldwin 	struct alloc_state *as;
305*c825d4dcSJohn Baldwin 	struct pcib_window *w;
306*c825d4dcSJohn Baldwin 	int rid;
307*c825d4dcSJohn Baldwin 
308*c825d4dcSJohn Baldwin 	as = arg;
309*c825d4dcSJohn Baldwin 	if (as->error != 0)
310*c825d4dcSJohn Baldwin 		return;
311*c825d4dcSJohn Baldwin 
312*c825d4dcSJohn Baldwin 	w = &as->sc->io;
313*c825d4dcSJohn Baldwin 	rid = w->reg;
314*c825d4dcSJohn Baldwin 	if (bootverbose)
315*c825d4dcSJohn Baldwin 		device_printf(as->sc->dev,
316*c825d4dcSJohn Baldwin 		    "allocating non-ISA range %#lx-%#lx\n", start, end);
317*c825d4dcSJohn Baldwin 	as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT,
318*c825d4dcSJohn Baldwin 	    &rid, start, end, end - start + 1, 0);
319*c825d4dcSJohn Baldwin 	if (as->res[as->count] == NULL)
320*c825d4dcSJohn Baldwin 		as->error = ENXIO;
321*c825d4dcSJohn Baldwin 	else
322*c825d4dcSJohn Baldwin 		as->count++;
323*c825d4dcSJohn Baldwin }
324*c825d4dcSJohn Baldwin 
325*c825d4dcSJohn Baldwin static int
326*c825d4dcSJohn Baldwin pcib_alloc_nonisa_ranges(struct pcib_softc *sc, u_long start, u_long end)
327*c825d4dcSJohn Baldwin {
328*c825d4dcSJohn Baldwin 	struct alloc_state as;
329*c825d4dcSJohn Baldwin 	int i, new_count;
330*c825d4dcSJohn Baldwin 
331*c825d4dcSJohn Baldwin 	/* First, see how many ranges we need. */
332*c825d4dcSJohn Baldwin 	new_count = 0;
333*c825d4dcSJohn Baldwin 	pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count);
334*c825d4dcSJohn Baldwin 
335*c825d4dcSJohn Baldwin 	/* Second, allocate the ranges. */
336*c825d4dcSJohn Baldwin 	as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF,
337*c825d4dcSJohn Baldwin 	    M_WAITOK);
338*c825d4dcSJohn Baldwin 	as.sc = sc;
339*c825d4dcSJohn Baldwin 	as.count = 0;
340*c825d4dcSJohn Baldwin 	as.error = 0;
341*c825d4dcSJohn Baldwin 	pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as);
342*c825d4dcSJohn Baldwin 	if (as.error != 0) {
343*c825d4dcSJohn Baldwin 		for (i = 0; i < as.count; i++)
344*c825d4dcSJohn Baldwin 			bus_release_resource(sc->dev, SYS_RES_IOPORT,
345*c825d4dcSJohn Baldwin 			    sc->io.reg, as.res[i]);
346*c825d4dcSJohn Baldwin 		free(as.res, M_DEVBUF);
347*c825d4dcSJohn Baldwin 		return (as.error);
348*c825d4dcSJohn Baldwin 	}
349*c825d4dcSJohn Baldwin 	KASSERT(as.count == new_count, ("%s: count mismatch", __func__));
350*c825d4dcSJohn Baldwin 
351*c825d4dcSJohn Baldwin 	/* Third, add the ranges to the window. */
352*c825d4dcSJohn Baldwin 	pcib_add_window_resources(&sc->io, as.res, as.count);
353*c825d4dcSJohn Baldwin 	free(as.res, M_DEVBUF);
354*c825d4dcSJohn Baldwin 	return (0);
355*c825d4dcSJohn Baldwin }
356*c825d4dcSJohn Baldwin 
35783c41143SJohn Baldwin static void
35883c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type,
35983c41143SJohn Baldwin     int flags, pci_addr_t max_address)
36083c41143SJohn Baldwin {
361*c825d4dcSJohn Baldwin 	struct resource *res;
36283c41143SJohn Baldwin 	char buf[64];
36383c41143SJohn Baldwin 	int error, rid;
36483c41143SJohn Baldwin 
36583c41143SJohn Baldwin 	if (max_address != (u_long)max_address)
36683c41143SJohn Baldwin 		max_address = ~0ul;
36783c41143SJohn Baldwin 	w->rman.rm_start = 0;
36883c41143SJohn Baldwin 	w->rman.rm_end = max_address;
36983c41143SJohn Baldwin 	w->rman.rm_type = RMAN_ARRAY;
37083c41143SJohn Baldwin 	snprintf(buf, sizeof(buf), "%s %s window",
37183c41143SJohn Baldwin 	    device_get_nameunit(sc->dev), w->name);
37283c41143SJohn Baldwin 	w->rman.rm_descr = strdup(buf, M_DEVBUF);
37383c41143SJohn Baldwin 	error = rman_init(&w->rman);
37483c41143SJohn Baldwin 	if (error)
37583c41143SJohn Baldwin 		panic("Failed to initialize %s %s rman",
37683c41143SJohn Baldwin 		    device_get_nameunit(sc->dev), w->name);
37783c41143SJohn Baldwin 
37883c41143SJohn Baldwin 	if (!pcib_is_window_open(w))
37983c41143SJohn Baldwin 		return;
38083c41143SJohn Baldwin 
38183c41143SJohn Baldwin 	if (w->base > max_address || w->limit > max_address) {
38283c41143SJohn Baldwin 		device_printf(sc->dev,
38383c41143SJohn Baldwin 		    "initial %s window has too many bits, ignoring\n", w->name);
38483c41143SJohn Baldwin 		return;
38583c41143SJohn Baldwin 	}
386*c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE)
387*c825d4dcSJohn Baldwin 		(void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit);
388*c825d4dcSJohn Baldwin 	else {
38983c41143SJohn Baldwin 		rid = w->reg;
390*c825d4dcSJohn Baldwin 		res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit,
39183c41143SJohn Baldwin 		    w->limit - w->base + 1, flags);
392*c825d4dcSJohn Baldwin 		if (res != NULL)
393*c825d4dcSJohn Baldwin 			pcib_add_window_resources(w, &res, 1);
394*c825d4dcSJohn Baldwin 	}
39583c41143SJohn Baldwin 	if (w->res == NULL) {
39683c41143SJohn Baldwin 		device_printf(sc->dev,
39783c41143SJohn Baldwin 		    "failed to allocate initial %s window: %#jx-%#jx\n",
39883c41143SJohn Baldwin 		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
39983c41143SJohn Baldwin 		w->base = max_address;
40083c41143SJohn Baldwin 		w->limit = 0;
40183c41143SJohn Baldwin 		pcib_write_windows(sc, w->mask);
40283c41143SJohn Baldwin 		return;
40383c41143SJohn Baldwin 	}
40483c41143SJohn Baldwin 	pcib_activate_window(sc, type);
40583c41143SJohn Baldwin }
40683c41143SJohn Baldwin 
40783c41143SJohn Baldwin /*
40883c41143SJohn Baldwin  * Initialize I/O windows.
40983c41143SJohn Baldwin  */
41083c41143SJohn Baldwin static void
41183c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc)
41283c41143SJohn Baldwin {
41383c41143SJohn Baldwin 	pci_addr_t max;
41483c41143SJohn Baldwin 	device_t dev;
41583c41143SJohn Baldwin 	uint32_t val;
41683c41143SJohn Baldwin 
41783c41143SJohn Baldwin 	dev = sc->dev;
41883c41143SJohn Baldwin 
41983c41143SJohn Baldwin 	/* Determine if the I/O port window is implemented. */
42083c41143SJohn Baldwin 	val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
42183c41143SJohn Baldwin 	if (val == 0) {
42283c41143SJohn Baldwin 		/*
42383c41143SJohn Baldwin 		 * If 'val' is zero, then only 16-bits of I/O space
42483c41143SJohn Baldwin 		 * are supported.
42583c41143SJohn Baldwin 		 */
42683c41143SJohn Baldwin 		pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
42783c41143SJohn Baldwin 		if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) {
42883c41143SJohn Baldwin 			sc->io.valid = 1;
42983c41143SJohn Baldwin 			pci_write_config(dev, PCIR_IOBASEL_1, 0, 1);
43083c41143SJohn Baldwin 		}
43183c41143SJohn Baldwin 	} else
43283c41143SJohn Baldwin 		sc->io.valid = 1;
43383c41143SJohn Baldwin 
43483c41143SJohn Baldwin 	/* Read the existing I/O port window. */
43583c41143SJohn Baldwin 	if (sc->io.valid) {
43683c41143SJohn Baldwin 		sc->io.reg = PCIR_IOBASEL_1;
43783c41143SJohn Baldwin 		sc->io.step = 12;
43883c41143SJohn Baldwin 		sc->io.mask = WIN_IO;
43983c41143SJohn Baldwin 		sc->io.name = "I/O port";
44083c41143SJohn Baldwin 		if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
44183c41143SJohn Baldwin 			sc->io.base = PCI_PPBIOBASE(
44283c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOBASEH_1, 2), val);
44383c41143SJohn Baldwin 			sc->io.limit = PCI_PPBIOLIMIT(
44483c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITH_1, 2),
44583c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
44683c41143SJohn Baldwin 			max = 0xffffffff;
44783c41143SJohn Baldwin 		} else {
44883c41143SJohn Baldwin 			sc->io.base = PCI_PPBIOBASE(0, val);
44983c41143SJohn Baldwin 			sc->io.limit = PCI_PPBIOLIMIT(0,
45083c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_IOLIMITL_1, 1));
45183c41143SJohn Baldwin 			max = 0xffff;
45283c41143SJohn Baldwin 		}
45383c41143SJohn Baldwin 		pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max);
45483c41143SJohn Baldwin 	}
45583c41143SJohn Baldwin 
45683c41143SJohn Baldwin 	/* Read the existing memory window. */
45783c41143SJohn Baldwin 	sc->mem.valid = 1;
45883c41143SJohn Baldwin 	sc->mem.reg = PCIR_MEMBASE_1;
45983c41143SJohn Baldwin 	sc->mem.step = 20;
46083c41143SJohn Baldwin 	sc->mem.mask = WIN_MEM;
46183c41143SJohn Baldwin 	sc->mem.name = "memory";
46283c41143SJohn Baldwin 	sc->mem.base = PCI_PPBMEMBASE(0,
46383c41143SJohn Baldwin 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
46483c41143SJohn Baldwin 	sc->mem.limit = PCI_PPBMEMLIMIT(0,
46583c41143SJohn Baldwin 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
46683c41143SJohn Baldwin 	pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff);
46783c41143SJohn Baldwin 
46883c41143SJohn Baldwin 	/* Determine if the prefetchable memory window is implemented. */
46983c41143SJohn Baldwin 	val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
47083c41143SJohn Baldwin 	if (val == 0) {
47183c41143SJohn Baldwin 		/*
47283c41143SJohn Baldwin 		 * If 'val' is zero, then only 32-bits of memory space
47383c41143SJohn Baldwin 		 * are supported.
47483c41143SJohn Baldwin 		 */
47583c41143SJohn Baldwin 		pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
47683c41143SJohn Baldwin 		if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) {
47783c41143SJohn Baldwin 			sc->pmem.valid = 1;
47883c41143SJohn Baldwin 			pci_write_config(dev, PCIR_PMBASEL_1, 0, 2);
47983c41143SJohn Baldwin 		}
48083c41143SJohn Baldwin 	} else
48183c41143SJohn Baldwin 		sc->pmem.valid = 1;
48283c41143SJohn Baldwin 
48383c41143SJohn Baldwin 	/* Read the existing prefetchable memory window. */
48483c41143SJohn Baldwin 	if (sc->pmem.valid) {
48583c41143SJohn Baldwin 		sc->pmem.reg = PCIR_PMBASEL_1;
48683c41143SJohn Baldwin 		sc->pmem.step = 20;
48783c41143SJohn Baldwin 		sc->pmem.mask = WIN_PMEM;
48883c41143SJohn Baldwin 		sc->pmem.name = "prefetch";
48983c41143SJohn Baldwin 		if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
49083c41143SJohn Baldwin 			sc->pmem.base = PCI_PPBMEMBASE(
49183c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMBASEH_1, 4), val);
49283c41143SJohn Baldwin 			sc->pmem.limit = PCI_PPBMEMLIMIT(
49383c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITH_1, 4),
49483c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
49583c41143SJohn Baldwin 			max = 0xffffffffffffffff;
49683c41143SJohn Baldwin 		} else {
49783c41143SJohn Baldwin 			sc->pmem.base = PCI_PPBMEMBASE(0, val);
49883c41143SJohn Baldwin 			sc->pmem.limit = PCI_PPBMEMLIMIT(0,
49983c41143SJohn Baldwin 			    pci_read_config(dev, PCIR_PMLIMITL_1, 2));
50083c41143SJohn Baldwin 			max = 0xffffffff;
50183c41143SJohn Baldwin 		}
50283c41143SJohn Baldwin 		pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY,
50383c41143SJohn Baldwin 		    RF_PREFETCHABLE, max);
50483c41143SJohn Baldwin 	}
50583c41143SJohn Baldwin }
50683c41143SJohn Baldwin 
50783c41143SJohn Baldwin #else
50883c41143SJohn Baldwin 
509bb0d0a8eSMike Smith /*
510b0a2d4b8SWarner Losh  * Is the prefetch window open (eg, can we allocate memory in it?)
511b0a2d4b8SWarner Losh  */
512b0a2d4b8SWarner Losh static int
513b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc)
514b0a2d4b8SWarner Losh {
515b0a2d4b8SWarner Losh 	return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
516b0a2d4b8SWarner Losh }
517b0a2d4b8SWarner Losh 
518b0a2d4b8SWarner Losh /*
519b0a2d4b8SWarner Losh  * Is the nonprefetch window open (eg, can we allocate memory in it?)
520b0a2d4b8SWarner Losh  */
521b0a2d4b8SWarner Losh static int
522b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc)
523b0a2d4b8SWarner Losh {
524b0a2d4b8SWarner Losh 	return (sc->membase > 0 && sc->membase < sc->memlimit);
525b0a2d4b8SWarner Losh }
526b0a2d4b8SWarner Losh 
527b0a2d4b8SWarner Losh /*
528b0a2d4b8SWarner Losh  * Is the io window open (eg, can we allocate ports in it?)
529b0a2d4b8SWarner Losh  */
530b0a2d4b8SWarner Losh static int
531b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc)
532b0a2d4b8SWarner Losh {
533b0a2d4b8SWarner Losh 	return (sc->iobase > 0 && sc->iobase < sc->iolimit);
534b0a2d4b8SWarner Losh }
535b0a2d4b8SWarner Losh 
536b0a2d4b8SWarner Losh /*
537e36af292SJung-uk Kim  * Get current I/O decode.
538e36af292SJung-uk Kim  */
539e36af292SJung-uk Kim static void
540e36af292SJung-uk Kim pcib_get_io_decode(struct pcib_softc *sc)
541e36af292SJung-uk Kim {
542e36af292SJung-uk Kim 	device_t	dev;
543e36af292SJung-uk Kim 	uint32_t	iolow;
544e36af292SJung-uk Kim 
545e36af292SJung-uk Kim 	dev = sc->dev;
546e36af292SJung-uk Kim 
547e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
548e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
549e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(
550e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow);
551e36af292SJung-uk Kim 	else
552e36af292SJung-uk Kim 		sc->iobase = PCI_PPBIOBASE(0, iolow);
553e36af292SJung-uk Kim 
554e36af292SJung-uk Kim 	iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
555e36af292SJung-uk Kim 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
556e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(
557e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow);
558e36af292SJung-uk Kim 	else
559e36af292SJung-uk Kim 		sc->iolimit = PCI_PPBIOLIMIT(0, iolow);
560e36af292SJung-uk Kim }
561e36af292SJung-uk Kim 
562e36af292SJung-uk Kim /*
563e36af292SJung-uk Kim  * Get current memory decode.
564e36af292SJung-uk Kim  */
565e36af292SJung-uk Kim static void
566e36af292SJung-uk Kim pcib_get_mem_decode(struct pcib_softc *sc)
567e36af292SJung-uk Kim {
568e36af292SJung-uk Kim 	device_t	dev;
569e36af292SJung-uk Kim 	pci_addr_t	pmemlow;
570e36af292SJung-uk Kim 
571e36af292SJung-uk Kim 	dev = sc->dev;
572e36af292SJung-uk Kim 
573e36af292SJung-uk Kim 	sc->membase = PCI_PPBMEMBASE(0,
574e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMBASE_1, 2));
575e36af292SJung-uk Kim 	sc->memlimit = PCI_PPBMEMLIMIT(0,
576e36af292SJung-uk Kim 	    pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
577e36af292SJung-uk Kim 
578e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2);
579e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
580e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(
581e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow);
582e36af292SJung-uk Kim 	else
583e36af292SJung-uk Kim 		sc->pmembase = PCI_PPBMEMBASE(0, pmemlow);
584e36af292SJung-uk Kim 
585e36af292SJung-uk Kim 	pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2);
586e36af292SJung-uk Kim 	if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
587e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(
588e36af292SJung-uk Kim 		    pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow);
589e36af292SJung-uk Kim 	else
590e36af292SJung-uk Kim 		sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow);
591e36af292SJung-uk Kim }
592e36af292SJung-uk Kim 
593e36af292SJung-uk Kim /*
594e36af292SJung-uk Kim  * Restore previous I/O decode.
595e36af292SJung-uk Kim  */
596e36af292SJung-uk Kim static void
597e36af292SJung-uk Kim pcib_set_io_decode(struct pcib_softc *sc)
598e36af292SJung-uk Kim {
599e36af292SJung-uk Kim 	device_t	dev;
600e36af292SJung-uk Kim 	uint32_t	iohi;
601e36af292SJung-uk Kim 
602e36af292SJung-uk Kim 	dev = sc->dev;
603e36af292SJung-uk Kim 
604e36af292SJung-uk Kim 	iohi = sc->iobase >> 16;
605e36af292SJung-uk Kim 	if (iohi > 0)
606e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2);
607e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1);
608e36af292SJung-uk Kim 
609e36af292SJung-uk Kim 	iohi = sc->iolimit >> 16;
610e36af292SJung-uk Kim 	if (iohi > 0)
611e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2);
612e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1);
613e36af292SJung-uk Kim }
614e36af292SJung-uk Kim 
615e36af292SJung-uk Kim /*
616e36af292SJung-uk Kim  * Restore previous memory decode.
617e36af292SJung-uk Kim  */
618e36af292SJung-uk Kim static void
619e36af292SJung-uk Kim pcib_set_mem_decode(struct pcib_softc *sc)
620e36af292SJung-uk Kim {
621e36af292SJung-uk Kim 	device_t	dev;
622e36af292SJung-uk Kim 	pci_addr_t	pmemhi;
623e36af292SJung-uk Kim 
624e36af292SJung-uk Kim 	dev = sc->dev;
625e36af292SJung-uk Kim 
626e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2);
627e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2);
628e36af292SJung-uk Kim 
629e36af292SJung-uk Kim 	pmemhi = sc->pmembase >> 32;
630e36af292SJung-uk Kim 	if (pmemhi > 0)
631e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4);
632e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2);
633e36af292SJung-uk Kim 
634e36af292SJung-uk Kim 	pmemhi = sc->pmemlimit >> 32;
635e36af292SJung-uk Kim 	if (pmemhi > 0)
636e36af292SJung-uk Kim 		pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4);
637e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2);
638e36af292SJung-uk Kim }
63983c41143SJohn Baldwin #endif
640e36af292SJung-uk Kim 
641e36af292SJung-uk Kim /*
642e36af292SJung-uk Kim  * Get current bridge configuration.
643e36af292SJung-uk Kim  */
644e36af292SJung-uk Kim static void
645e36af292SJung-uk Kim pcib_cfg_save(struct pcib_softc *sc)
646e36af292SJung-uk Kim {
647e36af292SJung-uk Kim 	device_t	dev;
648e36af292SJung-uk Kim 
649e36af292SJung-uk Kim 	dev = sc->dev;
650e36af292SJung-uk Kim 
651e36af292SJung-uk Kim 	sc->command = pci_read_config(dev, PCIR_COMMAND, 2);
652e36af292SJung-uk Kim 	sc->pribus = pci_read_config(dev, PCIR_PRIBUS_1, 1);
653e36af292SJung-uk Kim 	sc->secbus = pci_read_config(dev, PCIR_SECBUS_1, 1);
654e36af292SJung-uk Kim 	sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1);
655e36af292SJung-uk Kim 	sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
656e36af292SJung-uk Kim 	sc->seclat = pci_read_config(dev, PCIR_SECLAT_1, 1);
65783c41143SJohn Baldwin #ifndef NEW_PCIB
658e36af292SJung-uk Kim 	if (sc->command & PCIM_CMD_PORTEN)
659e36af292SJung-uk Kim 		pcib_get_io_decode(sc);
660e36af292SJung-uk Kim 	if (sc->command & PCIM_CMD_MEMEN)
661e36af292SJung-uk Kim 		pcib_get_mem_decode(sc);
66283c41143SJohn Baldwin #endif
663e36af292SJung-uk Kim }
664e36af292SJung-uk Kim 
665e36af292SJung-uk Kim /*
666e36af292SJung-uk Kim  * Restore previous bridge configuration.
667e36af292SJung-uk Kim  */
668e36af292SJung-uk Kim static void
669e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc)
670e36af292SJung-uk Kim {
671e36af292SJung-uk Kim 	device_t	dev;
672e36af292SJung-uk Kim 
673e36af292SJung-uk Kim 	dev = sc->dev;
674e36af292SJung-uk Kim 
675e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_COMMAND, sc->command, 2);
676e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
677e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_SECBUS_1, sc->secbus, 1);
678e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_SUBBUS_1, sc->subbus, 1);
679e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_BRIDGECTL_1, sc->bridgectl, 2);
680e36af292SJung-uk Kim 	pci_write_config(dev, PCIR_SECLAT_1, sc->seclat, 1);
68183c41143SJohn Baldwin #ifdef NEW_PCIB
68283c41143SJohn Baldwin 	pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM);
68383c41143SJohn Baldwin #else
684e36af292SJung-uk Kim 	if (sc->command & PCIM_CMD_PORTEN)
685e36af292SJung-uk Kim 		pcib_set_io_decode(sc);
686e36af292SJung-uk Kim 	if (sc->command & PCIM_CMD_MEMEN)
687e36af292SJung-uk Kim 		pcib_set_mem_decode(sc);
68883c41143SJohn Baldwin #endif
689e36af292SJung-uk Kim }
690e36af292SJung-uk Kim 
691e36af292SJung-uk Kim /*
692bb0d0a8eSMike Smith  * Generic device interface
693bb0d0a8eSMike Smith  */
694bb0d0a8eSMike Smith static int
695bb0d0a8eSMike Smith pcib_probe(device_t dev)
696bb0d0a8eSMike Smith {
697bb0d0a8eSMike Smith     if ((pci_get_class(dev) == PCIC_BRIDGE) &&
698bb0d0a8eSMike Smith 	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
699bb0d0a8eSMike Smith 	device_set_desc(dev, "PCI-PCI bridge");
700bb0d0a8eSMike Smith 	return(-10000);
701bb0d0a8eSMike Smith     }
702bb0d0a8eSMike Smith     return(ENXIO);
703bb0d0a8eSMike Smith }
704bb0d0a8eSMike Smith 
7056f0d5884SJohn Baldwin void
7066f0d5884SJohn Baldwin pcib_attach_common(device_t dev)
707bb0d0a8eSMike Smith {
708bb0d0a8eSMike Smith     struct pcib_softc	*sc;
709abf07f13SWarner Losh     struct sysctl_ctx_list *sctx;
710abf07f13SWarner Losh     struct sysctl_oid	*soid;
711*c825d4dcSJohn Baldwin     int comma;
712bb0d0a8eSMike Smith 
713bb0d0a8eSMike Smith     sc = device_get_softc(dev);
714bb0d0a8eSMike Smith     sc->dev = dev;
715bb0d0a8eSMike Smith 
7164fa59183SMike Smith     /*
7174fa59183SMike Smith      * Get current bridge configuration.
7184fa59183SMike Smith      */
71955aaf894SMarius Strobl     sc->domain = pci_get_domain(dev);
7204fa59183SMike Smith     sc->secstat = pci_read_config(dev, PCIR_SECSTAT_1, 2);
721e36af292SJung-uk Kim     pcib_cfg_save(sc);
7224fa59183SMike Smith 
7234fa59183SMike Smith     /*
724abf07f13SWarner Losh      * Setup sysctl reporting nodes
725abf07f13SWarner Losh      */
726abf07f13SWarner Losh     sctx = device_get_sysctl_ctx(dev);
727abf07f13SWarner Losh     soid = device_get_sysctl_tree(dev);
728abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
729abf07f13SWarner Losh       CTLFLAG_RD, &sc->domain, 0, "Domain number");
730abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
731abf07f13SWarner Losh       CTLFLAG_RD, &sc->pribus, 0, "Primary bus number");
732abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
733abf07f13SWarner Losh       CTLFLAG_RD, &sc->secbus, 0, "Secondary bus number");
734abf07f13SWarner Losh     SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
735abf07f13SWarner Losh       CTLFLAG_RD, &sc->subbus, 0, "Subordinate bus number");
736abf07f13SWarner Losh 
737abf07f13SWarner Losh     /*
7384fa59183SMike Smith      * Quirk handling.
7394fa59183SMike Smith      */
7404fa59183SMike Smith     switch (pci_get_devid(dev)) {
7414fa59183SMike Smith     case 0x12258086:		/* Intel 82454KX/GX (Orion) */
7424fa59183SMike Smith 	{
743b0cb115fSWarner Losh 	    uint8_t	supbus;
7444fa59183SMike Smith 
7454fa59183SMike Smith 	    supbus = pci_read_config(dev, 0x41, 1);
7464fa59183SMike Smith 	    if (supbus != 0xff) {
7474fa59183SMike Smith 		sc->secbus = supbus + 1;
7484fa59183SMike Smith 		sc->subbus = supbus + 1;
7494fa59183SMike Smith 	    }
7504fa59183SMike Smith 	    break;
7514fa59183SMike Smith 	}
7524fa59183SMike Smith 
753e4b59fc5SWarner Losh     /*
754e4b59fc5SWarner Losh      * The i82380FB mobile docking controller is a PCI-PCI bridge,
755e4b59fc5SWarner Losh      * and it is a subtractive bridge.  However, the ProgIf is wrong
756e4b59fc5SWarner Losh      * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
757e4b59fc5SWarner Losh      * happen.  There's also a Toshiba bridge that behaves this
758e4b59fc5SWarner Losh      * way.
759e4b59fc5SWarner Losh      */
760e4b59fc5SWarner Losh     case 0x124b8086:		/* Intel 82380FB Mobile */
761e4b59fc5SWarner Losh     case 0x060513d7:		/* Toshiba ???? */
762e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
763e4b59fc5SWarner Losh 	break;
764c94d6dbeSJung-uk Kim 
765c94d6dbeSJung-uk Kim     /* Compaq R3000 BIOS sets wrong subordinate bus number. */
766c94d6dbeSJung-uk Kim     case 0x00dd10de:
767c94d6dbeSJung-uk Kim 	{
768c94d6dbeSJung-uk Kim 	    char *cp;
769c94d6dbeSJung-uk Kim 
7701def0ca6SJung-uk Kim 	    if ((cp = getenv("smbios.planar.maker")) == NULL)
771c94d6dbeSJung-uk Kim 		break;
7721def0ca6SJung-uk Kim 	    if (strncmp(cp, "Compal", 6) != 0) {
7731def0ca6SJung-uk Kim 		freeenv(cp);
774c94d6dbeSJung-uk Kim 		break;
7751def0ca6SJung-uk Kim 	    }
7761def0ca6SJung-uk Kim 	    freeenv(cp);
7771def0ca6SJung-uk Kim 	    if ((cp = getenv("smbios.planar.product")) == NULL)
7781def0ca6SJung-uk Kim 		break;
7791def0ca6SJung-uk Kim 	    if (strncmp(cp, "08A0", 4) != 0) {
7801def0ca6SJung-uk Kim 		freeenv(cp);
7811def0ca6SJung-uk Kim 		break;
7821def0ca6SJung-uk Kim 	    }
7831def0ca6SJung-uk Kim 	    freeenv(cp);
784c94d6dbeSJung-uk Kim 	    if (sc->subbus < 0xa) {
785c94d6dbeSJung-uk Kim 		pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
786c94d6dbeSJung-uk Kim 		sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1);
787c94d6dbeSJung-uk Kim 	    }
788c94d6dbeSJung-uk Kim 	    break;
789c94d6dbeSJung-uk Kim 	}
790e4b59fc5SWarner Losh     }
791e4b59fc5SWarner Losh 
79222bf1c7fSJohn Baldwin     if (pci_msi_device_blacklisted(dev))
79322bf1c7fSJohn Baldwin 	sc->flags |= PCIB_DISABLE_MSI;
79422bf1c7fSJohn Baldwin 
79568e9cbd3SMarius Strobl     if (pci_msix_device_blacklisted(dev))
79668e9cbd3SMarius Strobl 	sc->flags |= PCIB_DISABLE_MSIX;
79768e9cbd3SMarius Strobl 
798e4b59fc5SWarner Losh     /*
799e4b59fc5SWarner Losh      * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
800e4b59fc5SWarner Losh      * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
801e4b59fc5SWarner Losh      * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
802e4b59fc5SWarner Losh      * This means they act as if they were subtractively decoding
803e4b59fc5SWarner Losh      * bridges and pass all transactions.  Mark them and real ProgIf 1
804e4b59fc5SWarner Losh      * parts as subtractive.
805e4b59fc5SWarner Losh      */
806e4b59fc5SWarner Losh     if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
807657d9f9fSJohn Baldwin       pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
808e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
809e4b59fc5SWarner Losh 
81083c41143SJohn Baldwin #ifdef NEW_PCIB
81183c41143SJohn Baldwin     pcib_probe_windows(sc);
81283c41143SJohn Baldwin #endif
813bb0d0a8eSMike Smith     if (bootverbose) {
81455aaf894SMarius Strobl 	device_printf(dev, "  domain            %d\n", sc->domain);
815bb0d0a8eSMike Smith 	device_printf(dev, "  secondary bus     %d\n", sc->secbus);
816bb0d0a8eSMike Smith 	device_printf(dev, "  subordinate bus   %d\n", sc->subbus);
81783c41143SJohn Baldwin #ifdef NEW_PCIB
81883c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->io))
81983c41143SJohn Baldwin 	    device_printf(dev, "  I/O decode        0x%jx-0x%jx\n",
82083c41143SJohn Baldwin 	      (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit);
82183c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->mem))
82283c41143SJohn Baldwin 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
82383c41143SJohn Baldwin 	      (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit);
82483c41143SJohn Baldwin 	if (pcib_is_window_open(&sc->pmem))
82583c41143SJohn Baldwin 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
82683c41143SJohn Baldwin 	      (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit);
82783c41143SJohn Baldwin #else
82883c41143SJohn Baldwin 	if (pcib_is_io_open(sc))
82983c41143SJohn Baldwin 	    device_printf(dev, "  I/O decode        0x%x-0x%x\n",
83083c41143SJohn Baldwin 	      sc->iobase, sc->iolimit);
831b0a2d4b8SWarner Losh 	if (pcib_is_nonprefetch_open(sc))
832b0a2d4b8SWarner Losh 	    device_printf(dev, "  memory decode     0x%jx-0x%jx\n",
833b0a2d4b8SWarner Losh 	      (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
834b0a2d4b8SWarner Losh 	if (pcib_is_prefetch_open(sc))
835b0a2d4b8SWarner Losh 	    device_printf(dev, "  prefetched decode 0x%jx-0x%jx\n",
836b0a2d4b8SWarner Losh 	      (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
83783c41143SJohn Baldwin #endif
838*c825d4dcSJohn Baldwin 	if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) ||
839*c825d4dcSJohn Baldwin 	    sc->flags & PCIB_SUBTRACTIVE) {
840*c825d4dcSJohn Baldwin 		device_printf(dev, "  special decode    ");
841*c825d4dcSJohn Baldwin 		comma = 0;
842*c825d4dcSJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) {
843*c825d4dcSJohn Baldwin 			printf("ISA");
844*c825d4dcSJohn Baldwin 			comma = 1;
845*c825d4dcSJohn Baldwin 		}
846*c825d4dcSJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) {
847*c825d4dcSJohn Baldwin 			printf("%sVGA", comma ? ", " : "");
848*c825d4dcSJohn Baldwin 			comma = 1;
849*c825d4dcSJohn Baldwin 		}
850e4b59fc5SWarner Losh 		if (sc->flags & PCIB_SUBTRACTIVE)
851*c825d4dcSJohn Baldwin 			printf("%ssubtractive", comma ? ", " : "");
852*c825d4dcSJohn Baldwin 		printf("\n");
853*c825d4dcSJohn Baldwin 	}
854bb0d0a8eSMike Smith     }
855bb0d0a8eSMike Smith 
856bb0d0a8eSMike Smith     /*
857bb0d0a8eSMike Smith      * XXX If the secondary bus number is zero, we should assign a bus number
8587e178674SWarner Losh      *     since the BIOS hasn't, then initialise the bridge.  A simple
8597e178674SWarner Losh      *     bus_alloc_resource with the a couple of busses seems like the right
8607e178674SWarner Losh      *     approach, but we don't know what busses the BIOS might have already
8617e178674SWarner Losh      *     assigned to other bridges on this bus that probe later than we do.
8627e178674SWarner Losh      *
8637e178674SWarner Losh      *     If the subordinate bus number is less than the secondary bus number,
864bb0d0a8eSMike Smith      *     we should pick a better value.  One sensible alternative would be to
865bb0d0a8eSMike Smith      *     pick 255; the only tradeoff here is that configuration transactions
8667e178674SWarner Losh      *     would be more widely routed than absolutely necessary.  We could
8677e178674SWarner Losh      *     then do a walk of the tree later and fix it.
868bb0d0a8eSMike Smith      */
869ef888152SJohn Baldwin 
870ef888152SJohn Baldwin     /*
871ef888152SJohn Baldwin      * Always enable busmastering on bridges so that transactions
872ef888152SJohn Baldwin      * initiated on the secondary bus are passed through to the
873ef888152SJohn Baldwin      * primary bus.
874ef888152SJohn Baldwin      */
875ef888152SJohn Baldwin     pci_enable_busmaster(dev);
8766f0d5884SJohn Baldwin }
877bb0d0a8eSMike Smith 
87838906aedSJohn Baldwin int
8796f0d5884SJohn Baldwin pcib_attach(device_t dev)
8806f0d5884SJohn Baldwin {
8816f0d5884SJohn Baldwin     struct pcib_softc	*sc;
8826f0d5884SJohn Baldwin     device_t		child;
8836f0d5884SJohn Baldwin 
8846f0d5884SJohn Baldwin     pcib_attach_common(dev);
8856f0d5884SJohn Baldwin     sc = device_get_softc(dev);
886bb0d0a8eSMike Smith     if (sc->secbus != 0) {
887cea0a895SJohn Baldwin 	child = device_add_child(dev, "pci", sc->secbus);
888bb0d0a8eSMike Smith 	if (child != NULL)
889bb0d0a8eSMike Smith 	    return(bus_generic_attach(dev));
890bb0d0a8eSMike Smith     }
891bb0d0a8eSMike Smith 
892bb0d0a8eSMike Smith     /* no secondary bus; we should have fixed this */
893bb0d0a8eSMike Smith     return(0);
894bb0d0a8eSMike Smith }
895bb0d0a8eSMike Smith 
8966f0d5884SJohn Baldwin int
897e36af292SJung-uk Kim pcib_suspend(device_t dev)
898e36af292SJung-uk Kim {
89962508c53SJohn Baldwin 	device_t	pcib;
900e36af292SJung-uk Kim 	int		dstate, error;
901e36af292SJung-uk Kim 
902e36af292SJung-uk Kim 	pcib_cfg_save(device_get_softc(dev));
903e36af292SJung-uk Kim 	error = bus_generic_suspend(dev);
904f3e0b109SJung-uk Kim 	if (error == 0 && pci_do_power_suspend) {
905e36af292SJung-uk Kim 		dstate = PCI_POWERSTATE_D3;
90662508c53SJohn Baldwin 		pcib = device_get_parent(device_get_parent(dev));
90762508c53SJohn Baldwin 		if (PCIB_POWER_FOR_SLEEP(pcib, dev, &dstate) == 0)
908e36af292SJung-uk Kim 			pci_set_powerstate(dev, dstate);
909e36af292SJung-uk Kim 	}
910e36af292SJung-uk Kim 	return (error);
911e36af292SJung-uk Kim }
912e36af292SJung-uk Kim 
913e36af292SJung-uk Kim int
914e36af292SJung-uk Kim pcib_resume(device_t dev)
915e36af292SJung-uk Kim {
91662508c53SJohn Baldwin 	device_t	pcib;
917e36af292SJung-uk Kim 
918e36af292SJung-uk Kim 	if (pci_do_power_resume) {
91962508c53SJohn Baldwin 		pcib = device_get_parent(device_get_parent(dev));
92062508c53SJohn Baldwin 		if (PCIB_POWER_FOR_SLEEP(pcib, dev, NULL) == 0)
921e36af292SJung-uk Kim 			pci_set_powerstate(dev, PCI_POWERSTATE_D0);
922e36af292SJung-uk Kim 	}
923e36af292SJung-uk Kim 	pcib_cfg_restore(device_get_softc(dev));
924e36af292SJung-uk Kim 	return (bus_generic_resume(dev));
925e36af292SJung-uk Kim }
926e36af292SJung-uk Kim 
927e36af292SJung-uk Kim int
928bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
929bb0d0a8eSMike Smith {
930bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
931bb0d0a8eSMike Smith 
932bb0d0a8eSMike Smith     switch (which) {
93355aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
93455aaf894SMarius Strobl 	*result = sc->domain;
93555aaf894SMarius Strobl 	return(0);
936bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
937bb0d0a8eSMike Smith 	*result = sc->secbus;
938bb0d0a8eSMike Smith 	return(0);
939bb0d0a8eSMike Smith     }
940bb0d0a8eSMike Smith     return(ENOENT);
941bb0d0a8eSMike Smith }
942bb0d0a8eSMike Smith 
9436f0d5884SJohn Baldwin int
944bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
945bb0d0a8eSMike Smith {
946bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
947bb0d0a8eSMike Smith 
948bb0d0a8eSMike Smith     switch (which) {
94955aaf894SMarius Strobl     case PCIB_IVAR_DOMAIN:
95055aaf894SMarius Strobl 	return(EINVAL);
951bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
952bb0d0a8eSMike Smith 	sc->secbus = value;
95355aaf894SMarius Strobl 	return(0);
954bb0d0a8eSMike Smith     }
955bb0d0a8eSMike Smith     return(ENOENT);
956bb0d0a8eSMike Smith }
957bb0d0a8eSMike Smith 
95883c41143SJohn Baldwin #ifdef NEW_PCIB
95983c41143SJohn Baldwin /*
96083c41143SJohn Baldwin  * Attempt to allocate a resource from the existing resources assigned
96183c41143SJohn Baldwin  * to a window.
96283c41143SJohn Baldwin  */
96383c41143SJohn Baldwin static struct resource *
96483c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w,
96583c41143SJohn Baldwin     device_t child, int type, int *rid, u_long start, u_long end, u_long count,
96683c41143SJohn Baldwin     u_int flags)
96783c41143SJohn Baldwin {
96883c41143SJohn Baldwin 	struct resource *res;
96983c41143SJohn Baldwin 
97083c41143SJohn Baldwin 	if (!pcib_is_window_open(w))
97183c41143SJohn Baldwin 		return (NULL);
97283c41143SJohn Baldwin 
97383c41143SJohn Baldwin 	res = rman_reserve_resource(&w->rman, start, end, count,
97483c41143SJohn Baldwin 	    flags & ~RF_ACTIVE, child);
97583c41143SJohn Baldwin 	if (res == NULL)
97683c41143SJohn Baldwin 		return (NULL);
97783c41143SJohn Baldwin 
97883c41143SJohn Baldwin 	if (bootverbose)
97983c41143SJohn Baldwin 		device_printf(sc->dev,
98083c41143SJohn Baldwin 		    "allocated %s range (%#lx-%#lx) for rid %x of %s\n",
98183c41143SJohn Baldwin 		    w->name, rman_get_start(res), rman_get_end(res), *rid,
98283c41143SJohn Baldwin 		    pcib_child_name(child));
98383c41143SJohn Baldwin 	rman_set_rid(res, *rid);
98483c41143SJohn Baldwin 
98583c41143SJohn Baldwin 	/*
98683c41143SJohn Baldwin 	 * If the resource should be active, pass that request up the
98783c41143SJohn Baldwin 	 * tree.  This assumes the parent drivers can handle
98883c41143SJohn Baldwin 	 * activating sub-allocated resources.
98983c41143SJohn Baldwin 	 */
99083c41143SJohn Baldwin 	if (flags & RF_ACTIVE) {
99183c41143SJohn Baldwin 		if (bus_activate_resource(child, type, *rid, res) != 0) {
99283c41143SJohn Baldwin 			rman_release_resource(res);
99383c41143SJohn Baldwin 			return (NULL);
99483c41143SJohn Baldwin 		}
99583c41143SJohn Baldwin 	}
99683c41143SJohn Baldwin 
99783c41143SJohn Baldwin 	return (res);
99883c41143SJohn Baldwin }
99983c41143SJohn Baldwin 
1000*c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */
1001*c825d4dcSJohn Baldwin static int
1002*c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type,
1003*c825d4dcSJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
1004*c825d4dcSJohn Baldwin {
1005*c825d4dcSJohn Baldwin 	struct resource *res;
1006*c825d4dcSJohn Baldwin 	u_long base, limit, wmask;
1007*c825d4dcSJohn Baldwin 	int rid;
1008*c825d4dcSJohn Baldwin 
1009*c825d4dcSJohn Baldwin 	/*
1010*c825d4dcSJohn Baldwin 	 * If this is an I/O window on a bridge with ISA enable set
1011*c825d4dcSJohn Baldwin 	 * and the start address is below 64k, then try to allocate an
1012*c825d4dcSJohn Baldwin 	 * initial window of 0x1000 bytes long starting at address
1013*c825d4dcSJohn Baldwin 	 * 0xf000 and walking down.  Note that if the original request
1014*c825d4dcSJohn Baldwin 	 * was larger than the non-aliased range size of 0x100 our
1015*c825d4dcSJohn Baldwin 	 * caller would have raised the start address up to 64k
1016*c825d4dcSJohn Baldwin 	 * already.
1017*c825d4dcSJohn Baldwin 	 */
1018*c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1019*c825d4dcSJohn Baldwin 	    start < 65536) {
1020*c825d4dcSJohn Baldwin 		for (base = 0xf000; (long)base >= 0; base -= 0x1000) {
1021*c825d4dcSJohn Baldwin 			limit = base + 0xfff;
1022*c825d4dcSJohn Baldwin 
1023*c825d4dcSJohn Baldwin 			/*
1024*c825d4dcSJohn Baldwin 			 * Skip ranges that wouldn't work for the
1025*c825d4dcSJohn Baldwin 			 * original request.  Note that the actual
1026*c825d4dcSJohn Baldwin 			 * window that overlaps are the non-alias
1027*c825d4dcSJohn Baldwin 			 * ranges within [base, limit], so this isn't
1028*c825d4dcSJohn Baldwin 			 * quite a simple comparison.
1029*c825d4dcSJohn Baldwin 			 */
1030*c825d4dcSJohn Baldwin 			if (start + count > limit - 0x400)
1031*c825d4dcSJohn Baldwin 				continue;
1032*c825d4dcSJohn Baldwin 			if (base == 0) {
1033*c825d4dcSJohn Baldwin 				/*
1034*c825d4dcSJohn Baldwin 				 * The first open region for the window at
1035*c825d4dcSJohn Baldwin 				 * 0 is 0x400-0x4ff.
1036*c825d4dcSJohn Baldwin 				 */
1037*c825d4dcSJohn Baldwin 				if (end - count + 1 < 0x400)
1038*c825d4dcSJohn Baldwin 					continue;
1039*c825d4dcSJohn Baldwin 			} else {
1040*c825d4dcSJohn Baldwin 				if (end - count + 1 < base)
1041*c825d4dcSJohn Baldwin 					continue;
1042*c825d4dcSJohn Baldwin 			}
1043*c825d4dcSJohn Baldwin 
1044*c825d4dcSJohn Baldwin 			if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) {
1045*c825d4dcSJohn Baldwin 				w->base = base;
1046*c825d4dcSJohn Baldwin 				w->limit = limit;
1047*c825d4dcSJohn Baldwin 				return (0);
1048*c825d4dcSJohn Baldwin 			}
1049*c825d4dcSJohn Baldwin 		}
1050*c825d4dcSJohn Baldwin 		return (ENOSPC);
1051*c825d4dcSJohn Baldwin 	}
1052*c825d4dcSJohn Baldwin 
1053*c825d4dcSJohn Baldwin 	wmask = (1ul << w->step) - 1;
1054*c825d4dcSJohn Baldwin 	if (RF_ALIGNMENT(flags) < w->step) {
1055*c825d4dcSJohn Baldwin 		flags &= ~RF_ALIGNMENT_MASK;
1056*c825d4dcSJohn Baldwin 		flags |= RF_ALIGNMENT_LOG2(w->step);
1057*c825d4dcSJohn Baldwin 	}
1058*c825d4dcSJohn Baldwin 	start &= ~wmask;
1059*c825d4dcSJohn Baldwin 	end |= wmask;
1060*c825d4dcSJohn Baldwin 	count = roundup2(count, 1ul << w->step);
1061*c825d4dcSJohn Baldwin 	rid = w->reg;
1062*c825d4dcSJohn Baldwin 	res = bus_alloc_resource(sc->dev, type, &rid, start, end, count,
1063*c825d4dcSJohn Baldwin 	    flags & ~RF_ACTIVE);
1064*c825d4dcSJohn Baldwin 	if (res == NULL)
1065*c825d4dcSJohn Baldwin 		return (ENOSPC);
1066*c825d4dcSJohn Baldwin 	pcib_add_window_resources(w, &res, 1);
1067*c825d4dcSJohn Baldwin 	pcib_activate_window(sc, type);
1068*c825d4dcSJohn Baldwin 	w->base = rman_get_start(res);
1069*c825d4dcSJohn Baldwin 	w->limit = rman_get_end(res);
1070*c825d4dcSJohn Baldwin 	return (0);
1071*c825d4dcSJohn Baldwin }
1072*c825d4dcSJohn Baldwin 
1073*c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */
1074*c825d4dcSJohn Baldwin static int
1075*c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type,
1076*c825d4dcSJohn Baldwin     u_long base, u_long limit)
1077*c825d4dcSJohn Baldwin {
1078*c825d4dcSJohn Baldwin 	struct resource *res;
1079*c825d4dcSJohn Baldwin 	int error, i, force_64k_base;
1080*c825d4dcSJohn Baldwin 
1081*c825d4dcSJohn Baldwin 	KASSERT(base <= w->base && limit >= w->limit,
1082*c825d4dcSJohn Baldwin 	    ("attempting to shrink window"));
1083*c825d4dcSJohn Baldwin 
1084*c825d4dcSJohn Baldwin 	/*
1085*c825d4dcSJohn Baldwin 	 * XXX: pcib_grow_window() doesn't try to do this anyway and
1086*c825d4dcSJohn Baldwin 	 * the error handling for all the edge cases would be tedious.
1087*c825d4dcSJohn Baldwin 	 */
1088*c825d4dcSJohn Baldwin 	KASSERT(limit == w->limit || base == w->base,
1089*c825d4dcSJohn Baldwin 	    ("attempting to grow both ends of a window"));
1090*c825d4dcSJohn Baldwin 
1091*c825d4dcSJohn Baldwin 	/*
1092*c825d4dcSJohn Baldwin 	 * Yet more special handling for requests to expand an I/O
1093*c825d4dcSJohn Baldwin 	 * window behind an ISA-enabled bridge.  Since I/O windows
1094*c825d4dcSJohn Baldwin 	 * have to grow in 0x1000 increments and the end of the 0xffff
1095*c825d4dcSJohn Baldwin 	 * range is an alias, growing a window below 64k will always
1096*c825d4dcSJohn Baldwin 	 * result in allocating new resources and never adjusting an
1097*c825d4dcSJohn Baldwin 	 * existing resource.
1098*c825d4dcSJohn Baldwin 	 */
1099*c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1100*c825d4dcSJohn Baldwin 	    (limit <= 65535 || (base <= 65535 && base != w->base))) {
1101*c825d4dcSJohn Baldwin 		KASSERT(limit == w->limit || limit <= 65535,
1102*c825d4dcSJohn Baldwin 		    ("attempting to grow both ends across 64k ISA alias"));
1103*c825d4dcSJohn Baldwin 
1104*c825d4dcSJohn Baldwin 		if (base != w->base)
1105*c825d4dcSJohn Baldwin 			error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1);
1106*c825d4dcSJohn Baldwin 		else
1107*c825d4dcSJohn Baldwin 			error = pcib_alloc_nonisa_ranges(sc, w->limit + 1,
1108*c825d4dcSJohn Baldwin 			    limit);
1109*c825d4dcSJohn Baldwin 		if (error == 0) {
1110*c825d4dcSJohn Baldwin 			w->base = base;
1111*c825d4dcSJohn Baldwin 			w->limit = limit;
1112*c825d4dcSJohn Baldwin 		}
1113*c825d4dcSJohn Baldwin 		return (error);
1114*c825d4dcSJohn Baldwin 	}
1115*c825d4dcSJohn Baldwin 
1116*c825d4dcSJohn Baldwin 	/*
1117*c825d4dcSJohn Baldwin 	 * Find the existing resource to adjust.  Usually there is only one,
1118*c825d4dcSJohn Baldwin 	 * but for an ISA-enabled bridge we might be growing the I/O window
1119*c825d4dcSJohn Baldwin 	 * above 64k and need to find the existing resource that maps all
1120*c825d4dcSJohn Baldwin 	 * of the area above 64k.
1121*c825d4dcSJohn Baldwin 	 */
1122*c825d4dcSJohn Baldwin 	for (i = 0; i < w->count; i++) {
1123*c825d4dcSJohn Baldwin 		if (rman_get_end(w->res[i]) == w->limit)
1124*c825d4dcSJohn Baldwin 			break;
1125*c825d4dcSJohn Baldwin 	}
1126*c825d4dcSJohn Baldwin 	KASSERT(i != w->count, ("did not find existing resource"));
1127*c825d4dcSJohn Baldwin 	res = w->res[i];
1128*c825d4dcSJohn Baldwin 
1129*c825d4dcSJohn Baldwin 	/*
1130*c825d4dcSJohn Baldwin 	 * Usually the resource we found should match the window's
1131*c825d4dcSJohn Baldwin 	 * existing range.  The one exception is the ISA-enabled case
1132*c825d4dcSJohn Baldwin 	 * mentioned above in which case the resource should start at
1133*c825d4dcSJohn Baldwin 	 * 64k.
1134*c825d4dcSJohn Baldwin 	 */
1135*c825d4dcSJohn Baldwin 	if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1136*c825d4dcSJohn Baldwin 	    w->base <= 65535) {
1137*c825d4dcSJohn Baldwin 		KASSERT(rman_get_start(res) == 65536,
1138*c825d4dcSJohn Baldwin 		    ("existing resource mismatch"));
1139*c825d4dcSJohn Baldwin 		force_64k_base = 1;
1140*c825d4dcSJohn Baldwin 	} else {
1141*c825d4dcSJohn Baldwin 		KASSERT(w->base == rman_get_start(res),
1142*c825d4dcSJohn Baldwin 		    ("existing resource mismatch"));
1143*c825d4dcSJohn Baldwin 		force_64k_base = 0;
1144*c825d4dcSJohn Baldwin 	}
1145*c825d4dcSJohn Baldwin 
1146*c825d4dcSJohn Baldwin 	error = bus_adjust_resource(sc->dev, type, res, force_64k_base ?
1147*c825d4dcSJohn Baldwin 	    rman_get_start(res) : base, limit);
1148*c825d4dcSJohn Baldwin 	if (error)
1149*c825d4dcSJohn Baldwin 		return (error);
1150*c825d4dcSJohn Baldwin 
1151*c825d4dcSJohn Baldwin 	/* Add the newly allocated region to the resource manager. */
1152*c825d4dcSJohn Baldwin 	if (w->base != base) {
1153*c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, base, w->base - 1);
1154*c825d4dcSJohn Baldwin 		w->base = base;
1155*c825d4dcSJohn Baldwin 	} else {
1156*c825d4dcSJohn Baldwin 		error = rman_manage_region(&w->rman, w->limit + 1, limit);
1157*c825d4dcSJohn Baldwin 		w->limit = limit;
1158*c825d4dcSJohn Baldwin 	}
1159*c825d4dcSJohn Baldwin 	if (error) {
1160*c825d4dcSJohn Baldwin 		if (bootverbose)
1161*c825d4dcSJohn Baldwin 			device_printf(sc->dev,
1162*c825d4dcSJohn Baldwin 			    "failed to expand %s resource manager\n", w->name);
1163*c825d4dcSJohn Baldwin 		(void)bus_adjust_resource(sc->dev, type, res, force_64k_base ?
1164*c825d4dcSJohn Baldwin 		    rman_get_start(res) : w->base, w->limit);
1165*c825d4dcSJohn Baldwin 	}
1166*c825d4dcSJohn Baldwin 	return (error);
1167*c825d4dcSJohn Baldwin }
1168*c825d4dcSJohn Baldwin 
116983c41143SJohn Baldwin /*
117083c41143SJohn Baldwin  * Attempt to grow a window to make room for a given resource request.
117183c41143SJohn Baldwin  */
117283c41143SJohn Baldwin static int
117383c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type,
117483c41143SJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
117583c41143SJohn Baldwin {
1176a7b5acacSJohn Baldwin 	u_long align, start_free, end_free, front, back, wmask;
1177*c825d4dcSJohn Baldwin 	int error;
117883c41143SJohn Baldwin 
117983c41143SJohn Baldwin 	/*
118083c41143SJohn Baldwin 	 * Clamp the desired resource range to the maximum address
118183c41143SJohn Baldwin 	 * this window supports.  Reject impossible requests.
1182*c825d4dcSJohn Baldwin 	 *
1183*c825d4dcSJohn Baldwin 	 * For I/O port requests behind a bridge with the ISA enable
1184*c825d4dcSJohn Baldwin 	 * bit set, force large allocations to start above 64k.
118583c41143SJohn Baldwin 	 */
118683c41143SJohn Baldwin 	if (!w->valid)
118783c41143SJohn Baldwin 		return (EINVAL);
1188*c825d4dcSJohn Baldwin 	if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 &&
1189*c825d4dcSJohn Baldwin 	    start < 65536)
1190*c825d4dcSJohn Baldwin 		start = 65536;
119183c41143SJohn Baldwin 	if (end > w->rman.rm_end)
119283c41143SJohn Baldwin 		end = w->rman.rm_end;
119383c41143SJohn Baldwin 	if (start + count - 1 > end || start + count < start)
119483c41143SJohn Baldwin 		return (EINVAL);
1195a7b5acacSJohn Baldwin 	wmask = (1ul << w->step) - 1;
119683c41143SJohn Baldwin 
119783c41143SJohn Baldwin 	/*
119883c41143SJohn Baldwin 	 * If there is no resource at all, just try to allocate enough
119983c41143SJohn Baldwin 	 * aligned space for this resource.
120083c41143SJohn Baldwin 	 */
120183c41143SJohn Baldwin 	if (w->res == NULL) {
1202*c825d4dcSJohn Baldwin 		error = pcib_alloc_new_window(sc, w, type, start, end, count,
1203*c825d4dcSJohn Baldwin 		    flags);
1204*c825d4dcSJohn Baldwin 		if (error) {
120583c41143SJohn Baldwin 			if (bootverbose)
120683c41143SJohn Baldwin 				device_printf(sc->dev,
120783c41143SJohn Baldwin 		    "failed to allocate initial %s window (%#lx-%#lx,%#lx)\n",
120883c41143SJohn Baldwin 				    w->name, start, end, count);
120983c41143SJohn Baldwin 			return (error);
121083c41143SJohn Baldwin 		}
1211*c825d4dcSJohn Baldwin 		if (bootverbose)
1212*c825d4dcSJohn Baldwin 			device_printf(sc->dev,
1213*c825d4dcSJohn Baldwin 			    "allocated initial %s window of %#jx-%#jx\n",
1214*c825d4dcSJohn Baldwin 			    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
121583c41143SJohn Baldwin 		goto updatewin;
121683c41143SJohn Baldwin 	}
121783c41143SJohn Baldwin 
121883c41143SJohn Baldwin 	/*
121983c41143SJohn Baldwin 	 * See if growing the window would help.  Compute the minimum
122083c41143SJohn Baldwin 	 * amount of address space needed on both the front and back
122183c41143SJohn Baldwin 	 * ends of the existing window to satisfy the allocation.
122283c41143SJohn Baldwin 	 *
122383c41143SJohn Baldwin 	 * For each end, build a candidate region adjusting for the
122483c41143SJohn Baldwin 	 * required alignment, etc.  If there is a free region at the
122583c41143SJohn Baldwin 	 * edge of the window, grow from the inner edge of the free
122683c41143SJohn Baldwin 	 * region.  Otherwise grow from the window boundary.
122783c41143SJohn Baldwin 	 *
1228*c825d4dcSJohn Baldwin 	 * Growing an I/O window below 64k for a bridge with the ISA
1229*c825d4dcSJohn Baldwin 	 * enable bit doesn't require any special magic as the step
1230*c825d4dcSJohn Baldwin 	 * size of an I/O window (1k) always includes multiple
1231*c825d4dcSJohn Baldwin 	 * non-alias ranges when it is grown in either direction.
1232*c825d4dcSJohn Baldwin 	 *
123383c41143SJohn Baldwin 	 * XXX: Special case: if w->res is completely empty and the
123483c41143SJohn Baldwin 	 * request size is larger than w->res, we should find the
123583c41143SJohn Baldwin 	 * optimal aligned buffer containing w->res and allocate that.
123683c41143SJohn Baldwin 	 */
123783c41143SJohn Baldwin 	if (bootverbose)
123883c41143SJohn Baldwin 		device_printf(sc->dev,
123983c41143SJohn Baldwin 		    "attempting to grow %s window for (%#lx-%#lx,%#lx)\n",
124083c41143SJohn Baldwin 		    w->name, start, end, count);
124183c41143SJohn Baldwin 	align = 1ul << RF_ALIGNMENT(flags);
1242*c825d4dcSJohn Baldwin 	if (start < w->base) {
124383c41143SJohn Baldwin 		if (rman_first_free_region(&w->rman, &start_free, &end_free) !=
1244*c825d4dcSJohn Baldwin 		    0 || start_free != w->base)
1245*c825d4dcSJohn Baldwin 			end_free = w->base;
124683c41143SJohn Baldwin 		if (end_free > end)
1247ddac8cc9SJohn Baldwin 			end_free = end + 1;
124883c41143SJohn Baldwin 
124983c41143SJohn Baldwin 		/* Move end_free down until it is properly aligned. */
125083c41143SJohn Baldwin 		end_free &= ~(align - 1);
1251a49dcb46SJohn Baldwin 		end_free--;
1252a49dcb46SJohn Baldwin 		front = end_free - (count - 1);
125383c41143SJohn Baldwin 
125483c41143SJohn Baldwin 		/*
125583c41143SJohn Baldwin 		 * The resource would now be allocated at (front,
125683c41143SJohn Baldwin 		 * end_free).  Ensure that fits in the (start, end)
125783c41143SJohn Baldwin 		 * bounds.  end_free is checked above.  If 'front' is
125883c41143SJohn Baldwin 		 * ok, ensure it is properly aligned for this window.
125983c41143SJohn Baldwin 		 * Also check for underflow.
126083c41143SJohn Baldwin 		 */
126183c41143SJohn Baldwin 		if (front >= start && front <= end_free) {
126283c41143SJohn Baldwin 			if (bootverbose)
126383c41143SJohn Baldwin 				printf("\tfront candidate range: %#lx-%#lx\n",
126483c41143SJohn Baldwin 				    front, end_free);
1265a7b5acacSJohn Baldwin 			front &= ~wmask;
1266*c825d4dcSJohn Baldwin 			front = w->base - front;
126783c41143SJohn Baldwin 		} else
126883c41143SJohn Baldwin 			front = 0;
126983c41143SJohn Baldwin 	} else
127083c41143SJohn Baldwin 		front = 0;
1271*c825d4dcSJohn Baldwin 	if (end > w->limit) {
127283c41143SJohn Baldwin 		if (rman_last_free_region(&w->rman, &start_free, &end_free) !=
1273*c825d4dcSJohn Baldwin 		    0 || end_free != w->limit)
1274*c825d4dcSJohn Baldwin 			start_free = w->limit + 1;
127583c41143SJohn Baldwin 		if (start_free < start)
127683c41143SJohn Baldwin 			start_free = start;
127783c41143SJohn Baldwin 
127883c41143SJohn Baldwin 		/* Move start_free up until it is properly aligned. */
127983c41143SJohn Baldwin 		start_free = roundup2(start_free, align);
1280a49dcb46SJohn Baldwin 		back = start_free + count - 1;
128183c41143SJohn Baldwin 
128283c41143SJohn Baldwin 		/*
128383c41143SJohn Baldwin 		 * The resource would now be allocated at (start_free,
128483c41143SJohn Baldwin 		 * back).  Ensure that fits in the (start, end)
128583c41143SJohn Baldwin 		 * bounds.  start_free is checked above.  If 'back' is
128683c41143SJohn Baldwin 		 * ok, ensure it is properly aligned for this window.
128783c41143SJohn Baldwin 		 * Also check for overflow.
128883c41143SJohn Baldwin 		 */
128983c41143SJohn Baldwin 		if (back <= end && start_free <= back) {
129083c41143SJohn Baldwin 			if (bootverbose)
129183c41143SJohn Baldwin 				printf("\tback candidate range: %#lx-%#lx\n",
129283c41143SJohn Baldwin 				    start_free, back);
1293a7b5acacSJohn Baldwin 			back |= wmask;
1294*c825d4dcSJohn Baldwin 			back -= w->limit;
129583c41143SJohn Baldwin 		} else
129683c41143SJohn Baldwin 			back = 0;
129783c41143SJohn Baldwin 	} else
129883c41143SJohn Baldwin 		back = 0;
129983c41143SJohn Baldwin 
130083c41143SJohn Baldwin 	/*
130183c41143SJohn Baldwin 	 * Try to allocate the smallest needed region first.
130283c41143SJohn Baldwin 	 * If that fails, fall back to the other region.
130383c41143SJohn Baldwin 	 */
130483c41143SJohn Baldwin 	error = ENOSPC;
130583c41143SJohn Baldwin 	while (front != 0 || back != 0) {
130683c41143SJohn Baldwin 		if (front != 0 && (front <= back || back == 0)) {
1307*c825d4dcSJohn Baldwin 			error = pcib_expand_window(sc, w, type, w->base - front,
1308*c825d4dcSJohn Baldwin 			    w->limit);
130983c41143SJohn Baldwin 			if (error == 0)
131083c41143SJohn Baldwin 				break;
131183c41143SJohn Baldwin 			front = 0;
131283c41143SJohn Baldwin 		} else {
1313*c825d4dcSJohn Baldwin 			error = pcib_expand_window(sc, w, type, w->base,
1314*c825d4dcSJohn Baldwin 			    w->limit + back);
131583c41143SJohn Baldwin 			if (error == 0)
131683c41143SJohn Baldwin 				break;
131783c41143SJohn Baldwin 			back = 0;
131883c41143SJohn Baldwin 		}
131983c41143SJohn Baldwin 	}
132083c41143SJohn Baldwin 
132183c41143SJohn Baldwin 	if (error)
132283c41143SJohn Baldwin 		return (error);
132383c41143SJohn Baldwin 	if (bootverbose)
1324*c825d4dcSJohn Baldwin 		device_printf(sc->dev, "grew %s window to %#jx-%#jx\n",
1325*c825d4dcSJohn Baldwin 		    w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
132683c41143SJohn Baldwin 
132783c41143SJohn Baldwin updatewin:
1328*c825d4dcSJohn Baldwin 	/* Write the new window. */
1329a7b5acacSJohn Baldwin 	KASSERT((w->base & wmask) == 0, ("start address is not aligned"));
1330a7b5acacSJohn Baldwin 	KASSERT((w->limit & wmask) == wmask, ("end address is not aligned"));
133183c41143SJohn Baldwin 	pcib_write_windows(sc, w->mask);
133283c41143SJohn Baldwin 	return (0);
133383c41143SJohn Baldwin }
133483c41143SJohn Baldwin 
133583c41143SJohn Baldwin /*
133683c41143SJohn Baldwin  * We have to trap resource allocation requests and ensure that the bridge
133783c41143SJohn Baldwin  * is set up to, or capable of handling them.
133883c41143SJohn Baldwin  */
133983c41143SJohn Baldwin struct resource *
134083c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
134183c41143SJohn Baldwin     u_long start, u_long end, u_long count, u_int flags)
134283c41143SJohn Baldwin {
134383c41143SJohn Baldwin 	struct pcib_softc *sc;
134483c41143SJohn Baldwin 	struct resource *r;
134583c41143SJohn Baldwin 
134683c41143SJohn Baldwin 	sc = device_get_softc(dev);
134783c41143SJohn Baldwin 
134883c41143SJohn Baldwin 	/*
134983c41143SJohn Baldwin 	 * VGA resources are decoded iff the VGA enable bit is set in
135083c41143SJohn Baldwin 	 * the bridge control register.  VGA resources do not fall into
135183c41143SJohn Baldwin 	 * the resource windows and are passed up to the parent.
135283c41143SJohn Baldwin 	 */
135383c41143SJohn Baldwin 	if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) ||
135483c41143SJohn Baldwin 	    (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) {
135583c41143SJohn Baldwin 		if (sc->bridgectl & PCIB_BCR_VGA_ENABLE)
135683c41143SJohn Baldwin 			return (bus_generic_alloc_resource(dev, child, type,
135783c41143SJohn Baldwin 			    rid, start, end, count, flags));
135883c41143SJohn Baldwin 		else
135983c41143SJohn Baldwin 			return (NULL);
136083c41143SJohn Baldwin 	}
136183c41143SJohn Baldwin 
136283c41143SJohn Baldwin 	switch (type) {
136383c41143SJohn Baldwin 	case SYS_RES_IOPORT:
1364*c825d4dcSJohn Baldwin 		if (pcib_is_isa_range(sc, start, end, count))
1365*c825d4dcSJohn Baldwin 			return (NULL);
136683c41143SJohn Baldwin 		r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start,
136783c41143SJohn Baldwin 		    end, count, flags);
1368a6c82265SMarius Strobl 		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
136983c41143SJohn Baldwin 			break;
137083c41143SJohn Baldwin 		if (pcib_grow_window(sc, &sc->io, type, start, end, count,
137183c41143SJohn Baldwin 		    flags) == 0)
137283c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->io, child, type,
137383c41143SJohn Baldwin 			    rid, start, end, count, flags);
137483c41143SJohn Baldwin 		break;
137583c41143SJohn Baldwin 	case SYS_RES_MEMORY:
137683c41143SJohn Baldwin 		/*
137783c41143SJohn Baldwin 		 * For prefetchable resources, prefer the prefetchable
137883c41143SJohn Baldwin 		 * memory window, but fall back to the regular memory
137983c41143SJohn Baldwin 		 * window if that fails.  Try both windows before
138083c41143SJohn Baldwin 		 * attempting to grow a window in case the firmware
138183c41143SJohn Baldwin 		 * has used a range in the regular memory window to
138283c41143SJohn Baldwin 		 * map a prefetchable BAR.
138383c41143SJohn Baldwin 		 */
138483c41143SJohn Baldwin 		if (flags & RF_PREFETCHABLE) {
138583c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->pmem, child, type,
138683c41143SJohn Baldwin 			    rid, start, end, count, flags);
138783c41143SJohn Baldwin 			if (r != NULL)
138883c41143SJohn Baldwin 				break;
138983c41143SJohn Baldwin 		}
139083c41143SJohn Baldwin 		r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid,
139183c41143SJohn Baldwin 		    start, end, count, flags);
1392a6c82265SMarius Strobl 		if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
139383c41143SJohn Baldwin 			break;
139483c41143SJohn Baldwin 		if (flags & RF_PREFETCHABLE) {
139583c41143SJohn Baldwin 			if (pcib_grow_window(sc, &sc->pmem, type, start, end,
139683c41143SJohn Baldwin 			    count, flags) == 0) {
139783c41143SJohn Baldwin 				r = pcib_suballoc_resource(sc, &sc->pmem, child,
139883c41143SJohn Baldwin 				    type, rid, start, end, count, flags);
139983c41143SJohn Baldwin 				if (r != NULL)
140083c41143SJohn Baldwin 					break;
140183c41143SJohn Baldwin 			}
140283c41143SJohn Baldwin 		}
140383c41143SJohn Baldwin 		if (pcib_grow_window(sc, &sc->mem, type, start, end, count,
140483c41143SJohn Baldwin 		    flags & ~RF_PREFETCHABLE) == 0)
140583c41143SJohn Baldwin 			r = pcib_suballoc_resource(sc, &sc->mem, child, type,
140683c41143SJohn Baldwin 			    rid, start, end, count, flags);
140783c41143SJohn Baldwin 		break;
140883c41143SJohn Baldwin 	default:
140983c41143SJohn Baldwin 		return (bus_generic_alloc_resource(dev, child, type, rid,
141083c41143SJohn Baldwin 		    start, end, count, flags));
141183c41143SJohn Baldwin 	}
141283c41143SJohn Baldwin 
141383c41143SJohn Baldwin 	/*
141483c41143SJohn Baldwin 	 * If attempts to suballocate from the window fail but this is a
141583c41143SJohn Baldwin 	 * subtractive bridge, pass the request up the tree.
141683c41143SJohn Baldwin 	 */
141783c41143SJohn Baldwin 	if (sc->flags & PCIB_SUBTRACTIVE && r == NULL)
141883c41143SJohn Baldwin 		return (bus_generic_alloc_resource(dev, child, type, rid,
141983c41143SJohn Baldwin 		    start, end, count, flags));
142083c41143SJohn Baldwin 	return (r);
142183c41143SJohn Baldwin }
142283c41143SJohn Baldwin 
142383c41143SJohn Baldwin int
142483c41143SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
142583c41143SJohn Baldwin     u_long start, u_long end)
142683c41143SJohn Baldwin {
142783c41143SJohn Baldwin 	struct pcib_softc *sc;
142883c41143SJohn Baldwin 
142983c41143SJohn Baldwin 	sc = device_get_softc(bus);
143083c41143SJohn Baldwin 	if (pcib_is_resource_managed(sc, type, r))
143183c41143SJohn Baldwin 		return (rman_adjust_resource(r, start, end));
143283c41143SJohn Baldwin 	return (bus_generic_adjust_resource(bus, child, type, r, start, end));
143383c41143SJohn Baldwin }
143483c41143SJohn Baldwin 
143583c41143SJohn Baldwin int
143683c41143SJohn Baldwin pcib_release_resource(device_t dev, device_t child, int type, int rid,
143783c41143SJohn Baldwin     struct resource *r)
143883c41143SJohn Baldwin {
143983c41143SJohn Baldwin 	struct pcib_softc *sc;
144083c41143SJohn Baldwin 	int error;
144183c41143SJohn Baldwin 
144283c41143SJohn Baldwin 	sc = device_get_softc(dev);
144383c41143SJohn Baldwin 	if (pcib_is_resource_managed(sc, type, r)) {
144483c41143SJohn Baldwin 		if (rman_get_flags(r) & RF_ACTIVE) {
144583c41143SJohn Baldwin 			error = bus_deactivate_resource(child, type, rid, r);
144683c41143SJohn Baldwin 			if (error)
144783c41143SJohn Baldwin 				return (error);
144883c41143SJohn Baldwin 		}
144983c41143SJohn Baldwin 		return (rman_release_resource(r));
145083c41143SJohn Baldwin 	}
145183c41143SJohn Baldwin 	return (bus_generic_release_resource(dev, child, type, rid, r));
145283c41143SJohn Baldwin }
145383c41143SJohn Baldwin #else
1454bb0d0a8eSMike Smith /*
1455bb0d0a8eSMike Smith  * We have to trap resource allocation requests and ensure that the bridge
1456bb0d0a8eSMike Smith  * is set up to, or capable of handling them.
1457bb0d0a8eSMike Smith  */
14586f0d5884SJohn Baldwin struct resource *
1459bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
1460bb0d0a8eSMike Smith     u_long start, u_long end, u_long count, u_int flags)
1461bb0d0a8eSMike Smith {
1462bb0d0a8eSMike Smith 	struct pcib_softc	*sc = device_get_softc(dev);
146326043836SJohn Baldwin 	const char *name, *suffix;
1464a8b354a8SWarner Losh 	int ok;
1465bb0d0a8eSMike Smith 
1466bb0d0a8eSMike Smith 	/*
1467bb0d0a8eSMike Smith 	 * Fail the allocation for this range if it's not supported.
1468bb0d0a8eSMike Smith 	 */
146926043836SJohn Baldwin 	name = device_get_nameunit(child);
147026043836SJohn Baldwin 	if (name == NULL) {
147126043836SJohn Baldwin 		name = "";
147226043836SJohn Baldwin 		suffix = "";
147326043836SJohn Baldwin 	} else
147426043836SJohn Baldwin 		suffix = " ";
1475bb0d0a8eSMike Smith 	switch (type) {
1476bb0d0a8eSMike Smith 	case SYS_RES_IOPORT:
1477a8b354a8SWarner Losh 		ok = 0;
1478e4b59fc5SWarner Losh 		if (!pcib_is_io_open(sc))
1479e4b59fc5SWarner Losh 			break;
1480a8b354a8SWarner Losh 		ok = (start >= sc->iobase && end <= sc->iolimit);
1481d98d9b12SMarcel Moolenaar 
1482d98d9b12SMarcel Moolenaar 		/*
1483d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA I/O addresses when the
1484d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
1485d98d9b12SMarcel Moolenaar 		 */
1486d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_ioport_range(start, end))
1487d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1488d98d9b12SMarcel Moolenaar 
1489e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1490a8b354a8SWarner Losh 			if (!ok) {
149112b8c86eSWarner Losh 				if (start < sc->iobase)
149212b8c86eSWarner Losh 					start = sc->iobase;
149312b8c86eSWarner Losh 				if (end > sc->iolimit)
149412b8c86eSWarner Losh 					end = sc->iolimit;
14952daa7a07SWarner Losh 				if (start < end)
14962daa7a07SWarner Losh 					ok = 1;
1497a8b354a8SWarner Losh 			}
14981c54ff33SMatthew N. Dodd 		} else {
1499e4b59fc5SWarner Losh 			ok = 1;
15009dffe835SWarner Losh #if 0
1501795dceffSWarner Losh 			/*
1502795dceffSWarner Losh 			 * If we overlap with the subtractive range, then
1503795dceffSWarner Losh 			 * pick the upper range to use.
1504795dceffSWarner Losh 			 */
1505795dceffSWarner Losh 			if (start < sc->iolimit && end > sc->iobase)
1506795dceffSWarner Losh 				start = sc->iolimit + 1;
15079dffe835SWarner Losh #endif
150812b8c86eSWarner Losh 		}
1509a8b354a8SWarner Losh 		if (end < start) {
15102daa7a07SWarner Losh 			device_printf(dev, "ioport: end (%lx) < start (%lx)\n",
15112daa7a07SWarner Losh 			    end, start);
1512a8b354a8SWarner Losh 			start = 0;
1513a8b354a8SWarner Losh 			end = 0;
1514a8b354a8SWarner Losh 			ok = 0;
1515a8b354a8SWarner Losh 		}
1516a8b354a8SWarner Losh 		if (!ok) {
151726043836SJohn Baldwin 			device_printf(dev, "%s%srequested unsupported I/O "
1518a8b354a8SWarner Losh 			    "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
151926043836SJohn Baldwin 			    name, suffix, start, end, sc->iobase, sc->iolimit);
1520bb0d0a8eSMike Smith 			return (NULL);
1521bb0d0a8eSMike Smith 		}
15224fa59183SMike Smith 		if (bootverbose)
15232daa7a07SWarner Losh 			device_printf(dev,
152426043836SJohn Baldwin 			    "%s%srequested I/O range 0x%lx-0x%lx: in range\n",
152526043836SJohn Baldwin 			    name, suffix, start, end);
1526bb0d0a8eSMike Smith 		break;
1527bb0d0a8eSMike Smith 
1528bb0d0a8eSMike Smith 	case SYS_RES_MEMORY:
1529a8b354a8SWarner Losh 		ok = 0;
1530a8b354a8SWarner Losh 		if (pcib_is_nonprefetch_open(sc))
1531a8b354a8SWarner Losh 			ok = ok || (start >= sc->membase && end <= sc->memlimit);
1532a8b354a8SWarner Losh 		if (pcib_is_prefetch_open(sc))
1533a8b354a8SWarner Losh 			ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
1534d98d9b12SMarcel Moolenaar 
1535d98d9b12SMarcel Moolenaar 		/*
1536d98d9b12SMarcel Moolenaar 		 * Make sure we allow access to VGA memory addresses when the
1537d98d9b12SMarcel Moolenaar 		 * bridge has the "VGA Enable" bit set.
1538d98d9b12SMarcel Moolenaar 		 */
1539d98d9b12SMarcel Moolenaar 		if (!ok && pci_is_vga_memory_range(start, end))
1540d98d9b12SMarcel Moolenaar 			ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
1541d98d9b12SMarcel Moolenaar 
1542e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
1543a8b354a8SWarner Losh 			if (!ok) {
1544a8b354a8SWarner Losh 				ok = 1;
1545a8b354a8SWarner Losh 				if (flags & RF_PREFETCHABLE) {
1546a8b354a8SWarner Losh 					if (pcib_is_prefetch_open(sc)) {
1547a8b354a8SWarner Losh 						if (start < sc->pmembase)
1548a8b354a8SWarner Losh 							start = sc->pmembase;
1549a8b354a8SWarner Losh 						if (end > sc->pmemlimit)
1550a8b354a8SWarner Losh 							end = sc->pmemlimit;
1551a8b354a8SWarner Losh 					} else {
1552a8b354a8SWarner Losh 						ok = 0;
1553a8b354a8SWarner Losh 					}
1554a8b354a8SWarner Losh 				} else {	/* non-prefetchable */
1555a8b354a8SWarner Losh 					if (pcib_is_nonprefetch_open(sc)) {
1556a8b354a8SWarner Losh 						if (start < sc->membase)
155712b8c86eSWarner Losh 							start = sc->membase;
155812b8c86eSWarner Losh 						if (end > sc->memlimit)
155912b8c86eSWarner Losh 							end = sc->memlimit;
15601c54ff33SMatthew N. Dodd 					} else {
1561a8b354a8SWarner Losh 						ok = 0;
1562a8b354a8SWarner Losh 					}
1563a8b354a8SWarner Losh 				}
1564a8b354a8SWarner Losh 			}
1565a8b354a8SWarner Losh 		} else if (!ok) {
1566e4b59fc5SWarner Losh 			ok = 1;	/* subtractive bridge: always ok */
15679dffe835SWarner Losh #if 0
1568a8b354a8SWarner Losh 			if (pcib_is_nonprefetch_open(sc)) {
1569795dceffSWarner Losh 				if (start < sc->memlimit && end > sc->membase)
1570795dceffSWarner Losh 					start = sc->memlimit + 1;
1571a8b354a8SWarner Losh 			}
1572a8b354a8SWarner Losh 			if (pcib_is_prefetch_open(sc)) {
1573795dceffSWarner Losh 				if (start < sc->pmemlimit && end > sc->pmembase)
1574795dceffSWarner Losh 					start = sc->pmemlimit + 1;
15751c54ff33SMatthew N. Dodd 			}
15769dffe835SWarner Losh #endif
157712b8c86eSWarner Losh 		}
1578a8b354a8SWarner Losh 		if (end < start) {
15792daa7a07SWarner Losh 			device_printf(dev, "memory: end (%lx) < start (%lx)\n",
15802daa7a07SWarner Losh 			    end, start);
1581a8b354a8SWarner Losh 			start = 0;
1582a8b354a8SWarner Losh 			end = 0;
1583a8b354a8SWarner Losh 			ok = 0;
1584a8b354a8SWarner Losh 		}
1585a8b354a8SWarner Losh 		if (!ok && bootverbose)
158634428485SWarner Losh 			device_printf(dev,
158726043836SJohn Baldwin 			    "%s%srequested unsupported memory range %#lx-%#lx "
1588b0a2d4b8SWarner Losh 			    "(decoding %#jx-%#jx, %#jx-%#jx)\n",
158926043836SJohn Baldwin 			    name, suffix, start, end,
1590b0a2d4b8SWarner Losh 			    (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
1591b0a2d4b8SWarner Losh 			    (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
1592a8b354a8SWarner Losh 		if (!ok)
1593bb0d0a8eSMike Smith 			return (NULL);
15944fa59183SMike Smith 		if (bootverbose)
159526043836SJohn Baldwin 			device_printf(dev,"%s%srequested memory range "
15962daa7a07SWarner Losh 			    "0x%lx-0x%lx: good\n",
159726043836SJohn Baldwin 			    name, suffix, start, end);
15984fa59183SMike Smith 		break;
15994fa59183SMike Smith 
1600bb0d0a8eSMike Smith 	default:
16014fa59183SMike Smith 		break;
1602bb0d0a8eSMike Smith 	}
1603bb0d0a8eSMike Smith 	/*
1604bb0d0a8eSMike Smith 	 * Bridge is OK decoding this resource, so pass it up.
1605bb0d0a8eSMike Smith 	 */
16062daa7a07SWarner Losh 	return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
16072daa7a07SWarner Losh 	    count, flags));
1608bb0d0a8eSMike Smith }
160983c41143SJohn Baldwin #endif
1610bb0d0a8eSMike Smith 
1611bb0d0a8eSMike Smith /*
1612bb0d0a8eSMike Smith  * PCIB interface.
1613bb0d0a8eSMike Smith  */
16146f0d5884SJohn Baldwin int
1615bb0d0a8eSMike Smith pcib_maxslots(device_t dev)
1616bb0d0a8eSMike Smith {
16174fa59183SMike Smith     return(PCI_SLOTMAX);
1618bb0d0a8eSMike Smith }
1619bb0d0a8eSMike Smith 
1620bb0d0a8eSMike Smith /*
1621bb0d0a8eSMike Smith  * Since we are a child of a PCI bus, its parent must support the pcib interface.
1622bb0d0a8eSMike Smith  */
1623b0cb115fSWarner Losh uint32_t
1624795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
1625bb0d0a8eSMike Smith {
1626bb0d0a8eSMike Smith     return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width));
1627bb0d0a8eSMike Smith }
1628bb0d0a8eSMike Smith 
16296f0d5884SJohn Baldwin void
1630795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
1631bb0d0a8eSMike Smith {
1632bb0d0a8eSMike Smith     PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width);
1633bb0d0a8eSMike Smith }
1634bb0d0a8eSMike Smith 
1635bb0d0a8eSMike Smith /*
1636bb0d0a8eSMike Smith  * Route an interrupt across a PCI bridge.
1637bb0d0a8eSMike Smith  */
16382c2d1d07SBenno Rice int
1639bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin)
1640bb0d0a8eSMike Smith {
1641bb0d0a8eSMike Smith     device_t	bus;
1642bb0d0a8eSMike Smith     int		parent_intpin;
1643bb0d0a8eSMike Smith     int		intnum;
1644bb0d0a8eSMike Smith 
1645bb0d0a8eSMike Smith     /*
1646bb0d0a8eSMike Smith      *
1647bb0d0a8eSMike Smith      * The PCI standard defines a swizzle of the child-side device/intpin to
1648bb0d0a8eSMike Smith      * the parent-side intpin as follows.
1649bb0d0a8eSMike Smith      *
1650bb0d0a8eSMike Smith      * device = device on child bus
1651bb0d0a8eSMike Smith      * child_intpin = intpin on child bus slot (0-3)
1652bb0d0a8eSMike Smith      * parent_intpin = intpin on parent bus slot (0-3)
1653bb0d0a8eSMike Smith      *
1654bb0d0a8eSMike Smith      * parent_intpin = (device + child_intpin) % 4
1655bb0d0a8eSMike Smith      */
1656cdc95e1bSBernd Walter     parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
1657bb0d0a8eSMike Smith 
1658bb0d0a8eSMike Smith     /*
1659bb0d0a8eSMike Smith      * Our parent is a PCI bus.  Its parent must export the pcib interface
1660bb0d0a8eSMike Smith      * which includes the ability to route interrupts.
1661bb0d0a8eSMike Smith      */
1662bb0d0a8eSMike Smith     bus = device_get_parent(pcib);
1663bb0d0a8eSMike Smith     intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
166439981fedSJohn Baldwin     if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
1665c6a121abSJohn Baldwin 	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
1666c6a121abSJohn Baldwin 	    pci_get_slot(dev), 'A' + pin - 1, intnum);
16678046c4b9SMike Smith     }
1668bb0d0a8eSMike Smith     return(intnum);
1669bb0d0a8eSMike Smith }
1670b173edafSJohn Baldwin 
1671e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
16729bf4c9c1SJohn Baldwin int
16739bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
16749bf4c9c1SJohn Baldwin {
1675bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
16769bf4c9c1SJohn Baldwin 	device_t bus;
16779bf4c9c1SJohn Baldwin 
167822bf1c7fSJohn Baldwin 	if (sc->flags & PCIB_DISABLE_MSI)
167922bf1c7fSJohn Baldwin 		return (ENXIO);
16809bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
16819bf4c9c1SJohn Baldwin 	return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
16829bf4c9c1SJohn Baldwin 	    irqs));
16839bf4c9c1SJohn Baldwin }
16849bf4c9c1SJohn Baldwin 
1685e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */
16869bf4c9c1SJohn Baldwin int
16879bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
16889bf4c9c1SJohn Baldwin {
16899bf4c9c1SJohn Baldwin 	device_t bus;
16909bf4c9c1SJohn Baldwin 
16919bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
16929bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
16939bf4c9c1SJohn Baldwin }
16949bf4c9c1SJohn Baldwin 
16959bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */
16969bf4c9c1SJohn Baldwin int
1697e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
16989bf4c9c1SJohn Baldwin {
1699bd82bbb1SAndrew Gallatin 	struct pcib_softc *sc = device_get_softc(pcib);
17009bf4c9c1SJohn Baldwin 	device_t bus;
17019bf4c9c1SJohn Baldwin 
170268e9cbd3SMarius Strobl 	if (sc->flags & PCIB_DISABLE_MSIX)
170322bf1c7fSJohn Baldwin 		return (ENXIO);
17049bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
1705e706f7f0SJohn Baldwin 	return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
17065fe82bcaSJohn Baldwin }
17075fe82bcaSJohn Baldwin 
17089bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */
17099bf4c9c1SJohn Baldwin int
17109bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq)
17119bf4c9c1SJohn Baldwin {
17129bf4c9c1SJohn Baldwin 	device_t bus;
17139bf4c9c1SJohn Baldwin 
17149bf4c9c1SJohn Baldwin 	bus = device_get_parent(pcib);
17159bf4c9c1SJohn Baldwin 	return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
17169bf4c9c1SJohn Baldwin }
17179bf4c9c1SJohn Baldwin 
1718e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */
1719e706f7f0SJohn Baldwin int
1720e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
1721e706f7f0SJohn Baldwin     uint32_t *data)
1722e706f7f0SJohn Baldwin {
1723e706f7f0SJohn Baldwin 	device_t bus;
17244522ac77SLuoqi Chen 	int error;
1725e706f7f0SJohn Baldwin 
1726e706f7f0SJohn Baldwin 	bus = device_get_parent(pcib);
17274522ac77SLuoqi Chen 	error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
17284522ac77SLuoqi Chen 	if (error)
17294522ac77SLuoqi Chen 		return (error);
17304522ac77SLuoqi Chen 
17314522ac77SLuoqi Chen 	pci_ht_map_msi(pcib, *addr);
17324522ac77SLuoqi Chen 	return (0);
1733e706f7f0SJohn Baldwin }
1734e706f7f0SJohn Baldwin 
173562508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */
173662508c53SJohn Baldwin int
173762508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
173862508c53SJohn Baldwin {
173962508c53SJohn Baldwin 	device_t bus;
174062508c53SJohn Baldwin 
174162508c53SJohn Baldwin 	bus = device_get_parent(pcib);
174262508c53SJohn Baldwin 	return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate));
174362508c53SJohn Baldwin }
1744