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