2 * Low-Level PCI Support for PC -- Routing of Interrupts
4 * (c) 1999--2000 Martin Mares <mj@suse.cz>
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/malloc.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
18 #include <asm/io_apic.h>
22 #define PIRQ_SIGNATURE (('$' << 0) + ('P' << 8) + ('I' << 16) + ('R' << 24))
23 #define PIRQ_VERSION 0x0100
25 static struct irq_routing_table
*pirq_table
;
28 * Never use: 0, 1, 2 (timer, keyboard, and cascade)
29 * Avoid using: 13, 14 and 15 (FP error and IDE).
30 * Penalize: 3, 4, 6, 7, 12 (known ISA uses: serial, floppy, parallel and mouse)
32 unsigned int pcibios_irq_mask
= 0xfff8;
34 static int pirq_penalty
[16] = {
35 1000000, 1000000, 1000000, 1000, 1000, 0, 1000, 1000,
36 0, 0, 0, 0, 1000, 100000, 100000, 100000
42 int (*get
)(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
);
43 int (*set
)(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
, int new);
47 * Search 0xf0000 -- 0xfffff for the PCI IRQ Routing Table.
50 static struct irq_routing_table
* __init
pirq_find_routing_table(void)
53 struct irq_routing_table
*rt
;
57 for(addr
= (u8
*) __va(0xf0000); addr
< (u8
*) __va(0x100000); addr
+= 16) {
58 rt
= (struct irq_routing_table
*) addr
;
59 if (rt
->signature
!= PIRQ_SIGNATURE
||
60 rt
->version
!= PIRQ_VERSION
||
62 rt
->size
< sizeof(struct irq_routing_table
))
65 for(i
=0; i
<rt
->size
; i
++)
68 DBG("PCI: Interrupt Routing Table found at 0x%p\n", rt
);
76 * If we have a IRQ routing table, use it to search for peer host
77 * bridges. It's a gross hack, but since there are no other known
78 * ways how to get a list of buses, we have to go this way.
81 static void __init
pirq_peer_trick(void)
83 struct irq_routing_table
*rt
= pirq_table
;
88 memset(busmap
, 0, sizeof(busmap
));
89 for(i
=0; i
< (rt
->size
- sizeof(struct irq_routing_table
)) / sizeof(struct irq_info
); i
++) {
94 DBG("%02x:%02x slot=%02x", e
->bus
, e
->devfn
/8, e
->slot
);
96 DBG(" %d:%02x/%04x", j
, e
->irq
[j
].link
, e
->irq
[j
].bitmap
);
104 * It might be a secondary bus, but in this case its parent is already
105 * known (ascending bus order) and therefore pci_scan_bus returns immediately.
107 if (busmap
[i
] && pci_scan_bus(i
, pci_root_bus
->ops
, NULL
))
108 printk("PCI: Discovered primary peer bus %02x [IRQ]\n", i
);
109 pcibios_last_bus
= -1;
113 * Code for querying and setting of IRQ routes on various interrupt routers.
116 static void eisa_set_level_irq(unsigned int irq
)
118 unsigned char mask
= 1 << (irq
& 7);
119 unsigned int port
= 0x4d0 + (irq
>> 3);
120 unsigned char val
= inb(port
);
124 outb(val
| mask
, port
);
129 * Common IRQ routing practice: nybbles in config space,
130 * offset by some magic constant.
132 static unsigned int read_config_nybble(struct pci_dev
*router
, unsigned offset
, unsigned nr
)
135 unsigned reg
= offset
+ (nr
>> 1);
137 pci_read_config_byte(router
, reg
, &x
);
138 return (nr
& 1) ? (x
>> 4) : (x
& 0xf);
141 static void write_config_nybble(struct pci_dev
*router
, unsigned offset
, unsigned nr
, unsigned int val
)
144 unsigned reg
= offset
+ (nr
>> 1);
146 pci_read_config_byte(router
, reg
, &x
);
147 x
= (nr
& 1) ? ((x
& 0x0f) | (val
<< 4)) : ((x
& 0xf0) | val
);
148 pci_write_config_byte(router
, reg
, x
);
152 * ALI pirq entries are damn ugly, and completely undocumented.
153 * This has been figured out from pirq tables, and it's not a pretty
156 static int pirq_ali_get(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
)
158 static unsigned char irqmap
[16] = { 0, 9, 3, 10, 4, 5, 7, 6, 1, 11, 0, 12, 0, 14, 0, 15 };
164 return irqmap
[read_config_nybble(router
, 0x48, pirq
-1)];
166 return irqmap
[read_config_nybble(router
, 0x44, 0)];
168 return irqmap
[read_config_nybble(router
, 0x75, 0)];
172 static void pirq_ali_ide_interrupt(struct pci_dev
*router
, unsigned reg
, unsigned val
, unsigned irq
)
176 pci_read_config_byte(router
, reg
, &x
);
177 x
= (x
& 0xe0) | val
; /* clear the level->edge transform */
178 pci_write_config_byte(router
, reg
, x
);
181 static int pirq_ali_set(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
, int irq
)
183 static unsigned char irqmap
[16] = { 0, 8, 0, 2, 4, 5, 7, 6, 0, 1, 3, 9, 11, 0, 13, 15 };
184 unsigned int val
= irqmap
[irq
];
189 write_config_nybble(router
, 0x48, pirq
-1, val
);
192 pirq_ali_ide_interrupt(router
, 0x44, val
, irq
);
195 pirq_ali_ide_interrupt(router
, 0x75, val
, irq
);
204 * The Intel PIIX4 pirq rules are fairly simple: "pirq" is
205 * just a pointer to the config space. However, something
206 * funny is going on with 0xfe/0xff, and apparently they
207 * should handle IDE irq routing. Ignore them for now.
209 static int pirq_piix_get(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
)
218 pci_read_config_byte(router
, pirq
, &x
);
219 return (x
< 16) ? x
: 0;
223 static int pirq_piix_set(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
, int irq
)
230 pci_write_config_byte(router
, pirq
, irq
);
236 * The VIA pirq rules are nibble-based, like ALI,
237 * but without the ugly irq number munging or the
238 * strange special cases..
240 static int pirq_via_get(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
)
242 return read_config_nybble(router
, 0x55, pirq
);
245 static int pirq_via_set(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
, int irq
)
247 write_config_nybble(router
, 0x55, pirq
, irq
);
252 * OPTI: high four bits are nibble pointer..
253 * I wonder what the low bits do?
255 static int pirq_opti_get(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
)
257 return read_config_nybble(router
, 0xb8, pirq
>> 4);
260 static int pirq_opti_set(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
, int irq
)
262 write_config_nybble(router
, 0xb8, pirq
>> 4, irq
);
267 * Cyrix: nibble offset 0x5C
269 static int pirq_cyrix_get(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
)
271 return read_config_nybble(router
, 0x5C, pirq
-1);
274 static int pirq_cyrix_set(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
, int irq
)
276 write_config_nybble(router
, 0x5C, pirq
-1, irq
);
280 static int pirq_sis_get(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
)
283 int reg
= 0x41 + (pirq
- 'A') ;
285 pci_read_config_byte(router
, reg
, &x
);
286 return (x
& 0x80) ? 0 : (x
& 0x0f);
289 static int pirq_sis_set(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
, int irq
)
292 int reg
= 0x41 + (pirq
- 'A') ;
294 pci_read_config_byte(router
, reg
, &x
);
295 x
= (pirq
& 0x20) ? 0 : (irq
& 0x0f);
296 pci_write_config_byte(router
, reg
, x
);
301 #ifdef CONFIG_PCI_BIOS
303 static int pirq_bios_set(struct pci_dev
*router
, struct pci_dev
*dev
, int pirq
, int irq
)
305 struct pci_dev
*bridge
;
306 int pin
= pci_get_interrupt_pin(dev
, &bridge
);
307 return pcibios_set_irq_routing(bridge
, pin
, irq
);
310 static struct irq_router pirq_bios_router
=
311 { "BIOS", 0, 0, NULL
, pirq_bios_set
};
315 static struct irq_router pirq_routers
[] = {
316 { "PIIX", PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82371FB_0
, pirq_piix_get
, pirq_piix_set
},
317 { "PIIX", PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82371SB_0
, pirq_piix_get
, pirq_piix_set
},
318 { "PIIX", PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82371AB_0
, pirq_piix_get
, pirq_piix_set
},
319 { "PIIX", PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82371MX
, pirq_piix_get
, pirq_piix_set
},
320 { "PIIX", PCI_VENDOR_ID_INTEL
, PCI_DEVICE_ID_INTEL_82443MX_0
, pirq_piix_get
, pirq_piix_set
},
322 { "ALI", PCI_VENDOR_ID_AL
, PCI_DEVICE_ID_AL_M1533
, pirq_ali_get
, pirq_ali_set
},
324 { "VIA", PCI_VENDOR_ID_VIA
, PCI_DEVICE_ID_VIA_82C586_0
, pirq_via_get
, pirq_via_set
},
325 { "VIA", PCI_VENDOR_ID_VIA
, PCI_DEVICE_ID_VIA_82C596
, pirq_via_get
, pirq_via_set
},
326 { "VIA", PCI_VENDOR_ID_VIA
, PCI_DEVICE_ID_VIA_82C686
, pirq_via_get
, pirq_via_set
},
328 { "OPTI", PCI_VENDOR_ID_OPTI
, PCI_DEVICE_ID_OPTI_82C700
, pirq_opti_get
, pirq_opti_set
},
330 { "NatSemi", PCI_VENDOR_ID_CYRIX
, PCI_DEVICE_ID_CYRIX_5520
, pirq_cyrix_get
, pirq_cyrix_set
},
331 { "SIS", PCI_VENDOR_ID_SI
, PCI_DEVICE_ID_SI_503
, pirq_sis_get
, pirq_sis_set
},
332 { "default", 0, 0, NULL
, NULL
}
335 static struct irq_router
*pirq_router
;
336 static struct pci_dev
*pirq_router_dev
;
338 static void __init
pirq_find_router(void)
340 struct irq_routing_table
*rt
= pirq_table
;
341 struct irq_router
*r
;
343 #ifdef CONFIG_PCI_BIOS
344 if (!rt
->signature
) {
345 printk("PCI: Using BIOS for IRQ routing\n");
346 pirq_router
= &pirq_bios_router
;
350 /* fall back to default router if nothing else found */
351 pirq_router
= pirq_routers
+ sizeof(pirq_routers
) / sizeof(pirq_routers
[0]) - 1;
353 pirq_router_dev
= pci_find_slot(rt
->rtr_bus
, rt
->rtr_devfn
);
354 if (!pirq_router_dev
) {
355 DBG("PCI: Interrupt router not found at %02x:%02x\n", rt
->rtr_bus
, rt
->rtr_devfn
);
359 for(r
=pirq_routers
; r
->vendor
; r
++) {
360 /* Exact match against router table entry? Use it! */
361 if (r
->vendor
== rt
->rtr_vendor
&& r
->device
== rt
->rtr_device
) {
365 /* Match against router device entry? Use it as a fallback */
366 if (r
->vendor
== pirq_router_dev
->vendor
&& r
->device
== pirq_router_dev
->device
) {
370 printk("PCI: Using IRQ router %s [%04x/%04x] at %s\n",
372 pirq_router_dev
->vendor
,
373 pirq_router_dev
->device
,
374 pirq_router_dev
->slot_name
);
377 static struct irq_info
*pirq_get_info(struct pci_dev
*dev
, int pin
)
379 struct irq_routing_table
*rt
= pirq_table
;
380 int entries
= (rt
->size
- sizeof(struct irq_routing_table
)) / sizeof(struct irq_info
);
381 struct irq_info
*info
;
383 for (info
= rt
->slots
; entries
--; info
++)
384 if (info
->bus
== dev
->bus
->number
&& PCI_SLOT(info
->devfn
) == PCI_SLOT(dev
->devfn
))
389 static void pcibios_test_irq_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
393 static int pcibios_lookup_irq(struct pci_dev
*dev
, int assign
)
395 struct irq_info
*info
;
396 int i
, pirq
, pin
, newirq
;
399 struct irq_router
*r
= pirq_router
;
400 struct pci_dev
*dev2
, *d
;
406 /* Find IRQ routing entry */
407 pin
= pci_get_interrupt_pin(dev
, &d
);
409 DBG(" -> no interrupt pin\n");
412 DBG("IRQ for %s(%d) via %s", dev
->slot_name
, pin
, d
->slot_name
);
413 info
= pirq_get_info(d
, pin
);
415 DBG(" -> not found in routing table\n");
418 pirq
= info
->irq
[pin
].link
;
419 mask
= info
->irq
[pin
].bitmap
;
421 DBG(" -> not routed\n");
424 DBG(" -> PIRQ %02x, mask %04x, excl %04x", pirq
, mask
, pirq_table
->exclusive_irqs
);
425 mask
&= pcibios_irq_mask
;
428 * Find the best IRQ to assign: use the one
429 * reported by the device if possible.
432 if (!newirq
&& assign
) {
433 for (i
= 0; i
< 16; i
++) {
434 if (!(mask
& (1 << i
)))
436 if (pirq_penalty
[i
] < pirq_penalty
[newirq
] &&
437 !request_irq(i
, pcibios_test_irq_handler
, SA_SHIRQ
, "pci-test", dev
)) {
443 DBG(" -> newirq=%d", newirq
);
445 /* Try to get current IRQ */
446 if (r
->get
&& (irq
= r
->get(pirq_router_dev
, d
, pirq
))) {
447 DBG(" -> got IRQ %d\n", irq
);
449 /* We refuse to override the dev->irq information. Give a warning! */
450 if (dev
->irq
&& dev
->irq
!= irq
) {
451 printk("IRQ routing conflict in pirq table! Try 'pci=autoirq'\n");
454 } else if (newirq
&& r
->set
&& (dev
->class >> 8) != PCI_CLASS_DISPLAY_VGA
) {
455 DBG(" -> assigning IRQ %d", newirq
);
456 if (r
->set(pirq_router_dev
, d
, pirq
, newirq
)) {
457 eisa_set_level_irq(newirq
);
465 DBG(" ... failed\n");
466 if (newirq
&& mask
== (1 << newirq
)) {
472 printk("PCI: %s IRQ %d for device %s\n", msg
, irq
, dev
->slot_name
);
474 /* Update IRQ for all devices with the same pirq value */
475 pci_for_each_dev(dev2
) {
476 if ((pin
= pci_get_interrupt_pin(dev2
, &d
)) >= 0 &&
477 (info
= pirq_get_info(d
, pin
)) &&
478 info
->irq
[pin
].link
== pirq
) {
482 printk("PCI: The same IRQ used for device %s\n", dev2
->slot_name
);
488 void __init
pcibios_irq_init(void)
490 DBG("PCI: IRQ init\n");
491 pirq_table
= pirq_find_routing_table();
492 #ifdef CONFIG_PCI_BIOS
493 if (!pirq_table
&& (pci_probe
& PCI_BIOS_IRQ_SCAN
))
494 pirq_table
= pcibios_get_irq_routing_table();
499 if (pirq_table
->exclusive_irqs
) {
502 if (!(pirq_table
->exclusive_irqs
& (1 << i
)))
503 pirq_penalty
[i
] += 100;
505 /* If we're using the I/O APIC, avoid using the PCI IRQ routing table */
506 if (io_apic_assign_pci_irqs
)
511 void __init
pcibios_fixup_irqs(void)
516 DBG("PCI: IRQ fixup\n");
517 pci_for_each_dev(dev
) {
519 * If the BIOS has set an out of range IRQ number, just ignore it.
520 * Also keep track of which IRQ's are already in use.
522 if (dev
->irq
>= 16) {
523 DBG("%s: ignoring bogus IRQ %d\n", dev
->slot_name
, dev
->irq
);
526 /* If the IRQ is already assigned to a PCI device, ignore its ISA use penalty */
527 if (pirq_penalty
[dev
->irq
] >= 100 && pirq_penalty
[dev
->irq
] < 100000)
528 pirq_penalty
[dev
->irq
] = 0;
529 pirq_penalty
[dev
->irq
]++;
532 pci_for_each_dev(dev
) {
533 pci_read_config_byte(dev
, PCI_INTERRUPT_PIN
, &pin
);
534 #ifdef CONFIG_X86_IO_APIC
536 * Recalculate IRQ numbers if we use the I/O APIC.
538 if (io_apic_assign_pci_irqs
)
543 pin
--; /* interrupt pins are numbered starting from 1 */
544 irq
= IO_APIC_get_PCI_irq_vector(dev
->bus
->number
, PCI_SLOT(dev
->devfn
), pin
);
546 * Will be removed completely if things work out well with fuzzy parsing
549 if (irq
< 0 && dev
->bus
->parent
) { /* go back to the bridge */
550 struct pci_dev
* bridge
= dev
->bus
->self
;
552 pin
= (pin
+ PCI_SLOT(dev
->devfn
)) % 4;
553 irq
= IO_APIC_get_PCI_irq_vector(bridge
->bus
->number
,
554 PCI_SLOT(bridge
->devfn
), pin
);
556 printk(KERN_WARNING
"PCI: using PPB(B%d,I%d,P%d) to get irq %d\n",
557 bridge
->bus
->number
, PCI_SLOT(bridge
->devfn
), pin
, irq
);
561 printk("PCI->APIC IRQ transform: (B%d,I%d,P%d) -> %d\n",
562 dev
->bus
->number
, PCI_SLOT(dev
->devfn
), pin
, irq
);
569 * Still no IRQ? Try to lookup one...
571 if (pin
&& !dev
->irq
)
572 pcibios_lookup_irq(dev
, 0);
576 void pcibios_penalize_isa_irq(int irq
)
579 * If any ISAPnP device reports an IRQ in its list of possible
580 * IRQ's, we try to avoid assigning it to PCI devices.
582 pirq_penalty
[irq
] += 100;
585 void pcibios_enable_irq(struct pci_dev
*dev
)
588 pci_read_config_byte(dev
, PCI_INTERRUPT_PIN
, &pin
);
589 if (pin
&& !pcibios_lookup_irq(dev
, 1) && !dev
->irq
) {
591 if (io_apic_assign_pci_irqs
)
592 msg
= " Probably buggy MP table.";
593 else if (pci_probe
& PCI_BIOS_IRQ_SCAN
)
596 msg
= " Please try using pci=biosirq.";
597 printk(KERN_WARNING
"PCI: No IRQ known for interrupt pin %c of device %s.%s\n",
598 'A' + pin
- 1, dev
->slot_name
, msg
);