Merge branch 'merge' into powerpc-next
[pandora-kernel.git] / arch / powerpc / 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  * PowerPC-64 Support added by Dave Engebretsen, Peter Bergner, and
10  * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
11  *
12  *      This program is free software; you can redistribute it and/or
13  *      modify it under the terms of the GNU General Public License
14  *      as published by the Free Software Foundation; either version
15  *      2 of the License, or (at your option) any later version.
16  */
17
18 #undef DEBUG
19
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/sched.h>
23 #include <linux/smp.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/init.h>
27 #include <linux/spinlock.h>
28 #include <linux/cache.h>
29 #include <linux/err.h>
30 #include <linux/sysdev.h>
31 #include <linux/cpu.h>
32 #include <linux/notifier.h>
33 #include <linux/topology.h>
34
35 #include <asm/ptrace.h>
36 #include <asm/atomic.h>
37 #include <asm/irq.h>
38 #include <asm/page.h>
39 #include <asm/pgtable.h>
40 #include <asm/prom.h>
41 #include <asm/smp.h>
42 #include <asm/time.h>
43 #include <asm/machdep.h>
44 #include <asm/cputable.h>
45 #include <asm/system.h>
46 #include <asm/mpic.h>
47 #include <asm/vdso_datapage.h>
48 #ifdef CONFIG_PPC64
49 #include <asm/paca.h>
50 #endif
51
52 #ifdef DEBUG
53 #include <asm/udbg.h>
54 #define DBG(fmt...) udbg_printf(fmt)
55 #else
56 #define DBG(fmt...)
57 #endif
58
59 int smp_hw_index[NR_CPUS];
60 struct thread_info *secondary_ti;
61
62 cpumask_t cpu_possible_map = CPU_MASK_NONE;
63 cpumask_t cpu_online_map = CPU_MASK_NONE;
64 DEFINE_PER_CPU(cpumask_t, cpu_sibling_map) = CPU_MASK_NONE;
65
66 EXPORT_SYMBOL(cpu_online_map);
67 EXPORT_SYMBOL(cpu_possible_map);
68 EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
69
70 /* SMP operations for this machine */
71 struct smp_ops_t *smp_ops;
72
73 static volatile unsigned int cpu_callin_map[NR_CPUS];
74
75 void smp_call_function_interrupt(void);
76
77 int smt_enabled_at_boot = 1;
78
79 static int ipi_fail_ok;
80
81 static void (*crash_ipi_function_ptr)(struct pt_regs *) = NULL;
82
83 #ifdef CONFIG_PPC64
84 void __devinit smp_generic_kick_cpu(int nr)
85 {
86         BUG_ON(nr < 0 || nr >= NR_CPUS);
87
88         /*
89          * The processor is currently spinning, waiting for the
90          * cpu_start field to become non-zero After we set cpu_start,
91          * the processor will continue on to secondary_start
92          */
93         paca[nr].cpu_start = 1;
94         smp_mb();
95 }
96 #endif
97
98 void smp_message_recv(int msg)
99 {
100         switch(msg) {
101         case PPC_MSG_CALL_FUNCTION:
102                 smp_call_function_interrupt();
103                 break;
104         case PPC_MSG_RESCHEDULE:
105                 /* XXX Do we have to do this? */
106                 set_need_resched();
107                 break;
108         case PPC_MSG_DEBUGGER_BREAK:
109                 if (crash_ipi_function_ptr) {
110                         crash_ipi_function_ptr(get_irq_regs());
111                         break;
112                 }
113 #ifdef CONFIG_DEBUGGER
114                 debugger_ipi(get_irq_regs());
115                 break;
116 #endif /* CONFIG_DEBUGGER */
117                 /* FALLTHROUGH */
118         default:
119                 printk("SMP %d: smp_message_recv(): unknown msg %d\n",
120                        smp_processor_id(), msg);
121                 break;
122         }
123 }
124
125 void smp_send_reschedule(int cpu)
126 {
127         if (likely(smp_ops))
128                 smp_ops->message_pass(cpu, PPC_MSG_RESCHEDULE);
129 }
130
131 #ifdef CONFIG_DEBUGGER
132 void smp_send_debugger_break(int cpu)
133 {
134         if (likely(smp_ops))
135                 smp_ops->message_pass(cpu, PPC_MSG_DEBUGGER_BREAK);
136 }
137 #endif
138
139 #ifdef CONFIG_KEXEC
140 void crash_send_ipi(void (*crash_ipi_callback)(struct pt_regs *))
141 {
142         crash_ipi_function_ptr = crash_ipi_callback;
143         if (crash_ipi_callback && smp_ops) {
144                 mb();
145                 smp_ops->message_pass(MSG_ALL_BUT_SELF, PPC_MSG_DEBUGGER_BREAK);
146         }
147 }
148 #endif
149
150 static void stop_this_cpu(void *dummy)
151 {
152         local_irq_disable();
153         while (1)
154                 ;
155 }
156
157 /*
158  * Structure and data for smp_call_function(). This is designed to minimise
159  * static memory requirements. It also looks cleaner.
160  * Stolen from the i386 version.
161  */
162 static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_lock);
163
164 static struct call_data_struct {
165         void (*func) (void *info);
166         void *info;
167         atomic_t started;
168         atomic_t finished;
169         int wait;
170 } *call_data;
171
172 /* delay of at least 8 seconds */
173 #define SMP_CALL_TIMEOUT        8
174
175 /*
176  * These functions send a 'generic call function' IPI to other online
177  * CPUS in the system.
178  *
179  * [SUMMARY] Run a function on other CPUs.
180  * <func> The function to run. This must be fast and non-blocking.
181  * <info> An arbitrary pointer to pass to the function.
182  * <nonatomic> currently unused.
183  * <wait> If true, wait (atomically) until function has completed on other CPUs.
184  * [RETURNS] 0 on success, else a negative status code. Does not return until
185  * remote CPUs are nearly ready to execute <<func>> or are or have executed.
186  * <map> is a cpu map of the cpus to send IPI to.
187  *
188  * You must not call this function with disabled interrupts or from a
189  * hardware interrupt handler or from a bottom half handler.
190  */
191 static int __smp_call_function_map(void (*func) (void *info), void *info,
192                                    int nonatomic, int wait, cpumask_t map)
193 {
194         struct call_data_struct data;
195         int ret = -1, num_cpus;
196         int cpu;
197         u64 timeout;
198
199         if (unlikely(smp_ops == NULL))
200                 return ret;
201
202         data.func = func;
203         data.info = info;
204         atomic_set(&data.started, 0);
205         data.wait = wait;
206         if (wait)
207                 atomic_set(&data.finished, 0);
208
209         /* remove 'self' from the map */
210         if (cpu_isset(smp_processor_id(), map))
211                 cpu_clear(smp_processor_id(), map);
212
213         /* sanity check the map, remove any non-online processors. */
214         cpus_and(map, map, cpu_online_map);
215
216         num_cpus = cpus_weight(map);
217         if (!num_cpus)
218                 goto done;
219
220         call_data = &data;
221         smp_wmb();
222         /* Send a message to all CPUs in the map */
223         for_each_cpu_mask(cpu, map)
224                 smp_ops->message_pass(cpu, PPC_MSG_CALL_FUNCTION);
225
226         timeout = get_tb() + (u64) SMP_CALL_TIMEOUT * tb_ticks_per_sec;
227
228         /* Wait for indication that they have received the message */
229         while (atomic_read(&data.started) != num_cpus) {
230                 HMT_low();
231                 if (get_tb() >= timeout) {
232                         printk("smp_call_function on cpu %d: other cpus not "
233                                 "responding (%d)\n", smp_processor_id(),
234                                 atomic_read(&data.started));
235                         if (!ipi_fail_ok)
236                                 debugger(NULL);
237                         goto out;
238                 }
239         }
240
241         /* optionally wait for the CPUs to complete */
242         if (wait) {
243                 while (atomic_read(&data.finished) != num_cpus) {
244                         HMT_low();
245                         if (get_tb() >= timeout) {
246                                 printk("smp_call_function on cpu %d: other "
247                                         "cpus not finishing (%d/%d)\n",
248                                         smp_processor_id(),
249                                         atomic_read(&data.finished),
250                                         atomic_read(&data.started));
251                                 debugger(NULL);
252                                 goto out;
253                         }
254                 }
255         }
256
257  done:
258         ret = 0;
259
260  out:
261         call_data = NULL;
262         HMT_medium();
263         return ret;
264 }
265
266 static int __smp_call_function(void (*func)(void *info), void *info,
267                                int nonatomic, int wait)
268 {
269         int ret;
270         spin_lock(&call_lock);
271         ret =__smp_call_function_map(func, info, nonatomic, wait,
272                                        cpu_online_map);
273         spin_unlock(&call_lock);
274         return ret;
275 }
276
277 int smp_call_function(void (*func) (void *info), void *info, int nonatomic,
278                         int wait)
279 {
280         /* Can deadlock when called with interrupts disabled */
281         WARN_ON(irqs_disabled());
282
283         return __smp_call_function(func, info, nonatomic, wait);
284 }
285 EXPORT_SYMBOL(smp_call_function);
286
287 int smp_call_function_single(int cpu, void (*func) (void *info), void *info,
288                              int nonatomic, int wait)
289 {
290         cpumask_t map = CPU_MASK_NONE;
291         int ret = 0;
292
293         /* Can deadlock when called with interrupts disabled */
294         WARN_ON(irqs_disabled());
295
296         if (!cpu_online(cpu))
297                 return -EINVAL;
298
299         cpu_set(cpu, map);
300         if (cpu != get_cpu()) {
301                 spin_lock(&call_lock);
302                 ret = __smp_call_function_map(func, info, nonatomic, wait, map);
303                 spin_unlock(&call_lock);
304         } else {
305                 local_irq_disable();
306                 func(info);
307                 local_irq_enable();
308         }
309         put_cpu();
310         return ret;
311 }
312 EXPORT_SYMBOL(smp_call_function_single);
313
314 void smp_send_stop(void)
315 {
316         int nolock;
317
318         /* It's OK to fail sending the IPI, since the alternative is to
319          * be stuck forever waiting on the other CPU to take the interrupt.
320          *
321          * It's better to at least continue and go through reboot, since this
322          * function is usually called at panic or reboot time in the first
323          * place.
324          */
325         ipi_fail_ok = 1;
326
327         /* Don't deadlock in case we got called through panic */
328         nolock = !spin_trylock(&call_lock);
329         __smp_call_function_map(stop_this_cpu, NULL, 1, 0, cpu_online_map);
330         if (!nolock)
331                 spin_unlock(&call_lock);
332 }
333
334 void smp_call_function_interrupt(void)
335 {
336         void (*func) (void *info);
337         void *info;
338         int wait;
339
340         /* call_data will be NULL if the sender timed out while
341          * waiting on us to receive the call.
342          */
343         if (!call_data)
344                 return;
345
346         func = call_data->func;
347         info = call_data->info;
348         wait = call_data->wait;
349
350         if (!wait)
351                 smp_mb__before_atomic_inc();
352
353         /*
354          * Notify initiating CPU that I've grabbed the data and am
355          * about to execute the function
356          */
357         atomic_inc(&call_data->started);
358         /*
359          * At this point the info structure may be out of scope unless wait==1
360          */
361         (*func)(info);
362         if (wait) {
363                 smp_mb__before_atomic_inc();
364                 atomic_inc(&call_data->finished);
365         }
366 }
367
368 struct thread_info *current_set[NR_CPUS];
369
370 static void __devinit smp_store_cpu_info(int id)
371 {
372         per_cpu(pvr, id) = mfspr(SPRN_PVR);
373 }
374
375 static void __init smp_create_idle(unsigned int cpu)
376 {
377         struct task_struct *p;
378
379         /* create a process for the processor */
380         p = fork_idle(cpu);
381         if (IS_ERR(p))
382                 panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
383 #ifdef CONFIG_PPC64
384         paca[cpu].__current = p;
385         paca[cpu].kstack = (unsigned long) task_thread_info(p)
386                 + THREAD_SIZE - STACK_FRAME_OVERHEAD;
387 #endif
388         current_set[cpu] = task_thread_info(p);
389         task_thread_info(p)->cpu = cpu;
390 }
391
392 void __init smp_prepare_cpus(unsigned int max_cpus)
393 {
394         unsigned int cpu;
395
396         DBG("smp_prepare_cpus\n");
397
398         /* 
399          * setup_cpu may need to be called on the boot cpu. We havent
400          * spun any cpus up but lets be paranoid.
401          */
402         BUG_ON(boot_cpuid != smp_processor_id());
403
404         /* Fixup boot cpu */
405         smp_store_cpu_info(boot_cpuid);
406         cpu_callin_map[boot_cpuid] = 1;
407
408         if (smp_ops)
409                 max_cpus = smp_ops->probe();
410         else
411                 max_cpus = 1;
412  
413         smp_space_timers(max_cpus);
414
415         for_each_possible_cpu(cpu)
416                 if (cpu != boot_cpuid)
417                         smp_create_idle(cpu);
418 }
419
420 void __devinit smp_prepare_boot_cpu(void)
421 {
422         BUG_ON(smp_processor_id() != boot_cpuid);
423
424         cpu_set(boot_cpuid, cpu_online_map);
425 #ifdef CONFIG_PPC64
426         paca[boot_cpuid].__current = current;
427 #endif
428         current_set[boot_cpuid] = task_thread_info(current);
429 }
430
431 #ifdef CONFIG_HOTPLUG_CPU
432 /* State of each CPU during hotplug phases */
433 DEFINE_PER_CPU(int, cpu_state) = { 0 };
434
435 int generic_cpu_disable(void)
436 {
437         unsigned int cpu = smp_processor_id();
438
439         if (cpu == boot_cpuid)
440                 return -EBUSY;
441
442         cpu_clear(cpu, cpu_online_map);
443 #ifdef CONFIG_PPC64
444         vdso_data->processorCount--;
445         fixup_irqs(cpu_online_map);
446 #endif
447         return 0;
448 }
449
450 int generic_cpu_enable(unsigned int cpu)
451 {
452         /* Do the normal bootup if we haven't
453          * already bootstrapped. */
454         if (system_state != SYSTEM_RUNNING)
455                 return -ENOSYS;
456
457         /* get the target out of it's holding state */
458         per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
459         smp_wmb();
460
461         while (!cpu_online(cpu))
462                 cpu_relax();
463
464 #ifdef CONFIG_PPC64
465         fixup_irqs(cpu_online_map);
466         /* counter the irq disable in fixup_irqs */
467         local_irq_enable();
468 #endif
469         return 0;
470 }
471
472 void generic_cpu_die(unsigned int cpu)
473 {
474         int i;
475
476         for (i = 0; i < 100; i++) {
477                 smp_rmb();
478                 if (per_cpu(cpu_state, cpu) == CPU_DEAD)
479                         return;
480                 msleep(100);
481         }
482         printk(KERN_ERR "CPU%d didn't die...\n", cpu);
483 }
484
485 void generic_mach_cpu_die(void)
486 {
487         unsigned int cpu;
488
489         local_irq_disable();
490         cpu = smp_processor_id();
491         printk(KERN_DEBUG "CPU%d offline\n", cpu);
492         __get_cpu_var(cpu_state) = CPU_DEAD;
493         smp_wmb();
494         while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
495                 cpu_relax();
496         cpu_set(cpu, cpu_online_map);
497         local_irq_enable();
498 }
499 #endif
500
501 static int __devinit cpu_enable(unsigned int cpu)
502 {
503         if (smp_ops && smp_ops->cpu_enable)
504                 return smp_ops->cpu_enable(cpu);
505
506         return -ENOSYS;
507 }
508
509 int __cpuinit __cpu_up(unsigned int cpu)
510 {
511         int c;
512
513         secondary_ti = current_set[cpu];
514         if (!cpu_enable(cpu))
515                 return 0;
516
517         if (smp_ops == NULL ||
518             (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu)))
519                 return -EINVAL;
520
521         /* Make sure callin-map entry is 0 (can be leftover a CPU
522          * hotplug
523          */
524         cpu_callin_map[cpu] = 0;
525
526         /* The information for processor bringup must
527          * be written out to main store before we release
528          * the processor.
529          */
530         smp_mb();
531
532         /* wake up cpus */
533         DBG("smp: kicking cpu %d\n", cpu);
534         smp_ops->kick_cpu(cpu);
535
536         /*
537          * wait to see if the cpu made a callin (is actually up).
538          * use this value that I found through experimentation.
539          * -- Cort
540          */
541         if (system_state < SYSTEM_RUNNING)
542                 for (c = 50000; c && !cpu_callin_map[cpu]; c--)
543                         udelay(100);
544 #ifdef CONFIG_HOTPLUG_CPU
545         else
546                 /*
547                  * CPUs can take much longer to come up in the
548                  * hotplug case.  Wait five seconds.
549                  */
550                 for (c = 25; c && !cpu_callin_map[cpu]; c--) {
551                         msleep(200);
552                 }
553 #endif
554
555         if (!cpu_callin_map[cpu]) {
556                 printk("Processor %u is stuck.\n", cpu);
557                 return -ENOENT;
558         }
559
560         printk("Processor %u found.\n", cpu);
561
562         if (smp_ops->give_timebase)
563                 smp_ops->give_timebase();
564
565         /* Wait until cpu puts itself in the online map */
566         while (!cpu_online(cpu))
567                 cpu_relax();
568
569         return 0;
570 }
571
572
573 /* Activate a secondary processor. */
574 int __devinit start_secondary(void *unused)
575 {
576         unsigned int cpu = smp_processor_id();
577
578         atomic_inc(&init_mm.mm_count);
579         current->active_mm = &init_mm;
580
581         smp_store_cpu_info(cpu);
582         set_dec(tb_ticks_per_jiffy);
583         preempt_disable();
584         cpu_callin_map[cpu] = 1;
585
586         smp_ops->setup_cpu(cpu);
587         if (smp_ops->take_timebase)
588                 smp_ops->take_timebase();
589
590         if (system_state > SYSTEM_BOOTING)
591                 snapshot_timebase();
592
593         secondary_cpu_time_init();
594
595         spin_lock(&call_lock);
596         cpu_set(cpu, cpu_online_map);
597         spin_unlock(&call_lock);
598
599         local_irq_enable();
600
601         cpu_idle();
602         return 0;
603 }
604
605 int setup_profiling_timer(unsigned int multiplier)
606 {
607         return 0;
608 }
609
610 void __init smp_cpus_done(unsigned int max_cpus)
611 {
612         cpumask_t old_mask;
613
614         /* We want the setup_cpu() here to be called from CPU 0, but our
615          * init thread may have been "borrowed" by another CPU in the meantime
616          * se we pin us down to CPU 0 for a short while
617          */
618         old_mask = current->cpus_allowed;
619         set_cpus_allowed(current, cpumask_of_cpu(boot_cpuid));
620         
621         if (smp_ops)
622                 smp_ops->setup_cpu(boot_cpuid);
623
624         set_cpus_allowed(current, old_mask);
625
626         snapshot_timebases();
627
628         dump_numa_cpu_topology();
629 }
630
631 #ifdef CONFIG_HOTPLUG_CPU
632 int __cpu_disable(void)
633 {
634         if (smp_ops->cpu_disable)
635                 return smp_ops->cpu_disable();
636
637         return -ENOSYS;
638 }
639
640 void __cpu_die(unsigned int cpu)
641 {
642         if (smp_ops->cpu_die)
643                 smp_ops->cpu_die(cpu);
644 }
645 #endif