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