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