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