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 | ||
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> | |
66b15db6 | 20 | #include <linux/export.h> |
cc532915 | 21 | #include <linux/crash_dump.h> |
cc532915 ME |
22 | #include <linux/delay.h> |
23 | #include <linux/elf.h> | |
24 | #include <linux/elfcore.h> | |
25 | #include <linux/init.h> | |
d6c1a908 | 26 | #include <linux/irq.h> |
cc532915 | 27 | #include <linux/types.h> |
95f72d1e | 28 | #include <linux/memblock.h> |
cc532915 ME |
29 | |
30 | #include <asm/processor.h> | |
31 | #include <asm/machdep.h> | |
c0ce7d08 | 32 | #include <asm/kexec.h> |
cc532915 | 33 | #include <asm/kdump.h> |
d9b2b2a2 | 34 | #include <asm/prom.h> |
cc532915 | 35 | #include <asm/firmware.h> |
f6cc82fc | 36 | #include <asm/smp.h> |
496b010e MN |
37 | #include <asm/system.h> |
38 | #include <asm/setjmp.h> | |
cc532915 ME |
39 | |
40 | #ifdef DEBUG | |
41 | #include <asm/udbg.h> | |
42 | #define DBG(fmt...) udbg_printf(fmt) | |
43 | #else | |
44 | #define DBG(fmt...) | |
45 | #endif | |
46 | ||
47 | /* This keeps a track of which one is crashing cpu. */ | |
48 | int crashing_cpu = -1; | |
c0ce7d08 | 49 | static cpumask_t cpus_in_crash = CPU_MASK_NONE; |
cc532915 | 50 | |
158d5b5e | 51 | #define CRASH_HANDLER_MAX 3 |
496b010e MN |
52 | /* NULL terminated list of shutdown handles */ |
53 | static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX+1]; | |
54 | static DEFINE_SPINLOCK(crash_handlers_lock); | |
55 | ||
07fe0c61 AB |
56 | static unsigned long crash_shutdown_buf[JMP_BUF_LEN]; |
57 | static int crash_shutdown_cpu = -1; | |
58 | ||
59 | static int handle_fault(struct pt_regs *regs) | |
60 | { | |
61 | if (crash_shutdown_cpu == smp_processor_id()) | |
62 | longjmp(crash_shutdown_buf, 1); | |
63 | return 0; | |
64 | } | |
65 | ||
cc532915 | 66 | #ifdef CONFIG_SMP |
cc532915 ME |
67 | |
68 | void crash_ipi_callback(struct pt_regs *regs) | |
69 | { | |
70 | int cpu = smp_processor_id(); | |
71 | ||
cc532915 ME |
72 | if (!cpu_online(cpu)) |
73 | return; | |
74 | ||
d04c56f7 | 75 | hard_irq_disable(); |
104699c0 | 76 | if (!cpumask_test_cpu(cpu, &cpus_in_crash)) |
85916f81 | 77 | crash_save_cpu(regs, cpu); |
104699c0 | 78 | cpumask_set_cpu(cpu, &cpus_in_crash); |
cc532915 | 79 | |
c0ce7d08 DW |
80 | /* |
81 | * Starting the kdump boot. | |
82 | * This barrier is needed to make sure that all CPUs are stopped. | |
c0ce7d08 | 83 | */ |
104699c0 | 84 | while (!cpumask_test_cpu(crashing_cpu, &cpus_in_crash)) |
c0ce7d08 DW |
85 | cpu_relax(); |
86 | ||
87 | if (ppc_md.kexec_cpu_down) | |
88 | ppc_md.kexec_cpu_down(1, 1); | |
b6f35b49 ME |
89 | |
90 | #ifdef CONFIG_PPC64 | |
cc532915 | 91 | kexec_smp_wait(); |
b6f35b49 ME |
92 | #else |
93 | for (;;); /* FIXME */ | |
94 | #endif | |
95 | ||
cc532915 ME |
96 | /* NOTREACHED */ |
97 | } | |
98 | ||
c0ce7d08 | 99 | static void crash_kexec_prepare_cpus(int cpu) |
cc532915 ME |
100 | { |
101 | unsigned int msecs; | |
c0ce7d08 | 102 | unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */ |
07fe0c61 AB |
103 | int tries = 0; |
104 | int (*old_handler)(struct pt_regs *regs); | |
cc532915 | 105 | |
9b00ac06 AB |
106 | printk(KERN_EMERG "Sending IPI to other CPUs\n"); |
107 | ||
cc532915 ME |
108 | crash_send_ipi(crash_ipi_callback); |
109 | smp_wmb(); | |
110 | ||
07fe0c61 | 111 | again: |
cc532915 | 112 | /* |
158d5b5e | 113 | * FIXME: Until we will have the way to stop other CPUs reliably, |
cc532915 | 114 | * the crash CPU will send an IPI and wait for other CPUs to |
c0ce7d08 | 115 | * respond. |
01aaed9d | 116 | * Delay of at least 10 seconds. |
cc532915 | 117 | */ |
01aaed9d | 118 | msecs = 10000; |
104699c0 | 119 | while ((cpumask_weight(&cpus_in_crash) < ncpus) && (--msecs > 0)) { |
c0ce7d08 | 120 | cpu_relax(); |
cc532915 ME |
121 | mdelay(1); |
122 | } | |
123 | ||
124 | /* Would it be better to replace the trap vector here? */ | |
125 | ||
07fe0c61 AB |
126 | if (cpumask_weight(&cpus_in_crash) >= ncpus) { |
127 | printk(KERN_EMERG "IPI complete\n"); | |
128 | return; | |
c0ce7d08 | 129 | } |
9b00ac06 | 130 | |
07fe0c61 AB |
131 | printk(KERN_EMERG "ERROR: %d cpu(s) not responding\n", |
132 | ncpus - cpumask_weight(&cpus_in_crash)); | |
133 | ||
134 | /* | |
135 | * If we have a panic timeout set then we can't wait indefinitely | |
136 | * for someone to activate system reset. We also give up on the | |
137 | * second time through if system reset fail to work. | |
138 | */ | |
139 | if ((panic_timeout > 0) || (tries > 0)) | |
140 | return; | |
141 | ||
142 | /* | |
143 | * A system reset will cause all CPUs to take an 0x100 exception. | |
144 | * The primary CPU returns here via setjmp, and the secondary | |
145 | * CPUs reexecute the crash_kexec_secondary path. | |
146 | */ | |
147 | old_handler = __debugger; | |
148 | __debugger = handle_fault; | |
149 | crash_shutdown_cpu = smp_processor_id(); | |
150 | ||
151 | if (setjmp(crash_shutdown_buf) == 0) { | |
152 | printk(KERN_EMERG "Activate system reset (dumprestart) " | |
153 | "to stop other cpu(s)\n"); | |
154 | ||
155 | /* | |
156 | * A system reset will force all CPUs to execute the | |
157 | * crash code again. We need to reset cpus_in_crash so we | |
158 | * wait for everyone to do this. | |
159 | */ | |
160 | cpus_in_crash = CPU_MASK_NONE; | |
161 | smp_mb(); | |
162 | ||
163 | while (cpumask_weight(&cpus_in_crash) < ncpus) | |
164 | cpu_relax(); | |
165 | } | |
166 | ||
167 | crash_shutdown_cpu = -1; | |
168 | __debugger = old_handler; | |
169 | ||
170 | tries++; | |
171 | goto again; | |
cc532915 | 172 | } |
c0ce7d08 DW |
173 | |
174 | /* | |
9b00ac06 | 175 | * This function will be called by secondary cpus. |
c0ce7d08 DW |
176 | */ |
177 | void crash_kexec_secondary(struct pt_regs *regs) | |
178 | { | |
c0ce7d08 | 179 | unsigned long flags; |
9b00ac06 | 180 | int msecs = 500; |
c0ce7d08 DW |
181 | |
182 | local_irq_save(flags); | |
9b00ac06 AB |
183 | |
184 | /* Wait 500ms for the primary crash CPU to signal its progress */ | |
c0ce7d08 DW |
185 | while (crashing_cpu < 0) { |
186 | if (--msecs < 0) { | |
9b00ac06 | 187 | /* No response, kdump image may not have been loaded */ |
c0ce7d08 DW |
188 | local_irq_restore(flags); |
189 | return; | |
190 | } | |
9b00ac06 | 191 | |
c0ce7d08 DW |
192 | mdelay(1); |
193 | cpu_relax(); | |
194 | } | |
9b00ac06 | 195 | |
c0ce7d08 DW |
196 | crash_ipi_callback(regs); |
197 | } | |
198 | ||
7c7a81b5 | 199 | #else /* ! CONFIG_SMP */ |
7c7a81b5 | 200 | |
c0ce7d08 | 201 | static void crash_kexec_prepare_cpus(int cpu) |
cc532915 ME |
202 | { |
203 | /* | |
204 | * move the secondarys to us so that we can copy | |
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 |
216 | void crash_kexec_secondary(struct pt_regs *regs) |
217 | { | |
c0ce7d08 | 218 | } |
7c7a81b5 | 219 | #endif /* CONFIG_SMP */ |
cc532915 | 220 | |
7707e411 BH |
221 | /* wait for all the CPUs to hit real mode but timeout if they don't come in */ |
222 | #if defined(CONFIG_SMP) && defined(CONFIG_PPC_STD_MMU_64) | |
223 | static void crash_kexec_wait_realmode(int cpu) | |
224 | { | |
225 | unsigned int msecs; | |
226 | int i; | |
227 | ||
228 | msecs = 10000; | |
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 | |
244 | static inline void crash_kexec_wait_realmode(int cpu) {} | |
245 | #endif /* CONFIG_SMP && CONFIG_PPC_STD_MMU_64 */ | |
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 | */ | |
251 | int 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 | } | |
273 | EXPORT_SYMBOL(crash_shutdown_register); | |
274 | ||
275 | int 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 */ | |
289 | for (; crash_shutdown_handles[i]; i++) | |
290 | crash_shutdown_handles[i] = | |
291 | crash_shutdown_handles[i+1]; | |
292 | rc = 0; | |
293 | } | |
294 | ||
295 | spin_unlock(&crash_handlers_lock); | |
296 | return rc; | |
297 | } | |
298 | EXPORT_SYMBOL(crash_shutdown_unregister); | |
299 | ||
cc532915 ME |
300 | void default_machine_crash_shutdown(struct pt_regs *regs) |
301 | { | |
496b010e MN |
302 | unsigned int i; |
303 | int (*old_handler)(struct pt_regs *regs); | |
304 | ||
d6c1a908 | 305 | |
cc532915 ME |
306 | /* |
307 | * This function is only called after the system | |
f18190bd | 308 | * has panicked or is otherwise in a critical state. |
cc532915 ME |
309 | * The minimum amount of code to allow a kexec'd kernel |
310 | * to run successfully needs to happen here. | |
311 | * | |
312 | * In practice this means stopping other cpus in | |
313 | * an SMP system. | |
314 | * The kernel is broken so disable interrupts. | |
315 | */ | |
d04c56f7 | 316 | hard_irq_disable(); |
cc532915 | 317 | |
249ec228 AB |
318 | /* |
319 | * Make a note of crashing cpu. Will be used in machine_kexec | |
320 | * such that another IPI will not be sent. | |
321 | */ | |
322 | crashing_cpu = smp_processor_id(); | |
323 | crash_save_cpu(regs, crashing_cpu); | |
324 | crash_kexec_prepare_cpus(crashing_cpu); | |
104699c0 | 325 | cpumask_set_cpu(crashing_cpu, &cpus_in_crash); |
249ec228 | 326 | crash_kexec_wait_realmode(crashing_cpu); |
249ec228 | 327 | |
c71635d2 | 328 | machine_kexec_mask_interrupts(); |
496b010e MN |
329 | |
330 | /* | |
331 | * Call registered shutdown routines savely. Swap out | |
332 | * __debugger_fault_handler, and replace on exit. | |
333 | */ | |
334 | old_handler = __debugger_fault_handler; | |
335 | __debugger_fault_handler = handle_fault; | |
06440794 | 336 | crash_shutdown_cpu = smp_processor_id(); |
496b010e MN |
337 | for (i = 0; crash_shutdown_handles[i]; i++) { |
338 | if (setjmp(crash_shutdown_buf) == 0) { | |
339 | /* | |
340 | * Insert syncs and delay to ensure | |
341 | * instructions in the dangerous region don't | |
342 | * leak away from this protected region. | |
343 | */ | |
344 | asm volatile("sync; isync"); | |
345 | /* dangerous region */ | |
346 | crash_shutdown_handles[i](); | |
347 | asm volatile("sync; isync"); | |
348 | } | |
d6c1a908 | 349 | } |
06440794 | 350 | crash_shutdown_cpu = -1; |
496b010e | 351 | __debugger_fault_handler = old_handler; |
d6c1a908 | 352 | |
c0ce7d08 DW |
353 | if (ppc_md.kexec_cpu_down) |
354 | ppc_md.kexec_cpu_down(1, 0); | |
cc532915 | 355 | } |