1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Code to handle transition of Linux booting another kernel. 4 * 5 * Copyright (C) 2002-2003 Eric Biederman <ebiederm@xmission.com> 6 * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz 7 * Copyright (C) 2005 IBM Corporation. 8 */ 9 10 #include <linux/kexec.h> 11 #include <linux/reboot.h> 12 #include <linux/threads.h> 13 #include <linux/memblock.h> 14 #include <linux/of.h> 15 #include <linux/irq.h> 16 #include <linux/ftrace.h> 17 18 #include <asm/kdump.h> 19 #include <asm/machdep.h> 20 #include <asm/pgalloc.h> 21 #include <asm/sections.h> 22 #include <asm/setup.h> 23 #include <asm/firmware.h> 24 25 #define cpu_to_be_ulong __PASTE(cpu_to_be, BITS_PER_LONG) 26 #define __be_word __PASTE(__be, BITS_PER_LONG) 27 28 #ifdef CONFIG_CRASH_DUMP 29 void machine_crash_shutdown(struct pt_regs *regs) 30 { 31 default_machine_crash_shutdown(regs); 32 } 33 #endif 34 35 void machine_kexec_cleanup(struct kimage *image) 36 { 37 } 38 39 /* 40 * Do not allocate memory (or fail in any way) in machine_kexec(). 41 * We are past the point of no return, committed to rebooting now. 42 */ 43 void machine_kexec(struct kimage *image) 44 { 45 int save_ftrace_enabled; 46 47 save_ftrace_enabled = __ftrace_enabled_save(); 48 this_cpu_disable_ftrace(); 49 50 if (ppc_md.machine_kexec) 51 ppc_md.machine_kexec(image); 52 else 53 default_machine_kexec(image); 54 55 this_cpu_enable_ftrace(); 56 __ftrace_enabled_restore(save_ftrace_enabled); 57 58 /* Fall back to normal restart if we're still alive. */ 59 machine_restart(NULL); 60 for(;;); 61 } 62 63 #ifdef CONFIG_CRASH_RESERVE 64 65 static unsigned long long crashk_cma_size; 66 67 static unsigned long long __init get_crash_base(unsigned long long crash_base) 68 { 69 70 #ifndef CONFIG_NONSTATIC_KERNEL 71 if (crash_base != KDUMP_KERNELBASE) 72 printk("Crash kernel location must be 0x%x\n", 73 KDUMP_KERNELBASE); 74 75 return KDUMP_KERNELBASE; 76 #else 77 unsigned long long crash_base_align; 78 79 if (!crash_base) { 80 #ifdef CONFIG_PPC64 81 /* 82 * On the LPAR platform place the crash kernel to mid of 83 * RMA size (max. of 512MB) to ensure the crash kernel 84 * gets enough space to place itself and some stack to be 85 * in the first segment. At the same time normal kernel 86 * also get enough space to allocate memory for essential 87 * system resource in the first segment. Keep the crash 88 * kernel starts at 128MB offset on other platforms. 89 */ 90 if (firmware_has_feature(FW_FEATURE_LPAR)) 91 crash_base = min_t(u64, ppc64_rma_size / 2, SZ_512M); 92 else 93 crash_base = min_t(u64, ppc64_rma_size / 2, SZ_128M); 94 #else 95 crash_base = KDUMP_KERNELBASE; 96 #endif 97 } 98 99 crash_base_align = PAGE_ALIGN(crash_base); 100 if (crash_base != crash_base_align) 101 pr_warn("Crash kernel base must be aligned to 0x%lx\n", PAGE_SIZE); 102 103 return crash_base_align; 104 #endif 105 } 106 107 void __init arch_reserve_crashkernel(void) 108 { 109 unsigned long long crash_size, crash_base, crash_end; 110 unsigned long long kernel_start, kernel_size; 111 unsigned long long total_mem_sz; 112 int ret; 113 114 total_mem_sz = memory_limit ? memory_limit : memblock_phys_mem_size(); 115 116 /* use common parsing */ 117 ret = parse_crashkernel(boot_command_line, total_mem_sz, &crash_size, 118 &crash_base, NULL, &crashk_cma_size, NULL); 119 120 if (ret) 121 return; 122 123 crash_base = get_crash_base(crash_base); 124 crash_end = crash_base + crash_size - 1; 125 126 kernel_start = __pa(_stext); 127 kernel_size = _end - _stext; 128 129 /* The crash region must not overlap the current kernel */ 130 if ((kernel_start + kernel_size > crash_base) && (kernel_start <= crash_end)) { 131 pr_warn("Crash kernel can not overlap current kernel\n"); 132 return; 133 } 134 135 reserve_crashkernel_generic(crash_size, crash_base, 0, false); 136 } 137 138 void __init kdump_cma_reserve(void) 139 { 140 if (crashk_cma_size) 141 reserve_crashkernel_cma(crashk_cma_size); 142 } 143 144 int __init overlaps_crashkernel(unsigned long start, unsigned long size) 145 { 146 return (start + size) > crashk_res.start && start <= crashk_res.end; 147 } 148 149 /* Values we need to export to the second kernel via the device tree. */ 150 static __be_word crashk_base; 151 static __be_word crashk_size; 152 static __be_word mem_limit; 153 154 static struct property crashk_base_prop = { 155 .name = "linux,crashkernel-base", 156 .length = sizeof(__be_word), 157 .value = &crashk_base 158 }; 159 160 static struct property crashk_size_prop = { 161 .name = "linux,crashkernel-size", 162 .length = sizeof(__be_word), 163 .value = &crashk_size, 164 }; 165 166 static struct property memory_limit_prop = { 167 .name = "linux,memory-limit", 168 .length = sizeof(__be_word), 169 .value = &mem_limit, 170 }; 171 172 static void __init export_crashk_values(struct device_node *node) 173 { 174 /* There might be existing crash kernel properties, but we can't 175 * be sure what's in them, so remove them. */ 176 of_remove_property(node, of_find_property(node, 177 "linux,crashkernel-base", NULL)); 178 of_remove_property(node, of_find_property(node, 179 "linux,crashkernel-size", NULL)); 180 181 if (crashk_res.start != 0) { 182 crashk_base = cpu_to_be_ulong(crashk_res.start), 183 of_add_property(node, &crashk_base_prop); 184 crashk_size = cpu_to_be_ulong(resource_size(&crashk_res)); 185 of_add_property(node, &crashk_size_prop); 186 } 187 188 /* 189 * memory_limit is required by the kexec-tools to limit the 190 * crash regions to the actual memory used. 191 */ 192 mem_limit = cpu_to_be_ulong(memory_limit); 193 of_update_property(node, &memory_limit_prop); 194 } 195 #endif /* CONFIG_CRASH_RESERVE */ 196 197 static __be_word kernel_end; 198 199 static struct property kernel_end_prop = { 200 .name = "linux,kernel-end", 201 .length = sizeof(__be_word), 202 .value = &kernel_end, 203 }; 204 205 static int __init kexec_setup(void) 206 { 207 struct device_node *node; 208 209 node = of_find_node_by_path("/chosen"); 210 if (!node) 211 return -ENOENT; 212 213 /* remove any stale properties so ours can be found */ 214 of_remove_property(node, of_find_property(node, kernel_end_prop.name, 215 NULL)); 216 217 /* information needed by userspace when using default_machine_kexec */ 218 kernel_end = cpu_to_be_ulong(__pa(_end)); 219 of_add_property(node, &kernel_end_prop); 220 221 #ifdef CONFIG_CRASH_RESERVE 222 export_crashk_values(node); 223 #endif 224 of_node_put(node); 225 return 0; 226 } 227 late_initcall(kexec_setup); 228