]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/powerpc/kernel/crash.c
UBUNTU: Ubuntu-4.13.0-45.50
[mirror_ubuntu-artful-kernel.git] / arch / powerpc / kernel / crash.c
CommitLineData
cc532915
ME
1/*
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
cc532915
ME
13#include <linux/kernel.h>
14#include <linux/smp.h>
15#include <linux/reboot.h>
16#include <linux/kexec.h>
66b15db6 17#include <linux/export.h>
cc532915 18#include <linux/crash_dump.h>
cc532915 19#include <linux/delay.h>
d6c1a908 20#include <linux/irq.h>
cc532915
ME
21#include <linux/types.h>
22
23#include <asm/processor.h>
24#include <asm/machdep.h>
c0ce7d08 25#include <asm/kexec.h>
cc532915 26#include <asm/kdump.h>
d9b2b2a2 27#include <asm/prom.h>
f6cc82fc 28#include <asm/smp.h>
496b010e 29#include <asm/setjmp.h>
ae3a197e 30#include <asm/debug.h>
cc532915 31
549e88a1
AB
32/*
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
2440c01e 46static int time_to_dump;
cc532915 47
158d5b5e 48#define CRASH_HANDLER_MAX 3
1d145165
SJS
49/* List of shutdown handles */
50static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX];
496b010e
MN
51static DEFINE_SPINLOCK(crash_handlers_lock);
52
07fe0c61
AB
53static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
54static int crash_shutdown_cpu = -1;
55
56static int handle_fault(struct pt_regs *regs)
57{
58 if (crash_shutdown_cpu == smp_processor_id())
59 longjmp(crash_shutdown_buf, 1);
60 return 0;
61}
62
cc532915 63#ifdef CONFIG_SMP
cc532915 64
897e01a0 65static atomic_t cpus_in_crash;
34852ed5 66static void crash_ipi_callback(struct pt_regs *regs)
cc532915 67{
2440c01e
AB
68 static cpumask_t cpus_state_saved = CPU_MASK_NONE;
69
cc532915
ME
70 int cpu = smp_processor_id();
71
cc532915
ME
72 if (!cpu_online(cpu))
73 return;
74
d04c56f7 75 hard_irq_disable();
2440c01e 76 if (!cpumask_test_cpu(cpu, &cpus_state_saved)) {
85916f81 77 crash_save_cpu(regs, cpu);
2440c01e
AB
78 cpumask_set_cpu(cpu, &cpus_state_saved);
79 }
80
81 atomic_inc(&cpus_in_crash);
c645073f 82 smp_mb__after_atomic();
cc532915 83
c0ce7d08
DW
84 /*
85 * Starting the kdump boot.
86 * This barrier is needed to make sure that all CPUs are stopped.
c0ce7d08 87 */
2440c01e 88 while (!time_to_dump)
c0ce7d08
DW
89 cpu_relax();
90
91 if (ppc_md.kexec_cpu_down)
92 ppc_md.kexec_cpu_down(1, 1);
b6f35b49
ME
93
94#ifdef CONFIG_PPC64
cc532915 95 kexec_smp_wait();
b6f35b49
ME
96#else
97 for (;;); /* FIXME */
98#endif
99
cc532915
ME
100 /* NOTREACHED */
101}
102
c0ce7d08 103static void crash_kexec_prepare_cpus(int cpu)
cc532915
ME
104{
105 unsigned int msecs;
c0ce7d08 106 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
07fe0c61
AB
107 int tries = 0;
108 int (*old_handler)(struct pt_regs *regs);
cc532915 109
9b00ac06
AB
110 printk(KERN_EMERG "Sending IPI to other CPUs\n");
111
cc532915
ME
112 crash_send_ipi(crash_ipi_callback);
113 smp_wmb();
114
07fe0c61 115again:
cc532915 116 /*
158d5b5e 117 * FIXME: Until we will have the way to stop other CPUs reliably,
cc532915 118 * the crash CPU will send an IPI and wait for other CPUs to
c0ce7d08 119 * respond.
cc532915 120 */
549e88a1 121 msecs = IPI_TIMEOUT;
2440c01e 122 while ((atomic_read(&cpus_in_crash) < ncpus) && (--msecs > 0))
cc532915 123 mdelay(1);
cc532915
ME
124
125 /* Would it be better to replace the trap vector here? */
126
2440c01e 127 if (atomic_read(&cpus_in_crash) >= ncpus) {
07fe0c61
AB
128 printk(KERN_EMERG "IPI complete\n");
129 return;
c0ce7d08 130 }
9b00ac06 131
07fe0c61 132 printk(KERN_EMERG "ERROR: %d cpu(s) not responding\n",
2440c01e 133 ncpus - atomic_read(&cpus_in_crash));
07fe0c61
AB
134
135 /*
136 * If we have a panic timeout set then we can't wait indefinitely
137 * for someone to activate system reset. We also give up on the
138 * second time through if system reset fail to work.
139 */
140 if ((panic_timeout > 0) || (tries > 0))
141 return;
142
143 /*
144 * A system reset will cause all CPUs to take an 0x100 exception.
145 * The primary CPU returns here via setjmp, and the secondary
146 * CPUs reexecute the crash_kexec_secondary path.
147 */
148 old_handler = __debugger;
149 __debugger = handle_fault;
150 crash_shutdown_cpu = smp_processor_id();
151
152 if (setjmp(crash_shutdown_buf) == 0) {
153 printk(KERN_EMERG "Activate system reset (dumprestart) "
154 "to stop other cpu(s)\n");
155
156 /*
157 * A system reset will force all CPUs to execute the
158 * crash code again. We need to reset cpus_in_crash so we
159 * wait for everyone to do this.
160 */
2440c01e 161 atomic_set(&cpus_in_crash, 0);
07fe0c61
AB
162 smp_mb();
163
2440c01e 164 while (atomic_read(&cpus_in_crash) < ncpus)
07fe0c61
AB
165 cpu_relax();
166 }
167
168 crash_shutdown_cpu = -1;
169 __debugger = old_handler;
170
171 tries++;
172 goto again;
cc532915 173}
c0ce7d08
DW
174
175/*
9b00ac06 176 * This function will be called by secondary cpus.
c0ce7d08
DW
177 */
178void crash_kexec_secondary(struct pt_regs *regs)
179{
c0ce7d08 180 unsigned long flags;
549e88a1 181 int msecs = SECONDARY_TIMEOUT;
c0ce7d08
DW
182
183 local_irq_save(flags);
9b00ac06 184
549e88a1 185 /* Wait for the primary crash CPU to signal its progress */
c0ce7d08
DW
186 while (crashing_cpu < 0) {
187 if (--msecs < 0) {
9b00ac06 188 /* No response, kdump image may not have been loaded */
c0ce7d08
DW
189 local_irq_restore(flags);
190 return;
191 }
9b00ac06 192
c0ce7d08 193 mdelay(1);
c0ce7d08 194 }
9b00ac06 195
c0ce7d08
DW
196 crash_ipi_callback(regs);
197}
198
7c7a81b5 199#else /* ! CONFIG_SMP */
7c7a81b5 200
c0ce7d08 201static void crash_kexec_prepare_cpus(int cpu)
cc532915
ME
202{
203 /*
8c27474a 204 * move the secondaries to us so that we can copy
cc532915
ME
205 * the new kernel 0-0x100 safely
206 *
207 * do this if kexec in setup.c ?
208 */
b6f35b49 209#ifdef CONFIG_PPC64
cc532915 210 smp_release_cpus();
b6f35b49
ME
211#else
212 /* FIXME */
213#endif
cc532915
ME
214}
215
c0ce7d08
DW
216void crash_kexec_secondary(struct pt_regs *regs)
217{
c0ce7d08 218}
7c7a81b5 219#endif /* CONFIG_SMP */
cc532915 220
7707e411 221/* wait for all the CPUs to hit real mode but timeout if they don't come in */
eeaab663
SW
222#if defined(CONFIG_SMP) && defined(CONFIG_PPC64)
223static void __maybe_unused crash_kexec_wait_realmode(int cpu)
7707e411
BH
224{
225 unsigned int msecs;
226 int i;
227
549e88a1 228 msecs = REAL_MODE_TIMEOUT;
bd9e5eef 229 for (i=0; i < nr_cpu_ids && msecs > 0; i++) {
7707e411
BH
230 if (i == cpu)
231 continue;
232
233 while (paca[i].kexec_state < KEXEC_STATE_REAL_MODE) {
234 barrier();
63f21a56 235 if (!cpu_possible(i) || !cpu_online(i) || (msecs <= 0))
7707e411 236 break;
7707e411
BH
237 msecs--;
238 mdelay(1);
239 }
240 }
241 mb();
242}
243#else
244static inline void crash_kexec_wait_realmode(int cpu) {}
eeaab663 245#endif /* CONFIG_SMP && CONFIG_PPC64 */
7707e411 246
496b010e
MN
247/*
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 */
1d145165 289 for (; i < (CRASH_HANDLER_MAX - 1); i++)
496b010e
MN
290 crash_shutdown_handles[i] =
291 crash_shutdown_handles[i+1];
1d145165
SJS
292 /*
293 * Reset last entry to NULL now that it has been shifted down,
294 * this will allow new handles to be added here.
295 */
296 crash_shutdown_handles[i] = NULL;
496b010e
MN
297 rc = 0;
298 }
299
300 spin_unlock(&crash_handlers_lock);
301 return rc;
302}
303EXPORT_SYMBOL(crash_shutdown_unregister);
304
cc532915
ME
305void default_machine_crash_shutdown(struct pt_regs *regs)
306{
496b010e
MN
307 unsigned int i;
308 int (*old_handler)(struct pt_regs *regs);
309
cc532915
ME
310 /*
311 * This function is only called after the system
f18190bd 312 * has panicked or is otherwise in a critical state.
cc532915
ME
313 * The minimum amount of code to allow a kexec'd kernel
314 * to run successfully needs to happen here.
315 *
316 * In practice this means stopping other cpus in
317 * an SMP system.
318 * The kernel is broken so disable interrupts.
319 */
d04c56f7 320 hard_irq_disable();
cc532915 321
249ec228
AB
322 /*
323 * Make a note of crashing cpu. Will be used in machine_kexec
324 * such that another IPI will not be sent.
325 */
326 crashing_cpu = smp_processor_id();
549e88a1
AB
327
328 /*
329 * If we came in via system reset, wait a while for the secondary
330 * CPUs to enter.
331 */
332 if (TRAP(regs) == 0x100)
333 mdelay(PRIMARY_TIMEOUT);
334
249ec228 335 crash_kexec_prepare_cpus(crashing_cpu);
2440c01e
AB
336
337 crash_save_cpu(regs, crashing_cpu);
338
339 time_to_dump = 1;
340
249ec228 341 crash_kexec_wait_realmode(crashing_cpu);
249ec228 342
c71635d2 343 machine_kexec_mask_interrupts();
496b010e
MN
344
345 /*
8c27474a 346 * Call registered shutdown routines safely. Swap out
496b010e
MN
347 * __debugger_fault_handler, and replace on exit.
348 */
349 old_handler = __debugger_fault_handler;
350 __debugger_fault_handler = handle_fault;
06440794 351 crash_shutdown_cpu = smp_processor_id();
a7d63928 352 for (i = 0; i < CRASH_HANDLER_MAX && crash_shutdown_handles[i]; i++) {
496b010e
MN
353 if (setjmp(crash_shutdown_buf) == 0) {
354 /*
355 * Insert syncs and delay to ensure
356 * instructions in the dangerous region don't
357 * leak away from this protected region.
358 */
359 asm volatile("sync; isync");
360 /* dangerous region */
361 crash_shutdown_handles[i]();
362 asm volatile("sync; isync");
363 }
d6c1a908 364 }
06440794 365 crash_shutdown_cpu = -1;
496b010e 366 __debugger_fault_handler = old_handler;
d6c1a908 367
c0ce7d08
DW
368 if (ppc_md.kexec_cpu_down)
369 ppc_md.kexec_cpu_down(1, 0);
cc532915 370}