blob: 00b215125d3ea38beda6ac8f23d95e17f23ba87a [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
Michael Ellermancc532912005-12-04 18:39:43 +110013#include <linux/kernel.h>
14#include <linux/smp.h>
15#include <linux/reboot.h>
16#include <linux/kexec.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040017#include <linux/export.h>
Michael Ellermancc532912005-12-04 18:39:43 +110018#include <linux/crash_dump.h>
Michael Ellermancc532912005-12-04 18:39:43 +110019#include <linux/delay.h>
Michael Ellermand6c1a902006-04-04 13:43:01 +020020#include <linux/irq.h>
Michael Ellermancc532912005-12-04 18:39:43 +110021#include <linux/types.h>
22
23#include <asm/processor.h>
24#include <asm/machdep.h>
David Wilderc0ce7d02006-06-23 15:29:34 -070025#include <asm/kexec.h>
Michael Ellermancc532912005-12-04 18:39:43 +110026#include <asm/kdump.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080027#include <asm/prom.h>
Haren Mynenif6cc82f2006-01-10 19:25:25 -080028#include <asm/smp.h>
Michael Neuling496b0102008-01-18 15:50:30 +110029#include <asm/setjmp.h>
David Howellsae3a1972012-03-28 18:30:02 +010030#include <asm/debug.h>
Michael Ellermancc532912005-12-04 18:39:43 +110031
Anton Blanchard549e88a2011-11-30 00:23:16 +000032/*
33 * The primary CPU waits a while for all secondary CPUs to enter. This is to
34 * avoid sending an IPI if the secondary CPUs are entering
35 * crash_kexec_secondary on their own (eg via a system reset).
36 *
37 * The secondary timeout has to be longer than the primary. Both timeouts are
38 * in milliseconds.
39 */
40#define PRIMARY_TIMEOUT 500
41#define SECONDARY_TIMEOUT 1000
42
43#define IPI_TIMEOUT 10000
44#define REAL_MODE_TIMEOUT 10000
45
Anton Blanchard2440c012011-11-30 00:23:17 +000046static int time_to_dump;
Balbir Singh4145f352017-12-15 19:14:55 +110047/*
48 * crash_wake_offline should be set to 1 by platforms that intend to wake
49 * up offline cpus prior to jumping to a kdump kernel. Currently powernv
50 * sets it to 1, since we want to avoid things from happening when an
51 * offline CPU wakes up due to something like an HMI (malfunction error),
52 * which propagates to all threads.
53 */
54int crash_wake_offline;
Michael Ellermancc532912005-12-04 18:39:43 +110055
Anton Blanchard158d5b5e2011-01-21 13:43:59 +110056#define CRASH_HANDLER_MAX 3
Suraj Jitindar Singh1d145162016-05-11 10:57:32 +100057/* List of shutdown handles */
58static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX];
Michael Neuling496b0102008-01-18 15:50:30 +110059static DEFINE_SPINLOCK(crash_handlers_lock);
60
Anton Blanchard07fe0c62011-11-30 00:23:11 +000061static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
62static int crash_shutdown_cpu = -1;
63
64static int handle_fault(struct pt_regs *regs)
65{
66 if (crash_shutdown_cpu == smp_processor_id())
67 longjmp(crash_shutdown_buf, 1);
68 return 0;
69}
70
Michael Ellermancc532912005-12-04 18:39:43 +110071#ifdef CONFIG_SMP
Michael Ellermancc532912005-12-04 18:39:43 +110072
Christian Kujau897e01a2012-01-17 19:13:05 +000073static atomic_t cpus_in_crash;
Balbir Singh4145f352017-12-15 19:14:55 +110074void crash_ipi_callback(struct pt_regs *regs)
Michael Ellermancc532912005-12-04 18:39:43 +110075{
Anton Blanchard2440c012011-11-30 00:23:17 +000076 static cpumask_t cpus_state_saved = CPU_MASK_NONE;
77
Michael Ellermancc532912005-12-04 18:39:43 +110078 int cpu = smp_processor_id();
79
Paul Mackerrasd04c56f2006-10-04 16:47:49 +100080 hard_irq_disable();
Anton Blanchard2440c012011-11-30 00:23:17 +000081 if (!cpumask_test_cpu(cpu, &cpus_state_saved)) {
Magnus Damm85916f82006-12-06 20:40:41 -080082 crash_save_cpu(regs, cpu);
Anton Blanchard2440c012011-11-30 00:23:17 +000083 cpumask_set_cpu(cpu, &cpus_state_saved);
84 }
85
86 atomic_inc(&cpus_in_crash);
Peter Zijlstrac6450732014-03-13 19:00:35 +010087 smp_mb__after_atomic();
David Wilderc0ce7d02006-06-23 15:29:34 -070088
89 /*
David Wilderc0ce7d02006-06-23 15:29:34 -070090 * Starting the kdump boot.
91 * This barrier is needed to make sure that all CPUs are stopped.
David Wilderc0ce7d02006-06-23 15:29:34 -070092 */
Anton Blanchard2440c012011-11-30 00:23:17 +000093 while (!time_to_dump)
David Wilderc0ce7d02006-06-23 15:29:34 -070094 cpu_relax();
95
Michael Ellermancc532912005-12-04 18:39:43 +110096 if (ppc_md.kexec_cpu_down)
97 ppc_md.kexec_cpu_down(1, 1);
Michael Ellermanb6f35b42006-07-05 14:39:43 +100098
99#ifdef CONFIG_PPC64
Michael Ellermancc532912005-12-04 18:39:43 +1100100 kexec_smp_wait();
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000101#else
102 for (;;); /* FIXME */
103#endif
104
Michael Ellermancc532912005-12-04 18:39:43 +1100105 /* NOTREACHED */
106}
107
David Wilderc0ce7d02006-06-23 15:29:34 -0700108static void crash_kexec_prepare_cpus(int cpu)
Michael Ellermancc532912005-12-04 18:39:43 +1100109{
110 unsigned int msecs;
David Wilderc0ce7d02006-06-23 15:29:34 -0700111 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000112 int tries = 0;
113 int (*old_handler)(struct pt_regs *regs);
Michael Ellermancc532912005-12-04 18:39:43 +1100114
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000115 printk(KERN_EMERG "Sending IPI to other CPUs\n");
116
Balbir Singh4145f352017-12-15 19:14:55 +1100117 if (crash_wake_offline)
118 ncpus = num_present_cpus() - 1;
119
Michael Ellermancc532912005-12-04 18:39:43 +1100120 crash_send_ipi(crash_ipi_callback);
121 smp_wmb();
122
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000123again:
Michael Ellermancc532912005-12-04 18:39:43 +1100124 /*
Anton Blanchard158d5b5e2011-01-21 13:43:59 +1100125 * FIXME: Until we will have the way to stop other CPUs reliably,
Michael Ellermancc532912005-12-04 18:39:43 +1100126 * the crash CPU will send an IPI and wait for other CPUs to
David Wilderc0ce7d02006-06-23 15:29:34 -0700127 * respond.
Michael Ellermancc532912005-12-04 18:39:43 +1100128 */
Anton Blanchard549e88a2011-11-30 00:23:16 +0000129 msecs = IPI_TIMEOUT;
Anton Blanchard2440c012011-11-30 00:23:17 +0000130 while ((atomic_read(&cpus_in_crash) < ncpus) && (--msecs > 0))
Michael Ellermancc532912005-12-04 18:39:43 +1100131 mdelay(1);
Michael Ellermancc532912005-12-04 18:39:43 +1100132
133 /* Would it be better to replace the trap vector here? */
134
Anton Blanchard2440c012011-11-30 00:23:17 +0000135 if (atomic_read(&cpus_in_crash) >= ncpus) {
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000136 printk(KERN_EMERG "IPI complete\n");
137 return;
David Wilderc0ce7d02006-06-23 15:29:34 -0700138 }
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000139
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000140 printk(KERN_EMERG "ERROR: %d cpu(s) not responding\n",
Anton Blanchard2440c012011-11-30 00:23:17 +0000141 ncpus - atomic_read(&cpus_in_crash));
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000142
143 /*
144 * If we have a panic timeout set then we can't wait indefinitely
145 * for someone to activate system reset. We also give up on the
146 * second time through if system reset fail to work.
147 */
148 if ((panic_timeout > 0) || (tries > 0))
149 return;
150
151 /*
152 * A system reset will cause all CPUs to take an 0x100 exception.
153 * The primary CPU returns here via setjmp, and the secondary
154 * CPUs reexecute the crash_kexec_secondary path.
155 */
156 old_handler = __debugger;
157 __debugger = handle_fault;
158 crash_shutdown_cpu = smp_processor_id();
159
160 if (setjmp(crash_shutdown_buf) == 0) {
161 printk(KERN_EMERG "Activate system reset (dumprestart) "
162 "to stop other cpu(s)\n");
163
164 /*
165 * A system reset will force all CPUs to execute the
166 * crash code again. We need to reset cpus_in_crash so we
167 * wait for everyone to do this.
168 */
Anton Blanchard2440c012011-11-30 00:23:17 +0000169 atomic_set(&cpus_in_crash, 0);
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000170 smp_mb();
171
Anton Blanchard2440c012011-11-30 00:23:17 +0000172 while (atomic_read(&cpus_in_crash) < ncpus)
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000173 cpu_relax();
174 }
175
176 crash_shutdown_cpu = -1;
177 __debugger = old_handler;
178
179 tries++;
180 goto again;
Michael Ellermancc532912005-12-04 18:39:43 +1100181}
David Wilderc0ce7d02006-06-23 15:29:34 -0700182
183/*
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000184 * This function will be called by secondary cpus.
David Wilderc0ce7d02006-06-23 15:29:34 -0700185 */
186void crash_kexec_secondary(struct pt_regs *regs)
187{
David Wilderc0ce7d02006-06-23 15:29:34 -0700188 unsigned long flags;
Anton Blanchard549e88a2011-11-30 00:23:16 +0000189 int msecs = SECONDARY_TIMEOUT;
David Wilderc0ce7d02006-06-23 15:29:34 -0700190
191 local_irq_save(flags);
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000192
Anton Blanchard549e88a2011-11-30 00:23:16 +0000193 /* Wait for the primary crash CPU to signal its progress */
David Wilderc0ce7d02006-06-23 15:29:34 -0700194 while (crashing_cpu < 0) {
195 if (--msecs < 0) {
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000196 /* No response, kdump image may not have been loaded */
David Wilderc0ce7d02006-06-23 15:29:34 -0700197 local_irq_restore(flags);
198 return;
199 }
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000200
David Wilderc0ce7d02006-06-23 15:29:34 -0700201 mdelay(1);
David Wilderc0ce7d02006-06-23 15:29:34 -0700202 }
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000203
David Wilderc0ce7d02006-06-23 15:29:34 -0700204 crash_ipi_callback(regs);
205}
206
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000207#else /* ! CONFIG_SMP */
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000208
David Wilderc0ce7d02006-06-23 15:29:34 -0700209static void crash_kexec_prepare_cpus(int cpu)
Michael Ellermancc532912005-12-04 18:39:43 +1100210{
211 /*
Anton Blanchard8c274742011-11-30 00:23:12 +0000212 * move the secondaries to us so that we can copy
Michael Ellermancc532912005-12-04 18:39:43 +1100213 * the new kernel 0-0x100 safely
214 *
215 * do this if kexec in setup.c ?
216 */
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000217#ifdef CONFIG_PPC64
Michael Ellermancc532912005-12-04 18:39:43 +1100218 smp_release_cpus();
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000219#else
220 /* FIXME */
221#endif
Michael Ellermancc532912005-12-04 18:39:43 +1100222}
223
David Wilderc0ce7d02006-06-23 15:29:34 -0700224void crash_kexec_secondary(struct pt_regs *regs)
225{
David Wilderc0ce7d02006-06-23 15:29:34 -0700226}
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000227#endif /* CONFIG_SMP */
Michael Ellermancc532912005-12-04 18:39:43 +1100228
Ben Hutchings7707e412011-04-24 15:04:31 +0000229/* wait for all the CPUs to hit real mode but timeout if they don't come in */
Scott Woodeeaab662015-10-06 22:48:16 -0500230#if defined(CONFIG_SMP) && defined(CONFIG_PPC64)
231static void __maybe_unused crash_kexec_wait_realmode(int cpu)
Ben Hutchings7707e412011-04-24 15:04:31 +0000232{
233 unsigned int msecs;
234 int i;
235
Anton Blanchard549e88a2011-11-30 00:23:16 +0000236 msecs = REAL_MODE_TIMEOUT;
Milton Millerbd9e5ee2011-05-10 19:28:41 +0000237 for (i=0; i < nr_cpu_ids && msecs > 0; i++) {
Ben Hutchings7707e412011-04-24 15:04:31 +0000238 if (i == cpu)
239 continue;
240
241 while (paca[i].kexec_state < KEXEC_STATE_REAL_MODE) {
242 barrier();
Michael Neuling63f21a52011-07-04 20:40:10 +0000243 if (!cpu_possible(i) || !cpu_online(i) || (msecs <= 0))
Ben Hutchings7707e412011-04-24 15:04:31 +0000244 break;
Ben Hutchings7707e412011-04-24 15:04:31 +0000245 msecs--;
246 mdelay(1);
247 }
248 }
249 mb();
250}
251#else
252static inline void crash_kexec_wait_realmode(int cpu) {}
Scott Woodeeaab662015-10-06 22:48:16 -0500253#endif /* CONFIG_SMP && CONFIG_PPC64 */
Ben Hutchings7707e412011-04-24 15:04:31 +0000254
Michael Neuling496b0102008-01-18 15:50:30 +1100255/*
256 * Register a function to be called on shutdown. Only use this if you
257 * can't reset your device in the second kernel.
258 */
259int crash_shutdown_register(crash_shutdown_t handler)
260{
261 unsigned int i, rc;
262
263 spin_lock(&crash_handlers_lock);
264 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
265 if (!crash_shutdown_handles[i]) {
266 /* Insert handle at first empty entry */
267 crash_shutdown_handles[i] = handler;
268 rc = 0;
269 break;
270 }
271
272 if (i == CRASH_HANDLER_MAX) {
273 printk(KERN_ERR "Crash shutdown handles full, "
274 "not registered.\n");
275 rc = 1;
276 }
277
278 spin_unlock(&crash_handlers_lock);
279 return rc;
280}
281EXPORT_SYMBOL(crash_shutdown_register);
282
283int crash_shutdown_unregister(crash_shutdown_t handler)
284{
285 unsigned int i, rc;
286
287 spin_lock(&crash_handlers_lock);
288 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
289 if (crash_shutdown_handles[i] == handler)
290 break;
291
292 if (i == CRASH_HANDLER_MAX) {
293 printk(KERN_ERR "Crash shutdown handle not found\n");
294 rc = 1;
295 } else {
296 /* Shift handles down */
Suraj Jitindar Singh1d145162016-05-11 10:57:32 +1000297 for (; i < (CRASH_HANDLER_MAX - 1); i++)
Michael Neuling496b0102008-01-18 15:50:30 +1100298 crash_shutdown_handles[i] =
299 crash_shutdown_handles[i+1];
Suraj Jitindar Singh1d145162016-05-11 10:57:32 +1000300 /*
301 * Reset last entry to NULL now that it has been shifted down,
302 * this will allow new handles to be added here.
303 */
304 crash_shutdown_handles[i] = NULL;
Michael Neuling496b0102008-01-18 15:50:30 +1100305 rc = 0;
306 }
307
308 spin_unlock(&crash_handlers_lock);
309 return rc;
310}
311EXPORT_SYMBOL(crash_shutdown_unregister);
312
Michael Ellermancc532912005-12-04 18:39:43 +1100313void default_machine_crash_shutdown(struct pt_regs *regs)
314{
Michael Neuling496b0102008-01-18 15:50:30 +1100315 unsigned int i;
316 int (*old_handler)(struct pt_regs *regs);
317
Michael Ellermancc532912005-12-04 18:39:43 +1100318 /*
319 * This function is only called after the system
Lee Revellf18190b2006-06-26 18:30:00 +0200320 * has panicked or is otherwise in a critical state.
Michael Ellermancc532912005-12-04 18:39:43 +1100321 * The minimum amount of code to allow a kexec'd kernel
322 * to run successfully needs to happen here.
323 *
324 * In practice this means stopping other cpus in
325 * an SMP system.
326 * The kernel is broken so disable interrupts.
327 */
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000328 hard_irq_disable();
Michael Ellermancc532912005-12-04 18:39:43 +1100329
Anton Blanchard249ec222010-08-02 20:39:41 +0000330 /*
331 * Make a note of crashing cpu. Will be used in machine_kexec
332 * such that another IPI will not be sent.
333 */
334 crashing_cpu = smp_processor_id();
Anton Blanchard549e88a2011-11-30 00:23:16 +0000335
336 /*
337 * If we came in via system reset, wait a while for the secondary
338 * CPUs to enter.
339 */
340 if (TRAP(regs) == 0x100)
341 mdelay(PRIMARY_TIMEOUT);
342
Anton Blanchard249ec222010-08-02 20:39:41 +0000343 crash_kexec_prepare_cpus(crashing_cpu);
Anton Blanchard2440c012011-11-30 00:23:17 +0000344
345 crash_save_cpu(regs, crashing_cpu);
346
347 time_to_dump = 1;
348
Anton Blanchard249ec222010-08-02 20:39:41 +0000349 crash_kexec_wait_realmode(crashing_cpu);
Anton Blanchard249ec222010-08-02 20:39:41 +0000350
Matthew McClintockc71635d2010-09-16 17:58:23 -0500351 machine_kexec_mask_interrupts();
Michael Ellermand6c1a902006-04-04 13:43:01 +0200352
Michael Ellermancc532912005-12-04 18:39:43 +1100353 /*
Anton Blanchard8c274742011-11-30 00:23:12 +0000354 * Call registered shutdown routines safely. Swap out
Michael Neuling496b0102008-01-18 15:50:30 +1100355 * __debugger_fault_handler, and replace on exit.
356 */
357 old_handler = __debugger_fault_handler;
358 __debugger_fault_handler = handle_fault;
Anton Blanchard06440792010-05-10 16:25:51 +0000359 crash_shutdown_cpu = smp_processor_id();
Suraj Jitindar Singha7d63922016-07-11 14:17:31 +1000360 for (i = 0; i < CRASH_HANDLER_MAX && crash_shutdown_handles[i]; i++) {
Michael Neuling496b0102008-01-18 15:50:30 +1100361 if (setjmp(crash_shutdown_buf) == 0) {
362 /*
363 * Insert syncs and delay to ensure
364 * instructions in the dangerous region don't
365 * leak away from this protected region.
366 */
367 asm volatile("sync; isync");
368 /* dangerous region */
369 crash_shutdown_handles[i]();
370 asm volatile("sync; isync");
371 }
372 }
Anton Blanchard06440792010-05-10 16:25:51 +0000373 crash_shutdown_cpu = -1;
Michael Neuling496b0102008-01-18 15:50:30 +1100374 __debugger_fault_handler = old_handler;
375
David Wilderc0ce7d02006-06-23 15:29:34 -0700376 if (ppc_md.kexec_cpu_down)
377 ppc_md.kexec_cpu_down(1, 0);
Michael Ellermancc532912005-12-04 18:39:43 +1100378}