blob: 61f09fb969ee6303e1d9023b1f6824348d13b220 [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>
Jaswinder Singh Rajput72ade5f2009-01-04 16:32:36 +053018#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Thomas Gleixnere05d7232007-02-16 01:27:58 -080020#include <asm/apic.h>
Thomas Gleixnere05d7232007-02-16 01:27:58 -080021
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
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020028#ifdef CONFIG_DEBUG_STACKOVERFLOW
29/* Debugging check for stack overflow: is there less than 1KB free? */
30static int check_stack_overflow(void)
31{
32 long sp;
33
34 __asm__ __volatile__("andl %%esp,%0" :
35 "=r" (sp) : "0" (THREAD_SIZE - 1));
36
37 return sp < (sizeof(struct thread_info) + STACK_WARN);
38}
39
40static void print_stack_overflow(void)
41{
42 printk(KERN_WARNING "low stack detected by irq handler\n");
43 dump_stack();
44}
45
46#else
47static inline int check_stack_overflow(void) { return 0; }
48static inline void print_stack_overflow(void) { }
49#endif
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#ifdef CONFIG_4KSTACKS
52/*
53 * per-CPU IRQ handling contexts (thread information and stack)
54 */
55union irq_ctx {
56 struct thread_info tinfo;
57 u32 stack[THREAD_SIZE/sizeof(u32)];
58};
59
Andreas Mohr22722052006-06-23 02:05:30 -070060static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly;
61static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Jeremy Fitzhardingecbcd79c2008-07-08 15:06:27 -070063static char softirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
64static char hardirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss;
Thomas Gleixner403d8ef2008-05-05 18:13:50 +020065
66static void call_on_stack(void *func, void *stack)
67{
68 asm volatile("xchgl %%ebx,%%esp \n"
69 "call *%%edi \n"
70 "movl %%ebx,%%esp \n"
71 : "=b" (stack)
72 : "0" (stack),
73 "D"(func)
74 : "memory", "cc", "edx", "ecx", "eax");
Andi Kleen04b361a2008-05-05 12:36:38 +020075}
76
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020077static inline int
78execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq)
79{
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 union irq_ctx *curctx, *irqctx;
Thomas Gleixner403d8ef2008-05-05 18:13:50 +020081 u32 *isp, arg1, arg2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 curctx = (union irq_ctx *) current_thread_info();
84 irqctx = hardirq_ctx[smp_processor_id()];
85
86 /*
87 * this is where we switch to the IRQ stack. However, if we are
88 * already using the IRQ stack (because we interrupted a hardirq
89 * handler) we can't do that and just have to keep using the
90 * current stack (which is the irq stack already after all)
91 */
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020092 if (unlikely(curctx == irqctx))
93 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020095 /* build the stack frame on the IRQ stack */
Jaswinder Singh Rajput72ade5f2009-01-04 16:32:36 +053096 isp = (u32 *) ((char *)irqctx + sizeof(*irqctx));
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +020097 irqctx->tinfo.task = curctx->tinfo.task;
98 irqctx->tinfo.previous_esp = current_stack_pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200100 /*
101 * Copy the softirq bits in preempt_count so that the
102 * softirq checks work in the hardirq context.
103 */
104 irqctx->tinfo.preempt_count =
105 (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) |
106 (curctx->tinfo.preempt_count & SOFTIRQ_MASK);
Björn Steinbrinka5d157e2006-06-25 16:24:40 +0200107
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200108 if (unlikely(overflow))
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200109 call_on_stack(print_stack_overflow, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200111 asm volatile("xchgl %%ebx,%%esp \n"
112 "call *%%edi \n"
113 "movl %%ebx,%%esp \n"
114 : "=a" (arg1), "=d" (arg2), "=b" (isp)
115 : "0" (irq), "1" (desc), "2" (isp),
116 "D" (desc->handle_irq)
117 : "memory", "cc", "ecx");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 1;
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121/*
122 * allocate per-cpu stacks for hardirq and for softirq processing
123 */
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200124void __cpuinit irq_ctx_init(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 union irq_ctx *irqctx;
127
128 if (hardirq_ctx[cpu])
129 return;
130
131 irqctx = (union irq_ctx*) &hardirq_stack[cpu*THREAD_SIZE];
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200132 irqctx->tinfo.task = NULL;
133 irqctx->tinfo.exec_domain = NULL;
134 irqctx->tinfo.cpu = cpu;
135 irqctx->tinfo.preempt_count = HARDIRQ_OFFSET;
136 irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 hardirq_ctx[cpu] = irqctx;
139
Jaswinder Singh Rajput72ade5f2009-01-04 16:32:36 +0530140 irqctx = (union irq_ctx *) &softirq_stack[cpu*THREAD_SIZE];
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200141 irqctx->tinfo.task = NULL;
142 irqctx->tinfo.exec_domain = NULL;
143 irqctx->tinfo.cpu = cpu;
144 irqctx->tinfo.preempt_count = 0;
145 irqctx->tinfo.addr_limit = MAKE_MM_SEG(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 softirq_ctx[cpu] = irqctx;
148
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200149 printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n",
Jaswinder Singh Rajput72ade5f2009-01-04 16:32:36 +0530150 cpu, hardirq_ctx[cpu], softirq_ctx[cpu]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Li Shaohuae1367da2005-06-25 14:54:56 -0700153void irq_ctx_exit(int cpu)
154{
155 hardirq_ctx[cpu] = NULL;
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158asmlinkage 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 */
Jaswinder Singh Rajput72ade5f2009-01-04 16:32:36 +0530177 isp = (u32 *) ((char *)irqctx + sizeof(*irqctx));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200179 call_on_stack(__do_softirq, isp);
Ingo Molnar55f327f2006-07-03 00:24:43 -0700180 /*
181 * Shouldnt happen, we returned above if in_interrupt():
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200182 */
Ingo Molnar55f327f2006-07-03 00:24:43 -0700183 WARN_ON_ONCE(softirq_count());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185
186 local_irq_restore(flags);
187}
Thomas Gleixner403d8ef2008-05-05 18:13:50 +0200188
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200189#else
190static inline int
191execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq) { return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#endif
193
Jeremy Fitzhardinge9b2b76a2009-02-06 14:09:40 -0800194bool handle_irq(unsigned irq, struct pt_regs *regs)
195{
196 struct irq_desc *desc;
197 int overflow;
198
199 overflow = check_stack_overflow();
200
201 desc = irq_to_desc(irq);
202 if (unlikely(!desc))
203 return false;
204
205 if (!execute_on_irq_stack(overflow, desc, irq)) {
206 if (unlikely(overflow))
207 print_stack_overflow();
208 desc->handle_irq(irq, desc);
209 }
210
211 return true;
212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200215 * do_IRQ handles all normal device IRQ's (the special
216 * SMP cross-CPU interrupts have their own specific
217 * handlers).
218 */
219unsigned int do_IRQ(struct pt_regs *regs)
220{
221 struct pt_regs *old_regs;
222 /* high bit used in ret_from_ code */
Yinghai Lu497c9a12008-08-19 20:50:28 -0700223 unsigned vector = ~regs->orig_ax;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700224 unsigned irq;
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200225
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200226 old_regs = set_irq_regs(regs);
227 irq_enter();
Yinghai Lu497c9a12008-08-19 20:50:28 -0700228 irq = __get_cpu_var(vector_irq)[vector];
Thomas Gleixnerde9b10a2008-05-05 15:58:15 +0200229
Jeremy Fitzhardinge9b2b76a2009-02-06 14:09:40 -0800230 if (!handle_irq(irq, regs)) {
Yinghai Lu7a959cf2008-08-19 20:50:32 -0700231 printk(KERN_EMERG "%s: cannot handle IRQ %d vector %#x cpu %d\n",
232 __func__, irq, vector, smp_processor_id());
Yinghai Lu497c9a12008-08-19 20:50:28 -0700233 BUG();
234 }
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 irq_exit();
237 set_irq_regs(old_regs);
238 return 1;
239}
240
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700241#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar1dcdd3d2009-01-28 17:55:37 +0100242#include <asm/genapic.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700243
Mike Travisd7b381b2008-12-16 17:33:58 -0800244/* A cpu has been removed from cpu_online_mask. Reset irq affinities. */
245void fixup_irqs(void)
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700246{
247 unsigned int irq;
248 static int warned;
Yinghai Lu199751d2008-08-19 20:50:27 -0700249 struct irq_desc *desc;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700250
Yinghai Lu199751d2008-08-19 20:50:27 -0700251 for_each_irq_desc(irq, desc) {
Mike Travisd7b381b2008-12-16 17:33:58 -0800252 const struct cpumask *affinity;
Yinghai Lu08678b02008-08-19 20:50:05 -0700253
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800254 if (!desc)
255 continue;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700256 if (irq == 2)
257 continue;
258
Mike Travis7f7ace02009-01-10 21:58:08 -0800259 affinity = desc->affinity;
Mike Travisd7b381b2008-12-16 17:33:58 -0800260 if (cpumask_any_and(affinity, cpu_online_mask) >= nr_cpu_ids) {
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700261 printk("Breaking affinity for irq %i\n", irq);
Mike Travisd7b381b2008-12-16 17:33:58 -0800262 affinity = cpu_all_mask;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700263 }
Yinghai Lu08678b02008-08-19 20:50:05 -0700264 if (desc->chip->set_affinity)
Mike Travisd7b381b2008-12-16 17:33:58 -0800265 desc->chip->set_affinity(irq, affinity);
Yinghai Lu08678b02008-08-19 20:50:05 -0700266 else if (desc->action && !(warned++))
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700267 printk("Cannot set affinity for irq %i\n", irq);
268 }
269
270#if 0
271 barrier();
272 /* Ingo Molnar says: "after the IO-APIC masks have been redirected
273 [note the nop - the interrupt-enable boundary on x86 is two
274 instructions from sti] - to flush out pending hardirqs and
275 IPIs. After this point nothing is supposed to reach this CPU." */
276 __asm__ __volatile__("sti; nop; cli");
277 barrier();
278#else
279 /* That doesn't seem sufficient. Give it 1ms. */
280 local_irq_enable();
281 mdelay(1);
282 local_irq_disable();
283#endif
284}
285#endif
286