| 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> | 
| Jaswinder Singh Rajput | 72ade5f | 2009-01-04 16:32:36 +0530 | [diff] [blame] | 18 | #include <linux/uaccess.h> | 
| Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 19 | #include <linux/percpu.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 |  | 
| Thomas Gleixner | e05d723 | 2007-02-16 01:27:58 -0800 | [diff] [blame] | 21 | #include <asm/apic.h> | 
| Thomas Gleixner | e05d723 | 2007-02-16 01:27:58 -0800 | [diff] [blame] | 22 |  | 
| Fenghua Yu | f34e3b6 | 2007-07-19 01:48:13 -0700 | [diff] [blame] | 23 | DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | EXPORT_PER_CPU_SYMBOL(irq_stat); | 
|  | 25 |  | 
| Jeremy Fitzhardinge | 7c3576d | 2007-05-02 19:27:16 +0200 | [diff] [blame] | 26 | DEFINE_PER_CPU(struct pt_regs *, irq_regs); | 
|  | 27 | EXPORT_PER_CPU_SYMBOL(irq_regs); | 
|  | 28 |  | 
| Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 29 | #ifdef CONFIG_DEBUG_STACKOVERFLOW | 
|  | 30 | /* Debugging check for stack overflow: is there less than 1KB free? */ | 
|  | 31 | static int check_stack_overflow(void) | 
|  | 32 | { | 
|  | 33 | long sp; | 
|  | 34 |  | 
|  | 35 | __asm__ __volatile__("andl %%esp,%0" : | 
|  | 36 | "=r" (sp) : "0" (THREAD_SIZE - 1)); | 
|  | 37 |  | 
|  | 38 | return sp < (sizeof(struct thread_info) + STACK_WARN); | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | static void print_stack_overflow(void) | 
|  | 42 | { | 
|  | 43 | printk(KERN_WARNING "low stack detected by irq handler\n"); | 
|  | 44 | dump_stack(); | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | #else | 
|  | 48 | static inline int check_stack_overflow(void) { return 0; } | 
|  | 49 | static inline void print_stack_overflow(void) { } | 
|  | 50 | #endif | 
|  | 51 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #ifdef CONFIG_4KSTACKS | 
|  | 53 | /* | 
|  | 54 | * per-CPU IRQ handling contexts (thread information and stack) | 
|  | 55 | */ | 
|  | 56 | union irq_ctx { | 
|  | 57 | struct thread_info      tinfo; | 
|  | 58 | u32                     stack[THREAD_SIZE/sizeof(u32)]; | 
| Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 59 | } __attribute__((aligned(PAGE_SIZE))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 |  | 
| Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 61 | static DEFINE_PER_CPU(union irq_ctx *, hardirq_ctx); | 
|  | 62 | static DEFINE_PER_CPU(union irq_ctx *, softirq_ctx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 |  | 
| Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 64 | static DEFINE_PER_CPU_PAGE_ALIGNED(union irq_ctx, hardirq_stack); | 
|  | 65 | static DEFINE_PER_CPU_PAGE_ALIGNED(union irq_ctx, softirq_stack); | 
| Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 66 |  | 
|  | 67 | static void call_on_stack(void *func, void *stack) | 
|  | 68 | { | 
|  | 69 | asm volatile("xchgl	%%ebx,%%esp	\n" | 
|  | 70 | "call	*%%edi		\n" | 
|  | 71 | "movl	%%ebx,%%esp	\n" | 
|  | 72 | : "=b" (stack) | 
|  | 73 | : "0" (stack), | 
|  | 74 | "D"(func) | 
|  | 75 | : "memory", "cc", "edx", "ecx", "eax"); | 
| Andi Kleen | 04b361a | 2008-05-05 12:36:38 +0200 | [diff] [blame] | 76 | } | 
|  | 77 |  | 
| Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 78 | static inline int | 
|  | 79 | execute_on_irq_stack(int overflow, struct irq_desc *desc, int irq) | 
|  | 80 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | union irq_ctx *curctx, *irqctx; | 
| Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 82 | u32 *isp, arg1, arg2; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 |  | 
|  | 84 | curctx = (union irq_ctx *) current_thread_info(); | 
| Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 85 | irqctx = __get_cpu_var(hardirq_ctx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 |  | 
|  | 87 | /* | 
|  | 88 | * this is where we switch to the IRQ stack. However, if we are | 
|  | 89 | * already using the IRQ stack (because we interrupted a hardirq | 
|  | 90 | * handler) we can't do that and just have to keep using the | 
|  | 91 | * current stack (which is the irq stack already after all) | 
|  | 92 | */ | 
| Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 93 | if (unlikely(curctx == irqctx)) | 
|  | 94 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 |  | 
| Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 96 | /* build the stack frame on the IRQ stack */ | 
| Jaswinder Singh Rajput | 72ade5f | 2009-01-04 16:32:36 +0530 | [diff] [blame] | 97 | isp = (u32 *) ((char *)irqctx + sizeof(*irqctx)); | 
| Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 98 | irqctx->tinfo.task = curctx->tinfo.task; | 
|  | 99 | irqctx->tinfo.previous_esp = current_stack_pointer; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 |  | 
| Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 101 | /* | 
|  | 102 | * Copy the softirq bits in preempt_count so that the | 
|  | 103 | * softirq checks work in the hardirq context. | 
|  | 104 | */ | 
|  | 105 | irqctx->tinfo.preempt_count = | 
|  | 106 | (irqctx->tinfo.preempt_count & ~SOFTIRQ_MASK) | | 
|  | 107 | (curctx->tinfo.preempt_count & SOFTIRQ_MASK); | 
| Björn Steinbrink | a5d157e | 2006-06-25 16:24:40 +0200 | [diff] [blame] | 108 |  | 
| Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 109 | if (unlikely(overflow)) | 
| Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 110 | call_on_stack(print_stack_overflow, isp); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 |  | 
| Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 112 | asm volatile("xchgl	%%ebx,%%esp	\n" | 
|  | 113 | "call	*%%edi		\n" | 
|  | 114 | "movl	%%ebx,%%esp	\n" | 
|  | 115 | : "=a" (arg1), "=d" (arg2), "=b" (isp) | 
|  | 116 | :  "0" (irq),   "1" (desc),  "2" (isp), | 
|  | 117 | "D" (desc->handle_irq) | 
|  | 118 | : "memory", "cc", "ecx"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | return 1; | 
|  | 120 | } | 
|  | 121 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* | 
|  | 123 | * allocate per-cpu stacks for hardirq and for softirq processing | 
|  | 124 | */ | 
| Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 125 | void __cpuinit irq_ctx_init(int cpu) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { | 
|  | 127 | union irq_ctx *irqctx; | 
|  | 128 |  | 
| Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 129 | if (per_cpu(hardirq_ctx, cpu)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | return; | 
|  | 131 |  | 
| Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 132 | irqctx = &per_cpu(hardirq_stack, cpu); | 
| Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 133 | irqctx->tinfo.task		= NULL; | 
|  | 134 | irqctx->tinfo.exec_domain	= NULL; | 
|  | 135 | irqctx->tinfo.cpu		= cpu; | 
|  | 136 | irqctx->tinfo.preempt_count	= HARDIRQ_OFFSET; | 
|  | 137 | irqctx->tinfo.addr_limit	= MAKE_MM_SEG(0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 |  | 
| Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 139 | per_cpu(hardirq_ctx, cpu) = irqctx; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 |  | 
| Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 141 | irqctx = &per_cpu(softirq_stack, cpu); | 
| Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 142 | irqctx->tinfo.task		= NULL; | 
|  | 143 | irqctx->tinfo.exec_domain	= NULL; | 
|  | 144 | irqctx->tinfo.cpu		= cpu; | 
|  | 145 | irqctx->tinfo.preempt_count	= 0; | 
|  | 146 | irqctx->tinfo.addr_limit	= MAKE_MM_SEG(0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 |  | 
| Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 148 | per_cpu(softirq_ctx, cpu) = irqctx; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 |  | 
| Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 150 | printk(KERN_DEBUG "CPU %u irqstacks, hard=%p soft=%p\n", | 
| Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 151 | cpu, per_cpu(hardirq_ctx, cpu),  per_cpu(softirq_ctx, cpu)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
| Li Shaohua | e1367da | 2005-06-25 14:54:56 -0700 | [diff] [blame] | 154 | void irq_ctx_exit(int cpu) | 
|  | 155 | { | 
| Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 156 | per_cpu(hardirq_ctx, cpu) = NULL; | 
| Li Shaohua | e1367da | 2005-06-25 14:54:56 -0700 | [diff] [blame] | 157 | } | 
|  | 158 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | asmlinkage void do_softirq(void) | 
|  | 160 | { | 
|  | 161 | unsigned long flags; | 
|  | 162 | struct thread_info *curctx; | 
|  | 163 | union irq_ctx *irqctx; | 
|  | 164 | u32 *isp; | 
|  | 165 |  | 
|  | 166 | if (in_interrupt()) | 
|  | 167 | return; | 
|  | 168 |  | 
|  | 169 | local_irq_save(flags); | 
|  | 170 |  | 
|  | 171 | if (local_softirq_pending()) { | 
|  | 172 | curctx = current_thread_info(); | 
| Lai Jiangshan | 42f8fae | 2009-02-17 11:46:42 +0800 | [diff] [blame] | 173 | irqctx = __get_cpu_var(softirq_ctx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | irqctx->tinfo.task = curctx->task; | 
|  | 175 | irqctx->tinfo.previous_esp = current_stack_pointer; | 
|  | 176 |  | 
|  | 177 | /* build the stack frame on the softirq stack */ | 
| Jaswinder Singh Rajput | 72ade5f | 2009-01-04 16:32:36 +0530 | [diff] [blame] | 178 | isp = (u32 *) ((char *)irqctx + sizeof(*irqctx)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 |  | 
| Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 180 | call_on_stack(__do_softirq, isp); | 
| Ingo Molnar | 55f327f | 2006-07-03 00:24:43 -0700 | [diff] [blame] | 181 | /* | 
|  | 182 | * Shouldnt happen, we returned above if in_interrupt(): | 
| Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 183 | */ | 
| Ingo Molnar | 55f327f | 2006-07-03 00:24:43 -0700 | [diff] [blame] | 184 | WARN_ON_ONCE(softirq_count()); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } | 
|  | 186 |  | 
|  | 187 | local_irq_restore(flags); | 
|  | 188 | } | 
| Thomas Gleixner | 403d8ef | 2008-05-05 18:13:50 +0200 | [diff] [blame] | 189 |  | 
| Thomas Gleixner | de9b10a | 2008-05-05 15:58:15 +0200 | [diff] [blame] | 190 | #else | 
|  | 191 | static inline int | 
|  | 192 | 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] | 193 | #endif | 
|  | 194 |  | 
| Jeremy Fitzhardinge | 9b2b76a | 2009-02-06 14:09:40 -0800 | [diff] [blame] | 195 | bool handle_irq(unsigned irq, struct pt_regs *regs) | 
|  | 196 | { | 
|  | 197 | struct irq_desc *desc; | 
|  | 198 | int overflow; | 
|  | 199 |  | 
|  | 200 | overflow = check_stack_overflow(); | 
|  | 201 |  | 
|  | 202 | desc = irq_to_desc(irq); | 
|  | 203 | if (unlikely(!desc)) | 
|  | 204 | return false; | 
|  | 205 |  | 
|  | 206 | if (!execute_on_irq_stack(overflow, desc, irq)) { | 
|  | 207 | if (unlikely(overflow)) | 
|  | 208 | print_stack_overflow(); | 
|  | 209 | desc->handle_irq(irq, desc); | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | return true; | 
|  | 213 | } | 
|  | 214 |  | 
| Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 215 | #ifdef CONFIG_HOTPLUG_CPU | 
| Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 216 |  | 
| Mike Travis | d7b381b | 2008-12-16 17:33:58 -0800 | [diff] [blame] | 217 | /* A cpu has been removed from cpu_online_mask.  Reset irq affinities. */ | 
|  | 218 | void fixup_irqs(void) | 
| Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 219 | { | 
|  | 220 | unsigned int irq; | 
|  | 221 | static int warned; | 
| Yinghai Lu | 199751d | 2008-08-19 20:50:27 -0700 | [diff] [blame] | 222 | struct irq_desc *desc; | 
| Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 223 |  | 
| Yinghai Lu | 199751d | 2008-08-19 20:50:27 -0700 | [diff] [blame] | 224 | for_each_irq_desc(irq, desc) { | 
| Mike Travis | d7b381b | 2008-12-16 17:33:58 -0800 | [diff] [blame] | 225 | const struct cpumask *affinity; | 
| Yinghai Lu | 08678b0 | 2008-08-19 20:50:05 -0700 | [diff] [blame] | 226 |  | 
| Yinghai Lu | 0b8f1ef | 2008-12-05 18:58:31 -0800 | [diff] [blame] | 227 | if (!desc) | 
|  | 228 | continue; | 
| Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 229 | if (irq == 2) | 
|  | 230 | continue; | 
|  | 231 |  | 
| Mike Travis | 7f7ace0 | 2009-01-10 21:58:08 -0800 | [diff] [blame] | 232 | affinity = desc->affinity; | 
| Mike Travis | d7b381b | 2008-12-16 17:33:58 -0800 | [diff] [blame] | 233 | if (cpumask_any_and(affinity, cpu_online_mask) >= nr_cpu_ids) { | 
| Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 234 | printk("Breaking affinity for irq %i\n", irq); | 
| Mike Travis | d7b381b | 2008-12-16 17:33:58 -0800 | [diff] [blame] | 235 | affinity = cpu_all_mask; | 
| Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 236 | } | 
| Yinghai Lu | 08678b0 | 2008-08-19 20:50:05 -0700 | [diff] [blame] | 237 | if (desc->chip->set_affinity) | 
| Mike Travis | d7b381b | 2008-12-16 17:33:58 -0800 | [diff] [blame] | 238 | desc->chip->set_affinity(irq, affinity); | 
| Yinghai Lu | 08678b0 | 2008-08-19 20:50:05 -0700 | [diff] [blame] | 239 | else if (desc->action && !(warned++)) | 
| Zwane Mwaikambo | f370513 | 2005-06-25 14:54:50 -0700 | [diff] [blame] | 240 | 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 |  |