2 * Copyright (c) 2002 Mitsuru IWASAKI <iwasaki@jp.kfreebsd.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
25 * __FBSDID("$FreeBSD: src/sys/dev/acpica/acpi_pci_link.c,v 1.56.2.1.6.1 2009/04/15 03:14:26 kensmith Exp $");
28 #define MPASS(ex) MPASS4(ex, #ex, __FILE__, __LINE__)
29 #define MPASS4(ex, what, file, line) \
30 KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line))
32 #include <sys/cdefs.h>
35 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
43 #include <dev/acpica5/acpivar.h>
44 #include <dev/acpica5/acpi_pcibvar.h>
46 #include <bus/pci/i386/pci_cfgreg.h>
47 #include <bus/pci/pcireg.h>
48 #include <bus/pci/pcivar.h>
51 /* Hooks for the ACPI CA debugging infrastructure. */
52 #define _COMPONENT ACPI_BUS
53 ACPI_MODULE_NAME("PCI_LINK")
55 ACPI_SERIAL_DECL(pci_link, "ACPI PCI link");
57 #define NUM_ISA_INTERRUPTS 16
58 #define NUM_ACPI_INTERRUPTS 256
61 * An ACPI PCI link device may contain multiple links. Each link has its
62 * own ACPI resource. _PRT entries specify which link is being used via
65 * XXX: A note about Source Indices and DPFs: Currently we assume that
66 * the DPF start and end tags are not counted towards the index that
67 * Source Index corresponds to. Also, we assume that when DPFs are in use
68 * they various sets overlap in terms of Indices. Here's an example
69 * resource list indicating these assumptions:
83 * The XXX is because I'm not sure if this is a valid assumption to make.
86 /* States during DPF processing. */
93 struct acpi_pci_link_softc {
96 struct link *pl_links;
101 struct acpi_pci_link_softc *l_sc;
104 uint8_t l_initial_irq;
111 ACPI_RESOURCE l_prs_template;
114 struct link_count_request {
119 struct link_res_request {
120 struct acpi_pci_link_softc *sc;
126 MALLOC_DEFINE(M_PCI_LINK, "pci_link", "ACPI PCI Link structures");
128 static int pci_link_interrupt_weights[NUM_ACPI_INTERRUPTS];
129 static int pci_link_bios_isa_irqs;
131 static char *pci_link_ids[] = { "PNP0C0F", NULL };
134 * Fetch the short name associated with an ACPI handle and save it in the
138 acpi_short_name(ACPI_HANDLE handle, char *buffer, size_t buflen)
143 buf.Pointer = buffer;
144 return (AcpiGetName(handle, ACPI_SINGLE_NAME, &buf));
148 acpi_pci_link_probe(device_t dev)
150 char descr[28], name[12];
153 * We explicitly do not check _STA since not all systems set it to
156 if (!acpi_enabled("pci_link") ||
157 ACPI_ID_PROBE(device_get_parent(dev), dev, pci_link_ids) == NULL)
160 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), name,
162 ksnprintf(descr, sizeof(descr), "ACPI PCI Link %s", name);
163 device_set_desc_copy(dev, descr);
165 device_set_desc(dev, "ACPI PCI Link");
171 acpi_count_irq_resources(ACPI_RESOURCE *res, void *context)
173 struct link_count_request *req;
175 req = (struct link_count_request *)context;
177 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
178 switch (req->in_dpf) {
180 /* We've started the first DPF. */
181 req->in_dpf = DPF_FIRST;
184 /* We've started the second DPF. */
185 req->in_dpf = DPF_IGNORE;
189 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
190 /* We are finished with DPF parsing. */
191 KASSERT(req->in_dpf != DPF_OUTSIDE,
192 ("%s: end dpf when not parsing a dpf", __func__));
193 req->in_dpf = DPF_OUTSIDE;
195 case ACPI_RESOURCE_TYPE_IRQ:
196 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
198 * Don't count resources if we are in a DPF set that we are
201 if (req->in_dpf != DPF_IGNORE)
208 link_add_crs(ACPI_RESOURCE *res, void *context)
210 struct link_res_request *req;
213 ACPI_SERIAL_ASSERT(pci_link);
214 req = (struct link_res_request *)context;
216 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
217 switch (req->in_dpf) {
219 /* We've started the first DPF. */
220 req->in_dpf = DPF_FIRST;
223 /* We've started the second DPF. */
225 "%s: Multiple dependent functions within a current resource",
230 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
231 /* We are finished with DPF parsing. */
232 KASSERT(req->in_dpf != DPF_OUTSIDE,
233 ("%s: end dpf when not parsing a dpf", __func__));
234 req->in_dpf = DPF_OUTSIDE;
236 case ACPI_RESOURCE_TYPE_IRQ:
237 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
238 KASSERT(req->link_index < req->sc->pl_num_links,
239 ("%s: array boundary violation", __func__));
240 link = &req->sc->pl_links[req->link_index];
241 link->l_res_index = req->res_index;
246 * Only use the current value if there's one IRQ. Some
247 * systems return multiple IRQs (which is nonsense for _CRS)
248 * when the link hasn't been programmed.
250 if (res->Type == ACPI_RESOURCE_TYPE_IRQ) {
251 if (res->Data.Irq.InterruptCount == 1)
252 link->l_irq = res->Data.Irq.Interrupts[0];
253 } else if (res->Data.ExtendedIrq.InterruptCount == 1)
254 link->l_irq = res->Data.ExtendedIrq.Interrupts[0];
257 * An IRQ of zero means that the link isn't routed.
259 if (link->l_irq == 0)
260 link->l_irq = PCI_INVALID_IRQ;
269 * Populate the set of possible IRQs for each device.
272 link_add_prs(ACPI_RESOURCE *res, void *context)
274 struct link_res_request *req;
277 UINT32 *ext_irqs = NULL;
278 int i, is_ext_irq = 1;
280 ACPI_SERIAL_ASSERT(pci_link);
281 req = (struct link_res_request *)context;
283 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
284 switch (req->in_dpf) {
286 /* We've started the first DPF. */
287 req->in_dpf = DPF_FIRST;
290 /* We've started the second DPF. */
291 req->in_dpf = DPF_IGNORE;
295 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
296 /* We are finished with DPF parsing. */
297 KASSERT(req->in_dpf != DPF_OUTSIDE,
298 ("%s: end dpf when not parsing a dpf", __func__));
299 req->in_dpf = DPF_OUTSIDE;
301 case ACPI_RESOURCE_TYPE_IRQ:
304 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
306 * Don't parse resources if we are in a DPF set that we are
309 if (req->in_dpf == DPF_IGNORE)
312 KASSERT(req->link_index < req->sc->pl_num_links,
313 ("%s: array boundary violation", __func__));
314 link = &req->sc->pl_links[req->link_index];
315 if (link->l_res_index == -1) {
316 KASSERT(req->sc->pl_crs_bad,
317 ("res_index should be set"));
318 link->l_res_index = req->res_index;
324 * Stash a copy of the resource for later use when doing
327 bcopy(res, &link->l_prs_template, sizeof(ACPI_RESOURCE));
330 res->Data.ExtendedIrq.InterruptCount;
331 ext_irqs = res->Data.ExtendedIrq.Interrupts;
333 link->l_num_irqs = res->Data.Irq.InterruptCount;
334 irqs = res->Data.Irq.Interrupts;
336 if (link->l_num_irqs == 0)
340 * Save a list of the valid IRQs. Also, if all of the
341 * valid IRQs are ISA IRQs, then mark this link as
342 * routed via an ISA interrupt.
344 link->l_isa_irq = TRUE;
346 link->l_irqs = kmalloc(sizeof(int) * link->l_num_irqs,
347 M_PCI_LINK, M_WAITOK | M_ZERO);
348 for (i = 0; i < link->l_num_irqs; i++) {
350 link->l_irqs[i] = ext_irqs[i];
351 if (ext_irqs[i] >= NUM_ISA_INTERRUPTS)
352 link->l_isa_irq = FALSE;
354 link->l_irqs[i] = irqs[i];
355 if (irqs[i] >= NUM_ISA_INTERRUPTS)
356 link->l_isa_irq = FALSE;
361 if (req->in_dpf == DPF_IGNORE)
363 if (req->sc->pl_crs_bad)
364 device_printf(req->sc->pl_dev,
365 "Warning: possible resource %d will be lost during _SRS\n",
373 link_valid_irq(struct link *link, int irq)
377 ACPI_SERIAL_ASSERT(pci_link);
379 /* Invalid interrupts are never valid. */
380 if (!PCI_INTERRUPT_VALID(irq))
383 /* Any interrupt in the list of possible interrupts is valid. */
384 for (i = 0; i < link->l_num_irqs; i++)
385 if (link->l_irqs[i] == irq)
389 * For links routed via an ISA interrupt, if the SCI is routed via
390 * an ISA interrupt, the SCI is always treated as a valid IRQ.
392 if (link->l_isa_irq && AcpiGbl_FADT.SciInterrupt == irq &&
393 irq < NUM_ISA_INTERRUPTS)
396 /* If the interrupt wasn't found in the list it is not valid. */
401 acpi_pci_link_dump(struct acpi_pci_link_softc *sc, int header, const char *tag)
407 ACPI_SERIAL_ASSERT(pci_link);
409 ksnprintf(buf, sizeof(buf), "%s:",
410 device_get_nameunit(sc->pl_dev));
411 kprintf("%-16.16s Index IRQ Rtd Ref IRQs\n", buf);
413 for (i = 0; i < sc->pl_num_links; i++) {
414 link = &sc->pl_links[i];
415 kprintf(" %-14.14s %5d %3d %c %3d ", i == 0 ? tag : "", i,
416 link->l_irq, link->l_routed ? 'Y' : 'N',
418 if (link->l_num_irqs == 0)
420 else for (j = 0; j < link->l_num_irqs; j++)
421 kprintf(" %d", link->l_irqs[j]);
427 acpi_pci_link_attach(device_t dev)
429 struct acpi_pci_link_softc *sc;
430 struct link_count_request creq;
431 struct link_res_request rreq;
435 sc = device_get_softc(dev);
437 ACPI_SERIAL_INIT(pci_link);
438 ACPI_SERIAL_BEGIN(pci_link);
441 * Count the number of current resources so we know how big of
442 * a link array to allocate. On some systems, _CRS is broken,
443 * so for those systems try to derive the count from _PRS instead.
445 creq.in_dpf = DPF_OUTSIDE;
447 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
448 acpi_count_irq_resources, &creq);
449 sc->pl_crs_bad = ACPI_FAILURE(status);
450 if (sc->pl_crs_bad) {
451 creq.in_dpf = DPF_OUTSIDE;
453 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
454 acpi_count_irq_resources, &creq);
455 if (ACPI_FAILURE(status)) {
457 "Unable to parse _CRS or _PRS: %s\n",
458 AcpiFormatException(status));
459 ACPI_SERIAL_END(pci_link);
463 sc->pl_num_links = creq.count;
464 if (creq.count == 0) {
465 ACPI_SERIAL_END(pci_link);
468 sc->pl_links = kmalloc(sizeof(struct link) * sc->pl_num_links,
469 M_PCI_LINK, M_WAITOK | M_ZERO);
471 /* Initialize the child links. */
472 for (i = 0; i < sc->pl_num_links; i++) {
473 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
474 sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ;
475 sc->pl_links[i].l_sc = sc;
476 sc->pl_links[i].l_isa_irq = FALSE;
477 sc->pl_links[i].l_res_index = -1;
480 /* Try to read the current settings from _CRS if it is valid. */
481 if (!sc->pl_crs_bad) {
482 rreq.in_dpf = DPF_OUTSIDE;
486 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
487 link_add_crs, &rreq);
488 if (ACPI_FAILURE(status)) {
489 device_printf(dev, "Unable to parse _CRS: %s\n",
490 AcpiFormatException(status));
496 * Try to read the possible settings from _PRS. Note that if the
497 * _CRS is toast, we depend on having a working _PRS. However, if
498 * _CRS works, then it is ok for _PRS to be missing.
500 rreq.in_dpf = DPF_OUTSIDE;
504 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
505 link_add_prs, &rreq);
506 if (ACPI_FAILURE(status) &&
507 (status != AE_NOT_FOUND || sc->pl_crs_bad)) {
508 device_printf(dev, "Unable to parse _PRS: %s\n",
509 AcpiFormatException(status));
513 acpi_pci_link_dump(sc, 1, "Initial Probe");
515 /* Verify initial IRQs if we have _PRS. */
516 if (status != AE_NOT_FOUND)
517 for (i = 0; i < sc->pl_num_links; i++)
518 if (!link_valid_irq(&sc->pl_links[i],
519 sc->pl_links[i].l_irq))
520 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
522 acpi_pci_link_dump(sc, 0, "Validation");
524 /* Save initial IRQs. */
525 for (i = 0; i < sc->pl_num_links; i++)
526 sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq;
529 * Try to disable this link. If successful, set the current IRQ to
530 * zero and flags to indicate this link is not routed. If we can't
531 * run _DIS (i.e., the method doesn't exist), assume the initial
532 * IRQ was routed by the BIOS.
534 if (ACPI_SUCCESS(AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL,
536 for (i = 0; i < sc->pl_num_links; i++)
537 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
539 for (i = 0; i < sc->pl_num_links; i++)
540 if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq))
541 sc->pl_links[i].l_routed = TRUE;
543 acpi_pci_link_dump(sc, 0, "After Disable");
544 ACPI_SERIAL_END(pci_link);
547 ACPI_SERIAL_END(pci_link);
548 for (i = 0; i < sc->pl_num_links; i++)
549 if (sc->pl_links[i].l_irqs != NULL)
550 kfree(sc->pl_links[i].l_irqs, M_PCI_LINK);
551 kfree(sc->pl_links, M_PCI_LINK);
555 /* XXX: Note that this is identical to pci_pir_search_irq(). */
557 acpi_pci_link_search_irq(int bus, int device, int pin)
560 uint8_t func, maxfunc;
562 /* See if we have a valid device at function 0. */
563 value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1);
564 if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
565 return (PCI_INVALID_IRQ);
566 if (value & PCIM_MFDEV)
567 maxfunc = PCI_FUNCMAX;
571 /* Scan all possible functions at this device. */
572 for (func = 0; func <= maxfunc; func++) {
573 value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4);
574 if (value == 0xffffffff)
576 value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
579 * See if it uses the pin in question. Note that the passed
580 * in pin uses 0 for A, .. 3 for D whereas the intpin
581 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
583 if (value != pin + 1)
585 value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
588 "ACPI: Found matching pin for %d.%d.INT%c at func %d: %d\n",
589 bus, device, pin + 'A', func, value);
590 if (value != PCI_INVALID_IRQ)
593 return (PCI_INVALID_IRQ);
597 * Find the link structure that corresponds to the resource index passed in
598 * via 'source_index'.
601 acpi_pci_link_lookup(device_t dev, int source_index)
603 struct acpi_pci_link_softc *sc;
606 ACPI_SERIAL_ASSERT(pci_link);
607 sc = device_get_softc(dev);
608 for (i = 0; i < sc->pl_num_links; i++)
609 if (sc->pl_links[i].l_res_index == source_index)
610 return (&sc->pl_links[i]);
615 acpi_pci_link_add_reference(device_t dev, int index, device_t pcib, int slot,
623 * Look up the PCI bus for the specified PCI bridge device. Note
624 * that the PCI bridge device might not have any children yet.
625 * However, looking up its bus number doesn't require a valid child
626 * device, so we just pass NULL.
628 if (BUS_READ_IVAR(pcib, NULL, PCIB_IVAR_BUS, &bus) != 0) {
629 device_printf(pcib, "Unable to read PCI bus number");
630 panic("PCI bridge without a bus number");
633 /* Bump the reference count. */
634 ACPI_SERIAL_BEGIN(pci_link);
635 link = acpi_pci_link_lookup(dev, index);
637 device_printf(dev, "apparently invalid index %d\n", index);
638 ACPI_SERIAL_END(pci_link);
641 link->l_references++;
643 pci_link_interrupt_weights[link->l_irq]++;
646 * The BIOS only routes interrupts via ISA IRQs using the ATPICs
647 * (8259As). Thus, if this link is routed via an ISA IRQ, go
648 * look to see if the BIOS routed an IRQ for this link at the
649 * indicated (bus, slot, pin). If so, we prefer that IRQ for
650 * this link and add that IRQ to our list of known-good IRQs.
651 * This provides a good work-around for link devices whose _CRS
652 * method is either broken or bogus. We only use the value
653 * returned by _CRS if we can't find a valid IRQ via this method
656 * If this link is not routed via an ISA IRQ (because we are using
657 * APIC for example), then don't bother looking up the BIOS IRQ
658 * as if we find one it won't be valid anyway.
660 if (!link->l_isa_irq) {
661 ACPI_SERIAL_END(pci_link);
665 /* Try to find a BIOS IRQ setting from any matching devices. */
666 bios_irq = acpi_pci_link_search_irq(bus, slot, pin);
667 if (!PCI_INTERRUPT_VALID(bios_irq)) {
668 ACPI_SERIAL_END(pci_link);
672 /* Validate the BIOS IRQ. */
673 if (!link_valid_irq(link, bios_irq)) {
674 device_printf(dev, "BIOS IRQ %u for %d.%d.INT%c is invalid\n",
675 bios_irq, (int)bus, slot, pin + 'A');
676 } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) {
677 link->l_bios_irq = bios_irq;
678 if (bios_irq < NUM_ISA_INTERRUPTS)
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))
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)
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',
690 ACPI_SERIAL_END(pci_link);
694 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
696 ACPI_RESOURCE *resource, *end, newres, *resptr;
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)) {
711 device_printf(sc->pl_dev,
712 "Unable to fetch current resources: %s\n",
713 AcpiFormatException(status));
717 /* Fill in IRQ resources via link structures. */
718 srsbuf->Pointer = NULL;
721 in_dpf = DPF_OUTSIDE;
722 resource = (ACPI_RESOURCE *)crsbuf.Pointer;
723 end = (ACPI_RESOURCE *)((char *)crsbuf.Pointer + crsbuf.Length);
725 switch (resource->Type) {
726 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
729 /* We've started the first DPF. */
733 /* We've started the second DPF. */
735 "%s: Multiple dependent functions within a current resource",
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;
748 case ACPI_RESOURCE_TYPE_IRQ:
749 MPASS(i < sc->pl_num_links);
750 MPASS(link->l_prs_template.Type == ACPI_RESOURCE_TYPE_IRQ);
751 newres = link->l_prs_template;
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;
760 resptr->Data.Irq.Interrupts[0] = 0;
764 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
765 MPASS(i < sc->pl_num_links);
766 MPASS(link->l_prs_template.Type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ);
767 newres = link->l_prs_template;
769 resptr->Data.ExtendedIrq.InterruptCount = 1;
770 if (PCI_INTERRUPT_VALID(link->l_irq))
771 resptr->Data.ExtendedIrq.Interrupts[0] =
774 resptr->Data.ExtendedIrq.Interrupts[0] = 0;
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);
793 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
795 resource = ACPI_NEXT_RESOURCE(resource);
799 AcpiOsFree(crsbuf.Pointer);
804 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc,
807 ACPI_RESOURCE newres;
812 /* Start off with an empty buffer. */
813 srsbuf->Pointer = NULL;
815 for (i = 0; i < sc->pl_num_links; i++) {
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) {
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;
830 newres.Data.Irq.Interrupts[0] = 0;
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] =
839 newres.Data.ExtendedIrq.Interrupts[0] = 0;
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);
857 acpi_pci_link_route_irqs(device_t dev)
859 struct acpi_pci_link_softc *sc;
860 ACPI_RESOURCE *resource, *end;
866 ACPI_SERIAL_ASSERT(pci_link);
867 sc = device_get_softc(dev);
869 status = acpi_pci_link_srs_from_links(sc, &srsbuf);
871 status = acpi_pci_link_srs_from_crs(sc, &srsbuf);
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);
883 * Perform acpi_config_intr() on each IRQ resource if it was just
884 * routed for the first time.
888 resource = (ACPI_RESOURCE *)srsbuf.Pointer;
889 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length);
891 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
893 switch (resource->Type) {
894 case ACPI_RESOURCE_TYPE_IRQ:
895 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
896 MPASS(i < sc->pl_num_links);
899 * Only configure the interrupt and update the
900 * weights if this link has a valid IRQ and was
901 * previously unrouted.
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] +=
914 resource = ACPI_NEXT_RESOURCE(resource);
918 AcpiOsFree(srsbuf.Pointer);
923 acpi_pci_link_resume(device_t dev)
925 struct acpi_pci_link_softc *sc;
930 * If all of our links are routed, then restore the link via _SRS,
931 * otherwise, disable the link via _DIS.
933 ACPI_SERIAL_BEGIN(pci_link);
934 sc = device_get_softc(dev);
936 for (i = 0; i < sc->pl_num_links; i++)
937 if (sc->pl_links[i].l_routed)
939 if (routed == sc->pl_num_links)
940 status = acpi_pci_link_route_irqs(dev);
942 AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL, NULL);
945 ACPI_SERIAL_END(pci_link);
946 if (ACPI_FAILURE(status))
953 * Pick an IRQ to use for this unrouted link.
956 acpi_pci_link_choose_irq(device_t dev, struct link *link)
958 char tunable_buffer[64], link_name[5];
959 u_int8_t best_irq, pos_irq;
960 int best_weight, pos_weight, i;
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__));
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))
974 "Warning, IRQ %d is not listed as valid\n",
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))
983 "Warning, IRQ %d is not listed as valid\n",
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.
993 if (PCI_INTERRUPT_VALID(link->l_bios_irq))
994 return (link->l_bios_irq);
997 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS,
1000 if (PCI_INTERRUPT_VALID(link->l_initial_irq))
1001 return (link->l_initial_irq);
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.
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)
1015 pos_weight = pci_link_interrupt_weights[pos_irq];
1016 if (pos_weight < best_weight) {
1017 best_weight = pos_weight;
1023 * If this is an ISA IRQ, try using the SCI if it is also an ISA
1024 * interrupt as a fallback.
1026 if (link->l_isa_irq) {
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;
1035 if (PCI_INTERRUPT_VALID(best_irq)) {
1037 device_printf(dev, "Picked IRQ %u with weight %d\n",
1038 best_irq, best_weight);
1040 device_printf(dev, "Unable to choose an IRQ\n");
1045 acpi_pci_link_route_interrupt(device_t dev, int index)
1049 if (!acpi_enabled("pci_link"))
1050 return (PCI_INVALID_IRQ);
1052 ACPI_SERIAL_BEGIN(pci_link);
1053 link = acpi_pci_link_lookup(dev, index);
1055 panic("%s: apparently invalid index %d", __func__, index);
1058 * If this link device is already routed to an interrupt, just return
1059 * the interrupt it is routed to.
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);
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);
1073 * Try to route the interrupt we picked. If it fails, then
1074 * assume the interrupt is not routed.
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;
1082 ACPI_SERIAL_END(pci_link);
1083 return (link->l_irq);
1087 * This is gross, but we abuse the identify routine to perform one-time
1088 * SYSINIT() style initialization for the driver.
1091 acpi_pci_link_identify(driver_t *driver, device_t parent)
1095 * If the SCI is an ISA IRQ, add it to the bitmask of known good
1098 * XXX: If we are using the APIC, the SCI might have been
1099 * rerouted to an APIC pin in which case this is invalid. However,
1100 * if we are using the APIC, we also shouldn't be having any PCI
1101 * interrupts routed via ISA IRQs, so this is probably ok.
1103 if (AcpiGbl_FADT.SciInterrupt < NUM_ISA_INTERRUPTS)
1104 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT.SciInterrupt);
1107 static device_method_t acpi_pci_link_methods[] = {
1108 /* Device interface */
1109 DEVMETHOD(device_identify, acpi_pci_link_identify),
1110 DEVMETHOD(device_probe, acpi_pci_link_probe),
1111 DEVMETHOD(device_attach, acpi_pci_link_attach),
1112 DEVMETHOD(device_resume, acpi_pci_link_resume),
1117 static driver_t acpi_pci_link_driver = {
1119 acpi_pci_link_methods,
1120 sizeof(struct acpi_pci_link_softc),
1123 static devclass_t pci_link_devclass;
1125 DRIVER_MODULE(acpi_pci_link, acpi, acpi_pci_link_driver, pci_link_devclass, 0,
1127 MODULE_DEPEND(acpi_pci_link, acpi, 1, 1, 1);