blob: 2bb252c01f07c583a8bf9cb863853593b5224759 [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 Blanchard8c274742011-11-30 00:23:12 +000046/* This keeps a track of which one is the crashing cpu. */
Michael Ellermancc532912005-12-04 18:39:43 +110047int crashing_cpu = -1;
Anton Blanchard2440c012011-11-30 00:23:17 +000048static int time_to_dump;
Michael Ellermancc532912005-12-04 18:39:43 +110049
Anton Blanchard158d5b5e2011-01-21 13:43:59 +110050#define CRASH_HANDLER_MAX 3
Michael Neuling496b0102008-01-18 15:50:30 +110051/* NULL terminated list of shutdown handles */
52static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX+1];
53static DEFINE_SPINLOCK(crash_handlers_lock);
54
Anton Blanchard07fe0c62011-11-30 00:23:11 +000055static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
56static int crash_shutdown_cpu = -1;
57
58static int handle_fault(struct pt_regs *regs)
59{
60 if (crash_shutdown_cpu == smp_processor_id())
61 longjmp(crash_shutdown_buf, 1);
62 return 0;
63}
64
Michael Ellermancc532912005-12-04 18:39:43 +110065#ifdef CONFIG_SMP
Michael Ellermancc532912005-12-04 18:39:43 +110066
Christian Kujau897e01a2012-01-17 19:13:05 +000067static atomic_t cpus_in_crash;
Michael Ellermancc532912005-12-04 18:39:43 +110068void crash_ipi_callback(struct pt_regs *regs)
69{
Anton Blanchard2440c012011-11-30 00:23:17 +000070 static cpumask_t cpus_state_saved = CPU_MASK_NONE;
71
Michael Ellermancc532912005-12-04 18:39:43 +110072 int cpu = smp_processor_id();
73
Michael Ellermancc532912005-12-04 18:39:43 +110074 if (!cpu_online(cpu))
75 return;
76
Paul Mackerrasd04c56f2006-10-04 16:47:49 +100077 hard_irq_disable();
Anton Blanchard2440c012011-11-30 00:23:17 +000078 if (!cpumask_test_cpu(cpu, &cpus_state_saved)) {
Magnus Damm85916f82006-12-06 20:40:41 -080079 crash_save_cpu(regs, cpu);
Anton Blanchard2440c012011-11-30 00:23:17 +000080 cpumask_set_cpu(cpu, &cpus_state_saved);
81 }
82
83 atomic_inc(&cpus_in_crash);
Peter Zijlstrac6450732014-03-13 19:00:35 +010084 smp_mb__after_atomic();
David Wilderc0ce7d02006-06-23 15:29:34 -070085
86 /*
David Wilderc0ce7d02006-06-23 15:29:34 -070087 * Starting the kdump boot.
88 * This barrier is needed to make sure that all CPUs are stopped.
David Wilderc0ce7d02006-06-23 15:29:34 -070089 */
Anton Blanchard2440c012011-11-30 00:23:17 +000090 while (!time_to_dump)
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 -0700105static void crash_kexec_prepare_cpus(int cpu)
Michael Ellermancc532912005-12-04 18:39:43 +1100106{
107 unsigned int msecs;
David Wilderc0ce7d02006-06-23 15:29:34 -0700108 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000109 int tries = 0;
110 int (*old_handler)(struct pt_regs *regs);
Michael Ellermancc532912005-12-04 18:39:43 +1100111
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000112 printk(KERN_EMERG "Sending IPI to other CPUs\n");
113
Michael Ellermancc532912005-12-04 18:39:43 +1100114 crash_send_ipi(crash_ipi_callback);
115 smp_wmb();
116
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000117again:
Michael Ellermancc532912005-12-04 18:39:43 +1100118 /*
Anton Blanchard158d5b5e2011-01-21 13:43:59 +1100119 * FIXME: Until we will have the way to stop other CPUs reliably,
Michael Ellermancc532912005-12-04 18:39:43 +1100120 * the crash CPU will send an IPI and wait for other CPUs to
David Wilderc0ce7d02006-06-23 15:29:34 -0700121 * respond.
Michael Ellermancc532912005-12-04 18:39:43 +1100122 */
Anton Blanchard549e88a2011-11-30 00:23:16 +0000123 msecs = IPI_TIMEOUT;
Anton Blanchard2440c012011-11-30 00:23:17 +0000124 while ((atomic_read(&cpus_in_crash) < ncpus) && (--msecs > 0))
Michael Ellermancc532912005-12-04 18:39:43 +1100125 mdelay(1);
Michael Ellermancc532912005-12-04 18:39:43 +1100126
127 /* Would it be better to replace the trap vector here? */
128
Anton Blanchard2440c012011-11-30 00:23:17 +0000129 if (atomic_read(&cpus_in_crash) >= ncpus) {
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000130 printk(KERN_EMERG "IPI complete\n");
131 return;
David Wilderc0ce7d02006-06-23 15:29:34 -0700132 }
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000133
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000134 printk(KERN_EMERG "ERROR: %d cpu(s) not responding\n",
Anton Blanchard2440c012011-11-30 00:23:17 +0000135 ncpus - atomic_read(&cpus_in_crash));
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000136
137 /*
138 * If we have a panic timeout set then we can't wait indefinitely
139 * for someone to activate system reset. We also give up on the
140 * second time through if system reset fail to work.
141 */
142 if ((panic_timeout > 0) || (tries > 0))
143 return;
144
145 /*
146 * A system reset will cause all CPUs to take an 0x100 exception.
147 * The primary CPU returns here via setjmp, and the secondary
148 * CPUs reexecute the crash_kexec_secondary path.
149 */
150 old_handler = __debugger;
151 __debugger = handle_fault;
152 crash_shutdown_cpu = smp_processor_id();
153
154 if (setjmp(crash_shutdown_buf) == 0) {
155 printk(KERN_EMERG "Activate system reset (dumprestart) "
156 "to stop other cpu(s)\n");
157
158 /*
159 * A system reset will force all CPUs to execute the
160 * crash code again. We need to reset cpus_in_crash so we
161 * wait for everyone to do this.
162 */
Anton Blanchard2440c012011-11-30 00:23:17 +0000163 atomic_set(&cpus_in_crash, 0);
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000164 smp_mb();
165
Anton Blanchard2440c012011-11-30 00:23:17 +0000166 while (atomic_read(&cpus_in_crash) < ncpus)
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000167 cpu_relax();
168 }
169
170 crash_shutdown_cpu = -1;
171 __debugger = old_handler;
172
173 tries++;
174 goto again;
Michael Ellermancc532912005-12-04 18:39:43 +1100175}
David Wilderc0ce7d02006-06-23 15:29:34 -0700176
177/*
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000178 * This function will be called by secondary cpus.
David Wilderc0ce7d02006-06-23 15:29:34 -0700179 */
180void crash_kexec_secondary(struct pt_regs *regs)
181{
David Wilderc0ce7d02006-06-23 15:29:34 -0700182 unsigned long flags;
Anton Blanchard549e88a2011-11-30 00:23:16 +0000183 int msecs = SECONDARY_TIMEOUT;
David Wilderc0ce7d02006-06-23 15:29:34 -0700184
185 local_irq_save(flags);
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000186
Anton Blanchard549e88a2011-11-30 00:23:16 +0000187 /* Wait for the primary crash CPU to signal its progress */
David Wilderc0ce7d02006-06-23 15:29:34 -0700188 while (crashing_cpu < 0) {
189 if (--msecs < 0) {
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000190 /* No response, kdump image may not have been loaded */
David Wilderc0ce7d02006-06-23 15:29:34 -0700191 local_irq_restore(flags);
192 return;
193 }
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000194
David Wilderc0ce7d02006-06-23 15:29:34 -0700195 mdelay(1);
David Wilderc0ce7d02006-06-23 15:29:34 -0700196 }
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000197
David Wilderc0ce7d02006-06-23 15:29:34 -0700198 crash_ipi_callback(regs);
199}
200
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000201#else /* ! CONFIG_SMP */
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000202
David Wilderc0ce7d02006-06-23 15:29:34 -0700203static void crash_kexec_prepare_cpus(int cpu)
Michael Ellermancc532912005-12-04 18:39:43 +1100204{
205 /*
Anton Blanchard8c274742011-11-30 00:23:12 +0000206 * move the secondaries to us so that we can copy
Michael Ellermancc532912005-12-04 18:39:43 +1100207 * the new kernel 0-0x100 safely
208 *
209 * do this if kexec in setup.c ?
210 */
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000211#ifdef CONFIG_PPC64
Michael Ellermancc532912005-12-04 18:39:43 +1100212 smp_release_cpus();
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000213#else
214 /* FIXME */
215#endif
Michael Ellermancc532912005-12-04 18:39:43 +1100216}
217
David Wilderc0ce7d02006-06-23 15:29:34 -0700218void crash_kexec_secondary(struct pt_regs *regs)
219{
David Wilderc0ce7d02006-06-23 15:29:34 -0700220}
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000221#endif /* CONFIG_SMP */
Michael Ellermancc532912005-12-04 18:39:43 +1100222
Ben Hutchings7707e412011-04-24 15:04:31 +0000223/* wait for all the CPUs to hit real mode but timeout if they don't come in */
Scott Woodeeaab662015-10-06 22:48:16 -0500224#if defined(CONFIG_SMP) && defined(CONFIG_PPC64)
225static void __maybe_unused crash_kexec_wait_realmode(int cpu)
Ben Hutchings7707e412011-04-24 15:04:31 +0000226{
227 unsigned int msecs;
228 int i;
229
Anton Blanchard549e88a2011-11-30 00:23:16 +0000230 msecs = REAL_MODE_TIMEOUT;
Milton Millerbd9e5ee2011-05-10 19:28:41 +0000231 for (i=0; i < nr_cpu_ids && msecs > 0; i++) {
Ben Hutchings7707e412011-04-24 15:04:31 +0000232 if (i == cpu)
233 continue;
234
235 while (paca[i].kexec_state < KEXEC_STATE_REAL_MODE) {
236 barrier();
Michael Neuling63f21a52011-07-04 20:40:10 +0000237 if (!cpu_possible(i) || !cpu_online(i) || (msecs <= 0))
Ben Hutchings7707e412011-04-24 15:04:31 +0000238 break;
Ben Hutchings7707e412011-04-24 15:04:31 +0000239 msecs--;
240 mdelay(1);
241 }
242 }
243 mb();
244}
245#else
246static inline void crash_kexec_wait_realmode(int cpu) {}
Scott Woodeeaab662015-10-06 22:48:16 -0500247#endif /* CONFIG_SMP && CONFIG_PPC64 */
Ben Hutchings7707e412011-04-24 15:04:31 +0000248
Michael Neuling496b0102008-01-18 15:50:30 +1100249/*
250 * Register a function to be called on shutdown. Only use this if you
251 * can't reset your device in the second kernel.
252 */
253int crash_shutdown_register(crash_shutdown_t handler)
254{
255 unsigned int i, rc;
256
257 spin_lock(&crash_handlers_lock);
258 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
259 if (!crash_shutdown_handles[i]) {
260 /* Insert handle at first empty entry */
261 crash_shutdown_handles[i] = handler;
262 rc = 0;
263 break;
264 }
265
266 if (i == CRASH_HANDLER_MAX) {
267 printk(KERN_ERR "Crash shutdown handles full, "
268 "not registered.\n");
269 rc = 1;
270 }
271
272 spin_unlock(&crash_handlers_lock);
273 return rc;
274}
275EXPORT_SYMBOL(crash_shutdown_register);
276
277int crash_shutdown_unregister(crash_shutdown_t handler)
278{
279 unsigned int i, rc;
280
281 spin_lock(&crash_handlers_lock);
282 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
283 if (crash_shutdown_handles[i] == handler)
284 break;
285
286 if (i == CRASH_HANDLER_MAX) {
287 printk(KERN_ERR "Crash shutdown handle not found\n");
288 rc = 1;
289 } else {
290 /* Shift handles down */
291 for (; crash_shutdown_handles[i]; i++)
292 crash_shutdown_handles[i] =
293 crash_shutdown_handles[i+1];
294 rc = 0;
295 }
296
297 spin_unlock(&crash_handlers_lock);
298 return rc;
299}
300EXPORT_SYMBOL(crash_shutdown_unregister);
301
Michael Ellermancc532912005-12-04 18:39:43 +1100302void default_machine_crash_shutdown(struct pt_regs *regs)
303{
Michael Neuling496b0102008-01-18 15:50:30 +1100304 unsigned int i;
305 int (*old_handler)(struct pt_regs *regs);
306
Michael Ellermancc532912005-12-04 18:39:43 +1100307 /*
308 * This function is only called after the system
Lee Revellf18190b2006-06-26 18:30:00 +0200309 * has panicked or is otherwise in a critical state.
Michael Ellermancc532912005-12-04 18:39:43 +1100310 * The minimum amount of code to allow a kexec'd kernel
311 * to run successfully needs to happen here.
312 *
313 * In practice this means stopping other cpus in
314 * an SMP system.
315 * The kernel is broken so disable interrupts.
316 */
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000317 hard_irq_disable();
Michael Ellermancc532912005-12-04 18:39:43 +1100318
Anton Blanchard249ec222010-08-02 20:39:41 +0000319 /*
320 * Make a note of crashing cpu. Will be used in machine_kexec
321 * such that another IPI will not be sent.
322 */
323 crashing_cpu = smp_processor_id();
Anton Blanchard549e88a2011-11-30 00:23:16 +0000324
325 /*
326 * If we came in via system reset, wait a while for the secondary
327 * CPUs to enter.
328 */
329 if (TRAP(regs) == 0x100)
330 mdelay(PRIMARY_TIMEOUT);
331
Anton Blanchard249ec222010-08-02 20:39:41 +0000332 crash_kexec_prepare_cpus(crashing_cpu);
Anton Blanchard2440c012011-11-30 00:23:17 +0000333
334 crash_save_cpu(regs, crashing_cpu);
335
336 time_to_dump = 1;
337
Anton Blanchard249ec222010-08-02 20:39:41 +0000338 crash_kexec_wait_realmode(crashing_cpu);
Anton Blanchard249ec222010-08-02 20:39:41 +0000339
Matthew McClintockc71635d2010-09-16 17:58:23 -0500340 machine_kexec_mask_interrupts();
Michael Ellermand6c1a902006-04-04 13:43:01 +0200341
Michael Ellermancc532912005-12-04 18:39:43 +1100342 /*
Anton Blanchard8c274742011-11-30 00:23:12 +0000343 * Call registered shutdown routines safely. Swap out
Michael Neuling496b0102008-01-18 15:50:30 +1100344 * __debugger_fault_handler, and replace on exit.
345 */
346 old_handler = __debugger_fault_handler;
347 __debugger_fault_handler = handle_fault;
Anton Blanchard06440792010-05-10 16:25:51 +0000348 crash_shutdown_cpu = smp_processor_id();
Michael Neuling496b0102008-01-18 15:50:30 +1100349 for (i = 0; crash_shutdown_handles[i]; i++) {
350 if (setjmp(crash_shutdown_buf) == 0) {
351 /*
352 * Insert syncs and delay to ensure
353 * instructions in the dangerous region don't
354 * leak away from this protected region.
355 */
356 asm volatile("sync; isync");
357 /* dangerous region */
358 crash_shutdown_handles[i]();
359 asm volatile("sync; isync");
360 }
361 }
Anton Blanchard06440792010-05-10 16:25:51 +0000362 crash_shutdown_cpu = -1;
Michael Neuling496b0102008-01-18 15:50:30 +1100363 __debugger_fault_handler = old_handler;
364
David Wilderc0ce7d02006-06-23 15:29:34 -0700365 if (ppc_md.kexec_cpu_down)
366 ppc_md.kexec_cpu_down(1, 0);
Michael Ellermancc532912005-12-04 18:39:43 +1100367}