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