blob: 4471d90d25f1f23bbeb43cb849d4cdb572004586 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Russell Kingc97d4862006-10-25 13:59:16 +010010#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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>
Alexey Dobriyan4e950f62007-07-30 02:36:13 +040020#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/cpu.h>
22#include <linux/smp.h>
23#include <linux/seq_file.h>
Russell Kingc97d4862006-10-25 13:59:16 +010024#include <linux/irq.h>
Russell Kingbc282482009-05-17 18:58:34 +010025#include <linux/percpu.h>
26#include <linux/clockchips.h>
Russell King3c030be2010-11-30 11:07:35 +000027#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/atomic.h>
30#include <asm/cacheflush.h>
31#include <asm/cpu.h>
Russell King42578c82009-06-11 15:35:00 +010032#include <asm/cputype.h>
Jamie Iles0fd86292011-10-08 11:20:42 +010033#include <asm/exception.h>
34#include <asm/topology.h>
Russell Kinge65f38e2005-06-18 09:33:31 +010035#include <asm/mmu_context.h>
36#include <asm/pgtable.h>
37#include <asm/pgalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/processor.h>
Russell King37b05b62010-10-01 15:38:24 +010039#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/tlbflush.h>
41#include <asm/ptrace.h>
Russell Kingbc282482009-05-17 18:58:34 +010042#include <asm/localtimer.h>
Colin Cross692c3e252011-02-10 12:54:10 -080043#include <asm/smp_plat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
Russell Kinge65f38e2005-06-18 09:33:31 +010046 * as from 2.5, kernels no longer have an init_tasks structure
47 * so we need some other way of telling a new secondary core
48 * where to place its SVC stack
49 */
50struct secondary_data secondary_data;
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052enum ipi_msg_type {
Santosh Shilimkar7f685e52011-10-10 19:23:35 -070053 IPI_CPU_START = 1,
Russell King24480d92010-11-15 09:54:18 +000054 IPI_TIMER = 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 IPI_RESCHEDULE,
56 IPI_CALL_FUNC,
Jens Axboef6dd9fa52008-06-10 20:48:30 +020057 IPI_CALL_FUNC_SINGLE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 IPI_CPU_STOP,
Dima Zavin8b7fd322011-10-18 16:59:54 -070059 IPI_CPU_BACKTRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Russell Kingbd6f68a2005-07-17 21:35:41 +010062int __cpuinit __cpu_up(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Russell King71f512e2005-11-02 21:51:40 +000064 struct cpuinfo_arm *ci = &per_cpu(cpu_data, cpu);
65 struct task_struct *idle = ci->idle;
Russell Kinge65f38e2005-06-18 09:33:31 +010066 pgd_t *pgd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 int ret;
68
69 /*
Russell King71f512e2005-11-02 21:51:40 +000070 * Spawn a new process manually, if not already done.
71 * Grab a pointer to its task struct so we can mess with it
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
Russell King71f512e2005-11-02 21:51:40 +000073 if (!idle) {
74 idle = fork_idle(cpu);
75 if (IS_ERR(idle)) {
76 printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
77 return PTR_ERR(idle);
78 }
79 ci->idle = idle;
Santosh Shilimkar13ea9cc2010-04-30 06:51:20 +010080 } else {
81 /*
82 * Since this idle thread is being re-used, call
83 * init_idle() to reinitialize the thread structure.
84 */
85 init_idle(idle, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87
88 /*
Russell Kinge65f38e2005-06-18 09:33:31 +010089 * Allocate initial page tables to allow the new CPU to
90 * enable the MMU safely. This essentially means a set
91 * of our "standard" page tables, with the addition of
92 * a 1:1 mapping for the physical address of the kernel.
93 */
94 pgd = pgd_alloc(&init_mm);
Russell King37b05b62010-10-01 15:38:24 +010095 if (!pgd)
96 return -ENOMEM;
97
98 if (PHYS_OFFSET != PAGE_OFFSET) {
99#ifndef CONFIG_HOTPLUG_CPU
100 identity_mapping_add(pgd, __pa(__init_begin), __pa(__init_end));
101#endif
102 identity_mapping_add(pgd, __pa(_stext), __pa(_etext));
103 identity_mapping_add(pgd, __pa(_sdata), __pa(_edata));
104 }
Russell Kinge65f38e2005-06-18 09:33:31 +0100105
106 /*
107 * We need to tell the secondary core where to find
108 * its stack and the page tables.
109 */
Al Viro32d39a92006-01-12 01:05:58 -0800110 secondary_data.stack = task_stack_page(idle) + THREAD_START_SP;
Russell Kinge65f38e2005-06-18 09:33:31 +0100111 secondary_data.pgdir = virt_to_phys(pgd);
Catalin Marinasd4279582011-05-26 11:22:44 +0100112 secondary_data.swapper_pg_dir = virt_to_phys(swapper_pg_dir);
Russell King10272472010-02-12 14:36:24 +0000113 __cpuc_flush_dcache_area(&secondary_data, sizeof(secondary_data));
114 outer_clean_range(__pa(&secondary_data), __pa(&secondary_data + 1));
Russell Kinge65f38e2005-06-18 09:33:31 +0100115
116 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * Now bring the CPU into our world.
118 */
119 ret = boot_secondary(cpu, idle);
Russell Kinge65f38e2005-06-18 09:33:31 +0100120 if (ret == 0) {
121 unsigned long timeout;
122
123 /*
124 * CPU was successfully started, wait for it
125 * to come online or time out.
126 */
127 timeout = jiffies + HZ;
128 while (time_before(jiffies, timeout)) {
129 if (cpu_online(cpu))
130 break;
131
132 udelay(10);
133 barrier();
134 }
135
Russell King58613cd2010-12-18 12:34:39 +0000136 if (!cpu_online(cpu)) {
137 pr_crit("CPU%u: failed to come online\n", cpu);
Russell Kinge65f38e2005-06-18 09:33:31 +0100138 ret = -EIO;
Russell King58613cd2010-12-18 12:34:39 +0000139 }
140 } else {
141 pr_err("CPU%u: failed to boot: %d\n", cpu, ret);
Russell Kinge65f38e2005-06-18 09:33:31 +0100142 }
143
Russell King5d430452005-11-08 10:44:46 +0000144 secondary_data.stack = NULL;
Russell Kinge65f38e2005-06-18 09:33:31 +0100145 secondary_data.pgdir = 0;
146
Russell King37b05b62010-10-01 15:38:24 +0100147 if (PHYS_OFFSET != PAGE_OFFSET) {
148#ifndef CONFIG_HOTPLUG_CPU
149 identity_mapping_del(pgd, __pa(__init_begin), __pa(__init_end));
150#endif
151 identity_mapping_del(pgd, __pa(_stext), __pa(_etext));
152 identity_mapping_del(pgd, __pa(_sdata), __pa(_edata));
153 }
154
Benjamin Herrenschmidt5e541972008-02-04 22:29:14 -0800155 pgd_free(&init_mm, pgd);
Russell Kinge65f38e2005-06-18 09:33:31 +0100156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return ret;
158}
159
Russell Kinga054a812005-11-02 22:24:33 +0000160#ifdef CONFIG_HOTPLUG_CPU
Russell King10034aa2010-12-20 14:28:02 +0000161static void percpu_timer_stop(void);
162
Russell Kinga054a812005-11-02 22:24:33 +0000163/*
164 * __cpu_disable runs on the processor to be shutdown.
165 */
Russell King90140c32009-09-27 21:04:48 +0100166int __cpu_disable(void)
Russell Kinga054a812005-11-02 22:24:33 +0000167{
168 unsigned int cpu = smp_processor_id();
169 struct task_struct *p;
170 int ret;
171
Russell King8e2a43f2010-05-15 10:18:05 +0100172 ret = platform_cpu_disable(cpu);
Russell Kinga054a812005-11-02 22:24:33 +0000173 if (ret)
174 return ret;
175
176 /*
177 * Take this CPU offline. Once we clear this, we can't return,
178 * and we must not schedule until we're ready to give up the cpu.
179 */
Russell Kinge03cdad2009-05-28 14:16:52 +0100180 set_cpu_online(cpu, false);
Russell Kinga054a812005-11-02 22:24:33 +0000181
182 /*
183 * OK - migrate IRQs away from this CPU
184 */
185 migrate_irqs();
186
187 /*
Russell King37ee16a2005-11-08 19:08:05 +0000188 * Stop the local timer for this CPU.
189 */
Russell King10034aa2010-12-20 14:28:02 +0000190 percpu_timer_stop();
Russell King37ee16a2005-11-08 19:08:05 +0000191
192 /*
Russell Kinga054a812005-11-02 22:24:33 +0000193 * Flush user cache and TLB mappings, and then remove this CPU
194 * from the vm mask set of all processes.
195 */
196 flush_cache_all();
197 local_flush_tlb_all();
198
199 read_lock(&tasklist_lock);
200 for_each_process(p) {
201 if (p->mm)
Rusty Russell56f8ba82009-09-24 09:34:49 -0600202 cpumask_clear_cpu(cpu, mm_cpumask(p->mm));
Russell Kinga054a812005-11-02 22:24:33 +0000203 }
204 read_unlock(&tasklist_lock);
205
206 return 0;
207}
208
Russell King3c030be2010-11-30 11:07:35 +0000209static DECLARE_COMPLETION(cpu_died);
210
Russell Kinga054a812005-11-02 22:24:33 +0000211/*
212 * called on the thread which is asking for a CPU to be shutdown -
213 * waits until shutdown has completed, or it is timed out.
214 */
Russell King90140c32009-09-27 21:04:48 +0100215void __cpu_die(unsigned int cpu)
Russell Kinga054a812005-11-02 22:24:33 +0000216{
Russell King3c030be2010-11-30 11:07:35 +0000217 if (!wait_for_completion_timeout(&cpu_died, msecs_to_jiffies(5000))) {
218 pr_err("CPU%u: cpu didn't die\n", cpu);
219 return;
220 }
Jeff Ohlstein4d0bd422011-07-19 18:47:08 -0700221 pr_debug("CPU%u: shutdown\n", cpu);
Russell King3c030be2010-11-30 11:07:35 +0000222
Russell Kinga054a812005-11-02 22:24:33 +0000223 if (!platform_cpu_kill(cpu))
224 printk("CPU%u: unable to kill\n", cpu);
225}
226
227/*
228 * Called from the idle thread for the CPU which has been shutdown.
229 *
230 * Note that we disable IRQs here, but do not re-enable them
231 * before returning to the caller. This is also the behaviour
232 * of the other hotplug-cpu capable cores, so presumably coming
233 * out of idle fixes this.
234 */
Russell King90140c32009-09-27 21:04:48 +0100235void __ref cpu_die(void)
Russell Kinga054a812005-11-02 22:24:33 +0000236{
237 unsigned int cpu = smp_processor_id();
238
Russell Kinga054a812005-11-02 22:24:33 +0000239 idle_task_exit();
240
Russell Kingf36d3402010-11-30 12:21:30 +0000241 local_irq_disable();
242 mb();
243
Russell King3c030be2010-11-30 11:07:35 +0000244 /* Tell __cpu_die() that this CPU is now safe to dispose of */
245 complete(&cpu_died);
246
Russell Kinga054a812005-11-02 22:24:33 +0000247 /*
248 * actual CPU shutdown procedure is at least platform (if not
Russell King3c030be2010-11-30 11:07:35 +0000249 * CPU) specific.
Russell Kinga054a812005-11-02 22:24:33 +0000250 */
251 platform_cpu_die(cpu);
252
253 /*
254 * Do not return to the idle loop - jump back to the secondary
255 * cpu initialisation. There's some initialisation which needs
256 * to be repeated to undo the effects of taking the CPU offline.
257 */
258 __asm__("mov sp, %0\n"
Russell Kingfaabfa02010-12-20 16:58:19 +0000259 " mov fp, #0\n"
Russell Kinga054a812005-11-02 22:24:33 +0000260 " b secondary_start_kernel"
261 :
Al Viro32d39a92006-01-12 01:05:58 -0800262 : "r" (task_stack_page(current) + THREAD_SIZE - 8));
Russell Kinga054a812005-11-02 22:24:33 +0000263}
264#endif /* CONFIG_HOTPLUG_CPU */
265
Colin Cross692c3e252011-02-10 12:54:10 -0800266int __cpu_logical_map[NR_CPUS];
267
268void __init smp_setup_processor_id(void)
269{
270 int i;
271 u32 cpu = is_smp() ? read_cpuid_mpidr() & 0xff : 0;
272
273 cpu_logical_map(0) = cpu;
274 for (i = 1; i < NR_CPUS; ++i)
275 cpu_logical_map(i) = i == cpu ? 0 : i;
276
277 printk(KERN_INFO "Booting Linux on physical CPU %d\n", cpu);
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/*
Russell King05c74a62010-12-03 11:09:48 +0000281 * Called by both boot and secondaries to move global data into
282 * per-processor storage.
283 */
284static void __cpuinit smp_store_cpu_info(unsigned int cpuid)
285{
286 struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
287
288 cpu_info->loops_per_jiffy = loops_per_jiffy;
289}
290
291/*
Russell Kinge65f38e2005-06-18 09:33:31 +0100292 * This is the secondary CPU boot entry. We're using this CPUs
293 * idle thread stack, but a set of temporary page tables.
294 */
Russell Kingbd6f68a2005-07-17 21:35:41 +0100295asmlinkage void __cpuinit secondary_start_kernel(void)
Russell Kinge65f38e2005-06-18 09:33:31 +0100296{
297 struct mm_struct *mm = &init_mm;
Russell Kingda2660d2005-11-12 17:21:47 +0000298 unsigned int cpu = smp_processor_id();
Russell Kinge65f38e2005-06-18 09:33:31 +0100299
Jeff Ohlstein4d0bd422011-07-19 18:47:08 -0700300 pr_debug("CPU%u: Booted secondary processor\n", cpu);
Russell Kinge65f38e2005-06-18 09:33:31 +0100301
302 /*
303 * All kernel threads share the same mm context; grab a
304 * reference and switch to it.
305 */
Russell Kinge65f38e2005-06-18 09:33:31 +0100306 atomic_inc(&mm->mm_count);
307 current->active_mm = mm;
Rusty Russell56f8ba82009-09-24 09:34:49 -0600308 cpumask_set_cpu(cpu, mm_cpumask(mm));
Russell Kinge65f38e2005-06-18 09:33:31 +0100309 cpu_switch_mm(mm->pgd, mm);
310 enter_lazy_tlb(mm, current);
Russell King505d7b12005-07-28 20:32:47 +0100311 local_flush_tlb_all();
Russell Kinge65f38e2005-06-18 09:33:31 +0100312
313 cpu_init();
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800314 preempt_disable();
Russell King2c0136d2010-12-03 15:00:49 +0000315 trace_hardirqs_off();
Russell Kinge65f38e2005-06-18 09:33:31 +0100316
317 /*
318 * Give the platform a chance to do its own initialisation.
319 */
320 platform_secondary_init(cpu);
321
322 /*
323 * Enable local interrupts.
324 */
Manfred Spraule545a612008-09-07 16:57:22 +0200325 notify_cpu_starting(cpu);
Russell Kinge65f38e2005-06-18 09:33:31 +0100326 local_irq_enable();
327 local_fiq_enable();
328
Catalin Marinasa8655e82008-02-04 17:30:57 +0100329 /*
Russell Kingbc282482009-05-17 18:58:34 +0100330 * Setup the percpu timer for this CPU.
Catalin Marinasa8655e82008-02-04 17:30:57 +0100331 */
Russell Kingbc282482009-05-17 18:58:34 +0100332 percpu_timer_setup();
Catalin Marinasa8655e82008-02-04 17:30:57 +0100333
Russell Kinge65f38e2005-06-18 09:33:31 +0100334 calibrate_delay();
335
336 smp_store_cpu_info(cpu);
337
338 /*
Russell King573619d2011-06-20 16:46:01 +0100339 * OK, now it's safe to let the boot CPU continue. Wait for
340 * the CPU migration code to notice that the CPU is online
341 * before we continue.
Russell Kinge65f38e2005-06-18 09:33:31 +0100342 */
Russell Kinge03cdad2009-05-28 14:16:52 +0100343 set_cpu_online(cpu, true);
Russell King573619d2011-06-20 16:46:01 +0100344 while (!cpu_active(cpu))
345 cpu_relax();
Russell Kinge65f38e2005-06-18 09:33:31 +0100346
347 /*
348 * OK, it's off to the idle thread for us
349 */
350 cpu_idle();
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353void __init smp_cpus_done(unsigned int max_cpus)
354{
355 int cpu;
356 unsigned long bogosum = 0;
357
358 for_each_online_cpu(cpu)
359 bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
360
361 printk(KERN_INFO "SMP: Total of %d processors activated "
362 "(%lu.%02lu BogoMIPS).\n",
363 num_online_cpus(),
364 bogosum / (500000/HZ),
365 (bogosum / (5000/HZ)) % 100);
366}
367
368void __init smp_prepare_boot_cpu(void)
369{
370 unsigned int cpu = smp_processor_id();
371
Russell King71f512e2005-11-02 21:51:40 +0000372 per_cpu(cpu_data, cpu).idle = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Russell King05c74a62010-12-03 11:09:48 +0000375void __init smp_prepare_cpus(unsigned int max_cpus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Russell King05c74a62010-12-03 11:09:48 +0000377 unsigned int ncores = num_possible_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Russell King05c74a62010-12-03 11:09:48 +0000379 smp_store_cpu_info(smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /*
Russell King05c74a62010-12-03 11:09:48 +0000382 * are we trying to boot more cores than exist?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
Russell King05c74a62010-12-03 11:09:48 +0000384 if (max_cpus > ncores)
385 max_cpus = ncores;
Stephen Boyd69425ff2011-07-07 01:56:51 +0100386 if (ncores > 1 && max_cpus) {
Russell King05c74a62010-12-03 11:09:48 +0000387 /*
388 * Enable the local timer or broadcast device for the
389 * boot CPU, but only if we have more than one CPU.
390 */
391 percpu_timer_setup();
392
393 /*
Stephen Boyd69425ff2011-07-07 01:56:51 +0100394 * Initialise the present map, which describes the set of CPUs
395 * actually populated at the present time. A platform should
396 * re-initialize the map in platform_smp_prepare_cpus() if
397 * present != possible (e.g. physical hotplug).
398 */
399 init_cpu_present(&cpu_possible_map);
400
401 /*
Russell King05c74a62010-12-03 11:09:48 +0000402 * Initialise the SCU if there are more than one CPU
403 * and let them know where to start.
404 */
405 platform_smp_prepare_cpus(max_cpus);
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
Russell King0f7b3322011-04-03 13:01:30 +0100409static void (*smp_cross_call)(const struct cpumask *, unsigned int);
410
411void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int))
412{
413 smp_cross_call = fn;
414}
415
Russell King82668102009-05-17 16:20:18 +0100416void arch_send_call_function_ipi_mask(const struct cpumask *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Russell Kinge3fbb082010-12-20 14:47:19 +0000418 smp_cross_call(mask, IPI_CALL_FUNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
Jens Axboef6dd9fa52008-06-10 20:48:30 +0200421void arch_send_call_function_single_ipi(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Russell Kinge3fbb082010-12-20 14:47:19 +0000423 smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
Catalin Marinas3e459992008-02-04 17:28:56 +0100425
Russell King4a88abd2010-11-15 14:40:29 +0000426static const char *ipi_types[NR_IPI] = {
Santosh Shilimkar7f685e52011-10-10 19:23:35 -0700427#define S(x,s) [x - IPI_CPU_START] = s
428 S(IPI_CPU_START, "CPU start interrupts"),
Russell King4a88abd2010-11-15 14:40:29 +0000429 S(IPI_TIMER, "Timer broadcast interrupts"),
430 S(IPI_RESCHEDULE, "Rescheduling interrupts"),
431 S(IPI_CALL_FUNC, "Function call interrupts"),
432 S(IPI_CALL_FUNC_SINGLE, "Single function call interrupts"),
433 S(IPI_CPU_STOP, "CPU stop interrupts"),
Dima Zavin8b7fd322011-10-18 16:59:54 -0700434 S(IPI_CPU_BACKTRACE, "CPU backtrace"),
Russell King4a88abd2010-11-15 14:40:29 +0000435};
436
Russell Kingf13cd412010-11-15 14:33:51 +0000437void show_ipi_list(struct seq_file *p, int prec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Russell King4a88abd2010-11-15 14:40:29 +0000439 unsigned int cpu, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Russell King4a88abd2010-11-15 14:40:29 +0000441 for (i = 0; i < NR_IPI; i++) {
442 seq_printf(p, "%*s%u: ", prec - 1, "IPI", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Russell King4a88abd2010-11-15 14:40:29 +0000444 for_each_present_cpu(cpu)
445 seq_printf(p, "%10u ",
446 __get_irq_stat(cpu, ipi_irqs[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Russell King4a88abd2010-11-15 14:40:29 +0000448 seq_printf(p, " %s\n", ipi_types[i]);
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Russell Kingb54992f2010-11-15 14:46:46 +0000452u64 smp_irq_stat_cpu(unsigned int cpu)
Russell King37ee16a2005-11-08 19:08:05 +0000453{
Russell Kingb54992f2010-11-15 14:46:46 +0000454 u64 sum = 0;
455 int i;
Russell King37ee16a2005-11-08 19:08:05 +0000456
Russell Kingb54992f2010-11-15 14:46:46 +0000457 for (i = 0; i < NR_IPI; i++)
458 sum += __get_irq_stat(cpu, ipi_irqs[i]);
Russell King37ee16a2005-11-08 19:08:05 +0000459
Russell Kingb54992f2010-11-15 14:46:46 +0000460 return sum;
Russell King37ee16a2005-11-08 19:08:05 +0000461}
462
Russell Kingbc282482009-05-17 18:58:34 +0100463/*
464 * Timer (local or broadcast) support
465 */
466static DEFINE_PER_CPU(struct clock_event_device, percpu_clockevent);
467
Russell Kingc97d4862006-10-25 13:59:16 +0100468static void ipi_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
Russell Kingbc282482009-05-17 18:58:34 +0100470 struct clock_event_device *evt = &__get_cpu_var(percpu_clockevent);
Russell Kingbc282482009-05-17 18:58:34 +0100471 evt->event_handler(evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Russell Kingbc282482009-05-17 18:58:34 +0100474#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
475static void smp_timer_broadcast(const struct cpumask *mask)
476{
Russell Kinge3fbb082010-12-20 14:47:19 +0000477 smp_cross_call(mask, IPI_TIMER);
Russell Kingbc282482009-05-17 18:58:34 +0100478}
Russell King5388a6b2010-07-26 13:19:43 +0100479#else
480#define smp_timer_broadcast NULL
481#endif
Russell Kingbc282482009-05-17 18:58:34 +0100482
483static void broadcast_timer_set_mode(enum clock_event_mode mode,
484 struct clock_event_device *evt)
485{
486}
487
Stephen Boyda8d25182011-04-27 01:34:27 +0100488static void __cpuinit broadcast_timer_setup(struct clock_event_device *evt)
Russell Kingbc282482009-05-17 18:58:34 +0100489{
490 evt->name = "dummy_timer";
491 evt->features = CLOCK_EVT_FEAT_ONESHOT |
492 CLOCK_EVT_FEAT_PERIODIC |
493 CLOCK_EVT_FEAT_DUMMY;
494 evt->rating = 400;
495 evt->mult = 1;
496 evt->set_mode = broadcast_timer_set_mode;
Russell Kingbc282482009-05-17 18:58:34 +0100497
498 clockevents_register_device(evt);
499}
Russell Kingbc282482009-05-17 18:58:34 +0100500
Marc Zyngier483e4832012-01-10 19:26:45 +0000501static struct local_timer_ops *lt_ops;
502
503#ifdef CONFIG_LOCAL_TIMERS
504int local_timer_register(struct local_timer_ops *ops)
505{
506 if (lt_ops)
507 return -EBUSY;
508
509 lt_ops = ops;
510 return 0;
511}
512#endif
513
514int __cpuinit __attribute__ ((weak)) local_timer_setup(struct clock_event_device *clk)
515{
516 if (lt_ops)
517 return lt_ops->setup(clk);
518
519 return -ENXIO;
520}
521
522void __attribute__ ((weak)) local_timer_stop(struct clock_event_device *clk)
523{
524 if (lt_ops)
525 lt_ops->stop(clk);
526}
527
Russell Kingbc282482009-05-17 18:58:34 +0100528void __cpuinit percpu_timer_setup(void)
529{
530 unsigned int cpu = smp_processor_id();
531 struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
532
533 evt->cpumask = cpumask_of(cpu);
Russell King5388a6b2010-07-26 13:19:43 +0100534 evt->broadcast = smp_timer_broadcast;
Russell Kingbc282482009-05-17 18:58:34 +0100535
Santosh Shilimkaraf90f102011-02-23 18:53:15 +0100536 if (local_timer_setup(evt))
537 broadcast_timer_setup(evt);
Russell Kingbc282482009-05-17 18:58:34 +0100538}
539
Russell King10034aa2010-12-20 14:28:02 +0000540#ifdef CONFIG_HOTPLUG_CPU
541/*
542 * The generic clock events code purposely does not stop the local timer
543 * on CPU_DEAD/CPU_DEAD_FROZEN hotplug events, so we have to do it
544 * manually here.
545 */
546static void percpu_timer_stop(void)
547{
548 unsigned int cpu = smp_processor_id();
549 struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
550
Trilok Sonieecb28c2011-07-20 16:24:14 +0100551 local_timer_stop(evt);
Russell King10034aa2010-12-20 14:28:02 +0000552}
553#endif
554
Thomas Gleixner450ea482009-07-03 08:44:46 -0500555static DEFINE_RAW_SPINLOCK(stop_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557/*
558 * ipi_cpu_stop - handle IPI from smp_send_stop()
559 */
560static void ipi_cpu_stop(unsigned int cpu)
561{
Russell King3d3f78d2010-07-26 13:31:27 +0100562 if (system_state == SYSTEM_BOOTING ||
563 system_state == SYSTEM_RUNNING) {
Thomas Gleixner450ea482009-07-03 08:44:46 -0500564 raw_spin_lock(&stop_lock);
Russell King3d3f78d2010-07-26 13:31:27 +0100565 printk(KERN_CRIT "CPU%u: stopping\n", cpu);
566 dump_stack();
Thomas Gleixner450ea482009-07-03 08:44:46 -0500567 raw_spin_unlock(&stop_lock);
Russell King3d3f78d2010-07-26 13:31:27 +0100568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Russell Kinge03cdad2009-05-28 14:16:52 +0100570 set_cpu_online(cpu, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 local_fiq_disable();
573 local_irq_disable();
574
575 while (1)
576 cpu_relax();
577}
578
Dima Zavin8b7fd322011-10-18 16:59:54 -0700579static cpumask_t backtrace_mask;
580static DEFINE_RAW_SPINLOCK(backtrace_lock);
581
582/* "in progress" flag of arch_trigger_all_cpu_backtrace */
583static unsigned long backtrace_flag;
584
585void smp_send_all_cpu_backtrace(void)
586{
587 unsigned int this_cpu = smp_processor_id();
588 int i;
589
590 if (test_and_set_bit(0, &backtrace_flag))
591 /*
592 * If there is already a trigger_all_cpu_backtrace() in progress
593 * (backtrace_flag == 1), don't output double cpu dump infos.
594 */
595 return;
596
597 cpumask_copy(&backtrace_mask, cpu_online_mask);
598 cpu_clear(this_cpu, backtrace_mask);
599
600 pr_info("Backtrace for cpu %d (current):\n", this_cpu);
601 dump_stack();
602
603 pr_info("\nsending IPI to all other CPUs:\n");
604 smp_cross_call(&backtrace_mask, IPI_CPU_BACKTRACE);
605
606 /* Wait for up to 10 seconds for all other CPUs to do the backtrace */
607 for (i = 0; i < 10 * 1000; i++) {
608 if (cpumask_empty(&backtrace_mask))
609 break;
610 mdelay(1);
611 }
612
613 clear_bit(0, &backtrace_flag);
614 smp_mb__after_clear_bit();
615}
616
617/*
618 * ipi_cpu_backtrace - handle IPI from smp_send_all_cpu_backtrace()
619 */
620static void ipi_cpu_backtrace(unsigned int cpu, struct pt_regs *regs)
621{
622 if (cpu_isset(cpu, backtrace_mask)) {
623 raw_spin_lock(&backtrace_lock);
624 pr_warning("IPI backtrace for cpu %d\n", cpu);
625 show_regs(regs);
626 raw_spin_unlock(&backtrace_lock);
627 cpu_clear(cpu, backtrace_mask);
628 }
629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631/*
632 * Main handler for inter-processor interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
Russell King40737232011-01-06 22:32:52 +0000634asmlinkage void __exception_irq_entry do_IPI(int ipinr, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Shawn Guo93699402011-10-06 15:18:14 +0100636 handle_IPI(ipinr, regs);
637}
638
639void handle_IPI(int ipinr, struct pt_regs *regs)
640{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 unsigned int cpu = smp_processor_id();
Russell Kingc97d4862006-10-25 13:59:16 +0100642 struct pt_regs *old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Santosh Shilimkar7f685e52011-10-10 19:23:35 -0700644 if (ipinr >= IPI_CPU_START && ipinr < IPI_CPU_START + NR_IPI)
645 __inc_irq_stat(cpu, ipi_irqs[ipinr - IPI_CPU_START]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Russell King24480d92010-11-15 09:54:18 +0000647 switch (ipinr) {
Santosh Shilimkar7f685e52011-10-10 19:23:35 -0700648 case IPI_CPU_START:
649 /* Wake up from WFI/WFE using SGI */
650 break;
Russell King24480d92010-11-15 09:54:18 +0000651 case IPI_TIMER:
Russell King0e6fe722012-01-19 15:20:58 +0000652 irq_enter();
Russell King24480d92010-11-15 09:54:18 +0000653 ipi_timer();
Russell King0e6fe722012-01-19 15:20:58 +0000654 irq_exit();
Russell King24480d92010-11-15 09:54:18 +0000655 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Russell King24480d92010-11-15 09:54:18 +0000657 case IPI_RESCHEDULE:
Peter Zijlstra184748c2011-04-05 17:23:39 +0200658 scheduler_ipi();
Russell King24480d92010-11-15 09:54:18 +0000659 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Russell King24480d92010-11-15 09:54:18 +0000661 case IPI_CALL_FUNC:
Russell King0e6fe722012-01-19 15:20:58 +0000662 irq_enter();
Russell King24480d92010-11-15 09:54:18 +0000663 generic_smp_call_function_interrupt();
Russell King0e6fe722012-01-19 15:20:58 +0000664 irq_exit();
Russell King24480d92010-11-15 09:54:18 +0000665 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Russell King24480d92010-11-15 09:54:18 +0000667 case IPI_CALL_FUNC_SINGLE:
Russell King0e6fe722012-01-19 15:20:58 +0000668 irq_enter();
Russell King24480d92010-11-15 09:54:18 +0000669 generic_smp_call_function_single_interrupt();
Russell King0e6fe722012-01-19 15:20:58 +0000670 irq_exit();
Russell King24480d92010-11-15 09:54:18 +0000671 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Russell King24480d92010-11-15 09:54:18 +0000673 case IPI_CPU_STOP:
Russell King0e6fe722012-01-19 15:20:58 +0000674 irq_enter();
Russell King24480d92010-11-15 09:54:18 +0000675 ipi_cpu_stop(cpu);
Russell King0e6fe722012-01-19 15:20:58 +0000676 irq_exit();
Russell King24480d92010-11-15 09:54:18 +0000677 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Dima Zavin8b7fd322011-10-18 16:59:54 -0700679 case IPI_CPU_BACKTRACE:
680 ipi_cpu_backtrace(cpu, regs);
681 break;
682
Russell King24480d92010-11-15 09:54:18 +0000683 default:
684 printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
685 cpu, ipinr);
686 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Russell Kingc97d4862006-10-25 13:59:16 +0100688 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691void smp_send_reschedule(int cpu)
692{
Vikram Mulukutlab4cb09e2011-07-08 13:40:25 -0700693
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700694 if (unlikely(cpu_is_offline(cpu))) {
Vikram Mulukutlab4cb09e2011-07-08 13:40:25 -0700695 WARN_ON(1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700696 return;
697 }
Russell Kinge3fbb082010-12-20 14:47:19 +0000698 smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701void smp_send_stop(void)
702{
Russell King28e18292010-12-02 09:53:54 +0000703 unsigned long timeout;
704
705 if (num_online_cpus() > 1) {
706 cpumask_t mask = cpu_online_map;
707 cpu_clear(smp_processor_id(), mask);
708
Russell Kinge3fbb082010-12-20 14:47:19 +0000709 smp_cross_call(&mask, IPI_CPU_STOP);
Russell King28e18292010-12-02 09:53:54 +0000710 }
711
712 /* Wait up to one second for other CPUs to stop */
713 timeout = USEC_PER_SEC;
714 while (num_online_cpus() > 1 && timeout--)
715 udelay(1);
716
717 if (num_online_cpus() > 1)
718 pr_warning("SMP: failed to stop secondary CPUs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721/*
722 * not supported here
723 */
Russell King5048bcb2007-07-23 12:59:46 +0100724int setup_profiling_timer(unsigned int multiplier)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 return -EINVAL;
727}