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