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/acpi.h>
39 #include <dev/acpica/acpivar.h>
40 #include <dev/acpica/acpi_pcibvar.h>
42 #include <machine/pci_cfgreg.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
47 /* Hooks for the ACPI CA debugging infrastructure. */
48 #define _COMPONENT ACPI_BUS
49 ACPI_MODULE_NAME("PCI_LINK")
51 ACPI_SERIAL_DECL(pci_link, "ACPI PCI link");
53 #define NUM_ISA_INTERRUPTS 16
54 #define NUM_ACPI_INTERRUPTS 256
57 * An ACPI PCI link device may contain multiple links. Each link has its
58 * own ACPI resource. _PRT entries specify which link is being used via
61 * XXX: A note about Source Indices and DPFs: Currently we assume that
62 * the DPF start and end tags are not counted towards the index that
63 * Source Index corresponds to. Also, we assume that when DPFs are in use
64 * they various sets overlap in terms of Indices. Here's an example
65 * resource list indicating these assumptions:
79 * The XXX is because I'm not sure if this is a valid assumption to make.
82 /* States during DPF processing. */
89 struct acpi_pci_link_softc {
92 struct link *pl_links;
97 struct acpi_pci_link_softc *l_sc;
100 uint8_t l_initial_irq;
107 ACPI_RESOURCE l_prs_template;
110 struct link_count_request {
115 struct link_res_request {
116 struct acpi_pci_link_softc *sc;
122 MALLOC_DEFINE(M_PCI_LINK, "pci_link", "ACPI PCI Link structures");
124 static int pci_link_interrupt_weights[NUM_ACPI_INTERRUPTS];
125 static int pci_link_bios_isa_irqs;
127 static char *pci_link_ids[] = { "PNP0C0F", NULL };
130 * Fetch the short name associated with an ACPI handle and save it in the
134 acpi_short_name(ACPI_HANDLE handle, char *buffer, size_t buflen)
139 buf.Pointer = buffer;
140 return (AcpiGetName(handle, ACPI_SINGLE_NAME, &buf));
144 acpi_pci_link_probe(device_t dev)
146 char descr[28], name[12];
149 * We explicitly do not check _STA since not all systems set it to
152 if (acpi_disabled("pci_link") ||
153 ACPI_ID_PROBE(device_get_parent(dev), dev, pci_link_ids) == NULL)
156 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), name,
158 snprintf(descr, sizeof(descr), "ACPI PCI Link %s", name);
159 device_set_desc_copy(dev, descr);
161 device_set_desc(dev, "ACPI PCI Link");
167 acpi_count_irq_resources(ACPI_RESOURCE *res, void *context)
169 struct link_count_request *req;
171 req = (struct link_count_request *)context;
173 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
174 switch (req->in_dpf) {
176 /* We've started the first DPF. */
177 req->in_dpf = DPF_FIRST;
180 /* We've started the second DPF. */
181 req->in_dpf = DPF_IGNORE;
185 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
186 /* We are finished with DPF parsing. */
187 KASSERT(req->in_dpf != DPF_OUTSIDE,
188 ("%s: end dpf when not parsing a dpf", __func__));
189 req->in_dpf = DPF_OUTSIDE;
191 case ACPI_RESOURCE_TYPE_IRQ:
192 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
194 * Don't count resources if we are in a DPF set that we are
197 if (req->in_dpf != DPF_IGNORE)
204 link_add_crs(ACPI_RESOURCE *res, void *context)
206 struct link_res_request *req;
209 ACPI_SERIAL_ASSERT(pci_link);
210 req = (struct link_res_request *)context;
212 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
213 switch (req->in_dpf) {
215 /* We've started the first DPF. */
216 req->in_dpf = DPF_FIRST;
219 /* We've started the second DPF. */
221 "%s: Multiple dependent functions within a current resource",
226 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
227 /* We are finished with DPF parsing. */
228 KASSERT(req->in_dpf != DPF_OUTSIDE,
229 ("%s: end dpf when not parsing a dpf", __func__));
230 req->in_dpf = DPF_OUTSIDE;
232 case ACPI_RESOURCE_TYPE_IRQ:
233 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
234 KASSERT(req->link_index < req->sc->pl_num_links,
235 ("%s: array boundary violation", __func__));
236 link = &req->sc->pl_links[req->link_index];
237 link->l_res_index = req->res_index;
242 * Only use the current value if there's one IRQ. Some
243 * systems return multiple IRQs (which is nonsense for _CRS)
244 * when the link hasn't been programmed.
246 if (res->Type == ACPI_RESOURCE_TYPE_IRQ) {
247 if (res->Data.Irq.InterruptCount == 1)
248 link->l_irq = res->Data.Irq.Interrupts[0];
249 } else if (res->Data.ExtendedIrq.InterruptCount == 1)
250 link->l_irq = res->Data.ExtendedIrq.Interrupts[0];
253 * An IRQ of zero means that the link isn't routed.
255 if (link->l_irq == 0)
256 link->l_irq = PCI_INVALID_IRQ;
265 * Populate the set of possible IRQs for each device.
268 link_add_prs(ACPI_RESOURCE *res, void *context)
270 struct link_res_request *req;
273 UINT32 *ext_irqs = NULL;
274 int i, is_ext_irq = 1;
276 ACPI_SERIAL_ASSERT(pci_link);
277 req = (struct link_res_request *)context;
279 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
280 switch (req->in_dpf) {
282 /* We've started the first DPF. */
283 req->in_dpf = DPF_FIRST;
286 /* We've started the second DPF. */
287 req->in_dpf = DPF_IGNORE;
291 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
292 /* We are finished with DPF parsing. */
293 KASSERT(req->in_dpf != DPF_OUTSIDE,
294 ("%s: end dpf when not parsing a dpf", __func__));
295 req->in_dpf = DPF_OUTSIDE;
297 case ACPI_RESOURCE_TYPE_IRQ:
300 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
302 * Don't parse resources if we are in a DPF set that we are
305 if (req->in_dpf == DPF_IGNORE)
308 KASSERT(req->link_index < req->sc->pl_num_links,
309 ("%s: array boundary violation", __func__));
310 link = &req->sc->pl_links[req->link_index];
311 if (link->l_res_index == -1) {
312 KASSERT(req->sc->pl_crs_bad,
313 ("res_index should be set"));
314 link->l_res_index = req->res_index;
320 * Stash a copy of the resource for later use when doing
323 bcopy(res, &link->l_prs_template, sizeof(ACPI_RESOURCE));
326 res->Data.ExtendedIrq.InterruptCount;
327 ext_irqs = res->Data.ExtendedIrq.Interrupts;
329 link->l_num_irqs = res->Data.Irq.InterruptCount;
330 irqs = res->Data.Irq.Interrupts;
332 if (link->l_num_irqs == 0)
336 * Save a list of the valid IRQs. Also, if all of the
337 * valid IRQs are ISA IRQs, then mark this link as
338 * routed via an ISA interrupt.
340 link->l_isa_irq = TRUE;
341 link->l_irqs = malloc(sizeof(int) * link->l_num_irqs,
342 M_PCI_LINK, M_WAITOK | M_ZERO);
343 for (i = 0; i < link->l_num_irqs; i++) {
345 link->l_irqs[i] = ext_irqs[i];
346 if (ext_irqs[i] >= NUM_ISA_INTERRUPTS)
347 link->l_isa_irq = FALSE;
349 link->l_irqs[i] = irqs[i];
350 if (irqs[i] >= NUM_ISA_INTERRUPTS)
351 link->l_isa_irq = FALSE;
356 if (req->in_dpf == DPF_IGNORE)
358 if (req->sc->pl_crs_bad)
359 device_printf(req->sc->pl_dev,
360 "Warning: possible resource %d will be lost during _SRS\n",
368 link_valid_irq(struct link *link, int irq)
372 ACPI_SERIAL_ASSERT(pci_link);
374 /* Invalid interrupts are never valid. */
375 if (!PCI_INTERRUPT_VALID(irq))
378 /* Any interrupt in the list of possible interrupts is valid. */
379 for (i = 0; i < link->l_num_irqs; i++)
380 if (link->l_irqs[i] == irq)
384 * For links routed via an ISA interrupt, if the SCI is routed via
385 * an ISA interrupt, the SCI is always treated as a valid IRQ.
387 if (link->l_isa_irq && AcpiGbl_FADT->SciInt == irq &&
388 irq < NUM_ISA_INTERRUPTS)
391 /* If the interrupt wasn't found in the list it is not valid. */
396 acpi_pci_link_dump(struct acpi_pci_link_softc *sc)
401 ACPI_SERIAL_ASSERT(pci_link);
402 printf("Index IRQ Rtd Ref IRQs\n");
403 for (i = 0; i < sc->pl_num_links; i++) {
404 link = &sc->pl_links[i];
405 printf("%5d %3d %c %3d ", i, link->l_irq,
406 link->l_routed ? 'Y' : 'N', link->l_references);
407 if (link->l_num_irqs == 0)
409 else for (j = 0; j < link->l_num_irqs; j++)
410 printf(" %d", link->l_irqs[j]);
416 acpi_pci_link_attach(device_t dev)
418 struct acpi_pci_link_softc *sc;
419 struct link_count_request creq;
420 struct link_res_request rreq;
424 sc = device_get_softc(dev);
426 ACPI_SERIAL_BEGIN(pci_link);
429 * Count the number of current resources so we know how big of
430 * a link array to allocate. On some systems, _CRS is broken,
431 * so for those systems try to derive the count from _PRS instead.
433 creq.in_dpf = DPF_OUTSIDE;
435 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
436 acpi_count_irq_resources, &creq);
437 sc->pl_crs_bad = ACPI_FAILURE(status);
438 if (sc->pl_crs_bad) {
439 creq.in_dpf = DPF_OUTSIDE;
441 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
442 acpi_count_irq_resources, &creq);
443 if (ACPI_FAILURE(status)) {
445 "Unable to parse _CRS or _PRS: %s\n",
446 AcpiFormatException(status));
447 ACPI_SERIAL_END(pci_link);
451 sc->pl_num_links = creq.count;
452 if (creq.count == 0) {
453 ACPI_SERIAL_END(pci_link);
456 sc->pl_links = malloc(sizeof(struct link) * sc->pl_num_links,
457 M_PCI_LINK, M_WAITOK | M_ZERO);
459 /* Initialize the child links. */
460 for (i = 0; i < sc->pl_num_links; i++) {
461 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
462 sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ;
463 sc->pl_links[i].l_sc = sc;
464 sc->pl_links[i].l_isa_irq = FALSE;
465 sc->pl_links[i].l_res_index = -1;
468 /* Try to read the current settings from _CRS if it is valid. */
469 if (!sc->pl_crs_bad) {
470 rreq.in_dpf = DPF_OUTSIDE;
474 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
475 link_add_crs, &rreq);
476 if (ACPI_FAILURE(status)) {
477 device_printf(dev, "Unable to parse _CRS: %s\n",
478 AcpiFormatException(status));
484 * Try to read the possible settings from _PRS. Note that if the
485 * _CRS is toast, we depend on having a working _PRS. However, if
486 * _CRS works, then it is ok for _PRS to be missing.
488 rreq.in_dpf = DPF_OUTSIDE;
492 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
493 link_add_prs, &rreq);
494 if (ACPI_FAILURE(status) &&
495 (status != AE_NOT_FOUND || sc->pl_crs_bad)) {
496 device_printf(dev, "Unable to parse _PRS: %s\n",
497 AcpiFormatException(status));
501 device_printf(dev, "Links after initial probe:\n");
502 acpi_pci_link_dump(sc);
505 /* Verify initial IRQs if we have _PRS. */
506 if (status != AE_NOT_FOUND)
507 for (i = 0; i < sc->pl_num_links; i++)
508 if (!link_valid_irq(&sc->pl_links[i],
509 sc->pl_links[i].l_irq))
510 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
512 device_printf(dev, "Links after initial validation:\n");
513 acpi_pci_link_dump(sc);
516 /* Save initial IRQs. */
517 for (i = 0; i < sc->pl_num_links; i++)
518 sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq;
521 * Try to disable this link. If successful, set the current IRQ to
522 * zero and flags to indicate this link is not routed. If we can't
523 * run _DIS (i.e., the method doesn't exist), assume the initial
524 * IRQ was routed by the BIOS.
526 if (ACPI_SUCCESS(AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL,
528 for (i = 0; i < sc->pl_num_links; i++)
529 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
531 for (i = 0; i < sc->pl_num_links; i++)
532 if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq))
533 sc->pl_links[i].l_routed = TRUE;
535 device_printf(dev, "Links after disable:\n");
536 acpi_pci_link_dump(sc);
538 ACPI_SERIAL_END(pci_link);
541 ACPI_SERIAL_END(pci_link);
542 for (i = 0; i < sc->pl_num_links; i++)
543 if (sc->pl_links[i].l_irqs != NULL)
544 free(sc->pl_links[i].l_irqs, M_PCI_LINK);
545 free(sc->pl_links, M_PCI_LINK);
549 /* XXX: Note that this is identical to pci_pir_search_irq(). */
551 acpi_pci_link_search_irq(int bus, int device, int pin)
554 uint8_t func, maxfunc;
556 /* See if we have a valid device at function 0. */
557 value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1);
558 if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
559 return (PCI_INVALID_IRQ);
560 if (value & PCIM_MFDEV)
561 maxfunc = PCI_FUNCMAX;
565 /* Scan all possible functions at this device. */
566 for (func = 0; func <= maxfunc; func++) {
567 value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4);
568 if (value == 0xffffffff)
570 value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
573 * See if it uses the pin in question. Note that the passed
574 * in pin uses 0 for A, .. 3 for D whereas the intpin
575 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
577 if (value != pin + 1)
579 value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
582 "ACPI: Found matching pin for %d.%d.INT%c at func %d: %d\n",
583 bus, device, pin + 'A', func, value);
584 if (value != PCI_INVALID_IRQ)
587 return (PCI_INVALID_IRQ);
591 * Find the link structure that corresponds to the resource index passed in
592 * via 'source_index'.
595 acpi_pci_link_lookup(device_t dev, int source_index)
597 struct acpi_pci_link_softc *sc;
600 ACPI_SERIAL_ASSERT(pci_link);
601 sc = device_get_softc(dev);
602 for (i = 0; i < sc->pl_num_links; i++)
603 if (sc->pl_links[i].l_res_index == source_index)
604 return (&sc->pl_links[i]);
609 acpi_pci_link_add_reference(device_t dev, int index, device_t pcib, int slot,
617 * Look up the PCI bus for the specified PCI bridge device. Note
618 * that the PCI bridge device might not have any children yet.
619 * However, looking up its bus number doesn't require a valid child
620 * device, so we just pass NULL.
622 if (BUS_READ_IVAR(pcib, NULL, PCIB_IVAR_BUS, &bus) != 0) {
623 device_printf(pcib, "Unable to read PCI bus number");
624 panic("PCI bridge without a bus number");
627 /* Bump the reference count. */
628 ACPI_SERIAL_BEGIN(pci_link);
629 link = acpi_pci_link_lookup(dev, index);
631 device_printf(dev, "apparently invalid index %d\n", index);
632 ACPI_SERIAL_END(pci_link);
635 link->l_references++;
637 pci_link_interrupt_weights[link->l_irq]++;
640 * The BIOS only routes interrupts via ISA IRQs using the ATPICs
641 * (8259As). Thus, if this link is routed via an ISA IRQ, go
642 * look to see if the BIOS routed an IRQ for this link at the
643 * indicated (bus, slot, pin). If so, we prefer that IRQ for
644 * this link and add that IRQ to our list of known-good IRQs.
645 * This provides a good work-around for link devices whose _CRS
646 * method is either broken or bogus. We only use the value
647 * returned by _CRS if we can't find a valid IRQ via this method
650 * If this link is not routed via an ISA IRQ (because we are using
651 * APIC for example), then don't bother looking up the BIOS IRQ
652 * as if we find one it won't be valid anyway.
654 if (!link->l_isa_irq) {
655 ACPI_SERIAL_END(pci_link);
659 /* Try to find a BIOS IRQ setting from any matching devices. */
660 bios_irq = acpi_pci_link_search_irq(bus, slot, pin);
661 if (!PCI_INTERRUPT_VALID(bios_irq)) {
662 ACPI_SERIAL_END(pci_link);
666 /* Validate the BIOS IRQ. */
667 if (!link_valid_irq(link, bios_irq)) {
668 device_printf(dev, "BIOS IRQ %u for %d.%d.INT%c is invalid\n",
669 bios_irq, (int)bus, slot, pin + 'A');
670 } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) {
671 link->l_bios_irq = bios_irq;
672 if (bios_irq < NUM_ISA_INTERRUPTS)
673 pci_link_bios_isa_irqs |= (1 << bios_irq);
674 if (bios_irq != link->l_initial_irq &&
675 PCI_INTERRUPT_VALID(link->l_initial_irq))
677 "BIOS IRQ %u does not match initial IRQ %u\n",
678 bios_irq, link->l_initial_irq);
679 } else if (bios_irq != link->l_bios_irq)
681 "BIOS IRQ %u for %d.%d.INT%c does not match previous BIOS IRQ %u\n",
682 bios_irq, (int)bus, slot, pin + 'A',
684 ACPI_SERIAL_END(pci_link);
688 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
690 ACPI_RESOURCE *resource, *end, newres, *resptr;
696 /* Fetch the _CRS. */
697 ACPI_SERIAL_ASSERT(pci_link);
698 crsbuf.Pointer = NULL;
699 crsbuf.Length = ACPI_ALLOCATE_BUFFER;
700 status = AcpiGetCurrentResources(acpi_get_handle(sc->pl_dev), &crsbuf);
701 if (ACPI_SUCCESS(status) && crsbuf.Pointer == NULL)
702 status = AE_NO_MEMORY;
703 if (ACPI_FAILURE(status)) {
705 device_printf(sc->pl_dev,
706 "Unable to fetch current resources: %s\n",
707 AcpiFormatException(status));
711 /* Fill in IRQ resources via link structures. */
712 srsbuf->Pointer = NULL;
715 in_dpf = DPF_OUTSIDE;
716 resource = (ACPI_RESOURCE *)crsbuf.Pointer;
717 end = (ACPI_RESOURCE *)((char *)crsbuf.Pointer + crsbuf.Length);
719 switch (resource->Type) {
720 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
723 /* We've started the first DPF. */
727 /* We've started the second DPF. */
729 "%s: Multiple dependent functions within a current resource",
735 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
736 /* We are finished with DPF parsing. */
737 KASSERT(in_dpf != DPF_OUTSIDE,
738 ("%s: end dpf when not parsing a dpf", __func__));
739 in_dpf = DPF_OUTSIDE;
742 case ACPI_RESOURCE_TYPE_IRQ:
743 MPASS(i < sc->pl_num_links);
744 MPASS(link->l_prs_template.Type == ACPI_RESOURCE_TYPE_IRQ);
745 newres = link->l_prs_template;
747 resptr->Data.Irq.InterruptCount = 1;
748 if (PCI_INTERRUPT_VALID(link->l_irq)) {
749 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
750 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
751 __func__, link->l_irq));
752 resptr->Data.Irq.Interrupts[0] = link->l_irq;
754 resptr->Data.Irq.Interrupts[0] = 0;
758 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
759 MPASS(i < sc->pl_num_links);
760 MPASS(link->l_prs_template.Type == ACPI_RESOURCE_TYPE_EXTENDED_IRQ);
761 newres = link->l_prs_template;
763 resptr->Data.ExtendedIrq.InterruptCount = 1;
764 if (PCI_INTERRUPT_VALID(link->l_irq))
765 resptr->Data.ExtendedIrq.Interrupts[0] =
768 resptr->Data.ExtendedIrq.Interrupts[0] = 0;
775 if (resptr != NULL) {
776 status = acpi_AppendBufferResource(srsbuf, resptr);
777 if (ACPI_FAILURE(status)) {
778 device_printf(sc->pl_dev,
779 "Unable to build resources: %s\n",
780 AcpiFormatException(status));
781 if (srsbuf->Pointer != NULL)
782 AcpiOsFree(srsbuf->Pointer);
783 AcpiOsFree(crsbuf.Pointer);
787 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
789 resource = ACPI_NEXT_RESOURCE(resource);
793 AcpiOsFree(crsbuf.Pointer);
798 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc,
801 ACPI_RESOURCE newres;
806 /* Start off with an empty buffer. */
807 srsbuf->Pointer = NULL;
809 for (i = 0; i < sc->pl_num_links; i++) {
811 /* Add a new IRQ resource from each link. */
812 link = &sc->pl_links[i];
813 newres = link->l_prs_template;
814 if (newres.Type == ACPI_RESOURCE_TYPE_IRQ) {
816 /* Build an IRQ resource. */
817 newres.Data.Irq.InterruptCount = 1;
818 if (PCI_INTERRUPT_VALID(link->l_irq)) {
819 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
820 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
821 __func__, link->l_irq));
822 newres.Data.Irq.Interrupts[0] = link->l_irq;
824 newres.Data.Irq.Interrupts[0] = 0;
827 /* Build an ExtIRQ resuorce. */
828 newres.Data.ExtendedIrq.InterruptCount = 1;
829 if (PCI_INTERRUPT_VALID(link->l_irq))
830 newres.Data.ExtendedIrq.Interrupts[0] =
833 newres.Data.ExtendedIrq.Interrupts[0] = 0;
836 /* Add the new resource to the end of the _SRS buffer. */
837 status = acpi_AppendBufferResource(srsbuf, &newres);
838 if (ACPI_FAILURE(status)) {
839 device_printf(sc->pl_dev,
840 "Unable to build resources: %s\n",
841 AcpiFormatException(status));
842 if (srsbuf->Pointer != NULL)
843 AcpiOsFree(srsbuf->Pointer);
851 acpi_pci_link_route_irqs(device_t dev)
853 struct acpi_pci_link_softc *sc;
854 ACPI_RESOURCE *resource, *end;
860 ACPI_SERIAL_ASSERT(pci_link);
861 sc = device_get_softc(dev);
863 status = acpi_pci_link_srs_from_links(sc, &srsbuf);
865 status = acpi_pci_link_srs_from_crs(sc, &srsbuf);
867 /* Write out new resources via _SRS. */
868 status = AcpiSetCurrentResources(acpi_get_handle(dev), &srsbuf);
869 if (ACPI_FAILURE(status)) {
870 device_printf(dev, "Unable to route IRQs: %s\n",
871 AcpiFormatException(status));
872 AcpiOsFree(srsbuf.Pointer);
877 * Perform acpi_config_intr() on each IRQ resource if it was just
878 * routed for the first time.
882 resource = (ACPI_RESOURCE *)srsbuf.Pointer;
883 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length);
885 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
887 switch (resource->Type) {
888 case ACPI_RESOURCE_TYPE_IRQ:
889 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
890 MPASS(i < sc->pl_num_links);
893 * Only configure the interrupt and update the
894 * weights if this link has a valid IRQ and was
895 * previously unrouted.
897 if (!link->l_routed &&
898 PCI_INTERRUPT_VALID(link->l_irq)) {
899 link->l_routed = TRUE;
900 acpi_config_intr(dev, resource);
901 pci_link_interrupt_weights[link->l_irq] +=
908 resource = ACPI_NEXT_RESOURCE(resource);
912 AcpiOsFree(srsbuf.Pointer);
917 acpi_pci_link_resume(device_t dev)
921 ACPI_SERIAL_BEGIN(pci_link);
922 status = acpi_pci_link_route_irqs(dev);
923 ACPI_SERIAL_END(pci_link);
924 if (ACPI_FAILURE(status))
931 * Pick an IRQ to use for this unrouted link.
934 acpi_pci_link_choose_irq(device_t dev, struct link *link)
936 char tunable_buffer[64], link_name[5];
937 u_int8_t best_irq, pos_irq;
938 int best_weight, pos_weight, i;
940 KASSERT(!link->l_routed, ("%s: link already routed", __func__));
941 KASSERT(!PCI_INTERRUPT_VALID(link->l_irq),
942 ("%s: link already has an IRQ", __func__));
944 /* Check for a tunable override and use it if it is valid. */
945 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), link_name,
946 sizeof(link_name)))) {
947 snprintf(tunable_buffer, sizeof(tunable_buffer),
948 "hw.pci.link.%s.%d.irq", link_name, link->l_res_index);
949 if (getenv_int(tunable_buffer, &i) &&
950 PCI_INTERRUPT_VALID(i) && link_valid_irq(link, i))
952 snprintf(tunable_buffer, sizeof(tunable_buffer),
953 "hw.pci.link.%s.irq", link_name);
954 if (getenv_int(tunable_buffer, &i) &&
955 PCI_INTERRUPT_VALID(i) && link_valid_irq(link, i))
960 * If we have a valid BIOS IRQ, use that. We trust what the BIOS
961 * says it routed over what _CRS says the link thinks is routed.
963 if (PCI_INTERRUPT_VALID(link->l_bios_irq))
964 return (link->l_bios_irq);
967 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS,
970 if (PCI_INTERRUPT_VALID(link->l_initial_irq))
971 return (link->l_initial_irq);
974 * Ok, we have no useful hints, so we have to pick from the
975 * possible IRQs. For ISA IRQs we only use interrupts that
976 * have already been used by the BIOS.
978 best_irq = PCI_INVALID_IRQ;
979 best_weight = INT_MAX;
980 for (i = 0; i < link->l_num_irqs; i++) {
981 pos_irq = link->l_irqs[i];
982 if (pos_irq < NUM_ISA_INTERRUPTS &&
983 (pci_link_bios_isa_irqs & 1 << pos_irq) == 0)
985 pos_weight = pci_link_interrupt_weights[pos_irq];
986 if (pos_weight < best_weight) {
987 best_weight = pos_weight;
993 * If this is an ISA IRQ, try using the SCI if it is also an ISA
994 * interrupt as a fallback.
996 if (link->l_isa_irq) {
997 pos_irq = AcpiGbl_FADT->SciInt;
998 pos_weight = pci_link_interrupt_weights[pos_irq];
999 if (pos_weight < best_weight) {
1000 best_weight = pos_weight;
1005 if (PCI_INTERRUPT_VALID(best_irq)) {
1007 device_printf(dev, "Picked IRQ %u with weight %d\n",
1008 best_irq, best_weight);
1010 device_printf(dev, "Unable to choose an IRQ\n");
1015 acpi_pci_link_route_interrupt(device_t dev, int index)
1019 if (acpi_disabled("pci_link"))
1020 return (PCI_INVALID_IRQ);
1022 ACPI_SERIAL_BEGIN(pci_link);
1023 link = acpi_pci_link_lookup(dev, index);
1025 panic("%s: apparently invalid index %d", __func__, index);
1028 * If this link device is already routed to an interrupt, just return
1029 * the interrupt it is routed to.
1031 if (link->l_routed) {
1032 KASSERT(PCI_INTERRUPT_VALID(link->l_irq),
1033 ("%s: link is routed but has an invalid IRQ", __func__));
1034 ACPI_SERIAL_END(pci_link);
1035 return (link->l_irq);
1038 /* Choose an IRQ if we need one. */
1039 if (!PCI_INTERRUPT_VALID(link->l_irq)) {
1040 link->l_irq = acpi_pci_link_choose_irq(dev, link);
1043 * Try to route the interrupt we picked. If it fails, then
1044 * assume the interrupt is not routed.
1046 if (PCI_INTERRUPT_VALID(link->l_irq)) {
1047 acpi_pci_link_route_irqs(dev);
1048 if (!link->l_routed)
1049 link->l_irq = PCI_INVALID_IRQ;
1052 ACPI_SERIAL_END(pci_link);
1054 return (link->l_irq);
1058 * This is gross, but we abuse the identify routine to perform one-time
1059 * SYSINIT() style initialization for the driver.
1062 acpi_pci_link_identify(driver_t *driver, device_t parent)
1066 * If the SCI is an ISA IRQ, add it to the bitmask of known good
1069 * XXX: If we are using the APIC, the SCI might have been
1070 * rerouted to an APIC pin in which case this is invalid. However,
1071 * if we are using the APIC, we also shouldn't be having any PCI
1072 * interrupts routed via ISA IRQs, so this is probably ok.
1074 if (AcpiGbl_FADT->SciInt < NUM_ISA_INTERRUPTS)
1075 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT->SciInt);
1078 static device_method_t acpi_pci_link_methods[] = {
1079 /* Device interface */
1080 DEVMETHOD(device_identify, acpi_pci_link_identify),
1081 DEVMETHOD(device_probe, acpi_pci_link_probe),
1082 DEVMETHOD(device_attach, acpi_pci_link_attach),
1083 DEVMETHOD(device_resume, acpi_pci_link_resume),
1088 static driver_t acpi_pci_link_driver = {
1090 acpi_pci_link_methods,
1091 sizeof(struct acpi_pci_link_softc),
1094 static devclass_t pci_link_devclass;
1096 DRIVER_MODULE(acpi_pci_link, acpi, acpi_pci_link_driver, pci_link_devclass, 0,
1098 MODULE_DEPEND(acpi_pci_link, acpi, 1, 1, 1);