| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 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> | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 21 | #include <linux/delay.h> | 
 | 22 | #include <linux/elf.h> | 
 | 23 | #include <linux/elfcore.h> | 
 | 24 | #include <linux/init.h> | 
| Michael Ellerman | d6c1a90 | 2006-04-04 13:43:01 +0200 | [diff] [blame] | 25 | #include <linux/irq.h> | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 26 | #include <linux/types.h> | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 27 | #include <linux/irq.h> | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 28 |  | 
 | 29 | #include <asm/processor.h> | 
 | 30 | #include <asm/machdep.h> | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 31 | #include <asm/kexec.h> | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 32 | #include <asm/kdump.h> | 
 | 33 | #include <asm/lmb.h> | 
 | 34 | #include <asm/firmware.h> | 
| Haren Myneni | f6cc82f | 2006-01-10 19:25:25 -0800 | [diff] [blame] | 35 | #include <asm/smp.h> | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 36 |  | 
 | 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. */ | 
 | 45 | int crashing_cpu = -1; | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 46 | static cpumask_t cpus_in_crash = CPU_MASK_NONE; | 
| Michael Ellerman | b6f35b4 | 2006-07-05 14:39:43 +1000 | [diff] [blame] | 47 | cpumask_t cpus_in_sr = CPU_MASK_NONE; | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 48 |  | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 49 | #ifdef CONFIG_SMP | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 50 | static atomic_t enter_on_soft_reset = ATOMIC_INIT(0); | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 51 |  | 
 | 52 | void crash_ipi_callback(struct pt_regs *regs) | 
 | 53 | { | 
 | 54 | 	int cpu = smp_processor_id(); | 
 | 55 |  | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 56 | 	if (!cpu_online(cpu)) | 
 | 57 | 		return; | 
 | 58 |  | 
| Paul Mackerras | d04c56f | 2006-10-04 16:47:49 +1000 | [diff] [blame] | 59 | 	hard_irq_disable(); | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 60 | 	if (!cpu_isset(cpu, cpus_in_crash)) | 
| Magnus Damm | 85916f8 | 2006-12-06 20:40:41 -0800 | [diff] [blame] | 61 | 		crash_save_cpu(regs, cpu); | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 62 | 	cpu_set(cpu, cpus_in_crash); | 
 | 63 |  | 
 | 64 | 	/* | 
 | 65 | 	 * Entered via soft-reset - could be the kdump | 
 | 66 | 	 * process is invoked using soft-reset or user activated | 
 | 67 | 	 * it if some CPU did not respond to an IPI. | 
 | 68 | 	 * For soft-reset, the secondary CPU can enter this func | 
 | 69 | 	 * twice. 1 - using IPI, and 2. soft-reset. | 
 | 70 | 	 * Tell the kexec CPU that entered via soft-reset and ready | 
 | 71 | 	 * to go down. | 
 | 72 | 	 */ | 
 | 73 | 	if (cpu_isset(cpu, cpus_in_sr)) { | 
 | 74 | 		cpu_clear(cpu, cpus_in_sr); | 
 | 75 | 		atomic_inc(&enter_on_soft_reset); | 
 | 76 | 	} | 
 | 77 |  | 
 | 78 | 	/* | 
 | 79 | 	 * Starting the kdump boot. | 
 | 80 | 	 * This barrier is needed to make sure that all CPUs are stopped. | 
 | 81 | 	 * If not, soft-reset will be invoked to bring other CPUs. | 
 | 82 | 	 */ | 
 | 83 | 	while (!cpu_isset(crashing_cpu, cpus_in_crash)) | 
 | 84 | 		cpu_relax(); | 
 | 85 |  | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 86 | 	if (ppc_md.kexec_cpu_down) | 
 | 87 | 		ppc_md.kexec_cpu_down(1, 1); | 
| Michael Ellerman | b6f35b4 | 2006-07-05 14:39:43 +1000 | [diff] [blame] | 88 |  | 
 | 89 | #ifdef CONFIG_PPC64 | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 90 | 	kexec_smp_wait(); | 
| Michael Ellerman | b6f35b4 | 2006-07-05 14:39:43 +1000 | [diff] [blame] | 91 | #else | 
 | 92 | 	for (;;);	/* FIXME */ | 
 | 93 | #endif | 
 | 94 |  | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 95 | 	/* NOTREACHED */ | 
 | 96 | } | 
 | 97 |  | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 98 | /* | 
 | 99 |  * Wait until all CPUs are entered via soft-reset. | 
 | 100 |  */ | 
 | 101 | static void crash_soft_reset_check(int cpu) | 
 | 102 | { | 
 | 103 | 	unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */ | 
 | 104 |  | 
 | 105 | 	cpu_clear(cpu, cpus_in_sr); | 
 | 106 | 	while (atomic_read(&enter_on_soft_reset) != ncpus) | 
 | 107 | 		cpu_relax(); | 
 | 108 | } | 
 | 109 |  | 
 | 110 |  | 
 | 111 | static void crash_kexec_prepare_cpus(int cpu) | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 112 | { | 
 | 113 | 	unsigned int msecs; | 
 | 114 |  | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 115 | 	unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */ | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 116 |  | 
 | 117 | 	crash_send_ipi(crash_ipi_callback); | 
 | 118 | 	smp_wmb(); | 
 | 119 |  | 
 | 120 | 	/* | 
 | 121 | 	 * FIXME: Until we will have the way to stop other CPUSs reliabally, | 
 | 122 | 	 * the crash CPU will send an IPI and wait for other CPUs to | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 123 | 	 * respond. | 
| Haren Myneni | 01aaed9 | 2006-02-07 15:47:03 -0800 | [diff] [blame] | 124 | 	 * Delay of at least 10 seconds. | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 125 | 	 */ | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 126 | 	printk(KERN_EMERG "Sending IPI to other cpus...\n"); | 
| Haren Myneni | 01aaed9 | 2006-02-07 15:47:03 -0800 | [diff] [blame] | 127 | 	msecs = 10000; | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 128 | 	while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) { | 
 | 129 | 		cpu_relax(); | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 130 | 		mdelay(1); | 
 | 131 | 	} | 
 | 132 |  | 
 | 133 | 	/* Would it be better to replace the trap vector here? */ | 
 | 134 |  | 
 | 135 | 	/* | 
 | 136 | 	 * FIXME: In case if we do not get all CPUs, one possibility: ask the | 
 | 137 | 	 * user to do soft reset such that we get all. | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 138 | 	 * Soft-reset will be used until better mechanism is implemented. | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 139 | 	 */ | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 140 | 	if (cpus_weight(cpus_in_crash) < ncpus) { | 
 | 141 | 		printk(KERN_EMERG "done waiting: %d cpu(s) not responding\n", | 
 | 142 | 			ncpus - cpus_weight(cpus_in_crash)); | 
 | 143 | 		printk(KERN_EMERG "Activate soft-reset to stop other cpu(s)\n"); | 
 | 144 | 		cpus_in_sr = CPU_MASK_NONE; | 
 | 145 | 		atomic_set(&enter_on_soft_reset, 0); | 
 | 146 | 		while (cpus_weight(cpus_in_crash) < ncpus) | 
 | 147 | 			cpu_relax(); | 
 | 148 | 	} | 
 | 149 | 	/* | 
 | 150 | 	 * Make sure all CPUs are entered via soft-reset if the kdump is | 
 | 151 | 	 * invoked using soft-reset. | 
 | 152 | 	 */ | 
 | 153 | 	if (cpu_isset(cpu, cpus_in_sr)) | 
 | 154 | 		crash_soft_reset_check(cpu); | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 155 | 	/* Leave the IPI callback set */ | 
 | 156 | } | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 157 |  | 
 | 158 | /* | 
 | 159 |  * This function will be called by secondary cpus or by kexec cpu | 
 | 160 |  * if soft-reset is activated to stop some CPUs. | 
 | 161 |  */ | 
 | 162 | void crash_kexec_secondary(struct pt_regs *regs) | 
 | 163 | { | 
 | 164 | 	int cpu = smp_processor_id(); | 
 | 165 | 	unsigned long flags; | 
 | 166 | 	int msecs = 5; | 
 | 167 |  | 
 | 168 | 	local_irq_save(flags); | 
 | 169 | 	/* Wait 5ms if the kexec CPU is not entered yet. */ | 
 | 170 | 	while (crashing_cpu < 0) { | 
 | 171 | 		if (--msecs < 0) { | 
 | 172 | 			/* | 
 | 173 | 			 * Either kdump image is not loaded or | 
 | 174 | 			 * kdump process is not started - Probably xmon | 
 | 175 | 			 * exited using 'x'(exit and recover) or | 
 | 176 | 			 * kexec_should_crash() failed for all running tasks. | 
 | 177 | 			 */ | 
 | 178 | 			cpu_clear(cpu, cpus_in_sr); | 
 | 179 | 			local_irq_restore(flags); | 
 | 180 | 			return; | 
 | 181 | 		} | 
 | 182 | 		mdelay(1); | 
 | 183 | 		cpu_relax(); | 
 | 184 | 	} | 
 | 185 | 	if (cpu == crashing_cpu) { | 
 | 186 | 		/* | 
 | 187 | 		 * Panic CPU will enter this func only via soft-reset. | 
 | 188 | 		 * Wait until all secondary CPUs entered and | 
 | 189 | 		 * then start kexec boot. | 
 | 190 | 		 */ | 
 | 191 | 		crash_soft_reset_check(cpu); | 
 | 192 | 		cpu_set(crashing_cpu, cpus_in_crash); | 
 | 193 | 		if (ppc_md.kexec_cpu_down) | 
 | 194 | 			ppc_md.kexec_cpu_down(1, 0); | 
 | 195 | 		machine_kexec(kexec_crash_image); | 
 | 196 | 		/* NOTREACHED */ | 
 | 197 | 	} | 
 | 198 | 	crash_ipi_callback(regs); | 
 | 199 | } | 
 | 200 |  | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 201 | #else | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 202 | static void crash_kexec_prepare_cpus(int cpu) | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 203 | { | 
 | 204 | 	/* | 
 | 205 | 	 * move the secondarys to us so that we can copy | 
 | 206 | 	 * the new kernel 0-0x100 safely | 
 | 207 | 	 * | 
 | 208 | 	 * do this if kexec in setup.c ? | 
 | 209 | 	 */ | 
| Michael Ellerman | b6f35b4 | 2006-07-05 14:39:43 +1000 | [diff] [blame] | 210 | #ifdef CONFIG_PPC64 | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 211 | 	smp_release_cpus(); | 
| Michael Ellerman | b6f35b4 | 2006-07-05 14:39:43 +1000 | [diff] [blame] | 212 | #else | 
 | 213 | 	/* FIXME */ | 
 | 214 | #endif | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 215 | } | 
 | 216 |  | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 217 | void crash_kexec_secondary(struct pt_regs *regs) | 
 | 218 | { | 
 | 219 | 	cpus_in_sr = CPU_MASK_NONE; | 
 | 220 | } | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 221 | #endif | 
 | 222 |  | 
 | 223 | void default_machine_crash_shutdown(struct pt_regs *regs) | 
 | 224 | { | 
| Michael Ellerman | d6c1a90 | 2006-04-04 13:43:01 +0200 | [diff] [blame] | 225 | 	unsigned int irq; | 
 | 226 |  | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 227 | 	/* | 
 | 228 | 	 * This function is only called after the system | 
| Lee Revell | f18190b | 2006-06-26 18:30:00 +0200 | [diff] [blame] | 229 | 	 * has panicked or is otherwise in a critical state. | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 230 | 	 * The minimum amount of code to allow a kexec'd kernel | 
 | 231 | 	 * to run successfully needs to happen here. | 
 | 232 | 	 * | 
 | 233 | 	 * In practice this means stopping other cpus in | 
 | 234 | 	 * an SMP system. | 
 | 235 | 	 * The kernel is broken so disable interrupts. | 
 | 236 | 	 */ | 
| Paul Mackerras | d04c56f | 2006-10-04 16:47:49 +1000 | [diff] [blame] | 237 | 	hard_irq_disable(); | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 238 |  | 
| Michael Ellerman | d6c1a90 | 2006-04-04 13:43:01 +0200 | [diff] [blame] | 239 | 	for_each_irq(irq) { | 
| Ingo Molnar | a8553ac | 2006-06-29 02:24:38 -0700 | [diff] [blame] | 240 | 		struct irq_desc *desc = irq_desc + irq; | 
| Michael Ellerman | d6c1a90 | 2006-04-04 13:43:01 +0200 | [diff] [blame] | 241 |  | 
 | 242 | 		if (desc->status & IRQ_INPROGRESS) | 
| Mohan Kumar M | 2e8e8da | 2006-09-12 17:48:21 +0530 | [diff] [blame] | 243 | 			desc->chip->eoi(irq); | 
| Michael Ellerman | d6c1a90 | 2006-04-04 13:43:01 +0200 | [diff] [blame] | 244 |  | 
 | 245 | 		if (!(desc->status & IRQ_DISABLED)) | 
| Ingo Molnar | d1bef4e | 2006-06-29 02:24:36 -0700 | [diff] [blame] | 246 | 			desc->chip->disable(irq); | 
| Michael Ellerman | d6c1a90 | 2006-04-04 13:43:01 +0200 | [diff] [blame] | 247 | 	} | 
 | 248 |  | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 249 | 	/* | 
 | 250 | 	 * Make a note of crashing cpu. Will be used in machine_kexec | 
 | 251 | 	 * such that another IPI will not be sent. | 
 | 252 | 	 */ | 
 | 253 | 	crashing_cpu = smp_processor_id(); | 
| Magnus Damm | 85916f8 | 2006-12-06 20:40:41 -0800 | [diff] [blame] | 254 | 	crash_save_cpu(regs, crashing_cpu); | 
| David Wilder | c0ce7d0 | 2006-06-23 15:29:34 -0700 | [diff] [blame] | 255 | 	crash_kexec_prepare_cpus(crashing_cpu); | 
 | 256 | 	cpu_set(crashing_cpu, cpus_in_crash); | 
 | 257 | 	if (ppc_md.kexec_cpu_down) | 
 | 258 | 		ppc_md.kexec_cpu_down(1, 0); | 
| Michael Ellerman | cc53291 | 2005-12-04 18:39:43 +1100 | [diff] [blame] | 259 | } |