1 /*-
2 * Copyright (c) 2002 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
3 * All rights reserved.
4 *
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.
13 *
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.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: releng/6.1/sys/dev/acpica/acpi_pci_link.c 154197 2006-01-10 21:37:18Z jhb $");
29
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/limits.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37
38 #include <contrib/dev/acpica/acpi.h>
39 #include <dev/acpica/acpivar.h>
40 #include <dev/acpica/acpi_pcibvar.h>
41
42 #include <machine/pci_cfgreg.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
45 #include "pcib_if.h"
46
47 /* Hooks for the ACPI CA debugging infrastructure. */
48 #define _COMPONENT ACPI_BUS
49 ACPI_MODULE_NAME("PCI_LINK")
50
51 ACPI_SERIAL_DECL(pci_link, "ACPI PCI link");
52
53 #define NUM_ISA_INTERRUPTS 16
54 #define NUM_ACPI_INTERRUPTS 256
55
56 /*
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.
60 *
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:
66 *
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
78 *
79 * The XXX is because I'm not sure if this is a valid assumption to make.
80 */
81
82 /* States during DPF processing. */
83 #define DPF_OUTSIDE 0
84 #define DPF_FIRST 1
85 #define DPF_IGNORE 2
86
87 struct link;
88
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;
94 };
95
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 int l_res_index;
102 int l_num_irqs;
103 int *l_irqs;
104 int l_references;
105 int l_routed:1;
106 int l_isa_irq:1;
107 ACPI_RESOURCE l_prs_template;
108 };
109
110 struct link_count_request {
111 int in_dpf;
112 int count;
113 };
114
115 struct link_res_request {
116 struct acpi_pci_link_softc *sc;
117 int in_dpf;
118 int res_index;
119 int link_index;
120 };
121
122 MALLOC_DEFINE(M_PCI_LINK, "PCI Link", "ACPI PCI Link structures");
123
124 static int pci_link_interrupt_weights[NUM_ACPI_INTERRUPTS];
125 static int pci_link_bios_isa_irqs;
126
127 static char *pci_link_ids[] = { "PNP0C0F", NULL };
128
129 /*
130 * Fetch the short name associated with an ACPI handle and save it in the
131 * passed in buffer.
132 */
133 static ACPI_STATUS
134 acpi_short_name(ACPI_HANDLE handle, char *buffer, size_t buflen)
135 {
136 ACPI_BUFFER buf;
137
138 buf.Length = buflen;
139 buf.Pointer = buffer;
140 return (AcpiGetName(handle, ACPI_SINGLE_NAME, &buf));
141 }
142
143 static int
144 acpi_pci_link_probe(device_t dev)
145 {
146 char descr[28], name[12];
147
148 /*
149 * We explicitly do not check _STA since not all systems set it to
150 * sensible values.
151 */
152 if (acpi_disabled("pci_link") ||
153 ACPI_ID_PROBE(device_get_parent(dev), dev, pci_link_ids) == NULL)
154 return (ENXIO);
155
156 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), name,
157 sizeof(name)))) {
158 snprintf(descr, sizeof(descr), "ACPI PCI Link %s", name);
159 device_set_desc_copy(dev, descr);
160 } else
161 device_set_desc(dev, "ACPI PCI Link");
162 device_quiet(dev);
163 return (0);
164 }
165
166 static ACPI_STATUS
167 acpi_count_irq_resources(ACPI_RESOURCE *res, void *context)
168 {
169 struct link_count_request *req;
170
171 req = (struct link_count_request *)context;
172 switch (res->Id) {
173 case ACPI_RSTYPE_START_DPF:
174 switch (req->in_dpf) {
175 case DPF_OUTSIDE:
176 /* We've started the first DPF. */
177 req->in_dpf = DPF_FIRST;
178 break;
179 case DPF_FIRST:
180 /* We've started the second DPF. */
181 req->in_dpf = DPF_IGNORE;
182 break;
183 }
184 break;
185 case ACPI_RSTYPE_END_DPF:
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;
190 break;
191 case ACPI_RSTYPE_IRQ:
192 case ACPI_RSTYPE_EXT_IRQ:
193 /*
194 * Don't count resources if we are in a DPF set that we are
195 * ignoring.
196 */
197 if (req->in_dpf != DPF_IGNORE)
198 req->count++;
199 }
200 return (AE_OK);
201 }
202
203 static ACPI_STATUS
204 link_add_crs(ACPI_RESOURCE *res, void *context)
205 {
206 struct link_res_request *req;
207 struct link *link;
208
209 ACPI_SERIAL_ASSERT(pci_link);
210 req = (struct link_res_request *)context;
211 switch (res->Id) {
212 case ACPI_RSTYPE_START_DPF:
213 switch (req->in_dpf) {
214 case DPF_OUTSIDE:
215 /* We've started the first DPF. */
216 req->in_dpf = DPF_FIRST;
217 break;
218 case DPF_FIRST:
219 /* We've started the second DPF. */
220 panic(
221 "%s: Multiple dependent functions within a current resource",
222 __func__);
223 break;
224 }
225 break;
226 case ACPI_RSTYPE_END_DPF:
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;
231 break;
232 case ACPI_RSTYPE_IRQ:
233 case ACPI_RSTYPE_EXT_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;
238 req->link_index++;
239 req->res_index++;
240
241 /*
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.
245 */
246 if (res->Id == ACPI_RSTYPE_IRQ) {
247 if (res->Data.Irq.NumberOfInterrupts == 1)
248 link->l_irq = res->Data.Irq.Interrupts[0];
249 } else if (res->Data.ExtendedIrq.NumberOfInterrupts == 1)
250 link->l_irq = res->Data.ExtendedIrq.Interrupts[0];
251
252 /*
253 * An IRQ of zero means that the link isn't routed.
254 */
255 if (link->l_irq == 0)
256 link->l_irq = PCI_INVALID_IRQ;
257 break;
258 default:
259 req->res_index++;
260 }
261 return (AE_OK);
262 }
263
264 /*
265 * Populate the set of possible IRQs for each device.
266 */
267 static ACPI_STATUS
268 link_add_prs(ACPI_RESOURCE *res, void *context)
269 {
270 struct link_res_request *req;
271 struct link *link;
272 UINT32 *irqs;
273 int i;
274
275 ACPI_SERIAL_ASSERT(pci_link);
276 req = (struct link_res_request *)context;
277 switch (res->Id) {
278 case ACPI_RSTYPE_START_DPF:
279 switch (req->in_dpf) {
280 case DPF_OUTSIDE:
281 /* We've started the first DPF. */
282 req->in_dpf = DPF_FIRST;
283 break;
284 case DPF_FIRST:
285 /* We've started the second DPF. */
286 req->in_dpf = DPF_IGNORE;
287 break;
288 }
289 break;
290 case ACPI_RSTYPE_END_DPF:
291 /* We are finished with DPF parsing. */
292 KASSERT(req->in_dpf != DPF_OUTSIDE,
293 ("%s: end dpf when not parsing a dpf", __func__));
294 req->in_dpf = DPF_OUTSIDE;
295 break;
296 case ACPI_RSTYPE_IRQ:
297 case ACPI_RSTYPE_EXT_IRQ:
298 /*
299 * Don't parse resources if we are in a DPF set that we are
300 * ignoring.
301 */
302 if (req->in_dpf == DPF_IGNORE)
303 break;
304
305 KASSERT(req->link_index < req->sc->pl_num_links,
306 ("%s: array boundary violation", __func__));
307 link = &req->sc->pl_links[req->link_index];
308 if (link->l_res_index == -1) {
309 KASSERT(req->sc->pl_crs_bad,
310 ("res_index should be set"));
311 link->l_res_index = req->res_index;
312 }
313 req->link_index++;
314 req->res_index++;
315
316 /*
317 * Stash a copy of the resource for later use when doing
318 * _SRS.
319 */
320 bcopy(res, &link->l_prs_template, sizeof(ACPI_RESOURCE));
321 if (res->Id == ACPI_RSTYPE_IRQ) {
322 link->l_num_irqs = res->Data.Irq.NumberOfInterrupts;
323 irqs = res->Data.Irq.Interrupts;
324 } else {
325 link->l_num_irqs =
326 res->Data.ExtendedIrq.NumberOfInterrupts;
327 irqs = res->Data.ExtendedIrq.Interrupts;
328 }
329 if (link->l_num_irqs == 0)
330 break;
331
332 /*
333 * Save a list of the valid IRQs. Also, if all of the
334 * valid IRQs are ISA IRQs, then mark this link as
335 * routed via an ISA interrupt.
336 */
337 link->l_isa_irq = TRUE;
338 link->l_irqs = malloc(sizeof(int) * link->l_num_irqs,
339 M_PCI_LINK, M_WAITOK | M_ZERO);
340 for (i = 0; i < link->l_num_irqs; i++) {
341 link->l_irqs[i] = irqs[i];
342 if (irqs[i] >= NUM_ISA_INTERRUPTS)
343 link->l_isa_irq = FALSE;
344 }
345 break;
346 default:
347 if (req->in_dpf == DPF_IGNORE)
348 break;
349 if (req->sc->pl_crs_bad)
350 device_printf(req->sc->pl_dev,
351 "Warning: possible resource %d will be lost during _SRS\n",
352 req->res_index);
353 req->res_index++;
354 }
355 return (AE_OK);
356 }
357
358 static int
359 link_valid_irq(struct link *link, int irq)
360 {
361 int i;
362
363 ACPI_SERIAL_ASSERT(pci_link);
364
365 /* Invalid interrupts are never valid. */
366 if (!PCI_INTERRUPT_VALID(irq))
367 return (FALSE);
368
369 /* Any interrupt in the list of possible interrupts is valid. */
370 for (i = 0; i < link->l_num_irqs; i++)
371 if (link->l_irqs[i] == irq)
372 return (TRUE);
373
374 /*
375 * For links routed via an ISA interrupt, if the SCI is routed via
376 * an ISA interrupt, the SCI is always treated as a valid IRQ.
377 */
378 if (link->l_isa_irq && AcpiGbl_FADT->SciInt == irq &&
379 irq < NUM_ISA_INTERRUPTS)
380 return (TRUE);
381
382 /* If the interrupt wasn't found in the list it is not valid. */
383 return (FALSE);
384 }
385
386 static void
387 acpi_pci_link_dump(struct acpi_pci_link_softc *sc)
388 {
389 struct link *link;
390 int i, j;
391
392 ACPI_SERIAL_ASSERT(pci_link);
393 printf("Index IRQ Rtd Ref IRQs\n");
394 for (i = 0; i < sc->pl_num_links; i++) {
395 link = &sc->pl_links[i];
396 printf("%5d %3d %c %3d ", i, link->l_irq,
397 link->l_routed ? 'Y' : 'N', link->l_references);
398 if (link->l_num_irqs == 0)
399 printf(" none");
400 else for (j = 0; j < link->l_num_irqs; j++)
401 printf(" %d", link->l_irqs[j]);
402 printf("\n");
403 }
404 }
405
406 static int
407 acpi_pci_link_attach(device_t dev)
408 {
409 struct acpi_pci_link_softc *sc;
410 struct link_count_request creq;
411 struct link_res_request rreq;
412 ACPI_STATUS status;
413 int i;
414
415 sc = device_get_softc(dev);
416 sc->pl_dev = dev;
417 ACPI_SERIAL_BEGIN(pci_link);
418
419 /*
420 * Count the number of current resources so we know how big of
421 * a link array to allocate. On some systems, _CRS is broken,
422 * so for those systems try to derive the count from _PRS instead.
423 */
424 creq.in_dpf = DPF_OUTSIDE;
425 creq.count = 0;
426 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
427 acpi_count_irq_resources, &creq);
428 sc->pl_crs_bad = ACPI_FAILURE(status);
429 if (sc->pl_crs_bad) {
430 creq.in_dpf = DPF_OUTSIDE;
431 creq.count = 0;
432 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
433 acpi_count_irq_resources, &creq);
434 if (ACPI_FAILURE(status)) {
435 device_printf(dev,
436 "Unable to parse _CRS or _PRS: %s\n",
437 AcpiFormatException(status));
438 ACPI_SERIAL_END(pci_link);
439 return (ENXIO);
440 }
441 }
442 sc->pl_num_links = creq.count;
443 if (creq.count == 0) {
444 ACPI_SERIAL_END(pci_link);
445 return (0);
446 }
447 sc->pl_links = malloc(sizeof(struct link) * sc->pl_num_links,
448 M_PCI_LINK, M_WAITOK | M_ZERO);
449
450 /* Initialize the child links. */
451 for (i = 0; i < sc->pl_num_links; i++) {
452 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
453 sc->pl_links[i].l_bios_irq = PCI_INVALID_IRQ;
454 sc->pl_links[i].l_sc = sc;
455 sc->pl_links[i].l_isa_irq = FALSE;
456 sc->pl_links[i].l_res_index = -1;
457 }
458
459 /* Try to read the current settings from _CRS if it is valid. */
460 if (!sc->pl_crs_bad) {
461 rreq.in_dpf = DPF_OUTSIDE;
462 rreq.link_index = 0;
463 rreq.res_index = 0;
464 rreq.sc = sc;
465 status = AcpiWalkResources(acpi_get_handle(dev), "_CRS",
466 link_add_crs, &rreq);
467 if (ACPI_FAILURE(status)) {
468 device_printf(dev, "Unable to parse _CRS: %s\n",
469 AcpiFormatException(status));
470 goto fail;
471 }
472 }
473
474 /*
475 * Try to read the possible settings from _PRS. Note that if the
476 * _CRS is toast, we depend on having a working _PRS. However, if
477 * _CRS works, then it is ok for _PRS to be missing.
478 */
479 rreq.in_dpf = DPF_OUTSIDE;
480 rreq.link_index = 0;
481 rreq.res_index = 0;
482 rreq.sc = sc;
483 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
484 link_add_prs, &rreq);
485 if (ACPI_FAILURE(status) &&
486 (status != AE_NOT_FOUND || sc->pl_crs_bad)) {
487 device_printf(dev, "Unable to parse _PRS: %s\n",
488 AcpiFormatException(status));
489 goto fail;
490 }
491 if (bootverbose) {
492 device_printf(dev, "Links after initial probe:\n");
493 acpi_pci_link_dump(sc);
494 }
495
496 /* Verify initial IRQs if we have _PRS. */
497 if (status != AE_NOT_FOUND)
498 for (i = 0; i < sc->pl_num_links; i++)
499 if (!link_valid_irq(&sc->pl_links[i],
500 sc->pl_links[i].l_irq))
501 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
502 if (bootverbose) {
503 device_printf(dev, "Links after initial validation:\n");
504 acpi_pci_link_dump(sc);
505 }
506
507 /* Save initial IRQs. */
508 for (i = 0; i < sc->pl_num_links; i++)
509 sc->pl_links[i].l_initial_irq = sc->pl_links[i].l_irq;
510
511 /*
512 * Try to disable this link. If successful, set the current IRQ to
513 * zero and flags to indicate this link is not routed. If we can't
514 * run _DIS (i.e., the method doesn't exist), assume the initial
515 * IRQ was routed by the BIOS.
516 */
517 if (ACPI_SUCCESS(AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL,
518 NULL)))
519 for (i = 0; i < sc->pl_num_links; i++)
520 sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
521 else
522 for (i = 0; i < sc->pl_num_links; i++)
523 if (PCI_INTERRUPT_VALID(sc->pl_links[i].l_irq))
524 sc->pl_links[i].l_routed = TRUE;
525 if (bootverbose) {
526 device_printf(dev, "Links after disable:\n");
527 acpi_pci_link_dump(sc);
528 }
529 ACPI_SERIAL_END(pci_link);
530 return (0);
531 fail:
532 ACPI_SERIAL_END(pci_link);
533 for (i = 0; i < sc->pl_num_links; i++)
534 if (sc->pl_links[i].l_irqs != NULL)
535 free(sc->pl_links[i].l_irqs, M_PCI_LINK);
536 free(sc->pl_links, M_PCI_LINK);
537 return (ENXIO);
538 }
539
540 /* XXX: Note that this is identical to pci_pir_search_irq(). */
541 static uint8_t
542 acpi_pci_link_search_irq(int bus, int device, int pin)
543 {
544 uint32_t value;
545 uint8_t func, maxfunc;
546
547 /* See if we have a valid device at function 0. */
548 value = pci_cfgregread(bus, device, 0, PCIR_HDRTYPE, 1);
549 if ((value & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
550 return (PCI_INVALID_IRQ);
551 if (value & PCIM_MFDEV)
552 maxfunc = PCI_FUNCMAX;
553 else
554 maxfunc = 0;
555
556 /* Scan all possible functions at this device. */
557 for (func = 0; func <= maxfunc; func++) {
558 value = pci_cfgregread(bus, device, func, PCIR_DEVVENDOR, 4);
559 if (value == 0xffffffff)
560 continue;
561 value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
562
563 /*
564 * See if it uses the pin in question. Note that the passed
565 * in pin uses 0 for A, .. 3 for D whereas the intpin
566 * register uses 0 for no interrupt, 1 for A, .. 4 for D.
567 */
568 if (value != pin + 1)
569 continue;
570 value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
571 if (bootverbose)
572 printf(
573 "ACPI: Found matching pin for %d.%d.INT%c at func %d: %d\n",
574 bus, device, pin + 'A', func, value);
575 if (value != PCI_INVALID_IRQ)
576 return (value);
577 }
578 return (PCI_INVALID_IRQ);
579 }
580
581 /*
582 * Find the link structure that corresponds to the resource index passed in
583 * via 'source_index'.
584 */
585 static struct link *
586 acpi_pci_link_lookup(device_t dev, int source_index)
587 {
588 struct acpi_pci_link_softc *sc;
589 int i;
590
591 ACPI_SERIAL_ASSERT(pci_link);
592 sc = device_get_softc(dev);
593 for (i = 0; i < sc->pl_num_links; i++)
594 if (sc->pl_links[i].l_res_index == source_index)
595 return (&sc->pl_links[i]);
596 return (NULL);
597 }
598
599 void
600 acpi_pci_link_add_reference(device_t dev, int index, device_t pcib, int slot,
601 int pin)
602 {
603 struct link *link;
604 uint8_t bios_irq;
605 uintptr_t bus;
606
607 /*
608 * Look up the PCI bus for the specified PCI bridge device. Note
609 * that the PCI bridge device might not have any children yet.
610 * However, looking up its bus number doesn't require a valid child
611 * device, so we just pass NULL.
612 */
613 if (BUS_READ_IVAR(pcib, NULL, PCIB_IVAR_BUS, &bus) != 0) {
614 device_printf(pcib, "Unable to read PCI bus number");
615 panic("PCI bridge without a bus number");
616 }
617
618 /* Bump the reference count. */
619 ACPI_SERIAL_BEGIN(pci_link);
620 link = acpi_pci_link_lookup(dev, index);
621 if (link == NULL) {
622 device_printf(dev, "apparently invalid index %d\n", index);
623 ACPI_SERIAL_END(pci_link);
624 return;
625 }
626 link->l_references++;
627 if (link->l_routed)
628 pci_link_interrupt_weights[link->l_irq]++;
629
630 /*
631 * The BIOS only routes interrupts via ISA IRQs using the ATPICs
632 * (8259As). Thus, if this link is routed via an ISA IRQ, go
633 * look to see if the BIOS routed an IRQ for this link at the
634 * indicated (bus, slot, pin). If so, we prefer that IRQ for
635 * this link and add that IRQ to our list of known-good IRQs.
636 * This provides a good work-around for link devices whose _CRS
637 * method is either broken or bogus. We only use the value
638 * returned by _CRS if we can't find a valid IRQ via this method
639 * in fact.
640 *
641 * If this link is not routed via an ISA IRQ (because we are using
642 * APIC for example), then don't bother looking up the BIOS IRQ
643 * as if we find one it won't be valid anyway.
644 */
645 if (!link->l_isa_irq) {
646 ACPI_SERIAL_END(pci_link);
647 return;
648 }
649
650 /* Try to find a BIOS IRQ setting from any matching devices. */
651 bios_irq = acpi_pci_link_search_irq(bus, slot, pin);
652 if (!PCI_INTERRUPT_VALID(bios_irq)) {
653 ACPI_SERIAL_END(pci_link);
654 return;
655 }
656
657 /* Validate the BIOS IRQ. */
658 if (!link_valid_irq(link, bios_irq)) {
659 device_printf(dev, "BIOS IRQ %u for %d.%d.INT%c is invalid\n",
660 bios_irq, (int)bus, slot, pin + 'A');
661 } else if (!PCI_INTERRUPT_VALID(link->l_bios_irq)) {
662 link->l_bios_irq = bios_irq;
663 if (bios_irq < NUM_ISA_INTERRUPTS)
664 pci_link_bios_isa_irqs |= (1 << bios_irq);
665 if (bios_irq != link->l_initial_irq &&
666 PCI_INTERRUPT_VALID(link->l_initial_irq))
667 device_printf(dev,
668 "BIOS IRQ %u does not match initial IRQ %u\n",
669 bios_irq, link->l_initial_irq);
670 } else if (bios_irq != link->l_bios_irq)
671 device_printf(dev,
672 "BIOS IRQ %u for %d.%d.INT%c does not match previous BIOS IRQ %u\n",
673 bios_irq, (int)bus, slot, pin + 'A',
674 link->l_bios_irq);
675 ACPI_SERIAL_END(pci_link);
676 }
677
678 static ACPI_STATUS
679 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
680 {
681 ACPI_RESOURCE *resource, *end, newres, *resptr;
682 ACPI_BUFFER crsbuf;
683 ACPI_STATUS status;
684 struct link *link;
685 int i, in_dpf;
686
687 /* Fetch the _CRS. */
688 ACPI_SERIAL_ASSERT(pci_link);
689 crsbuf.Pointer = NULL;
690 crsbuf.Length = ACPI_ALLOCATE_BUFFER;
691 status = AcpiGetCurrentResources(acpi_get_handle(sc->pl_dev), &crsbuf);
692 if (ACPI_SUCCESS(status) && crsbuf.Pointer == NULL)
693 status = AE_NO_MEMORY;
694 if (ACPI_FAILURE(status)) {
695 if (bootverbose)
696 device_printf(sc->pl_dev,
697 "Unable to fetch current resources: %s\n",
698 AcpiFormatException(status));
699 return (status);
700 }
701
702 /* Fill in IRQ resources via link structures. */
703 srsbuf->Pointer = NULL;
704 link = sc->pl_links;
705 i = 0;
706 in_dpf = DPF_OUTSIDE;
707 resource = (ACPI_RESOURCE *)crsbuf.Pointer;
708 end = (ACPI_RESOURCE *)((char *)crsbuf.Pointer + crsbuf.Length);
709 for (;;) {
710 switch (resource->Id) {
711 case ACPI_RSTYPE_START_DPF:
712 switch (in_dpf) {
713 case DPF_OUTSIDE:
714 /* We've started the first DPF. */
715 in_dpf = DPF_FIRST;
716 break;
717 case DPF_FIRST:
718 /* We've started the second DPF. */
719 panic(
720 "%s: Multiple dependent functions within a current resource",
721 __func__);
722 break;
723 }
724 resptr = NULL;
725 break;
726 case ACPI_RSTYPE_END_DPF:
727 /* We are finished with DPF parsing. */
728 KASSERT(in_dpf != DPF_OUTSIDE,
729 ("%s: end dpf when not parsing a dpf", __func__));
730 in_dpf = DPF_OUTSIDE;
731 resptr = NULL;
732 break;
733 case ACPI_RSTYPE_IRQ:
734 MPASS(i < sc->pl_num_links);
735 MPASS(link->l_prs_template.Id == ACPI_RSTYPE_IRQ);
736 newres = link->l_prs_template;
737 resptr = &newres;
738 resptr->Data.Irq.NumberOfInterrupts = 1;
739 if (PCI_INTERRUPT_VALID(link->l_irq)) {
740 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
741 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
742 __func__, link->l_irq));
743 resptr->Data.Irq.Interrupts[0] = link->l_irq;
744 } else
745 resptr->Data.Irq.Interrupts[0] = 0;
746 link++;
747 i++;
748 break;
749 case ACPI_RSTYPE_EXT_IRQ:
750 MPASS(i < sc->pl_num_links);
751 MPASS(link->l_prs_template.Id == ACPI_RSTYPE_EXT_IRQ);
752 newres = link->l_prs_template;
753 resptr = &newres;
754 resptr->Data.ExtendedIrq.NumberOfInterrupts = 1;
755 if (PCI_INTERRUPT_VALID(link->l_irq))
756 resptr->Data.ExtendedIrq.Interrupts[0] =
757 link->l_irq;
758 else
759 resptr->Data.ExtendedIrq.Interrupts[0] = 0;
760 link++;
761 i++;
762 break;
763 default:
764 resptr = resource;
765 }
766 if (resptr != NULL) {
767 status = acpi_AppendBufferResource(srsbuf, resptr);
768 if (ACPI_FAILURE(status)) {
769 device_printf(sc->pl_dev,
770 "Unable to build resources: %s\n",
771 AcpiFormatException(status));
772 if (srsbuf->Pointer != NULL)
773 AcpiOsFree(srsbuf->Pointer);
774 AcpiOsFree(crsbuf.Pointer);
775 return (status);
776 }
777 }
778 if (resource->Id == ACPI_RSTYPE_END_TAG)
779 break;
780 resource = ACPI_NEXT_RESOURCE(resource);
781 if (resource >= end)
782 break;
783 }
784 AcpiOsFree(crsbuf.Pointer);
785 return (AE_OK);
786 }
787
788 static ACPI_STATUS
789 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc,
790 ACPI_BUFFER *srsbuf)
791 {
792 ACPI_RESOURCE newres;
793 ACPI_STATUS status;
794 struct link *link;
795 int i;
796
797 /* Start off with an empty buffer. */
798 srsbuf->Pointer = NULL;
799 link = sc->pl_links;
800 for (i = 0; i < sc->pl_num_links; i++) {
801
802 /* Add a new IRQ resource from each link. */
803 link = &sc->pl_links[i];
804 newres = link->l_prs_template;
805 if (newres.Id == ACPI_RSTYPE_IRQ) {
806
807 /* Build an IRQ resource. */
808 newres.Data.Irq.NumberOfInterrupts = 1;
809 if (PCI_INTERRUPT_VALID(link->l_irq)) {
810 KASSERT(link->l_irq < NUM_ISA_INTERRUPTS,
811 ("%s: can't put non-ISA IRQ %d in legacy IRQ resource type",
812 __func__, link->l_irq));
813 newres.Data.Irq.Interrupts[0] = link->l_irq;
814 } else
815 newres.Data.Irq.Interrupts[0] = 0;
816 } else {
817
818 /* Build an ExtIRQ resuorce. */
819 newres.Data.ExtendedIrq.NumberOfInterrupts = 1;
820 if (PCI_INTERRUPT_VALID(link->l_irq))
821 newres.Data.ExtendedIrq.Interrupts[0] =
822 link->l_irq;
823 else
824 newres.Data.ExtendedIrq.Interrupts[0] = 0;
825 }
826
827 /* Add the new resource to the end of the _SRS buffer. */
828 status = acpi_AppendBufferResource(srsbuf, &newres);
829 if (ACPI_FAILURE(status)) {
830 device_printf(sc->pl_dev,
831 "Unable to build resources: %s\n",
832 AcpiFormatException(status));
833 if (srsbuf->Pointer != NULL)
834 AcpiOsFree(srsbuf->Pointer);
835 return (status);
836 }
837 }
838 return (AE_OK);
839 }
840
841 static ACPI_STATUS
842 acpi_pci_link_route_irqs(device_t dev)
843 {
844 struct acpi_pci_link_softc *sc;
845 ACPI_RESOURCE *resource, *end;
846 ACPI_BUFFER srsbuf;
847 ACPI_STATUS status;
848 struct link *link;
849 int i;
850
851 ACPI_SERIAL_ASSERT(pci_link);
852 sc = device_get_softc(dev);
853 if (sc->pl_crs_bad)
854 status = acpi_pci_link_srs_from_links(sc, &srsbuf);
855 else
856 status = acpi_pci_link_srs_from_crs(sc, &srsbuf);
857
858 /* Write out new resources via _SRS. */
859 status = AcpiSetCurrentResources(acpi_get_handle(dev), &srsbuf);
860 if (ACPI_FAILURE(status)) {
861 device_printf(dev, "Unable to route IRQs: %s\n",
862 AcpiFormatException(status));
863 AcpiOsFree(srsbuf.Pointer);
864 return (status);
865 }
866
867 /*
868 * Perform acpi_config_intr() on each IRQ resource if it was just
869 * routed for the first time.
870 */
871 link = sc->pl_links;
872 i = 0;
873 resource = (ACPI_RESOURCE *)srsbuf.Pointer;
874 end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length);
875 for (;;) {
876 if (resource->Id == ACPI_RSTYPE_END_TAG)
877 break;
878 switch (resource->Id) {
879 case ACPI_RSTYPE_IRQ:
880 case ACPI_RSTYPE_EXT_IRQ:
881 MPASS(i < sc->pl_num_links);
882
883 /*
884 * Only configure the interrupt and update the
885 * weights if this link has a valid IRQ and was
886 * previously unrouted.
887 */
888 if (!link->l_routed &&
889 PCI_INTERRUPT_VALID(link->l_irq)) {
890 link->l_routed = TRUE;
891 acpi_config_intr(dev, resource);
892 pci_link_interrupt_weights[link->l_irq] +=
893 link->l_references;
894 }
895 link++;
896 i++;
897 break;
898 }
899 resource = ACPI_NEXT_RESOURCE(resource);
900 if (resource >= end)
901 break;
902 }
903 AcpiOsFree(srsbuf.Pointer);
904 return (AE_OK);
905 }
906
907 static int
908 acpi_pci_link_resume(device_t dev)
909 {
910 ACPI_STATUS status;
911
912 ACPI_SERIAL_BEGIN(pci_link);
913 status = acpi_pci_link_route_irqs(dev);
914 ACPI_SERIAL_END(pci_link);
915 if (ACPI_FAILURE(status))
916 return (ENXIO);
917 else
918 return (0);
919 }
920
921 /*
922 * Pick an IRQ to use for this unrouted link.
923 */
924 static uint8_t
925 acpi_pci_link_choose_irq(device_t dev, struct link *link)
926 {
927 char tunable_buffer[64], link_name[5];
928 u_int8_t best_irq, pos_irq;
929 int best_weight, pos_weight, i;
930
931 KASSERT(!link->l_routed, ("%s: link already routed", __func__));
932 KASSERT(!PCI_INTERRUPT_VALID(link->l_irq),
933 ("%s: link already has an IRQ", __func__));
934
935 /* Check for a tunable override and use it if it is valid. */
936 if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), link_name,
937 sizeof(link_name)))) {
938 snprintf(tunable_buffer, sizeof(tunable_buffer),
939 "hw.pci.link.%s.%d.irq", link_name, link->l_res_index);
940 if (getenv_int(tunable_buffer, &i) &&
941 PCI_INTERRUPT_VALID(i) && link_valid_irq(link, i))
942 return (i);
943 snprintf(tunable_buffer, sizeof(tunable_buffer),
944 "hw.pci.link.%s.irq", link_name);
945 if (getenv_int(tunable_buffer, &i) &&
946 PCI_INTERRUPT_VALID(i) && link_valid_irq(link, i))
947 return (i);
948 }
949
950 /*
951 * If we have a valid BIOS IRQ, use that. We trust what the BIOS
952 * says it routed over what _CRS says the link thinks is routed.
953 */
954 if (PCI_INTERRUPT_VALID(link->l_bios_irq))
955 return (link->l_bios_irq);
956
957 /*
958 * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS,
959 * then use that.
960 */
961 if (PCI_INTERRUPT_VALID(link->l_initial_irq))
962 return (link->l_initial_irq);
963
964 /*
965 * Ok, we have no useful hints, so we have to pick from the
966 * possible IRQs. For ISA IRQs we only use interrupts that
967 * have already been used by the BIOS.
968 */
969 best_irq = PCI_INVALID_IRQ;
970 best_weight = INT_MAX;
971 for (i = 0; i < link->l_num_irqs; i++) {
972 pos_irq = link->l_irqs[i];
973 if (pos_irq < NUM_ISA_INTERRUPTS &&
974 (pci_link_bios_isa_irqs & 1 << pos_irq) == 0)
975 continue;
976 pos_weight = pci_link_interrupt_weights[pos_irq];
977 if (pos_weight < best_weight) {
978 best_weight = pos_weight;
979 best_irq = pos_irq;
980 }
981 }
982
983 /*
984 * If this is an ISA IRQ, try using the SCI if it is also an ISA
985 * interrupt as a fallback.
986 */
987 if (link->l_isa_irq) {
988 pos_irq = AcpiGbl_FADT->SciInt;
989 pos_weight = pci_link_interrupt_weights[pos_irq];
990 if (pos_weight < best_weight) {
991 best_weight = pos_weight;
992 best_irq = pos_irq;
993 }
994 }
995
996 if (PCI_INTERRUPT_VALID(best_irq)) {
997 if (bootverbose)
998 device_printf(dev, "Picked IRQ %u with weight %d\n",
999 best_irq, best_weight);
1000 } else
1001 device_printf(dev, "Unable to choose an IRQ\n");
1002 return (best_irq);
1003 }
1004
1005 int
1006 acpi_pci_link_route_interrupt(device_t dev, int index)
1007 {
1008 struct link *link;
1009
1010 if (acpi_disabled("pci_link"))
1011 return (PCI_INVALID_IRQ);
1012
1013 ACPI_SERIAL_BEGIN(pci_link);
1014 link = acpi_pci_link_lookup(dev, index);
1015 if (link == NULL)
1016 panic("%s: apparently invalid index %d", __func__, index);
1017
1018 /*
1019 * If this link device is already routed to an interrupt, just return
1020 * the interrupt it is routed to.
1021 */
1022 if (link->l_routed) {
1023 KASSERT(PCI_INTERRUPT_VALID(link->l_irq),
1024 ("%s: link is routed but has an invalid IRQ", __func__));
1025 ACPI_SERIAL_END(pci_link);
1026 return (link->l_irq);
1027 }
1028
1029 /* Choose an IRQ if we need one. */
1030 if (!PCI_INTERRUPT_VALID(link->l_irq)) {
1031 link->l_irq = acpi_pci_link_choose_irq(dev, link);
1032
1033 /*
1034 * Try to route the interrupt we picked. If it fails, then
1035 * assume the interrupt is not routed.
1036 */
1037 if (PCI_INTERRUPT_VALID(link->l_irq)) {
1038 acpi_pci_link_route_irqs(dev);
1039 if (!link->l_routed)
1040 link->l_irq = PCI_INVALID_IRQ;
1041 }
1042 }
1043 ACPI_SERIAL_END(pci_link);
1044
1045 return (link->l_irq);
1046 }
1047
1048 /*
1049 * This is gross, but we abuse the identify routine to perform one-time
1050 * SYSINIT() style initialization for the driver.
1051 */
1052 static void
1053 acpi_pci_link_identify(driver_t *driver, device_t parent)
1054 {
1055
1056 /*
1057 * If the SCI is an ISA IRQ, add it to the bitmask of known good
1058 * ISA IRQs.
1059 *
1060 * XXX: If we are using the APIC, the SCI might have been
1061 * rerouted to an APIC pin in which case this is invalid. However,
1062 * if we are using the APIC, we also shouldn't be having any PCI
1063 * interrupts routed via ISA IRQs, so this is probably ok.
1064 */
1065 if (AcpiGbl_FADT->SciInt < NUM_ISA_INTERRUPTS)
1066 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT->SciInt);
1067 }
1068
1069 static device_method_t acpi_pci_link_methods[] = {
1070 /* Device interface */
1071 DEVMETHOD(device_identify, acpi_pci_link_identify),
1072 DEVMETHOD(device_probe, acpi_pci_link_probe),
1073 DEVMETHOD(device_attach, acpi_pci_link_attach),
1074 DEVMETHOD(device_resume, acpi_pci_link_resume),
1075
1076 {0, 0}
1077 };
1078
1079 static driver_t acpi_pci_link_driver = {
1080 "pci_link",
1081 acpi_pci_link_methods,
1082 sizeof(struct acpi_pci_link_softc),
1083 };
1084
1085 static devclass_t pci_link_devclass;
1086
1087 DRIVER_MODULE(acpi_pci_link, acpi, acpi_pci_link_driver, pci_link_devclass, 0,
1088 0);
1089 MODULE_DEPEND(acpi_pci_link, acpi, 1, 1, 1);
Cache object: 72ede008df675e0432f414458ab37706
|