4f9feba2fc95df61a10215f69adddf5c0a7c7409
[linux-flexiantxendom0-3.2.10.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
19 #include "pci.h"
20
21 #define PIRQ_SIGNATURE  (('$' << 0) + ('P' << 8) + ('I' << 16) + ('R' << 24))
22 #define PIRQ_VERSION 0x0100
23
24 int broken_hp_bios_irq9;
25
26 static struct irq_routing_table *pirq_table;
27
28 /*
29  * Never use: 0, 1, 2 (timer, keyboard, and cascade)
30  * Avoid using: 13, 14 and 15 (FP error and IDE).
31  * Penalize: 3, 4, 6, 7, 12 (known ISA uses: serial, floppy, parallel and mouse)
32  */
33 unsigned int pcibios_irq_mask = 0xfff8;
34
35 static int pirq_penalty[16] = {
36         1000000, 1000000, 1000000, 1000, 1000, 0, 1000, 1000,
37         0, 0, 0, 0, 1000, 100000, 100000, 100000
38 };
39
40 struct irq_router {
41         char *name;
42         u16 vendor, device;
43         int (*get)(struct pci_dev *router, struct pci_dev *dev, int pirq);
44         int (*set)(struct pci_dev *router, struct pci_dev *dev, int pirq, int new);
45 };
46
47 int (*pcibios_enable_irq)(struct pci_dev *dev) = NULL;
48
49 /*
50  *  Search 0xf0000 -- 0xfffff for the PCI IRQ Routing Table.
51  */
52
53 static struct irq_routing_table * __init pirq_find_routing_table(void)
54 {
55         u8 *addr;
56         struct irq_routing_table *rt;
57         int i;
58         u8 sum;
59
60         for(addr = (u8 *) __va(0xf0000); addr < (u8 *) __va(0x100000); addr += 16) {
61                 rt = (struct irq_routing_table *) addr;
62                 if (rt->signature != PIRQ_SIGNATURE ||
63                     rt->version != PIRQ_VERSION ||
64                     rt->size % 16 ||
65                     rt->size < sizeof(struct irq_routing_table))
66                         continue;
67                 sum = 0;
68                 for(i=0; i<rt->size; i++)
69                         sum += addr[i];
70                 if (!sum) {
71                         DBG("PCI: Interrupt Routing Table found at 0x%p\n", rt);
72                         return rt;
73                 }
74         }
75         return NULL;
76 }
77
78 /*
79  *  If we have a IRQ routing table, use it to search for peer host
80  *  bridges.  It's a gross hack, but since there are no other known
81  *  ways how to get a list of buses, we have to go this way.
82  */
83
84 static void __init pirq_peer_trick(void)
85 {
86         struct irq_routing_table *rt = pirq_table;
87         u8 busmap[256];
88         int i;
89         struct irq_info *e;
90
91         memset(busmap, 0, sizeof(busmap));
92         for(i=0; i < (rt->size - sizeof(struct irq_routing_table)) / sizeof(struct irq_info); i++) {
93                 e = &rt->slots[i];
94 #ifdef DEBUG
95                 {
96                         int j;
97                         DBG("%02x:%02x slot=%02x", e->bus, e->devfn/8, e->slot);
98                         for(j=0; j<4; j++)
99                                 DBG(" %d:%02x/%04x", j, e->irq[j].link, e->irq[j].bitmap);
100                         DBG("\n");
101                 }
102 #endif
103                 busmap[e->bus] = 1;
104         }
105         for(i = 1; i < 256; i++) {
106                 if (!busmap[i] || pci_find_bus(0, i))
107                         continue;
108                 if (pci_scan_bus(i, &pci_root_ops, NULL))
109                         printk(KERN_INFO "PCI: Discovered primary peer bus %02x [IRQ]\n", i);
110         }
111         pcibios_last_bus = -1;
112 }
113
114 /*
115  *  Code for querying and setting of IRQ routes on various interrupt routers.
116  */
117
118 void eisa_set_level_irq(unsigned int irq)
119 {
120         unsigned char mask = 1 << (irq & 7);
121         unsigned int port = 0x4d0 + (irq >> 3);
122         unsigned char val = inb(port);
123
124         if (!(val & mask)) {
125                 DBG(" -> edge");
126                 outb(val | mask, port);
127         }
128 }
129
130 /*
131  * Common IRQ routing practice: nybbles in config space,
132  * offset by some magic constant.
133  */
134 static unsigned int read_config_nybble(struct pci_dev *router, unsigned offset, unsigned nr)
135 {
136         u8 x;
137         unsigned reg = offset + (nr >> 1);
138
139         pci_read_config_byte(router, reg, &x);
140         return (nr & 1) ? (x >> 4) : (x & 0xf);
141 }
142
143 static void write_config_nybble(struct pci_dev *router, unsigned offset, unsigned nr, unsigned int val)
144 {
145         u8 x;
146         unsigned reg = offset + (nr >> 1);
147
148         pci_read_config_byte(router, reg, &x);
149         x = (nr & 1) ? ((x & 0x0f) | (val << 4)) : ((x & 0xf0) | val);
150         pci_write_config_byte(router, reg, x);
151 }
152
153 /*
154  * ALI pirq entries are damn ugly, and completely undocumented.
155  * This has been figured out from pirq tables, and it's not a pretty
156  * picture.
157  */
158 static int pirq_ali_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
159 {
160         static unsigned char irqmap[16] = { 0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15 };
161
162         return irqmap[read_config_nybble(router, 0x48, pirq-1)];
163 }
164
165 static int pirq_ali_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
166 {
167         static unsigned char irqmap[16] = { 0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15 };
168         unsigned int val = irqmap[irq];
169                 
170         if (val) {
171                 write_config_nybble(router, 0x48, pirq-1, val);
172                 return 1;
173         }
174         return 0;
175 }
176
177 /*
178  * The Intel PIIX4 pirq rules are fairly simple: "pirq" is
179  * just a pointer to the config space.
180  */
181 static int pirq_piix_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
182 {
183         u8 x;
184
185         pci_read_config_byte(router, pirq, &x);
186         return (x < 16) ? x : 0;
187 }
188
189 static int pirq_piix_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
190 {
191         pci_write_config_byte(router, pirq, irq);
192         return 1;
193 }
194
195 /*
196  * The VIA pirq rules are nibble-based, like ALI,
197  * but without the ugly irq number munging.
198  * However, PIRQD is in the upper instead of lower 4 bits.
199  */
200 static int pirq_via_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
201 {
202         return read_config_nybble(router, 0x55, pirq == 4 ? 5 : pirq);
203 }
204
205 static int pirq_via_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
206 {
207         write_config_nybble(router, 0x55, pirq == 4 ? 5 : pirq, irq);
208         return 1;
209 }
210
211 /*
212  * ITE 8330G pirq rules are nibble-based
213  * FIXME: pirqmap may be { 1, 0, 3, 2 },
214  *        2+3 are both mapped to irq 9 on my system
215  */
216 static int pirq_ite_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
217 {
218         static unsigned char pirqmap[4] = { 1, 0, 2, 3 };
219         return read_config_nybble(router,0x43, pirqmap[pirq-1]);
220 }
221
222 static int pirq_ite_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
223 {
224         static unsigned char pirqmap[4] = { 1, 0, 2, 3 };
225         write_config_nybble(router, 0x43, pirqmap[pirq-1], irq);
226         return 1;
227 }
228
229 /*
230  * OPTI: high four bits are nibble pointer..
231  * I wonder what the low bits do?
232  */
233 static int pirq_opti_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
234 {
235         return read_config_nybble(router, 0xb8, pirq >> 4);
236 }
237
238 static int pirq_opti_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
239 {
240         write_config_nybble(router, 0xb8, pirq >> 4, irq);
241         return 1;
242 }
243
244 /*
245  * Cyrix: nibble offset 0x5C
246  * 0x5C bits 7:4 is INTB bits 3:0 is INTA 
247  * 0x5D bits 7:4 is INTD bits 3:0 is INTC
248  */
249 static int pirq_cyrix_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
250 {
251         return read_config_nybble(router, 0x5C, (pirq-1)^1);
252 }
253
254 static int pirq_cyrix_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
255 {
256         write_config_nybble(router, 0x5C, (pirq-1)^1, irq);
257         return 1;
258 }
259
260 /*
261  *      PIRQ routing for SiS 85C503 router used in several SiS chipsets
262  *      According to the SiS 5595 datasheet (preliminary V1.0, 12/24/1997)
263  *      the related registers work as follows:
264  *      
265  *      general: one byte per re-routable IRQ,
266  *               bit 7      IRQ mapping enabled (0) or disabled (1)
267  *               bits [6:4] reserved
268  *               bits [3:0] IRQ to map to
269  *                   allowed: 3-7, 9-12, 14-15
270  *                   reserved: 0, 1, 2, 8, 13
271  *
272  *      individual registers in device config space:
273  *
274  *      0x41/0x42/0x43/0x44:    PCI INT A/B/C/D - bits as in general case
275  *
276  *      0x61:                   IDEIRQ: bits as in general case - but:
277  *                              bits [6:5] must be written 01
278  *                              bit 4 channel-select primary (0), secondary (1)
279  *
280  *      0x62:                   USBIRQ: bits as in general case - but:
281  *                              bit 4 OHCI function disabled (0), enabled (1)
282  *      
283  *      0x6a:                   ACPI/SCI IRQ - bits as in general case
284  *
285  *      0x7e:                   Data Acq. Module IRQ - bits as in general case
286  *
287  *      Apparently there are systems implementing PCI routing table using both
288  *      link values 0x01-0x04 and 0x41-0x44 for PCI INTA..D, but register offsets
289  *      like 0x62 as link values for USBIRQ e.g. So there is no simple
290  *      "register = offset + pirq" relation.
291  *      Currently we support PCI INTA..D and USBIRQ and try our best to handle
292  *      both link mappings.
293  *      IDE/ACPI/DAQ mapping is currently unsupported (left untouched as set by BIOS).
294  */
295
296 static int pirq_sis_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
297 {
298         u8 x;
299         int reg = pirq;
300
301         switch(pirq) {
302                 case 0x01:
303                 case 0x02:
304                 case 0x03:
305                 case 0x04:
306                         reg += 0x40;
307                 case 0x41:
308                 case 0x42:
309                 case 0x43:
310                 case 0x44:
311                 case 0x62:
312                         pci_read_config_byte(router, reg, &x);
313                         if (reg != 0x62)
314                                 break;
315                         if (!(x & 0x40))
316                                 return 0;
317                         break;
318                 case 0x61:
319                 case 0x6a:
320                 case 0x7e:
321                         printk(KERN_INFO "SiS pirq: advanced IDE/ACPI/DAQ mapping not yet implemented\n");
322                         return 0;
323                 default:                        
324                         printk(KERN_INFO "SiS router pirq escape (%d)\n", pirq);
325                         return 0;
326         }
327         return (x & 0x80) ? 0 : (x & 0x0f);
328 }
329
330 static int pirq_sis_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
331 {
332         u8 x;
333         int reg = pirq;
334
335         switch(pirq) {
336                 case 0x01:
337                 case 0x02:
338                 case 0x03:
339                 case 0x04:
340                         reg += 0x40;
341                 case 0x41:
342                 case 0x42:
343                 case 0x43:
344                 case 0x44:
345                 case 0x62:
346                         x = (irq&0x0f) ? (irq&0x0f) : 0x80;
347                         if (reg != 0x62)
348                                 break;
349                         /* always mark OHCI enabled, as nothing else knows about this */
350                         x |= 0x40;
351                         break;
352                 case 0x61:
353                 case 0x6a:
354                 case 0x7e:
355                         printk(KERN_INFO "advanced SiS pirq mapping not yet implemented\n");
356                         return 0;
357                 default:                        
358                         printk(KERN_INFO "SiS router pirq escape (%d)\n", pirq);
359                         return 0;
360         }
361         pci_write_config_byte(router, reg, x);
362
363         return 1;
364 }
365
366 /*
367  * VLSI: nibble offset 0x74 - educated guess due to routing table and
368  *       config space of VLSI 82C534 PCI-bridge/router (1004:0102)
369  *       Tested on HP OmniBook 800 covering PIRQ 1, 2, 4, 8 for onboard
370  *       devices, PIRQ 3 for non-pci(!) soundchip and (untested) PIRQ 6
371  *       for the busbridge to the docking station.
372  */
373
374 static int pirq_vlsi_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
375 {
376         if (pirq > 8) {
377                 printk(KERN_INFO "VLSI router pirq escape (%d)\n", pirq);
378                 return 0;
379         }
380         return read_config_nybble(router, 0x74, pirq-1);
381 }
382
383 static int pirq_vlsi_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
384 {
385         if (pirq > 8) {
386                 printk(KERN_INFO "VLSI router pirq escape (%d)\n", pirq);
387                 return 0;
388         }
389         write_config_nybble(router, 0x74, pirq-1, irq);
390         return 1;
391 }
392
393 /*
394  * ServerWorks: PCI interrupts mapped to system IRQ lines through Index
395  * and Redirect I/O registers (0x0c00 and 0x0c01).  The Index register
396  * format is (PCIIRQ## | 0x10), e.g.: PCIIRQ10=0x1a.  The Redirect
397  * register is a straight binary coding of desired PIC IRQ (low nibble).
398  *
399  * The 'link' value in the PIRQ table is already in the correct format
400  * for the Index register.  There are some special index values:
401  * 0x00 for ACPI (SCI), 0x01 for USB, 0x02 for IDE0, 0x04 for IDE1,
402  * and 0x03 for SMBus.
403  */
404 static int pirq_serverworks_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
405 {
406         outb_p(pirq, 0xc00);
407         return inb(0xc01) & 0xf;
408 }
409
410 static int pirq_serverworks_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
411 {
412         outb_p(pirq, 0xc00);
413         outb_p(irq, 0xc01);
414         return 1;
415 }
416
417 /* Support for AMD756 PCI IRQ Routing
418  * Jhon H. Caicedo <jhcaiced@osso.org.co>
419  * Jun/21/2001 0.2.0 Release, fixed to use "nybble" functions... (jhcaiced)
420  * Jun/19/2001 Alpha Release 0.1.0 (jhcaiced)
421  * The AMD756 pirq rules are nibble-based
422  * offset 0x56 0-3 PIRQA  4-7  PIRQB
423  * offset 0x57 0-3 PIRQC  4-7  PIRQD
424  */
425 static int pirq_amd756_get(struct pci_dev *router, struct pci_dev *dev, int pirq)
426 {
427         u8 irq;
428         irq = 0;
429         if (pirq <= 4)
430         {
431                 irq = read_config_nybble(router, 0x56, pirq - 1);
432         }
433         printk(KERN_INFO "AMD756: dev %04x:%04x, router pirq : %d get irq : %2d\n",
434                 dev->vendor, dev->device, pirq, irq);
435         return irq;
436 }
437
438 static int pirq_amd756_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
439 {
440         printk(KERN_INFO "AMD756: dev %04x:%04x, router pirq : %d SET irq : %2d\n", 
441                 dev->vendor, dev->device, pirq, irq);
442         if (pirq <= 4)
443         {
444                 write_config_nybble(router, 0x56, pirq - 1, irq);
445         }
446         return 1;
447 }
448
449 #ifdef CONFIG_PCI_BIOS
450
451 static int pirq_bios_set(struct pci_dev *router, struct pci_dev *dev, int pirq, int irq)
452 {
453         struct pci_dev *bridge;
454         int pin = pci_get_interrupt_pin(dev, &bridge);
455         return pcibios_set_irq_routing(bridge, pin, irq);
456 }
457
458 static struct irq_router pirq_bios_router =
459         { "BIOS", 0, 0, NULL, pirq_bios_set };
460
461 #endif
462
463 static struct irq_router pirq_routers[] = {
464         { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371FB_0, pirq_piix_get, pirq_piix_set },
465         { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371SB_0, pirq_piix_get, pirq_piix_set },
466         { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_0, pirq_piix_get, pirq_piix_set },
467         { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371MX,   pirq_piix_get, pirq_piix_set },
468         { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82443MX_0, pirq_piix_get, pirq_piix_set },
469         { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AA_0, pirq_piix_get, pirq_piix_set },
470         { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801AB_0, pirq_piix_get, pirq_piix_set },
471         { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_0, pirq_piix_get, pirq_piix_set },
472         { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801BA_10, pirq_piix_get, pirq_piix_set },
473         { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_0, pirq_piix_get, pirq_piix_set },
474         { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801CA_12, pirq_piix_get, pirq_piix_set },
475         { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, pirq_piix_get, pirq_piix_set },
476         { "PIIX", PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801E_0, pirq_piix_get, pirq_piix_set },
477
478         { "ALI", PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, pirq_ali_get, pirq_ali_set },
479
480         { "ITE", PCI_VENDOR_ID_ITE, PCI_DEVICE_ID_ITE_IT8330G_0, pirq_ite_get, pirq_ite_set },
481
482         { "VIA", PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C586_0, pirq_via_get, pirq_via_set },
483         { "VIA", PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C596, pirq_via_get, pirq_via_set },
484         { "VIA", PCI_VENDOR_ID_VIA, PCI_DEVICE_ID_VIA_82C686, pirq_via_get, pirq_via_set },
485
486         { "OPTI", PCI_VENDOR_ID_OPTI, PCI_DEVICE_ID_OPTI_82C700, pirq_opti_get, pirq_opti_set },
487
488         { "NatSemi", PCI_VENDOR_ID_CYRIX, PCI_DEVICE_ID_CYRIX_5520, pirq_cyrix_get, pirq_cyrix_set },
489         { "SIS", PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503, pirq_sis_get, pirq_sis_set },
490         { "VLSI 82C534", PCI_VENDOR_ID_VLSI, PCI_DEVICE_ID_VLSI_82C534, pirq_vlsi_get, pirq_vlsi_set },
491         { "ServerWorks", PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_OSB4,
492           pirq_serverworks_get, pirq_serverworks_set },
493         { "ServerWorks", PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_CSB5,
494           pirq_serverworks_get, pirq_serverworks_set },
495         { "AMD756 VIPER", PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_740B,
496                 pirq_amd756_get, pirq_amd756_set },
497         { "AMD766", PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_VIPER_7413,
498                 pirq_amd756_get, pirq_amd756_set },
499         { "AMD768", PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_OPUS_7443,
500                 pirq_amd756_get, pirq_amd756_set },
501
502         { "default", 0, 0, NULL, NULL }
503 };
504
505 static struct irq_router *pirq_router;
506 static struct pci_dev *pirq_router_dev;
507
508 static void __init pirq_find_router(void)
509 {
510         struct irq_routing_table *rt = pirq_table;
511         struct irq_router *r;
512
513 #ifdef CONFIG_PCI_BIOS
514         if (!rt->signature) {
515                 printk(KERN_INFO "PCI: Using BIOS for IRQ routing\n");
516                 pirq_router = &pirq_bios_router;
517                 return;
518         }
519 #endif
520
521         DBG("PCI: Attempting to find IRQ router for %04x:%04x\n",
522             rt->rtr_vendor, rt->rtr_device);
523
524         /* fall back to default router if nothing else found */
525         pirq_router = &pirq_routers[ARRAY_SIZE(pirq_routers) - 1];
526
527         pirq_router_dev = pci_find_slot(rt->rtr_bus, rt->rtr_devfn);
528         if (!pirq_router_dev) {
529                 DBG("PCI: Interrupt router not found at %02x:%02x\n", rt->rtr_bus, rt->rtr_devfn);
530                 return;
531         }
532
533         for(r=pirq_routers; r->vendor; r++) {
534                 /* Exact match against router table entry? Use it! */
535                 if (r->vendor == rt->rtr_vendor && r->device == rt->rtr_device) {
536                         pirq_router = r;
537                         break;
538                 }
539                 /* Match against router device entry? Use it as a fallback */
540                 if (r->vendor == pirq_router_dev->vendor && r->device == pirq_router_dev->device) {
541                         pirq_router = r;
542                 }
543         }
544         printk(KERN_INFO "PCI: Using IRQ router %s [%04x/%04x] at %s\n",
545                 pirq_router->name,
546                 pirq_router_dev->vendor,
547                 pirq_router_dev->device,
548                 pirq_router_dev->slot_name);
549 }
550
551 static struct irq_info *pirq_get_info(struct pci_dev *dev)
552 {
553         struct irq_routing_table *rt = pirq_table;
554         int entries = (rt->size - sizeof(struct irq_routing_table)) / sizeof(struct irq_info);
555         struct irq_info *info;
556
557         for (info = rt->slots; entries--; info++)
558                 if (info->bus == dev->bus->number && PCI_SLOT(info->devfn) == PCI_SLOT(dev->devfn))
559                         return info;
560         return NULL;
561 }
562
563 static irqreturn_t pcibios_test_irq_handler(int irq, void *dev_id, struct pt_regs *regs)
564 {
565         return IRQ_NONE;
566 }
567
568 static int pcibios_lookup_irq(struct pci_dev *dev, int assign)
569 {
570         u8 pin;
571         struct irq_info *info;
572         int i, pirq, newirq;
573         int irq = 0;
574         u32 mask;
575         struct irq_router *r = pirq_router;
576         struct pci_dev *dev2 = NULL;
577         char *msg = NULL;
578
579         /* Find IRQ pin */
580         pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
581         if (!pin) {
582                 DBG(" -> no interrupt pin\n");
583                 return 0;
584         }
585         pin = pin - 1;
586
587         /* Find IRQ routing entry */
588
589         if (!pirq_table)
590                 return 0;
591         
592         DBG("IRQ for %s:%d", dev->slot_name, pin);
593         info = pirq_get_info(dev);
594         if (!info) {
595                 DBG(" -> not found in routing table\n");
596                 return 0;
597         }
598         pirq = info->irq[pin].link;
599         mask = info->irq[pin].bitmap;
600         if (!pirq) {
601                 DBG(" -> not routed\n");
602                 return 0;
603         }
604         DBG(" -> PIRQ %02x, mask %04x, excl %04x", pirq, mask, pirq_table->exclusive_irqs);
605         mask &= pcibios_irq_mask;
606
607         /* Work around broken HP Pavilion Notebooks which assign USB to
608            IRQ 9 even though it is actually wired to IRQ 11 */
609
610         if (broken_hp_bios_irq9 && pirq == 0x59 && dev->irq == 9) {
611                 dev->irq = 11;
612                 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, 11);
613                 r->set(pirq_router_dev, dev, pirq, 11);
614         }
615
616         /*
617          * Find the best IRQ to assign: use the one
618          * reported by the device if possible.
619          */
620         newirq = dev->irq;
621         if (!((1 << newirq) & mask)) {
622                 if ( pci_probe & PCI_USE_PIRQ_MASK) newirq = 0;
623                 else printk(KERN_WARNING "PCI: IRQ %i for device %s doesn't match PIRQ mask - try pci=usepirqmask\n", newirq, dev->slot_name);
624         }
625         if (!newirq && assign) {
626                 for (i = 0; i < 16; i++) {
627                         if (!(mask & (1 << i)))
628                                 continue;
629                         if (pirq_penalty[i] < pirq_penalty[newirq] &&
630                             !request_irq(i, pcibios_test_irq_handler, SA_SHIRQ, "pci-test", dev)) {
631                                 free_irq(i, dev);
632                                 newirq = i;
633                         }
634                 }
635         }
636         DBG(" -> newirq=%d", newirq);
637
638         /* Check if it is hardcoded */
639         if ((pirq & 0xf0) == 0xf0) {
640                 irq = pirq & 0xf;
641                 DBG(" -> hardcoded IRQ %d\n", irq);
642                 msg = "Hardcoded";
643         } else if ( r->get && (irq = r->get(pirq_router_dev, dev, pirq)) && \
644         ((!(pci_probe & PCI_USE_PIRQ_MASK)) || ((1 << irq) & mask)) ) {
645                 DBG(" -> got IRQ %d\n", irq);
646                 msg = "Found";
647         } else if (newirq && r->set && (dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) {
648                 DBG(" -> assigning IRQ %d", newirq);
649                 if (r->set(pirq_router_dev, dev, pirq, newirq)) {
650                         eisa_set_level_irq(newirq);
651                         DBG(" ... OK\n");
652                         msg = "Assigned";
653                         irq = newirq;
654                 }
655         }
656
657         if (!irq) {
658                 DBG(" ... failed\n");
659                 if (newirq && mask == (1 << newirq)) {
660                         msg = "Guessed";
661                         irq = newirq;
662                 } else
663                         return 0;
664         }
665         printk(KERN_INFO "PCI: %s IRQ %d for device %s\n", msg, irq, dev->slot_name);
666
667         /* Update IRQ for all devices with the same pirq value */
668         while ((dev2 = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev2)) != NULL) {
669                 pci_read_config_byte(dev2, PCI_INTERRUPT_PIN, &pin);
670                 if (!pin)
671                         continue;
672                 pin--;
673                 info = pirq_get_info(dev2);
674                 if (!info)
675                         continue;
676                 if (info->irq[pin].link == pirq) {
677                         /* We refuse to override the dev->irq information. Give a warning! */
678                         if ( dev2->irq && dev2->irq != irq && \
679                         (!(pci_probe & PCI_USE_PIRQ_MASK) || \
680                         ((1 << dev2->irq) & mask)) ) {
681                                 printk(KERN_INFO "IRQ routing conflict for %s, have irq %d, want irq %d\n",
682                                        dev2->slot_name, dev2->irq, irq);
683                                 continue;
684                         }
685                         dev2->irq = irq;
686                         pirq_penalty[irq]++;
687                         if (dev != dev2)
688                                 printk(KERN_INFO "PCI: Sharing IRQ %d with %s\n", irq, dev2->slot_name);
689                 }
690         }
691         return 1;
692 }
693
694 static void __init pcibios_fixup_irqs(void)
695 {
696         struct pci_dev *dev = NULL;
697         u8 pin;
698
699         DBG("PCI: IRQ fixup\n");
700         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
701                 /*
702                  * If the BIOS has set an out of range IRQ number, just ignore it.
703                  * Also keep track of which IRQ's are already in use.
704                  */
705                 if (dev->irq >= 16) {
706                         DBG("%s: ignoring bogus IRQ %d\n", dev->slot_name, dev->irq);
707                         dev->irq = 0;
708                 }
709                 /* If the IRQ is already assigned to a PCI device, ignore its ISA use penalty */
710                 if (pirq_penalty[dev->irq] >= 100 && pirq_penalty[dev->irq] < 100000)
711                         pirq_penalty[dev->irq] = 0;
712                 pirq_penalty[dev->irq]++;
713         }
714
715         dev = NULL;
716         while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
717                 pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
718 #ifdef CONFIG_X86_IO_APIC
719                 /*
720                  * Recalculate IRQ numbers if we use the I/O APIC.
721                  */
722                 if (io_apic_assign_pci_irqs)
723                 {
724                         int irq;
725
726                         if (pin) {
727                                 pin--;          /* interrupt pins are numbered starting from 1 */
728                                 irq = IO_APIC_get_PCI_irq_vector(dev->bus->number, PCI_SLOT(dev->devfn), pin);
729         /*
730          * Busses behind bridges are typically not listed in the MP-table.
731          * In this case we have to look up the IRQ based on the parent bus,
732          * parent slot, and pin number. The SMP code detects such bridged
733          * busses itself so we should get into this branch reliably.
734          */
735                                 if (irq < 0 && dev->bus->parent) { /* go back to the bridge */
736                                         struct pci_dev * bridge = dev->bus->self;
737
738                                         pin = (pin + PCI_SLOT(dev->devfn)) % 4;
739                                         irq = IO_APIC_get_PCI_irq_vector(bridge->bus->number, 
740                                                         PCI_SLOT(bridge->devfn), pin);
741                                         if (irq >= 0)
742                                                 printk(KERN_WARNING "PCI: using PPB(B%d,I%d,P%d) to get irq %d\n", 
743                                                         bridge->bus->number, PCI_SLOT(bridge->devfn), pin, irq);
744                                 }
745                                 if (irq >= 0) {
746                                         printk(KERN_INFO "PCI->APIC IRQ transform: (B%d,I%d,P%d) -> %d\n",
747                                                 dev->bus->number, PCI_SLOT(dev->devfn), pin, irq);
748                                         dev->irq = irq;
749                                 }
750                         }
751                 }
752 #endif
753                 /*
754                  * Still no IRQ? Try to lookup one...
755                  */
756                 if (pin && !dev->irq)
757                         pcibios_lookup_irq(dev, 0);
758         }
759 }
760
761 static int __init pcibios_irq_init(void)
762 {
763         DBG("PCI: IRQ init\n");
764
765         if (pcibios_enable_irq)
766                 return 0;
767
768         pirq_table = pirq_find_routing_table();
769
770 #ifdef CONFIG_PCI_BIOS
771         if (!pirq_table && (pci_probe & PCI_BIOS_IRQ_SCAN))
772                 pirq_table = pcibios_get_irq_routing_table();
773 #endif
774         if (pirq_table) {
775                 pirq_peer_trick();
776                 pirq_find_router();
777                 if (pirq_table->exclusive_irqs) {
778                         int i;
779                         for (i=0; i<16; i++)
780                                 if (!(pirq_table->exclusive_irqs & (1 << i)))
781                                         pirq_penalty[i] += 100;
782                 }
783                 /* If we're using the I/O APIC, avoid using the PCI IRQ routing table */
784                 if (io_apic_assign_pci_irqs)
785                         pirq_table = NULL;
786         }
787
788         pcibios_enable_irq = pirq_enable_irq;
789
790         pcibios_fixup_irqs();
791         return 0;
792 }
793
794 subsys_initcall(pcibios_irq_init);
795
796
797 void pcibios_penalize_isa_irq(int irq)
798 {
799         /*
800          *  If any ISAPnP device reports an IRQ in its list of possible
801          *  IRQ's, we try to avoid assigning it to PCI devices.
802          */
803         pirq_penalty[irq] += 100;
804 }
805
806 int pirq_enable_irq(struct pci_dev *dev)
807 {
808         u8 pin;
809         extern int interrupt_line_quirk;
810         pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
811         if (pin && !pcibios_lookup_irq(dev, 1) && !dev->irq) {
812                 char *msg;
813                 if (io_apic_assign_pci_irqs)
814                         msg = " Probably buggy MP table.";
815                 else if (pci_probe & PCI_BIOS_IRQ_SCAN)
816                         msg = "";
817                 else
818                         msg = " Please try using pci=biosirq.";
819                         
820                 /* With IDE legacy devices the IRQ lookup failure is not a problem.. */
821                 if (dev->class >> 8 == PCI_CLASS_STORAGE_IDE && !(dev->class & 0x5))
822                         return 0;
823                         
824                 printk(KERN_WARNING "PCI: No IRQ known for interrupt pin %c of device %s.%s\n",
825                        'A' + pin - 1, dev->slot_name, msg);
826         }
827         /* VIA bridges use interrupt line for apic/pci steering across
828            the V-Link */
829         else if (interrupt_line_quirk)
830                 pci_write_config_byte(dev, PCI_INTERRUPT_LINE, dev->irq);
831         return 0;
832 }