xref: /freebsd/sys/dev/acpica/acpi_pcib.c (revision 6fd05b64b5b65dd4ba9b86482a0634a5f0b96c29)
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 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
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 <dev/acpica/acpivar.h>
39 #include <dev/acpica/acpi_pcibvar.h>
40 
41 #include <dev/pci/pcivar.h>
42 #include "pcib_if.h"
43 
44 /* Hooks for the ACPI CA debugging infrastructure. */
45 #define _COMPONENT	ACPI_BUS
46 ACPI_MODULE_NAME("PCI")
47 
48 int
49 acpi_pcib_attach(device_t dev, ACPI_BUFFER *prt, int busno)
50 {
51     device_t			child;
52     ACPI_STATUS			status;
53 
54     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
55 
56     /*
57      * Don't attach if we're not really there.
58      *
59      * XXX: This isn't entirely correct since we may be a PCI bus
60      * on a hot-plug docking station, etc.
61      */
62     if (!acpi_DeviceIsPresent(dev))
63 	return_VALUE(ENXIO);
64 
65     /*
66      * Get the PCI interrupt routing table for this bus.  If we can't
67      * get it, this is not an error but may reduce functionality.
68      */
69     prt->Length = ACPI_ALLOCATE_BUFFER;
70     status = AcpiGetIrqRoutingTable(acpi_get_handle(dev), prt);
71     if (ACPI_FAILURE(status))
72 	device_printf(dev,
73 	    "could not get PCI interrupt routing table for %s - %s\n",
74 	    acpi_name(acpi_get_handle(dev)), AcpiFormatException(status));
75 
76     /*
77      * Attach the PCI bus proper.
78      */
79     if ((child = device_add_child(dev, "pci", busno)) == NULL) {
80 	device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
81 	return_VALUE(ENXIO);
82     }
83 
84     /*
85      * Now go scan the bus.
86      */
87     acpi_pci_link_config(dev, prt, busno);
88 
89     return_VALUE (bus_generic_attach(dev));
90 }
91 
92 int
93 acpi_pcib_resume(device_t dev, ACPI_BUFFER *prt, int busno)
94 {
95     acpi_pci_link_resume(dev, prt, busno);
96     return (bus_generic_resume(dev));
97 }
98 
99 /*
100  * Route an interrupt for a child of the bridge.
101  *
102  * XXX clean up error messages
103  *
104  * XXX this function is somewhat bulky
105  */
106 int
107 acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin,
108     ACPI_BUFFER *prtbuf)
109 {
110     ACPI_PCI_ROUTING_TABLE	*prt;
111     ACPI_HANDLE			lnkdev;
112     ACPI_BUFFER			crsbuf, prsbuf, buf;
113     ACPI_RESOURCE		*crsres, *prsres, resbuf;
114     ACPI_DEVICE_INFO		*devinfo;
115     ACPI_STATUS			status;
116     UINT32			NumberOfInterrupts;
117     UINT32			*Interrupts;
118     u_int8_t			*prtp;
119     int				interrupt;
120     int				i;
121 
122     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
123 
124     crsres = NULL;
125     buf.Pointer = NULL;
126     crsbuf.Pointer = NULL;
127     prsbuf.Pointer = NULL;
128     interrupt = PCI_INVALID_IRQ;
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->Id != ACPI_RSTYPE_IRQ && crsres->Id != ACPI_RSTYPE_EXT_IRQ) {
247 	device_printf(pcib, "_CRS resource entry has unsupported type %d\n",
248 		      crsres->Id);
249 	goto out;
250     }
251 
252     /* Set variables based on resource type. */
253     if (crsres->Id == ACPI_RSTYPE_IRQ) {
254 	NumberOfInterrupts = crsres->Data.Irq.NumberOfInterrupts;
255 	Interrupts = crsres->Data.Irq.Interrupts;
256     } else {
257 	NumberOfInterrupts = crsres->Data.ExtendedIrq.NumberOfInterrupts;
258 	Interrupts = crsres->Data.ExtendedIrq.Interrupts;
259     }
260 
261     /* If there's more than one interrupt, this is an error. */
262     if (NumberOfInterrupts > 1) {
263 	device_printf(pcib, "device has too many interrupts (%d)\n",
264 		      NumberOfInterrupts);
265 	goto out;
266     }
267 
268     /*
269      * If there's only one interrupt, and it's not zero, then it's already
270      * routed.
271      *
272      * Note that we could also check the 'decoding' bit in _STA, but can't
273      * depend on it since it's not part of the spec.
274      *
275      * XXX check ASL examples to see if this is an acceptable set of tests
276      */
277     if (NumberOfInterrupts == 1 && Interrupts[0] != 0) {
278 	if (bootverbose)
279 	    device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
280 		pci_get_slot(dev), 'A' + pin, Interrupts[0]);
281 	interrupt = Interrupts[0];
282 	goto out;
283     }
284 
285     /*
286      * There isn't an interrupt, so we have to look at _PRS to get one.
287      * Get the set of allowed interrupts from the _PRS resource indexed
288      * by SourceIndex.
289      */
290     if (prsbuf.Pointer == NULL) {
291 	device_printf(pcib, "no routed irq and no _PRS on irq link device\n");
292 	goto out;
293     }
294 
295     /*
296      * Search through the _PRS resources, looking for an IRQ or extended
297      * IRQ resource.  Skip dependent function resources for now.  In the
298      * future, we might use these for priority but this is good enough for
299      * now until BIOS vendors actually mean something by using them.
300      */
301     prsres = NULL;
302     for (i = prt->SourceIndex; prsres == NULL; i++) {
303 	if (ACPI_FAILURE(acpi_FindIndexedResource(&prsbuf, i, &prsres))) {
304 	    device_printf(pcib, "_PRS lacks IRQ resource, routing failed\n");
305 	    goto out;
306 	}
307 	switch (prsres->Id) {
308 	case ACPI_RSTYPE_IRQ:
309 	    NumberOfInterrupts = prsres->Data.Irq.NumberOfInterrupts;
310 	    Interrupts = prsres->Data.Irq.Interrupts;
311 	    break;
312 	case ACPI_RSTYPE_EXT_IRQ:
313 	    NumberOfInterrupts = prsres->Data.ExtendedIrq.NumberOfInterrupts;
314 	    Interrupts = prsres->Data.ExtendedIrq.Interrupts;
315 	    break;
316 	case ACPI_RSTYPE_START_DPF:
317 	    prsres = NULL;
318 	    continue;
319 	default:
320 	    device_printf(pcib, "_PRS has invalid type %d\n", prsres->Id);
321 	    goto out;
322 	}
323     }
324 
325     /* There has to be at least one interrupt available. */
326     if (NumberOfInterrupts < 1) {
327 	device_printf(pcib, "device has no interrupts\n");
328 	goto out;
329     }
330 
331     /*
332      * Pick an interrupt to use.  Note that a more scientific approach than
333      * just taking the first one available would be desirable.
334      *
335      * The PCI BIOS $PIR table offers "preferred PCI interrupts", but ACPI
336      * doesn't seem to offer a similar mechanism, so picking a "good"
337      * interrupt here is a difficult task.
338      *
339      * Build a resource buffer and pass it to AcpiSetCurrentResources to
340      * route the new interrupt.
341      */
342     if (bootverbose) {
343 	device_printf(pcib, "possible interrupts:");
344 	for (i = 0; i < NumberOfInterrupts; i++)
345 	    printf("  %d", Interrupts[i]);
346 	printf("\n");
347     }
348 
349     /* This should never happen. */
350     if (crsbuf.Pointer != NULL)
351 	AcpiOsFree(crsbuf.Pointer);
352 
353     /* XXX Data.Irq and Data.ExtendedIrq are implicitly structure-copied. */
354     crsbuf.Pointer = NULL;
355     crsres = NULL;
356     if (prsres->Id == ACPI_RSTYPE_IRQ) {
357 	resbuf.Id = ACPI_RSTYPE_IRQ;
358 	resbuf.Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_IRQ);
359 	resbuf.Data.Irq = prsres->Data.Irq;
360 	resbuf.Data.Irq.NumberOfInterrupts = 1;
361 	resbuf.Data.Irq.Interrupts[0] = Interrupts[0];
362     } else {
363 	resbuf.Id = ACPI_RSTYPE_EXT_IRQ;
364 	resbuf.Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_EXT_IRQ);
365 	resbuf.Data.ExtendedIrq = prsres->Data.ExtendedIrq;
366 	resbuf.Data.ExtendedIrq.NumberOfInterrupts = 1;
367 	resbuf.Data.ExtendedIrq.Interrupts[0] = Interrupts[0];
368     }
369     if (ACPI_FAILURE(status = acpi_AppendBufferResource(&crsbuf, &resbuf))) {
370 	device_printf(pcib, "buf append failed for interrupt %d via %s - %s\n",
371 		      Interrupts[0], acpi_name(lnkdev),
372 		      AcpiFormatException(status));
373 	goto out;
374     }
375     /* XXX Figure out how this is happening when the append succeeds. */
376     if (crsbuf.Pointer == NULL) {
377 	device_printf(pcib, "_CRS buf NULL after append?\n");
378 	goto out;
379     }
380     if (ACPI_FAILURE(status = AcpiSetCurrentResources(lnkdev, &crsbuf))) {
381 	device_printf(pcib, "_SRS failed for interrupt %d via %s - %s\n",
382 		      Interrupts[0], acpi_name(lnkdev),
383 		      AcpiFormatException(status));
384 	goto out;
385     }
386     crsres = &resbuf;
387 
388     /* Return the interrupt we just routed. */
389     if (bootverbose)
390 	device_printf(pcib, "slot %d INT%c routed to irq %d via %s\n",
391 	    pci_get_slot(dev), 'A' + pin, Interrupts[0], acpi_name(lnkdev));
392     interrupt = Interrupts[0];
393 
394 out:
395     if (PCI_INTERRUPT_VALID(interrupt) && crsres != NULL)
396 	acpi_config_intr(dev, crsres);
397     if (crsbuf.Pointer != NULL)
398 	AcpiOsFree(crsbuf.Pointer);
399     if (prsbuf.Pointer != NULL)
400 	AcpiOsFree(prsbuf.Pointer);
401     if (buf.Pointer != NULL)
402 	AcpiOsFree(buf.Pointer);
403 
404     return_VALUE (interrupt);
405 }
406