1 /* 2 * Copyright (c) 2002 Mitsuru IWASAKI <iwasaki@jp.kfreebsd.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 * $FreeBSD: src/sys/dev/acpica/acpi_pci_link.c,v 1.56.2.1.6.1 2009/04/15 03:14:26 kensmith Exp $ 27 */ 28 29 #include "opt_acpi.h" 30 #include <sys/param.h> 31 #include <sys/bus.h> 32 #include <sys/kernel.h> 33 #include <sys/limits.h> 34 #include <sys/malloc.h> 35 #include <sys/module.h> 36 37 #include "acpi.h" 38 #include <dev/acpica/acpivar.h> 39 #include <dev/acpica/acpi_pcibvar.h> 40 #include <dev/acpica/acpi_sci_var.h> 41 42 #include <bus/pci/i386/pci_cfgreg.h> 43 #include <bus/pci/pcireg.h> 44 #include <bus/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 ksnprintf(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 342 link->l_irqs = kmalloc(sizeof(int) * link->l_num_irqs, 343 M_PCI_LINK, M_WAITOK | M_ZERO); 344 for (i = 0; i < link->l_num_irqs; i++) { 345 if (is_ext_irq) { 346 link->l_irqs[i] = ext_irqs[i]; 347 if (ext_irqs[i] >= NUM_ISA_INTERRUPTS) 348 link->l_isa_irq = FALSE; 349 } else { 350 link->l_irqs[i] = irqs[i]; 351 if (irqs[i] >= NUM_ISA_INTERRUPTS) 352 link->l_isa_irq = FALSE; 353 } 354 } 355 break; 356 default: 357 if (req->in_dpf == DPF_IGNORE) 358 break; 359 if (req->sc->pl_crs_bad) 360 device_printf(req->sc->pl_dev, 361 "Warning: possible resource %d will be lost during _SRS\n", 362 req->res_index); 363 req->res_index++; 364 } 365 return (AE_OK); 366 } 367 368 static int 369 link_valid_irq(struct link *link, int irq) 370 { 371 int i; 372 373 ACPI_SERIAL_ASSERT(pci_link); 374 375 /* Invalid interrupts are never valid. */ 376 if (!PCI_INTERRUPT_VALID(irq)) 377 return (FALSE); 378 379 /* Any interrupt in the list of possible interrupts is valid. */ 380 for (i = 0; i < link->l_num_irqs; i++) 381 if (link->l_irqs[i] == irq) 382 return (TRUE); 383 384 /* 385 * For links routed via an ISA interrupt, if the SCI is routed via 386 * an ISA interrupt, the SCI is always treated as a valid IRQ. 387 */ 388 if (link->l_isa_irq && AcpiGbl_FADT.SciInterrupt == irq && 389 irq < NUM_ISA_INTERRUPTS) 390 return (TRUE); 391 392 /* If the interrupt wasn't found in the list it is not valid. */ 393 return (FALSE); 394 } 395 396 static void 397 acpi_pci_link_dump(struct acpi_pci_link_softc *sc, int header, const char *tag) 398 { 399 struct link *link; 400 char buf[16]; 401 int i, j; 402 403 ACPI_SERIAL_ASSERT(pci_link); 404 if (header) { 405 ksnprintf(buf, sizeof(buf), "%s:", 406 device_get_nameunit(sc->pl_dev)); 407 kprintf("%-16.16s Index IRQ Rtd Ref IRQs\n", buf); 408 } 409 for (i = 0; i < sc->pl_num_links; i++) { 410 link = &sc->pl_links[i]; 411 kprintf(" %-14.14s %5d %3d %c %3d ", i == 0 ? tag : "", i, 412 link->l_irq, link->l_routed ? 'Y' : 'N', 413 link->l_references); 414 if (link->l_num_irqs == 0) 415 kprintf(" none"); 416 else for (j = 0; j < link->l_num_irqs; j++) 417 kprintf(" %d", link->l_irqs[j]); 418 kprintf("\n"); 419 } 420 } 421 422 static int 423 acpi_pci_link_attach(device_t dev) 424 { 425 struct acpi_pci_link_softc *sc; 426 struct link_count_request creq; 427 struct link_res_request rreq; 428 ACPI_STATUS status; 429 int i; 430 431 sc = device_get_softc(dev); 432 sc->pl_dev = dev; 433 ACPI_SERIAL_INIT(pci_link); 434 ACPI_SERIAL_BEGIN(pci_link); 435 436 /* 437 * Count the number of current resources so we know how big of 438 * a link array to allocate. On some systems, _CRS is broken, 439 * so for those systems try to derive the count from _PRS instead. 440 */ 441 creq.in_dpf = DPF_OUTSIDE; 442 creq.count = 0; 443 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS", 444 acpi_count_irq_resources, &creq); 445 sc->pl_crs_bad = ACPI_FAILURE(status); 446 if (sc->pl_crs_bad) { 447 creq.in_dpf = DPF_OUTSIDE; 448 creq.count = 0; 449 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS", 450 acpi_count_irq_resources, &creq); 451 if (ACPI_FAILURE(status)) { 452 device_printf(dev, 453 "Unable to parse _CRS or _PRS: %s\n", 454 AcpiFormatException(status)); 455 ACPI_SERIAL_END(pci_link); 456 return (ENXIO); 457 } 458 } 459 sc->pl_num_links = creq.count; 460 if (creq.count == 0) { 461 ACPI_SERIAL_END(pci_link); 462 return (0); 463 } 464 sc->pl_links = kmalloc(sizeof(struct link) * sc->pl_num_links, 465 M_PCI_LINK, M_WAITOK | M_ZERO); 466 467 /* Initialize the child links. */ 468 for (i = 0; i < sc->pl_num_links; i++) { 469 sc->pl_links[i].l_irq = PCI_INVALID_IRQ; 470 sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ; 471 sc->pl_links[i].l_sc = sc; 472 sc->pl_links[i].l_isa_irq = FALSE; 473 sc->pl_links[i].l_res_index = -1; 474 } 475 476 /* Try to read the current settings from _CRS if it is valid. */ 477 if (!sc->pl_crs_bad) { 478 rreq.in_dpf = DPF_OUTSIDE; 479 rreq.link_index = 0; 480 rreq.res_index = 0; 481 rreq.sc = sc; 482 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS", 483 link_add_crs, &rreq); 484 if (ACPI_FAILURE(status)) { 485 device_printf(dev, "Unable to parse _CRS: %s\n", 486 AcpiFormatException(status)); 487 goto fail; 488 } 489 } 490 491 /* 492 * Try to read the possible settings from _PRS. Note that if the 493 * _CRS is toast, we depend on having a working _PRS. However, if 494 * _CRS works, then it is ok for _PRS to be missing. 495 */ 496 rreq.in_dpf = DPF_OUTSIDE; 497 rreq.link_index = 0; 498 rreq.res_index = 0; 499 rreq.sc = sc; 500 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS", 501 link_add_prs, &rreq); 502 if (ACPI_FAILURE(status) && 503 (status != AE_NOT_FOUND || sc->pl_crs_bad)) { 504 device_printf(dev, "Unable to parse _PRS: %s\n", 505 AcpiFormatException(status)); 506 goto fail; 507 } 508 if (bootverbose) 509 acpi_pci_link_dump(sc, 1, "Initial Probe"); 510 511 /* Verify initial IRQs if we have _PRS. */ 512 if (status != AE_NOT_FOUND) 513 for (i = 0; i < sc->pl_num_links; i++) 514 if (!link_valid_irq(&sc->pl_links[i], 515 sc->pl_links[i].l_irq)) 516 sc->pl_links[i].l_irq = PCI_INVALID_IRQ; 517 if (bootverbose) 518 acpi_pci_link_dump(sc, 0, "Validation"); 519 520 /* Save initial IRQs. */ 521 for (i = 0; i < sc->pl_num_links; i++) 522 sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq; 523 524 /* 525 * Try to disable this link. If successful, set the current IRQ to 526 * zero and flags to indicate this link is not routed. If we can't 527 * run _DIS (i.e., the method doesn't exist), assume the initial 528 * IRQ was routed by the BIOS. 529 */ 530 if (ACPI_SUCCESS(AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL, 531 NULL))) 532 for (i = 0; i < sc->pl_num_links; i++) 533 sc->pl_links[i].l_irq = PCI_INVALID_IRQ; 534 else 535 for (i = 0; i < sc->pl_num_links; i++) 536 if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq)) 537 sc->pl_links[i].l_routed = TRUE; 538 if (bootverbose) 539 acpi_pci_link_dump(sc, 0, "After Disable"); 540 ACPI_SERIAL_END(pci_link); 541 return (0); 542 fail: 543 ACPI_SERIAL_END(pci_link); 544 for (i = 0; i < sc->pl_num_links; i++) 545 if (sc->pl_links[i].l_irqs != NULL) 546 kfree(sc->pl_links[i].l_irqs, M_PCI_LINK); 547 kfree(sc->pl_links, M_PCI_LINK); 548 return (ENXIO); 549 } 550 551 /* XXX: Note that this is identical to pci_pir_search_irq(). */ 552 static uint8_t 553 acpi_pci_link_search_irq(int bus, int device, int pin) 554 { 555 uint32_t value; 556 uint8_t func, maxfunc; 557 558 /* See if we have a valid device at function 0. */ 559 value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1); 560 if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE) 561 return (PCI_INVALID_IRQ); 562 if (value & PCIM_MFDEV) 563 maxfunc = PCI_FUNCMAX; 564 else 565 maxfunc = 0; 566 567 /* Scan all possible functions at this device. */ 568 for (func = 0; func <= maxfunc; func++) { 569 value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4); 570 if (value == 0xffffffff) 571 continue; 572 value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1); 573 574 /* 575 * See if it uses the pin in question. Note that the passed 576 * in pin uses 0 for A, .. 3 for D whereas the intpin 577 * register uses 0 for no interrupt, 1 for A, .. 4 for D. 578 */ 579 if (value != pin + 1) 580 continue; 581 value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1); 582 if (bootverbose) 583 kprintf( 584 "ACPI: Found matching pin for %d.%d.INT%c at func %d: %d\n", 585 bus, device, pin + 'A', func, value); 586 if (value != PCI_INVALID_IRQ) 587 return (value); 588 } 589 return (PCI_INVALID_IRQ); 590 } 591 592 /* 593 * Find the link structure that corresponds to the resource index passed in 594 * via 'source_index'. 595 */ 596 static struct link * 597 acpi_pci_link_lookup(device_t dev, int source_index) 598 { 599 struct acpi_pci_link_softc *sc; 600 int i; 601 602 ACPI_SERIAL_ASSERT(pci_link); 603 sc = device_get_softc(dev); 604 for (i = 0; i < sc->pl_num_links; i++) 605 if (sc->pl_links[i].l_res_index == source_index) 606 return (&sc->pl_links[i]); 607 return (NULL); 608 } 609 610 void 611 acpi_pci_link_add_reference(device_t dev, int index, device_t pcib, int slot, 612 int pin) 613 { 614 struct link *link; 615 uint8_t bios_irq; 616 uintptr_t bus; 617 618 /* 619 * Look up the PCI bus for the specified PCI bridge device. Note 620 * that the PCI bridge device might not have any children yet. 621 * However, looking up its bus number doesn't require a valid child 622 * device, so we just pass NULL. 623 */ 624 if (BUS_READ_IVAR(pcib, NULL, PCIB_IVAR_BUS, &bus) != 0) { 625 device_printf(pcib, "Unable to read PCI bus number"); 626 panic("PCI bridge without a bus number"); 627 } 628 629 /* Bump the reference count. */ 630 ACPI_SERIAL_BEGIN(pci_link); 631 link = acpi_pci_link_lookup(dev, index); 632 if (link == NULL) { 633 device_printf(dev, "apparently invalid index %d\n", index); 634 ACPI_SERIAL_END(pci_link); 635 return; 636 } 637 link->l_references++; 638 if (link->l_routed) 639 pci_link_interrupt_weights[link->l_irq]++; 640 641 /* 642 * The BIOS only routes interrupts via ISA IRQs using the ATPICs 643 * (8259As). Thus, if this link is routed via an ISA IRQ, go 644 * look to see if the BIOS routed an IRQ for this link at the 645 * indicated (bus, slot, pin). If so, we prefer that IRQ for 646 * this link and add that IRQ to our list of known-good IRQs. 647 * This provides a good work-around for link devices whose _CRS 648 * method is either broken or bogus. We only use the value 649 * returned by _CRS if we can't find a valid IRQ via this method 650 * in fact. 651 * 652 * If this link is not routed via an ISA IRQ (because we are using 653 * APIC for example), then don't bother looking up the BIOS IRQ 654 * as if we find one it won't be valid anyway. 655 */ 656 if (!link->l_isa_irq) { 657 ACPI_SERIAL_END(pci_link); 658 return; 659 } 660 661 /* Try to find a BIOS IRQ setting from any matching devices. */ 662 bios_irq = acpi_pci_link_search_irq(bus, slot, pin); 663 if (!PCI_INTERRUPT_VALID(bios_irq)) { 664 ACPI_SERIAL_END(pci_link); 665 return; 666 } 667 668 /* Validate the BIOS IRQ. */ 669 if (!link_valid_irq(link, bios_irq)) { 670 device_printf(dev, "BIOS IRQ %u for %d.%d.INT%c is invalid\n", 671 bios_irq, (int)bus, slot, pin + 'A'); 672 } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) { 673 link->l_bios_irq = bios_irq; 674 /* 675 * SCI setting is handled by acpi_pci_link_identify() 676 */ 677 if (bios_irq < NUM_ISA_INTERRUPTS && 678 AcpiGbl_FADT.SciInterrupt != bios_irq) 679 pci_link_bios_isa_irqs |= (1 << bios_irq); 680 if (bios_irq != link->l_initial_irq && 681 PCI_INTERRUPT_VALID(link->l_initial_irq)) 682 device_printf(dev, 683 "BIOS IRQ %u does not match initial IRQ %u\n", 684 bios_irq, link->l_initial_irq); 685 } else if (bios_irq != link->l_bios_irq) 686 device_printf(dev, 687 "BIOS IRQ %u for %d.%d.INT%c does not match previous BIOS IRQ %u\n", 688 bios_irq, (int)bus, slot, pin + 'A', 689 link->l_bios_irq); 690 ACPI_SERIAL_END(pci_link); 691 } 692 693 static ACPI_STATUS 694 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf) 695 { 696 ACPI_RESOURCE *resource, *end, newres, *resptr; 697 ACPI_BUFFER crsbuf; 698 ACPI_STATUS status; 699 struct link *link; 700 int i, in_dpf; 701 702 /* Fetch the _CRS. */ 703 ACPI_SERIAL_ASSERT(pci_link); 704 crsbuf.Pointer = NULL; 705 crsbuf.Length = ACPI_ALLOCATE_BUFFER; 706 status = AcpiGetCurrentResources(acpi_get_handle(sc->pl_dev), &crsbuf); 707 if (ACPI_SUCCESS(status) && crsbuf.Pointer == NULL) 708 status = AE_NO_MEMORY; 709 if (ACPI_FAILURE(status)) { 710 if (bootverbose) 711 device_printf(sc->pl_dev, 712 "Unable to fetch current resources: %s\n", 713 AcpiFormatException(status)); 714 return (status); 715 } 716 717 /* Fill in IRQ resources via link structures. */ 718 srsbuf->Pointer = NULL; 719 link = sc->pl_links; 720 i = 0; 721 in_dpf = DPF_OUTSIDE; 722 resource = (ACPI_RESOURCE *)crsbuf.Pointer; 723 end = (ACPI_RESOURCE *)((char *)crsbuf.Pointer + crsbuf.Length); 724 for (;;) { 725 switch (resource->Type) { 726 case ACPI_RESOURCE_TYPE_START_DEPENDENT: 727 switch (in_dpf) { 728 case DPF_OUTSIDE: 729 /* We've started the first DPF. */ 730 in_dpf = DPF_FIRST; 731 break; 732 case DPF_FIRST: 733 /* We've started the second DPF. */ 734 panic( 735 "%s: Multiple dependent functions within a current resource", 736 __func__); 737 break; 738 } 739 resptr = NULL; 740 break; 741 case ACPI_RESOURCE_TYPE_END_DEPENDENT: 742 /* We are finished with DPF parsing. */ 743 KASSERT(in_dpf != DPF_OUTSIDE, 744 ("%s: end dpf when not parsing a dpf", __func__)); 745 in_dpf = DPF_OUTSIDE; 746 resptr = NULL; 747 break; 748 case ACPI_RESOURCE_TYPE_IRQ: 749 KKASSERT(i < sc->pl_num_links); 750 KKASSERT(link->l_prs_template.Type == ACPI_RESOURCE_TYPE_IRQ); 751 newres = link->l_prs_template; 752 resptr = &newres; 753 resptr->Data.Irq.InterruptCount = 1; 754 if (PCI_INTERRUPT_VALID(link->l_irq)) { 755 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS, 756 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type", 757 __func__, link->l_irq)); 758 resptr->Data.Irq.Interrupts[0] = link->l_irq; 759 } else 760 resptr->Data.Irq.Interrupts[0] = 0; 761 link++; 762 i++; 763 break; 764 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 765 KKASSERT(i < sc->pl_num_links); 766 KKASSERT(link->l_prs_template.Type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ); 767 newres = link->l_prs_template; 768 resptr = &newres; 769 resptr->Data.ExtendedIrq.InterruptCount = 1; 770 if (PCI_INTERRUPT_VALID(link->l_irq)) 771 resptr->Data.ExtendedIrq.Interrupts[0] = 772 link->l_irq; 773 else 774 resptr->Data.ExtendedIrq.Interrupts[0] = 0; 775 link++; 776 i++; 777 break; 778 default: 779 resptr = resource; 780 } 781 if (resptr != NULL) { 782 status = acpi_AppendBufferResource(srsbuf, resptr); 783 if (ACPI_FAILURE(status)) { 784 device_printf(sc->pl_dev, 785 "Unable to build resources: %s\n", 786 AcpiFormatException(status)); 787 if (srsbuf->Pointer != NULL) 788 AcpiOsFree(srsbuf->Pointer); 789 AcpiOsFree(crsbuf.Pointer); 790 return (status); 791 } 792 } 793 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG) 794 break; 795 resource = ACPI_NEXT_RESOURCE(resource); 796 if (resource >= end) 797 break; 798 } 799 AcpiOsFree(crsbuf.Pointer); 800 return (AE_OK); 801 } 802 803 static ACPI_STATUS 804 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc, 805 ACPI_BUFFER *srsbuf) 806 { 807 ACPI_RESOURCE newres; 808 ACPI_STATUS status; 809 struct link *link; 810 int i; 811 812 /* Start off with an empty buffer. */ 813 srsbuf->Pointer = NULL; 814 link = sc->pl_links; 815 for (i = 0; i < sc->pl_num_links; i++) { 816 817 /* Add a new IRQ resource from each link. */ 818 link = &sc->pl_links[i]; 819 newres = link->l_prs_template; 820 if (newres.Type == ACPI_RESOURCE_TYPE_IRQ) { 821 822 /* Build an IRQ resource. */ 823 newres.Data.Irq.InterruptCount = 1; 824 if (PCI_INTERRUPT_VALID(link->l_irq)) { 825 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS, 826 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type", 827 __func__, link->l_irq)); 828 newres.Data.Irq.Interrupts[0] = link->l_irq; 829 } else 830 newres.Data.Irq.Interrupts[0] = 0; 831 } else { 832 833 /* Build an ExtIRQ resuorce. */ 834 newres.Data.ExtendedIrq.InterruptCount = 1; 835 if (PCI_INTERRUPT_VALID(link->l_irq)) 836 newres.Data.ExtendedIrq.Interrupts[0] = 837 link->l_irq; 838 else 839 newres.Data.ExtendedIrq.Interrupts[0] = 0; 840 } 841 842 /* Add the new resource to the end of the _SRS buffer. */ 843 status = acpi_AppendBufferResource(srsbuf, &newres); 844 if (ACPI_FAILURE(status)) { 845 device_printf(sc->pl_dev, 846 "Unable to build resources: %s\n", 847 AcpiFormatException(status)); 848 if (srsbuf->Pointer != NULL) 849 AcpiOsFree(srsbuf->Pointer); 850 return (status); 851 } 852 } 853 return (AE_OK); 854 } 855 856 static ACPI_STATUS 857 acpi_pci_link_route_irqs(device_t dev) 858 { 859 struct acpi_pci_link_softc *sc; 860 ACPI_RESOURCE *resource, *end; 861 ACPI_BUFFER srsbuf; 862 ACPI_STATUS status; 863 struct link *link; 864 int i; 865 866 ACPI_SERIAL_ASSERT(pci_link); 867 sc = device_get_softc(dev); 868 if (sc->pl_crs_bad) 869 status = acpi_pci_link_srs_from_links(sc, &srsbuf); 870 else 871 status = acpi_pci_link_srs_from_crs(sc, &srsbuf); 872 873 /* Write out new resources via _SRS. */ 874 status = AcpiSetCurrentResources(acpi_get_handle(dev), &srsbuf); 875 if (ACPI_FAILURE(status)) { 876 device_printf(dev, "Unable to route IRQs: %s\n", 877 AcpiFormatException(status)); 878 AcpiOsFree(srsbuf.Pointer); 879 return (status); 880 } 881 882 /* 883 * Perform acpi_config_intr() on each IRQ resource if it was just 884 * routed for the first time. 885 */ 886 link = sc->pl_links; 887 i = 0; 888 resource = (ACPI_RESOURCE *)srsbuf.Pointer; 889 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length); 890 for (;;) { 891 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG) 892 break; 893 switch (resource->Type) { 894 case ACPI_RESOURCE_TYPE_IRQ: 895 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: 896 KKASSERT(i < sc->pl_num_links); 897 898 /* 899 * Only configure the interrupt and update the 900 * weights if this link has a valid IRQ and was 901 * previously unrouted. 902 */ 903 if (!link->l_routed && 904 PCI_INTERRUPT_VALID(link->l_irq)) { 905 link->l_routed = TRUE; 906 acpi_config_intr(dev, resource); 907 pci_link_interrupt_weights[link->l_irq] += 908 link->l_references; 909 } 910 link++; 911 i++; 912 break; 913 } 914 resource = ACPI_NEXT_RESOURCE(resource); 915 if (resource >= end) 916 break; 917 } 918 AcpiOsFree(srsbuf.Pointer); 919 return (AE_OK); 920 } 921 922 static int 923 acpi_pci_link_resume(device_t dev) 924 { 925 struct acpi_pci_link_softc *sc; 926 ACPI_STATUS status; 927 int i, routed; 928 929 /* 930 * If all of our links are routed, then restore the link via _SRS, 931 * otherwise, disable the link via _DIS. 932 */ 933 ACPI_SERIAL_BEGIN(pci_link); 934 sc = device_get_softc(dev); 935 routed = 0; 936 for (i = 0; i < sc->pl_num_links; i++) 937 if (sc->pl_links[i].l_routed) 938 routed++; 939 if (routed == sc->pl_num_links) 940 status = acpi_pci_link_route_irqs(dev); 941 else { 942 AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL, NULL); 943 status = AE_OK; 944 } 945 ACPI_SERIAL_END(pci_link); 946 if (ACPI_FAILURE(status)) 947 return (ENXIO); 948 else 949 return (0); 950 } 951 952 /* 953 * Pick an IRQ to use for this unrouted link. 954 */ 955 static uint8_t 956 acpi_pci_link_choose_irq(device_t dev, struct link *link) 957 { 958 char tunable_buffer[64], link_name[5]; 959 u_int8_t best_irq, pos_irq; 960 int best_weight, pos_weight, i; 961 962 KASSERT(!link->l_routed, ("%s: link already routed", __func__)); 963 KASSERT(!PCI_INTERRUPT_VALID(link->l_irq), 964 ("%s: link already has an IRQ", __func__)); 965 966 /* Check for a tunable override. */ 967 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), link_name, 968 sizeof(link_name)))) { 969 ksnprintf(tunable_buffer, sizeof(tunable_buffer), 970 "hw.pci.link.%s.%d.irq", link_name, link->l_res_index); 971 if (kgetenv_int(tunable_buffer, &i) && PCI_INTERRUPT_VALID(i)) { 972 if (!link_valid_irq(link, i)) 973 device_printf(dev, 974 "Warning, IRQ %d is not listed as valid\n", 975 i); 976 return (i); 977 } 978 ksnprintf(tunable_buffer, sizeof(tunable_buffer), 979 "hw.pci.link.%s.irq", link_name); 980 if (kgetenv_int(tunable_buffer, &i) && PCI_INTERRUPT_VALID(i)) { 981 if (!link_valid_irq(link, i)) 982 device_printf(dev, 983 "Warning, IRQ %d is not listed as valid\n", 984 i); 985 return (i); 986 } 987 } 988 989 /* 990 * If we have a valid BIOS IRQ, use that. We trust what the BIOS 991 * says it routed over what _CRS says the link thinks is routed. 992 */ 993 if (PCI_INTERRUPT_VALID(link->l_bios_irq)) 994 return (link->l_bios_irq); 995 996 /* 997 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS, 998 * then use that. 999 */ 1000 if (PCI_INTERRUPT_VALID(link->l_initial_irq)) 1001 return (link->l_initial_irq); 1002 1003 /* 1004 * Ok, we have no useful hints, so we have to pick from the 1005 * possible IRQs. For ISA IRQs we only use interrupts that 1006 * have already been used by the BIOS. 1007 */ 1008 best_irq = PCI_INVALID_IRQ; 1009 best_weight = INT_MAX; 1010 for (i = 0; i < link->l_num_irqs; i++) { 1011 pos_irq = link->l_irqs[i]; 1012 if (pos_irq < NUM_ISA_INTERRUPTS && 1013 (pci_link_bios_isa_irqs & 1 << pos_irq) == 0) 1014 continue; 1015 pos_weight = pci_link_interrupt_weights[pos_irq]; 1016 if (pos_weight < best_weight) { 1017 best_weight = pos_weight; 1018 best_irq = pos_irq; 1019 } 1020 } 1021 1022 /* 1023 * If this is an ISA IRQ and SCI could be shared, try using 1024 * the SCI as a fallback. 1025 */ 1026 if (link->l_isa_irq && acpi_sci_pci_shariable()) { 1027 pos_irq = AcpiGbl_FADT.SciInterrupt; 1028 pos_weight = pci_link_interrupt_weights[pos_irq]; 1029 if (pos_weight < best_weight) { 1030 best_weight = pos_weight; 1031 best_irq = pos_irq; 1032 } 1033 } 1034 1035 if (PCI_INTERRUPT_VALID(best_irq)) { 1036 if (bootverbose) 1037 device_printf(dev, "Picked IRQ %u with weight %d\n", 1038 best_irq, best_weight); 1039 } else 1040 device_printf(dev, "Unable to choose an IRQ\n"); 1041 return (best_irq); 1042 } 1043 1044 int 1045 acpi_pci_link_route_interrupt(device_t dev, int index) 1046 { 1047 struct link *link; 1048 1049 if (acpi_disabled("pci_link")) 1050 return (PCI_INVALID_IRQ); 1051 1052 ACPI_SERIAL_BEGIN(pci_link); 1053 link = acpi_pci_link_lookup(dev, index); 1054 if (link == NULL) 1055 panic("%s: apparently invalid index %d", __func__, index); 1056 1057 /* 1058 * If this link device is already routed to an interrupt, just return 1059 * the interrupt it is routed to. 1060 */ 1061 if (link->l_routed) { 1062 KASSERT(PCI_INTERRUPT_VALID(link->l_irq), 1063 ("%s: link is routed but has an invalid IRQ", __func__)); 1064 ACPI_SERIAL_END(pci_link); 1065 return (link->l_irq); 1066 } 1067 1068 /* Choose an IRQ if we need one. */ 1069 if (!PCI_INTERRUPT_VALID(link->l_irq)) { 1070 link->l_irq = acpi_pci_link_choose_irq(dev, link); 1071 1072 /* 1073 * Try to route the interrupt we picked. If it fails, then 1074 * assume the interrupt is not routed. 1075 */ 1076 if (PCI_INTERRUPT_VALID(link->l_irq)) { 1077 acpi_pci_link_route_irqs(dev); 1078 if (!link->l_routed) 1079 link->l_irq = PCI_INVALID_IRQ; 1080 } 1081 } 1082 ACPI_SERIAL_END(pci_link); 1083 return (link->l_irq); 1084 } 1085 1086 /* 1087 * This is gross, but we abuse the identify routine to perform one-time 1088 * SYSINIT() style initialization for the driver. 1089 */ 1090 static void 1091 acpi_pci_link_identify(driver_t *driver, device_t parent) 1092 { 1093 /* 1094 * If the SCI is an ISA IRQ and could be shared, 1095 * add it to the bitmask of known good ISA IRQs. 1096 */ 1097 if (AcpiGbl_FADT.SciInterrupt < NUM_ISA_INTERRUPTS && 1098 acpi_sci_pci_shariable()) 1099 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT.SciInterrupt); 1100 } 1101 1102 static device_method_t acpi_pci_link_methods[] = { 1103 /* Device interface */ 1104 DEVMETHOD(device_identify, acpi_pci_link_identify), 1105 DEVMETHOD(device_probe, acpi_pci_link_probe), 1106 DEVMETHOD(device_attach, acpi_pci_link_attach), 1107 DEVMETHOD(device_resume, acpi_pci_link_resume), 1108 1109 DEVMETHOD_END 1110 }; 1111 1112 static driver_t acpi_pci_link_driver = { 1113 "pci_link", 1114 acpi_pci_link_methods, 1115 sizeof(struct acpi_pci_link_softc), 1116 }; 1117 1118 static devclass_t pci_link_devclass; 1119 1120 DRIVER_MODULE(acpi_pci_link, acpi, acpi_pci_link_driver, pci_link_devclass, 1121 NULL, NULL); 1122 MODULE_DEPEND(acpi_pci_link, acpi, 1, 1, 1); 1123