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>
28 #include <linux/irq_work.h>
30 #include <linux/atomic.h>
32 #include <asm/cacheflush.h>
34 #include <asm/cputype.h>
35 #include <asm/exception.h>
36 #include <asm/idmap.h>
37 #include <asm/topology.h>
38 #include <asm/mmu_context.h>
39 #include <asm/pgtable.h>
40 #include <asm/pgalloc.h>
41 #include <asm/processor.h>
42 #include <asm/sections.h>
43 #include <asm/tlbflush.h>
44 #include <asm/ptrace.h>
45 #include <asm/smp_plat.h>
47 #include <asm/mach/arch.h>
50 #define CREATE_TRACE_POINTS
51 #include <trace/events/ipi.h>
54 * as from 2.5, kernels no longer have an init_tasks structure
55 * so we need some other way of telling a new secondary core
56 * where to place its SVC stack
58 struct secondary_data secondary_data
;
61 * control for which core is the next to come out of the secondary
64 volatile int pen_release
= -1;
77 static DECLARE_COMPLETION(cpu_running
);
79 static struct smp_operations smp_ops
;
81 void __init
smp_set_ops(struct smp_operations
*ops
)
87 static unsigned long get_arch_pgd(pgd_t
*pgd
)
89 phys_addr_t pgdir
= virt_to_idmap(pgd
);
90 BUG_ON(pgdir
& ARCH_PGD_MASK
);
91 return pgdir
>> ARCH_PGD_SHIFT
;
94 int __cpu_up(unsigned int cpu
, struct task_struct
*idle
)
99 * We need to tell the secondary core where to find
100 * its stack and the page tables.
102 secondary_data
.stack
= task_stack_page(idle
) + THREAD_START_SP
;
103 #ifdef CONFIG_ARM_MPU
104 secondary_data
.mpu_rgn_szr
= mpu_rgn_info
.rgns
[MPU_RAM_REGION
].drsr
;
108 secondary_data
.pgdir
= get_arch_pgd(idmap_pgd
);
109 secondary_data
.swapper_pg_dir
= get_arch_pgd(swapper_pg_dir
);
111 sync_cache_w(&secondary_data
);
114 * Now bring the CPU into our world.
116 ret
= boot_secondary(cpu
, idle
);
119 * CPU was successfully started, wait for it
120 * to come online or time out.
122 wait_for_completion_timeout(&cpu_running
,
123 msecs_to_jiffies(1000));
125 if (!cpu_online(cpu
)) {
126 pr_crit("CPU%u: failed to come online\n", cpu
);
130 pr_err("CPU%u: failed to boot: %d\n", cpu
, ret
);
134 memset(&secondary_data
, 0, sizeof(secondary_data
));
138 /* platform specific SMP operations */
139 void __init
smp_init_cpus(void)
141 if (smp_ops
.smp_init_cpus
)
142 smp_ops
.smp_init_cpus();
145 int boot_secondary(unsigned int cpu
, struct task_struct
*idle
)
147 if (smp_ops
.smp_boot_secondary
)
148 return smp_ops
.smp_boot_secondary(cpu
, idle
);
152 int platform_can_cpu_hotplug(void)
154 #ifdef CONFIG_HOTPLUG_CPU
155 if (smp_ops
.cpu_kill
)
162 #ifdef CONFIG_HOTPLUG_CPU
163 static int platform_cpu_kill(unsigned int cpu
)
165 if (smp_ops
.cpu_kill
)
166 return smp_ops
.cpu_kill(cpu
);
170 static int platform_cpu_disable(unsigned int cpu
)
172 if (smp_ops
.cpu_disable
)
173 return smp_ops
.cpu_disable(cpu
);
176 * By default, allow disabling all CPUs except the first one,
177 * since this is special on a lot of platforms, e.g. because
178 * of clock tick interrupts.
180 return cpu
== 0 ? -EPERM
: 0;
183 * __cpu_disable runs on the processor to be shutdown.
185 int __cpu_disable(void)
187 unsigned int cpu
= smp_processor_id();
190 ret
= platform_cpu_disable(cpu
);
195 * Take this CPU offline. Once we clear this, we can't return,
196 * and we must not schedule until we're ready to give up the cpu.
198 set_cpu_online(cpu
, false);
201 * OK - migrate IRQs away from this CPU
206 * Flush user cache and TLB mappings, and then remove this CPU
207 * from the vm mask set of all processes.
209 * Caches are flushed to the Level of Unification Inner Shareable
210 * to write-back dirty lines to unified caches shared by all CPUs.
213 local_flush_tlb_all();
215 clear_tasks_mm_cpumask(cpu
);
220 static DECLARE_COMPLETION(cpu_died
);
223 * called on the thread which is asking for a CPU to be shutdown -
224 * waits until shutdown has completed, or it is timed out.
226 void __cpu_die(unsigned int cpu
)
228 if (!wait_for_completion_timeout(&cpu_died
, msecs_to_jiffies(5000))) {
229 pr_err("CPU%u: cpu didn't die\n", cpu
);
232 printk(KERN_NOTICE
"CPU%u: shutdown\n", cpu
);
235 * platform_cpu_kill() is generally expected to do the powering off
236 * and/or cutting of clocks to the dying CPU. Optionally, this may
237 * be done by the CPU which is dying in preference to supporting
238 * this call, but that means there is _no_ synchronisation between
239 * the requesting CPU and the dying CPU actually losing power.
241 if (!platform_cpu_kill(cpu
))
242 printk("CPU%u: unable to kill\n", cpu
);
246 * Called from the idle thread for the CPU which has been shutdown.
248 * Note that we disable IRQs here, but do not re-enable them
249 * before returning to the caller. This is also the behaviour
250 * of the other hotplug-cpu capable cores, so presumably coming
251 * out of idle fixes this.
253 void __ref
cpu_die(void)
255 unsigned int cpu
= smp_processor_id();
262 * Flush the data out of the L1 cache for this CPU. This must be
263 * before the completion to ensure that data is safely written out
264 * before platform_cpu_kill() gets called - which may disable
265 * *this* CPU and power down its cache.
270 * Tell __cpu_die() that this CPU is now safe to dispose of. Once
271 * this returns, power and/or clocks can be removed at any point
272 * from this CPU and its cache by platform_cpu_kill().
277 * Ensure that the cache lines associated with that completion are
278 * written out. This covers the case where _this_ CPU is doing the
279 * powering down, to ensure that the completion is visible to the
280 * CPU waiting for this one.
285 * The actual CPU shutdown procedure is at least platform (if not
286 * CPU) specific. This may remove power, or it may simply spin.
288 * Platforms are generally expected *NOT* to return from this call,
289 * although there are some which do because they have no way to
290 * power down the CPU. These platforms are the _only_ reason we
291 * have a return path which uses the fragment of assembly below.
293 * The return path should not be used for platforms which can
297 smp_ops
.cpu_die(cpu
);
299 pr_warn("CPU%u: smp_ops.cpu_die() returned, trying to resuscitate\n",
303 * Do not return to the idle loop - jump back to the secondary
304 * cpu initialisation. There's some initialisation which needs
305 * to be repeated to undo the effects of taking the CPU offline.
307 __asm__("mov sp, %0\n"
309 " b secondary_start_kernel"
311 : "r" (task_stack_page(current
) + THREAD_SIZE
- 8));
313 #endif /* CONFIG_HOTPLUG_CPU */
316 * Called by both boot and secondaries to move global data into
317 * per-processor storage.
319 static void smp_store_cpu_info(unsigned int cpuid
)
321 struct cpuinfo_arm
*cpu_info
= &per_cpu(cpu_data
, cpuid
);
323 cpu_info
->loops_per_jiffy
= loops_per_jiffy
;
324 cpu_info
->cpuid
= read_cpuid_id();
326 store_cpu_topology(cpuid
);
330 * This is the secondary CPU boot entry. We're using this CPUs
331 * idle thread stack, but a set of temporary page tables.
333 asmlinkage
void secondary_start_kernel(void)
335 struct mm_struct
*mm
= &init_mm
;
339 * The identity mapping is uncached (strongly ordered), so
340 * switch away from it before attempting any exclusive accesses.
342 cpu_switch_mm(mm
->pgd
, mm
);
343 local_flush_bp_all();
344 enter_lazy_tlb(mm
, current
);
345 local_flush_tlb_all();
348 * All kernel threads share the same mm context; grab a
349 * reference and switch to it.
351 cpu
= smp_processor_id();
352 atomic_inc(&mm
->mm_count
);
353 current
->active_mm
= mm
;
354 cpumask_set_cpu(cpu
, mm_cpumask(mm
));
358 printk("CPU%u: Booted secondary processor\n", cpu
);
361 trace_hardirqs_off();
364 * Give the platform a chance to do its own initialisation.
366 if (smp_ops
.smp_secondary_init
)
367 smp_ops
.smp_secondary_init(cpu
);
369 notify_cpu_starting(cpu
);
373 smp_store_cpu_info(cpu
);
376 * OK, now it's safe to let the boot CPU continue. Wait for
377 * the CPU migration code to notice that the CPU is online
378 * before we continue - which happens after __cpu_up returns.
380 set_cpu_online(cpu
, true);
381 complete(&cpu_running
);
387 * OK, it's off to the idle thread for us
389 cpu_startup_entry(CPUHP_ONLINE
);
392 void __init
smp_cpus_done(unsigned int max_cpus
)
394 printk(KERN_INFO
"SMP: Total of %d processors activated.\n",
400 void __init
smp_prepare_boot_cpu(void)
402 set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
405 void __init
smp_prepare_cpus(unsigned int max_cpus
)
407 unsigned int ncores
= num_possible_cpus();
411 smp_store_cpu_info(smp_processor_id());
414 * are we trying to boot more cores than exist?
416 if (max_cpus
> ncores
)
418 if (ncores
> 1 && max_cpus
) {
420 * Initialise the present map, which describes the set of CPUs
421 * actually populated at the present time. A platform should
422 * re-initialize the map in the platforms smp_prepare_cpus()
423 * if present != possible (e.g. physical hotplug).
425 init_cpu_present(cpu_possible_mask
);
428 * Initialise the SCU if there are more than one CPU
429 * and let them know where to start.
431 if (smp_ops
.smp_prepare_cpus
)
432 smp_ops
.smp_prepare_cpus(max_cpus
);
436 static void (*__smp_cross_call
)(const struct cpumask
*, unsigned int);
438 void __init
set_smp_cross_call(void (*fn
)(const struct cpumask
*, unsigned int))
440 if (!__smp_cross_call
)
441 __smp_cross_call
= fn
;
444 static const char *ipi_types
[NR_IPI
] __tracepoint_string
= {
445 #define S(x,s) [x] = s
446 S(IPI_WAKEUP
, "CPU wakeup interrupts"),
447 S(IPI_TIMER
, "Timer broadcast interrupts"),
448 S(IPI_RESCHEDULE
, "Rescheduling interrupts"),
449 S(IPI_CALL_FUNC
, "Function call interrupts"),
450 S(IPI_CALL_FUNC_SINGLE
, "Single function call interrupts"),
451 S(IPI_CPU_STOP
, "CPU stop interrupts"),
452 S(IPI_IRQ_WORK
, "IRQ work interrupts"),
453 S(IPI_COMPLETION
, "completion interrupts"),
456 static void smp_cross_call(const struct cpumask
*target
, unsigned int ipinr
)
458 trace_ipi_raise(target
, ipi_types
[ipinr
]);
459 __smp_cross_call(target
, ipinr
);
462 void show_ipi_list(struct seq_file
*p
, int prec
)
466 for (i
= 0; i
< NR_IPI
; i
++) {
467 seq_printf(p
, "%*s%u: ", prec
- 1, "IPI", i
);
469 for_each_online_cpu(cpu
)
470 seq_printf(p
, "%10u ",
471 __get_irq_stat(cpu
, ipi_irqs
[i
]));
473 seq_printf(p
, " %s\n", ipi_types
[i
]);
477 u64
smp_irq_stat_cpu(unsigned int cpu
)
482 for (i
= 0; i
< NR_IPI
; i
++)
483 sum
+= __get_irq_stat(cpu
, ipi_irqs
[i
]);
488 void arch_send_call_function_ipi_mask(const struct cpumask
*mask
)
490 smp_cross_call(mask
, IPI_CALL_FUNC
);
493 void arch_send_wakeup_ipi_mask(const struct cpumask
*mask
)
495 smp_cross_call(mask
, IPI_WAKEUP
);
498 void arch_send_call_function_single_ipi(int cpu
)
500 smp_cross_call(cpumask_of(cpu
), IPI_CALL_FUNC_SINGLE
);
503 #ifdef CONFIG_IRQ_WORK
504 void arch_irq_work_raise(void)
507 smp_cross_call(cpumask_of(smp_processor_id()), IPI_IRQ_WORK
);
511 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
512 void tick_broadcast(const struct cpumask
*mask
)
514 smp_cross_call(mask
, IPI_TIMER
);
518 static DEFINE_RAW_SPINLOCK(stop_lock
);
521 * ipi_cpu_stop - handle IPI from smp_send_stop()
523 static void ipi_cpu_stop(unsigned int cpu
)
525 if (system_state
== SYSTEM_BOOTING
||
526 system_state
== SYSTEM_RUNNING
) {
527 raw_spin_lock(&stop_lock
);
528 printk(KERN_CRIT
"CPU%u: stopping\n", cpu
);
530 raw_spin_unlock(&stop_lock
);
533 set_cpu_online(cpu
, false);
542 static DEFINE_PER_CPU(struct completion
*, cpu_completion
);
544 int register_ipi_completion(struct completion
*completion
, int cpu
)
546 per_cpu(cpu_completion
, cpu
) = completion
;
547 return IPI_COMPLETION
;
550 static void ipi_complete(unsigned int cpu
)
552 complete(per_cpu(cpu_completion
, cpu
));
556 * Main handler for inter-processor interrupts
558 asmlinkage
void __exception_irq_entry
do_IPI(int ipinr
, struct pt_regs
*regs
)
560 handle_IPI(ipinr
, regs
);
563 void handle_IPI(int ipinr
, struct pt_regs
*regs
)
565 unsigned int cpu
= smp_processor_id();
566 struct pt_regs
*old_regs
= set_irq_regs(regs
);
568 if ((unsigned)ipinr
< NR_IPI
) {
569 trace_ipi_entry(ipi_types
[ipinr
]);
570 __inc_irq_stat(cpu
, ipi_irqs
[ipinr
]);
577 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
580 tick_receive_broadcast();
591 generic_smp_call_function_interrupt();
595 case IPI_CALL_FUNC_SINGLE
:
597 generic_smp_call_function_single_interrupt();
607 #ifdef CONFIG_IRQ_WORK
622 printk(KERN_CRIT
"CPU%u: Unknown IPI message 0x%x\n",
627 if ((unsigned)ipinr
< NR_IPI
)
628 trace_ipi_exit(ipi_types
[ipinr
]);
629 set_irq_regs(old_regs
);
632 void smp_send_reschedule(int cpu
)
634 smp_cross_call(cpumask_of(cpu
), IPI_RESCHEDULE
);
637 void smp_send_stop(void)
639 unsigned long timeout
;
642 cpumask_copy(&mask
, cpu_online_mask
);
643 cpumask_clear_cpu(smp_processor_id(), &mask
);
644 if (!cpumask_empty(&mask
))
645 smp_cross_call(&mask
, IPI_CPU_STOP
);
647 /* Wait up to one second for other CPUs to stop */
648 timeout
= USEC_PER_SEC
;
649 while (num_online_cpus() > 1 && timeout
--)
652 if (num_online_cpus() > 1)
653 pr_warning("SMP: failed to stop secondary CPUs\n");
659 int setup_profiling_timer(unsigned int multiplier
)
664 #ifdef CONFIG_CPU_FREQ
666 static DEFINE_PER_CPU(unsigned long, l_p_j_ref
);
667 static DEFINE_PER_CPU(unsigned long, l_p_j_ref_freq
);
668 static unsigned long global_l_p_j_ref
;
669 static unsigned long global_l_p_j_ref_freq
;
671 static int cpufreq_callback(struct notifier_block
*nb
,
672 unsigned long val
, void *data
)
674 struct cpufreq_freqs
*freq
= data
;
677 if (freq
->flags
& CPUFREQ_CONST_LOOPS
)
680 if (!per_cpu(l_p_j_ref
, cpu
)) {
681 per_cpu(l_p_j_ref
, cpu
) =
682 per_cpu(cpu_data
, cpu
).loops_per_jiffy
;
683 per_cpu(l_p_j_ref_freq
, cpu
) = freq
->old
;
684 if (!global_l_p_j_ref
) {
685 global_l_p_j_ref
= loops_per_jiffy
;
686 global_l_p_j_ref_freq
= freq
->old
;
690 if ((val
== CPUFREQ_PRECHANGE
&& freq
->old
< freq
->new) ||
691 (val
== CPUFREQ_POSTCHANGE
&& freq
->old
> freq
->new)) {
692 loops_per_jiffy
= cpufreq_scale(global_l_p_j_ref
,
693 global_l_p_j_ref_freq
,
695 per_cpu(cpu_data
, cpu
).loops_per_jiffy
=
696 cpufreq_scale(per_cpu(l_p_j_ref
, cpu
),
697 per_cpu(l_p_j_ref_freq
, cpu
),
703 static struct notifier_block cpufreq_notifier
= {
704 .notifier_call
= cpufreq_callback
,
707 static int __init
register_cpufreq_notifier(void)
709 return cpufreq_register_notifier(&cpufreq_notifier
,
710 CPUFREQ_TRANSITION_NOTIFIER
);
712 core_initcall(register_cpufreq_notifier
);