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