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