blob: 983296f1c813a3baeb34435258b9ff8af737a172 [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
Hidetoshi Setoc0cd6612008-04-30 18:55:48 +0900101static inline void
102handle_call_data(void)
103{
104 struct call_data_struct *data;
105 void (*func)(void *info);
106 void *info;
107 int wait;
108
109 /* release the 'pointer lock' */
110 data = (struct call_data_struct *)call_data;
111 func = data->func;
112 info = data->info;
113 wait = data->wait;
114
115 mb();
116 atomic_inc(&data->started);
117 /* At this point the structure may be gone unless wait is true. */
118 (*func)(info);
119
120 /* Notify the sending CPU that the task is done. */
121 mb();
122 if (wait)
123 atomic_inc(&data->finished);
124}
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static void
Hidetoshi Setoc0cd6612008-04-30 18:55:48 +0900127stop_this_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 /*
130 * Remove this CPU:
131 */
132 cpu_clear(smp_processor_id(), cpu_online_map);
133 max_xtp();
134 local_irq_disable();
135 cpu_halt();
136}
137
138void
139cpu_die(void)
140{
141 max_xtp();
142 local_irq_disable();
143 cpu_halt();
144 /* Should never be here */
145 BUG();
146 for (;;);
147}
148
149irqreturn_t
Keith Owens024e4f22006-10-18 15:36:49 +1000150handle_IPI (int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 int this_cpu = get_cpu();
153 unsigned long *pending_ipis = &__ia64_per_cpu_var(ipi_operation);
154 unsigned long ops;
155
156 mb(); /* Order interrupt and bit testing. */
157 while ((ops = xchg(pending_ipis, 0)) != 0) {
158 mb(); /* Order bit clearing and data access. */
159 do {
160 unsigned long which;
161
162 which = ffz(~ops);
163 ops &= ~(1 << which);
164
165 switch (which) {
Hidetoshi Setoc0cd6612008-04-30 18:55:48 +0900166 case IPI_CALL_FUNC:
167 handle_call_data();
168 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Hidetoshi Setoc0cd6612008-04-30 18:55:48 +0900170 case IPI_CPU_STOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 stop_this_cpu();
172 break;
Horms45a98fc2006-12-12 17:49:03 +0900173#ifdef CONFIG_KEXEC
Hidetoshi Setoc0cd6612008-04-30 18:55:48 +0900174 case IPI_KDUMP_CPU_STOP:
Zou Nan haia79561132006-12-07 09:51:35 -0800175 unw_init_running(kdump_cpu_freeze, NULL);
176 break;
177#endif
Hidetoshi Setoc0cd6612008-04-30 18:55:48 +0900178 default:
179 printk(KERN_CRIT "Unknown IPI on CPU %d: %lu\n",
180 this_cpu, which);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 break;
182 }
183 } while (ops);
184 mb(); /* Order data access and bit testing. */
185 }
186 put_cpu();
187 return IRQ_HANDLED;
188}
189
190/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700191 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
193static inline void
194send_IPI_single (int dest_cpu, int op)
195{
196 set_bit(op, &per_cpu(ipi_operation, dest_cpu));
197 platform_send_ipi(dest_cpu, IA64_IPI_VECTOR, IA64_IPI_DM_INT, 0);
198}
199
200/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700201 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
203static inline void
204send_IPI_allbutself (int op)
205{
206 unsigned int i;
207
hawkes@sgi.comdc565b52005-10-10 08:43:26 -0700208 for_each_online_cpu(i) {
209 if (i != smp_processor_id())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 send_IPI_single(i, op);
211 }
212}
213
214/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700215 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
217static inline void
Xiantao Zhang31a6b112008-04-03 11:39:43 -0700218send_IPI_mask(cpumask_t mask, int op)
219{
220 unsigned int cpu;
221
222 for_each_cpu_mask(cpu, mask) {
223 send_IPI_single(cpu, op);
224 }
225}
226
227/*
228 * Called with preemption disabled.
229 */
230static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231send_IPI_all (int op)
232{
233 int i;
234
hawkes@sgi.comdc565b52005-10-10 08:43:26 -0700235 for_each_online_cpu(i) {
236 send_IPI_single(i, op);
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700241 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243static inline void
244send_IPI_self (int op)
245{
246 send_IPI_single(smp_processor_id(), op);
247}
248
Horms45a98fc2006-12-12 17:49:03 +0900249#ifdef CONFIG_KEXEC
Zou Nan haia79561132006-12-07 09:51:35 -0800250void
Al Viroccbebda2007-02-09 16:38:10 +0000251kdump_smp_send_stop(void)
Zou Nan haia79561132006-12-07 09:51:35 -0800252{
253 send_IPI_allbutself(IPI_KDUMP_CPU_STOP);
254}
255
256void
Al Viroccbebda2007-02-09 16:38:10 +0000257kdump_smp_send_init(void)
Zou Nan haia79561132006-12-07 09:51:35 -0800258{
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);
265 }
266 }
267}
268#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700270 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 */
272void
273smp_send_reschedule (int cpu)
274{
275 platform_send_ipi(cpu, IA64_IPI_RESCHEDULE, IA64_IPI_DM_INT, 0);
276}
277
Jack Steiner3be44b92007-05-08 14:50:43 -0700278/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700279 * Called with preemption disabled.
Jack Steiner3be44b92007-05-08 14:50:43 -0700280 */
281static void
282smp_send_local_flush_tlb (int cpu)
283{
284 platform_send_ipi(cpu, IA64_IPI_LOCAL_TLB_FLUSH, IA64_IPI_DM_INT, 0);
285}
286
287void
288smp_local_flush_tlb(void)
289{
290 /*
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.
296 */
297 ia64_fetchadd(1, &local_tlb_flush_counts[smp_processor_id()].count, acq);
298 local_flush_tlb_all();
299}
300
301#define FLUSH_DELAY 5 /* Usec backoff to eliminate excessive cacheline bouncing */
302
303void
304smp_flush_tlb_cpumask(cpumask_t xcpumask)
305{
306 unsigned int *counts = __ia64_per_cpu_var(shadow_flush_counts);
307 cpumask_t cpumask = xcpumask;
308 int mycpu, cpu, flush_mycpu = 0;
309
310 preempt_disable();
311 mycpu = smp_processor_id();
312
313 for_each_cpu_mask(cpu, cpumask)
314 counts[cpu] = local_tlb_flush_counts[cpu].count;
315
316 mb();
317 for_each_cpu_mask(cpu, cpumask) {
318 if (cpu == mycpu)
319 flush_mycpu = 1;
320 else
321 smp_send_local_flush_tlb(cpu);
322 }
323
324 if (flush_mycpu)
325 smp_local_flush_tlb();
326
327 for_each_cpu_mask(cpu, cpumask)
328 while(counts[cpu] == local_tlb_flush_counts[cpu].count)
329 udelay(FLUSH_DELAY);
330
331 preempt_enable();
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334void
335smp_flush_tlb_all (void)
336{
337 on_each_cpu((void (*)(void *))local_flush_tlb_all, NULL, 1, 1);
338}
339
340void
341smp_flush_tlb_mm (struct mm_struct *mm)
342{
Peter Chubba68db7632005-06-23 21:14:00 -0700343 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* this happens for the common case of a single-threaded fork(): */
345 if (likely(mm == current->active_mm && atomic_read(&mm->mm_users) == 1))
346 {
347 local_finish_flush_tlb_mm(mm);
Peter Chubba68db7632005-06-23 21:14:00 -0700348 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return;
350 }
351
Peter Chubba68db7632005-06-23 21:14:00 -0700352 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /*
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
358 * rather trivial.
359 */
360 on_each_cpu((void (*)(void *))local_finish_flush_tlb_mm, mm, 1, 1);
361}
362
363/*
Avi Kivity8a2d8692007-07-19 18:32:43 +0300364 * Run a function on a specific CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * <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.
370 *
371 * Does not return until the remote CPU is nearly ready to execute <func>
372 * or is or has executed.
373 */
374
375int
376smp_call_function_single (int cpuid, void (*func) (void *info), void *info, int nonatomic,
377 int wait)
378{
379 struct call_data_struct data;
380 int cpus = 1;
381 int me = get_cpu(); /* prevent preemption and reschedule on another processor */
382
383 if (cpuid == me) {
Avi Kivity8a2d8692007-07-19 18:32:43 +0300384 local_irq_disable();
385 func(info);
386 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 put_cpu();
Avi Kivity8a2d8692007-07-19 18:32:43 +0300388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
391 data.func = func;
392 data.info = info;
393 atomic_set(&data.started, 0);
394 data.wait = wait;
395 if (wait)
396 atomic_set(&data.finished, 0);
397
398 spin_lock_bh(&call_lock);
399
400 call_data = &data;
401 mb(); /* ensure store to call_data precedes setting of IPI_CALL_FUNC */
402 send_IPI_single(cpuid, IPI_CALL_FUNC);
403
404 /* Wait for response */
405 while (atomic_read(&data.started) != cpus)
406 cpu_relax();
407
408 if (wait)
409 while (atomic_read(&data.finished) != cpus)
410 cpu_relax();
411 call_data = NULL;
412
413 spin_unlock_bh(&call_lock);
414 put_cpu();
415 return 0;
416}
417EXPORT_SYMBOL(smp_call_function_single);
418
Xiantao Zhang31a6b112008-04-03 11:39:43 -0700419/**
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.
426 *
427 * Returns 0 on success, else a negative status code.
428 *
429 * If @wait is true, then returns once @func has returned; otherwise
430 * it returns just before the target cpu calls @func.
431 *
432 * You must not call this function with disabled interrupts or from a
433 * hardware interrupt handler or from a bottom half handler.
434 */
435int smp_call_function_mask(cpumask_t mask,
436 void (*func)(void *), void *info,
437 int wait)
438{
439 struct call_data_struct data;
440 cpumask_t allbutself;
441 int cpus;
442
443 spin_lock(&call_lock);
444 allbutself = cpu_online_map;
445 cpu_clear(smp_processor_id(), allbutself);
446
447 cpus_and(mask, mask, allbutself);
448 cpus = cpus_weight(mask);
449 if (!cpus) {
450 spin_unlock(&call_lock);
451 return 0;
452 }
453
454 /* Can deadlock when called with interrupts disabled */
455 WARN_ON(irqs_disabled());
456
457 data.func = func;
458 data.info = info;
459 atomic_set(&data.started, 0);
460 data.wait = wait;
461 if (wait)
462 atomic_set(&data.finished, 0);
463
464 call_data = &data;
465 mb(); /* ensure store to call_data precedes setting of IPI_CALL_FUNC*/
466
467 /* Send a message to other CPUs */
468 if (cpus_equal(mask, allbutself))
469 send_IPI_allbutself(IPI_CALL_FUNC);
470 else
471 send_IPI_mask(mask, IPI_CALL_FUNC);
472
473 /* Wait for response */
474 while (atomic_read(&data.started) != cpus)
475 cpu_relax();
476
477 if (wait)
478 while (atomic_read(&data.finished) != cpus)
479 cpu_relax();
480 call_data = NULL;
481
482 spin_unlock(&call_lock);
483 return 0;
484
485}
486EXPORT_SYMBOL(smp_call_function_mask);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/*
489 * this function sends a 'generic call function' IPI to all other CPUs
490 * in the system.
491 */
492
493/*
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.
500 *
501 * Does not return until remote CPUs are nearly ready to execute <func> or are or have
502 * executed.
503 *
504 * You must not call this function with disabled interrupts or from a
505 * hardware interrupt handler or from a bottom half handler.
506 */
507int
508smp_call_function (void (*func) (void *info), void *info, int nonatomic, int wait)
509{
510 struct call_data_struct data;
Kenji Kaneshige5ee773732006-10-27 19:49:53 +0900511 int cpus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Kenji Kaneshige5ee773732006-10-27 19:49:53 +0900513 spin_lock(&call_lock);
514 cpus = num_online_cpus() - 1;
515 if (!cpus) {
516 spin_unlock(&call_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return 0;
Kenji Kaneshige5ee773732006-10-27 19:49:53 +0900518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /* Can deadlock when called with interrupts disabled */
521 WARN_ON(irqs_disabled());
522
523 data.func = func;
524 data.info = info;
525 atomic_set(&data.started, 0);
526 data.wait = wait;
527 if (wait)
528 atomic_set(&data.finished, 0);
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 call_data = &data;
531 mb(); /* ensure store to call_data precedes setting of IPI_CALL_FUNC */
532 send_IPI_allbutself(IPI_CALL_FUNC);
533
534 /* Wait for response */
535 while (atomic_read(&data.started) != cpus)
536 cpu_relax();
537
538 if (wait)
539 while (atomic_read(&data.finished) != cpus)
540 cpu_relax();
541 call_data = NULL;
542
543 spin_unlock(&call_lock);
544 return 0;
545}
546EXPORT_SYMBOL(smp_call_function);
547
548/*
549 * this function calls the 'stop' function on all other CPUs in the system.
550 */
551void
552smp_send_stop (void)
553{
554 send_IPI_allbutself(IPI_CPU_STOP);
555}
556
Tony Luckcb2e0912007-07-20 16:14:28 -0700557int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558setup_profiling_timer (unsigned int multiplier)
559{
560 return -EINVAL;
561}