2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
38 #include <dev/acpica/acpivar.h>
39 #include <dev/acpica/acpi_pcibvar.h>
41 #include <machine/pci_cfgreg.h>
42 #include <dev/pci/pcivar.h>
43 #include <dev/pci/pcib_private.h>
46 /* Hooks for the ACPI CA debugging infrastructure. */
47 #define _COMPONENT ACPI_BUS
48 ACPI_MODULE_NAME("PCI")
51 acpi_pcib_attach(device_t dev, ACPI_BUFFER *prt, int busno)
56 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
59 * Don't attach if we're not really there.
61 * XXX: This isn't entirely correct since we may be a PCI bus
62 * on a hot-plug docking station, etc.
64 if (!acpi_DeviceIsPresent(dev))
68 * Get the PCI interrupt routing table for this bus. If we can't
69 * get it, this is not an error but may reduce functionality.
71 prt->Length = ACPI_ALLOCATE_BUFFER;
72 status = AcpiGetIrqRoutingTable(acpi_get_handle(dev), prt);
73 if (ACPI_FAILURE(status))
75 "could not get PCI interrupt routing table for %s - %s\n",
76 acpi_name(acpi_get_handle(dev)), AcpiFormatException(status));
79 * Attach the PCI bus proper.
81 if ((child = device_add_child(dev, "pci", busno)) == NULL) {
82 device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
87 * Now go scan the bus.
89 acpi_pci_link_config(dev, prt, busno);
91 return_VALUE (bus_generic_attach(dev));
95 acpi_pcib_resume(device_t dev, ACPI_BUFFER *prt, int busno)
97 acpi_pci_link_resume(dev, prt, busno);
98 return (bus_generic_resume(dev));
102 * Route an interrupt for a child of the bridge.
104 * XXX clean up error messages
106 * XXX this function is somewhat bulky
109 acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin,
112 ACPI_PCI_ROUTING_TABLE *prt;
114 ACPI_BUFFER crsbuf, prsbuf, buf;
115 ACPI_RESOURCE *crsres, *prsres, resbuf;
116 ACPI_DEVICE_INFO *devinfo;
118 UINT32 NumberOfInterrupts;
124 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
127 crsbuf.Pointer = NULL;
128 prsbuf.Pointer = NULL;
131 /* ACPI numbers pins 0-3, not 1-4 like the BIOS. */
134 /* We failed to retrieve the routing table. */
135 prtp = prtbuf->Pointer;
139 /* Scan the table to look for this device. */
141 prt = (ACPI_PCI_ROUTING_TABLE *)prtp;
143 /* We hit the end of the table. */
144 if (prt->Length == 0)
148 * Compare the slot number (high word of Address) and pin number
149 * (note that ACPI uses 0 for INTA) to check for a match.
151 * Note that the low word of the Address field (function number)
152 * is required by the specification to be 0xffff. We don't risk
155 if (((prt->Address & 0xffff0000) >> 16) == pci_get_slot(dev) &&
158 device_printf(pcib, "matched entry for %d.%d.INT%c (src %s)\n",
159 pci_get_bus(dev), pci_get_slot(dev), 'A' + pin,
164 /* Skip to the next entry. */
169 * If source is empty/NULL, the source index is the global IRQ number.
171 if (prt->Source == NULL || prt->Source[0] == '\0') {
173 device_printf(pcib, "device is hardwired to IRQ %d\n",
175 interrupt = prt->SourceIndex;
180 * We have to find the source device (PCI interrupt link device).
182 if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, prt->Source, &lnkdev))) {
183 device_printf(pcib, "couldn't find PCI interrupt link device %s\n",
189 * Verify that this is a PCI link device and that it's present.
191 buf.Length = ACPI_ALLOCATE_BUFFER;
192 if (ACPI_FAILURE(AcpiGetObjectInfo(lnkdev, &buf))) {
193 device_printf(pcib, "couldn't validate PCI interrupt link device %s\n",
197 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
198 if ((devinfo->Valid & ACPI_VALID_HID) == 0 ||
199 strcmp("PNP0C0F", devinfo->HardwareId.Value) != 0) {
200 device_printf(pcib, "PCI interrupt link %s has invalid _HID (%s)\n",
201 prt->Source, devinfo->HardwareId.Value);
204 if ((devinfo->Valid & ACPI_VALID_STA) != 0 &&
205 (devinfo->CurrentStatus & 0x9) != 0x9) {
206 device_printf(pcib, "PCI interrupt link device %s not present\n",
212 * Get the current and possible resources for the interrupt link device.
213 * If we fail to get the current resources, this is a fatal error.
215 crsbuf.Length = ACPI_ALLOCATE_BUFFER;
216 if (ACPI_FAILURE(status = AcpiGetCurrentResources(lnkdev, &crsbuf))) {
217 device_printf(pcib, "PCI interrupt link device _CRS failed - %s\n",
218 AcpiFormatException(status));
221 prsbuf.Length = ACPI_ALLOCATE_BUFFER;
222 if (ACPI_FAILURE(status = AcpiGetPossibleResources(lnkdev, &prsbuf))) {
223 device_printf(pcib, "PCI interrupt link device _PRS failed - %s\n",
224 AcpiFormatException(status));
226 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "got %ld bytes for %s._CRS\n",
227 (long)crsbuf.Length, acpi_name(lnkdev)));
228 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "got %ld bytes for %s._PRS\n",
229 (long)prsbuf.Length, acpi_name(lnkdev)));
232 * The interrupt may already be routed, so check _CRS first. We don't
233 * check the 'decoding' bit in the _STA result, since there's nothing in
234 * the spec that mandates it be set, however some BIOS' will set it if
235 * the decode is active.
237 * The Source Index points to the particular resource entry we're
240 if (ACPI_FAILURE(acpi_FindIndexedResource(&crsbuf, prt->SourceIndex,
242 device_printf(pcib, "_CRS buffer corrupt, cannot route interrupt\n");
246 /* Type-check the resource we've found. */
247 if (crsres->Id != ACPI_RSTYPE_IRQ && crsres->Id != ACPI_RSTYPE_EXT_IRQ) {
248 device_printf(pcib, "_CRS resource entry has unsupported type %d\n",
253 /* Set variables based on resource type. */
254 if (crsres->Id == ACPI_RSTYPE_IRQ) {
255 NumberOfInterrupts = crsres->Data.Irq.NumberOfInterrupts;
256 Interrupts = crsres->Data.Irq.Interrupts;
258 NumberOfInterrupts = crsres->Data.ExtendedIrq.NumberOfInterrupts;
259 Interrupts = crsres->Data.ExtendedIrq.Interrupts;
262 /* If there's more than one interrupt, this is an error. */
263 if (NumberOfInterrupts > 1) {
264 device_printf(pcib, "device has too many interrupts (%d)\n",
270 * If there's only one interrupt, and it's not zero, then it's already
273 * Note that we could also check the 'decoding' bit in _STA, but can't
274 * depend on it since it's not part of the spec.
276 * XXX check ASL examples to see if this is an acceptable set of tests
278 if (NumberOfInterrupts == 1 && Interrupts[0] != 0) {
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];
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
290 if (prsbuf.Pointer == NULL) {
291 device_printf(pcib, "no routed irq and no _PRS on irq link device\n");
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.
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");
307 switch (prsres->Id) {
308 case ACPI_RSTYPE_IRQ:
309 NumberOfInterrupts = prsres->Data.Irq.NumberOfInterrupts;
310 Interrupts = prsres->Data.Irq.Interrupts;
312 case ACPI_RSTYPE_EXT_IRQ:
313 NumberOfInterrupts = prsres->Data.ExtendedIrq.NumberOfInterrupts;
314 Interrupts = prsres->Data.ExtendedIrq.Interrupts;
316 case ACPI_RSTYPE_START_DPF:
320 device_printf(pcib, "_PRS has invalid type %d\n", prsres->Id);
325 /* There has to be at least one interrupt available. */
326 if (NumberOfInterrupts < 1) {
327 device_printf(pcib, "device has no interrupts\n");
332 * Pick an interrupt to use. Note that a more scientific approach than
333 * just taking the first one available would be desirable.
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.
339 * Build a resource buffer and pass it to AcpiSetCurrentResources to
340 * route the new interrupt.
342 device_printf(pcib, "possible interrupts:");
343 for (i = 0; i < NumberOfInterrupts; i++)
344 printf(" %d", Interrupts[i]);
347 /* This should never happen. */
348 if (crsbuf.Pointer != NULL)
349 AcpiOsFree(crsbuf.Pointer);
351 /* XXX Data.Irq and Data.ExtendedIrq are implicitly structure-copied. */
352 crsbuf.Pointer = NULL;
353 if (prsres->Id == ACPI_RSTYPE_IRQ) {
354 resbuf.Id = ACPI_RSTYPE_IRQ;
355 resbuf.Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_IRQ);
356 resbuf.Data.Irq = prsres->Data.Irq;
357 resbuf.Data.Irq.NumberOfInterrupts = 1;
358 resbuf.Data.Irq.Interrupts[0] = Interrupts[0];
360 resbuf.Id = ACPI_RSTYPE_EXT_IRQ;
361 resbuf.Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_EXT_IRQ);
362 resbuf.Data.ExtendedIrq = prsres->Data.ExtendedIrq;
363 resbuf.Data.ExtendedIrq.NumberOfInterrupts = 1;
364 resbuf.Data.ExtendedIrq.Interrupts[0] = Interrupts[0];
366 if (ACPI_FAILURE(status = acpi_AppendBufferResource(&crsbuf, &resbuf))) {
367 device_printf(pcib, "buf append failed for interrupt %d via %s - %s\n",
368 Interrupts[0], acpi_name(lnkdev),
369 AcpiFormatException(status));
372 /* XXX Figure out how this is happening when the append succeeds. */
373 if (crsbuf.Pointer == NULL) {
374 device_printf(pcib, "_CRS buf NULL after append?\n");
377 if (ACPI_FAILURE(status = AcpiSetCurrentResources(lnkdev, &crsbuf))) {
378 device_printf(pcib, "_SRS failed for interrupt %d via %s - %s\n",
379 Interrupts[0], acpi_name(lnkdev),
380 AcpiFormatException(status));
384 /* Return the interrupt we just routed. */
385 device_printf(pcib, "slot %d INT%c routed to irq %d via %s\n",
386 pci_get_slot(dev), 'A' + pin, Interrupts[0],
388 interrupt = Interrupts[0];
391 if (crsbuf.Pointer != NULL)
392 AcpiOsFree(crsbuf.Pointer);
393 if (prsbuf.Pointer != NULL)
394 AcpiOsFree(prsbuf.Pointer);
395 if (buf.Pointer != NULL)
396 AcpiOsFree(buf.Pointer);
398 /* XXX APIC_IO interrupt mapping? */
399 return_VALUE (interrupt);