upgrade to fedora-2.6.12-1.1398.FC4 + vserver 2.0.rc7
[linux-2.6.git] / arch / i386 / kernel / irq.c
1 /*
2  *      linux/arch/i386/kernel/irq.c
3  *
4  *      Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
5  *
6  * This file contains the lowest level x86-specific interrupt
7  * entry, irq-stacks and irq statistics code. All the remaining
8  * irq logic is done by the generic kernel/irq/ code and
9  * by the x86-specific irq controller code. (e.g. i8259.c and
10  * io_apic.c.)
11  */
12
13 #include <asm/uaccess.h>
14 #include <linux/module.h>
15 #include <linux/seq_file.h>
16 #include <linux/interrupt.h>
17 #include <linux/kernel_stat.h>
18
19 DEFINE_PER_CPU(irq_cpustat_t, irq_stat) ____cacheline_maxaligned_in_smp;
20 EXPORT_PER_CPU_SYMBOL(irq_stat);
21
22 #ifndef CONFIG_X86_LOCAL_APIC
23 /*
24  * 'what should we do if we get a hw irq event on an illegal vector'.
25  * each architecture has to answer this themselves.
26  */
27 void ack_bad_irq(unsigned int irq)
28 {
29         printk("unexpected IRQ trap at vector %02x\n", irq);
30 }
31 #endif
32
33 #ifdef CONFIG_IRQSTACKS
34 /*
35  * per-CPU IRQ handling contexts (thread information and stack)
36  */
37 union irq_ctx {
38         struct thread_info      tinfo;
39         u32                     stack[THREAD_SIZE/sizeof(u32)];
40 };
41
42 /*
43  * per-CPU IRQ handling stacks
44  */
45 static union irq_ctx *hardirq_ctx[NR_CPUS];
46 static union irq_ctx *softirq_ctx[NR_CPUS];
47 #endif
48
49
50 /*
51  * do_IRQ handles all normal device IRQ's (the special
52  * SMP cross-CPU interrupts have their own specific
53  * handlers).
54  */
55 fastcall unsigned int do_IRQ(struct pt_regs *regs)
56 {       
57         /* high bits used in ret_from_ code */
58         int irq = regs->orig_eax & 0xff;
59 #ifdef CONFIG_IRQSTACKS
60         union irq_ctx *curctx, *irqctx;
61         u32 *isp;
62 #endif
63
64         irq_enter();
65 #ifdef CONFIG_DEBUG_STACKOVERFLOW
66         /* Debugging check for stack overflow: is there less than 1KB free? */
67         {
68                 long esp;
69
70                 __asm__ __volatile__("andl %%esp,%0" :
71                                         "=r" (esp) : "0" (THREAD_SIZE - 1));
72                 if (unlikely(esp < (sizeof(struct thread_info) + STACK_WARN))) {
73                         printk("do_IRQ: stack overflow: %ld\n",
74                                 esp - sizeof(struct thread_info));
75                         dump_stack();
76                 }
77         }
78 #endif
79
80 #ifdef CONFIG_IRQSTACKS
81
82         curctx = (union irq_ctx *) current_thread_info();
83         irqctx = hardirq_ctx[smp_processor_id()];
84
85         /*
86          * this is where we switch to the IRQ stack. However, if we are
87          * already using the IRQ stack (because we interrupted a hardirq
88          * handler) we can't do that and just have to keep using the
89          * current stack (which is the irq stack already after all)
90          */
91         if (curctx != irqctx) {
92                 int arg1, arg2, ebx;
93
94                 /* build the stack frame on the IRQ stack */
95                 isp = (u32*) ((char*)irqctx + sizeof(*irqctx));
96                 irqctx->tinfo.task = curctx->tinfo.task;
97                 irqctx->tinfo.previous_esp = current_stack_pointer;
98
99                 asm volatile(
100                         "       xchgl   %%ebx,%%esp      \n"
101                         "       call    __do_IRQ         \n"
102                         "       movl   %%ebx,%%esp      \n"
103                         : "=a" (arg1), "=d" (arg2), "=b" (ebx)
104                         :  "0" (irq),   "1" (regs),  "2" (isp)
105                         : "memory", "cc", "ecx"
106                 );
107         } else
108 #endif
109                 __do_IRQ(irq, regs);
110
111         irq_exit();
112
113         return 1;
114 }
115
116 #ifdef CONFIG_IRQSTACKS
117 /*
118  * These should really be __section__(".bss.page_aligned") as well, but
119  * gcc's 3.0 and earlier don't handle that correctly.
120  */
121 static char softirq_stack[NR_CPUS * THREAD_SIZE]
122                 __attribute__((__aligned__(THREAD_SIZE)));
123
124 static char hardirq_stack[NR_CPUS * THREAD_SIZE]
125                 __attribute__((__aligned__(THREAD_SIZE)));
126
127 /*
128  * allocate per-cpu stacks for hardirq and for softirq processing
129  */
130 void irq_ctx_init(int cpu)
131 {
132         union irq_ctx *irqctx;
133
134         if (hardirq_ctx[cpu])
135                 return;
136
137         irqctx = (union irq_ctx*) &hardirq_stack[cpu*THREAD_SIZE];
138         irqctx->tinfo.task              = NULL;
139         irqctx->tinfo.exec_domain       = NULL;
140         irqctx->tinfo.cpu               = cpu;
141         irqctx->tinfo.preempt_count     = HARDIRQ_OFFSET;
142         irqctx->tinfo.addr_limit        = MAKE_MM_SEG(0);
143
144         hardirq_ctx[cpu] = irqctx;
145
146         irqctx = (union irq_ctx*) &softirq_stack[cpu*THREAD_SIZE];
147         irqctx->tinfo.task              = NULL;
148         irqctx->tinfo.exec_domain       = NULL;
149         irqctx->tinfo.cpu               = cpu;
150         irqctx->tinfo.preempt_count     = SOFTIRQ_OFFSET;
151         irqctx->tinfo.addr_limit        = MAKE_MM_SEG(0);
152
153         softirq_ctx[cpu] = irqctx;
154
155         printk("CPU %u irqstacks, hard=%p soft=%p\n",
156                 cpu,hardirq_ctx[cpu],softirq_ctx[cpu]);
157 }
158
159 extern asmlinkage void __do_softirq(void);
160
161 asmlinkage void do_softirq(void)
162 {
163         unsigned long flags;
164         struct thread_info *curctx;
165         union irq_ctx *irqctx;
166         u32 *isp;
167
168         if (in_interrupt())
169                 return;
170
171         local_irq_save(flags);
172
173         if (local_softirq_pending()) {
174                 curctx = current_thread_info();
175                 irqctx = softirq_ctx[smp_processor_id()];
176                 irqctx->tinfo.task = curctx->task;
177                 irqctx->tinfo.previous_esp = current_stack_pointer;
178
179                 /* build the stack frame on the softirq stack */
180                 isp = (u32*) ((char*)irqctx + sizeof(*irqctx));
181
182                 asm volatile(
183                         "       xchgl   %%ebx,%%esp     \n"
184                         "       call    __do_softirq    \n"
185                         "       movl    %%ebx,%%esp     \n"
186                         : "=b"(isp)
187                         : "0"(isp)
188                         : "memory", "cc", "edx", "ecx", "eax"
189                 );
190         }
191
192         local_irq_restore(flags);
193 }
194
195 EXPORT_SYMBOL(do_softirq);
196 #endif
197
198 /*
199  * Interrupt statistics:
200  */
201
202 atomic_t irq_err_count;
203
204 /*
205  * /proc/interrupts printing:
206  */
207
208 int show_interrupts(struct seq_file *p, void *v)
209 {
210         int i = *(loff_t *) v, j;
211         struct irqaction * action;
212         unsigned long flags;
213
214         if (i == 0) {
215                 seq_printf(p, "           ");
216                 for (j=0; j<NR_CPUS; j++)
217                         if (cpu_online(j))
218                                 seq_printf(p, "CPU%d       ",j);
219                 seq_putc(p, '\n');
220         }
221
222         if (i < NR_IRQS) {
223                 spin_lock_irqsave(&irq_desc[i].lock, flags);
224                 action = irq_desc[i].action;
225                 if (!action)
226                         goto skip;
227                 seq_printf(p, "%3d: ",i);
228 #ifndef CONFIG_SMP
229                 seq_printf(p, "%10u ", kstat_irqs(i));
230 #else
231                 for (j = 0; j < NR_CPUS; j++)
232                         if (cpu_online(j))
233                                 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
234 #endif
235                 seq_printf(p, " %14s", irq_desc[i].handler->typename);
236                 seq_printf(p, "  %s", action->name);
237
238                 for (action=action->next; action; action = action->next)
239                         seq_printf(p, ", %s", action->name);
240
241                 seq_putc(p, '\n');
242 skip:
243                 spin_unlock_irqrestore(&irq_desc[i].lock, flags);
244         } else if (i == NR_IRQS) {
245                 seq_printf(p, "NMI: ");
246                 for (j = 0; j < NR_CPUS; j++)
247                         if (cpu_online(j))
248                                 seq_printf(p, "%10u ", nmi_count(j));
249                 seq_putc(p, '\n');
250 #ifdef CONFIG_X86_LOCAL_APIC
251                 seq_printf(p, "LOC: ");
252                 for (j = 0; j < NR_CPUS; j++)
253                         if (cpu_online(j))
254                                 seq_printf(p, "%10u ",
255                                         per_cpu(irq_stat,j).apic_timer_irqs);
256                 seq_putc(p, '\n');
257 #endif
258                 seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
259 #if defined(CONFIG_X86_IO_APIC)
260                 seq_printf(p, "MIS: %10u\n", atomic_read(&irq_mis_count));
261 #endif
262         }
263         return 0;
264 }