1 /*- 2 * Copyright (c) 2002 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 #include "opt_acpi.h" 29 #include <sys/param.h> 30 #include <sys/bus.h> 31 #include <sys/kernel.h> 32 #include <sys/limits.h> 33 #include <sys/malloc.h> 34 #include <sys/module.h> 35 36 #include <contrib/dev/acpica/include/acpi.h> 37 38 #include <dev/acpica/acpivar.h> 39 #include <dev/acpica/acpi_pcibvar.h> 40 41 #include <machine/pci_cfgreg.h> 42 #include <dev/pci/pcireg.h> 43 #include <dev/pci/pcivar.h> 44 #include "pcib_if.h" 45 46 /* Hooks for the ACPI CA debugging infrastructure. */ 47 #define _COMPONENT ACPI_BUS 48 ACPI_MODULE_NAME("PCI_LINK") 49 50 ACPI_SERIAL_DECL(pci_link, "ACPI PCI link"); 51 52 #define NUM_ISA_INTERRUPTS 16 53 #define NUM_ACPI_INTERRUPTS 256 54 55 /* 56 * An ACPI PCI link device may contain multiple links. Each link has its 57 * own ACPI resource. _PRT entries specify which link is being used via 58 * the Source Index. 59 * 60 * XXX: A note about Source Indices and DPFs: Currently we assume that 61 * the DPF start and end tags are not counted towards the index that 62 * Source Index corresponds to. Also, we assume that when DPFs are in use 63 * they various sets overlap in terms of Indices. Here's an example 64 * resource list indicating these assumptions: 65 * 66 * Resource Index 67 * -------- ----- 68 * I/O Port 0 69 * Start DPF - 70 * IRQ 1 71 * MemIO 2 72 * Start DPF - 73 * IRQ 1 74 * MemIO 2 75 * End DPF - 76 * DMA Channel 3 77 * 78 * The XXX is because I'm not sure if this is a valid assumption to make. 79 */ 80 81 /* States during DPF processing. */ 82 #define DPF_OUTSIDE 0 83 #define DPF_FIRST 1 84 #define DPF_IGNORE 2 85 86 struct link; 87 88 struct acpi_pci_link_softc { 89 int pl_num_links; 90 int pl_crs_bad; 91 struct link *pl_links; 92 device_t pl_dev; 93 }; 94 95 struct link { 96 struct acpi_pci_link_softc *l_sc; 97 uint8_t l_bios_irq; 98 uint8_t l_irq; 99 uint8_t l_initial_irq; 100 UINT32 l_crs_type; 101 int l_res_index; 102 int l_num_irqs; 103 int *l_irqs; 104 int l_references; 105 bool l_routed:1; 106 bool l_isa_irq:1; 107 ACPI_RESOURCE l_prs_template; 108 }; 109 110 struct link_count_request { 111 int in_dpf; 112 int count; 113 }; 114 115 struct link_res_request { 116 struct acpi_pci_link_softc *sc; 117 int in_dpf; 118 int res_index; 119 int link_index; 120 }; 121 122 static MALLOC_DEFINE(M_PCI_LINK, "pci_link", "ACPI PCI Link structures"); 123 124 static int pci_link_interrupt_weights[NUM_ACPI_INTERRUPTS]; 125 static int pci_link_bios_isa_irqs; 126 127 static char *pci_link_ids[] = { "PNP0C0F", NULL }; 128 129 /* 130 * Fetch the short name associated with an ACPI handle and save it in the 131 * passed in buffer. 132 */ 133 static ACPI_STATUS 134 acpi_short_name(ACPI_HANDLE handle, char *buffer, size_t buflen) 135 { 136 ACPI_BUFFER buf; 137 138 buf.Length = buflen; 139 buf.Pointer = buffer; 140 return (AcpiGetName(handle, ACPI_SINGLE_NAME, &buf)); 141 } 142 143 static int 144 acpi_pci_link_probe(device_t dev) 145 { 146 char descr[28], name[12]; 147 int rv; 148 149 /* 150 * We explicitly do not check _STA since not all systems set it to 151 * sensible values. 152 */ 153 if (acpi_disabled("pci_link")) 154 return (ENXIO); 155 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, pci_link_ids, NULL); 156 if (rv > 0) 157 return (rv); 158 159 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), name, 160 sizeof(name)))) { 161 snprintf(descr, sizeof(descr), "ACPI PCI Link %s", name); 162 device_set_desc_copy(dev, descr); 163 } else 164 device_set_desc(dev, "ACPI PCI Link"); 165 device_quiet(dev); 166 return (rv); 167 } 168 169 static ACPI_STATUS 170 acpi_count_irq_resources(ACPI_RESOURCE *res, void *context) 171 { 172 struct link_count_request *req; 173 174 req = (struct link_count_request *)context; 175 switch (res->Type) { 176 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 177 switch (req->in_dpf) { 178 case DPF_OUTSIDE: 179 /* We've started the first DPF. */ 180 req->in_dpf = DPF_FIRST; 181 break; 182 case DPF_FIRST: 183 /* We've started the second DPF. */ 184 req->in_dpf = DPF_IGNORE; 185 break; 186 } 187 break; 188 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 189 /* We are finished with DPF parsing. */ 190 KASSERT(req->in_dpf != DPF_OUTSIDE, 191 ("%s: end dpf when not parsing a dpf", __func__)); 192 req->in_dpf = DPF_OUTSIDE; 193 break; 194 case ACPI_RESOURCE_TYPE_IRQ: 195 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 196 /* 197 * Don't count resources if we are in a DPF set that we are 198 * ignoring. 199 */ 200 if (req->in_dpf != DPF_IGNORE) 201 req->count++; 202 } 203 return (AE_OK); 204 } 205 206 static ACPI_STATUS 207 link_add_crs(ACPI_RESOURCE *res, void *context) 208 { 209 struct link_res_request *req; 210 struct link *link; 211 212 ACPI_SERIAL_ASSERT(pci_link); 213 req = (struct link_res_request *)context; 214 switch (res->Type) { 215 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 216 switch (req->in_dpf) { 217 case DPF_OUTSIDE: 218 /* We've started the first DPF. */ 219 req->in_dpf = DPF_FIRST; 220 break; 221 case DPF_FIRST: 222 /* We've started the second DPF. */ 223 panic( 224 "%s: Multiple dependent functions within a current resource", 225 __func__); 226 break; 227 } 228 break; 229 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 230 /* We are finished with DPF parsing. */ 231 KASSERT(req->in_dpf != DPF_OUTSIDE, 232 ("%s: end dpf when not parsing a dpf", __func__)); 233 req->in_dpf = DPF_OUTSIDE; 234 break; 235 case ACPI_RESOURCE_TYPE_IRQ: 236 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 237 KASSERT(req->link_index < req->sc->pl_num_links, 238 ("%s: array boundary violation", __func__)); 239 link = &req->sc->pl_links[req->link_index]; 240 link->l_res_index = req->res_index; 241 link->l_crs_type = res->Type; 242 req->link_index++; 243 req->res_index++; 244 245 /* 246 * Only use the current value if there's one IRQ. Some 247 * systems return multiple IRQs (which is nonsense for _CRS) 248 * when the link hasn't been programmed. 249 */ 250 if (res->Type == ACPI_RESOURCE_TYPE_IRQ) { 251 if (res->Data.Irq.InterruptCount == 1) 252 link->l_irq = res->Data.Irq.Interrupts[0]; 253 } else if (res->Data.ExtendedIrq.InterruptCount == 1) 254 link->l_irq = res->Data.ExtendedIrq.Interrupts[0]; 255 256 /* 257 * An IRQ of zero means that the link isn't routed. 258 */ 259 if (link->l_irq == 0) 260 link->l_irq = PCI_INVALID_IRQ; 261 break; 262 default: 263 req->res_index++; 264 } 265 return (AE_OK); 266 } 267 268 /* 269 * Populate the set of possible IRQs for each device. 270 */ 271 static ACPI_STATUS 272 link_add_prs(ACPI_RESOURCE *res, void *context) 273 { 274 ACPI_RESOURCE *tmp; 275 struct link_res_request *req; 276 struct link *link; 277 UINT8 *irqs = NULL; 278 UINT32 *ext_irqs = NULL; 279 int i, is_ext_irq = 1; 280 281 ACPI_SERIAL_ASSERT(pci_link); 282 req = (struct link_res_request *)context; 283 switch (res->Type) { 284 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 285 switch (req->in_dpf) { 286 case DPF_OUTSIDE: 287 /* We've started the first DPF. */ 288 req->in_dpf = DPF_FIRST; 289 break; 290 case DPF_FIRST: 291 /* We've started the second DPF. */ 292 req->in_dpf = DPF_IGNORE; 293 break; 294 } 295 break; 296 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 297 /* We are finished with DPF parsing. */ 298 KASSERT(req->in_dpf != DPF_OUTSIDE, 299 ("%s: end dpf when not parsing a dpf", __func__)); 300 req->in_dpf = DPF_OUTSIDE; 301 break; 302 case ACPI_RESOURCE_TYPE_IRQ: 303 is_ext_irq = 0; 304 /* fall through */ 305 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 306 /* 307 * Don't parse resources if we are in a DPF set that we are 308 * ignoring. 309 */ 310 if (req->in_dpf == DPF_IGNORE) 311 break; 312 313 KASSERT(req->link_index < req->sc->pl_num_links, 314 ("%s: array boundary violation", __func__)); 315 link = &req->sc->pl_links[req->link_index]; 316 if (link->l_res_index == -1) { 317 KASSERT(req->sc->pl_crs_bad, 318 ("res_index should be set")); 319 link->l_res_index = req->res_index; 320 } 321 req->link_index++; 322 req->res_index++; 323 324 /* 325 * Stash a copy of the resource for later use when doing 326 * _SRS. 327 */ 328 tmp = &link->l_prs_template; 329 if (is_ext_irq) { 330 bcopy(res, tmp, ACPI_RS_SIZE(tmp->Data.ExtendedIrq)); 331 332 /* 333 * XXX acpi_AppendBufferResource() cannot handle 334 * optional data. 335 */ 336 bzero(&tmp->Data.ExtendedIrq.ResourceSource, 337 sizeof(tmp->Data.ExtendedIrq.ResourceSource)); 338 tmp->Length = ACPI_RS_SIZE(tmp->Data.ExtendedIrq); 339 340 link->l_num_irqs = 341 res->Data.ExtendedIrq.InterruptCount; 342 ext_irqs = res->Data.ExtendedIrq.Interrupts; 343 } else { 344 bcopy(res, tmp, ACPI_RS_SIZE(tmp->Data.Irq)); 345 link->l_num_irqs = res->Data.Irq.InterruptCount; 346 irqs = res->Data.Irq.Interrupts; 347 } 348 if (link->l_num_irqs == 0) 349 break; 350 351 /* 352 * Save a list of the valid IRQs. Also, if all of the 353 * valid IRQs are ISA IRQs, then mark this link as 354 * routed via an ISA interrupt. 355 */ 356 link->l_isa_irq = true; 357 link->l_irqs = malloc(sizeof(int) * link->l_num_irqs, 358 M_PCI_LINK, M_WAITOK | M_ZERO); 359 for (i = 0; i < link->l_num_irqs; i++) { 360 if (is_ext_irq) { 361 link->l_irqs[i] = ext_irqs[i]; 362 if (ext_irqs[i] >= NUM_ISA_INTERRUPTS) 363 link->l_isa_irq = false; 364 } else { 365 link->l_irqs[i] = irqs[i]; 366 if (irqs[i] >= NUM_ISA_INTERRUPTS) 367 link->l_isa_irq = false; 368 } 369 } 370 371 /* 372 * If this is not an ISA IRQ but _CRS used a non-extended 373 * IRQ descriptor, don't use _CRS as a template for _SRS. 374 */ 375 if (!req->sc->pl_crs_bad && !link->l_isa_irq && 376 link->l_crs_type == ACPI_RESOURCE_TYPE_IRQ) 377 req->sc->pl_crs_bad = true; 378 break; 379 default: 380 if (req->in_dpf == DPF_IGNORE) 381 break; 382 if (req->sc->pl_crs_bad) 383 device_printf(req->sc->pl_dev, 384 "Warning: possible resource %d will be lost during _SRS\n", 385 req->res_index); 386 req->res_index++; 387 } 388 return (AE_OK); 389 } 390 391 static bool 392 link_valid_irq(struct link *link, int irq) 393 { 394 int i; 395 396 ACPI_SERIAL_ASSERT(pci_link); 397 398 /* Invalid interrupts are never valid. */ 399 if (!PCI_INTERRUPT_VALID(irq)) 400 return (false); 401 402 /* Any interrupt in the list of possible interrupts is valid. */ 403 for (i = 0; i < link->l_num_irqs; i++) 404 if (link->l_irqs[i] == irq) 405 return (true); 406 407 /* 408 * For links routed via an ISA interrupt, if the SCI is routed via 409 * an ISA interrupt, the SCI is always treated as a valid IRQ. 410 */ 411 if (link->l_isa_irq && AcpiGbl_FADT.SciInterrupt == irq && 412 irq < NUM_ISA_INTERRUPTS) 413 return (true); 414 415 /* If the interrupt wasn't found in the list it is not valid. */ 416 return (false); 417 } 418 419 static void 420 acpi_pci_link_dump(struct acpi_pci_link_softc *sc, int header, const char *tag) 421 { 422 struct link *link; 423 char buf[16]; 424 int i, j; 425 426 ACPI_SERIAL_ASSERT(pci_link); 427 if (header) { 428 snprintf(buf, sizeof(buf), "%s:", 429 device_get_nameunit(sc->pl_dev)); 430 printf("%-16.16s Index IRQ Rtd Ref IRQs\n", buf); 431 } 432 for (i = 0; i < sc->pl_num_links; i++) { 433 link = &sc->pl_links[i]; 434 printf(" %-14.14s %5d %3d %c %3d ", i == 0 ? tag : "", i, 435 link->l_irq, link->l_routed ? 'Y' : 'N', 436 link->l_references); 437 if (link->l_num_irqs == 0) 438 printf(" none"); 439 else for (j = 0; j < link->l_num_irqs; j++) 440 printf(" %d", link->l_irqs[j]); 441 printf("\n"); 442 } 443 } 444 445 static int 446 acpi_pci_link_attach(device_t dev) 447 { 448 struct acpi_pci_link_softc *sc; 449 struct link_count_request creq; 450 struct link_res_request rreq; 451 ACPI_STATUS status; 452 int i; 453 454 sc = device_get_softc(dev); 455 sc->pl_dev = dev; 456 ACPI_SERIAL_BEGIN(pci_link); 457 458 /* 459 * Count the number of current resources so we know how big of 460 * a link array to allocate. On some systems, _CRS is broken, 461 * so for those systems try to derive the count from _PRS instead. 462 */ 463 creq.in_dpf = DPF_OUTSIDE; 464 creq.count = 0; 465 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS", 466 acpi_count_irq_resources, &creq); 467 sc->pl_crs_bad = ACPI_FAILURE(status); 468 if (sc->pl_crs_bad) { 469 creq.in_dpf = DPF_OUTSIDE; 470 creq.count = 0; 471 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS", 472 acpi_count_irq_resources, &creq); 473 if (ACPI_FAILURE(status)) { 474 device_printf(dev, 475 "Unable to parse _CRS or _PRS: %s\n", 476 AcpiFormatException(status)); 477 ACPI_SERIAL_END(pci_link); 478 return (ENXIO); 479 } 480 } 481 sc->pl_num_links = creq.count; 482 if (creq.count == 0) { 483 ACPI_SERIAL_END(pci_link); 484 return (0); 485 } 486 sc->pl_links = malloc(sizeof(struct link) * sc->pl_num_links, 487 M_PCI_LINK, M_WAITOK | M_ZERO); 488 489 /* Initialize the child links. */ 490 for (i = 0; i < sc->pl_num_links; i++) { 491 sc->pl_links[i].l_irq = PCI_INVALID_IRQ; 492 sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ; 493 sc->pl_links[i].l_sc = sc; 494 sc->pl_links[i].l_isa_irq = false; 495 sc->pl_links[i].l_res_index = -1; 496 } 497 498 /* Try to read the current settings from _CRS if it is valid. */ 499 if (!sc->pl_crs_bad) { 500 rreq.in_dpf = DPF_OUTSIDE; 501 rreq.link_index = 0; 502 rreq.res_index = 0; 503 rreq.sc = sc; 504 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS", 505 link_add_crs, &rreq); 506 if (ACPI_FAILURE(status)) { 507 device_printf(dev, "Unable to parse _CRS: %s\n", 508 AcpiFormatException(status)); 509 goto fail; 510 } 511 } 512 513 /* 514 * Try to read the possible settings from _PRS. Note that if the 515 * _CRS is toast, we depend on having a working _PRS. However, if 516 * _CRS works, then it is ok for _PRS to be missing. 517 */ 518 rreq.in_dpf = DPF_OUTSIDE; 519 rreq.link_index = 0; 520 rreq.res_index = 0; 521 rreq.sc = sc; 522 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS", 523 link_add_prs, &rreq); 524 if (ACPI_FAILURE(status) && 525 (status != AE_NOT_FOUND || sc->pl_crs_bad)) { 526 device_printf(dev, "Unable to parse _PRS: %s\n", 527 AcpiFormatException(status)); 528 goto fail; 529 } 530 if (bootverbose) 531 acpi_pci_link_dump(sc, 1, "Initial Probe"); 532 533 /* Verify initial IRQs if we have _PRS. */ 534 if (status != AE_NOT_FOUND) 535 for (i = 0; i < sc->pl_num_links; i++) 536 if (!link_valid_irq(&sc->pl_links[i], 537 sc->pl_links[i].l_irq)) 538 sc->pl_links[i].l_irq = PCI_INVALID_IRQ; 539 if (bootverbose) 540 acpi_pci_link_dump(sc, 0, "Validation"); 541 542 /* Save initial IRQs. */ 543 for (i = 0; i < sc->pl_num_links; i++) 544 sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq; 545 546 /* 547 * Try to disable this link. If successful, set the current IRQ to 548 * zero and flags to indicate this link is not routed. If we can't 549 * run _DIS (i.e., the method doesn't exist), assume the initial 550 * IRQ was routed by the BIOS. 551 */ 552 if (ACPI_SUCCESS(AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL, 553 NULL))) 554 for (i = 0; i < sc->pl_num_links; i++) 555 sc->pl_links[i].l_irq = PCI_INVALID_IRQ; 556 else 557 for (i = 0; i < sc->pl_num_links; i++) 558 if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq)) 559 sc->pl_links[i].l_routed = true; 560 if (bootverbose) 561 acpi_pci_link_dump(sc, 0, "After Disable"); 562 ACPI_SERIAL_END(pci_link); 563 return (0); 564 fail: 565 ACPI_SERIAL_END(pci_link); 566 for (i = 0; i < sc->pl_num_links; i++) 567 if (sc->pl_links[i].l_irqs != NULL) 568 free(sc->pl_links[i].l_irqs, M_PCI_LINK); 569 free(sc->pl_links, M_PCI_LINK); 570 return (ENXIO); 571 } 572 573 /* XXX: Note that this is identical to pci_pir_search_irq(). */ 574 static uint8_t 575 acpi_pci_link_search_irq(int domain, int bus, int device, int pin) 576 { 577 uint32_t value; 578 uint8_t func, maxfunc; 579 580 /* See if we have a valid device at function 0. */ 581 value = pci_cfgregread(domain, bus, device, 0, PCIR_VENDOR, 2); 582 if (value == PCIV_INVALID) 583 return (PCI_INVALID_IRQ); 584 value = pci_cfgregread(domain, bus, device, 0, PCIR_HDRTYPE, 1); 585 if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE) 586 return (PCI_INVALID_IRQ); 587 if (value & PCIM_MFDEV) 588 maxfunc = PCI_FUNCMAX; 589 else 590 maxfunc = 0; 591 592 /* Scan all possible functions at this device. */ 593 for (func = 0; func <= maxfunc; func++) { 594 value = pci_cfgregread(domain, bus, device, func, PCIR_VENDOR, 595 2); 596 if (value == PCIV_INVALID) 597 continue; 598 value = pci_cfgregread(domain, bus, device, func, PCIR_INTPIN, 599 1); 600 601 /* 602 * See if it uses the pin in question. Note that the passed 603 * in pin uses 0 for A, .. 3 for D whereas the intpin 604 * register uses 0 for no interrupt, 1 for A, .. 4 for D. 605 */ 606 if (value != pin + 1) 607 continue; 608 value = pci_cfgregread(domain, bus, device, func, PCIR_INTLINE, 609 1); 610 if (bootverbose) 611 printf( 612 "ACPI: Found matching pin for %d.%d.INT%c at func %d: %d\n", 613 bus, device, pin + 'A', func, value); 614 if (value != PCI_INVALID_IRQ) 615 return (value); 616 } 617 return (PCI_INVALID_IRQ); 618 } 619 620 /* 621 * Find the link structure that corresponds to the resource index passed in 622 * via 'source_index'. 623 */ 624 static struct link * 625 acpi_pci_link_lookup(device_t dev, int source_index) 626 { 627 struct acpi_pci_link_softc *sc; 628 int i; 629 630 ACPI_SERIAL_ASSERT(pci_link); 631 sc = device_get_softc(dev); 632 for (i = 0; i < sc->pl_num_links; i++) 633 if (sc->pl_links[i].l_res_index == source_index) 634 return (&sc->pl_links[i]); 635 return (NULL); 636 } 637 638 void 639 acpi_pci_link_add_reference(device_t dev, int index, device_t pcib, int slot, 640 int pin) 641 { 642 struct link *link; 643 uint8_t bios_irq; 644 uintptr_t bus, domain; 645 646 /* 647 * Look up the PCI domain and bus for the specified PCI bridge 648 * device. Note that the PCI bridge device might not have any 649 * children yet. However, looking up these IVARs doesn't 650 * require a valid child device, so we just pass NULL. 651 */ 652 if (BUS_READ_IVAR(pcib, NULL, PCIB_IVAR_BUS, &bus) != 0) { 653 device_printf(pcib, "Unable to read PCI bus number"); 654 panic("PCI bridge without a bus number"); 655 } 656 if (BUS_READ_IVAR(pcib, NULL, PCIB_IVAR_DOMAIN, &domain) != 0) { 657 device_printf(pcib, "Unable to read PCI domain number"); 658 panic("PCI bridge without a domain number"); 659 } 660 661 /* Bump the reference count. */ 662 ACPI_SERIAL_BEGIN(pci_link); 663 link = acpi_pci_link_lookup(dev, index); 664 if (link == NULL) { 665 device_printf(dev, "apparently invalid index %d\n", index); 666 ACPI_SERIAL_END(pci_link); 667 return; 668 } 669 link->l_references++; 670 if (link->l_routed) 671 pci_link_interrupt_weights[link->l_irq]++; 672 673 /* 674 * The BIOS only routes interrupts via ISA IRQs using the ATPICs 675 * (8259As). Thus, if this link is routed via an ISA IRQ, go 676 * look to see if the BIOS routed an IRQ for this link at the 677 * indicated (domain, bus, slot, pin). If so, we prefer that IRQ for 678 * this link and add that IRQ to our list of known-good IRQs. 679 * This provides a good work-around for link devices whose _CRS 680 * method is either broken or bogus. We only use the value 681 * returned by _CRS if we can't find a valid IRQ via this method 682 * in fact. 683 * 684 * If this link is not routed via an ISA IRQ (because we are using 685 * APIC for example), then don't bother looking up the BIOS IRQ 686 * as if we find one it won't be valid anyway. 687 */ 688 if (!link->l_isa_irq) { 689 ACPI_SERIAL_END(pci_link); 690 return; 691 } 692 693 /* Try to find a BIOS IRQ setting from any matching devices. */ 694 bios_irq = acpi_pci_link_search_irq(domain, bus, slot, pin); 695 if (!PCI_INTERRUPT_VALID(bios_irq)) { 696 ACPI_SERIAL_END(pci_link); 697 return; 698 } 699 700 /* Validate the BIOS IRQ. */ 701 if (!link_valid_irq(link, bios_irq)) { 702 device_printf(dev, "BIOS IRQ %u for %d.%d.INT%c is invalid\n", 703 bios_irq, (int)bus, slot, pin + 'A'); 704 } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) { 705 link->l_bios_irq = bios_irq; 706 if (bios_irq < NUM_ISA_INTERRUPTS) 707 pci_link_bios_isa_irqs |= (1 << bios_irq); 708 if (bios_irq != link->l_initial_irq && 709 PCI_INTERRUPT_VALID(link->l_initial_irq)) 710 device_printf(dev, 711 "BIOS IRQ %u does not match initial IRQ %u\n", 712 bios_irq, link->l_initial_irq); 713 } else if (bios_irq != link->l_bios_irq) 714 device_printf(dev, 715 "BIOS IRQ %u for %d.%d.INT%c does not match previous BIOS IRQ %u\n", 716 bios_irq, (int)bus, slot, pin + 'A', 717 link->l_bios_irq); 718 ACPI_SERIAL_END(pci_link); 719 } 720 721 static ACPI_STATUS 722 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf) 723 { 724 ACPI_RESOURCE *end, *res; 725 ACPI_STATUS status; 726 struct link *link; 727 int i __diagused, in_dpf; 728 729 /* Fetch the _CRS. */ 730 ACPI_SERIAL_ASSERT(pci_link); 731 srsbuf->Pointer = NULL; 732 srsbuf->Length = ACPI_ALLOCATE_BUFFER; 733 status = AcpiGetCurrentResources(acpi_get_handle(sc->pl_dev), srsbuf); 734 if (ACPI_SUCCESS(status) && srsbuf->Pointer == NULL) 735 status = AE_NO_MEMORY; 736 if (ACPI_FAILURE(status)) { 737 if (bootverbose) 738 device_printf(sc->pl_dev, 739 "Unable to fetch current resources: %s\n", 740 AcpiFormatException(status)); 741 return (status); 742 } 743 744 /* Fill in IRQ resources via link structures. */ 745 link = sc->pl_links; 746 i = 0; 747 in_dpf = DPF_OUTSIDE; 748 res = (ACPI_RESOURCE *)srsbuf->Pointer; 749 end = (ACPI_RESOURCE *)((char *)srsbuf->Pointer + srsbuf->Length); 750 for (;;) { 751 switch (res->Type) { 752 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 753 switch (in_dpf) { 754 case DPF_OUTSIDE: 755 /* We've started the first DPF. */ 756 in_dpf = DPF_FIRST; 757 break; 758 case DPF_FIRST: 759 /* We've started the second DPF. */ 760 panic( 761 "%s: Multiple dependent functions within a current resource", 762 __func__); 763 break; 764 } 765 break; 766 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 767 /* We are finished with DPF parsing. */ 768 KASSERT(in_dpf != DPF_OUTSIDE, 769 ("%s: end dpf when not parsing a dpf", __func__)); 770 in_dpf = DPF_OUTSIDE; 771 break; 772 case ACPI_RESOURCE_TYPE_IRQ: 773 MPASS(i < sc->pl_num_links); 774 res->Data.Irq.InterruptCount = 1; 775 if (PCI_INTERRUPT_VALID(link->l_irq)) { 776 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS, 777 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type", 778 __func__, link->l_irq)); 779 res->Data.Irq.Interrupts[0] = link->l_irq; 780 } else 781 res->Data.Irq.Interrupts[0] = 0; 782 link++; 783 i++; 784 break; 785 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 786 MPASS(i < sc->pl_num_links); 787 res->Data.ExtendedIrq.InterruptCount = 1; 788 if (PCI_INTERRUPT_VALID(link->l_irq)) 789 res->Data.ExtendedIrq.Interrupts[0] = 790 link->l_irq; 791 else 792 res->Data.ExtendedIrq.Interrupts[0] = 0; 793 link++; 794 i++; 795 break; 796 } 797 if (res->Type == ACPI_RESOURCE_TYPE_END_TAG) 798 break; 799 res = ACPI_NEXT_RESOURCE(res); 800 if (res >= end) 801 break; 802 } 803 return (AE_OK); 804 } 805 806 static ACPI_STATUS 807 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc, 808 ACPI_BUFFER *srsbuf) 809 { 810 ACPI_RESOURCE newres; 811 ACPI_STATUS status; 812 struct link *link; 813 int i; 814 815 /* Start off with an empty buffer. */ 816 srsbuf->Pointer = NULL; 817 link = sc->pl_links; 818 for (i = 0; i < sc->pl_num_links; i++) { 819 /* Add a new IRQ resource from each link. */ 820 link = &sc->pl_links[i]; 821 if (link->l_prs_template.Type == ACPI_RESOURCE_TYPE_IRQ) { 822 /* Build an IRQ resource. */ 823 bcopy(&link->l_prs_template, &newres, 824 ACPI_RS_SIZE(newres.Data.Irq)); 825 newres.Data.Irq.InterruptCount = 1; 826 if (PCI_INTERRUPT_VALID(link->l_irq)) { 827 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS, 828 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type", 829 __func__, link->l_irq)); 830 newres.Data.Irq.Interrupts[0] = link->l_irq; 831 } else 832 newres.Data.Irq.Interrupts[0] = 0; 833 } else { 834 /* Build an ExtIRQ resuorce. */ 835 bcopy(&link->l_prs_template, &newres, 836 ACPI_RS_SIZE(newres.Data.ExtendedIrq)); 837 newres.Data.ExtendedIrq.InterruptCount = 1; 838 if (PCI_INTERRUPT_VALID(link->l_irq)) 839 newres.Data.ExtendedIrq.Interrupts[0] = 840 link->l_irq; 841 else 842 newres.Data.ExtendedIrq.Interrupts[0] = 0; 843 } 844 845 /* Add the new resource to the end of the _SRS buffer. */ 846 status = acpi_AppendBufferResource(srsbuf, &newres); 847 if (ACPI_FAILURE(status)) { 848 device_printf(sc->pl_dev, 849 "Unable to build resources: %s\n", 850 AcpiFormatException(status)); 851 if (srsbuf->Pointer != NULL) { 852 AcpiOsFree(srsbuf->Pointer); 853 srsbuf->Pointer = NULL; 854 } 855 return (status); 856 } 857 } 858 return (AE_OK); 859 } 860 861 static ACPI_STATUS 862 acpi_pci_link_route_irqs(device_t dev) 863 { 864 struct acpi_pci_link_softc *sc; 865 ACPI_RESOURCE *resource, *end; 866 ACPI_BUFFER srsbuf; 867 ACPI_STATUS status; 868 struct link *link; 869 int i __diagused; 870 871 ACPI_SERIAL_ASSERT(pci_link); 872 sc = device_get_softc(dev); 873 if (sc->pl_crs_bad) 874 status = acpi_pci_link_srs_from_links(sc, &srsbuf); 875 else 876 status = acpi_pci_link_srs_from_crs(sc, &srsbuf); 877 if (ACPI_FAILURE(status)) 878 return (status); 879 880 /* Write out new resources via _SRS. */ 881 status = AcpiSetCurrentResources(acpi_get_handle(dev), &srsbuf); 882 if (ACPI_FAILURE(status)) { 883 device_printf(dev, "Unable to route IRQs: %s\n", 884 AcpiFormatException(status)); 885 AcpiOsFree(srsbuf.Pointer); 886 return (status); 887 } 888 889 /* 890 * Perform acpi_config_intr() on each IRQ resource if it was just 891 * routed for the first time. 892 */ 893 link = sc->pl_links; 894 i = 0; 895 resource = (ACPI_RESOURCE *)srsbuf.Pointer; 896 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length); 897 for (;;) { 898 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG) 899 break; 900 switch (resource->Type) { 901 case ACPI_RESOURCE_TYPE_IRQ: 902 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 903 MPASS(i < sc->pl_num_links); 904 905 /* 906 * Only configure the interrupt and update the 907 * weights if this link has a valid IRQ and was 908 * previously unrouted. 909 */ 910 if (!link->l_routed && 911 PCI_INTERRUPT_VALID(link->l_irq)) { 912 link->l_routed = true; 913 acpi_config_intr(dev, resource); 914 pci_link_interrupt_weights[link->l_irq] += 915 link->l_references; 916 } 917 link++; 918 i++; 919 break; 920 } 921 resource = ACPI_NEXT_RESOURCE(resource); 922 if (resource >= end) 923 break; 924 } 925 AcpiOsFree(srsbuf.Pointer); 926 return (AE_OK); 927 } 928 929 static int 930 acpi_pci_link_resume(device_t dev) 931 { 932 struct acpi_pci_link_softc *sc; 933 ACPI_STATUS status; 934 int i, routed; 935 936 /* 937 * If all of our links are routed, then restore the link via _SRS, 938 * otherwise, disable the link via _DIS. 939 */ 940 ACPI_SERIAL_BEGIN(pci_link); 941 sc = device_get_softc(dev); 942 routed = 0; 943 for (i = 0; i < sc->pl_num_links; i++) 944 if (sc->pl_links[i].l_routed) 945 routed++; 946 if (routed == sc->pl_num_links) 947 status = acpi_pci_link_route_irqs(dev); 948 else { 949 AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL, NULL); 950 status = AE_OK; 951 } 952 ACPI_SERIAL_END(pci_link); 953 if (ACPI_FAILURE(status)) 954 return (ENXIO); 955 else 956 return (0); 957 } 958 959 /* 960 * Pick an IRQ to use for this unrouted link. 961 */ 962 static uint8_t 963 acpi_pci_link_choose_irq(device_t dev, struct link *link) 964 { 965 char tunable_buffer[64], link_name[5]; 966 u_int8_t best_irq, pos_irq; 967 int best_weight, pos_weight, i; 968 969 KASSERT(!link->l_routed, ("%s: link already routed", __func__)); 970 KASSERT(!PCI_INTERRUPT_VALID(link->l_irq), 971 ("%s: link already has an IRQ", __func__)); 972 973 /* Check for a tunable override. */ 974 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), link_name, 975 sizeof(link_name)))) { 976 snprintf(tunable_buffer, sizeof(tunable_buffer), 977 "hw.pci.link.%s.%d.irq", link_name, link->l_res_index); 978 if (getenv_int(tunable_buffer, &i) && PCI_INTERRUPT_VALID(i)) { 979 if (!link_valid_irq(link, i)) 980 device_printf(dev, 981 "Warning, IRQ %d is not listed as valid\n", 982 i); 983 return (i); 984 } 985 snprintf(tunable_buffer, sizeof(tunable_buffer), 986 "hw.pci.link.%s.irq", link_name); 987 if (getenv_int(tunable_buffer, &i) && PCI_INTERRUPT_VALID(i)) { 988 if (!link_valid_irq(link, i)) 989 device_printf(dev, 990 "Warning, IRQ %d is not listed as valid\n", 991 i); 992 return (i); 993 } 994 } 995 996 /* 997 * If we have a valid BIOS IRQ, use that. We trust what the BIOS 998 * says it routed over what _CRS says the link thinks is routed. 999 */ 1000 if (PCI_INTERRUPT_VALID(link->l_bios_irq)) 1001 return (link->l_bios_irq); 1002 1003 /* 1004 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS, 1005 * then use that. 1006 */ 1007 if (PCI_INTERRUPT_VALID(link->l_initial_irq)) 1008 return (link->l_initial_irq); 1009 1010 /* 1011 * Ok, we have no useful hints, so we have to pick from the 1012 * possible IRQs. For ISA IRQs we only use interrupts that 1013 * have already been used by the BIOS. 1014 */ 1015 best_irq = PCI_INVALID_IRQ; 1016 best_weight = INT_MAX; 1017 for (i = 0; i < link->l_num_irqs; i++) { 1018 pos_irq = link->l_irqs[i]; 1019 if (pos_irq < NUM_ISA_INTERRUPTS && 1020 (pci_link_bios_isa_irqs & 1 << pos_irq) == 0) 1021 continue; 1022 pos_weight = pci_link_interrupt_weights[pos_irq]; 1023 if (pos_weight < best_weight) { 1024 best_weight = pos_weight; 1025 best_irq = pos_irq; 1026 } 1027 } 1028 1029 /* 1030 * If this is an ISA IRQ, try using the SCI if it is also an ISA 1031 * interrupt as a fallback. 1032 */ 1033 if (link->l_isa_irq) { 1034 pos_irq = AcpiGbl_FADT.SciInterrupt; 1035 pos_weight = pci_link_interrupt_weights[pos_irq]; 1036 if (pos_weight < best_weight) { 1037 best_weight = pos_weight; 1038 best_irq = pos_irq; 1039 } 1040 } 1041 1042 if (PCI_INTERRUPT_VALID(best_irq)) { 1043 if (bootverbose) 1044 device_printf(dev, "Picked IRQ %u with weight %d\n", 1045 best_irq, best_weight); 1046 } else 1047 device_printf(dev, "Unable to choose an IRQ\n"); 1048 return (best_irq); 1049 } 1050 1051 int 1052 acpi_pci_link_route_interrupt(device_t dev, int index) 1053 { 1054 struct link *link; 1055 1056 if (acpi_disabled("pci_link")) 1057 return (PCI_INVALID_IRQ); 1058 1059 ACPI_SERIAL_BEGIN(pci_link); 1060 link = acpi_pci_link_lookup(dev, index); 1061 if (link == NULL) 1062 panic("%s: apparently invalid index %d", __func__, index); 1063 1064 /* 1065 * If this link device is already routed to an interrupt, just return 1066 * the interrupt it is routed to. 1067 */ 1068 if (link->l_routed) { 1069 KASSERT(PCI_INTERRUPT_VALID(link->l_irq), 1070 ("%s: link is routed but has an invalid IRQ", __func__)); 1071 ACPI_SERIAL_END(pci_link); 1072 return (link->l_irq); 1073 } 1074 1075 /* Choose an IRQ if we need one. */ 1076 if (!PCI_INTERRUPT_VALID(link->l_irq)) { 1077 link->l_irq = acpi_pci_link_choose_irq(dev, link); 1078 1079 /* 1080 * Try to route the interrupt we picked. If it fails, then 1081 * assume the interrupt is not routed. 1082 */ 1083 if (PCI_INTERRUPT_VALID(link->l_irq)) { 1084 acpi_pci_link_route_irqs(dev); 1085 if (!link->l_routed) 1086 link->l_irq = PCI_INVALID_IRQ; 1087 } 1088 } 1089 ACPI_SERIAL_END(pci_link); 1090 1091 return (link->l_irq); 1092 } 1093 1094 /* 1095 * This is gross, but we abuse the identify routine to perform one-time 1096 * SYSINIT() style initialization for the driver. 1097 */ 1098 static void 1099 acpi_pci_link_identify(driver_t *driver, device_t parent) 1100 { 1101 1102 /* 1103 * If the SCI is an ISA IRQ, add it to the bitmask of known good 1104 * ISA IRQs. 1105 * 1106 * XXX: If we are using the APIC, the SCI might have been 1107 * rerouted to an APIC pin in which case this is invalid. However, 1108 * if we are using the APIC, we also shouldn't be having any PCI 1109 * interrupts routed via ISA IRQs, so this is probably ok. 1110 */ 1111 if (AcpiGbl_FADT.SciInterrupt < NUM_ISA_INTERRUPTS) 1112 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT.SciInterrupt); 1113 } 1114 1115 static device_method_t acpi_pci_link_methods[] = { 1116 /* Device interface */ 1117 DEVMETHOD(device_identify, acpi_pci_link_identify), 1118 DEVMETHOD(device_probe, acpi_pci_link_probe), 1119 DEVMETHOD(device_attach, acpi_pci_link_attach), 1120 DEVMETHOD(device_resume, acpi_pci_link_resume), 1121 1122 DEVMETHOD_END 1123 }; 1124 1125 static driver_t acpi_pci_link_driver = { 1126 "pci_link", 1127 acpi_pci_link_methods, 1128 sizeof(struct acpi_pci_link_softc), 1129 }; 1130 1131 DRIVER_MODULE(acpi_pci_link, acpi, acpi_pci_link_driver, 0, 0); 1132 MODULE_DEPEND(acpi_pci_link, acpi, 1, 1, 1); 1133