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 3882cb5c3bSJohn Baldwin #include "opt_pci.h" 3982cb5c3bSJohn Baldwin 40bb0d0a8eSMike Smith #include <sys/param.h> 41bb0d0a8eSMike Smith #include <sys/bus.h> 4283c41143SJohn Baldwin #include <sys/kernel.h> 4383c41143SJohn Baldwin #include <sys/malloc.h> 4483c41143SJohn Baldwin #include <sys/module.h> 45a8b354a8SWarner Losh #include <sys/rman.h> 461c54ff33SMatthew N. Dodd #include <sys/sysctl.h> 4783c41143SJohn Baldwin #include <sys/systm.h> 4882cb5c3bSJohn Baldwin #include <sys/taskqueue.h> 49bb0d0a8eSMike Smith 5038d8c994SWarner Losh #include <dev/pci/pcivar.h> 5138d8c994SWarner Losh #include <dev/pci/pcireg.h> 5262508c53SJohn Baldwin #include <dev/pci/pci_private.h> 5338d8c994SWarner Losh #include <dev/pci/pcib_private.h> 54bb0d0a8eSMike Smith 55bb0d0a8eSMike Smith #include "pcib_if.h" 56bb0d0a8eSMike Smith 57bb0d0a8eSMike Smith static int pcib_probe(device_t dev); 58e36af292SJung-uk Kim static int pcib_suspend(device_t dev); 59e36af292SJung-uk Kim static int pcib_resume(device_t dev); 6062508c53SJohn Baldwin static int pcib_power_for_sleep(device_t pcib, device_t dev, 6162508c53SJohn Baldwin int *pstate); 62*d7be980dSAndrew Turner static int pcib_ari_get_id(device_t pcib, device_t dev, 63*d7be980dSAndrew Turner enum pci_id_type type, uintptr_t *id); 6455d3ea17SRyan Stone static uint32_t pcib_read_config(device_t dev, u_int b, u_int s, 6555d3ea17SRyan Stone u_int f, u_int reg, int width); 6655d3ea17SRyan Stone static void pcib_write_config(device_t dev, u_int b, u_int s, 6755d3ea17SRyan Stone u_int f, u_int reg, uint32_t val, int width); 6855d3ea17SRyan Stone static int pcib_ari_maxslots(device_t dev); 6955d3ea17SRyan Stone static int pcib_ari_maxfuncs(device_t dev); 7055d3ea17SRyan Stone static int pcib_try_enable_ari(device_t pcib, device_t dev); 712397d2d8SRyan Stone static int pcib_ari_enabled(device_t pcib); 722397d2d8SRyan Stone static void pcib_ari_decode_rid(device_t pcib, uint16_t rid, 732397d2d8SRyan Stone int *bus, int *slot, int *func); 7482cb5c3bSJohn Baldwin #ifdef PCI_HP 7582cb5c3bSJohn Baldwin static void pcib_pcie_ab_timeout(void *arg); 7682cb5c3bSJohn Baldwin static void pcib_pcie_cc_timeout(void *arg); 7782cb5c3bSJohn Baldwin static void pcib_pcie_dll_timeout(void *arg); 7882cb5c3bSJohn Baldwin #endif 79bb0d0a8eSMike Smith 80bb0d0a8eSMike Smith static device_method_t pcib_methods[] = { 81bb0d0a8eSMike Smith /* Device interface */ 82bb0d0a8eSMike Smith DEVMETHOD(device_probe, pcib_probe), 83bb0d0a8eSMike Smith DEVMETHOD(device_attach, pcib_attach), 844e30440dSWarner Losh DEVMETHOD(device_detach, bus_generic_detach), 85bb0d0a8eSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 86e36af292SJung-uk Kim DEVMETHOD(device_suspend, pcib_suspend), 87e36af292SJung-uk Kim DEVMETHOD(device_resume, pcib_resume), 88bb0d0a8eSMike Smith 89bb0d0a8eSMike Smith /* Bus interface */ 9082cb5c3bSJohn Baldwin DEVMETHOD(bus_child_present, pcib_child_present), 91bb0d0a8eSMike Smith DEVMETHOD(bus_read_ivar, pcib_read_ivar), 92bb0d0a8eSMike Smith DEVMETHOD(bus_write_ivar, pcib_write_ivar), 93bb0d0a8eSMike Smith DEVMETHOD(bus_alloc_resource, pcib_alloc_resource), 9483c41143SJohn Baldwin #ifdef NEW_PCIB 9583c41143SJohn Baldwin DEVMETHOD(bus_adjust_resource, pcib_adjust_resource), 9683c41143SJohn Baldwin DEVMETHOD(bus_release_resource, pcib_release_resource), 9783c41143SJohn Baldwin #else 98d2c9344fSJohn Baldwin DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 99bb0d0a8eSMike Smith DEVMETHOD(bus_release_resource, bus_generic_release_resource), 10083c41143SJohn Baldwin #endif 101bb0d0a8eSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 102bb0d0a8eSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 103bb0d0a8eSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 104bb0d0a8eSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 105bb0d0a8eSMike Smith 106bb0d0a8eSMike Smith /* pcib interface */ 10755d3ea17SRyan Stone DEVMETHOD(pcib_maxslots, pcib_ari_maxslots), 10855d3ea17SRyan Stone DEVMETHOD(pcib_maxfuncs, pcib_ari_maxfuncs), 109bb0d0a8eSMike Smith DEVMETHOD(pcib_read_config, pcib_read_config), 110bb0d0a8eSMike Smith DEVMETHOD(pcib_write_config, pcib_write_config), 111bb0d0a8eSMike Smith DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt), 1129bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi), 1139bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msi, pcib_release_msi), 1149bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix), 1159bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msix, pcib_release_msix), 116e706f7f0SJohn Baldwin DEVMETHOD(pcib_map_msi, pcib_map_msi), 11762508c53SJohn Baldwin DEVMETHOD(pcib_power_for_sleep, pcib_power_for_sleep), 118*d7be980dSAndrew Turner DEVMETHOD(pcib_get_id, pcib_ari_get_id), 11955d3ea17SRyan Stone DEVMETHOD(pcib_try_enable_ari, pcib_try_enable_ari), 1202397d2d8SRyan Stone DEVMETHOD(pcib_ari_enabled, pcib_ari_enabled), 1212397d2d8SRyan Stone DEVMETHOD(pcib_decode_rid, pcib_ari_decode_rid), 122bb0d0a8eSMike Smith 1234b7ec270SMarius Strobl DEVMETHOD_END 124bb0d0a8eSMike Smith }; 125bb0d0a8eSMike Smith 12604dda605SJohn Baldwin static devclass_t pcib_devclass; 127bb0d0a8eSMike Smith 12804dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc)); 12968e9cbd3SMarius Strobl DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, NULL, NULL); 130bb0d0a8eSMike Smith 13183c41143SJohn Baldwin #ifdef NEW_PCIB 1320070c94bSJohn Baldwin SYSCTL_DECL(_hw_pci); 1330070c94bSJohn Baldwin 1340070c94bSJohn Baldwin static int pci_clear_pcib; 1350070c94bSJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0, 1360070c94bSJohn Baldwin "Clear firmware-assigned resources for PCI-PCI bridge I/O windows."); 13783c41143SJohn Baldwin 13883c41143SJohn Baldwin /* 13983c41143SJohn Baldwin * Is a resource from a child device sub-allocated from one of our 14083c41143SJohn Baldwin * resource managers? 14183c41143SJohn Baldwin */ 14283c41143SJohn Baldwin static int 14383c41143SJohn Baldwin pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r) 14483c41143SJohn Baldwin { 14583c41143SJohn Baldwin 14683c41143SJohn Baldwin switch (type) { 1474edef187SJohn Baldwin #ifdef PCI_RES_BUS 1484edef187SJohn Baldwin case PCI_RES_BUS: 1494edef187SJohn Baldwin return (rman_is_region_manager(r, &sc->bus.rman)); 1504edef187SJohn Baldwin #endif 15183c41143SJohn Baldwin case SYS_RES_IOPORT: 15283c41143SJohn Baldwin return (rman_is_region_manager(r, &sc->io.rman)); 15383c41143SJohn Baldwin case SYS_RES_MEMORY: 15483c41143SJohn Baldwin /* Prefetchable resources may live in either memory rman. */ 15583c41143SJohn Baldwin if (rman_get_flags(r) & RF_PREFETCHABLE && 15683c41143SJohn Baldwin rman_is_region_manager(r, &sc->pmem.rman)) 15783c41143SJohn Baldwin return (1); 15883c41143SJohn Baldwin return (rman_is_region_manager(r, &sc->mem.rman)); 15983c41143SJohn Baldwin } 16083c41143SJohn Baldwin return (0); 16183c41143SJohn Baldwin } 16283c41143SJohn Baldwin 16383c41143SJohn Baldwin static int 16483c41143SJohn Baldwin pcib_is_window_open(struct pcib_window *pw) 16583c41143SJohn Baldwin { 16683c41143SJohn Baldwin 16783c41143SJohn Baldwin return (pw->valid && pw->base < pw->limit); 16883c41143SJohn Baldwin } 16983c41143SJohn Baldwin 17083c41143SJohn Baldwin /* 17183c41143SJohn Baldwin * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and 17283c41143SJohn Baldwin * handle for the resource, we could pass RF_ACTIVE up to the PCI bus 17383c41143SJohn Baldwin * when allocating the resource windows and rely on the PCI bus driver 17483c41143SJohn Baldwin * to do this for us. 17583c41143SJohn Baldwin */ 17683c41143SJohn Baldwin static void 17783c41143SJohn Baldwin pcib_activate_window(struct pcib_softc *sc, int type) 17883c41143SJohn Baldwin { 17983c41143SJohn Baldwin 18083c41143SJohn Baldwin PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type); 18183c41143SJohn Baldwin } 18283c41143SJohn Baldwin 18383c41143SJohn Baldwin static void 18483c41143SJohn Baldwin pcib_write_windows(struct pcib_softc *sc, int mask) 18583c41143SJohn Baldwin { 18683c41143SJohn Baldwin device_t dev; 18783c41143SJohn Baldwin uint32_t val; 18883c41143SJohn Baldwin 18983c41143SJohn Baldwin dev = sc->dev; 19083c41143SJohn Baldwin if (sc->io.valid && mask & WIN_IO) { 19183c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 19283c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 19383c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEH_1, 19483c41143SJohn Baldwin sc->io.base >> 16, 2); 19583c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITH_1, 19683c41143SJohn Baldwin sc->io.limit >> 16, 2); 19783c41143SJohn Baldwin } 19883c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1); 19983c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1); 20083c41143SJohn Baldwin } 20183c41143SJohn Baldwin 20283c41143SJohn Baldwin if (mask & WIN_MEM) { 20383c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2); 20483c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2); 20583c41143SJohn Baldwin } 20683c41143SJohn Baldwin 20783c41143SJohn Baldwin if (sc->pmem.valid && mask & WIN_PMEM) { 20883c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 20983c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 21083c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEH_1, 21183c41143SJohn Baldwin sc->pmem.base >> 32, 4); 21283c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITH_1, 21383c41143SJohn Baldwin sc->pmem.limit >> 32, 4); 21483c41143SJohn Baldwin } 21583c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2); 21683c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2); 21783c41143SJohn Baldwin } 21883c41143SJohn Baldwin } 21983c41143SJohn Baldwin 220c825d4dcSJohn Baldwin /* 221c825d4dcSJohn Baldwin * This is used to reject I/O port allocations that conflict with an 222c825d4dcSJohn Baldwin * ISA alias range. 223c825d4dcSJohn Baldwin */ 224c825d4dcSJohn Baldwin static int 2252dd1bdf1SJustin Hibbits pcib_is_isa_range(struct pcib_softc *sc, rman_res_t start, rman_res_t end, 2262dd1bdf1SJustin Hibbits rman_res_t count) 227c825d4dcSJohn Baldwin { 2282dd1bdf1SJustin Hibbits rman_res_t next_alias; 229c825d4dcSJohn Baldwin 230c825d4dcSJohn Baldwin if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE)) 231c825d4dcSJohn Baldwin return (0); 232c825d4dcSJohn Baldwin 233c825d4dcSJohn Baldwin /* Only check fixed ranges for overlap. */ 234c825d4dcSJohn Baldwin if (start + count - 1 != end) 235c825d4dcSJohn Baldwin return (0); 236c825d4dcSJohn Baldwin 237c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */ 238c825d4dcSJohn Baldwin if (start >= 65536) 239c825d4dcSJohn Baldwin return (0); 240c825d4dcSJohn Baldwin 241c825d4dcSJohn Baldwin /* Check for overlap with 0x000 - 0x0ff as a special case. */ 242c825d4dcSJohn Baldwin if (start < 0x100) 243c825d4dcSJohn Baldwin goto alias; 244c825d4dcSJohn Baldwin 245c825d4dcSJohn Baldwin /* 246c825d4dcSJohn Baldwin * If the start address is an alias, the range is an alias. 247c825d4dcSJohn Baldwin * Otherwise, compute the start of the next alias range and 248c825d4dcSJohn Baldwin * check if it is before the end of the candidate range. 249c825d4dcSJohn Baldwin */ 250c825d4dcSJohn Baldwin if ((start & 0x300) != 0) 251c825d4dcSJohn Baldwin goto alias; 252c825d4dcSJohn Baldwin next_alias = (start & ~0x3fful) | 0x100; 253c825d4dcSJohn Baldwin if (next_alias <= end) 254c825d4dcSJohn Baldwin goto alias; 255c825d4dcSJohn Baldwin return (0); 256c825d4dcSJohn Baldwin 257c825d4dcSJohn Baldwin alias: 258c825d4dcSJohn Baldwin if (bootverbose) 259c825d4dcSJohn Baldwin device_printf(sc->dev, 260da1b038aSJustin Hibbits "I/O range %#jx-%#jx overlaps with an ISA alias\n", start, 261c825d4dcSJohn Baldwin end); 262c825d4dcSJohn Baldwin return (1); 263c825d4dcSJohn Baldwin } 264c825d4dcSJohn Baldwin 265c825d4dcSJohn Baldwin static void 266c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res, 267c825d4dcSJohn Baldwin int count) 268c825d4dcSJohn Baldwin { 269c825d4dcSJohn Baldwin struct resource **newarray; 270c825d4dcSJohn Baldwin int error, i; 271c825d4dcSJohn Baldwin 272c825d4dcSJohn Baldwin newarray = malloc(sizeof(struct resource *) * (w->count + count), 273c825d4dcSJohn Baldwin M_DEVBUF, M_WAITOK); 274c825d4dcSJohn Baldwin if (w->res != NULL) 275c825d4dcSJohn Baldwin bcopy(w->res, newarray, sizeof(struct resource *) * w->count); 276c825d4dcSJohn Baldwin bcopy(res, newarray + w->count, sizeof(struct resource *) * count); 277c825d4dcSJohn Baldwin free(w->res, M_DEVBUF); 278c825d4dcSJohn Baldwin w->res = newarray; 279c825d4dcSJohn Baldwin w->count += count; 280c825d4dcSJohn Baldwin 281c825d4dcSJohn Baldwin for (i = 0; i < count; i++) { 282c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, rman_get_start(res[i]), 283c825d4dcSJohn Baldwin rman_get_end(res[i])); 284c825d4dcSJohn Baldwin if (error) 285c825d4dcSJohn Baldwin panic("Failed to add resource to rman"); 286c825d4dcSJohn Baldwin } 287c825d4dcSJohn Baldwin } 288c825d4dcSJohn Baldwin 2892dd1bdf1SJustin Hibbits typedef void (nonisa_callback)(rman_res_t start, rman_res_t end, void *arg); 290c825d4dcSJohn Baldwin 291c825d4dcSJohn Baldwin static void 2922dd1bdf1SJustin Hibbits pcib_walk_nonisa_ranges(rman_res_t start, rman_res_t end, nonisa_callback *cb, 293c825d4dcSJohn Baldwin void *arg) 294c825d4dcSJohn Baldwin { 2952dd1bdf1SJustin Hibbits rman_res_t next_end; 296c825d4dcSJohn Baldwin 297c825d4dcSJohn Baldwin /* 298c825d4dcSJohn Baldwin * If start is within an ISA alias range, move up to the start 299c825d4dcSJohn Baldwin * of the next non-alias range. As a special case, addresses 300c825d4dcSJohn Baldwin * in the range 0x000 - 0x0ff should also be skipped since 301c825d4dcSJohn Baldwin * those are used for various system I/O devices in ISA 302c825d4dcSJohn Baldwin * systems. 303c825d4dcSJohn Baldwin */ 304c825d4dcSJohn Baldwin if (start <= 65535) { 305c825d4dcSJohn Baldwin if (start < 0x100 || (start & 0x300) != 0) { 306c825d4dcSJohn Baldwin start &= ~0x3ff; 307c825d4dcSJohn Baldwin start += 0x400; 308c825d4dcSJohn Baldwin } 309c825d4dcSJohn Baldwin } 310c825d4dcSJohn Baldwin 311c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */ 312c825d4dcSJohn Baldwin while (start <= MIN(end, 65535)) { 313c825d4dcSJohn Baldwin next_end = MIN(start | 0xff, end); 314c825d4dcSJohn Baldwin cb(start, next_end, arg); 315c825d4dcSJohn Baldwin start += 0x400; 316c825d4dcSJohn Baldwin } 317c825d4dcSJohn Baldwin 318c825d4dcSJohn Baldwin if (start <= end) 319c825d4dcSJohn Baldwin cb(start, end, arg); 320c825d4dcSJohn Baldwin } 321c825d4dcSJohn Baldwin 322c825d4dcSJohn Baldwin static void 3232dd1bdf1SJustin Hibbits count_ranges(rman_res_t start, rman_res_t end, void *arg) 324c825d4dcSJohn Baldwin { 325c825d4dcSJohn Baldwin int *countp; 326c825d4dcSJohn Baldwin 327c825d4dcSJohn Baldwin countp = arg; 328c825d4dcSJohn Baldwin (*countp)++; 329c825d4dcSJohn Baldwin } 330c825d4dcSJohn Baldwin 331c825d4dcSJohn Baldwin struct alloc_state { 332c825d4dcSJohn Baldwin struct resource **res; 333c825d4dcSJohn Baldwin struct pcib_softc *sc; 334c825d4dcSJohn Baldwin int count, error; 335c825d4dcSJohn Baldwin }; 336c825d4dcSJohn Baldwin 337c825d4dcSJohn Baldwin static void 3382dd1bdf1SJustin Hibbits alloc_ranges(rman_res_t start, rman_res_t end, void *arg) 339c825d4dcSJohn Baldwin { 340c825d4dcSJohn Baldwin struct alloc_state *as; 341c825d4dcSJohn Baldwin struct pcib_window *w; 342c825d4dcSJohn Baldwin int rid; 343c825d4dcSJohn Baldwin 344c825d4dcSJohn Baldwin as = arg; 345c825d4dcSJohn Baldwin if (as->error != 0) 346c825d4dcSJohn Baldwin return; 347c825d4dcSJohn Baldwin 348c825d4dcSJohn Baldwin w = &as->sc->io; 349c825d4dcSJohn Baldwin rid = w->reg; 350c825d4dcSJohn Baldwin if (bootverbose) 351c825d4dcSJohn Baldwin device_printf(as->sc->dev, 352da1b038aSJustin Hibbits "allocating non-ISA range %#jx-%#jx\n", start, end); 353c825d4dcSJohn Baldwin as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT, 354c825d4dcSJohn Baldwin &rid, start, end, end - start + 1, 0); 355c825d4dcSJohn Baldwin if (as->res[as->count] == NULL) 356c825d4dcSJohn Baldwin as->error = ENXIO; 357c825d4dcSJohn Baldwin else 358c825d4dcSJohn Baldwin as->count++; 359c825d4dcSJohn Baldwin } 360c825d4dcSJohn Baldwin 361c825d4dcSJohn Baldwin static int 3622dd1bdf1SJustin Hibbits pcib_alloc_nonisa_ranges(struct pcib_softc *sc, rman_res_t start, rman_res_t end) 363c825d4dcSJohn Baldwin { 364c825d4dcSJohn Baldwin struct alloc_state as; 365c825d4dcSJohn Baldwin int i, new_count; 366c825d4dcSJohn Baldwin 367c825d4dcSJohn Baldwin /* First, see how many ranges we need. */ 368c825d4dcSJohn Baldwin new_count = 0; 369c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count); 370c825d4dcSJohn Baldwin 371c825d4dcSJohn Baldwin /* Second, allocate the ranges. */ 372c825d4dcSJohn Baldwin as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF, 373c825d4dcSJohn Baldwin M_WAITOK); 374c825d4dcSJohn Baldwin as.sc = sc; 375c825d4dcSJohn Baldwin as.count = 0; 376c825d4dcSJohn Baldwin as.error = 0; 377c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as); 378c825d4dcSJohn Baldwin if (as.error != 0) { 379c825d4dcSJohn Baldwin for (i = 0; i < as.count; i++) 380c825d4dcSJohn Baldwin bus_release_resource(sc->dev, SYS_RES_IOPORT, 381c825d4dcSJohn Baldwin sc->io.reg, as.res[i]); 382c825d4dcSJohn Baldwin free(as.res, M_DEVBUF); 383c825d4dcSJohn Baldwin return (as.error); 384c825d4dcSJohn Baldwin } 385c825d4dcSJohn Baldwin KASSERT(as.count == new_count, ("%s: count mismatch", __func__)); 386c825d4dcSJohn Baldwin 387c825d4dcSJohn Baldwin /* Third, add the ranges to the window. */ 388c825d4dcSJohn Baldwin pcib_add_window_resources(&sc->io, as.res, as.count); 389c825d4dcSJohn Baldwin free(as.res, M_DEVBUF); 390c825d4dcSJohn Baldwin return (0); 391c825d4dcSJohn Baldwin } 392c825d4dcSJohn Baldwin 39383c41143SJohn Baldwin static void 39483c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type, 39583c41143SJohn Baldwin int flags, pci_addr_t max_address) 39683c41143SJohn Baldwin { 397c825d4dcSJohn Baldwin struct resource *res; 39883c41143SJohn Baldwin char buf[64]; 39983c41143SJohn Baldwin int error, rid; 40083c41143SJohn Baldwin 40189977ce2SJustin Hibbits if (max_address != (rman_res_t)max_address) 402534ccd7bSJustin Hibbits max_address = ~0; 40383c41143SJohn Baldwin w->rman.rm_start = 0; 40483c41143SJohn Baldwin w->rman.rm_end = max_address; 40583c41143SJohn Baldwin w->rman.rm_type = RMAN_ARRAY; 40683c41143SJohn Baldwin snprintf(buf, sizeof(buf), "%s %s window", 40783c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name); 40883c41143SJohn Baldwin w->rman.rm_descr = strdup(buf, M_DEVBUF); 40983c41143SJohn Baldwin error = rman_init(&w->rman); 41083c41143SJohn Baldwin if (error) 41183c41143SJohn Baldwin panic("Failed to initialize %s %s rman", 41283c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name); 41383c41143SJohn Baldwin 41483c41143SJohn Baldwin if (!pcib_is_window_open(w)) 41583c41143SJohn Baldwin return; 41683c41143SJohn Baldwin 41783c41143SJohn Baldwin if (w->base > max_address || w->limit > max_address) { 41883c41143SJohn Baldwin device_printf(sc->dev, 41983c41143SJohn Baldwin "initial %s window has too many bits, ignoring\n", w->name); 42083c41143SJohn Baldwin return; 42183c41143SJohn Baldwin } 422c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE) 423c825d4dcSJohn Baldwin (void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit); 424c825d4dcSJohn Baldwin else { 42583c41143SJohn Baldwin rid = w->reg; 426c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit, 42783c41143SJohn Baldwin w->limit - w->base + 1, flags); 428c825d4dcSJohn Baldwin if (res != NULL) 429c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 430c825d4dcSJohn Baldwin } 43183c41143SJohn Baldwin if (w->res == NULL) { 43283c41143SJohn Baldwin device_printf(sc->dev, 43383c41143SJohn Baldwin "failed to allocate initial %s window: %#jx-%#jx\n", 43483c41143SJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 43583c41143SJohn Baldwin w->base = max_address; 43683c41143SJohn Baldwin w->limit = 0; 43783c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 43883c41143SJohn Baldwin return; 43983c41143SJohn Baldwin } 44083c41143SJohn Baldwin pcib_activate_window(sc, type); 44183c41143SJohn Baldwin } 44283c41143SJohn Baldwin 44383c41143SJohn Baldwin /* 44483c41143SJohn Baldwin * Initialize I/O windows. 44583c41143SJohn Baldwin */ 44683c41143SJohn Baldwin static void 44783c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc) 44883c41143SJohn Baldwin { 44983c41143SJohn Baldwin pci_addr_t max; 45083c41143SJohn Baldwin device_t dev; 45183c41143SJohn Baldwin uint32_t val; 45283c41143SJohn Baldwin 45383c41143SJohn Baldwin dev = sc->dev; 45483c41143SJohn Baldwin 4550070c94bSJohn Baldwin if (pci_clear_pcib) { 456809923caSJustin Hibbits pcib_bridge_init(dev); 4570070c94bSJohn Baldwin } 4580070c94bSJohn Baldwin 45983c41143SJohn Baldwin /* Determine if the I/O port window is implemented. */ 46083c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 46183c41143SJohn Baldwin if (val == 0) { 46283c41143SJohn Baldwin /* 46383c41143SJohn Baldwin * If 'val' is zero, then only 16-bits of I/O space 46483c41143SJohn Baldwin * are supported. 46583c41143SJohn Baldwin */ 46683c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 46783c41143SJohn Baldwin if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) { 46883c41143SJohn Baldwin sc->io.valid = 1; 46983c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0, 1); 47083c41143SJohn Baldwin } 47183c41143SJohn Baldwin } else 47283c41143SJohn Baldwin sc->io.valid = 1; 47383c41143SJohn Baldwin 47483c41143SJohn Baldwin /* Read the existing I/O port window. */ 47583c41143SJohn Baldwin if (sc->io.valid) { 47683c41143SJohn Baldwin sc->io.reg = PCIR_IOBASEL_1; 47783c41143SJohn Baldwin sc->io.step = 12; 47883c41143SJohn Baldwin sc->io.mask = WIN_IO; 47983c41143SJohn Baldwin sc->io.name = "I/O port"; 48083c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 48183c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE( 48283c41143SJohn Baldwin pci_read_config(dev, PCIR_IOBASEH_1, 2), val); 48383c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT( 48483c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITH_1, 2), 48583c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 48683c41143SJohn Baldwin max = 0xffffffff; 48783c41143SJohn Baldwin } else { 48883c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE(0, val); 48983c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT(0, 49083c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 49183c41143SJohn Baldwin max = 0xffff; 49283c41143SJohn Baldwin } 49383c41143SJohn Baldwin pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max); 49483c41143SJohn Baldwin } 49583c41143SJohn Baldwin 49683c41143SJohn Baldwin /* Read the existing memory window. */ 49783c41143SJohn Baldwin sc->mem.valid = 1; 49883c41143SJohn Baldwin sc->mem.reg = PCIR_MEMBASE_1; 49983c41143SJohn Baldwin sc->mem.step = 20; 50083c41143SJohn Baldwin sc->mem.mask = WIN_MEM; 50183c41143SJohn Baldwin sc->mem.name = "memory"; 50283c41143SJohn Baldwin sc->mem.base = PCI_PPBMEMBASE(0, 50383c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMBASE_1, 2)); 50483c41143SJohn Baldwin sc->mem.limit = PCI_PPBMEMLIMIT(0, 50583c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 50683c41143SJohn Baldwin pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff); 50783c41143SJohn Baldwin 50883c41143SJohn Baldwin /* Determine if the prefetchable memory window is implemented. */ 50983c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 51083c41143SJohn Baldwin if (val == 0) { 51183c41143SJohn Baldwin /* 51283c41143SJohn Baldwin * If 'val' is zero, then only 32-bits of memory space 51383c41143SJohn Baldwin * are supported. 51483c41143SJohn Baldwin */ 51583c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 51683c41143SJohn Baldwin if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) { 51783c41143SJohn Baldwin sc->pmem.valid = 1; 51883c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0, 2); 51983c41143SJohn Baldwin } 52083c41143SJohn Baldwin } else 52183c41143SJohn Baldwin sc->pmem.valid = 1; 52283c41143SJohn Baldwin 52383c41143SJohn Baldwin /* Read the existing prefetchable memory window. */ 52483c41143SJohn Baldwin if (sc->pmem.valid) { 52583c41143SJohn Baldwin sc->pmem.reg = PCIR_PMBASEL_1; 52683c41143SJohn Baldwin sc->pmem.step = 20; 52783c41143SJohn Baldwin sc->pmem.mask = WIN_PMEM; 52883c41143SJohn Baldwin sc->pmem.name = "prefetch"; 52983c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 53083c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE( 53183c41143SJohn Baldwin pci_read_config(dev, PCIR_PMBASEH_1, 4), val); 53283c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT( 53383c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITH_1, 4), 53483c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 53583c41143SJohn Baldwin max = 0xffffffffffffffff; 53683c41143SJohn Baldwin } else { 53783c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE(0, val); 53883c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT(0, 53983c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 54083c41143SJohn Baldwin max = 0xffffffff; 54183c41143SJohn Baldwin } 54283c41143SJohn Baldwin pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY, 54383c41143SJohn Baldwin RF_PREFETCHABLE, max); 54483c41143SJohn Baldwin } 54583c41143SJohn Baldwin } 54683c41143SJohn Baldwin 5474edef187SJohn Baldwin #ifdef PCI_RES_BUS 5484edef187SJohn Baldwin /* 5494edef187SJohn Baldwin * Allocate a suitable secondary bus for this bridge if needed and 5504edef187SJohn Baldwin * initialize the resource manager for the secondary bus range. Note 5514edef187SJohn Baldwin * that the minimum count is a desired value and this may allocate a 5524edef187SJohn Baldwin * smaller range. 5534edef187SJohn Baldwin */ 5544edef187SJohn Baldwin void 5554edef187SJohn Baldwin pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count) 5564edef187SJohn Baldwin { 5574edef187SJohn Baldwin char buf[64]; 558ad6f36f8SJohn Baldwin int error, rid, sec_reg; 5594edef187SJohn Baldwin 5604edef187SJohn Baldwin switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) { 5614edef187SJohn Baldwin case PCIM_HDRTYPE_BRIDGE: 562ad6f36f8SJohn Baldwin sec_reg = PCIR_SECBUS_1; 5634edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_1; 5644edef187SJohn Baldwin break; 5654edef187SJohn Baldwin case PCIM_HDRTYPE_CARDBUS: 566ad6f36f8SJohn Baldwin sec_reg = PCIR_SECBUS_2; 5674edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_2; 5684edef187SJohn Baldwin break; 5694edef187SJohn Baldwin default: 5704edef187SJohn Baldwin panic("not a PCI bridge"); 5714edef187SJohn Baldwin } 572ad6f36f8SJohn Baldwin bus->sec = pci_read_config(dev, sec_reg, 1); 573ad6f36f8SJohn Baldwin bus->sub = pci_read_config(dev, bus->sub_reg, 1); 5744edef187SJohn Baldwin bus->dev = dev; 5754edef187SJohn Baldwin bus->rman.rm_start = 0; 5764edef187SJohn Baldwin bus->rman.rm_end = PCI_BUSMAX; 5774edef187SJohn Baldwin bus->rman.rm_type = RMAN_ARRAY; 5784edef187SJohn Baldwin snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev)); 5794edef187SJohn Baldwin bus->rman.rm_descr = strdup(buf, M_DEVBUF); 5804edef187SJohn Baldwin error = rman_init(&bus->rman); 5814edef187SJohn Baldwin if (error) 5824edef187SJohn Baldwin panic("Failed to initialize %s bus number rman", 5834edef187SJohn Baldwin device_get_nameunit(dev)); 5844edef187SJohn Baldwin 5854edef187SJohn Baldwin /* 5864edef187SJohn Baldwin * Allocate a bus range. This will return an existing bus range 5874edef187SJohn Baldwin * if one exists, or a new bus range if one does not. 5884edef187SJohn Baldwin */ 5894edef187SJohn Baldwin rid = 0; 590c47476d7SJustin Hibbits bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid, 5914edef187SJohn Baldwin min_count, 0); 5924edef187SJohn Baldwin if (bus->res == NULL) { 5934edef187SJohn Baldwin /* 5944edef187SJohn Baldwin * Fall back to just allocating a range of a single bus 5954edef187SJohn Baldwin * number. 5964edef187SJohn Baldwin */ 597c47476d7SJustin Hibbits bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid, 5984edef187SJohn Baldwin 1, 0); 5994edef187SJohn Baldwin } else if (rman_get_size(bus->res) < min_count) 6004edef187SJohn Baldwin /* 6014edef187SJohn Baldwin * Attempt to grow the existing range to satisfy the 6024edef187SJohn Baldwin * minimum desired count. 6034edef187SJohn Baldwin */ 6044edef187SJohn Baldwin (void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res, 6054edef187SJohn Baldwin rman_get_start(bus->res), rman_get_start(bus->res) + 6064edef187SJohn Baldwin min_count - 1); 6074edef187SJohn Baldwin 6084edef187SJohn Baldwin /* 6094edef187SJohn Baldwin * Add the initial resource to the rman. 6104edef187SJohn Baldwin */ 6114edef187SJohn Baldwin if (bus->res != NULL) { 6124edef187SJohn Baldwin error = rman_manage_region(&bus->rman, rman_get_start(bus->res), 6134edef187SJohn Baldwin rman_get_end(bus->res)); 6144edef187SJohn Baldwin if (error) 6154edef187SJohn Baldwin panic("Failed to add resource to rman"); 6164edef187SJohn Baldwin bus->sec = rman_get_start(bus->res); 6174edef187SJohn Baldwin bus->sub = rman_get_end(bus->res); 6184edef187SJohn Baldwin } 6194edef187SJohn Baldwin } 6204edef187SJohn Baldwin 6214edef187SJohn Baldwin static struct resource * 6224edef187SJohn Baldwin pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid, 6232dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 6244edef187SJohn Baldwin { 6254edef187SJohn Baldwin struct resource *res; 6264edef187SJohn Baldwin 6274edef187SJohn Baldwin res = rman_reserve_resource(&bus->rman, start, end, count, flags, 6284edef187SJohn Baldwin child); 6294edef187SJohn Baldwin if (res == NULL) 6304edef187SJohn Baldwin return (NULL); 6314edef187SJohn Baldwin 6324edef187SJohn Baldwin if (bootverbose) 6334edef187SJohn Baldwin device_printf(bus->dev, 634da1b038aSJustin Hibbits "allocated bus range (%ju-%ju) for rid %d of %s\n", 6354edef187SJohn Baldwin rman_get_start(res), rman_get_end(res), *rid, 6364edef187SJohn Baldwin pcib_child_name(child)); 6374edef187SJohn Baldwin rman_set_rid(res, *rid); 6384edef187SJohn Baldwin return (res); 6394edef187SJohn Baldwin } 6404edef187SJohn Baldwin 6414edef187SJohn Baldwin /* 6424edef187SJohn Baldwin * Attempt to grow the secondary bus range. This is much simpler than 6434edef187SJohn Baldwin * for I/O windows as the range can only be grown by increasing 6444edef187SJohn Baldwin * subbus. 6454edef187SJohn Baldwin */ 6464edef187SJohn Baldwin static int 6472dd1bdf1SJustin Hibbits pcib_grow_subbus(struct pcib_secbus *bus, rman_res_t new_end) 6484edef187SJohn Baldwin { 6492dd1bdf1SJustin Hibbits rman_res_t old_end; 6504edef187SJohn Baldwin int error; 6514edef187SJohn Baldwin 6524edef187SJohn Baldwin old_end = rman_get_end(bus->res); 6534edef187SJohn Baldwin KASSERT(new_end > old_end, ("attempt to shrink subbus")); 6544edef187SJohn Baldwin error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res, 6554edef187SJohn Baldwin rman_get_start(bus->res), new_end); 6564edef187SJohn Baldwin if (error) 6574edef187SJohn Baldwin return (error); 6584edef187SJohn Baldwin if (bootverbose) 659da1b038aSJustin Hibbits device_printf(bus->dev, "grew bus range to %ju-%ju\n", 6604edef187SJohn Baldwin rman_get_start(bus->res), rman_get_end(bus->res)); 6614edef187SJohn Baldwin error = rman_manage_region(&bus->rman, old_end + 1, 6624edef187SJohn Baldwin rman_get_end(bus->res)); 6634edef187SJohn Baldwin if (error) 6644edef187SJohn Baldwin panic("Failed to add resource to rman"); 6654edef187SJohn Baldwin bus->sub = rman_get_end(bus->res); 6664edef187SJohn Baldwin pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1); 6674edef187SJohn Baldwin return (0); 6684edef187SJohn Baldwin } 6694edef187SJohn Baldwin 6704edef187SJohn Baldwin struct resource * 6714edef187SJohn Baldwin pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid, 6722dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 6734edef187SJohn Baldwin { 6744edef187SJohn Baldwin struct resource *res; 6752dd1bdf1SJustin Hibbits rman_res_t start_free, end_free, new_end; 6764edef187SJohn Baldwin 6774edef187SJohn Baldwin /* 6784edef187SJohn Baldwin * First, see if the request can be satisified by the existing 6794edef187SJohn Baldwin * bus range. 6804edef187SJohn Baldwin */ 6814edef187SJohn Baldwin res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags); 6824edef187SJohn Baldwin if (res != NULL) 6834edef187SJohn Baldwin return (res); 6844edef187SJohn Baldwin 6854edef187SJohn Baldwin /* 6864edef187SJohn Baldwin * Figure out a range to grow the bus range. First, find the 6874edef187SJohn Baldwin * first bus number after the last allocated bus in the rman and 6884edef187SJohn Baldwin * enforce that as a minimum starting point for the range. 6894edef187SJohn Baldwin */ 6904edef187SJohn Baldwin if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 || 6914edef187SJohn Baldwin end_free != bus->sub) 6924edef187SJohn Baldwin start_free = bus->sub + 1; 6934edef187SJohn Baldwin if (start_free < start) 6944edef187SJohn Baldwin start_free = start; 6954edef187SJohn Baldwin new_end = start_free + count - 1; 6964edef187SJohn Baldwin 6974edef187SJohn Baldwin /* 6984edef187SJohn Baldwin * See if this new range would satisfy the request if it 6994edef187SJohn Baldwin * succeeds. 7004edef187SJohn Baldwin */ 7014edef187SJohn Baldwin if (new_end > end) 7024edef187SJohn Baldwin return (NULL); 7034edef187SJohn Baldwin 7044edef187SJohn Baldwin /* Finally, attempt to grow the existing resource. */ 7054edef187SJohn Baldwin if (bootverbose) { 7064edef187SJohn Baldwin device_printf(bus->dev, 707da1b038aSJustin Hibbits "attempting to grow bus range for %ju buses\n", count); 708da1b038aSJustin Hibbits printf("\tback candidate range: %ju-%ju\n", start_free, 7094edef187SJohn Baldwin new_end); 7104edef187SJohn Baldwin } 7114edef187SJohn Baldwin if (pcib_grow_subbus(bus, new_end) == 0) 7124edef187SJohn Baldwin return (pcib_suballoc_bus(bus, child, rid, start, end, count, 7134edef187SJohn Baldwin flags)); 7144edef187SJohn Baldwin return (NULL); 7154edef187SJohn Baldwin } 7164edef187SJohn Baldwin #endif 7174edef187SJohn Baldwin 71883c41143SJohn Baldwin #else 71983c41143SJohn Baldwin 720bb0d0a8eSMike Smith /* 721b0a2d4b8SWarner Losh * Is the prefetch window open (eg, can we allocate memory in it?) 722b0a2d4b8SWarner Losh */ 723b0a2d4b8SWarner Losh static int 724b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc) 725b0a2d4b8SWarner Losh { 726b0a2d4b8SWarner Losh return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit); 727b0a2d4b8SWarner Losh } 728b0a2d4b8SWarner Losh 729b0a2d4b8SWarner Losh /* 730b0a2d4b8SWarner Losh * Is the nonprefetch window open (eg, can we allocate memory in it?) 731b0a2d4b8SWarner Losh */ 732b0a2d4b8SWarner Losh static int 733b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc) 734b0a2d4b8SWarner Losh { 735b0a2d4b8SWarner Losh return (sc->membase > 0 && sc->membase < sc->memlimit); 736b0a2d4b8SWarner Losh } 737b0a2d4b8SWarner Losh 738b0a2d4b8SWarner Losh /* 739b0a2d4b8SWarner Losh * Is the io window open (eg, can we allocate ports in it?) 740b0a2d4b8SWarner Losh */ 741b0a2d4b8SWarner Losh static int 742b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc) 743b0a2d4b8SWarner Losh { 744b0a2d4b8SWarner Losh return (sc->iobase > 0 && sc->iobase < sc->iolimit); 745b0a2d4b8SWarner Losh } 746b0a2d4b8SWarner Losh 747b0a2d4b8SWarner Losh /* 748e36af292SJung-uk Kim * Get current I/O decode. 749e36af292SJung-uk Kim */ 750e36af292SJung-uk Kim static void 751e36af292SJung-uk Kim pcib_get_io_decode(struct pcib_softc *sc) 752e36af292SJung-uk Kim { 753e36af292SJung-uk Kim device_t dev; 754e36af292SJung-uk Kim uint32_t iolow; 755e36af292SJung-uk Kim 756e36af292SJung-uk Kim dev = sc->dev; 757e36af292SJung-uk Kim 758e36af292SJung-uk Kim iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1); 759e36af292SJung-uk Kim if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 760e36af292SJung-uk Kim sc->iobase = PCI_PPBIOBASE( 761e36af292SJung-uk Kim pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow); 762e36af292SJung-uk Kim else 763e36af292SJung-uk Kim sc->iobase = PCI_PPBIOBASE(0, iolow); 764e36af292SJung-uk Kim 765e36af292SJung-uk Kim iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1); 766e36af292SJung-uk Kim if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 767e36af292SJung-uk Kim sc->iolimit = PCI_PPBIOLIMIT( 768e36af292SJung-uk Kim pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow); 769e36af292SJung-uk Kim else 770e36af292SJung-uk Kim sc->iolimit = PCI_PPBIOLIMIT(0, iolow); 771e36af292SJung-uk Kim } 772e36af292SJung-uk Kim 773e36af292SJung-uk Kim /* 774e36af292SJung-uk Kim * Get current memory decode. 775e36af292SJung-uk Kim */ 776e36af292SJung-uk Kim static void 777e36af292SJung-uk Kim pcib_get_mem_decode(struct pcib_softc *sc) 778e36af292SJung-uk Kim { 779e36af292SJung-uk Kim device_t dev; 780e36af292SJung-uk Kim pci_addr_t pmemlow; 781e36af292SJung-uk Kim 782e36af292SJung-uk Kim dev = sc->dev; 783e36af292SJung-uk Kim 784e36af292SJung-uk Kim sc->membase = PCI_PPBMEMBASE(0, 785e36af292SJung-uk Kim pci_read_config(dev, PCIR_MEMBASE_1, 2)); 786e36af292SJung-uk Kim sc->memlimit = PCI_PPBMEMLIMIT(0, 787e36af292SJung-uk Kim pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 788e36af292SJung-uk Kim 789e36af292SJung-uk Kim pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2); 790e36af292SJung-uk Kim if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 791e36af292SJung-uk Kim sc->pmembase = PCI_PPBMEMBASE( 792e36af292SJung-uk Kim pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow); 793e36af292SJung-uk Kim else 794e36af292SJung-uk Kim sc->pmembase = PCI_PPBMEMBASE(0, pmemlow); 795e36af292SJung-uk Kim 796e36af292SJung-uk Kim pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2); 797e36af292SJung-uk Kim if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 798e36af292SJung-uk Kim sc->pmemlimit = PCI_PPBMEMLIMIT( 799e36af292SJung-uk Kim pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow); 800e36af292SJung-uk Kim else 801e36af292SJung-uk Kim sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow); 802e36af292SJung-uk Kim } 803e36af292SJung-uk Kim 804e36af292SJung-uk Kim /* 805e36af292SJung-uk Kim * Restore previous I/O decode. 806e36af292SJung-uk Kim */ 807e36af292SJung-uk Kim static void 808e36af292SJung-uk Kim pcib_set_io_decode(struct pcib_softc *sc) 809e36af292SJung-uk Kim { 810e36af292SJung-uk Kim device_t dev; 811e36af292SJung-uk Kim uint32_t iohi; 812e36af292SJung-uk Kim 813e36af292SJung-uk Kim dev = sc->dev; 814e36af292SJung-uk Kim 815e36af292SJung-uk Kim iohi = sc->iobase >> 16; 816e36af292SJung-uk Kim if (iohi > 0) 817e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2); 818e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1); 819e36af292SJung-uk Kim 820e36af292SJung-uk Kim iohi = sc->iolimit >> 16; 821e36af292SJung-uk Kim if (iohi > 0) 822e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2); 823e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1); 824e36af292SJung-uk Kim } 825e36af292SJung-uk Kim 826e36af292SJung-uk Kim /* 827e36af292SJung-uk Kim * Restore previous memory decode. 828e36af292SJung-uk Kim */ 829e36af292SJung-uk Kim static void 830e36af292SJung-uk Kim pcib_set_mem_decode(struct pcib_softc *sc) 831e36af292SJung-uk Kim { 832e36af292SJung-uk Kim device_t dev; 833e36af292SJung-uk Kim pci_addr_t pmemhi; 834e36af292SJung-uk Kim 835e36af292SJung-uk Kim dev = sc->dev; 836e36af292SJung-uk Kim 837e36af292SJung-uk Kim pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2); 838e36af292SJung-uk Kim pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2); 839e36af292SJung-uk Kim 840e36af292SJung-uk Kim pmemhi = sc->pmembase >> 32; 841e36af292SJung-uk Kim if (pmemhi > 0) 842e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4); 843e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2); 844e36af292SJung-uk Kim 845e36af292SJung-uk Kim pmemhi = sc->pmemlimit >> 32; 846e36af292SJung-uk Kim if (pmemhi > 0) 847e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4); 848e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2); 849e36af292SJung-uk Kim } 85083c41143SJohn Baldwin #endif 851e36af292SJung-uk Kim 85282cb5c3bSJohn Baldwin #ifdef PCI_HP 85382cb5c3bSJohn Baldwin /* 85482cb5c3bSJohn Baldwin * PCI-express HotPlug support. 85582cb5c3bSJohn Baldwin */ 85682cb5c3bSJohn Baldwin static void 85782cb5c3bSJohn Baldwin pcib_probe_hotplug(struct pcib_softc *sc) 85882cb5c3bSJohn Baldwin { 85982cb5c3bSJohn Baldwin device_t dev; 86082cb5c3bSJohn Baldwin 86182cb5c3bSJohn Baldwin dev = sc->dev; 86282cb5c3bSJohn Baldwin if (pci_find_cap(dev, PCIY_EXPRESS, NULL) != 0) 86382cb5c3bSJohn Baldwin return; 86482cb5c3bSJohn Baldwin 86582cb5c3bSJohn Baldwin if (!(pcie_read_config(dev, PCIER_FLAGS, 2) & PCIEM_FLAGS_SLOT)) 86682cb5c3bSJohn Baldwin return; 86782cb5c3bSJohn Baldwin 86882cb5c3bSJohn Baldwin sc->pcie_link_cap = pcie_read_config(dev, PCIER_LINK_CAP, 4); 86982cb5c3bSJohn Baldwin sc->pcie_slot_cap = pcie_read_config(dev, PCIER_SLOT_CAP, 4); 87082cb5c3bSJohn Baldwin 87182cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_HPC) 87282cb5c3bSJohn Baldwin sc->flags |= PCIB_HOTPLUG; 87382cb5c3bSJohn Baldwin } 87482cb5c3bSJohn Baldwin 87582cb5c3bSJohn Baldwin /* 87682cb5c3bSJohn Baldwin * Send a HotPlug command to the slot control register. If this slot 87782cb5c3bSJohn Baldwin * uses command completion interrupts, these updates will be buffered 87882cb5c3bSJohn Baldwin * while a previous command is completing. 87982cb5c3bSJohn Baldwin */ 88082cb5c3bSJohn Baldwin static void 88182cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(struct pcib_softc *sc, uint16_t val, uint16_t mask) 88282cb5c3bSJohn Baldwin { 88382cb5c3bSJohn Baldwin device_t dev; 88482cb5c3bSJohn Baldwin uint16_t ctl, new; 88582cb5c3bSJohn Baldwin 88682cb5c3bSJohn Baldwin dev = sc->dev; 88782cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS) { 88882cb5c3bSJohn Baldwin ctl = pcie_read_config(dev, PCIER_SLOT_CTL, 2); 88982cb5c3bSJohn Baldwin new = (ctl & ~mask) | val; 89082cb5c3bSJohn Baldwin if (new != ctl) 89182cb5c3bSJohn Baldwin pcie_write_config(dev, PCIER_SLOT_CTL, new, 2); 89282cb5c3bSJohn Baldwin return; 89382cb5c3bSJohn Baldwin } 89482cb5c3bSJohn Baldwin 89582cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) { 89682cb5c3bSJohn Baldwin sc->pcie_pending_link_ctl_val &= ~mask; 89782cb5c3bSJohn Baldwin sc->pcie_pending_link_ctl_val |= val; 89882cb5c3bSJohn Baldwin sc->pcie_pending_link_ctl_mask |= mask; 89982cb5c3bSJohn Baldwin } else { 90082cb5c3bSJohn Baldwin ctl = pcie_read_config(dev, PCIER_SLOT_CTL, 2); 90182cb5c3bSJohn Baldwin new = (ctl & ~mask) | val; 90282cb5c3bSJohn Baldwin if (new != ctl) { 90382cb5c3bSJohn Baldwin pcie_write_config(dev, PCIER_SLOT_CTL, ctl, 2); 90482cb5c3bSJohn Baldwin sc->flags |= PCIB_HOTPLUG_CMD_PENDING; 90582cb5c3bSJohn Baldwin if (!cold) 90682cb5c3bSJohn Baldwin callout_reset(&sc->pcie_cc_timer, hz, 90782cb5c3bSJohn Baldwin pcib_pcie_cc_timeout, sc); 90882cb5c3bSJohn Baldwin } 90982cb5c3bSJohn Baldwin } 91082cb5c3bSJohn Baldwin } 91182cb5c3bSJohn Baldwin 91282cb5c3bSJohn Baldwin static void 91382cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(struct pcib_softc *sc) 91482cb5c3bSJohn Baldwin { 91582cb5c3bSJohn Baldwin device_t dev; 91682cb5c3bSJohn Baldwin uint16_t ctl, new; 91782cb5c3bSJohn Baldwin 91882cb5c3bSJohn Baldwin dev = sc->dev; 91982cb5c3bSJohn Baldwin 92082cb5c3bSJohn Baldwin if (bootverbose) 92182cb5c3bSJohn Baldwin device_printf(dev, "Command Completed\n"); 92282cb5c3bSJohn Baldwin if (!(sc->flags & PCIB_HOTPLUG_CMD_PENDING)) 92382cb5c3bSJohn Baldwin return; 92482cb5c3bSJohn Baldwin if (sc->pcie_pending_link_ctl_mask != 0) { 92582cb5c3bSJohn Baldwin ctl = pcie_read_config(dev, PCIER_SLOT_CTL, 2); 92682cb5c3bSJohn Baldwin new = ctl & ~sc->pcie_pending_link_ctl_mask; 92782cb5c3bSJohn Baldwin new |= sc->pcie_pending_link_ctl_val; 92882cb5c3bSJohn Baldwin if (new != ctl) { 92982cb5c3bSJohn Baldwin pcie_write_config(dev, PCIER_SLOT_CTL, ctl, 2); 93082cb5c3bSJohn Baldwin if (!cold) 93182cb5c3bSJohn Baldwin callout_reset(&sc->pcie_cc_timer, hz, 93282cb5c3bSJohn Baldwin pcib_pcie_cc_timeout, sc); 93382cb5c3bSJohn Baldwin } else 93482cb5c3bSJohn Baldwin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 93582cb5c3bSJohn Baldwin sc->pcie_pending_link_ctl_mask = 0; 93682cb5c3bSJohn Baldwin sc->pcie_pending_link_ctl_val = 0; 93782cb5c3bSJohn Baldwin } else { 93882cb5c3bSJohn Baldwin callout_stop(&sc->pcie_cc_timer); 93982cb5c3bSJohn Baldwin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 94082cb5c3bSJohn Baldwin } 94182cb5c3bSJohn Baldwin } 94282cb5c3bSJohn Baldwin 94382cb5c3bSJohn Baldwin /* 94482cb5c3bSJohn Baldwin * Returns true if a card is fully inserted from the user's 94582cb5c3bSJohn Baldwin * perspective. It may not yet be ready for access, but the driver 94682cb5c3bSJohn Baldwin * can now start enabling access if necessary. 94782cb5c3bSJohn Baldwin */ 94882cb5c3bSJohn Baldwin static bool 94982cb5c3bSJohn Baldwin pcib_hotplug_inserted(struct pcib_softc *sc) 95082cb5c3bSJohn Baldwin { 95182cb5c3bSJohn Baldwin 95282cb5c3bSJohn Baldwin /* Pretend the card isn't present if a detach is forced. */ 95382cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACHING) 95482cb5c3bSJohn Baldwin return (false); 95582cb5c3bSJohn Baldwin 95682cb5c3bSJohn Baldwin /* Card must be present in the slot. */ 95782cb5c3bSJohn Baldwin if ((sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS) == 0) 95882cb5c3bSJohn Baldwin return (false); 95982cb5c3bSJohn Baldwin 96082cb5c3bSJohn Baldwin /* A power fault implicitly turns off power to the slot. */ 96182cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD) 96282cb5c3bSJohn Baldwin return (false); 96382cb5c3bSJohn Baldwin 96482cb5c3bSJohn Baldwin /* If the MRL is disengaged, the slot is powered off. */ 96582cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP && 96682cb5c3bSJohn Baldwin (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS) != 0) 96782cb5c3bSJohn Baldwin return (false); 96882cb5c3bSJohn Baldwin 96982cb5c3bSJohn Baldwin return (true); 97082cb5c3bSJohn Baldwin } 97182cb5c3bSJohn Baldwin 97282cb5c3bSJohn Baldwin /* 97382cb5c3bSJohn Baldwin * Returns -1 if the card is fully inserted, powered, and ready for 97482cb5c3bSJohn Baldwin * access. Otherwise, returns 0. 97582cb5c3bSJohn Baldwin */ 97682cb5c3bSJohn Baldwin static int 97782cb5c3bSJohn Baldwin pcib_hotplug_present(struct pcib_softc *sc) 97882cb5c3bSJohn Baldwin { 97982cb5c3bSJohn Baldwin device_t dev; 98082cb5c3bSJohn Baldwin 98182cb5c3bSJohn Baldwin dev = sc->dev; 98282cb5c3bSJohn Baldwin 98382cb5c3bSJohn Baldwin /* Card must be inserted. */ 98482cb5c3bSJohn Baldwin if (!pcib_hotplug_inserted(sc)) 98582cb5c3bSJohn Baldwin return (0); 98682cb5c3bSJohn Baldwin 98782cb5c3bSJohn Baldwin /* 98882cb5c3bSJohn Baldwin * Require the Electromechanical Interlock to be engaged if 98982cb5c3bSJohn Baldwin * present. 99082cb5c3bSJohn Baldwin */ 99182cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP && 99282cb5c3bSJohn Baldwin (sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS) == 0) 99382cb5c3bSJohn Baldwin return (0); 99482cb5c3bSJohn Baldwin 99582cb5c3bSJohn Baldwin /* Require the Data Link Layer to be active. */ 99682cb5c3bSJohn Baldwin if (sc->pcie_link_cap & PCIEM_LINK_CAP_DL_ACTIVE) { 99782cb5c3bSJohn Baldwin if (!(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE)) 99882cb5c3bSJohn Baldwin return (0); 99982cb5c3bSJohn Baldwin } 100082cb5c3bSJohn Baldwin 100182cb5c3bSJohn Baldwin return (-1); 100282cb5c3bSJohn Baldwin } 100382cb5c3bSJohn Baldwin 100482cb5c3bSJohn Baldwin static void 100582cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(struct pcib_softc *sc, uint16_t val, uint16_t mask, 100682cb5c3bSJohn Baldwin bool schedule_task) 100782cb5c3bSJohn Baldwin { 100882cb5c3bSJohn Baldwin bool card_inserted; 100982cb5c3bSJohn Baldwin 101082cb5c3bSJohn Baldwin /* Clear DETACHING if Present Detect has cleared. */ 101182cb5c3bSJohn Baldwin if ((sc->pcie_slot_sta & (PCIEM_SLOT_STA_PDC | PCIEM_SLOT_STA_PDS)) == 101282cb5c3bSJohn Baldwin PCIEM_SLOT_STA_PDC) 101382cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACHING; 101482cb5c3bSJohn Baldwin 101582cb5c3bSJohn Baldwin card_inserted = pcib_hotplug_inserted(sc); 101682cb5c3bSJohn Baldwin 101782cb5c3bSJohn Baldwin /* Turn the power indicator on if a card is inserted. */ 101882cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PIP) { 101982cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_PIC; 102082cb5c3bSJohn Baldwin if (card_inserted) 102182cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_ON; 102282cb5c3bSJohn Baldwin else if (sc->flags & PCIB_DETACH_PENDING) 102382cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_BLINK; 102482cb5c3bSJohn Baldwin else 102582cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_OFF; 102682cb5c3bSJohn Baldwin } 102782cb5c3bSJohn Baldwin 102882cb5c3bSJohn Baldwin /* Turn the power on via the Power Controller if a card is inserted. */ 102982cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) { 103082cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_PCC; 103182cb5c3bSJohn Baldwin if (card_inserted) 103282cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PC_ON; 103382cb5c3bSJohn Baldwin else 103482cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PC_OFF; 103582cb5c3bSJohn Baldwin } 103682cb5c3bSJohn Baldwin 103782cb5c3bSJohn Baldwin /* 103882cb5c3bSJohn Baldwin * If a card is inserted, enable the Electromechanical 103982cb5c3bSJohn Baldwin * Interlock. If a card is not inserted (or we are in the 104082cb5c3bSJohn Baldwin * process of detaching), disable the Electromechanical 104182cb5c3bSJohn Baldwin * Interlock. 104282cb5c3bSJohn Baldwin */ 104382cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP) { 104482cb5c3bSJohn Baldwin if (card_inserted != 104582cb5c3bSJohn Baldwin !(sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS)) { 104682cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_EIC; 104782cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_EIC; 104882cb5c3bSJohn Baldwin } 104982cb5c3bSJohn Baldwin } 105082cb5c3bSJohn Baldwin 105182cb5c3bSJohn Baldwin /* 105282cb5c3bSJohn Baldwin * Start a timer to see if the Data Link Layer times out. 105382cb5c3bSJohn Baldwin * Note that we only start the timer if Presence Detect 105482cb5c3bSJohn Baldwin * changed on this interrupt. Stop any scheduled timer if 105582cb5c3bSJohn Baldwin * the Data Link Layer is active. 105682cb5c3bSJohn Baldwin */ 105782cb5c3bSJohn Baldwin if (sc->pcie_link_cap & PCIEM_LINK_CAP_DL_ACTIVE) { 105882cb5c3bSJohn Baldwin if (card_inserted && 105982cb5c3bSJohn Baldwin !(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) && 106082cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_PDC) { 106182cb5c3bSJohn Baldwin if (cold) 106282cb5c3bSJohn Baldwin device_printf(sc->dev, 106382cb5c3bSJohn Baldwin "Data Link Layer inactive\n"); 106482cb5c3bSJohn Baldwin else 106582cb5c3bSJohn Baldwin callout_reset(&sc->pcie_dll_timer, hz, 106682cb5c3bSJohn Baldwin pcib_pcie_dll_timeout, sc); 106782cb5c3bSJohn Baldwin } else if (sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) 106882cb5c3bSJohn Baldwin callout_stop(&sc->pcie_dll_timer); 106982cb5c3bSJohn Baldwin } 107082cb5c3bSJohn Baldwin 107182cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(sc, val, mask); 107282cb5c3bSJohn Baldwin 107382cb5c3bSJohn Baldwin /* 107482cb5c3bSJohn Baldwin * During attach the child "pci" device is added sychronously; 107582cb5c3bSJohn Baldwin * otherwise, the task is scheduled to manage the child 107682cb5c3bSJohn Baldwin * device. 107782cb5c3bSJohn Baldwin */ 107882cb5c3bSJohn Baldwin if (schedule_task && 107982cb5c3bSJohn Baldwin (pcib_hotplug_present(sc) != 0) != (sc->child != NULL)) 108082cb5c3bSJohn Baldwin taskqueue_enqueue(taskqueue_thread, &sc->pcie_hp_task); 108182cb5c3bSJohn Baldwin } 108282cb5c3bSJohn Baldwin 108382cb5c3bSJohn Baldwin static void 108482cb5c3bSJohn Baldwin pcib_pcie_intr(void *arg) 108582cb5c3bSJohn Baldwin { 108682cb5c3bSJohn Baldwin struct pcib_softc *sc; 108782cb5c3bSJohn Baldwin device_t dev; 108882cb5c3bSJohn Baldwin 108982cb5c3bSJohn Baldwin sc = arg; 109082cb5c3bSJohn Baldwin dev = sc->dev; 109182cb5c3bSJohn Baldwin sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 109282cb5c3bSJohn Baldwin 109382cb5c3bSJohn Baldwin /* Clear the events just reported. */ 109482cb5c3bSJohn Baldwin pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2); 109582cb5c3bSJohn Baldwin 109682cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_ABP) { 109782cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 109882cb5c3bSJohn Baldwin device_printf(dev, 109982cb5c3bSJohn Baldwin "Attention Button Pressed: Detach Cancelled\n"); 110082cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 110182cb5c3bSJohn Baldwin callout_stop(&sc->pcie_ab_timer); 110282cb5c3bSJohn Baldwin } else { 110382cb5c3bSJohn Baldwin device_printf(dev, 110482cb5c3bSJohn Baldwin "Attention Button Pressed: Detaching in 5 seconds\n"); 110582cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACH_PENDING; 110682cb5c3bSJohn Baldwin callout_reset(&sc->pcie_ab_timer, 5 * hz, 110782cb5c3bSJohn Baldwin pcib_pcie_ab_timeout, sc); 110882cb5c3bSJohn Baldwin } 110982cb5c3bSJohn Baldwin } 111082cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD) 111182cb5c3bSJohn Baldwin device_printf(dev, "Power Fault Detected\n"); 111282cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSC) 111382cb5c3bSJohn Baldwin device_printf(dev, "MRL Sensor Changed to %s\n", 111482cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS ? "open" : 111582cb5c3bSJohn Baldwin "closed"); 111682cb5c3bSJohn Baldwin if (bootverbose && sc->pcie_slot_sta & PCIEM_SLOT_STA_PDC) 111782cb5c3bSJohn Baldwin device_printf(dev, "Present Detect Changed to %s\n", 111882cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS ? "card present" : 111982cb5c3bSJohn Baldwin "empty"); 112082cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_CC) 112182cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(sc); 112282cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_DLLSC) { 112382cb5c3bSJohn Baldwin sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 112482cb5c3bSJohn Baldwin if (bootverbose) 112582cb5c3bSJohn Baldwin device_printf(dev, 112682cb5c3bSJohn Baldwin "Data Link Layer State Changed to %s\n", 112782cb5c3bSJohn Baldwin sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE ? 112882cb5c3bSJohn Baldwin "active" : "inactive"); 112982cb5c3bSJohn Baldwin } 113082cb5c3bSJohn Baldwin 113182cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 113282cb5c3bSJohn Baldwin } 113382cb5c3bSJohn Baldwin 113482cb5c3bSJohn Baldwin static void 113582cb5c3bSJohn Baldwin pcib_pcie_hotplug_task(void *context, int pending) 113682cb5c3bSJohn Baldwin { 113782cb5c3bSJohn Baldwin struct pcib_softc *sc; 113882cb5c3bSJohn Baldwin device_t dev; 113982cb5c3bSJohn Baldwin 114082cb5c3bSJohn Baldwin sc = context; 114182cb5c3bSJohn Baldwin mtx_lock(&Giant); 114282cb5c3bSJohn Baldwin dev = sc->dev; 114382cb5c3bSJohn Baldwin if (pcib_hotplug_present(sc) != 0) { 114482cb5c3bSJohn Baldwin if (sc->child == NULL) { 114582cb5c3bSJohn Baldwin sc->child = device_add_child(dev, "pci", -1); 114682cb5c3bSJohn Baldwin bus_generic_attach(dev); 114782cb5c3bSJohn Baldwin } 114882cb5c3bSJohn Baldwin } else { 114982cb5c3bSJohn Baldwin if (sc->child != NULL) { 115082cb5c3bSJohn Baldwin if (device_delete_child(dev, sc->child) == 0) 115182cb5c3bSJohn Baldwin sc->child = NULL; 115282cb5c3bSJohn Baldwin } 115382cb5c3bSJohn Baldwin } 115482cb5c3bSJohn Baldwin mtx_unlock(&Giant); 115582cb5c3bSJohn Baldwin } 115682cb5c3bSJohn Baldwin 115782cb5c3bSJohn Baldwin static void 115882cb5c3bSJohn Baldwin pcib_pcie_ab_timeout(void *arg) 115982cb5c3bSJohn Baldwin { 116082cb5c3bSJohn Baldwin struct pcib_softc *sc; 116182cb5c3bSJohn Baldwin device_t dev; 116282cb5c3bSJohn Baldwin 116382cb5c3bSJohn Baldwin sc = arg; 116482cb5c3bSJohn Baldwin dev = sc->dev; 116582cb5c3bSJohn Baldwin mtx_assert(&Giant, MA_OWNED); 116682cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 116782cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING; 116882cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 116982cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 117082cb5c3bSJohn Baldwin } 117182cb5c3bSJohn Baldwin } 117282cb5c3bSJohn Baldwin 117382cb5c3bSJohn Baldwin static void 117482cb5c3bSJohn Baldwin pcib_pcie_cc_timeout(void *arg) 117582cb5c3bSJohn Baldwin { 117682cb5c3bSJohn Baldwin struct pcib_softc *sc; 117782cb5c3bSJohn Baldwin device_t dev; 117882cb5c3bSJohn Baldwin 117982cb5c3bSJohn Baldwin sc = arg; 118082cb5c3bSJohn Baldwin dev = sc->dev; 118182cb5c3bSJohn Baldwin mtx_assert(&Giant, MA_OWNED); 118282cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) { 118382cb5c3bSJohn Baldwin device_printf(dev, 118482cb5c3bSJohn Baldwin "Hotplug Command Timed Out - forcing detach\n"); 118582cb5c3bSJohn Baldwin sc->flags &= ~(PCIB_HOTPLUG_CMD_PENDING | PCIB_DETACH_PENDING); 118682cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING; 118782cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 118882cb5c3bSJohn Baldwin } 118982cb5c3bSJohn Baldwin } 119082cb5c3bSJohn Baldwin 119182cb5c3bSJohn Baldwin static void 119282cb5c3bSJohn Baldwin pcib_pcie_dll_timeout(void *arg) 119382cb5c3bSJohn Baldwin { 119482cb5c3bSJohn Baldwin struct pcib_softc *sc; 119582cb5c3bSJohn Baldwin device_t dev; 119682cb5c3bSJohn Baldwin uint16_t sta; 119782cb5c3bSJohn Baldwin 119882cb5c3bSJohn Baldwin sc = arg; 119982cb5c3bSJohn Baldwin dev = sc->dev; 120082cb5c3bSJohn Baldwin mtx_assert(&Giant, MA_OWNED); 120182cb5c3bSJohn Baldwin sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 120282cb5c3bSJohn Baldwin if (!(sta & PCIEM_LINK_STA_DL_ACTIVE)) { 120382cb5c3bSJohn Baldwin device_printf(dev, 120482cb5c3bSJohn Baldwin "Timed out waiting for Data Link Layer Active\n"); 120582cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING; 120682cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 120782cb5c3bSJohn Baldwin } else if (sta != sc->pcie_link_sta) { 120882cb5c3bSJohn Baldwin device_printf(dev, 120982cb5c3bSJohn Baldwin "Missed HotPlug interrupt waiting for DLL Active\n"); 121082cb5c3bSJohn Baldwin pcib_pcie_intr(sc); 121182cb5c3bSJohn Baldwin } 121282cb5c3bSJohn Baldwin } 121382cb5c3bSJohn Baldwin 121482cb5c3bSJohn Baldwin static int 121582cb5c3bSJohn Baldwin pcib_alloc_pcie_irq(struct pcib_softc *sc) 121682cb5c3bSJohn Baldwin { 121782cb5c3bSJohn Baldwin device_t dev; 121882cb5c3bSJohn Baldwin int count, error, rid; 121982cb5c3bSJohn Baldwin 122082cb5c3bSJohn Baldwin rid = -1; 122182cb5c3bSJohn Baldwin dev = sc->dev; 122282cb5c3bSJohn Baldwin 122382cb5c3bSJohn Baldwin /* 122482cb5c3bSJohn Baldwin * For simplicity, only use MSI-X if there is a single message. 122582cb5c3bSJohn Baldwin * To support a device with multiple messages we would have to 122682cb5c3bSJohn Baldwin * use remap intr if the MSI number is not 0. 122782cb5c3bSJohn Baldwin */ 122882cb5c3bSJohn Baldwin count = pci_msix_count(dev); 122982cb5c3bSJohn Baldwin if (count == 1) { 123082cb5c3bSJohn Baldwin error = pci_alloc_msix(dev, &count); 123182cb5c3bSJohn Baldwin if (error == 0) 123282cb5c3bSJohn Baldwin rid = 1; 123382cb5c3bSJohn Baldwin } 123482cb5c3bSJohn Baldwin 123582cb5c3bSJohn Baldwin if (rid < 0 && pci_msi_count(dev) > 0) { 123682cb5c3bSJohn Baldwin count = 1; 123782cb5c3bSJohn Baldwin error = pci_alloc_msi(dev, &count); 123882cb5c3bSJohn Baldwin if (error == 0) 123982cb5c3bSJohn Baldwin rid = 1; 124082cb5c3bSJohn Baldwin } 124182cb5c3bSJohn Baldwin 124282cb5c3bSJohn Baldwin if (rid < 0) 124382cb5c3bSJohn Baldwin rid = 0; 124482cb5c3bSJohn Baldwin 124582cb5c3bSJohn Baldwin sc->pcie_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 124682cb5c3bSJohn Baldwin RF_ACTIVE); 124782cb5c3bSJohn Baldwin if (sc->pcie_irq == NULL) { 124882cb5c3bSJohn Baldwin device_printf(dev, 124982cb5c3bSJohn Baldwin "Failed to allocate interrupt for PCI-e events\n"); 125082cb5c3bSJohn Baldwin if (rid > 0) 125182cb5c3bSJohn Baldwin pci_release_msi(dev); 125282cb5c3bSJohn Baldwin return (ENXIO); 125382cb5c3bSJohn Baldwin } 125482cb5c3bSJohn Baldwin 125582cb5c3bSJohn Baldwin error = bus_setup_intr(dev, sc->pcie_irq, INTR_TYPE_MISC, 125682cb5c3bSJohn Baldwin NULL, pcib_pcie_intr, sc, &sc->pcie_ihand); 125782cb5c3bSJohn Baldwin if (error) { 125882cb5c3bSJohn Baldwin device_printf(dev, "Failed to setup PCI-e interrupt handler\n"); 125982cb5c3bSJohn Baldwin bus_release_resource(dev, SYS_RES_IRQ, rid, sc->pcie_irq); 126082cb5c3bSJohn Baldwin if (rid > 0) 126182cb5c3bSJohn Baldwin pci_release_msi(dev); 126282cb5c3bSJohn Baldwin return (error); 126382cb5c3bSJohn Baldwin } 126482cb5c3bSJohn Baldwin return (0); 126582cb5c3bSJohn Baldwin } 126682cb5c3bSJohn Baldwin 126782cb5c3bSJohn Baldwin static void 126882cb5c3bSJohn Baldwin pcib_setup_hotplug(struct pcib_softc *sc) 126982cb5c3bSJohn Baldwin { 127082cb5c3bSJohn Baldwin device_t dev; 127182cb5c3bSJohn Baldwin uint16_t mask, val; 127282cb5c3bSJohn Baldwin 127382cb5c3bSJohn Baldwin dev = sc->dev; 127482cb5c3bSJohn Baldwin callout_init(&sc->pcie_ab_timer, 0); 127582cb5c3bSJohn Baldwin callout_init(&sc->pcie_cc_timer, 0); 127682cb5c3bSJohn Baldwin callout_init(&sc->pcie_dll_timer, 0); 127782cb5c3bSJohn Baldwin TASK_INIT(&sc->pcie_hp_task, 0, pcib_pcie_hotplug_task, sc); 127882cb5c3bSJohn Baldwin 127982cb5c3bSJohn Baldwin /* Allocate IRQ. */ 128082cb5c3bSJohn Baldwin if (pcib_alloc_pcie_irq(sc) != 0) 128182cb5c3bSJohn Baldwin return; 128282cb5c3bSJohn Baldwin 128382cb5c3bSJohn Baldwin sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 128482cb5c3bSJohn Baldwin sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 128582cb5c3bSJohn Baldwin 128682cb5c3bSJohn Baldwin /* Enable HotPlug events. */ 128782cb5c3bSJohn Baldwin mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | 128882cb5c3bSJohn Baldwin PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE | 128982cb5c3bSJohn Baldwin PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE; 129082cb5c3bSJohn Baldwin val = PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_HPIE; 129182cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_APB) 129282cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_ABPE; 129382cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) 129482cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PFDE; 129582cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) 129682cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_MRLSCE; 129782cb5c3bSJohn Baldwin if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS)) 129882cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_CCIE; 129982cb5c3bSJohn Baldwin if (sc->pcie_link_cap & PCIEM_LINK_CAP_DL_ACTIVE) 130082cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_DLLSCE; 130182cb5c3bSJohn Baldwin 130282cb5c3bSJohn Baldwin /* Turn the attention indicator off. */ 130382cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) { 130482cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_AIC; 130582cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_AI_OFF; 130682cb5c3bSJohn Baldwin } 130782cb5c3bSJohn Baldwin 130882cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, val, mask, false); 130982cb5c3bSJohn Baldwin } 131082cb5c3bSJohn Baldwin #endif 131182cb5c3bSJohn Baldwin 1312e36af292SJung-uk Kim /* 1313e36af292SJung-uk Kim * Get current bridge configuration. 1314e36af292SJung-uk Kim */ 1315e36af292SJung-uk Kim static void 1316e36af292SJung-uk Kim pcib_cfg_save(struct pcib_softc *sc) 1317e36af292SJung-uk Kim { 1318ad6f36f8SJohn Baldwin #ifndef NEW_PCIB 1319e36af292SJung-uk Kim device_t dev; 1320ad6f36f8SJohn Baldwin uint16_t command; 1321e36af292SJung-uk Kim 1322e36af292SJung-uk Kim dev = sc->dev; 1323e36af292SJung-uk Kim 1324ad6f36f8SJohn Baldwin command = pci_read_config(dev, PCIR_COMMAND, 2); 1325ad6f36f8SJohn Baldwin if (command & PCIM_CMD_PORTEN) 1326e36af292SJung-uk Kim pcib_get_io_decode(sc); 1327ad6f36f8SJohn Baldwin if (command & PCIM_CMD_MEMEN) 1328e36af292SJung-uk Kim pcib_get_mem_decode(sc); 132983c41143SJohn Baldwin #endif 1330e36af292SJung-uk Kim } 1331e36af292SJung-uk Kim 1332e36af292SJung-uk Kim /* 1333e36af292SJung-uk Kim * Restore previous bridge configuration. 1334e36af292SJung-uk Kim */ 1335e36af292SJung-uk Kim static void 1336e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc) 1337e36af292SJung-uk Kim { 1338e36af292SJung-uk Kim device_t dev; 1339ad6f36f8SJohn Baldwin #ifndef NEW_PCIB 1340ad6f36f8SJohn Baldwin uint16_t command; 1341ad6f36f8SJohn Baldwin #endif 1342e36af292SJung-uk Kim dev = sc->dev; 1343e36af292SJung-uk Kim 134483c41143SJohn Baldwin #ifdef NEW_PCIB 134583c41143SJohn Baldwin pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM); 134683c41143SJohn Baldwin #else 1347ad6f36f8SJohn Baldwin command = pci_read_config(dev, PCIR_COMMAND, 2); 1348ad6f36f8SJohn Baldwin if (command & PCIM_CMD_PORTEN) 1349e36af292SJung-uk Kim pcib_set_io_decode(sc); 1350ad6f36f8SJohn Baldwin if (command & PCIM_CMD_MEMEN) 1351e36af292SJung-uk Kim pcib_set_mem_decode(sc); 135283c41143SJohn Baldwin #endif 1353e36af292SJung-uk Kim } 1354e36af292SJung-uk Kim 1355e36af292SJung-uk Kim /* 1356bb0d0a8eSMike Smith * Generic device interface 1357bb0d0a8eSMike Smith */ 1358bb0d0a8eSMike Smith static int 1359bb0d0a8eSMike Smith pcib_probe(device_t dev) 1360bb0d0a8eSMike Smith { 1361bb0d0a8eSMike Smith if ((pci_get_class(dev) == PCIC_BRIDGE) && 1362bb0d0a8eSMike Smith (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) { 1363bb0d0a8eSMike Smith device_set_desc(dev, "PCI-PCI bridge"); 1364b7cbd25bSMarcel Moolenaar return(-10000); 1365bb0d0a8eSMike Smith } 1366bb0d0a8eSMike Smith return(ENXIO); 1367bb0d0a8eSMike Smith } 1368bb0d0a8eSMike Smith 13696f0d5884SJohn Baldwin void 13706f0d5884SJohn Baldwin pcib_attach_common(device_t dev) 1371bb0d0a8eSMike Smith { 1372bb0d0a8eSMike Smith struct pcib_softc *sc; 1373abf07f13SWarner Losh struct sysctl_ctx_list *sctx; 1374abf07f13SWarner Losh struct sysctl_oid *soid; 1375c825d4dcSJohn Baldwin int comma; 1376bb0d0a8eSMike Smith 1377bb0d0a8eSMike Smith sc = device_get_softc(dev); 1378bb0d0a8eSMike Smith sc->dev = dev; 1379bb0d0a8eSMike Smith 13804fa59183SMike Smith /* 13814fa59183SMike Smith * Get current bridge configuration. 13824fa59183SMike Smith */ 138355aaf894SMarius Strobl sc->domain = pci_get_domain(dev); 1384ad6f36f8SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 1385ad6f36f8SJohn Baldwin sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1); 1386ad6f36f8SJohn Baldwin sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1); 1387ad6f36f8SJohn Baldwin #endif 1388ad6f36f8SJohn Baldwin sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 1389e36af292SJung-uk Kim pcib_cfg_save(sc); 13904fa59183SMike Smith 13914fa59183SMike Smith /* 13924edef187SJohn Baldwin * The primary bus register should always be the bus of the 13934edef187SJohn Baldwin * parent. 13944edef187SJohn Baldwin */ 13954edef187SJohn Baldwin sc->pribus = pci_get_bus(dev); 13964edef187SJohn Baldwin pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1); 13974edef187SJohn Baldwin 13984edef187SJohn Baldwin /* 1399abf07f13SWarner Losh * Setup sysctl reporting nodes 1400abf07f13SWarner Losh */ 1401abf07f13SWarner Losh sctx = device_get_sysctl_ctx(dev); 1402abf07f13SWarner Losh soid = device_get_sysctl_tree(dev); 1403abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain", 1404abf07f13SWarner Losh CTLFLAG_RD, &sc->domain, 0, "Domain number"); 1405abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus", 1406abf07f13SWarner Losh CTLFLAG_RD, &sc->pribus, 0, "Primary bus number"); 1407abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus", 14084edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number"); 1409abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus", 14104edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number"); 1411abf07f13SWarner Losh 1412abf07f13SWarner Losh /* 14134fa59183SMike Smith * Quirk handling. 14144fa59183SMike Smith */ 14154fa59183SMike Smith switch (pci_get_devid(dev)) { 14162ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 14174fa59183SMike Smith case 0x12258086: /* Intel 82454KX/GX (Orion) */ 14184fa59183SMike Smith { 1419b0cb115fSWarner Losh uint8_t supbus; 14204fa59183SMike Smith 14214fa59183SMike Smith supbus = pci_read_config(dev, 0x41, 1); 14224fa59183SMike Smith if (supbus != 0xff) { 14234edef187SJohn Baldwin sc->bus.sec = supbus + 1; 14244edef187SJohn Baldwin sc->bus.sub = supbus + 1; 14254fa59183SMike Smith } 14264fa59183SMike Smith break; 14274fa59183SMike Smith } 14284edef187SJohn Baldwin #endif 14294fa59183SMike Smith 1430e4b59fc5SWarner Losh /* 1431e4b59fc5SWarner Losh * The i82380FB mobile docking controller is a PCI-PCI bridge, 1432e4b59fc5SWarner Losh * and it is a subtractive bridge. However, the ProgIf is wrong 1433e4b59fc5SWarner Losh * so the normal setting of PCIB_SUBTRACTIVE bit doesn't 14344718610dSZbigniew Bodek * happen. There are also Toshiba and Cavium ThunderX bridges 14354718610dSZbigniew Bodek * that behave this way. 1436e4b59fc5SWarner Losh */ 14374718610dSZbigniew Bodek case 0xa002177d: /* Cavium ThunderX */ 1438e4b59fc5SWarner Losh case 0x124b8086: /* Intel 82380FB Mobile */ 1439e4b59fc5SWarner Losh case 0x060513d7: /* Toshiba ???? */ 1440e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 1441e4b59fc5SWarner Losh break; 1442c94d6dbeSJung-uk Kim 14432ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 1444c94d6dbeSJung-uk Kim /* Compaq R3000 BIOS sets wrong subordinate bus number. */ 1445c94d6dbeSJung-uk Kim case 0x00dd10de: 1446c94d6dbeSJung-uk Kim { 1447c94d6dbeSJung-uk Kim char *cp; 1448c94d6dbeSJung-uk Kim 14492be111bfSDavide Italiano if ((cp = kern_getenv("smbios.planar.maker")) == NULL) 1450c94d6dbeSJung-uk Kim break; 14511def0ca6SJung-uk Kim if (strncmp(cp, "Compal", 6) != 0) { 14521def0ca6SJung-uk Kim freeenv(cp); 1453c94d6dbeSJung-uk Kim break; 14541def0ca6SJung-uk Kim } 14551def0ca6SJung-uk Kim freeenv(cp); 14562be111bfSDavide Italiano if ((cp = kern_getenv("smbios.planar.product")) == NULL) 14571def0ca6SJung-uk Kim break; 14581def0ca6SJung-uk Kim if (strncmp(cp, "08A0", 4) != 0) { 14591def0ca6SJung-uk Kim freeenv(cp); 14601def0ca6SJung-uk Kim break; 14611def0ca6SJung-uk Kim } 14621def0ca6SJung-uk Kim freeenv(cp); 14634edef187SJohn Baldwin if (sc->bus.sub < 0xa) { 1464c94d6dbeSJung-uk Kim pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1); 14654edef187SJohn Baldwin sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1); 1466c94d6dbeSJung-uk Kim } 1467c94d6dbeSJung-uk Kim break; 1468c94d6dbeSJung-uk Kim } 14694edef187SJohn Baldwin #endif 1470e4b59fc5SWarner Losh } 1471e4b59fc5SWarner Losh 147222bf1c7fSJohn Baldwin if (pci_msi_device_blacklisted(dev)) 147322bf1c7fSJohn Baldwin sc->flags |= PCIB_DISABLE_MSI; 147422bf1c7fSJohn Baldwin 147568e9cbd3SMarius Strobl if (pci_msix_device_blacklisted(dev)) 147668e9cbd3SMarius Strobl sc->flags |= PCIB_DISABLE_MSIX; 147768e9cbd3SMarius Strobl 1478e4b59fc5SWarner Losh /* 1479e4b59fc5SWarner Losh * Intel 815, 845 and other chipsets say they are PCI-PCI bridges, 1480e4b59fc5SWarner Losh * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM, 1481e4b59fc5SWarner Losh * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese. 1482e4b59fc5SWarner Losh * This means they act as if they were subtractively decoding 1483e4b59fc5SWarner Losh * bridges and pass all transactions. Mark them and real ProgIf 1 1484e4b59fc5SWarner Losh * parts as subtractive. 1485e4b59fc5SWarner Losh */ 1486e4b59fc5SWarner Losh if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 || 1487657d9f9fSJohn Baldwin pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE) 1488e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 1489e4b59fc5SWarner Losh 149082cb5c3bSJohn Baldwin #ifdef PCI_HP 149182cb5c3bSJohn Baldwin pcib_probe_hotplug(sc); 149282cb5c3bSJohn Baldwin #endif 149383c41143SJohn Baldwin #ifdef NEW_PCIB 14944edef187SJohn Baldwin #ifdef PCI_RES_BUS 14954edef187SJohn Baldwin pcib_setup_secbus(dev, &sc->bus, 1); 14964edef187SJohn Baldwin #endif 149783c41143SJohn Baldwin pcib_probe_windows(sc); 149883c41143SJohn Baldwin #endif 149982cb5c3bSJohn Baldwin #ifdef PCI_HP 150082cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG) 150182cb5c3bSJohn Baldwin pcib_setup_hotplug(sc); 150282cb5c3bSJohn Baldwin #endif 1503bb0d0a8eSMike Smith if (bootverbose) { 150455aaf894SMarius Strobl device_printf(dev, " domain %d\n", sc->domain); 15054edef187SJohn Baldwin device_printf(dev, " secondary bus %d\n", sc->bus.sec); 15064edef187SJohn Baldwin device_printf(dev, " subordinate bus %d\n", sc->bus.sub); 150783c41143SJohn Baldwin #ifdef NEW_PCIB 150883c41143SJohn Baldwin if (pcib_is_window_open(&sc->io)) 150983c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%jx-0x%jx\n", 151083c41143SJohn Baldwin (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit); 151183c41143SJohn Baldwin if (pcib_is_window_open(&sc->mem)) 151283c41143SJohn Baldwin device_printf(dev, " memory decode 0x%jx-0x%jx\n", 151383c41143SJohn Baldwin (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit); 151483c41143SJohn Baldwin if (pcib_is_window_open(&sc->pmem)) 151583c41143SJohn Baldwin device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 151683c41143SJohn Baldwin (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit); 151783c41143SJohn Baldwin #else 151883c41143SJohn Baldwin if (pcib_is_io_open(sc)) 151983c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%x-0x%x\n", 152083c41143SJohn Baldwin sc->iobase, sc->iolimit); 1521b0a2d4b8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 1522b0a2d4b8SWarner Losh device_printf(dev, " memory decode 0x%jx-0x%jx\n", 1523b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit); 1524b0a2d4b8SWarner Losh if (pcib_is_prefetch_open(sc)) 1525b0a2d4b8SWarner Losh device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 1526b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 152783c41143SJohn Baldwin #endif 1528c825d4dcSJohn Baldwin if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) || 1529c825d4dcSJohn Baldwin sc->flags & PCIB_SUBTRACTIVE) { 1530c825d4dcSJohn Baldwin device_printf(dev, " special decode "); 1531c825d4dcSJohn Baldwin comma = 0; 1532c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) { 1533c825d4dcSJohn Baldwin printf("ISA"); 1534c825d4dcSJohn Baldwin comma = 1; 1535c825d4dcSJohn Baldwin } 1536c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) { 1537c825d4dcSJohn Baldwin printf("%sVGA", comma ? ", " : ""); 1538c825d4dcSJohn Baldwin comma = 1; 1539c825d4dcSJohn Baldwin } 1540e4b59fc5SWarner Losh if (sc->flags & PCIB_SUBTRACTIVE) 1541c825d4dcSJohn Baldwin printf("%ssubtractive", comma ? ", " : ""); 1542c825d4dcSJohn Baldwin printf("\n"); 1543c825d4dcSJohn Baldwin } 1544bb0d0a8eSMike Smith } 1545bb0d0a8eSMike Smith 1546bb0d0a8eSMike Smith /* 1547ef888152SJohn Baldwin * Always enable busmastering on bridges so that transactions 1548ef888152SJohn Baldwin * initiated on the secondary bus are passed through to the 1549ef888152SJohn Baldwin * primary bus. 1550ef888152SJohn Baldwin */ 1551ef888152SJohn Baldwin pci_enable_busmaster(dev); 15526f0d5884SJohn Baldwin } 1553bb0d0a8eSMike Smith 155482cb5c3bSJohn Baldwin #ifdef PCI_HP 155582cb5c3bSJohn Baldwin static int 155682cb5c3bSJohn Baldwin pcib_present(struct pcib_softc *sc) 155782cb5c3bSJohn Baldwin { 155882cb5c3bSJohn Baldwin 155982cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG) 156082cb5c3bSJohn Baldwin return (pcib_hotplug_present(sc) != 0); 156182cb5c3bSJohn Baldwin return (1); 156282cb5c3bSJohn Baldwin } 156382cb5c3bSJohn Baldwin #endif 156482cb5c3bSJohn Baldwin 156538906aedSJohn Baldwin int 156667e7d085SJohn Baldwin pcib_attach_child(device_t dev) 15676f0d5884SJohn Baldwin { 15686f0d5884SJohn Baldwin struct pcib_softc *sc; 15696f0d5884SJohn Baldwin 15706f0d5884SJohn Baldwin sc = device_get_softc(dev); 157167e7d085SJohn Baldwin if (sc->bus.sec == 0) { 157267e7d085SJohn Baldwin /* no secondary bus; we should have fixed this */ 157367e7d085SJohn Baldwin return(0); 157467e7d085SJohn Baldwin } 157567e7d085SJohn Baldwin 157682cb5c3bSJohn Baldwin #ifdef PCI_HP 157782cb5c3bSJohn Baldwin if (!pcib_present(sc)) { 157882cb5c3bSJohn Baldwin /* An empty HotPlug slot, so don't add a PCI bus yet. */ 157982cb5c3bSJohn Baldwin return (0); 158082cb5c3bSJohn Baldwin } 158182cb5c3bSJohn Baldwin #endif 158282cb5c3bSJohn Baldwin 158367e7d085SJohn Baldwin sc->child = device_add_child(dev, "pci", -1); 1584bb0d0a8eSMike Smith return (bus_generic_attach(dev)); 1585bb0d0a8eSMike Smith } 1586bb0d0a8eSMike Smith 158767e7d085SJohn Baldwin int 158867e7d085SJohn Baldwin pcib_attach(device_t dev) 158967e7d085SJohn Baldwin { 159067e7d085SJohn Baldwin 159167e7d085SJohn Baldwin pcib_attach_common(dev); 159267e7d085SJohn Baldwin return (pcib_attach_child(dev)); 1593bb0d0a8eSMike Smith } 1594bb0d0a8eSMike Smith 15956f0d5884SJohn Baldwin int 1596e36af292SJung-uk Kim pcib_suspend(device_t dev) 1597e36af292SJung-uk Kim { 1598e36af292SJung-uk Kim 1599e36af292SJung-uk Kim pcib_cfg_save(device_get_softc(dev)); 16007212fc6aSJohn Baldwin return (bus_generic_suspend(dev)); 1601e36af292SJung-uk Kim } 1602e36af292SJung-uk Kim 1603e36af292SJung-uk Kim int 1604e36af292SJung-uk Kim pcib_resume(device_t dev) 1605e36af292SJung-uk Kim { 1606e36af292SJung-uk Kim 1607e36af292SJung-uk Kim pcib_cfg_restore(device_get_softc(dev)); 1608e36af292SJung-uk Kim return (bus_generic_resume(dev)); 1609e36af292SJung-uk Kim } 1610e36af292SJung-uk Kim 1611809923caSJustin Hibbits void 1612809923caSJustin Hibbits pcib_bridge_init(device_t dev) 1613809923caSJustin Hibbits { 1614809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 1615809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2); 1616809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1); 1617809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2); 1618809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2); 1619809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2); 1620809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 1621809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4); 1622809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2); 1623809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4); 1624809923caSJustin Hibbits } 1625809923caSJustin Hibbits 1626e36af292SJung-uk Kim int 162782cb5c3bSJohn Baldwin pcib_child_present(device_t dev, device_t child) 162882cb5c3bSJohn Baldwin { 162982cb5c3bSJohn Baldwin #ifdef PCI_HP 163082cb5c3bSJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 163182cb5c3bSJohn Baldwin int retval; 163282cb5c3bSJohn Baldwin 163382cb5c3bSJohn Baldwin retval = bus_child_present(dev); 163482cb5c3bSJohn Baldwin if (retval != 0 && sc->flags & PCIB_HOTPLUG) 163582cb5c3bSJohn Baldwin retval = pcib_hotplug_present(sc); 163682cb5c3bSJohn Baldwin return (retval); 163782cb5c3bSJohn Baldwin #else 163882cb5c3bSJohn Baldwin return (bus_child_present(dev)); 163982cb5c3bSJohn Baldwin #endif 164082cb5c3bSJohn Baldwin } 164182cb5c3bSJohn Baldwin 164282cb5c3bSJohn Baldwin int 1643bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 1644bb0d0a8eSMike Smith { 1645bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 1646bb0d0a8eSMike Smith 1647bb0d0a8eSMike Smith switch (which) { 164855aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 164955aaf894SMarius Strobl *result = sc->domain; 165055aaf894SMarius Strobl return(0); 1651bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 16524edef187SJohn Baldwin *result = sc->bus.sec; 1653bb0d0a8eSMike Smith return(0); 1654bb0d0a8eSMike Smith } 1655bb0d0a8eSMike Smith return(ENOENT); 1656bb0d0a8eSMike Smith } 1657bb0d0a8eSMike Smith 16586f0d5884SJohn Baldwin int 1659bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 1660bb0d0a8eSMike Smith { 1661bb0d0a8eSMike Smith 1662bb0d0a8eSMike Smith switch (which) { 166355aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 166455aaf894SMarius Strobl return(EINVAL); 1665bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 16664edef187SJohn Baldwin return(EINVAL); 1667bb0d0a8eSMike Smith } 1668bb0d0a8eSMike Smith return(ENOENT); 1669bb0d0a8eSMike Smith } 1670bb0d0a8eSMike Smith 167183c41143SJohn Baldwin #ifdef NEW_PCIB 167283c41143SJohn Baldwin /* 167383c41143SJohn Baldwin * Attempt to allocate a resource from the existing resources assigned 167483c41143SJohn Baldwin * to a window. 167583c41143SJohn Baldwin */ 167683c41143SJohn Baldwin static struct resource * 167783c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w, 16782dd1bdf1SJustin Hibbits device_t child, int type, int *rid, rman_res_t start, rman_res_t end, 16792dd1bdf1SJustin Hibbits rman_res_t count, u_int flags) 168083c41143SJohn Baldwin { 168183c41143SJohn Baldwin struct resource *res; 168283c41143SJohn Baldwin 168383c41143SJohn Baldwin if (!pcib_is_window_open(w)) 168483c41143SJohn Baldwin return (NULL); 168583c41143SJohn Baldwin 168683c41143SJohn Baldwin res = rman_reserve_resource(&w->rman, start, end, count, 168783c41143SJohn Baldwin flags & ~RF_ACTIVE, child); 168883c41143SJohn Baldwin if (res == NULL) 168983c41143SJohn Baldwin return (NULL); 169083c41143SJohn Baldwin 169183c41143SJohn Baldwin if (bootverbose) 169283c41143SJohn Baldwin device_printf(sc->dev, 1693da1b038aSJustin Hibbits "allocated %s range (%#jx-%#jx) for rid %x of %s\n", 169483c41143SJohn Baldwin w->name, rman_get_start(res), rman_get_end(res), *rid, 169583c41143SJohn Baldwin pcib_child_name(child)); 169683c41143SJohn Baldwin rman_set_rid(res, *rid); 169783c41143SJohn Baldwin 169883c41143SJohn Baldwin /* 169983c41143SJohn Baldwin * If the resource should be active, pass that request up the 170083c41143SJohn Baldwin * tree. This assumes the parent drivers can handle 170183c41143SJohn Baldwin * activating sub-allocated resources. 170283c41143SJohn Baldwin */ 170383c41143SJohn Baldwin if (flags & RF_ACTIVE) { 170483c41143SJohn Baldwin if (bus_activate_resource(child, type, *rid, res) != 0) { 170583c41143SJohn Baldwin rman_release_resource(res); 170683c41143SJohn Baldwin return (NULL); 170783c41143SJohn Baldwin } 170883c41143SJohn Baldwin } 170983c41143SJohn Baldwin 171083c41143SJohn Baldwin return (res); 171183c41143SJohn Baldwin } 171283c41143SJohn Baldwin 1713c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */ 1714c825d4dcSJohn Baldwin static int 1715c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type, 17162dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 1717c825d4dcSJohn Baldwin { 1718c825d4dcSJohn Baldwin struct resource *res; 17192dd1bdf1SJustin Hibbits rman_res_t base, limit, wmask; 1720c825d4dcSJohn Baldwin int rid; 1721c825d4dcSJohn Baldwin 1722c825d4dcSJohn Baldwin /* 1723c825d4dcSJohn Baldwin * If this is an I/O window on a bridge with ISA enable set 1724c825d4dcSJohn Baldwin * and the start address is below 64k, then try to allocate an 1725c825d4dcSJohn Baldwin * initial window of 0x1000 bytes long starting at address 1726c825d4dcSJohn Baldwin * 0xf000 and walking down. Note that if the original request 1727c825d4dcSJohn Baldwin * was larger than the non-aliased range size of 0x100 our 1728c825d4dcSJohn Baldwin * caller would have raised the start address up to 64k 1729c825d4dcSJohn Baldwin * already. 1730c825d4dcSJohn Baldwin */ 1731c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1732c825d4dcSJohn Baldwin start < 65536) { 1733c825d4dcSJohn Baldwin for (base = 0xf000; (long)base >= 0; base -= 0x1000) { 1734c825d4dcSJohn Baldwin limit = base + 0xfff; 1735c825d4dcSJohn Baldwin 1736c825d4dcSJohn Baldwin /* 1737c825d4dcSJohn Baldwin * Skip ranges that wouldn't work for the 1738c825d4dcSJohn Baldwin * original request. Note that the actual 1739c825d4dcSJohn Baldwin * window that overlaps are the non-alias 1740c825d4dcSJohn Baldwin * ranges within [base, limit], so this isn't 1741c825d4dcSJohn Baldwin * quite a simple comparison. 1742c825d4dcSJohn Baldwin */ 1743c825d4dcSJohn Baldwin if (start + count > limit - 0x400) 1744c825d4dcSJohn Baldwin continue; 1745c825d4dcSJohn Baldwin if (base == 0) { 1746c825d4dcSJohn Baldwin /* 1747c825d4dcSJohn Baldwin * The first open region for the window at 1748c825d4dcSJohn Baldwin * 0 is 0x400-0x4ff. 1749c825d4dcSJohn Baldwin */ 1750c825d4dcSJohn Baldwin if (end - count + 1 < 0x400) 1751c825d4dcSJohn Baldwin continue; 1752c825d4dcSJohn Baldwin } else { 1753c825d4dcSJohn Baldwin if (end - count + 1 < base) 1754c825d4dcSJohn Baldwin continue; 1755c825d4dcSJohn Baldwin } 1756c825d4dcSJohn Baldwin 1757c825d4dcSJohn Baldwin if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) { 1758c825d4dcSJohn Baldwin w->base = base; 1759c825d4dcSJohn Baldwin w->limit = limit; 1760c825d4dcSJohn Baldwin return (0); 1761c825d4dcSJohn Baldwin } 1762c825d4dcSJohn Baldwin } 1763c825d4dcSJohn Baldwin return (ENOSPC); 1764c825d4dcSJohn Baldwin } 1765c825d4dcSJohn Baldwin 176689977ce2SJustin Hibbits wmask = ((rman_res_t)1 << w->step) - 1; 1767c825d4dcSJohn Baldwin if (RF_ALIGNMENT(flags) < w->step) { 1768c825d4dcSJohn Baldwin flags &= ~RF_ALIGNMENT_MASK; 1769c825d4dcSJohn Baldwin flags |= RF_ALIGNMENT_LOG2(w->step); 1770c825d4dcSJohn Baldwin } 1771c825d4dcSJohn Baldwin start &= ~wmask; 1772c825d4dcSJohn Baldwin end |= wmask; 177389977ce2SJustin Hibbits count = roundup2(count, (rman_res_t)1 << w->step); 1774c825d4dcSJohn Baldwin rid = w->reg; 1775c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, start, end, count, 1776c825d4dcSJohn Baldwin flags & ~RF_ACTIVE); 1777c825d4dcSJohn Baldwin if (res == NULL) 1778c825d4dcSJohn Baldwin return (ENOSPC); 1779c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 1780c825d4dcSJohn Baldwin pcib_activate_window(sc, type); 1781c825d4dcSJohn Baldwin w->base = rman_get_start(res); 1782c825d4dcSJohn Baldwin w->limit = rman_get_end(res); 1783c825d4dcSJohn Baldwin return (0); 1784c825d4dcSJohn Baldwin } 1785c825d4dcSJohn Baldwin 1786c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */ 1787c825d4dcSJohn Baldwin static int 1788c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type, 17892dd1bdf1SJustin Hibbits rman_res_t base, rman_res_t limit) 1790c825d4dcSJohn Baldwin { 1791c825d4dcSJohn Baldwin struct resource *res; 1792c825d4dcSJohn Baldwin int error, i, force_64k_base; 1793c825d4dcSJohn Baldwin 1794c825d4dcSJohn Baldwin KASSERT(base <= w->base && limit >= w->limit, 1795c825d4dcSJohn Baldwin ("attempting to shrink window")); 1796c825d4dcSJohn Baldwin 1797c825d4dcSJohn Baldwin /* 1798c825d4dcSJohn Baldwin * XXX: pcib_grow_window() doesn't try to do this anyway and 1799c825d4dcSJohn Baldwin * the error handling for all the edge cases would be tedious. 1800c825d4dcSJohn Baldwin */ 1801c825d4dcSJohn Baldwin KASSERT(limit == w->limit || base == w->base, 1802c825d4dcSJohn Baldwin ("attempting to grow both ends of a window")); 1803c825d4dcSJohn Baldwin 1804c825d4dcSJohn Baldwin /* 1805c825d4dcSJohn Baldwin * Yet more special handling for requests to expand an I/O 1806c825d4dcSJohn Baldwin * window behind an ISA-enabled bridge. Since I/O windows 1807c825d4dcSJohn Baldwin * have to grow in 0x1000 increments and the end of the 0xffff 1808c825d4dcSJohn Baldwin * range is an alias, growing a window below 64k will always 1809c825d4dcSJohn Baldwin * result in allocating new resources and never adjusting an 1810c825d4dcSJohn Baldwin * existing resource. 1811c825d4dcSJohn Baldwin */ 1812c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1813c825d4dcSJohn Baldwin (limit <= 65535 || (base <= 65535 && base != w->base))) { 1814c825d4dcSJohn Baldwin KASSERT(limit == w->limit || limit <= 65535, 1815c825d4dcSJohn Baldwin ("attempting to grow both ends across 64k ISA alias")); 1816c825d4dcSJohn Baldwin 1817c825d4dcSJohn Baldwin if (base != w->base) 1818c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1); 1819c825d4dcSJohn Baldwin else 1820c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, w->limit + 1, 1821c825d4dcSJohn Baldwin limit); 1822c825d4dcSJohn Baldwin if (error == 0) { 1823c825d4dcSJohn Baldwin w->base = base; 1824c825d4dcSJohn Baldwin w->limit = limit; 1825c825d4dcSJohn Baldwin } 1826c825d4dcSJohn Baldwin return (error); 1827c825d4dcSJohn Baldwin } 1828c825d4dcSJohn Baldwin 1829c825d4dcSJohn Baldwin /* 1830c825d4dcSJohn Baldwin * Find the existing resource to adjust. Usually there is only one, 1831c825d4dcSJohn Baldwin * but for an ISA-enabled bridge we might be growing the I/O window 1832c825d4dcSJohn Baldwin * above 64k and need to find the existing resource that maps all 1833c825d4dcSJohn Baldwin * of the area above 64k. 1834c825d4dcSJohn Baldwin */ 1835c825d4dcSJohn Baldwin for (i = 0; i < w->count; i++) { 1836c825d4dcSJohn Baldwin if (rman_get_end(w->res[i]) == w->limit) 1837c825d4dcSJohn Baldwin break; 1838c825d4dcSJohn Baldwin } 1839c825d4dcSJohn Baldwin KASSERT(i != w->count, ("did not find existing resource")); 1840c825d4dcSJohn Baldwin res = w->res[i]; 1841c825d4dcSJohn Baldwin 1842c825d4dcSJohn Baldwin /* 1843c825d4dcSJohn Baldwin * Usually the resource we found should match the window's 1844c825d4dcSJohn Baldwin * existing range. The one exception is the ISA-enabled case 1845c825d4dcSJohn Baldwin * mentioned above in which case the resource should start at 1846c825d4dcSJohn Baldwin * 64k. 1847c825d4dcSJohn Baldwin */ 1848c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1849c825d4dcSJohn Baldwin w->base <= 65535) { 1850c825d4dcSJohn Baldwin KASSERT(rman_get_start(res) == 65536, 1851c825d4dcSJohn Baldwin ("existing resource mismatch")); 1852c825d4dcSJohn Baldwin force_64k_base = 1; 1853c825d4dcSJohn Baldwin } else { 1854c825d4dcSJohn Baldwin KASSERT(w->base == rman_get_start(res), 1855c825d4dcSJohn Baldwin ("existing resource mismatch")); 1856c825d4dcSJohn Baldwin force_64k_base = 0; 1857c825d4dcSJohn Baldwin } 1858c825d4dcSJohn Baldwin 1859c825d4dcSJohn Baldwin error = bus_adjust_resource(sc->dev, type, res, force_64k_base ? 1860c825d4dcSJohn Baldwin rman_get_start(res) : base, limit); 1861c825d4dcSJohn Baldwin if (error) 1862c825d4dcSJohn Baldwin return (error); 1863c825d4dcSJohn Baldwin 1864c825d4dcSJohn Baldwin /* Add the newly allocated region to the resource manager. */ 1865c825d4dcSJohn Baldwin if (w->base != base) { 1866c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, base, w->base - 1); 1867c825d4dcSJohn Baldwin w->base = base; 1868c825d4dcSJohn Baldwin } else { 1869c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, w->limit + 1, limit); 1870c825d4dcSJohn Baldwin w->limit = limit; 1871c825d4dcSJohn Baldwin } 1872c825d4dcSJohn Baldwin if (error) { 1873c825d4dcSJohn Baldwin if (bootverbose) 1874c825d4dcSJohn Baldwin device_printf(sc->dev, 1875c825d4dcSJohn Baldwin "failed to expand %s resource manager\n", w->name); 1876c825d4dcSJohn Baldwin (void)bus_adjust_resource(sc->dev, type, res, force_64k_base ? 1877c825d4dcSJohn Baldwin rman_get_start(res) : w->base, w->limit); 1878c825d4dcSJohn Baldwin } 1879c825d4dcSJohn Baldwin return (error); 1880c825d4dcSJohn Baldwin } 1881c825d4dcSJohn Baldwin 188283c41143SJohn Baldwin /* 188383c41143SJohn Baldwin * Attempt to grow a window to make room for a given resource request. 188483c41143SJohn Baldwin */ 188583c41143SJohn Baldwin static int 188683c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type, 18872dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 188883c41143SJohn Baldwin { 18892dd1bdf1SJustin Hibbits rman_res_t align, start_free, end_free, front, back, wmask; 1890c825d4dcSJohn Baldwin int error; 189183c41143SJohn Baldwin 189283c41143SJohn Baldwin /* 189383c41143SJohn Baldwin * Clamp the desired resource range to the maximum address 189483c41143SJohn Baldwin * this window supports. Reject impossible requests. 1895c825d4dcSJohn Baldwin * 1896c825d4dcSJohn Baldwin * For I/O port requests behind a bridge with the ISA enable 1897c825d4dcSJohn Baldwin * bit set, force large allocations to start above 64k. 189883c41143SJohn Baldwin */ 189983c41143SJohn Baldwin if (!w->valid) 190083c41143SJohn Baldwin return (EINVAL); 1901c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 && 1902c825d4dcSJohn Baldwin start < 65536) 1903c825d4dcSJohn Baldwin start = 65536; 190483c41143SJohn Baldwin if (end > w->rman.rm_end) 190583c41143SJohn Baldwin end = w->rman.rm_end; 190683c41143SJohn Baldwin if (start + count - 1 > end || start + count < start) 190783c41143SJohn Baldwin return (EINVAL); 190889977ce2SJustin Hibbits wmask = ((rman_res_t)1 << w->step) - 1; 190983c41143SJohn Baldwin 191083c41143SJohn Baldwin /* 191183c41143SJohn Baldwin * If there is no resource at all, just try to allocate enough 191283c41143SJohn Baldwin * aligned space for this resource. 191383c41143SJohn Baldwin */ 191483c41143SJohn Baldwin if (w->res == NULL) { 1915c825d4dcSJohn Baldwin error = pcib_alloc_new_window(sc, w, type, start, end, count, 1916c825d4dcSJohn Baldwin flags); 1917c825d4dcSJohn Baldwin if (error) { 191883c41143SJohn Baldwin if (bootverbose) 191983c41143SJohn Baldwin device_printf(sc->dev, 1920da1b038aSJustin Hibbits "failed to allocate initial %s window (%#jx-%#jx,%#jx)\n", 192183c41143SJohn Baldwin w->name, start, end, count); 192283c41143SJohn Baldwin return (error); 192383c41143SJohn Baldwin } 1924c825d4dcSJohn Baldwin if (bootverbose) 1925c825d4dcSJohn Baldwin device_printf(sc->dev, 1926c825d4dcSJohn Baldwin "allocated initial %s window of %#jx-%#jx\n", 1927c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 192883c41143SJohn Baldwin goto updatewin; 192983c41143SJohn Baldwin } 193083c41143SJohn Baldwin 193183c41143SJohn Baldwin /* 193283c41143SJohn Baldwin * See if growing the window would help. Compute the minimum 193383c41143SJohn Baldwin * amount of address space needed on both the front and back 193483c41143SJohn Baldwin * ends of the existing window to satisfy the allocation. 193583c41143SJohn Baldwin * 193683c41143SJohn Baldwin * For each end, build a candidate region adjusting for the 193783c41143SJohn Baldwin * required alignment, etc. If there is a free region at the 193883c41143SJohn Baldwin * edge of the window, grow from the inner edge of the free 193983c41143SJohn Baldwin * region. Otherwise grow from the window boundary. 194083c41143SJohn Baldwin * 1941c825d4dcSJohn Baldwin * Growing an I/O window below 64k for a bridge with the ISA 1942c825d4dcSJohn Baldwin * enable bit doesn't require any special magic as the step 1943c825d4dcSJohn Baldwin * size of an I/O window (1k) always includes multiple 1944c825d4dcSJohn Baldwin * non-alias ranges when it is grown in either direction. 1945c825d4dcSJohn Baldwin * 194683c41143SJohn Baldwin * XXX: Special case: if w->res is completely empty and the 194783c41143SJohn Baldwin * request size is larger than w->res, we should find the 194883c41143SJohn Baldwin * optimal aligned buffer containing w->res and allocate that. 194983c41143SJohn Baldwin */ 195083c41143SJohn Baldwin if (bootverbose) 195183c41143SJohn Baldwin device_printf(sc->dev, 1952da1b038aSJustin Hibbits "attempting to grow %s window for (%#jx-%#jx,%#jx)\n", 195383c41143SJohn Baldwin w->name, start, end, count); 195489977ce2SJustin Hibbits align = (rman_res_t)1 << RF_ALIGNMENT(flags); 1955c825d4dcSJohn Baldwin if (start < w->base) { 195683c41143SJohn Baldwin if (rman_first_free_region(&w->rman, &start_free, &end_free) != 1957c825d4dcSJohn Baldwin 0 || start_free != w->base) 1958c825d4dcSJohn Baldwin end_free = w->base; 195983c41143SJohn Baldwin if (end_free > end) 1960ddac8cc9SJohn Baldwin end_free = end + 1; 196183c41143SJohn Baldwin 196283c41143SJohn Baldwin /* Move end_free down until it is properly aligned. */ 196383c41143SJohn Baldwin end_free &= ~(align - 1); 1964a49dcb46SJohn Baldwin end_free--; 1965a49dcb46SJohn Baldwin front = end_free - (count - 1); 196683c41143SJohn Baldwin 196783c41143SJohn Baldwin /* 196883c41143SJohn Baldwin * The resource would now be allocated at (front, 196983c41143SJohn Baldwin * end_free). Ensure that fits in the (start, end) 197083c41143SJohn Baldwin * bounds. end_free is checked above. If 'front' is 197183c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 197283c41143SJohn Baldwin * Also check for underflow. 197383c41143SJohn Baldwin */ 197483c41143SJohn Baldwin if (front >= start && front <= end_free) { 197583c41143SJohn Baldwin if (bootverbose) 1976da1b038aSJustin Hibbits printf("\tfront candidate range: %#jx-%#jx\n", 197783c41143SJohn Baldwin front, end_free); 1978a7b5acacSJohn Baldwin front &= ~wmask; 1979c825d4dcSJohn Baldwin front = w->base - front; 198083c41143SJohn Baldwin } else 198183c41143SJohn Baldwin front = 0; 198283c41143SJohn Baldwin } else 198383c41143SJohn Baldwin front = 0; 1984c825d4dcSJohn Baldwin if (end > w->limit) { 198583c41143SJohn Baldwin if (rman_last_free_region(&w->rman, &start_free, &end_free) != 1986c825d4dcSJohn Baldwin 0 || end_free != w->limit) 1987c825d4dcSJohn Baldwin start_free = w->limit + 1; 198883c41143SJohn Baldwin if (start_free < start) 198983c41143SJohn Baldwin start_free = start; 199083c41143SJohn Baldwin 199183c41143SJohn Baldwin /* Move start_free up until it is properly aligned. */ 199283c41143SJohn Baldwin start_free = roundup2(start_free, align); 1993a49dcb46SJohn Baldwin back = start_free + count - 1; 199483c41143SJohn Baldwin 199583c41143SJohn Baldwin /* 199683c41143SJohn Baldwin * The resource would now be allocated at (start_free, 199783c41143SJohn Baldwin * back). Ensure that fits in the (start, end) 199883c41143SJohn Baldwin * bounds. start_free is checked above. If 'back' is 199983c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 200083c41143SJohn Baldwin * Also check for overflow. 200183c41143SJohn Baldwin */ 200283c41143SJohn Baldwin if (back <= end && start_free <= back) { 200383c41143SJohn Baldwin if (bootverbose) 2004da1b038aSJustin Hibbits printf("\tback candidate range: %#jx-%#jx\n", 200583c41143SJohn Baldwin start_free, back); 2006a7b5acacSJohn Baldwin back |= wmask; 2007c825d4dcSJohn Baldwin back -= w->limit; 200883c41143SJohn Baldwin } else 200983c41143SJohn Baldwin back = 0; 201083c41143SJohn Baldwin } else 201183c41143SJohn Baldwin back = 0; 201283c41143SJohn Baldwin 201383c41143SJohn Baldwin /* 201483c41143SJohn Baldwin * Try to allocate the smallest needed region first. 201583c41143SJohn Baldwin * If that fails, fall back to the other region. 201683c41143SJohn Baldwin */ 201783c41143SJohn Baldwin error = ENOSPC; 201883c41143SJohn Baldwin while (front != 0 || back != 0) { 201983c41143SJohn Baldwin if (front != 0 && (front <= back || back == 0)) { 2020c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base - front, 2021c825d4dcSJohn Baldwin w->limit); 202283c41143SJohn Baldwin if (error == 0) 202383c41143SJohn Baldwin break; 202483c41143SJohn Baldwin front = 0; 202583c41143SJohn Baldwin } else { 2026c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base, 2027c825d4dcSJohn Baldwin w->limit + back); 202883c41143SJohn Baldwin if (error == 0) 202983c41143SJohn Baldwin break; 203083c41143SJohn Baldwin back = 0; 203183c41143SJohn Baldwin } 203283c41143SJohn Baldwin } 203383c41143SJohn Baldwin 203483c41143SJohn Baldwin if (error) 203583c41143SJohn Baldwin return (error); 203683c41143SJohn Baldwin if (bootverbose) 2037c825d4dcSJohn Baldwin device_printf(sc->dev, "grew %s window to %#jx-%#jx\n", 2038c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 203983c41143SJohn Baldwin 204083c41143SJohn Baldwin updatewin: 2041c825d4dcSJohn Baldwin /* Write the new window. */ 2042a7b5acacSJohn Baldwin KASSERT((w->base & wmask) == 0, ("start address is not aligned")); 2043a7b5acacSJohn Baldwin KASSERT((w->limit & wmask) == wmask, ("end address is not aligned")); 204483c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 204583c41143SJohn Baldwin return (0); 204683c41143SJohn Baldwin } 204783c41143SJohn Baldwin 204883c41143SJohn Baldwin /* 204983c41143SJohn Baldwin * We have to trap resource allocation requests and ensure that the bridge 205083c41143SJohn Baldwin * is set up to, or capable of handling them. 205183c41143SJohn Baldwin */ 205283c41143SJohn Baldwin struct resource * 205383c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 20542dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 205583c41143SJohn Baldwin { 205683c41143SJohn Baldwin struct pcib_softc *sc; 205783c41143SJohn Baldwin struct resource *r; 205883c41143SJohn Baldwin 205983c41143SJohn Baldwin sc = device_get_softc(dev); 206083c41143SJohn Baldwin 206183c41143SJohn Baldwin /* 206283c41143SJohn Baldwin * VGA resources are decoded iff the VGA enable bit is set in 206383c41143SJohn Baldwin * the bridge control register. VGA resources do not fall into 206483c41143SJohn Baldwin * the resource windows and are passed up to the parent. 206583c41143SJohn Baldwin */ 206683c41143SJohn Baldwin if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) || 206783c41143SJohn Baldwin (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) { 206883c41143SJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) 206983c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, 207083c41143SJohn Baldwin rid, start, end, count, flags)); 207183c41143SJohn Baldwin else 207283c41143SJohn Baldwin return (NULL); 207383c41143SJohn Baldwin } 207483c41143SJohn Baldwin 207583c41143SJohn Baldwin switch (type) { 20764edef187SJohn Baldwin #ifdef PCI_RES_BUS 20774edef187SJohn Baldwin case PCI_RES_BUS: 20784edef187SJohn Baldwin return (pcib_alloc_subbus(&sc->bus, child, rid, start, end, 20794edef187SJohn Baldwin count, flags)); 20804edef187SJohn Baldwin #endif 208183c41143SJohn Baldwin case SYS_RES_IOPORT: 2082c825d4dcSJohn Baldwin if (pcib_is_isa_range(sc, start, end, count)) 2083c825d4dcSJohn Baldwin return (NULL); 208483c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start, 208583c41143SJohn Baldwin end, count, flags); 2086a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 208783c41143SJohn Baldwin break; 208883c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->io, type, start, end, count, 208983c41143SJohn Baldwin flags) == 0) 209083c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, 209183c41143SJohn Baldwin rid, start, end, count, flags); 209283c41143SJohn Baldwin break; 209383c41143SJohn Baldwin case SYS_RES_MEMORY: 209483c41143SJohn Baldwin /* 209583c41143SJohn Baldwin * For prefetchable resources, prefer the prefetchable 209683c41143SJohn Baldwin * memory window, but fall back to the regular memory 209783c41143SJohn Baldwin * window if that fails. Try both windows before 209883c41143SJohn Baldwin * attempting to grow a window in case the firmware 209983c41143SJohn Baldwin * has used a range in the regular memory window to 210083c41143SJohn Baldwin * map a prefetchable BAR. 210183c41143SJohn Baldwin */ 210283c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 210383c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, type, 210483c41143SJohn Baldwin rid, start, end, count, flags); 210583c41143SJohn Baldwin if (r != NULL) 210683c41143SJohn Baldwin break; 210783c41143SJohn Baldwin } 210883c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid, 210983c41143SJohn Baldwin start, end, count, flags); 2110a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 211183c41143SJohn Baldwin break; 211283c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 211383c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->pmem, type, start, end, 211483c41143SJohn Baldwin count, flags) == 0) { 211583c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, 211683c41143SJohn Baldwin type, rid, start, end, count, flags); 211783c41143SJohn Baldwin if (r != NULL) 211883c41143SJohn Baldwin break; 211983c41143SJohn Baldwin } 212083c41143SJohn Baldwin } 212183c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->mem, type, start, end, count, 212283c41143SJohn Baldwin flags & ~RF_PREFETCHABLE) == 0) 212383c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, 212483c41143SJohn Baldwin rid, start, end, count, flags); 212583c41143SJohn Baldwin break; 212683c41143SJohn Baldwin default: 212783c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 212883c41143SJohn Baldwin start, end, count, flags)); 212983c41143SJohn Baldwin } 213083c41143SJohn Baldwin 213183c41143SJohn Baldwin /* 213283c41143SJohn Baldwin * If attempts to suballocate from the window fail but this is a 213383c41143SJohn Baldwin * subtractive bridge, pass the request up the tree. 213483c41143SJohn Baldwin */ 213583c41143SJohn Baldwin if (sc->flags & PCIB_SUBTRACTIVE && r == NULL) 213683c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 213783c41143SJohn Baldwin start, end, count, flags)); 213883c41143SJohn Baldwin return (r); 213983c41143SJohn Baldwin } 214083c41143SJohn Baldwin 214183c41143SJohn Baldwin int 214283c41143SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r, 21432dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end) 214483c41143SJohn Baldwin { 214583c41143SJohn Baldwin struct pcib_softc *sc; 214683c41143SJohn Baldwin 214783c41143SJohn Baldwin sc = device_get_softc(bus); 214883c41143SJohn Baldwin if (pcib_is_resource_managed(sc, type, r)) 214983c41143SJohn Baldwin return (rman_adjust_resource(r, start, end)); 215083c41143SJohn Baldwin return (bus_generic_adjust_resource(bus, child, type, r, start, end)); 215183c41143SJohn Baldwin } 215283c41143SJohn Baldwin 215383c41143SJohn Baldwin int 215483c41143SJohn Baldwin pcib_release_resource(device_t dev, device_t child, int type, int rid, 215583c41143SJohn Baldwin struct resource *r) 215683c41143SJohn Baldwin { 215783c41143SJohn Baldwin struct pcib_softc *sc; 215883c41143SJohn Baldwin int error; 215983c41143SJohn Baldwin 216083c41143SJohn Baldwin sc = device_get_softc(dev); 216183c41143SJohn Baldwin if (pcib_is_resource_managed(sc, type, r)) { 216283c41143SJohn Baldwin if (rman_get_flags(r) & RF_ACTIVE) { 216383c41143SJohn Baldwin error = bus_deactivate_resource(child, type, rid, r); 216483c41143SJohn Baldwin if (error) 216583c41143SJohn Baldwin return (error); 216683c41143SJohn Baldwin } 216783c41143SJohn Baldwin return (rman_release_resource(r)); 216883c41143SJohn Baldwin } 216983c41143SJohn Baldwin return (bus_generic_release_resource(dev, child, type, rid, r)); 217083c41143SJohn Baldwin } 217183c41143SJohn Baldwin #else 2172bb0d0a8eSMike Smith /* 2173bb0d0a8eSMike Smith * We have to trap resource allocation requests and ensure that the bridge 2174bb0d0a8eSMike Smith * is set up to, or capable of handling them. 2175bb0d0a8eSMike Smith */ 21766f0d5884SJohn Baldwin struct resource * 2177bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 21782dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 2179bb0d0a8eSMike Smith { 2180bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 218126043836SJohn Baldwin const char *name, *suffix; 2182a8b354a8SWarner Losh int ok; 2183bb0d0a8eSMike Smith 2184bb0d0a8eSMike Smith /* 2185bb0d0a8eSMike Smith * Fail the allocation for this range if it's not supported. 2186bb0d0a8eSMike Smith */ 218726043836SJohn Baldwin name = device_get_nameunit(child); 218826043836SJohn Baldwin if (name == NULL) { 218926043836SJohn Baldwin name = ""; 219026043836SJohn Baldwin suffix = ""; 219126043836SJohn Baldwin } else 219226043836SJohn Baldwin suffix = " "; 2193bb0d0a8eSMike Smith switch (type) { 2194bb0d0a8eSMike Smith case SYS_RES_IOPORT: 2195a8b354a8SWarner Losh ok = 0; 2196e4b59fc5SWarner Losh if (!pcib_is_io_open(sc)) 2197e4b59fc5SWarner Losh break; 2198a8b354a8SWarner Losh ok = (start >= sc->iobase && end <= sc->iolimit); 2199d98d9b12SMarcel Moolenaar 2200d98d9b12SMarcel Moolenaar /* 2201d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA I/O addresses when the 2202d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 2203d98d9b12SMarcel Moolenaar */ 2204d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_ioport_range(start, end)) 2205d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 2206d98d9b12SMarcel Moolenaar 2207e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 2208a8b354a8SWarner Losh if (!ok) { 220912b8c86eSWarner Losh if (start < sc->iobase) 221012b8c86eSWarner Losh start = sc->iobase; 221112b8c86eSWarner Losh if (end > sc->iolimit) 221212b8c86eSWarner Losh end = sc->iolimit; 22132daa7a07SWarner Losh if (start < end) 22142daa7a07SWarner Losh ok = 1; 2215a8b354a8SWarner Losh } 22161c54ff33SMatthew N. Dodd } else { 2217e4b59fc5SWarner Losh ok = 1; 22189dffe835SWarner Losh #if 0 2219795dceffSWarner Losh /* 2220795dceffSWarner Losh * If we overlap with the subtractive range, then 2221795dceffSWarner Losh * pick the upper range to use. 2222795dceffSWarner Losh */ 2223795dceffSWarner Losh if (start < sc->iolimit && end > sc->iobase) 2224795dceffSWarner Losh start = sc->iolimit + 1; 22259dffe835SWarner Losh #endif 222612b8c86eSWarner Losh } 2227a8b354a8SWarner Losh if (end < start) { 2228da1b038aSJustin Hibbits device_printf(dev, "ioport: end (%jx) < start (%jx)\n", 22292daa7a07SWarner Losh end, start); 2230a8b354a8SWarner Losh start = 0; 2231a8b354a8SWarner Losh end = 0; 2232a8b354a8SWarner Losh ok = 0; 2233a8b354a8SWarner Losh } 2234a8b354a8SWarner Losh if (!ok) { 223526043836SJohn Baldwin device_printf(dev, "%s%srequested unsupported I/O " 2236da1b038aSJustin Hibbits "range 0x%jx-0x%jx (decoding 0x%x-0x%x)\n", 223726043836SJohn Baldwin name, suffix, start, end, sc->iobase, sc->iolimit); 2238bb0d0a8eSMike Smith return (NULL); 2239bb0d0a8eSMike Smith } 22404fa59183SMike Smith if (bootverbose) 22412daa7a07SWarner Losh device_printf(dev, 2242da1b038aSJustin Hibbits "%s%srequested I/O range 0x%jx-0x%jx: in range\n", 224326043836SJohn Baldwin name, suffix, start, end); 2244bb0d0a8eSMike Smith break; 2245bb0d0a8eSMike Smith 2246bb0d0a8eSMike Smith case SYS_RES_MEMORY: 2247a8b354a8SWarner Losh ok = 0; 2248a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 2249a8b354a8SWarner Losh ok = ok || (start >= sc->membase && end <= sc->memlimit); 2250a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) 2251a8b354a8SWarner Losh ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit); 2252d98d9b12SMarcel Moolenaar 2253d98d9b12SMarcel Moolenaar /* 2254d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA memory addresses when the 2255d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 2256d98d9b12SMarcel Moolenaar */ 2257d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_memory_range(start, end)) 2258d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 2259d98d9b12SMarcel Moolenaar 2260e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 2261a8b354a8SWarner Losh if (!ok) { 2262a8b354a8SWarner Losh ok = 1; 2263a8b354a8SWarner Losh if (flags & RF_PREFETCHABLE) { 2264a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 2265a8b354a8SWarner Losh if (start < sc->pmembase) 2266a8b354a8SWarner Losh start = sc->pmembase; 2267a8b354a8SWarner Losh if (end > sc->pmemlimit) 2268a8b354a8SWarner Losh end = sc->pmemlimit; 2269a8b354a8SWarner Losh } else { 2270a8b354a8SWarner Losh ok = 0; 2271a8b354a8SWarner Losh } 2272a8b354a8SWarner Losh } else { /* non-prefetchable */ 2273a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 2274a8b354a8SWarner Losh if (start < sc->membase) 227512b8c86eSWarner Losh start = sc->membase; 227612b8c86eSWarner Losh if (end > sc->memlimit) 227712b8c86eSWarner Losh end = sc->memlimit; 22781c54ff33SMatthew N. Dodd } else { 2279a8b354a8SWarner Losh ok = 0; 2280a8b354a8SWarner Losh } 2281a8b354a8SWarner Losh } 2282a8b354a8SWarner Losh } 2283a8b354a8SWarner Losh } else if (!ok) { 2284e4b59fc5SWarner Losh ok = 1; /* subtractive bridge: always ok */ 22859dffe835SWarner Losh #if 0 2286a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 2287795dceffSWarner Losh if (start < sc->memlimit && end > sc->membase) 2288795dceffSWarner Losh start = sc->memlimit + 1; 2289a8b354a8SWarner Losh } 2290a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 2291795dceffSWarner Losh if (start < sc->pmemlimit && end > sc->pmembase) 2292795dceffSWarner Losh start = sc->pmemlimit + 1; 22931c54ff33SMatthew N. Dodd } 22949dffe835SWarner Losh #endif 229512b8c86eSWarner Losh } 2296a8b354a8SWarner Losh if (end < start) { 2297da1b038aSJustin Hibbits device_printf(dev, "memory: end (%jx) < start (%jx)\n", 22982daa7a07SWarner Losh end, start); 2299a8b354a8SWarner Losh start = 0; 2300a8b354a8SWarner Losh end = 0; 2301a8b354a8SWarner Losh ok = 0; 2302a8b354a8SWarner Losh } 2303a8b354a8SWarner Losh if (!ok && bootverbose) 230434428485SWarner Losh device_printf(dev, 2305da1b038aSJustin Hibbits "%s%srequested unsupported memory range %#jx-%#jx " 2306b0a2d4b8SWarner Losh "(decoding %#jx-%#jx, %#jx-%#jx)\n", 230726043836SJohn Baldwin name, suffix, start, end, 2308b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit, 2309b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 2310a8b354a8SWarner Losh if (!ok) 2311bb0d0a8eSMike Smith return (NULL); 23124fa59183SMike Smith if (bootverbose) 231326043836SJohn Baldwin device_printf(dev,"%s%srequested memory range " 2314da1b038aSJustin Hibbits "0x%jx-0x%jx: good\n", 231526043836SJohn Baldwin name, suffix, start, end); 23164fa59183SMike Smith break; 23174fa59183SMike Smith 2318bb0d0a8eSMike Smith default: 23194fa59183SMike Smith break; 2320bb0d0a8eSMike Smith } 2321bb0d0a8eSMike Smith /* 2322bb0d0a8eSMike Smith * Bridge is OK decoding this resource, so pass it up. 2323bb0d0a8eSMike Smith */ 23242daa7a07SWarner Losh return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 23252daa7a07SWarner Losh count, flags)); 2326bb0d0a8eSMike Smith } 232783c41143SJohn Baldwin #endif 2328bb0d0a8eSMike Smith 2329bb0d0a8eSMike Smith /* 233055d3ea17SRyan Stone * If ARI is enabled on this downstream port, translate the function number 233155d3ea17SRyan Stone * to the non-ARI slot/function. The downstream port will convert it back in 233255d3ea17SRyan Stone * hardware. If ARI is not enabled slot and func are not modified. 233355d3ea17SRyan Stone */ 233455d3ea17SRyan Stone static __inline void 233555d3ea17SRyan Stone pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func) 233655d3ea17SRyan Stone { 233755d3ea17SRyan Stone struct pcib_softc *sc; 233855d3ea17SRyan Stone int ari_func; 233955d3ea17SRyan Stone 234055d3ea17SRyan Stone sc = device_get_softc(pcib); 234155d3ea17SRyan Stone ari_func = *func; 234255d3ea17SRyan Stone 234355d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 234455d3ea17SRyan Stone KASSERT(*slot == 0, 234555d3ea17SRyan Stone ("Non-zero slot number with ARI enabled!")); 234655d3ea17SRyan Stone *slot = PCIE_ARI_SLOT(ari_func); 234755d3ea17SRyan Stone *func = PCIE_ARI_FUNC(ari_func); 234855d3ea17SRyan Stone } 234955d3ea17SRyan Stone } 235055d3ea17SRyan Stone 235155d3ea17SRyan Stone 235255d3ea17SRyan Stone static void 235355d3ea17SRyan Stone pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos) 235455d3ea17SRyan Stone { 235555d3ea17SRyan Stone uint32_t ctl2; 235655d3ea17SRyan Stone 235755d3ea17SRyan Stone ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4); 235855d3ea17SRyan Stone ctl2 |= PCIEM_CTL2_ARI; 235955d3ea17SRyan Stone pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4); 236055d3ea17SRyan Stone 236155d3ea17SRyan Stone sc->flags |= PCIB_ENABLE_ARI; 236255d3ea17SRyan Stone } 236355d3ea17SRyan Stone 236455d3ea17SRyan Stone /* 2365bb0d0a8eSMike Smith * PCIB interface. 2366bb0d0a8eSMike Smith */ 23676f0d5884SJohn Baldwin int 2368bb0d0a8eSMike Smith pcib_maxslots(device_t dev) 2369bb0d0a8eSMike Smith { 23704fa59183SMike Smith return (PCI_SLOTMAX); 2371bb0d0a8eSMike Smith } 2372bb0d0a8eSMike Smith 237355d3ea17SRyan Stone static int 237455d3ea17SRyan Stone pcib_ari_maxslots(device_t dev) 237555d3ea17SRyan Stone { 237655d3ea17SRyan Stone struct pcib_softc *sc; 237755d3ea17SRyan Stone 237855d3ea17SRyan Stone sc = device_get_softc(dev); 237955d3ea17SRyan Stone 238055d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 238155d3ea17SRyan Stone return (PCIE_ARI_SLOTMAX); 238255d3ea17SRyan Stone else 238355d3ea17SRyan Stone return (PCI_SLOTMAX); 238455d3ea17SRyan Stone } 238555d3ea17SRyan Stone 238655d3ea17SRyan Stone static int 238755d3ea17SRyan Stone pcib_ari_maxfuncs(device_t dev) 238855d3ea17SRyan Stone { 238955d3ea17SRyan Stone struct pcib_softc *sc; 239055d3ea17SRyan Stone 239155d3ea17SRyan Stone sc = device_get_softc(dev); 239255d3ea17SRyan Stone 239355d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 239455d3ea17SRyan Stone return (PCIE_ARI_FUNCMAX); 239555d3ea17SRyan Stone else 239655d3ea17SRyan Stone return (PCI_FUNCMAX); 239755d3ea17SRyan Stone } 239855d3ea17SRyan Stone 23992397d2d8SRyan Stone static void 24002397d2d8SRyan Stone pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot, 24012397d2d8SRyan Stone int *func) 24022397d2d8SRyan Stone { 24032397d2d8SRyan Stone struct pcib_softc *sc; 24042397d2d8SRyan Stone 24052397d2d8SRyan Stone sc = device_get_softc(pcib); 24062397d2d8SRyan Stone 24072397d2d8SRyan Stone *bus = PCI_RID2BUS(rid); 24082397d2d8SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 24092397d2d8SRyan Stone *slot = PCIE_ARI_RID2SLOT(rid); 24102397d2d8SRyan Stone *func = PCIE_ARI_RID2FUNC(rid); 24112397d2d8SRyan Stone } else { 24122397d2d8SRyan Stone *slot = PCI_RID2SLOT(rid); 24132397d2d8SRyan Stone *func = PCI_RID2FUNC(rid); 24142397d2d8SRyan Stone } 24152397d2d8SRyan Stone } 24162397d2d8SRyan Stone 2417bb0d0a8eSMike Smith /* 2418bb0d0a8eSMike Smith * Since we are a child of a PCI bus, its parent must support the pcib interface. 2419bb0d0a8eSMike Smith */ 242055d3ea17SRyan Stone static uint32_t 2421795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width) 2422bb0d0a8eSMike Smith { 242382cb5c3bSJohn Baldwin #ifdef PCI_HP 242482cb5c3bSJohn Baldwin struct pcib_softc *sc; 242555d3ea17SRyan Stone 242682cb5c3bSJohn Baldwin sc = device_get_softc(dev); 242782cb5c3bSJohn Baldwin if (!pcib_present(sc)) { 242882cb5c3bSJohn Baldwin switch (width) { 242982cb5c3bSJohn Baldwin case 2: 243082cb5c3bSJohn Baldwin return (0xffff); 243182cb5c3bSJohn Baldwin case 1: 243282cb5c3bSJohn Baldwin return (0xff); 243382cb5c3bSJohn Baldwin default: 243482cb5c3bSJohn Baldwin return (0xffffffff); 243582cb5c3bSJohn Baldwin } 243682cb5c3bSJohn Baldwin } 243782cb5c3bSJohn Baldwin #endif 243855d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 243955d3ea17SRyan Stone return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, 244055d3ea17SRyan Stone f, reg, width)); 2441bb0d0a8eSMike Smith } 2442bb0d0a8eSMike Smith 244355d3ea17SRyan Stone static void 2444795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width) 2445bb0d0a8eSMike Smith { 244682cb5c3bSJohn Baldwin #ifdef PCI_HP 244782cb5c3bSJohn Baldwin struct pcib_softc *sc; 244855d3ea17SRyan Stone 244982cb5c3bSJohn Baldwin sc = device_get_softc(dev); 245082cb5c3bSJohn Baldwin if (!pcib_present(sc)) 245182cb5c3bSJohn Baldwin return; 245282cb5c3bSJohn Baldwin #endif 245355d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 245455d3ea17SRyan Stone PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, 245555d3ea17SRyan Stone reg, val, width); 2456bb0d0a8eSMike Smith } 2457bb0d0a8eSMike Smith 2458bb0d0a8eSMike Smith /* 2459bb0d0a8eSMike Smith * Route an interrupt across a PCI bridge. 2460bb0d0a8eSMike Smith */ 24612c2d1d07SBenno Rice int 2462bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin) 2463bb0d0a8eSMike Smith { 2464bb0d0a8eSMike Smith device_t bus; 2465bb0d0a8eSMike Smith int parent_intpin; 2466bb0d0a8eSMike Smith int intnum; 2467bb0d0a8eSMike Smith 2468bb0d0a8eSMike Smith /* 2469bb0d0a8eSMike Smith * 2470bb0d0a8eSMike Smith * The PCI standard defines a swizzle of the child-side device/intpin to 2471bb0d0a8eSMike Smith * the parent-side intpin as follows. 2472bb0d0a8eSMike Smith * 2473bb0d0a8eSMike Smith * device = device on child bus 2474bb0d0a8eSMike Smith * child_intpin = intpin on child bus slot (0-3) 2475bb0d0a8eSMike Smith * parent_intpin = intpin on parent bus slot (0-3) 2476bb0d0a8eSMike Smith * 2477bb0d0a8eSMike Smith * parent_intpin = (device + child_intpin) % 4 2478bb0d0a8eSMike Smith */ 2479cdc95e1bSBernd Walter parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4; 2480bb0d0a8eSMike Smith 2481bb0d0a8eSMike Smith /* 2482bb0d0a8eSMike Smith * Our parent is a PCI bus. Its parent must export the pcib interface 2483bb0d0a8eSMike Smith * which includes the ability to route interrupts. 2484bb0d0a8eSMike Smith */ 2485bb0d0a8eSMike Smith bus = device_get_parent(pcib); 2486bb0d0a8eSMike Smith intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1); 248739981fedSJohn Baldwin if (PCI_INTERRUPT_VALID(intnum) && bootverbose) { 2488c6a121abSJohn Baldwin device_printf(pcib, "slot %d INT%c is routed to irq %d\n", 2489c6a121abSJohn Baldwin pci_get_slot(dev), 'A' + pin - 1, intnum); 24908046c4b9SMike Smith } 2491bb0d0a8eSMike Smith return(intnum); 2492bb0d0a8eSMike Smith } 2493b173edafSJohn Baldwin 2494e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */ 24959bf4c9c1SJohn Baldwin int 24969bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs) 24979bf4c9c1SJohn Baldwin { 2498bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 24999bf4c9c1SJohn Baldwin device_t bus; 25009bf4c9c1SJohn Baldwin 250122bf1c7fSJohn Baldwin if (sc->flags & PCIB_DISABLE_MSI) 250222bf1c7fSJohn Baldwin return (ENXIO); 25039bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 25049bf4c9c1SJohn Baldwin return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 25059bf4c9c1SJohn Baldwin irqs)); 25069bf4c9c1SJohn Baldwin } 25079bf4c9c1SJohn Baldwin 2508e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */ 25099bf4c9c1SJohn Baldwin int 25109bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs) 25119bf4c9c1SJohn Baldwin { 25129bf4c9c1SJohn Baldwin device_t bus; 25139bf4c9c1SJohn Baldwin 25149bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 25159bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs)); 25169bf4c9c1SJohn Baldwin } 25179bf4c9c1SJohn Baldwin 25189bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */ 25199bf4c9c1SJohn Baldwin int 2520e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq) 25219bf4c9c1SJohn Baldwin { 2522bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 25239bf4c9c1SJohn Baldwin device_t bus; 25249bf4c9c1SJohn Baldwin 252568e9cbd3SMarius Strobl if (sc->flags & PCIB_DISABLE_MSIX) 252622bf1c7fSJohn Baldwin return (ENXIO); 25279bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 2528e706f7f0SJohn Baldwin return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq)); 25295fe82bcaSJohn Baldwin } 25305fe82bcaSJohn Baldwin 25319bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */ 25329bf4c9c1SJohn Baldwin int 25339bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq) 25349bf4c9c1SJohn Baldwin { 25359bf4c9c1SJohn Baldwin device_t bus; 25369bf4c9c1SJohn Baldwin 25379bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 25389bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq)); 25399bf4c9c1SJohn Baldwin } 25409bf4c9c1SJohn Baldwin 2541e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */ 2542e706f7f0SJohn Baldwin int 2543e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, 2544e706f7f0SJohn Baldwin uint32_t *data) 2545e706f7f0SJohn Baldwin { 2546e706f7f0SJohn Baldwin device_t bus; 25474522ac77SLuoqi Chen int error; 2548e706f7f0SJohn Baldwin 2549e706f7f0SJohn Baldwin bus = device_get_parent(pcib); 25504522ac77SLuoqi Chen error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data); 25514522ac77SLuoqi Chen if (error) 25524522ac77SLuoqi Chen return (error); 25534522ac77SLuoqi Chen 25544522ac77SLuoqi Chen pci_ht_map_msi(pcib, *addr); 25554522ac77SLuoqi Chen return (0); 2556e706f7f0SJohn Baldwin } 2557e706f7f0SJohn Baldwin 255862508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */ 255962508c53SJohn Baldwin int 256062508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate) 256162508c53SJohn Baldwin { 256262508c53SJohn Baldwin device_t bus; 256362508c53SJohn Baldwin 256462508c53SJohn Baldwin bus = device_get_parent(pcib); 256562508c53SJohn Baldwin return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate)); 256662508c53SJohn Baldwin } 25675605a99eSRyan Stone 25682397d2d8SRyan Stone static int 25692397d2d8SRyan Stone pcib_ari_enabled(device_t pcib) 25702397d2d8SRyan Stone { 25712397d2d8SRyan Stone struct pcib_softc *sc; 25722397d2d8SRyan Stone 25732397d2d8SRyan Stone sc = device_get_softc(pcib); 25742397d2d8SRyan Stone 25752397d2d8SRyan Stone return ((sc->flags & PCIB_ENABLE_ARI) != 0); 25762397d2d8SRyan Stone } 25772397d2d8SRyan Stone 2578*d7be980dSAndrew Turner static int 2579*d7be980dSAndrew Turner pcib_ari_get_id(device_t pcib, device_t dev, enum pci_id_type type, 2580*d7be980dSAndrew Turner uintptr_t *id) 258155d3ea17SRyan Stone { 258255d3ea17SRyan Stone struct pcib_softc *sc; 258355d3ea17SRyan Stone uint8_t bus, slot, func; 258455d3ea17SRyan Stone 2585*d7be980dSAndrew Turner if (type != PCI_ID_RID) 2586*d7be980dSAndrew Turner return (ENXIO); 2587*d7be980dSAndrew Turner 258855d3ea17SRyan Stone sc = device_get_softc(pcib); 258955d3ea17SRyan Stone 259055d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 259155d3ea17SRyan Stone bus = pci_get_bus(dev); 259255d3ea17SRyan Stone func = pci_get_function(dev); 259355d3ea17SRyan Stone 2594*d7be980dSAndrew Turner *id = (PCI_ARI_RID(bus, func)); 259555d3ea17SRyan Stone } else { 259655d3ea17SRyan Stone bus = pci_get_bus(dev); 259755d3ea17SRyan Stone slot = pci_get_slot(dev); 259855d3ea17SRyan Stone func = pci_get_function(dev); 259955d3ea17SRyan Stone 2600*d7be980dSAndrew Turner *id = (PCI_RID(bus, slot, func)); 260155d3ea17SRyan Stone } 2602*d7be980dSAndrew Turner 2603*d7be980dSAndrew Turner return (0); 260455d3ea17SRyan Stone } 260555d3ea17SRyan Stone 260655d3ea17SRyan Stone /* 260755d3ea17SRyan Stone * Check that the downstream port (pcib) and the endpoint device (dev) both 260855d3ea17SRyan Stone * support ARI. If so, enable it and return 0, otherwise return an error. 260955d3ea17SRyan Stone */ 261055d3ea17SRyan Stone static int 261155d3ea17SRyan Stone pcib_try_enable_ari(device_t pcib, device_t dev) 261255d3ea17SRyan Stone { 261355d3ea17SRyan Stone struct pcib_softc *sc; 261455d3ea17SRyan Stone int error; 261555d3ea17SRyan Stone uint32_t cap2; 261655d3ea17SRyan Stone int ari_cap_off; 261755d3ea17SRyan Stone uint32_t ari_ver; 261855d3ea17SRyan Stone uint32_t pcie_pos; 261955d3ea17SRyan Stone 262055d3ea17SRyan Stone sc = device_get_softc(pcib); 262155d3ea17SRyan Stone 262255d3ea17SRyan Stone /* 262355d3ea17SRyan Stone * ARI is controlled in a register in the PCIe capability structure. 262455d3ea17SRyan Stone * If the downstream port does not have the PCIe capability structure 262555d3ea17SRyan Stone * then it does not support ARI. 262655d3ea17SRyan Stone */ 262755d3ea17SRyan Stone error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos); 262855d3ea17SRyan Stone if (error != 0) 262955d3ea17SRyan Stone return (ENODEV); 263055d3ea17SRyan Stone 263155d3ea17SRyan Stone /* Check that the PCIe port advertises ARI support. */ 263255d3ea17SRyan Stone cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4); 263355d3ea17SRyan Stone if (!(cap2 & PCIEM_CAP2_ARI)) 263455d3ea17SRyan Stone return (ENODEV); 263555d3ea17SRyan Stone 263655d3ea17SRyan Stone /* 263755d3ea17SRyan Stone * Check that the endpoint device advertises ARI support via the ARI 263855d3ea17SRyan Stone * extended capability structure. 263955d3ea17SRyan Stone */ 264055d3ea17SRyan Stone error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off); 264155d3ea17SRyan Stone if (error != 0) 264255d3ea17SRyan Stone return (ENODEV); 264355d3ea17SRyan Stone 264455d3ea17SRyan Stone /* 264555d3ea17SRyan Stone * Finally, check that the endpoint device supports the same version 264655d3ea17SRyan Stone * of ARI that we do. 264755d3ea17SRyan Stone */ 264855d3ea17SRyan Stone ari_ver = pci_read_config(dev, ari_cap_off, 4); 264955d3ea17SRyan Stone if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) { 265055d3ea17SRyan Stone if (bootverbose) 265155d3ea17SRyan Stone device_printf(pcib, 265255d3ea17SRyan Stone "Unsupported version of ARI (%d) detected\n", 265355d3ea17SRyan Stone PCI_EXTCAP_VER(ari_ver)); 265455d3ea17SRyan Stone 265555d3ea17SRyan Stone return (ENXIO); 265655d3ea17SRyan Stone } 265755d3ea17SRyan Stone 265855d3ea17SRyan Stone pcib_enable_ari(sc, pcie_pos); 265955d3ea17SRyan Stone 266055d3ea17SRyan Stone return (0); 266155d3ea17SRyan Stone } 2662