if_iwm - Partly sync if_iwm_binding.c to Linux iwlwifi code.
[dragonfly.git] / sys / dev / acpica / acpi_pci_link.c
blob80ddceb0aee02ce59c235a485cadf180686d9f3e
1 /*
2 * Copyright (c) 2002 Mitsuru IWASAKI <iwasaki@jp.kfreebsd.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
26 * $FreeBSD: src/sys/dev/acpica/acpi_pci_link.c,v 1.65 2013/04/23 00:40:24 svnexp Exp $
29 #include "opt_acpi.h"
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/limits.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
37 #include "acpi.h"
38 #include <dev/acpica/acpivar.h>
39 #include <dev/acpica/acpi_pcibvar.h>
40 #include <dev/acpica/acpi_sci_var.h>
42 #include <bus/pci/pci_cfgreg.h>
43 #include <bus/pci/pcireg.h>
44 #include <bus/pci/pcivar.h>
45 #include "pcib_if.h"
47 /* Hooks for the ACPICA 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
59 * the Source Index.
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:
67 * Resource Index
68 * -------- -----
69 * I/O Port 0
70 * Start DPF -
71 * IRQ 1
72 * MemIO 2
73 * Start DPF -
74 * IRQ 1
75 * MemIO 2
76 * End DPF -
77 * DMA Channel 3
79 * The XXX is because I'm not sure if this is a valid assumption to make.
82 /* States during DPF processing. */
83 #define DPF_OUTSIDE 0
84 #define DPF_FIRST 1
85 #define DPF_IGNORE 2
87 struct link;
89 struct acpi_pci_link_softc {
90 int pl_num_links;
91 int pl_crs_bad;
92 struct link *pl_links;
93 device_t pl_dev;
96 struct link {
97 struct acpi_pci_link_softc *l_sc;
98 uint8_t l_bios_irq;
99 uint8_t l_irq;
100 uint8_t l_initial_irq;
101 UINT32 l_crs_type;
102 int l_res_index;
103 int l_num_irqs;
104 int *l_irqs;
105 int l_references;
106 int l_routed:1;
107 int l_isa_irq:1;
108 ACPI_RESOURCE l_prs_template;
111 struct link_count_request {
112 int in_dpf;
113 int count;
116 struct link_res_request {
117 struct acpi_pci_link_softc *sc;
118 int in_dpf;
119 int res_index;
120 int link_index;
123 static 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
132 * passed in buffer.
134 static ACPI_STATUS
135 acpi_short_name(ACPI_HANDLE handle, char *buffer, size_t buflen)
137 ACPI_BUFFER buf;
139 buf.Length = buflen;
140 buf.Pointer = buffer;
141 return (AcpiGetName(handle, ACPI_SINGLE_NAME, &buf));
144 static int
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
151 * sensible values.
153 if (acpi_disabled("pci_link") ||
154 ACPI_ID_PROBE(device_get_parent(dev), dev, pci_link_ids) == NULL)
155 return (ENXIO);
157 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), name,
158 sizeof(name)))) {
159 ksnprintf(descr, sizeof(descr), "ACPI PCI Link %s", name);
160 device_set_desc_copy(dev, descr);
161 } else
162 device_set_desc(dev, "ACPI PCI Link");
163 device_quiet(dev);
164 return (0);
167 static ACPI_STATUS
168 acpi_count_irq_resources(ACPI_RESOURCE *res, void *context)
170 struct link_count_request *req;
172 req = (struct link_count_request *)context;
173 switch (res->Type) {
174 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
175 switch (req->in_dpf) {
176 case DPF_OUTSIDE:
177 /* We've started the first DPF. */
178 req->in_dpf = DPF_FIRST;
179 break;
180 case DPF_FIRST:
181 /* We've started the second DPF. */
182 req->in_dpf = DPF_IGNORE;
183 break;
185 break;
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;
191 break;
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
196 * ignoring.
198 if (req->in_dpf != DPF_IGNORE)
199 req->count++;
201 return (AE_OK);
204 static ACPI_STATUS
205 link_add_crs(ACPI_RESOURCE *res, void *context)
207 struct link_res_request *req;
208 struct link *link;
210 ACPI_SERIAL_ASSERT(pci_link);
211 req = (struct link_res_request *)context;
212 switch (res->Type) {
213 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
214 switch (req->in_dpf) {
215 case DPF_OUTSIDE:
216 /* We've started the first DPF. */
217 req->in_dpf = DPF_FIRST;
218 break;
219 case DPF_FIRST:
220 /* We've started the second DPF. */
221 panic(
222 "%s: Multiple dependent functions within a current resource",
223 __func__);
224 break;
226 break;
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;
232 break;
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;
239 link->l_crs_type = res->Type;
240 req->link_index++;
241 req->res_index++;
244 * Only use the current value if there's one IRQ. Some
245 * systems return multiple IRQs (which is nonsense for _CRS)
246 * when the link hasn't been programmed.
248 if (res->Type == ACPI_RESOURCE_TYPE_IRQ) {
249 if (res->Data.Irq.InterruptCount == 1)
250 link->l_irq = res->Data.Irq.Interrupts[0];
251 } else if (res->Data.ExtendedIrq.InterruptCount == 1)
252 link->l_irq = res->Data.ExtendedIrq.Interrupts[0];
255 * An IRQ of zero means that the link isn't routed.
257 if (link->l_irq == 0)
258 link->l_irq = PCI_INVALID_IRQ;
259 break;
260 default:
261 req->res_index++;
263 return (AE_OK);
267 * Populate the set of possible IRQs for each device.
269 static ACPI_STATUS
270 link_add_prs(ACPI_RESOURCE *res, void *context)
272 ACPI_RESOURCE *tmp;
273 struct link_res_request *req;
274 struct link *link;
275 UINT8 *irqs = NULL;
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;
281 switch (res->Type) {
282 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
283 switch (req->in_dpf) {
284 case DPF_OUTSIDE:
285 /* We've started the first DPF. */
286 req->in_dpf = DPF_FIRST;
287 break;
288 case DPF_FIRST:
289 /* We've started the second DPF. */
290 req->in_dpf = DPF_IGNORE;
291 break;
293 break;
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;
299 break;
300 case ACPI_RESOURCE_TYPE_IRQ:
301 is_ext_irq = 0;
302 /* fall through */
303 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
305 * Don't parse resources if we are in a DPF set that we are
306 * ignoring.
308 if (req->in_dpf == DPF_IGNORE)
309 break;
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;
319 req->link_index++;
320 req->res_index++;
323 * Stash a copy of the resource for later use when doing
324 * _SRS.
326 tmp = &link->l_prs_template;
327 if (is_ext_irq) {
328 bcopy(res, tmp, ACPI_RS_SIZE(tmp->Data.ExtendedIrq));
331 * XXX acpi_AppendBufferResource() cannot handle
332 * optional data.
334 bzero(&tmp->Data.ExtendedIrq.ResourceSource,
335 sizeof(tmp->Data.ExtendedIrq.ResourceSource));
336 tmp->Length = ACPI_RS_SIZE(tmp->Data.ExtendedIrq);
338 link->l_num_irqs =
339 res->Data.ExtendedIrq.InterruptCount;
340 ext_irqs = res->Data.ExtendedIrq.Interrupts;
341 } else {
342 bcopy(res, tmp, ACPI_RS_SIZE(tmp->Data.Irq));
343 link->l_num_irqs = res->Data.Irq.InterruptCount;
344 irqs = res->Data.Irq.Interrupts;
346 if (link->l_num_irqs == 0)
347 break;
350 * Save a list of the valid IRQs. Also, if all of the
351 * valid IRQs are ISA IRQs, then mark this link as
352 * routed via an ISA interrupt.
354 link->l_isa_irq = TRUE;
356 link->l_irqs = kmalloc(sizeof(int) * link->l_num_irqs,
357 M_PCI_LINK, M_WAITOK | M_ZERO);
358 for (i = 0; i < link->l_num_irqs; i++) {
359 if (is_ext_irq) {
360 link->l_irqs[i] = ext_irqs[i];
361 if (ext_irqs[i] >= NUM_ISA_INTERRUPTS)
362 link->l_isa_irq = FALSE;
363 } else {
364 link->l_irqs[i] = irqs[i];
365 if (irqs[i] >= NUM_ISA_INTERRUPTS)
366 link->l_isa_irq = FALSE;
371 * If this is not an ISA IRQ but _CRS used a non-extended
372 * IRQ descriptor, don't use _CRS as a template for _SRS.
374 if (!req->sc->pl_crs_bad && !link->l_isa_irq &&
375 link->l_crs_type == ACPI_RESOURCE_TYPE_IRQ)
376 req->sc->pl_crs_bad = TRUE;
377 break;
378 default:
379 if (req->in_dpf == DPF_IGNORE)
380 break;
381 if (req->sc->pl_crs_bad)
382 device_printf(req->sc->pl_dev,
383 "Warning: possible resource %d will be lost during _SRS\n",
384 req->res_index);
385 req->res_index++;
387 return (AE_OK);
390 static int
391 link_valid_irq(struct link *link, int irq)
393 int i;
395 ACPI_SERIAL_ASSERT(pci_link);
397 /* Invalid interrupts are never valid. */
398 if (!PCI_INTERRUPT_VALID(irq))
399 return (FALSE);
401 /* Any interrupt in the list of possible interrupts is valid. */
402 for (i = 0; i < link->l_num_irqs; i++)
403 if (link->l_irqs[i] == irq)
404 return (TRUE);
407 * For links routed via an ISA interrupt, if the SCI is routed via
408 * an ISA interrupt, the SCI is always treated as a valid IRQ.
410 if (link->l_isa_irq && AcpiGbl_FADT.SciInterrupt == irq &&
411 irq < NUM_ISA_INTERRUPTS)
412 return (TRUE);
414 /* If the interrupt wasn't found in the list it is not valid. */
415 return (FALSE);
418 static void
419 acpi_pci_link_dump(struct acpi_pci_link_softc *sc, int header, const char *tag)
421 struct link *link;
422 char buf[16];
423 int i, j;
425 ACPI_SERIAL_ASSERT(pci_link);
426 if (header) {
427 ksnprintf(buf, sizeof(buf), "%s:",
428 device_get_nameunit(sc->pl_dev));
429 kprintf("%-16.16s Index IRQ Rtd Ref IRQs\n", buf);
431 for (i = 0; i < sc->pl_num_links; i++) {
432 link = &sc->pl_links[i];
433 kprintf(" %-14.14s %5d %3d %c %3d ", i == 0 ? tag : "", i,
434 link->l_irq, link->l_routed ? 'Y' : 'N',
435 link->l_references);
436 if (link->l_num_irqs == 0)
437 kprintf(" none");
438 else for (j = 0; j < link->l_num_irqs; j++)
439 kprintf(" %d", link->l_irqs[j]);
440 kprintf("\n");
444 static int
445 acpi_pci_link_attach(device_t dev)
447 struct acpi_pci_link_softc *sc;
448 struct link_count_request creq;
449 struct link_res_request rreq;
450 ACPI_STATUS status;
451 int i;
453 sc = device_get_softc(dev);
454 sc->pl_dev = dev;
455 ACPI_SERIAL_INIT(pci_link);
456 ACPI_SERIAL_BEGIN(pci_link);
459 * Count the number of current resources so we know how big of
460 * a link array to allocate. On some systems, _CRS is broken,
461 * so for those systems try to derive the count from _PRS instead.
463 creq.in_dpf = DPF_OUTSIDE;
464 creq.count = 0;
465 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
466 acpi_count_irq_resources, &creq);
467 sc->pl_crs_bad = ACPI_FAILURE(status);
468 if (sc->pl_crs_bad) {
469 creq.in_dpf = DPF_OUTSIDE;
470 creq.count = 0;
471 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
472 acpi_count_irq_resources, &creq);
473 if (ACPI_FAILURE(status)) {
474 device_printf(dev,
475 "Unable to parse _CRS or _PRS: %s\n",
476 AcpiFormatException(status));
477 ACPI_SERIAL_END(pci_link);
478 return (ENXIO);
481 sc->pl_num_links = creq.count;
482 if (creq.count == 0) {
483 ACPI_SERIAL_END(pci_link);
484 return (0);
486 sc->pl_links = kmalloc(sizeof(struct link) * sc->pl_num_links,
487 M_PCI_LINK, M_WAITOK | M_ZERO);
489 /* Initialize the child links. */
490 for (i = 0; i < sc->pl_num_links; i++) {
491 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
492 sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ;
493 sc->pl_links[i].l_sc = sc;
494 sc->pl_links[i].l_isa_irq = FALSE;
495 sc->pl_links[i].l_res_index = -1;
498 /* Try to read the current settings from _CRS if it is valid. */
499 if (!sc->pl_crs_bad) {
500 rreq.in_dpf = DPF_OUTSIDE;
501 rreq.link_index = 0;
502 rreq.res_index = 0;
503 rreq.sc = sc;
504 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
505 link_add_crs, &rreq);
506 if (ACPI_FAILURE(status)) {
507 device_printf(dev, "Unable to parse _CRS: %s\n",
508 AcpiFormatException(status));
509 goto fail;
514 * Try to read the possible settings from _PRS. Note that if the
515 * _CRS is toast, we depend on having a working _PRS. However, if
516 * _CRS works, then it is ok for _PRS to be missing.
518 rreq.in_dpf = DPF_OUTSIDE;
519 rreq.link_index = 0;
520 rreq.res_index = 0;
521 rreq.sc = sc;
522 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
523 link_add_prs, &rreq);
524 if (ACPI_FAILURE(status) &&
525 (status != AE_NOT_FOUND || sc->pl_crs_bad)) {
526 device_printf(dev, "Unable to parse _PRS: %s\n",
527 AcpiFormatException(status));
528 goto fail;
530 if (bootverbose)
531 acpi_pci_link_dump(sc, 1, "Initial Probe");
533 /* Verify initial IRQs if we have _PRS. */
534 if (status != AE_NOT_FOUND)
535 for (i = 0; i < sc->pl_num_links; i++)
536 if (!link_valid_irq(&sc->pl_links[i],
537 sc->pl_links[i].l_irq))
538 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
539 if (bootverbose)
540 acpi_pci_link_dump(sc, 0, "Validation");
542 /* Save initial IRQs. */
543 for (i = 0; i < sc->pl_num_links; i++)
544 sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq;
547 * Try to disable this link. If successful, set the current IRQ to
548 * zero and flags to indicate this link is not routed. If we can't
549 * run _DIS (i.e., the method doesn't exist), assume the initial
550 * IRQ was routed by the BIOS.
552 if (ACPI_SUCCESS(AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL,
553 NULL)))
554 for (i = 0; i < sc->pl_num_links; i++)
555 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
556 else
557 for (i = 0; i < sc->pl_num_links; i++)
558 if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq))
559 sc->pl_links[i].l_routed = TRUE;
560 if (bootverbose)
561 acpi_pci_link_dump(sc, 0, "After Disable");
562 ACPI_SERIAL_END(pci_link);
563 return (0);
564 fail:
565 ACPI_SERIAL_END(pci_link);
566 for (i = 0; i < sc->pl_num_links; i++)
567 if (sc->pl_links[i].l_irqs != NULL)
568 kfree(sc->pl_links[i].l_irqs, M_PCI_LINK);
569 kfree(sc->pl_links, M_PCI_LINK);
570 return (ENXIO);
573 /* XXX: Note that this is identical to pci_pir_search_irq(). */
574 static uint8_t
575 acpi_pci_link_search_irq(int bus, int device, int pin)
577 uint32_t value;
578 uint8_t func, maxfunc;
580 /* See if we have a valid device at function 0. */
581 value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1);
582 if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
583 return (PCI_INVALID_IRQ);
584 if (value & PCIM_MFDEV)
585 maxfunc = PCI_FUNCMAX;
586 else
587 maxfunc = 0;
589 /* Scan all possible functions at this device. */
590 for (func = 0; func <= maxfunc; func++) {
591 value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4);
592 if (value == 0xffffffff)
593 continue;
594 value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
597 * See if it uses the pin in question. Note that the passed
598 * in pin uses 0 for A, .. 3 for D whereas the intpin
599 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
601 if (value != pin + 1)
602 continue;
603 value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
604 if (bootverbose)
605 kprintf(
606 "ACPI: Found matching pin for %d.%d.INT%c at func %d: %d\n",
607 bus, device, pin + 'A', func, value);
608 if (value != PCI_INVALID_IRQ)
609 return (value);
611 return (PCI_INVALID_IRQ);
615 * Find the link structure that corresponds to the resource index passed in
616 * via 'source_index'.
618 static struct link *
619 acpi_pci_link_lookup(device_t dev, int source_index)
621 struct acpi_pci_link_softc *sc;
622 int i;
624 ACPI_SERIAL_ASSERT(pci_link);
625 sc = device_get_softc(dev);
626 for (i = 0; i < sc->pl_num_links; i++)
627 if (sc->pl_links[i].l_res_index == source_index)
628 return (&sc->pl_links[i]);
629 return (NULL);
632 void
633 acpi_pci_link_add_reference(device_t dev, int index, device_t pcib, int slot,
634 int pin)
636 struct link *link;
637 uint8_t bios_irq;
638 uintptr_t bus;
641 * Look up the PCI bus for the specified PCI bridge device. Note
642 * that the PCI bridge device might not have any children yet.
643 * However, looking up its bus number doesn't require a valid child
644 * device, so we just pass NULL.
646 if (BUS_READ_IVAR(pcib, NULL, PCIB_IVAR_BUS, &bus) != 0) {
647 device_printf(pcib, "Unable to read PCI bus number");
648 panic("PCI bridge without a bus number");
651 /* Bump the reference count. */
652 ACPI_SERIAL_BEGIN(pci_link);
653 link = acpi_pci_link_lookup(dev, index);
654 if (link == NULL) {
655 device_printf(dev, "apparently invalid index %d\n", index);
656 ACPI_SERIAL_END(pci_link);
657 return;
659 link->l_references++;
660 if (link->l_routed)
661 pci_link_interrupt_weights[link->l_irq]++;
664 * The BIOS only routes interrupts via ISA IRQs using the ATPICs
665 * (8259As). Thus, if this link is routed via an ISA IRQ, go
666 * look to see if the BIOS routed an IRQ for this link at the
667 * indicated (bus, slot, pin). If so, we prefer that IRQ for
668 * this link and add that IRQ to our list of known-good IRQs.
669 * This provides a good work-around for link devices whose _CRS
670 * method is either broken or bogus. We only use the value
671 * returned by _CRS if we can't find a valid IRQ via this method
672 * in fact.
674 * If this link is not routed via an ISA IRQ (because we are using
675 * APIC for example), then don't bother looking up the BIOS IRQ
676 * as if we find one it won't be valid anyway.
678 if (!link->l_isa_irq) {
679 ACPI_SERIAL_END(pci_link);
680 return;
683 /* Try to find a BIOS IRQ setting from any matching devices. */
684 bios_irq = acpi_pci_link_search_irq(bus, slot, pin);
685 if (!PCI_INTERRUPT_VALID(bios_irq)) {
686 ACPI_SERIAL_END(pci_link);
687 return;
690 /* Validate the BIOS IRQ. */
691 if (!link_valid_irq(link, bios_irq)) {
692 device_printf(dev, "BIOS IRQ %u for %d.%d.INT%c is invalid\n",
693 bios_irq, (int)bus, slot, pin + 'A');
694 } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) {
695 link->l_bios_irq = bios_irq;
697 * SCI setting is handled by acpi_pci_link_identify()
699 if (bios_irq < NUM_ISA_INTERRUPTS &&
700 AcpiGbl_FADT.SciInterrupt != bios_irq)
701 pci_link_bios_isa_irqs |= (1 << bios_irq);
702 if (bios_irq != link->l_initial_irq &&
703 PCI_INTERRUPT_VALID(link->l_initial_irq))
704 device_printf(dev,
705 "BIOS IRQ %u does not match initial IRQ %u\n",
706 bios_irq, link->l_initial_irq);
707 } else if (bios_irq != link->l_bios_irq)
708 device_printf(dev,
709 "BIOS IRQ %u for %d.%d.INT%c does not match previous BIOS IRQ %u\n",
710 bios_irq, (int)bus, slot, pin + 'A',
711 link->l_bios_irq);
712 ACPI_SERIAL_END(pci_link);
715 static ACPI_STATUS
716 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
718 ACPI_RESOURCE *end, *res;
719 ACPI_STATUS status;
720 struct link *link;
721 int i, in_dpf;
723 /* Fetch the _CRS. */
724 ACPI_SERIAL_ASSERT(pci_link);
725 srsbuf->Pointer = NULL;
726 srsbuf->Length = ACPI_ALLOCATE_BUFFER;
727 status = AcpiGetCurrentResources(acpi_get_handle(sc->pl_dev), srsbuf);
728 if (ACPI_SUCCESS(status) && srsbuf->Pointer == NULL)
729 status = AE_NO_MEMORY;
730 if (ACPI_FAILURE(status)) {
731 if (bootverbose)
732 device_printf(sc->pl_dev,
733 "Unable to fetch current resources: %s\n",
734 AcpiFormatException(status));
735 return (status);
738 /* Fill in IRQ resources via link structures. */
739 link = sc->pl_links;
740 i = 0;
741 in_dpf = DPF_OUTSIDE;
742 res = (ACPI_RESOURCE *)srsbuf->Pointer;
743 end = (ACPI_RESOURCE *)((char *)srsbuf->Pointer + srsbuf->Length);
744 for (;;) {
745 switch (res->Type) {
746 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
747 switch (in_dpf) {
748 case DPF_OUTSIDE:
749 /* We've started the first DPF. */
750 in_dpf = DPF_FIRST;
751 break;
752 case DPF_FIRST:
753 /* We've started the second DPF. */
754 panic(
755 "%s: Multiple dependent functions within a current resource",
756 __func__);
757 break;
759 break;
760 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
761 /* We are finished with DPF parsing. */
762 KASSERT(in_dpf != DPF_OUTSIDE,
763 ("%s: end dpf when not parsing a dpf", __func__));
764 in_dpf = DPF_OUTSIDE;
765 break;
766 case ACPI_RESOURCE_TYPE_IRQ:
767 KKASSERT(i < sc->pl_num_links);
768 res->Data.Irq.InterruptCount = 1;
769 if (PCI_INTERRUPT_VALID(link->l_irq)) {
770 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
771 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
772 __func__, link->l_irq));
773 res->Data.Irq.Interrupts[0] = link->l_irq;
774 } else
775 res->Data.Irq.Interrupts[0] = 0;
776 link++;
777 i++;
778 break;
779 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
780 KKASSERT(i < sc->pl_num_links);
781 res->Data.ExtendedIrq.InterruptCount = 1;
782 if (PCI_INTERRUPT_VALID(link->l_irq))
783 res->Data.ExtendedIrq.Interrupts[0] =
784 link->l_irq;
785 else
786 res->Data.ExtendedIrq.Interrupts[0] = 0;
787 link++;
788 i++;
789 break;
791 if (res->Type == ACPI_RESOURCE_TYPE_END_TAG)
792 break;
793 res = ACPI_NEXT_RESOURCE(res);
794 if (res >= end)
795 break;
797 return (AE_OK);
800 static ACPI_STATUS
801 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc,
802 ACPI_BUFFER *srsbuf)
804 ACPI_RESOURCE newres;
805 ACPI_STATUS status;
806 struct link *link;
807 int i;
809 /* Start off with an empty buffer. */
810 srsbuf->Pointer = NULL;
811 link = sc->pl_links;
812 for (i = 0; i < sc->pl_num_links; i++) {
814 /* Add a new IRQ resource from each link. */
815 link = &sc->pl_links[i];
816 if (link->l_prs_template.Type == ACPI_RESOURCE_TYPE_IRQ) {
818 /* Build an IRQ resource. */
819 bcopy(&link->l_prs_template, &newres,
820 ACPI_RS_SIZE(newres.Data.Irq));
821 newres.Data.Irq.InterruptCount = 1;
822 if (PCI_INTERRUPT_VALID(link->l_irq)) {
823 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
824 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
825 __func__, link->l_irq));
826 newres.Data.Irq.Interrupts[0] = link->l_irq;
827 } else
828 newres.Data.Irq.Interrupts[0] = 0;
829 } else {
831 /* Build an ExtIRQ resuorce. */
832 bcopy(&link->l_prs_template, &newres,
833 ACPI_RS_SIZE(newres.Data.ExtendedIrq));
834 newres.Data.ExtendedIrq.InterruptCount = 1;
835 if (PCI_INTERRUPT_VALID(link->l_irq))
836 newres.Data.ExtendedIrq.Interrupts[0] =
837 link->l_irq;
838 else
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);
850 return (status);
853 return (AE_OK);
856 static ACPI_STATUS
857 acpi_pci_link_route_irqs(device_t dev)
859 struct acpi_pci_link_softc *sc;
860 ACPI_RESOURCE *resource, *end;
861 ACPI_BUFFER srsbuf;
862 ACPI_STATUS status;
863 struct link *link;
864 int i;
866 ACPI_SERIAL_ASSERT(pci_link);
867 sc = device_get_softc(dev);
868 if (sc->pl_crs_bad)
869 status = acpi_pci_link_srs_from_links(sc, &srsbuf);
870 else
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);
879 return (status);
883 * Perform acpi_config_intr() on each IRQ resource if it was just
884 * routed for the first time.
886 link = sc->pl_links;
887 i = 0;
888 resource = (ACPI_RESOURCE *)srsbuf.Pointer;
889 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length);
890 for (;;) {
891 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
892 break;
893 switch (resource->Type) {
894 case ACPI_RESOURCE_TYPE_IRQ:
895 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
896 KKASSERT(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] +=
908 link->l_references;
910 link++;
911 i++;
912 break;
914 resource = ACPI_NEXT_RESOURCE(resource);
915 if (resource >= end)
916 break;
918 AcpiOsFree(srsbuf.Pointer);
919 return (AE_OK);
922 static int
923 acpi_pci_link_resume(device_t dev)
925 struct acpi_pci_link_softc *sc;
926 ACPI_STATUS status;
927 int i, routed;
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);
935 routed = 0;
936 for (i = 0; i < sc->pl_num_links; i++)
937 if (sc->pl_links[i].l_routed)
938 routed++;
939 if (routed == sc->pl_num_links)
940 status = acpi_pci_link_route_irqs(dev);
941 else {
942 AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL, NULL);
943 status = AE_OK;
945 ACPI_SERIAL_END(pci_link);
946 if (ACPI_FAILURE(status))
947 return (ENXIO);
948 else
949 return (0);
953 * Pick an IRQ to use for this unrouted link.
955 static uint8_t
956 acpi_pci_link_choose_irq(device_t dev, struct link *link)
958 char tunable_buffer[64], link_name[5];
959 uint8_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))
973 device_printf(dev,
974 "Warning, IRQ %d is not listed as valid\n",
976 return (i);
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))
982 device_printf(dev,
983 "Warning, IRQ %d is not listed as valid\n",
985 return (i);
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,
998 * then use that.
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)
1014 continue;
1015 pos_weight = pci_link_interrupt_weights[pos_irq];
1016 if (pos_weight < best_weight) {
1017 best_weight = pos_weight;
1018 best_irq = pos_irq;
1023 * If this is an ISA IRQ and SCI could be shared, try using
1024 * the SCI as a fallback.
1026 if (link->l_isa_irq && acpi_sci_pci_shareable()) {
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;
1031 best_irq = pos_irq;
1035 if (PCI_INTERRUPT_VALID(best_irq)) {
1036 if (bootverbose)
1037 device_printf(dev, "Picked IRQ %u with weight %d\n",
1038 best_irq, best_weight);
1039 } else
1040 device_printf(dev, "Unable to choose an IRQ\n");
1041 return (best_irq);
1045 acpi_pci_link_route_interrupt(device_t dev, int index)
1047 struct link *link;
1049 if (acpi_disabled("pci_link"))
1050 return (PCI_INVALID_IRQ);
1052 ACPI_SERIAL_BEGIN(pci_link);
1053 link = acpi_pci_link_lookup(dev, index);
1054 if (link == NULL)
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.
1090 static void
1091 acpi_pci_link_identify(driver_t *driver, device_t parent)
1094 * If the SCI is an ISA IRQ and could be shared,
1095 * add it to the bitmask of known good ISA IRQs.
1097 if (AcpiGbl_FADT.SciInterrupt < NUM_ISA_INTERRUPTS &&
1098 acpi_sci_pci_shareable())
1099 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT.SciInterrupt);
1102 static device_method_t acpi_pci_link_methods[] = {
1103 /* Device interface */
1104 DEVMETHOD(device_identify, acpi_pci_link_identify),
1105 DEVMETHOD(device_probe, acpi_pci_link_probe),
1106 DEVMETHOD(device_attach, acpi_pci_link_attach),
1107 DEVMETHOD(device_resume, acpi_pci_link_resume),
1109 DEVMETHOD_END
1112 static driver_t acpi_pci_link_driver = {
1113 "pci_link",
1114 acpi_pci_link_methods,
1115 sizeof(struct acpi_pci_link_softc),
1118 static devclass_t pci_link_devclass;
1120 DRIVER_MODULE(acpi_pci_link, acpi, acpi_pci_link_driver, pci_link_devclass,
1121 NULL, NULL);
1122 MODULE_DEPEND(acpi_pci_link, acpi, 1, 1, 1);