Merge tag 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/groeck...
[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/export.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/device.h>
31 #include <linux/cpu.h>
32 #include <linux/notifier.h>
33 #include <linux/topology.h>
34
35 #include <asm/ptrace.h>
36 #include <linux/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/cputhreads.h>
45 #include <asm/cputable.h>
46 #include <asm/mpic.h>
47 #include <asm/vdso_datapage.h>
48 #ifdef CONFIG_PPC64
49 #include <asm/paca.h>
50 #endif
51 #include <asm/vdso.h>
52 #include <asm/debug.h>
53
54 #ifdef DEBUG
55 #include <asm/udbg.h>
56 #define DBG(fmt...) udbg_printf(fmt)
57 #else
58 #define DBG(fmt...)
59 #endif
60
61 #ifdef CONFIG_HOTPLUG_CPU
62 /* State of each CPU during hotplug phases */
63 static DEFINE_PER_CPU(int, cpu_state) = { 0 };
64 #endif
65
66 struct thread_info *secondary_ti;
67
68 DEFINE_PER_CPU(cpumask_var_t, cpu_sibling_map);
69 DEFINE_PER_CPU(cpumask_var_t, cpu_core_map);
70
71 EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
72 EXPORT_PER_CPU_SYMBOL(cpu_core_map);
73
74 /* SMP operations for this machine */
75 struct smp_ops_t *smp_ops;
76
77 /* Can't be static due to PowerMac hackery */
78 volatile unsigned int cpu_callin_map[NR_CPUS];
79
80 int smt_enabled_at_boot = 1;
81
82 static void (*crash_ipi_function_ptr)(struct pt_regs *) = NULL;
83
84 #ifdef CONFIG_PPC64
85 int __devinit smp_generic_kick_cpu(int nr)
86 {
87         BUG_ON(nr < 0 || nr >= NR_CPUS);
88
89         /*
90          * The processor is currently spinning, waiting for the
91          * cpu_start field to become non-zero After we set cpu_start,
92          * the processor will continue on to secondary_start
93          */
94         if (!paca[nr].cpu_start) {
95                 paca[nr].cpu_start = 1;
96                 smp_mb();
97                 return 0;
98         }
99
100 #ifdef CONFIG_HOTPLUG_CPU
101         /*
102          * Ok it's not there, so it might be soft-unplugged, let's
103          * try to bring it back
104          */
105         per_cpu(cpu_state, nr) = CPU_UP_PREPARE;
106         smp_wmb();
107         smp_send_reschedule(nr);
108 #endif /* CONFIG_HOTPLUG_CPU */
109
110         return 0;
111 }
112 #endif /* CONFIG_PPC64 */
113
114 static irqreturn_t call_function_action(int irq, void *data)
115 {
116         generic_smp_call_function_interrupt();
117         return IRQ_HANDLED;
118 }
119
120 static irqreturn_t reschedule_action(int irq, void *data)
121 {
122         scheduler_ipi();
123         return IRQ_HANDLED;
124 }
125
126 static irqreturn_t call_function_single_action(int irq, void *data)
127 {
128         generic_smp_call_function_single_interrupt();
129         return IRQ_HANDLED;
130 }
131
132 static irqreturn_t debug_ipi_action(int irq, void *data)
133 {
134         if (crash_ipi_function_ptr) {
135                 crash_ipi_function_ptr(get_irq_regs());
136                 return IRQ_HANDLED;
137         }
138
139 #ifdef CONFIG_DEBUGGER
140         debugger_ipi(get_irq_regs());
141 #endif /* CONFIG_DEBUGGER */
142
143         return IRQ_HANDLED;
144 }
145
146 static irq_handler_t smp_ipi_action[] = {
147         [PPC_MSG_CALL_FUNCTION] =  call_function_action,
148         [PPC_MSG_RESCHEDULE] = reschedule_action,
149         [PPC_MSG_CALL_FUNC_SINGLE] = call_function_single_action,
150         [PPC_MSG_DEBUGGER_BREAK] = debug_ipi_action,
151 };
152
153 const char *smp_ipi_name[] = {
154         [PPC_MSG_CALL_FUNCTION] =  "ipi call function",
155         [PPC_MSG_RESCHEDULE] = "ipi reschedule",
156         [PPC_MSG_CALL_FUNC_SINGLE] = "ipi call function single",
157         [PPC_MSG_DEBUGGER_BREAK] = "ipi debugger",
158 };
159
160 /* optional function to request ipi, for controllers with >= 4 ipis */
161 int smp_request_message_ipi(int virq, int msg)
162 {
163         int err;
164
165         if (msg < 0 || msg > PPC_MSG_DEBUGGER_BREAK) {
166                 return -EINVAL;
167         }
168 #if !defined(CONFIG_DEBUGGER) && !defined(CONFIG_KEXEC)
169         if (msg == PPC_MSG_DEBUGGER_BREAK) {
170                 return 1;
171         }
172 #endif
173         err = request_irq(virq, smp_ipi_action[msg],
174                           IRQF_PERCPU | IRQF_NO_THREAD,
175                           smp_ipi_name[msg], 0);
176         WARN(err < 0, "unable to request_irq %d for %s (rc %d)\n",
177                 virq, smp_ipi_name[msg], err);
178
179         return err;
180 }
181
182 #ifdef CONFIG_PPC_SMP_MUXED_IPI
183 struct cpu_messages {
184         int messages;                   /* current messages */
185         unsigned long data;             /* data for cause ipi */
186 };
187 static DEFINE_PER_CPU_SHARED_ALIGNED(struct cpu_messages, ipi_message);
188
189 void smp_muxed_ipi_set_data(int cpu, unsigned long data)
190 {
191         struct cpu_messages *info = &per_cpu(ipi_message, cpu);
192
193         info->data = data;
194 }
195
196 void smp_muxed_ipi_message_pass(int cpu, int msg)
197 {
198         struct cpu_messages *info = &per_cpu(ipi_message, cpu);
199         char *message = (char *)&info->messages;
200
201         message[msg] = 1;
202         mb();
203         smp_ops->cause_ipi(cpu, info->data);
204 }
205
206 irqreturn_t smp_ipi_demux(void)
207 {
208         struct cpu_messages *info = &__get_cpu_var(ipi_message);
209         unsigned int all;
210
211         mb();   /* order any irq clear */
212
213         do {
214                 all = xchg_local(&info->messages, 0);
215
216 #ifdef __BIG_ENDIAN
217                 if (all & (1 << (24 - 8 * PPC_MSG_CALL_FUNCTION)))
218                         generic_smp_call_function_interrupt();
219                 if (all & (1 << (24 - 8 * PPC_MSG_RESCHEDULE)))
220                         scheduler_ipi();
221                 if (all & (1 << (24 - 8 * PPC_MSG_CALL_FUNC_SINGLE)))
222                         generic_smp_call_function_single_interrupt();
223                 if (all & (1 << (24 - 8 * PPC_MSG_DEBUGGER_BREAK)))
224                         debug_ipi_action(0, NULL);
225 #else
226 #error Unsupported ENDIAN
227 #endif
228         } while (info->messages);
229
230         return IRQ_HANDLED;
231 }
232 #endif /* CONFIG_PPC_SMP_MUXED_IPI */
233
234 static inline void do_message_pass(int cpu, int msg)
235 {
236         if (smp_ops->message_pass)
237                 smp_ops->message_pass(cpu, msg);
238 #ifdef CONFIG_PPC_SMP_MUXED_IPI
239         else
240                 smp_muxed_ipi_message_pass(cpu, msg);
241 #endif
242 }
243
244 void smp_send_reschedule(int cpu)
245 {
246         if (likely(smp_ops))
247                 do_message_pass(cpu, PPC_MSG_RESCHEDULE);
248 }
249 EXPORT_SYMBOL_GPL(smp_send_reschedule);
250
251 void arch_send_call_function_single_ipi(int cpu)
252 {
253         do_message_pass(cpu, PPC_MSG_CALL_FUNC_SINGLE);
254 }
255
256 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
257 {
258         unsigned int cpu;
259
260         for_each_cpu(cpu, mask)
261                 do_message_pass(cpu, PPC_MSG_CALL_FUNCTION);
262 }
263
264 #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
265 void smp_send_debugger_break(void)
266 {
267         int cpu;
268         int me = raw_smp_processor_id();
269
270         if (unlikely(!smp_ops))
271                 return;
272
273         for_each_online_cpu(cpu)
274                 if (cpu != me)
275                         do_message_pass(cpu, PPC_MSG_DEBUGGER_BREAK);
276 }
277 #endif
278
279 #ifdef CONFIG_KEXEC
280 void crash_send_ipi(void (*crash_ipi_callback)(struct pt_regs *))
281 {
282         crash_ipi_function_ptr = crash_ipi_callback;
283         if (crash_ipi_callback) {
284                 mb();
285                 smp_send_debugger_break();
286         }
287 }
288 #endif
289
290 static void stop_this_cpu(void *dummy)
291 {
292         /* Remove this CPU */
293         set_cpu_online(smp_processor_id(), false);
294
295         local_irq_disable();
296         while (1)
297                 ;
298 }
299
300 void smp_send_stop(void)
301 {
302         smp_call_function(stop_this_cpu, NULL, 0);
303 }
304
305 struct thread_info *current_set[NR_CPUS];
306
307 static void __devinit smp_store_cpu_info(int id)
308 {
309         per_cpu(cpu_pvr, id) = mfspr(SPRN_PVR);
310 #ifdef CONFIG_PPC_FSL_BOOK3E
311         per_cpu(next_tlbcam_idx, id)
312                 = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) - 1;
313 #endif
314 }
315
316 void __init smp_prepare_cpus(unsigned int max_cpus)
317 {
318         unsigned int cpu;
319
320         DBG("smp_prepare_cpus\n");
321
322         /* 
323          * setup_cpu may need to be called on the boot cpu. We havent
324          * spun any cpus up but lets be paranoid.
325          */
326         BUG_ON(boot_cpuid != smp_processor_id());
327
328         /* Fixup boot cpu */
329         smp_store_cpu_info(boot_cpuid);
330         cpu_callin_map[boot_cpuid] = 1;
331
332         for_each_possible_cpu(cpu) {
333                 zalloc_cpumask_var_node(&per_cpu(cpu_sibling_map, cpu),
334                                         GFP_KERNEL, cpu_to_node(cpu));
335                 zalloc_cpumask_var_node(&per_cpu(cpu_core_map, cpu),
336                                         GFP_KERNEL, cpu_to_node(cpu));
337         }
338
339         cpumask_set_cpu(boot_cpuid, cpu_sibling_mask(boot_cpuid));
340         cpumask_set_cpu(boot_cpuid, cpu_core_mask(boot_cpuid));
341
342         if (smp_ops)
343                 if (smp_ops->probe)
344                         max_cpus = smp_ops->probe();
345                 else
346                         max_cpus = NR_CPUS;
347         else
348                 max_cpus = 1;
349 }
350
351 void __devinit smp_prepare_boot_cpu(void)
352 {
353         BUG_ON(smp_processor_id() != boot_cpuid);
354 #ifdef CONFIG_PPC64
355         paca[boot_cpuid].__current = current;
356 #endif
357         current_set[boot_cpuid] = task_thread_info(current);
358 }
359
360 #ifdef CONFIG_HOTPLUG_CPU
361
362 int generic_cpu_disable(void)
363 {
364         unsigned int cpu = smp_processor_id();
365
366         if (cpu == boot_cpuid)
367                 return -EBUSY;
368
369         set_cpu_online(cpu, false);
370 #ifdef CONFIG_PPC64
371         vdso_data->processorCount--;
372 #endif
373         migrate_irqs();
374         return 0;
375 }
376
377 void generic_cpu_die(unsigned int cpu)
378 {
379         int i;
380
381         for (i = 0; i < 100; i++) {
382                 smp_rmb();
383                 if (per_cpu(cpu_state, cpu) == CPU_DEAD)
384                         return;
385                 msleep(100);
386         }
387         printk(KERN_ERR "CPU%d didn't die...\n", cpu);
388 }
389
390 void generic_mach_cpu_die(void)
391 {
392         unsigned int cpu;
393
394         local_irq_disable();
395         idle_task_exit();
396         cpu = smp_processor_id();
397         printk(KERN_DEBUG "CPU%d offline\n", cpu);
398         __get_cpu_var(cpu_state) = CPU_DEAD;
399         smp_wmb();
400         while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
401                 cpu_relax();
402 }
403
404 void generic_set_cpu_dead(unsigned int cpu)
405 {
406         per_cpu(cpu_state, cpu) = CPU_DEAD;
407 }
408
409 int generic_check_cpu_restart(unsigned int cpu)
410 {
411         return per_cpu(cpu_state, cpu) == CPU_UP_PREPARE;
412 }
413 #endif
414
415 static void cpu_idle_thread_init(unsigned int cpu, struct task_struct *idle)
416 {
417         struct thread_info *ti = task_thread_info(idle);
418
419 #ifdef CONFIG_PPC64
420         paca[cpu].__current = idle;
421         paca[cpu].kstack = (unsigned long)ti + THREAD_SIZE - STACK_FRAME_OVERHEAD;
422 #endif
423         ti->cpu = cpu;
424         secondary_ti = current_set[cpu] = ti;
425 }
426
427 int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *tidle)
428 {
429         int rc, c;
430
431         if (smp_ops == NULL ||
432             (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu)))
433                 return -EINVAL;
434
435         cpu_idle_thread_init(cpu, tidle);
436
437         /* Make sure callin-map entry is 0 (can be leftover a CPU
438          * hotplug
439          */
440         cpu_callin_map[cpu] = 0;
441
442         /* The information for processor bringup must
443          * be written out to main store before we release
444          * the processor.
445          */
446         smp_mb();
447
448         /* wake up cpus */
449         DBG("smp: kicking cpu %d\n", cpu);
450         rc = smp_ops->kick_cpu(cpu);
451         if (rc) {
452                 pr_err("smp: failed starting cpu %d (rc %d)\n", cpu, rc);
453                 return rc;
454         }
455
456         /*
457          * wait to see if the cpu made a callin (is actually up).
458          * use this value that I found through experimentation.
459          * -- Cort
460          */
461         if (system_state < SYSTEM_RUNNING)
462                 for (c = 50000; c && !cpu_callin_map[cpu]; c--)
463                         udelay(100);
464 #ifdef CONFIG_HOTPLUG_CPU
465         else
466                 /*
467                  * CPUs can take much longer to come up in the
468                  * hotplug case.  Wait five seconds.
469                  */
470                 for (c = 5000; c && !cpu_callin_map[cpu]; c--)
471                         msleep(1);
472 #endif
473
474         if (!cpu_callin_map[cpu]) {
475                 printk(KERN_ERR "Processor %u is stuck.\n", cpu);
476                 return -ENOENT;
477         }
478
479         DBG("Processor %u found.\n", cpu);
480
481         if (smp_ops->give_timebase)
482                 smp_ops->give_timebase();
483
484         /* Wait until cpu puts itself in the online map */
485         while (!cpu_online(cpu))
486                 cpu_relax();
487
488         return 0;
489 }
490
491 /* Return the value of the reg property corresponding to the given
492  * logical cpu.
493  */
494 int cpu_to_core_id(int cpu)
495 {
496         struct device_node *np;
497         const int *reg;
498         int id = -1;
499
500         np = of_get_cpu_node(cpu, NULL);
501         if (!np)
502                 goto out;
503
504         reg = of_get_property(np, "reg", NULL);
505         if (!reg)
506                 goto out;
507
508         id = *reg;
509 out:
510         of_node_put(np);
511         return id;
512 }
513
514 /* Helper routines for cpu to core mapping */
515 int cpu_core_index_of_thread(int cpu)
516 {
517         return cpu >> threads_shift;
518 }
519 EXPORT_SYMBOL_GPL(cpu_core_index_of_thread);
520
521 int cpu_first_thread_of_core(int core)
522 {
523         return core << threads_shift;
524 }
525 EXPORT_SYMBOL_GPL(cpu_first_thread_of_core);
526
527 /* Must be called when no change can occur to cpu_present_mask,
528  * i.e. during cpu online or offline.
529  */
530 static struct device_node *cpu_to_l2cache(int cpu)
531 {
532         struct device_node *np;
533         struct device_node *cache;
534
535         if (!cpu_present(cpu))
536                 return NULL;
537
538         np = of_get_cpu_node(cpu, NULL);
539         if (np == NULL)
540                 return NULL;
541
542         cache = of_find_next_cache_node(np);
543
544         of_node_put(np);
545
546         return cache;
547 }
548
549 /* Activate a secondary processor. */
550 void __devinit start_secondary(void *unused)
551 {
552         unsigned int cpu = smp_processor_id();
553         struct device_node *l2_cache;
554         int i, base;
555
556         atomic_inc(&init_mm.mm_count);
557         current->active_mm = &init_mm;
558
559         smp_store_cpu_info(cpu);
560         set_dec(tb_ticks_per_jiffy);
561         preempt_disable();
562         cpu_callin_map[cpu] = 1;
563
564         if (smp_ops->setup_cpu)
565                 smp_ops->setup_cpu(cpu);
566         if (smp_ops->take_timebase)
567                 smp_ops->take_timebase();
568
569         secondary_cpu_time_init();
570
571 #ifdef CONFIG_PPC64
572         if (system_state == SYSTEM_RUNNING)
573                 vdso_data->processorCount++;
574
575         vdso_getcpu_init();
576 #endif
577         notify_cpu_starting(cpu);
578         set_cpu_online(cpu, true);
579         /* Update sibling maps */
580         base = cpu_first_thread_sibling(cpu);
581         for (i = 0; i < threads_per_core; i++) {
582                 if (cpu_is_offline(base + i))
583                         continue;
584                 cpumask_set_cpu(cpu, cpu_sibling_mask(base + i));
585                 cpumask_set_cpu(base + i, cpu_sibling_mask(cpu));
586
587                 /* cpu_core_map should be a superset of
588                  * cpu_sibling_map even if we don't have cache
589                  * information, so update the former here, too.
590                  */
591                 cpumask_set_cpu(cpu, cpu_core_mask(base + i));
592                 cpumask_set_cpu(base + i, cpu_core_mask(cpu));
593         }
594         l2_cache = cpu_to_l2cache(cpu);
595         for_each_online_cpu(i) {
596                 struct device_node *np = cpu_to_l2cache(i);
597                 if (!np)
598                         continue;
599                 if (np == l2_cache) {
600                         cpumask_set_cpu(cpu, cpu_core_mask(i));
601                         cpumask_set_cpu(i, cpu_core_mask(cpu));
602                 }
603                 of_node_put(np);
604         }
605         of_node_put(l2_cache);
606
607         local_irq_enable();
608
609         cpu_idle();
610
611         BUG();
612 }
613
614 int setup_profiling_timer(unsigned int multiplier)
615 {
616         return 0;
617 }
618
619 void __init smp_cpus_done(unsigned int max_cpus)
620 {
621         cpumask_var_t old_mask;
622
623         /* We want the setup_cpu() here to be called from CPU 0, but our
624          * init thread may have been "borrowed" by another CPU in the meantime
625          * se we pin us down to CPU 0 for a short while
626          */
627         alloc_cpumask_var(&old_mask, GFP_NOWAIT);
628         cpumask_copy(old_mask, tsk_cpus_allowed(current));
629         set_cpus_allowed_ptr(current, cpumask_of(boot_cpuid));
630         
631         if (smp_ops && smp_ops->setup_cpu)
632                 smp_ops->setup_cpu(boot_cpuid);
633
634         set_cpus_allowed_ptr(current, old_mask);
635
636         free_cpumask_var(old_mask);
637
638         if (smp_ops && smp_ops->bringup_done)
639                 smp_ops->bringup_done();
640
641         dump_numa_cpu_topology();
642
643 }
644
645 int arch_sd_sibling_asym_packing(void)
646 {
647         if (cpu_has_feature(CPU_FTR_ASYM_SMT)) {
648                 printk_once(KERN_INFO "Enabling Asymmetric SMT scheduling\n");
649                 return SD_ASYM_PACKING;
650         }
651         return 0;
652 }
653
654 #ifdef CONFIG_HOTPLUG_CPU
655 int __cpu_disable(void)
656 {
657         struct device_node *l2_cache;
658         int cpu = smp_processor_id();
659         int base, i;
660         int err;
661
662         if (!smp_ops->cpu_disable)
663                 return -ENOSYS;
664
665         err = smp_ops->cpu_disable();
666         if (err)
667                 return err;
668
669         /* Update sibling maps */
670         base = cpu_first_thread_sibling(cpu);
671         for (i = 0; i < threads_per_core; i++) {
672                 cpumask_clear_cpu(cpu, cpu_sibling_mask(base + i));
673                 cpumask_clear_cpu(base + i, cpu_sibling_mask(cpu));
674                 cpumask_clear_cpu(cpu, cpu_core_mask(base + i));
675                 cpumask_clear_cpu(base + i, cpu_core_mask(cpu));
676         }
677
678         l2_cache = cpu_to_l2cache(cpu);
679         for_each_present_cpu(i) {
680                 struct device_node *np = cpu_to_l2cache(i);
681                 if (!np)
682                         continue;
683                 if (np == l2_cache) {
684                         cpumask_clear_cpu(cpu, cpu_core_mask(i));
685                         cpumask_clear_cpu(i, cpu_core_mask(cpu));
686                 }
687                 of_node_put(np);
688         }
689         of_node_put(l2_cache);
690
691
692         return 0;
693 }
694
695 void __cpu_die(unsigned int cpu)
696 {
697         if (smp_ops->cpu_die)
698                 smp_ops->cpu_die(cpu);
699 }
700
701 static DEFINE_MUTEX(powerpc_cpu_hotplug_driver_mutex);
702
703 void cpu_hotplug_driver_lock()
704 {
705         mutex_lock(&powerpc_cpu_hotplug_driver_mutex);
706 }
707
708 void cpu_hotplug_driver_unlock()
709 {
710         mutex_unlock(&powerpc_cpu_hotplug_driver_mutex);
711 }
712
713 void cpu_die(void)
714 {
715         if (ppc_md.cpu_die)
716                 ppc_md.cpu_die();
717
718         /* If we return, we re-enter start_secondary */
719         start_secondary_resume();
720 }
721
722 #endif