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
12 #include <linux/init.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>
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>
32 * Smarter SMP flushing macros.
35 * These mean you can really definitely utterly forget about
36 * writing to user space from interrupts. (Its not allowed anyway).
38 * Optimizations Manfred Spraul <manfred@colorfullife.com>
40 * More scalable flush, from Andi Kleen
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
45 * the right per cpu variable for the flush data.
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.
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.
53 union smp_flush_state {
55 cpumask_t flush_cpumask;
56 struct mm_struct *flush_mm;
57 unsigned long flush_va;
58 #define FLUSH_ALL -1ULL
59 spinlock_t tlbstate_lock;
61 char pad[SMP_CACHE_BYTES];
62 } ____cacheline_aligned;
64 /* State is put into the per CPU data section, but padded
65 to a full cache line because other CPUs can access it and we don't
66 want false sharing in the per cpu data segment. */
67 static DEFINE_PER_CPU(union smp_flush_state, flush_state);
70 * We cannot call mmdrop() because we are in interrupt context,
71 * instead update mm->cpu_vm_mask.
73 static inline void leave_mm(int cpu)
75 if (read_pda(mmu_state) == TLBSTATE_OK)
77 clear_bit(cpu, &read_pda(active_mm)->cpu_vm_mask);
78 load_cr3(swapper_pg_dir);
83 * The flush IPI assumes that a thread switch happens in this order:
84 * [cpu0: the cpu that switches]
85 * 1) switch_mm() either 1a) or 1b)
86 * 1a) thread switch to a different mm
87 * 1a1) clear_bit(cpu, &old_mm->cpu_vm_mask);
88 * Stop ipi delivery for the old mm. This is not synchronized with
89 * the other cpus, but smp_invalidate_interrupt ignore flush ipis
90 * for the wrong mm, and in the worst case we perform a superfluous
92 * 1a2) set cpu mmu_state to TLBSTATE_OK
93 * Now the smp_invalidate_interrupt won't call leave_mm if cpu0
94 * was in lazy tlb mode.
95 * 1a3) update cpu active_mm
96 * Now cpu0 accepts tlb flushes for the new mm.
97 * 1a4) set_bit(cpu, &new_mm->cpu_vm_mask);
98 * Now the other cpus will send tlb flush ipis.
100 * 1b) thread switch without mm change
101 * cpu active_mm is correct, cpu0 already handles
103 * 1b1) set cpu mmu_state to TLBSTATE_OK
104 * 1b2) test_and_set the cpu bit in cpu_vm_mask.
105 * Atomically set the bit [other cpus will start sending flush ipis],
107 * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
108 * 2) switch %%esp, ie current
110 * The interrupt must handle 2 special cases:
111 * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
112 * - the cpu performs speculative tlb reads, i.e. even if the cpu only
113 * runs in kernel space, the cpu could load tlb entries for user space
116 * The good news is that cpu mmu_state is local to each cpu, no
117 * write/read ordering problems.
123 * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
124 * 2) Leave the mm if we are in the lazy tlb mode.
126 * Interrupts are disabled.
129 asmlinkage void smp_invalidate_interrupt(struct pt_regs *regs)
133 union smp_flush_state *f;
135 cpu = smp_processor_id();
137 * orig_rax contains the interrupt vector - 256.
138 * Use that to determine where the sender put the data.
140 sender = regs->orig_rax + 256 - INVALIDATE_TLB_VECTOR_START;
141 f = &per_cpu(flush_state, sender);
143 if (!cpu_isset(cpu, f->flush_cpumask))
146 * This was a BUG() but until someone can quote me the
147 * line from the intel manual that guarantees an IPI to
148 * multiple CPUs is retried _only_ on the erroring CPUs
149 * its staying as a return
154 if (f->flush_mm == read_pda(active_mm)) {
155 if (read_pda(mmu_state) == TLBSTATE_OK) {
156 if (f->flush_va == FLUSH_ALL)
159 __flush_tlb_one(f->flush_va);
165 cpu_clear(cpu, f->flush_cpumask);
168 static void flush_tlb_others(cpumask_t cpumask, struct mm_struct *mm,
172 union smp_flush_state *f;
174 /* Caller has disabled preemption */
175 sender = smp_processor_id() % NUM_INVALIDATE_TLB_VECTORS;
176 f = &per_cpu(flush_state, sender);
178 /* Could avoid this lock when
179 num_online_cpus() <= NUM_INVALIDATE_TLB_VECTORS, but it is
180 probably not worth checking this for a cache-hot lock. */
181 spin_lock(&f->tlbstate_lock);
185 cpus_or(f->flush_cpumask, cpumask, f->flush_cpumask);
188 * We have to send the IPI only to
191 send_IPI_mask(cpumask, INVALIDATE_TLB_VECTOR_START + sender);
193 while (!cpus_empty(f->flush_cpumask))
198 spin_unlock(&f->tlbstate_lock);
201 int __cpuinit init_smp_flush(void)
204 for_each_cpu_mask(i, cpu_possible_map) {
205 spin_lock_init(&per_cpu(flush_state.tlbstate_lock, i));
210 core_initcall(init_smp_flush);
212 void flush_tlb_current_task(void)
214 struct mm_struct *mm = current->mm;
218 cpu_mask = mm->cpu_vm_mask;
219 cpu_clear(smp_processor_id(), cpu_mask);
222 if (!cpus_empty(cpu_mask))
223 flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
227 void flush_tlb_mm (struct mm_struct * mm)
232 cpu_mask = mm->cpu_vm_mask;
233 cpu_clear(smp_processor_id(), cpu_mask);
235 if (current->active_mm == mm) {
239 leave_mm(smp_processor_id());
241 if (!cpus_empty(cpu_mask))
242 flush_tlb_others(cpu_mask, mm, FLUSH_ALL);
247 void flush_tlb_page(struct vm_area_struct * vma, unsigned long va)
249 struct mm_struct *mm = vma->vm_mm;
253 cpu_mask = mm->cpu_vm_mask;
254 cpu_clear(smp_processor_id(), cpu_mask);
256 if (current->active_mm == mm) {
260 leave_mm(smp_processor_id());
263 if (!cpus_empty(cpu_mask))
264 flush_tlb_others(cpu_mask, mm, va);
269 static void do_flush_tlb_all(void* info)
271 unsigned long cpu = smp_processor_id();
274 if (read_pda(mmu_state) == TLBSTATE_LAZY)
278 void flush_tlb_all(void)
280 on_each_cpu(do_flush_tlb_all, NULL, 1, 1);
283 void smp_kdb_stop(void)
285 send_IPI_allbutself(KDB_VECTOR);
289 * this function sends a 'reschedule' IPI to another CPU.
290 * it goes straight through and wastes no time serializing
291 * anything. Worst case is that we lose a reschedule ...
294 void smp_send_reschedule(int cpu)
296 send_IPI_mask(cpumask_of_cpu(cpu), RESCHEDULE_VECTOR);
300 * Structure and data for smp_call_function(). This is designed to minimise
301 * static memory requirements. It also looks cleaner.
303 static DEFINE_SPINLOCK(call_lock);
305 struct call_data_struct {
306 void (*func) (void *info);
313 static struct call_data_struct * call_data;
315 void lock_ipi_call_lock(void)
317 spin_lock_irq(&call_lock);
320 void unlock_ipi_call_lock(void)
322 spin_unlock_irq(&call_lock);
326 * this function sends a 'generic call function' IPI to one other CPU
329 * cpu is a standard Linux logical CPU number.
332 __smp_call_function_single(int cpu, void (*func) (void *info), void *info,
333 int nonatomic, int wait)
335 struct call_data_struct data;
340 atomic_set(&data.started, 0);
343 atomic_set(&data.finished, 0);
347 /* Send a message to all other CPUs and wait for them to respond */
348 send_IPI_mask(cpumask_of_cpu(cpu), CALL_FUNCTION_VECTOR);
350 /* Wait for response */
351 while (atomic_read(&data.started) != cpus)
357 while (atomic_read(&data.finished) != cpus)
362 * smp_call_function_single - Run a function on another CPU
363 * @func: The function to run. This must be fast and non-blocking.
364 * @info: An arbitrary pointer to pass to the function.
365 * @nonatomic: Currently unused.
366 * @wait: If true, wait until function has completed on other CPUs.
368 * Retrurns 0 on success, else a negative status code.
370 * Does not return until the remote CPU is nearly ready to execute <func>
371 * or is or has executed.
374 int smp_call_function_single (int cpu, void (*func) (void *info), void *info,
375 int nonatomic, int wait)
377 /* prevent preemption and reschedule on another processor */
384 spin_lock_bh(&call_lock);
385 __smp_call_function_single(cpu, func, info, nonatomic, wait);
386 spin_unlock_bh(&call_lock);
392 * this function sends a 'generic call function' IPI to all other CPUs
395 static void __smp_call_function (void (*func) (void *info), void *info,
396 int nonatomic, int wait)
398 struct call_data_struct data;
399 int cpus = num_online_cpus()-1;
406 atomic_set(&data.started, 0);
409 atomic_set(&data.finished, 0);
413 /* Send a message to all other CPUs and wait for them to respond */
414 send_IPI_allbutself(CALL_FUNCTION_VECTOR);
416 /* Wait for response */
417 while (atomic_read(&data.started) != cpus)
423 while (atomic_read(&data.finished) != cpus)
428 * smp_call_function - run a function on all other CPUs.
429 * @func: The function to run. This must be fast and non-blocking.
430 * @info: An arbitrary pointer to pass to the function.
431 * @nonatomic: currently unused.
432 * @wait: If true, wait (atomically) until function has completed on other
435 * Returns 0 on success, else a negative status code. Does not return until
436 * remote CPUs are nearly ready to execute func or are or have executed.
438 * You must not call this function with disabled interrupts or from a
439 * hardware interrupt handler or from a bottom half handler.
440 * Actually there are a few legal cases, like panic.
442 int smp_call_function (void (*func) (void *info), void *info, int nonatomic,
445 spin_lock(&call_lock);
446 __smp_call_function(func,info,nonatomic,wait);
447 spin_unlock(&call_lock);
451 void smp_stop_cpu(void)
457 cpu_clear(smp_processor_id(), cpu_online_map);
458 local_irq_save(flags);
459 disable_local_APIC();
460 local_irq_restore(flags);
463 static void smp_really_stop_cpu(void *dummy)
470 void smp_send_stop(void)
475 /* Don't deadlock on the call lock in panic */
476 if (!spin_trylock(&call_lock)) {
477 /* ignore locking because we have paniced anyways */
480 __smp_call_function(smp_really_stop_cpu, NULL, 0, 0);
482 spin_unlock(&call_lock);
485 disable_local_APIC();
490 * Reschedule call back. Nothing to do,
491 * all the work is done automatically when
492 * we return from the interrupt.
494 asmlinkage void smp_reschedule_interrupt(void)
499 asmlinkage void smp_call_function_interrupt(void)
501 void (*func) (void *info) = call_data->func;
502 void *info = call_data->info;
503 int wait = call_data->wait;
507 * Notify initiating CPU that I've grabbed the data and am
508 * about to execute the function
511 atomic_inc(&call_data->started);
513 * At this point the info structure may be out of scope unless wait==1
520 atomic_inc(&call_data->finished);
524 int safe_smp_processor_id(void)
531 apicid = hard_smp_processor_id();
532 if (x86_cpu_to_apicid[apicid] == apicid)
535 for (i = 0; i < NR_CPUS; ++i) {
536 if (x86_cpu_to_apicid[i] == apicid)
540 /* No entries in x86_cpu_to_apicid? Either no MPS|ACPI,
541 * or called too early. Either way, we must be CPU 0. */
542 if (x86_cpu_to_apicid[0] == BAD_APICID)
545 return 0; /* Should not happen */