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 __cpuinitdata pen_release = -1;
74 static DECLARE_COMPLETION(cpu_running);
76 static struct smp_operations smp_ops;
78 void __init smp_set_ops(struct smp_operations *ops)
84 int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *idle)
89 * We need to tell the secondary core where to find
90 * its stack and the page tables.
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));
99 * Now bring the CPU into our world.
101 ret = boot_secondary(cpu, idle);
104 * CPU was successfully started, wait for it
105 * to come online or time out.
107 wait_for_completion_timeout(&cpu_running,
108 msecs_to_jiffies(1000));
110 if (!cpu_online(cpu)) {
111 pr_crit("CPU%u: failed to come online\n", cpu);
115 pr_err("CPU%u: failed to boot: %d\n", cpu, ret);
118 secondary_data.stack = NULL;
119 secondary_data.pgdir = 0;
124 /* platform specific SMP operations */
125 void __init smp_init_cpus(void)
127 if (smp_ops.smp_init_cpus)
128 smp_ops.smp_init_cpus();
131 int __cpuinit boot_secondary(unsigned int cpu, struct task_struct *idle)
133 if (smp_ops.smp_boot_secondary)
134 return smp_ops.smp_boot_secondary(cpu, idle);
138 #ifdef CONFIG_HOTPLUG_CPU
139 static void percpu_timer_stop(void);
141 static int platform_cpu_kill(unsigned int cpu)
143 if (smp_ops.cpu_kill)
144 return smp_ops.cpu_kill(cpu);
148 static int platform_cpu_disable(unsigned int cpu)
150 if (smp_ops.cpu_disable)
151 return smp_ops.cpu_disable(cpu);
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.
158 return cpu == 0 ? -EPERM : 0;
161 * __cpu_disable runs on the processor to be shutdown.
163 int __cpuinit __cpu_disable(void)
165 unsigned int cpu = smp_processor_id();
168 ret = platform_cpu_disable(cpu);
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.
176 set_cpu_online(cpu, false);
179 * OK - migrate IRQs away from this CPU
184 * Stop the local timer for this CPU.
189 * Flush user cache and TLB mappings, and then remove this CPU
190 * from the vm mask set of all processes.
192 * Caches are flushed to the Level of Unification Inner Shareable
193 * to write-back dirty lines to unified caches shared by all CPUs.
196 local_flush_tlb_all();
198 clear_tasks_mm_cpumask(cpu);
203 static DECLARE_COMPLETION(cpu_died);
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.
209 void __cpuinit __cpu_die(unsigned int cpu)
211 if (!wait_for_completion_timeout(&cpu_died, msecs_to_jiffies(5000))) {
212 pr_err("CPU%u: cpu didn't die\n", cpu);
215 printk(KERN_NOTICE "CPU%u: shutdown\n", cpu);
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.
224 if (!platform_cpu_kill(cpu))
225 printk("CPU%u: unable to kill\n", cpu);
229 * Called from the idle thread for the CPU which has been shutdown.
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.
236 void __ref cpu_die(void)
238 unsigned int cpu = smp_processor_id();
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.
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().
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.
268 * The actual CPU shutdown procedure is at least platform (if not
269 * CPU) specific. This may remove power, or it may simply spin.
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.
276 * The return path should not be used for platforms which can
280 smp_ops.cpu_die(cpu);
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.
287 __asm__("mov sp, %0\n"
289 " b secondary_start_kernel"
291 : "r" (task_stack_page(current) + THREAD_SIZE - 8));
293 #endif /* CONFIG_HOTPLUG_CPU */
296 * Called by both boot and secondaries to move global data into
297 * per-processor storage.
299 static void __cpuinit smp_store_cpu_info(unsigned int cpuid)
301 struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
303 cpu_info->loops_per_jiffy = loops_per_jiffy;
304 cpu_info->cpuid = read_cpuid_id();
306 store_cpu_topology(cpuid);
309 static void percpu_timer_setup(void);
312 * This is the secondary CPU boot entry. We're using this CPUs
313 * idle thread stack, but a set of temporary page tables.
315 asmlinkage void __cpuinit secondary_start_kernel(void)
317 struct mm_struct *mm = &init_mm;
321 * The identity mapping is uncached (strongly ordered), so
322 * switch away from it before attempting any exclusive accesses.
324 cpu_switch_mm(mm->pgd, mm);
325 local_flush_bp_all();
326 enter_lazy_tlb(mm, current);
327 local_flush_tlb_all();
330 * All kernel threads share the same mm context; grab a
331 * reference and switch to it.
333 cpu = smp_processor_id();
334 atomic_inc(&mm->mm_count);
335 current->active_mm = mm;
336 cpumask_set_cpu(cpu, mm_cpumask(mm));
340 printk("CPU%u: Booted secondary processor\n", cpu);
343 trace_hardirqs_off();
346 * Give the platform a chance to do its own initialisation.
348 if (smp_ops.smp_secondary_init)
349 smp_ops.smp_secondary_init(cpu);
351 notify_cpu_starting(cpu);
355 smp_store_cpu_info(cpu);
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.
362 set_cpu_online(cpu, true);
363 complete(&cpu_running);
366 * Setup the percpu timer for this CPU.
368 percpu_timer_setup();
374 * OK, it's off to the idle thread for us
376 cpu_startup_entry(CPUHP_ONLINE);
379 void __init smp_cpus_done(unsigned int max_cpus)
382 unsigned long bogosum = 0;
384 for_each_online_cpu(cpu)
385 bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
387 printk(KERN_INFO "SMP: Total of %d processors activated "
388 "(%lu.%02lu BogoMIPS).\n",
390 bogosum / (500000/HZ),
391 (bogosum / (5000/HZ)) % 100);
396 void __init smp_prepare_boot_cpu(void)
398 set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
401 void __init smp_prepare_cpus(unsigned int max_cpus)
403 unsigned int ncores = num_possible_cpus();
407 smp_store_cpu_info(smp_processor_id());
410 * are we trying to boot more cores than exist?
412 if (max_cpus > ncores)
414 if (ncores > 1 && max_cpus) {
416 * Enable the local timer or broadcast device for the
417 * boot CPU, but only if we have more than one CPU.
419 percpu_timer_setup();
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).
427 init_cpu_present(cpu_possible_mask);
430 * Initialise the SCU if there are more than one CPU
431 * and let them know where to start.
433 if (smp_ops.smp_prepare_cpus)
434 smp_ops.smp_prepare_cpus(max_cpus);
438 static void (*smp_cross_call)(const struct cpumask *, unsigned int);
440 void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int))
446 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
448 smp_cross_call(mask, IPI_CALL_FUNC);
451 void arch_send_wakeup_ipi_mask(const struct cpumask *mask)
453 smp_cross_call(mask, IPI_WAKEUP);
456 void arch_send_call_function_single_ipi(int cpu)
458 smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
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"),
471 void show_ipi_list(struct seq_file *p, int prec)
475 for (i = 0; i < NR_IPI; i++) {
476 seq_printf(p, "%*s%u: ", prec - 1, "IPI", i);
478 for_each_online_cpu(cpu)
479 seq_printf(p, "%10u ",
480 __get_irq_stat(cpu, ipi_irqs[i]));
482 seq_printf(p, " %s\n", ipi_types[i]);
486 u64 smp_irq_stat_cpu(unsigned int cpu)
491 for (i = 0; i < NR_IPI; i++)
492 sum += __get_irq_stat(cpu, ipi_irqs[i]);
498 * Timer (local or broadcast) support
500 static DEFINE_PER_CPU(struct clock_event_device, percpu_clockevent);
502 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
503 void tick_broadcast(const struct cpumask *mask)
505 smp_cross_call(mask, IPI_TIMER);
509 static void broadcast_timer_set_mode(enum clock_event_mode mode,
510 struct clock_event_device *evt)
514 static void __cpuinit broadcast_timer_setup(struct clock_event_device *evt)
516 evt->name = "dummy_timer";
517 evt->features = CLOCK_EVT_FEAT_ONESHOT |
518 CLOCK_EVT_FEAT_PERIODIC |
519 CLOCK_EVT_FEAT_DUMMY;
522 evt->set_mode = broadcast_timer_set_mode;
524 clockevents_register_device(evt);
527 static struct local_timer_ops *lt_ops;
529 #ifdef CONFIG_LOCAL_TIMERS
530 int local_timer_register(struct local_timer_ops *ops)
532 if (!is_smp() || !setup_max_cpus)
543 static void __cpuinit percpu_timer_setup(void)
545 unsigned int cpu = smp_processor_id();
546 struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
548 evt->cpumask = cpumask_of(cpu);
550 if (!lt_ops || lt_ops->setup(evt))
551 broadcast_timer_setup(evt);
554 #ifdef CONFIG_HOTPLUG_CPU
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
560 static void percpu_timer_stop(void)
562 unsigned int cpu = smp_processor_id();
563 struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
570 static DEFINE_RAW_SPINLOCK(stop_lock);
573 * ipi_cpu_stop - handle IPI from smp_send_stop()
575 static void ipi_cpu_stop(unsigned int cpu)
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);
582 raw_spin_unlock(&stop_lock);
585 set_cpu_online(cpu, false);
595 * Main handler for inter-processor interrupts
597 asmlinkage void __exception_irq_entry do_IPI(int ipinr, struct pt_regs *regs)
599 handle_IPI(ipinr, regs);
602 void handle_IPI(int ipinr, struct pt_regs *regs)
604 unsigned int cpu = smp_processor_id();
605 struct pt_regs *old_regs = set_irq_regs(regs);
608 __inc_irq_stat(cpu, ipi_irqs[ipinr]);
610 trace_arm_ipi_entry(ipinr);
615 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
618 tick_receive_broadcast();
629 generic_smp_call_function_interrupt();
633 case IPI_CALL_FUNC_SINGLE:
635 generic_smp_call_function_single_interrupt();
646 printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
650 trace_arm_ipi_exit(ipinr);
651 set_irq_regs(old_regs);
654 void smp_send_reschedule(int cpu)
656 smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
659 void smp_send_stop(void)
661 unsigned long timeout;
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);
669 /* Wait up to one second for other CPUs to stop */
670 timeout = USEC_PER_SEC;
671 while (num_online_cpus() > 1 && timeout--)
674 if (num_online_cpus() > 1)
675 pr_warning("SMP: failed to stop secondary CPUs\n");
681 int setup_profiling_timer(unsigned int multiplier)
686 #ifdef CONFIG_CPU_FREQ
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;
693 static int cpufreq_callback(struct notifier_block *nb,
694 unsigned long val, void *data)
696 struct cpufreq_freqs *freq = data;
699 if (freq->flags & CPUFREQ_CONST_LOOPS)
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;
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,
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),
726 static struct notifier_block cpufreq_notifier = {
727 .notifier_call = cpufreq_callback,
730 static int __init register_cpufreq_notifier(void)
732 return cpufreq_register_notifier(&cpufreq_notifier,
733 CPUFREQ_TRANSITION_NOTIFIER);
735 core_initcall(register_cpufreq_notifier);