1 /*- 2 * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier 3 * Copyright (c) 2000 Michael Smith <msmith@freebsd.org> 4 * Copyright (c) 2000 BSDi 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 /* 35 * PCI:PCI bridge support. 36 */ 37 38 #include <sys/param.h> 39 #include <sys/bus.h> 40 #include <sys/kernel.h> 41 #include <sys/libkern.h> 42 #include <sys/malloc.h> 43 #include <sys/module.h> 44 #include <sys/rman.h> 45 #include <sys/sysctl.h> 46 #include <sys/systm.h> 47 48 #include <machine/bus.h> 49 #include <machine/resource.h> 50 51 #include <dev/pci/pcivar.h> 52 #include <dev/pci/pcireg.h> 53 #include <dev/pci/pci_private.h> 54 #include <dev/pci/pcib_private.h> 55 56 #include "pcib_if.h" 57 58 static int pcib_probe(device_t dev); 59 static int pcib_suspend(device_t dev); 60 static int pcib_resume(device_t dev); 61 static int pcib_power_for_sleep(device_t pcib, device_t dev, 62 int *pstate); 63 64 static device_method_t pcib_methods[] = { 65 /* Device interface */ 66 DEVMETHOD(device_probe, pcib_probe), 67 DEVMETHOD(device_attach, pcib_attach), 68 DEVMETHOD(device_detach, bus_generic_detach), 69 DEVMETHOD(device_shutdown, bus_generic_shutdown), 70 DEVMETHOD(device_suspend, pcib_suspend), 71 DEVMETHOD(device_resume, pcib_resume), 72 73 /* Bus interface */ 74 DEVMETHOD(bus_print_child, bus_generic_print_child), 75 DEVMETHOD(bus_read_ivar, pcib_read_ivar), 76 DEVMETHOD(bus_write_ivar, pcib_write_ivar), 77 DEVMETHOD(bus_alloc_resource, pcib_alloc_resource), 78 #ifdef NEW_PCIB 79 DEVMETHOD(bus_adjust_resource, pcib_adjust_resource), 80 DEVMETHOD(bus_release_resource, pcib_release_resource), 81 #else 82 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 83 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 84 #endif 85 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 86 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 87 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 88 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 89 90 /* pcib interface */ 91 DEVMETHOD(pcib_maxslots, pcib_maxslots), 92 DEVMETHOD(pcib_read_config, pcib_read_config), 93 DEVMETHOD(pcib_write_config, pcib_write_config), 94 DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt), 95 DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi), 96 DEVMETHOD(pcib_release_msi, pcib_release_msi), 97 DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix), 98 DEVMETHOD(pcib_release_msix, pcib_release_msix), 99 DEVMETHOD(pcib_map_msi, pcib_map_msi), 100 DEVMETHOD(pcib_power_for_sleep, pcib_power_for_sleep), 101 102 { 0, 0 } 103 }; 104 105 static devclass_t pcib_devclass; 106 107 DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc)); 108 DRIVER_MODULE(pcib, pci, pcib_driver, pcib_devclass, 0, 0); 109 110 #ifdef NEW_PCIB 111 /* 112 * XXX Todo: 113 * - properly handle the ISA enable bit. If it is set, we should change 114 * the behavior of the I/O window resource and rman to not allocate the 115 * blocked ranges (upper 768 bytes of each 1K in the first 64k of the 116 * I/O port address space). 117 */ 118 119 /* 120 * Is a resource from a child device sub-allocated from one of our 121 * resource managers? 122 */ 123 static int 124 pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r) 125 { 126 127 switch (type) { 128 case SYS_RES_IOPORT: 129 return (rman_is_region_manager(r, &sc->io.rman)); 130 case SYS_RES_MEMORY: 131 /* Prefetchable resources may live in either memory rman. */ 132 if (rman_get_flags(r) & RF_PREFETCHABLE && 133 rman_is_region_manager(r, &sc->pmem.rman)) 134 return (1); 135 return (rman_is_region_manager(r, &sc->mem.rman)); 136 } 137 return (0); 138 } 139 140 static int 141 pcib_is_window_open(struct pcib_window *pw) 142 { 143 144 return (pw->valid && pw->base < pw->limit); 145 } 146 147 /* 148 * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and 149 * handle for the resource, we could pass RF_ACTIVE up to the PCI bus 150 * when allocating the resource windows and rely on the PCI bus driver 151 * to do this for us. 152 */ 153 static void 154 pcib_activate_window(struct pcib_softc *sc, int type) 155 { 156 157 PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type); 158 } 159 160 static void 161 pcib_write_windows(struct pcib_softc *sc, int mask) 162 { 163 device_t dev; 164 uint32_t val; 165 166 dev = sc->dev; 167 if (sc->io.valid && mask & WIN_IO) { 168 val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 169 if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 170 pci_write_config(dev, PCIR_IOBASEH_1, 171 sc->io.base >> 16, 2); 172 pci_write_config(dev, PCIR_IOLIMITH_1, 173 sc->io.limit >> 16, 2); 174 } 175 pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1); 176 pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1); 177 } 178 179 if (mask & WIN_MEM) { 180 pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2); 181 pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2); 182 } 183 184 if (sc->pmem.valid && mask & WIN_PMEM) { 185 val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 186 if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 187 pci_write_config(dev, PCIR_PMBASEH_1, 188 sc->pmem.base >> 32, 4); 189 pci_write_config(dev, PCIR_PMLIMITH_1, 190 sc->pmem.limit >> 32, 4); 191 } 192 pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2); 193 pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2); 194 } 195 } 196 197 static void 198 pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type, 199 int flags, pci_addr_t max_address) 200 { 201 char buf[64]; 202 int error, rid; 203 204 if (max_address != (u_long)max_address) 205 max_address = ~0ul; 206 w->rman.rm_start = 0; 207 w->rman.rm_end = max_address; 208 w->rman.rm_type = RMAN_ARRAY; 209 snprintf(buf, sizeof(buf), "%s %s window", 210 device_get_nameunit(sc->dev), w->name); 211 w->rman.rm_descr = strdup(buf, M_DEVBUF); 212 error = rman_init(&w->rman); 213 if (error) 214 panic("Failed to initialize %s %s rman", 215 device_get_nameunit(sc->dev), w->name); 216 217 if (!pcib_is_window_open(w)) 218 return; 219 220 if (w->base > max_address || w->limit > max_address) { 221 device_printf(sc->dev, 222 "initial %s window has too many bits, ignoring\n", w->name); 223 return; 224 } 225 rid = w->reg; 226 w->res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit, 227 w->limit - w->base + 1, flags); 228 if (w->res == NULL) { 229 device_printf(sc->dev, 230 "failed to allocate initial %s window: %#jx-%#jx\n", 231 w->name, (uintmax_t)w->base, (uintmax_t)w->limit); 232 w->base = max_address; 233 w->limit = 0; 234 pcib_write_windows(sc, w->mask); 235 return; 236 } 237 pcib_activate_window(sc, type); 238 239 error = rman_manage_region(&w->rman, rman_get_start(w->res), 240 rman_get_end(w->res)); 241 if (error) 242 panic("Failed to initialize rman with resource"); 243 } 244 245 /* 246 * Initialize I/O windows. 247 */ 248 static void 249 pcib_probe_windows(struct pcib_softc *sc) 250 { 251 pci_addr_t max; 252 device_t dev; 253 uint32_t val; 254 255 dev = sc->dev; 256 257 /* Determine if the I/O port window is implemented. */ 258 val = pci_read_config(dev, PCIR_IOBASEL_1, 1); 259 if (val == 0) { 260 /* 261 * If 'val' is zero, then only 16-bits of I/O space 262 * are supported. 263 */ 264 pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1); 265 if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) { 266 sc->io.valid = 1; 267 pci_write_config(dev, PCIR_IOBASEL_1, 0, 1); 268 } 269 } else 270 sc->io.valid = 1; 271 272 /* Read the existing I/O port window. */ 273 if (sc->io.valid) { 274 sc->io.reg = PCIR_IOBASEL_1; 275 sc->io.step = 12; 276 sc->io.mask = WIN_IO; 277 sc->io.name = "I/O port"; 278 if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) { 279 sc->io.base = PCI_PPBIOBASE( 280 pci_read_config(dev, PCIR_IOBASEH_1, 2), val); 281 sc->io.limit = PCI_PPBIOLIMIT( 282 pci_read_config(dev, PCIR_IOLIMITH_1, 2), 283 pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 284 max = 0xffffffff; 285 } else { 286 sc->io.base = PCI_PPBIOBASE(0, val); 287 sc->io.limit = PCI_PPBIOLIMIT(0, 288 pci_read_config(dev, PCIR_IOLIMITL_1, 1)); 289 max = 0xffff; 290 } 291 pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max); 292 } 293 294 /* Read the existing memory window. */ 295 sc->mem.valid = 1; 296 sc->mem.reg = PCIR_MEMBASE_1; 297 sc->mem.step = 20; 298 sc->mem.mask = WIN_MEM; 299 sc->mem.name = "memory"; 300 sc->mem.base = PCI_PPBMEMBASE(0, 301 pci_read_config(dev, PCIR_MEMBASE_1, 2)); 302 sc->mem.limit = PCI_PPBMEMLIMIT(0, 303 pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 304 pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff); 305 306 /* Determine if the prefetchable memory window is implemented. */ 307 val = pci_read_config(dev, PCIR_PMBASEL_1, 2); 308 if (val == 0) { 309 /* 310 * If 'val' is zero, then only 32-bits of memory space 311 * are supported. 312 */ 313 pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2); 314 if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) { 315 sc->pmem.valid = 1; 316 pci_write_config(dev, PCIR_PMBASEL_1, 0, 2); 317 } 318 } else 319 sc->pmem.valid = 1; 320 321 /* Read the existing prefetchable memory window. */ 322 if (sc->pmem.valid) { 323 sc->pmem.reg = PCIR_PMBASEL_1; 324 sc->pmem.step = 20; 325 sc->pmem.mask = WIN_PMEM; 326 sc->pmem.name = "prefetch"; 327 if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) { 328 sc->pmem.base = PCI_PPBMEMBASE( 329 pci_read_config(dev, PCIR_PMBASEH_1, 4), val); 330 sc->pmem.limit = PCI_PPBMEMLIMIT( 331 pci_read_config(dev, PCIR_PMLIMITH_1, 4), 332 pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 333 max = 0xffffffffffffffff; 334 } else { 335 sc->pmem.base = PCI_PPBMEMBASE(0, val); 336 sc->pmem.limit = PCI_PPBMEMLIMIT(0, 337 pci_read_config(dev, PCIR_PMLIMITL_1, 2)); 338 max = 0xffffffff; 339 } 340 pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY, 341 RF_PREFETCHABLE, max); 342 } 343 } 344 345 #else 346 347 /* 348 * Is the prefetch window open (eg, can we allocate memory in it?) 349 */ 350 static int 351 pcib_is_prefetch_open(struct pcib_softc *sc) 352 { 353 return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit); 354 } 355 356 /* 357 * Is the nonprefetch window open (eg, can we allocate memory in it?) 358 */ 359 static int 360 pcib_is_nonprefetch_open(struct pcib_softc *sc) 361 { 362 return (sc->membase > 0 && sc->membase < sc->memlimit); 363 } 364 365 /* 366 * Is the io window open (eg, can we allocate ports in it?) 367 */ 368 static int 369 pcib_is_io_open(struct pcib_softc *sc) 370 { 371 return (sc->iobase > 0 && sc->iobase < sc->iolimit); 372 } 373 374 /* 375 * Get current I/O decode. 376 */ 377 static void 378 pcib_get_io_decode(struct pcib_softc *sc) 379 { 380 device_t dev; 381 uint32_t iolow; 382 383 dev = sc->dev; 384 385 iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1); 386 if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 387 sc->iobase = PCI_PPBIOBASE( 388 pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow); 389 else 390 sc->iobase = PCI_PPBIOBASE(0, iolow); 391 392 iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1); 393 if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32) 394 sc->iolimit = PCI_PPBIOLIMIT( 395 pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow); 396 else 397 sc->iolimit = PCI_PPBIOLIMIT(0, iolow); 398 } 399 400 /* 401 * Get current memory decode. 402 */ 403 static void 404 pcib_get_mem_decode(struct pcib_softc *sc) 405 { 406 device_t dev; 407 pci_addr_t pmemlow; 408 409 dev = sc->dev; 410 411 sc->membase = PCI_PPBMEMBASE(0, 412 pci_read_config(dev, PCIR_MEMBASE_1, 2)); 413 sc->memlimit = PCI_PPBMEMLIMIT(0, 414 pci_read_config(dev, PCIR_MEMLIMIT_1, 2)); 415 416 pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2); 417 if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 418 sc->pmembase = PCI_PPBMEMBASE( 419 pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow); 420 else 421 sc->pmembase = PCI_PPBMEMBASE(0, pmemlow); 422 423 pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2); 424 if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64) 425 sc->pmemlimit = PCI_PPBMEMLIMIT( 426 pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow); 427 else 428 sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow); 429 } 430 431 /* 432 * Restore previous I/O decode. 433 */ 434 static void 435 pcib_set_io_decode(struct pcib_softc *sc) 436 { 437 device_t dev; 438 uint32_t iohi; 439 440 dev = sc->dev; 441 442 iohi = sc->iobase >> 16; 443 if (iohi > 0) 444 pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2); 445 pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1); 446 447 iohi = sc->iolimit >> 16; 448 if (iohi > 0) 449 pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2); 450 pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1); 451 } 452 453 /* 454 * Restore previous memory decode. 455 */ 456 static void 457 pcib_set_mem_decode(struct pcib_softc *sc) 458 { 459 device_t dev; 460 pci_addr_t pmemhi; 461 462 dev = sc->dev; 463 464 pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2); 465 pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2); 466 467 pmemhi = sc->pmembase >> 32; 468 if (pmemhi > 0) 469 pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4); 470 pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2); 471 472 pmemhi = sc->pmemlimit >> 32; 473 if (pmemhi > 0) 474 pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4); 475 pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2); 476 } 477 #endif 478 479 /* 480 * Get current bridge configuration. 481 */ 482 static void 483 pcib_cfg_save(struct pcib_softc *sc) 484 { 485 device_t dev; 486 487 dev = sc->dev; 488 489 sc->command = pci_read_config(dev, PCIR_COMMAND, 2); 490 sc->pribus = pci_read_config(dev, PCIR_PRIBUS_1, 1); 491 sc->secbus = pci_read_config(dev, PCIR_SECBUS_1, 1); 492 sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1); 493 sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2); 494 sc->seclat = pci_read_config(dev, PCIR_SECLAT_1, 1); 495 #ifndef NEW_PCIB 496 if (sc->command & PCIM_CMD_PORTEN) 497 pcib_get_io_decode(sc); 498 if (sc->command & PCIM_CMD_MEMEN) 499 pcib_get_mem_decode(sc); 500 #endif 501 } 502 503 /* 504 * Restore previous bridge configuration. 505 */ 506 static void 507 pcib_cfg_restore(struct pcib_softc *sc) 508 { 509 device_t dev; 510 511 dev = sc->dev; 512 513 pci_write_config(dev, PCIR_COMMAND, sc->command, 2); 514 pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1); 515 pci_write_config(dev, PCIR_SECBUS_1, sc->secbus, 1); 516 pci_write_config(dev, PCIR_SUBBUS_1, sc->subbus, 1); 517 pci_write_config(dev, PCIR_BRIDGECTL_1, sc->bridgectl, 2); 518 pci_write_config(dev, PCIR_SECLAT_1, sc->seclat, 1); 519 #ifdef NEW_PCIB 520 pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM); 521 #else 522 if (sc->command & PCIM_CMD_PORTEN) 523 pcib_set_io_decode(sc); 524 if (sc->command & PCIM_CMD_MEMEN) 525 pcib_set_mem_decode(sc); 526 #endif 527 } 528 529 /* 530 * Generic device interface 531 */ 532 static int 533 pcib_probe(device_t dev) 534 { 535 if ((pci_get_class(dev) == PCIC_BRIDGE) && 536 (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) { 537 device_set_desc(dev, "PCI-PCI bridge"); 538 return(-10000); 539 } 540 return(ENXIO); 541 } 542 543 void 544 pcib_attach_common(device_t dev) 545 { 546 struct pcib_softc *sc; 547 struct sysctl_ctx_list *sctx; 548 struct sysctl_oid *soid; 549 550 sc = device_get_softc(dev); 551 sc->dev = dev; 552 553 /* 554 * Get current bridge configuration. 555 */ 556 sc->domain = pci_get_domain(dev); 557 sc->secstat = pci_read_config(dev, PCIR_SECSTAT_1, 2); 558 pcib_cfg_save(sc); 559 560 /* 561 * Setup sysctl reporting nodes 562 */ 563 sctx = device_get_sysctl_ctx(dev); 564 soid = device_get_sysctl_tree(dev); 565 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain", 566 CTLFLAG_RD, &sc->domain, 0, "Domain number"); 567 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus", 568 CTLFLAG_RD, &sc->pribus, 0, "Primary bus number"); 569 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus", 570 CTLFLAG_RD, &sc->secbus, 0, "Secondary bus number"); 571 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus", 572 CTLFLAG_RD, &sc->subbus, 0, "Subordinate bus number"); 573 574 /* 575 * Quirk handling. 576 */ 577 switch (pci_get_devid(dev)) { 578 case 0x12258086: /* Intel 82454KX/GX (Orion) */ 579 { 580 uint8_t supbus; 581 582 supbus = pci_read_config(dev, 0x41, 1); 583 if (supbus != 0xff) { 584 sc->secbus = supbus + 1; 585 sc->subbus = supbus + 1; 586 } 587 break; 588 } 589 590 /* 591 * The i82380FB mobile docking controller is a PCI-PCI bridge, 592 * and it is a subtractive bridge. However, the ProgIf is wrong 593 * so the normal setting of PCIB_SUBTRACTIVE bit doesn't 594 * happen. There's also a Toshiba bridge that behaves this 595 * way. 596 */ 597 case 0x124b8086: /* Intel 82380FB Mobile */ 598 case 0x060513d7: /* Toshiba ???? */ 599 sc->flags |= PCIB_SUBTRACTIVE; 600 break; 601 602 /* Compaq R3000 BIOS sets wrong subordinate bus number. */ 603 case 0x00dd10de: 604 { 605 char *cp; 606 607 if ((cp = getenv("smbios.planar.maker")) == NULL) 608 break; 609 if (strncmp(cp, "Compal", 6) != 0) { 610 freeenv(cp); 611 break; 612 } 613 freeenv(cp); 614 if ((cp = getenv("smbios.planar.product")) == NULL) 615 break; 616 if (strncmp(cp, "08A0", 4) != 0) { 617 freeenv(cp); 618 break; 619 } 620 freeenv(cp); 621 if (sc->subbus < 0xa) { 622 pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1); 623 sc->subbus = pci_read_config(dev, PCIR_SUBBUS_1, 1); 624 } 625 break; 626 } 627 } 628 629 if (pci_msi_device_blacklisted(dev)) 630 sc->flags |= PCIB_DISABLE_MSI; 631 632 /* 633 * Intel 815, 845 and other chipsets say they are PCI-PCI bridges, 634 * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM, 635 * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese. 636 * This means they act as if they were subtractively decoding 637 * bridges and pass all transactions. Mark them and real ProgIf 1 638 * parts as subtractive. 639 */ 640 if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 || 641 pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE) 642 sc->flags |= PCIB_SUBTRACTIVE; 643 644 #ifdef NEW_PCIB 645 pcib_probe_windows(sc); 646 #endif 647 if (bootverbose) { 648 device_printf(dev, " domain %d\n", sc->domain); 649 device_printf(dev, " secondary bus %d\n", sc->secbus); 650 device_printf(dev, " subordinate bus %d\n", sc->subbus); 651 #ifdef NEW_PCIB 652 if (pcib_is_window_open(&sc->io)) 653 device_printf(dev, " I/O decode 0x%jx-0x%jx\n", 654 (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit); 655 if (pcib_is_window_open(&sc->mem)) 656 device_printf(dev, " memory decode 0x%jx-0x%jx\n", 657 (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit); 658 if (pcib_is_window_open(&sc->pmem)) 659 device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 660 (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit); 661 #else 662 if (pcib_is_io_open(sc)) 663 device_printf(dev, " I/O decode 0x%x-0x%x\n", 664 sc->iobase, sc->iolimit); 665 if (pcib_is_nonprefetch_open(sc)) 666 device_printf(dev, " memory decode 0x%jx-0x%jx\n", 667 (uintmax_t)sc->membase, (uintmax_t)sc->memlimit); 668 if (pcib_is_prefetch_open(sc)) 669 device_printf(dev, " prefetched decode 0x%jx-0x%jx\n", 670 (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 671 #endif 672 else 673 device_printf(dev, " no prefetched decode\n"); 674 if (sc->flags & PCIB_SUBTRACTIVE) 675 device_printf(dev, " Subtractively decoded bridge.\n"); 676 } 677 678 /* 679 * XXX If the secondary bus number is zero, we should assign a bus number 680 * since the BIOS hasn't, then initialise the bridge. A simple 681 * bus_alloc_resource with the a couple of busses seems like the right 682 * approach, but we don't know what busses the BIOS might have already 683 * assigned to other bridges on this bus that probe later than we do. 684 * 685 * If the subordinate bus number is less than the secondary bus number, 686 * we should pick a better value. One sensible alternative would be to 687 * pick 255; the only tradeoff here is that configuration transactions 688 * would be more widely routed than absolutely necessary. We could 689 * then do a walk of the tree later and fix it. 690 */ 691 } 692 693 int 694 pcib_attach(device_t dev) 695 { 696 struct pcib_softc *sc; 697 device_t child; 698 699 pcib_attach_common(dev); 700 sc = device_get_softc(dev); 701 if (sc->secbus != 0) { 702 child = device_add_child(dev, "pci", sc->secbus); 703 if (child != NULL) 704 return(bus_generic_attach(dev)); 705 } 706 707 /* no secondary bus; we should have fixed this */ 708 return(0); 709 } 710 711 int 712 pcib_suspend(device_t dev) 713 { 714 device_t pcib; 715 int dstate, error; 716 717 pcib_cfg_save(device_get_softc(dev)); 718 error = bus_generic_suspend(dev); 719 if (error == 0 && pci_do_power_suspend) { 720 dstate = PCI_POWERSTATE_D3; 721 pcib = device_get_parent(device_get_parent(dev)); 722 if (PCIB_POWER_FOR_SLEEP(pcib, dev, &dstate) == 0) 723 pci_set_powerstate(dev, dstate); 724 } 725 return (error); 726 } 727 728 int 729 pcib_resume(device_t dev) 730 { 731 device_t pcib; 732 733 if (pci_do_power_resume) { 734 pcib = device_get_parent(device_get_parent(dev)); 735 if (PCIB_POWER_FOR_SLEEP(pcib, dev, NULL) == 0) 736 pci_set_powerstate(dev, PCI_POWERSTATE_D0); 737 } 738 pcib_cfg_restore(device_get_softc(dev)); 739 return (bus_generic_resume(dev)); 740 } 741 742 int 743 pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 744 { 745 struct pcib_softc *sc = device_get_softc(dev); 746 747 switch (which) { 748 case PCIB_IVAR_DOMAIN: 749 *result = sc->domain; 750 return(0); 751 case PCIB_IVAR_BUS: 752 *result = sc->secbus; 753 return(0); 754 } 755 return(ENOENT); 756 } 757 758 int 759 pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value) 760 { 761 struct pcib_softc *sc = device_get_softc(dev); 762 763 switch (which) { 764 case PCIB_IVAR_DOMAIN: 765 return(EINVAL); 766 case PCIB_IVAR_BUS: 767 sc->secbus = value; 768 return(0); 769 } 770 return(ENOENT); 771 } 772 773 #ifdef NEW_PCIB 774 static const char * 775 pcib_child_name(device_t child) 776 { 777 static char buf[64]; 778 779 if (device_get_nameunit(child) != NULL) 780 return (device_get_nameunit(child)); 781 snprintf(buf, sizeof(buf), "pci%d:%d:%d:%d", pci_get_domain(child), 782 pci_get_bus(child), pci_get_slot(child), pci_get_function(child)); 783 return (buf); 784 } 785 786 /* 787 * Attempt to allocate a resource from the existing resources assigned 788 * to a window. 789 */ 790 static struct resource * 791 pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w, 792 device_t child, int type, int *rid, u_long start, u_long end, u_long count, 793 u_int flags) 794 { 795 struct resource *res; 796 797 if (!pcib_is_window_open(w)) 798 return (NULL); 799 800 res = rman_reserve_resource(&w->rman, start, end, count, 801 flags & ~RF_ACTIVE, child); 802 if (res == NULL) 803 return (NULL); 804 805 if (bootverbose) 806 device_printf(sc->dev, 807 "allocated %s range (%#lx-%#lx) for rid %x of %s\n", 808 w->name, rman_get_start(res), rman_get_end(res), *rid, 809 pcib_child_name(child)); 810 rman_set_rid(res, *rid); 811 812 /* 813 * If the resource should be active, pass that request up the 814 * tree. This assumes the parent drivers can handle 815 * activating sub-allocated resources. 816 */ 817 if (flags & RF_ACTIVE) { 818 if (bus_activate_resource(child, type, *rid, res) != 0) { 819 rman_release_resource(res); 820 return (NULL); 821 } 822 } 823 824 return (res); 825 } 826 827 /* 828 * Attempt to grow a window to make room for a given resource request. 829 * The 'step' parameter is log_2 of the desired I/O window's alignment. 830 */ 831 static int 832 pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type, 833 u_long start, u_long end, u_long count, u_int flags) 834 { 835 u_long align, start_free, end_free, front, back; 836 int error, rid; 837 838 /* 839 * Clamp the desired resource range to the maximum address 840 * this window supports. Reject impossible requests. 841 */ 842 if (!w->valid) 843 return (EINVAL); 844 if (end > w->rman.rm_end) 845 end = w->rman.rm_end; 846 if (start + count - 1 > end || start + count < start) 847 return (EINVAL); 848 849 /* 850 * If there is no resource at all, just try to allocate enough 851 * aligned space for this resource. 852 */ 853 if (w->res == NULL) { 854 if (RF_ALIGNMENT(flags) < w->step) { 855 flags &= ~RF_ALIGNMENT_MASK; 856 flags |= RF_ALIGNMENT_LOG2(w->step); 857 } 858 start &= ~((1ul << w->step) - 1); 859 end |= ((1ul << w->step) - 1); 860 count = roundup2(count, 1ul << w->step); 861 rid = w->reg; 862 w->res = bus_alloc_resource(sc->dev, type, &rid, start, end, 863 count, flags & ~RF_ACTIVE); 864 if (w->res == NULL) { 865 if (bootverbose) 866 device_printf(sc->dev, 867 "failed to allocate initial %s window (%#lx-%#lx,%#lx)\n", 868 w->name, start, end, count); 869 return (ENXIO); 870 } 871 if (bootverbose) 872 device_printf(sc->dev, 873 "allocated initial %s window of %#lx-%#lx\n", 874 w->name, rman_get_start(w->res), 875 rman_get_end(w->res)); 876 error = rman_manage_region(&w->rman, rman_get_start(w->res), 877 rman_get_end(w->res)); 878 if (error) { 879 if (bootverbose) 880 device_printf(sc->dev, 881 "failed to add initial %s window to rman\n", 882 w->name); 883 bus_release_resource(sc->dev, type, w->reg, w->res); 884 w->res = NULL; 885 return (error); 886 } 887 pcib_activate_window(sc, type); 888 goto updatewin; 889 } 890 891 /* 892 * See if growing the window would help. Compute the minimum 893 * amount of address space needed on both the front and back 894 * ends of the existing window to satisfy the allocation. 895 * 896 * For each end, build a candidate region adjusting for the 897 * required alignment, etc. If there is a free region at the 898 * edge of the window, grow from the inner edge of the free 899 * region. Otherwise grow from the window boundary. 900 * 901 * XXX: Special case: if w->res is completely empty and the 902 * request size is larger than w->res, we should find the 903 * optimal aligned buffer containing w->res and allocate that. 904 */ 905 if (bootverbose) 906 device_printf(sc->dev, 907 "attempting to grow %s window for (%#lx-%#lx,%#lx)\n", 908 w->name, start, end, count); 909 align = 1ul << RF_ALIGNMENT(flags); 910 if (start < rman_get_start(w->res)) { 911 if (rman_first_free_region(&w->rman, &start_free, &end_free) != 912 0 || start_free != rman_get_start(w->res)) 913 end_free = rman_get_start(w->res) - 1; 914 if (end_free > end) 915 end_free = end; 916 917 /* Move end_free down until it is properly aligned. */ 918 end_free &= ~(align - 1); 919 front = end_free - count; 920 921 /* 922 * The resource would now be allocated at (front, 923 * end_free). Ensure that fits in the (start, end) 924 * bounds. end_free is checked above. If 'front' is 925 * ok, ensure it is properly aligned for this window. 926 * Also check for underflow. 927 */ 928 if (front >= start && front <= end_free) { 929 if (bootverbose) 930 printf("\tfront candidate range: %#lx-%#lx\n", 931 front, end_free); 932 front &= (1ul << w->step) - 1; 933 front = rman_get_start(w->res) - front; 934 } else 935 front = 0; 936 } else 937 front = 0; 938 if (end > rman_get_end(w->res)) { 939 if (rman_last_free_region(&w->rman, &start_free, &end_free) != 940 0 || end_free != rman_get_end(w->res)) 941 start_free = rman_get_end(w->res) + 1; 942 if (start_free < start) 943 start_free = start; 944 945 /* Move start_free up until it is properly aligned. */ 946 start_free = roundup2(start_free, align); 947 back = start_free + count; 948 949 /* 950 * The resource would now be allocated at (start_free, 951 * back). Ensure that fits in the (start, end) 952 * bounds. start_free is checked above. If 'back' is 953 * ok, ensure it is properly aligned for this window. 954 * Also check for overflow. 955 */ 956 if (back <= end && start_free <= back) { 957 if (bootverbose) 958 printf("\tback candidate range: %#lx-%#lx\n", 959 start_free, back); 960 back = roundup2(back, w->step) - 1; 961 back -= rman_get_end(w->res); 962 } else 963 back = 0; 964 } else 965 back = 0; 966 967 /* 968 * Try to allocate the smallest needed region first. 969 * If that fails, fall back to the other region. 970 */ 971 error = ENOSPC; 972 while (front != 0 || back != 0) { 973 if (front != 0 && (front <= back || back == 0)) { 974 error = bus_adjust_resource(sc->dev, type, w->res, 975 rman_get_start(w->res) - front, 976 rman_get_end(w->res)); 977 if (error == 0) 978 break; 979 front = 0; 980 } else { 981 error = bus_adjust_resource(sc->dev, type, w->res, 982 rman_get_start(w->res), 983 rman_get_end(w->res) + back); 984 if (error == 0) 985 break; 986 back = 0; 987 } 988 } 989 990 if (error) 991 return (error); 992 if (bootverbose) 993 device_printf(sc->dev, "grew %s window to %#lx-%#lx\n", 994 w->name, rman_get_start(w->res), rman_get_end(w->res)); 995 996 /* Add the newly allocated region to the resource manager. */ 997 if (w->base != rman_get_start(w->res)) { 998 KASSERT(w->limit == rman_get_end(w->res), ("both ends moved")); 999 error = rman_manage_region(&w->rman, rman_get_start(w->res), 1000 w->base - 1); 1001 } else { 1002 KASSERT(w->limit != rman_get_end(w->res), 1003 ("neither end moved")); 1004 error = rman_manage_region(&w->rman, w->limit + 1, 1005 rman_get_end(w->res)); 1006 } 1007 if (error) { 1008 if (bootverbose) 1009 device_printf(sc->dev, 1010 "failed to expand %s resource manager\n", w->name); 1011 bus_adjust_resource(sc->dev, type, w->res, w->base, w->limit); 1012 return (error); 1013 } 1014 1015 updatewin: 1016 /* Save the new window. */ 1017 w->base = rman_get_start(w->res); 1018 w->limit = rman_get_end(w->res); 1019 KASSERT((w->base & ((1ul << w->step) - 1)) == 0, 1020 ("start address is not aligned")); 1021 KASSERT((w->limit & ((1ul << w->step) - 1)) == (1ul << w->step) - 1, 1022 ("end address is not aligned")); 1023 pcib_write_windows(sc, w->mask); 1024 return (0); 1025 } 1026 1027 /* 1028 * We have to trap resource allocation requests and ensure that the bridge 1029 * is set up to, or capable of handling them. 1030 */ 1031 struct resource * 1032 pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 1033 u_long start, u_long end, u_long count, u_int flags) 1034 { 1035 struct pcib_softc *sc; 1036 struct resource *r; 1037 1038 sc = device_get_softc(dev); 1039 1040 /* 1041 * VGA resources are decoded iff the VGA enable bit is set in 1042 * the bridge control register. VGA resources do not fall into 1043 * the resource windows and are passed up to the parent. 1044 */ 1045 if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) || 1046 (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) { 1047 if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) 1048 return (bus_generic_alloc_resource(dev, child, type, 1049 rid, start, end, count, flags)); 1050 else 1051 return (NULL); 1052 } 1053 1054 switch (type) { 1055 case SYS_RES_IOPORT: 1056 r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start, 1057 end, count, flags); 1058 if (r != NULL) 1059 break; 1060 if (pcib_grow_window(sc, &sc->io, type, start, end, count, 1061 flags) == 0) 1062 r = pcib_suballoc_resource(sc, &sc->io, child, type, 1063 rid, start, end, count, flags); 1064 break; 1065 case SYS_RES_MEMORY: 1066 /* 1067 * For prefetchable resources, prefer the prefetchable 1068 * memory window, but fall back to the regular memory 1069 * window if that fails. Try both windows before 1070 * attempting to grow a window in case the firmware 1071 * has used a range in the regular memory window to 1072 * map a prefetchable BAR. 1073 */ 1074 if (flags & RF_PREFETCHABLE) { 1075 r = pcib_suballoc_resource(sc, &sc->pmem, child, type, 1076 rid, start, end, count, flags); 1077 if (r != NULL) 1078 break; 1079 } 1080 r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid, 1081 start, end, count, flags); 1082 if (r != NULL) 1083 break; 1084 if (flags & RF_PREFETCHABLE) { 1085 if (pcib_grow_window(sc, &sc->pmem, type, start, end, 1086 count, flags) == 0) { 1087 r = pcib_suballoc_resource(sc, &sc->pmem, child, 1088 type, rid, start, end, count, flags); 1089 if (r != NULL) 1090 break; 1091 } 1092 } 1093 if (pcib_grow_window(sc, &sc->mem, type, start, end, count, 1094 flags & ~RF_PREFETCHABLE) == 0) 1095 r = pcib_suballoc_resource(sc, &sc->mem, child, type, 1096 rid, start, end, count, flags); 1097 break; 1098 default: 1099 return (bus_generic_alloc_resource(dev, child, type, rid, 1100 start, end, count, flags)); 1101 } 1102 1103 /* 1104 * If attempts to suballocate from the window fail but this is a 1105 * subtractive bridge, pass the request up the tree. 1106 */ 1107 if (sc->flags & PCIB_SUBTRACTIVE && r == NULL) 1108 return (bus_generic_alloc_resource(dev, child, type, rid, 1109 start, end, count, flags)); 1110 return (r); 1111 } 1112 1113 int 1114 pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r, 1115 u_long start, u_long end) 1116 { 1117 struct pcib_softc *sc; 1118 1119 sc = device_get_softc(bus); 1120 if (pcib_is_resource_managed(sc, type, r)) 1121 return (rman_adjust_resource(r, start, end)); 1122 return (bus_generic_adjust_resource(bus, child, type, r, start, end)); 1123 } 1124 1125 int 1126 pcib_release_resource(device_t dev, device_t child, int type, int rid, 1127 struct resource *r) 1128 { 1129 struct pcib_softc *sc; 1130 int error; 1131 1132 sc = device_get_softc(dev); 1133 if (pcib_is_resource_managed(sc, type, r)) { 1134 if (rman_get_flags(r) & RF_ACTIVE) { 1135 error = bus_deactivate_resource(child, type, rid, r); 1136 if (error) 1137 return (error); 1138 } 1139 return (rman_release_resource(r)); 1140 } 1141 return (bus_generic_release_resource(dev, child, type, rid, r)); 1142 } 1143 #else 1144 /* 1145 * We have to trap resource allocation requests and ensure that the bridge 1146 * is set up to, or capable of handling them. 1147 */ 1148 struct resource * 1149 pcib_alloc_resource(device_t dev, device_t child, int type, int *rid, 1150 u_long start, u_long end, u_long count, u_int flags) 1151 { 1152 struct pcib_softc *sc = device_get_softc(dev); 1153 const char *name, *suffix; 1154 int ok; 1155 1156 /* 1157 * Fail the allocation for this range if it's not supported. 1158 */ 1159 name = device_get_nameunit(child); 1160 if (name == NULL) { 1161 name = ""; 1162 suffix = ""; 1163 } else 1164 suffix = " "; 1165 switch (type) { 1166 case SYS_RES_IOPORT: 1167 ok = 0; 1168 if (!pcib_is_io_open(sc)) 1169 break; 1170 ok = (start >= sc->iobase && end <= sc->iolimit); 1171 1172 /* 1173 * Make sure we allow access to VGA I/O addresses when the 1174 * bridge has the "VGA Enable" bit set. 1175 */ 1176 if (!ok && pci_is_vga_ioport_range(start, end)) 1177 ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 1178 1179 if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 1180 if (!ok) { 1181 if (start < sc->iobase) 1182 start = sc->iobase; 1183 if (end > sc->iolimit) 1184 end = sc->iolimit; 1185 if (start < end) 1186 ok = 1; 1187 } 1188 } else { 1189 ok = 1; 1190 #if 0 1191 /* 1192 * If we overlap with the subtractive range, then 1193 * pick the upper range to use. 1194 */ 1195 if (start < sc->iolimit && end > sc->iobase) 1196 start = sc->iolimit + 1; 1197 #endif 1198 } 1199 if (end < start) { 1200 device_printf(dev, "ioport: end (%lx) < start (%lx)\n", 1201 end, start); 1202 start = 0; 1203 end = 0; 1204 ok = 0; 1205 } 1206 if (!ok) { 1207 device_printf(dev, "%s%srequested unsupported I/O " 1208 "range 0x%lx-0x%lx (decoding 0x%x-0x%x)\n", 1209 name, suffix, start, end, sc->iobase, sc->iolimit); 1210 return (NULL); 1211 } 1212 if (bootverbose) 1213 device_printf(dev, 1214 "%s%srequested I/O range 0x%lx-0x%lx: in range\n", 1215 name, suffix, start, end); 1216 break; 1217 1218 case SYS_RES_MEMORY: 1219 ok = 0; 1220 if (pcib_is_nonprefetch_open(sc)) 1221 ok = ok || (start >= sc->membase && end <= sc->memlimit); 1222 if (pcib_is_prefetch_open(sc)) 1223 ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit); 1224 1225 /* 1226 * Make sure we allow access to VGA memory addresses when the 1227 * bridge has the "VGA Enable" bit set. 1228 */ 1229 if (!ok && pci_is_vga_memory_range(start, end)) 1230 ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0; 1231 1232 if ((sc->flags & PCIB_SUBTRACTIVE) == 0) { 1233 if (!ok) { 1234 ok = 1; 1235 if (flags & RF_PREFETCHABLE) { 1236 if (pcib_is_prefetch_open(sc)) { 1237 if (start < sc->pmembase) 1238 start = sc->pmembase; 1239 if (end > sc->pmemlimit) 1240 end = sc->pmemlimit; 1241 } else { 1242 ok = 0; 1243 } 1244 } else { /* non-prefetchable */ 1245 if (pcib_is_nonprefetch_open(sc)) { 1246 if (start < sc->membase) 1247 start = sc->membase; 1248 if (end > sc->memlimit) 1249 end = sc->memlimit; 1250 } else { 1251 ok = 0; 1252 } 1253 } 1254 } 1255 } else if (!ok) { 1256 ok = 1; /* subtractive bridge: always ok */ 1257 #if 0 1258 if (pcib_is_nonprefetch_open(sc)) { 1259 if (start < sc->memlimit && end > sc->membase) 1260 start = sc->memlimit + 1; 1261 } 1262 if (pcib_is_prefetch_open(sc)) { 1263 if (start < sc->pmemlimit && end > sc->pmembase) 1264 start = sc->pmemlimit + 1; 1265 } 1266 #endif 1267 } 1268 if (end < start) { 1269 device_printf(dev, "memory: end (%lx) < start (%lx)\n", 1270 end, start); 1271 start = 0; 1272 end = 0; 1273 ok = 0; 1274 } 1275 if (!ok && bootverbose) 1276 device_printf(dev, 1277 "%s%srequested unsupported memory range %#lx-%#lx " 1278 "(decoding %#jx-%#jx, %#jx-%#jx)\n", 1279 name, suffix, start, end, 1280 (uintmax_t)sc->membase, (uintmax_t)sc->memlimit, 1281 (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit); 1282 if (!ok) 1283 return (NULL); 1284 if (bootverbose) 1285 device_printf(dev,"%s%srequested memory range " 1286 "0x%lx-0x%lx: good\n", 1287 name, suffix, start, end); 1288 break; 1289 1290 default: 1291 break; 1292 } 1293 /* 1294 * Bridge is OK decoding this resource, so pass it up. 1295 */ 1296 return (bus_generic_alloc_resource(dev, child, type, rid, start, end, 1297 count, flags)); 1298 } 1299 #endif 1300 1301 /* 1302 * PCIB interface. 1303 */ 1304 int 1305 pcib_maxslots(device_t dev) 1306 { 1307 return(PCI_SLOTMAX); 1308 } 1309 1310 /* 1311 * Since we are a child of a PCI bus, its parent must support the pcib interface. 1312 */ 1313 uint32_t 1314 pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width) 1315 { 1316 return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, width)); 1317 } 1318 1319 void 1320 pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width) 1321 { 1322 PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f, reg, val, width); 1323 } 1324 1325 /* 1326 * Route an interrupt across a PCI bridge. 1327 */ 1328 int 1329 pcib_route_interrupt(device_t pcib, device_t dev, int pin) 1330 { 1331 device_t bus; 1332 int parent_intpin; 1333 int intnum; 1334 1335 /* 1336 * 1337 * The PCI standard defines a swizzle of the child-side device/intpin to 1338 * the parent-side intpin as follows. 1339 * 1340 * device = device on child bus 1341 * child_intpin = intpin on child bus slot (0-3) 1342 * parent_intpin = intpin on parent bus slot (0-3) 1343 * 1344 * parent_intpin = (device + child_intpin) % 4 1345 */ 1346 parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4; 1347 1348 /* 1349 * Our parent is a PCI bus. Its parent must export the pcib interface 1350 * which includes the ability to route interrupts. 1351 */ 1352 bus = device_get_parent(pcib); 1353 intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1); 1354 if (PCI_INTERRUPT_VALID(intnum) && bootverbose) { 1355 device_printf(pcib, "slot %d INT%c is routed to irq %d\n", 1356 pci_get_slot(dev), 'A' + pin - 1, intnum); 1357 } 1358 return(intnum); 1359 } 1360 1361 /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */ 1362 int 1363 pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs) 1364 { 1365 struct pcib_softc *sc = device_get_softc(pcib); 1366 device_t bus; 1367 1368 if (sc->flags & PCIB_DISABLE_MSI) 1369 return (ENXIO); 1370 bus = device_get_parent(pcib); 1371 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount, 1372 irqs)); 1373 } 1374 1375 /* Pass request to release MSI/MSI-X messages up to the parent bridge. */ 1376 int 1377 pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs) 1378 { 1379 device_t bus; 1380 1381 bus = device_get_parent(pcib); 1382 return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs)); 1383 } 1384 1385 /* Pass request to alloc an MSI-X message up to the parent bridge. */ 1386 int 1387 pcib_alloc_msix(device_t pcib, device_t dev, int *irq) 1388 { 1389 struct pcib_softc *sc = device_get_softc(pcib); 1390 device_t bus; 1391 1392 if (sc->flags & PCIB_DISABLE_MSI) 1393 return (ENXIO); 1394 bus = device_get_parent(pcib); 1395 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq)); 1396 } 1397 1398 /* Pass request to release an MSI-X message up to the parent bridge. */ 1399 int 1400 pcib_release_msix(device_t pcib, device_t dev, int irq) 1401 { 1402 device_t bus; 1403 1404 bus = device_get_parent(pcib); 1405 return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq)); 1406 } 1407 1408 /* Pass request to map MSI/MSI-X message up to parent bridge. */ 1409 int 1410 pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, 1411 uint32_t *data) 1412 { 1413 device_t bus; 1414 int error; 1415 1416 bus = device_get_parent(pcib); 1417 error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data); 1418 if (error) 1419 return (error); 1420 1421 pci_ht_map_msi(pcib, *addr); 1422 return (0); 1423 } 1424 1425 /* Pass request for device power state up to parent bridge. */ 1426 int 1427 pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate) 1428 { 1429 device_t bus; 1430 1431 bus = device_get_parent(pcib); 1432 return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate)); 1433 } 1434 1435 /* 1436 * Try to read the bus number of a host-PCI bridge using appropriate config 1437 * registers. 1438 */ 1439 int 1440 host_pcib_get_busno(pci_read_config_fn read_config, int bus, int slot, int func, 1441 uint8_t *busnum) 1442 { 1443 uint32_t id; 1444 1445 id = read_config(bus, slot, func, PCIR_DEVVENDOR, 4); 1446 if (id == 0xffffffff) 1447 return (0); 1448 1449 switch (id) { 1450 case 0x12258086: 1451 /* Intel 824?? */ 1452 /* XXX This is a guess */ 1453 /* *busnum = read_config(bus, slot, func, 0x41, 1); */ 1454 *busnum = bus; 1455 break; 1456 case 0x84c48086: 1457 /* Intel 82454KX/GX (Orion) */ 1458 *busnum = read_config(bus, slot, func, 0x4a, 1); 1459 break; 1460 case 0x84ca8086: 1461 /* 1462 * For the 450nx chipset, there is a whole bundle of 1463 * things pretending to be host bridges. The MIOC will 1464 * be seen first and isn't really a pci bridge (the 1465 * actual busses are attached to the PXB's). We need to 1466 * read the registers of the MIOC to figure out the 1467 * bus numbers for the PXB channels. 1468 * 1469 * Since the MIOC doesn't have a pci bus attached, we 1470 * pretend it wasn't there. 1471 */ 1472 return (0); 1473 case 0x84cb8086: 1474 switch (slot) { 1475 case 0x12: 1476 /* Intel 82454NX PXB#0, Bus#A */ 1477 *busnum = read_config(bus, 0x10, func, 0xd0, 1); 1478 break; 1479 case 0x13: 1480 /* Intel 82454NX PXB#0, Bus#B */ 1481 *busnum = read_config(bus, 0x10, func, 0xd1, 1) + 1; 1482 break; 1483 case 0x14: 1484 /* Intel 82454NX PXB#1, Bus#A */ 1485 *busnum = read_config(bus, 0x10, func, 0xd3, 1); 1486 break; 1487 case 0x15: 1488 /* Intel 82454NX PXB#1, Bus#B */ 1489 *busnum = read_config(bus, 0x10, func, 0xd4, 1) + 1; 1490 break; 1491 } 1492 break; 1493 1494 /* ServerWorks -- vendor 0x1166 */ 1495 case 0x00051166: 1496 case 0x00061166: 1497 case 0x00081166: 1498 case 0x00091166: 1499 case 0x00101166: 1500 case 0x00111166: 1501 case 0x00171166: 1502 case 0x01011166: 1503 case 0x010f1014: 1504 case 0x01101166: 1505 case 0x02011166: 1506 case 0x02251166: 1507 case 0x03021014: 1508 *busnum = read_config(bus, slot, func, 0x44, 1); 1509 break; 1510 1511 /* Compaq/HP -- vendor 0x0e11 */ 1512 case 0x60100e11: 1513 *busnum = read_config(bus, slot, func, 0xc8, 1); 1514 break; 1515 default: 1516 /* Don't know how to read bus number. */ 1517 return 0; 1518 } 1519 1520 return 1; 1521 } 1522