ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / x86_64 / kernel / i8259.c
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
16 #include <asm/acpi.h>
17 #include <asm/atomic.h>
18 #include <asm/system.h>
19 #include <asm/io.h>
20 #include <asm/irq.h>
21 #include <asm/hw_irq.h>
22 #include <asm/bitops.h>
23 #include <asm/pgtable.h>
24 #include <asm/delay.h>
25 #include <asm/desc.h>
26 #include <asm/apic.h>
27
28 #include <linux/irq.h>
29
30 /*
31  * Common place to define all x86 IRQ vectors
32  *
33  * This builds up the IRQ handler stubs using some ugly macros in irq.h
34  *
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.
39  */
40
41 #define BI(x,y) \
42         BUILD_IRQ(x##y)
43
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)
49
50 /*
51  * ISA PIC or low IO-APIC triggered (INTA-cycle or APIC) interrupts:
52  * (these are usually mapped to vectors 0x20-0x2f)
53  */
54 BUILD_16_IRQS(0x0)
55
56 #ifdef CONFIG_X86_LOCAL_APIC
57 /*
58  * The IO-APIC gives us many more interrupt sources. Most of these 
59  * are unused but an SMP system is supposed to have enough memory ...
60  * sometimes (mostly wrt. hw bugs) we get corrupted vectors all
61  * across the spectrum, so we really want to be prepared to get all
62  * of these. Plus, more powerful systems might have more than 64
63  * IO-APIC registers.
64  *
65  * (these are usually mapped into the 0x30-0xff vector range)
66  */
67                    BUILD_16_IRQS(0x1) BUILD_16_IRQS(0x2) BUILD_16_IRQS(0x3)
68 BUILD_16_IRQS(0x4) BUILD_16_IRQS(0x5) BUILD_16_IRQS(0x6) BUILD_16_IRQS(0x7)
69 BUILD_16_IRQS(0x8) BUILD_16_IRQS(0x9) BUILD_16_IRQS(0xa) BUILD_16_IRQS(0xb)
70 BUILD_16_IRQS(0xc) BUILD_16_IRQS(0xd)
71 #endif
72
73 #undef BUILD_16_IRQS
74 #undef BI
75
76
77 #define IRQ(x,y) \
78         IRQ##x##y##_interrupt
79
80 #define IRQLIST_16(x) \
81         IRQ(x,0), IRQ(x,1), IRQ(x,2), IRQ(x,3), \
82         IRQ(x,4), IRQ(x,5), IRQ(x,6), IRQ(x,7), \
83         IRQ(x,8), IRQ(x,9), IRQ(x,a), IRQ(x,b), \
84         IRQ(x,c), IRQ(x,d), IRQ(x,e), IRQ(x,f)
85
86 void (*interrupt[NR_IRQS])(void) = {
87         IRQLIST_16(0x0),
88
89 #ifdef CONFIG_X86_IO_APIC
90                          IRQLIST_16(0x1), IRQLIST_16(0x2), IRQLIST_16(0x3),
91         IRQLIST_16(0x4), IRQLIST_16(0x5), IRQLIST_16(0x6), IRQLIST_16(0x7),
92         IRQLIST_16(0x8), IRQLIST_16(0x9), IRQLIST_16(0xa), IRQLIST_16(0xb),
93         IRQLIST_16(0xc), IRQLIST_16(0xd)
94 #endif
95 };
96
97 #undef IRQ
98 #undef IRQLIST_16
99
100 /*
101  * This is the 'legacy' 8259A Programmable Interrupt Controller,
102  * present in the majority of PC/AT boxes.
103  * plus some generic x86 specific things if generic specifics makes
104  * any sense at all.
105  * this file should become arch/i386/kernel/irq.c when the old irq.c
106  * moves to arch independent land
107  */
108
109 spinlock_t i8259A_lock = SPIN_LOCK_UNLOCKED;
110
111 static void end_8259A_irq (unsigned int irq)
112 {
113         if (irq > 256) { 
114                 char var;
115                 printk("return %p stack %p ti %p\n", __builtin_return_address(0), &var, current->thread_info); 
116
117                 BUG(); 
118         }
119
120         if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)) &&
121             irq_desc[irq].action)
122                 enable_8259A_irq(irq);
123 }
124
125 #define shutdown_8259A_irq      disable_8259A_irq
126
127 void mask_and_ack_8259A(unsigned int);
128
129 static unsigned int startup_8259A_irq(unsigned int irq)
130
131         enable_8259A_irq(irq);
132         return 0; /* never anything pending */
133 }
134
135 static struct hw_interrupt_type i8259A_irq_type = {
136         "XT-PIC",
137         startup_8259A_irq,
138         shutdown_8259A_irq,
139         enable_8259A_irq,
140         disable_8259A_irq,
141         mask_and_ack_8259A,
142         end_8259A_irq,
143         NULL
144 };
145
146 /*
147  * 8259A PIC functions to handle ISA devices:
148  */
149
150 /*
151  * This contains the irq mask for both 8259A irq controllers,
152  */
153 static unsigned int cached_irq_mask = 0xffff;
154
155 #define __byte(x,y)     (((unsigned char *)&(y))[x])
156 #define cached_21       (__byte(0,cached_irq_mask))
157 #define cached_A1       (__byte(1,cached_irq_mask))
158
159 /*
160  * Not all IRQs can be routed through the IO-APIC, eg. on certain (older)
161  * boards the timer interrupt is not really connected to any IO-APIC pin,
162  * it's fed to the master 8259A's IR0 line only.
163  *
164  * Any '1' bit in this mask means the IRQ is routed through the IO-APIC.
165  * this 'mixed mode' IRQ handling costs nothing because it's only used
166  * at IRQ setup time.
167  */
168 unsigned long io_apic_irqs;
169
170 void disable_8259A_irq(unsigned int irq)
171 {
172         unsigned int mask = 1 << irq;
173         unsigned long flags;
174
175         spin_lock_irqsave(&i8259A_lock, flags);
176         cached_irq_mask |= mask;
177         if (irq & 8)
178                 outb(cached_A1,0xA1);
179         else
180                 outb(cached_21,0x21);
181         spin_unlock_irqrestore(&i8259A_lock, flags);
182 }
183
184 void enable_8259A_irq(unsigned int irq)
185 {
186         unsigned int mask = ~(1 << irq);
187         unsigned long flags;
188
189         spin_lock_irqsave(&i8259A_lock, flags);
190         cached_irq_mask &= mask;
191         if (irq & 8)
192                 outb(cached_A1,0xA1);
193         else
194                 outb(cached_21,0x21);
195         spin_unlock_irqrestore(&i8259A_lock, flags);
196 }
197
198 int i8259A_irq_pending(unsigned int irq)
199 {
200         unsigned int mask = 1<<irq;
201         unsigned long flags;
202         int ret;
203
204         spin_lock_irqsave(&i8259A_lock, flags);
205         if (irq < 8)
206                 ret = inb(0x20) & mask;
207         else
208                 ret = inb(0xA0) & (mask >> 8);
209         spin_unlock_irqrestore(&i8259A_lock, flags);
210
211         return ret;
212 }
213
214 void make_8259A_irq(unsigned int irq)
215 {
216         disable_irq_nosync(irq);
217         io_apic_irqs &= ~(1<<irq);
218         irq_desc[irq].handler = &i8259A_irq_type;
219         enable_irq(irq);
220 }
221
222 /*
223  * This function assumes to be called rarely. Switching between
224  * 8259A registers is slow.
225  * This has to be protected by the irq controller spinlock
226  * before being called.
227  */
228 static inline int i8259A_irq_real(unsigned int irq)
229 {
230         int value;
231         int irqmask = 1<<irq;
232
233         if (irq < 8) {
234                 outb(0x0B,0x20);                /* ISR register */
235                 value = inb(0x20) & irqmask;
236                 outb(0x0A,0x20);                /* back to the IRR register */
237                 return value;
238         }
239         outb(0x0B,0xA0);                /* ISR register */
240         value = inb(0xA0) & (irqmask >> 8);
241         outb(0x0A,0xA0);                /* back to the IRR register */
242         return value;
243 }
244
245 /*
246  * Careful! The 8259A is a fragile beast, it pretty
247  * much _has_ to be done exactly like this (mask it
248  * first, _then_ send the EOI, and the order of EOI
249  * to the two 8259s is important!
250  */
251 void mask_and_ack_8259A(unsigned int irq)
252 {
253         unsigned int irqmask = 1 << irq;
254         unsigned long flags;
255
256         spin_lock_irqsave(&i8259A_lock, flags);
257         /*
258          * Lightweight spurious IRQ detection. We do not want
259          * to overdo spurious IRQ handling - it's usually a sign
260          * of hardware problems, so we only do the checks we can
261          * do without slowing down good hardware unnecesserily.
262          *
263          * Note that IRQ7 and IRQ15 (the two spurious IRQs
264          * usually resulting from the 8259A-1|2 PICs) occur
265          * even if the IRQ is masked in the 8259A. Thus we
266          * can check spurious 8259A IRQs without doing the
267          * quite slow i8259A_irq_real() call for every IRQ.
268          * This does not cover 100% of spurious interrupts,
269          * but should be enough to warn the user that there
270          * is something bad going on ...
271          */
272         if (cached_irq_mask & irqmask)
273                 goto spurious_8259A_irq;
274         cached_irq_mask |= irqmask;
275
276 handle_real_irq:
277         if (irq & 8) {
278                 inb(0xA1);              /* DUMMY - (do we need this?) */
279                 outb(cached_A1,0xA1);
280                 outb(0x60+(irq&7),0xA0);/* 'Specific EOI' to slave */
281                 outb(0x62,0x20);        /* 'Specific EOI' to master-IRQ2 */
282         } else {
283                 inb(0x21);              /* DUMMY - (do we need this?) */
284                 outb(cached_21,0x21);
285                 outb(0x60+irq,0x20);    /* 'Specific EOI' to master */
286         }
287         spin_unlock_irqrestore(&i8259A_lock, flags);
288         return;
289
290 spurious_8259A_irq:
291         /*
292          * this is the slow path - should happen rarely.
293          */
294         if (i8259A_irq_real(irq))
295                 /*
296                  * oops, the IRQ _is_ in service according to the
297                  * 8259A - not spurious, go handle it.
298                  */
299                 goto handle_real_irq;
300
301         {
302                 static int spurious_irq_mask;
303                 /*
304                  * At this point we can be sure the IRQ is spurious,
305                  * lets ACK and report it. [once per IRQ]
306                  */
307                 if (!(spurious_irq_mask & irqmask)) {
308                         printk("spurious 8259A interrupt: IRQ%d.\n", irq);
309                         spurious_irq_mask |= irqmask;
310                 }
311                 atomic_inc(&irq_err_count);
312                 /*
313                  * Theoretically we do not have to handle this IRQ,
314                  * but in Linux this does not cause problems and is
315                  * simpler for us.
316                  */
317                 goto handle_real_irq;
318         }
319 }
320
321 void __init init_8259A(int auto_eoi)
322 {
323         unsigned long flags;
324
325         spin_lock_irqsave(&i8259A_lock, flags);
326
327         outb(0xff, 0x21);       /* mask all of 8259A-1 */
328         outb(0xff, 0xA1);       /* mask all of 8259A-2 */
329
330         /*
331          * outb_p - this has to work on a wide range of PC hardware.
332          */
333         outb_p(0x11, 0x20);     /* ICW1: select 8259A-1 init */
334         outb_p(0x20 + 0, 0x21); /* ICW2: 8259A-1 IR0-7 mapped to 0x20-0x27 */
335         outb_p(0x04, 0x21);     /* 8259A-1 (the master) has a slave on IR2 */
336         if (auto_eoi)
337                 outb_p(0x03, 0x21);     /* master does Auto EOI */
338         else
339                 outb_p(0x01, 0x21);     /* master expects normal EOI */
340
341         outb_p(0x11, 0xA0);     /* ICW1: select 8259A-2 init */
342         outb_p(0x20 + 8, 0xA1); /* ICW2: 8259A-2 IR0-7 mapped to 0x28-0x2f */
343         outb_p(0x02, 0xA1);     /* 8259A-2 is a slave on master's IR2 */
344         outb_p(0x01, 0xA1);     /* (slave's support for AEOI in flat mode
345                                     is to be investigated) */
346
347         if (auto_eoi)
348                 /*
349                  * in AEOI mode we just have to mask the interrupt
350                  * when acking.
351                  */
352                 i8259A_irq_type.ack = disable_8259A_irq;
353         else
354                 i8259A_irq_type.ack = mask_and_ack_8259A;
355
356         udelay(100);            /* wait for 8259A to initialize */
357
358         outb(cached_21, 0x21);  /* restore master IRQ mask */
359         outb(cached_A1, 0xA1);  /* restore slave IRQ mask */
360
361         spin_unlock_irqrestore(&i8259A_lock, flags);
362 }
363
364 /*
365  * IRQ2 is cascade interrupt to second interrupt controller
366  */
367
368 static struct irqaction irq2 = { no_action, 0, 0, "cascade", NULL, NULL};
369
370 void __init init_ISA_irqs (void)
371 {
372         int i;
373
374 #ifdef CONFIG_X86_LOCAL_APIC
375         init_bsp_APIC();
376 #endif
377         init_8259A(0);
378
379         for (i = 0; i < NR_IRQS; i++) {
380                 irq_desc[i].status = IRQ_DISABLED;
381                 irq_desc[i].action = 0;
382                 irq_desc[i].depth = 1;
383
384                 if (i < 16) {
385                         /*
386                          * 16 old-style INTA-cycle interrupts:
387                          */
388                         irq_desc[i].handler = &i8259A_irq_type;
389                 } else {
390                         /*
391                          * 'high' PCI IRQs filled in on demand
392                          */
393                         irq_desc[i].handler = &no_irq_type;
394                 }
395         }
396 }
397
398 void apic_timer_interrupt(void);
399 void spurious_interrupt(void);
400 void error_interrupt(void);
401 void reschedule_interrupt(void);
402 void call_function_interrupt(void);
403 void invalidate_interrupt(void);
404
405 static void setup_timer(void)
406 {
407         outb_p(0x34,0x43);              /* binary, mode 2, LSB/MSB, ch 0 */
408         udelay(10);
409         outb_p(LATCH & 0xff , 0x40);    /* LSB */
410         udelay(10);
411         outb(LATCH >> 8 , 0x40);        /* MSB */
412 }
413
414 static int timer_resume(struct sys_device *dev)
415 {
416         setup_timer();
417         return 0;
418 }
419
420 static struct sysdev_class timer_sysclass = {
421         set_kset_name("timer"),
422         .resume         = timer_resume,
423 };
424
425 static struct sys_device device_timer = {
426         .id             = 0,
427         .cls            = &timer_sysclass,
428 };
429
430 static int __init init_timer_sysfs(void)
431 {
432         int error = sysdev_class_register(&timer_sysclass);
433         if (!error)
434                 error = sysdev_register(&device_timer);
435         return error;
436 }
437
438 device_initcall(init_timer_sysfs);
439
440 void __init init_IRQ(void)
441 {
442         int i;
443
444         init_ISA_irqs();
445         /*
446          * Cover the whole vector space, no vector can escape
447          * us. (some of these will be overridden and become
448          * 'special' SMP interrupts)
449          */
450         for (i = 0; i < (NR_VECTORS - FIRST_EXTERNAL_VECTOR); i++) {
451                 int vector = FIRST_EXTERNAL_VECTOR + i;
452                 if (i >= NR_IRQS)
453                         break;
454                 if (vector != IA32_SYSCALL_VECTOR && vector != KDB_VECTOR) { 
455                         set_intr_gate(vector, interrupt[i]);
456         }
457         }
458
459 #ifdef CONFIG_SMP
460         /*
461          * IRQ0 must be given a fixed assignment and initialized,
462          * because it's used before the IO-APIC is set up.
463          */
464         set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
465
466         /*
467          * The reschedule interrupt is a CPU-to-CPU reschedule-helper
468          * IPI, driven by wakeup.
469          */
470         set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
471
472         /* IPI for invalidation */
473         set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
474
475         /* IPI for generic function call */
476         set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
477 #endif  
478
479 #ifdef CONFIG_X86_LOCAL_APIC
480         /* self generated IPI for local APIC timer */
481         set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
482
483         /* IPI vectors for APIC spurious and error interrupts */
484         set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
485         set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
486 #endif
487
488         /*
489          * Set the clock to HZ Hz, we already have a valid
490          * vector now:
491          */
492         setup_timer();
493
494         if (!acpi_ioapic)
495                 setup_irq(2, &irq2);
496 }