blob: 03abfdb1a6e4c5cb8e4c7e407f0a19e3763de2df [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/errno.h>
2#include <linux/signal.h>
3#include <linux/sched.h>
4#include <linux/ioport.h>
5#include <linux/interrupt.h>
6#include <linux/slab.h>
7#include <linux/random.h>
8#include <linux/smp_lock.h>
9#include <linux/init.h>
10#include <linux/kernel_stat.h>
11#include <linux/sysdev.h>
12#include <linux/bitops.h>
13
14#include <asm/8253pit.h>
15#include <asm/atomic.h>
16#include <asm/system.h>
17#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/timer.h>
19#include <asm/pgtable.h>
20#include <asm/delay.h>
21#include <asm/desc.h>
22#include <asm/apic.h>
23#include <asm/arch_hooks.h>
24#include <asm/i8259.h>
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <io_ports.h>
27
28/*
29 * This is the 'legacy' 8259A Programmable Interrupt Controller,
30 * present in the majority of PC/AT boxes.
31 * plus some generic x86 specific things if generic specifics makes
32 * any sense at all.
33 * this file should become arch/i386/kernel/irq.c when the old irq.c
34 * moves to arch independent land
35 */
36
Matthew Garrett35d534a2006-09-26 10:52:41 +020037static int i8259A_auto_eoi;
Ingo Molnarf5b9ed72006-10-04 02:16:26 -070038DEFINE_SPINLOCK(i8259A_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static void mask_and_ack_8259A(unsigned int);
40
Ingo Molnarf5b9ed72006-10-04 02:16:26 -070041static struct irq_chip i8259A_chip = {
42 .name = "XT-PIC",
43 .mask = disable_8259A_irq,
Ingo Molnar76d21602007-02-16 01:28:24 -080044 .disable = disable_8259A_irq,
Ingo Molnarf5b9ed72006-10-04 02:16:26 -070045 .unmask = enable_8259A_irq,
46 .mask_ack = mask_and_ack_8259A,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
49/*
50 * 8259A PIC functions to handle ISA devices:
51 */
52
53/*
54 * This contains the irq mask for both 8259A irq controllers,
55 */
56unsigned int cached_irq_mask = 0xffff;
57
58/*
59 * Not all IRQs can be routed through the IO-APIC, eg. on certain (older)
60 * boards the timer interrupt is not really connected to any IO-APIC pin,
61 * it's fed to the master 8259A's IR0 line only.
62 *
63 * Any '1' bit in this mask means the IRQ is routed through the IO-APIC.
64 * this 'mixed mode' IRQ handling costs nothing because it's only used
65 * at IRQ setup time.
66 */
67unsigned long io_apic_irqs;
68
69void disable_8259A_irq(unsigned int irq)
70{
71 unsigned int mask = 1 << irq;
72 unsigned long flags;
73
74 spin_lock_irqsave(&i8259A_lock, flags);
75 cached_irq_mask |= mask;
76 if (irq & 8)
77 outb(cached_slave_mask, PIC_SLAVE_IMR);
78 else
79 outb(cached_master_mask, PIC_MASTER_IMR);
80 spin_unlock_irqrestore(&i8259A_lock, flags);
81}
82
83void enable_8259A_irq(unsigned int irq)
84{
85 unsigned int mask = ~(1 << irq);
86 unsigned long flags;
87
88 spin_lock_irqsave(&i8259A_lock, flags);
89 cached_irq_mask &= mask;
90 if (irq & 8)
91 outb(cached_slave_mask, PIC_SLAVE_IMR);
92 else
93 outb(cached_master_mask, PIC_MASTER_IMR);
94 spin_unlock_irqrestore(&i8259A_lock, flags);
95}
96
97int i8259A_irq_pending(unsigned int irq)
98{
99 unsigned int mask = 1<<irq;
100 unsigned long flags;
101 int ret;
102
103 spin_lock_irqsave(&i8259A_lock, flags);
104 if (irq < 8)
105 ret = inb(PIC_MASTER_CMD) & mask;
106 else
107 ret = inb(PIC_SLAVE_CMD) & (mask >> 8);
108 spin_unlock_irqrestore(&i8259A_lock, flags);
109
110 return ret;
111}
112
113void make_8259A_irq(unsigned int irq)
114{
115 disable_irq_nosync(irq);
116 io_apic_irqs &= ~(1<<irq);
Ingo Molnara460e742006-10-17 00:10:03 -0700117 set_irq_chip_and_handler_name(irq, &i8259A_chip, handle_level_irq,
118 "XT");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 enable_irq(irq);
120}
121
122/*
123 * This function assumes to be called rarely. Switching between
124 * 8259A registers is slow.
125 * This has to be protected by the irq controller spinlock
126 * before being called.
127 */
128static inline int i8259A_irq_real(unsigned int irq)
129{
130 int value;
131 int irqmask = 1<<irq;
132
133 if (irq < 8) {
134 outb(0x0B,PIC_MASTER_CMD); /* ISR register */
135 value = inb(PIC_MASTER_CMD) & irqmask;
136 outb(0x0A,PIC_MASTER_CMD); /* back to the IRR register */
137 return value;
138 }
139 outb(0x0B,PIC_SLAVE_CMD); /* ISR register */
140 value = inb(PIC_SLAVE_CMD) & (irqmask >> 8);
141 outb(0x0A,PIC_SLAVE_CMD); /* back to the IRR register */
142 return value;
143}
144
145/*
146 * Careful! The 8259A is a fragile beast, it pretty
147 * much _has_ to be done exactly like this (mask it
148 * first, _then_ send the EOI, and the order of EOI
149 * to the two 8259s is important!
150 */
151static void mask_and_ack_8259A(unsigned int irq)
152{
153 unsigned int irqmask = 1 << irq;
154 unsigned long flags;
155
156 spin_lock_irqsave(&i8259A_lock, flags);
157 /*
158 * Lightweight spurious IRQ detection. We do not want
159 * to overdo spurious IRQ handling - it's usually a sign
160 * of hardware problems, so we only do the checks we can
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200161 * do without slowing down good hardware unnecessarily.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 *
163 * Note that IRQ7 and IRQ15 (the two spurious IRQs
164 * usually resulting from the 8259A-1|2 PICs) occur
165 * even if the IRQ is masked in the 8259A. Thus we
166 * can check spurious 8259A IRQs without doing the
167 * quite slow i8259A_irq_real() call for every IRQ.
168 * This does not cover 100% of spurious interrupts,
169 * but should be enough to warn the user that there
170 * is something bad going on ...
171 */
172 if (cached_irq_mask & irqmask)
173 goto spurious_8259A_irq;
174 cached_irq_mask |= irqmask;
175
176handle_real_irq:
177 if (irq & 8) {
178 inb(PIC_SLAVE_IMR); /* DUMMY - (do we need this?) */
179 outb(cached_slave_mask, PIC_SLAVE_IMR);
180 outb(0x60+(irq&7),PIC_SLAVE_CMD);/* 'Specific EOI' to slave */
181 outb(0x60+PIC_CASCADE_IR,PIC_MASTER_CMD); /* 'Specific EOI' to master-IRQ2 */
182 } else {
183 inb(PIC_MASTER_IMR); /* DUMMY - (do we need this?) */
184 outb(cached_master_mask, PIC_MASTER_IMR);
185 outb(0x60+irq,PIC_MASTER_CMD); /* 'Specific EOI to master */
186 }
187 spin_unlock_irqrestore(&i8259A_lock, flags);
188 return;
189
190spurious_8259A_irq:
191 /*
192 * this is the slow path - should happen rarely.
193 */
194 if (i8259A_irq_real(irq))
195 /*
196 * oops, the IRQ _is_ in service according to the
197 * 8259A - not spurious, go handle it.
198 */
199 goto handle_real_irq;
200
201 {
202 static int spurious_irq_mask;
203 /*
204 * At this point we can be sure the IRQ is spurious,
205 * lets ACK and report it. [once per IRQ]
206 */
207 if (!(spurious_irq_mask & irqmask)) {
208 printk(KERN_DEBUG "spurious 8259A interrupt: IRQ%d.\n", irq);
209 spurious_irq_mask |= irqmask;
210 }
211 atomic_inc(&irq_err_count);
212 /*
213 * Theoretically we do not have to handle this IRQ,
214 * but in Linux this does not cause problems and is
215 * simpler for us.
216 */
217 goto handle_real_irq;
218 }
219}
220
221static char irq_trigger[2];
222/**
223 * ELCR registers (0x4d0, 0x4d1) control edge/level of IRQ
224 */
225static void restore_ELCR(char *trigger)
226{
227 outb(trigger[0], 0x4d0);
228 outb(trigger[1], 0x4d1);
229}
230
231static void save_ELCR(char *trigger)
232{
233 /* IRQ 0,1,2,8,13 are marked as reserved */
234 trigger[0] = inb(0x4d0) & 0xF8;
235 trigger[1] = inb(0x4d1) & 0xDE;
236}
237
238static int i8259A_resume(struct sys_device *dev)
239{
Matthew Garrett35d534a2006-09-26 10:52:41 +0200240 init_8259A(i8259A_auto_eoi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 restore_ELCR(irq_trigger);
242 return 0;
243}
244
Pavel Machek438510f2005-04-16 15:25:24 -0700245static int i8259A_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
247 save_ELCR(irq_trigger);
248 return 0;
249}
250
Eric W. Biedermancee5dab2005-06-25 14:57:43 -0700251static int i8259A_shutdown(struct sys_device *dev)
252{
253 /* Put the i8259A into a quiescent state that
254 * the kernel initialization code can get it
255 * out of.
256 */
Andreas Mohr110cb1d2006-06-23 02:04:28 -0700257 outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
258 outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-1 */
Eric W. Biedermancee5dab2005-06-25 14:57:43 -0700259 return 0;
260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262static struct sysdev_class i8259_sysdev_class = {
263 set_kset_name("i8259"),
264 .suspend = i8259A_suspend,
265 .resume = i8259A_resume,
Eric W. Biedermancee5dab2005-06-25 14:57:43 -0700266 .shutdown = i8259A_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267};
268
269static struct sys_device device_i8259A = {
270 .id = 0,
271 .cls = &i8259_sysdev_class,
272};
273
274static int __init i8259A_init_sysfs(void)
275{
276 int error = sysdev_class_register(&i8259_sysdev_class);
277 if (!error)
278 error = sysdev_register(&device_i8259A);
279 return error;
280}
281
282device_initcall(i8259A_init_sysfs);
283
284void init_8259A(int auto_eoi)
285{
286 unsigned long flags;
287
Matthew Garrett35d534a2006-09-26 10:52:41 +0200288 i8259A_auto_eoi = auto_eoi;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 spin_lock_irqsave(&i8259A_lock, flags);
291
292 outb(0xff, PIC_MASTER_IMR); /* mask all of 8259A-1 */
293 outb(0xff, PIC_SLAVE_IMR); /* mask all of 8259A-2 */
294
295 /*
296 * outb_p - this has to work on a wide range of PC hardware.
297 */
298 outb_p(0x11, PIC_MASTER_CMD); /* ICW1: select 8259A-1 init */
299 outb_p(0x20 + 0, PIC_MASTER_IMR); /* ICW2: 8259A-1 IR0-7 mapped to 0x20-0x27 */
300 outb_p(1U << PIC_CASCADE_IR, PIC_MASTER_IMR); /* 8259A-1 (the master) has a slave on IR2 */
301 if (auto_eoi) /* master does Auto EOI */
302 outb_p(MASTER_ICW4_DEFAULT | PIC_ICW4_AEOI, PIC_MASTER_IMR);
303 else /* master expects normal EOI */
304 outb_p(MASTER_ICW4_DEFAULT, PIC_MASTER_IMR);
305
306 outb_p(0x11, PIC_SLAVE_CMD); /* ICW1: select 8259A-2 init */
307 outb_p(0x20 + 8, PIC_SLAVE_IMR); /* ICW2: 8259A-2 IR0-7 mapped to 0x28-0x2f */
308 outb_p(PIC_CASCADE_IR, PIC_SLAVE_IMR); /* 8259A-2 is a slave on master's IR2 */
309 outb_p(SLAVE_ICW4_DEFAULT, PIC_SLAVE_IMR); /* (slave's support for AEOI in flat mode is to be investigated) */
310 if (auto_eoi)
311 /*
Ingo Molnarf5b9ed72006-10-04 02:16:26 -0700312 * In AEOI mode we just have to mask the interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * when acking.
314 */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -0700315 i8259A_chip.mask_ack = disable_8259A_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 else
Ingo Molnarf5b9ed72006-10-04 02:16:26 -0700317 i8259A_chip.mask_ack = mask_and_ack_8259A;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 udelay(100); /* wait for 8259A to initialize */
320
321 outb(cached_master_mask, PIC_MASTER_IMR); /* restore master IRQ mask */
322 outb(cached_slave_mask, PIC_SLAVE_IMR); /* restore slave IRQ mask */
323
324 spin_unlock_irqrestore(&i8259A_lock, flags);
325}
326
327/*
328 * Note that on a 486, we don't want to do a SIGFPE on an irq13
329 * as the irq is unreliable, and exception 16 works correctly
330 * (ie as explained in the intel literature). On a 386, you
331 * can't use exception 16 due to bad IBM design, so we have to
332 * rely on the less exact irq13.
333 *
334 * Careful.. Not only is IRQ13 unreliable, but it is also
335 * leads to races. IBM designers who came up with it should
336 * be shot.
337 */
338
339
David Howells7d12e782006-10-05 14:55:46 +0100340static irqreturn_t math_error_irq(int cpl, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 extern void math_error(void __user *);
343 outb(0,0xF0);
344 if (ignore_fpu_irq || !boot_cpu_data.hard_math)
345 return IRQ_NONE;
David Howells7d12e782006-10-05 14:55:46 +0100346 math_error((void __user *)get_irq_regs()->eip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return IRQ_HANDLED;
348}
349
350/*
351 * New motherboards sometimes make IRQ 13 be a PCI interrupt,
352 * so allow interrupt sharing.
353 */
354static struct irqaction fpu_irq = { math_error_irq, 0, CPU_MASK_NONE, "fpu", NULL, NULL };
355
356void __init init_ISA_irqs (void)
357{
358 int i;
359
360#ifdef CONFIG_X86_LOCAL_APIC
361 init_bsp_APIC();
362#endif
363 init_8259A(0);
364
365 for (i = 0; i < NR_IRQS; i++) {
366 irq_desc[i].status = IRQ_DISABLED;
367 irq_desc[i].action = NULL;
368 irq_desc[i].depth = 1;
369
370 if (i < 16) {
371 /*
372 * 16 old-style INTA-cycle interrupts:
373 */
Ingo Molnara460e742006-10-17 00:10:03 -0700374 set_irq_chip_and_handler_name(i, &i8259A_chip,
375 handle_level_irq, "XT");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 } else {
377 /*
378 * 'high' PCI IRQs filled in on demand
379 */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -0700380 irq_desc[i].chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382 }
383}
384
Rusty Russelld3561b72006-12-07 02:14:07 +0100385/* Overridden in paravirt.c */
386void init_IRQ(void) __attribute__((weak, alias("native_init_IRQ")));
387
388void __init native_init_IRQ(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 int i;
391
392 /* all the set up before the call gates are initialised */
393 pre_intr_init_hook();
394
395 /*
396 * Cover the whole vector space, no vector can escape
397 * us. (some of these will be overridden and become
398 * 'special' SMP interrupts)
399 */
400 for (i = 0; i < (NR_VECTORS - FIRST_EXTERNAL_VECTOR); i++) {
401 int vector = FIRST_EXTERNAL_VECTOR + i;
402 if (i >= NR_IRQS)
403 break;
404 if (vector != SYSCALL_VECTOR)
405 set_intr_gate(vector, interrupt[i]);
406 }
407
408 /* setup after call gates are initialised (usually add in
409 * the architecture specific gates)
410 */
411 intr_init_hook();
412
413 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 * External FPU? Set up irq13 if so, for
415 * original braindamaged IBM FERR coupling.
416 */
417 if (boot_cpu_data.hard_math && !cpu_has_fpu)
418 setup_irq(FPU_IRQ, &fpu_irq);
419
420 irq_ctx_init(smp_processor_id());
421}