blob: f04c18e08b8b6dcf9f52e422db1c4aa2b0a2854a [file] [log] [blame]
Michael Ellermancc532912005-12-04 18:39:43 +11001/*
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>
Michael Ellermancc532912005-12-04 18:39:43 +110021#include <linux/delay.h>
22#include <linux/elf.h>
23#include <linux/elfcore.h>
24#include <linux/init.h>
Michael Ellermand6c1a902006-04-04 13:43:01 +020025#include <linux/irq.h>
Michael Ellermancc532912005-12-04 18:39:43 +110026#include <linux/types.h>
David Wilderc0ce7d02006-06-23 15:29:34 -070027#include <linux/irq.h>
Michael Ellermancc532912005-12-04 18:39:43 +110028
29#include <asm/processor.h>
30#include <asm/machdep.h>
David Wilderc0ce7d02006-06-23 15:29:34 -070031#include <asm/kexec.h>
Michael Ellermancc532912005-12-04 18:39:43 +110032#include <asm/kdump.h>
33#include <asm/lmb.h>
34#include <asm/firmware.h>
Haren Mynenif6cc82f2006-01-10 19:25:25 -080035#include <asm/smp.h>
Michael Ellermancc532912005-12-04 18:39:43 +110036
37#ifdef DEBUG
38#include <asm/udbg.h>
39#define DBG(fmt...) udbg_printf(fmt)
40#else
41#define DBG(fmt...)
42#endif
43
44/* This keeps a track of which one is crashing cpu. */
45int crashing_cpu = -1;
David Wilderc0ce7d02006-06-23 15:29:34 -070046static cpumask_t cpus_in_crash = CPU_MASK_NONE;
Michael Ellermanb6f35b42006-07-05 14:39:43 +100047cpumask_t cpus_in_sr = CPU_MASK_NONE;
Michael Ellermancc532912005-12-04 18:39:43 +110048
49static u32 *append_elf_note(u32 *buf, char *name, unsigned type, void *data,
50 size_t data_len)
51{
52 struct elf_note note;
53
54 note.n_namesz = strlen(name) + 1;
55 note.n_descsz = data_len;
56 note.n_type = type;
57 memcpy(buf, &note, sizeof(note));
58 buf += (sizeof(note) +3)/4;
59 memcpy(buf, name, note.n_namesz);
60 buf += (note.n_namesz + 3)/4;
61 memcpy(buf, data, note.n_descsz);
62 buf += (note.n_descsz + 3)/4;
63
64 return buf;
65}
66
67static void final_note(u32 *buf)
68{
69 struct elf_note note;
70
71 note.n_namesz = 0;
72 note.n_descsz = 0;
73 note.n_type = 0;
74 memcpy(buf, &note, sizeof(note));
75}
76
77static void crash_save_this_cpu(struct pt_regs *regs, int cpu)
78{
79 struct elf_prstatus prstatus;
80 u32 *buf;
81
82 if ((cpu < 0) || (cpu >= NR_CPUS))
83 return;
84
85 /* Using ELF notes here is opportunistic.
86 * I need a well defined structure format
87 * for the data I pass, and I need tags
88 * on the data to indicate what information I have
89 * squirrelled away. ELF notes happen to provide
90 * all of that that no need to invent something new.
91 */
Haren Myneni8385a6a2006-01-13 19:15:36 -080092 buf = (u32*)per_cpu_ptr(crash_notes, cpu);
93 if (!buf)
94 return;
95
Michael Ellermancc532912005-12-04 18:39:43 +110096 memset(&prstatus, 0, sizeof(prstatus));
97 prstatus.pr_pid = current->pid;
98 elf_core_copy_regs(&prstatus.pr_reg, regs);
99 buf = append_elf_note(buf, "CORE", NT_PRSTATUS, &prstatus,
100 sizeof(prstatus));
101 final_note(buf);
102}
103
Michael Ellermancc532912005-12-04 18:39:43 +1100104#ifdef CONFIG_SMP
David Wilderc0ce7d02006-06-23 15:29:34 -0700105static atomic_t enter_on_soft_reset = ATOMIC_INIT(0);
Michael Ellermancc532912005-12-04 18:39:43 +1100106
107void crash_ipi_callback(struct pt_regs *regs)
108{
109 int cpu = smp_processor_id();
110
Michael Ellermancc532912005-12-04 18:39:43 +1100111 if (!cpu_online(cpu))
112 return;
113
David Wilderc0ce7d02006-06-23 15:29:34 -0700114 local_irq_disable();
115 if (!cpu_isset(cpu, cpus_in_crash))
116 crash_save_this_cpu(regs, cpu);
117 cpu_set(cpu, cpus_in_crash);
118
119 /*
120 * Entered via soft-reset - could be the kdump
121 * process is invoked using soft-reset or user activated
122 * it if some CPU did not respond to an IPI.
123 * For soft-reset, the secondary CPU can enter this func
124 * twice. 1 - using IPI, and 2. soft-reset.
125 * Tell the kexec CPU that entered via soft-reset and ready
126 * to go down.
127 */
128 if (cpu_isset(cpu, cpus_in_sr)) {
129 cpu_clear(cpu, cpus_in_sr);
130 atomic_inc(&enter_on_soft_reset);
131 }
132
133 /*
134 * Starting the kdump boot.
135 * This barrier is needed to make sure that all CPUs are stopped.
136 * If not, soft-reset will be invoked to bring other CPUs.
137 */
138 while (!cpu_isset(crashing_cpu, cpus_in_crash))
139 cpu_relax();
140
Michael Ellermancc532912005-12-04 18:39:43 +1100141 if (ppc_md.kexec_cpu_down)
142 ppc_md.kexec_cpu_down(1, 1);
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000143
144#ifdef CONFIG_PPC64
Michael Ellermancc532912005-12-04 18:39:43 +1100145 kexec_smp_wait();
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000146#else
147 for (;;); /* FIXME */
148#endif
149
Michael Ellermancc532912005-12-04 18:39:43 +1100150 /* NOTREACHED */
151}
152
David Wilderc0ce7d02006-06-23 15:29:34 -0700153/*
154 * Wait until all CPUs are entered via soft-reset.
155 */
156static void crash_soft_reset_check(int cpu)
157{
158 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
159
160 cpu_clear(cpu, cpus_in_sr);
161 while (atomic_read(&enter_on_soft_reset) != ncpus)
162 cpu_relax();
163}
164
165
166static void crash_kexec_prepare_cpus(int cpu)
Michael Ellermancc532912005-12-04 18:39:43 +1100167{
168 unsigned int msecs;
169
David Wilderc0ce7d02006-06-23 15:29:34 -0700170 unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
Michael Ellermancc532912005-12-04 18:39:43 +1100171
172 crash_send_ipi(crash_ipi_callback);
173 smp_wmb();
174
175 /*
176 * FIXME: Until we will have the way to stop other CPUSs reliabally,
177 * the crash CPU will send an IPI and wait for other CPUs to
David Wilderc0ce7d02006-06-23 15:29:34 -0700178 * respond.
Haren Myneni01aaed92006-02-07 15:47:03 -0800179 * Delay of at least 10 seconds.
Michael Ellermancc532912005-12-04 18:39:43 +1100180 */
David Wilderc0ce7d02006-06-23 15:29:34 -0700181 printk(KERN_EMERG "Sending IPI to other cpus...\n");
Haren Myneni01aaed92006-02-07 15:47:03 -0800182 msecs = 10000;
David Wilderc0ce7d02006-06-23 15:29:34 -0700183 while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) {
184 cpu_relax();
Michael Ellermancc532912005-12-04 18:39:43 +1100185 mdelay(1);
186 }
187
188 /* Would it be better to replace the trap vector here? */
189
190 /*
191 * FIXME: In case if we do not get all CPUs, one possibility: ask the
192 * user to do soft reset such that we get all.
David Wilderc0ce7d02006-06-23 15:29:34 -0700193 * Soft-reset will be used until better mechanism is implemented.
Michael Ellermancc532912005-12-04 18:39:43 +1100194 */
David Wilderc0ce7d02006-06-23 15:29:34 -0700195 if (cpus_weight(cpus_in_crash) < ncpus) {
196 printk(KERN_EMERG "done waiting: %d cpu(s) not responding\n",
197 ncpus - cpus_weight(cpus_in_crash));
198 printk(KERN_EMERG "Activate soft-reset to stop other cpu(s)\n");
199 cpus_in_sr = CPU_MASK_NONE;
200 atomic_set(&enter_on_soft_reset, 0);
201 while (cpus_weight(cpus_in_crash) < ncpus)
202 cpu_relax();
203 }
204 /*
205 * Make sure all CPUs are entered via soft-reset if the kdump is
206 * invoked using soft-reset.
207 */
208 if (cpu_isset(cpu, cpus_in_sr))
209 crash_soft_reset_check(cpu);
Michael Ellermancc532912005-12-04 18:39:43 +1100210 /* Leave the IPI callback set */
211}
David Wilderc0ce7d02006-06-23 15:29:34 -0700212
213/*
214 * This function will be called by secondary cpus or by kexec cpu
215 * if soft-reset is activated to stop some CPUs.
216 */
217void crash_kexec_secondary(struct pt_regs *regs)
218{
219 int cpu = smp_processor_id();
220 unsigned long flags;
221 int msecs = 5;
222
223 local_irq_save(flags);
224 /* Wait 5ms if the kexec CPU is not entered yet. */
225 while (crashing_cpu < 0) {
226 if (--msecs < 0) {
227 /*
228 * Either kdump image is not loaded or
229 * kdump process is not started - Probably xmon
230 * exited using 'x'(exit and recover) or
231 * kexec_should_crash() failed for all running tasks.
232 */
233 cpu_clear(cpu, cpus_in_sr);
234 local_irq_restore(flags);
235 return;
236 }
237 mdelay(1);
238 cpu_relax();
239 }
240 if (cpu == crashing_cpu) {
241 /*
242 * Panic CPU will enter this func only via soft-reset.
243 * Wait until all secondary CPUs entered and
244 * then start kexec boot.
245 */
246 crash_soft_reset_check(cpu);
247 cpu_set(crashing_cpu, cpus_in_crash);
248 if (ppc_md.kexec_cpu_down)
249 ppc_md.kexec_cpu_down(1, 0);
250 machine_kexec(kexec_crash_image);
251 /* NOTREACHED */
252 }
253 crash_ipi_callback(regs);
254}
255
Michael Ellermancc532912005-12-04 18:39:43 +1100256#else
David Wilderc0ce7d02006-06-23 15:29:34 -0700257static void crash_kexec_prepare_cpus(int cpu)
Michael Ellermancc532912005-12-04 18:39:43 +1100258{
259 /*
260 * move the secondarys to us so that we can copy
261 * the new kernel 0-0x100 safely
262 *
263 * do this if kexec in setup.c ?
264 */
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000265#ifdef CONFIG_PPC64
Michael Ellermancc532912005-12-04 18:39:43 +1100266 smp_release_cpus();
Michael Ellermanb6f35b42006-07-05 14:39:43 +1000267#else
268 /* FIXME */
269#endif
Michael Ellermancc532912005-12-04 18:39:43 +1100270}
271
David Wilderc0ce7d02006-06-23 15:29:34 -0700272void crash_kexec_secondary(struct pt_regs *regs)
273{
274 cpus_in_sr = CPU_MASK_NONE;
275}
Michael Ellermancc532912005-12-04 18:39:43 +1100276#endif
277
278void default_machine_crash_shutdown(struct pt_regs *regs)
279{
Michael Ellermand6c1a902006-04-04 13:43:01 +0200280 unsigned int irq;
281
Michael Ellermancc532912005-12-04 18:39:43 +1100282 /*
283 * This function is only called after the system
Lee Revellf18190b2006-06-26 18:30:00 +0200284 * has panicked or is otherwise in a critical state.
Michael Ellermancc532912005-12-04 18:39:43 +1100285 * The minimum amount of code to allow a kexec'd kernel
286 * to run successfully needs to happen here.
287 *
288 * In practice this means stopping other cpus in
289 * an SMP system.
290 * The kernel is broken so disable interrupts.
291 */
292 local_irq_disable();
293
Michael Ellermand6c1a902006-04-04 13:43:01 +0200294 for_each_irq(irq) {
Ingo Molnara8553ac2006-06-29 02:24:38 -0700295 struct irq_desc *desc = irq_desc + irq;
Michael Ellermand6c1a902006-04-04 13:43:01 +0200296
297 if (desc->status & IRQ_INPROGRESS)
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700298 desc->chip->end(irq);
Michael Ellermand6c1a902006-04-04 13:43:01 +0200299
300 if (!(desc->status & IRQ_DISABLED))
Ingo Molnard1bef4e2006-06-29 02:24:36 -0700301 desc->chip->disable(irq);
Michael Ellermand6c1a902006-04-04 13:43:01 +0200302 }
303
Michael Ellermancc532912005-12-04 18:39:43 +1100304 /*
305 * Make a note of crashing cpu. Will be used in machine_kexec
306 * such that another IPI will not be sent.
307 */
308 crashing_cpu = smp_processor_id();
Haren Myneni8385a6a2006-01-13 19:15:36 -0800309 crash_save_this_cpu(regs, crashing_cpu);
David Wilderc0ce7d02006-06-23 15:29:34 -0700310 crash_kexec_prepare_cpus(crashing_cpu);
311 cpu_set(crashing_cpu, cpus_in_crash);
312 if (ppc_md.kexec_cpu_down)
313 ppc_md.kexec_cpu_down(1, 0);
Michael Ellermancc532912005-12-04 18:39:43 +1100314}