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>
28 #include <asm/processor.h>
29 #include <asm/machdep.h>
30 #include <asm/kexec.h>
31 #include <asm/kdump.h>
33 #include <asm/firmware.h>
35 #include <asm/system.h>
36 #include <asm/setjmp.h>
40 #define DBG(fmt...) udbg_printf(fmt)
45 /* This keeps a track of which one is crashing cpu. */
46 int crashing_cpu
= -1;
47 static cpumask_t cpus_in_crash
= CPU_MASK_NONE
;
48 cpumask_t cpus_in_sr
= CPU_MASK_NONE
;
50 #define CRASH_HANDLER_MAX 1
51 /* NULL terminated list of shutdown handles */
52 static crash_shutdown_t crash_shutdown_handles
[CRASH_HANDLER_MAX
+1];
53 static DEFINE_SPINLOCK(crash_handlers_lock
);
56 static atomic_t enter_on_soft_reset
= ATOMIC_INIT(0);
58 void crash_ipi_callback(struct pt_regs
*regs
)
60 int cpu
= smp_processor_id();
66 if (!cpu_isset(cpu
, cpus_in_crash
))
67 crash_save_cpu(regs
, cpu
);
68 cpu_set(cpu
, cpus_in_crash
);
71 * Entered via soft-reset - could be the kdump
72 * process is invoked using soft-reset or user activated
73 * it if some CPU did not respond to an IPI.
74 * For soft-reset, the secondary CPU can enter this func
75 * twice. 1 - using IPI, and 2. soft-reset.
76 * Tell the kexec CPU that entered via soft-reset and ready
79 if (cpu_isset(cpu
, cpus_in_sr
)) {
80 cpu_clear(cpu
, cpus_in_sr
);
81 atomic_inc(&enter_on_soft_reset
);
85 * Starting the kdump boot.
86 * This barrier is needed to make sure that all CPUs are stopped.
87 * If not, soft-reset will be invoked to bring other CPUs.
89 while (!cpu_isset(crashing_cpu
, cpus_in_crash
))
92 if (ppc_md
.kexec_cpu_down
)
93 ppc_md
.kexec_cpu_down(1, 1);
105 * Wait until all CPUs are entered via soft-reset.
107 static void crash_soft_reset_check(int cpu
)
109 unsigned int ncpus
= num_online_cpus() - 1;/* Excluding the panic cpu */
111 cpu_clear(cpu
, cpus_in_sr
);
112 while (atomic_read(&enter_on_soft_reset
) != ncpus
)
117 static void crash_kexec_prepare_cpus(int cpu
)
121 unsigned int ncpus
= num_online_cpus() - 1;/* Excluding the panic cpu */
123 crash_send_ipi(crash_ipi_callback
);
127 * FIXME: Until we will have the way to stop other CPUSs reliabally,
128 * the crash CPU will send an IPI and wait for other CPUs to
130 * Delay of at least 10 seconds.
132 printk(KERN_EMERG
"Sending IPI to other cpus...\n");
134 while ((cpus_weight(cpus_in_crash
) < ncpus
) && (--msecs
> 0)) {
139 /* Would it be better to replace the trap vector here? */
142 * FIXME: In case if we do not get all CPUs, one possibility: ask the
143 * user to do soft reset such that we get all.
144 * Soft-reset will be used until better mechanism is implemented.
146 if (cpus_weight(cpus_in_crash
) < ncpus
) {
147 printk(KERN_EMERG
"done waiting: %d cpu(s) not responding\n",
148 ncpus
- cpus_weight(cpus_in_crash
));
149 printk(KERN_EMERG
"Activate soft-reset to stop other cpu(s)\n");
150 cpus_in_sr
= CPU_MASK_NONE
;
151 atomic_set(&enter_on_soft_reset
, 0);
152 while (cpus_weight(cpus_in_crash
) < ncpus
)
156 * Make sure all CPUs are entered via soft-reset if the kdump is
157 * invoked using soft-reset.
159 if (cpu_isset(cpu
, cpus_in_sr
))
160 crash_soft_reset_check(cpu
);
161 /* Leave the IPI callback set */
165 * This function will be called by secondary cpus or by kexec cpu
166 * if soft-reset is activated to stop some CPUs.
168 void crash_kexec_secondary(struct pt_regs
*regs
)
170 int cpu
= smp_processor_id();
174 local_irq_save(flags
);
175 /* Wait 5ms if the kexec CPU is not entered yet. */
176 while (crashing_cpu
< 0) {
179 * Either kdump image is not loaded or
180 * kdump process is not started - Probably xmon
181 * exited using 'x'(exit and recover) or
182 * kexec_should_crash() failed for all running tasks.
184 cpu_clear(cpu
, cpus_in_sr
);
185 local_irq_restore(flags
);
191 if (cpu
== crashing_cpu
) {
193 * Panic CPU will enter this func only via soft-reset.
194 * Wait until all secondary CPUs entered and
195 * then start kexec boot.
197 crash_soft_reset_check(cpu
);
198 cpu_set(crashing_cpu
, cpus_in_crash
);
199 if (ppc_md
.kexec_cpu_down
)
200 ppc_md
.kexec_cpu_down(1, 0);
201 machine_kexec(kexec_crash_image
);
204 crash_ipi_callback(regs
);
208 static void crash_kexec_prepare_cpus(int cpu
)
211 * move the secondarys to us so that we can copy
212 * the new kernel 0-0x100 safely
214 * do this if kexec in setup.c ?
223 void crash_kexec_secondary(struct pt_regs
*regs
)
225 cpus_in_sr
= CPU_MASK_NONE
;
228 #ifdef CONFIG_SPU_BASE
231 #include <asm/spu_priv1.h>
233 struct crash_spu_info
{
235 u32 saved_spu_runcntl_RW
;
236 u32 saved_spu_status_R
;
237 u32 saved_spu_npc_RW
;
238 u64 saved_mfc_sr1_RW
;
243 #define CRASH_NUM_SPUS 16 /* Enough for current hardware */
244 static struct crash_spu_info crash_spu_info
[CRASH_NUM_SPUS
];
246 static void crash_kexec_stop_spus(void)
252 for (i
= 0; i
< CRASH_NUM_SPUS
; i
++) {
253 if (!crash_spu_info
[i
].spu
)
256 spu
= crash_spu_info
[i
].spu
;
258 crash_spu_info
[i
].saved_spu_runcntl_RW
=
259 in_be32(&spu
->problem
->spu_runcntl_RW
);
260 crash_spu_info
[i
].saved_spu_status_R
=
261 in_be32(&spu
->problem
->spu_status_R
);
262 crash_spu_info
[i
].saved_spu_npc_RW
=
263 in_be32(&spu
->problem
->spu_npc_RW
);
265 crash_spu_info
[i
].saved_mfc_dar
= spu_mfc_dar_get(spu
);
266 crash_spu_info
[i
].saved_mfc_dsisr
= spu_mfc_dsisr_get(spu
);
267 tmp
= spu_mfc_sr1_get(spu
);
268 crash_spu_info
[i
].saved_mfc_sr1_RW
= tmp
;
270 tmp
&= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK
;
271 spu_mfc_sr1_set(spu
, tmp
);
277 void crash_register_spus(struct list_head
*list
)
281 list_for_each_entry(spu
, list
, full_list
) {
282 if (WARN_ON(spu
->number
>= CRASH_NUM_SPUS
))
285 crash_spu_info
[spu
->number
].spu
= spu
;
290 static inline void crash_kexec_stop_spus(void)
293 #endif /* CONFIG_SPU_BASE */
296 * Register a function to be called on shutdown. Only use this if you
297 * can't reset your device in the second kernel.
299 int crash_shutdown_register(crash_shutdown_t handler
)
303 spin_lock(&crash_handlers_lock
);
304 for (i
= 0 ; i
< CRASH_HANDLER_MAX
; i
++)
305 if (!crash_shutdown_handles
[i
]) {
306 /* Insert handle at first empty entry */
307 crash_shutdown_handles
[i
] = handler
;
312 if (i
== CRASH_HANDLER_MAX
) {
313 printk(KERN_ERR
"Crash shutdown handles full, "
314 "not registered.\n");
318 spin_unlock(&crash_handlers_lock
);
321 EXPORT_SYMBOL(crash_shutdown_register
);
323 int crash_shutdown_unregister(crash_shutdown_t handler
)
327 spin_lock(&crash_handlers_lock
);
328 for (i
= 0 ; i
< CRASH_HANDLER_MAX
; i
++)
329 if (crash_shutdown_handles
[i
] == handler
)
332 if (i
== CRASH_HANDLER_MAX
) {
333 printk(KERN_ERR
"Crash shutdown handle not found\n");
336 /* Shift handles down */
337 for (; crash_shutdown_handles
[i
]; i
++)
338 crash_shutdown_handles
[i
] =
339 crash_shutdown_handles
[i
+1];
343 spin_unlock(&crash_handlers_lock
);
346 EXPORT_SYMBOL(crash_shutdown_unregister
);
348 static unsigned long crash_shutdown_buf
[JMP_BUF_LEN
];
350 static int handle_fault(struct pt_regs
*regs
)
352 longjmp(crash_shutdown_buf
, 1);
356 void default_machine_crash_shutdown(struct pt_regs
*regs
)
359 int (*old_handler
)(struct pt_regs
*regs
);
363 * This function is only called after the system
364 * has panicked or is otherwise in a critical state.
365 * The minimum amount of code to allow a kexec'd kernel
366 * to run successfully needs to happen here.
368 * In practice this means stopping other cpus in
370 * The kernel is broken so disable interrupts.
375 struct irq_desc
*desc
= irq_desc
+ i
;
377 if (desc
->status
& IRQ_INPROGRESS
)
380 if (!(desc
->status
& IRQ_DISABLED
))
381 desc
->chip
->disable(i
);
385 * Call registered shutdown routines savely. Swap out
386 * __debugger_fault_handler, and replace on exit.
388 old_handler
= __debugger_fault_handler
;
389 __debugger_fault_handler
= handle_fault
;
390 for (i
= 0; crash_shutdown_handles
[i
]; i
++) {
391 if (setjmp(crash_shutdown_buf
) == 0) {
393 * Insert syncs and delay to ensure
394 * instructions in the dangerous region don't
395 * leak away from this protected region.
397 asm volatile("sync; isync");
398 /* dangerous region */
399 crash_shutdown_handles
[i
]();
400 asm volatile("sync; isync");
403 __debugger_fault_handler
= old_handler
;
406 * Make a note of crashing cpu. Will be used in machine_kexec
407 * such that another IPI will not be sent.
409 crashing_cpu
= smp_processor_id();
410 crash_save_cpu(regs
, crashing_cpu
);
411 crash_kexec_prepare_cpus(crashing_cpu
);
412 cpu_set(crashing_cpu
, cpus_in_crash
);
413 crash_kexec_stop_spus();
414 if (ppc_md
.kexec_cpu_down
)
415 ppc_md
.kexec_cpu_down(1, 0);