Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / powerpc / kernel / crash.c
blob571132ed12c1e4f74961eef16815533fa78c8351
1 /*
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 #undef DEBUG
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>
32 #include <asm/lmb.h>
33 #include <asm/firmware.h>
34 #include <asm/smp.h>
35 #include <asm/system.h>
36 #include <asm/setjmp.h>
38 #ifdef DEBUG
39 #include <asm/udbg.h>
40 #define DBG(fmt...) udbg_printf(fmt)
41 #else
42 #define DBG(fmt...)
43 #endif
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);
55 #ifdef CONFIG_SMP
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();
62 if (!cpu_online(cpu))
63 return;
65 hard_irq_disable();
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
77 * to go down.
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))
90 cpu_relax();
92 if (ppc_md.kexec_cpu_down)
93 ppc_md.kexec_cpu_down(1, 1);
95 #ifdef CONFIG_PPC64
96 kexec_smp_wait();
97 #else
98 for (;;); /* FIXME */
99 #endif
101 /* NOTREACHED */
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)
113 cpu_relax();
117 static void crash_kexec_prepare_cpus(int cpu)
119 unsigned int msecs;
121 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
123 crash_send_ipi(crash_ipi_callback);
124 smp_wmb();
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
129 * respond.
130 * Delay of at least 10 seconds.
132 printk(KERN_EMERG "Sending IPI to other cpus...\n");
133 msecs = 10000;
134 while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) {
135 cpu_relax();
136 mdelay(1);
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)
153 cpu_relax();
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();
171 unsigned long flags;
172 int msecs = 5;
174 local_irq_save(flags);
175 /* Wait 5ms if the kexec CPU is not entered yet. */
176 while (crashing_cpu < 0) {
177 if (--msecs < 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);
186 return;
188 mdelay(1);
189 cpu_relax();
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);
202 /* NOTREACHED */
204 crash_ipi_callback(regs);
207 #else
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 ?
216 #ifdef CONFIG_PPC64
217 smp_release_cpus();
218 #else
219 /* FIXME */
220 #endif
223 void crash_kexec_secondary(struct pt_regs *regs)
225 cpus_in_sr = CPU_MASK_NONE;
227 #endif
228 #ifdef CONFIG_SPU_BASE
230 #include <asm/spu.h>
231 #include <asm/spu_priv1.h>
233 struct crash_spu_info {
234 struct spu *spu;
235 u32 saved_spu_runcntl_RW;
236 u32 saved_spu_status_R;
237 u32 saved_spu_npc_RW;
238 u64 saved_mfc_sr1_RW;
239 u64 saved_mfc_dar;
240 u64 saved_mfc_dsisr;
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)
248 struct spu *spu;
249 int i;
250 u64 tmp;
252 for (i = 0; i < CRASH_NUM_SPUS; i++) {
253 if (!crash_spu_info[i].spu)
254 continue;
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);
273 __delay(200);
277 void crash_register_spus(struct list_head *list)
279 struct spu *spu;
281 list_for_each_entry(spu, list, full_list) {
282 if (WARN_ON(spu->number >= CRASH_NUM_SPUS))
283 continue;
285 crash_spu_info[spu->number].spu = spu;
289 #else
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)
301 unsigned int i, rc;
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;
308 rc = 0;
309 break;
312 if (i == CRASH_HANDLER_MAX) {
313 printk(KERN_ERR "Crash shutdown handles full, "
314 "not registered.\n");
315 rc = 1;
318 spin_unlock(&crash_handlers_lock);
319 return rc;
321 EXPORT_SYMBOL(crash_shutdown_register);
323 int crash_shutdown_unregister(crash_shutdown_t handler)
325 unsigned int i, rc;
327 spin_lock(&crash_handlers_lock);
328 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
329 if (crash_shutdown_handles[i] == handler)
330 break;
332 if (i == CRASH_HANDLER_MAX) {
333 printk(KERN_ERR "Crash shutdown handle not found\n");
334 rc = 1;
335 } else {
336 /* Shift handles down */
337 for (; crash_shutdown_handles[i]; i++)
338 crash_shutdown_handles[i] =
339 crash_shutdown_handles[i+1];
340 rc = 0;
343 spin_unlock(&crash_handlers_lock);
344 return rc;
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);
353 return 0;
356 void default_machine_crash_shutdown(struct pt_regs *regs)
358 unsigned int i;
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
369 * an SMP system.
370 * The kernel is broken so disable interrupts.
372 hard_irq_disable();
374 for_each_irq(i) {
375 struct irq_desc *desc = irq_desc + i;
377 if (desc->status & IRQ_INPROGRESS)
378 desc->chip->eoi(i);
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);