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;
109 ACPI_RESOURCE l_prs_template;
112 struct link_count_request {
117 struct link_res_request {
118 struct acpi_pci_link_softc *sc;
124 static MALLOC_DEFINE(M_PCI_LINK, "pci_link", "ACPI PCI Link structures");
126 static int pci_link_interrupt_weights[NUM_ACPI_INTERRUPTS];
127 static int pci_link_bios_isa_irqs;
129 static char *pci_link_ids[] = { "PNP0C0F", NULL };
132 * Fetch the short name associated with an ACPI handle and save it in the
136 acpi_short_name(ACPI_HANDLE handle, char *buffer, size_t buflen)
141 buf.Pointer = buffer;
142 return (AcpiGetName(handle, ACPI_SINGLE_NAME, &buf));
146 acpi_pci_link_probe(device_t dev)
148 char descr[28], name[12];
152 * We explicitly do not check _STA since not all systems set it to
155 if (acpi_disabled("pci_link"))
157 rv = ACPI_ID_PROBE(device_get_parent(dev), dev, pci_link_ids, NULL);
161 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), name,
163 snprintf(descr, sizeof(descr), "ACPI PCI Link %s", name);
164 device_set_desc_copy(dev, descr);
166 device_set_desc(dev, "ACPI PCI Link");
172 acpi_count_irq_resources(ACPI_RESOURCE *res, void *context)
174 struct link_count_request *req;
176 req = (struct link_count_request *)context;
178 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
179 switch (req->in_dpf) {
181 /* We've started the first DPF. */
182 req->in_dpf = DPF_FIRST;
185 /* We've started the second DPF. */
186 req->in_dpf = DPF_IGNORE;
190 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
191 /* We are finished with DPF parsing. */
192 KASSERT(req->in_dpf != DPF_OUTSIDE,
193 ("%s: end dpf when not parsing a dpf", __func__));
194 req->in_dpf = DPF_OUTSIDE;
196 case ACPI_RESOURCE_TYPE_IRQ:
197 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
199 * Don't count resources if we are in a DPF set that we are
202 if (req->in_dpf != DPF_IGNORE)
209 link_add_crs(ACPI_RESOURCE *res, void *context)
211 struct link_res_request *req;
214 ACPI_SERIAL_ASSERT(pci_link);
215 req = (struct link_res_request *)context;
217 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
218 switch (req->in_dpf) {
220 /* We've started the first DPF. */
221 req->in_dpf = DPF_FIRST;
224 /* We've started the second DPF. */
226 "%s: Multiple dependent functions within a current resource",
231 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
232 /* We are finished with DPF parsing. */
233 KASSERT(req->in_dpf != DPF_OUTSIDE,
234 ("%s: end dpf when not parsing a dpf", __func__));
235 req->in_dpf = DPF_OUTSIDE;
237 case ACPI_RESOURCE_TYPE_IRQ:
238 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
239 KASSERT(req->link_index < req->sc->pl_num_links,
240 ("%s: array boundary violation", __func__));
241 link = &req->sc->pl_links[req->link_index];
242 link->l_res_index = req->res_index;
243 link->l_crs_type = res->Type;
248 * Only use the current value if there's one IRQ. Some
249 * systems return multiple IRQs (which is nonsense for _CRS)
250 * when the link hasn't been programmed.
252 if (res->Type == ACPI_RESOURCE_TYPE_IRQ) {
253 if (res->Data.Irq.InterruptCount == 1)
254 link->l_irq = res->Data.Irq.Interrupts[0];
255 } else if (res->Data.ExtendedIrq.InterruptCount == 1)
256 link->l_irq = res->Data.ExtendedIrq.Interrupts[0];
259 * An IRQ of zero means that the link isn't routed.
261 if (link->l_irq == 0)
262 link->l_irq = PCI_INVALID_IRQ;
271 * Populate the set of possible IRQs for each device.
274 link_add_prs(ACPI_RESOURCE *res, void *context)
277 struct link_res_request *req;
280 UINT32 *ext_irqs = NULL;
281 int i, is_ext_irq = 1;
283 ACPI_SERIAL_ASSERT(pci_link);
284 req = (struct link_res_request *)context;
286 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
287 switch (req->in_dpf) {
289 /* We've started the first DPF. */
290 req->in_dpf = DPF_FIRST;
293 /* We've started the second DPF. */
294 req->in_dpf = DPF_IGNORE;
298 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
299 /* We are finished with DPF parsing. */
300 KASSERT(req->in_dpf != DPF_OUTSIDE,
301 ("%s: end dpf when not parsing a dpf", __func__));
302 req->in_dpf = DPF_OUTSIDE;
304 case ACPI_RESOURCE_TYPE_IRQ:
307 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
309 * Don't parse resources if we are in a DPF set that we are
312 if (req->in_dpf == DPF_IGNORE)
315 KASSERT(req->link_index < req->sc->pl_num_links,
316 ("%s: array boundary violation", __func__));
317 link = &req->sc->pl_links[req->link_index];
318 if (link->l_res_index == -1) {
319 KASSERT(req->sc->pl_crs_bad,
320 ("res_index should be set"));
321 link->l_res_index = req->res_index;
327 * Stash a copy of the resource for later use when doing
330 tmp = &link->l_prs_template;
332 bcopy(res, tmp, ACPI_RS_SIZE(tmp->Data.ExtendedIrq));
335 * XXX acpi_AppendBufferResource() cannot handle
338 bzero(&tmp->Data.ExtendedIrq.ResourceSource,
339 sizeof(tmp->Data.ExtendedIrq.ResourceSource));
340 tmp->Length = ACPI_RS_SIZE(tmp->Data.ExtendedIrq);
343 res->Data.ExtendedIrq.InterruptCount;
344 ext_irqs = res->Data.ExtendedIrq.Interrupts;
346 bcopy(res, tmp, ACPI_RS_SIZE(tmp->Data.Irq));
347 link->l_num_irqs = res->Data.Irq.InterruptCount;
348 irqs = res->Data.Irq.Interrupts;
350 if (link->l_num_irqs == 0)
354 * Save a list of the valid IRQs. Also, if all of the
355 * valid IRQs are ISA IRQs, then mark this link as
356 * routed via an ISA interrupt.
358 link->l_isa_irq = TRUE;
359 link->l_irqs = malloc(sizeof(int) * link->l_num_irqs,
360 M_PCI_LINK, M_WAITOK | M_ZERO);
361 for (i = 0; i < link->l_num_irqs; i++) {
363 link->l_irqs[i] = ext_irqs[i];
364 if (ext_irqs[i] >= NUM_ISA_INTERRUPTS)
365 link->l_isa_irq = FALSE;
367 link->l_irqs[i] = irqs[i];
368 if (irqs[i] >= NUM_ISA_INTERRUPTS)
369 link->l_isa_irq = FALSE;
374 * If this is not an ISA IRQ but _CRS used a non-extended
375 * IRQ descriptor, don't use _CRS as a template for _SRS.
377 if (!req->sc->pl_crs_bad && !link->l_isa_irq &&
378 link->l_crs_type == ACPI_RESOURCE_TYPE_IRQ)
379 req->sc->pl_crs_bad = TRUE;
382 if (req->in_dpf == DPF_IGNORE)
384 if (req->sc->pl_crs_bad)
385 device_printf(req->sc->pl_dev,
386 "Warning: possible resource %d will be lost during _SRS\n",
394 link_valid_irq(struct link *link, int irq)
398 ACPI_SERIAL_ASSERT(pci_link);
400 /* Invalid interrupts are never valid. */
401 if (!PCI_INTERRUPT_VALID(irq))
404 /* Any interrupt in the list of possible interrupts is valid. */
405 for (i = 0; i < link->l_num_irqs; i++)
406 if (link->l_irqs[i] == irq)
410 * For links routed via an ISA interrupt, if the SCI is routed via
411 * an ISA interrupt, the SCI is always treated as a valid IRQ.
413 if (link->l_isa_irq && AcpiGbl_FADT.SciInterrupt == irq &&
414 irq < NUM_ISA_INTERRUPTS)
417 /* If the interrupt wasn't found in the list it is not valid. */
422 acpi_pci_link_dump(struct acpi_pci_link_softc *sc, int header, const char *tag)
428 ACPI_SERIAL_ASSERT(pci_link);
430 snprintf(buf, sizeof(buf), "%s:",
431 device_get_nameunit(sc->pl_dev));
432 printf("%-16.16s Index IRQ Rtd Ref IRQs\n", buf);
434 for (i = 0; i < sc->pl_num_links; i++) {
435 link = &sc->pl_links[i];
436 printf(" %-14.14s %5d %3d %c %3d ", i == 0 ? tag : "", i,
437 link->l_irq, link->l_routed ? 'Y' : 'N',
439 if (link->l_num_irqs == 0)
441 else for (j = 0; j < link->l_num_irqs; j++)
442 printf(" %d", link->l_irqs[j]);
448 acpi_pci_link_attach(device_t dev)
450 struct acpi_pci_link_softc *sc;
451 struct link_count_request creq;
452 struct link_res_request rreq;
456 sc = device_get_softc(dev);
458 ACPI_SERIAL_BEGIN(pci_link);
461 * Count the number of current resources so we know how big of
462 * a link array to allocate. On some systems, _CRS is broken,
463 * so for those systems try to derive the count from _PRS instead.
465 creq.in_dpf = DPF_OUTSIDE;
467 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
468 acpi_count_irq_resources, &creq);
469 sc->pl_crs_bad = ACPI_FAILURE(status);
470 if (sc->pl_crs_bad) {
471 creq.in_dpf = DPF_OUTSIDE;
473 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
474 acpi_count_irq_resources, &creq);
475 if (ACPI_FAILURE(status)) {
477 "Unable to parse _CRS or _PRS: %s\n",
478 AcpiFormatException(status));
479 ACPI_SERIAL_END(pci_link);
483 sc->pl_num_links = creq.count;
484 if (creq.count == 0) {
485 ACPI_SERIAL_END(pci_link);
488 sc->pl_links = malloc(sizeof(struct link) * sc->pl_num_links,
489 M_PCI_LINK, M_WAITOK | M_ZERO);
491 /* Initialize the child links. */
492 for (i = 0; i < sc->pl_num_links; i++) {
493 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
494 sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ;
495 sc->pl_links[i].l_sc = sc;
496 sc->pl_links[i].l_isa_irq = FALSE;
497 sc->pl_links[i].l_res_index = -1;
500 /* Try to read the current settings from _CRS if it is valid. */
501 if (!sc->pl_crs_bad) {
502 rreq.in_dpf = DPF_OUTSIDE;
506 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
507 link_add_crs, &rreq);
508 if (ACPI_FAILURE(status)) {
509 device_printf(dev, "Unable to parse _CRS: %s\n",
510 AcpiFormatException(status));
516 * Try to read the possible settings from _PRS. Note that if the
517 * _CRS is toast, we depend on having a working _PRS. However, if
518 * _CRS works, then it is ok for _PRS to be missing.
520 rreq.in_dpf = DPF_OUTSIDE;
524 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
525 link_add_prs, &rreq);
526 if (ACPI_FAILURE(status) &&
527 (status != AE_NOT_FOUND || sc->pl_crs_bad)) {
528 device_printf(dev, "Unable to parse _PRS: %s\n",
529 AcpiFormatException(status));
533 acpi_pci_link_dump(sc, 1, "Initial Probe");
535 /* Verify initial IRQs if we have _PRS. */
536 if (status != AE_NOT_FOUND)
537 for (i = 0; i < sc->pl_num_links; i++)
538 if (!link_valid_irq(&sc->pl_links[i],
539 sc->pl_links[i].l_irq))
540 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
542 acpi_pci_link_dump(sc, 0, "Validation");
544 /* Save initial IRQs. */
545 for (i = 0; i < sc->pl_num_links; i++)
546 sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq;
549 * Try to disable this link. If successful, set the current IRQ to
550 * zero and flags to indicate this link is not routed. If we can't
551 * run _DIS (i.e., the method doesn't exist), assume the initial
552 * IRQ was routed by the BIOS.
554 if (ACPI_SUCCESS(AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL,
556 for (i = 0; i < sc->pl_num_links; i++)
557 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
559 for (i = 0; i < sc->pl_num_links; i++)
560 if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq))
561 sc->pl_links[i].l_routed = TRUE;
563 acpi_pci_link_dump(sc, 0, "After Disable");
564 ACPI_SERIAL_END(pci_link);
567 ACPI_SERIAL_END(pci_link);
568 for (i = 0; i < sc->pl_num_links; i++)
569 if (sc->pl_links[i].l_irqs != NULL)
570 free(sc->pl_links[i].l_irqs, M_PCI_LINK);
571 free(sc->pl_links, M_PCI_LINK);
575 /* XXX: Note that this is identical to pci_pir_search_irq(). */
577 acpi_pci_link_search_irq(int bus, int device, int pin)
580 uint8_t func, maxfunc;
582 /* See if we have a valid device at function 0. */
583 value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1);
584 if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
585 return (PCI_INVALID_IRQ);
586 if (value & PCIM_MFDEV)
587 maxfunc = PCI_FUNCMAX;
591 /* Scan all possible functions at this device. */
592 for (func = 0; func <= maxfunc; func++) {
593 value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4);
594 if (value == 0xffffffff)
596 value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
599 * See if it uses the pin in question. Note that the passed
600 * in pin uses 0 for A, .. 3 for D whereas the intpin
601 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
603 if (value != pin + 1)
605 value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
608 "ACPI: Found matching pin for %d.%d.INT%c at func %d: %d\n",
609 bus, device, pin + 'A', func, value);
610 if (value != PCI_INVALID_IRQ)
613 return (PCI_INVALID_IRQ);
617 * Find the link structure that corresponds to the resource index passed in
618 * via 'source_index'.
621 acpi_pci_link_lookup(device_t dev, int source_index)
623 struct acpi_pci_link_softc *sc;
626 ACPI_SERIAL_ASSERT(pci_link);
627 sc = device_get_softc(dev);
628 for (i = 0; i < sc->pl_num_links; i++)
629 if (sc->pl_links[i].l_res_index == source_index)
630 return (&sc->pl_links[i]);
635 acpi_pci_link_add_reference(device_t dev, int index, device_t pcib, int slot,
643 * Look up the PCI bus for the specified PCI bridge device. Note
644 * that the PCI bridge device might not have any children yet.
645 * However, looking up its bus number doesn't require a valid child
646 * device, so we just pass NULL.
648 if (BUS_READ_IVAR(pcib, NULL, PCIB_IVAR_BUS, &bus) != 0) {
649 device_printf(pcib, "Unable to read PCI bus number");
650 panic("PCI bridge without a bus number");
653 /* Bump the reference count. */
654 ACPI_SERIAL_BEGIN(pci_link);
655 link = acpi_pci_link_lookup(dev, index);
657 device_printf(dev, "apparently invalid index %d\n", index);
658 ACPI_SERIAL_END(pci_link);
661 link->l_references++;
663 pci_link_interrupt_weights[link->l_irq]++;
666 * The BIOS only routes interrupts via ISA IRQs using the ATPICs
667 * (8259As). Thus, if this link is routed via an ISA IRQ, go
668 * look to see if the BIOS routed an IRQ for this link at the
669 * indicated (bus, slot, pin). If so, we prefer that IRQ for
670 * this link and add that IRQ to our list of known-good IRQs.
671 * This provides a good work-around for link devices whose _CRS
672 * method is either broken or bogus. We only use the value
673 * returned by _CRS if we can't find a valid IRQ via this method
676 * If this link is not routed via an ISA IRQ (because we are using
677 * APIC for example), then don't bother looking up the BIOS IRQ
678 * as if we find one it won't be valid anyway.
680 if (!link->l_isa_irq) {
681 ACPI_SERIAL_END(pci_link);
685 /* Try to find a BIOS IRQ setting from any matching devices. */
686 bios_irq = acpi_pci_link_search_irq(bus, slot, pin);
687 if (!PCI_INTERRUPT_VALID(bios_irq)) {
688 ACPI_SERIAL_END(pci_link);
692 /* Validate the BIOS IRQ. */
693 if (!link_valid_irq(link, bios_irq)) {
694 device_printf(dev, "BIOS IRQ %u for %d.%d.INT%c is invalid\n",
695 bios_irq, (int)bus, slot, pin + 'A');
696 } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) {
697 link->l_bios_irq = bios_irq;
698 if (bios_irq < NUM_ISA_INTERRUPTS)
699 pci_link_bios_isa_irqs |= (1 << bios_irq);
700 if (bios_irq != link->l_initial_irq &&
701 PCI_INTERRUPT_VALID(link->l_initial_irq))
703 "BIOS IRQ %u does not match initial IRQ %u\n",
704 bios_irq, link->l_initial_irq);
705 } else if (bios_irq != link->l_bios_irq)
707 "BIOS IRQ %u for %d.%d.INT%c does not match previous BIOS IRQ %u\n",
708 bios_irq, (int)bus, slot, pin + 'A',
710 ACPI_SERIAL_END(pci_link);
714 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
716 ACPI_RESOURCE *end, *res;
721 /* Fetch the _CRS. */
722 ACPI_SERIAL_ASSERT(pci_link);
723 srsbuf->Pointer = NULL;
724 srsbuf->Length = ACPI_ALLOCATE_BUFFER;
725 status = AcpiGetCurrentResources(acpi_get_handle(sc->pl_dev), srsbuf);
726 if (ACPI_SUCCESS(status) && srsbuf->Pointer == NULL)
727 status = AE_NO_MEMORY;
728 if (ACPI_FAILURE(status)) {
730 device_printf(sc->pl_dev,
731 "Unable to fetch current resources: %s\n",
732 AcpiFormatException(status));
736 /* Fill in IRQ resources via link structures. */
739 in_dpf = DPF_OUTSIDE;
740 res = (ACPI_RESOURCE *)srsbuf->Pointer;
741 end = (ACPI_RESOURCE *)((char *)srsbuf->Pointer + srsbuf->Length);
744 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
747 /* We've started the first DPF. */
751 /* We've started the second DPF. */
753 "%s: Multiple dependent functions within a current resource",
758 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
759 /* We are finished with DPF parsing. */
760 KASSERT(in_dpf != DPF_OUTSIDE,
761 ("%s: end dpf when not parsing a dpf", __func__));
762 in_dpf = DPF_OUTSIDE;
764 case ACPI_RESOURCE_TYPE_IRQ:
765 MPASS(i < sc->pl_num_links);
766 res->Data.Irq.InterruptCount = 1;
767 if (PCI_INTERRUPT_VALID(link->l_irq)) {
768 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
769 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
770 __func__, link->l_irq));
771 res->Data.Irq.Interrupts[0] = link->l_irq;
773 res->Data.Irq.Interrupts[0] = 0;
777 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
778 MPASS(i < sc->pl_num_links);
779 res->Data.ExtendedIrq.InterruptCount = 1;
780 if (PCI_INTERRUPT_VALID(link->l_irq))
781 res->Data.ExtendedIrq.Interrupts[0] =
784 res->Data.ExtendedIrq.Interrupts[0] = 0;
789 if (res->Type == ACPI_RESOURCE_TYPE_END_TAG)
791 res = ACPI_NEXT_RESOURCE(res);
799 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc,
802 ACPI_RESOURCE newres;
807 /* Start off with an empty buffer. */
808 srsbuf->Pointer = NULL;
810 for (i = 0; i < sc->pl_num_links; i++) {
812 /* Add a new IRQ resource from each link. */
813 link = &sc->pl_links[i];
814 if (link->l_prs_template.Type == ACPI_RESOURCE_TYPE_IRQ) {
816 /* Build an IRQ resource. */
817 bcopy(&link->l_prs_template, &newres,
818 ACPI_RS_SIZE(newres.Data.Irq));
819 newres.Data.Irq.InterruptCount = 1;
820 if (PCI_INTERRUPT_VALID(link->l_irq)) {
821 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
822 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
823 __func__, link->l_irq));
824 newres.Data.Irq.Interrupts[0] = link->l_irq;
826 newres.Data.Irq.Interrupts[0] = 0;
829 /* Build an ExtIRQ resuorce. */
830 bcopy(&link->l_prs_template, &newres,
831 ACPI_RS_SIZE(newres.Data.ExtendedIrq));
832 newres.Data.ExtendedIrq.InterruptCount = 1;
833 if (PCI_INTERRUPT_VALID(link->l_irq))
834 newres.Data.ExtendedIrq.Interrupts[0] =
837 newres.Data.ExtendedIrq.Interrupts[0] = 0;
840 /* Add the new resource to the end of the _SRS buffer. */
841 status = acpi_AppendBufferResource(srsbuf, &newres);
842 if (ACPI_FAILURE(status)) {
843 device_printf(sc->pl_dev,
844 "Unable to build resources: %s\n",
845 AcpiFormatException(status));
846 if (srsbuf->Pointer != NULL)
847 AcpiOsFree(srsbuf->Pointer);
855 acpi_pci_link_route_irqs(device_t dev)
857 struct acpi_pci_link_softc *sc;
858 ACPI_RESOURCE *resource, *end;
864 ACPI_SERIAL_ASSERT(pci_link);
865 sc = device_get_softc(dev);
867 status = acpi_pci_link_srs_from_links(sc, &srsbuf);
869 status = acpi_pci_link_srs_from_crs(sc, &srsbuf);
871 /* Write out new resources via _SRS. */
872 status = AcpiSetCurrentResources(acpi_get_handle(dev), &srsbuf);
873 if (ACPI_FAILURE(status)) {
874 device_printf(dev, "Unable to route IRQs: %s\n",
875 AcpiFormatException(status));
876 AcpiOsFree(srsbuf.Pointer);
881 * Perform acpi_config_intr() on each IRQ resource if it was just
882 * routed for the first time.
886 resource = (ACPI_RESOURCE *)srsbuf.Pointer;
887 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length);
889 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
891 switch (resource->Type) {
892 case ACPI_RESOURCE_TYPE_IRQ:
893 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
894 MPASS(i < sc->pl_num_links);
897 * Only configure the interrupt and update the
898 * weights if this link has a valid IRQ and was
899 * previously unrouted.
901 if (!link->l_routed &&
902 PCI_INTERRUPT_VALID(link->l_irq)) {
903 link->l_routed = TRUE;
904 acpi_config_intr(dev, resource);
905 pci_link_interrupt_weights[link->l_irq] +=
912 resource = ACPI_NEXT_RESOURCE(resource);
916 AcpiOsFree(srsbuf.Pointer);
921 acpi_pci_link_resume(device_t dev)
923 struct acpi_pci_link_softc *sc;
928 * If all of our links are routed, then restore the link via _SRS,
929 * otherwise, disable the link via _DIS.
931 ACPI_SERIAL_BEGIN(pci_link);
932 sc = device_get_softc(dev);
934 for (i = 0; i < sc->pl_num_links; i++)
935 if (sc->pl_links[i].l_routed)
937 if (routed == sc->pl_num_links)
938 status = acpi_pci_link_route_irqs(dev);
940 AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL, NULL);
943 ACPI_SERIAL_END(pci_link);
944 if (ACPI_FAILURE(status))
951 * Pick an IRQ to use for this unrouted link.
954 acpi_pci_link_choose_irq(device_t dev, struct link *link)
956 char tunable_buffer[64], link_name[5];
957 u_int8_t best_irq, pos_irq;
958 int best_weight, pos_weight, i;
960 KASSERT(!link->l_routed, ("%s: link already routed", __func__));
961 KASSERT(!PCI_INTERRUPT_VALID(link->l_irq),
962 ("%s: link already has an IRQ", __func__));
964 /* Check for a tunable override. */
965 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), link_name,
966 sizeof(link_name)))) {
967 snprintf(tunable_buffer, sizeof(tunable_buffer),
968 "hw.pci.link.%s.%d.irq", link_name, link->l_res_index);
969 if (getenv_int(tunable_buffer, &i) && PCI_INTERRUPT_VALID(i)) {
970 if (!link_valid_irq(link, i))
972 "Warning, IRQ %d is not listed as valid\n",
976 snprintf(tunable_buffer, sizeof(tunable_buffer),
977 "hw.pci.link.%s.irq", link_name);
978 if (getenv_int(tunable_buffer, &i) && PCI_INTERRUPT_VALID(i)) {
979 if (!link_valid_irq(link, i))
981 "Warning, IRQ %d is not listed as valid\n",
988 * If we have a valid BIOS IRQ, use that. We trust what the BIOS
989 * says it routed over what _CRS says the link thinks is routed.
991 if (PCI_INTERRUPT_VALID(link->l_bios_irq))
992 return (link->l_bios_irq);
995 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS,
998 if (PCI_INTERRUPT_VALID(link->l_initial_irq))
999 return (link->l_initial_irq);
1002 * Ok, we have no useful hints, so we have to pick from the
1003 * possible IRQs. For ISA IRQs we only use interrupts that
1004 * have already been used by the BIOS.
1006 best_irq = PCI_INVALID_IRQ;
1007 best_weight = INT_MAX;
1008 for (i = 0; i < link->l_num_irqs; i++) {
1009 pos_irq = link->l_irqs[i];
1010 if (pos_irq < NUM_ISA_INTERRUPTS &&
1011 (pci_link_bios_isa_irqs & 1 << pos_irq) == 0)
1013 pos_weight = pci_link_interrupt_weights[pos_irq];
1014 if (pos_weight < best_weight) {
1015 best_weight = pos_weight;
1021 * If this is an ISA IRQ, try using the SCI if it is also an ISA
1022 * interrupt as a fallback.
1024 if (link->l_isa_irq) {
1025 pos_irq = AcpiGbl_FADT.SciInterrupt;
1026 pos_weight = pci_link_interrupt_weights[pos_irq];
1027 if (pos_weight < best_weight) {
1028 best_weight = pos_weight;
1033 if (PCI_INTERRUPT_VALID(best_irq)) {
1035 device_printf(dev, "Picked IRQ %u with weight %d\n",
1036 best_irq, best_weight);
1038 device_printf(dev, "Unable to choose an IRQ\n");
1043 acpi_pci_link_route_interrupt(device_t dev, int index)
1047 if (acpi_disabled("pci_link"))
1048 return (PCI_INVALID_IRQ);
1050 ACPI_SERIAL_BEGIN(pci_link);
1051 link = acpi_pci_link_lookup(dev, index);
1053 panic("%s: apparently invalid index %d", __func__, index);
1056 * If this link device is already routed to an interrupt, just return
1057 * the interrupt it is routed to.
1059 if (link->l_routed) {
1060 KASSERT(PCI_INTERRUPT_VALID(link->l_irq),
1061 ("%s: link is routed but has an invalid IRQ", __func__));
1062 ACPI_SERIAL_END(pci_link);
1063 return (link->l_irq);
1066 /* Choose an IRQ if we need one. */
1067 if (!PCI_INTERRUPT_VALID(link->l_irq)) {
1068 link->l_irq = acpi_pci_link_choose_irq(dev, link);
1071 * Try to route the interrupt we picked. If it fails, then
1072 * assume the interrupt is not routed.
1074 if (PCI_INTERRUPT_VALID(link->l_irq)) {
1075 acpi_pci_link_route_irqs(dev);
1076 if (!link->l_routed)
1077 link->l_irq = PCI_INVALID_IRQ;
1080 ACPI_SERIAL_END(pci_link);
1082 return (link->l_irq);
1086 * This is gross, but we abuse the identify routine to perform one-time
1087 * SYSINIT() style initialization for the driver.
1090 acpi_pci_link_identify(driver_t *driver, device_t parent)
1094 * If the SCI is an ISA IRQ, add it to the bitmask of known good
1097 * XXX: If we are using the APIC, the SCI might have been
1098 * rerouted to an APIC pin in which case this is invalid. However,
1099 * if we are using the APIC, we also shouldn't be having any PCI
1100 * interrupts routed via ISA IRQs, so this is probably ok.
1102 if (AcpiGbl_FADT.SciInterrupt < NUM_ISA_INTERRUPTS)
1103 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT.SciInterrupt);
1106 static device_method_t acpi_pci_link_methods[] = {
1107 /* Device interface */
1108 DEVMETHOD(device_identify, acpi_pci_link_identify),
1109 DEVMETHOD(device_probe, acpi_pci_link_probe),
1110 DEVMETHOD(device_attach, acpi_pci_link_attach),
1111 DEVMETHOD(device_resume, acpi_pci_link_resume),
1116 static driver_t acpi_pci_link_driver = {
1118 acpi_pci_link_methods,
1119 sizeof(struct acpi_pci_link_softc),
1122 static devclass_t pci_link_devclass;
1124 DRIVER_MODULE(acpi_pci_link, acpi, acpi_pci_link_driver, pci_link_devclass, 0,
1126 MODULE_DEPEND(acpi_pci_link, acpi, 1, 1, 1);