Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mfashe...
[pandora-kernel.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 #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>
33 #include <linux/kexec.h>
34
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 /*
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.
56  */
57 static struct local_tlb_flush_counts {
58         unsigned int count;
59 } __attribute__((__aligned__(32))) local_tlb_flush_counts[NR_CPUS];
60
61 static DEFINE_PER_CPU(unsigned int, shadow_flush_counts[NR_CPUS]) ____cacheline_aligned;
62
63
64 /*
65  * Structure and data for smp_call_function(). This is designed to minimise static memory
66  * requirements. It also looks cleaner.
67  */
68 static  __cacheline_aligned DEFINE_SPINLOCK(call_lock);
69
70 struct call_data_struct {
71         void (*func) (void *info);
72         void *info;
73         long wait;
74         atomic_t started;
75         atomic_t finished;
76 };
77
78 static volatile struct call_data_struct *call_data;
79
80 #define IPI_CALL_FUNC           0
81 #define IPI_CPU_STOP            1
82 #define IPI_KDUMP_CPU_STOP      3
83
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);
86
87 extern void cpu_halt (void);
88
89 void
90 lock_ipi_calllock(void)
91 {
92         spin_lock_irq(&call_lock);
93 }
94
95 void
96 unlock_ipi_calllock(void)
97 {
98         spin_unlock_irq(&call_lock);
99 }
100
101 static inline void
102 handle_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
126 static void
127 stop_this_cpu(void)
128 {
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
138 void
139 cpu_die(void)
140 {
141         max_xtp();
142         local_irq_disable();
143         cpu_halt();
144         /* Should never be here */
145         BUG();
146         for (;;);
147 }
148
149 irqreturn_t
150 handle_IPI (int irq, void *dev_id)
151 {
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) {
166                         case IPI_CALL_FUNC:
167                                 handle_call_data();
168                                 break;
169
170                         case IPI_CPU_STOP:
171                                 stop_this_cpu();
172                                 break;
173 #ifdef CONFIG_KEXEC
174                         case IPI_KDUMP_CPU_STOP:
175                                 unw_init_running(kdump_cpu_freeze, NULL);
176                                 break;
177 #endif
178                         default:
179                                 printk(KERN_CRIT "Unknown IPI on CPU %d: %lu\n",
180                                                 this_cpu, which);
181                                 break;
182                         }
183                 } while (ops);
184                 mb();   /* Order data access and bit testing. */
185         }
186         put_cpu();
187         return IRQ_HANDLED;
188 }
189
190 /*
191  * Called with preemption disabled.
192  */
193 static inline void
194 send_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 /*
201  * Called with preemption disabled.
202  */
203 static inline void
204 send_IPI_allbutself (int op)
205 {
206         unsigned int i;
207
208         for_each_online_cpu(i) {
209                 if (i != smp_processor_id())
210                         send_IPI_single(i, op);
211         }
212 }
213
214 /*
215  * Called with preemption disabled.
216  */
217 static inline void
218 send_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  */
230 static inline void
231 send_IPI_all (int op)
232 {
233         int i;
234
235         for_each_online_cpu(i) {
236                 send_IPI_single(i, op);
237         }
238 }
239
240 /*
241  * Called with preemption disabled.
242  */
243 static inline void
244 send_IPI_self (int op)
245 {
246         send_IPI_single(smp_processor_id(), op);
247 }
248
249 #ifdef CONFIG_KEXEC
250 void
251 kdump_smp_send_stop(void)
252 {
253         send_IPI_allbutself(IPI_KDUMP_CPU_STOP);
254 }
255
256 void
257 kdump_smp_send_init(void)
258 {
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
269 /*
270  * Called with preemption disabled.
271  */
272 void
273 smp_send_reschedule (int cpu)
274 {
275         platform_send_ipi(cpu, IA64_IPI_RESCHEDULE, IA64_IPI_DM_INT, 0);
276 }
277
278 /*
279  * Called with preemption disabled.
280  */
281 static void
282 smp_send_local_flush_tlb (int cpu)
283 {
284         platform_send_ipi(cpu, IA64_IPI_LOCAL_TLB_FLUSH, IA64_IPI_DM_INT, 0);
285 }
286
287 void
288 smp_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
303 void
304 smp_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
334 void
335 smp_flush_tlb_all (void)
336 {
337         on_each_cpu((void (*)(void *))local_flush_tlb_all, NULL, 1, 1);
338 }
339
340 void
341 smp_flush_tlb_mm (struct mm_struct *mm)
342 {
343         preempt_disable();
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))
346         {
347                 local_finish_flush_tlb_mm(mm);
348                 preempt_enable();
349                 return;
350         }
351
352         preempt_enable();
353         /*
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 /*
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.
370  *
371  * Does not return until the remote CPU is nearly ready to execute <func>
372  * or is or has executed.
373  */
374
375 int
376 smp_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) {
384                 local_irq_disable();
385                 func(info);
386                 local_irq_enable();
387                 put_cpu();
388                 return 0;
389         }
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 }
417 EXPORT_SYMBOL(smp_call_function_single);
418
419 /**
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  */
435 int 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 }
486 EXPORT_SYMBOL(smp_call_function_mask);
487
488 /*
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  */
507 int
508 smp_call_function (void (*func) (void *info), void *info, int nonatomic, int wait)
509 {
510         struct call_data_struct data;
511         int cpus;
512
513         spin_lock(&call_lock);
514         cpus = num_online_cpus() - 1;
515         if (!cpus) {
516                 spin_unlock(&call_lock);
517                 return 0;
518         }
519
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
530         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 }
546 EXPORT_SYMBOL(smp_call_function);
547
548 /*
549  * this function calls the 'stop' function on all other CPUs in the system.
550  */
551 void
552 smp_send_stop (void)
553 {
554         send_IPI_allbutself(IPI_CPU_STOP);
555 }
556
557 int
558 setup_profiling_timer (unsigned int multiplier)
559 {
560         return -EINVAL;
561 }