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> 41bb0d0a8eSMike Smith 42bb0d0a8eSMike Smith #include <machine/resource.h> 43bb0d0a8eSMike Smith 44bb0d0a8eSMike Smith #include <pci/pcivar.h> 45bb0d0a8eSMike Smith #include <pci/pcireg.h> 466f0d5884SJohn Baldwin #include <pci/pcib_private.h> 47bb0d0a8eSMike Smith 48bb0d0a8eSMike Smith #include "pcib_if.h" 497a852c22SWarner Losh #include "opt_pci.h" 50bb0d0a8eSMike Smith 51bb0d0a8eSMike Smith static int pcib_probe(device_t dev); 52bb0d0a8eSMike Smith static int pcib_route_interrupt(device_t pcib, device_t dev, int pin); 53bb0d0a8eSMike Smith 54bb0d0a8eSMike Smith static device_method_t pcib_methods[] = { 55bb0d0a8eSMike Smith /* Device interface */ 56bb0d0a8eSMike Smith DEVMETHOD(device_probe, pcib_probe), 57bb0d0a8eSMike Smith DEVMETHOD(device_attach, pcib_attach), 58bb0d0a8eSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 59bb0d0a8eSMike Smith DEVMETHOD(device_suspend, bus_generic_suspend), 60bb0d0a8eSMike Smith DEVMETHOD(device_resume, bus_generic_resume), 61bb0d0a8eSMike Smith 62bb0d0a8eSMike Smith /* Bus interface */ 63bb0d0a8eSMike Smith DEVMETHOD(bus_print_child, bus_generic_print_child), 64bb0d0a8eSMike Smith DEVMETHOD(bus_read_ivar, pcib_read_ivar), 65bb0d0a8eSMike Smith DEVMETHOD(bus_write_ivar, pcib_write_ivar), 66bb0d0a8eSMike Smith DEVMETHOD(bus_alloc_resource, pcib_alloc_resource), 67bb0d0a8eSMike Smith DEVMETHOD(bus_release_resource, bus_generic_release_resource), 68bb0d0a8eSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 69bb0d0a8eSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 70bb0d0a8eSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 71bb0d0a8eSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 72bb0d0a8eSMike Smith 73bb0d0a8eSMike Smith /* pcib interface */ 74bb0d0a8eSMike Smith DEVMETHOD(pcib_maxslots, pcib_maxslots), 75bb0d0a8eSMike Smith DEVMETHOD(pcib_read_config, pcib_read_config), 76bb0d0a8eSMike Smith DEVMETHOD(pcib_write_config, pcib_write_config), 77bb0d0a8eSMike Smith DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt), 78bb0d0a8eSMike Smith 79bb0d0a8eSMike Smith { 0, 0 } 80bb0d0a8eSMike Smith }; 81bb0d0a8eSMike Smith 82bb0d0a8eSMike Smith static driver_t pcib_driver = { 83bb0d0a8eSMike Smith "pcib", 84bb0d0a8eSMike Smith pcib_methods, 85bb0d0a8eSMike Smith sizeof(struct pcib_softc), 86bb0d0a8eSMike Smith }; 87bb0d0a8eSMike Smith 886f0d5884SJohn Baldwin devclass_t pcib_devclass; 89bb0d0a8eSMike Smith 90bb0d0a8eSMike Smith DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, 0, 0); 91bb0d0a8eSMike Smith 92bb0d0a8eSMike Smith /* 93bb0d0a8eSMike Smith * Generic device interface 94bb0d0a8eSMike Smith */ 95bb0d0a8eSMike Smith static int 96bb0d0a8eSMike Smith pcib_probe(device_t dev) 97bb0d0a8eSMike Smith { 98bb0d0a8eSMike Smith if ((pci_get_class(dev) == PCIC_BRIDGE) && 99bb0d0a8eSMike Smith (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) { 100bb0d0a8eSMike Smith device_set_desc(dev, "PCI-PCI bridge"); 101bb0d0a8eSMike Smith return(-10000); 102bb0d0a8eSMike Smith } 103bb0d0a8eSMike Smith return(ENXIO); 104bb0d0a8eSMike Smith } 105bb0d0a8eSMike Smith 1066f0d5884SJohn Baldwin void 1076f0d5884SJohn Baldwin pcib_attach_common(device_t dev) 108bb0d0a8eSMike Smith { 109bb0d0a8eSMike Smith struct pcib_softc *sc; 1104fa59183SMike Smith u_int8_t iolow; 111bb0d0a8eSMike Smith 112bb0d0a8eSMike Smith sc = device_get_softc(dev); 113bb0d0a8eSMike Smith sc->dev = dev; 114bb0d0a8eSMike Smith 1154fa59183SMike Smith /* 1164fa59183SMike Smith * Get current bridge configuration. 1174fa59183SMike Smith */ 1188983cfbfSMike Smith sc->command = pci_read_config(dev, PCIR_COMMAND, 1); 1194fa59183SMike Smith sc->secbus = pci_read_config(dev, PCIR_SECBUS_1, 1); 1204fa59183SMike Smith sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1); 1214fa59183SMike Smith sc->secstat = pci_read_config(dev, PCIR_SECSTAT_1, 2); 1224fa59183SMike Smith sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 1234fa59183SMike Smith sc->seclat = pci_read_config(dev, PCIR_SECLAT_1, 1); 1244fa59183SMike Smith 1254fa59183SMike Smith /* 1264fa59183SMike Smith * Determine current I/O decode. 1274fa59183SMike Smith */ 1288983cfbfSMike Smith if (sc->command & PCIM_CMD_PORTEN) { 1294fa59183SMike Smith iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1); 1304fa59183SMike Smith if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 1314fa59183SMike Smith sc->iobase = PCI_PPBIOBASE(pci_read_config(dev, PCIR_IOBASEH_1, 2), 1324fa59183SMike Smith pci_read_config(dev, PCIR_IOBASEL_1, 1)); 1334fa59183SMike Smith } else { 1344fa59183SMike Smith sc->iobase = PCI_PPBIOBASE(0, pci_read_config(dev, PCIR_IOBASEL_1, 1)); 1354fa59183SMike Smith } 1364fa59183SMike Smith 1374fa59183SMike Smith iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1); 1384fa59183SMike Smith if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 1394fa59183SMike Smith sc->iolimit = PCI_PPBIOLIMIT(pci_read_config(dev, PCIR_IOLIMITH_1, 2), 1404fa59183SMike Smith pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 1414fa59183SMike Smith } else { 1424fa59183SMike Smith sc->iolimit = PCI_PPBIOLIMIT(0, pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 1434fa59183SMike Smith } 1448983cfbfSMike Smith } 1454fa59183SMike Smith 1464fa59183SMike Smith /* 1474fa59183SMike Smith * Determine current memory decode. 1484fa59183SMike Smith */ 1498983cfbfSMike Smith if (sc->command & PCIM_CMD_MEMEN) { 1504fa59183SMike Smith sc->membase = PCI_PPBMEMBASE(0, pci_read_config(dev, PCIR_MEMBASE_1, 2)); 1514fa59183SMike Smith sc->memlimit = PCI_PPBMEMLIMIT(0, pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 1524fa59183SMike Smith sc->pmembase = PCI_PPBMEMBASE((pci_addr_t)pci_read_config(dev, PCIR_PMBASEH_1, 4), 1534fa59183SMike Smith pci_read_config(dev, PCIR_PMBASEL_1, 2)); 1544fa59183SMike Smith sc->pmemlimit = PCI_PPBMEMLIMIT((pci_addr_t)pci_read_config(dev, PCIR_PMLIMITH_1, 4), 1554fa59183SMike Smith pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 1568983cfbfSMike Smith } 1574fa59183SMike Smith 1584fa59183SMike Smith /* 1594fa59183SMike Smith * Quirk handling. 1604fa59183SMike Smith */ 1614fa59183SMike Smith switch (pci_get_devid(dev)) { 1624fa59183SMike Smith case 0x12258086: /* Intel 82454KX/GX (Orion) */ 1634fa59183SMike Smith { 1644fa59183SMike Smith u_int8_t supbus; 1654fa59183SMike Smith 1664fa59183SMike Smith supbus = pci_read_config(dev, 0x41, 1); 1674fa59183SMike Smith if (supbus != 0xff) { 1684fa59183SMike Smith sc->secbus = supbus + 1; 1694fa59183SMike Smith sc->subbus = supbus + 1; 1704fa59183SMike Smith } 1714fa59183SMike Smith } 1724fa59183SMike Smith break; 1734fa59183SMike Smith } 1744fa59183SMike Smith 175bb0d0a8eSMike Smith if (bootverbose) { 176bb0d0a8eSMike Smith device_printf(dev, " secondary bus %d\n", sc->secbus); 177bb0d0a8eSMike Smith device_printf(dev, " subordinate bus %d\n", sc->subbus); 178bb0d0a8eSMike Smith device_printf(dev, " I/O decode 0x%x-0x%x\n", sc->iobase, sc->iolimit); 179bb0d0a8eSMike Smith device_printf(dev, " memory decode 0x%x-0x%x\n", sc->membase, sc->memlimit); 180bb0d0a8eSMike Smith device_printf(dev, " prefetched decode 0x%x-0x%x\n", sc->pmembase, sc->pmemlimit); 181bb0d0a8eSMike Smith } 182bb0d0a8eSMike Smith 183bb0d0a8eSMike Smith /* 184bb0d0a8eSMike Smith * XXX If the secondary bus number is zero, we should assign a bus number 185bb0d0a8eSMike Smith * since the BIOS hasn't, then initialise the bridge. 186bb0d0a8eSMike Smith */ 187bb0d0a8eSMike Smith 188bb0d0a8eSMike Smith /* 189bb0d0a8eSMike Smith * XXX If the subordinate bus number is less than the secondary bus number, 190bb0d0a8eSMike Smith * we should pick a better value. One sensible alternative would be to 191bb0d0a8eSMike Smith * pick 255; the only tradeoff here is that configuration transactions 192bb0d0a8eSMike Smith * would be more widely routed than absolutely necessary. 193bb0d0a8eSMike Smith */ 1946f0d5884SJohn Baldwin } 195bb0d0a8eSMike Smith 19638906aedSJohn Baldwin int 1976f0d5884SJohn Baldwin pcib_attach(device_t dev) 1986f0d5884SJohn Baldwin { 1996f0d5884SJohn Baldwin struct pcib_softc *sc; 2006f0d5884SJohn Baldwin device_t child; 2016f0d5884SJohn Baldwin 2026f0d5884SJohn Baldwin pcib_attach_common(dev); 2036f0d5884SJohn Baldwin sc = device_get_softc(dev); 204bb0d0a8eSMike Smith if (sc->secbus != 0) { 205cea0a895SJohn Baldwin child = device_add_child(dev, "pci", sc->secbus); 206bb0d0a8eSMike Smith if (child != NULL) 207bb0d0a8eSMike Smith return(bus_generic_attach(dev)); 208bb0d0a8eSMike Smith } 209bb0d0a8eSMike Smith 210bb0d0a8eSMike Smith /* no secondary bus; we should have fixed this */ 211bb0d0a8eSMike Smith return(0); 212bb0d0a8eSMike Smith } 213bb0d0a8eSMike Smith 2146f0d5884SJohn Baldwin int 215bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 216bb0d0a8eSMike Smith { 217bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 218bb0d0a8eSMike Smith 219bb0d0a8eSMike Smith switch (which) { 220bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 221bb0d0a8eSMike Smith *result = sc->secbus; 222bb0d0a8eSMike Smith return(0); 223bb0d0a8eSMike Smith } 224bb0d0a8eSMike Smith return(ENOENT); 225bb0d0a8eSMike Smith } 226bb0d0a8eSMike Smith 2276f0d5884SJohn Baldwin int 228bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 229bb0d0a8eSMike Smith { 230bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 231bb0d0a8eSMike Smith 232bb0d0a8eSMike Smith switch (which) { 233bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 234bb0d0a8eSMike Smith sc->secbus = value; 235bb0d0a8eSMike Smith break; 236bb0d0a8eSMike Smith } 237bb0d0a8eSMike Smith return(ENOENT); 238bb0d0a8eSMike Smith } 239bb0d0a8eSMike Smith 240bb0d0a8eSMike Smith /* 241d0036d6eSWarner Losh * Is this a decoded ISA I/O port address? Note, we need to do the mask that 242d0036d6eSWarner Losh * we do below because of the ISA alias addresses. I'm not 100% sure that 243d0036d6eSWarner Losh * this is correct. 244d0036d6eSWarner Losh */ 245d0036d6eSWarner Losh static int 246d0036d6eSWarner Losh pcib_is_isa_io(u_long start) 247d0036d6eSWarner Losh { 24812b8c86eSWarner Losh if ((start & 0xfffUL) > 0x3ffUL || start == 0) 249d0036d6eSWarner Losh return (0); 250d0036d6eSWarner Losh return (1); 251d0036d6eSWarner Losh } 252d0036d6eSWarner Losh 253d0036d6eSWarner Losh /* 254d0036d6eSWarner Losh * Is this a decoded ISA memory address? 255d0036d6eSWarner Losh */ 256d0036d6eSWarner Losh static int 257d0036d6eSWarner Losh pcib_is_isa_mem(u_long start) 258d0036d6eSWarner Losh { 25912b8c86eSWarner Losh if (start > 0xfffffUL || start == 0) 260d0036d6eSWarner Losh return (0); 261d0036d6eSWarner Losh return (1); 262d0036d6eSWarner Losh } 263d0036d6eSWarner Losh 264d0036d6eSWarner Losh /* 265bb0d0a8eSMike Smith * We have to trap resource allocation requests and ensure that the bridge 266bb0d0a8eSMike Smith * is set up to, or capable of handling them. 267bb0d0a8eSMike Smith */ 2686f0d5884SJohn Baldwin struct resource * 269bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 270bb0d0a8eSMike Smith u_long start, u_long end, u_long count, u_int flags) 271bb0d0a8eSMike Smith { 272bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 273bb0d0a8eSMike Smith 274bb0d0a8eSMike Smith /* 275bb0d0a8eSMike Smith * If this is a "default" allocation against this rid, we can't work 276bb0d0a8eSMike Smith * out where it's coming from (we should actually never see these) so we 277bb0d0a8eSMike Smith * just have to punt. 278bb0d0a8eSMike Smith */ 279bb0d0a8eSMike Smith if ((start == 0) && (end == ~0)) { 280bb0d0a8eSMike Smith device_printf(dev, "can't decode default resource id %d for %s%d, bypassing\n", 281bb0d0a8eSMike Smith *rid, device_get_name(child), device_get_unit(child)); 282bb0d0a8eSMike Smith } else { 283bb0d0a8eSMike Smith /* 284bb0d0a8eSMike Smith * Fail the allocation for this range if it's not supported. 285bb0d0a8eSMike Smith * 286bb0d0a8eSMike Smith * XXX we should probably just fix up the bridge decode and soldier on. 287bb0d0a8eSMike Smith */ 288bb0d0a8eSMike Smith switch (type) { 289bb0d0a8eSMike Smith case SYS_RES_IOPORT: 29012b8c86eSWarner Losh if (!pcib_is_isa_io(start)) { 2916e47a4f6SPoul-Henning Kamp #ifndef PCI_ALLOW_UNSUPPORTED_IO_RANGE 29212b8c86eSWarner Losh if (start < sc->iobase) 29312b8c86eSWarner Losh start = sc->iobase; 29412b8c86eSWarner Losh if (end > sc->iolimit) 29512b8c86eSWarner Losh end = sc->iolimit; 29612b8c86eSWarner Losh if (end < start) 29712b8c86eSWarner Losh start = 0; 2986e47a4f6SPoul-Henning Kamp #else 2996e47a4f6SPoul-Henning Kamp if (start < sc->iobase) 3001efefb2dSWarner Losh printf("start (%lx) < sc->iobase (%x)\n", start, sc->iobase); 3016e47a4f6SPoul-Henning Kamp if (end > sc->iolimit) 3021efefb2dSWarner Losh printf("end (%lx) > sc->iolimit (%x)\n", end, sc->iolimit); 3036e47a4f6SPoul-Henning Kamp if (end < start) 3041efefb2dSWarner Losh printf("end (%lx) < start (%lx)\n", end, start); 3056e47a4f6SPoul-Henning Kamp #endif 30612b8c86eSWarner Losh } 307d0036d6eSWarner Losh if (!pcib_is_isa_io(start) && 308d0036d6eSWarner Losh ((start < sc->iobase) || (end > sc->iolimit))) { 309bb0d0a8eSMike Smith device_printf(dev, "device %s%d requested unsupported I/O range 0x%lx-0x%lx" 310bb0d0a8eSMike Smith " (decoding 0x%x-0x%x)\n", 311bb0d0a8eSMike Smith device_get_name(child), device_get_unit(child), start, end, 312bb0d0a8eSMike Smith sc->iobase, sc->iolimit); 313bb0d0a8eSMike Smith return (NULL); 314bb0d0a8eSMike Smith } 3154fa59183SMike Smith if (bootverbose) 3164fa59183SMike Smith device_printf(sc->dev, "device %s%d requested decoded I/O range 0x%lx-0x%lx\n", 3174fa59183SMike Smith device_get_name(child), device_get_unit(child), start, end); 318bb0d0a8eSMike Smith break; 319bb0d0a8eSMike Smith 320bb0d0a8eSMike Smith /* 321bb0d0a8eSMike Smith * XXX will have to decide whether the device making the request is asking 322bb0d0a8eSMike Smith * for prefetchable memory or not. If it's coming from another bridge 323bb0d0a8eSMike Smith * down the line, do we assume not, or ask the bridge to pass in another 324bb0d0a8eSMike Smith * flag as the request bubbles up? 325bb0d0a8eSMike Smith */ 326bb0d0a8eSMike Smith case SYS_RES_MEMORY: 32712b8c86eSWarner Losh if (!pcib_is_isa_mem(start)) { 3286e47a4f6SPoul-Henning Kamp #ifndef PCI_ALLOW_UNSUPPORTED_IO_RANGE 3298961964dSWarner Losh if (start < sc->membase && end >= sc->membase) 33012b8c86eSWarner Losh start = sc->membase; 33112b8c86eSWarner Losh if (end > sc->memlimit) 33212b8c86eSWarner Losh end = sc->memlimit; 33312b8c86eSWarner Losh if (end < start) 33412b8c86eSWarner Losh start = 0; 3356e47a4f6SPoul-Henning Kamp #else 3366e47a4f6SPoul-Henning Kamp if (start < sc->membase && end > sc->membase) 3371efefb2dSWarner Losh printf("start (%lx) < sc->membase (%x)\n", start, sc->membase); 3386e47a4f6SPoul-Henning Kamp if (end > sc->memlimit) 3391efefb2dSWarner Losh printf("end (%lx) > sc->memlimit (%x)\n", end, sc->memlimit); 3406e47a4f6SPoul-Henning Kamp if (end < start) 3411efefb2dSWarner Losh printf("end (%lx) < start (%lx)\n", end, start); 3426e47a4f6SPoul-Henning Kamp #endif 34312b8c86eSWarner Losh } 344d0036d6eSWarner Losh if (!pcib_is_isa_mem(start) && 345d0036d6eSWarner Losh (((start < sc->membase) || (end > sc->memlimit)) && 346d0036d6eSWarner Losh ((start < sc->pmembase) || (end > sc->pmemlimit)))) { 34734428485SWarner Losh if (bootverbose) 34834428485SWarner Losh device_printf(dev, 34934428485SWarner Losh "device %s%d requested unsupported memory range " 35034428485SWarner Losh "0x%lx-0x%lx (decoding 0x%x-0x%x, 0x%x-0x%x)\n", 35134428485SWarner Losh device_get_name(child), device_get_unit(child), start, 35234428485SWarner Losh end, sc->membase, sc->memlimit, sc->pmembase, 35334428485SWarner Losh sc->pmemlimit); 3549efaa0aeSBrooks Davis #ifndef PCI_ALLOW_UNSUPPORTED_IO_RANGE 355bb0d0a8eSMike Smith return(NULL); 3569efaa0aeSBrooks Davis #endif 357bb0d0a8eSMike Smith } 3584fa59183SMike Smith if (bootverbose) 3594fa59183SMike Smith device_printf(sc->dev, "device %s%d requested decoded memory range 0x%lx-0x%lx\n", 3604fa59183SMike Smith device_get_name(child), device_get_unit(child), start, end); 3614fa59183SMike Smith break; 3624fa59183SMike Smith 363bb0d0a8eSMike Smith default: 3644fa59183SMike Smith break; 365bb0d0a8eSMike Smith } 366bb0d0a8eSMike Smith } 3674fa59183SMike Smith 368bb0d0a8eSMike Smith /* 369bb0d0a8eSMike Smith * Bridge is OK decoding this resource, so pass it up. 370bb0d0a8eSMike Smith */ 371bb0d0a8eSMike Smith return(bus_generic_alloc_resource(dev, child, type, rid, start, end, count, flags)); 372bb0d0a8eSMike Smith } 373bb0d0a8eSMike Smith 374bb0d0a8eSMike Smith /* 375bb0d0a8eSMike Smith * PCIB interface. 376bb0d0a8eSMike Smith */ 3776f0d5884SJohn Baldwin int 378bb0d0a8eSMike Smith pcib_maxslots(device_t dev) 379bb0d0a8eSMike Smith { 3804fa59183SMike Smith return(PCI_SLOTMAX); 381bb0d0a8eSMike Smith } 382bb0d0a8eSMike Smith 383bb0d0a8eSMike Smith /* 384bb0d0a8eSMike Smith * Since we are a child of a PCI bus, its parent must support the pcib interface. 385bb0d0a8eSMike Smith */ 3866f0d5884SJohn Baldwin u_int32_t 387bb0d0a8eSMike Smith pcib_read_config(device_t dev, int b, int s, int f, int reg, int width) 388bb0d0a8eSMike Smith { 389bb0d0a8eSMike Smith return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width)); 390bb0d0a8eSMike Smith } 391bb0d0a8eSMike Smith 3926f0d5884SJohn Baldwin void 393bb0d0a8eSMike Smith pcib_write_config(device_t dev, int b, int s, int f, int reg, u_int32_t val, int width) 394bb0d0a8eSMike Smith { 395bb0d0a8eSMike Smith PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width); 396bb0d0a8eSMike Smith } 397bb0d0a8eSMike Smith 398bb0d0a8eSMike Smith /* 399bb0d0a8eSMike Smith * Route an interrupt across a PCI bridge. 400bb0d0a8eSMike Smith */ 401bb0d0a8eSMike Smith static int 402bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin) 403bb0d0a8eSMike Smith { 404bb0d0a8eSMike Smith device_t bus; 405bb0d0a8eSMike Smith int parent_intpin; 406bb0d0a8eSMike Smith int intnum; 407bb0d0a8eSMike Smith 408bb0d0a8eSMike Smith /* 409bb0d0a8eSMike Smith * 410bb0d0a8eSMike Smith * The PCI standard defines a swizzle of the child-side device/intpin to 411bb0d0a8eSMike Smith * the parent-side intpin as follows. 412bb0d0a8eSMike Smith * 413bb0d0a8eSMike Smith * device = device on child bus 414bb0d0a8eSMike Smith * child_intpin = intpin on child bus slot (0-3) 415bb0d0a8eSMike Smith * parent_intpin = intpin on parent bus slot (0-3) 416bb0d0a8eSMike Smith * 417bb0d0a8eSMike Smith * parent_intpin = (device + child_intpin) % 4 418bb0d0a8eSMike Smith */ 419bb0d0a8eSMike Smith parent_intpin = (pci_get_slot(pcib) + (pin - 1)) % 4; 420bb0d0a8eSMike Smith 421bb0d0a8eSMike Smith /* 422bb0d0a8eSMike Smith * Our parent is a PCI bus. Its parent must export the pcib interface 423bb0d0a8eSMike Smith * which includes the ability to route interrupts. 424bb0d0a8eSMike Smith */ 425bb0d0a8eSMike Smith bus = device_get_parent(pcib); 426bb0d0a8eSMike Smith intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1); 4278046c4b9SMike Smith if (PCI_INTERRUPT_VALID(intnum)) { 428c6a121abSJohn Baldwin device_printf(pcib, "slot %d INT%c is routed to irq %d\n", 429c6a121abSJohn Baldwin pci_get_slot(dev), 'A' + pin - 1, intnum); 4308046c4b9SMike Smith } 431bb0d0a8eSMike Smith return(intnum); 432bb0d0a8eSMike Smith } 433