blob: 9fcd4e63048f65f5ae638833c7a51b756883acbb [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
Arun Sharma600634972011-07-26 16:09:06 -070035#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/tlbflush.h>
48#include <asm/unistd.h>
49#include <asm/mca.h>
50
51/*
Jack Steiner3be44b92007-05-08 14:50:43 -070052 * Note: alignment of 4 entries/cacheline was empirically determined
53 * to be a good tradeoff between hot cachelines & spreading the array
54 * across too many cacheline.
55 */
56static struct local_tlb_flush_counts {
57 unsigned int count;
58} __attribute__((__aligned__(32))) local_tlb_flush_counts[NR_CPUS];
59
Tejun Heob9bf3122009-06-24 15:13:47 +090060static DEFINE_PER_CPU_SHARED_ALIGNED(unsigned short [NR_CPUS],
61 shadow_flush_counts);
Jack Steiner3be44b92007-05-08 14:50:43 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define IPI_CALL_FUNC 0
64#define IPI_CPU_STOP 1
Jens Axboef27b4332008-06-26 11:22:30 +020065#define IPI_CALL_FUNC_SINGLE 2
Zou Nan haia79561132006-12-07 09:51:35 -080066#define IPI_KDUMP_CPU_STOP 3
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* This needs to be cacheline aligned because it is written to by *other* CPUs. */
Matthew Wilcoxe088a4a2009-05-22 13:49:49 -070069static DEFINE_PER_CPU_SHARED_ALIGNED(unsigned long, ipi_operation);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71extern void cpu_halt (void);
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static void
Hidetoshi Setoc0cd6612008-04-30 18:55:48 +090074stop_this_cpu(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 /*
77 * Remove this CPU:
78 */
Srivatsa S. Bhat7d7f9842012-03-28 14:42:46 -070079 set_cpu_online(smp_processor_id(), false);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 max_xtp();
81 local_irq_disable();
82 cpu_halt();
83}
84
85void
86cpu_die(void)
87{
88 max_xtp();
89 local_irq_disable();
90 cpu_halt();
91 /* Should never be here */
92 BUG();
93 for (;;);
94}
95
96irqreturn_t
Keith Owens024e4f22006-10-18 15:36:49 +100097handle_IPI (int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 int this_cpu = get_cpu();
100 unsigned long *pending_ipis = &__ia64_per_cpu_var(ipi_operation);
101 unsigned long ops;
102
103 mb(); /* Order interrupt and bit testing. */
104 while ((ops = xchg(pending_ipis, 0)) != 0) {
105 mb(); /* Order bit clearing and data access. */
106 do {
107 unsigned long which;
108
109 which = ffz(~ops);
110 ops &= ~(1 << which);
111
112 switch (which) {
Hidetoshi Setoc0cd6612008-04-30 18:55:48 +0900113 case IPI_CPU_STOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 stop_this_cpu();
115 break;
Jens Axboef27b4332008-06-26 11:22:30 +0200116 case IPI_CALL_FUNC:
117 generic_smp_call_function_interrupt();
118 break;
119 case IPI_CALL_FUNC_SINGLE:
120 generic_smp_call_function_single_interrupt();
121 break;
Horms45a98fc2006-12-12 17:49:03 +0900122#ifdef CONFIG_KEXEC
Hidetoshi Setoc0cd6612008-04-30 18:55:48 +0900123 case IPI_KDUMP_CPU_STOP:
Zou Nan haia79561132006-12-07 09:51:35 -0800124 unw_init_running(kdump_cpu_freeze, NULL);
125 break;
126#endif
Hidetoshi Setoc0cd6612008-04-30 18:55:48 +0900127 default:
128 printk(KERN_CRIT "Unknown IPI on CPU %d: %lu\n",
129 this_cpu, which);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 break;
131 }
132 } while (ops);
133 mb(); /* Order data access and bit testing. */
134 }
135 put_cpu();
136 return IRQ_HANDLED;
137}
138
Jens Axboef27b4332008-06-26 11:22:30 +0200139
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700142 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 */
144static inline void
145send_IPI_single (int dest_cpu, int op)
146{
147 set_bit(op, &per_cpu(ipi_operation, dest_cpu));
148 platform_send_ipi(dest_cpu, IA64_IPI_VECTOR, IA64_IPI_DM_INT, 0);
149}
150
151/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700152 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 */
154static inline void
155send_IPI_allbutself (int op)
156{
157 unsigned int i;
158
hawkes@sgi.comdc565b52005-10-10 08:43:26 -0700159 for_each_online_cpu(i) {
160 if (i != smp_processor_id())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 send_IPI_single(i, op);
162 }
163}
164
165/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700166 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
168static inline void
Rusty Russell40fe6972009-03-16 14:12:41 +1030169send_IPI_mask(const struct cpumask *mask, int op)
Xiantao Zhang31a6b112008-04-03 11:39:43 -0700170{
171 unsigned int cpu;
172
Rusty Russell40fe6972009-03-16 14:12:41 +1030173 for_each_cpu(cpu, mask) {
Xiantao Zhang31a6b112008-04-03 11:39:43 -0700174 send_IPI_single(cpu, op);
175 }
176}
177
178/*
179 * Called with preemption disabled.
180 */
181static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182send_IPI_all (int op)
183{
184 int i;
185
hawkes@sgi.comdc565b52005-10-10 08:43:26 -0700186 for_each_online_cpu(i) {
187 send_IPI_single(i, op);
188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700192 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
194static inline void
195send_IPI_self (int op)
196{
197 send_IPI_single(smp_processor_id(), op);
198}
199
Horms45a98fc2006-12-12 17:49:03 +0900200#ifdef CONFIG_KEXEC
Zou Nan haia79561132006-12-07 09:51:35 -0800201void
Al Viroccbebda2007-02-09 16:38:10 +0000202kdump_smp_send_stop(void)
Zou Nan haia79561132006-12-07 09:51:35 -0800203{
204 send_IPI_allbutself(IPI_KDUMP_CPU_STOP);
205}
206
207void
Al Viroccbebda2007-02-09 16:38:10 +0000208kdump_smp_send_init(void)
Zou Nan haia79561132006-12-07 09:51:35 -0800209{
210 unsigned int cpu, self_cpu;
211 self_cpu = smp_processor_id();
212 for_each_online_cpu(cpu) {
213 if (cpu != self_cpu) {
214 if(kdump_status[cpu] == 0)
215 platform_send_ipi(cpu, 0, IA64_IPI_DM_INIT, 0);
216 }
217 }
218}
219#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700221 * Called with preemption disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 */
223void
224smp_send_reschedule (int cpu)
225{
226 platform_send_ipi(cpu, IA64_IPI_RESCHEDULE, IA64_IPI_DM_INT, 0);
227}
Marcelo Tosattic4cb7682009-04-14 18:08:37 -0300228EXPORT_SYMBOL_GPL(smp_send_reschedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Jack Steiner3be44b92007-05-08 14:50:43 -0700230/*
Simon Arlott72fdbdc2007-05-11 14:55:43 -0700231 * Called with preemption disabled.
Jack Steiner3be44b92007-05-08 14:50:43 -0700232 */
233static void
234smp_send_local_flush_tlb (int cpu)
235{
236 platform_send_ipi(cpu, IA64_IPI_LOCAL_TLB_FLUSH, IA64_IPI_DM_INT, 0);
237}
238
239void
240smp_local_flush_tlb(void)
241{
242 /*
243 * Use atomic ops. Otherwise, the load/increment/store sequence from
244 * a "++" operation can have the line stolen between the load & store.
245 * The overhead of the atomic op in negligible in this case & offers
246 * significant benefit for the brief periods where lots of cpus
247 * are simultaneously flushing TLBs.
248 */
249 ia64_fetchadd(1, &local_tlb_flush_counts[smp_processor_id()].count, acq);
250 local_flush_tlb_all();
251}
252
253#define FLUSH_DELAY 5 /* Usec backoff to eliminate excessive cacheline bouncing */
254
255void
256smp_flush_tlb_cpumask(cpumask_t xcpumask)
257{
Robin Holt97653f92008-08-15 09:21:24 -0500258 unsigned short *counts = __ia64_per_cpu_var(shadow_flush_counts);
Jack Steiner3be44b92007-05-08 14:50:43 -0700259 cpumask_t cpumask = xcpumask;
260 int mycpu, cpu, flush_mycpu = 0;
261
262 preempt_disable();
263 mycpu = smp_processor_id();
264
265 for_each_cpu_mask(cpu, cpumask)
Robin Holt97653f92008-08-15 09:21:24 -0500266 counts[cpu] = local_tlb_flush_counts[cpu].count & 0xffff;
Jack Steiner3be44b92007-05-08 14:50:43 -0700267
268 mb();
269 for_each_cpu_mask(cpu, cpumask) {
270 if (cpu == mycpu)
271 flush_mycpu = 1;
272 else
273 smp_send_local_flush_tlb(cpu);
274 }
275
276 if (flush_mycpu)
277 smp_local_flush_tlb();
278
279 for_each_cpu_mask(cpu, cpumask)
Robin Holt97653f92008-08-15 09:21:24 -0500280 while(counts[cpu] == (local_tlb_flush_counts[cpu].count & 0xffff))
Jack Steiner3be44b92007-05-08 14:50:43 -0700281 udelay(FLUSH_DELAY);
282
283 preempt_enable();
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286void
287smp_flush_tlb_all (void)
288{
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200289 on_each_cpu((void (*)(void *))local_flush_tlb_all, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
292void
293smp_flush_tlb_mm (struct mm_struct *mm)
294{
Dimitri Sivanich75c1c912010-12-28 13:34:42 -0600295 cpumask_var_t cpus;
Peter Chubba68db7632005-06-23 21:14:00 -0700296 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* this happens for the common case of a single-threaded fork(): */
298 if (likely(mm == current->active_mm && atomic_read(&mm->mm_users) == 1))
299 {
300 local_finish_flush_tlb_mm(mm);
Peter Chubba68db7632005-06-23 21:14:00 -0700301 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return;
303 }
Dimitri Sivanich75c1c912010-12-28 13:34:42 -0600304 if (!alloc_cpumask_var(&cpus, GFP_ATOMIC)) {
305 smp_call_function((void (*)(void *))local_finish_flush_tlb_mm,
306 mm, 1);
307 } else {
308 cpumask_copy(cpus, mm_cpumask(mm));
309 smp_call_function_many(cpus,
310 (void (*)(void *))local_finish_flush_tlb_mm, mm, 1);
311 free_cpumask_var(cpus);
312 }
Dimitri Sivanichedb91dc2009-04-15 10:56:25 -0500313 local_irq_disable();
314 local_finish_flush_tlb_mm(mm);
315 local_irq_enable();
Peter Chubba68db7632005-06-23 21:14:00 -0700316 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Jens Axboef27b4332008-06-26 11:22:30 +0200319void arch_send_call_function_single_ipi(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Jens Axboef27b4332008-06-26 11:22:30 +0200321 send_IPI_single(cpu, IPI_CALL_FUNC_SINGLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Rusty Russell40fe6972009-03-16 14:12:41 +1030324void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Xiantao Zhang31a6b112008-04-03 11:39:43 -0700325{
Jens Axboef27b4332008-06-26 11:22:30 +0200326 send_IPI_mask(mask, IPI_CALL_FUNC);
Xiantao Zhang31a6b112008-04-03 11:39:43 -0700327}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329/*
330 * this function calls the 'stop' function on all other CPUs in the system.
331 */
332void
333smp_send_stop (void)
334{
335 send_IPI_allbutself(IPI_CPU_STOP);
336}
337
Tony Luckcb2e0912007-07-20 16:14:28 -0700338int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339setup_profiling_timer (unsigned int multiplier)
340{
341 return -EINVAL;
342}