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
26 * $FreeBSD: src/sys/dev/acpica/acpi_pci_link.c,v 1.56.2.1.6.1 2009/04/15 03:14:26 kensmith Exp $
29 #define MPASS(ex) MPASS4(ex, #ex, __FILE__, __LINE__)
30 #define MPASS4(ex, what, file, line) \
31 KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line))
34 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/limits.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
42 #include <dev/acpica5/acpivar.h>
43 #include <dev/acpica5/acpi_pcibvar.h>
45 #include <bus/pci/i386/pci_cfgreg.h>
46 #include <bus/pci/pcireg.h>
47 #include <bus/pci/pcivar.h>
50 /* Hooks for the ACPI CA debugging infrastructure. */
51 #define _COMPONENT ACPI_BUS
52 ACPI_MODULE_NAME("PCI_LINK")
54 ACPI_SERIAL_DECL(pci_link, "ACPI PCI link");
56 #define NUM_ISA_INTERRUPTS 16
57 #define NUM_ACPI_INTERRUPTS 256
60 * An ACPI PCI link device may contain multiple links. Each link has its
61 * own ACPI resource. _PRT entries specify which link is being used via
64 * XXX: A note about Source Indices and DPFs: Currently we assume that
65 * the DPF start and end tags are not counted towards the index that
66 * Source Index corresponds to. Also, we assume that when DPFs are in use
67 * they various sets overlap in terms of Indices. Here's an example
68 * resource list indicating these assumptions:
82 * The XXX is because I'm not sure if this is a valid assumption to make.
85 /* States during DPF processing. */
92 struct acpi_pci_link_softc {
95 struct link *pl_links;
100 struct acpi_pci_link_softc *l_sc;
103 uint8_t l_initial_irq;
110 ACPI_RESOURCE l_prs_template;
113 struct link_count_request {
118 struct link_res_request {
119 struct acpi_pci_link_softc *sc;
125 MALLOC_DEFINE(M_PCI_LINK, "pci_link", "ACPI PCI Link structures");
127 static int pci_link_interrupt_weights[NUM_ACPI_INTERRUPTS];
128 static int pci_link_bios_isa_irqs;
130 static char *pci_link_ids[] = { "PNP0C0F", NULL };
133 * Fetch the short name associated with an ACPI handle and save it in the
137 acpi_short_name(ACPI_HANDLE handle, char *buffer, size_t buflen)
142 buf.Pointer = buffer;
143 return (AcpiGetName(handle, ACPI_SINGLE_NAME, &buf));
147 acpi_pci_link_probe(device_t dev)
149 char descr[28], name[12];
152 * We explicitly do not check _STA since not all systems set it to
155 if (!acpi_enabled("pci_link") ||
156 ACPI_ID_PROBE(device_get_parent(dev), dev, pci_link_ids) == NULL)
159 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), name,
161 ksnprintf(descr, sizeof(descr), "ACPI PCI Link %s", name);
162 device_set_desc_copy(dev, descr);
164 device_set_desc(dev, "ACPI PCI Link");
170 acpi_count_irq_resources(ACPI_RESOURCE *res, void *context)
172 struct link_count_request *req;
174 req = (struct link_count_request *)context;
176 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
177 switch (req->in_dpf) {
179 /* We've started the first DPF. */
180 req->in_dpf = DPF_FIRST;
183 /* We've started the second DPF. */
184 req->in_dpf = DPF_IGNORE;
188 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
189 /* We are finished with DPF parsing. */
190 KASSERT(req->in_dpf != DPF_OUTSIDE,
191 ("%s: end dpf when not parsing a dpf", __func__));
192 req->in_dpf = DPF_OUTSIDE;
194 case ACPI_RESOURCE_TYPE_IRQ:
195 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
197 * Don't count resources if we are in a DPF set that we are
200 if (req->in_dpf != DPF_IGNORE)
207 link_add_crs(ACPI_RESOURCE *res, void *context)
209 struct link_res_request *req;
212 ACPI_SERIAL_ASSERT(pci_link);
213 req = (struct link_res_request *)context;
215 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
216 switch (req->in_dpf) {
218 /* We've started the first DPF. */
219 req->in_dpf = DPF_FIRST;
222 /* We've started the second DPF. */
224 "%s: Multiple dependent functions within a current resource",
229 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
230 /* We are finished with DPF parsing. */
231 KASSERT(req->in_dpf != DPF_OUTSIDE,
232 ("%s: end dpf when not parsing a dpf", __func__));
233 req->in_dpf = DPF_OUTSIDE;
235 case ACPI_RESOURCE_TYPE_IRQ:
236 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
237 KASSERT(req->link_index < req->sc->pl_num_links,
238 ("%s: array boundary violation", __func__));
239 link = &req->sc->pl_links[req->link_index];
240 link->l_res_index = req->res_index;
245 * Only use the current value if there's one IRQ. Some
246 * systems return multiple IRQs (which is nonsense for _CRS)
247 * when the link hasn't been programmed.
249 if (res->Type == ACPI_RESOURCE_TYPE_IRQ) {
250 if (res->Data.Irq.InterruptCount == 1)
251 link->l_irq = res->Data.Irq.Interrupts[0];
252 } else if (res->Data.ExtendedIrq.InterruptCount == 1)
253 link->l_irq = res->Data.ExtendedIrq.Interrupts[0];
256 * An IRQ of zero means that the link isn't routed.
258 if (link->l_irq == 0)
259 link->l_irq = PCI_INVALID_IRQ;
268 * Populate the set of possible IRQs for each device.
271 link_add_prs(ACPI_RESOURCE *res, void *context)
273 struct link_res_request *req;
276 UINT32 *ext_irqs = NULL;
277 int i, is_ext_irq = 1;
279 ACPI_SERIAL_ASSERT(pci_link);
280 req = (struct link_res_request *)context;
282 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
283 switch (req->in_dpf) {
285 /* We've started the first DPF. */
286 req->in_dpf = DPF_FIRST;
289 /* We've started the second DPF. */
290 req->in_dpf = DPF_IGNORE;
294 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
295 /* We are finished with DPF parsing. */
296 KASSERT(req->in_dpf != DPF_OUTSIDE,
297 ("%s: end dpf when not parsing a dpf", __func__));
298 req->in_dpf = DPF_OUTSIDE;
300 case ACPI_RESOURCE_TYPE_IRQ:
303 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
305 * Don't parse resources if we are in a DPF set that we are
308 if (req->in_dpf == DPF_IGNORE)
311 KASSERT(req->link_index < req->sc->pl_num_links,
312 ("%s: array boundary violation", __func__));
313 link = &req->sc->pl_links[req->link_index];
314 if (link->l_res_index == -1) {
315 KASSERT(req->sc->pl_crs_bad,
316 ("res_index should be set"));
317 link->l_res_index = req->res_index;
323 * Stash a copy of the resource for later use when doing
326 bcopy(res, &link->l_prs_template, sizeof(ACPI_RESOURCE));
329 res->Data.ExtendedIrq.InterruptCount;
330 ext_irqs = res->Data.ExtendedIrq.Interrupts;
332 link->l_num_irqs = res->Data.Irq.InterruptCount;
333 irqs = res->Data.Irq.Interrupts;
335 if (link->l_num_irqs == 0)
339 * Save a list of the valid IRQs. Also, if all of the
340 * valid IRQs are ISA IRQs, then mark this link as
341 * routed via an ISA interrupt.
343 link->l_isa_irq = TRUE;
345 link->l_irqs = kmalloc(sizeof(int) * link->l_num_irqs,
346 M_PCI_LINK, M_WAITOK | M_ZERO);
347 for (i = 0; i < link->l_num_irqs; i++) {
349 link->l_irqs[i] = ext_irqs[i];
350 if (ext_irqs[i] >= NUM_ISA_INTERRUPTS)
351 link->l_isa_irq = FALSE;
353 link->l_irqs[i] = irqs[i];
354 if (irqs[i] >= NUM_ISA_INTERRUPTS)
355 link->l_isa_irq = FALSE;
360 if (req->in_dpf == DPF_IGNORE)
362 if (req->sc->pl_crs_bad)
363 device_printf(req->sc->pl_dev,
364 "Warning: possible resource %d will be lost during _SRS\n",
372 link_valid_irq(struct link *link, int irq)
376 ACPI_SERIAL_ASSERT(pci_link);
378 /* Invalid interrupts are never valid. */
379 if (!PCI_INTERRUPT_VALID(irq))
382 /* Any interrupt in the list of possible interrupts is valid. */
383 for (i = 0; i < link->l_num_irqs; i++)
384 if (link->l_irqs[i] == irq)
388 * For links routed via an ISA interrupt, if the SCI is routed via
389 * an ISA interrupt, the SCI is always treated as a valid IRQ.
391 if (link->l_isa_irq && AcpiGbl_FADT.SciInterrupt == irq &&
392 irq < NUM_ISA_INTERRUPTS)
395 /* If the interrupt wasn't found in the list it is not valid. */
400 acpi_pci_link_dump(struct acpi_pci_link_softc *sc, int header, const char *tag)
406 ACPI_SERIAL_ASSERT(pci_link);
408 ksnprintf(buf, sizeof(buf), "%s:",
409 device_get_nameunit(sc->pl_dev));
410 kprintf("%-16.16s Index IRQ Rtd Ref IRQs\n", buf);
412 for (i = 0; i < sc->pl_num_links; i++) {
413 link = &sc->pl_links[i];
414 kprintf(" %-14.14s %5d %3d %c %3d ", i == 0 ? tag : "", i,
415 link->l_irq, link->l_routed ? 'Y' : 'N',
417 if (link->l_num_irqs == 0)
419 else for (j = 0; j < link->l_num_irqs; j++)
420 kprintf(" %d", link->l_irqs[j]);
426 acpi_pci_link_attach(device_t dev)
428 struct acpi_pci_link_softc *sc;
429 struct link_count_request creq;
430 struct link_res_request rreq;
434 sc = device_get_softc(dev);
436 ACPI_SERIAL_INIT(pci_link);
437 ACPI_SERIAL_BEGIN(pci_link);
440 * Count the number of current resources so we know how big of
441 * a link array to allocate. On some systems, _CRS is broken,
442 * so for those systems try to derive the count from _PRS instead.
444 creq.in_dpf = DPF_OUTSIDE;
446 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
447 acpi_count_irq_resources, &creq);
448 sc->pl_crs_bad = ACPI_FAILURE(status);
449 if (sc->pl_crs_bad) {
450 creq.in_dpf = DPF_OUTSIDE;
452 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
453 acpi_count_irq_resources, &creq);
454 if (ACPI_FAILURE(status)) {
456 "Unable to parse _CRS or _PRS: %s\n",
457 AcpiFormatException(status));
458 ACPI_SERIAL_END(pci_link);
462 sc->pl_num_links = creq.count;
463 if (creq.count == 0) {
464 ACPI_SERIAL_END(pci_link);
467 sc->pl_links = kmalloc(sizeof(struct link) * sc->pl_num_links,
468 M_PCI_LINK, M_WAITOK | M_ZERO);
470 /* Initialize the child links. */
471 for (i = 0; i < sc->pl_num_links; i++) {
472 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
473 sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ;
474 sc->pl_links[i].l_sc = sc;
475 sc->pl_links[i].l_isa_irq = FALSE;
476 sc->pl_links[i].l_res_index = -1;
479 /* Try to read the current settings from _CRS if it is valid. */
480 if (!sc->pl_crs_bad) {
481 rreq.in_dpf = DPF_OUTSIDE;
485 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
486 link_add_crs, &rreq);
487 if (ACPI_FAILURE(status)) {
488 device_printf(dev, "Unable to parse _CRS: %s\n",
489 AcpiFormatException(status));
495 * Try to read the possible settings from _PRS. Note that if the
496 * _CRS is toast, we depend on having a working _PRS. However, if
497 * _CRS works, then it is ok for _PRS to be missing.
499 rreq.in_dpf = DPF_OUTSIDE;
503 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
504 link_add_prs, &rreq);
505 if (ACPI_FAILURE(status) &&
506 (status != AE_NOT_FOUND || sc->pl_crs_bad)) {
507 device_printf(dev, "Unable to parse _PRS: %s\n",
508 AcpiFormatException(status));
512 acpi_pci_link_dump(sc, 1, "Initial Probe");
514 /* Verify initial IRQs if we have _PRS. */
515 if (status != AE_NOT_FOUND)
516 for (i = 0; i < sc->pl_num_links; i++)
517 if (!link_valid_irq(&sc->pl_links[i],
518 sc->pl_links[i].l_irq))
519 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
521 acpi_pci_link_dump(sc, 0, "Validation");
523 /* Save initial IRQs. */
524 for (i = 0; i < sc->pl_num_links; i++)
525 sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq;
528 * Try to disable this link. If successful, set the current IRQ to
529 * zero and flags to indicate this link is not routed. If we can't
530 * run _DIS (i.e., the method doesn't exist), assume the initial
531 * IRQ was routed by the BIOS.
533 if (ACPI_SUCCESS(AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL,
535 for (i = 0; i < sc->pl_num_links; i++)
536 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
538 for (i = 0; i < sc->pl_num_links; i++)
539 if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq))
540 sc->pl_links[i].l_routed = TRUE;
542 acpi_pci_link_dump(sc, 0, "After Disable");
543 ACPI_SERIAL_END(pci_link);
546 ACPI_SERIAL_END(pci_link);
547 for (i = 0; i < sc->pl_num_links; i++)
548 if (sc->pl_links[i].l_irqs != NULL)
549 kfree(sc->pl_links[i].l_irqs, M_PCI_LINK);
550 kfree(sc->pl_links, M_PCI_LINK);
554 /* XXX: Note that this is identical to pci_pir_search_irq(). */
556 acpi_pci_link_search_irq(int bus, int device, int pin)
559 uint8_t func, maxfunc;
561 /* See if we have a valid device at function 0. */
562 value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1);
563 if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
564 return (PCI_INVALID_IRQ);
565 if (value & PCIM_MFDEV)
566 maxfunc = PCI_FUNCMAX;
570 /* Scan all possible functions at this device. */
571 for (func = 0; func <= maxfunc; func++) {
572 value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4);
573 if (value == 0xffffffff)
575 value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
578 * See if it uses the pin in question. Note that the passed
579 * in pin uses 0 for A, .. 3 for D whereas the intpin
580 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
582 if (value != pin + 1)
584 value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
587 "ACPI: Found matching pin for %d.%d.INT%c at func %d: %d\n",
588 bus, device, pin + 'A', func, value);
589 if (value != PCI_INVALID_IRQ)
592 return (PCI_INVALID_IRQ);
596 * Find the link structure that corresponds to the resource index passed in
597 * via 'source_index'.
600 acpi_pci_link_lookup(device_t dev, int source_index)
602 struct acpi_pci_link_softc *sc;
605 ACPI_SERIAL_ASSERT(pci_link);
606 sc = device_get_softc(dev);
607 for (i = 0; i < sc->pl_num_links; i++)
608 if (sc->pl_links[i].l_res_index == source_index)
609 return (&sc->pl_links[i]);
614 acpi_pci_link_add_reference(device_t dev, int index, device_t pcib, int slot,
622 * Look up the PCI bus for the specified PCI bridge device. Note
623 * that the PCI bridge device might not have any children yet.
624 * However, looking up its bus number doesn't require a valid child
625 * device, so we just pass NULL.
627 if (BUS_READ_IVAR(pcib, NULL, PCIB_IVAR_BUS, &bus) != 0) {
628 device_printf(pcib, "Unable to read PCI bus number");
629 panic("PCI bridge without a bus number");
632 /* Bump the reference count. */
633 ACPI_SERIAL_BEGIN(pci_link);
634 link = acpi_pci_link_lookup(dev, index);
636 device_printf(dev, "apparently invalid index %d\n", index);
637 ACPI_SERIAL_END(pci_link);
640 link->l_references++;
642 pci_link_interrupt_weights[link->l_irq]++;
645 * The BIOS only routes interrupts via ISA IRQs using the ATPICs
646 * (8259As). Thus, if this link is routed via an ISA IRQ, go
647 * look to see if the BIOS routed an IRQ for this link at the
648 * indicated (bus, slot, pin). If so, we prefer that IRQ for
649 * this link and add that IRQ to our list of known-good IRQs.
650 * This provides a good work-around for link devices whose _CRS
651 * method is either broken or bogus. We only use the value
652 * returned by _CRS if we can't find a valid IRQ via this method
655 * If this link is not routed via an ISA IRQ (because we are using
656 * APIC for example), then don't bother looking up the BIOS IRQ
657 * as if we find one it won't be valid anyway.
659 if (!link->l_isa_irq) {
660 ACPI_SERIAL_END(pci_link);
664 /* Try to find a BIOS IRQ setting from any matching devices. */
665 bios_irq = acpi_pci_link_search_irq(bus, slot, pin);
666 if (!PCI_INTERRUPT_VALID(bios_irq)) {
667 ACPI_SERIAL_END(pci_link);
671 /* Validate the BIOS IRQ. */
672 if (!link_valid_irq(link, bios_irq)) {
673 device_printf(dev, "BIOS IRQ %u for %d.%d.INT%c is invalid\n",
674 bios_irq, (int)bus, slot, pin + 'A');
675 } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) {
676 link->l_bios_irq = bios_irq;
677 if (bios_irq < NUM_ISA_INTERRUPTS)
678 pci_link_bios_isa_irqs |= (1 << bios_irq);
679 if (bios_irq != link->l_initial_irq &&
680 PCI_INTERRUPT_VALID(link->l_initial_irq))
682 "BIOS IRQ %u does not match initial IRQ %u\n",
683 bios_irq, link->l_initial_irq);
684 } else if (bios_irq != link->l_bios_irq)
686 "BIOS IRQ %u for %d.%d.INT%c does not match previous BIOS IRQ %u\n",
687 bios_irq, (int)bus, slot, pin + 'A',
689 ACPI_SERIAL_END(pci_link);
693 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
695 ACPI_RESOURCE *resource, *end, newres, *resptr;
701 /* Fetch the _CRS. */
702 ACPI_SERIAL_ASSERT(pci_link);
703 crsbuf.Pointer = NULL;
704 crsbuf.Length = ACPI_ALLOCATE_BUFFER;
705 status = AcpiGetCurrentResources(acpi_get_handle(sc->pl_dev), &crsbuf);
706 if (ACPI_SUCCESS(status) && crsbuf.Pointer == NULL)
707 status = AE_NO_MEMORY;
708 if (ACPI_FAILURE(status)) {
710 device_printf(sc->pl_dev,
711 "Unable to fetch current resources: %s\n",
712 AcpiFormatException(status));
716 /* Fill in IRQ resources via link structures. */
717 srsbuf->Pointer = NULL;
720 in_dpf = DPF_OUTSIDE;
721 resource = (ACPI_RESOURCE *)crsbuf.Pointer;
722 end = (ACPI_RESOURCE *)((char *)crsbuf.Pointer + crsbuf.Length);
724 switch (resource->Type) {
725 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
728 /* We've started the first DPF. */
732 /* We've started the second DPF. */
734 "%s: Multiple dependent functions within a current resource",
740 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
741 /* We are finished with DPF parsing. */
742 KASSERT(in_dpf != DPF_OUTSIDE,
743 ("%s: end dpf when not parsing a dpf", __func__));
744 in_dpf = DPF_OUTSIDE;
747 case ACPI_RESOURCE_TYPE_IRQ:
748 MPASS(i < sc->pl_num_links);
749 MPASS(link->l_prs_template.Type == ACPI_RESOURCE_TYPE_IRQ);
750 newres = link->l_prs_template;
752 resptr->Data.Irq.InterruptCount = 1;
753 if (PCI_INTERRUPT_VALID(link->l_irq)) {
754 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
755 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
756 __func__, link->l_irq));
757 resptr->Data.Irq.Interrupts[0] = link->l_irq;
759 resptr->Data.Irq.Interrupts[0] = 0;
763 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
764 MPASS(i < sc->pl_num_links);
765 MPASS(link->l_prs_template.Type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ);
766 newres = link->l_prs_template;
768 resptr->Data.ExtendedIrq.InterruptCount = 1;
769 if (PCI_INTERRUPT_VALID(link->l_irq))
770 resptr->Data.ExtendedIrq.Interrupts[0] =
773 resptr->Data.ExtendedIrq.Interrupts[0] = 0;
780 if (resptr != NULL) {
781 status = acpi_AppendBufferResource(srsbuf, resptr);
782 if (ACPI_FAILURE(status)) {
783 device_printf(sc->pl_dev,
784 "Unable to build resources: %s\n",
785 AcpiFormatException(status));
786 if (srsbuf->Pointer != NULL)
787 AcpiOsFree(srsbuf->Pointer);
788 AcpiOsFree(crsbuf.Pointer);
792 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
794 resource = ACPI_NEXT_RESOURCE(resource);
798 AcpiOsFree(crsbuf.Pointer);
803 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc,
806 ACPI_RESOURCE newres;
811 /* Start off with an empty buffer. */
812 srsbuf->Pointer = NULL;
814 for (i = 0; i < sc->pl_num_links; i++) {
816 /* Add a new IRQ resource from each link. */
817 link = &sc->pl_links[i];
818 newres = link->l_prs_template;
819 if (newres.Type == ACPI_RESOURCE_TYPE_IRQ) {
821 /* Build an IRQ resource. */
822 newres.Data.Irq.InterruptCount = 1;
823 if (PCI_INTERRUPT_VALID(link->l_irq)) {
824 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
825 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
826 __func__, link->l_irq));
827 newres.Data.Irq.Interrupts[0] = link->l_irq;
829 newres.Data.Irq.Interrupts[0] = 0;
832 /* Build an ExtIRQ resuorce. */
833 newres.Data.ExtendedIrq.InterruptCount = 1;
834 if (PCI_INTERRUPT_VALID(link->l_irq))
835 newres.Data.ExtendedIrq.Interrupts[0] =
838 newres.Data.ExtendedIrq.Interrupts[0] = 0;
841 /* Add the new resource to the end of the _SRS buffer. */
842 status = acpi_AppendBufferResource(srsbuf, &newres);
843 if (ACPI_FAILURE(status)) {
844 device_printf(sc->pl_dev,
845 "Unable to build resources: %s\n",
846 AcpiFormatException(status));
847 if (srsbuf->Pointer != NULL)
848 AcpiOsFree(srsbuf->Pointer);
856 acpi_pci_link_route_irqs(device_t dev)
858 struct acpi_pci_link_softc *sc;
859 ACPI_RESOURCE *resource, *end;
865 ACPI_SERIAL_ASSERT(pci_link);
866 sc = device_get_softc(dev);
868 status = acpi_pci_link_srs_from_links(sc, &srsbuf);
870 status = acpi_pci_link_srs_from_crs(sc, &srsbuf);
872 /* Write out new resources via _SRS. */
873 status = AcpiSetCurrentResources(acpi_get_handle(dev), &srsbuf);
874 if (ACPI_FAILURE(status)) {
875 device_printf(dev, "Unable to route IRQs: %s\n",
876 AcpiFormatException(status));
877 AcpiOsFree(srsbuf.Pointer);
882 * Perform acpi_config_intr() on each IRQ resource if it was just
883 * routed for the first time.
887 resource = (ACPI_RESOURCE *)srsbuf.Pointer;
888 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length);
890 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
892 switch (resource->Type) {
893 case ACPI_RESOURCE_TYPE_IRQ:
894 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
895 MPASS(i < sc->pl_num_links);
898 * Only configure the interrupt and update the
899 * weights if this link has a valid IRQ and was
900 * previously unrouted.
902 if (!link->l_routed &&
903 PCI_INTERRUPT_VALID(link->l_irq)) {
904 link->l_routed = TRUE;
905 acpi_config_intr(dev, resource);
906 pci_link_interrupt_weights[link->l_irq] +=
913 resource = ACPI_NEXT_RESOURCE(resource);
917 AcpiOsFree(srsbuf.Pointer);
922 acpi_pci_link_resume(device_t dev)
924 struct acpi_pci_link_softc *sc;
929 * If all of our links are routed, then restore the link via _SRS,
930 * otherwise, disable the link via _DIS.
932 ACPI_SERIAL_BEGIN(pci_link);
933 sc = device_get_softc(dev);
935 for (i = 0; i < sc->pl_num_links; i++)
936 if (sc->pl_links[i].l_routed)
938 if (routed == sc->pl_num_links)
939 status = acpi_pci_link_route_irqs(dev);
941 AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL, NULL);
944 ACPI_SERIAL_END(pci_link);
945 if (ACPI_FAILURE(status))
952 * Pick an IRQ to use for this unrouted link.
955 acpi_pci_link_choose_irq(device_t dev, struct link *link)
957 char tunable_buffer[64], link_name[5];
958 u_int8_t best_irq, pos_irq;
959 int best_weight, pos_weight, i;
961 KASSERT(!link->l_routed, ("%s: link already routed", __func__));
962 KASSERT(!PCI_INTERRUPT_VALID(link->l_irq),
963 ("%s: link already has an IRQ", __func__));
965 /* Check for a tunable override. */
966 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), link_name,
967 sizeof(link_name)))) {
968 ksnprintf(tunable_buffer, sizeof(tunable_buffer),
969 "hw.pci.link.%s.%d.irq", link_name, link->l_res_index);
970 if (kgetenv_int(tunable_buffer, &i) && PCI_INTERRUPT_VALID(i)) {
971 if (!link_valid_irq(link, i))
973 "Warning, IRQ %d is not listed as valid\n",
977 ksnprintf(tunable_buffer, sizeof(tunable_buffer),
978 "hw.pci.link.%s.irq", link_name);
979 if (kgetenv_int(tunable_buffer, &i) && PCI_INTERRUPT_VALID(i)) {
980 if (!link_valid_irq(link, i))
982 "Warning, IRQ %d is not listed as valid\n",
989 * If we have a valid BIOS IRQ, use that. We trust what the BIOS
990 * says it routed over what _CRS says the link thinks is routed.
992 if (PCI_INTERRUPT_VALID(link->l_bios_irq))
993 return (link->l_bios_irq);
996 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS,
999 if (PCI_INTERRUPT_VALID(link->l_initial_irq))
1000 return (link->l_initial_irq);
1003 * Ok, we have no useful hints, so we have to pick from the
1004 * possible IRQs. For ISA IRQs we only use interrupts that
1005 * have already been used by the BIOS.
1007 best_irq = PCI_INVALID_IRQ;
1008 best_weight = INT_MAX;
1009 for (i = 0; i < link->l_num_irqs; i++) {
1010 pos_irq = link->l_irqs[i];
1011 if (pos_irq < NUM_ISA_INTERRUPTS &&
1012 (pci_link_bios_isa_irqs & 1 << pos_irq) == 0)
1014 pos_weight = pci_link_interrupt_weights[pos_irq];
1015 if (pos_weight < best_weight) {
1016 best_weight = pos_weight;
1022 * If this is an ISA IRQ, try using the SCI if it is also an ISA
1023 * interrupt as a fallback.
1025 if (link->l_isa_irq) {
1026 pos_irq = AcpiGbl_FADT.SciInterrupt;
1027 pos_weight = pci_link_interrupt_weights[pos_irq];
1028 if (pos_weight < best_weight) {
1029 best_weight = pos_weight;
1034 if (PCI_INTERRUPT_VALID(best_irq)) {
1036 device_printf(dev, "Picked IRQ %u with weight %d\n",
1037 best_irq, best_weight);
1039 device_printf(dev, "Unable to choose an IRQ\n");
1044 acpi_pci_link_route_interrupt(device_t dev, int index)
1048 if (!acpi_enabled("pci_link"))
1049 return (PCI_INVALID_IRQ);
1051 ACPI_SERIAL_BEGIN(pci_link);
1052 link = acpi_pci_link_lookup(dev, index);
1054 panic("%s: apparently invalid index %d", __func__, index);
1057 * If this link device is already routed to an interrupt, just return
1058 * the interrupt it is routed to.
1060 if (link->l_routed) {
1061 KASSERT(PCI_INTERRUPT_VALID(link->l_irq),
1062 ("%s: link is routed but has an invalid IRQ", __func__));
1063 ACPI_SERIAL_END(pci_link);
1064 return (link->l_irq);
1067 /* Choose an IRQ if we need one. */
1068 if (!PCI_INTERRUPT_VALID(link->l_irq)) {
1069 link->l_irq = acpi_pci_link_choose_irq(dev, link);
1072 * Try to route the interrupt we picked. If it fails, then
1073 * assume the interrupt is not routed.
1075 if (PCI_INTERRUPT_VALID(link->l_irq)) {
1076 acpi_pci_link_route_irqs(dev);
1077 if (!link->l_routed)
1078 link->l_irq = PCI_INVALID_IRQ;
1081 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);