1 #include <linux/linkage.h>
2 #include <linux/errno.h>
3 #include <linux/signal.h>
4 #include <linux/sched.h>
5 #include <linux/ioport.h>
6 #include <linux/interrupt.h>
7 #include <linux/timex.h>
8 #include <linux/slab.h>
9 #include <linux/random.h>
10 #include <linux/smp_lock.h>
11 #include <linux/init.h>
12 #include <linux/kernel_stat.h>
13 #include <linux/sysdev.h>
14 #include <linux/bitops.h>
17 #include <asm/atomic.h>
18 #include <asm/system.h>
20 #include <asm/hw_irq.h>
21 #include <asm/pgtable.h>
22 #include <asm/delay.h>
27 * Common place to define all x86 IRQ vectors
29 * This builds up the IRQ handler stubs using some ugly macros in irq.h
31 * These macros create the low-level assembly IRQ routines that save
32 * register context and call do_IRQ(). do_IRQ() then does all the
33 * operations that are needed to keep the AT (or SMP IOAPIC)
34 * interrupt-controller happy.
40 #define BUILD_16_IRQS(x) \
41 BI(x,0) BI(x,1) BI(x,2) BI(x,3) \
42 BI(x,4) BI(x,5) BI(x,6) BI(x,7) \
43 BI(x,8) BI(x,9) BI(x,a) BI(x,b) \
44 BI(x,c) BI(x,d) BI(x,e) BI(x,f)
46 #define BUILD_15_IRQS(x) \
47 BI(x,0) BI(x,1) BI(x,2) BI(x,3) \
48 BI(x,4) BI(x,5) BI(x,6) BI(x,7) \
49 BI(x,8) BI(x,9) BI(x,a) BI(x,b) \
50 BI(x,c) BI(x,d) BI(x,e)
53 * ISA PIC or low IO-APIC triggered (INTA-cycle or APIC) interrupts:
54 * (these are usually mapped to vectors 0x20-0x2f)
59 * The IO-APIC gives us many more interrupt sources. Most of these
60 * are unused but an SMP system is supposed to have enough memory ...
61 * sometimes (mostly wrt. hw bugs) we get corrupted vectors all
62 * across the spectrum, so we really want to be prepared to get all
63 * of these. Plus, more powerful systems might have more than 64
66 * (these are usually mapped into the 0x30-0xff vector range)
68 BUILD_16_IRQS(0x1) BUILD_16_IRQS(0x2) BUILD_16_IRQS(0x3)
69 BUILD_16_IRQS(0x4) BUILD_16_IRQS(0x5) BUILD_16_IRQS(0x6) BUILD_16_IRQS(0x7)
70 BUILD_16_IRQS(0x8) BUILD_16_IRQS(0x9) BUILD_16_IRQS(0xa) BUILD_16_IRQS(0xb)
71 BUILD_16_IRQS(0xc) BUILD_16_IRQS(0xd)
85 #define IRQLIST_16(x) \
86 IRQ(x,0), IRQ(x,1), IRQ(x,2), IRQ(x,3), \
87 IRQ(x,4), IRQ(x,5), IRQ(x,6), IRQ(x,7), \
88 IRQ(x,8), IRQ(x,9), IRQ(x,a), IRQ(x,b), \
89 IRQ(x,c), IRQ(x,d), IRQ(x,e), IRQ(x,f)
91 #define IRQLIST_15(x) \
92 IRQ(x,0), IRQ(x,1), IRQ(x,2), IRQ(x,3), \
93 IRQ(x,4), IRQ(x,5), IRQ(x,6), IRQ(x,7), \
94 IRQ(x,8), IRQ(x,9), IRQ(x,a), IRQ(x,b), \
95 IRQ(x,c), IRQ(x,d), IRQ(x,e)
97 void (*interrupt[NR_IRQS])(void) = {
100 IRQLIST_16(0x1), IRQLIST_16(0x2), IRQLIST_16(0x3),
101 IRQLIST_16(0x4), IRQLIST_16(0x5), IRQLIST_16(0x6), IRQLIST_16(0x7),
102 IRQLIST_16(0x8), IRQLIST_16(0x9), IRQLIST_16(0xa), IRQLIST_16(0xb),
103 IRQLIST_16(0xc), IRQLIST_16(0xd)
105 #ifdef CONFIG_PCI_MSI
116 * This is the 'legacy' 8259A Programmable Interrupt Controller,
117 * present in the majority of PC/AT boxes.
118 * plus some generic x86 specific things if generic specifics makes
120 * this file should become arch/i386/kernel/irq.c when the old irq.c
121 * moves to arch independent land
124 DEFINE_SPINLOCK(i8259A_lock);
126 static int i8259A_auto_eoi;
128 static void end_8259A_irq (unsigned int irq)
132 printk("return %p stack %p ti %p\n", __builtin_return_address(0), &var, task_thread_info(current));
137 if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)) &&
138 irq_desc[irq].action)
139 enable_8259A_irq(irq);
142 #define shutdown_8259A_irq disable_8259A_irq
144 static void mask_and_ack_8259A(unsigned int);
146 static unsigned int startup_8259A_irq(unsigned int irq)
148 enable_8259A_irq(irq);
149 return 0; /* never anything pending */
152 static struct hw_interrupt_type i8259A_irq_type = {
153 .typename = "XT-PIC",
154 .startup = startup_8259A_irq,
155 .shutdown = shutdown_8259A_irq,
156 .enable = enable_8259A_irq,
157 .disable = disable_8259A_irq,
158 .ack = mask_and_ack_8259A,
159 .end = end_8259A_irq,
163 * 8259A PIC functions to handle ISA devices:
167 * This contains the irq mask for both 8259A irq controllers,
169 static unsigned int cached_irq_mask = 0xffff;
171 #define __byte(x,y) (((unsigned char *)&(y))[x])
172 #define cached_21 (__byte(0,cached_irq_mask))
173 #define cached_A1 (__byte(1,cached_irq_mask))
176 * Not all IRQs can be routed through the IO-APIC, eg. on certain (older)
177 * boards the timer interrupt is not really connected to any IO-APIC pin,
178 * it's fed to the master 8259A's IR0 line only.
180 * Any '1' bit in this mask means the IRQ is routed through the IO-APIC.
181 * this 'mixed mode' IRQ handling costs nothing because it's only used
184 unsigned long io_apic_irqs;
186 void disable_8259A_irq(unsigned int irq)
188 unsigned int mask = 1 << irq;
191 spin_lock_irqsave(&i8259A_lock, flags);
192 cached_irq_mask |= mask;
194 outb(cached_A1,0xA1);
196 outb(cached_21,0x21);
197 spin_unlock_irqrestore(&i8259A_lock, flags);
200 void enable_8259A_irq(unsigned int irq)
202 unsigned int mask = ~(1 << irq);
205 spin_lock_irqsave(&i8259A_lock, flags);
206 cached_irq_mask &= mask;
208 outb(cached_A1,0xA1);
210 outb(cached_21,0x21);
211 spin_unlock_irqrestore(&i8259A_lock, flags);
214 int i8259A_irq_pending(unsigned int irq)
216 unsigned int mask = 1<<irq;
220 spin_lock_irqsave(&i8259A_lock, flags);
222 ret = inb(0x20) & mask;
224 ret = inb(0xA0) & (mask >> 8);
225 spin_unlock_irqrestore(&i8259A_lock, flags);
230 void make_8259A_irq(unsigned int irq)
232 disable_irq_nosync(irq);
233 io_apic_irqs &= ~(1<<irq);
234 irq_desc[irq].chip = &i8259A_irq_type;
239 * This function assumes to be called rarely. Switching between
240 * 8259A registers is slow.
241 * This has to be protected by the irq controller spinlock
242 * before being called.
244 static inline int i8259A_irq_real(unsigned int irq)
247 int irqmask = 1<<irq;
250 outb(0x0B,0x20); /* ISR register */
251 value = inb(0x20) & irqmask;
252 outb(0x0A,0x20); /* back to the IRR register */
255 outb(0x0B,0xA0); /* ISR register */
256 value = inb(0xA0) & (irqmask >> 8);
257 outb(0x0A,0xA0); /* back to the IRR register */
262 * Careful! The 8259A is a fragile beast, it pretty
263 * much _has_ to be done exactly like this (mask it
264 * first, _then_ send the EOI, and the order of EOI
265 * to the two 8259s is important!
267 static void mask_and_ack_8259A(unsigned int irq)
269 unsigned int irqmask = 1 << irq;
272 spin_lock_irqsave(&i8259A_lock, flags);
274 * Lightweight spurious IRQ detection. We do not want
275 * to overdo spurious IRQ handling - it's usually a sign
276 * of hardware problems, so we only do the checks we can
277 * do without slowing down good hardware unnecessarily.
279 * Note that IRQ7 and IRQ15 (the two spurious IRQs
280 * usually resulting from the 8259A-1|2 PICs) occur
281 * even if the IRQ is masked in the 8259A. Thus we
282 * can check spurious 8259A IRQs without doing the
283 * quite slow i8259A_irq_real() call for every IRQ.
284 * This does not cover 100% of spurious interrupts,
285 * but should be enough to warn the user that there
286 * is something bad going on ...
288 if (cached_irq_mask & irqmask)
289 goto spurious_8259A_irq;
290 cached_irq_mask |= irqmask;
294 inb(0xA1); /* DUMMY - (do we need this?) */
295 outb(cached_A1,0xA1);
296 outb(0x60+(irq&7),0xA0);/* 'Specific EOI' to slave */
297 outb(0x62,0x20); /* 'Specific EOI' to master-IRQ2 */
299 inb(0x21); /* DUMMY - (do we need this?) */
300 outb(cached_21,0x21);
301 outb(0x60+irq,0x20); /* 'Specific EOI' to master */
303 spin_unlock_irqrestore(&i8259A_lock, flags);
308 * this is the slow path - should happen rarely.
310 if (i8259A_irq_real(irq))
312 * oops, the IRQ _is_ in service according to the
313 * 8259A - not spurious, go handle it.
315 goto handle_real_irq;
318 static int spurious_irq_mask;
320 * At this point we can be sure the IRQ is spurious,
321 * lets ACK and report it. [once per IRQ]
323 if (!(spurious_irq_mask & irqmask)) {
324 printk(KERN_DEBUG "spurious 8259A interrupt: IRQ%d.\n", irq);
325 spurious_irq_mask |= irqmask;
327 atomic_inc(&irq_err_count);
329 * Theoretically we do not have to handle this IRQ,
330 * but in Linux this does not cause problems and is
333 goto handle_real_irq;
337 void init_8259A(int auto_eoi)
341 i8259A_auto_eoi = auto_eoi;
343 spin_lock_irqsave(&i8259A_lock, flags);
345 outb(0xff, 0x21); /* mask all of 8259A-1 */
346 outb(0xff, 0xA1); /* mask all of 8259A-2 */
349 * outb_p - this has to work on a wide range of PC hardware.
351 outb_p(0x11, 0x20); /* ICW1: select 8259A-1 init */
352 outb_p(0x20 + 0, 0x21); /* ICW2: 8259A-1 IR0-7 mapped to 0x20-0x27 */
353 outb_p(0x04, 0x21); /* 8259A-1 (the master) has a slave on IR2 */
355 outb_p(0x03, 0x21); /* master does Auto EOI */
357 outb_p(0x01, 0x21); /* master expects normal EOI */
359 outb_p(0x11, 0xA0); /* ICW1: select 8259A-2 init */
360 outb_p(0x20 + 8, 0xA1); /* ICW2: 8259A-2 IR0-7 mapped to 0x28-0x2f */
361 outb_p(0x02, 0xA1); /* 8259A-2 is a slave on master's IR2 */
362 outb_p(0x01, 0xA1); /* (slave's support for AEOI in flat mode
363 is to be investigated) */
367 * in AEOI mode we just have to mask the interrupt
370 i8259A_irq_type.ack = disable_8259A_irq;
372 i8259A_irq_type.ack = mask_and_ack_8259A;
374 udelay(100); /* wait for 8259A to initialize */
376 outb(cached_21, 0x21); /* restore master IRQ mask */
377 outb(cached_A1, 0xA1); /* restore slave IRQ mask */
379 spin_unlock_irqrestore(&i8259A_lock, flags);
382 static char irq_trigger[2];
384 * ELCR registers (0x4d0, 0x4d1) control edge/level of IRQ
386 static void restore_ELCR(char *trigger)
388 outb(trigger[0], 0x4d0);
389 outb(trigger[1], 0x4d1);
392 static void save_ELCR(char *trigger)
394 /* IRQ 0,1,2,8,13 are marked as reserved */
395 trigger[0] = inb(0x4d0) & 0xF8;
396 trigger[1] = inb(0x4d1) & 0xDE;
399 static int i8259A_resume(struct sys_device *dev)
401 init_8259A(i8259A_auto_eoi);
402 restore_ELCR(irq_trigger);
406 static int i8259A_suspend(struct sys_device *dev, pm_message_t state)
408 save_ELCR(irq_trigger);
412 static int i8259A_shutdown(struct sys_device *dev)
414 /* Put the i8259A into a quiescent state that
415 * the kernel initialization code can get it
418 outb(0xff, 0x21); /* mask all of 8259A-1 */
419 outb(0xff, 0xA1); /* mask all of 8259A-1 */
423 static struct sysdev_class i8259_sysdev_class = {
424 set_kset_name("i8259"),
425 .suspend = i8259A_suspend,
426 .resume = i8259A_resume,
427 .shutdown = i8259A_shutdown,
430 static struct sys_device device_i8259A = {
432 .cls = &i8259_sysdev_class,
435 static int __init i8259A_init_sysfs(void)
437 int error = sysdev_class_register(&i8259_sysdev_class);
439 error = sysdev_register(&device_i8259A);
443 device_initcall(i8259A_init_sysfs);
446 * IRQ2 is cascade interrupt to second interrupt controller
449 static struct irqaction irq2 = { no_action, 0, CPU_MASK_NONE, "cascade", NULL, NULL};
451 void __init init_ISA_irqs (void)
458 for (i = 0; i < NR_IRQS; i++) {
459 irq_desc[i].status = IRQ_DISABLED;
460 irq_desc[i].action = NULL;
461 irq_desc[i].depth = 1;
465 * 16 old-style INTA-cycle interrupts:
467 irq_desc[i].chip = &i8259A_irq_type;
470 * 'high' PCI IRQs filled in on demand
472 irq_desc[i].chip = &no_irq_type;
477 void apic_timer_interrupt(void);
478 void spurious_interrupt(void);
479 void error_interrupt(void);
480 void reschedule_interrupt(void);
481 void call_function_interrupt(void);
482 void invalidate_interrupt0(void);
483 void invalidate_interrupt1(void);
484 void invalidate_interrupt2(void);
485 void invalidate_interrupt3(void);
486 void invalidate_interrupt4(void);
487 void invalidate_interrupt5(void);
488 void invalidate_interrupt6(void);
489 void invalidate_interrupt7(void);
490 void thermal_interrupt(void);
491 void threshold_interrupt(void);
492 void i8254_timer_resume(void);
494 static void setup_timer_hardware(void)
496 outb_p(0x34,0x43); /* binary, mode 2, LSB/MSB, ch 0 */
498 outb_p(LATCH & 0xff , 0x40); /* LSB */
500 outb(LATCH >> 8 , 0x40); /* MSB */
503 static int timer_resume(struct sys_device *dev)
505 setup_timer_hardware();
509 void i8254_timer_resume(void)
511 setup_timer_hardware();
514 static struct sysdev_class timer_sysclass = {
515 set_kset_name("timer_pit"),
516 .resume = timer_resume,
519 static struct sys_device device_timer = {
521 .cls = &timer_sysclass,
524 static int __init init_timer_sysfs(void)
526 int error = sysdev_class_register(&timer_sysclass);
528 error = sysdev_register(&device_timer);
532 device_initcall(init_timer_sysfs);
534 void __init init_IRQ(void)
540 * Cover the whole vector space, no vector can escape
541 * us. (some of these will be overridden and become
542 * 'special' SMP interrupts)
544 for (i = 0; i < (NR_VECTORS - FIRST_EXTERNAL_VECTOR); i++) {
545 int vector = FIRST_EXTERNAL_VECTOR + i;
548 if (vector != IA32_SYSCALL_VECTOR)
549 set_intr_gate(vector, interrupt[i]);
554 * IRQ0 must be given a fixed assignment and initialized,
555 * because it's used before the IO-APIC is set up.
557 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
560 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
561 * IPI, driven by wakeup.
563 set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
565 /* IPIs for invalidation */
566 set_intr_gate(INVALIDATE_TLB_VECTOR_START+0, invalidate_interrupt0);
567 set_intr_gate(INVALIDATE_TLB_VECTOR_START+1, invalidate_interrupt1);
568 set_intr_gate(INVALIDATE_TLB_VECTOR_START+2, invalidate_interrupt2);
569 set_intr_gate(INVALIDATE_TLB_VECTOR_START+3, invalidate_interrupt3);
570 set_intr_gate(INVALIDATE_TLB_VECTOR_START+4, invalidate_interrupt4);
571 set_intr_gate(INVALIDATE_TLB_VECTOR_START+5, invalidate_interrupt5);
572 set_intr_gate(INVALIDATE_TLB_VECTOR_START+6, invalidate_interrupt6);
573 set_intr_gate(INVALIDATE_TLB_VECTOR_START+7, invalidate_interrupt7);
575 /* IPI for generic function call */
576 set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
578 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
579 set_intr_gate(THRESHOLD_APIC_VECTOR, threshold_interrupt);
581 /* self generated IPI for local APIC timer */
582 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
584 /* IPI vectors for APIC spurious and error interrupts */
585 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
586 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
589 * Set the clock to HZ Hz, we already have a valid
592 setup_timer_hardware();