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), 855fe82bcaSJohn Baldwin DEVMETHOD(pcib_remap_msix, pcib_remap_msix), 869bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msix, pcib_release_msix), 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); 1504fa59183SMike Smith sc->secbus = pci_read_config(dev, PCIR_SECBUS_1, 1); 1514fa59183SMike Smith sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1); 1524fa59183SMike Smith sc->secstat = pci_read_config(dev, PCIR_SECSTAT_1, 2); 1534fa59183SMike Smith sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 1544fa59183SMike Smith sc->seclat = pci_read_config(dev, PCIR_SECLAT_1, 1); 1554fa59183SMike Smith 1564fa59183SMike Smith /* 1574fa59183SMike Smith * Determine current I/O decode. 1584fa59183SMike Smith */ 1598983cfbfSMike Smith if (sc->command & PCIM_CMD_PORTEN) { 1604fa59183SMike Smith iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1); 1614fa59183SMike Smith if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 1624fa59183SMike Smith sc->iobase = PCI_PPBIOBASE(pci_read_config(dev, PCIR_IOBASEH_1, 2), 1634fa59183SMike Smith pci_read_config(dev, PCIR_IOBASEL_1, 1)); 1644fa59183SMike Smith } else { 1654fa59183SMike Smith sc->iobase = PCI_PPBIOBASE(0, pci_read_config(dev, PCIR_IOBASEL_1, 1)); 1664fa59183SMike Smith } 1674fa59183SMike Smith 1684fa59183SMike Smith iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1); 1694fa59183SMike Smith if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 1704fa59183SMike Smith sc->iolimit = PCI_PPBIOLIMIT(pci_read_config(dev, PCIR_IOLIMITH_1, 2), 1714fa59183SMike Smith pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 1724fa59183SMike Smith } else { 1734fa59183SMike Smith sc->iolimit = PCI_PPBIOLIMIT(0, pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 1744fa59183SMike Smith } 1758983cfbfSMike Smith } 1764fa59183SMike Smith 1774fa59183SMike Smith /* 1784fa59183SMike Smith * Determine current memory decode. 1794fa59183SMike Smith */ 1808983cfbfSMike Smith if (sc->command & PCIM_CMD_MEMEN) { 1814fa59183SMike Smith sc->membase = PCI_PPBMEMBASE(0, pci_read_config(dev, PCIR_MEMBASE_1, 2)); 1824fa59183SMike Smith sc->memlimit = PCI_PPBMEMLIMIT(0, pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 183413d6375SWarner Losh sc->pmembase = PCI_PPBMEMBASE(pci_read_config(dev, PCIR_PMBASEH_1, 4), 1844fa59183SMike Smith pci_read_config(dev, PCIR_PMBASEL_1, 2)); 185413d6375SWarner Losh sc->pmemlimit = PCI_PPBMEMLIMIT(pci_read_config(dev, PCIR_PMLIMITH_1, 4), 1864fa59183SMike Smith pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 1878983cfbfSMike Smith } 1884fa59183SMike Smith 1894fa59183SMike Smith /* 1904fa59183SMike Smith * Quirk handling. 1914fa59183SMike Smith */ 1924fa59183SMike Smith switch (pci_get_devid(dev)) { 1934fa59183SMike Smith case 0x12258086: /* Intel 82454KX/GX (Orion) */ 1944fa59183SMike Smith { 195b0cb115fSWarner Losh uint8_t supbus; 1964fa59183SMike Smith 1974fa59183SMike Smith supbus = pci_read_config(dev, 0x41, 1); 1984fa59183SMike Smith if (supbus != 0xff) { 1994fa59183SMike Smith sc->secbus = supbus + 1; 2004fa59183SMike Smith sc->subbus = supbus + 1; 2014fa59183SMike Smith } 2024fa59183SMike Smith break; 2034fa59183SMike Smith } 2044fa59183SMike Smith 205e4b59fc5SWarner Losh /* 206e4b59fc5SWarner Losh * The i82380FB mobile docking controller is a PCI-PCI bridge, 207e4b59fc5SWarner Losh * and it is a subtractive bridge. However, the ProgIf is wrong 208e4b59fc5SWarner Losh * so the normal setting of PCIB_SUBTRACTIVE bit doesn't 209e4b59fc5SWarner Losh * happen. There's also a Toshiba bridge that behaves this 210e4b59fc5SWarner Losh * way. 211e4b59fc5SWarner Losh */ 212e4b59fc5SWarner Losh case 0x124b8086: /* Intel 82380FB Mobile */ 213e4b59fc5SWarner Losh case 0x060513d7: /* Toshiba ???? */ 214e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 215e4b59fc5SWarner Losh break; 216c94d6dbeSJung-uk Kim 217c94d6dbeSJung-uk Kim /* Compaq R3000 BIOS sets wrong subordinate bus number. */ 218c94d6dbeSJung-uk Kim case 0x00dd10de: 219c94d6dbeSJung-uk Kim { 220c94d6dbeSJung-uk Kim char *cp; 221c94d6dbeSJung-uk Kim 2221def0ca6SJung-uk Kim if ((cp = getenv("smbios.planar.maker")) == NULL) 223c94d6dbeSJung-uk Kim break; 2241def0ca6SJung-uk Kim if (strncmp(cp, "Compal", 6) != 0) { 2251def0ca6SJung-uk Kim freeenv(cp); 226c94d6dbeSJung-uk Kim break; 2271def0ca6SJung-uk Kim } 2281def0ca6SJung-uk Kim freeenv(cp); 2291def0ca6SJung-uk Kim if ((cp = getenv("smbios.planar.product")) == NULL) 2301def0ca6SJung-uk Kim break; 2311def0ca6SJung-uk Kim if (strncmp(cp, "08A0", 4) != 0) { 2321def0ca6SJung-uk Kim freeenv(cp); 2331def0ca6SJung-uk Kim break; 2341def0ca6SJung-uk Kim } 2351def0ca6SJung-uk Kim freeenv(cp); 236c94d6dbeSJung-uk Kim if (sc->subbus < 0xa) { 237c94d6dbeSJung-uk Kim pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1); 238c94d6dbeSJung-uk Kim sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1); 239c94d6dbeSJung-uk Kim } 240c94d6dbeSJung-uk Kim break; 241c94d6dbeSJung-uk Kim } 242e4b59fc5SWarner Losh } 243e4b59fc5SWarner Losh 24422bf1c7fSJohn Baldwin if (pci_msi_device_blacklisted(dev)) 24522bf1c7fSJohn Baldwin sc->flags |= PCIB_DISABLE_MSI; 24622bf1c7fSJohn Baldwin 247e4b59fc5SWarner Losh /* 248e4b59fc5SWarner Losh * Intel 815, 845 and other chipsets say they are PCI-PCI bridges, 249e4b59fc5SWarner Losh * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM, 250e4b59fc5SWarner Losh * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese. 251e4b59fc5SWarner Losh * This means they act as if they were subtractively decoding 252e4b59fc5SWarner Losh * bridges and pass all transactions. Mark them and real ProgIf 1 253e4b59fc5SWarner Losh * parts as subtractive. 254e4b59fc5SWarner Losh */ 255e4b59fc5SWarner Losh if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 || 256e4b59fc5SWarner Losh pci_read_config(dev, PCIR_PROGIF, 1) == 1) 257e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 258e4b59fc5SWarner Losh 259bb0d0a8eSMike Smith if (bootverbose) { 260bb0d0a8eSMike Smith device_printf(dev, " secondary bus %d\n", sc->secbus); 261bb0d0a8eSMike Smith device_printf(dev, " subordinate bus %d\n", sc->subbus); 262bb0d0a8eSMike Smith device_printf(dev, " I/O decode 0x%x-0x%x\n", sc->iobase, sc->iolimit); 263b0a2d4b8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 264b0a2d4b8SWarner Losh device_printf(dev, " memory decode 0x%jx-0x%jx\n", 265b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit); 266b0a2d4b8SWarner Losh if (pcib_is_prefetch_open(sc)) 267b0a2d4b8SWarner Losh device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 268b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 269b0a2d4b8SWarner Losh else 270b0a2d4b8SWarner Losh device_printf(dev, " no prefetched decode\n"); 271e4b59fc5SWarner Losh if (sc->flags & PCIB_SUBTRACTIVE) 272e4b59fc5SWarner Losh device_printf(dev, " Subtractively decoded bridge.\n"); 273bb0d0a8eSMike Smith } 274bb0d0a8eSMike Smith 275bb0d0a8eSMike Smith /* 276bb0d0a8eSMike Smith * XXX If the secondary bus number is zero, we should assign a bus number 277bb0d0a8eSMike Smith * since the BIOS hasn't, then initialise the bridge. 278bb0d0a8eSMike Smith */ 279bb0d0a8eSMike Smith 280bb0d0a8eSMike Smith /* 281bb0d0a8eSMike Smith * XXX If the subordinate bus number is less than the secondary bus number, 282bb0d0a8eSMike Smith * we should pick a better value. One sensible alternative would be to 283bb0d0a8eSMike Smith * pick 255; the only tradeoff here is that configuration transactions 284bb0d0a8eSMike Smith * would be more widely routed than absolutely necessary. 285bb0d0a8eSMike Smith */ 2866f0d5884SJohn Baldwin } 287bb0d0a8eSMike Smith 28838906aedSJohn Baldwin int 2896f0d5884SJohn Baldwin pcib_attach(device_t dev) 2906f0d5884SJohn Baldwin { 2916f0d5884SJohn Baldwin struct pcib_softc *sc; 2926f0d5884SJohn Baldwin device_t child; 2936f0d5884SJohn Baldwin 2946f0d5884SJohn Baldwin pcib_attach_common(dev); 2956f0d5884SJohn Baldwin sc = device_get_softc(dev); 296bb0d0a8eSMike Smith if (sc->secbus != 0) { 297cea0a895SJohn Baldwin child = device_add_child(dev, "pci", sc->secbus); 298bb0d0a8eSMike Smith if (child != NULL) 299bb0d0a8eSMike Smith return(bus_generic_attach(dev)); 300bb0d0a8eSMike Smith } 301bb0d0a8eSMike Smith 302bb0d0a8eSMike Smith /* no secondary bus; we should have fixed this */ 303bb0d0a8eSMike Smith return(0); 304bb0d0a8eSMike Smith } 305bb0d0a8eSMike Smith 3066f0d5884SJohn Baldwin int 307bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 308bb0d0a8eSMike Smith { 309bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 310bb0d0a8eSMike Smith 311bb0d0a8eSMike Smith switch (which) { 312bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 313bb0d0a8eSMike Smith *result = sc->secbus; 314bb0d0a8eSMike Smith return(0); 315bb0d0a8eSMike Smith } 316bb0d0a8eSMike Smith return(ENOENT); 317bb0d0a8eSMike Smith } 318bb0d0a8eSMike Smith 3196f0d5884SJohn Baldwin int 320bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 321bb0d0a8eSMike Smith { 322bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 323bb0d0a8eSMike Smith 324bb0d0a8eSMike Smith switch (which) { 325bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 326bb0d0a8eSMike Smith sc->secbus = value; 327bb0d0a8eSMike Smith break; 328bb0d0a8eSMike Smith } 329bb0d0a8eSMike Smith return(ENOENT); 330bb0d0a8eSMike Smith } 331bb0d0a8eSMike Smith 332bb0d0a8eSMike Smith /* 333bb0d0a8eSMike Smith * We have to trap resource allocation requests and ensure that the bridge 334bb0d0a8eSMike Smith * is set up to, or capable of handling them. 335bb0d0a8eSMike Smith */ 3366f0d5884SJohn Baldwin struct resource * 337bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 338bb0d0a8eSMike Smith u_long start, u_long end, u_long count, u_int flags) 339bb0d0a8eSMike Smith { 340bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 34126043836SJohn Baldwin const char *name, *suffix; 342a8b354a8SWarner Losh int ok; 343bb0d0a8eSMike Smith 344bb0d0a8eSMike Smith /* 345bb0d0a8eSMike Smith * Fail the allocation for this range if it's not supported. 346bb0d0a8eSMike Smith */ 34726043836SJohn Baldwin name = device_get_nameunit(child); 34826043836SJohn Baldwin if (name == NULL) { 34926043836SJohn Baldwin name = ""; 35026043836SJohn Baldwin suffix = ""; 35126043836SJohn Baldwin } else 35226043836SJohn Baldwin suffix = " "; 353bb0d0a8eSMike Smith switch (type) { 354bb0d0a8eSMike Smith case SYS_RES_IOPORT: 355a8b354a8SWarner Losh ok = 0; 356e4b59fc5SWarner Losh if (!pcib_is_io_open(sc)) 357e4b59fc5SWarner Losh break; 358a8b354a8SWarner Losh ok = (start >= sc->iobase && end <= sc->iolimit); 359d98d9b12SMarcel Moolenaar 360d98d9b12SMarcel Moolenaar /* 361d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA I/O addresses when the 362d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 363d98d9b12SMarcel Moolenaar */ 364d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_ioport_range(start, end)) 365d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 366d98d9b12SMarcel Moolenaar 367e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 368a8b354a8SWarner Losh if (!ok) { 36912b8c86eSWarner Losh if (start < sc->iobase) 37012b8c86eSWarner Losh start = sc->iobase; 37112b8c86eSWarner Losh if (end > sc->iolimit) 37212b8c86eSWarner Losh end = sc->iolimit; 3732daa7a07SWarner Losh if (start < end) 3742daa7a07SWarner Losh ok = 1; 375a8b354a8SWarner Losh } 3761c54ff33SMatthew N. Dodd } else { 377e4b59fc5SWarner Losh ok = 1; 378cd8b53edSWarner Losh #if 1 379e4b59fc5SWarner Losh if (start < sc->iobase && end > sc->iolimit) { 380e4b59fc5SWarner Losh start = sc->iobase; 381e4b59fc5SWarner Losh end = sc->iolimit; 3821c54ff33SMatthew N. Dodd } 38370be3980SWarner Losh #endif 38412b8c86eSWarner Losh } 385a8b354a8SWarner Losh if (end < start) { 3862daa7a07SWarner Losh device_printf(dev, "ioport: end (%lx) < start (%lx)\n", 3872daa7a07SWarner Losh end, start); 388a8b354a8SWarner Losh start = 0; 389a8b354a8SWarner Losh end = 0; 390a8b354a8SWarner Losh ok = 0; 391a8b354a8SWarner Losh } 392a8b354a8SWarner Losh if (!ok) { 39326043836SJohn Baldwin device_printf(dev, "%s%srequested unsupported I/O " 394a8b354a8SWarner Losh "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n", 39526043836SJohn Baldwin name, suffix, start, end, sc->iobase, sc->iolimit); 396bb0d0a8eSMike Smith return (NULL); 397bb0d0a8eSMike Smith } 3984fa59183SMike Smith if (bootverbose) 3992daa7a07SWarner Losh device_printf(dev, 40026043836SJohn Baldwin "%s%srequested I/O range 0x%lx-0x%lx: in range\n", 40126043836SJohn Baldwin name, suffix, start, end); 402bb0d0a8eSMike Smith break; 403bb0d0a8eSMike Smith 404bb0d0a8eSMike Smith case SYS_RES_MEMORY: 405a8b354a8SWarner Losh ok = 0; 406a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 407a8b354a8SWarner Losh ok = ok || (start >= sc->membase && end <= sc->memlimit); 408a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) 409a8b354a8SWarner Losh ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit); 410d98d9b12SMarcel Moolenaar 411d98d9b12SMarcel Moolenaar /* 412d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA memory addresses when the 413d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 414d98d9b12SMarcel Moolenaar */ 415d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_memory_range(start, end)) 416d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 417d98d9b12SMarcel Moolenaar 418e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 419a8b354a8SWarner Losh if (!ok) { 420a8b354a8SWarner Losh ok = 1; 421a8b354a8SWarner Losh if (flags & RF_PREFETCHABLE) { 422a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 423a8b354a8SWarner Losh if (start < sc->pmembase) 424a8b354a8SWarner Losh start = sc->pmembase; 425a8b354a8SWarner Losh if (end > sc->pmemlimit) 426a8b354a8SWarner Losh end = sc->pmemlimit; 427a8b354a8SWarner Losh } else { 428a8b354a8SWarner Losh ok = 0; 429a8b354a8SWarner Losh } 430a8b354a8SWarner Losh } else { /* non-prefetchable */ 431a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 432a8b354a8SWarner Losh if (start < sc->membase) 43312b8c86eSWarner Losh start = sc->membase; 43412b8c86eSWarner Losh if (end > sc->memlimit) 43512b8c86eSWarner Losh end = sc->memlimit; 4361c54ff33SMatthew N. Dodd } else { 437a8b354a8SWarner Losh ok = 0; 438a8b354a8SWarner Losh } 439a8b354a8SWarner Losh } 440a8b354a8SWarner Losh } 441a8b354a8SWarner Losh } else if (!ok) { 442e4b59fc5SWarner Losh ok = 1; /* subtractive bridge: always ok */ 443cd8b53edSWarner Losh #if 1 444a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 445e4b59fc5SWarner Losh if (start < sc->membase && end > sc->memlimit) { 446e4b59fc5SWarner Losh start = sc->membase; 447e4b59fc5SWarner Losh end = sc->memlimit; 448e4b59fc5SWarner Losh } 449a8b354a8SWarner Losh } 450a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 451e4b59fc5SWarner Losh if (start < sc->pmembase && end > sc->pmemlimit) { 452e4b59fc5SWarner Losh start = sc->pmembase; 453e4b59fc5SWarner Losh end = sc->pmemlimit; 454a8b354a8SWarner Losh } 4551c54ff33SMatthew N. Dodd } 45670be3980SWarner Losh #endif 45712b8c86eSWarner Losh } 458a8b354a8SWarner Losh if (end < start) { 4592daa7a07SWarner Losh device_printf(dev, "memory: end (%lx) < start (%lx)\n", 4602daa7a07SWarner Losh end, start); 461a8b354a8SWarner Losh start = 0; 462a8b354a8SWarner Losh end = 0; 463a8b354a8SWarner Losh ok = 0; 464a8b354a8SWarner Losh } 465a8b354a8SWarner Losh if (!ok && bootverbose) 46634428485SWarner Losh device_printf(dev, 46726043836SJohn Baldwin "%s%srequested unsupported memory range %#lx-%#lx " 468b0a2d4b8SWarner Losh "(decoding %#jx-%#jx, %#jx-%#jx)\n", 46926043836SJohn Baldwin name, suffix, start, end, 470b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit, 471b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 472a8b354a8SWarner Losh if (!ok) 473bb0d0a8eSMike Smith return (NULL); 4744fa59183SMike Smith if (bootverbose) 47526043836SJohn Baldwin device_printf(dev,"%s%srequested memory range " 4762daa7a07SWarner Losh "0x%lx-0x%lx: good\n", 47726043836SJohn Baldwin name, suffix, start, end); 4784fa59183SMike Smith break; 4794fa59183SMike Smith 480bb0d0a8eSMike Smith default: 4814fa59183SMike Smith break; 482bb0d0a8eSMike Smith } 483bb0d0a8eSMike Smith /* 484bb0d0a8eSMike Smith * Bridge is OK decoding this resource, so pass it up. 485bb0d0a8eSMike Smith */ 4862daa7a07SWarner Losh return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 4872daa7a07SWarner Losh count, flags)); 488bb0d0a8eSMike Smith } 489bb0d0a8eSMike Smith 490bb0d0a8eSMike Smith /* 491bb0d0a8eSMike Smith * PCIB interface. 492bb0d0a8eSMike Smith */ 4936f0d5884SJohn Baldwin int 494bb0d0a8eSMike Smith pcib_maxslots(device_t dev) 495bb0d0a8eSMike Smith { 4964fa59183SMike Smith return(PCI_SLOTMAX); 497bb0d0a8eSMike Smith } 498bb0d0a8eSMike Smith 499bb0d0a8eSMike Smith /* 500bb0d0a8eSMike Smith * Since we are a child of a PCI bus, its parent must support the pcib interface. 501bb0d0a8eSMike Smith */ 502b0cb115fSWarner Losh uint32_t 503bb0d0a8eSMike Smith pcib_read_config(device_t dev, int b, int s, int f, int reg, int width) 504bb0d0a8eSMike Smith { 505bb0d0a8eSMike Smith return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width)); 506bb0d0a8eSMike Smith } 507bb0d0a8eSMike Smith 5086f0d5884SJohn Baldwin void 509b0cb115fSWarner Losh pcib_write_config(device_t dev, int b, int s, int f, int reg, uint32_t val, int width) 510bb0d0a8eSMike Smith { 511bb0d0a8eSMike Smith PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width); 512bb0d0a8eSMike Smith } 513bb0d0a8eSMike Smith 514bb0d0a8eSMike Smith /* 515bb0d0a8eSMike Smith * Route an interrupt across a PCI bridge. 516bb0d0a8eSMike Smith */ 5172c2d1d07SBenno Rice int 518bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin) 519bb0d0a8eSMike Smith { 520bb0d0a8eSMike Smith device_t bus; 521bb0d0a8eSMike Smith int parent_intpin; 522bb0d0a8eSMike Smith int intnum; 523bb0d0a8eSMike Smith 524bb0d0a8eSMike Smith /* 525bb0d0a8eSMike Smith * 526bb0d0a8eSMike Smith * The PCI standard defines a swizzle of the child-side device/intpin to 527bb0d0a8eSMike Smith * the parent-side intpin as follows. 528bb0d0a8eSMike Smith * 529bb0d0a8eSMike Smith * device = device on child bus 530bb0d0a8eSMike Smith * child_intpin = intpin on child bus slot (0-3) 531bb0d0a8eSMike Smith * parent_intpin = intpin on parent bus slot (0-3) 532bb0d0a8eSMike Smith * 533bb0d0a8eSMike Smith * parent_intpin = (device + child_intpin) % 4 534bb0d0a8eSMike Smith */ 535cdc95e1bSBernd Walter parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4; 536bb0d0a8eSMike Smith 537bb0d0a8eSMike Smith /* 538bb0d0a8eSMike Smith * Our parent is a PCI bus. Its parent must export the pcib interface 539bb0d0a8eSMike Smith * which includes the ability to route interrupts. 540bb0d0a8eSMike Smith */ 541bb0d0a8eSMike Smith bus = device_get_parent(pcib); 542bb0d0a8eSMike Smith intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1); 54339981fedSJohn Baldwin if (PCI_INTERRUPT_VALID(intnum) && bootverbose) { 544c6a121abSJohn Baldwin device_printf(pcib, "slot %d INT%c is routed to irq %d\n", 545c6a121abSJohn Baldwin pci_get_slot(dev), 'A' + pin - 1, intnum); 5468046c4b9SMike Smith } 547bb0d0a8eSMike Smith return(intnum); 548bb0d0a8eSMike Smith } 549b173edafSJohn Baldwin 5509bf4c9c1SJohn Baldwin /* Pass request to alloc MSI messages up to the parent bridge. */ 5519bf4c9c1SJohn Baldwin int 5529bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs) 5539bf4c9c1SJohn Baldwin { 55422bf1c7fSJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 5559bf4c9c1SJohn Baldwin device_t bus; 5569bf4c9c1SJohn Baldwin 55722bf1c7fSJohn Baldwin if (sc->flags & PCIB_DISABLE_MSI) 55822bf1c7fSJohn Baldwin return (ENXIO); 5599bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 5609bf4c9c1SJohn Baldwin return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 5619bf4c9c1SJohn Baldwin irqs)); 5629bf4c9c1SJohn Baldwin } 5639bf4c9c1SJohn Baldwin 5649bf4c9c1SJohn Baldwin /* Pass request to release MSI messages up to the parent bridge. */ 5659bf4c9c1SJohn Baldwin int 5669bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs) 5679bf4c9c1SJohn Baldwin { 5689bf4c9c1SJohn Baldwin device_t bus; 5699bf4c9c1SJohn Baldwin 5709bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 5719bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs)); 5729bf4c9c1SJohn Baldwin } 5739bf4c9c1SJohn Baldwin 5749bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */ 5759bf4c9c1SJohn Baldwin int 5769bf4c9c1SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int index, int *irq) 5779bf4c9c1SJohn Baldwin { 57822bf1c7fSJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 5799bf4c9c1SJohn Baldwin device_t bus; 5809bf4c9c1SJohn Baldwin 58122bf1c7fSJohn Baldwin if (sc->flags & PCIB_DISABLE_MSI) 58222bf1c7fSJohn Baldwin return (ENXIO); 5839bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 5849bf4c9c1SJohn Baldwin return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, index, irq)); 5859bf4c9c1SJohn Baldwin } 5869bf4c9c1SJohn Baldwin 5875fe82bcaSJohn Baldwin /* Pass request to remap an MSI-X message up to the parent bridge. */ 5885fe82bcaSJohn Baldwin int 5895fe82bcaSJohn Baldwin pcib_remap_msix(device_t pcib, device_t dev, int index, int irq) 5905fe82bcaSJohn Baldwin { 5915fe82bcaSJohn Baldwin device_t bus; 5925fe82bcaSJohn Baldwin 5935fe82bcaSJohn Baldwin bus = device_get_parent(pcib); 5945fe82bcaSJohn Baldwin return (PCIB_REMAP_MSIX(device_get_parent(bus), dev, index, irq)); 5955fe82bcaSJohn Baldwin } 5965fe82bcaSJohn Baldwin 5979bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */ 5989bf4c9c1SJohn Baldwin int 5999bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq) 6009bf4c9c1SJohn Baldwin { 6019bf4c9c1SJohn Baldwin device_t bus; 6029bf4c9c1SJohn Baldwin 6039bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 6049bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq)); 6059bf4c9c1SJohn Baldwin } 6069bf4c9c1SJohn Baldwin 607b173edafSJohn Baldwin /* 608b173edafSJohn Baldwin * Try to read the bus number of a host-PCI bridge using appropriate config 609b173edafSJohn Baldwin * registers. 610b173edafSJohn Baldwin */ 611b173edafSJohn Baldwin int 612b173edafSJohn Baldwin host_pcib_get_busno(pci_read_config_fn read_config, int bus, int slot, int func, 613b0cb115fSWarner Losh uint8_t *busnum) 614b173edafSJohn Baldwin { 615b0cb115fSWarner Losh uint32_t id; 616b173edafSJohn Baldwin 617b173edafSJohn Baldwin id = read_config(bus, slot, func, PCIR_DEVVENDOR, 4); 6181bbf2464SJohn Baldwin if (id == 0xffffffff) 619b173edafSJohn Baldwin return (0); 620b173edafSJohn Baldwin 621b173edafSJohn Baldwin switch (id) { 622b173edafSJohn Baldwin case 0x12258086: 623b173edafSJohn Baldwin /* Intel 824?? */ 624b173edafSJohn Baldwin /* XXX This is a guess */ 625b173edafSJohn Baldwin /* *busnum = read_config(bus, slot, func, 0x41, 1); */ 626b173edafSJohn Baldwin *busnum = bus; 627b173edafSJohn Baldwin break; 628b173edafSJohn Baldwin case 0x84c48086: 629b173edafSJohn Baldwin /* Intel 82454KX/GX (Orion) */ 630b173edafSJohn Baldwin *busnum = read_config(bus, slot, func, 0x4a, 1); 631b173edafSJohn Baldwin break; 632b173edafSJohn Baldwin case 0x84ca8086: 633b173edafSJohn Baldwin /* 634b173edafSJohn Baldwin * For the 450nx chipset, there is a whole bundle of 635b173edafSJohn Baldwin * things pretending to be host bridges. The MIOC will 636b173edafSJohn Baldwin * be seen first and isn't really a pci bridge (the 637b173edafSJohn Baldwin * actual busses are attached to the PXB's). We need to 638b173edafSJohn Baldwin * read the registers of the MIOC to figure out the 639b173edafSJohn Baldwin * bus numbers for the PXB channels. 640b173edafSJohn Baldwin * 641b173edafSJohn Baldwin * Since the MIOC doesn't have a pci bus attached, we 642b173edafSJohn Baldwin * pretend it wasn't there. 643b173edafSJohn Baldwin */ 644b173edafSJohn Baldwin return (0); 645b173edafSJohn Baldwin case 0x84cb8086: 646b173edafSJohn Baldwin switch (slot) { 647b173edafSJohn Baldwin case 0x12: 648b173edafSJohn Baldwin /* Intel 82454NX PXB#0, Bus#A */ 6491bbf2464SJohn Baldwin *busnum = read_config(bus, 0x10, func, 0xd0, 1); 650b173edafSJohn Baldwin break; 651b173edafSJohn Baldwin case 0x13: 652b173edafSJohn Baldwin /* Intel 82454NX PXB#0, Bus#B */ 6531bbf2464SJohn Baldwin *busnum = read_config(bus, 0x10, func, 0xd1, 1) + 1; 654b173edafSJohn Baldwin break; 655b173edafSJohn Baldwin case 0x14: 656b173edafSJohn Baldwin /* Intel 82454NX PXB#1, Bus#A */ 6571bbf2464SJohn Baldwin *busnum = read_config(bus, 0x10, func, 0xd3, 1); 658b173edafSJohn Baldwin break; 659b173edafSJohn Baldwin case 0x15: 660b173edafSJohn Baldwin /* Intel 82454NX PXB#1, Bus#B */ 6611bbf2464SJohn Baldwin *busnum = read_config(bus, 0x10, func, 0xd4, 1) + 1; 662b173edafSJohn Baldwin break; 663b173edafSJohn Baldwin } 664b173edafSJohn Baldwin break; 665b173edafSJohn Baldwin 666b173edafSJohn Baldwin /* ServerWorks -- vendor 0x1166 */ 667b173edafSJohn Baldwin case 0x00051166: 668b173edafSJohn Baldwin case 0x00061166: 669b173edafSJohn Baldwin case 0x00081166: 670b173edafSJohn Baldwin case 0x00091166: 671b173edafSJohn Baldwin case 0x00101166: 672b173edafSJohn Baldwin case 0x00111166: 673b173edafSJohn Baldwin case 0x00171166: 674b173edafSJohn Baldwin case 0x01011166: 675b173edafSJohn Baldwin case 0x010f1014: 676b173edafSJohn Baldwin case 0x02011166: 677b173edafSJohn Baldwin case 0x03021014: 678b173edafSJohn Baldwin *busnum = read_config(bus, slot, func, 0x44, 1); 679b173edafSJohn Baldwin break; 6805165a17dSJohn Baldwin 6815165a17dSJohn Baldwin /* Compaq/HP -- vendor 0x0e11 */ 6825165a17dSJohn Baldwin case 0x60100e11: 6835165a17dSJohn Baldwin *busnum = read_config(bus, slot, func, 0xc8, 1); 6845165a17dSJohn Baldwin break; 685b173edafSJohn Baldwin default: 686b173edafSJohn Baldwin /* Don't know how to read bus number. */ 687b173edafSJohn Baldwin return 0; 688b173edafSJohn Baldwin } 689b173edafSJohn Baldwin 690b173edafSJohn Baldwin return 1; 691b173edafSJohn Baldwin } 692