[PATCH] Remove all ifdefs for local/io apic
[sfrench/cifs-2.6.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 void end_8259A_irq (unsigned int irq)
127 {
128         if (irq > 256) { 
129                 char var;
130                 printk("return %p stack %p ti %p\n", __builtin_return_address(0), &var, task_thread_info(current));
131
132                 BUG(); 
133         }
134
135         if (!(irq_desc[irq].status & (IRQ_DISABLED|IRQ_INPROGRESS)) &&
136             irq_desc[irq].action)
137                 enable_8259A_irq(irq);
138 }
139
140 #define shutdown_8259A_irq      disable_8259A_irq
141
142 static void mask_and_ack_8259A(unsigned int);
143
144 static unsigned int startup_8259A_irq(unsigned int irq)
145
146         enable_8259A_irq(irq);
147         return 0; /* never anything pending */
148 }
149
150 static struct hw_interrupt_type i8259A_irq_type = {
151         .typename = "XT-PIC",
152         .startup = startup_8259A_irq,
153         .shutdown = shutdown_8259A_irq,
154         .enable = enable_8259A_irq,
155         .disable = disable_8259A_irq,
156         .ack = mask_and_ack_8259A,
157         .end = end_8259A_irq,
158 };
159
160 /*
161  * 8259A PIC functions to handle ISA devices:
162  */
163
164 /*
165  * This contains the irq mask for both 8259A irq controllers,
166  */
167 static unsigned int cached_irq_mask = 0xffff;
168
169 #define __byte(x,y)     (((unsigned char *)&(y))[x])
170 #define cached_21       (__byte(0,cached_irq_mask))
171 #define cached_A1       (__byte(1,cached_irq_mask))
172
173 /*
174  * Not all IRQs can be routed through the IO-APIC, eg. on certain (older)
175  * boards the timer interrupt is not really connected to any IO-APIC pin,
176  * it's fed to the master 8259A's IR0 line only.
177  *
178  * Any '1' bit in this mask means the IRQ is routed through the IO-APIC.
179  * this 'mixed mode' IRQ handling costs nothing because it's only used
180  * at IRQ setup time.
181  */
182 unsigned long io_apic_irqs;
183
184 void disable_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 void enable_8259A_irq(unsigned int irq)
199 {
200         unsigned int mask = ~(1 << irq);
201         unsigned long flags;
202
203         spin_lock_irqsave(&i8259A_lock, flags);
204         cached_irq_mask &= mask;
205         if (irq & 8)
206                 outb(cached_A1,0xA1);
207         else
208                 outb(cached_21,0x21);
209         spin_unlock_irqrestore(&i8259A_lock, flags);
210 }
211
212 int i8259A_irq_pending(unsigned int irq)
213 {
214         unsigned int mask = 1<<irq;
215         unsigned long flags;
216         int ret;
217
218         spin_lock_irqsave(&i8259A_lock, flags);
219         if (irq < 8)
220                 ret = inb(0x20) & mask;
221         else
222                 ret = inb(0xA0) & (mask >> 8);
223         spin_unlock_irqrestore(&i8259A_lock, flags);
224
225         return ret;
226 }
227
228 void make_8259A_irq(unsigned int irq)
229 {
230         disable_irq_nosync(irq);
231         io_apic_irqs &= ~(1<<irq);
232         irq_desc[irq].chip = &i8259A_irq_type;
233         enable_irq(irq);
234 }
235
236 /*
237  * This function assumes to be called rarely. Switching between
238  * 8259A registers is slow.
239  * This has to be protected by the irq controller spinlock
240  * before being called.
241  */
242 static inline int i8259A_irq_real(unsigned int irq)
243 {
244         int value;
245         int irqmask = 1<<irq;
246
247         if (irq < 8) {
248                 outb(0x0B,0x20);                /* ISR register */
249                 value = inb(0x20) & irqmask;
250                 outb(0x0A,0x20);                /* back to the IRR register */
251                 return value;
252         }
253         outb(0x0B,0xA0);                /* ISR register */
254         value = inb(0xA0) & (irqmask >> 8);
255         outb(0x0A,0xA0);                /* back to the IRR register */
256         return value;
257 }
258
259 /*
260  * Careful! The 8259A is a fragile beast, it pretty
261  * much _has_ to be done exactly like this (mask it
262  * first, _then_ send the EOI, and the order of EOI
263  * to the two 8259s is important!
264  */
265 static void mask_and_ack_8259A(unsigned int irq)
266 {
267         unsigned int irqmask = 1 << irq;
268         unsigned long flags;
269
270         spin_lock_irqsave(&i8259A_lock, flags);
271         /*
272          * Lightweight spurious IRQ detection. We do not want
273          * to overdo spurious IRQ handling - it's usually a sign
274          * of hardware problems, so we only do the checks we can
275          * do without slowing down good hardware unnecessarily.
276          *
277          * Note that IRQ7 and IRQ15 (the two spurious IRQs
278          * usually resulting from the 8259A-1|2 PICs) occur
279          * even if the IRQ is masked in the 8259A. Thus we
280          * can check spurious 8259A IRQs without doing the
281          * quite slow i8259A_irq_real() call for every IRQ.
282          * This does not cover 100% of spurious interrupts,
283          * but should be enough to warn the user that there
284          * is something bad going on ...
285          */
286         if (cached_irq_mask & irqmask)
287                 goto spurious_8259A_irq;
288         cached_irq_mask |= irqmask;
289
290 handle_real_irq:
291         if (irq & 8) {
292                 inb(0xA1);              /* DUMMY - (do we need this?) */
293                 outb(cached_A1,0xA1);
294                 outb(0x60+(irq&7),0xA0);/* 'Specific EOI' to slave */
295                 outb(0x62,0x20);        /* 'Specific EOI' to master-IRQ2 */
296         } else {
297                 inb(0x21);              /* DUMMY - (do we need this?) */
298                 outb(cached_21,0x21);
299                 outb(0x60+irq,0x20);    /* 'Specific EOI' to master */
300         }
301         spin_unlock_irqrestore(&i8259A_lock, flags);
302         return;
303
304 spurious_8259A_irq:
305         /*
306          * this is the slow path - should happen rarely.
307          */
308         if (i8259A_irq_real(irq))
309                 /*
310                  * oops, the IRQ _is_ in service according to the
311                  * 8259A - not spurious, go handle it.
312                  */
313                 goto handle_real_irq;
314
315         {
316                 static int spurious_irq_mask;
317                 /*
318                  * At this point we can be sure the IRQ is spurious,
319                  * lets ACK and report it. [once per IRQ]
320                  */
321                 if (!(spurious_irq_mask & irqmask)) {
322                         printk(KERN_DEBUG "spurious 8259A interrupt: IRQ%d.\n", irq);
323                         spurious_irq_mask |= irqmask;
324                 }
325                 atomic_inc(&irq_err_count);
326                 /*
327                  * Theoretically we do not have to handle this IRQ,
328                  * but in Linux this does not cause problems and is
329                  * simpler for us.
330                  */
331                 goto handle_real_irq;
332         }
333 }
334
335 void init_8259A(int auto_eoi)
336 {
337         unsigned long flags;
338
339         spin_lock_irqsave(&i8259A_lock, flags);
340
341         outb(0xff, 0x21);       /* mask all of 8259A-1 */
342         outb(0xff, 0xA1);       /* mask all of 8259A-2 */
343
344         /*
345          * outb_p - this has to work on a wide range of PC hardware.
346          */
347         outb_p(0x11, 0x20);     /* ICW1: select 8259A-1 init */
348         outb_p(0x20 + 0, 0x21); /* ICW2: 8259A-1 IR0-7 mapped to 0x20-0x27 */
349         outb_p(0x04, 0x21);     /* 8259A-1 (the master) has a slave on IR2 */
350         if (auto_eoi)
351                 outb_p(0x03, 0x21);     /* master does Auto EOI */
352         else
353                 outb_p(0x01, 0x21);     /* master expects normal EOI */
354
355         outb_p(0x11, 0xA0);     /* ICW1: select 8259A-2 init */
356         outb_p(0x20 + 8, 0xA1); /* ICW2: 8259A-2 IR0-7 mapped to 0x28-0x2f */
357         outb_p(0x02, 0xA1);     /* 8259A-2 is a slave on master's IR2 */
358         outb_p(0x01, 0xA1);     /* (slave's support for AEOI in flat mode
359                                     is to be investigated) */
360
361         if (auto_eoi)
362                 /*
363                  * in AEOI mode we just have to mask the interrupt
364                  * when acking.
365                  */
366                 i8259A_irq_type.ack = disable_8259A_irq;
367         else
368                 i8259A_irq_type.ack = mask_and_ack_8259A;
369
370         udelay(100);            /* wait for 8259A to initialize */
371
372         outb(cached_21, 0x21);  /* restore master IRQ mask */
373         outb(cached_A1, 0xA1);  /* restore slave IRQ mask */
374
375         spin_unlock_irqrestore(&i8259A_lock, flags);
376 }
377
378 static char irq_trigger[2];
379 /**
380  * ELCR registers (0x4d0, 0x4d1) control edge/level of IRQ
381  */
382 static void restore_ELCR(char *trigger)
383 {
384         outb(trigger[0], 0x4d0);
385         outb(trigger[1], 0x4d1);
386 }
387
388 static void save_ELCR(char *trigger)
389 {
390         /* IRQ 0,1,2,8,13 are marked as reserved */
391         trigger[0] = inb(0x4d0) & 0xF8;
392         trigger[1] = inb(0x4d1) & 0xDE;
393 }
394
395 static int i8259A_resume(struct sys_device *dev)
396 {
397         init_8259A(0);
398         restore_ELCR(irq_trigger);
399         return 0;
400 }
401
402 static int i8259A_suspend(struct sys_device *dev, pm_message_t state)
403 {
404         save_ELCR(irq_trigger);
405         return 0;
406 }
407
408 static int i8259A_shutdown(struct sys_device *dev)
409 {
410         /* Put the i8259A into a quiescent state that
411          * the kernel initialization code can get it
412          * out of.
413          */
414         outb(0xff, 0x21);       /* mask all of 8259A-1 */
415         outb(0xff, 0xA1);       /* mask all of 8259A-1 */
416         return 0;
417 }
418
419 static struct sysdev_class i8259_sysdev_class = {
420         set_kset_name("i8259"),
421         .suspend = i8259A_suspend,
422         .resume = i8259A_resume,
423         .shutdown = i8259A_shutdown,
424 };
425
426 static struct sys_device device_i8259A = {
427         .id     = 0,
428         .cls    = &i8259_sysdev_class,
429 };
430
431 static int __init i8259A_init_sysfs(void)
432 {
433         int error = sysdev_class_register(&i8259_sysdev_class);
434         if (!error)
435                 error = sysdev_register(&device_i8259A);
436         return error;
437 }
438
439 device_initcall(i8259A_init_sysfs);
440
441 /*
442  * IRQ2 is cascade interrupt to second interrupt controller
443  */
444
445 static struct irqaction irq2 = { no_action, 0, CPU_MASK_NONE, "cascade", NULL, NULL};
446
447 void __init init_ISA_irqs (void)
448 {
449         int i;
450
451         init_bsp_APIC();
452         init_8259A(0);
453
454         for (i = 0; i < NR_IRQS; i++) {
455                 irq_desc[i].status = IRQ_DISABLED;
456                 irq_desc[i].action = NULL;
457                 irq_desc[i].depth = 1;
458
459                 if (i < 16) {
460                         /*
461                          * 16 old-style INTA-cycle interrupts:
462                          */
463                         irq_desc[i].chip = &i8259A_irq_type;
464                 } else {
465                         /*
466                          * 'high' PCI IRQs filled in on demand
467                          */
468                         irq_desc[i].chip = &no_irq_type;
469                 }
470         }
471 }
472
473 void apic_timer_interrupt(void);
474 void spurious_interrupt(void);
475 void error_interrupt(void);
476 void reschedule_interrupt(void);
477 void call_function_interrupt(void);
478 void invalidate_interrupt0(void);
479 void invalidate_interrupt1(void);
480 void invalidate_interrupt2(void);
481 void invalidate_interrupt3(void);
482 void invalidate_interrupt4(void);
483 void invalidate_interrupt5(void);
484 void invalidate_interrupt6(void);
485 void invalidate_interrupt7(void);
486 void thermal_interrupt(void);
487 void threshold_interrupt(void);
488 void i8254_timer_resume(void);
489
490 static void setup_timer_hardware(void)
491 {
492         outb_p(0x34,0x43);              /* binary, mode 2, LSB/MSB, ch 0 */
493         udelay(10);
494         outb_p(LATCH & 0xff , 0x40);    /* LSB */
495         udelay(10);
496         outb(LATCH >> 8 , 0x40);        /* MSB */
497 }
498
499 static int timer_resume(struct sys_device *dev)
500 {
501         setup_timer_hardware();
502         return 0;
503 }
504
505 void i8254_timer_resume(void)
506 {
507         setup_timer_hardware();
508 }
509
510 static struct sysdev_class timer_sysclass = {
511         set_kset_name("timer_pit"),
512         .resume         = timer_resume,
513 };
514
515 static struct sys_device device_timer = {
516         .id             = 0,
517         .cls            = &timer_sysclass,
518 };
519
520 static int __init init_timer_sysfs(void)
521 {
522         int error = sysdev_class_register(&timer_sysclass);
523         if (!error)
524                 error = sysdev_register(&device_timer);
525         return error;
526 }
527
528 device_initcall(init_timer_sysfs);
529
530 void __init init_IRQ(void)
531 {
532         int i;
533
534         init_ISA_irqs();
535         /*
536          * Cover the whole vector space, no vector can escape
537          * us. (some of these will be overridden and become
538          * 'special' SMP interrupts)
539          */
540         for (i = 0; i < (NR_VECTORS - FIRST_EXTERNAL_VECTOR); i++) {
541                 int vector = FIRST_EXTERNAL_VECTOR + i;
542                 if (i >= NR_IRQS)
543                         break;
544                 if (vector != IA32_SYSCALL_VECTOR)
545                         set_intr_gate(vector, interrupt[i]);
546         }
547
548 #ifdef CONFIG_SMP
549         /*
550          * IRQ0 must be given a fixed assignment and initialized,
551          * because it's used before the IO-APIC is set up.
552          */
553         set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
554
555         /*
556          * The reschedule interrupt is a CPU-to-CPU reschedule-helper
557          * IPI, driven by wakeup.
558          */
559         set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
560
561         /* IPIs for invalidation */
562         set_intr_gate(INVALIDATE_TLB_VECTOR_START+0, invalidate_interrupt0);
563         set_intr_gate(INVALIDATE_TLB_VECTOR_START+1, invalidate_interrupt1);
564         set_intr_gate(INVALIDATE_TLB_VECTOR_START+2, invalidate_interrupt2);
565         set_intr_gate(INVALIDATE_TLB_VECTOR_START+3, invalidate_interrupt3);
566         set_intr_gate(INVALIDATE_TLB_VECTOR_START+4, invalidate_interrupt4);
567         set_intr_gate(INVALIDATE_TLB_VECTOR_START+5, invalidate_interrupt5);
568         set_intr_gate(INVALIDATE_TLB_VECTOR_START+6, invalidate_interrupt6);
569         set_intr_gate(INVALIDATE_TLB_VECTOR_START+7, invalidate_interrupt7);
570
571         /* IPI for generic function call */
572         set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
573 #endif  
574         set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
575         set_intr_gate(THRESHOLD_APIC_VECTOR, threshold_interrupt);
576
577         /* self generated IPI for local APIC timer */
578         set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
579
580         /* IPI vectors for APIC spurious and error interrupts */
581         set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
582         set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
583
584         /*
585          * Set the clock to HZ Hz, we already have a valid
586          * vector now:
587          */
588         setup_timer_hardware();
589
590         if (!acpi_ioapic)
591                 setup_irq(2, &irq2);
592 }