24c9d120e97d39269d49ef12a67a8c0f5b29eab7
[dragonfly.git] / sys / dev / acpica5 / acpi_pci_link.c
1 /*
2  * Copyright (c) 2002 Mitsuru IWASAKI <iwasaki@jp.kfreebsd.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  * __FBSDID("$FreeBSD: src/sys/dev/acpica/acpi_pci_link.c,v 1.56.2.1.6.1 2009/04/15 03:14:26 kensmith Exp $");
26  */
27
28 #define MPASS(ex)               MPASS4(ex, #ex, __FILE__, __LINE__)
29 #define MPASS4(ex, what, file, line)                                    \
30         KASSERT((ex), ("Assertion %s failed at %s:%d", what, file, line))
31
32 #include <sys/cdefs.h>
33
34 #include "opt_acpi.h"
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/kernel.h>
38 #include <sys/limits.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41
42 #include "acpi.h"
43 #include <dev/acpica5/acpivar.h>
44 #include <dev/acpica5/acpi_pcibvar.h>
45
46 #include <bus/pci/i386/pci_cfgreg.h>
47 #include <bus/pci/pcireg.h>
48 #include <bus/pci/pcivar.h>
49 #include "pcib_if.h"
50
51 /* Hooks for the ACPI CA debugging infrastructure. */
52 #define _COMPONENT      ACPI_BUS
53 ACPI_MODULE_NAME("PCI_LINK")
54
55 ACPI_SERIAL_DECL(pci_link, "ACPI PCI link");
56
57 #define NUM_ISA_INTERRUPTS      16
58 #define NUM_ACPI_INTERRUPTS     256
59
60 /*
61  * An ACPI PCI link device may contain multiple links.  Each link has its
62  * own ACPI resource.  _PRT entries specify which link is being used via
63  * the Source Index.
64  *
65  * XXX: A note about Source Indices and DPFs:  Currently we assume that
66  * the DPF start and end tags are not counted towards the index that
67  * Source Index corresponds to.  Also, we assume that when DPFs are in use
68  * they various sets overlap in terms of Indices.  Here's an example
69  * resource list indicating these assumptions:
70  *
71  * Resource             Index
72  * --------             -----
73  * I/O Port             0
74  * Start DPF            -
75  * IRQ                  1
76  * MemIO                2
77  * Start DPF            -
78  * IRQ                  1
79  * MemIO                2
80  * End DPF              -
81  * DMA Channel          3
82  *
83  * The XXX is because I'm not sure if this is a valid assumption to make.
84  */
85
86 /* States during DPF processing. */
87 #define DPF_OUTSIDE     0
88 #define DPF_FIRST       1
89 #define DPF_IGNORE      2
90
91 struct link;
92
93 struct acpi_pci_link_softc {
94         int     pl_num_links;
95         int     pl_crs_bad;
96         struct link *pl_links;
97         device_t pl_dev;
98 };
99
100 struct link {
101         struct acpi_pci_link_softc *l_sc;
102         uint8_t l_bios_irq;
103         uint8_t l_irq;
104         uint8_t l_initial_irq;
105         int     l_res_index;
106         int     l_num_irqs;
107         int     *l_irqs;
108         int     l_references;
109         int     l_routed:1;
110         int     l_isa_irq:1;
111         ACPI_RESOURCE l_prs_template;
112 };
113
114 struct link_count_request {
115         int     in_dpf;
116         int     count;
117 };
118
119 struct link_res_request {
120         struct acpi_pci_link_softc *sc;
121         int     in_dpf;
122         int     res_index;
123         int     link_index;
124 };
125
126 MALLOC_DEFINE(M_PCI_LINK, "pci_link", "ACPI PCI Link structures");
127
128 static int pci_link_interrupt_weights[NUM_ACPI_INTERRUPTS];
129 static int pci_link_bios_isa_irqs;
130
131 static char *pci_link_ids[] = { "PNP0C0F", NULL };
132
133 /*
134  * Fetch the short name associated with an ACPI handle and save it in the
135  * passed in buffer.
136  */
137 static ACPI_STATUS
138 acpi_short_name(ACPI_HANDLE handle, char *buffer, size_t buflen)
139 {
140         ACPI_BUFFER buf;
141
142         buf.Length = buflen;
143         buf.Pointer = buffer;
144         return (AcpiGetName(handle, ACPI_SINGLE_NAME, &buf));
145 }
146
147 static int
148 acpi_pci_link_probe(device_t dev)
149 {
150         char descr[28], name[12];
151
152         /*
153          * We explicitly do not check _STA since not all systems set it to
154          * sensible values.
155          */
156         if (acpi_disabled("pci_link") ||
157             ACPI_ID_PROBE(device_get_parent(dev), dev, pci_link_ids) == NULL)
158                 return (ENXIO);
159
160         if (ACPI_SUCCESS(acpi_short_name(acpi_get_handle(dev), name,
161             sizeof(name)))) {
162                 ksnprintf(descr, sizeof(descr), "ACPI PCI Link %s", name);
163                 device_set_desc_copy(dev, descr);
164         } else
165                 device_set_desc(dev, "ACPI PCI Link");
166         device_quiet(dev);
167         return (0);
168 }
169
170 static ACPI_STATUS
171 acpi_count_irq_resources(ACPI_RESOURCE *res, void *context)
172 {
173         struct link_count_request *req;
174
175         req = (struct link_count_request *)context;
176         switch (res->Type) {
177         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
178                 switch (req->in_dpf) {
179                 case DPF_OUTSIDE:
180                         /* We've started the first DPF. */
181                         req->in_dpf = DPF_FIRST;
182                         break;
183                 case DPF_FIRST:
184                         /* We've started the second DPF. */
185                         req->in_dpf = DPF_IGNORE;
186                         break;
187                 }
188                 break;
189         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
190                 /* We are finished with DPF parsing. */
191                 KASSERT(req->in_dpf != DPF_OUTSIDE,
192                     ("%s: end dpf when not parsing a dpf", __func__));
193                 req->in_dpf = DPF_OUTSIDE;
194                 break;
195         case ACPI_RESOURCE_TYPE_IRQ:
196         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
197                 /*
198                  * Don't count resources if we are in a DPF set that we are
199                  * ignoring.
200                  */
201                 if (req->in_dpf != DPF_IGNORE)
202                         req->count++;
203         }
204         return (AE_OK);
205 }
206
207 static ACPI_STATUS
208 link_add_crs(ACPI_RESOURCE *res, void *context)
209 {
210         struct link_res_request *req;
211         struct link *link;
212
213         ACPI_SERIAL_ASSERT(pci_link);
214         req = (struct link_res_request *)context;
215         switch (res->Type) {
216         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
217                 switch (req->in_dpf) {
218                 case DPF_OUTSIDE:
219                         /* We've started the first DPF. */
220                         req->in_dpf = DPF_FIRST;
221                         break;
222                 case DPF_FIRST:
223                         /* We've started the second DPF. */
224                         panic(
225                 "%s: Multiple dependent functions within a current resource",
226                             __func__);
227                         break;
228                 }
229                 break;
230         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
231                 /* We are finished with DPF parsing. */
232                 KASSERT(req->in_dpf != DPF_OUTSIDE,
233                     ("%s: end dpf when not parsing a dpf", __func__));
234                 req->in_dpf = DPF_OUTSIDE;
235                 break;
236         case ACPI_RESOURCE_TYPE_IRQ:
237         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
238                 KASSERT(req->link_index < req->sc->pl_num_links,
239                     ("%s: array boundary violation", __func__));
240                 link = &req->sc->pl_links[req->link_index];
241                 link->l_res_index = req->res_index;
242                 req->link_index++;
243                 req->res_index++;
244
245                 /*
246                  * Only use the current value if there's one IRQ.  Some
247                  * systems return multiple IRQs (which is nonsense for _CRS)
248                  * when the link hasn't been programmed.
249                  */
250                 if (res->Type == ACPI_RESOURCE_TYPE_IRQ) {
251                         if (res->Data.Irq.InterruptCount == 1)
252                                 link->l_irq = res->Data.Irq.Interrupts[0];
253                 } else if (res->Data.ExtendedIrq.InterruptCount == 1)
254                         link->l_irq = res->Data.ExtendedIrq.Interrupts[0];
255
256                 /*
257                  * An IRQ of zero means that the link isn't routed.
258                  */
259                 if (link->l_irq == 0)
260                         link->l_irq = PCI_INVALID_IRQ;
261                 break;
262         default:
263                 req->res_index++;
264         }
265         return (AE_OK);
266 }
267
268 /*
269  * Populate the set of possible IRQs for each device.
270  */
271 static ACPI_STATUS
272 link_add_prs(ACPI_RESOURCE *res, void *context)
273 {
274         struct link_res_request *req;
275         struct link *link;
276         UINT8 *irqs = NULL;
277         UINT32 *ext_irqs = NULL;
278         int i, is_ext_irq = 1;
279
280         ACPI_SERIAL_ASSERT(pci_link);
281         req = (struct link_res_request *)context;
282         switch (res->Type) {
283         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
284                 switch (req->in_dpf) {
285                 case DPF_OUTSIDE:
286                         /* We've started the first DPF. */
287                         req->in_dpf = DPF_FIRST;
288                         break;
289                 case DPF_FIRST:
290                         /* We've started the second DPF. */
291                         req->in_dpf = DPF_IGNORE;
292                         break;
293                 }
294                 break;
295         case ACPI_RESOURCE_TYPE_END_DEPENDENT:
296                 /* We are finished with DPF parsing. */
297                 KASSERT(req->in_dpf != DPF_OUTSIDE,
298                     ("%s: end dpf when not parsing a dpf", __func__));
299                 req->in_dpf = DPF_OUTSIDE;
300                 break;
301         case ACPI_RESOURCE_TYPE_IRQ:
302                 is_ext_irq = 0;
303                 /* fall through */
304         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
305                 /*
306                  * Don't parse resources if we are in a DPF set that we are
307                  * ignoring.
308                  */
309                 if (req->in_dpf == DPF_IGNORE)
310                         break;
311
312                 KASSERT(req->link_index < req->sc->pl_num_links,
313                     ("%s: array boundary violation", __func__));
314                 link = &req->sc->pl_links[req->link_index];
315                 if (link->l_res_index == -1) {
316                         KASSERT(req->sc->pl_crs_bad,
317                             ("res_index should be set"));
318                         link->l_res_index = req->res_index;
319                 }
320                 req->link_index++;
321                 req->res_index++;
322
323                 /*
324                  * Stash a copy of the resource for later use when doing
325                  * _SRS.
326                  */
327                 bcopy(res, &link->l_prs_template, sizeof(ACPI_RESOURCE));
328                 if (is_ext_irq) {
329                         link->l_num_irqs =
330                             res->Data.ExtendedIrq.InterruptCount;
331                         ext_irqs = res->Data.ExtendedIrq.Interrupts;
332                 } else {
333                         link->l_num_irqs = res->Data.Irq.InterruptCount;
334                         irqs = res->Data.Irq.Interrupts;
335                 }
336                 if (link->l_num_irqs == 0)
337                         break;
338
339                 /*
340                  * Save a list of the valid IRQs.  Also, if all of the
341                  * valid IRQs are ISA IRQs, then mark this link as
342                  * routed via an ISA interrupt.
343                  */
344                 link->l_isa_irq = TRUE;
345
346                 link->l_irqs = kmalloc(sizeof(int) * link->l_num_irqs,
347                     M_PCI_LINK, M_WAITOK | M_ZERO);
348                 for (i = 0; i < link->l_num_irqs; i++) {
349                         if (is_ext_irq) {
350                                 link->l_irqs[i] = ext_irqs[i];
351                                 if (ext_irqs[i] >= NUM_ISA_INTERRUPTS)
352                                         link->l_isa_irq = FALSE;
353                         } else {
354                                 link->l_irqs[i] = irqs[i];
355                                 if (irqs[i] >= NUM_ISA_INTERRUPTS)
356                                         link->l_isa_irq = FALSE;
357                         }
358                 }
359                 break;
360         default:
361                 if (req->in_dpf == DPF_IGNORE)
362                         break;
363                 if (req->sc->pl_crs_bad)
364                         device_printf(req->sc->pl_dev,
365                     "Warning: possible resource %d will be lost during _SRS\n",
366                             req->res_index);
367                 req->res_index++;
368         }
369         return (AE_OK);
370 }
371
372 static int
373 link_valid_irq(struct link *link, int irq)
374 {
375         int i;
376
377         ACPI_SERIAL_ASSERT(pci_link);
378
379         /* Invalid interrupts are never valid. */
380         if (!PCI_INTERRUPT_VALID(irq))
381                 return (FALSE);
382
383         /* Any interrupt in the list of possible interrupts is valid. */
384         for (i = 0; i < link->l_num_irqs; i++)
385                 if (link->l_irqs[i] == irq)
386                          return (TRUE);
387
388         /*
389          * For links routed via an ISA interrupt, if the SCI is routed via
390          * an ISA interrupt, the SCI is always treated as a valid IRQ.
391          */
392         if (link->l_isa_irq && AcpiGbl_FADT.SciInterrupt == irq &&
393             irq < NUM_ISA_INTERRUPTS)
394                 return (TRUE);
395
396         /* If the interrupt wasn't found in the list it is not valid. */
397         return (FALSE);
398 }
399
400 static void
401 acpi_pci_link_dump(struct acpi_pci_link_softc *sc, int header, const char *tag)
402 {
403         struct link *link;
404         char buf[16];
405         int i, j;
406
407         ACPI_SERIAL_ASSERT(pci_link);
408         if (header) {
409                 ksnprintf(buf, sizeof(buf), "%s:",
410                     device_get_nameunit(sc->pl_dev));
411                 kprintf("%-16.16s  Index  IRQ  Rtd  Ref  IRQs\n", buf);
412         }
413         for (i = 0; i < sc->pl_num_links; i++) {
414                 link = &sc->pl_links[i];
415                 kprintf("  %-14.14s  %5d  %3d   %c   %3d ", i == 0 ? tag : "", i,
416                     link->l_irq, link->l_routed ? 'Y' : 'N',
417                     link->l_references);
418                 if (link->l_num_irqs == 0)
419                         kprintf(" none");
420                 else for (j = 0; j < link->l_num_irqs; j++)
421                         kprintf(" %d", link->l_irqs[j]);
422                 kprintf("\n");
423         }
424 }
425
426 static int
427 acpi_pci_link_attach(device_t dev)
428 {
429         struct acpi_pci_link_softc *sc;
430         struct link_count_request creq;
431         struct link_res_request rreq;
432         ACPI_STATUS status;
433         int i;
434
435         sc = device_get_softc(dev);
436         sc->pl_dev = dev;
437         ACPI_SERIAL_BEGIN(pci_link);
438
439         /*
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.
443          */
444         creq.in_dpf = DPF_OUTSIDE;
445         creq.count = 0;
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;
451                 creq.count = 0;
452                 status = AcpiWalkResources(acpi_get_handle(dev), "_PRS",
453                     acpi_count_irq_resources, &creq);
454                 if (ACPI_FAILURE(status)) {
455                         device_printf(dev,
456                             "Unable to parse _CRS or _PRS: %s\n",
457                             AcpiFormatException(status));
458                         ACPI_SERIAL_END(pci_link);
459                         return (ENXIO);
460                 }
461         }
462         sc->pl_num_links = creq.count;
463         if (creq.count == 0) {
464                 ACPI_SERIAL_END(pci_link);
465                 return (0);
466         }
467         sc->pl_links = kmalloc(sizeof(struct link) * sc->pl_num_links,
468             M_PCI_LINK, M_WAITOK | M_ZERO);
469
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;
477         }
478
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;
482                 rreq.link_index = 0;
483                 rreq.res_index = 0;
484                 rreq.sc = sc;
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));
490                         goto fail;
491                 }
492         }
493
494         /*
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.
498          */
499         rreq.in_dpf = DPF_OUTSIDE;
500         rreq.link_index = 0;
501         rreq.res_index = 0;
502         rreq.sc = sc;
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));
509                 goto fail;
510         }
511         if (bootverbose)
512                 acpi_pci_link_dump(sc, 1, "Initial Probe");
513
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;
520         if (bootverbose)
521                 acpi_pci_link_dump(sc, 0, "Validation");
522
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;
526
527         /*
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.
532          */
533         if (ACPI_SUCCESS(AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL,
534             NULL)))
535                 for (i = 0; i < sc->pl_num_links; i++)
536                         sc->pl_links[i].l_irq = PCI_INVALID_IRQ;
537         else
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;
541         if (bootverbose)
542                 acpi_pci_link_dump(sc, 0, "After Disable");
543         ACPI_SERIAL_END(pci_link);
544         return (0);
545 fail:
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);
551         return (ENXIO);
552 }
553
554 /* XXX: Note that this is identical to pci_pir_search_irq(). */
555 static uint8_t
556 acpi_pci_link_search_irq(int bus, int device, int pin)
557 {
558         uint32_t value;
559         uint8_t func, maxfunc;
560
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;
567         else
568                 maxfunc = 0;
569
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)
574                         continue;
575                 value = pci_cfgregread(bus, device, func, PCIR_INTPIN, 1);
576
577                 /*
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.
581                  */
582                 if (value != pin + 1)
583                         continue;
584                 value = pci_cfgregread(bus, device, func, PCIR_INTLINE, 1);
585                 if (bootverbose)
586                         kprintf(
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)
590                         return (value);
591         }
592         return (PCI_INVALID_IRQ);
593 }
594
595 /*
596  * Find the link structure that corresponds to the resource index passed in
597  * via 'source_index'.
598  */
599 static struct link *
600 acpi_pci_link_lookup(device_t dev, int source_index)
601 {
602         struct acpi_pci_link_softc *sc;
603         int i;
604
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]);
610         return (NULL);
611 }
612
613 void
614 acpi_pci_link_add_reference(device_t dev, int index, device_t pcib, int slot,
615     int pin)
616 {
617         struct link *link;
618         uint8_t bios_irq;
619         uintptr_t bus;
620
621         /*
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.
626          */
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");
630         }
631
632         /* Bump the reference count. */
633         ACPI_SERIAL_BEGIN(pci_link);
634         link = acpi_pci_link_lookup(dev, index);
635         if (link == NULL) {
636                 device_printf(dev, "apparently invalid index %d\n", index);
637                 ACPI_SERIAL_END(pci_link);
638                 return;
639         }
640         link->l_references++;
641         if (link->l_routed)
642                 pci_link_interrupt_weights[link->l_irq]++;
643
644         /*
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
653          * in fact.
654          *
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.
658          */
659         if (!link->l_isa_irq) {
660                 ACPI_SERIAL_END(pci_link);
661                 return;
662         }
663
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);
668                 return;
669         }
670
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))
681                         device_printf(dev,
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)
685                 device_printf(dev,
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',
688                     link->l_bios_irq);
689         ACPI_SERIAL_END(pci_link);
690 }
691
692 static ACPI_STATUS
693 acpi_pci_link_srs_from_crs(struct acpi_pci_link_softc *sc, ACPI_BUFFER *srsbuf)
694 {
695         ACPI_RESOURCE *resource, *end, newres, *resptr;
696         ACPI_BUFFER crsbuf;
697         ACPI_STATUS status;
698         struct link *link;
699         int i, in_dpf;
700
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)) {
709                 if (bootverbose)
710                         device_printf(sc->pl_dev,
711                             "Unable to fetch current resources: %s\n",
712                             AcpiFormatException(status));
713                 return (status);
714         }
715
716         /* Fill in IRQ resources via link structures. */
717         srsbuf->Pointer = NULL;
718         link = sc->pl_links;
719         i = 0;
720         in_dpf = DPF_OUTSIDE;
721         resource = (ACPI_RESOURCE *)crsbuf.Pointer;
722         end = (ACPI_RESOURCE *)((char *)crsbuf.Pointer + crsbuf.Length);
723         for (;;) {
724                 switch (resource->Type) {
725                 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
726                         switch (in_dpf) {
727                         case DPF_OUTSIDE:
728                                 /* We've started the first DPF. */
729                                 in_dpf = DPF_FIRST;
730                                 break;
731                         case DPF_FIRST:
732                                 /* We've started the second DPF. */
733                                 panic(
734                 "%s: Multiple dependent functions within a current resource",
735                                     __func__);
736                                 break;
737                         }
738                         resptr = NULL;
739                         break;
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;
745                         resptr = NULL;
746                         break;
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;
751                         resptr = &newres;
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;
758                         } else
759                                 resptr->Data.Irq.Interrupts[0] = 0;
760                         link++;
761                         i++;
762                         break;
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;
767                         resptr = &newres;
768                         resptr->Data.ExtendedIrq.InterruptCount = 1;
769                         if (PCI_INTERRUPT_VALID(link->l_irq))
770                                 resptr->Data.ExtendedIrq.Interrupts[0] =
771                                     link->l_irq;
772                         else
773                                 resptr->Data.ExtendedIrq.Interrupts[0] = 0;
774                         link++;
775                         i++;
776                         break;
777                 default:
778                         resptr = resource;
779                 }
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);
789                                 return (status);
790                         }
791                 }
792                 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
793                         break;
794                 resource = ACPI_NEXT_RESOURCE(resource);
795                 if (resource >= end)
796                         break;
797         }
798         AcpiOsFree(crsbuf.Pointer);
799         return (AE_OK);
800 }
801
802 static ACPI_STATUS
803 acpi_pci_link_srs_from_links(struct acpi_pci_link_softc *sc,
804     ACPI_BUFFER *srsbuf)
805 {
806         ACPI_RESOURCE newres;
807         ACPI_STATUS status;
808         struct link *link;
809         int i;
810
811         /* Start off with an empty buffer. */
812         srsbuf->Pointer = NULL;
813         link = sc->pl_links;
814         for (i = 0; i < sc->pl_num_links; i++) {
815
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) {
820
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;
828                         } else
829                                 newres.Data.Irq.Interrupts[0] = 0;
830                 } else {
831
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] =
836                                     link->l_irq;
837                         else
838                                 newres.Data.ExtendedIrq.Interrupts[0] = 0;
839                 }
840
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);
849                         return (status);
850                 }
851         }
852         return (AE_OK);
853 }
854
855 static ACPI_STATUS
856 acpi_pci_link_route_irqs(device_t dev)
857 {
858         struct acpi_pci_link_softc *sc;
859         ACPI_RESOURCE *resource, *end;
860         ACPI_BUFFER srsbuf;
861         ACPI_STATUS status;
862         struct link *link;
863         int i;
864
865         ACPI_SERIAL_ASSERT(pci_link);
866         sc = device_get_softc(dev);
867         if (sc->pl_crs_bad)
868                 status = acpi_pci_link_srs_from_links(sc, &srsbuf);
869         else
870                 status = acpi_pci_link_srs_from_crs(sc, &srsbuf);
871
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);
878                 return (status);
879         }
880
881         /*
882          * Perform acpi_config_intr() on each IRQ resource if it was just
883          * routed for the first time.
884          */
885         link = sc->pl_links;
886         i = 0;
887         resource = (ACPI_RESOURCE *)srsbuf.Pointer;
888         end = (ACPI_RESOURCE *)((char *)srsbuf.Pointer + srsbuf.Length);
889         for (;;) {
890                 if (resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
891                         break;
892                 switch (resource->Type) {
893                 case ACPI_RESOURCE_TYPE_IRQ:
894                 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
895                         MPASS(i < sc->pl_num_links);
896
897                         /*
898                          * Only configure the interrupt and update the
899                          * weights if this link has a valid IRQ and was
900                          * previously unrouted.
901                          */
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] +=
907                                     link->l_references;
908                         }
909                         link++;
910                         i++;
911                         break;
912                 }
913                 resource = ACPI_NEXT_RESOURCE(resource);
914                 if (resource >= end)
915                         break;
916         }
917         AcpiOsFree(srsbuf.Pointer);
918         return (AE_OK);
919 }
920
921 static int
922 acpi_pci_link_resume(device_t dev)
923 {
924         struct acpi_pci_link_softc *sc;
925         ACPI_STATUS status;
926         int i, routed;
927
928         /*
929          * If all of our links are routed, then restore the link via _SRS,
930          * otherwise, disable the link via _DIS.
931          */
932         ACPI_SERIAL_BEGIN(pci_link);
933         sc = device_get_softc(dev);
934         routed = 0;
935         for (i = 0; i < sc->pl_num_links; i++)
936                 if (sc->pl_links[i].l_routed)
937                         routed++;
938         if (routed == sc->pl_num_links)
939                 status = acpi_pci_link_route_irqs(dev);
940         else {
941                 AcpiEvaluateObject(acpi_get_handle(dev), "_DIS", NULL, NULL);
942                 status = AE_OK;
943         }
944         ACPI_SERIAL_END(pci_link);
945         if (ACPI_FAILURE(status))
946                 return (ENXIO);
947         else
948                 return (0);
949 }
950
951 /*
952  * Pick an IRQ to use for this unrouted link.
953  */
954 static uint8_t
955 acpi_pci_link_choose_irq(device_t dev, struct link *link)
956 {
957         char tunable_buffer[64], link_name[5];
958         u_int8_t best_irq, pos_irq;
959         int best_weight, pos_weight, i;
960
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__));
964
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))
972                                 device_printf(dev,
973                                     "Warning, IRQ %d is not listed as valid\n",
974                                     i);
975                         return (i);
976                 }
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))
981                                 device_printf(dev,
982                                     "Warning, IRQ %d is not listed as valid\n",
983                                     i);
984                         return (i);
985                 }
986         }
987
988         /*
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.
991          */
992         if (PCI_INTERRUPT_VALID(link->l_bios_irq))
993                 return (link->l_bios_irq);
994
995         /*
996          * If we don't have a BIOS IRQ but do have a valid IRQ from _CRS,
997          * then use that.
998          */
999         if (PCI_INTERRUPT_VALID(link->l_initial_irq))
1000                 return (link->l_initial_irq);
1001
1002         /*
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.
1006          */
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)
1013                         continue;
1014                 pos_weight = pci_link_interrupt_weights[pos_irq];
1015                 if (pos_weight < best_weight) {
1016                         best_weight = pos_weight;
1017                         best_irq = pos_irq;
1018                 }
1019         }
1020
1021         /*
1022          * If this is an ISA IRQ, try using the SCI if it is also an ISA
1023          * interrupt as a fallback.
1024          */
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;
1030                         best_irq = pos_irq;
1031                 }
1032         }
1033
1034         if (PCI_INTERRUPT_VALID(best_irq)) {
1035                 if (bootverbose)
1036                         device_printf(dev, "Picked IRQ %u with weight %d\n",
1037                             best_irq, best_weight);
1038         } else
1039                 device_printf(dev, "Unable to choose an IRQ\n");
1040         return (best_irq);
1041 }
1042
1043 int
1044 acpi_pci_link_route_interrupt(device_t dev, int index)
1045 {
1046         struct link *link;
1047
1048         if (acpi_disabled("pci_link"))
1049                 return (PCI_INVALID_IRQ);
1050
1051         ACPI_SERIAL_BEGIN(pci_link);
1052         link = acpi_pci_link_lookup(dev, index);
1053         if (link == NULL)
1054                 panic("%s: apparently invalid index %d", __func__, index);
1055
1056         /*
1057          * If this link device is already routed to an interrupt, just return
1058          * the interrupt it is routed to.
1059          */
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);
1065         }
1066
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);
1070
1071                 /*
1072                  * Try to route the interrupt we picked.  If it fails, then
1073                  * assume the interrupt is not routed.
1074                  */
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;
1079                 }
1080         }
1081         ACPI_SERIAL_END(pci_link);
1082         return (link->l_irq);
1083 }
1084
1085 /*
1086  * This is gross, but we abuse the identify routine to perform one-time
1087  * SYSINIT() style initialization for the driver.
1088  */
1089 static void
1090 acpi_pci_link_identify(driver_t *driver, device_t parent)
1091 {
1092
1093         /*
1094          * If the SCI is an ISA IRQ, add it to the bitmask of known good
1095          * ISA IRQs.
1096          *
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.
1101          */
1102         if (AcpiGbl_FADT.SciInterrupt < NUM_ISA_INTERRUPTS)
1103                 pci_link_bios_isa_irqs |= (1 << AcpiGbl_FADT.SciInterrupt);
1104 }
1105
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),
1112
1113         {0, 0}
1114 };
1115
1116 static driver_t acpi_pci_link_driver = {
1117         "pci_link",
1118         acpi_pci_link_methods,
1119         sizeof(struct acpi_pci_link_softc),
1120 };
1121
1122 static devclass_t pci_link_devclass;
1123
1124 DRIVER_MODULE(acpi_pci_link, acpi, acpi_pci_link_driver, pci_link_devclass, 0,
1125     0);
1126 MODULE_DEPEND(acpi_pci_link, acpi, 1, 1, 1);