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