Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Intel SMP support routines. |
| 3 | * |
| 4 | * (c) 1995 Alan Cox, Building #3 <alan@redhat.com> |
| 5 | * (c) 1998-99, 2000 Ingo Molnar <mingo@redhat.com> |
| 6 | * (c) 2002,2003 Andi Kleen, SuSE Labs. |
| 7 | * |
| 8 | * This code is released under the GNU General Public License version 2 or |
| 9 | * later. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/init.h> |
| 13 | |
| 14 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/delay.h> |
| 16 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/smp.h> |
| 18 | #include <linux/kernel_stat.h> |
| 19 | #include <linux/mc146818rtc.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | |
| 22 | #include <asm/mtrr.h> |
| 23 | #include <asm/pgalloc.h> |
| 24 | #include <asm/tlbflush.h> |
| 25 | #include <asm/mach_apic.h> |
| 26 | #include <asm/mmu_context.h> |
| 27 | #include <asm/proto.h> |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 28 | #include <asm/apicdef.h> |
Andi Kleen | 95833c8 | 2006-01-11 22:44:36 +0100 | [diff] [blame] | 29 | #include <asm/idle.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | /* |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 32 | * Smarter SMP flushing macros. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | * c/o Linus Torvalds. |
| 34 | * |
| 35 | * These mean you can really definitely utterly forget about |
| 36 | * writing to user space from interrupts. (Its not allowed anyway). |
| 37 | * |
| 38 | * Optimizations Manfred Spraul <manfred@colorfullife.com> |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 39 | * |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 40 | * More scalable flush, from Andi Kleen |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 41 | * |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 42 | * To avoid global state use 8 different call vectors. |
| 43 | * Each CPU uses a specific vector to trigger flushes on other |
| 44 | * CPUs. Depending on the received vector the target CPUs look into |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 45 | * the right per cpu variable for the flush data. |
| 46 | * |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 47 | * With more than 8 CPUs they are hashed to the 8 available |
| 48 | * vectors. The limited global vector space forces us to this right now. |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 49 | * In future when interrupts are split into per CPU domains this could be |
| 50 | * fixed, at the cost of triggering multiple IPIs in some cases. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | */ |
| 52 | |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 53 | union smp_flush_state { |
| 54 | struct { |
| 55 | cpumask_t flush_cpumask; |
| 56 | struct mm_struct *flush_mm; |
| 57 | unsigned long flush_va; |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 58 | spinlock_t tlbstate_lock; |
| 59 | }; |
| 60 | char pad[SMP_CACHE_BYTES]; |
| 61 | } ____cacheline_aligned; |
| 62 | |
| 63 | /* State is put into the per CPU data section, but padded |
| 64 | to a full cache line because other CPUs can access it and we don't |
| 65 | want false sharing in the per cpu data segment. */ |
| 66 | static DEFINE_PER_CPU(union smp_flush_state, flush_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | /* |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 69 | * We cannot call mmdrop() because we are in interrupt context, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * instead update mm->cpu_vm_mask. |
| 71 | */ |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 72 | static inline void leave_mm(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
| 74 | if (read_pda(mmu_state) == TLBSTATE_OK) |
| 75 | BUG(); |
Brian Gerst | b1fc513 | 2006-03-25 16:31:13 +0100 | [diff] [blame] | 76 | cpu_clear(cpu, read_pda(active_mm)->cpu_vm_mask); |
Linus Torvalds | e3ebadd | 2007-05-07 08:44:24 -0700 | [diff] [blame] | 77 | load_cr3(swapper_pg_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /* |
| 81 | * |
| 82 | * The flush IPI assumes that a thread switch happens in this order: |
| 83 | * [cpu0: the cpu that switches] |
| 84 | * 1) switch_mm() either 1a) or 1b) |
| 85 | * 1a) thread switch to a different mm |
Brian Gerst | b1fc513 | 2006-03-25 16:31:13 +0100 | [diff] [blame] | 86 | * 1a1) cpu_clear(cpu, old_mm->cpu_vm_mask); |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 87 | * Stop ipi delivery for the old mm. This is not synchronized with |
| 88 | * the other cpus, but smp_invalidate_interrupt ignore flush ipis |
| 89 | * for the wrong mm, and in the worst case we perform a superfluous |
| 90 | * tlb flush. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | * 1a2) set cpu mmu_state to TLBSTATE_OK |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 92 | * Now the smp_invalidate_interrupt won't call leave_mm if cpu0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | * was in lazy tlb mode. |
| 94 | * 1a3) update cpu active_mm |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 95 | * Now cpu0 accepts tlb flushes for the new mm. |
Brian Gerst | b1fc513 | 2006-03-25 16:31:13 +0100 | [diff] [blame] | 96 | * 1a4) cpu_set(cpu, new_mm->cpu_vm_mask); |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 97 | * Now the other cpus will send tlb flush ipis. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | * 1a4) change cr3. |
| 99 | * 1b) thread switch without mm change |
| 100 | * cpu active_mm is correct, cpu0 already handles |
| 101 | * flush ipis. |
| 102 | * 1b1) set cpu mmu_state to TLBSTATE_OK |
| 103 | * 1b2) test_and_set the cpu bit in cpu_vm_mask. |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 104 | * Atomically set the bit [other cpus will start sending flush ipis], |
| 105 | * and test the bit. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | * 1b3) if the bit was 0: leave_mm was called, flush the tlb. |
| 107 | * 2) switch %%esp, ie current |
| 108 | * |
| 109 | * The interrupt must handle 2 special cases: |
| 110 | * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm. |
| 111 | * - the cpu performs speculative tlb reads, i.e. even if the cpu only |
| 112 | * runs in kernel space, the cpu could load tlb entries for user space |
| 113 | * pages. |
| 114 | * |
| 115 | * The good news is that cpu mmu_state is local to each cpu, no |
| 116 | * write/read ordering problems. |
| 117 | */ |
| 118 | |
| 119 | /* |
| 120 | * TLB flush IPI: |
| 121 | * |
| 122 | * 1) Flush the tlb entries if the cpu uses the mm that's being flushed. |
| 123 | * 2) Leave the mm if we are in the lazy tlb mode. |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 124 | * |
| 125 | * Interrupts are disabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | */ |
| 127 | |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 128 | asmlinkage void smp_invalidate_interrupt(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 130 | int cpu; |
| 131 | int sender; |
| 132 | union smp_flush_state *f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 134 | cpu = smp_processor_id(); |
| 135 | /* |
Rusty Russell | 19eadf9 | 2006-06-27 02:53:44 -0700 | [diff] [blame] | 136 | * orig_rax contains the negated interrupt vector. |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 137 | * Use that to determine where the sender put the data. |
| 138 | */ |
H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame^] | 139 | sender = ~regs->orig_ax - INVALIDATE_TLB_VECTOR_START; |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 140 | f = &per_cpu(flush_state, sender); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 142 | if (!cpu_isset(cpu, f->flush_cpumask)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | goto out; |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 144 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | * This was a BUG() but until someone can quote me the |
| 146 | * line from the intel manual that guarantees an IPI to |
| 147 | * multiple CPUs is retried _only_ on the erroring CPUs |
| 148 | * its staying as a return |
| 149 | * |
| 150 | * BUG(); |
| 151 | */ |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 152 | |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 153 | if (f->flush_mm == read_pda(active_mm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | if (read_pda(mmu_state) == TLBSTATE_OK) { |
Thomas Gleixner | 0b9c99b | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 155 | if (f->flush_va == TLB_FLUSH_ALL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | local_flush_tlb(); |
| 157 | else |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 158 | __flush_tlb_one(f->flush_va); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } else |
| 160 | leave_mm(cpu); |
| 161 | } |
Andi Kleen | 5df3574 | 2005-07-28 21:15:22 -0700 | [diff] [blame] | 162 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | ack_APIC_irq(); |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 164 | cpu_clear(cpu, f->flush_cpumask); |
Joe Korty | 38e760a | 2007-10-17 18:04:40 +0200 | [diff] [blame] | 165 | add_pda(irq_tlb_count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Thomas Gleixner | 0b9c99b | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 168 | void native_flush_tlb_others(const cpumask_t *cpumaskp, struct mm_struct *mm, |
| 169 | unsigned long va) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 171 | int sender; |
| 172 | union smp_flush_state *f; |
Thomas Gleixner | 0b9c99b | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 173 | cpumask_t cpumask = *cpumaskp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 175 | /* Caller has disabled preemption */ |
| 176 | sender = smp_processor_id() % NUM_INVALIDATE_TLB_VECTORS; |
| 177 | f = &per_cpu(flush_state, sender); |
| 178 | |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 179 | /* |
| 180 | * Could avoid this lock when |
| 181 | * num_online_cpus() <= NUM_INVALIDATE_TLB_VECTORS, but it is |
| 182 | * probably not worth checking this for a cache-hot lock. |
| 183 | */ |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 184 | spin_lock(&f->tlbstate_lock); |
| 185 | |
| 186 | f->flush_mm = mm; |
| 187 | f->flush_va = va; |
| 188 | cpus_or(f->flush_cpumask, cpumask, f->flush_cpumask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * We have to send the IPI only to |
| 192 | * CPUs affected. |
| 193 | */ |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 194 | send_IPI_mask(cpumask, INVALIDATE_TLB_VECTOR_START + sender); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 196 | while (!cpus_empty(f->flush_cpumask)) |
| 197 | cpu_relax(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 199 | f->flush_mm = NULL; |
| 200 | f->flush_va = 0; |
| 201 | spin_unlock(&f->tlbstate_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 203 | |
| 204 | int __cpuinit init_smp_flush(void) |
| 205 | { |
| 206 | int i; |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 207 | |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 208 | for_each_cpu_mask(i, cpu_possible_map) { |
Alexey Dobriyan | 825e037 | 2006-08-05 12:14:34 -0700 | [diff] [blame] | 209 | spin_lock_init(&per_cpu(flush_state, i).tlbstate_lock); |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 210 | } |
| 211 | return 0; |
| 212 | } |
Andi Kleen | e5bc8b6 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 213 | core_initcall(init_smp_flush); |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | void flush_tlb_current_task(void) |
| 216 | { |
| 217 | struct mm_struct *mm = current->mm; |
| 218 | cpumask_t cpu_mask; |
| 219 | |
| 220 | preempt_disable(); |
| 221 | cpu_mask = mm->cpu_vm_mask; |
| 222 | cpu_clear(smp_processor_id(), cpu_mask); |
| 223 | |
| 224 | local_flush_tlb(); |
| 225 | if (!cpus_empty(cpu_mask)) |
Thomas Gleixner | 0b9c99b | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 226 | flush_tlb_others(cpu_mask, mm, TLB_FLUSH_ALL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | preempt_enable(); |
| 228 | } |
| 229 | |
| 230 | void flush_tlb_mm (struct mm_struct * mm) |
| 231 | { |
| 232 | cpumask_t cpu_mask; |
| 233 | |
| 234 | preempt_disable(); |
| 235 | cpu_mask = mm->cpu_vm_mask; |
| 236 | cpu_clear(smp_processor_id(), cpu_mask); |
| 237 | |
| 238 | if (current->active_mm == mm) { |
| 239 | if (current->mm) |
| 240 | local_flush_tlb(); |
| 241 | else |
| 242 | leave_mm(smp_processor_id()); |
| 243 | } |
| 244 | if (!cpus_empty(cpu_mask)) |
Thomas Gleixner | 0b9c99b | 2008-01-30 13:30:35 +0100 | [diff] [blame] | 245 | flush_tlb_others(cpu_mask, mm, TLB_FLUSH_ALL); |
Linus Torvalds | da8f153 | 2007-09-21 12:09:41 -0700 | [diff] [blame] | 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | preempt_enable(); |
| 248 | } |
| 249 | |
| 250 | void flush_tlb_page(struct vm_area_struct * vma, unsigned long va) |
| 251 | { |
| 252 | struct mm_struct *mm = vma->vm_mm; |
| 253 | cpumask_t cpu_mask; |
| 254 | |
| 255 | preempt_disable(); |
| 256 | cpu_mask = mm->cpu_vm_mask; |
| 257 | cpu_clear(smp_processor_id(), cpu_mask); |
| 258 | |
| 259 | if (current->active_mm == mm) { |
| 260 | if(current->mm) |
| 261 | __flush_tlb_one(va); |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 262 | else |
| 263 | leave_mm(smp_processor_id()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | if (!cpus_empty(cpu_mask)) |
| 267 | flush_tlb_others(cpu_mask, mm, va); |
| 268 | |
| 269 | preempt_enable(); |
| 270 | } |
| 271 | |
| 272 | static void do_flush_tlb_all(void* info) |
| 273 | { |
| 274 | unsigned long cpu = smp_processor_id(); |
| 275 | |
| 276 | __flush_tlb_all(); |
| 277 | if (read_pda(mmu_state) == TLBSTATE_LAZY) |
| 278 | leave_mm(cpu); |
| 279 | } |
| 280 | |
| 281 | void flush_tlb_all(void) |
| 282 | { |
| 283 | on_each_cpu(do_flush_tlb_all, NULL, 1, 1); |
| 284 | } |
| 285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | /* |
| 287 | * this function sends a 'reschedule' IPI to another CPU. |
| 288 | * it goes straight through and wastes no time serializing |
| 289 | * anything. Worst case is that we lose a reschedule ... |
| 290 | */ |
| 291 | |
| 292 | void smp_send_reschedule(int cpu) |
| 293 | { |
| 294 | send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR); |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Structure and data for smp_call_function(). This is designed to minimise |
| 299 | * static memory requirements. It also looks cleaner. |
| 300 | */ |
| 301 | static DEFINE_SPINLOCK(call_lock); |
| 302 | |
| 303 | struct call_data_struct { |
| 304 | void (*func) (void *info); |
| 305 | void *info; |
| 306 | atomic_t started; |
| 307 | atomic_t finished; |
| 308 | int wait; |
| 309 | }; |
| 310 | |
| 311 | static struct call_data_struct * call_data; |
| 312 | |
Ashok Raj | 884d9e4 | 2005-06-25 14:55:02 -0700 | [diff] [blame] | 313 | void lock_ipi_call_lock(void) |
| 314 | { |
| 315 | spin_lock_irq(&call_lock); |
| 316 | } |
| 317 | |
| 318 | void unlock_ipi_call_lock(void) |
| 319 | { |
| 320 | spin_unlock_irq(&call_lock); |
| 321 | } |
| 322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | /* |
Laurent Vivier | 66d16ed | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 324 | * this function sends a 'generic call function' IPI to all other CPU |
| 325 | * of the system defined in the mask. |
Eric W. Biederman | 3d483f4 | 2005-07-29 14:03:29 -0700 | [diff] [blame] | 326 | */ |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 327 | static int __smp_call_function_mask(cpumask_t mask, |
| 328 | void (*func)(void *), void *info, |
| 329 | int wait) |
Eric W. Biederman | 3d483f4 | 2005-07-29 14:03:29 -0700 | [diff] [blame] | 330 | { |
| 331 | struct call_data_struct data; |
Laurent Vivier | 66d16ed | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 332 | cpumask_t allbutself; |
| 333 | int cpus; |
| 334 | |
| 335 | allbutself = cpu_online_map; |
| 336 | cpu_clear(smp_processor_id(), allbutself); |
| 337 | |
| 338 | cpus_and(mask, mask, allbutself); |
| 339 | cpus = cpus_weight(mask); |
| 340 | |
| 341 | if (!cpus) |
| 342 | return 0; |
Eric W. Biederman | 3d483f4 | 2005-07-29 14:03:29 -0700 | [diff] [blame] | 343 | |
| 344 | data.func = func; |
| 345 | data.info = info; |
| 346 | atomic_set(&data.started, 0); |
| 347 | data.wait = wait; |
| 348 | if (wait) |
| 349 | atomic_set(&data.finished, 0); |
| 350 | |
| 351 | call_data = &data; |
| 352 | wmb(); |
Laurent Vivier | 66d16ed | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 353 | |
| 354 | /* Send a message to other CPUs */ |
| 355 | if (cpus_equal(mask, allbutself)) |
| 356 | send_IPI_allbutself(CALL_FUNCTION_VECTOR); |
| 357 | else |
| 358 | send_IPI_mask(mask, CALL_FUNCTION_VECTOR); |
Eric W. Biederman | 3d483f4 | 2005-07-29 14:03:29 -0700 | [diff] [blame] | 359 | |
| 360 | /* Wait for response */ |
| 361 | while (atomic_read(&data.started) != cpus) |
| 362 | cpu_relax(); |
| 363 | |
| 364 | if (!wait) |
Laurent Vivier | 66d16ed | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 365 | return 0; |
Eric W. Biederman | 3d483f4 | 2005-07-29 14:03:29 -0700 | [diff] [blame] | 366 | |
| 367 | while (atomic_read(&data.finished) != cpus) |
| 368 | cpu_relax(); |
Laurent Vivier | 66d16ed | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 369 | |
| 370 | return 0; |
Eric W. Biederman | 3d483f4 | 2005-07-29 14:03:29 -0700 | [diff] [blame] | 371 | } |
Laurent Vivier | 66d16ed | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 372 | /** |
| 373 | * smp_call_function_mask(): Run a function on a set of other CPUs. |
| 374 | * @mask: The set of cpus to run on. Must not include the current cpu. |
| 375 | * @func: The function to run. This must be fast and non-blocking. |
| 376 | * @info: An arbitrary pointer to pass to the function. |
| 377 | * @wait: If true, wait (atomically) until function has completed on other CPUs. |
| 378 | * |
| 379 | * Returns 0 on success, else a negative status code. |
| 380 | * |
| 381 | * If @wait is true, then returns once @func has returned; otherwise |
| 382 | * it returns just before the target cpu calls @func. |
| 383 | * |
| 384 | * You must not call this function with disabled interrupts or from a |
| 385 | * hardware interrupt handler or from a bottom half handler. |
| 386 | */ |
| 387 | int smp_call_function_mask(cpumask_t mask, |
| 388 | void (*func)(void *), void *info, |
| 389 | int wait) |
| 390 | { |
| 391 | int ret; |
| 392 | |
| 393 | /* Can deadlock when called with interrupts disabled */ |
| 394 | WARN_ON(irqs_disabled()); |
| 395 | |
| 396 | spin_lock(&call_lock); |
| 397 | ret = __smp_call_function_mask(mask, func, info, wait); |
| 398 | spin_unlock(&call_lock); |
| 399 | return ret; |
| 400 | } |
| 401 | EXPORT_SYMBOL(smp_call_function_mask); |
Eric W. Biederman | 3d483f4 | 2005-07-29 14:03:29 -0700 | [diff] [blame] | 402 | |
| 403 | /* |
Avi Kivity | 4055551 | 2007-07-09 17:11:49 +0300 | [diff] [blame] | 404 | * smp_call_function_single - Run a function on a specific CPU |
Eric W. Biederman | 3d483f4 | 2005-07-29 14:03:29 -0700 | [diff] [blame] | 405 | * @func: The function to run. This must be fast and non-blocking. |
| 406 | * @info: An arbitrary pointer to pass to the function. |
| 407 | * @nonatomic: Currently unused. |
| 408 | * @wait: If true, wait until function has completed on other CPUs. |
| 409 | * |
| 410 | * Retrurns 0 on success, else a negative status code. |
| 411 | * |
| 412 | * Does not return until the remote CPU is nearly ready to execute <func> |
| 413 | * or is or has executed. |
| 414 | */ |
| 415 | |
| 416 | int smp_call_function_single (int cpu, void (*func) (void *info), void *info, |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 417 | int nonatomic, int wait) |
Eric W. Biederman | 3d483f4 | 2005-07-29 14:03:29 -0700 | [diff] [blame] | 418 | { |
| 419 | /* prevent preemption and reschedule on another processor */ |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 420 | int ret, me = get_cpu(); |
Andrew Morton | a38a44c | 2006-12-06 20:38:16 -0800 | [diff] [blame] | 421 | |
| 422 | /* Can deadlock when called with interrupts disabled */ |
| 423 | WARN_ON(irqs_disabled()); |
| 424 | |
Avi Kivity | 4055551 | 2007-07-09 17:11:49 +0300 | [diff] [blame] | 425 | if (cpu == me) { |
| 426 | local_irq_disable(); |
| 427 | func(info); |
| 428 | local_irq_enable(); |
| 429 | put_cpu(); |
| 430 | return 0; |
| 431 | } |
| 432 | |
Laurent Vivier | 66d16ed | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 433 | ret = smp_call_function_mask(cpumask_of_cpu(cpu), func, info, wait); |
| 434 | |
Eric W. Biederman | 3d483f4 | 2005-07-29 14:03:29 -0700 | [diff] [blame] | 435 | put_cpu(); |
Laurent Vivier | 66d16ed | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 436 | return ret; |
Eric W. Biederman | 3d483f4 | 2005-07-29 14:03:29 -0700 | [diff] [blame] | 437 | } |
Andi Kleen | 64a26a73 | 2006-12-07 02:14:19 +0100 | [diff] [blame] | 438 | EXPORT_SYMBOL(smp_call_function_single); |
Eric W. Biederman | 3d483f4 | 2005-07-29 14:03:29 -0700 | [diff] [blame] | 439 | |
| 440 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | * smp_call_function - run a function on all other CPUs. |
| 442 | * @func: The function to run. This must be fast and non-blocking. |
| 443 | * @info: An arbitrary pointer to pass to the function. |
| 444 | * @nonatomic: currently unused. |
| 445 | * @wait: If true, wait (atomically) until function has completed on other |
| 446 | * CPUs. |
| 447 | * |
| 448 | * Returns 0 on success, else a negative status code. Does not return until |
| 449 | * remote CPUs are nearly ready to execute func or are or have executed. |
| 450 | * |
| 451 | * You must not call this function with disabled interrupts or from a |
| 452 | * hardware interrupt handler or from a bottom half handler. |
| 453 | * Actually there are a few legal cases, like panic. |
| 454 | */ |
| 455 | int smp_call_function (void (*func) (void *info), void *info, int nonatomic, |
| 456 | int wait) |
| 457 | { |
Laurent Vivier | 66d16ed | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 458 | return smp_call_function_mask(cpu_online_map, func, info, wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | } |
Andi Kleen | 2ee60e17 | 2006-06-26 13:59:44 +0200 | [diff] [blame] | 460 | EXPORT_SYMBOL(smp_call_function); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
Jan Beulich | 9964cf7 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 462 | static void stop_this_cpu(void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { |
Jan Beulich | 9964cf7 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 464 | local_irq_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | /* |
| 466 | * Remove this CPU: |
| 467 | */ |
| 468 | cpu_clear(smp_processor_id(), cpu_online_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | disable_local_APIC(); |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 470 | for (;;) |
Jan Beulich | 46d13a3 | 2006-06-26 13:57:59 +0200 | [diff] [blame] | 471 | halt(); |
Thomas Gleixner | 16da2f9 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
| 474 | void smp_send_stop(void) |
| 475 | { |
Jan Beulich | 9964cf7 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 476 | int nolock; |
| 477 | unsigned long flags; |
| 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | if (reboot_force) |
| 480 | return; |
Jan Beulich | 9964cf7 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | /* Don't deadlock on the call lock in panic */ |
Jan Beulich | 9964cf7 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 483 | nolock = !spin_trylock(&call_lock); |
| 484 | local_irq_save(flags); |
Laurent Vivier | 66d16ed | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 485 | __smp_call_function_mask(cpu_online_map, stop_this_cpu, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | if (!nolock) |
| 487 | spin_unlock(&call_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | disable_local_APIC(); |
Jan Beulich | 9964cf7 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 489 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Reschedule call back. Nothing to do, |
| 494 | * all the work is done automatically when |
| 495 | * we return from the interrupt. |
| 496 | */ |
| 497 | asmlinkage void smp_reschedule_interrupt(void) |
| 498 | { |
| 499 | ack_APIC_irq(); |
Joe Korty | 38e760a | 2007-10-17 18:04:40 +0200 | [diff] [blame] | 500 | add_pda(irq_resched_count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | asmlinkage void smp_call_function_interrupt(void) |
| 504 | { |
| 505 | void (*func) (void *info) = call_data->func; |
| 506 | void *info = call_data->info; |
| 507 | int wait = call_data->wait; |
| 508 | |
| 509 | ack_APIC_irq(); |
| 510 | /* |
| 511 | * Notify initiating CPU that I've grabbed the data and am |
| 512 | * about to execute the function |
| 513 | */ |
| 514 | mb(); |
| 515 | atomic_inc(&call_data->started); |
| 516 | /* |
| 517 | * At this point the info structure may be out of scope unless wait==1 |
| 518 | */ |
Andi Kleen | 95833c8 | 2006-01-11 22:44:36 +0100 | [diff] [blame] | 519 | exit_idle(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | irq_enter(); |
| 521 | (*func)(info); |
Joe Korty | 38e760a | 2007-10-17 18:04:40 +0200 | [diff] [blame] | 522 | add_pda(irq_call_count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | irq_exit(); |
| 524 | if (wait) { |
| 525 | mb(); |
| 526 | atomic_inc(&call_data->finished); |
| 527 | } |
| 528 | } |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 529 | |