1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier 5 * Copyright (c) 2000 Michael Smith <msmith@freebsd.org> 6 * Copyright (c) 2000 BSDi 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 /* 37 * PCI:PCI bridge support. 38 */ 39 40 #include "opt_pci.h" 41 42 #include <sys/param.h> 43 #include <sys/bus.h> 44 #include <sys/kernel.h> 45 #include <sys/lock.h> 46 #include <sys/malloc.h> 47 #include <sys/module.h> 48 #include <sys/mutex.h> 49 #include <sys/pciio.h> 50 #include <sys/rman.h> 51 #include <sys/sysctl.h> 52 #include <sys/systm.h> 53 #include <sys/taskqueue.h> 54 55 #include <dev/pci/pcivar.h> 56 #include <dev/pci/pcireg.h> 57 #include <dev/pci/pci_private.h> 58 #include <dev/pci/pcib_private.h> 59 60 #include "pcib_if.h" 61 62 static int pcib_probe(device_t dev); 63 static int pcib_suspend(device_t dev); 64 static int pcib_resume(device_t dev); 65 static int pcib_power_for_sleep(device_t pcib, device_t dev, 66 int *pstate); 67 static int pcib_ari_get_id(device_t pcib, device_t dev, 68 enum pci_id_type type, uintptr_t *id); 69 static uint32_t pcib_read_config(device_t dev, u_int b, u_int s, 70 u_int f, u_int reg, int width); 71 static void pcib_write_config(device_t dev, u_int b, u_int s, 72 u_int f, u_int reg, uint32_t val, int width); 73 static int pcib_ari_maxslots(device_t dev); 74 static int pcib_ari_maxfuncs(device_t dev); 75 static int pcib_try_enable_ari(device_t pcib, device_t dev); 76 static int pcib_ari_enabled(device_t pcib); 77 static void pcib_ari_decode_rid(device_t pcib, uint16_t rid, 78 int *bus, int *slot, int *func); 79 #ifdef PCI_HP 80 static void pcib_pcie_ab_timeout(void *arg, int pending); 81 static void pcib_pcie_cc_timeout(void *arg, int pending); 82 static void pcib_pcie_dll_timeout(void *arg, int pending); 83 #endif 84 static int pcib_request_feature_default(device_t pcib, device_t dev, 85 enum pci_feature feature); 86 static int pcib_reset_child(device_t dev, device_t child, int flags); 87 88 static device_method_t pcib_methods[] = { 89 /* Device interface */ 90 DEVMETHOD(device_probe, pcib_probe), 91 DEVMETHOD(device_attach, pcib_attach), 92 DEVMETHOD(device_detach, pcib_detach), 93 DEVMETHOD(device_shutdown, bus_generic_shutdown), 94 DEVMETHOD(device_suspend, pcib_suspend), 95 DEVMETHOD(device_resume, pcib_resume), 96 97 /* Bus interface */ 98 DEVMETHOD(bus_child_present, pcib_child_present), 99 DEVMETHOD(bus_read_ivar, pcib_read_ivar), 100 DEVMETHOD(bus_write_ivar, pcib_write_ivar), 101 DEVMETHOD(bus_alloc_resource, pcib_alloc_resource), 102 #ifdef NEW_PCIB 103 DEVMETHOD(bus_adjust_resource, pcib_adjust_resource), 104 DEVMETHOD(bus_release_resource, pcib_release_resource), 105 #else 106 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 107 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 108 #endif 109 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 110 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 111 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 112 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 113 DEVMETHOD(bus_reset_child, pcib_reset_child), 114 115 /* pcib interface */ 116 DEVMETHOD(pcib_maxslots, pcib_ari_maxslots), 117 DEVMETHOD(pcib_maxfuncs, pcib_ari_maxfuncs), 118 DEVMETHOD(pcib_read_config, pcib_read_config), 119 DEVMETHOD(pcib_write_config, pcib_write_config), 120 DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt), 121 DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi), 122 DEVMETHOD(pcib_release_msi, pcib_release_msi), 123 DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix), 124 DEVMETHOD(pcib_release_msix, pcib_release_msix), 125 DEVMETHOD(pcib_map_msi, pcib_map_msi), 126 DEVMETHOD(pcib_power_for_sleep, pcib_power_for_sleep), 127 DEVMETHOD(pcib_get_id, pcib_ari_get_id), 128 DEVMETHOD(pcib_try_enable_ari, pcib_try_enable_ari), 129 DEVMETHOD(pcib_ari_enabled, pcib_ari_enabled), 130 DEVMETHOD(pcib_decode_rid, pcib_ari_decode_rid), 131 DEVMETHOD(pcib_request_feature, pcib_request_feature_default), 132 133 DEVMETHOD_END 134 }; 135 136 DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc)); 137 EARLY_DRIVER_MODULE(pcib, pci, pcib_driver, NULL, NULL, BUS_PASS_BUS); 138 139 #if defined(NEW_PCIB) || defined(PCI_HP) 140 SYSCTL_DECL(_hw_pci); 141 #endif 142 143 #ifdef NEW_PCIB 144 static int pci_clear_pcib; 145 SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0, 146 "Clear firmware-assigned resources for PCI-PCI bridge I/O windows."); 147 148 /* 149 * Get the corresponding window if this resource from a child device was 150 * sub-allocated from one of our window resource managers. 151 */ 152 static struct pcib_window * 153 pcib_get_resource_window(struct pcib_softc *sc, int type, struct resource *r) 154 { 155 switch (type) { 156 case SYS_RES_IOPORT: 157 if (rman_is_region_manager(r, &sc->io.rman)) 158 return (&sc->io); 159 break; 160 case SYS_RES_MEMORY: 161 /* Prefetchable resources may live in either memory rman. */ 162 if (rman_get_flags(r) & RF_PREFETCHABLE && 163 rman_is_region_manager(r, &sc->pmem.rman)) 164 return (&sc->pmem); 165 if (rman_is_region_manager(r, &sc->mem.rman)) 166 return (&sc->mem); 167 break; 168 } 169 return (NULL); 170 } 171 172 /* 173 * Is a resource from a child device sub-allocated from one of our 174 * resource managers? 175 */ 176 static int 177 pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r) 178 { 179 180 #ifdef PCI_RES_BUS 181 if (type == PCI_RES_BUS) 182 return (rman_is_region_manager(r, &sc->bus.rman)); 183 #endif 184 return (pcib_get_resource_window(sc, type, r) != NULL); 185 } 186 187 static int 188 pcib_is_window_open(struct pcib_window *pw) 189 { 190 191 return (pw->valid && pw->base < pw->limit); 192 } 193 194 /* 195 * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and 196 * handle for the resource, we could pass RF_ACTIVE up to the PCI bus 197 * when allocating the resource windows and rely on the PCI bus driver 198 * to do this for us. 199 */ 200 static void 201 pcib_activate_window(struct pcib_softc *sc, int type) 202 { 203 204 PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type); 205 } 206 207 static void 208 pcib_write_windows(struct pcib_softc *sc, int mask) 209 { 210 device_t dev; 211 uint32_t val; 212 213 dev = sc->dev; 214 if (sc->io.valid && mask & WIN_IO) { 215 val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 216 if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 217 pci_write_config(dev, PCIR_IOBASEH_1, 218 sc->io.base >> 16, 2); 219 pci_write_config(dev, PCIR_IOLIMITH_1, 220 sc->io.limit >> 16, 2); 221 } 222 pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1); 223 pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1); 224 } 225 226 if (mask & WIN_MEM) { 227 pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2); 228 pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2); 229 } 230 231 if (sc->pmem.valid && mask & WIN_PMEM) { 232 val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 233 if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 234 pci_write_config(dev, PCIR_PMBASEH_1, 235 sc->pmem.base >> 32, 4); 236 pci_write_config(dev, PCIR_PMLIMITH_1, 237 sc->pmem.limit >> 32, 4); 238 } 239 pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2); 240 pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2); 241 } 242 } 243 244 /* 245 * This is used to reject I/O port allocations that conflict with an 246 * ISA alias range. 247 */ 248 static int 249 pcib_is_isa_range(struct pcib_softc *sc, rman_res_t start, rman_res_t end, 250 rman_res_t count) 251 { 252 rman_res_t next_alias; 253 254 if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE)) 255 return (0); 256 257 /* Only check fixed ranges for overlap. */ 258 if (start + count - 1 != end) 259 return (0); 260 261 /* ISA aliases are only in the lower 64KB of I/O space. */ 262 if (start >= 65536) 263 return (0); 264 265 /* Check for overlap with 0x000 - 0x0ff as a special case. */ 266 if (start < 0x100) 267 goto alias; 268 269 /* 270 * If the start address is an alias, the range is an alias. 271 * Otherwise, compute the start of the next alias range and 272 * check if it is before the end of the candidate range. 273 */ 274 if ((start & 0x300) != 0) 275 goto alias; 276 next_alias = (start & ~0x3fful) | 0x100; 277 if (next_alias <= end) 278 goto alias; 279 return (0); 280 281 alias: 282 if (bootverbose) 283 device_printf(sc->dev, 284 "I/O range %#jx-%#jx overlaps with an ISA alias\n", start, 285 end); 286 return (1); 287 } 288 289 static void 290 pcib_add_window_resources(struct pcib_window *w, struct resource **res, 291 int count) 292 { 293 struct resource **newarray; 294 int error, i; 295 296 newarray = malloc(sizeof(struct resource *) * (w->count + count), 297 M_DEVBUF, M_WAITOK); 298 if (w->res != NULL) 299 bcopy(w->res, newarray, sizeof(struct resource *) * w->count); 300 bcopy(res, newarray + w->count, sizeof(struct resource *) * count); 301 free(w->res, M_DEVBUF); 302 w->res = newarray; 303 w->count += count; 304 305 for (i = 0; i < count; i++) { 306 error = rman_manage_region(&w->rman, rman_get_start(res[i]), 307 rman_get_end(res[i])); 308 if (error) 309 panic("Failed to add resource to rman"); 310 } 311 } 312 313 typedef void (nonisa_callback)(rman_res_t start, rman_res_t end, void *arg); 314 315 static void 316 pcib_walk_nonisa_ranges(rman_res_t start, rman_res_t end, nonisa_callback *cb, 317 void *arg) 318 { 319 rman_res_t next_end; 320 321 /* 322 * If start is within an ISA alias range, move up to the start 323 * of the next non-alias range. As a special case, addresses 324 * in the range 0x000 - 0x0ff should also be skipped since 325 * those are used for various system I/O devices in ISA 326 * systems. 327 */ 328 if (start <= 65535) { 329 if (start < 0x100 || (start & 0x300) != 0) { 330 start &= ~0x3ff; 331 start += 0x400; 332 } 333 } 334 335 /* ISA aliases are only in the lower 64KB of I/O space. */ 336 while (start <= MIN(end, 65535)) { 337 next_end = MIN(start | 0xff, end); 338 cb(start, next_end, arg); 339 start += 0x400; 340 } 341 342 if (start <= end) 343 cb(start, end, arg); 344 } 345 346 static void 347 count_ranges(rman_res_t start, rman_res_t end, void *arg) 348 { 349 int *countp; 350 351 countp = arg; 352 (*countp)++; 353 } 354 355 struct alloc_state { 356 struct resource **res; 357 struct pcib_softc *sc; 358 int count, error; 359 }; 360 361 static void 362 alloc_ranges(rman_res_t start, rman_res_t end, void *arg) 363 { 364 struct alloc_state *as; 365 struct pcib_window *w; 366 int rid; 367 368 as = arg; 369 if (as->error != 0) 370 return; 371 372 w = &as->sc->io; 373 rid = w->reg; 374 if (bootverbose) 375 device_printf(as->sc->dev, 376 "allocating non-ISA range %#jx-%#jx\n", start, end); 377 as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT, 378 &rid, start, end, end - start + 1, 0); 379 if (as->res[as->count] == NULL) 380 as->error = ENXIO; 381 else 382 as->count++; 383 } 384 385 static int 386 pcib_alloc_nonisa_ranges(struct pcib_softc *sc, rman_res_t start, rman_res_t end) 387 { 388 struct alloc_state as; 389 int i, new_count; 390 391 /* First, see how many ranges we need. */ 392 new_count = 0; 393 pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count); 394 395 /* Second, allocate the ranges. */ 396 as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF, 397 M_WAITOK); 398 as.sc = sc; 399 as.count = 0; 400 as.error = 0; 401 pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as); 402 if (as.error != 0) { 403 for (i = 0; i < as.count; i++) 404 bus_release_resource(sc->dev, SYS_RES_IOPORT, 405 sc->io.reg, as.res[i]); 406 free(as.res, M_DEVBUF); 407 return (as.error); 408 } 409 KASSERT(as.count == new_count, ("%s: count mismatch", __func__)); 410 411 /* Third, add the ranges to the window. */ 412 pcib_add_window_resources(&sc->io, as.res, as.count); 413 free(as.res, M_DEVBUF); 414 return (0); 415 } 416 417 static void 418 pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type, 419 int flags, pci_addr_t max_address) 420 { 421 struct resource *res; 422 char buf[64]; 423 int error, rid; 424 425 if (max_address != (rman_res_t)max_address) 426 max_address = ~0; 427 w->rman.rm_start = 0; 428 w->rman.rm_end = max_address; 429 w->rman.rm_type = RMAN_ARRAY; 430 snprintf(buf, sizeof(buf), "%s %s window", 431 device_get_nameunit(sc->dev), w->name); 432 w->rman.rm_descr = strdup(buf, M_DEVBUF); 433 error = rman_init(&w->rman); 434 if (error) 435 panic("Failed to initialize %s %s rman", 436 device_get_nameunit(sc->dev), w->name); 437 438 if (!pcib_is_window_open(w)) 439 return; 440 441 if (w->base > max_address || w->limit > max_address) { 442 device_printf(sc->dev, 443 "initial %s window has too many bits, ignoring\n", w->name); 444 return; 445 } 446 if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE) 447 (void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit); 448 else { 449 rid = w->reg; 450 res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit, 451 w->limit - w->base + 1, flags); 452 if (res != NULL) 453 pcib_add_window_resources(w, &res, 1); 454 } 455 if (w->res == NULL) { 456 device_printf(sc->dev, 457 "failed to allocate initial %s window: %#jx-%#jx\n", 458 w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 459 w->base = max_address; 460 w->limit = 0; 461 pcib_write_windows(sc, w->mask); 462 return; 463 } 464 pcib_activate_window(sc, type); 465 } 466 467 /* 468 * Initialize I/O windows. 469 */ 470 static void 471 pcib_probe_windows(struct pcib_softc *sc) 472 { 473 pci_addr_t max; 474 device_t dev; 475 uint32_t val; 476 477 dev = sc->dev; 478 479 if (pci_clear_pcib) { 480 pcib_bridge_init(dev); 481 } 482 483 /* Determine if the I/O port window is implemented. */ 484 val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 485 if (val == 0) { 486 /* 487 * If 'val' is zero, then only 16-bits of I/O space 488 * are supported. 489 */ 490 pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 491 if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) { 492 sc->io.valid = 1; 493 pci_write_config(dev, PCIR_IOBASEL_1, 0, 1); 494 } 495 } else 496 sc->io.valid = 1; 497 498 /* Read the existing I/O port window. */ 499 if (sc->io.valid) { 500 sc->io.reg = PCIR_IOBASEL_1; 501 sc->io.step = 12; 502 sc->io.mask = WIN_IO; 503 sc->io.name = "I/O port"; 504 if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 505 sc->io.base = PCI_PPBIOBASE( 506 pci_read_config(dev, PCIR_IOBASEH_1, 2), val); 507 sc->io.limit = PCI_PPBIOLIMIT( 508 pci_read_config(dev, PCIR_IOLIMITH_1, 2), 509 pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 510 max = 0xffffffff; 511 } else { 512 sc->io.base = PCI_PPBIOBASE(0, val); 513 sc->io.limit = PCI_PPBIOLIMIT(0, 514 pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 515 max = 0xffff; 516 } 517 pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max); 518 } 519 520 /* Read the existing memory window. */ 521 sc->mem.valid = 1; 522 sc->mem.reg = PCIR_MEMBASE_1; 523 sc->mem.step = 20; 524 sc->mem.mask = WIN_MEM; 525 sc->mem.name = "memory"; 526 sc->mem.base = PCI_PPBMEMBASE(0, 527 pci_read_config(dev, PCIR_MEMBASE_1, 2)); 528 sc->mem.limit = PCI_PPBMEMLIMIT(0, 529 pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 530 pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff); 531 532 /* Determine if the prefetchable memory window is implemented. */ 533 val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 534 if (val == 0) { 535 /* 536 * If 'val' is zero, then only 32-bits of memory space 537 * are supported. 538 */ 539 pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 540 if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) { 541 sc->pmem.valid = 1; 542 pci_write_config(dev, PCIR_PMBASEL_1, 0, 2); 543 } 544 } else 545 sc->pmem.valid = 1; 546 547 /* Read the existing prefetchable memory window. */ 548 if (sc->pmem.valid) { 549 sc->pmem.reg = PCIR_PMBASEL_1; 550 sc->pmem.step = 20; 551 sc->pmem.mask = WIN_PMEM; 552 sc->pmem.name = "prefetch"; 553 if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 554 sc->pmem.base = PCI_PPBMEMBASE( 555 pci_read_config(dev, PCIR_PMBASEH_1, 4), val); 556 sc->pmem.limit = PCI_PPBMEMLIMIT( 557 pci_read_config(dev, PCIR_PMLIMITH_1, 4), 558 pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 559 max = 0xffffffffffffffff; 560 } else { 561 sc->pmem.base = PCI_PPBMEMBASE(0, val); 562 sc->pmem.limit = PCI_PPBMEMLIMIT(0, 563 pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 564 max = 0xffffffff; 565 } 566 pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY, 567 RF_PREFETCHABLE, max); 568 } 569 } 570 571 static void 572 pcib_release_window(struct pcib_softc *sc, struct pcib_window *w, int type) 573 { 574 device_t dev; 575 int error, i; 576 577 if (!w->valid) 578 return; 579 580 dev = sc->dev; 581 error = rman_fini(&w->rman); 582 if (error) { 583 device_printf(dev, "failed to release %s rman\n", w->name); 584 return; 585 } 586 free(__DECONST(char *, w->rman.rm_descr), M_DEVBUF); 587 588 for (i = 0; i < w->count; i++) { 589 error = bus_free_resource(dev, type, w->res[i]); 590 if (error) 591 device_printf(dev, 592 "failed to release %s resource: %d\n", w->name, 593 error); 594 } 595 free(w->res, M_DEVBUF); 596 } 597 598 static void 599 pcib_free_windows(struct pcib_softc *sc) 600 { 601 602 pcib_release_window(sc, &sc->pmem, SYS_RES_MEMORY); 603 pcib_release_window(sc, &sc->mem, SYS_RES_MEMORY); 604 pcib_release_window(sc, &sc->io, SYS_RES_IOPORT); 605 } 606 607 #ifdef PCI_RES_BUS 608 /* 609 * Allocate a suitable secondary bus for this bridge if needed and 610 * initialize the resource manager for the secondary bus range. Note 611 * that the minimum count is a desired value and this may allocate a 612 * smaller range. 613 */ 614 void 615 pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count) 616 { 617 char buf[64]; 618 int error, rid, sec_reg; 619 620 switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) { 621 case PCIM_HDRTYPE_BRIDGE: 622 sec_reg = PCIR_SECBUS_1; 623 bus->sub_reg = PCIR_SUBBUS_1; 624 break; 625 case PCIM_HDRTYPE_CARDBUS: 626 sec_reg = PCIR_SECBUS_2; 627 bus->sub_reg = PCIR_SUBBUS_2; 628 break; 629 default: 630 panic("not a PCI bridge"); 631 } 632 bus->sec = pci_read_config(dev, sec_reg, 1); 633 bus->sub = pci_read_config(dev, bus->sub_reg, 1); 634 bus->dev = dev; 635 bus->rman.rm_start = 0; 636 bus->rman.rm_end = PCI_BUSMAX; 637 bus->rman.rm_type = RMAN_ARRAY; 638 snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev)); 639 bus->rman.rm_descr = strdup(buf, M_DEVBUF); 640 error = rman_init(&bus->rman); 641 if (error) 642 panic("Failed to initialize %s bus number rman", 643 device_get_nameunit(dev)); 644 645 /* 646 * Allocate a bus range. This will return an existing bus range 647 * if one exists, or a new bus range if one does not. 648 */ 649 rid = 0; 650 bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid, 651 min_count, 0); 652 if (bus->res == NULL) { 653 /* 654 * Fall back to just allocating a range of a single bus 655 * number. 656 */ 657 bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid, 658 1, 0); 659 } else if (rman_get_size(bus->res) < min_count) 660 /* 661 * Attempt to grow the existing range to satisfy the 662 * minimum desired count. 663 */ 664 (void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res, 665 rman_get_start(bus->res), rman_get_start(bus->res) + 666 min_count - 1); 667 668 /* 669 * Add the initial resource to the rman. 670 */ 671 if (bus->res != NULL) { 672 error = rman_manage_region(&bus->rman, rman_get_start(bus->res), 673 rman_get_end(bus->res)); 674 if (error) 675 panic("Failed to add resource to rman"); 676 bus->sec = rman_get_start(bus->res); 677 bus->sub = rman_get_end(bus->res); 678 } 679 } 680 681 void 682 pcib_free_secbus(device_t dev, struct pcib_secbus *bus) 683 { 684 int error; 685 686 error = rman_fini(&bus->rman); 687 if (error) { 688 device_printf(dev, "failed to release bus number rman\n"); 689 return; 690 } 691 free(__DECONST(char *, bus->rman.rm_descr), M_DEVBUF); 692 693 error = bus_free_resource(dev, PCI_RES_BUS, bus->res); 694 if (error) 695 device_printf(dev, 696 "failed to release bus numbers resource: %d\n", error); 697 } 698 699 static struct resource * 700 pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid, 701 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 702 { 703 struct resource *res; 704 705 res = rman_reserve_resource(&bus->rman, start, end, count, flags, 706 child); 707 if (res == NULL) 708 return (NULL); 709 710 if (bootverbose) 711 device_printf(bus->dev, 712 "allocated bus range (%ju-%ju) for rid %d of %s\n", 713 rman_get_start(res), rman_get_end(res), *rid, 714 pcib_child_name(child)); 715 rman_set_rid(res, *rid); 716 return (res); 717 } 718 719 /* 720 * Attempt to grow the secondary bus range. This is much simpler than 721 * for I/O windows as the range can only be grown by increasing 722 * subbus. 723 */ 724 static int 725 pcib_grow_subbus(struct pcib_secbus *bus, rman_res_t new_end) 726 { 727 rman_res_t old_end; 728 int error; 729 730 old_end = rman_get_end(bus->res); 731 KASSERT(new_end > old_end, ("attempt to shrink subbus")); 732 error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res, 733 rman_get_start(bus->res), new_end); 734 if (error) 735 return (error); 736 if (bootverbose) 737 device_printf(bus->dev, "grew bus range to %ju-%ju\n", 738 rman_get_start(bus->res), rman_get_end(bus->res)); 739 error = rman_manage_region(&bus->rman, old_end + 1, 740 rman_get_end(bus->res)); 741 if (error) 742 panic("Failed to add resource to rman"); 743 bus->sub = rman_get_end(bus->res); 744 pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1); 745 return (0); 746 } 747 748 struct resource * 749 pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid, 750 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 751 { 752 struct resource *res; 753 rman_res_t start_free, end_free, new_end; 754 755 /* 756 * First, see if the request can be satisified by the existing 757 * bus range. 758 */ 759 res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags); 760 if (res != NULL) 761 return (res); 762 763 /* 764 * Figure out a range to grow the bus range. First, find the 765 * first bus number after the last allocated bus in the rman and 766 * enforce that as a minimum starting point for the range. 767 */ 768 if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 || 769 end_free != bus->sub) 770 start_free = bus->sub + 1; 771 if (start_free < start) 772 start_free = start; 773 new_end = start_free + count - 1; 774 775 /* 776 * See if this new range would satisfy the request if it 777 * succeeds. 778 */ 779 if (new_end > end) 780 return (NULL); 781 782 /* Finally, attempt to grow the existing resource. */ 783 if (bootverbose) { 784 device_printf(bus->dev, 785 "attempting to grow bus range for %ju buses\n", count); 786 printf("\tback candidate range: %ju-%ju\n", start_free, 787 new_end); 788 } 789 if (pcib_grow_subbus(bus, new_end) == 0) 790 return (pcib_suballoc_bus(bus, child, rid, start, end, count, 791 flags)); 792 return (NULL); 793 } 794 #endif 795 796 #else 797 798 /* 799 * Is the prefetch window open (eg, can we allocate memory in it?) 800 */ 801 static int 802 pcib_is_prefetch_open(struct pcib_softc *sc) 803 { 804 return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit); 805 } 806 807 /* 808 * Is the nonprefetch window open (eg, can we allocate memory in it?) 809 */ 810 static int 811 pcib_is_nonprefetch_open(struct pcib_softc *sc) 812 { 813 return (sc->membase > 0 && sc->membase < sc->memlimit); 814 } 815 816 /* 817 * Is the io window open (eg, can we allocate ports in it?) 818 */ 819 static int 820 pcib_is_io_open(struct pcib_softc *sc) 821 { 822 return (sc->iobase > 0 && sc->iobase < sc->iolimit); 823 } 824 825 /* 826 * Get current I/O decode. 827 */ 828 static void 829 pcib_get_io_decode(struct pcib_softc *sc) 830 { 831 device_t dev; 832 uint32_t iolow; 833 834 dev = sc->dev; 835 836 iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1); 837 if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 838 sc->iobase = PCI_PPBIOBASE( 839 pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow); 840 else 841 sc->iobase = PCI_PPBIOBASE(0, iolow); 842 843 iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1); 844 if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 845 sc->iolimit = PCI_PPBIOLIMIT( 846 pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow); 847 else 848 sc->iolimit = PCI_PPBIOLIMIT(0, iolow); 849 } 850 851 /* 852 * Get current memory decode. 853 */ 854 static void 855 pcib_get_mem_decode(struct pcib_softc *sc) 856 { 857 device_t dev; 858 pci_addr_t pmemlow; 859 860 dev = sc->dev; 861 862 sc->membase = PCI_PPBMEMBASE(0, 863 pci_read_config(dev, PCIR_MEMBASE_1, 2)); 864 sc->memlimit = PCI_PPBMEMLIMIT(0, 865 pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 866 867 pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2); 868 if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 869 sc->pmembase = PCI_PPBMEMBASE( 870 pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow); 871 else 872 sc->pmembase = PCI_PPBMEMBASE(0, pmemlow); 873 874 pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2); 875 if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 876 sc->pmemlimit = PCI_PPBMEMLIMIT( 877 pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow); 878 else 879 sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow); 880 } 881 882 /* 883 * Restore previous I/O decode. 884 */ 885 static void 886 pcib_set_io_decode(struct pcib_softc *sc) 887 { 888 device_t dev; 889 uint32_t iohi; 890 891 dev = sc->dev; 892 893 iohi = sc->iobase >> 16; 894 if (iohi > 0) 895 pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2); 896 pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1); 897 898 iohi = sc->iolimit >> 16; 899 if (iohi > 0) 900 pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2); 901 pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1); 902 } 903 904 /* 905 * Restore previous memory decode. 906 */ 907 static void 908 pcib_set_mem_decode(struct pcib_softc *sc) 909 { 910 device_t dev; 911 pci_addr_t pmemhi; 912 913 dev = sc->dev; 914 915 pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2); 916 pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2); 917 918 pmemhi = sc->pmembase >> 32; 919 if (pmemhi > 0) 920 pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4); 921 pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2); 922 923 pmemhi = sc->pmemlimit >> 32; 924 if (pmemhi > 0) 925 pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4); 926 pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2); 927 } 928 #endif 929 930 #ifdef PCI_HP 931 /* 932 * PCI-express HotPlug support. 933 */ 934 static int pci_enable_pcie_hp = 1; 935 SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_hp, CTLFLAG_RDTUN, 936 &pci_enable_pcie_hp, 0, 937 "Enable support for native PCI-express HotPlug."); 938 939 TASKQUEUE_DEFINE_THREAD(pci_hp); 940 941 static void 942 pcib_probe_hotplug(struct pcib_softc *sc) 943 { 944 device_t dev; 945 uint32_t link_cap; 946 uint16_t link_sta, slot_sta; 947 948 if (!pci_enable_pcie_hp) 949 return; 950 951 dev = sc->dev; 952 if (pci_find_cap(dev, PCIY_EXPRESS, NULL) != 0) 953 return; 954 955 if (!(pcie_read_config(dev, PCIER_FLAGS, 2) & PCIEM_FLAGS_SLOT)) 956 return; 957 958 sc->pcie_slot_cap = pcie_read_config(dev, PCIER_SLOT_CAP, 4); 959 960 if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_HPC) == 0) 961 return; 962 link_cap = pcie_read_config(dev, PCIER_LINK_CAP, 4); 963 if ((link_cap & PCIEM_LINK_CAP_DL_ACTIVE) == 0) 964 return; 965 966 /* 967 * Some devices report that they have an MRL when they actually 968 * do not. Since they always report that the MRL is open, child 969 * devices would be ignored. Try to detect these devices and 970 * ignore their claim of HotPlug support. 971 * 972 * If there is an open MRL but the Data Link Layer is active, 973 * the MRL is not real. 974 */ 975 if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) != 0) { 976 link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 977 slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 978 if ((slot_sta & PCIEM_SLOT_STA_MRLSS) != 0 && 979 (link_sta & PCIEM_LINK_STA_DL_ACTIVE) != 0) { 980 return; 981 } 982 } 983 984 /* 985 * Now that we're sure we want to do hot plug, ask the 986 * firmware, if any, if that's OK. 987 */ 988 if (pcib_request_feature(dev, PCI_FEATURE_HP) != 0) { 989 if (bootverbose) 990 device_printf(dev, "Unable to activate hot plug feature.\n"); 991 return; 992 } 993 994 sc->flags |= PCIB_HOTPLUG; 995 } 996 997 /* 998 * Send a HotPlug command to the slot control register. If this slot 999 * uses command completion interrupts and a previous command is still 1000 * in progress, then the command is dropped. Once the previous 1001 * command completes or times out, pcib_pcie_hotplug_update() will be 1002 * invoked to post a new command based on the slot's state at that 1003 * time. 1004 */ 1005 static void 1006 pcib_pcie_hotplug_command(struct pcib_softc *sc, uint16_t val, uint16_t mask) 1007 { 1008 device_t dev; 1009 uint16_t ctl, new; 1010 1011 dev = sc->dev; 1012 1013 if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) 1014 return; 1015 1016 ctl = pcie_read_config(dev, PCIER_SLOT_CTL, 2); 1017 new = (ctl & ~mask) | val; 1018 if (new == ctl) 1019 return; 1020 if (bootverbose) 1021 device_printf(dev, "HotPlug command: %04x -> %04x\n", ctl, new); 1022 pcie_write_config(dev, PCIER_SLOT_CTL, new, 2); 1023 if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS) && 1024 (ctl & new) & PCIEM_SLOT_CTL_CCIE) { 1025 sc->flags |= PCIB_HOTPLUG_CMD_PENDING; 1026 if (!cold) 1027 taskqueue_enqueue_timeout(taskqueue_pci_hp, 1028 &sc->pcie_cc_task, hz); 1029 } 1030 } 1031 1032 static void 1033 pcib_pcie_hotplug_command_completed(struct pcib_softc *sc) 1034 { 1035 device_t dev; 1036 1037 dev = sc->dev; 1038 1039 if (bootverbose) 1040 device_printf(dev, "Command Completed\n"); 1041 if (!(sc->flags & PCIB_HOTPLUG_CMD_PENDING)) 1042 return; 1043 taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_cc_task, NULL); 1044 sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 1045 wakeup(sc); 1046 } 1047 1048 /* 1049 * Returns true if a card is fully inserted from the user's 1050 * perspective. It may not yet be ready for access, but the driver 1051 * can now start enabling access if necessary. 1052 */ 1053 static bool 1054 pcib_hotplug_inserted(struct pcib_softc *sc) 1055 { 1056 1057 /* Pretend the card isn't present if a detach is forced. */ 1058 if (sc->flags & PCIB_DETACHING) 1059 return (false); 1060 1061 /* Card must be present in the slot. */ 1062 if ((sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS) == 0) 1063 return (false); 1064 1065 /* A power fault implicitly turns off power to the slot. */ 1066 if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD) 1067 return (false); 1068 1069 /* If the MRL is disengaged, the slot is powered off. */ 1070 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP && 1071 (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS) != 0) 1072 return (false); 1073 1074 return (true); 1075 } 1076 1077 /* 1078 * Returns -1 if the card is fully inserted, powered, and ready for 1079 * access. Otherwise, returns 0. 1080 */ 1081 static int 1082 pcib_hotplug_present(struct pcib_softc *sc) 1083 { 1084 1085 /* Card must be inserted. */ 1086 if (!pcib_hotplug_inserted(sc)) 1087 return (0); 1088 1089 /* Require the Data Link Layer to be active. */ 1090 if (!(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE)) 1091 return (0); 1092 1093 return (-1); 1094 } 1095 1096 static int pci_enable_pcie_ei = 0; 1097 SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_ei, CTLFLAG_RWTUN, 1098 &pci_enable_pcie_ei, 0, 1099 "Enable support for PCI-express Electromechanical Interlock."); 1100 1101 static void 1102 pcib_pcie_hotplug_update(struct pcib_softc *sc, uint16_t val, uint16_t mask, 1103 bool schedule_task) 1104 { 1105 bool card_inserted, ei_engaged; 1106 1107 /* Clear DETACHING if Presence Detect has cleared. */ 1108 if ((sc->pcie_slot_sta & (PCIEM_SLOT_STA_PDC | PCIEM_SLOT_STA_PDS)) == 1109 PCIEM_SLOT_STA_PDC) 1110 sc->flags &= ~PCIB_DETACHING; 1111 1112 card_inserted = pcib_hotplug_inserted(sc); 1113 1114 /* Turn the power indicator on if a card is inserted. */ 1115 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PIP) { 1116 mask |= PCIEM_SLOT_CTL_PIC; 1117 if (card_inserted) 1118 val |= PCIEM_SLOT_CTL_PI_ON; 1119 else if (sc->flags & PCIB_DETACH_PENDING) 1120 val |= PCIEM_SLOT_CTL_PI_BLINK; 1121 else 1122 val |= PCIEM_SLOT_CTL_PI_OFF; 1123 } 1124 1125 /* Turn the power on via the Power Controller if a card is inserted. */ 1126 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) { 1127 mask |= PCIEM_SLOT_CTL_PCC; 1128 if (card_inserted) 1129 val |= PCIEM_SLOT_CTL_PC_ON; 1130 else 1131 val |= PCIEM_SLOT_CTL_PC_OFF; 1132 } 1133 1134 /* 1135 * If a card is inserted, enable the Electromechanical 1136 * Interlock. If a card is not inserted (or we are in the 1137 * process of detaching), disable the Electromechanical 1138 * Interlock. 1139 */ 1140 if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP) && 1141 pci_enable_pcie_ei) { 1142 mask |= PCIEM_SLOT_CTL_EIC; 1143 ei_engaged = (sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS) != 0; 1144 if (card_inserted != ei_engaged) 1145 val |= PCIEM_SLOT_CTL_EIC; 1146 } 1147 1148 /* 1149 * Start a timer to see if the Data Link Layer times out. 1150 * Note that we only start the timer if Presence Detect or MRL Sensor 1151 * changed on this interrupt. Stop any scheduled timer if 1152 * the Data Link Layer is active. 1153 */ 1154 if (card_inserted && 1155 !(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) && 1156 sc->pcie_slot_sta & 1157 (PCIEM_SLOT_STA_MRLSC | PCIEM_SLOT_STA_PDC)) { 1158 if (cold) 1159 device_printf(sc->dev, 1160 "Data Link Layer inactive\n"); 1161 else 1162 taskqueue_enqueue_timeout(taskqueue_pci_hp, 1163 &sc->pcie_dll_task, hz); 1164 } else if (sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) 1165 taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_dll_task, 1166 NULL); 1167 1168 pcib_pcie_hotplug_command(sc, val, mask); 1169 1170 /* 1171 * During attach the child "pci" device is added synchronously; 1172 * otherwise, the task is scheduled to manage the child 1173 * device. 1174 */ 1175 if (schedule_task && 1176 (pcib_hotplug_present(sc) != 0) != (sc->child != NULL)) 1177 taskqueue_enqueue(taskqueue_pci_hp, &sc->pcie_hp_task); 1178 } 1179 1180 static void 1181 pcib_pcie_intr_hotplug(void *arg) 1182 { 1183 struct pcib_softc *sc; 1184 device_t dev; 1185 uint16_t old_slot_sta; 1186 1187 sc = arg; 1188 dev = sc->dev; 1189 PCIB_HP_LOCK(sc); 1190 old_slot_sta = sc->pcie_slot_sta; 1191 sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 1192 1193 /* Clear the events just reported. */ 1194 pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2); 1195 1196 if (bootverbose) 1197 device_printf(dev, "HotPlug interrupt: %#x\n", 1198 sc->pcie_slot_sta); 1199 1200 if (sc->pcie_slot_sta & PCIEM_SLOT_STA_ABP) { 1201 if (sc->flags & PCIB_DETACH_PENDING) { 1202 device_printf(dev, 1203 "Attention Button Pressed: Detach Cancelled\n"); 1204 sc->flags &= ~PCIB_DETACH_PENDING; 1205 taskqueue_cancel_timeout(taskqueue_pci_hp, 1206 &sc->pcie_ab_task, NULL); 1207 } else if (old_slot_sta & PCIEM_SLOT_STA_PDS) { 1208 /* Only initiate detach sequence if device present. */ 1209 device_printf(dev, 1210 "Attention Button Pressed: Detaching in 5 seconds\n"); 1211 sc->flags |= PCIB_DETACH_PENDING; 1212 taskqueue_enqueue_timeout(taskqueue_pci_hp, 1213 &sc->pcie_ab_task, 5 * hz); 1214 } 1215 } 1216 if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD) 1217 device_printf(dev, "Power Fault Detected\n"); 1218 if (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSC) 1219 device_printf(dev, "MRL Sensor Changed to %s\n", 1220 sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS ? "open" : 1221 "closed"); 1222 if (bootverbose && sc->pcie_slot_sta & PCIEM_SLOT_STA_PDC) 1223 device_printf(dev, "Presence Detect Changed to %s\n", 1224 sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS ? "card present" : 1225 "empty"); 1226 if (sc->pcie_slot_sta & PCIEM_SLOT_STA_CC) 1227 pcib_pcie_hotplug_command_completed(sc); 1228 if (sc->pcie_slot_sta & PCIEM_SLOT_STA_DLLSC) { 1229 sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 1230 if (bootverbose) 1231 device_printf(dev, 1232 "Data Link Layer State Changed to %s\n", 1233 sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE ? 1234 "active" : "inactive"); 1235 } 1236 1237 pcib_pcie_hotplug_update(sc, 0, 0, true); 1238 PCIB_HP_UNLOCK(sc); 1239 } 1240 1241 static void 1242 pcib_pcie_hotplug_task(void *context, int pending) 1243 { 1244 struct pcib_softc *sc; 1245 device_t dev; 1246 1247 sc = context; 1248 PCIB_HP_LOCK(sc); 1249 dev = sc->dev; 1250 if (pcib_hotplug_present(sc) != 0) { 1251 if (sc->child == NULL) { 1252 sc->child = device_add_child(dev, "pci", -1); 1253 bus_generic_attach(dev); 1254 } 1255 } else { 1256 if (sc->child != NULL) { 1257 if (device_delete_child(dev, sc->child) == 0) 1258 sc->child = NULL; 1259 } 1260 } 1261 PCIB_HP_UNLOCK(sc); 1262 } 1263 1264 static void 1265 pcib_pcie_ab_timeout(void *arg, int pending) 1266 { 1267 struct pcib_softc *sc = arg; 1268 1269 PCIB_HP_LOCK(sc); 1270 if (sc->flags & PCIB_DETACH_PENDING) { 1271 sc->flags |= PCIB_DETACHING; 1272 sc->flags &= ~PCIB_DETACH_PENDING; 1273 pcib_pcie_hotplug_update(sc, 0, 0, true); 1274 } 1275 PCIB_HP_UNLOCK(sc); 1276 } 1277 1278 static void 1279 pcib_pcie_cc_timeout(void *arg, int pending) 1280 { 1281 struct pcib_softc *sc = arg; 1282 device_t dev = sc->dev; 1283 uint16_t sta; 1284 1285 PCIB_HP_LOCK(sc); 1286 sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 1287 if (!(sta & PCIEM_SLOT_STA_CC)) { 1288 device_printf(dev, "HotPlug Command Timed Out\n"); 1289 sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 1290 } else { 1291 device_printf(dev, 1292 "Missed HotPlug interrupt waiting for Command Completion\n"); 1293 pcib_pcie_intr_hotplug(sc); 1294 } 1295 PCIB_HP_UNLOCK(sc); 1296 } 1297 1298 static void 1299 pcib_pcie_dll_timeout(void *arg, int pending) 1300 { 1301 struct pcib_softc *sc = arg; 1302 device_t dev = sc->dev; 1303 uint16_t sta; 1304 1305 PCIB_HP_LOCK(sc); 1306 sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 1307 if (!(sta & PCIEM_LINK_STA_DL_ACTIVE)) { 1308 device_printf(dev, 1309 "Timed out waiting for Data Link Layer Active\n"); 1310 sc->flags |= PCIB_DETACHING; 1311 pcib_pcie_hotplug_update(sc, 0, 0, true); 1312 } else if (sta != sc->pcie_link_sta) { 1313 device_printf(dev, 1314 "Missed HotPlug interrupt waiting for DLL Active\n"); 1315 pcib_pcie_intr_hotplug(sc); 1316 } 1317 PCIB_HP_UNLOCK(sc); 1318 } 1319 1320 static int 1321 pcib_alloc_pcie_irq(struct pcib_softc *sc) 1322 { 1323 device_t dev; 1324 int count, error, mem_rid, rid; 1325 1326 rid = -1; 1327 dev = sc->dev; 1328 1329 /* 1330 * For simplicity, only use MSI-X if there is a single message. 1331 * To support a device with multiple messages we would have to 1332 * use remap intr if the MSI number is not 0. 1333 */ 1334 count = pci_msix_count(dev); 1335 if (count == 1) { 1336 mem_rid = pci_msix_table_bar(dev); 1337 sc->pcie_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 1338 &mem_rid, RF_ACTIVE); 1339 if (sc->pcie_mem == NULL) { 1340 device_printf(dev, 1341 "Failed to allocate BAR for MSI-X table\n"); 1342 } else { 1343 error = pci_alloc_msix(dev, &count); 1344 if (error == 0) 1345 rid = 1; 1346 } 1347 } 1348 1349 if (rid < 0 && pci_msi_count(dev) > 0) { 1350 count = 1; 1351 error = pci_alloc_msi(dev, &count); 1352 if (error == 0) 1353 rid = 1; 1354 } 1355 1356 if (rid < 0) 1357 rid = 0; 1358 1359 sc->pcie_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1360 RF_ACTIVE | RF_SHAREABLE); 1361 if (sc->pcie_irq == NULL) { 1362 device_printf(dev, 1363 "Failed to allocate interrupt for PCI-e events\n"); 1364 if (rid > 0) 1365 pci_release_msi(dev); 1366 return (ENXIO); 1367 } 1368 1369 error = bus_setup_intr(dev, sc->pcie_irq, INTR_TYPE_MISC|INTR_MPSAFE, 1370 NULL, pcib_pcie_intr_hotplug, sc, &sc->pcie_ihand); 1371 if (error) { 1372 device_printf(dev, "Failed to setup PCI-e interrupt handler\n"); 1373 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->pcie_irq); 1374 if (rid > 0) 1375 pci_release_msi(dev); 1376 return (error); 1377 } 1378 return (0); 1379 } 1380 1381 static int 1382 pcib_release_pcie_irq(struct pcib_softc *sc) 1383 { 1384 device_t dev; 1385 int error; 1386 1387 dev = sc->dev; 1388 error = bus_teardown_intr(dev, sc->pcie_irq, sc->pcie_ihand); 1389 if (error) 1390 return (error); 1391 error = bus_free_resource(dev, SYS_RES_IRQ, sc->pcie_irq); 1392 if (error) 1393 return (error); 1394 error = pci_release_msi(dev); 1395 if (error) 1396 return (error); 1397 if (sc->pcie_mem != NULL) 1398 error = bus_free_resource(dev, SYS_RES_MEMORY, sc->pcie_mem); 1399 return (error); 1400 } 1401 1402 static void 1403 pcib_setup_hotplug(struct pcib_softc *sc) 1404 { 1405 device_t dev; 1406 uint16_t mask, val; 1407 1408 dev = sc->dev; 1409 TASK_INIT(&sc->pcie_hp_task, 0, pcib_pcie_hotplug_task, sc); 1410 TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_ab_task, 0, 1411 pcib_pcie_ab_timeout, sc); 1412 TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_cc_task, 0, 1413 pcib_pcie_cc_timeout, sc); 1414 TIMEOUT_TASK_INIT(taskqueue_pci_hp, &sc->pcie_dll_task, 0, 1415 pcib_pcie_dll_timeout, sc); 1416 sc->pcie_hp_lock = bus_topo_mtx(); 1417 1418 /* Allocate IRQ. */ 1419 if (pcib_alloc_pcie_irq(sc) != 0) 1420 return; 1421 1422 sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2); 1423 sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2); 1424 1425 /* Clear any events previously pending. */ 1426 pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2); 1427 1428 /* Enable HotPlug events. */ 1429 mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | 1430 PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE | 1431 PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE; 1432 val = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | PCIEM_SLOT_CTL_PDCE; 1433 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_APB) 1434 val |= PCIEM_SLOT_CTL_ABPE; 1435 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) 1436 val |= PCIEM_SLOT_CTL_PFDE; 1437 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) 1438 val |= PCIEM_SLOT_CTL_MRLSCE; 1439 if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS)) 1440 val |= PCIEM_SLOT_CTL_CCIE; 1441 1442 /* Turn the attention indicator off. */ 1443 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) { 1444 mask |= PCIEM_SLOT_CTL_AIC; 1445 val |= PCIEM_SLOT_CTL_AI_OFF; 1446 } 1447 1448 pcib_pcie_hotplug_update(sc, val, mask, false); 1449 } 1450 1451 static int 1452 pcib_detach_hotplug(struct pcib_softc *sc) 1453 { 1454 uint16_t mask, val; 1455 int error; 1456 1457 /* Disable the card in the slot and force it to detach. */ 1458 if (sc->flags & PCIB_DETACH_PENDING) { 1459 sc->flags &= ~PCIB_DETACH_PENDING; 1460 taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_ab_task, 1461 NULL); 1462 } 1463 sc->flags |= PCIB_DETACHING; 1464 1465 if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) { 1466 taskqueue_cancel_timeout(taskqueue_pci_hp, &sc->pcie_cc_task, 1467 NULL); 1468 tsleep(sc, 0, "hpcmd", hz); 1469 sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING; 1470 } 1471 1472 /* Disable HotPlug events. */ 1473 mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | 1474 PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE | 1475 PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE; 1476 val = 0; 1477 1478 /* Turn the attention indicator off. */ 1479 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) { 1480 mask |= PCIEM_SLOT_CTL_AIC; 1481 val |= PCIEM_SLOT_CTL_AI_OFF; 1482 } 1483 1484 pcib_pcie_hotplug_update(sc, val, mask, false); 1485 1486 error = pcib_release_pcie_irq(sc); 1487 if (error) 1488 return (error); 1489 taskqueue_drain(taskqueue_pci_hp, &sc->pcie_hp_task); 1490 taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_ab_task); 1491 taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_cc_task); 1492 taskqueue_drain_timeout(taskqueue_pci_hp, &sc->pcie_dll_task); 1493 return (0); 1494 } 1495 #endif 1496 1497 /* 1498 * Get current bridge configuration. 1499 */ 1500 static void 1501 pcib_cfg_save(struct pcib_softc *sc) 1502 { 1503 #ifndef NEW_PCIB 1504 device_t dev; 1505 uint16_t command; 1506 1507 dev = sc->dev; 1508 1509 command = pci_read_config(dev, PCIR_COMMAND, 2); 1510 if (command & PCIM_CMD_PORTEN) 1511 pcib_get_io_decode(sc); 1512 if (command & PCIM_CMD_MEMEN) 1513 pcib_get_mem_decode(sc); 1514 #endif 1515 } 1516 1517 /* 1518 * Restore previous bridge configuration. 1519 */ 1520 static void 1521 pcib_cfg_restore(struct pcib_softc *sc) 1522 { 1523 #ifndef NEW_PCIB 1524 uint16_t command; 1525 #endif 1526 1527 #ifdef NEW_PCIB 1528 pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM); 1529 #else 1530 command = pci_read_config(sc->dev, PCIR_COMMAND, 2); 1531 if (command & PCIM_CMD_PORTEN) 1532 pcib_set_io_decode(sc); 1533 if (command & PCIM_CMD_MEMEN) 1534 pcib_set_mem_decode(sc); 1535 #endif 1536 } 1537 1538 /* 1539 * Generic device interface 1540 */ 1541 static int 1542 pcib_probe(device_t dev) 1543 { 1544 if ((pci_get_class(dev) == PCIC_BRIDGE) && 1545 (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) { 1546 device_set_desc(dev, "PCI-PCI bridge"); 1547 return(-10000); 1548 } 1549 return(ENXIO); 1550 } 1551 1552 void 1553 pcib_attach_common(device_t dev) 1554 { 1555 struct pcib_softc *sc; 1556 struct sysctl_ctx_list *sctx; 1557 struct sysctl_oid *soid; 1558 int comma; 1559 1560 sc = device_get_softc(dev); 1561 sc->dev = dev; 1562 1563 /* 1564 * Get current bridge configuration. 1565 */ 1566 sc->domain = pci_get_domain(dev); 1567 #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 1568 sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1); 1569 sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1); 1570 #endif 1571 sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 1572 pcib_cfg_save(sc); 1573 1574 /* 1575 * The primary bus register should always be the bus of the 1576 * parent. 1577 */ 1578 sc->pribus = pci_get_bus(dev); 1579 pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1); 1580 1581 /* 1582 * Setup sysctl reporting nodes 1583 */ 1584 sctx = device_get_sysctl_ctx(dev); 1585 soid = device_get_sysctl_tree(dev); 1586 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain", 1587 CTLFLAG_RD, &sc->domain, 0, "Domain number"); 1588 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus", 1589 CTLFLAG_RD, &sc->pribus, 0, "Primary bus number"); 1590 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus", 1591 CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number"); 1592 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus", 1593 CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number"); 1594 1595 /* 1596 * Quirk handling. 1597 */ 1598 switch (pci_get_devid(dev)) { 1599 #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 1600 case 0x12258086: /* Intel 82454KX/GX (Orion) */ 1601 { 1602 uint8_t supbus; 1603 1604 supbus = pci_read_config(dev, 0x41, 1); 1605 if (supbus != 0xff) { 1606 sc->bus.sec = supbus + 1; 1607 sc->bus.sub = supbus + 1; 1608 } 1609 break; 1610 } 1611 #endif 1612 1613 /* 1614 * The i82380FB mobile docking controller is a PCI-PCI bridge, 1615 * and it is a subtractive bridge. However, the ProgIf is wrong 1616 * so the normal setting of PCIB_SUBTRACTIVE bit doesn't 1617 * happen. There are also Toshiba and Cavium ThunderX bridges 1618 * that behave this way. 1619 */ 1620 case 0xa002177d: /* Cavium ThunderX */ 1621 case 0x124b8086: /* Intel 82380FB Mobile */ 1622 case 0x060513d7: /* Toshiba ???? */ 1623 sc->flags |= PCIB_SUBTRACTIVE; 1624 break; 1625 1626 #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS)) 1627 /* Compaq R3000 BIOS sets wrong subordinate bus number. */ 1628 case 0x00dd10de: 1629 { 1630 char *cp; 1631 1632 if ((cp = kern_getenv("smbios.planar.maker")) == NULL) 1633 break; 1634 if (strncmp(cp, "Compal", 6) != 0) { 1635 freeenv(cp); 1636 break; 1637 } 1638 freeenv(cp); 1639 if ((cp = kern_getenv("smbios.planar.product")) == NULL) 1640 break; 1641 if (strncmp(cp, "08A0", 4) != 0) { 1642 freeenv(cp); 1643 break; 1644 } 1645 freeenv(cp); 1646 if (sc->bus.sub < 0xa) { 1647 pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1); 1648 sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1); 1649 } 1650 break; 1651 } 1652 #endif 1653 } 1654 1655 if (pci_msi_device_blacklisted(dev)) 1656 sc->flags |= PCIB_DISABLE_MSI; 1657 1658 if (pci_msix_device_blacklisted(dev)) 1659 sc->flags |= PCIB_DISABLE_MSIX; 1660 1661 /* 1662 * Intel 815, 845 and other chipsets say they are PCI-PCI bridges, 1663 * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM, 1664 * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese. 1665 * This means they act as if they were subtractively decoding 1666 * bridges and pass all transactions. Mark them and real ProgIf 1 1667 * parts as subtractive. 1668 */ 1669 if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 || 1670 pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE) 1671 sc->flags |= PCIB_SUBTRACTIVE; 1672 1673 #ifdef PCI_HP 1674 pcib_probe_hotplug(sc); 1675 #endif 1676 #ifdef NEW_PCIB 1677 #ifdef PCI_RES_BUS 1678 pcib_setup_secbus(dev, &sc->bus, 1); 1679 #endif 1680 pcib_probe_windows(sc); 1681 #endif 1682 #ifdef PCI_HP 1683 if (sc->flags & PCIB_HOTPLUG) 1684 pcib_setup_hotplug(sc); 1685 #endif 1686 if (bootverbose) { 1687 device_printf(dev, " domain %d\n", sc->domain); 1688 device_printf(dev, " secondary bus %d\n", sc->bus.sec); 1689 device_printf(dev, " subordinate bus %d\n", sc->bus.sub); 1690 #ifdef NEW_PCIB 1691 if (pcib_is_window_open(&sc->io)) 1692 device_printf(dev, " I/O decode 0x%jx-0x%jx\n", 1693 (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit); 1694 if (pcib_is_window_open(&sc->mem)) 1695 device_printf(dev, " memory decode 0x%jx-0x%jx\n", 1696 (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit); 1697 if (pcib_is_window_open(&sc->pmem)) 1698 device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 1699 (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit); 1700 #else 1701 if (pcib_is_io_open(sc)) 1702 device_printf(dev, " I/O decode 0x%x-0x%x\n", 1703 sc->iobase, sc->iolimit); 1704 if (pcib_is_nonprefetch_open(sc)) 1705 device_printf(dev, " memory decode 0x%jx-0x%jx\n", 1706 (uintmax_t)sc->membase, (uintmax_t)sc->memlimit); 1707 if (pcib_is_prefetch_open(sc)) 1708 device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 1709 (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 1710 #endif 1711 if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) || 1712 sc->flags & PCIB_SUBTRACTIVE) { 1713 device_printf(dev, " special decode "); 1714 comma = 0; 1715 if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) { 1716 printf("ISA"); 1717 comma = 1; 1718 } 1719 if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) { 1720 printf("%sVGA", comma ? ", " : ""); 1721 comma = 1; 1722 } 1723 if (sc->flags & PCIB_SUBTRACTIVE) 1724 printf("%ssubtractive", comma ? ", " : ""); 1725 printf("\n"); 1726 } 1727 } 1728 1729 /* 1730 * Always enable busmastering on bridges so that transactions 1731 * initiated on the secondary bus are passed through to the 1732 * primary bus. 1733 */ 1734 pci_enable_busmaster(dev); 1735 } 1736 1737 #ifdef PCI_HP 1738 static int 1739 pcib_present(struct pcib_softc *sc) 1740 { 1741 1742 if (sc->flags & PCIB_HOTPLUG) 1743 return (pcib_hotplug_present(sc) != 0); 1744 return (1); 1745 } 1746 #endif 1747 1748 int 1749 pcib_attach_child(device_t dev) 1750 { 1751 struct pcib_softc *sc; 1752 1753 sc = device_get_softc(dev); 1754 if (sc->bus.sec == 0) { 1755 /* no secondary bus; we should have fixed this */ 1756 return(0); 1757 } 1758 1759 #ifdef PCI_HP 1760 if (!pcib_present(sc)) { 1761 /* An empty HotPlug slot, so don't add a PCI bus yet. */ 1762 return (0); 1763 } 1764 #endif 1765 1766 sc->child = device_add_child(dev, "pci", -1); 1767 return (bus_generic_attach(dev)); 1768 } 1769 1770 int 1771 pcib_attach(device_t dev) 1772 { 1773 1774 pcib_attach_common(dev); 1775 return (pcib_attach_child(dev)); 1776 } 1777 1778 int 1779 pcib_detach(device_t dev) 1780 { 1781 #if defined(PCI_HP) || defined(NEW_PCIB) 1782 struct pcib_softc *sc; 1783 #endif 1784 int error; 1785 1786 #if defined(PCI_HP) || defined(NEW_PCIB) 1787 sc = device_get_softc(dev); 1788 #endif 1789 error = bus_generic_detach(dev); 1790 if (error) 1791 return (error); 1792 #ifdef PCI_HP 1793 if (sc->flags & PCIB_HOTPLUG) { 1794 error = pcib_detach_hotplug(sc); 1795 if (error) 1796 return (error); 1797 } 1798 #endif 1799 error = device_delete_children(dev); 1800 if (error) 1801 return (error); 1802 #ifdef NEW_PCIB 1803 pcib_free_windows(sc); 1804 #ifdef PCI_RES_BUS 1805 pcib_free_secbus(dev, &sc->bus); 1806 #endif 1807 #endif 1808 return (0); 1809 } 1810 1811 int 1812 pcib_suspend(device_t dev) 1813 { 1814 1815 pcib_cfg_save(device_get_softc(dev)); 1816 return (bus_generic_suspend(dev)); 1817 } 1818 1819 int 1820 pcib_resume(device_t dev) 1821 { 1822 1823 pcib_cfg_restore(device_get_softc(dev)); 1824 1825 /* 1826 * Restore the Command register only after restoring the windows. 1827 * The bridge should not be claiming random windows. 1828 */ 1829 pci_write_config(dev, PCIR_COMMAND, pci_get_cmdreg(dev), 2); 1830 return (bus_generic_resume(dev)); 1831 } 1832 1833 void 1834 pcib_bridge_init(device_t dev) 1835 { 1836 pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 1837 pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2); 1838 pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1); 1839 pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2); 1840 pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2); 1841 pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2); 1842 pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 1843 pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4); 1844 pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2); 1845 pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4); 1846 } 1847 1848 int 1849 pcib_child_present(device_t dev, device_t child) 1850 { 1851 #ifdef PCI_HP 1852 struct pcib_softc *sc = device_get_softc(dev); 1853 int retval; 1854 1855 retval = bus_child_present(dev); 1856 if (retval != 0 && sc->flags & PCIB_HOTPLUG) 1857 retval = pcib_hotplug_present(sc); 1858 return (retval); 1859 #else 1860 return (bus_child_present(dev)); 1861 #endif 1862 } 1863 1864 int 1865 pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 1866 { 1867 struct pcib_softc *sc = device_get_softc(dev); 1868 1869 switch (which) { 1870 case PCIB_IVAR_DOMAIN: 1871 *result = sc->domain; 1872 return(0); 1873 case PCIB_IVAR_BUS: 1874 *result = sc->bus.sec; 1875 return(0); 1876 } 1877 return(ENOENT); 1878 } 1879 1880 int 1881 pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 1882 { 1883 1884 switch (which) { 1885 case PCIB_IVAR_DOMAIN: 1886 return(EINVAL); 1887 case PCIB_IVAR_BUS: 1888 return(EINVAL); 1889 } 1890 return(ENOENT); 1891 } 1892 1893 #ifdef NEW_PCIB 1894 /* 1895 * Attempt to allocate a resource from the existing resources assigned 1896 * to a window. 1897 */ 1898 static struct resource * 1899 pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w, 1900 device_t child, int type, int *rid, rman_res_t start, rman_res_t end, 1901 rman_res_t count, u_int flags) 1902 { 1903 struct resource *res; 1904 1905 if (!pcib_is_window_open(w)) 1906 return (NULL); 1907 1908 res = rman_reserve_resource(&w->rman, start, end, count, 1909 flags & ~RF_ACTIVE, child); 1910 if (res == NULL) 1911 return (NULL); 1912 1913 if (bootverbose) 1914 device_printf(sc->dev, 1915 "allocated %s range (%#jx-%#jx) for rid %x of %s\n", 1916 w->name, rman_get_start(res), rman_get_end(res), *rid, 1917 pcib_child_name(child)); 1918 rman_set_rid(res, *rid); 1919 1920 /* 1921 * If the resource should be active, pass that request up the 1922 * tree. This assumes the parent drivers can handle 1923 * activating sub-allocated resources. 1924 */ 1925 if (flags & RF_ACTIVE) { 1926 if (bus_activate_resource(child, type, *rid, res) != 0) { 1927 rman_release_resource(res); 1928 return (NULL); 1929 } 1930 } 1931 1932 return (res); 1933 } 1934 1935 /* Allocate a fresh resource range for an unconfigured window. */ 1936 static int 1937 pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type, 1938 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 1939 { 1940 struct resource *res; 1941 rman_res_t base, limit, wmask; 1942 int rid; 1943 1944 /* 1945 * If this is an I/O window on a bridge with ISA enable set 1946 * and the start address is below 64k, then try to allocate an 1947 * initial window of 0x1000 bytes long starting at address 1948 * 0xf000 and walking down. Note that if the original request 1949 * was larger than the non-aliased range size of 0x100 our 1950 * caller would have raised the start address up to 64k 1951 * already. 1952 */ 1953 if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 1954 start < 65536) { 1955 for (base = 0xf000; (long)base >= 0; base -= 0x1000) { 1956 limit = base + 0xfff; 1957 1958 /* 1959 * Skip ranges that wouldn't work for the 1960 * original request. Note that the actual 1961 * window that overlaps are the non-alias 1962 * ranges within [base, limit], so this isn't 1963 * quite a simple comparison. 1964 */ 1965 if (start + count > limit - 0x400) 1966 continue; 1967 if (base == 0) { 1968 /* 1969 * The first open region for the window at 1970 * 0 is 0x400-0x4ff. 1971 */ 1972 if (end - count + 1 < 0x400) 1973 continue; 1974 } else { 1975 if (end - count + 1 < base) 1976 continue; 1977 } 1978 1979 if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) { 1980 w->base = base; 1981 w->limit = limit; 1982 return (0); 1983 } 1984 } 1985 return (ENOSPC); 1986 } 1987 1988 wmask = ((rman_res_t)1 << w->step) - 1; 1989 if (RF_ALIGNMENT(flags) < w->step) { 1990 flags &= ~RF_ALIGNMENT_MASK; 1991 flags |= RF_ALIGNMENT_LOG2(w->step); 1992 } 1993 start &= ~wmask; 1994 end |= wmask; 1995 count = roundup2(count, (rman_res_t)1 << w->step); 1996 rid = w->reg; 1997 res = bus_alloc_resource(sc->dev, type, &rid, start, end, count, 1998 flags & ~RF_ACTIVE); 1999 if (res == NULL) 2000 return (ENOSPC); 2001 pcib_add_window_resources(w, &res, 1); 2002 pcib_activate_window(sc, type); 2003 w->base = rman_get_start(res); 2004 w->limit = rman_get_end(res); 2005 return (0); 2006 } 2007 2008 /* Try to expand an existing window to the requested base and limit. */ 2009 static int 2010 pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type, 2011 rman_res_t base, rman_res_t limit) 2012 { 2013 struct resource *res; 2014 int error, i, force_64k_base; 2015 2016 KASSERT(base <= w->base && limit >= w->limit, 2017 ("attempting to shrink window")); 2018 2019 /* 2020 * XXX: pcib_grow_window() doesn't try to do this anyway and 2021 * the error handling for all the edge cases would be tedious. 2022 */ 2023 KASSERT(limit == w->limit || base == w->base, 2024 ("attempting to grow both ends of a window")); 2025 2026 /* 2027 * Yet more special handling for requests to expand an I/O 2028 * window behind an ISA-enabled bridge. Since I/O windows 2029 * have to grow in 0x1000 increments and the end of the 0xffff 2030 * range is an alias, growing a window below 64k will always 2031 * result in allocating new resources and never adjusting an 2032 * existing resource. 2033 */ 2034 if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 2035 (limit <= 65535 || (base <= 65535 && base != w->base))) { 2036 KASSERT(limit == w->limit || limit <= 65535, 2037 ("attempting to grow both ends across 64k ISA alias")); 2038 2039 if (base != w->base) 2040 error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1); 2041 else 2042 error = pcib_alloc_nonisa_ranges(sc, w->limit + 1, 2043 limit); 2044 if (error == 0) { 2045 w->base = base; 2046 w->limit = limit; 2047 } 2048 return (error); 2049 } 2050 2051 /* 2052 * Find the existing resource to adjust. Usually there is only one, 2053 * but for an ISA-enabled bridge we might be growing the I/O window 2054 * above 64k and need to find the existing resource that maps all 2055 * of the area above 64k. 2056 */ 2057 for (i = 0; i < w->count; i++) { 2058 if (rman_get_end(w->res[i]) == w->limit) 2059 break; 2060 } 2061 KASSERT(i != w->count, ("did not find existing resource")); 2062 res = w->res[i]; 2063 2064 /* 2065 * Usually the resource we found should match the window's 2066 * existing range. The one exception is the ISA-enabled case 2067 * mentioned above in which case the resource should start at 2068 * 64k. 2069 */ 2070 if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE && 2071 w->base <= 65535) { 2072 KASSERT(rman_get_start(res) == 65536, 2073 ("existing resource mismatch")); 2074 force_64k_base = 1; 2075 } else { 2076 KASSERT(w->base == rman_get_start(res), 2077 ("existing resource mismatch")); 2078 force_64k_base = 0; 2079 } 2080 2081 error = bus_adjust_resource(sc->dev, type, res, force_64k_base ? 2082 rman_get_start(res) : base, limit); 2083 if (error) 2084 return (error); 2085 2086 /* Add the newly allocated region to the resource manager. */ 2087 if (w->base != base) { 2088 error = rman_manage_region(&w->rman, base, w->base - 1); 2089 w->base = base; 2090 } else { 2091 error = rman_manage_region(&w->rman, w->limit + 1, limit); 2092 w->limit = limit; 2093 } 2094 if (error) { 2095 if (bootverbose) 2096 device_printf(sc->dev, 2097 "failed to expand %s resource manager\n", w->name); 2098 (void)bus_adjust_resource(sc->dev, type, res, force_64k_base ? 2099 rman_get_start(res) : w->base, w->limit); 2100 } 2101 return (error); 2102 } 2103 2104 /* 2105 * Attempt to grow a window to make room for a given resource request. 2106 */ 2107 static int 2108 pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type, 2109 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 2110 { 2111 rman_res_t align, start_free, end_free, front, back, wmask; 2112 int error; 2113 2114 /* 2115 * Clamp the desired resource range to the maximum address 2116 * this window supports. Reject impossible requests. 2117 * 2118 * For I/O port requests behind a bridge with the ISA enable 2119 * bit set, force large allocations to start above 64k. 2120 */ 2121 if (!w->valid) 2122 return (EINVAL); 2123 if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 && 2124 start < 65536) 2125 start = 65536; 2126 if (end > w->rman.rm_end) 2127 end = w->rman.rm_end; 2128 if (start + count - 1 > end || start + count < start) 2129 return (EINVAL); 2130 wmask = ((rman_res_t)1 << w->step) - 1; 2131 2132 /* 2133 * If there is no resource at all, just try to allocate enough 2134 * aligned space for this resource. 2135 */ 2136 if (w->res == NULL) { 2137 error = pcib_alloc_new_window(sc, w, type, start, end, count, 2138 flags); 2139 if (error) { 2140 if (bootverbose) 2141 device_printf(sc->dev, 2142 "failed to allocate initial %s window (%#jx-%#jx,%#jx)\n", 2143 w->name, start, end, count); 2144 return (error); 2145 } 2146 if (bootverbose) 2147 device_printf(sc->dev, 2148 "allocated initial %s window of %#jx-%#jx\n", 2149 w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 2150 goto updatewin; 2151 } 2152 2153 /* 2154 * See if growing the window would help. Compute the minimum 2155 * amount of address space needed on both the front and back 2156 * ends of the existing window to satisfy the allocation. 2157 * 2158 * For each end, build a candidate region adjusting for the 2159 * required alignment, etc. If there is a free region at the 2160 * edge of the window, grow from the inner edge of the free 2161 * region. Otherwise grow from the window boundary. 2162 * 2163 * Growing an I/O window below 64k for a bridge with the ISA 2164 * enable bit doesn't require any special magic as the step 2165 * size of an I/O window (1k) always includes multiple 2166 * non-alias ranges when it is grown in either direction. 2167 * 2168 * XXX: Special case: if w->res is completely empty and the 2169 * request size is larger than w->res, we should find the 2170 * optimal aligned buffer containing w->res and allocate that. 2171 */ 2172 if (bootverbose) 2173 device_printf(sc->dev, 2174 "attempting to grow %s window for (%#jx-%#jx,%#jx)\n", 2175 w->name, start, end, count); 2176 align = (rman_res_t)1 << RF_ALIGNMENT(flags); 2177 if (start < w->base) { 2178 if (rman_first_free_region(&w->rman, &start_free, &end_free) != 2179 0 || start_free != w->base) 2180 end_free = w->base; 2181 if (end_free > end) 2182 end_free = end + 1; 2183 2184 /* Move end_free down until it is properly aligned. */ 2185 end_free &= ~(align - 1); 2186 end_free--; 2187 front = end_free - (count - 1); 2188 2189 /* 2190 * The resource would now be allocated at (front, 2191 * end_free). Ensure that fits in the (start, end) 2192 * bounds. end_free is checked above. If 'front' is 2193 * ok, ensure it is properly aligned for this window. 2194 * Also check for underflow. 2195 */ 2196 if (front >= start && front <= end_free) { 2197 if (bootverbose) 2198 printf("\tfront candidate range: %#jx-%#jx\n", 2199 front, end_free); 2200 front &= ~wmask; 2201 front = w->base - front; 2202 } else 2203 front = 0; 2204 } else 2205 front = 0; 2206 if (end > w->limit) { 2207 if (rman_last_free_region(&w->rman, &start_free, &end_free) != 2208 0 || end_free != w->limit) 2209 start_free = w->limit + 1; 2210 if (start_free < start) 2211 start_free = start; 2212 2213 /* Move start_free up until it is properly aligned. */ 2214 start_free = roundup2(start_free, align); 2215 back = start_free + count - 1; 2216 2217 /* 2218 * The resource would now be allocated at (start_free, 2219 * back). Ensure that fits in the (start, end) 2220 * bounds. start_free is checked above. If 'back' is 2221 * ok, ensure it is properly aligned for this window. 2222 * Also check for overflow. 2223 */ 2224 if (back <= end && start_free <= back) { 2225 if (bootverbose) 2226 printf("\tback candidate range: %#jx-%#jx\n", 2227 start_free, back); 2228 back |= wmask; 2229 back -= w->limit; 2230 } else 2231 back = 0; 2232 } else 2233 back = 0; 2234 2235 /* 2236 * Try to allocate the smallest needed region first. 2237 * If that fails, fall back to the other region. 2238 */ 2239 error = ENOSPC; 2240 while (front != 0 || back != 0) { 2241 if (front != 0 && (front <= back || back == 0)) { 2242 error = pcib_expand_window(sc, w, type, w->base - front, 2243 w->limit); 2244 if (error == 0) 2245 break; 2246 front = 0; 2247 } else { 2248 error = pcib_expand_window(sc, w, type, w->base, 2249 w->limit + back); 2250 if (error == 0) 2251 break; 2252 back = 0; 2253 } 2254 } 2255 2256 if (error) 2257 return (error); 2258 if (bootverbose) 2259 device_printf(sc->dev, "grew %s window to %#jx-%#jx\n", 2260 w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 2261 2262 updatewin: 2263 /* Write the new window. */ 2264 KASSERT((w->base & wmask) == 0, ("start address is not aligned")); 2265 KASSERT((w->limit & wmask) == wmask, ("end address is not aligned")); 2266 pcib_write_windows(sc, w->mask); 2267 return (0); 2268 } 2269 2270 /* 2271 * We have to trap resource allocation requests and ensure that the bridge 2272 * is set up to, or capable of handling them. 2273 */ 2274 struct resource * 2275 pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 2276 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 2277 { 2278 struct pcib_softc *sc; 2279 struct resource *r; 2280 2281 sc = device_get_softc(dev); 2282 2283 /* 2284 * VGA resources are decoded iff the VGA enable bit is set in 2285 * the bridge control register. VGA resources do not fall into 2286 * the resource windows and are passed up to the parent. 2287 */ 2288 if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) || 2289 (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) { 2290 if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) 2291 return (bus_generic_alloc_resource(dev, child, type, 2292 rid, start, end, count, flags)); 2293 else 2294 return (NULL); 2295 } 2296 2297 switch (type) { 2298 #ifdef PCI_RES_BUS 2299 case PCI_RES_BUS: 2300 return (pcib_alloc_subbus(&sc->bus, child, rid, start, end, 2301 count, flags)); 2302 #endif 2303 case SYS_RES_IOPORT: 2304 if (pcib_is_isa_range(sc, start, end, count)) 2305 return (NULL); 2306 r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start, 2307 end, count, flags); 2308 if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 2309 break; 2310 if (pcib_grow_window(sc, &sc->io, type, start, end, count, 2311 flags) == 0) 2312 r = pcib_suballoc_resource(sc, &sc->io, child, type, 2313 rid, start, end, count, flags); 2314 break; 2315 case SYS_RES_MEMORY: 2316 /* 2317 * For prefetchable resources, prefer the prefetchable 2318 * memory window, but fall back to the regular memory 2319 * window if that fails. Try both windows before 2320 * attempting to grow a window in case the firmware 2321 * has used a range in the regular memory window to 2322 * map a prefetchable BAR. 2323 */ 2324 if (flags & RF_PREFETCHABLE) { 2325 r = pcib_suballoc_resource(sc, &sc->pmem, child, type, 2326 rid, start, end, count, flags); 2327 if (r != NULL) 2328 break; 2329 } 2330 r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid, 2331 start, end, count, flags); 2332 if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0) 2333 break; 2334 if (flags & RF_PREFETCHABLE) { 2335 if (pcib_grow_window(sc, &sc->pmem, type, start, end, 2336 count, flags) == 0) { 2337 r = pcib_suballoc_resource(sc, &sc->pmem, child, 2338 type, rid, start, end, count, flags); 2339 if (r != NULL) 2340 break; 2341 } 2342 } 2343 if (pcib_grow_window(sc, &sc->mem, type, start, end, count, 2344 flags & ~RF_PREFETCHABLE) == 0) 2345 r = pcib_suballoc_resource(sc, &sc->mem, child, type, 2346 rid, start, end, count, flags); 2347 break; 2348 default: 2349 return (bus_generic_alloc_resource(dev, child, type, rid, 2350 start, end, count, flags)); 2351 } 2352 2353 /* 2354 * If attempts to suballocate from the window fail but this is a 2355 * subtractive bridge, pass the request up the tree. 2356 */ 2357 if (sc->flags & PCIB_SUBTRACTIVE && r == NULL) 2358 return (bus_generic_alloc_resource(dev, child, type, rid, 2359 start, end, count, flags)); 2360 return (r); 2361 } 2362 2363 int 2364 pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r, 2365 rman_res_t start, rman_res_t end) 2366 { 2367 struct pcib_softc *sc; 2368 struct pcib_window *w; 2369 rman_res_t wmask; 2370 int error; 2371 2372 sc = device_get_softc(bus); 2373 2374 /* 2375 * If the resource wasn't sub-allocated from one of our region 2376 * managers then just pass the request up. 2377 */ 2378 if (!pcib_is_resource_managed(sc, type, r)) 2379 return (bus_generic_adjust_resource(bus, child, type, r, 2380 start, end)); 2381 2382 #ifdef PCI_RES_BUS 2383 if (type == PCI_RES_BUS) { 2384 /* 2385 * If our bus range isn't big enough to grow the sub-allocation 2386 * then we need to grow our bus range. Any request that would 2387 * require us to decrease the start of our own bus range is 2388 * invalid, we can only extend the end; ignore such requests 2389 * and let rman_adjust_resource fail below. 2390 */ 2391 if (start >= sc->bus.sec && end > sc->bus.sub) { 2392 error = pcib_grow_subbus(&sc->bus, end); 2393 if (error != 0) 2394 return (error); 2395 } 2396 } else 2397 #endif 2398 { 2399 /* 2400 * Resource is managed and not a secondary bus number, must 2401 * be from one of our windows. 2402 */ 2403 w = pcib_get_resource_window(sc, type, r); 2404 KASSERT(w != NULL, 2405 ("%s: no window for resource (%#jx-%#jx) type %d", 2406 __func__, rman_get_start(r), rman_get_end(r), type)); 2407 2408 /* 2409 * If our window isn't big enough to grow the sub-allocation 2410 * then we need to expand the window. 2411 */ 2412 if (start < w->base || end > w->limit) { 2413 wmask = ((rman_res_t)1 << w->step) - 1; 2414 error = pcib_expand_window(sc, w, type, 2415 MIN(start & ~wmask, w->base), 2416 MAX(end | wmask, w->limit)); 2417 if (error != 0) 2418 return (error); 2419 if (bootverbose) 2420 device_printf(sc->dev, 2421 "grew %s window to %#jx-%#jx\n", 2422 w->name, (uintmax_t)w->base, 2423 (uintmax_t)w->limit); 2424 pcib_write_windows(sc, w->mask); 2425 } 2426 } 2427 2428 return (rman_adjust_resource(r, start, end)); 2429 } 2430 2431 int 2432 pcib_release_resource(device_t dev, device_t child, int type, int rid, 2433 struct resource *r) 2434 { 2435 struct pcib_softc *sc; 2436 int error; 2437 2438 sc = device_get_softc(dev); 2439 if (pcib_is_resource_managed(sc, type, r)) { 2440 if (rman_get_flags(r) & RF_ACTIVE) { 2441 error = bus_deactivate_resource(child, type, rid, r); 2442 if (error) 2443 return (error); 2444 } 2445 return (rman_release_resource(r)); 2446 } 2447 return (bus_generic_release_resource(dev, child, type, rid, r)); 2448 } 2449 #else 2450 /* 2451 * We have to trap resource allocation requests and ensure that the bridge 2452 * is set up to, or capable of handling them. 2453 */ 2454 struct resource * 2455 pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 2456 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 2457 { 2458 struct pcib_softc *sc = device_get_softc(dev); 2459 const char *name, *suffix; 2460 int ok; 2461 2462 /* 2463 * Fail the allocation for this range if it's not supported. 2464 */ 2465 name = device_get_nameunit(child); 2466 if (name == NULL) { 2467 name = ""; 2468 suffix = ""; 2469 } else 2470 suffix = " "; 2471 switch (type) { 2472 case SYS_RES_IOPORT: 2473 ok = 0; 2474 if (!pcib_is_io_open(sc)) 2475 break; 2476 ok = (start >= sc->iobase && end <= sc->iolimit); 2477 2478 /* 2479 * Make sure we allow access to VGA I/O addresses when the 2480 * bridge has the "VGA Enable" bit set. 2481 */ 2482 if (!ok && pci_is_vga_ioport_range(start, end)) 2483 ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 2484 2485 if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 2486 if (!ok) { 2487 if (start < sc->iobase) 2488 start = sc->iobase; 2489 if (end > sc->iolimit) 2490 end = sc->iolimit; 2491 if (start < end) 2492 ok = 1; 2493 } 2494 } else { 2495 ok = 1; 2496 #if 0 2497 /* 2498 * If we overlap with the subtractive range, then 2499 * pick the upper range to use. 2500 */ 2501 if (start < sc->iolimit && end > sc->iobase) 2502 start = sc->iolimit + 1; 2503 #endif 2504 } 2505 if (end < start) { 2506 device_printf(dev, "ioport: end (%jx) < start (%jx)\n", 2507 end, start); 2508 start = 0; 2509 end = 0; 2510 ok = 0; 2511 } 2512 if (!ok) { 2513 device_printf(dev, "%s%srequested unsupported I/O " 2514 "range 0x%jx-0x%jx (decoding 0x%x-0x%x)\n", 2515 name, suffix, start, end, sc->iobase, sc->iolimit); 2516 return (NULL); 2517 } 2518 if (bootverbose) 2519 device_printf(dev, 2520 "%s%srequested I/O range 0x%jx-0x%jx: in range\n", 2521 name, suffix, start, end); 2522 break; 2523 2524 case SYS_RES_MEMORY: 2525 ok = 0; 2526 if (pcib_is_nonprefetch_open(sc)) 2527 ok = ok || (start >= sc->membase && end <= sc->memlimit); 2528 if (pcib_is_prefetch_open(sc)) 2529 ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit); 2530 2531 /* 2532 * Make sure we allow access to VGA memory addresses when the 2533 * bridge has the "VGA Enable" bit set. 2534 */ 2535 if (!ok && pci_is_vga_memory_range(start, end)) 2536 ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 2537 2538 if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 2539 if (!ok) { 2540 ok = 1; 2541 if (flags & RF_PREFETCHABLE) { 2542 if (pcib_is_prefetch_open(sc)) { 2543 if (start < sc->pmembase) 2544 start = sc->pmembase; 2545 if (end > sc->pmemlimit) 2546 end = sc->pmemlimit; 2547 } else { 2548 ok = 0; 2549 } 2550 } else { /* non-prefetchable */ 2551 if (pcib_is_nonprefetch_open(sc)) { 2552 if (start < sc->membase) 2553 start = sc->membase; 2554 if (end > sc->memlimit) 2555 end = sc->memlimit; 2556 } else { 2557 ok = 0; 2558 } 2559 } 2560 } 2561 } else if (!ok) { 2562 ok = 1; /* subtractive bridge: always ok */ 2563 #if 0 2564 if (pcib_is_nonprefetch_open(sc)) { 2565 if (start < sc->memlimit && end > sc->membase) 2566 start = sc->memlimit + 1; 2567 } 2568 if (pcib_is_prefetch_open(sc)) { 2569 if (start < sc->pmemlimit && end > sc->pmembase) 2570 start = sc->pmemlimit + 1; 2571 } 2572 #endif 2573 } 2574 if (end < start) { 2575 device_printf(dev, "memory: end (%jx) < start (%jx)\n", 2576 end, start); 2577 start = 0; 2578 end = 0; 2579 ok = 0; 2580 } 2581 if (!ok && bootverbose) 2582 device_printf(dev, 2583 "%s%srequested unsupported memory range %#jx-%#jx " 2584 "(decoding %#jx-%#jx, %#jx-%#jx)\n", 2585 name, suffix, start, end, 2586 (uintmax_t)sc->membase, (uintmax_t)sc->memlimit, 2587 (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 2588 if (!ok) 2589 return (NULL); 2590 if (bootverbose) 2591 device_printf(dev,"%s%srequested memory range " 2592 "0x%jx-0x%jx: good\n", 2593 name, suffix, start, end); 2594 break; 2595 2596 default: 2597 break; 2598 } 2599 /* 2600 * Bridge is OK decoding this resource, so pass it up. 2601 */ 2602 return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 2603 count, flags)); 2604 } 2605 #endif 2606 2607 /* 2608 * If ARI is enabled on this downstream port, translate the function number 2609 * to the non-ARI slot/function. The downstream port will convert it back in 2610 * hardware. If ARI is not enabled slot and func are not modified. 2611 */ 2612 static __inline void 2613 pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func) 2614 { 2615 struct pcib_softc *sc; 2616 int ari_func; 2617 2618 sc = device_get_softc(pcib); 2619 ari_func = *func; 2620 2621 if (sc->flags & PCIB_ENABLE_ARI) { 2622 KASSERT(*slot == 0, 2623 ("Non-zero slot number with ARI enabled!")); 2624 *slot = PCIE_ARI_SLOT(ari_func); 2625 *func = PCIE_ARI_FUNC(ari_func); 2626 } 2627 } 2628 2629 static void 2630 pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos) 2631 { 2632 uint32_t ctl2; 2633 2634 ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4); 2635 ctl2 |= PCIEM_CTL2_ARI; 2636 pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4); 2637 2638 sc->flags |= PCIB_ENABLE_ARI; 2639 } 2640 2641 /* 2642 * PCIB interface. 2643 */ 2644 int 2645 pcib_maxslots(device_t dev) 2646 { 2647 #if !defined(__amd64__) && !defined(__i386__) 2648 uint32_t pcie_pos; 2649 uint16_t val; 2650 2651 /* 2652 * If this is a PCIe rootport or downstream switch port, there's only 2653 * one slot permitted. 2654 */ 2655 if (pci_find_cap(dev, PCIY_EXPRESS, &pcie_pos) == 0) { 2656 val = pci_read_config(dev, pcie_pos + PCIER_FLAGS, 2); 2657 val &= PCIEM_FLAGS_TYPE; 2658 if (val == PCIEM_TYPE_ROOT_PORT || 2659 val == PCIEM_TYPE_DOWNSTREAM_PORT) 2660 return (0); 2661 } 2662 #endif 2663 return (PCI_SLOTMAX); 2664 } 2665 2666 static int 2667 pcib_ari_maxslots(device_t dev) 2668 { 2669 struct pcib_softc *sc; 2670 2671 sc = device_get_softc(dev); 2672 2673 if (sc->flags & PCIB_ENABLE_ARI) 2674 return (PCIE_ARI_SLOTMAX); 2675 else 2676 return (pcib_maxslots(dev)); 2677 } 2678 2679 static int 2680 pcib_ari_maxfuncs(device_t dev) 2681 { 2682 struct pcib_softc *sc; 2683 2684 sc = device_get_softc(dev); 2685 2686 if (sc->flags & PCIB_ENABLE_ARI) 2687 return (PCIE_ARI_FUNCMAX); 2688 else 2689 return (PCI_FUNCMAX); 2690 } 2691 2692 static void 2693 pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot, 2694 int *func) 2695 { 2696 struct pcib_softc *sc; 2697 2698 sc = device_get_softc(pcib); 2699 2700 *bus = PCI_RID2BUS(rid); 2701 if (sc->flags & PCIB_ENABLE_ARI) { 2702 *slot = PCIE_ARI_RID2SLOT(rid); 2703 *func = PCIE_ARI_RID2FUNC(rid); 2704 } else { 2705 *slot = PCI_RID2SLOT(rid); 2706 *func = PCI_RID2FUNC(rid); 2707 } 2708 } 2709 2710 /* 2711 * Since we are a child of a PCI bus, its parent must support the pcib interface. 2712 */ 2713 static uint32_t 2714 pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width) 2715 { 2716 #ifdef PCI_HP 2717 struct pcib_softc *sc; 2718 2719 sc = device_get_softc(dev); 2720 if (!pcib_present(sc)) { 2721 switch (width) { 2722 case 2: 2723 return (0xffff); 2724 case 1: 2725 return (0xff); 2726 default: 2727 return (0xffffffff); 2728 } 2729 } 2730 #endif 2731 pcib_xlate_ari(dev, b, &s, &f); 2732 return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, 2733 f, reg, width)); 2734 } 2735 2736 static void 2737 pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width) 2738 { 2739 #ifdef PCI_HP 2740 struct pcib_softc *sc; 2741 2742 sc = device_get_softc(dev); 2743 if (!pcib_present(sc)) 2744 return; 2745 #endif 2746 pcib_xlate_ari(dev, b, &s, &f); 2747 PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, 2748 reg, val, width); 2749 } 2750 2751 /* 2752 * Route an interrupt across a PCI bridge. 2753 */ 2754 int 2755 pcib_route_interrupt(device_t pcib, device_t dev, int pin) 2756 { 2757 device_t bus; 2758 int parent_intpin; 2759 int intnum; 2760 2761 /* 2762 * 2763 * The PCI standard defines a swizzle of the child-side device/intpin to 2764 * the parent-side intpin as follows. 2765 * 2766 * device = device on child bus 2767 * child_intpin = intpin on child bus slot (0-3) 2768 * parent_intpin = intpin on parent bus slot (0-3) 2769 * 2770 * parent_intpin = (device + child_intpin) % 4 2771 */ 2772 parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4; 2773 2774 /* 2775 * Our parent is a PCI bus. Its parent must export the pcib interface 2776 * which includes the ability to route interrupts. 2777 */ 2778 bus = device_get_parent(pcib); 2779 intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1); 2780 if (PCI_INTERRUPT_VALID(intnum) && bootverbose) { 2781 device_printf(pcib, "slot %d INT%c is routed to irq %d\n", 2782 pci_get_slot(dev), 'A' + pin - 1, intnum); 2783 } 2784 return(intnum); 2785 } 2786 2787 /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */ 2788 int 2789 pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs) 2790 { 2791 struct pcib_softc *sc = device_get_softc(pcib); 2792 device_t bus; 2793 2794 if (sc->flags & PCIB_DISABLE_MSI) 2795 return (ENXIO); 2796 bus = device_get_parent(pcib); 2797 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 2798 irqs)); 2799 } 2800 2801 /* Pass request to release MSI/MSI-X messages up to the parent bridge. */ 2802 int 2803 pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs) 2804 { 2805 device_t bus; 2806 2807 bus = device_get_parent(pcib); 2808 return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs)); 2809 } 2810 2811 /* Pass request to alloc an MSI-X message up to the parent bridge. */ 2812 int 2813 pcib_alloc_msix(device_t pcib, device_t dev, int *irq) 2814 { 2815 struct pcib_softc *sc = device_get_softc(pcib); 2816 device_t bus; 2817 2818 if (sc->flags & PCIB_DISABLE_MSIX) 2819 return (ENXIO); 2820 bus = device_get_parent(pcib); 2821 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq)); 2822 } 2823 2824 /* Pass request to release an MSI-X message up to the parent bridge. */ 2825 int 2826 pcib_release_msix(device_t pcib, device_t dev, int irq) 2827 { 2828 device_t bus; 2829 2830 bus = device_get_parent(pcib); 2831 return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq)); 2832 } 2833 2834 /* Pass request to map MSI/MSI-X message up to parent bridge. */ 2835 int 2836 pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, 2837 uint32_t *data) 2838 { 2839 device_t bus; 2840 int error; 2841 2842 bus = device_get_parent(pcib); 2843 error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data); 2844 if (error) 2845 return (error); 2846 2847 pci_ht_map_msi(pcib, *addr); 2848 return (0); 2849 } 2850 2851 /* Pass request for device power state up to parent bridge. */ 2852 int 2853 pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate) 2854 { 2855 device_t bus; 2856 2857 bus = device_get_parent(pcib); 2858 return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate)); 2859 } 2860 2861 static int 2862 pcib_ari_enabled(device_t pcib) 2863 { 2864 struct pcib_softc *sc; 2865 2866 sc = device_get_softc(pcib); 2867 2868 return ((sc->flags & PCIB_ENABLE_ARI) != 0); 2869 } 2870 2871 static int 2872 pcib_ari_get_id(device_t pcib, device_t dev, enum pci_id_type type, 2873 uintptr_t *id) 2874 { 2875 struct pcib_softc *sc; 2876 device_t bus_dev; 2877 uint8_t bus, slot, func; 2878 2879 if (type != PCI_ID_RID) { 2880 bus_dev = device_get_parent(pcib); 2881 return (PCIB_GET_ID(device_get_parent(bus_dev), dev, type, id)); 2882 } 2883 2884 sc = device_get_softc(pcib); 2885 2886 if (sc->flags & PCIB_ENABLE_ARI) { 2887 bus = pci_get_bus(dev); 2888 func = pci_get_function(dev); 2889 2890 *id = (PCI_ARI_RID(bus, func)); 2891 } else { 2892 bus = pci_get_bus(dev); 2893 slot = pci_get_slot(dev); 2894 func = pci_get_function(dev); 2895 2896 *id = (PCI_RID(bus, slot, func)); 2897 } 2898 2899 return (0); 2900 } 2901 2902 /* 2903 * Check that the downstream port (pcib) and the endpoint device (dev) both 2904 * support ARI. If so, enable it and return 0, otherwise return an error. 2905 */ 2906 static int 2907 pcib_try_enable_ari(device_t pcib, device_t dev) 2908 { 2909 struct pcib_softc *sc; 2910 int error; 2911 uint32_t cap2; 2912 int ari_cap_off; 2913 uint32_t ari_ver; 2914 uint32_t pcie_pos; 2915 2916 sc = device_get_softc(pcib); 2917 2918 /* 2919 * ARI is controlled in a register in the PCIe capability structure. 2920 * If the downstream port does not have the PCIe capability structure 2921 * then it does not support ARI. 2922 */ 2923 error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos); 2924 if (error != 0) 2925 return (ENODEV); 2926 2927 /* Check that the PCIe port advertises ARI support. */ 2928 cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4); 2929 if (!(cap2 & PCIEM_CAP2_ARI)) 2930 return (ENODEV); 2931 2932 /* 2933 * Check that the endpoint device advertises ARI support via the ARI 2934 * extended capability structure. 2935 */ 2936 error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off); 2937 if (error != 0) 2938 return (ENODEV); 2939 2940 /* 2941 * Finally, check that the endpoint device supports the same version 2942 * of ARI that we do. 2943 */ 2944 ari_ver = pci_read_config(dev, ari_cap_off, 4); 2945 if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) { 2946 if (bootverbose) 2947 device_printf(pcib, 2948 "Unsupported version of ARI (%d) detected\n", 2949 PCI_EXTCAP_VER(ari_ver)); 2950 2951 return (ENXIO); 2952 } 2953 2954 pcib_enable_ari(sc, pcie_pos); 2955 2956 return (0); 2957 } 2958 2959 int 2960 pcib_request_feature_allow(device_t pcib, device_t dev, 2961 enum pci_feature feature) 2962 { 2963 /* 2964 * No host firmware we have to negotiate with, so we allow 2965 * every valid feature requested. 2966 */ 2967 switch (feature) { 2968 case PCI_FEATURE_AER: 2969 case PCI_FEATURE_HP: 2970 break; 2971 default: 2972 return (EINVAL); 2973 } 2974 2975 return (0); 2976 } 2977 2978 int 2979 pcib_request_feature(device_t dev, enum pci_feature feature) 2980 { 2981 2982 /* 2983 * Invoke PCIB_REQUEST_FEATURE of this bridge first in case 2984 * the firmware overrides the method of PCI-PCI bridges. 2985 */ 2986 return (PCIB_REQUEST_FEATURE(dev, dev, feature)); 2987 } 2988 2989 /* 2990 * Pass the request to use this PCI feature up the tree. Either there's a 2991 * firmware like ACPI that's using this feature that will approve (or deny) the 2992 * request to take it over, or the platform has no such firmware, in which case 2993 * the request will be approved. If the request is approved, the OS is expected 2994 * to make use of the feature or render it harmless. 2995 */ 2996 static int 2997 pcib_request_feature_default(device_t pcib, device_t dev, 2998 enum pci_feature feature) 2999 { 3000 device_t bus; 3001 3002 /* 3003 * Our parent is necessarily a pci bus. Its parent will either be 3004 * another pci bridge (which passes it up) or a host bridge that can 3005 * approve or reject the request. 3006 */ 3007 bus = device_get_parent(pcib); 3008 return (PCIB_REQUEST_FEATURE(device_get_parent(bus), dev, feature)); 3009 } 3010 3011 static int 3012 pcib_reset_child(device_t dev, device_t child, int flags) 3013 { 3014 struct pci_devinfo *pdinfo; 3015 int error; 3016 3017 error = 0; 3018 if (dev == NULL || device_get_parent(child) != dev) 3019 goto out; 3020 error = ENXIO; 3021 if (device_get_devclass(child) != devclass_find("pci")) 3022 goto out; 3023 pdinfo = device_get_ivars(dev); 3024 if (pdinfo->cfg.pcie.pcie_location != 0 && 3025 (pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_DOWNSTREAM_PORT || 3026 pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_ROOT_PORT)) { 3027 error = bus_helper_reset_prepare(child, flags); 3028 if (error == 0) { 3029 error = pcie_link_reset(dev, 3030 pdinfo->cfg.pcie.pcie_location); 3031 /* XXXKIB call _post even if error != 0 ? */ 3032 bus_helper_reset_post(child, flags); 3033 } 3034 } 3035 out: 3036 return (error); 3037 } 3038