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)
271 struct link_res_request *req;
274 UINT32 *ext_irqs = NULL;
275 int i, is_ext_irq = 1;
277 ACPI_SERIAL_ASSERT(pci_link);
278 req = (struct link_res_request *)context;
280 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
281 switch (req->in_dpf) {
283 /* We've started the first DPF. */
284 req->in_dpf = DPF_FIRST;
287 /* We've started the second DPF. */
288 req->in_dpf = DPF_IGNORE;
292 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
293 /* We are finished with DPF parsing. */
294 KASSERT(req->in_dpf != DPF_OUTSIDE,
295 ("%s: end dpf when not parsing a dpf", __func__));
296 req->in_dpf = DPF_OUTSIDE;
298 case ACPI_RESOURCE_TYPE_IRQ:
301 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
303 * Don't parse resources if we are in a DPF set that we are
306 if (req->in_dpf == DPF_IGNORE)
309 KASSERT(req->link_index < req->sc->pl_num_links,
310 ("%s: array boundary violation", __func__));
311 link = &req->sc->pl_links[req->link_index];
312 if (link->l_res_index == -1) {
313 KASSERT(req->sc->pl_crs_bad,
314 ("res_index should be set"));
315 link->l_res_index = req->res_index;
321 * Stash a copy of the resource for later use when doing
324 bcopy(res, &link->l_prs_template, sizeof(ACPI_RESOURCE));
327 res->Data.ExtendedIrq.InterruptCount;
328 ext_irqs = res->Data.ExtendedIrq.Interrupts;
330 link->l_num_irqs = res->Data.Irq.InterruptCount;
331 irqs = res->Data.Irq.Interrupts;
333 if (link->l_num_irqs == 0)
337 * Save a list of the valid IRQs. Also, if all of the
338 * valid IRQs are ISA IRQs, then mark this link as
339 * routed via an ISA interrupt.
341 link->l_isa_irq = TRUE;
342 link->l_irqs = malloc(sizeof(int) * link->l_num_irqs,
343 M_PCI_LINK, M_WAITOK | M_ZERO);
344 for (i = 0; i < link->l_num_irqs; i++) {
346 link->l_irqs[i] = ext_irqs[i];
347 if (ext_irqs[i] >= NUM_ISA_INTERRUPTS)
348 link->l_isa_irq = FALSE;
350 link->l_irqs[i] = irqs[i];
351 if (irqs[i] >= NUM_ISA_INTERRUPTS)
352 link->l_isa_irq = FALSE;
357 if (req->in_dpf == DPF_IGNORE)
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",
369 link_valid_irq(struct link *link, int irq)
373 ACPI_SERIAL_ASSERT(pci_link);
375 /* Invalid interrupts are never valid. */
376 if (!PCI_INTERRUPT_VALID(irq))
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)
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.
388 if (link->l_isa_irq && AcpiGbl_FADT.SciInterrupt == irq &&
389 irq < NUM_ISA_INTERRUPTS)
392 /* If the interrupt wasn't found in the list it is not valid. */
397 acpi_pci_link_dump(struct acpi_pci_link_softc *sc, int header, const char *tag)
403 ACPI_SERIAL_ASSERT(pci_link);
405 snprintf(buf, sizeof(buf), "%s:",
406 device_get_nameunit(sc->pl_dev));
407 printf("%-16.16s Index IRQ Rtd Ref IRQs\n", buf);
409 for (i = 0; i < sc->pl_num_links; i++) {
410 link = &sc->pl_links[i];
411 printf(" %-14.14s %5d %3d %c %3d ", i == 0 ? tag : "", i,
412 link->l_irq, link->l_routed ? 'Y' : 'N',
414 if (link->l_num_irqs == 0)
416 else for (j = 0; j < link->l_num_irqs; j++)
417 printf(" %d", link->l_irqs[j]);
423 acpi_pci_link_attach(device_t dev)
425 struct acpi_pci_link_softc *sc;
426 struct link_count_request creq;
427 struct link_res_request rreq;
431 sc = device_get_softc(dev);
433 ACPI_SERIAL_BEGIN(pci_link);
436 * Count the number of current resources so we know how big of
437 * a link array to allocate. On some systems, _CRS is broken,
438 * so for those systems try to derive the count from _PRS instead.
440 creq.in_dpf = DPF_OUTSIDE;
442 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
443 acpi_count_irq_resources, &creq);
444 sc->pl_crs_bad = ACPI_FAILURE(status);
445 if (sc->pl_crs_bad) {
446 creq.in_dpf = DPF_OUTSIDE;
448 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
449 acpi_count_irq_resources, &creq);
450 if (ACPI_FAILURE(status)) {
452 "Unable to parse _CRS or _PRS: %s\n",
453 AcpiFormatException(status));
454 ACPI_SERIAL_END(pci_link);
458 sc->pl_num_links = creq.count;
459 if (creq.count == 0) {
460 ACPI_SERIAL_END(pci_link);
463 sc->pl_links = malloc(sizeof(struct link) * sc->pl_num_links,
464 M_PCI_LINK, M_WAITOK | M_ZERO);
466 /* Initialize the child links. */
467 for (i = 0; i < sc->pl_num_links; i++) {
468 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
469 sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ;
470 sc->pl_links[i].l_sc = sc;
471 sc->pl_links[i].l_isa_irq = FALSE;
472 sc->pl_links[i].l_res_index = -1;
475 /* Try to read the current settings from _CRS if it is valid. */
476 if (!sc->pl_crs_bad) {
477 rreq.in_dpf = DPF_OUTSIDE;
481 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
482 link_add_crs, &rreq);
483 if (ACPI_FAILURE(status)) {
484 device_printf(dev, "Unable to parse _CRS: %s\n",
485 AcpiFormatException(status));
491 * Try to read the possible settings from _PRS. Note that if the
492 * _CRS is toast, we depend on having a working _PRS. However, if
493 * _CRS works, then it is ok for _PRS to be missing.
495 rreq.in_dpf = DPF_OUTSIDE;
499 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
500 link_add_prs, &rreq);
501 if (ACPI_FAILURE(status) &&
502 (status != AE_NOT_FOUND || sc->pl_crs_bad)) {
503 device_printf(dev, "Unable to parse _PRS: %s\n",
504 AcpiFormatException(status));
508 acpi_pci_link_dump(sc, 1, "Initial Probe");
510 /* Verify initial IRQs if we have _PRS. */
511 if (status != AE_NOT_FOUND)
512 for (i = 0; i < sc->pl_num_links; i++)
513 if (!link_valid_irq(&sc->pl_links[i],
514 sc->pl_links[i].l_irq))
515 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
517 acpi_pci_link_dump(sc, 0, "Validation");
519 /* Save initial IRQs. */
520 for (i = 0; i < sc->pl_num_links; i++)
521 sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq;
524 * Try to disable this link. If successful, set the current IRQ to
525 * zero and flags to indicate this link is not routed. If we can't
526 * run _DIS (i.e., the method doesn't exist), assume the initial
527 * IRQ was routed by the BIOS.
529 if (ACPI_SUCCESS(AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL,
531 for (i = 0; i < sc->pl_num_links; i++)
532 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
534 for (i = 0; i < sc->pl_num_links; i++)
535 if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq))
536 sc->pl_links[i].l_routed = TRUE;
538 acpi_pci_link_dump(sc, 0, "After Disable");
539 ACPI_SERIAL_END(pci_link);
542 ACPI_SERIAL_END(pci_link);
543 for (i = 0; i < sc->pl_num_links; i++)
544 if (sc->pl_links[i].l_irqs != NULL)
545 free(sc->pl_links[i].l_irqs, M_PCI_LINK);
546 free(sc->pl_links, M_PCI_LINK);
550 /* XXX: Note that this is identical to pci_pir_search_irq(). */
552 acpi_pci_link_search_irq(int bus, int device, int pin)
555 uint8_t func, maxfunc;
557 /* See if we have a valid device at function 0. */
558 value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1);
559 if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
560 return (PCI_INVALID_IRQ);
561 if (value & PCIM_MFDEV)
562 maxfunc = PCI_FUNCMAX;
566 /* Scan all possible functions at this device. */
567 for (func = 0; func <= maxfunc; func++) {
568 value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4);
569 if (value == 0xffffffff)
571 value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
574 * See if it uses the pin in question. Note that the passed
575 * in pin uses 0 for A, .. 3 for D whereas the intpin
576 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
578 if (value != pin + 1)
580 value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
583 "ACPI: Found matching pin for %d.%d.INT%c at func %d: %d\n",
584 bus, device, pin + 'A', func, value);
585 if (value != PCI_INVALID_IRQ)
588 return (PCI_INVALID_IRQ);
592 * Find the link structure that corresponds to the resource index passed in
593 * via 'source_index'.
596 acpi_pci_link_lookup(device_t dev, int source_index)
598 struct acpi_pci_link_softc *sc;
601 ACPI_SERIAL_ASSERT(pci_link);
602 sc = device_get_softc(dev);
603 for (i = 0; i < sc->pl_num_links; i++)
604 if (sc->pl_links[i].l_res_index == source_index)
605 return (&sc->pl_links[i]);
610 acpi_pci_link_add_reference(device_t dev, int index, device_t pcib, int slot,
618 * Look up the PCI bus for the specified PCI bridge device. Note
619 * that the PCI bridge device might not have any children yet.
620 * However, looking up its bus number doesn't require a valid child
621 * device, so we just pass NULL.
623 if (BUS_READ_IVAR(pcib, NULL, PCIB_IVAR_BUS, &bus) != 0) {
624 device_printf(pcib, "Unable to read PCI bus number");
625 panic("PCI bridge without a bus number");
628 /* Bump the reference count. */
629 ACPI_SERIAL_BEGIN(pci_link);
630 link = acpi_pci_link_lookup(dev, index);
632 device_printf(dev, "apparently invalid index %d\n", index);
633 ACPI_SERIAL_END(pci_link);
636 link->l_references++;
638 pci_link_interrupt_weights[link->l_irq]++;
641 * The BIOS only routes interrupts via ISA IRQs using the ATPICs
642 * (8259As). Thus, if this link is routed via an ISA IRQ, go
643 * look to see if the BIOS routed an IRQ for this link at the
644 * indicated (bus, slot, pin). If so, we prefer that IRQ for
645 * this link and add that IRQ to our list of known-good IRQs.
646 * This provides a good work-around for link devices whose _CRS
647 * method is either broken or bogus. We only use the value
648 * returned by _CRS if we can't find a valid IRQ via this method
651 * If this link is not routed via an ISA IRQ (because we are using
652 * APIC for example), then don't bother looking up the BIOS IRQ
653 * as if we find one it won't be valid anyway.
655 if (!link->l_isa_irq) {
656 ACPI_SERIAL_END(pci_link);
660 /* Try to find a BIOS IRQ setting from any matching devices. */
661 bios_irq = acpi_pci_link_search_irq(bus, slot, pin);
662 if (!PCI_INTERRUPT_VALID(bios_irq)) {
663 ACPI_SERIAL_END(pci_link);
667 /* Validate the BIOS IRQ. */
668 if (!link_valid_irq(link, bios_irq)) {
669 device_printf(dev, "BIOS IRQ %u for %d.%d.INT%c is invalid\n",
670 bios_irq, (int)bus, slot, pin + 'A');
671 } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) {
672 link->l_bios_irq = bios_irq;
673 if (bios_irq < NUM_ISA_INTERRUPTS)
674 pci_link_bios_isa_irqs |= (1 << bios_irq);
675 if (bios_irq != link->l_initial_irq &&
676 PCI_INTERRUPT_VALID(link->l_initial_irq))
678 "BIOS IRQ %u does not match initial IRQ %u\n",
679 bios_irq, link->l_initial_irq);
680 } else if (bios_irq != link->l_bios_irq)
682 "BIOS IRQ %u for %d.%d.INT%c does not match previous BIOS IRQ %u\n",
683 bios_irq, (int)bus, slot, pin + 'A',
685 ACPI_SERIAL_END(pci_link);
689 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
691 ACPI_RESOURCE *resource, *end, newres, *resptr;
697 /* Fetch the _CRS. */
698 ACPI_SERIAL_ASSERT(pci_link);
699 crsbuf.Pointer = NULL;
700 crsbuf.Length = ACPI_ALLOCATE_BUFFER;
701 status = AcpiGetCurrentResources(acpi_get_handle(sc->pl_dev), &crsbuf);
702 if (ACPI_SUCCESS(status) && crsbuf.Pointer == NULL)
703 status = AE_NO_MEMORY;
704 if (ACPI_FAILURE(status)) {
706 device_printf(sc->pl_dev,
707 "Unable to fetch current resources: %s\n",
708 AcpiFormatException(status));
712 /* Fill in IRQ resources via link structures. */
713 srsbuf->Pointer = NULL;
716 in_dpf = DPF_OUTSIDE;
717 resource = (ACPI_RESOURCE *)crsbuf.Pointer;
718 end = (ACPI_RESOURCE *)((char *)crsbuf.Pointer + crsbuf.Length);
720 switch (resource->Type) {
721 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
724 /* We've started the first DPF. */
728 /* We've started the second DPF. */
730 "%s: Multiple dependent functions within a current resource",
736 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
737 /* We are finished with DPF parsing. */
738 KASSERT(in_dpf != DPF_OUTSIDE,
739 ("%s: end dpf when not parsing a dpf", __func__));
740 in_dpf = DPF_OUTSIDE;
743 case ACPI_RESOURCE_TYPE_IRQ:
744 MPASS(i < sc->pl_num_links);
745 MPASS(link->l_prs_template.Type == ACPI_RESOURCE_TYPE_IRQ);
746 newres = link->l_prs_template;
748 resptr->Data.Irq.InterruptCount = 1;
749 if (PCI_INTERRUPT_VALID(link->l_irq)) {
750 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
751 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
752 __func__, link->l_irq));
753 resptr->Data.Irq.Interrupts[0] = link->l_irq;
755 resptr->Data.Irq.Interrupts[0] = 0;
759 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
760 MPASS(i < sc->pl_num_links);
761 MPASS(link->l_prs_template.Type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ);
762 newres = link->l_prs_template;
764 resptr->Data.ExtendedIrq.InterruptCount = 1;
765 if (PCI_INTERRUPT_VALID(link->l_irq))
766 resptr->Data.ExtendedIrq.Interrupts[0] =
769 resptr->Data.ExtendedIrq.Interrupts[0] = 0;
776 if (resptr != NULL) {
777 status = acpi_AppendBufferResource(srsbuf, resptr);
778 if (ACPI_FAILURE(status)) {
779 device_printf(sc->pl_dev,
780 "Unable to build resources: %s\n",
781 AcpiFormatException(status));
782 if (srsbuf->Pointer != NULL)
783 AcpiOsFree(srsbuf->Pointer);
784 AcpiOsFree(crsbuf.Pointer);
788 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
790 resource = ACPI_NEXT_RESOURCE(resource);
794 AcpiOsFree(crsbuf.Pointer);
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 newres = link->l_prs_template;
815 if (newres.Type == ACPI_RESOURCE_TYPE_IRQ) {
817 /* Build an IRQ resource. */
818 newres.Data.Irq.InterruptCount = 1;
819 if (PCI_INTERRUPT_VALID(link->l_irq)) {
820 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
821 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
822 __func__, link->l_irq));
823 newres.Data.Irq.Interrupts[0] = link->l_irq;
825 newres.Data.Irq.Interrupts[0] = 0;
828 /* Build an ExtIRQ resuorce. */
829 newres.Data.ExtendedIrq.InterruptCount = 1;
830 if (PCI_INTERRUPT_VALID(link->l_irq))
831 newres.Data.ExtendedIrq.Interrupts[0] =
834 newres.Data.ExtendedIrq.Interrupts[0] = 0;
837 /* Add the new resource to the end of the _SRS buffer. */
838 status = acpi_AppendBufferResource(srsbuf, &newres);
839 if (ACPI_FAILURE(status)) {
840 device_printf(sc->pl_dev,
841 "Unable to build resources: %s\n",
842 AcpiFormatException(status));
843 if (srsbuf->Pointer != NULL)
844 AcpiOsFree(srsbuf->Pointer);
852 acpi_pci_link_route_irqs(device_t dev)
854 struct acpi_pci_link_softc *sc;
855 ACPI_RESOURCE *resource, *end;
861 ACPI_SERIAL_ASSERT(pci_link);
862 sc = device_get_softc(dev);
864 status = acpi_pci_link_srs_from_links(sc, &srsbuf);
866 status = acpi_pci_link_srs_from_crs(sc, &srsbuf);
868 /* Write out new resources via _SRS. */
869 status = AcpiSetCurrentResources(acpi_get_handle(dev), &srsbuf);
870 if (ACPI_FAILURE(status)) {
871 device_printf(dev, "Unable to route IRQs: %s\n",
872 AcpiFormatException(status));
873 AcpiOsFree(srsbuf.Pointer);
878 * Perform acpi_config_intr() on each IRQ resource if it was just
879 * routed for the first time.
883 resource = (ACPI_RESOURCE *)srsbuf.Pointer;
884 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length);
886 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
888 switch (resource->Type) {
889 case ACPI_RESOURCE_TYPE_IRQ:
890 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
891 MPASS(i < sc->pl_num_links);
894 * Only configure the interrupt and update the
895 * weights if this link has a valid IRQ and was
896 * previously unrouted.
898 if (!link->l_routed &&
899 PCI_INTERRUPT_VALID(link->l_irq)) {
900 link->l_routed = TRUE;
901 acpi_config_intr(dev, resource);
902 pci_link_interrupt_weights[link->l_irq] +=
909 resource = ACPI_NEXT_RESOURCE(resource);
913 AcpiOsFree(srsbuf.Pointer);
918 acpi_pci_link_resume(device_t dev)
920 struct acpi_pci_link_softc *sc;
925 * If all of our links are routed, then restore the link via _SRS,
926 * otherwise, disable the link via _DIS.
928 ACPI_SERIAL_BEGIN(pci_link);
929 sc = device_get_softc(dev);
931 for (i = 0; i < sc->pl_num_links; i++)
932 if (sc->pl_links[i].l_routed)
934 if (routed == sc->pl_num_links)
935 status = acpi_pci_link_route_irqs(dev);
937 AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL, NULL);
940 ACPI_SERIAL_END(pci_link);
941 if (ACPI_FAILURE(status))
948 * Pick an IRQ to use for this unrouted link.
951 acpi_pci_link_choose_irq(device_t dev, struct link *link)
953 char tunable_buffer[64], link_name[5];
954 u_int8_t best_irq, pos_irq;
955 int best_weight, pos_weight, i;
957 KASSERT(!link->l_routed, ("%s: link already routed", __func__));
958 KASSERT(!PCI_INTERRUPT_VALID(link->l_irq),
959 ("%s: link already has an IRQ", __func__));
961 /* Check for a tunable override. */
962 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), link_name,
963 sizeof(link_name)))) {
964 snprintf(tunable_buffer, sizeof(tunable_buffer),
965 "hw.pci.link.%s.%d.irq", link_name, link->l_res_index);
966 if (getenv_int(tunable_buffer, &i) && PCI_INTERRUPT_VALID(i)) {
967 if (!link_valid_irq(link, i))
969 "Warning, IRQ %d is not listed as valid\n",
973 snprintf(tunable_buffer, sizeof(tunable_buffer),
974 "hw.pci.link.%s.irq", link_name);
975 if (getenv_int(tunable_buffer, &i) && PCI_INTERRUPT_VALID(i)) {
976 if (!link_valid_irq(link, i))
978 "Warning, IRQ %d is not listed as valid\n",
985 * If we have a valid BIOS IRQ, use that. We trust what the BIOS
986 * says it routed over what _CRS says the link thinks is routed.
988 if (PCI_INTERRUPT_VALID(link->l_bios_irq))
989 return (link->l_bios_irq);
992 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS,
995 if (PCI_INTERRUPT_VALID(link->l_initial_irq))
996 return (link->l_initial_irq);
999 * Ok, we have no useful hints, so we have to pick from the
1000 * possible IRQs. For ISA IRQs we only use interrupts that
1001 * have already been used by the BIOS.
1003 best_irq = PCI_INVALID_IRQ;
1004 best_weight = INT_MAX;
1005 for (i = 0; i < link->l_num_irqs; i++) {
1006 pos_irq = link->l_irqs[i];
1007 if (pos_irq < NUM_ISA_INTERRUPTS &&
1008 (pci_link_bios_isa_irqs & 1 << pos_irq) == 0)
1010 pos_weight = pci_link_interrupt_weights[pos_irq];
1011 if (pos_weight < best_weight) {
1012 best_weight = pos_weight;
1018 * If this is an ISA IRQ, try using the SCI if it is also an ISA
1019 * interrupt as a fallback.
1021 if (link->l_isa_irq) {
1022 pos_irq = AcpiGbl_FADT.SciInterrupt;
1023 pos_weight = pci_link_interrupt_weights[pos_irq];
1024 if (pos_weight < best_weight) {
1025 best_weight = pos_weight;
1030 if (PCI_INTERRUPT_VALID(best_irq)) {
1032 device_printf(dev, "Picked IRQ %u with weight %d\n",
1033 best_irq, best_weight);
1035 device_printf(dev, "Unable to choose an IRQ\n");
1040 acpi_pci_link_route_interrupt(device_t dev, int index)
1044 if (acpi_disabled("pci_link"))
1045 return (PCI_INVALID_IRQ);
1047 ACPI_SERIAL_BEGIN(pci_link);
1048 link = acpi_pci_link_lookup(dev, index);
1050 panic("%s: apparently invalid index %d", __func__, index);
1053 * If this link device is already routed to an interrupt, just return
1054 * the interrupt it is routed to.
1056 if (link->l_routed) {
1057 KASSERT(PCI_INTERRUPT_VALID(link->l_irq),
1058 ("%s: link is routed but has an invalid IRQ", __func__));
1059 ACPI_SERIAL_END(pci_link);
1060 return (link->l_irq);
1063 /* Choose an IRQ if we need one. */
1064 if (!PCI_INTERRUPT_VALID(link->l_irq)) {
1065 link->l_irq = acpi_pci_link_choose_irq(dev, link);
1068 * Try to route the interrupt we picked. If it fails, then
1069 * assume the interrupt is not routed.
1071 if (PCI_INTERRUPT_VALID(link->l_irq)) {
1072 acpi_pci_link_route_irqs(dev);
1073 if (!link->l_routed)
1074 link->l_irq = PCI_INVALID_IRQ;
1077 ACPI_SERIAL_END(pci_link);
1079 return (link->l_irq);
1083 * This is gross, but we abuse the identify routine to perform one-time
1084 * SYSINIT() style initialization for the driver.
1087 acpi_pci_link_identify(driver_t *driver, device_t parent)
1091 * If the SCI is an ISA IRQ, add it to the bitmask of known good
1094 * XXX: If we are using the APIC, the SCI might have been
1095 * rerouted to an APIC pin in which case this is invalid. However,
1096 * if we are using the APIC, we also shouldn't be having any PCI
1097 * interrupts routed via ISA IRQs, so this is probably ok.
1099 if (AcpiGbl_FADT.SciInterrupt < NUM_ISA_INTERRUPTS)
1100 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT.SciInterrupt);
1103 static device_method_t acpi_pci_link_methods[] = {
1104 /* Device interface */
1105 DEVMETHOD(device_identify, acpi_pci_link_identify),
1106 DEVMETHOD(device_probe, acpi_pci_link_probe),
1107 DEVMETHOD(device_attach, acpi_pci_link_attach),
1108 DEVMETHOD(device_resume, acpi_pci_link_resume),
1113 static driver_t acpi_pci_link_driver = {
1115 acpi_pci_link_methods,
1116 sizeof(struct acpi_pci_link_softc),
1119 static devclass_t pci_link_devclass;
1121 DRIVER_MODULE(acpi_pci_link, acpi, acpi_pci_link_driver, pci_link_devclass, 0,
1123 MODULE_DEPEND(acpi_pci_link, acpi, 1, 1, 1);