1 // SPDX-License-Identifier: GPL-2.0-only
3 * Architecture specific (PPC64) functions for kexec based crash dumps.
5 * Copyright (C) 2005, IBM Corp.
7 * Created by: Haren Myneni
10 #include <linux/kernel.h>
11 #include <linux/smp.h>
12 #include <linux/reboot.h>
13 #include <linux/kexec.h>
14 #include <linux/export.h>
15 #include <linux/crash_dump.h>
16 #include <linux/delay.h>
17 #include <linux/irq.h>
18 #include <linux/types.h>
20 #include <asm/processor.h>
21 #include <asm/machdep.h>
22 #include <asm/kexec.h>
25 #include <asm/setjmp.h>
26 #include <asm/debug.h>
29 * The primary CPU waits a while for all secondary CPUs to enter. This is to
30 * avoid sending an IPI if the secondary CPUs are entering
31 * crash_kexec_secondary on their own (eg via a system reset).
33 * The secondary timeout has to be longer than the primary. Both timeouts are
36 #define PRIMARY_TIMEOUT 500
37 #define SECONDARY_TIMEOUT 1000
39 #define IPI_TIMEOUT 10000
40 #define REAL_MODE_TIMEOUT 10000
42 static int time_to_dump;
44 * crash_wake_offline should be set to 1 by platforms that intend to wake
45 * up offline cpus prior to jumping to a kdump kernel. Currently powernv
46 * sets it to 1, since we want to avoid things from happening when an
47 * offline CPU wakes up due to something like an HMI (malfunction error),
48 * which propagates to all threads.
50 int crash_wake_offline;
52 #define CRASH_HANDLER_MAX 3
53 /* List of shutdown handles */
54 static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX];
55 static DEFINE_SPINLOCK(crash_handlers_lock);
57 static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
58 static int crash_shutdown_cpu = -1;
60 static int handle_fault(struct pt_regs *regs)
62 if (crash_shutdown_cpu == smp_processor_id())
63 longjmp(crash_shutdown_buf, 1);
69 static atomic_t cpus_in_crash;
70 void crash_ipi_callback(struct pt_regs *regs)
72 static cpumask_t cpus_state_saved = CPU_MASK_NONE;
74 int cpu = smp_processor_id();
77 if (!cpumask_test_cpu(cpu, &cpus_state_saved)) {
78 crash_save_cpu(regs, cpu);
79 cpumask_set_cpu(cpu, &cpus_state_saved);
82 atomic_inc(&cpus_in_crash);
83 smp_mb__after_atomic();
86 * Starting the kdump boot.
87 * This barrier is needed to make sure that all CPUs are stopped.
92 if (ppc_md.kexec_cpu_down)
93 ppc_md.kexec_cpu_down(1, 1);
104 static void crash_kexec_prepare_cpus(int cpu)
107 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
109 int (*old_handler)(struct pt_regs *regs);
111 printk(KERN_EMERG "Sending IPI to other CPUs\n");
113 if (crash_wake_offline)
114 ncpus = num_present_cpus() - 1;
116 crash_send_ipi(crash_ipi_callback);
121 * FIXME: Until we will have the way to stop other CPUs reliably,
122 * the crash CPU will send an IPI and wait for other CPUs to
126 while ((atomic_read(&cpus_in_crash) < ncpus) && (--msecs > 0))
129 /* Would it be better to replace the trap vector here? */
131 if (atomic_read(&cpus_in_crash) >= ncpus) {
132 printk(KERN_EMERG "IPI complete\n");
136 printk(KERN_EMERG "ERROR: %d cpu(s) not responding\n",
137 ncpus - atomic_read(&cpus_in_crash));
140 * If we have a panic timeout set then we can't wait indefinitely
141 * for someone to activate system reset. We also give up on the
142 * second time through if system reset fail to work.
144 if ((panic_timeout > 0) || (tries > 0))
148 * A system reset will cause all CPUs to take an 0x100 exception.
149 * The primary CPU returns here via setjmp, and the secondary
150 * CPUs reexecute the crash_kexec_secondary path.
152 old_handler = __debugger;
153 __debugger = handle_fault;
154 crash_shutdown_cpu = smp_processor_id();
156 if (setjmp(crash_shutdown_buf) == 0) {
157 printk(KERN_EMERG "Activate system reset (dumprestart) "
158 "to stop other cpu(s)\n");
161 * A system reset will force all CPUs to execute the
162 * crash code again. We need to reset cpus_in_crash so we
163 * wait for everyone to do this.
165 atomic_set(&cpus_in_crash, 0);
168 while (atomic_read(&cpus_in_crash) < ncpus)
172 crash_shutdown_cpu = -1;
173 __debugger = old_handler;
180 * This function will be called by secondary cpus.
182 void crash_kexec_secondary(struct pt_regs *regs)
185 int msecs = SECONDARY_TIMEOUT;
187 local_irq_save(flags);
189 /* Wait for the primary crash CPU to signal its progress */
190 while (crashing_cpu < 0) {
192 /* No response, kdump image may not have been loaded */
193 local_irq_restore(flags);
200 crash_ipi_callback(regs);
203 #else /* ! CONFIG_SMP */
205 static void crash_kexec_prepare_cpus(int cpu)
208 * move the secondaries to us so that we can copy
209 * the new kernel 0-0x100 safely
211 * do this if kexec in setup.c ?
220 void crash_kexec_secondary(struct pt_regs *regs)
223 #endif /* CONFIG_SMP */
225 /* wait for all the CPUs to hit real mode but timeout if they don't come in */
226 #if defined(CONFIG_SMP) && defined(CONFIG_PPC64)
227 static void __maybe_unused crash_kexec_wait_realmode(int cpu)
232 msecs = REAL_MODE_TIMEOUT;
233 for (i=0; i < nr_cpu_ids && msecs > 0; i++) {
237 while (paca_ptrs[i]->kexec_state < KEXEC_STATE_REAL_MODE) {
239 if (!cpu_possible(i) || !cpu_online(i) || (msecs <= 0))
248 static inline void crash_kexec_wait_realmode(int cpu) {}
249 #endif /* CONFIG_SMP && CONFIG_PPC64 */
252 * Register a function to be called on shutdown. Only use this if you
253 * can't reset your device in the second kernel.
255 int crash_shutdown_register(crash_shutdown_t handler)
259 spin_lock(&crash_handlers_lock);
260 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
261 if (!crash_shutdown_handles[i]) {
262 /* Insert handle at first empty entry */
263 crash_shutdown_handles[i] = handler;
268 if (i == CRASH_HANDLER_MAX) {
269 printk(KERN_ERR "Crash shutdown handles full, "
270 "not registered.\n");
274 spin_unlock(&crash_handlers_lock);
277 EXPORT_SYMBOL(crash_shutdown_register);
279 int crash_shutdown_unregister(crash_shutdown_t handler)
283 spin_lock(&crash_handlers_lock);
284 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
285 if (crash_shutdown_handles[i] == handler)
288 if (i == CRASH_HANDLER_MAX) {
289 printk(KERN_ERR "Crash shutdown handle not found\n");
292 /* Shift handles down */
293 for (; i < (CRASH_HANDLER_MAX - 1); i++)
294 crash_shutdown_handles[i] =
295 crash_shutdown_handles[i+1];
297 * Reset last entry to NULL now that it has been shifted down,
298 * this will allow new handles to be added here.
300 crash_shutdown_handles[i] = NULL;
304 spin_unlock(&crash_handlers_lock);
307 EXPORT_SYMBOL(crash_shutdown_unregister);
309 void default_machine_crash_shutdown(struct pt_regs *regs)
312 int (*old_handler)(struct pt_regs *regs);
315 * This function is only called after the system
316 * has panicked or is otherwise in a critical state.
317 * The minimum amount of code to allow a kexec'd kernel
318 * to run successfully needs to happen here.
320 * In practice this means stopping other cpus in
322 * The kernel is broken so disable interrupts.
327 * Make a note of crashing cpu. Will be used in machine_kexec
328 * such that another IPI will not be sent.
330 crashing_cpu = smp_processor_id();
333 * If we came in via system reset, wait a while for the secondary
336 if (TRAP(regs) == 0x100)
337 mdelay(PRIMARY_TIMEOUT);
339 crash_kexec_prepare_cpus(crashing_cpu);
341 crash_save_cpu(regs, crashing_cpu);
345 crash_kexec_wait_realmode(crashing_cpu);
347 machine_kexec_mask_interrupts();
350 * Call registered shutdown routines safely. Swap out
351 * __debugger_fault_handler, and replace on exit.
353 old_handler = __debugger_fault_handler;
354 __debugger_fault_handler = handle_fault;
355 crash_shutdown_cpu = smp_processor_id();
356 for (i = 0; i < CRASH_HANDLER_MAX && crash_shutdown_handles[i]; i++) {
357 if (setjmp(crash_shutdown_buf) == 0) {
359 * Insert syncs and delay to ensure
360 * instructions in the dangerous region don't
361 * leak away from this protected region.
363 asm volatile("sync; isync");
364 /* dangerous region */
365 crash_shutdown_handles[i]();
366 asm volatile("sync; isync");
369 crash_shutdown_cpu = -1;
370 __debugger_fault_handler = old_handler;
372 if (ppc_md.kexec_cpu_down)
373 ppc_md.kexec_cpu_down(1, 0);