xref: /freebsd/sys/dev/pci/pci_pci.c (revision e4b59fc50065e183c020898b461f49b7ba1483bd)
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/systm.h>
40bb0d0a8eSMike Smith #include <sys/kernel.h>
41bb0d0a8eSMike Smith #include <sys/bus.h>
42a8b354a8SWarner Losh #include <machine/bus.h>
43a8b354a8SWarner Losh #include <sys/rman.h>
441c54ff33SMatthew N. Dodd #include <sys/sysctl.h>
45bb0d0a8eSMike Smith 
46bb0d0a8eSMike Smith #include <machine/resource.h>
47bb0d0a8eSMike Smith 
4838d8c994SWarner Losh #include <dev/pci/pcivar.h>
4938d8c994SWarner Losh #include <dev/pci/pcireg.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);
55bb0d0a8eSMike Smith 
56bb0d0a8eSMike Smith static device_method_t pcib_methods[] = {
57bb0d0a8eSMike Smith     /* Device interface */
58bb0d0a8eSMike Smith     DEVMETHOD(device_probe,		pcib_probe),
59bb0d0a8eSMike Smith     DEVMETHOD(device_attach,		pcib_attach),
60bb0d0a8eSMike Smith     DEVMETHOD(device_shutdown,		bus_generic_shutdown),
61bb0d0a8eSMike Smith     DEVMETHOD(device_suspend,		bus_generic_suspend),
62bb0d0a8eSMike Smith     DEVMETHOD(device_resume,		bus_generic_resume),
63bb0d0a8eSMike Smith 
64bb0d0a8eSMike Smith     /* Bus interface */
65bb0d0a8eSMike Smith     DEVMETHOD(bus_print_child,		bus_generic_print_child),
66bb0d0a8eSMike Smith     DEVMETHOD(bus_read_ivar,		pcib_read_ivar),
67bb0d0a8eSMike Smith     DEVMETHOD(bus_write_ivar,		pcib_write_ivar),
68bb0d0a8eSMike Smith     DEVMETHOD(bus_alloc_resource,	pcib_alloc_resource),
69bb0d0a8eSMike Smith     DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
70bb0d0a8eSMike Smith     DEVMETHOD(bus_activate_resource,	bus_generic_activate_resource),
71bb0d0a8eSMike Smith     DEVMETHOD(bus_deactivate_resource,	bus_generic_deactivate_resource),
72bb0d0a8eSMike Smith     DEVMETHOD(bus_setup_intr,		bus_generic_setup_intr),
73bb0d0a8eSMike Smith     DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
74bb0d0a8eSMike Smith 
75bb0d0a8eSMike Smith     /* pcib interface */
76bb0d0a8eSMike Smith     DEVMETHOD(pcib_maxslots,		pcib_maxslots),
77bb0d0a8eSMike Smith     DEVMETHOD(pcib_read_config,		pcib_read_config),
78bb0d0a8eSMike Smith     DEVMETHOD(pcib_write_config,	pcib_write_config),
79bb0d0a8eSMike Smith     DEVMETHOD(pcib_route_interrupt,	pcib_route_interrupt),
80bb0d0a8eSMike Smith 
81bb0d0a8eSMike Smith     { 0, 0 }
82bb0d0a8eSMike Smith };
83bb0d0a8eSMike Smith 
84bb0d0a8eSMike Smith static driver_t pcib_driver = {
85bb0d0a8eSMike Smith     "pcib",
86bb0d0a8eSMike Smith     pcib_methods,
87bb0d0a8eSMike Smith     sizeof(struct pcib_softc),
88bb0d0a8eSMike Smith };
89bb0d0a8eSMike Smith 
906f0d5884SJohn Baldwin devclass_t pcib_devclass;
91bb0d0a8eSMike Smith 
92bb0d0a8eSMike Smith DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, 0, 0);
93bb0d0a8eSMike Smith 
94bb0d0a8eSMike Smith /*
95bb0d0a8eSMike Smith  * Generic device interface
96bb0d0a8eSMike Smith  */
97bb0d0a8eSMike Smith static int
98bb0d0a8eSMike Smith pcib_probe(device_t dev)
99bb0d0a8eSMike Smith {
100bb0d0a8eSMike Smith     if ((pci_get_class(dev) == PCIC_BRIDGE) &&
101bb0d0a8eSMike Smith 	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
102bb0d0a8eSMike Smith 	device_set_desc(dev, "PCI-PCI bridge");
103bb0d0a8eSMike Smith 	return(-10000);
104bb0d0a8eSMike Smith     }
105bb0d0a8eSMike Smith     return(ENXIO);
106bb0d0a8eSMike Smith }
107bb0d0a8eSMike Smith 
1086f0d5884SJohn Baldwin void
1096f0d5884SJohn Baldwin pcib_attach_common(device_t dev)
110bb0d0a8eSMike Smith {
111bb0d0a8eSMike Smith     struct pcib_softc	*sc;
112b0cb115fSWarner Losh     uint8_t		iolow;
113bb0d0a8eSMike Smith 
114bb0d0a8eSMike Smith     sc = device_get_softc(dev);
115bb0d0a8eSMike Smith     sc->dev = dev;
116bb0d0a8eSMike Smith 
1174fa59183SMike Smith     /*
1184fa59183SMike Smith      * Get current bridge configuration.
1194fa59183SMike Smith      */
1208983cfbfSMike Smith     sc->command   = pci_read_config(dev, PCIR_COMMAND, 1);
1214fa59183SMike Smith     sc->secbus    = pci_read_config(dev, PCIR_SECBUS_1, 1);
1224fa59183SMike Smith     sc->subbus    = pci_read_config(dev, PCIR_SUBBUS_1, 1);
1234fa59183SMike Smith     sc->secstat   = pci_read_config(dev, PCIR_SECSTAT_1, 2);
1244fa59183SMike Smith     sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
1254fa59183SMike Smith     sc->seclat    = pci_read_config(dev, PCIR_SECLAT_1, 1);
1264fa59183SMike Smith 
1274fa59183SMike Smith     /*
1284fa59183SMike Smith      * Determine current I/O decode.
1294fa59183SMike Smith      */
1308983cfbfSMike Smith     if (sc->command & PCIM_CMD_PORTEN) {
1314fa59183SMike Smith 	iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
1324fa59183SMike Smith 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
1334fa59183SMike Smith 	    sc->iobase = PCI_PPBIOBASE(pci_read_config(dev, PCIR_IOBASEH_1, 2),
1344fa59183SMike Smith 				       pci_read_config(dev, PCIR_IOBASEL_1, 1));
1354fa59183SMike Smith 	} else {
1364fa59183SMike Smith 	    sc->iobase = PCI_PPBIOBASE(0, pci_read_config(dev, PCIR_IOBASEL_1, 1));
1374fa59183SMike Smith 	}
1384fa59183SMike Smith 
1394fa59183SMike Smith 	iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
1404fa59183SMike Smith 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
1414fa59183SMike Smith 	    sc->iolimit = PCI_PPBIOLIMIT(pci_read_config(dev, PCIR_IOLIMITH_1, 2),
1424fa59183SMike Smith 					 pci_read_config(dev, PCIR_IOLIMITL_1, 1));
1434fa59183SMike Smith 	} else {
1444fa59183SMike Smith 	    sc->iolimit = PCI_PPBIOLIMIT(0, pci_read_config(dev, PCIR_IOLIMITL_1, 1));
1454fa59183SMike Smith 	}
1468983cfbfSMike Smith     }
1474fa59183SMike Smith 
1484fa59183SMike Smith     /*
1494fa59183SMike Smith      * Determine current memory decode.
1504fa59183SMike Smith      */
1518983cfbfSMike Smith     if (sc->command & PCIM_CMD_MEMEN) {
1524fa59183SMike Smith 	sc->membase   = PCI_PPBMEMBASE(0, pci_read_config(dev, PCIR_MEMBASE_1, 2));
1534fa59183SMike Smith 	sc->memlimit  = PCI_PPBMEMLIMIT(0, pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
1544fa59183SMike Smith 	sc->pmembase  = PCI_PPBMEMBASE((pci_addr_t)pci_read_config(dev, PCIR_PMBASEH_1, 4),
1554fa59183SMike Smith 				       pci_read_config(dev, PCIR_PMBASEL_1, 2));
1564fa59183SMike Smith 	sc->pmemlimit = PCI_PPBMEMLIMIT((pci_addr_t)pci_read_config(dev, PCIR_PMLIMITH_1, 4),
1574fa59183SMike Smith 					pci_read_config(dev, PCIR_PMLIMITL_1, 2));
1588983cfbfSMike Smith     }
1594fa59183SMike Smith 
1604fa59183SMike Smith     /*
1614fa59183SMike Smith      * Quirk handling.
1624fa59183SMike Smith      */
1634fa59183SMike Smith     switch (pci_get_devid(dev)) {
1644fa59183SMike Smith     case 0x12258086:		/* Intel 82454KX/GX (Orion) */
1654fa59183SMike Smith 	{
166b0cb115fSWarner Losh 	    uint8_t	supbus;
1674fa59183SMike Smith 
1684fa59183SMike Smith 	    supbus = pci_read_config(dev, 0x41, 1);
1694fa59183SMike Smith 	    if (supbus != 0xff) {
1704fa59183SMike Smith 		sc->secbus = supbus + 1;
1714fa59183SMike Smith 		sc->subbus = supbus + 1;
1724fa59183SMike Smith 	    }
1734fa59183SMike Smith 	    break;
1744fa59183SMike Smith 	}
1754fa59183SMike Smith 
176e4b59fc5SWarner Losh     /*
177e4b59fc5SWarner Losh      * The i82380FB mobile docking controller is a PCI-PCI bridge,
178e4b59fc5SWarner Losh      * and it is a subtractive bridge.  However, the ProgIf is wrong
179e4b59fc5SWarner Losh      * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
180e4b59fc5SWarner Losh      * happen.  There's also a Toshiba bridge that behaves this
181e4b59fc5SWarner Losh      * way.
182e4b59fc5SWarner Losh      */
183e4b59fc5SWarner Losh     case 0x124b8086:		/* Intel 82380FB Mobile */
184e4b59fc5SWarner Losh     case 0x060513d7:		/* Toshiba ???? */
185e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
186e4b59fc5SWarner Losh 	break;
187e4b59fc5SWarner Losh     }
188e4b59fc5SWarner Losh 
189e4b59fc5SWarner Losh     /*
190e4b59fc5SWarner Losh      * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
191e4b59fc5SWarner Losh      * but have a ProgIF of 0x80.  The 82801 family (AA, AB, BAM/CAM,
192e4b59fc5SWarner Losh      * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
193e4b59fc5SWarner Losh      * This means they act as if they were subtractively decoding
194e4b59fc5SWarner Losh      * bridges and pass all transactions.  Mark them and real ProgIf 1
195e4b59fc5SWarner Losh      * parts as subtractive.
196e4b59fc5SWarner Losh      */
197e4b59fc5SWarner Losh     if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
198e4b59fc5SWarner Losh       pci_read_config(dev, PCIR_PROGIF, 1) == 1)
199e4b59fc5SWarner Losh 	sc->flags |= PCIB_SUBTRACTIVE;
200e4b59fc5SWarner Losh 
201bb0d0a8eSMike Smith     if (bootverbose) {
202bb0d0a8eSMike Smith 	device_printf(dev, "  secondary bus     %d\n", sc->secbus);
203bb0d0a8eSMike Smith 	device_printf(dev, "  subordinate bus   %d\n", sc->subbus);
204bb0d0a8eSMike Smith 	device_printf(dev, "  I/O decode        0x%x-0x%x\n", sc->iobase, sc->iolimit);
205bb0d0a8eSMike Smith 	device_printf(dev, "  memory decode     0x%x-0x%x\n", sc->membase, sc->memlimit);
206bb0d0a8eSMike Smith 	device_printf(dev, "  prefetched decode 0x%x-0x%x\n", sc->pmembase, sc->pmemlimit);
207e4b59fc5SWarner Losh 	if (sc->flags & PCIB_SUBTRACTIVE)
208e4b59fc5SWarner Losh 	    device_printf(dev, "  Subtractively decoded bridge.\n");
209bb0d0a8eSMike Smith     }
210bb0d0a8eSMike Smith 
211bb0d0a8eSMike Smith     /*
212bb0d0a8eSMike Smith      * XXX If the secondary bus number is zero, we should assign a bus number
213bb0d0a8eSMike Smith      *     since the BIOS hasn't, then initialise the bridge.
214bb0d0a8eSMike Smith      */
215bb0d0a8eSMike Smith 
216bb0d0a8eSMike Smith     /*
217bb0d0a8eSMike Smith      * XXX If the subordinate bus number is less than the secondary bus number,
218bb0d0a8eSMike Smith      *     we should pick a better value.  One sensible alternative would be to
219bb0d0a8eSMike Smith      *     pick 255; the only tradeoff here is that configuration transactions
220bb0d0a8eSMike Smith      *     would be more widely routed than absolutely necessary.
221bb0d0a8eSMike Smith      */
2226f0d5884SJohn Baldwin }
223bb0d0a8eSMike Smith 
22438906aedSJohn Baldwin int
2256f0d5884SJohn Baldwin pcib_attach(device_t dev)
2266f0d5884SJohn Baldwin {
2276f0d5884SJohn Baldwin     struct pcib_softc	*sc;
2286f0d5884SJohn Baldwin     device_t		child;
2296f0d5884SJohn Baldwin 
2306f0d5884SJohn Baldwin     pcib_attach_common(dev);
2316f0d5884SJohn Baldwin     sc = device_get_softc(dev);
232bb0d0a8eSMike Smith     if (sc->secbus != 0) {
233cea0a895SJohn Baldwin 	child = device_add_child(dev, "pci", sc->secbus);
234bb0d0a8eSMike Smith 	if (child != NULL)
235bb0d0a8eSMike Smith 	    return(bus_generic_attach(dev));
236bb0d0a8eSMike Smith     }
237bb0d0a8eSMike Smith 
238bb0d0a8eSMike Smith     /* no secondary bus; we should have fixed this */
239bb0d0a8eSMike Smith     return(0);
240bb0d0a8eSMike Smith }
241bb0d0a8eSMike Smith 
2426f0d5884SJohn Baldwin int
243bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
244bb0d0a8eSMike Smith {
245bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
246bb0d0a8eSMike Smith 
247bb0d0a8eSMike Smith     switch (which) {
248bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
249bb0d0a8eSMike Smith 	*result = sc->secbus;
250bb0d0a8eSMike Smith 	return(0);
251bb0d0a8eSMike Smith     }
252bb0d0a8eSMike Smith     return(ENOENT);
253bb0d0a8eSMike Smith }
254bb0d0a8eSMike Smith 
2556f0d5884SJohn Baldwin int
256bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
257bb0d0a8eSMike Smith {
258bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
259bb0d0a8eSMike Smith 
260bb0d0a8eSMike Smith     switch (which) {
261bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
262bb0d0a8eSMike Smith 	sc->secbus = value;
263bb0d0a8eSMike Smith 	break;
264bb0d0a8eSMike Smith     }
265bb0d0a8eSMike Smith     return(ENOENT);
266bb0d0a8eSMike Smith }
267bb0d0a8eSMike Smith 
268bb0d0a8eSMike Smith /*
269a8b354a8SWarner Losh  * Is the prefetch window open (eg, can we allocate memory in it?)
270a8b354a8SWarner Losh  */
271a8b354a8SWarner Losh static int
272a8b354a8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc)
273a8b354a8SWarner Losh {
274a8b354a8SWarner Losh 	return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
275a8b354a8SWarner Losh }
276a8b354a8SWarner Losh 
277a8b354a8SWarner Losh /*
278a8b354a8SWarner Losh  * Is the nonprefetch window open (eg, can we allocate memory in it?)
279a8b354a8SWarner Losh  */
280a8b354a8SWarner Losh static int
281a8b354a8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc)
282a8b354a8SWarner Losh {
283a8b354a8SWarner Losh 	return (sc->membase > 0 && sc->membase < sc->memlimit);
284a8b354a8SWarner Losh }
285a8b354a8SWarner Losh 
286a8b354a8SWarner Losh /*
287a8b354a8SWarner Losh  * Is the io window open (eg, can we allocate ports in it?)
288a8b354a8SWarner Losh  */
289a8b354a8SWarner Losh static int
290a8b354a8SWarner Losh pcib_is_io_open(struct pcib_softc *sc)
291a8b354a8SWarner Losh {
292a8b354a8SWarner Losh 	return (sc->iobase > 0 && sc->iobase < sc->iolimit);
293a8b354a8SWarner Losh }
294a8b354a8SWarner Losh 
295a8b354a8SWarner Losh /*
296bb0d0a8eSMike Smith  * We have to trap resource allocation requests and ensure that the bridge
297bb0d0a8eSMike Smith  * is set up to, or capable of handling them.
298bb0d0a8eSMike Smith  */
2996f0d5884SJohn Baldwin struct resource *
300bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
301bb0d0a8eSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
302bb0d0a8eSMike Smith {
303bb0d0a8eSMike Smith 	struct pcib_softc	*sc = device_get_softc(dev);
304a8b354a8SWarner Losh 	int ok;
305bb0d0a8eSMike Smith 
306bb0d0a8eSMike Smith 	/*
307bb0d0a8eSMike Smith 	 * Fail the allocation for this range if it's not supported.
308bb0d0a8eSMike Smith 	 */
309bb0d0a8eSMike Smith 	switch (type) {
310bb0d0a8eSMike Smith 	case SYS_RES_IOPORT:
311a8b354a8SWarner Losh 		ok = 0;
312e4b59fc5SWarner Losh 		if (!pcib_is_io_open(sc))
313e4b59fc5SWarner Losh 			break;
314a8b354a8SWarner Losh 		ok = (start >= sc->iobase && end <= sc->iolimit);
315e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
316a8b354a8SWarner Losh 			if (!ok) {
31712b8c86eSWarner Losh 				if (start < sc->iobase)
31812b8c86eSWarner Losh 					start = sc->iobase;
31912b8c86eSWarner Losh 				if (end > sc->iolimit)
32012b8c86eSWarner Losh 					end = sc->iolimit;
321a8b354a8SWarner Losh 			}
3221c54ff33SMatthew N. Dodd 		} else {
323e4b59fc5SWarner Losh 			ok = 1;
324e4b59fc5SWarner Losh 			if (start < sc->iobase && end > sc->iolimit) {
325e4b59fc5SWarner Losh 				start = sc->iobase;
326e4b59fc5SWarner Losh 				end = sc->iolimit;
3271c54ff33SMatthew N. Dodd 			}
328e4b59fc5SWarner Losh 
32912b8c86eSWarner Losh 		}
330a8b354a8SWarner Losh 		if (end < start) {
331e4b59fc5SWarner Losh 			device_printf(dev, "ioport: end (%lx) < start (%lx)\n", end, start);
332a8b354a8SWarner Losh 			start = 0;
333a8b354a8SWarner Losh 			end = 0;
334a8b354a8SWarner Losh 			ok = 0;
335a8b354a8SWarner Losh 		}
336a8b354a8SWarner Losh 		if (!ok) {
337e4b59fc5SWarner Losh 			device_printf(dev, "device %s requested unsupported I/O "
338a8b354a8SWarner Losh 			    "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
339e4b59fc5SWarner Losh 			    device_get_nameunit(child), start, end,
340bb0d0a8eSMike Smith 			    sc->iobase, sc->iolimit);
341bb0d0a8eSMike Smith 			return (NULL);
342bb0d0a8eSMike Smith 		}
3434fa59183SMike Smith 		if (bootverbose)
344e4b59fc5SWarner Losh 			device_printf(dev, "device %s requested decoded I/O range 0x%lx-0x%lx\n",
345e4b59fc5SWarner Losh 			    device_get_nameunit(child), start, end);
346bb0d0a8eSMike Smith 		break;
347bb0d0a8eSMike Smith 
348bb0d0a8eSMike Smith 	case SYS_RES_MEMORY:
349a8b354a8SWarner Losh 		ok = 0;
350a8b354a8SWarner Losh 		if (pcib_is_nonprefetch_open(sc))
351a8b354a8SWarner Losh 			ok = ok || (start >= sc->membase && end <= sc->memlimit);
352a8b354a8SWarner Losh 		if (pcib_is_prefetch_open(sc))
353a8b354a8SWarner Losh 			ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
354e4b59fc5SWarner Losh 		if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
355a8b354a8SWarner Losh 			if (!ok) {
356a8b354a8SWarner Losh 				ok = 1;
357a8b354a8SWarner Losh 				if (flags & RF_PREFETCHABLE) {
358a8b354a8SWarner Losh 					if (pcib_is_prefetch_open(sc)) {
359a8b354a8SWarner Losh 						if (start < sc->pmembase)
360a8b354a8SWarner Losh 							start = sc->pmembase;
361a8b354a8SWarner Losh 						if (end > sc->pmemlimit)
362a8b354a8SWarner Losh 							end = sc->pmemlimit;
363a8b354a8SWarner Losh 					} else {
364a8b354a8SWarner Losh 						ok = 0;
365a8b354a8SWarner Losh 					}
366a8b354a8SWarner Losh 				} else {	/* non-prefetchable */
367a8b354a8SWarner Losh 					if (pcib_is_nonprefetch_open(sc)) {
368a8b354a8SWarner Losh 						if (start < sc->membase)
36912b8c86eSWarner Losh 							start = sc->membase;
37012b8c86eSWarner Losh 						if (end > sc->memlimit)
37112b8c86eSWarner Losh 							end = sc->memlimit;
3721c54ff33SMatthew N. Dodd 					} else {
373a8b354a8SWarner Losh 						ok = 0;
374a8b354a8SWarner Losh 					}
375a8b354a8SWarner Losh 				}
376a8b354a8SWarner Losh 			}
377a8b354a8SWarner Losh 		} else if (!ok) {
378e4b59fc5SWarner Losh 			ok = 1;	/* subtractive bridge: always ok */
379a8b354a8SWarner Losh 			if (pcib_is_nonprefetch_open(sc)) {
380e4b59fc5SWarner Losh 				if (start < sc->membase && end > sc->memlimit) {
381e4b59fc5SWarner Losh 					start = sc->membase;
382e4b59fc5SWarner Losh 					end = sc->memlimit;
383e4b59fc5SWarner Losh 				}
384a8b354a8SWarner Losh 			}
385a8b354a8SWarner Losh 			if (pcib_is_prefetch_open(sc)) {
386e4b59fc5SWarner Losh 				if (start < sc->pmembase && end > sc->pmemlimit) {
387e4b59fc5SWarner Losh 					start = sc->pmembase;
388e4b59fc5SWarner Losh 					end = sc->pmemlimit;
389a8b354a8SWarner Losh 				}
3901c54ff33SMatthew N. Dodd 			}
39112b8c86eSWarner Losh 		}
392a8b354a8SWarner Losh 		if (end < start) {
393e4b59fc5SWarner Losh 			device_printf(dev, "memory: end (%lx) < start (%lx)\n", end, start);
394a8b354a8SWarner Losh 			start = 0;
395a8b354a8SWarner Losh 			end = 0;
396a8b354a8SWarner Losh 			ok = 0;
397a8b354a8SWarner Losh 		}
398a8b354a8SWarner Losh 		if (!ok && bootverbose)
39934428485SWarner Losh 			device_printf(dev,
400e4b59fc5SWarner Losh 			    "device %s requested unsupported memory range "
40134428485SWarner Losh 			    "0x%lx-0x%lx (decoding 0x%x-0x%x, 0x%x-0x%x)\n",
402e4b59fc5SWarner Losh 			    device_get_nameunit(child), start, end,
403e4b59fc5SWarner Losh 			    sc->membase, sc->memlimit, sc->pmembase,
40434428485SWarner Losh 			    sc->pmemlimit);
405a8b354a8SWarner Losh 		if (!ok)
406bb0d0a8eSMike Smith 			return (NULL);
4074fa59183SMike Smith 		if (bootverbose)
408e4b59fc5SWarner Losh 			device_printf(dev,"device %s requested decoded memory range 0x%lx-0x%lx\n",
409e4b59fc5SWarner Losh 			    device_get_nameunit(child), start, end);
4104fa59183SMike Smith 		break;
4114fa59183SMike Smith 
412bb0d0a8eSMike Smith 	default:
4134fa59183SMike Smith 		break;
414bb0d0a8eSMike Smith 	}
415bb0d0a8eSMike Smith 	/*
416bb0d0a8eSMike Smith 	 * Bridge is OK decoding this resource, so pass it up.
417bb0d0a8eSMike Smith 	 */
418bb0d0a8eSMike Smith 	return (bus_generic_alloc_resource(dev, child, type, rid, start, end, count, flags));
419bb0d0a8eSMike Smith }
420bb0d0a8eSMike Smith 
421bb0d0a8eSMike Smith /*
422bb0d0a8eSMike Smith  * PCIB interface.
423bb0d0a8eSMike Smith  */
4246f0d5884SJohn Baldwin int
425bb0d0a8eSMike Smith pcib_maxslots(device_t dev)
426bb0d0a8eSMike Smith {
4274fa59183SMike Smith     return(PCI_SLOTMAX);
428bb0d0a8eSMike Smith }
429bb0d0a8eSMike Smith 
430bb0d0a8eSMike Smith /*
431bb0d0a8eSMike Smith  * Since we are a child of a PCI bus, its parent must support the pcib interface.
432bb0d0a8eSMike Smith  */
433b0cb115fSWarner Losh uint32_t
434bb0d0a8eSMike Smith pcib_read_config(device_t dev, int b, int s, int f, int reg, int width)
435bb0d0a8eSMike Smith {
436bb0d0a8eSMike Smith     return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width));
437bb0d0a8eSMike Smith }
438bb0d0a8eSMike Smith 
4396f0d5884SJohn Baldwin void
440b0cb115fSWarner Losh pcib_write_config(device_t dev, int b, int s, int f, int reg, uint32_t val, int width)
441bb0d0a8eSMike Smith {
442bb0d0a8eSMike Smith     PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width);
443bb0d0a8eSMike Smith }
444bb0d0a8eSMike Smith 
445bb0d0a8eSMike Smith /*
446bb0d0a8eSMike Smith  * Route an interrupt across a PCI bridge.
447bb0d0a8eSMike Smith  */
4482c2d1d07SBenno Rice int
449bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin)
450bb0d0a8eSMike Smith {
451bb0d0a8eSMike Smith     device_t	bus;
452bb0d0a8eSMike Smith     int		parent_intpin;
453bb0d0a8eSMike Smith     int		intnum;
454bb0d0a8eSMike Smith 
455bb0d0a8eSMike Smith     /*
456bb0d0a8eSMike Smith      *
457bb0d0a8eSMike Smith      * The PCI standard defines a swizzle of the child-side device/intpin to
458bb0d0a8eSMike Smith      * the parent-side intpin as follows.
459bb0d0a8eSMike Smith      *
460bb0d0a8eSMike Smith      * device = device on child bus
461bb0d0a8eSMike Smith      * child_intpin = intpin on child bus slot (0-3)
462bb0d0a8eSMike Smith      * parent_intpin = intpin on parent bus slot (0-3)
463bb0d0a8eSMike Smith      *
464bb0d0a8eSMike Smith      * parent_intpin = (device + child_intpin) % 4
465bb0d0a8eSMike Smith      */
466cdc95e1bSBernd Walter     parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
467bb0d0a8eSMike Smith 
468bb0d0a8eSMike Smith     /*
469bb0d0a8eSMike Smith      * Our parent is a PCI bus.  Its parent must export the pcib interface
470bb0d0a8eSMike Smith      * which includes the ability to route interrupts.
471bb0d0a8eSMike Smith      */
472bb0d0a8eSMike Smith     bus = device_get_parent(pcib);
473bb0d0a8eSMike Smith     intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
4748046c4b9SMike Smith     if (PCI_INTERRUPT_VALID(intnum)) {
475c6a121abSJohn Baldwin 	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
476c6a121abSJohn Baldwin 	    pci_get_slot(dev), 'A' + pin - 1, intnum);
4778046c4b9SMike Smith     }
478bb0d0a8eSMike Smith     return(intnum);
479bb0d0a8eSMike Smith }
480b173edafSJohn Baldwin 
481b173edafSJohn Baldwin /*
482b173edafSJohn Baldwin  * Try to read the bus number of a host-PCI bridge using appropriate config
483b173edafSJohn Baldwin  * registers.
484b173edafSJohn Baldwin  */
485b173edafSJohn Baldwin int
486b173edafSJohn Baldwin host_pcib_get_busno(pci_read_config_fn read_config, int bus, int slot, int func,
487b0cb115fSWarner Losh     uint8_t *busnum)
488b173edafSJohn Baldwin {
489b0cb115fSWarner Losh 	uint32_t id;
490b173edafSJohn Baldwin 
491b173edafSJohn Baldwin 	id = read_config(bus, slot, func, PCIR_DEVVENDOR, 4);
4921bbf2464SJohn Baldwin 	if (id == 0xffffffff)
493b173edafSJohn Baldwin 		return (0);
494b173edafSJohn Baldwin 
495b173edafSJohn Baldwin 	switch (id) {
496b173edafSJohn Baldwin 	case 0x12258086:
497b173edafSJohn Baldwin 		/* Intel 824?? */
498b173edafSJohn Baldwin 		/* XXX This is a guess */
499b173edafSJohn Baldwin 		/* *busnum = read_config(bus, slot, func, 0x41, 1); */
500b173edafSJohn Baldwin 		*busnum = bus;
501b173edafSJohn Baldwin 		break;
502b173edafSJohn Baldwin 	case 0x84c48086:
503b173edafSJohn Baldwin 		/* Intel 82454KX/GX (Orion) */
504b173edafSJohn Baldwin 		*busnum = read_config(bus, slot, func, 0x4a, 1);
505b173edafSJohn Baldwin 		break;
506b173edafSJohn Baldwin 	case 0x84ca8086:
507b173edafSJohn Baldwin 		/*
508b173edafSJohn Baldwin 		 * For the 450nx chipset, there is a whole bundle of
509b173edafSJohn Baldwin 		 * things pretending to be host bridges. The MIOC will
510b173edafSJohn Baldwin 		 * be seen first and isn't really a pci bridge (the
511b173edafSJohn Baldwin 		 * actual busses are attached to the PXB's). We need to
512b173edafSJohn Baldwin 		 * read the registers of the MIOC to figure out the
513b173edafSJohn Baldwin 		 * bus numbers for the PXB channels.
514b173edafSJohn Baldwin 		 *
515b173edafSJohn Baldwin 		 * Since the MIOC doesn't have a pci bus attached, we
516b173edafSJohn Baldwin 		 * pretend it wasn't there.
517b173edafSJohn Baldwin 		 */
518b173edafSJohn Baldwin 		return (0);
519b173edafSJohn Baldwin 	case 0x84cb8086:
520b173edafSJohn Baldwin 		switch (slot) {
521b173edafSJohn Baldwin 		case 0x12:
522b173edafSJohn Baldwin 			/* Intel 82454NX PXB#0, Bus#A */
5231bbf2464SJohn Baldwin 			*busnum = read_config(bus, 0x10, func, 0xd0, 1);
524b173edafSJohn Baldwin 			break;
525b173edafSJohn Baldwin 		case 0x13:
526b173edafSJohn Baldwin 			/* Intel 82454NX PXB#0, Bus#B */
5271bbf2464SJohn Baldwin 			*busnum = read_config(bus, 0x10, func, 0xd1, 1) + 1;
528b173edafSJohn Baldwin 			break;
529b173edafSJohn Baldwin 		case 0x14:
530b173edafSJohn Baldwin 			/* Intel 82454NX PXB#1, Bus#A */
5311bbf2464SJohn Baldwin 			*busnum = read_config(bus, 0x10, func, 0xd3, 1);
532b173edafSJohn Baldwin 			break;
533b173edafSJohn Baldwin 		case 0x15:
534b173edafSJohn Baldwin 			/* Intel 82454NX PXB#1, Bus#B */
5351bbf2464SJohn Baldwin 			*busnum = read_config(bus, 0x10, func, 0xd4, 1) + 1;
536b173edafSJohn Baldwin 			break;
537b173edafSJohn Baldwin 		}
538b173edafSJohn Baldwin 		break;
539b173edafSJohn Baldwin 
540b173edafSJohn Baldwin 		/* ServerWorks -- vendor 0x1166 */
541b173edafSJohn Baldwin 	case 0x00051166:
542b173edafSJohn Baldwin 	case 0x00061166:
543b173edafSJohn Baldwin 	case 0x00081166:
544b173edafSJohn Baldwin 	case 0x00091166:
545b173edafSJohn Baldwin 	case 0x00101166:
546b173edafSJohn Baldwin 	case 0x00111166:
547b173edafSJohn Baldwin 	case 0x00171166:
548b173edafSJohn Baldwin 	case 0x01011166:
549b173edafSJohn Baldwin 	case 0x010f1014:
550b173edafSJohn Baldwin 	case 0x02011166:
551b173edafSJohn Baldwin 	case 0x03021014:
552b173edafSJohn Baldwin 		*busnum = read_config(bus, slot, func, 0x44, 1);
553b173edafSJohn Baldwin 		break;
554b173edafSJohn Baldwin 	default:
555b173edafSJohn Baldwin 		/* Don't know how to read bus number. */
556b173edafSJohn Baldwin 		return 0;
557b173edafSJohn Baldwin 	}
558b173edafSJohn Baldwin 
559b173edafSJohn Baldwin 	return 1;
560b173edafSJohn Baldwin }
561