blob: c422e10337be779f2841f14e9b8f7ace8798aa37 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Low-Level PCI Support for PC -- Routing of Interrupts
3 *
4 * (c) 1999--2000 Martin Mares <mj@ucw.cz>
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/types.h>
8#include <linux/kernel.h>
9#include <linux/pci.h>
10#include <linux/init.h>
11#include <linux/slab.h>
12#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/dmi.h>
Miklos Vajna273c1122008-05-13 18:38:56 +020014#include <linux/io.h>
15#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/io_apic.h>
Nick Pigginb33fa1f2005-10-01 02:34:42 +100017#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/acpi.h>
19
20#include "pci.h"
21
22#define PIRQ_SIGNATURE (('$' << 0) + ('P' << 8) + ('I' << 16) + ('R' << 24))
23#define PIRQ_VERSION 0x0100
24
25static int broken_hp_bios_irq9;
26static int acer_tm360_irqrouting;
27
28static struct irq_routing_table *pirq_table;
29
30static int pirq_enable_irq(struct pci_dev *dev);
31
32/*
33 * Never use: 0, 1, 2 (timer, keyboard, and cascade)
34 * Avoid using: 13, 14 and 15 (FP error and IDE).
35 * Penalize: 3, 4, 6, 7, 12 (known ISA uses: serial, floppy, parallel and mouse)
36 */
37unsigned int pcibios_irq_mask = 0xfff8;
38
39static int pirq_penalty[16] = {
40 1000000, 1000000, 1000000, 1000, 1000, 0, 1000, 1000,
41 0, 0, 0, 0, 1000, 100000, 100000, 100000
42};
43
44struct irq_router {
45 char *name;
46 u16 vendor, device;
47 int (*get)(struct pci_dev *router, struct pci_dev *dev, int pirq);
Miklos Vajna273c1122008-05-13 18:38:56 +020048 int (*set)(struct pci_dev *router, struct pci_dev *dev, int pirq,
49 int new);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
52struct irq_router_handler {
53 u16 vendor;
54 int (*probe)(struct irq_router *r, struct pci_dev *router, u16 device);
55};
56
57int (*pcibios_enable_irq)(struct pci_dev *dev) = NULL;
David Shaohua Li87bec662005-07-27 23:02:00 -040058void (*pcibios_disable_irq)(struct pci_dev *dev) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
jayalk@intworks.biz120bb422005-03-21 20:20:42 -080061 * Check passed address for the PCI IRQ Routing Table signature
62 * and perform checksum verification.
63 */
64
Miklos Vajna273c1122008-05-13 18:38:56 +020065static inline struct irq_routing_table *pirq_check_routing_table(u8 *addr)
jayalk@intworks.biz120bb422005-03-21 20:20:42 -080066{
67 struct irq_routing_table *rt;
68 int i;
69 u8 sum;
70
71 rt = (struct irq_routing_table *) addr;
72 if (rt->signature != PIRQ_SIGNATURE ||
73 rt->version != PIRQ_VERSION ||
74 rt->size % 16 ||
75 rt->size < sizeof(struct irq_routing_table))
76 return NULL;
77 sum = 0;
Miklos Vajna273c1122008-05-13 18:38:56 +020078 for (i = 0; i < rt->size; i++)
jayalk@intworks.biz120bb422005-03-21 20:20:42 -080079 sum += addr[i];
80 if (!sum) {
Miklos Vajna273c1122008-05-13 18:38:56 +020081 DBG(KERN_DEBUG "PCI: Interrupt Routing Table found at 0x%p\n",
82 rt);
jayalk@intworks.biz120bb422005-03-21 20:20:42 -080083 return rt;
84 }
85 return NULL;
86}
87
88
89
90/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * Search 0xf0000 -- 0xfffff for the PCI IRQ Routing Table.
92 */
93
94static struct irq_routing_table * __init pirq_find_routing_table(void)
95{
96 u8 *addr;
97 struct irq_routing_table *rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
jayalk@intworks.biz120bb422005-03-21 20:20:42 -080099 if (pirq_table_addr) {
100 rt = pirq_check_routing_table((u8 *) __va(pirq_table_addr));
101 if (rt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return rt;
jayalk@intworks.biz120bb422005-03-21 20:20:42 -0800103 printk(KERN_WARNING "PCI: PIRQ table NOT found at pirqaddr\n");
104 }
Miklos Vajna273c1122008-05-13 18:38:56 +0200105 for (addr = (u8 *) __va(0xf0000); addr < (u8 *) __va(0x100000);
106 addr += 16) {
jayalk@intworks.biz120bb422005-03-21 20:20:42 -0800107 rt = pirq_check_routing_table(addr);
108 if (rt)
109 return rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111 return NULL;
112}
113
114/*
115 * If we have a IRQ routing table, use it to search for peer host
116 * bridges. It's a gross hack, but since there are no other known
117 * ways how to get a list of buses, we have to go this way.
118 */
119
120static void __init pirq_peer_trick(void)
121{
122 struct irq_routing_table *rt = pirq_table;
123 u8 busmap[256];
124 int i;
125 struct irq_info *e;
126
127 memset(busmap, 0, sizeof(busmap));
Miklos Vajna273c1122008-05-13 18:38:56 +0200128 for (i = 0; i < (rt->size - sizeof(struct irq_routing_table)) /
129 sizeof(struct irq_info); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 e = &rt->slots[i];
131#ifdef DEBUG
132 {
133 int j;
Miklos Vajna273c1122008-05-13 18:38:56 +0200134 DBG(KERN_DEBUG "%02x:%02x slot=%02x", e->bus,
135 e->devfn/8, e->slot);
136 for (j = 0; j < 4; j++)
137 DBG(" %d:%02x/%04x", j, e->irq[j].link,
138 e->irq[j].bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 DBG("\n");
140 }
141#endif
142 busmap[e->bus] = 1;
143 }
Miklos Vajna273c1122008-05-13 18:38:56 +0200144 for (i = 1; i < 256; i++) {
Yinghai Lu871d5f82008-02-19 03:20:09 -0800145 int node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!busmap[i] || pci_find_bus(0, i))
147 continue;
Yinghai Lu871d5f82008-02-19 03:20:09 -0800148 node = get_mp_bus_to_node(i);
149 if (pci_scan_bus_on_node(i, &pci_root_ops, node))
Muli Ben-Yehuda73c59af2007-08-10 13:01:19 -0700150 printk(KERN_INFO "PCI: Discovered primary peer "
151 "bus %02x [IRQ]\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153 pcibios_last_bus = -1;
154}
155
156/*
157 * Code for querying and setting of IRQ routes on various interrupt routers.
158 */
159
160void eisa_set_level_irq(unsigned int irq)
161{
162 unsigned char mask = 1 << (irq & 7);
163 unsigned int port = 0x4d0 + (irq >> 3);
164 unsigned char val;
165 static u16 eisa_irq_mask;
166
167 if (irq >= 16 || (1 << irq) & eisa_irq_mask)
168 return;
169
170 eisa_irq_mask |= (1 << irq);
Daniel Marjamäki81745512005-12-01 18:01:28 +0100171 printk(KERN_DEBUG "PCI: setting IRQ %u as level-triggered\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 val = inb(port);
173 if (!(val & mask)) {
Daniel Marjamäki81745512005-12-01 18:01:28 +0100174 DBG(KERN_DEBUG " -> edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 outb(val | mask, port);
176 }
177}
178
179/*
Simon Arlott27b46d72007-10-20 01:13:56 +0200180 * Common IRQ routing practice: nibbles in config space,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * offset by some magic constant.
182 */
Miklos Vajna273c1122008-05-13 18:38:56 +0200183static unsigned int
184read_config_nybble(struct pci_dev *router, unsigned offset, unsigned nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 u8 x;
187 unsigned reg = offset + (nr >> 1);
188
189 pci_read_config_byte(router, reg, &x);
190 return (nr & 1) ? (x >> 4) : (x & 0xf);
191}
192
Miklos Vajna273c1122008-05-13 18:38:56 +0200193static void write_config_nybble(struct pci_dev *router, unsigned offset,
194 unsigned nr, unsigned int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 u8 x;
197 unsigned reg = offset + (nr >> 1);
198
199 pci_read_config_byte(router, reg, &x);
200 x = (nr & 1) ? ((x & 0x0f) | (val << 4)) : ((x & 0xf0) | val);
201 pci_write_config_byte(router, reg, x);
202}
203
204/*
205 * ALI pirq entries are damn ugly, and completely undocumented.
206 * This has been figured out from pirq tables, and it's not a pretty
207 * picture.
208 */
209static int pirq_ali_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
210{
Miklos Vajna273c1122008-05-13 18:38:56 +0200211 static const unsigned char irqmap[16] = { 0, 9, 3, 10, 4, 5, 7, 6, 1,
212 11, 0, 12, 0, 14, 0, 15 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Björn Steinbrink223ac2f2008-03-31 04:22:53 +0200214 WARN_ON_ONCE(pirq > 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return irqmap[read_config_nybble(router, 0x48, pirq-1)];
216}
217
Miklos Vajna273c1122008-05-13 18:38:56 +0200218static int
219pirq_ali_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Miklos Vajna273c1122008-05-13 18:38:56 +0200221 static const unsigned char irqmap[16] = { 0, 8, 0, 2, 4, 5, 7, 6, 0, 1,
222 3, 9, 11, 0, 13, 15 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 unsigned int val = irqmap[irq];
Ingo Molnar7d409d62008-01-30 13:32:00 +0100224
Björn Steinbrink223ac2f2008-03-31 04:22:53 +0200225 WARN_ON_ONCE(pirq > 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (val) {
227 write_config_nybble(router, 0x48, pirq-1, val);
228 return 1;
229 }
230 return 0;
231}
232
233/*
234 * The Intel PIIX4 pirq rules are fairly simple: "pirq" is
235 * just a pointer to the config space.
236 */
237static int pirq_piix_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
238{
239 u8 x;
240
241 pci_read_config_byte(router, pirq, &x);
242 return (x < 16) ? x : 0;
243}
244
Miklos Vajna273c1122008-05-13 18:38:56 +0200245static int
246pirq_piix_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 pci_write_config_byte(router, pirq, irq);
249 return 1;
250}
251
252/*
253 * The VIA pirq rules are nibble-based, like ALI,
254 * but without the ugly irq number munging.
255 * However, PIRQD is in the upper instead of lower 4 bits.
256 */
257static int pirq_via_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
258{
259 return read_config_nybble(router, 0x55, pirq == 4 ? 5 : pirq);
260}
261
Miklos Vajna273c1122008-05-13 18:38:56 +0200262static int
263pirq_via_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 write_config_nybble(router, 0x55, pirq == 4 ? 5 : pirq, irq);
266 return 1;
267}
268
269/*
Aleksey Gorelov80bb82a2005-06-23 00:08:29 -0700270 * The VIA pirq rules are nibble-based, like ALI,
271 * but without the ugly irq number munging.
272 * However, for 82C586, nibble map is different .
273 */
Miklos Vajna273c1122008-05-13 18:38:56 +0200274static int
275pirq_via586_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
Aleksey Gorelov80bb82a2005-06-23 00:08:29 -0700276{
Daniel Ritzf3ac8432006-11-14 02:03:25 -0800277 static const unsigned int pirqmap[5] = { 3, 2, 5, 1, 1 };
Ingo Molnar7d409d62008-01-30 13:32:00 +0100278
Björn Steinbrink223ac2f2008-03-31 04:22:53 +0200279 WARN_ON_ONCE(pirq > 5);
Aleksey Gorelov80bb82a2005-06-23 00:08:29 -0700280 return read_config_nybble(router, 0x55, pirqmap[pirq-1]);
281}
282
Miklos Vajna273c1122008-05-13 18:38:56 +0200283static int
284pirq_via586_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
Aleksey Gorelov80bb82a2005-06-23 00:08:29 -0700285{
Daniel Ritzf3ac8432006-11-14 02:03:25 -0800286 static const unsigned int pirqmap[5] = { 3, 2, 5, 1, 1 };
Ingo Molnar7d409d62008-01-30 13:32:00 +0100287
Björn Steinbrink223ac2f2008-03-31 04:22:53 +0200288 WARN_ON_ONCE(pirq > 5);
Aleksey Gorelov80bb82a2005-06-23 00:08:29 -0700289 write_config_nybble(router, 0x55, pirqmap[pirq-1], irq);
290 return 1;
291}
292
293/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * ITE 8330G pirq rules are nibble-based
295 * FIXME: pirqmap may be { 1, 0, 3, 2 },
296 * 2+3 are both mapped to irq 9 on my system
297 */
298static int pirq_ite_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
299{
Andreas Mohra0de1f02006-06-23 02:04:28 -0700300 static const unsigned char pirqmap[4] = { 1, 0, 2, 3 };
Ingo Molnar7d409d62008-01-30 13:32:00 +0100301
Björn Steinbrink223ac2f2008-03-31 04:22:53 +0200302 WARN_ON_ONCE(pirq > 4);
Miklos Vajna273c1122008-05-13 18:38:56 +0200303 return read_config_nybble(router, 0x43, pirqmap[pirq-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Miklos Vajna273c1122008-05-13 18:38:56 +0200306static int
307pirq_ite_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Andreas Mohra0de1f02006-06-23 02:04:28 -0700309 static const unsigned char pirqmap[4] = { 1, 0, 2, 3 };
Ingo Molnar7d409d62008-01-30 13:32:00 +0100310
Björn Steinbrink223ac2f2008-03-31 04:22:53 +0200311 WARN_ON_ONCE(pirq > 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 write_config_nybble(router, 0x43, pirqmap[pirq-1], irq);
313 return 1;
314}
315
316/*
317 * OPTI: high four bits are nibble pointer..
318 * I wonder what the low bits do?
319 */
320static int pirq_opti_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
321{
322 return read_config_nybble(router, 0xb8, pirq >> 4);
323}
324
Miklos Vajna273c1122008-05-13 18:38:56 +0200325static int
326pirq_opti_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 write_config_nybble(router, 0xb8, pirq >> 4, irq);
329 return 1;
330}
331
332/*
333 * Cyrix: nibble offset 0x5C
Miklos Vajna273c1122008-05-13 18:38:56 +0200334 * 0x5C bits 7:4 is INTB bits 3:0 is INTA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * 0x5D bits 7:4 is INTD bits 3:0 is INTC
336 */
337static int pirq_cyrix_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
338{
339 return read_config_nybble(router, 0x5C, (pirq-1)^1);
340}
341
Miklos Vajna273c1122008-05-13 18:38:56 +0200342static int
343pirq_cyrix_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 write_config_nybble(router, 0x5C, (pirq-1)^1, irq);
346 return 1;
347}
348
349/*
350 * PIRQ routing for SiS 85C503 router used in several SiS chipsets.
351 * We have to deal with the following issues here:
352 * - vendors have different ideas about the meaning of link values
353 * - some onboard devices (integrated in the chipset) have special
354 * links and are thus routed differently (i.e. not via PCI INTA-INTD)
355 * - different revision of the router have a different layout for
356 * the routing registers, particularly for the onchip devices
357 *
358 * For all routing registers the common thing is we have one byte
359 * per routeable link which is defined as:
360 * bit 7 IRQ mapping enabled (0) or disabled (1)
361 * bits [6:4] reserved (sometimes used for onchip devices)
362 * bits [3:0] IRQ to map to
363 * allowed: 3-7, 9-12, 14-15
364 * reserved: 0, 1, 2, 8, 13
365 *
366 * The config-space registers located at 0x41/0x42/0x43/0x44 are
367 * always used to route the normal PCI INT A/B/C/D respectively.
368 * Apparently there are systems implementing PCI routing table using
369 * link values 0x01-0x04 and others using 0x41-0x44 for PCI INTA..D.
370 * We try our best to handle both link mappings.
Miklos Vajna273c1122008-05-13 18:38:56 +0200371 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * Currently (2003-05-21) it appears most SiS chipsets follow the
373 * definition of routing registers from the SiS-5595 southbridge.
374 * According to the SiS 5595 datasheets the revision id's of the
375 * router (ISA-bridge) should be 0x01 or 0xb0.
376 *
377 * Furthermore we've also seen lspci dumps with revision 0x00 and 0xb1.
378 * Looks like these are used in a number of SiS 5xx/6xx/7xx chipsets.
379 * They seem to work with the current routing code. However there is
380 * some concern because of the two USB-OHCI HCs (original SiS 5595
381 * had only one). YMMV.
382 *
383 * Onchip routing for router rev-id 0x01/0xb0 and probably 0x00/0xb1:
384 *
385 * 0x61: IDEIRQ:
386 * bits [6:5] must be written 01
387 * bit 4 channel-select primary (0), secondary (1)
388 *
389 * 0x62: USBIRQ:
390 * bit 6 OHCI function disabled (0), enabled (1)
Miklos Vajna273c1122008-05-13 18:38:56 +0200391 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 * 0x6a: ACPI/SCI IRQ: bits 4-6 reserved
393 *
394 * 0x7e: Data Acq. Module IRQ - bits 4-6 reserved
395 *
396 * We support USBIRQ (in addition to INTA-INTD) and keep the
397 * IDE, ACPI and DAQ routing untouched as set by the BIOS.
398 *
399 * Currently the only reported exception is the new SiS 65x chipset
400 * which includes the SiS 69x southbridge. Here we have the 85C503
401 * router revision 0x04 and there are changes in the register layout
402 * mostly related to the different USB HCs with USB 2.0 support.
403 *
404 * Onchip routing for router rev-id 0x04 (try-and-error observation)
405 *
406 * 0x60/0x61/0x62/0x63: 1xEHCI and 3xOHCI (companion) USB-HCs
407 * bit 6-4 are probably unused, not like 5595
408 */
409
410#define PIRQ_SIS_IRQ_MASK 0x0f
411#define PIRQ_SIS_IRQ_DISABLE 0x80
412#define PIRQ_SIS_USB_ENABLE 0x40
413
414static int pirq_sis_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
415{
416 u8 x;
417 int reg;
418
419 reg = pirq;
420 if (reg >= 0x01 && reg <= 0x04)
421 reg += 0x40;
422 pci_read_config_byte(router, reg, &x);
423 return (x & PIRQ_SIS_IRQ_DISABLE) ? 0 : (x & PIRQ_SIS_IRQ_MASK);
424}
425
Miklos Vajna273c1122008-05-13 18:38:56 +0200426static int
427pirq_sis_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 u8 x;
430 int reg;
431
432 reg = pirq;
433 if (reg >= 0x01 && reg <= 0x04)
434 reg += 0x40;
435 pci_read_config_byte(router, reg, &x);
436 x &= ~(PIRQ_SIS_IRQ_MASK | PIRQ_SIS_IRQ_DISABLE);
437 x |= irq ? irq: PIRQ_SIS_IRQ_DISABLE;
438 pci_write_config_byte(router, reg, x);
439 return 1;
440}
441
442
443/*
444 * VLSI: nibble offset 0x74 - educated guess due to routing table and
445 * config space of VLSI 82C534 PCI-bridge/router (1004:0102)
446 * Tested on HP OmniBook 800 covering PIRQ 1, 2, 4, 8 for onboard
447 * devices, PIRQ 3 for non-pci(!) soundchip and (untested) PIRQ 6
448 * for the busbridge to the docking station.
449 */
450
451static int pirq_vlsi_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
452{
Ingo Molnar7d409d62008-01-30 13:32:00 +0100453 WARN_ON_ONCE(pirq >= 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (pirq > 8) {
455 printk(KERN_INFO "VLSI router pirq escape (%d)\n", pirq);
456 return 0;
457 }
458 return read_config_nybble(router, 0x74, pirq-1);
459}
460
Miklos Vajna273c1122008-05-13 18:38:56 +0200461static int
462pirq_vlsi_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Ingo Molnar7d409d62008-01-30 13:32:00 +0100464 WARN_ON_ONCE(pirq >= 9);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (pirq > 8) {
466 printk(KERN_INFO "VLSI router pirq escape (%d)\n", pirq);
467 return 0;
468 }
469 write_config_nybble(router, 0x74, pirq-1, irq);
470 return 1;
471}
472
473/*
474 * ServerWorks: PCI interrupts mapped to system IRQ lines through Index
475 * and Redirect I/O registers (0x0c00 and 0x0c01). The Index register
476 * format is (PCIIRQ## | 0x10), e.g.: PCIIRQ10=0x1a. The Redirect
477 * register is a straight binary coding of desired PIC IRQ (low nibble).
478 *
479 * The 'link' value in the PIRQ table is already in the correct format
480 * for the Index register. There are some special index values:
481 * 0x00 for ACPI (SCI), 0x01 for USB, 0x02 for IDE0, 0x04 for IDE1,
482 * and 0x03 for SMBus.
483 */
Miklos Vajna273c1122008-05-13 18:38:56 +0200484static int
485pirq_serverworks_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Alan Coxc11b68b2008-01-30 13:33:14 +0100487 outb(pirq, 0xc00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return inb(0xc01) & 0xf;
489}
490
Miklos Vajna273c1122008-05-13 18:38:56 +0200491static int pirq_serverworks_set(struct pci_dev *router, struct pci_dev *dev,
492 int pirq, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Alan Coxc11b68b2008-01-30 13:33:14 +0100494 outb(pirq, 0xc00);
495 outb(irq, 0xc01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return 1;
497}
498
499/* Support for AMD756 PCI IRQ Routing
500 * Jhon H. Caicedo <jhcaiced@osso.org.co>
501 * Jun/21/2001 0.2.0 Release, fixed to use "nybble" functions... (jhcaiced)
502 * Jun/19/2001 Alpha Release 0.1.0 (jhcaiced)
503 * The AMD756 pirq rules are nibble-based
504 * offset 0x56 0-3 PIRQA 4-7 PIRQB
505 * offset 0x57 0-3 PIRQC 4-7 PIRQD
506 */
Miklos Vajna273c1122008-05-13 18:38:56 +0200507static int
508pirq_amd756_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 u8 irq;
511 irq = 0;
512 if (pirq <= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 irq = read_config_nybble(router, 0x56, pirq - 1);
Miklos Vajna273c1122008-05-13 18:38:56 +0200514 printk(KERN_INFO
515 "AMD756: dev %04x:%04x, router pirq : %d get irq : %2d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 dev->vendor, dev->device, pirq, irq);
517 return irq;
518}
519
Miklos Vajna273c1122008-05-13 18:38:56 +0200520static int
521pirq_amd756_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Miklos Vajna273c1122008-05-13 18:38:56 +0200523 printk(KERN_INFO
524 "AMD756: dev %04x:%04x, router pirq : %d SET irq : %2d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 dev->vendor, dev->device, pirq, irq);
526 if (pirq <= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 write_config_nybble(router, 0x56, pirq - 1, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return 1;
529}
530
Thomas Backlundb205f6b2007-08-10 14:42:15 -0700531/*
532 * PicoPower PT86C523
533 */
534static int pirq_pico_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
535{
536 outb(0x10 + ((pirq - 1) >> 1), 0x24);
537 return ((pirq - 1) & 1) ? (inb(0x26) >> 4) : (inb(0x26) & 0xf);
538}
539
540static int pirq_pico_set(struct pci_dev *router, struct pci_dev *dev, int pirq,
541 int irq)
542{
543 unsigned int x;
544 outb(0x10 + ((pirq - 1) >> 1), 0x24);
545 x = inb(0x26);
546 x = ((pirq - 1) & 1) ? ((x & 0x0f) | (irq << 4)) : ((x & 0xf0) | (irq));
547 outb(x, 0x26);
548 return 1;
549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#ifdef CONFIG_PCI_BIOS
552
Miklos Vajna273c1122008-05-13 18:38:56 +0200553static int
554pirq_bios_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 struct pci_dev *bridge;
557 int pin = pci_get_interrupt_pin(dev, &bridge);
558 return pcibios_set_irq_routing(bridge, pin, irq);
559}
560
561#endif
562
Miklos Vajna273c1122008-05-13 18:38:56 +0200563static __init int
564intel_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Andreas Mohra0de1f02006-06-23 02:04:28 -0700566 static struct pci_device_id __initdata pirq_440gx[] = {
Miklos Vajna273c1122008-05-13 18:38:56 +0200567 { PCI_DEVICE(PCI_VENDOR_ID_INTEL,
568 PCI_DEVICE_ID_INTEL_82443GX_0) },
569 { PCI_DEVICE(PCI_VENDOR_ID_INTEL,
570 PCI_DEVICE_ID_INTEL_82443GX_2) },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 { },
572 };
573
574 /* 440GX has a proprietary PIRQ router -- don't use it */
575 if (pci_dev_present(pirq_440gx))
576 return 0;
577
Miklos Vajna273c1122008-05-13 18:38:56 +0200578 switch (device) {
579 case PCI_DEVICE_ID_INTEL_82371FB_0:
580 case PCI_DEVICE_ID_INTEL_82371SB_0:
581 case PCI_DEVICE_ID_INTEL_82371AB_0:
582 case PCI_DEVICE_ID_INTEL_82371MX:
583 case PCI_DEVICE_ID_INTEL_82443MX_0:
584 case PCI_DEVICE_ID_INTEL_82801AA_0:
585 case PCI_DEVICE_ID_INTEL_82801AB_0:
586 case PCI_DEVICE_ID_INTEL_82801BA_0:
587 case PCI_DEVICE_ID_INTEL_82801BA_10:
588 case PCI_DEVICE_ID_INTEL_82801CA_0:
589 case PCI_DEVICE_ID_INTEL_82801CA_12:
590 case PCI_DEVICE_ID_INTEL_82801DB_0:
591 case PCI_DEVICE_ID_INTEL_82801E_0:
592 case PCI_DEVICE_ID_INTEL_82801EB_0:
593 case PCI_DEVICE_ID_INTEL_ESB_1:
594 case PCI_DEVICE_ID_INTEL_ICH6_0:
595 case PCI_DEVICE_ID_INTEL_ICH6_1:
596 case PCI_DEVICE_ID_INTEL_ICH7_0:
597 case PCI_DEVICE_ID_INTEL_ICH7_1:
598 case PCI_DEVICE_ID_INTEL_ICH7_30:
599 case PCI_DEVICE_ID_INTEL_ICH7_31:
600 case PCI_DEVICE_ID_INTEL_ESB2_0:
601 case PCI_DEVICE_ID_INTEL_ICH8_0:
602 case PCI_DEVICE_ID_INTEL_ICH8_1:
603 case PCI_DEVICE_ID_INTEL_ICH8_2:
604 case PCI_DEVICE_ID_INTEL_ICH8_3:
605 case PCI_DEVICE_ID_INTEL_ICH8_4:
606 case PCI_DEVICE_ID_INTEL_ICH9_0:
607 case PCI_DEVICE_ID_INTEL_ICH9_1:
608 case PCI_DEVICE_ID_INTEL_ICH9_2:
609 case PCI_DEVICE_ID_INTEL_ICH9_3:
610 case PCI_DEVICE_ID_INTEL_ICH9_4:
611 case PCI_DEVICE_ID_INTEL_ICH9_5:
612 case PCI_DEVICE_ID_INTEL_TOLAPAI_0:
613 case PCI_DEVICE_ID_INTEL_ICH10_0:
614 case PCI_DEVICE_ID_INTEL_ICH10_1:
615 case PCI_DEVICE_ID_INTEL_ICH10_2:
616 case PCI_DEVICE_ID_INTEL_ICH10_3:
617 r->name = "PIIX/ICH";
618 r->get = pirq_piix_get;
619 r->set = pirq_piix_set;
620 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622 return 0;
623}
624
aleksey_gorelov@phoenix.com750deaa2005-10-30 14:59:36 -0800625static __init int via_router_probe(struct irq_router *r,
626 struct pci_dev *router, u16 device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
628 /* FIXME: We should move some of the quirk fixup stuff here */
Giancarlo Formicucciaac122592005-07-28 01:07:33 -0700629
aleksey_gorelov@phoenix.com750deaa2005-10-30 14:59:36 -0800630 /*
Simon Arlott27b46d72007-10-20 01:13:56 +0200631 * workarounds for some buggy BIOSes
aleksey_gorelov@phoenix.com750deaa2005-10-30 14:59:36 -0800632 */
633 if (device == PCI_DEVICE_ID_VIA_82C586_0) {
Miklos Vajna273c1122008-05-13 18:38:56 +0200634 switch (router->device) {
aleksey_gorelov@phoenix.com750deaa2005-10-30 14:59:36 -0800635 case PCI_DEVICE_ID_VIA_82C686:
636 /*
637 * Asus k7m bios wrongly reports 82C686A
638 * as 586-compatible
639 */
640 device = PCI_DEVICE_ID_VIA_82C686;
641 break;
642 case PCI_DEVICE_ID_VIA_8235:
643 /**
644 * Asus a7v-x bios wrongly reports 8235
645 * as 586-compatible
646 */
647 device = PCI_DEVICE_ID_VIA_8235;
648 break;
Bertram Felgenhauer9f67fd52008-06-05 15:31:22 -0700649 case PCI_DEVICE_ID_VIA_8237:
650 /**
651 * Asus a7v600 bios wrongly reports 8237
652 * as 586-compatible
653 */
654 device = PCI_DEVICE_ID_VIA_8237;
655 break;
aleksey_gorelov@phoenix.com750deaa2005-10-30 14:59:36 -0800656 }
Giancarlo Formicucciaac122592005-07-28 01:07:33 -0700657 }
658
Miklos Vajna273c1122008-05-13 18:38:56 +0200659 switch (device) {
aleksey_gorelov@phoenix.com750deaa2005-10-30 14:59:36 -0800660 case PCI_DEVICE_ID_VIA_82C586_0:
661 r->name = "VIA";
662 r->get = pirq_via586_get;
663 r->set = pirq_via586_set;
664 return 1;
665 case PCI_DEVICE_ID_VIA_82C596:
666 case PCI_DEVICE_ID_VIA_82C686:
667 case PCI_DEVICE_ID_VIA_8231:
Grzegorz Janoszka40d8b892006-03-21 11:57:19 +0100668 case PCI_DEVICE_ID_VIA_8233A:
aleksey_gorelov@phoenix.com750deaa2005-10-30 14:59:36 -0800669 case PCI_DEVICE_ID_VIA_8235:
Grzegorz Janoszka40d8b892006-03-21 11:57:19 +0100670 case PCI_DEVICE_ID_VIA_8237:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* FIXME: add new ones for 8233/5 */
aleksey_gorelov@phoenix.com750deaa2005-10-30 14:59:36 -0800672 r->name = "VIA";
673 r->get = pirq_via_get;
674 r->set = pirq_via_set;
675 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 return 0;
678}
679
Miklos Vajna273c1122008-05-13 18:38:56 +0200680static __init int
681vlsi_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Miklos Vajna273c1122008-05-13 18:38:56 +0200683 switch (device) {
684 case PCI_DEVICE_ID_VLSI_82C534:
685 r->name = "VLSI 82C534";
686 r->get = pirq_vlsi_get;
687 r->set = pirq_vlsi_set;
688 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690 return 0;
691}
692
693
Miklos Vajna273c1122008-05-13 18:38:56 +0200694static __init int serverworks_router_probe(struct irq_router *r,
695 struct pci_dev *router, u16 device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Miklos Vajna273c1122008-05-13 18:38:56 +0200697 switch (device) {
698 case PCI_DEVICE_ID_SERVERWORKS_OSB4:
699 case PCI_DEVICE_ID_SERVERWORKS_CSB5:
700 r->name = "ServerWorks";
701 r->get = pirq_serverworks_get;
702 r->set = pirq_serverworks_set;
703 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705 return 0;
706}
707
Miklos Vajna273c1122008-05-13 18:38:56 +0200708static __init int
709sis_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 if (device != PCI_DEVICE_ID_SI_503)
712 return 0;
Miklos Vajna273c1122008-05-13 18:38:56 +0200713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 r->name = "SIS";
715 r->get = pirq_sis_get;
716 r->set = pirq_sis_set;
717 return 1;
718}
719
Miklos Vajna273c1122008-05-13 18:38:56 +0200720static __init int
721cyrix_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Miklos Vajna273c1122008-05-13 18:38:56 +0200723 switch (device) {
724 case PCI_DEVICE_ID_CYRIX_5520:
725 r->name = "NatSemi";
726 r->get = pirq_cyrix_get;
727 r->set = pirq_cyrix_set;
728 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730 return 0;
731}
732
Miklos Vajna273c1122008-05-13 18:38:56 +0200733static __init int
734opti_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Miklos Vajna273c1122008-05-13 18:38:56 +0200736 switch (device) {
737 case PCI_DEVICE_ID_OPTI_82C700:
738 r->name = "OPTI";
739 r->get = pirq_opti_get;
740 r->set = pirq_opti_set;
741 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743 return 0;
744}
745
Miklos Vajna273c1122008-05-13 18:38:56 +0200746static __init int
747ite_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Miklos Vajna273c1122008-05-13 18:38:56 +0200749 switch (device) {
750 case PCI_DEVICE_ID_ITE_IT8330G_0:
751 r->name = "ITE";
752 r->get = pirq_ite_get;
753 r->set = pirq_ite_set;
754 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756 return 0;
757}
758
Miklos Vajna273c1122008-05-13 18:38:56 +0200759static __init int
760ali_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Miklos Vajna273c1122008-05-13 18:38:56 +0200762 switch (device) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 case PCI_DEVICE_ID_AL_M1533:
764 case PCI_DEVICE_ID_AL_M1563:
Daniel Marjamäki81745512005-12-01 18:01:28 +0100765 printk(KERN_DEBUG "PCI: Using ALI IRQ Router\n");
766 r->name = "ALI";
767 r->get = pirq_ali_get;
768 r->set = pirq_ali_set;
769 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771 return 0;
772}
773
Miklos Vajna273c1122008-05-13 18:38:56 +0200774static __init int
775amd_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Miklos Vajna273c1122008-05-13 18:38:56 +0200777 switch (device) {
778 case PCI_DEVICE_ID_AMD_VIPER_740B:
779 r->name = "AMD756";
780 break;
781 case PCI_DEVICE_ID_AMD_VIPER_7413:
782 r->name = "AMD766";
783 break;
784 case PCI_DEVICE_ID_AMD_VIPER_7443:
785 r->name = "AMD768";
786 break;
787 default:
788 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790 r->get = pirq_amd756_get;
791 r->set = pirq_amd756_set;
792 return 1;
793}
Miklos Vajna273c1122008-05-13 18:38:56 +0200794
795static __init int
796pico_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
Thomas Backlundb205f6b2007-08-10 14:42:15 -0700797{
798 switch (device) {
799 case PCI_DEVICE_ID_PICOPOWER_PT86C523:
800 r->name = "PicoPower PT86C523";
801 r->get = pirq_pico_get;
802 r->set = pirq_pico_set;
803 return 1;
804
805 case PCI_DEVICE_ID_PICOPOWER_PT86C523BBP:
806 r->name = "PicoPower PT86C523 rev. BB+";
807 r->get = pirq_pico_get;
808 r->set = pirq_pico_set;
809 return 1;
810 }
811 return 0;
812}
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814static __initdata struct irq_router_handler pirq_routers[] = {
815 { PCI_VENDOR_ID_INTEL, intel_router_probe },
816 { PCI_VENDOR_ID_AL, ali_router_probe },
817 { PCI_VENDOR_ID_ITE, ite_router_probe },
818 { PCI_VENDOR_ID_VIA, via_router_probe },
819 { PCI_VENDOR_ID_OPTI, opti_router_probe },
820 { PCI_VENDOR_ID_SI, sis_router_probe },
821 { PCI_VENDOR_ID_CYRIX, cyrix_router_probe },
822 { PCI_VENDOR_ID_VLSI, vlsi_router_probe },
823 { PCI_VENDOR_ID_SERVERWORKS, serverworks_router_probe },
824 { PCI_VENDOR_ID_AMD, amd_router_probe },
Thomas Backlundb205f6b2007-08-10 14:42:15 -0700825 { PCI_VENDOR_ID_PICOPOWER, pico_router_probe },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* Someone with docs needs to add the ATI Radeon IGP */
827 { 0, NULL }
828};
829static struct irq_router pirq_router;
830static struct pci_dev *pirq_router_dev;
831
832
833/*
834 * FIXME: should we have an option to say "generic for
835 * chipset" ?
836 */
Miklos Vajna273c1122008-05-13 18:38:56 +0200837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838static void __init pirq_find_router(struct irq_router *r)
839{
840 struct irq_routing_table *rt = pirq_table;
841 struct irq_router_handler *h;
842
843#ifdef CONFIG_PCI_BIOS
844 if (!rt->signature) {
845 printk(KERN_INFO "PCI: Using BIOS for IRQ routing\n");
846 r->set = pirq_bios_set;
847 r->name = "BIOS";
848 return;
849 }
850#endif
851
852 /* Default unless a driver reloads it */
853 r->name = "default";
854 r->get = NULL;
855 r->set = NULL;
Miklos Vajna273c1122008-05-13 18:38:56 +0200856
Daniel Marjamäki81745512005-12-01 18:01:28 +0100857 DBG(KERN_DEBUG "PCI: Attempting to find IRQ router for %04x:%04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 rt->rtr_vendor, rt->rtr_device);
859
Alan Cox7cd8b682006-12-07 02:14:03 +0100860 pirq_router_dev = pci_get_bus_and_slot(rt->rtr_bus, rt->rtr_devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (!pirq_router_dev) {
Daniel Marjamäki81745512005-12-01 18:01:28 +0100862 DBG(KERN_DEBUG "PCI: Interrupt router not found at "
863 "%02x:%02x\n", rt->rtr_bus, rt->rtr_devfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return;
865 }
866
Miklos Vajna273c1122008-05-13 18:38:56 +0200867 for (h = pirq_routers; h->vendor; h++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* First look for a router match */
Miklos Vajna273c1122008-05-13 18:38:56 +0200869 if (rt->rtr_vendor == h->vendor &&
870 h->probe(r, pirq_router_dev, rt->rtr_device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 break;
872 /* Fall back to a device match */
Miklos Vajna273c1122008-05-13 18:38:56 +0200873 if (pirq_router_dev->vendor == h->vendor &&
874 h->probe(r, pirq_router_dev, pirq_router_dev->device))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 break;
876 }
877 printk(KERN_INFO "PCI: Using IRQ router %s [%04x/%04x] at %s\n",
878 pirq_router.name,
879 pirq_router_dev->vendor,
880 pirq_router_dev->device,
881 pci_name(pirq_router_dev));
Alan Cox7cd8b682006-12-07 02:14:03 +0100882
883 /* The device remains referenced for the kernel lifetime */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
885
886static struct irq_info *pirq_get_info(struct pci_dev *dev)
887{
888 struct irq_routing_table *rt = pirq_table;
Miklos Vajna273c1122008-05-13 18:38:56 +0200889 int entries = (rt->size - sizeof(struct irq_routing_table)) /
890 sizeof(struct irq_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 struct irq_info *info;
892
893 for (info = rt->slots; entries--; info++)
Miklos Vajna273c1122008-05-13 18:38:56 +0200894 if (info->bus == dev->bus->number &&
895 PCI_SLOT(info->devfn) == PCI_SLOT(dev->devfn))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return info;
897 return NULL;
898}
899
900static int pcibios_lookup_irq(struct pci_dev *dev, int assign)
901{
902 u8 pin;
903 struct irq_info *info;
904 int i, pirq, newirq;
905 int irq = 0;
906 u32 mask;
907 struct irq_router *r = &pirq_router;
908 struct pci_dev *dev2 = NULL;
909 char *msg = NULL;
910
911 /* Find IRQ pin */
912 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
913 if (!pin) {
Daniel Marjamäki81745512005-12-01 18:01:28 +0100914 DBG(KERN_DEBUG " -> no interrupt pin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return 0;
916 }
917 pin = pin - 1;
918
919 /* Find IRQ routing entry */
920
921 if (!pirq_table)
922 return 0;
Miklos Vajna273c1122008-05-13 18:38:56 +0200923
Daniel Marjamäki81745512005-12-01 18:01:28 +0100924 DBG(KERN_DEBUG "IRQ for %s[%c]", pci_name(dev), 'A' + pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 info = pirq_get_info(dev);
926 if (!info) {
Daniel Marjamäki81745512005-12-01 18:01:28 +0100927 DBG(" -> not found in routing table\n" KERN_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return 0;
929 }
930 pirq = info->irq[pin].link;
931 mask = info->irq[pin].bitmap;
932 if (!pirq) {
Daniel Marjamäki81745512005-12-01 18:01:28 +0100933 DBG(" -> not routed\n" KERN_DEBUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return 0;
935 }
Miklos Vajna273c1122008-05-13 18:38:56 +0200936 DBG(" -> PIRQ %02x, mask %04x, excl %04x", pirq, mask,
937 pirq_table->exclusive_irqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 mask &= pcibios_irq_mask;
939
940 /* Work around broken HP Pavilion Notebooks which assign USB to
941 IRQ 9 even though it is actually wired to IRQ 11 */
942
943 if (broken_hp_bios_irq9 && pirq == 0x59 && dev->irq == 9) {
944 dev->irq = 11;
945 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, 11);
946 r->set(pirq_router_dev, dev, pirq, 11);
947 }
948
949 /* same for Acer Travelmate 360, but with CB and irq 11 -> 10 */
Miklos Vajna273c1122008-05-13 18:38:56 +0200950 if (acer_tm360_irqrouting && dev->irq == 11 &&
951 dev->vendor == PCI_VENDOR_ID_O2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 pirq = 0x68;
953 mask = 0x400;
954 dev->irq = r->get(pirq_router_dev, dev, pirq);
955 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
956 }
957
958 /*
959 * Find the best IRQ to assign: use the one
960 * reported by the device if possible.
961 */
962 newirq = dev->irq;
Linus Torvalds7ed409182006-01-06 08:43:16 -0800963 if (newirq && !((1 << newirq) & mask)) {
Miklos Vajna273c1122008-05-13 18:38:56 +0200964 if (pci_probe & PCI_USE_PIRQ_MASK)
965 newirq = 0;
966 else
967 printk("\n" KERN_WARNING
968 "PCI: IRQ %i for device %s doesn't match PIRQ mask - try pci=usepirqmask\n"
969 KERN_DEBUG, newirq,
970 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972 if (!newirq && assign) {
973 for (i = 0; i < 16; i++) {
974 if (!(mask & (1 << i)))
975 continue;
Miklos Vajna273c1122008-05-13 18:38:56 +0200976 if (pirq_penalty[i] < pirq_penalty[newirq] &&
977 can_request_irq(i, IRQF_SHARED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 newirq = i;
979 }
980 }
981 DBG(" -> newirq=%d", newirq);
982
983 /* Check if it is hardcoded */
984 if ((pirq & 0xf0) == 0xf0) {
985 irq = pirq & 0xf;
986 DBG(" -> hardcoded IRQ %d\n", irq);
987 msg = "Hardcoded";
Miklos Vajna273c1122008-05-13 18:38:56 +0200988 } else if (r->get && (irq = r->get(pirq_router_dev, dev, pirq)) && \
989 ((!(pci_probe & PCI_USE_PIRQ_MASK)) || ((1 << irq) & mask))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 DBG(" -> got IRQ %d\n", irq);
991 msg = "Found";
Linus Torvaldsafa024c2006-06-23 02:04:21 -0700992 eisa_set_level_irq(irq);
Miklos Vajna273c1122008-05-13 18:38:56 +0200993 } else if (newirq && r->set &&
994 (dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 DBG(" -> assigning IRQ %d", newirq);
996 if (r->set(pirq_router_dev, dev, pirq, newirq)) {
997 eisa_set_level_irq(newirq);
998 DBG(" ... OK\n");
999 msg = "Assigned";
1000 irq = newirq;
1001 }
1002 }
1003
1004 if (!irq) {
1005 DBG(" ... failed\n");
1006 if (newirq && mask == (1 << newirq)) {
1007 msg = "Guessed";
1008 irq = newirq;
1009 } else
1010 return 0;
1011 }
Miklos Vajna273c1122008-05-13 18:38:56 +02001012 printk(KERN_INFO "PCI: %s IRQ %d for device %s\n", msg, irq,
1013 pci_name(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 /* Update IRQ for all devices with the same pirq value */
1016 while ((dev2 = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev2)) != NULL) {
1017 pci_read_config_byte(dev2, PCI_INTERRUPT_PIN, &pin);
1018 if (!pin)
1019 continue;
1020 pin--;
1021 info = pirq_get_info(dev2);
1022 if (!info)
1023 continue;
1024 if (info->irq[pin].link == pirq) {
Miklos Vajna273c1122008-05-13 18:38:56 +02001025 /*
1026 * We refuse to override the dev->irq
1027 * information. Give a warning!
1028 */
1029 if (dev2->irq && dev2->irq != irq && \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 (!(pci_probe & PCI_USE_PIRQ_MASK) || \
Miklos Vajna273c1122008-05-13 18:38:56 +02001031 ((1 << dev2->irq) & mask))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032#ifndef CONFIG_PCI_MSI
Miklos Vajna273c1122008-05-13 18:38:56 +02001033 printk(KERN_INFO
1034 "IRQ routing conflict for %s, have irq %d, want irq %d\n",
1035 pci_name(dev2), dev2->irq, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036#endif
Miklos Vajna273c1122008-05-13 18:38:56 +02001037 continue;
1038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 dev2->irq = irq;
1040 pirq_penalty[irq]++;
1041 if (dev != dev2)
Miklos Vajna273c1122008-05-13 18:38:56 +02001042 printk(KERN_INFO
1043 "PCI: Sharing IRQ %d with %s\n",
1044 irq, pci_name(dev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046 }
1047 return 1;
1048}
1049
1050static void __init pcibios_fixup_irqs(void)
1051{
1052 struct pci_dev *dev = NULL;
1053 u8 pin;
1054
Daniel Marjamäki81745512005-12-01 18:01:28 +01001055 DBG(KERN_DEBUG "PCI: IRQ fixup\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
1057 /*
Miklos Vajna273c1122008-05-13 18:38:56 +02001058 * If the BIOS has set an out of range IRQ number, just
1059 * ignore it. Also keep track of which IRQ's are
1060 * already in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
1062 if (dev->irq >= 16) {
Miklos Vajna273c1122008-05-13 18:38:56 +02001063 DBG(KERN_DEBUG "%s: ignoring bogus IRQ %d\n",
1064 pci_name(dev), dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 dev->irq = 0;
1066 }
Miklos Vajna273c1122008-05-13 18:38:56 +02001067 /*
1068 * If the IRQ is already assigned to a PCI device,
1069 * ignore its ISA use penalty
1070 */
1071 if (pirq_penalty[dev->irq] >= 100 &&
1072 pirq_penalty[dev->irq] < 100000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 pirq_penalty[dev->irq] = 0;
1074 pirq_penalty[dev->irq]++;
1075 }
1076
1077 dev = NULL;
1078 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
1079 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
1080#ifdef CONFIG_X86_IO_APIC
1081 /*
1082 * Recalculate IRQ numbers if we use the I/O APIC.
1083 */
Miklos Vajna273c1122008-05-13 18:38:56 +02001084 if (io_apic_assign_pci_irqs) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 int irq;
1086
1087 if (pin) {
Miklos Vajna273c1122008-05-13 18:38:56 +02001088 /*
1089 * interrupt pins are numbered starting
1090 * from 1
1091 */
1092 pin--;
1093 irq = IO_APIC_get_PCI_irq_vector(dev->bus->number,
1094 PCI_SLOT(dev->devfn), pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 /*
1096 * Busses behind bridges are typically not listed in the MP-table.
1097 * In this case we have to look up the IRQ based on the parent bus,
1098 * parent slot, and pin number. The SMP code detects such bridged
1099 * busses itself so we should get into this branch reliably.
1100 */
Miklos Vajna273c1122008-05-13 18:38:56 +02001101 if (irq < 0 && dev->bus->parent) {
1102 /* go back to the bridge */
1103 struct pci_dev *bridge = dev->bus->self;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 pin = (pin + PCI_SLOT(dev->devfn)) % 4;
Miklos Vajna273c1122008-05-13 18:38:56 +02001106 irq = IO_APIC_get_PCI_irq_vector(bridge->bus->number,
1107 PCI_SLOT(bridge->devfn),
1108 pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (irq >= 0)
Miklos Vajna273c1122008-05-13 18:38:56 +02001110 printk(KERN_WARNING
1111 "PCI: using PPB %s[%c] to get irq %d\n",
1112 pci_name(bridge),
1113 'A' + pin, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115 if (irq >= 0) {
Miklos Vajna273c1122008-05-13 18:38:56 +02001116 printk(KERN_INFO
1117 "PCI->APIC IRQ transform: %s[%c] -> IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 pci_name(dev), 'A' + pin, irq);
1119 dev->irq = irq;
1120 }
1121 }
1122 }
1123#endif
1124 /*
1125 * Still no IRQ? Try to lookup one...
1126 */
1127 if (pin && !dev->irq)
1128 pcibios_lookup_irq(dev, 0);
1129 }
1130}
1131
1132/*
1133 * Work around broken HP Pavilion Notebooks which assign USB to
1134 * IRQ 9 even though it is actually wired to IRQ 11
1135 */
Jeff Garzik18552562007-10-03 15:15:40 -04001136static int __init fix_broken_hp_bios_irq9(const struct dmi_system_id *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 if (!broken_hp_bios_irq9) {
1139 broken_hp_bios_irq9 = 1;
Miklos Vajna273c1122008-05-13 18:38:56 +02001140 printk(KERN_INFO "%s detected - fixing broken IRQ routing\n",
1141 d->ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143 return 0;
1144}
1145
1146/*
1147 * Work around broken Acer TravelMate 360 Notebooks which assign
1148 * Cardbus to IRQ 11 even though it is actually wired to IRQ 10
1149 */
Jeff Garzik18552562007-10-03 15:15:40 -04001150static int __init fix_acer_tm360_irqrouting(const struct dmi_system_id *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 if (!acer_tm360_irqrouting) {
1153 acer_tm360_irqrouting = 1;
Miklos Vajna273c1122008-05-13 18:38:56 +02001154 printk(KERN_INFO "%s detected - fixing broken IRQ routing\n",
1155 d->ident);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157 return 0;
1158}
1159
1160static struct dmi_system_id __initdata pciirq_dmi_table[] = {
1161 {
1162 .callback = fix_broken_hp_bios_irq9,
1163 .ident = "HP Pavilion N5400 Series Laptop",
1164 .matches = {
1165 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
1166 DMI_MATCH(DMI_BIOS_VERSION, "GE.M1.03"),
Miklos Vajna273c1122008-05-13 18:38:56 +02001167 DMI_MATCH(DMI_PRODUCT_VERSION,
1168 "HP Pavilion Notebook Model GE"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 DMI_MATCH(DMI_BOARD_VERSION, "OmniBook N32N-736"),
1170 },
1171 },
1172 {
1173 .callback = fix_acer_tm360_irqrouting,
1174 .ident = "Acer TravelMate 36x Laptop",
1175 .matches = {
1176 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1177 DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate 360"),
1178 },
1179 },
1180 { }
1181};
1182
1183static int __init pcibios_irq_init(void)
1184{
Daniel Marjamäki81745512005-12-01 18:01:28 +01001185 DBG(KERN_DEBUG "PCI: IRQ init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 if (pcibios_enable_irq || raw_pci_ops == NULL)
1188 return 0;
1189
1190 dmi_check_system(pciirq_dmi_table);
1191
1192 pirq_table = pirq_find_routing_table();
1193
1194#ifdef CONFIG_PCI_BIOS
1195 if (!pirq_table && (pci_probe & PCI_BIOS_IRQ_SCAN))
1196 pirq_table = pcibios_get_irq_routing_table();
1197#endif
1198 if (pirq_table) {
1199 pirq_peer_trick();
1200 pirq_find_router(&pirq_router);
1201 if (pirq_table->exclusive_irqs) {
1202 int i;
Miklos Vajna273c1122008-05-13 18:38:56 +02001203 for (i = 0; i < 16; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (!(pirq_table->exclusive_irqs & (1 << i)))
1205 pirq_penalty[i] += 100;
1206 }
Miklos Vajna273c1122008-05-13 18:38:56 +02001207 /*
1208 * If we're using the I/O APIC, avoid using the PCI IRQ
1209 * routing table
1210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (io_apic_assign_pci_irqs)
1212 pirq_table = NULL;
1213 }
1214
1215 pcibios_enable_irq = pirq_enable_irq;
1216
1217 pcibios_fixup_irqs();
1218 return 0;
1219}
1220
1221subsys_initcall(pcibios_irq_init);
1222
1223
David Shaohua Lic9c3e452005-04-01 00:07:31 -05001224static void pirq_penalize_isa_irq(int irq, int active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225{
1226 /*
1227 * If any ISAPnP device reports an IRQ in its list of possible
1228 * IRQ's, we try to avoid assigning it to PCI devices.
1229 */
David Shaohua Lic9c3e452005-04-01 00:07:31 -05001230 if (irq < 16) {
1231 if (active)
1232 pirq_penalty[irq] += 1000;
1233 else
1234 pirq_penalty[irq] += 100;
1235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237
David Shaohua Lic9c3e452005-04-01 00:07:31 -05001238void pcibios_penalize_isa_irq(int irq, int active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Len Brown6153df7b2005-08-25 12:27:09 -04001240#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (!acpi_noirq)
David Shaohua Lic9c3e452005-04-01 00:07:31 -05001242 acpi_penalize_isa_irq(irq, active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 else
1244#endif
David Shaohua Lic9c3e452005-04-01 00:07:31 -05001245 pirq_penalize_isa_irq(irq, active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
1248static int pirq_enable_irq(struct pci_dev *dev)
1249{
1250 u8 pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 struct pci_dev *temp_dev;
1252
1253 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
1254 if (pin && !pcibios_lookup_irq(dev, 1) && !dev->irq) {
1255 char *msg = "";
1256
Miklos Vajna273c1122008-05-13 18:38:56 +02001257 pin--; /* interrupt pins are numbered starting from 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 if (io_apic_assign_pci_irqs) {
1260 int irq;
1261
Miklos Vajna273c1122008-05-13 18:38:56 +02001262 irq = IO_APIC_get_PCI_irq_vector(dev->bus->number,
1263 PCI_SLOT(dev->devfn), pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 /*
Miklos Vajna273c1122008-05-13 18:38:56 +02001265 * Busses behind bridges are typically not
1266 * listed in the MP-table. In this case we have
1267 * to look up the IRQ based on the parent bus,
1268 * parent slot, and pin number. The SMP code
1269 * detects such bridged busses itself so we
1270 * should get into this branch reliably.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 */
1272 temp_dev = dev;
Miklos Vajna273c1122008-05-13 18:38:56 +02001273 while (irq < 0 && dev->bus->parent) {
1274 /* go back to the bridge */
1275 struct pci_dev *bridge = dev->bus->self;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 pin = (pin + PCI_SLOT(dev->devfn)) % 4;
Miklos Vajna273c1122008-05-13 18:38:56 +02001278 irq = IO_APIC_get_PCI_irq_vector(bridge->bus->number,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 PCI_SLOT(bridge->devfn), pin);
1280 if (irq >= 0)
Miklos Vajna273c1122008-05-13 18:38:56 +02001281 printk(KERN_WARNING
1282 "PCI: using PPB %s[%c] to get irq %d\n",
1283 pci_name(bridge),
1284 'A' + pin, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 dev = bridge;
1286 }
1287 dev = temp_dev;
1288 if (irq >= 0) {
Miklos Vajna273c1122008-05-13 18:38:56 +02001289 printk(KERN_INFO
1290 "PCI->APIC IRQ transform: %s[%c] -> IRQ %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 pci_name(dev), 'A' + pin, irq);
1292 dev->irq = irq;
1293 return 0;
1294 } else
1295 msg = " Probably buggy MP table.";
1296 } else if (pci_probe & PCI_BIOS_IRQ_SCAN)
1297 msg = "";
1298 else
1299 msg = " Please try using pci=biosirq.";
1300
Miklos Vajna273c1122008-05-13 18:38:56 +02001301 /*
1302 * With IDE legacy devices the IRQ lookup failure is not
1303 * a problem..
1304 */
1305 if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE &&
1306 !(dev->class & 0x5))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return 0;
1308
Miklos Vajna273c1122008-05-13 18:38:56 +02001309 printk(KERN_WARNING
1310 "PCI: No IRQ known for interrupt pin %c of device %s.%s\n",
1311 'A' + pin, pci_name(dev), msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return 0;
1314}