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; 69b377ff81SJohn Baldwin static bus_activate_resource_t pcib_activate_resource; 70b377ff81SJohn Baldwin static bus_deactivate_resource_t pcib_deactivate_resource; 71b377ff81SJohn Baldwin static bus_map_resource_t pcib_map_resource; 72b377ff81SJohn Baldwin static bus_unmap_resource_t pcib_unmap_resource; 7303719c65SJohn Baldwin #endif 7403719c65SJohn Baldwin static int pcib_reset_child(device_t dev, device_t child, int flags); 7503719c65SJohn Baldwin 7662508c53SJohn Baldwin static int pcib_power_for_sleep(device_t pcib, device_t dev, 7762508c53SJohn Baldwin int *pstate); 78d7be980dSAndrew Turner static int pcib_ari_get_id(device_t pcib, device_t dev, 79d7be980dSAndrew Turner enum pci_id_type type, uintptr_t *id); 8055d3ea17SRyan Stone static uint32_t pcib_read_config(device_t dev, u_int b, u_int s, 8155d3ea17SRyan Stone u_int f, u_int reg, int width); 8255d3ea17SRyan Stone static void pcib_write_config(device_t dev, u_int b, u_int s, 8355d3ea17SRyan Stone u_int f, u_int reg, uint32_t val, int width); 8455d3ea17SRyan Stone static int pcib_ari_maxslots(device_t dev); 8555d3ea17SRyan Stone static int pcib_ari_maxfuncs(device_t dev); 8655d3ea17SRyan Stone static int pcib_try_enable_ari(device_t pcib, device_t dev); 872397d2d8SRyan Stone static int pcib_ari_enabled(device_t pcib); 882397d2d8SRyan Stone static void pcib_ari_decode_rid(device_t pcib, uint16_t rid, 892397d2d8SRyan Stone int *bus, int *slot, int *func); 9082cb5c3bSJohn Baldwin #ifdef PCI_HP 91fa3b03d3SAlexander Motin static void pcib_pcie_ab_timeout(void *arg, int pending); 92fa3b03d3SAlexander Motin static void pcib_pcie_cc_timeout(void *arg, int pending); 93fa3b03d3SAlexander Motin static void pcib_pcie_dll_timeout(void *arg, int pending); 9482cb5c3bSJohn Baldwin #endif 951ffd07bdSJohn Baldwin static int pcib_request_feature_default(device_t pcib, device_t dev, 964cb67729SWarner Losh enum pci_feature feature); 97bb0d0a8eSMike Smith 98bb0d0a8eSMike Smith static device_method_t pcib_methods[] = { 99bb0d0a8eSMike Smith /* Device interface */ 100bb0d0a8eSMike Smith DEVMETHOD(device_probe, pcib_probe), 101bb0d0a8eSMike Smith DEVMETHOD(device_attach, pcib_attach), 1026f33eaa5SJohn Baldwin DEVMETHOD(device_detach, pcib_detach), 103bb0d0a8eSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 104e36af292SJung-uk Kim DEVMETHOD(device_suspend, pcib_suspend), 105e36af292SJung-uk Kim DEVMETHOD(device_resume, pcib_resume), 106bb0d0a8eSMike Smith 107bb0d0a8eSMike Smith /* Bus interface */ 10882cb5c3bSJohn Baldwin DEVMETHOD(bus_child_present, pcib_child_present), 109bb0d0a8eSMike Smith DEVMETHOD(bus_read_ivar, pcib_read_ivar), 110bb0d0a8eSMike Smith DEVMETHOD(bus_write_ivar, pcib_write_ivar), 111bb0d0a8eSMike Smith DEVMETHOD(bus_alloc_resource, pcib_alloc_resource), 11283c41143SJohn Baldwin #ifdef NEW_PCIB 11383c41143SJohn Baldwin DEVMETHOD(bus_adjust_resource, pcib_adjust_resource), 11483c41143SJohn Baldwin DEVMETHOD(bus_release_resource, pcib_release_resource), 115b377ff81SJohn Baldwin DEVMETHOD(bus_activate_resource, pcib_activate_resource), 116b377ff81SJohn Baldwin DEVMETHOD(bus_deactivate_resource, pcib_deactivate_resource), 117b377ff81SJohn Baldwin DEVMETHOD(bus_map_resource, pcib_map_resource), 118b377ff81SJohn Baldwin DEVMETHOD(bus_unmap_resource, pcib_unmap_resource), 11983c41143SJohn Baldwin #else 120d2c9344fSJohn Baldwin DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 121bb0d0a8eSMike Smith DEVMETHOD(bus_release_resource, bus_generic_release_resource), 122bb0d0a8eSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 123bb0d0a8eSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 124b377ff81SJohn Baldwin #endif 125bb0d0a8eSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 126bb0d0a8eSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 1275db2a4a8SKonstantin Belousov DEVMETHOD(bus_reset_child, pcib_reset_child), 128bb0d0a8eSMike Smith 129bb0d0a8eSMike Smith /* pcib interface */ 13055d3ea17SRyan Stone DEVMETHOD(pcib_maxslots, pcib_ari_maxslots), 13155d3ea17SRyan Stone DEVMETHOD(pcib_maxfuncs, pcib_ari_maxfuncs), 132bb0d0a8eSMike Smith DEVMETHOD(pcib_read_config, pcib_read_config), 133bb0d0a8eSMike Smith DEVMETHOD(pcib_write_config, pcib_write_config), 134bb0d0a8eSMike Smith DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt), 1359bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi), 1369bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msi, pcib_release_msi), 1379bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix), 1389bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msix, pcib_release_msix), 139e706f7f0SJohn Baldwin DEVMETHOD(pcib_map_msi, pcib_map_msi), 14062508c53SJohn Baldwin DEVMETHOD(pcib_power_for_sleep, pcib_power_for_sleep), 141d7be980dSAndrew Turner DEVMETHOD(pcib_get_id, pcib_ari_get_id), 14255d3ea17SRyan Stone DEVMETHOD(pcib_try_enable_ari, pcib_try_enable_ari), 1432397d2d8SRyan Stone DEVMETHOD(pcib_ari_enabled, pcib_ari_enabled), 1442397d2d8SRyan Stone DEVMETHOD(pcib_decode_rid, pcib_ari_decode_rid), 1451ffd07bdSJohn Baldwin DEVMETHOD(pcib_request_feature, pcib_request_feature_default), 146bb0d0a8eSMike Smith 1474b7ec270SMarius Strobl DEVMETHOD_END 148bb0d0a8eSMike Smith }; 149bb0d0a8eSMike Smith 15004dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc)); 15197a41013SJohn Baldwin EARLY_DRIVER_MODULE(pcib, pci, pcib_driver, NULL, NULL, BUS_PASS_BUS); 152bb0d0a8eSMike Smith 1536ca2d094SBjoern A. Zeeb #if defined(NEW_PCIB) || defined(PCI_HP) 1540070c94bSJohn Baldwin SYSCTL_DECL(_hw_pci); 1556ca2d094SBjoern A. Zeeb #endif 1560070c94bSJohn Baldwin 1576ca2d094SBjoern A. Zeeb #ifdef NEW_PCIB 1580070c94bSJohn Baldwin static int pci_clear_pcib; 1590070c94bSJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0, 1600070c94bSJohn Baldwin "Clear firmware-assigned resources for PCI-PCI bridge I/O windows."); 16183c41143SJohn Baldwin 16283c41143SJohn Baldwin /* 16326245980SJessica Clarke * Get the corresponding window if this resource from a child device was 16426245980SJessica Clarke * sub-allocated from one of our window resource managers. 16526245980SJessica Clarke */ 16626245980SJessica Clarke static struct pcib_window * 167582b84cdSJohn Baldwin pcib_get_resource_window(struct pcib_softc *sc, struct resource *r) 16826245980SJessica Clarke { 169582b84cdSJohn Baldwin switch (rman_get_type(r)) { 17026245980SJessica Clarke case SYS_RES_IOPORT: 17126245980SJessica Clarke if (rman_is_region_manager(r, &sc->io.rman)) 17226245980SJessica Clarke return (&sc->io); 17326245980SJessica Clarke break; 17426245980SJessica Clarke case SYS_RES_MEMORY: 17526245980SJessica Clarke /* Prefetchable resources may live in either memory rman. */ 17626245980SJessica Clarke if (rman_get_flags(r) & RF_PREFETCHABLE && 17726245980SJessica Clarke rman_is_region_manager(r, &sc->pmem.rman)) 17826245980SJessica Clarke return (&sc->pmem); 17926245980SJessica Clarke if (rman_is_region_manager(r, &sc->mem.rman)) 18026245980SJessica Clarke return (&sc->mem); 18126245980SJessica Clarke break; 18226245980SJessica Clarke } 18326245980SJessica Clarke return (NULL); 18426245980SJessica Clarke } 18526245980SJessica Clarke 18626245980SJessica Clarke /* 18783c41143SJohn Baldwin * Is a resource from a child device sub-allocated from one of our 18883c41143SJohn Baldwin * resource managers? 18983c41143SJohn Baldwin */ 19083c41143SJohn Baldwin static int 191582b84cdSJohn Baldwin pcib_is_resource_managed(struct pcib_softc *sc, struct resource *r) 19283c41143SJohn Baldwin { 19383c41143SJohn Baldwin 1944edef187SJohn Baldwin #ifdef PCI_RES_BUS 195582b84cdSJohn Baldwin if (rman_get_type(r) == PCI_RES_BUS) 1964edef187SJohn Baldwin return (rman_is_region_manager(r, &sc->bus.rman)); 1974edef187SJohn Baldwin #endif 198582b84cdSJohn Baldwin return (pcib_get_resource_window(sc, r) != NULL); 19983c41143SJohn Baldwin } 20083c41143SJohn Baldwin 20183c41143SJohn Baldwin static int 20283c41143SJohn Baldwin pcib_is_window_open(struct pcib_window *pw) 20383c41143SJohn Baldwin { 20483c41143SJohn Baldwin 20583c41143SJohn Baldwin return (pw->valid && pw->base < pw->limit); 20683c41143SJohn Baldwin } 20783c41143SJohn Baldwin 20883c41143SJohn Baldwin /* 20983c41143SJohn Baldwin * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and 21083c41143SJohn Baldwin * handle for the resource, we could pass RF_ACTIVE up to the PCI bus 21183c41143SJohn Baldwin * when allocating the resource windows and rely on the PCI bus driver 21283c41143SJohn Baldwin * to do this for us. 21383c41143SJohn Baldwin */ 21483c41143SJohn Baldwin static void 21583c41143SJohn Baldwin pcib_activate_window(struct pcib_softc *sc, int type) 21683c41143SJohn Baldwin { 21783c41143SJohn Baldwin 21883c41143SJohn Baldwin PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type); 21983c41143SJohn Baldwin } 22083c41143SJohn Baldwin 22183c41143SJohn Baldwin static void 22283c41143SJohn Baldwin pcib_write_windows(struct pcib_softc *sc, int mask) 22383c41143SJohn Baldwin { 22483c41143SJohn Baldwin device_t dev; 22583c41143SJohn Baldwin uint32_t val; 22683c41143SJohn Baldwin 22783c41143SJohn Baldwin dev = sc->dev; 22883c41143SJohn Baldwin if (sc->io.valid && mask & WIN_IO) { 22983c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 23083c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 23183c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEH_1, 23283c41143SJohn Baldwin sc->io.base >> 16, 2); 23383c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITH_1, 23483c41143SJohn Baldwin sc->io.limit >> 16, 2); 23583c41143SJohn Baldwin } 23683c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1); 23783c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1); 23883c41143SJohn Baldwin } 23983c41143SJohn Baldwin 24083c41143SJohn Baldwin if (mask & WIN_MEM) { 24183c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2); 24283c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2); 24383c41143SJohn Baldwin } 24483c41143SJohn Baldwin 24583c41143SJohn Baldwin if (sc->pmem.valid && mask & WIN_PMEM) { 24683c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 24783c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 24883c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEH_1, 24983c41143SJohn Baldwin sc->pmem.base >> 32, 4); 25083c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITH_1, 25183c41143SJohn Baldwin sc->pmem.limit >> 32, 4); 25283c41143SJohn Baldwin } 25383c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2); 25483c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2); 25583c41143SJohn Baldwin } 25683c41143SJohn Baldwin } 25783c41143SJohn Baldwin 258c825d4dcSJohn Baldwin /* 259c825d4dcSJohn Baldwin * This is used to reject I/O port allocations that conflict with an 260c825d4dcSJohn Baldwin * ISA alias range. 261c825d4dcSJohn Baldwin */ 262c825d4dcSJohn Baldwin static int 2632dd1bdf1SJustin Hibbits pcib_is_isa_range(struct pcib_softc *sc, rman_res_t start, rman_res_t end, 2642dd1bdf1SJustin Hibbits rman_res_t count) 265c825d4dcSJohn Baldwin { 2662dd1bdf1SJustin Hibbits rman_res_t next_alias; 267c825d4dcSJohn Baldwin 268c825d4dcSJohn Baldwin if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE)) 269c825d4dcSJohn Baldwin return (0); 270c825d4dcSJohn Baldwin 271c825d4dcSJohn Baldwin /* Only check fixed ranges for overlap. */ 272c825d4dcSJohn Baldwin if (start + count - 1 != end) 273c825d4dcSJohn Baldwin return (0); 274c825d4dcSJohn Baldwin 275c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */ 276c825d4dcSJohn Baldwin if (start >= 65536) 277c825d4dcSJohn Baldwin return (0); 278c825d4dcSJohn Baldwin 279c825d4dcSJohn Baldwin /* Check for overlap with 0x000 - 0x0ff as a special case. */ 280c825d4dcSJohn Baldwin if (start < 0x100) 281c825d4dcSJohn Baldwin goto alias; 282c825d4dcSJohn Baldwin 283c825d4dcSJohn Baldwin /* 284c825d4dcSJohn Baldwin * If the start address is an alias, the range is an alias. 285c825d4dcSJohn Baldwin * Otherwise, compute the start of the next alias range and 286c825d4dcSJohn Baldwin * check if it is before the end of the candidate range. 287c825d4dcSJohn Baldwin */ 288c825d4dcSJohn Baldwin if ((start & 0x300) != 0) 289c825d4dcSJohn Baldwin goto alias; 290c825d4dcSJohn Baldwin next_alias = (start & ~0x3fful) | 0x100; 291c825d4dcSJohn Baldwin if (next_alias <= end) 292c825d4dcSJohn Baldwin goto alias; 293c825d4dcSJohn Baldwin return (0); 294c825d4dcSJohn Baldwin 295c825d4dcSJohn Baldwin alias: 296c825d4dcSJohn Baldwin if (bootverbose) 297c825d4dcSJohn Baldwin device_printf(sc->dev, 298da1b038aSJustin Hibbits "I/O range %#jx-%#jx overlaps with an ISA alias\n", start, 299c825d4dcSJohn Baldwin end); 300c825d4dcSJohn Baldwin return (1); 301c825d4dcSJohn Baldwin } 302c825d4dcSJohn Baldwin 303c825d4dcSJohn Baldwin static void 304c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res, 305c825d4dcSJohn Baldwin int count) 306c825d4dcSJohn Baldwin { 307c825d4dcSJohn Baldwin struct resource **newarray; 308c825d4dcSJohn Baldwin int error, i; 309c825d4dcSJohn Baldwin 310c825d4dcSJohn Baldwin newarray = malloc(sizeof(struct resource *) * (w->count + count), 311c825d4dcSJohn Baldwin M_DEVBUF, M_WAITOK); 312c825d4dcSJohn Baldwin if (w->res != NULL) 313c825d4dcSJohn Baldwin bcopy(w->res, newarray, sizeof(struct resource *) * w->count); 314c825d4dcSJohn Baldwin bcopy(res, newarray + w->count, sizeof(struct resource *) * count); 315c825d4dcSJohn Baldwin free(w->res, M_DEVBUF); 316c825d4dcSJohn Baldwin w->res = newarray; 317c825d4dcSJohn Baldwin w->count += count; 318c825d4dcSJohn Baldwin 319c825d4dcSJohn Baldwin for (i = 0; i < count; i++) { 320c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, rman_get_start(res[i]), 321c825d4dcSJohn Baldwin rman_get_end(res[i])); 322c825d4dcSJohn Baldwin if (error) 323c825d4dcSJohn Baldwin panic("Failed to add resource to rman"); 324c825d4dcSJohn Baldwin } 325c825d4dcSJohn Baldwin } 326c825d4dcSJohn Baldwin 3272dd1bdf1SJustin Hibbits typedef void (nonisa_callback)(rman_res_t start, rman_res_t end, void *arg); 328c825d4dcSJohn Baldwin 329c825d4dcSJohn Baldwin static void 3302dd1bdf1SJustin Hibbits pcib_walk_nonisa_ranges(rman_res_t start, rman_res_t end, nonisa_callback *cb, 331c825d4dcSJohn Baldwin void *arg) 332c825d4dcSJohn Baldwin { 3332dd1bdf1SJustin Hibbits rman_res_t next_end; 334c825d4dcSJohn Baldwin 335c825d4dcSJohn Baldwin /* 336c825d4dcSJohn Baldwin * If start is within an ISA alias range, move up to the start 337c825d4dcSJohn Baldwin * of the next non-alias range. As a special case, addresses 338c825d4dcSJohn Baldwin * in the range 0x000 - 0x0ff should also be skipped since 339c825d4dcSJohn Baldwin * those are used for various system I/O devices in ISA 340c825d4dcSJohn Baldwin * systems. 341c825d4dcSJohn Baldwin */ 342c825d4dcSJohn Baldwin if (start <= 65535) { 343c825d4dcSJohn Baldwin if (start < 0x100 || (start & 0x300) != 0) { 344c825d4dcSJohn Baldwin start &= ~0x3ff; 345c825d4dcSJohn Baldwin start += 0x400; 346c825d4dcSJohn Baldwin } 347c825d4dcSJohn Baldwin } 348c825d4dcSJohn Baldwin 349c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */ 350c825d4dcSJohn Baldwin while (start <= MIN(end, 65535)) { 351c825d4dcSJohn Baldwin next_end = MIN(start | 0xff, end); 352c825d4dcSJohn Baldwin cb(start, next_end, arg); 353c825d4dcSJohn Baldwin start += 0x400; 354c825d4dcSJohn Baldwin } 355c825d4dcSJohn Baldwin 356c825d4dcSJohn Baldwin if (start <= end) 357c825d4dcSJohn Baldwin cb(start, end, arg); 358c825d4dcSJohn Baldwin } 359c825d4dcSJohn Baldwin 360c825d4dcSJohn Baldwin static void 3612dd1bdf1SJustin Hibbits count_ranges(rman_res_t start, rman_res_t end, void *arg) 362c825d4dcSJohn Baldwin { 363c825d4dcSJohn Baldwin int *countp; 364c825d4dcSJohn Baldwin 365c825d4dcSJohn Baldwin countp = arg; 366c825d4dcSJohn Baldwin (*countp)++; 367c825d4dcSJohn Baldwin } 368c825d4dcSJohn Baldwin 369c825d4dcSJohn Baldwin struct alloc_state { 370c825d4dcSJohn Baldwin struct resource **res; 371c825d4dcSJohn Baldwin struct pcib_softc *sc; 372c825d4dcSJohn Baldwin int count, error; 373c825d4dcSJohn Baldwin }; 374c825d4dcSJohn Baldwin 375c825d4dcSJohn Baldwin static void 3762dd1bdf1SJustin Hibbits alloc_ranges(rman_res_t start, rman_res_t end, void *arg) 377c825d4dcSJohn Baldwin { 378c825d4dcSJohn Baldwin struct alloc_state *as; 379c825d4dcSJohn Baldwin struct pcib_window *w; 380c825d4dcSJohn Baldwin int rid; 381c825d4dcSJohn Baldwin 382c825d4dcSJohn Baldwin as = arg; 383c825d4dcSJohn Baldwin if (as->error != 0) 384c825d4dcSJohn Baldwin return; 385c825d4dcSJohn Baldwin 386c825d4dcSJohn Baldwin w = &as->sc->io; 387c825d4dcSJohn Baldwin rid = w->reg; 388c825d4dcSJohn Baldwin if (bootverbose) 389c825d4dcSJohn Baldwin device_printf(as->sc->dev, 390da1b038aSJustin Hibbits "allocating non-ISA range %#jx-%#jx\n", start, end); 391c825d4dcSJohn Baldwin as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT, 392b377ff81SJohn Baldwin &rid, start, end, end - start + 1, RF_ACTIVE | RF_UNMAPPED); 393c825d4dcSJohn Baldwin if (as->res[as->count] == NULL) 394c825d4dcSJohn Baldwin as->error = ENXIO; 395c825d4dcSJohn Baldwin else 396c825d4dcSJohn Baldwin as->count++; 397c825d4dcSJohn Baldwin } 398c825d4dcSJohn Baldwin 399c825d4dcSJohn Baldwin static int 4002dd1bdf1SJustin Hibbits pcib_alloc_nonisa_ranges(struct pcib_softc *sc, rman_res_t start, rman_res_t end) 401c825d4dcSJohn Baldwin { 402c825d4dcSJohn Baldwin struct alloc_state as; 403c825d4dcSJohn Baldwin int i, new_count; 404c825d4dcSJohn Baldwin 405c825d4dcSJohn Baldwin /* First, see how many ranges we need. */ 406c825d4dcSJohn Baldwin new_count = 0; 407c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count); 408c825d4dcSJohn Baldwin 409c825d4dcSJohn Baldwin /* Second, allocate the ranges. */ 410c825d4dcSJohn Baldwin as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF, 411c825d4dcSJohn Baldwin M_WAITOK); 412c825d4dcSJohn Baldwin as.sc = sc; 413c825d4dcSJohn Baldwin as.count = 0; 414c825d4dcSJohn Baldwin as.error = 0; 415c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as); 416c825d4dcSJohn Baldwin if (as.error != 0) { 417c825d4dcSJohn Baldwin for (i = 0; i < as.count; i++) 418c825d4dcSJohn Baldwin bus_release_resource(sc->dev, SYS_RES_IOPORT, 419c825d4dcSJohn Baldwin sc->io.reg, as.res[i]); 420c825d4dcSJohn Baldwin free(as.res, M_DEVBUF); 421c825d4dcSJohn Baldwin return (as.error); 422c825d4dcSJohn Baldwin } 423c825d4dcSJohn Baldwin KASSERT(as.count == new_count, ("%s: count mismatch", __func__)); 424c825d4dcSJohn Baldwin 425c825d4dcSJohn Baldwin /* Third, add the ranges to the window. */ 426c825d4dcSJohn Baldwin pcib_add_window_resources(&sc->io, as.res, as.count); 427c825d4dcSJohn Baldwin free(as.res, M_DEVBUF); 428c825d4dcSJohn Baldwin return (0); 429c825d4dcSJohn Baldwin } 430c825d4dcSJohn Baldwin 43183c41143SJohn Baldwin static void 43283c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type, 43383c41143SJohn Baldwin int flags, pci_addr_t max_address) 43483c41143SJohn Baldwin { 435c825d4dcSJohn Baldwin struct resource *res; 43683c41143SJohn Baldwin char buf[64]; 43783c41143SJohn Baldwin int error, rid; 43883c41143SJohn Baldwin 43989977ce2SJustin Hibbits if (max_address != (rman_res_t)max_address) 440534ccd7bSJustin Hibbits max_address = ~0; 44183c41143SJohn Baldwin w->rman.rm_start = 0; 44283c41143SJohn Baldwin w->rman.rm_end = max_address; 44383c41143SJohn Baldwin w->rman.rm_type = RMAN_ARRAY; 44483c41143SJohn Baldwin snprintf(buf, sizeof(buf), "%s %s window", 44583c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name); 44683c41143SJohn Baldwin w->rman.rm_descr = strdup(buf, M_DEVBUF); 44783c41143SJohn Baldwin error = rman_init(&w->rman); 44883c41143SJohn Baldwin if (error) 44983c41143SJohn Baldwin panic("Failed to initialize %s %s rman", 45083c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name); 45183c41143SJohn Baldwin 45283c41143SJohn Baldwin if (!pcib_is_window_open(w)) 45383c41143SJohn Baldwin return; 45483c41143SJohn Baldwin 45583c41143SJohn Baldwin if (w->base > max_address || w->limit > max_address) { 45683c41143SJohn Baldwin device_printf(sc->dev, 45783c41143SJohn Baldwin "initial %s window has too many bits, ignoring\n", w->name); 45883c41143SJohn Baldwin return; 45983c41143SJohn Baldwin } 460c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE) 461c825d4dcSJohn Baldwin (void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit); 462c825d4dcSJohn Baldwin else { 46383c41143SJohn Baldwin rid = w->reg; 464c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit, 465b377ff81SJohn Baldwin w->limit - w->base + 1, flags | RF_ACTIVE | RF_UNMAPPED); 466c825d4dcSJohn Baldwin if (res != NULL) 467c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 468c825d4dcSJohn Baldwin } 46983c41143SJohn Baldwin if (w->res == NULL) { 47083c41143SJohn Baldwin device_printf(sc->dev, 47183c41143SJohn Baldwin "failed to allocate initial %s window: %#jx-%#jx\n", 47283c41143SJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 47383c41143SJohn Baldwin w->base = max_address; 47483c41143SJohn Baldwin w->limit = 0; 47583c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 47683c41143SJohn Baldwin return; 47783c41143SJohn Baldwin } 47883c41143SJohn Baldwin pcib_activate_window(sc, type); 47983c41143SJohn Baldwin } 48083c41143SJohn Baldwin 48183c41143SJohn Baldwin /* 48283c41143SJohn Baldwin * Initialize I/O windows. 48383c41143SJohn Baldwin */ 48483c41143SJohn Baldwin static void 48583c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc) 48683c41143SJohn Baldwin { 48783c41143SJohn Baldwin pci_addr_t max; 48883c41143SJohn Baldwin device_t dev; 48983c41143SJohn Baldwin uint32_t val; 49083c41143SJohn Baldwin 49183c41143SJohn Baldwin dev = sc->dev; 49283c41143SJohn Baldwin 4930070c94bSJohn Baldwin if (pci_clear_pcib) { 494809923caSJustin Hibbits pcib_bridge_init(dev); 4950070c94bSJohn Baldwin } 4960070c94bSJohn Baldwin 49783c41143SJohn Baldwin /* Determine if the I/O port window is implemented. */ 49883c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 49983c41143SJohn Baldwin if (val == 0) { 50083c41143SJohn Baldwin /* 50183c41143SJohn Baldwin * If 'val' is zero, then only 16-bits of I/O space 50283c41143SJohn Baldwin * are supported. 50383c41143SJohn Baldwin */ 50483c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 50583c41143SJohn Baldwin if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) { 50683c41143SJohn Baldwin sc->io.valid = 1; 50783c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0, 1); 50883c41143SJohn Baldwin } 50983c41143SJohn Baldwin } else 51083c41143SJohn Baldwin sc->io.valid = 1; 51183c41143SJohn Baldwin 51283c41143SJohn Baldwin /* Read the existing I/O port window. */ 51383c41143SJohn Baldwin if (sc->io.valid) { 51483c41143SJohn Baldwin sc->io.reg = PCIR_IOBASEL_1; 51583c41143SJohn Baldwin sc->io.step = 12; 51683c41143SJohn Baldwin sc->io.mask = WIN_IO; 51783c41143SJohn Baldwin sc->io.name = "I/O port"; 51883c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 51983c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE( 52083c41143SJohn Baldwin pci_read_config(dev, PCIR_IOBASEH_1, 2), val); 52183c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT( 52283c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITH_1, 2), 52383c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 52483c41143SJohn Baldwin max = 0xffffffff; 52583c41143SJohn Baldwin } else { 52683c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE(0, val); 52783c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT(0, 52883c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 52983c41143SJohn Baldwin max = 0xffff; 53083c41143SJohn Baldwin } 53183c41143SJohn Baldwin pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max); 53283c41143SJohn Baldwin } 53383c41143SJohn Baldwin 53483c41143SJohn Baldwin /* Read the existing memory window. */ 53583c41143SJohn Baldwin sc->mem.valid = 1; 53683c41143SJohn Baldwin sc->mem.reg = PCIR_MEMBASE_1; 53783c41143SJohn Baldwin sc->mem.step = 20; 53883c41143SJohn Baldwin sc->mem.mask = WIN_MEM; 53983c41143SJohn Baldwin sc->mem.name = "memory"; 54083c41143SJohn Baldwin sc->mem.base = PCI_PPBMEMBASE(0, 54183c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMBASE_1, 2)); 54283c41143SJohn Baldwin sc->mem.limit = PCI_PPBMEMLIMIT(0, 54383c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 54483c41143SJohn Baldwin pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff); 54583c41143SJohn Baldwin 54683c41143SJohn Baldwin /* Determine if the prefetchable memory window is implemented. */ 54783c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 54883c41143SJohn Baldwin if (val == 0) { 54983c41143SJohn Baldwin /* 55083c41143SJohn Baldwin * If 'val' is zero, then only 32-bits of memory space 55183c41143SJohn Baldwin * are supported. 55283c41143SJohn Baldwin */ 55383c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 55483c41143SJohn Baldwin if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) { 55583c41143SJohn Baldwin sc->pmem.valid = 1; 55683c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0, 2); 55783c41143SJohn Baldwin } 55883c41143SJohn Baldwin } else 55983c41143SJohn Baldwin sc->pmem.valid = 1; 56083c41143SJohn Baldwin 56183c41143SJohn Baldwin /* Read the existing prefetchable memory window. */ 56283c41143SJohn Baldwin if (sc->pmem.valid) { 56383c41143SJohn Baldwin sc->pmem.reg = PCIR_PMBASEL_1; 56483c41143SJohn Baldwin sc->pmem.step = 20; 56583c41143SJohn Baldwin sc->pmem.mask = WIN_PMEM; 56683c41143SJohn Baldwin sc->pmem.name = "prefetch"; 56783c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 56883c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE( 56983c41143SJohn Baldwin pci_read_config(dev, PCIR_PMBASEH_1, 4), val); 57083c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT( 57183c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITH_1, 4), 57283c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 57383c41143SJohn Baldwin max = 0xffffffffffffffff; 57483c41143SJohn Baldwin } else { 57583c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE(0, val); 57683c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT(0, 57783c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 57883c41143SJohn Baldwin max = 0xffffffff; 57983c41143SJohn Baldwin } 58083c41143SJohn Baldwin pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY, 58183c41143SJohn Baldwin RF_PREFETCHABLE, max); 58283c41143SJohn Baldwin } 58383c41143SJohn Baldwin } 58483c41143SJohn Baldwin 5856f33eaa5SJohn Baldwin static void 5866f33eaa5SJohn Baldwin pcib_release_window(struct pcib_softc *sc, struct pcib_window *w, int type) 5876f33eaa5SJohn Baldwin { 5886f33eaa5SJohn Baldwin device_t dev; 5896f33eaa5SJohn Baldwin int error, i; 5906f33eaa5SJohn Baldwin 5916f33eaa5SJohn Baldwin if (!w->valid) 5926f33eaa5SJohn Baldwin return; 5936f33eaa5SJohn Baldwin 5946f33eaa5SJohn Baldwin dev = sc->dev; 5956f33eaa5SJohn Baldwin error = rman_fini(&w->rman); 5966f33eaa5SJohn Baldwin if (error) { 5976f33eaa5SJohn Baldwin device_printf(dev, "failed to release %s rman\n", w->name); 5986f33eaa5SJohn Baldwin return; 5996f33eaa5SJohn Baldwin } 6006f33eaa5SJohn Baldwin free(__DECONST(char *, w->rman.rm_descr), M_DEVBUF); 6016f33eaa5SJohn Baldwin 6026f33eaa5SJohn Baldwin for (i = 0; i < w->count; i++) { 6036f33eaa5SJohn Baldwin error = bus_free_resource(dev, type, w->res[i]); 6046f33eaa5SJohn Baldwin if (error) 6056f33eaa5SJohn Baldwin device_printf(dev, 6066f33eaa5SJohn Baldwin "failed to release %s resource: %d\n", w->name, 6076f33eaa5SJohn Baldwin error); 6086f33eaa5SJohn Baldwin } 6096f33eaa5SJohn Baldwin free(w->res, M_DEVBUF); 6106f33eaa5SJohn Baldwin } 6116f33eaa5SJohn Baldwin 6126f33eaa5SJohn Baldwin static void 6136f33eaa5SJohn Baldwin pcib_free_windows(struct pcib_softc *sc) 6146f33eaa5SJohn Baldwin { 6156f33eaa5SJohn Baldwin 6166f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->pmem, SYS_RES_MEMORY); 6176f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->mem, SYS_RES_MEMORY); 6186f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->io, SYS_RES_IOPORT); 6196f33eaa5SJohn Baldwin } 6206f33eaa5SJohn Baldwin 6214edef187SJohn Baldwin #ifdef PCI_RES_BUS 6224edef187SJohn Baldwin /* 6234edef187SJohn Baldwin * Allocate a suitable secondary bus for this bridge if needed and 6244edef187SJohn Baldwin * initialize the resource manager for the secondary bus range. Note 6254edef187SJohn Baldwin * that the minimum count is a desired value and this may allocate a 6264edef187SJohn Baldwin * smaller range. 6274edef187SJohn Baldwin */ 6284edef187SJohn Baldwin void 6294edef187SJohn Baldwin pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count) 6304edef187SJohn Baldwin { 6314edef187SJohn Baldwin char buf[64]; 632ad6f36f8SJohn Baldwin int error, rid, sec_reg; 6334edef187SJohn Baldwin 6344edef187SJohn Baldwin switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) { 6354edef187SJohn Baldwin case PCIM_HDRTYPE_BRIDGE: 636ad6f36f8SJohn Baldwin sec_reg = PCIR_SECBUS_1; 6374edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_1; 6384edef187SJohn Baldwin break; 6394edef187SJohn Baldwin case PCIM_HDRTYPE_CARDBUS: 640ad6f36f8SJohn Baldwin sec_reg = PCIR_SECBUS_2; 6414edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_2; 6424edef187SJohn Baldwin break; 6434edef187SJohn Baldwin default: 6444edef187SJohn Baldwin panic("not a PCI bridge"); 6454edef187SJohn Baldwin } 646ad6f36f8SJohn Baldwin bus->sec = pci_read_config(dev, sec_reg, 1); 647ad6f36f8SJohn Baldwin bus->sub = pci_read_config(dev, bus->sub_reg, 1); 6484edef187SJohn Baldwin bus->dev = dev; 6494edef187SJohn Baldwin bus->rman.rm_start = 0; 6504edef187SJohn Baldwin bus->rman.rm_end = PCI_BUSMAX; 6514edef187SJohn Baldwin bus->rman.rm_type = RMAN_ARRAY; 6524edef187SJohn Baldwin snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev)); 6534edef187SJohn Baldwin bus->rman.rm_descr = strdup(buf, M_DEVBUF); 6544edef187SJohn Baldwin error = rman_init(&bus->rman); 6554edef187SJohn Baldwin if (error) 6564edef187SJohn Baldwin panic("Failed to initialize %s bus number rman", 6574edef187SJohn Baldwin device_get_nameunit(dev)); 6584edef187SJohn Baldwin 6594edef187SJohn Baldwin /* 6604edef187SJohn Baldwin * Allocate a bus range. This will return an existing bus range 6614edef187SJohn Baldwin * if one exists, or a new bus range if one does not. 6624edef187SJohn Baldwin */ 6634edef187SJohn Baldwin rid = 0; 664c47476d7SJustin Hibbits bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid, 66536efc64aSJohn Baldwin min_count, RF_ACTIVE); 6664edef187SJohn Baldwin if (bus->res == NULL) { 6674edef187SJohn Baldwin /* 6684edef187SJohn Baldwin * Fall back to just allocating a range of a single bus 6694edef187SJohn Baldwin * number. 6704edef187SJohn Baldwin */ 671c47476d7SJustin Hibbits bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid, 67236efc64aSJohn Baldwin 1, RF_ACTIVE); 6734edef187SJohn Baldwin } else if (rman_get_size(bus->res) < min_count) 6744edef187SJohn Baldwin /* 6754edef187SJohn Baldwin * Attempt to grow the existing range to satisfy the 6764edef187SJohn Baldwin * minimum desired count. 6774edef187SJohn Baldwin */ 6784edef187SJohn Baldwin (void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res, 6794edef187SJohn Baldwin rman_get_start(bus->res), rman_get_start(bus->res) + 6804edef187SJohn Baldwin min_count - 1); 6814edef187SJohn Baldwin 6824edef187SJohn Baldwin /* 6834edef187SJohn Baldwin * Add the initial resource to the rman. 6844edef187SJohn Baldwin */ 6854edef187SJohn Baldwin if (bus->res != NULL) { 6864edef187SJohn Baldwin error = rman_manage_region(&bus->rman, rman_get_start(bus->res), 6874edef187SJohn Baldwin rman_get_end(bus->res)); 6884edef187SJohn Baldwin if (error) 6894edef187SJohn Baldwin panic("Failed to add resource to rman"); 6904edef187SJohn Baldwin bus->sec = rman_get_start(bus->res); 6914edef187SJohn Baldwin bus->sub = rman_get_end(bus->res); 6924edef187SJohn Baldwin } 6934edef187SJohn Baldwin } 6944edef187SJohn Baldwin 6956f33eaa5SJohn Baldwin void 6966f33eaa5SJohn Baldwin pcib_free_secbus(device_t dev, struct pcib_secbus *bus) 6976f33eaa5SJohn Baldwin { 6986f33eaa5SJohn Baldwin int error; 6996f33eaa5SJohn Baldwin 7006f33eaa5SJohn Baldwin error = rman_fini(&bus->rman); 7016f33eaa5SJohn Baldwin if (error) { 7026f33eaa5SJohn Baldwin device_printf(dev, "failed to release bus number rman\n"); 7036f33eaa5SJohn Baldwin return; 7046f33eaa5SJohn Baldwin } 7056f33eaa5SJohn Baldwin free(__DECONST(char *, bus->rman.rm_descr), M_DEVBUF); 7066f33eaa5SJohn Baldwin 7076f33eaa5SJohn Baldwin error = bus_free_resource(dev, PCI_RES_BUS, bus->res); 7086f33eaa5SJohn Baldwin if (error) 7096f33eaa5SJohn Baldwin device_printf(dev, 7106f33eaa5SJohn Baldwin "failed to release bus numbers resource: %d\n", error); 7116f33eaa5SJohn Baldwin } 7126f33eaa5SJohn Baldwin 7134edef187SJohn Baldwin static struct resource * 7144edef187SJohn Baldwin pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid, 7152dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 7164edef187SJohn Baldwin { 7174edef187SJohn Baldwin struct resource *res; 7184edef187SJohn Baldwin 7194edef187SJohn Baldwin res = rman_reserve_resource(&bus->rman, start, end, count, flags, 7204edef187SJohn Baldwin child); 7214edef187SJohn Baldwin if (res == NULL) 7224edef187SJohn Baldwin return (NULL); 7234edef187SJohn Baldwin 7244edef187SJohn Baldwin if (bootverbose) 7254edef187SJohn Baldwin device_printf(bus->dev, 726da1b038aSJustin Hibbits "allocated bus range (%ju-%ju) for rid %d of %s\n", 7274edef187SJohn Baldwin rman_get_start(res), rman_get_end(res), *rid, 7284edef187SJohn Baldwin pcib_child_name(child)); 7294edef187SJohn Baldwin rman_set_rid(res, *rid); 7301b9bcfffSJohn Baldwin rman_set_type(res, PCI_RES_BUS); 7314edef187SJohn Baldwin return (res); 7324edef187SJohn Baldwin } 7334edef187SJohn Baldwin 7344edef187SJohn Baldwin /* 7354edef187SJohn Baldwin * Attempt to grow the secondary bus range. This is much simpler than 7364edef187SJohn Baldwin * for I/O windows as the range can only be grown by increasing 7374edef187SJohn Baldwin * subbus. 7384edef187SJohn Baldwin */ 7394edef187SJohn Baldwin static int 7402dd1bdf1SJustin Hibbits pcib_grow_subbus(struct pcib_secbus *bus, rman_res_t new_end) 7414edef187SJohn Baldwin { 7422dd1bdf1SJustin Hibbits rman_res_t old_end; 7434edef187SJohn Baldwin int error; 7444edef187SJohn Baldwin 7454edef187SJohn Baldwin old_end = rman_get_end(bus->res); 7464edef187SJohn Baldwin KASSERT(new_end > old_end, ("attempt to shrink subbus")); 7474edef187SJohn Baldwin error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res, 7484edef187SJohn Baldwin rman_get_start(bus->res), new_end); 7494edef187SJohn Baldwin if (error) 7504edef187SJohn Baldwin return (error); 7514edef187SJohn Baldwin if (bootverbose) 752da1b038aSJustin Hibbits device_printf(bus->dev, "grew bus range to %ju-%ju\n", 7534edef187SJohn Baldwin rman_get_start(bus->res), rman_get_end(bus->res)); 7544edef187SJohn Baldwin error = rman_manage_region(&bus->rman, old_end + 1, 7554edef187SJohn Baldwin rman_get_end(bus->res)); 7564edef187SJohn Baldwin if (error) 7574edef187SJohn Baldwin panic("Failed to add resource to rman"); 7584edef187SJohn Baldwin bus->sub = rman_get_end(bus->res); 7594edef187SJohn Baldwin pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1); 7604edef187SJohn Baldwin return (0); 7614edef187SJohn Baldwin } 7624edef187SJohn Baldwin 7634edef187SJohn Baldwin struct resource * 7644edef187SJohn Baldwin pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid, 7652dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 7664edef187SJohn Baldwin { 7674edef187SJohn Baldwin struct resource *res; 7682dd1bdf1SJustin Hibbits rman_res_t start_free, end_free, new_end; 7694edef187SJohn Baldwin 7704edef187SJohn Baldwin /* 7714edef187SJohn Baldwin * First, see if the request can be satisified by the existing 7724edef187SJohn Baldwin * bus range. 7734edef187SJohn Baldwin */ 7744edef187SJohn Baldwin res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags); 7754edef187SJohn Baldwin if (res != NULL) 7764edef187SJohn Baldwin return (res); 7774edef187SJohn Baldwin 7784edef187SJohn Baldwin /* 7794edef187SJohn Baldwin * Figure out a range to grow the bus range. First, find the 7804edef187SJohn Baldwin * first bus number after the last allocated bus in the rman and 7814edef187SJohn Baldwin * enforce that as a minimum starting point for the range. 7824edef187SJohn Baldwin */ 7834edef187SJohn Baldwin if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 || 7844edef187SJohn Baldwin end_free != bus->sub) 7854edef187SJohn Baldwin start_free = bus->sub + 1; 7864edef187SJohn Baldwin if (start_free < start) 7874edef187SJohn Baldwin start_free = start; 7884edef187SJohn Baldwin new_end = start_free + count - 1; 7894edef187SJohn Baldwin 7904edef187SJohn Baldwin /* 7914edef187SJohn Baldwin * See if this new range would satisfy the request if it 7924edef187SJohn Baldwin * succeeds. 7934edef187SJohn Baldwin */ 7944edef187SJohn Baldwin if (new_end > end) 7954edef187SJohn Baldwin return (NULL); 7964edef187SJohn Baldwin 7974edef187SJohn Baldwin /* Finally, attempt to grow the existing resource. */ 7984edef187SJohn Baldwin if (bootverbose) { 7994edef187SJohn Baldwin device_printf(bus->dev, 800da1b038aSJustin Hibbits "attempting to grow bus range for %ju buses\n", count); 801da1b038aSJustin Hibbits printf("\tback candidate range: %ju-%ju\n", start_free, 8024edef187SJohn Baldwin new_end); 8034edef187SJohn Baldwin } 8044edef187SJohn Baldwin if (pcib_grow_subbus(bus, new_end) == 0) 8054edef187SJohn Baldwin return (pcib_suballoc_bus(bus, child, rid, start, end, count, 8064edef187SJohn Baldwin flags)); 8074edef187SJohn Baldwin return (NULL); 8084edef187SJohn Baldwin } 8094edef187SJohn Baldwin #endif 8104edef187SJohn Baldwin 81183c41143SJohn Baldwin #else 81283c41143SJohn Baldwin 813bb0d0a8eSMike Smith /* 814b0a2d4b8SWarner Losh * Is the prefetch window open (eg, can we allocate memory in it?) 815b0a2d4b8SWarner Losh */ 816b0a2d4b8SWarner Losh static int 817b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc) 818b0a2d4b8SWarner Losh { 819b0a2d4b8SWarner Losh return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit); 820b0a2d4b8SWarner Losh } 821b0a2d4b8SWarner Losh 822b0a2d4b8SWarner Losh /* 823b0a2d4b8SWarner Losh * Is the nonprefetch window open (eg, can we allocate memory in it?) 824b0a2d4b8SWarner Losh */ 825b0a2d4b8SWarner Losh static int 826b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc) 827b0a2d4b8SWarner Losh { 828b0a2d4b8SWarner Losh return (sc->membase > 0 && sc->membase < sc->memlimit); 829b0a2d4b8SWarner Losh } 830b0a2d4b8SWarner Losh 831b0a2d4b8SWarner Losh /* 832b0a2d4b8SWarner Losh * Is the io window open (eg, can we allocate ports in it?) 833b0a2d4b8SWarner Losh */ 834b0a2d4b8SWarner Losh static int 835b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc) 836b0a2d4b8SWarner Losh { 837b0a2d4b8SWarner Losh return (sc->iobase > 0 && sc->iobase < sc->iolimit); 838b0a2d4b8SWarner Losh } 839b0a2d4b8SWarner Losh 840b0a2d4b8SWarner Losh /* 841e36af292SJung-uk Kim * Get current I/O decode. 842e36af292SJung-uk Kim */ 843e36af292SJung-uk Kim static void 844e36af292SJung-uk Kim pcib_get_io_decode(struct pcib_softc *sc) 845e36af292SJung-uk Kim { 846e36af292SJung-uk Kim device_t dev; 847e36af292SJung-uk Kim uint32_t iolow; 848e36af292SJung-uk Kim 849e36af292SJung-uk Kim dev = sc->dev; 850e36af292SJung-uk Kim 851e36af292SJung-uk Kim iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1); 852e36af292SJung-uk Kim if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 853e36af292SJung-uk Kim sc->iobase = PCI_PPBIOBASE( 854e36af292SJung-uk Kim pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow); 855e36af292SJung-uk Kim else 856e36af292SJung-uk Kim sc->iobase = PCI_PPBIOBASE(0, iolow); 857e36af292SJung-uk Kim 858e36af292SJung-uk Kim iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1); 859e36af292SJung-uk Kim if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 860e36af292SJung-uk Kim sc->iolimit = PCI_PPBIOLIMIT( 861e36af292SJung-uk Kim pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow); 862e36af292SJung-uk Kim else 863e36af292SJung-uk Kim sc->iolimit = PCI_PPBIOLIMIT(0, iolow); 864e36af292SJung-uk Kim } 865e36af292SJung-uk Kim 866e36af292SJung-uk Kim /* 867e36af292SJung-uk Kim * Get current memory decode. 868e36af292SJung-uk Kim */ 869e36af292SJung-uk Kim static void 870e36af292SJung-uk Kim pcib_get_mem_decode(struct pcib_softc *sc) 871e36af292SJung-uk Kim { 872e36af292SJung-uk Kim device_t dev; 873e36af292SJung-uk Kim pci_addr_t pmemlow; 874e36af292SJung-uk Kim 875e36af292SJung-uk Kim dev = sc->dev; 876e36af292SJung-uk Kim 877e36af292SJung-uk Kim sc->membase = PCI_PPBMEMBASE(0, 878e36af292SJung-uk Kim pci_read_config(dev, PCIR_MEMBASE_1, 2)); 879e36af292SJung-uk Kim sc->memlimit = PCI_PPBMEMLIMIT(0, 880e36af292SJung-uk Kim pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 881e36af292SJung-uk Kim 882e36af292SJung-uk Kim pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2); 883e36af292SJung-uk Kim if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 884e36af292SJung-uk Kim sc->pmembase = PCI_PPBMEMBASE( 885e36af292SJung-uk Kim pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow); 886e36af292SJung-uk Kim else 887e36af292SJung-uk Kim sc->pmembase = PCI_PPBMEMBASE(0, pmemlow); 888e36af292SJung-uk Kim 889e36af292SJung-uk Kim pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2); 890e36af292SJung-uk Kim if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 891e36af292SJung-uk Kim sc->pmemlimit = PCI_PPBMEMLIMIT( 892e36af292SJung-uk Kim pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow); 893e36af292SJung-uk Kim else 894e36af292SJung-uk Kim sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow); 895e36af292SJung-uk Kim } 896e36af292SJung-uk Kim 897e36af292SJung-uk Kim /* 898e36af292SJung-uk Kim * Restore previous I/O decode. 899e36af292SJung-uk Kim */ 900e36af292SJung-uk Kim static void 901e36af292SJung-uk Kim pcib_set_io_decode(struct pcib_softc *sc) 902e36af292SJung-uk Kim { 903e36af292SJung-uk Kim device_t dev; 904e36af292SJung-uk Kim uint32_t iohi; 905e36af292SJung-uk Kim 906e36af292SJung-uk Kim dev = sc->dev; 907e36af292SJung-uk Kim 908e36af292SJung-uk Kim iohi = sc->iobase >> 16; 909e36af292SJung-uk Kim if (iohi > 0) 910e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2); 911e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1); 912e36af292SJung-uk Kim 913e36af292SJung-uk Kim iohi = sc->iolimit >> 16; 914e36af292SJung-uk Kim if (iohi > 0) 915e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2); 916e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1); 917e36af292SJung-uk Kim } 918e36af292SJung-uk Kim 919e36af292SJung-uk Kim /* 920e36af292SJung-uk Kim * Restore previous memory decode. 921e36af292SJung-uk Kim */ 922e36af292SJung-uk Kim static void 923e36af292SJung-uk Kim pcib_set_mem_decode(struct pcib_softc *sc) 924e36af292SJung-uk Kim { 925e36af292SJung-uk Kim device_t dev; 926e36af292SJung-uk Kim pci_addr_t pmemhi; 927e36af292SJung-uk Kim 928e36af292SJung-uk Kim dev = sc->dev; 929e36af292SJung-uk Kim 930e36af292SJung-uk Kim pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2); 931e36af292SJung-uk Kim pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2); 932e36af292SJung-uk Kim 933e36af292SJung-uk Kim pmemhi = sc->pmembase >> 32; 934e36af292SJung-uk Kim if (pmemhi > 0) 935e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4); 936e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2); 937e36af292SJung-uk Kim 938e36af292SJung-uk Kim pmemhi = sc->pmemlimit >> 32; 939e36af292SJung-uk Kim if (pmemhi > 0) 940e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4); 941e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2); 942e36af292SJung-uk Kim } 94383c41143SJohn Baldwin #endif 944e36af292SJung-uk Kim 94582cb5c3bSJohn Baldwin #ifdef PCI_HP 94682cb5c3bSJohn Baldwin /* 94782cb5c3bSJohn Baldwin * PCI-express HotPlug support. 94882cb5c3bSJohn Baldwin */ 94925a57bd6SJohn Baldwin static int pci_enable_pcie_hp = 1; 95025a57bd6SJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_hp, CTLFLAG_RDTUN, 95125a57bd6SJohn Baldwin &pci_enable_pcie_hp, 0, 95225a57bd6SJohn Baldwin "Enable support for native PCI-express HotPlug."); 95325a57bd6SJohn Baldwin 95412588ce0SAndriy Gapon TASKQUEUE_DEFINE_THREAD(pci_hp); 95512588ce0SAndriy Gapon 95682cb5c3bSJohn Baldwin static void 95782cb5c3bSJohn Baldwin pcib_probe_hotplug(struct pcib_softc *sc) 95882cb5c3bSJohn Baldwin { 95982cb5c3bSJohn Baldwin device_t dev; 96037290148SEric van Gyzen uint32_t link_cap; 961991d431fSEric van Gyzen uint16_t link_sta, slot_sta; 96282cb5c3bSJohn Baldwin 96325a57bd6SJohn Baldwin if (!pci_enable_pcie_hp) 96425a57bd6SJohn Baldwin return; 96525a57bd6SJohn Baldwin 96682cb5c3bSJohn Baldwin dev = sc->dev; 96782cb5c3bSJohn Baldwin if (pci_find_cap(dev, PCIY_EXPRESS, NULL) != 0) 96882cb5c3bSJohn Baldwin return; 96982cb5c3bSJohn Baldwin 97082cb5c3bSJohn Baldwin if (!(pcie_read_config(dev, PCIER_FLAGS, 2) & PCIEM_FLAGS_SLOT)) 97182cb5c3bSJohn Baldwin return; 97282cb5c3bSJohn Baldwin 97382cb5c3bSJohn Baldwin sc->pcie_slot_cap = pcie_read_config(dev, PCIER_SLOT_CAP, 4); 97482cb5c3bSJohn Baldwin 975991d431fSEric van Gyzen if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_HPC) == 0) 9762611037cSJohn Baldwin return; 97737290148SEric van Gyzen link_cap = pcie_read_config(dev, PCIER_LINK_CAP, 4); 97837290148SEric van Gyzen if ((link_cap & PCIEM_LINK_CAP_DL_ACTIVE) == 0) 9792ffb582aSJohn Baldwin return; 9802611037cSJohn Baldwin 981991d431fSEric van Gyzen /* 982991d431fSEric van Gyzen * Some devices report that they have an MRL when they actually 983991d431fSEric van Gyzen * do not. Since they always report that the MRL is open, child 984991d431fSEric van Gyzen * devices would be ignored. Try to detect these devices and 985991d431fSEric van Gyzen * ignore their claim of HotPlug support. 986991d431fSEric van Gyzen * 987991d431fSEric van Gyzen * If there is an open MRL but the Data Link Layer is active, 988991d431fSEric van Gyzen * the MRL is not real. 989991d431fSEric van Gyzen */ 99037290148SEric van Gyzen if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) != 0) { 991991d431fSEric van Gyzen link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 992991d431fSEric van Gyzen slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 993991d431fSEric van Gyzen if ((slot_sta & PCIEM_SLOT_STA_MRLSS) != 0 && 994991d431fSEric van Gyzen (link_sta & PCIEM_LINK_STA_DL_ACTIVE) != 0) { 995991d431fSEric van Gyzen return; 996991d431fSEric van Gyzen } 997991d431fSEric van Gyzen } 998991d431fSEric van Gyzen 99928586889SWarner Losh /* 100028586889SWarner Losh * Now that we're sure we want to do hot plug, ask the 100128586889SWarner Losh * firmware, if any, if that's OK. 100228586889SWarner Losh */ 10031ffd07bdSJohn Baldwin if (pcib_request_feature(dev, PCI_FEATURE_HP) != 0) { 100428586889SWarner Losh if (bootverbose) 100528586889SWarner Losh device_printf(dev, "Unable to activate hot plug feature.\n"); 100628586889SWarner Losh return; 100728586889SWarner Losh } 100828586889SWarner Losh 100982cb5c3bSJohn Baldwin sc->flags |= PCIB_HOTPLUG; 101082cb5c3bSJohn Baldwin } 101182cb5c3bSJohn Baldwin 101282cb5c3bSJohn Baldwin /* 101382cb5c3bSJohn Baldwin * Send a HotPlug command to the slot control register. If this slot 101407454911SJohn Baldwin * uses command completion interrupts and a previous command is still 101507454911SJohn Baldwin * in progress, then the command is dropped. Once the previous 101607454911SJohn Baldwin * command completes or times out, pcib_pcie_hotplug_update() will be 101707454911SJohn Baldwin * invoked to post a new command based on the slot's state at that 101807454911SJohn Baldwin * time. 101982cb5c3bSJohn Baldwin */ 102082cb5c3bSJohn Baldwin static void 102182cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(struct pcib_softc *sc, uint16_t val, uint16_t mask) 102282cb5c3bSJohn Baldwin { 102382cb5c3bSJohn Baldwin device_t dev; 102482cb5c3bSJohn Baldwin uint16_t ctl, new; 102582cb5c3bSJohn Baldwin 102682cb5c3bSJohn Baldwin dev = sc->dev; 102782cb5c3bSJohn Baldwin 102807454911SJohn Baldwin if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) 102907454911SJohn Baldwin return; 103007454911SJohn Baldwin 103182cb5c3bSJohn Baldwin ctl = pcie_read_config(dev, PCIER_SLOT_CTL, 2); 103282cb5c3bSJohn Baldwin new = (ctl & ~mask) | val; 103307454911SJohn Baldwin if (new == ctl) 103407454911SJohn Baldwin return; 1035991d431fSEric van Gyzen if (bootverbose) 1036991d431fSEric van Gyzen device_printf(dev, "HotPlug command: %04x -> %04x\n", ctl, new); 103707454911SJohn Baldwin pcie_write_config(dev, PCIER_SLOT_CTL, new, 2); 10386f33eaa5SJohn Baldwin if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS) && 10396f33eaa5SJohn Baldwin (ctl & new) & PCIEM_SLOT_CTL_CCIE) { 104082cb5c3bSJohn Baldwin sc->flags |= PCIB_HOTPLUG_CMD_PENDING; 104182cb5c3bSJohn Baldwin if (!cold) 1042fa3b03d3SAlexander Motin taskqueue_enqueue_timeout(taskqueue_pci_hp, 1043fa3b03d3SAlexander Motin &sc->pcie_cc_task, hz); 104482cb5c3bSJohn Baldwin } 104582cb5c3bSJohn Baldwin } 104682cb5c3bSJohn Baldwin 104782cb5c3bSJohn Baldwin static void 104882cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(struct pcib_softc *sc) 104982cb5c3bSJohn Baldwin { 105082cb5c3bSJohn Baldwin device_t dev; 105182cb5c3bSJohn Baldwin 105282cb5c3bSJohn Baldwin dev = sc->dev; 105382cb5c3bSJohn Baldwin 105482cb5c3bSJohn Baldwin if (bootverbose) 105582cb5c3bSJohn Baldwin device_printf(dev, "Command Completed\n"); 105682cb5c3bSJohn Baldwin if (!(sc->flags & PCIB_HOTPLUG_CMD_PENDING)) 105782cb5c3bSJohn Baldwin return; 1058fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_cc_task, NULL); 105982cb5c3bSJohn Baldwin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 10606f33eaa5SJohn Baldwin wakeup(sc); 106182cb5c3bSJohn Baldwin } 106282cb5c3bSJohn Baldwin 106382cb5c3bSJohn Baldwin /* 106482cb5c3bSJohn Baldwin * Returns true if a card is fully inserted from the user's 106582cb5c3bSJohn Baldwin * perspective. It may not yet be ready for access, but the driver 106682cb5c3bSJohn Baldwin * can now start enabling access if necessary. 106782cb5c3bSJohn Baldwin */ 106882cb5c3bSJohn Baldwin static bool 106982cb5c3bSJohn Baldwin pcib_hotplug_inserted(struct pcib_softc *sc) 107082cb5c3bSJohn Baldwin { 107182cb5c3bSJohn Baldwin 107282cb5c3bSJohn Baldwin /* Pretend the card isn't present if a detach is forced. */ 107382cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACHING) 107482cb5c3bSJohn Baldwin return (false); 107582cb5c3bSJohn Baldwin 107682cb5c3bSJohn Baldwin /* Card must be present in the slot. */ 107782cb5c3bSJohn Baldwin if ((sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS) == 0) 107882cb5c3bSJohn Baldwin return (false); 107982cb5c3bSJohn Baldwin 108082cb5c3bSJohn Baldwin /* A power fault implicitly turns off power to the slot. */ 108182cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD) 108282cb5c3bSJohn Baldwin return (false); 108382cb5c3bSJohn Baldwin 108482cb5c3bSJohn Baldwin /* If the MRL is disengaged, the slot is powered off. */ 108582cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP && 108682cb5c3bSJohn Baldwin (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS) != 0) 108782cb5c3bSJohn Baldwin return (false); 108882cb5c3bSJohn Baldwin 108982cb5c3bSJohn Baldwin return (true); 109082cb5c3bSJohn Baldwin } 109182cb5c3bSJohn Baldwin 109282cb5c3bSJohn Baldwin /* 109382cb5c3bSJohn Baldwin * Returns -1 if the card is fully inserted, powered, and ready for 109482cb5c3bSJohn Baldwin * access. Otherwise, returns 0. 109582cb5c3bSJohn Baldwin */ 109682cb5c3bSJohn Baldwin static int 109782cb5c3bSJohn Baldwin pcib_hotplug_present(struct pcib_softc *sc) 109882cb5c3bSJohn Baldwin { 109982cb5c3bSJohn Baldwin 110082cb5c3bSJohn Baldwin /* Card must be inserted. */ 110182cb5c3bSJohn Baldwin if (!pcib_hotplug_inserted(sc)) 110282cb5c3bSJohn Baldwin return (0); 110382cb5c3bSJohn Baldwin 110482cb5c3bSJohn Baldwin /* Require the Data Link Layer to be active. */ 110582cb5c3bSJohn Baldwin if (!(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE)) 110682cb5c3bSJohn Baldwin return (0); 110782cb5c3bSJohn Baldwin 110882cb5c3bSJohn Baldwin return (-1); 110982cb5c3bSJohn Baldwin } 111082cb5c3bSJohn Baldwin 1111a58536b9SAlexander Motin static int pci_enable_pcie_ei = 0; 1112a58536b9SAlexander Motin SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_ei, CTLFLAG_RWTUN, 1113a58536b9SAlexander Motin &pci_enable_pcie_ei, 0, 1114a58536b9SAlexander Motin "Enable support for PCI-express Electromechanical Interlock."); 1115a58536b9SAlexander Motin 111682cb5c3bSJohn Baldwin static void 111782cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(struct pcib_softc *sc, uint16_t val, uint16_t mask, 111882cb5c3bSJohn Baldwin bool schedule_task) 111982cb5c3bSJohn Baldwin { 1120a1566487SEric van Gyzen bool card_inserted, ei_engaged; 112182cb5c3bSJohn Baldwin 1122991d431fSEric van Gyzen /* Clear DETACHING if Presence Detect has cleared. */ 112382cb5c3bSJohn Baldwin if ((sc->pcie_slot_sta & (PCIEM_SLOT_STA_PDC | PCIEM_SLOT_STA_PDS)) == 112482cb5c3bSJohn Baldwin PCIEM_SLOT_STA_PDC) 112582cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACHING; 112682cb5c3bSJohn Baldwin 112782cb5c3bSJohn Baldwin card_inserted = pcib_hotplug_inserted(sc); 112882cb5c3bSJohn Baldwin 112982cb5c3bSJohn Baldwin /* Turn the power indicator on if a card is inserted. */ 113082cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PIP) { 113182cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_PIC; 113282cb5c3bSJohn Baldwin if (card_inserted) 113382cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_ON; 113482cb5c3bSJohn Baldwin else if (sc->flags & PCIB_DETACH_PENDING) 113582cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_BLINK; 113682cb5c3bSJohn Baldwin else 113782cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_OFF; 113882cb5c3bSJohn Baldwin } 113982cb5c3bSJohn Baldwin 114082cb5c3bSJohn Baldwin /* Turn the power on via the Power Controller if a card is inserted. */ 114182cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) { 114282cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_PCC; 114382cb5c3bSJohn Baldwin if (card_inserted) 114482cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PC_ON; 114582cb5c3bSJohn Baldwin else 114682cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PC_OFF; 114782cb5c3bSJohn Baldwin } 114882cb5c3bSJohn Baldwin 114982cb5c3bSJohn Baldwin /* 115082cb5c3bSJohn Baldwin * If a card is inserted, enable the Electromechanical 115182cb5c3bSJohn Baldwin * Interlock. If a card is not inserted (or we are in the 115282cb5c3bSJohn Baldwin * process of detaching), disable the Electromechanical 115382cb5c3bSJohn Baldwin * Interlock. 115482cb5c3bSJohn Baldwin */ 1155a58536b9SAlexander Motin if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP) && 1156a58536b9SAlexander Motin pci_enable_pcie_ei) { 115782cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_EIC; 1158a1566487SEric van Gyzen ei_engaged = (sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS) != 0; 1159a1566487SEric van Gyzen if (card_inserted != ei_engaged) 116082cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_EIC; 116182cb5c3bSJohn Baldwin } 116282cb5c3bSJohn Baldwin 116382cb5c3bSJohn Baldwin /* 116482cb5c3bSJohn Baldwin * Start a timer to see if the Data Link Layer times out. 1165991d431fSEric van Gyzen * Note that we only start the timer if Presence Detect or MRL Sensor 116682cb5c3bSJohn Baldwin * changed on this interrupt. Stop any scheduled timer if 116782cb5c3bSJohn Baldwin * the Data Link Layer is active. 116882cb5c3bSJohn Baldwin */ 116982cb5c3bSJohn Baldwin if (card_inserted && 117082cb5c3bSJohn Baldwin !(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) && 1171991d431fSEric van Gyzen sc->pcie_slot_sta & 1172991d431fSEric van Gyzen (PCIEM_SLOT_STA_MRLSC | PCIEM_SLOT_STA_PDC)) { 117382cb5c3bSJohn Baldwin if (cold) 117482cb5c3bSJohn Baldwin device_printf(sc->dev, 117582cb5c3bSJohn Baldwin "Data Link Layer inactive\n"); 117682cb5c3bSJohn Baldwin else 1177fa3b03d3SAlexander Motin taskqueue_enqueue_timeout(taskqueue_pci_hp, 1178fa3b03d3SAlexander Motin &sc->pcie_dll_task, hz); 117982cb5c3bSJohn Baldwin } else if (sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) 1180fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_dll_task, 1181fa3b03d3SAlexander Motin NULL); 118282cb5c3bSJohn Baldwin 118382cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(sc, val, mask); 118482cb5c3bSJohn Baldwin 118582cb5c3bSJohn Baldwin /* 1186a1566487SEric van Gyzen * During attach the child "pci" device is added synchronously; 118782cb5c3bSJohn Baldwin * otherwise, the task is scheduled to manage the child 118882cb5c3bSJohn Baldwin * device. 118982cb5c3bSJohn Baldwin */ 119082cb5c3bSJohn Baldwin if (schedule_task && 119182cb5c3bSJohn Baldwin (pcib_hotplug_present(sc) != 0) != (sc->child != NULL)) 119212588ce0SAndriy Gapon taskqueue_enqueue(taskqueue_pci_hp, &sc->pcie_hp_task); 119382cb5c3bSJohn Baldwin } 119482cb5c3bSJohn Baldwin 119582cb5c3bSJohn Baldwin static void 11968a1926c5SWarner Losh pcib_pcie_intr_hotplug(void *arg) 119782cb5c3bSJohn Baldwin { 119882cb5c3bSJohn Baldwin struct pcib_softc *sc; 119982cb5c3bSJohn Baldwin device_t dev; 1200e0235fd3SColin Percival uint16_t old_slot_sta; 120182cb5c3bSJohn Baldwin 120282cb5c3bSJohn Baldwin sc = arg; 120382cb5c3bSJohn Baldwin dev = sc->dev; 120413d700adSScott Long PCIB_HP_LOCK(sc); 1205e0235fd3SColin Percival old_slot_sta = sc->pcie_slot_sta; 120682cb5c3bSJohn Baldwin sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 120782cb5c3bSJohn Baldwin 120882cb5c3bSJohn Baldwin /* Clear the events just reported. */ 120982cb5c3bSJohn Baldwin pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2); 121082cb5c3bSJohn Baldwin 1211991d431fSEric van Gyzen if (bootverbose) 1212991d431fSEric van Gyzen device_printf(dev, "HotPlug interrupt: %#x\n", 1213991d431fSEric van Gyzen sc->pcie_slot_sta); 1214991d431fSEric van Gyzen 121582cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_ABP) { 121682cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 121782cb5c3bSJohn Baldwin device_printf(dev, 121882cb5c3bSJohn Baldwin "Attention Button Pressed: Detach Cancelled\n"); 121982cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 1220fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, 1221fa3b03d3SAlexander Motin &sc->pcie_ab_task, NULL); 1222e0235fd3SColin Percival } else if (old_slot_sta & PCIEM_SLOT_STA_PDS) { 1223e0235fd3SColin Percival /* Only initiate detach sequence if device present. */ 122482cb5c3bSJohn Baldwin device_printf(dev, 122582cb5c3bSJohn Baldwin "Attention Button Pressed: Detaching in 5 seconds\n"); 122682cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACH_PENDING; 1227fa3b03d3SAlexander Motin taskqueue_enqueue_timeout(taskqueue_pci_hp, 1228fa3b03d3SAlexander Motin &sc->pcie_ab_task, 5 * hz); 122982cb5c3bSJohn Baldwin } 123082cb5c3bSJohn Baldwin } 123182cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD) 123282cb5c3bSJohn Baldwin device_printf(dev, "Power Fault Detected\n"); 123382cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSC) 123482cb5c3bSJohn Baldwin device_printf(dev, "MRL Sensor Changed to %s\n", 123582cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS ? "open" : 123682cb5c3bSJohn Baldwin "closed"); 123782cb5c3bSJohn Baldwin if (bootverbose && sc->pcie_slot_sta & PCIEM_SLOT_STA_PDC) 1238991d431fSEric van Gyzen device_printf(dev, "Presence Detect Changed to %s\n", 123982cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS ? "card present" : 124082cb5c3bSJohn Baldwin "empty"); 124182cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_CC) 124282cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(sc); 124382cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_DLLSC) { 124482cb5c3bSJohn Baldwin sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 124582cb5c3bSJohn Baldwin if (bootverbose) 124682cb5c3bSJohn Baldwin device_printf(dev, 124782cb5c3bSJohn Baldwin "Data Link Layer State Changed to %s\n", 124882cb5c3bSJohn Baldwin sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE ? 124982cb5c3bSJohn Baldwin "active" : "inactive"); 125082cb5c3bSJohn Baldwin } 125182cb5c3bSJohn Baldwin 125282cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 125313d700adSScott Long PCIB_HP_UNLOCK(sc); 125482cb5c3bSJohn Baldwin } 125582cb5c3bSJohn Baldwin 125682cb5c3bSJohn Baldwin static void 125782cb5c3bSJohn Baldwin pcib_pcie_hotplug_task(void *context, int pending) 125882cb5c3bSJohn Baldwin { 125982cb5c3bSJohn Baldwin struct pcib_softc *sc; 126082cb5c3bSJohn Baldwin device_t dev; 126182cb5c3bSJohn Baldwin 126282cb5c3bSJohn Baldwin sc = context; 126313d700adSScott Long PCIB_HP_LOCK(sc); 126482cb5c3bSJohn Baldwin dev = sc->dev; 126582cb5c3bSJohn Baldwin if (pcib_hotplug_present(sc) != 0) { 126682cb5c3bSJohn Baldwin if (sc->child == NULL) { 126782cb5c3bSJohn Baldwin sc->child = device_add_child(dev, "pci", -1); 126882cb5c3bSJohn Baldwin bus_generic_attach(dev); 126982cb5c3bSJohn Baldwin } 127082cb5c3bSJohn Baldwin } else { 127182cb5c3bSJohn Baldwin if (sc->child != NULL) { 127282cb5c3bSJohn Baldwin if (device_delete_child(dev, sc->child) == 0) 127382cb5c3bSJohn Baldwin sc->child = NULL; 127482cb5c3bSJohn Baldwin } 127582cb5c3bSJohn Baldwin } 127613d700adSScott Long PCIB_HP_UNLOCK(sc); 127782cb5c3bSJohn Baldwin } 127882cb5c3bSJohn Baldwin 127982cb5c3bSJohn Baldwin static void 1280fa3b03d3SAlexander Motin pcib_pcie_ab_timeout(void *arg, int pending) 128182cb5c3bSJohn Baldwin { 1282fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 128382cb5c3bSJohn Baldwin 1284fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 128582cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 128682cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING; 128782cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 128882cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 128982cb5c3bSJohn Baldwin } 1290fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 129182cb5c3bSJohn Baldwin } 129282cb5c3bSJohn Baldwin 129382cb5c3bSJohn Baldwin static void 1294fa3b03d3SAlexander Motin pcib_pcie_cc_timeout(void *arg, int pending) 129582cb5c3bSJohn Baldwin { 1296fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 1297fa3b03d3SAlexander Motin device_t dev = sc->dev; 12986f33eaa5SJohn Baldwin uint16_t sta; 129982cb5c3bSJohn Baldwin 1300fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 13016f33eaa5SJohn Baldwin sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 13026f33eaa5SJohn Baldwin if (!(sta & PCIEM_SLOT_STA_CC)) { 130321e51c82SAlexander Motin device_printf(dev, "HotPlug Command Timed Out\n"); 130421e51c82SAlexander Motin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 13056f33eaa5SJohn Baldwin } else { 13066f33eaa5SJohn Baldwin device_printf(dev, 13076f33eaa5SJohn Baldwin "Missed HotPlug interrupt waiting for Command Completion\n"); 13088a1926c5SWarner Losh pcib_pcie_intr_hotplug(sc); 130982cb5c3bSJohn Baldwin } 1310fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 131182cb5c3bSJohn Baldwin } 131282cb5c3bSJohn Baldwin 131382cb5c3bSJohn Baldwin static void 1314fa3b03d3SAlexander Motin pcib_pcie_dll_timeout(void *arg, int pending) 131582cb5c3bSJohn Baldwin { 1316fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 1317fa3b03d3SAlexander Motin device_t dev = sc->dev; 131882cb5c3bSJohn Baldwin uint16_t sta; 131982cb5c3bSJohn Baldwin 1320fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 132182cb5c3bSJohn Baldwin sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 132282cb5c3bSJohn Baldwin if (!(sta & PCIEM_LINK_STA_DL_ACTIVE)) { 132382cb5c3bSJohn Baldwin device_printf(dev, 132482cb5c3bSJohn Baldwin "Timed out waiting for Data Link Layer Active\n"); 132582cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING; 132682cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 132782cb5c3bSJohn Baldwin } else if (sta != sc->pcie_link_sta) { 132882cb5c3bSJohn Baldwin device_printf(dev, 132982cb5c3bSJohn Baldwin "Missed HotPlug interrupt waiting for DLL Active\n"); 13308a1926c5SWarner Losh pcib_pcie_intr_hotplug(sc); 133182cb5c3bSJohn Baldwin } 1332fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 133382cb5c3bSJohn Baldwin } 133482cb5c3bSJohn Baldwin 133582cb5c3bSJohn Baldwin static int 133682cb5c3bSJohn Baldwin pcib_alloc_pcie_irq(struct pcib_softc *sc) 133782cb5c3bSJohn Baldwin { 133882cb5c3bSJohn Baldwin device_t dev; 1339e6b83836SJohn Baldwin int count, error, mem_rid, rid; 134082cb5c3bSJohn Baldwin 134182cb5c3bSJohn Baldwin rid = -1; 134282cb5c3bSJohn Baldwin dev = sc->dev; 134382cb5c3bSJohn Baldwin 134482cb5c3bSJohn Baldwin /* 134582cb5c3bSJohn Baldwin * For simplicity, only use MSI-X if there is a single message. 134682cb5c3bSJohn Baldwin * To support a device with multiple messages we would have to 134782cb5c3bSJohn Baldwin * use remap intr if the MSI number is not 0. 134882cb5c3bSJohn Baldwin */ 134982cb5c3bSJohn Baldwin count = pci_msix_count(dev); 135082cb5c3bSJohn Baldwin if (count == 1) { 1351e6b83836SJohn Baldwin mem_rid = pci_msix_table_bar(dev); 1352e6b83836SJohn Baldwin sc->pcie_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 1353e6b83836SJohn Baldwin &mem_rid, RF_ACTIVE); 1354e6b83836SJohn Baldwin if (sc->pcie_mem == NULL) { 1355e6b83836SJohn Baldwin device_printf(dev, 1356e6b83836SJohn Baldwin "Failed to allocate BAR for MSI-X table\n"); 1357e6b83836SJohn Baldwin } else { 135882cb5c3bSJohn Baldwin error = pci_alloc_msix(dev, &count); 135982cb5c3bSJohn Baldwin if (error == 0) 136082cb5c3bSJohn Baldwin rid = 1; 136182cb5c3bSJohn Baldwin } 1362e6b83836SJohn Baldwin } 136382cb5c3bSJohn Baldwin 136482cb5c3bSJohn Baldwin if (rid < 0 && pci_msi_count(dev) > 0) { 136582cb5c3bSJohn Baldwin count = 1; 136682cb5c3bSJohn Baldwin error = pci_alloc_msi(dev, &count); 136782cb5c3bSJohn Baldwin if (error == 0) 136882cb5c3bSJohn Baldwin rid = 1; 136982cb5c3bSJohn Baldwin } 137082cb5c3bSJohn Baldwin 137182cb5c3bSJohn Baldwin if (rid < 0) 137282cb5c3bSJohn Baldwin rid = 0; 137382cb5c3bSJohn Baldwin 137482cb5c3bSJohn Baldwin sc->pcie_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1375f14f0051SChuck Tuffli RF_ACTIVE | RF_SHAREABLE); 137682cb5c3bSJohn Baldwin if (sc->pcie_irq == NULL) { 137782cb5c3bSJohn Baldwin device_printf(dev, 137882cb5c3bSJohn Baldwin "Failed to allocate interrupt for PCI-e events\n"); 137982cb5c3bSJohn Baldwin if (rid > 0) 138082cb5c3bSJohn Baldwin pci_release_msi(dev); 138182cb5c3bSJohn Baldwin return (ENXIO); 138282cb5c3bSJohn Baldwin } 138382cb5c3bSJohn Baldwin 138413d700adSScott Long error = bus_setup_intr(dev, sc->pcie_irq, INTR_TYPE_MISC|INTR_MPSAFE, 13858a1926c5SWarner Losh NULL, pcib_pcie_intr_hotplug, sc, &sc->pcie_ihand); 138682cb5c3bSJohn Baldwin if (error) { 138782cb5c3bSJohn Baldwin device_printf(dev, "Failed to setup PCI-e interrupt handler\n"); 138882cb5c3bSJohn Baldwin bus_release_resource(dev, SYS_RES_IRQ, rid, sc->pcie_irq); 138982cb5c3bSJohn Baldwin if (rid > 0) 139082cb5c3bSJohn Baldwin pci_release_msi(dev); 139182cb5c3bSJohn Baldwin return (error); 139282cb5c3bSJohn Baldwin } 139382cb5c3bSJohn Baldwin return (0); 139482cb5c3bSJohn Baldwin } 139582cb5c3bSJohn Baldwin 13966f33eaa5SJohn Baldwin static int 13976f33eaa5SJohn Baldwin pcib_release_pcie_irq(struct pcib_softc *sc) 13986f33eaa5SJohn Baldwin { 13996f33eaa5SJohn Baldwin device_t dev; 14006f33eaa5SJohn Baldwin int error; 14016f33eaa5SJohn Baldwin 14026f33eaa5SJohn Baldwin dev = sc->dev; 14036f33eaa5SJohn Baldwin error = bus_teardown_intr(dev, sc->pcie_irq, sc->pcie_ihand); 14046f33eaa5SJohn Baldwin if (error) 14056f33eaa5SJohn Baldwin return (error); 14066f33eaa5SJohn Baldwin error = bus_free_resource(dev, SYS_RES_IRQ, sc->pcie_irq); 14076f33eaa5SJohn Baldwin if (error) 14086f33eaa5SJohn Baldwin return (error); 1409e6b83836SJohn Baldwin error = pci_release_msi(dev); 1410e6b83836SJohn Baldwin if (error) 1411e6b83836SJohn Baldwin return (error); 1412e6b83836SJohn Baldwin if (sc->pcie_mem != NULL) 1413e6b83836SJohn Baldwin error = bus_free_resource(dev, SYS_RES_MEMORY, sc->pcie_mem); 1414e6b83836SJohn Baldwin return (error); 14156f33eaa5SJohn Baldwin } 14166f33eaa5SJohn Baldwin 141782cb5c3bSJohn Baldwin static void 141882cb5c3bSJohn Baldwin pcib_setup_hotplug(struct pcib_softc *sc) 141982cb5c3bSJohn Baldwin { 142082cb5c3bSJohn Baldwin device_t dev; 142182cb5c3bSJohn Baldwin uint16_t mask, val; 142282cb5c3bSJohn Baldwin 142382cb5c3bSJohn Baldwin dev = sc->dev; 142482cb5c3bSJohn Baldwin TASK_INIT(&sc->pcie_hp_task, 0, pcib_pcie_hotplug_task, sc); 1425fa3b03d3SAlexander Motin TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_ab_task, 0, 1426fa3b03d3SAlexander Motin pcib_pcie_ab_timeout, sc); 1427fa3b03d3SAlexander Motin TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_cc_task, 0, 1428fa3b03d3SAlexander Motin pcib_pcie_cc_timeout, sc); 1429fa3b03d3SAlexander Motin TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_dll_task, 0, 1430fa3b03d3SAlexander Motin pcib_pcie_dll_timeout, sc); 1431c6df6f53SWarner Losh sc->pcie_hp_lock = bus_topo_mtx(); 143282cb5c3bSJohn Baldwin 143382cb5c3bSJohn Baldwin /* Allocate IRQ. */ 143482cb5c3bSJohn Baldwin if (pcib_alloc_pcie_irq(sc) != 0) 143582cb5c3bSJohn Baldwin return; 143682cb5c3bSJohn Baldwin 143782cb5c3bSJohn Baldwin sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 143882cb5c3bSJohn Baldwin sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 143982cb5c3bSJohn Baldwin 14406f33eaa5SJohn Baldwin /* Clear any events previously pending. */ 14416f33eaa5SJohn Baldwin pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2); 14426f33eaa5SJohn Baldwin 144382cb5c3bSJohn Baldwin /* Enable HotPlug events. */ 144482cb5c3bSJohn Baldwin mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | 144582cb5c3bSJohn Baldwin PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE | 144682cb5c3bSJohn Baldwin PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE; 144737290148SEric van Gyzen val = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | PCIEM_SLOT_CTL_PDCE; 144882cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_APB) 144982cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_ABPE; 145082cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) 145182cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PFDE; 145282cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) 145382cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_MRLSCE; 145482cb5c3bSJohn Baldwin if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS)) 145582cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_CCIE; 145682cb5c3bSJohn Baldwin 145782cb5c3bSJohn Baldwin /* Turn the attention indicator off. */ 145882cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) { 145982cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_AIC; 146082cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_AI_OFF; 146182cb5c3bSJohn Baldwin } 146282cb5c3bSJohn Baldwin 146382cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, val, mask, false); 146482cb5c3bSJohn Baldwin } 14656f33eaa5SJohn Baldwin 14666f33eaa5SJohn Baldwin static int 14676f33eaa5SJohn Baldwin pcib_detach_hotplug(struct pcib_softc *sc) 14686f33eaa5SJohn Baldwin { 14696f33eaa5SJohn Baldwin uint16_t mask, val; 14706f33eaa5SJohn Baldwin int error; 14716f33eaa5SJohn Baldwin 14726f33eaa5SJohn Baldwin /* Disable the card in the slot and force it to detach. */ 14736f33eaa5SJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 14746f33eaa5SJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 1475fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_ab_task, 1476fa3b03d3SAlexander Motin NULL); 14776f33eaa5SJohn Baldwin } 14786f33eaa5SJohn Baldwin sc->flags |= PCIB_DETACHING; 14796f33eaa5SJohn Baldwin 14806f33eaa5SJohn Baldwin if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) { 1481fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_cc_task, 1482fa3b03d3SAlexander Motin NULL); 14836f33eaa5SJohn Baldwin tsleep(sc, 0, "hpcmd", hz); 14846f33eaa5SJohn Baldwin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 14856f33eaa5SJohn Baldwin } 14866f33eaa5SJohn Baldwin 14876f33eaa5SJohn Baldwin /* Disable HotPlug events. */ 14886f33eaa5SJohn Baldwin mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | 14896f33eaa5SJohn Baldwin PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE | 14906f33eaa5SJohn Baldwin PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE; 14916f33eaa5SJohn Baldwin val = 0; 14926f33eaa5SJohn Baldwin 14936f33eaa5SJohn Baldwin /* Turn the attention indicator off. */ 14946f33eaa5SJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) { 14956f33eaa5SJohn Baldwin mask |= PCIEM_SLOT_CTL_AIC; 14966f33eaa5SJohn Baldwin val |= PCIEM_SLOT_CTL_AI_OFF; 14976f33eaa5SJohn Baldwin } 14986f33eaa5SJohn Baldwin 14996f33eaa5SJohn Baldwin pcib_pcie_hotplug_update(sc, val, mask, false); 15006f33eaa5SJohn Baldwin 15016f33eaa5SJohn Baldwin error = pcib_release_pcie_irq(sc); 15026f33eaa5SJohn Baldwin if (error) 15036f33eaa5SJohn Baldwin return (error); 150412588ce0SAndriy Gapon taskqueue_drain(taskqueue_pci_hp, &sc->pcie_hp_task); 1505fa3b03d3SAlexander Motin taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_ab_task); 1506fa3b03d3SAlexander Motin taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_cc_task); 1507fa3b03d3SAlexander Motin taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_dll_task); 15086f33eaa5SJohn Baldwin return (0); 15096f33eaa5SJohn Baldwin } 151082cb5c3bSJohn Baldwin #endif 151182cb5c3bSJohn Baldwin 1512e36af292SJung-uk Kim /* 1513e36af292SJung-uk Kim * Get current bridge configuration. 1514e36af292SJung-uk Kim */ 1515e36af292SJung-uk Kim static void 1516e36af292SJung-uk Kim pcib_cfg_save(struct pcib_softc *sc) 1517e36af292SJung-uk Kim { 1518ad6f36f8SJohn Baldwin #ifndef NEW_PCIB 1519e36af292SJung-uk Kim device_t dev; 1520ad6f36f8SJohn Baldwin uint16_t command; 1521e36af292SJung-uk Kim 1522e36af292SJung-uk Kim dev = sc->dev; 1523e36af292SJung-uk Kim 1524ad6f36f8SJohn Baldwin command = pci_read_config(dev, PCIR_COMMAND, 2); 1525ad6f36f8SJohn Baldwin if (command & PCIM_CMD_PORTEN) 1526e36af292SJung-uk Kim pcib_get_io_decode(sc); 1527ad6f36f8SJohn Baldwin if (command & PCIM_CMD_MEMEN) 1528e36af292SJung-uk Kim pcib_get_mem_decode(sc); 152983c41143SJohn Baldwin #endif 1530e36af292SJung-uk Kim } 1531e36af292SJung-uk Kim 1532e36af292SJung-uk Kim /* 1533e36af292SJung-uk Kim * Restore previous bridge configuration. 1534e36af292SJung-uk Kim */ 1535e36af292SJung-uk Kim static void 1536e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc) 1537e36af292SJung-uk Kim { 1538ad6f36f8SJohn Baldwin #ifndef NEW_PCIB 1539ad6f36f8SJohn Baldwin uint16_t command; 1540ad6f36f8SJohn Baldwin #endif 1541e36af292SJung-uk Kim 154283c41143SJohn Baldwin #ifdef NEW_PCIB 154383c41143SJohn Baldwin pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM); 154483c41143SJohn Baldwin #else 1545151ba793SAlexander Kabaev command = pci_read_config(sc->dev, PCIR_COMMAND, 2); 1546ad6f36f8SJohn Baldwin if (command & PCIM_CMD_PORTEN) 1547e36af292SJung-uk Kim pcib_set_io_decode(sc); 1548ad6f36f8SJohn Baldwin if (command & PCIM_CMD_MEMEN) 1549e36af292SJung-uk Kim pcib_set_mem_decode(sc); 155083c41143SJohn Baldwin #endif 1551e36af292SJung-uk Kim } 1552e36af292SJung-uk Kim 1553e36af292SJung-uk Kim /* 1554bb0d0a8eSMike Smith * Generic device interface 1555bb0d0a8eSMike Smith */ 1556bb0d0a8eSMike Smith static int 1557bb0d0a8eSMike Smith pcib_probe(device_t dev) 1558bb0d0a8eSMike Smith { 1559bb0d0a8eSMike Smith if ((pci_get_class(dev) == PCIC_BRIDGE) && 1560bb0d0a8eSMike Smith (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) { 1561bb0d0a8eSMike Smith device_set_desc(dev, "PCI-PCI bridge"); 1562b7cbd25bSMarcel Moolenaar return(-10000); 1563bb0d0a8eSMike Smith } 1564bb0d0a8eSMike Smith return(ENXIO); 1565bb0d0a8eSMike Smith } 1566bb0d0a8eSMike Smith 15676f0d5884SJohn Baldwin void 15686f0d5884SJohn Baldwin pcib_attach_common(device_t dev) 1569bb0d0a8eSMike Smith { 1570bb0d0a8eSMike Smith struct pcib_softc *sc; 1571abf07f13SWarner Losh struct sysctl_ctx_list *sctx; 1572abf07f13SWarner Losh struct sysctl_oid *soid; 1573c825d4dcSJohn Baldwin int comma; 1574bb0d0a8eSMike Smith 1575bb0d0a8eSMike Smith sc = device_get_softc(dev); 1576bb0d0a8eSMike Smith sc->dev = dev; 1577bb0d0a8eSMike Smith 15784fa59183SMike Smith /* 15794fa59183SMike Smith * Get current bridge configuration. 15804fa59183SMike Smith */ 158155aaf894SMarius Strobl sc->domain = pci_get_domain(dev); 1582ad6f36f8SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 1583ad6f36f8SJohn Baldwin sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1); 1584ad6f36f8SJohn Baldwin sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1); 1585ad6f36f8SJohn Baldwin #endif 1586ad6f36f8SJohn Baldwin sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 1587e36af292SJung-uk Kim pcib_cfg_save(sc); 15884fa59183SMike Smith 15894fa59183SMike Smith /* 15904edef187SJohn Baldwin * The primary bus register should always be the bus of the 15914edef187SJohn Baldwin * parent. 15924edef187SJohn Baldwin */ 15934edef187SJohn Baldwin sc->pribus = pci_get_bus(dev); 15944edef187SJohn Baldwin pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1); 15954edef187SJohn Baldwin 15964edef187SJohn Baldwin /* 1597abf07f13SWarner Losh * Setup sysctl reporting nodes 1598abf07f13SWarner Losh */ 1599abf07f13SWarner Losh sctx = device_get_sysctl_ctx(dev); 1600abf07f13SWarner Losh soid = device_get_sysctl_tree(dev); 1601abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain", 1602abf07f13SWarner Losh CTLFLAG_RD, &sc->domain, 0, "Domain number"); 1603abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus", 1604abf07f13SWarner Losh CTLFLAG_RD, &sc->pribus, 0, "Primary bus number"); 1605abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus", 16064edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number"); 1607abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus", 16084edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number"); 1609abf07f13SWarner Losh 1610abf07f13SWarner Losh /* 16114fa59183SMike Smith * Quirk handling. 16124fa59183SMike Smith */ 16134fa59183SMike Smith switch (pci_get_devid(dev)) { 16142ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 16154fa59183SMike Smith case 0x12258086: /* Intel 82454KX/GX (Orion) */ 16164fa59183SMike Smith { 1617b0cb115fSWarner Losh uint8_t supbus; 16184fa59183SMike Smith 16194fa59183SMike Smith supbus = pci_read_config(dev, 0x41, 1); 16204fa59183SMike Smith if (supbus != 0xff) { 16214edef187SJohn Baldwin sc->bus.sec = supbus + 1; 16224edef187SJohn Baldwin sc->bus.sub = supbus + 1; 16234fa59183SMike Smith } 16244fa59183SMike Smith break; 16254fa59183SMike Smith } 16264edef187SJohn Baldwin #endif 16274fa59183SMike Smith 1628e4b59fc5SWarner Losh /* 1629e4b59fc5SWarner Losh * The i82380FB mobile docking controller is a PCI-PCI bridge, 1630e4b59fc5SWarner Losh * and it is a subtractive bridge. However, the ProgIf is wrong 1631e4b59fc5SWarner Losh * so the normal setting of PCIB_SUBTRACTIVE bit doesn't 16324718610dSZbigniew Bodek * happen. There are also Toshiba and Cavium ThunderX bridges 16334718610dSZbigniew Bodek * that behave this way. 1634e4b59fc5SWarner Losh */ 16354718610dSZbigniew Bodek case 0xa002177d: /* Cavium ThunderX */ 1636e4b59fc5SWarner Losh case 0x124b8086: /* Intel 82380FB Mobile */ 1637e4b59fc5SWarner Losh case 0x060513d7: /* Toshiba ???? */ 1638e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 1639e4b59fc5SWarner Losh break; 1640c94d6dbeSJung-uk Kim 16412ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 1642c94d6dbeSJung-uk Kim /* Compaq R3000 BIOS sets wrong subordinate bus number. */ 1643c94d6dbeSJung-uk Kim case 0x00dd10de: 1644c94d6dbeSJung-uk Kim { 1645c94d6dbeSJung-uk Kim char *cp; 1646c94d6dbeSJung-uk Kim 16472be111bfSDavide Italiano if ((cp = kern_getenv("smbios.planar.maker")) == NULL) 1648c94d6dbeSJung-uk Kim break; 16491def0ca6SJung-uk Kim if (strncmp(cp, "Compal", 6) != 0) { 16501def0ca6SJung-uk Kim freeenv(cp); 1651c94d6dbeSJung-uk Kim break; 16521def0ca6SJung-uk Kim } 16531def0ca6SJung-uk Kim freeenv(cp); 16542be111bfSDavide Italiano if ((cp = kern_getenv("smbios.planar.product")) == NULL) 16551def0ca6SJung-uk Kim break; 16561def0ca6SJung-uk Kim if (strncmp(cp, "08A0", 4) != 0) { 16571def0ca6SJung-uk Kim freeenv(cp); 16581def0ca6SJung-uk Kim break; 16591def0ca6SJung-uk Kim } 16601def0ca6SJung-uk Kim freeenv(cp); 16614edef187SJohn Baldwin if (sc->bus.sub < 0xa) { 1662c94d6dbeSJung-uk Kim pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1); 16634edef187SJohn Baldwin sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1); 1664c94d6dbeSJung-uk Kim } 1665c94d6dbeSJung-uk Kim break; 1666c94d6dbeSJung-uk Kim } 16674edef187SJohn Baldwin #endif 1668e4b59fc5SWarner Losh } 1669e4b59fc5SWarner Losh 167022bf1c7fSJohn Baldwin if (pci_msi_device_blacklisted(dev)) 167122bf1c7fSJohn Baldwin sc->flags |= PCIB_DISABLE_MSI; 167222bf1c7fSJohn Baldwin 167368e9cbd3SMarius Strobl if (pci_msix_device_blacklisted(dev)) 167468e9cbd3SMarius Strobl sc->flags |= PCIB_DISABLE_MSIX; 167568e9cbd3SMarius Strobl 1676e4b59fc5SWarner Losh /* 1677e4b59fc5SWarner Losh * Intel 815, 845 and other chipsets say they are PCI-PCI bridges, 1678e4b59fc5SWarner Losh * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM, 1679e4b59fc5SWarner Losh * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese. 1680e4b59fc5SWarner Losh * This means they act as if they were subtractively decoding 1681e4b59fc5SWarner Losh * bridges and pass all transactions. Mark them and real ProgIf 1 1682e4b59fc5SWarner Losh * parts as subtractive. 1683e4b59fc5SWarner Losh */ 1684e4b59fc5SWarner Losh if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 || 1685657d9f9fSJohn Baldwin pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE) 1686e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 1687e4b59fc5SWarner Losh 168882cb5c3bSJohn Baldwin #ifdef PCI_HP 168982cb5c3bSJohn Baldwin pcib_probe_hotplug(sc); 169082cb5c3bSJohn Baldwin #endif 169183c41143SJohn Baldwin #ifdef NEW_PCIB 16924edef187SJohn Baldwin #ifdef PCI_RES_BUS 16934edef187SJohn Baldwin pcib_setup_secbus(dev, &sc->bus, 1); 16944edef187SJohn Baldwin #endif 169583c41143SJohn Baldwin pcib_probe_windows(sc); 169683c41143SJohn Baldwin #endif 169782cb5c3bSJohn Baldwin #ifdef PCI_HP 169882cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG) 169982cb5c3bSJohn Baldwin pcib_setup_hotplug(sc); 170082cb5c3bSJohn Baldwin #endif 1701bb0d0a8eSMike Smith if (bootverbose) { 170255aaf894SMarius Strobl device_printf(dev, " domain %d\n", sc->domain); 17034edef187SJohn Baldwin device_printf(dev, " secondary bus %d\n", sc->bus.sec); 17044edef187SJohn Baldwin device_printf(dev, " subordinate bus %d\n", sc->bus.sub); 170583c41143SJohn Baldwin #ifdef NEW_PCIB 170683c41143SJohn Baldwin if (pcib_is_window_open(&sc->io)) 170783c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%jx-0x%jx\n", 170883c41143SJohn Baldwin (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit); 170983c41143SJohn Baldwin if (pcib_is_window_open(&sc->mem)) 171083c41143SJohn Baldwin device_printf(dev, " memory decode 0x%jx-0x%jx\n", 171183c41143SJohn Baldwin (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit); 171283c41143SJohn Baldwin if (pcib_is_window_open(&sc->pmem)) 171383c41143SJohn Baldwin device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 171483c41143SJohn Baldwin (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit); 171583c41143SJohn Baldwin #else 171683c41143SJohn Baldwin if (pcib_is_io_open(sc)) 171783c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%x-0x%x\n", 171883c41143SJohn Baldwin sc->iobase, sc->iolimit); 1719b0a2d4b8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 1720b0a2d4b8SWarner Losh device_printf(dev, " memory decode 0x%jx-0x%jx\n", 1721b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit); 1722b0a2d4b8SWarner Losh if (pcib_is_prefetch_open(sc)) 1723b0a2d4b8SWarner Losh device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 1724b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 172583c41143SJohn Baldwin #endif 1726c825d4dcSJohn Baldwin if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) || 1727c825d4dcSJohn Baldwin sc->flags & PCIB_SUBTRACTIVE) { 1728c825d4dcSJohn Baldwin device_printf(dev, " special decode "); 1729c825d4dcSJohn Baldwin comma = 0; 1730c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) { 1731c825d4dcSJohn Baldwin printf("ISA"); 1732c825d4dcSJohn Baldwin comma = 1; 1733c825d4dcSJohn Baldwin } 1734c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) { 1735c825d4dcSJohn Baldwin printf("%sVGA", comma ? ", " : ""); 1736c825d4dcSJohn Baldwin comma = 1; 1737c825d4dcSJohn Baldwin } 1738e4b59fc5SWarner Losh if (sc->flags & PCIB_SUBTRACTIVE) 1739c825d4dcSJohn Baldwin printf("%ssubtractive", comma ? ", " : ""); 1740c825d4dcSJohn Baldwin printf("\n"); 1741c825d4dcSJohn Baldwin } 1742bb0d0a8eSMike Smith } 1743bb0d0a8eSMike Smith 1744bb0d0a8eSMike Smith /* 1745ef888152SJohn Baldwin * Always enable busmastering on bridges so that transactions 1746ef888152SJohn Baldwin * initiated on the secondary bus are passed through to the 1747ef888152SJohn Baldwin * primary bus. 1748ef888152SJohn Baldwin */ 1749ef888152SJohn Baldwin pci_enable_busmaster(dev); 17506f0d5884SJohn Baldwin } 1751bb0d0a8eSMike Smith 175282cb5c3bSJohn Baldwin #ifdef PCI_HP 175382cb5c3bSJohn Baldwin static int 175482cb5c3bSJohn Baldwin pcib_present(struct pcib_softc *sc) 175582cb5c3bSJohn Baldwin { 175682cb5c3bSJohn Baldwin 175782cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG) 175882cb5c3bSJohn Baldwin return (pcib_hotplug_present(sc) != 0); 175982cb5c3bSJohn Baldwin return (1); 176082cb5c3bSJohn Baldwin } 176182cb5c3bSJohn Baldwin #endif 176282cb5c3bSJohn Baldwin 176338906aedSJohn Baldwin int 176467e7d085SJohn Baldwin pcib_attach_child(device_t dev) 17656f0d5884SJohn Baldwin { 17666f0d5884SJohn Baldwin struct pcib_softc *sc; 17676f0d5884SJohn Baldwin 17686f0d5884SJohn Baldwin sc = device_get_softc(dev); 176967e7d085SJohn Baldwin if (sc->bus.sec == 0) { 177067e7d085SJohn Baldwin /* no secondary bus; we should have fixed this */ 177167e7d085SJohn Baldwin return(0); 177267e7d085SJohn Baldwin } 177367e7d085SJohn Baldwin 177482cb5c3bSJohn Baldwin #ifdef PCI_HP 177582cb5c3bSJohn Baldwin if (!pcib_present(sc)) { 177682cb5c3bSJohn Baldwin /* An empty HotPlug slot, so don't add a PCI bus yet. */ 177782cb5c3bSJohn Baldwin return (0); 177882cb5c3bSJohn Baldwin } 177982cb5c3bSJohn Baldwin #endif 178082cb5c3bSJohn Baldwin 178167e7d085SJohn Baldwin sc->child = device_add_child(dev, "pci", -1); 1782bb0d0a8eSMike Smith return (bus_generic_attach(dev)); 1783bb0d0a8eSMike Smith } 1784bb0d0a8eSMike Smith 178567e7d085SJohn Baldwin int 178667e7d085SJohn Baldwin pcib_attach(device_t dev) 178767e7d085SJohn Baldwin { 178867e7d085SJohn Baldwin 178967e7d085SJohn Baldwin pcib_attach_common(dev); 179067e7d085SJohn Baldwin return (pcib_attach_child(dev)); 1791bb0d0a8eSMike Smith } 1792bb0d0a8eSMike Smith 17936f0d5884SJohn Baldwin int 17946f33eaa5SJohn Baldwin pcib_detach(device_t dev) 17956f33eaa5SJohn Baldwin { 17966f33eaa5SJohn Baldwin #if defined(PCI_HP) || defined(NEW_PCIB) 17976f33eaa5SJohn Baldwin struct pcib_softc *sc; 17986f33eaa5SJohn Baldwin #endif 17996f33eaa5SJohn Baldwin int error; 18006f33eaa5SJohn Baldwin 18016f33eaa5SJohn Baldwin #if defined(PCI_HP) || defined(NEW_PCIB) 18026f33eaa5SJohn Baldwin sc = device_get_softc(dev); 18036f33eaa5SJohn Baldwin #endif 18046f33eaa5SJohn Baldwin error = bus_generic_detach(dev); 18056f33eaa5SJohn Baldwin if (error) 18066f33eaa5SJohn Baldwin return (error); 18076f33eaa5SJohn Baldwin #ifdef PCI_HP 18086f33eaa5SJohn Baldwin if (sc->flags & PCIB_HOTPLUG) { 18096f33eaa5SJohn Baldwin error = pcib_detach_hotplug(sc); 18106f33eaa5SJohn Baldwin if (error) 18116f33eaa5SJohn Baldwin return (error); 18126f33eaa5SJohn Baldwin } 18136f33eaa5SJohn Baldwin #endif 18146f33eaa5SJohn Baldwin error = device_delete_children(dev); 18156f33eaa5SJohn Baldwin if (error) 18166f33eaa5SJohn Baldwin return (error); 18176f33eaa5SJohn Baldwin #ifdef NEW_PCIB 18186f33eaa5SJohn Baldwin pcib_free_windows(sc); 18196f33eaa5SJohn Baldwin #ifdef PCI_RES_BUS 18206f33eaa5SJohn Baldwin pcib_free_secbus(dev, &sc->bus); 18216f33eaa5SJohn Baldwin #endif 18226f33eaa5SJohn Baldwin #endif 18236f33eaa5SJohn Baldwin return (0); 18246f33eaa5SJohn Baldwin } 18256f33eaa5SJohn Baldwin 18266f33eaa5SJohn Baldwin int 1827e36af292SJung-uk Kim pcib_suspend(device_t dev) 1828e36af292SJung-uk Kim { 1829e36af292SJung-uk Kim 1830e36af292SJung-uk Kim pcib_cfg_save(device_get_softc(dev)); 18317212fc6aSJohn Baldwin return (bus_generic_suspend(dev)); 1832e36af292SJung-uk Kim } 1833e36af292SJung-uk Kim 1834e36af292SJung-uk Kim int 1835e36af292SJung-uk Kim pcib_resume(device_t dev) 1836e36af292SJung-uk Kim { 1837e36af292SJung-uk Kim 1838e36af292SJung-uk Kim pcib_cfg_restore(device_get_softc(dev)); 1839cffd37daSAndriy Gapon 1840cffd37daSAndriy Gapon /* 1841cffd37daSAndriy Gapon * Restore the Command register only after restoring the windows. 1842cffd37daSAndriy Gapon * The bridge should not be claiming random windows. 1843cffd37daSAndriy Gapon */ 1844cffd37daSAndriy Gapon pci_write_config(dev, PCIR_COMMAND, pci_get_cmdreg(dev), 2); 1845e36af292SJung-uk Kim return (bus_generic_resume(dev)); 1846e36af292SJung-uk Kim } 1847e36af292SJung-uk Kim 1848809923caSJustin Hibbits void 1849809923caSJustin Hibbits pcib_bridge_init(device_t dev) 1850809923caSJustin Hibbits { 1851809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 1852809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2); 1853809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1); 1854809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2); 1855809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2); 1856809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2); 1857809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 1858809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4); 1859809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2); 1860809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4); 1861809923caSJustin Hibbits } 1862809923caSJustin Hibbits 1863e36af292SJung-uk Kim int 186482cb5c3bSJohn Baldwin pcib_child_present(device_t dev, device_t child) 186582cb5c3bSJohn Baldwin { 186682cb5c3bSJohn Baldwin #ifdef PCI_HP 186782cb5c3bSJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 186882cb5c3bSJohn Baldwin int retval; 186982cb5c3bSJohn Baldwin 187082cb5c3bSJohn Baldwin retval = bus_child_present(dev); 187182cb5c3bSJohn Baldwin if (retval != 0 && sc->flags & PCIB_HOTPLUG) 187282cb5c3bSJohn Baldwin retval = pcib_hotplug_present(sc); 187382cb5c3bSJohn Baldwin return (retval); 187482cb5c3bSJohn Baldwin #else 187582cb5c3bSJohn Baldwin return (bus_child_present(dev)); 187682cb5c3bSJohn Baldwin #endif 187782cb5c3bSJohn Baldwin } 187882cb5c3bSJohn Baldwin 187982cb5c3bSJohn Baldwin int 1880bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 1881bb0d0a8eSMike Smith { 1882bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 1883bb0d0a8eSMike Smith 1884bb0d0a8eSMike Smith switch (which) { 188555aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 188655aaf894SMarius Strobl *result = sc->domain; 188755aaf894SMarius Strobl return(0); 1888bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 18894edef187SJohn Baldwin *result = sc->bus.sec; 1890bb0d0a8eSMike Smith return(0); 1891bb0d0a8eSMike Smith } 1892bb0d0a8eSMike Smith return(ENOENT); 1893bb0d0a8eSMike Smith } 1894bb0d0a8eSMike Smith 18956f0d5884SJohn Baldwin int 1896bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 1897bb0d0a8eSMike Smith { 1898bb0d0a8eSMike Smith 1899bb0d0a8eSMike Smith switch (which) { 190055aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 190155aaf894SMarius Strobl return(EINVAL); 1902bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 19034edef187SJohn Baldwin return(EINVAL); 1904bb0d0a8eSMike Smith } 1905bb0d0a8eSMike Smith return(ENOENT); 1906bb0d0a8eSMike Smith } 1907bb0d0a8eSMike Smith 190883c41143SJohn Baldwin #ifdef NEW_PCIB 190983c41143SJohn Baldwin /* 191083c41143SJohn Baldwin * Attempt to allocate a resource from the existing resources assigned 191183c41143SJohn Baldwin * to a window. 191283c41143SJohn Baldwin */ 191383c41143SJohn Baldwin static struct resource * 191483c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w, 19152dd1bdf1SJustin Hibbits device_t child, int type, int *rid, rman_res_t start, rman_res_t end, 19162dd1bdf1SJustin Hibbits rman_res_t count, u_int flags) 191783c41143SJohn Baldwin { 191883c41143SJohn Baldwin struct resource *res; 191983c41143SJohn Baldwin 192083c41143SJohn Baldwin if (!pcib_is_window_open(w)) 192183c41143SJohn Baldwin return (NULL); 192283c41143SJohn Baldwin 192383c41143SJohn Baldwin res = rman_reserve_resource(&w->rman, start, end, count, 192483c41143SJohn Baldwin flags & ~RF_ACTIVE, child); 192583c41143SJohn Baldwin if (res == NULL) 192683c41143SJohn Baldwin return (NULL); 192783c41143SJohn Baldwin 192883c41143SJohn Baldwin if (bootverbose) 192983c41143SJohn Baldwin device_printf(sc->dev, 1930da1b038aSJustin Hibbits "allocated %s range (%#jx-%#jx) for rid %x of %s\n", 193183c41143SJohn Baldwin w->name, rman_get_start(res), rman_get_end(res), *rid, 193283c41143SJohn Baldwin pcib_child_name(child)); 193383c41143SJohn Baldwin rman_set_rid(res, *rid); 19341b9bcfffSJohn Baldwin rman_set_type(res, type); 193583c41143SJohn Baldwin 193683c41143SJohn Baldwin if (flags & RF_ACTIVE) { 193783c41143SJohn Baldwin if (bus_activate_resource(child, type, *rid, res) != 0) { 193883c41143SJohn Baldwin rman_release_resource(res); 193983c41143SJohn Baldwin return (NULL); 194083c41143SJohn Baldwin } 194183c41143SJohn Baldwin } 194283c41143SJohn Baldwin 194383c41143SJohn Baldwin return (res); 194483c41143SJohn Baldwin } 194583c41143SJohn Baldwin 1946c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */ 1947c825d4dcSJohn Baldwin static int 1948c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type, 19492dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 1950c825d4dcSJohn Baldwin { 1951c825d4dcSJohn Baldwin struct resource *res; 19522dd1bdf1SJustin Hibbits rman_res_t base, limit, wmask; 1953c825d4dcSJohn Baldwin int rid; 1954c825d4dcSJohn Baldwin 1955c825d4dcSJohn Baldwin /* 1956c825d4dcSJohn Baldwin * If this is an I/O window on a bridge with ISA enable set 1957c825d4dcSJohn Baldwin * and the start address is below 64k, then try to allocate an 1958c825d4dcSJohn Baldwin * initial window of 0x1000 bytes long starting at address 1959c825d4dcSJohn Baldwin * 0xf000 and walking down. Note that if the original request 1960c825d4dcSJohn Baldwin * was larger than the non-aliased range size of 0x100 our 1961c825d4dcSJohn Baldwin * caller would have raised the start address up to 64k 1962c825d4dcSJohn Baldwin * already. 1963c825d4dcSJohn Baldwin */ 1964c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1965c825d4dcSJohn Baldwin start < 65536) { 1966c825d4dcSJohn Baldwin for (base = 0xf000; (long)base >= 0; base -= 0x1000) { 1967c825d4dcSJohn Baldwin limit = base + 0xfff; 1968c825d4dcSJohn Baldwin 1969c825d4dcSJohn Baldwin /* 1970c825d4dcSJohn Baldwin * Skip ranges that wouldn't work for the 1971c825d4dcSJohn Baldwin * original request. Note that the actual 1972c825d4dcSJohn Baldwin * window that overlaps are the non-alias 1973c825d4dcSJohn Baldwin * ranges within [base, limit], so this isn't 1974c825d4dcSJohn Baldwin * quite a simple comparison. 1975c825d4dcSJohn Baldwin */ 1976c825d4dcSJohn Baldwin if (start + count > limit - 0x400) 1977c825d4dcSJohn Baldwin continue; 1978c825d4dcSJohn Baldwin if (base == 0) { 1979c825d4dcSJohn Baldwin /* 1980c825d4dcSJohn Baldwin * The first open region for the window at 1981c825d4dcSJohn Baldwin * 0 is 0x400-0x4ff. 1982c825d4dcSJohn Baldwin */ 1983c825d4dcSJohn Baldwin if (end - count + 1 < 0x400) 1984c825d4dcSJohn Baldwin continue; 1985c825d4dcSJohn Baldwin } else { 1986c825d4dcSJohn Baldwin if (end - count + 1 < base) 1987c825d4dcSJohn Baldwin continue; 1988c825d4dcSJohn Baldwin } 1989c825d4dcSJohn Baldwin 1990c825d4dcSJohn Baldwin if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) { 1991c825d4dcSJohn Baldwin w->base = base; 1992c825d4dcSJohn Baldwin w->limit = limit; 1993c825d4dcSJohn Baldwin return (0); 1994c825d4dcSJohn Baldwin } 1995c825d4dcSJohn Baldwin } 1996c825d4dcSJohn Baldwin return (ENOSPC); 1997c825d4dcSJohn Baldwin } 1998c825d4dcSJohn Baldwin 199989977ce2SJustin Hibbits wmask = ((rman_res_t)1 << w->step) - 1; 2000c825d4dcSJohn Baldwin if (RF_ALIGNMENT(flags) < w->step) { 2001c825d4dcSJohn Baldwin flags &= ~RF_ALIGNMENT_MASK; 2002c825d4dcSJohn Baldwin flags |= RF_ALIGNMENT_LOG2(w->step); 2003c825d4dcSJohn Baldwin } 2004c825d4dcSJohn Baldwin start &= ~wmask; 2005c825d4dcSJohn Baldwin end |= wmask; 200689977ce2SJustin Hibbits count = roundup2(count, (rman_res_t)1 << w->step); 2007c825d4dcSJohn Baldwin rid = w->reg; 2008c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, start, end, count, 2009b377ff81SJohn Baldwin flags | RF_ACTIVE | RF_UNMAPPED); 2010c825d4dcSJohn Baldwin if (res == NULL) 2011c825d4dcSJohn Baldwin return (ENOSPC); 2012c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 2013c825d4dcSJohn Baldwin pcib_activate_window(sc, type); 2014c825d4dcSJohn Baldwin w->base = rman_get_start(res); 2015c825d4dcSJohn Baldwin w->limit = rman_get_end(res); 2016c825d4dcSJohn Baldwin return (0); 2017c825d4dcSJohn Baldwin } 2018c825d4dcSJohn Baldwin 2019c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */ 2020c825d4dcSJohn Baldwin static int 2021c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type, 20222dd1bdf1SJustin Hibbits rman_res_t base, rman_res_t limit) 2023c825d4dcSJohn Baldwin { 2024c825d4dcSJohn Baldwin struct resource *res; 2025c825d4dcSJohn Baldwin int error, i, force_64k_base; 2026c825d4dcSJohn Baldwin 2027c825d4dcSJohn Baldwin KASSERT(base <= w->base && limit >= w->limit, 2028c825d4dcSJohn Baldwin ("attempting to shrink window")); 2029c825d4dcSJohn Baldwin 2030c825d4dcSJohn Baldwin /* 2031c825d4dcSJohn Baldwin * XXX: pcib_grow_window() doesn't try to do this anyway and 2032c825d4dcSJohn Baldwin * the error handling for all the edge cases would be tedious. 2033c825d4dcSJohn Baldwin */ 2034c825d4dcSJohn Baldwin KASSERT(limit == w->limit || base == w->base, 2035c825d4dcSJohn Baldwin ("attempting to grow both ends of a window")); 2036c825d4dcSJohn Baldwin 2037c825d4dcSJohn Baldwin /* 2038c825d4dcSJohn Baldwin * Yet more special handling for requests to expand an I/O 2039c825d4dcSJohn Baldwin * window behind an ISA-enabled bridge. Since I/O windows 2040c825d4dcSJohn Baldwin * have to grow in 0x1000 increments and the end of the 0xffff 2041c825d4dcSJohn Baldwin * range is an alias, growing a window below 64k will always 2042c825d4dcSJohn Baldwin * result in allocating new resources and never adjusting an 2043c825d4dcSJohn Baldwin * existing resource. 2044c825d4dcSJohn Baldwin */ 2045c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 2046c825d4dcSJohn Baldwin (limit <= 65535 || (base <= 65535 && base != w->base))) { 2047c825d4dcSJohn Baldwin KASSERT(limit == w->limit || limit <= 65535, 2048c825d4dcSJohn Baldwin ("attempting to grow both ends across 64k ISA alias")); 2049c825d4dcSJohn Baldwin 2050c825d4dcSJohn Baldwin if (base != w->base) 2051c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1); 2052c825d4dcSJohn Baldwin else 2053c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, w->limit + 1, 2054c825d4dcSJohn Baldwin limit); 2055c825d4dcSJohn Baldwin if (error == 0) { 2056c825d4dcSJohn Baldwin w->base = base; 2057c825d4dcSJohn Baldwin w->limit = limit; 2058c825d4dcSJohn Baldwin } 2059c825d4dcSJohn Baldwin return (error); 2060c825d4dcSJohn Baldwin } 2061c825d4dcSJohn Baldwin 2062c825d4dcSJohn Baldwin /* 2063c825d4dcSJohn Baldwin * Find the existing resource to adjust. Usually there is only one, 2064c825d4dcSJohn Baldwin * but for an ISA-enabled bridge we might be growing the I/O window 2065c825d4dcSJohn Baldwin * above 64k and need to find the existing resource that maps all 2066c825d4dcSJohn Baldwin * of the area above 64k. 2067c825d4dcSJohn Baldwin */ 2068c825d4dcSJohn Baldwin for (i = 0; i < w->count; i++) { 2069c825d4dcSJohn Baldwin if (rman_get_end(w->res[i]) == w->limit) 2070c825d4dcSJohn Baldwin break; 2071c825d4dcSJohn Baldwin } 2072c825d4dcSJohn Baldwin KASSERT(i != w->count, ("did not find existing resource")); 2073c825d4dcSJohn Baldwin res = w->res[i]; 2074c825d4dcSJohn Baldwin 2075c825d4dcSJohn Baldwin /* 2076c825d4dcSJohn Baldwin * Usually the resource we found should match the window's 2077c825d4dcSJohn Baldwin * existing range. The one exception is the ISA-enabled case 2078c825d4dcSJohn Baldwin * mentioned above in which case the resource should start at 2079c825d4dcSJohn Baldwin * 64k. 2080c825d4dcSJohn Baldwin */ 2081c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 2082c825d4dcSJohn Baldwin w->base <= 65535) { 2083c825d4dcSJohn Baldwin KASSERT(rman_get_start(res) == 65536, 2084c825d4dcSJohn Baldwin ("existing resource mismatch")); 2085c825d4dcSJohn Baldwin force_64k_base = 1; 2086c825d4dcSJohn Baldwin } else { 2087c825d4dcSJohn Baldwin KASSERT(w->base == rman_get_start(res), 2088c825d4dcSJohn Baldwin ("existing resource mismatch")); 2089c825d4dcSJohn Baldwin force_64k_base = 0; 2090c825d4dcSJohn Baldwin } 2091c825d4dcSJohn Baldwin 2092c825d4dcSJohn Baldwin error = bus_adjust_resource(sc->dev, type, res, force_64k_base ? 2093c825d4dcSJohn Baldwin rman_get_start(res) : base, limit); 2094c825d4dcSJohn Baldwin if (error) 2095c825d4dcSJohn Baldwin return (error); 2096c825d4dcSJohn Baldwin 2097c825d4dcSJohn Baldwin /* Add the newly allocated region to the resource manager. */ 2098c825d4dcSJohn Baldwin if (w->base != base) { 2099c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, base, w->base - 1); 2100c825d4dcSJohn Baldwin w->base = base; 2101c825d4dcSJohn Baldwin } else { 2102c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, w->limit + 1, limit); 2103c825d4dcSJohn Baldwin w->limit = limit; 2104c825d4dcSJohn Baldwin } 2105c825d4dcSJohn Baldwin if (error) { 2106c825d4dcSJohn Baldwin if (bootverbose) 2107c825d4dcSJohn Baldwin device_printf(sc->dev, 2108c825d4dcSJohn Baldwin "failed to expand %s resource manager\n", w->name); 2109c825d4dcSJohn Baldwin (void)bus_adjust_resource(sc->dev, type, res, force_64k_base ? 2110c825d4dcSJohn Baldwin rman_get_start(res) : w->base, w->limit); 2111c825d4dcSJohn Baldwin } 2112c825d4dcSJohn Baldwin return (error); 2113c825d4dcSJohn Baldwin } 2114c825d4dcSJohn Baldwin 211583c41143SJohn Baldwin /* 211683c41143SJohn Baldwin * Attempt to grow a window to make room for a given resource request. 211783c41143SJohn Baldwin */ 211883c41143SJohn Baldwin static int 211983c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type, 21202dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 212183c41143SJohn Baldwin { 21222dd1bdf1SJustin Hibbits rman_res_t align, start_free, end_free, front, back, wmask; 2123c825d4dcSJohn Baldwin int error; 212483c41143SJohn Baldwin 212583c41143SJohn Baldwin /* 212683c41143SJohn Baldwin * Clamp the desired resource range to the maximum address 212783c41143SJohn Baldwin * this window supports. Reject impossible requests. 2128c825d4dcSJohn Baldwin * 2129c825d4dcSJohn Baldwin * For I/O port requests behind a bridge with the ISA enable 2130c825d4dcSJohn Baldwin * bit set, force large allocations to start above 64k. 213183c41143SJohn Baldwin */ 213283c41143SJohn Baldwin if (!w->valid) 213383c41143SJohn Baldwin return (EINVAL); 2134c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 && 2135c825d4dcSJohn Baldwin start < 65536) 2136c825d4dcSJohn Baldwin start = 65536; 213783c41143SJohn Baldwin if (end > w->rman.rm_end) 213883c41143SJohn Baldwin end = w->rman.rm_end; 213983c41143SJohn Baldwin if (start + count - 1 > end || start + count < start) 214083c41143SJohn Baldwin return (EINVAL); 214189977ce2SJustin Hibbits wmask = ((rman_res_t)1 << w->step) - 1; 214283c41143SJohn Baldwin 214383c41143SJohn Baldwin /* 214483c41143SJohn Baldwin * If there is no resource at all, just try to allocate enough 214583c41143SJohn Baldwin * aligned space for this resource. 214683c41143SJohn Baldwin */ 214783c41143SJohn Baldwin if (w->res == NULL) { 2148c825d4dcSJohn Baldwin error = pcib_alloc_new_window(sc, w, type, start, end, count, 2149c825d4dcSJohn Baldwin flags); 2150c825d4dcSJohn Baldwin if (error) { 215183c41143SJohn Baldwin if (bootverbose) 215283c41143SJohn Baldwin device_printf(sc->dev, 2153da1b038aSJustin Hibbits "failed to allocate initial %s window (%#jx-%#jx,%#jx)\n", 215483c41143SJohn Baldwin w->name, start, end, count); 215583c41143SJohn Baldwin return (error); 215683c41143SJohn Baldwin } 2157c825d4dcSJohn Baldwin if (bootverbose) 2158c825d4dcSJohn Baldwin device_printf(sc->dev, 2159c825d4dcSJohn Baldwin "allocated initial %s window of %#jx-%#jx\n", 2160c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 216183c41143SJohn Baldwin goto updatewin; 216283c41143SJohn Baldwin } 216383c41143SJohn Baldwin 216483c41143SJohn Baldwin /* 216583c41143SJohn Baldwin * See if growing the window would help. Compute the minimum 216683c41143SJohn Baldwin * amount of address space needed on both the front and back 216783c41143SJohn Baldwin * ends of the existing window to satisfy the allocation. 216883c41143SJohn Baldwin * 216983c41143SJohn Baldwin * For each end, build a candidate region adjusting for the 217083c41143SJohn Baldwin * required alignment, etc. If there is a free region at the 217183c41143SJohn Baldwin * edge of the window, grow from the inner edge of the free 217283c41143SJohn Baldwin * region. Otherwise grow from the window boundary. 217383c41143SJohn Baldwin * 2174c825d4dcSJohn Baldwin * Growing an I/O window below 64k for a bridge with the ISA 2175c825d4dcSJohn Baldwin * enable bit doesn't require any special magic as the step 2176c825d4dcSJohn Baldwin * size of an I/O window (1k) always includes multiple 2177c825d4dcSJohn Baldwin * non-alias ranges when it is grown in either direction. 2178c825d4dcSJohn Baldwin * 217983c41143SJohn Baldwin * XXX: Special case: if w->res is completely empty and the 218083c41143SJohn Baldwin * request size is larger than w->res, we should find the 218183c41143SJohn Baldwin * optimal aligned buffer containing w->res and allocate that. 218283c41143SJohn Baldwin */ 218383c41143SJohn Baldwin if (bootverbose) 218483c41143SJohn Baldwin device_printf(sc->dev, 2185da1b038aSJustin Hibbits "attempting to grow %s window for (%#jx-%#jx,%#jx)\n", 218683c41143SJohn Baldwin w->name, start, end, count); 218789977ce2SJustin Hibbits align = (rman_res_t)1 << RF_ALIGNMENT(flags); 2188c825d4dcSJohn Baldwin if (start < w->base) { 218983c41143SJohn Baldwin if (rman_first_free_region(&w->rman, &start_free, &end_free) != 2190c825d4dcSJohn Baldwin 0 || start_free != w->base) 2191c825d4dcSJohn Baldwin end_free = w->base; 219283c41143SJohn Baldwin if (end_free > end) 2193ddac8cc9SJohn Baldwin end_free = end + 1; 219483c41143SJohn Baldwin 219583c41143SJohn Baldwin /* Move end_free down until it is properly aligned. */ 219683c41143SJohn Baldwin end_free &= ~(align - 1); 2197a49dcb46SJohn Baldwin end_free--; 2198a49dcb46SJohn Baldwin front = end_free - (count - 1); 219983c41143SJohn Baldwin 220083c41143SJohn Baldwin /* 220183c41143SJohn Baldwin * The resource would now be allocated at (front, 220283c41143SJohn Baldwin * end_free). Ensure that fits in the (start, end) 220383c41143SJohn Baldwin * bounds. end_free is checked above. If 'front' is 220483c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 220583c41143SJohn Baldwin * Also check for underflow. 220683c41143SJohn Baldwin */ 220783c41143SJohn Baldwin if (front >= start && front <= end_free) { 220883c41143SJohn Baldwin if (bootverbose) 2209da1b038aSJustin Hibbits printf("\tfront candidate range: %#jx-%#jx\n", 221083c41143SJohn Baldwin front, end_free); 2211a7b5acacSJohn Baldwin front &= ~wmask; 2212c825d4dcSJohn Baldwin front = w->base - front; 221383c41143SJohn Baldwin } else 221483c41143SJohn Baldwin front = 0; 221583c41143SJohn Baldwin } else 221683c41143SJohn Baldwin front = 0; 2217c825d4dcSJohn Baldwin if (end > w->limit) { 221883c41143SJohn Baldwin if (rman_last_free_region(&w->rman, &start_free, &end_free) != 2219c825d4dcSJohn Baldwin 0 || end_free != w->limit) 2220c825d4dcSJohn Baldwin start_free = w->limit + 1; 222183c41143SJohn Baldwin if (start_free < start) 222283c41143SJohn Baldwin start_free = start; 222383c41143SJohn Baldwin 222483c41143SJohn Baldwin /* Move start_free up until it is properly aligned. */ 222583c41143SJohn Baldwin start_free = roundup2(start_free, align); 2226a49dcb46SJohn Baldwin back = start_free + count - 1; 222783c41143SJohn Baldwin 222883c41143SJohn Baldwin /* 222983c41143SJohn Baldwin * The resource would now be allocated at (start_free, 223083c41143SJohn Baldwin * back). Ensure that fits in the (start, end) 223183c41143SJohn Baldwin * bounds. start_free is checked above. If 'back' is 223283c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 223383c41143SJohn Baldwin * Also check for overflow. 223483c41143SJohn Baldwin */ 223583c41143SJohn Baldwin if (back <= end && start_free <= back) { 223683c41143SJohn Baldwin if (bootverbose) 2237da1b038aSJustin Hibbits printf("\tback candidate range: %#jx-%#jx\n", 223883c41143SJohn Baldwin start_free, back); 2239a7b5acacSJohn Baldwin back |= wmask; 2240c825d4dcSJohn Baldwin back -= w->limit; 224183c41143SJohn Baldwin } else 224283c41143SJohn Baldwin back = 0; 224383c41143SJohn Baldwin } else 224483c41143SJohn Baldwin back = 0; 224583c41143SJohn Baldwin 224683c41143SJohn Baldwin /* 224783c41143SJohn Baldwin * Try to allocate the smallest needed region first. 224883c41143SJohn Baldwin * If that fails, fall back to the other region. 224983c41143SJohn Baldwin */ 225083c41143SJohn Baldwin error = ENOSPC; 225183c41143SJohn Baldwin while (front != 0 || back != 0) { 225283c41143SJohn Baldwin if (front != 0 && (front <= back || back == 0)) { 2253c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base - front, 2254c825d4dcSJohn Baldwin w->limit); 225583c41143SJohn Baldwin if (error == 0) 225683c41143SJohn Baldwin break; 225783c41143SJohn Baldwin front = 0; 225883c41143SJohn Baldwin } else { 2259c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base, 2260c825d4dcSJohn Baldwin w->limit + back); 226183c41143SJohn Baldwin if (error == 0) 226283c41143SJohn Baldwin break; 226383c41143SJohn Baldwin back = 0; 226483c41143SJohn Baldwin } 226583c41143SJohn Baldwin } 226683c41143SJohn Baldwin 226783c41143SJohn Baldwin if (error) 226883c41143SJohn Baldwin return (error); 226983c41143SJohn Baldwin if (bootverbose) 2270c825d4dcSJohn Baldwin device_printf(sc->dev, "grew %s window to %#jx-%#jx\n", 2271c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 227283c41143SJohn Baldwin 227383c41143SJohn Baldwin updatewin: 2274c825d4dcSJohn Baldwin /* Write the new window. */ 2275a7b5acacSJohn Baldwin KASSERT((w->base & wmask) == 0, ("start address is not aligned")); 2276a7b5acacSJohn Baldwin KASSERT((w->limit & wmask) == wmask, ("end address is not aligned")); 227783c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 227883c41143SJohn Baldwin return (0); 227983c41143SJohn Baldwin } 228083c41143SJohn Baldwin 228183c41143SJohn Baldwin /* 228283c41143SJohn Baldwin * We have to trap resource allocation requests and ensure that the bridge 228383c41143SJohn Baldwin * is set up to, or capable of handling them. 228483c41143SJohn Baldwin */ 228503719c65SJohn Baldwin static struct resource * 228683c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 22872dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 228883c41143SJohn Baldwin { 228983c41143SJohn Baldwin struct pcib_softc *sc; 229083c41143SJohn Baldwin struct resource *r; 229183c41143SJohn Baldwin 229283c41143SJohn Baldwin sc = device_get_softc(dev); 229383c41143SJohn Baldwin 229483c41143SJohn Baldwin /* 229583c41143SJohn Baldwin * VGA resources are decoded iff the VGA enable bit is set in 229683c41143SJohn Baldwin * the bridge control register. VGA resources do not fall into 229783c41143SJohn Baldwin * the resource windows and are passed up to the parent. 229883c41143SJohn Baldwin */ 229983c41143SJohn Baldwin if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) || 230083c41143SJohn Baldwin (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) { 230183c41143SJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) 230283c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, 230383c41143SJohn Baldwin rid, start, end, count, flags)); 230483c41143SJohn Baldwin else 230583c41143SJohn Baldwin return (NULL); 230683c41143SJohn Baldwin } 230783c41143SJohn Baldwin 230883c41143SJohn Baldwin switch (type) { 23094edef187SJohn Baldwin #ifdef PCI_RES_BUS 23104edef187SJohn Baldwin case PCI_RES_BUS: 23114edef187SJohn Baldwin return (pcib_alloc_subbus(&sc->bus, child, rid, start, end, 23124edef187SJohn Baldwin count, flags)); 23134edef187SJohn Baldwin #endif 231483c41143SJohn Baldwin case SYS_RES_IOPORT: 2315c825d4dcSJohn Baldwin if (pcib_is_isa_range(sc, start, end, count)) 2316c825d4dcSJohn Baldwin return (NULL); 231783c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start, 231883c41143SJohn Baldwin end, count, flags); 2319a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 232083c41143SJohn Baldwin break; 232183c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->io, type, start, end, count, 232283c41143SJohn Baldwin flags) == 0) 232383c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, 232483c41143SJohn Baldwin rid, start, end, count, flags); 232583c41143SJohn Baldwin break; 232683c41143SJohn Baldwin case SYS_RES_MEMORY: 232783c41143SJohn Baldwin /* 232883c41143SJohn Baldwin * For prefetchable resources, prefer the prefetchable 232983c41143SJohn Baldwin * memory window, but fall back to the regular memory 233083c41143SJohn Baldwin * window if that fails. Try both windows before 233183c41143SJohn Baldwin * attempting to grow a window in case the firmware 233283c41143SJohn Baldwin * has used a range in the regular memory window to 233383c41143SJohn Baldwin * map a prefetchable BAR. 233483c41143SJohn Baldwin */ 233583c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 233683c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, type, 233783c41143SJohn Baldwin rid, start, end, count, flags); 233883c41143SJohn Baldwin if (r != NULL) 233983c41143SJohn Baldwin break; 234083c41143SJohn Baldwin } 234183c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid, 234283c41143SJohn Baldwin start, end, count, flags); 2343a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 234483c41143SJohn Baldwin break; 234583c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 234683c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->pmem, type, start, end, 234783c41143SJohn Baldwin count, flags) == 0) { 234883c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, 234983c41143SJohn Baldwin type, rid, start, end, count, flags); 235083c41143SJohn Baldwin if (r != NULL) 235183c41143SJohn Baldwin break; 235283c41143SJohn Baldwin } 235383c41143SJohn Baldwin } 235483c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->mem, type, start, end, count, 235583c41143SJohn Baldwin flags & ~RF_PREFETCHABLE) == 0) 235683c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, 235783c41143SJohn Baldwin rid, start, end, count, flags); 235883c41143SJohn Baldwin break; 235983c41143SJohn Baldwin default: 236083c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 236183c41143SJohn Baldwin start, end, count, flags)); 236283c41143SJohn Baldwin } 236383c41143SJohn Baldwin 236483c41143SJohn Baldwin /* 236583c41143SJohn Baldwin * If attempts to suballocate from the window fail but this is a 236683c41143SJohn Baldwin * subtractive bridge, pass the request up the tree. 236783c41143SJohn Baldwin */ 236883c41143SJohn Baldwin if (sc->flags & PCIB_SUBTRACTIVE && r == NULL) 236983c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 237083c41143SJohn Baldwin start, end, count, flags)); 237183c41143SJohn Baldwin return (r); 237283c41143SJohn Baldwin } 237383c41143SJohn Baldwin 237403719c65SJohn Baldwin static int 2375fef01f04SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, struct resource *r, 23762dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end) 237783c41143SJohn Baldwin { 237883c41143SJohn Baldwin struct pcib_softc *sc; 237926245980SJessica Clarke struct pcib_window *w; 238015cb3b54SAlexander Motin rman_res_t wmask; 2381fef01f04SJohn Baldwin int error, type; 238283c41143SJohn Baldwin 238383c41143SJohn Baldwin sc = device_get_softc(bus); 2384fef01f04SJohn Baldwin type = rman_get_type(r); 238526245980SJessica Clarke 238626245980SJessica Clarke /* 238726245980SJessica Clarke * If the resource wasn't sub-allocated from one of our region 238826245980SJessica Clarke * managers then just pass the request up. 238926245980SJessica Clarke */ 2390582b84cdSJohn Baldwin if (!pcib_is_resource_managed(sc, r)) 2391fef01f04SJohn Baldwin return (bus_generic_adjust_resource(bus, child, r, start, end)); 239226245980SJessica Clarke 239326245980SJessica Clarke #ifdef PCI_RES_BUS 239431776afdSJessica Clarke if (type == PCI_RES_BUS) { 239531776afdSJessica Clarke /* 239631776afdSJessica Clarke * If our bus range isn't big enough to grow the sub-allocation 239731776afdSJessica Clarke * then we need to grow our bus range. Any request that would 239831776afdSJessica Clarke * require us to decrease the start of our own bus range is 239931776afdSJessica Clarke * invalid, we can only extend the end; ignore such requests 240031776afdSJessica Clarke * and let rman_adjust_resource fail below. 240131776afdSJessica Clarke */ 240231776afdSJessica Clarke if (start >= sc->bus.sec && end > sc->bus.sub) { 240331776afdSJessica Clarke error = pcib_grow_subbus(&sc->bus, end); 240431776afdSJessica Clarke if (error != 0) 240531776afdSJessica Clarke return (error); 240631776afdSJessica Clarke } 240731776afdSJessica Clarke } else 240826245980SJessica Clarke #endif 240926245980SJessica Clarke { 241026245980SJessica Clarke /* 241126245980SJessica Clarke * Resource is managed and not a secondary bus number, must 241226245980SJessica Clarke * be from one of our windows. 241326245980SJessica Clarke */ 2414582b84cdSJohn Baldwin w = pcib_get_resource_window(sc, r); 241526245980SJessica Clarke KASSERT(w != NULL, 241626245980SJessica Clarke ("%s: no window for resource (%#jx-%#jx) type %d", 241726245980SJessica Clarke __func__, rman_get_start(r), rman_get_end(r), type)); 241826245980SJessica Clarke 241926245980SJessica Clarke /* 242026245980SJessica Clarke * If our window isn't big enough to grow the sub-allocation 242126245980SJessica Clarke * then we need to expand the window. 242226245980SJessica Clarke */ 242326245980SJessica Clarke if (start < w->base || end > w->limit) { 242415cb3b54SAlexander Motin wmask = ((rman_res_t)1 << w->step) - 1; 242515cb3b54SAlexander Motin error = pcib_expand_window(sc, w, type, 242615cb3b54SAlexander Motin MIN(start & ~wmask, w->base), 242715cb3b54SAlexander Motin MAX(end | wmask, w->limit)); 242826245980SJessica Clarke if (error != 0) 242926245980SJessica Clarke return (error); 243015cb3b54SAlexander Motin if (bootverbose) 243115cb3b54SAlexander Motin device_printf(sc->dev, 243215cb3b54SAlexander Motin "grew %s window to %#jx-%#jx\n", 243315cb3b54SAlexander Motin w->name, (uintmax_t)w->base, 243415cb3b54SAlexander Motin (uintmax_t)w->limit); 243515cb3b54SAlexander Motin pcib_write_windows(sc, w->mask); 243626245980SJessica Clarke } 243726245980SJessica Clarke } 243826245980SJessica Clarke 243983c41143SJohn Baldwin return (rman_adjust_resource(r, start, end)); 244083c41143SJohn Baldwin } 244183c41143SJohn Baldwin 244203719c65SJohn Baldwin static int 244383c41143SJohn Baldwin pcib_release_resource(device_t dev, device_t child, int type, int rid, 244483c41143SJohn Baldwin struct resource *r) 244583c41143SJohn Baldwin { 244683c41143SJohn Baldwin struct pcib_softc *sc; 244783c41143SJohn Baldwin int error; 244883c41143SJohn Baldwin 244983c41143SJohn Baldwin sc = device_get_softc(dev); 2450582b84cdSJohn Baldwin if (pcib_is_resource_managed(sc, r)) { 245183c41143SJohn Baldwin if (rman_get_flags(r) & RF_ACTIVE) { 245283c41143SJohn Baldwin error = bus_deactivate_resource(child, type, rid, r); 245383c41143SJohn Baldwin if (error) 245483c41143SJohn Baldwin return (error); 245583c41143SJohn Baldwin } 245683c41143SJohn Baldwin return (rman_release_resource(r)); 245783c41143SJohn Baldwin } 245883c41143SJohn Baldwin return (bus_generic_release_resource(dev, child, type, rid, r)); 245983c41143SJohn Baldwin } 2460b377ff81SJohn Baldwin 2461b377ff81SJohn Baldwin static int 2462*2baed46eSJohn Baldwin pcib_activate_resource(device_t dev, device_t child, struct resource *r) 2463b377ff81SJohn Baldwin { 2464b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 2465b377ff81SJohn Baldwin struct resource_map map; 2466*2baed46eSJohn Baldwin int error, type; 2467b377ff81SJohn Baldwin 2468582b84cdSJohn Baldwin if (!pcib_is_resource_managed(sc, r)) 2469*2baed46eSJohn Baldwin return (bus_generic_activate_resource(dev, child, r)); 2470b377ff81SJohn Baldwin 2471b377ff81SJohn Baldwin error = rman_activate_resource(r); 2472b377ff81SJohn Baldwin if (error != 0) 2473b377ff81SJohn Baldwin return (error); 2474b377ff81SJohn Baldwin 2475*2baed46eSJohn Baldwin type = rman_get_type(r); 2476b377ff81SJohn Baldwin if ((rman_get_flags(r) & RF_UNMAPPED) == 0 && 2477b377ff81SJohn Baldwin (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { 2478d77f2092SJohn Baldwin error = BUS_MAP_RESOURCE(dev, child, r, NULL, &map); 2479b377ff81SJohn Baldwin if (error != 0) { 2480b377ff81SJohn Baldwin rman_deactivate_resource(r); 2481b377ff81SJohn Baldwin return (error); 2482b377ff81SJohn Baldwin } 2483b377ff81SJohn Baldwin 2484b377ff81SJohn Baldwin rman_set_mapping(r, &map); 2485b377ff81SJohn Baldwin } 2486b377ff81SJohn Baldwin return (0); 2487b377ff81SJohn Baldwin } 2488b377ff81SJohn Baldwin 2489b377ff81SJohn Baldwin static int 2490*2baed46eSJohn Baldwin pcib_deactivate_resource(device_t dev, device_t child, struct resource *r) 2491b377ff81SJohn Baldwin { 2492b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 2493b377ff81SJohn Baldwin struct resource_map map; 2494*2baed46eSJohn Baldwin int error, type; 2495b377ff81SJohn Baldwin 2496582b84cdSJohn Baldwin if (!pcib_is_resource_managed(sc, r)) 2497*2baed46eSJohn Baldwin return (bus_generic_deactivate_resource(dev, child, r)); 2498b377ff81SJohn Baldwin 2499b377ff81SJohn Baldwin error = rman_deactivate_resource(r); 2500b377ff81SJohn Baldwin if (error != 0) 2501b377ff81SJohn Baldwin return (error); 2502b377ff81SJohn Baldwin 2503*2baed46eSJohn Baldwin type = rman_get_type(r); 2504b377ff81SJohn Baldwin if ((rman_get_flags(r) & RF_UNMAPPED) == 0 && 2505b377ff81SJohn Baldwin (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) { 2506b377ff81SJohn Baldwin rman_get_mapping(r, &map); 2507d77f2092SJohn Baldwin BUS_UNMAP_RESOURCE(dev, child, r, &map); 2508b377ff81SJohn Baldwin } 2509b377ff81SJohn Baldwin return (0); 2510b377ff81SJohn Baldwin } 2511b377ff81SJohn Baldwin 2512b377ff81SJohn Baldwin static struct resource * 2513b377ff81SJohn Baldwin pcib_find_parent_resource(struct pcib_window *w, struct resource *r) 2514b377ff81SJohn Baldwin { 2515b377ff81SJohn Baldwin for (int i = 0; i < w->count; i++) { 2516b377ff81SJohn Baldwin if (rman_get_start(w->res[i]) <= rman_get_start(r) && 2517b377ff81SJohn Baldwin rman_get_end(w->res[i]) >= rman_get_end(r)) 2518b377ff81SJohn Baldwin return (w->res[i]); 2519b377ff81SJohn Baldwin } 2520b377ff81SJohn Baldwin return (NULL); 2521b377ff81SJohn Baldwin } 2522b377ff81SJohn Baldwin 2523b377ff81SJohn Baldwin static int 2524d77f2092SJohn Baldwin pcib_map_resource(device_t dev, device_t child, struct resource *r, 2525b377ff81SJohn Baldwin struct resource_map_request *argsp, struct resource_map *map) 2526b377ff81SJohn Baldwin { 2527b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 2528b377ff81SJohn Baldwin struct resource_map_request args; 2529b377ff81SJohn Baldwin struct pcib_window *w; 2530b377ff81SJohn Baldwin struct resource *pres; 2531b377ff81SJohn Baldwin rman_res_t length, start; 2532b377ff81SJohn Baldwin int error; 2533b377ff81SJohn Baldwin 2534582b84cdSJohn Baldwin w = pcib_get_resource_window(sc, r); 2535b377ff81SJohn Baldwin if (w == NULL) 2536d77f2092SJohn Baldwin return (bus_generic_map_resource(dev, child, r, argsp, map)); 2537b377ff81SJohn Baldwin 2538b377ff81SJohn Baldwin /* Resources must be active to be mapped. */ 2539b377ff81SJohn Baldwin if (!(rman_get_flags(r) & RF_ACTIVE)) 2540b377ff81SJohn Baldwin return (ENXIO); 2541b377ff81SJohn Baldwin 2542b377ff81SJohn Baldwin resource_init_map_request(&args); 2543b377ff81SJohn Baldwin error = resource_validate_map_request(r, argsp, &args, &start, &length); 2544b377ff81SJohn Baldwin if (error) 2545b377ff81SJohn Baldwin return (error); 2546b377ff81SJohn Baldwin 2547b377ff81SJohn Baldwin pres = pcib_find_parent_resource(w, r); 2548b377ff81SJohn Baldwin if (pres == NULL) 2549b377ff81SJohn Baldwin return (ENOENT); 2550b377ff81SJohn Baldwin 2551b377ff81SJohn Baldwin args.offset = start - rman_get_start(pres); 2552b377ff81SJohn Baldwin args.length = length; 2553d77f2092SJohn Baldwin return (bus_generic_map_resource(dev, child, pres, &args, map)); 2554b377ff81SJohn Baldwin } 2555b377ff81SJohn Baldwin 2556b377ff81SJohn Baldwin static int 2557d77f2092SJohn Baldwin pcib_unmap_resource(device_t dev, device_t child, struct resource *r, 2558b377ff81SJohn Baldwin struct resource_map *map) 2559b377ff81SJohn Baldwin { 2560b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 2561b377ff81SJohn Baldwin struct pcib_window *w; 2562b377ff81SJohn Baldwin 2563582b84cdSJohn Baldwin w = pcib_get_resource_window(sc, r); 2564b377ff81SJohn Baldwin if (w != NULL) { 2565b377ff81SJohn Baldwin r = pcib_find_parent_resource(w, r); 2566b377ff81SJohn Baldwin if (r == NULL) 2567b377ff81SJohn Baldwin return (ENOENT); 2568b377ff81SJohn Baldwin } 2569d77f2092SJohn Baldwin return (bus_generic_unmap_resource(dev, child, r, map)); 2570b377ff81SJohn Baldwin } 257183c41143SJohn Baldwin #else 2572bb0d0a8eSMike Smith /* 2573bb0d0a8eSMike Smith * We have to trap resource allocation requests and ensure that the bridge 2574bb0d0a8eSMike Smith * is set up to, or capable of handling them. 2575bb0d0a8eSMike Smith */ 257603719c65SJohn Baldwin static struct resource * 2577bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 25782dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 2579bb0d0a8eSMike Smith { 2580bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 258126043836SJohn Baldwin const char *name, *suffix; 2582a8b354a8SWarner Losh int ok; 2583bb0d0a8eSMike Smith 2584bb0d0a8eSMike Smith /* 2585bb0d0a8eSMike Smith * Fail the allocation for this range if it's not supported. 2586bb0d0a8eSMike Smith */ 258726043836SJohn Baldwin name = device_get_nameunit(child); 258826043836SJohn Baldwin if (name == NULL) { 258926043836SJohn Baldwin name = ""; 259026043836SJohn Baldwin suffix = ""; 259126043836SJohn Baldwin } else 259226043836SJohn Baldwin suffix = " "; 2593bb0d0a8eSMike Smith switch (type) { 2594bb0d0a8eSMike Smith case SYS_RES_IOPORT: 2595a8b354a8SWarner Losh ok = 0; 2596e4b59fc5SWarner Losh if (!pcib_is_io_open(sc)) 2597e4b59fc5SWarner Losh break; 2598a8b354a8SWarner Losh ok = (start >= sc->iobase && end <= sc->iolimit); 2599d98d9b12SMarcel Moolenaar 2600d98d9b12SMarcel Moolenaar /* 2601d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA I/O addresses when the 2602d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 2603d98d9b12SMarcel Moolenaar */ 2604d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_ioport_range(start, end)) 2605d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 2606d98d9b12SMarcel Moolenaar 2607e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 2608a8b354a8SWarner Losh if (!ok) { 260912b8c86eSWarner Losh if (start < sc->iobase) 261012b8c86eSWarner Losh start = sc->iobase; 261112b8c86eSWarner Losh if (end > sc->iolimit) 261212b8c86eSWarner Losh end = sc->iolimit; 26132daa7a07SWarner Losh if (start < end) 26142daa7a07SWarner Losh ok = 1; 2615a8b354a8SWarner Losh } 26161c54ff33SMatthew N. Dodd } else { 2617e4b59fc5SWarner Losh ok = 1; 26189dffe835SWarner Losh #if 0 2619795dceffSWarner Losh /* 2620795dceffSWarner Losh * If we overlap with the subtractive range, then 2621795dceffSWarner Losh * pick the upper range to use. 2622795dceffSWarner Losh */ 2623795dceffSWarner Losh if (start < sc->iolimit && end > sc->iobase) 2624795dceffSWarner Losh start = sc->iolimit + 1; 26259dffe835SWarner Losh #endif 262612b8c86eSWarner Losh } 2627a8b354a8SWarner Losh if (end < start) { 2628da1b038aSJustin Hibbits device_printf(dev, "ioport: end (%jx) < start (%jx)\n", 26292daa7a07SWarner Losh end, start); 2630a8b354a8SWarner Losh start = 0; 2631a8b354a8SWarner Losh end = 0; 2632a8b354a8SWarner Losh ok = 0; 2633a8b354a8SWarner Losh } 2634a8b354a8SWarner Losh if (!ok) { 263526043836SJohn Baldwin device_printf(dev, "%s%srequested unsupported I/O " 2636da1b038aSJustin Hibbits "range 0x%jx-0x%jx (decoding 0x%x-0x%x)\n", 263726043836SJohn Baldwin name, suffix, start, end, sc->iobase, sc->iolimit); 2638bb0d0a8eSMike Smith return (NULL); 2639bb0d0a8eSMike Smith } 26404fa59183SMike Smith if (bootverbose) 26412daa7a07SWarner Losh device_printf(dev, 2642da1b038aSJustin Hibbits "%s%srequested I/O range 0x%jx-0x%jx: in range\n", 264326043836SJohn Baldwin name, suffix, start, end); 2644bb0d0a8eSMike Smith break; 2645bb0d0a8eSMike Smith 2646bb0d0a8eSMike Smith case SYS_RES_MEMORY: 2647a8b354a8SWarner Losh ok = 0; 2648a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 2649a8b354a8SWarner Losh ok = ok || (start >= sc->membase && end <= sc->memlimit); 2650a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) 2651a8b354a8SWarner Losh ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit); 2652d98d9b12SMarcel Moolenaar 2653d98d9b12SMarcel Moolenaar /* 2654d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA memory addresses when the 2655d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 2656d98d9b12SMarcel Moolenaar */ 2657d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_memory_range(start, end)) 2658d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 2659d98d9b12SMarcel Moolenaar 2660e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 2661a8b354a8SWarner Losh if (!ok) { 2662a8b354a8SWarner Losh ok = 1; 2663a8b354a8SWarner Losh if (flags & RF_PREFETCHABLE) { 2664a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 2665a8b354a8SWarner Losh if (start < sc->pmembase) 2666a8b354a8SWarner Losh start = sc->pmembase; 2667a8b354a8SWarner Losh if (end > sc->pmemlimit) 2668a8b354a8SWarner Losh end = sc->pmemlimit; 2669a8b354a8SWarner Losh } else { 2670a8b354a8SWarner Losh ok = 0; 2671a8b354a8SWarner Losh } 2672a8b354a8SWarner Losh } else { /* non-prefetchable */ 2673a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 2674a8b354a8SWarner Losh if (start < sc->membase) 267512b8c86eSWarner Losh start = sc->membase; 267612b8c86eSWarner Losh if (end > sc->memlimit) 267712b8c86eSWarner Losh end = sc->memlimit; 26781c54ff33SMatthew N. Dodd } else { 2679a8b354a8SWarner Losh ok = 0; 2680a8b354a8SWarner Losh } 2681a8b354a8SWarner Losh } 2682a8b354a8SWarner Losh } 2683a8b354a8SWarner Losh } else if (!ok) { 2684e4b59fc5SWarner Losh ok = 1; /* subtractive bridge: always ok */ 26859dffe835SWarner Losh #if 0 2686a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 2687795dceffSWarner Losh if (start < sc->memlimit && end > sc->membase) 2688795dceffSWarner Losh start = sc->memlimit + 1; 2689a8b354a8SWarner Losh } 2690a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 2691795dceffSWarner Losh if (start < sc->pmemlimit && end > sc->pmembase) 2692795dceffSWarner Losh start = sc->pmemlimit + 1; 26931c54ff33SMatthew N. Dodd } 26949dffe835SWarner Losh #endif 269512b8c86eSWarner Losh } 2696a8b354a8SWarner Losh if (end < start) { 2697da1b038aSJustin Hibbits device_printf(dev, "memory: end (%jx) < start (%jx)\n", 26982daa7a07SWarner Losh end, start); 2699a8b354a8SWarner Losh start = 0; 2700a8b354a8SWarner Losh end = 0; 2701a8b354a8SWarner Losh ok = 0; 2702a8b354a8SWarner Losh } 2703a8b354a8SWarner Losh if (!ok && bootverbose) 270434428485SWarner Losh device_printf(dev, 2705da1b038aSJustin Hibbits "%s%srequested unsupported memory range %#jx-%#jx " 2706b0a2d4b8SWarner Losh "(decoding %#jx-%#jx, %#jx-%#jx)\n", 270726043836SJohn Baldwin name, suffix, start, end, 2708b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit, 2709b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 2710a8b354a8SWarner Losh if (!ok) 2711bb0d0a8eSMike Smith return (NULL); 27124fa59183SMike Smith if (bootverbose) 271326043836SJohn Baldwin device_printf(dev,"%s%srequested memory range " 2714da1b038aSJustin Hibbits "0x%jx-0x%jx: good\n", 271526043836SJohn Baldwin name, suffix, start, end); 27164fa59183SMike Smith break; 27174fa59183SMike Smith 2718bb0d0a8eSMike Smith default: 27194fa59183SMike Smith break; 2720bb0d0a8eSMike Smith } 2721bb0d0a8eSMike Smith /* 2722bb0d0a8eSMike Smith * Bridge is OK decoding this resource, so pass it up. 2723bb0d0a8eSMike Smith */ 27242daa7a07SWarner Losh return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 27252daa7a07SWarner Losh count, flags)); 2726bb0d0a8eSMike Smith } 272783c41143SJohn Baldwin #endif 2728bb0d0a8eSMike Smith 2729bb0d0a8eSMike Smith /* 273055d3ea17SRyan Stone * If ARI is enabled on this downstream port, translate the function number 273155d3ea17SRyan Stone * to the non-ARI slot/function. The downstream port will convert it back in 273255d3ea17SRyan Stone * hardware. If ARI is not enabled slot and func are not modified. 273355d3ea17SRyan Stone */ 273455d3ea17SRyan Stone static __inline void 273555d3ea17SRyan Stone pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func) 273655d3ea17SRyan Stone { 273755d3ea17SRyan Stone struct pcib_softc *sc; 273855d3ea17SRyan Stone int ari_func; 273955d3ea17SRyan Stone 274055d3ea17SRyan Stone sc = device_get_softc(pcib); 274155d3ea17SRyan Stone ari_func = *func; 274255d3ea17SRyan Stone 274355d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 274455d3ea17SRyan Stone KASSERT(*slot == 0, 274555d3ea17SRyan Stone ("Non-zero slot number with ARI enabled!")); 274655d3ea17SRyan Stone *slot = PCIE_ARI_SLOT(ari_func); 274755d3ea17SRyan Stone *func = PCIE_ARI_FUNC(ari_func); 274855d3ea17SRyan Stone } 274955d3ea17SRyan Stone } 275055d3ea17SRyan Stone 275155d3ea17SRyan Stone static void 275255d3ea17SRyan Stone pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos) 275355d3ea17SRyan Stone { 275455d3ea17SRyan Stone uint32_t ctl2; 275555d3ea17SRyan Stone 275655d3ea17SRyan Stone ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4); 275755d3ea17SRyan Stone ctl2 |= PCIEM_CTL2_ARI; 275855d3ea17SRyan Stone pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4); 275955d3ea17SRyan Stone 276055d3ea17SRyan Stone sc->flags |= PCIB_ENABLE_ARI; 276155d3ea17SRyan Stone } 276255d3ea17SRyan Stone 276355d3ea17SRyan Stone /* 2764bb0d0a8eSMike Smith * PCIB interface. 2765bb0d0a8eSMike Smith */ 27666f0d5884SJohn Baldwin int 2767bb0d0a8eSMike Smith pcib_maxslots(device_t dev) 2768bb0d0a8eSMike Smith { 27695502348dSJustin Hibbits #if !defined(__amd64__) && !defined(__i386__) 27708b92ad43SJustin Hibbits uint32_t pcie_pos; 27718b92ad43SJustin Hibbits uint16_t val; 27728b92ad43SJustin Hibbits 27738b92ad43SJustin Hibbits /* 27748b92ad43SJustin Hibbits * If this is a PCIe rootport or downstream switch port, there's only 27758b92ad43SJustin Hibbits * one slot permitted. 27768b92ad43SJustin Hibbits */ 27778b92ad43SJustin Hibbits if (pci_find_cap(dev, PCIY_EXPRESS, &pcie_pos) == 0) { 27788b92ad43SJustin Hibbits val = pci_read_config(dev, pcie_pos + PCIER_FLAGS, 2); 27798b92ad43SJustin Hibbits val &= PCIEM_FLAGS_TYPE; 27808b92ad43SJustin Hibbits if (val == PCIEM_TYPE_ROOT_PORT || 27818b92ad43SJustin Hibbits val == PCIEM_TYPE_DOWNSTREAM_PORT) 27828b92ad43SJustin Hibbits return (0); 27838b92ad43SJustin Hibbits } 27845502348dSJustin Hibbits #endif 27854fa59183SMike Smith return (PCI_SLOTMAX); 2786bb0d0a8eSMike Smith } 2787bb0d0a8eSMike Smith 278855d3ea17SRyan Stone static int 278955d3ea17SRyan Stone pcib_ari_maxslots(device_t dev) 279055d3ea17SRyan Stone { 279155d3ea17SRyan Stone struct pcib_softc *sc; 279255d3ea17SRyan Stone 279355d3ea17SRyan Stone sc = device_get_softc(dev); 279455d3ea17SRyan Stone 279555d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 279655d3ea17SRyan Stone return (PCIE_ARI_SLOTMAX); 279755d3ea17SRyan Stone else 27988b92ad43SJustin Hibbits return (pcib_maxslots(dev)); 279955d3ea17SRyan Stone } 280055d3ea17SRyan Stone 280155d3ea17SRyan Stone static int 280255d3ea17SRyan Stone pcib_ari_maxfuncs(device_t dev) 280355d3ea17SRyan Stone { 280455d3ea17SRyan Stone struct pcib_softc *sc; 280555d3ea17SRyan Stone 280655d3ea17SRyan Stone sc = device_get_softc(dev); 280755d3ea17SRyan Stone 280855d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 280955d3ea17SRyan Stone return (PCIE_ARI_FUNCMAX); 281055d3ea17SRyan Stone else 281155d3ea17SRyan Stone return (PCI_FUNCMAX); 281255d3ea17SRyan Stone } 281355d3ea17SRyan Stone 28142397d2d8SRyan Stone static void 28152397d2d8SRyan Stone pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot, 28162397d2d8SRyan Stone int *func) 28172397d2d8SRyan Stone { 28182397d2d8SRyan Stone struct pcib_softc *sc; 28192397d2d8SRyan Stone 28202397d2d8SRyan Stone sc = device_get_softc(pcib); 28212397d2d8SRyan Stone 28222397d2d8SRyan Stone *bus = PCI_RID2BUS(rid); 28232397d2d8SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 28242397d2d8SRyan Stone *slot = PCIE_ARI_RID2SLOT(rid); 28252397d2d8SRyan Stone *func = PCIE_ARI_RID2FUNC(rid); 28262397d2d8SRyan Stone } else { 28272397d2d8SRyan Stone *slot = PCI_RID2SLOT(rid); 28282397d2d8SRyan Stone *func = PCI_RID2FUNC(rid); 28292397d2d8SRyan Stone } 28302397d2d8SRyan Stone } 28312397d2d8SRyan Stone 2832bb0d0a8eSMike Smith /* 2833bb0d0a8eSMike Smith * Since we are a child of a PCI bus, its parent must support the pcib interface. 2834bb0d0a8eSMike Smith */ 283555d3ea17SRyan Stone static uint32_t 2836795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width) 2837bb0d0a8eSMike Smith { 283882cb5c3bSJohn Baldwin #ifdef PCI_HP 283982cb5c3bSJohn Baldwin struct pcib_softc *sc; 284055d3ea17SRyan Stone 284182cb5c3bSJohn Baldwin sc = device_get_softc(dev); 284282cb5c3bSJohn Baldwin if (!pcib_present(sc)) { 284382cb5c3bSJohn Baldwin switch (width) { 284482cb5c3bSJohn Baldwin case 2: 284582cb5c3bSJohn Baldwin return (0xffff); 284682cb5c3bSJohn Baldwin case 1: 284782cb5c3bSJohn Baldwin return (0xff); 284882cb5c3bSJohn Baldwin default: 284982cb5c3bSJohn Baldwin return (0xffffffff); 285082cb5c3bSJohn Baldwin } 285182cb5c3bSJohn Baldwin } 285282cb5c3bSJohn Baldwin #endif 285355d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 285455d3ea17SRyan Stone return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, 285555d3ea17SRyan Stone f, reg, width)); 2856bb0d0a8eSMike Smith } 2857bb0d0a8eSMike Smith 285855d3ea17SRyan Stone static void 2859795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width) 2860bb0d0a8eSMike Smith { 286182cb5c3bSJohn Baldwin #ifdef PCI_HP 286282cb5c3bSJohn Baldwin struct pcib_softc *sc; 286355d3ea17SRyan Stone 286482cb5c3bSJohn Baldwin sc = device_get_softc(dev); 286582cb5c3bSJohn Baldwin if (!pcib_present(sc)) 286682cb5c3bSJohn Baldwin return; 286782cb5c3bSJohn Baldwin #endif 286855d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 286955d3ea17SRyan Stone PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, 287055d3ea17SRyan Stone reg, val, width); 2871bb0d0a8eSMike Smith } 2872bb0d0a8eSMike Smith 2873bb0d0a8eSMike Smith /* 2874bb0d0a8eSMike Smith * Route an interrupt across a PCI bridge. 2875bb0d0a8eSMike Smith */ 28762c2d1d07SBenno Rice int 2877bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin) 2878bb0d0a8eSMike Smith { 2879bb0d0a8eSMike Smith device_t bus; 2880bb0d0a8eSMike Smith int parent_intpin; 2881bb0d0a8eSMike Smith int intnum; 2882bb0d0a8eSMike Smith 2883bb0d0a8eSMike Smith /* 2884bb0d0a8eSMike Smith * 2885bb0d0a8eSMike Smith * The PCI standard defines a swizzle of the child-side device/intpin to 2886bb0d0a8eSMike Smith * the parent-side intpin as follows. 2887bb0d0a8eSMike Smith * 2888bb0d0a8eSMike Smith * device = device on child bus 2889bb0d0a8eSMike Smith * child_intpin = intpin on child bus slot (0-3) 2890bb0d0a8eSMike Smith * parent_intpin = intpin on parent bus slot (0-3) 2891bb0d0a8eSMike Smith * 2892bb0d0a8eSMike Smith * parent_intpin = (device + child_intpin) % 4 2893bb0d0a8eSMike Smith */ 2894cdc95e1bSBernd Walter parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4; 2895bb0d0a8eSMike Smith 2896bb0d0a8eSMike Smith /* 2897bb0d0a8eSMike Smith * Our parent is a PCI bus. Its parent must export the pcib interface 2898bb0d0a8eSMike Smith * which includes the ability to route interrupts. 2899bb0d0a8eSMike Smith */ 2900bb0d0a8eSMike Smith bus = device_get_parent(pcib); 2901bb0d0a8eSMike Smith intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1); 290239981fedSJohn Baldwin if (PCI_INTERRUPT_VALID(intnum) && bootverbose) { 2903c6a121abSJohn Baldwin device_printf(pcib, "slot %d INT%c is routed to irq %d\n", 2904c6a121abSJohn Baldwin pci_get_slot(dev), 'A' + pin - 1, intnum); 29058046c4b9SMike Smith } 2906bb0d0a8eSMike Smith return(intnum); 2907bb0d0a8eSMike Smith } 2908b173edafSJohn Baldwin 2909e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */ 29109bf4c9c1SJohn Baldwin int 29119bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs) 29129bf4c9c1SJohn Baldwin { 2913bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 29149bf4c9c1SJohn Baldwin device_t bus; 29159bf4c9c1SJohn Baldwin 291622bf1c7fSJohn Baldwin if (sc->flags & PCIB_DISABLE_MSI) 291722bf1c7fSJohn Baldwin return (ENXIO); 29189bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 29199bf4c9c1SJohn Baldwin return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 29209bf4c9c1SJohn Baldwin irqs)); 29219bf4c9c1SJohn Baldwin } 29229bf4c9c1SJohn Baldwin 2923e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */ 29249bf4c9c1SJohn Baldwin int 29259bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs) 29269bf4c9c1SJohn Baldwin { 29279bf4c9c1SJohn Baldwin device_t bus; 29289bf4c9c1SJohn Baldwin 29299bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 29309bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs)); 29319bf4c9c1SJohn Baldwin } 29329bf4c9c1SJohn Baldwin 29339bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */ 29349bf4c9c1SJohn Baldwin int 2935e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq) 29369bf4c9c1SJohn Baldwin { 2937bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 29389bf4c9c1SJohn Baldwin device_t bus; 29399bf4c9c1SJohn Baldwin 294068e9cbd3SMarius Strobl if (sc->flags & PCIB_DISABLE_MSIX) 294122bf1c7fSJohn Baldwin return (ENXIO); 29429bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 2943e706f7f0SJohn Baldwin return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq)); 29445fe82bcaSJohn Baldwin } 29455fe82bcaSJohn Baldwin 29469bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */ 29479bf4c9c1SJohn Baldwin int 29489bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq) 29499bf4c9c1SJohn Baldwin { 29509bf4c9c1SJohn Baldwin device_t bus; 29519bf4c9c1SJohn Baldwin 29529bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 29539bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq)); 29549bf4c9c1SJohn Baldwin } 29559bf4c9c1SJohn Baldwin 2956e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */ 2957e706f7f0SJohn Baldwin int 2958e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, 2959e706f7f0SJohn Baldwin uint32_t *data) 2960e706f7f0SJohn Baldwin { 2961e706f7f0SJohn Baldwin device_t bus; 29624522ac77SLuoqi Chen int error; 2963e706f7f0SJohn Baldwin 2964e706f7f0SJohn Baldwin bus = device_get_parent(pcib); 29654522ac77SLuoqi Chen error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data); 29664522ac77SLuoqi Chen if (error) 29674522ac77SLuoqi Chen return (error); 29684522ac77SLuoqi Chen 29694522ac77SLuoqi Chen pci_ht_map_msi(pcib, *addr); 29704522ac77SLuoqi Chen return (0); 2971e706f7f0SJohn Baldwin } 2972e706f7f0SJohn Baldwin 297362508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */ 297462508c53SJohn Baldwin int 297562508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate) 297662508c53SJohn Baldwin { 297762508c53SJohn Baldwin device_t bus; 297862508c53SJohn Baldwin 297962508c53SJohn Baldwin bus = device_get_parent(pcib); 298062508c53SJohn Baldwin return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate)); 298162508c53SJohn Baldwin } 29825605a99eSRyan Stone 29832397d2d8SRyan Stone static int 29842397d2d8SRyan Stone pcib_ari_enabled(device_t pcib) 29852397d2d8SRyan Stone { 29862397d2d8SRyan Stone struct pcib_softc *sc; 29872397d2d8SRyan Stone 29882397d2d8SRyan Stone sc = device_get_softc(pcib); 29892397d2d8SRyan Stone 29902397d2d8SRyan Stone return ((sc->flags & PCIB_ENABLE_ARI) != 0); 29912397d2d8SRyan Stone } 29922397d2d8SRyan Stone 2993d7be980dSAndrew Turner static int 2994d7be980dSAndrew Turner pcib_ari_get_id(device_t pcib, device_t dev, enum pci_id_type type, 2995d7be980dSAndrew Turner uintptr_t *id) 299655d3ea17SRyan Stone { 299755d3ea17SRyan Stone struct pcib_softc *sc; 29981e43b18cSAndrew Turner device_t bus_dev; 299955d3ea17SRyan Stone uint8_t bus, slot, func; 300055d3ea17SRyan Stone 30011e43b18cSAndrew Turner if (type != PCI_ID_RID) { 30021e43b18cSAndrew Turner bus_dev = device_get_parent(pcib); 30031e43b18cSAndrew Turner return (PCIB_GET_ID(device_get_parent(bus_dev), dev, type, id)); 30041e43b18cSAndrew Turner } 3005d7be980dSAndrew Turner 300655d3ea17SRyan Stone sc = device_get_softc(pcib); 300755d3ea17SRyan Stone 300855d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 300955d3ea17SRyan Stone bus = pci_get_bus(dev); 301055d3ea17SRyan Stone func = pci_get_function(dev); 301155d3ea17SRyan Stone 3012d7be980dSAndrew Turner *id = (PCI_ARI_RID(bus, func)); 301355d3ea17SRyan Stone } else { 301455d3ea17SRyan Stone bus = pci_get_bus(dev); 301555d3ea17SRyan Stone slot = pci_get_slot(dev); 301655d3ea17SRyan Stone func = pci_get_function(dev); 301755d3ea17SRyan Stone 3018d7be980dSAndrew Turner *id = (PCI_RID(bus, slot, func)); 301955d3ea17SRyan Stone } 3020d7be980dSAndrew Turner 3021d7be980dSAndrew Turner return (0); 302255d3ea17SRyan Stone } 302355d3ea17SRyan Stone 302455d3ea17SRyan Stone /* 302555d3ea17SRyan Stone * Check that the downstream port (pcib) and the endpoint device (dev) both 302655d3ea17SRyan Stone * support ARI. If so, enable it and return 0, otherwise return an error. 302755d3ea17SRyan Stone */ 302855d3ea17SRyan Stone static int 302955d3ea17SRyan Stone pcib_try_enable_ari(device_t pcib, device_t dev) 303055d3ea17SRyan Stone { 303155d3ea17SRyan Stone struct pcib_softc *sc; 303255d3ea17SRyan Stone int error; 303355d3ea17SRyan Stone uint32_t cap2; 303455d3ea17SRyan Stone int ari_cap_off; 303555d3ea17SRyan Stone uint32_t ari_ver; 303655d3ea17SRyan Stone uint32_t pcie_pos; 303755d3ea17SRyan Stone 303855d3ea17SRyan Stone sc = device_get_softc(pcib); 303955d3ea17SRyan Stone 304055d3ea17SRyan Stone /* 304155d3ea17SRyan Stone * ARI is controlled in a register in the PCIe capability structure. 304255d3ea17SRyan Stone * If the downstream port does not have the PCIe capability structure 304355d3ea17SRyan Stone * then it does not support ARI. 304455d3ea17SRyan Stone */ 304555d3ea17SRyan Stone error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos); 304655d3ea17SRyan Stone if (error != 0) 304755d3ea17SRyan Stone return (ENODEV); 304855d3ea17SRyan Stone 304955d3ea17SRyan Stone /* Check that the PCIe port advertises ARI support. */ 305055d3ea17SRyan Stone cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4); 305155d3ea17SRyan Stone if (!(cap2 & PCIEM_CAP2_ARI)) 305255d3ea17SRyan Stone return (ENODEV); 305355d3ea17SRyan Stone 305455d3ea17SRyan Stone /* 305555d3ea17SRyan Stone * Check that the endpoint device advertises ARI support via the ARI 305655d3ea17SRyan Stone * extended capability structure. 305755d3ea17SRyan Stone */ 305855d3ea17SRyan Stone error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off); 305955d3ea17SRyan Stone if (error != 0) 306055d3ea17SRyan Stone return (ENODEV); 306155d3ea17SRyan Stone 306255d3ea17SRyan Stone /* 306355d3ea17SRyan Stone * Finally, check that the endpoint device supports the same version 306455d3ea17SRyan Stone * of ARI that we do. 306555d3ea17SRyan Stone */ 306655d3ea17SRyan Stone ari_ver = pci_read_config(dev, ari_cap_off, 4); 306755d3ea17SRyan Stone if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) { 306855d3ea17SRyan Stone if (bootverbose) 306955d3ea17SRyan Stone device_printf(pcib, 307055d3ea17SRyan Stone "Unsupported version of ARI (%d) detected\n", 307155d3ea17SRyan Stone PCI_EXTCAP_VER(ari_ver)); 307255d3ea17SRyan Stone 307355d3ea17SRyan Stone return (ENXIO); 307455d3ea17SRyan Stone } 307555d3ea17SRyan Stone 307655d3ea17SRyan Stone pcib_enable_ari(sc, pcie_pos); 307755d3ea17SRyan Stone 307855d3ea17SRyan Stone return (0); 307955d3ea17SRyan Stone } 30804cb67729SWarner Losh 308128586889SWarner Losh int 308228586889SWarner Losh pcib_request_feature_allow(device_t pcib, device_t dev, 308328586889SWarner Losh enum pci_feature feature) 308428586889SWarner Losh { 308528586889SWarner Losh /* 30865914c62eSGavin Atkinson * No host firmware we have to negotiate with, so we allow 308728586889SWarner Losh * every valid feature requested. 308828586889SWarner Losh */ 308928586889SWarner Losh switch (feature) { 309028586889SWarner Losh case PCI_FEATURE_AER: 309128586889SWarner Losh case PCI_FEATURE_HP: 309228586889SWarner Losh break; 309328586889SWarner Losh default: 309428586889SWarner Losh return (EINVAL); 309528586889SWarner Losh } 309628586889SWarner Losh 309728586889SWarner Losh return (0); 309828586889SWarner Losh } 309928586889SWarner Losh 31001ffd07bdSJohn Baldwin int 31011ffd07bdSJohn Baldwin pcib_request_feature(device_t dev, enum pci_feature feature) 31021ffd07bdSJohn Baldwin { 31031ffd07bdSJohn Baldwin 31041ffd07bdSJohn Baldwin /* 31051ffd07bdSJohn Baldwin * Invoke PCIB_REQUEST_FEATURE of this bridge first in case 31061ffd07bdSJohn Baldwin * the firmware overrides the method of PCI-PCI bridges. 31071ffd07bdSJohn Baldwin */ 31081ffd07bdSJohn Baldwin return (PCIB_REQUEST_FEATURE(dev, dev, feature)); 31091ffd07bdSJohn Baldwin } 31101ffd07bdSJohn Baldwin 31114cb67729SWarner Losh /* 31124cb67729SWarner Losh * Pass the request to use this PCI feature up the tree. Either there's a 31134cb67729SWarner Losh * firmware like ACPI that's using this feature that will approve (or deny) the 31144cb67729SWarner Losh * request to take it over, or the platform has no such firmware, in which case 31154cb67729SWarner Losh * the request will be approved. If the request is approved, the OS is expected 31164cb67729SWarner Losh * to make use of the feature or render it harmless. 31174cb67729SWarner Losh */ 31184cb67729SWarner Losh static int 31191ffd07bdSJohn Baldwin pcib_request_feature_default(device_t pcib, device_t dev, 31201ffd07bdSJohn Baldwin enum pci_feature feature) 31214cb67729SWarner Losh { 31224cb67729SWarner Losh device_t bus; 31234cb67729SWarner Losh 31244cb67729SWarner Losh /* 31254cb67729SWarner Losh * Our parent is necessarily a pci bus. Its parent will either be 31264cb67729SWarner Losh * another pci bridge (which passes it up) or a host bridge that can 31274cb67729SWarner Losh * approve or reject the request. 31284cb67729SWarner Losh */ 31294cb67729SWarner Losh bus = device_get_parent(pcib); 31304cb67729SWarner Losh return (PCIB_REQUEST_FEATURE(device_get_parent(bus), dev, feature)); 31314cb67729SWarner Losh } 31325db2a4a8SKonstantin Belousov 31335db2a4a8SKonstantin Belousov static int 31345db2a4a8SKonstantin Belousov pcib_reset_child(device_t dev, device_t child, int flags) 31355db2a4a8SKonstantin Belousov { 31365db2a4a8SKonstantin Belousov struct pci_devinfo *pdinfo; 31375db2a4a8SKonstantin Belousov int error; 31385db2a4a8SKonstantin Belousov 31395db2a4a8SKonstantin Belousov error = 0; 31405db2a4a8SKonstantin Belousov if (dev == NULL || device_get_parent(child) != dev) 31415db2a4a8SKonstantin Belousov goto out; 31425db2a4a8SKonstantin Belousov error = ENXIO; 31435db2a4a8SKonstantin Belousov if (device_get_devclass(child) != devclass_find("pci")) 31445db2a4a8SKonstantin Belousov goto out; 31455db2a4a8SKonstantin Belousov pdinfo = device_get_ivars(dev); 31465db2a4a8SKonstantin Belousov if (pdinfo->cfg.pcie.pcie_location != 0 && 31475db2a4a8SKonstantin Belousov (pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_DOWNSTREAM_PORT || 31485db2a4a8SKonstantin Belousov pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_ROOT_PORT)) { 31495db2a4a8SKonstantin Belousov error = bus_helper_reset_prepare(child, flags); 31505db2a4a8SKonstantin Belousov if (error == 0) { 31515db2a4a8SKonstantin Belousov error = pcie_link_reset(dev, 31525db2a4a8SKonstantin Belousov pdinfo->cfg.pcie.pcie_location); 31535db2a4a8SKonstantin Belousov /* XXXKIB call _post even if error != 0 ? */ 31545db2a4a8SKonstantin Belousov bus_helper_reset_post(child, flags); 31555db2a4a8SKonstantin Belousov } 31565db2a4a8SKonstantin Belousov } 31575db2a4a8SKonstantin Belousov out: 31585db2a4a8SKonstantin Belousov return (error); 31595db2a4a8SKonstantin Belousov } 3160