blob: 21f2c781ded102506daac960019710d9a30dcf75 [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
Anton Blanchard158d5b5e2011-01-21 13:43:59 +110051#define CRASH_HANDLER_MAX 3
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();
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000067 if (!cpumask_test_cpu(cpu, &cpus_in_crash))
Magnus Damm85916f82006-12-06 20:40:41 -080068 crash_save_cpu(regs, cpu);
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000069 cpumask_set_cpu(cpu, &cpus_in_crash);
David Wilderc0ce7d02006-06-23 15:29:34 -070070
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 */
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000080 if (cpumask_test_cpu(cpu, &cpus_in_sr)) {
81 cpumask_clear_cpu(cpu, &cpus_in_sr);
David Wilderc0ce7d02006-06-23 15:29:34 -070082 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 */
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000090 while (!cpumask_test_cpu(crashing_cpu, &cpus_in_crash))
David Wilderc0ce7d02006-06-23 15:29:34 -070091 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
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000112 cpumask_clear_cpu(cpu, &cpus_in_sr);
David Wilderc0ce7d02006-06-23 15:29:34 -0700113 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 /*
Anton Blanchard158d5b5e2011-01-21 13:43:59 +1100128 * FIXME: Until we will have the way to stop other CPUs reliably,
Michael Ellermancc532912005-12-04 18:39:43 +1100129 * 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;
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000135 while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) {
David Wilderc0ce7d02006-06-23 15:29:34 -0700136 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 */
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000147 if (cpumask_weight(&cpus_in_crash) < ncpus) {
David Wilderc0ce7d02006-06-23 15:29:34 -0700148 printk(KERN_EMERG "done waiting: %d cpu(s) not responding\n",
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000149 ncpus - cpumask_weight(&cpus_in_crash));
David Wilderc0ce7d02006-06-23 15:29:34 -0700150 printk(KERN_EMERG "Activate soft-reset to stop other cpu(s)\n");
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000151 cpumask_clear(&cpus_in_sr);
David Wilderc0ce7d02006-06-23 15:29:34 -0700152 atomic_set(&enter_on_soft_reset, 0);
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000153 while (cpumask_weight(&cpus_in_crash) < ncpus)
David Wilderc0ce7d02006-06-23 15:29:34 -0700154 cpu_relax();
155 }
156 /*
157 * Make sure all CPUs are entered via soft-reset if the kdump is
158 * invoked using soft-reset.
159 */
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000160 if (cpumask_test_cpu(cpu, &cpus_in_sr))
David Wilderc0ce7d02006-06-23 15:29:34 -0700161 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
165/*
166 * This function will be called by secondary cpus or by kexec cpu
167 * if soft-reset is activated to stop some CPUs.
168 */
169void crash_kexec_secondary(struct pt_regs *regs)
170{
171 int cpu = smp_processor_id();
172 unsigned long flags;
173 int msecs = 5;
174
175 local_irq_save(flags);
176 /* Wait 5ms if the kexec CPU is not entered yet. */
177 while (crashing_cpu < 0) {
178 if (--msecs < 0) {
179 /*
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.
184 */
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000185 cpumask_clear_cpu(cpu, &cpus_in_sr);
David Wilderc0ce7d02006-06-23 15:29:34 -0700186 local_irq_restore(flags);
187 return;
188 }
189 mdelay(1);
190 cpu_relax();
191 }
192 if (cpu == crashing_cpu) {
193 /*
194 * Panic CPU will enter this func only via soft-reset.
195 * Wait until all secondary CPUs entered and
196 * then start kexec boot.
197 */
198 crash_soft_reset_check(cpu);
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000199 cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
David Wilderc0ce7d02006-06-23 15:29:34 -0700200 if (ppc_md.kexec_cpu_down)
201 ppc_md.kexec_cpu_down(1, 0);
202 machine_kexec(kexec_crash_image);
203 /* NOTREACHED */
204 }
205 crash_ipi_callback(regs);
206}
207
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000208#else /* ! CONFIG_SMP */
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000209
David Wilderc0ce7d02006-06-23 15:29:34 -0700210static void crash_kexec_prepare_cpus(int cpu)
Michael Ellermancc532912005-12-04 18:39:43 +1100211{
212 /*
213 * move the secondarys to us so that we can copy
214 * the new kernel 0-0x100 safely
215 *
216 * do this if kexec in setup.c ?
217 */
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000218#ifdef CONFIG_PPC64
Michael Ellermancc532912005-12-04 18:39:43 +1100219 smp_release_cpus();
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000220#else
221 /* FIXME */
222#endif
Michael Ellermancc532912005-12-04 18:39:43 +1100223}
224
David Wilderc0ce7d02006-06-23 15:29:34 -0700225void crash_kexec_secondary(struct pt_regs *regs)
226{
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000227 cpumask_clear(&cpus_in_sr);
David Wilderc0ce7d02006-06-23 15:29:34 -0700228}
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000229#endif /* CONFIG_SMP */
Michael Ellermancc532912005-12-04 18:39:43 +1100230
Ben Hutchings7707e412011-04-24 15:04:31 +0000231/* wait for all the CPUs to hit real mode but timeout if they don't come in */
232#if defined(CONFIG_SMP) && defined(CONFIG_PPC_STD_MMU_64)
233static void crash_kexec_wait_realmode(int cpu)
234{
235 unsigned int msecs;
236 int i;
237
238 msecs = 10000;
239 for (i=0; i < NR_CPUS && msecs > 0; i++) {
240 if (i == cpu)
241 continue;
242
243 while (paca[i].kexec_state < KEXEC_STATE_REAL_MODE) {
244 barrier();
245 if (!cpu_possible(i)) {
246 break;
247 }
248 if (!cpu_online(i)) {
249 break;
250 }
251 msecs--;
252 mdelay(1);
253 }
254 }
255 mb();
256}
257#else
258static inline void crash_kexec_wait_realmode(int cpu) {}
259#endif /* CONFIG_SMP && CONFIG_PPC_STD_MMU_64 */
260
Michael Neuling496b0102008-01-18 15:50:30 +1100261/*
262 * Register a function to be called on shutdown. Only use this if you
263 * can't reset your device in the second kernel.
264 */
265int crash_shutdown_register(crash_shutdown_t handler)
266{
267 unsigned int i, rc;
268
269 spin_lock(&crash_handlers_lock);
270 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
271 if (!crash_shutdown_handles[i]) {
272 /* Insert handle at first empty entry */
273 crash_shutdown_handles[i] = handler;
274 rc = 0;
275 break;
276 }
277
278 if (i == CRASH_HANDLER_MAX) {
279 printk(KERN_ERR "Crash shutdown handles full, "
280 "not registered.\n");
281 rc = 1;
282 }
283
284 spin_unlock(&crash_handlers_lock);
285 return rc;
286}
287EXPORT_SYMBOL(crash_shutdown_register);
288
289int crash_shutdown_unregister(crash_shutdown_t handler)
290{
291 unsigned int i, rc;
292
293 spin_lock(&crash_handlers_lock);
294 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
295 if (crash_shutdown_handles[i] == handler)
296 break;
297
298 if (i == CRASH_HANDLER_MAX) {
299 printk(KERN_ERR "Crash shutdown handle not found\n");
300 rc = 1;
301 } else {
302 /* Shift handles down */
303 for (; crash_shutdown_handles[i]; i++)
304 crash_shutdown_handles[i] =
305 crash_shutdown_handles[i+1];
306 rc = 0;
307 }
308
309 spin_unlock(&crash_handlers_lock);
310 return rc;
311}
312EXPORT_SYMBOL(crash_shutdown_unregister);
313
314static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
Anton Blanchard06440792010-05-10 16:25:51 +0000315static int crash_shutdown_cpu = -1;
Michael Neuling496b0102008-01-18 15:50:30 +1100316
317static int handle_fault(struct pt_regs *regs)
318{
Anton Blanchard06440792010-05-10 16:25:51 +0000319 if (crash_shutdown_cpu == smp_processor_id())
320 longjmp(crash_shutdown_buf, 1);
Michael Neuling496b0102008-01-18 15:50:30 +1100321 return 0;
322}
323
Michael Ellermancc532912005-12-04 18:39:43 +1100324void default_machine_crash_shutdown(struct pt_regs *regs)
325{
Michael Neuling496b0102008-01-18 15:50:30 +1100326 unsigned int i;
327 int (*old_handler)(struct pt_regs *regs);
328
Michael Ellermand6c1a902006-04-04 13:43:01 +0200329
Michael Ellermancc532912005-12-04 18:39:43 +1100330 /*
331 * This function is only called after the system
Lee Revellf18190b2006-06-26 18:30:00 +0200332 * has panicked or is otherwise in a critical state.
Michael Ellermancc532912005-12-04 18:39:43 +1100333 * The minimum amount of code to allow a kexec'd kernel
334 * to run successfully needs to happen here.
335 *
336 * In practice this means stopping other cpus in
337 * an SMP system.
338 * The kernel is broken so disable interrupts.
339 */
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000340 hard_irq_disable();
Michael Ellermancc532912005-12-04 18:39:43 +1100341
Anton Blanchard249ec222010-08-02 20:39:41 +0000342 /*
343 * Make a note of crashing cpu. Will be used in machine_kexec
344 * such that another IPI will not be sent.
345 */
346 crashing_cpu = smp_processor_id();
347 crash_save_cpu(regs, crashing_cpu);
348 crash_kexec_prepare_cpus(crashing_cpu);
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000349 cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
Anton Blanchard249ec222010-08-02 20:39:41 +0000350 crash_kexec_wait_realmode(crashing_cpu);
Anton Blanchard249ec222010-08-02 20:39:41 +0000351
Matthew McClintockc71635d2010-09-16 17:58:23 -0500352 machine_kexec_mask_interrupts();
Michael Ellermand6c1a902006-04-04 13:43:01 +0200353
Michael Ellermancc532912005-12-04 18:39:43 +1100354 /*
Michael Neuling496b0102008-01-18 15:50:30 +1100355 * Call registered shutdown routines savely. Swap out
356 * __debugger_fault_handler, and replace on exit.
357 */
358 old_handler = __debugger_fault_handler;
359 __debugger_fault_handler = handle_fault;
Anton Blanchard06440792010-05-10 16:25:51 +0000360 crash_shutdown_cpu = smp_processor_id();
Michael Neuling496b0102008-01-18 15:50:30 +1100361 for (i = 0; crash_shutdown_handles[i]; i++) {
362 if (setjmp(crash_shutdown_buf) == 0) {
363 /*
364 * Insert syncs and delay to ensure
365 * instructions in the dangerous region don't
366 * leak away from this protected region.
367 */
368 asm volatile("sync; isync");
369 /* dangerous region */
370 crash_shutdown_handles[i]();
371 asm volatile("sync; isync");
372 }
373 }
Anton Blanchard06440792010-05-10 16:25:51 +0000374 crash_shutdown_cpu = -1;
Michael Neuling496b0102008-01-18 15:50:30 +1100375 __debugger_fault_handler = old_handler;
376
David Wilderc0ce7d02006-06-23 15:29:34 -0700377 if (ppc_md.kexec_cpu_down)
378 ppc_md.kexec_cpu_down(1, 0);
Michael Ellermancc532912005-12-04 18:39:43 +1100379}