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