blob: b942980e96505f0feb2327dfe747131972f88854 [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 Ellermancc532912005-12-04 18:39:43 +110020#include <linux/init.h>
Michael Ellermand6c1a902006-04-04 13:43:01 +020021#include <linux/irq.h>
Michael Ellermancc532912005-12-04 18:39:43 +110022#include <linux/types.h>
23
24#include <asm/processor.h>
25#include <asm/machdep.h>
David Wilderc0ce7d02006-06-23 15:29:34 -070026#include <asm/kexec.h>
Michael Ellermancc532912005-12-04 18:39:43 +110027#include <asm/kdump.h>
David S. Millerd9b2b2a2008-02-13 16:56:49 -080028#include <asm/prom.h>
Haren Mynenif6cc82f2006-01-10 19:25:25 -080029#include <asm/smp.h>
Michael Neuling496b0102008-01-18 15:50:30 +110030#include <asm/system.h>
31#include <asm/setjmp.h>
Michael Ellermancc532912005-12-04 18:39:43 +110032
Anton Blanchard549e88a2011-11-30 00:23:16 +000033/*
34 * The primary CPU waits a while for all secondary CPUs to enter. This is to
35 * avoid sending an IPI if the secondary CPUs are entering
36 * crash_kexec_secondary on their own (eg via a system reset).
37 *
38 * The secondary timeout has to be longer than the primary. Both timeouts are
39 * in milliseconds.
40 */
41#define PRIMARY_TIMEOUT 500
42#define SECONDARY_TIMEOUT 1000
43
44#define IPI_TIMEOUT 10000
45#define REAL_MODE_TIMEOUT 10000
46
Anton Blanchard8c274742011-11-30 00:23:12 +000047/* This keeps a track of which one is the crashing cpu. */
Michael Ellermancc532912005-12-04 18:39:43 +110048int 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.
Michael Ellermancc532912005-12-04 18:39:43 +1100116 */
Anton Blanchard549e88a2011-11-30 00:23:16 +0000117 msecs = IPI_TIMEOUT;
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000118 while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) {
Michael Ellermancc532912005-12-04 18:39:43 +1100119 mdelay(1);
120 }
121
122 /* Would it be better to replace the trap vector here? */
123
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000124 if (cpumask_weight(&cpus_in_crash) >= ncpus) {
125 printk(KERN_EMERG "IPI complete\n");
126 return;
David Wilderc0ce7d02006-06-23 15:29:34 -0700127 }
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000128
Anton Blanchard07fe0c62011-11-30 00:23:11 +0000129 printk(KERN_EMERG "ERROR: %d cpu(s) not responding\n",
130 ncpus - cpumask_weight(&cpus_in_crash));
131
132 /*
133 * If we have a panic timeout set then we can't wait indefinitely
134 * for someone to activate system reset. We also give up on the
135 * second time through if system reset fail to work.
136 */
137 if ((panic_timeout > 0) || (tries > 0))
138 return;
139
140 /*
141 * A system reset will cause all CPUs to take an 0x100 exception.
142 * The primary CPU returns here via setjmp, and the secondary
143 * CPUs reexecute the crash_kexec_secondary path.
144 */
145 old_handler = __debugger;
146 __debugger = handle_fault;
147 crash_shutdown_cpu = smp_processor_id();
148
149 if (setjmp(crash_shutdown_buf) == 0) {
150 printk(KERN_EMERG "Activate system reset (dumprestart) "
151 "to stop other cpu(s)\n");
152
153 /*
154 * A system reset will force all CPUs to execute the
155 * crash code again. We need to reset cpus_in_crash so we
156 * wait for everyone to do this.
157 */
158 cpus_in_crash = CPU_MASK_NONE;
159 smp_mb();
160
161 while (cpumask_weight(&cpus_in_crash) < ncpus)
162 cpu_relax();
163 }
164
165 crash_shutdown_cpu = -1;
166 __debugger = old_handler;
167
168 tries++;
169 goto again;
Michael Ellermancc532912005-12-04 18:39:43 +1100170}
David Wilderc0ce7d02006-06-23 15:29:34 -0700171
172/*
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000173 * This function will be called by secondary cpus.
David Wilderc0ce7d02006-06-23 15:29:34 -0700174 */
175void crash_kexec_secondary(struct pt_regs *regs)
176{
David Wilderc0ce7d02006-06-23 15:29:34 -0700177 unsigned long flags;
Anton Blanchard549e88a2011-11-30 00:23:16 +0000178 int msecs = SECONDARY_TIMEOUT;
David Wilderc0ce7d02006-06-23 15:29:34 -0700179
180 local_irq_save(flags);
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000181
Anton Blanchard549e88a2011-11-30 00:23:16 +0000182 /* Wait for the primary crash CPU to signal its progress */
David Wilderc0ce7d02006-06-23 15:29:34 -0700183 while (crashing_cpu < 0) {
184 if (--msecs < 0) {
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000185 /* No response, kdump image may not have been loaded */
David Wilderc0ce7d02006-06-23 15:29:34 -0700186 local_irq_restore(flags);
187 return;
188 }
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000189
David Wilderc0ce7d02006-06-23 15:29:34 -0700190 mdelay(1);
David Wilderc0ce7d02006-06-23 15:29:34 -0700191 }
Anton Blanchard9b00ac02011-11-30 00:23:10 +0000192
David Wilderc0ce7d02006-06-23 15:29:34 -0700193 crash_ipi_callback(regs);
194}
195
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000196#else /* ! CONFIG_SMP */
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000197
David Wilderc0ce7d02006-06-23 15:29:34 -0700198static void crash_kexec_prepare_cpus(int cpu)
Michael Ellermancc532912005-12-04 18:39:43 +1100199{
200 /*
Anton Blanchard8c274742011-11-30 00:23:12 +0000201 * move the secondaries to us so that we can copy
Michael Ellermancc532912005-12-04 18:39:43 +1100202 * the new kernel 0-0x100 safely
203 *
204 * do this if kexec in setup.c ?
205 */
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000206#ifdef CONFIG_PPC64
Michael Ellermancc532912005-12-04 18:39:43 +1100207 smp_release_cpus();
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000208#else
209 /* FIXME */
210#endif
Michael Ellermancc532912005-12-04 18:39:43 +1100211}
212
David Wilderc0ce7d02006-06-23 15:29:34 -0700213void crash_kexec_secondary(struct pt_regs *regs)
214{
David Wilderc0ce7d02006-06-23 15:29:34 -0700215}
Paul Gortmaker7c7a81b2011-04-13 06:30:08 +0000216#endif /* CONFIG_SMP */
Michael Ellermancc532912005-12-04 18:39:43 +1100217
Ben Hutchings7707e412011-04-24 15:04:31 +0000218/* wait for all the CPUs to hit real mode but timeout if they don't come in */
219#if defined(CONFIG_SMP) && defined(CONFIG_PPC_STD_MMU_64)
220static void crash_kexec_wait_realmode(int cpu)
221{
222 unsigned int msecs;
223 int i;
224
Anton Blanchard549e88a2011-11-30 00:23:16 +0000225 msecs = REAL_MODE_TIMEOUT;
Milton Millerbd9e5ee2011-05-10 19:28:41 +0000226 for (i=0; i < nr_cpu_ids && msecs > 0; i++) {
Ben Hutchings7707e412011-04-24 15:04:31 +0000227 if (i == cpu)
228 continue;
229
230 while (paca[i].kexec_state < KEXEC_STATE_REAL_MODE) {
231 barrier();
Michael Neuling63f21a52011-07-04 20:40:10 +0000232 if (!cpu_possible(i) || !cpu_online(i) || (msecs <= 0))
Ben Hutchings7707e412011-04-24 15:04:31 +0000233 break;
Ben Hutchings7707e412011-04-24 15:04:31 +0000234 msecs--;
235 mdelay(1);
236 }
237 }
238 mb();
239}
240#else
241static inline void crash_kexec_wait_realmode(int cpu) {}
242#endif /* CONFIG_SMP && CONFIG_PPC_STD_MMU_64 */
243
Michael Neuling496b0102008-01-18 15:50:30 +1100244/*
245 * Register a function to be called on shutdown. Only use this if you
246 * can't reset your device in the second kernel.
247 */
248int crash_shutdown_register(crash_shutdown_t handler)
249{
250 unsigned int i, rc;
251
252 spin_lock(&crash_handlers_lock);
253 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
254 if (!crash_shutdown_handles[i]) {
255 /* Insert handle at first empty entry */
256 crash_shutdown_handles[i] = handler;
257 rc = 0;
258 break;
259 }
260
261 if (i == CRASH_HANDLER_MAX) {
262 printk(KERN_ERR "Crash shutdown handles full, "
263 "not registered.\n");
264 rc = 1;
265 }
266
267 spin_unlock(&crash_handlers_lock);
268 return rc;
269}
270EXPORT_SYMBOL(crash_shutdown_register);
271
272int crash_shutdown_unregister(crash_shutdown_t handler)
273{
274 unsigned int i, rc;
275
276 spin_lock(&crash_handlers_lock);
277 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
278 if (crash_shutdown_handles[i] == handler)
279 break;
280
281 if (i == CRASH_HANDLER_MAX) {
282 printk(KERN_ERR "Crash shutdown handle not found\n");
283 rc = 1;
284 } else {
285 /* Shift handles down */
286 for (; crash_shutdown_handles[i]; i++)
287 crash_shutdown_handles[i] =
288 crash_shutdown_handles[i+1];
289 rc = 0;
290 }
291
292 spin_unlock(&crash_handlers_lock);
293 return rc;
294}
295EXPORT_SYMBOL(crash_shutdown_unregister);
296
Michael Ellermancc532912005-12-04 18:39:43 +1100297void default_machine_crash_shutdown(struct pt_regs *regs)
298{
Michael Neuling496b0102008-01-18 15:50:30 +1100299 unsigned int i;
300 int (*old_handler)(struct pt_regs *regs);
301
Michael Ellermancc532912005-12-04 18:39:43 +1100302 /*
303 * This function is only called after the system
Lee Revellf18190b2006-06-26 18:30:00 +0200304 * has panicked or is otherwise in a critical state.
Michael Ellermancc532912005-12-04 18:39:43 +1100305 * The minimum amount of code to allow a kexec'd kernel
306 * to run successfully needs to happen here.
307 *
308 * In practice this means stopping other cpus in
309 * an SMP system.
310 * The kernel is broken so disable interrupts.
311 */
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000312 hard_irq_disable();
Michael Ellermancc532912005-12-04 18:39:43 +1100313
Anton Blanchard249ec222010-08-02 20:39:41 +0000314 /*
315 * Make a note of crashing cpu. Will be used in machine_kexec
316 * such that another IPI will not be sent.
317 */
318 crashing_cpu = smp_processor_id();
319 crash_save_cpu(regs, crashing_cpu);
Anton Blanchard549e88a2011-11-30 00:23:16 +0000320
321 /*
322 * If we came in via system reset, wait a while for the secondary
323 * CPUs to enter.
324 */
325 if (TRAP(regs) == 0x100)
326 mdelay(PRIMARY_TIMEOUT);
327
Anton Blanchard249ec222010-08-02 20:39:41 +0000328 crash_kexec_prepare_cpus(crashing_cpu);
KOSAKI Motohiro104699c2011-04-28 05:07:23 +0000329 cpumask_set_cpu(crashing_cpu, &cpus_in_crash);
Anton Blanchard249ec222010-08-02 20:39:41 +0000330 crash_kexec_wait_realmode(crashing_cpu);
Anton Blanchard249ec222010-08-02 20:39:41 +0000331
Matthew McClintockc71635d2010-09-16 17:58:23 -0500332 machine_kexec_mask_interrupts();
Michael Ellermand6c1a902006-04-04 13:43:01 +0200333
Michael Ellermancc532912005-12-04 18:39:43 +1100334 /*
Anton Blanchard8c274742011-11-30 00:23:12 +0000335 * Call registered shutdown routines safely. Swap out
Michael Neuling496b0102008-01-18 15:50:30 +1100336 * __debugger_fault_handler, and replace on exit.
337 */
338 old_handler = __debugger_fault_handler;
339 __debugger_fault_handler = handle_fault;
Anton Blanchard06440792010-05-10 16:25:51 +0000340 crash_shutdown_cpu = smp_processor_id();
Michael Neuling496b0102008-01-18 15:50:30 +1100341 for (i = 0; crash_shutdown_handles[i]; i++) {
342 if (setjmp(crash_shutdown_buf) == 0) {
343 /*
344 * Insert syncs and delay to ensure
345 * instructions in the dangerous region don't
346 * leak away from this protected region.
347 */
348 asm volatile("sync; isync");
349 /* dangerous region */
350 crash_shutdown_handles[i]();
351 asm volatile("sync; isync");
352 }
353 }
Anton Blanchard06440792010-05-10 16:25:51 +0000354 crash_shutdown_cpu = -1;
Michael Neuling496b0102008-01-18 15:50:30 +1100355 __debugger_fault_handler = old_handler;
356
David Wilderc0ce7d02006-06-23 15:29:34 -0700357 if (ppc_md.kexec_cpu_down)
358 ppc_md.kexec_cpu_down(1, 0);
Michael Ellermancc532912005-12-04 18:39:43 +1100359}