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