blob: 9a9d4c4893305848dc91ded242577d55ff3ef13a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * SMP Support
3 *
4 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
5 * Copyright (C) 1999, 2001, 2003 David Mosberger-Tang <davidm@hpl.hp.com>
6 *
7 * Lots of stuff stolen from arch/alpha/kernel/smp.c
8 *
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
17 * scheme.
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
20 */
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>
28#include <linux/mm.h>
29#include <linux/cache.h>
30#include <linux/delay.h>
31#include <linux/efi.h>
32#include <linux/bitops.h>
Zou Nan haia79561132006-12-07 09:51:35 -080033#include <linux/kexec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <asm/atomic.h>
36#include <asm/current.h>
37#include <asm/delay.h>
38#include <asm/machvec.h>
39#include <asm/io.h>
40#include <asm/irq.h>
41#include <asm/page.h>
42#include <asm/pgalloc.h>
43#include <asm/pgtable.h>
44#include <asm/processor.h>
45#include <asm/ptrace.h>
46#include <asm/sal.h>
47#include <asm/system.h>
48#include <asm/tlbflush.h>
49#include <asm/unistd.h>
50#include <asm/mca.h>
51
52/*
Jack Steiner3be44b92007-05-08 14:50:43 -070053 * 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.
56 */
57static struct local_tlb_flush_counts {
58 unsigned int count;
59} __attribute__((__aligned__(32))) local_tlb_flush_counts[NR_CPUS];
60
61static DEFINE_PER_CPU(unsigned int, shadow_flush_counts[NR_CPUS]) ____cacheline_aligned;
62
63
64/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * Structure and data for smp_call_function(). This is designed to minimise static memory
66 * requirements. It also looks cleaner.
67 */
68static __cacheline_aligned DEFINE_SPINLOCK(call_lock);
69
70struct call_data_struct {
71 void (*func) (void *info);
72 void *info;
73 long wait;
74 atomic_t started;
75 atomic_t finished;
76};
77
78static volatile struct call_data_struct *call_data;
79
80#define IPI_CALL_FUNC 0
81#define IPI_CPU_STOP 1
Zou Nan haia79561132006-12-07 09:51:35 -080082#define IPI_KDUMP_CPU_STOP 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/* This needs to be cacheline aligned because it is written to by *other* CPUs. */
Fenghua Yuf34e3b62007-07-19 01:48:13 -070085static DEFINE_PER_CPU_SHARED_ALIGNED(u64, ipi_operation);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87extern void cpu_halt (void);
88
89void
90lock_ipi_calllock(void)
91{
92 spin_lock_irq(&call_lock);
93}
94
95void
96unlock_ipi_calllock(void)
97{
98 spin_unlock_irq(&call_lock);
99}
100
101static void
102stop_this_cpu (void)
103{
104 /*
105 * Remove this CPU:
106 */
107 cpu_clear(smp_processor_id(), cpu_online_map);
108 max_xtp();
109 local_irq_disable();
110 cpu_halt();
111}
112
113void
114cpu_die(void)
115{
116 max_xtp();
117 local_irq_disable();
118 cpu_halt();
119 /* Should never be here */
120 BUG();
121 for (;;);
122}
123
124irqreturn_t
Keith Owens024e4f22006-10-18 15:36:49 +1000125handle_IPI (int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 int this_cpu = get_cpu();
128 unsigned long *pending_ipis = &__ia64_per_cpu_var(ipi_operation);
129 unsigned long ops;
130
131 mb(); /* Order interrupt and bit testing. */
132 while ((ops = xchg(pending_ipis, 0)) != 0) {
133 mb(); /* Order bit clearing and data access. */
134 do {
135 unsigned long which;
136
137 which = ffz(~ops);
138 ops &= ~(1 << which);
139
140 switch (which) {
141 case IPI_CALL_FUNC:
142 {
143 struct call_data_struct *data;
144 void (*func)(void *info);
145 void *info;
146 int wait;
147
148 /* release the 'pointer lock' */
149 data = (struct call_data_struct *) call_data;
150 func = data->func;
151 info = data->info;
152 wait = data->wait;
153
154 mb();
155 atomic_inc(&data->started);
156 /*
157 * At this point the structure may be gone unless
158 * wait is true.
159 */
160 (*func)(info);
161
162 /* Notify the sending CPU that the task is done. */
163 mb();
164 if (wait)
165 atomic_inc(&data->finished);
166 }
167 break;
168
169 case IPI_CPU_STOP:
170 stop_this_cpu();
171 break;
Horms45a98fc2006-12-12 17:49:03 +0900172#ifdef CONFIG_KEXEC
Zou Nan haia79561132006-12-07 09:51:35 -0800173 case IPI_KDUMP_CPU_STOP:
174 unw_init_running(kdump_cpu_freeze, NULL);
175 break;
176#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 default:
178 printk(KERN_CRIT "Unknown IPI on CPU %d: %lu\n", this_cpu, which);
179 break;
180 }
181 } while (ops);
182 mb(); /* Order data access and bit testing. */
183 }
184 put_cpu();
185 return IRQ_HANDLED;
186}
187
188/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700189 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
191static inline void
192send_IPI_single (int dest_cpu, int op)
193{
194 set_bit(op, &per_cpu(ipi_operation, dest_cpu));
195 platform_send_ipi(dest_cpu, IA64_IPI_VECTOR, IA64_IPI_DM_INT, 0);
196}
197
198/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700199 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
201static inline void
202send_IPI_allbutself (int op)
203{
204 unsigned int i;
205
hawkes@sgi.comdc565b52005-10-10 08:43:26 -0700206 for_each_online_cpu(i) {
207 if (i != smp_processor_id())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 send_IPI_single(i, op);
209 }
210}
211
212/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700213 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
215static inline void
Xiantao Zhang31a6b112008-04-03 11:39:43 -0700216send_IPI_mask(cpumask_t mask, int op)
217{
218 unsigned int cpu;
219
220 for_each_cpu_mask(cpu, mask) {
221 send_IPI_single(cpu, op);
222 }
223}
224
225/*
226 * Called with preemption disabled.
227 */
228static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229send_IPI_all (int op)
230{
231 int i;
232
hawkes@sgi.comdc565b52005-10-10 08:43:26 -0700233 for_each_online_cpu(i) {
234 send_IPI_single(i, op);
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700239 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241static inline void
242send_IPI_self (int op)
243{
244 send_IPI_single(smp_processor_id(), op);
245}
246
Horms45a98fc2006-12-12 17:49:03 +0900247#ifdef CONFIG_KEXEC
Zou Nan haia79561132006-12-07 09:51:35 -0800248void
Al Viroccbebda2007-02-09 16:38:10 +0000249kdump_smp_send_stop(void)
Zou Nan haia79561132006-12-07 09:51:35 -0800250{
251 send_IPI_allbutself(IPI_KDUMP_CPU_STOP);
252}
253
254void
Al Viroccbebda2007-02-09 16:38:10 +0000255kdump_smp_send_init(void)
Zou Nan haia79561132006-12-07 09:51:35 -0800256{
257 unsigned int cpu, self_cpu;
258 self_cpu = smp_processor_id();
259 for_each_online_cpu(cpu) {
260 if (cpu != self_cpu) {
261 if(kdump_status[cpu] == 0)
262 platform_send_ipi(cpu, 0, IA64_IPI_DM_INIT, 0);
263 }
264 }
265}
266#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700268 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
270void
271smp_send_reschedule (int cpu)
272{
273 platform_send_ipi(cpu, IA64_IPI_RESCHEDULE, IA64_IPI_DM_INT, 0);
274}
275
Jack Steiner3be44b92007-05-08 14:50:43 -0700276/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700277 * Called with preemption disabled.
Jack Steiner3be44b92007-05-08 14:50:43 -0700278 */
279static void
280smp_send_local_flush_tlb (int cpu)
281{
282 platform_send_ipi(cpu, IA64_IPI_LOCAL_TLB_FLUSH, IA64_IPI_DM_INT, 0);
283}
284
285void
286smp_local_flush_tlb(void)
287{
288 /*
289 * Use atomic ops. Otherwise, the load/increment/store sequence from
290 * a "++" operation can have the line stolen between the load & store.
291 * The overhead of the atomic op in negligible in this case & offers
292 * significant benefit for the brief periods where lots of cpus
293 * are simultaneously flushing TLBs.
294 */
295 ia64_fetchadd(1, &local_tlb_flush_counts[smp_processor_id()].count, acq);
296 local_flush_tlb_all();
297}
298
299#define FLUSH_DELAY 5 /* Usec backoff to eliminate excessive cacheline bouncing */
300
301void
302smp_flush_tlb_cpumask(cpumask_t xcpumask)
303{
304 unsigned int *counts = __ia64_per_cpu_var(shadow_flush_counts);
305 cpumask_t cpumask = xcpumask;
306 int mycpu, cpu, flush_mycpu = 0;
307
308 preempt_disable();
309 mycpu = smp_processor_id();
310
311 for_each_cpu_mask(cpu, cpumask)
312 counts[cpu] = local_tlb_flush_counts[cpu].count;
313
314 mb();
315 for_each_cpu_mask(cpu, cpumask) {
316 if (cpu == mycpu)
317 flush_mycpu = 1;
318 else
319 smp_send_local_flush_tlb(cpu);
320 }
321
322 if (flush_mycpu)
323 smp_local_flush_tlb();
324
325 for_each_cpu_mask(cpu, cpumask)
326 while(counts[cpu] == local_tlb_flush_counts[cpu].count)
327 udelay(FLUSH_DELAY);
328
329 preempt_enable();
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332void
333smp_flush_tlb_all (void)
334{
335 on_each_cpu((void (*)(void *))local_flush_tlb_all, NULL, 1, 1);
336}
337
338void
339smp_flush_tlb_mm (struct mm_struct *mm)
340{
Peter Chubba68db7632005-06-23 21:14:00 -0700341 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* this happens for the common case of a single-threaded fork(): */
343 if (likely(mm == current->active_mm && atomic_read(&mm->mm_users) == 1))
344 {
345 local_finish_flush_tlb_mm(mm);
Peter Chubba68db7632005-06-23 21:14:00 -0700346 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return;
348 }
349
Peter Chubba68db7632005-06-23 21:14:00 -0700350 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /*
352 * We could optimize this further by using mm->cpu_vm_mask to track which CPUs
353 * have been running in the address space. It's not clear that this is worth the
354 * trouble though: to avoid races, we have to raise the IPI on the target CPU
355 * anyhow, and once a CPU is interrupted, the cost of local_flush_tlb_all() is
356 * rather trivial.
357 */
358 on_each_cpu((void (*)(void *))local_finish_flush_tlb_mm, mm, 1, 1);
359}
360
361/*
Avi Kivity8a2d8692007-07-19 18:32:43 +0300362 * Run a function on a specific CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * <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.
367 * [RETURNS] 0 on success, else a negative status code.
368 *
369 * Does not return until the remote CPU is nearly ready to execute <func>
370 * or is or has executed.
371 */
372
373int
374smp_call_function_single (int cpuid, void (*func) (void *info), void *info, int nonatomic,
375 int wait)
376{
377 struct call_data_struct data;
378 int cpus = 1;
379 int me = get_cpu(); /* prevent preemption and reschedule on another processor */
380
381 if (cpuid == me) {
Avi Kivity8a2d8692007-07-19 18:32:43 +0300382 local_irq_disable();
383 func(info);
384 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 put_cpu();
Avi Kivity8a2d8692007-07-19 18:32:43 +0300386 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388
389 data.func = func;
390 data.info = info;
391 atomic_set(&data.started, 0);
392 data.wait = wait;
393 if (wait)
394 atomic_set(&data.finished, 0);
395
396 spin_lock_bh(&call_lock);
397
398 call_data = &data;
399 mb(); /* ensure store to call_data precedes setting of IPI_CALL_FUNC */
400 send_IPI_single(cpuid, IPI_CALL_FUNC);
401
402 /* Wait for response */
403 while (atomic_read(&data.started) != cpus)
404 cpu_relax();
405
406 if (wait)
407 while (atomic_read(&data.finished) != cpus)
408 cpu_relax();
409 call_data = NULL;
410
411 spin_unlock_bh(&call_lock);
412 put_cpu();
413 return 0;
414}
415EXPORT_SYMBOL(smp_call_function_single);
416
Xiantao Zhang31a6b112008-04-03 11:39:43 -0700417/**
418 * smp_call_function_mask(): Run a function on a set of other CPUs.
419 * <mask> The set of cpus to run on. Must not include the current cpu.
420 * <func> The function to run. This must be fast and non-blocking.
421 * <info> An arbitrary pointer to pass to the function.
422 * <wait> If true, wait (atomically) until function
423 * has completed on other CPUs.
424 *
425 * Returns 0 on success, else a negative status code.
426 *
427 * If @wait is true, then returns once @func has returned; otherwise
428 * it returns just before the target cpu calls @func.
429 *
430 * You must not call this function with disabled interrupts or from a
431 * hardware interrupt handler or from a bottom half handler.
432 */
433int smp_call_function_mask(cpumask_t mask,
434 void (*func)(void *), void *info,
435 int wait)
436{
437 struct call_data_struct data;
438 cpumask_t allbutself;
439 int cpus;
440
441 spin_lock(&call_lock);
442 allbutself = cpu_online_map;
443 cpu_clear(smp_processor_id(), allbutself);
444
445 cpus_and(mask, mask, allbutself);
446 cpus = cpus_weight(mask);
447 if (!cpus) {
448 spin_unlock(&call_lock);
449 return 0;
450 }
451
452 /* Can deadlock when called with interrupts disabled */
453 WARN_ON(irqs_disabled());
454
455 data.func = func;
456 data.info = info;
457 atomic_set(&data.started, 0);
458 data.wait = wait;
459 if (wait)
460 atomic_set(&data.finished, 0);
461
462 call_data = &data;
463 mb(); /* ensure store to call_data precedes setting of IPI_CALL_FUNC*/
464
465 /* Send a message to other CPUs */
466 if (cpus_equal(mask, allbutself))
467 send_IPI_allbutself(IPI_CALL_FUNC);
468 else
469 send_IPI_mask(mask, IPI_CALL_FUNC);
470
471 /* Wait for response */
472 while (atomic_read(&data.started) != cpus)
473 cpu_relax();
474
475 if (wait)
476 while (atomic_read(&data.finished) != cpus)
477 cpu_relax();
478 call_data = NULL;
479
480 spin_unlock(&call_lock);
481 return 0;
482
483}
484EXPORT_SYMBOL(smp_call_function_mask);
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/*
487 * this function sends a 'generic call function' IPI to all other CPUs
488 * in the system.
489 */
490
491/*
492 * [SUMMARY] Run a function on all other CPUs.
493 * <func> The function to run. This must be fast and non-blocking.
494 * <info> An arbitrary pointer to pass to the function.
495 * <nonatomic> currently unused.
496 * <wait> If true, wait (atomically) until function has completed on other CPUs.
497 * [RETURNS] 0 on success, else a negative status code.
498 *
499 * Does not return until remote CPUs are nearly ready to execute <func> or are or have
500 * executed.
501 *
502 * You must not call this function with disabled interrupts or from a
503 * hardware interrupt handler or from a bottom half handler.
504 */
505int
506smp_call_function (void (*func) (void *info), void *info, int nonatomic, int wait)
507{
508 struct call_data_struct data;
Kenji Kaneshige5ee773732006-10-27 19:49:53 +0900509 int cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Kenji Kaneshige5ee773732006-10-27 19:49:53 +0900511 spin_lock(&call_lock);
512 cpus = num_online_cpus() - 1;
513 if (!cpus) {
514 spin_unlock(&call_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return 0;
Kenji Kaneshige5ee773732006-10-27 19:49:53 +0900516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /* Can deadlock when called with interrupts disabled */
519 WARN_ON(irqs_disabled());
520
521 data.func = func;
522 data.info = info;
523 atomic_set(&data.started, 0);
524 data.wait = wait;
525 if (wait)
526 atomic_set(&data.finished, 0);
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 call_data = &data;
529 mb(); /* ensure store to call_data precedes setting of IPI_CALL_FUNC */
530 send_IPI_allbutself(IPI_CALL_FUNC);
531
532 /* Wait for response */
533 while (atomic_read(&data.started) != cpus)
534 cpu_relax();
535
536 if (wait)
537 while (atomic_read(&data.finished) != cpus)
538 cpu_relax();
539 call_data = NULL;
540
541 spin_unlock(&call_lock);
542 return 0;
543}
544EXPORT_SYMBOL(smp_call_function);
545
546/*
547 * this function calls the 'stop' function on all other CPUs in the system.
548 */
549void
550smp_send_stop (void)
551{
552 send_IPI_allbutself(IPI_CPU_STOP);
553}
554
Tony Luckcb2e0912007-07-20 16:14:28 -0700555int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556setup_profiling_timer (unsigned int multiplier)
557{
558 return -EINVAL;
559}