xref: /freebsd/sys/dev/acpica/acpi_pcib.c (revision e0c27215058b5786c78fcfb3963eebe61a989511)
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$
28  */
29 #include "opt_acpi.h"
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/malloc.h>
33 #include <sys/kernel.h>
34 
35 #include "acpi.h"
36 
37 #include <dev/acpica/acpivar.h>
38 #include <dev/acpica/acpi_pcibvar.h>
39 
40 #include <machine/pci_cfgreg.h>
41 #include <pci/pcivar.h>
42 #include <pci/pcib_private.h>
43 #include "pcib_if.h"
44 
45 /*
46  * Hooks for the ACPI CA debugging infrastructure
47  */
48 #define _COMPONENT	ACPI_BUS
49 ACPI_MODULE_NAME("PCI")
50 
51 int
52 acpi_pcib_attach(device_t dev, ACPI_BUFFER *prt, int busno)
53 {
54     device_t			child;
55     ACPI_STATUS			status;
56 
57     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
58 
59     /*
60      * Don't attach if we're not really there.
61      *
62      * XXX: This isn't entirely correct since we may be a PCI bus
63      * on a hot-plug docking station, etc.
64      */
65     if (!acpi_DeviceIsPresent(dev))
66 	return_VALUE(ENXIO);
67 
68     /*
69      * Get the PCI interrupt routing table for this bus.
70      */
71     prt->Length = ACPI_ALLOCATE_BUFFER;
72     status = AcpiGetIrqRoutingTable(acpi_get_handle(dev), prt);
73     if (ACPI_FAILURE(status))
74 	/* This is not an error, but it may reduce functionality. */
75 	device_printf(dev,
76 	    "could not get PCI interrupt routing table for %s - %s\n",
77 	    acpi_name(acpi_get_handle(dev)), AcpiFormatException(status));
78 
79     /*
80      * Attach the PCI bus proper.
81      */
82     if ((child = device_add_child(dev, "pci", busno)) == NULL) {
83 	device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
84 	return_VALUE(ENXIO);
85     }
86 
87     /*
88      * Now go scan the bus.
89      */
90     acpi_pci_link_config(dev, prt, busno);
91     return_VALUE(bus_generic_attach(dev));
92 }
93 
94 int
95 acpi_pcib_resume(device_t dev, ACPI_BUFFER *prt, int busno)
96 {
97     acpi_pci_link_resume(dev, prt, busno);
98     return (bus_generic_resume(dev));
99 }
100 
101 /*
102  * Route an interrupt for a child of the bridge.
103  *
104  * XXX clean up error messages
105  *
106  * XXX this function is somewhat bulky
107  */
108 int
109 acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin,
110     ACPI_BUFFER *prtbuf)
111 {
112     ACPI_PCI_ROUTING_TABLE	*prt;
113     ACPI_HANDLE			lnkdev;
114     ACPI_BUFFER			crsbuf, prsbuf;
115     ACPI_RESOURCE		*crsres, *prsres, resbuf;
116     ACPI_DEVICE_INFO		devinfo;
117     ACPI_BUFFER			buf = {sizeof(devinfo), &devinfo};
118     ACPI_STATUS			status;
119     u_int8_t			*prtp;
120     int				interrupt;
121     int				i;
122 
123     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
124 
125     crsbuf.Pointer = NULL;
126     prsbuf.Pointer = NULL;
127     interrupt = 255;
128 
129     /* ACPI numbers pins 0-3, not 1-4 like the BIOS */
130     pin--;
131 
132     prtp = prtbuf->Pointer;
133     if (prtp == NULL)			/* didn't get routing table */
134 	goto out;
135 
136     /* scan the table looking for this device */
137     for (;;) {
138 	prt = (ACPI_PCI_ROUTING_TABLE *)prtp;
139 
140 	if (prt->Length == 0)		/* end of table */
141 	    goto out;
142 
143 	/*
144 	 * Compare the slot number (high word of Address) and pin number
145 	 * (note that ACPI uses 0 for INTA) to check for a match.
146 	 *
147 	 * Note that the low word of the Address field (function number)
148 	 * is required by the specification to be 0xffff.  We don't risk
149 	 * checking it here.
150 	 */
151 	if ((((prt->Address & 0xffff0000) >> 16) == pci_get_slot(dev)) &&
152 	    (prt->Pin == pin)) {
153 	    if (bootverbose)
154 		device_printf(pcib, "matched entry for %d.%d.INT%c (source %s)\n",
155 			      pci_get_bus(dev), pci_get_slot(dev), 'A' + pin, prt->Source);
156 	    break;
157 	}
158 
159 	/* skip to next entry */
160 	prtp += prt->Length;
161     }
162 
163     /*
164      * If source is empty/NULL, the source index is the global IRQ number.
165      */
166     if ((prt->Source == NULL) || (prt->Source[0] == '\0')) {
167 	if (bootverbose)
168 	    device_printf(pcib, "device is hardwired to IRQ %d\n",
169 		prt->SourceIndex);
170 	interrupt = prt->SourceIndex;
171 	goto out;
172     }
173 
174     /*
175      * We have to find the source device (PCI interrupt link device)
176      */
177     if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, prt->Source, &lnkdev))) {
178 	device_printf(pcib, "couldn't find PCI interrupt link device %s\n",
179 	    prt->Source);
180 	goto out;
181     }
182 
183     /*
184      * Verify that this is a PCI link device, and that it's present.
185      */
186     if (ACPI_FAILURE(AcpiGetObjectInfo(lnkdev, &buf))) {
187 	device_printf(pcib, "couldn't validate PCI interrupt link device %s\n",
188 	    prt->Source);
189 	goto out;
190     }
191     if (!(devinfo.Valid & ACPI_VALID_HID) || strcmp("PNP0C0F", devinfo.HardwareId.Value)) {
192 	device_printf(pcib, "PCI interrupt link device %s has wrong _HID (%s)\n",
193 		      prt->Source, devinfo.HardwareId.Value);
194 	goto out;
195     }
196     if (devinfo.Valid & ACPI_VALID_STA && (devinfo.CurrentStatus & 0x9) != 0x9) {
197 	device_printf(pcib, "PCI interrupt link device %s not present\n",
198 		      prt->Source);
199 	goto out;
200     }
201 
202     /*
203      * Get the current and possible resources for the interrupt link device.
204      */
205     crsbuf.Length = ACPI_ALLOCATE_BUFFER;
206     if (ACPI_FAILURE(status = AcpiGetCurrentResources(lnkdev, &crsbuf))) {
207 	device_printf(pcib, "couldn't get PCI interrupt link device _CRS data - %s\n",
208 		      AcpiFormatException(status));
209 	goto out;	/* this is fatal */
210     }
211     prsbuf.Length = ACPI_ALLOCATE_BUFFER;
212     if (ACPI_FAILURE(status = AcpiGetPossibleResources(lnkdev, &prsbuf))) {
213 	device_printf(pcib, "couldn't get PCI interrupt link device _PRS data - %s\n",
214 		      AcpiFormatException(status));
215 	/* this is not fatal, since it may be hardwired */
216     }
217     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "got %ld bytes for %s._CRS\n",
218 	(long)crsbuf.Length, acpi_name(lnkdev)));
219     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "got %ld bytes for %s._PRS\n",
220 	(long)prsbuf.Length, acpi_name(lnkdev)));
221 
222     /*
223      * The interrupt may already be routed, so check _CRS first.  We don't check the
224      * 'decoding' bit in the _STA result, since there's nothing in the spec that
225      * mandates it be set, however some BIOS' will set it if the decode is active.
226      *
227      * The Source Index points to the particular resource entry we're interested in.
228      */
229     if (ACPI_FAILURE(acpi_FindIndexedResource(&crsbuf, prt->SourceIndex, &crsres))) {
230 	device_printf(pcib, "_CRS buffer corrupt, cannot route interrupt\n");
231 	goto out;
232     }
233 
234     /* type-check the resource we've got */
235     if (crsres->Id != ACPI_RSTYPE_IRQ) {    /* XXX ACPI_RSTYPE_EXT_IRQ */
236 	device_printf(pcib, "_CRS resource entry has unsupported type %d\n",
237 	    crsres->Id);
238 	goto out;
239     }
240 
241     /* if there's more than one interrupt, we are confused */
242     if (crsres->Data.Irq.NumberOfInterrupts > 1) {
243 	device_printf(pcib, "device has too many interrupts (%d)\n",
244 	    crsres->Data.Irq.NumberOfInterrupts);
245 	goto out;
246     }
247 
248     /*
249      * If there's only one interrupt, and it's not zero, then we're already routed.
250      *
251      * Note that we could also check the 'decoding' bit in _STA, but can't depend on
252      * it since it's not part of the spec.
253      *
254      * XXX check ASL examples to see if this is an acceptable set of tests
255      */
256     if ((crsres->Data.Irq.NumberOfInterrupts == 1) && (crsres->Data.Irq.Interrupts[0] != 0)) {
257 	device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
258 	    pci_get_slot(dev), 'A' + pin, crsres->Data.Irq.Interrupts[0]);
259 	interrupt = crsres->Data.Irq.Interrupts[0];
260 	goto out;
261     }
262 
263     /*
264      * There isn't an interrupt, so we have to look at _PRS to get one.
265      * Get the set of allowed interrupts from the _PRS resource indexed by SourceIndex.
266      */
267     if (prsbuf.Pointer == NULL) {
268 	device_printf(pcib, "device has no routed interrupt and no _PRS on PCI interrupt link device\n");
269 	goto out;
270     }
271     if (ACPI_FAILURE(acpi_FindIndexedResource(&prsbuf, prt->SourceIndex, &prsres))) {
272 	device_printf(pcib, "_PRS buffer corrupt, cannot route interrupt\n");
273 	goto out;
274     }
275 
276     /* type-check the resource we've got */
277     if (prsres->Id != ACPI_RSTYPE_IRQ) {    /* XXX ACPI_RSTYPE_EXT_IRQ */
278 	device_printf(pcib, "_PRS resource entry has unsupported type %d\n",
279 	    prsres->Id);
280 	goto out;
281     }
282 
283     /* there has to be at least one interrupt available */
284     if (prsres->Data.Irq.NumberOfInterrupts < 1) {
285 	device_printf(pcib, "device has no interrupts\n");
286 	goto out;
287     }
288 
289     /*
290      * Pick an interrupt to use.  Note that a more scientific approach than just
291      * taking the first one available would be desirable.
292      *
293      * The PCI BIOS $PIR table offers "preferred PCI interrupts", but ACPI doesn't
294      * seem to offer a similar mechanism, so picking a "good" interrupt here is a
295      * difficult task.
296      *
297      * Build a resource buffer and pass it to AcpiSetCurrentResources to route the
298      * new interrupt.
299      */
300     device_printf(pcib, "possible interrupts:");
301     for (i = 0; i < prsres->Data.Irq.NumberOfInterrupts; i++)
302 	printf("  %d", prsres->Data.Irq.Interrupts[i]);
303     printf("\n");
304 
305     if (crsbuf.Pointer != NULL)			/* should never happen */
306 	AcpiOsFree(crsbuf.Pointer);
307     crsbuf.Pointer = NULL;
308     resbuf.Id = ACPI_RSTYPE_IRQ;
309     resbuf.Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_IRQ);
310     resbuf.Data.Irq = prsres->Data.Irq;		/* structure copy other fields */
311     resbuf.Data.Irq.NumberOfInterrupts = 1;
312     resbuf.Data.Irq.Interrupts[0] = prsres->Data.Irq.Interrupts[0];	/* just take first... */
313     if (ACPI_FAILURE(status = acpi_AppendBufferResource(&crsbuf, &resbuf))) {
314 	device_printf(pcib, "couldn't route interrupt %d via %s, interrupt resource build failed - %s\n",
315 		      prsres->Data.Irq.Interrupts[0], acpi_name(lnkdev), AcpiFormatException(status));
316 	goto out;
317     }
318     if (ACPI_FAILURE(status = AcpiSetCurrentResources(lnkdev, &crsbuf))) {
319 	device_printf(pcib, "couldn't route interrupt %d via %s - %s\n",
320 		      prsres->Data.Irq.Interrupts[0], acpi_name(lnkdev), AcpiFormatException(status));
321 	goto out;
322     }
323 
324     /* successful, return the interrupt we just routed */
325     device_printf(pcib, "slot %d INT%c routed to irq %d via %s\n",
326 	pci_get_slot(dev), 'A' + pin, prsres->Data.Irq.Interrupts[0],
327 	acpi_name(lnkdev));
328     interrupt = prsres->Data.Irq.Interrupts[0];
329 
330  out:
331     if (crsbuf.Pointer != NULL)
332 	AcpiOsFree(crsbuf.Pointer);
333     if (prsbuf.Pointer != NULL)
334 	AcpiOsFree(prsbuf.Pointer);
335 
336     /* XXX APIC_IO interrupt mapping? */
337     return_VALUE(interrupt);
338 }
339 
340