[PATCH] x86-64: Make remote TLB flush more scalable
[linux/fpc-iii.git] / arch / x86_64 / kernel / smp.c
blob801db885955cadff1ae21d03a7dc404efa1b14c8
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/irq.h>
16 #include <linux/delay.h>
17 #include <linux/spinlock.h>
18 #include <linux/smp_lock.h>
19 #include <linux/smp.h>
20 #include <linux/kernel_stat.h>
21 #include <linux/mc146818rtc.h>
22 #include <linux/interrupt.h>
24 #include <asm/mtrr.h>
25 #include <asm/pgalloc.h>
26 #include <asm/tlbflush.h>
27 #include <asm/mach_apic.h>
28 #include <asm/mmu_context.h>
29 #include <asm/proto.h>
30 #include <asm/apicdef.h>
32 #define __cpuinit __init
35 * Smarter SMP flushing macros.
36 * c/o Linus Torvalds.
38 * These mean you can really definitely utterly forget about
39 * writing to user space from interrupts. (Its not allowed anyway).
41 * Optimizations Manfred Spraul <manfred@colorfullife.com>
43 * More scalable flush, from Andi Kleen
45 * To avoid global state use 8 different call vectors.
46 * Each CPU uses a specific vector to trigger flushes on other
47 * CPUs. Depending on the received vector the target CPUs look into
48 * the right per cpu variable for the flush data.
50 * With more than 8 CPUs they are hashed to the 8 available
51 * vectors. The limited global vector space forces us to this right now.
52 * In future when interrupts are split into per CPU domains this could be
53 * fixed, at the cost of triggering multiple IPIs in some cases.
56 union smp_flush_state {
57 struct {
58 cpumask_t flush_cpumask;
59 struct mm_struct *flush_mm;
60 unsigned long flush_va;
61 #define FLUSH_ALL -1ULL
62 spinlock_t tlbstate_lock;
64 char pad[SMP_CACHE_BYTES];
65 } ____cacheline_aligned;
67 /* State is put into the per CPU data section, but padded
68 to a full cache line because other CPUs can access it and we don't
69 want false sharing in the per cpu data segment. */
70 static DEFINE_PER_CPU(union smp_flush_state, flush_state);
73 * We cannot call mmdrop() because we are in interrupt context,
74 * instead update mm->cpu_vm_mask.
76 static inline void leave_mm(int cpu)
78 if (read_pda(mmu_state) == TLBSTATE_OK)
79 BUG();
80 clear_bit(cpu, &read_pda(active_mm)->cpu_vm_mask);
81 load_cr3(swapper_pg_dir);
86 * The flush IPI assumes that a thread switch happens in this order:
87 * [cpu0: the cpu that switches]
88 * 1) switch_mm() either 1a) or 1b)
89 * 1a) thread switch to a different mm
90 * 1a1) clear_bit(cpu, &old_mm->cpu_vm_mask);
91 * Stop ipi delivery for the old mm. This is not synchronized with
92 * the other cpus, but smp_invalidate_interrupt ignore flush ipis
93 * for the wrong mm, and in the worst case we perform a superfluous
94 * tlb flush.
95 * 1a2) set cpu mmu_state to TLBSTATE_OK
96 * Now the smp_invalidate_interrupt won't call leave_mm if cpu0
97 * was in lazy tlb mode.
98 * 1a3) update cpu active_mm
99 * Now cpu0 accepts tlb flushes for the new mm.
100 * 1a4) set_bit(cpu, &new_mm->cpu_vm_mask);
101 * Now the other cpus will send tlb flush ipis.
102 * 1a4) change cr3.
103 * 1b) thread switch without mm change
104 * cpu active_mm is correct, cpu0 already handles
105 * flush ipis.
106 * 1b1) set cpu mmu_state to TLBSTATE_OK
107 * 1b2) test_and_set the cpu bit in cpu_vm_mask.
108 * Atomically set the bit [other cpus will start sending flush ipis],
109 * and test the bit.
110 * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
111 * 2) switch %%esp, ie current
113 * The interrupt must handle 2 special cases:
114 * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
115 * - the cpu performs speculative tlb reads, i.e. even if the cpu only
116 * runs in kernel space, the cpu could load tlb entries for user space
117 * pages.
119 * The good news is that cpu mmu_state is local to each cpu, no
120 * write/read ordering problems.
124 * TLB flush IPI:
126 * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
127 * 2) Leave the mm if we are in the lazy tlb mode.
129 * Interrupts are disabled.
132 asmlinkage void smp_invalidate_interrupt(struct pt_regs *regs)
134 int cpu;
135 int sender;
136 union smp_flush_state *f;
138 cpu = smp_processor_id();
140 * orig_rax contains the interrupt vector - 256.
141 * Use that to determine where the sender put the data.
143 sender = regs->orig_rax + 256 - INVALIDATE_TLB_VECTOR_START;
144 f = &per_cpu(flush_state, sender);
146 if (!cpu_isset(cpu, f->flush_cpumask))
147 goto out;
149 * This was a BUG() but until someone can quote me the
150 * line from the intel manual that guarantees an IPI to
151 * multiple CPUs is retried _only_ on the erroring CPUs
152 * its staying as a return
154 * BUG();
157 if (f->flush_mm == read_pda(active_mm)) {
158 if (read_pda(mmu_state) == TLBSTATE_OK) {
159 if (f->flush_va == FLUSH_ALL)
160 local_flush_tlb();
161 else
162 __flush_tlb_one(f->flush_va);
163 } else
164 leave_mm(cpu);
166 out:
167 ack_APIC_irq();
168 cpu_clear(cpu, f->flush_cpumask);
171 static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
172 unsigned long va)
174 int sender;
175 union smp_flush_state *f;
177 /* Caller has disabled preemption */
178 sender = smp_processor_id() % NUM_INVALIDATE_TLB_VECTORS;
179 f = &per_cpu(flush_state, sender);
181 /* Could avoid this lock when
182 num_online_cpus() <= NUM_INVALIDATE_TLB_VECTORS, but it is
183 probably not worth checking this for a cache-hot lock. */
184 spin_lock(&f->tlbstate_lock);
186 f->flush_mm = mm;
187 f->flush_va = va;
188 cpus_or(f->flush_cpumask, cpumask, f->flush_cpumask);
191 * We have to send the IPI only to
192 * CPUs affected.
194 send_IPI_mask(cpumask, INVALIDATE_TLB_VECTOR_START + sender);
196 while (!cpus_empty(f->flush_cpumask))
197 cpu_relax();
199 f->flush_mm = NULL;
200 f->flush_va = 0;
201 spin_unlock(&f->tlbstate_lock);
204 int __cpuinit init_smp_flush(void)
206 int i;
207 for_each_cpu_mask(i, cpu_possible_map) {
208 spin_lock_init(&per_cpu(flush_state.tlbstate_lock, i));
210 return 0;
213 core_initcall(init_smp_flush);
215 void flush_tlb_current_task(void)
217 struct mm_struct *mm = current->mm;
218 cpumask_t cpu_mask;
220 preempt_disable();
221 cpu_mask = mm->cpu_vm_mask;
222 cpu_clear(smp_processor_id(), cpu_mask);
224 local_flush_tlb();
225 if (!cpus_empty(cpu_mask))
226 flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
227 preempt_enable();
230 void flush_tlb_mm (struct mm_struct * mm)
232 cpumask_t cpu_mask;
234 preempt_disable();
235 cpu_mask = mm->cpu_vm_mask;
236 cpu_clear(smp_processor_id(), cpu_mask);
238 if (current->active_mm == mm) {
239 if (current->mm)
240 local_flush_tlb();
241 else
242 leave_mm(smp_processor_id());
244 if (!cpus_empty(cpu_mask))
245 flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
247 preempt_enable();
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();
272 static void do_flush_tlb_all(void* info)
274 unsigned long cpu = smp_processor_id();
276 __flush_tlb_all();
277 if (read_pda(mmu_state) == TLBSTATE_LAZY)
278 leave_mm(cpu);
281 void flush_tlb_all(void)
283 on_each_cpu(do_flush_tlb_all, NULL, 1, 1);
286 void smp_kdb_stop(void)
288 send_IPI_allbutself(KDB_VECTOR);
292 * this function sends a 'reschedule' IPI to another CPU.
293 * it goes straight through and wastes no time serializing
294 * anything. Worst case is that we lose a reschedule ...
297 void smp_send_reschedule(int cpu)
299 send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR);
303 * Structure and data for smp_call_function(). This is designed to minimise
304 * static memory requirements. It also looks cleaner.
306 static DEFINE_SPINLOCK(call_lock);
308 struct call_data_struct {
309 void (*func) (void *info);
310 void *info;
311 atomic_t started;
312 atomic_t finished;
313 int wait;
316 static struct call_data_struct * call_data;
318 void lock_ipi_call_lock(void)
320 spin_lock_irq(&call_lock);
323 void unlock_ipi_call_lock(void)
325 spin_unlock_irq(&call_lock);
329 * this function sends a 'generic call function' IPI to one other CPU
330 * in the system.
332 * cpu is a standard Linux logical CPU number.
334 static void
335 __smp_call_function_single(int cpu, void (*func) (void *info), void *info,
336 int nonatomic, int wait)
338 struct call_data_struct data;
339 int cpus = 1;
341 data.func = func;
342 data.info = info;
343 atomic_set(&data.started, 0);
344 data.wait = wait;
345 if (wait)
346 atomic_set(&data.finished, 0);
348 call_data = &data;
349 wmb();
350 /* Send a message to all other CPUs and wait for them to respond */
351 send_IPI_mask(cpumask_of_cpu(cpu), CALL_FUNCTION_VECTOR);
353 /* Wait for response */
354 while (atomic_read(&data.started) != cpus)
355 cpu_relax();
357 if (!wait)
358 return;
360 while (atomic_read(&data.finished) != cpus)
361 cpu_relax();
365 * smp_call_function_single - Run a function on another CPU
366 * @func: The function to run. This must be fast and non-blocking.
367 * @info: An arbitrary pointer to pass to the function.
368 * @nonatomic: Currently unused.
369 * @wait: If true, wait until function has completed on other CPUs.
371 * Retrurns 0 on success, else a negative status code.
373 * Does not return until the remote CPU is nearly ready to execute <func>
374 * or is or has executed.
377 int smp_call_function_single (int cpu, void (*func) (void *info), void *info,
378 int nonatomic, int wait)
380 /* prevent preemption and reschedule on another processor */
381 int me = get_cpu();
382 if (cpu == me) {
383 WARN_ON(1);
384 put_cpu();
385 return -EBUSY;
387 spin_lock_bh(&call_lock);
388 __smp_call_function_single(cpu, func, info, nonatomic, wait);
389 spin_unlock_bh(&call_lock);
390 put_cpu();
391 return 0;
395 * this function sends a 'generic call function' IPI to all other CPUs
396 * in the system.
398 static void __smp_call_function (void (*func) (void *info), void *info,
399 int nonatomic, int wait)
401 struct call_data_struct data;
402 int cpus = num_online_cpus()-1;
404 if (!cpus)
405 return;
407 data.func = func;
408 data.info = info;
409 atomic_set(&data.started, 0);
410 data.wait = wait;
411 if (wait)
412 atomic_set(&data.finished, 0);
414 call_data = &data;
415 wmb();
416 /* Send a message to all other CPUs and wait for them to respond */
417 send_IPI_allbutself(CALL_FUNCTION_VECTOR);
419 /* Wait for response */
420 while (atomic_read(&data.started) != cpus)
421 cpu_relax();
423 if (!wait)
424 return;
426 while (atomic_read(&data.finished) != cpus)
427 cpu_relax();
431 * smp_call_function - run a function on all other CPUs.
432 * @func: The function to run. This must be fast and non-blocking.
433 * @info: An arbitrary pointer to pass to the function.
434 * @nonatomic: currently unused.
435 * @wait: If true, wait (atomically) until function has completed on other
436 * CPUs.
438 * Returns 0 on success, else a negative status code. Does not return until
439 * remote CPUs are nearly ready to execute func or are or have executed.
441 * You must not call this function with disabled interrupts or from a
442 * hardware interrupt handler or from a bottom half handler.
443 * Actually there are a few legal cases, like panic.
445 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
446 int wait)
448 spin_lock(&call_lock);
449 __smp_call_function(func,info,nonatomic,wait);
450 spin_unlock(&call_lock);
451 return 0;
454 void smp_stop_cpu(void)
457 * Remove this CPU:
459 cpu_clear(smp_processor_id(), cpu_online_map);
460 local_irq_disable();
461 disable_local_APIC();
462 local_irq_enable();
465 static void smp_really_stop_cpu(void *dummy)
467 smp_stop_cpu();
468 for (;;)
469 asm("hlt");
472 void smp_send_stop(void)
474 int nolock = 0;
475 if (reboot_force)
476 return;
477 /* Don't deadlock on the call lock in panic */
478 if (!spin_trylock(&call_lock)) {
479 /* ignore locking because we have paniced anyways */
480 nolock = 1;
482 __smp_call_function(smp_really_stop_cpu, NULL, 0, 0);
483 if (!nolock)
484 spin_unlock(&call_lock);
486 local_irq_disable();
487 disable_local_APIC();
488 local_irq_enable();
492 * Reschedule call back. Nothing to do,
493 * all the work is done automatically when
494 * we return from the interrupt.
496 asmlinkage void smp_reschedule_interrupt(void)
498 ack_APIC_irq();
501 asmlinkage void smp_call_function_interrupt(void)
503 void (*func) (void *info) = call_data->func;
504 void *info = call_data->info;
505 int wait = call_data->wait;
507 ack_APIC_irq();
509 * Notify initiating CPU that I've grabbed the data and am
510 * about to execute the function
512 mb();
513 atomic_inc(&call_data->started);
515 * At this point the info structure may be out of scope unless wait==1
517 irq_enter();
518 (*func)(info);
519 irq_exit();
520 if (wait) {
521 mb();
522 atomic_inc(&call_data->finished);
526 int safe_smp_processor_id(void)
528 int apicid, i;
530 if (disable_apic)
531 return 0;
533 apicid = hard_smp_processor_id();
534 if (x86_cpu_to_apicid[apicid] == apicid)
535 return apicid;
537 for (i = 0; i < NR_CPUS; ++i) {
538 if (x86_cpu_to_apicid[i] == apicid)
539 return i;
542 /* No entries in x86_cpu_to_apicid? Either no MPS|ACPI,
543 * or called too early. Either way, we must be CPU 0. */
544 if (x86_cpu_to_apicid[0] == BAD_APICID)
545 return 0;
547 return 0; /* Should not happen */