blob: 4c07a43a3242a1ebe96004de447060b0c0888fbd [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +02002#include <linux/kernel.h>
3#include <linux/smp.h>
4#include <linux/reboot.h>
5#include <linux/kexec.h>
6#include <linux/bootmem.h>
7#include <linux/crash_dump.h>
8#include <linux/delay.h>
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +02009#include <linux/irq.h>
10#include <linux/types.h>
11#include <linux/sched.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010012#include <linux/sched/task_stack.h>
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020013
14/* This keeps a track of which one is crashing cpu. */
15static int crashing_cpu = -1;
16static cpumask_t cpus_in_crash = CPU_MASK_NONE;
17
18#ifdef CONFIG_SMP
Corey Minyardc80e1b62016-04-11 09:10:19 -050019static void crash_shutdown_secondary(void *passed_regs)
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020020{
Corey Minyardc80e1b62016-04-11 09:10:19 -050021 struct pt_regs *regs = passed_regs;
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020022 int cpu = smp_processor_id();
23
Corey Minyardc80e1b62016-04-11 09:10:19 -050024 /*
25 * If we are passed registers, use those. Otherwise get the
26 * regs from the last interrupt, which should be correct, as
27 * we are in an interrupt. But if the regs are not there,
28 * pull them from the top of the stack. They are probably
29 * wrong, but we need something to keep from crashing again.
30 */
31 if (!regs)
32 regs = get_irq_regs();
33 if (!regs)
34 regs = task_pt_regs(current);
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020035
36 if (!cpu_online(cpu))
37 return;
38
Dengcheng Zhudc57aaf2018-09-11 14:49:20 -070039 /* We won't be sent IPIs any more. */
40 set_cpu_online(cpu, false);
41
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020042 local_irq_disable();
Rusty Russell8dd92892015-03-05 10:49:17 +103043 if (!cpumask_test_cpu(cpu, &cpus_in_crash))
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020044 crash_save_cpu(regs, cpu);
Rusty Russell8dd92892015-03-05 10:49:17 +103045 cpumask_set_cpu(cpu, &cpus_in_crash);
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020046
47 while (!atomic_read(&kexec_ready_to_reboot))
48 cpu_relax();
49 relocated_kexec_smp_wait(NULL);
50 /* NOTREACHED */
51}
52
53static void crash_kexec_prepare_cpus(void)
54{
Hidehiro Kawai54c721b2016-10-11 13:54:26 -070055 static int cpus_stopped;
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020056 unsigned int msecs;
Hidehiro Kawai54c721b2016-10-11 13:54:26 -070057 unsigned int ncpus;
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020058
Hidehiro Kawai54c721b2016-10-11 13:54:26 -070059 if (cpus_stopped)
60 return;
61
62 ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020063
Marcin Nowakowskic83c2ee2016-12-02 09:58:28 +010064 smp_call_function(crash_shutdown_secondary, NULL, 0);
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020065 smp_wmb();
66
67 /*
68 * The crash CPU sends an IPI and wait for other CPUs to
69 * respond. Delay of at least 10 seconds.
70 */
71 pr_emerg("Sending IPI to other cpus...\n");
72 msecs = 10000;
Rusty Russell8dd92892015-03-05 10:49:17 +103073 while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) {
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020074 cpu_relax();
75 mdelay(1);
76 }
Hidehiro Kawai54c721b2016-10-11 13:54:26 -070077
78 cpus_stopped = 1;
79}
80
81/* Override the weak function in kernel/panic.c */
82void crash_smp_send_stop(void)
83{
84 if (_crash_smp_send_stop)
85 _crash_smp_send_stop();
86
87 crash_kexec_prepare_cpus();
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020088}
89
90#else /* !defined(CONFIG_SMP) */
91static void crash_kexec_prepare_cpus(void) {}
Ralf Baechle70342282013-01-22 12:59:30 +010092#endif /* !defined(CONFIG_SMP) */
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020093
94void default_machine_crash_shutdown(struct pt_regs *regs)
95{
96 local_irq_disable();
97 crashing_cpu = smp_processor_id();
98 crash_save_cpu(regs, crashing_cpu);
99 crash_kexec_prepare_cpus();
Rusty Russell8dd92892015-03-05 10:49:17 +1030100 cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200101}