ACPI: PCI: ignore _PRT function information
[linux-2.6-block.git] / drivers / acpi / pci_irq.c
1 /*
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
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>
40
41 #define _COMPONENT              ACPI_PCI_COMPONENT
42 ACPI_MODULE_NAME("pci_irq");
43
44 static struct acpi_prt_list acpi_prt;
45 static DEFINE_SPINLOCK(acpi_prt_lock);
46
47 /* --------------------------------------------------------------------------
48                          PCI IRQ Routing Table (PRT) Support
49    -------------------------------------------------------------------------- */
50
51 static struct acpi_prt_entry *acpi_pci_irq_find_prt_entry(int segment,
52                                                           int bus,
53                                                           int device, int pin)
54 {
55         struct acpi_prt_entry *entry = NULL;
56
57         if (!acpi_prt.count)
58                 return NULL;
59
60         /*
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).
63          *
64          */
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;
73                 }
74         }
75
76         spin_unlock(&acpi_prt_lock);
77         return NULL;
78 }
79
80 /* http://bugzilla.kernel.org/show_bug.cgi?id=4773 */
81 static struct dmi_system_id medion_md9580[] = {
82         {
83                 .ident = "Medion MD9580-F laptop",
84                 .matches = {
85                         DMI_MATCH(DMI_SYS_VENDOR, "MEDIONNB"),
86                         DMI_MATCH(DMI_PRODUCT_NAME, "A555"),
87                 },
88         },
89         { }
90 };
91
92 /* http://bugzilla.kernel.org/show_bug.cgi?id=5044 */
93 static struct dmi_system_id dell_optiplex[] = {
94         {
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+"),
99                 },
100         },
101         { }
102 };
103
104 /* http://bugzilla.kernel.org/show_bug.cgi?id=10138 */
105 static struct dmi_system_id hp_t5710[] = {
106         {
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"),
112                 },
113         },
114         { }
115 };
116
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;
125 };
126
127 /*
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.
131  */
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"},
142 };
143
144 static void
145 do_prt_fixups(struct acpi_prt_entry *entry, struct acpi_pci_routing_table *prt)
146 {
147         int i;
148         struct prt_quirk *quirk;
149
150         for (i = 0; i < ARRAY_SIZE(prt_quirks); i++) {
151                 quirk = &prt_quirks[i];
152
153                 /* All current quirks involve link devices, not GSIs */
154                 if (!prt->source)
155                         continue;
156
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 PCI INT %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);
171                 }
172         }
173 }
174
175 static int
176 acpi_pci_irq_add_entry(acpi_handle handle,
177                        int segment, int bus, struct acpi_pci_routing_table *prt)
178 {
179         struct acpi_prt_entry *entry = NULL;
180
181         entry = kzalloc(sizeof(struct acpi_prt_entry), GFP_KERNEL);
182         if (!entry)
183                 return -ENOMEM;
184
185         entry->id.segment = segment;
186         entry->id.bus = bus;
187         entry->id.device = (prt->address >> 16) & 0xFFFF;
188         entry->pin = prt->pin;
189
190         do_prt_fixups(entry, prt);
191
192         /*
193          * Type 1: Dynamic
194          * ---------------
195          * The 'source' field specifies the PCI interrupt link device used to
196          * configure the IRQ assigned to this slot|dev|pin.  The 'source_index'
197          * indicates which resource descriptor in the resource template (of
198          * the link device) this interrupt is allocated from.
199          * 
200          * NOTE: Don't query the Link Device for IRQ information at this time
201          *       because Link Device enumeration may not have occurred yet
202          *       (e.g. exists somewhere 'below' this _PRT entry in the ACPI
203          *       namespace).
204          */
205         if (prt->source[0]) {
206                 acpi_get_handle(handle, prt->source, &entry->link.handle);
207                 entry->link.index = prt->source_index;
208         }
209         /*
210          * Type 2: Static
211          * --------------
212          * The 'source' field is NULL, and the 'source_index' field specifies
213          * the IRQ value, which is hardwired to specific interrupt inputs on
214          * the interrupt controller.
215          */
216         else
217                 entry->link.index = prt->source_index;
218
219         ACPI_DEBUG_PRINT_RAW((ACPI_DB_INFO,
220                               "      %04x:%02x:%02x[%c] -> %s[%d]\n",
221                               entry->id.segment, entry->id.bus,
222                               entry->id.device, ('A' + entry->pin), prt->source,
223                               entry->link.index));
224
225         spin_lock(&acpi_prt_lock);
226         list_add_tail(&entry->node, &acpi_prt.entries);
227         acpi_prt.count++;
228         spin_unlock(&acpi_prt_lock);
229
230         return 0;
231 }
232
233 static void
234 acpi_pci_irq_del_entry(int segment, int bus, struct acpi_prt_entry *entry)
235 {
236         if (segment == entry->id.segment && bus == entry->id.bus) {
237                 acpi_prt.count--;
238                 list_del(&entry->node);
239                 kfree(entry);
240         }
241 }
242
243 int acpi_pci_irq_add_prt(acpi_handle handle, int segment, int bus)
244 {
245         acpi_status status;
246         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
247         struct acpi_pci_routing_table *entry;
248         static int first_time = 1;
249
250         if (first_time) {
251                 acpi_prt.count = 0;
252                 INIT_LIST_HEAD(&acpi_prt.entries);
253                 first_time = 0;
254         }
255
256         /* 'handle' is the _PRT's parent (root bridge or PCI-PCI bridge) */
257         status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
258         if (ACPI_FAILURE(status))
259                 return -ENODEV;
260
261         printk(KERN_DEBUG "ACPI: PCI Interrupt Routing Table [%s._PRT]\n",
262                (char *) buffer.pointer);
263
264         kfree(buffer.pointer);
265
266         buffer.length = ACPI_ALLOCATE_BUFFER;
267         buffer.pointer = NULL;
268
269         status = acpi_get_irq_routing_table(handle, &buffer);
270         if (ACPI_FAILURE(status)) {
271                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRT [%s]",
272                                 acpi_format_exception(status)));
273                 kfree(buffer.pointer);
274                 return -ENODEV;
275         }
276
277         entry = buffer.pointer;
278         while (entry && (entry->length > 0)) {
279                 acpi_pci_irq_add_entry(handle, segment, bus, entry);
280                 entry = (struct acpi_pci_routing_table *)
281                     ((unsigned long)entry + entry->length);
282         }
283
284         kfree(buffer.pointer);
285         return 0;
286 }
287
288 void acpi_pci_irq_del_prt(int segment, int bus)
289 {
290         struct list_head *node = NULL, *n = NULL;
291         struct acpi_prt_entry *entry = NULL;
292
293         if (!acpi_prt.count) {
294                 return;
295         }
296
297         printk(KERN_DEBUG
298                "ACPI: Delete PCI Interrupt Routing Table for %04x:%02x\n",
299                segment, bus);
300         spin_lock(&acpi_prt_lock);
301         list_for_each_safe(node, n, &acpi_prt.entries) {
302                 entry = list_entry(node, struct acpi_prt_entry, node);
303
304                 acpi_pci_irq_del_entry(segment, bus, entry);
305         }
306         spin_unlock(&acpi_prt_lock);
307 }
308
309 /* --------------------------------------------------------------------------
310                           PCI Interrupt Routing Support
311    -------------------------------------------------------------------------- */
312 typedef int (*irq_lookup_func) (struct acpi_prt_entry *, int *, int *, char **);
313
314 static int
315 acpi_pci_allocate_irq(struct acpi_prt_entry *entry,
316                       int *triggering, int *polarity, char **link)
317 {
318         int irq;
319
320
321         if (entry->link.handle) {
322                 irq = acpi_pci_link_allocate_irq(entry->link.handle,
323                                                  entry->link.index, triggering,
324                                                  polarity, link);
325                 if (irq < 0) {
326                         printk(KERN_WARNING PREFIX
327                                       "Invalid IRQ link routing entry\n");
328                         return -1;
329                 }
330         } else {
331                 irq = entry->link.index;
332                 *triggering = ACPI_LEVEL_SENSITIVE;
333                 *polarity = ACPI_ACTIVE_LOW;
334         }
335
336         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found IRQ %d\n", irq));
337         return irq;
338 }
339
340 static int
341 acpi_pci_free_irq(struct acpi_prt_entry *entry,
342                   int *triggering, int *polarity, char **link)
343 {
344         int irq;
345
346         if (entry->link.handle) {
347                 irq = acpi_pci_link_free_irq(entry->link.handle);
348         } else {
349                 irq = entry->link.index;
350         }
351         return irq;
352 }
353
354 /*
355  * acpi_pci_irq_lookup
356  * success: return IRQ >= 0
357  * failure: return -1
358  */
359 static int
360 acpi_pci_irq_lookup(struct pci_bus *bus,
361                     int device,
362                     int pin,
363                     int *triggering,
364                     int *polarity, char **link, irq_lookup_func func)
365 {
366         struct acpi_prt_entry *entry = NULL;
367         int segment = pci_domain_nr(bus);
368         int bus_nr = bus->number;
369         int ret;
370
371
372         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
373                           "Searching for _PRT entry for %04x:%02x:%02x[%c]\n",
374                           segment, bus_nr, device, ('A' + pin)));
375
376         entry = acpi_pci_irq_find_prt_entry(segment, bus_nr, device, pin);
377         if (!entry) {
378                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "_PRT entry not found\n"));
379                 return -1;
380         }
381
382         ret = func(entry, triggering, polarity, link);
383         return ret;
384 }
385
386 /*
387  * acpi_pci_irq_derive
388  * success: return IRQ >= 0
389  * failure: return < 0
390  */
391 static int
392 acpi_pci_irq_derive(struct pci_dev *dev,
393                     int pin,
394                     int *triggering,
395                     int *polarity, char **link, irq_lookup_func func)
396 {
397         struct pci_dev *bridge = dev;
398         int irq = -1;
399         u8 bridge_pin = 0, orig_pin = pin;
400
401
402         /* 
403          * Attempt to derive an IRQ for this device from a parent bridge's
404          * PCI interrupt routing entry (eg. yenta bridge and add-in card bridge).
405          */
406         while (irq < 0 && bridge->bus->self) {
407                 pin = (pin + PCI_SLOT(bridge->devfn)) % 4;
408                 bridge = bridge->bus->self;
409
410                 if ((bridge->class >> 8) == PCI_CLASS_BRIDGE_CARDBUS) {
411                         /* PC card has the same IRQ as its cardbridge */
412                         bridge_pin = bridge->pin;
413                         if (!bridge_pin) {
414                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
415                                                   "No interrupt pin configured for device %s\n",
416                                                   pci_name(bridge)));
417                                 return -1;
418                         }
419                         /* Pin is from 0 to 3 */
420                         bridge_pin--;
421                         pin = bridge_pin;
422                 }
423
424                 irq = acpi_pci_irq_lookup(bridge->bus, PCI_SLOT(bridge->devfn),
425                                           pin, triggering, polarity,
426                                           link, func);
427         }
428
429         if (irq < 0) {
430                 dev_warn(&dev->dev, "can't derive routing for PCI INT %c\n",
431                          'A' + orig_pin);
432                 return -1;
433         }
434
435         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Derive IRQ %d for device %s from %s\n",
436                           irq, pci_name(dev), pci_name(bridge)));
437
438         return irq;
439 }
440
441 /*
442  * acpi_pci_irq_enable
443  * success: return 0
444  * failure: return < 0
445  */
446
447 int acpi_pci_irq_enable(struct pci_dev *dev)
448 {
449         int irq = 0;
450         u8 pin = 0;
451         int triggering = ACPI_LEVEL_SENSITIVE;
452         int polarity = ACPI_ACTIVE_LOW;
453         char *link = NULL;
454         char link_desc[16];
455         int rc;
456
457
458         pin = dev->pin;
459         if (!pin) {
460                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
461                                   "No interrupt pin configured for device %s\n",
462                                   pci_name(dev)));
463                 return 0;
464         }
465         pin--;
466
467         /* 
468          * First we check the PCI IRQ routing table (PRT) for an IRQ.  PRT
469          * values override any BIOS-assigned IRQs set during boot.
470          */
471         irq = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
472                                   &triggering, &polarity, &link,
473                                   acpi_pci_allocate_irq);
474
475         /*
476          * If no PRT entry was found, we'll try to derive an IRQ from the
477          * device's parent bridge.
478          */
479         if (irq < 0)
480                 irq = acpi_pci_irq_derive(dev, pin, &triggering,
481                                           &polarity, &link,
482                                           acpi_pci_allocate_irq);
483
484         if (irq < 0) {
485                 /*
486                  * IDE legacy mode controller IRQs are magic. Why do compat
487                  * extensions always make such a nasty mess.
488                  */
489                 if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE &&
490                                 (dev->class & 0x05) == 0)
491                         return 0;
492         }
493         /*
494          * No IRQ known to the ACPI subsystem - maybe the BIOS / 
495          * driver reported one, then use it. Exit in any case.
496          */
497         if (irq < 0) {
498                 dev_warn(&dev->dev, "PCI INT %c: no GSI", 'A' + pin);
499                 /* Interrupt Line values above 0xF are forbidden */
500                 if (dev->irq > 0 && (dev->irq <= 0xF)) {
501                         printk(" - using IRQ %d\n", dev->irq);
502                         acpi_register_gsi(dev->irq, ACPI_LEVEL_SENSITIVE,
503                                           ACPI_ACTIVE_LOW);
504                         return 0;
505                 } else {
506                         printk("\n");
507                         return 0;
508                 }
509         }
510
511         rc = acpi_register_gsi(irq, triggering, polarity);
512         if (rc < 0) {
513                 dev_warn(&dev->dev, "PCI INT %c: failed to register GSI\n",
514                          'A' + pin);
515                 return rc;
516         }
517         dev->irq = rc;
518
519         if (link)
520                 snprintf(link_desc, sizeof(link_desc), " -> Link[%s]", link);
521         else
522                 link_desc[0] = '\0';
523
524         dev_info(&dev->dev, "PCI INT %c%s -> GSI %u (%s, %s) -> IRQ %d\n",
525                  'A' + pin, link_desc, irq,
526                  (triggering == ACPI_LEVEL_SENSITIVE) ? "level" : "edge",
527                  (polarity == ACPI_ACTIVE_LOW) ? "low" : "high", dev->irq);
528
529         return 0;
530 }
531
532 /* FIXME: implement x86/x86_64 version */
533 void __attribute__ ((weak)) acpi_unregister_gsi(u32 i)
534 {
535 }
536
537 void acpi_pci_irq_disable(struct pci_dev *dev)
538 {
539         int gsi = 0;
540         u8 pin = 0;
541         int triggering = ACPI_LEVEL_SENSITIVE;
542         int polarity = ACPI_ACTIVE_LOW;
543
544
545         pin = dev->pin;
546         if (!pin)
547                 return;
548         pin--;
549
550         /*
551          * First we check the PCI IRQ routing table (PRT) for an IRQ.
552          */
553         gsi = acpi_pci_irq_lookup(dev->bus, PCI_SLOT(dev->devfn), pin,
554                                   &triggering, &polarity, NULL,
555                                   acpi_pci_free_irq);
556         /*
557          * If no PRT entry was found, we'll try to derive an IRQ from the
558          * device's parent bridge.
559          */
560         if (gsi < 0)
561                 gsi = acpi_pci_irq_derive(dev, pin,
562                                           &triggering, &polarity, NULL,
563                                           acpi_pci_free_irq);
564         if (gsi < 0)
565                 return;
566
567         /*
568          * TBD: It might be worth clearing dev->irq by magic constant
569          * (e.g. PCI_UNDEFINED_IRQ).
570          */
571
572         dev_info(&dev->dev, "PCI INT %c disabled\n", 'A' + pin);
573         acpi_unregister_gsi(gsi);
574 }