include/asm-x86/posix_types_32.h: checkpatch cleanups - formatting only
[sfrench/cifs-2.6.git] / arch / powerpc / kernel / crash.c
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>
20 #include <linux/crash_dump.h>
21 #include <linux/delay.h>
22 #include <linux/elf.h>
23 #include <linux/elfcore.h>
24 #include <linux/init.h>
25 #include <linux/irq.h>
26 #include <linux/types.h>
27
28 #include <asm/processor.h>
29 #include <asm/machdep.h>
30 #include <asm/kexec.h>
31 #include <asm/kdump.h>
32 #include <asm/lmb.h>
33 #include <asm/firmware.h>
34 #include <asm/smp.h>
35 #include <asm/system.h>
36 #include <asm/setjmp.h>
37
38 #ifdef DEBUG
39 #include <asm/udbg.h>
40 #define DBG(fmt...) udbg_printf(fmt)
41 #else
42 #define DBG(fmt...)
43 #endif
44
45 /* This keeps a track of which one is crashing cpu. */
46 int crashing_cpu = -1;
47 static cpumask_t cpus_in_crash = CPU_MASK_NONE;
48 cpumask_t cpus_in_sr = CPU_MASK_NONE;
49
50 #define CRASH_HANDLER_MAX 1
51 /* NULL terminated list of shutdown handles */
52 static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX+1];
53 static DEFINE_SPINLOCK(crash_handlers_lock);
54
55 #ifdef CONFIG_SMP
56 static atomic_t enter_on_soft_reset = ATOMIC_INIT(0);
57
58 void crash_ipi_callback(struct pt_regs *regs)
59 {
60         int cpu = smp_processor_id();
61
62         if (!cpu_online(cpu))
63                 return;
64
65         hard_irq_disable();
66         if (!cpu_isset(cpu, cpus_in_crash))
67                 crash_save_cpu(regs, cpu);
68         cpu_set(cpu, cpus_in_crash);
69
70         /*
71          * Entered via soft-reset - could be the kdump
72          * process is invoked using soft-reset or user activated
73          * it if some CPU did not respond to an IPI.
74          * For soft-reset, the secondary CPU can enter this func
75          * twice. 1 - using IPI, and 2. soft-reset.
76          * Tell the kexec CPU that entered via soft-reset and ready
77          * to go down.
78          */
79         if (cpu_isset(cpu, cpus_in_sr)) {
80                 cpu_clear(cpu, cpus_in_sr);
81                 atomic_inc(&enter_on_soft_reset);
82         }
83
84         /*
85          * Starting the kdump boot.
86          * This barrier is needed to make sure that all CPUs are stopped.
87          * If not, soft-reset will be invoked to bring other CPUs.
88          */
89         while (!cpu_isset(crashing_cpu, cpus_in_crash))
90                 cpu_relax();
91
92         if (ppc_md.kexec_cpu_down)
93                 ppc_md.kexec_cpu_down(1, 1);
94
95 #ifdef CONFIG_PPC64
96         kexec_smp_wait();
97 #else
98         for (;;);       /* FIXME */
99 #endif
100
101         /* NOTREACHED */
102 }
103
104 /*
105  * Wait until all CPUs are entered via soft-reset.
106  */
107 static void crash_soft_reset_check(int cpu)
108 {
109         unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
110
111         cpu_clear(cpu, cpus_in_sr);
112         while (atomic_read(&enter_on_soft_reset) != ncpus)
113                 cpu_relax();
114 }
115
116
117 static void crash_kexec_prepare_cpus(int cpu)
118 {
119         unsigned int msecs;
120
121         unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
122
123         crash_send_ipi(crash_ipi_callback);
124         smp_wmb();
125
126         /*
127          * FIXME: Until we will have the way to stop other CPUSs reliabally,
128          * the crash CPU will send an IPI and wait for other CPUs to
129          * respond.
130          * Delay of at least 10 seconds.
131          */
132         printk(KERN_EMERG "Sending IPI to other cpus...\n");
133         msecs = 10000;
134         while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) {
135                 cpu_relax();
136                 mdelay(1);
137         }
138
139         /* Would it be better to replace the trap vector here? */
140
141         /*
142          * FIXME: In case if we do not get all CPUs, one possibility: ask the
143          * user to do soft reset such that we get all.
144          * Soft-reset will be used until better mechanism is implemented.
145          */
146         if (cpus_weight(cpus_in_crash) < ncpus) {
147                 printk(KERN_EMERG "done waiting: %d cpu(s) not responding\n",
148                         ncpus - cpus_weight(cpus_in_crash));
149                 printk(KERN_EMERG "Activate soft-reset to stop other cpu(s)\n");
150                 cpus_in_sr = CPU_MASK_NONE;
151                 atomic_set(&enter_on_soft_reset, 0);
152                 while (cpus_weight(cpus_in_crash) < ncpus)
153                         cpu_relax();
154         }
155         /*
156          * Make sure all CPUs are entered via soft-reset if the kdump is
157          * invoked using soft-reset.
158          */
159         if (cpu_isset(cpu, cpus_in_sr))
160                 crash_soft_reset_check(cpu);
161         /* Leave the IPI callback set */
162 }
163
164 /*
165  * This function will be called by secondary cpus or by kexec cpu
166  * if soft-reset is activated to stop some CPUs.
167  */
168 void crash_kexec_secondary(struct pt_regs *regs)
169 {
170         int cpu = smp_processor_id();
171         unsigned long flags;
172         int msecs = 5;
173
174         local_irq_save(flags);
175         /* Wait 5ms if the kexec CPU is not entered yet. */
176         while (crashing_cpu < 0) {
177                 if (--msecs < 0) {
178                         /*
179                          * Either kdump image is not loaded or
180                          * kdump process is not started - Probably xmon
181                          * exited using 'x'(exit and recover) or
182                          * kexec_should_crash() failed for all running tasks.
183                          */
184                         cpu_clear(cpu, cpus_in_sr);
185                         local_irq_restore(flags);
186                         return;
187                 }
188                 mdelay(1);
189                 cpu_relax();
190         }
191         if (cpu == crashing_cpu) {
192                 /*
193                  * Panic CPU will enter this func only via soft-reset.
194                  * Wait until all secondary CPUs entered and
195                  * then start kexec boot.
196                  */
197                 crash_soft_reset_check(cpu);
198                 cpu_set(crashing_cpu, cpus_in_crash);
199                 if (ppc_md.kexec_cpu_down)
200                         ppc_md.kexec_cpu_down(1, 0);
201                 machine_kexec(kexec_crash_image);
202                 /* NOTREACHED */
203         }
204         crash_ipi_callback(regs);
205 }
206
207 #else
208 static void crash_kexec_prepare_cpus(int cpu)
209 {
210         /*
211          * move the secondarys to us so that we can copy
212          * the new kernel 0-0x100 safely
213          *
214          * do this if kexec in setup.c ?
215          */
216 #ifdef CONFIG_PPC64
217         smp_release_cpus();
218 #else
219         /* FIXME */
220 #endif
221 }
222
223 void crash_kexec_secondary(struct pt_regs *regs)
224 {
225         cpus_in_sr = CPU_MASK_NONE;
226 }
227 #endif
228 #ifdef CONFIG_SPU_BASE
229
230 #include <asm/spu.h>
231 #include <asm/spu_priv1.h>
232
233 struct crash_spu_info {
234         struct spu *spu;
235         u32 saved_spu_runcntl_RW;
236         u32 saved_spu_status_R;
237         u32 saved_spu_npc_RW;
238         u64 saved_mfc_sr1_RW;
239         u64 saved_mfc_dar;
240         u64 saved_mfc_dsisr;
241 };
242
243 #define CRASH_NUM_SPUS  16      /* Enough for current hardware */
244 static struct crash_spu_info crash_spu_info[CRASH_NUM_SPUS];
245
246 static void crash_kexec_stop_spus(void)
247 {
248         struct spu *spu;
249         int i;
250         u64 tmp;
251
252         for (i = 0; i < CRASH_NUM_SPUS; i++) {
253                 if (!crash_spu_info[i].spu)
254                         continue;
255
256                 spu = crash_spu_info[i].spu;
257
258                 crash_spu_info[i].saved_spu_runcntl_RW =
259                         in_be32(&spu->problem->spu_runcntl_RW);
260                 crash_spu_info[i].saved_spu_status_R =
261                         in_be32(&spu->problem->spu_status_R);
262                 crash_spu_info[i].saved_spu_npc_RW =
263                         in_be32(&spu->problem->spu_npc_RW);
264
265                 crash_spu_info[i].saved_mfc_dar    = spu_mfc_dar_get(spu);
266                 crash_spu_info[i].saved_mfc_dsisr  = spu_mfc_dsisr_get(spu);
267                 tmp = spu_mfc_sr1_get(spu);
268                 crash_spu_info[i].saved_mfc_sr1_RW = tmp;
269
270                 tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
271                 spu_mfc_sr1_set(spu, tmp);
272
273                 __delay(200);
274         }
275 }
276
277 void crash_register_spus(struct list_head *list)
278 {
279         struct spu *spu;
280
281         list_for_each_entry(spu, list, full_list) {
282                 if (WARN_ON(spu->number >= CRASH_NUM_SPUS))
283                         continue;
284
285                 crash_spu_info[spu->number].spu = spu;
286         }
287 }
288
289 #else
290 static inline void crash_kexec_stop_spus(void)
291 {
292 }
293 #endif /* CONFIG_SPU_BASE */
294
295 /*
296  * Register a function to be called on shutdown.  Only use this if you
297  * can't reset your device in the second kernel.
298  */
299 int crash_shutdown_register(crash_shutdown_t handler)
300 {
301         unsigned int i, rc;
302
303         spin_lock(&crash_handlers_lock);
304         for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
305                 if (!crash_shutdown_handles[i]) {
306                         /* Insert handle at first empty entry */
307                         crash_shutdown_handles[i] = handler;
308                         rc = 0;
309                         break;
310                 }
311
312         if (i == CRASH_HANDLER_MAX) {
313                 printk(KERN_ERR "Crash shutdown handles full, "
314                        "not registered.\n");
315                 rc = 1;
316         }
317
318         spin_unlock(&crash_handlers_lock);
319         return rc;
320 }
321 EXPORT_SYMBOL(crash_shutdown_register);
322
323 int crash_shutdown_unregister(crash_shutdown_t handler)
324 {
325         unsigned int i, rc;
326
327         spin_lock(&crash_handlers_lock);
328         for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
329                 if (crash_shutdown_handles[i] == handler)
330                         break;
331
332         if (i == CRASH_HANDLER_MAX) {
333                 printk(KERN_ERR "Crash shutdown handle not found\n");
334                 rc = 1;
335         } else {
336                 /* Shift handles down */
337                 for (; crash_shutdown_handles[i]; i++)
338                         crash_shutdown_handles[i] =
339                                 crash_shutdown_handles[i+1];
340                 rc = 0;
341         }
342
343         spin_unlock(&crash_handlers_lock);
344         return rc;
345 }
346 EXPORT_SYMBOL(crash_shutdown_unregister);
347
348 static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
349
350 static int handle_fault(struct pt_regs *regs)
351 {
352         longjmp(crash_shutdown_buf, 1);
353         return 0;
354 }
355
356 void default_machine_crash_shutdown(struct pt_regs *regs)
357 {
358         unsigned int i;
359         int (*old_handler)(struct pt_regs *regs);
360
361
362         /*
363          * This function is only called after the system
364          * has panicked or is otherwise in a critical state.
365          * The minimum amount of code to allow a kexec'd kernel
366          * to run successfully needs to happen here.
367          *
368          * In practice this means stopping other cpus in
369          * an SMP system.
370          * The kernel is broken so disable interrupts.
371          */
372         hard_irq_disable();
373
374         for_each_irq(i) {
375                 struct irq_desc *desc = irq_desc + i;
376
377                 if (desc->status & IRQ_INPROGRESS)
378                         desc->chip->eoi(i);
379
380                 if (!(desc->status & IRQ_DISABLED))
381                         desc->chip->disable(i);
382         }
383
384         /*
385          * Call registered shutdown routines savely.  Swap out
386          * __debugger_fault_handler, and replace on exit.
387          */
388         old_handler = __debugger_fault_handler;
389         __debugger_fault_handler = handle_fault;
390         for (i = 0; crash_shutdown_handles[i]; i++) {
391                 if (setjmp(crash_shutdown_buf) == 0) {
392                         /*
393                          * Insert syncs and delay to ensure
394                          * instructions in the dangerous region don't
395                          * leak away from this protected region.
396                          */
397                         asm volatile("sync; isync");
398                         /* dangerous region */
399                         crash_shutdown_handles[i]();
400                         asm volatile("sync; isync");
401                 }
402         }
403         __debugger_fault_handler = old_handler;
404
405         /*
406          * Make a note of crashing cpu. Will be used in machine_kexec
407          * such that another IPI will not be sent.
408          */
409         crashing_cpu = smp_processor_id();
410         crash_save_cpu(regs, crashing_cpu);
411         crash_kexec_prepare_cpus(crashing_cpu);
412         cpu_set(crashing_cpu, cpus_in_crash);
413         crash_kexec_stop_spus();
414         if (ppc_md.kexec_cpu_down)
415                 ppc_md.kexec_cpu_down(1, 0);
416 }