blob: c536ccfc54136529919b7159b416c7c7ec271417 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * pci_irq.c - ACPI PCI Interrupt Routing ($Revision: 11 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 * Copyright (C) 2002 Dominik Brodowski <devel@brodo.de>
7 *
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 */
26
27#include <linux/config.h>
28
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/types.h>
33#include <linux/proc_fs.h>
34#include <linux/spinlock.h>
35#include <linux/pm.h>
36#include <linux/pci.h>
37#include <linux/acpi.h>
38#include <acpi/acpi_bus.h>
39#include <acpi/acpi_drivers.h>
40
41
42#define _COMPONENT ACPI_PCI_COMPONENT
43ACPI_MODULE_NAME ("pci_irq")
44
45static struct acpi_prt_list acpi_prt;
46static DEFINE_SPINLOCK(acpi_prt_lock);
47
48/* --------------------------------------------------------------------------
49 PCI IRQ Routing Table (PRT) Support
50 -------------------------------------------------------------------------- */
51
52static struct acpi_prt_entry *
53acpi_pci_irq_find_prt_entry (
54 int segment,
55 int bus,
56 int device,
57 int pin)
58{
59 struct list_head *node = NULL;
60 struct acpi_prt_entry *entry = NULL;
61
62 ACPI_FUNCTION_TRACE("acpi_pci_irq_find_prt_entry");
63
64 if (!acpi_prt.count)
65 return_PTR(NULL);
66
67 /*
68 * Parse through all PRT entries looking for a match on the specified
69 * PCI device's segment, bus, device, and pin (don't care about func).
70 *
71 */
72 spin_lock(&acpi_prt_lock);
73 list_for_each(node, &acpi_prt.entries) {
74 entry = list_entry(node, struct acpi_prt_entry, node);
75 if ((segment == entry->id.segment)
76 && (bus == entry->id.bus)
77 && (device == entry->id.device)
78 && (pin == entry->pin)) {
79 spin_unlock(&acpi_prt_lock);
80 return_PTR(entry);
81 }
82 }
83
84 spin_unlock(&acpi_prt_lock);
85 return_PTR(NULL);
86}
87
88
89static int
90acpi_pci_irq_add_entry (
91 acpi_handle handle,
92 int segment,
93 int bus,
94 struct acpi_pci_routing_table *prt)
95{
96 struct acpi_prt_entry *entry = NULL;
97
98 ACPI_FUNCTION_TRACE("acpi_pci_irq_add_entry");
99
100 if (!prt)
101 return_VALUE(-EINVAL);
102
103 entry = kmalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
104 if (!entry)
105 return_VALUE(-ENOMEM);
106 memset(entry, 0, sizeof(struct acpi_prt_entry));
107
108 entry->id.segment = segment;
109 entry->id.bus = bus;
110 entry->id.device = (prt->address >> 16) & 0xFFFF;
111 entry->id.function = prt->address & 0xFFFF;
112 entry->pin = prt->pin;
113
114 /*
115 * Type 1: Dynamic
116 * ---------------
117 * The 'source' field specifies the PCI interrupt link device used to
118 * configure the IRQ assigned to this slot|dev|pin. The 'source_index'
119 * indicates which resource descriptor in the resource template (of
120 * the link device) this interrupt is allocated from.
121 *
122 * NOTE: Don't query the Link Device for IRQ information at this time
123 * because Link Device enumeration may not have occurred yet
124 * (e.g. exists somewhere 'below' this _PRT entry in the ACPI
125 * namespace).
126 */
127 if (prt->source[0]) {
128 acpi_get_handle(handle, prt->source, &entry->link.handle);
129 entry->link.index = prt->source_index;
130 }
131 /*
132 * Type 2: Static
133 * --------------
134 * The 'source' field is NULL, and the 'source_index' field specifies
135 * the IRQ value, which is hardwired to specific interrupt inputs on
136 * the interrupt controller.
137 */
138 else
139 entry->link.index = prt->source_index;
140
141 ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
142 " %02X:%02X:%02X[%c] -> %s[%d]\n",
143 entry->id.segment, entry->id.bus, entry->id.device,
144 ('A' + entry->pin), prt->source, entry->link.index));
145
146 spin_lock(&acpi_prt_lock);
147 list_add_tail(&entry->node, &acpi_prt.entries);
148 acpi_prt.count++;
149 spin_unlock(&acpi_prt_lock);
150
151 return_VALUE(0);
152}
153
154
155static void
156acpi_pci_irq_del_entry (
157 int segment,
158 int bus,
159 struct acpi_prt_entry *entry)
160{
161 if (segment == entry->id.segment && bus == entry->id.bus){
162 acpi_prt.count--;
163 list_del(&entry->node);
164 kfree(entry);
165 }
166}
167
168
169int
170acpi_pci_irq_add_prt (
171 acpi_handle handle,
172 int segment,
173 int bus)
174{
175 acpi_status status = AE_OK;
176 char *pathname = NULL;
177 struct acpi_buffer buffer = {0, NULL};
178 struct acpi_pci_routing_table *prt = NULL;
179 struct acpi_pci_routing_table *entry = NULL;
180 static int first_time = 1;
181
182 ACPI_FUNCTION_TRACE("acpi_pci_irq_add_prt");
183
184 pathname = (char *) kmalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
185 if(!pathname)
186 return_VALUE(-ENOMEM);
187 memset(pathname, 0, ACPI_PATHNAME_MAX);
188
189 if (first_time) {
190 acpi_prt.count = 0;
191 INIT_LIST_HEAD(&acpi_prt.entries);
192 first_time = 0;
193 }
194
195 /*
196 * NOTE: We're given a 'handle' to the _PRT object's parent device
197 * (either a PCI root bridge or PCI-PCI bridge).
198 */
199
200 buffer.length = ACPI_PATHNAME_MAX;
201 buffer.pointer = pathname;
202 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
203
204 printk(KERN_DEBUG "ACPI: PCI Interrupt Routing Table [%s._PRT]\n",
205 pathname);
206
207 /*
208 * Evaluate this _PRT and add its entries to our global list (acpi_prt).
209 */
210
211 buffer.length = 0;
212 buffer.pointer = NULL;
213 kfree(pathname);
214 status = acpi_get_irq_routing_table(handle, &buffer);
215 if (status != AE_BUFFER_OVERFLOW) {
216 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRT [%s]\n",
217 acpi_format_exception(status)));
218 return_VALUE(-ENODEV);
219 }
220
221 prt = kmalloc(buffer.length, GFP_KERNEL);
222 if (!prt){
223 return_VALUE(-ENOMEM);
224 }
225 memset(prt, 0, buffer.length);
226 buffer.pointer = prt;
227
228 status = acpi_get_irq_routing_table(handle, &buffer);
229 if (ACPI_FAILURE(status)) {
230 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRT [%s]\n",
231 acpi_format_exception(status)));
232 kfree(buffer.pointer);
233 return_VALUE(-ENODEV);
234 }
235
236 entry = prt;
237
238 while (entry && (entry->length > 0)) {
239 acpi_pci_irq_add_entry(handle, segment, bus, entry);
240 entry = (struct acpi_pci_routing_table *)
241 ((unsigned long) entry + entry->length);
242 }
243
244 kfree(prt);
245
246 return_VALUE(0);
247}
248
249void
250acpi_pci_irq_del_prt (int segment, int bus)
251{
252 struct list_head *node = NULL, *n = NULL;
253 struct acpi_prt_entry *entry = NULL;
254
255 if (!acpi_prt.count) {
256 return;
257 }
258
259 printk(KERN_DEBUG "ACPI: Delete PCI Interrupt Routing Table for %x:%x\n",
260 segment, bus);
261 spin_lock(&acpi_prt_lock);
262 list_for_each_safe(node, n, &acpi_prt.entries) {
263 entry = list_entry(node, struct acpi_prt_entry, node);
264
265 acpi_pci_irq_del_entry(segment, bus, entry);
266 }
267 spin_unlock(&acpi_prt_lock);
268}
269/* --------------------------------------------------------------------------
270 PCI Interrupt Routing Support
271 -------------------------------------------------------------------------- */
David Shaohua Li87bec662005-07-27 23:02:00 -0400272typedef int (*irq_lookup_func)(struct acpi_prt_entry *, int *, int *, char **);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
David Shaohua Li87bec662005-07-27 23:02:00 -0400274static int
275acpi_pci_allocate_irq(struct acpi_prt_entry *entry,
276 int *edge_level,
277 int *active_high_low,
278 char **link)
279{
280 int irq;
281
282 ACPI_FUNCTION_TRACE("acpi_pci_allocate_irq");
283
284 if (entry->link.handle) {
285 irq = acpi_pci_link_allocate_irq(entry->link.handle,
286 entry->link.index, edge_level, active_high_low, link);
287 if (irq < 0) {
288 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Invalid IRQ link routing entry\n"));
289 return_VALUE(-1);
290 }
291 } else {
292 irq = entry->link.index;
293 *edge_level = ACPI_LEVEL_SENSITIVE;
294 *active_high_low = ACPI_ACTIVE_LOW;
295 }
296
297 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found IRQ %d\n", irq));
298 return_VALUE(irq);
299}
300
301static int
302acpi_pci_free_irq(struct acpi_prt_entry *entry,
303 int *edge_level,
304 int *active_high_low,
305 char **link)
306{
307 int irq;
308
309 ACPI_FUNCTION_TRACE("acpi_pci_free_irq");
310 if (entry->link.handle) {
311 irq = acpi_pci_link_free_irq(entry->link.handle);
312 } else {
313 irq = entry->link.index;
314 }
315 return_VALUE(irq);
316}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/*
318 * acpi_pci_irq_lookup
319 * success: return IRQ >= 0
320 * failure: return -1
321 */
322static int
323acpi_pci_irq_lookup (
324 struct pci_bus *bus,
325 int device,
326 int pin,
327 int *edge_level,
328 int *active_high_low,
David Shaohua Li87bec662005-07-27 23:02:00 -0400329 char **link,
330 irq_lookup_func func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct acpi_prt_entry *entry = NULL;
333 int segment = pci_domain_nr(bus);
334 int bus_nr = bus->number;
David Shaohua Li87bec662005-07-27 23:02:00 -0400335 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 ACPI_FUNCTION_TRACE("acpi_pci_irq_lookup");
338
339 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
340 "Searching for PRT entry for %02x:%02x:%02x[%c]\n",
341 segment, bus_nr, device, ('A' + pin)));
342
343 entry = acpi_pci_irq_find_prt_entry(segment, bus_nr, device, pin);
344 if (!entry) {
345 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PRT entry not found\n"));
346 return_VALUE(-1);
347 }
348
David Shaohua Li87bec662005-07-27 23:02:00 -0400349 ret = func(entry, edge_level, active_high_low, link);
350 return_VALUE(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353/*
354 * acpi_pci_irq_derive
355 * success: return IRQ >= 0
356 * failure: return < 0
357 */
358static int
359acpi_pci_irq_derive (
360 struct pci_dev *dev,
361 int pin,
362 int *edge_level,
363 int *active_high_low,
David Shaohua Li87bec662005-07-27 23:02:00 -0400364 char **link,
365 irq_lookup_func func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct pci_dev *bridge = dev;
368 int irq = -1;
369 u8 bridge_pin = 0;
370
371 ACPI_FUNCTION_TRACE("acpi_pci_irq_derive");
372
373 if (!dev)
374 return_VALUE(-EINVAL);
375
376 /*
377 * Attempt to derive an IRQ for this device from a parent bridge's
378 * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
379 */
380 while (irq < 0 && bridge->bus->self) {
381 pin = (pin + PCI_SLOT(bridge->devfn)) % 4;
382 bridge = bridge->bus->self;
383
384 if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
385 /* PC card has the same IRQ as its cardbridge */
386 pci_read_config_byte(bridge, PCI_INTERRUPT_PIN, &bridge_pin);
387 if (!bridge_pin) {
388 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
389 "No interrupt pin configured for device %s\n", pci_name(bridge)));
390 return_VALUE(-1);
391 }
392 /* Pin is from 0 to 3 */
393 bridge_pin --;
394 pin = bridge_pin;
395 }
396
397 irq = acpi_pci_irq_lookup(bridge->bus, PCI_SLOT(bridge->devfn),
David Shaohua Li87bec662005-07-27 23:02:00 -0400398 pin, edge_level, active_high_low, link, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400
401 if (irq < 0) {
402 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Unable to derive IRQ for device %s\n", pci_name(dev)));
403 return_VALUE(-1);
404 }
405
406 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Derive IRQ %d for device %s from %s\n",
407 irq, pci_name(dev), pci_name(bridge)));
408
409 return_VALUE(irq);
410}
411
412/*
413 * acpi_pci_irq_enable
414 * success: return 0
415 * failure: return < 0
416 */
417
418int
419acpi_pci_irq_enable (
420 struct pci_dev *dev)
421{
422 int irq = 0;
423 u8 pin = 0;
424 int edge_level = ACPI_LEVEL_SENSITIVE;
425 int active_high_low = ACPI_ACTIVE_LOW;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 char *link = NULL;
427
428 ACPI_FUNCTION_TRACE("acpi_pci_irq_enable");
429
430 if (!dev)
431 return_VALUE(-EINVAL);
432
433 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
434 if (!pin) {
435 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No interrupt pin configured for device %s\n", pci_name(dev)));
436 return_VALUE(0);
437 }
438 pin--;
439
440 if (!dev->bus) {
441 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid (NULL) 'bus' field\n"));
442 return_VALUE(-ENODEV);
443 }
444
445 /*
446 * First we check the PCI IRQ routing table (PRT) for an IRQ. PRT
447 * values override any BIOS-assigned IRQs set during boot.
448 */
449 irq = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
David Shaohua Li87bec662005-07-27 23:02:00 -0400450 &edge_level, &active_high_low, &link, acpi_pci_allocate_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /*
453 * If no PRT entry was found, we'll try to derive an IRQ from the
454 * device's parent bridge.
455 */
456 if (irq < 0)
457 irq = acpi_pci_irq_derive(dev, pin, &edge_level,
David Shaohua Li87bec662005-07-27 23:02:00 -0400458 &active_high_low, &link, acpi_pci_allocate_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 /*
461 * No IRQ known to the ACPI subsystem - maybe the BIOS /
462 * driver reported one, then use it. Exit in any case.
463 */
464 if (irq < 0) {
465 printk(KERN_WARNING PREFIX "PCI Interrupt %s[%c]: no GSI",
466 pci_name(dev), ('A' + pin));
467 /* Interrupt Line values above 0xF are forbidden */
468 if (dev->irq >= 0 && (dev->irq <= 0xF)) {
469 printk(" - using IRQ %d\n", dev->irq);
470 return_VALUE(0);
471 }
472 else {
473 printk("\n");
474 return_VALUE(0);
475 }
476 }
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 dev->irq = acpi_register_gsi(irq, edge_level, active_high_low);
479
480 printk(KERN_INFO PREFIX "PCI Interrupt %s[%c] -> ",
481 pci_name(dev), 'A' + pin);
482
483 if (link)
484 printk("Link [%s] -> ", link);
485
486 printk("GSI %u (%s, %s) -> IRQ %d\n", irq,
487 (edge_level == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
488 (active_high_low == ACPI_ACTIVE_LOW) ? "low" : "high",
489 dev->irq);
490
491 return_VALUE(0);
492}
493EXPORT_SYMBOL(acpi_pci_irq_enable);
494
495
David Shaohua Li87bec662005-07-27 23:02:00 -0400496/* FIXME: implement x86/x86_64 version */
497void __attribute__((weak)) acpi_unregister_gsi(u32 i) {}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499void
500acpi_pci_irq_disable (
501 struct pci_dev *dev)
502{
503 int gsi = 0;
504 u8 pin = 0;
505 int edge_level = ACPI_LEVEL_SENSITIVE;
506 int active_high_low = ACPI_ACTIVE_LOW;
507
508 ACPI_FUNCTION_TRACE("acpi_pci_irq_disable");
509
510 if (!dev)
511 return_VOID;
512
513 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
514 if (!pin)
515 return_VOID;
516 pin--;
517
518 if (!dev->bus)
519 return_VOID;
520
521 /*
522 * First we check the PCI IRQ routing table (PRT) for an IRQ.
523 */
524 gsi = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
David Shaohua Li87bec662005-07-27 23:02:00 -0400525 &edge_level, &active_high_low, NULL, acpi_pci_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /*
527 * If no PRT entry was found, we'll try to derive an IRQ from the
528 * device's parent bridge.
529 */
530 if (gsi < 0)
531 gsi = acpi_pci_irq_derive(dev, pin,
David Shaohua Li87bec662005-07-27 23:02:00 -0400532 &edge_level, &active_high_low, NULL, acpi_pci_free_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (gsi < 0)
534 return_VOID;
535
536 /*
537 * TBD: It might be worth clearing dev->irq by magic constant
538 * (e.g. PCI_UNDEFINED_IRQ).
539 */
540
541 printk(KERN_INFO PREFIX "PCI interrupt for device %s disabled\n",
542 pci_name(dev));
543
544 acpi_unregister_gsi(gsi);
545
546 return_VOID;
547}