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.
13 #include <linux/kernel.h>
14 #include <linux/smp.h>
15 #include <linux/reboot.h>
16 #include <linux/kexec.h>
17 #include <linux/export.h>
18 #include <linux/crash_dump.h>
19 #include <linux/delay.h>
20 #include <linux/init.h>
21 #include <linux/irq.h>
22 #include <linux/types.h>
24 #include <asm/processor.h>
25 #include <asm/machdep.h>
26 #include <asm/kexec.h>
27 #include <asm/kdump.h>
30 #include <asm/system.h>
31 #include <asm/setjmp.h>
34 * The primary CPU waits a while for all secondary CPUs to enter. This is to
35 * avoid sending an IPI if the secondary CPUs are entering
36 * crash_kexec_secondary on their own (eg via a system reset).
38 * The secondary timeout has to be longer than the primary. Both timeouts are
41 #define PRIMARY_TIMEOUT 500
42 #define SECONDARY_TIMEOUT 1000
44 #define IPI_TIMEOUT 10000
45 #define REAL_MODE_TIMEOUT 10000
47 /* This keeps a track of which one is the crashing cpu. */
48 int crashing_cpu
= -1;
49 static int time_to_dump
;
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
);
56 static unsigned long crash_shutdown_buf
[JMP_BUF_LEN
];
57 static int crash_shutdown_cpu
= -1;
59 static int handle_fault(struct pt_regs
*regs
)
61 if (crash_shutdown_cpu
== smp_processor_id())
62 longjmp(crash_shutdown_buf
, 1);
68 static atomic_t cpus_in_crash
;
69 void crash_ipi_callback(struct pt_regs
*regs
)
71 static cpumask_t cpus_state_saved
= CPU_MASK_NONE
;
73 int cpu
= smp_processor_id();
79 if (!cpumask_test_cpu(cpu
, &cpus_state_saved
)) {
80 crash_save_cpu(regs
, cpu
);
81 cpumask_set_cpu(cpu
, &cpus_state_saved
);
84 atomic_inc(&cpus_in_crash
);
85 smp_mb__after_atomic_inc();
88 * Starting the kdump boot.
89 * This barrier is needed to make sure that all CPUs are stopped.
94 if (ppc_md
.kexec_cpu_down
)
95 ppc_md
.kexec_cpu_down(1, 1);
100 for (;;); /* FIXME */
106 static void crash_kexec_prepare_cpus(int cpu
)
109 unsigned int ncpus
= num_online_cpus() - 1;/* Excluding the panic cpu */
111 int (*old_handler
)(struct pt_regs
*regs
);
113 printk(KERN_EMERG
"Sending IPI to other CPUs\n");
115 crash_send_ipi(crash_ipi_callback
);
120 * FIXME: Until we will have the way to stop other CPUs reliably,
121 * the crash CPU will send an IPI and wait for other CPUs to
125 while ((atomic_read(&cpus_in_crash
) < ncpus
) && (--msecs
> 0))
128 /* Would it be better to replace the trap vector here? */
130 if (atomic_read(&cpus_in_crash
) >= ncpus
) {
131 printk(KERN_EMERG
"IPI complete\n");
135 printk(KERN_EMERG
"ERROR: %d cpu(s) not responding\n",
136 ncpus
- atomic_read(&cpus_in_crash
));
139 * If we have a panic timeout set then we can't wait indefinitely
140 * for someone to activate system reset. We also give up on the
141 * second time through if system reset fail to work.
143 if ((panic_timeout
> 0) || (tries
> 0))
147 * A system reset will cause all CPUs to take an 0x100 exception.
148 * The primary CPU returns here via setjmp, and the secondary
149 * CPUs reexecute the crash_kexec_secondary path.
151 old_handler
= __debugger
;
152 __debugger
= handle_fault
;
153 crash_shutdown_cpu
= smp_processor_id();
155 if (setjmp(crash_shutdown_buf
) == 0) {
156 printk(KERN_EMERG
"Activate system reset (dumprestart) "
157 "to stop other cpu(s)\n");
160 * A system reset will force all CPUs to execute the
161 * crash code again. We need to reset cpus_in_crash so we
162 * wait for everyone to do this.
164 atomic_set(&cpus_in_crash
, 0);
167 while (atomic_read(&cpus_in_crash
) < ncpus
)
171 crash_shutdown_cpu
= -1;
172 __debugger
= old_handler
;
179 * This function will be called by secondary cpus.
181 void crash_kexec_secondary(struct pt_regs
*regs
)
184 int msecs
= SECONDARY_TIMEOUT
;
186 local_irq_save(flags
);
188 /* Wait for the primary crash CPU to signal its progress */
189 while (crashing_cpu
< 0) {
191 /* No response, kdump image may not have been loaded */
192 local_irq_restore(flags
);
199 crash_ipi_callback(regs
);
202 #else /* ! CONFIG_SMP */
204 static void crash_kexec_prepare_cpus(int cpu
)
207 * move the secondaries to us so that we can copy
208 * the new kernel 0-0x100 safely
210 * do this if kexec in setup.c ?
219 void crash_kexec_secondary(struct pt_regs
*regs
)
222 #endif /* CONFIG_SMP */
224 /* wait for all the CPUs to hit real mode but timeout if they don't come in */
225 #if defined(CONFIG_SMP) && defined(CONFIG_PPC_STD_MMU_64)
226 static void crash_kexec_wait_realmode(int cpu
)
231 msecs
= REAL_MODE_TIMEOUT
;
232 for (i
=0; i
< nr_cpu_ids
&& msecs
> 0; i
++) {
236 while (paca
[i
].kexec_state
< KEXEC_STATE_REAL_MODE
) {
238 if (!cpu_possible(i
) || !cpu_online(i
) || (msecs
<= 0))
247 static inline void crash_kexec_wait_realmode(int cpu
) {}
248 #endif /* CONFIG_SMP && CONFIG_PPC_STD_MMU_64 */
251 * Register a function to be called on shutdown. Only use this if you
252 * can't reset your device in the second kernel.
254 int crash_shutdown_register(crash_shutdown_t handler
)
258 spin_lock(&crash_handlers_lock
);
259 for (i
= 0 ; i
< CRASH_HANDLER_MAX
; i
++)
260 if (!crash_shutdown_handles
[i
]) {
261 /* Insert handle at first empty entry */
262 crash_shutdown_handles
[i
] = handler
;
267 if (i
== CRASH_HANDLER_MAX
) {
268 printk(KERN_ERR
"Crash shutdown handles full, "
269 "not registered.\n");
273 spin_unlock(&crash_handlers_lock
);
276 EXPORT_SYMBOL(crash_shutdown_register
);
278 int crash_shutdown_unregister(crash_shutdown_t handler
)
282 spin_lock(&crash_handlers_lock
);
283 for (i
= 0 ; i
< CRASH_HANDLER_MAX
; i
++)
284 if (crash_shutdown_handles
[i
] == handler
)
287 if (i
== CRASH_HANDLER_MAX
) {
288 printk(KERN_ERR
"Crash shutdown handle not found\n");
291 /* Shift handles down */
292 for (; crash_shutdown_handles
[i
]; i
++)
293 crash_shutdown_handles
[i
] =
294 crash_shutdown_handles
[i
+1];
298 spin_unlock(&crash_handlers_lock
);
301 EXPORT_SYMBOL(crash_shutdown_unregister
);
303 void default_machine_crash_shutdown(struct pt_regs
*regs
)
306 int (*old_handler
)(struct pt_regs
*regs
);
309 * This function is only called after the system
310 * has panicked or is otherwise in a critical state.
311 * The minimum amount of code to allow a kexec'd kernel
312 * to run successfully needs to happen here.
314 * In practice this means stopping other cpus in
316 * The kernel is broken so disable interrupts.
321 * Make a note of crashing cpu. Will be used in machine_kexec
322 * such that another IPI will not be sent.
324 crashing_cpu
= smp_processor_id();
327 * If we came in via system reset, wait a while for the secondary
330 if (TRAP(regs
) == 0x100)
331 mdelay(PRIMARY_TIMEOUT
);
333 crash_kexec_prepare_cpus(crashing_cpu
);
335 crash_save_cpu(regs
, crashing_cpu
);
339 crash_kexec_wait_realmode(crashing_cpu
);
341 machine_kexec_mask_interrupts();
344 * Call registered shutdown routines safely. Swap out
345 * __debugger_fault_handler, and replace on exit.
347 old_handler
= __debugger_fault_handler
;
348 __debugger_fault_handler
= handle_fault
;
349 crash_shutdown_cpu
= smp_processor_id();
350 for (i
= 0; crash_shutdown_handles
[i
]; i
++) {
351 if (setjmp(crash_shutdown_buf
) == 0) {
353 * Insert syncs and delay to ensure
354 * instructions in the dangerous region don't
355 * leak away from this protected region.
357 asm volatile("sync; isync");
358 /* dangerous region */
359 crash_shutdown_handles
[i
]();
360 asm volatile("sync; isync");
363 crash_shutdown_cpu
= -1;
364 __debugger_fault_handler
= old_handler
;
366 if (ppc_md
.kexec_cpu_down
)
367 ppc_md
.kexec_cpu_down(1, 0);