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