- Separate out show_stack changes into own patch.
[linux-flexiantxendom0-3.2.10.git] / arch / ia64 / kernel / smp.c
1 /*
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 #define __KERNEL_SYSCALLS__
22
23 #include <linux/config.h>
24
25 #include <linux/kernel.h>
26 #include <linux/sched.h>
27 #include <linux/init.h>
28 #include <linux/interrupt.h>
29 #include <linux/smp.h>
30 #include <linux/kernel_stat.h>
31 #include <linux/mm.h>
32 #include <linux/cache.h>
33 #include <linux/delay.h>
34 #include <linux/cache.h>
35 #include <linux/efi.h>
36
37 #include <asm/atomic.h>
38 #include <asm/bitops.h>
39 #include <asm/current.h>
40 #include <asm/delay.h>
41 #include <asm/machvec.h>
42 #include <asm/io.h>
43 #include <asm/irq.h>
44 #include <asm/page.h>
45 #include <asm/pgalloc.h>
46 #include <asm/pgtable.h>
47 #include <asm/processor.h>
48 #include <asm/ptrace.h>
49 #include <asm/sal.h>
50 #include <asm/system.h>
51 #include <asm/tlbflush.h>
52 #include <asm/unistd.h>
53 #include <asm/mca.h>
54
55 /*
56  * Structure and data for smp_call_function(). This is designed to minimise static memory
57  * requirements. It also looks cleaner.
58  */
59 static spinlock_t call_lock __cacheline_aligned = SPIN_LOCK_UNLOCKED;
60
61 struct call_data_struct {
62         void (*func) (void *info);
63         void *info;
64         long wait;
65         atomic_t started;
66         atomic_t finished;
67 };
68
69 static volatile struct call_data_struct *call_data;
70
71 #define IPI_CALL_FUNC           0
72 #define IPI_CPU_STOP            1
73
74 /* This needs to be cacheline aligned because it is written to by *other* CPUs.  */
75 static DEFINE_PER_CPU(__u64, ipi_operation) ____cacheline_aligned;
76
77 static void
78 stop_this_cpu (void)
79 {
80         extern void cpu_halt (void);
81         /*
82          * Remove this CPU:
83          */
84         clear_bit(smp_processor_id(), &cpu_online_map);
85         max_xtp();
86         local_irq_disable();
87         cpu_halt();
88 }
89
90 irqreturn_t
91 handle_IPI (int irq, void *dev_id, struct pt_regs *regs)
92 {
93         int this_cpu = get_cpu();
94         unsigned long *pending_ipis = &__get_cpu_var(ipi_operation);
95         unsigned long ops;
96
97         /* Count this now; we may make a call that never returns. */
98         local_cpu_data->ipi_count++;
99
100         mb();   /* Order interrupt and bit testing. */
101         while ((ops = xchg(pending_ipis, 0)) != 0) {
102                 mb();   /* Order bit clearing and data access. */
103                 do {
104                         unsigned long which;
105
106                         which = ffz(~ops);
107                         ops &= ~(1 << which);
108
109                         switch (which) {
110                               case IPI_CALL_FUNC:
111                               {
112                                       struct call_data_struct *data;
113                                       void (*func)(void *info);
114                                       void *info;
115                                       int wait;
116
117                                       /* release the 'pointer lock' */
118                                       data = (struct call_data_struct *) call_data;
119                                       func = data->func;
120                                       info = data->info;
121                                       wait = data->wait;
122
123                                       mb();
124                                       atomic_inc(&data->started);
125                                       /*
126                                        * At this point the structure may be gone unless
127                                        * wait is true.
128                                        */
129                                       (*func)(info);
130
131                                       /* Notify the sending CPU that the task is done.  */
132                                       mb();
133                                       if (wait)
134                                               atomic_inc(&data->finished);
135                               }
136                               break;
137
138                               case IPI_CPU_STOP:
139                                 stop_this_cpu();
140                                 break;
141
142                               default:
143                                 printk(KERN_CRIT "Unknown IPI on CPU %d: %lu\n", this_cpu, which);
144                                 break;
145                         }
146                 } while (ops);
147                 mb();   /* Order data access and bit testing. */
148         }
149         put_cpu();
150         return IRQ_HANDLED;
151 }
152
153 /*
154  * Called with preeemption disabled.
155  */
156 static inline void
157 send_IPI_single (int dest_cpu, int op)
158 {
159         set_bit(op, &per_cpu(ipi_operation, dest_cpu));
160         platform_send_ipi(dest_cpu, IA64_IPI_VECTOR, IA64_IPI_DM_INT, 0);
161 }
162
163 /*
164  * Called with preeemption disabled.
165  */
166 static inline void
167 send_IPI_allbutself (int op)
168 {
169         unsigned int i;
170
171         for (i = 0; i < NR_CPUS; i++) {
172                 if (cpu_online(i) && i != smp_processor_id())
173                         send_IPI_single(i, op);
174         }
175 }
176
177 /*
178  * Called with preeemption disabled.
179  */
180 static inline void
181 send_IPI_all (int op)
182 {
183         int i;
184
185         for (i = 0; i < NR_CPUS; i++)
186                 if (cpu_online(i))
187                         send_IPI_single(i, op);
188 }
189
190 /*
191  * Called with preeemption disabled.
192  */
193 static inline void
194 send_IPI_self (int op)
195 {
196         send_IPI_single(smp_processor_id(), op);
197 }
198
199 /*
200  * Called with preeemption disabled.
201  */
202 void
203 smp_send_reschedule (int cpu)
204 {
205         platform_send_ipi(cpu, IA64_IPI_RESCHEDULE, IA64_IPI_DM_INT, 0);
206 }
207
208 /*
209  * This function sends a reschedule IPI to all (other) CPUs.  This should only be used if
210  * some 'global' task became runnable, such as a RT task, that must be handled now. The
211  * first CPU that manages to grab the task will run it.
212  */
213 void
214 smp_send_reschedule_all (void)
215 {
216         int i;
217         int cpu = get_cpu(); /* disable preemption */
218
219         for (i = 0; i < NR_CPUS; i++)
220                 if (cpu_online(i) && i != cpu)
221                         smp_send_reschedule(i);
222         put_cpu();
223 }
224
225
226 void
227 smp_flush_tlb_all (void)
228 {
229         on_each_cpu((void (*)(void *))local_flush_tlb_all, 0, 1, 1);
230 }
231
232 void
233 smp_flush_tlb_mm (struct mm_struct *mm)
234 {
235         /* this happens for the common case of a single-threaded fork():  */
236         if (likely(mm == current->active_mm && atomic_read(&mm->mm_users) == 1))
237         {
238                 local_finish_flush_tlb_mm(mm);
239                 return;
240         }
241
242         /*
243          * We could optimize this further by using mm->cpu_vm_mask to track which CPUs
244          * have been running in the address space.  It's not clear that this is worth the
245          * trouble though: to avoid races, we have to raise the IPI on the target CPU
246          * anyhow, and once a CPU is interrupted, the cost of local_flush_tlb_all() is
247          * rather trivial.
248          */
249         on_each_cpu((void (*)(void *))local_finish_flush_tlb_mm, mm, 1, 1);
250 }
251
252 /*
253  * Run a function on another CPU
254  *  <func>      The function to run. This must be fast and non-blocking.
255  *  <info>      An arbitrary pointer to pass to the function.
256  *  <nonatomic> Currently unused.
257  *  <wait>      If true, wait until function has completed on other CPUs.
258  *  [RETURNS]   0 on success, else a negative status code.
259  *
260  * Does not return until the remote CPU is nearly ready to execute <func>
261  * or is or has executed.
262  */
263
264 int
265 smp_call_function_single (int cpuid, void (*func) (void *info), void *info, int nonatomic,
266                           int wait)
267 {
268         struct call_data_struct data;
269         int cpus = 1;
270         int me = get_cpu(); /* prevent preemption and reschedule on another processor */
271
272         if (cpuid == me) {
273                 printk("%s: trying to call self\n", __FUNCTION__);
274                 put_cpu();
275                 return -EBUSY;
276         }
277
278         data.func = func;
279         data.info = info;
280         atomic_set(&data.started, 0);
281         data.wait = wait;
282         if (wait)
283                 atomic_set(&data.finished, 0);
284
285         spin_lock_bh(&call_lock);
286
287         call_data = &data;
288         mb();   /* ensure store to call_data precedes setting of IPI_CALL_FUNC */
289         send_IPI_single(cpuid, IPI_CALL_FUNC);
290
291         /* Wait for response */
292         while (atomic_read(&data.started) != cpus)
293                 barrier();
294
295         if (wait)
296                 while (atomic_read(&data.finished) != cpus)
297                         barrier();
298         call_data = NULL;
299
300         spin_unlock_bh(&call_lock);
301         put_cpu();
302         return 0;
303 }
304
305 /*
306  * this function sends a 'generic call function' IPI to all other CPUs
307  * in the system.
308  */
309
310 /*
311  *  [SUMMARY]   Run a function on all other CPUs.
312  *  <func>      The function to run. This must be fast and non-blocking.
313  *  <info>      An arbitrary pointer to pass to the function.
314  *  <nonatomic> currently unused.
315  *  <wait>      If true, wait (atomically) until function has completed on other CPUs.
316  *  [RETURNS]   0 on success, else a negative status code.
317  *
318  * Does not return until remote CPUs are nearly ready to execute <func> or are or have
319  * executed.
320  *
321  * You must not call this function with disabled interrupts or from a
322  * hardware interrupt handler or from a bottom half handler.
323  */
324 int
325 smp_call_function (void (*func) (void *info), void *info, int nonatomic, int wait)
326 {
327         struct call_data_struct data;
328         int cpus = num_online_cpus()-1;
329
330         if (!cpus)
331                 return 0;
332
333         data.func = func;
334         data.info = info;
335         atomic_set(&data.started, 0);
336         data.wait = wait;
337         if (wait)
338                 atomic_set(&data.finished, 0);
339
340         spin_lock(&call_lock);
341
342         call_data = &data;
343         mb();   /* ensure store to call_data precedes setting of IPI_CALL_FUNC */
344         send_IPI_allbutself(IPI_CALL_FUNC);
345
346         /* Wait for response */
347         while (atomic_read(&data.started) != cpus)
348                 barrier();
349
350         if (wait)
351                 while (atomic_read(&data.finished) != cpus)
352                         barrier();
353         call_data = NULL;
354
355         spin_unlock(&call_lock);
356         return 0;
357 }
358
359 void
360 smp_do_timer (struct pt_regs *regs)
361 {
362         int user = user_mode(regs);
363
364         if (--local_cpu_data->prof_counter <= 0) {
365                 local_cpu_data->prof_counter = local_cpu_data->prof_multiplier;
366                 update_process_times(user);
367         }
368 }
369
370 /*
371  * this function calls the 'stop' function on all other CPUs in the system.
372  */
373 void
374 smp_send_stop (void)
375 {
376         send_IPI_allbutself(IPI_CPU_STOP);
377 }
378
379 int __init
380 setup_profiling_timer (unsigned int multiplier)
381 {
382         return -EINVAL;
383 }