1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/kernel.h>
14#include <linux/smp.h>
15#include <linux/reboot.h>
16#include <linux/kexec.h>
17#include <linux/export.h>
18#include <linux/crash_dump.h>
19#include <linux/delay.h>
20#include <linux/irq.h>
21#include <linux/types.h>
22
23#include <asm/processor.h>
24#include <asm/machdep.h>
25#include <asm/kexec.h>
26#include <asm/kdump.h>
27#include <asm/prom.h>
28#include <asm/smp.h>
29#include <asm/setjmp.h>
30#include <asm/debug.h>
31
32
33
34
35
36
37
38
39
40#define PRIMARY_TIMEOUT 500
41#define SECONDARY_TIMEOUT 1000
42
43#define IPI_TIMEOUT 10000
44#define REAL_MODE_TIMEOUT 10000
45
46
47int crashing_cpu = -1;
48static int time_to_dump;
49
50#define CRASH_HANDLER_MAX 3
51
52static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX];
53static DEFINE_SPINLOCK(crash_handlers_lock);
54
55static 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
65#ifdef CONFIG_SMP
66
67static atomic_t cpus_in_crash;
68static void crash_ipi_callback(struct pt_regs *regs)
69{
70 static cpumask_t cpus_state_saved = CPU_MASK_NONE;
71
72 int cpu = smp_processor_id();
73
74 if (!cpu_online(cpu))
75 return;
76
77 hard_irq_disable();
78 if (!cpumask_test_cpu(cpu, &cpus_state_saved)) {
79 crash_save_cpu(regs, cpu);
80 cpumask_set_cpu(cpu, &cpus_state_saved);
81 }
82
83 atomic_inc(&cpus_in_crash);
84 smp_mb__after_atomic();
85
86
87
88
89
90 while (!time_to_dump)
91 cpu_relax();
92
93 if (ppc_md.kexec_cpu_down)
94 ppc_md.kexec_cpu_down(1, 1);
95
96#ifdef CONFIG_PPC64
97 kexec_smp_wait();
98#else
99 for (;;);
100#endif
101
102
103}
104
105static void crash_kexec_prepare_cpus(int cpu)
106{
107 unsigned int msecs;
108 unsigned int ncpus = num_online_cpus() - 1;
109 int tries = 0;
110 int (*old_handler)(struct pt_regs *regs);
111
112 printk(KERN_EMERG "Sending IPI to other CPUs\n");
113
114 crash_send_ipi(crash_ipi_callback);
115 smp_wmb();
116
117again:
118
119
120
121
122
123 msecs = IPI_TIMEOUT;
124 while ((atomic_read(&cpus_in_crash) < ncpus) && (--msecs > 0))
125 mdelay(1);
126
127
128
129 if (atomic_read(&cpus_in_crash) >= ncpus) {
130 printk(KERN_EMERG "IPI complete\n");
131 return;
132 }
133
134 printk(KERN_EMERG "ERROR: %d cpu(s) not responding\n",
135 ncpus - atomic_read(&cpus_in_crash));
136
137
138
139
140
141
142 if ((panic_timeout > 0) || (tries > 0))
143 return;
144
145
146
147
148
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
160
161
162
163 atomic_set(&cpus_in_crash, 0);
164 smp_mb();
165
166 while (atomic_read(&cpus_in_crash) < ncpus)
167 cpu_relax();
168 }
169
170 crash_shutdown_cpu = -1;
171 __debugger = old_handler;
172
173 tries++;
174 goto again;
175}
176
177
178
179
180void crash_kexec_secondary(struct pt_regs *regs)
181{
182 unsigned long flags;
183 int msecs = SECONDARY_TIMEOUT;
184
185 local_irq_save(flags);
186
187
188 while (crashing_cpu < 0) {
189 if (--msecs < 0) {
190
191 local_irq_restore(flags);
192 return;
193 }
194
195 mdelay(1);
196 }
197
198 crash_ipi_callback(regs);
199}
200
201#else
202
203static void crash_kexec_prepare_cpus(int cpu)
204{
205
206
207
208
209
210
211#ifdef CONFIG_PPC64
212 smp_release_cpus();
213#else
214
215#endif
216}
217
218void crash_kexec_secondary(struct pt_regs *regs)
219{
220}
221#endif
222
223
224#if defined(CONFIG_SMP) && defined(CONFIG_PPC64)
225static void __maybe_unused crash_kexec_wait_realmode(int cpu)
226{
227 unsigned int msecs;
228 int i;
229
230 msecs = REAL_MODE_TIMEOUT;
231 for (i=0; i < nr_cpu_ids && msecs > 0; i++) {
232 if (i == cpu)
233 continue;
234
235 while (paca[i].kexec_state < KEXEC_STATE_REAL_MODE) {
236 barrier();
237 if (!cpu_possible(i) || !cpu_online(i) || (msecs <= 0))
238 break;
239 msecs--;
240 mdelay(1);
241 }
242 }
243 mb();
244}
245#else
246static inline void crash_kexec_wait_realmode(int cpu) {}
247#endif
248
249
250
251
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
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
291 for (; i < (CRASH_HANDLER_MAX - 1); i++)
292 crash_shutdown_handles[i] =
293 crash_shutdown_handles[i+1];
294
295
296
297
298 crash_shutdown_handles[i] = NULL;
299 rc = 0;
300 }
301
302 spin_unlock(&crash_handlers_lock);
303 return rc;
304}
305EXPORT_SYMBOL(crash_shutdown_unregister);
306
307void default_machine_crash_shutdown(struct pt_regs *regs)
308{
309 unsigned int i;
310 int (*old_handler)(struct pt_regs *regs);
311
312
313
314
315
316
317
318
319
320
321
322 hard_irq_disable();
323
324
325
326
327
328 crashing_cpu = smp_processor_id();
329
330
331
332
333
334 if (TRAP(regs) == 0x100)
335 mdelay(PRIMARY_TIMEOUT);
336
337 crash_kexec_prepare_cpus(crashing_cpu);
338
339 crash_save_cpu(regs, crashing_cpu);
340
341 time_to_dump = 1;
342
343 crash_kexec_wait_realmode(crashing_cpu);
344
345 machine_kexec_mask_interrupts();
346
347
348
349
350
351 old_handler = __debugger_fault_handler;
352 __debugger_fault_handler = handle_fault;
353 crash_shutdown_cpu = smp_processor_id();
354 for (i = 0; i < CRASH_HANDLER_MAX && crash_shutdown_handles[i]; i++) {
355 if (setjmp(crash_shutdown_buf) == 0) {
356
357
358
359
360
361 asm volatile("sync; isync");
362
363 crash_shutdown_handles[i]();
364 asm volatile("sync; isync");
365 }
366 }
367 crash_shutdown_cpu = -1;
368 __debugger_fault_handler = old_handler;
369
370 if (ppc_md.kexec_cpu_down)
371 ppc_md.kexec_cpu_down(1, 0);
372}
373