4 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
5 * Copyright (C) 1999, 2001, 2003 David Mosberger-Tang <davidm@hpl.hp.com>
7 * Lots of stuff stolen from arch/alpha/kernel/smp.c
9 * 01/05/16 Rohit Seth <rohit.seth@intel.com> IA64-SMP functions. Reorganized
10 * the existing code (on the lines of x86 port).
11 * 00/09/11 David Mosberger <davidm@hpl.hp.com> Do loops_per_jiffy
12 * calibration on each CPU.
13 * 00/08/23 Asit Mallick <asit.k.mallick@intel.com> fixed logical processor id
14 * 00/03/31 Rohit Seth <rohit.seth@intel.com> Fixes for Bootstrap Processor
15 * & cpu_online_map now gets done here (instead of setup.c)
16 * 99/10/05 davidm Update to bring it in sync with new command-line processing
18 * 10/13/00 Goutham Rao <goutham.rao@intel.com> Updated smp_call_function and
19 * smp_call_function_single to resend IPI on timeouts
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/smp.h>
27 #include <linux/kernel_stat.h>
29 #include <linux/cache.h>
30 #include <linux/delay.h>
31 #include <linux/efi.h>
32 #include <linux/bitops.h>
33 #include <linux/kexec.h>
35 #include <asm/atomic.h>
36 #include <asm/current.h>
37 #include <asm/delay.h>
38 #include <asm/machvec.h>
42 #include <asm/pgalloc.h>
43 #include <asm/pgtable.h>
44 #include <asm/processor.h>
45 #include <asm/ptrace.h>
47 #include <asm/system.h>
48 #include <asm/tlbflush.h>
49 #include <asm/unistd.h>
53 * Note: alignment of 4 entries/cacheline was empirically determined
54 * to be a good tradeoff between hot cachelines & spreading the array
55 * across too many cacheline.
57 static struct local_tlb_flush_counts {
59 } __attribute__((__aligned__(32))) local_tlb_flush_counts[NR_CPUS];
61 static DEFINE_PER_CPU(unsigned int, shadow_flush_counts[NR_CPUS]) ____cacheline_aligned;
65 * Structure and data for smp_call_function(). This is designed to minimise static memory
66 * requirements. It also looks cleaner.
68 static __cacheline_aligned DEFINE_SPINLOCK(call_lock);
70 struct call_data_struct {
71 void (*func) (void *info);
78 static volatile struct call_data_struct *call_data;
80 #define IPI_CALL_FUNC 0
81 #define IPI_CPU_STOP 1
82 #define IPI_KDUMP_CPU_STOP 3
84 /* This needs to be cacheline aligned because it is written to by *other* CPUs. */
85 static DEFINE_PER_CPU_SHARED_ALIGNED(u64, ipi_operation);
87 extern void cpu_halt (void);
90 lock_ipi_calllock(void)
92 spin_lock_irq(&call_lock);
96 unlock_ipi_calllock(void)
98 spin_unlock_irq(&call_lock);
102 handle_call_data(void)
104 struct call_data_struct *data;
105 void (*func)(void *info);
109 /* release the 'pointer lock' */
110 data = (struct call_data_struct *)call_data;
116 atomic_inc(&data->started);
117 /* At this point the structure may be gone unless wait is true. */
120 /* Notify the sending CPU that the task is done. */
123 atomic_inc(&data->finished);
132 cpu_clear(smp_processor_id(), cpu_online_map);
144 /* Should never be here */
150 handle_IPI (int irq, void *dev_id)
152 int this_cpu = get_cpu();
153 unsigned long *pending_ipis = &__ia64_per_cpu_var(ipi_operation);
156 mb(); /* Order interrupt and bit testing. */
157 while ((ops = xchg(pending_ipis, 0)) != 0) {
158 mb(); /* Order bit clearing and data access. */
163 ops &= ~(1 << which);
174 case IPI_KDUMP_CPU_STOP:
175 unw_init_running(kdump_cpu_freeze, NULL);
179 printk(KERN_CRIT "Unknown IPI on CPU %d: %lu\n",
184 mb(); /* Order data access and bit testing. */
191 * Called with preemption disabled.
194 send_IPI_single (int dest_cpu, int op)
196 set_bit(op, &per_cpu(ipi_operation, dest_cpu));
197 platform_send_ipi(dest_cpu, IA64_IPI_VECTOR, IA64_IPI_DM_INT, 0);
201 * Called with preemption disabled.
204 send_IPI_allbutself (int op)
208 for_each_online_cpu(i) {
209 if (i != smp_processor_id())
210 send_IPI_single(i, op);
215 * Called with preemption disabled.
218 send_IPI_mask(cpumask_t mask, int op)
222 for_each_cpu_mask(cpu, mask) {
223 send_IPI_single(cpu, op);
228 * Called with preemption disabled.
231 send_IPI_all (int op)
235 for_each_online_cpu(i) {
236 send_IPI_single(i, op);
241 * Called with preemption disabled.
244 send_IPI_self (int op)
246 send_IPI_single(smp_processor_id(), op);
251 kdump_smp_send_stop(void)
253 send_IPI_allbutself(IPI_KDUMP_CPU_STOP);
257 kdump_smp_send_init(void)
259 unsigned int cpu, self_cpu;
260 self_cpu = smp_processor_id();
261 for_each_online_cpu(cpu) {
262 if (cpu != self_cpu) {
263 if(kdump_status[cpu] == 0)
264 platform_send_ipi(cpu, 0, IA64_IPI_DM_INIT, 0);
270 * Called with preemption disabled.
273 smp_send_reschedule (int cpu)
275 platform_send_ipi(cpu, IA64_IPI_RESCHEDULE, IA64_IPI_DM_INT, 0);
279 * Called with preemption disabled.
282 smp_send_local_flush_tlb (int cpu)
284 platform_send_ipi(cpu, IA64_IPI_LOCAL_TLB_FLUSH, IA64_IPI_DM_INT, 0);
288 smp_local_flush_tlb(void)
291 * Use atomic ops. Otherwise, the load/increment/store sequence from
292 * a "++" operation can have the line stolen between the load & store.
293 * The overhead of the atomic op in negligible in this case & offers
294 * significant benefit for the brief periods where lots of cpus
295 * are simultaneously flushing TLBs.
297 ia64_fetchadd(1, &local_tlb_flush_counts[smp_processor_id()].count, acq);
298 local_flush_tlb_all();
301 #define FLUSH_DELAY 5 /* Usec backoff to eliminate excessive cacheline bouncing */
304 smp_flush_tlb_cpumask(cpumask_t xcpumask)
306 unsigned int *counts = __ia64_per_cpu_var(shadow_flush_counts);
307 cpumask_t cpumask = xcpumask;
308 int mycpu, cpu, flush_mycpu = 0;
311 mycpu = smp_processor_id();
313 for_each_cpu_mask(cpu, cpumask)
314 counts[cpu] = local_tlb_flush_counts[cpu].count;
317 for_each_cpu_mask(cpu, cpumask) {
321 smp_send_local_flush_tlb(cpu);
325 smp_local_flush_tlb();
327 for_each_cpu_mask(cpu, cpumask)
328 while(counts[cpu] == local_tlb_flush_counts[cpu].count)
335 smp_flush_tlb_all (void)
337 on_each_cpu((void (*)(void *))local_flush_tlb_all, NULL, 1, 1);
341 smp_flush_tlb_mm (struct mm_struct *mm)
344 /* this happens for the common case of a single-threaded fork(): */
345 if (likely(mm == current->active_mm && atomic_read(&mm->mm_users) == 1))
347 local_finish_flush_tlb_mm(mm);
354 * We could optimize this further by using mm->cpu_vm_mask to track which CPUs
355 * have been running in the address space. It's not clear that this is worth the
356 * trouble though: to avoid races, we have to raise the IPI on the target CPU
357 * anyhow, and once a CPU is interrupted, the cost of local_flush_tlb_all() is
360 on_each_cpu((void (*)(void *))local_finish_flush_tlb_mm, mm, 1, 1);
364 * Run a function on a specific CPU
365 * <func> The function to run. This must be fast and non-blocking.
366 * <info> An arbitrary pointer to pass to the function.
367 * <nonatomic> Currently unused.
368 * <wait> If true, wait until function has completed on other CPUs.
369 * [RETURNS] 0 on success, else a negative status code.
371 * Does not return until the remote CPU is nearly ready to execute <func>
372 * or is or has executed.
376 smp_call_function_single (int cpuid, void (*func) (void *info), void *info, int nonatomic,
379 struct call_data_struct data;
381 int me = get_cpu(); /* prevent preemption and reschedule on another processor */
393 atomic_set(&data.started, 0);
396 atomic_set(&data.finished, 0);
398 spin_lock_bh(&call_lock);
401 mb(); /* ensure store to call_data precedes setting of IPI_CALL_FUNC */
402 send_IPI_single(cpuid, IPI_CALL_FUNC);
404 /* Wait for response */
405 while (atomic_read(&data.started) != cpus)
409 while (atomic_read(&data.finished) != cpus)
413 spin_unlock_bh(&call_lock);
417 EXPORT_SYMBOL(smp_call_function_single);
420 * smp_call_function_mask(): Run a function on a set of other CPUs.
421 * <mask> The set of cpus to run on. Must not include the current cpu.
422 * <func> The function to run. This must be fast and non-blocking.
423 * <info> An arbitrary pointer to pass to the function.
424 * <wait> If true, wait (atomically) until function
425 * has completed on other CPUs.
427 * Returns 0 on success, else a negative status code.
429 * If @wait is true, then returns once @func has returned; otherwise
430 * it returns just before the target cpu calls @func.
432 * You must not call this function with disabled interrupts or from a
433 * hardware interrupt handler or from a bottom half handler.
435 int smp_call_function_mask(cpumask_t mask,
436 void (*func)(void *), void *info,
439 struct call_data_struct data;
440 cpumask_t allbutself;
443 spin_lock(&call_lock);
444 allbutself = cpu_online_map;
445 cpu_clear(smp_processor_id(), allbutself);
447 cpus_and(mask, mask, allbutself);
448 cpus = cpus_weight(mask);
450 spin_unlock(&call_lock);
454 /* Can deadlock when called with interrupts disabled */
455 WARN_ON(irqs_disabled());
459 atomic_set(&data.started, 0);
462 atomic_set(&data.finished, 0);
465 mb(); /* ensure store to call_data precedes setting of IPI_CALL_FUNC*/
467 /* Send a message to other CPUs */
468 if (cpus_equal(mask, allbutself))
469 send_IPI_allbutself(IPI_CALL_FUNC);
471 send_IPI_mask(mask, IPI_CALL_FUNC);
473 /* Wait for response */
474 while (atomic_read(&data.started) != cpus)
478 while (atomic_read(&data.finished) != cpus)
482 spin_unlock(&call_lock);
486 EXPORT_SYMBOL(smp_call_function_mask);
489 * this function sends a 'generic call function' IPI to all other CPUs
494 * [SUMMARY] Run a function on all other CPUs.
495 * <func> The function to run. This must be fast and non-blocking.
496 * <info> An arbitrary pointer to pass to the function.
497 * <nonatomic> currently unused.
498 * <wait> If true, wait (atomically) until function has completed on other CPUs.
499 * [RETURNS] 0 on success, else a negative status code.
501 * Does not return until remote CPUs are nearly ready to execute <func> or are or have
504 * You must not call this function with disabled interrupts or from a
505 * hardware interrupt handler or from a bottom half handler.
508 smp_call_function (void (*func) (void *info), void *info, int nonatomic, int wait)
510 struct call_data_struct data;
513 spin_lock(&call_lock);
514 cpus = num_online_cpus() - 1;
516 spin_unlock(&call_lock);
520 /* Can deadlock when called with interrupts disabled */
521 WARN_ON(irqs_disabled());
525 atomic_set(&data.started, 0);
528 atomic_set(&data.finished, 0);
531 mb(); /* ensure store to call_data precedes setting of IPI_CALL_FUNC */
532 send_IPI_allbutself(IPI_CALL_FUNC);
534 /* Wait for response */
535 while (atomic_read(&data.started) != cpus)
539 while (atomic_read(&data.finished) != cpus)
543 spin_unlock(&call_lock);
546 EXPORT_SYMBOL(smp_call_function);
549 * this function calls the 'stop' function on all other CPUs in the system.
554 send_IPI_allbutself(IPI_CPU_STOP);
558 setup_profiling_timer (unsigned int multiplier)