1bb0d0a8eSMike Smith /*- 2bb0d0a8eSMike Smith * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier 3bb0d0a8eSMike Smith * Copyright (c) 2000 Michael Smith <msmith@freebsd.org> 4bb0d0a8eSMike Smith * Copyright (c) 2000 BSDi 5bb0d0a8eSMike Smith * All rights reserved. 6bb0d0a8eSMike Smith * 7bb0d0a8eSMike Smith * Redistribution and use in source and binary forms, with or without 8bb0d0a8eSMike Smith * modification, are permitted provided that the following conditions 9bb0d0a8eSMike Smith * are met: 10bb0d0a8eSMike Smith * 1. Redistributions of source code must retain the above copyright 11bb0d0a8eSMike Smith * notice, this list of conditions and the following disclaimer. 12bb0d0a8eSMike Smith * 2. Redistributions in binary form must reproduce the above copyright 13bb0d0a8eSMike Smith * notice, this list of conditions and the following disclaimer in the 14bb0d0a8eSMike Smith * documentation and/or other materials provided with the distribution. 15bb0d0a8eSMike Smith * 3. The name of the author may not be used to endorse or promote products 16bb0d0a8eSMike Smith * derived from this software without specific prior written permission. 17bb0d0a8eSMike Smith * 18bb0d0a8eSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19bb0d0a8eSMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20bb0d0a8eSMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21bb0d0a8eSMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22bb0d0a8eSMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23bb0d0a8eSMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24bb0d0a8eSMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25bb0d0a8eSMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26bb0d0a8eSMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27bb0d0a8eSMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28bb0d0a8eSMike Smith * SUCH DAMAGE. 29bb0d0a8eSMike Smith */ 30bb0d0a8eSMike Smith 31aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 32aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 33aad970f1SDavid E. O'Brien 34bb0d0a8eSMike Smith /* 35bb0d0a8eSMike Smith * PCI:PCI bridge support. 36bb0d0a8eSMike Smith */ 37bb0d0a8eSMike Smith 38bb0d0a8eSMike Smith #include <sys/param.h> 39bb0d0a8eSMike Smith #include <sys/bus.h> 4083c41143SJohn Baldwin #include <sys/kernel.h> 4183c41143SJohn Baldwin #include <sys/malloc.h> 4283c41143SJohn Baldwin #include <sys/module.h> 43a8b354a8SWarner Losh #include <sys/rman.h> 441c54ff33SMatthew N. Dodd #include <sys/sysctl.h> 4583c41143SJohn Baldwin #include <sys/systm.h> 46bb0d0a8eSMike Smith 4738d8c994SWarner Losh #include <dev/pci/pcivar.h> 4838d8c994SWarner Losh #include <dev/pci/pcireg.h> 4962508c53SJohn Baldwin #include <dev/pci/pci_private.h> 5038d8c994SWarner Losh #include <dev/pci/pcib_private.h> 51bb0d0a8eSMike Smith 52bb0d0a8eSMike Smith #include "pcib_if.h" 53bb0d0a8eSMike Smith 54bb0d0a8eSMike Smith static int pcib_probe(device_t dev); 55e36af292SJung-uk Kim static int pcib_suspend(device_t dev); 56e36af292SJung-uk Kim static int pcib_resume(device_t dev); 5762508c53SJohn Baldwin static int pcib_power_for_sleep(device_t pcib, device_t dev, 5862508c53SJohn Baldwin int *pstate); 59*55d3ea17SRyan Stone static uint16_t pcib_ari_get_rid(device_t pcib, device_t dev); 60*55d3ea17SRyan Stone static uint32_t pcib_read_config(device_t dev, u_int b, u_int s, 61*55d3ea17SRyan Stone u_int f, u_int reg, int width); 62*55d3ea17SRyan Stone static void pcib_write_config(device_t dev, u_int b, u_int s, 63*55d3ea17SRyan Stone u_int f, u_int reg, uint32_t val, int width); 64*55d3ea17SRyan Stone static int pcib_ari_maxslots(device_t dev); 65*55d3ea17SRyan Stone static int pcib_ari_maxfuncs(device_t dev); 66*55d3ea17SRyan Stone static int pcib_try_enable_ari(device_t pcib, device_t dev); 67bb0d0a8eSMike Smith 68bb0d0a8eSMike Smith static device_method_t pcib_methods[] = { 69bb0d0a8eSMike Smith /* Device interface */ 70bb0d0a8eSMike Smith DEVMETHOD(device_probe, pcib_probe), 71bb0d0a8eSMike Smith DEVMETHOD(device_attach, pcib_attach), 724e30440dSWarner Losh DEVMETHOD(device_detach, bus_generic_detach), 73bb0d0a8eSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 74e36af292SJung-uk Kim DEVMETHOD(device_suspend, pcib_suspend), 75e36af292SJung-uk Kim DEVMETHOD(device_resume, pcib_resume), 76bb0d0a8eSMike Smith 77bb0d0a8eSMike Smith /* Bus interface */ 78bb0d0a8eSMike Smith DEVMETHOD(bus_read_ivar, pcib_read_ivar), 79bb0d0a8eSMike Smith DEVMETHOD(bus_write_ivar, pcib_write_ivar), 80bb0d0a8eSMike Smith DEVMETHOD(bus_alloc_resource, pcib_alloc_resource), 8183c41143SJohn Baldwin #ifdef NEW_PCIB 8283c41143SJohn Baldwin DEVMETHOD(bus_adjust_resource, pcib_adjust_resource), 8383c41143SJohn Baldwin DEVMETHOD(bus_release_resource, pcib_release_resource), 8483c41143SJohn Baldwin #else 85d2c9344fSJohn Baldwin DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 86bb0d0a8eSMike Smith DEVMETHOD(bus_release_resource, bus_generic_release_resource), 8783c41143SJohn Baldwin #endif 88bb0d0a8eSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 89bb0d0a8eSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 90bb0d0a8eSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 91bb0d0a8eSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 92bb0d0a8eSMike Smith 93bb0d0a8eSMike Smith /* pcib interface */ 94*55d3ea17SRyan Stone DEVMETHOD(pcib_maxslots, pcib_ari_maxslots), 95*55d3ea17SRyan Stone DEVMETHOD(pcib_maxfuncs, pcib_ari_maxfuncs), 96bb0d0a8eSMike Smith DEVMETHOD(pcib_read_config, pcib_read_config), 97bb0d0a8eSMike Smith DEVMETHOD(pcib_write_config, pcib_write_config), 98bb0d0a8eSMike Smith DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt), 999bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi), 1009bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msi, pcib_release_msi), 1019bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix), 1029bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msix, pcib_release_msix), 103e706f7f0SJohn Baldwin DEVMETHOD(pcib_map_msi, pcib_map_msi), 10462508c53SJohn Baldwin DEVMETHOD(pcib_power_for_sleep, pcib_power_for_sleep), 105*55d3ea17SRyan Stone DEVMETHOD(pcib_get_rid, pcib_ari_get_rid), 106*55d3ea17SRyan Stone DEVMETHOD(pcib_try_enable_ari, pcib_try_enable_ari), 107bb0d0a8eSMike Smith 1084b7ec270SMarius Strobl DEVMETHOD_END 109bb0d0a8eSMike Smith }; 110bb0d0a8eSMike Smith 11104dda605SJohn Baldwin static devclass_t pcib_devclass; 112bb0d0a8eSMike Smith 11304dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc)); 11468e9cbd3SMarius Strobl DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, NULL, NULL); 115bb0d0a8eSMike Smith 11683c41143SJohn Baldwin #ifdef NEW_PCIB 1170070c94bSJohn Baldwin SYSCTL_DECL(_hw_pci); 1180070c94bSJohn Baldwin 1190070c94bSJohn Baldwin static int pci_clear_pcib; 1200070c94bSJohn Baldwin TUNABLE_INT("hw.pci.clear_pcib", &pci_clear_pcib); 1210070c94bSJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0, 1220070c94bSJohn Baldwin "Clear firmware-assigned resources for PCI-PCI bridge I/O windows."); 12383c41143SJohn Baldwin 12483c41143SJohn Baldwin /* 12583c41143SJohn Baldwin * Is a resource from a child device sub-allocated from one of our 12683c41143SJohn Baldwin * resource managers? 12783c41143SJohn Baldwin */ 12883c41143SJohn Baldwin static int 12983c41143SJohn Baldwin pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r) 13083c41143SJohn Baldwin { 13183c41143SJohn Baldwin 13283c41143SJohn Baldwin switch (type) { 1334edef187SJohn Baldwin #ifdef PCI_RES_BUS 1344edef187SJohn Baldwin case PCI_RES_BUS: 1354edef187SJohn Baldwin return (rman_is_region_manager(r, &sc->bus.rman)); 1364edef187SJohn Baldwin #endif 13783c41143SJohn Baldwin case SYS_RES_IOPORT: 13883c41143SJohn Baldwin return (rman_is_region_manager(r, &sc->io.rman)); 13983c41143SJohn Baldwin case SYS_RES_MEMORY: 14083c41143SJohn Baldwin /* Prefetchable resources may live in either memory rman. */ 14183c41143SJohn Baldwin if (rman_get_flags(r) & RF_PREFETCHABLE && 14283c41143SJohn Baldwin rman_is_region_manager(r, &sc->pmem.rman)) 14383c41143SJohn Baldwin return (1); 14483c41143SJohn Baldwin return (rman_is_region_manager(r, &sc->mem.rman)); 14583c41143SJohn Baldwin } 14683c41143SJohn Baldwin return (0); 14783c41143SJohn Baldwin } 14883c41143SJohn Baldwin 14983c41143SJohn Baldwin static int 15083c41143SJohn Baldwin pcib_is_window_open(struct pcib_window *pw) 15183c41143SJohn Baldwin { 15283c41143SJohn Baldwin 15383c41143SJohn Baldwin return (pw->valid && pw->base < pw->limit); 15483c41143SJohn Baldwin } 15583c41143SJohn Baldwin 15683c41143SJohn Baldwin /* 15783c41143SJohn Baldwin * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and 15883c41143SJohn Baldwin * handle for the resource, we could pass RF_ACTIVE up to the PCI bus 15983c41143SJohn Baldwin * when allocating the resource windows and rely on the PCI bus driver 16083c41143SJohn Baldwin * to do this for us. 16183c41143SJohn Baldwin */ 16283c41143SJohn Baldwin static void 16383c41143SJohn Baldwin pcib_activate_window(struct pcib_softc *sc, int type) 16483c41143SJohn Baldwin { 16583c41143SJohn Baldwin 16683c41143SJohn Baldwin PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type); 16783c41143SJohn Baldwin } 16883c41143SJohn Baldwin 16983c41143SJohn Baldwin static void 17083c41143SJohn Baldwin pcib_write_windows(struct pcib_softc *sc, int mask) 17183c41143SJohn Baldwin { 17283c41143SJohn Baldwin device_t dev; 17383c41143SJohn Baldwin uint32_t val; 17483c41143SJohn Baldwin 17583c41143SJohn Baldwin dev = sc->dev; 17683c41143SJohn Baldwin if (sc->io.valid && mask & WIN_IO) { 17783c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 17883c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 17983c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEH_1, 18083c41143SJohn Baldwin sc->io.base >> 16, 2); 18183c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITH_1, 18283c41143SJohn Baldwin sc->io.limit >> 16, 2); 18383c41143SJohn Baldwin } 18483c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1); 18583c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1); 18683c41143SJohn Baldwin } 18783c41143SJohn Baldwin 18883c41143SJohn Baldwin if (mask & WIN_MEM) { 18983c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2); 19083c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2); 19183c41143SJohn Baldwin } 19283c41143SJohn Baldwin 19383c41143SJohn Baldwin if (sc->pmem.valid && mask & WIN_PMEM) { 19483c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 19583c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 19683c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEH_1, 19783c41143SJohn Baldwin sc->pmem.base >> 32, 4); 19883c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITH_1, 19983c41143SJohn Baldwin sc->pmem.limit >> 32, 4); 20083c41143SJohn Baldwin } 20183c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2); 20283c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2); 20383c41143SJohn Baldwin } 20483c41143SJohn Baldwin } 20583c41143SJohn Baldwin 206c825d4dcSJohn Baldwin /* 207c825d4dcSJohn Baldwin * This is used to reject I/O port allocations that conflict with an 208c825d4dcSJohn Baldwin * ISA alias range. 209c825d4dcSJohn Baldwin */ 210c825d4dcSJohn Baldwin static int 211c825d4dcSJohn Baldwin pcib_is_isa_range(struct pcib_softc *sc, u_long start, u_long end, u_long count) 212c825d4dcSJohn Baldwin { 213c825d4dcSJohn Baldwin u_long next_alias; 214c825d4dcSJohn Baldwin 215c825d4dcSJohn Baldwin if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE)) 216c825d4dcSJohn Baldwin return (0); 217c825d4dcSJohn Baldwin 218c825d4dcSJohn Baldwin /* Only check fixed ranges for overlap. */ 219c825d4dcSJohn Baldwin if (start + count - 1 != end) 220c825d4dcSJohn Baldwin return (0); 221c825d4dcSJohn Baldwin 222c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */ 223c825d4dcSJohn Baldwin if (start >= 65536) 224c825d4dcSJohn Baldwin return (0); 225c825d4dcSJohn Baldwin 226c825d4dcSJohn Baldwin /* Check for overlap with 0x000 - 0x0ff as a special case. */ 227c825d4dcSJohn Baldwin if (start < 0x100) 228c825d4dcSJohn Baldwin goto alias; 229c825d4dcSJohn Baldwin 230c825d4dcSJohn Baldwin /* 231c825d4dcSJohn Baldwin * If the start address is an alias, the range is an alias. 232c825d4dcSJohn Baldwin * Otherwise, compute the start of the next alias range and 233c825d4dcSJohn Baldwin * check if it is before the end of the candidate range. 234c825d4dcSJohn Baldwin */ 235c825d4dcSJohn Baldwin if ((start & 0x300) != 0) 236c825d4dcSJohn Baldwin goto alias; 237c825d4dcSJohn Baldwin next_alias = (start & ~0x3fful) | 0x100; 238c825d4dcSJohn Baldwin if (next_alias <= end) 239c825d4dcSJohn Baldwin goto alias; 240c825d4dcSJohn Baldwin return (0); 241c825d4dcSJohn Baldwin 242c825d4dcSJohn Baldwin alias: 243c825d4dcSJohn Baldwin if (bootverbose) 244c825d4dcSJohn Baldwin device_printf(sc->dev, 245c825d4dcSJohn Baldwin "I/O range %#lx-%#lx overlaps with an ISA alias\n", start, 246c825d4dcSJohn Baldwin end); 247c825d4dcSJohn Baldwin return (1); 248c825d4dcSJohn Baldwin } 249c825d4dcSJohn Baldwin 250c825d4dcSJohn Baldwin static void 251c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res, 252c825d4dcSJohn Baldwin int count) 253c825d4dcSJohn Baldwin { 254c825d4dcSJohn Baldwin struct resource **newarray; 255c825d4dcSJohn Baldwin int error, i; 256c825d4dcSJohn Baldwin 257c825d4dcSJohn Baldwin newarray = malloc(sizeof(struct resource *) * (w->count + count), 258c825d4dcSJohn Baldwin M_DEVBUF, M_WAITOK); 259c825d4dcSJohn Baldwin if (w->res != NULL) 260c825d4dcSJohn Baldwin bcopy(w->res, newarray, sizeof(struct resource *) * w->count); 261c825d4dcSJohn Baldwin bcopy(res, newarray + w->count, sizeof(struct resource *) * count); 262c825d4dcSJohn Baldwin free(w->res, M_DEVBUF); 263c825d4dcSJohn Baldwin w->res = newarray; 264c825d4dcSJohn Baldwin w->count += count; 265c825d4dcSJohn Baldwin 266c825d4dcSJohn Baldwin for (i = 0; i < count; i++) { 267c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, rman_get_start(res[i]), 268c825d4dcSJohn Baldwin rman_get_end(res[i])); 269c825d4dcSJohn Baldwin if (error) 270c825d4dcSJohn Baldwin panic("Failed to add resource to rman"); 271c825d4dcSJohn Baldwin } 272c825d4dcSJohn Baldwin } 273c825d4dcSJohn Baldwin 274c825d4dcSJohn Baldwin typedef void (nonisa_callback)(u_long start, u_long end, void *arg); 275c825d4dcSJohn Baldwin 276c825d4dcSJohn Baldwin static void 277c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(u_long start, u_long end, nonisa_callback *cb, 278c825d4dcSJohn Baldwin void *arg) 279c825d4dcSJohn Baldwin { 280c825d4dcSJohn Baldwin u_long next_end; 281c825d4dcSJohn Baldwin 282c825d4dcSJohn Baldwin /* 283c825d4dcSJohn Baldwin * If start is within an ISA alias range, move up to the start 284c825d4dcSJohn Baldwin * of the next non-alias range. As a special case, addresses 285c825d4dcSJohn Baldwin * in the range 0x000 - 0x0ff should also be skipped since 286c825d4dcSJohn Baldwin * those are used for various system I/O devices in ISA 287c825d4dcSJohn Baldwin * systems. 288c825d4dcSJohn Baldwin */ 289c825d4dcSJohn Baldwin if (start <= 65535) { 290c825d4dcSJohn Baldwin if (start < 0x100 || (start & 0x300) != 0) { 291c825d4dcSJohn Baldwin start &= ~0x3ff; 292c825d4dcSJohn Baldwin start += 0x400; 293c825d4dcSJohn Baldwin } 294c825d4dcSJohn Baldwin } 295c825d4dcSJohn Baldwin 296c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */ 297c825d4dcSJohn Baldwin while (start <= MIN(end, 65535)) { 298c825d4dcSJohn Baldwin next_end = MIN(start | 0xff, end); 299c825d4dcSJohn Baldwin cb(start, next_end, arg); 300c825d4dcSJohn Baldwin start += 0x400; 301c825d4dcSJohn Baldwin } 302c825d4dcSJohn Baldwin 303c825d4dcSJohn Baldwin if (start <= end) 304c825d4dcSJohn Baldwin cb(start, end, arg); 305c825d4dcSJohn Baldwin } 306c825d4dcSJohn Baldwin 307c825d4dcSJohn Baldwin static void 308c825d4dcSJohn Baldwin count_ranges(u_long start, u_long end, void *arg) 309c825d4dcSJohn Baldwin { 310c825d4dcSJohn Baldwin int *countp; 311c825d4dcSJohn Baldwin 312c825d4dcSJohn Baldwin countp = arg; 313c825d4dcSJohn Baldwin (*countp)++; 314c825d4dcSJohn Baldwin } 315c825d4dcSJohn Baldwin 316c825d4dcSJohn Baldwin struct alloc_state { 317c825d4dcSJohn Baldwin struct resource **res; 318c825d4dcSJohn Baldwin struct pcib_softc *sc; 319c825d4dcSJohn Baldwin int count, error; 320c825d4dcSJohn Baldwin }; 321c825d4dcSJohn Baldwin 322c825d4dcSJohn Baldwin static void 323c825d4dcSJohn Baldwin alloc_ranges(u_long start, u_long end, void *arg) 324c825d4dcSJohn Baldwin { 325c825d4dcSJohn Baldwin struct alloc_state *as; 326c825d4dcSJohn Baldwin struct pcib_window *w; 327c825d4dcSJohn Baldwin int rid; 328c825d4dcSJohn Baldwin 329c825d4dcSJohn Baldwin as = arg; 330c825d4dcSJohn Baldwin if (as->error != 0) 331c825d4dcSJohn Baldwin return; 332c825d4dcSJohn Baldwin 333c825d4dcSJohn Baldwin w = &as->sc->io; 334c825d4dcSJohn Baldwin rid = w->reg; 335c825d4dcSJohn Baldwin if (bootverbose) 336c825d4dcSJohn Baldwin device_printf(as->sc->dev, 337c825d4dcSJohn Baldwin "allocating non-ISA range %#lx-%#lx\n", start, end); 338c825d4dcSJohn Baldwin as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT, 339c825d4dcSJohn Baldwin &rid, start, end, end - start + 1, 0); 340c825d4dcSJohn Baldwin if (as->res[as->count] == NULL) 341c825d4dcSJohn Baldwin as->error = ENXIO; 342c825d4dcSJohn Baldwin else 343c825d4dcSJohn Baldwin as->count++; 344c825d4dcSJohn Baldwin } 345c825d4dcSJohn Baldwin 346c825d4dcSJohn Baldwin static int 347c825d4dcSJohn Baldwin pcib_alloc_nonisa_ranges(struct pcib_softc *sc, u_long start, u_long end) 348c825d4dcSJohn Baldwin { 349c825d4dcSJohn Baldwin struct alloc_state as; 350c825d4dcSJohn Baldwin int i, new_count; 351c825d4dcSJohn Baldwin 352c825d4dcSJohn Baldwin /* First, see how many ranges we need. */ 353c825d4dcSJohn Baldwin new_count = 0; 354c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count); 355c825d4dcSJohn Baldwin 356c825d4dcSJohn Baldwin /* Second, allocate the ranges. */ 357c825d4dcSJohn Baldwin as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF, 358c825d4dcSJohn Baldwin M_WAITOK); 359c825d4dcSJohn Baldwin as.sc = sc; 360c825d4dcSJohn Baldwin as.count = 0; 361c825d4dcSJohn Baldwin as.error = 0; 362c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as); 363c825d4dcSJohn Baldwin if (as.error != 0) { 364c825d4dcSJohn Baldwin for (i = 0; i < as.count; i++) 365c825d4dcSJohn Baldwin bus_release_resource(sc->dev, SYS_RES_IOPORT, 366c825d4dcSJohn Baldwin sc->io.reg, as.res[i]); 367c825d4dcSJohn Baldwin free(as.res, M_DEVBUF); 368c825d4dcSJohn Baldwin return (as.error); 369c825d4dcSJohn Baldwin } 370c825d4dcSJohn Baldwin KASSERT(as.count == new_count, ("%s: count mismatch", __func__)); 371c825d4dcSJohn Baldwin 372c825d4dcSJohn Baldwin /* Third, add the ranges to the window. */ 373c825d4dcSJohn Baldwin pcib_add_window_resources(&sc->io, as.res, as.count); 374c825d4dcSJohn Baldwin free(as.res, M_DEVBUF); 375c825d4dcSJohn Baldwin return (0); 376c825d4dcSJohn Baldwin } 377c825d4dcSJohn Baldwin 37883c41143SJohn Baldwin static void 37983c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type, 38083c41143SJohn Baldwin int flags, pci_addr_t max_address) 38183c41143SJohn Baldwin { 382c825d4dcSJohn Baldwin struct resource *res; 38383c41143SJohn Baldwin char buf[64]; 38483c41143SJohn Baldwin int error, rid; 38583c41143SJohn Baldwin 38683c41143SJohn Baldwin if (max_address != (u_long)max_address) 38783c41143SJohn Baldwin max_address = ~0ul; 38883c41143SJohn Baldwin w->rman.rm_start = 0; 38983c41143SJohn Baldwin w->rman.rm_end = max_address; 39083c41143SJohn Baldwin w->rman.rm_type = RMAN_ARRAY; 39183c41143SJohn Baldwin snprintf(buf, sizeof(buf), "%s %s window", 39283c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name); 39383c41143SJohn Baldwin w->rman.rm_descr = strdup(buf, M_DEVBUF); 39483c41143SJohn Baldwin error = rman_init(&w->rman); 39583c41143SJohn Baldwin if (error) 39683c41143SJohn Baldwin panic("Failed to initialize %s %s rman", 39783c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name); 39883c41143SJohn Baldwin 39983c41143SJohn Baldwin if (!pcib_is_window_open(w)) 40083c41143SJohn Baldwin return; 40183c41143SJohn Baldwin 40283c41143SJohn Baldwin if (w->base > max_address || w->limit > max_address) { 40383c41143SJohn Baldwin device_printf(sc->dev, 40483c41143SJohn Baldwin "initial %s window has too many bits, ignoring\n", w->name); 40583c41143SJohn Baldwin return; 40683c41143SJohn Baldwin } 407c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE) 408c825d4dcSJohn Baldwin (void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit); 409c825d4dcSJohn Baldwin else { 41083c41143SJohn Baldwin rid = w->reg; 411c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit, 41283c41143SJohn Baldwin w->limit - w->base + 1, flags); 413c825d4dcSJohn Baldwin if (res != NULL) 414c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 415c825d4dcSJohn Baldwin } 41683c41143SJohn Baldwin if (w->res == NULL) { 41783c41143SJohn Baldwin device_printf(sc->dev, 41883c41143SJohn Baldwin "failed to allocate initial %s window: %#jx-%#jx\n", 41983c41143SJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 42083c41143SJohn Baldwin w->base = max_address; 42183c41143SJohn Baldwin w->limit = 0; 42283c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 42383c41143SJohn Baldwin return; 42483c41143SJohn Baldwin } 42583c41143SJohn Baldwin pcib_activate_window(sc, type); 42683c41143SJohn Baldwin } 42783c41143SJohn Baldwin 42883c41143SJohn Baldwin /* 42983c41143SJohn Baldwin * Initialize I/O windows. 43083c41143SJohn Baldwin */ 43183c41143SJohn Baldwin static void 43283c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc) 43383c41143SJohn Baldwin { 43483c41143SJohn Baldwin pci_addr_t max; 43583c41143SJohn Baldwin device_t dev; 43683c41143SJohn Baldwin uint32_t val; 43783c41143SJohn Baldwin 43883c41143SJohn Baldwin dev = sc->dev; 43983c41143SJohn Baldwin 4400070c94bSJohn Baldwin if (pci_clear_pcib) { 4410070c94bSJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 4420070c94bSJohn Baldwin pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2); 4430070c94bSJohn Baldwin pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1); 4440070c94bSJohn Baldwin pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2); 4450070c94bSJohn Baldwin pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2); 4460070c94bSJohn Baldwin pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2); 4470070c94bSJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 4480070c94bSJohn Baldwin pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4); 4490070c94bSJohn Baldwin pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2); 4500070c94bSJohn Baldwin pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4); 4510070c94bSJohn Baldwin } 4520070c94bSJohn Baldwin 45383c41143SJohn Baldwin /* Determine if the I/O port window is implemented. */ 45483c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 45583c41143SJohn Baldwin if (val == 0) { 45683c41143SJohn Baldwin /* 45783c41143SJohn Baldwin * If 'val' is zero, then only 16-bits of I/O space 45883c41143SJohn Baldwin * are supported. 45983c41143SJohn Baldwin */ 46083c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 46183c41143SJohn Baldwin if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) { 46283c41143SJohn Baldwin sc->io.valid = 1; 46383c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0, 1); 46483c41143SJohn Baldwin } 46583c41143SJohn Baldwin } else 46683c41143SJohn Baldwin sc->io.valid = 1; 46783c41143SJohn Baldwin 46883c41143SJohn Baldwin /* Read the existing I/O port window. */ 46983c41143SJohn Baldwin if (sc->io.valid) { 47083c41143SJohn Baldwin sc->io.reg = PCIR_IOBASEL_1; 47183c41143SJohn Baldwin sc->io.step = 12; 47283c41143SJohn Baldwin sc->io.mask = WIN_IO; 47383c41143SJohn Baldwin sc->io.name = "I/O port"; 47483c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 47583c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE( 47683c41143SJohn Baldwin pci_read_config(dev, PCIR_IOBASEH_1, 2), val); 47783c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT( 47883c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITH_1, 2), 47983c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 48083c41143SJohn Baldwin max = 0xffffffff; 48183c41143SJohn Baldwin } else { 48283c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE(0, val); 48383c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT(0, 48483c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 48583c41143SJohn Baldwin max = 0xffff; 48683c41143SJohn Baldwin } 48783c41143SJohn Baldwin pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max); 48883c41143SJohn Baldwin } 48983c41143SJohn Baldwin 49083c41143SJohn Baldwin /* Read the existing memory window. */ 49183c41143SJohn Baldwin sc->mem.valid = 1; 49283c41143SJohn Baldwin sc->mem.reg = PCIR_MEMBASE_1; 49383c41143SJohn Baldwin sc->mem.step = 20; 49483c41143SJohn Baldwin sc->mem.mask = WIN_MEM; 49583c41143SJohn Baldwin sc->mem.name = "memory"; 49683c41143SJohn Baldwin sc->mem.base = PCI_PPBMEMBASE(0, 49783c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMBASE_1, 2)); 49883c41143SJohn Baldwin sc->mem.limit = PCI_PPBMEMLIMIT(0, 49983c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 50083c41143SJohn Baldwin pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff); 50183c41143SJohn Baldwin 50283c41143SJohn Baldwin /* Determine if the prefetchable memory window is implemented. */ 50383c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 50483c41143SJohn Baldwin if (val == 0) { 50583c41143SJohn Baldwin /* 50683c41143SJohn Baldwin * If 'val' is zero, then only 32-bits of memory space 50783c41143SJohn Baldwin * are supported. 50883c41143SJohn Baldwin */ 50983c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 51083c41143SJohn Baldwin if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) { 51183c41143SJohn Baldwin sc->pmem.valid = 1; 51283c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0, 2); 51383c41143SJohn Baldwin } 51483c41143SJohn Baldwin } else 51583c41143SJohn Baldwin sc->pmem.valid = 1; 51683c41143SJohn Baldwin 51783c41143SJohn Baldwin /* Read the existing prefetchable memory window. */ 51883c41143SJohn Baldwin if (sc->pmem.valid) { 51983c41143SJohn Baldwin sc->pmem.reg = PCIR_PMBASEL_1; 52083c41143SJohn Baldwin sc->pmem.step = 20; 52183c41143SJohn Baldwin sc->pmem.mask = WIN_PMEM; 52283c41143SJohn Baldwin sc->pmem.name = "prefetch"; 52383c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 52483c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE( 52583c41143SJohn Baldwin pci_read_config(dev, PCIR_PMBASEH_1, 4), val); 52683c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT( 52783c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITH_1, 4), 52883c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 52983c41143SJohn Baldwin max = 0xffffffffffffffff; 53083c41143SJohn Baldwin } else { 53183c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE(0, val); 53283c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT(0, 53383c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 53483c41143SJohn Baldwin max = 0xffffffff; 53583c41143SJohn Baldwin } 53683c41143SJohn Baldwin pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY, 53783c41143SJohn Baldwin RF_PREFETCHABLE, max); 53883c41143SJohn Baldwin } 53983c41143SJohn Baldwin } 54083c41143SJohn Baldwin 5414edef187SJohn Baldwin #ifdef PCI_RES_BUS 5424edef187SJohn Baldwin /* 5434edef187SJohn Baldwin * Allocate a suitable secondary bus for this bridge if needed and 5444edef187SJohn Baldwin * initialize the resource manager for the secondary bus range. Note 5454edef187SJohn Baldwin * that the minimum count is a desired value and this may allocate a 5464edef187SJohn Baldwin * smaller range. 5474edef187SJohn Baldwin */ 5484edef187SJohn Baldwin void 5494edef187SJohn Baldwin pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count) 5504edef187SJohn Baldwin { 5514edef187SJohn Baldwin char buf[64]; 5524edef187SJohn Baldwin int error, rid; 5534edef187SJohn Baldwin 5544edef187SJohn Baldwin switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) { 5554edef187SJohn Baldwin case PCIM_HDRTYPE_BRIDGE: 5564edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_1; 5574edef187SJohn Baldwin break; 5584edef187SJohn Baldwin case PCIM_HDRTYPE_CARDBUS: 5594edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_2; 5604edef187SJohn Baldwin break; 5614edef187SJohn Baldwin default: 5624edef187SJohn Baldwin panic("not a PCI bridge"); 5634edef187SJohn Baldwin } 5644edef187SJohn Baldwin bus->dev = dev; 5654edef187SJohn Baldwin bus->rman.rm_start = 0; 5664edef187SJohn Baldwin bus->rman.rm_end = PCI_BUSMAX; 5674edef187SJohn Baldwin bus->rman.rm_type = RMAN_ARRAY; 5684edef187SJohn Baldwin snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev)); 5694edef187SJohn Baldwin bus->rman.rm_descr = strdup(buf, M_DEVBUF); 5704edef187SJohn Baldwin error = rman_init(&bus->rman); 5714edef187SJohn Baldwin if (error) 5724edef187SJohn Baldwin panic("Failed to initialize %s bus number rman", 5734edef187SJohn Baldwin device_get_nameunit(dev)); 5744edef187SJohn Baldwin 5754edef187SJohn Baldwin /* 5764edef187SJohn Baldwin * Allocate a bus range. This will return an existing bus range 5774edef187SJohn Baldwin * if one exists, or a new bus range if one does not. 5784edef187SJohn Baldwin */ 5794edef187SJohn Baldwin rid = 0; 5804edef187SJohn Baldwin bus->res = bus_alloc_resource(dev, PCI_RES_BUS, &rid, 0ul, ~0ul, 5814edef187SJohn Baldwin min_count, 0); 5824edef187SJohn Baldwin if (bus->res == NULL) { 5834edef187SJohn Baldwin /* 5844edef187SJohn Baldwin * Fall back to just allocating a range of a single bus 5854edef187SJohn Baldwin * number. 5864edef187SJohn Baldwin */ 5874edef187SJohn Baldwin bus->res = bus_alloc_resource(dev, PCI_RES_BUS, &rid, 0ul, ~0ul, 5884edef187SJohn Baldwin 1, 0); 5894edef187SJohn Baldwin } else if (rman_get_size(bus->res) < min_count) 5904edef187SJohn Baldwin /* 5914edef187SJohn Baldwin * Attempt to grow the existing range to satisfy the 5924edef187SJohn Baldwin * minimum desired count. 5934edef187SJohn Baldwin */ 5944edef187SJohn Baldwin (void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res, 5954edef187SJohn Baldwin rman_get_start(bus->res), rman_get_start(bus->res) + 5964edef187SJohn Baldwin min_count - 1); 5974edef187SJohn Baldwin 5984edef187SJohn Baldwin /* 5994edef187SJohn Baldwin * Add the initial resource to the rman. 6004edef187SJohn Baldwin */ 6014edef187SJohn Baldwin if (bus->res != NULL) { 6024edef187SJohn Baldwin error = rman_manage_region(&bus->rman, rman_get_start(bus->res), 6034edef187SJohn Baldwin rman_get_end(bus->res)); 6044edef187SJohn Baldwin if (error) 6054edef187SJohn Baldwin panic("Failed to add resource to rman"); 6064edef187SJohn Baldwin bus->sec = rman_get_start(bus->res); 6074edef187SJohn Baldwin bus->sub = rman_get_end(bus->res); 6084edef187SJohn Baldwin } 6094edef187SJohn Baldwin } 6104edef187SJohn Baldwin 6114edef187SJohn Baldwin static struct resource * 6124edef187SJohn Baldwin pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid, 6134edef187SJohn Baldwin u_long start, u_long end, u_long count, u_int flags) 6144edef187SJohn Baldwin { 6154edef187SJohn Baldwin struct resource *res; 6164edef187SJohn Baldwin 6174edef187SJohn Baldwin res = rman_reserve_resource(&bus->rman, start, end, count, flags, 6184edef187SJohn Baldwin child); 6194edef187SJohn Baldwin if (res == NULL) 6204edef187SJohn Baldwin return (NULL); 6214edef187SJohn Baldwin 6224edef187SJohn Baldwin if (bootverbose) 6234edef187SJohn Baldwin device_printf(bus->dev, 6244edef187SJohn Baldwin "allocated bus range (%lu-%lu) for rid %d of %s\n", 6254edef187SJohn Baldwin rman_get_start(res), rman_get_end(res), *rid, 6264edef187SJohn Baldwin pcib_child_name(child)); 6274edef187SJohn Baldwin rman_set_rid(res, *rid); 6284edef187SJohn Baldwin return (res); 6294edef187SJohn Baldwin } 6304edef187SJohn Baldwin 6314edef187SJohn Baldwin /* 6324edef187SJohn Baldwin * Attempt to grow the secondary bus range. This is much simpler than 6334edef187SJohn Baldwin * for I/O windows as the range can only be grown by increasing 6344edef187SJohn Baldwin * subbus. 6354edef187SJohn Baldwin */ 6364edef187SJohn Baldwin static int 6374edef187SJohn Baldwin pcib_grow_subbus(struct pcib_secbus *bus, u_long new_end) 6384edef187SJohn Baldwin { 6394edef187SJohn Baldwin u_long old_end; 6404edef187SJohn Baldwin int error; 6414edef187SJohn Baldwin 6424edef187SJohn Baldwin old_end = rman_get_end(bus->res); 6434edef187SJohn Baldwin KASSERT(new_end > old_end, ("attempt to shrink subbus")); 6444edef187SJohn Baldwin error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res, 6454edef187SJohn Baldwin rman_get_start(bus->res), new_end); 6464edef187SJohn Baldwin if (error) 6474edef187SJohn Baldwin return (error); 6484edef187SJohn Baldwin if (bootverbose) 6494edef187SJohn Baldwin device_printf(bus->dev, "grew bus range to %lu-%lu\n", 6504edef187SJohn Baldwin rman_get_start(bus->res), rman_get_end(bus->res)); 6514edef187SJohn Baldwin error = rman_manage_region(&bus->rman, old_end + 1, 6524edef187SJohn Baldwin rman_get_end(bus->res)); 6534edef187SJohn Baldwin if (error) 6544edef187SJohn Baldwin panic("Failed to add resource to rman"); 6554edef187SJohn Baldwin bus->sub = rman_get_end(bus->res); 6564edef187SJohn Baldwin pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1); 6574edef187SJohn Baldwin return (0); 6584edef187SJohn Baldwin } 6594edef187SJohn Baldwin 6604edef187SJohn Baldwin struct resource * 6614edef187SJohn Baldwin pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid, 6624edef187SJohn Baldwin u_long start, u_long end, u_long count, u_int flags) 6634edef187SJohn Baldwin { 6644edef187SJohn Baldwin struct resource *res; 6654edef187SJohn Baldwin u_long start_free, end_free, new_end; 6664edef187SJohn Baldwin 6674edef187SJohn Baldwin /* 6684edef187SJohn Baldwin * First, see if the request can be satisified by the existing 6694edef187SJohn Baldwin * bus range. 6704edef187SJohn Baldwin */ 6714edef187SJohn Baldwin res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags); 6724edef187SJohn Baldwin if (res != NULL) 6734edef187SJohn Baldwin return (res); 6744edef187SJohn Baldwin 6754edef187SJohn Baldwin /* 6764edef187SJohn Baldwin * Figure out a range to grow the bus range. First, find the 6774edef187SJohn Baldwin * first bus number after the last allocated bus in the rman and 6784edef187SJohn Baldwin * enforce that as a minimum starting point for the range. 6794edef187SJohn Baldwin */ 6804edef187SJohn Baldwin if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 || 6814edef187SJohn Baldwin end_free != bus->sub) 6824edef187SJohn Baldwin start_free = bus->sub + 1; 6834edef187SJohn Baldwin if (start_free < start) 6844edef187SJohn Baldwin start_free = start; 6854edef187SJohn Baldwin new_end = start_free + count - 1; 6864edef187SJohn Baldwin 6874edef187SJohn Baldwin /* 6884edef187SJohn Baldwin * See if this new range would satisfy the request if it 6894edef187SJohn Baldwin * succeeds. 6904edef187SJohn Baldwin */ 6914edef187SJohn Baldwin if (new_end > end) 6924edef187SJohn Baldwin return (NULL); 6934edef187SJohn Baldwin 6944edef187SJohn Baldwin /* Finally, attempt to grow the existing resource. */ 6954edef187SJohn Baldwin if (bootverbose) { 6964edef187SJohn Baldwin device_printf(bus->dev, 6974edef187SJohn Baldwin "attempting to grow bus range for %lu buses\n", count); 6984edef187SJohn Baldwin printf("\tback candidate range: %lu-%lu\n", start_free, 6994edef187SJohn Baldwin new_end); 7004edef187SJohn Baldwin } 7014edef187SJohn Baldwin if (pcib_grow_subbus(bus, new_end) == 0) 7024edef187SJohn Baldwin return (pcib_suballoc_bus(bus, child, rid, start, end, count, 7034edef187SJohn Baldwin flags)); 7044edef187SJohn Baldwin return (NULL); 7054edef187SJohn Baldwin } 7064edef187SJohn Baldwin #endif 7074edef187SJohn Baldwin 70883c41143SJohn Baldwin #else 70983c41143SJohn Baldwin 710bb0d0a8eSMike Smith /* 711b0a2d4b8SWarner Losh * Is the prefetch window open (eg, can we allocate memory in it?) 712b0a2d4b8SWarner Losh */ 713b0a2d4b8SWarner Losh static int 714b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc) 715b0a2d4b8SWarner Losh { 716b0a2d4b8SWarner Losh return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit); 717b0a2d4b8SWarner Losh } 718b0a2d4b8SWarner Losh 719b0a2d4b8SWarner Losh /* 720b0a2d4b8SWarner Losh * Is the nonprefetch window open (eg, can we allocate memory in it?) 721b0a2d4b8SWarner Losh */ 722b0a2d4b8SWarner Losh static int 723b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc) 724b0a2d4b8SWarner Losh { 725b0a2d4b8SWarner Losh return (sc->membase > 0 && sc->membase < sc->memlimit); 726b0a2d4b8SWarner Losh } 727b0a2d4b8SWarner Losh 728b0a2d4b8SWarner Losh /* 729b0a2d4b8SWarner Losh * Is the io window open (eg, can we allocate ports in it?) 730b0a2d4b8SWarner Losh */ 731b0a2d4b8SWarner Losh static int 732b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc) 733b0a2d4b8SWarner Losh { 734b0a2d4b8SWarner Losh return (sc->iobase > 0 && sc->iobase < sc->iolimit); 735b0a2d4b8SWarner Losh } 736b0a2d4b8SWarner Losh 737b0a2d4b8SWarner Losh /* 738e36af292SJung-uk Kim * Get current I/O decode. 739e36af292SJung-uk Kim */ 740e36af292SJung-uk Kim static void 741e36af292SJung-uk Kim pcib_get_io_decode(struct pcib_softc *sc) 742e36af292SJung-uk Kim { 743e36af292SJung-uk Kim device_t dev; 744e36af292SJung-uk Kim uint32_t iolow; 745e36af292SJung-uk Kim 746e36af292SJung-uk Kim dev = sc->dev; 747e36af292SJung-uk Kim 748e36af292SJung-uk Kim iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1); 749e36af292SJung-uk Kim if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 750e36af292SJung-uk Kim sc->iobase = PCI_PPBIOBASE( 751e36af292SJung-uk Kim pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow); 752e36af292SJung-uk Kim else 753e36af292SJung-uk Kim sc->iobase = PCI_PPBIOBASE(0, iolow); 754e36af292SJung-uk Kim 755e36af292SJung-uk Kim iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1); 756e36af292SJung-uk Kim if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 757e36af292SJung-uk Kim sc->iolimit = PCI_PPBIOLIMIT( 758e36af292SJung-uk Kim pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow); 759e36af292SJung-uk Kim else 760e36af292SJung-uk Kim sc->iolimit = PCI_PPBIOLIMIT(0, iolow); 761e36af292SJung-uk Kim } 762e36af292SJung-uk Kim 763e36af292SJung-uk Kim /* 764e36af292SJung-uk Kim * Get current memory decode. 765e36af292SJung-uk Kim */ 766e36af292SJung-uk Kim static void 767e36af292SJung-uk Kim pcib_get_mem_decode(struct pcib_softc *sc) 768e36af292SJung-uk Kim { 769e36af292SJung-uk Kim device_t dev; 770e36af292SJung-uk Kim pci_addr_t pmemlow; 771e36af292SJung-uk Kim 772e36af292SJung-uk Kim dev = sc->dev; 773e36af292SJung-uk Kim 774e36af292SJung-uk Kim sc->membase = PCI_PPBMEMBASE(0, 775e36af292SJung-uk Kim pci_read_config(dev, PCIR_MEMBASE_1, 2)); 776e36af292SJung-uk Kim sc->memlimit = PCI_PPBMEMLIMIT(0, 777e36af292SJung-uk Kim pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 778e36af292SJung-uk Kim 779e36af292SJung-uk Kim pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2); 780e36af292SJung-uk Kim if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 781e36af292SJung-uk Kim sc->pmembase = PCI_PPBMEMBASE( 782e36af292SJung-uk Kim pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow); 783e36af292SJung-uk Kim else 784e36af292SJung-uk Kim sc->pmembase = PCI_PPBMEMBASE(0, pmemlow); 785e36af292SJung-uk Kim 786e36af292SJung-uk Kim pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2); 787e36af292SJung-uk Kim if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 788e36af292SJung-uk Kim sc->pmemlimit = PCI_PPBMEMLIMIT( 789e36af292SJung-uk Kim pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow); 790e36af292SJung-uk Kim else 791e36af292SJung-uk Kim sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow); 792e36af292SJung-uk Kim } 793e36af292SJung-uk Kim 794e36af292SJung-uk Kim /* 795e36af292SJung-uk Kim * Restore previous I/O decode. 796e36af292SJung-uk Kim */ 797e36af292SJung-uk Kim static void 798e36af292SJung-uk Kim pcib_set_io_decode(struct pcib_softc *sc) 799e36af292SJung-uk Kim { 800e36af292SJung-uk Kim device_t dev; 801e36af292SJung-uk Kim uint32_t iohi; 802e36af292SJung-uk Kim 803e36af292SJung-uk Kim dev = sc->dev; 804e36af292SJung-uk Kim 805e36af292SJung-uk Kim iohi = sc->iobase >> 16; 806e36af292SJung-uk Kim if (iohi > 0) 807e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2); 808e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1); 809e36af292SJung-uk Kim 810e36af292SJung-uk Kim iohi = sc->iolimit >> 16; 811e36af292SJung-uk Kim if (iohi > 0) 812e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2); 813e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1); 814e36af292SJung-uk Kim } 815e36af292SJung-uk Kim 816e36af292SJung-uk Kim /* 817e36af292SJung-uk Kim * Restore previous memory decode. 818e36af292SJung-uk Kim */ 819e36af292SJung-uk Kim static void 820e36af292SJung-uk Kim pcib_set_mem_decode(struct pcib_softc *sc) 821e36af292SJung-uk Kim { 822e36af292SJung-uk Kim device_t dev; 823e36af292SJung-uk Kim pci_addr_t pmemhi; 824e36af292SJung-uk Kim 825e36af292SJung-uk Kim dev = sc->dev; 826e36af292SJung-uk Kim 827e36af292SJung-uk Kim pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2); 828e36af292SJung-uk Kim pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2); 829e36af292SJung-uk Kim 830e36af292SJung-uk Kim pmemhi = sc->pmembase >> 32; 831e36af292SJung-uk Kim if (pmemhi > 0) 832e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4); 833e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2); 834e36af292SJung-uk Kim 835e36af292SJung-uk Kim pmemhi = sc->pmemlimit >> 32; 836e36af292SJung-uk Kim if (pmemhi > 0) 837e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4); 838e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2); 839e36af292SJung-uk Kim } 84083c41143SJohn Baldwin #endif 841e36af292SJung-uk Kim 842e36af292SJung-uk Kim /* 843e36af292SJung-uk Kim * Get current bridge configuration. 844e36af292SJung-uk Kim */ 845e36af292SJung-uk Kim static void 846e36af292SJung-uk Kim pcib_cfg_save(struct pcib_softc *sc) 847e36af292SJung-uk Kim { 848e36af292SJung-uk Kim device_t dev; 849e36af292SJung-uk Kim 850e36af292SJung-uk Kim dev = sc->dev; 851e36af292SJung-uk Kim 852e36af292SJung-uk Kim sc->command = pci_read_config(dev, PCIR_COMMAND, 2); 853e36af292SJung-uk Kim sc->pribus = pci_read_config(dev, PCIR_PRIBUS_1, 1); 8544edef187SJohn Baldwin sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1); 8554edef187SJohn Baldwin sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1); 856e36af292SJung-uk Kim sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 857e36af292SJung-uk Kim sc->seclat = pci_read_config(dev, PCIR_SECLAT_1, 1); 85883c41143SJohn Baldwin #ifndef NEW_PCIB 859e36af292SJung-uk Kim if (sc->command & PCIM_CMD_PORTEN) 860e36af292SJung-uk Kim pcib_get_io_decode(sc); 861e36af292SJung-uk Kim if (sc->command & PCIM_CMD_MEMEN) 862e36af292SJung-uk Kim pcib_get_mem_decode(sc); 86383c41143SJohn Baldwin #endif 864e36af292SJung-uk Kim } 865e36af292SJung-uk Kim 866e36af292SJung-uk Kim /* 867e36af292SJung-uk Kim * Restore previous bridge configuration. 868e36af292SJung-uk Kim */ 869e36af292SJung-uk Kim static void 870e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc) 871e36af292SJung-uk Kim { 872e36af292SJung-uk Kim device_t dev; 873e36af292SJung-uk Kim 874e36af292SJung-uk Kim dev = sc->dev; 875e36af292SJung-uk Kim 876e36af292SJung-uk Kim pci_write_config(dev, PCIR_COMMAND, sc->command, 2); 877e36af292SJung-uk Kim pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1); 8784edef187SJohn Baldwin pci_write_config(dev, PCIR_SECBUS_1, sc->bus.sec, 1); 8794edef187SJohn Baldwin pci_write_config(dev, PCIR_SUBBUS_1, sc->bus.sub, 1); 880e36af292SJung-uk Kim pci_write_config(dev, PCIR_BRIDGECTL_1, sc->bridgectl, 2); 881e36af292SJung-uk Kim pci_write_config(dev, PCIR_SECLAT_1, sc->seclat, 1); 88283c41143SJohn Baldwin #ifdef NEW_PCIB 88383c41143SJohn Baldwin pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM); 88483c41143SJohn Baldwin #else 885e36af292SJung-uk Kim if (sc->command & PCIM_CMD_PORTEN) 886e36af292SJung-uk Kim pcib_set_io_decode(sc); 887e36af292SJung-uk Kim if (sc->command & PCIM_CMD_MEMEN) 888e36af292SJung-uk Kim pcib_set_mem_decode(sc); 88983c41143SJohn Baldwin #endif 890e36af292SJung-uk Kim } 891e36af292SJung-uk Kim 892e36af292SJung-uk Kim /* 893bb0d0a8eSMike Smith * Generic device interface 894bb0d0a8eSMike Smith */ 895bb0d0a8eSMike Smith static int 896bb0d0a8eSMike Smith pcib_probe(device_t dev) 897bb0d0a8eSMike Smith { 898bb0d0a8eSMike Smith if ((pci_get_class(dev) == PCIC_BRIDGE) && 899bb0d0a8eSMike Smith (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) { 900bb0d0a8eSMike Smith device_set_desc(dev, "PCI-PCI bridge"); 901bb0d0a8eSMike Smith return(-10000); 902bb0d0a8eSMike Smith } 903bb0d0a8eSMike Smith return(ENXIO); 904bb0d0a8eSMike Smith } 905bb0d0a8eSMike Smith 9066f0d5884SJohn Baldwin void 9076f0d5884SJohn Baldwin pcib_attach_common(device_t dev) 908bb0d0a8eSMike Smith { 909bb0d0a8eSMike Smith struct pcib_softc *sc; 910abf07f13SWarner Losh struct sysctl_ctx_list *sctx; 911abf07f13SWarner Losh struct sysctl_oid *soid; 912c825d4dcSJohn Baldwin int comma; 913bb0d0a8eSMike Smith 914bb0d0a8eSMike Smith sc = device_get_softc(dev); 915bb0d0a8eSMike Smith sc->dev = dev; 916bb0d0a8eSMike Smith 9174fa59183SMike Smith /* 9184fa59183SMike Smith * Get current bridge configuration. 9194fa59183SMike Smith */ 92055aaf894SMarius Strobl sc->domain = pci_get_domain(dev); 9214fa59183SMike Smith sc->secstat = pci_read_config(dev, PCIR_SECSTAT_1, 2); 922e36af292SJung-uk Kim pcib_cfg_save(sc); 9234fa59183SMike Smith 9244fa59183SMike Smith /* 9254edef187SJohn Baldwin * The primary bus register should always be the bus of the 9264edef187SJohn Baldwin * parent. 9274edef187SJohn Baldwin */ 9284edef187SJohn Baldwin sc->pribus = pci_get_bus(dev); 9294edef187SJohn Baldwin pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1); 9304edef187SJohn Baldwin 9314edef187SJohn Baldwin /* 932abf07f13SWarner Losh * Setup sysctl reporting nodes 933abf07f13SWarner Losh */ 934abf07f13SWarner Losh sctx = device_get_sysctl_ctx(dev); 935abf07f13SWarner Losh soid = device_get_sysctl_tree(dev); 936abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain", 937abf07f13SWarner Losh CTLFLAG_RD, &sc->domain, 0, "Domain number"); 938abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus", 939abf07f13SWarner Losh CTLFLAG_RD, &sc->pribus, 0, "Primary bus number"); 940abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus", 9414edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number"); 942abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus", 9434edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number"); 944abf07f13SWarner Losh 945abf07f13SWarner Losh /* 9464fa59183SMike Smith * Quirk handling. 9474fa59183SMike Smith */ 9484fa59183SMike Smith switch (pci_get_devid(dev)) { 9494edef187SJohn Baldwin #if !defined(NEW_PCIB) && !defined(PCI_RES_BUS) 9504fa59183SMike Smith case 0x12258086: /* Intel 82454KX/GX (Orion) */ 9514fa59183SMike Smith { 952b0cb115fSWarner Losh uint8_t supbus; 9534fa59183SMike Smith 9544fa59183SMike Smith supbus = pci_read_config(dev, 0x41, 1); 9554fa59183SMike Smith if (supbus != 0xff) { 9564edef187SJohn Baldwin sc->bus.sec = supbus + 1; 9574edef187SJohn Baldwin sc->bus.sub = supbus + 1; 9584fa59183SMike Smith } 9594fa59183SMike Smith break; 9604fa59183SMike Smith } 9614edef187SJohn Baldwin #endif 9624fa59183SMike Smith 963e4b59fc5SWarner Losh /* 964e4b59fc5SWarner Losh * The i82380FB mobile docking controller is a PCI-PCI bridge, 965e4b59fc5SWarner Losh * and it is a subtractive bridge. However, the ProgIf is wrong 966e4b59fc5SWarner Losh * so the normal setting of PCIB_SUBTRACTIVE bit doesn't 967e4b59fc5SWarner Losh * happen. There's also a Toshiba bridge that behaves this 968e4b59fc5SWarner Losh * way. 969e4b59fc5SWarner Losh */ 970e4b59fc5SWarner Losh case 0x124b8086: /* Intel 82380FB Mobile */ 971e4b59fc5SWarner Losh case 0x060513d7: /* Toshiba ???? */ 972e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 973e4b59fc5SWarner Losh break; 974c94d6dbeSJung-uk Kim 9754edef187SJohn Baldwin #if !defined(NEW_PCIB) && !defined(PCI_RES_BUS) 976c94d6dbeSJung-uk Kim /* Compaq R3000 BIOS sets wrong subordinate bus number. */ 977c94d6dbeSJung-uk Kim case 0x00dd10de: 978c94d6dbeSJung-uk Kim { 979c94d6dbeSJung-uk Kim char *cp; 980c94d6dbeSJung-uk Kim 9811def0ca6SJung-uk Kim if ((cp = getenv("smbios.planar.maker")) == NULL) 982c94d6dbeSJung-uk Kim break; 9831def0ca6SJung-uk Kim if (strncmp(cp, "Compal", 6) != 0) { 9841def0ca6SJung-uk Kim freeenv(cp); 985c94d6dbeSJung-uk Kim break; 9861def0ca6SJung-uk Kim } 9871def0ca6SJung-uk Kim freeenv(cp); 9881def0ca6SJung-uk Kim if ((cp = getenv("smbios.planar.product")) == NULL) 9891def0ca6SJung-uk Kim break; 9901def0ca6SJung-uk Kim if (strncmp(cp, "08A0", 4) != 0) { 9911def0ca6SJung-uk Kim freeenv(cp); 9921def0ca6SJung-uk Kim break; 9931def0ca6SJung-uk Kim } 9941def0ca6SJung-uk Kim freeenv(cp); 9954edef187SJohn Baldwin if (sc->bus.sub < 0xa) { 996c94d6dbeSJung-uk Kim pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1); 9974edef187SJohn Baldwin sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1); 998c94d6dbeSJung-uk Kim } 999c94d6dbeSJung-uk Kim break; 1000c94d6dbeSJung-uk Kim } 10014edef187SJohn Baldwin #endif 1002e4b59fc5SWarner Losh } 1003e4b59fc5SWarner Losh 100422bf1c7fSJohn Baldwin if (pci_msi_device_blacklisted(dev)) 100522bf1c7fSJohn Baldwin sc->flags |= PCIB_DISABLE_MSI; 100622bf1c7fSJohn Baldwin 100768e9cbd3SMarius Strobl if (pci_msix_device_blacklisted(dev)) 100868e9cbd3SMarius Strobl sc->flags |= PCIB_DISABLE_MSIX; 100968e9cbd3SMarius Strobl 1010e4b59fc5SWarner Losh /* 1011e4b59fc5SWarner Losh * Intel 815, 845 and other chipsets say they are PCI-PCI bridges, 1012e4b59fc5SWarner Losh * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM, 1013e4b59fc5SWarner Losh * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese. 1014e4b59fc5SWarner Losh * This means they act as if they were subtractively decoding 1015e4b59fc5SWarner Losh * bridges and pass all transactions. Mark them and real ProgIf 1 1016e4b59fc5SWarner Losh * parts as subtractive. 1017e4b59fc5SWarner Losh */ 1018e4b59fc5SWarner Losh if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 || 1019657d9f9fSJohn Baldwin pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE) 1020e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 1021e4b59fc5SWarner Losh 102283c41143SJohn Baldwin #ifdef NEW_PCIB 10234edef187SJohn Baldwin #ifdef PCI_RES_BUS 10244edef187SJohn Baldwin pcib_setup_secbus(dev, &sc->bus, 1); 10254edef187SJohn Baldwin #endif 102683c41143SJohn Baldwin pcib_probe_windows(sc); 102783c41143SJohn Baldwin #endif 1028bb0d0a8eSMike Smith if (bootverbose) { 102955aaf894SMarius Strobl device_printf(dev, " domain %d\n", sc->domain); 10304edef187SJohn Baldwin device_printf(dev, " secondary bus %d\n", sc->bus.sec); 10314edef187SJohn Baldwin device_printf(dev, " subordinate bus %d\n", sc->bus.sub); 103283c41143SJohn Baldwin #ifdef NEW_PCIB 103383c41143SJohn Baldwin if (pcib_is_window_open(&sc->io)) 103483c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%jx-0x%jx\n", 103583c41143SJohn Baldwin (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit); 103683c41143SJohn Baldwin if (pcib_is_window_open(&sc->mem)) 103783c41143SJohn Baldwin device_printf(dev, " memory decode 0x%jx-0x%jx\n", 103883c41143SJohn Baldwin (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit); 103983c41143SJohn Baldwin if (pcib_is_window_open(&sc->pmem)) 104083c41143SJohn Baldwin device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 104183c41143SJohn Baldwin (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit); 104283c41143SJohn Baldwin #else 104383c41143SJohn Baldwin if (pcib_is_io_open(sc)) 104483c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%x-0x%x\n", 104583c41143SJohn Baldwin sc->iobase, sc->iolimit); 1046b0a2d4b8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 1047b0a2d4b8SWarner Losh device_printf(dev, " memory decode 0x%jx-0x%jx\n", 1048b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit); 1049b0a2d4b8SWarner Losh if (pcib_is_prefetch_open(sc)) 1050b0a2d4b8SWarner Losh device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 1051b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 105283c41143SJohn Baldwin #endif 1053c825d4dcSJohn Baldwin if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) || 1054c825d4dcSJohn Baldwin sc->flags & PCIB_SUBTRACTIVE) { 1055c825d4dcSJohn Baldwin device_printf(dev, " special decode "); 1056c825d4dcSJohn Baldwin comma = 0; 1057c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) { 1058c825d4dcSJohn Baldwin printf("ISA"); 1059c825d4dcSJohn Baldwin comma = 1; 1060c825d4dcSJohn Baldwin } 1061c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) { 1062c825d4dcSJohn Baldwin printf("%sVGA", comma ? ", " : ""); 1063c825d4dcSJohn Baldwin comma = 1; 1064c825d4dcSJohn Baldwin } 1065e4b59fc5SWarner Losh if (sc->flags & PCIB_SUBTRACTIVE) 1066c825d4dcSJohn Baldwin printf("%ssubtractive", comma ? ", " : ""); 1067c825d4dcSJohn Baldwin printf("\n"); 1068c825d4dcSJohn Baldwin } 1069bb0d0a8eSMike Smith } 1070bb0d0a8eSMike Smith 1071bb0d0a8eSMike Smith /* 1072ef888152SJohn Baldwin * Always enable busmastering on bridges so that transactions 1073ef888152SJohn Baldwin * initiated on the secondary bus are passed through to the 1074ef888152SJohn Baldwin * primary bus. 1075ef888152SJohn Baldwin */ 1076ef888152SJohn Baldwin pci_enable_busmaster(dev); 10776f0d5884SJohn Baldwin } 1078bb0d0a8eSMike Smith 107938906aedSJohn Baldwin int 10806f0d5884SJohn Baldwin pcib_attach(device_t dev) 10816f0d5884SJohn Baldwin { 10826f0d5884SJohn Baldwin struct pcib_softc *sc; 10836f0d5884SJohn Baldwin device_t child; 10846f0d5884SJohn Baldwin 10856f0d5884SJohn Baldwin pcib_attach_common(dev); 10866f0d5884SJohn Baldwin sc = device_get_softc(dev); 10874edef187SJohn Baldwin if (sc->bus.sec != 0) { 10884edef187SJohn Baldwin child = device_add_child(dev, "pci", sc->bus.sec); 1089bb0d0a8eSMike Smith if (child != NULL) 1090bb0d0a8eSMike Smith return(bus_generic_attach(dev)); 1091bb0d0a8eSMike Smith } 1092bb0d0a8eSMike Smith 1093bb0d0a8eSMike Smith /* no secondary bus; we should have fixed this */ 1094bb0d0a8eSMike Smith return(0); 1095bb0d0a8eSMike Smith } 1096bb0d0a8eSMike Smith 10976f0d5884SJohn Baldwin int 1098e36af292SJung-uk Kim pcib_suspend(device_t dev) 1099e36af292SJung-uk Kim { 110062508c53SJohn Baldwin device_t pcib; 1101e36af292SJung-uk Kim int dstate, error; 1102e36af292SJung-uk Kim 1103e36af292SJung-uk Kim pcib_cfg_save(device_get_softc(dev)); 1104e36af292SJung-uk Kim error = bus_generic_suspend(dev); 1105f3e0b109SJung-uk Kim if (error == 0 && pci_do_power_suspend) { 1106e36af292SJung-uk Kim dstate = PCI_POWERSTATE_D3; 110762508c53SJohn Baldwin pcib = device_get_parent(device_get_parent(dev)); 110862508c53SJohn Baldwin if (PCIB_POWER_FOR_SLEEP(pcib, dev, &dstate) == 0) 1109e36af292SJung-uk Kim pci_set_powerstate(dev, dstate); 1110e36af292SJung-uk Kim } 1111e36af292SJung-uk Kim return (error); 1112e36af292SJung-uk Kim } 1113e36af292SJung-uk Kim 1114e36af292SJung-uk Kim int 1115e36af292SJung-uk Kim pcib_resume(device_t dev) 1116e36af292SJung-uk Kim { 111762508c53SJohn Baldwin device_t pcib; 1118e36af292SJung-uk Kim 1119e36af292SJung-uk Kim if (pci_do_power_resume) { 112062508c53SJohn Baldwin pcib = device_get_parent(device_get_parent(dev)); 112162508c53SJohn Baldwin if (PCIB_POWER_FOR_SLEEP(pcib, dev, NULL) == 0) 1122e36af292SJung-uk Kim pci_set_powerstate(dev, PCI_POWERSTATE_D0); 1123e36af292SJung-uk Kim } 1124e36af292SJung-uk Kim pcib_cfg_restore(device_get_softc(dev)); 1125e36af292SJung-uk Kim return (bus_generic_resume(dev)); 1126e36af292SJung-uk Kim } 1127e36af292SJung-uk Kim 1128e36af292SJung-uk Kim int 1129bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 1130bb0d0a8eSMike Smith { 1131bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 1132bb0d0a8eSMike Smith 1133bb0d0a8eSMike Smith switch (which) { 113455aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 113555aaf894SMarius Strobl *result = sc->domain; 113655aaf894SMarius Strobl return(0); 1137bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 11384edef187SJohn Baldwin *result = sc->bus.sec; 1139bb0d0a8eSMike Smith return(0); 1140bb0d0a8eSMike Smith } 1141bb0d0a8eSMike Smith return(ENOENT); 1142bb0d0a8eSMike Smith } 1143bb0d0a8eSMike Smith 11446f0d5884SJohn Baldwin int 1145bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 1146bb0d0a8eSMike Smith { 1147bb0d0a8eSMike Smith 1148bb0d0a8eSMike Smith switch (which) { 114955aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 115055aaf894SMarius Strobl return(EINVAL); 1151bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 11524edef187SJohn Baldwin return(EINVAL); 1153bb0d0a8eSMike Smith } 1154bb0d0a8eSMike Smith return(ENOENT); 1155bb0d0a8eSMike Smith } 1156bb0d0a8eSMike Smith 115783c41143SJohn Baldwin #ifdef NEW_PCIB 115883c41143SJohn Baldwin /* 115983c41143SJohn Baldwin * Attempt to allocate a resource from the existing resources assigned 116083c41143SJohn Baldwin * to a window. 116183c41143SJohn Baldwin */ 116283c41143SJohn Baldwin static struct resource * 116383c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w, 116483c41143SJohn Baldwin device_t child, int type, int *rid, u_long start, u_long end, u_long count, 116583c41143SJohn Baldwin u_int flags) 116683c41143SJohn Baldwin { 116783c41143SJohn Baldwin struct resource *res; 116883c41143SJohn Baldwin 116983c41143SJohn Baldwin if (!pcib_is_window_open(w)) 117083c41143SJohn Baldwin return (NULL); 117183c41143SJohn Baldwin 117283c41143SJohn Baldwin res = rman_reserve_resource(&w->rman, start, end, count, 117383c41143SJohn Baldwin flags & ~RF_ACTIVE, child); 117483c41143SJohn Baldwin if (res == NULL) 117583c41143SJohn Baldwin return (NULL); 117683c41143SJohn Baldwin 117783c41143SJohn Baldwin if (bootverbose) 117883c41143SJohn Baldwin device_printf(sc->dev, 117983c41143SJohn Baldwin "allocated %s range (%#lx-%#lx) for rid %x of %s\n", 118083c41143SJohn Baldwin w->name, rman_get_start(res), rman_get_end(res), *rid, 118183c41143SJohn Baldwin pcib_child_name(child)); 118283c41143SJohn Baldwin rman_set_rid(res, *rid); 118383c41143SJohn Baldwin 118483c41143SJohn Baldwin /* 118583c41143SJohn Baldwin * If the resource should be active, pass that request up the 118683c41143SJohn Baldwin * tree. This assumes the parent drivers can handle 118783c41143SJohn Baldwin * activating sub-allocated resources. 118883c41143SJohn Baldwin */ 118983c41143SJohn Baldwin if (flags & RF_ACTIVE) { 119083c41143SJohn Baldwin if (bus_activate_resource(child, type, *rid, res) != 0) { 119183c41143SJohn Baldwin rman_release_resource(res); 119283c41143SJohn Baldwin return (NULL); 119383c41143SJohn Baldwin } 119483c41143SJohn Baldwin } 119583c41143SJohn Baldwin 119683c41143SJohn Baldwin return (res); 119783c41143SJohn Baldwin } 119883c41143SJohn Baldwin 1199c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */ 1200c825d4dcSJohn Baldwin static int 1201c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type, 1202c825d4dcSJohn Baldwin u_long start, u_long end, u_long count, u_int flags) 1203c825d4dcSJohn Baldwin { 1204c825d4dcSJohn Baldwin struct resource *res; 1205c825d4dcSJohn Baldwin u_long base, limit, wmask; 1206c825d4dcSJohn Baldwin int rid; 1207c825d4dcSJohn Baldwin 1208c825d4dcSJohn Baldwin /* 1209c825d4dcSJohn Baldwin * If this is an I/O window on a bridge with ISA enable set 1210c825d4dcSJohn Baldwin * and the start address is below 64k, then try to allocate an 1211c825d4dcSJohn Baldwin * initial window of 0x1000 bytes long starting at address 1212c825d4dcSJohn Baldwin * 0xf000 and walking down. Note that if the original request 1213c825d4dcSJohn Baldwin * was larger than the non-aliased range size of 0x100 our 1214c825d4dcSJohn Baldwin * caller would have raised the start address up to 64k 1215c825d4dcSJohn Baldwin * already. 1216c825d4dcSJohn Baldwin */ 1217c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1218c825d4dcSJohn Baldwin start < 65536) { 1219c825d4dcSJohn Baldwin for (base = 0xf000; (long)base >= 0; base -= 0x1000) { 1220c825d4dcSJohn Baldwin limit = base + 0xfff; 1221c825d4dcSJohn Baldwin 1222c825d4dcSJohn Baldwin /* 1223c825d4dcSJohn Baldwin * Skip ranges that wouldn't work for the 1224c825d4dcSJohn Baldwin * original request. Note that the actual 1225c825d4dcSJohn Baldwin * window that overlaps are the non-alias 1226c825d4dcSJohn Baldwin * ranges within [base, limit], so this isn't 1227c825d4dcSJohn Baldwin * quite a simple comparison. 1228c825d4dcSJohn Baldwin */ 1229c825d4dcSJohn Baldwin if (start + count > limit - 0x400) 1230c825d4dcSJohn Baldwin continue; 1231c825d4dcSJohn Baldwin if (base == 0) { 1232c825d4dcSJohn Baldwin /* 1233c825d4dcSJohn Baldwin * The first open region for the window at 1234c825d4dcSJohn Baldwin * 0 is 0x400-0x4ff. 1235c825d4dcSJohn Baldwin */ 1236c825d4dcSJohn Baldwin if (end - count + 1 < 0x400) 1237c825d4dcSJohn Baldwin continue; 1238c825d4dcSJohn Baldwin } else { 1239c825d4dcSJohn Baldwin if (end - count + 1 < base) 1240c825d4dcSJohn Baldwin continue; 1241c825d4dcSJohn Baldwin } 1242c825d4dcSJohn Baldwin 1243c825d4dcSJohn Baldwin if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) { 1244c825d4dcSJohn Baldwin w->base = base; 1245c825d4dcSJohn Baldwin w->limit = limit; 1246c825d4dcSJohn Baldwin return (0); 1247c825d4dcSJohn Baldwin } 1248c825d4dcSJohn Baldwin } 1249c825d4dcSJohn Baldwin return (ENOSPC); 1250c825d4dcSJohn Baldwin } 1251c825d4dcSJohn Baldwin 1252c825d4dcSJohn Baldwin wmask = (1ul << w->step) - 1; 1253c825d4dcSJohn Baldwin if (RF_ALIGNMENT(flags) < w->step) { 1254c825d4dcSJohn Baldwin flags &= ~RF_ALIGNMENT_MASK; 1255c825d4dcSJohn Baldwin flags |= RF_ALIGNMENT_LOG2(w->step); 1256c825d4dcSJohn Baldwin } 1257c825d4dcSJohn Baldwin start &= ~wmask; 1258c825d4dcSJohn Baldwin end |= wmask; 1259c825d4dcSJohn Baldwin count = roundup2(count, 1ul << w->step); 1260c825d4dcSJohn Baldwin rid = w->reg; 1261c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, start, end, count, 1262c825d4dcSJohn Baldwin flags & ~RF_ACTIVE); 1263c825d4dcSJohn Baldwin if (res == NULL) 1264c825d4dcSJohn Baldwin return (ENOSPC); 1265c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 1266c825d4dcSJohn Baldwin pcib_activate_window(sc, type); 1267c825d4dcSJohn Baldwin w->base = rman_get_start(res); 1268c825d4dcSJohn Baldwin w->limit = rman_get_end(res); 1269c825d4dcSJohn Baldwin return (0); 1270c825d4dcSJohn Baldwin } 1271c825d4dcSJohn Baldwin 1272c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */ 1273c825d4dcSJohn Baldwin static int 1274c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type, 1275c825d4dcSJohn Baldwin u_long base, u_long limit) 1276c825d4dcSJohn Baldwin { 1277c825d4dcSJohn Baldwin struct resource *res; 1278c825d4dcSJohn Baldwin int error, i, force_64k_base; 1279c825d4dcSJohn Baldwin 1280c825d4dcSJohn Baldwin KASSERT(base <= w->base && limit >= w->limit, 1281c825d4dcSJohn Baldwin ("attempting to shrink window")); 1282c825d4dcSJohn Baldwin 1283c825d4dcSJohn Baldwin /* 1284c825d4dcSJohn Baldwin * XXX: pcib_grow_window() doesn't try to do this anyway and 1285c825d4dcSJohn Baldwin * the error handling for all the edge cases would be tedious. 1286c825d4dcSJohn Baldwin */ 1287c825d4dcSJohn Baldwin KASSERT(limit == w->limit || base == w->base, 1288c825d4dcSJohn Baldwin ("attempting to grow both ends of a window")); 1289c825d4dcSJohn Baldwin 1290c825d4dcSJohn Baldwin /* 1291c825d4dcSJohn Baldwin * Yet more special handling for requests to expand an I/O 1292c825d4dcSJohn Baldwin * window behind an ISA-enabled bridge. Since I/O windows 1293c825d4dcSJohn Baldwin * have to grow in 0x1000 increments and the end of the 0xffff 1294c825d4dcSJohn Baldwin * range is an alias, growing a window below 64k will always 1295c825d4dcSJohn Baldwin * result in allocating new resources and never adjusting an 1296c825d4dcSJohn Baldwin * existing resource. 1297c825d4dcSJohn Baldwin */ 1298c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1299c825d4dcSJohn Baldwin (limit <= 65535 || (base <= 65535 && base != w->base))) { 1300c825d4dcSJohn Baldwin KASSERT(limit == w->limit || limit <= 65535, 1301c825d4dcSJohn Baldwin ("attempting to grow both ends across 64k ISA alias")); 1302c825d4dcSJohn Baldwin 1303c825d4dcSJohn Baldwin if (base != w->base) 1304c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1); 1305c825d4dcSJohn Baldwin else 1306c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, w->limit + 1, 1307c825d4dcSJohn Baldwin limit); 1308c825d4dcSJohn Baldwin if (error == 0) { 1309c825d4dcSJohn Baldwin w->base = base; 1310c825d4dcSJohn Baldwin w->limit = limit; 1311c825d4dcSJohn Baldwin } 1312c825d4dcSJohn Baldwin return (error); 1313c825d4dcSJohn Baldwin } 1314c825d4dcSJohn Baldwin 1315c825d4dcSJohn Baldwin /* 1316c825d4dcSJohn Baldwin * Find the existing resource to adjust. Usually there is only one, 1317c825d4dcSJohn Baldwin * but for an ISA-enabled bridge we might be growing the I/O window 1318c825d4dcSJohn Baldwin * above 64k and need to find the existing resource that maps all 1319c825d4dcSJohn Baldwin * of the area above 64k. 1320c825d4dcSJohn Baldwin */ 1321c825d4dcSJohn Baldwin for (i = 0; i < w->count; i++) { 1322c825d4dcSJohn Baldwin if (rman_get_end(w->res[i]) == w->limit) 1323c825d4dcSJohn Baldwin break; 1324c825d4dcSJohn Baldwin } 1325c825d4dcSJohn Baldwin KASSERT(i != w->count, ("did not find existing resource")); 1326c825d4dcSJohn Baldwin res = w->res[i]; 1327c825d4dcSJohn Baldwin 1328c825d4dcSJohn Baldwin /* 1329c825d4dcSJohn Baldwin * Usually the resource we found should match the window's 1330c825d4dcSJohn Baldwin * existing range. The one exception is the ISA-enabled case 1331c825d4dcSJohn Baldwin * mentioned above in which case the resource should start at 1332c825d4dcSJohn Baldwin * 64k. 1333c825d4dcSJohn Baldwin */ 1334c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1335c825d4dcSJohn Baldwin w->base <= 65535) { 1336c825d4dcSJohn Baldwin KASSERT(rman_get_start(res) == 65536, 1337c825d4dcSJohn Baldwin ("existing resource mismatch")); 1338c825d4dcSJohn Baldwin force_64k_base = 1; 1339c825d4dcSJohn Baldwin } else { 1340c825d4dcSJohn Baldwin KASSERT(w->base == rman_get_start(res), 1341c825d4dcSJohn Baldwin ("existing resource mismatch")); 1342c825d4dcSJohn Baldwin force_64k_base = 0; 1343c825d4dcSJohn Baldwin } 1344c825d4dcSJohn Baldwin 1345c825d4dcSJohn Baldwin error = bus_adjust_resource(sc->dev, type, res, force_64k_base ? 1346c825d4dcSJohn Baldwin rman_get_start(res) : base, limit); 1347c825d4dcSJohn Baldwin if (error) 1348c825d4dcSJohn Baldwin return (error); 1349c825d4dcSJohn Baldwin 1350c825d4dcSJohn Baldwin /* Add the newly allocated region to the resource manager. */ 1351c825d4dcSJohn Baldwin if (w->base != base) { 1352c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, base, w->base - 1); 1353c825d4dcSJohn Baldwin w->base = base; 1354c825d4dcSJohn Baldwin } else { 1355c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, w->limit + 1, limit); 1356c825d4dcSJohn Baldwin w->limit = limit; 1357c825d4dcSJohn Baldwin } 1358c825d4dcSJohn Baldwin if (error) { 1359c825d4dcSJohn Baldwin if (bootverbose) 1360c825d4dcSJohn Baldwin device_printf(sc->dev, 1361c825d4dcSJohn Baldwin "failed to expand %s resource manager\n", w->name); 1362c825d4dcSJohn Baldwin (void)bus_adjust_resource(sc->dev, type, res, force_64k_base ? 1363c825d4dcSJohn Baldwin rman_get_start(res) : w->base, w->limit); 1364c825d4dcSJohn Baldwin } 1365c825d4dcSJohn Baldwin return (error); 1366c825d4dcSJohn Baldwin } 1367c825d4dcSJohn Baldwin 136883c41143SJohn Baldwin /* 136983c41143SJohn Baldwin * Attempt to grow a window to make room for a given resource request. 137083c41143SJohn Baldwin */ 137183c41143SJohn Baldwin static int 137283c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type, 137383c41143SJohn Baldwin u_long start, u_long end, u_long count, u_int flags) 137483c41143SJohn Baldwin { 1375a7b5acacSJohn Baldwin u_long align, start_free, end_free, front, back, wmask; 1376c825d4dcSJohn Baldwin int error; 137783c41143SJohn Baldwin 137883c41143SJohn Baldwin /* 137983c41143SJohn Baldwin * Clamp the desired resource range to the maximum address 138083c41143SJohn Baldwin * this window supports. Reject impossible requests. 1381c825d4dcSJohn Baldwin * 1382c825d4dcSJohn Baldwin * For I/O port requests behind a bridge with the ISA enable 1383c825d4dcSJohn Baldwin * bit set, force large allocations to start above 64k. 138483c41143SJohn Baldwin */ 138583c41143SJohn Baldwin if (!w->valid) 138683c41143SJohn Baldwin return (EINVAL); 1387c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 && 1388c825d4dcSJohn Baldwin start < 65536) 1389c825d4dcSJohn Baldwin start = 65536; 139083c41143SJohn Baldwin if (end > w->rman.rm_end) 139183c41143SJohn Baldwin end = w->rman.rm_end; 139283c41143SJohn Baldwin if (start + count - 1 > end || start + count < start) 139383c41143SJohn Baldwin return (EINVAL); 1394a7b5acacSJohn Baldwin wmask = (1ul << w->step) - 1; 139583c41143SJohn Baldwin 139683c41143SJohn Baldwin /* 139783c41143SJohn Baldwin * If there is no resource at all, just try to allocate enough 139883c41143SJohn Baldwin * aligned space for this resource. 139983c41143SJohn Baldwin */ 140083c41143SJohn Baldwin if (w->res == NULL) { 1401c825d4dcSJohn Baldwin error = pcib_alloc_new_window(sc, w, type, start, end, count, 1402c825d4dcSJohn Baldwin flags); 1403c825d4dcSJohn Baldwin if (error) { 140483c41143SJohn Baldwin if (bootverbose) 140583c41143SJohn Baldwin device_printf(sc->dev, 140683c41143SJohn Baldwin "failed to allocate initial %s window (%#lx-%#lx,%#lx)\n", 140783c41143SJohn Baldwin w->name, start, end, count); 140883c41143SJohn Baldwin return (error); 140983c41143SJohn Baldwin } 1410c825d4dcSJohn Baldwin if (bootverbose) 1411c825d4dcSJohn Baldwin device_printf(sc->dev, 1412c825d4dcSJohn Baldwin "allocated initial %s window of %#jx-%#jx\n", 1413c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 141483c41143SJohn Baldwin goto updatewin; 141583c41143SJohn Baldwin } 141683c41143SJohn Baldwin 141783c41143SJohn Baldwin /* 141883c41143SJohn Baldwin * See if growing the window would help. Compute the minimum 141983c41143SJohn Baldwin * amount of address space needed on both the front and back 142083c41143SJohn Baldwin * ends of the existing window to satisfy the allocation. 142183c41143SJohn Baldwin * 142283c41143SJohn Baldwin * For each end, build a candidate region adjusting for the 142383c41143SJohn Baldwin * required alignment, etc. If there is a free region at the 142483c41143SJohn Baldwin * edge of the window, grow from the inner edge of the free 142583c41143SJohn Baldwin * region. Otherwise grow from the window boundary. 142683c41143SJohn Baldwin * 1427c825d4dcSJohn Baldwin * Growing an I/O window below 64k for a bridge with the ISA 1428c825d4dcSJohn Baldwin * enable bit doesn't require any special magic as the step 1429c825d4dcSJohn Baldwin * size of an I/O window (1k) always includes multiple 1430c825d4dcSJohn Baldwin * non-alias ranges when it is grown in either direction. 1431c825d4dcSJohn Baldwin * 143283c41143SJohn Baldwin * XXX: Special case: if w->res is completely empty and the 143383c41143SJohn Baldwin * request size is larger than w->res, we should find the 143483c41143SJohn Baldwin * optimal aligned buffer containing w->res and allocate that. 143583c41143SJohn Baldwin */ 143683c41143SJohn Baldwin if (bootverbose) 143783c41143SJohn Baldwin device_printf(sc->dev, 143883c41143SJohn Baldwin "attempting to grow %s window for (%#lx-%#lx,%#lx)\n", 143983c41143SJohn Baldwin w->name, start, end, count); 144083c41143SJohn Baldwin align = 1ul << RF_ALIGNMENT(flags); 1441c825d4dcSJohn Baldwin if (start < w->base) { 144283c41143SJohn Baldwin if (rman_first_free_region(&w->rman, &start_free, &end_free) != 1443c825d4dcSJohn Baldwin 0 || start_free != w->base) 1444c825d4dcSJohn Baldwin end_free = w->base; 144583c41143SJohn Baldwin if (end_free > end) 1446ddac8cc9SJohn Baldwin end_free = end + 1; 144783c41143SJohn Baldwin 144883c41143SJohn Baldwin /* Move end_free down until it is properly aligned. */ 144983c41143SJohn Baldwin end_free &= ~(align - 1); 1450a49dcb46SJohn Baldwin end_free--; 1451a49dcb46SJohn Baldwin front = end_free - (count - 1); 145283c41143SJohn Baldwin 145383c41143SJohn Baldwin /* 145483c41143SJohn Baldwin * The resource would now be allocated at (front, 145583c41143SJohn Baldwin * end_free). Ensure that fits in the (start, end) 145683c41143SJohn Baldwin * bounds. end_free is checked above. If 'front' is 145783c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 145883c41143SJohn Baldwin * Also check for underflow. 145983c41143SJohn Baldwin */ 146083c41143SJohn Baldwin if (front >= start && front <= end_free) { 146183c41143SJohn Baldwin if (bootverbose) 146283c41143SJohn Baldwin printf("\tfront candidate range: %#lx-%#lx\n", 146383c41143SJohn Baldwin front, end_free); 1464a7b5acacSJohn Baldwin front &= ~wmask; 1465c825d4dcSJohn Baldwin front = w->base - front; 146683c41143SJohn Baldwin } else 146783c41143SJohn Baldwin front = 0; 146883c41143SJohn Baldwin } else 146983c41143SJohn Baldwin front = 0; 1470c825d4dcSJohn Baldwin if (end > w->limit) { 147183c41143SJohn Baldwin if (rman_last_free_region(&w->rman, &start_free, &end_free) != 1472c825d4dcSJohn Baldwin 0 || end_free != w->limit) 1473c825d4dcSJohn Baldwin start_free = w->limit + 1; 147483c41143SJohn Baldwin if (start_free < start) 147583c41143SJohn Baldwin start_free = start; 147683c41143SJohn Baldwin 147783c41143SJohn Baldwin /* Move start_free up until it is properly aligned. */ 147883c41143SJohn Baldwin start_free = roundup2(start_free, align); 1479a49dcb46SJohn Baldwin back = start_free + count - 1; 148083c41143SJohn Baldwin 148183c41143SJohn Baldwin /* 148283c41143SJohn Baldwin * The resource would now be allocated at (start_free, 148383c41143SJohn Baldwin * back). Ensure that fits in the (start, end) 148483c41143SJohn Baldwin * bounds. start_free is checked above. If 'back' is 148583c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 148683c41143SJohn Baldwin * Also check for overflow. 148783c41143SJohn Baldwin */ 148883c41143SJohn Baldwin if (back <= end && start_free <= back) { 148983c41143SJohn Baldwin if (bootverbose) 149083c41143SJohn Baldwin printf("\tback candidate range: %#lx-%#lx\n", 149183c41143SJohn Baldwin start_free, back); 1492a7b5acacSJohn Baldwin back |= wmask; 1493c825d4dcSJohn Baldwin back -= w->limit; 149483c41143SJohn Baldwin } else 149583c41143SJohn Baldwin back = 0; 149683c41143SJohn Baldwin } else 149783c41143SJohn Baldwin back = 0; 149883c41143SJohn Baldwin 149983c41143SJohn Baldwin /* 150083c41143SJohn Baldwin * Try to allocate the smallest needed region first. 150183c41143SJohn Baldwin * If that fails, fall back to the other region. 150283c41143SJohn Baldwin */ 150383c41143SJohn Baldwin error = ENOSPC; 150483c41143SJohn Baldwin while (front != 0 || back != 0) { 150583c41143SJohn Baldwin if (front != 0 && (front <= back || back == 0)) { 1506c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base - front, 1507c825d4dcSJohn Baldwin w->limit); 150883c41143SJohn Baldwin if (error == 0) 150983c41143SJohn Baldwin break; 151083c41143SJohn Baldwin front = 0; 151183c41143SJohn Baldwin } else { 1512c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base, 1513c825d4dcSJohn Baldwin w->limit + back); 151483c41143SJohn Baldwin if (error == 0) 151583c41143SJohn Baldwin break; 151683c41143SJohn Baldwin back = 0; 151783c41143SJohn Baldwin } 151883c41143SJohn Baldwin } 151983c41143SJohn Baldwin 152083c41143SJohn Baldwin if (error) 152183c41143SJohn Baldwin return (error); 152283c41143SJohn Baldwin if (bootverbose) 1523c825d4dcSJohn Baldwin device_printf(sc->dev, "grew %s window to %#jx-%#jx\n", 1524c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 152583c41143SJohn Baldwin 152683c41143SJohn Baldwin updatewin: 1527c825d4dcSJohn Baldwin /* Write the new window. */ 1528a7b5acacSJohn Baldwin KASSERT((w->base & wmask) == 0, ("start address is not aligned")); 1529a7b5acacSJohn Baldwin KASSERT((w->limit & wmask) == wmask, ("end address is not aligned")); 153083c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 153183c41143SJohn Baldwin return (0); 153283c41143SJohn Baldwin } 153383c41143SJohn Baldwin 153483c41143SJohn Baldwin /* 153583c41143SJohn Baldwin * We have to trap resource allocation requests and ensure that the bridge 153683c41143SJohn Baldwin * is set up to, or capable of handling them. 153783c41143SJohn Baldwin */ 153883c41143SJohn Baldwin struct resource * 153983c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 154083c41143SJohn Baldwin u_long start, u_long end, u_long count, u_int flags) 154183c41143SJohn Baldwin { 154283c41143SJohn Baldwin struct pcib_softc *sc; 154383c41143SJohn Baldwin struct resource *r; 154483c41143SJohn Baldwin 154583c41143SJohn Baldwin sc = device_get_softc(dev); 154683c41143SJohn Baldwin 154783c41143SJohn Baldwin /* 154883c41143SJohn Baldwin * VGA resources are decoded iff the VGA enable bit is set in 154983c41143SJohn Baldwin * the bridge control register. VGA resources do not fall into 155083c41143SJohn Baldwin * the resource windows and are passed up to the parent. 155183c41143SJohn Baldwin */ 155283c41143SJohn Baldwin if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) || 155383c41143SJohn Baldwin (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) { 155483c41143SJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) 155583c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, 155683c41143SJohn Baldwin rid, start, end, count, flags)); 155783c41143SJohn Baldwin else 155883c41143SJohn Baldwin return (NULL); 155983c41143SJohn Baldwin } 156083c41143SJohn Baldwin 156183c41143SJohn Baldwin switch (type) { 15624edef187SJohn Baldwin #ifdef PCI_RES_BUS 15634edef187SJohn Baldwin case PCI_RES_BUS: 15644edef187SJohn Baldwin return (pcib_alloc_subbus(&sc->bus, child, rid, start, end, 15654edef187SJohn Baldwin count, flags)); 15664edef187SJohn Baldwin #endif 156783c41143SJohn Baldwin case SYS_RES_IOPORT: 1568c825d4dcSJohn Baldwin if (pcib_is_isa_range(sc, start, end, count)) 1569c825d4dcSJohn Baldwin return (NULL); 157083c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start, 157183c41143SJohn Baldwin end, count, flags); 1572a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 157383c41143SJohn Baldwin break; 157483c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->io, type, start, end, count, 157583c41143SJohn Baldwin flags) == 0) 157683c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, 157783c41143SJohn Baldwin rid, start, end, count, flags); 157883c41143SJohn Baldwin break; 157983c41143SJohn Baldwin case SYS_RES_MEMORY: 158083c41143SJohn Baldwin /* 158183c41143SJohn Baldwin * For prefetchable resources, prefer the prefetchable 158283c41143SJohn Baldwin * memory window, but fall back to the regular memory 158383c41143SJohn Baldwin * window if that fails. Try both windows before 158483c41143SJohn Baldwin * attempting to grow a window in case the firmware 158583c41143SJohn Baldwin * has used a range in the regular memory window to 158683c41143SJohn Baldwin * map a prefetchable BAR. 158783c41143SJohn Baldwin */ 158883c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 158983c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, type, 159083c41143SJohn Baldwin rid, start, end, count, flags); 159183c41143SJohn Baldwin if (r != NULL) 159283c41143SJohn Baldwin break; 159383c41143SJohn Baldwin } 159483c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid, 159583c41143SJohn Baldwin start, end, count, flags); 1596a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 159783c41143SJohn Baldwin break; 159883c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 159983c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->pmem, type, start, end, 160083c41143SJohn Baldwin count, flags) == 0) { 160183c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, 160283c41143SJohn Baldwin type, rid, start, end, count, flags); 160383c41143SJohn Baldwin if (r != NULL) 160483c41143SJohn Baldwin break; 160583c41143SJohn Baldwin } 160683c41143SJohn Baldwin } 160783c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->mem, type, start, end, count, 160883c41143SJohn Baldwin flags & ~RF_PREFETCHABLE) == 0) 160983c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, 161083c41143SJohn Baldwin rid, start, end, count, flags); 161183c41143SJohn Baldwin break; 161283c41143SJohn Baldwin default: 161383c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 161483c41143SJohn Baldwin start, end, count, flags)); 161583c41143SJohn Baldwin } 161683c41143SJohn Baldwin 161783c41143SJohn Baldwin /* 161883c41143SJohn Baldwin * If attempts to suballocate from the window fail but this is a 161983c41143SJohn Baldwin * subtractive bridge, pass the request up the tree. 162083c41143SJohn Baldwin */ 162183c41143SJohn Baldwin if (sc->flags & PCIB_SUBTRACTIVE && r == NULL) 162283c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 162383c41143SJohn Baldwin start, end, count, flags)); 162483c41143SJohn Baldwin return (r); 162583c41143SJohn Baldwin } 162683c41143SJohn Baldwin 162783c41143SJohn Baldwin int 162883c41143SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r, 162983c41143SJohn Baldwin u_long start, u_long end) 163083c41143SJohn Baldwin { 163183c41143SJohn Baldwin struct pcib_softc *sc; 163283c41143SJohn Baldwin 163383c41143SJohn Baldwin sc = device_get_softc(bus); 163483c41143SJohn Baldwin if (pcib_is_resource_managed(sc, type, r)) 163583c41143SJohn Baldwin return (rman_adjust_resource(r, start, end)); 163683c41143SJohn Baldwin return (bus_generic_adjust_resource(bus, child, type, r, start, end)); 163783c41143SJohn Baldwin } 163883c41143SJohn Baldwin 163983c41143SJohn Baldwin int 164083c41143SJohn Baldwin pcib_release_resource(device_t dev, device_t child, int type, int rid, 164183c41143SJohn Baldwin struct resource *r) 164283c41143SJohn Baldwin { 164383c41143SJohn Baldwin struct pcib_softc *sc; 164483c41143SJohn Baldwin int error; 164583c41143SJohn Baldwin 164683c41143SJohn Baldwin sc = device_get_softc(dev); 164783c41143SJohn Baldwin if (pcib_is_resource_managed(sc, type, r)) { 164883c41143SJohn Baldwin if (rman_get_flags(r) & RF_ACTIVE) { 164983c41143SJohn Baldwin error = bus_deactivate_resource(child, type, rid, r); 165083c41143SJohn Baldwin if (error) 165183c41143SJohn Baldwin return (error); 165283c41143SJohn Baldwin } 165383c41143SJohn Baldwin return (rman_release_resource(r)); 165483c41143SJohn Baldwin } 165583c41143SJohn Baldwin return (bus_generic_release_resource(dev, child, type, rid, r)); 165683c41143SJohn Baldwin } 165783c41143SJohn Baldwin #else 1658bb0d0a8eSMike Smith /* 1659bb0d0a8eSMike Smith * We have to trap resource allocation requests and ensure that the bridge 1660bb0d0a8eSMike Smith * is set up to, or capable of handling them. 1661bb0d0a8eSMike Smith */ 16626f0d5884SJohn Baldwin struct resource * 1663bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 1664bb0d0a8eSMike Smith u_long start, u_long end, u_long count, u_int flags) 1665bb0d0a8eSMike Smith { 1666bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 166726043836SJohn Baldwin const char *name, *suffix; 1668a8b354a8SWarner Losh int ok; 1669bb0d0a8eSMike Smith 1670bb0d0a8eSMike Smith /* 1671bb0d0a8eSMike Smith * Fail the allocation for this range if it's not supported. 1672bb0d0a8eSMike Smith */ 167326043836SJohn Baldwin name = device_get_nameunit(child); 167426043836SJohn Baldwin if (name == NULL) { 167526043836SJohn Baldwin name = ""; 167626043836SJohn Baldwin suffix = ""; 167726043836SJohn Baldwin } else 167826043836SJohn Baldwin suffix = " "; 1679bb0d0a8eSMike Smith switch (type) { 1680bb0d0a8eSMike Smith case SYS_RES_IOPORT: 1681a8b354a8SWarner Losh ok = 0; 1682e4b59fc5SWarner Losh if (!pcib_is_io_open(sc)) 1683e4b59fc5SWarner Losh break; 1684a8b354a8SWarner Losh ok = (start >= sc->iobase && end <= sc->iolimit); 1685d98d9b12SMarcel Moolenaar 1686d98d9b12SMarcel Moolenaar /* 1687d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA I/O addresses when the 1688d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 1689d98d9b12SMarcel Moolenaar */ 1690d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_ioport_range(start, end)) 1691d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 1692d98d9b12SMarcel Moolenaar 1693e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 1694a8b354a8SWarner Losh if (!ok) { 169512b8c86eSWarner Losh if (start < sc->iobase) 169612b8c86eSWarner Losh start = sc->iobase; 169712b8c86eSWarner Losh if (end > sc->iolimit) 169812b8c86eSWarner Losh end = sc->iolimit; 16992daa7a07SWarner Losh if (start < end) 17002daa7a07SWarner Losh ok = 1; 1701a8b354a8SWarner Losh } 17021c54ff33SMatthew N. Dodd } else { 1703e4b59fc5SWarner Losh ok = 1; 17049dffe835SWarner Losh #if 0 1705795dceffSWarner Losh /* 1706795dceffSWarner Losh * If we overlap with the subtractive range, then 1707795dceffSWarner Losh * pick the upper range to use. 1708795dceffSWarner Losh */ 1709795dceffSWarner Losh if (start < sc->iolimit && end > sc->iobase) 1710795dceffSWarner Losh start = sc->iolimit + 1; 17119dffe835SWarner Losh #endif 171212b8c86eSWarner Losh } 1713a8b354a8SWarner Losh if (end < start) { 17142daa7a07SWarner Losh device_printf(dev, "ioport: end (%lx) < start (%lx)\n", 17152daa7a07SWarner Losh end, start); 1716a8b354a8SWarner Losh start = 0; 1717a8b354a8SWarner Losh end = 0; 1718a8b354a8SWarner Losh ok = 0; 1719a8b354a8SWarner Losh } 1720a8b354a8SWarner Losh if (!ok) { 172126043836SJohn Baldwin device_printf(dev, "%s%srequested unsupported I/O " 1722a8b354a8SWarner Losh "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n", 172326043836SJohn Baldwin name, suffix, start, end, sc->iobase, sc->iolimit); 1724bb0d0a8eSMike Smith return (NULL); 1725bb0d0a8eSMike Smith } 17264fa59183SMike Smith if (bootverbose) 17272daa7a07SWarner Losh device_printf(dev, 172826043836SJohn Baldwin "%s%srequested I/O range 0x%lx-0x%lx: in range\n", 172926043836SJohn Baldwin name, suffix, start, end); 1730bb0d0a8eSMike Smith break; 1731bb0d0a8eSMike Smith 1732bb0d0a8eSMike Smith case SYS_RES_MEMORY: 1733a8b354a8SWarner Losh ok = 0; 1734a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 1735a8b354a8SWarner Losh ok = ok || (start >= sc->membase && end <= sc->memlimit); 1736a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) 1737a8b354a8SWarner Losh ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit); 1738d98d9b12SMarcel Moolenaar 1739d98d9b12SMarcel Moolenaar /* 1740d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA memory addresses when the 1741d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 1742d98d9b12SMarcel Moolenaar */ 1743d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_memory_range(start, end)) 1744d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 1745d98d9b12SMarcel Moolenaar 1746e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 1747a8b354a8SWarner Losh if (!ok) { 1748a8b354a8SWarner Losh ok = 1; 1749a8b354a8SWarner Losh if (flags & RF_PREFETCHABLE) { 1750a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 1751a8b354a8SWarner Losh if (start < sc->pmembase) 1752a8b354a8SWarner Losh start = sc->pmembase; 1753a8b354a8SWarner Losh if (end > sc->pmemlimit) 1754a8b354a8SWarner Losh end = sc->pmemlimit; 1755a8b354a8SWarner Losh } else { 1756a8b354a8SWarner Losh ok = 0; 1757a8b354a8SWarner Losh } 1758a8b354a8SWarner Losh } else { /* non-prefetchable */ 1759a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 1760a8b354a8SWarner Losh if (start < sc->membase) 176112b8c86eSWarner Losh start = sc->membase; 176212b8c86eSWarner Losh if (end > sc->memlimit) 176312b8c86eSWarner Losh end = sc->memlimit; 17641c54ff33SMatthew N. Dodd } else { 1765a8b354a8SWarner Losh ok = 0; 1766a8b354a8SWarner Losh } 1767a8b354a8SWarner Losh } 1768a8b354a8SWarner Losh } 1769a8b354a8SWarner Losh } else if (!ok) { 1770e4b59fc5SWarner Losh ok = 1; /* subtractive bridge: always ok */ 17719dffe835SWarner Losh #if 0 1772a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 1773795dceffSWarner Losh if (start < sc->memlimit && end > sc->membase) 1774795dceffSWarner Losh start = sc->memlimit + 1; 1775a8b354a8SWarner Losh } 1776a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 1777795dceffSWarner Losh if (start < sc->pmemlimit && end > sc->pmembase) 1778795dceffSWarner Losh start = sc->pmemlimit + 1; 17791c54ff33SMatthew N. Dodd } 17809dffe835SWarner Losh #endif 178112b8c86eSWarner Losh } 1782a8b354a8SWarner Losh if (end < start) { 17832daa7a07SWarner Losh device_printf(dev, "memory: end (%lx) < start (%lx)\n", 17842daa7a07SWarner Losh end, start); 1785a8b354a8SWarner Losh start = 0; 1786a8b354a8SWarner Losh end = 0; 1787a8b354a8SWarner Losh ok = 0; 1788a8b354a8SWarner Losh } 1789a8b354a8SWarner Losh if (!ok && bootverbose) 179034428485SWarner Losh device_printf(dev, 179126043836SJohn Baldwin "%s%srequested unsupported memory range %#lx-%#lx " 1792b0a2d4b8SWarner Losh "(decoding %#jx-%#jx, %#jx-%#jx)\n", 179326043836SJohn Baldwin name, suffix, start, end, 1794b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit, 1795b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 1796a8b354a8SWarner Losh if (!ok) 1797bb0d0a8eSMike Smith return (NULL); 17984fa59183SMike Smith if (bootverbose) 179926043836SJohn Baldwin device_printf(dev,"%s%srequested memory range " 18002daa7a07SWarner Losh "0x%lx-0x%lx: good\n", 180126043836SJohn Baldwin name, suffix, start, end); 18024fa59183SMike Smith break; 18034fa59183SMike Smith 1804bb0d0a8eSMike Smith default: 18054fa59183SMike Smith break; 1806bb0d0a8eSMike Smith } 1807bb0d0a8eSMike Smith /* 1808bb0d0a8eSMike Smith * Bridge is OK decoding this resource, so pass it up. 1809bb0d0a8eSMike Smith */ 18102daa7a07SWarner Losh return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 18112daa7a07SWarner Losh count, flags)); 1812bb0d0a8eSMike Smith } 181383c41143SJohn Baldwin #endif 1814bb0d0a8eSMike Smith 1815bb0d0a8eSMike Smith /* 1816*55d3ea17SRyan Stone * If ARI is enabled on this downstream port, translate the function number 1817*55d3ea17SRyan Stone * to the non-ARI slot/function. The downstream port will convert it back in 1818*55d3ea17SRyan Stone * hardware. If ARI is not enabled slot and func are not modified. 1819*55d3ea17SRyan Stone */ 1820*55d3ea17SRyan Stone static __inline void 1821*55d3ea17SRyan Stone pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func) 1822*55d3ea17SRyan Stone { 1823*55d3ea17SRyan Stone struct pcib_softc *sc; 1824*55d3ea17SRyan Stone int ari_func; 1825*55d3ea17SRyan Stone 1826*55d3ea17SRyan Stone sc = device_get_softc(pcib); 1827*55d3ea17SRyan Stone ari_func = *func; 1828*55d3ea17SRyan Stone 1829*55d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 1830*55d3ea17SRyan Stone KASSERT(*slot == 0, 1831*55d3ea17SRyan Stone ("Non-zero slot number with ARI enabled!")); 1832*55d3ea17SRyan Stone *slot = PCIE_ARI_SLOT(ari_func); 1833*55d3ea17SRyan Stone *func = PCIE_ARI_FUNC(ari_func); 1834*55d3ea17SRyan Stone } 1835*55d3ea17SRyan Stone } 1836*55d3ea17SRyan Stone 1837*55d3ea17SRyan Stone 1838*55d3ea17SRyan Stone static void 1839*55d3ea17SRyan Stone pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos) 1840*55d3ea17SRyan Stone { 1841*55d3ea17SRyan Stone uint32_t ctl2; 1842*55d3ea17SRyan Stone 1843*55d3ea17SRyan Stone ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4); 1844*55d3ea17SRyan Stone ctl2 |= PCIEM_CTL2_ARI; 1845*55d3ea17SRyan Stone pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4); 1846*55d3ea17SRyan Stone 1847*55d3ea17SRyan Stone sc->flags |= PCIB_ENABLE_ARI; 1848*55d3ea17SRyan Stone } 1849*55d3ea17SRyan Stone 1850*55d3ea17SRyan Stone /* 1851bb0d0a8eSMike Smith * PCIB interface. 1852bb0d0a8eSMike Smith */ 18536f0d5884SJohn Baldwin int 1854bb0d0a8eSMike Smith pcib_maxslots(device_t dev) 1855bb0d0a8eSMike Smith { 18564fa59183SMike Smith return (PCI_SLOTMAX); 1857bb0d0a8eSMike Smith } 1858bb0d0a8eSMike Smith 1859*55d3ea17SRyan Stone static int 1860*55d3ea17SRyan Stone pcib_ari_maxslots(device_t dev) 1861*55d3ea17SRyan Stone { 1862*55d3ea17SRyan Stone struct pcib_softc *sc; 1863*55d3ea17SRyan Stone 1864*55d3ea17SRyan Stone sc = device_get_softc(dev); 1865*55d3ea17SRyan Stone 1866*55d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 1867*55d3ea17SRyan Stone return (PCIE_ARI_SLOTMAX); 1868*55d3ea17SRyan Stone else 1869*55d3ea17SRyan Stone return (PCI_SLOTMAX); 1870*55d3ea17SRyan Stone } 1871*55d3ea17SRyan Stone 1872*55d3ea17SRyan Stone static int 1873*55d3ea17SRyan Stone pcib_ari_maxfuncs(device_t dev) 1874*55d3ea17SRyan Stone { 1875*55d3ea17SRyan Stone struct pcib_softc *sc; 1876*55d3ea17SRyan Stone 1877*55d3ea17SRyan Stone sc = device_get_softc(dev); 1878*55d3ea17SRyan Stone 1879*55d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 1880*55d3ea17SRyan Stone return (PCIE_ARI_FUNCMAX); 1881*55d3ea17SRyan Stone else 1882*55d3ea17SRyan Stone return (PCI_FUNCMAX); 1883*55d3ea17SRyan Stone } 1884*55d3ea17SRyan Stone 1885bb0d0a8eSMike Smith /* 1886bb0d0a8eSMike Smith * Since we are a child of a PCI bus, its parent must support the pcib interface. 1887bb0d0a8eSMike Smith */ 1888*55d3ea17SRyan Stone static uint32_t 1889795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width) 1890bb0d0a8eSMike Smith { 1891*55d3ea17SRyan Stone 1892*55d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 1893*55d3ea17SRyan Stone return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, 1894*55d3ea17SRyan Stone f, reg, width)); 1895bb0d0a8eSMike Smith } 1896bb0d0a8eSMike Smith 1897*55d3ea17SRyan Stone static void 1898795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width) 1899bb0d0a8eSMike Smith { 1900*55d3ea17SRyan Stone 1901*55d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 1902*55d3ea17SRyan Stone PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, 1903*55d3ea17SRyan Stone reg, val, width); 1904bb0d0a8eSMike Smith } 1905bb0d0a8eSMike Smith 1906bb0d0a8eSMike Smith /* 1907bb0d0a8eSMike Smith * Route an interrupt across a PCI bridge. 1908bb0d0a8eSMike Smith */ 19092c2d1d07SBenno Rice int 1910bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin) 1911bb0d0a8eSMike Smith { 1912bb0d0a8eSMike Smith device_t bus; 1913bb0d0a8eSMike Smith int parent_intpin; 1914bb0d0a8eSMike Smith int intnum; 1915bb0d0a8eSMike Smith 1916bb0d0a8eSMike Smith /* 1917bb0d0a8eSMike Smith * 1918bb0d0a8eSMike Smith * The PCI standard defines a swizzle of the child-side device/intpin to 1919bb0d0a8eSMike Smith * the parent-side intpin as follows. 1920bb0d0a8eSMike Smith * 1921bb0d0a8eSMike Smith * device = device on child bus 1922bb0d0a8eSMike Smith * child_intpin = intpin on child bus slot (0-3) 1923bb0d0a8eSMike Smith * parent_intpin = intpin on parent bus slot (0-3) 1924bb0d0a8eSMike Smith * 1925bb0d0a8eSMike Smith * parent_intpin = (device + child_intpin) % 4 1926bb0d0a8eSMike Smith */ 1927cdc95e1bSBernd Walter parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4; 1928bb0d0a8eSMike Smith 1929bb0d0a8eSMike Smith /* 1930bb0d0a8eSMike Smith * Our parent is a PCI bus. Its parent must export the pcib interface 1931bb0d0a8eSMike Smith * which includes the ability to route interrupts. 1932bb0d0a8eSMike Smith */ 1933bb0d0a8eSMike Smith bus = device_get_parent(pcib); 1934bb0d0a8eSMike Smith intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1); 193539981fedSJohn Baldwin if (PCI_INTERRUPT_VALID(intnum) && bootverbose) { 1936c6a121abSJohn Baldwin device_printf(pcib, "slot %d INT%c is routed to irq %d\n", 1937c6a121abSJohn Baldwin pci_get_slot(dev), 'A' + pin - 1, intnum); 19388046c4b9SMike Smith } 1939bb0d0a8eSMike Smith return(intnum); 1940bb0d0a8eSMike Smith } 1941b173edafSJohn Baldwin 1942e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */ 19439bf4c9c1SJohn Baldwin int 19449bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs) 19459bf4c9c1SJohn Baldwin { 1946bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 19479bf4c9c1SJohn Baldwin device_t bus; 19489bf4c9c1SJohn Baldwin 194922bf1c7fSJohn Baldwin if (sc->flags & PCIB_DISABLE_MSI) 195022bf1c7fSJohn Baldwin return (ENXIO); 19519bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 19529bf4c9c1SJohn Baldwin return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 19539bf4c9c1SJohn Baldwin irqs)); 19549bf4c9c1SJohn Baldwin } 19559bf4c9c1SJohn Baldwin 1956e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */ 19579bf4c9c1SJohn Baldwin int 19589bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs) 19599bf4c9c1SJohn Baldwin { 19609bf4c9c1SJohn Baldwin device_t bus; 19619bf4c9c1SJohn Baldwin 19629bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 19639bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs)); 19649bf4c9c1SJohn Baldwin } 19659bf4c9c1SJohn Baldwin 19669bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */ 19679bf4c9c1SJohn Baldwin int 1968e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq) 19699bf4c9c1SJohn Baldwin { 1970bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 19719bf4c9c1SJohn Baldwin device_t bus; 19729bf4c9c1SJohn Baldwin 197368e9cbd3SMarius Strobl if (sc->flags & PCIB_DISABLE_MSIX) 197422bf1c7fSJohn Baldwin return (ENXIO); 19759bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 1976e706f7f0SJohn Baldwin return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq)); 19775fe82bcaSJohn Baldwin } 19785fe82bcaSJohn Baldwin 19799bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */ 19809bf4c9c1SJohn Baldwin int 19819bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq) 19829bf4c9c1SJohn Baldwin { 19839bf4c9c1SJohn Baldwin device_t bus; 19849bf4c9c1SJohn Baldwin 19859bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 19869bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq)); 19879bf4c9c1SJohn Baldwin } 19889bf4c9c1SJohn Baldwin 1989e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */ 1990e706f7f0SJohn Baldwin int 1991e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, 1992e706f7f0SJohn Baldwin uint32_t *data) 1993e706f7f0SJohn Baldwin { 1994e706f7f0SJohn Baldwin device_t bus; 19954522ac77SLuoqi Chen int error; 1996e706f7f0SJohn Baldwin 1997e706f7f0SJohn Baldwin bus = device_get_parent(pcib); 19984522ac77SLuoqi Chen error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data); 19994522ac77SLuoqi Chen if (error) 20004522ac77SLuoqi Chen return (error); 20014522ac77SLuoqi Chen 20024522ac77SLuoqi Chen pci_ht_map_msi(pcib, *addr); 20034522ac77SLuoqi Chen return (0); 2004e706f7f0SJohn Baldwin } 2005e706f7f0SJohn Baldwin 200662508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */ 200762508c53SJohn Baldwin int 200862508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate) 200962508c53SJohn Baldwin { 201062508c53SJohn Baldwin device_t bus; 201162508c53SJohn Baldwin 201262508c53SJohn Baldwin bus = device_get_parent(pcib); 201362508c53SJohn Baldwin return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate)); 201462508c53SJohn Baldwin } 20155605a99eSRyan Stone 2016*55d3ea17SRyan Stone static uint16_t 2017*55d3ea17SRyan Stone pcib_ari_get_rid(device_t pcib, device_t dev) 2018*55d3ea17SRyan Stone { 2019*55d3ea17SRyan Stone struct pcib_softc *sc; 2020*55d3ea17SRyan Stone uint8_t bus, slot, func; 2021*55d3ea17SRyan Stone 2022*55d3ea17SRyan Stone sc = device_get_softc(pcib); 2023*55d3ea17SRyan Stone 2024*55d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 2025*55d3ea17SRyan Stone bus = pci_get_bus(dev); 2026*55d3ea17SRyan Stone func = pci_get_function(dev); 2027*55d3ea17SRyan Stone 2028*55d3ea17SRyan Stone return (PCI_ARI_RID(bus, func)); 2029*55d3ea17SRyan Stone } else { 2030*55d3ea17SRyan Stone bus = pci_get_bus(dev); 2031*55d3ea17SRyan Stone slot = pci_get_slot(dev); 2032*55d3ea17SRyan Stone func = pci_get_function(dev); 2033*55d3ea17SRyan Stone 2034*55d3ea17SRyan Stone return (PCI_RID(bus, slot, func)); 2035*55d3ea17SRyan Stone } 2036*55d3ea17SRyan Stone } 2037*55d3ea17SRyan Stone 2038*55d3ea17SRyan Stone /* 2039*55d3ea17SRyan Stone * Check that the downstream port (pcib) and the endpoint device (dev) both 2040*55d3ea17SRyan Stone * support ARI. If so, enable it and return 0, otherwise return an error. 2041*55d3ea17SRyan Stone */ 2042*55d3ea17SRyan Stone static int 2043*55d3ea17SRyan Stone pcib_try_enable_ari(device_t pcib, device_t dev) 2044*55d3ea17SRyan Stone { 2045*55d3ea17SRyan Stone struct pcib_softc *sc; 2046*55d3ea17SRyan Stone int error; 2047*55d3ea17SRyan Stone uint32_t cap2; 2048*55d3ea17SRyan Stone int ari_cap_off; 2049*55d3ea17SRyan Stone uint32_t ari_ver; 2050*55d3ea17SRyan Stone uint32_t pcie_pos; 2051*55d3ea17SRyan Stone 2052*55d3ea17SRyan Stone sc = device_get_softc(pcib); 2053*55d3ea17SRyan Stone 2054*55d3ea17SRyan Stone /* 2055*55d3ea17SRyan Stone * ARI is controlled in a register in the PCIe capability structure. 2056*55d3ea17SRyan Stone * If the downstream port does not have the PCIe capability structure 2057*55d3ea17SRyan Stone * then it does not support ARI. 2058*55d3ea17SRyan Stone */ 2059*55d3ea17SRyan Stone error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos); 2060*55d3ea17SRyan Stone if (error != 0) 2061*55d3ea17SRyan Stone return (ENODEV); 2062*55d3ea17SRyan Stone 2063*55d3ea17SRyan Stone /* Check that the PCIe port advertises ARI support. */ 2064*55d3ea17SRyan Stone cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4); 2065*55d3ea17SRyan Stone if (!(cap2 & PCIEM_CAP2_ARI)) 2066*55d3ea17SRyan Stone return (ENODEV); 2067*55d3ea17SRyan Stone 2068*55d3ea17SRyan Stone /* 2069*55d3ea17SRyan Stone * Check that the endpoint device advertises ARI support via the ARI 2070*55d3ea17SRyan Stone * extended capability structure. 2071*55d3ea17SRyan Stone */ 2072*55d3ea17SRyan Stone error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off); 2073*55d3ea17SRyan Stone if (error != 0) 2074*55d3ea17SRyan Stone return (ENODEV); 2075*55d3ea17SRyan Stone 2076*55d3ea17SRyan Stone /* 2077*55d3ea17SRyan Stone * Finally, check that the endpoint device supports the same version 2078*55d3ea17SRyan Stone * of ARI that we do. 2079*55d3ea17SRyan Stone */ 2080*55d3ea17SRyan Stone ari_ver = pci_read_config(dev, ari_cap_off, 4); 2081*55d3ea17SRyan Stone if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) { 2082*55d3ea17SRyan Stone if (bootverbose) 2083*55d3ea17SRyan Stone device_printf(pcib, 2084*55d3ea17SRyan Stone "Unsupported version of ARI (%d) detected\n", 2085*55d3ea17SRyan Stone PCI_EXTCAP_VER(ari_ver)); 2086*55d3ea17SRyan Stone 2087*55d3ea17SRyan Stone return (ENXIO); 2088*55d3ea17SRyan Stone } 2089*55d3ea17SRyan Stone 2090*55d3ea17SRyan Stone pcib_enable_ari(sc, pcie_pos); 2091*55d3ea17SRyan Stone 2092*55d3ea17SRyan Stone return (0); 2093*55d3ea17SRyan Stone } 2094*55d3ea17SRyan Stone 2095