Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / dev / acpica5 / acpi_pcib.c
1 /*-
2  * Copyright (c) 2000 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/acpica/acpi_pcib.c,v 1.43 2004/05/06 02:18:58 njl Exp $
28  * $DragonFly: src/sys/dev/acpica5/acpi_pcib.c,v 1.3 2006/12/22 23:26:14 swildner Exp $
29  */
30
31 #include "opt_acpi.h"
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
36
37 #include "acpi.h"
38 #include "accommon.h"
39 #include "acpivar.h"
40 #include "acpi_pcibvar.h"
41
42 #include <bus/pci/pcivar.h>
43 #include "pcib_if.h"
44
45 /* Hooks for the ACPI CA debugging infrastructure. */
46 #define _COMPONENT      ACPI_BUS
47 ACPI_MODULE_NAME("PCI")
48
49 int
50 acpi_pcib_attach(device_t dev, ACPI_BUFFER *prt, int busno)
51 {
52     device_t                    child;
53     ACPI_STATUS                 status;
54
55     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
56
57     /*
58      * Don't attach if we're not really there.
59      *
60      * XXX: This isn't entirely correct since we may be a PCI bus
61      * on a hot-plug docking station, etc.
62      */
63     if (!acpi_DeviceIsPresent(dev))
64         return_VALUE(ENXIO);
65
66     /*
67      * Get the PCI interrupt routing table for this bus.  If we can't
68      * get it, this is not an error but may reduce functionality.
69      */
70     prt->Length = ACPI_ALLOCATE_BUFFER;
71     status = AcpiGetIrqRoutingTable(acpi_get_handle(dev), prt);
72     if (ACPI_FAILURE(status))
73         device_printf(dev,
74             "could not get PCI interrupt routing table for %s - %s\n",
75             acpi_name(acpi_get_handle(dev)), AcpiFormatException(status));
76
77     /*
78      * Attach the PCI bus proper.
79      */
80     if ((child = device_add_child(dev, "pci", busno)) == NULL) {
81         device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
82         return_VALUE(ENXIO);
83     }
84
85     /*
86      * Now go scan the bus.
87      */
88     acpi_pci_link_config(dev, prt, busno);
89
90     return_VALUE (bus_generic_attach(dev));
91 }
92
93 int
94 acpi_pcib_resume(device_t dev, ACPI_BUFFER *prt, int busno)
95 {
96     acpi_pci_link_resume(dev, prt, busno);
97     return (bus_generic_resume(dev));
98 }
99
100 /*
101  * Route an interrupt for a child of the bridge.
102  *
103  * XXX clean up error messages
104  *
105  * XXX this function is somewhat bulky
106  */
107 int
108 acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin,
109     ACPI_BUFFER *prtbuf)
110 {
111     ACPI_PCI_ROUTING_TABLE      *prt;
112     ACPI_HANDLE                 lnkdev;
113     ACPI_BUFFER                 crsbuf, prsbuf, buf;
114     ACPI_RESOURCE               *crsres, *prsres, resbuf;
115     ACPI_DEVICE_INFO            *devinfo;
116     ACPI_STATUS                 status;
117     UINT32                      InterruptCount, intr;
118     u_int8_t                    *prtp;
119     int                         interrupt;
120     int                         i;
121
122     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
123     
124     buf.Pointer = NULL;
125     crsbuf.Pointer = NULL;
126     prsbuf.Pointer = NULL;
127     interrupt = 255;
128     intr = 0;
129
130     /* ACPI numbers pins 0-3, not 1-4 like the BIOS. */
131     pin--;
132
133     /* We failed to retrieve the routing table. */
134     prtp = prtbuf->Pointer;
135     if (prtp == NULL)
136         goto out;
137
138     /* Scan the table to look for this device. */
139     for (;;) {
140         prt = (ACPI_PCI_ROUTING_TABLE *)prtp;
141
142         /* We hit the end of the table. */
143         if (prt->Length == 0)
144             goto out;
145
146         /*
147          * Compare the slot number (high word of Address) and pin number
148          * (note that ACPI uses 0 for INTA) to check for a match.
149          *
150          * Note that the low word of the Address field (function number)
151          * is required by the specification to be 0xffff.  We don't risk
152          * checking it here.
153          */
154         if (((prt->Address & 0xffff0000) >> 16) == pci_get_slot(dev) &&
155             prt->Pin == pin) {
156             if (bootverbose)
157                 device_printf(pcib, "matched entry for %d.%d.INT%c (src %s)\n",
158                               pci_get_bus(dev), pci_get_slot(dev), 'A' + pin,
159                               prt->Source);
160             break;
161         }
162         
163         /* Skip to the next entry. */
164         prtp += prt->Length;
165     }
166
167     /*
168      * If source is empty/NULL, the source index is the global IRQ number.
169      */
170     if (prt->Source == NULL || prt->Source[0] == '\0') {
171         if (bootverbose)
172             device_printf(pcib, "device is hardwired to IRQ %d\n",
173                           prt->SourceIndex);
174         interrupt = prt->SourceIndex;
175         goto out;
176     }
177     
178     /*
179      * We have to find the source device (PCI interrupt link device).
180      */
181     if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, prt->Source, &lnkdev))) {
182         device_printf(pcib, "couldn't find PCI interrupt link device %s\n",
183                       prt->Source);
184         goto out;
185     }
186
187     /*
188      * Verify that this is a PCI link device and that it's present.
189      */
190     buf.Length = ACPI_ALLOCATE_BUFFER;
191     if (ACPI_FAILURE(AcpiGetObjectInfo(lnkdev, &buf))) {
192         device_printf(pcib, "couldn't validate PCI interrupt link device %s\n",
193                       prt->Source);
194         goto out;
195     }
196     devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
197     if ((devinfo->Valid & ACPI_VALID_HID) == 0 ||
198         strcmp("PNP0C0F", devinfo->HardwareId.Value) != 0) {
199         device_printf(pcib, "PCI interrupt link %s has invalid _HID (%s)\n",
200                       prt->Source, devinfo->HardwareId.Value);
201         goto out;
202     }
203     if ((devinfo->Valid & ACPI_VALID_STA) != 0 &&
204         (devinfo->CurrentStatus & 0x9) != 0x9) {
205         device_printf(pcib, "PCI interrupt link device %s not present\n",
206                       prt->Source);
207         goto out;
208     }
209
210     /*
211      * Get the current and possible resources for the interrupt link device.
212      * If we fail to get the current resources, this is a fatal error.
213      */
214     crsbuf.Length = ACPI_ALLOCATE_BUFFER;
215     if (ACPI_FAILURE(status = AcpiGetCurrentResources(lnkdev, &crsbuf))) {
216         device_printf(pcib, "PCI interrupt link device _CRS failed - %s\n",
217                       AcpiFormatException(status));
218         goto out;
219     }
220     prsbuf.Length = ACPI_ALLOCATE_BUFFER;
221     if (ACPI_FAILURE(status = AcpiGetPossibleResources(lnkdev, &prsbuf))) {
222         device_printf(pcib, "PCI interrupt link device _PRS failed - %s\n",
223                       AcpiFormatException(status));
224     }
225     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "got %ld bytes for %s._CRS\n",
226                      (long)crsbuf.Length, acpi_name(lnkdev)));
227     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "got %ld bytes for %s._PRS\n",
228                      (long)prsbuf.Length, acpi_name(lnkdev)));
229
230     /*
231      * The interrupt may already be routed, so check _CRS first.  We don't
232      * check the 'decoding' bit in the _STA result, since there's nothing in
233      * the spec that mandates it be set, however some BIOS' will set it if
234      * the decode is active.
235      *
236      * The Source Index points to the particular resource entry we're
237      * interested in.
238      */
239     if (ACPI_FAILURE(acpi_FindIndexedResource(&crsbuf, prt->SourceIndex,
240         &crsres))) {
241         device_printf(pcib, "_CRS buffer corrupt, cannot route interrupt\n");
242         goto out;
243     }
244
245     /* Type-check the resource we've found. */
246     if (crsres->Type != ACPI_RESOURCE_TYPE_IRQ && crsres->Type != ACPI_RESOURCE_TYPE_EXTENDED_IRQ) {
247         device_printf(pcib, "_CRS resource entry has unsupported type %d\n",
248                       crsres->Type);
249         goto out;
250     }
251
252     /* Set variables based on resource type. */
253     if (crsres->Type == ACPI_RESOURCE_TYPE_IRQ) {
254         InterruptCount = crsres->Data.Irq.InterruptCount;
255         if (InterruptCount >= 1)
256             intr = crsres->Data.Irq.Interrupts[0];
257     } else {
258         InterruptCount = crsres->Data.ExtendedIrq.InterruptCount;
259         if (InterruptCount >= 1)
260             intr = crsres->Data.ExtendedIrq.Interrupts[0];
261     }
262
263     /* If there's more than one interrupt, this is an error. */
264     if (InterruptCount > 1) {
265         device_printf(pcib, "device has too many interrupts (%d)\n",
266                       InterruptCount);
267         goto out;
268     }
269
270     /* 
271      * If there's only one interrupt, and it's not zero, then it's already
272      * routed.
273      *
274      * Note that we could also check the 'decoding' bit in _STA, but can't
275      * depend on it since it's not part of the spec.
276      *
277      * XXX check ASL examples to see if this is an acceptable set of tests
278      */
279     if (InterruptCount == 1 && intr != 0) {
280         device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
281                       pci_get_slot(dev), 'A' + pin, intr);
282         interrupt = intr;
283         goto out;
284     }
285     
286     /* 
287      * There isn't an interrupt, so we have to look at _PRS to get one.
288      * Get the set of allowed interrupts from the _PRS resource indexed
289      * by SourceIndex.
290      */
291     if (prsbuf.Pointer == NULL) {
292         device_printf(pcib, "no routed irq and no _PRS on irq link device\n");
293         goto out;
294     }
295
296     /*
297      * Search through the _PRS resources, looking for an IRQ or extended
298      * IRQ resource.  Skip dependent function resources for now.  In the
299      * future, we might use these for priority but this is good enough for
300      * now until BIOS vendors actually mean something by using them.
301      */
302     prsres = NULL;
303     for (i = prt->SourceIndex; prsres == NULL; i++) {
304         if (ACPI_FAILURE(acpi_FindIndexedResource(&prsbuf, i, &prsres))) {
305             device_printf(pcib, "_PRS lacks IRQ resource, routing failed\n");
306             goto out;
307         }
308         switch (prsres->Type) {
309         case ACPI_RESOURCE_TYPE_IRQ:
310             InterruptCount = prsres->Data.Irq.InterruptCount;
311             device_printf(pcib, "possible interrupts:");
312             for (i = 0; i < InterruptCount; i++)
313                 kprintf("  %d", prsres->Data.Irq.Interrupts[i]);
314             kprintf("\n");
315             intr = prsres->Data.Irq.Interrupts[0];
316             break;
317         case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
318             InterruptCount = prsres->Data.ExtendedIrq.InterruptCount;
319             device_printf(pcib, "possible interrupts:");
320             for (i = 0; i < InterruptCount; i++)
321                 kprintf("  %d", prsres->Data.ExtendedIrq.Interrupts[i]);
322             kprintf("\n");
323             intr = prsres->Data.ExtendedIrq.Interrupts[0];
324             break;
325         case ACPI_RESOURCE_TYPE_START_DEPENDENT:
326             prsres = NULL;
327             continue;
328         default:
329             device_printf(pcib, "_PRS has invalid type %d\n", prsres->Type);
330             goto out;
331         }
332     }
333
334     /* There has to be at least one interrupt available. */
335     if (InterruptCount < 1) {
336         device_printf(pcib, "device has no interrupts\n");
337         goto out;
338     }
339
340     /*
341      * Pick an interrupt to use.  Note that a more scientific approach than
342      * just taking the first one available would be desirable.
343      *
344      * The PCI BIOS $PIR table offers "preferred PCI interrupts", but ACPI
345      * doesn't seem to offer a similar mechanism, so picking a "good"
346      * interrupt here is a difficult task.
347      *
348      * Build a resource buffer and pass it to AcpiSetCurrentResources to
349      * route the new interrupt.
350      */
351
352     /* This should never happen. */
353     if (crsbuf.Pointer != NULL)
354         AcpiOsFree(crsbuf.Pointer);
355
356     /* XXX Data.Irq and Data.ExtendedIrq are implicitly structure-copied. */
357     crsbuf.Pointer = NULL;
358     if (prsres->Type == ACPI_RESOURCE_TYPE_IRQ) {
359         resbuf.Type = ACPI_RESOURCE_TYPE_IRQ;
360         resbuf.Length = sizeof(ACPI_RESOURCE_IRQ);
361         resbuf.Data.Irq = prsres->Data.Irq;
362         resbuf.Data.Irq.InterruptCount = 1;
363         resbuf.Data.Irq.Interrupts[0] = intr;
364     } else {
365         resbuf.Type = ACPI_RESOURCE_TYPE_EXTENDED_IRQ;
366         resbuf.Length = sizeof(ACPI_RESOURCE_EXTENDED_IRQ);
367         resbuf.Data.ExtendedIrq = prsres->Data.ExtendedIrq;
368         resbuf.Data.ExtendedIrq.InterruptCount = 1;
369         resbuf.Data.ExtendedIrq.Interrupts[0] = intr;
370     }
371     if (ACPI_FAILURE(status = acpi_AppendBufferResource(&crsbuf, &resbuf))) {
372         device_printf(pcib, "buf append failed for interrupt %d via %s - %s\n",
373                       intr, acpi_name(lnkdev), AcpiFormatException(status));
374         goto out;
375     }
376     /* XXX Figure out how this is happening when the append succeeds. */
377     if (crsbuf.Pointer == NULL) {
378         device_printf(pcib, "_CRS buf NULL after append?\n");
379         goto out;
380     }
381     if (ACPI_FAILURE(status = AcpiSetCurrentResources(lnkdev, &crsbuf))) {
382         device_printf(pcib, "_SRS failed for interrupt %d via %s - %s\n",
383                       intr, acpi_name(lnkdev), AcpiFormatException(status));
384         goto out;
385     }
386     
387     /* Return the interrupt we just routed. */
388     device_printf(pcib, "slot %d INT%c routed to irq %d via %s\n", 
389                   pci_get_slot(dev), 'A' + pin, intr, acpi_name(lnkdev));
390     interrupt = intr;
391
392 out:
393     if (crsbuf.Pointer != NULL)
394         AcpiOsFree(crsbuf.Pointer);
395     if (prsbuf.Pointer != NULL)
396         AcpiOsFree(prsbuf.Pointer);
397     if (buf.Pointer != NULL)
398         AcpiOsFree(buf.Pointer);
399
400     /* XXX APIC_IO interrupt mapping? */
401     return_VALUE (interrupt);
402 }