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