patch-2_6_7-vs1_9_1_12
[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_1:
480                 case PCI_DEVICE_ID_INTEL_ICH6_0:
481                 case PCI_DEVICE_ID_INTEL_ICH6_1:
482                         r->name = "PIIX/ICH";
483                         r->get = pirq_piix_get;
484                         r->set = pirq_piix_set;
485                         return 1;
486         }
487         return 0;
488 }
489
490 static __init int via_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
491 {
492         /* FIXME: We should move some of the quirk fixup stuff here */
493         switch(device)
494         {
495                 case PCI_DEVICE_ID_VIA_82C586_0:
496                 case PCI_DEVICE_ID_VIA_82C596:
497                 case PCI_DEVICE_ID_VIA_82C686:
498                 case PCI_DEVICE_ID_VIA_8231:
499                 /* FIXME: add new ones for 8233/5 */
500                         r->name = "VIA";
501                         r->get = pirq_via_get;
502                         r->set = pirq_via_set;
503                         return 1;
504         }
505         return 0;
506 }
507
508 static __init int vlsi_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
509 {
510         switch(device)
511         {
512                 case PCI_DEVICE_ID_VLSI_82C534:
513                         r->name = "VLSI 82C534";
514                         r->get = pirq_vlsi_get;
515                         r->set = pirq_vlsi_set;
516                         return 1;
517         }
518         return 0;
519 }
520
521
522 static __init int serverworks_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
523 {
524         switch(device)
525         {
526                 case PCI_DEVICE_ID_SERVERWORKS_OSB4:
527                 case PCI_DEVICE_ID_SERVERWORKS_CSB5:
528                         r->name = "ServerWorks";
529                         r->get = pirq_serverworks_get;
530                         r->set = pirq_serverworks_set;
531                         return 1;
532         }
533         return 0;
534 }
535
536 static __init int sis_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
537 {
538         if (device != PCI_DEVICE_ID_SI_503)
539                 return 0;
540                 
541         r->name = "SIS";
542         r->get = pirq_sis_get;
543         r->set = pirq_sis_set;
544         return 1;
545 }
546
547 static __init int cyrix_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
548 {
549         switch(device)
550         {
551                 case PCI_DEVICE_ID_CYRIX_5520:
552                         r->name = "NatSemi";
553                         r->get = pirq_cyrix_get;
554                         r->set = pirq_cyrix_set;
555                         return 1;
556         }
557         return 0;
558 }
559
560 static __init int opti_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
561 {
562         switch(device)
563         {
564                 case PCI_DEVICE_ID_OPTI_82C700:
565                         r->name = "OPTI";
566                         r->get = pirq_opti_get;
567                         r->set = pirq_opti_set;
568                         return 1;
569         }
570         return 0;
571 }
572
573 static __init int ite_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
574 {
575         switch(device)
576         {
577                 case PCI_DEVICE_ID_ITE_IT8330G_0:
578                         r->name = "ITE";
579                         r->get = pirq_ite_get;
580                         r->set = pirq_ite_set;
581                         return 1;
582         }
583         return 0;
584 }
585
586 static __init int ali_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
587 {
588         switch(device)
589         {
590         case PCI_DEVICE_ID_AL_M1533:
591         case PCI_DEVICE_ID_AL_M1563:
592                 printk("PCI: Using ALI IRQ Router\n");
593                         r->name = "ALI";
594                         r->get = pirq_ali_get;
595                         r->set = pirq_ali_set;
596                         return 1;
597         }
598         return 0;
599 }
600
601 static __init int amd_router_probe(struct irq_router *r, struct pci_dev *router, u16 device)
602 {
603         switch(device)
604         {
605                 case PCI_DEVICE_ID_AMD_VIPER_740B:
606                         r->name = "AMD756";
607                         break;
608                 case PCI_DEVICE_ID_AMD_VIPER_7413:
609                         r->name = "AMD766";
610                         break;
611                 case PCI_DEVICE_ID_AMD_VIPER_7443:
612                         r->name = "AMD768";
613                         break;
614                 default:
615                         return 0;
616         }
617         r->get = pirq_amd756_get;
618         r->set = pirq_amd756_set;
619         return 1;
620 }
621                 
622 static __initdata struct irq_router_handler pirq_routers[] = {
623         { PCI_VENDOR_ID_INTEL, intel_router_probe },
624         { PCI_VENDOR_ID_AL, ali_router_probe },
625         { PCI_VENDOR_ID_ITE, ite_router_probe },
626         { PCI_VENDOR_ID_VIA, via_router_probe },
627         { PCI_VENDOR_ID_OPTI, opti_router_probe },
628         { PCI_VENDOR_ID_SI, sis_router_probe },
629         { PCI_VENDOR_ID_CYRIX, cyrix_router_probe },
630         { PCI_VENDOR_ID_VLSI, vlsi_router_probe },
631         { PCI_VENDOR_ID_SERVERWORKS, serverworks_router_probe },
632         { PCI_VENDOR_ID_AMD, amd_router_probe },
633         /* Someone with docs needs to add the ATI Radeon IGP */
634         { 0, NULL }
635 };
636 static struct irq_router pirq_router;
637 static struct pci_dev *pirq_router_dev;
638
639
640 /*
641  *      FIXME: should we have an option to say "generic for
642  *      chipset" ?
643  */
644  
645 static void __init pirq_find_router(struct irq_router *r)
646 {
647         struct irq_routing_table *rt = pirq_table;
648         struct irq_router_handler *h;
649
650 #ifdef CONFIG_PCI_BIOS
651         if (!rt->signature) {
652                 printk(KERN_INFO "PCI: Using BIOS for IRQ routing\n");
653                 r->set = pirq_bios_set;
654                 r->name = "BIOS";
655                 return;
656         }
657 #endif
658
659         /* Default unless a driver reloads it */
660         r->name = "default";
661         r->get = NULL;
662         r->set = NULL;
663         
664         DBG("PCI: Attempting to find IRQ router for %04x:%04x\n",
665             rt->rtr_vendor, rt->rtr_device);
666
667         pirq_router_dev = pci_find_slot(rt->rtr_bus, rt->rtr_devfn);
668         if (!pirq_router_dev) {
669                 DBG("PCI: Interrupt router not found at %02x:%02x\n", rt->rtr_bus, rt->rtr_devfn);
670                 return;
671         }
672
673         for( h = pirq_routers; h->vendor; h++) {
674                 /* First look for a router match */
675                 if (rt->rtr_vendor == h->vendor && h->probe(r, pirq_router_dev, rt->rtr_device))
676                         break;
677                 /* Fall back to a device match */
678                 if (pirq_router_dev->vendor == h->vendor && h->probe(r, pirq_router_dev, pirq_router_dev->device))
679                         break;
680         }
681         printk(KERN_INFO "PCI: Using IRQ router %s [%04x/%04x] at %s\n",
682                 pirq_router.name,
683                 pirq_router_dev->vendor,
684                 pirq_router_dev->device,
685                 pci_name(pirq_router_dev));
686 }
687
688 static struct irq_info *pirq_get_info(struct pci_dev *dev)
689 {
690         struct irq_routing_table *rt = pirq_table;
691         int entries = (rt->size - sizeof(struct irq_routing_table)) / sizeof(struct irq_info);
692         struct irq_info *info;
693
694         for (info = rt->slots; entries--; info++)
695                 if (info->bus == dev->bus->number && PCI_SLOT(info->devfn) == PCI_SLOT(dev->devfn))
696                         return info;
697         return NULL;
698 }
699
700 static int pcibios_lookup_irq(struct pci_dev *dev, int assign)
701 {
702         u8 pin;
703         struct irq_info *info;
704         int i, pirq, newirq;
705         int irq = 0;
706         u32 mask;
707         struct irq_router *r = &pirq_router;
708         struct pci_dev *dev2 = NULL;
709         char *msg = NULL;
710
711         /* Find IRQ pin */
712         pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
713         if (!pin) {
714                 DBG(" -> no interrupt pin\n");
715                 return 0;
716         }
717         pin = pin - 1;
718
719         /* Find IRQ routing entry */
720
721         if (!pirq_table)
722                 return 0;
723         
724         DBG("IRQ for %s:%d", pci_name(dev), pin);
725         info = pirq_get_info(dev);
726         if (!info) {
727                 DBG(" -> not found in routing table\n");
728                 return 0;
729         }
730         pirq = info->irq[pin].link;
731         mask = info->irq[pin].bitmap;
732         if (!pirq) {
733                 DBG(" -> not routed\n");
734                 return 0;
735         }
736         DBG(" -> PIRQ %02x, mask %04x, excl %04x", pirq, mask, pirq_table->exclusive_irqs);
737         mask &= pcibios_irq_mask;
738
739         /* Work around broken HP Pavilion Notebooks which assign USB to
740            IRQ 9 even though it is actually wired to IRQ 11 */
741
742         if (broken_hp_bios_irq9 && pirq == 0x59 && dev->irq == 9) {
743                 dev->irq = 11;
744                 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, 11);
745                 r->set(pirq_router_dev, dev, pirq, 11);
746         }
747
748         /*
749          * Find the best IRQ to assign: use the one
750          * reported by the device if possible.
751          */
752         newirq = dev->irq;
753         if (!((1 << newirq) & mask)) {
754                 if ( pci_probe & PCI_USE_PIRQ_MASK) newirq = 0;
755                 else printk(KERN_WARNING "PCI: IRQ %i for device %s doesn't match PIRQ mask - try pci=usepirqmask\n", newirq, pci_name(dev));
756         }
757         if (!newirq && assign) {
758                 for (i = 0; i < 16; i++) {
759                         if (!(mask & (1 << i)))
760                                 continue;
761                         if (pirq_penalty[i] < pirq_penalty[newirq] && can_request_irq(i, SA_SHIRQ))
762                                 newirq = i;
763                 }
764         }
765         DBG(" -> newirq=%d", newirq);
766
767         /* Check if it is hardcoded */
768         if ((pirq & 0xf0) == 0xf0) {
769                 irq = pirq & 0xf;
770                 DBG(" -> hardcoded IRQ %d\n", irq);
771                 msg = "Hardcoded";
772         } else if ( r->get && (irq = r->get(pirq_router_dev, dev, pirq)) && \
773         ((!(pci_probe & PCI_USE_PIRQ_MASK)) || ((1 << irq) & mask)) ) {
774                 DBG(" -> got IRQ %d\n", irq);
775                 msg = "Found";
776         } else if (newirq && r->set && (dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) {
777                 DBG(" -> assigning IRQ %d", newirq);
778                 if (r->set(pirq_router_dev, dev, pirq, newirq)) {
779                         eisa_set_level_irq(newirq);
780                         DBG(" ... OK\n");
781                         msg = "Assigned";
782                         irq = newirq;
783                 }
784         }
785
786         if (!irq) {
787                 DBG(" ... failed\n");
788                 if (newirq && mask == (1 << newirq)) {
789                         msg = "Guessed";
790                         irq = newirq;
791                 } else
792                         return 0;
793         }
794         printk(KERN_INFO "PCI: %s IRQ %d for device %s\n", msg, irq, pci_name(dev));
795
796         /* Update IRQ for all devices with the same pirq value */
797         while ((dev2 = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev2)) != NULL) {
798                 pci_read_config_byte(dev2, PCI_INTERRUPT_PIN, &pin);
799                 if (!pin)
800                         continue;
801                 pin--;
802                 info = pirq_get_info(dev2);
803                 if (!info)
804                         continue;
805                 if (info->irq[pin].link == pirq) {
806                         /* We refuse to override the dev->irq information. Give a warning! */
807                         if ( dev2->irq && dev2->irq != irq && \
808                         (!(pci_probe & PCI_USE_PIRQ_MASK) || \
809                         ((1 << dev2->irq) & mask)) ) {
810 #ifndef CONFIG_PCI_USE_VECTOR
811                                 printk(KERN_INFO "IRQ routing conflict for %s, have irq %d, want irq %d\n",
812                                        pci_name(dev2), dev2->irq, irq);
813 #endif
814                                 continue;
815                         }
816                         dev2->irq = irq;
817                         pirq_penalty[irq]++;
818                         if (dev != dev2)
819                                 printk(KERN_INFO "PCI: Sharing IRQ %d with %s\n", irq, pci_name(dev2));
820                 }
821         }
822         return 1;
823 }
824
825 static void __init pcibios_fixup_irqs(void)
826 {
827         struct pci_dev *dev = NULL;
828         u8 pin;
829
830         DBG("PCI: IRQ fixup\n");
831         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
832                 /*
833                  * If the BIOS has set an out of range IRQ number, just ignore it.
834                  * Also keep track of which IRQ's are already in use.
835                  */
836                 if (dev->irq >= 16) {
837                         DBG("%s: ignoring bogus IRQ %d\n", pci_name(dev), dev->irq);
838                         dev->irq = 0;
839                 }
840                 /* If the IRQ is already assigned to a PCI device, ignore its ISA use penalty */
841                 if (pirq_penalty[dev->irq] >= 100 && pirq_penalty[dev->irq] < 100000)
842                         pirq_penalty[dev->irq] = 0;
843                 pirq_penalty[dev->irq]++;
844         }
845
846         dev = NULL;
847         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
848                 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
849 #ifdef CONFIG_X86_IO_APIC
850                 /*
851                  * Recalculate IRQ numbers if we use the I/O APIC.
852                  */
853                 if (io_apic_assign_pci_irqs)
854                 {
855                         int irq;
856
857                         if (pin) {
858                                 pin--;          /* interrupt pins are numbered starting from 1 */
859                                 irq = IO_APIC_get_PCI_irq_vector(dev->bus->number, PCI_SLOT(dev->devfn), pin);
860         /*
861          * Busses behind bridges are typically not listed in the MP-table.
862          * In this case we have to look up the IRQ based on the parent bus,
863          * parent slot, and pin number. The SMP code detects such bridged
864          * busses itself so we should get into this branch reliably.
865          */
866                                 if (irq < 0 && dev->bus->parent) { /* go back to the bridge */
867                                         struct pci_dev * bridge = dev->bus->self;
868
869                                         pin = (pin + PCI_SLOT(dev->devfn)) % 4;
870                                         irq = IO_APIC_get_PCI_irq_vector(bridge->bus->number, 
871                                                         PCI_SLOT(bridge->devfn), pin);
872                                         if (irq >= 0)
873                                                 printk(KERN_WARNING "PCI: using PPB(B%d,I%d,P%d) to get irq %d\n", 
874                                                         bridge->bus->number, PCI_SLOT(bridge->devfn), pin, irq);
875                                 }
876                                 if (irq >= 0) {
877                                         if (use_pci_vector() &&
878                                                 !platform_legacy_irq(irq))
879                                                 irq = IO_APIC_VECTOR(irq);
880
881                                         printk(KERN_INFO "PCI->APIC IRQ transform: (B%d,I%d,P%d) -> %d\n",
882                                                 dev->bus->number, PCI_SLOT(dev->devfn), pin, irq);
883                                         dev->irq = irq;
884                                 }
885                         }
886                 }
887 #endif
888                 /*
889                  * Still no IRQ? Try to lookup one...
890                  */
891                 if (pin && !dev->irq)
892                         pcibios_lookup_irq(dev, 0);
893         }
894 }
895
896 static int __init pcibios_irq_init(void)
897 {
898         DBG("PCI: IRQ init\n");
899
900         if (pcibios_enable_irq || raw_pci_ops == NULL)
901                 return 0;
902
903         pirq_table = pirq_find_routing_table();
904
905 #ifdef CONFIG_PCI_BIOS
906         if (!pirq_table && (pci_probe & PCI_BIOS_IRQ_SCAN))
907                 pirq_table = pcibios_get_irq_routing_table();
908 #endif
909         if (pirq_table) {
910                 pirq_peer_trick();
911                 pirq_find_router(&pirq_router);
912                 if (pirq_table->exclusive_irqs) {
913                         int i;
914                         for (i=0; i<16; i++)
915                                 if (!(pirq_table->exclusive_irqs & (1 << i)))
916                                         pirq_penalty[i] += 100;
917                 }
918                 /* If we're using the I/O APIC, avoid using the PCI IRQ routing table */
919                 if (io_apic_assign_pci_irqs)
920                         pirq_table = NULL;
921         }
922
923         pcibios_enable_irq = pirq_enable_irq;
924
925         pcibios_fixup_irqs();
926         return 0;
927 }
928
929 subsys_initcall(pcibios_irq_init);
930
931
932 void pcibios_penalize_isa_irq(int irq)
933 {
934         /*
935          *  If any ISAPnP device reports an IRQ in its list of possible
936          *  IRQ's, we try to avoid assigning it to PCI devices.
937          */
938         pirq_penalty[irq] += 100;
939 }
940
941 int pirq_enable_irq(struct pci_dev *dev)
942 {
943         u8 pin;
944         extern int interrupt_line_quirk;
945         struct pci_dev *temp_dev;
946
947         pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
948         if (pin && !pcibios_lookup_irq(dev, 1) && !dev->irq) {
949                 char *msg;
950                 msg = "";
951                 if (io_apic_assign_pci_irqs) {
952                         int irq;
953
954                         if (pin) {
955                                 pin--;          /* interrupt pins are numbered starting from 1 */
956                                 irq = IO_APIC_get_PCI_irq_vector(dev->bus->number, PCI_SLOT(dev->devfn), pin);
957                                 /*
958                                  * Busses behind bridges are typically not listed in the MP-table.
959                                  * In this case we have to look up the IRQ based on the parent bus,
960                                  * parent slot, and pin number. The SMP code detects such bridged
961                                  * busses itself so we should get into this branch reliably.
962                                  */
963                                 temp_dev = dev;
964                                 while (irq < 0 && dev->bus->parent) { /* go back to the bridge */
965                                         struct pci_dev * bridge = dev->bus->self;
966
967                                         pin = (pin + PCI_SLOT(dev->devfn)) % 4;
968                                         irq = IO_APIC_get_PCI_irq_vector(bridge->bus->number, 
969                                                         PCI_SLOT(bridge->devfn), pin);
970                                         if (irq >= 0)
971                                                 printk(KERN_WARNING "PCI: using PPB(B%d,I%d,P%d) to get irq %d\n", 
972                                                         bridge->bus->number, PCI_SLOT(bridge->devfn), pin, irq);
973                                         dev = bridge;
974                                 }
975                                 dev = temp_dev;
976                                 if (irq >= 0) {
977 #ifdef CONFIG_PCI_USE_VECTOR
978                                         if (!platform_legacy_irq(irq))
979                                                 irq = IO_APIC_VECTOR(irq);
980 #endif
981                                         printk(KERN_INFO "PCI->APIC IRQ transform: (B%d,I%d,P%d) -> %d\n",
982                                                 dev->bus->number, PCI_SLOT(dev->devfn), pin, irq);
983                                         dev->irq = irq;
984                                         return 0;
985                                 } else
986                                         msg = " Probably buggy MP table.";
987                         }
988                 } else if (pci_probe & PCI_BIOS_IRQ_SCAN)
989                         msg = "";
990                 else
991                         msg = " Please try using pci=biosirq.";
992                         
993                 /* With IDE legacy devices the IRQ lookup failure is not a problem.. */
994                 if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE && !(dev->class & 0x5))
995                         return 0;
996                         
997                 printk(KERN_WARNING "PCI: No IRQ known for interrupt pin %c of device %s.%s\n",
998                        'A' + pin - 1, pci_name(dev), msg);
999         }
1000         /* VIA bridges use interrupt line for apic/pci steering across
1001            the V-Link */
1002         else if (interrupt_line_quirk)
1003                 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
1004         return 0;
1005 }
1006
1007 int pci_vector_resources(int last, int nr_released)
1008 {
1009         int count = nr_released;
1010
1011         int next = last;
1012         int offset = (last % 8);
1013
1014         while (next < FIRST_SYSTEM_VECTOR) {
1015                 next += 8;
1016 #ifdef CONFIG_X86_64
1017                 if (next == IA32_SYSCALL_VECTOR)
1018                         continue;
1019 #else
1020                 if (next == SYSCALL_VECTOR)
1021                         continue;
1022 #endif
1023                 count++;
1024                 if (next >= FIRST_SYSTEM_VECTOR) {
1025                         if (offset%8) {
1026                                 next = FIRST_DEVICE_VECTOR + offset;
1027                                 offset++;
1028                                 continue;
1029                         }
1030                         count--;
1031                 }
1032         }
1033
1034         return count;
1035 }