Bring in YONETANI Tomokazu's acpi-update-2.patch (27-May-2004), a major
[dragonfly.git] / sys / dev / acpica5 / acpi_pcib.c
CommitLineData
5ed44076
MD
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 *
f9d8cd12
MD
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.2 2004/06/27 08:52:39 dillon Exp $
5ed44076
MD
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"
5ed44076
MD
38#include "acpivar.h"
39#include "acpi_pcibvar.h"
40
5ed44076 41#include <bus/pci/pcivar.h>
5ed44076
MD
42#include "pcib_if.h"
43
f9d8cd12 44/* Hooks for the ACPI CA debugging infrastructure. */
5ed44076
MD
45#define _COMPONENT ACPI_BUS
46ACPI_MODULE_NAME("PCI")
47
48int
49acpi_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 /*
f9d8cd12
MD
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.
5ed44076
MD
68 */
69 prt->Length = ACPI_ALLOCATE_BUFFER;
70 status = AcpiGetIrqRoutingTable(acpi_get_handle(dev), prt);
71 if (ACPI_FAILURE(status))
5ed44076
MD
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);
f9d8cd12
MD
88
89 return_VALUE (bus_generic_attach(dev));
5ed44076
MD
90}
91
92int
93acpi_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 */
106int
107acpi_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 buf.Pointer = NULL;
125 crsbuf.Pointer = NULL;
126 prsbuf.Pointer = NULL;
127 interrupt = 255;
128
f9d8cd12 129 /* ACPI numbers pins 0-3, not 1-4 like the BIOS. */
5ed44076
MD
130 pin--;
131
f9d8cd12 132 /* We failed to retrieve the routing table. */
5ed44076 133 prtp = prtbuf->Pointer;
f9d8cd12 134 if (prtp == NULL)
5ed44076
MD
135 goto out;
136
f9d8cd12 137 /* Scan the table to look for this device. */
5ed44076
MD
138 for (;;) {
139 prt = (ACPI_PCI_ROUTING_TABLE *)prtp;
140
f9d8cd12
MD
141 /* We hit the end of the table. */
142 if (prt->Length == 0)
5ed44076
MD
143 goto out;
144
145 /*
146 * Compare the slot number (high word of Address) and pin number
147 * (note that ACPI uses 0 for INTA) to check for a match.
148 *
149 * Note that the low word of the Address field (function number)
150 * is required by the specification to be 0xffff. We don't risk
151 * checking it here.
152 */
f9d8cd12
MD
153 if (((prt->Address & 0xffff0000) >> 16) == pci_get_slot(dev) &&
154 prt->Pin == pin) {
5ed44076 155 if (bootverbose)
f9d8cd12
MD
156 device_printf(pcib, "matched entry for %d.%d.INT%c (src %s)\n",
157 pci_get_bus(dev), pci_get_slot(dev), 'A' + pin,
158 prt->Source);
5ed44076
MD
159 break;
160 }
161
f9d8cd12 162 /* Skip to the next entry. */
5ed44076
MD
163 prtp += prt->Length;
164 }
165
166 /*
167 * If source is empty/NULL, the source index is the global IRQ number.
168 */
f9d8cd12 169 if (prt->Source == NULL || prt->Source[0] == '\0') {
5ed44076
MD
170 if (bootverbose)
171 device_printf(pcib, "device is hardwired to IRQ %d\n",
f9d8cd12 172 prt->SourceIndex);
5ed44076
MD
173 interrupt = prt->SourceIndex;
174 goto out;
175 }
176
177 /*
f9d8cd12 178 * We have to find the source device (PCI interrupt link device).
5ed44076
MD
179 */
180 if (ACPI_FAILURE(AcpiGetHandle(ACPI_ROOT_OBJECT, prt->Source, &lnkdev))) {
181 device_printf(pcib, "couldn't find PCI interrupt link device %s\n",
f9d8cd12 182 prt->Source);
5ed44076
MD
183 goto out;
184 }
185
186 /*
f9d8cd12 187 * Verify that this is a PCI link device and that it's present.
5ed44076
MD
188 */
189 buf.Length = ACPI_ALLOCATE_BUFFER;
190 if (ACPI_FAILURE(AcpiGetObjectInfo(lnkdev, &buf))) {
191 device_printf(pcib, "couldn't validate PCI interrupt link device %s\n",
f9d8cd12 192 prt->Source);
5ed44076
MD
193 goto out;
194 }
195 devinfo = (ACPI_DEVICE_INFO *)buf.Pointer;
196 if ((devinfo->Valid & ACPI_VALID_HID) == 0 ||
197 strcmp("PNP0C0F", devinfo->HardwareId.Value) != 0) {
f9d8cd12 198 device_printf(pcib, "PCI interrupt link %s has invalid _HID (%s)\n",
5ed44076
MD
199 prt->Source, devinfo->HardwareId.Value);
200 goto out;
201 }
202 if ((devinfo->Valid & ACPI_VALID_STA) != 0 &&
203 (devinfo->CurrentStatus & 0x9) != 0x9) {
5ed44076
MD
204 device_printf(pcib, "PCI interrupt link device %s not present\n",
205 prt->Source);
206 goto out;
207 }
208
209 /*
210 * Get the current and possible resources for the interrupt link device.
f9d8cd12 211 * If we fail to get the current resources, this is a fatal error.
5ed44076
MD
212 */
213 crsbuf.Length = ACPI_ALLOCATE_BUFFER;
214 if (ACPI_FAILURE(status = AcpiGetCurrentResources(lnkdev, &crsbuf))) {
f9d8cd12 215 device_printf(pcib, "PCI interrupt link device _CRS failed - %s\n",
5ed44076 216 AcpiFormatException(status));
f9d8cd12 217 goto out;
5ed44076
MD
218 }
219 prsbuf.Length = ACPI_ALLOCATE_BUFFER;
220 if (ACPI_FAILURE(status = AcpiGetPossibleResources(lnkdev, &prsbuf))) {
f9d8cd12 221 device_printf(pcib, "PCI interrupt link device _PRS failed - %s\n",
5ed44076 222 AcpiFormatException(status));
5ed44076
MD
223 }
224 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "got %ld bytes for %s._CRS\n",
f9d8cd12 225 (long)crsbuf.Length, acpi_name(lnkdev)));
5ed44076 226 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "got %ld bytes for %s._PRS\n",
f9d8cd12 227 (long)prsbuf.Length, acpi_name(lnkdev)));
5ed44076
MD
228
229 /*
f9d8cd12
MD
230 * The interrupt may already be routed, so check _CRS first. We don't
231 * check the 'decoding' bit in the _STA result, since there's nothing in
232 * the spec that mandates it be set, however some BIOS' will set it if
233 * the decode is active.
5ed44076 234 *
f9d8cd12
MD
235 * The Source Index points to the particular resource entry we're
236 * interested in.
5ed44076 237 */
f9d8cd12
MD
238 if (ACPI_FAILURE(acpi_FindIndexedResource(&crsbuf, prt->SourceIndex,
239 &crsres))) {
5ed44076
MD
240 device_printf(pcib, "_CRS buffer corrupt, cannot route interrupt\n");
241 goto out;
242 }
243
f9d8cd12 244 /* Type-check the resource we've found. */
5ed44076
MD
245 if (crsres->Id != ACPI_RSTYPE_IRQ && crsres->Id != ACPI_RSTYPE_EXT_IRQ) {
246 device_printf(pcib, "_CRS resource entry has unsupported type %d\n",
f9d8cd12 247 crsres->Id);
5ed44076
MD
248 goto out;
249 }
250
f9d8cd12 251 /* Set variables based on resource type. */
5ed44076
MD
252 if (crsres->Id == ACPI_RSTYPE_IRQ) {
253 NumberOfInterrupts = crsres->Data.Irq.NumberOfInterrupts;
254 Interrupts = crsres->Data.Irq.Interrupts;
255 } else {
256 NumberOfInterrupts = crsres->Data.ExtendedIrq.NumberOfInterrupts;
257 Interrupts = crsres->Data.ExtendedIrq.Interrupts;
258 }
259
f9d8cd12 260 /* If there's more than one interrupt, this is an error. */
5ed44076
MD
261 if (NumberOfInterrupts > 1) {
262 device_printf(pcib, "device has too many interrupts (%d)\n",
f9d8cd12 263 NumberOfInterrupts);
5ed44076
MD
264 goto out;
265 }
266
267 /*
f9d8cd12
MD
268 * If there's only one interrupt, and it's not zero, then it's already
269 * routed.
5ed44076 270 *
f9d8cd12
MD
271 * Note that we could also check the 'decoding' bit in _STA, but can't
272 * depend on it since it's not part of the spec.
5ed44076
MD
273 *
274 * XXX check ASL examples to see if this is an acceptable set of tests
275 */
f9d8cd12 276 if (NumberOfInterrupts == 1 && Interrupts[0] != 0) {
5ed44076 277 device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
f9d8cd12 278 pci_get_slot(dev), 'A' + pin, Interrupts[0]);
5ed44076
MD
279 interrupt = Interrupts[0];
280 goto out;
281 }
282
283 /*
284 * There isn't an interrupt, so we have to look at _PRS to get one.
f9d8cd12
MD
285 * Get the set of allowed interrupts from the _PRS resource indexed
286 * by SourceIndex.
5ed44076
MD
287 */
288 if (prsbuf.Pointer == NULL) {
f9d8cd12 289 device_printf(pcib, "no routed irq and no _PRS on irq link device\n");
5ed44076
MD
290 goto out;
291 }
292
f9d8cd12
MD
293 /*
294 * Search through the _PRS resources, looking for an IRQ or extended
295 * IRQ resource. Skip dependent function resources for now. In the
296 * future, we might use these for priority but this is good enough for
297 * now until BIOS vendors actually mean something by using them.
298 */
299 prsres = NULL;
300 for (i = prt->SourceIndex; prsres == NULL; i++) {
301 if (ACPI_FAILURE(acpi_FindIndexedResource(&prsbuf, i, &prsres))) {
302 device_printf(pcib, "_PRS lacks IRQ resource, routing failed\n");
303 goto out;
304 }
305 switch (prsres->Id) {
306 case ACPI_RSTYPE_IRQ:
307 NumberOfInterrupts = prsres->Data.Irq.NumberOfInterrupts;
308 Interrupts = prsres->Data.Irq.Interrupts;
309 break;
310 case ACPI_RSTYPE_EXT_IRQ:
311 NumberOfInterrupts = prsres->Data.ExtendedIrq.NumberOfInterrupts;
312 Interrupts = prsres->Data.ExtendedIrq.Interrupts;
313 break;
314 case ACPI_RSTYPE_START_DPF:
315 prsres = NULL;
316 continue;
317 default:
318 device_printf(pcib, "_PRS has invalid type %d\n", prsres->Id);
319 goto out;
320 }
5ed44076
MD
321 }
322
f9d8cd12 323 /* There has to be at least one interrupt available. */
5ed44076
MD
324 if (NumberOfInterrupts < 1) {
325 device_printf(pcib, "device has no interrupts\n");
326 goto out;
327 }
328
329 /*
f9d8cd12
MD
330 * Pick an interrupt to use. Note that a more scientific approach than
331 * just taking the first one available would be desirable.
5ed44076 332 *
f9d8cd12
MD
333 * The PCI BIOS $PIR table offers "preferred PCI interrupts", but ACPI
334 * doesn't seem to offer a similar mechanism, so picking a "good"
335 * interrupt here is a difficult task.
5ed44076 336 *
f9d8cd12
MD
337 * Build a resource buffer and pass it to AcpiSetCurrentResources to
338 * route the new interrupt.
5ed44076
MD
339 */
340 device_printf(pcib, "possible interrupts:");
341 for (i = 0; i < NumberOfInterrupts; i++)
342 printf(" %d", Interrupts[i]);
343 printf("\n");
344
f9d8cd12
MD
345 /* This should never happen. */
346 if (crsbuf.Pointer != NULL)
5ed44076 347 AcpiOsFree(crsbuf.Pointer);
f9d8cd12
MD
348
349 /* XXX Data.Irq and Data.ExtendedIrq are implicitly structure-copied. */
5ed44076
MD
350 crsbuf.Pointer = NULL;
351 if (prsres->Id == ACPI_RSTYPE_IRQ) {
352 resbuf.Id = ACPI_RSTYPE_IRQ;
353 resbuf.Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_IRQ);
f9d8cd12 354 resbuf.Data.Irq = prsres->Data.Irq;
5ed44076 355 resbuf.Data.Irq.NumberOfInterrupts = 1;
f9d8cd12 356 resbuf.Data.Irq.Interrupts[0] = Interrupts[0];
5ed44076
MD
357 } else {
358 resbuf.Id = ACPI_RSTYPE_EXT_IRQ;
f9d8cd12
MD
359 resbuf.Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_EXT_IRQ);
360 resbuf.Data.ExtendedIrq = prsres->Data.ExtendedIrq;
5ed44076 361 resbuf.Data.ExtendedIrq.NumberOfInterrupts = 1;
f9d8cd12 362 resbuf.Data.ExtendedIrq.Interrupts[0] = Interrupts[0];
5ed44076
MD
363 }
364 if (ACPI_FAILURE(status = acpi_AppendBufferResource(&crsbuf, &resbuf))) {
f9d8cd12
MD
365 device_printf(pcib, "buf append failed for interrupt %d via %s - %s\n",
366 Interrupts[0], acpi_name(lnkdev),
367 AcpiFormatException(status));
368 goto out;
369 }
370 /* XXX Figure out how this is happening when the append succeeds. */
371 if (crsbuf.Pointer == NULL) {
372 device_printf(pcib, "_CRS buf NULL after append?\n");
5ed44076
MD
373 goto out;
374 }
375 if (ACPI_FAILURE(status = AcpiSetCurrentResources(lnkdev, &crsbuf))) {
f9d8cd12
MD
376 device_printf(pcib, "_SRS failed for interrupt %d via %s - %s\n",
377 Interrupts[0], acpi_name(lnkdev),
378 AcpiFormatException(status));
5ed44076
MD
379 goto out;
380 }
381
f9d8cd12 382 /* Return the interrupt we just routed. */
5ed44076 383 device_printf(pcib, "slot %d INT%c routed to irq %d via %s\n",
f9d8cd12
MD
384 pci_get_slot(dev), 'A' + pin, Interrupts[0],
385 acpi_name(lnkdev));
5ed44076
MD
386 interrupt = Interrupts[0];
387
f9d8cd12 388out:
5ed44076
MD
389 if (crsbuf.Pointer != NULL)
390 AcpiOsFree(crsbuf.Pointer);
391 if (prsbuf.Pointer != NULL)
392 AcpiOsFree(prsbuf.Pointer);
393 if (buf.Pointer != NULL)
394 AcpiOsFree(buf.Pointer);
395
396 /* XXX APIC_IO interrupt mapping? */
f9d8cd12 397 return_VALUE (interrupt);
5ed44076 398}