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