Michael Ellerman | 3d1229d | 2005-11-14 23:35:00 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Code to handle transition of Linux booting another kernel. |
| 3 | * |
| 4 | * Copyright (C) 2002-2003 Eric Biederman <ebiederm@xmission.com> |
| 5 | * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz |
| 6 | * Copyright (C) 2005 IBM Corporation. |
| 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 | #include <linux/kexec.h> |
| 13 | #include <linux/reboot.h> |
| 14 | #include <linux/threads.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 15 | #include <linux/memblock.h> |
Dale Farnsworth | 2e8e4f5 | 2008-12-16 06:22:59 +0000 | [diff] [blame] | 16 | #include <linux/of.h> |
Michael Ellerman | 3d1229d | 2005-11-14 23:35:00 +1100 | [diff] [blame] | 17 | #include <asm/machdep.h> |
David S. Miller | d9b2b2a | 2008-02-13 16:56:49 -0800 | [diff] [blame] | 18 | #include <asm/prom.h> |
Dale Farnsworth | 2e8e4f5 | 2008-12-16 06:22:59 +0000 | [diff] [blame] | 19 | #include <asm/sections.h> |
Michael Ellerman | 3d1229d | 2005-11-14 23:35:00 +1100 | [diff] [blame] | 20 | |
Michael Ellerman | 3d1229d | 2005-11-14 23:35:00 +1100 | [diff] [blame] | 21 | void machine_crash_shutdown(struct pt_regs *regs) |
| 22 | { |
| 23 | if (ppc_md.machine_crash_shutdown) |
Michael Ellerman | cd0ca2c | 2005-12-04 18:39:12 +1100 | [diff] [blame] | 24 | ppc_md.machine_crash_shutdown(regs); |
Anton Vorontsov | 77733f8 | 2008-12-16 06:23:05 +0000 | [diff] [blame] | 25 | else |
| 26 | default_machine_crash_shutdown(regs); |
Michael Ellerman | 3d1229d | 2005-11-14 23:35:00 +1100 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /* |
| 30 | * Do what every setup is needed on image and the |
| 31 | * reboot code buffer to allow us to avoid allocations |
| 32 | * later. |
| 33 | */ |
| 34 | int machine_kexec_prepare(struct kimage *image) |
| 35 | { |
| 36 | if (ppc_md.machine_kexec_prepare) |
| 37 | return ppc_md.machine_kexec_prepare(image); |
Anton Vorontsov | 77733f8 | 2008-12-16 06:23:05 +0000 | [diff] [blame] | 38 | else |
| 39 | return default_machine_kexec_prepare(image); |
Michael Ellerman | 3d1229d | 2005-11-14 23:35:00 +1100 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void machine_kexec_cleanup(struct kimage *image) |
| 43 | { |
| 44 | if (ppc_md.machine_kexec_cleanup) |
| 45 | ppc_md.machine_kexec_cleanup(image); |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * Do not allocate memory (or fail in any way) in machine_kexec(). |
| 50 | * We are past the point of no return, committed to rebooting now. |
| 51 | */ |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 52 | void machine_kexec(struct kimage *image) |
Michael Ellerman | 3d1229d | 2005-11-14 23:35:00 +1100 | [diff] [blame] | 53 | { |
| 54 | if (ppc_md.machine_kexec) |
| 55 | ppc_md.machine_kexec(image); |
Anton Vorontsov | 77733f8 | 2008-12-16 06:23:05 +0000 | [diff] [blame] | 56 | else |
| 57 | default_machine_kexec(image); |
| 58 | |
| 59 | /* Fall back to normal restart if we're still alive. */ |
| 60 | machine_restart(NULL); |
Michael Ellerman | 3d1229d | 2005-11-14 23:35:00 +1100 | [diff] [blame] | 61 | for(;;); |
| 62 | } |
Michael Ellerman | 47585d8 | 2006-07-05 14:39:42 +1000 | [diff] [blame] | 63 | |
Michael Ellerman | 47585d8 | 2006-07-05 14:39:42 +1000 | [diff] [blame] | 64 | void __init reserve_crashkernel(void) |
| 65 | { |
Bernhard Walle | edd8ce6 | 2007-10-18 23:41:01 -0700 | [diff] [blame] | 66 | unsigned long long crash_size, crash_base; |
| 67 | int ret; |
Michael Ellerman | 47585d8 | 2006-07-05 14:39:42 +1000 | [diff] [blame] | 68 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 69 | /* this is necessary because of memblock_phys_mem_size() */ |
| 70 | memblock_analyze(); |
Bernhard Walle | edd8ce6 | 2007-10-18 23:41:01 -0700 | [diff] [blame] | 71 | |
| 72 | /* use common parsing */ |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 73 | ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), |
Bernhard Walle | edd8ce6 | 2007-10-18 23:41:01 -0700 | [diff] [blame] | 74 | &crash_size, &crash_base); |
| 75 | if (ret == 0 && crash_size > 0) { |
Bernhard Walle | edd8ce6 | 2007-10-18 23:41:01 -0700 | [diff] [blame] | 76 | crashk_res.start = crash_base; |
Michael Ellerman | eabd909 | 2008-04-30 14:47:12 +1000 | [diff] [blame] | 77 | crashk_res.end = crash_base + crash_size - 1; |
Bernhard Walle | edd8ce6 | 2007-10-18 23:41:01 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Michael Ellerman | eabd909 | 2008-04-30 14:47:12 +1000 | [diff] [blame] | 80 | if (crashk_res.end == crashk_res.start) { |
| 81 | crashk_res.start = crashk_res.end = 0; |
Michael Ellerman | 47585d8 | 2006-07-05 14:39:42 +1000 | [diff] [blame] | 82 | return; |
Michael Ellerman | eabd909 | 2008-04-30 14:47:12 +1000 | [diff] [blame] | 83 | } |
Michael Ellerman | 47585d8 | 2006-07-05 14:39:42 +1000 | [diff] [blame] | 84 | |
| 85 | /* We might have got these values via the command line or the |
| 86 | * device tree, either way sanitise them now. */ |
| 87 | |
Michael Ellerman | eabd909 | 2008-04-30 14:47:12 +1000 | [diff] [blame] | 88 | crash_size = crashk_res.end - crashk_res.start + 1; |
| 89 | |
Mohan Kumar M | 54622f1 | 2008-10-21 17:38:10 +0000 | [diff] [blame] | 90 | #ifndef CONFIG_RELOCATABLE |
Michael Ellerman | 47585d8 | 2006-07-05 14:39:42 +1000 | [diff] [blame] | 91 | if (crashk_res.start != KDUMP_KERNELBASE) |
| 92 | printk("Crash kernel location must be 0x%x\n", |
| 93 | KDUMP_KERNELBASE); |
| 94 | |
| 95 | crashk_res.start = KDUMP_KERNELBASE; |
Milton Miller | 66c721e | 2009-01-02 10:46:15 +0000 | [diff] [blame] | 96 | #else |
| 97 | if (!crashk_res.start) { |
| 98 | /* |
| 99 | * unspecified address, choose a region of specified size |
| 100 | * can overlap with initrd (ignoring corruption when retained) |
| 101 | * ppc64 requires kernel and some stacks to be in first segemnt |
| 102 | */ |
| 103 | crashk_res.start = KDUMP_KERNELBASE; |
| 104 | } |
| 105 | |
| 106 | crash_base = PAGE_ALIGN(crashk_res.start); |
| 107 | if (crash_base != crashk_res.start) { |
| 108 | printk("Crash kernel base must be aligned to 0x%lx\n", |
| 109 | PAGE_SIZE); |
| 110 | crashk_res.start = crash_base; |
| 111 | } |
| 112 | |
Mohan Kumar M | 54622f1 | 2008-10-21 17:38:10 +0000 | [diff] [blame] | 113 | #endif |
Bernhard Walle | edd8ce6 | 2007-10-18 23:41:01 -0700 | [diff] [blame] | 114 | crash_size = PAGE_ALIGN(crash_size); |
| 115 | crashk_res.end = crashk_res.start + crash_size - 1; |
Michael Ellerman | 47585d8 | 2006-07-05 14:39:42 +1000 | [diff] [blame] | 116 | |
Milton Miller | 66c721e | 2009-01-02 10:46:15 +0000 | [diff] [blame] | 117 | /* The crash region must not overlap the current kernel */ |
| 118 | if (overlaps_crashkernel(__pa(_stext), _end - _stext)) { |
| 119 | printk(KERN_WARNING |
| 120 | "Crash kernel can not overlap current kernel\n"); |
| 121 | crashk_res.start = crashk_res.end = 0; |
| 122 | return; |
| 123 | } |
| 124 | |
Michael Ellerman | 47585d8 | 2006-07-05 14:39:42 +1000 | [diff] [blame] | 125 | /* Crash kernel trumps memory limit */ |
| 126 | if (memory_limit && memory_limit <= crashk_res.end) { |
| 127 | memory_limit = crashk_res.end + 1; |
Becky Bruce | 49a8496 | 2009-05-08 12:19:27 +0000 | [diff] [blame] | 128 | printk("Adjusted memory limit for crashkernel, now 0x%llx\n", |
| 129 | (unsigned long long)memory_limit); |
Michael Ellerman | 47585d8 | 2006-07-05 14:39:42 +1000 | [diff] [blame] | 130 | } |
| 131 | |
Bernhard Walle | edd8ce6 | 2007-10-18 23:41:01 -0700 | [diff] [blame] | 132 | printk(KERN_INFO "Reserving %ldMB of memory at %ldMB " |
| 133 | "for crashkernel (System RAM: %ldMB)\n", |
| 134 | (unsigned long)(crash_size >> 20), |
| 135 | (unsigned long)(crashk_res.start >> 20), |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 136 | (unsigned long)(memblock_phys_mem_size() >> 20)); |
Bernhard Walle | edd8ce6 | 2007-10-18 23:41:01 -0700 | [diff] [blame] | 137 | |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame^] | 138 | memblock_reserve(crashk_res.start, crash_size); |
Michael Ellerman | 47585d8 | 2006-07-05 14:39:42 +1000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | int overlaps_crashkernel(unsigned long start, unsigned long size) |
| 142 | { |
| 143 | return (start + size) > crashk_res.start && start <= crashk_res.end; |
| 144 | } |
Dale Farnsworth | 2e8e4f5 | 2008-12-16 06:22:59 +0000 | [diff] [blame] | 145 | |
| 146 | /* Values we need to export to the second kernel via the device tree. */ |
| 147 | static unsigned long kernel_end; |
Dale Farnsworth | 6f29c32 | 2008-12-17 10:09:06 +0000 | [diff] [blame] | 148 | static unsigned long crashk_size; |
Dale Farnsworth | 2e8e4f5 | 2008-12-16 06:22:59 +0000 | [diff] [blame] | 149 | |
| 150 | static struct property kernel_end_prop = { |
| 151 | .name = "linux,kernel-end", |
| 152 | .length = sizeof(unsigned long), |
| 153 | .value = &kernel_end, |
| 154 | }; |
| 155 | |
Dale Farnsworth | 6f29c32 | 2008-12-17 10:09:06 +0000 | [diff] [blame] | 156 | static struct property crashk_base_prop = { |
| 157 | .name = "linux,crashkernel-base", |
| 158 | .length = sizeof(unsigned long), |
| 159 | .value = &crashk_res.start, |
| 160 | }; |
| 161 | |
| 162 | static struct property crashk_size_prop = { |
| 163 | .name = "linux,crashkernel-size", |
| 164 | .length = sizeof(unsigned long), |
| 165 | .value = &crashk_size, |
| 166 | }; |
| 167 | |
| 168 | static void __init export_crashk_values(struct device_node *node) |
| 169 | { |
| 170 | struct property *prop; |
| 171 | |
| 172 | /* There might be existing crash kernel properties, but we can't |
| 173 | * be sure what's in them, so remove them. */ |
| 174 | prop = of_find_property(node, "linux,crashkernel-base", NULL); |
| 175 | if (prop) |
| 176 | prom_remove_property(node, prop); |
| 177 | |
| 178 | prop = of_find_property(node, "linux,crashkernel-size", NULL); |
| 179 | if (prop) |
| 180 | prom_remove_property(node, prop); |
| 181 | |
| 182 | if (crashk_res.start != 0) { |
| 183 | prom_add_property(node, &crashk_base_prop); |
| 184 | crashk_size = crashk_res.end - crashk_res.start + 1; |
| 185 | prom_add_property(node, &crashk_size_prop); |
| 186 | } |
| 187 | } |
| 188 | |
Dale Farnsworth | 2e8e4f5 | 2008-12-16 06:22:59 +0000 | [diff] [blame] | 189 | static int __init kexec_setup(void) |
| 190 | { |
| 191 | struct device_node *node; |
| 192 | struct property *prop; |
| 193 | |
| 194 | node = of_find_node_by_path("/chosen"); |
| 195 | if (!node) |
| 196 | return -ENOENT; |
| 197 | |
| 198 | /* remove any stale properties so ours can be found */ |
| 199 | prop = of_find_property(node, kernel_end_prop.name, NULL); |
| 200 | if (prop) |
| 201 | prom_remove_property(node, prop); |
| 202 | |
| 203 | /* information needed by userspace when using default_machine_kexec */ |
| 204 | kernel_end = __pa(_end); |
| 205 | prom_add_property(node, &kernel_end_prop); |
| 206 | |
Dale Farnsworth | 6f29c32 | 2008-12-17 10:09:06 +0000 | [diff] [blame] | 207 | export_crashk_values(node); |
| 208 | |
Dale Farnsworth | 2e8e4f5 | 2008-12-16 06:22:59 +0000 | [diff] [blame] | 209 | of_node_put(node); |
| 210 | return 0; |
| 211 | } |
| 212 | late_initcall(kexec_setup); |