blob: 4beb9a13873d12c2461e7a448f032f179e1a3c9c [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
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700214#ifdef CONFIG_HOTPLUG_CPU
Ingo Molnar1dcdd3d2009-01-28 17:55:37 +0100215#include <asm/genapic.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700216
Mike Travisd7b381b2008-12-16 17:33:58 -0800217/* A cpu has been removed from cpu_online_mask. Reset irq affinities. */
218void fixup_irqs(void)
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700219{
220 unsigned int irq;
221 static int warned;
Yinghai Lu199751d2008-08-19 20:50:27 -0700222 struct irq_desc *desc;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700223
Yinghai Lu199751d2008-08-19 20:50:27 -0700224 for_each_irq_desc(irq, desc) {
Mike Travisd7b381b2008-12-16 17:33:58 -0800225 const struct cpumask *affinity;
Yinghai Lu08678b02008-08-19 20:50:05 -0700226
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800227 if (!desc)
228 continue;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700229 if (irq == 2)
230 continue;
231
Mike Travis7f7ace02009-01-10 21:58:08 -0800232 affinity = desc->affinity;
Mike Travisd7b381b2008-12-16 17:33:58 -0800233 if (cpumask_any_and(affinity, cpu_online_mask) >= nr_cpu_ids) {
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700234 printk("Breaking affinity for irq %i\n", irq);
Mike Travisd7b381b2008-12-16 17:33:58 -0800235 affinity = cpu_all_mask;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700236 }
Yinghai Lu08678b02008-08-19 20:50:05 -0700237 if (desc->chip->set_affinity)
Mike Travisd7b381b2008-12-16 17:33:58 -0800238 desc->chip->set_affinity(irq, affinity);
Yinghai Lu08678b02008-08-19 20:50:05 -0700239 else if (desc->action && !(warned++))
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700240 printk("Cannot set affinity for irq %i\n", irq);
241 }
242
243#if 0
244 barrier();
245 /* Ingo Molnar says: "after the IO-APIC masks have been redirected
246 [note the nop - the interrupt-enable boundary on x86 is two
247 instructions from sti] - to flush out pending hardirqs and
248 IPIs. After this point nothing is supposed to reach this CPU." */
249 __asm__ __volatile__("sti; nop; cli");
250 barrier();
251#else
252 /* That doesn't seem sufficient. Give it 1ms. */
253 local_irq_enable();
254 mdelay(1);
255 local_irq_disable();
256#endif
257}
258#endif
259