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> 34aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 35aad970f1SDavid E. O'Brien 36bb0d0a8eSMike Smith /* 37bb0d0a8eSMike Smith * PCI:PCI bridge support. 38bb0d0a8eSMike Smith */ 39bb0d0a8eSMike Smith 4082cb5c3bSJohn Baldwin #include "opt_pci.h" 4182cb5c3bSJohn Baldwin 42bb0d0a8eSMike Smith #include <sys/param.h> 43bb0d0a8eSMike Smith #include <sys/bus.h> 4483c41143SJohn Baldwin #include <sys/kernel.h> 45e2e050c8SConrad Meyer #include <sys/lock.h> 4683c41143SJohn Baldwin #include <sys/malloc.h> 4783c41143SJohn Baldwin #include <sys/module.h> 48e2e050c8SConrad Meyer #include <sys/mutex.h> 495db2a4a8SKonstantin Belousov #include <sys/pciio.h> 50a8b354a8SWarner Losh #include <sys/rman.h> 511c54ff33SMatthew N. Dodd #include <sys/sysctl.h> 5283c41143SJohn Baldwin #include <sys/systm.h> 5382cb5c3bSJohn Baldwin #include <sys/taskqueue.h> 54bb0d0a8eSMike Smith 5538d8c994SWarner Losh #include <dev/pci/pcivar.h> 5638d8c994SWarner Losh #include <dev/pci/pcireg.h> 5762508c53SJohn Baldwin #include <dev/pci/pci_private.h> 5838d8c994SWarner Losh #include <dev/pci/pcib_private.h> 59bb0d0a8eSMike Smith 60bb0d0a8eSMike Smith #include "pcib_if.h" 61bb0d0a8eSMike Smith 62bb0d0a8eSMike Smith static int pcib_probe(device_t dev); 63e36af292SJung-uk Kim static int pcib_suspend(device_t dev); 64e36af292SJung-uk Kim static int pcib_resume(device_t dev); 6562508c53SJohn Baldwin static int pcib_power_for_sleep(device_t pcib, device_t dev, 6662508c53SJohn Baldwin int *pstate); 67d7be980dSAndrew Turner static int pcib_ari_get_id(device_t pcib, device_t dev, 68d7be980dSAndrew Turner enum pci_id_type type, uintptr_t *id); 6955d3ea17SRyan Stone static uint32_t pcib_read_config(device_t dev, u_int b, u_int s, 7055d3ea17SRyan Stone u_int f, u_int reg, int width); 7155d3ea17SRyan Stone static void pcib_write_config(device_t dev, u_int b, u_int s, 7255d3ea17SRyan Stone u_int f, u_int reg, uint32_t val, int width); 7355d3ea17SRyan Stone static int pcib_ari_maxslots(device_t dev); 7455d3ea17SRyan Stone static int pcib_ari_maxfuncs(device_t dev); 7555d3ea17SRyan Stone static int pcib_try_enable_ari(device_t pcib, device_t dev); 762397d2d8SRyan Stone static int pcib_ari_enabled(device_t pcib); 772397d2d8SRyan Stone static void pcib_ari_decode_rid(device_t pcib, uint16_t rid, 782397d2d8SRyan Stone int *bus, int *slot, int *func); 7982cb5c3bSJohn Baldwin #ifdef PCI_HP 80fa3b03d3SAlexander Motin static void pcib_pcie_ab_timeout(void *arg, int pending); 81fa3b03d3SAlexander Motin static void pcib_pcie_cc_timeout(void *arg, int pending); 82fa3b03d3SAlexander Motin static void pcib_pcie_dll_timeout(void *arg, int pending); 8382cb5c3bSJohn Baldwin #endif 841ffd07bdSJohn Baldwin static int pcib_request_feature_default(device_t pcib, device_t dev, 854cb67729SWarner Losh enum pci_feature feature); 865db2a4a8SKonstantin Belousov static int pcib_reset_child(device_t dev, device_t child, int flags); 87bb0d0a8eSMike Smith 88bb0d0a8eSMike Smith static device_method_t pcib_methods[] = { 89bb0d0a8eSMike Smith /* Device interface */ 90bb0d0a8eSMike Smith DEVMETHOD(device_probe, pcib_probe), 91bb0d0a8eSMike Smith DEVMETHOD(device_attach, pcib_attach), 926f33eaa5SJohn Baldwin DEVMETHOD(device_detach, pcib_detach), 93bb0d0a8eSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 94e36af292SJung-uk Kim DEVMETHOD(device_suspend, pcib_suspend), 95e36af292SJung-uk Kim DEVMETHOD(device_resume, pcib_resume), 96bb0d0a8eSMike Smith 97bb0d0a8eSMike Smith /* Bus interface */ 9882cb5c3bSJohn Baldwin DEVMETHOD(bus_child_present, pcib_child_present), 99bb0d0a8eSMike Smith DEVMETHOD(bus_read_ivar, pcib_read_ivar), 100bb0d0a8eSMike Smith DEVMETHOD(bus_write_ivar, pcib_write_ivar), 101bb0d0a8eSMike Smith DEVMETHOD(bus_alloc_resource, pcib_alloc_resource), 10283c41143SJohn Baldwin #ifdef NEW_PCIB 10383c41143SJohn Baldwin DEVMETHOD(bus_adjust_resource, pcib_adjust_resource), 10483c41143SJohn Baldwin DEVMETHOD(bus_release_resource, pcib_release_resource), 10583c41143SJohn Baldwin #else 106d2c9344fSJohn Baldwin DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 107bb0d0a8eSMike Smith DEVMETHOD(bus_release_resource, bus_generic_release_resource), 10883c41143SJohn Baldwin #endif 109bb0d0a8eSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 110bb0d0a8eSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 111bb0d0a8eSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 112bb0d0a8eSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 1135db2a4a8SKonstantin Belousov DEVMETHOD(bus_reset_child, pcib_reset_child), 114bb0d0a8eSMike Smith 115bb0d0a8eSMike Smith /* pcib interface */ 11655d3ea17SRyan Stone DEVMETHOD(pcib_maxslots, pcib_ari_maxslots), 11755d3ea17SRyan Stone DEVMETHOD(pcib_maxfuncs, pcib_ari_maxfuncs), 118bb0d0a8eSMike Smith DEVMETHOD(pcib_read_config, pcib_read_config), 119bb0d0a8eSMike Smith DEVMETHOD(pcib_write_config, pcib_write_config), 120bb0d0a8eSMike Smith DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt), 1219bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi), 1229bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msi, pcib_release_msi), 1239bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix), 1249bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msix, pcib_release_msix), 125e706f7f0SJohn Baldwin DEVMETHOD(pcib_map_msi, pcib_map_msi), 12662508c53SJohn Baldwin DEVMETHOD(pcib_power_for_sleep, pcib_power_for_sleep), 127d7be980dSAndrew Turner DEVMETHOD(pcib_get_id, pcib_ari_get_id), 12855d3ea17SRyan Stone DEVMETHOD(pcib_try_enable_ari, pcib_try_enable_ari), 1292397d2d8SRyan Stone DEVMETHOD(pcib_ari_enabled, pcib_ari_enabled), 1302397d2d8SRyan Stone DEVMETHOD(pcib_decode_rid, pcib_ari_decode_rid), 1311ffd07bdSJohn Baldwin DEVMETHOD(pcib_request_feature, pcib_request_feature_default), 132bb0d0a8eSMike Smith 1334b7ec270SMarius Strobl DEVMETHOD_END 134bb0d0a8eSMike Smith }; 135bb0d0a8eSMike Smith 13604dda605SJohn Baldwin static devclass_t pcib_devclass; 137bb0d0a8eSMike Smith 13804dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc)); 139bfed756aSJustin Hibbits EARLY_DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, NULL, NULL, 140bfed756aSJustin Hibbits BUS_PASS_BUS); 141bb0d0a8eSMike Smith 1426ca2d094SBjoern A. Zeeb #if defined(NEW_PCIB) || defined(PCI_HP) 1430070c94bSJohn Baldwin SYSCTL_DECL(_hw_pci); 1446ca2d094SBjoern A. Zeeb #endif 1450070c94bSJohn Baldwin 1466ca2d094SBjoern A. Zeeb #ifdef NEW_PCIB 1470070c94bSJohn Baldwin static int pci_clear_pcib; 1480070c94bSJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0, 1490070c94bSJohn Baldwin "Clear firmware-assigned resources for PCI-PCI bridge I/O windows."); 15083c41143SJohn Baldwin 15183c41143SJohn Baldwin /* 15226245980SJessica Clarke * Get the corresponding window if this resource from a child device was 15326245980SJessica Clarke * sub-allocated from one of our window resource managers. 15426245980SJessica Clarke */ 15526245980SJessica Clarke static struct pcib_window * 15626245980SJessica Clarke pcib_get_resource_window(struct pcib_softc *sc, int type, struct resource *r) 15726245980SJessica Clarke { 15826245980SJessica Clarke switch (type) { 15926245980SJessica Clarke case SYS_RES_IOPORT: 16026245980SJessica Clarke if (rman_is_region_manager(r, &sc->io.rman)) 16126245980SJessica Clarke return (&sc->io); 16226245980SJessica Clarke break; 16326245980SJessica Clarke case SYS_RES_MEMORY: 16426245980SJessica Clarke /* Prefetchable resources may live in either memory rman. */ 16526245980SJessica Clarke if (rman_get_flags(r) & RF_PREFETCHABLE && 16626245980SJessica Clarke rman_is_region_manager(r, &sc->pmem.rman)) 16726245980SJessica Clarke return (&sc->pmem); 16826245980SJessica Clarke if (rman_is_region_manager(r, &sc->mem.rman)) 16926245980SJessica Clarke return (&sc->mem); 17026245980SJessica Clarke break; 17126245980SJessica Clarke } 17226245980SJessica Clarke return (NULL); 17326245980SJessica Clarke } 17426245980SJessica Clarke 17526245980SJessica Clarke /* 17683c41143SJohn Baldwin * Is a resource from a child device sub-allocated from one of our 17783c41143SJohn Baldwin * resource managers? 17883c41143SJohn Baldwin */ 17983c41143SJohn Baldwin static int 18083c41143SJohn Baldwin pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r) 18183c41143SJohn Baldwin { 18283c41143SJohn Baldwin 1834edef187SJohn Baldwin #ifdef PCI_RES_BUS 18426245980SJessica Clarke if (type == PCI_RES_BUS) 1854edef187SJohn Baldwin return (rman_is_region_manager(r, &sc->bus.rman)); 1864edef187SJohn Baldwin #endif 18726245980SJessica Clarke return (pcib_get_resource_window(sc, type, r) != NULL); 18883c41143SJohn Baldwin } 18983c41143SJohn Baldwin 19083c41143SJohn Baldwin static int 19183c41143SJohn Baldwin pcib_is_window_open(struct pcib_window *pw) 19283c41143SJohn Baldwin { 19383c41143SJohn Baldwin 19483c41143SJohn Baldwin return (pw->valid && pw->base < pw->limit); 19583c41143SJohn Baldwin } 19683c41143SJohn Baldwin 19783c41143SJohn Baldwin /* 19883c41143SJohn Baldwin * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and 19983c41143SJohn Baldwin * handle for the resource, we could pass RF_ACTIVE up to the PCI bus 20083c41143SJohn Baldwin * when allocating the resource windows and rely on the PCI bus driver 20183c41143SJohn Baldwin * to do this for us. 20283c41143SJohn Baldwin */ 20383c41143SJohn Baldwin static void 20483c41143SJohn Baldwin pcib_activate_window(struct pcib_softc *sc, int type) 20583c41143SJohn Baldwin { 20683c41143SJohn Baldwin 20783c41143SJohn Baldwin PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type); 20883c41143SJohn Baldwin } 20983c41143SJohn Baldwin 21083c41143SJohn Baldwin static void 21183c41143SJohn Baldwin pcib_write_windows(struct pcib_softc *sc, int mask) 21283c41143SJohn Baldwin { 21383c41143SJohn Baldwin device_t dev; 21483c41143SJohn Baldwin uint32_t val; 21583c41143SJohn Baldwin 21683c41143SJohn Baldwin dev = sc->dev; 21783c41143SJohn Baldwin if (sc->io.valid && mask & WIN_IO) { 21883c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 21983c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 22083c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEH_1, 22183c41143SJohn Baldwin sc->io.base >> 16, 2); 22283c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITH_1, 22383c41143SJohn Baldwin sc->io.limit >> 16, 2); 22483c41143SJohn Baldwin } 22583c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1); 22683c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1); 22783c41143SJohn Baldwin } 22883c41143SJohn Baldwin 22983c41143SJohn Baldwin if (mask & WIN_MEM) { 23083c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2); 23183c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2); 23283c41143SJohn Baldwin } 23383c41143SJohn Baldwin 23483c41143SJohn Baldwin if (sc->pmem.valid && mask & WIN_PMEM) { 23583c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 23683c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 23783c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEH_1, 23883c41143SJohn Baldwin sc->pmem.base >> 32, 4); 23983c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITH_1, 24083c41143SJohn Baldwin sc->pmem.limit >> 32, 4); 24183c41143SJohn Baldwin } 24283c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2); 24383c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2); 24483c41143SJohn Baldwin } 24583c41143SJohn Baldwin } 24683c41143SJohn Baldwin 247c825d4dcSJohn Baldwin /* 248c825d4dcSJohn Baldwin * This is used to reject I/O port allocations that conflict with an 249c825d4dcSJohn Baldwin * ISA alias range. 250c825d4dcSJohn Baldwin */ 251c825d4dcSJohn Baldwin static int 2522dd1bdf1SJustin Hibbits pcib_is_isa_range(struct pcib_softc *sc, rman_res_t start, rman_res_t end, 2532dd1bdf1SJustin Hibbits rman_res_t count) 254c825d4dcSJohn Baldwin { 2552dd1bdf1SJustin Hibbits rman_res_t next_alias; 256c825d4dcSJohn Baldwin 257c825d4dcSJohn Baldwin if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE)) 258c825d4dcSJohn Baldwin return (0); 259c825d4dcSJohn Baldwin 260c825d4dcSJohn Baldwin /* Only check fixed ranges for overlap. */ 261c825d4dcSJohn Baldwin if (start + count - 1 != end) 262c825d4dcSJohn Baldwin return (0); 263c825d4dcSJohn Baldwin 264c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */ 265c825d4dcSJohn Baldwin if (start >= 65536) 266c825d4dcSJohn Baldwin return (0); 267c825d4dcSJohn Baldwin 268c825d4dcSJohn Baldwin /* Check for overlap with 0x000 - 0x0ff as a special case. */ 269c825d4dcSJohn Baldwin if (start < 0x100) 270c825d4dcSJohn Baldwin goto alias; 271c825d4dcSJohn Baldwin 272c825d4dcSJohn Baldwin /* 273c825d4dcSJohn Baldwin * If the start address is an alias, the range is an alias. 274c825d4dcSJohn Baldwin * Otherwise, compute the start of the next alias range and 275c825d4dcSJohn Baldwin * check if it is before the end of the candidate range. 276c825d4dcSJohn Baldwin */ 277c825d4dcSJohn Baldwin if ((start & 0x300) != 0) 278c825d4dcSJohn Baldwin goto alias; 279c825d4dcSJohn Baldwin next_alias = (start & ~0x3fful) | 0x100; 280c825d4dcSJohn Baldwin if (next_alias <= end) 281c825d4dcSJohn Baldwin goto alias; 282c825d4dcSJohn Baldwin return (0); 283c825d4dcSJohn Baldwin 284c825d4dcSJohn Baldwin alias: 285c825d4dcSJohn Baldwin if (bootverbose) 286c825d4dcSJohn Baldwin device_printf(sc->dev, 287da1b038aSJustin Hibbits "I/O range %#jx-%#jx overlaps with an ISA alias\n", start, 288c825d4dcSJohn Baldwin end); 289c825d4dcSJohn Baldwin return (1); 290c825d4dcSJohn Baldwin } 291c825d4dcSJohn Baldwin 292c825d4dcSJohn Baldwin static void 293c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res, 294c825d4dcSJohn Baldwin int count) 295c825d4dcSJohn Baldwin { 296c825d4dcSJohn Baldwin struct resource **newarray; 297c825d4dcSJohn Baldwin int error, i; 298c825d4dcSJohn Baldwin 299c825d4dcSJohn Baldwin newarray = malloc(sizeof(struct resource *) * (w->count + count), 300c825d4dcSJohn Baldwin M_DEVBUF, M_WAITOK); 301c825d4dcSJohn Baldwin if (w->res != NULL) 302c825d4dcSJohn Baldwin bcopy(w->res, newarray, sizeof(struct resource *) * w->count); 303c825d4dcSJohn Baldwin bcopy(res, newarray + w->count, sizeof(struct resource *) * count); 304c825d4dcSJohn Baldwin free(w->res, M_DEVBUF); 305c825d4dcSJohn Baldwin w->res = newarray; 306c825d4dcSJohn Baldwin w->count += count; 307c825d4dcSJohn Baldwin 308c825d4dcSJohn Baldwin for (i = 0; i < count; i++) { 309c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, rman_get_start(res[i]), 310c825d4dcSJohn Baldwin rman_get_end(res[i])); 311c825d4dcSJohn Baldwin if (error) 312c825d4dcSJohn Baldwin panic("Failed to add resource to rman"); 313c825d4dcSJohn Baldwin } 314c825d4dcSJohn Baldwin } 315c825d4dcSJohn Baldwin 3162dd1bdf1SJustin Hibbits typedef void (nonisa_callback)(rman_res_t start, rman_res_t end, void *arg); 317c825d4dcSJohn Baldwin 318c825d4dcSJohn Baldwin static void 3192dd1bdf1SJustin Hibbits pcib_walk_nonisa_ranges(rman_res_t start, rman_res_t end, nonisa_callback *cb, 320c825d4dcSJohn Baldwin void *arg) 321c825d4dcSJohn Baldwin { 3222dd1bdf1SJustin Hibbits rman_res_t next_end; 323c825d4dcSJohn Baldwin 324c825d4dcSJohn Baldwin /* 325c825d4dcSJohn Baldwin * If start is within an ISA alias range, move up to the start 326c825d4dcSJohn Baldwin * of the next non-alias range. As a special case, addresses 327c825d4dcSJohn Baldwin * in the range 0x000 - 0x0ff should also be skipped since 328c825d4dcSJohn Baldwin * those are used for various system I/O devices in ISA 329c825d4dcSJohn Baldwin * systems. 330c825d4dcSJohn Baldwin */ 331c825d4dcSJohn Baldwin if (start <= 65535) { 332c825d4dcSJohn Baldwin if (start < 0x100 || (start & 0x300) != 0) { 333c825d4dcSJohn Baldwin start &= ~0x3ff; 334c825d4dcSJohn Baldwin start += 0x400; 335c825d4dcSJohn Baldwin } 336c825d4dcSJohn Baldwin } 337c825d4dcSJohn Baldwin 338c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */ 339c825d4dcSJohn Baldwin while (start <= MIN(end, 65535)) { 340c825d4dcSJohn Baldwin next_end = MIN(start | 0xff, end); 341c825d4dcSJohn Baldwin cb(start, next_end, arg); 342c825d4dcSJohn Baldwin start += 0x400; 343c825d4dcSJohn Baldwin } 344c825d4dcSJohn Baldwin 345c825d4dcSJohn Baldwin if (start <= end) 346c825d4dcSJohn Baldwin cb(start, end, arg); 347c825d4dcSJohn Baldwin } 348c825d4dcSJohn Baldwin 349c825d4dcSJohn Baldwin static void 3502dd1bdf1SJustin Hibbits count_ranges(rman_res_t start, rman_res_t end, void *arg) 351c825d4dcSJohn Baldwin { 352c825d4dcSJohn Baldwin int *countp; 353c825d4dcSJohn Baldwin 354c825d4dcSJohn Baldwin countp = arg; 355c825d4dcSJohn Baldwin (*countp)++; 356c825d4dcSJohn Baldwin } 357c825d4dcSJohn Baldwin 358c825d4dcSJohn Baldwin struct alloc_state { 359c825d4dcSJohn Baldwin struct resource **res; 360c825d4dcSJohn Baldwin struct pcib_softc *sc; 361c825d4dcSJohn Baldwin int count, error; 362c825d4dcSJohn Baldwin }; 363c825d4dcSJohn Baldwin 364c825d4dcSJohn Baldwin static void 3652dd1bdf1SJustin Hibbits alloc_ranges(rman_res_t start, rman_res_t end, void *arg) 366c825d4dcSJohn Baldwin { 367c825d4dcSJohn Baldwin struct alloc_state *as; 368c825d4dcSJohn Baldwin struct pcib_window *w; 369c825d4dcSJohn Baldwin int rid; 370c825d4dcSJohn Baldwin 371c825d4dcSJohn Baldwin as = arg; 372c825d4dcSJohn Baldwin if (as->error != 0) 373c825d4dcSJohn Baldwin return; 374c825d4dcSJohn Baldwin 375c825d4dcSJohn Baldwin w = &as->sc->io; 376c825d4dcSJohn Baldwin rid = w->reg; 377c825d4dcSJohn Baldwin if (bootverbose) 378c825d4dcSJohn Baldwin device_printf(as->sc->dev, 379da1b038aSJustin Hibbits "allocating non-ISA range %#jx-%#jx\n", start, end); 380c825d4dcSJohn Baldwin as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT, 381c825d4dcSJohn Baldwin &rid, start, end, end - start + 1, 0); 382c825d4dcSJohn Baldwin if (as->res[as->count] == NULL) 383c825d4dcSJohn Baldwin as->error = ENXIO; 384c825d4dcSJohn Baldwin else 385c825d4dcSJohn Baldwin as->count++; 386c825d4dcSJohn Baldwin } 387c825d4dcSJohn Baldwin 388c825d4dcSJohn Baldwin static int 3892dd1bdf1SJustin Hibbits pcib_alloc_nonisa_ranges(struct pcib_softc *sc, rman_res_t start, rman_res_t end) 390c825d4dcSJohn Baldwin { 391c825d4dcSJohn Baldwin struct alloc_state as; 392c825d4dcSJohn Baldwin int i, new_count; 393c825d4dcSJohn Baldwin 394c825d4dcSJohn Baldwin /* First, see how many ranges we need. */ 395c825d4dcSJohn Baldwin new_count = 0; 396c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count); 397c825d4dcSJohn Baldwin 398c825d4dcSJohn Baldwin /* Second, allocate the ranges. */ 399c825d4dcSJohn Baldwin as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF, 400c825d4dcSJohn Baldwin M_WAITOK); 401c825d4dcSJohn Baldwin as.sc = sc; 402c825d4dcSJohn Baldwin as.count = 0; 403c825d4dcSJohn Baldwin as.error = 0; 404c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as); 405c825d4dcSJohn Baldwin if (as.error != 0) { 406c825d4dcSJohn Baldwin for (i = 0; i < as.count; i++) 407c825d4dcSJohn Baldwin bus_release_resource(sc->dev, SYS_RES_IOPORT, 408c825d4dcSJohn Baldwin sc->io.reg, as.res[i]); 409c825d4dcSJohn Baldwin free(as.res, M_DEVBUF); 410c825d4dcSJohn Baldwin return (as.error); 411c825d4dcSJohn Baldwin } 412c825d4dcSJohn Baldwin KASSERT(as.count == new_count, ("%s: count mismatch", __func__)); 413c825d4dcSJohn Baldwin 414c825d4dcSJohn Baldwin /* Third, add the ranges to the window. */ 415c825d4dcSJohn Baldwin pcib_add_window_resources(&sc->io, as.res, as.count); 416c825d4dcSJohn Baldwin free(as.res, M_DEVBUF); 417c825d4dcSJohn Baldwin return (0); 418c825d4dcSJohn Baldwin } 419c825d4dcSJohn Baldwin 42083c41143SJohn Baldwin static void 42183c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type, 42283c41143SJohn Baldwin int flags, pci_addr_t max_address) 42383c41143SJohn Baldwin { 424c825d4dcSJohn Baldwin struct resource *res; 42583c41143SJohn Baldwin char buf[64]; 42683c41143SJohn Baldwin int error, rid; 42783c41143SJohn Baldwin 42889977ce2SJustin Hibbits if (max_address != (rman_res_t)max_address) 429534ccd7bSJustin Hibbits max_address = ~0; 43083c41143SJohn Baldwin w->rman.rm_start = 0; 43183c41143SJohn Baldwin w->rman.rm_end = max_address; 43283c41143SJohn Baldwin w->rman.rm_type = RMAN_ARRAY; 43383c41143SJohn Baldwin snprintf(buf, sizeof(buf), "%s %s window", 43483c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name); 43583c41143SJohn Baldwin w->rman.rm_descr = strdup(buf, M_DEVBUF); 43683c41143SJohn Baldwin error = rman_init(&w->rman); 43783c41143SJohn Baldwin if (error) 43883c41143SJohn Baldwin panic("Failed to initialize %s %s rman", 43983c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name); 44083c41143SJohn Baldwin 44183c41143SJohn Baldwin if (!pcib_is_window_open(w)) 44283c41143SJohn Baldwin return; 44383c41143SJohn Baldwin 44483c41143SJohn Baldwin if (w->base > max_address || w->limit > max_address) { 44583c41143SJohn Baldwin device_printf(sc->dev, 44683c41143SJohn Baldwin "initial %s window has too many bits, ignoring\n", w->name); 44783c41143SJohn Baldwin return; 44883c41143SJohn Baldwin } 449c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE) 450c825d4dcSJohn Baldwin (void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit); 451c825d4dcSJohn Baldwin else { 45283c41143SJohn Baldwin rid = w->reg; 453c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit, 45483c41143SJohn Baldwin w->limit - w->base + 1, flags); 455c825d4dcSJohn Baldwin if (res != NULL) 456c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 457c825d4dcSJohn Baldwin } 45883c41143SJohn Baldwin if (w->res == NULL) { 45983c41143SJohn Baldwin device_printf(sc->dev, 46083c41143SJohn Baldwin "failed to allocate initial %s window: %#jx-%#jx\n", 46183c41143SJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 46283c41143SJohn Baldwin w->base = max_address; 46383c41143SJohn Baldwin w->limit = 0; 46483c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 46583c41143SJohn Baldwin return; 46683c41143SJohn Baldwin } 46783c41143SJohn Baldwin pcib_activate_window(sc, type); 46883c41143SJohn Baldwin } 46983c41143SJohn Baldwin 47083c41143SJohn Baldwin /* 47183c41143SJohn Baldwin * Initialize I/O windows. 47283c41143SJohn Baldwin */ 47383c41143SJohn Baldwin static void 47483c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc) 47583c41143SJohn Baldwin { 47683c41143SJohn Baldwin pci_addr_t max; 47783c41143SJohn Baldwin device_t dev; 47883c41143SJohn Baldwin uint32_t val; 47983c41143SJohn Baldwin 48083c41143SJohn Baldwin dev = sc->dev; 48183c41143SJohn Baldwin 4820070c94bSJohn Baldwin if (pci_clear_pcib) { 483809923caSJustin Hibbits pcib_bridge_init(dev); 4840070c94bSJohn Baldwin } 4850070c94bSJohn Baldwin 48683c41143SJohn Baldwin /* Determine if the I/O port window is implemented. */ 48783c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 48883c41143SJohn Baldwin if (val == 0) { 48983c41143SJohn Baldwin /* 49083c41143SJohn Baldwin * If 'val' is zero, then only 16-bits of I/O space 49183c41143SJohn Baldwin * are supported. 49283c41143SJohn Baldwin */ 49383c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 49483c41143SJohn Baldwin if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) { 49583c41143SJohn Baldwin sc->io.valid = 1; 49683c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0, 1); 49783c41143SJohn Baldwin } 49883c41143SJohn Baldwin } else 49983c41143SJohn Baldwin sc->io.valid = 1; 50083c41143SJohn Baldwin 50183c41143SJohn Baldwin /* Read the existing I/O port window. */ 50283c41143SJohn Baldwin if (sc->io.valid) { 50383c41143SJohn Baldwin sc->io.reg = PCIR_IOBASEL_1; 50483c41143SJohn Baldwin sc->io.step = 12; 50583c41143SJohn Baldwin sc->io.mask = WIN_IO; 50683c41143SJohn Baldwin sc->io.name = "I/O port"; 50783c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 50883c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE( 50983c41143SJohn Baldwin pci_read_config(dev, PCIR_IOBASEH_1, 2), val); 51083c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT( 51183c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITH_1, 2), 51283c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 51383c41143SJohn Baldwin max = 0xffffffff; 51483c41143SJohn Baldwin } else { 51583c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE(0, val); 51683c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT(0, 51783c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 51883c41143SJohn Baldwin max = 0xffff; 51983c41143SJohn Baldwin } 52083c41143SJohn Baldwin pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max); 52183c41143SJohn Baldwin } 52283c41143SJohn Baldwin 52383c41143SJohn Baldwin /* Read the existing memory window. */ 52483c41143SJohn Baldwin sc->mem.valid = 1; 52583c41143SJohn Baldwin sc->mem.reg = PCIR_MEMBASE_1; 52683c41143SJohn Baldwin sc->mem.step = 20; 52783c41143SJohn Baldwin sc->mem.mask = WIN_MEM; 52883c41143SJohn Baldwin sc->mem.name = "memory"; 52983c41143SJohn Baldwin sc->mem.base = PCI_PPBMEMBASE(0, 53083c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMBASE_1, 2)); 53183c41143SJohn Baldwin sc->mem.limit = PCI_PPBMEMLIMIT(0, 53283c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 53383c41143SJohn Baldwin pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff); 53483c41143SJohn Baldwin 53583c41143SJohn Baldwin /* Determine if the prefetchable memory window is implemented. */ 53683c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 53783c41143SJohn Baldwin if (val == 0) { 53883c41143SJohn Baldwin /* 53983c41143SJohn Baldwin * If 'val' is zero, then only 32-bits of memory space 54083c41143SJohn Baldwin * are supported. 54183c41143SJohn Baldwin */ 54283c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 54383c41143SJohn Baldwin if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) { 54483c41143SJohn Baldwin sc->pmem.valid = 1; 54583c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0, 2); 54683c41143SJohn Baldwin } 54783c41143SJohn Baldwin } else 54883c41143SJohn Baldwin sc->pmem.valid = 1; 54983c41143SJohn Baldwin 55083c41143SJohn Baldwin /* Read the existing prefetchable memory window. */ 55183c41143SJohn Baldwin if (sc->pmem.valid) { 55283c41143SJohn Baldwin sc->pmem.reg = PCIR_PMBASEL_1; 55383c41143SJohn Baldwin sc->pmem.step = 20; 55483c41143SJohn Baldwin sc->pmem.mask = WIN_PMEM; 55583c41143SJohn Baldwin sc->pmem.name = "prefetch"; 55683c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 55783c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE( 55883c41143SJohn Baldwin pci_read_config(dev, PCIR_PMBASEH_1, 4), val); 55983c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT( 56083c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITH_1, 4), 56183c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 56283c41143SJohn Baldwin max = 0xffffffffffffffff; 56383c41143SJohn Baldwin } else { 56483c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE(0, val); 56583c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT(0, 56683c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 56783c41143SJohn Baldwin max = 0xffffffff; 56883c41143SJohn Baldwin } 56983c41143SJohn Baldwin pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY, 57083c41143SJohn Baldwin RF_PREFETCHABLE, max); 57183c41143SJohn Baldwin } 57283c41143SJohn Baldwin } 57383c41143SJohn Baldwin 5746f33eaa5SJohn Baldwin static void 5756f33eaa5SJohn Baldwin pcib_release_window(struct pcib_softc *sc, struct pcib_window *w, int type) 5766f33eaa5SJohn Baldwin { 5776f33eaa5SJohn Baldwin device_t dev; 5786f33eaa5SJohn Baldwin int error, i; 5796f33eaa5SJohn Baldwin 5806f33eaa5SJohn Baldwin if (!w->valid) 5816f33eaa5SJohn Baldwin return; 5826f33eaa5SJohn Baldwin 5836f33eaa5SJohn Baldwin dev = sc->dev; 5846f33eaa5SJohn Baldwin error = rman_fini(&w->rman); 5856f33eaa5SJohn Baldwin if (error) { 5866f33eaa5SJohn Baldwin device_printf(dev, "failed to release %s rman\n", w->name); 5876f33eaa5SJohn Baldwin return; 5886f33eaa5SJohn Baldwin } 5896f33eaa5SJohn Baldwin free(__DECONST(char *, w->rman.rm_descr), M_DEVBUF); 5906f33eaa5SJohn Baldwin 5916f33eaa5SJohn Baldwin for (i = 0; i < w->count; i++) { 5926f33eaa5SJohn Baldwin error = bus_free_resource(dev, type, w->res[i]); 5936f33eaa5SJohn Baldwin if (error) 5946f33eaa5SJohn Baldwin device_printf(dev, 5956f33eaa5SJohn Baldwin "failed to release %s resource: %d\n", w->name, 5966f33eaa5SJohn Baldwin error); 5976f33eaa5SJohn Baldwin } 5986f33eaa5SJohn Baldwin free(w->res, M_DEVBUF); 5996f33eaa5SJohn Baldwin } 6006f33eaa5SJohn Baldwin 6016f33eaa5SJohn Baldwin static void 6026f33eaa5SJohn Baldwin pcib_free_windows(struct pcib_softc *sc) 6036f33eaa5SJohn Baldwin { 6046f33eaa5SJohn Baldwin 6056f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->pmem, SYS_RES_MEMORY); 6066f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->mem, SYS_RES_MEMORY); 6076f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->io, SYS_RES_IOPORT); 6086f33eaa5SJohn Baldwin } 6096f33eaa5SJohn Baldwin 6104edef187SJohn Baldwin #ifdef PCI_RES_BUS 6114edef187SJohn Baldwin /* 6124edef187SJohn Baldwin * Allocate a suitable secondary bus for this bridge if needed and 6134edef187SJohn Baldwin * initialize the resource manager for the secondary bus range. Note 6144edef187SJohn Baldwin * that the minimum count is a desired value and this may allocate a 6154edef187SJohn Baldwin * smaller range. 6164edef187SJohn Baldwin */ 6174edef187SJohn Baldwin void 6184edef187SJohn Baldwin pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count) 6194edef187SJohn Baldwin { 6204edef187SJohn Baldwin char buf[64]; 621ad6f36f8SJohn Baldwin int error, rid, sec_reg; 6224edef187SJohn Baldwin 6234edef187SJohn Baldwin switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) { 6244edef187SJohn Baldwin case PCIM_HDRTYPE_BRIDGE: 625ad6f36f8SJohn Baldwin sec_reg = PCIR_SECBUS_1; 6264edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_1; 6274edef187SJohn Baldwin break; 6284edef187SJohn Baldwin case PCIM_HDRTYPE_CARDBUS: 629ad6f36f8SJohn Baldwin sec_reg = PCIR_SECBUS_2; 6304edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_2; 6314edef187SJohn Baldwin break; 6324edef187SJohn Baldwin default: 6334edef187SJohn Baldwin panic("not a PCI bridge"); 6344edef187SJohn Baldwin } 635ad6f36f8SJohn Baldwin bus->sec = pci_read_config(dev, sec_reg, 1); 636ad6f36f8SJohn Baldwin bus->sub = pci_read_config(dev, bus->sub_reg, 1); 6374edef187SJohn Baldwin bus->dev = dev; 6384edef187SJohn Baldwin bus->rman.rm_start = 0; 6394edef187SJohn Baldwin bus->rman.rm_end = PCI_BUSMAX; 6404edef187SJohn Baldwin bus->rman.rm_type = RMAN_ARRAY; 6414edef187SJohn Baldwin snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev)); 6424edef187SJohn Baldwin bus->rman.rm_descr = strdup(buf, M_DEVBUF); 6434edef187SJohn Baldwin error = rman_init(&bus->rman); 6444edef187SJohn Baldwin if (error) 6454edef187SJohn Baldwin panic("Failed to initialize %s bus number rman", 6464edef187SJohn Baldwin device_get_nameunit(dev)); 6474edef187SJohn Baldwin 6484edef187SJohn Baldwin /* 6494edef187SJohn Baldwin * Allocate a bus range. This will return an existing bus range 6504edef187SJohn Baldwin * if one exists, or a new bus range if one does not. 6514edef187SJohn Baldwin */ 6524edef187SJohn Baldwin rid = 0; 653c47476d7SJustin Hibbits bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid, 6544edef187SJohn Baldwin min_count, 0); 6554edef187SJohn Baldwin if (bus->res == NULL) { 6564edef187SJohn Baldwin /* 6574edef187SJohn Baldwin * Fall back to just allocating a range of a single bus 6584edef187SJohn Baldwin * number. 6594edef187SJohn Baldwin */ 660c47476d7SJustin Hibbits bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid, 6614edef187SJohn Baldwin 1, 0); 6624edef187SJohn Baldwin } else if (rman_get_size(bus->res) < min_count) 6634edef187SJohn Baldwin /* 6644edef187SJohn Baldwin * Attempt to grow the existing range to satisfy the 6654edef187SJohn Baldwin * minimum desired count. 6664edef187SJohn Baldwin */ 6674edef187SJohn Baldwin (void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res, 6684edef187SJohn Baldwin rman_get_start(bus->res), rman_get_start(bus->res) + 6694edef187SJohn Baldwin min_count - 1); 6704edef187SJohn Baldwin 6714edef187SJohn Baldwin /* 6724edef187SJohn Baldwin * Add the initial resource to the rman. 6734edef187SJohn Baldwin */ 6744edef187SJohn Baldwin if (bus->res != NULL) { 6754edef187SJohn Baldwin error = rman_manage_region(&bus->rman, rman_get_start(bus->res), 6764edef187SJohn Baldwin rman_get_end(bus->res)); 6774edef187SJohn Baldwin if (error) 6784edef187SJohn Baldwin panic("Failed to add resource to rman"); 6794edef187SJohn Baldwin bus->sec = rman_get_start(bus->res); 6804edef187SJohn Baldwin bus->sub = rman_get_end(bus->res); 6814edef187SJohn Baldwin } 6824edef187SJohn Baldwin } 6834edef187SJohn Baldwin 6846f33eaa5SJohn Baldwin void 6856f33eaa5SJohn Baldwin pcib_free_secbus(device_t dev, struct pcib_secbus *bus) 6866f33eaa5SJohn Baldwin { 6876f33eaa5SJohn Baldwin int error; 6886f33eaa5SJohn Baldwin 6896f33eaa5SJohn Baldwin error = rman_fini(&bus->rman); 6906f33eaa5SJohn Baldwin if (error) { 6916f33eaa5SJohn Baldwin device_printf(dev, "failed to release bus number rman\n"); 6926f33eaa5SJohn Baldwin return; 6936f33eaa5SJohn Baldwin } 6946f33eaa5SJohn Baldwin free(__DECONST(char *, bus->rman.rm_descr), M_DEVBUF); 6956f33eaa5SJohn Baldwin 6966f33eaa5SJohn Baldwin error = bus_free_resource(dev, PCI_RES_BUS, bus->res); 6976f33eaa5SJohn Baldwin if (error) 6986f33eaa5SJohn Baldwin device_printf(dev, 6996f33eaa5SJohn Baldwin "failed to release bus numbers resource: %d\n", error); 7006f33eaa5SJohn Baldwin } 7016f33eaa5SJohn Baldwin 7024edef187SJohn Baldwin static struct resource * 7034edef187SJohn Baldwin pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid, 7042dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 7054edef187SJohn Baldwin { 7064edef187SJohn Baldwin struct resource *res; 7074edef187SJohn Baldwin 7084edef187SJohn Baldwin res = rman_reserve_resource(&bus->rman, start, end, count, flags, 7094edef187SJohn Baldwin child); 7104edef187SJohn Baldwin if (res == NULL) 7114edef187SJohn Baldwin return (NULL); 7124edef187SJohn Baldwin 7134edef187SJohn Baldwin if (bootverbose) 7144edef187SJohn Baldwin device_printf(bus->dev, 715da1b038aSJustin Hibbits "allocated bus range (%ju-%ju) for rid %d of %s\n", 7164edef187SJohn Baldwin rman_get_start(res), rman_get_end(res), *rid, 7174edef187SJohn Baldwin pcib_child_name(child)); 7184edef187SJohn Baldwin rman_set_rid(res, *rid); 7194edef187SJohn Baldwin return (res); 7204edef187SJohn Baldwin } 7214edef187SJohn Baldwin 7224edef187SJohn Baldwin /* 7234edef187SJohn Baldwin * Attempt to grow the secondary bus range. This is much simpler than 7244edef187SJohn Baldwin * for I/O windows as the range can only be grown by increasing 7254edef187SJohn Baldwin * subbus. 7264edef187SJohn Baldwin */ 7274edef187SJohn Baldwin static int 7282dd1bdf1SJustin Hibbits pcib_grow_subbus(struct pcib_secbus *bus, rman_res_t new_end) 7294edef187SJohn Baldwin { 7302dd1bdf1SJustin Hibbits rman_res_t old_end; 7314edef187SJohn Baldwin int error; 7324edef187SJohn Baldwin 7334edef187SJohn Baldwin old_end = rman_get_end(bus->res); 7344edef187SJohn Baldwin KASSERT(new_end > old_end, ("attempt to shrink subbus")); 7354edef187SJohn Baldwin error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res, 7364edef187SJohn Baldwin rman_get_start(bus->res), new_end); 7374edef187SJohn Baldwin if (error) 7384edef187SJohn Baldwin return (error); 7394edef187SJohn Baldwin if (bootverbose) 740da1b038aSJustin Hibbits device_printf(bus->dev, "grew bus range to %ju-%ju\n", 7414edef187SJohn Baldwin rman_get_start(bus->res), rman_get_end(bus->res)); 7424edef187SJohn Baldwin error = rman_manage_region(&bus->rman, old_end + 1, 7434edef187SJohn Baldwin rman_get_end(bus->res)); 7444edef187SJohn Baldwin if (error) 7454edef187SJohn Baldwin panic("Failed to add resource to rman"); 7464edef187SJohn Baldwin bus->sub = rman_get_end(bus->res); 7474edef187SJohn Baldwin pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1); 7484edef187SJohn Baldwin return (0); 7494edef187SJohn Baldwin } 7504edef187SJohn Baldwin 7514edef187SJohn Baldwin struct resource * 7524edef187SJohn Baldwin pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid, 7532dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 7544edef187SJohn Baldwin { 7554edef187SJohn Baldwin struct resource *res; 7562dd1bdf1SJustin Hibbits rman_res_t start_free, end_free, new_end; 7574edef187SJohn Baldwin 7584edef187SJohn Baldwin /* 7594edef187SJohn Baldwin * First, see if the request can be satisified by the existing 7604edef187SJohn Baldwin * bus range. 7614edef187SJohn Baldwin */ 7624edef187SJohn Baldwin res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags); 7634edef187SJohn Baldwin if (res != NULL) 7644edef187SJohn Baldwin return (res); 7654edef187SJohn Baldwin 7664edef187SJohn Baldwin /* 7674edef187SJohn Baldwin * Figure out a range to grow the bus range. First, find the 7684edef187SJohn Baldwin * first bus number after the last allocated bus in the rman and 7694edef187SJohn Baldwin * enforce that as a minimum starting point for the range. 7704edef187SJohn Baldwin */ 7714edef187SJohn Baldwin if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 || 7724edef187SJohn Baldwin end_free != bus->sub) 7734edef187SJohn Baldwin start_free = bus->sub + 1; 7744edef187SJohn Baldwin if (start_free < start) 7754edef187SJohn Baldwin start_free = start; 7764edef187SJohn Baldwin new_end = start_free + count - 1; 7774edef187SJohn Baldwin 7784edef187SJohn Baldwin /* 7794edef187SJohn Baldwin * See if this new range would satisfy the request if it 7804edef187SJohn Baldwin * succeeds. 7814edef187SJohn Baldwin */ 7824edef187SJohn Baldwin if (new_end > end) 7834edef187SJohn Baldwin return (NULL); 7844edef187SJohn Baldwin 7854edef187SJohn Baldwin /* Finally, attempt to grow the existing resource. */ 7864edef187SJohn Baldwin if (bootverbose) { 7874edef187SJohn Baldwin device_printf(bus->dev, 788da1b038aSJustin Hibbits "attempting to grow bus range for %ju buses\n", count); 789da1b038aSJustin Hibbits printf("\tback candidate range: %ju-%ju\n", start_free, 7904edef187SJohn Baldwin new_end); 7914edef187SJohn Baldwin } 7924edef187SJohn Baldwin if (pcib_grow_subbus(bus, new_end) == 0) 7934edef187SJohn Baldwin return (pcib_suballoc_bus(bus, child, rid, start, end, count, 7944edef187SJohn Baldwin flags)); 7954edef187SJohn Baldwin return (NULL); 7964edef187SJohn Baldwin } 7974edef187SJohn Baldwin #endif 7984edef187SJohn Baldwin 79983c41143SJohn Baldwin #else 80083c41143SJohn Baldwin 801bb0d0a8eSMike Smith /* 802b0a2d4b8SWarner Losh * Is the prefetch window open (eg, can we allocate memory in it?) 803b0a2d4b8SWarner Losh */ 804b0a2d4b8SWarner Losh static int 805b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc) 806b0a2d4b8SWarner Losh { 807b0a2d4b8SWarner Losh return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit); 808b0a2d4b8SWarner Losh } 809b0a2d4b8SWarner Losh 810b0a2d4b8SWarner Losh /* 811b0a2d4b8SWarner Losh * Is the nonprefetch window open (eg, can we allocate memory in it?) 812b0a2d4b8SWarner Losh */ 813b0a2d4b8SWarner Losh static int 814b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc) 815b0a2d4b8SWarner Losh { 816b0a2d4b8SWarner Losh return (sc->membase > 0 && sc->membase < sc->memlimit); 817b0a2d4b8SWarner Losh } 818b0a2d4b8SWarner Losh 819b0a2d4b8SWarner Losh /* 820b0a2d4b8SWarner Losh * Is the io window open (eg, can we allocate ports in it?) 821b0a2d4b8SWarner Losh */ 822b0a2d4b8SWarner Losh static int 823b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc) 824b0a2d4b8SWarner Losh { 825b0a2d4b8SWarner Losh return (sc->iobase > 0 && sc->iobase < sc->iolimit); 826b0a2d4b8SWarner Losh } 827b0a2d4b8SWarner Losh 828b0a2d4b8SWarner Losh /* 829e36af292SJung-uk Kim * Get current I/O decode. 830e36af292SJung-uk Kim */ 831e36af292SJung-uk Kim static void 832e36af292SJung-uk Kim pcib_get_io_decode(struct pcib_softc *sc) 833e36af292SJung-uk Kim { 834e36af292SJung-uk Kim device_t dev; 835e36af292SJung-uk Kim uint32_t iolow; 836e36af292SJung-uk Kim 837e36af292SJung-uk Kim dev = sc->dev; 838e36af292SJung-uk Kim 839e36af292SJung-uk Kim iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1); 840e36af292SJung-uk Kim if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 841e36af292SJung-uk Kim sc->iobase = PCI_PPBIOBASE( 842e36af292SJung-uk Kim pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow); 843e36af292SJung-uk Kim else 844e36af292SJung-uk Kim sc->iobase = PCI_PPBIOBASE(0, iolow); 845e36af292SJung-uk Kim 846e36af292SJung-uk Kim iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1); 847e36af292SJung-uk Kim if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 848e36af292SJung-uk Kim sc->iolimit = PCI_PPBIOLIMIT( 849e36af292SJung-uk Kim pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow); 850e36af292SJung-uk Kim else 851e36af292SJung-uk Kim sc->iolimit = PCI_PPBIOLIMIT(0, iolow); 852e36af292SJung-uk Kim } 853e36af292SJung-uk Kim 854e36af292SJung-uk Kim /* 855e36af292SJung-uk Kim * Get current memory decode. 856e36af292SJung-uk Kim */ 857e36af292SJung-uk Kim static void 858e36af292SJung-uk Kim pcib_get_mem_decode(struct pcib_softc *sc) 859e36af292SJung-uk Kim { 860e36af292SJung-uk Kim device_t dev; 861e36af292SJung-uk Kim pci_addr_t pmemlow; 862e36af292SJung-uk Kim 863e36af292SJung-uk Kim dev = sc->dev; 864e36af292SJung-uk Kim 865e36af292SJung-uk Kim sc->membase = PCI_PPBMEMBASE(0, 866e36af292SJung-uk Kim pci_read_config(dev, PCIR_MEMBASE_1, 2)); 867e36af292SJung-uk Kim sc->memlimit = PCI_PPBMEMLIMIT(0, 868e36af292SJung-uk Kim pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 869e36af292SJung-uk Kim 870e36af292SJung-uk Kim pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2); 871e36af292SJung-uk Kim if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 872e36af292SJung-uk Kim sc->pmembase = PCI_PPBMEMBASE( 873e36af292SJung-uk Kim pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow); 874e36af292SJung-uk Kim else 875e36af292SJung-uk Kim sc->pmembase = PCI_PPBMEMBASE(0, pmemlow); 876e36af292SJung-uk Kim 877e36af292SJung-uk Kim pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2); 878e36af292SJung-uk Kim if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 879e36af292SJung-uk Kim sc->pmemlimit = PCI_PPBMEMLIMIT( 880e36af292SJung-uk Kim pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow); 881e36af292SJung-uk Kim else 882e36af292SJung-uk Kim sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow); 883e36af292SJung-uk Kim } 884e36af292SJung-uk Kim 885e36af292SJung-uk Kim /* 886e36af292SJung-uk Kim * Restore previous I/O decode. 887e36af292SJung-uk Kim */ 888e36af292SJung-uk Kim static void 889e36af292SJung-uk Kim pcib_set_io_decode(struct pcib_softc *sc) 890e36af292SJung-uk Kim { 891e36af292SJung-uk Kim device_t dev; 892e36af292SJung-uk Kim uint32_t iohi; 893e36af292SJung-uk Kim 894e36af292SJung-uk Kim dev = sc->dev; 895e36af292SJung-uk Kim 896e36af292SJung-uk Kim iohi = sc->iobase >> 16; 897e36af292SJung-uk Kim if (iohi > 0) 898e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2); 899e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1); 900e36af292SJung-uk Kim 901e36af292SJung-uk Kim iohi = sc->iolimit >> 16; 902e36af292SJung-uk Kim if (iohi > 0) 903e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2); 904e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1); 905e36af292SJung-uk Kim } 906e36af292SJung-uk Kim 907e36af292SJung-uk Kim /* 908e36af292SJung-uk Kim * Restore previous memory decode. 909e36af292SJung-uk Kim */ 910e36af292SJung-uk Kim static void 911e36af292SJung-uk Kim pcib_set_mem_decode(struct pcib_softc *sc) 912e36af292SJung-uk Kim { 913e36af292SJung-uk Kim device_t dev; 914e36af292SJung-uk Kim pci_addr_t pmemhi; 915e36af292SJung-uk Kim 916e36af292SJung-uk Kim dev = sc->dev; 917e36af292SJung-uk Kim 918e36af292SJung-uk Kim pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2); 919e36af292SJung-uk Kim pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2); 920e36af292SJung-uk Kim 921e36af292SJung-uk Kim pmemhi = sc->pmembase >> 32; 922e36af292SJung-uk Kim if (pmemhi > 0) 923e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4); 924e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2); 925e36af292SJung-uk Kim 926e36af292SJung-uk Kim pmemhi = sc->pmemlimit >> 32; 927e36af292SJung-uk Kim if (pmemhi > 0) 928e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4); 929e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2); 930e36af292SJung-uk Kim } 93183c41143SJohn Baldwin #endif 932e36af292SJung-uk Kim 93382cb5c3bSJohn Baldwin #ifdef PCI_HP 93482cb5c3bSJohn Baldwin /* 93582cb5c3bSJohn Baldwin * PCI-express HotPlug support. 93682cb5c3bSJohn Baldwin */ 93725a57bd6SJohn Baldwin static int pci_enable_pcie_hp = 1; 93825a57bd6SJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_hp, CTLFLAG_RDTUN, 93925a57bd6SJohn Baldwin &pci_enable_pcie_hp, 0, 94025a57bd6SJohn Baldwin "Enable support for native PCI-express HotPlug."); 94125a57bd6SJohn Baldwin 94212588ce0SAndriy Gapon TASKQUEUE_DEFINE_THREAD(pci_hp); 94312588ce0SAndriy Gapon 94482cb5c3bSJohn Baldwin static void 94582cb5c3bSJohn Baldwin pcib_probe_hotplug(struct pcib_softc *sc) 94682cb5c3bSJohn Baldwin { 94782cb5c3bSJohn Baldwin device_t dev; 94837290148SEric van Gyzen uint32_t link_cap; 949991d431fSEric van Gyzen uint16_t link_sta, slot_sta; 95082cb5c3bSJohn Baldwin 95125a57bd6SJohn Baldwin if (!pci_enable_pcie_hp) 95225a57bd6SJohn Baldwin return; 95325a57bd6SJohn Baldwin 95482cb5c3bSJohn Baldwin dev = sc->dev; 95582cb5c3bSJohn Baldwin if (pci_find_cap(dev, PCIY_EXPRESS, NULL) != 0) 95682cb5c3bSJohn Baldwin return; 95782cb5c3bSJohn Baldwin 95882cb5c3bSJohn Baldwin if (!(pcie_read_config(dev, PCIER_FLAGS, 2) & PCIEM_FLAGS_SLOT)) 95982cb5c3bSJohn Baldwin return; 96082cb5c3bSJohn Baldwin 96182cb5c3bSJohn Baldwin sc->pcie_slot_cap = pcie_read_config(dev, PCIER_SLOT_CAP, 4); 96282cb5c3bSJohn Baldwin 963991d431fSEric van Gyzen if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_HPC) == 0) 9642611037cSJohn Baldwin return; 96537290148SEric van Gyzen link_cap = pcie_read_config(dev, PCIER_LINK_CAP, 4); 96637290148SEric van Gyzen if ((link_cap & PCIEM_LINK_CAP_DL_ACTIVE) == 0) 9672ffb582aSJohn Baldwin return; 9682611037cSJohn Baldwin 969991d431fSEric van Gyzen /* 970991d431fSEric van Gyzen * Some devices report that they have an MRL when they actually 971991d431fSEric van Gyzen * do not. Since they always report that the MRL is open, child 972991d431fSEric van Gyzen * devices would be ignored. Try to detect these devices and 973991d431fSEric van Gyzen * ignore their claim of HotPlug support. 974991d431fSEric van Gyzen * 975991d431fSEric van Gyzen * If there is an open MRL but the Data Link Layer is active, 976991d431fSEric van Gyzen * the MRL is not real. 977991d431fSEric van Gyzen */ 97837290148SEric van Gyzen if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) != 0) { 979991d431fSEric van Gyzen link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 980991d431fSEric van Gyzen slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 981991d431fSEric van Gyzen if ((slot_sta & PCIEM_SLOT_STA_MRLSS) != 0 && 982991d431fSEric van Gyzen (link_sta & PCIEM_LINK_STA_DL_ACTIVE) != 0) { 983991d431fSEric van Gyzen return; 984991d431fSEric van Gyzen } 985991d431fSEric van Gyzen } 986991d431fSEric van Gyzen 98728586889SWarner Losh /* 98828586889SWarner Losh * Now that we're sure we want to do hot plug, ask the 98928586889SWarner Losh * firmware, if any, if that's OK. 99028586889SWarner Losh */ 9911ffd07bdSJohn Baldwin if (pcib_request_feature(dev, PCI_FEATURE_HP) != 0) { 99228586889SWarner Losh if (bootverbose) 99328586889SWarner Losh device_printf(dev, "Unable to activate hot plug feature.\n"); 99428586889SWarner Losh return; 99528586889SWarner Losh } 99628586889SWarner Losh 99782cb5c3bSJohn Baldwin sc->flags |= PCIB_HOTPLUG; 99882cb5c3bSJohn Baldwin } 99982cb5c3bSJohn Baldwin 100082cb5c3bSJohn Baldwin /* 100182cb5c3bSJohn Baldwin * Send a HotPlug command to the slot control register. If this slot 100207454911SJohn Baldwin * uses command completion interrupts and a previous command is still 100307454911SJohn Baldwin * in progress, then the command is dropped. Once the previous 100407454911SJohn Baldwin * command completes or times out, pcib_pcie_hotplug_update() will be 100507454911SJohn Baldwin * invoked to post a new command based on the slot's state at that 100607454911SJohn Baldwin * time. 100782cb5c3bSJohn Baldwin */ 100882cb5c3bSJohn Baldwin static void 100982cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(struct pcib_softc *sc, uint16_t val, uint16_t mask) 101082cb5c3bSJohn Baldwin { 101182cb5c3bSJohn Baldwin device_t dev; 101282cb5c3bSJohn Baldwin uint16_t ctl, new; 101382cb5c3bSJohn Baldwin 101482cb5c3bSJohn Baldwin dev = sc->dev; 101582cb5c3bSJohn Baldwin 101607454911SJohn Baldwin if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) 101707454911SJohn Baldwin return; 101807454911SJohn Baldwin 101982cb5c3bSJohn Baldwin ctl = pcie_read_config(dev, PCIER_SLOT_CTL, 2); 102082cb5c3bSJohn Baldwin new = (ctl & ~mask) | val; 102107454911SJohn Baldwin if (new == ctl) 102207454911SJohn Baldwin return; 1023991d431fSEric van Gyzen if (bootverbose) 1024991d431fSEric van Gyzen device_printf(dev, "HotPlug command: %04x -> %04x\n", ctl, new); 102507454911SJohn Baldwin pcie_write_config(dev, PCIER_SLOT_CTL, new, 2); 10266f33eaa5SJohn Baldwin if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS) && 10276f33eaa5SJohn Baldwin (ctl & new) & PCIEM_SLOT_CTL_CCIE) { 102882cb5c3bSJohn Baldwin sc->flags |= PCIB_HOTPLUG_CMD_PENDING; 102982cb5c3bSJohn Baldwin if (!cold) 1030fa3b03d3SAlexander Motin taskqueue_enqueue_timeout(taskqueue_pci_hp, 1031fa3b03d3SAlexander Motin &sc->pcie_cc_task, hz); 103282cb5c3bSJohn Baldwin } 103382cb5c3bSJohn Baldwin } 103482cb5c3bSJohn Baldwin 103582cb5c3bSJohn Baldwin static void 103682cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(struct pcib_softc *sc) 103782cb5c3bSJohn Baldwin { 103882cb5c3bSJohn Baldwin device_t dev; 103982cb5c3bSJohn Baldwin 104082cb5c3bSJohn Baldwin dev = sc->dev; 104182cb5c3bSJohn Baldwin 104282cb5c3bSJohn Baldwin if (bootverbose) 104382cb5c3bSJohn Baldwin device_printf(dev, "Command Completed\n"); 104482cb5c3bSJohn Baldwin if (!(sc->flags & PCIB_HOTPLUG_CMD_PENDING)) 104582cb5c3bSJohn Baldwin return; 1046fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_cc_task, NULL); 104782cb5c3bSJohn Baldwin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 10486f33eaa5SJohn Baldwin wakeup(sc); 104982cb5c3bSJohn Baldwin } 105082cb5c3bSJohn Baldwin 105182cb5c3bSJohn Baldwin /* 105282cb5c3bSJohn Baldwin * Returns true if a card is fully inserted from the user's 105382cb5c3bSJohn Baldwin * perspective. It may not yet be ready for access, but the driver 105482cb5c3bSJohn Baldwin * can now start enabling access if necessary. 105582cb5c3bSJohn Baldwin */ 105682cb5c3bSJohn Baldwin static bool 105782cb5c3bSJohn Baldwin pcib_hotplug_inserted(struct pcib_softc *sc) 105882cb5c3bSJohn Baldwin { 105982cb5c3bSJohn Baldwin 106082cb5c3bSJohn Baldwin /* Pretend the card isn't present if a detach is forced. */ 106182cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACHING) 106282cb5c3bSJohn Baldwin return (false); 106382cb5c3bSJohn Baldwin 106482cb5c3bSJohn Baldwin /* Card must be present in the slot. */ 106582cb5c3bSJohn Baldwin if ((sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS) == 0) 106682cb5c3bSJohn Baldwin return (false); 106782cb5c3bSJohn Baldwin 106882cb5c3bSJohn Baldwin /* A power fault implicitly turns off power to the slot. */ 106982cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD) 107082cb5c3bSJohn Baldwin return (false); 107182cb5c3bSJohn Baldwin 107282cb5c3bSJohn Baldwin /* If the MRL is disengaged, the slot is powered off. */ 107382cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP && 107482cb5c3bSJohn Baldwin (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS) != 0) 107582cb5c3bSJohn Baldwin return (false); 107682cb5c3bSJohn Baldwin 107782cb5c3bSJohn Baldwin return (true); 107882cb5c3bSJohn Baldwin } 107982cb5c3bSJohn Baldwin 108082cb5c3bSJohn Baldwin /* 108182cb5c3bSJohn Baldwin * Returns -1 if the card is fully inserted, powered, and ready for 108282cb5c3bSJohn Baldwin * access. Otherwise, returns 0. 108382cb5c3bSJohn Baldwin */ 108482cb5c3bSJohn Baldwin static int 108582cb5c3bSJohn Baldwin pcib_hotplug_present(struct pcib_softc *sc) 108682cb5c3bSJohn Baldwin { 108782cb5c3bSJohn Baldwin 108882cb5c3bSJohn Baldwin /* Card must be inserted. */ 108982cb5c3bSJohn Baldwin if (!pcib_hotplug_inserted(sc)) 109082cb5c3bSJohn Baldwin return (0); 109182cb5c3bSJohn Baldwin 109282cb5c3bSJohn Baldwin /* Require the Data Link Layer to be active. */ 109382cb5c3bSJohn Baldwin if (!(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE)) 109482cb5c3bSJohn Baldwin return (0); 109582cb5c3bSJohn Baldwin 109682cb5c3bSJohn Baldwin return (-1); 109782cb5c3bSJohn Baldwin } 109882cb5c3bSJohn Baldwin 109982cb5c3bSJohn Baldwin static void 110082cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(struct pcib_softc *sc, uint16_t val, uint16_t mask, 110182cb5c3bSJohn Baldwin bool schedule_task) 110282cb5c3bSJohn Baldwin { 1103a1566487SEric van Gyzen bool card_inserted, ei_engaged; 110482cb5c3bSJohn Baldwin 1105991d431fSEric van Gyzen /* Clear DETACHING if Presence Detect has cleared. */ 110682cb5c3bSJohn Baldwin if ((sc->pcie_slot_sta & (PCIEM_SLOT_STA_PDC | PCIEM_SLOT_STA_PDS)) == 110782cb5c3bSJohn Baldwin PCIEM_SLOT_STA_PDC) 110882cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACHING; 110982cb5c3bSJohn Baldwin 111082cb5c3bSJohn Baldwin card_inserted = pcib_hotplug_inserted(sc); 111182cb5c3bSJohn Baldwin 111282cb5c3bSJohn Baldwin /* Turn the power indicator on if a card is inserted. */ 111382cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PIP) { 111482cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_PIC; 111582cb5c3bSJohn Baldwin if (card_inserted) 111682cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_ON; 111782cb5c3bSJohn Baldwin else if (sc->flags & PCIB_DETACH_PENDING) 111882cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_BLINK; 111982cb5c3bSJohn Baldwin else 112082cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_OFF; 112182cb5c3bSJohn Baldwin } 112282cb5c3bSJohn Baldwin 112382cb5c3bSJohn Baldwin /* Turn the power on via the Power Controller if a card is inserted. */ 112482cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) { 112582cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_PCC; 112682cb5c3bSJohn Baldwin if (card_inserted) 112782cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PC_ON; 112882cb5c3bSJohn Baldwin else 112982cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PC_OFF; 113082cb5c3bSJohn Baldwin } 113182cb5c3bSJohn Baldwin 113282cb5c3bSJohn Baldwin /* 113382cb5c3bSJohn Baldwin * If a card is inserted, enable the Electromechanical 113482cb5c3bSJohn Baldwin * Interlock. If a card is not inserted (or we are in the 113582cb5c3bSJohn Baldwin * process of detaching), disable the Electromechanical 113682cb5c3bSJohn Baldwin * Interlock. 113782cb5c3bSJohn Baldwin */ 113882cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP) { 113982cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_EIC; 1140a1566487SEric van Gyzen ei_engaged = (sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS) != 0; 1141a1566487SEric van Gyzen if (card_inserted != ei_engaged) 114282cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_EIC; 114382cb5c3bSJohn Baldwin } 114482cb5c3bSJohn Baldwin 114582cb5c3bSJohn Baldwin /* 114682cb5c3bSJohn Baldwin * Start a timer to see if the Data Link Layer times out. 1147991d431fSEric van Gyzen * Note that we only start the timer if Presence Detect or MRL Sensor 114882cb5c3bSJohn Baldwin * changed on this interrupt. Stop any scheduled timer if 114982cb5c3bSJohn Baldwin * the Data Link Layer is active. 115082cb5c3bSJohn Baldwin */ 115182cb5c3bSJohn Baldwin if (card_inserted && 115282cb5c3bSJohn Baldwin !(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) && 1153991d431fSEric van Gyzen sc->pcie_slot_sta & 1154991d431fSEric van Gyzen (PCIEM_SLOT_STA_MRLSC | PCIEM_SLOT_STA_PDC)) { 115582cb5c3bSJohn Baldwin if (cold) 115682cb5c3bSJohn Baldwin device_printf(sc->dev, 115782cb5c3bSJohn Baldwin "Data Link Layer inactive\n"); 115882cb5c3bSJohn Baldwin else 1159fa3b03d3SAlexander Motin taskqueue_enqueue_timeout(taskqueue_pci_hp, 1160fa3b03d3SAlexander Motin &sc->pcie_dll_task, hz); 116182cb5c3bSJohn Baldwin } else if (sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) 1162fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_dll_task, 1163fa3b03d3SAlexander Motin NULL); 116482cb5c3bSJohn Baldwin 116582cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(sc, val, mask); 116682cb5c3bSJohn Baldwin 116782cb5c3bSJohn Baldwin /* 1168a1566487SEric van Gyzen * During attach the child "pci" device is added synchronously; 116982cb5c3bSJohn Baldwin * otherwise, the task is scheduled to manage the child 117082cb5c3bSJohn Baldwin * device. 117182cb5c3bSJohn Baldwin */ 117282cb5c3bSJohn Baldwin if (schedule_task && 117382cb5c3bSJohn Baldwin (pcib_hotplug_present(sc) != 0) != (sc->child != NULL)) 117412588ce0SAndriy Gapon taskqueue_enqueue(taskqueue_pci_hp, &sc->pcie_hp_task); 117582cb5c3bSJohn Baldwin } 117682cb5c3bSJohn Baldwin 117782cb5c3bSJohn Baldwin static void 11788a1926c5SWarner Losh pcib_pcie_intr_hotplug(void *arg) 117982cb5c3bSJohn Baldwin { 118082cb5c3bSJohn Baldwin struct pcib_softc *sc; 118182cb5c3bSJohn Baldwin device_t dev; 1182e0235fd3SColin Percival uint16_t old_slot_sta; 118382cb5c3bSJohn Baldwin 118482cb5c3bSJohn Baldwin sc = arg; 118582cb5c3bSJohn Baldwin dev = sc->dev; 118613d700adSScott Long PCIB_HP_LOCK(sc); 1187e0235fd3SColin Percival old_slot_sta = sc->pcie_slot_sta; 118882cb5c3bSJohn Baldwin sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 118982cb5c3bSJohn Baldwin 119082cb5c3bSJohn Baldwin /* Clear the events just reported. */ 119182cb5c3bSJohn Baldwin pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2); 119282cb5c3bSJohn Baldwin 1193991d431fSEric van Gyzen if (bootverbose) 1194991d431fSEric van Gyzen device_printf(dev, "HotPlug interrupt: %#x\n", 1195991d431fSEric van Gyzen sc->pcie_slot_sta); 1196991d431fSEric van Gyzen 119782cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_ABP) { 119882cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 119982cb5c3bSJohn Baldwin device_printf(dev, 120082cb5c3bSJohn Baldwin "Attention Button Pressed: Detach Cancelled\n"); 120182cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 1202fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, 1203fa3b03d3SAlexander Motin &sc->pcie_ab_task, NULL); 1204e0235fd3SColin Percival } else if (old_slot_sta & PCIEM_SLOT_STA_PDS) { 1205e0235fd3SColin Percival /* Only initiate detach sequence if device present. */ 120682cb5c3bSJohn Baldwin device_printf(dev, 120782cb5c3bSJohn Baldwin "Attention Button Pressed: Detaching in 5 seconds\n"); 120882cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACH_PENDING; 1209fa3b03d3SAlexander Motin taskqueue_enqueue_timeout(taskqueue_pci_hp, 1210fa3b03d3SAlexander Motin &sc->pcie_ab_task, 5 * hz); 121182cb5c3bSJohn Baldwin } 121282cb5c3bSJohn Baldwin } 121382cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD) 121482cb5c3bSJohn Baldwin device_printf(dev, "Power Fault Detected\n"); 121582cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSC) 121682cb5c3bSJohn Baldwin device_printf(dev, "MRL Sensor Changed to %s\n", 121782cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS ? "open" : 121882cb5c3bSJohn Baldwin "closed"); 121982cb5c3bSJohn Baldwin if (bootverbose && sc->pcie_slot_sta & PCIEM_SLOT_STA_PDC) 1220991d431fSEric van Gyzen device_printf(dev, "Presence Detect Changed to %s\n", 122182cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS ? "card present" : 122282cb5c3bSJohn Baldwin "empty"); 122382cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_CC) 122482cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(sc); 122582cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_DLLSC) { 122682cb5c3bSJohn Baldwin sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 122782cb5c3bSJohn Baldwin if (bootverbose) 122882cb5c3bSJohn Baldwin device_printf(dev, 122982cb5c3bSJohn Baldwin "Data Link Layer State Changed to %s\n", 123082cb5c3bSJohn Baldwin sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE ? 123182cb5c3bSJohn Baldwin "active" : "inactive"); 123282cb5c3bSJohn Baldwin } 123382cb5c3bSJohn Baldwin 123482cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 123513d700adSScott Long PCIB_HP_UNLOCK(sc); 123682cb5c3bSJohn Baldwin } 123782cb5c3bSJohn Baldwin 123882cb5c3bSJohn Baldwin static void 123982cb5c3bSJohn Baldwin pcib_pcie_hotplug_task(void *context, int pending) 124082cb5c3bSJohn Baldwin { 124182cb5c3bSJohn Baldwin struct pcib_softc *sc; 124282cb5c3bSJohn Baldwin device_t dev; 124382cb5c3bSJohn Baldwin 124482cb5c3bSJohn Baldwin sc = context; 124513d700adSScott Long PCIB_HP_LOCK(sc); 124682cb5c3bSJohn Baldwin dev = sc->dev; 124782cb5c3bSJohn Baldwin if (pcib_hotplug_present(sc) != 0) { 124882cb5c3bSJohn Baldwin if (sc->child == NULL) { 124982cb5c3bSJohn Baldwin sc->child = device_add_child(dev, "pci", -1); 125082cb5c3bSJohn Baldwin bus_generic_attach(dev); 125182cb5c3bSJohn Baldwin } 125282cb5c3bSJohn Baldwin } else { 125382cb5c3bSJohn Baldwin if (sc->child != NULL) { 125482cb5c3bSJohn Baldwin if (device_delete_child(dev, sc->child) == 0) 125582cb5c3bSJohn Baldwin sc->child = NULL; 125682cb5c3bSJohn Baldwin } 125782cb5c3bSJohn Baldwin } 125813d700adSScott Long PCIB_HP_UNLOCK(sc); 125982cb5c3bSJohn Baldwin } 126082cb5c3bSJohn Baldwin 126182cb5c3bSJohn Baldwin static void 1262fa3b03d3SAlexander Motin pcib_pcie_ab_timeout(void *arg, int pending) 126382cb5c3bSJohn Baldwin { 1264fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 126582cb5c3bSJohn Baldwin 1266fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 126782cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 126882cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING; 126982cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 127082cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 127182cb5c3bSJohn Baldwin } 1272fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 127382cb5c3bSJohn Baldwin } 127482cb5c3bSJohn Baldwin 127582cb5c3bSJohn Baldwin static void 1276fa3b03d3SAlexander Motin pcib_pcie_cc_timeout(void *arg, int pending) 127782cb5c3bSJohn Baldwin { 1278fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 1279fa3b03d3SAlexander Motin device_t dev = sc->dev; 12806f33eaa5SJohn Baldwin uint16_t sta; 128182cb5c3bSJohn Baldwin 1282fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 12836f33eaa5SJohn Baldwin sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 12846f33eaa5SJohn Baldwin if (!(sta & PCIEM_SLOT_STA_CC)) { 128521e51c82SAlexander Motin device_printf(dev, "HotPlug Command Timed Out\n"); 128621e51c82SAlexander Motin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 12876f33eaa5SJohn Baldwin } else { 12886f33eaa5SJohn Baldwin device_printf(dev, 12896f33eaa5SJohn Baldwin "Missed HotPlug interrupt waiting for Command Completion\n"); 12908a1926c5SWarner Losh pcib_pcie_intr_hotplug(sc); 129182cb5c3bSJohn Baldwin } 1292fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 129382cb5c3bSJohn Baldwin } 129482cb5c3bSJohn Baldwin 129582cb5c3bSJohn Baldwin static void 1296fa3b03d3SAlexander Motin pcib_pcie_dll_timeout(void *arg, int pending) 129782cb5c3bSJohn Baldwin { 1298fa3b03d3SAlexander Motin struct pcib_softc *sc = arg; 1299fa3b03d3SAlexander Motin device_t dev = sc->dev; 130082cb5c3bSJohn Baldwin uint16_t sta; 130182cb5c3bSJohn Baldwin 1302fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc); 130382cb5c3bSJohn Baldwin sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 130482cb5c3bSJohn Baldwin if (!(sta & PCIEM_LINK_STA_DL_ACTIVE)) { 130582cb5c3bSJohn Baldwin device_printf(dev, 130682cb5c3bSJohn Baldwin "Timed out waiting for Data Link Layer Active\n"); 130782cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING; 130882cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true); 130982cb5c3bSJohn Baldwin } else if (sta != sc->pcie_link_sta) { 131082cb5c3bSJohn Baldwin device_printf(dev, 131182cb5c3bSJohn Baldwin "Missed HotPlug interrupt waiting for DLL Active\n"); 13128a1926c5SWarner Losh pcib_pcie_intr_hotplug(sc); 131382cb5c3bSJohn Baldwin } 1314fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc); 131582cb5c3bSJohn Baldwin } 131682cb5c3bSJohn Baldwin 131782cb5c3bSJohn Baldwin static int 131882cb5c3bSJohn Baldwin pcib_alloc_pcie_irq(struct pcib_softc *sc) 131982cb5c3bSJohn Baldwin { 132082cb5c3bSJohn Baldwin device_t dev; 132182cb5c3bSJohn Baldwin int count, error, rid; 132282cb5c3bSJohn Baldwin 132382cb5c3bSJohn Baldwin rid = -1; 132482cb5c3bSJohn Baldwin dev = sc->dev; 132582cb5c3bSJohn Baldwin 132682cb5c3bSJohn Baldwin /* 132782cb5c3bSJohn Baldwin * For simplicity, only use MSI-X if there is a single message. 132882cb5c3bSJohn Baldwin * To support a device with multiple messages we would have to 132982cb5c3bSJohn Baldwin * use remap intr if the MSI number is not 0. 133082cb5c3bSJohn Baldwin */ 133182cb5c3bSJohn Baldwin count = pci_msix_count(dev); 133282cb5c3bSJohn Baldwin if (count == 1) { 133382cb5c3bSJohn Baldwin error = pci_alloc_msix(dev, &count); 133482cb5c3bSJohn Baldwin if (error == 0) 133582cb5c3bSJohn Baldwin rid = 1; 133682cb5c3bSJohn Baldwin } 133782cb5c3bSJohn Baldwin 133882cb5c3bSJohn Baldwin if (rid < 0 && pci_msi_count(dev) > 0) { 133982cb5c3bSJohn Baldwin count = 1; 134082cb5c3bSJohn Baldwin error = pci_alloc_msi(dev, &count); 134182cb5c3bSJohn Baldwin if (error == 0) 134282cb5c3bSJohn Baldwin rid = 1; 134382cb5c3bSJohn Baldwin } 134482cb5c3bSJohn Baldwin 134582cb5c3bSJohn Baldwin if (rid < 0) 134682cb5c3bSJohn Baldwin rid = 0; 134782cb5c3bSJohn Baldwin 134882cb5c3bSJohn Baldwin sc->pcie_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1349f14f0051SChuck Tuffli RF_ACTIVE | RF_SHAREABLE); 135082cb5c3bSJohn Baldwin if (sc->pcie_irq == NULL) { 135182cb5c3bSJohn Baldwin device_printf(dev, 135282cb5c3bSJohn Baldwin "Failed to allocate interrupt for PCI-e events\n"); 135382cb5c3bSJohn Baldwin if (rid > 0) 135482cb5c3bSJohn Baldwin pci_release_msi(dev); 135582cb5c3bSJohn Baldwin return (ENXIO); 135682cb5c3bSJohn Baldwin } 135782cb5c3bSJohn Baldwin 135813d700adSScott Long error = bus_setup_intr(dev, sc->pcie_irq, INTR_TYPE_MISC|INTR_MPSAFE, 13598a1926c5SWarner Losh NULL, pcib_pcie_intr_hotplug, sc, &sc->pcie_ihand); 136082cb5c3bSJohn Baldwin if (error) { 136182cb5c3bSJohn Baldwin device_printf(dev, "Failed to setup PCI-e interrupt handler\n"); 136282cb5c3bSJohn Baldwin bus_release_resource(dev, SYS_RES_IRQ, rid, sc->pcie_irq); 136382cb5c3bSJohn Baldwin if (rid > 0) 136482cb5c3bSJohn Baldwin pci_release_msi(dev); 136582cb5c3bSJohn Baldwin return (error); 136682cb5c3bSJohn Baldwin } 136782cb5c3bSJohn Baldwin return (0); 136882cb5c3bSJohn Baldwin } 136982cb5c3bSJohn Baldwin 13706f33eaa5SJohn Baldwin static int 13716f33eaa5SJohn Baldwin pcib_release_pcie_irq(struct pcib_softc *sc) 13726f33eaa5SJohn Baldwin { 13736f33eaa5SJohn Baldwin device_t dev; 13746f33eaa5SJohn Baldwin int error; 13756f33eaa5SJohn Baldwin 13766f33eaa5SJohn Baldwin dev = sc->dev; 13776f33eaa5SJohn Baldwin error = bus_teardown_intr(dev, sc->pcie_irq, sc->pcie_ihand); 13786f33eaa5SJohn Baldwin if (error) 13796f33eaa5SJohn Baldwin return (error); 13806f33eaa5SJohn Baldwin error = bus_free_resource(dev, SYS_RES_IRQ, sc->pcie_irq); 13816f33eaa5SJohn Baldwin if (error) 13826f33eaa5SJohn Baldwin return (error); 13836f33eaa5SJohn Baldwin return (pci_release_msi(dev)); 13846f33eaa5SJohn Baldwin } 13856f33eaa5SJohn Baldwin 138682cb5c3bSJohn Baldwin static void 138782cb5c3bSJohn Baldwin pcib_setup_hotplug(struct pcib_softc *sc) 138882cb5c3bSJohn Baldwin { 138982cb5c3bSJohn Baldwin device_t dev; 139082cb5c3bSJohn Baldwin uint16_t mask, val; 139182cb5c3bSJohn Baldwin 139282cb5c3bSJohn Baldwin dev = sc->dev; 139382cb5c3bSJohn Baldwin TASK_INIT(&sc->pcie_hp_task, 0, pcib_pcie_hotplug_task, sc); 1394fa3b03d3SAlexander Motin TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_ab_task, 0, 1395fa3b03d3SAlexander Motin pcib_pcie_ab_timeout, sc); 1396fa3b03d3SAlexander Motin TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_cc_task, 0, 1397fa3b03d3SAlexander Motin pcib_pcie_cc_timeout, sc); 1398fa3b03d3SAlexander Motin TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_dll_task, 0, 1399fa3b03d3SAlexander Motin pcib_pcie_dll_timeout, sc); 140013d700adSScott Long sc->pcie_hp_lock = &Giant; 140182cb5c3bSJohn Baldwin 140282cb5c3bSJohn Baldwin /* Allocate IRQ. */ 140382cb5c3bSJohn Baldwin if (pcib_alloc_pcie_irq(sc) != 0) 140482cb5c3bSJohn Baldwin return; 140582cb5c3bSJohn Baldwin 140682cb5c3bSJohn Baldwin sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 140782cb5c3bSJohn Baldwin sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 140882cb5c3bSJohn Baldwin 14096f33eaa5SJohn Baldwin /* Clear any events previously pending. */ 14106f33eaa5SJohn Baldwin pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2); 14116f33eaa5SJohn Baldwin 141282cb5c3bSJohn Baldwin /* Enable HotPlug events. */ 141382cb5c3bSJohn Baldwin mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | 141482cb5c3bSJohn Baldwin PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE | 141582cb5c3bSJohn Baldwin PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE; 141637290148SEric van Gyzen val = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | PCIEM_SLOT_CTL_PDCE; 141782cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_APB) 141882cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_ABPE; 141982cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) 142082cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PFDE; 142182cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) 142282cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_MRLSCE; 142382cb5c3bSJohn Baldwin if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS)) 142482cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_CCIE; 142582cb5c3bSJohn Baldwin 142682cb5c3bSJohn Baldwin /* Turn the attention indicator off. */ 142782cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) { 142882cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_AIC; 142982cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_AI_OFF; 143082cb5c3bSJohn Baldwin } 143182cb5c3bSJohn Baldwin 143282cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, val, mask, false); 143382cb5c3bSJohn Baldwin } 14346f33eaa5SJohn Baldwin 14356f33eaa5SJohn Baldwin static int 14366f33eaa5SJohn Baldwin pcib_detach_hotplug(struct pcib_softc *sc) 14376f33eaa5SJohn Baldwin { 14386f33eaa5SJohn Baldwin uint16_t mask, val; 14396f33eaa5SJohn Baldwin int error; 14406f33eaa5SJohn Baldwin 14416f33eaa5SJohn Baldwin /* Disable the card in the slot and force it to detach. */ 14426f33eaa5SJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) { 14436f33eaa5SJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING; 1444fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_ab_task, 1445fa3b03d3SAlexander Motin NULL); 14466f33eaa5SJohn Baldwin } 14476f33eaa5SJohn Baldwin sc->flags |= PCIB_DETACHING; 14486f33eaa5SJohn Baldwin 14496f33eaa5SJohn Baldwin if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) { 1450fa3b03d3SAlexander Motin taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_cc_task, 1451fa3b03d3SAlexander Motin NULL); 14526f33eaa5SJohn Baldwin tsleep(sc, 0, "hpcmd", hz); 14536f33eaa5SJohn Baldwin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 14546f33eaa5SJohn Baldwin } 14556f33eaa5SJohn Baldwin 14566f33eaa5SJohn Baldwin /* Disable HotPlug events. */ 14576f33eaa5SJohn Baldwin mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | 14586f33eaa5SJohn Baldwin PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE | 14596f33eaa5SJohn Baldwin PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE; 14606f33eaa5SJohn Baldwin val = 0; 14616f33eaa5SJohn Baldwin 14626f33eaa5SJohn Baldwin /* Turn the attention indicator off. */ 14636f33eaa5SJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) { 14646f33eaa5SJohn Baldwin mask |= PCIEM_SLOT_CTL_AIC; 14656f33eaa5SJohn Baldwin val |= PCIEM_SLOT_CTL_AI_OFF; 14666f33eaa5SJohn Baldwin } 14676f33eaa5SJohn Baldwin 14686f33eaa5SJohn Baldwin pcib_pcie_hotplug_update(sc, val, mask, false); 14696f33eaa5SJohn Baldwin 14706f33eaa5SJohn Baldwin error = pcib_release_pcie_irq(sc); 14716f33eaa5SJohn Baldwin if (error) 14726f33eaa5SJohn Baldwin return (error); 147312588ce0SAndriy Gapon taskqueue_drain(taskqueue_pci_hp, &sc->pcie_hp_task); 1474fa3b03d3SAlexander Motin taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_ab_task); 1475fa3b03d3SAlexander Motin taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_cc_task); 1476fa3b03d3SAlexander Motin taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_dll_task); 14776f33eaa5SJohn Baldwin return (0); 14786f33eaa5SJohn Baldwin } 147982cb5c3bSJohn Baldwin #endif 148082cb5c3bSJohn Baldwin 1481e36af292SJung-uk Kim /* 1482e36af292SJung-uk Kim * Get current bridge configuration. 1483e36af292SJung-uk Kim */ 1484e36af292SJung-uk Kim static void 1485e36af292SJung-uk Kim pcib_cfg_save(struct pcib_softc *sc) 1486e36af292SJung-uk Kim { 1487ad6f36f8SJohn Baldwin #ifndef NEW_PCIB 1488e36af292SJung-uk Kim device_t dev; 1489ad6f36f8SJohn Baldwin uint16_t command; 1490e36af292SJung-uk Kim 1491e36af292SJung-uk Kim dev = sc->dev; 1492e36af292SJung-uk Kim 1493ad6f36f8SJohn Baldwin command = pci_read_config(dev, PCIR_COMMAND, 2); 1494ad6f36f8SJohn Baldwin if (command & PCIM_CMD_PORTEN) 1495e36af292SJung-uk Kim pcib_get_io_decode(sc); 1496ad6f36f8SJohn Baldwin if (command & PCIM_CMD_MEMEN) 1497e36af292SJung-uk Kim pcib_get_mem_decode(sc); 149883c41143SJohn Baldwin #endif 1499e36af292SJung-uk Kim } 1500e36af292SJung-uk Kim 1501e36af292SJung-uk Kim /* 1502e36af292SJung-uk Kim * Restore previous bridge configuration. 1503e36af292SJung-uk Kim */ 1504e36af292SJung-uk Kim static void 1505e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc) 1506e36af292SJung-uk Kim { 1507ad6f36f8SJohn Baldwin #ifndef NEW_PCIB 1508ad6f36f8SJohn Baldwin uint16_t command; 1509ad6f36f8SJohn Baldwin #endif 1510e36af292SJung-uk Kim 151183c41143SJohn Baldwin #ifdef NEW_PCIB 151283c41143SJohn Baldwin pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM); 151383c41143SJohn Baldwin #else 1514151ba793SAlexander Kabaev command = pci_read_config(sc->dev, PCIR_COMMAND, 2); 1515ad6f36f8SJohn Baldwin if (command & PCIM_CMD_PORTEN) 1516e36af292SJung-uk Kim pcib_set_io_decode(sc); 1517ad6f36f8SJohn Baldwin if (command & PCIM_CMD_MEMEN) 1518e36af292SJung-uk Kim pcib_set_mem_decode(sc); 151983c41143SJohn Baldwin #endif 1520e36af292SJung-uk Kim } 1521e36af292SJung-uk Kim 1522e36af292SJung-uk Kim /* 1523bb0d0a8eSMike Smith * Generic device interface 1524bb0d0a8eSMike Smith */ 1525bb0d0a8eSMike Smith static int 1526bb0d0a8eSMike Smith pcib_probe(device_t dev) 1527bb0d0a8eSMike Smith { 1528bb0d0a8eSMike Smith if ((pci_get_class(dev) == PCIC_BRIDGE) && 1529bb0d0a8eSMike Smith (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) { 1530bb0d0a8eSMike Smith device_set_desc(dev, "PCI-PCI bridge"); 1531b7cbd25bSMarcel Moolenaar return(-10000); 1532bb0d0a8eSMike Smith } 1533bb0d0a8eSMike Smith return(ENXIO); 1534bb0d0a8eSMike Smith } 1535bb0d0a8eSMike Smith 15366f0d5884SJohn Baldwin void 15376f0d5884SJohn Baldwin pcib_attach_common(device_t dev) 1538bb0d0a8eSMike Smith { 1539bb0d0a8eSMike Smith struct pcib_softc *sc; 1540abf07f13SWarner Losh struct sysctl_ctx_list *sctx; 1541abf07f13SWarner Losh struct sysctl_oid *soid; 1542c825d4dcSJohn Baldwin int comma; 1543bb0d0a8eSMike Smith 1544bb0d0a8eSMike Smith sc = device_get_softc(dev); 1545bb0d0a8eSMike Smith sc->dev = dev; 1546bb0d0a8eSMike Smith 15474fa59183SMike Smith /* 15484fa59183SMike Smith * Get current bridge configuration. 15494fa59183SMike Smith */ 155055aaf894SMarius Strobl sc->domain = pci_get_domain(dev); 1551ad6f36f8SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 1552ad6f36f8SJohn Baldwin sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1); 1553ad6f36f8SJohn Baldwin sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1); 1554ad6f36f8SJohn Baldwin #endif 1555ad6f36f8SJohn Baldwin sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 1556e36af292SJung-uk Kim pcib_cfg_save(sc); 15574fa59183SMike Smith 15584fa59183SMike Smith /* 15594edef187SJohn Baldwin * The primary bus register should always be the bus of the 15604edef187SJohn Baldwin * parent. 15614edef187SJohn Baldwin */ 15624edef187SJohn Baldwin sc->pribus = pci_get_bus(dev); 15634edef187SJohn Baldwin pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1); 15644edef187SJohn Baldwin 15654edef187SJohn Baldwin /* 1566abf07f13SWarner Losh * Setup sysctl reporting nodes 1567abf07f13SWarner Losh */ 1568abf07f13SWarner Losh sctx = device_get_sysctl_ctx(dev); 1569abf07f13SWarner Losh soid = device_get_sysctl_tree(dev); 1570abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain", 1571abf07f13SWarner Losh CTLFLAG_RD, &sc->domain, 0, "Domain number"); 1572abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus", 1573abf07f13SWarner Losh CTLFLAG_RD, &sc->pribus, 0, "Primary bus number"); 1574abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus", 15754edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number"); 1576abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus", 15774edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number"); 1578abf07f13SWarner Losh 1579abf07f13SWarner Losh /* 15804fa59183SMike Smith * Quirk handling. 15814fa59183SMike Smith */ 15824fa59183SMike Smith switch (pci_get_devid(dev)) { 15832ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 15844fa59183SMike Smith case 0x12258086: /* Intel 82454KX/GX (Orion) */ 15854fa59183SMike Smith { 1586b0cb115fSWarner Losh uint8_t supbus; 15874fa59183SMike Smith 15884fa59183SMike Smith supbus = pci_read_config(dev, 0x41, 1); 15894fa59183SMike Smith if (supbus != 0xff) { 15904edef187SJohn Baldwin sc->bus.sec = supbus + 1; 15914edef187SJohn Baldwin sc->bus.sub = supbus + 1; 15924fa59183SMike Smith } 15934fa59183SMike Smith break; 15944fa59183SMike Smith } 15954edef187SJohn Baldwin #endif 15964fa59183SMike Smith 1597e4b59fc5SWarner Losh /* 1598e4b59fc5SWarner Losh * The i82380FB mobile docking controller is a PCI-PCI bridge, 1599e4b59fc5SWarner Losh * and it is a subtractive bridge. However, the ProgIf is wrong 1600e4b59fc5SWarner Losh * so the normal setting of PCIB_SUBTRACTIVE bit doesn't 16014718610dSZbigniew Bodek * happen. There are also Toshiba and Cavium ThunderX bridges 16024718610dSZbigniew Bodek * that behave this way. 1603e4b59fc5SWarner Losh */ 16044718610dSZbigniew Bodek case 0xa002177d: /* Cavium ThunderX */ 1605e4b59fc5SWarner Losh case 0x124b8086: /* Intel 82380FB Mobile */ 1606e4b59fc5SWarner Losh case 0x060513d7: /* Toshiba ???? */ 1607e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 1608e4b59fc5SWarner Losh break; 1609c94d6dbeSJung-uk Kim 16102ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 1611c94d6dbeSJung-uk Kim /* Compaq R3000 BIOS sets wrong subordinate bus number. */ 1612c94d6dbeSJung-uk Kim case 0x00dd10de: 1613c94d6dbeSJung-uk Kim { 1614c94d6dbeSJung-uk Kim char *cp; 1615c94d6dbeSJung-uk Kim 16162be111bfSDavide Italiano if ((cp = kern_getenv("smbios.planar.maker")) == NULL) 1617c94d6dbeSJung-uk Kim break; 16181def0ca6SJung-uk Kim if (strncmp(cp, "Compal", 6) != 0) { 16191def0ca6SJung-uk Kim freeenv(cp); 1620c94d6dbeSJung-uk Kim break; 16211def0ca6SJung-uk Kim } 16221def0ca6SJung-uk Kim freeenv(cp); 16232be111bfSDavide Italiano if ((cp = kern_getenv("smbios.planar.product")) == NULL) 16241def0ca6SJung-uk Kim break; 16251def0ca6SJung-uk Kim if (strncmp(cp, "08A0", 4) != 0) { 16261def0ca6SJung-uk Kim freeenv(cp); 16271def0ca6SJung-uk Kim break; 16281def0ca6SJung-uk Kim } 16291def0ca6SJung-uk Kim freeenv(cp); 16304edef187SJohn Baldwin if (sc->bus.sub < 0xa) { 1631c94d6dbeSJung-uk Kim pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1); 16324edef187SJohn Baldwin sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1); 1633c94d6dbeSJung-uk Kim } 1634c94d6dbeSJung-uk Kim break; 1635c94d6dbeSJung-uk Kim } 16364edef187SJohn Baldwin #endif 1637e4b59fc5SWarner Losh } 1638e4b59fc5SWarner Losh 163922bf1c7fSJohn Baldwin if (pci_msi_device_blacklisted(dev)) 164022bf1c7fSJohn Baldwin sc->flags |= PCIB_DISABLE_MSI; 164122bf1c7fSJohn Baldwin 164268e9cbd3SMarius Strobl if (pci_msix_device_blacklisted(dev)) 164368e9cbd3SMarius Strobl sc->flags |= PCIB_DISABLE_MSIX; 164468e9cbd3SMarius Strobl 1645e4b59fc5SWarner Losh /* 1646e4b59fc5SWarner Losh * Intel 815, 845 and other chipsets say they are PCI-PCI bridges, 1647e4b59fc5SWarner Losh * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM, 1648e4b59fc5SWarner Losh * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese. 1649e4b59fc5SWarner Losh * This means they act as if they were subtractively decoding 1650e4b59fc5SWarner Losh * bridges and pass all transactions. Mark them and real ProgIf 1 1651e4b59fc5SWarner Losh * parts as subtractive. 1652e4b59fc5SWarner Losh */ 1653e4b59fc5SWarner Losh if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 || 1654657d9f9fSJohn Baldwin pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE) 1655e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 1656e4b59fc5SWarner Losh 165782cb5c3bSJohn Baldwin #ifdef PCI_HP 165882cb5c3bSJohn Baldwin pcib_probe_hotplug(sc); 165982cb5c3bSJohn Baldwin #endif 166083c41143SJohn Baldwin #ifdef NEW_PCIB 16614edef187SJohn Baldwin #ifdef PCI_RES_BUS 16624edef187SJohn Baldwin pcib_setup_secbus(dev, &sc->bus, 1); 16634edef187SJohn Baldwin #endif 166483c41143SJohn Baldwin pcib_probe_windows(sc); 166583c41143SJohn Baldwin #endif 166682cb5c3bSJohn Baldwin #ifdef PCI_HP 166782cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG) 166882cb5c3bSJohn Baldwin pcib_setup_hotplug(sc); 166982cb5c3bSJohn Baldwin #endif 1670bb0d0a8eSMike Smith if (bootverbose) { 167155aaf894SMarius Strobl device_printf(dev, " domain %d\n", sc->domain); 16724edef187SJohn Baldwin device_printf(dev, " secondary bus %d\n", sc->bus.sec); 16734edef187SJohn Baldwin device_printf(dev, " subordinate bus %d\n", sc->bus.sub); 167483c41143SJohn Baldwin #ifdef NEW_PCIB 167583c41143SJohn Baldwin if (pcib_is_window_open(&sc->io)) 167683c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%jx-0x%jx\n", 167783c41143SJohn Baldwin (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit); 167883c41143SJohn Baldwin if (pcib_is_window_open(&sc->mem)) 167983c41143SJohn Baldwin device_printf(dev, " memory decode 0x%jx-0x%jx\n", 168083c41143SJohn Baldwin (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit); 168183c41143SJohn Baldwin if (pcib_is_window_open(&sc->pmem)) 168283c41143SJohn Baldwin device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 168383c41143SJohn Baldwin (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit); 168483c41143SJohn Baldwin #else 168583c41143SJohn Baldwin if (pcib_is_io_open(sc)) 168683c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%x-0x%x\n", 168783c41143SJohn Baldwin sc->iobase, sc->iolimit); 1688b0a2d4b8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 1689b0a2d4b8SWarner Losh device_printf(dev, " memory decode 0x%jx-0x%jx\n", 1690b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit); 1691b0a2d4b8SWarner Losh if (pcib_is_prefetch_open(sc)) 1692b0a2d4b8SWarner Losh device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 1693b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 169483c41143SJohn Baldwin #endif 1695c825d4dcSJohn Baldwin if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) || 1696c825d4dcSJohn Baldwin sc->flags & PCIB_SUBTRACTIVE) { 1697c825d4dcSJohn Baldwin device_printf(dev, " special decode "); 1698c825d4dcSJohn Baldwin comma = 0; 1699c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) { 1700c825d4dcSJohn Baldwin printf("ISA"); 1701c825d4dcSJohn Baldwin comma = 1; 1702c825d4dcSJohn Baldwin } 1703c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) { 1704c825d4dcSJohn Baldwin printf("%sVGA", comma ? ", " : ""); 1705c825d4dcSJohn Baldwin comma = 1; 1706c825d4dcSJohn Baldwin } 1707e4b59fc5SWarner Losh if (sc->flags & PCIB_SUBTRACTIVE) 1708c825d4dcSJohn Baldwin printf("%ssubtractive", comma ? ", " : ""); 1709c825d4dcSJohn Baldwin printf("\n"); 1710c825d4dcSJohn Baldwin } 1711bb0d0a8eSMike Smith } 1712bb0d0a8eSMike Smith 1713bb0d0a8eSMike Smith /* 1714ef888152SJohn Baldwin * Always enable busmastering on bridges so that transactions 1715ef888152SJohn Baldwin * initiated on the secondary bus are passed through to the 1716ef888152SJohn Baldwin * primary bus. 1717ef888152SJohn Baldwin */ 1718ef888152SJohn Baldwin pci_enable_busmaster(dev); 17196f0d5884SJohn Baldwin } 1720bb0d0a8eSMike Smith 172182cb5c3bSJohn Baldwin #ifdef PCI_HP 172282cb5c3bSJohn Baldwin static int 172382cb5c3bSJohn Baldwin pcib_present(struct pcib_softc *sc) 172482cb5c3bSJohn Baldwin { 172582cb5c3bSJohn Baldwin 172682cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG) 172782cb5c3bSJohn Baldwin return (pcib_hotplug_present(sc) != 0); 172882cb5c3bSJohn Baldwin return (1); 172982cb5c3bSJohn Baldwin } 173082cb5c3bSJohn Baldwin #endif 173182cb5c3bSJohn Baldwin 173238906aedSJohn Baldwin int 173367e7d085SJohn Baldwin pcib_attach_child(device_t dev) 17346f0d5884SJohn Baldwin { 17356f0d5884SJohn Baldwin struct pcib_softc *sc; 17366f0d5884SJohn Baldwin 17376f0d5884SJohn Baldwin sc = device_get_softc(dev); 173867e7d085SJohn Baldwin if (sc->bus.sec == 0) { 173967e7d085SJohn Baldwin /* no secondary bus; we should have fixed this */ 174067e7d085SJohn Baldwin return(0); 174167e7d085SJohn Baldwin } 174267e7d085SJohn Baldwin 174382cb5c3bSJohn Baldwin #ifdef PCI_HP 174482cb5c3bSJohn Baldwin if (!pcib_present(sc)) { 174582cb5c3bSJohn Baldwin /* An empty HotPlug slot, so don't add a PCI bus yet. */ 174682cb5c3bSJohn Baldwin return (0); 174782cb5c3bSJohn Baldwin } 174882cb5c3bSJohn Baldwin #endif 174982cb5c3bSJohn Baldwin 175067e7d085SJohn Baldwin sc->child = device_add_child(dev, "pci", -1); 1751bb0d0a8eSMike Smith return (bus_generic_attach(dev)); 1752bb0d0a8eSMike Smith } 1753bb0d0a8eSMike Smith 175467e7d085SJohn Baldwin int 175567e7d085SJohn Baldwin pcib_attach(device_t dev) 175667e7d085SJohn Baldwin { 175767e7d085SJohn Baldwin 175867e7d085SJohn Baldwin pcib_attach_common(dev); 175967e7d085SJohn Baldwin return (pcib_attach_child(dev)); 1760bb0d0a8eSMike Smith } 1761bb0d0a8eSMike Smith 17626f0d5884SJohn Baldwin int 17636f33eaa5SJohn Baldwin pcib_detach(device_t dev) 17646f33eaa5SJohn Baldwin { 17656f33eaa5SJohn Baldwin #if defined(PCI_HP) || defined(NEW_PCIB) 17666f33eaa5SJohn Baldwin struct pcib_softc *sc; 17676f33eaa5SJohn Baldwin #endif 17686f33eaa5SJohn Baldwin int error; 17696f33eaa5SJohn Baldwin 17706f33eaa5SJohn Baldwin #if defined(PCI_HP) || defined(NEW_PCIB) 17716f33eaa5SJohn Baldwin sc = device_get_softc(dev); 17726f33eaa5SJohn Baldwin #endif 17736f33eaa5SJohn Baldwin error = bus_generic_detach(dev); 17746f33eaa5SJohn Baldwin if (error) 17756f33eaa5SJohn Baldwin return (error); 17766f33eaa5SJohn Baldwin #ifdef PCI_HP 17776f33eaa5SJohn Baldwin if (sc->flags & PCIB_HOTPLUG) { 17786f33eaa5SJohn Baldwin error = pcib_detach_hotplug(sc); 17796f33eaa5SJohn Baldwin if (error) 17806f33eaa5SJohn Baldwin return (error); 17816f33eaa5SJohn Baldwin } 17826f33eaa5SJohn Baldwin #endif 17836f33eaa5SJohn Baldwin error = device_delete_children(dev); 17846f33eaa5SJohn Baldwin if (error) 17856f33eaa5SJohn Baldwin return (error); 17866f33eaa5SJohn Baldwin #ifdef NEW_PCIB 17876f33eaa5SJohn Baldwin pcib_free_windows(sc); 17886f33eaa5SJohn Baldwin #ifdef PCI_RES_BUS 17896f33eaa5SJohn Baldwin pcib_free_secbus(dev, &sc->bus); 17906f33eaa5SJohn Baldwin #endif 17916f33eaa5SJohn Baldwin #endif 17926f33eaa5SJohn Baldwin return (0); 17936f33eaa5SJohn Baldwin } 17946f33eaa5SJohn Baldwin 17956f33eaa5SJohn Baldwin int 1796e36af292SJung-uk Kim pcib_suspend(device_t dev) 1797e36af292SJung-uk Kim { 1798e36af292SJung-uk Kim 1799e36af292SJung-uk Kim pcib_cfg_save(device_get_softc(dev)); 18007212fc6aSJohn Baldwin return (bus_generic_suspend(dev)); 1801e36af292SJung-uk Kim } 1802e36af292SJung-uk Kim 1803e36af292SJung-uk Kim int 1804e36af292SJung-uk Kim pcib_resume(device_t dev) 1805e36af292SJung-uk Kim { 1806e36af292SJung-uk Kim 1807e36af292SJung-uk Kim pcib_cfg_restore(device_get_softc(dev)); 1808cffd37daSAndriy Gapon 1809cffd37daSAndriy Gapon /* 1810cffd37daSAndriy Gapon * Restore the Command register only after restoring the windows. 1811cffd37daSAndriy Gapon * The bridge should not be claiming random windows. 1812cffd37daSAndriy Gapon */ 1813cffd37daSAndriy Gapon pci_write_config(dev, PCIR_COMMAND, pci_get_cmdreg(dev), 2); 1814e36af292SJung-uk Kim return (bus_generic_resume(dev)); 1815e36af292SJung-uk Kim } 1816e36af292SJung-uk Kim 1817809923caSJustin Hibbits void 1818809923caSJustin Hibbits pcib_bridge_init(device_t dev) 1819809923caSJustin Hibbits { 1820809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 1821809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2); 1822809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1); 1823809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2); 1824809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2); 1825809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2); 1826809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 1827809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4); 1828809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2); 1829809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4); 1830809923caSJustin Hibbits } 1831809923caSJustin Hibbits 1832e36af292SJung-uk Kim int 183382cb5c3bSJohn Baldwin pcib_child_present(device_t dev, device_t child) 183482cb5c3bSJohn Baldwin { 183582cb5c3bSJohn Baldwin #ifdef PCI_HP 183682cb5c3bSJohn Baldwin struct pcib_softc *sc = device_get_softc(dev); 183782cb5c3bSJohn Baldwin int retval; 183882cb5c3bSJohn Baldwin 183982cb5c3bSJohn Baldwin retval = bus_child_present(dev); 184082cb5c3bSJohn Baldwin if (retval != 0 && sc->flags & PCIB_HOTPLUG) 184182cb5c3bSJohn Baldwin retval = pcib_hotplug_present(sc); 184282cb5c3bSJohn Baldwin return (retval); 184382cb5c3bSJohn Baldwin #else 184482cb5c3bSJohn Baldwin return (bus_child_present(dev)); 184582cb5c3bSJohn Baldwin #endif 184682cb5c3bSJohn Baldwin } 184782cb5c3bSJohn Baldwin 184882cb5c3bSJohn Baldwin int 1849bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 1850bb0d0a8eSMike Smith { 1851bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 1852bb0d0a8eSMike Smith 1853bb0d0a8eSMike Smith switch (which) { 185455aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 185555aaf894SMarius Strobl *result = sc->domain; 185655aaf894SMarius Strobl return(0); 1857bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 18584edef187SJohn Baldwin *result = sc->bus.sec; 1859bb0d0a8eSMike Smith return(0); 1860bb0d0a8eSMike Smith } 1861bb0d0a8eSMike Smith return(ENOENT); 1862bb0d0a8eSMike Smith } 1863bb0d0a8eSMike Smith 18646f0d5884SJohn Baldwin int 1865bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 1866bb0d0a8eSMike Smith { 1867bb0d0a8eSMike Smith 1868bb0d0a8eSMike Smith switch (which) { 186955aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 187055aaf894SMarius Strobl return(EINVAL); 1871bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 18724edef187SJohn Baldwin return(EINVAL); 1873bb0d0a8eSMike Smith } 1874bb0d0a8eSMike Smith return(ENOENT); 1875bb0d0a8eSMike Smith } 1876bb0d0a8eSMike Smith 187783c41143SJohn Baldwin #ifdef NEW_PCIB 187883c41143SJohn Baldwin /* 187983c41143SJohn Baldwin * Attempt to allocate a resource from the existing resources assigned 188083c41143SJohn Baldwin * to a window. 188183c41143SJohn Baldwin */ 188283c41143SJohn Baldwin static struct resource * 188383c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w, 18842dd1bdf1SJustin Hibbits device_t child, int type, int *rid, rman_res_t start, rman_res_t end, 18852dd1bdf1SJustin Hibbits rman_res_t count, u_int flags) 188683c41143SJohn Baldwin { 188783c41143SJohn Baldwin struct resource *res; 188883c41143SJohn Baldwin 188983c41143SJohn Baldwin if (!pcib_is_window_open(w)) 189083c41143SJohn Baldwin return (NULL); 189183c41143SJohn Baldwin 189283c41143SJohn Baldwin res = rman_reserve_resource(&w->rman, start, end, count, 189383c41143SJohn Baldwin flags & ~RF_ACTIVE, child); 189483c41143SJohn Baldwin if (res == NULL) 189583c41143SJohn Baldwin return (NULL); 189683c41143SJohn Baldwin 189783c41143SJohn Baldwin if (bootverbose) 189883c41143SJohn Baldwin device_printf(sc->dev, 1899da1b038aSJustin Hibbits "allocated %s range (%#jx-%#jx) for rid %x of %s\n", 190083c41143SJohn Baldwin w->name, rman_get_start(res), rman_get_end(res), *rid, 190183c41143SJohn Baldwin pcib_child_name(child)); 190283c41143SJohn Baldwin rman_set_rid(res, *rid); 190383c41143SJohn Baldwin 190483c41143SJohn Baldwin /* 190583c41143SJohn Baldwin * If the resource should be active, pass that request up the 190683c41143SJohn Baldwin * tree. This assumes the parent drivers can handle 190783c41143SJohn Baldwin * activating sub-allocated resources. 190883c41143SJohn Baldwin */ 190983c41143SJohn Baldwin if (flags & RF_ACTIVE) { 191083c41143SJohn Baldwin if (bus_activate_resource(child, type, *rid, res) != 0) { 191183c41143SJohn Baldwin rman_release_resource(res); 191283c41143SJohn Baldwin return (NULL); 191383c41143SJohn Baldwin } 191483c41143SJohn Baldwin } 191583c41143SJohn Baldwin 191683c41143SJohn Baldwin return (res); 191783c41143SJohn Baldwin } 191883c41143SJohn Baldwin 1919c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */ 1920c825d4dcSJohn Baldwin static int 1921c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type, 19222dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 1923c825d4dcSJohn Baldwin { 1924c825d4dcSJohn Baldwin struct resource *res; 19252dd1bdf1SJustin Hibbits rman_res_t base, limit, wmask; 1926c825d4dcSJohn Baldwin int rid; 1927c825d4dcSJohn Baldwin 1928c825d4dcSJohn Baldwin /* 1929c825d4dcSJohn Baldwin * If this is an I/O window on a bridge with ISA enable set 1930c825d4dcSJohn Baldwin * and the start address is below 64k, then try to allocate an 1931c825d4dcSJohn Baldwin * initial window of 0x1000 bytes long starting at address 1932c825d4dcSJohn Baldwin * 0xf000 and walking down. Note that if the original request 1933c825d4dcSJohn Baldwin * was larger than the non-aliased range size of 0x100 our 1934c825d4dcSJohn Baldwin * caller would have raised the start address up to 64k 1935c825d4dcSJohn Baldwin * already. 1936c825d4dcSJohn Baldwin */ 1937c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1938c825d4dcSJohn Baldwin start < 65536) { 1939c825d4dcSJohn Baldwin for (base = 0xf000; (long)base >= 0; base -= 0x1000) { 1940c825d4dcSJohn Baldwin limit = base + 0xfff; 1941c825d4dcSJohn Baldwin 1942c825d4dcSJohn Baldwin /* 1943c825d4dcSJohn Baldwin * Skip ranges that wouldn't work for the 1944c825d4dcSJohn Baldwin * original request. Note that the actual 1945c825d4dcSJohn Baldwin * window that overlaps are the non-alias 1946c825d4dcSJohn Baldwin * ranges within [base, limit], so this isn't 1947c825d4dcSJohn Baldwin * quite a simple comparison. 1948c825d4dcSJohn Baldwin */ 1949c825d4dcSJohn Baldwin if (start + count > limit - 0x400) 1950c825d4dcSJohn Baldwin continue; 1951c825d4dcSJohn Baldwin if (base == 0) { 1952c825d4dcSJohn Baldwin /* 1953c825d4dcSJohn Baldwin * The first open region for the window at 1954c825d4dcSJohn Baldwin * 0 is 0x400-0x4ff. 1955c825d4dcSJohn Baldwin */ 1956c825d4dcSJohn Baldwin if (end - count + 1 < 0x400) 1957c825d4dcSJohn Baldwin continue; 1958c825d4dcSJohn Baldwin } else { 1959c825d4dcSJohn Baldwin if (end - count + 1 < base) 1960c825d4dcSJohn Baldwin continue; 1961c825d4dcSJohn Baldwin } 1962c825d4dcSJohn Baldwin 1963c825d4dcSJohn Baldwin if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) { 1964c825d4dcSJohn Baldwin w->base = base; 1965c825d4dcSJohn Baldwin w->limit = limit; 1966c825d4dcSJohn Baldwin return (0); 1967c825d4dcSJohn Baldwin } 1968c825d4dcSJohn Baldwin } 1969c825d4dcSJohn Baldwin return (ENOSPC); 1970c825d4dcSJohn Baldwin } 1971c825d4dcSJohn Baldwin 197289977ce2SJustin Hibbits wmask = ((rman_res_t)1 << w->step) - 1; 1973c825d4dcSJohn Baldwin if (RF_ALIGNMENT(flags) < w->step) { 1974c825d4dcSJohn Baldwin flags &= ~RF_ALIGNMENT_MASK; 1975c825d4dcSJohn Baldwin flags |= RF_ALIGNMENT_LOG2(w->step); 1976c825d4dcSJohn Baldwin } 1977c825d4dcSJohn Baldwin start &= ~wmask; 1978c825d4dcSJohn Baldwin end |= wmask; 197989977ce2SJustin Hibbits count = roundup2(count, (rman_res_t)1 << w->step); 1980c825d4dcSJohn Baldwin rid = w->reg; 1981c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, start, end, count, 1982c825d4dcSJohn Baldwin flags & ~RF_ACTIVE); 1983c825d4dcSJohn Baldwin if (res == NULL) 1984c825d4dcSJohn Baldwin return (ENOSPC); 1985c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 1986c825d4dcSJohn Baldwin pcib_activate_window(sc, type); 1987c825d4dcSJohn Baldwin w->base = rman_get_start(res); 1988c825d4dcSJohn Baldwin w->limit = rman_get_end(res); 1989c825d4dcSJohn Baldwin return (0); 1990c825d4dcSJohn Baldwin } 1991c825d4dcSJohn Baldwin 1992c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */ 1993c825d4dcSJohn Baldwin static int 1994c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type, 19952dd1bdf1SJustin Hibbits rman_res_t base, rman_res_t limit) 1996c825d4dcSJohn Baldwin { 1997c825d4dcSJohn Baldwin struct resource *res; 1998c825d4dcSJohn Baldwin int error, i, force_64k_base; 1999c825d4dcSJohn Baldwin 2000c825d4dcSJohn Baldwin KASSERT(base <= w->base && limit >= w->limit, 2001c825d4dcSJohn Baldwin ("attempting to shrink window")); 2002c825d4dcSJohn Baldwin 2003c825d4dcSJohn Baldwin /* 2004c825d4dcSJohn Baldwin * XXX: pcib_grow_window() doesn't try to do this anyway and 2005c825d4dcSJohn Baldwin * the error handling for all the edge cases would be tedious. 2006c825d4dcSJohn Baldwin */ 2007c825d4dcSJohn Baldwin KASSERT(limit == w->limit || base == w->base, 2008c825d4dcSJohn Baldwin ("attempting to grow both ends of a window")); 2009c825d4dcSJohn Baldwin 2010c825d4dcSJohn Baldwin /* 2011c825d4dcSJohn Baldwin * Yet more special handling for requests to expand an I/O 2012c825d4dcSJohn Baldwin * window behind an ISA-enabled bridge. Since I/O windows 2013c825d4dcSJohn Baldwin * have to grow in 0x1000 increments and the end of the 0xffff 2014c825d4dcSJohn Baldwin * range is an alias, growing a window below 64k will always 2015c825d4dcSJohn Baldwin * result in allocating new resources and never adjusting an 2016c825d4dcSJohn Baldwin * existing resource. 2017c825d4dcSJohn Baldwin */ 2018c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 2019c825d4dcSJohn Baldwin (limit <= 65535 || (base <= 65535 && base != w->base))) { 2020c825d4dcSJohn Baldwin KASSERT(limit == w->limit || limit <= 65535, 2021c825d4dcSJohn Baldwin ("attempting to grow both ends across 64k ISA alias")); 2022c825d4dcSJohn Baldwin 2023c825d4dcSJohn Baldwin if (base != w->base) 2024c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1); 2025c825d4dcSJohn Baldwin else 2026c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, w->limit + 1, 2027c825d4dcSJohn Baldwin limit); 2028c825d4dcSJohn Baldwin if (error == 0) { 2029c825d4dcSJohn Baldwin w->base = base; 2030c825d4dcSJohn Baldwin w->limit = limit; 2031c825d4dcSJohn Baldwin } 2032c825d4dcSJohn Baldwin return (error); 2033c825d4dcSJohn Baldwin } 2034c825d4dcSJohn Baldwin 2035c825d4dcSJohn Baldwin /* 2036c825d4dcSJohn Baldwin * Find the existing resource to adjust. Usually there is only one, 2037c825d4dcSJohn Baldwin * but for an ISA-enabled bridge we might be growing the I/O window 2038c825d4dcSJohn Baldwin * above 64k and need to find the existing resource that maps all 2039c825d4dcSJohn Baldwin * of the area above 64k. 2040c825d4dcSJohn Baldwin */ 2041c825d4dcSJohn Baldwin for (i = 0; i < w->count; i++) { 2042c825d4dcSJohn Baldwin if (rman_get_end(w->res[i]) == w->limit) 2043c825d4dcSJohn Baldwin break; 2044c825d4dcSJohn Baldwin } 2045c825d4dcSJohn Baldwin KASSERT(i != w->count, ("did not find existing resource")); 2046c825d4dcSJohn Baldwin res = w->res[i]; 2047c825d4dcSJohn Baldwin 2048c825d4dcSJohn Baldwin /* 2049c825d4dcSJohn Baldwin * Usually the resource we found should match the window's 2050c825d4dcSJohn Baldwin * existing range. The one exception is the ISA-enabled case 2051c825d4dcSJohn Baldwin * mentioned above in which case the resource should start at 2052c825d4dcSJohn Baldwin * 64k. 2053c825d4dcSJohn Baldwin */ 2054c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 2055c825d4dcSJohn Baldwin w->base <= 65535) { 2056c825d4dcSJohn Baldwin KASSERT(rman_get_start(res) == 65536, 2057c825d4dcSJohn Baldwin ("existing resource mismatch")); 2058c825d4dcSJohn Baldwin force_64k_base = 1; 2059c825d4dcSJohn Baldwin } else { 2060c825d4dcSJohn Baldwin KASSERT(w->base == rman_get_start(res), 2061c825d4dcSJohn Baldwin ("existing resource mismatch")); 2062c825d4dcSJohn Baldwin force_64k_base = 0; 2063c825d4dcSJohn Baldwin } 2064c825d4dcSJohn Baldwin 2065c825d4dcSJohn Baldwin error = bus_adjust_resource(sc->dev, type, res, force_64k_base ? 2066c825d4dcSJohn Baldwin rman_get_start(res) : base, limit); 2067c825d4dcSJohn Baldwin if (error) 2068c825d4dcSJohn Baldwin return (error); 2069c825d4dcSJohn Baldwin 2070c825d4dcSJohn Baldwin /* Add the newly allocated region to the resource manager. */ 2071c825d4dcSJohn Baldwin if (w->base != base) { 2072c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, base, w->base - 1); 2073c825d4dcSJohn Baldwin w->base = base; 2074c825d4dcSJohn Baldwin } else { 2075c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, w->limit + 1, limit); 2076c825d4dcSJohn Baldwin w->limit = limit; 2077c825d4dcSJohn Baldwin } 2078c825d4dcSJohn Baldwin if (error) { 2079c825d4dcSJohn Baldwin if (bootverbose) 2080c825d4dcSJohn Baldwin device_printf(sc->dev, 2081c825d4dcSJohn Baldwin "failed to expand %s resource manager\n", w->name); 2082c825d4dcSJohn Baldwin (void)bus_adjust_resource(sc->dev, type, res, force_64k_base ? 2083c825d4dcSJohn Baldwin rman_get_start(res) : w->base, w->limit); 2084c825d4dcSJohn Baldwin } 2085c825d4dcSJohn Baldwin return (error); 2086c825d4dcSJohn Baldwin } 2087c825d4dcSJohn Baldwin 208883c41143SJohn Baldwin /* 208983c41143SJohn Baldwin * Attempt to grow a window to make room for a given resource request. 209083c41143SJohn Baldwin */ 209183c41143SJohn Baldwin static int 209283c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type, 20932dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 209483c41143SJohn Baldwin { 20952dd1bdf1SJustin Hibbits rman_res_t align, start_free, end_free, front, back, wmask; 2096c825d4dcSJohn Baldwin int error; 209783c41143SJohn Baldwin 209883c41143SJohn Baldwin /* 209983c41143SJohn Baldwin * Clamp the desired resource range to the maximum address 210083c41143SJohn Baldwin * this window supports. Reject impossible requests. 2101c825d4dcSJohn Baldwin * 2102c825d4dcSJohn Baldwin * For I/O port requests behind a bridge with the ISA enable 2103c825d4dcSJohn Baldwin * bit set, force large allocations to start above 64k. 210483c41143SJohn Baldwin */ 210583c41143SJohn Baldwin if (!w->valid) 210683c41143SJohn Baldwin return (EINVAL); 2107c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 && 2108c825d4dcSJohn Baldwin start < 65536) 2109c825d4dcSJohn Baldwin start = 65536; 211083c41143SJohn Baldwin if (end > w->rman.rm_end) 211183c41143SJohn Baldwin end = w->rman.rm_end; 211283c41143SJohn Baldwin if (start + count - 1 > end || start + count < start) 211383c41143SJohn Baldwin return (EINVAL); 211489977ce2SJustin Hibbits wmask = ((rman_res_t)1 << w->step) - 1; 211583c41143SJohn Baldwin 211683c41143SJohn Baldwin /* 211783c41143SJohn Baldwin * If there is no resource at all, just try to allocate enough 211883c41143SJohn Baldwin * aligned space for this resource. 211983c41143SJohn Baldwin */ 212083c41143SJohn Baldwin if (w->res == NULL) { 2121c825d4dcSJohn Baldwin error = pcib_alloc_new_window(sc, w, type, start, end, count, 2122c825d4dcSJohn Baldwin flags); 2123c825d4dcSJohn Baldwin if (error) { 212483c41143SJohn Baldwin if (bootverbose) 212583c41143SJohn Baldwin device_printf(sc->dev, 2126da1b038aSJustin Hibbits "failed to allocate initial %s window (%#jx-%#jx,%#jx)\n", 212783c41143SJohn Baldwin w->name, start, end, count); 212883c41143SJohn Baldwin return (error); 212983c41143SJohn Baldwin } 2130c825d4dcSJohn Baldwin if (bootverbose) 2131c825d4dcSJohn Baldwin device_printf(sc->dev, 2132c825d4dcSJohn Baldwin "allocated initial %s window of %#jx-%#jx\n", 2133c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 213483c41143SJohn Baldwin goto updatewin; 213583c41143SJohn Baldwin } 213683c41143SJohn Baldwin 213783c41143SJohn Baldwin /* 213883c41143SJohn Baldwin * See if growing the window would help. Compute the minimum 213983c41143SJohn Baldwin * amount of address space needed on both the front and back 214083c41143SJohn Baldwin * ends of the existing window to satisfy the allocation. 214183c41143SJohn Baldwin * 214283c41143SJohn Baldwin * For each end, build a candidate region adjusting for the 214383c41143SJohn Baldwin * required alignment, etc. If there is a free region at the 214483c41143SJohn Baldwin * edge of the window, grow from the inner edge of the free 214583c41143SJohn Baldwin * region. Otherwise grow from the window boundary. 214683c41143SJohn Baldwin * 2147c825d4dcSJohn Baldwin * Growing an I/O window below 64k for a bridge with the ISA 2148c825d4dcSJohn Baldwin * enable bit doesn't require any special magic as the step 2149c825d4dcSJohn Baldwin * size of an I/O window (1k) always includes multiple 2150c825d4dcSJohn Baldwin * non-alias ranges when it is grown in either direction. 2151c825d4dcSJohn Baldwin * 215283c41143SJohn Baldwin * XXX: Special case: if w->res is completely empty and the 215383c41143SJohn Baldwin * request size is larger than w->res, we should find the 215483c41143SJohn Baldwin * optimal aligned buffer containing w->res and allocate that. 215583c41143SJohn Baldwin */ 215683c41143SJohn Baldwin if (bootverbose) 215783c41143SJohn Baldwin device_printf(sc->dev, 2158da1b038aSJustin Hibbits "attempting to grow %s window for (%#jx-%#jx,%#jx)\n", 215983c41143SJohn Baldwin w->name, start, end, count); 216089977ce2SJustin Hibbits align = (rman_res_t)1 << RF_ALIGNMENT(flags); 2161c825d4dcSJohn Baldwin if (start < w->base) { 216283c41143SJohn Baldwin if (rman_first_free_region(&w->rman, &start_free, &end_free) != 2163c825d4dcSJohn Baldwin 0 || start_free != w->base) 2164c825d4dcSJohn Baldwin end_free = w->base; 216583c41143SJohn Baldwin if (end_free > end) 2166ddac8cc9SJohn Baldwin end_free = end + 1; 216783c41143SJohn Baldwin 216883c41143SJohn Baldwin /* Move end_free down until it is properly aligned. */ 216983c41143SJohn Baldwin end_free &= ~(align - 1); 2170a49dcb46SJohn Baldwin end_free--; 2171a49dcb46SJohn Baldwin front = end_free - (count - 1); 217283c41143SJohn Baldwin 217383c41143SJohn Baldwin /* 217483c41143SJohn Baldwin * The resource would now be allocated at (front, 217583c41143SJohn Baldwin * end_free). Ensure that fits in the (start, end) 217683c41143SJohn Baldwin * bounds. end_free is checked above. If 'front' is 217783c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 217883c41143SJohn Baldwin * Also check for underflow. 217983c41143SJohn Baldwin */ 218083c41143SJohn Baldwin if (front >= start && front <= end_free) { 218183c41143SJohn Baldwin if (bootverbose) 2182da1b038aSJustin Hibbits printf("\tfront candidate range: %#jx-%#jx\n", 218383c41143SJohn Baldwin front, end_free); 2184a7b5acacSJohn Baldwin front &= ~wmask; 2185c825d4dcSJohn Baldwin front = w->base - front; 218683c41143SJohn Baldwin } else 218783c41143SJohn Baldwin front = 0; 218883c41143SJohn Baldwin } else 218983c41143SJohn Baldwin front = 0; 2190c825d4dcSJohn Baldwin if (end > w->limit) { 219183c41143SJohn Baldwin if (rman_last_free_region(&w->rman, &start_free, &end_free) != 2192c825d4dcSJohn Baldwin 0 || end_free != w->limit) 2193c825d4dcSJohn Baldwin start_free = w->limit + 1; 219483c41143SJohn Baldwin if (start_free < start) 219583c41143SJohn Baldwin start_free = start; 219683c41143SJohn Baldwin 219783c41143SJohn Baldwin /* Move start_free up until it is properly aligned. */ 219883c41143SJohn Baldwin start_free = roundup2(start_free, align); 2199a49dcb46SJohn Baldwin back = start_free + count - 1; 220083c41143SJohn Baldwin 220183c41143SJohn Baldwin /* 220283c41143SJohn Baldwin * The resource would now be allocated at (start_free, 220383c41143SJohn Baldwin * back). Ensure that fits in the (start, end) 220483c41143SJohn Baldwin * bounds. start_free is checked above. If 'back' is 220583c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 220683c41143SJohn Baldwin * Also check for overflow. 220783c41143SJohn Baldwin */ 220883c41143SJohn Baldwin if (back <= end && start_free <= back) { 220983c41143SJohn Baldwin if (bootverbose) 2210da1b038aSJustin Hibbits printf("\tback candidate range: %#jx-%#jx\n", 221183c41143SJohn Baldwin start_free, back); 2212a7b5acacSJohn Baldwin back |= wmask; 2213c825d4dcSJohn Baldwin back -= w->limit; 221483c41143SJohn Baldwin } else 221583c41143SJohn Baldwin back = 0; 221683c41143SJohn Baldwin } else 221783c41143SJohn Baldwin back = 0; 221883c41143SJohn Baldwin 221983c41143SJohn Baldwin /* 222083c41143SJohn Baldwin * Try to allocate the smallest needed region first. 222183c41143SJohn Baldwin * If that fails, fall back to the other region. 222283c41143SJohn Baldwin */ 222383c41143SJohn Baldwin error = ENOSPC; 222483c41143SJohn Baldwin while (front != 0 || back != 0) { 222583c41143SJohn Baldwin if (front != 0 && (front <= back || back == 0)) { 2226c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base - front, 2227c825d4dcSJohn Baldwin w->limit); 222883c41143SJohn Baldwin if (error == 0) 222983c41143SJohn Baldwin break; 223083c41143SJohn Baldwin front = 0; 223183c41143SJohn Baldwin } else { 2232c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base, 2233c825d4dcSJohn Baldwin w->limit + back); 223483c41143SJohn Baldwin if (error == 0) 223583c41143SJohn Baldwin break; 223683c41143SJohn Baldwin back = 0; 223783c41143SJohn Baldwin } 223883c41143SJohn Baldwin } 223983c41143SJohn Baldwin 224083c41143SJohn Baldwin if (error) 224183c41143SJohn Baldwin return (error); 224283c41143SJohn Baldwin if (bootverbose) 2243c825d4dcSJohn Baldwin device_printf(sc->dev, "grew %s window to %#jx-%#jx\n", 2244c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 224583c41143SJohn Baldwin 224683c41143SJohn Baldwin updatewin: 2247c825d4dcSJohn Baldwin /* Write the new window. */ 2248a7b5acacSJohn Baldwin KASSERT((w->base & wmask) == 0, ("start address is not aligned")); 2249a7b5acacSJohn Baldwin KASSERT((w->limit & wmask) == wmask, ("end address is not aligned")); 225083c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 225183c41143SJohn Baldwin return (0); 225283c41143SJohn Baldwin } 225383c41143SJohn Baldwin 225483c41143SJohn Baldwin /* 225583c41143SJohn Baldwin * We have to trap resource allocation requests and ensure that the bridge 225683c41143SJohn Baldwin * is set up to, or capable of handling them. 225783c41143SJohn Baldwin */ 225883c41143SJohn Baldwin struct resource * 225983c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 22602dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 226183c41143SJohn Baldwin { 226283c41143SJohn Baldwin struct pcib_softc *sc; 226383c41143SJohn Baldwin struct resource *r; 226483c41143SJohn Baldwin 226583c41143SJohn Baldwin sc = device_get_softc(dev); 226683c41143SJohn Baldwin 226783c41143SJohn Baldwin /* 226883c41143SJohn Baldwin * VGA resources are decoded iff the VGA enable bit is set in 226983c41143SJohn Baldwin * the bridge control register. VGA resources do not fall into 227083c41143SJohn Baldwin * the resource windows and are passed up to the parent. 227183c41143SJohn Baldwin */ 227283c41143SJohn Baldwin if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) || 227383c41143SJohn Baldwin (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) { 227483c41143SJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) 227583c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, 227683c41143SJohn Baldwin rid, start, end, count, flags)); 227783c41143SJohn Baldwin else 227883c41143SJohn Baldwin return (NULL); 227983c41143SJohn Baldwin } 228083c41143SJohn Baldwin 228183c41143SJohn Baldwin switch (type) { 22824edef187SJohn Baldwin #ifdef PCI_RES_BUS 22834edef187SJohn Baldwin case PCI_RES_BUS: 22844edef187SJohn Baldwin return (pcib_alloc_subbus(&sc->bus, child, rid, start, end, 22854edef187SJohn Baldwin count, flags)); 22864edef187SJohn Baldwin #endif 228783c41143SJohn Baldwin case SYS_RES_IOPORT: 2288c825d4dcSJohn Baldwin if (pcib_is_isa_range(sc, start, end, count)) 2289c825d4dcSJohn Baldwin return (NULL); 229083c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start, 229183c41143SJohn Baldwin end, count, flags); 2292a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 229383c41143SJohn Baldwin break; 229483c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->io, type, start, end, count, 229583c41143SJohn Baldwin flags) == 0) 229683c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, 229783c41143SJohn Baldwin rid, start, end, count, flags); 229883c41143SJohn Baldwin break; 229983c41143SJohn Baldwin case SYS_RES_MEMORY: 230083c41143SJohn Baldwin /* 230183c41143SJohn Baldwin * For prefetchable resources, prefer the prefetchable 230283c41143SJohn Baldwin * memory window, but fall back to the regular memory 230383c41143SJohn Baldwin * window if that fails. Try both windows before 230483c41143SJohn Baldwin * attempting to grow a window in case the firmware 230583c41143SJohn Baldwin * has used a range in the regular memory window to 230683c41143SJohn Baldwin * map a prefetchable BAR. 230783c41143SJohn Baldwin */ 230883c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 230983c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, type, 231083c41143SJohn Baldwin rid, start, end, count, flags); 231183c41143SJohn Baldwin if (r != NULL) 231283c41143SJohn Baldwin break; 231383c41143SJohn Baldwin } 231483c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid, 231583c41143SJohn Baldwin start, end, count, flags); 2316a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 231783c41143SJohn Baldwin break; 231883c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 231983c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->pmem, type, start, end, 232083c41143SJohn Baldwin count, flags) == 0) { 232183c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, 232283c41143SJohn Baldwin type, rid, start, end, count, flags); 232383c41143SJohn Baldwin if (r != NULL) 232483c41143SJohn Baldwin break; 232583c41143SJohn Baldwin } 232683c41143SJohn Baldwin } 232783c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->mem, type, start, end, count, 232883c41143SJohn Baldwin flags & ~RF_PREFETCHABLE) == 0) 232983c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, 233083c41143SJohn Baldwin rid, start, end, count, flags); 233183c41143SJohn Baldwin break; 233283c41143SJohn Baldwin default: 233383c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 233483c41143SJohn Baldwin start, end, count, flags)); 233583c41143SJohn Baldwin } 233683c41143SJohn Baldwin 233783c41143SJohn Baldwin /* 233883c41143SJohn Baldwin * If attempts to suballocate from the window fail but this is a 233983c41143SJohn Baldwin * subtractive bridge, pass the request up the tree. 234083c41143SJohn Baldwin */ 234183c41143SJohn Baldwin if (sc->flags & PCIB_SUBTRACTIVE && r == NULL) 234283c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 234383c41143SJohn Baldwin start, end, count, flags)); 234483c41143SJohn Baldwin return (r); 234583c41143SJohn Baldwin } 234683c41143SJohn Baldwin 234783c41143SJohn Baldwin int 234883c41143SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r, 23492dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end) 235083c41143SJohn Baldwin { 235183c41143SJohn Baldwin struct pcib_softc *sc; 235226245980SJessica Clarke struct pcib_window *w; 235315cb3b54SAlexander Motin rman_res_t wmask; 235426245980SJessica Clarke int error; 235583c41143SJohn Baldwin 235683c41143SJohn Baldwin sc = device_get_softc(bus); 235726245980SJessica Clarke 235826245980SJessica Clarke /* 235926245980SJessica Clarke * If the resource wasn't sub-allocated from one of our region 236026245980SJessica Clarke * managers then just pass the request up. 236126245980SJessica Clarke */ 236226245980SJessica Clarke if (!pcib_is_resource_managed(sc, type, r)) 236326245980SJessica Clarke return (bus_generic_adjust_resource(bus, child, type, r, 236426245980SJessica Clarke start, end)); 236526245980SJessica Clarke 236626245980SJessica Clarke #ifdef PCI_RES_BUS 2367*31776afdSJessica Clarke if (type == PCI_RES_BUS) { 2368*31776afdSJessica Clarke /* 2369*31776afdSJessica Clarke * If our bus range isn't big enough to grow the sub-allocation 2370*31776afdSJessica Clarke * then we need to grow our bus range. Any request that would 2371*31776afdSJessica Clarke * require us to decrease the start of our own bus range is 2372*31776afdSJessica Clarke * invalid, we can only extend the end; ignore such requests 2373*31776afdSJessica Clarke * and let rman_adjust_resource fail below. 2374*31776afdSJessica Clarke */ 2375*31776afdSJessica Clarke if (start >= sc->bus.sec && end > sc->bus.sub) { 2376*31776afdSJessica Clarke error = pcib_grow_subbus(&sc->bus, end); 2377*31776afdSJessica Clarke if (error != 0) 2378*31776afdSJessica Clarke return (error); 2379*31776afdSJessica Clarke } 2380*31776afdSJessica Clarke } else 238126245980SJessica Clarke #endif 238226245980SJessica Clarke { 238326245980SJessica Clarke /* 238426245980SJessica Clarke * Resource is managed and not a secondary bus number, must 238526245980SJessica Clarke * be from one of our windows. 238626245980SJessica Clarke */ 238726245980SJessica Clarke w = pcib_get_resource_window(sc, type, r); 238826245980SJessica Clarke KASSERT(w != NULL, 238926245980SJessica Clarke ("%s: no window for resource (%#jx-%#jx) type %d", 239026245980SJessica Clarke __func__, rman_get_start(r), rman_get_end(r), type)); 239126245980SJessica Clarke 239226245980SJessica Clarke /* 239326245980SJessica Clarke * If our window isn't big enough to grow the sub-allocation 239426245980SJessica Clarke * then we need to expand the window. 239526245980SJessica Clarke */ 239626245980SJessica Clarke if (start < w->base || end > w->limit) { 239715cb3b54SAlexander Motin wmask = ((rman_res_t)1 << w->step) - 1; 239815cb3b54SAlexander Motin error = pcib_expand_window(sc, w, type, 239915cb3b54SAlexander Motin MIN(start & ~wmask, w->base), 240015cb3b54SAlexander Motin MAX(end | wmask, w->limit)); 240126245980SJessica Clarke if (error != 0) 240226245980SJessica Clarke return (error); 240315cb3b54SAlexander Motin if (bootverbose) 240415cb3b54SAlexander Motin device_printf(sc->dev, 240515cb3b54SAlexander Motin "grew %s window to %#jx-%#jx\n", 240615cb3b54SAlexander Motin w->name, (uintmax_t)w->base, 240715cb3b54SAlexander Motin (uintmax_t)w->limit); 240815cb3b54SAlexander Motin pcib_write_windows(sc, w->mask); 240926245980SJessica Clarke } 241026245980SJessica Clarke } 241126245980SJessica Clarke 241283c41143SJohn Baldwin return (rman_adjust_resource(r, start, end)); 241383c41143SJohn Baldwin } 241483c41143SJohn Baldwin 241583c41143SJohn Baldwin int 241683c41143SJohn Baldwin pcib_release_resource(device_t dev, device_t child, int type, int rid, 241783c41143SJohn Baldwin struct resource *r) 241883c41143SJohn Baldwin { 241983c41143SJohn Baldwin struct pcib_softc *sc; 242083c41143SJohn Baldwin int error; 242183c41143SJohn Baldwin 242283c41143SJohn Baldwin sc = device_get_softc(dev); 242383c41143SJohn Baldwin if (pcib_is_resource_managed(sc, type, r)) { 242483c41143SJohn Baldwin if (rman_get_flags(r) & RF_ACTIVE) { 242583c41143SJohn Baldwin error = bus_deactivate_resource(child, type, rid, r); 242683c41143SJohn Baldwin if (error) 242783c41143SJohn Baldwin return (error); 242883c41143SJohn Baldwin } 242983c41143SJohn Baldwin return (rman_release_resource(r)); 243083c41143SJohn Baldwin } 243183c41143SJohn Baldwin return (bus_generic_release_resource(dev, child, type, rid, r)); 243283c41143SJohn Baldwin } 243383c41143SJohn Baldwin #else 2434bb0d0a8eSMike Smith /* 2435bb0d0a8eSMike Smith * We have to trap resource allocation requests and ensure that the bridge 2436bb0d0a8eSMike Smith * is set up to, or capable of handling them. 2437bb0d0a8eSMike Smith */ 24386f0d5884SJohn Baldwin struct resource * 2439bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 24402dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 2441bb0d0a8eSMike Smith { 2442bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 244326043836SJohn Baldwin const char *name, *suffix; 2444a8b354a8SWarner Losh int ok; 2445bb0d0a8eSMike Smith 2446bb0d0a8eSMike Smith /* 2447bb0d0a8eSMike Smith * Fail the allocation for this range if it's not supported. 2448bb0d0a8eSMike Smith */ 244926043836SJohn Baldwin name = device_get_nameunit(child); 245026043836SJohn Baldwin if (name == NULL) { 245126043836SJohn Baldwin name = ""; 245226043836SJohn Baldwin suffix = ""; 245326043836SJohn Baldwin } else 245426043836SJohn Baldwin suffix = " "; 2455bb0d0a8eSMike Smith switch (type) { 2456bb0d0a8eSMike Smith case SYS_RES_IOPORT: 2457a8b354a8SWarner Losh ok = 0; 2458e4b59fc5SWarner Losh if (!pcib_is_io_open(sc)) 2459e4b59fc5SWarner Losh break; 2460a8b354a8SWarner Losh ok = (start >= sc->iobase && end <= sc->iolimit); 2461d98d9b12SMarcel Moolenaar 2462d98d9b12SMarcel Moolenaar /* 2463d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA I/O addresses when the 2464d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 2465d98d9b12SMarcel Moolenaar */ 2466d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_ioport_range(start, end)) 2467d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 2468d98d9b12SMarcel Moolenaar 2469e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 2470a8b354a8SWarner Losh if (!ok) { 247112b8c86eSWarner Losh if (start < sc->iobase) 247212b8c86eSWarner Losh start = sc->iobase; 247312b8c86eSWarner Losh if (end > sc->iolimit) 247412b8c86eSWarner Losh end = sc->iolimit; 24752daa7a07SWarner Losh if (start < end) 24762daa7a07SWarner Losh ok = 1; 2477a8b354a8SWarner Losh } 24781c54ff33SMatthew N. Dodd } else { 2479e4b59fc5SWarner Losh ok = 1; 24809dffe835SWarner Losh #if 0 2481795dceffSWarner Losh /* 2482795dceffSWarner Losh * If we overlap with the subtractive range, then 2483795dceffSWarner Losh * pick the upper range to use. 2484795dceffSWarner Losh */ 2485795dceffSWarner Losh if (start < sc->iolimit && end > sc->iobase) 2486795dceffSWarner Losh start = sc->iolimit + 1; 24879dffe835SWarner Losh #endif 248812b8c86eSWarner Losh } 2489a8b354a8SWarner Losh if (end < start) { 2490da1b038aSJustin Hibbits device_printf(dev, "ioport: end (%jx) < start (%jx)\n", 24912daa7a07SWarner Losh end, start); 2492a8b354a8SWarner Losh start = 0; 2493a8b354a8SWarner Losh end = 0; 2494a8b354a8SWarner Losh ok = 0; 2495a8b354a8SWarner Losh } 2496a8b354a8SWarner Losh if (!ok) { 249726043836SJohn Baldwin device_printf(dev, "%s%srequested unsupported I/O " 2498da1b038aSJustin Hibbits "range 0x%jx-0x%jx (decoding 0x%x-0x%x)\n", 249926043836SJohn Baldwin name, suffix, start, end, sc->iobase, sc->iolimit); 2500bb0d0a8eSMike Smith return (NULL); 2501bb0d0a8eSMike Smith } 25024fa59183SMike Smith if (bootverbose) 25032daa7a07SWarner Losh device_printf(dev, 2504da1b038aSJustin Hibbits "%s%srequested I/O range 0x%jx-0x%jx: in range\n", 250526043836SJohn Baldwin name, suffix, start, end); 2506bb0d0a8eSMike Smith break; 2507bb0d0a8eSMike Smith 2508bb0d0a8eSMike Smith case SYS_RES_MEMORY: 2509a8b354a8SWarner Losh ok = 0; 2510a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 2511a8b354a8SWarner Losh ok = ok || (start >= sc->membase && end <= sc->memlimit); 2512a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) 2513a8b354a8SWarner Losh ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit); 2514d98d9b12SMarcel Moolenaar 2515d98d9b12SMarcel Moolenaar /* 2516d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA memory addresses when the 2517d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 2518d98d9b12SMarcel Moolenaar */ 2519d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_memory_range(start, end)) 2520d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 2521d98d9b12SMarcel Moolenaar 2522e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 2523a8b354a8SWarner Losh if (!ok) { 2524a8b354a8SWarner Losh ok = 1; 2525a8b354a8SWarner Losh if (flags & RF_PREFETCHABLE) { 2526a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 2527a8b354a8SWarner Losh if (start < sc->pmembase) 2528a8b354a8SWarner Losh start = sc->pmembase; 2529a8b354a8SWarner Losh if (end > sc->pmemlimit) 2530a8b354a8SWarner Losh end = sc->pmemlimit; 2531a8b354a8SWarner Losh } else { 2532a8b354a8SWarner Losh ok = 0; 2533a8b354a8SWarner Losh } 2534a8b354a8SWarner Losh } else { /* non-prefetchable */ 2535a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 2536a8b354a8SWarner Losh if (start < sc->membase) 253712b8c86eSWarner Losh start = sc->membase; 253812b8c86eSWarner Losh if (end > sc->memlimit) 253912b8c86eSWarner Losh end = sc->memlimit; 25401c54ff33SMatthew N. Dodd } else { 2541a8b354a8SWarner Losh ok = 0; 2542a8b354a8SWarner Losh } 2543a8b354a8SWarner Losh } 2544a8b354a8SWarner Losh } 2545a8b354a8SWarner Losh } else if (!ok) { 2546e4b59fc5SWarner Losh ok = 1; /* subtractive bridge: always ok */ 25479dffe835SWarner Losh #if 0 2548a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 2549795dceffSWarner Losh if (start < sc->memlimit && end > sc->membase) 2550795dceffSWarner Losh start = sc->memlimit + 1; 2551a8b354a8SWarner Losh } 2552a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 2553795dceffSWarner Losh if (start < sc->pmemlimit && end > sc->pmembase) 2554795dceffSWarner Losh start = sc->pmemlimit + 1; 25551c54ff33SMatthew N. Dodd } 25569dffe835SWarner Losh #endif 255712b8c86eSWarner Losh } 2558a8b354a8SWarner Losh if (end < start) { 2559da1b038aSJustin Hibbits device_printf(dev, "memory: end (%jx) < start (%jx)\n", 25602daa7a07SWarner Losh end, start); 2561a8b354a8SWarner Losh start = 0; 2562a8b354a8SWarner Losh end = 0; 2563a8b354a8SWarner Losh ok = 0; 2564a8b354a8SWarner Losh } 2565a8b354a8SWarner Losh if (!ok && bootverbose) 256634428485SWarner Losh device_printf(dev, 2567da1b038aSJustin Hibbits "%s%srequested unsupported memory range %#jx-%#jx " 2568b0a2d4b8SWarner Losh "(decoding %#jx-%#jx, %#jx-%#jx)\n", 256926043836SJohn Baldwin name, suffix, start, end, 2570b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit, 2571b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 2572a8b354a8SWarner Losh if (!ok) 2573bb0d0a8eSMike Smith return (NULL); 25744fa59183SMike Smith if (bootverbose) 257526043836SJohn Baldwin device_printf(dev,"%s%srequested memory range " 2576da1b038aSJustin Hibbits "0x%jx-0x%jx: good\n", 257726043836SJohn Baldwin name, suffix, start, end); 25784fa59183SMike Smith break; 25794fa59183SMike Smith 2580bb0d0a8eSMike Smith default: 25814fa59183SMike Smith break; 2582bb0d0a8eSMike Smith } 2583bb0d0a8eSMike Smith /* 2584bb0d0a8eSMike Smith * Bridge is OK decoding this resource, so pass it up. 2585bb0d0a8eSMike Smith */ 25862daa7a07SWarner Losh return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 25872daa7a07SWarner Losh count, flags)); 2588bb0d0a8eSMike Smith } 258983c41143SJohn Baldwin #endif 2590bb0d0a8eSMike Smith 2591bb0d0a8eSMike Smith /* 259255d3ea17SRyan Stone * If ARI is enabled on this downstream port, translate the function number 259355d3ea17SRyan Stone * to the non-ARI slot/function. The downstream port will convert it back in 259455d3ea17SRyan Stone * hardware. If ARI is not enabled slot and func are not modified. 259555d3ea17SRyan Stone */ 259655d3ea17SRyan Stone static __inline void 259755d3ea17SRyan Stone pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func) 259855d3ea17SRyan Stone { 259955d3ea17SRyan Stone struct pcib_softc *sc; 260055d3ea17SRyan Stone int ari_func; 260155d3ea17SRyan Stone 260255d3ea17SRyan Stone sc = device_get_softc(pcib); 260355d3ea17SRyan Stone ari_func = *func; 260455d3ea17SRyan Stone 260555d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 260655d3ea17SRyan Stone KASSERT(*slot == 0, 260755d3ea17SRyan Stone ("Non-zero slot number with ARI enabled!")); 260855d3ea17SRyan Stone *slot = PCIE_ARI_SLOT(ari_func); 260955d3ea17SRyan Stone *func = PCIE_ARI_FUNC(ari_func); 261055d3ea17SRyan Stone } 261155d3ea17SRyan Stone } 261255d3ea17SRyan Stone 261355d3ea17SRyan Stone static void 261455d3ea17SRyan Stone pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos) 261555d3ea17SRyan Stone { 261655d3ea17SRyan Stone uint32_t ctl2; 261755d3ea17SRyan Stone 261855d3ea17SRyan Stone ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4); 261955d3ea17SRyan Stone ctl2 |= PCIEM_CTL2_ARI; 262055d3ea17SRyan Stone pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4); 262155d3ea17SRyan Stone 262255d3ea17SRyan Stone sc->flags |= PCIB_ENABLE_ARI; 262355d3ea17SRyan Stone } 262455d3ea17SRyan Stone 262555d3ea17SRyan Stone /* 2626bb0d0a8eSMike Smith * PCIB interface. 2627bb0d0a8eSMike Smith */ 26286f0d5884SJohn Baldwin int 2629bb0d0a8eSMike Smith pcib_maxslots(device_t dev) 2630bb0d0a8eSMike Smith { 26315502348dSJustin Hibbits #if !defined(__amd64__) && !defined(__i386__) 26328b92ad43SJustin Hibbits uint32_t pcie_pos; 26338b92ad43SJustin Hibbits uint16_t val; 26348b92ad43SJustin Hibbits 26358b92ad43SJustin Hibbits /* 26368b92ad43SJustin Hibbits * If this is a PCIe rootport or downstream switch port, there's only 26378b92ad43SJustin Hibbits * one slot permitted. 26388b92ad43SJustin Hibbits */ 26398b92ad43SJustin Hibbits if (pci_find_cap(dev, PCIY_EXPRESS, &pcie_pos) == 0) { 26408b92ad43SJustin Hibbits val = pci_read_config(dev, pcie_pos + PCIER_FLAGS, 2); 26418b92ad43SJustin Hibbits val &= PCIEM_FLAGS_TYPE; 26428b92ad43SJustin Hibbits if (val == PCIEM_TYPE_ROOT_PORT || 26438b92ad43SJustin Hibbits val == PCIEM_TYPE_DOWNSTREAM_PORT) 26448b92ad43SJustin Hibbits return (0); 26458b92ad43SJustin Hibbits } 26465502348dSJustin Hibbits #endif 26474fa59183SMike Smith return (PCI_SLOTMAX); 2648bb0d0a8eSMike Smith } 2649bb0d0a8eSMike Smith 265055d3ea17SRyan Stone static int 265155d3ea17SRyan Stone pcib_ari_maxslots(device_t dev) 265255d3ea17SRyan Stone { 265355d3ea17SRyan Stone struct pcib_softc *sc; 265455d3ea17SRyan Stone 265555d3ea17SRyan Stone sc = device_get_softc(dev); 265655d3ea17SRyan Stone 265755d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 265855d3ea17SRyan Stone return (PCIE_ARI_SLOTMAX); 265955d3ea17SRyan Stone else 26608b92ad43SJustin Hibbits return (pcib_maxslots(dev)); 266155d3ea17SRyan Stone } 266255d3ea17SRyan Stone 266355d3ea17SRyan Stone static int 266455d3ea17SRyan Stone pcib_ari_maxfuncs(device_t dev) 266555d3ea17SRyan Stone { 266655d3ea17SRyan Stone struct pcib_softc *sc; 266755d3ea17SRyan Stone 266855d3ea17SRyan Stone sc = device_get_softc(dev); 266955d3ea17SRyan Stone 267055d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 267155d3ea17SRyan Stone return (PCIE_ARI_FUNCMAX); 267255d3ea17SRyan Stone else 267355d3ea17SRyan Stone return (PCI_FUNCMAX); 267455d3ea17SRyan Stone } 267555d3ea17SRyan Stone 26762397d2d8SRyan Stone static void 26772397d2d8SRyan Stone pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot, 26782397d2d8SRyan Stone int *func) 26792397d2d8SRyan Stone { 26802397d2d8SRyan Stone struct pcib_softc *sc; 26812397d2d8SRyan Stone 26822397d2d8SRyan Stone sc = device_get_softc(pcib); 26832397d2d8SRyan Stone 26842397d2d8SRyan Stone *bus = PCI_RID2BUS(rid); 26852397d2d8SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 26862397d2d8SRyan Stone *slot = PCIE_ARI_RID2SLOT(rid); 26872397d2d8SRyan Stone *func = PCIE_ARI_RID2FUNC(rid); 26882397d2d8SRyan Stone } else { 26892397d2d8SRyan Stone *slot = PCI_RID2SLOT(rid); 26902397d2d8SRyan Stone *func = PCI_RID2FUNC(rid); 26912397d2d8SRyan Stone } 26922397d2d8SRyan Stone } 26932397d2d8SRyan Stone 2694bb0d0a8eSMike Smith /* 2695bb0d0a8eSMike Smith * Since we are a child of a PCI bus, its parent must support the pcib interface. 2696bb0d0a8eSMike Smith */ 269755d3ea17SRyan Stone static uint32_t 2698795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width) 2699bb0d0a8eSMike Smith { 270082cb5c3bSJohn Baldwin #ifdef PCI_HP 270182cb5c3bSJohn Baldwin struct pcib_softc *sc; 270255d3ea17SRyan Stone 270382cb5c3bSJohn Baldwin sc = device_get_softc(dev); 270482cb5c3bSJohn Baldwin if (!pcib_present(sc)) { 270582cb5c3bSJohn Baldwin switch (width) { 270682cb5c3bSJohn Baldwin case 2: 270782cb5c3bSJohn Baldwin return (0xffff); 270882cb5c3bSJohn Baldwin case 1: 270982cb5c3bSJohn Baldwin return (0xff); 271082cb5c3bSJohn Baldwin default: 271182cb5c3bSJohn Baldwin return (0xffffffff); 271282cb5c3bSJohn Baldwin } 271382cb5c3bSJohn Baldwin } 271482cb5c3bSJohn Baldwin #endif 271555d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 271655d3ea17SRyan Stone return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, 271755d3ea17SRyan Stone f, reg, width)); 2718bb0d0a8eSMike Smith } 2719bb0d0a8eSMike Smith 272055d3ea17SRyan Stone static void 2721795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width) 2722bb0d0a8eSMike Smith { 272382cb5c3bSJohn Baldwin #ifdef PCI_HP 272482cb5c3bSJohn Baldwin struct pcib_softc *sc; 272555d3ea17SRyan Stone 272682cb5c3bSJohn Baldwin sc = device_get_softc(dev); 272782cb5c3bSJohn Baldwin if (!pcib_present(sc)) 272882cb5c3bSJohn Baldwin return; 272982cb5c3bSJohn Baldwin #endif 273055d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 273155d3ea17SRyan Stone PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, 273255d3ea17SRyan Stone reg, val, width); 2733bb0d0a8eSMike Smith } 2734bb0d0a8eSMike Smith 2735bb0d0a8eSMike Smith /* 2736bb0d0a8eSMike Smith * Route an interrupt across a PCI bridge. 2737bb0d0a8eSMike Smith */ 27382c2d1d07SBenno Rice int 2739bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin) 2740bb0d0a8eSMike Smith { 2741bb0d0a8eSMike Smith device_t bus; 2742bb0d0a8eSMike Smith int parent_intpin; 2743bb0d0a8eSMike Smith int intnum; 2744bb0d0a8eSMike Smith 2745bb0d0a8eSMike Smith /* 2746bb0d0a8eSMike Smith * 2747bb0d0a8eSMike Smith * The PCI standard defines a swizzle of the child-side device/intpin to 2748bb0d0a8eSMike Smith * the parent-side intpin as follows. 2749bb0d0a8eSMike Smith * 2750bb0d0a8eSMike Smith * device = device on child bus 2751bb0d0a8eSMike Smith * child_intpin = intpin on child bus slot (0-3) 2752bb0d0a8eSMike Smith * parent_intpin = intpin on parent bus slot (0-3) 2753bb0d0a8eSMike Smith * 2754bb0d0a8eSMike Smith * parent_intpin = (device + child_intpin) % 4 2755bb0d0a8eSMike Smith */ 2756cdc95e1bSBernd Walter parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4; 2757bb0d0a8eSMike Smith 2758bb0d0a8eSMike Smith /* 2759bb0d0a8eSMike Smith * Our parent is a PCI bus. Its parent must export the pcib interface 2760bb0d0a8eSMike Smith * which includes the ability to route interrupts. 2761bb0d0a8eSMike Smith */ 2762bb0d0a8eSMike Smith bus = device_get_parent(pcib); 2763bb0d0a8eSMike Smith intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1); 276439981fedSJohn Baldwin if (PCI_INTERRUPT_VALID(intnum) && bootverbose) { 2765c6a121abSJohn Baldwin device_printf(pcib, "slot %d INT%c is routed to irq %d\n", 2766c6a121abSJohn Baldwin pci_get_slot(dev), 'A' + pin - 1, intnum); 27678046c4b9SMike Smith } 2768bb0d0a8eSMike Smith return(intnum); 2769bb0d0a8eSMike Smith } 2770b173edafSJohn Baldwin 2771e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */ 27729bf4c9c1SJohn Baldwin int 27739bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs) 27749bf4c9c1SJohn Baldwin { 2775bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 27769bf4c9c1SJohn Baldwin device_t bus; 27779bf4c9c1SJohn Baldwin 277822bf1c7fSJohn Baldwin if (sc->flags & PCIB_DISABLE_MSI) 277922bf1c7fSJohn Baldwin return (ENXIO); 27809bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 27819bf4c9c1SJohn Baldwin return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 27829bf4c9c1SJohn Baldwin irqs)); 27839bf4c9c1SJohn Baldwin } 27849bf4c9c1SJohn Baldwin 2785e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */ 27869bf4c9c1SJohn Baldwin int 27879bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs) 27889bf4c9c1SJohn Baldwin { 27899bf4c9c1SJohn Baldwin device_t bus; 27909bf4c9c1SJohn Baldwin 27919bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 27929bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs)); 27939bf4c9c1SJohn Baldwin } 27949bf4c9c1SJohn Baldwin 27959bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */ 27969bf4c9c1SJohn Baldwin int 2797e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq) 27989bf4c9c1SJohn Baldwin { 2799bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 28009bf4c9c1SJohn Baldwin device_t bus; 28019bf4c9c1SJohn Baldwin 280268e9cbd3SMarius Strobl if (sc->flags & PCIB_DISABLE_MSIX) 280322bf1c7fSJohn Baldwin return (ENXIO); 28049bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 2805e706f7f0SJohn Baldwin return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq)); 28065fe82bcaSJohn Baldwin } 28075fe82bcaSJohn Baldwin 28089bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */ 28099bf4c9c1SJohn Baldwin int 28109bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq) 28119bf4c9c1SJohn Baldwin { 28129bf4c9c1SJohn Baldwin device_t bus; 28139bf4c9c1SJohn Baldwin 28149bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 28159bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq)); 28169bf4c9c1SJohn Baldwin } 28179bf4c9c1SJohn Baldwin 2818e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */ 2819e706f7f0SJohn Baldwin int 2820e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, 2821e706f7f0SJohn Baldwin uint32_t *data) 2822e706f7f0SJohn Baldwin { 2823e706f7f0SJohn Baldwin device_t bus; 28244522ac77SLuoqi Chen int error; 2825e706f7f0SJohn Baldwin 2826e706f7f0SJohn Baldwin bus = device_get_parent(pcib); 28274522ac77SLuoqi Chen error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data); 28284522ac77SLuoqi Chen if (error) 28294522ac77SLuoqi Chen return (error); 28304522ac77SLuoqi Chen 28314522ac77SLuoqi Chen pci_ht_map_msi(pcib, *addr); 28324522ac77SLuoqi Chen return (0); 2833e706f7f0SJohn Baldwin } 2834e706f7f0SJohn Baldwin 283562508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */ 283662508c53SJohn Baldwin int 283762508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate) 283862508c53SJohn Baldwin { 283962508c53SJohn Baldwin device_t bus; 284062508c53SJohn Baldwin 284162508c53SJohn Baldwin bus = device_get_parent(pcib); 284262508c53SJohn Baldwin return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate)); 284362508c53SJohn Baldwin } 28445605a99eSRyan Stone 28452397d2d8SRyan Stone static int 28462397d2d8SRyan Stone pcib_ari_enabled(device_t pcib) 28472397d2d8SRyan Stone { 28482397d2d8SRyan Stone struct pcib_softc *sc; 28492397d2d8SRyan Stone 28502397d2d8SRyan Stone sc = device_get_softc(pcib); 28512397d2d8SRyan Stone 28522397d2d8SRyan Stone return ((sc->flags & PCIB_ENABLE_ARI) != 0); 28532397d2d8SRyan Stone } 28542397d2d8SRyan Stone 2855d7be980dSAndrew Turner static int 2856d7be980dSAndrew Turner pcib_ari_get_id(device_t pcib, device_t dev, enum pci_id_type type, 2857d7be980dSAndrew Turner uintptr_t *id) 285855d3ea17SRyan Stone { 285955d3ea17SRyan Stone struct pcib_softc *sc; 28601e43b18cSAndrew Turner device_t bus_dev; 286155d3ea17SRyan Stone uint8_t bus, slot, func; 286255d3ea17SRyan Stone 28631e43b18cSAndrew Turner if (type != PCI_ID_RID) { 28641e43b18cSAndrew Turner bus_dev = device_get_parent(pcib); 28651e43b18cSAndrew Turner return (PCIB_GET_ID(device_get_parent(bus_dev), dev, type, id)); 28661e43b18cSAndrew Turner } 2867d7be980dSAndrew Turner 286855d3ea17SRyan Stone sc = device_get_softc(pcib); 286955d3ea17SRyan Stone 287055d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 287155d3ea17SRyan Stone bus = pci_get_bus(dev); 287255d3ea17SRyan Stone func = pci_get_function(dev); 287355d3ea17SRyan Stone 2874d7be980dSAndrew Turner *id = (PCI_ARI_RID(bus, func)); 287555d3ea17SRyan Stone } else { 287655d3ea17SRyan Stone bus = pci_get_bus(dev); 287755d3ea17SRyan Stone slot = pci_get_slot(dev); 287855d3ea17SRyan Stone func = pci_get_function(dev); 287955d3ea17SRyan Stone 2880d7be980dSAndrew Turner *id = (PCI_RID(bus, slot, func)); 288155d3ea17SRyan Stone } 2882d7be980dSAndrew Turner 2883d7be980dSAndrew Turner return (0); 288455d3ea17SRyan Stone } 288555d3ea17SRyan Stone 288655d3ea17SRyan Stone /* 288755d3ea17SRyan Stone * Check that the downstream port (pcib) and the endpoint device (dev) both 288855d3ea17SRyan Stone * support ARI. If so, enable it and return 0, otherwise return an error. 288955d3ea17SRyan Stone */ 289055d3ea17SRyan Stone static int 289155d3ea17SRyan Stone pcib_try_enable_ari(device_t pcib, device_t dev) 289255d3ea17SRyan Stone { 289355d3ea17SRyan Stone struct pcib_softc *sc; 289455d3ea17SRyan Stone int error; 289555d3ea17SRyan Stone uint32_t cap2; 289655d3ea17SRyan Stone int ari_cap_off; 289755d3ea17SRyan Stone uint32_t ari_ver; 289855d3ea17SRyan Stone uint32_t pcie_pos; 289955d3ea17SRyan Stone 290055d3ea17SRyan Stone sc = device_get_softc(pcib); 290155d3ea17SRyan Stone 290255d3ea17SRyan Stone /* 290355d3ea17SRyan Stone * ARI is controlled in a register in the PCIe capability structure. 290455d3ea17SRyan Stone * If the downstream port does not have the PCIe capability structure 290555d3ea17SRyan Stone * then it does not support ARI. 290655d3ea17SRyan Stone */ 290755d3ea17SRyan Stone error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos); 290855d3ea17SRyan Stone if (error != 0) 290955d3ea17SRyan Stone return (ENODEV); 291055d3ea17SRyan Stone 291155d3ea17SRyan Stone /* Check that the PCIe port advertises ARI support. */ 291255d3ea17SRyan Stone cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4); 291355d3ea17SRyan Stone if (!(cap2 & PCIEM_CAP2_ARI)) 291455d3ea17SRyan Stone return (ENODEV); 291555d3ea17SRyan Stone 291655d3ea17SRyan Stone /* 291755d3ea17SRyan Stone * Check that the endpoint device advertises ARI support via the ARI 291855d3ea17SRyan Stone * extended capability structure. 291955d3ea17SRyan Stone */ 292055d3ea17SRyan Stone error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off); 292155d3ea17SRyan Stone if (error != 0) 292255d3ea17SRyan Stone return (ENODEV); 292355d3ea17SRyan Stone 292455d3ea17SRyan Stone /* 292555d3ea17SRyan Stone * Finally, check that the endpoint device supports the same version 292655d3ea17SRyan Stone * of ARI that we do. 292755d3ea17SRyan Stone */ 292855d3ea17SRyan Stone ari_ver = pci_read_config(dev, ari_cap_off, 4); 292955d3ea17SRyan Stone if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) { 293055d3ea17SRyan Stone if (bootverbose) 293155d3ea17SRyan Stone device_printf(pcib, 293255d3ea17SRyan Stone "Unsupported version of ARI (%d) detected\n", 293355d3ea17SRyan Stone PCI_EXTCAP_VER(ari_ver)); 293455d3ea17SRyan Stone 293555d3ea17SRyan Stone return (ENXIO); 293655d3ea17SRyan Stone } 293755d3ea17SRyan Stone 293855d3ea17SRyan Stone pcib_enable_ari(sc, pcie_pos); 293955d3ea17SRyan Stone 294055d3ea17SRyan Stone return (0); 294155d3ea17SRyan Stone } 29424cb67729SWarner Losh 294328586889SWarner Losh int 294428586889SWarner Losh pcib_request_feature_allow(device_t pcib, device_t dev, 294528586889SWarner Losh enum pci_feature feature) 294628586889SWarner Losh { 294728586889SWarner Losh /* 29485914c62eSGavin Atkinson * No host firmware we have to negotiate with, so we allow 294928586889SWarner Losh * every valid feature requested. 295028586889SWarner Losh */ 295128586889SWarner Losh switch (feature) { 295228586889SWarner Losh case PCI_FEATURE_AER: 295328586889SWarner Losh case PCI_FEATURE_HP: 295428586889SWarner Losh break; 295528586889SWarner Losh default: 295628586889SWarner Losh return (EINVAL); 295728586889SWarner Losh } 295828586889SWarner Losh 295928586889SWarner Losh return (0); 296028586889SWarner Losh } 296128586889SWarner Losh 29621ffd07bdSJohn Baldwin int 29631ffd07bdSJohn Baldwin pcib_request_feature(device_t dev, enum pci_feature feature) 29641ffd07bdSJohn Baldwin { 29651ffd07bdSJohn Baldwin 29661ffd07bdSJohn Baldwin /* 29671ffd07bdSJohn Baldwin * Invoke PCIB_REQUEST_FEATURE of this bridge first in case 29681ffd07bdSJohn Baldwin * the firmware overrides the method of PCI-PCI bridges. 29691ffd07bdSJohn Baldwin */ 29701ffd07bdSJohn Baldwin return (PCIB_REQUEST_FEATURE(dev, dev, feature)); 29711ffd07bdSJohn Baldwin } 29721ffd07bdSJohn Baldwin 29734cb67729SWarner Losh /* 29744cb67729SWarner Losh * Pass the request to use this PCI feature up the tree. Either there's a 29754cb67729SWarner Losh * firmware like ACPI that's using this feature that will approve (or deny) the 29764cb67729SWarner Losh * request to take it over, or the platform has no such firmware, in which case 29774cb67729SWarner Losh * the request will be approved. If the request is approved, the OS is expected 29784cb67729SWarner Losh * to make use of the feature or render it harmless. 29794cb67729SWarner Losh */ 29804cb67729SWarner Losh static int 29811ffd07bdSJohn Baldwin pcib_request_feature_default(device_t pcib, device_t dev, 29821ffd07bdSJohn Baldwin enum pci_feature feature) 29834cb67729SWarner Losh { 29844cb67729SWarner Losh device_t bus; 29854cb67729SWarner Losh 29864cb67729SWarner Losh /* 29874cb67729SWarner Losh * Our parent is necessarily a pci bus. Its parent will either be 29884cb67729SWarner Losh * another pci bridge (which passes it up) or a host bridge that can 29894cb67729SWarner Losh * approve or reject the request. 29904cb67729SWarner Losh */ 29914cb67729SWarner Losh bus = device_get_parent(pcib); 29924cb67729SWarner Losh return (PCIB_REQUEST_FEATURE(device_get_parent(bus), dev, feature)); 29934cb67729SWarner Losh } 29945db2a4a8SKonstantin Belousov 29955db2a4a8SKonstantin Belousov static int 29965db2a4a8SKonstantin Belousov pcib_reset_child(device_t dev, device_t child, int flags) 29975db2a4a8SKonstantin Belousov { 29985db2a4a8SKonstantin Belousov struct pci_devinfo *pdinfo; 29995db2a4a8SKonstantin Belousov int error; 30005db2a4a8SKonstantin Belousov 30015db2a4a8SKonstantin Belousov error = 0; 30025db2a4a8SKonstantin Belousov if (dev == NULL || device_get_parent(child) != dev) 30035db2a4a8SKonstantin Belousov goto out; 30045db2a4a8SKonstantin Belousov error = ENXIO; 30055db2a4a8SKonstantin Belousov if (device_get_devclass(child) != devclass_find("pci")) 30065db2a4a8SKonstantin Belousov goto out; 30075db2a4a8SKonstantin Belousov pdinfo = device_get_ivars(dev); 30085db2a4a8SKonstantin Belousov if (pdinfo->cfg.pcie.pcie_location != 0 && 30095db2a4a8SKonstantin Belousov (pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_DOWNSTREAM_PORT || 30105db2a4a8SKonstantin Belousov pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_ROOT_PORT)) { 30115db2a4a8SKonstantin Belousov error = bus_helper_reset_prepare(child, flags); 30125db2a4a8SKonstantin Belousov if (error == 0) { 30135db2a4a8SKonstantin Belousov error = pcie_link_reset(dev, 30145db2a4a8SKonstantin Belousov pdinfo->cfg.pcie.pcie_location); 30155db2a4a8SKonstantin Belousov /* XXXKIB call _post even if error != 0 ? */ 30165db2a4a8SKonstantin Belousov bus_helper_reset_post(child, flags); 30175db2a4a8SKonstantin Belousov } 30185db2a4a8SKonstantin Belousov } 30195db2a4a8SKonstantin Belousov out: 30205db2a4a8SKonstantin Belousov return (error); 30215db2a4a8SKonstantin Belousov } 3022