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/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>
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.
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
{
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
)
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
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.
103 * 1b) thread switch without mm change
104 * cpu active_mm is correct, cpu0 already handles
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],
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
119 * The good news is that cpu mmu_state is local to each cpu, no
120 * write/read ordering problems.
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
)
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
))
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
157 if (f
->flush_mm
== read_pda(active_mm
)) {
158 if (read_pda(mmu_state
) == TLBSTATE_OK
) {
159 if (f
->flush_va
== FLUSH_ALL
)
162 __flush_tlb_one(f
->flush_va
);
168 cpu_clear(cpu
, f
->flush_cpumask
);
171 static void flush_tlb_others(cpumask_t cpumask
, struct mm_struct
*mm
,
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
);
188 cpus_or(f
->flush_cpumask
, cpumask
, f
->flush_cpumask
);
191 * We have to send the IPI only to
194 send_IPI_mask(cpumask
, INVALIDATE_TLB_VECTOR_START
+ sender
);
196 while (!cpus_empty(f
->flush_cpumask
))
201 spin_unlock(&f
->tlbstate_lock
);
204 int __cpuinit
init_smp_flush(void)
207 for_each_cpu_mask(i
, cpu_possible_map
) {
208 spin_lock_init(&per_cpu(flush_state
.tlbstate_lock
, i
));
213 core_initcall(init_smp_flush
);
215 void flush_tlb_current_task(void)
217 struct mm_struct
*mm
= current
->mm
;
221 cpu_mask
= mm
->cpu_vm_mask
;
222 cpu_clear(smp_processor_id(), cpu_mask
);
225 if (!cpus_empty(cpu_mask
))
226 flush_tlb_others(cpu_mask
, mm
, FLUSH_ALL
);
230 void flush_tlb_mm (struct mm_struct
* mm
)
235 cpu_mask
= mm
->cpu_vm_mask
;
236 cpu_clear(smp_processor_id(), cpu_mask
);
238 if (current
->active_mm
== mm
) {
242 leave_mm(smp_processor_id());
244 if (!cpus_empty(cpu_mask
))
245 flush_tlb_others(cpu_mask
, mm
, FLUSH_ALL
);
250 void flush_tlb_page(struct vm_area_struct
* vma
, unsigned long va
)
252 struct mm_struct
*mm
= vma
->vm_mm
;
256 cpu_mask
= mm
->cpu_vm_mask
;
257 cpu_clear(smp_processor_id(), cpu_mask
);
259 if (current
->active_mm
== mm
) {
263 leave_mm(smp_processor_id());
266 if (!cpus_empty(cpu_mask
))
267 flush_tlb_others(cpu_mask
, mm
, va
);
272 static void do_flush_tlb_all(void* info
)
274 unsigned long cpu
= smp_processor_id();
277 if (read_pda(mmu_state
) == TLBSTATE_LAZY
)
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
);
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
332 * cpu is a standard Linux logical CPU number.
335 __smp_call_function_single(int cpu
, void (*func
) (void *info
), void *info
,
336 int nonatomic
, int wait
)
338 struct call_data_struct data
;
343 atomic_set(&data
.started
, 0);
346 atomic_set(&data
.finished
, 0);
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
)
360 while (atomic_read(&data
.finished
) != cpus
)
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 */
387 spin_lock_bh(&call_lock
);
388 __smp_call_function_single(cpu
, func
, info
, nonatomic
, wait
);
389 spin_unlock_bh(&call_lock
);
395 * this function sends a 'generic call function' IPI to all other CPUs
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;
409 atomic_set(&data
.started
, 0);
412 atomic_set(&data
.finished
, 0);
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
)
426 while (atomic_read(&data
.finished
) != cpus
)
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
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
,
448 spin_lock(&call_lock
);
449 __smp_call_function(func
,info
,nonatomic
,wait
);
450 spin_unlock(&call_lock
);
454 void smp_stop_cpu(void)
459 cpu_clear(smp_processor_id(), cpu_online_map
);
461 disable_local_APIC();
465 static void smp_really_stop_cpu(void *dummy
)
472 void smp_send_stop(void)
477 /* Don't deadlock on the call lock in panic */
478 if (!spin_trylock(&call_lock
)) {
479 /* ignore locking because we have paniced anyways */
482 __smp_call_function(smp_really_stop_cpu
, NULL
, 0, 0);
484 spin_unlock(&call_lock
);
487 disable_local_APIC();
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)
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
;
509 * Notify initiating CPU that I've grabbed the data and am
510 * about to execute the function
513 atomic_inc(&call_data
->started
);
515 * At this point the info structure may be out of scope unless wait==1
522 atomic_inc(&call_data
->finished
);
526 int safe_smp_processor_id(void)
533 apicid
= hard_smp_processor_id();
534 if (x86_cpu_to_apicid
[apicid
] == apicid
)
537 for (i
= 0; i
< NR_CPUS
; ++i
) {
538 if (x86_cpu_to_apicid
[i
] == apicid
)
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
)
547 return 0; /* Should not happen */