blob: a8b6e2d705a46909163637eb9251829d0a7cc2a2 [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>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040020#include <linux/export.h>
Michael Ellermancc532912005-12-04 18:39:43 +110021#include <linux/crash_dump.h>
Michael Ellermancc532912005-12-04 18:39:43 +110022#include <linux/delay.h>
23#include <linux/elf.h>
24#include <linux/elfcore.h>
25#include <linux/init.h>
Michael Ellermand6c1a902006-04-04 13:43:01 +020026#include <linux/irq.h>
Michael Ellermancc532912005-12-04 18:39:43 +110027#include <linux/types.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100028#include <linux/memblock.h>
Michael Ellermancc532912005-12-04 18:39:43 +110029
30#include <asm/processor.h>
31#include <asm/machdep.h>
David Wilderc0ce7d02006-06-23 15:29:34 -070032#include <asm/kexec.h>
Michael Ellermancc532912005-12-04 18:39:43 +110033#include <asm/kdump.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080034#include <asm/prom.h>
Michael Ellermancc532912005-12-04 18:39:43 +110035#include <asm/firmware.h>
Haren Mynenif6cc82f2006-01-10 19:25:25 -080036#include <asm/smp.h>
Michael Neuling496b0102008-01-18 15:50:30 +110037#include <asm/system.h>
38#include <asm/setjmp.h>
Michael Ellermancc532912005-12-04 18:39:43 +110039
40#ifdef DEBUG
41#include <asm/udbg.h>
42#define DBG(fmt...) udbg_printf(fmt)
43#else
44#define DBG(fmt...)
45#endif
46
47/* This keeps a track of which one is crashing cpu. */
48int crashing_cpu = -1;
David Wilderc0ce7d02006-06-23 15:29:34 -070049static cpumask_t cpus_in_crash = 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
Anton Blanchard07fe0c62011-11-30 00:23:11 +000056static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
57static int crash_shutdown_cpu = -1;
58
59static int handle_fault(struct pt_regs *regs)
60{
61 if (crash_shutdown_cpu == smp_processor_id())
62 longjmp(crash_shutdown_buf, 1);
63 return 0;
64}
65
Michael Ellermancc532912005-12-04 18:39:43 +110066#ifdef CONFIG_SMP
Michael Ellermancc532912005-12-04 18:39:43 +110067
68void crash_ipi_callback(struct pt_regs *regs)
69{
70 int cpu = smp_processor_id();
71
Michael Ellermancc532912005-12-04 18:39:43 +110072 if (!cpu_online(cpu))
73 return;
74
Paul Mackerrasd04c56f2006-10-04 16:47:49 +100075 hard_irq_disable();
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000076 if (!cpumask_test_cpu(cpu, &cpus_in_crash))
Magnus Damm85916f82006-12-06 20:40:41 -080077 crash_save_cpu(regs, cpu);
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000078 cpumask_set_cpu(cpu, &cpus_in_crash);
David Wilderc0ce7d02006-06-23 15:29:34 -070079
80 /*
David Wilderc0ce7d02006-06-23 15:29:34 -070081 * Starting the kdump boot.
82 * This barrier is needed to make sure that all CPUs are stopped.
David Wilderc0ce7d02006-06-23 15:29:34 -070083 */
KOSAKI Motohiro104699c2011-04-28 05:07:23 +000084 while (!cpumask_test_cpu(crashing_cpu, &cpus_in_crash))
David Wilderc0ce7d02006-06-23 15:29:34 -070085 cpu_relax();
86
Michael Ellermancc532912005-12-04 18:39:43 +110087 if (ppc_md.kexec_cpu_down)
88 ppc_md.kexec_cpu_down(1, 1);
Michael Ellermanb6f35b42006-07-05 14:39:43 +100089
90#ifdef CONFIG_PPC64
Michael Ellermancc532912005-12-04 18:39:43 +110091 kexec_smp_wait();
Michael Ellermanb6f35b42006-07-05 14:39:43 +100092#else
93 for (;;); /* FIXME */
94#endif
95
Michael Ellermancc532912005-12-04 18:39:43 +110096 /* NOTREACHED */
97}
98
David Wilderc0ce7d02006-06-23 15:29:34 -070099static void crash_kexec_prepare_cpus(int cpu)
Michael Ellermancc532912005-12-04 18:39:43 +1100100{
101 unsigned int msecs;
David Wilderc0ce7d02006-06-23 15:29:34 -0700102 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000103 int tries = 0;
104 int (*old_handler)(struct pt_regs *regs);
Michael Ellermancc532912005-12-04 18:39:43 +1100105
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000106 printk(KERN_EMERG "Sending IPI to other CPUs\n");
107
Michael Ellermancc532912005-12-04 18:39:43 +1100108 crash_send_ipi(crash_ipi_callback);
109 smp_wmb();
110
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000111again:
Michael Ellermancc532912005-12-04 18:39:43 +1100112 /*
Anton Blanchard158d5b5e2011-01-21 13:43:59 +1100113 * FIXME: Until we will have the way to stop other CPUs reliably,
Michael Ellermancc532912005-12-04 18:39:43 +1100114 * the crash CPU will send an IPI and wait for other CPUs to
David Wilderc0ce7d02006-06-23 15:29:34 -0700115 * respond.
Haren Myneni01aaed92006-02-07 15:47:03 -0800116 * Delay of at least 10 seconds.
Michael Ellermancc532912005-12-04 18:39:43 +1100117 */
Haren Myneni01aaed92006-02-07 15:47:03 -0800118 msecs = 10000;
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000119 while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) {
David Wilderc0ce7d02006-06-23 15:29:34 -0700120 cpu_relax();
Michael Ellermancc532912005-12-04 18:39:43 +1100121 mdelay(1);
122 }
123
124 /* Would it be better to replace the trap vector here? */
125
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000126 if (cpumask_weight(&cpus_in_crash) >= ncpus) {
127 printk(KERN_EMERG "IPI complete\n");
128 return;
David Wilderc0ce7d02006-06-23 15:29:34 -0700129 }
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000130
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000131 printk(KERN_EMERG "ERROR: %d cpu(s) not responding\n",
132 ncpus - cpumask_weight(&cpus_in_crash));
133
134 /*
135 * If we have a panic timeout set then we can't wait indefinitely
136 * for someone to activate system reset. We also give up on the
137 * second time through if system reset fail to work.
138 */
139 if ((panic_timeout > 0) || (tries > 0))
140 return;
141
142 /*
143 * A system reset will cause all CPUs to take an 0x100 exception.
144 * The primary CPU returns here via setjmp, and the secondary
145 * CPUs reexecute the crash_kexec_secondary path.
146 */
147 old_handler = __debugger;
148 __debugger = handle_fault;
149 crash_shutdown_cpu = smp_processor_id();
150
151 if (setjmp(crash_shutdown_buf) == 0) {
152 printk(KERN_EMERG "Activate system reset (dumprestart) "
153 "to stop other cpu(s)\n");
154
155 /*
156 * A system reset will force all CPUs to execute the
157 * crash code again. We need to reset cpus_in_crash so we
158 * wait for everyone to do this.
159 */
160 cpus_in_crash = CPU_MASK_NONE;
161 smp_mb();
162
163 while (cpumask_weight(&cpus_in_crash) < ncpus)
164 cpu_relax();
165 }
166
167 crash_shutdown_cpu = -1;
168 __debugger = old_handler;
169
170 tries++;
171 goto again;
Michael Ellermancc532912005-12-04 18:39:43 +1100172}
David Wilderc0ce7d02006-06-23 15:29:34 -0700173
174/*
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000175 * This function will be called by secondary cpus.
David Wilderc0ce7d02006-06-23 15:29:34 -0700176 */
177void crash_kexec_secondary(struct pt_regs *regs)
178{
David Wilderc0ce7d02006-06-23 15:29:34 -0700179 unsigned long flags;
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000180 int msecs = 500;
David Wilderc0ce7d02006-06-23 15:29:34 -0700181
182 local_irq_save(flags);
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000183
184 /* Wait 500ms for the primary crash CPU to signal its progress */
David Wilderc0ce7d02006-06-23 15:29:34 -0700185 while (crashing_cpu < 0) {
186 if (--msecs < 0) {
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000187 /* No response, kdump image may not have been loaded */
David Wilderc0ce7d02006-06-23 15:29:34 -0700188 local_irq_restore(flags);
189 return;
190 }
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000191
David Wilderc0ce7d02006-06-23 15:29:34 -0700192 mdelay(1);
193 cpu_relax();
194 }
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000195
David Wilderc0ce7d02006-06-23 15:29:34 -0700196 crash_ipi_callback(regs);
197}
198
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000199#else /* ! CONFIG_SMP */
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000200
David Wilderc0ce7d02006-06-23 15:29:34 -0700201static void crash_kexec_prepare_cpus(int cpu)
Michael Ellermancc532912005-12-04 18:39:43 +1100202{
203 /*
204 * move the secondarys to us so that we can copy
205 * the new kernel 0-0x100 safely
206 *
207 * do this if kexec in setup.c ?
208 */
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000209#ifdef CONFIG_PPC64
Michael Ellermancc532912005-12-04 18:39:43 +1100210 smp_release_cpus();
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000211#else
212 /* FIXME */
213#endif
Michael Ellermancc532912005-12-04 18:39:43 +1100214}
215
David Wilderc0ce7d02006-06-23 15:29:34 -0700216void crash_kexec_secondary(struct pt_regs *regs)
217{
David Wilderc0ce7d02006-06-23 15:29:34 -0700218}
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000219#endif /* CONFIG_SMP */
Michael Ellermancc532912005-12-04 18:39:43 +1100220
Ben Hutchings7707e412011-04-24 15:04:31 +0000221/* wait for all the CPUs to hit real mode but timeout if they don't come in */
222#if defined(CONFIG_SMP) && defined(CONFIG_PPC_STD_MMU_64)
223static void crash_kexec_wait_realmode(int cpu)
224{
225 unsigned int msecs;
226 int i;
227
228 msecs = 10000;
Milton Millerbd9e5ee2011-05-10 19:28:41 +0000229 for (i=0; i < nr_cpu_ids && msecs > 0; i++) {
Ben Hutchings7707e412011-04-24 15:04:31 +0000230 if (i == cpu)
231 continue;
232
233 while (paca[i].kexec_state < KEXEC_STATE_REAL_MODE) {
234 barrier();
Michael Neuling63f21a52011-07-04 20:40:10 +0000235 if (!cpu_possible(i) || !cpu_online(i) || (msecs <= 0))
Ben Hutchings7707e412011-04-24 15:04:31 +0000236 break;
Ben Hutchings7707e412011-04-24 15:04:31 +0000237 msecs--;
238 mdelay(1);
239 }
240 }
241 mb();
242}
243#else
244static inline void crash_kexec_wait_realmode(int cpu) {}
245#endif /* CONFIG_SMP && CONFIG_PPC_STD_MMU_64 */
246
Michael Neuling496b0102008-01-18 15:50:30 +1100247/*
248 * Register a function to be called on shutdown. Only use this if you
249 * can't reset your device in the second kernel.
250 */
251int crash_shutdown_register(crash_shutdown_t handler)
252{
253 unsigned int i, rc;
254
255 spin_lock(&crash_handlers_lock);
256 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
257 if (!crash_shutdown_handles[i]) {
258 /* Insert handle at first empty entry */
259 crash_shutdown_handles[i] = handler;
260 rc = 0;
261 break;
262 }
263
264 if (i == CRASH_HANDLER_MAX) {
265 printk(KERN_ERR "Crash shutdown handles full, "
266 "not registered.\n");
267 rc = 1;
268 }
269
270 spin_unlock(&crash_handlers_lock);
271 return rc;
272}
273EXPORT_SYMBOL(crash_shutdown_register);
274
275int crash_shutdown_unregister(crash_shutdown_t handler)
276{
277 unsigned int i, rc;
278
279 spin_lock(&crash_handlers_lock);
280 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
281 if (crash_shutdown_handles[i] == handler)
282 break;
283
284 if (i == CRASH_HANDLER_MAX) {
285 printk(KERN_ERR "Crash shutdown handle not found\n");
286 rc = 1;
287 } else {
288 /* Shift handles down */
289 for (; crash_shutdown_handles[i]; i++)
290 crash_shutdown_handles[i] =
291 crash_shutdown_handles[i+1];
292 rc = 0;
293 }
294
295 spin_unlock(&crash_handlers_lock);
296 return rc;
297}
298EXPORT_SYMBOL(crash_shutdown_unregister);
299
Michael Ellermancc532912005-12-04 18:39:43 +1100300void default_machine_crash_shutdown(struct pt_regs *regs)
301{
Michael Neuling496b0102008-01-18 15:50:30 +1100302 unsigned int i;
303 int (*old_handler)(struct pt_regs *regs);
304
Michael Ellermand6c1a902006-04-04 13:43:01 +0200305
Michael Ellermancc532912005-12-04 18:39:43 +1100306 /*
307 * This function is only called after the system
Lee Revellf18190b2006-06-26 18:30:00 +0200308 * has panicked or is otherwise in a critical state.
Michael Ellermancc532912005-12-04 18:39:43 +1100309 * The minimum amount of code to allow a kexec'd kernel
310 * to run successfully needs to happen here.
311 *
312 * In practice this means stopping other cpus in
313 * an SMP system.
314 * The kernel is broken so disable interrupts.
315 */
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000316 hard_irq_disable();
Michael Ellermancc532912005-12-04 18:39:43 +1100317
Anton Blanchard249ec222010-08-02 20:39:41 +0000318 /*
319 * Make a note of crashing cpu. Will be used in machine_kexec
320 * such that another IPI will not be sent.
321 */
322 crashing_cpu = smp_processor_id();
323 crash_save_cpu(regs, crashing_cpu);
324 crash_kexec_prepare_cpus(crashing_cpu);
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000325 cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
Anton Blanchard249ec222010-08-02 20:39:41 +0000326 crash_kexec_wait_realmode(crashing_cpu);
Anton Blanchard249ec222010-08-02 20:39:41 +0000327
Matthew McClintockc71635d2010-09-16 17:58:23 -0500328 machine_kexec_mask_interrupts();
Michael Ellermand6c1a902006-04-04 13:43:01 +0200329
Michael Ellermancc532912005-12-04 18:39:43 +1100330 /*
Michael Neuling496b0102008-01-18 15:50:30 +1100331 * Call registered shutdown routines savely. Swap out
332 * __debugger_fault_handler, and replace on exit.
333 */
334 old_handler = __debugger_fault_handler;
335 __debugger_fault_handler = handle_fault;
Anton Blanchard06440792010-05-10 16:25:51 +0000336 crash_shutdown_cpu = smp_processor_id();
Michael Neuling496b0102008-01-18 15:50:30 +1100337 for (i = 0; crash_shutdown_handles[i]; i++) {
338 if (setjmp(crash_shutdown_buf) == 0) {
339 /*
340 * Insert syncs and delay to ensure
341 * instructions in the dangerous region don't
342 * leak away from this protected region.
343 */
344 asm volatile("sync; isync");
345 /* dangerous region */
346 crash_shutdown_handles[i]();
347 asm volatile("sync; isync");
348 }
349 }
Anton Blanchard06440792010-05-10 16:25:51 +0000350 crash_shutdown_cpu = -1;
Michael Neuling496b0102008-01-18 15:50:30 +1100351 __debugger_fault_handler = old_handler;
352
David Wilderc0ce7d02006-06-23 15:29:34 -0700353 if (ppc_md.kexec_cpu_down)
354 ppc_md.kexec_cpu_down(1, 0);
Michael Ellermancc532912005-12-04 18:39:43 +1100355}