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> 4141ee9f1cSPoul-Henning Kamp #include <sys/module.h> 42bb0d0a8eSMike Smith #include <sys/bus.h> 43a8b354a8SWarner Losh #include <machine/bus.h> 44a8b354a8SWarner Losh #include <sys/rman.h> 451c54ff33SMatthew N. Dodd #include <sys/sysctl.h> 46bb0d0a8eSMike Smith 47bb0d0a8eSMike Smith #include <machine/resource.h> 48bb0d0a8eSMike Smith 4938d8c994SWarner Losh #include <dev/pci/pcivar.h> 5038d8c994SWarner Losh #include <dev/pci/pcireg.h> 5138d8c994SWarner Losh #include <dev/pci/pcib_private.h> 52bb0d0a8eSMike Smith 53bb0d0a8eSMike Smith #include "pcib_if.h" 54bb0d0a8eSMike Smith 55bb0d0a8eSMike Smith static int pcib_probe(device_t dev); 56bb0d0a8eSMike Smith 57bb0d0a8eSMike Smith static device_method_t pcib_methods[] = { 58bb0d0a8eSMike Smith /* Device interface */ 59bb0d0a8eSMike Smith DEVMETHOD(device_probe, pcib_probe), 60bb0d0a8eSMike Smith DEVMETHOD(device_attach, pcib_attach), 614e30440dSWarner Losh DEVMETHOD(device_detach, bus_generic_detach), 62bb0d0a8eSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 63bb0d0a8eSMike Smith DEVMETHOD(device_suspend, bus_generic_suspend), 64bb0d0a8eSMike Smith DEVMETHOD(device_resume, bus_generic_resume), 65bb0d0a8eSMike Smith 66bb0d0a8eSMike Smith /* Bus interface */ 67bb0d0a8eSMike Smith DEVMETHOD(bus_print_child, bus_generic_print_child), 68bb0d0a8eSMike Smith DEVMETHOD(bus_read_ivar, pcib_read_ivar), 69bb0d0a8eSMike Smith DEVMETHOD(bus_write_ivar, pcib_write_ivar), 70bb0d0a8eSMike Smith DEVMETHOD(bus_alloc_resource, pcib_alloc_resource), 71bb0d0a8eSMike Smith DEVMETHOD(bus_release_resource, bus_generic_release_resource), 72bb0d0a8eSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 73bb0d0a8eSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 74bb0d0a8eSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 75bb0d0a8eSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 76bb0d0a8eSMike Smith 77bb0d0a8eSMike Smith /* pcib interface */ 78bb0d0a8eSMike Smith DEVMETHOD(pcib_maxslots, pcib_maxslots), 79bb0d0a8eSMike Smith DEVMETHOD(pcib_read_config, pcib_read_config), 80bb0d0a8eSMike Smith DEVMETHOD(pcib_write_config, pcib_write_config), 81bb0d0a8eSMike Smith DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt), 829bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi), 839bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msi, pcib_release_msi), 849bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix), 859bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msix, pcib_release_msix), 86e706f7f0SJohn Baldwin DEVMETHOD(pcib_map_msi, pcib_map_msi), 87bb0d0a8eSMike Smith 88bb0d0a8eSMike Smith { 0, 0 } 89bb0d0a8eSMike Smith }; 90bb0d0a8eSMike Smith 9104dda605SJohn Baldwin static devclass_t pcib_devclass; 92bb0d0a8eSMike Smith 9304dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc)); 94bb0d0a8eSMike Smith DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, 0, 0); 95bb0d0a8eSMike Smith 96bb0d0a8eSMike Smith /* 97b0a2d4b8SWarner Losh * Is the prefetch window open (eg, can we allocate memory in it?) 98b0a2d4b8SWarner Losh */ 99b0a2d4b8SWarner Losh static int 100b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc) 101b0a2d4b8SWarner Losh { 102b0a2d4b8SWarner Losh return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit); 103b0a2d4b8SWarner Losh } 104b0a2d4b8SWarner Losh 105b0a2d4b8SWarner Losh /* 106b0a2d4b8SWarner Losh * Is the nonprefetch window open (eg, can we allocate memory in it?) 107b0a2d4b8SWarner Losh */ 108b0a2d4b8SWarner Losh static int 109b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc) 110b0a2d4b8SWarner Losh { 111b0a2d4b8SWarner Losh return (sc->membase > 0 && sc->membase < sc->memlimit); 112b0a2d4b8SWarner Losh } 113b0a2d4b8SWarner Losh 114b0a2d4b8SWarner Losh /* 115b0a2d4b8SWarner Losh * Is the io window open (eg, can we allocate ports in it?) 116b0a2d4b8SWarner Losh */ 117b0a2d4b8SWarner Losh static int 118b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc) 119b0a2d4b8SWarner Losh { 120b0a2d4b8SWarner Losh return (sc->iobase > 0 && sc->iobase < sc->iolimit); 121b0a2d4b8SWarner Losh } 122b0a2d4b8SWarner Losh 123b0a2d4b8SWarner Losh /* 124bb0d0a8eSMike Smith * Generic device interface 125bb0d0a8eSMike Smith */ 126bb0d0a8eSMike Smith static int 127bb0d0a8eSMike Smith pcib_probe(device_t dev) 128bb0d0a8eSMike Smith { 129bb0d0a8eSMike Smith if ((pci_get_class(dev) == PCIC_BRIDGE) && 130bb0d0a8eSMike Smith (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) { 131bb0d0a8eSMike Smith device_set_desc(dev, "PCI-PCI bridge"); 132bb0d0a8eSMike Smith return(-10000); 133bb0d0a8eSMike Smith } 134bb0d0a8eSMike Smith return(ENXIO); 135bb0d0a8eSMike Smith } 136bb0d0a8eSMike Smith 1376f0d5884SJohn Baldwin void 1386f0d5884SJohn Baldwin pcib_attach_common(device_t dev) 139bb0d0a8eSMike Smith { 140bb0d0a8eSMike Smith struct pcib_softc *sc; 141b0cb115fSWarner Losh uint8_t iolow; 142bb0d0a8eSMike Smith 143bb0d0a8eSMike Smith sc = device_get_softc(dev); 144bb0d0a8eSMike Smith sc->dev = dev; 145bb0d0a8eSMike Smith 1464fa59183SMike Smith /* 1474fa59183SMike Smith * Get current bridge configuration. 1484fa59183SMike Smith */ 1498983cfbfSMike Smith sc->command = pci_read_config(dev, PCIR_COMMAND, 1); 15055aaf894SMarius Strobl sc->domain = pci_get_domain(dev); 1514fa59183SMike Smith sc->secbus = pci_read_config(dev, PCIR_SECBUS_1, 1); 1524fa59183SMike Smith sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1); 1534fa59183SMike Smith sc->secstat = pci_read_config(dev, PCIR_SECSTAT_1, 2); 1544fa59183SMike Smith sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 1554fa59183SMike Smith sc->seclat = pci_read_config(dev, PCIR_SECLAT_1, 1); 1564fa59183SMike Smith 1574fa59183SMike Smith /* 1584fa59183SMike Smith * Determine current I/O decode. 1594fa59183SMike Smith */ 1608983cfbfSMike Smith if (sc->command & PCIM_CMD_PORTEN) { 1614fa59183SMike Smith iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1); 1624fa59183SMike Smith if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 1634fa59183SMike Smith sc->iobase = PCI_PPBIOBASE(pci_read_config(dev, PCIR_IOBASEH_1, 2), 1644fa59183SMike Smith pci_read_config(dev, PCIR_IOBASEL_1, 1)); 1654fa59183SMike Smith } else { 1664fa59183SMike Smith sc->iobase = PCI_PPBIOBASE(0, pci_read_config(dev, PCIR_IOBASEL_1, 1)); 1674fa59183SMike Smith } 1684fa59183SMike Smith 1694fa59183SMike Smith iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1); 1704fa59183SMike Smith if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 1714fa59183SMike Smith sc->iolimit = PCI_PPBIOLIMIT(pci_read_config(dev, PCIR_IOLIMITH_1, 2), 1724fa59183SMike Smith pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 1734fa59183SMike Smith } else { 1744fa59183SMike Smith sc->iolimit = PCI_PPBIOLIMIT(0, pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 1754fa59183SMike Smith } 1768983cfbfSMike Smith } 1774fa59183SMike Smith 1784fa59183SMike Smith /* 1794fa59183SMike Smith * Determine current memory decode. 1804fa59183SMike Smith */ 1818983cfbfSMike Smith if (sc->command & PCIM_CMD_MEMEN) { 1824fa59183SMike Smith sc->membase = PCI_PPBMEMBASE(0, pci_read_config(dev, PCIR_MEMBASE_1, 2)); 1834fa59183SMike Smith sc->memlimit = PCI_PPBMEMLIMIT(0, pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 184413d6375SWarner Losh sc->pmembase = PCI_PPBMEMBASE(pci_read_config(dev, PCIR_PMBASEH_1, 4), 1854fa59183SMike Smith pci_read_config(dev, PCIR_PMBASEL_1, 2)); 186413d6375SWarner Losh sc->pmemlimit = PCI_PPBMEMLIMIT(pci_read_config(dev, PCIR_PMLIMITH_1, 4), 1874fa59183SMike Smith pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 1888983cfbfSMike Smith } 1894fa59183SMike Smith 1904fa59183SMike Smith /* 1914fa59183SMike Smith * Quirk handling. 1924fa59183SMike Smith */ 1934fa59183SMike Smith switch (pci_get_devid(dev)) { 1944fa59183SMike Smith case 0x12258086: /* Intel 82454KX/GX (Orion) */ 1954fa59183SMike Smith { 196b0cb115fSWarner Losh uint8_t supbus; 1974fa59183SMike Smith 1984fa59183SMike Smith supbus = pci_read_config(dev, 0x41, 1); 1994fa59183SMike Smith if (supbus != 0xff) { 2004fa59183SMike Smith sc->secbus = supbus + 1; 2014fa59183SMike Smith sc->subbus = supbus + 1; 2024fa59183SMike Smith } 2034fa59183SMike Smith break; 2044fa59183SMike Smith } 2054fa59183SMike Smith 206e4b59fc5SWarner Losh /* 207e4b59fc5SWarner Losh * The i82380FB mobile docking controller is a PCI-PCI bridge, 208e4b59fc5SWarner Losh * and it is a subtractive bridge. However, the ProgIf is wrong 209e4b59fc5SWarner Losh * so the normal setting of PCIB_SUBTRACTIVE bit doesn't 210e4b59fc5SWarner Losh * happen. There's also a Toshiba bridge that behaves this 211e4b59fc5SWarner Losh * way. 212e4b59fc5SWarner Losh */ 213e4b59fc5SWarner Losh case 0x124b8086: /* Intel 82380FB Mobile */ 214e4b59fc5SWarner Losh case 0x060513d7: /* Toshiba ???? */ 215e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 216e4b59fc5SWarner Losh break; 217c94d6dbeSJung-uk Kim 218c94d6dbeSJung-uk Kim /* Compaq R3000 BIOS sets wrong subordinate bus number. */ 219c94d6dbeSJung-uk Kim case 0x00dd10de: 220c94d6dbeSJung-uk Kim { 221c94d6dbeSJung-uk Kim char *cp; 222c94d6dbeSJung-uk Kim 2231def0ca6SJung-uk Kim if ((cp = getenv("smbios.planar.maker")) == NULL) 224c94d6dbeSJung-uk Kim break; 2251def0ca6SJung-uk Kim if (strncmp(cp, "Compal", 6) != 0) { 2261def0ca6SJung-uk Kim freeenv(cp); 227c94d6dbeSJung-uk Kim break; 2281def0ca6SJung-uk Kim } 2291def0ca6SJung-uk Kim freeenv(cp); 2301def0ca6SJung-uk Kim if ((cp = getenv("smbios.planar.product")) == NULL) 2311def0ca6SJung-uk Kim break; 2321def0ca6SJung-uk Kim if (strncmp(cp, "08A0", 4) != 0) { 2331def0ca6SJung-uk Kim freeenv(cp); 2341def0ca6SJung-uk Kim break; 2351def0ca6SJung-uk Kim } 2361def0ca6SJung-uk Kim freeenv(cp); 237c94d6dbeSJung-uk Kim if (sc->subbus < 0xa) { 238c94d6dbeSJung-uk Kim pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1); 239c94d6dbeSJung-uk Kim sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1); 240c94d6dbeSJung-uk Kim } 241c94d6dbeSJung-uk Kim break; 242c94d6dbeSJung-uk Kim } 243e4b59fc5SWarner Losh } 244e4b59fc5SWarner Losh 24522bf1c7fSJohn Baldwin if (pci_msi_device_blacklisted(dev)) 24622bf1c7fSJohn Baldwin sc->flags |= PCIB_DISABLE_MSI; 24722bf1c7fSJohn Baldwin 248e4b59fc5SWarner Losh /* 249e4b59fc5SWarner Losh * Intel 815, 845 and other chipsets say they are PCI-PCI bridges, 250e4b59fc5SWarner Losh * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM, 251e4b59fc5SWarner Losh * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese. 252e4b59fc5SWarner Losh * This means they act as if they were subtractively decoding 253e4b59fc5SWarner Losh * bridges and pass all transactions. Mark them and real ProgIf 1 254e4b59fc5SWarner Losh * parts as subtractive. 255e4b59fc5SWarner Losh */ 256e4b59fc5SWarner Losh if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 || 257657d9f9fSJohn Baldwin pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE) 258e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 259e4b59fc5SWarner Losh 260bb0d0a8eSMike Smith if (bootverbose) { 26155aaf894SMarius Strobl device_printf(dev, " domain %d\n", sc->domain); 262bb0d0a8eSMike Smith device_printf(dev, " secondary bus %d\n", sc->secbus); 263bb0d0a8eSMike Smith device_printf(dev, " subordinate bus %d\n", sc->subbus); 264bb0d0a8eSMike Smith device_printf(dev, " I/O decode 0x%x-0x%x\n", sc->iobase, sc->iolimit); 265b0a2d4b8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 266b0a2d4b8SWarner Losh device_printf(dev, " memory decode 0x%jx-0x%jx\n", 267b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit); 268b0a2d4b8SWarner Losh if (pcib_is_prefetch_open(sc)) 269b0a2d4b8SWarner Losh device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 270b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 271b0a2d4b8SWarner Losh else 272b0a2d4b8SWarner Losh device_printf(dev, " no prefetched decode\n"); 273e4b59fc5SWarner Losh if (sc->flags & PCIB_SUBTRACTIVE) 274e4b59fc5SWarner Losh device_printf(dev, " Subtractively decoded bridge.\n"); 275bb0d0a8eSMike Smith } 276bb0d0a8eSMike Smith 277bb0d0a8eSMike Smith /* 278bb0d0a8eSMike Smith * XXX If the secondary bus number is zero, we should assign a bus number 279bb0d0a8eSMike Smith * since the BIOS hasn't, then initialise the bridge. 280bb0d0a8eSMike Smith */ 281bb0d0a8eSMike Smith 282bb0d0a8eSMike Smith /* 283bb0d0a8eSMike Smith * XXX If the subordinate bus number is less than the secondary bus number, 284bb0d0a8eSMike Smith * we should pick a better value. One sensible alternative would be to 285bb0d0a8eSMike Smith * pick 255; the only tradeoff here is that configuration transactions 286bb0d0a8eSMike Smith * would be more widely routed than absolutely necessary. 287bb0d0a8eSMike Smith */ 2886f0d5884SJohn Baldwin } 289bb0d0a8eSMike Smith 29038906aedSJohn Baldwin int 2916f0d5884SJohn Baldwin pcib_attach(device_t dev) 2926f0d5884SJohn Baldwin { 2936f0d5884SJohn Baldwin struct pcib_softc *sc; 2946f0d5884SJohn Baldwin device_t child; 2956f0d5884SJohn Baldwin 2966f0d5884SJohn Baldwin pcib_attach_common(dev); 2976f0d5884SJohn Baldwin sc = device_get_softc(dev); 298bb0d0a8eSMike Smith if (sc->secbus != 0) { 299cea0a895SJohn Baldwin child = device_add_child(dev, "pci", sc->secbus); 300bb0d0a8eSMike Smith if (child != NULL) 301bb0d0a8eSMike Smith return(bus_generic_attach(dev)); 302bb0d0a8eSMike Smith } 303bb0d0a8eSMike Smith 304bb0d0a8eSMike Smith /* no secondary bus; we should have fixed this */ 305bb0d0a8eSMike Smith return(0); 306bb0d0a8eSMike Smith } 307bb0d0a8eSMike Smith 3086f0d5884SJohn Baldwin int 309bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 310bb0d0a8eSMike Smith { 311bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 312bb0d0a8eSMike Smith 313bb0d0a8eSMike Smith switch (which) { 31455aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 31555aaf894SMarius Strobl *result = sc->domain; 31655aaf894SMarius Strobl return(0); 317bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 318bb0d0a8eSMike Smith *result = sc->secbus; 319bb0d0a8eSMike Smith return(0); 320bb0d0a8eSMike Smith } 321bb0d0a8eSMike Smith return(ENOENT); 322bb0d0a8eSMike Smith } 323bb0d0a8eSMike Smith 3246f0d5884SJohn Baldwin int 325bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 326bb0d0a8eSMike Smith { 327bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 328bb0d0a8eSMike Smith 329bb0d0a8eSMike Smith switch (which) { 33055aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 33155aaf894SMarius Strobl return(EINVAL); 332bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 333bb0d0a8eSMike Smith sc->secbus = value; 33455aaf894SMarius Strobl return(0); 335bb0d0a8eSMike Smith } 336bb0d0a8eSMike Smith return(ENOENT); 337bb0d0a8eSMike Smith } 338bb0d0a8eSMike Smith 339bb0d0a8eSMike Smith /* 340bb0d0a8eSMike Smith * We have to trap resource allocation requests and ensure that the bridge 341bb0d0a8eSMike Smith * is set up to, or capable of handling them. 342bb0d0a8eSMike Smith */ 3436f0d5884SJohn Baldwin struct resource * 344bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 345bb0d0a8eSMike Smith u_long start, u_long end, u_long count, u_int flags) 346bb0d0a8eSMike Smith { 347bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 34826043836SJohn Baldwin const char *name, *suffix; 349a8b354a8SWarner Losh int ok; 350bb0d0a8eSMike Smith 351bb0d0a8eSMike Smith /* 352bb0d0a8eSMike Smith * Fail the allocation for this range if it's not supported. 353bb0d0a8eSMike Smith */ 35426043836SJohn Baldwin name = device_get_nameunit(child); 35526043836SJohn Baldwin if (name == NULL) { 35626043836SJohn Baldwin name = ""; 35726043836SJohn Baldwin suffix = ""; 35826043836SJohn Baldwin } else 35926043836SJohn Baldwin suffix = " "; 360bb0d0a8eSMike Smith switch (type) { 361bb0d0a8eSMike Smith case SYS_RES_IOPORT: 362a8b354a8SWarner Losh ok = 0; 363e4b59fc5SWarner Losh if (!pcib_is_io_open(sc)) 364e4b59fc5SWarner Losh break; 365a8b354a8SWarner Losh ok = (start >= sc->iobase && end <= sc->iolimit); 366d98d9b12SMarcel Moolenaar 367d98d9b12SMarcel Moolenaar /* 368d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA I/O addresses when the 369d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 370d98d9b12SMarcel Moolenaar */ 371d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_ioport_range(start, end)) 372d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 373d98d9b12SMarcel Moolenaar 374e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 375a8b354a8SWarner Losh if (!ok) { 37612b8c86eSWarner Losh if (start < sc->iobase) 37712b8c86eSWarner Losh start = sc->iobase; 37812b8c86eSWarner Losh if (end > sc->iolimit) 37912b8c86eSWarner Losh end = sc->iolimit; 3802daa7a07SWarner Losh if (start < end) 3812daa7a07SWarner Losh ok = 1; 382a8b354a8SWarner Losh } 3831c54ff33SMatthew N. Dodd } else { 384e4b59fc5SWarner Losh ok = 1; 385cd8b53edSWarner Losh #if 1 386e4b59fc5SWarner Losh if (start < sc->iobase && end > sc->iolimit) { 387e4b59fc5SWarner Losh start = sc->iobase; 388e4b59fc5SWarner Losh end = sc->iolimit; 3891c54ff33SMatthew N. Dodd } 39070be3980SWarner Losh #endif 39112b8c86eSWarner Losh } 392a8b354a8SWarner Losh if (end < start) { 3932daa7a07SWarner Losh device_printf(dev, "ioport: end (%lx) < start (%lx)\n", 3942daa7a07SWarner Losh end, start); 395a8b354a8SWarner Losh start = 0; 396a8b354a8SWarner Losh end = 0; 397a8b354a8SWarner Losh ok = 0; 398a8b354a8SWarner Losh } 399a8b354a8SWarner Losh if (!ok) { 40026043836SJohn Baldwin device_printf(dev, "%s%srequested unsupported I/O " 401a8b354a8SWarner Losh "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n", 40226043836SJohn Baldwin name, suffix, start, end, sc->iobase, sc->iolimit); 403bb0d0a8eSMike Smith return (NULL); 404bb0d0a8eSMike Smith } 4054fa59183SMike Smith if (bootverbose) 4062daa7a07SWarner Losh device_printf(dev, 40726043836SJohn Baldwin "%s%srequested I/O range 0x%lx-0x%lx: in range\n", 40826043836SJohn Baldwin name, suffix, start, end); 409bb0d0a8eSMike Smith break; 410bb0d0a8eSMike Smith 411bb0d0a8eSMike Smith case SYS_RES_MEMORY: 412a8b354a8SWarner Losh ok = 0; 413a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 414a8b354a8SWarner Losh ok = ok || (start >= sc->membase && end <= sc->memlimit); 415a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) 416a8b354a8SWarner Losh ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit); 417d98d9b12SMarcel Moolenaar 418d98d9b12SMarcel Moolenaar /* 419d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA memory addresses when the 420d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 421d98d9b12SMarcel Moolenaar */ 422d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_memory_range(start, end)) 423d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 424d98d9b12SMarcel Moolenaar 425e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 426a8b354a8SWarner Losh if (!ok) { 427a8b354a8SWarner Losh ok = 1; 428a8b354a8SWarner Losh if (flags & RF_PREFETCHABLE) { 429a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 430a8b354a8SWarner Losh if (start < sc->pmembase) 431a8b354a8SWarner Losh start = sc->pmembase; 432a8b354a8SWarner Losh if (end > sc->pmemlimit) 433a8b354a8SWarner Losh end = sc->pmemlimit; 434a8b354a8SWarner Losh } else { 435a8b354a8SWarner Losh ok = 0; 436a8b354a8SWarner Losh } 437a8b354a8SWarner Losh } else { /* non-prefetchable */ 438a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 439a8b354a8SWarner Losh if (start < sc->membase) 44012b8c86eSWarner Losh start = sc->membase; 44112b8c86eSWarner Losh if (end > sc->memlimit) 44212b8c86eSWarner Losh end = sc->memlimit; 4431c54ff33SMatthew N. Dodd } else { 444a8b354a8SWarner Losh ok = 0; 445a8b354a8SWarner Losh } 446a8b354a8SWarner Losh } 447a8b354a8SWarner Losh } 448a8b354a8SWarner Losh } else if (!ok) { 449e4b59fc5SWarner Losh ok = 1; /* subtractive bridge: always ok */ 450cd8b53edSWarner Losh #if 1 451a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 452e4b59fc5SWarner Losh if (start < sc->membase && end > sc->memlimit) { 453e4b59fc5SWarner Losh start = sc->membase; 454e4b59fc5SWarner Losh end = sc->memlimit; 455e4b59fc5SWarner Losh } 456a8b354a8SWarner Losh } 457a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 458e4b59fc5SWarner Losh if (start < sc->pmembase && end > sc->pmemlimit) { 459e4b59fc5SWarner Losh start = sc->pmembase; 460e4b59fc5SWarner Losh end = sc->pmemlimit; 461a8b354a8SWarner Losh } 4621c54ff33SMatthew N. Dodd } 46370be3980SWarner Losh #endif 46412b8c86eSWarner Losh } 465a8b354a8SWarner Losh if (end < start) { 4662daa7a07SWarner Losh device_printf(dev, "memory: end (%lx) < start (%lx)\n", 4672daa7a07SWarner Losh end, start); 468a8b354a8SWarner Losh start = 0; 469a8b354a8SWarner Losh end = 0; 470a8b354a8SWarner Losh ok = 0; 471a8b354a8SWarner Losh } 472a8b354a8SWarner Losh if (!ok && bootverbose) 47334428485SWarner Losh device_printf(dev, 47426043836SJohn Baldwin "%s%srequested unsupported memory range %#lx-%#lx " 475b0a2d4b8SWarner Losh "(decoding %#jx-%#jx, %#jx-%#jx)\n", 47626043836SJohn Baldwin name, suffix, start, end, 477b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit, 478b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 479a8b354a8SWarner Losh if (!ok) 480bb0d0a8eSMike Smith return (NULL); 4814fa59183SMike Smith if (bootverbose) 48226043836SJohn Baldwin device_printf(dev,"%s%srequested memory range " 4832daa7a07SWarner Losh "0x%lx-0x%lx: good\n", 48426043836SJohn Baldwin name, suffix, start, end); 4854fa59183SMike Smith break; 4864fa59183SMike Smith 487bb0d0a8eSMike Smith default: 4884fa59183SMike Smith break; 489bb0d0a8eSMike Smith } 490bb0d0a8eSMike Smith /* 491bb0d0a8eSMike Smith * Bridge is OK decoding this resource, so pass it up. 492bb0d0a8eSMike Smith */ 4932daa7a07SWarner Losh return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 4942daa7a07SWarner Losh count, flags)); 495bb0d0a8eSMike Smith } 496bb0d0a8eSMike Smith 497bb0d0a8eSMike Smith /* 498bb0d0a8eSMike Smith * PCIB interface. 499bb0d0a8eSMike Smith */ 5006f0d5884SJohn Baldwin int 501bb0d0a8eSMike Smith pcib_maxslots(device_t dev) 502bb0d0a8eSMike Smith { 5034fa59183SMike Smith return(PCI_SLOTMAX); 504bb0d0a8eSMike Smith } 505bb0d0a8eSMike Smith 506bb0d0a8eSMike Smith /* 507bb0d0a8eSMike Smith * Since we are a child of a PCI bus, its parent must support the pcib interface. 508bb0d0a8eSMike Smith */ 509b0cb115fSWarner Losh uint32_t 510bb0d0a8eSMike Smith pcib_read_config(device_t dev, int b, int s, int f, int reg, int width) 511bb0d0a8eSMike Smith { 512bb0d0a8eSMike Smith return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width)); 513bb0d0a8eSMike Smith } 514bb0d0a8eSMike Smith 5156f0d5884SJohn Baldwin void 516b0cb115fSWarner Losh pcib_write_config(device_t dev, int b, int s, int f, int reg, uint32_t val, int width) 517bb0d0a8eSMike Smith { 518bb0d0a8eSMike Smith PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width); 519bb0d0a8eSMike Smith } 520bb0d0a8eSMike Smith 521bb0d0a8eSMike Smith /* 522bb0d0a8eSMike Smith * Route an interrupt across a PCI bridge. 523bb0d0a8eSMike Smith */ 5242c2d1d07SBenno Rice int 525bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin) 526bb0d0a8eSMike Smith { 527bb0d0a8eSMike Smith device_t bus; 528bb0d0a8eSMike Smith int parent_intpin; 529bb0d0a8eSMike Smith int intnum; 530bb0d0a8eSMike Smith 531bb0d0a8eSMike Smith /* 532bb0d0a8eSMike Smith * 533bb0d0a8eSMike Smith * The PCI standard defines a swizzle of the child-side device/intpin to 534bb0d0a8eSMike Smith * the parent-side intpin as follows. 535bb0d0a8eSMike Smith * 536bb0d0a8eSMike Smith * device = device on child bus 537bb0d0a8eSMike Smith * child_intpin = intpin on child bus slot (0-3) 538bb0d0a8eSMike Smith * parent_intpin = intpin on parent bus slot (0-3) 539bb0d0a8eSMike Smith * 540bb0d0a8eSMike Smith * parent_intpin = (device + child_intpin) % 4 541bb0d0a8eSMike Smith */ 542cdc95e1bSBernd Walter parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4; 543bb0d0a8eSMike Smith 544bb0d0a8eSMike Smith /* 545bb0d0a8eSMike Smith * Our parent is a PCI bus. Its parent must export the pcib interface 546bb0d0a8eSMike Smith * which includes the ability to route interrupts. 547bb0d0a8eSMike Smith */ 548bb0d0a8eSMike Smith bus = device_get_parent(pcib); 549bb0d0a8eSMike Smith intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1); 55039981fedSJohn Baldwin if (PCI_INTERRUPT_VALID(intnum) && bootverbose) { 551c6a121abSJohn Baldwin device_printf(pcib, "slot %d INT%c is routed to irq %d\n", 552c6a121abSJohn Baldwin pci_get_slot(dev), 'A' + pin - 1, intnum); 5538046c4b9SMike Smith } 554bb0d0a8eSMike Smith return(intnum); 555bb0d0a8eSMike Smith } 556b173edafSJohn Baldwin 557e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */ 5589bf4c9c1SJohn Baldwin int 5599bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs) 5609bf4c9c1SJohn Baldwin { 561bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 5629bf4c9c1SJohn Baldwin device_t bus; 5639bf4c9c1SJohn Baldwin 56422bf1c7fSJohn Baldwin if (sc->flags & PCIB_DISABLE_MSI) 56522bf1c7fSJohn Baldwin return (ENXIO); 5669bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 5679bf4c9c1SJohn Baldwin return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 5689bf4c9c1SJohn Baldwin irqs)); 5699bf4c9c1SJohn Baldwin } 5709bf4c9c1SJohn Baldwin 571e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */ 5729bf4c9c1SJohn Baldwin int 5739bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs) 5749bf4c9c1SJohn Baldwin { 5759bf4c9c1SJohn Baldwin device_t bus; 5769bf4c9c1SJohn Baldwin 5779bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 5789bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs)); 5799bf4c9c1SJohn Baldwin } 5809bf4c9c1SJohn Baldwin 5819bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */ 5829bf4c9c1SJohn Baldwin int 583e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq) 5849bf4c9c1SJohn Baldwin { 585bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 5869bf4c9c1SJohn Baldwin device_t bus; 5879bf4c9c1SJohn Baldwin 58822bf1c7fSJohn Baldwin if (sc->flags & PCIB_DISABLE_MSI) 58922bf1c7fSJohn Baldwin return (ENXIO); 5909bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 591e706f7f0SJohn Baldwin return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq)); 5925fe82bcaSJohn Baldwin } 5935fe82bcaSJohn Baldwin 5949bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */ 5959bf4c9c1SJohn Baldwin int 5969bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq) 5979bf4c9c1SJohn Baldwin { 5989bf4c9c1SJohn Baldwin device_t bus; 5999bf4c9c1SJohn Baldwin 6009bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 6019bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq)); 6029bf4c9c1SJohn Baldwin } 6039bf4c9c1SJohn Baldwin 604e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */ 605e706f7f0SJohn Baldwin int 606e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, 607e706f7f0SJohn Baldwin uint32_t *data) 608e706f7f0SJohn Baldwin { 609e706f7f0SJohn Baldwin device_t bus; 610e706f7f0SJohn Baldwin 611e706f7f0SJohn Baldwin bus = device_get_parent(pcib); 612e706f7f0SJohn Baldwin return (PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data)); 613e706f7f0SJohn Baldwin } 614e706f7f0SJohn Baldwin 615b173edafSJohn Baldwin /* 616b173edafSJohn Baldwin * Try to read the bus number of a host-PCI bridge using appropriate config 617b173edafSJohn Baldwin * registers. 618b173edafSJohn Baldwin */ 619b173edafSJohn Baldwin int 620b173edafSJohn Baldwin host_pcib_get_busno(pci_read_config_fn read_config, int bus, int slot, int func, 621b0cb115fSWarner Losh uint8_t *busnum) 622b173edafSJohn Baldwin { 623b0cb115fSWarner Losh uint32_t id; 624b173edafSJohn Baldwin 625b173edafSJohn Baldwin id = read_config(bus, slot, func, PCIR_DEVVENDOR, 4); 6261bbf2464SJohn Baldwin if (id == 0xffffffff) 627b173edafSJohn Baldwin return (0); 628b173edafSJohn Baldwin 629b173edafSJohn Baldwin switch (id) { 630b173edafSJohn Baldwin case 0x12258086: 631b173edafSJohn Baldwin /* Intel 824?? */ 632b173edafSJohn Baldwin /* XXX This is a guess */ 633b173edafSJohn Baldwin /* *busnum = read_config(bus, slot, func, 0x41, 1); */ 634b173edafSJohn Baldwin *busnum = bus; 635b173edafSJohn Baldwin break; 636b173edafSJohn Baldwin case 0x84c48086: 637b173edafSJohn Baldwin /* Intel 82454KX/GX (Orion) */ 638b173edafSJohn Baldwin *busnum = read_config(bus, slot, func, 0x4a, 1); 639b173edafSJohn Baldwin break; 640b173edafSJohn Baldwin case 0x84ca8086: 641b173edafSJohn Baldwin /* 642b173edafSJohn Baldwin * For the 450nx chipset, there is a whole bundle of 643b173edafSJohn Baldwin * things pretending to be host bridges. The MIOC will 644b173edafSJohn Baldwin * be seen first and isn't really a pci bridge (the 645b173edafSJohn Baldwin * actual busses are attached to the PXB's). We need to 646b173edafSJohn Baldwin * read the registers of the MIOC to figure out the 647b173edafSJohn Baldwin * bus numbers for the PXB channels. 648b173edafSJohn Baldwin * 649b173edafSJohn Baldwin * Since the MIOC doesn't have a pci bus attached, we 650b173edafSJohn Baldwin * pretend it wasn't there. 651b173edafSJohn Baldwin */ 652b173edafSJohn Baldwin return (0); 653b173edafSJohn Baldwin case 0x84cb8086: 654b173edafSJohn Baldwin switch (slot) { 655b173edafSJohn Baldwin case 0x12: 656b173edafSJohn Baldwin /* Intel 82454NX PXB#0, Bus#A */ 6571bbf2464SJohn Baldwin *busnum = read_config(bus, 0x10, func, 0xd0, 1); 658b173edafSJohn Baldwin break; 659b173edafSJohn Baldwin case 0x13: 660b173edafSJohn Baldwin /* Intel 82454NX PXB#0, Bus#B */ 6611bbf2464SJohn Baldwin *busnum = read_config(bus, 0x10, func, 0xd1, 1) + 1; 662b173edafSJohn Baldwin break; 663b173edafSJohn Baldwin case 0x14: 664b173edafSJohn Baldwin /* Intel 82454NX PXB#1, Bus#A */ 6651bbf2464SJohn Baldwin *busnum = read_config(bus, 0x10, func, 0xd3, 1); 666b173edafSJohn Baldwin break; 667b173edafSJohn Baldwin case 0x15: 668b173edafSJohn Baldwin /* Intel 82454NX PXB#1, Bus#B */ 6691bbf2464SJohn Baldwin *busnum = read_config(bus, 0x10, func, 0xd4, 1) + 1; 670b173edafSJohn Baldwin break; 671b173edafSJohn Baldwin } 672b173edafSJohn Baldwin break; 673b173edafSJohn Baldwin 674b173edafSJohn Baldwin /* ServerWorks -- vendor 0x1166 */ 675b173edafSJohn Baldwin case 0x00051166: 676b173edafSJohn Baldwin case 0x00061166: 677b173edafSJohn Baldwin case 0x00081166: 678b173edafSJohn Baldwin case 0x00091166: 679b173edafSJohn Baldwin case 0x00101166: 680b173edafSJohn Baldwin case 0x00111166: 681b173edafSJohn Baldwin case 0x00171166: 682b173edafSJohn Baldwin case 0x01011166: 683b173edafSJohn Baldwin case 0x010f1014: 684b173edafSJohn Baldwin case 0x02011166: 685b173edafSJohn Baldwin case 0x03021014: 686b173edafSJohn Baldwin *busnum = read_config(bus, slot, func, 0x44, 1); 687b173edafSJohn Baldwin break; 6885165a17dSJohn Baldwin 6895165a17dSJohn Baldwin /* Compaq/HP -- vendor 0x0e11 */ 6905165a17dSJohn Baldwin case 0x60100e11: 6915165a17dSJohn Baldwin *busnum = read_config(bus, slot, func, 0xc8, 1); 6925165a17dSJohn Baldwin break; 693b173edafSJohn Baldwin default: 694b173edafSJohn Baldwin /* Don't know how to read bus number. */ 695b173edafSJohn Baldwin return 0; 696b173edafSJohn Baldwin } 697b173edafSJohn Baldwin 698b173edafSJohn Baldwin return 1; 699b173edafSJohn Baldwin } 700