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>
29 * the following functions deal with sending IPIs between CPUs.
31 * We use 'broadcast', CPU->CPU IPIs and self-IPIs too.
34 static inline unsigned int __prepare_ICR (unsigned int shortcut
, int vector
)
36 unsigned int icr
= APIC_DM_FIXED
| shortcut
| vector
| APIC_DEST_LOGICAL
;
37 if (vector
== KDB_VECTOR
)
38 icr
= (icr
& (~APIC_VECTOR_MASK
)) | APIC_DM_NMI
;
42 static inline int __prepare_ICR2 (unsigned int mask
)
44 return SET_APIC_DEST_FIELD(mask
);
47 static inline void __send_IPI_shortcut(unsigned int shortcut
, int vector
)
50 * Subtle. In the case of the 'never do double writes' workaround
51 * we have to lock out interrupts to be safe. As we don't care
52 * of the value read we use an atomic rmw access to avoid costly
53 * cli/sti. Otherwise we use an even cheaper single atomic write
64 * No need to touch the target chip field
66 cfg
= __prepare_ICR(shortcut
, vector
);
69 * Send the IPI. The write to APIC_ICR fires this off.
71 apic_write_around(APIC_ICR
, cfg
);
74 static inline void send_IPI_allbutself(int vector
)
77 * if there are no other CPUs in the system then
78 * we get an APIC send error if we try to broadcast.
79 * thus we have to avoid sending IPIs in this case.
81 if (num_online_cpus() > 1)
82 __send_IPI_shortcut(APIC_DEST_ALLBUT
, vector
);
85 static inline void send_IPI_all(int vector
)
87 __send_IPI_shortcut(APIC_DEST_ALLINC
, vector
);
90 void send_IPI_self(int vector
)
92 __send_IPI_shortcut(APIC_DEST_SELF
, vector
);
95 static inline void send_IPI_mask(cpumask_t cpumask
, int vector
)
97 unsigned long mask
= cpus_addr(cpumask
)[0];
101 local_save_flags(flags
);
107 apic_wait_icr_idle();
110 * prepare target chip field
112 cfg
= __prepare_ICR2(mask
);
113 apic_write_around(APIC_ICR2
, cfg
);
118 cfg
= __prepare_ICR(0, vector
);
121 * Send the IPI. The write to APIC_ICR fires this off.
123 apic_write_around(APIC_ICR
, cfg
);
124 local_irq_restore(flags
);
128 * Smarter SMP flushing macros.
129 * c/o Linus Torvalds.
131 * These mean you can really definitely utterly forget about
132 * writing to user space from interrupts. (Its not allowed anyway).
134 * Optimizations Manfred Spraul <manfred@colorfullife.com>
137 static cpumask_t flush_cpumask
;
138 static struct mm_struct
* flush_mm
;
139 static unsigned long flush_va
;
140 static spinlock_t tlbstate_lock
= SPIN_LOCK_UNLOCKED
;
141 #define FLUSH_ALL 0xffffffff
144 * We cannot call mmdrop() because we are in interrupt context,
145 * instead update mm->cpu_vm_mask.
147 static inline void leave_mm (unsigned long cpu
)
149 if (read_pda(mmu_state
) == TLBSTATE_OK
)
151 clear_bit(cpu
, &read_pda(active_mm
)->cpu_vm_mask
);
157 * The flush IPI assumes that a thread switch happens in this order:
158 * [cpu0: the cpu that switches]
159 * 1) switch_mm() either 1a) or 1b)
160 * 1a) thread switch to a different mm
161 * 1a1) clear_bit(cpu, &old_mm->cpu_vm_mask);
162 * Stop ipi delivery for the old mm. This is not synchronized with
163 * the other cpus, but smp_invalidate_interrupt ignore flush ipis
164 * for the wrong mm, and in the worst case we perform a superfluous
166 * 1a2) set cpu mmu_state to TLBSTATE_OK
167 * Now the smp_invalidate_interrupt won't call leave_mm if cpu0
168 * was in lazy tlb mode.
169 * 1a3) update cpu active_mm
170 * Now cpu0 accepts tlb flushes for the new mm.
171 * 1a4) set_bit(cpu, &new_mm->cpu_vm_mask);
172 * Now the other cpus will send tlb flush ipis.
174 * 1b) thread switch without mm change
175 * cpu active_mm is correct, cpu0 already handles
177 * 1b1) set cpu mmu_state to TLBSTATE_OK
178 * 1b2) test_and_set the cpu bit in cpu_vm_mask.
179 * Atomically set the bit [other cpus will start sending flush ipis],
181 * 1b3) if the bit was 0: leave_mm was called, flush the tlb.
182 * 2) switch %%esp, ie current
184 * The interrupt must handle 2 special cases:
185 * - cr3 is changed before %%esp, ie. it cannot use current->{active_,}mm.
186 * - the cpu performs speculative tlb reads, i.e. even if the cpu only
187 * runs in kernel space, the cpu could load tlb entries for user space
190 * The good news is that cpu mmu_state is local to each cpu, no
191 * write/read ordering problems.
197 * 1) Flush the tlb entries if the cpu uses the mm that's being flushed.
198 * 2) Leave the mm if we are in the lazy tlb mode.
201 asmlinkage
void smp_invalidate_interrupt (void)
207 if (!cpu_isset(cpu
, flush_cpumask
))
210 * This was a BUG() but until someone can quote me the
211 * line from the intel manual that guarantees an IPI to
212 * multiple CPUs is retried _only_ on the erroring CPUs
213 * its staying as a return
218 if (flush_mm
== read_pda(active_mm
)) {
219 if (read_pda(mmu_state
) == TLBSTATE_OK
) {
220 if (flush_va
== FLUSH_ALL
)
223 __flush_tlb_one(flush_va
);
228 cpu_clear(cpu
, flush_cpumask
);
231 put_cpu_no_resched();
234 static void flush_tlb_others(cpumask_t cpumask
, struct mm_struct
*mm
,
239 * A couple of (to be removed) sanity checks:
241 * - we do not send IPIs to not-yet booted CPUs.
242 * - current CPU must not be in mask
243 * - mask must exist :)
245 BUG_ON(cpus_empty(cpumask
));
246 cpus_and(tmp
, cpumask
, cpu_online_map
);
247 BUG_ON(!cpus_equal(tmp
, cpumask
));
248 BUG_ON(cpu_isset(smp_processor_id(), cpumask
));
253 * I'm not happy about this global shared spinlock in the
254 * MM hot path, but we'll see how contended it is.
255 * Temporarily this turns IRQs off, so that lockups are
256 * detected by the NMI watchdog.
258 spin_lock(&tlbstate_lock
);
262 cpus_or(flush_cpumask
, cpumask
, flush_cpumask
);
265 * We have to send the IPI only to
268 send_IPI_mask(cpumask
, INVALIDATE_TLB_VECTOR
);
270 while (!cpus_empty(flush_cpumask
))
271 mb(); /* nothing. lockup detection does not belong here */;
275 spin_unlock(&tlbstate_lock
);
278 void flush_tlb_current_task(void)
280 struct mm_struct
*mm
= current
->mm
;
284 cpu_mask
= mm
->cpu_vm_mask
;
285 cpu_clear(smp_processor_id(), cpu_mask
);
288 if (!cpus_empty(cpu_mask
))
289 flush_tlb_others(cpu_mask
, mm
, FLUSH_ALL
);
293 void flush_tlb_mm (struct mm_struct
* mm
)
298 cpu_mask
= mm
->cpu_vm_mask
;
299 cpu_clear(smp_processor_id(), cpu_mask
);
301 if (current
->active_mm
== mm
) {
305 leave_mm(smp_processor_id());
307 if (!cpus_empty(cpu_mask
))
308 flush_tlb_others(cpu_mask
, mm
, FLUSH_ALL
);
313 void flush_tlb_page(struct vm_area_struct
* vma
, unsigned long va
)
315 struct mm_struct
*mm
= vma
->vm_mm
;
319 cpu_mask
= mm
->cpu_vm_mask
;
320 cpu_clear(smp_processor_id(), cpu_mask
);
322 if (current
->active_mm
== mm
) {
326 leave_mm(smp_processor_id());
329 if (!cpus_empty(cpu_mask
))
330 flush_tlb_others(cpu_mask
, mm
, va
);
335 static void do_flush_tlb_all(void* info
)
337 unsigned long cpu
= smp_processor_id();
340 if (read_pda(mmu_state
) == TLBSTATE_LAZY
)
344 void flush_tlb_all(void)
346 on_each_cpu(do_flush_tlb_all
, NULL
, 1, 1);
349 void smp_kdb_stop(void)
351 send_IPI_allbutself(KDB_VECTOR
);
355 * this function sends a 'reschedule' IPI to another CPU.
356 * it goes straight through and wastes no time serializing
357 * anything. Worst case is that we lose a reschedule ...
360 void smp_send_reschedule(int cpu
)
362 send_IPI_mask(cpumask_of_cpu(cpu
), RESCHEDULE_VECTOR
);
366 * Structure and data for smp_call_function(). This is designed to minimise
367 * static memory requirements. It also looks cleaner.
369 static spinlock_t call_lock
= SPIN_LOCK_UNLOCKED
;
371 struct call_data_struct
{
372 void (*func
) (void *info
);
379 static struct call_data_struct
* call_data
;
382 * this function sends a 'generic call function' IPI to all other CPUs
385 static void __smp_call_function (void (*func
) (void *info
), void *info
,
386 int nonatomic
, int wait
)
388 struct call_data_struct data
;
389 int cpus
= num_online_cpus()-1;
396 atomic_set(&data
.started
, 0);
399 atomic_set(&data
.finished
, 0);
403 /* Send a message to all other CPUs and wait for them to respond */
404 send_IPI_allbutself(CALL_FUNCTION_VECTOR
);
406 /* Wait for response */
407 while (atomic_read(&data
.started
) != cpus
)
411 while (atomic_read(&data
.finished
) != cpus
)
416 * smp_call_function - run a function on all other CPUs.
417 * @func: The function to run. This must be fast and non-blocking.
418 * @info: An arbitrary pointer to pass to the function.
419 * @nonatomic: currently unused.
420 * @wait: If true, wait (atomically) until function has completed on other
423 * Returns 0 on success, else a negative status code. Does not return until
424 * remote CPUs are nearly ready to execute func or are or have executed.
426 * You must not call this function with disabled interrupts or from a
427 * hardware interrupt handler or from a bottom half handler.
428 * Actually there are a few legal cases, like panic.
430 int smp_call_function (void (*func
) (void *info
), void *info
, int nonatomic
,
433 spin_lock(&call_lock
);
434 __smp_call_function(func
,info
,nonatomic
,wait
);
435 spin_unlock(&call_lock
);
439 void smp_stop_cpu(void)
444 cpu_clear(smp_processor_id(), cpu_online_map
);
446 disable_local_APIC();
450 static void smp_really_stop_cpu(void *dummy
)
457 void smp_send_stop(void)
460 /* Don't deadlock on the call lock in panic */
461 if (!spin_trylock(&call_lock
)) {
463 /* ignore locking because we have paniced anyways */
466 __smp_call_function(smp_really_stop_cpu
, NULL
, 1, 0);
468 spin_unlock(&call_lock
);
473 * Reschedule call back. Nothing to do,
474 * all the work is done automatically when
475 * we return from the interrupt.
477 asmlinkage
void smp_reschedule_interrupt(void)
482 asmlinkage
void smp_call_function_interrupt(void)
484 void (*func
) (void *info
) = call_data
->func
;
485 void *info
= call_data
->info
;
486 int wait
= call_data
->wait
;
490 * Notify initiating CPU that I've grabbed the data and am
491 * about to execute the function
494 atomic_inc(&call_data
->started
);
496 * At this point the info structure may be out of scope unless wait==1
503 atomic_inc(&call_data
->finished
);