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)
47 * ISA PIC or low IO-APIC triggered (INTA-cycle or APIC) interrupts:
48 * (these are usually mapped to vectors 0x20-0x2f)
52 * The IO-APIC gives us many more interrupt sources. Most of these
53 * are unused but an SMP system is supposed to have enough memory ...
54 * sometimes (mostly wrt. hw bugs) we get corrupted vectors all
55 * across the spectrum, so we really want to be prepared to get all
56 * of these. Plus, more powerful systems might have more than 64
59 * (these are usually mapped into the 0x30-0xff vector range)
61 BUILD_16_IRQS(0x2) BUILD_16_IRQS(0x3)
62 BUILD_16_IRQS(0x4) BUILD_16_IRQS(0x5) BUILD_16_IRQS(0x6) BUILD_16_IRQS(0x7)
63 BUILD_16_IRQS(0x8) BUILD_16_IRQS(0x9) BUILD_16_IRQS(0xa) BUILD_16_IRQS(0xb)
64 BUILD_16_IRQS(0xc) BUILD_16_IRQS(0xd) BUILD_16_IRQS(0xe) BUILD_16_IRQS(0xf)
73 #define IRQLIST_16(x) \
74 IRQ(x,0), IRQ(x,1), IRQ(x,2), IRQ(x,3), \
75 IRQ(x,4), IRQ(x,5), IRQ(x,6), IRQ(x,7), \
76 IRQ(x,8), IRQ(x,9), IRQ(x,a), IRQ(x,b), \
77 IRQ(x,c), IRQ(x,d), IRQ(x,e), IRQ(x,f)
79 /* for the irq vectors */
80 static void (*interrupt[NR_VECTORS - FIRST_EXTERNAL_VECTOR])(void) = {
81 IRQLIST_16(0x2), IRQLIST_16(0x3),
82 IRQLIST_16(0x4), IRQLIST_16(0x5), IRQLIST_16(0x6), IRQLIST_16(0x7),
83 IRQLIST_16(0x8), IRQLIST_16(0x9), IRQLIST_16(0xa), IRQLIST_16(0xb),
84 IRQLIST_16(0xc), IRQLIST_16(0xd), IRQLIST_16(0xe), IRQLIST_16(0xf)
91 * This is the 'legacy' 8259A Programmable Interrupt Controller,
92 * present in the majority of PC/AT boxes.
93 * plus some generic x86 specific things if generic specifics makes
95 * this file should become arch/i386/kernel/irq.c when the old irq.c
96 * moves to arch independent land
99 static int i8259A_auto_eoi;
100 DEFINE_SPINLOCK(i8259A_lock);
101 static void mask_and_ack_8259A(unsigned int);
103 static struct irq_chip i8259A_chip = {
105 .mask = disable_8259A_irq,
106 .disable = disable_8259A_irq,
107 .unmask = enable_8259A_irq,
108 .mask_ack = mask_and_ack_8259A,
112 * 8259A PIC functions to handle ISA devices:
116 * This contains the irq mask for both 8259A irq controllers,
118 static unsigned int cached_irq_mask = 0xffff;
120 #define __byte(x,y) (((unsigned char *)&(y))[x])
121 #define cached_21 (__byte(0,cached_irq_mask))
122 #define cached_A1 (__byte(1,cached_irq_mask))
125 * Not all IRQs can be routed through the IO-APIC, eg. on certain (older)
126 * boards the timer interrupt is not really connected to any IO-APIC pin,
127 * it's fed to the master 8259A's IR0 line only.
129 * Any '1' bit in this mask means the IRQ is routed through the IO-APIC.
130 * this 'mixed mode' IRQ handling costs nothing because it's only used
133 unsigned long io_apic_irqs;
135 void disable_8259A_irq(unsigned int irq)
137 unsigned int mask = 1 << irq;
140 spin_lock_irqsave(&i8259A_lock, flags);
141 cached_irq_mask |= mask;
143 outb(cached_A1,0xA1);
145 outb(cached_21,0x21);
146 spin_unlock_irqrestore(&i8259A_lock, flags);
149 void enable_8259A_irq(unsigned int irq)
151 unsigned int mask = ~(1 << irq);
154 spin_lock_irqsave(&i8259A_lock, flags);
155 cached_irq_mask &= mask;
157 outb(cached_A1,0xA1);
159 outb(cached_21,0x21);
160 spin_unlock_irqrestore(&i8259A_lock, flags);
163 int i8259A_irq_pending(unsigned int irq)
165 unsigned int mask = 1<<irq;
169 spin_lock_irqsave(&i8259A_lock, flags);
171 ret = inb(0x20) & mask;
173 ret = inb(0xA0) & (mask >> 8);
174 spin_unlock_irqrestore(&i8259A_lock, flags);
179 void make_8259A_irq(unsigned int irq)
181 disable_irq_nosync(irq);
182 io_apic_irqs &= ~(1<<irq);
183 set_irq_chip_and_handler_name(irq, &i8259A_chip, handle_level_irq,
189 * This function assumes to be called rarely. Switching between
190 * 8259A registers is slow.
191 * This has to be protected by the irq controller spinlock
192 * before being called.
194 static inline int i8259A_irq_real(unsigned int irq)
197 int irqmask = 1<<irq;
200 outb(0x0B,0x20); /* ISR register */
201 value = inb(0x20) & irqmask;
202 outb(0x0A,0x20); /* back to the IRR register */
205 outb(0x0B,0xA0); /* ISR register */
206 value = inb(0xA0) & (irqmask >> 8);
207 outb(0x0A,0xA0); /* back to the IRR register */
212 * Careful! The 8259A is a fragile beast, it pretty
213 * much _has_ to be done exactly like this (mask it
214 * first, _then_ send the EOI, and the order of EOI
215 * to the two 8259s is important!
217 static void mask_and_ack_8259A(unsigned int irq)
219 unsigned int irqmask = 1 << irq;
222 spin_lock_irqsave(&i8259A_lock, flags);
224 * Lightweight spurious IRQ detection. We do not want
225 * to overdo spurious IRQ handling - it's usually a sign
226 * of hardware problems, so we only do the checks we can
227 * do without slowing down good hardware unnecessarily.
229 * Note that IRQ7 and IRQ15 (the two spurious IRQs
230 * usually resulting from the 8259A-1|2 PICs) occur
231 * even if the IRQ is masked in the 8259A. Thus we
232 * can check spurious 8259A IRQs without doing the
233 * quite slow i8259A_irq_real() call for every IRQ.
234 * This does not cover 100% of spurious interrupts,
235 * but should be enough to warn the user that there
236 * is something bad going on ...
238 if (cached_irq_mask & irqmask)
239 goto spurious_8259A_irq;
240 cached_irq_mask |= irqmask;
244 inb(0xA1); /* DUMMY - (do we need this?) */
245 outb(cached_A1,0xA1);
246 outb(0x60+(irq&7),0xA0);/* 'Specific EOI' to slave */
247 outb(0x62,0x20); /* 'Specific EOI' to master-IRQ2 */
249 inb(0x21); /* DUMMY - (do we need this?) */
250 outb(cached_21,0x21);
251 outb(0x60+irq,0x20); /* 'Specific EOI' to master */
253 spin_unlock_irqrestore(&i8259A_lock, flags);
258 * this is the slow path - should happen rarely.
260 if (i8259A_irq_real(irq))
262 * oops, the IRQ _is_ in service according to the
263 * 8259A - not spurious, go handle it.
265 goto handle_real_irq;
268 static int spurious_irq_mask;
270 * At this point we can be sure the IRQ is spurious,
271 * lets ACK and report it. [once per IRQ]
273 if (!(spurious_irq_mask & irqmask)) {
274 printk(KERN_DEBUG "spurious 8259A interrupt: IRQ%d.\n", irq);
275 spurious_irq_mask |= irqmask;
277 atomic_inc(&irq_err_count);
279 * Theoretically we do not have to handle this IRQ,
280 * but in Linux this does not cause problems and is
283 goto handle_real_irq;
287 void init_8259A(int auto_eoi)
291 i8259A_auto_eoi = auto_eoi;
293 spin_lock_irqsave(&i8259A_lock, flags);
295 outb(0xff, 0x21); /* mask all of 8259A-1 */
296 outb(0xff, 0xA1); /* mask all of 8259A-2 */
299 * outb_p - this has to work on a wide range of PC hardware.
301 outb_p(0x11, 0x20); /* ICW1: select 8259A-1 init */
302 outb_p(0x20 + 0, 0x21); /* ICW2: 8259A-1 IR0-7 mapped to 0x20-0x27 */
303 outb_p(0x04, 0x21); /* 8259A-1 (the master) has a slave on IR2 */
305 outb_p(0x03, 0x21); /* master does Auto EOI */
307 outb_p(0x01, 0x21); /* master expects normal EOI */
309 outb_p(0x11, 0xA0); /* ICW1: select 8259A-2 init */
310 outb_p(0x20 + 8, 0xA1); /* ICW2: 8259A-2 IR0-7 mapped to 0x28-0x2f */
311 outb_p(0x02, 0xA1); /* 8259A-2 is a slave on master's IR2 */
312 outb_p(0x01, 0xA1); /* (slave's support for AEOI in flat mode
313 is to be investigated) */
317 * in AEOI mode we just have to mask the interrupt
320 i8259A_chip.mask_ack = disable_8259A_irq;
322 i8259A_chip.mask_ack = mask_and_ack_8259A;
324 udelay(100); /* wait for 8259A to initialize */
326 outb(cached_21, 0x21); /* restore master IRQ mask */
327 outb(cached_A1, 0xA1); /* restore slave IRQ mask */
329 spin_unlock_irqrestore(&i8259A_lock, flags);
332 static char irq_trigger[2];
334 * ELCR registers (0x4d0, 0x4d1) control edge/level of IRQ
336 static void restore_ELCR(char *trigger)
338 outb(trigger[0], 0x4d0);
339 outb(trigger[1], 0x4d1);
342 static void save_ELCR(char *trigger)
344 /* IRQ 0,1,2,8,13 are marked as reserved */
345 trigger[0] = inb(0x4d0) & 0xF8;
346 trigger[1] = inb(0x4d1) & 0xDE;
349 static int i8259A_resume(struct sys_device *dev)
351 init_8259A(i8259A_auto_eoi);
352 restore_ELCR(irq_trigger);
356 static int i8259A_suspend(struct sys_device *dev, pm_message_t state)
358 save_ELCR(irq_trigger);
362 static int i8259A_shutdown(struct sys_device *dev)
364 /* Put the i8259A into a quiescent state that
365 * the kernel initialization code can get it
368 outb(0xff, 0x21); /* mask all of 8259A-1 */
369 outb(0xff, 0xA1); /* mask all of 8259A-1 */
373 static struct sysdev_class i8259_sysdev_class = {
374 set_kset_name("i8259"),
375 .suspend = i8259A_suspend,
376 .resume = i8259A_resume,
377 .shutdown = i8259A_shutdown,
380 static struct sys_device device_i8259A = {
382 .cls = &i8259_sysdev_class,
385 static int __init i8259A_init_sysfs(void)
387 int error = sysdev_class_register(&i8259_sysdev_class);
389 error = sysdev_register(&device_i8259A);
393 device_initcall(i8259A_init_sysfs);
396 * IRQ2 is cascade interrupt to second interrupt controller
399 static struct irqaction irq2 = { no_action, 0, CPU_MASK_NONE, "cascade", NULL, NULL};
400 DEFINE_PER_CPU(vector_irq_t, vector_irq) = {
401 [0 ... FIRST_EXTERNAL_VECTOR - 1] = -1,
402 [FIRST_EXTERNAL_VECTOR + 0] = 0,
403 [FIRST_EXTERNAL_VECTOR + 1] = 1,
404 [FIRST_EXTERNAL_VECTOR + 2] = 2,
405 [FIRST_EXTERNAL_VECTOR + 3] = 3,
406 [FIRST_EXTERNAL_VECTOR + 4] = 4,
407 [FIRST_EXTERNAL_VECTOR + 5] = 5,
408 [FIRST_EXTERNAL_VECTOR + 6] = 6,
409 [FIRST_EXTERNAL_VECTOR + 7] = 7,
410 [FIRST_EXTERNAL_VECTOR + 8] = 8,
411 [FIRST_EXTERNAL_VECTOR + 9] = 9,
412 [FIRST_EXTERNAL_VECTOR + 10] = 10,
413 [FIRST_EXTERNAL_VECTOR + 11] = 11,
414 [FIRST_EXTERNAL_VECTOR + 12] = 12,
415 [FIRST_EXTERNAL_VECTOR + 13] = 13,
416 [FIRST_EXTERNAL_VECTOR + 14] = 14,
417 [FIRST_EXTERNAL_VECTOR + 15] = 15,
418 [FIRST_EXTERNAL_VECTOR + 16 ... NR_VECTORS - 1] = -1
421 void __init init_ISA_irqs (void)
428 for (i = 0; i < NR_IRQS; i++) {
429 irq_desc[i].status = IRQ_DISABLED;
430 irq_desc[i].action = NULL;
431 irq_desc[i].depth = 1;
435 * 16 old-style INTA-cycle interrupts:
437 set_irq_chip_and_handler_name(i, &i8259A_chip,
438 handle_level_irq, "XT");
441 * 'high' PCI IRQs filled in on demand
443 irq_desc[i].chip = &no_irq_chip;
448 void apic_timer_interrupt(void);
449 void spurious_interrupt(void);
450 void error_interrupt(void);
451 void reschedule_interrupt(void);
452 void call_function_interrupt(void);
453 void invalidate_interrupt0(void);
454 void invalidate_interrupt1(void);
455 void invalidate_interrupt2(void);
456 void invalidate_interrupt3(void);
457 void invalidate_interrupt4(void);
458 void invalidate_interrupt5(void);
459 void invalidate_interrupt6(void);
460 void invalidate_interrupt7(void);
461 void thermal_interrupt(void);
462 void threshold_interrupt(void);
463 void i8254_timer_resume(void);
465 static void setup_timer_hardware(void)
467 outb_p(0x34,0x43); /* binary, mode 2, LSB/MSB, ch 0 */
469 outb_p(LATCH & 0xff , 0x40); /* LSB */
471 outb(LATCH >> 8 , 0x40); /* MSB */
474 static int timer_resume(struct sys_device *dev)
476 setup_timer_hardware();
480 void i8254_timer_resume(void)
482 setup_timer_hardware();
485 static struct sysdev_class timer_sysclass = {
486 set_kset_name("timer_pit"),
487 .resume = timer_resume,
490 static struct sys_device device_timer = {
492 .cls = &timer_sysclass,
495 static int __init init_timer_sysfs(void)
497 int error = sysdev_class_register(&timer_sysclass);
499 error = sysdev_register(&device_timer);
503 device_initcall(init_timer_sysfs);
505 void __init init_IRQ(void)
511 * Cover the whole vector space, no vector can escape
512 * us. (some of these will be overridden and become
513 * 'special' SMP interrupts)
515 for (i = 0; i < (NR_VECTORS - FIRST_EXTERNAL_VECTOR); i++) {
516 int vector = FIRST_EXTERNAL_VECTOR + i;
517 if (vector != IA32_SYSCALL_VECTOR)
518 set_intr_gate(vector, interrupt[i]);
523 * IRQ0 must be given a fixed assignment and initialized,
524 * because it's used before the IO-APIC is set up.
526 __get_cpu_var(vector_irq)[FIRST_DEVICE_VECTOR] = 0;
529 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
530 * IPI, driven by wakeup.
532 set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
534 /* IPIs for invalidation */
535 set_intr_gate(INVALIDATE_TLB_VECTOR_START+0, invalidate_interrupt0);
536 set_intr_gate(INVALIDATE_TLB_VECTOR_START+1, invalidate_interrupt1);
537 set_intr_gate(INVALIDATE_TLB_VECTOR_START+2, invalidate_interrupt2);
538 set_intr_gate(INVALIDATE_TLB_VECTOR_START+3, invalidate_interrupt3);
539 set_intr_gate(INVALIDATE_TLB_VECTOR_START+4, invalidate_interrupt4);
540 set_intr_gate(INVALIDATE_TLB_VECTOR_START+5, invalidate_interrupt5);
541 set_intr_gate(INVALIDATE_TLB_VECTOR_START+6, invalidate_interrupt6);
542 set_intr_gate(INVALIDATE_TLB_VECTOR_START+7, invalidate_interrupt7);
544 /* IPI for generic function call */
545 set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
547 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
548 set_intr_gate(THRESHOLD_APIC_VECTOR, threshold_interrupt);
550 /* self generated IPI for local APIC timer */
551 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
553 /* IPI vectors for APIC spurious and error interrupts */
554 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
555 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
558 * Set the clock to HZ Hz, we already have a valid
561 setup_timer_hardware();