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
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 $
32 #include <sys/param.h>
34 #include <sys/malloc.h>
35 #include <sys/kernel.h>
40 #include "acpi_pcibvar.h"
42 #include <bus/pci/pcivar.h>
45 /* Hooks for the ACPI CA debugging infrastructure. */
46 #define _COMPONENT ACPI_BUS
47 ACPI_MODULE_NAME("PCI")
50 acpi_pcib_attach(device_t dev, ACPI_BUFFER *prt, int busno)
55 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
58 * Don't attach if we're not really there.
60 * XXX: This isn't entirely correct since we may be a PCI bus
61 * on a hot-plug docking station, etc.
63 if (!acpi_DeviceIsPresent(dev))
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.
70 prt->Length = ACPI_ALLOCATE_BUFFER;
71 status = AcpiGetIrqRoutingTable(acpi_get_handle(dev), prt);
72 if (ACPI_FAILURE(status))
74 "could not get PCI interrupt routing table for %s - %s\n",
75 acpi_name(acpi_get_handle(dev)), AcpiFormatException(status));
78 * Attach the PCI bus proper.
80 if ((child = device_add_child(dev, "pci", busno)) == NULL) {
81 device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
86 * Now go scan the bus.
88 acpi_pci_link_config(dev, prt, busno);
90 return_VALUE (bus_generic_attach(dev));
94 acpi_pcib_resume(device_t dev, ACPI_BUFFER *prt, int busno)
96 acpi_pci_link_resume(dev, prt, busno);
97 return (bus_generic_resume(dev));
101 * Route an interrupt for a child of the bridge.
103 * XXX clean up error messages
105 * XXX this function is somewhat bulky
108 acpi_pcib_route_interrupt(device_t pcib, device_t dev, int pin,
111 ACPI_PCI_ROUTING_TABLE *prt;
113 ACPI_BUFFER crsbuf, prsbuf, buf;
114 ACPI_RESOURCE *crsres, *prsres, resbuf;
115 ACPI_DEVICE_INFO *devinfo;
117 UINT32 InterruptCount, intr;
122 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
125 crsbuf.Pointer = NULL;
126 prsbuf.Pointer = NULL;
130 /* ACPI numbers pins 0-3, not 1-4 like the BIOS. */
133 /* We failed to retrieve the routing table. */
134 prtp = prtbuf->Pointer;
138 /* Scan the table to look for this device. */
140 prt = (ACPI_PCI_ROUTING_TABLE *)prtp;
142 /* We hit the end of the table. */
143 if (prt->Length == 0)
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.
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
154 if (((prt->Address & 0xffff0000) >> 16) == pci_get_slot(dev) &&
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,
163 /* Skip to the next entry. */
168 * If source is empty/NULL, the source index is the global IRQ number.
170 if (prt->Source == NULL || prt->Source[0] == '\0') {
172 device_printf(pcib, "device is hardwired to IRQ %d\n",
174 interrupt = prt->SourceIndex;
179 * We have to find the source device (PCI interrupt link device).
181 if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, prt->Source, &lnkdev))) {
182 device_printf(pcib, "couldn't find PCI interrupt link device %s\n",
188 * Verify that this is a PCI link device and that it's present.
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",
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);
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",
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.
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));
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));
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)));
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.
236 * The Source Index points to the particular resource entry we're
239 if (ACPI_FAILURE(acpi_FindIndexedResource(&crsbuf, prt->SourceIndex,
241 device_printf(pcib, "_CRS buffer corrupt, cannot route interrupt\n");
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",
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];
258 InterruptCount = crsres->Data.ExtendedIrq.InterruptCount;
259 if (InterruptCount >= 1)
260 intr = crsres->Data.ExtendedIrq.Interrupts[0];
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",
271 * If there's only one interrupt, and it's not zero, then it's already
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.
277 * XXX check ASL examples to see if this is an acceptable set of tests
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);
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
291 if (prsbuf.Pointer == NULL) {
292 device_printf(pcib, "no routed irq and no _PRS on irq link device\n");
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.
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");
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]);
315 intr = prsres->Data.Irq.Interrupts[0];
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]);
323 intr = prsres->Data.ExtendedIrq.Interrupts[0];
325 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
329 device_printf(pcib, "_PRS has invalid type %d\n", prsres->Type);
334 /* There has to be at least one interrupt available. */
335 if (InterruptCount < 1) {
336 device_printf(pcib, "device has no interrupts\n");
341 * Pick an interrupt to use. Note that a more scientific approach than
342 * just taking the first one available would be desirable.
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.
348 * Build a resource buffer and pass it to AcpiSetCurrentResources to
349 * route the new interrupt.
352 /* This should never happen. */
353 if (crsbuf.Pointer != NULL)
354 AcpiOsFree(crsbuf.Pointer);
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;
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;
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));
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");
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));
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));
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);
400 /* XXX APIC_IO interrupt mapping? */
401 return_VALUE (interrupt);