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); 5955d3ea17SRyan Stone static uint16_t pcib_ari_get_rid(device_t pcib, device_t dev); 6055d3ea17SRyan Stone static uint32_t pcib_read_config(device_t dev, u_int b, u_int s, 6155d3ea17SRyan Stone u_int f, u_int reg, int width); 6255d3ea17SRyan Stone static void pcib_write_config(device_t dev, u_int b, u_int s, 6355d3ea17SRyan Stone u_int f, u_int reg, uint32_t val, int width); 6455d3ea17SRyan Stone static int pcib_ari_maxslots(device_t dev); 6555d3ea17SRyan Stone static int pcib_ari_maxfuncs(device_t dev); 6655d3ea17SRyan Stone static int pcib_try_enable_ari(device_t pcib, device_t dev); 672397d2d8SRyan Stone static int pcib_ari_enabled(device_t pcib); 682397d2d8SRyan Stone static void pcib_ari_decode_rid(device_t pcib, uint16_t rid, 692397d2d8SRyan Stone int *bus, int *slot, int *func); 70bb0d0a8eSMike Smith 71bb0d0a8eSMike Smith static device_method_t pcib_methods[] = { 72bb0d0a8eSMike Smith /* Device interface */ 73bb0d0a8eSMike Smith DEVMETHOD(device_probe, pcib_probe), 74bb0d0a8eSMike Smith DEVMETHOD(device_attach, pcib_attach), 754e30440dSWarner Losh DEVMETHOD(device_detach, bus_generic_detach), 76bb0d0a8eSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 77e36af292SJung-uk Kim DEVMETHOD(device_suspend, pcib_suspend), 78e36af292SJung-uk Kim DEVMETHOD(device_resume, pcib_resume), 79bb0d0a8eSMike Smith 80bb0d0a8eSMike Smith /* Bus interface */ 81bb0d0a8eSMike Smith DEVMETHOD(bus_read_ivar, pcib_read_ivar), 82bb0d0a8eSMike Smith DEVMETHOD(bus_write_ivar, pcib_write_ivar), 83bb0d0a8eSMike Smith DEVMETHOD(bus_alloc_resource, pcib_alloc_resource), 8483c41143SJohn Baldwin #ifdef NEW_PCIB 8583c41143SJohn Baldwin DEVMETHOD(bus_adjust_resource, pcib_adjust_resource), 8683c41143SJohn Baldwin DEVMETHOD(bus_release_resource, pcib_release_resource), 8783c41143SJohn Baldwin #else 88d2c9344fSJohn Baldwin DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 89bb0d0a8eSMike Smith DEVMETHOD(bus_release_resource, bus_generic_release_resource), 9083c41143SJohn Baldwin #endif 91bb0d0a8eSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 92bb0d0a8eSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 93bb0d0a8eSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 94bb0d0a8eSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 95bb0d0a8eSMike Smith 96bb0d0a8eSMike Smith /* pcib interface */ 9755d3ea17SRyan Stone DEVMETHOD(pcib_maxslots, pcib_ari_maxslots), 9855d3ea17SRyan Stone DEVMETHOD(pcib_maxfuncs, pcib_ari_maxfuncs), 99bb0d0a8eSMike Smith DEVMETHOD(pcib_read_config, pcib_read_config), 100bb0d0a8eSMike Smith DEVMETHOD(pcib_write_config, pcib_write_config), 101bb0d0a8eSMike Smith DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt), 1029bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi), 1039bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msi, pcib_release_msi), 1049bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix), 1059bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msix, pcib_release_msix), 106e706f7f0SJohn Baldwin DEVMETHOD(pcib_map_msi, pcib_map_msi), 10762508c53SJohn Baldwin DEVMETHOD(pcib_power_for_sleep, pcib_power_for_sleep), 10855d3ea17SRyan Stone DEVMETHOD(pcib_get_rid, pcib_ari_get_rid), 10955d3ea17SRyan Stone DEVMETHOD(pcib_try_enable_ari, pcib_try_enable_ari), 1102397d2d8SRyan Stone DEVMETHOD(pcib_ari_enabled, pcib_ari_enabled), 1112397d2d8SRyan Stone DEVMETHOD(pcib_decode_rid, pcib_ari_decode_rid), 112bb0d0a8eSMike Smith 1134b7ec270SMarius Strobl DEVMETHOD_END 114bb0d0a8eSMike Smith }; 115bb0d0a8eSMike Smith 11604dda605SJohn Baldwin static devclass_t pcib_devclass; 117bb0d0a8eSMike Smith 11804dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc)); 11968e9cbd3SMarius Strobl DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, NULL, NULL); 120bb0d0a8eSMike Smith 12183c41143SJohn Baldwin #ifdef NEW_PCIB 1220070c94bSJohn Baldwin SYSCTL_DECL(_hw_pci); 1230070c94bSJohn Baldwin 1240070c94bSJohn Baldwin static int pci_clear_pcib; 1250070c94bSJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0, 1260070c94bSJohn Baldwin "Clear firmware-assigned resources for PCI-PCI bridge I/O windows."); 12783c41143SJohn Baldwin 12883c41143SJohn Baldwin /* 12983c41143SJohn Baldwin * Is a resource from a child device sub-allocated from one of our 13083c41143SJohn Baldwin * resource managers? 13183c41143SJohn Baldwin */ 13283c41143SJohn Baldwin static int 13383c41143SJohn Baldwin pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r) 13483c41143SJohn Baldwin { 13583c41143SJohn Baldwin 13683c41143SJohn Baldwin switch (type) { 1374edef187SJohn Baldwin #ifdef PCI_RES_BUS 1384edef187SJohn Baldwin case PCI_RES_BUS: 1394edef187SJohn Baldwin return (rman_is_region_manager(r, &sc->bus.rman)); 1404edef187SJohn Baldwin #endif 14183c41143SJohn Baldwin case SYS_RES_IOPORT: 14283c41143SJohn Baldwin return (rman_is_region_manager(r, &sc->io.rman)); 14383c41143SJohn Baldwin case SYS_RES_MEMORY: 14483c41143SJohn Baldwin /* Prefetchable resources may live in either memory rman. */ 14583c41143SJohn Baldwin if (rman_get_flags(r) & RF_PREFETCHABLE && 14683c41143SJohn Baldwin rman_is_region_manager(r, &sc->pmem.rman)) 14783c41143SJohn Baldwin return (1); 14883c41143SJohn Baldwin return (rman_is_region_manager(r, &sc->mem.rman)); 14983c41143SJohn Baldwin } 15083c41143SJohn Baldwin return (0); 15183c41143SJohn Baldwin } 15283c41143SJohn Baldwin 15383c41143SJohn Baldwin static int 15483c41143SJohn Baldwin pcib_is_window_open(struct pcib_window *pw) 15583c41143SJohn Baldwin { 15683c41143SJohn Baldwin 15783c41143SJohn Baldwin return (pw->valid && pw->base < pw->limit); 15883c41143SJohn Baldwin } 15983c41143SJohn Baldwin 16083c41143SJohn Baldwin /* 16183c41143SJohn Baldwin * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and 16283c41143SJohn Baldwin * handle for the resource, we could pass RF_ACTIVE up to the PCI bus 16383c41143SJohn Baldwin * when allocating the resource windows and rely on the PCI bus driver 16483c41143SJohn Baldwin * to do this for us. 16583c41143SJohn Baldwin */ 16683c41143SJohn Baldwin static void 16783c41143SJohn Baldwin pcib_activate_window(struct pcib_softc *sc, int type) 16883c41143SJohn Baldwin { 16983c41143SJohn Baldwin 17083c41143SJohn Baldwin PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type); 17183c41143SJohn Baldwin } 17283c41143SJohn Baldwin 17383c41143SJohn Baldwin static void 17483c41143SJohn Baldwin pcib_write_windows(struct pcib_softc *sc, int mask) 17583c41143SJohn Baldwin { 17683c41143SJohn Baldwin device_t dev; 17783c41143SJohn Baldwin uint32_t val; 17883c41143SJohn Baldwin 17983c41143SJohn Baldwin dev = sc->dev; 18083c41143SJohn Baldwin if (sc->io.valid && mask & WIN_IO) { 18183c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 18283c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 18383c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEH_1, 18483c41143SJohn Baldwin sc->io.base >> 16, 2); 18583c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITH_1, 18683c41143SJohn Baldwin sc->io.limit >> 16, 2); 18783c41143SJohn Baldwin } 18883c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1); 18983c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1); 19083c41143SJohn Baldwin } 19183c41143SJohn Baldwin 19283c41143SJohn Baldwin if (mask & WIN_MEM) { 19383c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2); 19483c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2); 19583c41143SJohn Baldwin } 19683c41143SJohn Baldwin 19783c41143SJohn Baldwin if (sc->pmem.valid && mask & WIN_PMEM) { 19883c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 19983c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 20083c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEH_1, 20183c41143SJohn Baldwin sc->pmem.base >> 32, 4); 20283c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITH_1, 20383c41143SJohn Baldwin sc->pmem.limit >> 32, 4); 20483c41143SJohn Baldwin } 20583c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2); 20683c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2); 20783c41143SJohn Baldwin } 20883c41143SJohn Baldwin } 20983c41143SJohn Baldwin 210c825d4dcSJohn Baldwin /* 211c825d4dcSJohn Baldwin * This is used to reject I/O port allocations that conflict with an 212c825d4dcSJohn Baldwin * ISA alias range. 213c825d4dcSJohn Baldwin */ 214c825d4dcSJohn Baldwin static int 215c825d4dcSJohn Baldwin pcib_is_isa_range(struct pcib_softc *sc, u_long start, u_long end, u_long count) 216c825d4dcSJohn Baldwin { 217c825d4dcSJohn Baldwin u_long next_alias; 218c825d4dcSJohn Baldwin 219c825d4dcSJohn Baldwin if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE)) 220c825d4dcSJohn Baldwin return (0); 221c825d4dcSJohn Baldwin 222c825d4dcSJohn Baldwin /* Only check fixed ranges for overlap. */ 223c825d4dcSJohn Baldwin if (start + count - 1 != end) 224c825d4dcSJohn Baldwin return (0); 225c825d4dcSJohn Baldwin 226c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */ 227c825d4dcSJohn Baldwin if (start >= 65536) 228c825d4dcSJohn Baldwin return (0); 229c825d4dcSJohn Baldwin 230c825d4dcSJohn Baldwin /* Check for overlap with 0x000 - 0x0ff as a special case. */ 231c825d4dcSJohn Baldwin if (start < 0x100) 232c825d4dcSJohn Baldwin goto alias; 233c825d4dcSJohn Baldwin 234c825d4dcSJohn Baldwin /* 235c825d4dcSJohn Baldwin * If the start address is an alias, the range is an alias. 236c825d4dcSJohn Baldwin * Otherwise, compute the start of the next alias range and 237c825d4dcSJohn Baldwin * check if it is before the end of the candidate range. 238c825d4dcSJohn Baldwin */ 239c825d4dcSJohn Baldwin if ((start & 0x300) != 0) 240c825d4dcSJohn Baldwin goto alias; 241c825d4dcSJohn Baldwin next_alias = (start & ~0x3fful) | 0x100; 242c825d4dcSJohn Baldwin if (next_alias <= end) 243c825d4dcSJohn Baldwin goto alias; 244c825d4dcSJohn Baldwin return (0); 245c825d4dcSJohn Baldwin 246c825d4dcSJohn Baldwin alias: 247c825d4dcSJohn Baldwin if (bootverbose) 248c825d4dcSJohn Baldwin device_printf(sc->dev, 249c825d4dcSJohn Baldwin "I/O range %#lx-%#lx overlaps with an ISA alias\n", start, 250c825d4dcSJohn Baldwin end); 251c825d4dcSJohn Baldwin return (1); 252c825d4dcSJohn Baldwin } 253c825d4dcSJohn Baldwin 254c825d4dcSJohn Baldwin static void 255c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res, 256c825d4dcSJohn Baldwin int count) 257c825d4dcSJohn Baldwin { 258c825d4dcSJohn Baldwin struct resource **newarray; 259c825d4dcSJohn Baldwin int error, i; 260c825d4dcSJohn Baldwin 261c825d4dcSJohn Baldwin newarray = malloc(sizeof(struct resource *) * (w->count + count), 262c825d4dcSJohn Baldwin M_DEVBUF, M_WAITOK); 263c825d4dcSJohn Baldwin if (w->res != NULL) 264c825d4dcSJohn Baldwin bcopy(w->res, newarray, sizeof(struct resource *) * w->count); 265c825d4dcSJohn Baldwin bcopy(res, newarray + w->count, sizeof(struct resource *) * count); 266c825d4dcSJohn Baldwin free(w->res, M_DEVBUF); 267c825d4dcSJohn Baldwin w->res = newarray; 268c825d4dcSJohn Baldwin w->count += count; 269c825d4dcSJohn Baldwin 270c825d4dcSJohn Baldwin for (i = 0; i < count; i++) { 271c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, rman_get_start(res[i]), 272c825d4dcSJohn Baldwin rman_get_end(res[i])); 273c825d4dcSJohn Baldwin if (error) 274c825d4dcSJohn Baldwin panic("Failed to add resource to rman"); 275c825d4dcSJohn Baldwin } 276c825d4dcSJohn Baldwin } 277c825d4dcSJohn Baldwin 278c825d4dcSJohn Baldwin typedef void (nonisa_callback)(u_long start, u_long end, void *arg); 279c825d4dcSJohn Baldwin 280c825d4dcSJohn Baldwin static void 281c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(u_long start, u_long end, nonisa_callback *cb, 282c825d4dcSJohn Baldwin void *arg) 283c825d4dcSJohn Baldwin { 284c825d4dcSJohn Baldwin u_long next_end; 285c825d4dcSJohn Baldwin 286c825d4dcSJohn Baldwin /* 287c825d4dcSJohn Baldwin * If start is within an ISA alias range, move up to the start 288c825d4dcSJohn Baldwin * of the next non-alias range. As a special case, addresses 289c825d4dcSJohn Baldwin * in the range 0x000 - 0x0ff should also be skipped since 290c825d4dcSJohn Baldwin * those are used for various system I/O devices in ISA 291c825d4dcSJohn Baldwin * systems. 292c825d4dcSJohn Baldwin */ 293c825d4dcSJohn Baldwin if (start <= 65535) { 294c825d4dcSJohn Baldwin if (start < 0x100 || (start & 0x300) != 0) { 295c825d4dcSJohn Baldwin start &= ~0x3ff; 296c825d4dcSJohn Baldwin start += 0x400; 297c825d4dcSJohn Baldwin } 298c825d4dcSJohn Baldwin } 299c825d4dcSJohn Baldwin 300c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */ 301c825d4dcSJohn Baldwin while (start <= MIN(end, 65535)) { 302c825d4dcSJohn Baldwin next_end = MIN(start | 0xff, end); 303c825d4dcSJohn Baldwin cb(start, next_end, arg); 304c825d4dcSJohn Baldwin start += 0x400; 305c825d4dcSJohn Baldwin } 306c825d4dcSJohn Baldwin 307c825d4dcSJohn Baldwin if (start <= end) 308c825d4dcSJohn Baldwin cb(start, end, arg); 309c825d4dcSJohn Baldwin } 310c825d4dcSJohn Baldwin 311c825d4dcSJohn Baldwin static void 312c825d4dcSJohn Baldwin count_ranges(u_long start, u_long end, void *arg) 313c825d4dcSJohn Baldwin { 314c825d4dcSJohn Baldwin int *countp; 315c825d4dcSJohn Baldwin 316c825d4dcSJohn Baldwin countp = arg; 317c825d4dcSJohn Baldwin (*countp)++; 318c825d4dcSJohn Baldwin } 319c825d4dcSJohn Baldwin 320c825d4dcSJohn Baldwin struct alloc_state { 321c825d4dcSJohn Baldwin struct resource **res; 322c825d4dcSJohn Baldwin struct pcib_softc *sc; 323c825d4dcSJohn Baldwin int count, error; 324c825d4dcSJohn Baldwin }; 325c825d4dcSJohn Baldwin 326c825d4dcSJohn Baldwin static void 327c825d4dcSJohn Baldwin alloc_ranges(u_long start, u_long end, void *arg) 328c825d4dcSJohn Baldwin { 329c825d4dcSJohn Baldwin struct alloc_state *as; 330c825d4dcSJohn Baldwin struct pcib_window *w; 331c825d4dcSJohn Baldwin int rid; 332c825d4dcSJohn Baldwin 333c825d4dcSJohn Baldwin as = arg; 334c825d4dcSJohn Baldwin if (as->error != 0) 335c825d4dcSJohn Baldwin return; 336c825d4dcSJohn Baldwin 337c825d4dcSJohn Baldwin w = &as->sc->io; 338c825d4dcSJohn Baldwin rid = w->reg; 339c825d4dcSJohn Baldwin if (bootverbose) 340c825d4dcSJohn Baldwin device_printf(as->sc->dev, 341c825d4dcSJohn Baldwin "allocating non-ISA range %#lx-%#lx\n", start, end); 342c825d4dcSJohn Baldwin as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT, 343c825d4dcSJohn Baldwin &rid, start, end, end - start + 1, 0); 344c825d4dcSJohn Baldwin if (as->res[as->count] == NULL) 345c825d4dcSJohn Baldwin as->error = ENXIO; 346c825d4dcSJohn Baldwin else 347c825d4dcSJohn Baldwin as->count++; 348c825d4dcSJohn Baldwin } 349c825d4dcSJohn Baldwin 350c825d4dcSJohn Baldwin static int 351c825d4dcSJohn Baldwin pcib_alloc_nonisa_ranges(struct pcib_softc *sc, u_long start, u_long end) 352c825d4dcSJohn Baldwin { 353c825d4dcSJohn Baldwin struct alloc_state as; 354c825d4dcSJohn Baldwin int i, new_count; 355c825d4dcSJohn Baldwin 356c825d4dcSJohn Baldwin /* First, see how many ranges we need. */ 357c825d4dcSJohn Baldwin new_count = 0; 358c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count); 359c825d4dcSJohn Baldwin 360c825d4dcSJohn Baldwin /* Second, allocate the ranges. */ 361c825d4dcSJohn Baldwin as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF, 362c825d4dcSJohn Baldwin M_WAITOK); 363c825d4dcSJohn Baldwin as.sc = sc; 364c825d4dcSJohn Baldwin as.count = 0; 365c825d4dcSJohn Baldwin as.error = 0; 366c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as); 367c825d4dcSJohn Baldwin if (as.error != 0) { 368c825d4dcSJohn Baldwin for (i = 0; i < as.count; i++) 369c825d4dcSJohn Baldwin bus_release_resource(sc->dev, SYS_RES_IOPORT, 370c825d4dcSJohn Baldwin sc->io.reg, as.res[i]); 371c825d4dcSJohn Baldwin free(as.res, M_DEVBUF); 372c825d4dcSJohn Baldwin return (as.error); 373c825d4dcSJohn Baldwin } 374c825d4dcSJohn Baldwin KASSERT(as.count == new_count, ("%s: count mismatch", __func__)); 375c825d4dcSJohn Baldwin 376c825d4dcSJohn Baldwin /* Third, add the ranges to the window. */ 377c825d4dcSJohn Baldwin pcib_add_window_resources(&sc->io, as.res, as.count); 378c825d4dcSJohn Baldwin free(as.res, M_DEVBUF); 379c825d4dcSJohn Baldwin return (0); 380c825d4dcSJohn Baldwin } 381c825d4dcSJohn Baldwin 38283c41143SJohn Baldwin static void 38383c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type, 38483c41143SJohn Baldwin int flags, pci_addr_t max_address) 38583c41143SJohn Baldwin { 386c825d4dcSJohn Baldwin struct resource *res; 38783c41143SJohn Baldwin char buf[64]; 38883c41143SJohn Baldwin int error, rid; 38983c41143SJohn Baldwin 39083c41143SJohn Baldwin if (max_address != (u_long)max_address) 39183c41143SJohn Baldwin max_address = ~0ul; 39283c41143SJohn Baldwin w->rman.rm_start = 0; 39383c41143SJohn Baldwin w->rman.rm_end = max_address; 39483c41143SJohn Baldwin w->rman.rm_type = RMAN_ARRAY; 39583c41143SJohn Baldwin snprintf(buf, sizeof(buf), "%s %s window", 39683c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name); 39783c41143SJohn Baldwin w->rman.rm_descr = strdup(buf, M_DEVBUF); 39883c41143SJohn Baldwin error = rman_init(&w->rman); 39983c41143SJohn Baldwin if (error) 40083c41143SJohn Baldwin panic("Failed to initialize %s %s rman", 40183c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name); 40283c41143SJohn Baldwin 40383c41143SJohn Baldwin if (!pcib_is_window_open(w)) 40483c41143SJohn Baldwin return; 40583c41143SJohn Baldwin 40683c41143SJohn Baldwin if (w->base > max_address || w->limit > max_address) { 40783c41143SJohn Baldwin device_printf(sc->dev, 40883c41143SJohn Baldwin "initial %s window has too many bits, ignoring\n", w->name); 40983c41143SJohn Baldwin return; 41083c41143SJohn Baldwin } 411c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE) 412c825d4dcSJohn Baldwin (void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit); 413c825d4dcSJohn Baldwin else { 41483c41143SJohn Baldwin rid = w->reg; 415c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit, 41683c41143SJohn Baldwin w->limit - w->base + 1, flags); 417c825d4dcSJohn Baldwin if (res != NULL) 418c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 419c825d4dcSJohn Baldwin } 42083c41143SJohn Baldwin if (w->res == NULL) { 42183c41143SJohn Baldwin device_printf(sc->dev, 42283c41143SJohn Baldwin "failed to allocate initial %s window: %#jx-%#jx\n", 42383c41143SJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 42483c41143SJohn Baldwin w->base = max_address; 42583c41143SJohn Baldwin w->limit = 0; 42683c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 42783c41143SJohn Baldwin return; 42883c41143SJohn Baldwin } 42983c41143SJohn Baldwin pcib_activate_window(sc, type); 43083c41143SJohn Baldwin } 43183c41143SJohn Baldwin 43283c41143SJohn Baldwin /* 43383c41143SJohn Baldwin * Initialize I/O windows. 43483c41143SJohn Baldwin */ 43583c41143SJohn Baldwin static void 43683c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc) 43783c41143SJohn Baldwin { 43883c41143SJohn Baldwin pci_addr_t max; 43983c41143SJohn Baldwin device_t dev; 44083c41143SJohn Baldwin uint32_t val; 44183c41143SJohn Baldwin 44283c41143SJohn Baldwin dev = sc->dev; 44383c41143SJohn Baldwin 4440070c94bSJohn Baldwin if (pci_clear_pcib) { 445809923caSJustin Hibbits pcib_bridge_init(dev); 4460070c94bSJohn Baldwin } 4470070c94bSJohn Baldwin 44883c41143SJohn Baldwin /* Determine if the I/O port window is implemented. */ 44983c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 45083c41143SJohn Baldwin if (val == 0) { 45183c41143SJohn Baldwin /* 45283c41143SJohn Baldwin * If 'val' is zero, then only 16-bits of I/O space 45383c41143SJohn Baldwin * are supported. 45483c41143SJohn Baldwin */ 45583c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 45683c41143SJohn Baldwin if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) { 45783c41143SJohn Baldwin sc->io.valid = 1; 45883c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0, 1); 45983c41143SJohn Baldwin } 46083c41143SJohn Baldwin } else 46183c41143SJohn Baldwin sc->io.valid = 1; 46283c41143SJohn Baldwin 46383c41143SJohn Baldwin /* Read the existing I/O port window. */ 46483c41143SJohn Baldwin if (sc->io.valid) { 46583c41143SJohn Baldwin sc->io.reg = PCIR_IOBASEL_1; 46683c41143SJohn Baldwin sc->io.step = 12; 46783c41143SJohn Baldwin sc->io.mask = WIN_IO; 46883c41143SJohn Baldwin sc->io.name = "I/O port"; 46983c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 47083c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE( 47183c41143SJohn Baldwin pci_read_config(dev, PCIR_IOBASEH_1, 2), val); 47283c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT( 47383c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITH_1, 2), 47483c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 47583c41143SJohn Baldwin max = 0xffffffff; 47683c41143SJohn Baldwin } else { 47783c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE(0, val); 47883c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT(0, 47983c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 48083c41143SJohn Baldwin max = 0xffff; 48183c41143SJohn Baldwin } 48283c41143SJohn Baldwin pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max); 48383c41143SJohn Baldwin } 48483c41143SJohn Baldwin 48583c41143SJohn Baldwin /* Read the existing memory window. */ 48683c41143SJohn Baldwin sc->mem.valid = 1; 48783c41143SJohn Baldwin sc->mem.reg = PCIR_MEMBASE_1; 48883c41143SJohn Baldwin sc->mem.step = 20; 48983c41143SJohn Baldwin sc->mem.mask = WIN_MEM; 49083c41143SJohn Baldwin sc->mem.name = "memory"; 49183c41143SJohn Baldwin sc->mem.base = PCI_PPBMEMBASE(0, 49283c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMBASE_1, 2)); 49383c41143SJohn Baldwin sc->mem.limit = PCI_PPBMEMLIMIT(0, 49483c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 49583c41143SJohn Baldwin pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff); 49683c41143SJohn Baldwin 49783c41143SJohn Baldwin /* Determine if the prefetchable memory window is implemented. */ 49883c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 49983c41143SJohn Baldwin if (val == 0) { 50083c41143SJohn Baldwin /* 50183c41143SJohn Baldwin * If 'val' is zero, then only 32-bits of memory space 50283c41143SJohn Baldwin * are supported. 50383c41143SJohn Baldwin */ 50483c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 50583c41143SJohn Baldwin if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) { 50683c41143SJohn Baldwin sc->pmem.valid = 1; 50783c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0, 2); 50883c41143SJohn Baldwin } 50983c41143SJohn Baldwin } else 51083c41143SJohn Baldwin sc->pmem.valid = 1; 51183c41143SJohn Baldwin 51283c41143SJohn Baldwin /* Read the existing prefetchable memory window. */ 51383c41143SJohn Baldwin if (sc->pmem.valid) { 51483c41143SJohn Baldwin sc->pmem.reg = PCIR_PMBASEL_1; 51583c41143SJohn Baldwin sc->pmem.step = 20; 51683c41143SJohn Baldwin sc->pmem.mask = WIN_PMEM; 51783c41143SJohn Baldwin sc->pmem.name = "prefetch"; 51883c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 51983c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE( 52083c41143SJohn Baldwin pci_read_config(dev, PCIR_PMBASEH_1, 4), val); 52183c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT( 52283c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITH_1, 4), 52383c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 52483c41143SJohn Baldwin max = 0xffffffffffffffff; 52583c41143SJohn Baldwin } else { 52683c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE(0, val); 52783c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT(0, 52883c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 52983c41143SJohn Baldwin max = 0xffffffff; 53083c41143SJohn Baldwin } 53183c41143SJohn Baldwin pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY, 53283c41143SJohn Baldwin RF_PREFETCHABLE, max); 53383c41143SJohn Baldwin } 53483c41143SJohn Baldwin } 53583c41143SJohn Baldwin 5364edef187SJohn Baldwin #ifdef PCI_RES_BUS 5374edef187SJohn Baldwin /* 5384edef187SJohn Baldwin * Allocate a suitable secondary bus for this bridge if needed and 5394edef187SJohn Baldwin * initialize the resource manager for the secondary bus range. Note 5404edef187SJohn Baldwin * that the minimum count is a desired value and this may allocate a 5414edef187SJohn Baldwin * smaller range. 5424edef187SJohn Baldwin */ 5434edef187SJohn Baldwin void 5444edef187SJohn Baldwin pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count) 5454edef187SJohn Baldwin { 5464edef187SJohn Baldwin char buf[64]; 547ad6f36f8SJohn Baldwin int error, rid, sec_reg; 5484edef187SJohn Baldwin 5494edef187SJohn Baldwin switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) { 5504edef187SJohn Baldwin case PCIM_HDRTYPE_BRIDGE: 551ad6f36f8SJohn Baldwin sec_reg = PCIR_SECBUS_1; 5524edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_1; 5534edef187SJohn Baldwin break; 5544edef187SJohn Baldwin case PCIM_HDRTYPE_CARDBUS: 555ad6f36f8SJohn Baldwin sec_reg = PCIR_SECBUS_2; 5564edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_2; 5574edef187SJohn Baldwin break; 5584edef187SJohn Baldwin default: 5594edef187SJohn Baldwin panic("not a PCI bridge"); 5604edef187SJohn Baldwin } 561ad6f36f8SJohn Baldwin bus->sec = pci_read_config(dev, sec_reg, 1); 562ad6f36f8SJohn Baldwin bus->sub = pci_read_config(dev, bus->sub_reg, 1); 5634edef187SJohn Baldwin bus->dev = dev; 5644edef187SJohn Baldwin bus->rman.rm_start = 0; 5654edef187SJohn Baldwin bus->rman.rm_end = PCI_BUSMAX; 5664edef187SJohn Baldwin bus->rman.rm_type = RMAN_ARRAY; 5674edef187SJohn Baldwin snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev)); 5684edef187SJohn Baldwin bus->rman.rm_descr = strdup(buf, M_DEVBUF); 5694edef187SJohn Baldwin error = rman_init(&bus->rman); 5704edef187SJohn Baldwin if (error) 5714edef187SJohn Baldwin panic("Failed to initialize %s bus number rman", 5724edef187SJohn Baldwin device_get_nameunit(dev)); 5734edef187SJohn Baldwin 5744edef187SJohn Baldwin /* 5754edef187SJohn Baldwin * Allocate a bus range. This will return an existing bus range 5764edef187SJohn Baldwin * if one exists, or a new bus range if one does not. 5774edef187SJohn Baldwin */ 5784edef187SJohn Baldwin rid = 0; 5794edef187SJohn Baldwin bus->res = bus_alloc_resource(dev, PCI_RES_BUS, &rid, 0ul, ~0ul, 5804edef187SJohn Baldwin min_count, 0); 5814edef187SJohn Baldwin if (bus->res == NULL) { 5824edef187SJohn Baldwin /* 5834edef187SJohn Baldwin * Fall back to just allocating a range of a single bus 5844edef187SJohn Baldwin * number. 5854edef187SJohn Baldwin */ 5864edef187SJohn Baldwin bus->res = bus_alloc_resource(dev, PCI_RES_BUS, &rid, 0ul, ~0ul, 5874edef187SJohn Baldwin 1, 0); 5884edef187SJohn Baldwin } else if (rman_get_size(bus->res) < min_count) 5894edef187SJohn Baldwin /* 5904edef187SJohn Baldwin * Attempt to grow the existing range to satisfy the 5914edef187SJohn Baldwin * minimum desired count. 5924edef187SJohn Baldwin */ 5934edef187SJohn Baldwin (void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res, 5944edef187SJohn Baldwin rman_get_start(bus->res), rman_get_start(bus->res) + 5954edef187SJohn Baldwin min_count - 1); 5964edef187SJohn Baldwin 5974edef187SJohn Baldwin /* 5984edef187SJohn Baldwin * Add the initial resource to the rman. 5994edef187SJohn Baldwin */ 6004edef187SJohn Baldwin if (bus->res != NULL) { 6014edef187SJohn Baldwin error = rman_manage_region(&bus->rman, rman_get_start(bus->res), 6024edef187SJohn Baldwin rman_get_end(bus->res)); 6034edef187SJohn Baldwin if (error) 6044edef187SJohn Baldwin panic("Failed to add resource to rman"); 6054edef187SJohn Baldwin bus->sec = rman_get_start(bus->res); 6064edef187SJohn Baldwin bus->sub = rman_get_end(bus->res); 6074edef187SJohn Baldwin } 6084edef187SJohn Baldwin } 6094edef187SJohn Baldwin 6104edef187SJohn Baldwin static struct resource * 6114edef187SJohn Baldwin pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid, 6124edef187SJohn Baldwin u_long start, u_long end, u_long count, u_int flags) 6134edef187SJohn Baldwin { 6144edef187SJohn Baldwin struct resource *res; 6154edef187SJohn Baldwin 6164edef187SJohn Baldwin res = rman_reserve_resource(&bus->rman, start, end, count, flags, 6174edef187SJohn Baldwin child); 6184edef187SJohn Baldwin if (res == NULL) 6194edef187SJohn Baldwin return (NULL); 6204edef187SJohn Baldwin 6214edef187SJohn Baldwin if (bootverbose) 6224edef187SJohn Baldwin device_printf(bus->dev, 6234edef187SJohn Baldwin "allocated bus range (%lu-%lu) for rid %d of %s\n", 6244edef187SJohn Baldwin rman_get_start(res), rman_get_end(res), *rid, 6254edef187SJohn Baldwin pcib_child_name(child)); 6264edef187SJohn Baldwin rman_set_rid(res, *rid); 6274edef187SJohn Baldwin return (res); 6284edef187SJohn Baldwin } 6294edef187SJohn Baldwin 6304edef187SJohn Baldwin /* 6314edef187SJohn Baldwin * Attempt to grow the secondary bus range. This is much simpler than 6324edef187SJohn Baldwin * for I/O windows as the range can only be grown by increasing 6334edef187SJohn Baldwin * subbus. 6344edef187SJohn Baldwin */ 6354edef187SJohn Baldwin static int 6364edef187SJohn Baldwin pcib_grow_subbus(struct pcib_secbus *bus, u_long new_end) 6374edef187SJohn Baldwin { 6384edef187SJohn Baldwin u_long old_end; 6394edef187SJohn Baldwin int error; 6404edef187SJohn Baldwin 6414edef187SJohn Baldwin old_end = rman_get_end(bus->res); 6424edef187SJohn Baldwin KASSERT(new_end > old_end, ("attempt to shrink subbus")); 6434edef187SJohn Baldwin error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res, 6444edef187SJohn Baldwin rman_get_start(bus->res), new_end); 6454edef187SJohn Baldwin if (error) 6464edef187SJohn Baldwin return (error); 6474edef187SJohn Baldwin if (bootverbose) 6484edef187SJohn Baldwin device_printf(bus->dev, "grew bus range to %lu-%lu\n", 6494edef187SJohn Baldwin rman_get_start(bus->res), rman_get_end(bus->res)); 6504edef187SJohn Baldwin error = rman_manage_region(&bus->rman, old_end + 1, 6514edef187SJohn Baldwin rman_get_end(bus->res)); 6524edef187SJohn Baldwin if (error) 6534edef187SJohn Baldwin panic("Failed to add resource to rman"); 6544edef187SJohn Baldwin bus->sub = rman_get_end(bus->res); 6554edef187SJohn Baldwin pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1); 6564edef187SJohn Baldwin return (0); 6574edef187SJohn Baldwin } 6584edef187SJohn Baldwin 6594edef187SJohn Baldwin struct resource * 6604edef187SJohn Baldwin pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid, 6614edef187SJohn Baldwin u_long start, u_long end, u_long count, u_int flags) 6624edef187SJohn Baldwin { 6634edef187SJohn Baldwin struct resource *res; 6644edef187SJohn Baldwin u_long start_free, end_free, new_end; 6654edef187SJohn Baldwin 6664edef187SJohn Baldwin /* 6674edef187SJohn Baldwin * First, see if the request can be satisified by the existing 6684edef187SJohn Baldwin * bus range. 6694edef187SJohn Baldwin */ 6704edef187SJohn Baldwin res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags); 6714edef187SJohn Baldwin if (res != NULL) 6724edef187SJohn Baldwin return (res); 6734edef187SJohn Baldwin 6744edef187SJohn Baldwin /* 6754edef187SJohn Baldwin * Figure out a range to grow the bus range. First, find the 6764edef187SJohn Baldwin * first bus number after the last allocated bus in the rman and 6774edef187SJohn Baldwin * enforce that as a minimum starting point for the range. 6784edef187SJohn Baldwin */ 6794edef187SJohn Baldwin if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 || 6804edef187SJohn Baldwin end_free != bus->sub) 6814edef187SJohn Baldwin start_free = bus->sub + 1; 6824edef187SJohn Baldwin if (start_free < start) 6834edef187SJohn Baldwin start_free = start; 6844edef187SJohn Baldwin new_end = start_free + count - 1; 6854edef187SJohn Baldwin 6864edef187SJohn Baldwin /* 6874edef187SJohn Baldwin * See if this new range would satisfy the request if it 6884edef187SJohn Baldwin * succeeds. 6894edef187SJohn Baldwin */ 6904edef187SJohn Baldwin if (new_end > end) 6914edef187SJohn Baldwin return (NULL); 6924edef187SJohn Baldwin 6934edef187SJohn Baldwin /* Finally, attempt to grow the existing resource. */ 6944edef187SJohn Baldwin if (bootverbose) { 6954edef187SJohn Baldwin device_printf(bus->dev, 6964edef187SJohn Baldwin "attempting to grow bus range for %lu buses\n", count); 6974edef187SJohn Baldwin printf("\tback candidate range: %lu-%lu\n", start_free, 6984edef187SJohn Baldwin new_end); 6994edef187SJohn Baldwin } 7004edef187SJohn Baldwin if (pcib_grow_subbus(bus, new_end) == 0) 7014edef187SJohn Baldwin return (pcib_suballoc_bus(bus, child, rid, start, end, count, 7024edef187SJohn Baldwin flags)); 7034edef187SJohn Baldwin return (NULL); 7044edef187SJohn Baldwin } 7054edef187SJohn Baldwin #endif 7064edef187SJohn Baldwin 70783c41143SJohn Baldwin #else 70883c41143SJohn Baldwin 709bb0d0a8eSMike Smith /* 710b0a2d4b8SWarner Losh * Is the prefetch window open (eg, can we allocate memory in it?) 711b0a2d4b8SWarner Losh */ 712b0a2d4b8SWarner Losh static int 713b0a2d4b8SWarner Losh pcib_is_prefetch_open(struct pcib_softc *sc) 714b0a2d4b8SWarner Losh { 715b0a2d4b8SWarner Losh return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit); 716b0a2d4b8SWarner Losh } 717b0a2d4b8SWarner Losh 718b0a2d4b8SWarner Losh /* 719b0a2d4b8SWarner Losh * Is the nonprefetch window open (eg, can we allocate memory in it?) 720b0a2d4b8SWarner Losh */ 721b0a2d4b8SWarner Losh static int 722b0a2d4b8SWarner Losh pcib_is_nonprefetch_open(struct pcib_softc *sc) 723b0a2d4b8SWarner Losh { 724b0a2d4b8SWarner Losh return (sc->membase > 0 && sc->membase < sc->memlimit); 725b0a2d4b8SWarner Losh } 726b0a2d4b8SWarner Losh 727b0a2d4b8SWarner Losh /* 728b0a2d4b8SWarner Losh * Is the io window open (eg, can we allocate ports in it?) 729b0a2d4b8SWarner Losh */ 730b0a2d4b8SWarner Losh static int 731b0a2d4b8SWarner Losh pcib_is_io_open(struct pcib_softc *sc) 732b0a2d4b8SWarner Losh { 733b0a2d4b8SWarner Losh return (sc->iobase > 0 && sc->iobase < sc->iolimit); 734b0a2d4b8SWarner Losh } 735b0a2d4b8SWarner Losh 736b0a2d4b8SWarner Losh /* 737e36af292SJung-uk Kim * Get current I/O decode. 738e36af292SJung-uk Kim */ 739e36af292SJung-uk Kim static void 740e36af292SJung-uk Kim pcib_get_io_decode(struct pcib_softc *sc) 741e36af292SJung-uk Kim { 742e36af292SJung-uk Kim device_t dev; 743e36af292SJung-uk Kim uint32_t iolow; 744e36af292SJung-uk Kim 745e36af292SJung-uk Kim dev = sc->dev; 746e36af292SJung-uk Kim 747e36af292SJung-uk Kim iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1); 748e36af292SJung-uk Kim if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 749e36af292SJung-uk Kim sc->iobase = PCI_PPBIOBASE( 750e36af292SJung-uk Kim pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow); 751e36af292SJung-uk Kim else 752e36af292SJung-uk Kim sc->iobase = PCI_PPBIOBASE(0, iolow); 753e36af292SJung-uk Kim 754e36af292SJung-uk Kim iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1); 755e36af292SJung-uk Kim if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 756e36af292SJung-uk Kim sc->iolimit = PCI_PPBIOLIMIT( 757e36af292SJung-uk Kim pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow); 758e36af292SJung-uk Kim else 759e36af292SJung-uk Kim sc->iolimit = PCI_PPBIOLIMIT(0, iolow); 760e36af292SJung-uk Kim } 761e36af292SJung-uk Kim 762e36af292SJung-uk Kim /* 763e36af292SJung-uk Kim * Get current memory decode. 764e36af292SJung-uk Kim */ 765e36af292SJung-uk Kim static void 766e36af292SJung-uk Kim pcib_get_mem_decode(struct pcib_softc *sc) 767e36af292SJung-uk Kim { 768e36af292SJung-uk Kim device_t dev; 769e36af292SJung-uk Kim pci_addr_t pmemlow; 770e36af292SJung-uk Kim 771e36af292SJung-uk Kim dev = sc->dev; 772e36af292SJung-uk Kim 773e36af292SJung-uk Kim sc->membase = PCI_PPBMEMBASE(0, 774e36af292SJung-uk Kim pci_read_config(dev, PCIR_MEMBASE_1, 2)); 775e36af292SJung-uk Kim sc->memlimit = PCI_PPBMEMLIMIT(0, 776e36af292SJung-uk Kim pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 777e36af292SJung-uk Kim 778e36af292SJung-uk Kim pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2); 779e36af292SJung-uk Kim if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 780e36af292SJung-uk Kim sc->pmembase = PCI_PPBMEMBASE( 781e36af292SJung-uk Kim pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow); 782e36af292SJung-uk Kim else 783e36af292SJung-uk Kim sc->pmembase = PCI_PPBMEMBASE(0, pmemlow); 784e36af292SJung-uk Kim 785e36af292SJung-uk Kim pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2); 786e36af292SJung-uk Kim if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 787e36af292SJung-uk Kim sc->pmemlimit = PCI_PPBMEMLIMIT( 788e36af292SJung-uk Kim pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow); 789e36af292SJung-uk Kim else 790e36af292SJung-uk Kim sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow); 791e36af292SJung-uk Kim } 792e36af292SJung-uk Kim 793e36af292SJung-uk Kim /* 794e36af292SJung-uk Kim * Restore previous I/O decode. 795e36af292SJung-uk Kim */ 796e36af292SJung-uk Kim static void 797e36af292SJung-uk Kim pcib_set_io_decode(struct pcib_softc *sc) 798e36af292SJung-uk Kim { 799e36af292SJung-uk Kim device_t dev; 800e36af292SJung-uk Kim uint32_t iohi; 801e36af292SJung-uk Kim 802e36af292SJung-uk Kim dev = sc->dev; 803e36af292SJung-uk Kim 804e36af292SJung-uk Kim iohi = sc->iobase >> 16; 805e36af292SJung-uk Kim if (iohi > 0) 806e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2); 807e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1); 808e36af292SJung-uk Kim 809e36af292SJung-uk Kim iohi = sc->iolimit >> 16; 810e36af292SJung-uk Kim if (iohi > 0) 811e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2); 812e36af292SJung-uk Kim pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1); 813e36af292SJung-uk Kim } 814e36af292SJung-uk Kim 815e36af292SJung-uk Kim /* 816e36af292SJung-uk Kim * Restore previous memory decode. 817e36af292SJung-uk Kim */ 818e36af292SJung-uk Kim static void 819e36af292SJung-uk Kim pcib_set_mem_decode(struct pcib_softc *sc) 820e36af292SJung-uk Kim { 821e36af292SJung-uk Kim device_t dev; 822e36af292SJung-uk Kim pci_addr_t pmemhi; 823e36af292SJung-uk Kim 824e36af292SJung-uk Kim dev = sc->dev; 825e36af292SJung-uk Kim 826e36af292SJung-uk Kim pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2); 827e36af292SJung-uk Kim pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2); 828e36af292SJung-uk Kim 829e36af292SJung-uk Kim pmemhi = sc->pmembase >> 32; 830e36af292SJung-uk Kim if (pmemhi > 0) 831e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4); 832e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2); 833e36af292SJung-uk Kim 834e36af292SJung-uk Kim pmemhi = sc->pmemlimit >> 32; 835e36af292SJung-uk Kim if (pmemhi > 0) 836e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4); 837e36af292SJung-uk Kim pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2); 838e36af292SJung-uk Kim } 83983c41143SJohn Baldwin #endif 840e36af292SJung-uk Kim 841e36af292SJung-uk Kim /* 842e36af292SJung-uk Kim * Get current bridge configuration. 843e36af292SJung-uk Kim */ 844e36af292SJung-uk Kim static void 845e36af292SJung-uk Kim pcib_cfg_save(struct pcib_softc *sc) 846e36af292SJung-uk Kim { 847ad6f36f8SJohn Baldwin #ifndef NEW_PCIB 848e36af292SJung-uk Kim device_t dev; 849ad6f36f8SJohn Baldwin uint16_t command; 850e36af292SJung-uk Kim 851e36af292SJung-uk Kim dev = sc->dev; 852e36af292SJung-uk Kim 853ad6f36f8SJohn Baldwin command = pci_read_config(dev, PCIR_COMMAND, 2); 854ad6f36f8SJohn Baldwin if (command & PCIM_CMD_PORTEN) 855e36af292SJung-uk Kim pcib_get_io_decode(sc); 856ad6f36f8SJohn Baldwin if (command & PCIM_CMD_MEMEN) 857e36af292SJung-uk Kim pcib_get_mem_decode(sc); 85883c41143SJohn Baldwin #endif 859e36af292SJung-uk Kim } 860e36af292SJung-uk Kim 861e36af292SJung-uk Kim /* 862e36af292SJung-uk Kim * Restore previous bridge configuration. 863e36af292SJung-uk Kim */ 864e36af292SJung-uk Kim static void 865e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc) 866e36af292SJung-uk Kim { 867e36af292SJung-uk Kim device_t dev; 868ad6f36f8SJohn Baldwin #ifndef NEW_PCIB 869ad6f36f8SJohn Baldwin uint16_t command; 870ad6f36f8SJohn Baldwin #endif 871e36af292SJung-uk Kim dev = sc->dev; 872e36af292SJung-uk Kim 87383c41143SJohn Baldwin #ifdef NEW_PCIB 87483c41143SJohn Baldwin pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM); 87583c41143SJohn Baldwin #else 876ad6f36f8SJohn Baldwin command = pci_read_config(dev, PCIR_COMMAND, 2); 877ad6f36f8SJohn Baldwin if (command & PCIM_CMD_PORTEN) 878e36af292SJung-uk Kim pcib_set_io_decode(sc); 879ad6f36f8SJohn Baldwin if (command & PCIM_CMD_MEMEN) 880e36af292SJung-uk Kim pcib_set_mem_decode(sc); 88183c41143SJohn Baldwin #endif 882e36af292SJung-uk Kim } 883e36af292SJung-uk Kim 884e36af292SJung-uk Kim /* 885bb0d0a8eSMike Smith * Generic device interface 886bb0d0a8eSMike Smith */ 887bb0d0a8eSMike Smith static int 888bb0d0a8eSMike Smith pcib_probe(device_t dev) 889bb0d0a8eSMike Smith { 890bb0d0a8eSMike Smith if ((pci_get_class(dev) == PCIC_BRIDGE) && 891bb0d0a8eSMike Smith (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) { 892bb0d0a8eSMike Smith device_set_desc(dev, "PCI-PCI bridge"); 893b7cbd25bSMarcel Moolenaar return(-10000); 894bb0d0a8eSMike Smith } 895bb0d0a8eSMike Smith return(ENXIO); 896bb0d0a8eSMike Smith } 897bb0d0a8eSMike Smith 8986f0d5884SJohn Baldwin void 8996f0d5884SJohn Baldwin pcib_attach_common(device_t dev) 900bb0d0a8eSMike Smith { 901bb0d0a8eSMike Smith struct pcib_softc *sc; 902abf07f13SWarner Losh struct sysctl_ctx_list *sctx; 903abf07f13SWarner Losh struct sysctl_oid *soid; 904c825d4dcSJohn Baldwin int comma; 905bb0d0a8eSMike Smith 906bb0d0a8eSMike Smith sc = device_get_softc(dev); 907bb0d0a8eSMike Smith sc->dev = dev; 908bb0d0a8eSMike Smith 9094fa59183SMike Smith /* 9104fa59183SMike Smith * Get current bridge configuration. 9114fa59183SMike Smith */ 91255aaf894SMarius Strobl sc->domain = pci_get_domain(dev); 913ad6f36f8SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 914ad6f36f8SJohn Baldwin sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1); 915ad6f36f8SJohn Baldwin sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1); 916ad6f36f8SJohn Baldwin #endif 917ad6f36f8SJohn Baldwin sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 918e36af292SJung-uk Kim pcib_cfg_save(sc); 9194fa59183SMike Smith 9204fa59183SMike Smith /* 9214edef187SJohn Baldwin * The primary bus register should always be the bus of the 9224edef187SJohn Baldwin * parent. 9234edef187SJohn Baldwin */ 9244edef187SJohn Baldwin sc->pribus = pci_get_bus(dev); 9254edef187SJohn Baldwin pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1); 9264edef187SJohn Baldwin 9274edef187SJohn Baldwin /* 928abf07f13SWarner Losh * Setup sysctl reporting nodes 929abf07f13SWarner Losh */ 930abf07f13SWarner Losh sctx = device_get_sysctl_ctx(dev); 931abf07f13SWarner Losh soid = device_get_sysctl_tree(dev); 932abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain", 933abf07f13SWarner Losh CTLFLAG_RD, &sc->domain, 0, "Domain number"); 934abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus", 935abf07f13SWarner Losh CTLFLAG_RD, &sc->pribus, 0, "Primary bus number"); 936abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus", 9374edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number"); 938abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus", 9394edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number"); 940abf07f13SWarner Losh 941abf07f13SWarner Losh /* 9424fa59183SMike Smith * Quirk handling. 9434fa59183SMike Smith */ 9444fa59183SMike Smith switch (pci_get_devid(dev)) { 9452ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 9464fa59183SMike Smith case 0x12258086: /* Intel 82454KX/GX (Orion) */ 9474fa59183SMike Smith { 948b0cb115fSWarner Losh uint8_t supbus; 9494fa59183SMike Smith 9504fa59183SMike Smith supbus = pci_read_config(dev, 0x41, 1); 9514fa59183SMike Smith if (supbus != 0xff) { 9524edef187SJohn Baldwin sc->bus.sec = supbus + 1; 9534edef187SJohn Baldwin sc->bus.sub = supbus + 1; 9544fa59183SMike Smith } 9554fa59183SMike Smith break; 9564fa59183SMike Smith } 9574edef187SJohn Baldwin #endif 9584fa59183SMike Smith 959e4b59fc5SWarner Losh /* 960e4b59fc5SWarner Losh * The i82380FB mobile docking controller is a PCI-PCI bridge, 961e4b59fc5SWarner Losh * and it is a subtractive bridge. However, the ProgIf is wrong 962e4b59fc5SWarner Losh * so the normal setting of PCIB_SUBTRACTIVE bit doesn't 963*4718610dSZbigniew Bodek * happen. There are also Toshiba and Cavium ThunderX bridges 964*4718610dSZbigniew Bodek * that behave this way. 965e4b59fc5SWarner Losh */ 966*4718610dSZbigniew Bodek case 0xa002177d: /* Cavium ThunderX */ 967e4b59fc5SWarner Losh case 0x124b8086: /* Intel 82380FB Mobile */ 968e4b59fc5SWarner Losh case 0x060513d7: /* Toshiba ???? */ 969e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 970e4b59fc5SWarner Losh break; 971c94d6dbeSJung-uk Kim 9722ae5fd15SJohn Baldwin #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 973c94d6dbeSJung-uk Kim /* Compaq R3000 BIOS sets wrong subordinate bus number. */ 974c94d6dbeSJung-uk Kim case 0x00dd10de: 975c94d6dbeSJung-uk Kim { 976c94d6dbeSJung-uk Kim char *cp; 977c94d6dbeSJung-uk Kim 9782be111bfSDavide Italiano if ((cp = kern_getenv("smbios.planar.maker")) == NULL) 979c94d6dbeSJung-uk Kim break; 9801def0ca6SJung-uk Kim if (strncmp(cp, "Compal", 6) != 0) { 9811def0ca6SJung-uk Kim freeenv(cp); 982c94d6dbeSJung-uk Kim break; 9831def0ca6SJung-uk Kim } 9841def0ca6SJung-uk Kim freeenv(cp); 9852be111bfSDavide Italiano if ((cp = kern_getenv("smbios.planar.product")) == NULL) 9861def0ca6SJung-uk Kim break; 9871def0ca6SJung-uk Kim if (strncmp(cp, "08A0", 4) != 0) { 9881def0ca6SJung-uk Kim freeenv(cp); 9891def0ca6SJung-uk Kim break; 9901def0ca6SJung-uk Kim } 9911def0ca6SJung-uk Kim freeenv(cp); 9924edef187SJohn Baldwin if (sc->bus.sub < 0xa) { 993c94d6dbeSJung-uk Kim pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1); 9944edef187SJohn Baldwin sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1); 995c94d6dbeSJung-uk Kim } 996c94d6dbeSJung-uk Kim break; 997c94d6dbeSJung-uk Kim } 9984edef187SJohn Baldwin #endif 999e4b59fc5SWarner Losh } 1000e4b59fc5SWarner Losh 100122bf1c7fSJohn Baldwin if (pci_msi_device_blacklisted(dev)) 100222bf1c7fSJohn Baldwin sc->flags |= PCIB_DISABLE_MSI; 100322bf1c7fSJohn Baldwin 100468e9cbd3SMarius Strobl if (pci_msix_device_blacklisted(dev)) 100568e9cbd3SMarius Strobl sc->flags |= PCIB_DISABLE_MSIX; 100668e9cbd3SMarius Strobl 1007e4b59fc5SWarner Losh /* 1008e4b59fc5SWarner Losh * Intel 815, 845 and other chipsets say they are PCI-PCI bridges, 1009e4b59fc5SWarner Losh * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM, 1010e4b59fc5SWarner Losh * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese. 1011e4b59fc5SWarner Losh * This means they act as if they were subtractively decoding 1012e4b59fc5SWarner Losh * bridges and pass all transactions. Mark them and real ProgIf 1 1013e4b59fc5SWarner Losh * parts as subtractive. 1014e4b59fc5SWarner Losh */ 1015e4b59fc5SWarner Losh if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 || 1016657d9f9fSJohn Baldwin pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE) 1017e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE; 1018e4b59fc5SWarner Losh 101983c41143SJohn Baldwin #ifdef NEW_PCIB 10204edef187SJohn Baldwin #ifdef PCI_RES_BUS 10214edef187SJohn Baldwin pcib_setup_secbus(dev, &sc->bus, 1); 10224edef187SJohn Baldwin #endif 102383c41143SJohn Baldwin pcib_probe_windows(sc); 102483c41143SJohn Baldwin #endif 1025bb0d0a8eSMike Smith if (bootverbose) { 102655aaf894SMarius Strobl device_printf(dev, " domain %d\n", sc->domain); 10274edef187SJohn Baldwin device_printf(dev, " secondary bus %d\n", sc->bus.sec); 10284edef187SJohn Baldwin device_printf(dev, " subordinate bus %d\n", sc->bus.sub); 102983c41143SJohn Baldwin #ifdef NEW_PCIB 103083c41143SJohn Baldwin if (pcib_is_window_open(&sc->io)) 103183c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%jx-0x%jx\n", 103283c41143SJohn Baldwin (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit); 103383c41143SJohn Baldwin if (pcib_is_window_open(&sc->mem)) 103483c41143SJohn Baldwin device_printf(dev, " memory decode 0x%jx-0x%jx\n", 103583c41143SJohn Baldwin (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit); 103683c41143SJohn Baldwin if (pcib_is_window_open(&sc->pmem)) 103783c41143SJohn Baldwin device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 103883c41143SJohn Baldwin (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit); 103983c41143SJohn Baldwin #else 104083c41143SJohn Baldwin if (pcib_is_io_open(sc)) 104183c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%x-0x%x\n", 104283c41143SJohn Baldwin sc->iobase, sc->iolimit); 1043b0a2d4b8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 1044b0a2d4b8SWarner Losh device_printf(dev, " memory decode 0x%jx-0x%jx\n", 1045b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit); 1046b0a2d4b8SWarner Losh if (pcib_is_prefetch_open(sc)) 1047b0a2d4b8SWarner Losh device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 1048b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 104983c41143SJohn Baldwin #endif 1050c825d4dcSJohn Baldwin if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) || 1051c825d4dcSJohn Baldwin sc->flags & PCIB_SUBTRACTIVE) { 1052c825d4dcSJohn Baldwin device_printf(dev, " special decode "); 1053c825d4dcSJohn Baldwin comma = 0; 1054c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) { 1055c825d4dcSJohn Baldwin printf("ISA"); 1056c825d4dcSJohn Baldwin comma = 1; 1057c825d4dcSJohn Baldwin } 1058c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) { 1059c825d4dcSJohn Baldwin printf("%sVGA", comma ? ", " : ""); 1060c825d4dcSJohn Baldwin comma = 1; 1061c825d4dcSJohn Baldwin } 1062e4b59fc5SWarner Losh if (sc->flags & PCIB_SUBTRACTIVE) 1063c825d4dcSJohn Baldwin printf("%ssubtractive", comma ? ", " : ""); 1064c825d4dcSJohn Baldwin printf("\n"); 1065c825d4dcSJohn Baldwin } 1066bb0d0a8eSMike Smith } 1067bb0d0a8eSMike Smith 1068bb0d0a8eSMike Smith /* 1069ef888152SJohn Baldwin * Always enable busmastering on bridges so that transactions 1070ef888152SJohn Baldwin * initiated on the secondary bus are passed through to the 1071ef888152SJohn Baldwin * primary bus. 1072ef888152SJohn Baldwin */ 1073ef888152SJohn Baldwin pci_enable_busmaster(dev); 10746f0d5884SJohn Baldwin } 1075bb0d0a8eSMike Smith 107638906aedSJohn Baldwin int 10776f0d5884SJohn Baldwin pcib_attach(device_t dev) 10786f0d5884SJohn Baldwin { 10796f0d5884SJohn Baldwin struct pcib_softc *sc; 10806f0d5884SJohn Baldwin device_t child; 10816f0d5884SJohn Baldwin 10826f0d5884SJohn Baldwin pcib_attach_common(dev); 10836f0d5884SJohn Baldwin sc = device_get_softc(dev); 10844edef187SJohn Baldwin if (sc->bus.sec != 0) { 10854edef187SJohn Baldwin child = device_add_child(dev, "pci", sc->bus.sec); 1086bb0d0a8eSMike Smith if (child != NULL) 1087bb0d0a8eSMike Smith return(bus_generic_attach(dev)); 1088bb0d0a8eSMike Smith } 1089bb0d0a8eSMike Smith 1090bb0d0a8eSMike Smith /* no secondary bus; we should have fixed this */ 1091bb0d0a8eSMike Smith return(0); 1092bb0d0a8eSMike Smith } 1093bb0d0a8eSMike Smith 10946f0d5884SJohn Baldwin int 1095e36af292SJung-uk Kim pcib_suspend(device_t dev) 1096e36af292SJung-uk Kim { 1097e36af292SJung-uk Kim 1098e36af292SJung-uk Kim pcib_cfg_save(device_get_softc(dev)); 10997212fc6aSJohn Baldwin return (bus_generic_suspend(dev)); 1100e36af292SJung-uk Kim } 1101e36af292SJung-uk Kim 1102e36af292SJung-uk Kim int 1103e36af292SJung-uk Kim pcib_resume(device_t dev) 1104e36af292SJung-uk Kim { 1105e36af292SJung-uk Kim 1106e36af292SJung-uk Kim pcib_cfg_restore(device_get_softc(dev)); 1107e36af292SJung-uk Kim return (bus_generic_resume(dev)); 1108e36af292SJung-uk Kim } 1109e36af292SJung-uk Kim 1110809923caSJustin Hibbits void 1111809923caSJustin Hibbits pcib_bridge_init(device_t dev) 1112809923caSJustin Hibbits { 1113809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 1114809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2); 1115809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1); 1116809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2); 1117809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2); 1118809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2); 1119809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 1120809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4); 1121809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2); 1122809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4); 1123809923caSJustin Hibbits } 1124809923caSJustin Hibbits 1125e36af292SJung-uk Kim int 1126bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 1127bb0d0a8eSMike Smith { 1128bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 1129bb0d0a8eSMike Smith 1130bb0d0a8eSMike Smith switch (which) { 113155aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 113255aaf894SMarius Strobl *result = sc->domain; 113355aaf894SMarius Strobl return(0); 1134bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 11354edef187SJohn Baldwin *result = sc->bus.sec; 1136bb0d0a8eSMike Smith return(0); 1137bb0d0a8eSMike Smith } 1138bb0d0a8eSMike Smith return(ENOENT); 1139bb0d0a8eSMike Smith } 1140bb0d0a8eSMike Smith 11416f0d5884SJohn Baldwin int 1142bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 1143bb0d0a8eSMike Smith { 1144bb0d0a8eSMike Smith 1145bb0d0a8eSMike Smith switch (which) { 114655aaf894SMarius Strobl case PCIB_IVAR_DOMAIN: 114755aaf894SMarius Strobl return(EINVAL); 1148bb0d0a8eSMike Smith case PCIB_IVAR_BUS: 11494edef187SJohn Baldwin return(EINVAL); 1150bb0d0a8eSMike Smith } 1151bb0d0a8eSMike Smith return(ENOENT); 1152bb0d0a8eSMike Smith } 1153bb0d0a8eSMike Smith 115483c41143SJohn Baldwin #ifdef NEW_PCIB 115583c41143SJohn Baldwin /* 115683c41143SJohn Baldwin * Attempt to allocate a resource from the existing resources assigned 115783c41143SJohn Baldwin * to a window. 115883c41143SJohn Baldwin */ 115983c41143SJohn Baldwin static struct resource * 116083c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w, 116183c41143SJohn Baldwin device_t child, int type, int *rid, u_long start, u_long end, u_long count, 116283c41143SJohn Baldwin u_int flags) 116383c41143SJohn Baldwin { 116483c41143SJohn Baldwin struct resource *res; 116583c41143SJohn Baldwin 116683c41143SJohn Baldwin if (!pcib_is_window_open(w)) 116783c41143SJohn Baldwin return (NULL); 116883c41143SJohn Baldwin 116983c41143SJohn Baldwin res = rman_reserve_resource(&w->rman, start, end, count, 117083c41143SJohn Baldwin flags & ~RF_ACTIVE, child); 117183c41143SJohn Baldwin if (res == NULL) 117283c41143SJohn Baldwin return (NULL); 117383c41143SJohn Baldwin 117483c41143SJohn Baldwin if (bootverbose) 117583c41143SJohn Baldwin device_printf(sc->dev, 117683c41143SJohn Baldwin "allocated %s range (%#lx-%#lx) for rid %x of %s\n", 117783c41143SJohn Baldwin w->name, rman_get_start(res), rman_get_end(res), *rid, 117883c41143SJohn Baldwin pcib_child_name(child)); 117983c41143SJohn Baldwin rman_set_rid(res, *rid); 118083c41143SJohn Baldwin 118183c41143SJohn Baldwin /* 118283c41143SJohn Baldwin * If the resource should be active, pass that request up the 118383c41143SJohn Baldwin * tree. This assumes the parent drivers can handle 118483c41143SJohn Baldwin * activating sub-allocated resources. 118583c41143SJohn Baldwin */ 118683c41143SJohn Baldwin if (flags & RF_ACTIVE) { 118783c41143SJohn Baldwin if (bus_activate_resource(child, type, *rid, res) != 0) { 118883c41143SJohn Baldwin rman_release_resource(res); 118983c41143SJohn Baldwin return (NULL); 119083c41143SJohn Baldwin } 119183c41143SJohn Baldwin } 119283c41143SJohn Baldwin 119383c41143SJohn Baldwin return (res); 119483c41143SJohn Baldwin } 119583c41143SJohn Baldwin 1196c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */ 1197c825d4dcSJohn Baldwin static int 1198c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type, 1199c825d4dcSJohn Baldwin u_long start, u_long end, u_long count, u_int flags) 1200c825d4dcSJohn Baldwin { 1201c825d4dcSJohn Baldwin struct resource *res; 1202c825d4dcSJohn Baldwin u_long base, limit, wmask; 1203c825d4dcSJohn Baldwin int rid; 1204c825d4dcSJohn Baldwin 1205c825d4dcSJohn Baldwin /* 1206c825d4dcSJohn Baldwin * If this is an I/O window on a bridge with ISA enable set 1207c825d4dcSJohn Baldwin * and the start address is below 64k, then try to allocate an 1208c825d4dcSJohn Baldwin * initial window of 0x1000 bytes long starting at address 1209c825d4dcSJohn Baldwin * 0xf000 and walking down. Note that if the original request 1210c825d4dcSJohn Baldwin * was larger than the non-aliased range size of 0x100 our 1211c825d4dcSJohn Baldwin * caller would have raised the start address up to 64k 1212c825d4dcSJohn Baldwin * already. 1213c825d4dcSJohn Baldwin */ 1214c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1215c825d4dcSJohn Baldwin start < 65536) { 1216c825d4dcSJohn Baldwin for (base = 0xf000; (long)base >= 0; base -= 0x1000) { 1217c825d4dcSJohn Baldwin limit = base + 0xfff; 1218c825d4dcSJohn Baldwin 1219c825d4dcSJohn Baldwin /* 1220c825d4dcSJohn Baldwin * Skip ranges that wouldn't work for the 1221c825d4dcSJohn Baldwin * original request. Note that the actual 1222c825d4dcSJohn Baldwin * window that overlaps are the non-alias 1223c825d4dcSJohn Baldwin * ranges within [base, limit], so this isn't 1224c825d4dcSJohn Baldwin * quite a simple comparison. 1225c825d4dcSJohn Baldwin */ 1226c825d4dcSJohn Baldwin if (start + count > limit - 0x400) 1227c825d4dcSJohn Baldwin continue; 1228c825d4dcSJohn Baldwin if (base == 0) { 1229c825d4dcSJohn Baldwin /* 1230c825d4dcSJohn Baldwin * The first open region for the window at 1231c825d4dcSJohn Baldwin * 0 is 0x400-0x4ff. 1232c825d4dcSJohn Baldwin */ 1233c825d4dcSJohn Baldwin if (end - count + 1 < 0x400) 1234c825d4dcSJohn Baldwin continue; 1235c825d4dcSJohn Baldwin } else { 1236c825d4dcSJohn Baldwin if (end - count + 1 < base) 1237c825d4dcSJohn Baldwin continue; 1238c825d4dcSJohn Baldwin } 1239c825d4dcSJohn Baldwin 1240c825d4dcSJohn Baldwin if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) { 1241c825d4dcSJohn Baldwin w->base = base; 1242c825d4dcSJohn Baldwin w->limit = limit; 1243c825d4dcSJohn Baldwin return (0); 1244c825d4dcSJohn Baldwin } 1245c825d4dcSJohn Baldwin } 1246c825d4dcSJohn Baldwin return (ENOSPC); 1247c825d4dcSJohn Baldwin } 1248c825d4dcSJohn Baldwin 1249c825d4dcSJohn Baldwin wmask = (1ul << w->step) - 1; 1250c825d4dcSJohn Baldwin if (RF_ALIGNMENT(flags) < w->step) { 1251c825d4dcSJohn Baldwin flags &= ~RF_ALIGNMENT_MASK; 1252c825d4dcSJohn Baldwin flags |= RF_ALIGNMENT_LOG2(w->step); 1253c825d4dcSJohn Baldwin } 1254c825d4dcSJohn Baldwin start &= ~wmask; 1255c825d4dcSJohn Baldwin end |= wmask; 1256c825d4dcSJohn Baldwin count = roundup2(count, 1ul << w->step); 1257c825d4dcSJohn Baldwin rid = w->reg; 1258c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, start, end, count, 1259c825d4dcSJohn Baldwin flags & ~RF_ACTIVE); 1260c825d4dcSJohn Baldwin if (res == NULL) 1261c825d4dcSJohn Baldwin return (ENOSPC); 1262c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1); 1263c825d4dcSJohn Baldwin pcib_activate_window(sc, type); 1264c825d4dcSJohn Baldwin w->base = rman_get_start(res); 1265c825d4dcSJohn Baldwin w->limit = rman_get_end(res); 1266c825d4dcSJohn Baldwin return (0); 1267c825d4dcSJohn Baldwin } 1268c825d4dcSJohn Baldwin 1269c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */ 1270c825d4dcSJohn Baldwin static int 1271c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type, 1272c825d4dcSJohn Baldwin u_long base, u_long limit) 1273c825d4dcSJohn Baldwin { 1274c825d4dcSJohn Baldwin struct resource *res; 1275c825d4dcSJohn Baldwin int error, i, force_64k_base; 1276c825d4dcSJohn Baldwin 1277c825d4dcSJohn Baldwin KASSERT(base <= w->base && limit >= w->limit, 1278c825d4dcSJohn Baldwin ("attempting to shrink window")); 1279c825d4dcSJohn Baldwin 1280c825d4dcSJohn Baldwin /* 1281c825d4dcSJohn Baldwin * XXX: pcib_grow_window() doesn't try to do this anyway and 1282c825d4dcSJohn Baldwin * the error handling for all the edge cases would be tedious. 1283c825d4dcSJohn Baldwin */ 1284c825d4dcSJohn Baldwin KASSERT(limit == w->limit || base == w->base, 1285c825d4dcSJohn Baldwin ("attempting to grow both ends of a window")); 1286c825d4dcSJohn Baldwin 1287c825d4dcSJohn Baldwin /* 1288c825d4dcSJohn Baldwin * Yet more special handling for requests to expand an I/O 1289c825d4dcSJohn Baldwin * window behind an ISA-enabled bridge. Since I/O windows 1290c825d4dcSJohn Baldwin * have to grow in 0x1000 increments and the end of the 0xffff 1291c825d4dcSJohn Baldwin * range is an alias, growing a window below 64k will always 1292c825d4dcSJohn Baldwin * result in allocating new resources and never adjusting an 1293c825d4dcSJohn Baldwin * existing resource. 1294c825d4dcSJohn Baldwin */ 1295c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1296c825d4dcSJohn Baldwin (limit <= 65535 || (base <= 65535 && base != w->base))) { 1297c825d4dcSJohn Baldwin KASSERT(limit == w->limit || limit <= 65535, 1298c825d4dcSJohn Baldwin ("attempting to grow both ends across 64k ISA alias")); 1299c825d4dcSJohn Baldwin 1300c825d4dcSJohn Baldwin if (base != w->base) 1301c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1); 1302c825d4dcSJohn Baldwin else 1303c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, w->limit + 1, 1304c825d4dcSJohn Baldwin limit); 1305c825d4dcSJohn Baldwin if (error == 0) { 1306c825d4dcSJohn Baldwin w->base = base; 1307c825d4dcSJohn Baldwin w->limit = limit; 1308c825d4dcSJohn Baldwin } 1309c825d4dcSJohn Baldwin return (error); 1310c825d4dcSJohn Baldwin } 1311c825d4dcSJohn Baldwin 1312c825d4dcSJohn Baldwin /* 1313c825d4dcSJohn Baldwin * Find the existing resource to adjust. Usually there is only one, 1314c825d4dcSJohn Baldwin * but for an ISA-enabled bridge we might be growing the I/O window 1315c825d4dcSJohn Baldwin * above 64k and need to find the existing resource that maps all 1316c825d4dcSJohn Baldwin * of the area above 64k. 1317c825d4dcSJohn Baldwin */ 1318c825d4dcSJohn Baldwin for (i = 0; i < w->count; i++) { 1319c825d4dcSJohn Baldwin if (rman_get_end(w->res[i]) == w->limit) 1320c825d4dcSJohn Baldwin break; 1321c825d4dcSJohn Baldwin } 1322c825d4dcSJohn Baldwin KASSERT(i != w->count, ("did not find existing resource")); 1323c825d4dcSJohn Baldwin res = w->res[i]; 1324c825d4dcSJohn Baldwin 1325c825d4dcSJohn Baldwin /* 1326c825d4dcSJohn Baldwin * Usually the resource we found should match the window's 1327c825d4dcSJohn Baldwin * existing range. The one exception is the ISA-enabled case 1328c825d4dcSJohn Baldwin * mentioned above in which case the resource should start at 1329c825d4dcSJohn Baldwin * 64k. 1330c825d4dcSJohn Baldwin */ 1331c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1332c825d4dcSJohn Baldwin w->base <= 65535) { 1333c825d4dcSJohn Baldwin KASSERT(rman_get_start(res) == 65536, 1334c825d4dcSJohn Baldwin ("existing resource mismatch")); 1335c825d4dcSJohn Baldwin force_64k_base = 1; 1336c825d4dcSJohn Baldwin } else { 1337c825d4dcSJohn Baldwin KASSERT(w->base == rman_get_start(res), 1338c825d4dcSJohn Baldwin ("existing resource mismatch")); 1339c825d4dcSJohn Baldwin force_64k_base = 0; 1340c825d4dcSJohn Baldwin } 1341c825d4dcSJohn Baldwin 1342c825d4dcSJohn Baldwin error = bus_adjust_resource(sc->dev, type, res, force_64k_base ? 1343c825d4dcSJohn Baldwin rman_get_start(res) : base, limit); 1344c825d4dcSJohn Baldwin if (error) 1345c825d4dcSJohn Baldwin return (error); 1346c825d4dcSJohn Baldwin 1347c825d4dcSJohn Baldwin /* Add the newly allocated region to the resource manager. */ 1348c825d4dcSJohn Baldwin if (w->base != base) { 1349c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, base, w->base - 1); 1350c825d4dcSJohn Baldwin w->base = base; 1351c825d4dcSJohn Baldwin } else { 1352c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, w->limit + 1, limit); 1353c825d4dcSJohn Baldwin w->limit = limit; 1354c825d4dcSJohn Baldwin } 1355c825d4dcSJohn Baldwin if (error) { 1356c825d4dcSJohn Baldwin if (bootverbose) 1357c825d4dcSJohn Baldwin device_printf(sc->dev, 1358c825d4dcSJohn Baldwin "failed to expand %s resource manager\n", w->name); 1359c825d4dcSJohn Baldwin (void)bus_adjust_resource(sc->dev, type, res, force_64k_base ? 1360c825d4dcSJohn Baldwin rman_get_start(res) : w->base, w->limit); 1361c825d4dcSJohn Baldwin } 1362c825d4dcSJohn Baldwin return (error); 1363c825d4dcSJohn Baldwin } 1364c825d4dcSJohn Baldwin 136583c41143SJohn Baldwin /* 136683c41143SJohn Baldwin * Attempt to grow a window to make room for a given resource request. 136783c41143SJohn Baldwin */ 136883c41143SJohn Baldwin static int 136983c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type, 137083c41143SJohn Baldwin u_long start, u_long end, u_long count, u_int flags) 137183c41143SJohn Baldwin { 1372a7b5acacSJohn Baldwin u_long align, start_free, end_free, front, back, wmask; 1373c825d4dcSJohn Baldwin int error; 137483c41143SJohn Baldwin 137583c41143SJohn Baldwin /* 137683c41143SJohn Baldwin * Clamp the desired resource range to the maximum address 137783c41143SJohn Baldwin * this window supports. Reject impossible requests. 1378c825d4dcSJohn Baldwin * 1379c825d4dcSJohn Baldwin * For I/O port requests behind a bridge with the ISA enable 1380c825d4dcSJohn Baldwin * bit set, force large allocations to start above 64k. 138183c41143SJohn Baldwin */ 138283c41143SJohn Baldwin if (!w->valid) 138383c41143SJohn Baldwin return (EINVAL); 1384c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 && 1385c825d4dcSJohn Baldwin start < 65536) 1386c825d4dcSJohn Baldwin start = 65536; 138783c41143SJohn Baldwin if (end > w->rman.rm_end) 138883c41143SJohn Baldwin end = w->rman.rm_end; 138983c41143SJohn Baldwin if (start + count - 1 > end || start + count < start) 139083c41143SJohn Baldwin return (EINVAL); 1391a7b5acacSJohn Baldwin wmask = (1ul << w->step) - 1; 139283c41143SJohn Baldwin 139383c41143SJohn Baldwin /* 139483c41143SJohn Baldwin * If there is no resource at all, just try to allocate enough 139583c41143SJohn Baldwin * aligned space for this resource. 139683c41143SJohn Baldwin */ 139783c41143SJohn Baldwin if (w->res == NULL) { 1398c825d4dcSJohn Baldwin error = pcib_alloc_new_window(sc, w, type, start, end, count, 1399c825d4dcSJohn Baldwin flags); 1400c825d4dcSJohn Baldwin if (error) { 140183c41143SJohn Baldwin if (bootverbose) 140283c41143SJohn Baldwin device_printf(sc->dev, 140383c41143SJohn Baldwin "failed to allocate initial %s window (%#lx-%#lx,%#lx)\n", 140483c41143SJohn Baldwin w->name, start, end, count); 140583c41143SJohn Baldwin return (error); 140683c41143SJohn Baldwin } 1407c825d4dcSJohn Baldwin if (bootverbose) 1408c825d4dcSJohn Baldwin device_printf(sc->dev, 1409c825d4dcSJohn Baldwin "allocated initial %s window of %#jx-%#jx\n", 1410c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 141183c41143SJohn Baldwin goto updatewin; 141283c41143SJohn Baldwin } 141383c41143SJohn Baldwin 141483c41143SJohn Baldwin /* 141583c41143SJohn Baldwin * See if growing the window would help. Compute the minimum 141683c41143SJohn Baldwin * amount of address space needed on both the front and back 141783c41143SJohn Baldwin * ends of the existing window to satisfy the allocation. 141883c41143SJohn Baldwin * 141983c41143SJohn Baldwin * For each end, build a candidate region adjusting for the 142083c41143SJohn Baldwin * required alignment, etc. If there is a free region at the 142183c41143SJohn Baldwin * edge of the window, grow from the inner edge of the free 142283c41143SJohn Baldwin * region. Otherwise grow from the window boundary. 142383c41143SJohn Baldwin * 1424c825d4dcSJohn Baldwin * Growing an I/O window below 64k for a bridge with the ISA 1425c825d4dcSJohn Baldwin * enable bit doesn't require any special magic as the step 1426c825d4dcSJohn Baldwin * size of an I/O window (1k) always includes multiple 1427c825d4dcSJohn Baldwin * non-alias ranges when it is grown in either direction. 1428c825d4dcSJohn Baldwin * 142983c41143SJohn Baldwin * XXX: Special case: if w->res is completely empty and the 143083c41143SJohn Baldwin * request size is larger than w->res, we should find the 143183c41143SJohn Baldwin * optimal aligned buffer containing w->res and allocate that. 143283c41143SJohn Baldwin */ 143383c41143SJohn Baldwin if (bootverbose) 143483c41143SJohn Baldwin device_printf(sc->dev, 143583c41143SJohn Baldwin "attempting to grow %s window for (%#lx-%#lx,%#lx)\n", 143683c41143SJohn Baldwin w->name, start, end, count); 143783c41143SJohn Baldwin align = 1ul << RF_ALIGNMENT(flags); 1438c825d4dcSJohn Baldwin if (start < w->base) { 143983c41143SJohn Baldwin if (rman_first_free_region(&w->rman, &start_free, &end_free) != 1440c825d4dcSJohn Baldwin 0 || start_free != w->base) 1441c825d4dcSJohn Baldwin end_free = w->base; 144283c41143SJohn Baldwin if (end_free > end) 1443ddac8cc9SJohn Baldwin end_free = end + 1; 144483c41143SJohn Baldwin 144583c41143SJohn Baldwin /* Move end_free down until it is properly aligned. */ 144683c41143SJohn Baldwin end_free &= ~(align - 1); 1447a49dcb46SJohn Baldwin end_free--; 1448a49dcb46SJohn Baldwin front = end_free - (count - 1); 144983c41143SJohn Baldwin 145083c41143SJohn Baldwin /* 145183c41143SJohn Baldwin * The resource would now be allocated at (front, 145283c41143SJohn Baldwin * end_free). Ensure that fits in the (start, end) 145383c41143SJohn Baldwin * bounds. end_free is checked above. If 'front' is 145483c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 145583c41143SJohn Baldwin * Also check for underflow. 145683c41143SJohn Baldwin */ 145783c41143SJohn Baldwin if (front >= start && front <= end_free) { 145883c41143SJohn Baldwin if (bootverbose) 145983c41143SJohn Baldwin printf("\tfront candidate range: %#lx-%#lx\n", 146083c41143SJohn Baldwin front, end_free); 1461a7b5acacSJohn Baldwin front &= ~wmask; 1462c825d4dcSJohn Baldwin front = w->base - front; 146383c41143SJohn Baldwin } else 146483c41143SJohn Baldwin front = 0; 146583c41143SJohn Baldwin } else 146683c41143SJohn Baldwin front = 0; 1467c825d4dcSJohn Baldwin if (end > w->limit) { 146883c41143SJohn Baldwin if (rman_last_free_region(&w->rman, &start_free, &end_free) != 1469c825d4dcSJohn Baldwin 0 || end_free != w->limit) 1470c825d4dcSJohn Baldwin start_free = w->limit + 1; 147183c41143SJohn Baldwin if (start_free < start) 147283c41143SJohn Baldwin start_free = start; 147383c41143SJohn Baldwin 147483c41143SJohn Baldwin /* Move start_free up until it is properly aligned. */ 147583c41143SJohn Baldwin start_free = roundup2(start_free, align); 1476a49dcb46SJohn Baldwin back = start_free + count - 1; 147783c41143SJohn Baldwin 147883c41143SJohn Baldwin /* 147983c41143SJohn Baldwin * The resource would now be allocated at (start_free, 148083c41143SJohn Baldwin * back). Ensure that fits in the (start, end) 148183c41143SJohn Baldwin * bounds. start_free is checked above. If 'back' is 148283c41143SJohn Baldwin * ok, ensure it is properly aligned for this window. 148383c41143SJohn Baldwin * Also check for overflow. 148483c41143SJohn Baldwin */ 148583c41143SJohn Baldwin if (back <= end && start_free <= back) { 148683c41143SJohn Baldwin if (bootverbose) 148783c41143SJohn Baldwin printf("\tback candidate range: %#lx-%#lx\n", 148883c41143SJohn Baldwin start_free, back); 1489a7b5acacSJohn Baldwin back |= wmask; 1490c825d4dcSJohn Baldwin back -= w->limit; 149183c41143SJohn Baldwin } else 149283c41143SJohn Baldwin back = 0; 149383c41143SJohn Baldwin } else 149483c41143SJohn Baldwin back = 0; 149583c41143SJohn Baldwin 149683c41143SJohn Baldwin /* 149783c41143SJohn Baldwin * Try to allocate the smallest needed region first. 149883c41143SJohn Baldwin * If that fails, fall back to the other region. 149983c41143SJohn Baldwin */ 150083c41143SJohn Baldwin error = ENOSPC; 150183c41143SJohn Baldwin while (front != 0 || back != 0) { 150283c41143SJohn Baldwin if (front != 0 && (front <= back || back == 0)) { 1503c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base - front, 1504c825d4dcSJohn Baldwin w->limit); 150583c41143SJohn Baldwin if (error == 0) 150683c41143SJohn Baldwin break; 150783c41143SJohn Baldwin front = 0; 150883c41143SJohn Baldwin } else { 1509c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base, 1510c825d4dcSJohn Baldwin w->limit + back); 151183c41143SJohn Baldwin if (error == 0) 151283c41143SJohn Baldwin break; 151383c41143SJohn Baldwin back = 0; 151483c41143SJohn Baldwin } 151583c41143SJohn Baldwin } 151683c41143SJohn Baldwin 151783c41143SJohn Baldwin if (error) 151883c41143SJohn Baldwin return (error); 151983c41143SJohn Baldwin if (bootverbose) 1520c825d4dcSJohn Baldwin device_printf(sc->dev, "grew %s window to %#jx-%#jx\n", 1521c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 152283c41143SJohn Baldwin 152383c41143SJohn Baldwin updatewin: 1524c825d4dcSJohn Baldwin /* Write the new window. */ 1525a7b5acacSJohn Baldwin KASSERT((w->base & wmask) == 0, ("start address is not aligned")); 1526a7b5acacSJohn Baldwin KASSERT((w->limit & wmask) == wmask, ("end address is not aligned")); 152783c41143SJohn Baldwin pcib_write_windows(sc, w->mask); 152883c41143SJohn Baldwin return (0); 152983c41143SJohn Baldwin } 153083c41143SJohn Baldwin 153183c41143SJohn Baldwin /* 153283c41143SJohn Baldwin * We have to trap resource allocation requests and ensure that the bridge 153383c41143SJohn Baldwin * is set up to, or capable of handling them. 153483c41143SJohn Baldwin */ 153583c41143SJohn Baldwin struct resource * 153683c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 153783c41143SJohn Baldwin u_long start, u_long end, u_long count, u_int flags) 153883c41143SJohn Baldwin { 153983c41143SJohn Baldwin struct pcib_softc *sc; 154083c41143SJohn Baldwin struct resource *r; 154183c41143SJohn Baldwin 154283c41143SJohn Baldwin sc = device_get_softc(dev); 154383c41143SJohn Baldwin 154483c41143SJohn Baldwin /* 154583c41143SJohn Baldwin * VGA resources are decoded iff the VGA enable bit is set in 154683c41143SJohn Baldwin * the bridge control register. VGA resources do not fall into 154783c41143SJohn Baldwin * the resource windows and are passed up to the parent. 154883c41143SJohn Baldwin */ 154983c41143SJohn Baldwin if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) || 155083c41143SJohn Baldwin (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) { 155183c41143SJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) 155283c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, 155383c41143SJohn Baldwin rid, start, end, count, flags)); 155483c41143SJohn Baldwin else 155583c41143SJohn Baldwin return (NULL); 155683c41143SJohn Baldwin } 155783c41143SJohn Baldwin 155883c41143SJohn Baldwin switch (type) { 15594edef187SJohn Baldwin #ifdef PCI_RES_BUS 15604edef187SJohn Baldwin case PCI_RES_BUS: 15614edef187SJohn Baldwin return (pcib_alloc_subbus(&sc->bus, child, rid, start, end, 15624edef187SJohn Baldwin count, flags)); 15634edef187SJohn Baldwin #endif 156483c41143SJohn Baldwin case SYS_RES_IOPORT: 1565c825d4dcSJohn Baldwin if (pcib_is_isa_range(sc, start, end, count)) 1566c825d4dcSJohn Baldwin return (NULL); 156783c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start, 156883c41143SJohn Baldwin end, count, flags); 1569a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 157083c41143SJohn Baldwin break; 157183c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->io, type, start, end, count, 157283c41143SJohn Baldwin flags) == 0) 157383c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, 157483c41143SJohn Baldwin rid, start, end, count, flags); 157583c41143SJohn Baldwin break; 157683c41143SJohn Baldwin case SYS_RES_MEMORY: 157783c41143SJohn Baldwin /* 157883c41143SJohn Baldwin * For prefetchable resources, prefer the prefetchable 157983c41143SJohn Baldwin * memory window, but fall back to the regular memory 158083c41143SJohn Baldwin * window if that fails. Try both windows before 158183c41143SJohn Baldwin * attempting to grow a window in case the firmware 158283c41143SJohn Baldwin * has used a range in the regular memory window to 158383c41143SJohn Baldwin * map a prefetchable BAR. 158483c41143SJohn Baldwin */ 158583c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 158683c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, type, 158783c41143SJohn Baldwin rid, start, end, count, flags); 158883c41143SJohn Baldwin if (r != NULL) 158983c41143SJohn Baldwin break; 159083c41143SJohn Baldwin } 159183c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid, 159283c41143SJohn Baldwin start, end, count, flags); 1593a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 159483c41143SJohn Baldwin break; 159583c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) { 159683c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->pmem, type, start, end, 159783c41143SJohn Baldwin count, flags) == 0) { 159883c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, 159983c41143SJohn Baldwin type, rid, start, end, count, flags); 160083c41143SJohn Baldwin if (r != NULL) 160183c41143SJohn Baldwin break; 160283c41143SJohn Baldwin } 160383c41143SJohn Baldwin } 160483c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->mem, type, start, end, count, 160583c41143SJohn Baldwin flags & ~RF_PREFETCHABLE) == 0) 160683c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, 160783c41143SJohn Baldwin rid, start, end, count, flags); 160883c41143SJohn Baldwin break; 160983c41143SJohn Baldwin default: 161083c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 161183c41143SJohn Baldwin start, end, count, flags)); 161283c41143SJohn Baldwin } 161383c41143SJohn Baldwin 161483c41143SJohn Baldwin /* 161583c41143SJohn Baldwin * If attempts to suballocate from the window fail but this is a 161683c41143SJohn Baldwin * subtractive bridge, pass the request up the tree. 161783c41143SJohn Baldwin */ 161883c41143SJohn Baldwin if (sc->flags & PCIB_SUBTRACTIVE && r == NULL) 161983c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid, 162083c41143SJohn Baldwin start, end, count, flags)); 162183c41143SJohn Baldwin return (r); 162283c41143SJohn Baldwin } 162383c41143SJohn Baldwin 162483c41143SJohn Baldwin int 162583c41143SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r, 162683c41143SJohn Baldwin u_long start, u_long end) 162783c41143SJohn Baldwin { 162883c41143SJohn Baldwin struct pcib_softc *sc; 162983c41143SJohn Baldwin 163083c41143SJohn Baldwin sc = device_get_softc(bus); 163183c41143SJohn Baldwin if (pcib_is_resource_managed(sc, type, r)) 163283c41143SJohn Baldwin return (rman_adjust_resource(r, start, end)); 163383c41143SJohn Baldwin return (bus_generic_adjust_resource(bus, child, type, r, start, end)); 163483c41143SJohn Baldwin } 163583c41143SJohn Baldwin 163683c41143SJohn Baldwin int 163783c41143SJohn Baldwin pcib_release_resource(device_t dev, device_t child, int type, int rid, 163883c41143SJohn Baldwin struct resource *r) 163983c41143SJohn Baldwin { 164083c41143SJohn Baldwin struct pcib_softc *sc; 164183c41143SJohn Baldwin int error; 164283c41143SJohn Baldwin 164383c41143SJohn Baldwin sc = device_get_softc(dev); 164483c41143SJohn Baldwin if (pcib_is_resource_managed(sc, type, r)) { 164583c41143SJohn Baldwin if (rman_get_flags(r) & RF_ACTIVE) { 164683c41143SJohn Baldwin error = bus_deactivate_resource(child, type, rid, r); 164783c41143SJohn Baldwin if (error) 164883c41143SJohn Baldwin return (error); 164983c41143SJohn Baldwin } 165083c41143SJohn Baldwin return (rman_release_resource(r)); 165183c41143SJohn Baldwin } 165283c41143SJohn Baldwin return (bus_generic_release_resource(dev, child, type, rid, r)); 165383c41143SJohn Baldwin } 165483c41143SJohn Baldwin #else 1655bb0d0a8eSMike Smith /* 1656bb0d0a8eSMike Smith * We have to trap resource allocation requests and ensure that the bridge 1657bb0d0a8eSMike Smith * is set up to, or capable of handling them. 1658bb0d0a8eSMike Smith */ 16596f0d5884SJohn Baldwin struct resource * 1660bb0d0a8eSMike Smith pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 1661bb0d0a8eSMike Smith u_long start, u_long end, u_long count, u_int flags) 1662bb0d0a8eSMike Smith { 1663bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev); 166426043836SJohn Baldwin const char *name, *suffix; 1665a8b354a8SWarner Losh int ok; 1666bb0d0a8eSMike Smith 1667bb0d0a8eSMike Smith /* 1668bb0d0a8eSMike Smith * Fail the allocation for this range if it's not supported. 1669bb0d0a8eSMike Smith */ 167026043836SJohn Baldwin name = device_get_nameunit(child); 167126043836SJohn Baldwin if (name == NULL) { 167226043836SJohn Baldwin name = ""; 167326043836SJohn Baldwin suffix = ""; 167426043836SJohn Baldwin } else 167526043836SJohn Baldwin suffix = " "; 1676bb0d0a8eSMike Smith switch (type) { 1677bb0d0a8eSMike Smith case SYS_RES_IOPORT: 1678a8b354a8SWarner Losh ok = 0; 1679e4b59fc5SWarner Losh if (!pcib_is_io_open(sc)) 1680e4b59fc5SWarner Losh break; 1681a8b354a8SWarner Losh ok = (start >= sc->iobase && end <= sc->iolimit); 1682d98d9b12SMarcel Moolenaar 1683d98d9b12SMarcel Moolenaar /* 1684d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA I/O addresses when the 1685d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 1686d98d9b12SMarcel Moolenaar */ 1687d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_ioport_range(start, end)) 1688d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 1689d98d9b12SMarcel Moolenaar 1690e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 1691a8b354a8SWarner Losh if (!ok) { 169212b8c86eSWarner Losh if (start < sc->iobase) 169312b8c86eSWarner Losh start = sc->iobase; 169412b8c86eSWarner Losh if (end > sc->iolimit) 169512b8c86eSWarner Losh end = sc->iolimit; 16962daa7a07SWarner Losh if (start < end) 16972daa7a07SWarner Losh ok = 1; 1698a8b354a8SWarner Losh } 16991c54ff33SMatthew N. Dodd } else { 1700e4b59fc5SWarner Losh ok = 1; 17019dffe835SWarner Losh #if 0 1702795dceffSWarner Losh /* 1703795dceffSWarner Losh * If we overlap with the subtractive range, then 1704795dceffSWarner Losh * pick the upper range to use. 1705795dceffSWarner Losh */ 1706795dceffSWarner Losh if (start < sc->iolimit && end > sc->iobase) 1707795dceffSWarner Losh start = sc->iolimit + 1; 17089dffe835SWarner Losh #endif 170912b8c86eSWarner Losh } 1710a8b354a8SWarner Losh if (end < start) { 17112daa7a07SWarner Losh device_printf(dev, "ioport: end (%lx) < start (%lx)\n", 17122daa7a07SWarner Losh end, start); 1713a8b354a8SWarner Losh start = 0; 1714a8b354a8SWarner Losh end = 0; 1715a8b354a8SWarner Losh ok = 0; 1716a8b354a8SWarner Losh } 1717a8b354a8SWarner Losh if (!ok) { 171826043836SJohn Baldwin device_printf(dev, "%s%srequested unsupported I/O " 1719a8b354a8SWarner Losh "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n", 172026043836SJohn Baldwin name, suffix, start, end, sc->iobase, sc->iolimit); 1721bb0d0a8eSMike Smith return (NULL); 1722bb0d0a8eSMike Smith } 17234fa59183SMike Smith if (bootverbose) 17242daa7a07SWarner Losh device_printf(dev, 172526043836SJohn Baldwin "%s%srequested I/O range 0x%lx-0x%lx: in range\n", 172626043836SJohn Baldwin name, suffix, start, end); 1727bb0d0a8eSMike Smith break; 1728bb0d0a8eSMike Smith 1729bb0d0a8eSMike Smith case SYS_RES_MEMORY: 1730a8b354a8SWarner Losh ok = 0; 1731a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) 1732a8b354a8SWarner Losh ok = ok || (start >= sc->membase && end <= sc->memlimit); 1733a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) 1734a8b354a8SWarner Losh ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit); 1735d98d9b12SMarcel Moolenaar 1736d98d9b12SMarcel Moolenaar /* 1737d98d9b12SMarcel Moolenaar * Make sure we allow access to VGA memory addresses when the 1738d98d9b12SMarcel Moolenaar * bridge has the "VGA Enable" bit set. 1739d98d9b12SMarcel Moolenaar */ 1740d98d9b12SMarcel Moolenaar if (!ok && pci_is_vga_memory_range(start, end)) 1741d98d9b12SMarcel Moolenaar ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 1742d98d9b12SMarcel Moolenaar 1743e4b59fc5SWarner Losh if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 1744a8b354a8SWarner Losh if (!ok) { 1745a8b354a8SWarner Losh ok = 1; 1746a8b354a8SWarner Losh if (flags & RF_PREFETCHABLE) { 1747a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 1748a8b354a8SWarner Losh if (start < sc->pmembase) 1749a8b354a8SWarner Losh start = sc->pmembase; 1750a8b354a8SWarner Losh if (end > sc->pmemlimit) 1751a8b354a8SWarner Losh end = sc->pmemlimit; 1752a8b354a8SWarner Losh } else { 1753a8b354a8SWarner Losh ok = 0; 1754a8b354a8SWarner Losh } 1755a8b354a8SWarner Losh } else { /* non-prefetchable */ 1756a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 1757a8b354a8SWarner Losh if (start < sc->membase) 175812b8c86eSWarner Losh start = sc->membase; 175912b8c86eSWarner Losh if (end > sc->memlimit) 176012b8c86eSWarner Losh end = sc->memlimit; 17611c54ff33SMatthew N. Dodd } else { 1762a8b354a8SWarner Losh ok = 0; 1763a8b354a8SWarner Losh } 1764a8b354a8SWarner Losh } 1765a8b354a8SWarner Losh } 1766a8b354a8SWarner Losh } else if (!ok) { 1767e4b59fc5SWarner Losh ok = 1; /* subtractive bridge: always ok */ 17689dffe835SWarner Losh #if 0 1769a8b354a8SWarner Losh if (pcib_is_nonprefetch_open(sc)) { 1770795dceffSWarner Losh if (start < sc->memlimit && end > sc->membase) 1771795dceffSWarner Losh start = sc->memlimit + 1; 1772a8b354a8SWarner Losh } 1773a8b354a8SWarner Losh if (pcib_is_prefetch_open(sc)) { 1774795dceffSWarner Losh if (start < sc->pmemlimit && end > sc->pmembase) 1775795dceffSWarner Losh start = sc->pmemlimit + 1; 17761c54ff33SMatthew N. Dodd } 17779dffe835SWarner Losh #endif 177812b8c86eSWarner Losh } 1779a8b354a8SWarner Losh if (end < start) { 17802daa7a07SWarner Losh device_printf(dev, "memory: end (%lx) < start (%lx)\n", 17812daa7a07SWarner Losh end, start); 1782a8b354a8SWarner Losh start = 0; 1783a8b354a8SWarner Losh end = 0; 1784a8b354a8SWarner Losh ok = 0; 1785a8b354a8SWarner Losh } 1786a8b354a8SWarner Losh if (!ok && bootverbose) 178734428485SWarner Losh device_printf(dev, 178826043836SJohn Baldwin "%s%srequested unsupported memory range %#lx-%#lx " 1789b0a2d4b8SWarner Losh "(decoding %#jx-%#jx, %#jx-%#jx)\n", 179026043836SJohn Baldwin name, suffix, start, end, 1791b0a2d4b8SWarner Losh (uintmax_t)sc->membase, (uintmax_t)sc->memlimit, 1792b0a2d4b8SWarner Losh (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 1793a8b354a8SWarner Losh if (!ok) 1794bb0d0a8eSMike Smith return (NULL); 17954fa59183SMike Smith if (bootverbose) 179626043836SJohn Baldwin device_printf(dev,"%s%srequested memory range " 17972daa7a07SWarner Losh "0x%lx-0x%lx: good\n", 179826043836SJohn Baldwin name, suffix, start, end); 17994fa59183SMike Smith break; 18004fa59183SMike Smith 1801bb0d0a8eSMike Smith default: 18024fa59183SMike Smith break; 1803bb0d0a8eSMike Smith } 1804bb0d0a8eSMike Smith /* 1805bb0d0a8eSMike Smith * Bridge is OK decoding this resource, so pass it up. 1806bb0d0a8eSMike Smith */ 18072daa7a07SWarner Losh return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 18082daa7a07SWarner Losh count, flags)); 1809bb0d0a8eSMike Smith } 181083c41143SJohn Baldwin #endif 1811bb0d0a8eSMike Smith 1812bb0d0a8eSMike Smith /* 181355d3ea17SRyan Stone * If ARI is enabled on this downstream port, translate the function number 181455d3ea17SRyan Stone * to the non-ARI slot/function. The downstream port will convert it back in 181555d3ea17SRyan Stone * hardware. If ARI is not enabled slot and func are not modified. 181655d3ea17SRyan Stone */ 181755d3ea17SRyan Stone static __inline void 181855d3ea17SRyan Stone pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func) 181955d3ea17SRyan Stone { 182055d3ea17SRyan Stone struct pcib_softc *sc; 182155d3ea17SRyan Stone int ari_func; 182255d3ea17SRyan Stone 182355d3ea17SRyan Stone sc = device_get_softc(pcib); 182455d3ea17SRyan Stone ari_func = *func; 182555d3ea17SRyan Stone 182655d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 182755d3ea17SRyan Stone KASSERT(*slot == 0, 182855d3ea17SRyan Stone ("Non-zero slot number with ARI enabled!")); 182955d3ea17SRyan Stone *slot = PCIE_ARI_SLOT(ari_func); 183055d3ea17SRyan Stone *func = PCIE_ARI_FUNC(ari_func); 183155d3ea17SRyan Stone } 183255d3ea17SRyan Stone } 183355d3ea17SRyan Stone 183455d3ea17SRyan Stone 183555d3ea17SRyan Stone static void 183655d3ea17SRyan Stone pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos) 183755d3ea17SRyan Stone { 183855d3ea17SRyan Stone uint32_t ctl2; 183955d3ea17SRyan Stone 184055d3ea17SRyan Stone ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4); 184155d3ea17SRyan Stone ctl2 |= PCIEM_CTL2_ARI; 184255d3ea17SRyan Stone pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4); 184355d3ea17SRyan Stone 184455d3ea17SRyan Stone sc->flags |= PCIB_ENABLE_ARI; 184555d3ea17SRyan Stone } 184655d3ea17SRyan Stone 184755d3ea17SRyan Stone /* 1848bb0d0a8eSMike Smith * PCIB interface. 1849bb0d0a8eSMike Smith */ 18506f0d5884SJohn Baldwin int 1851bb0d0a8eSMike Smith pcib_maxslots(device_t dev) 1852bb0d0a8eSMike Smith { 18534fa59183SMike Smith return (PCI_SLOTMAX); 1854bb0d0a8eSMike Smith } 1855bb0d0a8eSMike Smith 185655d3ea17SRyan Stone static int 185755d3ea17SRyan Stone pcib_ari_maxslots(device_t dev) 185855d3ea17SRyan Stone { 185955d3ea17SRyan Stone struct pcib_softc *sc; 186055d3ea17SRyan Stone 186155d3ea17SRyan Stone sc = device_get_softc(dev); 186255d3ea17SRyan Stone 186355d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 186455d3ea17SRyan Stone return (PCIE_ARI_SLOTMAX); 186555d3ea17SRyan Stone else 186655d3ea17SRyan Stone return (PCI_SLOTMAX); 186755d3ea17SRyan Stone } 186855d3ea17SRyan Stone 186955d3ea17SRyan Stone static int 187055d3ea17SRyan Stone pcib_ari_maxfuncs(device_t dev) 187155d3ea17SRyan Stone { 187255d3ea17SRyan Stone struct pcib_softc *sc; 187355d3ea17SRyan Stone 187455d3ea17SRyan Stone sc = device_get_softc(dev); 187555d3ea17SRyan Stone 187655d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) 187755d3ea17SRyan Stone return (PCIE_ARI_FUNCMAX); 187855d3ea17SRyan Stone else 187955d3ea17SRyan Stone return (PCI_FUNCMAX); 188055d3ea17SRyan Stone } 188155d3ea17SRyan Stone 18822397d2d8SRyan Stone static void 18832397d2d8SRyan Stone pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot, 18842397d2d8SRyan Stone int *func) 18852397d2d8SRyan Stone { 18862397d2d8SRyan Stone struct pcib_softc *sc; 18872397d2d8SRyan Stone 18882397d2d8SRyan Stone sc = device_get_softc(pcib); 18892397d2d8SRyan Stone 18902397d2d8SRyan Stone *bus = PCI_RID2BUS(rid); 18912397d2d8SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 18922397d2d8SRyan Stone *slot = PCIE_ARI_RID2SLOT(rid); 18932397d2d8SRyan Stone *func = PCIE_ARI_RID2FUNC(rid); 18942397d2d8SRyan Stone } else { 18952397d2d8SRyan Stone *slot = PCI_RID2SLOT(rid); 18962397d2d8SRyan Stone *func = PCI_RID2FUNC(rid); 18972397d2d8SRyan Stone } 18982397d2d8SRyan Stone } 18992397d2d8SRyan Stone 1900bb0d0a8eSMike Smith /* 1901bb0d0a8eSMike Smith * Since we are a child of a PCI bus, its parent must support the pcib interface. 1902bb0d0a8eSMike Smith */ 190355d3ea17SRyan Stone static uint32_t 1904795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width) 1905bb0d0a8eSMike Smith { 190655d3ea17SRyan Stone 190755d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 190855d3ea17SRyan Stone return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, 190955d3ea17SRyan Stone f, reg, width)); 1910bb0d0a8eSMike Smith } 1911bb0d0a8eSMike Smith 191255d3ea17SRyan Stone static void 1913795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width) 1914bb0d0a8eSMike Smith { 191555d3ea17SRyan Stone 191655d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f); 191755d3ea17SRyan Stone PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, 191855d3ea17SRyan Stone reg, val, width); 1919bb0d0a8eSMike Smith } 1920bb0d0a8eSMike Smith 1921bb0d0a8eSMike Smith /* 1922bb0d0a8eSMike Smith * Route an interrupt across a PCI bridge. 1923bb0d0a8eSMike Smith */ 19242c2d1d07SBenno Rice int 1925bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin) 1926bb0d0a8eSMike Smith { 1927bb0d0a8eSMike Smith device_t bus; 1928bb0d0a8eSMike Smith int parent_intpin; 1929bb0d0a8eSMike Smith int intnum; 1930bb0d0a8eSMike Smith 1931bb0d0a8eSMike Smith /* 1932bb0d0a8eSMike Smith * 1933bb0d0a8eSMike Smith * The PCI standard defines a swizzle of the child-side device/intpin to 1934bb0d0a8eSMike Smith * the parent-side intpin as follows. 1935bb0d0a8eSMike Smith * 1936bb0d0a8eSMike Smith * device = device on child bus 1937bb0d0a8eSMike Smith * child_intpin = intpin on child bus slot (0-3) 1938bb0d0a8eSMike Smith * parent_intpin = intpin on parent bus slot (0-3) 1939bb0d0a8eSMike Smith * 1940bb0d0a8eSMike Smith * parent_intpin = (device + child_intpin) % 4 1941bb0d0a8eSMike Smith */ 1942cdc95e1bSBernd Walter parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4; 1943bb0d0a8eSMike Smith 1944bb0d0a8eSMike Smith /* 1945bb0d0a8eSMike Smith * Our parent is a PCI bus. Its parent must export the pcib interface 1946bb0d0a8eSMike Smith * which includes the ability to route interrupts. 1947bb0d0a8eSMike Smith */ 1948bb0d0a8eSMike Smith bus = device_get_parent(pcib); 1949bb0d0a8eSMike Smith intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1); 195039981fedSJohn Baldwin if (PCI_INTERRUPT_VALID(intnum) && bootverbose) { 1951c6a121abSJohn Baldwin device_printf(pcib, "slot %d INT%c is routed to irq %d\n", 1952c6a121abSJohn Baldwin pci_get_slot(dev), 'A' + pin - 1, intnum); 19538046c4b9SMike Smith } 1954bb0d0a8eSMike Smith return(intnum); 1955bb0d0a8eSMike Smith } 1956b173edafSJohn Baldwin 1957e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */ 19589bf4c9c1SJohn Baldwin int 19599bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs) 19609bf4c9c1SJohn Baldwin { 1961bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 19629bf4c9c1SJohn Baldwin device_t bus; 19639bf4c9c1SJohn Baldwin 196422bf1c7fSJohn Baldwin if (sc->flags & PCIB_DISABLE_MSI) 196522bf1c7fSJohn Baldwin return (ENXIO); 19669bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 19679bf4c9c1SJohn Baldwin return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 19689bf4c9c1SJohn Baldwin irqs)); 19699bf4c9c1SJohn Baldwin } 19709bf4c9c1SJohn Baldwin 1971e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */ 19729bf4c9c1SJohn Baldwin int 19739bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs) 19749bf4c9c1SJohn Baldwin { 19759bf4c9c1SJohn Baldwin device_t bus; 19769bf4c9c1SJohn Baldwin 19779bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 19789bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs)); 19799bf4c9c1SJohn Baldwin } 19809bf4c9c1SJohn Baldwin 19819bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */ 19829bf4c9c1SJohn Baldwin int 1983e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq) 19849bf4c9c1SJohn Baldwin { 1985bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib); 19869bf4c9c1SJohn Baldwin device_t bus; 19879bf4c9c1SJohn Baldwin 198868e9cbd3SMarius Strobl if (sc->flags & PCIB_DISABLE_MSIX) 198922bf1c7fSJohn Baldwin return (ENXIO); 19909bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 1991e706f7f0SJohn Baldwin return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq)); 19925fe82bcaSJohn Baldwin } 19935fe82bcaSJohn Baldwin 19949bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */ 19959bf4c9c1SJohn Baldwin int 19969bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq) 19979bf4c9c1SJohn Baldwin { 19989bf4c9c1SJohn Baldwin device_t bus; 19999bf4c9c1SJohn Baldwin 20009bf4c9c1SJohn Baldwin bus = device_get_parent(pcib); 20019bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq)); 20029bf4c9c1SJohn Baldwin } 20039bf4c9c1SJohn Baldwin 2004e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */ 2005e706f7f0SJohn Baldwin int 2006e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, 2007e706f7f0SJohn Baldwin uint32_t *data) 2008e706f7f0SJohn Baldwin { 2009e706f7f0SJohn Baldwin device_t bus; 20104522ac77SLuoqi Chen int error; 2011e706f7f0SJohn Baldwin 2012e706f7f0SJohn Baldwin bus = device_get_parent(pcib); 20134522ac77SLuoqi Chen error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data); 20144522ac77SLuoqi Chen if (error) 20154522ac77SLuoqi Chen return (error); 20164522ac77SLuoqi Chen 20174522ac77SLuoqi Chen pci_ht_map_msi(pcib, *addr); 20184522ac77SLuoqi Chen return (0); 2019e706f7f0SJohn Baldwin } 2020e706f7f0SJohn Baldwin 202162508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */ 202262508c53SJohn Baldwin int 202362508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate) 202462508c53SJohn Baldwin { 202562508c53SJohn Baldwin device_t bus; 202662508c53SJohn Baldwin 202762508c53SJohn Baldwin bus = device_get_parent(pcib); 202862508c53SJohn Baldwin return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate)); 202962508c53SJohn Baldwin } 20305605a99eSRyan Stone 20312397d2d8SRyan Stone static int 20322397d2d8SRyan Stone pcib_ari_enabled(device_t pcib) 20332397d2d8SRyan Stone { 20342397d2d8SRyan Stone struct pcib_softc *sc; 20352397d2d8SRyan Stone 20362397d2d8SRyan Stone sc = device_get_softc(pcib); 20372397d2d8SRyan Stone 20382397d2d8SRyan Stone return ((sc->flags & PCIB_ENABLE_ARI) != 0); 20392397d2d8SRyan Stone } 20402397d2d8SRyan Stone 204155d3ea17SRyan Stone static uint16_t 204255d3ea17SRyan Stone pcib_ari_get_rid(device_t pcib, device_t dev) 204355d3ea17SRyan Stone { 204455d3ea17SRyan Stone struct pcib_softc *sc; 204555d3ea17SRyan Stone uint8_t bus, slot, func; 204655d3ea17SRyan Stone 204755d3ea17SRyan Stone sc = device_get_softc(pcib); 204855d3ea17SRyan Stone 204955d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) { 205055d3ea17SRyan Stone bus = pci_get_bus(dev); 205155d3ea17SRyan Stone func = pci_get_function(dev); 205255d3ea17SRyan Stone 205355d3ea17SRyan Stone return (PCI_ARI_RID(bus, func)); 205455d3ea17SRyan Stone } else { 205555d3ea17SRyan Stone bus = pci_get_bus(dev); 205655d3ea17SRyan Stone slot = pci_get_slot(dev); 205755d3ea17SRyan Stone func = pci_get_function(dev); 205855d3ea17SRyan Stone 205955d3ea17SRyan Stone return (PCI_RID(bus, slot, func)); 206055d3ea17SRyan Stone } 206155d3ea17SRyan Stone } 206255d3ea17SRyan Stone 206355d3ea17SRyan Stone /* 206455d3ea17SRyan Stone * Check that the downstream port (pcib) and the endpoint device (dev) both 206555d3ea17SRyan Stone * support ARI. If so, enable it and return 0, otherwise return an error. 206655d3ea17SRyan Stone */ 206755d3ea17SRyan Stone static int 206855d3ea17SRyan Stone pcib_try_enable_ari(device_t pcib, device_t dev) 206955d3ea17SRyan Stone { 207055d3ea17SRyan Stone struct pcib_softc *sc; 207155d3ea17SRyan Stone int error; 207255d3ea17SRyan Stone uint32_t cap2; 207355d3ea17SRyan Stone int ari_cap_off; 207455d3ea17SRyan Stone uint32_t ari_ver; 207555d3ea17SRyan Stone uint32_t pcie_pos; 207655d3ea17SRyan Stone 207755d3ea17SRyan Stone sc = device_get_softc(pcib); 207855d3ea17SRyan Stone 207955d3ea17SRyan Stone /* 208055d3ea17SRyan Stone * ARI is controlled in a register in the PCIe capability structure. 208155d3ea17SRyan Stone * If the downstream port does not have the PCIe capability structure 208255d3ea17SRyan Stone * then it does not support ARI. 208355d3ea17SRyan Stone */ 208455d3ea17SRyan Stone error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos); 208555d3ea17SRyan Stone if (error != 0) 208655d3ea17SRyan Stone return (ENODEV); 208755d3ea17SRyan Stone 208855d3ea17SRyan Stone /* Check that the PCIe port advertises ARI support. */ 208955d3ea17SRyan Stone cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4); 209055d3ea17SRyan Stone if (!(cap2 & PCIEM_CAP2_ARI)) 209155d3ea17SRyan Stone return (ENODEV); 209255d3ea17SRyan Stone 209355d3ea17SRyan Stone /* 209455d3ea17SRyan Stone * Check that the endpoint device advertises ARI support via the ARI 209555d3ea17SRyan Stone * extended capability structure. 209655d3ea17SRyan Stone */ 209755d3ea17SRyan Stone error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off); 209855d3ea17SRyan Stone if (error != 0) 209955d3ea17SRyan Stone return (ENODEV); 210055d3ea17SRyan Stone 210155d3ea17SRyan Stone /* 210255d3ea17SRyan Stone * Finally, check that the endpoint device supports the same version 210355d3ea17SRyan Stone * of ARI that we do. 210455d3ea17SRyan Stone */ 210555d3ea17SRyan Stone ari_ver = pci_read_config(dev, ari_cap_off, 4); 210655d3ea17SRyan Stone if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) { 210755d3ea17SRyan Stone if (bootverbose) 210855d3ea17SRyan Stone device_printf(pcib, 210955d3ea17SRyan Stone "Unsupported version of ARI (%d) detected\n", 211055d3ea17SRyan Stone PCI_EXTCAP_VER(ari_ver)); 211155d3ea17SRyan Stone 211255d3ea17SRyan Stone return (ENXIO); 211355d3ea17SRyan Stone } 211455d3ea17SRyan Stone 211555d3ea17SRyan Stone pcib_enable_ari(sc, pcie_pos); 211655d3ea17SRyan Stone 211755d3ea17SRyan Stone return (0); 211855d3ea17SRyan Stone } 211955d3ea17SRyan Stone 2120