blob: 571132ed12c1e4f74961eef16815533fa78c8351 [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>
27
28#include <asm/processor.h>
29#include <asm/machdep.h>
David Wilderc0ce7d02006-06-23 15:29:34 -070030#include <asm/kexec.h>
Michael Ellermancc532912005-12-04 18:39:43 +110031#include <asm/kdump.h>
32#include <asm/lmb.h>
33#include <asm/firmware.h>
Haren Mynenif6cc82f2006-01-10 19:25:25 -080034#include <asm/smp.h>
Michael Neuling496b0102008-01-18 15:50:30 +110035#include <asm/system.h>
36#include <asm/setjmp.h>
Michael Ellermancc532912005-12-04 18:39:43 +110037
38#ifdef DEBUG
39#include <asm/udbg.h>
40#define DBG(fmt...) udbg_printf(fmt)
41#else
42#define DBG(fmt...)
43#endif
44
45/* This keeps a track of which one is crashing cpu. */
46int crashing_cpu = -1;
David Wilderc0ce7d02006-06-23 15:29:34 -070047static cpumask_t cpus_in_crash = CPU_MASK_NONE;
Michael Ellermanb6f35b42006-07-05 14:39:43 +100048cpumask_t cpus_in_sr = CPU_MASK_NONE;
Michael Ellermancc532912005-12-04 18:39:43 +110049
Michael Neuling496b0102008-01-18 15:50:30 +110050#define CRASH_HANDLER_MAX 1
51/* NULL terminated list of shutdown handles */
52static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX+1];
53static DEFINE_SPINLOCK(crash_handlers_lock);
54
Michael Ellermancc532912005-12-04 18:39:43 +110055#ifdef CONFIG_SMP
David Wilderc0ce7d02006-06-23 15:29:34 -070056static atomic_t enter_on_soft_reset = ATOMIC_INIT(0);
Michael Ellermancc532912005-12-04 18:39:43 +110057
58void crash_ipi_callback(struct pt_regs *regs)
59{
60 int cpu = smp_processor_id();
61
Michael Ellermancc532912005-12-04 18:39:43 +110062 if (!cpu_online(cpu))
63 return;
64
Paul Mackerrasd04c56f2006-10-04 16:47:49 +100065 hard_irq_disable();
David Wilderc0ce7d02006-06-23 15:29:34 -070066 if (!cpu_isset(cpu, cpus_in_crash))
Magnus Damm85916f82006-12-06 20:40:41 -080067 crash_save_cpu(regs, cpu);
David Wilderc0ce7d02006-06-23 15:29:34 -070068 cpu_set(cpu, cpus_in_crash);
69
70 /*
71 * Entered via soft-reset - could be the kdump
72 * process is invoked using soft-reset or user activated
73 * it if some CPU did not respond to an IPI.
74 * For soft-reset, the secondary CPU can enter this func
75 * twice. 1 - using IPI, and 2. soft-reset.
76 * Tell the kexec CPU that entered via soft-reset and ready
77 * to go down.
78 */
79 if (cpu_isset(cpu, cpus_in_sr)) {
80 cpu_clear(cpu, cpus_in_sr);
81 atomic_inc(&enter_on_soft_reset);
82 }
83
84 /*
85 * Starting the kdump boot.
86 * This barrier is needed to make sure that all CPUs are stopped.
87 * If not, soft-reset will be invoked to bring other CPUs.
88 */
89 while (!cpu_isset(crashing_cpu, cpus_in_crash))
90 cpu_relax();
91
Michael Ellermancc532912005-12-04 18:39:43 +110092 if (ppc_md.kexec_cpu_down)
93 ppc_md.kexec_cpu_down(1, 1);
Michael Ellermanb6f35b42006-07-05 14:39:43 +100094
95#ifdef CONFIG_PPC64
Michael Ellermancc532912005-12-04 18:39:43 +110096 kexec_smp_wait();
Michael Ellermanb6f35b42006-07-05 14:39:43 +100097#else
98 for (;;); /* FIXME */
99#endif
100
Michael Ellermancc532912005-12-04 18:39:43 +1100101 /* NOTREACHED */
102}
103
David Wilderc0ce7d02006-06-23 15:29:34 -0700104/*
105 * Wait until all CPUs are entered via soft-reset.
106 */
107static void crash_soft_reset_check(int cpu)
108{
109 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
110
111 cpu_clear(cpu, cpus_in_sr);
112 while (atomic_read(&enter_on_soft_reset) != ncpus)
113 cpu_relax();
114}
115
116
117static void crash_kexec_prepare_cpus(int cpu)
Michael Ellermancc532912005-12-04 18:39:43 +1100118{
119 unsigned int msecs;
120
David Wilderc0ce7d02006-06-23 15:29:34 -0700121 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
Michael Ellermancc532912005-12-04 18:39:43 +1100122
123 crash_send_ipi(crash_ipi_callback);
124 smp_wmb();
125
126 /*
127 * FIXME: Until we will have the way to stop other CPUSs reliabally,
128 * the crash CPU will send an IPI and wait for other CPUs to
David Wilderc0ce7d02006-06-23 15:29:34 -0700129 * respond.
Haren Myneni01aaed92006-02-07 15:47:03 -0800130 * Delay of at least 10 seconds.
Michael Ellermancc532912005-12-04 18:39:43 +1100131 */
David Wilderc0ce7d02006-06-23 15:29:34 -0700132 printk(KERN_EMERG "Sending IPI to other cpus...\n");
Haren Myneni01aaed92006-02-07 15:47:03 -0800133 msecs = 10000;
David Wilderc0ce7d02006-06-23 15:29:34 -0700134 while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) {
135 cpu_relax();
Michael Ellermancc532912005-12-04 18:39:43 +1100136 mdelay(1);
137 }
138
139 /* Would it be better to replace the trap vector here? */
140
141 /*
142 * FIXME: In case if we do not get all CPUs, one possibility: ask the
143 * user to do soft reset such that we get all.
David Wilderc0ce7d02006-06-23 15:29:34 -0700144 * Soft-reset will be used until better mechanism is implemented.
Michael Ellermancc532912005-12-04 18:39:43 +1100145 */
David Wilderc0ce7d02006-06-23 15:29:34 -0700146 if (cpus_weight(cpus_in_crash) < ncpus) {
147 printk(KERN_EMERG "done waiting: %d cpu(s) not responding\n",
148 ncpus - cpus_weight(cpus_in_crash));
149 printk(KERN_EMERG "Activate soft-reset to stop other cpu(s)\n");
150 cpus_in_sr = CPU_MASK_NONE;
151 atomic_set(&enter_on_soft_reset, 0);
152 while (cpus_weight(cpus_in_crash) < ncpus)
153 cpu_relax();
154 }
155 /*
156 * Make sure all CPUs are entered via soft-reset if the kdump is
157 * invoked using soft-reset.
158 */
159 if (cpu_isset(cpu, cpus_in_sr))
160 crash_soft_reset_check(cpu);
Michael Ellermancc532912005-12-04 18:39:43 +1100161 /* Leave the IPI callback set */
162}
David Wilderc0ce7d02006-06-23 15:29:34 -0700163
164/*
165 * This function will be called by secondary cpus or by kexec cpu
166 * if soft-reset is activated to stop some CPUs.
167 */
168void crash_kexec_secondary(struct pt_regs *regs)
169{
170 int cpu = smp_processor_id();
171 unsigned long flags;
172 int msecs = 5;
173
174 local_irq_save(flags);
175 /* Wait 5ms if the kexec CPU is not entered yet. */
176 while (crashing_cpu < 0) {
177 if (--msecs < 0) {
178 /*
179 * Either kdump image is not loaded or
180 * kdump process is not started - Probably xmon
181 * exited using 'x'(exit and recover) or
182 * kexec_should_crash() failed for all running tasks.
183 */
184 cpu_clear(cpu, cpus_in_sr);
185 local_irq_restore(flags);
186 return;
187 }
188 mdelay(1);
189 cpu_relax();
190 }
191 if (cpu == crashing_cpu) {
192 /*
193 * Panic CPU will enter this func only via soft-reset.
194 * Wait until all secondary CPUs entered and
195 * then start kexec boot.
196 */
197 crash_soft_reset_check(cpu);
198 cpu_set(crashing_cpu, cpus_in_crash);
199 if (ppc_md.kexec_cpu_down)
200 ppc_md.kexec_cpu_down(1, 0);
201 machine_kexec(kexec_crash_image);
202 /* NOTREACHED */
203 }
204 crash_ipi_callback(regs);
205}
206
Michael Ellermancc532912005-12-04 18:39:43 +1100207#else
David Wilderc0ce7d02006-06-23 15:29:34 -0700208static void crash_kexec_prepare_cpus(int cpu)
Michael Ellermancc532912005-12-04 18:39:43 +1100209{
210 /*
211 * move the secondarys to us so that we can copy
212 * the new kernel 0-0x100 safely
213 *
214 * do this if kexec in setup.c ?
215 */
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000216#ifdef CONFIG_PPC64
Michael Ellermancc532912005-12-04 18:39:43 +1100217 smp_release_cpus();
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000218#else
219 /* FIXME */
220#endif
Michael Ellermancc532912005-12-04 18:39:43 +1100221}
222
David Wilderc0ce7d02006-06-23 15:29:34 -0700223void crash_kexec_secondary(struct pt_regs *regs)
224{
225 cpus_in_sr = CPU_MASK_NONE;
226}
Michael Ellermancc532912005-12-04 18:39:43 +1100227#endif
Andre Detsch8d2655e2007-07-20 21:39:27 +0200228#ifdef CONFIG_SPU_BASE
229
230#include <asm/spu.h>
231#include <asm/spu_priv1.h>
232
233struct crash_spu_info {
234 struct spu *spu;
235 u32 saved_spu_runcntl_RW;
236 u32 saved_spu_status_R;
237 u32 saved_spu_npc_RW;
238 u64 saved_mfc_sr1_RW;
239 u64 saved_mfc_dar;
240 u64 saved_mfc_dsisr;
241};
242
243#define CRASH_NUM_SPUS 16 /* Enough for current hardware */
244static struct crash_spu_info crash_spu_info[CRASH_NUM_SPUS];
245
246static void crash_kexec_stop_spus(void)
247{
248 struct spu *spu;
249 int i;
250 u64 tmp;
251
252 for (i = 0; i < CRASH_NUM_SPUS; i++) {
253 if (!crash_spu_info[i].spu)
254 continue;
255
256 spu = crash_spu_info[i].spu;
257
258 crash_spu_info[i].saved_spu_runcntl_RW =
259 in_be32(&spu->problem->spu_runcntl_RW);
260 crash_spu_info[i].saved_spu_status_R =
261 in_be32(&spu->problem->spu_status_R);
262 crash_spu_info[i].saved_spu_npc_RW =
263 in_be32(&spu->problem->spu_npc_RW);
264
265 crash_spu_info[i].saved_mfc_dar = spu_mfc_dar_get(spu);
266 crash_spu_info[i].saved_mfc_dsisr = spu_mfc_dsisr_get(spu);
267 tmp = spu_mfc_sr1_get(spu);
268 crash_spu_info[i].saved_mfc_sr1_RW = tmp;
269
270 tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
271 spu_mfc_sr1_set(spu, tmp);
272
273 __delay(200);
274 }
275}
276
277void crash_register_spus(struct list_head *list)
278{
279 struct spu *spu;
280
281 list_for_each_entry(spu, list, full_list) {
282 if (WARN_ON(spu->number >= CRASH_NUM_SPUS))
283 continue;
284
285 crash_spu_info[spu->number].spu = spu;
286 }
287}
288
289#else
290static inline void crash_kexec_stop_spus(void)
291{
292}
293#endif /* CONFIG_SPU_BASE */
Michael Ellermancc532912005-12-04 18:39:43 +1100294
Michael Neuling496b0102008-01-18 15:50:30 +1100295/*
296 * Register a function to be called on shutdown. Only use this if you
297 * can't reset your device in the second kernel.
298 */
299int crash_shutdown_register(crash_shutdown_t handler)
300{
301 unsigned int i, rc;
302
303 spin_lock(&crash_handlers_lock);
304 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
305 if (!crash_shutdown_handles[i]) {
306 /* Insert handle at first empty entry */
307 crash_shutdown_handles[i] = handler;
308 rc = 0;
309 break;
310 }
311
312 if (i == CRASH_HANDLER_MAX) {
313 printk(KERN_ERR "Crash shutdown handles full, "
314 "not registered.\n");
315 rc = 1;
316 }
317
318 spin_unlock(&crash_handlers_lock);
319 return rc;
320}
321EXPORT_SYMBOL(crash_shutdown_register);
322
323int crash_shutdown_unregister(crash_shutdown_t handler)
324{
325 unsigned int i, rc;
326
327 spin_lock(&crash_handlers_lock);
328 for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
329 if (crash_shutdown_handles[i] == handler)
330 break;
331
332 if (i == CRASH_HANDLER_MAX) {
333 printk(KERN_ERR "Crash shutdown handle not found\n");
334 rc = 1;
335 } else {
336 /* Shift handles down */
337 for (; crash_shutdown_handles[i]; i++)
338 crash_shutdown_handles[i] =
339 crash_shutdown_handles[i+1];
340 rc = 0;
341 }
342
343 spin_unlock(&crash_handlers_lock);
344 return rc;
345}
346EXPORT_SYMBOL(crash_shutdown_unregister);
347
348static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
349
350static int handle_fault(struct pt_regs *regs)
351{
352 longjmp(crash_shutdown_buf, 1);
353 return 0;
354}
355
Michael Ellermancc532912005-12-04 18:39:43 +1100356void default_machine_crash_shutdown(struct pt_regs *regs)
357{
Michael Neuling496b0102008-01-18 15:50:30 +1100358 unsigned int i;
359 int (*old_handler)(struct pt_regs *regs);
360
Michael Ellermand6c1a902006-04-04 13:43:01 +0200361
Michael Ellermancc532912005-12-04 18:39:43 +1100362 /*
363 * This function is only called after the system
Lee Revellf18190b2006-06-26 18:30:00 +0200364 * has panicked or is otherwise in a critical state.
Michael Ellermancc532912005-12-04 18:39:43 +1100365 * The minimum amount of code to allow a kexec'd kernel
366 * to run successfully needs to happen here.
367 *
368 * In practice this means stopping other cpus in
369 * an SMP system.
370 * The kernel is broken so disable interrupts.
371 */
Paul Mackerrasd04c56f2006-10-04 16:47:49 +1000372 hard_irq_disable();
Michael Ellermancc532912005-12-04 18:39:43 +1100373
Michael Neuling496b0102008-01-18 15:50:30 +1100374 for_each_irq(i) {
375 struct irq_desc *desc = irq_desc + i;
Michael Ellermand6c1a902006-04-04 13:43:01 +0200376
377 if (desc->status & IRQ_INPROGRESS)
Michael Neuling496b0102008-01-18 15:50:30 +1100378 desc->chip->eoi(i);
Michael Ellermand6c1a902006-04-04 13:43:01 +0200379
380 if (!(desc->status & IRQ_DISABLED))
Michael Neuling496b0102008-01-18 15:50:30 +1100381 desc->chip->disable(i);
Michael Ellermand6c1a902006-04-04 13:43:01 +0200382 }
383
Michael Ellermancc532912005-12-04 18:39:43 +1100384 /*
Michael Neuling496b0102008-01-18 15:50:30 +1100385 * Call registered shutdown routines savely. Swap out
386 * __debugger_fault_handler, and replace on exit.
387 */
388 old_handler = __debugger_fault_handler;
389 __debugger_fault_handler = handle_fault;
390 for (i = 0; crash_shutdown_handles[i]; i++) {
391 if (setjmp(crash_shutdown_buf) == 0) {
392 /*
393 * Insert syncs and delay to ensure
394 * instructions in the dangerous region don't
395 * leak away from this protected region.
396 */
397 asm volatile("sync; isync");
398 /* dangerous region */
399 crash_shutdown_handles[i]();
400 asm volatile("sync; isync");
401 }
402 }
403 __debugger_fault_handler = old_handler;
404
405 /*
Michael Ellermancc532912005-12-04 18:39:43 +1100406 * Make a note of crashing cpu. Will be used in machine_kexec
407 * such that another IPI will not be sent.
408 */
409 crashing_cpu = smp_processor_id();
Magnus Damm85916f82006-12-06 20:40:41 -0800410 crash_save_cpu(regs, crashing_cpu);
David Wilderc0ce7d02006-06-23 15:29:34 -0700411 crash_kexec_prepare_cpus(crashing_cpu);
412 cpu_set(crashing_cpu, cpus_in_crash);
Andre Detsch8d2655e2007-07-20 21:39:27 +0200413 crash_kexec_stop_spus();
David Wilderc0ce7d02006-06-23 15:29:34 -0700414 if (ppc_md.kexec_cpu_down)
415 ppc_md.kexec_cpu_down(1, 0);
Michael Ellermancc532912005-12-04 18:39:43 +1100416}