2 * linux/arch/arm/kernel/smp.c
4 * Copyright (C) 2002 ARM Limited, All Rights Reserved.
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.
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>
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>
29 #include <linux/atomic.h>
31 #include <asm/cacheflush.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>
47 #include <asm/mach/arch.h>
49 #define CREATE_TRACE_POINTS
50 #include <trace/events/arm-ipi.h>
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
57 struct secondary_data secondary_data;
60 * control for which core is the next to come out of the secondary
63 volatile int pen_release = -1;
75 static DECLARE_COMPLETION(cpu_running);
77 static struct smp_operations smp_ops;
79 void __init smp_set_ops(struct smp_operations *ops)
85 int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *idle)
90 * We need to tell the secondary core where to find
91 * its stack and the page tables.
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));
100 * Now bring the CPU into our world.
102 ret = boot_secondary(cpu, idle);
105 * CPU was successfully started, wait for it
106 * to come online or time out.
108 wait_for_completion_timeout(&cpu_running,
109 msecs_to_jiffies(1000));
111 if (!cpu_online(cpu)) {
112 pr_crit("CPU%u: failed to come online\n", cpu);
116 pr_err("CPU%u: failed to boot: %d\n", cpu, ret);
119 secondary_data.stack = NULL;
120 secondary_data.pgdir = 0;
125 /* platform specific SMP operations */
126 void __init smp_init_cpus(void)
128 if (smp_ops.smp_init_cpus)
129 smp_ops.smp_init_cpus();
132 int __cpuinit boot_secondary(unsigned int cpu, struct task_struct *idle)
134 if (smp_ops.smp_boot_secondary)
135 return smp_ops.smp_boot_secondary(cpu, idle);
139 #ifdef CONFIG_HOTPLUG_CPU
140 static void percpu_timer_stop(void);
142 static int platform_cpu_kill(unsigned int cpu)
144 if (smp_ops.cpu_kill)
145 return smp_ops.cpu_kill(cpu);
149 static int platform_cpu_disable(unsigned int cpu)
151 if (smp_ops.cpu_disable)
152 return smp_ops.cpu_disable(cpu);
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.
159 return cpu == 0 ? -EPERM : 0;
162 * __cpu_disable runs on the processor to be shutdown.
164 int __cpuinit __cpu_disable(void)
166 unsigned int cpu = smp_processor_id();
169 ret = platform_cpu_disable(cpu);
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.
177 set_cpu_online(cpu, false);
180 * OK - migrate IRQs away from this CPU
185 * Stop the local timer for this CPU.
190 * Flush user cache and TLB mappings, and then remove this CPU
191 * from the vm mask set of all processes.
193 * Caches are flushed to the Level of Unification Inner Shareable
194 * to write-back dirty lines to unified caches shared by all CPUs.
197 local_flush_tlb_all();
199 clear_tasks_mm_cpumask(cpu);
204 static DECLARE_COMPLETION(cpu_died);
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.
210 void __cpuinit __cpu_die(unsigned int cpu)
212 if (!wait_for_completion_timeout(&cpu_died, msecs_to_jiffies(5000))) {
213 pr_err("CPU%u: cpu didn't die\n", cpu);
216 printk(KERN_NOTICE "CPU%u: shutdown\n", cpu);
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.
225 if (!platform_cpu_kill(cpu))
226 printk("CPU%u: unable to kill\n", cpu);
230 * Called from the idle thread for the CPU which has been shutdown.
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.
237 void __ref cpu_die(void)
239 unsigned int cpu = smp_processor_id();
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.
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().
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.
269 * The actual CPU shutdown procedure is at least platform (if not
270 * CPU) specific. This may remove power, or it may simply spin.
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.
277 * The return path should not be used for platforms which can
281 smp_ops.cpu_die(cpu);
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.
288 __asm__("mov sp, %0\n"
290 " b secondary_start_kernel"
292 : "r" (task_stack_page(current) + THREAD_SIZE - 8));
294 #endif /* CONFIG_HOTPLUG_CPU */
297 * Called by both boot and secondaries to move global data into
298 * per-processor storage.
300 static void __cpuinit smp_store_cpu_info(unsigned int cpuid)
302 struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
304 cpu_info->loops_per_jiffy = loops_per_jiffy;
305 cpu_info->cpuid = read_cpuid_id();
307 store_cpu_topology(cpuid);
310 static void percpu_timer_setup(void);
313 * This is the secondary CPU boot entry. We're using this CPUs
314 * idle thread stack, but a set of temporary page tables.
316 asmlinkage void __cpuinit secondary_start_kernel(void)
318 struct mm_struct *mm = &init_mm;
322 * The identity mapping is uncached (strongly ordered), so
323 * switch away from it before attempting any exclusive accesses.
325 cpu_switch_mm(mm->pgd, mm);
326 local_flush_bp_all();
327 enter_lazy_tlb(mm, current);
328 local_flush_tlb_all();
331 * All kernel threads share the same mm context; grab a
332 * reference and switch to it.
334 cpu = smp_processor_id();
335 atomic_inc(&mm->mm_count);
336 current->active_mm = mm;
337 cpumask_set_cpu(cpu, mm_cpumask(mm));
341 printk("CPU%u: Booted secondary processor\n", cpu);
344 trace_hardirqs_off();
347 * Give the platform a chance to do its own initialisation.
349 if (smp_ops.smp_secondary_init)
350 smp_ops.smp_secondary_init(cpu);
352 notify_cpu_starting(cpu);
356 smp_store_cpu_info(cpu);
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.
363 set_cpu_online(cpu, true);
364 complete(&cpu_running);
367 * Setup the percpu timer for this CPU.
369 percpu_timer_setup();
375 * OK, it's off to the idle thread for us
377 cpu_startup_entry(CPUHP_ONLINE);
380 void __init smp_cpus_done(unsigned int max_cpus)
383 unsigned long bogosum = 0;
385 for_each_online_cpu(cpu)
386 bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
388 printk(KERN_INFO "SMP: Total of %d processors activated "
389 "(%lu.%02lu BogoMIPS).\n",
391 bogosum / (500000/HZ),
392 (bogosum / (5000/HZ)) % 100);
397 void __init smp_prepare_boot_cpu(void)
399 set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
402 void __init smp_prepare_cpus(unsigned int max_cpus)
404 unsigned int ncores = num_possible_cpus();
408 smp_store_cpu_info(smp_processor_id());
411 * are we trying to boot more cores than exist?
413 if (max_cpus > ncores)
415 if (ncores > 1 && max_cpus) {
417 * Enable the local timer or broadcast device for the
418 * boot CPU, but only if we have more than one CPU.
420 percpu_timer_setup();
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).
428 init_cpu_present(cpu_possible_mask);
431 * Initialise the SCU if there are more than one CPU
432 * and let them know where to start.
434 if (smp_ops.smp_prepare_cpus)
435 smp_ops.smp_prepare_cpus(max_cpus);
439 static void (*smp_cross_call)(const struct cpumask *, unsigned int);
441 void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int))
447 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
449 smp_cross_call(mask, IPI_CALL_FUNC);
452 void arch_send_wakeup_ipi_mask(const struct cpumask *mask)
454 smp_cross_call(mask, IPI_WAKEUP);
457 void arch_send_call_function_single_ipi(int cpu)
459 smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
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"),
473 void show_ipi_list(struct seq_file *p, int prec)
477 for (i = 0; i < NR_IPI; i++) {
478 seq_printf(p, "%*s%u: ", prec - 1, "IPI", i);
480 for_each_online_cpu(cpu)
481 seq_printf(p, "%10u ",
482 __get_irq_stat(cpu, ipi_irqs[i]));
484 seq_printf(p, " %s\n", ipi_types[i]);
488 u64 smp_irq_stat_cpu(unsigned int cpu)
493 for (i = 0; i < NR_IPI; i++)
494 sum += __get_irq_stat(cpu, ipi_irqs[i]);
500 * Timer (local or broadcast) support
502 static DEFINE_PER_CPU(struct clock_event_device, percpu_clockevent);
504 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
505 void tick_broadcast(const struct cpumask *mask)
507 smp_cross_call(mask, IPI_TIMER);
511 static void broadcast_timer_set_mode(enum clock_event_mode mode,
512 struct clock_event_device *evt)
516 static void __cpuinit broadcast_timer_setup(struct clock_event_device *evt)
518 evt->name = "dummy_timer";
519 evt->features = CLOCK_EVT_FEAT_ONESHOT |
520 CLOCK_EVT_FEAT_PERIODIC |
521 CLOCK_EVT_FEAT_DUMMY;
524 evt->set_mode = broadcast_timer_set_mode;
526 clockevents_register_device(evt);
529 static struct local_timer_ops *lt_ops;
531 #ifdef CONFIG_LOCAL_TIMERS
532 int local_timer_register(struct local_timer_ops *ops)
534 if (!is_smp() || !setup_max_cpus)
545 static void __cpuinit percpu_timer_setup(void)
547 unsigned int cpu = smp_processor_id();
548 struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
550 evt->cpumask = cpumask_of(cpu);
552 if (!lt_ops || lt_ops->setup(evt))
553 broadcast_timer_setup(evt);
556 #ifdef CONFIG_HOTPLUG_CPU
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
562 static void percpu_timer_stop(void)
564 unsigned int cpu = smp_processor_id();
565 struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
572 static DEFINE_RAW_SPINLOCK(stop_lock);
575 * ipi_cpu_stop - handle IPI from smp_send_stop()
577 static void ipi_cpu_stop(unsigned int cpu)
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);
584 raw_spin_unlock(&stop_lock);
587 set_cpu_online(cpu, false);
596 static DEFINE_PER_CPU(struct completion *, cpu_completion);
598 int register_ipi_completion(struct completion *completion, int cpu)
600 per_cpu(cpu_completion, cpu) = completion;
601 return IPI_COMPLETION;
604 static void ipi_complete(unsigned int cpu)
606 complete(per_cpu(cpu_completion, cpu));
610 * Main handler for inter-processor interrupts
612 asmlinkage void __exception_irq_entry do_IPI(int ipinr, struct pt_regs *regs)
614 handle_IPI(ipinr, regs);
617 void handle_IPI(int ipinr, struct pt_regs *regs)
619 unsigned int cpu = smp_processor_id();
620 struct pt_regs *old_regs = set_irq_regs(regs);
623 __inc_irq_stat(cpu, ipi_irqs[ipinr]);
625 trace_arm_ipi_entry(ipinr);
630 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
633 tick_receive_broadcast();
644 generic_smp_call_function_interrupt();
648 case IPI_CALL_FUNC_SINGLE:
650 generic_smp_call_function_single_interrupt();
667 printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
671 trace_arm_ipi_exit(ipinr);
672 set_irq_regs(old_regs);
675 void smp_send_reschedule(int cpu)
677 smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
680 void smp_send_stop(void)
682 unsigned long timeout;
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);
690 /* Wait up to one second for other CPUs to stop */
691 timeout = USEC_PER_SEC;
692 while (num_online_cpus() > 1 && timeout--)
695 if (num_online_cpus() > 1)
696 pr_warning("SMP: failed to stop secondary CPUs\n");
702 int setup_profiling_timer(unsigned int multiplier)
707 #ifdef CONFIG_CPU_FREQ
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;
714 static int cpufreq_callback(struct notifier_block *nb,
715 unsigned long val, void *data)
717 struct cpufreq_freqs *freq = data;
720 if (freq->flags & CPUFREQ_CONST_LOOPS)
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;
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,
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),
747 static struct notifier_block cpufreq_notifier = {
748 .notifier_call = cpufreq_callback,
751 static int __init register_cpufreq_notifier(void)
753 return cpufreq_register_notifier(&cpufreq_notifier,
754 CPUFREQ_TRANSITION_NOTIFIER);
756 core_initcall(register_cpufreq_notifier);