blob: 5a71518be0f10b550187135741e994a852c900b3 [file] [log] [blame]
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +02001#include <linux/kernel.h>
2#include <linux/smp.h>
3#include <linux/reboot.h>
4#include <linux/kexec.h>
5#include <linux/bootmem.h>
6#include <linux/crash_dump.h>
7#include <linux/delay.h>
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +02008#include <linux/irq.h>
9#include <linux/types.h>
10#include <linux/sched.h>
11
12/* This keeps a track of which one is crashing cpu. */
13static int crashing_cpu = -1;
14static cpumask_t cpus_in_crash = CPU_MASK_NONE;
15
16#ifdef CONFIG_SMP
Corey Minyardc80e1b62016-04-11 09:10:19 -050017static void crash_shutdown_secondary(void *passed_regs)
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020018{
Corey Minyardc80e1b62016-04-11 09:10:19 -050019 struct pt_regs *regs = passed_regs;
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020020 int cpu = smp_processor_id();
21
Corey Minyardc80e1b62016-04-11 09:10:19 -050022 /*
23 * If we are passed registers, use those. Otherwise get the
24 * regs from the last interrupt, which should be correct, as
25 * we are in an interrupt. But if the regs are not there,
26 * pull them from the top of the stack. They are probably
27 * wrong, but we need something to keep from crashing again.
28 */
29 if (!regs)
30 regs = get_irq_regs();
31 if (!regs)
32 regs = task_pt_regs(current);
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020033
34 if (!cpu_online(cpu))
35 return;
36
37 local_irq_disable();
Rusty Russell8dd92892015-03-05 10:49:17 +103038 if (!cpumask_test_cpu(cpu, &cpus_in_crash))
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020039 crash_save_cpu(regs, cpu);
Rusty Russell8dd92892015-03-05 10:49:17 +103040 cpumask_set_cpu(cpu, &cpus_in_crash);
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020041
42 while (!atomic_read(&kexec_ready_to_reboot))
43 cpu_relax();
44 relocated_kexec_smp_wait(NULL);
45 /* NOTREACHED */
46}
47
48static void crash_kexec_prepare_cpus(void)
49{
Hidehiro Kawai54c721b2016-10-11 13:54:26 -070050 static int cpus_stopped;
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020051 unsigned int msecs;
Hidehiro Kawai54c721b2016-10-11 13:54:26 -070052 unsigned int ncpus;
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020053
Hidehiro Kawai54c721b2016-10-11 13:54:26 -070054 if (cpus_stopped)
55 return;
56
57 ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020058
Marcin Nowakowskic83c2ee2016-12-02 09:58:28 +010059 smp_call_function(crash_shutdown_secondary, NULL, 0);
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020060 smp_wmb();
61
62 /*
63 * The crash CPU sends an IPI and wait for other CPUs to
64 * respond. Delay of at least 10 seconds.
65 */
66 pr_emerg("Sending IPI to other cpus...\n");
67 msecs = 10000;
Rusty Russell8dd92892015-03-05 10:49:17 +103068 while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) {
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020069 cpu_relax();
70 mdelay(1);
71 }
Hidehiro Kawai54c721b2016-10-11 13:54:26 -070072
73 cpus_stopped = 1;
74}
75
76/* Override the weak function in kernel/panic.c */
77void crash_smp_send_stop(void)
78{
79 if (_crash_smp_send_stop)
80 _crash_smp_send_stop();
81
82 crash_kexec_prepare_cpus();
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020083}
84
85#else /* !defined(CONFIG_SMP) */
86static void crash_kexec_prepare_cpus(void) {}
Ralf Baechle70342282013-01-22 12:59:30 +010087#endif /* !defined(CONFIG_SMP) */
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020088
89void default_machine_crash_shutdown(struct pt_regs *regs)
90{
91 local_irq_disable();
92 crashing_cpu = smp_processor_id();
93 crash_save_cpu(regs, crashing_cpu);
94 crash_kexec_prepare_cpus();
Rusty Russell8dd92892015-03-05 10:49:17 +103095 cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020096}