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