2 * Architecture specific (PPC64) functions for kexec based crash dumps.
4 * Copyright (C) 2005, IBM Corp.
6 * Created by: Haren Myneni
8 * This source code is licensed under the GNU General Public License,
9 * Version 2. See the file COPYING for more details.
15 #include <linux/kernel.h>
16 #include <linux/smp.h>
17 #include <linux/reboot.h>
18 #include <linux/kexec.h>
19 #include <linux/bootmem.h>
20 #include <linux/export.h>
21 #include <linux/crash_dump.h>
22 #include <linux/delay.h>
23 #include <linux/elf.h>
24 #include <linux/elfcore.h>
25 #include <linux/init.h>
26 #include <linux/irq.h>
27 #include <linux/types.h>
28 #include <linux/memblock.h>
30 #include <asm/processor.h>
31 #include <asm/machdep.h>
32 #include <asm/kexec.h>
33 #include <asm/kdump.h>
35 #include <asm/firmware.h>
37 #include <asm/system.h>
38 #include <asm/setjmp.h>
42 #define DBG(fmt...) udbg_printf(fmt)
47 /* This keeps a track of which one is crashing cpu. */
48 int crashing_cpu = -1;
49 static cpumask_t cpus_in_crash = CPU_MASK_NONE;
51 #define CRASH_HANDLER_MAX 3
52 /* NULL terminated list of shutdown handles */
53 static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX+1];
54 static DEFINE_SPINLOCK(crash_handlers_lock);
58 void crash_ipi_callback(struct pt_regs *regs)
60 int cpu = smp_processor_id();
66 if (!cpumask_test_cpu(cpu, &cpus_in_crash))
67 crash_save_cpu(regs, cpu);
68 cpumask_set_cpu(cpu, &cpus_in_crash);
71 * Starting the kdump boot.
72 * This barrier is needed to make sure that all CPUs are stopped.
74 while (!cpumask_test_cpu(crashing_cpu, &cpus_in_crash))
77 if (ppc_md.kexec_cpu_down)
78 ppc_md.kexec_cpu_down(1, 1);
89 static void crash_kexec_prepare_cpus(int cpu)
93 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
95 printk(KERN_EMERG "Sending IPI to other CPUs\n");
97 crash_send_ipi(crash_ipi_callback);
101 * FIXME: Until we will have the way to stop other CPUs reliably,
102 * the crash CPU will send an IPI and wait for other CPUs to
104 * Delay of at least 10 seconds.
107 while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) {
112 /* Would it be better to replace the trap vector here? */
114 if (cpumask_weight(&cpus_in_crash) < ncpus) {
115 printk(KERN_EMERG "ERROR: %d CPU(s) not responding\n",
116 ncpus - cpumask_weight(&cpus_in_crash));
119 printk(KERN_EMERG "IPI complete\n");
123 * This function will be called by secondary cpus.
125 void crash_kexec_secondary(struct pt_regs *regs)
130 local_irq_save(flags);
132 /* Wait 500ms for the primary crash CPU to signal its progress */
133 while (crashing_cpu < 0) {
135 /* No response, kdump image may not have been loaded */
136 local_irq_restore(flags);
144 crash_ipi_callback(regs);
147 #else /* ! CONFIG_SMP */
149 static void crash_kexec_prepare_cpus(int cpu)
152 * move the secondarys to us so that we can copy
153 * the new kernel 0-0x100 safely
155 * do this if kexec in setup.c ?
164 void crash_kexec_secondary(struct pt_regs *regs)
167 #endif /* CONFIG_SMP */
169 /* wait for all the CPUs to hit real mode but timeout if they don't come in */
170 #if defined(CONFIG_SMP) && defined(CONFIG_PPC_STD_MMU_64)
171 static void crash_kexec_wait_realmode(int cpu)
177 for (i=0; i < nr_cpu_ids && msecs > 0; i++) {
181 while (paca[i].kexec_state < KEXEC_STATE_REAL_MODE) {
183 if (!cpu_possible(i) || !cpu_online(i) || (msecs <= 0))
192 static inline void crash_kexec_wait_realmode(int cpu) {}
193 #endif /* CONFIG_SMP && CONFIG_PPC_STD_MMU_64 */
196 * Register a function to be called on shutdown. Only use this if you
197 * can't reset your device in the second kernel.
199 int crash_shutdown_register(crash_shutdown_t handler)
203 spin_lock(&crash_handlers_lock);
204 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
205 if (!crash_shutdown_handles[i]) {
206 /* Insert handle at first empty entry */
207 crash_shutdown_handles[i] = handler;
212 if (i == CRASH_HANDLER_MAX) {
213 printk(KERN_ERR "Crash shutdown handles full, "
214 "not registered.\n");
218 spin_unlock(&crash_handlers_lock);
221 EXPORT_SYMBOL(crash_shutdown_register);
223 int crash_shutdown_unregister(crash_shutdown_t handler)
227 spin_lock(&crash_handlers_lock);
228 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
229 if (crash_shutdown_handles[i] == handler)
232 if (i == CRASH_HANDLER_MAX) {
233 printk(KERN_ERR "Crash shutdown handle not found\n");
236 /* Shift handles down */
237 for (; crash_shutdown_handles[i]; i++)
238 crash_shutdown_handles[i] =
239 crash_shutdown_handles[i+1];
243 spin_unlock(&crash_handlers_lock);
246 EXPORT_SYMBOL(crash_shutdown_unregister);
248 static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
249 static int crash_shutdown_cpu = -1;
251 static int handle_fault(struct pt_regs *regs)
253 if (crash_shutdown_cpu == smp_processor_id())
254 longjmp(crash_shutdown_buf, 1);
258 void default_machine_crash_shutdown(struct pt_regs *regs)
261 int (*old_handler)(struct pt_regs *regs);
265 * This function is only called after the system
266 * has panicked or is otherwise in a critical state.
267 * The minimum amount of code to allow a kexec'd kernel
268 * to run successfully needs to happen here.
270 * In practice this means stopping other cpus in
272 * The kernel is broken so disable interrupts.
277 * Make a note of crashing cpu. Will be used in machine_kexec
278 * such that another IPI will not be sent.
280 crashing_cpu = smp_processor_id();
281 crash_save_cpu(regs, crashing_cpu);
282 crash_kexec_prepare_cpus(crashing_cpu);
283 cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
284 crash_kexec_wait_realmode(crashing_cpu);
286 machine_kexec_mask_interrupts();
289 * Call registered shutdown routines savely. Swap out
290 * __debugger_fault_handler, and replace on exit.
292 old_handler = __debugger_fault_handler;
293 __debugger_fault_handler = handle_fault;
294 crash_shutdown_cpu = smp_processor_id();
295 for (i = 0; crash_shutdown_handles[i]; i++) {
296 if (setjmp(crash_shutdown_buf) == 0) {
298 * Insert syncs and delay to ensure
299 * instructions in the dangerous region don't
300 * leak away from this protected region.
302 asm volatile("sync; isync");
303 /* dangerous region */
304 crash_shutdown_handles[i]();
305 asm volatile("sync; isync");
308 crash_shutdown_cpu = -1;
309 __debugger_fault_handler = old_handler;
311 if (ppc_md.kexec_cpu_down)
312 ppc_md.kexec_cpu_down(1, 0);