more debug output
[linux-2.4.git] / arch / i386 / kernel / pci-irq.c
1 /*
2  *      Low-Level PCI Support for PC -- Routing of Interrupts
3  *
4  *      (c) 1999--2000 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <linux/config.h>
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/pci.h>
11 #include <linux/init.h>
12 #include <linux/slab.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15
16 #include <asm/io.h>
17 #include <asm/smp.h>
18 #include <asm/io_apic.h>
19
20 #include "pci-i386.h"
21
22 #define PIRQ_SIGNATURE  (('$' << 0) + ('P' << 8) + ('I' << 16) + ('R' << 24))
23 #define PIRQ_VERSION 0x0100
24
25 int broken_hp_bios_irq9;
26 int acer_tm360_irqrouting;
27
28 static struct irq_routing_table *pirq_table;
29
30 /*
31  * Never use: 0, 1, 2 (timer, keyboard, and cascade)
32  * Avoid using: 13, 14 and 15 (FP error and IDE).
33  * Penalize: 3, 4, 6, 7, 12 (known ISA uses: serial, floppy, parallel and mouse)
34  */
35 unsigned int pcibios_irq_mask = 0xfff8;
36
37 static int pirq_penalty[16] = {
38         1000000, 1000000, 1000000, 1000, 1000, 0, 1000, 1000,
39         0, 0, 0, 0, 1000, 100000, 100000, 100000
40 };
41
42 struct irq_router {
43         char *name;
44         u16 vendor, device;
45         int (*get)(struct pci_dev *router, struct pci_dev *dev, int pirq);
46         int (*set)(struct pci_dev *router, struct pci_dev *dev, int pirq, int new);
47 };
48
49 struct irq_router_handler {
50         u16 vendor;
51         int (*probe)(struct irq_router *r, struct pci_dev *router, u16 device);
52 };
53
54 /*
55  *  Search 0xf0000 -- 0xfffff for the PCI IRQ Routing Table.
56  */
57
58 static struct irq_routing_table * __init pirq_find_routing_table(void)
59 {
60         u8 *addr;
61         struct irq_routing_table *rt;
62         int i;
63         u8 sum;
64
65         for(addr = (u8 *) __va(0xf0000); addr < (u8 *) __va(0x100000); addr += 16) {
66                 rt = (struct irq_routing_table *) addr;
67                 if (rt->signature != PIRQ_SIGNATURE ||
68                     rt->version != PIRQ_VERSION ||
69                     rt->size % 16 ||
70                     rt->size < sizeof(struct irq_routing_table))
71                         continue;
72                 sum = 0;
73                 for(i=0; i<rt->size; i++)
74                         sum += addr[i];
75                 if (!sum) {
76                         DBG("PCI: Interrupt Routing Table found at 0x%p\n", rt);
77                         return rt;
78                 }
79         }
80         return NULL;
81 }
82
83 /*
84  *  If we have a IRQ routing table, use it to search for peer host
85  *  bridges.  It's a gross hack, but since there are no other known
86  *  ways how to get a list of buses, we have to go this way.
87  */
88
89 static void __init pirq_peer_trick(void)
90 {
91         struct irq_routing_table *rt = pirq_table;
92         u8 busmap[256];
93         int i;
94         struct irq_info *e;
95
96         memset(busmap, 0, sizeof(busmap));
97         for(i=0; i < (rt->size - sizeof(struct irq_routing_table)) / sizeof(struct irq_info); i++) {
98                 e = &rt->slots[i];
99 #ifdef DEBUG
100                 {
101                         int j;
102                         DBG("%02x:%02x slot=%02x", e->bus, e->devfn/8, e->slot);
103                         for(j=0; j<4; j++)
104                                 DBG(" %d:%02x/%04x", j, e->irq[j].link, e->irq[j].bitmap);
105                         DBG("\n");
106                 }
107 #endif
108                 busmap[e->bus] = 1;
109         }
110         for(i=1; i<256; i++)
111                 /*
112                  *  It might be a secondary bus, but in this case its parent is already
113                  *  known (ascending bus order) and therefore pci_scan_bus returns immediately.
114                  */
115                 if (busmap[i] && pci_scan_bus(i, pci_root_bus->ops, NULL))
116                         printk(KERN_INFO "PCI: Discovered primary peer bus %02x [IRQ]\n", i);
117         pcibios_last_bus = -1;
118 }
119
120 /*
121  *  Code for querying and setting of IRQ routes on various interrupt routers.
122  */
123
124 void eisa_set_level_irq(unsigned int irq)
125 {
126         unsigned char mask = 1 << (irq & 7);
127         unsigned int port = 0x4d0 + (irq >> 3);
128         unsigned char val = inb(port);
129
130         if (!(val & mask)) {
131                 DBG(" -> edge");
132                 outb(val | mask, port);
133         }
134 }
135
136 /*
137  * Common IRQ routing practice: nybbles in config space,
138  * offset by some magic constant.
139  */
140 static unsigned int read_config_nybble(struct pci_dev *router, unsigned offset, unsigned nr)
141 {
142         u8 x;
143         unsigned reg = offset + (nr >> 1);
144
145         pci_read_config_byte(router, reg, &x);
146         return (nr & 1) ? (x >> 4) : (x & 0xf);
147 }
148
149 static void write_config_nybble(struct pci_dev *router, unsigned offset, unsigned nr, unsigned int val)
150 {
151         u8 x;
152         unsigned reg = offset + (nr >> 1);
153
154         pci_read_config_byte(router, reg, &x);
155         x = (nr & 1) ? ((x & 0x0f) | (val << 4)) : ((x & 0xf0) | val);
156         pci_write_config_byte(router, reg, x);
157 }
158
159 /*
160  * ALI pirq entries are damn ugly, and completely undocumented.
161  * This has been figured out from pirq tables, and it's not a pretty
162  * picture.
163  */
164 static int pirq_ali_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
165 {
166         static unsigned char irqmap[16] = { 0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15 };
167
168         return irqmap[read_config_nybble(router, 0x48, pirq-1)];
169 }
170
171 static int pirq_ali_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
172 {
173         static unsigned char irqmap[16] = { 0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15 };
174         unsigned int val = irqmap[irq];
175                 
176         if (val) {
177                 write_config_nybble(router, 0x48, pirq-1, val);
178                 return 1;
179         }
180         return 0;
181 }
182
183 /*
184  * The Intel PIIX4 pirq rules are fairly simple: "pirq" is
185  * just a pointer to the config space.
186  */
187 static int pirq_piix_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
188 {
189         u8 x;
190
191         pci_read_config_byte(router, pirq, &x);
192         return (x < 16) ? x : 0;
193 }
194
195 static int pirq_piix_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
196 {
197         pci_write_config_byte(router, pirq, irq);
198         return 1;
199 }
200
201 /*
202  * The VIA pirq rules are nibble-based, like ALI,
203  * but without the ugly irq number munging.
204  * However, PIRQD is in the upper instead of lower nibble.
205  */
206 static int pirq_via_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
207 {
208         return read_config_nybble(router, 0x55, pirq == 4 ? 5 : pirq);
209 }
210
211 static int pirq_via_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
212 {
213         write_config_nybble(router, 0x55, pirq == 4 ? 5 : pirq, irq);
214         return 1;
215 }
216
217 /*
218  * The VIA pirq rules are nibble-based, like ALI,
219  * but without the ugly irq number munging.
220  * However, for 82C586, nibble map is different .
221  */
222 static int pirq_via586_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
223 {
224         static unsigned int pirqmap[4] = { 3, 2, 5, 1 };
225         return read_config_nybble(router, 0x55, pirqmap[pirq-1]);
226 }
227
228 static int pirq_via586_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
229 {
230         static unsigned int pirqmap[4] = { 3, 2, 5, 1 };
231         write_config_nybble(router, 0x55, pirqmap[pirq-1], irq);
232         return 1;
233 }
234
235 /*
236  * ITE 8330G pirq rules are nibble-based
237  * FIXME: pirqmap may be { 1, 0, 3, 2 },
238  *        2+3 are both mapped to irq 9 on my system
239  */
240 static int pirq_ite_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
241 {
242         static unsigned char pirqmap[4] = { 1, 0, 2, 3 };
243         return read_config_nybble(router,0x43, pirqmap[pirq-1]);
244 }
245
246 static int pirq_ite_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
247 {
248         static unsigned char pirqmap[4] = { 1, 0, 2, 3 };
249         write_config_nybble(router, 0x43, pirqmap[pirq-1], irq);
250         return 1;
251 }
252
253 /*
254  * OPTI: high four bits are nibble pointer..
255  * I wonder what the low bits do?
256  */
257 static int pirq_opti_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
258 {
259         return read_config_nybble(router, 0xb8, pirq >> 4);
260 }
261
262 static int pirq_opti_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
263 {
264         write_config_nybble(router, 0xb8, pirq >> 4, irq);
265         return 1;
266 }
267
268 /*
269  * OPTI Viper-M/N+: Bit field with 3 bits per entry.
270  * Due to the lack of a specification the information about this chipset
271  * was taken from the NetBSD source code.
272  */
273 static int pirq_viper_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
274 {
275         static const int viper_irq_decode[] = { 0, 5, 9, 10, 11, 12, 14, 15 };
276         u32 irq;
277
278         pci_read_config_dword(router, 0x40, &irq);
279         irq >>= (pirq-1)*3;
280         irq &= 7;
281
282         return viper_irq_decode[irq];
283 }
284
285 static int pirq_viper_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
286 {
287         static const int viper_irq_map[] = { -1, -1, -1, -1, -1, 1, -1, -1, -1, 2, 3, 4, 5, -1, 6, 7 };
288         int newval = viper_irq_map[irq];
289         u32 val;
290         u32 mask = 7 << (3*(pirq-1));
291 #if 0
292         mask |= 0x10000UL << (pirq-1);  /* edge triggered */
293 #endif
294
295         if ( newval == -1 )
296                 return 0;
297         
298         pci_read_config_dword(router, 0x40, &val);
299         val &= ~mask;
300         val |= newval << (3*(pirq-1));
301         pci_write_config_dword(router, 0x40, val);
302
303         return 1;
304 }
305
306 /*
307  * Cyrix: nibble offset 0x5C
308  */
309 static int pirq_cyrix_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
310 {
311         return read_config_nybble(router, 0x5C, (pirq-1)^1);
312 }
313
314 static int pirq_cyrix_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
315 {
316         write_config_nybble(router, 0x5C, (pirq-1)^1, irq);
317         return 1;
318 }
319
320 /*
321  *      PIRQ routing for SiS 85C503 router used in several SiS chipsets.
322  *      We have to deal with the following issues here:
323  *      - vendors have different ideas about the meaning of link values
324  *      - some onboard devices (integrated in the chipset) have special
325  *        links and are thus routed differently (i.e. not via PCI INTA-INTD)
326  *      - different revision of the router have a different layout for
327  *        the routing registers, particularly for the onchip devices
328  *
329  *      For all routing registers the common thing is we have one byte
330  *      per routeable link which is defined as:
331  *               bit 7      IRQ mapping enabled (0) or disabled (1)
332  *               bits [6:4] reserved (sometimes used for onchip devices)
333  *               bits [3:0] IRQ to map to
334  *                   allowed: 3-7, 9-12, 14-15
335  *                   reserved: 0, 1, 2, 8, 13
336  *
337  *      The config-space registers located at 0x41/0x42/0x43/0x44 are
338  *      always used to route the normal PCI INT A/B/C/D respectively.
339  *      Apparently there are systems implementing PCI routing table using
340  *      link values 0x01-0x04 and others using 0x41-0x44 for PCI INTA..D.
341  *      We try our best to handle both link mappings.
342  *      
343  *      Currently (2003-05-21) it appears most SiS chipsets follow the
344  *      definition of routing registers from the SiS-5595 southbridge.
345  *      According to the SiS 5595 datasheets the revision id's of the
346  *      router (ISA-bridge) should be 0x01 or 0xb0.
347  *
348  *      Furthermore we've also seen lspci dumps with revision 0x00 and 0xb1.
349  *      Looks like these are used in a number of SiS 5xx/6xx/7xx chipsets.
350  *      They seem to work with the current routing code. However there is
351  *      some concern because of the two USB-OHCI HCs (original SiS 5595
352  *      had only one). YMMV.
353  *
354  *      Onchip routing for router rev-id 0x01/0xb0 and probably 0x00/0xb1:
355  *
356  *      0x61:   IDEIRQ:
357  *              bits [6:5] must be written 01
358  *              bit 4 channel-select primary (0), secondary (1)
359  *
360  *      0x62:   USBIRQ:
361  *              bit 6 OHCI function disabled (0), enabled (1)
362  *      
363  *      0x6a:   ACPI/SCI IRQ: bits 4-6 reserved
364  *
365  *      0x7e:   Data Acq. Module IRQ - bits 4-6 reserved
366  *
367  *      We support USBIRQ (in addition to INTA-INTD) and keep the
368  *      IDE, ACPI and DAQ routing untouched as set by the BIOS.
369  *
370  *      Currently the only reported exception is the new SiS 65x chipset
371  *      which includes the SiS 69x southbridge. Here we have the 85C503
372  *      router revision 0x04 and there are changes in the register layout
373  *      mostly related to the different USB HCs with USB 2.0 support.
374  *
375  *      Onchip routing for router rev-id 0x04 (try-and-error observation)
376  *
377  *      0x60/0x61/0x62/0x63:    1xEHCI and 3xOHCI (companion) USB-HCs
378  *                              bit 6-4 are probably unused, not like 5595
379  */
380
381 #define PIRQ_SIS_IRQ_MASK       0x0f
382 #define PIRQ_SIS_IRQ_DISABLE    0x80
383 #define PIRQ_SIS_USB_ENABLE     0x40
384 #define PIRQ_SIS_DETECT_REGISTER 0x40
385
386 /* return value:
387  * -1 on error
388  * 0 for PCI INTA-INTD
389  * 0 or enable bit mask to check or set for onchip functions
390  */
391 static inline int pirq_sis5595_onchip(int pirq, int *reg)
392 {
393         int ret = -1;
394
395         *reg = pirq;
396         switch(pirq) {
397         case 0x01:
398         case 0x02:
399         case 0x03:
400         case 0x04:
401                 *reg += 0x40;
402         case 0x41:
403         case 0x42:
404         case 0x43:
405         case 0x44:
406                 ret = 0;
407                 break;
408
409         case 0x62:
410                 ret = PIRQ_SIS_USB_ENABLE;      /* documented for 5595 */
411                 break;
412
413         case 0x61:
414         case 0x6a:
415         case 0x7e:
416                 printk(KERN_INFO "SiS pirq: IDE/ACPI/DAQ mapping not implemented: (%u)\n",
417                        (unsigned) pirq);
418                 /* fall thru */
419         default:
420                 printk(KERN_INFO "SiS router unknown request: (%u)\n",
421                        (unsigned) pirq);
422                 break;
423         }
424         return ret;
425 }               
426
427 /* return value:
428  * -1 on error
429  * 0 for PCI INTA-INTD
430  * 0 or enable bit mask to check or set for onchip functions
431  */
432 static inline int pirq_sis96x_onchip(int pirq, int *reg)
433 {
434         int ret = -1;
435
436         *reg = pirq;
437         switch(pirq) {
438         case 0x01:
439         case 0x02:
440         case 0x03:
441         case 0x04:
442                 *reg += 0x40;
443         case 0x41:
444         case 0x42:
445         case 0x43:
446         case 0x44:
447         case 0x60:
448         case 0x61:
449         case 0x62:
450         case 0x63:
451                 ret = 0;
452                 break;
453
454         default:
455                 printk(KERN_INFO "SiS router unknown request: (%u)\n",
456                        (unsigned) pirq);
457                 break;
458         }
459         return ret;
460 }               
461
462
463 static int pirq_sis5595_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
464 {
465         u8 x;
466         int reg, check;
467
468         check = pirq_sis5595_onchip(pirq, &reg);
469         if (check < 0)
470                 return 0;
471
472         pci_read_config_byte(router, reg, &x);
473         if (check != 0  &&  !(x & check))
474                 return 0;
475
476         return (x & PIRQ_SIS_IRQ_DISABLE) ? 0 : (x & PIRQ_SIS_IRQ_MASK);
477 }
478
479 static int pirq_sis96x_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
480 {
481         u8 x;
482         int reg, check;
483
484         check = pirq_sis96x_onchip(pirq, &reg);
485         if (check < 0)
486                 return 0;
487
488         pci_read_config_byte(router, reg, &x);
489         if (check != 0  &&  !(x & check))
490                 return 0;
491
492         return (x & PIRQ_SIS_IRQ_DISABLE) ? 0 : (x & PIRQ_SIS_IRQ_MASK);
493 }
494
495 static int pirq_sis5595_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
496 {
497         u8 x;
498         int reg, set;
499
500         set = pirq_sis5595_onchip(pirq, &reg);
501         if (set < 0)
502                 return 0;
503
504         x = (irq & PIRQ_SIS_IRQ_MASK);
505         if (x == 0)
506                 x = PIRQ_SIS_IRQ_DISABLE;
507         else
508                 x |= set;
509
510         pci_write_config_byte(router, reg, x);
511
512         return 1;
513 }
514
515 static int pirq_sis96x_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
516 {
517         u8 x;
518         int reg, set;
519
520         set = pirq_sis96x_onchip(pirq, &reg);
521         if (set < 0)
522                 return 0;
523
524         x = (irq & PIRQ_SIS_IRQ_MASK);
525         if (x == 0)
526                 x = PIRQ_SIS_IRQ_DISABLE;
527         else
528                 x |= set;
529
530         pci_write_config_byte(router, reg, x);
531
532         return 1;
533 }
534
535
536 /*
537  * VLSI: nibble offset 0x74 - educated guess due to routing table and
538  *       config space of VLSI 82C534 PCI-bridge/router (1004:0102)
539  *       Tested on HP OmniBook 800 covering PIRQ 1, 2, 4, 8 for onboard
540  *       devices, PIRQ 3 for non-pci(!) soundchip and (untested) PIRQ 6
541  *       for the busbridge to the docking station.
542  */
543
544 static int pirq_vlsi_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
545 {
546         if (pirq > 8) {
547                 printk(KERN_INFO "VLSI router pirq escape (%d)\n", pirq);
548                 return 0;
549         }
550         return read_config_nybble(router, 0x74, pirq-1);
551 }
552
553 static int pirq_vlsi_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
554 {
555         if (pirq > 8) {
556                 printk(KERN_INFO "VLSI router pirq escape (%d)\n", pirq);
557                 return 0;
558         }
559         write_config_nybble(router, 0x74, pirq-1, irq);
560         return 1;
561 }
562
563 /*
564  * ServerWorks: PCI interrupts mapped to system IRQ lines through Index
565  * and Redirect I/O registers (0x0c00 and 0x0c01).  The Index register
566  * format is (PCIIRQ## | 0x10), e.g.: PCIIRQ10=0x1a.  The Redirect
567  * register is a straight binary coding of desired PIC IRQ (low nibble).
568  *
569  * The 'link' value in the PIRQ table is already in the correct format
570  * for the Index register.  There are some special index values:
571  * 0x00 for ACPI (SCI), 0x01 for USB, 0x02 for IDE0, 0x04 for IDE1,
572  * and 0x03 for SMBus.
573  */
574 static int pirq_serverworks_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
575 {
576         outb_p(pirq, 0xc00);
577         return inb(0xc01) & 0xf;
578 }
579
580 static int pirq_serverworks_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
581 {
582         outb_p(pirq, 0xc00);
583         outb_p(irq, 0xc01);
584         return 1;
585 }
586
587 /* Support for AMD756 PCI IRQ Routing
588  * Jhon H. Caicedo <jhcaiced@osso.org.co>
589  * Jun/21/2001 0.2.0 Release, fixed to use "nybble" functions... (jhcaiced)
590  * Jun/19/2001 Alpha Release 0.1.0 (jhcaiced)
591  * The AMD756 pirq rules are nibble-based
592  * offset 0x56 0-3 PIRQA  4-7  PIRQB
593  * offset 0x57 0-3 PIRQC  4-7  PIRQD
594  */
595 static int pirq_amd756_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
596 {
597         u8 irq;
598         irq = 0;
599         if (pirq <= 4)
600         {
601                 irq = read_config_nybble(router, 0x56, pirq - 1);
602         }
603         printk(KERN_INFO "AMD756: dev %04x:%04x, router pirq : %d get irq : %2d\n",
604                 dev->vendor, dev->device, pirq, irq);
605         return irq;
606 }
607
608 static int pirq_amd756_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
609 {
610         printk(KERN_INFO "AMD756: dev %04x:%04x, router pirq : %d SET irq : %2d\n", 
611                 dev->vendor, dev->device, pirq, irq);
612         if (pirq <= 4)
613         {
614                 write_config_nybble(router, 0x56, pirq - 1, irq);
615         }
616         return 1;
617 }
618
619 #ifdef CONFIG_PCI_BIOS
620
621 static int pirq_bios_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
622 {
623         struct pci_dev *bridge;
624         int pin = pci_get_interrupt_pin(dev, &bridge);
625         return pcibios_set_irq_routing(bridge, pin, irq);
626 }
627
628 #endif
629
630
631 static __init int intel_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
632 {
633         /* We must not touch 440GX even if we have tables. 440GX has
634            different IRQ routing weirdness */
635         if(pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443GX_0, NULL) ||
636            pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443GX_2, NULL))
637                 return 0;
638         switch(device)
639         {
640                 case PCI_DEVICE_ID_INTEL_82371FB_0:
641                 case PCI_DEVICE_ID_INTEL_82371SB_0:
642                 case PCI_DEVICE_ID_INTEL_82371AB_0:
643                 case PCI_DEVICE_ID_INTEL_82371MX:
644                 case PCI_DEVICE_ID_INTEL_82443MX_0:
645                 case PCI_DEVICE_ID_INTEL_82801AA_0:
646                 case PCI_DEVICE_ID_INTEL_82801AB_0:
647                 case PCI_DEVICE_ID_INTEL_82801BA_0:
648                 case PCI_DEVICE_ID_INTEL_82801BA_10:
649                 case PCI_DEVICE_ID_INTEL_82801CA_0:
650                 case PCI_DEVICE_ID_INTEL_82801CA_12:
651                 case PCI_DEVICE_ID_INTEL_82801DB_0:
652                 case PCI_DEVICE_ID_INTEL_82801E_0:
653                 case PCI_DEVICE_ID_INTEL_82801EB_0:
654                 case PCI_DEVICE_ID_INTEL_ESB_0:
655                 case PCI_DEVICE_ID_INTEL_ICH6_0:
656                         r->name = "PIIX/ICH";
657                         r->get = pirq_piix_get;
658                         r->set = pirq_piix_set;
659                         return 1;
660         }
661         return 0;
662 }
663
664 static __init int via_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
665 {
666         /* FIXME: We should move some of the quirk fixup stuff here */
667
668         /*
669          * work arounds for some buggy BIOSes
670          */
671         if (device == PCI_DEVICE_ID_VIA_82C586_0) {
672                 switch(router->device)
673                 {
674                         case PCI_DEVICE_ID_VIA_82C686:
675                                 /*
676                                  * Asus k7m bios wrongly reports 82C686A 
677                                  * as 586-compatible 
678                                  */
679                                 device = PCI_DEVICE_ID_VIA_82C686;
680                                 break;
681                         case PCI_DEVICE_ID_VIA_8235:
682                                 /**
683                                  * Asus a7v-x bios wrongly reports 8235
684                                  * as 586-compatible
685                                  */
686                                 device = PCI_DEVICE_ID_VIA_8235;
687                                 break;
688                 }       
689         }
690
691         switch(device)
692         {
693                 case PCI_DEVICE_ID_VIA_82C586_0:
694                         r->name = "VIA";
695                         r->get = pirq_via586_get;
696                         r->set = pirq_via586_set;
697                         return 1;
698                 case PCI_DEVICE_ID_VIA_82C596:
699                 case PCI_DEVICE_ID_VIA_82C686:
700                 case PCI_DEVICE_ID_VIA_8231:
701                 case PCI_DEVICE_ID_VIA_8235:
702                 /* FIXME: add new ones for 8233/5 */
703                         r->name = "VIA";
704                         r->get = pirq_via_get;
705                         r->set = pirq_via_set;
706                         return 1;
707         }
708         return 0;
709 }
710
711 static __init int vlsi_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
712 {
713         switch(device)
714         {
715                 case PCI_DEVICE_ID_VLSI_82C534:
716                         r->name = "VLSI 82C534";
717                         r->get = pirq_vlsi_get;
718                         r->set = pirq_vlsi_set;
719                         return 1;
720         }
721         return 0;
722 }
723
724
725 static __init int serverworks_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
726 {
727         switch(device)
728         {
729                 case PCI_DEVICE_ID_SERVERWORKS_OSB4:
730                 case PCI_DEVICE_ID_SERVERWORKS_CSB5:
731                         r->name = "ServerWorks";
732                         r->get = pirq_serverworks_get;
733                         r->set = pirq_serverworks_set;
734                         return 1;
735         }
736         return 0;
737 }
738
739 static __init int sis_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
740 {
741         u8 reg;
742         u16 devid;
743
744         if (device != PCI_DEVICE_ID_SI_503)
745                 return 0;
746                 
747         /*
748          * In case of SiS south bridge, we need to detect the two
749          * kinds of routing tables we have seen so far (5595 and 96x). 
750          * Since the maintain the same device ID, we need to do poke 
751          * the PCI configuration space to find the router type we are
752          * dealing with.
753          */
754
755         /*
756          * Factoid: writing bit6 of register 0x40 of the router config space
757          * will make the SB to show up 0x096x inside the device id. Note,
758          * we need to restore register 0x40 after the device id poke.
759          */
760
761         pci_read_config_byte(router, PIRQ_SIS_DETECT_REGISTER, &reg);
762         pci_write_config_byte(router, PIRQ_SIS_DETECT_REGISTER, reg | (1 << 6));
763         pci_read_config_word(router, PCI_DEVICE_ID, &devid);
764         pci_write_config_byte(router, PIRQ_SIS_DETECT_REGISTER, reg);
765
766         if ((devid & 0xfff0) == 0x0960) {
767                 r->name = "SIS96x";
768                 r->get = pirq_sis96x_get;
769                 r->set = pirq_sis96x_set;
770                 DBG("PCI: Detecting SiS router at %02x:%02x : SiS096x detected\n",
771                     rt->rtr_bus, rt->rtr_devfn);
772         } else {
773                 r->name = "SIS5595";
774                 r->get = pirq_sis5595_get;
775                 r->set = pirq_sis5595_set;
776                 DBG("PCI: Detecting SiS router at %02x:%02x : SiS5595 detected\n",
777                     rt->rtr_bus, rt->rtr_devfn);
778         }
779         return 1;
780 }
781
782 static __init int cyrix_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
783 {
784         switch(device)
785         {
786                 case PCI_DEVICE_ID_CYRIX_5520:
787                         r->name = "NatSemi";
788                         r->get = pirq_cyrix_get;
789                         r->set = pirq_cyrix_set;
790                         return 1;
791         }
792         return 0;
793 }
794
795 static __init int opti_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
796 {
797         switch(device)
798         {
799                 case PCI_DEVICE_ID_OPTI_82C700:
800                         r->name = "OPTI";
801                         r->get = pirq_opti_get;
802                         r->set = pirq_opti_set;
803                         break;
804                 case PCI_DEVICE_ID_OPTI_82C558:
805                         r->name = "OPTI VIPER";
806                         r->get = pirq_viper_get;
807                         r->set = pirq_viper_set;
808                         break;
809                 default:
810                         return 0;
811         }
812         return 1;
813 }
814
815 static __init int ite_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
816 {
817         switch(device)
818         {
819                 case PCI_DEVICE_ID_ITE_IT8330G_0:
820                         r->name = "ITE";
821                         r->get = pirq_ite_get;
822                         r->set = pirq_ite_set;
823                         return 1;
824         }
825         return 0;
826 }
827
828 static __init int ali_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
829 {
830         switch(device)
831         {
832                 case PCI_DEVICE_ID_AL_M1533:
833                         r->name = "ALI";
834                         r->get = pirq_ali_get;
835                         r->set = pirq_ali_set;
836                         return 1;
837                 /* Should add 156x some day */
838         }
839         return 0;
840 }
841
842 static __init int amd_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
843 {
844         switch(device)
845         {
846                 case PCI_DEVICE_ID_AMD_VIPER_740B:
847                         r->name = "AMD756";
848                         break;
849                 case PCI_DEVICE_ID_AMD_VIPER_7413:
850                         r->name = "AMD766";
851                         break;
852                 case PCI_DEVICE_ID_AMD_VIPER_7443:
853                         r->name = "AMD768";
854                         break;
855                 default:
856                         return 0;
857         }
858         r->get = pirq_amd756_get;
859         r->set = pirq_amd756_set;
860         return 1;
861 }
862                 
863 static __initdata struct irq_router_handler pirq_routers[] = {
864         { PCI_VENDOR_ID_INTEL, intel_router_probe },
865         { PCI_VENDOR_ID_AL, ali_router_probe },
866         { PCI_VENDOR_ID_ITE, ite_router_probe },
867         { PCI_VENDOR_ID_VIA, via_router_probe },
868         { PCI_VENDOR_ID_OPTI, opti_router_probe },
869         { PCI_VENDOR_ID_SI, sis_router_probe },
870         { PCI_VENDOR_ID_CYRIX, cyrix_router_probe },
871         { PCI_VENDOR_ID_VLSI, vlsi_router_probe },
872         { PCI_VENDOR_ID_SERVERWORKS, serverworks_router_probe },
873         { PCI_VENDOR_ID_AMD, amd_router_probe },
874         /* Someone with docs needs to add the ATI Radeon IGP */
875         { 0, NULL }
876 };
877 static struct irq_router pirq_router;
878 static struct pci_dev *pirq_router_dev;
879
880 /*
881  *      FIXME: should we have an option to say "generic for
882  *      chipset" ?
883  */
884  
885 static void __init pirq_find_router(struct irq_router *r)
886 {
887         struct irq_routing_table *rt = pirq_table;
888         struct irq_router_handler *h;
889
890 #ifdef CONFIG_PCI_BIOS
891         if (!rt->signature) {
892                 printk(KERN_INFO "PCI: Using BIOS for IRQ routing\n");
893                 r->set = pirq_bios_set;
894                 r->name = "BIOS";
895                 return;
896         }
897 #endif
898
899         /* Default unless a driver reloads it */
900         r->name = "default";
901         r->get = NULL;
902         r->set = NULL;
903         
904         DBG("PCI: Attempting to find IRQ router for %04x:%04x\n",
905             rt->rtr_vendor, rt->rtr_device);
906
907         pirq_router_dev = pci_find_slot(rt->rtr_bus, rt->rtr_devfn);
908         if (!pirq_router_dev) {
909                 DBG("PCI: Interrupt router not found at %02x:%02x\n", rt->rtr_bus, rt->rtr_devfn);
910                 return;
911         }
912
913         for( h = pirq_routers; h->vendor; h++) {
914                 /* First look for a router match */
915                 if (rt->rtr_vendor == h->vendor && h->probe(r, pirq_router_dev, rt->rtr_device))
916                         break;
917                 /* Fall back to a device match */
918                 if (pirq_router_dev->vendor == h->vendor && h->probe(r, pirq_router_dev, pirq_router_dev->device))
919                         break;
920         }
921         printk(KERN_INFO "PCI: Using IRQ router %s [%04x/%04x] at %s\n",
922                 pirq_router.name,
923                 pirq_router_dev->vendor,
924                 pirq_router_dev->device,
925                 pirq_router_dev->slot_name);
926 }
927
928 static struct irq_info *pirq_get_info(struct pci_dev *dev)
929 {
930         struct irq_routing_table *rt = pirq_table;
931         int entries = (rt->size - sizeof(struct irq_routing_table)) / sizeof(struct irq_info);
932         struct irq_info *info;
933
934         for (info = rt->slots; entries--; info++)
935                 if (info->bus == dev->bus->number && PCI_SLOT(info->devfn) == PCI_SLOT(dev->devfn))
936                         return info;
937         return NULL;
938 }
939
940 static void pcibios_test_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
941 {
942 }
943
944 static int pcibios_lookup_irq(struct pci_dev *dev, int assign)
945 {
946         u8 pin;
947         struct irq_info *info;
948         int i, pirq, newirq;
949         int irq = 0;
950         u32 mask;
951         struct irq_router *r = &pirq_router;
952         struct pci_dev *dev2;
953         char *msg = NULL;
954
955         if (!pirq_table)
956                 return 0;
957
958         /* Find IRQ routing entry */
959         pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
960         if (!pin) {
961                 DBG(" -> no interrupt pin\n");
962                 return 0;
963         }
964         pin = pin - 1;
965         
966         DBG("IRQ for %s:%d", dev->slot_name, pin);
967         info = pirq_get_info(dev);
968         if (!info) {
969                 DBG(" -> not found in routing table\n");
970                 return 0;
971         }
972         pirq = info->irq[pin].link;
973         mask = info->irq[pin].bitmap;
974         if (!pirq) {
975                 DBG(" -> not routed\n");
976                 return 0;
977         }
978         DBG(" -> PIRQ %02x, mask %04x, excl %04x", pirq, mask, pirq_table->exclusive_irqs);
979         mask &= pcibios_irq_mask;
980
981         /* Work around broken HP Pavilion Notebooks which assign USB to
982            IRQ 9 even though it is actually wired to IRQ 11 */
983
984         if (broken_hp_bios_irq9 && pirq == 0x59 && dev->irq == 9) {
985                 dev->irq = 11;
986                 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, 11);
987                 r->set(pirq_router_dev, dev, pirq, 11);
988         }
989
990         /* same for Acer Travelmate 360, but with CB and irq 11 -> 10 */
991         if (acer_tm360_irqrouting && dev->irq == 11 && dev->vendor == PCI_VENDOR_ID_O2) {
992                 pirq = 0x68;
993                 mask = 0x400;
994                 dev->irq = r->get(pirq_router_dev, dev, pirq);
995                 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
996         }
997
998         /*
999          * Find the best IRQ to assign: use the one
1000          * reported by the device if possible.
1001          */
1002         newirq = dev->irq;
1003         if (!newirq && assign) {
1004                 for (i = 0; i < 16; i++) {
1005                         if (!(mask & (1 << i)))
1006                                 continue;
1007                         if (pirq_penalty[i] < pirq_penalty[newirq] &&
1008                             !request_irq(i, pcibios_test_irq_handler, SA_SHIRQ, "pci-test", dev)) {
1009                                 free_irq(i, dev);
1010                                 newirq = i;
1011                         }
1012                 }
1013         }
1014         DBG(" -> newirq=%d", newirq);
1015
1016         /* Check if it is hardcoded */
1017         if ((pirq & 0xf0) == 0xf0) {
1018                 irq = pirq & 0xf;
1019                 DBG(" -> hardcoded IRQ %d\n", irq);
1020                 msg = "Hardcoded";
1021         } else if (r->get && (irq = r->get(pirq_router_dev, dev, pirq))) {
1022                 DBG(" -> got IRQ %d\n", irq);
1023                 msg = "Found";
1024         } else if (newirq && r->set && (dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) {
1025                 DBG(" -> assigning IRQ %d", newirq);
1026                 if (r->set(pirq_router_dev, dev, pirq, newirq)) {
1027                         eisa_set_level_irq(newirq);
1028                         DBG(" ... OK\n");
1029                         msg = "Assigned";
1030                         irq = newirq;
1031                 }
1032         }
1033
1034         if (!irq) {
1035                 DBG(" ... failed\n");
1036                 if (newirq && mask == (1 << newirq)) {
1037                         msg = "Guessed";
1038                         irq = newirq;
1039                 } else
1040                         return 0;
1041         }
1042         printk(KERN_INFO "PCI: %s IRQ %d for device %s\n", msg, irq, dev->slot_name);
1043
1044         /* Update IRQ for all devices with the same pirq value */
1045         pci_for_each_dev(dev2) {
1046                 pci_read_config_byte(dev2, PCI_INTERRUPT_PIN, &pin);
1047                 if (!pin)
1048                         continue;
1049                 pin--;
1050                 info = pirq_get_info(dev2);
1051                 if (!info)
1052                         continue;
1053                 if (info->irq[pin].link == pirq) {
1054                         /* We refuse to override the dev->irq information. Give a warning! */
1055                         if (dev2->irq && dev2->irq != irq) {
1056                                 printk(KERN_INFO "IRQ routing conflict for %s, have irq %d, want irq %d\n",
1057                                        dev2->slot_name, dev2->irq, irq);
1058                                 continue;
1059                         }
1060                         dev2->irq = irq;
1061                         pirq_penalty[irq]++;
1062                         if (dev != dev2)
1063                                 printk(KERN_INFO "PCI: Sharing IRQ %d with %s\n", irq, dev2->slot_name);
1064                 }
1065         }
1066         return 1;
1067 }
1068
1069 void __init pcibios_irq_init(void)
1070 {
1071         DBG("PCI: IRQ init\n");
1072         pirq_table = pirq_find_routing_table();
1073 #ifdef CONFIG_PCI_BIOS
1074         if (!pirq_table && (pci_probe & PCI_BIOS_IRQ_SCAN))
1075                 pirq_table = pcibios_get_irq_routing_table();
1076 #endif
1077         if (pirq_table) {
1078                 pirq_peer_trick();
1079                 pirq_find_router(&pirq_router);
1080                 if (pirq_table->exclusive_irqs) {
1081                         int i;
1082                         for (i=0; i<16; i++)
1083                                 if (!(pirq_table->exclusive_irqs & (1 << i)))
1084                                         pirq_penalty[i] += 100;
1085                 }
1086                 /* If we're using the I/O APIC, avoid using the PCI IRQ routing table */
1087                 if (io_apic_assign_pci_irqs)
1088                         pirq_table = NULL;
1089         }
1090 }
1091
1092 void __init pcibios_fixup_irqs(void)
1093 {
1094         struct pci_dev *dev;
1095         u8 pin;
1096
1097         DBG("PCI: IRQ fixup\n");
1098         pci_for_each_dev(dev) {
1099                 /*
1100                  * If the BIOS has set an out of range IRQ number, just ignore it.
1101                  * Also keep track of which IRQ's are already in use.
1102                  */
1103                 if (dev->irq >= 16) {
1104                         DBG("%s: ignoring bogus IRQ %d\n", dev->slot_name, dev->irq);
1105                         dev->irq = 0;
1106                 }
1107                 /* If the IRQ is already assigned to a PCI device, ignore its ISA use penalty */
1108                 if (pirq_penalty[dev->irq] >= 100 && pirq_penalty[dev->irq] < 100000)
1109                         pirq_penalty[dev->irq] = 0;
1110                 pirq_penalty[dev->irq]++;
1111         }
1112
1113         pci_for_each_dev(dev) {
1114                 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
1115 #ifdef CONFIG_X86_IO_APIC
1116                 /*
1117                  * Recalculate IRQ numbers if we use the I/O APIC.
1118                  */
1119                 if (io_apic_assign_pci_irqs)
1120                 {
1121                         int irq;
1122
1123                         if (pin) {
1124                                 pin--;          /* interrupt pins are numbered starting from 1 */
1125                                 irq = IO_APIC_get_PCI_irq_vector(dev->bus->number, PCI_SLOT(dev->devfn), pin);
1126         /*
1127          * Busses behind bridges are typically not listed in the MP-table.
1128          * In this case we have to look up the IRQ based on the parent bus,
1129          * parent slot, and pin number. The SMP code detects such bridged
1130          * busses itself so we should get into this branch reliably.
1131          */
1132                                 if (irq < 0 && dev->bus->parent) { /* go back to the bridge */
1133                                         struct pci_dev * bridge = dev->bus->self;
1134
1135                                         pin = (pin + PCI_SLOT(dev->devfn)) % 4;
1136                                         irq = IO_APIC_get_PCI_irq_vector(bridge->bus->number, 
1137                                                         PCI_SLOT(bridge->devfn), pin);
1138                                         if (irq >= 0)
1139                                                 printk(KERN_WARNING "PCI: using PPB(B%d,I%d,P%d) to get irq %d\n", 
1140                                                         bridge->bus->number, PCI_SLOT(bridge->devfn), pin, irq);
1141                                 }
1142                                 if (irq >= 0) {
1143                                         printk(KERN_INFO "PCI->APIC IRQ transform: (B%d,I%d,P%d) -> %d\n",
1144                                                 dev->bus->number, PCI_SLOT(dev->devfn), pin, irq);
1145                                         dev->irq = irq;
1146                                 }
1147                         }
1148                 }
1149 #endif
1150                 /*
1151                  * Still no IRQ? Try to lookup one...
1152                  */
1153                 if (pin && !dev->irq)
1154                         pcibios_lookup_irq(dev, 0);
1155         }
1156 }
1157
1158 void pcibios_penalize_isa_irq(int irq)
1159 {
1160         /*
1161          *  If any ISAPnP device reports an IRQ in its list of possible
1162          *  IRQ's, we try to avoid assigning it to PCI devices.
1163          */
1164         pirq_penalty[irq] += 100;
1165 }
1166
1167 void pcibios_enable_irq(struct pci_dev *dev)
1168 {
1169         u8 pin;
1170         extern int via_interrupt_line_quirk;
1171         struct pci_dev *temp_dev;
1172         
1173         pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
1174         if (pin && !pcibios_lookup_irq(dev, 1) && !dev->irq) {
1175                 char *msg;
1176
1177                 pin--;          /* interrupt pins are numbered starting from 1 */
1178
1179                 /* With IDE legacy devices the IRQ lookup failure is not a problem.. */
1180                 if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE && !(dev->class & 0x5))
1181                         return;
1182
1183                 if (io_apic_assign_pci_irqs) {
1184                         int irq;
1185
1186                         irq = IO_APIC_get_PCI_irq_vector(dev->bus->number, PCI_SLOT(dev->devfn), pin);
1187                         /*
1188                          * Busses behind bridges are typically not listed in the MP-table.
1189                          * In this case we have to look up the IRQ based on the parent bus,
1190                          * parent slot, and pin number. The SMP code detects such bridged
1191                          * busses itself so we should get into this branch reliably.
1192                          */
1193                         temp_dev = dev;
1194                         while (irq < 0 && dev->bus->parent) { /* go back to the bridge */
1195                                 struct pci_dev * bridge = dev->bus->self;
1196
1197                                 pin = (pin + PCI_SLOT(dev->devfn)) % 4;
1198                                 irq = IO_APIC_get_PCI_irq_vector(bridge->bus->number, 
1199                                                 PCI_SLOT(bridge->devfn), pin);
1200                                 if (irq >= 0)
1201                                         printk(KERN_WARNING "PCI: using PPB(B%d,I%d,P%d) to get irq %d\n", 
1202                                                 bridge->bus->number, PCI_SLOT(bridge->devfn), pin, irq);
1203                                 dev = bridge;
1204                         }
1205                         dev = temp_dev;
1206                         if (irq >= 0) {
1207                                 printk(KERN_INFO "PCI->APIC IRQ transform: (B%d,I%d,P%d) -> %d\n",
1208                                         dev->bus->number, PCI_SLOT(dev->devfn), pin, irq);
1209                                 dev->irq = irq;
1210                                 return;
1211                         } else
1212                                 msg = " Probably buggy MP table.";
1213                 } else if (pci_probe & PCI_BIOS_IRQ_SCAN)
1214                         msg = "";
1215                 else
1216                         msg = " Please try using pci=biosirq.";
1217                 printk(KERN_WARNING "PCI: No IRQ known for interrupt pin %c of device %s.%s\n",
1218                        'A' + pin, dev->slot_name, msg);
1219         }
1220         /* VIA bridges use interrupt line for apic/pci steering across
1221            the V-Link */
1222         else if (via_interrupt_line_quirk)
1223                 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq & 15);
1224                 
1225 }