xref: /freebsd/sys/dev/pci/pci_pci.c (revision a8b354a8091ce75edbe1686ea28b82aaf7ae5896)
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  *	$FreeBSD$
31bb0d0a8eSMike Smith  */
32bb0d0a8eSMike Smith 
33bb0d0a8eSMike Smith /*
34bb0d0a8eSMike Smith  * PCI:PCI bridge support.
35bb0d0a8eSMike Smith  */
36bb0d0a8eSMike Smith 
37bb0d0a8eSMike Smith #include <sys/param.h>
38bb0d0a8eSMike Smith #include <sys/systm.h>
39bb0d0a8eSMike Smith #include <sys/kernel.h>
40bb0d0a8eSMike Smith #include <sys/bus.h>
41a8b354a8SWarner Losh #include <machine/bus.h>
42a8b354a8SWarner Losh #include <sys/rman.h>
431c54ff33SMatthew N. Dodd #include <sys/sysctl.h>
44bb0d0a8eSMike Smith 
45bb0d0a8eSMike Smith #include <machine/resource.h>
46bb0d0a8eSMike Smith 
47bb0d0a8eSMike Smith #include <pci/pcivar.h>
48bb0d0a8eSMike Smith #include <pci/pcireg.h>
496f0d5884SJohn Baldwin #include <pci/pcib_private.h>
50bb0d0a8eSMike Smith 
51bb0d0a8eSMike Smith #include "pcib_if.h"
52bb0d0a8eSMike Smith 
53bb0d0a8eSMike Smith static int		pcib_probe(device_t dev);
54bb0d0a8eSMike Smith static int		pcib_route_interrupt(device_t pcib, device_t dev, int pin);
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 /*
951c54ff33SMatthew N. Dodd  * sysctl and tunable vars
961c54ff33SMatthew N. Dodd  */
971c54ff33SMatthew N. Dodd static int pci_allow_unsupported_io_range = 0;
981c54ff33SMatthew N. Dodd TUNABLE_INT("hw.pci.allow_unsupported_io_range",
991c54ff33SMatthew N. Dodd 	(int *)&pci_allow_unsupported_io_range);
1001c54ff33SMatthew N. Dodd SYSCTL_DECL(_hw_pci);
1011c54ff33SMatthew N. Dodd SYSCTL_INT(_hw_pci, OID_AUTO, allow_unsupported_io_range, CTLFLAG_RD,
1021c54ff33SMatthew N. Dodd 	&pci_allow_unsupported_io_range, 0,
1031c54ff33SMatthew N. Dodd 	"Allows the PCI Bridge to pass through an unsupported memory range "
1041c54ff33SMatthew N. Dodd 	"assigned by the BIOS.");
1051c54ff33SMatthew N. Dodd 
1061c54ff33SMatthew N. Dodd /*
107bb0d0a8eSMike Smith  * Generic device interface
108bb0d0a8eSMike Smith  */
109bb0d0a8eSMike Smith static int
110bb0d0a8eSMike Smith pcib_probe(device_t dev)
111bb0d0a8eSMike Smith {
112bb0d0a8eSMike Smith     if ((pci_get_class(dev) == PCIC_BRIDGE) &&
113bb0d0a8eSMike Smith 	(pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
114bb0d0a8eSMike Smith 	device_set_desc(dev, "PCI-PCI bridge");
115bb0d0a8eSMike Smith 	return(-10000);
116bb0d0a8eSMike Smith     }
117bb0d0a8eSMike Smith     return(ENXIO);
118bb0d0a8eSMike Smith }
119bb0d0a8eSMike Smith 
1206f0d5884SJohn Baldwin void
1216f0d5884SJohn Baldwin pcib_attach_common(device_t dev)
122bb0d0a8eSMike Smith {
123bb0d0a8eSMike Smith     struct pcib_softc	*sc;
1244fa59183SMike Smith     u_int8_t		iolow;
125bb0d0a8eSMike Smith 
126bb0d0a8eSMike Smith     sc = device_get_softc(dev);
127bb0d0a8eSMike Smith     sc->dev = dev;
128bb0d0a8eSMike Smith 
1294fa59183SMike Smith     /*
1304fa59183SMike Smith      * Get current bridge configuration.
1314fa59183SMike Smith      */
1328983cfbfSMike Smith     sc->command   = pci_read_config(dev, PCIR_COMMAND, 1);
1334fa59183SMike Smith     sc->secbus    = pci_read_config(dev, PCIR_SECBUS_1, 1);
1344fa59183SMike Smith     sc->subbus    = pci_read_config(dev, PCIR_SUBBUS_1, 1);
1354fa59183SMike Smith     sc->secstat   = pci_read_config(dev, PCIR_SECSTAT_1, 2);
1364fa59183SMike Smith     sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
1374fa59183SMike Smith     sc->seclat    = pci_read_config(dev, PCIR_SECLAT_1, 1);
1384fa59183SMike Smith 
1394fa59183SMike Smith     /*
1404fa59183SMike Smith      * Determine current I/O decode.
1414fa59183SMike Smith      */
1428983cfbfSMike Smith     if (sc->command & PCIM_CMD_PORTEN) {
1434fa59183SMike Smith 	iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
1444fa59183SMike Smith 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
1454fa59183SMike Smith 	    sc->iobase = PCI_PPBIOBASE(pci_read_config(dev, PCIR_IOBASEH_1, 2),
1464fa59183SMike Smith 				       pci_read_config(dev, PCIR_IOBASEL_1, 1));
1474fa59183SMike Smith 	} else {
1484fa59183SMike Smith 	    sc->iobase = PCI_PPBIOBASE(0, pci_read_config(dev, PCIR_IOBASEL_1, 1));
1494fa59183SMike Smith 	}
1504fa59183SMike Smith 
1514fa59183SMike Smith 	iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
1524fa59183SMike Smith 	if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
1534fa59183SMike Smith 	    sc->iolimit = PCI_PPBIOLIMIT(pci_read_config(dev, PCIR_IOLIMITH_1, 2),
1544fa59183SMike Smith 					 pci_read_config(dev, PCIR_IOLIMITL_1, 1));
1554fa59183SMike Smith 	} else {
1564fa59183SMike Smith 	    sc->iolimit = PCI_PPBIOLIMIT(0, pci_read_config(dev, PCIR_IOLIMITL_1, 1));
1574fa59183SMike Smith 	}
1588983cfbfSMike Smith     }
1594fa59183SMike Smith 
1604fa59183SMike Smith     /*
1614fa59183SMike Smith      * Determine current memory decode.
1624fa59183SMike Smith      */
1638983cfbfSMike Smith     if (sc->command & PCIM_CMD_MEMEN) {
1644fa59183SMike Smith 	sc->membase   = PCI_PPBMEMBASE(0, pci_read_config(dev, PCIR_MEMBASE_1, 2));
1654fa59183SMike Smith 	sc->memlimit  = PCI_PPBMEMLIMIT(0, pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
1664fa59183SMike Smith 	sc->pmembase  = PCI_PPBMEMBASE((pci_addr_t)pci_read_config(dev, PCIR_PMBASEH_1, 4),
1674fa59183SMike Smith 				       pci_read_config(dev, PCIR_PMBASEL_1, 2));
1684fa59183SMike Smith 	sc->pmemlimit = PCI_PPBMEMLIMIT((pci_addr_t)pci_read_config(dev, PCIR_PMLIMITH_1, 4),
1694fa59183SMike Smith 					pci_read_config(dev, PCIR_PMLIMITL_1, 2));
1708983cfbfSMike Smith     }
1714fa59183SMike Smith 
1724fa59183SMike Smith     /*
1734fa59183SMike Smith      * Quirk handling.
1744fa59183SMike Smith      */
1754fa59183SMike Smith     switch (pci_get_devid(dev)) {
1764fa59183SMike Smith 	case 0x12258086:		/* Intel 82454KX/GX (Orion) */
1774fa59183SMike Smith 	{
1784fa59183SMike Smith 	    u_int8_t	supbus;
1794fa59183SMike Smith 
1804fa59183SMike Smith 	    supbus = pci_read_config(dev, 0x41, 1);
1814fa59183SMike Smith 	    if (supbus != 0xff) {
1824fa59183SMike Smith 		sc->secbus = supbus + 1;
1834fa59183SMike Smith 		sc->subbus = supbus + 1;
1844fa59183SMike Smith 	    }
1854fa59183SMike Smith 	}
1864fa59183SMike Smith 	break;
1874fa59183SMike Smith     }
1884fa59183SMike Smith 
189bb0d0a8eSMike Smith     if (bootverbose) {
190bb0d0a8eSMike Smith 	device_printf(dev, "  secondary bus     %d\n", sc->secbus);
191bb0d0a8eSMike Smith 	device_printf(dev, "  subordinate bus   %d\n", sc->subbus);
192bb0d0a8eSMike Smith 	device_printf(dev, "  I/O decode        0x%x-0x%x\n", sc->iobase, sc->iolimit);
193bb0d0a8eSMike Smith 	device_printf(dev, "  memory decode     0x%x-0x%x\n", sc->membase, sc->memlimit);
194bb0d0a8eSMike Smith 	device_printf(dev, "  prefetched decode 0x%x-0x%x\n", sc->pmembase, sc->pmemlimit);
195bb0d0a8eSMike Smith     }
196bb0d0a8eSMike Smith 
197bb0d0a8eSMike Smith     /*
198bb0d0a8eSMike Smith      * XXX If the secondary bus number is zero, we should assign a bus number
199bb0d0a8eSMike Smith      *     since the BIOS hasn't, then initialise the bridge.
200bb0d0a8eSMike Smith      */
201bb0d0a8eSMike Smith 
202bb0d0a8eSMike Smith     /*
203bb0d0a8eSMike Smith      * XXX If the subordinate bus number is less than the secondary bus number,
204bb0d0a8eSMike Smith      *     we should pick a better value.  One sensible alternative would be to
205bb0d0a8eSMike Smith      *     pick 255; the only tradeoff here is that configuration transactions
206bb0d0a8eSMike Smith      *     would be more widely routed than absolutely necessary.
207bb0d0a8eSMike Smith      */
2086f0d5884SJohn Baldwin }
209bb0d0a8eSMike Smith 
21038906aedSJohn Baldwin int
2116f0d5884SJohn Baldwin pcib_attach(device_t dev)
2126f0d5884SJohn Baldwin {
2136f0d5884SJohn Baldwin     struct pcib_softc	*sc;
2146f0d5884SJohn Baldwin     device_t		child;
2156f0d5884SJohn Baldwin 
2166f0d5884SJohn Baldwin     pcib_attach_common(dev);
2176f0d5884SJohn Baldwin     sc = device_get_softc(dev);
218bb0d0a8eSMike Smith     if (sc->secbus != 0) {
219cea0a895SJohn Baldwin 	child = device_add_child(dev, "pci", sc->secbus);
220bb0d0a8eSMike Smith 	if (child != NULL)
221bb0d0a8eSMike Smith 	    return(bus_generic_attach(dev));
222bb0d0a8eSMike Smith     }
223bb0d0a8eSMike Smith 
224bb0d0a8eSMike Smith     /* no secondary bus; we should have fixed this */
225bb0d0a8eSMike Smith     return(0);
226bb0d0a8eSMike Smith }
227bb0d0a8eSMike Smith 
2286f0d5884SJohn Baldwin int
229bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
230bb0d0a8eSMike Smith {
231bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
232bb0d0a8eSMike Smith 
233bb0d0a8eSMike Smith     switch (which) {
234bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
235bb0d0a8eSMike Smith 	*result = sc->secbus;
236bb0d0a8eSMike Smith 	return(0);
237bb0d0a8eSMike Smith     }
238bb0d0a8eSMike Smith     return(ENOENT);
239bb0d0a8eSMike Smith }
240bb0d0a8eSMike Smith 
2416f0d5884SJohn Baldwin int
242bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
243bb0d0a8eSMike Smith {
244bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
245bb0d0a8eSMike Smith 
246bb0d0a8eSMike Smith     switch (which) {
247bb0d0a8eSMike Smith     case PCIB_IVAR_BUS:
248bb0d0a8eSMike Smith 	sc->secbus = value;
249bb0d0a8eSMike Smith 	break;
250bb0d0a8eSMike Smith     }
251bb0d0a8eSMike Smith     return(ENOENT);
252bb0d0a8eSMike Smith }
253bb0d0a8eSMike Smith 
254bb0d0a8eSMike Smith /*
255d0036d6eSWarner Losh  * Is this a decoded ISA I/O port address?  Note, we need to do the mask that
256d0036d6eSWarner Losh  * we do below because of the ISA alias addresses.  I'm not 100% sure that
257a8b354a8SWarner Losh  * this is correct.  Maybe the bridge needs to be subtractive decode for
258a8b354a8SWarner Losh  * this to work?
259d0036d6eSWarner Losh  */
260d0036d6eSWarner Losh static int
261d0036d6eSWarner Losh pcib_is_isa_io(u_long start)
262d0036d6eSWarner Losh {
26312b8c86eSWarner Losh     if ((start & 0xfffUL)  > 0x3ffUL || start == 0)
264d0036d6eSWarner Losh 	return (0);
265d0036d6eSWarner Losh     return (1);
266d0036d6eSWarner Losh }
267d0036d6eSWarner Losh 
268d0036d6eSWarner Losh /*
269d0036d6eSWarner Losh  * Is this a decoded ISA memory address?
270d0036d6eSWarner Losh  */
271d0036d6eSWarner Losh static int
272d0036d6eSWarner Losh pcib_is_isa_mem(u_long start)
273d0036d6eSWarner Losh {
27412b8c86eSWarner Losh     if (start > 0xfffffUL || start == 0)
275d0036d6eSWarner Losh 	return (0);
276d0036d6eSWarner Losh     return (1);
277d0036d6eSWarner Losh }
278d0036d6eSWarner Losh 
279d0036d6eSWarner Losh /*
280a8b354a8SWarner Losh  * Is the prefetch window open (eg, can we allocate memory in it?)
281a8b354a8SWarner Losh  */
282a8b354a8SWarner Losh static int
283a8b354a8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc)
284a8b354a8SWarner Losh {
285a8b354a8SWarner Losh     return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
286a8b354a8SWarner Losh }
287a8b354a8SWarner Losh 
288a8b354a8SWarner Losh /*
289a8b354a8SWarner Losh  * Is the nonprefetch window open (eg, can we allocate memory in it?)
290a8b354a8SWarner Losh  */
291a8b354a8SWarner Losh static int
292a8b354a8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc)
293a8b354a8SWarner Losh {
294a8b354a8SWarner Losh     return (sc->membase > 0 && sc->membase < sc->memlimit);
295a8b354a8SWarner Losh }
296a8b354a8SWarner Losh 
297a8b354a8SWarner Losh /*
298a8b354a8SWarner Losh  * Is the io window open (eg, can we allocate ports in it?)
299a8b354a8SWarner Losh  */
300a8b354a8SWarner Losh static int
301a8b354a8SWarner Losh pcib_is_io_open(struct pcib_softc *sc)
302a8b354a8SWarner Losh {
303a8b354a8SWarner Losh     return (sc->iobase > 0 && sc->iobase < sc->iolimit);
304a8b354a8SWarner Losh }
305a8b354a8SWarner Losh 
306a8b354a8SWarner Losh /*
307bb0d0a8eSMike Smith  * We have to trap resource allocation requests and ensure that the bridge
308bb0d0a8eSMike Smith  * is set up to, or capable of handling them.
309bb0d0a8eSMike Smith  */
3106f0d5884SJohn Baldwin struct resource *
311bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
312bb0d0a8eSMike Smith 		    u_long start, u_long end, u_long count, u_int flags)
313bb0d0a8eSMike Smith {
314bb0d0a8eSMike Smith     struct pcib_softc	*sc = device_get_softc(dev);
315a8b354a8SWarner Losh     int ok;
316bb0d0a8eSMike Smith 
317bb0d0a8eSMike Smith     /*
318bb0d0a8eSMike Smith      * If this is a "default" allocation against this rid, we can't work
319bb0d0a8eSMike Smith      * out where it's coming from (we should actually never see these) so we
320bb0d0a8eSMike Smith      * just have to punt.
321bb0d0a8eSMike Smith      */
322bb0d0a8eSMike Smith     if ((start == 0) && (end == ~0)) {
323bb0d0a8eSMike Smith 	device_printf(dev, "can't decode default resource id %d for %s%d, bypassing\n",
324bb0d0a8eSMike Smith 		      *rid, device_get_name(child), device_get_unit(child));
325bb0d0a8eSMike Smith     } else {
326bb0d0a8eSMike Smith 	/*
327bb0d0a8eSMike Smith 	 * Fail the allocation for this range if it's not supported.
328bb0d0a8eSMike Smith 	 */
329bb0d0a8eSMike Smith 	switch (type) {
330bb0d0a8eSMike Smith 	case SYS_RES_IOPORT:
331a8b354a8SWarner Losh 	    ok = 1;
33212b8c86eSWarner Losh 	    if (!pcib_is_isa_io(start)) {
333a8b354a8SWarner Losh 		ok = 0;
334a8b354a8SWarner Losh 		if (pcib_is_io_open(sc))
335a8b354a8SWarner Losh 		    ok = (start >= sc->iobase && end <= sc->iolimit);
3361c54ff33SMatthew N. Dodd 		if (!pci_allow_unsupported_io_range) {
337a8b354a8SWarner Losh 		    if (!ok) {
33812b8c86eSWarner Losh 			if (start < sc->iobase)
33912b8c86eSWarner Losh 			    start = sc->iobase;
34012b8c86eSWarner Losh 			if (end > sc->iolimit)
34112b8c86eSWarner Losh 			    end = sc->iolimit;
342a8b354a8SWarner Losh 		    }
3431c54ff33SMatthew N. Dodd 		} else {
3446e47a4f6SPoul-Henning Kamp 		    if (start < sc->iobase)
3451c54ff33SMatthew N. Dodd 			printf("start (%lx) < sc->iobase (%x)\n", start,
3461c54ff33SMatthew N. Dodd 				sc->iobase);
3476e47a4f6SPoul-Henning Kamp 		    if (end > sc->iolimit)
3481c54ff33SMatthew N. Dodd 			printf("end (%lx) > sc->iolimit (%x)\n",
3491c54ff33SMatthew N. Dodd 				end, sc->iolimit);
3506e47a4f6SPoul-Henning Kamp 		    if (end < start)
3511efefb2dSWarner Losh 			printf("end (%lx) < start (%lx)\n", end, start);
3521c54ff33SMatthew N. Dodd 		}
35312b8c86eSWarner Losh 	    }
354a8b354a8SWarner Losh 	    if (end < start) {
355a8b354a8SWarner Losh 		start = 0;
356a8b354a8SWarner Losh 		end = 0;
357a8b354a8SWarner Losh 		ok = 0;
358a8b354a8SWarner Losh 	    }
359a8b354a8SWarner Losh 	    if (!ok) {
360a8b354a8SWarner Losh 		device_printf(dev, "device %s%d requested unsupported I/O "
361a8b354a8SWarner Losh 		  "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n",
362bb0d0a8eSMike Smith 		  device_get_name(child), device_get_unit(child), start, end,
363bb0d0a8eSMike Smith 		  sc->iobase, sc->iolimit);
364bb0d0a8eSMike Smith 		return (NULL);
365bb0d0a8eSMike Smith 	    }
3664fa59183SMike Smith 	    if (bootverbose)
3674fa59183SMike Smith 		device_printf(sc->dev, "device %s%d requested decoded I/O range 0x%lx-0x%lx\n",
3684fa59183SMike Smith 			      device_get_name(child), device_get_unit(child), start, end);
369bb0d0a8eSMike Smith 	    break;
370bb0d0a8eSMike Smith 
371bb0d0a8eSMike Smith 	case SYS_RES_MEMORY:
372a8b354a8SWarner Losh 	    ok = 1;
37312b8c86eSWarner Losh 	    if (!pcib_is_isa_mem(start)) {
374a8b354a8SWarner Losh 		ok = 0;
375a8b354a8SWarner Losh 		if (pcib_is_nonprefetch_open(sc))
376a8b354a8SWarner Losh 		    ok = ok || (start >= sc->membase && end <= sc->memlimit);
377a8b354a8SWarner Losh 		if (pcib_is_prefetch_open(sc))
378a8b354a8SWarner Losh 		    ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
3791c54ff33SMatthew N. Dodd 		if (!pci_allow_unsupported_io_range) {
380a8b354a8SWarner Losh 		    if (!ok) {
381a8b354a8SWarner Losh 			ok = 1;
382a8b354a8SWarner Losh 			if (flags & RF_PREFETCHABLE) {
383a8b354a8SWarner Losh 			    if (pcib_is_prefetch_open(sc)) {
384a8b354a8SWarner Losh 				if (start < sc->pmembase)
385a8b354a8SWarner Losh 				    start = sc->pmembase;
386a8b354a8SWarner Losh 				if (end > sc->pmemlimit)
387a8b354a8SWarner Losh 				    end = sc->pmemlimit;
388a8b354a8SWarner Losh 			    } else {
389a8b354a8SWarner Losh 				ok = 0;
390a8b354a8SWarner Losh 			    }
391a8b354a8SWarner Losh 			} else {	/* non-prefetchable */
392a8b354a8SWarner Losh 			    if (pcib_is_nonprefetch_open(sc)) {
393a8b354a8SWarner Losh 				if (start < sc->membase)
39412b8c86eSWarner Losh 				    start = sc->membase;
39512b8c86eSWarner Losh 				if (end > sc->memlimit)
39612b8c86eSWarner Losh 				    end = sc->memlimit;
3971c54ff33SMatthew N. Dodd 			    } else {
398a8b354a8SWarner Losh 				ok = 0;
399a8b354a8SWarner Losh 			    }
400a8b354a8SWarner Losh 			}
401a8b354a8SWarner Losh 		    }
402a8b354a8SWarner Losh 		} else if (!ok) {
403a8b354a8SWarner Losh 		    ok = 1;	/* pci_allow_unsupported_ranges -> always ok */
404a8b354a8SWarner Losh 		    if (pcib_is_nonprefetch_open(sc)) {
405a8b354a8SWarner Losh 			if (start < sc->membase)
4061c54ff33SMatthew N. Dodd 			    printf("start (%lx) < sc->membase (%x)\n",
4071c54ff33SMatthew N. Dodd 			      start, sc->membase);
4086e47a4f6SPoul-Henning Kamp 			if (end > sc->memlimit)
4091c54ff33SMatthew N. Dodd 			    printf("end (%lx) > sc->memlimit (%x)\n",
4101c54ff33SMatthew N. Dodd 			      end, sc->memlimit);
411a8b354a8SWarner Losh 		    }
412a8b354a8SWarner Losh 		    if (pcib_is_prefetch_open(sc)) {
413a8b354a8SWarner Losh 			if (start < sc->pmembase)
414a8b354a8SWarner Losh 			    printf("start (%lx) < sc->pmembase (%x)\n",
415a8b354a8SWarner Losh 			      start, sc->pmembase);
416a8b354a8SWarner Losh 			if (end > sc->pmemlimit)
417a8b354a8SWarner Losh 			    printf("end (%lx) > sc->pmemlimit (%x)\n",
418a8b354a8SWarner Losh 			      end, sc->memlimit);
419a8b354a8SWarner Losh 		    }
4206e47a4f6SPoul-Henning Kamp 		    if (end < start)
4211efefb2dSWarner Losh 			printf("end (%lx) < start (%lx)\n", end, start);
4221c54ff33SMatthew N. Dodd 		}
42312b8c86eSWarner Losh 	    }
424a8b354a8SWarner Losh 	    if (end < start) {
425a8b354a8SWarner Losh 		start = 0;
426a8b354a8SWarner Losh 		end = 0;
427a8b354a8SWarner Losh 		ok = 0;
428a8b354a8SWarner Losh 	    }
429a8b354a8SWarner Losh 	    if (!ok && bootverbose)
43034428485SWarner Losh 		device_printf(dev,
43134428485SWarner Losh 		  "device %s%d requested unsupported memory range "
43234428485SWarner Losh 		  "0x%lx-0x%lx (decoding 0x%x-0x%x, 0x%x-0x%x)\n",
43334428485SWarner Losh 		  device_get_name(child), device_get_unit(child), start,
43434428485SWarner Losh 		  end, sc->membase, sc->memlimit, sc->pmembase,
43534428485SWarner Losh 		  sc->pmemlimit);
436a8b354a8SWarner Losh 	    if (!ok)
437bb0d0a8eSMike Smith 		return (NULL);
4384fa59183SMike Smith 	    if (bootverbose)
4394fa59183SMike Smith 		device_printf(sc->dev, "device %s%d requested decoded memory range 0x%lx-0x%lx\n",
4404fa59183SMike Smith 		  device_get_name(child), device_get_unit(child), start, end);
4414fa59183SMike Smith 	    break;
4424fa59183SMike Smith 
443bb0d0a8eSMike Smith 	default:
4444fa59183SMike Smith 	    break;
445bb0d0a8eSMike Smith 	}
446bb0d0a8eSMike Smith     }
4474fa59183SMike Smith 
448bb0d0a8eSMike Smith     /*
449bb0d0a8eSMike Smith      * Bridge is OK decoding this resource, so pass it up.
450bb0d0a8eSMike Smith      */
451bb0d0a8eSMike Smith     return(bus_generic_alloc_resource(dev, child, type, rid, start, end, count, flags));
452bb0d0a8eSMike Smith }
453bb0d0a8eSMike Smith 
454bb0d0a8eSMike Smith /*
455bb0d0a8eSMike Smith  * PCIB interface.
456bb0d0a8eSMike Smith  */
4576f0d5884SJohn Baldwin int
458bb0d0a8eSMike Smith pcib_maxslots(device_t dev)
459bb0d0a8eSMike Smith {
4604fa59183SMike Smith     return(PCI_SLOTMAX);
461bb0d0a8eSMike Smith }
462bb0d0a8eSMike Smith 
463bb0d0a8eSMike Smith /*
464bb0d0a8eSMike Smith  * Since we are a child of a PCI bus, its parent must support the pcib interface.
465bb0d0a8eSMike Smith  */
4666f0d5884SJohn Baldwin u_int32_t
467bb0d0a8eSMike Smith pcib_read_config(device_t dev, int b, int s, int f, int reg, int width)
468bb0d0a8eSMike Smith {
469bb0d0a8eSMike Smith     return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width));
470bb0d0a8eSMike Smith }
471bb0d0a8eSMike Smith 
4726f0d5884SJohn Baldwin void
473bb0d0a8eSMike Smith pcib_write_config(device_t dev, int b, int s, int f, int reg, u_int32_t val, int width)
474bb0d0a8eSMike Smith {
475bb0d0a8eSMike Smith     PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width);
476bb0d0a8eSMike Smith }
477bb0d0a8eSMike Smith 
478bb0d0a8eSMike Smith /*
479bb0d0a8eSMike Smith  * Route an interrupt across a PCI bridge.
480bb0d0a8eSMike Smith  */
481bb0d0a8eSMike Smith static int
482bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin)
483bb0d0a8eSMike Smith {
484bb0d0a8eSMike Smith     device_t	bus;
485bb0d0a8eSMike Smith     int		parent_intpin;
486bb0d0a8eSMike Smith     int		intnum;
487bb0d0a8eSMike Smith 
488bb0d0a8eSMike Smith     /*
489bb0d0a8eSMike Smith      *
490bb0d0a8eSMike Smith      * The PCI standard defines a swizzle of the child-side device/intpin to
491bb0d0a8eSMike Smith      * the parent-side intpin as follows.
492bb0d0a8eSMike Smith      *
493bb0d0a8eSMike Smith      * device = device on child bus
494bb0d0a8eSMike Smith      * child_intpin = intpin on child bus slot (0-3)
495bb0d0a8eSMike Smith      * parent_intpin = intpin on parent bus slot (0-3)
496bb0d0a8eSMike Smith      *
497bb0d0a8eSMike Smith      * parent_intpin = (device + child_intpin) % 4
498bb0d0a8eSMike Smith      */
499bb0d0a8eSMike Smith     parent_intpin = (pci_get_slot(pcib) + (pin - 1)) % 4;
500bb0d0a8eSMike Smith 
501bb0d0a8eSMike Smith     /*
502bb0d0a8eSMike Smith      * Our parent is a PCI bus.  Its parent must export the pcib interface
503bb0d0a8eSMike Smith      * which includes the ability to route interrupts.
504bb0d0a8eSMike Smith      */
505bb0d0a8eSMike Smith     bus = device_get_parent(pcib);
506bb0d0a8eSMike Smith     intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
5078046c4b9SMike Smith     if (PCI_INTERRUPT_VALID(intnum)) {
508c6a121abSJohn Baldwin 	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
509c6a121abSJohn Baldwin 	    pci_get_slot(dev), 'A' + pin - 1, intnum);
5108046c4b9SMike Smith     }
511bb0d0a8eSMike Smith     return(intnum);
512bb0d0a8eSMike Smith }
513b173edafSJohn Baldwin 
514b173edafSJohn Baldwin /*
515b173edafSJohn Baldwin  * Try to read the bus number of a host-PCI bridge using appropriate config
516b173edafSJohn Baldwin  * registers.
517b173edafSJohn Baldwin  */
518b173edafSJohn Baldwin int
519b173edafSJohn Baldwin host_pcib_get_busno(pci_read_config_fn read_config, int bus, int slot, int func,
520b173edafSJohn Baldwin     u_int8_t *busnum)
521b173edafSJohn Baldwin {
522b173edafSJohn Baldwin 	u_int32_t id;
523b173edafSJohn Baldwin 
524b173edafSJohn Baldwin 	id = read_config(bus, slot, func, PCIR_DEVVENDOR, 4);
5251bbf2464SJohn Baldwin 	if (id == 0xffffffff)
526b173edafSJohn Baldwin 		return (0);
527b173edafSJohn Baldwin 
528b173edafSJohn Baldwin 	switch (id) {
529b173edafSJohn Baldwin 	case 0x12258086:
530b173edafSJohn Baldwin 		/* Intel 824?? */
531b173edafSJohn Baldwin 		/* XXX This is a guess */
532b173edafSJohn Baldwin 		/* *busnum = read_config(bus, slot, func, 0x41, 1); */
533b173edafSJohn Baldwin 		*busnum = bus;
534b173edafSJohn Baldwin 		break;
535b173edafSJohn Baldwin 	case 0x84c48086:
536b173edafSJohn Baldwin 		/* Intel 82454KX/GX (Orion) */
537b173edafSJohn Baldwin 		*busnum = read_config(bus, slot, func, 0x4a, 1);
538b173edafSJohn Baldwin 		break;
539b173edafSJohn Baldwin 	case 0x84ca8086:
540b173edafSJohn Baldwin 		/*
541b173edafSJohn Baldwin 		 * For the 450nx chipset, there is a whole bundle of
542b173edafSJohn Baldwin 		 * things pretending to be host bridges. The MIOC will
543b173edafSJohn Baldwin 		 * be seen first and isn't really a pci bridge (the
544b173edafSJohn Baldwin 		 * actual busses are attached to the PXB's). We need to
545b173edafSJohn Baldwin 		 * read the registers of the MIOC to figure out the
546b173edafSJohn Baldwin 		 * bus numbers for the PXB channels.
547b173edafSJohn Baldwin 		 *
548b173edafSJohn Baldwin 		 * Since the MIOC doesn't have a pci bus attached, we
549b173edafSJohn Baldwin 		 * pretend it wasn't there.
550b173edafSJohn Baldwin 		 */
551b173edafSJohn Baldwin 		return (0);
552b173edafSJohn Baldwin 	case 0x84cb8086:
553b173edafSJohn Baldwin 		switch (slot) {
554b173edafSJohn Baldwin 		case 0x12:
555b173edafSJohn Baldwin 			/* Intel 82454NX PXB#0, Bus#A */
5561bbf2464SJohn Baldwin 			*busnum = read_config(bus, 0x10, func, 0xd0, 1);
557b173edafSJohn Baldwin 			break;
558b173edafSJohn Baldwin 		case 0x13:
559b173edafSJohn Baldwin 			/* Intel 82454NX PXB#0, Bus#B */
5601bbf2464SJohn Baldwin 			*busnum = read_config(bus, 0x10, func, 0xd1, 1) + 1;
561b173edafSJohn Baldwin 			break;
562b173edafSJohn Baldwin 		case 0x14:
563b173edafSJohn Baldwin 			/* Intel 82454NX PXB#1, Bus#A */
5641bbf2464SJohn Baldwin 			*busnum = read_config(bus, 0x10, func, 0xd3, 1);
565b173edafSJohn Baldwin 			break;
566b173edafSJohn Baldwin 		case 0x15:
567b173edafSJohn Baldwin 			/* Intel 82454NX PXB#1, Bus#B */
5681bbf2464SJohn Baldwin 			*busnum = read_config(bus, 0x10, func, 0xd4, 1) + 1;
569b173edafSJohn Baldwin 			break;
570b173edafSJohn Baldwin 		}
571b173edafSJohn Baldwin 		break;
572b173edafSJohn Baldwin 
573b173edafSJohn Baldwin 		/* ServerWorks -- vendor 0x1166 */
574b173edafSJohn Baldwin 	case 0x00051166:
575b173edafSJohn Baldwin 	case 0x00061166:
576b173edafSJohn Baldwin 	case 0x00081166:
577b173edafSJohn Baldwin 	case 0x00091166:
578b173edafSJohn Baldwin 	case 0x00101166:
579b173edafSJohn Baldwin 	case 0x00111166:
580b173edafSJohn Baldwin 	case 0x00171166:
581b173edafSJohn Baldwin 	case 0x01011166:
582b173edafSJohn Baldwin 	case 0x010f1014:
583b173edafSJohn Baldwin 	case 0x02011166:
584b173edafSJohn Baldwin 	case 0x03021014:
585b173edafSJohn Baldwin 		*busnum = read_config(bus, slot, func, 0x44, 1);
586b173edafSJohn Baldwin 		break;
587b173edafSJohn Baldwin 	default:
588b173edafSJohn Baldwin 		/* Don't know how to read bus number. */
589b173edafSJohn Baldwin 		return 0;
590b173edafSJohn Baldwin 	}
591b173edafSJohn Baldwin 
592b173edafSJohn Baldwin 	return 1;
593b173edafSJohn Baldwin }
594