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