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