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