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/irq.h>
21 #include <linux/types.h>
23 #include <asm/processor.h>
24 #include <asm/machdep.h>
25 #include <asm/kexec.h>
26 #include <asm/kdump.h>
29 #include <asm/setjmp.h>
30 #include <asm/debug.h>
33 * The primary CPU waits a while for all secondary CPUs to enter. This is to
34 * avoid sending an IPI if the secondary CPUs are entering
35 * crash_kexec_secondary on their own (eg via a system reset).
37 * The secondary timeout has to be longer than the primary. Both timeouts are
40 #define PRIMARY_TIMEOUT 500
41 #define SECONDARY_TIMEOUT 1000
43 #define IPI_TIMEOUT 10000
44 #define REAL_MODE_TIMEOUT 10000
46 static int time_to_dump
;
48 #define CRASH_HANDLER_MAX 3
49 /* List of shutdown handles */
50 static crash_shutdown_t crash_shutdown_handles
[CRASH_HANDLER_MAX
];
51 static DEFINE_SPINLOCK(crash_handlers_lock
);
53 static unsigned long crash_shutdown_buf
[JMP_BUF_LEN
];
54 static int crash_shutdown_cpu
= -1;
56 static int handle_fault(struct pt_regs
*regs
)
58 if (crash_shutdown_cpu
== smp_processor_id())
59 longjmp(crash_shutdown_buf
, 1);
65 static atomic_t cpus_in_crash
;
66 static void crash_ipi_callback(struct pt_regs
*regs
)
68 static cpumask_t cpus_state_saved
= CPU_MASK_NONE
;
70 int cpu
= smp_processor_id();
76 if (!cpumask_test_cpu(cpu
, &cpus_state_saved
)) {
77 crash_save_cpu(regs
, cpu
);
78 cpumask_set_cpu(cpu
, &cpus_state_saved
);
81 atomic_inc(&cpus_in_crash
);
82 smp_mb__after_atomic();
85 * Starting the kdump boot.
86 * This barrier is needed to make sure that all CPUs are stopped.
91 if (ppc_md
.kexec_cpu_down
)
92 ppc_md
.kexec_cpu_down(1, 1);
103 static void crash_kexec_prepare_cpus(int cpu
)
106 unsigned int ncpus
= num_online_cpus() - 1;/* Excluding the panic cpu */
108 int (*old_handler
)(struct pt_regs
*regs
);
110 printk(KERN_EMERG
"Sending IPI to other CPUs\n");
112 crash_send_ipi(crash_ipi_callback
);
117 * FIXME: Until we will have the way to stop other CPUs reliably,
118 * the crash CPU will send an IPI and wait for other CPUs to
122 while ((atomic_read(&cpus_in_crash
) < ncpus
) && (--msecs
> 0))
125 /* Would it be better to replace the trap vector here? */
127 if (atomic_read(&cpus_in_crash
) >= ncpus
) {
128 printk(KERN_EMERG
"IPI complete\n");
132 printk(KERN_EMERG
"ERROR: %d cpu(s) not responding\n",
133 ncpus
- atomic_read(&cpus_in_crash
));
136 * If we have a panic timeout set then we can't wait indefinitely
137 * for someone to activate system reset. We also give up on the
138 * second time through if system reset fail to work.
140 if ((panic_timeout
> 0) || (tries
> 0))
144 * A system reset will cause all CPUs to take an 0x100 exception.
145 * The primary CPU returns here via setjmp, and the secondary
146 * CPUs reexecute the crash_kexec_secondary path.
148 old_handler
= __debugger
;
149 __debugger
= handle_fault
;
150 crash_shutdown_cpu
= smp_processor_id();
152 if (setjmp(crash_shutdown_buf
) == 0) {
153 printk(KERN_EMERG
"Activate system reset (dumprestart) "
154 "to stop other cpu(s)\n");
157 * A system reset will force all CPUs to execute the
158 * crash code again. We need to reset cpus_in_crash so we
159 * wait for everyone to do this.
161 atomic_set(&cpus_in_crash
, 0);
164 while (atomic_read(&cpus_in_crash
) < ncpus
)
168 crash_shutdown_cpu
= -1;
169 __debugger
= old_handler
;
176 * This function will be called by secondary cpus.
178 void crash_kexec_secondary(struct pt_regs
*regs
)
181 int msecs
= SECONDARY_TIMEOUT
;
183 local_irq_save(flags
);
185 /* Wait for the primary crash CPU to signal its progress */
186 while (crashing_cpu
< 0) {
188 /* No response, kdump image may not have been loaded */
189 local_irq_restore(flags
);
196 crash_ipi_callback(regs
);
199 #else /* ! CONFIG_SMP */
201 static void crash_kexec_prepare_cpus(int cpu
)
204 * move the secondaries to us so that we can copy
205 * the new kernel 0-0x100 safely
207 * do this if kexec in setup.c ?
216 void crash_kexec_secondary(struct pt_regs
*regs
)
219 #endif /* CONFIG_SMP */
221 /* wait for all the CPUs to hit real mode but timeout if they don't come in */
222 #if defined(CONFIG_SMP) && defined(CONFIG_PPC64)
223 static void __maybe_unused
crash_kexec_wait_realmode(int cpu
)
228 msecs
= REAL_MODE_TIMEOUT
;
229 for (i
=0; i
< nr_cpu_ids
&& msecs
> 0; i
++) {
233 while (paca
[i
].kexec_state
< KEXEC_STATE_REAL_MODE
) {
235 if (!cpu_possible(i
) || !cpu_online(i
) || (msecs
<= 0))
244 static inline void crash_kexec_wait_realmode(int cpu
) {}
245 #endif /* CONFIG_SMP && CONFIG_PPC64 */
248 * Register a function to be called on shutdown. Only use this if you
249 * can't reset your device in the second kernel.
251 int crash_shutdown_register(crash_shutdown_t handler
)
255 spin_lock(&crash_handlers_lock
);
256 for (i
= 0 ; i
< CRASH_HANDLER_MAX
; i
++)
257 if (!crash_shutdown_handles
[i
]) {
258 /* Insert handle at first empty entry */
259 crash_shutdown_handles
[i
] = handler
;
264 if (i
== CRASH_HANDLER_MAX
) {
265 printk(KERN_ERR
"Crash shutdown handles full, "
266 "not registered.\n");
270 spin_unlock(&crash_handlers_lock
);
273 EXPORT_SYMBOL(crash_shutdown_register
);
275 int crash_shutdown_unregister(crash_shutdown_t handler
)
279 spin_lock(&crash_handlers_lock
);
280 for (i
= 0 ; i
< CRASH_HANDLER_MAX
; i
++)
281 if (crash_shutdown_handles
[i
] == handler
)
284 if (i
== CRASH_HANDLER_MAX
) {
285 printk(KERN_ERR
"Crash shutdown handle not found\n");
288 /* Shift handles down */
289 for (; i
< (CRASH_HANDLER_MAX
- 1); i
++)
290 crash_shutdown_handles
[i
] =
291 crash_shutdown_handles
[i
+1];
293 * Reset last entry to NULL now that it has been shifted down,
294 * this will allow new handles to be added here.
296 crash_shutdown_handles
[i
] = NULL
;
300 spin_unlock(&crash_handlers_lock
);
303 EXPORT_SYMBOL(crash_shutdown_unregister
);
305 void default_machine_crash_shutdown(struct pt_regs
*regs
)
308 int (*old_handler
)(struct pt_regs
*regs
);
311 * This function is only called after the system
312 * has panicked or is otherwise in a critical state.
313 * The minimum amount of code to allow a kexec'd kernel
314 * to run successfully needs to happen here.
316 * In practice this means stopping other cpus in
318 * The kernel is broken so disable interrupts.
323 * Make a note of crashing cpu. Will be used in machine_kexec
324 * such that another IPI will not be sent.
326 crashing_cpu
= smp_processor_id();
329 * If we came in via system reset, wait a while for the secondary
332 if (TRAP(regs
) == 0x100)
333 mdelay(PRIMARY_TIMEOUT
);
335 crash_kexec_prepare_cpus(crashing_cpu
);
337 crash_save_cpu(regs
, crashing_cpu
);
341 crash_kexec_wait_realmode(crashing_cpu
);
343 machine_kexec_mask_interrupts();
346 * Call registered shutdown routines safely. Swap out
347 * __debugger_fault_handler, and replace on exit.
349 old_handler
= __debugger_fault_handler
;
350 __debugger_fault_handler
= handle_fault
;
351 crash_shutdown_cpu
= smp_processor_id();
352 for (i
= 0; i
< CRASH_HANDLER_MAX
&& crash_shutdown_handles
[i
]; i
++) {
353 if (setjmp(crash_shutdown_buf
) == 0) {
355 * Insert syncs and delay to ensure
356 * instructions in the dangerous region don't
357 * leak away from this protected region.
359 asm volatile("sync; isync");
360 /* dangerous region */
361 crash_shutdown_handles
[i
]();
362 asm volatile("sync; isync");
365 crash_shutdown_cpu
= -1;
366 __debugger_fault_handler
= old_handler
;
368 if (ppc_md
.kexec_cpu_down
)
369 ppc_md
.kexec_cpu_down(1, 0);