sync hh.org
[hh.org.git] / arch / x86_64 / kernel / smp.c
blob9f74c883568c419909b6542449161d5b109f6b51
1 /*
2 * Intel SMP support routines.
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.
8 * This code is released under the GNU General Public License version 2 or
9 * later.
12 #include <linux/init.h>
14 #include <linux/mm.h>
15 #include <linux/delay.h>
16 #include <linux/spinlock.h>
17 #include <linux/smp_lock.h>
18 #include <linux/smp.h>
19 #include <linux/kernel_stat.h>
20 #include <linux/mc146818rtc.h>
21 #include <linux/interrupt.h>
23 #include <asm/mtrr.h>
24 #include <asm/pgalloc.h>
25 #include <asm/tlbflush.h>
26 #include <asm/mach_apic.h>
27 #include <asm/mmu_context.h>
28 #include <asm/proto.h>
29 #include <asm/apicdef.h>
30 #include <asm/idle.h>
33 * Smarter SMP flushing macros.
34 * c/o Linus Torvalds.
36 * These mean you can really definitely utterly forget about
37 * writing to user space from interrupts. (Its not allowed anyway).
39 * Optimizations Manfred Spraul <manfred@colorfullife.com>
41 * More scalable flush, from Andi Kleen
43 * To avoid global state use 8 different call vectors.
44 * Each CPU uses a specific vector to trigger flushes on other
45 * CPUs. Depending on the received vector the target CPUs look into
46 * the right per cpu variable for the flush data.
48 * With more than 8 CPUs they are hashed to the 8 available
49 * vectors. The limited global vector space forces us to this right now.
50 * In future when interrupts are split into per CPU domains this could be
51 * fixed, at the cost of triggering multiple IPIs in some cases.
54 union smp_flush_state {
55 struct {
56 cpumask_t flush_cpumask;
57 struct mm_struct *flush_mm;
58 unsigned long flush_va;
59 #define FLUSH_ALL -1ULL
60 spinlock_t tlbstate_lock;
62 char pad[SMP_CACHE_BYTES];
63 } ____cacheline_aligned;
65 /* State is put into the per CPU data section, but padded
66 to a full cache line because other CPUs can access it and we don't
67 want false sharing in the per cpu data segment. */
68 static DEFINE_PER_CPU(union smp_flush_state, flush_state);
71 * We cannot call mmdrop() because we are in interrupt context,
72 * instead update mm->cpu_vm_mask.
74 static inline void leave_mm(int cpu)
76 if (read_pda(mmu_state) == TLBSTATE_OK)
77 BUG();
78 cpu_clear(cpu, read_pda(active_mm)->cpu_vm_mask);
79 load_cr3(swapper_pg_dir);
84 * The flush IPI assumes that a thread switch happens in this order:
85 * [cpu0: the cpu that switches]
86 * 1) switch_mm() either 1a) or 1b)
87 * 1a) thread switch to a different mm
88 * 1a1) cpu_clear(cpu, old_mm->cpu_vm_mask);
89 * Stop ipi delivery for the old mm. This is not synchronized with
90 * the other cpus, but smp_invalidate_interrupt ignore flush ipis
91 * for the wrong mm, and in the worst case we perform a superfluous
92 * tlb flush.
93 * 1a2) set cpu mmu_state to TLBSTATE_OK
94 * Now the smp_invalidate_interrupt won't call leave_mm if cpu0
95 * was in lazy tlb mode.
96 * 1a3) update cpu active_mm
97 * Now cpu0 accepts tlb flushes for the new mm.
98 * 1a4) cpu_set(cpu, new_mm->cpu_vm_mask);
99 * Now the other cpus will send tlb flush ipis.
100 * 1a4) change cr3.
101 * 1b) thread switch without mm change
102 * cpu active_mm is correct, cpu0 already handles
103 * flush ipis.
104 * 1b1) set cpu mmu_state to TLBSTATE_OK
105 * 1b2) test_and_set the cpu bit in cpu_vm_mask.
106 * Atomically set the bit [other cpus will start sending flush ipis],
107 * and test the bit.
108 * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
109 * 2) switch %%esp, ie current
111 * The interrupt must handle 2 special cases:
112 * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
113 * - the cpu performs speculative tlb reads, i.e. even if the cpu only
114 * runs in kernel space, the cpu could load tlb entries for user space
115 * pages.
117 * The good news is that cpu mmu_state is local to each cpu, no
118 * write/read ordering problems.
122 * TLB flush IPI:
124 * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
125 * 2) Leave the mm if we are in the lazy tlb mode.
127 * Interrupts are disabled.
130 asmlinkage void smp_invalidate_interrupt(struct pt_regs *regs)
132 int cpu;
133 int sender;
134 union smp_flush_state *f;
136 cpu = smp_processor_id();
138 * orig_rax contains the negated interrupt vector.
139 * Use that to determine where the sender put the data.
141 sender = ~regs->orig_rax - INVALIDATE_TLB_VECTOR_START;
142 f = &per_cpu(flush_state, sender);
144 if (!cpu_isset(cpu, f->flush_cpumask))
145 goto out;
147 * This was a BUG() but until someone can quote me the
148 * line from the intel manual that guarantees an IPI to
149 * multiple CPUs is retried _only_ on the erroring CPUs
150 * its staying as a return
152 * BUG();
155 if (f->flush_mm == read_pda(active_mm)) {
156 if (read_pda(mmu_state) == TLBSTATE_OK) {
157 if (f->flush_va == FLUSH_ALL)
158 local_flush_tlb();
159 else
160 __flush_tlb_one(f->flush_va);
161 } else
162 leave_mm(cpu);
164 out:
165 ack_APIC_irq();
166 cpu_clear(cpu, f->flush_cpumask);
169 static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
170 unsigned long va)
172 int sender;
173 union smp_flush_state *f;
175 /* Caller has disabled preemption */
176 sender = smp_processor_id() % NUM_INVALIDATE_TLB_VECTORS;
177 f = &per_cpu(flush_state, sender);
179 /* Could avoid this lock when
180 num_online_cpus() <= NUM_INVALIDATE_TLB_VECTORS, but it is
181 probably not worth checking this for a cache-hot lock. */
182 spin_lock(&f->tlbstate_lock);
184 f->flush_mm = mm;
185 f->flush_va = va;
186 cpus_or(f->flush_cpumask, cpumask, f->flush_cpumask);
189 * We have to send the IPI only to
190 * CPUs affected.
192 send_IPI_mask(cpumask, INVALIDATE_TLB_VECTOR_START + sender);
194 while (!cpus_empty(f->flush_cpumask))
195 cpu_relax();
197 f->flush_mm = NULL;
198 f->flush_va = 0;
199 spin_unlock(&f->tlbstate_lock);
202 int __cpuinit init_smp_flush(void)
204 int i;
205 for_each_cpu_mask(i, cpu_possible_map) {
206 spin_lock_init(&per_cpu(flush_state, i).tlbstate_lock);
208 return 0;
211 core_initcall(init_smp_flush);
213 void flush_tlb_current_task(void)
215 struct mm_struct *mm = current->mm;
216 cpumask_t cpu_mask;
218 preempt_disable();
219 cpu_mask = mm->cpu_vm_mask;
220 cpu_clear(smp_processor_id(), cpu_mask);
222 local_flush_tlb();
223 if (!cpus_empty(cpu_mask))
224 flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
225 preempt_enable();
227 EXPORT_SYMBOL(flush_tlb_current_task);
229 void flush_tlb_mm (struct mm_struct * mm)
231 cpumask_t cpu_mask;
233 preempt_disable();
234 cpu_mask = mm->cpu_vm_mask;
235 cpu_clear(smp_processor_id(), cpu_mask);
237 if (current->active_mm == mm) {
238 if (current->mm)
239 local_flush_tlb();
240 else
241 leave_mm(smp_processor_id());
243 if (!cpus_empty(cpu_mask))
244 flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
246 preempt_enable();
248 EXPORT_SYMBOL(flush_tlb_mm);
250 void flush_tlb_page(struct vm_area_struct * vma, unsigned long va)
252 struct mm_struct *mm = vma->vm_mm;
253 cpumask_t cpu_mask;
255 preempt_disable();
256 cpu_mask = mm->cpu_vm_mask;
257 cpu_clear(smp_processor_id(), cpu_mask);
259 if (current->active_mm == mm) {
260 if(current->mm)
261 __flush_tlb_one(va);
262 else
263 leave_mm(smp_processor_id());
266 if (!cpus_empty(cpu_mask))
267 flush_tlb_others(cpu_mask, mm, va);
269 preempt_enable();
271 EXPORT_SYMBOL(flush_tlb_page);
273 static void do_flush_tlb_all(void* info)
275 unsigned long cpu = smp_processor_id();
277 __flush_tlb_all();
278 if (read_pda(mmu_state) == TLBSTATE_LAZY)
279 leave_mm(cpu);
282 void flush_tlb_all(void)
284 on_each_cpu(do_flush_tlb_all, NULL, 1, 1);
288 * this function sends a 'reschedule' IPI to another CPU.
289 * it goes straight through and wastes no time serializing
290 * anything. Worst case is that we lose a reschedule ...
293 void smp_send_reschedule(int cpu)
295 send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR);
299 * Structure and data for smp_call_function(). This is designed to minimise
300 * static memory requirements. It also looks cleaner.
302 static DEFINE_SPINLOCK(call_lock);
304 struct call_data_struct {
305 void (*func) (void *info);
306 void *info;
307 atomic_t started;
308 atomic_t finished;
309 int wait;
312 static struct call_data_struct * call_data;
314 void lock_ipi_call_lock(void)
316 spin_lock_irq(&call_lock);
319 void unlock_ipi_call_lock(void)
321 spin_unlock_irq(&call_lock);
325 * this function sends a 'generic call function' IPI to one other CPU
326 * in the system.
328 * cpu is a standard Linux logical CPU number.
330 static void
331 __smp_call_function_single(int cpu, void (*func) (void *info), void *info,
332 int nonatomic, int wait)
334 struct call_data_struct data;
335 int cpus = 1;
337 data.func = func;
338 data.info = info;
339 atomic_set(&data.started, 0);
340 data.wait = wait;
341 if (wait)
342 atomic_set(&data.finished, 0);
344 call_data = &data;
345 wmb();
346 /* Send a message to all other CPUs and wait for them to respond */
347 send_IPI_mask(cpumask_of_cpu(cpu), CALL_FUNCTION_VECTOR);
349 /* Wait for response */
350 while (atomic_read(&data.started) != cpus)
351 cpu_relax();
353 if (!wait)
354 return;
356 while (atomic_read(&data.finished) != cpus)
357 cpu_relax();
361 * smp_call_function_single - Run a function on another CPU
362 * @func: The function to run. This must be fast and non-blocking.
363 * @info: An arbitrary pointer to pass to the function.
364 * @nonatomic: Currently unused.
365 * @wait: If true, wait until function has completed on other CPUs.
367 * Retrurns 0 on success, else a negative status code.
369 * Does not return until the remote CPU is nearly ready to execute <func>
370 * or is or has executed.
373 int smp_call_function_single (int cpu, void (*func) (void *info), void *info,
374 int nonatomic, int wait)
376 /* prevent preemption and reschedule on another processor */
377 int me = get_cpu();
378 if (cpu == me) {
379 put_cpu();
380 return 0;
382 spin_lock_bh(&call_lock);
383 __smp_call_function_single(cpu, func, info, nonatomic, wait);
384 spin_unlock_bh(&call_lock);
385 put_cpu();
386 return 0;
390 * this function sends a 'generic call function' IPI to all other CPUs
391 * in the system.
393 static void __smp_call_function (void (*func) (void *info), void *info,
394 int nonatomic, int wait)
396 struct call_data_struct data;
397 int cpus = num_online_cpus()-1;
399 if (!cpus)
400 return;
402 data.func = func;
403 data.info = info;
404 atomic_set(&data.started, 0);
405 data.wait = wait;
406 if (wait)
407 atomic_set(&data.finished, 0);
409 call_data = &data;
410 wmb();
411 /* Send a message to all other CPUs and wait for them to respond */
412 send_IPI_allbutself(CALL_FUNCTION_VECTOR);
414 /* Wait for response */
415 while (atomic_read(&data.started) != cpus)
416 cpu_relax();
418 if (!wait)
419 return;
421 while (atomic_read(&data.finished) != cpus)
422 cpu_relax();
426 * smp_call_function - run a function on all other CPUs.
427 * @func: The function to run. This must be fast and non-blocking.
428 * @info: An arbitrary pointer to pass to the function.
429 * @nonatomic: currently unused.
430 * @wait: If true, wait (atomically) until function has completed on other
431 * CPUs.
433 * Returns 0 on success, else a negative status code. Does not return until
434 * remote CPUs are nearly ready to execute func or are or have executed.
436 * You must not call this function with disabled interrupts or from a
437 * hardware interrupt handler or from a bottom half handler.
438 * Actually there are a few legal cases, like panic.
440 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
441 int wait)
443 spin_lock(&call_lock);
444 __smp_call_function(func,info,nonatomic,wait);
445 spin_unlock(&call_lock);
446 return 0;
448 EXPORT_SYMBOL(smp_call_function);
450 void smp_stop_cpu(void)
452 unsigned long flags;
454 * Remove this CPU:
456 cpu_clear(smp_processor_id(), cpu_online_map);
457 local_irq_save(flags);
458 disable_local_APIC();
459 local_irq_restore(flags);
462 static void smp_really_stop_cpu(void *dummy)
464 smp_stop_cpu();
465 for (;;)
466 halt();
469 void smp_send_stop(void)
471 int nolock = 0;
472 if (reboot_force)
473 return;
474 /* Don't deadlock on the call lock in panic */
475 if (!spin_trylock(&call_lock)) {
476 /* ignore locking because we have panicked anyways */
477 nolock = 1;
479 __smp_call_function(smp_really_stop_cpu, NULL, 0, 0);
480 if (!nolock)
481 spin_unlock(&call_lock);
483 local_irq_disable();
484 disable_local_APIC();
485 local_irq_enable();
489 * Reschedule call back. Nothing to do,
490 * all the work is done automatically when
491 * we return from the interrupt.
493 asmlinkage void smp_reschedule_interrupt(void)
495 ack_APIC_irq();
498 asmlinkage void smp_call_function_interrupt(void)
500 void (*func) (void *info) = call_data->func;
501 void *info = call_data->info;
502 int wait = call_data->wait;
504 ack_APIC_irq();
506 * Notify initiating CPU that I've grabbed the data and am
507 * about to execute the function
509 mb();
510 atomic_inc(&call_data->started);
512 * At this point the info structure may be out of scope unless wait==1
514 exit_idle();
515 irq_enter();
516 (*func)(info);
517 irq_exit();
518 if (wait) {
519 mb();
520 atomic_inc(&call_data->finished);