Merge remote-tracking branch 'lsk/v3.10/topic/big.LITTLE' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / arch / arm / kernel / smp.c
1 /*
2  *  linux/arch/arm/kernel/smp.c
3  *
4  *  Copyright (C) 2002 ARM Limited, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/module.h>
11 #include <linux/delay.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/cache.h>
17 #include <linux/profile.h>
18 #include <linux/errno.h>
19 #include <linux/mm.h>
20 #include <linux/err.h>
21 #include <linux/cpu.h>
22 #include <linux/seq_file.h>
23 #include <linux/irq.h>
24 #include <linux/percpu.h>
25 #include <linux/clockchips.h>
26 #include <linux/completion.h>
27 #include <linux/cpufreq.h>
28
29 #include <linux/atomic.h>
30 #include <asm/smp.h>
31 #include <asm/cacheflush.h>
32 #include <asm/cpu.h>
33 #include <asm/cputype.h>
34 #include <asm/exception.h>
35 #include <asm/idmap.h>
36 #include <asm/topology.h>
37 #include <asm/mmu_context.h>
38 #include <asm/pgtable.h>
39 #include <asm/pgalloc.h>
40 #include <asm/processor.h>
41 #include <asm/sections.h>
42 #include <asm/tlbflush.h>
43 #include <asm/ptrace.h>
44 #include <asm/localtimer.h>
45 #include <asm/smp_plat.h>
46 #include <asm/virt.h>
47 #include <asm/mach/arch.h>
48
49 #define CREATE_TRACE_POINTS
50 #include <trace/events/arm-ipi.h>
51
52 /*
53  * as from 2.5, kernels no longer have an init_tasks structure
54  * so we need some other way of telling a new secondary core
55  * where to place its SVC stack
56  */
57 struct secondary_data secondary_data;
58
59 /*
60  * control for which core is the next to come out of the secondary
61  * boot "holding pen"
62  */
63 volatile int pen_release = -1;
64
65 enum ipi_msg_type {
66         IPI_WAKEUP,
67         IPI_TIMER,
68         IPI_RESCHEDULE,
69         IPI_CALL_FUNC,
70         IPI_CALL_FUNC_SINGLE,
71         IPI_CPU_STOP,
72         IPI_COMPLETION,
73 };
74
75 static DECLARE_COMPLETION(cpu_running);
76
77 static struct smp_operations smp_ops;
78
79 void __init smp_set_ops(struct smp_operations *ops)
80 {
81         if (ops)
82                 smp_ops = *ops;
83 };
84
85 int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *idle)
86 {
87         int ret;
88
89         /*
90          * We need to tell the secondary core where to find
91          * its stack and the page tables.
92          */
93         secondary_data.stack = task_stack_page(idle) + THREAD_START_SP;
94         secondary_data.pgdir = virt_to_phys(idmap_pgd);
95         secondary_data.swapper_pg_dir = virt_to_phys(swapper_pg_dir);
96         __cpuc_flush_dcache_area(&secondary_data, sizeof(secondary_data));
97         outer_clean_range(__pa(&secondary_data), __pa(&secondary_data + 1));
98
99         /*
100          * Now bring the CPU into our world.
101          */
102         ret = boot_secondary(cpu, idle);
103         if (ret == 0) {
104                 /*
105                  * CPU was successfully started, wait for it
106                  * to come online or time out.
107                  */
108                 wait_for_completion_timeout(&cpu_running,
109                                                  msecs_to_jiffies(1000));
110
111                 if (!cpu_online(cpu)) {
112                         pr_crit("CPU%u: failed to come online\n", cpu);
113                         ret = -EIO;
114                 }
115         } else {
116                 pr_err("CPU%u: failed to boot: %d\n", cpu, ret);
117         }
118
119         secondary_data.stack = NULL;
120         secondary_data.pgdir = 0;
121
122         return ret;
123 }
124
125 /* platform specific SMP operations */
126 void __init smp_init_cpus(void)
127 {
128         if (smp_ops.smp_init_cpus)
129                 smp_ops.smp_init_cpus();
130 }
131
132 int __cpuinit boot_secondary(unsigned int cpu, struct task_struct *idle)
133 {
134         if (smp_ops.smp_boot_secondary)
135                 return smp_ops.smp_boot_secondary(cpu, idle);
136         return -ENOSYS;
137 }
138
139 #ifdef CONFIG_HOTPLUG_CPU
140 static void percpu_timer_stop(void);
141
142 static int platform_cpu_kill(unsigned int cpu)
143 {
144         if (smp_ops.cpu_kill)
145                 return smp_ops.cpu_kill(cpu);
146         return 1;
147 }
148
149 static int platform_cpu_disable(unsigned int cpu)
150 {
151         if (smp_ops.cpu_disable)
152                 return smp_ops.cpu_disable(cpu);
153
154         /*
155          * By default, allow disabling all CPUs except the first one,
156          * since this is special on a lot of platforms, e.g. because
157          * of clock tick interrupts.
158          */
159         return cpu == 0 ? -EPERM : 0;
160 }
161 /*
162  * __cpu_disable runs on the processor to be shutdown.
163  */
164 int __cpuinit __cpu_disable(void)
165 {
166         unsigned int cpu = smp_processor_id();
167         int ret;
168
169         ret = platform_cpu_disable(cpu);
170         if (ret)
171                 return ret;
172
173         /*
174          * Take this CPU offline.  Once we clear this, we can't return,
175          * and we must not schedule until we're ready to give up the cpu.
176          */
177         set_cpu_online(cpu, false);
178
179         /*
180          * OK - migrate IRQs away from this CPU
181          */
182         migrate_irqs();
183
184         /*
185          * Stop the local timer for this CPU.
186          */
187         percpu_timer_stop();
188
189         /*
190          * Flush user cache and TLB mappings, and then remove this CPU
191          * from the vm mask set of all processes.
192          *
193          * Caches are flushed to the Level of Unification Inner Shareable
194          * to write-back dirty lines to unified caches shared by all CPUs.
195          */
196         flush_cache_louis();
197         local_flush_tlb_all();
198
199         clear_tasks_mm_cpumask(cpu);
200
201         return 0;
202 }
203
204 static DECLARE_COMPLETION(cpu_died);
205
206 /*
207  * called on the thread which is asking for a CPU to be shutdown -
208  * waits until shutdown has completed, or it is timed out.
209  */
210 void __cpuinit __cpu_die(unsigned int cpu)
211 {
212         if (!wait_for_completion_timeout(&cpu_died, msecs_to_jiffies(5000))) {
213                 pr_err("CPU%u: cpu didn't die\n", cpu);
214                 return;
215         }
216         printk(KERN_NOTICE "CPU%u: shutdown\n", cpu);
217
218         /*
219          * platform_cpu_kill() is generally expected to do the powering off
220          * and/or cutting of clocks to the dying CPU.  Optionally, this may
221          * be done by the CPU which is dying in preference to supporting
222          * this call, but that means there is _no_ synchronisation between
223          * the requesting CPU and the dying CPU actually losing power.
224          */
225         if (!platform_cpu_kill(cpu))
226                 printk("CPU%u: unable to kill\n", cpu);
227 }
228
229 /*
230  * Called from the idle thread for the CPU which has been shutdown.
231  *
232  * Note that we disable IRQs here, but do not re-enable them
233  * before returning to the caller. This is also the behaviour
234  * of the other hotplug-cpu capable cores, so presumably coming
235  * out of idle fixes this.
236  */
237 void __ref cpu_die(void)
238 {
239         unsigned int cpu = smp_processor_id();
240
241         idle_task_exit();
242
243         local_irq_disable();
244
245         /*
246          * Flush the data out of the L1 cache for this CPU.  This must be
247          * before the completion to ensure that data is safely written out
248          * before platform_cpu_kill() gets called - which may disable
249          * *this* CPU and power down its cache.
250          */
251         flush_cache_louis();
252
253         /*
254          * Tell __cpu_die() that this CPU is now safe to dispose of.  Once
255          * this returns, power and/or clocks can be removed at any point
256          * from this CPU and its cache by platform_cpu_kill().
257          */
258         complete(&cpu_died);
259
260         /*
261          * Ensure that the cache lines associated with that completion are
262          * written out.  This covers the case where _this_ CPU is doing the
263          * powering down, to ensure that the completion is visible to the
264          * CPU waiting for this one.
265          */
266         flush_cache_louis();
267
268         /*
269          * The actual CPU shutdown procedure is at least platform (if not
270          * CPU) specific.  This may remove power, or it may simply spin.
271          *
272          * Platforms are generally expected *NOT* to return from this call,
273          * although there are some which do because they have no way to
274          * power down the CPU.  These platforms are the _only_ reason we
275          * have a return path which uses the fragment of assembly below.
276          *
277          * The return path should not be used for platforms which can
278          * power off the CPU.
279          */
280         if (smp_ops.cpu_die)
281                 smp_ops.cpu_die(cpu);
282
283         /*
284          * Do not return to the idle loop - jump back to the secondary
285          * cpu initialisation.  There's some initialisation which needs
286          * to be repeated to undo the effects of taking the CPU offline.
287          */
288         __asm__("mov    sp, %0\n"
289         "       mov     fp, #0\n"
290         "       b       secondary_start_kernel"
291                 :
292                 : "r" (task_stack_page(current) + THREAD_SIZE - 8));
293 }
294 #endif /* CONFIG_HOTPLUG_CPU */
295
296 /*
297  * Called by both boot and secondaries to move global data into
298  * per-processor storage.
299  */
300 static void __cpuinit smp_store_cpu_info(unsigned int cpuid)
301 {
302         struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
303
304         cpu_info->loops_per_jiffy = loops_per_jiffy;
305         cpu_info->cpuid = read_cpuid_id();
306
307         store_cpu_topology(cpuid);
308 }
309
310 static void percpu_timer_setup(void);
311
312 /*
313  * This is the secondary CPU boot entry.  We're using this CPUs
314  * idle thread stack, but a set of temporary page tables.
315  */
316 asmlinkage void __cpuinit secondary_start_kernel(void)
317 {
318         struct mm_struct *mm = &init_mm;
319         unsigned int cpu;
320
321         /*
322          * The identity mapping is uncached (strongly ordered), so
323          * switch away from it before attempting any exclusive accesses.
324          */
325         cpu_switch_mm(mm->pgd, mm);
326         local_flush_bp_all();
327         enter_lazy_tlb(mm, current);
328         local_flush_tlb_all();
329
330         /*
331          * All kernel threads share the same mm context; grab a
332          * reference and switch to it.
333          */
334         cpu = smp_processor_id();
335         atomic_inc(&mm->mm_count);
336         current->active_mm = mm;
337         cpumask_set_cpu(cpu, mm_cpumask(mm));
338
339         cpu_init();
340
341         printk("CPU%u: Booted secondary processor\n", cpu);
342
343         preempt_disable();
344         trace_hardirqs_off();
345
346         /*
347          * Give the platform a chance to do its own initialisation.
348          */
349         if (smp_ops.smp_secondary_init)
350                 smp_ops.smp_secondary_init(cpu);
351
352         notify_cpu_starting(cpu);
353
354         calibrate_delay();
355
356         smp_store_cpu_info(cpu);
357
358         /*
359          * OK, now it's safe to let the boot CPU continue.  Wait for
360          * the CPU migration code to notice that the CPU is online
361          * before we continue - which happens after __cpu_up returns.
362          */
363         set_cpu_online(cpu, true);
364         complete(&cpu_running);
365
366         /*
367          * Setup the percpu timer for this CPU.
368          */
369         percpu_timer_setup();
370
371         local_irq_enable();
372         local_fiq_enable();
373
374         /*
375          * OK, it's off to the idle thread for us
376          */
377         cpu_startup_entry(CPUHP_ONLINE);
378 }
379
380 void __init smp_cpus_done(unsigned int max_cpus)
381 {
382         int cpu;
383         unsigned long bogosum = 0;
384
385         for_each_online_cpu(cpu)
386                 bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
387
388         printk(KERN_INFO "SMP: Total of %d processors activated "
389                "(%lu.%02lu BogoMIPS).\n",
390                num_online_cpus(),
391                bogosum / (500000/HZ),
392                (bogosum / (5000/HZ)) % 100);
393
394         hyp_mode_check();
395 }
396
397 void __init smp_prepare_boot_cpu(void)
398 {
399         set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
400 }
401
402 void __init smp_prepare_cpus(unsigned int max_cpus)
403 {
404         unsigned int ncores = num_possible_cpus();
405
406         init_cpu_topology();
407
408         smp_store_cpu_info(smp_processor_id());
409
410         /*
411          * are we trying to boot more cores than exist?
412          */
413         if (max_cpus > ncores)
414                 max_cpus = ncores;
415         if (ncores > 1 && max_cpus) {
416                 /*
417                  * Enable the local timer or broadcast device for the
418                  * boot CPU, but only if we have more than one CPU.
419                  */
420                 percpu_timer_setup();
421
422                 /*
423                  * Initialise the present map, which describes the set of CPUs
424                  * actually populated at the present time. A platform should
425                  * re-initialize the map in the platforms smp_prepare_cpus()
426                  * if present != possible (e.g. physical hotplug).
427                  */
428                 init_cpu_present(cpu_possible_mask);
429
430                 /*
431                  * Initialise the SCU if there are more than one CPU
432                  * and let them know where to start.
433                  */
434                 if (smp_ops.smp_prepare_cpus)
435                         smp_ops.smp_prepare_cpus(max_cpus);
436         }
437 }
438
439 static void (*smp_cross_call)(const struct cpumask *, unsigned int);
440
441 void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int))
442 {
443         if (!smp_cross_call)
444                 smp_cross_call = fn;
445 }
446
447 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
448 {
449         smp_cross_call(mask, IPI_CALL_FUNC);
450 }
451
452 void arch_send_wakeup_ipi_mask(const struct cpumask *mask)
453 {
454         smp_cross_call(mask, IPI_WAKEUP);
455 }
456
457 void arch_send_call_function_single_ipi(int cpu)
458 {
459         smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
460 }
461
462 static const char *ipi_types[NR_IPI] = {
463 #define S(x,s)  [x] = s
464         S(IPI_WAKEUP, "CPU wakeup interrupts"),
465         S(IPI_TIMER, "Timer broadcast interrupts"),
466         S(IPI_RESCHEDULE, "Rescheduling interrupts"),
467         S(IPI_CALL_FUNC, "Function call interrupts"),
468         S(IPI_CALL_FUNC_SINGLE, "Single function call interrupts"),
469         S(IPI_CPU_STOP, "CPU stop interrupts"),
470         S(IPI_COMPLETION, "completion interrupts"),
471 };
472
473 void show_ipi_list(struct seq_file *p, int prec)
474 {
475         unsigned int cpu, i;
476
477         for (i = 0; i < NR_IPI; i++) {
478                 seq_printf(p, "%*s%u: ", prec - 1, "IPI", i);
479
480                 for_each_online_cpu(cpu)
481                         seq_printf(p, "%10u ",
482                                    __get_irq_stat(cpu, ipi_irqs[i]));
483
484                 seq_printf(p, " %s\n", ipi_types[i]);
485         }
486 }
487
488 u64 smp_irq_stat_cpu(unsigned int cpu)
489 {
490         u64 sum = 0;
491         int i;
492
493         for (i = 0; i < NR_IPI; i++)
494                 sum += __get_irq_stat(cpu, ipi_irqs[i]);
495
496         return sum;
497 }
498
499 /*
500  * Timer (local or broadcast) support
501  */
502 static DEFINE_PER_CPU(struct clock_event_device, percpu_clockevent);
503
504 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
505 void tick_broadcast(const struct cpumask *mask)
506 {
507         smp_cross_call(mask, IPI_TIMER);
508 }
509 #endif
510
511 static void broadcast_timer_set_mode(enum clock_event_mode mode,
512         struct clock_event_device *evt)
513 {
514 }
515
516 static void __cpuinit broadcast_timer_setup(struct clock_event_device *evt)
517 {
518         evt->name       = "dummy_timer";
519         evt->features   = CLOCK_EVT_FEAT_ONESHOT |
520                           CLOCK_EVT_FEAT_PERIODIC |
521                           CLOCK_EVT_FEAT_DUMMY;
522         evt->rating     = 100;
523         evt->mult       = 1;
524         evt->set_mode   = broadcast_timer_set_mode;
525
526         clockevents_register_device(evt);
527 }
528
529 static struct local_timer_ops *lt_ops;
530
531 #ifdef CONFIG_LOCAL_TIMERS
532 int local_timer_register(struct local_timer_ops *ops)
533 {
534         if (!is_smp() || !setup_max_cpus)
535                 return -ENXIO;
536
537         if (lt_ops)
538                 return -EBUSY;
539
540         lt_ops = ops;
541         return 0;
542 }
543 #endif
544
545 static void __cpuinit percpu_timer_setup(void)
546 {
547         unsigned int cpu = smp_processor_id();
548         struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
549
550         evt->cpumask = cpumask_of(cpu);
551
552         if (!lt_ops || lt_ops->setup(evt))
553                 broadcast_timer_setup(evt);
554 }
555
556 #ifdef CONFIG_HOTPLUG_CPU
557 /*
558  * The generic clock events code purposely does not stop the local timer
559  * on CPU_DEAD/CPU_DEAD_FROZEN hotplug events, so we have to do it
560  * manually here.
561  */
562 static void percpu_timer_stop(void)
563 {
564         unsigned int cpu = smp_processor_id();
565         struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
566
567         if (lt_ops)
568                 lt_ops->stop(evt);
569 }
570 #endif
571
572 static DEFINE_RAW_SPINLOCK(stop_lock);
573
574 /*
575  * ipi_cpu_stop - handle IPI from smp_send_stop()
576  */
577 static void ipi_cpu_stop(unsigned int cpu)
578 {
579         if (system_state == SYSTEM_BOOTING ||
580             system_state == SYSTEM_RUNNING) {
581                 raw_spin_lock(&stop_lock);
582                 printk(KERN_CRIT "CPU%u: stopping\n", cpu);
583                 dump_stack();
584                 raw_spin_unlock(&stop_lock);
585         }
586
587         set_cpu_online(cpu, false);
588
589         local_fiq_disable();
590         local_irq_disable();
591
592         while (1)
593                 cpu_relax();
594 }
595
596 static DEFINE_PER_CPU(struct completion *, cpu_completion);
597
598 int register_ipi_completion(struct completion *completion, int cpu)
599 {
600         per_cpu(cpu_completion, cpu) = completion;
601         return IPI_COMPLETION;
602 }
603
604 static void ipi_complete(unsigned int cpu)
605 {
606         complete(per_cpu(cpu_completion, cpu));
607 }
608
609 /*
610  * Main handler for inter-processor interrupts
611  */
612 asmlinkage void __exception_irq_entry do_IPI(int ipinr, struct pt_regs *regs)
613 {
614         handle_IPI(ipinr, regs);
615 }
616
617 void handle_IPI(int ipinr, struct pt_regs *regs)
618 {
619         unsigned int cpu = smp_processor_id();
620         struct pt_regs *old_regs = set_irq_regs(regs);
621
622         if (ipinr < NR_IPI)
623                 __inc_irq_stat(cpu, ipi_irqs[ipinr]);
624
625         trace_arm_ipi_entry(ipinr);
626         switch (ipinr) {
627         case IPI_WAKEUP:
628                 break;
629
630 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
631         case IPI_TIMER:
632                 irq_enter();
633                 tick_receive_broadcast();
634                 irq_exit();
635                 break;
636 #endif
637
638         case IPI_RESCHEDULE:
639                 scheduler_ipi();
640                 break;
641
642         case IPI_CALL_FUNC:
643                 irq_enter();
644                 generic_smp_call_function_interrupt();
645                 irq_exit();
646                 break;
647
648         case IPI_CALL_FUNC_SINGLE:
649                 irq_enter();
650                 generic_smp_call_function_single_interrupt();
651                 irq_exit();
652                 break;
653
654         case IPI_CPU_STOP:
655                 irq_enter();
656                 ipi_cpu_stop(cpu);
657                 irq_exit();
658                 break;
659
660         case IPI_COMPLETION:
661                 irq_enter();
662                 ipi_complete(cpu);
663                 irq_exit();
664                 break;
665
666         default:
667                 printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
668                        cpu, ipinr);
669                 break;
670         }
671         trace_arm_ipi_exit(ipinr);
672         set_irq_regs(old_regs);
673 }
674
675 void smp_send_reschedule(int cpu)
676 {
677         smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
678 }
679
680 void smp_send_stop(void)
681 {
682         unsigned long timeout;
683         struct cpumask mask;
684
685         cpumask_copy(&mask, cpu_online_mask);
686         cpumask_clear_cpu(smp_processor_id(), &mask);
687         if (!cpumask_empty(&mask))
688                 smp_cross_call(&mask, IPI_CPU_STOP);
689
690         /* Wait up to one second for other CPUs to stop */
691         timeout = USEC_PER_SEC;
692         while (num_online_cpus() > 1 && timeout--)
693                 udelay(1);
694
695         if (num_online_cpus() > 1)
696                 pr_warning("SMP: failed to stop secondary CPUs\n");
697 }
698
699 /*
700  * not supported here
701  */
702 int setup_profiling_timer(unsigned int multiplier)
703 {
704         return -EINVAL;
705 }
706
707 #ifdef CONFIG_CPU_FREQ
708
709 static DEFINE_PER_CPU(unsigned long, l_p_j_ref);
710 static DEFINE_PER_CPU(unsigned long, l_p_j_ref_freq);
711 static unsigned long global_l_p_j_ref;
712 static unsigned long global_l_p_j_ref_freq;
713
714 static int cpufreq_callback(struct notifier_block *nb,
715                                         unsigned long val, void *data)
716 {
717         struct cpufreq_freqs *freq = data;
718         int cpu = freq->cpu;
719
720         if (freq->flags & CPUFREQ_CONST_LOOPS)
721                 return NOTIFY_OK;
722
723         if (!per_cpu(l_p_j_ref, cpu)) {
724                 per_cpu(l_p_j_ref, cpu) =
725                         per_cpu(cpu_data, cpu).loops_per_jiffy;
726                 per_cpu(l_p_j_ref_freq, cpu) = freq->old;
727                 if (!global_l_p_j_ref) {
728                         global_l_p_j_ref = loops_per_jiffy;
729                         global_l_p_j_ref_freq = freq->old;
730                 }
731         }
732
733         if ((val == CPUFREQ_PRECHANGE  && freq->old < freq->new) ||
734             (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
735             (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
736                 loops_per_jiffy = cpufreq_scale(global_l_p_j_ref,
737                                                 global_l_p_j_ref_freq,
738                                                 freq->new);
739                 per_cpu(cpu_data, cpu).loops_per_jiffy =
740                         cpufreq_scale(per_cpu(l_p_j_ref, cpu),
741                                         per_cpu(l_p_j_ref_freq, cpu),
742                                         freq->new);
743         }
744         return NOTIFY_OK;
745 }
746
747 static struct notifier_block cpufreq_notifier = {
748         .notifier_call  = cpufreq_callback,
749 };
750
751 static int __init register_cpufreq_notifier(void)
752 {
753         return cpufreq_register_notifier(&cpufreq_notifier,
754                                                 CPUFREQ_TRANSITION_NOTIFIER);
755 }
756 core_initcall(register_cpufreq_notifier);
757
758 #endif