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/crash_dump.h>
21 #include <linux/delay.h>
22 #include <linux/elf.h>
23 #include <linux/elfcore.h>
24 #include <linux/init.h>
25 #include <linux/irq.h>
26 #include <linux/types.h>
27 #include <linux/lmb.h>
29 #include <asm/processor.h>
30 #include <asm/machdep.h>
31 #include <asm/kexec.h>
32 #include <asm/kdump.h>
34 #include <asm/firmware.h>
36 #include <asm/system.h>
37 #include <asm/setjmp.h>
41 #define DBG(fmt...) udbg_printf(fmt)
46 /* This keeps a track of which one is crashing cpu. */
47 int crashing_cpu = -1;
48 static cpumask_t cpus_in_crash = CPU_MASK_NONE;
49 cpumask_t cpus_in_sr = CPU_MASK_NONE;
51 #define CRASH_HANDLER_MAX 1
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);
57 static atomic_t enter_on_soft_reset = ATOMIC_INIT(0);
59 void crash_ipi_callback(struct pt_regs *regs)
61 int cpu = smp_processor_id();
67 if (!cpu_isset(cpu, cpus_in_crash))
68 crash_save_cpu(regs, cpu);
69 cpu_set(cpu, cpus_in_crash);
72 * Entered via soft-reset - could be the kdump
73 * process is invoked using soft-reset or user activated
74 * it if some CPU did not respond to an IPI.
75 * For soft-reset, the secondary CPU can enter this func
76 * twice. 1 - using IPI, and 2. soft-reset.
77 * Tell the kexec CPU that entered via soft-reset and ready
80 if (cpu_isset(cpu, cpus_in_sr)) {
81 cpu_clear(cpu, cpus_in_sr);
82 atomic_inc(&enter_on_soft_reset);
86 * Starting the kdump boot.
87 * This barrier is needed to make sure that all CPUs are stopped.
88 * If not, soft-reset will be invoked to bring other CPUs.
90 while (!cpu_isset(crashing_cpu, cpus_in_crash))
93 if (ppc_md.kexec_cpu_down)
94 ppc_md.kexec_cpu_down(1, 1);
106 * Wait until all CPUs are entered via soft-reset.
108 static void crash_soft_reset_check(int cpu)
110 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
112 cpu_clear(cpu, cpus_in_sr);
113 while (atomic_read(&enter_on_soft_reset) != ncpus)
118 static void crash_kexec_prepare_cpus(int cpu)
122 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
124 crash_send_ipi(crash_ipi_callback);
128 * FIXME: Until we will have the way to stop other CPUSs reliabally,
129 * the crash CPU will send an IPI and wait for other CPUs to
131 * Delay of at least 10 seconds.
133 printk(KERN_EMERG "Sending IPI to other cpus...\n");
135 while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) {
140 /* Would it be better to replace the trap vector here? */
143 * FIXME: In case if we do not get all CPUs, one possibility: ask the
144 * user to do soft reset such that we get all.
145 * Soft-reset will be used until better mechanism is implemented.
147 if (cpus_weight(cpus_in_crash) < ncpus) {
148 printk(KERN_EMERG "done waiting: %d cpu(s) not responding\n",
149 ncpus - cpus_weight(cpus_in_crash));
150 printk(KERN_EMERG "Activate soft-reset to stop other cpu(s)\n");
151 cpus_in_sr = CPU_MASK_NONE;
152 atomic_set(&enter_on_soft_reset, 0);
153 while (cpus_weight(cpus_in_crash) < ncpus)
157 * Make sure all CPUs are entered via soft-reset if the kdump is
158 * invoked using soft-reset.
160 if (cpu_isset(cpu, cpus_in_sr))
161 crash_soft_reset_check(cpu);
162 /* Leave the IPI callback set */
166 * This function will be called by secondary cpus or by kexec cpu
167 * if soft-reset is activated to stop some CPUs.
169 void crash_kexec_secondary(struct pt_regs *regs)
171 int cpu = smp_processor_id();
175 local_irq_save(flags);
176 /* Wait 5ms if the kexec CPU is not entered yet. */
177 while (crashing_cpu < 0) {
180 * Either kdump image is not loaded or
181 * kdump process is not started - Probably xmon
182 * exited using 'x'(exit and recover) or
183 * kexec_should_crash() failed for all running tasks.
185 cpu_clear(cpu, cpus_in_sr);
186 local_irq_restore(flags);
192 if (cpu == crashing_cpu) {
194 * Panic CPU will enter this func only via soft-reset.
195 * Wait until all secondary CPUs entered and
196 * then start kexec boot.
198 crash_soft_reset_check(cpu);
199 cpu_set(crashing_cpu, cpus_in_crash);
200 if (ppc_md.kexec_cpu_down)
201 ppc_md.kexec_cpu_down(1, 0);
202 machine_kexec(kexec_crash_image);
205 crash_ipi_callback(regs);
209 static void crash_kexec_prepare_cpus(int cpu)
212 * move the secondarys to us so that we can copy
213 * the new kernel 0-0x100 safely
215 * do this if kexec in setup.c ?
224 void crash_kexec_secondary(struct pt_regs *regs)
226 cpus_in_sr = CPU_MASK_NONE;
229 #ifdef CONFIG_SPU_BASE
232 #include <asm/spu_priv1.h>
234 struct crash_spu_info {
236 u32 saved_spu_runcntl_RW;
237 u32 saved_spu_status_R;
238 u32 saved_spu_npc_RW;
239 u64 saved_mfc_sr1_RW;
244 #define CRASH_NUM_SPUS 16 /* Enough for current hardware */
245 static struct crash_spu_info crash_spu_info[CRASH_NUM_SPUS];
247 static void crash_kexec_stop_spus(void)
253 for (i = 0; i < CRASH_NUM_SPUS; i++) {
254 if (!crash_spu_info[i].spu)
257 spu = crash_spu_info[i].spu;
259 crash_spu_info[i].saved_spu_runcntl_RW =
260 in_be32(&spu->problem->spu_runcntl_RW);
261 crash_spu_info[i].saved_spu_status_R =
262 in_be32(&spu->problem->spu_status_R);
263 crash_spu_info[i].saved_spu_npc_RW =
264 in_be32(&spu->problem->spu_npc_RW);
266 crash_spu_info[i].saved_mfc_dar = spu_mfc_dar_get(spu);
267 crash_spu_info[i].saved_mfc_dsisr = spu_mfc_dsisr_get(spu);
268 tmp = spu_mfc_sr1_get(spu);
269 crash_spu_info[i].saved_mfc_sr1_RW = tmp;
271 tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
272 spu_mfc_sr1_set(spu, tmp);
278 void crash_register_spus(struct list_head *list)
282 list_for_each_entry(spu, list, full_list) {
283 if (WARN_ON(spu->number >= CRASH_NUM_SPUS))
286 crash_spu_info[spu->number].spu = spu;
291 static inline void crash_kexec_stop_spus(void)
294 #endif /* CONFIG_SPU_BASE */
297 * Register a function to be called on shutdown. Only use this if you
298 * can't reset your device in the second kernel.
300 int crash_shutdown_register(crash_shutdown_t handler)
304 spin_lock(&crash_handlers_lock);
305 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
306 if (!crash_shutdown_handles[i]) {
307 /* Insert handle at first empty entry */
308 crash_shutdown_handles[i] = handler;
313 if (i == CRASH_HANDLER_MAX) {
314 printk(KERN_ERR "Crash shutdown handles full, "
315 "not registered.\n");
319 spin_unlock(&crash_handlers_lock);
322 EXPORT_SYMBOL(crash_shutdown_register);
324 int crash_shutdown_unregister(crash_shutdown_t handler)
328 spin_lock(&crash_handlers_lock);
329 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
330 if (crash_shutdown_handles[i] == handler)
333 if (i == CRASH_HANDLER_MAX) {
334 printk(KERN_ERR "Crash shutdown handle not found\n");
337 /* Shift handles down */
338 for (; crash_shutdown_handles[i]; i++)
339 crash_shutdown_handles[i] =
340 crash_shutdown_handles[i+1];
344 spin_unlock(&crash_handlers_lock);
347 EXPORT_SYMBOL(crash_shutdown_unregister);
349 static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
351 static int handle_fault(struct pt_regs *regs)
353 longjmp(crash_shutdown_buf, 1);
357 void default_machine_crash_shutdown(struct pt_regs *regs)
360 int (*old_handler)(struct pt_regs *regs);
364 * This function is only called after the system
365 * has panicked or is otherwise in a critical state.
366 * The minimum amount of code to allow a kexec'd kernel
367 * to run successfully needs to happen here.
369 * In practice this means stopping other cpus in
371 * The kernel is broken so disable interrupts.
376 struct irq_desc *desc = irq_desc + i;
378 if (desc->status & IRQ_INPROGRESS)
381 if (!(desc->status & IRQ_DISABLED))
382 desc->chip->disable(i);
386 * Call registered shutdown routines savely. Swap out
387 * __debugger_fault_handler, and replace on exit.
389 old_handler = __debugger_fault_handler;
390 __debugger_fault_handler = handle_fault;
391 for (i = 0; crash_shutdown_handles[i]; i++) {
392 if (setjmp(crash_shutdown_buf) == 0) {
394 * Insert syncs and delay to ensure
395 * instructions in the dangerous region don't
396 * leak away from this protected region.
398 asm volatile("sync; isync");
399 /* dangerous region */
400 crash_shutdown_handles[i]();
401 asm volatile("sync; isync");
404 __debugger_fault_handler = old_handler;
407 * Make a note of crashing cpu. Will be used in machine_kexec
408 * such that another IPI will not be sent.
410 crashing_cpu = smp_processor_id();
411 crash_save_cpu(regs, crashing_cpu);
412 crash_kexec_prepare_cpus(crashing_cpu);
413 cpu_set(crashing_cpu, cpus_in_crash);
414 crash_kexec_stop_spus();
415 if (ppc_md.kexec_cpu_down)
416 ppc_md.kexec_cpu_down(1, 0);