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 80312588ce0SAndriy Gapon TASKQUEUE_DEFINE_THREAD(pci_hp); 80412588ce0SAndriy Gapon 80582cb5c3bSJohn Baldwin static void 80682cb5c3bSJohn Baldwin pcib_probe_hotplug(struct pcib_softc *sc) 80782cb5c3bSJohn Baldwin { 80882cb5c3bSJohn Baldwin device_t dev; 80937290148SEric van Gyzen uint32_t link_cap; 810991d431fSEric van Gyzen uint16_t link_sta, slot_sta; 81182cb5c3bSJohn Baldwin 81225a57bd6SJohn Baldwin if (!pci_enable_pcie_hp) 81325a57bd6SJohn Baldwin return; 81425a57bd6SJohn Baldwin 81582cb5c3bSJohn Baldwin dev = sc->dev; 81682cb5c3bSJohn Baldwin if (pci_find_cap(dev, PCIY_EXPRESS, NULL) != 0) 81782cb5c3bSJohn Baldwin return; 81882cb5c3bSJohn Baldwin 81982cb5c3bSJohn Baldwin if (!(pcie_read_config(dev, PCIER_FLAGS, 2) & PCIEM_FLAGS_SLOT)) 82082cb5c3bSJohn Baldwin return; 82182cb5c3bSJohn Baldwin 82282cb5c3bSJohn Baldwin sc->pcie_slot_cap = pcie_read_config(dev, PCIER_SLOT_CAP, 4); 82382cb5c3bSJohn Baldwin 824991d431fSEric van Gyzen if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_HPC) == 0) 8252611037cSJohn Baldwin return; 82637290148SEric van Gyzen link_cap = pcie_read_config(dev, PCIER_LINK_CAP, 4); 82737290148SEric van Gyzen if ((link_cap & PCIEM_LINK_CAP_DL_ACTIVE) == 0) 8282ffb582aSJohn Baldwin return; 8292611037cSJohn Baldwin 830991d431fSEric van Gyzen /* 831991d431fSEric van Gyzen * Some devices report that they have an MRL when they actually 832991d431fSEric van Gyzen * do not. Since they always report that the MRL is open, child 833991d431fSEric van Gyzen * devices would be ignored. Try to detect these devices and 834991d431fSEric van Gyzen * ignore their claim of HotPlug support. 835991d431fSEric van Gyzen * 836991d431fSEric van Gyzen * If there is an open MRL but the Data Link Layer is active, 837991d431fSEric van Gyzen * the MRL is not real. 838991d431fSEric van Gyzen */ 83937290148SEric van Gyzen if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) != 0) { 840991d431fSEric van Gyzen link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 841991d431fSEric van Gyzen slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 842991d431fSEric van Gyzen if ((slot_sta & PCIEM_SLOT_STA_MRLSS) != 0 && 843991d431fSEric van Gyzen (link_sta & PCIEM_LINK_STA_DL_ACTIVE) != 0) { 844991d431fSEric van Gyzen return; 845991d431fSEric van Gyzen } 846991d431fSEric van Gyzen } 847991d431fSEric van Gyzen 84828586889SWarner Losh /* 84928586889SWarner Losh * Now that we're sure we want to do hot plug, ask the 85028586889SWarner Losh * firmware, if any, if that's OK. 85128586889SWarner Losh */ 8521ffd07bdSJohn Baldwin if (pcib_request_feature(dev, PCI_FEATURE_HP) != 0) { 85328586889SWarner Losh if (bootverbose) 85428586889SWarner Losh device_printf(dev, "Unable to activate hot plug feature.\n"); 85528586889SWarner Losh return; 85628586889SWarner Losh } 85728586889SWarner Losh 85882cb5c3bSJohn Baldwin sc->flags |= PCIB_HOTPLUG; 85982cb5c3bSJohn Baldwin } 86082cb5c3bSJohn Baldwin 86182cb5c3bSJohn Baldwin /* 86282cb5c3bSJohn Baldwin * Send a HotPlug command to the slot control register. If this slot 86307454911SJohn Baldwin * uses command completion interrupts and a previous command is still 86407454911SJohn Baldwin * in progress, then the command is dropped. Once the previous 86507454911SJohn Baldwin * command completes or times out, pcib_pcie_hotplug_update() will be 86607454911SJohn Baldwin * invoked to post a new command based on the slot's state at that 86707454911SJohn Baldwin * time. 86882cb5c3bSJohn Baldwin */ 86982cb5c3bSJohn Baldwin static void 87082cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(struct pcib_softc *sc, uint16_t val, uint16_t mask) 87182cb5c3bSJohn Baldwin { 87282cb5c3bSJohn Baldwin device_t dev; 87382cb5c3bSJohn Baldwin uint16_t ctl, new; 87482cb5c3bSJohn Baldwin 87582cb5c3bSJohn Baldwin dev = sc->dev; 87682cb5c3bSJohn Baldwin 87707454911SJohn Baldwin if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) 87807454911SJohn Baldwin return; 87907454911SJohn Baldwin 88082cb5c3bSJohn Baldwin ctl = pcie_read_config(dev, PCIER_SLOT_CTL, 2); 88182cb5c3bSJohn Baldwin new = (ctl & ~mask) | val; 88207454911SJohn Baldwin if (new == ctl) 88307454911SJohn Baldwin return; 884991d431fSEric van Gyzen if (bootverbose) 885991d431fSEric van Gyzen device_printf(dev, "HotPlug command: %04x -> %04x\n", ctl, new); 88607454911SJohn Baldwin pcie_write_config(dev, PCIER_SLOT_CTL, new, 2); 8876f33eaa5SJohn Baldwin if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS) && 8886f33eaa5SJohn Baldwin (ctl & new) & PCIEM_SLOT_CTL_CCIE) { 88982cb5c3bSJohn Baldwin sc->flags |= PCIB_HOTPLUG_CMD_PENDING; 89082cb5c3bSJohn Baldwin if (!cold) 891fa3b03d3SAlexander Motin taskqueue_enqueue_timeout(taskqueue_pci_hp, 892fa3b03d3SAlexander Motin &sc->pcie_cc_task, hz); 89382cb5c3bSJohn Baldwin } 89482cb5c3bSJohn Baldwin } 89582cb5c3bSJohn Baldwin 89682cb5c3bSJohn Baldwin static void 89782cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(struct pcib_softc *sc) 89882cb5c3bSJohn Baldwin { 89982cb5c3bSJohn Baldwin device_t dev; 90082cb5c3bSJohn Baldwin 90182cb5c3bSJohn Baldwin dev = sc->dev; 90282cb5c3bSJohn Baldwin 90382cb5c3bSJohn Baldwin if (bootverbose) 90482cb5c3bSJohn Baldwin device_printf(dev, "Command Completed\n"); 90582cb5c3bSJohn Baldwin if (!(sc->flags & PCIB_HOTPLUG_CMD_PENDING)) 90682cb5c3bSJohn Baldwin return; 907fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_cc_task, NULL); 90882cb5c3bSJohn Baldwin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 9096f33eaa5SJohn Baldwin wakeup(sc); 91082cb5c3bSJohn Baldwin } 91182cb5c3bSJohn Baldwin 91282cb5c3bSJohn Baldwin /* 91382cb5c3bSJohn Baldwin * Returns true if a card is fully inserted from the user's 91482cb5c3bSJohn Baldwin * perspective. It may not yet be ready for access, but the driver 91582cb5c3bSJohn Baldwin * can now start enabling access if necessary. 91682cb5c3bSJohn Baldwin */ 91782cb5c3bSJohn Baldwin static bool 91882cb5c3bSJohn Baldwin pcib_hotplug_inserted(struct pcib_softc *sc) 91982cb5c3bSJohn Baldwin { 92082cb5c3bSJohn Baldwin 92182cb5c3bSJohn Baldwin /* Pretend the card isn't present if a detach is forced. */ 92282cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACHING) 92382cb5c3bSJohn Baldwin return (false); 92482cb5c3bSJohn Baldwin 92582cb5c3bSJohn Baldwin /* Card must be present in the slot. */ 92682cb5c3bSJohn Baldwin if ((sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS) == 0) 92782cb5c3bSJohn Baldwin return (false); 92882cb5c3bSJohn Baldwin 92982cb5c3bSJohn Baldwin /* A power fault implicitly turns off power to the slot. */ 93082cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD) 93182cb5c3bSJohn Baldwin return (false); 93282cb5c3bSJohn Baldwin 93382cb5c3bSJohn Baldwin /* If the MRL is disengaged, the slot is powered off. */ 93482cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP && 93582cb5c3bSJohn Baldwin (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS) != 0) 93682cb5c3bSJohn Baldwin return (false); 93782cb5c3bSJohn Baldwin 93882cb5c3bSJohn Baldwin return (true); 93982cb5c3bSJohn Baldwin } 94082cb5c3bSJohn Baldwin 94182cb5c3bSJohn Baldwin /* 94282cb5c3bSJohn Baldwin * Returns -1 if the card is fully inserted, powered, and ready for 94382cb5c3bSJohn Baldwin * access. Otherwise, returns 0. 94482cb5c3bSJohn Baldwin */ 94582cb5c3bSJohn Baldwin static int 94682cb5c3bSJohn Baldwin pcib_hotplug_present(struct pcib_softc *sc) 94782cb5c3bSJohn Baldwin { 94882cb5c3bSJohn Baldwin 94982cb5c3bSJohn Baldwin /* Card must be inserted. */ 95082cb5c3bSJohn Baldwin if (!pcib_hotplug_inserted(sc)) 95182cb5c3bSJohn Baldwin return (0); 95282cb5c3bSJohn Baldwin 95382cb5c3bSJohn Baldwin /* Require the Data Link Layer to be active. */ 95482cb5c3bSJohn Baldwin if (!(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE)) 95582cb5c3bSJohn Baldwin return (0); 95682cb5c3bSJohn Baldwin 95782cb5c3bSJohn Baldwin return (-1); 95882cb5c3bSJohn Baldwin } 95982cb5c3bSJohn Baldwin 960a58536b9SAlexander Motin static int pci_enable_pcie_ei = 0; 961a58536b9SAlexander Motin SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_ei, CTLFLAG_RWTUN, 962a58536b9SAlexander Motin &pci_enable_pcie_ei, 0, 963a58536b9SAlexander Motin "Enable support for PCI-express Electromechanical Interlock."); 964a58536b9SAlexander Motin 96582cb5c3bSJohn Baldwin static void 96682cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(struct pcib_softc *sc, uint16_t val, uint16_t mask, 96782cb5c3bSJohn Baldwin bool schedule_task) 96882cb5c3bSJohn Baldwin { 969a1566487SEric van Gyzen bool card_inserted, ei_engaged; 97082cb5c3bSJohn Baldwin 971991d431fSEric van Gyzen /* Clear DETACHING if Presence Detect has cleared. */ 97282cb5c3bSJohn Baldwin if ((sc->pcie_slot_sta & (PCIEM_SLOT_STA_PDC | PCIEM_SLOT_STA_PDS)) == 97382cb5c3bSJohn Baldwin PCIEM_SLOT_STA_PDC) 97482cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACHING; 97582cb5c3bSJohn Baldwin 97682cb5c3bSJohn Baldwin card_inserted = pcib_hotplug_inserted(sc); 97782cb5c3bSJohn Baldwin 97882cb5c3bSJohn Baldwin /* Turn the power indicator on if a card is inserted. */ 97982cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PIP) { 98082cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_PIC; 98182cb5c3bSJohn Baldwin if (card_inserted) 98282cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_ON; 98382cb5c3bSJohn Baldwin else if (sc->flags & PCIB_DETACH_PENDING) 98482cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_BLINK; 98582cb5c3bSJohn Baldwin else 98682cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_OFF; 98782cb5c3bSJohn Baldwin } 98882cb5c3bSJohn Baldwin 98982cb5c3bSJohn Baldwin /* Turn the power on via the Power Controller if a card is inserted. */ 99082cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) { 99182cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_PCC; 99282cb5c3bSJohn Baldwin if (card_inserted) 99382cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PC_ON; 99482cb5c3bSJohn Baldwin else 99582cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PC_OFF; 99682cb5c3bSJohn Baldwin } 99782cb5c3bSJohn Baldwin 99882cb5c3bSJohn Baldwin /* 99982cb5c3bSJohn Baldwin * If a card is inserted, enable the Electromechanical 100082cb5c3bSJohn Baldwin * Interlock. If a card is not inserted (or we are in the 100182cb5c3bSJohn Baldwin * process of detaching), disable the Electromechanical 100282cb5c3bSJohn Baldwin * Interlock. 100382cb5c3bSJohn Baldwin */ 1004a58536b9SAlexander Motin if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP) && 1005a58536b9SAlexander Motin pci_enable_pcie_ei) { 100682cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_EIC; 1007a1566487SEric van Gyzen ei_engaged = (sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS) != 0; 1008a1566487SEric van Gyzen if (card_inserted != ei_engaged) 100982cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_EIC; 101082cb5c3bSJohn Baldwin } 101182cb5c3bSJohn Baldwin 101282cb5c3bSJohn Baldwin /* 101382cb5c3bSJohn Baldwin * Start a timer to see if the Data Link Layer times out. 1014991d431fSEric van Gyzen * Note that we only start the timer if Presence Detect or MRL Sensor 101582cb5c3bSJohn Baldwin * changed on this interrupt. Stop any scheduled timer if 101682cb5c3bSJohn Baldwin * the Data Link Layer is active. 101782cb5c3bSJohn Baldwin */ 101882cb5c3bSJohn Baldwin if (card_inserted && 101982cb5c3bSJohn Baldwin !(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) && 1020991d431fSEric van Gyzen sc->pcie_slot_sta & 1021991d431fSEric van Gyzen (PCIEM_SLOT_STA_MRLSC | PCIEM_SLOT_STA_PDC)) { 102282cb5c3bSJohn Baldwin if (cold) 102382cb5c3bSJohn Baldwin device_printf(sc->dev, 102482cb5c3bSJohn Baldwin "Data Link Layer inactive\n"); 102582cb5c3bSJohn Baldwin else 1026fa3b03d3SAlexander Motin taskqueue_enqueue_timeout(taskqueue_pci_hp, 1027fa3b03d3SAlexander Motin &sc->pcie_dll_task, hz); 102882cb5c3bSJohn Baldwin } else if (sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) 1029fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_dll_task, 1030fa3b03d3SAlexander Motin NULL); 103182cb5c3bSJohn Baldwin 103282cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(sc, val, mask); 103382cb5c3bSJohn Baldwin 103482cb5c3bSJohn Baldwin /* 1035a1566487SEric van Gyzen * During attach the child "pci" device is added synchronously; 103682cb5c3bSJohn Baldwin * otherwise, the task is scheduled to manage the child 103782cb5c3bSJohn Baldwin * device. 103882cb5c3bSJohn Baldwin */ 103982cb5c3bSJohn Baldwin if (schedule_task && 104082cb5c3bSJohn Baldwin (pcib_hotplug_present(sc) != 0) != (sc->child != NULL)) 104112588ce0SAndriy Gapon taskqueue_enqueue(taskqueue_pci_hp, &sc->pcie_hp_task); 104282cb5c3bSJohn Baldwin } 104382cb5c3bSJohn Baldwin 104482cb5c3bSJohn Baldwin static void 10458a1926c5SWarner Losh pcib_pcie_intr_hotplug(void *arg) 104682cb5c3bSJohn Baldwin { 104782cb5c3bSJohn Baldwin struct pcib_softc *sc; 104882cb5c3bSJohn Baldwin device_t dev; 1049e0235fd3SColin Percival uint16_t old_slot_sta; 105082cb5c3bSJohn Baldwin 105182cb5c3bSJohn Baldwin sc = arg; 105282cb5c3bSJohn Baldwin dev = sc->dev; 105313d700adSScott Long PCIB_HP_LOCK(sc); 1054e0235fd3SColin Percival old_slot_sta = sc->pcie_slot_sta; 105582cb5c3bSJohn Baldwin sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 105682cb5c3bSJohn Baldwin 105782cb5c3bSJohn Baldwin /* Clear the events just reported. */ 105882cb5c3bSJohn Baldwin pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2); 105982cb5c3bSJohn Baldwin 1060991d431fSEric van Gyzen if (bootverbose) 1061991d431fSEric van Gyzen device_printf(dev, "HotPlug interrupt: %#x\n", 1062991d431fSEric van Gyzen sc->pcie_slot_sta); 1063991d431fSEric van Gyzen 106482cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_ABP) { 106582cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 106682cb5c3bSJohn Baldwin device_printf(dev, 106782cb5c3bSJohn Baldwin "Attention Button Pressed: Detach Cancelled\n"); 106882cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 1069fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, 1070fa3b03d3SAlexander Motin &sc->pcie_ab_task, NULL); 1071e0235fd3SColin Percival } else if (old_slot_sta & PCIEM_SLOT_STA_PDS) { 1072e0235fd3SColin Percival /* Only initiate detach sequence if device present. */ 107382cb5c3bSJohn Baldwin device_printf(dev, 107482cb5c3bSJohn Baldwin "Attention Button Pressed: Detaching in 5 seconds\n"); 107582cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACH_PENDING; 1076fa3b03d3SAlexander Motin taskqueue_enqueue_timeout(taskqueue_pci_hp, 1077fa3b03d3SAlexander Motin &sc->pcie_ab_task, 5 * hz); 107882cb5c3bSJohn Baldwin } 107982cb5c3bSJohn Baldwin } 108082cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD) 108182cb5c3bSJohn Baldwin device_printf(dev, "Power Fault Detected\n"); 108282cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSC) 108382cb5c3bSJohn Baldwin device_printf(dev, "MRL Sensor Changed to %s\n", 108482cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS ? "open" : 108582cb5c3bSJohn Baldwin "closed"); 108682cb5c3bSJohn Baldwin if (bootverbose && sc->pcie_slot_sta & PCIEM_SLOT_STA_PDC) 1087991d431fSEric van Gyzen device_printf(dev, "Presence Detect Changed to %s\n", 108882cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS ? "card present" : 108982cb5c3bSJohn Baldwin "empty"); 109082cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_CC) 109182cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(sc); 109282cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_DLLSC) { 109382cb5c3bSJohn Baldwin sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 109482cb5c3bSJohn Baldwin if (bootverbose) 109582cb5c3bSJohn Baldwin device_printf(dev, 109682cb5c3bSJohn Baldwin "Data Link Layer State Changed to %s\n", 109782cb5c3bSJohn Baldwin sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE ? 109882cb5c3bSJohn Baldwin "active" : "inactive"); 109982cb5c3bSJohn Baldwin } 110082cb5c3bSJohn Baldwin 110182cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 110213d700adSScott Long PCIB_HP_UNLOCK(sc); 110382cb5c3bSJohn Baldwin } 110482cb5c3bSJohn Baldwin 110582cb5c3bSJohn Baldwin static void 110682cb5c3bSJohn Baldwin pcib_pcie_hotplug_task(void *context, int pending) 110782cb5c3bSJohn Baldwin { 110882cb5c3bSJohn Baldwin struct pcib_softc *sc; 110982cb5c3bSJohn Baldwin device_t dev; 111082cb5c3bSJohn Baldwin 111182cb5c3bSJohn Baldwin sc = context; 111213d700adSScott Long PCIB_HP_LOCK(sc); 111382cb5c3bSJohn Baldwin dev = sc->dev; 111482cb5c3bSJohn Baldwin if (pcib_hotplug_present(sc) != 0) { 111582cb5c3bSJohn Baldwin if (sc->child == NULL) { 11165b56413dSWarner Losh sc->child = device_add_child(dev, "pci", DEVICE_UNIT_ANY); 1117*18250ec6SJohn Baldwin bus_attach_children(dev); 111882cb5c3bSJohn Baldwin } 111982cb5c3bSJohn Baldwin } else { 112082cb5c3bSJohn Baldwin if (sc->child != NULL) { 112182cb5c3bSJohn Baldwin if (device_delete_child(dev, sc->child) == 0) 112282cb5c3bSJohn Baldwin sc->child = NULL; 112382cb5c3bSJohn Baldwin } 112482cb5c3bSJohn Baldwin } 112513d700adSScott Long PCIB_HP_UNLOCK(sc); 112682cb5c3bSJohn Baldwin } 112782cb5c3bSJohn Baldwin 112882cb5c3bSJohn Baldwin static void 1129fa3b03d3SAlexander Motin pcib_pcie_ab_timeout(void *arg, int pending) 113082cb5c3bSJohn Baldwin { 1131fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 113282cb5c3bSJohn Baldwin 1133fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 113482cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 113582cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING; 113682cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 113782cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 113882cb5c3bSJohn Baldwin } 1139fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 114082cb5c3bSJohn Baldwin } 114182cb5c3bSJohn Baldwin 114282cb5c3bSJohn Baldwin static void 1143fa3b03d3SAlexander Motin pcib_pcie_cc_timeout(void *arg, int pending) 114482cb5c3bSJohn Baldwin { 1145fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 1146fa3b03d3SAlexander Motin device_t dev = sc->dev; 11476f33eaa5SJohn Baldwin uint16_t sta; 114882cb5c3bSJohn Baldwin 1149fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 11506f33eaa5SJohn Baldwin sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 11516f33eaa5SJohn Baldwin if (!(sta & PCIEM_SLOT_STA_CC)) { 115221e51c82SAlexander Motin device_printf(dev, "HotPlug Command Timed Out\n"); 115321e51c82SAlexander Motin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 11546f33eaa5SJohn Baldwin } else { 11556f33eaa5SJohn Baldwin device_printf(dev, 11566f33eaa5SJohn Baldwin "Missed HotPlug interrupt waiting for Command Completion\n"); 11578a1926c5SWarner Losh pcib_pcie_intr_hotplug(sc); 115882cb5c3bSJohn Baldwin } 1159fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 116082cb5c3bSJohn Baldwin } 116182cb5c3bSJohn Baldwin 116282cb5c3bSJohn Baldwin static void 1163fa3b03d3SAlexander Motin pcib_pcie_dll_timeout(void *arg, int pending) 116482cb5c3bSJohn Baldwin { 1165fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 1166fa3b03d3SAlexander Motin device_t dev = sc->dev; 116782cb5c3bSJohn Baldwin uint16_t sta; 116882cb5c3bSJohn Baldwin 1169fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 117082cb5c3bSJohn Baldwin sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 117182cb5c3bSJohn Baldwin if (!(sta & PCIEM_LINK_STA_DL_ACTIVE)) { 117282cb5c3bSJohn Baldwin device_printf(dev, 117382cb5c3bSJohn Baldwin "Timed out waiting for Data Link Layer Active\n"); 117482cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING; 117582cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 117682cb5c3bSJohn Baldwin } else if (sta != sc->pcie_link_sta) { 117782cb5c3bSJohn Baldwin device_printf(dev, 117882cb5c3bSJohn Baldwin "Missed HotPlug interrupt waiting for DLL Active\n"); 11798a1926c5SWarner Losh pcib_pcie_intr_hotplug(sc); 118082cb5c3bSJohn Baldwin } 1181fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 118282cb5c3bSJohn Baldwin } 118382cb5c3bSJohn Baldwin 118482cb5c3bSJohn Baldwin static int 118582cb5c3bSJohn Baldwin pcib_alloc_pcie_irq(struct pcib_softc *sc) 118682cb5c3bSJohn Baldwin { 118782cb5c3bSJohn Baldwin device_t dev; 1188e6b83836SJohn Baldwin int count, error, mem_rid, rid; 118982cb5c3bSJohn Baldwin 119082cb5c3bSJohn Baldwin rid = -1; 119182cb5c3bSJohn Baldwin dev = sc->dev; 119282cb5c3bSJohn Baldwin 119382cb5c3bSJohn Baldwin /* 119482cb5c3bSJohn Baldwin * For simplicity, only use MSI-X if there is a single message. 119582cb5c3bSJohn Baldwin * To support a device with multiple messages we would have to 119682cb5c3bSJohn Baldwin * use remap intr if the MSI number is not 0. 119782cb5c3bSJohn Baldwin */ 119882cb5c3bSJohn Baldwin count = pci_msix_count(dev); 119982cb5c3bSJohn Baldwin if (count == 1) { 1200e6b83836SJohn Baldwin mem_rid = pci_msix_table_bar(dev); 1201e6b83836SJohn Baldwin sc->pcie_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 1202e6b83836SJohn Baldwin &mem_rid, RF_ACTIVE); 1203e6b83836SJohn Baldwin if (sc->pcie_mem == NULL) { 1204e6b83836SJohn Baldwin device_printf(dev, 1205e6b83836SJohn Baldwin "Failed to allocate BAR for MSI-X table\n"); 1206e6b83836SJohn Baldwin } else { 120782cb5c3bSJohn Baldwin error = pci_alloc_msix(dev, &count); 120882cb5c3bSJohn Baldwin if (error == 0) 120982cb5c3bSJohn Baldwin rid = 1; 121082cb5c3bSJohn Baldwin } 1211e6b83836SJohn Baldwin } 121282cb5c3bSJohn Baldwin 121382cb5c3bSJohn Baldwin if (rid < 0 && pci_msi_count(dev) > 0) { 121482cb5c3bSJohn Baldwin count = 1; 121582cb5c3bSJohn Baldwin error = pci_alloc_msi(dev, &count); 121682cb5c3bSJohn Baldwin if (error == 0) 121782cb5c3bSJohn Baldwin rid = 1; 121882cb5c3bSJohn Baldwin } 121982cb5c3bSJohn Baldwin 122082cb5c3bSJohn Baldwin if (rid < 0) 122182cb5c3bSJohn Baldwin rid = 0; 122282cb5c3bSJohn Baldwin 122382cb5c3bSJohn Baldwin sc->pcie_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1224f14f0051SChuck Tuffli RF_ACTIVE | RF_SHAREABLE); 122582cb5c3bSJohn Baldwin if (sc->pcie_irq == NULL) { 122682cb5c3bSJohn Baldwin device_printf(dev, 122782cb5c3bSJohn Baldwin "Failed to allocate interrupt for PCI-e events\n"); 122882cb5c3bSJohn Baldwin if (rid > 0) 122982cb5c3bSJohn Baldwin pci_release_msi(dev); 123082cb5c3bSJohn Baldwin return (ENXIO); 123182cb5c3bSJohn Baldwin } 123282cb5c3bSJohn Baldwin 123313d700adSScott Long error = bus_setup_intr(dev, sc->pcie_irq, INTR_TYPE_MISC|INTR_MPSAFE, 12348a1926c5SWarner Losh NULL, pcib_pcie_intr_hotplug, sc, &sc->pcie_ihand); 123582cb5c3bSJohn Baldwin if (error) { 123682cb5c3bSJohn Baldwin device_printf(dev, "Failed to setup PCI-e interrupt handler\n"); 123782cb5c3bSJohn Baldwin bus_release_resource(dev, SYS_RES_IRQ, rid, sc->pcie_irq); 123882cb5c3bSJohn Baldwin if (rid > 0) 123982cb5c3bSJohn Baldwin pci_release_msi(dev); 124082cb5c3bSJohn Baldwin return (error); 124182cb5c3bSJohn Baldwin } 124282cb5c3bSJohn Baldwin return (0); 124382cb5c3bSJohn Baldwin } 124482cb5c3bSJohn Baldwin 12456f33eaa5SJohn Baldwin static int 12466f33eaa5SJohn Baldwin pcib_release_pcie_irq(struct pcib_softc *sc) 12476f33eaa5SJohn Baldwin { 12486f33eaa5SJohn Baldwin device_t dev; 12496f33eaa5SJohn Baldwin int error; 12506f33eaa5SJohn Baldwin 12516f33eaa5SJohn Baldwin dev = sc->dev; 12526f33eaa5SJohn Baldwin error = bus_teardown_intr(dev, sc->pcie_irq, sc->pcie_ihand); 12536f33eaa5SJohn Baldwin if (error) 12546f33eaa5SJohn Baldwin return (error); 12556f33eaa5SJohn Baldwin error = bus_free_resource(dev, SYS_RES_IRQ, sc->pcie_irq); 12566f33eaa5SJohn Baldwin if (error) 12576f33eaa5SJohn Baldwin return (error); 1258e6b83836SJohn Baldwin error = pci_release_msi(dev); 1259e6b83836SJohn Baldwin if (error) 1260e6b83836SJohn Baldwin return (error); 1261e6b83836SJohn Baldwin if (sc->pcie_mem != NULL) 1262e6b83836SJohn Baldwin error = bus_free_resource(dev, SYS_RES_MEMORY, sc->pcie_mem); 1263e6b83836SJohn Baldwin return (error); 12646f33eaa5SJohn Baldwin } 12656f33eaa5SJohn Baldwin 126682cb5c3bSJohn Baldwin static void 126782cb5c3bSJohn Baldwin pcib_setup_hotplug(struct pcib_softc *sc) 126882cb5c3bSJohn Baldwin { 126982cb5c3bSJohn Baldwin device_t dev; 127082cb5c3bSJohn Baldwin uint16_t mask, val; 127182cb5c3bSJohn Baldwin 127282cb5c3bSJohn Baldwin dev = sc->dev; 127382cb5c3bSJohn Baldwin TASK_INIT(&sc->pcie_hp_task, 0, pcib_pcie_hotplug_task, sc); 1274fa3b03d3SAlexander Motin TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_ab_task, 0, 1275fa3b03d3SAlexander Motin pcib_pcie_ab_timeout, sc); 1276fa3b03d3SAlexander Motin TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_cc_task, 0, 1277fa3b03d3SAlexander Motin pcib_pcie_cc_timeout, sc); 1278fa3b03d3SAlexander Motin TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_dll_task, 0, 1279fa3b03d3SAlexander Motin pcib_pcie_dll_timeout, sc); 1280c6df6f53SWarner Losh sc->pcie_hp_lock = bus_topo_mtx(); 128182cb5c3bSJohn Baldwin 128282cb5c3bSJohn Baldwin /* Allocate IRQ. */ 128382cb5c3bSJohn Baldwin if (pcib_alloc_pcie_irq(sc) != 0) 128482cb5c3bSJohn Baldwin return; 128582cb5c3bSJohn Baldwin 128682cb5c3bSJohn Baldwin sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 128782cb5c3bSJohn Baldwin sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 128882cb5c3bSJohn Baldwin 12896f33eaa5SJohn Baldwin /* Clear any events previously pending. */ 12906f33eaa5SJohn Baldwin pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2); 12916f33eaa5SJohn Baldwin 129282cb5c3bSJohn Baldwin /* Enable HotPlug events. */ 129382cb5c3bSJohn Baldwin mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | 129482cb5c3bSJohn Baldwin PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE | 129582cb5c3bSJohn Baldwin PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE; 129637290148SEric van Gyzen val = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | PCIEM_SLOT_CTL_PDCE; 129782cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_APB) 129882cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_ABPE; 129982cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) 130082cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PFDE; 130182cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) 130282cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_MRLSCE; 130382cb5c3bSJohn Baldwin if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS)) 130482cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_CCIE; 130582cb5c3bSJohn Baldwin 130682cb5c3bSJohn Baldwin /* Turn the attention indicator off. */ 130782cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) { 130882cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_AIC; 130982cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_AI_OFF; 131082cb5c3bSJohn Baldwin } 131182cb5c3bSJohn Baldwin 131282cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, val, mask, false); 131382cb5c3bSJohn Baldwin } 13146f33eaa5SJohn Baldwin 13156f33eaa5SJohn Baldwin static int 13166f33eaa5SJohn Baldwin pcib_detach_hotplug(struct pcib_softc *sc) 13176f33eaa5SJohn Baldwin { 13186f33eaa5SJohn Baldwin uint16_t mask, val; 13196f33eaa5SJohn Baldwin int error; 13206f33eaa5SJohn Baldwin 13216f33eaa5SJohn Baldwin /* Disable the card in the slot and force it to detach. */ 13226f33eaa5SJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 13236f33eaa5SJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 1324fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_ab_task, 1325fa3b03d3SAlexander Motin NULL); 13266f33eaa5SJohn Baldwin } 13276f33eaa5SJohn Baldwin sc->flags |= PCIB_DETACHING; 13286f33eaa5SJohn Baldwin 13296f33eaa5SJohn Baldwin if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) { 1330fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_cc_task, 1331fa3b03d3SAlexander Motin NULL); 13326f33eaa5SJohn Baldwin tsleep(sc, 0, "hpcmd", hz); 13336f33eaa5SJohn Baldwin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 13346f33eaa5SJohn Baldwin } 13356f33eaa5SJohn Baldwin 13366f33eaa5SJohn Baldwin /* Disable HotPlug events. */ 13376f33eaa5SJohn Baldwin mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | 13386f33eaa5SJohn Baldwin PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE | 13396f33eaa5SJohn Baldwin PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE; 13406f33eaa5SJohn Baldwin val = 0; 13416f33eaa5SJohn Baldwin 13426f33eaa5SJohn Baldwin /* Turn the attention indicator off. */ 13436f33eaa5SJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) { 13446f33eaa5SJohn Baldwin mask |= PCIEM_SLOT_CTL_AIC; 13456f33eaa5SJohn Baldwin val |= PCIEM_SLOT_CTL_AI_OFF; 13466f33eaa5SJohn Baldwin } 13476f33eaa5SJohn Baldwin 13486f33eaa5SJohn Baldwin pcib_pcie_hotplug_update(sc, val, mask, false); 13496f33eaa5SJohn Baldwin 13506f33eaa5SJohn Baldwin error = pcib_release_pcie_irq(sc); 13516f33eaa5SJohn Baldwin if (error) 13526f33eaa5SJohn Baldwin return (error); 135312588ce0SAndriy Gapon taskqueue_drain(taskqueue_pci_hp, &sc->pcie_hp_task); 1354fa3b03d3SAlexander Motin taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_ab_task); 1355fa3b03d3SAlexander Motin taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_cc_task); 1356fa3b03d3SAlexander Motin taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_dll_task); 13576f33eaa5SJohn Baldwin return (0); 13586f33eaa5SJohn Baldwin } 135982cb5c3bSJohn Baldwin #endif 136082cb5c3bSJohn Baldwin 1361e36af292SJung-uk Kim /* 1362e36af292SJung-uk Kim * Restore previous bridge configuration. 1363e36af292SJung-uk Kim */ 1364e36af292SJung-uk Kim static void 1365e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc) 1366e36af292SJung-uk Kim { 136783c41143SJohn Baldwin pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM); 1368e36af292SJung-uk Kim } 1369e36af292SJung-uk Kim 1370e36af292SJung-uk Kim /* 1371bb0d0a8eSMike Smith * Generic device interface 1372bb0d0a8eSMike Smith */ 1373bb0d0a8eSMike Smith static int 1374bb0d0a8eSMike Smith pcib_probe(device_t dev) 1375bb0d0a8eSMike Smith { 1376bb0d0a8eSMike Smith if ((pci_get_class(dev) == PCIC_BRIDGE) && 1377bb0d0a8eSMike Smith (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) { 1378bb0d0a8eSMike Smith device_set_desc(dev, "PCI-PCI bridge"); 1379b7cbd25bSMarcel Moolenaar return(-10000); 1380bb0d0a8eSMike Smith } 1381bb0d0a8eSMike Smith return(ENXIO); 1382bb0d0a8eSMike Smith } 1383bb0d0a8eSMike Smith 13846f0d5884SJohn Baldwin void 13856f0d5884SJohn Baldwin pcib_attach_common(device_t dev) 1386bb0d0a8eSMike Smith { 1387bb0d0a8eSMike Smith struct pcib_softc *sc; 1388abf07f13SWarner Losh struct sysctl_ctx_list *sctx; 1389abf07f13SWarner Losh struct sysctl_oid *soid; 1390c825d4dcSJohn Baldwin int comma; 1391bb0d0a8eSMike Smith 1392bb0d0a8eSMike Smith sc = device_get_softc(dev); 1393bb0d0a8eSMike Smith sc->dev = dev; 1394bb0d0a8eSMike Smith 13954fa59183SMike Smith /* 13964fa59183SMike Smith * Get current bridge configuration. 13974fa59183SMike Smith */ 139855aaf894SMarius Strobl sc->domain = pci_get_domain(dev); 1399ad6f36f8SJohn Baldwin sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 14004fa59183SMike Smith 14014fa59183SMike Smith /* 14024edef187SJohn Baldwin * The primary bus register should always be the bus of the 14034edef187SJohn Baldwin * parent. 14044edef187SJohn Baldwin */ 14054edef187SJohn Baldwin sc->pribus = pci_get_bus(dev); 14064edef187SJohn Baldwin pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1); 14074edef187SJohn Baldwin 14084edef187SJohn Baldwin /* 1409abf07f13SWarner Losh * Setup sysctl reporting nodes 1410abf07f13SWarner Losh */ 1411abf07f13SWarner Losh sctx = device_get_sysctl_ctx(dev); 1412abf07f13SWarner Losh soid = device_get_sysctl_tree(dev); 1413abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain", 1414abf07f13SWarner Losh CTLFLAG_RD, &sc->domain, 0, "Domain number"); 1415abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus", 1416abf07f13SWarner Losh CTLFLAG_RD, &sc->pribus, 0, "Primary bus number"); 1417abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus", 14184edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number"); 1419abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus", 14204edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number"); 1421abf07f13SWarner Losh 1422abf07f13SWarner Losh /* 14234fa59183SMike Smith * Quirk handling. 14244fa59183SMike Smith */ 14254fa59183SMike Smith switch (pci_get_devid(dev)) { 1426e4b59fc5SWarner Losh /* 1427e4b59fc5SWarner Losh * The i82380FB mobile docking controller is a PCI-PCI bridge, 1428e4b59fc5SWarner Losh * and it is a subtractive bridge. However, the ProgIf is wrong 1429e4b59fc5SWarner Losh * so the normal setting of PCIB_SUBTRACTIVE bit doesn't 14304718610dSZbigniew Bodek * happen. There are also Toshiba and Cavium ThunderX bridges 14314718610dSZbigniew Bodek * that behave this way. 1432e4b59fc5SWarner Losh */ 14334718610dSZbigniew Bodek case 0xa002177d: /* Cavium ThunderX */ 1434e4b59fc5SWarner Losh case 0x124b8086: /* Intel 82380FB Mobile */ 1435e4b59fc5SWarner Losh case 0x060513d7: /* Toshiba ???? */ 1436e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 1437e4b59fc5SWarner Losh break; 1438e4b59fc5SWarner Losh } 1439e4b59fc5SWarner Losh 144022bf1c7fSJohn Baldwin if (pci_msi_device_blacklisted(dev)) 144122bf1c7fSJohn Baldwin sc->flags |= PCIB_DISABLE_MSI; 144222bf1c7fSJohn Baldwin 144368e9cbd3SMarius Strobl if (pci_msix_device_blacklisted(dev)) 144468e9cbd3SMarius Strobl sc->flags |= PCIB_DISABLE_MSIX; 144568e9cbd3SMarius Strobl 1446e4b59fc5SWarner Losh /* 1447e4b59fc5SWarner Losh * Intel 815, 845 and other chipsets say they are PCI-PCI bridges, 1448e4b59fc5SWarner Losh * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM, 1449e4b59fc5SWarner Losh * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese. 1450e4b59fc5SWarner Losh * This means they act as if they were subtractively decoding 1451e4b59fc5SWarner Losh * bridges and pass all transactions. Mark them and real ProgIf 1 1452e4b59fc5SWarner Losh * parts as subtractive. 1453e4b59fc5SWarner Losh */ 1454e4b59fc5SWarner Losh if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 || 1455657d9f9fSJohn Baldwin pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE) 1456e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 1457e4b59fc5SWarner Losh 145882cb5c3bSJohn Baldwin #ifdef PCI_HP 145982cb5c3bSJohn Baldwin pcib_probe_hotplug(sc); 146082cb5c3bSJohn Baldwin #endif 14614edef187SJohn Baldwin pcib_setup_secbus(dev, &sc->bus, 1); 146283c41143SJohn Baldwin pcib_probe_windows(sc); 146382cb5c3bSJohn Baldwin #ifdef PCI_HP 146482cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG) 146582cb5c3bSJohn Baldwin pcib_setup_hotplug(sc); 146682cb5c3bSJohn Baldwin #endif 1467bb0d0a8eSMike Smith if (bootverbose) { 146855aaf894SMarius Strobl device_printf(dev, " domain %d\n", sc->domain); 14694edef187SJohn Baldwin device_printf(dev, " secondary bus %d\n", sc->bus.sec); 14704edef187SJohn Baldwin device_printf(dev, " subordinate bus %d\n", sc->bus.sub); 147183c41143SJohn Baldwin if (pcib_is_window_open(&sc->io)) 147283c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%jx-0x%jx\n", 147383c41143SJohn Baldwin (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit); 147483c41143SJohn Baldwin if (pcib_is_window_open(&sc->mem)) 147583c41143SJohn Baldwin device_printf(dev, " memory decode 0x%jx-0x%jx\n", 147683c41143SJohn Baldwin (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit); 147783c41143SJohn Baldwin if (pcib_is_window_open(&sc->pmem)) 147883c41143SJohn Baldwin device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 147983c41143SJohn Baldwin (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit); 1480c825d4dcSJohn Baldwin if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) || 1481c825d4dcSJohn Baldwin sc->flags & PCIB_SUBTRACTIVE) { 1482c825d4dcSJohn Baldwin device_printf(dev, " special decode "); 1483c825d4dcSJohn Baldwin comma = 0; 1484c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) { 1485c825d4dcSJohn Baldwin printf("ISA"); 1486c825d4dcSJohn Baldwin comma = 1; 1487c825d4dcSJohn Baldwin } 1488c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) { 1489c825d4dcSJohn Baldwin printf("%sVGA", comma ? ", " : ""); 1490c825d4dcSJohn Baldwin comma = 1; 1491c825d4dcSJohn Baldwin } 1492e4b59fc5SWarner Losh if (sc->flags & PCIB_SUBTRACTIVE) 1493c825d4dcSJohn Baldwin printf("%ssubtractive", comma ? ", " : ""); 1494c825d4dcSJohn Baldwin printf("\n"); 1495c825d4dcSJohn Baldwin } 1496bb0d0a8eSMike Smith } 1497bb0d0a8eSMike Smith 1498bb0d0a8eSMike Smith /* 1499ef888152SJohn Baldwin * Always enable busmastering on bridges so that transactions 1500ef888152SJohn Baldwin * initiated on the secondary bus are passed through to the 1501ef888152SJohn Baldwin * primary bus. 1502ef888152SJohn Baldwin */ 1503ef888152SJohn Baldwin pci_enable_busmaster(dev); 15046f0d5884SJohn Baldwin } 1505bb0d0a8eSMike Smith 150682cb5c3bSJohn Baldwin #ifdef PCI_HP 150782cb5c3bSJohn Baldwin static int 150882cb5c3bSJohn Baldwin pcib_present(struct pcib_softc *sc) 150982cb5c3bSJohn Baldwin { 151082cb5c3bSJohn Baldwin 151182cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG) 151282cb5c3bSJohn Baldwin return (pcib_hotplug_present(sc) != 0); 151382cb5c3bSJohn Baldwin return (1); 151482cb5c3bSJohn Baldwin } 151582cb5c3bSJohn Baldwin #endif 151682cb5c3bSJohn Baldwin 151738906aedSJohn Baldwin int 151867e7d085SJohn Baldwin pcib_attach_child(device_t dev) 15196f0d5884SJohn Baldwin { 15206f0d5884SJohn Baldwin struct pcib_softc *sc; 15216f0d5884SJohn Baldwin 15226f0d5884SJohn Baldwin sc = device_get_softc(dev); 152367e7d085SJohn Baldwin if (sc->bus.sec == 0) { 152467e7d085SJohn Baldwin /* no secondary bus; we should have fixed this */ 152567e7d085SJohn Baldwin return(0); 152667e7d085SJohn Baldwin } 152767e7d085SJohn Baldwin 152882cb5c3bSJohn Baldwin #ifdef PCI_HP 152982cb5c3bSJohn Baldwin if (!pcib_present(sc)) { 153082cb5c3bSJohn Baldwin /* An empty HotPlug slot, so don't add a PCI bus yet. */ 153182cb5c3bSJohn Baldwin return (0); 153282cb5c3bSJohn Baldwin } 153382cb5c3bSJohn Baldwin #endif 153482cb5c3bSJohn Baldwin 15355b56413dSWarner Losh sc->child = device_add_child(dev, "pci", DEVICE_UNIT_ANY); 1536*18250ec6SJohn Baldwin bus_attach_children(dev); 1537*18250ec6SJohn Baldwin return (0); 1538bb0d0a8eSMike Smith } 1539bb0d0a8eSMike Smith 154067e7d085SJohn Baldwin int 154167e7d085SJohn Baldwin pcib_attach(device_t dev) 154267e7d085SJohn Baldwin { 154367e7d085SJohn Baldwin 154467e7d085SJohn Baldwin pcib_attach_common(dev); 154567e7d085SJohn Baldwin return (pcib_attach_child(dev)); 1546bb0d0a8eSMike Smith } 1547bb0d0a8eSMike Smith 15486f0d5884SJohn Baldwin int 15496f33eaa5SJohn Baldwin pcib_detach(device_t dev) 15506f33eaa5SJohn Baldwin { 15516f33eaa5SJohn Baldwin struct pcib_softc *sc; 15526f33eaa5SJohn Baldwin int error; 15536f33eaa5SJohn Baldwin 15546f33eaa5SJohn Baldwin sc = device_get_softc(dev); 15556f33eaa5SJohn Baldwin error = bus_generic_detach(dev); 15566f33eaa5SJohn Baldwin if (error) 15576f33eaa5SJohn Baldwin return (error); 15586f33eaa5SJohn Baldwin #ifdef PCI_HP 15596f33eaa5SJohn Baldwin if (sc->flags & PCIB_HOTPLUG) { 15606f33eaa5SJohn Baldwin error = pcib_detach_hotplug(sc); 15616f33eaa5SJohn Baldwin if (error) 15626f33eaa5SJohn Baldwin return (error); 15636f33eaa5SJohn Baldwin } 15646f33eaa5SJohn Baldwin #endif 15656f33eaa5SJohn Baldwin error = device_delete_children(dev); 15666f33eaa5SJohn Baldwin if (error) 15676f33eaa5SJohn Baldwin return (error); 15686f33eaa5SJohn Baldwin pcib_free_windows(sc); 15696f33eaa5SJohn Baldwin pcib_free_secbus(dev, &sc->bus); 15706f33eaa5SJohn Baldwin return (0); 15716f33eaa5SJohn Baldwin } 15726f33eaa5SJohn Baldwin 15736f33eaa5SJohn Baldwin int 1574e36af292SJung-uk Kim pcib_resume(device_t dev) 1575e36af292SJung-uk Kim { 1576e36af292SJung-uk Kim 1577e36af292SJung-uk Kim pcib_cfg_restore(device_get_softc(dev)); 1578cffd37daSAndriy Gapon 1579cffd37daSAndriy Gapon /* 1580cffd37daSAndriy Gapon * Restore the Command register only after restoring the windows. 1581cffd37daSAndriy Gapon * The bridge should not be claiming random windows. 1582cffd37daSAndriy Gapon */ 1583cffd37daSAndriy Gapon pci_write_config(dev, PCIR_COMMAND, pci_get_cmdreg(dev), 2); 1584e36af292SJung-uk Kim return (bus_generic_resume(dev)); 1585e36af292SJung-uk Kim } 1586e36af292SJung-uk Kim 1587809923caSJustin Hibbits void 1588809923caSJustin Hibbits pcib_bridge_init(device_t dev) 1589809923caSJustin Hibbits { 1590809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 1591809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2); 1592809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1); 1593809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2); 1594809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2); 1595809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2); 1596809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 1597809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4); 1598809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2); 1599809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4); 1600809923caSJustin Hibbits } 1601809923caSJustin Hibbits 1602e36af292SJung-uk Kim int 160382cb5c3bSJohn Baldwin pcib_child_present(device_t dev, device_t child) 160482cb5c3bSJohn Baldwin { 160582cb5c3bSJohn Baldwin #ifdef PCI_HP 160682cb5c3bSJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 160782cb5c3bSJohn Baldwin int retval; 160882cb5c3bSJohn Baldwin 160982cb5c3bSJohn Baldwin retval = bus_child_present(dev); 161082cb5c3bSJohn Baldwin if (retval != 0 && sc->flags & PCIB_HOTPLUG) 161182cb5c3bSJohn Baldwin retval = pcib_hotplug_present(sc); 161282cb5c3bSJohn Baldwin return (retval); 161382cb5c3bSJohn Baldwin #else 161482cb5c3bSJohn Baldwin return (bus_child_present(dev)); 161582cb5c3bSJohn Baldwin #endif 161682cb5c3bSJohn Baldwin } 161782cb5c3bSJohn Baldwin 161882cb5c3bSJohn Baldwin int 1619bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 1620bb0d0a8eSMike Smith { 1621bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 1622bb0d0a8eSMike Smith 1623bb0d0a8eSMike Smith switch (which) { 162455aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 162555aaf894SMarius Strobl *result = sc->domain; 162655aaf894SMarius Strobl return(0); 1627bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 16284edef187SJohn Baldwin *result = sc->bus.sec; 1629bb0d0a8eSMike Smith return(0); 1630bb0d0a8eSMike Smith } 1631bb0d0a8eSMike Smith return(ENOENT); 1632bb0d0a8eSMike Smith } 1633bb0d0a8eSMike Smith 16346f0d5884SJohn Baldwin int 1635bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 1636bb0d0a8eSMike Smith { 1637bb0d0a8eSMike Smith 1638bb0d0a8eSMike Smith switch (which) { 163955aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 164055aaf894SMarius Strobl return(EINVAL); 1641bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 16424edef187SJohn Baldwin return(EINVAL); 1643bb0d0a8eSMike Smith } 1644bb0d0a8eSMike Smith return(ENOENT); 1645bb0d0a8eSMike Smith } 1646bb0d0a8eSMike Smith 164783c41143SJohn Baldwin /* 164883c41143SJohn Baldwin * Attempt to allocate a resource from the existing resources assigned 164983c41143SJohn Baldwin * to a window. 165083c41143SJohn Baldwin */ 165183c41143SJohn Baldwin static struct resource * 165283c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w, 16532dd1bdf1SJustin Hibbits device_t child, int type, int *rid, rman_res_t start, rman_res_t end, 16542dd1bdf1SJustin Hibbits rman_res_t count, u_int flags) 165583c41143SJohn Baldwin { 165683c41143SJohn Baldwin struct resource *res; 165783c41143SJohn Baldwin 165883c41143SJohn Baldwin if (!pcib_is_window_open(w)) 165983c41143SJohn Baldwin return (NULL); 166083c41143SJohn Baldwin 166183c41143SJohn Baldwin res = rman_reserve_resource(&w->rman, start, end, count, 166283c41143SJohn Baldwin flags & ~RF_ACTIVE, child); 166383c41143SJohn Baldwin if (res == NULL) 166483c41143SJohn Baldwin return (NULL); 166583c41143SJohn Baldwin 166683c41143SJohn Baldwin if (bootverbose) 166783c41143SJohn Baldwin device_printf(sc->dev, 1668da1b038aSJustin Hibbits "allocated %s range (%#jx-%#jx) for rid %x of %s\n", 166983c41143SJohn Baldwin w->name, rman_get_start(res), rman_get_end(res), *rid, 167083c41143SJohn Baldwin pcib_child_name(child)); 167183c41143SJohn Baldwin rman_set_rid(res, *rid); 16721b9bcfffSJohn Baldwin rman_set_type(res, type); 167383c41143SJohn Baldwin 167483c41143SJohn Baldwin if (flags & RF_ACTIVE) { 167583c41143SJohn Baldwin if (bus_activate_resource(child, type, *rid, res) != 0) { 167683c41143SJohn Baldwin rman_release_resource(res); 167783c41143SJohn Baldwin return (NULL); 167883c41143SJohn Baldwin } 167983c41143SJohn Baldwin } 168083c41143SJohn Baldwin 168183c41143SJohn Baldwin return (res); 168283c41143SJohn Baldwin } 168383c41143SJohn Baldwin 1684c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */ 1685c825d4dcSJohn Baldwin static int 1686c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type, 16872dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 1688c825d4dcSJohn Baldwin { 1689c825d4dcSJohn Baldwin struct resource *res; 16902dd1bdf1SJustin Hibbits rman_res_t base, limit, wmask; 1691c825d4dcSJohn Baldwin int rid; 1692c825d4dcSJohn Baldwin 1693c825d4dcSJohn Baldwin /* 1694c825d4dcSJohn Baldwin * If this is an I/O window on a bridge with ISA enable set 1695c825d4dcSJohn Baldwin * and the start address is below 64k, then try to allocate an 1696c825d4dcSJohn Baldwin * initial window of 0x1000 bytes long starting at address 1697c825d4dcSJohn Baldwin * 0xf000 and walking down. Note that if the original request 1698c825d4dcSJohn Baldwin * was larger than the non-aliased range size of 0x100 our 1699c825d4dcSJohn Baldwin * caller would have raised the start address up to 64k 1700c825d4dcSJohn Baldwin * already. 1701c825d4dcSJohn Baldwin */ 1702c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1703c825d4dcSJohn Baldwin start < 65536) { 1704c825d4dcSJohn Baldwin for (base = 0xf000; (long)base >= 0; base -= 0x1000) { 1705c825d4dcSJohn Baldwin limit = base + 0xfff; 1706c825d4dcSJohn Baldwin 1707c825d4dcSJohn Baldwin /* 1708c825d4dcSJohn Baldwin * Skip ranges that wouldn't work for the 1709c825d4dcSJohn Baldwin * original request. Note that the actual 1710c825d4dcSJohn Baldwin * window that overlaps are the non-alias 1711c825d4dcSJohn Baldwin * ranges within [base, limit], so this isn't 1712c825d4dcSJohn Baldwin * quite a simple comparison. 1713c825d4dcSJohn Baldwin */ 1714c825d4dcSJohn Baldwin if (start + count > limit - 0x400) 1715c825d4dcSJohn Baldwin continue; 1716c825d4dcSJohn Baldwin if (base == 0) { 1717c825d4dcSJohn Baldwin /* 1718c825d4dcSJohn Baldwin * The first open region for the window at 1719c825d4dcSJohn Baldwin * 0 is 0x400-0x4ff. 1720c825d4dcSJohn Baldwin */ 1721c825d4dcSJohn Baldwin if (end - count + 1 < 0x400) 1722c825d4dcSJohn Baldwin continue; 1723c825d4dcSJohn Baldwin } else { 1724c825d4dcSJohn Baldwin if (end - count + 1 < base) 1725c825d4dcSJohn Baldwin continue; 1726c825d4dcSJohn Baldwin } 1727c825d4dcSJohn Baldwin 1728c825d4dcSJohn Baldwin if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) { 1729c825d4dcSJohn Baldwin w->base = base; 1730c825d4dcSJohn Baldwin w->limit = limit; 1731c825d4dcSJohn Baldwin return (0); 1732c825d4dcSJohn Baldwin } 1733c825d4dcSJohn Baldwin } 1734c825d4dcSJohn Baldwin return (ENOSPC); 1735c825d4dcSJohn Baldwin } 1736c825d4dcSJohn Baldwin 173789977ce2SJustin Hibbits wmask = ((rman_res_t)1 << w->step) - 1; 1738c825d4dcSJohn Baldwin if (RF_ALIGNMENT(flags) < w->step) { 1739c825d4dcSJohn Baldwin flags &= ~RF_ALIGNMENT_MASK; 1740c825d4dcSJohn Baldwin flags |= RF_ALIGNMENT_LOG2(w->step); 1741c825d4dcSJohn Baldwin } 1742c825d4dcSJohn Baldwin start &= ~wmask; 1743c825d4dcSJohn Baldwin end |= wmask; 174489977ce2SJustin Hibbits count = roundup2(count, (rman_res_t)1 << w->step); 1745c825d4dcSJohn Baldwin rid = w->reg; 1746c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, start, end, count, 1747b377ff81SJohn Baldwin flags | RF_ACTIVE | RF_UNMAPPED); 1748c825d4dcSJohn Baldwin if (res == NULL) 1749c825d4dcSJohn Baldwin return (ENOSPC); 1750c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 1751c825d4dcSJohn Baldwin pcib_activate_window(sc, type); 1752c825d4dcSJohn Baldwin w->base = rman_get_start(res); 1753c825d4dcSJohn Baldwin w->limit = rman_get_end(res); 1754c825d4dcSJohn Baldwin return (0); 1755c825d4dcSJohn Baldwin } 1756c825d4dcSJohn Baldwin 1757c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */ 1758c825d4dcSJohn Baldwin static int 1759c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type, 17602dd1bdf1SJustin Hibbits rman_res_t base, rman_res_t limit) 1761c825d4dcSJohn Baldwin { 1762c825d4dcSJohn Baldwin struct resource *res; 1763c825d4dcSJohn Baldwin int error, i, force_64k_base; 1764c825d4dcSJohn Baldwin 1765c825d4dcSJohn Baldwin KASSERT(base <= w->base && limit >= w->limit, 1766c825d4dcSJohn Baldwin ("attempting to shrink window")); 1767c825d4dcSJohn Baldwin 1768c825d4dcSJohn Baldwin /* 1769c825d4dcSJohn Baldwin * XXX: pcib_grow_window() doesn't try to do this anyway and 1770c825d4dcSJohn Baldwin * the error handling for all the edge cases would be tedious. 1771c825d4dcSJohn Baldwin */ 1772c825d4dcSJohn Baldwin KASSERT(limit == w->limit || base == w->base, 1773c825d4dcSJohn Baldwin ("attempting to grow both ends of a window")); 1774c825d4dcSJohn Baldwin 1775c825d4dcSJohn Baldwin /* 1776c825d4dcSJohn Baldwin * Yet more special handling for requests to expand an I/O 1777c825d4dcSJohn Baldwin * window behind an ISA-enabled bridge. Since I/O windows 1778c825d4dcSJohn Baldwin * have to grow in 0x1000 increments and the end of the 0xffff 1779c825d4dcSJohn Baldwin * range is an alias, growing a window below 64k will always 1780c825d4dcSJohn Baldwin * result in allocating new resources and never adjusting an 1781c825d4dcSJohn Baldwin * existing resource. 1782c825d4dcSJohn Baldwin */ 1783c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1784c825d4dcSJohn Baldwin (limit <= 65535 || (base <= 65535 && base != w->base))) { 1785c825d4dcSJohn Baldwin KASSERT(limit == w->limit || limit <= 65535, 1786c825d4dcSJohn Baldwin ("attempting to grow both ends across 64k ISA alias")); 1787c825d4dcSJohn Baldwin 1788c825d4dcSJohn Baldwin if (base != w->base) 1789c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1); 1790c825d4dcSJohn Baldwin else 1791c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, w->limit + 1, 1792c825d4dcSJohn Baldwin limit); 1793c825d4dcSJohn Baldwin if (error == 0) { 1794c825d4dcSJohn Baldwin w->base = base; 1795c825d4dcSJohn Baldwin w->limit = limit; 1796c825d4dcSJohn Baldwin } 1797c825d4dcSJohn Baldwin return (error); 1798c825d4dcSJohn Baldwin } 1799c825d4dcSJohn Baldwin 1800c825d4dcSJohn Baldwin /* 1801c825d4dcSJohn Baldwin * Find the existing resource to adjust. Usually there is only one, 1802c825d4dcSJohn Baldwin * but for an ISA-enabled bridge we might be growing the I/O window 1803c825d4dcSJohn Baldwin * above 64k and need to find the existing resource that maps all 1804c825d4dcSJohn Baldwin * of the area above 64k. 1805c825d4dcSJohn Baldwin */ 1806c825d4dcSJohn Baldwin for (i = 0; i < w->count; i++) { 1807c825d4dcSJohn Baldwin if (rman_get_end(w->res[i]) == w->limit) 1808c825d4dcSJohn Baldwin break; 1809c825d4dcSJohn Baldwin } 1810c825d4dcSJohn Baldwin KASSERT(i != w->count, ("did not find existing resource")); 1811c825d4dcSJohn Baldwin res = w->res[i]; 1812c825d4dcSJohn Baldwin 1813c825d4dcSJohn Baldwin /* 1814c825d4dcSJohn Baldwin * Usually the resource we found should match the window's 1815c825d4dcSJohn Baldwin * existing range. The one exception is the ISA-enabled case 1816c825d4dcSJohn Baldwin * mentioned above in which case the resource should start at 1817c825d4dcSJohn Baldwin * 64k. 1818c825d4dcSJohn Baldwin */ 1819c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1820c825d4dcSJohn Baldwin w->base <= 65535) { 1821c825d4dcSJohn Baldwin KASSERT(rman_get_start(res) == 65536, 1822c825d4dcSJohn Baldwin ("existing resource mismatch")); 1823c825d4dcSJohn Baldwin force_64k_base = 1; 1824c825d4dcSJohn Baldwin } else { 1825c825d4dcSJohn Baldwin KASSERT(w->base == rman_get_start(res), 1826c825d4dcSJohn Baldwin ("existing resource mismatch")); 1827c825d4dcSJohn Baldwin force_64k_base = 0; 1828c825d4dcSJohn Baldwin } 1829c825d4dcSJohn Baldwin 1830c825d4dcSJohn Baldwin error = bus_adjust_resource(sc->dev, type, res, force_64k_base ? 1831c825d4dcSJohn Baldwin rman_get_start(res) : base, limit); 1832c825d4dcSJohn Baldwin if (error) 1833c825d4dcSJohn Baldwin return (error); 1834c825d4dcSJohn Baldwin 1835c825d4dcSJohn Baldwin /* Add the newly allocated region to the resource manager. */ 1836c825d4dcSJohn Baldwin if (w->base != base) { 1837c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, base, w->base - 1); 1838c825d4dcSJohn Baldwin w->base = base; 1839c825d4dcSJohn Baldwin } else { 1840c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, w->limit + 1, limit); 1841c825d4dcSJohn Baldwin w->limit = limit; 1842c825d4dcSJohn Baldwin } 1843c825d4dcSJohn Baldwin if (error) { 1844c825d4dcSJohn Baldwin if (bootverbose) 1845c825d4dcSJohn Baldwin device_printf(sc->dev, 1846c825d4dcSJohn Baldwin "failed to expand %s resource manager\n", w->name); 1847c825d4dcSJohn Baldwin (void)bus_adjust_resource(sc->dev, type, res, force_64k_base ? 1848c825d4dcSJohn Baldwin rman_get_start(res) : w->base, w->limit); 1849c825d4dcSJohn Baldwin } 1850c825d4dcSJohn Baldwin return (error); 1851c825d4dcSJohn Baldwin } 1852c825d4dcSJohn Baldwin 185383c41143SJohn Baldwin /* 185483c41143SJohn Baldwin * Attempt to grow a window to make room for a given resource request. 185583c41143SJohn Baldwin */ 185683c41143SJohn Baldwin static int 185783c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type, 18582dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 185983c41143SJohn Baldwin { 18602dd1bdf1SJustin Hibbits rman_res_t align, start_free, end_free, front, back, wmask; 1861c825d4dcSJohn Baldwin int error; 186283c41143SJohn Baldwin 186383c41143SJohn Baldwin /* 186483c41143SJohn Baldwin * Clamp the desired resource range to the maximum address 186583c41143SJohn Baldwin * this window supports. Reject impossible requests. 1866c825d4dcSJohn Baldwin * 1867c825d4dcSJohn Baldwin * For I/O port requests behind a bridge with the ISA enable 1868c825d4dcSJohn Baldwin * bit set, force large allocations to start above 64k. 186983c41143SJohn Baldwin */ 187083c41143SJohn Baldwin if (!w->valid) 187183c41143SJohn Baldwin return (EINVAL); 1872c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 && 1873c825d4dcSJohn Baldwin start < 65536) 1874c825d4dcSJohn Baldwin start = 65536; 187583c41143SJohn Baldwin if (end > w->rman.rm_end) 187683c41143SJohn Baldwin end = w->rman.rm_end; 187783c41143SJohn Baldwin if (start + count - 1 > end || start + count < start) 187883c41143SJohn Baldwin return (EINVAL); 187989977ce2SJustin Hibbits wmask = ((rman_res_t)1 << w->step) - 1; 188083c41143SJohn Baldwin 188183c41143SJohn Baldwin /* 188283c41143SJohn Baldwin * If there is no resource at all, just try to allocate enough 188383c41143SJohn Baldwin * aligned space for this resource. 188483c41143SJohn Baldwin */ 188583c41143SJohn Baldwin if (w->res == NULL) { 1886c825d4dcSJohn Baldwin error = pcib_alloc_new_window(sc, w, type, start, end, count, 1887c825d4dcSJohn Baldwin flags); 1888c825d4dcSJohn Baldwin if (error) { 188983c41143SJohn Baldwin if (bootverbose) 189083c41143SJohn Baldwin device_printf(sc->dev, 1891da1b038aSJustin Hibbits "failed to allocate initial %s window (%#jx-%#jx,%#jx)\n", 189283c41143SJohn Baldwin w->name, start, end, count); 189383c41143SJohn Baldwin return (error); 189483c41143SJohn Baldwin } 1895c825d4dcSJohn Baldwin if (bootverbose) 1896c825d4dcSJohn Baldwin device_printf(sc->dev, 1897c825d4dcSJohn Baldwin "allocated initial %s window of %#jx-%#jx\n", 1898c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 189983c41143SJohn Baldwin goto updatewin; 190083c41143SJohn Baldwin } 190183c41143SJohn Baldwin 190283c41143SJohn Baldwin /* 190383c41143SJohn Baldwin * See if growing the window would help. Compute the minimum 190483c41143SJohn Baldwin * amount of address space needed on both the front and back 190583c41143SJohn Baldwin * ends of the existing window to satisfy the allocation. 190683c41143SJohn Baldwin * 190783c41143SJohn Baldwin * For each end, build a candidate region adjusting for the 190883c41143SJohn Baldwin * required alignment, etc. If there is a free region at the 190983c41143SJohn Baldwin * edge of the window, grow from the inner edge of the free 191083c41143SJohn Baldwin * region. Otherwise grow from the window boundary. 191183c41143SJohn Baldwin * 1912c825d4dcSJohn Baldwin * Growing an I/O window below 64k for a bridge with the ISA 1913c825d4dcSJohn Baldwin * enable bit doesn't require any special magic as the step 1914c825d4dcSJohn Baldwin * size of an I/O window (1k) always includes multiple 1915c825d4dcSJohn Baldwin * non-alias ranges when it is grown in either direction. 1916c825d4dcSJohn Baldwin * 191783c41143SJohn Baldwin * XXX: Special case: if w->res is completely empty and the 191883c41143SJohn Baldwin * request size is larger than w->res, we should find the 191983c41143SJohn Baldwin * optimal aligned buffer containing w->res and allocate that. 192083c41143SJohn Baldwin */ 192183c41143SJohn Baldwin if (bootverbose) 192283c41143SJohn Baldwin device_printf(sc->dev, 1923da1b038aSJustin Hibbits "attempting to grow %s window for (%#jx-%#jx,%#jx)\n", 192483c41143SJohn Baldwin w->name, start, end, count); 192589977ce2SJustin Hibbits align = (rman_res_t)1 << RF_ALIGNMENT(flags); 1926c825d4dcSJohn Baldwin if (start < w->base) { 192783c41143SJohn Baldwin if (rman_first_free_region(&w->rman, &start_free, &end_free) != 1928c825d4dcSJohn Baldwin 0 || start_free != w->base) 1929c825d4dcSJohn Baldwin end_free = w->base; 193083c41143SJohn Baldwin if (end_free > end) 1931ddac8cc9SJohn Baldwin end_free = end + 1; 193283c41143SJohn Baldwin 193383c41143SJohn Baldwin /* Move end_free down until it is properly aligned. */ 193483c41143SJohn Baldwin end_free &= ~(align - 1); 1935a49dcb46SJohn Baldwin end_free--; 1936a49dcb46SJohn Baldwin front = end_free - (count - 1); 193783c41143SJohn Baldwin 193883c41143SJohn Baldwin /* 193983c41143SJohn Baldwin * The resource would now be allocated at (front, 194083c41143SJohn Baldwin * end_free). Ensure that fits in the (start, end) 194183c41143SJohn Baldwin * bounds. end_free is checked above. If 'front' is 194283c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 194383c41143SJohn Baldwin * Also check for underflow. 194483c41143SJohn Baldwin */ 194583c41143SJohn Baldwin if (front >= start && front <= end_free) { 194683c41143SJohn Baldwin if (bootverbose) 1947da1b038aSJustin Hibbits printf("\tfront candidate range: %#jx-%#jx\n", 194883c41143SJohn Baldwin front, end_free); 1949a7b5acacSJohn Baldwin front &= ~wmask; 1950c825d4dcSJohn Baldwin front = w->base - front; 195183c41143SJohn Baldwin } else 195283c41143SJohn Baldwin front = 0; 195383c41143SJohn Baldwin } else 195483c41143SJohn Baldwin front = 0; 1955c825d4dcSJohn Baldwin if (end > w->limit) { 195683c41143SJohn Baldwin if (rman_last_free_region(&w->rman, &start_free, &end_free) != 1957c825d4dcSJohn Baldwin 0 || end_free != w->limit) 1958c825d4dcSJohn Baldwin start_free = w->limit + 1; 195983c41143SJohn Baldwin if (start_free < start) 196083c41143SJohn Baldwin start_free = start; 196183c41143SJohn Baldwin 196283c41143SJohn Baldwin /* Move start_free up until it is properly aligned. */ 196383c41143SJohn Baldwin start_free = roundup2(start_free, align); 1964a49dcb46SJohn Baldwin back = start_free + count - 1; 196583c41143SJohn Baldwin 196683c41143SJohn Baldwin /* 196783c41143SJohn Baldwin * The resource would now be allocated at (start_free, 196883c41143SJohn Baldwin * back). Ensure that fits in the (start, end) 196983c41143SJohn Baldwin * bounds. start_free is checked above. If 'back' is 197083c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 197183c41143SJohn Baldwin * Also check for overflow. 197283c41143SJohn Baldwin */ 197383c41143SJohn Baldwin if (back <= end && start_free <= back) { 197483c41143SJohn Baldwin if (bootverbose) 1975da1b038aSJustin Hibbits printf("\tback candidate range: %#jx-%#jx\n", 197683c41143SJohn Baldwin start_free, back); 1977a7b5acacSJohn Baldwin back |= wmask; 1978c825d4dcSJohn Baldwin back -= w->limit; 197983c41143SJohn Baldwin } else 198083c41143SJohn Baldwin back = 0; 198183c41143SJohn Baldwin } else 198283c41143SJohn Baldwin back = 0; 198383c41143SJohn Baldwin 198483c41143SJohn Baldwin /* 198583c41143SJohn Baldwin * Try to allocate the smallest needed region first. 198683c41143SJohn Baldwin * If that fails, fall back to the other region. 198783c41143SJohn Baldwin */ 198883c41143SJohn Baldwin error = ENOSPC; 198983c41143SJohn Baldwin while (front != 0 || back != 0) { 199083c41143SJohn Baldwin if (front != 0 && (front <= back || back == 0)) { 1991c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base - front, 1992c825d4dcSJohn Baldwin w->limit); 199383c41143SJohn Baldwin if (error == 0) 199483c41143SJohn Baldwin break; 199583c41143SJohn Baldwin front = 0; 199683c41143SJohn Baldwin } else { 1997c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base, 1998c825d4dcSJohn Baldwin w->limit + back); 199983c41143SJohn Baldwin if (error == 0) 200083c41143SJohn Baldwin break; 200183c41143SJohn Baldwin back = 0; 200283c41143SJohn Baldwin } 200383c41143SJohn Baldwin } 200483c41143SJohn Baldwin 200583c41143SJohn Baldwin if (error) 200683c41143SJohn Baldwin return (error); 200783c41143SJohn Baldwin if (bootverbose) 2008c825d4dcSJohn Baldwin device_printf(sc->dev, "grew %s window to %#jx-%#jx\n", 2009c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 201083c41143SJohn Baldwin 201183c41143SJohn Baldwin updatewin: 2012c825d4dcSJohn Baldwin /* Write the new window. */ 2013a7b5acacSJohn Baldwin KASSERT((w->base & wmask) == 0, ("start address is not aligned")); 2014a7b5acacSJohn Baldwin KASSERT((w->limit & wmask) == wmask, ("end address is not aligned")); 201583c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 201683c41143SJohn Baldwin return (0); 201783c41143SJohn Baldwin } 201883c41143SJohn Baldwin 201983c41143SJohn Baldwin /* 202083c41143SJohn Baldwin * We have to trap resource allocation requests and ensure that the bridge 202183c41143SJohn Baldwin * is set up to, or capable of handling them. 202283c41143SJohn Baldwin */ 202303719c65SJohn Baldwin static struct resource * 202483c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 20252dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 202683c41143SJohn Baldwin { 202783c41143SJohn Baldwin struct pcib_softc *sc; 202883c41143SJohn Baldwin struct resource *r; 202983c41143SJohn Baldwin 203083c41143SJohn Baldwin sc = device_get_softc(dev); 203183c41143SJohn Baldwin 203283c41143SJohn Baldwin /* 203383c41143SJohn Baldwin * VGA resources are decoded iff the VGA enable bit is set in 203483c41143SJohn Baldwin * the bridge control register. VGA resources do not fall into 203583c41143SJohn Baldwin * the resource windows and are passed up to the parent. 203683c41143SJohn Baldwin */ 203783c41143SJohn Baldwin if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) || 203883c41143SJohn Baldwin (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) { 203983c41143SJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) 204083c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, 204183c41143SJohn Baldwin rid, start, end, count, flags)); 204283c41143SJohn Baldwin else 204383c41143SJohn Baldwin return (NULL); 204483c41143SJohn Baldwin } 204583c41143SJohn Baldwin 204683c41143SJohn Baldwin switch (type) { 20474edef187SJohn Baldwin case PCI_RES_BUS: 20484edef187SJohn Baldwin return (pcib_alloc_subbus(&sc->bus, child, rid, start, end, 20494edef187SJohn Baldwin count, flags)); 205083c41143SJohn Baldwin case SYS_RES_IOPORT: 2051c825d4dcSJohn Baldwin if (pcib_is_isa_range(sc, start, end, count)) 2052c825d4dcSJohn Baldwin return (NULL); 205383c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start, 205483c41143SJohn Baldwin end, count, flags); 2055a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 205683c41143SJohn Baldwin break; 205783c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->io, type, start, end, count, 205883c41143SJohn Baldwin flags) == 0) 205983c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, 206083c41143SJohn Baldwin rid, start, end, count, flags); 206183c41143SJohn Baldwin break; 206283c41143SJohn Baldwin case SYS_RES_MEMORY: 206383c41143SJohn Baldwin /* 206483c41143SJohn Baldwin * For prefetchable resources, prefer the prefetchable 206583c41143SJohn Baldwin * memory window, but fall back to the regular memory 206683c41143SJohn Baldwin * window if that fails. Try both windows before 206783c41143SJohn Baldwin * attempting to grow a window in case the firmware 206883c41143SJohn Baldwin * has used a range in the regular memory window to 206983c41143SJohn Baldwin * map a prefetchable BAR. 207083c41143SJohn Baldwin */ 207183c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 207283c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, type, 207383c41143SJohn Baldwin rid, start, end, count, flags); 207483c41143SJohn Baldwin if (r != NULL) 207583c41143SJohn Baldwin break; 207683c41143SJohn Baldwin } 207783c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid, 207883c41143SJohn Baldwin start, end, count, flags); 2079a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 208083c41143SJohn Baldwin break; 208183c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 208283c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->pmem, type, start, end, 208383c41143SJohn Baldwin count, flags) == 0) { 208483c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, 208583c41143SJohn Baldwin type, rid, start, end, count, flags); 208683c41143SJohn Baldwin if (r != NULL) 208783c41143SJohn Baldwin break; 208883c41143SJohn Baldwin } 208983c41143SJohn Baldwin } 209083c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->mem, type, start, end, count, 209183c41143SJohn Baldwin flags & ~RF_PREFETCHABLE) == 0) 209283c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, 209383c41143SJohn Baldwin rid, start, end, count, flags); 209483c41143SJohn Baldwin break; 209583c41143SJohn Baldwin default: 209683c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 209783c41143SJohn Baldwin start, end, count, flags)); 209883c41143SJohn Baldwin } 209983c41143SJohn Baldwin 210083c41143SJohn Baldwin /* 210183c41143SJohn Baldwin * If attempts to suballocate from the window fail but this is a 210283c41143SJohn Baldwin * subtractive bridge, pass the request up the tree. 210383c41143SJohn Baldwin */ 210483c41143SJohn Baldwin if (sc->flags & PCIB_SUBTRACTIVE && r == NULL) 210583c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 210683c41143SJohn Baldwin start, end, count, flags)); 210783c41143SJohn Baldwin return (r); 210883c41143SJohn Baldwin } 210983c41143SJohn Baldwin 211003719c65SJohn Baldwin static int 2111fef01f04SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, struct resource *r, 21122dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end) 211383c41143SJohn Baldwin { 211483c41143SJohn Baldwin struct pcib_softc *sc; 211526245980SJessica Clarke struct pcib_window *w; 211615cb3b54SAlexander Motin rman_res_t wmask; 2117fef01f04SJohn Baldwin int error, type; 211883c41143SJohn Baldwin 211983c41143SJohn Baldwin sc = device_get_softc(bus); 2120fef01f04SJohn Baldwin type = rman_get_type(r); 212126245980SJessica Clarke 212226245980SJessica Clarke /* 212326245980SJessica Clarke * If the resource wasn't sub-allocated from one of our region 212426245980SJessica Clarke * managers then just pass the request up. 212526245980SJessica Clarke */ 2126582b84cdSJohn Baldwin if (!pcib_is_resource_managed(sc, r)) 2127fef01f04SJohn Baldwin return (bus_generic_adjust_resource(bus, child, r, start, end)); 212826245980SJessica Clarke 212931776afdSJessica Clarke if (type == PCI_RES_BUS) { 213031776afdSJessica Clarke /* 213131776afdSJessica Clarke * If our bus range isn't big enough to grow the sub-allocation 213231776afdSJessica Clarke * then we need to grow our bus range. Any request that would 213331776afdSJessica Clarke * require us to decrease the start of our own bus range is 213431776afdSJessica Clarke * invalid, we can only extend the end; ignore such requests 213531776afdSJessica Clarke * and let rman_adjust_resource fail below. 213631776afdSJessica Clarke */ 213731776afdSJessica Clarke if (start >= sc->bus.sec && end > sc->bus.sub) { 213831776afdSJessica Clarke error = pcib_grow_subbus(&sc->bus, end); 213931776afdSJessica Clarke if (error != 0) 214031776afdSJessica Clarke return (error); 214131776afdSJessica Clarke } 2142e1d442a6SJessica Clarke } else { 214326245980SJessica Clarke /* 214426245980SJessica Clarke * Resource is managed and not a secondary bus number, must 214526245980SJessica Clarke * be from one of our windows. 214626245980SJessica Clarke */ 2147582b84cdSJohn Baldwin w = pcib_get_resource_window(sc, r); 214826245980SJessica Clarke KASSERT(w != NULL, 214926245980SJessica Clarke ("%s: no window for resource (%#jx-%#jx) type %d", 215026245980SJessica Clarke __func__, rman_get_start(r), rman_get_end(r), type)); 215126245980SJessica Clarke 215226245980SJessica Clarke /* 215326245980SJessica Clarke * If our window isn't big enough to grow the sub-allocation 215426245980SJessica Clarke * then we need to expand the window. 215526245980SJessica Clarke */ 215626245980SJessica Clarke if (start < w->base || end > w->limit) { 215715cb3b54SAlexander Motin wmask = ((rman_res_t)1 << w->step) - 1; 215815cb3b54SAlexander Motin error = pcib_expand_window(sc, w, type, 215915cb3b54SAlexander Motin MIN(start & ~wmask, w->base), 216015cb3b54SAlexander Motin MAX(end | wmask, w->limit)); 216126245980SJessica Clarke if (error != 0) 216226245980SJessica Clarke return (error); 216315cb3b54SAlexander Motin if (bootverbose) 216415cb3b54SAlexander Motin device_printf(sc->dev, 216515cb3b54SAlexander Motin "grew %s window to %#jx-%#jx\n", 216615cb3b54SAlexander Motin w->name, (uintmax_t)w->base, 216715cb3b54SAlexander Motin (uintmax_t)w->limit); 216815cb3b54SAlexander Motin pcib_write_windows(sc, w->mask); 216926245980SJessica Clarke } 217026245980SJessica Clarke } 217126245980SJessica Clarke 217283c41143SJohn Baldwin return (rman_adjust_resource(r, start, end)); 217383c41143SJohn Baldwin } 217483c41143SJohn Baldwin 217503719c65SJohn Baldwin static int 21769dbf5b0eSJohn Baldwin pcib_release_resource(device_t dev, device_t child, struct resource *r) 217783c41143SJohn Baldwin { 217883c41143SJohn Baldwin struct pcib_softc *sc; 217983c41143SJohn Baldwin int error; 218083c41143SJohn Baldwin 218183c41143SJohn Baldwin sc = device_get_softc(dev); 2182582b84cdSJohn Baldwin if (pcib_is_resource_managed(sc, r)) { 218383c41143SJohn Baldwin if (rman_get_flags(r) & RF_ACTIVE) { 21849dbf5b0eSJohn Baldwin error = bus_deactivate_resource(child, r); 218583c41143SJohn Baldwin if (error) 218683c41143SJohn Baldwin return (error); 218783c41143SJohn Baldwin } 218883c41143SJohn Baldwin return (rman_release_resource(r)); 218983c41143SJohn Baldwin } 21909dbf5b0eSJohn Baldwin return (bus_generic_release_resource(dev, child, r)); 219183c41143SJohn Baldwin } 2192b377ff81SJohn Baldwin 2193b377ff81SJohn Baldwin static int 21942baed46eSJohn Baldwin pcib_activate_resource(device_t dev, device_t child, struct resource *r) 2195b377ff81SJohn Baldwin { 2196b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 2197b377ff81SJohn Baldwin struct resource_map map; 21982baed46eSJohn Baldwin int error, type; 2199b377ff81SJohn Baldwin 2200582b84cdSJohn Baldwin if (!pcib_is_resource_managed(sc, r)) 22012baed46eSJohn Baldwin return (bus_generic_activate_resource(dev, child, r)); 2202b377ff81SJohn Baldwin 2203b377ff81SJohn Baldwin error = rman_activate_resource(r); 2204b377ff81SJohn Baldwin if (error != 0) 2205b377ff81SJohn Baldwin return (error); 2206b377ff81SJohn Baldwin 22072baed46eSJohn Baldwin type = rman_get_type(r); 2208b377ff81SJohn Baldwin if ((rman_get_flags(r) & RF_UNMAPPED) == 0 && 2209b377ff81SJohn Baldwin (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { 2210d77f2092SJohn Baldwin error = BUS_MAP_RESOURCE(dev, child, r, NULL, &map); 2211b377ff81SJohn Baldwin if (error != 0) { 2212b377ff81SJohn Baldwin rman_deactivate_resource(r); 2213b377ff81SJohn Baldwin return (error); 2214b377ff81SJohn Baldwin } 2215b377ff81SJohn Baldwin 2216b377ff81SJohn Baldwin rman_set_mapping(r, &map); 2217b377ff81SJohn Baldwin } 2218b377ff81SJohn Baldwin return (0); 2219b377ff81SJohn Baldwin } 2220b377ff81SJohn Baldwin 2221b377ff81SJohn Baldwin static int 22222baed46eSJohn Baldwin pcib_deactivate_resource(device_t dev, device_t child, struct resource *r) 2223b377ff81SJohn Baldwin { 2224b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 2225b377ff81SJohn Baldwin struct resource_map map; 22262baed46eSJohn Baldwin int error, type; 2227b377ff81SJohn Baldwin 2228582b84cdSJohn Baldwin if (!pcib_is_resource_managed(sc, r)) 22292baed46eSJohn Baldwin return (bus_generic_deactivate_resource(dev, child, r)); 2230b377ff81SJohn Baldwin 2231b377ff81SJohn Baldwin error = rman_deactivate_resource(r); 2232b377ff81SJohn Baldwin if (error != 0) 2233b377ff81SJohn Baldwin return (error); 2234b377ff81SJohn Baldwin 22352baed46eSJohn Baldwin type = rman_get_type(r); 2236b377ff81SJohn Baldwin if ((rman_get_flags(r) & RF_UNMAPPED) == 0 && 2237b377ff81SJohn Baldwin (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { 2238b377ff81SJohn Baldwin rman_get_mapping(r, &map); 2239d77f2092SJohn Baldwin BUS_UNMAP_RESOURCE(dev, child, r, &map); 2240b377ff81SJohn Baldwin } 2241b377ff81SJohn Baldwin return (0); 2242b377ff81SJohn Baldwin } 2243b377ff81SJohn Baldwin 2244b377ff81SJohn Baldwin static struct resource * 2245b377ff81SJohn Baldwin pcib_find_parent_resource(struct pcib_window *w, struct resource *r) 2246b377ff81SJohn Baldwin { 2247b377ff81SJohn Baldwin for (int i = 0; i < w->count; i++) { 2248b377ff81SJohn Baldwin if (rman_get_start(w->res[i]) <= rman_get_start(r) && 2249b377ff81SJohn Baldwin rman_get_end(w->res[i]) >= rman_get_end(r)) 2250b377ff81SJohn Baldwin return (w->res[i]); 2251b377ff81SJohn Baldwin } 2252b377ff81SJohn Baldwin return (NULL); 2253b377ff81SJohn Baldwin } 2254b377ff81SJohn Baldwin 2255b377ff81SJohn Baldwin static int 2256d77f2092SJohn Baldwin pcib_map_resource(device_t dev, device_t child, struct resource *r, 2257b377ff81SJohn Baldwin struct resource_map_request *argsp, struct resource_map *map) 2258b377ff81SJohn Baldwin { 2259b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 2260b377ff81SJohn Baldwin struct resource_map_request args; 2261b377ff81SJohn Baldwin struct pcib_window *w; 2262b377ff81SJohn Baldwin struct resource *pres; 2263b377ff81SJohn Baldwin rman_res_t length, start; 2264b377ff81SJohn Baldwin int error; 2265b377ff81SJohn Baldwin 2266582b84cdSJohn Baldwin w = pcib_get_resource_window(sc, r); 2267b377ff81SJohn Baldwin if (w == NULL) 2268d77f2092SJohn Baldwin return (bus_generic_map_resource(dev, child, r, argsp, map)); 2269b377ff81SJohn Baldwin 2270b377ff81SJohn Baldwin /* Resources must be active to be mapped. */ 2271b377ff81SJohn Baldwin if (!(rman_get_flags(r) & RF_ACTIVE)) 2272b377ff81SJohn Baldwin return (ENXIO); 2273b377ff81SJohn Baldwin 2274b377ff81SJohn Baldwin resource_init_map_request(&args); 2275b377ff81SJohn Baldwin error = resource_validate_map_request(r, argsp, &args, &start, &length); 2276b377ff81SJohn Baldwin if (error) 2277b377ff81SJohn Baldwin return (error); 2278b377ff81SJohn Baldwin 2279b377ff81SJohn Baldwin pres = pcib_find_parent_resource(w, r); 2280b377ff81SJohn Baldwin if (pres == NULL) 2281b377ff81SJohn Baldwin return (ENOENT); 2282b377ff81SJohn Baldwin 2283b377ff81SJohn Baldwin args.offset = start - rman_get_start(pres); 2284b377ff81SJohn Baldwin args.length = length; 228598056127SJohn Baldwin return (bus_map_resource(dev, pres, &args, map)); 2286b377ff81SJohn Baldwin } 2287b377ff81SJohn Baldwin 2288b377ff81SJohn Baldwin static int 2289d77f2092SJohn Baldwin pcib_unmap_resource(device_t dev, device_t child, struct resource *r, 2290b377ff81SJohn Baldwin struct resource_map *map) 2291b377ff81SJohn Baldwin { 2292b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 2293b377ff81SJohn Baldwin struct pcib_window *w; 229498056127SJohn Baldwin struct resource *pres; 2295b377ff81SJohn Baldwin 2296582b84cdSJohn Baldwin w = pcib_get_resource_window(sc, r); 229798056127SJohn Baldwin if (w == NULL) 2298d77f2092SJohn Baldwin return (bus_generic_unmap_resource(dev, child, r, map)); 229998056127SJohn Baldwin 230098056127SJohn Baldwin pres = pcib_find_parent_resource(w, r); 230198056127SJohn Baldwin if (pres == NULL) 230298056127SJohn Baldwin return (ENOENT); 230398056127SJohn Baldwin return (bus_unmap_resource(dev, pres, map)); 2304b377ff81SJohn Baldwin } 2305bb0d0a8eSMike Smith 2306bb0d0a8eSMike Smith /* 230755d3ea17SRyan Stone * If ARI is enabled on this downstream port, translate the function number 230855d3ea17SRyan Stone * to the non-ARI slot/function. The downstream port will convert it back in 230955d3ea17SRyan Stone * hardware. If ARI is not enabled slot and func are not modified. 231055d3ea17SRyan Stone */ 231155d3ea17SRyan Stone static __inline void 231255d3ea17SRyan Stone pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func) 231355d3ea17SRyan Stone { 231455d3ea17SRyan Stone struct pcib_softc *sc; 231555d3ea17SRyan Stone int ari_func; 231655d3ea17SRyan Stone 231755d3ea17SRyan Stone sc = device_get_softc(pcib); 231855d3ea17SRyan Stone ari_func = *func; 231955d3ea17SRyan Stone 232055d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 232155d3ea17SRyan Stone KASSERT(*slot == 0, 232255d3ea17SRyan Stone ("Non-zero slot number with ARI enabled!")); 232355d3ea17SRyan Stone *slot = PCIE_ARI_SLOT(ari_func); 232455d3ea17SRyan Stone *func = PCIE_ARI_FUNC(ari_func); 232555d3ea17SRyan Stone } 232655d3ea17SRyan Stone } 232755d3ea17SRyan Stone 232855d3ea17SRyan Stone static void 232955d3ea17SRyan Stone pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos) 233055d3ea17SRyan Stone { 233155d3ea17SRyan Stone uint32_t ctl2; 233255d3ea17SRyan Stone 233355d3ea17SRyan Stone ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4); 233455d3ea17SRyan Stone ctl2 |= PCIEM_CTL2_ARI; 233555d3ea17SRyan Stone pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4); 233655d3ea17SRyan Stone 233755d3ea17SRyan Stone sc->flags |= PCIB_ENABLE_ARI; 233855d3ea17SRyan Stone } 233955d3ea17SRyan Stone 234055d3ea17SRyan Stone /* 2341bb0d0a8eSMike Smith * PCIB interface. 2342bb0d0a8eSMike Smith */ 23436f0d5884SJohn Baldwin int 2344bb0d0a8eSMike Smith pcib_maxslots(device_t dev) 2345bb0d0a8eSMike Smith { 23465502348dSJustin Hibbits #if !defined(__amd64__) && !defined(__i386__) 23478b92ad43SJustin Hibbits uint32_t pcie_pos; 23488b92ad43SJustin Hibbits uint16_t val; 23498b92ad43SJustin Hibbits 23508b92ad43SJustin Hibbits /* 23518b92ad43SJustin Hibbits * If this is a PCIe rootport or downstream switch port, there's only 23528b92ad43SJustin Hibbits * one slot permitted. 23538b92ad43SJustin Hibbits */ 23548b92ad43SJustin Hibbits if (pci_find_cap(dev, PCIY_EXPRESS, &pcie_pos) == 0) { 23558b92ad43SJustin Hibbits val = pci_read_config(dev, pcie_pos + PCIER_FLAGS, 2); 23568b92ad43SJustin Hibbits val &= PCIEM_FLAGS_TYPE; 23578b92ad43SJustin Hibbits if (val == PCIEM_TYPE_ROOT_PORT || 23588b92ad43SJustin Hibbits val == PCIEM_TYPE_DOWNSTREAM_PORT) 23598b92ad43SJustin Hibbits return (0); 23608b92ad43SJustin Hibbits } 23615502348dSJustin Hibbits #endif 23624fa59183SMike Smith return (PCI_SLOTMAX); 2363bb0d0a8eSMike Smith } 2364bb0d0a8eSMike Smith 236555d3ea17SRyan Stone static int 236655d3ea17SRyan Stone pcib_ari_maxslots(device_t dev) 236755d3ea17SRyan Stone { 236855d3ea17SRyan Stone struct pcib_softc *sc; 236955d3ea17SRyan Stone 237055d3ea17SRyan Stone sc = device_get_softc(dev); 237155d3ea17SRyan Stone 237255d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 237355d3ea17SRyan Stone return (PCIE_ARI_SLOTMAX); 237455d3ea17SRyan Stone else 23758b92ad43SJustin Hibbits return (pcib_maxslots(dev)); 237655d3ea17SRyan Stone } 237755d3ea17SRyan Stone 237855d3ea17SRyan Stone static int 237955d3ea17SRyan Stone pcib_ari_maxfuncs(device_t dev) 238055d3ea17SRyan Stone { 238155d3ea17SRyan Stone struct pcib_softc *sc; 238255d3ea17SRyan Stone 238355d3ea17SRyan Stone sc = device_get_softc(dev); 238455d3ea17SRyan Stone 238555d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 238655d3ea17SRyan Stone return (PCIE_ARI_FUNCMAX); 238755d3ea17SRyan Stone else 238855d3ea17SRyan Stone return (PCI_FUNCMAX); 238955d3ea17SRyan Stone } 239055d3ea17SRyan Stone 23912397d2d8SRyan Stone static void 23922397d2d8SRyan Stone pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot, 23932397d2d8SRyan Stone int *func) 23942397d2d8SRyan Stone { 23952397d2d8SRyan Stone struct pcib_softc *sc; 23962397d2d8SRyan Stone 23972397d2d8SRyan Stone sc = device_get_softc(pcib); 23982397d2d8SRyan Stone 23992397d2d8SRyan Stone *bus = PCI_RID2BUS(rid); 24002397d2d8SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 24012397d2d8SRyan Stone *slot = PCIE_ARI_RID2SLOT(rid); 24022397d2d8SRyan Stone *func = PCIE_ARI_RID2FUNC(rid); 24032397d2d8SRyan Stone } else { 24042397d2d8SRyan Stone *slot = PCI_RID2SLOT(rid); 24052397d2d8SRyan Stone *func = PCI_RID2FUNC(rid); 24062397d2d8SRyan Stone } 24072397d2d8SRyan Stone } 24082397d2d8SRyan Stone 2409bb0d0a8eSMike Smith /* 2410bb0d0a8eSMike Smith * Since we are a child of a PCI bus, its parent must support the pcib interface. 2411bb0d0a8eSMike Smith */ 241255d3ea17SRyan Stone static uint32_t 2413795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width) 2414bb0d0a8eSMike Smith { 241582cb5c3bSJohn Baldwin #ifdef PCI_HP 241682cb5c3bSJohn Baldwin struct pcib_softc *sc; 241755d3ea17SRyan Stone 241882cb5c3bSJohn Baldwin sc = device_get_softc(dev); 241982cb5c3bSJohn Baldwin if (!pcib_present(sc)) { 242082cb5c3bSJohn Baldwin switch (width) { 242182cb5c3bSJohn Baldwin case 2: 242282cb5c3bSJohn Baldwin return (0xffff); 242382cb5c3bSJohn Baldwin case 1: 242482cb5c3bSJohn Baldwin return (0xff); 242582cb5c3bSJohn Baldwin default: 242682cb5c3bSJohn Baldwin return (0xffffffff); 242782cb5c3bSJohn Baldwin } 242882cb5c3bSJohn Baldwin } 242982cb5c3bSJohn Baldwin #endif 243055d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 243155d3ea17SRyan Stone return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, 243255d3ea17SRyan Stone f, reg, width)); 2433bb0d0a8eSMike Smith } 2434bb0d0a8eSMike Smith 243555d3ea17SRyan Stone static void 2436795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width) 2437bb0d0a8eSMike Smith { 243882cb5c3bSJohn Baldwin #ifdef PCI_HP 243982cb5c3bSJohn Baldwin struct pcib_softc *sc; 244055d3ea17SRyan Stone 244182cb5c3bSJohn Baldwin sc = device_get_softc(dev); 244282cb5c3bSJohn Baldwin if (!pcib_present(sc)) 244382cb5c3bSJohn Baldwin return; 244482cb5c3bSJohn Baldwin #endif 244555d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 244655d3ea17SRyan Stone PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, 244755d3ea17SRyan Stone reg, val, width); 2448bb0d0a8eSMike Smith } 2449bb0d0a8eSMike Smith 2450bb0d0a8eSMike Smith /* 2451bb0d0a8eSMike Smith * Route an interrupt across a PCI bridge. 2452bb0d0a8eSMike Smith */ 24532c2d1d07SBenno Rice int 2454bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin) 2455bb0d0a8eSMike Smith { 2456bb0d0a8eSMike Smith device_t bus; 2457bb0d0a8eSMike Smith int parent_intpin; 2458bb0d0a8eSMike Smith int intnum; 2459bb0d0a8eSMike Smith 2460bb0d0a8eSMike Smith /* 2461bb0d0a8eSMike Smith * 2462bb0d0a8eSMike Smith * The PCI standard defines a swizzle of the child-side device/intpin to 2463bb0d0a8eSMike Smith * the parent-side intpin as follows. 2464bb0d0a8eSMike Smith * 2465bb0d0a8eSMike Smith * device = device on child bus 2466bb0d0a8eSMike Smith * child_intpin = intpin on child bus slot (0-3) 2467bb0d0a8eSMike Smith * parent_intpin = intpin on parent bus slot (0-3) 2468bb0d0a8eSMike Smith * 2469bb0d0a8eSMike Smith * parent_intpin = (device + child_intpin) % 4 2470bb0d0a8eSMike Smith */ 2471cdc95e1bSBernd Walter parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4; 2472bb0d0a8eSMike Smith 2473bb0d0a8eSMike Smith /* 2474bb0d0a8eSMike Smith * Our parent is a PCI bus. Its parent must export the pcib interface 2475bb0d0a8eSMike Smith * which includes the ability to route interrupts. 2476bb0d0a8eSMike Smith */ 2477bb0d0a8eSMike Smith bus = device_get_parent(pcib); 2478bb0d0a8eSMike Smith intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1); 247939981fedSJohn Baldwin if (PCI_INTERRUPT_VALID(intnum) && bootverbose) { 2480c6a121abSJohn Baldwin device_printf(pcib, "slot %d INT%c is routed to irq %d\n", 2481c6a121abSJohn Baldwin pci_get_slot(dev), 'A' + pin - 1, intnum); 24828046c4b9SMike Smith } 2483bb0d0a8eSMike Smith return(intnum); 2484bb0d0a8eSMike Smith } 2485b173edafSJohn Baldwin 2486e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */ 24879bf4c9c1SJohn Baldwin int 24889bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs) 24899bf4c9c1SJohn Baldwin { 2490bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 24919bf4c9c1SJohn Baldwin device_t bus; 24929bf4c9c1SJohn Baldwin 249322bf1c7fSJohn Baldwin if (sc->flags & PCIB_DISABLE_MSI) 249422bf1c7fSJohn Baldwin return (ENXIO); 24959bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 24969bf4c9c1SJohn Baldwin return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 24979bf4c9c1SJohn Baldwin irqs)); 24989bf4c9c1SJohn Baldwin } 24999bf4c9c1SJohn Baldwin 2500e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */ 25019bf4c9c1SJohn Baldwin int 25029bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs) 25039bf4c9c1SJohn Baldwin { 25049bf4c9c1SJohn Baldwin device_t bus; 25059bf4c9c1SJohn Baldwin 25069bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 25079bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs)); 25089bf4c9c1SJohn Baldwin } 25099bf4c9c1SJohn Baldwin 25109bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */ 25119bf4c9c1SJohn Baldwin int 2512e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq) 25139bf4c9c1SJohn Baldwin { 2514bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 25159bf4c9c1SJohn Baldwin device_t bus; 25169bf4c9c1SJohn Baldwin 251768e9cbd3SMarius Strobl if (sc->flags & PCIB_DISABLE_MSIX) 251822bf1c7fSJohn Baldwin return (ENXIO); 25199bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 2520e706f7f0SJohn Baldwin return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq)); 25215fe82bcaSJohn Baldwin } 25225fe82bcaSJohn Baldwin 25239bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */ 25249bf4c9c1SJohn Baldwin int 25259bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq) 25269bf4c9c1SJohn Baldwin { 25279bf4c9c1SJohn Baldwin device_t bus; 25289bf4c9c1SJohn Baldwin 25299bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 25309bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq)); 25319bf4c9c1SJohn Baldwin } 25329bf4c9c1SJohn Baldwin 2533e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */ 2534e706f7f0SJohn Baldwin int 2535e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, 2536e706f7f0SJohn Baldwin uint32_t *data) 2537e706f7f0SJohn Baldwin { 2538e706f7f0SJohn Baldwin device_t bus; 25394522ac77SLuoqi Chen int error; 2540e706f7f0SJohn Baldwin 2541e706f7f0SJohn Baldwin bus = device_get_parent(pcib); 25424522ac77SLuoqi Chen error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data); 25434522ac77SLuoqi Chen if (error) 25444522ac77SLuoqi Chen return (error); 25454522ac77SLuoqi Chen 25464522ac77SLuoqi Chen pci_ht_map_msi(pcib, *addr); 25474522ac77SLuoqi Chen return (0); 2548e706f7f0SJohn Baldwin } 2549e706f7f0SJohn Baldwin 255062508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */ 255162508c53SJohn Baldwin int 255262508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate) 255362508c53SJohn Baldwin { 255462508c53SJohn Baldwin device_t bus; 255562508c53SJohn Baldwin 255662508c53SJohn Baldwin bus = device_get_parent(pcib); 255762508c53SJohn Baldwin return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate)); 255862508c53SJohn Baldwin } 25595605a99eSRyan Stone 25602397d2d8SRyan Stone static int 25612397d2d8SRyan Stone pcib_ari_enabled(device_t pcib) 25622397d2d8SRyan Stone { 25632397d2d8SRyan Stone struct pcib_softc *sc; 25642397d2d8SRyan Stone 25652397d2d8SRyan Stone sc = device_get_softc(pcib); 25662397d2d8SRyan Stone 25672397d2d8SRyan Stone return ((sc->flags & PCIB_ENABLE_ARI) != 0); 25682397d2d8SRyan Stone } 25692397d2d8SRyan Stone 2570d7be980dSAndrew Turner static int 2571d7be980dSAndrew Turner pcib_ari_get_id(device_t pcib, device_t dev, enum pci_id_type type, 2572d7be980dSAndrew Turner uintptr_t *id) 257355d3ea17SRyan Stone { 257455d3ea17SRyan Stone struct pcib_softc *sc; 25751e43b18cSAndrew Turner device_t bus_dev; 257655d3ea17SRyan Stone uint8_t bus, slot, func; 257755d3ea17SRyan Stone 25781e43b18cSAndrew Turner if (type != PCI_ID_RID) { 25791e43b18cSAndrew Turner bus_dev = device_get_parent(pcib); 25801e43b18cSAndrew Turner return (PCIB_GET_ID(device_get_parent(bus_dev), dev, type, id)); 25811e43b18cSAndrew Turner } 2582d7be980dSAndrew Turner 258355d3ea17SRyan Stone sc = device_get_softc(pcib); 258455d3ea17SRyan Stone 258555d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 258655d3ea17SRyan Stone bus = pci_get_bus(dev); 258755d3ea17SRyan Stone func = pci_get_function(dev); 258855d3ea17SRyan Stone 2589d7be980dSAndrew Turner *id = (PCI_ARI_RID(bus, func)); 259055d3ea17SRyan Stone } else { 259155d3ea17SRyan Stone bus = pci_get_bus(dev); 259255d3ea17SRyan Stone slot = pci_get_slot(dev); 259355d3ea17SRyan Stone func = pci_get_function(dev); 259455d3ea17SRyan Stone 2595d7be980dSAndrew Turner *id = (PCI_RID(bus, slot, func)); 259655d3ea17SRyan Stone } 2597d7be980dSAndrew Turner 2598d7be980dSAndrew Turner return (0); 259955d3ea17SRyan Stone } 260055d3ea17SRyan Stone 260155d3ea17SRyan Stone /* 260255d3ea17SRyan Stone * Check that the downstream port (pcib) and the endpoint device (dev) both 260355d3ea17SRyan Stone * support ARI. If so, enable it and return 0, otherwise return an error. 260455d3ea17SRyan Stone */ 260555d3ea17SRyan Stone static int 260655d3ea17SRyan Stone pcib_try_enable_ari(device_t pcib, device_t dev) 260755d3ea17SRyan Stone { 260855d3ea17SRyan Stone struct pcib_softc *sc; 260955d3ea17SRyan Stone int error; 261055d3ea17SRyan Stone uint32_t cap2; 261155d3ea17SRyan Stone int ari_cap_off; 261255d3ea17SRyan Stone uint32_t ari_ver; 261355d3ea17SRyan Stone uint32_t pcie_pos; 261455d3ea17SRyan Stone 261555d3ea17SRyan Stone sc = device_get_softc(pcib); 261655d3ea17SRyan Stone 261755d3ea17SRyan Stone /* 261855d3ea17SRyan Stone * ARI is controlled in a register in the PCIe capability structure. 261955d3ea17SRyan Stone * If the downstream port does not have the PCIe capability structure 262055d3ea17SRyan Stone * then it does not support ARI. 262155d3ea17SRyan Stone */ 262255d3ea17SRyan Stone error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos); 262355d3ea17SRyan Stone if (error != 0) 262455d3ea17SRyan Stone return (ENODEV); 262555d3ea17SRyan Stone 262655d3ea17SRyan Stone /* Check that the PCIe port advertises ARI support. */ 262755d3ea17SRyan Stone cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4); 262855d3ea17SRyan Stone if (!(cap2 & PCIEM_CAP2_ARI)) 262955d3ea17SRyan Stone return (ENODEV); 263055d3ea17SRyan Stone 263155d3ea17SRyan Stone /* 263255d3ea17SRyan Stone * Check that the endpoint device advertises ARI support via the ARI 263355d3ea17SRyan Stone * extended capability structure. 263455d3ea17SRyan Stone */ 263555d3ea17SRyan Stone error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off); 263655d3ea17SRyan Stone if (error != 0) 263755d3ea17SRyan Stone return (ENODEV); 263855d3ea17SRyan Stone 263955d3ea17SRyan Stone /* 264055d3ea17SRyan Stone * Finally, check that the endpoint device supports the same version 264155d3ea17SRyan Stone * of ARI that we do. 264255d3ea17SRyan Stone */ 264355d3ea17SRyan Stone ari_ver = pci_read_config(dev, ari_cap_off, 4); 264455d3ea17SRyan Stone if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) { 264555d3ea17SRyan Stone if (bootverbose) 264655d3ea17SRyan Stone device_printf(pcib, 264755d3ea17SRyan Stone "Unsupported version of ARI (%d) detected\n", 264855d3ea17SRyan Stone PCI_EXTCAP_VER(ari_ver)); 264955d3ea17SRyan Stone 265055d3ea17SRyan Stone return (ENXIO); 265155d3ea17SRyan Stone } 265255d3ea17SRyan Stone 265355d3ea17SRyan Stone pcib_enable_ari(sc, pcie_pos); 265455d3ea17SRyan Stone 265555d3ea17SRyan Stone return (0); 265655d3ea17SRyan Stone } 26574cb67729SWarner Losh 265828586889SWarner Losh int 265928586889SWarner Losh pcib_request_feature_allow(device_t pcib, device_t dev, 266028586889SWarner Losh enum pci_feature feature) 266128586889SWarner Losh { 266228586889SWarner Losh /* 26635914c62eSGavin Atkinson * No host firmware we have to negotiate with, so we allow 266428586889SWarner Losh * every valid feature requested. 266528586889SWarner Losh */ 266628586889SWarner Losh switch (feature) { 266728586889SWarner Losh case PCI_FEATURE_AER: 266828586889SWarner Losh case PCI_FEATURE_HP: 266928586889SWarner Losh break; 267028586889SWarner Losh default: 267128586889SWarner Losh return (EINVAL); 267228586889SWarner Losh } 267328586889SWarner Losh 267428586889SWarner Losh return (0); 267528586889SWarner Losh } 267628586889SWarner Losh 26771ffd07bdSJohn Baldwin int 26781ffd07bdSJohn Baldwin pcib_request_feature(device_t dev, enum pci_feature feature) 26791ffd07bdSJohn Baldwin { 26801ffd07bdSJohn Baldwin 26811ffd07bdSJohn Baldwin /* 26821ffd07bdSJohn Baldwin * Invoke PCIB_REQUEST_FEATURE of this bridge first in case 26831ffd07bdSJohn Baldwin * the firmware overrides the method of PCI-PCI bridges. 26841ffd07bdSJohn Baldwin */ 26851ffd07bdSJohn Baldwin return (PCIB_REQUEST_FEATURE(dev, dev, feature)); 26861ffd07bdSJohn Baldwin } 26871ffd07bdSJohn Baldwin 26884cb67729SWarner Losh /* 26894cb67729SWarner Losh * Pass the request to use this PCI feature up the tree. Either there's a 26904cb67729SWarner Losh * firmware like ACPI that's using this feature that will approve (or deny) the 26914cb67729SWarner Losh * request to take it over, or the platform has no such firmware, in which case 26924cb67729SWarner Losh * the request will be approved. If the request is approved, the OS is expected 26934cb67729SWarner Losh * to make use of the feature or render it harmless. 26944cb67729SWarner Losh */ 26954cb67729SWarner Losh static int 26961ffd07bdSJohn Baldwin pcib_request_feature_default(device_t pcib, device_t dev, 26971ffd07bdSJohn Baldwin enum pci_feature feature) 26984cb67729SWarner Losh { 26994cb67729SWarner Losh device_t bus; 27004cb67729SWarner Losh 27014cb67729SWarner Losh /* 27024cb67729SWarner Losh * Our parent is necessarily a pci bus. Its parent will either be 27034cb67729SWarner Losh * another pci bridge (which passes it up) or a host bridge that can 27044cb67729SWarner Losh * approve or reject the request. 27054cb67729SWarner Losh */ 27064cb67729SWarner Losh bus = device_get_parent(pcib); 27074cb67729SWarner Losh return (PCIB_REQUEST_FEATURE(device_get_parent(bus), dev, feature)); 27084cb67729SWarner Losh } 27095db2a4a8SKonstantin Belousov 27105db2a4a8SKonstantin Belousov static int 27115db2a4a8SKonstantin Belousov pcib_reset_child(device_t dev, device_t child, int flags) 27125db2a4a8SKonstantin Belousov { 27135db2a4a8SKonstantin Belousov struct pci_devinfo *pdinfo; 27145db2a4a8SKonstantin Belousov int error; 27155db2a4a8SKonstantin Belousov 27165db2a4a8SKonstantin Belousov error = 0; 27175db2a4a8SKonstantin Belousov if (dev == NULL || device_get_parent(child) != dev) 27185db2a4a8SKonstantin Belousov goto out; 27195db2a4a8SKonstantin Belousov error = ENXIO; 27205db2a4a8SKonstantin Belousov if (device_get_devclass(child) != devclass_find("pci")) 27215db2a4a8SKonstantin Belousov goto out; 27225db2a4a8SKonstantin Belousov pdinfo = device_get_ivars(dev); 27235db2a4a8SKonstantin Belousov if (pdinfo->cfg.pcie.pcie_location != 0 && 27245db2a4a8SKonstantin Belousov (pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_DOWNSTREAM_PORT || 27255db2a4a8SKonstantin Belousov pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_ROOT_PORT)) { 27265db2a4a8SKonstantin Belousov error = bus_helper_reset_prepare(child, flags); 27275db2a4a8SKonstantin Belousov if (error == 0) { 27285db2a4a8SKonstantin Belousov error = pcie_link_reset(dev, 27295db2a4a8SKonstantin Belousov pdinfo->cfg.pcie.pcie_location); 27305db2a4a8SKonstantin Belousov /* XXXKIB call _post even if error != 0 ? */ 27315db2a4a8SKonstantin Belousov bus_helper_reset_post(child, flags); 27325db2a4a8SKonstantin Belousov } 27335db2a4a8SKonstantin Belousov } 27345db2a4a8SKonstantin Belousov out: 27355db2a4a8SKonstantin Belousov return (error); 27365db2a4a8SKonstantin Belousov } 2737