ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / i386 / 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 #include <asm/io.h>
16 #include <asm/smp.h>
17 #include <asm/io_apic.h>
18 #include <asm/hw_irq.h>
19
20 #include "pci.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
27 static struct irq_routing_table *pirq_table;
28
29 /*
30  * Never use: 0, 1, 2 (timer, keyboard, and cascade)
31  * Avoid using: 13, 14 and 15 (FP error and IDE).
32  * Penalize: 3, 4, 6, 7, 12 (known ISA uses: serial, floppy, parallel and mouse)
33  */
34 unsigned int pcibios_irq_mask = 0xfff8;
35
36 static int pirq_penalty[16] = {
37         1000000, 1000000, 1000000, 1000, 1000, 0, 1000, 1000,
38         0, 0, 0, 0, 1000, 100000, 100000, 100000
39 };
40
41 struct irq_router {
42         char *name;
43         u16 vendor, device;
44         int (*get)(struct pci_dev *router, struct pci_dev *dev, int pirq);
45         int (*set)(struct pci_dev *router, struct pci_dev *dev, int pirq, int new);
46 };
47
48 struct irq_router_handler {
49         u16 vendor;
50         int (*probe)(struct irq_router *r, struct pci_dev *router, u16 device);
51 };
52
53 int (*pcibios_enable_irq)(struct pci_dev *dev) = NULL;
54
55 /*
56  *  Search 0xf0000 -- 0xfffff for the PCI IRQ Routing Table.
57  */
58
59 static struct irq_routing_table * __init pirq_find_routing_table(void)
60 {
61         u8 *addr;
62         struct irq_routing_table *rt;
63         int i;
64         u8 sum;
65
66         for(addr = (u8 *) __va(0xf0000); addr < (u8 *) __va(0x100000); addr += 16) {
67                 rt = (struct irq_routing_table *) addr;
68                 if (rt->signature != PIRQ_SIGNATURE ||
69                     rt->version != PIRQ_VERSION ||
70                     rt->size % 16 ||
71                     rt->size < sizeof(struct irq_routing_table))
72                         continue;
73                 sum = 0;
74                 for(i=0; i<rt->size; i++)
75                         sum += addr[i];
76                 if (!sum) {
77                         DBG("PCI: Interrupt Routing Table found at 0x%p\n", rt);
78                         return rt;
79                 }
80         }
81         return NULL;
82 }
83
84 /*
85  *  If we have a IRQ routing table, use it to search for peer host
86  *  bridges.  It's a gross hack, but since there are no other known
87  *  ways how to get a list of buses, we have to go this way.
88  */
89
90 static void __init pirq_peer_trick(void)
91 {
92         struct irq_routing_table *rt = pirq_table;
93         u8 busmap[256];
94         int i;
95         struct irq_info *e;
96
97         memset(busmap, 0, sizeof(busmap));
98         for(i=0; i < (rt->size - sizeof(struct irq_routing_table)) / sizeof(struct irq_info); i++) {
99                 e = &rt->slots[i];
100 #ifdef DEBUG
101                 {
102                         int j;
103                         DBG("%02x:%02x slot=%02x", e->bus, e->devfn/8, e->slot);
104                         for(j=0; j<4; j++)
105                                 DBG(" %d:%02x/%04x", j, e->irq[j].link, e->irq[j].bitmap);
106                         DBG("\n");
107                 }
108 #endif
109                 busmap[e->bus] = 1;
110         }
111         for(i = 1; i < 256; i++) {
112                 if (!busmap[i] || pci_find_bus(0, i))
113                         continue;
114                 if (pci_scan_bus(i, &pci_root_ops, NULL))
115                         printk(KERN_INFO "PCI: Discovered primary peer bus %02x [IRQ]\n", i);
116         }
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 4 bits.
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  * ITE 8330G pirq rules are nibble-based
219  * FIXME: pirqmap may be { 1, 0, 3, 2 },
220  *        2+3 are both mapped to irq 9 on my system
221  */
222 static int pirq_ite_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
223 {
224         static unsigned char pirqmap[4] = { 1, 0, 2, 3 };
225         return read_config_nybble(router,0x43, pirqmap[pirq-1]);
226 }
227
228 static int pirq_ite_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
229 {
230         static unsigned char pirqmap[4] = { 1, 0, 2, 3 };
231         write_config_nybble(router, 0x43, pirqmap[pirq-1], irq);
232         return 1;
233 }
234
235 /*
236  * OPTI: high four bits are nibble pointer..
237  * I wonder what the low bits do?
238  */
239 static int pirq_opti_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
240 {
241         return read_config_nybble(router, 0xb8, pirq >> 4);
242 }
243
244 static int pirq_opti_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
245 {
246         write_config_nybble(router, 0xb8, pirq >> 4, irq);
247         return 1;
248 }
249
250 /*
251  * Cyrix: nibble offset 0x5C
252  * 0x5C bits 7:4 is INTB bits 3:0 is INTA 
253  * 0x5D bits 7:4 is INTD bits 3:0 is INTC
254  */
255 static int pirq_cyrix_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
256 {
257         return read_config_nybble(router, 0x5C, (pirq-1)^1);
258 }
259
260 static int pirq_cyrix_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
261 {
262         write_config_nybble(router, 0x5C, (pirq-1)^1, irq);
263         return 1;
264 }
265
266 /*
267  *      PIRQ routing for SiS 85C503 router used in several SiS chipsets.
268  *      We have to deal with the following issues here:
269  *      - vendors have different ideas about the meaning of link values
270  *      - some onboard devices (integrated in the chipset) have special
271  *        links and are thus routed differently (i.e. not via PCI INTA-INTD)
272  *      - different revision of the router have a different layout for
273  *        the routing registers, particularly for the onchip devices
274  *
275  *      For all routing registers the common thing is we have one byte
276  *      per routeable link which is defined as:
277  *               bit 7      IRQ mapping enabled (0) or disabled (1)
278  *               bits [6:4] reserved (sometimes used for onchip devices)
279  *               bits [3:0] IRQ to map to
280  *                   allowed: 3-7, 9-12, 14-15
281  *                   reserved: 0, 1, 2, 8, 13
282  *
283  *      The config-space registers located at 0x41/0x42/0x43/0x44 are
284  *      always used to route the normal PCI INT A/B/C/D respectively.
285  *      Apparently there are systems implementing PCI routing table using
286  *      link values 0x01-0x04 and others using 0x41-0x44 for PCI INTA..D.
287  *      We try our best to handle both link mappings.
288  *      
289  *      Currently (2003-05-21) it appears most SiS chipsets follow the
290  *      definition of routing registers from the SiS-5595 southbridge.
291  *      According to the SiS 5595 datasheets the revision id's of the
292  *      router (ISA-bridge) should be 0x01 or 0xb0.
293  *
294  *      Furthermore we've also seen lspci dumps with revision 0x00 and 0xb1.
295  *      Looks like these are used in a number of SiS 5xx/6xx/7xx chipsets.
296  *      They seem to work with the current routing code. However there is
297  *      some concern because of the two USB-OHCI HCs (original SiS 5595
298  *      had only one). YMMV.
299  *
300  *      Onchip routing for router rev-id 0x01/0xb0 and probably 0x00/0xb1:
301  *
302  *      0x61:   IDEIRQ:
303  *              bits [6:5] must be written 01
304  *              bit 4 channel-select primary (0), secondary (1)
305  *
306  *      0x62:   USBIRQ:
307  *              bit 6 OHCI function disabled (0), enabled (1)
308  *      
309  *      0x6a:   ACPI/SCI IRQ: bits 4-6 reserved
310  *
311  *      0x7e:   Data Acq. Module IRQ - bits 4-6 reserved
312  *
313  *      We support USBIRQ (in addition to INTA-INTD) and keep the
314  *      IDE, ACPI and DAQ routing untouched as set by the BIOS.
315  *
316  *      Currently the only reported exception is the new SiS 65x chipset
317  *      which includes the SiS 69x southbridge. Here we have the 85C503
318  *      router revision 0x04 and there are changes in the register layout
319  *      mostly related to the different USB HCs with USB 2.0 support.
320  *
321  *      Onchip routing for router rev-id 0x04 (try-and-error observation)
322  *
323  *      0x60/0x61/0x62/0x63:    1xEHCI and 3xOHCI (companion) USB-HCs
324  *                              bit 6-4 are probably unused, not like 5595
325  */
326
327 #define PIRQ_SIS_IRQ_MASK       0x0f
328 #define PIRQ_SIS_IRQ_DISABLE    0x80
329 #define PIRQ_SIS_USB_ENABLE     0x40
330
331 static int pirq_sis_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
332 {
333         u8 x;
334         int reg;
335
336         reg = pirq;
337         if (reg >= 0x01 && reg <= 0x04)
338                 reg += 0x40;
339         pci_read_config_byte(router, reg, &x);
340         return (x & PIRQ_SIS_IRQ_DISABLE) ? 0 : (x & PIRQ_SIS_IRQ_MASK);
341 }
342
343 static int pirq_sis_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
344 {
345         u8 x;
346         int reg;
347
348         reg = pirq;
349         if (reg >= 0x01 && reg <= 0x04)
350                 reg += 0x40;
351         pci_read_config_byte(router, reg, &x);
352         x &= ~(PIRQ_SIS_IRQ_MASK | PIRQ_SIS_IRQ_DISABLE);
353         x |= irq ? irq: PIRQ_SIS_IRQ_DISABLE;
354         pci_write_config_byte(router, reg, x);
355         return 1;
356 }
357
358
359 /*
360  * VLSI: nibble offset 0x74 - educated guess due to routing table and
361  *       config space of VLSI 82C534 PCI-bridge/router (1004:0102)
362  *       Tested on HP OmniBook 800 covering PIRQ 1, 2, 4, 8 for onboard
363  *       devices, PIRQ 3 for non-pci(!) soundchip and (untested) PIRQ 6
364  *       for the busbridge to the docking station.
365  */
366
367 static int pirq_vlsi_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
368 {
369         if (pirq > 8) {
370                 printk(KERN_INFO "VLSI router pirq escape (%d)\n", pirq);
371                 return 0;
372         }
373         return read_config_nybble(router, 0x74, pirq-1);
374 }
375
376 static int pirq_vlsi_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
377 {
378         if (pirq > 8) {
379                 printk(KERN_INFO "VLSI router pirq escape (%d)\n", pirq);
380                 return 0;
381         }
382         write_config_nybble(router, 0x74, pirq-1, irq);
383         return 1;
384 }
385
386 /*
387  * ServerWorks: PCI interrupts mapped to system IRQ lines through Index
388  * and Redirect I/O registers (0x0c00 and 0x0c01).  The Index register
389  * format is (PCIIRQ## | 0x10), e.g.: PCIIRQ10=0x1a.  The Redirect
390  * register is a straight binary coding of desired PIC IRQ (low nibble).
391  *
392  * The 'link' value in the PIRQ table is already in the correct format
393  * for the Index register.  There are some special index values:
394  * 0x00 for ACPI (SCI), 0x01 for USB, 0x02 for IDE0, 0x04 for IDE1,
395  * and 0x03 for SMBus.
396  */
397 static int pirq_serverworks_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
398 {
399         outb_p(pirq, 0xc00);
400         return inb(0xc01) & 0xf;
401 }
402
403 static int pirq_serverworks_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
404 {
405         outb_p(pirq, 0xc00);
406         outb_p(irq, 0xc01);
407         return 1;
408 }
409
410 /* Support for AMD756 PCI IRQ Routing
411  * Jhon H. Caicedo <jhcaiced@osso.org.co>
412  * Jun/21/2001 0.2.0 Release, fixed to use "nybble" functions... (jhcaiced)
413  * Jun/19/2001 Alpha Release 0.1.0 (jhcaiced)
414  * The AMD756 pirq rules are nibble-based
415  * offset 0x56 0-3 PIRQA  4-7  PIRQB
416  * offset 0x57 0-3 PIRQC  4-7  PIRQD
417  */
418 static int pirq_amd756_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
419 {
420         u8 irq;
421         irq = 0;
422         if (pirq <= 4)
423         {
424                 irq = read_config_nybble(router, 0x56, pirq - 1);
425         }
426         printk(KERN_INFO "AMD756: dev %04x:%04x, router pirq : %d get irq : %2d\n",
427                 dev->vendor, dev->device, pirq, irq);
428         return irq;
429 }
430
431 static int pirq_amd756_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
432 {
433         printk(KERN_INFO "AMD756: dev %04x:%04x, router pirq : %d SET irq : %2d\n", 
434                 dev->vendor, dev->device, pirq, irq);
435         if (pirq <= 4)
436         {
437                 write_config_nybble(router, 0x56, pirq - 1, irq);
438         }
439         return 1;
440 }
441
442 #ifdef CONFIG_PCI_BIOS
443
444 static int pirq_bios_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
445 {
446         struct pci_dev *bridge;
447         int pin = pci_get_interrupt_pin(dev, &bridge);
448         return pcibios_set_irq_routing(bridge, pin, irq);
449 }
450
451 #endif
452
453
454 static __init int intel_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
455 {
456         /* 440GX has a proprietary PIRQ router -- don't use it */
457         if (    pci_find_device(PCI_VENDOR_ID_INTEL,
458                                 PCI_DEVICE_ID_INTEL_82443GX_0, NULL) ||
459                 pci_find_device(PCI_VENDOR_ID_INTEL,
460                                 PCI_DEVICE_ID_INTEL_82443GX_2, NULL))
461                 return 0;
462
463         switch(device)
464         {
465                 case PCI_DEVICE_ID_INTEL_82371FB_0:
466                 case PCI_DEVICE_ID_INTEL_82371SB_0:
467                 case PCI_DEVICE_ID_INTEL_82371AB_0:
468                 case PCI_DEVICE_ID_INTEL_82371MX:
469                 case PCI_DEVICE_ID_INTEL_82443MX_0:
470                 case PCI_DEVICE_ID_INTEL_82801AA_0:
471                 case PCI_DEVICE_ID_INTEL_82801AB_0:
472                 case PCI_DEVICE_ID_INTEL_82801BA_0:
473                 case PCI_DEVICE_ID_INTEL_82801BA_10:
474                 case PCI_DEVICE_ID_INTEL_82801CA_0:
475                 case PCI_DEVICE_ID_INTEL_82801CA_12:
476                 case PCI_DEVICE_ID_INTEL_82801DB_0:
477                 case PCI_DEVICE_ID_INTEL_82801E_0:
478                 case PCI_DEVICE_ID_INTEL_82801EB_0:
479                 case PCI_DEVICE_ID_INTEL_ESB_0:
480                 case PCI_DEVICE_ID_INTEL_ICH6_0:
481                         r->name = "PIIX/ICH";
482                         r->get = pirq_piix_get;
483                         r->set = pirq_piix_set;
484                         return 1;
485         }
486         return 0;
487 }
488
489 static __init int via_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
490 {
491         /* FIXME: We should move some of the quirk fixup stuff here */
492         switch(device)
493         {
494                 case PCI_DEVICE_ID_VIA_82C586_0:
495                 case PCI_DEVICE_ID_VIA_82C596:
496                 case PCI_DEVICE_ID_VIA_82C686:
497                 case PCI_DEVICE_ID_VIA_8231:
498                 /* FIXME: add new ones for 8233/5 */
499                         r->name = "VIA";
500                         r->get = pirq_via_get;
501                         r->set = pirq_via_set;
502                         return 1;
503         }
504         return 0;
505 }
506
507 static __init int vlsi_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
508 {
509         switch(device)
510         {
511                 case PCI_DEVICE_ID_VLSI_82C534:
512                         r->name = "VLSI 82C534";
513                         r->get = pirq_vlsi_get;
514                         r->set = pirq_vlsi_set;
515                         return 1;
516         }
517         return 0;
518 }
519
520
521 static __init int serverworks_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
522 {
523         switch(device)
524         {
525                 case PCI_DEVICE_ID_SERVERWORKS_OSB4:
526                 case PCI_DEVICE_ID_SERVERWORKS_CSB5:
527                         r->name = "ServerWorks";
528                         r->get = pirq_serverworks_get;
529                         r->set = pirq_serverworks_set;
530                         return 1;
531         }
532         return 0;
533 }
534
535 static __init int sis_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
536 {
537         if (device != PCI_DEVICE_ID_SI_503)
538                 return 0;
539                 
540         r->name = "SIS";
541         r->get = pirq_sis_get;
542         r->set = pirq_sis_set;
543         DBG("PCI: Detecting SiS router at %02x:%02x\n",
544             rt->rtr_bus, rt->rtr_devfn);
545         return 1;
546 }
547
548 static __init int cyrix_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
549 {
550         switch(device)
551         {
552                 case PCI_DEVICE_ID_CYRIX_5520:
553                         r->name = "NatSemi";
554                         r->get = pirq_cyrix_get;
555                         r->set = pirq_cyrix_set;
556                         return 1;
557         }
558         return 0;
559 }
560
561 static __init int opti_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
562 {
563         switch(device)
564         {
565                 case PCI_DEVICE_ID_OPTI_82C700:
566                         r->name = "OPTI";
567                         r->get = pirq_opti_get;
568                         r->set = pirq_opti_set;
569                         return 1;
570         }
571         return 0;
572 }
573
574 static __init int ite_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
575 {
576         switch(device)
577         {
578                 case PCI_DEVICE_ID_ITE_IT8330G_0:
579                         r->name = "ITE";
580                         r->get = pirq_ite_get;
581                         r->set = pirq_ite_set;
582                         return 1;
583         }
584         return 0;
585 }
586
587 static __init int ali_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
588 {
589         switch(device)
590         {
591         case PCI_DEVICE_ID_AL_M1533:
592         case PCI_DEVICE_ID_AL_M1563:
593                 printk("PCI: Using ALI IRQ Router\n");
594                         r->name = "ALI";
595                         r->get = pirq_ali_get;
596                         r->set = pirq_ali_set;
597                         return 1;
598         }
599         return 0;
600 }
601
602 static __init int amd_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
603 {
604         switch(device)
605         {
606                 case PCI_DEVICE_ID_AMD_VIPER_740B:
607                         r->name = "AMD756";
608                         break;
609                 case PCI_DEVICE_ID_AMD_VIPER_7413:
610                         r->name = "AMD766";
611                         break;
612                 case PCI_DEVICE_ID_AMD_VIPER_7443:
613                         r->name = "AMD768";
614                         break;
615                 default:
616                         return 0;
617         }
618         r->get = pirq_amd756_get;
619         r->set = pirq_amd756_set;
620         return 1;
621 }
622                 
623 static __initdata struct irq_router_handler pirq_routers[] = {
624         { PCI_VENDOR_ID_INTEL, intel_router_probe },
625         { PCI_VENDOR_ID_AL, ali_router_probe },
626         { PCI_VENDOR_ID_ITE, ite_router_probe },
627         { PCI_VENDOR_ID_VIA, via_router_probe },
628         { PCI_VENDOR_ID_OPTI, opti_router_probe },
629         { PCI_VENDOR_ID_SI, sis_router_probe },
630         { PCI_VENDOR_ID_CYRIX, cyrix_router_probe },
631         { PCI_VENDOR_ID_VLSI, vlsi_router_probe },
632         { PCI_VENDOR_ID_SERVERWORKS, serverworks_router_probe },
633         { PCI_VENDOR_ID_AMD, amd_router_probe },
634         /* Someone with docs needs to add the ATI Radeon IGP */
635         { 0, NULL }
636 };
637 static struct irq_router pirq_router;
638 static struct pci_dev *pirq_router_dev;
639
640
641 /*
642  *      FIXME: should we have an option to say "generic for
643  *      chipset" ?
644  */
645  
646 static void __init pirq_find_router(struct irq_router *r)
647 {
648         struct irq_routing_table *rt = pirq_table;
649         struct irq_router_handler *h;
650
651 #ifdef CONFIG_PCI_BIOS
652         if (!rt->signature) {
653                 printk(KERN_INFO "PCI: Using BIOS for IRQ routing\n");
654                 r->set = pirq_bios_set;
655                 r->name = "BIOS";
656                 return;
657         }
658 #endif
659
660         /* Default unless a driver reloads it */
661         r->name = "default";
662         r->get = NULL;
663         r->set = NULL;
664         
665         DBG("PCI: Attempting to find IRQ router for %04x:%04x\n",
666             rt->rtr_vendor, rt->rtr_device);
667
668         pirq_router_dev = pci_find_slot(rt->rtr_bus, rt->rtr_devfn);
669         if (!pirq_router_dev) {
670                 DBG("PCI: Interrupt router not found at %02x:%02x\n", rt->rtr_bus, rt->rtr_devfn);
671                 return;
672         }
673
674         for( h = pirq_routers; h->vendor; h++) {
675                 /* First look for a router match */
676                 if (rt->rtr_vendor == h->vendor && h->probe(r, pirq_router_dev, rt->rtr_device))
677                         break;
678                 /* Fall back to a device match */
679                 if (pirq_router_dev->vendor == h->vendor && h->probe(r, pirq_router_dev, pirq_router_dev->device))
680                         break;
681         }
682         printk(KERN_INFO "PCI: Using IRQ router %s [%04x/%04x] at %s\n",
683                 pirq_router.name,
684                 pirq_router_dev->vendor,
685                 pirq_router_dev->device,
686                 pci_name(pirq_router_dev));
687 }
688
689 static struct irq_info *pirq_get_info(struct pci_dev *dev)
690 {
691         struct irq_routing_table *rt = pirq_table;
692         int entries = (rt->size - sizeof(struct irq_routing_table)) / sizeof(struct irq_info);
693         struct irq_info *info;
694
695         for (info = rt->slots; entries--; info++)
696                 if (info->bus == dev->bus->number && PCI_SLOT(info->devfn) == PCI_SLOT(dev->devfn))
697                         return info;
698         return NULL;
699 }
700
701 static int pcibios_lookup_irq(struct pci_dev *dev, int assign)
702 {
703         u8 pin;
704         struct irq_info *info;
705         int i, pirq, newirq;
706         int irq = 0;
707         u32 mask;
708         struct irq_router *r = &pirq_router;
709         struct pci_dev *dev2 = NULL;
710         char *msg = NULL;
711
712         /* Find IRQ pin */
713         pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
714         if (!pin) {
715                 DBG(" -> no interrupt pin\n");
716                 return 0;
717         }
718         pin = pin - 1;
719
720         /* Find IRQ routing entry */
721
722         if (!pirq_table)
723                 return 0;
724         
725         DBG("IRQ for %s:%d", pci_name(dev), pin);
726         info = pirq_get_info(dev);
727         if (!info) {
728                 DBG(" -> not found in routing table\n");
729                 return 0;
730         }
731         pirq = info->irq[pin].link;
732         mask = info->irq[pin].bitmap;
733         if (!pirq) {
734                 DBG(" -> not routed\n");
735                 return 0;
736         }
737         DBG(" -> PIRQ %02x, mask %04x, excl %04x", pirq, mask, pirq_table->exclusive_irqs);
738         mask &= pcibios_irq_mask;
739
740         /* Work around broken HP Pavilion Notebooks which assign USB to
741            IRQ 9 even though it is actually wired to IRQ 11 */
742
743         if (broken_hp_bios_irq9 && pirq == 0x59 && dev->irq == 9) {
744                 dev->irq = 11;
745                 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, 11);
746                 r->set(pirq_router_dev, dev, pirq, 11);
747         }
748
749         /*
750          * Find the best IRQ to assign: use the one
751          * reported by the device if possible.
752          */
753         newirq = dev->irq;
754         if (!((1 << newirq) & mask)) {
755                 if ( pci_probe & PCI_USE_PIRQ_MASK) newirq = 0;
756                 else printk(KERN_WARNING "PCI: IRQ %i for device %s doesn't match PIRQ mask - try pci=usepirqmask\n", newirq, pci_name(dev));
757         }
758         if (!newirq && assign) {
759                 for (i = 0; i < 16; i++) {
760                         if (!(mask & (1 << i)))
761                                 continue;
762                         if (pirq_penalty[i] < pirq_penalty[newirq] && can_request_irq(i, SA_SHIRQ))
763                                 newirq = i;
764                 }
765         }
766         DBG(" -> newirq=%d", newirq);
767
768         /* Check if it is hardcoded */
769         if ((pirq & 0xf0) == 0xf0) {
770                 irq = pirq & 0xf;
771                 DBG(" -> hardcoded IRQ %d\n", irq);
772                 msg = "Hardcoded";
773         } else if ( r->get && (irq = r->get(pirq_router_dev, dev, pirq)) && \
774         ((!(pci_probe & PCI_USE_PIRQ_MASK)) || ((1 << irq) & mask)) ) {
775                 DBG(" -> got IRQ %d\n", irq);
776                 msg = "Found";
777         } else if (newirq && r->set && (dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) {
778                 DBG(" -> assigning IRQ %d", newirq);
779                 if (r->set(pirq_router_dev, dev, pirq, newirq)) {
780                         eisa_set_level_irq(newirq);
781                         DBG(" ... OK\n");
782                         msg = "Assigned";
783                         irq = newirq;
784                 }
785         }
786
787         if (!irq) {
788                 DBG(" ... failed\n");
789                 if (newirq && mask == (1 << newirq)) {
790                         msg = "Guessed";
791                         irq = newirq;
792                 } else
793                         return 0;
794         }
795         printk(KERN_INFO "PCI: %s IRQ %d for device %s\n", msg, irq, pci_name(dev));
796
797         /* Update IRQ for all devices with the same pirq value */
798         while ((dev2 = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev2)) != NULL) {
799                 pci_read_config_byte(dev2, PCI_INTERRUPT_PIN, &pin);
800                 if (!pin)
801                         continue;
802                 pin--;
803                 info = pirq_get_info(dev2);
804                 if (!info)
805                         continue;
806                 if (info->irq[pin].link == pirq) {
807                         /* We refuse to override the dev->irq information. Give a warning! */
808                         if ( dev2->irq && dev2->irq != irq && \
809                         (!(pci_probe & PCI_USE_PIRQ_MASK) || \
810                         ((1 << dev2->irq) & mask)) ) {
811 #ifndef CONFIG_PCI_USE_VECTOR
812                                 printk(KERN_INFO "IRQ routing conflict for %s, have irq %d, want irq %d\n",
813                                        pci_name(dev2), dev2->irq, irq);
814 #endif
815                                 continue;
816                         }
817                         dev2->irq = irq;
818                         pirq_penalty[irq]++;
819                         if (dev != dev2)
820                                 printk(KERN_INFO "PCI: Sharing IRQ %d with %s\n", irq, pci_name(dev2));
821                 }
822         }
823         return 1;
824 }
825
826 static void __init pcibios_fixup_irqs(void)
827 {
828         struct pci_dev *dev = NULL;
829         u8 pin;
830
831         DBG("PCI: IRQ fixup\n");
832         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
833                 /*
834                  * If the BIOS has set an out of range IRQ number, just ignore it.
835                  * Also keep track of which IRQ's are already in use.
836                  */
837                 if (dev->irq >= 16) {
838                         DBG("%s: ignoring bogus IRQ %d\n", pci_name(dev), dev->irq);
839                         dev->irq = 0;
840                 }
841                 /* If the IRQ is already assigned to a PCI device, ignore its ISA use penalty */
842                 if (pirq_penalty[dev->irq] >= 100 && pirq_penalty[dev->irq] < 100000)
843                         pirq_penalty[dev->irq] = 0;
844                 pirq_penalty[dev->irq]++;
845         }
846
847         dev = NULL;
848         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
849                 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
850 #ifdef CONFIG_X86_IO_APIC
851                 /*
852                  * Recalculate IRQ numbers if we use the I/O APIC.
853                  */
854                 if (io_apic_assign_pci_irqs)
855                 {
856                         int irq;
857
858                         if (pin) {
859                                 pin--;          /* interrupt pins are numbered starting from 1 */
860                                 irq = IO_APIC_get_PCI_irq_vector(dev->bus->number, PCI_SLOT(dev->devfn), pin);
861         /*
862          * Busses behind bridges are typically not listed in the MP-table.
863          * In this case we have to look up the IRQ based on the parent bus,
864          * parent slot, and pin number. The SMP code detects such bridged
865          * busses itself so we should get into this branch reliably.
866          */
867                                 if (irq < 0 && dev->bus->parent) { /* go back to the bridge */
868                                         struct pci_dev * bridge = dev->bus->self;
869
870                                         pin = (pin + PCI_SLOT(dev->devfn)) % 4;
871                                         irq = IO_APIC_get_PCI_irq_vector(bridge->bus->number, 
872                                                         PCI_SLOT(bridge->devfn), pin);
873                                         if (irq >= 0)
874                                                 printk(KERN_WARNING "PCI: using PPB(B%d,I%d,P%d) to get irq %d\n", 
875                                                         bridge->bus->number, PCI_SLOT(bridge->devfn), pin, irq);
876                                 }
877                                 if (irq >= 0) {
878                                         if (use_pci_vector() &&
879                                                 !platform_legacy_irq(irq))
880                                                 irq = IO_APIC_VECTOR(irq);
881
882                                         printk(KERN_INFO "PCI->APIC IRQ transform: (B%d,I%d,P%d) -> %d\n",
883                                                 dev->bus->number, PCI_SLOT(dev->devfn), pin, irq);
884                                         dev->irq = irq;
885                                 }
886                         }
887                 }
888 #endif
889                 /*
890                  * Still no IRQ? Try to lookup one...
891                  */
892                 if (pin && !dev->irq)
893                         pcibios_lookup_irq(dev, 0);
894         }
895 }
896
897 static int __init pcibios_irq_init(void)
898 {
899         DBG("PCI: IRQ init\n");
900
901         if (pcibios_enable_irq)
902                 return 0;
903
904         pirq_table = pirq_find_routing_table();
905
906 #ifdef CONFIG_PCI_BIOS
907         if (!pirq_table && (pci_probe & PCI_BIOS_IRQ_SCAN))
908                 pirq_table = pcibios_get_irq_routing_table();
909 #endif
910         if (pirq_table) {
911                 pirq_peer_trick();
912                 pirq_find_router(&pirq_router);
913                 if (pirq_table->exclusive_irqs) {
914                         int i;
915                         for (i=0; i<16; i++)
916                                 if (!(pirq_table->exclusive_irqs & (1 << i)))
917                                         pirq_penalty[i] += 100;
918                 }
919                 /* If we're using the I/O APIC, avoid using the PCI IRQ routing table */
920                 if (io_apic_assign_pci_irqs)
921                         pirq_table = NULL;
922         }
923
924         pcibios_enable_irq = pirq_enable_irq;
925
926         pcibios_fixup_irqs();
927         return 0;
928 }
929
930 subsys_initcall(pcibios_irq_init);
931
932
933 void pcibios_penalize_isa_irq(int irq)
934 {
935         /*
936          *  If any ISAPnP device reports an IRQ in its list of possible
937          *  IRQ's, we try to avoid assigning it to PCI devices.
938          */
939         pirq_penalty[irq] += 100;
940 }
941
942 int pirq_enable_irq(struct pci_dev *dev)
943 {
944         u8 pin;
945         extern int interrupt_line_quirk;
946         struct pci_dev *temp_dev;
947
948         pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
949         if (pin && !pcibios_lookup_irq(dev, 1) && !dev->irq) {
950                 char *msg;
951                 msg = "";
952                 if (io_apic_assign_pci_irqs) {
953                         int irq;
954
955                         if (pin) {
956                                 pin--;          /* interrupt pins are numbered starting from 1 */
957                                 irq = IO_APIC_get_PCI_irq_vector(dev->bus->number, PCI_SLOT(dev->devfn), pin);
958                                 /*
959                                  * Busses behind bridges are typically not listed in the MP-table.
960                                  * In this case we have to look up the IRQ based on the parent bus,
961                                  * parent slot, and pin number. The SMP code detects such bridged
962                                  * busses itself so we should get into this branch reliably.
963                                  */
964                                 temp_dev = dev;
965                                 while (irq < 0 && dev->bus->parent) { /* go back to the bridge */
966                                         struct pci_dev * bridge = dev->bus->self;
967
968                                         pin = (pin + PCI_SLOT(dev->devfn)) % 4;
969                                         irq = IO_APIC_get_PCI_irq_vector(bridge->bus->number, 
970                                                         PCI_SLOT(bridge->devfn), pin);
971                                         if (irq >= 0)
972                                                 printk(KERN_WARNING "PCI: using PPB(B%d,I%d,P%d) to get irq %d\n", 
973                                                         bridge->bus->number, PCI_SLOT(bridge->devfn), pin, irq);
974                                         dev = bridge;
975                                 }
976                                 dev = temp_dev;
977                                 if (irq >= 0) {
978 #ifdef CONFIG_PCI_USE_VECTOR
979                                         if (!platform_legacy_irq(irq))
980                                                 irq = IO_APIC_VECTOR(irq);
981 #endif
982                                         printk(KERN_INFO "PCI->APIC IRQ transform: (B%d,I%d,P%d) -> %d\n",
983                                                 dev->bus->number, PCI_SLOT(dev->devfn), pin, irq);
984                                         dev->irq = irq;
985                                         return 0;
986                                 } else
987                                         msg = " Probably buggy MP table.";
988                         }
989                 } else if (pci_probe & PCI_BIOS_IRQ_SCAN)
990                         msg = "";
991                 else
992                         msg = " Please try using pci=biosirq.";
993                         
994                 /* With IDE legacy devices the IRQ lookup failure is not a problem.. */
995                 if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE && !(dev->class & 0x5))
996                         return 0;
997                         
998                 printk(KERN_WARNING "PCI: No IRQ known for interrupt pin %c of device %s.%s\n",
999                        'A' + pin - 1, pci_name(dev), msg);
1000         }
1001         /* VIA bridges use interrupt line for apic/pci steering across
1002            the V-Link */
1003         else if (interrupt_line_quirk)
1004                 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
1005         return 0;
1006 }
1007
1008 int pci_vector_resources(int last, int nr_released)
1009 {
1010         int count = nr_released;
1011
1012         int next = last;
1013         int offset = (last % 8);
1014
1015         while (next < FIRST_SYSTEM_VECTOR) {
1016                 next += 8;
1017 #ifdef CONFIG_X86_64
1018                 if (next == IA32_SYSCALL_VECTOR)
1019                         continue;
1020 #else
1021                 if (next == SYSCALL_VECTOR)
1022                         continue;
1023 #endif
1024                 count++;
1025                 if (next >= FIRST_SYSTEM_VECTOR) {
1026                         if (offset%8) {
1027                                 next = FIRST_DEVICE_VECTOR + offset;
1028                                 offset++;
1029                                 continue;
1030                         }
1031                         count--;
1032                 }
1033         }
1034
1035         return count;
1036 }