Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[deliverable/linux.git] / arch / powerpc / kernel / crash.c
CommitLineData
cc532915
ME
1/*
2 * Architecture specific (PPC64) functions for kexec based crash dumps.
3 *
4 * Copyright (C) 2005, IBM Corp.
5 *
6 * Created by: Haren Myneni
7 *
8 * This source code is licensed under the GNU General Public License,
9 * Version 2. See the file COPYING for more details.
10 *
11 */
12
13#undef DEBUG
14
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>
cc532915
ME
21#include <linux/delay.h>
22#include <linux/elf.h>
23#include <linux/elfcore.h>
24#include <linux/init.h>
d6c1a908 25#include <linux/irq.h>
cc532915
ME
26#include <linux/types.h>
27
28#include <asm/processor.h>
29#include <asm/machdep.h>
c0ce7d08 30#include <asm/kexec.h>
cc532915
ME
31#include <asm/kdump.h>
32#include <asm/lmb.h>
33#include <asm/firmware.h>
f6cc82fc 34#include <asm/smp.h>
496b010e
MN
35#include <asm/system.h>
36#include <asm/setjmp.h>
cc532915
ME
37
38#ifdef DEBUG
39#include <asm/udbg.h>
40#define DBG(fmt...) udbg_printf(fmt)
41#else
42#define DBG(fmt...)
43#endif
44
45/* This keeps a track of which one is crashing cpu. */
46int crashing_cpu = -1;
c0ce7d08 47static cpumask_t cpus_in_crash = CPU_MASK_NONE;
b6f35b49 48cpumask_t cpus_in_sr = CPU_MASK_NONE;
cc532915 49
496b010e
MN
50#define CRASH_HANDLER_MAX 1
51/* NULL terminated list of shutdown handles */
52static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX+1];
53static DEFINE_SPINLOCK(crash_handlers_lock);
54
cc532915 55#ifdef CONFIG_SMP
c0ce7d08 56static atomic_t enter_on_soft_reset = ATOMIC_INIT(0);
cc532915
ME
57
58void crash_ipi_callback(struct pt_regs *regs)
59{
60 int cpu = smp_processor_id();
61
cc532915
ME
62 if (!cpu_online(cpu))
63 return;
64
d04c56f7 65 hard_irq_disable();
c0ce7d08 66 if (!cpu_isset(cpu, cpus_in_crash))
85916f81 67 crash_save_cpu(regs, cpu);
c0ce7d08 68 cpu_set(cpu, cpus_in_crash);
cc532915 69
c0ce7d08
DW
70 /*
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.
78 */
79 if (cpu_isset(cpu, cpus_in_sr)) {
80 cpu_clear(cpu, cpus_in_sr);
81 atomic_inc(&enter_on_soft_reset);
82 }
83
84 /*
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.
88 */
89 while (!cpu_isset(crashing_cpu, cpus_in_crash))
90 cpu_relax();
91
92 if (ppc_md.kexec_cpu_down)
93 ppc_md.kexec_cpu_down(1, 1);
b6f35b49
ME
94
95#ifdef CONFIG_PPC64
cc532915 96 kexec_smp_wait();
b6f35b49
ME
97#else
98 for (;;); /* FIXME */
99#endif
100
cc532915
ME
101 /* NOTREACHED */
102}
103
c0ce7d08
DW
104/*
105 * Wait until all CPUs are entered via soft-reset.
106 */
107static void crash_soft_reset_check(int cpu)
108{
109 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
110
111 cpu_clear(cpu, cpus_in_sr);
112 while (atomic_read(&enter_on_soft_reset) != ncpus)
113 cpu_relax();
114}
115
116
117static void crash_kexec_prepare_cpus(int cpu)
cc532915
ME
118{
119 unsigned int msecs;
120
c0ce7d08 121 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
cc532915
ME
122
123 crash_send_ipi(crash_ipi_callback);
124 smp_wmb();
125
126 /*
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
c0ce7d08 129 * respond.
01aaed9d 130 * Delay of at least 10 seconds.
cc532915 131 */
c0ce7d08 132 printk(KERN_EMERG "Sending IPI to other cpus...\n");
01aaed9d 133 msecs = 10000;
c0ce7d08
DW
134 while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) {
135 cpu_relax();
cc532915
ME
136 mdelay(1);
137 }
138
139 /* Would it be better to replace the trap vector here? */
140
141 /*
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.
c0ce7d08
DW
144 * Soft-reset will be used until better mechanism is implemented.
145 */
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();
154 }
155 /*
156 * Make sure all CPUs are entered via soft-reset if the kdump is
157 * invoked using soft-reset.
cc532915 158 */
c0ce7d08
DW
159 if (cpu_isset(cpu, cpus_in_sr))
160 crash_soft_reset_check(cpu);
cc532915
ME
161 /* Leave the IPI callback set */
162}
c0ce7d08
DW
163
164/*
165 * This function will be called by secondary cpus or by kexec cpu
166 * if soft-reset is activated to stop some CPUs.
167 */
168void crash_kexec_secondary(struct pt_regs *regs)
169{
170 int cpu = smp_processor_id();
171 unsigned long flags;
172 int msecs = 5;
173
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) {
178 /*
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.
183 */
184 cpu_clear(cpu, cpus_in_sr);
185 local_irq_restore(flags);
186 return;
187 }
188 mdelay(1);
189 cpu_relax();
190 }
191 if (cpu == crashing_cpu) {
192 /*
193 * Panic CPU will enter this func only via soft-reset.
194 * Wait until all secondary CPUs entered and
195 * then start kexec boot.
196 */
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 */
203 }
204 crash_ipi_callback(regs);
205}
206
cc532915 207#else
c0ce7d08 208static void crash_kexec_prepare_cpus(int cpu)
cc532915
ME
209{
210 /*
211 * move the secondarys to us so that we can copy
212 * the new kernel 0-0x100 safely
213 *
214 * do this if kexec in setup.c ?
215 */
b6f35b49 216#ifdef CONFIG_PPC64
cc532915 217 smp_release_cpus();
b6f35b49
ME
218#else
219 /* FIXME */
220#endif
cc532915
ME
221}
222
c0ce7d08
DW
223void crash_kexec_secondary(struct pt_regs *regs)
224{
225 cpus_in_sr = CPU_MASK_NONE;
226}
cc532915 227#endif
8d2655e6
AD
228#ifdef CONFIG_SPU_BASE
229
230#include <asm/spu.h>
231#include <asm/spu_priv1.h>
232
233struct 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;
241};
242
243#define CRASH_NUM_SPUS 16 /* Enough for current hardware */
244static struct crash_spu_info crash_spu_info[CRASH_NUM_SPUS];
245
246static void crash_kexec_stop_spus(void)
247{
248 struct spu *spu;
249 int i;
250 u64 tmp;
251
252 for (i = 0; i < CRASH_NUM_SPUS; i++) {
253 if (!crash_spu_info[i].spu)
254 continue;
255
256 spu = crash_spu_info[i].spu;
257
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);
264
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;
269
270 tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
271 spu_mfc_sr1_set(spu, tmp);
272
273 __delay(200);
274 }
275}
276
277void crash_register_spus(struct list_head *list)
278{
279 struct spu *spu;
280
281 list_for_each_entry(spu, list, full_list) {
282 if (WARN_ON(spu->number >= CRASH_NUM_SPUS))
283 continue;
284
285 crash_spu_info[spu->number].spu = spu;
286 }
287}
288
289#else
290static inline void crash_kexec_stop_spus(void)
291{
292}
293#endif /* CONFIG_SPU_BASE */
cc532915 294
496b010e
MN
295/*
296 * Register a function to be called on shutdown. Only use this if you
297 * can't reset your device in the second kernel.
298 */
299int crash_shutdown_register(crash_shutdown_t handler)
300{
301 unsigned int i, rc;
302
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;
310 }
311
312 if (i == CRASH_HANDLER_MAX) {
313 printk(KERN_ERR "Crash shutdown handles full, "
314 "not registered.\n");
315 rc = 1;
316 }
317
318 spin_unlock(&crash_handlers_lock);
319 return rc;
320}
321EXPORT_SYMBOL(crash_shutdown_register);
322
323int crash_shutdown_unregister(crash_shutdown_t handler)
324{
325 unsigned int i, rc;
326
327 spin_lock(&crash_handlers_lock);
328 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
329 if (crash_shutdown_handles[i] == handler)
330 break;
331
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;
341 }
342
343 spin_unlock(&crash_handlers_lock);
344 return rc;
345}
346EXPORT_SYMBOL(crash_shutdown_unregister);
347
348static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
349
350static int handle_fault(struct pt_regs *regs)
351{
352 longjmp(crash_shutdown_buf, 1);
353 return 0;
354}
355
cc532915
ME
356void default_machine_crash_shutdown(struct pt_regs *regs)
357{
496b010e
MN
358 unsigned int i;
359 int (*old_handler)(struct pt_regs *regs);
360
d6c1a908 361
cc532915
ME
362 /*
363 * This function is only called after the system
f18190bd 364 * has panicked or is otherwise in a critical state.
cc532915
ME
365 * The minimum amount of code to allow a kexec'd kernel
366 * to run successfully needs to happen here.
367 *
368 * In practice this means stopping other cpus in
369 * an SMP system.
370 * The kernel is broken so disable interrupts.
371 */
d04c56f7 372 hard_irq_disable();
cc532915 373
496b010e
MN
374 for_each_irq(i) {
375 struct irq_desc *desc = irq_desc + i;
d6c1a908
ME
376
377 if (desc->status & IRQ_INPROGRESS)
496b010e 378 desc->chip->eoi(i);
d6c1a908
ME
379
380 if (!(desc->status & IRQ_DISABLED))
496b010e
MN
381 desc->chip->disable(i);
382 }
383
384 /*
385 * Call registered shutdown routines savely. Swap out
386 * __debugger_fault_handler, and replace on exit.
387 */
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) {
392 /*
393 * Insert syncs and delay to ensure
394 * instructions in the dangerous region don't
395 * leak away from this protected region.
396 */
397 asm volatile("sync; isync");
398 /* dangerous region */
399 crash_shutdown_handles[i]();
400 asm volatile("sync; isync");
401 }
d6c1a908 402 }
496b010e 403 __debugger_fault_handler = old_handler;
d6c1a908 404
cc532915
ME
405 /*
406 * Make a note of crashing cpu. Will be used in machine_kexec
407 * such that another IPI will not be sent.
408 */
409 crashing_cpu = smp_processor_id();
85916f81 410 crash_save_cpu(regs, crashing_cpu);
c0ce7d08
DW
411 crash_kexec_prepare_cpus(crashing_cpu);
412 cpu_set(crashing_cpu, cpus_in_crash);
8d2655e6 413 crash_kexec_stop_spus();
c0ce7d08
DW
414 if (ppc_md.kexec_cpu_down)
415 ppc_md.kexec_cpu_down(1, 0);
cc532915 416}
This page took 0.249934 seconds and 5 git commands to generate.