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