Linux 2.6.25
[linux-2.6/s3c2410-cpufreq.git] / drivers / acpi / pci_irq.c
blob89022a74faeeda9d9ed9b11f431c2e4c4eecb4d7
1 /*
2 * pci_irq.c - ACPI PCI Interrupt Routing ($Revision: 11 $)
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>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
24 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28 #include <linux/dmi.h>
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>
41 #define _COMPONENT ACPI_PCI_COMPONENT
42 ACPI_MODULE_NAME("pci_irq");
44 static struct acpi_prt_list acpi_prt;
45 static DEFINE_SPINLOCK(acpi_prt_lock);
47 /* --------------------------------------------------------------------------
48 PCI IRQ Routing Table (PRT) Support
49 -------------------------------------------------------------------------- */
51 static struct acpi_prt_entry *acpi_pci_irq_find_prt_entry(int segment,
52 int bus,
53 int device, int pin)
55 struct acpi_prt_entry *entry = NULL;
57 if (!acpi_prt.count)
58 return NULL;
61 * Parse through all PRT entries looking for a match on the specified
62 * PCI device's segment, bus, device, and pin (don't care about func).
65 spin_lock(&acpi_prt_lock);
66 list_for_each_entry(entry, &acpi_prt.entries, node) {
67 if ((segment == entry->id.segment)
68 && (bus == entry->id.bus)
69 && (device == entry->id.device)
70 && (pin == entry->pin)) {
71 spin_unlock(&acpi_prt_lock);
72 return entry;
76 spin_unlock(&acpi_prt_lock);
77 return NULL;
80 /* http://bugzilla.kernel.org/show_bug.cgi?id=4773 */
81 static struct dmi_system_id medion_md9580[] = {
83 .ident = "Medion MD9580-F laptop",
84 .matches = {
85 DMI_MATCH(DMI_SYS_VENDOR, "MEDIONNB"),
86 DMI_MATCH(DMI_PRODUCT_NAME, "A555"),
89 { }
92 /* http://bugzilla.kernel.org/show_bug.cgi?id=5044 */
93 static struct dmi_system_id dell_optiplex[] = {
95 .ident = "Dell Optiplex GX1",
96 .matches = {
97 DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
98 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex GX1 600S+"),
104 /* http://bugzilla.kernel.org/show_bug.cgi?id=10138 */
105 static struct dmi_system_id hp_t5710[] = {
107 .ident = "HP t5710",
108 .matches = {
109 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
110 DMI_MATCH(DMI_PRODUCT_NAME, "hp t5000 series"),
111 DMI_MATCH(DMI_BOARD_NAME, "098Ch"),
117 struct prt_quirk {
118 struct dmi_system_id *system;
119 unsigned int segment;
120 unsigned int bus;
121 unsigned int device;
122 unsigned char pin;
123 char *source; /* according to BIOS */
124 char *actual_source;
128 * These systems have incorrect _PRT entries. The BIOS claims the PCI
129 * interrupt at the listed segment/bus/device/pin is connected to the first
130 * link device, but it is actually connected to the second.
132 static struct prt_quirk prt_quirks[] = {
133 { medion_md9580, 0, 0, 9, 'A',
134 "\\_SB_.PCI0.ISA_.LNKA",
135 "\\_SB_.PCI0.ISA_.LNKB"},
136 { dell_optiplex, 0, 0, 0xd, 'A',
137 "\\_SB_.LNKB",
138 "\\_SB_.LNKA"},
139 { hp_t5710, 0, 0, 1, 'A',
140 "\\_SB_.PCI0.LNK1",
141 "\\_SB_.PCI0.LNK3"},
144 static void
145 do_prt_fixups(struct acpi_prt_entry *entry, struct acpi_pci_routing_table *prt)
147 int i;
148 struct prt_quirk *quirk;
150 for (i = 0; i < ARRAY_SIZE(prt_quirks); i++) {
151 quirk = &prt_quirks[i];
153 /* All current quirks involve link devices, not GSIs */
154 if (!prt->source)
155 continue;
157 if (dmi_check_system(quirk->system) &&
158 entry->id.segment == quirk->segment &&
159 entry->id.bus == quirk->bus &&
160 entry->id.device == quirk->device &&
161 entry->pin + 'A' == quirk->pin &&
162 !strcmp(prt->source, quirk->source) &&
163 strlen(prt->source) >= strlen(quirk->actual_source)) {
164 printk(KERN_WARNING PREFIX "firmware reports "
165 "%04x:%02x:%02x[%c] connected to %s; "
166 "changing to %s\n",
167 entry->id.segment, entry->id.bus,
168 entry->id.device, 'A' + entry->pin,
169 prt->source, quirk->actual_source);
170 strcpy(prt->source, quirk->actual_source);
175 static int
176 acpi_pci_irq_add_entry(acpi_handle handle,
177 int segment, int bus, struct acpi_pci_routing_table *prt)
179 struct acpi_prt_entry *entry = NULL;
182 if (!prt)
183 return -EINVAL;
185 entry = kzalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
186 if (!entry)
187 return -ENOMEM;
189 entry->id.segment = segment;
190 entry->id.bus = bus;
191 entry->id.device = (prt->address >> 16) & 0xFFFF;
192 entry->id.function = prt->address & 0xFFFF;
193 entry->pin = prt->pin;
195 do_prt_fixups(entry, prt);
198 * Type 1: Dynamic
199 * ---------------
200 * The 'source' field specifies the PCI interrupt link device used to
201 * configure the IRQ assigned to this slot|dev|pin. The 'source_index'
202 * indicates which resource descriptor in the resource template (of
203 * the link device) this interrupt is allocated from.
205 * NOTE: Don't query the Link Device for IRQ information at this time
206 * because Link Device enumeration may not have occurred yet
207 * (e.g. exists somewhere 'below' this _PRT entry in the ACPI
208 * namespace).
210 if (prt->source[0]) {
211 acpi_get_handle(handle, prt->source, &entry->link.handle);
212 entry->link.index = prt->source_index;
215 * Type 2: Static
216 * --------------
217 * The 'source' field is NULL, and the 'source_index' field specifies
218 * the IRQ value, which is hardwired to specific interrupt inputs on
219 * the interrupt controller.
221 else
222 entry->link.index = prt->source_index;
224 ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
225 " %02X:%02X:%02X[%c] -> %s[%d]\n",
226 entry->id.segment, entry->id.bus,
227 entry->id.device, ('A' + entry->pin), prt->source,
228 entry->link.index));
230 spin_lock(&acpi_prt_lock);
231 list_add_tail(&entry->node, &acpi_prt.entries);
232 acpi_prt.count++;
233 spin_unlock(&acpi_prt_lock);
235 return 0;
238 static void
239 acpi_pci_irq_del_entry(int segment, int bus, struct acpi_prt_entry *entry)
241 if (segment == entry->id.segment && bus == entry->id.bus) {
242 acpi_prt.count--;
243 list_del(&entry->node);
244 kfree(entry);
248 int acpi_pci_irq_add_prt(acpi_handle handle, int segment, int bus)
250 acpi_status status = AE_OK;
251 char *pathname = NULL;
252 struct acpi_buffer buffer = { 0, NULL };
253 struct acpi_pci_routing_table *prt = NULL;
254 struct acpi_pci_routing_table *entry = NULL;
255 static int first_time = 1;
258 pathname = kzalloc(ACPI_PATHNAME_MAX, GFP_KERNEL);
259 if (!pathname)
260 return -ENOMEM;
262 if (first_time) {
263 acpi_prt.count = 0;
264 INIT_LIST_HEAD(&acpi_prt.entries);
265 first_time = 0;
269 * NOTE: We're given a 'handle' to the _PRT object's parent device
270 * (either a PCI root bridge or PCI-PCI bridge).
273 buffer.length = ACPI_PATHNAME_MAX;
274 buffer.pointer = pathname;
275 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
277 printk(KERN_DEBUG "ACPI: PCI Interrupt Routing Table [%s._PRT]\n",
278 pathname);
281 * Evaluate this _PRT and add its entries to our global list (acpi_prt).
284 buffer.length = 0;
285 buffer.pointer = NULL;
286 kfree(pathname);
287 status = acpi_get_irq_routing_table(handle, &buffer);
288 if (status != AE_BUFFER_OVERFLOW) {
289 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRT [%s]",
290 acpi_format_exception(status)));
291 return -ENODEV;
294 prt = kzalloc(buffer.length, GFP_KERNEL);
295 if (!prt) {
296 return -ENOMEM;
298 buffer.pointer = prt;
300 status = acpi_get_irq_routing_table(handle, &buffer);
301 if (ACPI_FAILURE(status)) {
302 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRT [%s]",
303 acpi_format_exception(status)));
304 kfree(buffer.pointer);
305 return -ENODEV;
308 entry = prt;
310 while (entry && (entry->length > 0)) {
311 acpi_pci_irq_add_entry(handle, segment, bus, entry);
312 entry = (struct acpi_pci_routing_table *)
313 ((unsigned long)entry + entry->length);
316 kfree(prt);
318 return 0;
321 void acpi_pci_irq_del_prt(int segment, int bus)
323 struct list_head *node = NULL, *n = NULL;
324 struct acpi_prt_entry *entry = NULL;
326 if (!acpi_prt.count) {
327 return;
330 printk(KERN_DEBUG
331 "ACPI: Delete PCI Interrupt Routing Table for %x:%x\n", segment,
332 bus);
333 spin_lock(&acpi_prt_lock);
334 list_for_each_safe(node, n, &acpi_prt.entries) {
335 entry = list_entry(node, struct acpi_prt_entry, node);
337 acpi_pci_irq_del_entry(segment, bus, entry);
339 spin_unlock(&acpi_prt_lock);
342 /* --------------------------------------------------------------------------
343 PCI Interrupt Routing Support
344 -------------------------------------------------------------------------- */
345 typedef int (*irq_lookup_func) (struct acpi_prt_entry *, int *, int *, char **);
347 static int
348 acpi_pci_allocate_irq(struct acpi_prt_entry *entry,
349 int *triggering, int *polarity, char **link)
351 int irq;
354 if (entry->link.handle) {
355 irq = acpi_pci_link_allocate_irq(entry->link.handle,
356 entry->link.index, triggering,
357 polarity, link);
358 if (irq < 0) {
359 printk(KERN_WARNING PREFIX
360 "Invalid IRQ link routing entry\n");
361 return -1;
363 } else {
364 irq = entry->link.index;
365 *triggering = ACPI_LEVEL_SENSITIVE;
366 *polarity = ACPI_ACTIVE_LOW;
369 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found IRQ %d\n", irq));
370 return irq;
373 static int
374 acpi_pci_free_irq(struct acpi_prt_entry *entry,
375 int *triggering, int *polarity, char **link)
377 int irq;
379 if (entry->link.handle) {
380 irq = acpi_pci_link_free_irq(entry->link.handle);
381 } else {
382 irq = entry->link.index;
384 return irq;
388 * acpi_pci_irq_lookup
389 * success: return IRQ >= 0
390 * failure: return -1
392 static int
393 acpi_pci_irq_lookup(struct pci_bus *bus,
394 int device,
395 int pin,
396 int *triggering,
397 int *polarity, char **link, irq_lookup_func func)
399 struct acpi_prt_entry *entry = NULL;
400 int segment = pci_domain_nr(bus);
401 int bus_nr = bus->number;
402 int ret;
405 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
406 "Searching for PRT entry for %02x:%02x:%02x[%c]\n",
407 segment, bus_nr, device, ('A' + pin)));
409 entry = acpi_pci_irq_find_prt_entry(segment, bus_nr, device, pin);
410 if (!entry) {
411 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "PRT entry not found\n"));
412 return -1;
415 ret = func(entry, triggering, polarity, link);
416 return ret;
420 * acpi_pci_irq_derive
421 * success: return IRQ >= 0
422 * failure: return < 0
424 static int
425 acpi_pci_irq_derive(struct pci_dev *dev,
426 int pin,
427 int *triggering,
428 int *polarity, char **link, irq_lookup_func func)
430 struct pci_dev *bridge = dev;
431 int irq = -1;
432 u8 bridge_pin = 0;
435 if (!dev)
436 return -EINVAL;
439 * Attempt to derive an IRQ for this device from a parent bridge's
440 * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
442 while (irq < 0 && bridge->bus->self) {
443 pin = (pin + PCI_SLOT(bridge->devfn)) % 4;
444 bridge = bridge->bus->self;
446 if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
447 /* PC card has the same IRQ as its cardbridge */
448 bridge_pin = bridge->pin;
449 if (!bridge_pin) {
450 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
451 "No interrupt pin configured for device %s\n",
452 pci_name(bridge)));
453 return -1;
455 /* Pin is from 0 to 3 */
456 bridge_pin--;
457 pin = bridge_pin;
460 irq = acpi_pci_irq_lookup(bridge->bus, PCI_SLOT(bridge->devfn),
461 pin, triggering, polarity,
462 link, func);
465 if (irq < 0) {
466 printk(KERN_WARNING PREFIX "Unable to derive IRQ for device %s\n",
467 pci_name(dev));
468 return -1;
471 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Derive IRQ %d for device %s from %s\n",
472 irq, pci_name(dev), pci_name(bridge)));
474 return irq;
478 * acpi_pci_irq_enable
479 * success: return 0
480 * failure: return < 0
483 int acpi_pci_irq_enable(struct pci_dev *dev)
485 int irq = 0;
486 u8 pin = 0;
487 int triggering = ACPI_LEVEL_SENSITIVE;
488 int polarity = ACPI_ACTIVE_LOW;
489 char *link = NULL;
490 int rc;
493 if (!dev)
494 return -EINVAL;
496 pin = dev->pin;
497 if (!pin) {
498 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
499 "No interrupt pin configured for device %s\n",
500 pci_name(dev)));
501 return 0;
503 pin--;
505 if (!dev->bus) {
506 printk(KERN_ERR PREFIX "Invalid (NULL) 'bus' field\n");
507 return -ENODEV;
511 * First we check the PCI IRQ routing table (PRT) for an IRQ. PRT
512 * values override any BIOS-assigned IRQs set during boot.
514 irq = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
515 &triggering, &polarity, &link,
516 acpi_pci_allocate_irq);
519 * If no PRT entry was found, we'll try to derive an IRQ from the
520 * device's parent bridge.
522 if (irq < 0)
523 irq = acpi_pci_irq_derive(dev, pin, &triggering,
524 &polarity, &link,
525 acpi_pci_allocate_irq);
527 if (irq < 0) {
529 * IDE legacy mode controller IRQs are magic. Why do compat
530 * extensions always make such a nasty mess.
532 if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE &&
533 (dev->class & 0x05) == 0)
534 return 0;
537 * No IRQ known to the ACPI subsystem - maybe the BIOS /
538 * driver reported one, then use it. Exit in any case.
540 if (irq < 0) {
541 printk(KERN_WARNING PREFIX "PCI Interrupt %s[%c]: no GSI",
542 pci_name(dev), ('A' + pin));
543 /* Interrupt Line values above 0xF are forbidden */
544 if (dev->irq > 0 && (dev->irq <= 0xF)) {
545 printk(" - using IRQ %d\n", dev->irq);
546 acpi_register_gsi(dev->irq, ACPI_LEVEL_SENSITIVE,
547 ACPI_ACTIVE_LOW);
548 return 0;
549 } else {
550 printk("\n");
551 return 0;
555 rc = acpi_register_gsi(irq, triggering, polarity);
556 if (rc < 0) {
557 printk(KERN_WARNING PREFIX "PCI Interrupt %s[%c]: failed "
558 "to register GSI\n", pci_name(dev), ('A' + pin));
559 return rc;
561 dev->irq = rc;
563 printk(KERN_INFO PREFIX "PCI Interrupt %s[%c] -> ",
564 pci_name(dev), 'A' + pin);
566 if (link)
567 printk("Link [%s] -> ", link);
569 printk("GSI %u (%s, %s) -> IRQ %d\n", irq,
570 (triggering == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
571 (polarity == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
573 return 0;
576 /* FIXME: implement x86/x86_64 version */
577 void __attribute__ ((weak)) acpi_unregister_gsi(u32 i)
581 void acpi_pci_irq_disable(struct pci_dev *dev)
583 int gsi = 0;
584 u8 pin = 0;
585 int triggering = ACPI_LEVEL_SENSITIVE;
586 int polarity = ACPI_ACTIVE_LOW;
589 if (!dev || !dev->bus)
590 return;
592 pin = dev->pin;
593 if (!pin)
594 return;
595 pin--;
598 * First we check the PCI IRQ routing table (PRT) for an IRQ.
600 gsi = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
601 &triggering, &polarity, NULL,
602 acpi_pci_free_irq);
604 * If no PRT entry was found, we'll try to derive an IRQ from the
605 * device's parent bridge.
607 if (gsi < 0)
608 gsi = acpi_pci_irq_derive(dev, pin,
609 &triggering, &polarity, NULL,
610 acpi_pci_free_irq);
611 if (gsi < 0)
612 return;
615 * TBD: It might be worth clearing dev->irq by magic constant
616 * (e.g. PCI_UNDEFINED_IRQ).
619 printk(KERN_INFO PREFIX "PCI interrupt for device %s disabled\n",
620 pci_name(dev));
622 acpi_unregister_gsi(gsi);
624 return;