1 #include <linux/linkage.h>
2 #include <linux/config.h>
3 #include <linux/errno.h>
4 #include <linux/signal.h>
5 #include <linux/sched.h>
6 #include <linux/ioport.h>
7 #include <linux/interrupt.h>
8 #include <linux/timex.h>
9 #include <linux/slab.h>
10 #include <linux/random.h>
11 #include <linux/smp_lock.h>
12 #include <linux/init.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/sysdev.h>
15 #include <linux/bitops.h>
18 #include <asm/atomic.h>
19 #include <asm/system.h>
22 #include <asm/hw_irq.h>
23 #include <asm/pgtable.h>
24 #include <asm/delay.h>
28 #include <linux/irq.h>
31 * Common place to define all x86 IRQ vectors
33 * This builds up the IRQ handler stubs using some ugly macros in irq.h
35 * These macros create the low-level assembly IRQ routines that save
36 * register context and call do_IRQ(). do_IRQ() then does all the
37 * operations that are needed to keep the AT (or SMP IOAPIC)
38 * interrupt-controller happy.
44 #define BUILD_16_IRQS(x) \
45 BI(x,0) BI(x,1) BI(x,2) BI(x,3) \
46 BI(x,4) BI(x,5) BI(x,6) BI(x,7) \
47 BI(x,8) BI(x,9) BI(x,a) BI(x,b) \
48 BI(x,c) BI(x,d) BI(x,e) BI(x,f)
50 #define BUILD_14_IRQS(x) \
51 BI(x,0) BI(x,1) BI(x,2) BI(x,3) \
52 BI(x,4) BI(x,5) BI(x,6) BI(x,7) \
53 BI(x,8) BI(x,9) BI(x,a) BI(x,b) \
57 * ISA PIC or low IO-APIC triggered (INTA-cycle or APIC) interrupts:
58 * (these are usually mapped to vectors 0x20-0x2f)
62 #ifdef CONFIG_X86_LOCAL_APIC
64 * The IO-APIC gives us many more interrupt sources. Most of these
65 * are unused but an SMP system is supposed to have enough memory ...
66 * sometimes (mostly wrt. hw bugs) we get corrupted vectors all
67 * across the spectrum, so we really want to be prepared to get all
68 * of these. Plus, more powerful systems might have more than 64
71 * (these are usually mapped into the 0x30-0xff vector range)
73 BUILD_16_IRQS(0x1) BUILD_16_IRQS(0x2) BUILD_16_IRQS(0x3)
74 BUILD_16_IRQS(0x4) BUILD_16_IRQS(0x5) BUILD_16_IRQS(0x6) BUILD_16_IRQS(0x7)
75 BUILD_16_IRQS(0x8) BUILD_16_IRQS(0x9) BUILD_16_IRQS(0xa) BUILD_16_IRQS(0xb)
76 BUILD_16_IRQS(0xc) BUILD_16_IRQS(0xd)
92 #define IRQLIST_16(x) \
93 IRQ(x,0), IRQ(x,1), IRQ(x,2), IRQ(x,3), \
94 IRQ(x,4), IRQ(x,5), IRQ(x,6), IRQ(x,7), \
95 IRQ(x,8), IRQ(x,9), IRQ(x,a), IRQ(x,b), \
96 IRQ(x,c), IRQ(x,d), IRQ(x,e), IRQ(x,f)
98 #define IRQLIST_14(x) \
99 IRQ(x,0), IRQ(x,1), IRQ(x,2), IRQ(x,3), \
100 IRQ(x,4), IRQ(x,5), IRQ(x,6), IRQ(x,7), \
101 IRQ(x,8), IRQ(x,9), IRQ(x,a), IRQ(x,b), \
104 void (*interrupt
[NR_IRQS
])(void) = {
107 #ifdef CONFIG_X86_IO_APIC
108 IRQLIST_16(0x1), IRQLIST_16(0x2), IRQLIST_16(0x3),
109 IRQLIST_16(0x4), IRQLIST_16(0x5), IRQLIST_16(0x6), IRQLIST_16(0x7),
110 IRQLIST_16(0x8), IRQLIST_16(0x9), IRQLIST_16(0xa), IRQLIST_16(0xb),
111 IRQLIST_16(0xc), IRQLIST_16(0xd)
113 #ifdef CONFIG_PCI_MSI
125 * This is the 'legacy' 8259A Programmable Interrupt Controller,
126 * present in the majority of PC/AT boxes.
127 * plus some generic x86 specific things if generic specifics makes
129 * this file should become arch/i386/kernel/irq.c when the old irq.c
130 * moves to arch independent land
133 DEFINE_SPINLOCK(i8259A_lock
);
135 static void end_8259A_irq (unsigned int irq
)
139 printk("return %p stack %p ti %p\n", __builtin_return_address(0), &var
, current
->thread_info
);
144 if (!(irq_desc
[irq
].status
& (IRQ_DISABLED
|IRQ_INPROGRESS
)) &&
145 irq_desc
[irq
].action
)
146 enable_8259A_irq(irq
);
149 #define shutdown_8259A_irq disable_8259A_irq
151 static void mask_and_ack_8259A(unsigned int);
153 static unsigned int startup_8259A_irq(unsigned int irq
)
155 enable_8259A_irq(irq
);
156 return 0; /* never anything pending */
159 static struct hw_interrupt_type i8259A_irq_type
= {
171 * 8259A PIC functions to handle ISA devices:
175 * This contains the irq mask for both 8259A irq controllers,
177 static unsigned int cached_irq_mask
= 0xffff;
179 #define __byte(x,y) (((unsigned char *)&(y))[x])
180 #define cached_21 (__byte(0,cached_irq_mask))
181 #define cached_A1 (__byte(1,cached_irq_mask))
184 * Not all IRQs can be routed through the IO-APIC, eg. on certain (older)
185 * boards the timer interrupt is not really connected to any IO-APIC pin,
186 * it's fed to the master 8259A's IR0 line only.
188 * Any '1' bit in this mask means the IRQ is routed through the IO-APIC.
189 * this 'mixed mode' IRQ handling costs nothing because it's only used
192 unsigned long io_apic_irqs
;
194 void disable_8259A_irq(unsigned int irq
)
196 unsigned int mask
= 1 << irq
;
199 spin_lock_irqsave(&i8259A_lock
, flags
);
200 cached_irq_mask
|= mask
;
202 outb(cached_A1
,0xA1);
204 outb(cached_21
,0x21);
205 spin_unlock_irqrestore(&i8259A_lock
, flags
);
208 void enable_8259A_irq(unsigned int irq
)
210 unsigned int mask
= ~(1 << irq
);
213 spin_lock_irqsave(&i8259A_lock
, flags
);
214 cached_irq_mask
&= mask
;
216 outb(cached_A1
,0xA1);
218 outb(cached_21
,0x21);
219 spin_unlock_irqrestore(&i8259A_lock
, flags
);
222 int i8259A_irq_pending(unsigned int irq
)
224 unsigned int mask
= 1<<irq
;
228 spin_lock_irqsave(&i8259A_lock
, flags
);
230 ret
= inb(0x20) & mask
;
232 ret
= inb(0xA0) & (mask
>> 8);
233 spin_unlock_irqrestore(&i8259A_lock
, flags
);
238 void make_8259A_irq(unsigned int irq
)
240 disable_irq_nosync(irq
);
241 io_apic_irqs
&= ~(1<<irq
);
242 irq_desc
[irq
].handler
= &i8259A_irq_type
;
247 * This function assumes to be called rarely. Switching between
248 * 8259A registers is slow.
249 * This has to be protected by the irq controller spinlock
250 * before being called.
252 static inline int i8259A_irq_real(unsigned int irq
)
255 int irqmask
= 1<<irq
;
258 outb(0x0B,0x20); /* ISR register */
259 value
= inb(0x20) & irqmask
;
260 outb(0x0A,0x20); /* back to the IRR register */
263 outb(0x0B,0xA0); /* ISR register */
264 value
= inb(0xA0) & (irqmask
>> 8);
265 outb(0x0A,0xA0); /* back to the IRR register */
270 * Careful! The 8259A is a fragile beast, it pretty
271 * much _has_ to be done exactly like this (mask it
272 * first, _then_ send the EOI, and the order of EOI
273 * to the two 8259s is important!
275 static void mask_and_ack_8259A(unsigned int irq
)
277 unsigned int irqmask
= 1 << irq
;
280 spin_lock_irqsave(&i8259A_lock
, flags
);
282 * Lightweight spurious IRQ detection. We do not want
283 * to overdo spurious IRQ handling - it's usually a sign
284 * of hardware problems, so we only do the checks we can
285 * do without slowing down good hardware unnecesserily.
287 * Note that IRQ7 and IRQ15 (the two spurious IRQs
288 * usually resulting from the 8259A-1|2 PICs) occur
289 * even if the IRQ is masked in the 8259A. Thus we
290 * can check spurious 8259A IRQs without doing the
291 * quite slow i8259A_irq_real() call for every IRQ.
292 * This does not cover 100% of spurious interrupts,
293 * but should be enough to warn the user that there
294 * is something bad going on ...
296 if (cached_irq_mask
& irqmask
)
297 goto spurious_8259A_irq
;
298 cached_irq_mask
|= irqmask
;
302 inb(0xA1); /* DUMMY - (do we need this?) */
303 outb(cached_A1
,0xA1);
304 outb(0x60+(irq
&7),0xA0);/* 'Specific EOI' to slave */
305 outb(0x62,0x20); /* 'Specific EOI' to master-IRQ2 */
307 inb(0x21); /* DUMMY - (do we need this?) */
308 outb(cached_21
,0x21);
309 outb(0x60+irq
,0x20); /* 'Specific EOI' to master */
311 spin_unlock_irqrestore(&i8259A_lock
, flags
);
316 * this is the slow path - should happen rarely.
318 if (i8259A_irq_real(irq
))
320 * oops, the IRQ _is_ in service according to the
321 * 8259A - not spurious, go handle it.
323 goto handle_real_irq
;
326 static int spurious_irq_mask
;
328 * At this point we can be sure the IRQ is spurious,
329 * lets ACK and report it. [once per IRQ]
331 if (!(spurious_irq_mask
& irqmask
)) {
332 printk(KERN_DEBUG
"spurious 8259A interrupt: IRQ%d.\n", irq
);
333 spurious_irq_mask
|= irqmask
;
335 atomic_inc(&irq_err_count
);
337 * Theoretically we do not have to handle this IRQ,
338 * but in Linux this does not cause problems and is
341 goto handle_real_irq
;
345 void init_8259A(int auto_eoi
)
349 spin_lock_irqsave(&i8259A_lock
, flags
);
351 outb(0xff, 0x21); /* mask all of 8259A-1 */
352 outb(0xff, 0xA1); /* mask all of 8259A-2 */
355 * outb_p - this has to work on a wide range of PC hardware.
357 outb_p(0x11, 0x20); /* ICW1: select 8259A-1 init */
358 outb_p(0x20 + 0, 0x21); /* ICW2: 8259A-1 IR0-7 mapped to 0x20-0x27 */
359 outb_p(0x04, 0x21); /* 8259A-1 (the master) has a slave on IR2 */
361 outb_p(0x03, 0x21); /* master does Auto EOI */
363 outb_p(0x01, 0x21); /* master expects normal EOI */
365 outb_p(0x11, 0xA0); /* ICW1: select 8259A-2 init */
366 outb_p(0x20 + 8, 0xA1); /* ICW2: 8259A-2 IR0-7 mapped to 0x28-0x2f */
367 outb_p(0x02, 0xA1); /* 8259A-2 is a slave on master's IR2 */
368 outb_p(0x01, 0xA1); /* (slave's support for AEOI in flat mode
369 is to be investigated) */
373 * in AEOI mode we just have to mask the interrupt
376 i8259A_irq_type
.ack
= disable_8259A_irq
;
378 i8259A_irq_type
.ack
= mask_and_ack_8259A
;
380 udelay(100); /* wait for 8259A to initialize */
382 outb(cached_21
, 0x21); /* restore master IRQ mask */
383 outb(cached_A1
, 0xA1); /* restore slave IRQ mask */
385 spin_unlock_irqrestore(&i8259A_lock
, flags
);
388 static char irq_trigger
[2];
390 * ELCR registers (0x4d0, 0x4d1) control edge/level of IRQ
392 static void restore_ELCR(char *trigger
)
394 outb(trigger
[0], 0x4d0);
395 outb(trigger
[1], 0x4d1);
398 static void save_ELCR(char *trigger
)
400 /* IRQ 0,1,2,8,13 are marked as reserved */
401 trigger
[0] = inb(0x4d0) & 0xF8;
402 trigger
[1] = inb(0x4d1) & 0xDE;
405 static int i8259A_resume(struct sys_device
*dev
)
408 restore_ELCR(irq_trigger
);
412 static int i8259A_suspend(struct sys_device
*dev
, pm_message_t state
)
414 save_ELCR(irq_trigger
);
418 static struct sysdev_class i8259_sysdev_class
= {
419 set_kset_name("i8259"),
420 .suspend
= i8259A_suspend
,
421 .resume
= i8259A_resume
,
424 static struct sys_device device_i8259A
= {
426 .cls
= &i8259_sysdev_class
,
429 static int __init
i8259A_init_sysfs(void)
431 int error
= sysdev_class_register(&i8259_sysdev_class
);
433 error
= sysdev_register(&device_i8259A
);
437 device_initcall(i8259A_init_sysfs
);
440 * IRQ2 is cascade interrupt to second interrupt controller
443 static struct irqaction irq2
= { no_action
, 0, CPU_MASK_NONE
, "cascade", NULL
, NULL
};
445 void __init
init_ISA_irqs (void)
449 #ifdef CONFIG_X86_LOCAL_APIC
454 for (i
= 0; i
< NR_IRQS
; i
++) {
455 irq_desc
[i
].status
= IRQ_DISABLED
;
456 irq_desc
[i
].action
= NULL
;
457 irq_desc
[i
].depth
= 1;
461 * 16 old-style INTA-cycle interrupts:
463 irq_desc
[i
].handler
= &i8259A_irq_type
;
466 * 'high' PCI IRQs filled in on demand
468 irq_desc
[i
].handler
= &no_irq_type
;
473 void apic_timer_interrupt(void);
474 void spurious_interrupt(void);
475 void error_interrupt(void);
476 void reschedule_interrupt(void);
477 void call_function_interrupt(void);
478 void invalidate_interrupt(void);
479 void thermal_interrupt(void);
480 void i8254_timer_resume(void);
482 static void setup_timer(void)
484 outb_p(0x34,0x43); /* binary, mode 2, LSB/MSB, ch 0 */
486 outb_p(LATCH
& 0xff , 0x40); /* LSB */
488 outb(LATCH
>> 8 , 0x40); /* MSB */
491 static int timer_resume(struct sys_device
*dev
)
497 void i8254_timer_resume(void)
502 static struct sysdev_class timer_sysclass
= {
503 set_kset_name("timer"),
504 .resume
= timer_resume
,
507 static struct sys_device device_timer
= {
509 .cls
= &timer_sysclass
,
512 static int __init
init_timer_sysfs(void)
514 int error
= sysdev_class_register(&timer_sysclass
);
516 error
= sysdev_register(&device_timer
);
520 device_initcall(init_timer_sysfs
);
522 void __init
init_IRQ(void)
528 * Cover the whole vector space, no vector can escape
529 * us. (some of these will be overridden and become
530 * 'special' SMP interrupts)
532 for (i
= 0; i
< (NR_VECTORS
- FIRST_EXTERNAL_VECTOR
); i
++) {
533 int vector
= FIRST_EXTERNAL_VECTOR
+ i
;
536 if (vector
!= IA32_SYSCALL_VECTOR
&& vector
!= KDB_VECTOR
) {
537 set_intr_gate(vector
, interrupt
[i
]);
543 * IRQ0 must be given a fixed assignment and initialized,
544 * because it's used before the IO-APIC is set up.
546 set_intr_gate(FIRST_DEVICE_VECTOR
, interrupt
[0]);
549 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
550 * IPI, driven by wakeup.
552 set_intr_gate(RESCHEDULE_VECTOR
, reschedule_interrupt
);
554 /* IPI for invalidation */
555 set_intr_gate(INVALIDATE_TLB_VECTOR
, invalidate_interrupt
);
557 /* IPI for generic function call */
558 set_intr_gate(CALL_FUNCTION_VECTOR
, call_function_interrupt
);
560 set_intr_gate(THERMAL_APIC_VECTOR
, thermal_interrupt
);
562 #ifdef CONFIG_X86_LOCAL_APIC
563 /* self generated IPI for local APIC timer */
564 set_intr_gate(LOCAL_TIMER_VECTOR
, apic_timer_interrupt
);
566 /* IPI vectors for APIC spurious and error interrupts */
567 set_intr_gate(SPURIOUS_APIC_VECTOR
, spurious_interrupt
);
568 set_intr_gate(ERROR_APIC_VECTOR
, error_interrupt
);
572 * Set the clock to HZ Hz, we already have a valid