Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/seq_file.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/kernel_stat.h> |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 15 | #include <linux/notifier.h> |
| 16 | #include <linux/cpu.h> |
| 17 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Thomas Gleixner | e05d723 | 2007-02-16 01:27:58 -0800 | [diff] [blame] | 19 | #include <asm/apic.h> |
| 20 | #include <asm/uaccess.h> |
| 21 | |
Fenghua Yu | f34e3b6 | 2007-07-19 01:48:13 -0700 | [diff] [blame] | 22 | DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | EXPORT_PER_CPU_SYMBOL(irq_stat); |
| 24 | |
Jeremy Fitzhardinge | 7c3576d | 2007-05-02 19:27:16 +0200 | [diff] [blame] | 25 | DEFINE_PER_CPU(struct pt_regs *, irq_regs); |
| 26 | EXPORT_PER_CPU_SYMBOL(irq_regs); |
| 27 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 28 | #ifdef CONFIG_DEBUG_STACKOVERFLOW |
| 29 | /* Debugging check for stack overflow: is there less than 1KB free? */ |
| 30 | static 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 | |
| 40 | static void print_stack_overflow(void) |
| 41 | { |
| 42 | printk(KERN_WARNING "low stack detected by irq handler\n"); |
| 43 | dump_stack(); |
| 44 | } |
| 45 | |
| 46 | #else |
| 47 | static inline int check_stack_overflow(void) { return 0; } |
| 48 | static inline void print_stack_overflow(void) { } |
| 49 | #endif |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #ifdef CONFIG_4KSTACKS |
| 52 | /* |
| 53 | * per-CPU IRQ handling contexts (thread information and stack) |
| 54 | */ |
| 55 | union irq_ctx { |
| 56 | struct thread_info tinfo; |
| 57 | u32 stack[THREAD_SIZE/sizeof(u32)]; |
| 58 | }; |
| 59 | |
Andreas Mohr | 2272205 | 2006-06-23 02:05:30 -0700 | [diff] [blame] | 60 | static union irq_ctx *hardirq_ctx[NR_CPUS] __read_mostly; |
| 61 | static union irq_ctx *softirq_ctx[NR_CPUS] __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Jeremy Fitzhardinge | cbcd79c | 2008-07-08 15:06:27 -0700 | [diff] [blame] | 63 | static char softirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss; |
| 64 | static char hardirq_stack[NR_CPUS * THREAD_SIZE] __page_aligned_bss; |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 65 | |
| 66 | static 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 Kleen | 04b361a | 2008-05-05 12:36:38 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 77 | static inline int |
| 78 | execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq) |
| 79 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | union irq_ctx *curctx, *irqctx; |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 81 | u32 *isp, arg1, arg2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 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 Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 92 | if (unlikely(curctx == irqctx)) |
| 93 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 95 | /* build the stack frame on the IRQ stack */ |
| 96 | isp = (u32 *) ((char*)irqctx + sizeof(*irqctx)); |
| 97 | irqctx->tinfo.task = curctx->tinfo.task; |
| 98 | irqctx->tinfo.previous_esp = current_stack_pointer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 100 | /* |
| 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 Steinbrink | a5d157e | 2006-06-25 16:24:40 +0200 | [diff] [blame] | 107 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 108 | if (unlikely(overflow)) |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 109 | call_on_stack(print_stack_overflow, isp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 111 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return 1; |
| 119 | } |
| 120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | /* |
| 122 | * allocate per-cpu stacks for hardirq and for softirq processing |
| 123 | */ |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 124 | void __cpuinit irq_ctx_init(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 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 Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 132 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | hardirq_ctx[cpu] = irqctx; |
| 139 | |
| 140 | irqctx = (union irq_ctx*) &softirq_stack[cpu*THREAD_SIZE]; |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 141 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | softirq_ctx[cpu] = irqctx; |
| 148 | |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 149 | printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n", |
| 150 | cpu,hardirq_ctx[cpu],softirq_ctx[cpu]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Li Shaohua | e1367da | 2005-06-25 14:54:56 -0700 | [diff] [blame] | 153 | void irq_ctx_exit(int cpu) |
| 154 | { |
| 155 | hardirq_ctx[cpu] = NULL; |
| 156 | } |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 179 | call_on_stack(__do_softirq, isp); |
Ingo Molnar | 55f327f | 2006-07-03 00:24:43 -0700 | [diff] [blame] | 180 | /* |
| 181 | * Shouldnt happen, we returned above if in_interrupt(): |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 182 | */ |
Ingo Molnar | 55f327f | 2006-07-03 00:24:43 -0700 | [diff] [blame] | 183 | WARN_ON_ONCE(softirq_count()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | local_irq_restore(flags); |
| 187 | } |
Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 188 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 189 | #else |
| 190 | static inline int |
| 191 | execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq) { return 0; } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | #endif |
| 193 | |
| 194 | /* |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 195 | * do_IRQ handles all normal device IRQ's (the special |
| 196 | * SMP cross-CPU interrupts have their own specific |
| 197 | * handlers). |
| 198 | */ |
| 199 | unsigned int do_IRQ(struct pt_regs *regs) |
| 200 | { |
| 201 | struct pt_regs *old_regs; |
| 202 | /* high bit used in ret_from_ code */ |
Yinghai Lu | 497c9a1 | 2008-08-19 20:50:28 -0700 | [diff] [blame] | 203 | int overflow; |
| 204 | unsigned vector = ~regs->orig_ax; |
Yinghai Lu | 199751d | 2008-08-19 20:50:27 -0700 | [diff] [blame] | 205 | struct irq_desc *desc; |
Yinghai Lu | 497c9a1 | 2008-08-19 20:50:28 -0700 | [diff] [blame] | 206 | unsigned irq; |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 207 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 208 | |
| 209 | old_regs = set_irq_regs(regs); |
| 210 | irq_enter(); |
Yinghai Lu | 497c9a1 | 2008-08-19 20:50:28 -0700 | [diff] [blame] | 211 | irq = __get_cpu_var(vector_irq)[vector]; |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 212 | |
| 213 | overflow = check_stack_overflow(); |
| 214 | |
Yinghai Lu | 497c9a1 | 2008-08-19 20:50:28 -0700 | [diff] [blame] | 215 | desc = irq_to_desc(irq); |
| 216 | if (unlikely(!desc)) { |
Yinghai Lu | 7a959cf | 2008-08-19 20:50:32 -0700 | [diff] [blame] | 217 | printk(KERN_EMERG "%s: cannot handle IRQ %d vector %#x cpu %d\n", |
| 218 | __func__, irq, vector, smp_processor_id()); |
Yinghai Lu | 497c9a1 | 2008-08-19 20:50:28 -0700 | [diff] [blame] | 219 | BUG(); |
| 220 | } |
| 221 | |
Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 222 | if (!execute_on_irq_stack(overflow, desc, irq)) { |
| 223 | if (unlikely(overflow)) |
| 224 | print_stack_overflow(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | desc->handle_irq(irq, desc); |
Andi Kleen | 04b361a | 2008-05-05 12:36:38 +0200 | [diff] [blame] | 226 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
| 228 | irq_exit(); |
| 229 | set_irq_regs(old_regs); |
| 230 | return 1; |
| 231 | } |
| 232 | |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 233 | #ifdef CONFIG_HOTPLUG_CPU |
| 234 | #include <mach_apic.h> |
| 235 | |
Mike Travis | d7b381b | 2008-12-16 17:33:58 -0800 | [diff] [blame^] | 236 | /* A cpu has been removed from cpu_online_mask. Reset irq affinities. */ |
| 237 | void fixup_irqs(void) |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 238 | { |
| 239 | unsigned int irq; |
| 240 | static int warned; |
Yinghai Lu | 199751d | 2008-08-19 20:50:27 -0700 | [diff] [blame] | 241 | struct irq_desc *desc; |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 242 | |
Yinghai Lu | 199751d | 2008-08-19 20:50:27 -0700 | [diff] [blame] | 243 | for_each_irq_desc(irq, desc) { |
Mike Travis | d7b381b | 2008-12-16 17:33:58 -0800 | [diff] [blame^] | 244 | const struct cpumask *affinity; |
Yinghai Lu | 08678b0 | 2008-08-19 20:50:05 -0700 | [diff] [blame] | 245 | |
Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 246 | if (!desc) |
| 247 | continue; |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 248 | if (irq == 2) |
| 249 | continue; |
| 250 | |
Mike Travis | d7b381b | 2008-12-16 17:33:58 -0800 | [diff] [blame^] | 251 | affinity = &desc->affinity; |
| 252 | if (cpumask_any_and(affinity, cpu_online_mask) >= nr_cpu_ids) { |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 253 | printk("Breaking affinity for irq %i\n", irq); |
Mike Travis | d7b381b | 2008-12-16 17:33:58 -0800 | [diff] [blame^] | 254 | affinity = cpu_all_mask; |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 255 | } |
Yinghai Lu | 08678b0 | 2008-08-19 20:50:05 -0700 | [diff] [blame] | 256 | if (desc->chip->set_affinity) |
Mike Travis | d7b381b | 2008-12-16 17:33:58 -0800 | [diff] [blame^] | 257 | desc->chip->set_affinity(irq, affinity); |
Yinghai Lu | 08678b0 | 2008-08-19 20:50:05 -0700 | [diff] [blame] | 258 | else if (desc->action && !(warned++)) |
Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 259 | printk("Cannot set affinity for irq %i\n", irq); |
| 260 | } |
| 261 | |
| 262 | #if 0 |
| 263 | barrier(); |
| 264 | /* Ingo Molnar says: "after the IO-APIC masks have been redirected |
| 265 | [note the nop - the interrupt-enable boundary on x86 is two |
| 266 | instructions from sti] - to flush out pending hardirqs and |
| 267 | IPIs. After this point nothing is supposed to reach this CPU." */ |
| 268 | __asm__ __volatile__("sti; nop; cli"); |
| 269 | barrier(); |
| 270 | #else |
| 271 | /* That doesn't seem sufficient. Give it 1ms. */ |
| 272 | local_irq_enable(); |
| 273 | mdelay(1); |
| 274 | local_irq_disable(); |
| 275 | #endif |
| 276 | } |
| 277 | #endif |
| 278 | |