2 * Copyright (c) 2002 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/limits.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
38 #include <contrib/dev/acpica/include/acpi.h>
40 #include <dev/acpica/acpivar.h>
41 #include <dev/acpica/acpi_pcibvar.h>
43 #include <machine/pci_cfgreg.h>
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pcivar.h>
48 /* Hooks for the ACPI CA debugging infrastructure. */
49 #define _COMPONENT ACPI_BUS
50 ACPI_MODULE_NAME("PCI_LINK")
52 ACPI_SERIAL_DECL(pci_link, "ACPI PCI link");
54 #define NUM_ISA_INTERRUPTS 16
55 #define NUM_ACPI_INTERRUPTS 256
58 * An ACPI PCI link device may contain multiple links. Each link has its
59 * own ACPI resource. _PRT entries specify which link is being used via
62 * XXX: A note about Source Indices and DPFs: Currently we assume that
63 * the DPF start and end tags are not counted towards the index that
64 * Source Index corresponds to. Also, we assume that when DPFs are in use
65 * they various sets overlap in terms of Indices. Here's an example
66 * resource list indicating these assumptions:
80 * The XXX is because I'm not sure if this is a valid assumption to make.
83 /* States during DPF processing. */
90 struct acpi_pci_link_softc {
93 struct link *pl_links;
98 struct acpi_pci_link_softc *l_sc;
101 uint8_t l_initial_irq;
108 ACPI_RESOURCE l_prs_template;
111 struct link_count_request {
116 struct link_res_request {
117 struct acpi_pci_link_softc *sc;
123 MALLOC_DEFINE(M_PCI_LINK, "pci_link", "ACPI PCI Link structures");
125 static int pci_link_interrupt_weights[NUM_ACPI_INTERRUPTS];
126 static int pci_link_bios_isa_irqs;
128 static char *pci_link_ids[] = { "PNP0C0F", NULL };
131 * Fetch the short name associated with an ACPI handle and save it in the
135 acpi_short_name(ACPI_HANDLE handle, char *buffer, size_t buflen)
140 buf.Pointer = buffer;
141 return (AcpiGetName(handle, ACPI_SINGLE_NAME, &buf));
145 acpi_pci_link_probe(device_t dev)
147 char descr[28], name[12];
150 * We explicitly do not check _STA since not all systems set it to
153 if (acpi_disabled("pci_link") ||
154 ACPI_ID_PROBE(device_get_parent(dev), dev, pci_link_ids) == NULL)
157 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), name,
159 snprintf(descr, sizeof(descr), "ACPI PCI Link %s", name);
160 device_set_desc_copy(dev, descr);
162 device_set_desc(dev, "ACPI PCI Link");
168 acpi_count_irq_resources(ACPI_RESOURCE *res, void *context)
170 struct link_count_request *req;
172 req = (struct link_count_request *)context;
174 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
175 switch (req->in_dpf) {
177 /* We've started the first DPF. */
178 req->in_dpf = DPF_FIRST;
181 /* We've started the second DPF. */
182 req->in_dpf = DPF_IGNORE;
186 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
187 /* We are finished with DPF parsing. */
188 KASSERT(req->in_dpf != DPF_OUTSIDE,
189 ("%s: end dpf when not parsing a dpf", __func__));
190 req->in_dpf = DPF_OUTSIDE;
192 case ACPI_RESOURCE_TYPE_IRQ:
193 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
195 * Don't count resources if we are in a DPF set that we are
198 if (req->in_dpf != DPF_IGNORE)
205 link_add_crs(ACPI_RESOURCE *res, void *context)
207 struct link_res_request *req;
210 ACPI_SERIAL_ASSERT(pci_link);
211 req = (struct link_res_request *)context;
213 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
214 switch (req->in_dpf) {
216 /* We've started the first DPF. */
217 req->in_dpf = DPF_FIRST;
220 /* We've started the second DPF. */
222 "%s: Multiple dependent functions within a current resource",
227 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
228 /* We are finished with DPF parsing. */
229 KASSERT(req->in_dpf != DPF_OUTSIDE,
230 ("%s: end dpf when not parsing a dpf", __func__));
231 req->in_dpf = DPF_OUTSIDE;
233 case ACPI_RESOURCE_TYPE_IRQ:
234 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
235 KASSERT(req->link_index < req->sc->pl_num_links,
236 ("%s: array boundary violation", __func__));
237 link = &req->sc->pl_links[req->link_index];
238 link->l_res_index = req->res_index;
243 * Only use the current value if there's one IRQ. Some
244 * systems return multiple IRQs (which is nonsense for _CRS)
245 * when the link hasn't been programmed.
247 if (res->Type == ACPI_RESOURCE_TYPE_IRQ) {
248 if (res->Data.Irq.InterruptCount == 1)
249 link->l_irq = res->Data.Irq.Interrupts[0];
250 } else if (res->Data.ExtendedIrq.InterruptCount == 1)
251 link->l_irq = res->Data.ExtendedIrq.Interrupts[0];
254 * An IRQ of zero means that the link isn't routed.
256 if (link->l_irq == 0)
257 link->l_irq = PCI_INVALID_IRQ;
266 * Populate the set of possible IRQs for each device.
269 link_add_prs(ACPI_RESOURCE *res, void *context)
272 struct link_res_request *req;
275 UINT32 *ext_irqs = NULL;
276 int i, is_ext_irq = 1;
278 ACPI_SERIAL_ASSERT(pci_link);
279 req = (struct link_res_request *)context;
281 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
282 switch (req->in_dpf) {
284 /* We've started the first DPF. */
285 req->in_dpf = DPF_FIRST;
288 /* We've started the second DPF. */
289 req->in_dpf = DPF_IGNORE;
293 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
294 /* We are finished with DPF parsing. */
295 KASSERT(req->in_dpf != DPF_OUTSIDE,
296 ("%s: end dpf when not parsing a dpf", __func__));
297 req->in_dpf = DPF_OUTSIDE;
299 case ACPI_RESOURCE_TYPE_IRQ:
302 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
304 * Don't parse resources if we are in a DPF set that we are
307 if (req->in_dpf == DPF_IGNORE)
310 KASSERT(req->link_index < req->sc->pl_num_links,
311 ("%s: array boundary violation", __func__));
312 link = &req->sc->pl_links[req->link_index];
313 if (link->l_res_index == -1) {
314 KASSERT(req->sc->pl_crs_bad,
315 ("res_index should be set"));
316 link->l_res_index = req->res_index;
322 * Stash a copy of the resource for later use when doing
325 tmp = &link->l_prs_template;
327 bcopy(res, tmp, ACPI_RS_SIZE(tmp->Data.ExtendedIrq));
330 * XXX acpi_AppendBufferResource() cannot handle
333 bzero(&tmp->Data.ExtendedIrq.ResourceSource,
334 sizeof(tmp->Data.ExtendedIrq.ResourceSource));
335 tmp->Length = ACPI_RS_SIZE(tmp->Data.ExtendedIrq);
338 res->Data.ExtendedIrq.InterruptCount;
339 ext_irqs = res->Data.ExtendedIrq.Interrupts;
341 bcopy(res, tmp, ACPI_RS_SIZE(tmp->Data.Irq));
342 link->l_num_irqs = res->Data.Irq.InterruptCount;
343 irqs = res->Data.Irq.Interrupts;
345 if (link->l_num_irqs == 0)
349 * Save a list of the valid IRQs. Also, if all of the
350 * valid IRQs are ISA IRQs, then mark this link as
351 * routed via an ISA interrupt.
353 link->l_isa_irq = TRUE;
354 link->l_irqs = malloc(sizeof(int) * link->l_num_irqs,
355 M_PCI_LINK, M_WAITOK | M_ZERO);
356 for (i = 0; i < link->l_num_irqs; i++) {
358 link->l_irqs[i] = ext_irqs[i];
359 if (ext_irqs[i] >= NUM_ISA_INTERRUPTS)
360 link->l_isa_irq = FALSE;
362 link->l_irqs[i] = irqs[i];
363 if (irqs[i] >= NUM_ISA_INTERRUPTS)
364 link->l_isa_irq = FALSE;
369 if (req->in_dpf == DPF_IGNORE)
371 if (req->sc->pl_crs_bad)
372 device_printf(req->sc->pl_dev,
373 "Warning: possible resource %d will be lost during _SRS\n",
381 link_valid_irq(struct link *link, int irq)
385 ACPI_SERIAL_ASSERT(pci_link);
387 /* Invalid interrupts are never valid. */
388 if (!PCI_INTERRUPT_VALID(irq))
391 /* Any interrupt in the list of possible interrupts is valid. */
392 for (i = 0; i < link->l_num_irqs; i++)
393 if (link->l_irqs[i] == irq)
397 * For links routed via an ISA interrupt, if the SCI is routed via
398 * an ISA interrupt, the SCI is always treated as a valid IRQ.
400 if (link->l_isa_irq && AcpiGbl_FADT.SciInterrupt == irq &&
401 irq < NUM_ISA_INTERRUPTS)
404 /* If the interrupt wasn't found in the list it is not valid. */
409 acpi_pci_link_dump(struct acpi_pci_link_softc *sc, int header, const char *tag)
415 ACPI_SERIAL_ASSERT(pci_link);
417 snprintf(buf, sizeof(buf), "%s:",
418 device_get_nameunit(sc->pl_dev));
419 printf("%-16.16s Index IRQ Rtd Ref IRQs\n", buf);
421 for (i = 0; i < sc->pl_num_links; i++) {
422 link = &sc->pl_links[i];
423 printf(" %-14.14s %5d %3d %c %3d ", i == 0 ? tag : "", i,
424 link->l_irq, link->l_routed ? 'Y' : 'N',
426 if (link->l_num_irqs == 0)
428 else for (j = 0; j < link->l_num_irqs; j++)
429 printf(" %d", link->l_irqs[j]);
435 acpi_pci_link_attach(device_t dev)
437 struct acpi_pci_link_softc *sc;
438 struct link_count_request creq;
439 struct link_res_request rreq;
443 sc = device_get_softc(dev);
445 ACPI_SERIAL_BEGIN(pci_link);
448 * Count the number of current resources so we know how big of
449 * a link array to allocate. On some systems, _CRS is broken,
450 * so for those systems try to derive the count from _PRS instead.
452 creq.in_dpf = DPF_OUTSIDE;
454 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
455 acpi_count_irq_resources, &creq);
456 sc->pl_crs_bad = ACPI_FAILURE(status);
457 if (sc->pl_crs_bad) {
458 creq.in_dpf = DPF_OUTSIDE;
460 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
461 acpi_count_irq_resources, &creq);
462 if (ACPI_FAILURE(status)) {
464 "Unable to parse _CRS or _PRS: %s\n",
465 AcpiFormatException(status));
466 ACPI_SERIAL_END(pci_link);
470 sc->pl_num_links = creq.count;
471 if (creq.count == 0) {
472 ACPI_SERIAL_END(pci_link);
475 sc->pl_links = malloc(sizeof(struct link) * sc->pl_num_links,
476 M_PCI_LINK, M_WAITOK | M_ZERO);
478 /* Initialize the child links. */
479 for (i = 0; i < sc->pl_num_links; i++) {
480 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
481 sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ;
482 sc->pl_links[i].l_sc = sc;
483 sc->pl_links[i].l_isa_irq = FALSE;
484 sc->pl_links[i].l_res_index = -1;
487 /* Try to read the current settings from _CRS if it is valid. */
488 if (!sc->pl_crs_bad) {
489 rreq.in_dpf = DPF_OUTSIDE;
493 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
494 link_add_crs, &rreq);
495 if (ACPI_FAILURE(status)) {
496 device_printf(dev, "Unable to parse _CRS: %s\n",
497 AcpiFormatException(status));
503 * Try to read the possible settings from _PRS. Note that if the
504 * _CRS is toast, we depend on having a working _PRS. However, if
505 * _CRS works, then it is ok for _PRS to be missing.
507 rreq.in_dpf = DPF_OUTSIDE;
511 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
512 link_add_prs, &rreq);
513 if (ACPI_FAILURE(status) &&
514 (status != AE_NOT_FOUND || sc->pl_crs_bad)) {
515 device_printf(dev, "Unable to parse _PRS: %s\n",
516 AcpiFormatException(status));
520 acpi_pci_link_dump(sc, 1, "Initial Probe");
522 /* Verify initial IRQs if we have _PRS. */
523 if (status != AE_NOT_FOUND)
524 for (i = 0; i < sc->pl_num_links; i++)
525 if (!link_valid_irq(&sc->pl_links[i],
526 sc->pl_links[i].l_irq))
527 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
529 acpi_pci_link_dump(sc, 0, "Validation");
531 /* Save initial IRQs. */
532 for (i = 0; i < sc->pl_num_links; i++)
533 sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq;
536 * Try to disable this link. If successful, set the current IRQ to
537 * zero and flags to indicate this link is not routed. If we can't
538 * run _DIS (i.e., the method doesn't exist), assume the initial
539 * IRQ was routed by the BIOS.
541 if (ACPI_SUCCESS(AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL,
543 for (i = 0; i < sc->pl_num_links; i++)
544 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
546 for (i = 0; i < sc->pl_num_links; i++)
547 if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq))
548 sc->pl_links[i].l_routed = TRUE;
550 acpi_pci_link_dump(sc, 0, "After Disable");
551 ACPI_SERIAL_END(pci_link);
554 ACPI_SERIAL_END(pci_link);
555 for (i = 0; i < sc->pl_num_links; i++)
556 if (sc->pl_links[i].l_irqs != NULL)
557 free(sc->pl_links[i].l_irqs, M_PCI_LINK);
558 free(sc->pl_links, M_PCI_LINK);
562 /* XXX: Note that this is identical to pci_pir_search_irq(). */
564 acpi_pci_link_search_irq(int bus, int device, int pin)
567 uint8_t func, maxfunc;
569 /* See if we have a valid device at function 0. */
570 value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1);
571 if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
572 return (PCI_INVALID_IRQ);
573 if (value & PCIM_MFDEV)
574 maxfunc = PCI_FUNCMAX;
578 /* Scan all possible functions at this device. */
579 for (func = 0; func <= maxfunc; func++) {
580 value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4);
581 if (value == 0xffffffff)
583 value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
586 * See if it uses the pin in question. Note that the passed
587 * in pin uses 0 for A, .. 3 for D whereas the intpin
588 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
590 if (value != pin + 1)
592 value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
595 "ACPI: Found matching pin for %d.%d.INT%c at func %d: %d\n",
596 bus, device, pin + 'A', func, value);
597 if (value != PCI_INVALID_IRQ)
600 return (PCI_INVALID_IRQ);
604 * Find the link structure that corresponds to the resource index passed in
605 * via 'source_index'.
608 acpi_pci_link_lookup(device_t dev, int source_index)
610 struct acpi_pci_link_softc *sc;
613 ACPI_SERIAL_ASSERT(pci_link);
614 sc = device_get_softc(dev);
615 for (i = 0; i < sc->pl_num_links; i++)
616 if (sc->pl_links[i].l_res_index == source_index)
617 return (&sc->pl_links[i]);
622 acpi_pci_link_add_reference(device_t dev, int index, device_t pcib, int slot,
630 * Look up the PCI bus for the specified PCI bridge device. Note
631 * that the PCI bridge device might not have any children yet.
632 * However, looking up its bus number doesn't require a valid child
633 * device, so we just pass NULL.
635 if (BUS_READ_IVAR(pcib, NULL, PCIB_IVAR_BUS, &bus) != 0) {
636 device_printf(pcib, "Unable to read PCI bus number");
637 panic("PCI bridge without a bus number");
640 /* Bump the reference count. */
641 ACPI_SERIAL_BEGIN(pci_link);
642 link = acpi_pci_link_lookup(dev, index);
644 device_printf(dev, "apparently invalid index %d\n", index);
645 ACPI_SERIAL_END(pci_link);
648 link->l_references++;
650 pci_link_interrupt_weights[link->l_irq]++;
653 * The BIOS only routes interrupts via ISA IRQs using the ATPICs
654 * (8259As). Thus, if this link is routed via an ISA IRQ, go
655 * look to see if the BIOS routed an IRQ for this link at the
656 * indicated (bus, slot, pin). If so, we prefer that IRQ for
657 * this link and add that IRQ to our list of known-good IRQs.
658 * This provides a good work-around for link devices whose _CRS
659 * method is either broken or bogus. We only use the value
660 * returned by _CRS if we can't find a valid IRQ via this method
663 * If this link is not routed via an ISA IRQ (because we are using
664 * APIC for example), then don't bother looking up the BIOS IRQ
665 * as if we find one it won't be valid anyway.
667 if (!link->l_isa_irq) {
668 ACPI_SERIAL_END(pci_link);
672 /* Try to find a BIOS IRQ setting from any matching devices. */
673 bios_irq = acpi_pci_link_search_irq(bus, slot, pin);
674 if (!PCI_INTERRUPT_VALID(bios_irq)) {
675 ACPI_SERIAL_END(pci_link);
679 /* Validate the BIOS IRQ. */
680 if (!link_valid_irq(link, bios_irq)) {
681 device_printf(dev, "BIOS IRQ %u for %d.%d.INT%c is invalid\n",
682 bios_irq, (int)bus, slot, pin + 'A');
683 } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) {
684 link->l_bios_irq = bios_irq;
685 if (bios_irq < NUM_ISA_INTERRUPTS)
686 pci_link_bios_isa_irqs |= (1 << bios_irq);
687 if (bios_irq != link->l_initial_irq &&
688 PCI_INTERRUPT_VALID(link->l_initial_irq))
690 "BIOS IRQ %u does not match initial IRQ %u\n",
691 bios_irq, link->l_initial_irq);
692 } else if (bios_irq != link->l_bios_irq)
694 "BIOS IRQ %u for %d.%d.INT%c does not match previous BIOS IRQ %u\n",
695 bios_irq, (int)bus, slot, pin + 'A',
697 ACPI_SERIAL_END(pci_link);
701 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
703 ACPI_RESOURCE *end, *res;
708 /* Fetch the _CRS. */
709 ACPI_SERIAL_ASSERT(pci_link);
710 srsbuf->Pointer = NULL;
711 srsbuf->Length = ACPI_ALLOCATE_BUFFER;
712 status = AcpiGetCurrentResources(acpi_get_handle(sc->pl_dev), srsbuf);
713 if (ACPI_SUCCESS(status) && srsbuf->Pointer == NULL)
714 status = AE_NO_MEMORY;
715 if (ACPI_FAILURE(status)) {
717 device_printf(sc->pl_dev,
718 "Unable to fetch current resources: %s\n",
719 AcpiFormatException(status));
723 /* Fill in IRQ resources via link structures. */
726 in_dpf = DPF_OUTSIDE;
727 res = (ACPI_RESOURCE *)srsbuf->Pointer;
728 end = (ACPI_RESOURCE *)((char *)srsbuf->Pointer + srsbuf->Length);
731 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
734 /* We've started the first DPF. */
738 /* We've started the second DPF. */
740 "%s: Multiple dependent functions within a current resource",
745 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
746 /* We are finished with DPF parsing. */
747 KASSERT(in_dpf != DPF_OUTSIDE,
748 ("%s: end dpf when not parsing a dpf", __func__));
749 in_dpf = DPF_OUTSIDE;
751 case ACPI_RESOURCE_TYPE_IRQ:
752 MPASS(i < sc->pl_num_links);
753 res->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 res->Data.Irq.Interrupts[0] = link->l_irq;
760 res->Data.Irq.Interrupts[0] = 0;
764 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
765 MPASS(i < sc->pl_num_links);
766 res->Data.ExtendedIrq.InterruptCount = 1;
767 if (PCI_INTERRUPT_VALID(link->l_irq))
768 res->Data.ExtendedIrq.Interrupts[0] =
771 res->Data.ExtendedIrq.Interrupts[0] = 0;
776 if (res->Type == ACPI_RESOURCE_TYPE_END_TAG)
778 res = ACPI_NEXT_RESOURCE(res);
786 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc,
789 ACPI_RESOURCE newres;
794 /* Start off with an empty buffer. */
795 srsbuf->Pointer = NULL;
797 for (i = 0; i < sc->pl_num_links; i++) {
799 /* Add a new IRQ resource from each link. */
800 link = &sc->pl_links[i];
801 if (link->l_prs_template.Type == ACPI_RESOURCE_TYPE_IRQ) {
803 /* Build an IRQ resource. */
804 bcopy(&link->l_prs_template, &newres,
805 ACPI_RS_SIZE(newres.Data.Irq));
806 newres.Data.Irq.InterruptCount = 1;
807 if (PCI_INTERRUPT_VALID(link->l_irq)) {
808 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
809 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
810 __func__, link->l_irq));
811 newres.Data.Irq.Interrupts[0] = link->l_irq;
813 newres.Data.Irq.Interrupts[0] = 0;
816 /* Build an ExtIRQ resuorce. */
817 bcopy(&link->l_prs_template, &newres,
818 ACPI_RS_SIZE(newres.Data.ExtendedIrq));
819 newres.Data.ExtendedIrq.InterruptCount = 1;
820 if (PCI_INTERRUPT_VALID(link->l_irq))
821 newres.Data.ExtendedIrq.Interrupts[0] =
824 newres.Data.ExtendedIrq.Interrupts[0] = 0;
827 /* Add the new resource to the end of the _SRS buffer. */
828 status = acpi_AppendBufferResource(srsbuf, &newres);
829 if (ACPI_FAILURE(status)) {
830 device_printf(sc->pl_dev,
831 "Unable to build resources: %s\n",
832 AcpiFormatException(status));
833 if (srsbuf->Pointer != NULL)
834 AcpiOsFree(srsbuf->Pointer);
842 acpi_pci_link_route_irqs(device_t dev)
844 struct acpi_pci_link_softc *sc;
845 ACPI_RESOURCE *resource, *end;
851 ACPI_SERIAL_ASSERT(pci_link);
852 sc = device_get_softc(dev);
854 status = acpi_pci_link_srs_from_links(sc, &srsbuf);
856 status = acpi_pci_link_srs_from_crs(sc, &srsbuf);
858 /* Write out new resources via _SRS. */
859 status = AcpiSetCurrentResources(acpi_get_handle(dev), &srsbuf);
860 if (ACPI_FAILURE(status)) {
861 device_printf(dev, "Unable to route IRQs: %s\n",
862 AcpiFormatException(status));
863 AcpiOsFree(srsbuf.Pointer);
868 * Perform acpi_config_intr() on each IRQ resource if it was just
869 * routed for the first time.
873 resource = (ACPI_RESOURCE *)srsbuf.Pointer;
874 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length);
876 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
878 switch (resource->Type) {
879 case ACPI_RESOURCE_TYPE_IRQ:
880 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
881 MPASS(i < sc->pl_num_links);
884 * Only configure the interrupt and update the
885 * weights if this link has a valid IRQ and was
886 * previously unrouted.
888 if (!link->l_routed &&
889 PCI_INTERRUPT_VALID(link->l_irq)) {
890 link->l_routed = TRUE;
891 acpi_config_intr(dev, resource);
892 pci_link_interrupt_weights[link->l_irq] +=
899 resource = ACPI_NEXT_RESOURCE(resource);
903 AcpiOsFree(srsbuf.Pointer);
908 acpi_pci_link_resume(device_t dev)
910 struct acpi_pci_link_softc *sc;
915 * If all of our links are routed, then restore the link via _SRS,
916 * otherwise, disable the link via _DIS.
918 ACPI_SERIAL_BEGIN(pci_link);
919 sc = device_get_softc(dev);
921 for (i = 0; i < sc->pl_num_links; i++)
922 if (sc->pl_links[i].l_routed)
924 if (routed == sc->pl_num_links)
925 status = acpi_pci_link_route_irqs(dev);
927 AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL, NULL);
930 ACPI_SERIAL_END(pci_link);
931 if (ACPI_FAILURE(status))
938 * Pick an IRQ to use for this unrouted link.
941 acpi_pci_link_choose_irq(device_t dev, struct link *link)
943 char tunable_buffer[64], link_name[5];
944 u_int8_t best_irq, pos_irq;
945 int best_weight, pos_weight, i;
947 KASSERT(!link->l_routed, ("%s: link already routed", __func__));
948 KASSERT(!PCI_INTERRUPT_VALID(link->l_irq),
949 ("%s: link already has an IRQ", __func__));
951 /* Check for a tunable override. */
952 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), link_name,
953 sizeof(link_name)))) {
954 snprintf(tunable_buffer, sizeof(tunable_buffer),
955 "hw.pci.link.%s.%d.irq", link_name, link->l_res_index);
956 if (getenv_int(tunable_buffer, &i) && PCI_INTERRUPT_VALID(i)) {
957 if (!link_valid_irq(link, i))
959 "Warning, IRQ %d is not listed as valid\n",
963 snprintf(tunable_buffer, sizeof(tunable_buffer),
964 "hw.pci.link.%s.irq", link_name);
965 if (getenv_int(tunable_buffer, &i) && PCI_INTERRUPT_VALID(i)) {
966 if (!link_valid_irq(link, i))
968 "Warning, IRQ %d is not listed as valid\n",
975 * If we have a valid BIOS IRQ, use that. We trust what the BIOS
976 * says it routed over what _CRS says the link thinks is routed.
978 if (PCI_INTERRUPT_VALID(link->l_bios_irq))
979 return (link->l_bios_irq);
982 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS,
985 if (PCI_INTERRUPT_VALID(link->l_initial_irq))
986 return (link->l_initial_irq);
989 * Ok, we have no useful hints, so we have to pick from the
990 * possible IRQs. For ISA IRQs we only use interrupts that
991 * have already been used by the BIOS.
993 best_irq = PCI_INVALID_IRQ;
994 best_weight = INT_MAX;
995 for (i = 0; i < link->l_num_irqs; i++) {
996 pos_irq = link->l_irqs[i];
997 if (pos_irq < NUM_ISA_INTERRUPTS &&
998 (pci_link_bios_isa_irqs & 1 << pos_irq) == 0)
1000 pos_weight = pci_link_interrupt_weights[pos_irq];
1001 if (pos_weight < best_weight) {
1002 best_weight = pos_weight;
1008 * If this is an ISA IRQ, try using the SCI if it is also an ISA
1009 * interrupt as a fallback.
1011 if (link->l_isa_irq) {
1012 pos_irq = AcpiGbl_FADT.SciInterrupt;
1013 pos_weight = pci_link_interrupt_weights[pos_irq];
1014 if (pos_weight < best_weight) {
1015 best_weight = pos_weight;
1020 if (PCI_INTERRUPT_VALID(best_irq)) {
1022 device_printf(dev, "Picked IRQ %u with weight %d\n",
1023 best_irq, best_weight);
1025 device_printf(dev, "Unable to choose an IRQ\n");
1030 acpi_pci_link_route_interrupt(device_t dev, int index)
1034 if (acpi_disabled("pci_link"))
1035 return (PCI_INVALID_IRQ);
1037 ACPI_SERIAL_BEGIN(pci_link);
1038 link = acpi_pci_link_lookup(dev, index);
1040 panic("%s: apparently invalid index %d", __func__, index);
1043 * If this link device is already routed to an interrupt, just return
1044 * the interrupt it is routed to.
1046 if (link->l_routed) {
1047 KASSERT(PCI_INTERRUPT_VALID(link->l_irq),
1048 ("%s: link is routed but has an invalid IRQ", __func__));
1049 ACPI_SERIAL_END(pci_link);
1050 return (link->l_irq);
1053 /* Choose an IRQ if we need one. */
1054 if (!PCI_INTERRUPT_VALID(link->l_irq)) {
1055 link->l_irq = acpi_pci_link_choose_irq(dev, link);
1058 * Try to route the interrupt we picked. If it fails, then
1059 * assume the interrupt is not routed.
1061 if (PCI_INTERRUPT_VALID(link->l_irq)) {
1062 acpi_pci_link_route_irqs(dev);
1063 if (!link->l_routed)
1064 link->l_irq = PCI_INVALID_IRQ;
1067 ACPI_SERIAL_END(pci_link);
1069 return (link->l_irq);
1073 * This is gross, but we abuse the identify routine to perform one-time
1074 * SYSINIT() style initialization for the driver.
1077 acpi_pci_link_identify(driver_t *driver, device_t parent)
1081 * If the SCI is an ISA IRQ, add it to the bitmask of known good
1084 * XXX: If we are using the APIC, the SCI might have been
1085 * rerouted to an APIC pin in which case this is invalid. However,
1086 * if we are using the APIC, we also shouldn't be having any PCI
1087 * interrupts routed via ISA IRQs, so this is probably ok.
1089 if (AcpiGbl_FADT.SciInterrupt < NUM_ISA_INTERRUPTS)
1090 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT.SciInterrupt);
1093 static device_method_t acpi_pci_link_methods[] = {
1094 /* Device interface */
1095 DEVMETHOD(device_identify, acpi_pci_link_identify),
1096 DEVMETHOD(device_probe, acpi_pci_link_probe),
1097 DEVMETHOD(device_attach, acpi_pci_link_attach),
1098 DEVMETHOD(device_resume, acpi_pci_link_resume),
1103 static driver_t acpi_pci_link_driver = {
1105 acpi_pci_link_methods,
1106 sizeof(struct acpi_pci_link_softc),
1109 static devclass_t pci_link_devclass;
1111 DRIVER_MODULE(acpi_pci_link, acpi, acpi_pci_link_driver, pci_link_devclass, 0,
1113 MODULE_DEPEND(acpi_pci_link, acpi, 1, 1, 1);