vserver 1.9.5.x5
[linux-2.6.git] / arch / ppc / kernel / smp.c
1 /*
2  * Smp support for ppc.
3  *
4  * Written by Cort Dougan (cort@cs.nmt.edu) borrowing a great
5  * deal of code from the sparc and intel versions.
6  *
7  * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
8  *
9  */
10
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/delay.h>
20 #include <linux/init.h>
21 #include <linux/spinlock.h>
22 #include <linux/cache.h>
23
24 #include <asm/ptrace.h>
25 #include <asm/atomic.h>
26 #include <asm/irq.h>
27 #include <asm/page.h>
28 #include <asm/pgtable.h>
29 #include <asm/io.h>
30 #include <asm/prom.h>
31 #include <asm/smp.h>
32 #include <asm/residual.h>
33 #include <asm/time.h>
34 #include <asm/thread_info.h>
35 #include <asm/tlbflush.h>
36 #include <asm/xmon.h>
37
38 int smp_threads_ready;
39 volatile int smp_commenced;
40 int smp_tb_synchronized;
41 struct cpuinfo_PPC cpu_data[NR_CPUS];
42 struct klock_info_struct klock_info = { KLOCK_CLEAR, 0 };
43 atomic_t ipi_recv;
44 atomic_t ipi_sent;
45 unsigned long cache_decay_ticks = HZ/100;
46 cpumask_t cpu_online_map;
47 cpumask_t cpu_possible_map;
48 int smp_hw_index[NR_CPUS];
49 struct thread_info *secondary_ti;
50
51 EXPORT_SYMBOL(cpu_online_map);
52 EXPORT_SYMBOL(cpu_possible_map);
53
54 /* SMP operations for this machine */
55 static struct smp_ops_t *smp_ops;
56
57 /* all cpu mappings are 1-1 -- Cort */
58 volatile unsigned long cpu_callin_map[NR_CPUS];
59
60 int start_secondary(void *);
61 void smp_call_function_interrupt(void);
62 static int __smp_call_function(void (*func) (void *info), void *info,
63                                int wait, int target);
64
65 /* Low level assembly function used to backup CPU 0 state */
66 extern void __save_cpu_setup(void);
67
68 /* Since OpenPIC has only 4 IPIs, we use slightly different message numbers.
69  *
70  * Make sure this matches openpic_request_IPIs in open_pic.c, or what shows up
71  * in /proc/interrupts will be wrong!!! --Troy */
72 #define PPC_MSG_CALL_FUNCTION   0
73 #define PPC_MSG_RESCHEDULE      1
74 #define PPC_MSG_INVALIDATE_TLB  2
75 #define PPC_MSG_XMON_BREAK      3
76
77 static inline void
78 smp_message_pass(int target, int msg, unsigned long data, int wait)
79 {
80         if (smp_ops){
81                 atomic_inc(&ipi_sent);
82                 smp_ops->message_pass(target,msg,data,wait);
83         }
84 }
85
86 /*
87  * Common functions
88  */
89 void smp_message_recv(int msg, struct pt_regs *regs)
90 {
91         atomic_inc(&ipi_recv);
92
93         switch( msg ) {
94         case PPC_MSG_CALL_FUNCTION:
95                 smp_call_function_interrupt();
96                 break;
97         case PPC_MSG_RESCHEDULE:
98                 set_need_resched();
99                 break;
100         case PPC_MSG_INVALIDATE_TLB:
101                 _tlbia();
102                 break;
103 #ifdef CONFIG_XMON
104         case PPC_MSG_XMON_BREAK:
105                 xmon(regs);
106                 break;
107 #endif /* CONFIG_XMON */
108         default:
109                 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
110                        smp_processor_id(), msg);
111                 break;
112         }
113 }
114
115 /*
116  * 750's don't broadcast tlb invalidates so
117  * we have to emulate that behavior.
118  *   -- Cort
119  */
120 void smp_send_tlb_invalidate(int cpu)
121 {
122         if ( PVR_VER(mfspr(PVR)) == 8 )
123                 smp_message_pass(MSG_ALL_BUT_SELF, PPC_MSG_INVALIDATE_TLB, 0, 0);
124 }
125
126 void smp_send_reschedule(int cpu)
127 {
128         /*
129          * This is only used if `cpu' is running an idle task,
130          * so it will reschedule itself anyway...
131          *
132          * This isn't the case anymore since the other CPU could be
133          * sleeping and won't reschedule until the next interrupt (such
134          * as the timer).
135          *  -- Cort
136          */
137         /* This is only used if `cpu' is running an idle task,
138            so it will reschedule itself anyway... */
139         smp_message_pass(cpu, PPC_MSG_RESCHEDULE, 0, 0);
140 }
141
142 #ifdef CONFIG_XMON
143 void smp_send_xmon_break(int cpu)
144 {
145         smp_message_pass(cpu, PPC_MSG_XMON_BREAK, 0, 0);
146 }
147 #endif /* CONFIG_XMON */
148
149 static void stop_this_cpu(void *dummy)
150 {
151         local_irq_disable();
152         while (1)
153                 ;
154 }
155
156 void smp_send_stop(void)
157 {
158         smp_call_function(stop_this_cpu, NULL, 1, 0);
159 }
160
161 /*
162  * Structure and data for smp_call_function(). This is designed to minimise
163  * static memory requirements. It also looks cleaner.
164  * Stolen from the i386 version.
165  */
166 static DEFINE_SPINLOCK(call_lock);
167
168 static struct call_data_struct {
169         void (*func) (void *info);
170         void *info;
171         atomic_t started;
172         atomic_t finished;
173         int wait;
174 } *call_data;
175
176 /*
177  * this function sends a 'generic call function' IPI to all other CPUs
178  * in the system.
179  */
180
181 int smp_call_function(void (*func) (void *info), void *info, int nonatomic,
182                       int wait)
183 /*
184  * [SUMMARY] Run a function on all other CPUs.
185  * <func> The function to run. This must be fast and non-blocking.
186  * <info> An arbitrary pointer to pass to the function.
187  * <nonatomic> currently unused.
188  * <wait> If true, wait (atomically) until function has completed on other CPUs.
189  * [RETURNS] 0 on success, else a negative status code. Does not return until
190  * remote CPUs are nearly ready to execute <<func>> or are or have executed.
191  *
192  * You must not call this function with disabled interrupts or from a
193  * hardware interrupt handler or from a bottom half handler.
194  */
195 {
196         /* FIXME: get cpu lock with hotplug cpus, or change this to
197            bitmask. --RR */
198         if (num_online_cpus() <= 1)
199                 return 0;
200         /* Can deadlock when called with interrupts disabled */
201         WARN_ON(irqs_disabled());
202         return __smp_call_function(func, info, wait, MSG_ALL_BUT_SELF);
203 }
204
205 static int __smp_call_function(void (*func) (void *info), void *info,
206                                int wait, int target)
207 {
208         struct call_data_struct data;
209         int ret = -1;
210         int timeout;
211         int ncpus = 1;
212
213         if (target == MSG_ALL_BUT_SELF)
214                 ncpus = num_online_cpus() - 1;
215         else if (target == MSG_ALL)
216                 ncpus = num_online_cpus();
217
218         data.func = func;
219         data.info = info;
220         atomic_set(&data.started, 0);
221         data.wait = wait;
222         if (wait)
223                 atomic_set(&data.finished, 0);
224
225         spin_lock(&call_lock);
226         call_data = &data;
227         /* Send a message to all other CPUs and wait for them to respond */
228         smp_message_pass(target, PPC_MSG_CALL_FUNCTION, 0, 0);
229
230         /* Wait for response */
231         timeout = 1000000;
232         while (atomic_read(&data.started) != ncpus) {
233                 if (--timeout == 0) {
234                         printk("smp_call_function on cpu %d: other cpus not responding (%d)\n",
235                                smp_processor_id(), atomic_read(&data.started));
236                         goto out;
237                 }
238                 barrier();
239                 udelay(1);
240         }
241
242         if (wait) {
243                 timeout = 1000000;
244                 while (atomic_read(&data.finished) != ncpus) {
245                         if (--timeout == 0) {
246                                 printk("smp_call_function on cpu %d: other cpus not finishing (%d/%d)\n",
247                                        smp_processor_id(), atomic_read(&data.finished), atomic_read(&data.started));
248                                 goto out;
249                         }
250                         barrier();
251                         udelay(1);
252                 }
253         }
254         ret = 0;
255
256  out:
257         spin_unlock(&call_lock);
258         return ret;
259 }
260
261 void smp_call_function_interrupt(void)
262 {
263         void (*func) (void *info) = call_data->func;
264         void *info = call_data->info;
265         int wait = call_data->wait;
266
267         /*
268          * Notify initiating CPU that I've grabbed the data and am
269          * about to execute the function
270          */
271         atomic_inc(&call_data->started);
272         /*
273          * At this point the info structure may be out of scope unless wait==1
274          */
275         (*func)(info);
276         if (wait)
277                 atomic_inc(&call_data->finished);
278 }
279
280 static void __devinit smp_store_cpu_info(int id)
281 {
282         struct cpuinfo_PPC *c = &cpu_data[id];
283
284         /* assume bogomips are same for everything */
285         c->loops_per_jiffy = loops_per_jiffy;
286         c->pvr = mfspr(PVR);
287 }
288
289 void __init smp_prepare_cpus(unsigned int max_cpus)
290 {
291         int num_cpus, i;
292
293         /* Fixup boot cpu */
294         smp_store_cpu_info(smp_processor_id());
295         cpu_callin_map[smp_processor_id()] = 1;
296
297         smp_ops = ppc_md.smp_ops;
298         if (smp_ops == NULL) {
299                 printk("SMP not supported on this machine.\n");
300                 return;
301         }
302
303         /* Probe platform for CPUs: always linear. */
304         num_cpus = smp_ops->probe();
305         for (i = 0; i < num_cpus; ++i)
306                 cpu_set(i, cpu_possible_map);
307
308         /* Backup CPU 0 state */
309         __save_cpu_setup();
310
311         if (smp_ops->space_timers)
312                 smp_ops->space_timers(num_cpus);
313 }
314
315 void __devinit smp_prepare_boot_cpu(void)
316 {
317         cpu_set(smp_processor_id(), cpu_online_map);
318         cpu_set(smp_processor_id(), cpu_possible_map);
319 }
320
321 int __init setup_profiling_timer(unsigned int multiplier)
322 {
323         return 0;
324 }
325
326 /* Processor coming up starts here */
327 int __devinit start_secondary(void *unused)
328 {
329         int cpu;
330
331         atomic_inc(&init_mm.mm_count);
332         current->active_mm = &init_mm;
333
334         cpu = smp_processor_id();
335         smp_store_cpu_info(cpu);
336         set_dec(tb_ticks_per_jiffy);
337         cpu_callin_map[cpu] = 1;
338
339         printk("CPU %i done callin...\n", cpu);
340         smp_ops->setup_cpu(cpu);
341         printk("CPU %i done setup...\n", cpu);
342         local_irq_enable();
343         smp_ops->take_timebase();
344         printk("CPU %i done timebase take...\n", cpu);
345
346         cpu_idle();
347         return 0;
348 }
349
350 int __cpu_up(unsigned int cpu)
351 {
352         struct task_struct *p;
353         char buf[32];
354         int c;
355
356         /* create a process for the processor */
357         /* only regs.msr is actually used, and 0 is OK for it */
358         p = fork_idle(cpu);
359         if (IS_ERR(p))
360                 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
361         secondary_ti = p->thread_info;
362         p->thread_info->cpu = cpu;
363
364         /*
365          * There was a cache flush loop here to flush the cache
366          * to memory for the first 8MB of RAM.  The cache flush
367          * has been pushed into the kick_cpu function for those
368          * platforms that need it.
369          */
370
371         /* wake up cpu */
372         smp_ops->kick_cpu(cpu);
373         
374         /*
375          * wait to see if the cpu made a callin (is actually up).
376          * use this value that I found through experimentation.
377          * -- Cort
378          */
379         for (c = 1000; c && !cpu_callin_map[cpu]; c--)
380                 udelay(100);
381
382         if (!cpu_callin_map[cpu]) {
383                 sprintf(buf, "didn't find cpu %u", cpu);
384                 if (ppc_md.progress) ppc_md.progress(buf, 0x360+cpu);
385                 printk("Processor %u is stuck.\n", cpu);
386                 return -ENOENT;
387         }
388
389         sprintf(buf, "found cpu %u", cpu);
390         if (ppc_md.progress) ppc_md.progress(buf, 0x350+cpu);
391         printk("Processor %d found.\n", cpu);
392
393         smp_ops->give_timebase();
394         cpu_set(cpu, cpu_online_map);
395         return 0;
396 }
397
398 void smp_cpus_done(unsigned int max_cpus)
399 {
400         smp_ops->setup_cpu(0);
401 }