blob: 4457382f8667a7e770f51c9cb82ee09f9531fb1b [file] [log] [blame]
Michael Ellermancc532912005-12-04 18:39:43 +11001/*
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>
Michael Ellermancc532912005-12-04 18:39:43 +110021#include <linux/delay.h>
22#include <linux/elf.h>
23#include <linux/elfcore.h>
24#include <linux/init.h>
Michael Ellermand6c1a902006-04-04 13:43:01 +020025#include <linux/irq.h>
Michael Ellermancc532912005-12-04 18:39:43 +110026#include <linux/types.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100027#include <linux/memblock.h>
Michael Ellermancc532912005-12-04 18:39:43 +110028
29#include <asm/processor.h>
30#include <asm/machdep.h>
David Wilderc0ce7d02006-06-23 15:29:34 -070031#include <asm/kexec.h>
Michael Ellermancc532912005-12-04 18:39:43 +110032#include <asm/kdump.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080033#include <asm/prom.h>
Michael Ellermancc532912005-12-04 18:39:43 +110034#include <asm/firmware.h>
Haren Mynenif6cc82f2006-01-10 19:25:25 -080035#include <asm/smp.h>
Michael Neuling496b0102008-01-18 15:50:30 +110036#include <asm/system.h>
37#include <asm/setjmp.h>
Michael Ellermancc532912005-12-04 18:39:43 +110038
39#ifdef DEBUG
40#include <asm/udbg.h>
41#define DBG(fmt...) udbg_printf(fmt)
42#else
43#define DBG(fmt...)
44#endif
45
46/* This keeps a track of which one is crashing cpu. */
47int crashing_cpu = -1;
David Wilderc0ce7d02006-06-23 15:29:34 -070048static cpumask_t cpus_in_crash = CPU_MASK_NONE;
Michael Ellermanb6f35b42006-07-05 14:39:43 +100049cpumask_t cpus_in_sr = CPU_MASK_NONE;
Michael Ellermancc532912005-12-04 18:39:43 +110050
Arnd Bergmann36c35be2008-06-12 19:34:39 +100051#define CRASH_HANDLER_MAX 2
Michael Neuling496b0102008-01-18 15:50:30 +110052/* NULL terminated list of shutdown handles */
53static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX+1];
54static DEFINE_SPINLOCK(crash_handlers_lock);
55
Michael Ellermancc532912005-12-04 18:39:43 +110056#ifdef CONFIG_SMP
David Wilderc0ce7d02006-06-23 15:29:34 -070057static atomic_t enter_on_soft_reset = ATOMIC_INIT(0);
Michael Ellermancc532912005-12-04 18:39:43 +110058
59void crash_ipi_callback(struct pt_regs *regs)
60{
61 int cpu = smp_processor_id();
62
Michael Ellermancc532912005-12-04 18:39:43 +110063 if (!cpu_online(cpu))
64 return;
65
Paul Mackerrasd04c56f2006-10-04 16:47:49 +100066 hard_irq_disable();
David Wilderc0ce7d02006-06-23 15:29:34 -070067 if (!cpu_isset(cpu, cpus_in_crash))
Magnus Damm85916f82006-12-06 20:40:41 -080068 crash_save_cpu(regs, cpu);
David Wilderc0ce7d02006-06-23 15:29:34 -070069 cpu_set(cpu, cpus_in_crash);
70
71 /*
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
78 * to go down.
79 */
80 if (cpu_isset(cpu, cpus_in_sr)) {
81 cpu_clear(cpu, cpus_in_sr);
82 atomic_inc(&enter_on_soft_reset);
83 }
84
85 /*
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.
89 */
90 while (!cpu_isset(crashing_cpu, cpus_in_crash))
91 cpu_relax();
92
Michael Ellermancc532912005-12-04 18:39:43 +110093 if (ppc_md.kexec_cpu_down)
94 ppc_md.kexec_cpu_down(1, 1);
Michael Ellermanb6f35b42006-07-05 14:39:43 +100095
96#ifdef CONFIG_PPC64
Michael Ellermancc532912005-12-04 18:39:43 +110097 kexec_smp_wait();
Michael Ellermanb6f35b42006-07-05 14:39:43 +100098#else
99 for (;;); /* FIXME */
100#endif
101
Michael Ellermancc532912005-12-04 18:39:43 +1100102 /* NOTREACHED */
103}
104
David Wilderc0ce7d02006-06-23 15:29:34 -0700105/*
106 * Wait until all CPUs are entered via soft-reset.
107 */
108static void crash_soft_reset_check(int cpu)
109{
110 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
111
112 cpu_clear(cpu, cpus_in_sr);
113 while (atomic_read(&enter_on_soft_reset) != ncpus)
114 cpu_relax();
115}
116
117
118static void crash_kexec_prepare_cpus(int cpu)
Michael Ellermancc532912005-12-04 18:39:43 +1100119{
120 unsigned int msecs;
121
David Wilderc0ce7d02006-06-23 15:29:34 -0700122 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
Michael Ellermancc532912005-12-04 18:39:43 +1100123
124 crash_send_ipi(crash_ipi_callback);
125 smp_wmb();
126
127 /*
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
David Wilderc0ce7d02006-06-23 15:29:34 -0700130 * respond.
Haren Myneni01aaed92006-02-07 15:47:03 -0800131 * Delay of at least 10 seconds.
Michael Ellermancc532912005-12-04 18:39:43 +1100132 */
David Wilderc0ce7d02006-06-23 15:29:34 -0700133 printk(KERN_EMERG "Sending IPI to other cpus...\n");
Haren Myneni01aaed92006-02-07 15:47:03 -0800134 msecs = 10000;
David Wilderc0ce7d02006-06-23 15:29:34 -0700135 while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) {
136 cpu_relax();
Michael Ellermancc532912005-12-04 18:39:43 +1100137 mdelay(1);
138 }
139
140 /* Would it be better to replace the trap vector here? */
141
142 /*
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.
David Wilderc0ce7d02006-06-23 15:29:34 -0700145 * Soft-reset will be used until better mechanism is implemented.
Michael Ellermancc532912005-12-04 18:39:43 +1100146 */
David Wilderc0ce7d02006-06-23 15:29:34 -0700147 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)
154 cpu_relax();
155 }
156 /*
157 * Make sure all CPUs are entered via soft-reset if the kdump is
158 * invoked using soft-reset.
159 */
160 if (cpu_isset(cpu, cpus_in_sr))
161 crash_soft_reset_check(cpu);
Michael Ellermancc532912005-12-04 18:39:43 +1100162 /* Leave the IPI callback set */
163}
David Wilderc0ce7d02006-06-23 15:29:34 -0700164
Michael Neuling60adec62010-05-13 19:40:11 +0000165/* wait for all the CPUs to hit real mode but timeout if they don't come in */
Sebastian Andrzej Siewiorb3df8952010-04-04 22:19:03 +0200166#ifdef CONFIG_PPC_STD_MMU_64
Michael Neuling60adec62010-05-13 19:40:11 +0000167static void crash_kexec_wait_realmode(int cpu)
168{
169 unsigned int msecs;
170 int i;
171
172 msecs = 10000;
173 for (i=0; i < NR_CPUS && msecs > 0; i++) {
174 if (i == cpu)
175 continue;
176
177 while (paca[i].kexec_state < KEXEC_STATE_REAL_MODE) {
178 barrier();
179 if (!cpu_possible(i)) {
180 break;
181 }
182 if (!cpu_online(i)) {
183 break;
184 }
185 msecs--;
186 mdelay(1);
187 }
188 }
189 mb();
190}
Sebastian Andrzej Siewiorb3df8952010-04-04 22:19:03 +0200191#endif
Michael Neuling60adec62010-05-13 19:40:11 +0000192
David Wilderc0ce7d02006-06-23 15:29:34 -0700193/*
194 * This function will be called by secondary cpus or by kexec cpu
195 * if soft-reset is activated to stop some CPUs.
196 */
197void crash_kexec_secondary(struct pt_regs *regs)
198{
199 int cpu = smp_processor_id();
200 unsigned long flags;
201 int msecs = 5;
202
203 local_irq_save(flags);
204 /* Wait 5ms if the kexec CPU is not entered yet. */
205 while (crashing_cpu < 0) {
206 if (--msecs < 0) {
207 /*
208 * Either kdump image is not loaded or
209 * kdump process is not started - Probably xmon
210 * exited using 'x'(exit and recover) or
211 * kexec_should_crash() failed for all running tasks.
212 */
213 cpu_clear(cpu, cpus_in_sr);
214 local_irq_restore(flags);
215 return;
216 }
217 mdelay(1);
218 cpu_relax();
219 }
220 if (cpu == crashing_cpu) {
221 /*
222 * Panic CPU will enter this func only via soft-reset.
223 * Wait until all secondary CPUs entered and
224 * then start kexec boot.
225 */
226 crash_soft_reset_check(cpu);
227 cpu_set(crashing_cpu, cpus_in_crash);
228 if (ppc_md.kexec_cpu_down)
229 ppc_md.kexec_cpu_down(1, 0);
230 machine_kexec(kexec_crash_image);
231 /* NOTREACHED */
232 }
233 crash_ipi_callback(regs);
234}
235
Michael Ellermancc532912005-12-04 18:39:43 +1100236#else
David Wilderc0ce7d02006-06-23 15:29:34 -0700237static void crash_kexec_prepare_cpus(int cpu)
Michael Ellermancc532912005-12-04 18:39:43 +1100238{
239 /*
240 * move the secondarys to us so that we can copy
241 * the new kernel 0-0x100 safely
242 *
243 * do this if kexec in setup.c ?
244 */
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000245#ifdef CONFIG_PPC64
Michael Ellermancc532912005-12-04 18:39:43 +1100246 smp_release_cpus();
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000247#else
248 /* FIXME */
249#endif
Michael Ellermancc532912005-12-04 18:39:43 +1100250}
251
David Wilderc0ce7d02006-06-23 15:29:34 -0700252void crash_kexec_secondary(struct pt_regs *regs)
253{
254 cpus_in_sr = CPU_MASK_NONE;
255}
Michael Ellermancc532912005-12-04 18:39:43 +1100256#endif
Andre Detsch8d2655e2007-07-20 21:39:27 +0200257#ifdef CONFIG_SPU_BASE
258
259#include <asm/spu.h>
260#include <asm/spu_priv1.h>
261
262struct crash_spu_info {
263 struct spu *spu;
264 u32 saved_spu_runcntl_RW;
265 u32 saved_spu_status_R;
266 u32 saved_spu_npc_RW;
267 u64 saved_mfc_sr1_RW;
268 u64 saved_mfc_dar;
269 u64 saved_mfc_dsisr;
270};
271
272#define CRASH_NUM_SPUS 16 /* Enough for current hardware */
273static struct crash_spu_info crash_spu_info[CRASH_NUM_SPUS];
274
275static void crash_kexec_stop_spus(void)
276{
277 struct spu *spu;
278 int i;
279 u64 tmp;
280
281 for (i = 0; i < CRASH_NUM_SPUS; i++) {
282 if (!crash_spu_info[i].spu)
283 continue;
284
285 spu = crash_spu_info[i].spu;
286
287 crash_spu_info[i].saved_spu_runcntl_RW =
288 in_be32(&spu->problem->spu_runcntl_RW);
289 crash_spu_info[i].saved_spu_status_R =
290 in_be32(&spu->problem->spu_status_R);
291 crash_spu_info[i].saved_spu_npc_RW =
292 in_be32(&spu->problem->spu_npc_RW);
293
294 crash_spu_info[i].saved_mfc_dar = spu_mfc_dar_get(spu);
295 crash_spu_info[i].saved_mfc_dsisr = spu_mfc_dsisr_get(spu);
296 tmp = spu_mfc_sr1_get(spu);
297 crash_spu_info[i].saved_mfc_sr1_RW = tmp;
298
299 tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
300 spu_mfc_sr1_set(spu, tmp);
301
302 __delay(200);
303 }
304}
305
306void crash_register_spus(struct list_head *list)
307{
308 struct spu *spu;
309
310 list_for_each_entry(spu, list, full_list) {
311 if (WARN_ON(spu->number >= CRASH_NUM_SPUS))
312 continue;
313
314 crash_spu_info[spu->number].spu = spu;
315 }
316}
317
318#else
319static inline void crash_kexec_stop_spus(void)
320{
321}
322#endif /* CONFIG_SPU_BASE */
Michael Ellermancc532912005-12-04 18:39:43 +1100323
Michael Neuling496b0102008-01-18 15:50:30 +1100324/*
325 * Register a function to be called on shutdown. Only use this if you
326 * can't reset your device in the second kernel.
327 */
328int crash_shutdown_register(crash_shutdown_t handler)
329{
330 unsigned int i, rc;
331
332 spin_lock(&crash_handlers_lock);
333 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
334 if (!crash_shutdown_handles[i]) {
335 /* Insert handle at first empty entry */
336 crash_shutdown_handles[i] = handler;
337 rc = 0;
338 break;
339 }
340
341 if (i == CRASH_HANDLER_MAX) {
342 printk(KERN_ERR "Crash shutdown handles full, "
343 "not registered.\n");
344 rc = 1;
345 }
346
347 spin_unlock(&crash_handlers_lock);
348 return rc;
349}
350EXPORT_SYMBOL(crash_shutdown_register);
351
352int crash_shutdown_unregister(crash_shutdown_t handler)
353{
354 unsigned int i, rc;
355
356 spin_lock(&crash_handlers_lock);
357 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
358 if (crash_shutdown_handles[i] == handler)
359 break;
360
361 if (i == CRASH_HANDLER_MAX) {
362 printk(KERN_ERR "Crash shutdown handle not found\n");
363 rc = 1;
364 } else {
365 /* Shift handles down */
366 for (; crash_shutdown_handles[i]; i++)
367 crash_shutdown_handles[i] =
368 crash_shutdown_handles[i+1];
369 rc = 0;
370 }
371
372 spin_unlock(&crash_handlers_lock);
373 return rc;
374}
375EXPORT_SYMBOL(crash_shutdown_unregister);
376
377static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
Anton Blanchard06440792010-05-10 16:25:51 +0000378static int crash_shutdown_cpu = -1;
Michael Neuling496b0102008-01-18 15:50:30 +1100379
380static int handle_fault(struct pt_regs *regs)
381{
Anton Blanchard06440792010-05-10 16:25:51 +0000382 if (crash_shutdown_cpu == smp_processor_id())
383 longjmp(crash_shutdown_buf, 1);
Michael Neuling496b0102008-01-18 15:50:30 +1100384 return 0;
385}
386
Michael Ellermancc532912005-12-04 18:39:43 +1100387void default_machine_crash_shutdown(struct pt_regs *regs)
388{
Michael Neuling496b0102008-01-18 15:50:30 +1100389 unsigned int i;
390 int (*old_handler)(struct pt_regs *regs);
391
Michael Ellermand6c1a902006-04-04 13:43:01 +0200392
Michael Ellermancc532912005-12-04 18:39:43 +1100393 /*
394 * This function is only called after the system
Lee Revellf18190b2006-06-26 18:30:00 +0200395 * has panicked or is otherwise in a critical state.
Michael Ellermancc532912005-12-04 18:39:43 +1100396 * The minimum amount of code to allow a kexec'd kernel
397 * to run successfully needs to happen here.
398 *
399 * In practice this means stopping other cpus in
400 * an SMP system.
401 * The kernel is broken so disable interrupts.
402 */
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000403 hard_irq_disable();
Michael Ellermancc532912005-12-04 18:39:43 +1100404
Anton Blanchard249ec222010-08-02 20:39:41 +0000405 /*
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();
410 crash_save_cpu(regs, crashing_cpu);
411 crash_kexec_prepare_cpus(crashing_cpu);
412 cpu_set(crashing_cpu, cpus_in_crash);
413#if defined(CONFIG_PPC_STD_MMU_64) && defined(CONFIG_SMP)
414 crash_kexec_wait_realmode(crashing_cpu);
415#endif
416
Michael Neuling496b0102008-01-18 15:50:30 +1100417 for_each_irq(i) {
Michael Ellerman6cff46f2009-10-13 19:44:51 +0000418 struct irq_desc *desc = irq_to_desc(i);
Michael Ellermand6c1a902006-04-04 13:43:01 +0200419
Maxim Uvarov426b6cb2010-05-11 05:41:08 +0000420 if (!desc || !desc->chip || !desc->chip->eoi)
421 continue;
422
Michael Ellermand6c1a902006-04-04 13:43:01 +0200423 if (desc->status & IRQ_INPROGRESS)
Michael Neuling496b0102008-01-18 15:50:30 +1100424 desc->chip->eoi(i);
Michael Ellermand6c1a902006-04-04 13:43:01 +0200425
426 if (!(desc->status & IRQ_DISABLED))
Anton Blanchard5d7a8722010-05-10 16:27:38 +0000427 desc->chip->shutdown(i);
Michael Ellermand6c1a902006-04-04 13:43:01 +0200428 }
429
Michael Ellermancc532912005-12-04 18:39:43 +1100430 /*
Michael Neuling496b0102008-01-18 15:50:30 +1100431 * Call registered shutdown routines savely. Swap out
432 * __debugger_fault_handler, and replace on exit.
433 */
434 old_handler = __debugger_fault_handler;
435 __debugger_fault_handler = handle_fault;
Anton Blanchard06440792010-05-10 16:25:51 +0000436 crash_shutdown_cpu = smp_processor_id();
Michael Neuling496b0102008-01-18 15:50:30 +1100437 for (i = 0; crash_shutdown_handles[i]; i++) {
438 if (setjmp(crash_shutdown_buf) == 0) {
439 /*
440 * Insert syncs and delay to ensure
441 * instructions in the dangerous region don't
442 * leak away from this protected region.
443 */
444 asm volatile("sync; isync");
445 /* dangerous region */
446 crash_shutdown_handles[i]();
447 asm volatile("sync; isync");
448 }
449 }
Anton Blanchard06440792010-05-10 16:25:51 +0000450 crash_shutdown_cpu = -1;
Michael Neuling496b0102008-01-18 15:50:30 +1100451 __debugger_fault_handler = old_handler;
452
Andre Detsch8d2655e2007-07-20 21:39:27 +0200453 crash_kexec_stop_spus();
Anton Blanchard249ec222010-08-02 20:39:41 +0000454
David Wilderc0ce7d02006-06-23 15:29:34 -0700455 if (ppc_md.kexec_cpu_down)
456 ppc_md.kexec_cpu_down(1, 0);
Michael Ellermancc532912005-12-04 18:39:43 +1100457}