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