blob: 0a57e39159a8c4e3deec36604cab2a2cd9f135b0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1992, 1998 Linus Torvalds, Ingo Molnar
3 *
4 * This file contains the lowest level x86-specific interrupt
5 * entry, irq-stacks and irq statistics code. All the remaining
6 * irq logic is done by the generic kernel/irq/ code and
7 * by the x86-specific irq controller code. (e.g. i8259.c and
8 * io_apic.c.)
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/seq_file.h>
13#include <linux/interrupt.h>
14#include <linux/kernel_stat.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070015#include <linux/notifier.h>
16#include <linux/cpu.h>
17#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Thomas Gleixnere05d7232007-02-16 01:27:58 -080019#include <asm/apic.h>
20#include <asm/uaccess.h>
21
Fenghua Yuf34e3b62007-07-19 01:48:13 -070022DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023EXPORT_PER_CPU_SYMBOL(irq_stat);
24
Jeremy Fitzhardinge7c3576d2007-05-02 19:27:16 +020025DEFINE_PER_CPU(struct pt_regs *, irq_regs);
26EXPORT_PER_CPU_SYMBOL(irq_regs);
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
29 * 'what should we do if we get a hw irq event on an illegal vector'.
30 * each architecture has to answer this themselves.
31 */
32void ack_bad_irq(unsigned int irq)
33{
Thomas Gleixnere05d7232007-02-16 01:27:58 -080034 printk(KERN_ERR "unexpected IRQ trap at vector %02x\n", irq);
35
36#ifdef CONFIG_X86_LOCAL_APIC
37 /*
38 * Currently unexpected vectors happen only on SMP and APIC.
39 * We _must_ ack these because every local APIC has only N
40 * irq slots per priority level, and a 'hanging, unacked' IRQ
41 * holds up an irq slot - in excessive cases (when multiple
42 * unexpected vectors occur) that might lock up the APIC
43 * completely.
44 * But only ack when the APIC is enabled -AK
45 */
46 if (cpu_has_apic)
47 ack_APIC_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#endif
Thomas Gleixnere05d7232007-02-16 01:27:58 -080049}
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020051#ifdef CONFIG_DEBUG_STACKOVERFLOW
52/* Debugging check for stack overflow: is there less than 1KB free? */
53static int check_stack_overflow(void)
54{
55 long sp;
56
57 __asm__ __volatile__("andl %%esp,%0" :
58 "=r" (sp) : "0" (THREAD_SIZE - 1));
59
60 return sp < (sizeof(struct thread_info) + STACK_WARN);
61}
62
63static void print_stack_overflow(void)
64{
65 printk(KERN_WARNING "low stack detected by irq handler\n");
66 dump_stack();
67}
68
69#else
70static inline int check_stack_overflow(void) { return 0; }
71static inline void print_stack_overflow(void) { }
72#endif
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#ifdef CONFIG_4KSTACKS
75/*
76 * per-CPU IRQ handling contexts (thread information and stack)
77 */
78union irq_ctx {
79 struct thread_info tinfo;
80 u32 stack[THREAD_SIZE/sizeof(u32)];
81};
82
Andreas Mohr22722052006-06-23 02:05:30 -070083static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
84static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Jeremy Fitzhardingecbcd79c2008-07-08 15:06:27 -070086static char softirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
87static char hardirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
Thomas Gleixner403d8ef2008-05-05 18:13:50 +020088
89static void call_on_stack(void *func, void *stack)
90{
91 asm volatile("xchgl %%ebx,%%esp \n"
92 "call *%%edi \n"
93 "movl %%ebx,%%esp \n"
94 : "=b" (stack)
95 : "0" (stack),
96 "D"(func)
97 : "memory", "cc", "edx", "ecx", "eax");
Andi Kleen04b361a2008-05-05 12:36:38 +020098}
99
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200100static inline int
101execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq)
102{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 union irq_ctx *curctx, *irqctx;
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200104 u32 *isp, arg1, arg2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 curctx = (union irq_ctx *) current_thread_info();
107 irqctx = hardirq_ctx[smp_processor_id()];
108
109 /*
110 * this is where we switch to the IRQ stack. However, if we are
111 * already using the IRQ stack (because we interrupted a hardirq
112 * handler) we can't do that and just have to keep using the
113 * current stack (which is the irq stack already after all)
114 */
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200115 if (unlikely(curctx == irqctx))
116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200118 /* build the stack frame on the IRQ stack */
119 isp = (u32 *) ((char*)irqctx + sizeof(*irqctx));
120 irqctx->tinfo.task = curctx->tinfo.task;
121 irqctx->tinfo.previous_esp = current_stack_pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200123 /*
124 * Copy the softirq bits in preempt_count so that the
125 * softirq checks work in the hardirq context.
126 */
127 irqctx->tinfo.preempt_count =
128 (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
129 (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
Björn Steinbrinka5d157e2006-06-25 16:24:40 +0200130
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200131 if (unlikely(overflow))
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200132 call_on_stack(print_stack_overflow, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200134 asm volatile("xchgl %%ebx,%%esp \n"
135 "call *%%edi \n"
136 "movl %%ebx,%%esp \n"
137 : "=a" (arg1), "=d" (arg2), "=b" (isp)
138 : "0" (irq), "1" (desc), "2" (isp),
139 "D" (desc->handle_irq)
140 : "memory", "cc", "ecx");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return 1;
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*
145 * allocate per-cpu stacks for hardirq and for softirq processing
146 */
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200147void __cpuinit irq_ctx_init(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 union irq_ctx *irqctx;
150
151 if (hardirq_ctx[cpu])
152 return;
153
154 irqctx = (union irq_ctx*) &hardirq_stack[cpu*THREAD_SIZE];
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200155 irqctx->tinfo.task = NULL;
156 irqctx->tinfo.exec_domain = NULL;
157 irqctx->tinfo.cpu = cpu;
158 irqctx->tinfo.preempt_count = HARDIRQ_OFFSET;
159 irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 hardirq_ctx[cpu] = irqctx;
162
163 irqctx = (union irq_ctx*) &softirq_stack[cpu*THREAD_SIZE];
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200164 irqctx->tinfo.task = NULL;
165 irqctx->tinfo.exec_domain = NULL;
166 irqctx->tinfo.cpu = cpu;
167 irqctx->tinfo.preempt_count = 0;
168 irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 softirq_ctx[cpu] = irqctx;
171
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200172 printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n",
173 cpu,hardirq_ctx[cpu],softirq_ctx[cpu]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
Li Shaohuae1367da2005-06-25 14:54:56 -0700176void irq_ctx_exit(int cpu)
177{
178 hardirq_ctx[cpu] = NULL;
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181asmlinkage void do_softirq(void)
182{
183 unsigned long flags;
184 struct thread_info *curctx;
185 union irq_ctx *irqctx;
186 u32 *isp;
187
188 if (in_interrupt())
189 return;
190
191 local_irq_save(flags);
192
193 if (local_softirq_pending()) {
194 curctx = current_thread_info();
195 irqctx = softirq_ctx[smp_processor_id()];
196 irqctx->tinfo.task = curctx->task;
197 irqctx->tinfo.previous_esp = current_stack_pointer;
198
199 /* build the stack frame on the softirq stack */
200 isp = (u32*) ((char*)irqctx + sizeof(*irqctx));
201
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200202 call_on_stack(__do_softirq, isp);
Ingo Molnar55f327f2006-07-03 00:24:43 -0700203 /*
204 * Shouldnt happen, we returned above if in_interrupt():
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200205 */
Ingo Molnar55f327f2006-07-03 00:24:43 -0700206 WARN_ON_ONCE(softirq_count());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209 local_irq_restore(flags);
210}
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200211
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200212#else
213static inline int
214execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq) { return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#endif
216
217/*
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200218 * do_IRQ handles all normal device IRQ's (the special
219 * SMP cross-CPU interrupts have their own specific
220 * handlers).
221 */
222unsigned int do_IRQ(struct pt_regs *regs)
223{
224 struct pt_regs *old_regs;
225 /* high bit used in ret_from_ code */
226 int overflow, irq = ~regs->orig_ax;
Yinghai Lu199751d2008-08-19 20:50:27 -0700227 struct irq_desc *desc;
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200228
Yinghai Lu199751d2008-08-19 20:50:27 -0700229 desc = irq_to_desc(irq);
230 if (unlikely(!desc)) {
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200231 printk(KERN_EMERG "%s: cannot handle IRQ %d\n",
232 __func__, irq);
233 BUG();
234 }
235
236 old_regs = set_irq_regs(regs);
237 irq_enter();
238
239 overflow = check_stack_overflow();
240
241 if (!execute_on_irq_stack(overflow, desc, irq)) {
242 if (unlikely(overflow))
243 print_stack_overflow();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 desc->handle_irq(irq, desc);
Andi Kleen04b361a2008-05-05 12:36:38 +0200245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 irq_exit();
248 set_irq_regs(old_regs);
249 return 1;
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/*
253 * Interrupt statistics:
254 */
255
256atomic_t irq_err_count;
257
258/*
259 * /proc/interrupts printing:
260 */
261
262int show_interrupts(struct seq_file *p, void *v)
263{
264 int i = *(loff_t *) v, j;
265 struct irqaction * action;
266 unsigned long flags;
Yinghai Lu199751d2008-08-19 20:50:27 -0700267 unsigned int entries;
268 struct irq_desc *desc;
269 int tail = 0;
270
271#ifdef CONFIG_HAVE_SPARSE_IRQ
272 desc = (struct irq_desc *)v;
273 entries = -1U;
274 i = desc->irq;
275 if (!desc->next)
276 tail = 1;
277#else
278 entries = nr_irqs - 1;
279 i = *(loff_t *) v;
280 if (i == nr_irqs)
281 tail = 1;
282 else
283 desc = irq_to_desc(i);
284#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 if (i == 0) {
287 seq_printf(p, " ");
Natalie Protasevich9f40a722005-10-30 14:59:32 -0800288 for_each_online_cpu(j)
Jan Beulichbdbdaa72006-06-26 13:59:23 +0200289 seq_printf(p, "CPU%-8d",j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 seq_putc(p, '\n');
291 }
292
Yinghai Lu199751d2008-08-19 20:50:27 -0700293 if (i <= entries) {
Jan Beulich072f5d82007-10-17 18:04:40 +0200294 unsigned any_count = 0;
295
Yinghai Lu08678b02008-08-19 20:50:05 -0700296 spin_lock_irqsave(&desc->lock, flags);
Jan Beulich072f5d82007-10-17 18:04:40 +0200297#ifndef CONFIG_SMP
298 any_count = kstat_irqs(i);
299#else
300 for_each_online_cpu(j)
Yinghai Lu7f95ec92008-08-19 20:50:09 -0700301 any_count |= kstat_irqs_cpu(i, j);
Jan Beulich072f5d82007-10-17 18:04:40 +0200302#endif
Yinghai Lu08678b02008-08-19 20:50:05 -0700303 action = desc->action;
Jan Beulich072f5d82007-10-17 18:04:40 +0200304 if (!action && !any_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 goto skip;
Yinghai Lu199751d2008-08-19 20:50:27 -0700306 seq_printf(p, "%#x: ",i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307#ifndef CONFIG_SMP
308 seq_printf(p, "%10u ", kstat_irqs(i));
309#else
Natalie Protasevich9f40a722005-10-30 14:59:32 -0800310 for_each_online_cpu(j)
Yinghai Lu7f95ec92008-08-19 20:50:09 -0700311 seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312#endif
Yinghai Lu08678b02008-08-19 20:50:05 -0700313 seq_printf(p, " %8s", desc->chip->name);
314 seq_printf(p, "-%-8s", desc->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Jan Beulich072f5d82007-10-17 18:04:40 +0200316 if (action) {
317 seq_printf(p, " %s", action->name);
318 while ((action = action->next) != NULL)
319 seq_printf(p, ", %s", action->name);
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 seq_putc(p, '\n');
323skip:
Yinghai Lu08678b02008-08-19 20:50:05 -0700324 spin_unlock_irqrestore(&desc->lock, flags);
Yinghai Lu199751d2008-08-19 20:50:27 -0700325 }
326
327 if (tail) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 seq_printf(p, "NMI: ");
Natalie Protasevich9f40a722005-10-30 14:59:32 -0800329 for_each_online_cpu(j)
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700330 seq_printf(p, "%10u ", nmi_count(j));
Joe Korty38e760a2007-10-17 18:04:40 +0200331 seq_printf(p, " Non-maskable interrupts\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332#ifdef CONFIG_X86_LOCAL_APIC
333 seq_printf(p, "LOC: ");
Natalie Protasevich9f40a722005-10-30 14:59:32 -0800334 for_each_online_cpu(j)
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700335 seq_printf(p, "%10u ",
336 per_cpu(irq_stat,j).apic_timer_irqs);
Joe Korty38e760a2007-10-17 18:04:40 +0200337 seq_printf(p, " Local timer interrupts\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338#endif
Joe Korty38e760a2007-10-17 18:04:40 +0200339#ifdef CONFIG_SMP
340 seq_printf(p, "RES: ");
341 for_each_online_cpu(j)
342 seq_printf(p, "%10u ",
343 per_cpu(irq_stat,j).irq_resched_count);
344 seq_printf(p, " Rescheduling interrupts\n");
345 seq_printf(p, "CAL: ");
346 for_each_online_cpu(j)
347 seq_printf(p, "%10u ",
348 per_cpu(irq_stat,j).irq_call_count);
Andi Kleendc44e652008-09-04 13:47:38 +0200349 seq_printf(p, " Function call interrupts\n");
Joe Korty38e760a2007-10-17 18:04:40 +0200350 seq_printf(p, "TLB: ");
351 for_each_online_cpu(j)
352 seq_printf(p, "%10u ",
353 per_cpu(irq_stat,j).irq_tlb_count);
354 seq_printf(p, " TLB shootdowns\n");
355#endif
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200356#ifdef CONFIG_X86_MCE
Joe Korty38e760a2007-10-17 18:04:40 +0200357 seq_printf(p, "TRM: ");
358 for_each_online_cpu(j)
359 seq_printf(p, "%10u ",
360 per_cpu(irq_stat,j).irq_thermal_count);
361 seq_printf(p, " Thermal event interrupts\n");
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200362#endif
363#ifdef CONFIG_X86_LOCAL_APIC
Joe Korty38e760a2007-10-17 18:04:40 +0200364 seq_printf(p, "SPU: ");
365 for_each_online_cpu(j)
366 seq_printf(p, "%10u ",
367 per_cpu(irq_stat,j).irq_spurious_count);
368 seq_printf(p, " Spurious interrupts\n");
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200369#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 seq_printf(p, "ERR: %10u\n", atomic_read(&irq_err_count));
371#if defined(CONFIG_X86_IO_APIC)
372 seq_printf(p, "MIS: %10u\n", atomic_read(&irq_mis_count));
373#endif
374 }
375 return 0;
376}
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700377
Jan Beulicha2eddfa2008-05-12 15:44:41 +0200378/*
379 * /proc/stat helpers
380 */
381u64 arch_irq_stat_cpu(unsigned int cpu)
382{
383 u64 sum = nmi_count(cpu);
384
385#ifdef CONFIG_X86_LOCAL_APIC
386 sum += per_cpu(irq_stat, cpu).apic_timer_irqs;
387#endif
388#ifdef CONFIG_SMP
389 sum += per_cpu(irq_stat, cpu).irq_resched_count;
390 sum += per_cpu(irq_stat, cpu).irq_call_count;
391 sum += per_cpu(irq_stat, cpu).irq_tlb_count;
392#endif
393#ifdef CONFIG_X86_MCE
394 sum += per_cpu(irq_stat, cpu).irq_thermal_count;
395#endif
396#ifdef CONFIG_X86_LOCAL_APIC
397 sum += per_cpu(irq_stat, cpu).irq_spurious_count;
398#endif
399 return sum;
400}
401
402u64 arch_irq_stat(void)
403{
404 u64 sum = atomic_read(&irq_err_count);
405
406#ifdef CONFIG_X86_IO_APIC
407 sum += atomic_read(&irq_mis_count);
408#endif
409 return sum;
410}
411
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700412#ifdef CONFIG_HOTPLUG_CPU
413#include <mach_apic.h>
414
415void fixup_irqs(cpumask_t map)
416{
417 unsigned int irq;
418 static int warned;
Yinghai Lu199751d2008-08-19 20:50:27 -0700419 struct irq_desc *desc;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700420
Yinghai Lu199751d2008-08-19 20:50:27 -0700421 for_each_irq_desc(irq, desc) {
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700422 cpumask_t mask;
Yinghai Lu08678b02008-08-19 20:50:05 -0700423
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700424 if (irq == 2)
425 continue;
426
Yinghai Lu08678b02008-08-19 20:50:05 -0700427 cpus_and(mask, desc->affinity, map);
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700428 if (any_online_cpu(mask) == NR_CPUS) {
429 printk("Breaking affinity for irq %i\n", irq);
430 mask = map;
431 }
Yinghai Lu08678b02008-08-19 20:50:05 -0700432 if (desc->chip->set_affinity)
433 desc->chip->set_affinity(irq, mask);
434 else if (desc->action && !(warned++))
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700435 printk("Cannot set affinity for irq %i\n", irq);
436 }
437
438#if 0
439 barrier();
440 /* Ingo Molnar says: "after the IO-APIC masks have been redirected
441 [note the nop - the interrupt-enable boundary on x86 is two
442 instructions from sti] - to flush out pending hardirqs and
443 IPIs. After this point nothing is supposed to reach this CPU." */
444 __asm__ __volatile__("sti; nop; cli");
445 barrier();
446#else
447 /* That doesn't seem sufficient. Give it 1ms. */
448 local_irq_enable();
449 mdelay(1);
450 local_irq_disable();
451#endif
452}
453#endif
454