1bb0d0a8eSMike Smith /*- 2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3718cf2ccSPedro F. Giffuni * 4bb0d0a8eSMike Smith * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier 5bb0d0a8eSMike Smith * Copyright (c) 2000 Michael Smith <msmith@freebsd.org> 6bb0d0a8eSMike Smith * Copyright (c) 2000 BSDi 7bb0d0a8eSMike Smith * All rights reserved. 8bb0d0a8eSMike Smith * 9bb0d0a8eSMike Smith * Redistribution and use in source and binary forms, with or without 10bb0d0a8eSMike Smith * modification, are permitted provided that the following conditions 11bb0d0a8eSMike Smith * are met: 12bb0d0a8eSMike Smith * 1. Redistributions of source code must retain the above copyright 13bb0d0a8eSMike Smith * notice, this list of conditions and the following disclaimer. 14bb0d0a8eSMike Smith * 2. Redistributions in binary form must reproduce the above copyright 15bb0d0a8eSMike Smith * notice, this list of conditions and the following disclaimer in the 16bb0d0a8eSMike Smith * documentation and/or other materials provided with the distribution. 17bb0d0a8eSMike Smith * 3. The name of the author may not be used to endorse or promote products 18bb0d0a8eSMike Smith * derived from this software without specific prior written permission. 19bb0d0a8eSMike Smith * 20bb0d0a8eSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21bb0d0a8eSMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22bb0d0a8eSMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23bb0d0a8eSMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24bb0d0a8eSMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25bb0d0a8eSMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26bb0d0a8eSMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27bb0d0a8eSMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28bb0d0a8eSMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29bb0d0a8eSMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30bb0d0a8eSMike Smith * SUCH DAMAGE. 31bb0d0a8eSMike Smith */ 32bb0d0a8eSMike Smith 33aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 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> 43e2e050c8SConrad Meyer #include <sys/lock.h> 4483c41143SJohn Baldwin #include <sys/malloc.h> 4583c41143SJohn Baldwin #include <sys/module.h> 46e2e050c8SConrad Meyer #include <sys/mutex.h> 475db2a4a8SKonstantin Belousov #include <sys/pciio.h> 48a8b354a8SWarner Losh #include <sys/rman.h> 491c54ff33SMatthew N. Dodd #include <sys/sysctl.h> 5083c41143SJohn Baldwin #include <sys/systm.h> 5182cb5c3bSJohn Baldwin #include <sys/taskqueue.h> 52bb0d0a8eSMike Smith 5338d8c994SWarner Losh #include <dev/pci/pcivar.h> 5438d8c994SWarner Losh #include <dev/pci/pcireg.h> 5562508c53SJohn Baldwin #include <dev/pci/pci_private.h> 5638d8c994SWarner Losh #include <dev/pci/pcib_private.h> 57bb0d0a8eSMike Smith 58bb0d0a8eSMike Smith #include "pcib_if.h" 59bb0d0a8eSMike Smith 60bb0d0a8eSMike Smith static int pcib_probe(device_t dev); 61e36af292SJung-uk Kim static int pcib_resume(device_t dev); 6203719c65SJohn Baldwin 6303719c65SJohn Baldwin static bus_child_present_t pcib_child_present; 6403719c65SJohn Baldwin static bus_alloc_resource_t pcib_alloc_resource; 6503719c65SJohn Baldwin static bus_adjust_resource_t pcib_adjust_resource; 6603719c65SJohn Baldwin static bus_release_resource_t pcib_release_resource; 67b377ff81SJohn Baldwin static bus_activate_resource_t pcib_activate_resource; 68b377ff81SJohn Baldwin static bus_deactivate_resource_t pcib_deactivate_resource; 69b377ff81SJohn Baldwin static bus_map_resource_t pcib_map_resource; 70b377ff81SJohn Baldwin static bus_unmap_resource_t pcib_unmap_resource; 7103719c65SJohn Baldwin static int pcib_reset_child(device_t dev, device_t child, int flags); 7203719c65SJohn Baldwin 7362508c53SJohn Baldwin static int pcib_power_for_sleep(device_t pcib, device_t dev, 7462508c53SJohn Baldwin int *pstate); 75d7be980dSAndrew Turner static int pcib_ari_get_id(device_t pcib, device_t dev, 76d7be980dSAndrew Turner enum pci_id_type type, uintptr_t *id); 7755d3ea17SRyan Stone static uint32_t pcib_read_config(device_t dev, u_int b, u_int s, 7855d3ea17SRyan Stone u_int f, u_int reg, int width); 7955d3ea17SRyan Stone static void pcib_write_config(device_t dev, u_int b, u_int s, 8055d3ea17SRyan Stone u_int f, u_int reg, uint32_t val, int width); 8155d3ea17SRyan Stone static int pcib_ari_maxslots(device_t dev); 8255d3ea17SRyan Stone static int pcib_ari_maxfuncs(device_t dev); 8355d3ea17SRyan Stone static int pcib_try_enable_ari(device_t pcib, device_t dev); 842397d2d8SRyan Stone static int pcib_ari_enabled(device_t pcib); 852397d2d8SRyan Stone static void pcib_ari_decode_rid(device_t pcib, uint16_t rid, 862397d2d8SRyan Stone int *bus, int *slot, int *func); 8782cb5c3bSJohn Baldwin #ifdef PCI_HP 88fa3b03d3SAlexander Motin static void pcib_pcie_ab_timeout(void *arg, int pending); 89fa3b03d3SAlexander Motin static void pcib_pcie_cc_timeout(void *arg, int pending); 90fa3b03d3SAlexander Motin static void pcib_pcie_dll_timeout(void *arg, int pending); 9182cb5c3bSJohn Baldwin #endif 921ffd07bdSJohn Baldwin static int pcib_request_feature_default(device_t pcib, device_t dev, 934cb67729SWarner Losh enum pci_feature feature); 94bb0d0a8eSMike Smith 95bb0d0a8eSMike Smith static device_method_t pcib_methods[] = { 96bb0d0a8eSMike Smith /* Device interface */ 97bb0d0a8eSMike Smith DEVMETHOD(device_probe, pcib_probe), 98bb0d0a8eSMike Smith DEVMETHOD(device_attach, pcib_attach), 996f33eaa5SJohn Baldwin DEVMETHOD(device_detach, pcib_detach), 100bb0d0a8eSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 1018415a654SJessica Clarke DEVMETHOD(device_suspend, bus_generic_suspend), 102e36af292SJung-uk Kim DEVMETHOD(device_resume, pcib_resume), 103bb0d0a8eSMike Smith 104bb0d0a8eSMike Smith /* Bus interface */ 10582cb5c3bSJohn Baldwin DEVMETHOD(bus_child_present, pcib_child_present), 106bb0d0a8eSMike Smith DEVMETHOD(bus_read_ivar, pcib_read_ivar), 107bb0d0a8eSMike Smith DEVMETHOD(bus_write_ivar, pcib_write_ivar), 108bb0d0a8eSMike Smith DEVMETHOD(bus_alloc_resource, pcib_alloc_resource), 10983c41143SJohn Baldwin DEVMETHOD(bus_adjust_resource, pcib_adjust_resource), 11083c41143SJohn Baldwin DEVMETHOD(bus_release_resource, pcib_release_resource), 111b377ff81SJohn Baldwin DEVMETHOD(bus_activate_resource, pcib_activate_resource), 112b377ff81SJohn Baldwin DEVMETHOD(bus_deactivate_resource, pcib_deactivate_resource), 113b377ff81SJohn Baldwin DEVMETHOD(bus_map_resource, pcib_map_resource), 114b377ff81SJohn Baldwin DEVMETHOD(bus_unmap_resource, pcib_unmap_resource), 115bb0d0a8eSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 116bb0d0a8eSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 1175db2a4a8SKonstantin Belousov DEVMETHOD(bus_reset_child, pcib_reset_child), 118bb0d0a8eSMike Smith 119bb0d0a8eSMike Smith /* pcib interface */ 12055d3ea17SRyan Stone DEVMETHOD(pcib_maxslots, pcib_ari_maxslots), 12155d3ea17SRyan Stone DEVMETHOD(pcib_maxfuncs, pcib_ari_maxfuncs), 122bb0d0a8eSMike Smith DEVMETHOD(pcib_read_config, pcib_read_config), 123bb0d0a8eSMike Smith DEVMETHOD(pcib_write_config, pcib_write_config), 124bb0d0a8eSMike Smith DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt), 1259bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi), 1269bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msi, pcib_release_msi), 1279bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix), 1289bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msix, pcib_release_msix), 129e706f7f0SJohn Baldwin DEVMETHOD(pcib_map_msi, pcib_map_msi), 13062508c53SJohn Baldwin DEVMETHOD(pcib_power_for_sleep, pcib_power_for_sleep), 131d7be980dSAndrew Turner DEVMETHOD(pcib_get_id, pcib_ari_get_id), 13255d3ea17SRyan Stone DEVMETHOD(pcib_try_enable_ari, pcib_try_enable_ari), 1332397d2d8SRyan Stone DEVMETHOD(pcib_ari_enabled, pcib_ari_enabled), 1342397d2d8SRyan Stone DEVMETHOD(pcib_decode_rid, pcib_ari_decode_rid), 1351ffd07bdSJohn Baldwin DEVMETHOD(pcib_request_feature, pcib_request_feature_default), 136bb0d0a8eSMike Smith 1374b7ec270SMarius Strobl DEVMETHOD_END 138bb0d0a8eSMike Smith }; 139bb0d0a8eSMike Smith 14004dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc)); 14197a41013SJohn Baldwin EARLY_DRIVER_MODULE(pcib, pci, pcib_driver, NULL, NULL, BUS_PASS_BUS); 142bb0d0a8eSMike Smith 1430070c94bSJohn Baldwin SYSCTL_DECL(_hw_pci); 1440070c94bSJohn Baldwin 1450070c94bSJohn Baldwin static int pci_clear_pcib; 1460070c94bSJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0, 1470070c94bSJohn Baldwin "Clear firmware-assigned resources for PCI-PCI bridge I/O windows."); 14883c41143SJohn Baldwin 14983c41143SJohn Baldwin /* 15026245980SJessica Clarke * Get the corresponding window if this resource from a child device was 15126245980SJessica Clarke * sub-allocated from one of our window resource managers. 15226245980SJessica Clarke */ 15326245980SJessica Clarke static struct pcib_window * 154582b84cdSJohn Baldwin pcib_get_resource_window(struct pcib_softc *sc, struct resource *r) 15526245980SJessica Clarke { 156582b84cdSJohn Baldwin switch (rman_get_type(r)) { 15726245980SJessica Clarke case SYS_RES_IOPORT: 15826245980SJessica Clarke if (rman_is_region_manager(r, &sc->io.rman)) 15926245980SJessica Clarke return (&sc->io); 16026245980SJessica Clarke break; 16126245980SJessica Clarke case SYS_RES_MEMORY: 16226245980SJessica Clarke /* Prefetchable resources may live in either memory rman. */ 16326245980SJessica Clarke if (rman_get_flags(r) & RF_PREFETCHABLE && 16426245980SJessica Clarke rman_is_region_manager(r, &sc->pmem.rman)) 16526245980SJessica Clarke return (&sc->pmem); 16626245980SJessica Clarke if (rman_is_region_manager(r, &sc->mem.rman)) 16726245980SJessica Clarke return (&sc->mem); 16826245980SJessica Clarke break; 16926245980SJessica Clarke } 17026245980SJessica Clarke return (NULL); 17126245980SJessica Clarke } 17226245980SJessica Clarke 17326245980SJessica Clarke /* 17483c41143SJohn Baldwin * Is a resource from a child device sub-allocated from one of our 17583c41143SJohn Baldwin * resource managers? 17683c41143SJohn Baldwin */ 17783c41143SJohn Baldwin static int 178582b84cdSJohn Baldwin pcib_is_resource_managed(struct pcib_softc *sc, struct resource *r) 17983c41143SJohn Baldwin { 18083c41143SJohn Baldwin 181582b84cdSJohn Baldwin if (rman_get_type(r) == PCI_RES_BUS) 1824edef187SJohn Baldwin return (rman_is_region_manager(r, &sc->bus.rman)); 183582b84cdSJohn Baldwin return (pcib_get_resource_window(sc, r) != NULL); 18483c41143SJohn Baldwin } 18583c41143SJohn Baldwin 18683c41143SJohn Baldwin static int 18783c41143SJohn Baldwin pcib_is_window_open(struct pcib_window *pw) 18883c41143SJohn Baldwin { 18983c41143SJohn Baldwin 19083c41143SJohn Baldwin return (pw->valid && pw->base < pw->limit); 19183c41143SJohn Baldwin } 19283c41143SJohn Baldwin 19383c41143SJohn Baldwin /* 19483c41143SJohn Baldwin * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and 19583c41143SJohn Baldwin * handle for the resource, we could pass RF_ACTIVE up to the PCI bus 19683c41143SJohn Baldwin * when allocating the resource windows and rely on the PCI bus driver 19783c41143SJohn Baldwin * to do this for us. 19883c41143SJohn Baldwin */ 19983c41143SJohn Baldwin static void 20083c41143SJohn Baldwin pcib_activate_window(struct pcib_softc *sc, int type) 20183c41143SJohn Baldwin { 20283c41143SJohn Baldwin 20383c41143SJohn Baldwin PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type); 20483c41143SJohn Baldwin } 20583c41143SJohn Baldwin 20683c41143SJohn Baldwin static void 20783c41143SJohn Baldwin pcib_write_windows(struct pcib_softc *sc, int mask) 20883c41143SJohn Baldwin { 20983c41143SJohn Baldwin device_t dev; 21083c41143SJohn Baldwin uint32_t val; 21183c41143SJohn Baldwin 21283c41143SJohn Baldwin dev = sc->dev; 21383c41143SJohn Baldwin if (sc->io.valid && mask & WIN_IO) { 21483c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 21583c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 21683c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEH_1, 21783c41143SJohn Baldwin sc->io.base >> 16, 2); 21883c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITH_1, 21983c41143SJohn Baldwin sc->io.limit >> 16, 2); 22083c41143SJohn Baldwin } 22183c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1); 22283c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1); 22383c41143SJohn Baldwin } 22483c41143SJohn Baldwin 22583c41143SJohn Baldwin if (mask & WIN_MEM) { 22683c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2); 22783c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2); 22883c41143SJohn Baldwin } 22983c41143SJohn Baldwin 23083c41143SJohn Baldwin if (sc->pmem.valid && mask & WIN_PMEM) { 23183c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 23283c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 23383c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEH_1, 23483c41143SJohn Baldwin sc->pmem.base >> 32, 4); 23583c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITH_1, 23683c41143SJohn Baldwin sc->pmem.limit >> 32, 4); 23783c41143SJohn Baldwin } 23883c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2); 23983c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2); 24083c41143SJohn Baldwin } 24183c41143SJohn Baldwin } 24283c41143SJohn Baldwin 243c825d4dcSJohn Baldwin /* 244c825d4dcSJohn Baldwin * This is used to reject I/O port allocations that conflict with an 245c825d4dcSJohn Baldwin * ISA alias range. 246c825d4dcSJohn Baldwin */ 247c825d4dcSJohn Baldwin static int 2482dd1bdf1SJustin Hibbits pcib_is_isa_range(struct pcib_softc *sc, rman_res_t start, rman_res_t end, 2492dd1bdf1SJustin Hibbits rman_res_t count) 250c825d4dcSJohn Baldwin { 2512dd1bdf1SJustin Hibbits rman_res_t next_alias; 252c825d4dcSJohn Baldwin 253c825d4dcSJohn Baldwin if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE)) 254c825d4dcSJohn Baldwin return (0); 255c825d4dcSJohn Baldwin 256c825d4dcSJohn Baldwin /* Only check fixed ranges for overlap. */ 257c825d4dcSJohn Baldwin if (start + count - 1 != end) 258c825d4dcSJohn Baldwin return (0); 259c825d4dcSJohn Baldwin 260c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */ 261c825d4dcSJohn Baldwin if (start >= 65536) 262c825d4dcSJohn Baldwin return (0); 263c825d4dcSJohn Baldwin 264c825d4dcSJohn Baldwin /* Check for overlap with 0x000 - 0x0ff as a special case. */ 265c825d4dcSJohn Baldwin if (start < 0x100) 266c825d4dcSJohn Baldwin goto alias; 267c825d4dcSJohn Baldwin 268c825d4dcSJohn Baldwin /* 269c825d4dcSJohn Baldwin * If the start address is an alias, the range is an alias. 270c825d4dcSJohn Baldwin * Otherwise, compute the start of the next alias range and 271c825d4dcSJohn Baldwin * check if it is before the end of the candidate range. 272c825d4dcSJohn Baldwin */ 273c825d4dcSJohn Baldwin if ((start & 0x300) != 0) 274c825d4dcSJohn Baldwin goto alias; 275c825d4dcSJohn Baldwin next_alias = (start & ~0x3fful) | 0x100; 276c825d4dcSJohn Baldwin if (next_alias <= end) 277c825d4dcSJohn Baldwin goto alias; 278c825d4dcSJohn Baldwin return (0); 279c825d4dcSJohn Baldwin 280c825d4dcSJohn Baldwin alias: 281c825d4dcSJohn Baldwin if (bootverbose) 282c825d4dcSJohn Baldwin device_printf(sc->dev, 283da1b038aSJustin Hibbits "I/O range %#jx-%#jx overlaps with an ISA alias\n", start, 284c825d4dcSJohn Baldwin end); 285c825d4dcSJohn Baldwin return (1); 286c825d4dcSJohn Baldwin } 287c825d4dcSJohn Baldwin 288c825d4dcSJohn Baldwin static void 289c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res, 290c825d4dcSJohn Baldwin int count) 291c825d4dcSJohn Baldwin { 292c825d4dcSJohn Baldwin struct resource **newarray; 293c825d4dcSJohn Baldwin int error, i; 294c825d4dcSJohn Baldwin 295c825d4dcSJohn Baldwin newarray = malloc(sizeof(struct resource *) * (w->count + count), 296c825d4dcSJohn Baldwin M_DEVBUF, M_WAITOK); 297c825d4dcSJohn Baldwin if (w->res != NULL) 298c825d4dcSJohn Baldwin bcopy(w->res, newarray, sizeof(struct resource *) * w->count); 299c825d4dcSJohn Baldwin bcopy(res, newarray + w->count, sizeof(struct resource *) * count); 300c825d4dcSJohn Baldwin free(w->res, M_DEVBUF); 301c825d4dcSJohn Baldwin w->res = newarray; 302c825d4dcSJohn Baldwin w->count += count; 303c825d4dcSJohn Baldwin 304c825d4dcSJohn Baldwin for (i = 0; i < count; i++) { 305c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, rman_get_start(res[i]), 306c825d4dcSJohn Baldwin rman_get_end(res[i])); 307c825d4dcSJohn Baldwin if (error) 308c825d4dcSJohn Baldwin panic("Failed to add resource to rman"); 309c825d4dcSJohn Baldwin } 310c825d4dcSJohn Baldwin } 311c825d4dcSJohn Baldwin 3122dd1bdf1SJustin Hibbits typedef void (nonisa_callback)(rman_res_t start, rman_res_t end, void *arg); 313c825d4dcSJohn Baldwin 314c825d4dcSJohn Baldwin static void 3152dd1bdf1SJustin Hibbits pcib_walk_nonisa_ranges(rman_res_t start, rman_res_t end, nonisa_callback *cb, 316c825d4dcSJohn Baldwin void *arg) 317c825d4dcSJohn Baldwin { 3182dd1bdf1SJustin Hibbits rman_res_t next_end; 319c825d4dcSJohn Baldwin 320c825d4dcSJohn Baldwin /* 321c825d4dcSJohn Baldwin * If start is within an ISA alias range, move up to the start 322c825d4dcSJohn Baldwin * of the next non-alias range. As a special case, addresses 323c825d4dcSJohn Baldwin * in the range 0x000 - 0x0ff should also be skipped since 324c825d4dcSJohn Baldwin * those are used for various system I/O devices in ISA 325c825d4dcSJohn Baldwin * systems. 326c825d4dcSJohn Baldwin */ 327c825d4dcSJohn Baldwin if (start <= 65535) { 328c825d4dcSJohn Baldwin if (start < 0x100 || (start & 0x300) != 0) { 329c825d4dcSJohn Baldwin start &= ~0x3ff; 330c825d4dcSJohn Baldwin start += 0x400; 331c825d4dcSJohn Baldwin } 332c825d4dcSJohn Baldwin } 333c825d4dcSJohn Baldwin 334c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */ 335c825d4dcSJohn Baldwin while (start <= MIN(end, 65535)) { 336c825d4dcSJohn Baldwin next_end = MIN(start | 0xff, end); 337c825d4dcSJohn Baldwin cb(start, next_end, arg); 338c825d4dcSJohn Baldwin start += 0x400; 339c825d4dcSJohn Baldwin } 340c825d4dcSJohn Baldwin 341c825d4dcSJohn Baldwin if (start <= end) 342c825d4dcSJohn Baldwin cb(start, end, arg); 343c825d4dcSJohn Baldwin } 344c825d4dcSJohn Baldwin 345c825d4dcSJohn Baldwin static void 3462dd1bdf1SJustin Hibbits count_ranges(rman_res_t start, rman_res_t end, void *arg) 347c825d4dcSJohn Baldwin { 348c825d4dcSJohn Baldwin int *countp; 349c825d4dcSJohn Baldwin 350c825d4dcSJohn Baldwin countp = arg; 351c825d4dcSJohn Baldwin (*countp)++; 352c825d4dcSJohn Baldwin } 353c825d4dcSJohn Baldwin 354c825d4dcSJohn Baldwin struct alloc_state { 355c825d4dcSJohn Baldwin struct resource **res; 356c825d4dcSJohn Baldwin struct pcib_softc *sc; 357c825d4dcSJohn Baldwin int count, error; 358c825d4dcSJohn Baldwin }; 359c825d4dcSJohn Baldwin 360c825d4dcSJohn Baldwin static void 3612dd1bdf1SJustin Hibbits alloc_ranges(rman_res_t start, rman_res_t end, void *arg) 362c825d4dcSJohn Baldwin { 363c825d4dcSJohn Baldwin struct alloc_state *as; 364c825d4dcSJohn Baldwin struct pcib_window *w; 365c825d4dcSJohn Baldwin int rid; 366c825d4dcSJohn Baldwin 367c825d4dcSJohn Baldwin as = arg; 368c825d4dcSJohn Baldwin if (as->error != 0) 369c825d4dcSJohn Baldwin return; 370c825d4dcSJohn Baldwin 371c825d4dcSJohn Baldwin w = &as->sc->io; 372c825d4dcSJohn Baldwin rid = w->reg; 373c825d4dcSJohn Baldwin if (bootverbose) 374c825d4dcSJohn Baldwin device_printf(as->sc->dev, 375da1b038aSJustin Hibbits "allocating non-ISA range %#jx-%#jx\n", start, end); 376c825d4dcSJohn Baldwin as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT, 377b377ff81SJohn Baldwin &rid, start, end, end - start + 1, RF_ACTIVE | RF_UNMAPPED); 378c825d4dcSJohn Baldwin if (as->res[as->count] == NULL) 379c825d4dcSJohn Baldwin as->error = ENXIO; 380c825d4dcSJohn Baldwin else 381c825d4dcSJohn Baldwin as->count++; 382c825d4dcSJohn Baldwin } 383c825d4dcSJohn Baldwin 384c825d4dcSJohn Baldwin static int 3852dd1bdf1SJustin Hibbits pcib_alloc_nonisa_ranges(struct pcib_softc *sc, rman_res_t start, rman_res_t end) 386c825d4dcSJohn Baldwin { 387c825d4dcSJohn Baldwin struct alloc_state as; 388c825d4dcSJohn Baldwin int i, new_count; 389c825d4dcSJohn Baldwin 390c825d4dcSJohn Baldwin /* First, see how many ranges we need. */ 391c825d4dcSJohn Baldwin new_count = 0; 392c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count); 393c825d4dcSJohn Baldwin 394c825d4dcSJohn Baldwin /* Second, allocate the ranges. */ 395c825d4dcSJohn Baldwin as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF, 396c825d4dcSJohn Baldwin M_WAITOK); 397c825d4dcSJohn Baldwin as.sc = sc; 398c825d4dcSJohn Baldwin as.count = 0; 399c825d4dcSJohn Baldwin as.error = 0; 400c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as); 401c825d4dcSJohn Baldwin if (as.error != 0) { 402c825d4dcSJohn Baldwin for (i = 0; i < as.count; i++) 403c825d4dcSJohn Baldwin bus_release_resource(sc->dev, SYS_RES_IOPORT, 404c825d4dcSJohn Baldwin sc->io.reg, as.res[i]); 405c825d4dcSJohn Baldwin free(as.res, M_DEVBUF); 406c825d4dcSJohn Baldwin return (as.error); 407c825d4dcSJohn Baldwin } 408c825d4dcSJohn Baldwin KASSERT(as.count == new_count, ("%s: count mismatch", __func__)); 409c825d4dcSJohn Baldwin 410c825d4dcSJohn Baldwin /* Third, add the ranges to the window. */ 411c825d4dcSJohn Baldwin pcib_add_window_resources(&sc->io, as.res, as.count); 412c825d4dcSJohn Baldwin free(as.res, M_DEVBUF); 413c825d4dcSJohn Baldwin return (0); 414c825d4dcSJohn Baldwin } 415c825d4dcSJohn Baldwin 41683c41143SJohn Baldwin static void 41783c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type, 41883c41143SJohn Baldwin int flags, pci_addr_t max_address) 41983c41143SJohn Baldwin { 420c825d4dcSJohn Baldwin struct resource *res; 42183c41143SJohn Baldwin char buf[64]; 42283c41143SJohn Baldwin int error, rid; 42383c41143SJohn Baldwin 42489977ce2SJustin Hibbits if (max_address != (rman_res_t)max_address) 425534ccd7bSJustin Hibbits max_address = ~0; 42683c41143SJohn Baldwin w->rman.rm_start = 0; 42783c41143SJohn Baldwin w->rman.rm_end = max_address; 42883c41143SJohn Baldwin w->rman.rm_type = RMAN_ARRAY; 42983c41143SJohn Baldwin snprintf(buf, sizeof(buf), "%s %s window", 43083c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name); 43183c41143SJohn Baldwin w->rman.rm_descr = strdup(buf, M_DEVBUF); 43283c41143SJohn Baldwin error = rman_init(&w->rman); 43383c41143SJohn Baldwin if (error) 43483c41143SJohn Baldwin panic("Failed to initialize %s %s rman", 43583c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name); 43683c41143SJohn Baldwin 43783c41143SJohn Baldwin if (!pcib_is_window_open(w)) 43883c41143SJohn Baldwin return; 43983c41143SJohn Baldwin 44083c41143SJohn Baldwin if (w->base > max_address || w->limit > max_address) { 44183c41143SJohn Baldwin device_printf(sc->dev, 44283c41143SJohn Baldwin "initial %s window has too many bits, ignoring\n", w->name); 44383c41143SJohn Baldwin return; 44483c41143SJohn Baldwin } 445c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE) 446c825d4dcSJohn Baldwin (void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit); 447c825d4dcSJohn Baldwin else { 44883c41143SJohn Baldwin rid = w->reg; 449c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit, 450b377ff81SJohn Baldwin w->limit - w->base + 1, flags | RF_ACTIVE | RF_UNMAPPED); 451c825d4dcSJohn Baldwin if (res != NULL) 452c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 453c825d4dcSJohn Baldwin } 45483c41143SJohn Baldwin if (w->res == NULL) { 45583c41143SJohn Baldwin device_printf(sc->dev, 45683c41143SJohn Baldwin "failed to allocate initial %s window: %#jx-%#jx\n", 45783c41143SJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 45883c41143SJohn Baldwin w->base = max_address; 45983c41143SJohn Baldwin w->limit = 0; 46083c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 46183c41143SJohn Baldwin return; 46283c41143SJohn Baldwin } 46383c41143SJohn Baldwin pcib_activate_window(sc, type); 46483c41143SJohn Baldwin } 46583c41143SJohn Baldwin 46683c41143SJohn Baldwin /* 46783c41143SJohn Baldwin * Initialize I/O windows. 46883c41143SJohn Baldwin */ 46983c41143SJohn Baldwin static void 47083c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc) 47183c41143SJohn Baldwin { 47283c41143SJohn Baldwin pci_addr_t max; 47383c41143SJohn Baldwin device_t dev; 47483c41143SJohn Baldwin uint32_t val; 47583c41143SJohn Baldwin 47683c41143SJohn Baldwin dev = sc->dev; 47783c41143SJohn Baldwin 4780070c94bSJohn Baldwin if (pci_clear_pcib) { 479809923caSJustin Hibbits pcib_bridge_init(dev); 4800070c94bSJohn Baldwin } 4810070c94bSJohn Baldwin 48283c41143SJohn Baldwin /* Determine if the I/O port window is implemented. */ 48383c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 48483c41143SJohn Baldwin if (val == 0) { 48583c41143SJohn Baldwin /* 48683c41143SJohn Baldwin * If 'val' is zero, then only 16-bits of I/O space 48783c41143SJohn Baldwin * are supported. 48883c41143SJohn Baldwin */ 48983c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 49083c41143SJohn Baldwin if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) { 49183c41143SJohn Baldwin sc->io.valid = 1; 49283c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0, 1); 49383c41143SJohn Baldwin } 49483c41143SJohn Baldwin } else 49583c41143SJohn Baldwin sc->io.valid = 1; 49683c41143SJohn Baldwin 49783c41143SJohn Baldwin /* Read the existing I/O port window. */ 49883c41143SJohn Baldwin if (sc->io.valid) { 49983c41143SJohn Baldwin sc->io.reg = PCIR_IOBASEL_1; 50083c41143SJohn Baldwin sc->io.step = 12; 50183c41143SJohn Baldwin sc->io.mask = WIN_IO; 50283c41143SJohn Baldwin sc->io.name = "I/O port"; 50383c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 50483c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE( 50583c41143SJohn Baldwin pci_read_config(dev, PCIR_IOBASEH_1, 2), val); 50683c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT( 50783c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITH_1, 2), 50883c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 50983c41143SJohn Baldwin max = 0xffffffff; 51083c41143SJohn Baldwin } else { 51183c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE(0, val); 51283c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT(0, 51383c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 51483c41143SJohn Baldwin max = 0xffff; 51583c41143SJohn Baldwin } 51683c41143SJohn Baldwin pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max); 51783c41143SJohn Baldwin } 51883c41143SJohn Baldwin 51983c41143SJohn Baldwin /* Read the existing memory window. */ 52083c41143SJohn Baldwin sc->mem.valid = 1; 52183c41143SJohn Baldwin sc->mem.reg = PCIR_MEMBASE_1; 52283c41143SJohn Baldwin sc->mem.step = 20; 52383c41143SJohn Baldwin sc->mem.mask = WIN_MEM; 52483c41143SJohn Baldwin sc->mem.name = "memory"; 52583c41143SJohn Baldwin sc->mem.base = PCI_PPBMEMBASE(0, 52683c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMBASE_1, 2)); 52783c41143SJohn Baldwin sc->mem.limit = PCI_PPBMEMLIMIT(0, 52883c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 52983c41143SJohn Baldwin pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff); 53083c41143SJohn Baldwin 53183c41143SJohn Baldwin /* Determine if the prefetchable memory window is implemented. */ 53283c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 53383c41143SJohn Baldwin if (val == 0) { 53483c41143SJohn Baldwin /* 53583c41143SJohn Baldwin * If 'val' is zero, then only 32-bits of memory space 53683c41143SJohn Baldwin * are supported. 53783c41143SJohn Baldwin */ 53883c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 53983c41143SJohn Baldwin if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) { 54083c41143SJohn Baldwin sc->pmem.valid = 1; 54183c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0, 2); 54283c41143SJohn Baldwin } 54383c41143SJohn Baldwin } else 54483c41143SJohn Baldwin sc->pmem.valid = 1; 54583c41143SJohn Baldwin 54683c41143SJohn Baldwin /* Read the existing prefetchable memory window. */ 54783c41143SJohn Baldwin if (sc->pmem.valid) { 54883c41143SJohn Baldwin sc->pmem.reg = PCIR_PMBASEL_1; 54983c41143SJohn Baldwin sc->pmem.step = 20; 55083c41143SJohn Baldwin sc->pmem.mask = WIN_PMEM; 55183c41143SJohn Baldwin sc->pmem.name = "prefetch"; 55283c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 55383c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE( 55483c41143SJohn Baldwin pci_read_config(dev, PCIR_PMBASEH_1, 4), val); 55583c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT( 55683c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITH_1, 4), 55783c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 55883c41143SJohn Baldwin max = 0xffffffffffffffff; 55983c41143SJohn Baldwin } else { 56083c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE(0, val); 56183c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT(0, 56283c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 56383c41143SJohn Baldwin max = 0xffffffff; 56483c41143SJohn Baldwin } 56583c41143SJohn Baldwin pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY, 56683c41143SJohn Baldwin RF_PREFETCHABLE, max); 56783c41143SJohn Baldwin } 56883c41143SJohn Baldwin } 56983c41143SJohn Baldwin 5706f33eaa5SJohn Baldwin static void 5716f33eaa5SJohn Baldwin pcib_release_window(struct pcib_softc *sc, struct pcib_window *w, int type) 5726f33eaa5SJohn Baldwin { 5736f33eaa5SJohn Baldwin device_t dev; 5746f33eaa5SJohn Baldwin int error, i; 5756f33eaa5SJohn Baldwin 5766f33eaa5SJohn Baldwin if (!w->valid) 5776f33eaa5SJohn Baldwin return; 5786f33eaa5SJohn Baldwin 5796f33eaa5SJohn Baldwin dev = sc->dev; 5806f33eaa5SJohn Baldwin error = rman_fini(&w->rman); 5816f33eaa5SJohn Baldwin if (error) { 5826f33eaa5SJohn Baldwin device_printf(dev, "failed to release %s rman\n", w->name); 5836f33eaa5SJohn Baldwin return; 5846f33eaa5SJohn Baldwin } 5856f33eaa5SJohn Baldwin free(__DECONST(char *, w->rman.rm_descr), M_DEVBUF); 5866f33eaa5SJohn Baldwin 5876f33eaa5SJohn Baldwin for (i = 0; i < w->count; i++) { 5886f33eaa5SJohn Baldwin error = bus_free_resource(dev, type, w->res[i]); 5896f33eaa5SJohn Baldwin if (error) 5906f33eaa5SJohn Baldwin device_printf(dev, 5916f33eaa5SJohn Baldwin "failed to release %s resource: %d\n", w->name, 5926f33eaa5SJohn Baldwin error); 5936f33eaa5SJohn Baldwin } 5946f33eaa5SJohn Baldwin free(w->res, M_DEVBUF); 5956f33eaa5SJohn Baldwin } 5966f33eaa5SJohn Baldwin 5976f33eaa5SJohn Baldwin static void 5986f33eaa5SJohn Baldwin pcib_free_windows(struct pcib_softc *sc) 5996f33eaa5SJohn Baldwin { 6006f33eaa5SJohn Baldwin 6016f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->pmem, SYS_RES_MEMORY); 6026f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->mem, SYS_RES_MEMORY); 6036f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->io, SYS_RES_IOPORT); 6046f33eaa5SJohn Baldwin } 6056f33eaa5SJohn Baldwin 6064edef187SJohn Baldwin /* 6074edef187SJohn Baldwin * Allocate a suitable secondary bus for this bridge if needed and 6084edef187SJohn Baldwin * initialize the resource manager for the secondary bus range. Note 6094edef187SJohn Baldwin * that the minimum count is a desired value and this may allocate a 6104edef187SJohn Baldwin * smaller range. 6114edef187SJohn Baldwin */ 6124edef187SJohn Baldwin void 6134edef187SJohn Baldwin pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count) 6144edef187SJohn Baldwin { 6154edef187SJohn Baldwin char buf[64]; 616ad6f36f8SJohn Baldwin int error, rid, sec_reg; 6174edef187SJohn Baldwin 6184edef187SJohn Baldwin switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) { 6194edef187SJohn Baldwin case PCIM_HDRTYPE_BRIDGE: 620ad6f36f8SJohn Baldwin sec_reg = PCIR_SECBUS_1; 6214edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_1; 6224edef187SJohn Baldwin break; 6234edef187SJohn Baldwin case PCIM_HDRTYPE_CARDBUS: 624ad6f36f8SJohn Baldwin sec_reg = PCIR_SECBUS_2; 6254edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_2; 6264edef187SJohn Baldwin break; 6274edef187SJohn Baldwin default: 6284edef187SJohn Baldwin panic("not a PCI bridge"); 6294edef187SJohn Baldwin } 630ad6f36f8SJohn Baldwin bus->sec = pci_read_config(dev, sec_reg, 1); 631ad6f36f8SJohn Baldwin bus->sub = pci_read_config(dev, bus->sub_reg, 1); 6324edef187SJohn Baldwin bus->dev = dev; 6334edef187SJohn Baldwin bus->rman.rm_start = 0; 6344edef187SJohn Baldwin bus->rman.rm_end = PCI_BUSMAX; 6354edef187SJohn Baldwin bus->rman.rm_type = RMAN_ARRAY; 6364edef187SJohn Baldwin snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev)); 6374edef187SJohn Baldwin bus->rman.rm_descr = strdup(buf, M_DEVBUF); 6384edef187SJohn Baldwin error = rman_init(&bus->rman); 6394edef187SJohn Baldwin if (error) 6404edef187SJohn Baldwin panic("Failed to initialize %s bus number rman", 6414edef187SJohn Baldwin device_get_nameunit(dev)); 6424edef187SJohn Baldwin 6434edef187SJohn Baldwin /* 6444edef187SJohn Baldwin * Allocate a bus range. This will return an existing bus range 6454edef187SJohn Baldwin * if one exists, or a new bus range if one does not. 6464edef187SJohn Baldwin */ 6474edef187SJohn Baldwin rid = 0; 648c47476d7SJustin Hibbits bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid, 64936efc64aSJohn Baldwin min_count, RF_ACTIVE); 6504edef187SJohn Baldwin if (bus->res == NULL) { 6514edef187SJohn Baldwin /* 6524edef187SJohn Baldwin * Fall back to just allocating a range of a single bus 6534edef187SJohn Baldwin * number. 6544edef187SJohn Baldwin */ 655c47476d7SJustin Hibbits bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid, 65636efc64aSJohn Baldwin 1, RF_ACTIVE); 6574edef187SJohn Baldwin } else if (rman_get_size(bus->res) < min_count) 6584edef187SJohn Baldwin /* 6594edef187SJohn Baldwin * Attempt to grow the existing range to satisfy the 6604edef187SJohn Baldwin * minimum desired count. 6614edef187SJohn Baldwin */ 6624edef187SJohn Baldwin (void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res, 6634edef187SJohn Baldwin rman_get_start(bus->res), rman_get_start(bus->res) + 6644edef187SJohn Baldwin min_count - 1); 6654edef187SJohn Baldwin 6664edef187SJohn Baldwin /* 6674edef187SJohn Baldwin * Add the initial resource to the rman. 6684edef187SJohn Baldwin */ 6694edef187SJohn Baldwin if (bus->res != NULL) { 6704edef187SJohn Baldwin error = rman_manage_region(&bus->rman, rman_get_start(bus->res), 6714edef187SJohn Baldwin rman_get_end(bus->res)); 6724edef187SJohn Baldwin if (error) 6734edef187SJohn Baldwin panic("Failed to add resource to rman"); 6744edef187SJohn Baldwin bus->sec = rman_get_start(bus->res); 6754edef187SJohn Baldwin bus->sub = rman_get_end(bus->res); 6764edef187SJohn Baldwin } 6774edef187SJohn Baldwin } 6784edef187SJohn Baldwin 6796f33eaa5SJohn Baldwin void 6806f33eaa5SJohn Baldwin pcib_free_secbus(device_t dev, struct pcib_secbus *bus) 6816f33eaa5SJohn Baldwin { 6826f33eaa5SJohn Baldwin int error; 6836f33eaa5SJohn Baldwin 6846f33eaa5SJohn Baldwin error = rman_fini(&bus->rman); 6856f33eaa5SJohn Baldwin if (error) { 6866f33eaa5SJohn Baldwin device_printf(dev, "failed to release bus number rman\n"); 6876f33eaa5SJohn Baldwin return; 6886f33eaa5SJohn Baldwin } 6896f33eaa5SJohn Baldwin free(__DECONST(char *, bus->rman.rm_descr), M_DEVBUF); 6906f33eaa5SJohn Baldwin 6916f33eaa5SJohn Baldwin error = bus_free_resource(dev, PCI_RES_BUS, bus->res); 6926f33eaa5SJohn Baldwin if (error) 6936f33eaa5SJohn Baldwin device_printf(dev, 6946f33eaa5SJohn Baldwin "failed to release bus numbers resource: %d\n", error); 6956f33eaa5SJohn Baldwin } 6966f33eaa5SJohn Baldwin 6974edef187SJohn Baldwin static struct resource * 6984edef187SJohn Baldwin pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid, 6992dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 7004edef187SJohn Baldwin { 7014edef187SJohn Baldwin struct resource *res; 7024edef187SJohn Baldwin 7034edef187SJohn Baldwin res = rman_reserve_resource(&bus->rman, start, end, count, flags, 7044edef187SJohn Baldwin child); 7054edef187SJohn Baldwin if (res == NULL) 7064edef187SJohn Baldwin return (NULL); 7074edef187SJohn Baldwin 7084edef187SJohn Baldwin if (bootverbose) 7094edef187SJohn Baldwin device_printf(bus->dev, 710da1b038aSJustin Hibbits "allocated bus range (%ju-%ju) for rid %d of %s\n", 7114edef187SJohn Baldwin rman_get_start(res), rman_get_end(res), *rid, 7124edef187SJohn Baldwin pcib_child_name(child)); 7134edef187SJohn Baldwin rman_set_rid(res, *rid); 7141b9bcfffSJohn Baldwin rman_set_type(res, PCI_RES_BUS); 7154edef187SJohn Baldwin return (res); 7164edef187SJohn Baldwin } 7174edef187SJohn Baldwin 7184edef187SJohn Baldwin /* 7194edef187SJohn Baldwin * Attempt to grow the secondary bus range. This is much simpler than 7204edef187SJohn Baldwin * for I/O windows as the range can only be grown by increasing 7214edef187SJohn Baldwin * subbus. 7224edef187SJohn Baldwin */ 7234edef187SJohn Baldwin static int 7242dd1bdf1SJustin Hibbits pcib_grow_subbus(struct pcib_secbus *bus, rman_res_t new_end) 7254edef187SJohn Baldwin { 7262dd1bdf1SJustin Hibbits rman_res_t old_end; 7274edef187SJohn Baldwin int error; 7284edef187SJohn Baldwin 7294edef187SJohn Baldwin old_end = rman_get_end(bus->res); 7304edef187SJohn Baldwin KASSERT(new_end > old_end, ("attempt to shrink subbus")); 7314edef187SJohn Baldwin error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res, 7324edef187SJohn Baldwin rman_get_start(bus->res), new_end); 7334edef187SJohn Baldwin if (error) 7344edef187SJohn Baldwin return (error); 7354edef187SJohn Baldwin if (bootverbose) 736da1b038aSJustin Hibbits device_printf(bus->dev, "grew bus range to %ju-%ju\n", 7374edef187SJohn Baldwin rman_get_start(bus->res), rman_get_end(bus->res)); 7384edef187SJohn Baldwin error = rman_manage_region(&bus->rman, old_end + 1, 7394edef187SJohn Baldwin rman_get_end(bus->res)); 7404edef187SJohn Baldwin if (error) 7414edef187SJohn Baldwin panic("Failed to add resource to rman"); 7424edef187SJohn Baldwin bus->sub = rman_get_end(bus->res); 7434edef187SJohn Baldwin pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1); 7444edef187SJohn Baldwin return (0); 7454edef187SJohn Baldwin } 7464edef187SJohn Baldwin 7474edef187SJohn Baldwin struct resource * 7484edef187SJohn Baldwin pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid, 7492dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 7504edef187SJohn Baldwin { 7514edef187SJohn Baldwin struct resource *res; 7522dd1bdf1SJustin Hibbits rman_res_t start_free, end_free, new_end; 7534edef187SJohn Baldwin 7544edef187SJohn Baldwin /* 7554edef187SJohn Baldwin * First, see if the request can be satisified by the existing 7564edef187SJohn Baldwin * bus range. 7574edef187SJohn Baldwin */ 7584edef187SJohn Baldwin res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags); 7594edef187SJohn Baldwin if (res != NULL) 7604edef187SJohn Baldwin return (res); 7614edef187SJohn Baldwin 7624edef187SJohn Baldwin /* 7634edef187SJohn Baldwin * Figure out a range to grow the bus range. First, find the 7644edef187SJohn Baldwin * first bus number after the last allocated bus in the rman and 7654edef187SJohn Baldwin * enforce that as a minimum starting point for the range. 7664edef187SJohn Baldwin */ 7674edef187SJohn Baldwin if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 || 7684edef187SJohn Baldwin end_free != bus->sub) 7694edef187SJohn Baldwin start_free = bus->sub + 1; 7704edef187SJohn Baldwin if (start_free < start) 7714edef187SJohn Baldwin start_free = start; 7724edef187SJohn Baldwin new_end = start_free + count - 1; 7734edef187SJohn Baldwin 7744edef187SJohn Baldwin /* 7754edef187SJohn Baldwin * See if this new range would satisfy the request if it 7764edef187SJohn Baldwin * succeeds. 7774edef187SJohn Baldwin */ 7784edef187SJohn Baldwin if (new_end > end) 7794edef187SJohn Baldwin return (NULL); 7804edef187SJohn Baldwin 7814edef187SJohn Baldwin /* Finally, attempt to grow the existing resource. */ 7824edef187SJohn Baldwin if (bootverbose) { 7834edef187SJohn Baldwin device_printf(bus->dev, 784da1b038aSJustin Hibbits "attempting to grow bus range for %ju buses\n", count); 785da1b038aSJustin Hibbits printf("\tback candidate range: %ju-%ju\n", start_free, 7864edef187SJohn Baldwin new_end); 7874edef187SJohn Baldwin } 7884edef187SJohn Baldwin if (pcib_grow_subbus(bus, new_end) == 0) 7894edef187SJohn Baldwin return (pcib_suballoc_bus(bus, child, rid, start, end, count, 7904edef187SJohn Baldwin flags)); 7914edef187SJohn Baldwin return (NULL); 7924edef187SJohn Baldwin } 7934edef187SJohn Baldwin 79482cb5c3bSJohn Baldwin #ifdef PCI_HP 79582cb5c3bSJohn Baldwin /* 79682cb5c3bSJohn Baldwin * PCI-express HotPlug support. 79782cb5c3bSJohn Baldwin */ 79825a57bd6SJohn Baldwin static int pci_enable_pcie_hp = 1; 79925a57bd6SJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_hp, CTLFLAG_RDTUN, 80025a57bd6SJohn Baldwin &pci_enable_pcie_hp, 0, 80125a57bd6SJohn Baldwin "Enable support for native PCI-express HotPlug."); 80225a57bd6SJohn Baldwin 80382cb5c3bSJohn Baldwin static void 80482cb5c3bSJohn Baldwin pcib_probe_hotplug(struct pcib_softc *sc) 80582cb5c3bSJohn Baldwin { 80682cb5c3bSJohn Baldwin device_t dev; 80737290148SEric van Gyzen uint32_t link_cap; 808991d431fSEric van Gyzen uint16_t link_sta, slot_sta; 80982cb5c3bSJohn Baldwin 81025a57bd6SJohn Baldwin if (!pci_enable_pcie_hp) 81125a57bd6SJohn Baldwin return; 81225a57bd6SJohn Baldwin 81382cb5c3bSJohn Baldwin dev = sc->dev; 81482cb5c3bSJohn Baldwin if (pci_find_cap(dev, PCIY_EXPRESS, NULL) != 0) 81582cb5c3bSJohn Baldwin return; 81682cb5c3bSJohn Baldwin 81782cb5c3bSJohn Baldwin if (!(pcie_read_config(dev, PCIER_FLAGS, 2) & PCIEM_FLAGS_SLOT)) 81882cb5c3bSJohn Baldwin return; 81982cb5c3bSJohn Baldwin 82082cb5c3bSJohn Baldwin sc->pcie_slot_cap = pcie_read_config(dev, PCIER_SLOT_CAP, 4); 82182cb5c3bSJohn Baldwin 822991d431fSEric van Gyzen if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_HPC) == 0) 8232611037cSJohn Baldwin return; 82437290148SEric van Gyzen link_cap = pcie_read_config(dev, PCIER_LINK_CAP, 4); 82537290148SEric van Gyzen if ((link_cap & PCIEM_LINK_CAP_DL_ACTIVE) == 0) 8262ffb582aSJohn Baldwin return; 8272611037cSJohn Baldwin 828991d431fSEric van Gyzen /* 829991d431fSEric van Gyzen * Some devices report that they have an MRL when they actually 830991d431fSEric van Gyzen * do not. Since they always report that the MRL is open, child 831991d431fSEric van Gyzen * devices would be ignored. Try to detect these devices and 832991d431fSEric van Gyzen * ignore their claim of HotPlug support. 833991d431fSEric van Gyzen * 834991d431fSEric van Gyzen * If there is an open MRL but the Data Link Layer is active, 835991d431fSEric van Gyzen * the MRL is not real. 836991d431fSEric van Gyzen */ 83737290148SEric van Gyzen if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) != 0) { 838991d431fSEric van Gyzen link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 839991d431fSEric van Gyzen slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 840991d431fSEric van Gyzen if ((slot_sta & PCIEM_SLOT_STA_MRLSS) != 0 && 841991d431fSEric van Gyzen (link_sta & PCIEM_LINK_STA_DL_ACTIVE) != 0) { 842991d431fSEric van Gyzen return; 843991d431fSEric van Gyzen } 844991d431fSEric van Gyzen } 845991d431fSEric van Gyzen 84628586889SWarner Losh /* 84728586889SWarner Losh * Now that we're sure we want to do hot plug, ask the 84828586889SWarner Losh * firmware, if any, if that's OK. 84928586889SWarner Losh */ 8501ffd07bdSJohn Baldwin if (pcib_request_feature(dev, PCI_FEATURE_HP) != 0) { 85128586889SWarner Losh if (bootverbose) 85228586889SWarner Losh device_printf(dev, "Unable to activate hot plug feature.\n"); 85328586889SWarner Losh return; 85428586889SWarner Losh } 85528586889SWarner Losh 85682cb5c3bSJohn Baldwin sc->flags |= PCIB_HOTPLUG; 85782cb5c3bSJohn Baldwin } 85882cb5c3bSJohn Baldwin 85982cb5c3bSJohn Baldwin /* 86082cb5c3bSJohn Baldwin * Send a HotPlug command to the slot control register. If this slot 86107454911SJohn Baldwin * uses command completion interrupts and a previous command is still 86207454911SJohn Baldwin * in progress, then the command is dropped. Once the previous 86307454911SJohn Baldwin * command completes or times out, pcib_pcie_hotplug_update() will be 86407454911SJohn Baldwin * invoked to post a new command based on the slot's state at that 86507454911SJohn Baldwin * time. 86682cb5c3bSJohn Baldwin */ 86782cb5c3bSJohn Baldwin static void 86882cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(struct pcib_softc *sc, uint16_t val, uint16_t mask) 86982cb5c3bSJohn Baldwin { 87082cb5c3bSJohn Baldwin device_t dev; 87182cb5c3bSJohn Baldwin uint16_t ctl, new; 87282cb5c3bSJohn Baldwin 87382cb5c3bSJohn Baldwin dev = sc->dev; 87482cb5c3bSJohn Baldwin 87507454911SJohn Baldwin if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) 87607454911SJohn Baldwin return; 87707454911SJohn Baldwin 87882cb5c3bSJohn Baldwin ctl = pcie_read_config(dev, PCIER_SLOT_CTL, 2); 87982cb5c3bSJohn Baldwin new = (ctl & ~mask) | val; 88007454911SJohn Baldwin if (new == ctl) 88107454911SJohn Baldwin return; 882991d431fSEric van Gyzen if (bootverbose) 883991d431fSEric van Gyzen device_printf(dev, "HotPlug command: %04x -> %04x\n", ctl, new); 88407454911SJohn Baldwin pcie_write_config(dev, PCIER_SLOT_CTL, new, 2); 8856f33eaa5SJohn Baldwin if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS) && 8866f33eaa5SJohn Baldwin (ctl & new) & PCIEM_SLOT_CTL_CCIE) { 88782cb5c3bSJohn Baldwin sc->flags |= PCIB_HOTPLUG_CMD_PENDING; 88882cb5c3bSJohn Baldwin if (!cold) 889*b23314ecSJohn Baldwin taskqueue_enqueue_timeout(taskqueue_bus, 890fa3b03d3SAlexander Motin &sc->pcie_cc_task, hz); 89182cb5c3bSJohn Baldwin } 89282cb5c3bSJohn Baldwin } 89382cb5c3bSJohn Baldwin 89482cb5c3bSJohn Baldwin static void 89582cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(struct pcib_softc *sc) 89682cb5c3bSJohn Baldwin { 89782cb5c3bSJohn Baldwin device_t dev; 89882cb5c3bSJohn Baldwin 89982cb5c3bSJohn Baldwin dev = sc->dev; 90082cb5c3bSJohn Baldwin 90182cb5c3bSJohn Baldwin if (bootverbose) 90282cb5c3bSJohn Baldwin device_printf(dev, "Command Completed\n"); 90382cb5c3bSJohn Baldwin if (!(sc->flags & PCIB_HOTPLUG_CMD_PENDING)) 90482cb5c3bSJohn Baldwin return; 905*b23314ecSJohn Baldwin taskqueue_cancel_timeout(taskqueue_bus, &sc->pcie_cc_task, NULL); 90682cb5c3bSJohn Baldwin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 9076f33eaa5SJohn Baldwin wakeup(sc); 90882cb5c3bSJohn Baldwin } 90982cb5c3bSJohn Baldwin 91082cb5c3bSJohn Baldwin /* 91182cb5c3bSJohn Baldwin * Returns true if a card is fully inserted from the user's 91282cb5c3bSJohn Baldwin * perspective. It may not yet be ready for access, but the driver 91382cb5c3bSJohn Baldwin * can now start enabling access if necessary. 91482cb5c3bSJohn Baldwin */ 91582cb5c3bSJohn Baldwin static bool 91682cb5c3bSJohn Baldwin pcib_hotplug_inserted(struct pcib_softc *sc) 91782cb5c3bSJohn Baldwin { 91882cb5c3bSJohn Baldwin 91982cb5c3bSJohn Baldwin /* Pretend the card isn't present if a detach is forced. */ 92082cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACHING) 92182cb5c3bSJohn Baldwin return (false); 92282cb5c3bSJohn Baldwin 92382cb5c3bSJohn Baldwin /* Card must be present in the slot. */ 92482cb5c3bSJohn Baldwin if ((sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS) == 0) 92582cb5c3bSJohn Baldwin return (false); 92682cb5c3bSJohn Baldwin 92782cb5c3bSJohn Baldwin /* A power fault implicitly turns off power to the slot. */ 92882cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD) 92982cb5c3bSJohn Baldwin return (false); 93082cb5c3bSJohn Baldwin 93182cb5c3bSJohn Baldwin /* If the MRL is disengaged, the slot is powered off. */ 93282cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP && 93382cb5c3bSJohn Baldwin (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS) != 0) 93482cb5c3bSJohn Baldwin return (false); 93582cb5c3bSJohn Baldwin 93682cb5c3bSJohn Baldwin return (true); 93782cb5c3bSJohn Baldwin } 93882cb5c3bSJohn Baldwin 93982cb5c3bSJohn Baldwin /* 94082cb5c3bSJohn Baldwin * Returns -1 if the card is fully inserted, powered, and ready for 94182cb5c3bSJohn Baldwin * access. Otherwise, returns 0. 94282cb5c3bSJohn Baldwin */ 94382cb5c3bSJohn Baldwin static int 94482cb5c3bSJohn Baldwin pcib_hotplug_present(struct pcib_softc *sc) 94582cb5c3bSJohn Baldwin { 94682cb5c3bSJohn Baldwin 94782cb5c3bSJohn Baldwin /* Card must be inserted. */ 94882cb5c3bSJohn Baldwin if (!pcib_hotplug_inserted(sc)) 94982cb5c3bSJohn Baldwin return (0); 95082cb5c3bSJohn Baldwin 95182cb5c3bSJohn Baldwin /* Require the Data Link Layer to be active. */ 95282cb5c3bSJohn Baldwin if (!(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE)) 95382cb5c3bSJohn Baldwin return (0); 95482cb5c3bSJohn Baldwin 95582cb5c3bSJohn Baldwin return (-1); 95682cb5c3bSJohn Baldwin } 95782cb5c3bSJohn Baldwin 958a58536b9SAlexander Motin static int pci_enable_pcie_ei = 0; 959a58536b9SAlexander Motin SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_ei, CTLFLAG_RWTUN, 960a58536b9SAlexander Motin &pci_enable_pcie_ei, 0, 961a58536b9SAlexander Motin "Enable support for PCI-express Electromechanical Interlock."); 962a58536b9SAlexander Motin 96382cb5c3bSJohn Baldwin static void 96482cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(struct pcib_softc *sc, uint16_t val, uint16_t mask, 96582cb5c3bSJohn Baldwin bool schedule_task) 96682cb5c3bSJohn Baldwin { 967a1566487SEric van Gyzen bool card_inserted, ei_engaged; 96882cb5c3bSJohn Baldwin 969991d431fSEric van Gyzen /* Clear DETACHING if Presence Detect has cleared. */ 97082cb5c3bSJohn Baldwin if ((sc->pcie_slot_sta & (PCIEM_SLOT_STA_PDC | PCIEM_SLOT_STA_PDS)) == 97182cb5c3bSJohn Baldwin PCIEM_SLOT_STA_PDC) 97282cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACHING; 97382cb5c3bSJohn Baldwin 97482cb5c3bSJohn Baldwin card_inserted = pcib_hotplug_inserted(sc); 97582cb5c3bSJohn Baldwin 97682cb5c3bSJohn Baldwin /* Turn the power indicator on if a card is inserted. */ 97782cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PIP) { 97882cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_PIC; 97982cb5c3bSJohn Baldwin if (card_inserted) 98082cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_ON; 98182cb5c3bSJohn Baldwin else if (sc->flags & PCIB_DETACH_PENDING) 98282cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_BLINK; 98382cb5c3bSJohn Baldwin else 98482cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_OFF; 98582cb5c3bSJohn Baldwin } 98682cb5c3bSJohn Baldwin 98782cb5c3bSJohn Baldwin /* Turn the power on via the Power Controller if a card is inserted. */ 98882cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) { 98982cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_PCC; 99082cb5c3bSJohn Baldwin if (card_inserted) 99182cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PC_ON; 99282cb5c3bSJohn Baldwin else 99382cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PC_OFF; 99482cb5c3bSJohn Baldwin } 99582cb5c3bSJohn Baldwin 99682cb5c3bSJohn Baldwin /* 99782cb5c3bSJohn Baldwin * If a card is inserted, enable the Electromechanical 99882cb5c3bSJohn Baldwin * Interlock. If a card is not inserted (or we are in the 99982cb5c3bSJohn Baldwin * process of detaching), disable the Electromechanical 100082cb5c3bSJohn Baldwin * Interlock. 100182cb5c3bSJohn Baldwin */ 1002a58536b9SAlexander Motin if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP) && 1003a58536b9SAlexander Motin pci_enable_pcie_ei) { 100482cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_EIC; 1005a1566487SEric van Gyzen ei_engaged = (sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS) != 0; 1006a1566487SEric van Gyzen if (card_inserted != ei_engaged) 100782cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_EIC; 100882cb5c3bSJohn Baldwin } 100982cb5c3bSJohn Baldwin 101082cb5c3bSJohn Baldwin /* 101182cb5c3bSJohn Baldwin * Start a timer to see if the Data Link Layer times out. 1012991d431fSEric van Gyzen * Note that we only start the timer if Presence Detect or MRL Sensor 101382cb5c3bSJohn Baldwin * changed on this interrupt. Stop any scheduled timer if 101482cb5c3bSJohn Baldwin * the Data Link Layer is active. 101582cb5c3bSJohn Baldwin */ 101682cb5c3bSJohn Baldwin if (card_inserted && 101782cb5c3bSJohn Baldwin !(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) && 1018991d431fSEric van Gyzen sc->pcie_slot_sta & 1019991d431fSEric van Gyzen (PCIEM_SLOT_STA_MRLSC | PCIEM_SLOT_STA_PDC)) { 102082cb5c3bSJohn Baldwin if (cold) 102182cb5c3bSJohn Baldwin device_printf(sc->dev, 102282cb5c3bSJohn Baldwin "Data Link Layer inactive\n"); 102382cb5c3bSJohn Baldwin else 1024*b23314ecSJohn Baldwin taskqueue_enqueue_timeout(taskqueue_bus, 1025fa3b03d3SAlexander Motin &sc->pcie_dll_task, hz); 102682cb5c3bSJohn Baldwin } else if (sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) 1027*b23314ecSJohn Baldwin taskqueue_cancel_timeout(taskqueue_bus, &sc->pcie_dll_task, 1028fa3b03d3SAlexander Motin NULL); 102982cb5c3bSJohn Baldwin 103082cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(sc, val, mask); 103182cb5c3bSJohn Baldwin 103282cb5c3bSJohn Baldwin /* 1033a1566487SEric van Gyzen * During attach the child "pci" device is added synchronously; 103482cb5c3bSJohn Baldwin * otherwise, the task is scheduled to manage the child 103582cb5c3bSJohn Baldwin * device. 103682cb5c3bSJohn Baldwin */ 103782cb5c3bSJohn Baldwin if (schedule_task && 103882cb5c3bSJohn Baldwin (pcib_hotplug_present(sc) != 0) != (sc->child != NULL)) 1039*b23314ecSJohn Baldwin taskqueue_enqueue(taskqueue_bus, &sc->pcie_hp_task); 104082cb5c3bSJohn Baldwin } 104182cb5c3bSJohn Baldwin 104282cb5c3bSJohn Baldwin static void 10438a1926c5SWarner Losh pcib_pcie_intr_hotplug(void *arg) 104482cb5c3bSJohn Baldwin { 104582cb5c3bSJohn Baldwin struct pcib_softc *sc; 104682cb5c3bSJohn Baldwin device_t dev; 1047e0235fd3SColin Percival uint16_t old_slot_sta; 104882cb5c3bSJohn Baldwin 104982cb5c3bSJohn Baldwin sc = arg; 105082cb5c3bSJohn Baldwin dev = sc->dev; 105113d700adSScott Long PCIB_HP_LOCK(sc); 1052e0235fd3SColin Percival old_slot_sta = sc->pcie_slot_sta; 105382cb5c3bSJohn Baldwin sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 105482cb5c3bSJohn Baldwin 105582cb5c3bSJohn Baldwin /* Clear the events just reported. */ 105682cb5c3bSJohn Baldwin pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2); 105782cb5c3bSJohn Baldwin 1058991d431fSEric van Gyzen if (bootverbose) 1059991d431fSEric van Gyzen device_printf(dev, "HotPlug interrupt: %#x\n", 1060991d431fSEric van Gyzen sc->pcie_slot_sta); 1061991d431fSEric van Gyzen 106282cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_ABP) { 106382cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 106482cb5c3bSJohn Baldwin device_printf(dev, 106582cb5c3bSJohn Baldwin "Attention Button Pressed: Detach Cancelled\n"); 106682cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 1067*b23314ecSJohn Baldwin taskqueue_cancel_timeout(taskqueue_bus, 1068fa3b03d3SAlexander Motin &sc->pcie_ab_task, NULL); 1069e0235fd3SColin Percival } else if (old_slot_sta & PCIEM_SLOT_STA_PDS) { 1070e0235fd3SColin Percival /* Only initiate detach sequence if device present. */ 107182cb5c3bSJohn Baldwin device_printf(dev, 107282cb5c3bSJohn Baldwin "Attention Button Pressed: Detaching in 5 seconds\n"); 107382cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACH_PENDING; 1074*b23314ecSJohn Baldwin taskqueue_enqueue_timeout(taskqueue_bus, 1075fa3b03d3SAlexander Motin &sc->pcie_ab_task, 5 * hz); 107682cb5c3bSJohn Baldwin } 107782cb5c3bSJohn Baldwin } 107882cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD) 107982cb5c3bSJohn Baldwin device_printf(dev, "Power Fault Detected\n"); 108082cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSC) 108182cb5c3bSJohn Baldwin device_printf(dev, "MRL Sensor Changed to %s\n", 108282cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS ? "open" : 108382cb5c3bSJohn Baldwin "closed"); 108482cb5c3bSJohn Baldwin if (bootverbose && sc->pcie_slot_sta & PCIEM_SLOT_STA_PDC) 1085991d431fSEric van Gyzen device_printf(dev, "Presence Detect Changed to %s\n", 108682cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS ? "card present" : 108782cb5c3bSJohn Baldwin "empty"); 108882cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_CC) 108982cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(sc); 109082cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_DLLSC) { 109182cb5c3bSJohn Baldwin sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 109282cb5c3bSJohn Baldwin if (bootverbose) 109382cb5c3bSJohn Baldwin device_printf(dev, 109482cb5c3bSJohn Baldwin "Data Link Layer State Changed to %s\n", 109582cb5c3bSJohn Baldwin sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE ? 109682cb5c3bSJohn Baldwin "active" : "inactive"); 109782cb5c3bSJohn Baldwin } 109882cb5c3bSJohn Baldwin 109982cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 110013d700adSScott Long PCIB_HP_UNLOCK(sc); 110182cb5c3bSJohn Baldwin } 110282cb5c3bSJohn Baldwin 110382cb5c3bSJohn Baldwin static void 110482cb5c3bSJohn Baldwin pcib_pcie_hotplug_task(void *context, int pending) 110582cb5c3bSJohn Baldwin { 110682cb5c3bSJohn Baldwin struct pcib_softc *sc; 110782cb5c3bSJohn Baldwin device_t dev; 110882cb5c3bSJohn Baldwin 110982cb5c3bSJohn Baldwin sc = context; 111013d700adSScott Long PCIB_HP_LOCK(sc); 111182cb5c3bSJohn Baldwin dev = sc->dev; 111282cb5c3bSJohn Baldwin if (pcib_hotplug_present(sc) != 0) { 111382cb5c3bSJohn Baldwin if (sc->child == NULL) { 11145b56413dSWarner Losh sc->child = device_add_child(dev, "pci", DEVICE_UNIT_ANY); 111518250ec6SJohn Baldwin bus_attach_children(dev); 111682cb5c3bSJohn Baldwin } 111782cb5c3bSJohn Baldwin } else { 111882cb5c3bSJohn Baldwin if (sc->child != NULL) { 111982cb5c3bSJohn Baldwin if (device_delete_child(dev, sc->child) == 0) 112082cb5c3bSJohn Baldwin sc->child = NULL; 112182cb5c3bSJohn Baldwin } 112282cb5c3bSJohn Baldwin } 112313d700adSScott Long PCIB_HP_UNLOCK(sc); 112482cb5c3bSJohn Baldwin } 112582cb5c3bSJohn Baldwin 112682cb5c3bSJohn Baldwin static void 1127fa3b03d3SAlexander Motin pcib_pcie_ab_timeout(void *arg, int pending) 112882cb5c3bSJohn Baldwin { 1129fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 113082cb5c3bSJohn Baldwin 1131fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 113282cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 113382cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING; 113482cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 113582cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 113682cb5c3bSJohn Baldwin } 1137fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 113882cb5c3bSJohn Baldwin } 113982cb5c3bSJohn Baldwin 114082cb5c3bSJohn Baldwin static void 1141fa3b03d3SAlexander Motin pcib_pcie_cc_timeout(void *arg, int pending) 114282cb5c3bSJohn Baldwin { 1143fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 1144fa3b03d3SAlexander Motin device_t dev = sc->dev; 11456f33eaa5SJohn Baldwin uint16_t sta; 114682cb5c3bSJohn Baldwin 1147fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 11486f33eaa5SJohn Baldwin sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 11496f33eaa5SJohn Baldwin if (!(sta & PCIEM_SLOT_STA_CC)) { 115021e51c82SAlexander Motin device_printf(dev, "HotPlug Command Timed Out\n"); 115121e51c82SAlexander Motin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 11526f33eaa5SJohn Baldwin } else { 11536f33eaa5SJohn Baldwin device_printf(dev, 11546f33eaa5SJohn Baldwin "Missed HotPlug interrupt waiting for Command Completion\n"); 11558a1926c5SWarner Losh pcib_pcie_intr_hotplug(sc); 115682cb5c3bSJohn Baldwin } 1157fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 115882cb5c3bSJohn Baldwin } 115982cb5c3bSJohn Baldwin 116082cb5c3bSJohn Baldwin static void 1161fa3b03d3SAlexander Motin pcib_pcie_dll_timeout(void *arg, int pending) 116282cb5c3bSJohn Baldwin { 1163fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 1164fa3b03d3SAlexander Motin device_t dev = sc->dev; 116582cb5c3bSJohn Baldwin uint16_t sta; 116682cb5c3bSJohn Baldwin 1167fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 116882cb5c3bSJohn Baldwin sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 116982cb5c3bSJohn Baldwin if (!(sta & PCIEM_LINK_STA_DL_ACTIVE)) { 117082cb5c3bSJohn Baldwin device_printf(dev, 117182cb5c3bSJohn Baldwin "Timed out waiting for Data Link Layer Active\n"); 117282cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING; 117382cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 117482cb5c3bSJohn Baldwin } else if (sta != sc->pcie_link_sta) { 117582cb5c3bSJohn Baldwin device_printf(dev, 117682cb5c3bSJohn Baldwin "Missed HotPlug interrupt waiting for DLL Active\n"); 11778a1926c5SWarner Losh pcib_pcie_intr_hotplug(sc); 117882cb5c3bSJohn Baldwin } 1179fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 118082cb5c3bSJohn Baldwin } 118182cb5c3bSJohn Baldwin 118282cb5c3bSJohn Baldwin static int 118382cb5c3bSJohn Baldwin pcib_alloc_pcie_irq(struct pcib_softc *sc) 118482cb5c3bSJohn Baldwin { 118582cb5c3bSJohn Baldwin device_t dev; 1186e6b83836SJohn Baldwin int count, error, mem_rid, rid; 118782cb5c3bSJohn Baldwin 118882cb5c3bSJohn Baldwin rid = -1; 118982cb5c3bSJohn Baldwin dev = sc->dev; 119082cb5c3bSJohn Baldwin 119182cb5c3bSJohn Baldwin /* 119282cb5c3bSJohn Baldwin * For simplicity, only use MSI-X if there is a single message. 119382cb5c3bSJohn Baldwin * To support a device with multiple messages we would have to 119482cb5c3bSJohn Baldwin * use remap intr if the MSI number is not 0. 119582cb5c3bSJohn Baldwin */ 119682cb5c3bSJohn Baldwin count = pci_msix_count(dev); 119782cb5c3bSJohn Baldwin if (count == 1) { 1198e6b83836SJohn Baldwin mem_rid = pci_msix_table_bar(dev); 1199e6b83836SJohn Baldwin sc->pcie_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 1200e6b83836SJohn Baldwin &mem_rid, RF_ACTIVE); 1201e6b83836SJohn Baldwin if (sc->pcie_mem == NULL) { 1202e6b83836SJohn Baldwin device_printf(dev, 1203e6b83836SJohn Baldwin "Failed to allocate BAR for MSI-X table\n"); 1204e6b83836SJohn Baldwin } else { 120582cb5c3bSJohn Baldwin error = pci_alloc_msix(dev, &count); 120682cb5c3bSJohn Baldwin if (error == 0) 120782cb5c3bSJohn Baldwin rid = 1; 120882cb5c3bSJohn Baldwin } 1209e6b83836SJohn Baldwin } 121082cb5c3bSJohn Baldwin 121182cb5c3bSJohn Baldwin if (rid < 0 && pci_msi_count(dev) > 0) { 121282cb5c3bSJohn Baldwin count = 1; 121382cb5c3bSJohn Baldwin error = pci_alloc_msi(dev, &count); 121482cb5c3bSJohn Baldwin if (error == 0) 121582cb5c3bSJohn Baldwin rid = 1; 121682cb5c3bSJohn Baldwin } 121782cb5c3bSJohn Baldwin 121882cb5c3bSJohn Baldwin if (rid < 0) 121982cb5c3bSJohn Baldwin rid = 0; 122082cb5c3bSJohn Baldwin 122182cb5c3bSJohn Baldwin sc->pcie_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1222f14f0051SChuck Tuffli RF_ACTIVE | RF_SHAREABLE); 122382cb5c3bSJohn Baldwin if (sc->pcie_irq == NULL) { 122482cb5c3bSJohn Baldwin device_printf(dev, 122582cb5c3bSJohn Baldwin "Failed to allocate interrupt for PCI-e events\n"); 122682cb5c3bSJohn Baldwin if (rid > 0) 122782cb5c3bSJohn Baldwin pci_release_msi(dev); 122882cb5c3bSJohn Baldwin return (ENXIO); 122982cb5c3bSJohn Baldwin } 123082cb5c3bSJohn Baldwin 123113d700adSScott Long error = bus_setup_intr(dev, sc->pcie_irq, INTR_TYPE_MISC|INTR_MPSAFE, 12328a1926c5SWarner Losh NULL, pcib_pcie_intr_hotplug, sc, &sc->pcie_ihand); 123382cb5c3bSJohn Baldwin if (error) { 123482cb5c3bSJohn Baldwin device_printf(dev, "Failed to setup PCI-e interrupt handler\n"); 123582cb5c3bSJohn Baldwin bus_release_resource(dev, SYS_RES_IRQ, rid, sc->pcie_irq); 123682cb5c3bSJohn Baldwin if (rid > 0) 123782cb5c3bSJohn Baldwin pci_release_msi(dev); 123882cb5c3bSJohn Baldwin return (error); 123982cb5c3bSJohn Baldwin } 124082cb5c3bSJohn Baldwin return (0); 124182cb5c3bSJohn Baldwin } 124282cb5c3bSJohn Baldwin 12436f33eaa5SJohn Baldwin static int 12446f33eaa5SJohn Baldwin pcib_release_pcie_irq(struct pcib_softc *sc) 12456f33eaa5SJohn Baldwin { 12466f33eaa5SJohn Baldwin device_t dev; 12476f33eaa5SJohn Baldwin int error; 12486f33eaa5SJohn Baldwin 12496f33eaa5SJohn Baldwin dev = sc->dev; 12506f33eaa5SJohn Baldwin error = bus_teardown_intr(dev, sc->pcie_irq, sc->pcie_ihand); 12516f33eaa5SJohn Baldwin if (error) 12526f33eaa5SJohn Baldwin return (error); 12536f33eaa5SJohn Baldwin error = bus_free_resource(dev, SYS_RES_IRQ, sc->pcie_irq); 12546f33eaa5SJohn Baldwin if (error) 12556f33eaa5SJohn Baldwin return (error); 1256e6b83836SJohn Baldwin error = pci_release_msi(dev); 1257e6b83836SJohn Baldwin if (error) 1258e6b83836SJohn Baldwin return (error); 1259e6b83836SJohn Baldwin if (sc->pcie_mem != NULL) 1260e6b83836SJohn Baldwin error = bus_free_resource(dev, SYS_RES_MEMORY, sc->pcie_mem); 1261e6b83836SJohn Baldwin return (error); 12626f33eaa5SJohn Baldwin } 12636f33eaa5SJohn Baldwin 126482cb5c3bSJohn Baldwin static void 126582cb5c3bSJohn Baldwin pcib_setup_hotplug(struct pcib_softc *sc) 126682cb5c3bSJohn Baldwin { 126782cb5c3bSJohn Baldwin device_t dev; 126882cb5c3bSJohn Baldwin uint16_t mask, val; 126982cb5c3bSJohn Baldwin 127082cb5c3bSJohn Baldwin dev = sc->dev; 127182cb5c3bSJohn Baldwin TASK_INIT(&sc->pcie_hp_task, 0, pcib_pcie_hotplug_task, sc); 1272*b23314ecSJohn Baldwin TIMEOUT_TASK_INIT(taskqueue_bus, &sc->pcie_ab_task, 0, 1273fa3b03d3SAlexander Motin pcib_pcie_ab_timeout, sc); 1274*b23314ecSJohn Baldwin TIMEOUT_TASK_INIT(taskqueue_bus, &sc->pcie_cc_task, 0, 1275fa3b03d3SAlexander Motin pcib_pcie_cc_timeout, sc); 1276*b23314ecSJohn Baldwin TIMEOUT_TASK_INIT(taskqueue_bus, &sc->pcie_dll_task, 0, 1277fa3b03d3SAlexander Motin pcib_pcie_dll_timeout, sc); 1278c6df6f53SWarner Losh sc->pcie_hp_lock = bus_topo_mtx(); 127982cb5c3bSJohn Baldwin 128082cb5c3bSJohn Baldwin /* Allocate IRQ. */ 128182cb5c3bSJohn Baldwin if (pcib_alloc_pcie_irq(sc) != 0) 128282cb5c3bSJohn Baldwin return; 128382cb5c3bSJohn Baldwin 128482cb5c3bSJohn Baldwin sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 128582cb5c3bSJohn Baldwin sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 128682cb5c3bSJohn Baldwin 12876f33eaa5SJohn Baldwin /* Clear any events previously pending. */ 12886f33eaa5SJohn Baldwin pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2); 12896f33eaa5SJohn Baldwin 129082cb5c3bSJohn Baldwin /* Enable HotPlug events. */ 129182cb5c3bSJohn Baldwin mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | 129282cb5c3bSJohn Baldwin PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE | 129382cb5c3bSJohn Baldwin PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE; 129437290148SEric van Gyzen val = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | PCIEM_SLOT_CTL_PDCE; 129582cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_APB) 129682cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_ABPE; 129782cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) 129882cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PFDE; 129982cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) 130082cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_MRLSCE; 130182cb5c3bSJohn Baldwin if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS)) 130282cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_CCIE; 130382cb5c3bSJohn Baldwin 130482cb5c3bSJohn Baldwin /* Turn the attention indicator off. */ 130582cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) { 130682cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_AIC; 130782cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_AI_OFF; 130882cb5c3bSJohn Baldwin } 130982cb5c3bSJohn Baldwin 131082cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, val, mask, false); 131182cb5c3bSJohn Baldwin } 13126f33eaa5SJohn Baldwin 13136f33eaa5SJohn Baldwin static int 13146f33eaa5SJohn Baldwin pcib_detach_hotplug(struct pcib_softc *sc) 13156f33eaa5SJohn Baldwin { 13166f33eaa5SJohn Baldwin uint16_t mask, val; 13176f33eaa5SJohn Baldwin int error; 13186f33eaa5SJohn Baldwin 13196f33eaa5SJohn Baldwin /* Disable the card in the slot and force it to detach. */ 13206f33eaa5SJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 13216f33eaa5SJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 1322*b23314ecSJohn Baldwin taskqueue_cancel_timeout(taskqueue_bus, &sc->pcie_ab_task, 1323fa3b03d3SAlexander Motin NULL); 13246f33eaa5SJohn Baldwin } 13256f33eaa5SJohn Baldwin sc->flags |= PCIB_DETACHING; 13266f33eaa5SJohn Baldwin 13276f33eaa5SJohn Baldwin if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) { 1328*b23314ecSJohn Baldwin taskqueue_cancel_timeout(taskqueue_bus, &sc->pcie_cc_task, 1329fa3b03d3SAlexander Motin NULL); 13306f33eaa5SJohn Baldwin tsleep(sc, 0, "hpcmd", hz); 13316f33eaa5SJohn Baldwin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 13326f33eaa5SJohn Baldwin } 13336f33eaa5SJohn Baldwin 13346f33eaa5SJohn Baldwin /* Disable HotPlug events. */ 13356f33eaa5SJohn Baldwin mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | 13366f33eaa5SJohn Baldwin PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE | 13376f33eaa5SJohn Baldwin PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE; 13386f33eaa5SJohn Baldwin val = 0; 13396f33eaa5SJohn Baldwin 13406f33eaa5SJohn Baldwin /* Turn the attention indicator off. */ 13416f33eaa5SJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) { 13426f33eaa5SJohn Baldwin mask |= PCIEM_SLOT_CTL_AIC; 13436f33eaa5SJohn Baldwin val |= PCIEM_SLOT_CTL_AI_OFF; 13446f33eaa5SJohn Baldwin } 13456f33eaa5SJohn Baldwin 13466f33eaa5SJohn Baldwin pcib_pcie_hotplug_update(sc, val, mask, false); 13476f33eaa5SJohn Baldwin 13486f33eaa5SJohn Baldwin error = pcib_release_pcie_irq(sc); 13496f33eaa5SJohn Baldwin if (error) 13506f33eaa5SJohn Baldwin return (error); 1351*b23314ecSJohn Baldwin taskqueue_drain(taskqueue_bus, &sc->pcie_hp_task); 1352*b23314ecSJohn Baldwin taskqueue_drain_timeout(taskqueue_bus, &sc->pcie_ab_task); 1353*b23314ecSJohn Baldwin taskqueue_drain_timeout(taskqueue_bus, &sc->pcie_cc_task); 1354*b23314ecSJohn Baldwin taskqueue_drain_timeout(taskqueue_bus, &sc->pcie_dll_task); 13556f33eaa5SJohn Baldwin return (0); 13566f33eaa5SJohn Baldwin } 135782cb5c3bSJohn Baldwin #endif 135882cb5c3bSJohn Baldwin 1359e36af292SJung-uk Kim /* 1360e36af292SJung-uk Kim * Restore previous bridge configuration. 1361e36af292SJung-uk Kim */ 1362e36af292SJung-uk Kim static void 1363e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc) 1364e36af292SJung-uk Kim { 136583c41143SJohn Baldwin pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM); 1366e36af292SJung-uk Kim } 1367e36af292SJung-uk Kim 1368e36af292SJung-uk Kim /* 1369bb0d0a8eSMike Smith * Generic device interface 1370bb0d0a8eSMike Smith */ 1371bb0d0a8eSMike Smith static int 1372bb0d0a8eSMike Smith pcib_probe(device_t dev) 1373bb0d0a8eSMike Smith { 1374bb0d0a8eSMike Smith if ((pci_get_class(dev) == PCIC_BRIDGE) && 1375bb0d0a8eSMike Smith (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) { 1376bb0d0a8eSMike Smith device_set_desc(dev, "PCI-PCI bridge"); 1377b7cbd25bSMarcel Moolenaar return(-10000); 1378bb0d0a8eSMike Smith } 1379bb0d0a8eSMike Smith return(ENXIO); 1380bb0d0a8eSMike Smith } 1381bb0d0a8eSMike Smith 13826f0d5884SJohn Baldwin void 13836f0d5884SJohn Baldwin pcib_attach_common(device_t dev) 1384bb0d0a8eSMike Smith { 1385bb0d0a8eSMike Smith struct pcib_softc *sc; 1386abf07f13SWarner Losh struct sysctl_ctx_list *sctx; 1387abf07f13SWarner Losh struct sysctl_oid *soid; 1388c825d4dcSJohn Baldwin int comma; 1389bb0d0a8eSMike Smith 1390bb0d0a8eSMike Smith sc = device_get_softc(dev); 1391bb0d0a8eSMike Smith sc->dev = dev; 1392bb0d0a8eSMike Smith 13934fa59183SMike Smith /* 13944fa59183SMike Smith * Get current bridge configuration. 13954fa59183SMike Smith */ 139655aaf894SMarius Strobl sc->domain = pci_get_domain(dev); 1397ad6f36f8SJohn Baldwin sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 13984fa59183SMike Smith 13994fa59183SMike Smith /* 14004edef187SJohn Baldwin * The primary bus register should always be the bus of the 14014edef187SJohn Baldwin * parent. 14024edef187SJohn Baldwin */ 14034edef187SJohn Baldwin sc->pribus = pci_get_bus(dev); 14044edef187SJohn Baldwin pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1); 14054edef187SJohn Baldwin 14064edef187SJohn Baldwin /* 1407abf07f13SWarner Losh * Setup sysctl reporting nodes 1408abf07f13SWarner Losh */ 1409abf07f13SWarner Losh sctx = device_get_sysctl_ctx(dev); 1410abf07f13SWarner Losh soid = device_get_sysctl_tree(dev); 1411abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain", 1412abf07f13SWarner Losh CTLFLAG_RD, &sc->domain, 0, "Domain number"); 1413abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus", 1414abf07f13SWarner Losh CTLFLAG_RD, &sc->pribus, 0, "Primary bus number"); 1415abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus", 14164edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number"); 1417abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus", 14184edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number"); 1419abf07f13SWarner Losh 1420abf07f13SWarner Losh /* 14214fa59183SMike Smith * Quirk handling. 14224fa59183SMike Smith */ 14234fa59183SMike Smith switch (pci_get_devid(dev)) { 1424e4b59fc5SWarner Losh /* 1425e4b59fc5SWarner Losh * The i82380FB mobile docking controller is a PCI-PCI bridge, 1426e4b59fc5SWarner Losh * and it is a subtractive bridge. However, the ProgIf is wrong 1427e4b59fc5SWarner Losh * so the normal setting of PCIB_SUBTRACTIVE bit doesn't 14284718610dSZbigniew Bodek * happen. There are also Toshiba and Cavium ThunderX bridges 14294718610dSZbigniew Bodek * that behave this way. 1430e4b59fc5SWarner Losh */ 14314718610dSZbigniew Bodek case 0xa002177d: /* Cavium ThunderX */ 1432e4b59fc5SWarner Losh case 0x124b8086: /* Intel 82380FB Mobile */ 1433e4b59fc5SWarner Losh case 0x060513d7: /* Toshiba ???? */ 1434e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 1435e4b59fc5SWarner Losh break; 1436e4b59fc5SWarner Losh } 1437e4b59fc5SWarner Losh 143822bf1c7fSJohn Baldwin if (pci_msi_device_blacklisted(dev)) 143922bf1c7fSJohn Baldwin sc->flags |= PCIB_DISABLE_MSI; 144022bf1c7fSJohn Baldwin 144168e9cbd3SMarius Strobl if (pci_msix_device_blacklisted(dev)) 144268e9cbd3SMarius Strobl sc->flags |= PCIB_DISABLE_MSIX; 144368e9cbd3SMarius Strobl 1444e4b59fc5SWarner Losh /* 1445e4b59fc5SWarner Losh * Intel 815, 845 and other chipsets say they are PCI-PCI bridges, 1446e4b59fc5SWarner Losh * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM, 1447e4b59fc5SWarner Losh * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese. 1448e4b59fc5SWarner Losh * This means they act as if they were subtractively decoding 1449e4b59fc5SWarner Losh * bridges and pass all transactions. Mark them and real ProgIf 1 1450e4b59fc5SWarner Losh * parts as subtractive. 1451e4b59fc5SWarner Losh */ 1452e4b59fc5SWarner Losh if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 || 1453657d9f9fSJohn Baldwin pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE) 1454e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 1455e4b59fc5SWarner Losh 145682cb5c3bSJohn Baldwin #ifdef PCI_HP 145782cb5c3bSJohn Baldwin pcib_probe_hotplug(sc); 145882cb5c3bSJohn Baldwin #endif 14594edef187SJohn Baldwin pcib_setup_secbus(dev, &sc->bus, 1); 146083c41143SJohn Baldwin pcib_probe_windows(sc); 146182cb5c3bSJohn Baldwin #ifdef PCI_HP 146282cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG) 146382cb5c3bSJohn Baldwin pcib_setup_hotplug(sc); 146482cb5c3bSJohn Baldwin #endif 1465bb0d0a8eSMike Smith if (bootverbose) { 146655aaf894SMarius Strobl device_printf(dev, " domain %d\n", sc->domain); 14674edef187SJohn Baldwin device_printf(dev, " secondary bus %d\n", sc->bus.sec); 14684edef187SJohn Baldwin device_printf(dev, " subordinate bus %d\n", sc->bus.sub); 146983c41143SJohn Baldwin if (pcib_is_window_open(&sc->io)) 147083c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%jx-0x%jx\n", 147183c41143SJohn Baldwin (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit); 147283c41143SJohn Baldwin if (pcib_is_window_open(&sc->mem)) 147383c41143SJohn Baldwin device_printf(dev, " memory decode 0x%jx-0x%jx\n", 147483c41143SJohn Baldwin (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit); 147583c41143SJohn Baldwin if (pcib_is_window_open(&sc->pmem)) 147683c41143SJohn Baldwin device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 147783c41143SJohn Baldwin (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit); 1478c825d4dcSJohn Baldwin if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) || 1479c825d4dcSJohn Baldwin sc->flags & PCIB_SUBTRACTIVE) { 1480c825d4dcSJohn Baldwin device_printf(dev, " special decode "); 1481c825d4dcSJohn Baldwin comma = 0; 1482c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) { 1483c825d4dcSJohn Baldwin printf("ISA"); 1484c825d4dcSJohn Baldwin comma = 1; 1485c825d4dcSJohn Baldwin } 1486c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) { 1487c825d4dcSJohn Baldwin printf("%sVGA", comma ? ", " : ""); 1488c825d4dcSJohn Baldwin comma = 1; 1489c825d4dcSJohn Baldwin } 1490e4b59fc5SWarner Losh if (sc->flags & PCIB_SUBTRACTIVE) 1491c825d4dcSJohn Baldwin printf("%ssubtractive", comma ? ", " : ""); 1492c825d4dcSJohn Baldwin printf("\n"); 1493c825d4dcSJohn Baldwin } 1494bb0d0a8eSMike Smith } 1495bb0d0a8eSMike Smith 1496bb0d0a8eSMike Smith /* 1497ef888152SJohn Baldwin * Always enable busmastering on bridges so that transactions 1498ef888152SJohn Baldwin * initiated on the secondary bus are passed through to the 1499ef888152SJohn Baldwin * primary bus. 1500ef888152SJohn Baldwin */ 1501ef888152SJohn Baldwin pci_enable_busmaster(dev); 15026f0d5884SJohn Baldwin } 1503bb0d0a8eSMike Smith 150482cb5c3bSJohn Baldwin #ifdef PCI_HP 150582cb5c3bSJohn Baldwin static int 150682cb5c3bSJohn Baldwin pcib_present(struct pcib_softc *sc) 150782cb5c3bSJohn Baldwin { 150882cb5c3bSJohn Baldwin 150982cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG) 151082cb5c3bSJohn Baldwin return (pcib_hotplug_present(sc) != 0); 151182cb5c3bSJohn Baldwin return (1); 151282cb5c3bSJohn Baldwin } 151382cb5c3bSJohn Baldwin #endif 151482cb5c3bSJohn Baldwin 151538906aedSJohn Baldwin int 151667e7d085SJohn Baldwin pcib_attach_child(device_t dev) 15176f0d5884SJohn Baldwin { 15186f0d5884SJohn Baldwin struct pcib_softc *sc; 15196f0d5884SJohn Baldwin 15206f0d5884SJohn Baldwin sc = device_get_softc(dev); 152167e7d085SJohn Baldwin if (sc->bus.sec == 0) { 152267e7d085SJohn Baldwin /* no secondary bus; we should have fixed this */ 152367e7d085SJohn Baldwin return(0); 152467e7d085SJohn Baldwin } 152567e7d085SJohn Baldwin 152682cb5c3bSJohn Baldwin #ifdef PCI_HP 152782cb5c3bSJohn Baldwin if (!pcib_present(sc)) { 152882cb5c3bSJohn Baldwin /* An empty HotPlug slot, so don't add a PCI bus yet. */ 152982cb5c3bSJohn Baldwin return (0); 153082cb5c3bSJohn Baldwin } 153182cb5c3bSJohn Baldwin #endif 153282cb5c3bSJohn Baldwin 15335b56413dSWarner Losh sc->child = device_add_child(dev, "pci", DEVICE_UNIT_ANY); 153418250ec6SJohn Baldwin bus_attach_children(dev); 153518250ec6SJohn Baldwin return (0); 1536bb0d0a8eSMike Smith } 1537bb0d0a8eSMike Smith 153867e7d085SJohn Baldwin int 153967e7d085SJohn Baldwin pcib_attach(device_t dev) 154067e7d085SJohn Baldwin { 154167e7d085SJohn Baldwin 154267e7d085SJohn Baldwin pcib_attach_common(dev); 154367e7d085SJohn Baldwin return (pcib_attach_child(dev)); 1544bb0d0a8eSMike Smith } 1545bb0d0a8eSMike Smith 15466f0d5884SJohn Baldwin int 15476f33eaa5SJohn Baldwin pcib_detach(device_t dev) 15486f33eaa5SJohn Baldwin { 15496f33eaa5SJohn Baldwin struct pcib_softc *sc; 15506f33eaa5SJohn Baldwin int error; 15516f33eaa5SJohn Baldwin 15526f33eaa5SJohn Baldwin sc = device_get_softc(dev); 1553e9d38570SJohn Baldwin error = bus_detach_children(dev); 15546f33eaa5SJohn Baldwin if (error) 15556f33eaa5SJohn Baldwin return (error); 15566f33eaa5SJohn Baldwin #ifdef PCI_HP 15576f33eaa5SJohn Baldwin if (sc->flags & PCIB_HOTPLUG) { 15586f33eaa5SJohn Baldwin error = pcib_detach_hotplug(sc); 15596f33eaa5SJohn Baldwin if (error) 15606f33eaa5SJohn Baldwin return (error); 15616f33eaa5SJohn Baldwin } 15626f33eaa5SJohn Baldwin #endif 15636f33eaa5SJohn Baldwin error = device_delete_children(dev); 15646f33eaa5SJohn Baldwin if (error) 15656f33eaa5SJohn Baldwin return (error); 15666f33eaa5SJohn Baldwin pcib_free_windows(sc); 15676f33eaa5SJohn Baldwin pcib_free_secbus(dev, &sc->bus); 15686f33eaa5SJohn Baldwin return (0); 15696f33eaa5SJohn Baldwin } 15706f33eaa5SJohn Baldwin 15716f33eaa5SJohn Baldwin int 1572e36af292SJung-uk Kim pcib_resume(device_t dev) 1573e36af292SJung-uk Kim { 1574e36af292SJung-uk Kim 1575e36af292SJung-uk Kim pcib_cfg_restore(device_get_softc(dev)); 1576cffd37daSAndriy Gapon 1577cffd37daSAndriy Gapon /* 1578cffd37daSAndriy Gapon * Restore the Command register only after restoring the windows. 1579cffd37daSAndriy Gapon * The bridge should not be claiming random windows. 1580cffd37daSAndriy Gapon */ 1581cffd37daSAndriy Gapon pci_write_config(dev, PCIR_COMMAND, pci_get_cmdreg(dev), 2); 1582e36af292SJung-uk Kim return (bus_generic_resume(dev)); 1583e36af292SJung-uk Kim } 1584e36af292SJung-uk Kim 1585809923caSJustin Hibbits void 1586809923caSJustin Hibbits pcib_bridge_init(device_t dev) 1587809923caSJustin Hibbits { 1588809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 1589809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2); 1590809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1); 1591809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2); 1592809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2); 1593809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2); 1594809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 1595809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4); 1596809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2); 1597809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4); 1598809923caSJustin Hibbits } 1599809923caSJustin Hibbits 1600e36af292SJung-uk Kim int 160182cb5c3bSJohn Baldwin pcib_child_present(device_t dev, device_t child) 160282cb5c3bSJohn Baldwin { 160382cb5c3bSJohn Baldwin #ifdef PCI_HP 160482cb5c3bSJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 160582cb5c3bSJohn Baldwin int retval; 160682cb5c3bSJohn Baldwin 160782cb5c3bSJohn Baldwin retval = bus_child_present(dev); 160882cb5c3bSJohn Baldwin if (retval != 0 && sc->flags & PCIB_HOTPLUG) 160982cb5c3bSJohn Baldwin retval = pcib_hotplug_present(sc); 161082cb5c3bSJohn Baldwin return (retval); 161182cb5c3bSJohn Baldwin #else 161282cb5c3bSJohn Baldwin return (bus_child_present(dev)); 161382cb5c3bSJohn Baldwin #endif 161482cb5c3bSJohn Baldwin } 161582cb5c3bSJohn Baldwin 161682cb5c3bSJohn Baldwin int 1617bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 1618bb0d0a8eSMike Smith { 1619bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 1620bb0d0a8eSMike Smith 1621bb0d0a8eSMike Smith switch (which) { 162255aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 162355aaf894SMarius Strobl *result = sc->domain; 162455aaf894SMarius Strobl return(0); 1625bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 16264edef187SJohn Baldwin *result = sc->bus.sec; 1627bb0d0a8eSMike Smith return(0); 1628bb0d0a8eSMike Smith } 1629bb0d0a8eSMike Smith return(ENOENT); 1630bb0d0a8eSMike Smith } 1631bb0d0a8eSMike Smith 16326f0d5884SJohn Baldwin int 1633bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 1634bb0d0a8eSMike Smith { 1635bb0d0a8eSMike Smith 1636bb0d0a8eSMike Smith switch (which) { 163755aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 163855aaf894SMarius Strobl return(EINVAL); 1639bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 16404edef187SJohn Baldwin return(EINVAL); 1641bb0d0a8eSMike Smith } 1642bb0d0a8eSMike Smith return(ENOENT); 1643bb0d0a8eSMike Smith } 1644bb0d0a8eSMike Smith 164583c41143SJohn Baldwin /* 164683c41143SJohn Baldwin * Attempt to allocate a resource from the existing resources assigned 164783c41143SJohn Baldwin * to a window. 164883c41143SJohn Baldwin */ 164983c41143SJohn Baldwin static struct resource * 165083c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w, 16512dd1bdf1SJustin Hibbits device_t child, int type, int *rid, rman_res_t start, rman_res_t end, 16522dd1bdf1SJustin Hibbits rman_res_t count, u_int flags) 165383c41143SJohn Baldwin { 165483c41143SJohn Baldwin struct resource *res; 165583c41143SJohn Baldwin 165683c41143SJohn Baldwin if (!pcib_is_window_open(w)) 165783c41143SJohn Baldwin return (NULL); 165883c41143SJohn Baldwin 165983c41143SJohn Baldwin res = rman_reserve_resource(&w->rman, start, end, count, 166083c41143SJohn Baldwin flags & ~RF_ACTIVE, child); 166183c41143SJohn Baldwin if (res == NULL) 166283c41143SJohn Baldwin return (NULL); 166383c41143SJohn Baldwin 166483c41143SJohn Baldwin if (bootverbose) 166583c41143SJohn Baldwin device_printf(sc->dev, 1666da1b038aSJustin Hibbits "allocated %s range (%#jx-%#jx) for rid %x of %s\n", 166783c41143SJohn Baldwin w->name, rman_get_start(res), rman_get_end(res), *rid, 166883c41143SJohn Baldwin pcib_child_name(child)); 166983c41143SJohn Baldwin rman_set_rid(res, *rid); 16701b9bcfffSJohn Baldwin rman_set_type(res, type); 167183c41143SJohn Baldwin 167283c41143SJohn Baldwin if (flags & RF_ACTIVE) { 167383c41143SJohn Baldwin if (bus_activate_resource(child, type, *rid, res) != 0) { 167483c41143SJohn Baldwin rman_release_resource(res); 167583c41143SJohn Baldwin return (NULL); 167683c41143SJohn Baldwin } 167783c41143SJohn Baldwin } 167883c41143SJohn Baldwin 167983c41143SJohn Baldwin return (res); 168083c41143SJohn Baldwin } 168183c41143SJohn Baldwin 1682c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */ 1683c825d4dcSJohn Baldwin static int 1684c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type, 16852dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 1686c825d4dcSJohn Baldwin { 1687c825d4dcSJohn Baldwin struct resource *res; 16882dd1bdf1SJustin Hibbits rman_res_t base, limit, wmask; 1689c825d4dcSJohn Baldwin int rid; 1690c825d4dcSJohn Baldwin 1691c825d4dcSJohn Baldwin /* 1692c825d4dcSJohn Baldwin * If this is an I/O window on a bridge with ISA enable set 1693c825d4dcSJohn Baldwin * and the start address is below 64k, then try to allocate an 1694c825d4dcSJohn Baldwin * initial window of 0x1000 bytes long starting at address 1695c825d4dcSJohn Baldwin * 0xf000 and walking down. Note that if the original request 1696c825d4dcSJohn Baldwin * was larger than the non-aliased range size of 0x100 our 1697c825d4dcSJohn Baldwin * caller would have raised the start address up to 64k 1698c825d4dcSJohn Baldwin * already. 1699c825d4dcSJohn Baldwin */ 1700c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1701c825d4dcSJohn Baldwin start < 65536) { 1702c825d4dcSJohn Baldwin for (base = 0xf000; (long)base >= 0; base -= 0x1000) { 1703c825d4dcSJohn Baldwin limit = base + 0xfff; 1704c825d4dcSJohn Baldwin 1705c825d4dcSJohn Baldwin /* 1706c825d4dcSJohn Baldwin * Skip ranges that wouldn't work for the 1707c825d4dcSJohn Baldwin * original request. Note that the actual 1708c825d4dcSJohn Baldwin * window that overlaps are the non-alias 1709c825d4dcSJohn Baldwin * ranges within [base, limit], so this isn't 1710c825d4dcSJohn Baldwin * quite a simple comparison. 1711c825d4dcSJohn Baldwin */ 1712c825d4dcSJohn Baldwin if (start + count > limit - 0x400) 1713c825d4dcSJohn Baldwin continue; 1714c825d4dcSJohn Baldwin if (base == 0) { 1715c825d4dcSJohn Baldwin /* 1716c825d4dcSJohn Baldwin * The first open region for the window at 1717c825d4dcSJohn Baldwin * 0 is 0x400-0x4ff. 1718c825d4dcSJohn Baldwin */ 1719c825d4dcSJohn Baldwin if (end - count + 1 < 0x400) 1720c825d4dcSJohn Baldwin continue; 1721c825d4dcSJohn Baldwin } else { 1722c825d4dcSJohn Baldwin if (end - count + 1 < base) 1723c825d4dcSJohn Baldwin continue; 1724c825d4dcSJohn Baldwin } 1725c825d4dcSJohn Baldwin 1726c825d4dcSJohn Baldwin if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) { 1727c825d4dcSJohn Baldwin w->base = base; 1728c825d4dcSJohn Baldwin w->limit = limit; 1729c825d4dcSJohn Baldwin return (0); 1730c825d4dcSJohn Baldwin } 1731c825d4dcSJohn Baldwin } 1732c825d4dcSJohn Baldwin return (ENOSPC); 1733c825d4dcSJohn Baldwin } 1734c825d4dcSJohn Baldwin 173589977ce2SJustin Hibbits wmask = ((rman_res_t)1 << w->step) - 1; 1736c825d4dcSJohn Baldwin if (RF_ALIGNMENT(flags) < w->step) { 1737c825d4dcSJohn Baldwin flags &= ~RF_ALIGNMENT_MASK; 1738c825d4dcSJohn Baldwin flags |= RF_ALIGNMENT_LOG2(w->step); 1739c825d4dcSJohn Baldwin } 1740c825d4dcSJohn Baldwin start &= ~wmask; 1741c825d4dcSJohn Baldwin end |= wmask; 174289977ce2SJustin Hibbits count = roundup2(count, (rman_res_t)1 << w->step); 1743c825d4dcSJohn Baldwin rid = w->reg; 1744c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, start, end, count, 1745b377ff81SJohn Baldwin flags | RF_ACTIVE | RF_UNMAPPED); 1746c825d4dcSJohn Baldwin if (res == NULL) 1747c825d4dcSJohn Baldwin return (ENOSPC); 1748c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 1749c825d4dcSJohn Baldwin pcib_activate_window(sc, type); 1750c825d4dcSJohn Baldwin w->base = rman_get_start(res); 1751c825d4dcSJohn Baldwin w->limit = rman_get_end(res); 1752c825d4dcSJohn Baldwin return (0); 1753c825d4dcSJohn Baldwin } 1754c825d4dcSJohn Baldwin 1755c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */ 1756c825d4dcSJohn Baldwin static int 1757c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type, 17582dd1bdf1SJustin Hibbits rman_res_t base, rman_res_t limit) 1759c825d4dcSJohn Baldwin { 1760c825d4dcSJohn Baldwin struct resource *res; 1761c825d4dcSJohn Baldwin int error, i, force_64k_base; 1762c825d4dcSJohn Baldwin 1763c825d4dcSJohn Baldwin KASSERT(base <= w->base && limit >= w->limit, 1764c825d4dcSJohn Baldwin ("attempting to shrink window")); 1765c825d4dcSJohn Baldwin 1766c825d4dcSJohn Baldwin /* 1767c825d4dcSJohn Baldwin * XXX: pcib_grow_window() doesn't try to do this anyway and 1768c825d4dcSJohn Baldwin * the error handling for all the edge cases would be tedious. 1769c825d4dcSJohn Baldwin */ 1770c825d4dcSJohn Baldwin KASSERT(limit == w->limit || base == w->base, 1771c825d4dcSJohn Baldwin ("attempting to grow both ends of a window")); 1772c825d4dcSJohn Baldwin 1773c825d4dcSJohn Baldwin /* 1774c825d4dcSJohn Baldwin * Yet more special handling for requests to expand an I/O 1775c825d4dcSJohn Baldwin * window behind an ISA-enabled bridge. Since I/O windows 1776c825d4dcSJohn Baldwin * have to grow in 0x1000 increments and the end of the 0xffff 1777c825d4dcSJohn Baldwin * range is an alias, growing a window below 64k will always 1778c825d4dcSJohn Baldwin * result in allocating new resources and never adjusting an 1779c825d4dcSJohn Baldwin * existing resource. 1780c825d4dcSJohn Baldwin */ 1781c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1782c825d4dcSJohn Baldwin (limit <= 65535 || (base <= 65535 && base != w->base))) { 1783c825d4dcSJohn Baldwin KASSERT(limit == w->limit || limit <= 65535, 1784c825d4dcSJohn Baldwin ("attempting to grow both ends across 64k ISA alias")); 1785c825d4dcSJohn Baldwin 1786c825d4dcSJohn Baldwin if (base != w->base) 1787c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1); 1788c825d4dcSJohn Baldwin else 1789c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, w->limit + 1, 1790c825d4dcSJohn Baldwin limit); 1791c825d4dcSJohn Baldwin if (error == 0) { 1792c825d4dcSJohn Baldwin w->base = base; 1793c825d4dcSJohn Baldwin w->limit = limit; 1794c825d4dcSJohn Baldwin } 1795c825d4dcSJohn Baldwin return (error); 1796c825d4dcSJohn Baldwin } 1797c825d4dcSJohn Baldwin 1798c825d4dcSJohn Baldwin /* 1799c825d4dcSJohn Baldwin * Find the existing resource to adjust. Usually there is only one, 1800c825d4dcSJohn Baldwin * but for an ISA-enabled bridge we might be growing the I/O window 1801c825d4dcSJohn Baldwin * above 64k and need to find the existing resource that maps all 1802c825d4dcSJohn Baldwin * of the area above 64k. 1803c825d4dcSJohn Baldwin */ 1804c825d4dcSJohn Baldwin for (i = 0; i < w->count; i++) { 1805c825d4dcSJohn Baldwin if (rman_get_end(w->res[i]) == w->limit) 1806c825d4dcSJohn Baldwin break; 1807c825d4dcSJohn Baldwin } 1808c825d4dcSJohn Baldwin KASSERT(i != w->count, ("did not find existing resource")); 1809c825d4dcSJohn Baldwin res = w->res[i]; 1810c825d4dcSJohn Baldwin 1811c825d4dcSJohn Baldwin /* 1812c825d4dcSJohn Baldwin * Usually the resource we found should match the window's 1813c825d4dcSJohn Baldwin * existing range. The one exception is the ISA-enabled case 1814c825d4dcSJohn Baldwin * mentioned above in which case the resource should start at 1815c825d4dcSJohn Baldwin * 64k. 1816c825d4dcSJohn Baldwin */ 1817c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1818c825d4dcSJohn Baldwin w->base <= 65535) { 1819c825d4dcSJohn Baldwin KASSERT(rman_get_start(res) == 65536, 1820c825d4dcSJohn Baldwin ("existing resource mismatch")); 1821c825d4dcSJohn Baldwin force_64k_base = 1; 1822c825d4dcSJohn Baldwin } else { 1823c825d4dcSJohn Baldwin KASSERT(w->base == rman_get_start(res), 1824c825d4dcSJohn Baldwin ("existing resource mismatch")); 1825c825d4dcSJohn Baldwin force_64k_base = 0; 1826c825d4dcSJohn Baldwin } 1827c825d4dcSJohn Baldwin 1828c825d4dcSJohn Baldwin error = bus_adjust_resource(sc->dev, type, res, force_64k_base ? 1829c825d4dcSJohn Baldwin rman_get_start(res) : base, limit); 1830c825d4dcSJohn Baldwin if (error) 1831c825d4dcSJohn Baldwin return (error); 1832c825d4dcSJohn Baldwin 1833c825d4dcSJohn Baldwin /* Add the newly allocated region to the resource manager. */ 1834c825d4dcSJohn Baldwin if (w->base != base) { 1835c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, base, w->base - 1); 1836c825d4dcSJohn Baldwin w->base = base; 1837c825d4dcSJohn Baldwin } else { 1838c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, w->limit + 1, limit); 1839c825d4dcSJohn Baldwin w->limit = limit; 1840c825d4dcSJohn Baldwin } 1841c825d4dcSJohn Baldwin if (error) { 1842c825d4dcSJohn Baldwin if (bootverbose) 1843c825d4dcSJohn Baldwin device_printf(sc->dev, 1844c825d4dcSJohn Baldwin "failed to expand %s resource manager\n", w->name); 1845c825d4dcSJohn Baldwin (void)bus_adjust_resource(sc->dev, type, res, force_64k_base ? 1846c825d4dcSJohn Baldwin rman_get_start(res) : w->base, w->limit); 1847c825d4dcSJohn Baldwin } 1848c825d4dcSJohn Baldwin return (error); 1849c825d4dcSJohn Baldwin } 1850c825d4dcSJohn Baldwin 185183c41143SJohn Baldwin /* 185283c41143SJohn Baldwin * Attempt to grow a window to make room for a given resource request. 185383c41143SJohn Baldwin */ 185483c41143SJohn Baldwin static int 185583c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type, 18562dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 185783c41143SJohn Baldwin { 18582dd1bdf1SJustin Hibbits rman_res_t align, start_free, end_free, front, back, wmask; 1859c825d4dcSJohn Baldwin int error; 186083c41143SJohn Baldwin 186183c41143SJohn Baldwin /* 186283c41143SJohn Baldwin * Clamp the desired resource range to the maximum address 186383c41143SJohn Baldwin * this window supports. Reject impossible requests. 1864c825d4dcSJohn Baldwin * 1865c825d4dcSJohn Baldwin * For I/O port requests behind a bridge with the ISA enable 1866c825d4dcSJohn Baldwin * bit set, force large allocations to start above 64k. 186783c41143SJohn Baldwin */ 186883c41143SJohn Baldwin if (!w->valid) 186983c41143SJohn Baldwin return (EINVAL); 1870c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 && 1871c825d4dcSJohn Baldwin start < 65536) 1872c825d4dcSJohn Baldwin start = 65536; 187383c41143SJohn Baldwin if (end > w->rman.rm_end) 187483c41143SJohn Baldwin end = w->rman.rm_end; 187583c41143SJohn Baldwin if (start + count - 1 > end || start + count < start) 187683c41143SJohn Baldwin return (EINVAL); 187789977ce2SJustin Hibbits wmask = ((rman_res_t)1 << w->step) - 1; 187883c41143SJohn Baldwin 187983c41143SJohn Baldwin /* 188083c41143SJohn Baldwin * If there is no resource at all, just try to allocate enough 188183c41143SJohn Baldwin * aligned space for this resource. 188283c41143SJohn Baldwin */ 188383c41143SJohn Baldwin if (w->res == NULL) { 1884c825d4dcSJohn Baldwin error = pcib_alloc_new_window(sc, w, type, start, end, count, 1885c825d4dcSJohn Baldwin flags); 1886c825d4dcSJohn Baldwin if (error) { 188783c41143SJohn Baldwin if (bootverbose) 188883c41143SJohn Baldwin device_printf(sc->dev, 1889da1b038aSJustin Hibbits "failed to allocate initial %s window (%#jx-%#jx,%#jx)\n", 189083c41143SJohn Baldwin w->name, start, end, count); 189183c41143SJohn Baldwin return (error); 189283c41143SJohn Baldwin } 1893c825d4dcSJohn Baldwin if (bootverbose) 1894c825d4dcSJohn Baldwin device_printf(sc->dev, 1895c825d4dcSJohn Baldwin "allocated initial %s window of %#jx-%#jx\n", 1896c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 189783c41143SJohn Baldwin goto updatewin; 189883c41143SJohn Baldwin } 189983c41143SJohn Baldwin 190083c41143SJohn Baldwin /* 190183c41143SJohn Baldwin * See if growing the window would help. Compute the minimum 190283c41143SJohn Baldwin * amount of address space needed on both the front and back 190383c41143SJohn Baldwin * ends of the existing window to satisfy the allocation. 190483c41143SJohn Baldwin * 190583c41143SJohn Baldwin * For each end, build a candidate region adjusting for the 190683c41143SJohn Baldwin * required alignment, etc. If there is a free region at the 190783c41143SJohn Baldwin * edge of the window, grow from the inner edge of the free 190883c41143SJohn Baldwin * region. Otherwise grow from the window boundary. 190983c41143SJohn Baldwin * 1910c825d4dcSJohn Baldwin * Growing an I/O window below 64k for a bridge with the ISA 1911c825d4dcSJohn Baldwin * enable bit doesn't require any special magic as the step 1912c825d4dcSJohn Baldwin * size of an I/O window (1k) always includes multiple 1913c825d4dcSJohn Baldwin * non-alias ranges when it is grown in either direction. 1914c825d4dcSJohn Baldwin * 191583c41143SJohn Baldwin * XXX: Special case: if w->res is completely empty and the 191683c41143SJohn Baldwin * request size is larger than w->res, we should find the 191783c41143SJohn Baldwin * optimal aligned buffer containing w->res and allocate that. 191883c41143SJohn Baldwin */ 191983c41143SJohn Baldwin if (bootverbose) 192083c41143SJohn Baldwin device_printf(sc->dev, 1921da1b038aSJustin Hibbits "attempting to grow %s window for (%#jx-%#jx,%#jx)\n", 192283c41143SJohn Baldwin w->name, start, end, count); 192389977ce2SJustin Hibbits align = (rman_res_t)1 << RF_ALIGNMENT(flags); 1924c825d4dcSJohn Baldwin if (start < w->base) { 192583c41143SJohn Baldwin if (rman_first_free_region(&w->rman, &start_free, &end_free) != 1926c825d4dcSJohn Baldwin 0 || start_free != w->base) 1927c825d4dcSJohn Baldwin end_free = w->base; 192883c41143SJohn Baldwin if (end_free > end) 1929ddac8cc9SJohn Baldwin end_free = end + 1; 193083c41143SJohn Baldwin 193183c41143SJohn Baldwin /* Move end_free down until it is properly aligned. */ 193283c41143SJohn Baldwin end_free &= ~(align - 1); 1933a49dcb46SJohn Baldwin end_free--; 1934a49dcb46SJohn Baldwin front = end_free - (count - 1); 193583c41143SJohn Baldwin 193683c41143SJohn Baldwin /* 193783c41143SJohn Baldwin * The resource would now be allocated at (front, 193883c41143SJohn Baldwin * end_free). Ensure that fits in the (start, end) 193983c41143SJohn Baldwin * bounds. end_free is checked above. If 'front' is 194083c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 194183c41143SJohn Baldwin * Also check for underflow. 194283c41143SJohn Baldwin */ 194383c41143SJohn Baldwin if (front >= start && front <= end_free) { 194483c41143SJohn Baldwin if (bootverbose) 1945da1b038aSJustin Hibbits printf("\tfront candidate range: %#jx-%#jx\n", 194683c41143SJohn Baldwin front, end_free); 1947a7b5acacSJohn Baldwin front &= ~wmask; 1948c825d4dcSJohn Baldwin front = w->base - front; 194983c41143SJohn Baldwin } else 195083c41143SJohn Baldwin front = 0; 195183c41143SJohn Baldwin } else 195283c41143SJohn Baldwin front = 0; 1953c825d4dcSJohn Baldwin if (end > w->limit) { 195483c41143SJohn Baldwin if (rman_last_free_region(&w->rman, &start_free, &end_free) != 1955c825d4dcSJohn Baldwin 0 || end_free != w->limit) 1956c825d4dcSJohn Baldwin start_free = w->limit + 1; 195783c41143SJohn Baldwin if (start_free < start) 195883c41143SJohn Baldwin start_free = start; 195983c41143SJohn Baldwin 196083c41143SJohn Baldwin /* Move start_free up until it is properly aligned. */ 196183c41143SJohn Baldwin start_free = roundup2(start_free, align); 1962a49dcb46SJohn Baldwin back = start_free + count - 1; 196383c41143SJohn Baldwin 196483c41143SJohn Baldwin /* 196583c41143SJohn Baldwin * The resource would now be allocated at (start_free, 196683c41143SJohn Baldwin * back). Ensure that fits in the (start, end) 196783c41143SJohn Baldwin * bounds. start_free is checked above. If 'back' is 196883c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 196983c41143SJohn Baldwin * Also check for overflow. 197083c41143SJohn Baldwin */ 197183c41143SJohn Baldwin if (back <= end && start_free <= back) { 197283c41143SJohn Baldwin if (bootverbose) 1973da1b038aSJustin Hibbits printf("\tback candidate range: %#jx-%#jx\n", 197483c41143SJohn Baldwin start_free, back); 1975a7b5acacSJohn Baldwin back |= wmask; 1976c825d4dcSJohn Baldwin back -= w->limit; 197783c41143SJohn Baldwin } else 197883c41143SJohn Baldwin back = 0; 197983c41143SJohn Baldwin } else 198083c41143SJohn Baldwin back = 0; 198183c41143SJohn Baldwin 198283c41143SJohn Baldwin /* 198383c41143SJohn Baldwin * Try to allocate the smallest needed region first. 198483c41143SJohn Baldwin * If that fails, fall back to the other region. 198583c41143SJohn Baldwin */ 198683c41143SJohn Baldwin error = ENOSPC; 198783c41143SJohn Baldwin while (front != 0 || back != 0) { 198883c41143SJohn Baldwin if (front != 0 && (front <= back || back == 0)) { 1989c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base - front, 1990c825d4dcSJohn Baldwin w->limit); 199183c41143SJohn Baldwin if (error == 0) 199283c41143SJohn Baldwin break; 199383c41143SJohn Baldwin front = 0; 199483c41143SJohn Baldwin } else { 1995c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base, 1996c825d4dcSJohn Baldwin w->limit + back); 199783c41143SJohn Baldwin if (error == 0) 199883c41143SJohn Baldwin break; 199983c41143SJohn Baldwin back = 0; 200083c41143SJohn Baldwin } 200183c41143SJohn Baldwin } 200283c41143SJohn Baldwin 200383c41143SJohn Baldwin if (error) 200483c41143SJohn Baldwin return (error); 200583c41143SJohn Baldwin if (bootverbose) 2006c825d4dcSJohn Baldwin device_printf(sc->dev, "grew %s window to %#jx-%#jx\n", 2007c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 200883c41143SJohn Baldwin 200983c41143SJohn Baldwin updatewin: 2010c825d4dcSJohn Baldwin /* Write the new window. */ 2011a7b5acacSJohn Baldwin KASSERT((w->base & wmask) == 0, ("start address is not aligned")); 2012a7b5acacSJohn Baldwin KASSERT((w->limit & wmask) == wmask, ("end address is not aligned")); 201383c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 201483c41143SJohn Baldwin return (0); 201583c41143SJohn Baldwin } 201683c41143SJohn Baldwin 201783c41143SJohn Baldwin /* 201883c41143SJohn Baldwin * We have to trap resource allocation requests and ensure that the bridge 201983c41143SJohn Baldwin * is set up to, or capable of handling them. 202083c41143SJohn Baldwin */ 202103719c65SJohn Baldwin static struct resource * 202283c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 20232dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 202483c41143SJohn Baldwin { 202583c41143SJohn Baldwin struct pcib_softc *sc; 202683c41143SJohn Baldwin struct resource *r; 202783c41143SJohn Baldwin 202883c41143SJohn Baldwin sc = device_get_softc(dev); 202983c41143SJohn Baldwin 203083c41143SJohn Baldwin /* 203183c41143SJohn Baldwin * VGA resources are decoded iff the VGA enable bit is set in 203283c41143SJohn Baldwin * the bridge control register. VGA resources do not fall into 203383c41143SJohn Baldwin * the resource windows and are passed up to the parent. 203483c41143SJohn Baldwin */ 203583c41143SJohn Baldwin if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) || 203683c41143SJohn Baldwin (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) { 203783c41143SJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) 203883c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, 203983c41143SJohn Baldwin rid, start, end, count, flags)); 204083c41143SJohn Baldwin else 204183c41143SJohn Baldwin return (NULL); 204283c41143SJohn Baldwin } 204383c41143SJohn Baldwin 204483c41143SJohn Baldwin switch (type) { 20454edef187SJohn Baldwin case PCI_RES_BUS: 20464edef187SJohn Baldwin return (pcib_alloc_subbus(&sc->bus, child, rid, start, end, 20474edef187SJohn Baldwin count, flags)); 204883c41143SJohn Baldwin case SYS_RES_IOPORT: 2049c825d4dcSJohn Baldwin if (pcib_is_isa_range(sc, start, end, count)) 2050c825d4dcSJohn Baldwin return (NULL); 205183c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start, 205283c41143SJohn Baldwin end, count, flags); 2053a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 205483c41143SJohn Baldwin break; 205583c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->io, type, start, end, count, 205683c41143SJohn Baldwin flags) == 0) 205783c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, 205883c41143SJohn Baldwin rid, start, end, count, flags); 205983c41143SJohn Baldwin break; 206083c41143SJohn Baldwin case SYS_RES_MEMORY: 206183c41143SJohn Baldwin /* 206283c41143SJohn Baldwin * For prefetchable resources, prefer the prefetchable 206383c41143SJohn Baldwin * memory window, but fall back to the regular memory 206483c41143SJohn Baldwin * window if that fails. Try both windows before 206583c41143SJohn Baldwin * attempting to grow a window in case the firmware 206683c41143SJohn Baldwin * has used a range in the regular memory window to 206783c41143SJohn Baldwin * map a prefetchable BAR. 206883c41143SJohn Baldwin */ 206983c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 207083c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, type, 207183c41143SJohn Baldwin rid, start, end, count, flags); 207283c41143SJohn Baldwin if (r != NULL) 207383c41143SJohn Baldwin break; 207483c41143SJohn Baldwin } 207583c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid, 207683c41143SJohn Baldwin start, end, count, flags); 2077a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 207883c41143SJohn Baldwin break; 207983c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 208083c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->pmem, type, start, end, 208183c41143SJohn Baldwin count, flags) == 0) { 208283c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, 208383c41143SJohn Baldwin type, rid, start, end, count, flags); 208483c41143SJohn Baldwin if (r != NULL) 208583c41143SJohn Baldwin break; 208683c41143SJohn Baldwin } 208783c41143SJohn Baldwin } 208883c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->mem, type, start, end, count, 208983c41143SJohn Baldwin flags & ~RF_PREFETCHABLE) == 0) 209083c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, 209183c41143SJohn Baldwin rid, start, end, count, flags); 209283c41143SJohn Baldwin break; 209383c41143SJohn Baldwin default: 209483c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 209583c41143SJohn Baldwin start, end, count, flags)); 209683c41143SJohn Baldwin } 209783c41143SJohn Baldwin 209883c41143SJohn Baldwin /* 209983c41143SJohn Baldwin * If attempts to suballocate from the window fail but this is a 210083c41143SJohn Baldwin * subtractive bridge, pass the request up the tree. 210183c41143SJohn Baldwin */ 210283c41143SJohn Baldwin if (sc->flags & PCIB_SUBTRACTIVE && r == NULL) 210383c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 210483c41143SJohn Baldwin start, end, count, flags)); 210583c41143SJohn Baldwin return (r); 210683c41143SJohn Baldwin } 210783c41143SJohn Baldwin 210803719c65SJohn Baldwin static int 2109fef01f04SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, struct resource *r, 21102dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end) 211183c41143SJohn Baldwin { 211283c41143SJohn Baldwin struct pcib_softc *sc; 211326245980SJessica Clarke struct pcib_window *w; 211415cb3b54SAlexander Motin rman_res_t wmask; 2115fef01f04SJohn Baldwin int error, type; 211683c41143SJohn Baldwin 211783c41143SJohn Baldwin sc = device_get_softc(bus); 2118fef01f04SJohn Baldwin type = rman_get_type(r); 211926245980SJessica Clarke 212026245980SJessica Clarke /* 212126245980SJessica Clarke * If the resource wasn't sub-allocated from one of our region 212226245980SJessica Clarke * managers then just pass the request up. 212326245980SJessica Clarke */ 2124582b84cdSJohn Baldwin if (!pcib_is_resource_managed(sc, r)) 2125fef01f04SJohn Baldwin return (bus_generic_adjust_resource(bus, child, r, start, end)); 212626245980SJessica Clarke 212731776afdSJessica Clarke if (type == PCI_RES_BUS) { 212831776afdSJessica Clarke /* 212931776afdSJessica Clarke * If our bus range isn't big enough to grow the sub-allocation 213031776afdSJessica Clarke * then we need to grow our bus range. Any request that would 213131776afdSJessica Clarke * require us to decrease the start of our own bus range is 213231776afdSJessica Clarke * invalid, we can only extend the end; ignore such requests 213331776afdSJessica Clarke * and let rman_adjust_resource fail below. 213431776afdSJessica Clarke */ 213531776afdSJessica Clarke if (start >= sc->bus.sec && end > sc->bus.sub) { 213631776afdSJessica Clarke error = pcib_grow_subbus(&sc->bus, end); 213731776afdSJessica Clarke if (error != 0) 213831776afdSJessica Clarke return (error); 213931776afdSJessica Clarke } 2140e1d442a6SJessica Clarke } else { 214126245980SJessica Clarke /* 214226245980SJessica Clarke * Resource is managed and not a secondary bus number, must 214326245980SJessica Clarke * be from one of our windows. 214426245980SJessica Clarke */ 2145582b84cdSJohn Baldwin w = pcib_get_resource_window(sc, r); 214626245980SJessica Clarke KASSERT(w != NULL, 214726245980SJessica Clarke ("%s: no window for resource (%#jx-%#jx) type %d", 214826245980SJessica Clarke __func__, rman_get_start(r), rman_get_end(r), type)); 214926245980SJessica Clarke 215026245980SJessica Clarke /* 215126245980SJessica Clarke * If our window isn't big enough to grow the sub-allocation 215226245980SJessica Clarke * then we need to expand the window. 215326245980SJessica Clarke */ 215426245980SJessica Clarke if (start < w->base || end > w->limit) { 215515cb3b54SAlexander Motin wmask = ((rman_res_t)1 << w->step) - 1; 215615cb3b54SAlexander Motin error = pcib_expand_window(sc, w, type, 215715cb3b54SAlexander Motin MIN(start & ~wmask, w->base), 215815cb3b54SAlexander Motin MAX(end | wmask, w->limit)); 215926245980SJessica Clarke if (error != 0) 216026245980SJessica Clarke return (error); 216115cb3b54SAlexander Motin if (bootverbose) 216215cb3b54SAlexander Motin device_printf(sc->dev, 216315cb3b54SAlexander Motin "grew %s window to %#jx-%#jx\n", 216415cb3b54SAlexander Motin w->name, (uintmax_t)w->base, 216515cb3b54SAlexander Motin (uintmax_t)w->limit); 216615cb3b54SAlexander Motin pcib_write_windows(sc, w->mask); 216726245980SJessica Clarke } 216826245980SJessica Clarke } 216926245980SJessica Clarke 217083c41143SJohn Baldwin return (rman_adjust_resource(r, start, end)); 217183c41143SJohn Baldwin } 217283c41143SJohn Baldwin 217303719c65SJohn Baldwin static int 21749dbf5b0eSJohn Baldwin pcib_release_resource(device_t dev, device_t child, struct resource *r) 217583c41143SJohn Baldwin { 217683c41143SJohn Baldwin struct pcib_softc *sc; 217783c41143SJohn Baldwin int error; 217883c41143SJohn Baldwin 217983c41143SJohn Baldwin sc = device_get_softc(dev); 2180582b84cdSJohn Baldwin if (pcib_is_resource_managed(sc, r)) { 218183c41143SJohn Baldwin if (rman_get_flags(r) & RF_ACTIVE) { 21829dbf5b0eSJohn Baldwin error = bus_deactivate_resource(child, r); 218383c41143SJohn Baldwin if (error) 218483c41143SJohn Baldwin return (error); 218583c41143SJohn Baldwin } 218683c41143SJohn Baldwin return (rman_release_resource(r)); 218783c41143SJohn Baldwin } 21889dbf5b0eSJohn Baldwin return (bus_generic_release_resource(dev, child, r)); 218983c41143SJohn Baldwin } 2190b377ff81SJohn Baldwin 2191b377ff81SJohn Baldwin static int 21922baed46eSJohn Baldwin pcib_activate_resource(device_t dev, device_t child, struct resource *r) 2193b377ff81SJohn Baldwin { 2194b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 2195b377ff81SJohn Baldwin struct resource_map map; 21962baed46eSJohn Baldwin int error, type; 2197b377ff81SJohn Baldwin 2198582b84cdSJohn Baldwin if (!pcib_is_resource_managed(sc, r)) 21992baed46eSJohn Baldwin return (bus_generic_activate_resource(dev, child, r)); 2200b377ff81SJohn Baldwin 2201b377ff81SJohn Baldwin error = rman_activate_resource(r); 2202b377ff81SJohn Baldwin if (error != 0) 2203b377ff81SJohn Baldwin return (error); 2204b377ff81SJohn Baldwin 22052baed46eSJohn Baldwin type = rman_get_type(r); 2206b377ff81SJohn Baldwin if ((rman_get_flags(r) & RF_UNMAPPED) == 0 && 2207b377ff81SJohn Baldwin (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { 2208d77f2092SJohn Baldwin error = BUS_MAP_RESOURCE(dev, child, r, NULL, &map); 2209b377ff81SJohn Baldwin if (error != 0) { 2210b377ff81SJohn Baldwin rman_deactivate_resource(r); 2211b377ff81SJohn Baldwin return (error); 2212b377ff81SJohn Baldwin } 2213b377ff81SJohn Baldwin 2214b377ff81SJohn Baldwin rman_set_mapping(r, &map); 2215b377ff81SJohn Baldwin } 2216b377ff81SJohn Baldwin return (0); 2217b377ff81SJohn Baldwin } 2218b377ff81SJohn Baldwin 2219b377ff81SJohn Baldwin static int 22202baed46eSJohn Baldwin pcib_deactivate_resource(device_t dev, device_t child, struct resource *r) 2221b377ff81SJohn Baldwin { 2222b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 2223b377ff81SJohn Baldwin struct resource_map map; 22242baed46eSJohn Baldwin int error, type; 2225b377ff81SJohn Baldwin 2226582b84cdSJohn Baldwin if (!pcib_is_resource_managed(sc, r)) 22272baed46eSJohn Baldwin return (bus_generic_deactivate_resource(dev, child, r)); 2228b377ff81SJohn Baldwin 2229b377ff81SJohn Baldwin error = rman_deactivate_resource(r); 2230b377ff81SJohn Baldwin if (error != 0) 2231b377ff81SJohn Baldwin return (error); 2232b377ff81SJohn Baldwin 22332baed46eSJohn Baldwin type = rman_get_type(r); 2234b377ff81SJohn Baldwin if ((rman_get_flags(r) & RF_UNMAPPED) == 0 && 2235b377ff81SJohn Baldwin (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { 2236b377ff81SJohn Baldwin rman_get_mapping(r, &map); 2237d77f2092SJohn Baldwin BUS_UNMAP_RESOURCE(dev, child, r, &map); 2238b377ff81SJohn Baldwin } 2239b377ff81SJohn Baldwin return (0); 2240b377ff81SJohn Baldwin } 2241b377ff81SJohn Baldwin 2242b377ff81SJohn Baldwin static struct resource * 2243b377ff81SJohn Baldwin pcib_find_parent_resource(struct pcib_window *w, struct resource *r) 2244b377ff81SJohn Baldwin { 2245b377ff81SJohn Baldwin for (int i = 0; i < w->count; i++) { 2246b377ff81SJohn Baldwin if (rman_get_start(w->res[i]) <= rman_get_start(r) && 2247b377ff81SJohn Baldwin rman_get_end(w->res[i]) >= rman_get_end(r)) 2248b377ff81SJohn Baldwin return (w->res[i]); 2249b377ff81SJohn Baldwin } 2250b377ff81SJohn Baldwin return (NULL); 2251b377ff81SJohn Baldwin } 2252b377ff81SJohn Baldwin 2253b377ff81SJohn Baldwin static int 2254d77f2092SJohn Baldwin pcib_map_resource(device_t dev, device_t child, struct resource *r, 2255b377ff81SJohn Baldwin struct resource_map_request *argsp, struct resource_map *map) 2256b377ff81SJohn Baldwin { 2257b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 2258b377ff81SJohn Baldwin struct resource_map_request args; 2259b377ff81SJohn Baldwin struct pcib_window *w; 2260b377ff81SJohn Baldwin struct resource *pres; 2261b377ff81SJohn Baldwin rman_res_t length, start; 2262b377ff81SJohn Baldwin int error; 2263b377ff81SJohn Baldwin 2264582b84cdSJohn Baldwin w = pcib_get_resource_window(sc, r); 2265b377ff81SJohn Baldwin if (w == NULL) 2266d77f2092SJohn Baldwin return (bus_generic_map_resource(dev, child, r, argsp, map)); 2267b377ff81SJohn Baldwin 2268b377ff81SJohn Baldwin /* Resources must be active to be mapped. */ 2269b377ff81SJohn Baldwin if (!(rman_get_flags(r) & RF_ACTIVE)) 2270b377ff81SJohn Baldwin return (ENXIO); 2271b377ff81SJohn Baldwin 2272b377ff81SJohn Baldwin resource_init_map_request(&args); 2273b377ff81SJohn Baldwin error = resource_validate_map_request(r, argsp, &args, &start, &length); 2274b377ff81SJohn Baldwin if (error) 2275b377ff81SJohn Baldwin return (error); 2276b377ff81SJohn Baldwin 2277b377ff81SJohn Baldwin pres = pcib_find_parent_resource(w, r); 2278b377ff81SJohn Baldwin if (pres == NULL) 2279b377ff81SJohn Baldwin return (ENOENT); 2280b377ff81SJohn Baldwin 2281b377ff81SJohn Baldwin args.offset = start - rman_get_start(pres); 2282b377ff81SJohn Baldwin args.length = length; 228398056127SJohn Baldwin return (bus_map_resource(dev, pres, &args, map)); 2284b377ff81SJohn Baldwin } 2285b377ff81SJohn Baldwin 2286b377ff81SJohn Baldwin static int 2287d77f2092SJohn Baldwin pcib_unmap_resource(device_t dev, device_t child, struct resource *r, 2288b377ff81SJohn Baldwin struct resource_map *map) 2289b377ff81SJohn Baldwin { 2290b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 2291b377ff81SJohn Baldwin struct pcib_window *w; 229298056127SJohn Baldwin struct resource *pres; 2293b377ff81SJohn Baldwin 2294582b84cdSJohn Baldwin w = pcib_get_resource_window(sc, r); 229598056127SJohn Baldwin if (w == NULL) 2296d77f2092SJohn Baldwin return (bus_generic_unmap_resource(dev, child, r, map)); 229798056127SJohn Baldwin 229898056127SJohn Baldwin pres = pcib_find_parent_resource(w, r); 229998056127SJohn Baldwin if (pres == NULL) 230098056127SJohn Baldwin return (ENOENT); 230198056127SJohn Baldwin return (bus_unmap_resource(dev, pres, map)); 2302b377ff81SJohn Baldwin } 2303bb0d0a8eSMike Smith 2304bb0d0a8eSMike Smith /* 230555d3ea17SRyan Stone * If ARI is enabled on this downstream port, translate the function number 230655d3ea17SRyan Stone * to the non-ARI slot/function. The downstream port will convert it back in 230755d3ea17SRyan Stone * hardware. If ARI is not enabled slot and func are not modified. 230855d3ea17SRyan Stone */ 230955d3ea17SRyan Stone static __inline void 231055d3ea17SRyan Stone pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func) 231155d3ea17SRyan Stone { 231255d3ea17SRyan Stone struct pcib_softc *sc; 231355d3ea17SRyan Stone int ari_func; 231455d3ea17SRyan Stone 231555d3ea17SRyan Stone sc = device_get_softc(pcib); 231655d3ea17SRyan Stone ari_func = *func; 231755d3ea17SRyan Stone 231855d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 231955d3ea17SRyan Stone KASSERT(*slot == 0, 232055d3ea17SRyan Stone ("Non-zero slot number with ARI enabled!")); 232155d3ea17SRyan Stone *slot = PCIE_ARI_SLOT(ari_func); 232255d3ea17SRyan Stone *func = PCIE_ARI_FUNC(ari_func); 232355d3ea17SRyan Stone } 232455d3ea17SRyan Stone } 232555d3ea17SRyan Stone 232655d3ea17SRyan Stone static void 232755d3ea17SRyan Stone pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos) 232855d3ea17SRyan Stone { 232955d3ea17SRyan Stone uint32_t ctl2; 233055d3ea17SRyan Stone 233155d3ea17SRyan Stone ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4); 233255d3ea17SRyan Stone ctl2 |= PCIEM_CTL2_ARI; 233355d3ea17SRyan Stone pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4); 233455d3ea17SRyan Stone 233555d3ea17SRyan Stone sc->flags |= PCIB_ENABLE_ARI; 233655d3ea17SRyan Stone } 233755d3ea17SRyan Stone 233855d3ea17SRyan Stone /* 2339bb0d0a8eSMike Smith * PCIB interface. 2340bb0d0a8eSMike Smith */ 23416f0d5884SJohn Baldwin int 2342bb0d0a8eSMike Smith pcib_maxslots(device_t dev) 2343bb0d0a8eSMike Smith { 23445502348dSJustin Hibbits #if !defined(__amd64__) && !defined(__i386__) 23458b92ad43SJustin Hibbits uint32_t pcie_pos; 23468b92ad43SJustin Hibbits uint16_t val; 23478b92ad43SJustin Hibbits 23488b92ad43SJustin Hibbits /* 23498b92ad43SJustin Hibbits * If this is a PCIe rootport or downstream switch port, there's only 23508b92ad43SJustin Hibbits * one slot permitted. 23518b92ad43SJustin Hibbits */ 23528b92ad43SJustin Hibbits if (pci_find_cap(dev, PCIY_EXPRESS, &pcie_pos) == 0) { 23538b92ad43SJustin Hibbits val = pci_read_config(dev, pcie_pos + PCIER_FLAGS, 2); 23548b92ad43SJustin Hibbits val &= PCIEM_FLAGS_TYPE; 23558b92ad43SJustin Hibbits if (val == PCIEM_TYPE_ROOT_PORT || 23568b92ad43SJustin Hibbits val == PCIEM_TYPE_DOWNSTREAM_PORT) 23578b92ad43SJustin Hibbits return (0); 23588b92ad43SJustin Hibbits } 23595502348dSJustin Hibbits #endif 23604fa59183SMike Smith return (PCI_SLOTMAX); 2361bb0d0a8eSMike Smith } 2362bb0d0a8eSMike Smith 236355d3ea17SRyan Stone static int 236455d3ea17SRyan Stone pcib_ari_maxslots(device_t dev) 236555d3ea17SRyan Stone { 236655d3ea17SRyan Stone struct pcib_softc *sc; 236755d3ea17SRyan Stone 236855d3ea17SRyan Stone sc = device_get_softc(dev); 236955d3ea17SRyan Stone 237055d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 237155d3ea17SRyan Stone return (PCIE_ARI_SLOTMAX); 237255d3ea17SRyan Stone else 23738b92ad43SJustin Hibbits return (pcib_maxslots(dev)); 237455d3ea17SRyan Stone } 237555d3ea17SRyan Stone 237655d3ea17SRyan Stone static int 237755d3ea17SRyan Stone pcib_ari_maxfuncs(device_t dev) 237855d3ea17SRyan Stone { 237955d3ea17SRyan Stone struct pcib_softc *sc; 238055d3ea17SRyan Stone 238155d3ea17SRyan Stone sc = device_get_softc(dev); 238255d3ea17SRyan Stone 238355d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 238455d3ea17SRyan Stone return (PCIE_ARI_FUNCMAX); 238555d3ea17SRyan Stone else 238655d3ea17SRyan Stone return (PCI_FUNCMAX); 238755d3ea17SRyan Stone } 238855d3ea17SRyan Stone 23892397d2d8SRyan Stone static void 23902397d2d8SRyan Stone pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot, 23912397d2d8SRyan Stone int *func) 23922397d2d8SRyan Stone { 23932397d2d8SRyan Stone struct pcib_softc *sc; 23942397d2d8SRyan Stone 23952397d2d8SRyan Stone sc = device_get_softc(pcib); 23962397d2d8SRyan Stone 23972397d2d8SRyan Stone *bus = PCI_RID2BUS(rid); 23982397d2d8SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 23992397d2d8SRyan Stone *slot = PCIE_ARI_RID2SLOT(rid); 24002397d2d8SRyan Stone *func = PCIE_ARI_RID2FUNC(rid); 24012397d2d8SRyan Stone } else { 24022397d2d8SRyan Stone *slot = PCI_RID2SLOT(rid); 24032397d2d8SRyan Stone *func = PCI_RID2FUNC(rid); 24042397d2d8SRyan Stone } 24052397d2d8SRyan Stone } 24062397d2d8SRyan Stone 2407bb0d0a8eSMike Smith /* 2408bb0d0a8eSMike Smith * Since we are a child of a PCI bus, its parent must support the pcib interface. 2409bb0d0a8eSMike Smith */ 241055d3ea17SRyan Stone static uint32_t 2411795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width) 2412bb0d0a8eSMike Smith { 241382cb5c3bSJohn Baldwin #ifdef PCI_HP 241482cb5c3bSJohn Baldwin struct pcib_softc *sc; 241555d3ea17SRyan Stone 241682cb5c3bSJohn Baldwin sc = device_get_softc(dev); 241782cb5c3bSJohn Baldwin if (!pcib_present(sc)) { 241882cb5c3bSJohn Baldwin switch (width) { 241982cb5c3bSJohn Baldwin case 2: 242082cb5c3bSJohn Baldwin return (0xffff); 242182cb5c3bSJohn Baldwin case 1: 242282cb5c3bSJohn Baldwin return (0xff); 242382cb5c3bSJohn Baldwin default: 242482cb5c3bSJohn Baldwin return (0xffffffff); 242582cb5c3bSJohn Baldwin } 242682cb5c3bSJohn Baldwin } 242782cb5c3bSJohn Baldwin #endif 242855d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 242955d3ea17SRyan Stone return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, 243055d3ea17SRyan Stone f, reg, width)); 2431bb0d0a8eSMike Smith } 2432bb0d0a8eSMike Smith 243355d3ea17SRyan Stone static void 2434795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width) 2435bb0d0a8eSMike Smith { 243682cb5c3bSJohn Baldwin #ifdef PCI_HP 243782cb5c3bSJohn Baldwin struct pcib_softc *sc; 243855d3ea17SRyan Stone 243982cb5c3bSJohn Baldwin sc = device_get_softc(dev); 244082cb5c3bSJohn Baldwin if (!pcib_present(sc)) 244182cb5c3bSJohn Baldwin return; 244282cb5c3bSJohn Baldwin #endif 244355d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 244455d3ea17SRyan Stone PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, 244555d3ea17SRyan Stone reg, val, width); 2446bb0d0a8eSMike Smith } 2447bb0d0a8eSMike Smith 2448bb0d0a8eSMike Smith /* 2449bb0d0a8eSMike Smith * Route an interrupt across a PCI bridge. 2450bb0d0a8eSMike Smith */ 24512c2d1d07SBenno Rice int 2452bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin) 2453bb0d0a8eSMike Smith { 2454bb0d0a8eSMike Smith device_t bus; 2455bb0d0a8eSMike Smith int parent_intpin; 2456bb0d0a8eSMike Smith int intnum; 2457bb0d0a8eSMike Smith 2458bb0d0a8eSMike Smith /* 2459bb0d0a8eSMike Smith * 2460bb0d0a8eSMike Smith * The PCI standard defines a swizzle of the child-side device/intpin to 2461bb0d0a8eSMike Smith * the parent-side intpin as follows. 2462bb0d0a8eSMike Smith * 2463bb0d0a8eSMike Smith * device = device on child bus 2464bb0d0a8eSMike Smith * child_intpin = intpin on child bus slot (0-3) 2465bb0d0a8eSMike Smith * parent_intpin = intpin on parent bus slot (0-3) 2466bb0d0a8eSMike Smith * 2467bb0d0a8eSMike Smith * parent_intpin = (device + child_intpin) % 4 2468bb0d0a8eSMike Smith */ 2469cdc95e1bSBernd Walter parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4; 2470bb0d0a8eSMike Smith 2471bb0d0a8eSMike Smith /* 2472bb0d0a8eSMike Smith * Our parent is a PCI bus. Its parent must export the pcib interface 2473bb0d0a8eSMike Smith * which includes the ability to route interrupts. 2474bb0d0a8eSMike Smith */ 2475bb0d0a8eSMike Smith bus = device_get_parent(pcib); 2476bb0d0a8eSMike Smith intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1); 247739981fedSJohn Baldwin if (PCI_INTERRUPT_VALID(intnum) && bootverbose) { 2478c6a121abSJohn Baldwin device_printf(pcib, "slot %d INT%c is routed to irq %d\n", 2479c6a121abSJohn Baldwin pci_get_slot(dev), 'A' + pin - 1, intnum); 24808046c4b9SMike Smith } 2481bb0d0a8eSMike Smith return(intnum); 2482bb0d0a8eSMike Smith } 2483b173edafSJohn Baldwin 2484e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */ 24859bf4c9c1SJohn Baldwin int 24869bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs) 24879bf4c9c1SJohn Baldwin { 2488bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 24899bf4c9c1SJohn Baldwin device_t bus; 24909bf4c9c1SJohn Baldwin 249122bf1c7fSJohn Baldwin if (sc->flags & PCIB_DISABLE_MSI) 249222bf1c7fSJohn Baldwin return (ENXIO); 24939bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 24949bf4c9c1SJohn Baldwin return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 24959bf4c9c1SJohn Baldwin irqs)); 24969bf4c9c1SJohn Baldwin } 24979bf4c9c1SJohn Baldwin 2498e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */ 24999bf4c9c1SJohn Baldwin int 25009bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs) 25019bf4c9c1SJohn Baldwin { 25029bf4c9c1SJohn Baldwin device_t bus; 25039bf4c9c1SJohn Baldwin 25049bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 25059bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs)); 25069bf4c9c1SJohn Baldwin } 25079bf4c9c1SJohn Baldwin 25089bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */ 25099bf4c9c1SJohn Baldwin int 2510e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq) 25119bf4c9c1SJohn Baldwin { 2512bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 25139bf4c9c1SJohn Baldwin device_t bus; 25149bf4c9c1SJohn Baldwin 251568e9cbd3SMarius Strobl if (sc->flags & PCIB_DISABLE_MSIX) 251622bf1c7fSJohn Baldwin return (ENXIO); 25179bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 2518e706f7f0SJohn Baldwin return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq)); 25195fe82bcaSJohn Baldwin } 25205fe82bcaSJohn Baldwin 25219bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */ 25229bf4c9c1SJohn Baldwin int 25239bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq) 25249bf4c9c1SJohn Baldwin { 25259bf4c9c1SJohn Baldwin device_t bus; 25269bf4c9c1SJohn Baldwin 25279bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 25289bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq)); 25299bf4c9c1SJohn Baldwin } 25309bf4c9c1SJohn Baldwin 2531e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */ 2532e706f7f0SJohn Baldwin int 2533e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, 2534e706f7f0SJohn Baldwin uint32_t *data) 2535e706f7f0SJohn Baldwin { 2536e706f7f0SJohn Baldwin device_t bus; 25374522ac77SLuoqi Chen int error; 2538e706f7f0SJohn Baldwin 2539e706f7f0SJohn Baldwin bus = device_get_parent(pcib); 25404522ac77SLuoqi Chen error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data); 25414522ac77SLuoqi Chen if (error) 25424522ac77SLuoqi Chen return (error); 25434522ac77SLuoqi Chen 25444522ac77SLuoqi Chen pci_ht_map_msi(pcib, *addr); 25454522ac77SLuoqi Chen return (0); 2546e706f7f0SJohn Baldwin } 2547e706f7f0SJohn Baldwin 254862508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */ 254962508c53SJohn Baldwin int 255062508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate) 255162508c53SJohn Baldwin { 255262508c53SJohn Baldwin device_t bus; 255362508c53SJohn Baldwin 255462508c53SJohn Baldwin bus = device_get_parent(pcib); 255562508c53SJohn Baldwin return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate)); 255662508c53SJohn Baldwin } 25575605a99eSRyan Stone 25582397d2d8SRyan Stone static int 25592397d2d8SRyan Stone pcib_ari_enabled(device_t pcib) 25602397d2d8SRyan Stone { 25612397d2d8SRyan Stone struct pcib_softc *sc; 25622397d2d8SRyan Stone 25632397d2d8SRyan Stone sc = device_get_softc(pcib); 25642397d2d8SRyan Stone 25652397d2d8SRyan Stone return ((sc->flags & PCIB_ENABLE_ARI) != 0); 25662397d2d8SRyan Stone } 25672397d2d8SRyan Stone 2568d7be980dSAndrew Turner static int 2569d7be980dSAndrew Turner pcib_ari_get_id(device_t pcib, device_t dev, enum pci_id_type type, 2570d7be980dSAndrew Turner uintptr_t *id) 257155d3ea17SRyan Stone { 257255d3ea17SRyan Stone struct pcib_softc *sc; 25731e43b18cSAndrew Turner device_t bus_dev; 257455d3ea17SRyan Stone uint8_t bus, slot, func; 257555d3ea17SRyan Stone 25761e43b18cSAndrew Turner if (type != PCI_ID_RID) { 25771e43b18cSAndrew Turner bus_dev = device_get_parent(pcib); 25781e43b18cSAndrew Turner return (PCIB_GET_ID(device_get_parent(bus_dev), dev, type, id)); 25791e43b18cSAndrew Turner } 2580d7be980dSAndrew Turner 258155d3ea17SRyan Stone sc = device_get_softc(pcib); 258255d3ea17SRyan Stone 258355d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 258455d3ea17SRyan Stone bus = pci_get_bus(dev); 258555d3ea17SRyan Stone func = pci_get_function(dev); 258655d3ea17SRyan Stone 2587d7be980dSAndrew Turner *id = (PCI_ARI_RID(bus, func)); 258855d3ea17SRyan Stone } else { 258955d3ea17SRyan Stone bus = pci_get_bus(dev); 259055d3ea17SRyan Stone slot = pci_get_slot(dev); 259155d3ea17SRyan Stone func = pci_get_function(dev); 259255d3ea17SRyan Stone 2593d7be980dSAndrew Turner *id = (PCI_RID(bus, slot, func)); 259455d3ea17SRyan Stone } 2595d7be980dSAndrew Turner 2596d7be980dSAndrew Turner return (0); 259755d3ea17SRyan Stone } 259855d3ea17SRyan Stone 259955d3ea17SRyan Stone /* 260055d3ea17SRyan Stone * Check that the downstream port (pcib) and the endpoint device (dev) both 260155d3ea17SRyan Stone * support ARI. If so, enable it and return 0, otherwise return an error. 260255d3ea17SRyan Stone */ 260355d3ea17SRyan Stone static int 260455d3ea17SRyan Stone pcib_try_enable_ari(device_t pcib, device_t dev) 260555d3ea17SRyan Stone { 260655d3ea17SRyan Stone struct pcib_softc *sc; 260755d3ea17SRyan Stone int error; 260855d3ea17SRyan Stone uint32_t cap2; 260955d3ea17SRyan Stone int ari_cap_off; 261055d3ea17SRyan Stone uint32_t ari_ver; 261155d3ea17SRyan Stone uint32_t pcie_pos; 261255d3ea17SRyan Stone 261355d3ea17SRyan Stone sc = device_get_softc(pcib); 261455d3ea17SRyan Stone 261555d3ea17SRyan Stone /* 261655d3ea17SRyan Stone * ARI is controlled in a register in the PCIe capability structure. 261755d3ea17SRyan Stone * If the downstream port does not have the PCIe capability structure 261855d3ea17SRyan Stone * then it does not support ARI. 261955d3ea17SRyan Stone */ 262055d3ea17SRyan Stone error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos); 262155d3ea17SRyan Stone if (error != 0) 262255d3ea17SRyan Stone return (ENODEV); 262355d3ea17SRyan Stone 262455d3ea17SRyan Stone /* Check that the PCIe port advertises ARI support. */ 262555d3ea17SRyan Stone cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4); 262655d3ea17SRyan Stone if (!(cap2 & PCIEM_CAP2_ARI)) 262755d3ea17SRyan Stone return (ENODEV); 262855d3ea17SRyan Stone 262955d3ea17SRyan Stone /* 263055d3ea17SRyan Stone * Check that the endpoint device advertises ARI support via the ARI 263155d3ea17SRyan Stone * extended capability structure. 263255d3ea17SRyan Stone */ 263355d3ea17SRyan Stone error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off); 263455d3ea17SRyan Stone if (error != 0) 263555d3ea17SRyan Stone return (ENODEV); 263655d3ea17SRyan Stone 263755d3ea17SRyan Stone /* 263855d3ea17SRyan Stone * Finally, check that the endpoint device supports the same version 263955d3ea17SRyan Stone * of ARI that we do. 264055d3ea17SRyan Stone */ 264155d3ea17SRyan Stone ari_ver = pci_read_config(dev, ari_cap_off, 4); 264255d3ea17SRyan Stone if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) { 264355d3ea17SRyan Stone if (bootverbose) 264455d3ea17SRyan Stone device_printf(pcib, 264555d3ea17SRyan Stone "Unsupported version of ARI (%d) detected\n", 264655d3ea17SRyan Stone PCI_EXTCAP_VER(ari_ver)); 264755d3ea17SRyan Stone 264855d3ea17SRyan Stone return (ENXIO); 264955d3ea17SRyan Stone } 265055d3ea17SRyan Stone 265155d3ea17SRyan Stone pcib_enable_ari(sc, pcie_pos); 265255d3ea17SRyan Stone 265355d3ea17SRyan Stone return (0); 265455d3ea17SRyan Stone } 26554cb67729SWarner Losh 265628586889SWarner Losh int 265728586889SWarner Losh pcib_request_feature_allow(device_t pcib, device_t dev, 265828586889SWarner Losh enum pci_feature feature) 265928586889SWarner Losh { 266028586889SWarner Losh /* 26615914c62eSGavin Atkinson * No host firmware we have to negotiate with, so we allow 266228586889SWarner Losh * every valid feature requested. 266328586889SWarner Losh */ 266428586889SWarner Losh switch (feature) { 266528586889SWarner Losh case PCI_FEATURE_AER: 266628586889SWarner Losh case PCI_FEATURE_HP: 266728586889SWarner Losh break; 266828586889SWarner Losh default: 266928586889SWarner Losh return (EINVAL); 267028586889SWarner Losh } 267128586889SWarner Losh 267228586889SWarner Losh return (0); 267328586889SWarner Losh } 267428586889SWarner Losh 26751ffd07bdSJohn Baldwin int 26761ffd07bdSJohn Baldwin pcib_request_feature(device_t dev, enum pci_feature feature) 26771ffd07bdSJohn Baldwin { 26781ffd07bdSJohn Baldwin 26791ffd07bdSJohn Baldwin /* 26801ffd07bdSJohn Baldwin * Invoke PCIB_REQUEST_FEATURE of this bridge first in case 26811ffd07bdSJohn Baldwin * the firmware overrides the method of PCI-PCI bridges. 26821ffd07bdSJohn Baldwin */ 26831ffd07bdSJohn Baldwin return (PCIB_REQUEST_FEATURE(dev, dev, feature)); 26841ffd07bdSJohn Baldwin } 26851ffd07bdSJohn Baldwin 26864cb67729SWarner Losh /* 26874cb67729SWarner Losh * Pass the request to use this PCI feature up the tree. Either there's a 26884cb67729SWarner Losh * firmware like ACPI that's using this feature that will approve (or deny) the 26894cb67729SWarner Losh * request to take it over, or the platform has no such firmware, in which case 26904cb67729SWarner Losh * the request will be approved. If the request is approved, the OS is expected 26914cb67729SWarner Losh * to make use of the feature or render it harmless. 26924cb67729SWarner Losh */ 26934cb67729SWarner Losh static int 26941ffd07bdSJohn Baldwin pcib_request_feature_default(device_t pcib, device_t dev, 26951ffd07bdSJohn Baldwin enum pci_feature feature) 26964cb67729SWarner Losh { 26974cb67729SWarner Losh device_t bus; 26984cb67729SWarner Losh 26994cb67729SWarner Losh /* 27004cb67729SWarner Losh * Our parent is necessarily a pci bus. Its parent will either be 27014cb67729SWarner Losh * another pci bridge (which passes it up) or a host bridge that can 27024cb67729SWarner Losh * approve or reject the request. 27034cb67729SWarner Losh */ 27044cb67729SWarner Losh bus = device_get_parent(pcib); 27054cb67729SWarner Losh return (PCIB_REQUEST_FEATURE(device_get_parent(bus), dev, feature)); 27064cb67729SWarner Losh } 27075db2a4a8SKonstantin Belousov 27085db2a4a8SKonstantin Belousov static int 27095db2a4a8SKonstantin Belousov pcib_reset_child(device_t dev, device_t child, int flags) 27105db2a4a8SKonstantin Belousov { 27115db2a4a8SKonstantin Belousov struct pci_devinfo *pdinfo; 27125db2a4a8SKonstantin Belousov int error; 27135db2a4a8SKonstantin Belousov 27145db2a4a8SKonstantin Belousov error = 0; 27155db2a4a8SKonstantin Belousov if (dev == NULL || device_get_parent(child) != dev) 27165db2a4a8SKonstantin Belousov goto out; 27175db2a4a8SKonstantin Belousov error = ENXIO; 27185db2a4a8SKonstantin Belousov if (device_get_devclass(child) != devclass_find("pci")) 27195db2a4a8SKonstantin Belousov goto out; 27205db2a4a8SKonstantin Belousov pdinfo = device_get_ivars(dev); 27215db2a4a8SKonstantin Belousov if (pdinfo->cfg.pcie.pcie_location != 0 && 27225db2a4a8SKonstantin Belousov (pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_DOWNSTREAM_PORT || 27235db2a4a8SKonstantin Belousov pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_ROOT_PORT)) { 27245db2a4a8SKonstantin Belousov error = bus_helper_reset_prepare(child, flags); 27255db2a4a8SKonstantin Belousov if (error == 0) { 27265db2a4a8SKonstantin Belousov error = pcie_link_reset(dev, 27275db2a4a8SKonstantin Belousov pdinfo->cfg.pcie.pcie_location); 27285db2a4a8SKonstantin Belousov /* XXXKIB call _post even if error != 0 ? */ 27295db2a4a8SKonstantin Belousov bus_helper_reset_post(child, flags); 27305db2a4a8SKonstantin Belousov } 27315db2a4a8SKonstantin Belousov } 27325db2a4a8SKonstantin Belousov out: 27335db2a4a8SKonstantin Belousov return (error); 27345db2a4a8SKonstantin Belousov } 2735