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_suspend(device_t dev); 62e36af292SJung-uk Kim static int pcib_resume(device_t dev); 6303719c65SJohn Baldwin 6403719c65SJohn Baldwin static bus_child_present_t pcib_child_present; 6503719c65SJohn Baldwin static bus_alloc_resource_t pcib_alloc_resource; 6603719c65SJohn Baldwin #ifdef NEW_PCIB 6703719c65SJohn Baldwin static bus_adjust_resource_t pcib_adjust_resource; 6803719c65SJohn Baldwin static bus_release_resource_t pcib_release_resource; 6903719c65SJohn Baldwin #endif 7003719c65SJohn Baldwin static int pcib_reset_child(device_t dev, device_t child, int flags); 7103719c65SJohn Baldwin 7262508c53SJohn Baldwin static int pcib_power_for_sleep(device_t pcib, device_t dev, 7362508c53SJohn Baldwin int *pstate); 74d7be980dSAndrew Turner static int pcib_ari_get_id(device_t pcib, device_t dev, 75d7be980dSAndrew Turner enum pci_id_type type, uintptr_t *id); 7655d3ea17SRyan Stone static uint32_t pcib_read_config(device_t dev, u_int b, u_int s, 7755d3ea17SRyan Stone u_int f, u_int reg, int width); 7855d3ea17SRyan Stone static void pcib_write_config(device_t dev, u_int b, u_int s, 7955d3ea17SRyan Stone u_int f, u_int reg, uint32_t val, int width); 8055d3ea17SRyan Stone static int pcib_ari_maxslots(device_t dev); 8155d3ea17SRyan Stone static int pcib_ari_maxfuncs(device_t dev); 8255d3ea17SRyan Stone static int pcib_try_enable_ari(device_t pcib, device_t dev); 832397d2d8SRyan Stone static int pcib_ari_enabled(device_t pcib); 842397d2d8SRyan Stone static void pcib_ari_decode_rid(device_t pcib, uint16_t rid, 852397d2d8SRyan Stone int *bus, int *slot, int *func); 8682cb5c3bSJohn Baldwin #ifdef PCI_HP 87fa3b03d3SAlexander Motin static void pcib_pcie_ab_timeout(void *arg, int pending); 88fa3b03d3SAlexander Motin static void pcib_pcie_cc_timeout(void *arg, int pending); 89fa3b03d3SAlexander Motin static void pcib_pcie_dll_timeout(void *arg, int pending); 9082cb5c3bSJohn Baldwin #endif 911ffd07bdSJohn Baldwin static int pcib_request_feature_default(device_t pcib, device_t dev, 924cb67729SWarner Losh enum pci_feature feature); 93bb0d0a8eSMike Smith 94bb0d0a8eSMike Smith static device_method_t pcib_methods[] = { 95bb0d0a8eSMike Smith /* Device interface */ 96bb0d0a8eSMike Smith DEVMETHOD(device_probe, pcib_probe), 97bb0d0a8eSMike Smith DEVMETHOD(device_attach, pcib_attach), 986f33eaa5SJohn Baldwin DEVMETHOD(device_detach, pcib_detach), 99bb0d0a8eSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 100e36af292SJung-uk Kim DEVMETHOD(device_suspend, pcib_suspend), 101e36af292SJung-uk Kim DEVMETHOD(device_resume, pcib_resume), 102bb0d0a8eSMike Smith 103bb0d0a8eSMike Smith /* Bus interface */ 10482cb5c3bSJohn Baldwin DEVMETHOD(bus_child_present, pcib_child_present), 105bb0d0a8eSMike Smith DEVMETHOD(bus_read_ivar, pcib_read_ivar), 106bb0d0a8eSMike Smith DEVMETHOD(bus_write_ivar, pcib_write_ivar), 107bb0d0a8eSMike Smith DEVMETHOD(bus_alloc_resource, pcib_alloc_resource), 10883c41143SJohn Baldwin #ifdef NEW_PCIB 10983c41143SJohn Baldwin DEVMETHOD(bus_adjust_resource, pcib_adjust_resource), 11083c41143SJohn Baldwin DEVMETHOD(bus_release_resource, pcib_release_resource), 11183c41143SJohn Baldwin #else 112d2c9344fSJohn Baldwin DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 113bb0d0a8eSMike Smith DEVMETHOD(bus_release_resource, bus_generic_release_resource), 11483c41143SJohn Baldwin #endif 115bb0d0a8eSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 116bb0d0a8eSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 117bb0d0a8eSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 118bb0d0a8eSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 1195db2a4a8SKonstantin Belousov DEVMETHOD(bus_reset_child, pcib_reset_child), 120bb0d0a8eSMike Smith 121bb0d0a8eSMike Smith /* pcib interface */ 12255d3ea17SRyan Stone DEVMETHOD(pcib_maxslots, pcib_ari_maxslots), 12355d3ea17SRyan Stone DEVMETHOD(pcib_maxfuncs, pcib_ari_maxfuncs), 124bb0d0a8eSMike Smith DEVMETHOD(pcib_read_config, pcib_read_config), 125bb0d0a8eSMike Smith DEVMETHOD(pcib_write_config, pcib_write_config), 126bb0d0a8eSMike Smith DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt), 1279bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi), 1289bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msi, pcib_release_msi), 1299bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix), 1309bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msix, pcib_release_msix), 131e706f7f0SJohn Baldwin DEVMETHOD(pcib_map_msi, pcib_map_msi), 13262508c53SJohn Baldwin DEVMETHOD(pcib_power_for_sleep, pcib_power_for_sleep), 133d7be980dSAndrew Turner DEVMETHOD(pcib_get_id, pcib_ari_get_id), 13455d3ea17SRyan Stone DEVMETHOD(pcib_try_enable_ari, pcib_try_enable_ari), 1352397d2d8SRyan Stone DEVMETHOD(pcib_ari_enabled, pcib_ari_enabled), 1362397d2d8SRyan Stone DEVMETHOD(pcib_decode_rid, pcib_ari_decode_rid), 1371ffd07bdSJohn Baldwin DEVMETHOD(pcib_request_feature, pcib_request_feature_default), 138bb0d0a8eSMike Smith 1394b7ec270SMarius Strobl DEVMETHOD_END 140bb0d0a8eSMike Smith }; 141bb0d0a8eSMike Smith 14204dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc)); 14397a41013SJohn Baldwin EARLY_DRIVER_MODULE(pcib, pci, pcib_driver, NULL, NULL, BUS_PASS_BUS); 144bb0d0a8eSMike Smith 1456ca2d094SBjoern A. Zeeb #if defined(NEW_PCIB) || defined(PCI_HP) 1460070c94bSJohn Baldwin SYSCTL_DECL(_hw_pci); 1476ca2d094SBjoern A. Zeeb #endif 1480070c94bSJohn Baldwin 1496ca2d094SBjoern A. Zeeb #ifdef NEW_PCIB 1500070c94bSJohn Baldwin static int pci_clear_pcib; 1510070c94bSJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0, 1520070c94bSJohn Baldwin "Clear firmware-assigned resources for PCI-PCI bridge I/O windows."); 15383c41143SJohn Baldwin 15483c41143SJohn Baldwin /* 15526245980SJessica Clarke * Get the corresponding window if this resource from a child device was 15626245980SJessica Clarke * sub-allocated from one of our window resource managers. 15726245980SJessica Clarke */ 15826245980SJessica Clarke static struct pcib_window * 15926245980SJessica Clarke pcib_get_resource_window(struct pcib_softc *sc, int type, struct resource *r) 16026245980SJessica Clarke { 16126245980SJessica Clarke switch (type) { 16226245980SJessica Clarke case SYS_RES_IOPORT: 16326245980SJessica Clarke if (rman_is_region_manager(r, &sc->io.rman)) 16426245980SJessica Clarke return (&sc->io); 16526245980SJessica Clarke break; 16626245980SJessica Clarke case SYS_RES_MEMORY: 16726245980SJessica Clarke /* Prefetchable resources may live in either memory rman. */ 16826245980SJessica Clarke if (rman_get_flags(r) & RF_PREFETCHABLE && 16926245980SJessica Clarke rman_is_region_manager(r, &sc->pmem.rman)) 17026245980SJessica Clarke return (&sc->pmem); 17126245980SJessica Clarke if (rman_is_region_manager(r, &sc->mem.rman)) 17226245980SJessica Clarke return (&sc->mem); 17326245980SJessica Clarke break; 17426245980SJessica Clarke } 17526245980SJessica Clarke return (NULL); 17626245980SJessica Clarke } 17726245980SJessica Clarke 17826245980SJessica Clarke /* 17983c41143SJohn Baldwin * Is a resource from a child device sub-allocated from one of our 18083c41143SJohn Baldwin * resource managers? 18183c41143SJohn Baldwin */ 18283c41143SJohn Baldwin static int 18383c41143SJohn Baldwin pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r) 18483c41143SJohn Baldwin { 18583c41143SJohn Baldwin 1864edef187SJohn Baldwin #ifdef PCI_RES_BUS 18726245980SJessica Clarke if (type == PCI_RES_BUS) 1884edef187SJohn Baldwin return (rman_is_region_manager(r, &sc->bus.rman)); 1894edef187SJohn Baldwin #endif 19026245980SJessica Clarke return (pcib_get_resource_window(sc, type, r) != NULL); 19183c41143SJohn Baldwin } 19283c41143SJohn Baldwin 19383c41143SJohn Baldwin static int 19483c41143SJohn Baldwin pcib_is_window_open(struct pcib_window *pw) 19583c41143SJohn Baldwin { 19683c41143SJohn Baldwin 19783c41143SJohn Baldwin return (pw->valid && pw->base < pw->limit); 19883c41143SJohn Baldwin } 19983c41143SJohn Baldwin 20083c41143SJohn Baldwin /* 20183c41143SJohn Baldwin * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and 20283c41143SJohn Baldwin * handle for the resource, we could pass RF_ACTIVE up to the PCI bus 20383c41143SJohn Baldwin * when allocating the resource windows and rely on the PCI bus driver 20483c41143SJohn Baldwin * to do this for us. 20583c41143SJohn Baldwin */ 20683c41143SJohn Baldwin static void 20783c41143SJohn Baldwin pcib_activate_window(struct pcib_softc *sc, int type) 20883c41143SJohn Baldwin { 20983c41143SJohn Baldwin 21083c41143SJohn Baldwin PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type); 21183c41143SJohn Baldwin } 21283c41143SJohn Baldwin 21383c41143SJohn Baldwin static void 21483c41143SJohn Baldwin pcib_write_windows(struct pcib_softc *sc, int mask) 21583c41143SJohn Baldwin { 21683c41143SJohn Baldwin device_t dev; 21783c41143SJohn Baldwin uint32_t val; 21883c41143SJohn Baldwin 21983c41143SJohn Baldwin dev = sc->dev; 22083c41143SJohn Baldwin if (sc->io.valid && mask & WIN_IO) { 22183c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 22283c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 22383c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEH_1, 22483c41143SJohn Baldwin sc->io.base >> 16, 2); 22583c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITH_1, 22683c41143SJohn Baldwin sc->io.limit >> 16, 2); 22783c41143SJohn Baldwin } 22883c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1); 22983c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1); 23083c41143SJohn Baldwin } 23183c41143SJohn Baldwin 23283c41143SJohn Baldwin if (mask & WIN_MEM) { 23383c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2); 23483c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2); 23583c41143SJohn Baldwin } 23683c41143SJohn Baldwin 23783c41143SJohn Baldwin if (sc->pmem.valid && mask & WIN_PMEM) { 23883c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 23983c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 24083c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEH_1, 24183c41143SJohn Baldwin sc->pmem.base >> 32, 4); 24283c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITH_1, 24383c41143SJohn Baldwin sc->pmem.limit >> 32, 4); 24483c41143SJohn Baldwin } 24583c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2); 24683c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2); 24783c41143SJohn Baldwin } 24883c41143SJohn Baldwin } 24983c41143SJohn Baldwin 250c825d4dcSJohn Baldwin /* 251c825d4dcSJohn Baldwin * This is used to reject I/O port allocations that conflict with an 252c825d4dcSJohn Baldwin * ISA alias range. 253c825d4dcSJohn Baldwin */ 254c825d4dcSJohn Baldwin static int 2552dd1bdf1SJustin Hibbits pcib_is_isa_range(struct pcib_softc *sc, rman_res_t start, rman_res_t end, 2562dd1bdf1SJustin Hibbits rman_res_t count) 257c825d4dcSJohn Baldwin { 2582dd1bdf1SJustin Hibbits rman_res_t next_alias; 259c825d4dcSJohn Baldwin 260c825d4dcSJohn Baldwin if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE)) 261c825d4dcSJohn Baldwin return (0); 262c825d4dcSJohn Baldwin 263c825d4dcSJohn Baldwin /* Only check fixed ranges for overlap. */ 264c825d4dcSJohn Baldwin if (start + count - 1 != end) 265c825d4dcSJohn Baldwin return (0); 266c825d4dcSJohn Baldwin 267c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */ 268c825d4dcSJohn Baldwin if (start >= 65536) 269c825d4dcSJohn Baldwin return (0); 270c825d4dcSJohn Baldwin 271c825d4dcSJohn Baldwin /* Check for overlap with 0x000 - 0x0ff as a special case. */ 272c825d4dcSJohn Baldwin if (start < 0x100) 273c825d4dcSJohn Baldwin goto alias; 274c825d4dcSJohn Baldwin 275c825d4dcSJohn Baldwin /* 276c825d4dcSJohn Baldwin * If the start address is an alias, the range is an alias. 277c825d4dcSJohn Baldwin * Otherwise, compute the start of the next alias range and 278c825d4dcSJohn Baldwin * check if it is before the end of the candidate range. 279c825d4dcSJohn Baldwin */ 280c825d4dcSJohn Baldwin if ((start & 0x300) != 0) 281c825d4dcSJohn Baldwin goto alias; 282c825d4dcSJohn Baldwin next_alias = (start & ~0x3fful) | 0x100; 283c825d4dcSJohn Baldwin if (next_alias <= end) 284c825d4dcSJohn Baldwin goto alias; 285c825d4dcSJohn Baldwin return (0); 286c825d4dcSJohn Baldwin 287c825d4dcSJohn Baldwin alias: 288c825d4dcSJohn Baldwin if (bootverbose) 289c825d4dcSJohn Baldwin device_printf(sc->dev, 290da1b038aSJustin Hibbits "I/O range %#jx-%#jx overlaps with an ISA alias\n", start, 291c825d4dcSJohn Baldwin end); 292c825d4dcSJohn Baldwin return (1); 293c825d4dcSJohn Baldwin } 294c825d4dcSJohn Baldwin 295c825d4dcSJohn Baldwin static void 296c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res, 297c825d4dcSJohn Baldwin int count) 298c825d4dcSJohn Baldwin { 299c825d4dcSJohn Baldwin struct resource **newarray; 300c825d4dcSJohn Baldwin int error, i; 301c825d4dcSJohn Baldwin 302c825d4dcSJohn Baldwin newarray = malloc(sizeof(struct resource *) * (w->count + count), 303c825d4dcSJohn Baldwin M_DEVBUF, M_WAITOK); 304c825d4dcSJohn Baldwin if (w->res != NULL) 305c825d4dcSJohn Baldwin bcopy(w->res, newarray, sizeof(struct resource *) * w->count); 306c825d4dcSJohn Baldwin bcopy(res, newarray + w->count, sizeof(struct resource *) * count); 307c825d4dcSJohn Baldwin free(w->res, M_DEVBUF); 308c825d4dcSJohn Baldwin w->res = newarray; 309c825d4dcSJohn Baldwin w->count += count; 310c825d4dcSJohn Baldwin 311c825d4dcSJohn Baldwin for (i = 0; i < count; i++) { 312c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, rman_get_start(res[i]), 313c825d4dcSJohn Baldwin rman_get_end(res[i])); 314c825d4dcSJohn Baldwin if (error) 315c825d4dcSJohn Baldwin panic("Failed to add resource to rman"); 316c825d4dcSJohn Baldwin } 317c825d4dcSJohn Baldwin } 318c825d4dcSJohn Baldwin 3192dd1bdf1SJustin Hibbits typedef void (nonisa_callback)(rman_res_t start, rman_res_t end, void *arg); 320c825d4dcSJohn Baldwin 321c825d4dcSJohn Baldwin static void 3222dd1bdf1SJustin Hibbits pcib_walk_nonisa_ranges(rman_res_t start, rman_res_t end, nonisa_callback *cb, 323c825d4dcSJohn Baldwin void *arg) 324c825d4dcSJohn Baldwin { 3252dd1bdf1SJustin Hibbits rman_res_t next_end; 326c825d4dcSJohn Baldwin 327c825d4dcSJohn Baldwin /* 328c825d4dcSJohn Baldwin * If start is within an ISA alias range, move up to the start 329c825d4dcSJohn Baldwin * of the next non-alias range. As a special case, addresses 330c825d4dcSJohn Baldwin * in the range 0x000 - 0x0ff should also be skipped since 331c825d4dcSJohn Baldwin * those are used for various system I/O devices in ISA 332c825d4dcSJohn Baldwin * systems. 333c825d4dcSJohn Baldwin */ 334c825d4dcSJohn Baldwin if (start <= 65535) { 335c825d4dcSJohn Baldwin if (start < 0x100 || (start & 0x300) != 0) { 336c825d4dcSJohn Baldwin start &= ~0x3ff; 337c825d4dcSJohn Baldwin start += 0x400; 338c825d4dcSJohn Baldwin } 339c825d4dcSJohn Baldwin } 340c825d4dcSJohn Baldwin 341c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */ 342c825d4dcSJohn Baldwin while (start <= MIN(end, 65535)) { 343c825d4dcSJohn Baldwin next_end = MIN(start | 0xff, end); 344c825d4dcSJohn Baldwin cb(start, next_end, arg); 345c825d4dcSJohn Baldwin start += 0x400; 346c825d4dcSJohn Baldwin } 347c825d4dcSJohn Baldwin 348c825d4dcSJohn Baldwin if (start <= end) 349c825d4dcSJohn Baldwin cb(start, end, arg); 350c825d4dcSJohn Baldwin } 351c825d4dcSJohn Baldwin 352c825d4dcSJohn Baldwin static void 3532dd1bdf1SJustin Hibbits count_ranges(rman_res_t start, rman_res_t end, void *arg) 354c825d4dcSJohn Baldwin { 355c825d4dcSJohn Baldwin int *countp; 356c825d4dcSJohn Baldwin 357c825d4dcSJohn Baldwin countp = arg; 358c825d4dcSJohn Baldwin (*countp)++; 359c825d4dcSJohn Baldwin } 360c825d4dcSJohn Baldwin 361c825d4dcSJohn Baldwin struct alloc_state { 362c825d4dcSJohn Baldwin struct resource **res; 363c825d4dcSJohn Baldwin struct pcib_softc *sc; 364c825d4dcSJohn Baldwin int count, error; 365c825d4dcSJohn Baldwin }; 366c825d4dcSJohn Baldwin 367c825d4dcSJohn Baldwin static void 3682dd1bdf1SJustin Hibbits alloc_ranges(rman_res_t start, rman_res_t end, void *arg) 369c825d4dcSJohn Baldwin { 370c825d4dcSJohn Baldwin struct alloc_state *as; 371c825d4dcSJohn Baldwin struct pcib_window *w; 372c825d4dcSJohn Baldwin int rid; 373c825d4dcSJohn Baldwin 374c825d4dcSJohn Baldwin as = arg; 375c825d4dcSJohn Baldwin if (as->error != 0) 376c825d4dcSJohn Baldwin return; 377c825d4dcSJohn Baldwin 378c825d4dcSJohn Baldwin w = &as->sc->io; 379c825d4dcSJohn Baldwin rid = w->reg; 380c825d4dcSJohn Baldwin if (bootverbose) 381c825d4dcSJohn Baldwin device_printf(as->sc->dev, 382da1b038aSJustin Hibbits "allocating non-ISA range %#jx-%#jx\n", start, end); 383c825d4dcSJohn Baldwin as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT, 384c825d4dcSJohn Baldwin &rid, start, end, end - start + 1, 0); 385c825d4dcSJohn Baldwin if (as->res[as->count] == NULL) 386c825d4dcSJohn Baldwin as->error = ENXIO; 387c825d4dcSJohn Baldwin else 388c825d4dcSJohn Baldwin as->count++; 389c825d4dcSJohn Baldwin } 390c825d4dcSJohn Baldwin 391c825d4dcSJohn Baldwin static int 3922dd1bdf1SJustin Hibbits pcib_alloc_nonisa_ranges(struct pcib_softc *sc, rman_res_t start, rman_res_t end) 393c825d4dcSJohn Baldwin { 394c825d4dcSJohn Baldwin struct alloc_state as; 395c825d4dcSJohn Baldwin int i, new_count; 396c825d4dcSJohn Baldwin 397c825d4dcSJohn Baldwin /* First, see how many ranges we need. */ 398c825d4dcSJohn Baldwin new_count = 0; 399c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count); 400c825d4dcSJohn Baldwin 401c825d4dcSJohn Baldwin /* Second, allocate the ranges. */ 402c825d4dcSJohn Baldwin as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF, 403c825d4dcSJohn Baldwin M_WAITOK); 404c825d4dcSJohn Baldwin as.sc = sc; 405c825d4dcSJohn Baldwin as.count = 0; 406c825d4dcSJohn Baldwin as.error = 0; 407c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as); 408c825d4dcSJohn Baldwin if (as.error != 0) { 409c825d4dcSJohn Baldwin for (i = 0; i < as.count; i++) 410c825d4dcSJohn Baldwin bus_release_resource(sc->dev, SYS_RES_IOPORT, 411c825d4dcSJohn Baldwin sc->io.reg, as.res[i]); 412c825d4dcSJohn Baldwin free(as.res, M_DEVBUF); 413c825d4dcSJohn Baldwin return (as.error); 414c825d4dcSJohn Baldwin } 415c825d4dcSJohn Baldwin KASSERT(as.count == new_count, ("%s: count mismatch", __func__)); 416c825d4dcSJohn Baldwin 417c825d4dcSJohn Baldwin /* Third, add the ranges to the window. */ 418c825d4dcSJohn Baldwin pcib_add_window_resources(&sc->io, as.res, as.count); 419c825d4dcSJohn Baldwin free(as.res, M_DEVBUF); 420c825d4dcSJohn Baldwin return (0); 421c825d4dcSJohn Baldwin } 422c825d4dcSJohn Baldwin 42383c41143SJohn Baldwin static void 42483c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type, 42583c41143SJohn Baldwin int flags, pci_addr_t max_address) 42683c41143SJohn Baldwin { 427c825d4dcSJohn Baldwin struct resource *res; 42883c41143SJohn Baldwin char buf[64]; 42983c41143SJohn Baldwin int error, rid; 43083c41143SJohn Baldwin 43189977ce2SJustin Hibbits if (max_address != (rman_res_t)max_address) 432534ccd7bSJustin Hibbits max_address = ~0; 43383c41143SJohn Baldwin w->rman.rm_start = 0; 43483c41143SJohn Baldwin w->rman.rm_end = max_address; 43583c41143SJohn Baldwin w->rman.rm_type = RMAN_ARRAY; 43683c41143SJohn Baldwin snprintf(buf, sizeof(buf), "%s %s window", 43783c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name); 43883c41143SJohn Baldwin w->rman.rm_descr = strdup(buf, M_DEVBUF); 43983c41143SJohn Baldwin error = rman_init(&w->rman); 44083c41143SJohn Baldwin if (error) 44183c41143SJohn Baldwin panic("Failed to initialize %s %s rman", 44283c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name); 44383c41143SJohn Baldwin 44483c41143SJohn Baldwin if (!pcib_is_window_open(w)) 44583c41143SJohn Baldwin return; 44683c41143SJohn Baldwin 44783c41143SJohn Baldwin if (w->base > max_address || w->limit > max_address) { 44883c41143SJohn Baldwin device_printf(sc->dev, 44983c41143SJohn Baldwin "initial %s window has too many bits, ignoring\n", w->name); 45083c41143SJohn Baldwin return; 45183c41143SJohn Baldwin } 452c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE) 453c825d4dcSJohn Baldwin (void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit); 454c825d4dcSJohn Baldwin else { 45583c41143SJohn Baldwin rid = w->reg; 456c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit, 45783c41143SJohn Baldwin w->limit - w->base + 1, flags); 458c825d4dcSJohn Baldwin if (res != NULL) 459c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 460c825d4dcSJohn Baldwin } 46183c41143SJohn Baldwin if (w->res == NULL) { 46283c41143SJohn Baldwin device_printf(sc->dev, 46383c41143SJohn Baldwin "failed to allocate initial %s window: %#jx-%#jx\n", 46483c41143SJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 46583c41143SJohn Baldwin w->base = max_address; 46683c41143SJohn Baldwin w->limit = 0; 46783c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 46883c41143SJohn Baldwin return; 46983c41143SJohn Baldwin } 47083c41143SJohn Baldwin pcib_activate_window(sc, type); 47183c41143SJohn Baldwin } 47283c41143SJohn Baldwin 47383c41143SJohn Baldwin /* 47483c41143SJohn Baldwin * Initialize I/O windows. 47583c41143SJohn Baldwin */ 47683c41143SJohn Baldwin static void 47783c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc) 47883c41143SJohn Baldwin { 47983c41143SJohn Baldwin pci_addr_t max; 48083c41143SJohn Baldwin device_t dev; 48183c41143SJohn Baldwin uint32_t val; 48283c41143SJohn Baldwin 48383c41143SJohn Baldwin dev = sc->dev; 48483c41143SJohn Baldwin 4850070c94bSJohn Baldwin if (pci_clear_pcib) { 486809923caSJustin Hibbits pcib_bridge_init(dev); 4870070c94bSJohn Baldwin } 4880070c94bSJohn Baldwin 48983c41143SJohn Baldwin /* Determine if the I/O port window is implemented. */ 49083c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 49183c41143SJohn Baldwin if (val == 0) { 49283c41143SJohn Baldwin /* 49383c41143SJohn Baldwin * If 'val' is zero, then only 16-bits of I/O space 49483c41143SJohn Baldwin * are supported. 49583c41143SJohn Baldwin */ 49683c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 49783c41143SJohn Baldwin if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) { 49883c41143SJohn Baldwin sc->io.valid = 1; 49983c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0, 1); 50083c41143SJohn Baldwin } 50183c41143SJohn Baldwin } else 50283c41143SJohn Baldwin sc->io.valid = 1; 50383c41143SJohn Baldwin 50483c41143SJohn Baldwin /* Read the existing I/O port window. */ 50583c41143SJohn Baldwin if (sc->io.valid) { 50683c41143SJohn Baldwin sc->io.reg = PCIR_IOBASEL_1; 50783c41143SJohn Baldwin sc->io.step = 12; 50883c41143SJohn Baldwin sc->io.mask = WIN_IO; 50983c41143SJohn Baldwin sc->io.name = "I/O port"; 51083c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 51183c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE( 51283c41143SJohn Baldwin pci_read_config(dev, PCIR_IOBASEH_1, 2), val); 51383c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT( 51483c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITH_1, 2), 51583c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 51683c41143SJohn Baldwin max = 0xffffffff; 51783c41143SJohn Baldwin } else { 51883c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE(0, val); 51983c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT(0, 52083c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 52183c41143SJohn Baldwin max = 0xffff; 52283c41143SJohn Baldwin } 52383c41143SJohn Baldwin pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max); 52483c41143SJohn Baldwin } 52583c41143SJohn Baldwin 52683c41143SJohn Baldwin /* Read the existing memory window. */ 52783c41143SJohn Baldwin sc->mem.valid = 1; 52883c41143SJohn Baldwin sc->mem.reg = PCIR_MEMBASE_1; 52983c41143SJohn Baldwin sc->mem.step = 20; 53083c41143SJohn Baldwin sc->mem.mask = WIN_MEM; 53183c41143SJohn Baldwin sc->mem.name = "memory"; 53283c41143SJohn Baldwin sc->mem.base = PCI_PPBMEMBASE(0, 53383c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMBASE_1, 2)); 53483c41143SJohn Baldwin sc->mem.limit = PCI_PPBMEMLIMIT(0, 53583c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 53683c41143SJohn Baldwin pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff); 53783c41143SJohn Baldwin 53883c41143SJohn Baldwin /* Determine if the prefetchable memory window is implemented. */ 53983c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 54083c41143SJohn Baldwin if (val == 0) { 54183c41143SJohn Baldwin /* 54283c41143SJohn Baldwin * If 'val' is zero, then only 32-bits of memory space 54383c41143SJohn Baldwin * are supported. 54483c41143SJohn Baldwin */ 54583c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 54683c41143SJohn Baldwin if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) { 54783c41143SJohn Baldwin sc->pmem.valid = 1; 54883c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0, 2); 54983c41143SJohn Baldwin } 55083c41143SJohn Baldwin } else 55183c41143SJohn Baldwin sc->pmem.valid = 1; 55283c41143SJohn Baldwin 55383c41143SJohn Baldwin /* Read the existing prefetchable memory window. */ 55483c41143SJohn Baldwin if (sc->pmem.valid) { 55583c41143SJohn Baldwin sc->pmem.reg = PCIR_PMBASEL_1; 55683c41143SJohn Baldwin sc->pmem.step = 20; 55783c41143SJohn Baldwin sc->pmem.mask = WIN_PMEM; 55883c41143SJohn Baldwin sc->pmem.name = "prefetch"; 55983c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 56083c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE( 56183c41143SJohn Baldwin pci_read_config(dev, PCIR_PMBASEH_1, 4), val); 56283c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT( 56383c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITH_1, 4), 56483c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 56583c41143SJohn Baldwin max = 0xffffffffffffffff; 56683c41143SJohn Baldwin } else { 56783c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE(0, val); 56883c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT(0, 56983c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 57083c41143SJohn Baldwin max = 0xffffffff; 57183c41143SJohn Baldwin } 57283c41143SJohn Baldwin pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY, 57383c41143SJohn Baldwin RF_PREFETCHABLE, max); 57483c41143SJohn Baldwin } 57583c41143SJohn Baldwin } 57683c41143SJohn Baldwin 5776f33eaa5SJohn Baldwin static void 5786f33eaa5SJohn Baldwin pcib_release_window(struct pcib_softc *sc, struct pcib_window *w, int type) 5796f33eaa5SJohn Baldwin { 5806f33eaa5SJohn Baldwin device_t dev; 5816f33eaa5SJohn Baldwin int error, i; 5826f33eaa5SJohn Baldwin 5836f33eaa5SJohn Baldwin if (!w->valid) 5846f33eaa5SJohn Baldwin return; 5856f33eaa5SJohn Baldwin 5866f33eaa5SJohn Baldwin dev = sc->dev; 5876f33eaa5SJohn Baldwin error = rman_fini(&w->rman); 5886f33eaa5SJohn Baldwin if (error) { 5896f33eaa5SJohn Baldwin device_printf(dev, "failed to release %s rman\n", w->name); 5906f33eaa5SJohn Baldwin return; 5916f33eaa5SJohn Baldwin } 5926f33eaa5SJohn Baldwin free(__DECONST(char *, w->rman.rm_descr), M_DEVBUF); 5936f33eaa5SJohn Baldwin 5946f33eaa5SJohn Baldwin for (i = 0; i < w->count; i++) { 5956f33eaa5SJohn Baldwin error = bus_free_resource(dev, type, w->res[i]); 5966f33eaa5SJohn Baldwin if (error) 5976f33eaa5SJohn Baldwin device_printf(dev, 5986f33eaa5SJohn Baldwin "failed to release %s resource: %d\n", w->name, 5996f33eaa5SJohn Baldwin error); 6006f33eaa5SJohn Baldwin } 6016f33eaa5SJohn Baldwin free(w->res, M_DEVBUF); 6026f33eaa5SJohn Baldwin } 6036f33eaa5SJohn Baldwin 6046f33eaa5SJohn Baldwin static void 6056f33eaa5SJohn Baldwin pcib_free_windows(struct pcib_softc *sc) 6066f33eaa5SJohn Baldwin { 6076f33eaa5SJohn Baldwin 6086f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->pmem, SYS_RES_MEMORY); 6096f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->mem, SYS_RES_MEMORY); 6106f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->io, SYS_RES_IOPORT); 6116f33eaa5SJohn Baldwin } 6126f33eaa5SJohn Baldwin 6134edef187SJohn Baldwin #ifdef PCI_RES_BUS 6144edef187SJohn Baldwin /* 6154edef187SJohn Baldwin * Allocate a suitable secondary bus for this bridge if needed and 6164edef187SJohn Baldwin * initialize the resource manager for the secondary bus range. Note 6174edef187SJohn Baldwin * that the minimum count is a desired value and this may allocate a 6184edef187SJohn Baldwin * smaller range. 6194edef187SJohn Baldwin */ 6204edef187SJohn Baldwin void 6214edef187SJohn Baldwin pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count) 6224edef187SJohn Baldwin { 6234edef187SJohn Baldwin char buf[64]; 624ad6f36f8SJohn Baldwin int error, rid, sec_reg; 6254edef187SJohn Baldwin 6264edef187SJohn Baldwin switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) { 6274edef187SJohn Baldwin case PCIM_HDRTYPE_BRIDGE: 628ad6f36f8SJohn Baldwin sec_reg = PCIR_SECBUS_1; 6294edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_1; 6304edef187SJohn Baldwin break; 6314edef187SJohn Baldwin case PCIM_HDRTYPE_CARDBUS: 632ad6f36f8SJohn Baldwin sec_reg = PCIR_SECBUS_2; 6334edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_2; 6344edef187SJohn Baldwin break; 6354edef187SJohn Baldwin default: 6364edef187SJohn Baldwin panic("not a PCI bridge"); 6374edef187SJohn Baldwin } 638ad6f36f8SJohn Baldwin bus->sec = pci_read_config(dev, sec_reg, 1); 639ad6f36f8SJohn Baldwin bus->sub = pci_read_config(dev, bus->sub_reg, 1); 6404edef187SJohn Baldwin bus->dev = dev; 6414edef187SJohn Baldwin bus->rman.rm_start = 0; 6424edef187SJohn Baldwin bus->rman.rm_end = PCI_BUSMAX; 6434edef187SJohn Baldwin bus->rman.rm_type = RMAN_ARRAY; 6444edef187SJohn Baldwin snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev)); 6454edef187SJohn Baldwin bus->rman.rm_descr = strdup(buf, M_DEVBUF); 6464edef187SJohn Baldwin error = rman_init(&bus->rman); 6474edef187SJohn Baldwin if (error) 6484edef187SJohn Baldwin panic("Failed to initialize %s bus number rman", 6494edef187SJohn Baldwin device_get_nameunit(dev)); 6504edef187SJohn Baldwin 6514edef187SJohn Baldwin /* 6524edef187SJohn Baldwin * Allocate a bus range. This will return an existing bus range 6534edef187SJohn Baldwin * if one exists, or a new bus range if one does not. 6544edef187SJohn Baldwin */ 6554edef187SJohn Baldwin rid = 0; 656c47476d7SJustin Hibbits bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid, 657*36efc64aSJohn Baldwin min_count, RF_ACTIVE); 6584edef187SJohn Baldwin if (bus->res == NULL) { 6594edef187SJohn Baldwin /* 6604edef187SJohn Baldwin * Fall back to just allocating a range of a single bus 6614edef187SJohn Baldwin * number. 6624edef187SJohn Baldwin */ 663c47476d7SJustin Hibbits bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid, 664*36efc64aSJohn Baldwin 1, RF_ACTIVE); 6654edef187SJohn Baldwin } else if (rman_get_size(bus->res) < min_count) 6664edef187SJohn Baldwin /* 6674edef187SJohn Baldwin * Attempt to grow the existing range to satisfy the 6684edef187SJohn Baldwin * minimum desired count. 6694edef187SJohn Baldwin */ 6704edef187SJohn Baldwin (void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res, 6714edef187SJohn Baldwin rman_get_start(bus->res), rman_get_start(bus->res) + 6724edef187SJohn Baldwin min_count - 1); 6734edef187SJohn Baldwin 6744edef187SJohn Baldwin /* 6754edef187SJohn Baldwin * Add the initial resource to the rman. 6764edef187SJohn Baldwin */ 6774edef187SJohn Baldwin if (bus->res != NULL) { 6784edef187SJohn Baldwin error = rman_manage_region(&bus->rman, rman_get_start(bus->res), 6794edef187SJohn Baldwin rman_get_end(bus->res)); 6804edef187SJohn Baldwin if (error) 6814edef187SJohn Baldwin panic("Failed to add resource to rman"); 6824edef187SJohn Baldwin bus->sec = rman_get_start(bus->res); 6834edef187SJohn Baldwin bus->sub = rman_get_end(bus->res); 6844edef187SJohn Baldwin } 6854edef187SJohn Baldwin } 6864edef187SJohn Baldwin 6876f33eaa5SJohn Baldwin void 6886f33eaa5SJohn Baldwin pcib_free_secbus(device_t dev, struct pcib_secbus *bus) 6896f33eaa5SJohn Baldwin { 6906f33eaa5SJohn Baldwin int error; 6916f33eaa5SJohn Baldwin 6926f33eaa5SJohn Baldwin error = rman_fini(&bus->rman); 6936f33eaa5SJohn Baldwin if (error) { 6946f33eaa5SJohn Baldwin device_printf(dev, "failed to release bus number rman\n"); 6956f33eaa5SJohn Baldwin return; 6966f33eaa5SJohn Baldwin } 6976f33eaa5SJohn Baldwin free(__DECONST(char *, bus->rman.rm_descr), M_DEVBUF); 6986f33eaa5SJohn Baldwin 6996f33eaa5SJohn Baldwin error = bus_free_resource(dev, PCI_RES_BUS, bus->res); 7006f33eaa5SJohn Baldwin if (error) 7016f33eaa5SJohn Baldwin device_printf(dev, 7026f33eaa5SJohn Baldwin "failed to release bus numbers resource: %d\n", error); 7036f33eaa5SJohn Baldwin } 7046f33eaa5SJohn Baldwin 7054edef187SJohn Baldwin static struct resource * 7064edef187SJohn Baldwin pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid, 7072dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 7084edef187SJohn Baldwin { 7094edef187SJohn Baldwin struct resource *res; 7104edef187SJohn Baldwin 7114edef187SJohn Baldwin res = rman_reserve_resource(&bus->rman, start, end, count, flags, 7124edef187SJohn Baldwin child); 7134edef187SJohn Baldwin if (res == NULL) 7144edef187SJohn Baldwin return (NULL); 7154edef187SJohn Baldwin 7164edef187SJohn Baldwin if (bootverbose) 7174edef187SJohn Baldwin device_printf(bus->dev, 718da1b038aSJustin Hibbits "allocated bus range (%ju-%ju) for rid %d of %s\n", 7194edef187SJohn Baldwin rman_get_start(res), rman_get_end(res), *rid, 7204edef187SJohn Baldwin pcib_child_name(child)); 7214edef187SJohn Baldwin rman_set_rid(res, *rid); 7224edef187SJohn Baldwin return (res); 7234edef187SJohn Baldwin } 7244edef187SJohn Baldwin 7254edef187SJohn Baldwin /* 7264edef187SJohn Baldwin * Attempt to grow the secondary bus range. This is much simpler than 7274edef187SJohn Baldwin * for I/O windows as the range can only be grown by increasing 7284edef187SJohn Baldwin * subbus. 7294edef187SJohn Baldwin */ 7304edef187SJohn Baldwin static int 7312dd1bdf1SJustin Hibbits pcib_grow_subbus(struct pcib_secbus *bus, rman_res_t new_end) 7324edef187SJohn Baldwin { 7332dd1bdf1SJustin Hibbits rman_res_t old_end; 7344edef187SJohn Baldwin int error; 7354edef187SJohn Baldwin 7364edef187SJohn Baldwin old_end = rman_get_end(bus->res); 7374edef187SJohn Baldwin KASSERT(new_end > old_end, ("attempt to shrink subbus")); 7384edef187SJohn Baldwin error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res, 7394edef187SJohn Baldwin rman_get_start(bus->res), new_end); 7404edef187SJohn Baldwin if (error) 7414edef187SJohn Baldwin return (error); 7424edef187SJohn Baldwin if (bootverbose) 743da1b038aSJustin Hibbits device_printf(bus->dev, "grew bus range to %ju-%ju\n", 7444edef187SJohn Baldwin rman_get_start(bus->res), rman_get_end(bus->res)); 7454edef187SJohn Baldwin error = rman_manage_region(&bus->rman, old_end + 1, 7464edef187SJohn Baldwin rman_get_end(bus->res)); 7474edef187SJohn Baldwin if (error) 7484edef187SJohn Baldwin panic("Failed to add resource to rman"); 7494edef187SJohn Baldwin bus->sub = rman_get_end(bus->res); 7504edef187SJohn Baldwin pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1); 7514edef187SJohn Baldwin return (0); 7524edef187SJohn Baldwin } 7534edef187SJohn Baldwin 7544edef187SJohn Baldwin struct resource * 7554edef187SJohn Baldwin pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid, 7562dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 7574edef187SJohn Baldwin { 7584edef187SJohn Baldwin struct resource *res; 7592dd1bdf1SJustin Hibbits rman_res_t start_free, end_free, new_end; 7604edef187SJohn Baldwin 7614edef187SJohn Baldwin /* 7624edef187SJohn Baldwin * First, see if the request can be satisified by the existing 7634edef187SJohn Baldwin * bus range. 7644edef187SJohn Baldwin */ 7654edef187SJohn Baldwin res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags); 7664edef187SJohn Baldwin if (res != NULL) 7674edef187SJohn Baldwin return (res); 7684edef187SJohn Baldwin 7694edef187SJohn Baldwin /* 7704edef187SJohn Baldwin * Figure out a range to grow the bus range. First, find the 7714edef187SJohn Baldwin * first bus number after the last allocated bus in the rman and 7724edef187SJohn Baldwin * enforce that as a minimum starting point for the range. 7734edef187SJohn Baldwin */ 7744edef187SJohn Baldwin if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 || 7754edef187SJohn Baldwin end_free != bus->sub) 7764edef187SJohn Baldwin start_free = bus->sub + 1; 7774edef187SJohn Baldwin if (start_free < start) 7784edef187SJohn Baldwin start_free = start; 7794edef187SJohn Baldwin new_end = start_free + count - 1; 7804edef187SJohn Baldwin 7814edef187SJohn Baldwin /* 7824edef187SJohn Baldwin * See if this new range would satisfy the request if it 7834edef187SJohn Baldwin * succeeds. 7844edef187SJohn Baldwin */ 7854edef187SJohn Baldwin if (new_end > end) 7864edef187SJohn Baldwin return (NULL); 7874edef187SJohn Baldwin 7884edef187SJohn Baldwin /* Finally, attempt to grow the existing resource. */ 7894edef187SJohn Baldwin if (bootverbose) { 7904edef187SJohn Baldwin device_printf(bus->dev, 791da1b038aSJustin Hibbits "attempting to grow bus range for %ju buses\n", count); 792da1b038aSJustin Hibbits printf("\tback candidate range: %ju-%ju\n", start_free, 7934edef187SJohn Baldwin new_end); 7944edef187SJohn Baldwin } 7954edef187SJohn Baldwin if (pcib_grow_subbus(bus, new_end) == 0) 7964edef187SJohn Baldwin return (pcib_suballoc_bus(bus, child, rid, start, end, count, 7974edef187SJohn Baldwin flags)); 7984edef187SJohn Baldwin return (NULL); 7994edef187SJohn Baldwin } 8004edef187SJohn Baldwin #endif 8014edef187SJohn Baldwin 80283c41143SJohn Baldwin #else 80383c41143SJohn Baldwin 804bb0d0a8eSMike Smith /* 805b0a2d4b8SWarner Losh * Is the prefetch window open (eg, can we allocate memory in it?) 806b0a2d4b8SWarner Losh */ 807b0a2d4b8SWarner Losh static int 808b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc) 809b0a2d4b8SWarner Losh { 810b0a2d4b8SWarner Losh return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit); 811b0a2d4b8SWarner Losh } 812b0a2d4b8SWarner Losh 813b0a2d4b8SWarner Losh /* 814b0a2d4b8SWarner Losh * Is the nonprefetch window open (eg, can we allocate memory in it?) 815b0a2d4b8SWarner Losh */ 816b0a2d4b8SWarner Losh static int 817b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc) 818b0a2d4b8SWarner Losh { 819b0a2d4b8SWarner Losh return (sc->membase > 0 && sc->membase < sc->memlimit); 820b0a2d4b8SWarner Losh } 821b0a2d4b8SWarner Losh 822b0a2d4b8SWarner Losh /* 823b0a2d4b8SWarner Losh * Is the io window open (eg, can we allocate ports in it?) 824b0a2d4b8SWarner Losh */ 825b0a2d4b8SWarner Losh static int 826b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc) 827b0a2d4b8SWarner Losh { 828b0a2d4b8SWarner Losh return (sc->iobase > 0 && sc->iobase < sc->iolimit); 829b0a2d4b8SWarner Losh } 830b0a2d4b8SWarner Losh 831b0a2d4b8SWarner Losh /* 832e36af292SJung-uk Kim * Get current I/O decode. 833e36af292SJung-uk Kim */ 834e36af292SJung-uk Kim static void 835e36af292SJung-uk Kim pcib_get_io_decode(struct pcib_softc *sc) 836e36af292SJung-uk Kim { 837e36af292SJung-uk Kim device_t dev; 838e36af292SJung-uk Kim uint32_t iolow; 839e36af292SJung-uk Kim 840e36af292SJung-uk Kim dev = sc->dev; 841e36af292SJung-uk Kim 842e36af292SJung-uk Kim iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1); 843e36af292SJung-uk Kim if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 844e36af292SJung-uk Kim sc->iobase = PCI_PPBIOBASE( 845e36af292SJung-uk Kim pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow); 846e36af292SJung-uk Kim else 847e36af292SJung-uk Kim sc->iobase = PCI_PPBIOBASE(0, iolow); 848e36af292SJung-uk Kim 849e36af292SJung-uk Kim iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1); 850e36af292SJung-uk Kim if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 851e36af292SJung-uk Kim sc->iolimit = PCI_PPBIOLIMIT( 852e36af292SJung-uk Kim pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow); 853e36af292SJung-uk Kim else 854e36af292SJung-uk Kim sc->iolimit = PCI_PPBIOLIMIT(0, iolow); 855e36af292SJung-uk Kim } 856e36af292SJung-uk Kim 857e36af292SJung-uk Kim /* 858e36af292SJung-uk Kim * Get current memory decode. 859e36af292SJung-uk Kim */ 860e36af292SJung-uk Kim static void 861e36af292SJung-uk Kim pcib_get_mem_decode(struct pcib_softc *sc) 862e36af292SJung-uk Kim { 863e36af292SJung-uk Kim device_t dev; 864e36af292SJung-uk Kim pci_addr_t pmemlow; 865e36af292SJung-uk Kim 866e36af292SJung-uk Kim dev = sc->dev; 867e36af292SJung-uk Kim 868e36af292SJung-uk Kim sc->membase = PCI_PPBMEMBASE(0, 869e36af292SJung-uk Kim pci_read_config(dev, PCIR_MEMBASE_1, 2)); 870e36af292SJung-uk Kim sc->memlimit = PCI_PPBMEMLIMIT(0, 871e36af292SJung-uk Kim pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 872e36af292SJung-uk Kim 873e36af292SJung-uk Kim pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2); 874e36af292SJung-uk Kim if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 875e36af292SJung-uk Kim sc->pmembase = PCI_PPBMEMBASE( 876e36af292SJung-uk Kim pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow); 877e36af292SJung-uk Kim else 878e36af292SJung-uk Kim sc->pmembase = PCI_PPBMEMBASE(0, pmemlow); 879e36af292SJung-uk Kim 880e36af292SJung-uk Kim pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2); 881e36af292SJung-uk Kim if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 882e36af292SJung-uk Kim sc->pmemlimit = PCI_PPBMEMLIMIT( 883e36af292SJung-uk Kim pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow); 884e36af292SJung-uk Kim else 885e36af292SJung-uk Kim sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow); 886e36af292SJung-uk Kim } 887e36af292SJung-uk Kim 888e36af292SJung-uk Kim /* 889e36af292SJung-uk Kim * Restore previous I/O decode. 890e36af292SJung-uk Kim */ 891e36af292SJung-uk Kim static void 892e36af292SJung-uk Kim pcib_set_io_decode(struct pcib_softc *sc) 893e36af292SJung-uk Kim { 894e36af292SJung-uk Kim device_t dev; 895e36af292SJung-uk Kim uint32_t iohi; 896e36af292SJung-uk Kim 897e36af292SJung-uk Kim dev = sc->dev; 898e36af292SJung-uk Kim 899e36af292SJung-uk Kim iohi = sc->iobase >> 16; 900e36af292SJung-uk Kim if (iohi > 0) 901e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2); 902e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1); 903e36af292SJung-uk Kim 904e36af292SJung-uk Kim iohi = sc->iolimit >> 16; 905e36af292SJung-uk Kim if (iohi > 0) 906e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2); 907e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1); 908e36af292SJung-uk Kim } 909e36af292SJung-uk Kim 910e36af292SJung-uk Kim /* 911e36af292SJung-uk Kim * Restore previous memory decode. 912e36af292SJung-uk Kim */ 913e36af292SJung-uk Kim static void 914e36af292SJung-uk Kim pcib_set_mem_decode(struct pcib_softc *sc) 915e36af292SJung-uk Kim { 916e36af292SJung-uk Kim device_t dev; 917e36af292SJung-uk Kim pci_addr_t pmemhi; 918e36af292SJung-uk Kim 919e36af292SJung-uk Kim dev = sc->dev; 920e36af292SJung-uk Kim 921e36af292SJung-uk Kim pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2); 922e36af292SJung-uk Kim pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2); 923e36af292SJung-uk Kim 924e36af292SJung-uk Kim pmemhi = sc->pmembase >> 32; 925e36af292SJung-uk Kim if (pmemhi > 0) 926e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4); 927e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2); 928e36af292SJung-uk Kim 929e36af292SJung-uk Kim pmemhi = sc->pmemlimit >> 32; 930e36af292SJung-uk Kim if (pmemhi > 0) 931e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4); 932e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2); 933e36af292SJung-uk Kim } 93483c41143SJohn Baldwin #endif 935e36af292SJung-uk Kim 93682cb5c3bSJohn Baldwin #ifdef PCI_HP 93782cb5c3bSJohn Baldwin /* 93882cb5c3bSJohn Baldwin * PCI-express HotPlug support. 93982cb5c3bSJohn Baldwin */ 94025a57bd6SJohn Baldwin static int pci_enable_pcie_hp = 1; 94125a57bd6SJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_hp, CTLFLAG_RDTUN, 94225a57bd6SJohn Baldwin &pci_enable_pcie_hp, 0, 94325a57bd6SJohn Baldwin "Enable support for native PCI-express HotPlug."); 94425a57bd6SJohn Baldwin 94512588ce0SAndriy Gapon TASKQUEUE_DEFINE_THREAD(pci_hp); 94612588ce0SAndriy Gapon 94782cb5c3bSJohn Baldwin static void 94882cb5c3bSJohn Baldwin pcib_probe_hotplug(struct pcib_softc *sc) 94982cb5c3bSJohn Baldwin { 95082cb5c3bSJohn Baldwin device_t dev; 95137290148SEric van Gyzen uint32_t link_cap; 952991d431fSEric van Gyzen uint16_t link_sta, slot_sta; 95382cb5c3bSJohn Baldwin 95425a57bd6SJohn Baldwin if (!pci_enable_pcie_hp) 95525a57bd6SJohn Baldwin return; 95625a57bd6SJohn Baldwin 95782cb5c3bSJohn Baldwin dev = sc->dev; 95882cb5c3bSJohn Baldwin if (pci_find_cap(dev, PCIY_EXPRESS, NULL) != 0) 95982cb5c3bSJohn Baldwin return; 96082cb5c3bSJohn Baldwin 96182cb5c3bSJohn Baldwin if (!(pcie_read_config(dev, PCIER_FLAGS, 2) & PCIEM_FLAGS_SLOT)) 96282cb5c3bSJohn Baldwin return; 96382cb5c3bSJohn Baldwin 96482cb5c3bSJohn Baldwin sc->pcie_slot_cap = pcie_read_config(dev, PCIER_SLOT_CAP, 4); 96582cb5c3bSJohn Baldwin 966991d431fSEric van Gyzen if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_HPC) == 0) 9672611037cSJohn Baldwin return; 96837290148SEric van Gyzen link_cap = pcie_read_config(dev, PCIER_LINK_CAP, 4); 96937290148SEric van Gyzen if ((link_cap & PCIEM_LINK_CAP_DL_ACTIVE) == 0) 9702ffb582aSJohn Baldwin return; 9712611037cSJohn Baldwin 972991d431fSEric van Gyzen /* 973991d431fSEric van Gyzen * Some devices report that they have an MRL when they actually 974991d431fSEric van Gyzen * do not. Since they always report that the MRL is open, child 975991d431fSEric van Gyzen * devices would be ignored. Try to detect these devices and 976991d431fSEric van Gyzen * ignore their claim of HotPlug support. 977991d431fSEric van Gyzen * 978991d431fSEric van Gyzen * If there is an open MRL but the Data Link Layer is active, 979991d431fSEric van Gyzen * the MRL is not real. 980991d431fSEric van Gyzen */ 98137290148SEric van Gyzen if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) != 0) { 982991d431fSEric van Gyzen link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 983991d431fSEric van Gyzen slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 984991d431fSEric van Gyzen if ((slot_sta & PCIEM_SLOT_STA_MRLSS) != 0 && 985991d431fSEric van Gyzen (link_sta & PCIEM_LINK_STA_DL_ACTIVE) != 0) { 986991d431fSEric van Gyzen return; 987991d431fSEric van Gyzen } 988991d431fSEric van Gyzen } 989991d431fSEric van Gyzen 99028586889SWarner Losh /* 99128586889SWarner Losh * Now that we're sure we want to do hot plug, ask the 99228586889SWarner Losh * firmware, if any, if that's OK. 99328586889SWarner Losh */ 9941ffd07bdSJohn Baldwin if (pcib_request_feature(dev, PCI_FEATURE_HP) != 0) { 99528586889SWarner Losh if (bootverbose) 99628586889SWarner Losh device_printf(dev, "Unable to activate hot plug feature.\n"); 99728586889SWarner Losh return; 99828586889SWarner Losh } 99928586889SWarner Losh 100082cb5c3bSJohn Baldwin sc->flags |= PCIB_HOTPLUG; 100182cb5c3bSJohn Baldwin } 100282cb5c3bSJohn Baldwin 100382cb5c3bSJohn Baldwin /* 100482cb5c3bSJohn Baldwin * Send a HotPlug command to the slot control register. If this slot 100507454911SJohn Baldwin * uses command completion interrupts and a previous command is still 100607454911SJohn Baldwin * in progress, then the command is dropped. Once the previous 100707454911SJohn Baldwin * command completes or times out, pcib_pcie_hotplug_update() will be 100807454911SJohn Baldwin * invoked to post a new command based on the slot's state at that 100907454911SJohn Baldwin * time. 101082cb5c3bSJohn Baldwin */ 101182cb5c3bSJohn Baldwin static void 101282cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(struct pcib_softc *sc, uint16_t val, uint16_t mask) 101382cb5c3bSJohn Baldwin { 101482cb5c3bSJohn Baldwin device_t dev; 101582cb5c3bSJohn Baldwin uint16_t ctl, new; 101682cb5c3bSJohn Baldwin 101782cb5c3bSJohn Baldwin dev = sc->dev; 101882cb5c3bSJohn Baldwin 101907454911SJohn Baldwin if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) 102007454911SJohn Baldwin return; 102107454911SJohn Baldwin 102282cb5c3bSJohn Baldwin ctl = pcie_read_config(dev, PCIER_SLOT_CTL, 2); 102382cb5c3bSJohn Baldwin new = (ctl & ~mask) | val; 102407454911SJohn Baldwin if (new == ctl) 102507454911SJohn Baldwin return; 1026991d431fSEric van Gyzen if (bootverbose) 1027991d431fSEric van Gyzen device_printf(dev, "HotPlug command: %04x -> %04x\n", ctl, new); 102807454911SJohn Baldwin pcie_write_config(dev, PCIER_SLOT_CTL, new, 2); 10296f33eaa5SJohn Baldwin if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS) && 10306f33eaa5SJohn Baldwin (ctl & new) & PCIEM_SLOT_CTL_CCIE) { 103182cb5c3bSJohn Baldwin sc->flags |= PCIB_HOTPLUG_CMD_PENDING; 103282cb5c3bSJohn Baldwin if (!cold) 1033fa3b03d3SAlexander Motin taskqueue_enqueue_timeout(taskqueue_pci_hp, 1034fa3b03d3SAlexander Motin &sc->pcie_cc_task, hz); 103582cb5c3bSJohn Baldwin } 103682cb5c3bSJohn Baldwin } 103782cb5c3bSJohn Baldwin 103882cb5c3bSJohn Baldwin static void 103982cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(struct pcib_softc *sc) 104082cb5c3bSJohn Baldwin { 104182cb5c3bSJohn Baldwin device_t dev; 104282cb5c3bSJohn Baldwin 104382cb5c3bSJohn Baldwin dev = sc->dev; 104482cb5c3bSJohn Baldwin 104582cb5c3bSJohn Baldwin if (bootverbose) 104682cb5c3bSJohn Baldwin device_printf(dev, "Command Completed\n"); 104782cb5c3bSJohn Baldwin if (!(sc->flags & PCIB_HOTPLUG_CMD_PENDING)) 104882cb5c3bSJohn Baldwin return; 1049fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_cc_task, NULL); 105082cb5c3bSJohn Baldwin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 10516f33eaa5SJohn Baldwin wakeup(sc); 105282cb5c3bSJohn Baldwin } 105382cb5c3bSJohn Baldwin 105482cb5c3bSJohn Baldwin /* 105582cb5c3bSJohn Baldwin * Returns true if a card is fully inserted from the user's 105682cb5c3bSJohn Baldwin * perspective. It may not yet be ready for access, but the driver 105782cb5c3bSJohn Baldwin * can now start enabling access if necessary. 105882cb5c3bSJohn Baldwin */ 105982cb5c3bSJohn Baldwin static bool 106082cb5c3bSJohn Baldwin pcib_hotplug_inserted(struct pcib_softc *sc) 106182cb5c3bSJohn Baldwin { 106282cb5c3bSJohn Baldwin 106382cb5c3bSJohn Baldwin /* Pretend the card isn't present if a detach is forced. */ 106482cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACHING) 106582cb5c3bSJohn Baldwin return (false); 106682cb5c3bSJohn Baldwin 106782cb5c3bSJohn Baldwin /* Card must be present in the slot. */ 106882cb5c3bSJohn Baldwin if ((sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS) == 0) 106982cb5c3bSJohn Baldwin return (false); 107082cb5c3bSJohn Baldwin 107182cb5c3bSJohn Baldwin /* A power fault implicitly turns off power to the slot. */ 107282cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD) 107382cb5c3bSJohn Baldwin return (false); 107482cb5c3bSJohn Baldwin 107582cb5c3bSJohn Baldwin /* If the MRL is disengaged, the slot is powered off. */ 107682cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP && 107782cb5c3bSJohn Baldwin (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS) != 0) 107882cb5c3bSJohn Baldwin return (false); 107982cb5c3bSJohn Baldwin 108082cb5c3bSJohn Baldwin return (true); 108182cb5c3bSJohn Baldwin } 108282cb5c3bSJohn Baldwin 108382cb5c3bSJohn Baldwin /* 108482cb5c3bSJohn Baldwin * Returns -1 if the card is fully inserted, powered, and ready for 108582cb5c3bSJohn Baldwin * access. Otherwise, returns 0. 108682cb5c3bSJohn Baldwin */ 108782cb5c3bSJohn Baldwin static int 108882cb5c3bSJohn Baldwin pcib_hotplug_present(struct pcib_softc *sc) 108982cb5c3bSJohn Baldwin { 109082cb5c3bSJohn Baldwin 109182cb5c3bSJohn Baldwin /* Card must be inserted. */ 109282cb5c3bSJohn Baldwin if (!pcib_hotplug_inserted(sc)) 109382cb5c3bSJohn Baldwin return (0); 109482cb5c3bSJohn Baldwin 109582cb5c3bSJohn Baldwin /* Require the Data Link Layer to be active. */ 109682cb5c3bSJohn Baldwin if (!(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE)) 109782cb5c3bSJohn Baldwin return (0); 109882cb5c3bSJohn Baldwin 109982cb5c3bSJohn Baldwin return (-1); 110082cb5c3bSJohn Baldwin } 110182cb5c3bSJohn Baldwin 1102a58536b9SAlexander Motin static int pci_enable_pcie_ei = 0; 1103a58536b9SAlexander Motin SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_ei, CTLFLAG_RWTUN, 1104a58536b9SAlexander Motin &pci_enable_pcie_ei, 0, 1105a58536b9SAlexander Motin "Enable support for PCI-express Electromechanical Interlock."); 1106a58536b9SAlexander Motin 110782cb5c3bSJohn Baldwin static void 110882cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(struct pcib_softc *sc, uint16_t val, uint16_t mask, 110982cb5c3bSJohn Baldwin bool schedule_task) 111082cb5c3bSJohn Baldwin { 1111a1566487SEric van Gyzen bool card_inserted, ei_engaged; 111282cb5c3bSJohn Baldwin 1113991d431fSEric van Gyzen /* Clear DETACHING if Presence Detect has cleared. */ 111482cb5c3bSJohn Baldwin if ((sc->pcie_slot_sta & (PCIEM_SLOT_STA_PDC | PCIEM_SLOT_STA_PDS)) == 111582cb5c3bSJohn Baldwin PCIEM_SLOT_STA_PDC) 111682cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACHING; 111782cb5c3bSJohn Baldwin 111882cb5c3bSJohn Baldwin card_inserted = pcib_hotplug_inserted(sc); 111982cb5c3bSJohn Baldwin 112082cb5c3bSJohn Baldwin /* Turn the power indicator on if a card is inserted. */ 112182cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PIP) { 112282cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_PIC; 112382cb5c3bSJohn Baldwin if (card_inserted) 112482cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_ON; 112582cb5c3bSJohn Baldwin else if (sc->flags & PCIB_DETACH_PENDING) 112682cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_BLINK; 112782cb5c3bSJohn Baldwin else 112882cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_OFF; 112982cb5c3bSJohn Baldwin } 113082cb5c3bSJohn Baldwin 113182cb5c3bSJohn Baldwin /* Turn the power on via the Power Controller if a card is inserted. */ 113282cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) { 113382cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_PCC; 113482cb5c3bSJohn Baldwin if (card_inserted) 113582cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PC_ON; 113682cb5c3bSJohn Baldwin else 113782cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PC_OFF; 113882cb5c3bSJohn Baldwin } 113982cb5c3bSJohn Baldwin 114082cb5c3bSJohn Baldwin /* 114182cb5c3bSJohn Baldwin * If a card is inserted, enable the Electromechanical 114282cb5c3bSJohn Baldwin * Interlock. If a card is not inserted (or we are in the 114382cb5c3bSJohn Baldwin * process of detaching), disable the Electromechanical 114482cb5c3bSJohn Baldwin * Interlock. 114582cb5c3bSJohn Baldwin */ 1146a58536b9SAlexander Motin if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP) && 1147a58536b9SAlexander Motin pci_enable_pcie_ei) { 114882cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_EIC; 1149a1566487SEric van Gyzen ei_engaged = (sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS) != 0; 1150a1566487SEric van Gyzen if (card_inserted != ei_engaged) 115182cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_EIC; 115282cb5c3bSJohn Baldwin } 115382cb5c3bSJohn Baldwin 115482cb5c3bSJohn Baldwin /* 115582cb5c3bSJohn Baldwin * Start a timer to see if the Data Link Layer times out. 1156991d431fSEric van Gyzen * Note that we only start the timer if Presence Detect or MRL Sensor 115782cb5c3bSJohn Baldwin * changed on this interrupt. Stop any scheduled timer if 115882cb5c3bSJohn Baldwin * the Data Link Layer is active. 115982cb5c3bSJohn Baldwin */ 116082cb5c3bSJohn Baldwin if (card_inserted && 116182cb5c3bSJohn Baldwin !(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) && 1162991d431fSEric van Gyzen sc->pcie_slot_sta & 1163991d431fSEric van Gyzen (PCIEM_SLOT_STA_MRLSC | PCIEM_SLOT_STA_PDC)) { 116482cb5c3bSJohn Baldwin if (cold) 116582cb5c3bSJohn Baldwin device_printf(sc->dev, 116682cb5c3bSJohn Baldwin "Data Link Layer inactive\n"); 116782cb5c3bSJohn Baldwin else 1168fa3b03d3SAlexander Motin taskqueue_enqueue_timeout(taskqueue_pci_hp, 1169fa3b03d3SAlexander Motin &sc->pcie_dll_task, hz); 117082cb5c3bSJohn Baldwin } else if (sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) 1171fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_dll_task, 1172fa3b03d3SAlexander Motin NULL); 117382cb5c3bSJohn Baldwin 117482cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(sc, val, mask); 117582cb5c3bSJohn Baldwin 117682cb5c3bSJohn Baldwin /* 1177a1566487SEric van Gyzen * During attach the child "pci" device is added synchronously; 117882cb5c3bSJohn Baldwin * otherwise, the task is scheduled to manage the child 117982cb5c3bSJohn Baldwin * device. 118082cb5c3bSJohn Baldwin */ 118182cb5c3bSJohn Baldwin if (schedule_task && 118282cb5c3bSJohn Baldwin (pcib_hotplug_present(sc) != 0) != (sc->child != NULL)) 118312588ce0SAndriy Gapon taskqueue_enqueue(taskqueue_pci_hp, &sc->pcie_hp_task); 118482cb5c3bSJohn Baldwin } 118582cb5c3bSJohn Baldwin 118682cb5c3bSJohn Baldwin static void 11878a1926c5SWarner Losh pcib_pcie_intr_hotplug(void *arg) 118882cb5c3bSJohn Baldwin { 118982cb5c3bSJohn Baldwin struct pcib_softc *sc; 119082cb5c3bSJohn Baldwin device_t dev; 1191e0235fd3SColin Percival uint16_t old_slot_sta; 119282cb5c3bSJohn Baldwin 119382cb5c3bSJohn Baldwin sc = arg; 119482cb5c3bSJohn Baldwin dev = sc->dev; 119513d700adSScott Long PCIB_HP_LOCK(sc); 1196e0235fd3SColin Percival old_slot_sta = sc->pcie_slot_sta; 119782cb5c3bSJohn Baldwin sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 119882cb5c3bSJohn Baldwin 119982cb5c3bSJohn Baldwin /* Clear the events just reported. */ 120082cb5c3bSJohn Baldwin pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2); 120182cb5c3bSJohn Baldwin 1202991d431fSEric van Gyzen if (bootverbose) 1203991d431fSEric van Gyzen device_printf(dev, "HotPlug interrupt: %#x\n", 1204991d431fSEric van Gyzen sc->pcie_slot_sta); 1205991d431fSEric van Gyzen 120682cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_ABP) { 120782cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 120882cb5c3bSJohn Baldwin device_printf(dev, 120982cb5c3bSJohn Baldwin "Attention Button Pressed: Detach Cancelled\n"); 121082cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 1211fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, 1212fa3b03d3SAlexander Motin &sc->pcie_ab_task, NULL); 1213e0235fd3SColin Percival } else if (old_slot_sta & PCIEM_SLOT_STA_PDS) { 1214e0235fd3SColin Percival /* Only initiate detach sequence if device present. */ 121582cb5c3bSJohn Baldwin device_printf(dev, 121682cb5c3bSJohn Baldwin "Attention Button Pressed: Detaching in 5 seconds\n"); 121782cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACH_PENDING; 1218fa3b03d3SAlexander Motin taskqueue_enqueue_timeout(taskqueue_pci_hp, 1219fa3b03d3SAlexander Motin &sc->pcie_ab_task, 5 * hz); 122082cb5c3bSJohn Baldwin } 122182cb5c3bSJohn Baldwin } 122282cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD) 122382cb5c3bSJohn Baldwin device_printf(dev, "Power Fault Detected\n"); 122482cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSC) 122582cb5c3bSJohn Baldwin device_printf(dev, "MRL Sensor Changed to %s\n", 122682cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS ? "open" : 122782cb5c3bSJohn Baldwin "closed"); 122882cb5c3bSJohn Baldwin if (bootverbose && sc->pcie_slot_sta & PCIEM_SLOT_STA_PDC) 1229991d431fSEric van Gyzen device_printf(dev, "Presence Detect Changed to %s\n", 123082cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS ? "card present" : 123182cb5c3bSJohn Baldwin "empty"); 123282cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_CC) 123382cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(sc); 123482cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_DLLSC) { 123582cb5c3bSJohn Baldwin sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 123682cb5c3bSJohn Baldwin if (bootverbose) 123782cb5c3bSJohn Baldwin device_printf(dev, 123882cb5c3bSJohn Baldwin "Data Link Layer State Changed to %s\n", 123982cb5c3bSJohn Baldwin sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE ? 124082cb5c3bSJohn Baldwin "active" : "inactive"); 124182cb5c3bSJohn Baldwin } 124282cb5c3bSJohn Baldwin 124382cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 124413d700adSScott Long PCIB_HP_UNLOCK(sc); 124582cb5c3bSJohn Baldwin } 124682cb5c3bSJohn Baldwin 124782cb5c3bSJohn Baldwin static void 124882cb5c3bSJohn Baldwin pcib_pcie_hotplug_task(void *context, int pending) 124982cb5c3bSJohn Baldwin { 125082cb5c3bSJohn Baldwin struct pcib_softc *sc; 125182cb5c3bSJohn Baldwin device_t dev; 125282cb5c3bSJohn Baldwin 125382cb5c3bSJohn Baldwin sc = context; 125413d700adSScott Long PCIB_HP_LOCK(sc); 125582cb5c3bSJohn Baldwin dev = sc->dev; 125682cb5c3bSJohn Baldwin if (pcib_hotplug_present(sc) != 0) { 125782cb5c3bSJohn Baldwin if (sc->child == NULL) { 125882cb5c3bSJohn Baldwin sc->child = device_add_child(dev, "pci", -1); 125982cb5c3bSJohn Baldwin bus_generic_attach(dev); 126082cb5c3bSJohn Baldwin } 126182cb5c3bSJohn Baldwin } else { 126282cb5c3bSJohn Baldwin if (sc->child != NULL) { 126382cb5c3bSJohn Baldwin if (device_delete_child(dev, sc->child) == 0) 126482cb5c3bSJohn Baldwin sc->child = NULL; 126582cb5c3bSJohn Baldwin } 126682cb5c3bSJohn Baldwin } 126713d700adSScott Long PCIB_HP_UNLOCK(sc); 126882cb5c3bSJohn Baldwin } 126982cb5c3bSJohn Baldwin 127082cb5c3bSJohn Baldwin static void 1271fa3b03d3SAlexander Motin pcib_pcie_ab_timeout(void *arg, int pending) 127282cb5c3bSJohn Baldwin { 1273fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 127482cb5c3bSJohn Baldwin 1275fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 127682cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 127782cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING; 127882cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 127982cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 128082cb5c3bSJohn Baldwin } 1281fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 128282cb5c3bSJohn Baldwin } 128382cb5c3bSJohn Baldwin 128482cb5c3bSJohn Baldwin static void 1285fa3b03d3SAlexander Motin pcib_pcie_cc_timeout(void *arg, int pending) 128682cb5c3bSJohn Baldwin { 1287fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 1288fa3b03d3SAlexander Motin device_t dev = sc->dev; 12896f33eaa5SJohn Baldwin uint16_t sta; 129082cb5c3bSJohn Baldwin 1291fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 12926f33eaa5SJohn Baldwin sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 12936f33eaa5SJohn Baldwin if (!(sta & PCIEM_SLOT_STA_CC)) { 129421e51c82SAlexander Motin device_printf(dev, "HotPlug Command Timed Out\n"); 129521e51c82SAlexander Motin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 12966f33eaa5SJohn Baldwin } else { 12976f33eaa5SJohn Baldwin device_printf(dev, 12986f33eaa5SJohn Baldwin "Missed HotPlug interrupt waiting for Command Completion\n"); 12998a1926c5SWarner Losh pcib_pcie_intr_hotplug(sc); 130082cb5c3bSJohn Baldwin } 1301fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 130282cb5c3bSJohn Baldwin } 130382cb5c3bSJohn Baldwin 130482cb5c3bSJohn Baldwin static void 1305fa3b03d3SAlexander Motin pcib_pcie_dll_timeout(void *arg, int pending) 130682cb5c3bSJohn Baldwin { 1307fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 1308fa3b03d3SAlexander Motin device_t dev = sc->dev; 130982cb5c3bSJohn Baldwin uint16_t sta; 131082cb5c3bSJohn Baldwin 1311fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 131282cb5c3bSJohn Baldwin sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 131382cb5c3bSJohn Baldwin if (!(sta & PCIEM_LINK_STA_DL_ACTIVE)) { 131482cb5c3bSJohn Baldwin device_printf(dev, 131582cb5c3bSJohn Baldwin "Timed out waiting for Data Link Layer Active\n"); 131682cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING; 131782cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 131882cb5c3bSJohn Baldwin } else if (sta != sc->pcie_link_sta) { 131982cb5c3bSJohn Baldwin device_printf(dev, 132082cb5c3bSJohn Baldwin "Missed HotPlug interrupt waiting for DLL Active\n"); 13218a1926c5SWarner Losh pcib_pcie_intr_hotplug(sc); 132282cb5c3bSJohn Baldwin } 1323fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 132482cb5c3bSJohn Baldwin } 132582cb5c3bSJohn Baldwin 132682cb5c3bSJohn Baldwin static int 132782cb5c3bSJohn Baldwin pcib_alloc_pcie_irq(struct pcib_softc *sc) 132882cb5c3bSJohn Baldwin { 132982cb5c3bSJohn Baldwin device_t dev; 1330e6b83836SJohn Baldwin int count, error, mem_rid, rid; 133182cb5c3bSJohn Baldwin 133282cb5c3bSJohn Baldwin rid = -1; 133382cb5c3bSJohn Baldwin dev = sc->dev; 133482cb5c3bSJohn Baldwin 133582cb5c3bSJohn Baldwin /* 133682cb5c3bSJohn Baldwin * For simplicity, only use MSI-X if there is a single message. 133782cb5c3bSJohn Baldwin * To support a device with multiple messages we would have to 133882cb5c3bSJohn Baldwin * use remap intr if the MSI number is not 0. 133982cb5c3bSJohn Baldwin */ 134082cb5c3bSJohn Baldwin count = pci_msix_count(dev); 134182cb5c3bSJohn Baldwin if (count == 1) { 1342e6b83836SJohn Baldwin mem_rid = pci_msix_table_bar(dev); 1343e6b83836SJohn Baldwin sc->pcie_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 1344e6b83836SJohn Baldwin &mem_rid, RF_ACTIVE); 1345e6b83836SJohn Baldwin if (sc->pcie_mem == NULL) { 1346e6b83836SJohn Baldwin device_printf(dev, 1347e6b83836SJohn Baldwin "Failed to allocate BAR for MSI-X table\n"); 1348e6b83836SJohn Baldwin } else { 134982cb5c3bSJohn Baldwin error = pci_alloc_msix(dev, &count); 135082cb5c3bSJohn Baldwin if (error == 0) 135182cb5c3bSJohn Baldwin rid = 1; 135282cb5c3bSJohn Baldwin } 1353e6b83836SJohn Baldwin } 135482cb5c3bSJohn Baldwin 135582cb5c3bSJohn Baldwin if (rid < 0 && pci_msi_count(dev) > 0) { 135682cb5c3bSJohn Baldwin count = 1; 135782cb5c3bSJohn Baldwin error = pci_alloc_msi(dev, &count); 135882cb5c3bSJohn Baldwin if (error == 0) 135982cb5c3bSJohn Baldwin rid = 1; 136082cb5c3bSJohn Baldwin } 136182cb5c3bSJohn Baldwin 136282cb5c3bSJohn Baldwin if (rid < 0) 136382cb5c3bSJohn Baldwin rid = 0; 136482cb5c3bSJohn Baldwin 136582cb5c3bSJohn Baldwin sc->pcie_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1366f14f0051SChuck Tuffli RF_ACTIVE | RF_SHAREABLE); 136782cb5c3bSJohn Baldwin if (sc->pcie_irq == NULL) { 136882cb5c3bSJohn Baldwin device_printf(dev, 136982cb5c3bSJohn Baldwin "Failed to allocate interrupt for PCI-e events\n"); 137082cb5c3bSJohn Baldwin if (rid > 0) 137182cb5c3bSJohn Baldwin pci_release_msi(dev); 137282cb5c3bSJohn Baldwin return (ENXIO); 137382cb5c3bSJohn Baldwin } 137482cb5c3bSJohn Baldwin 137513d700adSScott Long error = bus_setup_intr(dev, sc->pcie_irq, INTR_TYPE_MISC|INTR_MPSAFE, 13768a1926c5SWarner Losh NULL, pcib_pcie_intr_hotplug, sc, &sc->pcie_ihand); 137782cb5c3bSJohn Baldwin if (error) { 137882cb5c3bSJohn Baldwin device_printf(dev, "Failed to setup PCI-e interrupt handler\n"); 137982cb5c3bSJohn Baldwin bus_release_resource(dev, SYS_RES_IRQ, rid, sc->pcie_irq); 138082cb5c3bSJohn Baldwin if (rid > 0) 138182cb5c3bSJohn Baldwin pci_release_msi(dev); 138282cb5c3bSJohn Baldwin return (error); 138382cb5c3bSJohn Baldwin } 138482cb5c3bSJohn Baldwin return (0); 138582cb5c3bSJohn Baldwin } 138682cb5c3bSJohn Baldwin 13876f33eaa5SJohn Baldwin static int 13886f33eaa5SJohn Baldwin pcib_release_pcie_irq(struct pcib_softc *sc) 13896f33eaa5SJohn Baldwin { 13906f33eaa5SJohn Baldwin device_t dev; 13916f33eaa5SJohn Baldwin int error; 13926f33eaa5SJohn Baldwin 13936f33eaa5SJohn Baldwin dev = sc->dev; 13946f33eaa5SJohn Baldwin error = bus_teardown_intr(dev, sc->pcie_irq, sc->pcie_ihand); 13956f33eaa5SJohn Baldwin if (error) 13966f33eaa5SJohn Baldwin return (error); 13976f33eaa5SJohn Baldwin error = bus_free_resource(dev, SYS_RES_IRQ, sc->pcie_irq); 13986f33eaa5SJohn Baldwin if (error) 13996f33eaa5SJohn Baldwin return (error); 1400e6b83836SJohn Baldwin error = pci_release_msi(dev); 1401e6b83836SJohn Baldwin if (error) 1402e6b83836SJohn Baldwin return (error); 1403e6b83836SJohn Baldwin if (sc->pcie_mem != NULL) 1404e6b83836SJohn Baldwin error = bus_free_resource(dev, SYS_RES_MEMORY, sc->pcie_mem); 1405e6b83836SJohn Baldwin return (error); 14066f33eaa5SJohn Baldwin } 14076f33eaa5SJohn Baldwin 140882cb5c3bSJohn Baldwin static void 140982cb5c3bSJohn Baldwin pcib_setup_hotplug(struct pcib_softc *sc) 141082cb5c3bSJohn Baldwin { 141182cb5c3bSJohn Baldwin device_t dev; 141282cb5c3bSJohn Baldwin uint16_t mask, val; 141382cb5c3bSJohn Baldwin 141482cb5c3bSJohn Baldwin dev = sc->dev; 141582cb5c3bSJohn Baldwin TASK_INIT(&sc->pcie_hp_task, 0, pcib_pcie_hotplug_task, sc); 1416fa3b03d3SAlexander Motin TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_ab_task, 0, 1417fa3b03d3SAlexander Motin pcib_pcie_ab_timeout, sc); 1418fa3b03d3SAlexander Motin TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_cc_task, 0, 1419fa3b03d3SAlexander Motin pcib_pcie_cc_timeout, sc); 1420fa3b03d3SAlexander Motin TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_dll_task, 0, 1421fa3b03d3SAlexander Motin pcib_pcie_dll_timeout, sc); 1422c6df6f53SWarner Losh sc->pcie_hp_lock = bus_topo_mtx(); 142382cb5c3bSJohn Baldwin 142482cb5c3bSJohn Baldwin /* Allocate IRQ. */ 142582cb5c3bSJohn Baldwin if (pcib_alloc_pcie_irq(sc) != 0) 142682cb5c3bSJohn Baldwin return; 142782cb5c3bSJohn Baldwin 142882cb5c3bSJohn Baldwin sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 142982cb5c3bSJohn Baldwin sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 143082cb5c3bSJohn Baldwin 14316f33eaa5SJohn Baldwin /* Clear any events previously pending. */ 14326f33eaa5SJohn Baldwin pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2); 14336f33eaa5SJohn Baldwin 143482cb5c3bSJohn Baldwin /* Enable HotPlug events. */ 143582cb5c3bSJohn Baldwin mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | 143682cb5c3bSJohn Baldwin PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE | 143782cb5c3bSJohn Baldwin PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE; 143837290148SEric van Gyzen val = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | PCIEM_SLOT_CTL_PDCE; 143982cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_APB) 144082cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_ABPE; 144182cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) 144282cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PFDE; 144382cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) 144482cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_MRLSCE; 144582cb5c3bSJohn Baldwin if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS)) 144682cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_CCIE; 144782cb5c3bSJohn Baldwin 144882cb5c3bSJohn Baldwin /* Turn the attention indicator off. */ 144982cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) { 145082cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_AIC; 145182cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_AI_OFF; 145282cb5c3bSJohn Baldwin } 145382cb5c3bSJohn Baldwin 145482cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, val, mask, false); 145582cb5c3bSJohn Baldwin } 14566f33eaa5SJohn Baldwin 14576f33eaa5SJohn Baldwin static int 14586f33eaa5SJohn Baldwin pcib_detach_hotplug(struct pcib_softc *sc) 14596f33eaa5SJohn Baldwin { 14606f33eaa5SJohn Baldwin uint16_t mask, val; 14616f33eaa5SJohn Baldwin int error; 14626f33eaa5SJohn Baldwin 14636f33eaa5SJohn Baldwin /* Disable the card in the slot and force it to detach. */ 14646f33eaa5SJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 14656f33eaa5SJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 1466fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_ab_task, 1467fa3b03d3SAlexander Motin NULL); 14686f33eaa5SJohn Baldwin } 14696f33eaa5SJohn Baldwin sc->flags |= PCIB_DETACHING; 14706f33eaa5SJohn Baldwin 14716f33eaa5SJohn Baldwin if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) { 1472fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_cc_task, 1473fa3b03d3SAlexander Motin NULL); 14746f33eaa5SJohn Baldwin tsleep(sc, 0, "hpcmd", hz); 14756f33eaa5SJohn Baldwin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 14766f33eaa5SJohn Baldwin } 14776f33eaa5SJohn Baldwin 14786f33eaa5SJohn Baldwin /* Disable HotPlug events. */ 14796f33eaa5SJohn Baldwin mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | 14806f33eaa5SJohn Baldwin PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE | 14816f33eaa5SJohn Baldwin PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE; 14826f33eaa5SJohn Baldwin val = 0; 14836f33eaa5SJohn Baldwin 14846f33eaa5SJohn Baldwin /* Turn the attention indicator off. */ 14856f33eaa5SJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) { 14866f33eaa5SJohn Baldwin mask |= PCIEM_SLOT_CTL_AIC; 14876f33eaa5SJohn Baldwin val |= PCIEM_SLOT_CTL_AI_OFF; 14886f33eaa5SJohn Baldwin } 14896f33eaa5SJohn Baldwin 14906f33eaa5SJohn Baldwin pcib_pcie_hotplug_update(sc, val, mask, false); 14916f33eaa5SJohn Baldwin 14926f33eaa5SJohn Baldwin error = pcib_release_pcie_irq(sc); 14936f33eaa5SJohn Baldwin if (error) 14946f33eaa5SJohn Baldwin return (error); 149512588ce0SAndriy Gapon taskqueue_drain(taskqueue_pci_hp, &sc->pcie_hp_task); 1496fa3b03d3SAlexander Motin taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_ab_task); 1497fa3b03d3SAlexander Motin taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_cc_task); 1498fa3b03d3SAlexander Motin taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_dll_task); 14996f33eaa5SJohn Baldwin return (0); 15006f33eaa5SJohn Baldwin } 150182cb5c3bSJohn Baldwin #endif 150282cb5c3bSJohn Baldwin 1503e36af292SJung-uk Kim /* 1504e36af292SJung-uk Kim * Get current bridge configuration. 1505e36af292SJung-uk Kim */ 1506e36af292SJung-uk Kim static void 1507e36af292SJung-uk Kim pcib_cfg_save(struct pcib_softc *sc) 1508e36af292SJung-uk Kim { 1509ad6f36f8SJohn Baldwin #ifndef NEW_PCIB 1510e36af292SJung-uk Kim device_t dev; 1511ad6f36f8SJohn Baldwin uint16_t command; 1512e36af292SJung-uk Kim 1513e36af292SJung-uk Kim dev = sc->dev; 1514e36af292SJung-uk Kim 1515ad6f36f8SJohn Baldwin command = pci_read_config(dev, PCIR_COMMAND, 2); 1516ad6f36f8SJohn Baldwin if (command & PCIM_CMD_PORTEN) 1517e36af292SJung-uk Kim pcib_get_io_decode(sc); 1518ad6f36f8SJohn Baldwin if (command & PCIM_CMD_MEMEN) 1519e36af292SJung-uk Kim pcib_get_mem_decode(sc); 152083c41143SJohn Baldwin #endif 1521e36af292SJung-uk Kim } 1522e36af292SJung-uk Kim 1523e36af292SJung-uk Kim /* 1524e36af292SJung-uk Kim * Restore previous bridge configuration. 1525e36af292SJung-uk Kim */ 1526e36af292SJung-uk Kim static void 1527e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc) 1528e36af292SJung-uk Kim { 1529ad6f36f8SJohn Baldwin #ifndef NEW_PCIB 1530ad6f36f8SJohn Baldwin uint16_t command; 1531ad6f36f8SJohn Baldwin #endif 1532e36af292SJung-uk Kim 153383c41143SJohn Baldwin #ifdef NEW_PCIB 153483c41143SJohn Baldwin pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM); 153583c41143SJohn Baldwin #else 1536151ba793SAlexander Kabaev command = pci_read_config(sc->dev, PCIR_COMMAND, 2); 1537ad6f36f8SJohn Baldwin if (command & PCIM_CMD_PORTEN) 1538e36af292SJung-uk Kim pcib_set_io_decode(sc); 1539ad6f36f8SJohn Baldwin if (command & PCIM_CMD_MEMEN) 1540e36af292SJung-uk Kim pcib_set_mem_decode(sc); 154183c41143SJohn Baldwin #endif 1542e36af292SJung-uk Kim } 1543e36af292SJung-uk Kim 1544e36af292SJung-uk Kim /* 1545bb0d0a8eSMike Smith * Generic device interface 1546bb0d0a8eSMike Smith */ 1547bb0d0a8eSMike Smith static int 1548bb0d0a8eSMike Smith pcib_probe(device_t dev) 1549bb0d0a8eSMike Smith { 1550bb0d0a8eSMike Smith if ((pci_get_class(dev) == PCIC_BRIDGE) && 1551bb0d0a8eSMike Smith (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) { 1552bb0d0a8eSMike Smith device_set_desc(dev, "PCI-PCI bridge"); 1553b7cbd25bSMarcel Moolenaar return(-10000); 1554bb0d0a8eSMike Smith } 1555bb0d0a8eSMike Smith return(ENXIO); 1556bb0d0a8eSMike Smith } 1557bb0d0a8eSMike Smith 15586f0d5884SJohn Baldwin void 15596f0d5884SJohn Baldwin pcib_attach_common(device_t dev) 1560bb0d0a8eSMike Smith { 1561bb0d0a8eSMike Smith struct pcib_softc *sc; 1562abf07f13SWarner Losh struct sysctl_ctx_list *sctx; 1563abf07f13SWarner Losh struct sysctl_oid *soid; 1564c825d4dcSJohn Baldwin int comma; 1565bb0d0a8eSMike Smith 1566bb0d0a8eSMike Smith sc = device_get_softc(dev); 1567bb0d0a8eSMike Smith sc->dev = dev; 1568bb0d0a8eSMike Smith 15694fa59183SMike Smith /* 15704fa59183SMike Smith * Get current bridge configuration. 15714fa59183SMike Smith */ 157255aaf894SMarius Strobl sc->domain = pci_get_domain(dev); 1573ad6f36f8SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 1574ad6f36f8SJohn Baldwin sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1); 1575ad6f36f8SJohn Baldwin sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1); 1576ad6f36f8SJohn Baldwin #endif 1577ad6f36f8SJohn Baldwin sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 1578e36af292SJung-uk Kim pcib_cfg_save(sc); 15794fa59183SMike Smith 15804fa59183SMike Smith /* 15814edef187SJohn Baldwin * The primary bus register should always be the bus of the 15824edef187SJohn Baldwin * parent. 15834edef187SJohn Baldwin */ 15844edef187SJohn Baldwin sc->pribus = pci_get_bus(dev); 15854edef187SJohn Baldwin pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1); 15864edef187SJohn Baldwin 15874edef187SJohn Baldwin /* 1588abf07f13SWarner Losh * Setup sysctl reporting nodes 1589abf07f13SWarner Losh */ 1590abf07f13SWarner Losh sctx = device_get_sysctl_ctx(dev); 1591abf07f13SWarner Losh soid = device_get_sysctl_tree(dev); 1592abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain", 1593abf07f13SWarner Losh CTLFLAG_RD, &sc->domain, 0, "Domain number"); 1594abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus", 1595abf07f13SWarner Losh CTLFLAG_RD, &sc->pribus, 0, "Primary bus number"); 1596abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus", 15974edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number"); 1598abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus", 15994edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number"); 1600abf07f13SWarner Losh 1601abf07f13SWarner Losh /* 16024fa59183SMike Smith * Quirk handling. 16034fa59183SMike Smith */ 16044fa59183SMike Smith switch (pci_get_devid(dev)) { 16052ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 16064fa59183SMike Smith case 0x12258086: /* Intel 82454KX/GX (Orion) */ 16074fa59183SMike Smith { 1608b0cb115fSWarner Losh uint8_t supbus; 16094fa59183SMike Smith 16104fa59183SMike Smith supbus = pci_read_config(dev, 0x41, 1); 16114fa59183SMike Smith if (supbus != 0xff) { 16124edef187SJohn Baldwin sc->bus.sec = supbus + 1; 16134edef187SJohn Baldwin sc->bus.sub = supbus + 1; 16144fa59183SMike Smith } 16154fa59183SMike Smith break; 16164fa59183SMike Smith } 16174edef187SJohn Baldwin #endif 16184fa59183SMike Smith 1619e4b59fc5SWarner Losh /* 1620e4b59fc5SWarner Losh * The i82380FB mobile docking controller is a PCI-PCI bridge, 1621e4b59fc5SWarner Losh * and it is a subtractive bridge. However, the ProgIf is wrong 1622e4b59fc5SWarner Losh * so the normal setting of PCIB_SUBTRACTIVE bit doesn't 16234718610dSZbigniew Bodek * happen. There are also Toshiba and Cavium ThunderX bridges 16244718610dSZbigniew Bodek * that behave this way. 1625e4b59fc5SWarner Losh */ 16264718610dSZbigniew Bodek case 0xa002177d: /* Cavium ThunderX */ 1627e4b59fc5SWarner Losh case 0x124b8086: /* Intel 82380FB Mobile */ 1628e4b59fc5SWarner Losh case 0x060513d7: /* Toshiba ???? */ 1629e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 1630e4b59fc5SWarner Losh break; 1631c94d6dbeSJung-uk Kim 16322ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 1633c94d6dbeSJung-uk Kim /* Compaq R3000 BIOS sets wrong subordinate bus number. */ 1634c94d6dbeSJung-uk Kim case 0x00dd10de: 1635c94d6dbeSJung-uk Kim { 1636c94d6dbeSJung-uk Kim char *cp; 1637c94d6dbeSJung-uk Kim 16382be111bfSDavide Italiano if ((cp = kern_getenv("smbios.planar.maker")) == NULL) 1639c94d6dbeSJung-uk Kim break; 16401def0ca6SJung-uk Kim if (strncmp(cp, "Compal", 6) != 0) { 16411def0ca6SJung-uk Kim freeenv(cp); 1642c94d6dbeSJung-uk Kim break; 16431def0ca6SJung-uk Kim } 16441def0ca6SJung-uk Kim freeenv(cp); 16452be111bfSDavide Italiano if ((cp = kern_getenv("smbios.planar.product")) == NULL) 16461def0ca6SJung-uk Kim break; 16471def0ca6SJung-uk Kim if (strncmp(cp, "08A0", 4) != 0) { 16481def0ca6SJung-uk Kim freeenv(cp); 16491def0ca6SJung-uk Kim break; 16501def0ca6SJung-uk Kim } 16511def0ca6SJung-uk Kim freeenv(cp); 16524edef187SJohn Baldwin if (sc->bus.sub < 0xa) { 1653c94d6dbeSJung-uk Kim pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1); 16544edef187SJohn Baldwin sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1); 1655c94d6dbeSJung-uk Kim } 1656c94d6dbeSJung-uk Kim break; 1657c94d6dbeSJung-uk Kim } 16584edef187SJohn Baldwin #endif 1659e4b59fc5SWarner Losh } 1660e4b59fc5SWarner Losh 166122bf1c7fSJohn Baldwin if (pci_msi_device_blacklisted(dev)) 166222bf1c7fSJohn Baldwin sc->flags |= PCIB_DISABLE_MSI; 166322bf1c7fSJohn Baldwin 166468e9cbd3SMarius Strobl if (pci_msix_device_blacklisted(dev)) 166568e9cbd3SMarius Strobl sc->flags |= PCIB_DISABLE_MSIX; 166668e9cbd3SMarius Strobl 1667e4b59fc5SWarner Losh /* 1668e4b59fc5SWarner Losh * Intel 815, 845 and other chipsets say they are PCI-PCI bridges, 1669e4b59fc5SWarner Losh * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM, 1670e4b59fc5SWarner Losh * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese. 1671e4b59fc5SWarner Losh * This means they act as if they were subtractively decoding 1672e4b59fc5SWarner Losh * bridges and pass all transactions. Mark them and real ProgIf 1 1673e4b59fc5SWarner Losh * parts as subtractive. 1674e4b59fc5SWarner Losh */ 1675e4b59fc5SWarner Losh if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 || 1676657d9f9fSJohn Baldwin pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE) 1677e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 1678e4b59fc5SWarner Losh 167982cb5c3bSJohn Baldwin #ifdef PCI_HP 168082cb5c3bSJohn Baldwin pcib_probe_hotplug(sc); 168182cb5c3bSJohn Baldwin #endif 168283c41143SJohn Baldwin #ifdef NEW_PCIB 16834edef187SJohn Baldwin #ifdef PCI_RES_BUS 16844edef187SJohn Baldwin pcib_setup_secbus(dev, &sc->bus, 1); 16854edef187SJohn Baldwin #endif 168683c41143SJohn Baldwin pcib_probe_windows(sc); 168783c41143SJohn Baldwin #endif 168882cb5c3bSJohn Baldwin #ifdef PCI_HP 168982cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG) 169082cb5c3bSJohn Baldwin pcib_setup_hotplug(sc); 169182cb5c3bSJohn Baldwin #endif 1692bb0d0a8eSMike Smith if (bootverbose) { 169355aaf894SMarius Strobl device_printf(dev, " domain %d\n", sc->domain); 16944edef187SJohn Baldwin device_printf(dev, " secondary bus %d\n", sc->bus.sec); 16954edef187SJohn Baldwin device_printf(dev, " subordinate bus %d\n", sc->bus.sub); 169683c41143SJohn Baldwin #ifdef NEW_PCIB 169783c41143SJohn Baldwin if (pcib_is_window_open(&sc->io)) 169883c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%jx-0x%jx\n", 169983c41143SJohn Baldwin (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit); 170083c41143SJohn Baldwin if (pcib_is_window_open(&sc->mem)) 170183c41143SJohn Baldwin device_printf(dev, " memory decode 0x%jx-0x%jx\n", 170283c41143SJohn Baldwin (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit); 170383c41143SJohn Baldwin if (pcib_is_window_open(&sc->pmem)) 170483c41143SJohn Baldwin device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 170583c41143SJohn Baldwin (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit); 170683c41143SJohn Baldwin #else 170783c41143SJohn Baldwin if (pcib_is_io_open(sc)) 170883c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%x-0x%x\n", 170983c41143SJohn Baldwin sc->iobase, sc->iolimit); 1710b0a2d4b8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 1711b0a2d4b8SWarner Losh device_printf(dev, " memory decode 0x%jx-0x%jx\n", 1712b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit); 1713b0a2d4b8SWarner Losh if (pcib_is_prefetch_open(sc)) 1714b0a2d4b8SWarner Losh device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 1715b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 171683c41143SJohn Baldwin #endif 1717c825d4dcSJohn Baldwin if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) || 1718c825d4dcSJohn Baldwin sc->flags & PCIB_SUBTRACTIVE) { 1719c825d4dcSJohn Baldwin device_printf(dev, " special decode "); 1720c825d4dcSJohn Baldwin comma = 0; 1721c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) { 1722c825d4dcSJohn Baldwin printf("ISA"); 1723c825d4dcSJohn Baldwin comma = 1; 1724c825d4dcSJohn Baldwin } 1725c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) { 1726c825d4dcSJohn Baldwin printf("%sVGA", comma ? ", " : ""); 1727c825d4dcSJohn Baldwin comma = 1; 1728c825d4dcSJohn Baldwin } 1729e4b59fc5SWarner Losh if (sc->flags & PCIB_SUBTRACTIVE) 1730c825d4dcSJohn Baldwin printf("%ssubtractive", comma ? ", " : ""); 1731c825d4dcSJohn Baldwin printf("\n"); 1732c825d4dcSJohn Baldwin } 1733bb0d0a8eSMike Smith } 1734bb0d0a8eSMike Smith 1735bb0d0a8eSMike Smith /* 1736ef888152SJohn Baldwin * Always enable busmastering on bridges so that transactions 1737ef888152SJohn Baldwin * initiated on the secondary bus are passed through to the 1738ef888152SJohn Baldwin * primary bus. 1739ef888152SJohn Baldwin */ 1740ef888152SJohn Baldwin pci_enable_busmaster(dev); 17416f0d5884SJohn Baldwin } 1742bb0d0a8eSMike Smith 174382cb5c3bSJohn Baldwin #ifdef PCI_HP 174482cb5c3bSJohn Baldwin static int 174582cb5c3bSJohn Baldwin pcib_present(struct pcib_softc *sc) 174682cb5c3bSJohn Baldwin { 174782cb5c3bSJohn Baldwin 174882cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG) 174982cb5c3bSJohn Baldwin return (pcib_hotplug_present(sc) != 0); 175082cb5c3bSJohn Baldwin return (1); 175182cb5c3bSJohn Baldwin } 175282cb5c3bSJohn Baldwin #endif 175382cb5c3bSJohn Baldwin 175438906aedSJohn Baldwin int 175567e7d085SJohn Baldwin pcib_attach_child(device_t dev) 17566f0d5884SJohn Baldwin { 17576f0d5884SJohn Baldwin struct pcib_softc *sc; 17586f0d5884SJohn Baldwin 17596f0d5884SJohn Baldwin sc = device_get_softc(dev); 176067e7d085SJohn Baldwin if (sc->bus.sec == 0) { 176167e7d085SJohn Baldwin /* no secondary bus; we should have fixed this */ 176267e7d085SJohn Baldwin return(0); 176367e7d085SJohn Baldwin } 176467e7d085SJohn Baldwin 176582cb5c3bSJohn Baldwin #ifdef PCI_HP 176682cb5c3bSJohn Baldwin if (!pcib_present(sc)) { 176782cb5c3bSJohn Baldwin /* An empty HotPlug slot, so don't add a PCI bus yet. */ 176882cb5c3bSJohn Baldwin return (0); 176982cb5c3bSJohn Baldwin } 177082cb5c3bSJohn Baldwin #endif 177182cb5c3bSJohn Baldwin 177267e7d085SJohn Baldwin sc->child = device_add_child(dev, "pci", -1); 1773bb0d0a8eSMike Smith return (bus_generic_attach(dev)); 1774bb0d0a8eSMike Smith } 1775bb0d0a8eSMike Smith 177667e7d085SJohn Baldwin int 177767e7d085SJohn Baldwin pcib_attach(device_t dev) 177867e7d085SJohn Baldwin { 177967e7d085SJohn Baldwin 178067e7d085SJohn Baldwin pcib_attach_common(dev); 178167e7d085SJohn Baldwin return (pcib_attach_child(dev)); 1782bb0d0a8eSMike Smith } 1783bb0d0a8eSMike Smith 17846f0d5884SJohn Baldwin int 17856f33eaa5SJohn Baldwin pcib_detach(device_t dev) 17866f33eaa5SJohn Baldwin { 17876f33eaa5SJohn Baldwin #if defined(PCI_HP) || defined(NEW_PCIB) 17886f33eaa5SJohn Baldwin struct pcib_softc *sc; 17896f33eaa5SJohn Baldwin #endif 17906f33eaa5SJohn Baldwin int error; 17916f33eaa5SJohn Baldwin 17926f33eaa5SJohn Baldwin #if defined(PCI_HP) || defined(NEW_PCIB) 17936f33eaa5SJohn Baldwin sc = device_get_softc(dev); 17946f33eaa5SJohn Baldwin #endif 17956f33eaa5SJohn Baldwin error = bus_generic_detach(dev); 17966f33eaa5SJohn Baldwin if (error) 17976f33eaa5SJohn Baldwin return (error); 17986f33eaa5SJohn Baldwin #ifdef PCI_HP 17996f33eaa5SJohn Baldwin if (sc->flags & PCIB_HOTPLUG) { 18006f33eaa5SJohn Baldwin error = pcib_detach_hotplug(sc); 18016f33eaa5SJohn Baldwin if (error) 18026f33eaa5SJohn Baldwin return (error); 18036f33eaa5SJohn Baldwin } 18046f33eaa5SJohn Baldwin #endif 18056f33eaa5SJohn Baldwin error = device_delete_children(dev); 18066f33eaa5SJohn Baldwin if (error) 18076f33eaa5SJohn Baldwin return (error); 18086f33eaa5SJohn Baldwin #ifdef NEW_PCIB 18096f33eaa5SJohn Baldwin pcib_free_windows(sc); 18106f33eaa5SJohn Baldwin #ifdef PCI_RES_BUS 18116f33eaa5SJohn Baldwin pcib_free_secbus(dev, &sc->bus); 18126f33eaa5SJohn Baldwin #endif 18136f33eaa5SJohn Baldwin #endif 18146f33eaa5SJohn Baldwin return (0); 18156f33eaa5SJohn Baldwin } 18166f33eaa5SJohn Baldwin 18176f33eaa5SJohn Baldwin int 1818e36af292SJung-uk Kim pcib_suspend(device_t dev) 1819e36af292SJung-uk Kim { 1820e36af292SJung-uk Kim 1821e36af292SJung-uk Kim pcib_cfg_save(device_get_softc(dev)); 18227212fc6aSJohn Baldwin return (bus_generic_suspend(dev)); 1823e36af292SJung-uk Kim } 1824e36af292SJung-uk Kim 1825e36af292SJung-uk Kim int 1826e36af292SJung-uk Kim pcib_resume(device_t dev) 1827e36af292SJung-uk Kim { 1828e36af292SJung-uk Kim 1829e36af292SJung-uk Kim pcib_cfg_restore(device_get_softc(dev)); 1830cffd37daSAndriy Gapon 1831cffd37daSAndriy Gapon /* 1832cffd37daSAndriy Gapon * Restore the Command register only after restoring the windows. 1833cffd37daSAndriy Gapon * The bridge should not be claiming random windows. 1834cffd37daSAndriy Gapon */ 1835cffd37daSAndriy Gapon pci_write_config(dev, PCIR_COMMAND, pci_get_cmdreg(dev), 2); 1836e36af292SJung-uk Kim return (bus_generic_resume(dev)); 1837e36af292SJung-uk Kim } 1838e36af292SJung-uk Kim 1839809923caSJustin Hibbits void 1840809923caSJustin Hibbits pcib_bridge_init(device_t dev) 1841809923caSJustin Hibbits { 1842809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 1843809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2); 1844809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1); 1845809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2); 1846809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2); 1847809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2); 1848809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 1849809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4); 1850809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2); 1851809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4); 1852809923caSJustin Hibbits } 1853809923caSJustin Hibbits 1854e36af292SJung-uk Kim int 185582cb5c3bSJohn Baldwin pcib_child_present(device_t dev, device_t child) 185682cb5c3bSJohn Baldwin { 185782cb5c3bSJohn Baldwin #ifdef PCI_HP 185882cb5c3bSJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 185982cb5c3bSJohn Baldwin int retval; 186082cb5c3bSJohn Baldwin 186182cb5c3bSJohn Baldwin retval = bus_child_present(dev); 186282cb5c3bSJohn Baldwin if (retval != 0 && sc->flags & PCIB_HOTPLUG) 186382cb5c3bSJohn Baldwin retval = pcib_hotplug_present(sc); 186482cb5c3bSJohn Baldwin return (retval); 186582cb5c3bSJohn Baldwin #else 186682cb5c3bSJohn Baldwin return (bus_child_present(dev)); 186782cb5c3bSJohn Baldwin #endif 186882cb5c3bSJohn Baldwin } 186982cb5c3bSJohn Baldwin 187082cb5c3bSJohn Baldwin int 1871bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 1872bb0d0a8eSMike Smith { 1873bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 1874bb0d0a8eSMike Smith 1875bb0d0a8eSMike Smith switch (which) { 187655aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 187755aaf894SMarius Strobl *result = sc->domain; 187855aaf894SMarius Strobl return(0); 1879bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 18804edef187SJohn Baldwin *result = sc->bus.sec; 1881bb0d0a8eSMike Smith return(0); 1882bb0d0a8eSMike Smith } 1883bb0d0a8eSMike Smith return(ENOENT); 1884bb0d0a8eSMike Smith } 1885bb0d0a8eSMike Smith 18866f0d5884SJohn Baldwin int 1887bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 1888bb0d0a8eSMike Smith { 1889bb0d0a8eSMike Smith 1890bb0d0a8eSMike Smith switch (which) { 189155aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 189255aaf894SMarius Strobl return(EINVAL); 1893bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 18944edef187SJohn Baldwin return(EINVAL); 1895bb0d0a8eSMike Smith } 1896bb0d0a8eSMike Smith return(ENOENT); 1897bb0d0a8eSMike Smith } 1898bb0d0a8eSMike Smith 189983c41143SJohn Baldwin #ifdef NEW_PCIB 190083c41143SJohn Baldwin /* 190183c41143SJohn Baldwin * Attempt to allocate a resource from the existing resources assigned 190283c41143SJohn Baldwin * to a window. 190383c41143SJohn Baldwin */ 190483c41143SJohn Baldwin static struct resource * 190583c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w, 19062dd1bdf1SJustin Hibbits device_t child, int type, int *rid, rman_res_t start, rman_res_t end, 19072dd1bdf1SJustin Hibbits rman_res_t count, u_int flags) 190883c41143SJohn Baldwin { 190983c41143SJohn Baldwin struct resource *res; 191083c41143SJohn Baldwin 191183c41143SJohn Baldwin if (!pcib_is_window_open(w)) 191283c41143SJohn Baldwin return (NULL); 191383c41143SJohn Baldwin 191483c41143SJohn Baldwin res = rman_reserve_resource(&w->rman, start, end, count, 191583c41143SJohn Baldwin flags & ~RF_ACTIVE, child); 191683c41143SJohn Baldwin if (res == NULL) 191783c41143SJohn Baldwin return (NULL); 191883c41143SJohn Baldwin 191983c41143SJohn Baldwin if (bootverbose) 192083c41143SJohn Baldwin device_printf(sc->dev, 1921da1b038aSJustin Hibbits "allocated %s range (%#jx-%#jx) for rid %x of %s\n", 192283c41143SJohn Baldwin w->name, rman_get_start(res), rman_get_end(res), *rid, 192383c41143SJohn Baldwin pcib_child_name(child)); 192483c41143SJohn Baldwin rman_set_rid(res, *rid); 192583c41143SJohn Baldwin 192683c41143SJohn Baldwin /* 192783c41143SJohn Baldwin * If the resource should be active, pass that request up the 192883c41143SJohn Baldwin * tree. This assumes the parent drivers can handle 192983c41143SJohn Baldwin * activating sub-allocated resources. 193083c41143SJohn Baldwin */ 193183c41143SJohn Baldwin if (flags & RF_ACTIVE) { 193283c41143SJohn Baldwin if (bus_activate_resource(child, type, *rid, res) != 0) { 193383c41143SJohn Baldwin rman_release_resource(res); 193483c41143SJohn Baldwin return (NULL); 193583c41143SJohn Baldwin } 193683c41143SJohn Baldwin } 193783c41143SJohn Baldwin 193883c41143SJohn Baldwin return (res); 193983c41143SJohn Baldwin } 194083c41143SJohn Baldwin 1941c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */ 1942c825d4dcSJohn Baldwin static int 1943c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type, 19442dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 1945c825d4dcSJohn Baldwin { 1946c825d4dcSJohn Baldwin struct resource *res; 19472dd1bdf1SJustin Hibbits rman_res_t base, limit, wmask; 1948c825d4dcSJohn Baldwin int rid; 1949c825d4dcSJohn Baldwin 1950c825d4dcSJohn Baldwin /* 1951c825d4dcSJohn Baldwin * If this is an I/O window on a bridge with ISA enable set 1952c825d4dcSJohn Baldwin * and the start address is below 64k, then try to allocate an 1953c825d4dcSJohn Baldwin * initial window of 0x1000 bytes long starting at address 1954c825d4dcSJohn Baldwin * 0xf000 and walking down. Note that if the original request 1955c825d4dcSJohn Baldwin * was larger than the non-aliased range size of 0x100 our 1956c825d4dcSJohn Baldwin * caller would have raised the start address up to 64k 1957c825d4dcSJohn Baldwin * already. 1958c825d4dcSJohn Baldwin */ 1959c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1960c825d4dcSJohn Baldwin start < 65536) { 1961c825d4dcSJohn Baldwin for (base = 0xf000; (long)base >= 0; base -= 0x1000) { 1962c825d4dcSJohn Baldwin limit = base + 0xfff; 1963c825d4dcSJohn Baldwin 1964c825d4dcSJohn Baldwin /* 1965c825d4dcSJohn Baldwin * Skip ranges that wouldn't work for the 1966c825d4dcSJohn Baldwin * original request. Note that the actual 1967c825d4dcSJohn Baldwin * window that overlaps are the non-alias 1968c825d4dcSJohn Baldwin * ranges within [base, limit], so this isn't 1969c825d4dcSJohn Baldwin * quite a simple comparison. 1970c825d4dcSJohn Baldwin */ 1971c825d4dcSJohn Baldwin if (start + count > limit - 0x400) 1972c825d4dcSJohn Baldwin continue; 1973c825d4dcSJohn Baldwin if (base == 0) { 1974c825d4dcSJohn Baldwin /* 1975c825d4dcSJohn Baldwin * The first open region for the window at 1976c825d4dcSJohn Baldwin * 0 is 0x400-0x4ff. 1977c825d4dcSJohn Baldwin */ 1978c825d4dcSJohn Baldwin if (end - count + 1 < 0x400) 1979c825d4dcSJohn Baldwin continue; 1980c825d4dcSJohn Baldwin } else { 1981c825d4dcSJohn Baldwin if (end - count + 1 < base) 1982c825d4dcSJohn Baldwin continue; 1983c825d4dcSJohn Baldwin } 1984c825d4dcSJohn Baldwin 1985c825d4dcSJohn Baldwin if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) { 1986c825d4dcSJohn Baldwin w->base = base; 1987c825d4dcSJohn Baldwin w->limit = limit; 1988c825d4dcSJohn Baldwin return (0); 1989c825d4dcSJohn Baldwin } 1990c825d4dcSJohn Baldwin } 1991c825d4dcSJohn Baldwin return (ENOSPC); 1992c825d4dcSJohn Baldwin } 1993c825d4dcSJohn Baldwin 199489977ce2SJustin Hibbits wmask = ((rman_res_t)1 << w->step) - 1; 1995c825d4dcSJohn Baldwin if (RF_ALIGNMENT(flags) < w->step) { 1996c825d4dcSJohn Baldwin flags &= ~RF_ALIGNMENT_MASK; 1997c825d4dcSJohn Baldwin flags |= RF_ALIGNMENT_LOG2(w->step); 1998c825d4dcSJohn Baldwin } 1999c825d4dcSJohn Baldwin start &= ~wmask; 2000c825d4dcSJohn Baldwin end |= wmask; 200189977ce2SJustin Hibbits count = roundup2(count, (rman_res_t)1 << w->step); 2002c825d4dcSJohn Baldwin rid = w->reg; 2003c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, start, end, count, 2004c825d4dcSJohn Baldwin flags & ~RF_ACTIVE); 2005c825d4dcSJohn Baldwin if (res == NULL) 2006c825d4dcSJohn Baldwin return (ENOSPC); 2007c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 2008c825d4dcSJohn Baldwin pcib_activate_window(sc, type); 2009c825d4dcSJohn Baldwin w->base = rman_get_start(res); 2010c825d4dcSJohn Baldwin w->limit = rman_get_end(res); 2011c825d4dcSJohn Baldwin return (0); 2012c825d4dcSJohn Baldwin } 2013c825d4dcSJohn Baldwin 2014c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */ 2015c825d4dcSJohn Baldwin static int 2016c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type, 20172dd1bdf1SJustin Hibbits rman_res_t base, rman_res_t limit) 2018c825d4dcSJohn Baldwin { 2019c825d4dcSJohn Baldwin struct resource *res; 2020c825d4dcSJohn Baldwin int error, i, force_64k_base; 2021c825d4dcSJohn Baldwin 2022c825d4dcSJohn Baldwin KASSERT(base <= w->base && limit >= w->limit, 2023c825d4dcSJohn Baldwin ("attempting to shrink window")); 2024c825d4dcSJohn Baldwin 2025c825d4dcSJohn Baldwin /* 2026c825d4dcSJohn Baldwin * XXX: pcib_grow_window() doesn't try to do this anyway and 2027c825d4dcSJohn Baldwin * the error handling for all the edge cases would be tedious. 2028c825d4dcSJohn Baldwin */ 2029c825d4dcSJohn Baldwin KASSERT(limit == w->limit || base == w->base, 2030c825d4dcSJohn Baldwin ("attempting to grow both ends of a window")); 2031c825d4dcSJohn Baldwin 2032c825d4dcSJohn Baldwin /* 2033c825d4dcSJohn Baldwin * Yet more special handling for requests to expand an I/O 2034c825d4dcSJohn Baldwin * window behind an ISA-enabled bridge. Since I/O windows 2035c825d4dcSJohn Baldwin * have to grow in 0x1000 increments and the end of the 0xffff 2036c825d4dcSJohn Baldwin * range is an alias, growing a window below 64k will always 2037c825d4dcSJohn Baldwin * result in allocating new resources and never adjusting an 2038c825d4dcSJohn Baldwin * existing resource. 2039c825d4dcSJohn Baldwin */ 2040c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 2041c825d4dcSJohn Baldwin (limit <= 65535 || (base <= 65535 && base != w->base))) { 2042c825d4dcSJohn Baldwin KASSERT(limit == w->limit || limit <= 65535, 2043c825d4dcSJohn Baldwin ("attempting to grow both ends across 64k ISA alias")); 2044c825d4dcSJohn Baldwin 2045c825d4dcSJohn Baldwin if (base != w->base) 2046c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1); 2047c825d4dcSJohn Baldwin else 2048c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, w->limit + 1, 2049c825d4dcSJohn Baldwin limit); 2050c825d4dcSJohn Baldwin if (error == 0) { 2051c825d4dcSJohn Baldwin w->base = base; 2052c825d4dcSJohn Baldwin w->limit = limit; 2053c825d4dcSJohn Baldwin } 2054c825d4dcSJohn Baldwin return (error); 2055c825d4dcSJohn Baldwin } 2056c825d4dcSJohn Baldwin 2057c825d4dcSJohn Baldwin /* 2058c825d4dcSJohn Baldwin * Find the existing resource to adjust. Usually there is only one, 2059c825d4dcSJohn Baldwin * but for an ISA-enabled bridge we might be growing the I/O window 2060c825d4dcSJohn Baldwin * above 64k and need to find the existing resource that maps all 2061c825d4dcSJohn Baldwin * of the area above 64k. 2062c825d4dcSJohn Baldwin */ 2063c825d4dcSJohn Baldwin for (i = 0; i < w->count; i++) { 2064c825d4dcSJohn Baldwin if (rman_get_end(w->res[i]) == w->limit) 2065c825d4dcSJohn Baldwin break; 2066c825d4dcSJohn Baldwin } 2067c825d4dcSJohn Baldwin KASSERT(i != w->count, ("did not find existing resource")); 2068c825d4dcSJohn Baldwin res = w->res[i]; 2069c825d4dcSJohn Baldwin 2070c825d4dcSJohn Baldwin /* 2071c825d4dcSJohn Baldwin * Usually the resource we found should match the window's 2072c825d4dcSJohn Baldwin * existing range. The one exception is the ISA-enabled case 2073c825d4dcSJohn Baldwin * mentioned above in which case the resource should start at 2074c825d4dcSJohn Baldwin * 64k. 2075c825d4dcSJohn Baldwin */ 2076c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 2077c825d4dcSJohn Baldwin w->base <= 65535) { 2078c825d4dcSJohn Baldwin KASSERT(rman_get_start(res) == 65536, 2079c825d4dcSJohn Baldwin ("existing resource mismatch")); 2080c825d4dcSJohn Baldwin force_64k_base = 1; 2081c825d4dcSJohn Baldwin } else { 2082c825d4dcSJohn Baldwin KASSERT(w->base == rman_get_start(res), 2083c825d4dcSJohn Baldwin ("existing resource mismatch")); 2084c825d4dcSJohn Baldwin force_64k_base = 0; 2085c825d4dcSJohn Baldwin } 2086c825d4dcSJohn Baldwin 2087c825d4dcSJohn Baldwin error = bus_adjust_resource(sc->dev, type, res, force_64k_base ? 2088c825d4dcSJohn Baldwin rman_get_start(res) : base, limit); 2089c825d4dcSJohn Baldwin if (error) 2090c825d4dcSJohn Baldwin return (error); 2091c825d4dcSJohn Baldwin 2092c825d4dcSJohn Baldwin /* Add the newly allocated region to the resource manager. */ 2093c825d4dcSJohn Baldwin if (w->base != base) { 2094c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, base, w->base - 1); 2095c825d4dcSJohn Baldwin w->base = base; 2096c825d4dcSJohn Baldwin } else { 2097c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, w->limit + 1, limit); 2098c825d4dcSJohn Baldwin w->limit = limit; 2099c825d4dcSJohn Baldwin } 2100c825d4dcSJohn Baldwin if (error) { 2101c825d4dcSJohn Baldwin if (bootverbose) 2102c825d4dcSJohn Baldwin device_printf(sc->dev, 2103c825d4dcSJohn Baldwin "failed to expand %s resource manager\n", w->name); 2104c825d4dcSJohn Baldwin (void)bus_adjust_resource(sc->dev, type, res, force_64k_base ? 2105c825d4dcSJohn Baldwin rman_get_start(res) : w->base, w->limit); 2106c825d4dcSJohn Baldwin } 2107c825d4dcSJohn Baldwin return (error); 2108c825d4dcSJohn Baldwin } 2109c825d4dcSJohn Baldwin 211083c41143SJohn Baldwin /* 211183c41143SJohn Baldwin * Attempt to grow a window to make room for a given resource request. 211283c41143SJohn Baldwin */ 211383c41143SJohn Baldwin static int 211483c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type, 21152dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 211683c41143SJohn Baldwin { 21172dd1bdf1SJustin Hibbits rman_res_t align, start_free, end_free, front, back, wmask; 2118c825d4dcSJohn Baldwin int error; 211983c41143SJohn Baldwin 212083c41143SJohn Baldwin /* 212183c41143SJohn Baldwin * Clamp the desired resource range to the maximum address 212283c41143SJohn Baldwin * this window supports. Reject impossible requests. 2123c825d4dcSJohn Baldwin * 2124c825d4dcSJohn Baldwin * For I/O port requests behind a bridge with the ISA enable 2125c825d4dcSJohn Baldwin * bit set, force large allocations to start above 64k. 212683c41143SJohn Baldwin */ 212783c41143SJohn Baldwin if (!w->valid) 212883c41143SJohn Baldwin return (EINVAL); 2129c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 && 2130c825d4dcSJohn Baldwin start < 65536) 2131c825d4dcSJohn Baldwin start = 65536; 213283c41143SJohn Baldwin if (end > w->rman.rm_end) 213383c41143SJohn Baldwin end = w->rman.rm_end; 213483c41143SJohn Baldwin if (start + count - 1 > end || start + count < start) 213583c41143SJohn Baldwin return (EINVAL); 213689977ce2SJustin Hibbits wmask = ((rman_res_t)1 << w->step) - 1; 213783c41143SJohn Baldwin 213883c41143SJohn Baldwin /* 213983c41143SJohn Baldwin * If there is no resource at all, just try to allocate enough 214083c41143SJohn Baldwin * aligned space for this resource. 214183c41143SJohn Baldwin */ 214283c41143SJohn Baldwin if (w->res == NULL) { 2143c825d4dcSJohn Baldwin error = pcib_alloc_new_window(sc, w, type, start, end, count, 2144c825d4dcSJohn Baldwin flags); 2145c825d4dcSJohn Baldwin if (error) { 214683c41143SJohn Baldwin if (bootverbose) 214783c41143SJohn Baldwin device_printf(sc->dev, 2148da1b038aSJustin Hibbits "failed to allocate initial %s window (%#jx-%#jx,%#jx)\n", 214983c41143SJohn Baldwin w->name, start, end, count); 215083c41143SJohn Baldwin return (error); 215183c41143SJohn Baldwin } 2152c825d4dcSJohn Baldwin if (bootverbose) 2153c825d4dcSJohn Baldwin device_printf(sc->dev, 2154c825d4dcSJohn Baldwin "allocated initial %s window of %#jx-%#jx\n", 2155c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 215683c41143SJohn Baldwin goto updatewin; 215783c41143SJohn Baldwin } 215883c41143SJohn Baldwin 215983c41143SJohn Baldwin /* 216083c41143SJohn Baldwin * See if growing the window would help. Compute the minimum 216183c41143SJohn Baldwin * amount of address space needed on both the front and back 216283c41143SJohn Baldwin * ends of the existing window to satisfy the allocation. 216383c41143SJohn Baldwin * 216483c41143SJohn Baldwin * For each end, build a candidate region adjusting for the 216583c41143SJohn Baldwin * required alignment, etc. If there is a free region at the 216683c41143SJohn Baldwin * edge of the window, grow from the inner edge of the free 216783c41143SJohn Baldwin * region. Otherwise grow from the window boundary. 216883c41143SJohn Baldwin * 2169c825d4dcSJohn Baldwin * Growing an I/O window below 64k for a bridge with the ISA 2170c825d4dcSJohn Baldwin * enable bit doesn't require any special magic as the step 2171c825d4dcSJohn Baldwin * size of an I/O window (1k) always includes multiple 2172c825d4dcSJohn Baldwin * non-alias ranges when it is grown in either direction. 2173c825d4dcSJohn Baldwin * 217483c41143SJohn Baldwin * XXX: Special case: if w->res is completely empty and the 217583c41143SJohn Baldwin * request size is larger than w->res, we should find the 217683c41143SJohn Baldwin * optimal aligned buffer containing w->res and allocate that. 217783c41143SJohn Baldwin */ 217883c41143SJohn Baldwin if (bootverbose) 217983c41143SJohn Baldwin device_printf(sc->dev, 2180da1b038aSJustin Hibbits "attempting to grow %s window for (%#jx-%#jx,%#jx)\n", 218183c41143SJohn Baldwin w->name, start, end, count); 218289977ce2SJustin Hibbits align = (rman_res_t)1 << RF_ALIGNMENT(flags); 2183c825d4dcSJohn Baldwin if (start < w->base) { 218483c41143SJohn Baldwin if (rman_first_free_region(&w->rman, &start_free, &end_free) != 2185c825d4dcSJohn Baldwin 0 || start_free != w->base) 2186c825d4dcSJohn Baldwin end_free = w->base; 218783c41143SJohn Baldwin if (end_free > end) 2188ddac8cc9SJohn Baldwin end_free = end + 1; 218983c41143SJohn Baldwin 219083c41143SJohn Baldwin /* Move end_free down until it is properly aligned. */ 219183c41143SJohn Baldwin end_free &= ~(align - 1); 2192a49dcb46SJohn Baldwin end_free--; 2193a49dcb46SJohn Baldwin front = end_free - (count - 1); 219483c41143SJohn Baldwin 219583c41143SJohn Baldwin /* 219683c41143SJohn Baldwin * The resource would now be allocated at (front, 219783c41143SJohn Baldwin * end_free). Ensure that fits in the (start, end) 219883c41143SJohn Baldwin * bounds. end_free is checked above. If 'front' is 219983c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 220083c41143SJohn Baldwin * Also check for underflow. 220183c41143SJohn Baldwin */ 220283c41143SJohn Baldwin if (front >= start && front <= end_free) { 220383c41143SJohn Baldwin if (bootverbose) 2204da1b038aSJustin Hibbits printf("\tfront candidate range: %#jx-%#jx\n", 220583c41143SJohn Baldwin front, end_free); 2206a7b5acacSJohn Baldwin front &= ~wmask; 2207c825d4dcSJohn Baldwin front = w->base - front; 220883c41143SJohn Baldwin } else 220983c41143SJohn Baldwin front = 0; 221083c41143SJohn Baldwin } else 221183c41143SJohn Baldwin front = 0; 2212c825d4dcSJohn Baldwin if (end > w->limit) { 221383c41143SJohn Baldwin if (rman_last_free_region(&w->rman, &start_free, &end_free) != 2214c825d4dcSJohn Baldwin 0 || end_free != w->limit) 2215c825d4dcSJohn Baldwin start_free = w->limit + 1; 221683c41143SJohn Baldwin if (start_free < start) 221783c41143SJohn Baldwin start_free = start; 221883c41143SJohn Baldwin 221983c41143SJohn Baldwin /* Move start_free up until it is properly aligned. */ 222083c41143SJohn Baldwin start_free = roundup2(start_free, align); 2221a49dcb46SJohn Baldwin back = start_free + count - 1; 222283c41143SJohn Baldwin 222383c41143SJohn Baldwin /* 222483c41143SJohn Baldwin * The resource would now be allocated at (start_free, 222583c41143SJohn Baldwin * back). Ensure that fits in the (start, end) 222683c41143SJohn Baldwin * bounds. start_free is checked above. If 'back' is 222783c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 222883c41143SJohn Baldwin * Also check for overflow. 222983c41143SJohn Baldwin */ 223083c41143SJohn Baldwin if (back <= end && start_free <= back) { 223183c41143SJohn Baldwin if (bootverbose) 2232da1b038aSJustin Hibbits printf("\tback candidate range: %#jx-%#jx\n", 223383c41143SJohn Baldwin start_free, back); 2234a7b5acacSJohn Baldwin back |= wmask; 2235c825d4dcSJohn Baldwin back -= w->limit; 223683c41143SJohn Baldwin } else 223783c41143SJohn Baldwin back = 0; 223883c41143SJohn Baldwin } else 223983c41143SJohn Baldwin back = 0; 224083c41143SJohn Baldwin 224183c41143SJohn Baldwin /* 224283c41143SJohn Baldwin * Try to allocate the smallest needed region first. 224383c41143SJohn Baldwin * If that fails, fall back to the other region. 224483c41143SJohn Baldwin */ 224583c41143SJohn Baldwin error = ENOSPC; 224683c41143SJohn Baldwin while (front != 0 || back != 0) { 224783c41143SJohn Baldwin if (front != 0 && (front <= back || back == 0)) { 2248c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base - front, 2249c825d4dcSJohn Baldwin w->limit); 225083c41143SJohn Baldwin if (error == 0) 225183c41143SJohn Baldwin break; 225283c41143SJohn Baldwin front = 0; 225383c41143SJohn Baldwin } else { 2254c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base, 2255c825d4dcSJohn Baldwin w->limit + back); 225683c41143SJohn Baldwin if (error == 0) 225783c41143SJohn Baldwin break; 225883c41143SJohn Baldwin back = 0; 225983c41143SJohn Baldwin } 226083c41143SJohn Baldwin } 226183c41143SJohn Baldwin 226283c41143SJohn Baldwin if (error) 226383c41143SJohn Baldwin return (error); 226483c41143SJohn Baldwin if (bootverbose) 2265c825d4dcSJohn Baldwin device_printf(sc->dev, "grew %s window to %#jx-%#jx\n", 2266c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 226783c41143SJohn Baldwin 226883c41143SJohn Baldwin updatewin: 2269c825d4dcSJohn Baldwin /* Write the new window. */ 2270a7b5acacSJohn Baldwin KASSERT((w->base & wmask) == 0, ("start address is not aligned")); 2271a7b5acacSJohn Baldwin KASSERT((w->limit & wmask) == wmask, ("end address is not aligned")); 227283c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 227383c41143SJohn Baldwin return (0); 227483c41143SJohn Baldwin } 227583c41143SJohn Baldwin 227683c41143SJohn Baldwin /* 227783c41143SJohn Baldwin * We have to trap resource allocation requests and ensure that the bridge 227883c41143SJohn Baldwin * is set up to, or capable of handling them. 227983c41143SJohn Baldwin */ 228003719c65SJohn Baldwin static struct resource * 228183c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 22822dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 228383c41143SJohn Baldwin { 228483c41143SJohn Baldwin struct pcib_softc *sc; 228583c41143SJohn Baldwin struct resource *r; 228683c41143SJohn Baldwin 228783c41143SJohn Baldwin sc = device_get_softc(dev); 228883c41143SJohn Baldwin 228983c41143SJohn Baldwin /* 229083c41143SJohn Baldwin * VGA resources are decoded iff the VGA enable bit is set in 229183c41143SJohn Baldwin * the bridge control register. VGA resources do not fall into 229283c41143SJohn Baldwin * the resource windows and are passed up to the parent. 229383c41143SJohn Baldwin */ 229483c41143SJohn Baldwin if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) || 229583c41143SJohn Baldwin (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) { 229683c41143SJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) 229783c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, 229883c41143SJohn Baldwin rid, start, end, count, flags)); 229983c41143SJohn Baldwin else 230083c41143SJohn Baldwin return (NULL); 230183c41143SJohn Baldwin } 230283c41143SJohn Baldwin 230383c41143SJohn Baldwin switch (type) { 23044edef187SJohn Baldwin #ifdef PCI_RES_BUS 23054edef187SJohn Baldwin case PCI_RES_BUS: 23064edef187SJohn Baldwin return (pcib_alloc_subbus(&sc->bus, child, rid, start, end, 23074edef187SJohn Baldwin count, flags)); 23084edef187SJohn Baldwin #endif 230983c41143SJohn Baldwin case SYS_RES_IOPORT: 2310c825d4dcSJohn Baldwin if (pcib_is_isa_range(sc, start, end, count)) 2311c825d4dcSJohn Baldwin return (NULL); 231283c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start, 231383c41143SJohn Baldwin end, count, flags); 2314a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 231583c41143SJohn Baldwin break; 231683c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->io, type, start, end, count, 231783c41143SJohn Baldwin flags) == 0) 231883c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, 231983c41143SJohn Baldwin rid, start, end, count, flags); 232083c41143SJohn Baldwin break; 232183c41143SJohn Baldwin case SYS_RES_MEMORY: 232283c41143SJohn Baldwin /* 232383c41143SJohn Baldwin * For prefetchable resources, prefer the prefetchable 232483c41143SJohn Baldwin * memory window, but fall back to the regular memory 232583c41143SJohn Baldwin * window if that fails. Try both windows before 232683c41143SJohn Baldwin * attempting to grow a window in case the firmware 232783c41143SJohn Baldwin * has used a range in the regular memory window to 232883c41143SJohn Baldwin * map a prefetchable BAR. 232983c41143SJohn Baldwin */ 233083c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 233183c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, type, 233283c41143SJohn Baldwin rid, start, end, count, flags); 233383c41143SJohn Baldwin if (r != NULL) 233483c41143SJohn Baldwin break; 233583c41143SJohn Baldwin } 233683c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid, 233783c41143SJohn Baldwin start, end, count, flags); 2338a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 233983c41143SJohn Baldwin break; 234083c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 234183c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->pmem, type, start, end, 234283c41143SJohn Baldwin count, flags) == 0) { 234383c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, 234483c41143SJohn Baldwin type, rid, start, end, count, flags); 234583c41143SJohn Baldwin if (r != NULL) 234683c41143SJohn Baldwin break; 234783c41143SJohn Baldwin } 234883c41143SJohn Baldwin } 234983c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->mem, type, start, end, count, 235083c41143SJohn Baldwin flags & ~RF_PREFETCHABLE) == 0) 235183c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, 235283c41143SJohn Baldwin rid, start, end, count, flags); 235383c41143SJohn Baldwin break; 235483c41143SJohn Baldwin default: 235583c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 235683c41143SJohn Baldwin start, end, count, flags)); 235783c41143SJohn Baldwin } 235883c41143SJohn Baldwin 235983c41143SJohn Baldwin /* 236083c41143SJohn Baldwin * If attempts to suballocate from the window fail but this is a 236183c41143SJohn Baldwin * subtractive bridge, pass the request up the tree. 236283c41143SJohn Baldwin */ 236383c41143SJohn Baldwin if (sc->flags & PCIB_SUBTRACTIVE && r == NULL) 236483c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 236583c41143SJohn Baldwin start, end, count, flags)); 236683c41143SJohn Baldwin return (r); 236783c41143SJohn Baldwin } 236883c41143SJohn Baldwin 236903719c65SJohn Baldwin static int 237083c41143SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r, 23712dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end) 237283c41143SJohn Baldwin { 237383c41143SJohn Baldwin struct pcib_softc *sc; 237426245980SJessica Clarke struct pcib_window *w; 237515cb3b54SAlexander Motin rman_res_t wmask; 237626245980SJessica Clarke int error; 237783c41143SJohn Baldwin 237883c41143SJohn Baldwin sc = device_get_softc(bus); 237926245980SJessica Clarke 238026245980SJessica Clarke /* 238126245980SJessica Clarke * If the resource wasn't sub-allocated from one of our region 238226245980SJessica Clarke * managers then just pass the request up. 238326245980SJessica Clarke */ 238426245980SJessica Clarke if (!pcib_is_resource_managed(sc, type, r)) 238526245980SJessica Clarke return (bus_generic_adjust_resource(bus, child, type, r, 238626245980SJessica Clarke start, end)); 238726245980SJessica Clarke 238826245980SJessica Clarke #ifdef PCI_RES_BUS 238931776afdSJessica Clarke if (type == PCI_RES_BUS) { 239031776afdSJessica Clarke /* 239131776afdSJessica Clarke * If our bus range isn't big enough to grow the sub-allocation 239231776afdSJessica Clarke * then we need to grow our bus range. Any request that would 239331776afdSJessica Clarke * require us to decrease the start of our own bus range is 239431776afdSJessica Clarke * invalid, we can only extend the end; ignore such requests 239531776afdSJessica Clarke * and let rman_adjust_resource fail below. 239631776afdSJessica Clarke */ 239731776afdSJessica Clarke if (start >= sc->bus.sec && end > sc->bus.sub) { 239831776afdSJessica Clarke error = pcib_grow_subbus(&sc->bus, end); 239931776afdSJessica Clarke if (error != 0) 240031776afdSJessica Clarke return (error); 240131776afdSJessica Clarke } 240231776afdSJessica Clarke } else 240326245980SJessica Clarke #endif 240426245980SJessica Clarke { 240526245980SJessica Clarke /* 240626245980SJessica Clarke * Resource is managed and not a secondary bus number, must 240726245980SJessica Clarke * be from one of our windows. 240826245980SJessica Clarke */ 240926245980SJessica Clarke w = pcib_get_resource_window(sc, type, r); 241026245980SJessica Clarke KASSERT(w != NULL, 241126245980SJessica Clarke ("%s: no window for resource (%#jx-%#jx) type %d", 241226245980SJessica Clarke __func__, rman_get_start(r), rman_get_end(r), type)); 241326245980SJessica Clarke 241426245980SJessica Clarke /* 241526245980SJessica Clarke * If our window isn't big enough to grow the sub-allocation 241626245980SJessica Clarke * then we need to expand the window. 241726245980SJessica Clarke */ 241826245980SJessica Clarke if (start < w->base || end > w->limit) { 241915cb3b54SAlexander Motin wmask = ((rman_res_t)1 << w->step) - 1; 242015cb3b54SAlexander Motin error = pcib_expand_window(sc, w, type, 242115cb3b54SAlexander Motin MIN(start & ~wmask, w->base), 242215cb3b54SAlexander Motin MAX(end | wmask, w->limit)); 242326245980SJessica Clarke if (error != 0) 242426245980SJessica Clarke return (error); 242515cb3b54SAlexander Motin if (bootverbose) 242615cb3b54SAlexander Motin device_printf(sc->dev, 242715cb3b54SAlexander Motin "grew %s window to %#jx-%#jx\n", 242815cb3b54SAlexander Motin w->name, (uintmax_t)w->base, 242915cb3b54SAlexander Motin (uintmax_t)w->limit); 243015cb3b54SAlexander Motin pcib_write_windows(sc, w->mask); 243126245980SJessica Clarke } 243226245980SJessica Clarke } 243326245980SJessica Clarke 243483c41143SJohn Baldwin return (rman_adjust_resource(r, start, end)); 243583c41143SJohn Baldwin } 243683c41143SJohn Baldwin 243703719c65SJohn Baldwin static int 243883c41143SJohn Baldwin pcib_release_resource(device_t dev, device_t child, int type, int rid, 243983c41143SJohn Baldwin struct resource *r) 244083c41143SJohn Baldwin { 244183c41143SJohn Baldwin struct pcib_softc *sc; 244283c41143SJohn Baldwin int error; 244383c41143SJohn Baldwin 244483c41143SJohn Baldwin sc = device_get_softc(dev); 244583c41143SJohn Baldwin if (pcib_is_resource_managed(sc, type, r)) { 244683c41143SJohn Baldwin if (rman_get_flags(r) & RF_ACTIVE) { 244783c41143SJohn Baldwin error = bus_deactivate_resource(child, type, rid, r); 244883c41143SJohn Baldwin if (error) 244983c41143SJohn Baldwin return (error); 245083c41143SJohn Baldwin } 245183c41143SJohn Baldwin return (rman_release_resource(r)); 245283c41143SJohn Baldwin } 245383c41143SJohn Baldwin return (bus_generic_release_resource(dev, child, type, rid, r)); 245483c41143SJohn Baldwin } 245583c41143SJohn Baldwin #else 2456bb0d0a8eSMike Smith /* 2457bb0d0a8eSMike Smith * We have to trap resource allocation requests and ensure that the bridge 2458bb0d0a8eSMike Smith * is set up to, or capable of handling them. 2459bb0d0a8eSMike Smith */ 246003719c65SJohn Baldwin static struct resource * 2461bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 24622dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 2463bb0d0a8eSMike Smith { 2464bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 246526043836SJohn Baldwin const char *name, *suffix; 2466a8b354a8SWarner Losh int ok; 2467bb0d0a8eSMike Smith 2468bb0d0a8eSMike Smith /* 2469bb0d0a8eSMike Smith * Fail the allocation for this range if it's not supported. 2470bb0d0a8eSMike Smith */ 247126043836SJohn Baldwin name = device_get_nameunit(child); 247226043836SJohn Baldwin if (name == NULL) { 247326043836SJohn Baldwin name = ""; 247426043836SJohn Baldwin suffix = ""; 247526043836SJohn Baldwin } else 247626043836SJohn Baldwin suffix = " "; 2477bb0d0a8eSMike Smith switch (type) { 2478bb0d0a8eSMike Smith case SYS_RES_IOPORT: 2479a8b354a8SWarner Losh ok = 0; 2480e4b59fc5SWarner Losh if (!pcib_is_io_open(sc)) 2481e4b59fc5SWarner Losh break; 2482a8b354a8SWarner Losh ok = (start >= sc->iobase && end <= sc->iolimit); 2483d98d9b12SMarcel Moolenaar 2484d98d9b12SMarcel Moolenaar /* 2485d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA I/O addresses when the 2486d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 2487d98d9b12SMarcel Moolenaar */ 2488d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_ioport_range(start, end)) 2489d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 2490d98d9b12SMarcel Moolenaar 2491e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 2492a8b354a8SWarner Losh if (!ok) { 249312b8c86eSWarner Losh if (start < sc->iobase) 249412b8c86eSWarner Losh start = sc->iobase; 249512b8c86eSWarner Losh if (end > sc->iolimit) 249612b8c86eSWarner Losh end = sc->iolimit; 24972daa7a07SWarner Losh if (start < end) 24982daa7a07SWarner Losh ok = 1; 2499a8b354a8SWarner Losh } 25001c54ff33SMatthew N. Dodd } else { 2501e4b59fc5SWarner Losh ok = 1; 25029dffe835SWarner Losh #if 0 2503795dceffSWarner Losh /* 2504795dceffSWarner Losh * If we overlap with the subtractive range, then 2505795dceffSWarner Losh * pick the upper range to use. 2506795dceffSWarner Losh */ 2507795dceffSWarner Losh if (start < sc->iolimit && end > sc->iobase) 2508795dceffSWarner Losh start = sc->iolimit + 1; 25099dffe835SWarner Losh #endif 251012b8c86eSWarner Losh } 2511a8b354a8SWarner Losh if (end < start) { 2512da1b038aSJustin Hibbits device_printf(dev, "ioport: end (%jx) < start (%jx)\n", 25132daa7a07SWarner Losh end, start); 2514a8b354a8SWarner Losh start = 0; 2515a8b354a8SWarner Losh end = 0; 2516a8b354a8SWarner Losh ok = 0; 2517a8b354a8SWarner Losh } 2518a8b354a8SWarner Losh if (!ok) { 251926043836SJohn Baldwin device_printf(dev, "%s%srequested unsupported I/O " 2520da1b038aSJustin Hibbits "range 0x%jx-0x%jx (decoding 0x%x-0x%x)\n", 252126043836SJohn Baldwin name, suffix, start, end, sc->iobase, sc->iolimit); 2522bb0d0a8eSMike Smith return (NULL); 2523bb0d0a8eSMike Smith } 25244fa59183SMike Smith if (bootverbose) 25252daa7a07SWarner Losh device_printf(dev, 2526da1b038aSJustin Hibbits "%s%srequested I/O range 0x%jx-0x%jx: in range\n", 252726043836SJohn Baldwin name, suffix, start, end); 2528bb0d0a8eSMike Smith break; 2529bb0d0a8eSMike Smith 2530bb0d0a8eSMike Smith case SYS_RES_MEMORY: 2531a8b354a8SWarner Losh ok = 0; 2532a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 2533a8b354a8SWarner Losh ok = ok || (start >= sc->membase && end <= sc->memlimit); 2534a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) 2535a8b354a8SWarner Losh ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit); 2536d98d9b12SMarcel Moolenaar 2537d98d9b12SMarcel Moolenaar /* 2538d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA memory addresses when the 2539d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 2540d98d9b12SMarcel Moolenaar */ 2541d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_memory_range(start, end)) 2542d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 2543d98d9b12SMarcel Moolenaar 2544e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 2545a8b354a8SWarner Losh if (!ok) { 2546a8b354a8SWarner Losh ok = 1; 2547a8b354a8SWarner Losh if (flags & RF_PREFETCHABLE) { 2548a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 2549a8b354a8SWarner Losh if (start < sc->pmembase) 2550a8b354a8SWarner Losh start = sc->pmembase; 2551a8b354a8SWarner Losh if (end > sc->pmemlimit) 2552a8b354a8SWarner Losh end = sc->pmemlimit; 2553a8b354a8SWarner Losh } else { 2554a8b354a8SWarner Losh ok = 0; 2555a8b354a8SWarner Losh } 2556a8b354a8SWarner Losh } else { /* non-prefetchable */ 2557a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 2558a8b354a8SWarner Losh if (start < sc->membase) 255912b8c86eSWarner Losh start = sc->membase; 256012b8c86eSWarner Losh if (end > sc->memlimit) 256112b8c86eSWarner Losh end = sc->memlimit; 25621c54ff33SMatthew N. Dodd } else { 2563a8b354a8SWarner Losh ok = 0; 2564a8b354a8SWarner Losh } 2565a8b354a8SWarner Losh } 2566a8b354a8SWarner Losh } 2567a8b354a8SWarner Losh } else if (!ok) { 2568e4b59fc5SWarner Losh ok = 1; /* subtractive bridge: always ok */ 25699dffe835SWarner Losh #if 0 2570a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 2571795dceffSWarner Losh if (start < sc->memlimit && end > sc->membase) 2572795dceffSWarner Losh start = sc->memlimit + 1; 2573a8b354a8SWarner Losh } 2574a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 2575795dceffSWarner Losh if (start < sc->pmemlimit && end > sc->pmembase) 2576795dceffSWarner Losh start = sc->pmemlimit + 1; 25771c54ff33SMatthew N. Dodd } 25789dffe835SWarner Losh #endif 257912b8c86eSWarner Losh } 2580a8b354a8SWarner Losh if (end < start) { 2581da1b038aSJustin Hibbits device_printf(dev, "memory: end (%jx) < start (%jx)\n", 25822daa7a07SWarner Losh end, start); 2583a8b354a8SWarner Losh start = 0; 2584a8b354a8SWarner Losh end = 0; 2585a8b354a8SWarner Losh ok = 0; 2586a8b354a8SWarner Losh } 2587a8b354a8SWarner Losh if (!ok && bootverbose) 258834428485SWarner Losh device_printf(dev, 2589da1b038aSJustin Hibbits "%s%srequested unsupported memory range %#jx-%#jx " 2590b0a2d4b8SWarner Losh "(decoding %#jx-%#jx, %#jx-%#jx)\n", 259126043836SJohn Baldwin name, suffix, start, end, 2592b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit, 2593b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 2594a8b354a8SWarner Losh if (!ok) 2595bb0d0a8eSMike Smith return (NULL); 25964fa59183SMike Smith if (bootverbose) 259726043836SJohn Baldwin device_printf(dev,"%s%srequested memory range " 2598da1b038aSJustin Hibbits "0x%jx-0x%jx: good\n", 259926043836SJohn Baldwin name, suffix, start, end); 26004fa59183SMike Smith break; 26014fa59183SMike Smith 2602bb0d0a8eSMike Smith default: 26034fa59183SMike Smith break; 2604bb0d0a8eSMike Smith } 2605bb0d0a8eSMike Smith /* 2606bb0d0a8eSMike Smith * Bridge is OK decoding this resource, so pass it up. 2607bb0d0a8eSMike Smith */ 26082daa7a07SWarner Losh return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 26092daa7a07SWarner Losh count, flags)); 2610bb0d0a8eSMike Smith } 261183c41143SJohn Baldwin #endif 2612bb0d0a8eSMike Smith 2613bb0d0a8eSMike Smith /* 261455d3ea17SRyan Stone * If ARI is enabled on this downstream port, translate the function number 261555d3ea17SRyan Stone * to the non-ARI slot/function. The downstream port will convert it back in 261655d3ea17SRyan Stone * hardware. If ARI is not enabled slot and func are not modified. 261755d3ea17SRyan Stone */ 261855d3ea17SRyan Stone static __inline void 261955d3ea17SRyan Stone pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func) 262055d3ea17SRyan Stone { 262155d3ea17SRyan Stone struct pcib_softc *sc; 262255d3ea17SRyan Stone int ari_func; 262355d3ea17SRyan Stone 262455d3ea17SRyan Stone sc = device_get_softc(pcib); 262555d3ea17SRyan Stone ari_func = *func; 262655d3ea17SRyan Stone 262755d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 262855d3ea17SRyan Stone KASSERT(*slot == 0, 262955d3ea17SRyan Stone ("Non-zero slot number with ARI enabled!")); 263055d3ea17SRyan Stone *slot = PCIE_ARI_SLOT(ari_func); 263155d3ea17SRyan Stone *func = PCIE_ARI_FUNC(ari_func); 263255d3ea17SRyan Stone } 263355d3ea17SRyan Stone } 263455d3ea17SRyan Stone 263555d3ea17SRyan Stone static void 263655d3ea17SRyan Stone pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos) 263755d3ea17SRyan Stone { 263855d3ea17SRyan Stone uint32_t ctl2; 263955d3ea17SRyan Stone 264055d3ea17SRyan Stone ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4); 264155d3ea17SRyan Stone ctl2 |= PCIEM_CTL2_ARI; 264255d3ea17SRyan Stone pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4); 264355d3ea17SRyan Stone 264455d3ea17SRyan Stone sc->flags |= PCIB_ENABLE_ARI; 264555d3ea17SRyan Stone } 264655d3ea17SRyan Stone 264755d3ea17SRyan Stone /* 2648bb0d0a8eSMike Smith * PCIB interface. 2649bb0d0a8eSMike Smith */ 26506f0d5884SJohn Baldwin int 2651bb0d0a8eSMike Smith pcib_maxslots(device_t dev) 2652bb0d0a8eSMike Smith { 26535502348dSJustin Hibbits #if !defined(__amd64__) && !defined(__i386__) 26548b92ad43SJustin Hibbits uint32_t pcie_pos; 26558b92ad43SJustin Hibbits uint16_t val; 26568b92ad43SJustin Hibbits 26578b92ad43SJustin Hibbits /* 26588b92ad43SJustin Hibbits * If this is a PCIe rootport or downstream switch port, there's only 26598b92ad43SJustin Hibbits * one slot permitted. 26608b92ad43SJustin Hibbits */ 26618b92ad43SJustin Hibbits if (pci_find_cap(dev, PCIY_EXPRESS, &pcie_pos) == 0) { 26628b92ad43SJustin Hibbits val = pci_read_config(dev, pcie_pos + PCIER_FLAGS, 2); 26638b92ad43SJustin Hibbits val &= PCIEM_FLAGS_TYPE; 26648b92ad43SJustin Hibbits if (val == PCIEM_TYPE_ROOT_PORT || 26658b92ad43SJustin Hibbits val == PCIEM_TYPE_DOWNSTREAM_PORT) 26668b92ad43SJustin Hibbits return (0); 26678b92ad43SJustin Hibbits } 26685502348dSJustin Hibbits #endif 26694fa59183SMike Smith return (PCI_SLOTMAX); 2670bb0d0a8eSMike Smith } 2671bb0d0a8eSMike Smith 267255d3ea17SRyan Stone static int 267355d3ea17SRyan Stone pcib_ari_maxslots(device_t dev) 267455d3ea17SRyan Stone { 267555d3ea17SRyan Stone struct pcib_softc *sc; 267655d3ea17SRyan Stone 267755d3ea17SRyan Stone sc = device_get_softc(dev); 267855d3ea17SRyan Stone 267955d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 268055d3ea17SRyan Stone return (PCIE_ARI_SLOTMAX); 268155d3ea17SRyan Stone else 26828b92ad43SJustin Hibbits return (pcib_maxslots(dev)); 268355d3ea17SRyan Stone } 268455d3ea17SRyan Stone 268555d3ea17SRyan Stone static int 268655d3ea17SRyan Stone pcib_ari_maxfuncs(device_t dev) 268755d3ea17SRyan Stone { 268855d3ea17SRyan Stone struct pcib_softc *sc; 268955d3ea17SRyan Stone 269055d3ea17SRyan Stone sc = device_get_softc(dev); 269155d3ea17SRyan Stone 269255d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 269355d3ea17SRyan Stone return (PCIE_ARI_FUNCMAX); 269455d3ea17SRyan Stone else 269555d3ea17SRyan Stone return (PCI_FUNCMAX); 269655d3ea17SRyan Stone } 269755d3ea17SRyan Stone 26982397d2d8SRyan Stone static void 26992397d2d8SRyan Stone pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot, 27002397d2d8SRyan Stone int *func) 27012397d2d8SRyan Stone { 27022397d2d8SRyan Stone struct pcib_softc *sc; 27032397d2d8SRyan Stone 27042397d2d8SRyan Stone sc = device_get_softc(pcib); 27052397d2d8SRyan Stone 27062397d2d8SRyan Stone *bus = PCI_RID2BUS(rid); 27072397d2d8SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 27082397d2d8SRyan Stone *slot = PCIE_ARI_RID2SLOT(rid); 27092397d2d8SRyan Stone *func = PCIE_ARI_RID2FUNC(rid); 27102397d2d8SRyan Stone } else { 27112397d2d8SRyan Stone *slot = PCI_RID2SLOT(rid); 27122397d2d8SRyan Stone *func = PCI_RID2FUNC(rid); 27132397d2d8SRyan Stone } 27142397d2d8SRyan Stone } 27152397d2d8SRyan Stone 2716bb0d0a8eSMike Smith /* 2717bb0d0a8eSMike Smith * Since we are a child of a PCI bus, its parent must support the pcib interface. 2718bb0d0a8eSMike Smith */ 271955d3ea17SRyan Stone static uint32_t 2720795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width) 2721bb0d0a8eSMike Smith { 272282cb5c3bSJohn Baldwin #ifdef PCI_HP 272382cb5c3bSJohn Baldwin struct pcib_softc *sc; 272455d3ea17SRyan Stone 272582cb5c3bSJohn Baldwin sc = device_get_softc(dev); 272682cb5c3bSJohn Baldwin if (!pcib_present(sc)) { 272782cb5c3bSJohn Baldwin switch (width) { 272882cb5c3bSJohn Baldwin case 2: 272982cb5c3bSJohn Baldwin return (0xffff); 273082cb5c3bSJohn Baldwin case 1: 273182cb5c3bSJohn Baldwin return (0xff); 273282cb5c3bSJohn Baldwin default: 273382cb5c3bSJohn Baldwin return (0xffffffff); 273482cb5c3bSJohn Baldwin } 273582cb5c3bSJohn Baldwin } 273682cb5c3bSJohn Baldwin #endif 273755d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 273855d3ea17SRyan Stone return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, 273955d3ea17SRyan Stone f, reg, width)); 2740bb0d0a8eSMike Smith } 2741bb0d0a8eSMike Smith 274255d3ea17SRyan Stone static void 2743795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width) 2744bb0d0a8eSMike Smith { 274582cb5c3bSJohn Baldwin #ifdef PCI_HP 274682cb5c3bSJohn Baldwin struct pcib_softc *sc; 274755d3ea17SRyan Stone 274882cb5c3bSJohn Baldwin sc = device_get_softc(dev); 274982cb5c3bSJohn Baldwin if (!pcib_present(sc)) 275082cb5c3bSJohn Baldwin return; 275182cb5c3bSJohn Baldwin #endif 275255d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 275355d3ea17SRyan Stone PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, 275455d3ea17SRyan Stone reg, val, width); 2755bb0d0a8eSMike Smith } 2756bb0d0a8eSMike Smith 2757bb0d0a8eSMike Smith /* 2758bb0d0a8eSMike Smith * Route an interrupt across a PCI bridge. 2759bb0d0a8eSMike Smith */ 27602c2d1d07SBenno Rice int 2761bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin) 2762bb0d0a8eSMike Smith { 2763bb0d0a8eSMike Smith device_t bus; 2764bb0d0a8eSMike Smith int parent_intpin; 2765bb0d0a8eSMike Smith int intnum; 2766bb0d0a8eSMike Smith 2767bb0d0a8eSMike Smith /* 2768bb0d0a8eSMike Smith * 2769bb0d0a8eSMike Smith * The PCI standard defines a swizzle of the child-side device/intpin to 2770bb0d0a8eSMike Smith * the parent-side intpin as follows. 2771bb0d0a8eSMike Smith * 2772bb0d0a8eSMike Smith * device = device on child bus 2773bb0d0a8eSMike Smith * child_intpin = intpin on child bus slot (0-3) 2774bb0d0a8eSMike Smith * parent_intpin = intpin on parent bus slot (0-3) 2775bb0d0a8eSMike Smith * 2776bb0d0a8eSMike Smith * parent_intpin = (device + child_intpin) % 4 2777bb0d0a8eSMike Smith */ 2778cdc95e1bSBernd Walter parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4; 2779bb0d0a8eSMike Smith 2780bb0d0a8eSMike Smith /* 2781bb0d0a8eSMike Smith * Our parent is a PCI bus. Its parent must export the pcib interface 2782bb0d0a8eSMike Smith * which includes the ability to route interrupts. 2783bb0d0a8eSMike Smith */ 2784bb0d0a8eSMike Smith bus = device_get_parent(pcib); 2785bb0d0a8eSMike Smith intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1); 278639981fedSJohn Baldwin if (PCI_INTERRUPT_VALID(intnum) && bootverbose) { 2787c6a121abSJohn Baldwin device_printf(pcib, "slot %d INT%c is routed to irq %d\n", 2788c6a121abSJohn Baldwin pci_get_slot(dev), 'A' + pin - 1, intnum); 27898046c4b9SMike Smith } 2790bb0d0a8eSMike Smith return(intnum); 2791bb0d0a8eSMike Smith } 2792b173edafSJohn Baldwin 2793e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */ 27949bf4c9c1SJohn Baldwin int 27959bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs) 27969bf4c9c1SJohn Baldwin { 2797bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 27989bf4c9c1SJohn Baldwin device_t bus; 27999bf4c9c1SJohn Baldwin 280022bf1c7fSJohn Baldwin if (sc->flags & PCIB_DISABLE_MSI) 280122bf1c7fSJohn Baldwin return (ENXIO); 28029bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 28039bf4c9c1SJohn Baldwin return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 28049bf4c9c1SJohn Baldwin irqs)); 28059bf4c9c1SJohn Baldwin } 28069bf4c9c1SJohn Baldwin 2807e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */ 28089bf4c9c1SJohn Baldwin int 28099bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs) 28109bf4c9c1SJohn Baldwin { 28119bf4c9c1SJohn Baldwin device_t bus; 28129bf4c9c1SJohn Baldwin 28139bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 28149bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs)); 28159bf4c9c1SJohn Baldwin } 28169bf4c9c1SJohn Baldwin 28179bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */ 28189bf4c9c1SJohn Baldwin int 2819e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq) 28209bf4c9c1SJohn Baldwin { 2821bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 28229bf4c9c1SJohn Baldwin device_t bus; 28239bf4c9c1SJohn Baldwin 282468e9cbd3SMarius Strobl if (sc->flags & PCIB_DISABLE_MSIX) 282522bf1c7fSJohn Baldwin return (ENXIO); 28269bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 2827e706f7f0SJohn Baldwin return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq)); 28285fe82bcaSJohn Baldwin } 28295fe82bcaSJohn Baldwin 28309bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */ 28319bf4c9c1SJohn Baldwin int 28329bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq) 28339bf4c9c1SJohn Baldwin { 28349bf4c9c1SJohn Baldwin device_t bus; 28359bf4c9c1SJohn Baldwin 28369bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 28379bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq)); 28389bf4c9c1SJohn Baldwin } 28399bf4c9c1SJohn Baldwin 2840e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */ 2841e706f7f0SJohn Baldwin int 2842e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, 2843e706f7f0SJohn Baldwin uint32_t *data) 2844e706f7f0SJohn Baldwin { 2845e706f7f0SJohn Baldwin device_t bus; 28464522ac77SLuoqi Chen int error; 2847e706f7f0SJohn Baldwin 2848e706f7f0SJohn Baldwin bus = device_get_parent(pcib); 28494522ac77SLuoqi Chen error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data); 28504522ac77SLuoqi Chen if (error) 28514522ac77SLuoqi Chen return (error); 28524522ac77SLuoqi Chen 28534522ac77SLuoqi Chen pci_ht_map_msi(pcib, *addr); 28544522ac77SLuoqi Chen return (0); 2855e706f7f0SJohn Baldwin } 2856e706f7f0SJohn Baldwin 285762508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */ 285862508c53SJohn Baldwin int 285962508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate) 286062508c53SJohn Baldwin { 286162508c53SJohn Baldwin device_t bus; 286262508c53SJohn Baldwin 286362508c53SJohn Baldwin bus = device_get_parent(pcib); 286462508c53SJohn Baldwin return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate)); 286562508c53SJohn Baldwin } 28665605a99eSRyan Stone 28672397d2d8SRyan Stone static int 28682397d2d8SRyan Stone pcib_ari_enabled(device_t pcib) 28692397d2d8SRyan Stone { 28702397d2d8SRyan Stone struct pcib_softc *sc; 28712397d2d8SRyan Stone 28722397d2d8SRyan Stone sc = device_get_softc(pcib); 28732397d2d8SRyan Stone 28742397d2d8SRyan Stone return ((sc->flags & PCIB_ENABLE_ARI) != 0); 28752397d2d8SRyan Stone } 28762397d2d8SRyan Stone 2877d7be980dSAndrew Turner static int 2878d7be980dSAndrew Turner pcib_ari_get_id(device_t pcib, device_t dev, enum pci_id_type type, 2879d7be980dSAndrew Turner uintptr_t *id) 288055d3ea17SRyan Stone { 288155d3ea17SRyan Stone struct pcib_softc *sc; 28821e43b18cSAndrew Turner device_t bus_dev; 288355d3ea17SRyan Stone uint8_t bus, slot, func; 288455d3ea17SRyan Stone 28851e43b18cSAndrew Turner if (type != PCI_ID_RID) { 28861e43b18cSAndrew Turner bus_dev = device_get_parent(pcib); 28871e43b18cSAndrew Turner return (PCIB_GET_ID(device_get_parent(bus_dev), dev, type, id)); 28881e43b18cSAndrew Turner } 2889d7be980dSAndrew Turner 289055d3ea17SRyan Stone sc = device_get_softc(pcib); 289155d3ea17SRyan Stone 289255d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 289355d3ea17SRyan Stone bus = pci_get_bus(dev); 289455d3ea17SRyan Stone func = pci_get_function(dev); 289555d3ea17SRyan Stone 2896d7be980dSAndrew Turner *id = (PCI_ARI_RID(bus, func)); 289755d3ea17SRyan Stone } else { 289855d3ea17SRyan Stone bus = pci_get_bus(dev); 289955d3ea17SRyan Stone slot = pci_get_slot(dev); 290055d3ea17SRyan Stone func = pci_get_function(dev); 290155d3ea17SRyan Stone 2902d7be980dSAndrew Turner *id = (PCI_RID(bus, slot, func)); 290355d3ea17SRyan Stone } 2904d7be980dSAndrew Turner 2905d7be980dSAndrew Turner return (0); 290655d3ea17SRyan Stone } 290755d3ea17SRyan Stone 290855d3ea17SRyan Stone /* 290955d3ea17SRyan Stone * Check that the downstream port (pcib) and the endpoint device (dev) both 291055d3ea17SRyan Stone * support ARI. If so, enable it and return 0, otherwise return an error. 291155d3ea17SRyan Stone */ 291255d3ea17SRyan Stone static int 291355d3ea17SRyan Stone pcib_try_enable_ari(device_t pcib, device_t dev) 291455d3ea17SRyan Stone { 291555d3ea17SRyan Stone struct pcib_softc *sc; 291655d3ea17SRyan Stone int error; 291755d3ea17SRyan Stone uint32_t cap2; 291855d3ea17SRyan Stone int ari_cap_off; 291955d3ea17SRyan Stone uint32_t ari_ver; 292055d3ea17SRyan Stone uint32_t pcie_pos; 292155d3ea17SRyan Stone 292255d3ea17SRyan Stone sc = device_get_softc(pcib); 292355d3ea17SRyan Stone 292455d3ea17SRyan Stone /* 292555d3ea17SRyan Stone * ARI is controlled in a register in the PCIe capability structure. 292655d3ea17SRyan Stone * If the downstream port does not have the PCIe capability structure 292755d3ea17SRyan Stone * then it does not support ARI. 292855d3ea17SRyan Stone */ 292955d3ea17SRyan Stone error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos); 293055d3ea17SRyan Stone if (error != 0) 293155d3ea17SRyan Stone return (ENODEV); 293255d3ea17SRyan Stone 293355d3ea17SRyan Stone /* Check that the PCIe port advertises ARI support. */ 293455d3ea17SRyan Stone cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4); 293555d3ea17SRyan Stone if (!(cap2 & PCIEM_CAP2_ARI)) 293655d3ea17SRyan Stone return (ENODEV); 293755d3ea17SRyan Stone 293855d3ea17SRyan Stone /* 293955d3ea17SRyan Stone * Check that the endpoint device advertises ARI support via the ARI 294055d3ea17SRyan Stone * extended capability structure. 294155d3ea17SRyan Stone */ 294255d3ea17SRyan Stone error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off); 294355d3ea17SRyan Stone if (error != 0) 294455d3ea17SRyan Stone return (ENODEV); 294555d3ea17SRyan Stone 294655d3ea17SRyan Stone /* 294755d3ea17SRyan Stone * Finally, check that the endpoint device supports the same version 294855d3ea17SRyan Stone * of ARI that we do. 294955d3ea17SRyan Stone */ 295055d3ea17SRyan Stone ari_ver = pci_read_config(dev, ari_cap_off, 4); 295155d3ea17SRyan Stone if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) { 295255d3ea17SRyan Stone if (bootverbose) 295355d3ea17SRyan Stone device_printf(pcib, 295455d3ea17SRyan Stone "Unsupported version of ARI (%d) detected\n", 295555d3ea17SRyan Stone PCI_EXTCAP_VER(ari_ver)); 295655d3ea17SRyan Stone 295755d3ea17SRyan Stone return (ENXIO); 295855d3ea17SRyan Stone } 295955d3ea17SRyan Stone 296055d3ea17SRyan Stone pcib_enable_ari(sc, pcie_pos); 296155d3ea17SRyan Stone 296255d3ea17SRyan Stone return (0); 296355d3ea17SRyan Stone } 29644cb67729SWarner Losh 296528586889SWarner Losh int 296628586889SWarner Losh pcib_request_feature_allow(device_t pcib, device_t dev, 296728586889SWarner Losh enum pci_feature feature) 296828586889SWarner Losh { 296928586889SWarner Losh /* 29705914c62eSGavin Atkinson * No host firmware we have to negotiate with, so we allow 297128586889SWarner Losh * every valid feature requested. 297228586889SWarner Losh */ 297328586889SWarner Losh switch (feature) { 297428586889SWarner Losh case PCI_FEATURE_AER: 297528586889SWarner Losh case PCI_FEATURE_HP: 297628586889SWarner Losh break; 297728586889SWarner Losh default: 297828586889SWarner Losh return (EINVAL); 297928586889SWarner Losh } 298028586889SWarner Losh 298128586889SWarner Losh return (0); 298228586889SWarner Losh } 298328586889SWarner Losh 29841ffd07bdSJohn Baldwin int 29851ffd07bdSJohn Baldwin pcib_request_feature(device_t dev, enum pci_feature feature) 29861ffd07bdSJohn Baldwin { 29871ffd07bdSJohn Baldwin 29881ffd07bdSJohn Baldwin /* 29891ffd07bdSJohn Baldwin * Invoke PCIB_REQUEST_FEATURE of this bridge first in case 29901ffd07bdSJohn Baldwin * the firmware overrides the method of PCI-PCI bridges. 29911ffd07bdSJohn Baldwin */ 29921ffd07bdSJohn Baldwin return (PCIB_REQUEST_FEATURE(dev, dev, feature)); 29931ffd07bdSJohn Baldwin } 29941ffd07bdSJohn Baldwin 29954cb67729SWarner Losh /* 29964cb67729SWarner Losh * Pass the request to use this PCI feature up the tree. Either there's a 29974cb67729SWarner Losh * firmware like ACPI that's using this feature that will approve (or deny) the 29984cb67729SWarner Losh * request to take it over, or the platform has no such firmware, in which case 29994cb67729SWarner Losh * the request will be approved. If the request is approved, the OS is expected 30004cb67729SWarner Losh * to make use of the feature or render it harmless. 30014cb67729SWarner Losh */ 30024cb67729SWarner Losh static int 30031ffd07bdSJohn Baldwin pcib_request_feature_default(device_t pcib, device_t dev, 30041ffd07bdSJohn Baldwin enum pci_feature feature) 30054cb67729SWarner Losh { 30064cb67729SWarner Losh device_t bus; 30074cb67729SWarner Losh 30084cb67729SWarner Losh /* 30094cb67729SWarner Losh * Our parent is necessarily a pci bus. Its parent will either be 30104cb67729SWarner Losh * another pci bridge (which passes it up) or a host bridge that can 30114cb67729SWarner Losh * approve or reject the request. 30124cb67729SWarner Losh */ 30134cb67729SWarner Losh bus = device_get_parent(pcib); 30144cb67729SWarner Losh return (PCIB_REQUEST_FEATURE(device_get_parent(bus), dev, feature)); 30154cb67729SWarner Losh } 30165db2a4a8SKonstantin Belousov 30175db2a4a8SKonstantin Belousov static int 30185db2a4a8SKonstantin Belousov pcib_reset_child(device_t dev, device_t child, int flags) 30195db2a4a8SKonstantin Belousov { 30205db2a4a8SKonstantin Belousov struct pci_devinfo *pdinfo; 30215db2a4a8SKonstantin Belousov int error; 30225db2a4a8SKonstantin Belousov 30235db2a4a8SKonstantin Belousov error = 0; 30245db2a4a8SKonstantin Belousov if (dev == NULL || device_get_parent(child) != dev) 30255db2a4a8SKonstantin Belousov goto out; 30265db2a4a8SKonstantin Belousov error = ENXIO; 30275db2a4a8SKonstantin Belousov if (device_get_devclass(child) != devclass_find("pci")) 30285db2a4a8SKonstantin Belousov goto out; 30295db2a4a8SKonstantin Belousov pdinfo = device_get_ivars(dev); 30305db2a4a8SKonstantin Belousov if (pdinfo->cfg.pcie.pcie_location != 0 && 30315db2a4a8SKonstantin Belousov (pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_DOWNSTREAM_PORT || 30325db2a4a8SKonstantin Belousov pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_ROOT_PORT)) { 30335db2a4a8SKonstantin Belousov error = bus_helper_reset_prepare(child, flags); 30345db2a4a8SKonstantin Belousov if (error == 0) { 30355db2a4a8SKonstantin Belousov error = pcie_link_reset(dev, 30365db2a4a8SKonstantin Belousov pdinfo->cfg.pcie.pcie_location); 30375db2a4a8SKonstantin Belousov /* XXXKIB call _post even if error != 0 ? */ 30385db2a4a8SKonstantin Belousov bus_helper_reset_post(child, flags); 30395db2a4a8SKonstantin Belousov } 30405db2a4a8SKonstantin Belousov } 30415db2a4a8SKonstantin Belousov out: 30425db2a4a8SKonstantin Belousov return (error); 30435db2a4a8SKonstantin Belousov } 3044