1 // SPDX-License-Identifier: GPL-2.0-only
3 * Code to handle transition of Linux booting another kernel.
6 * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz
7 * Copyright (C) 2005 IBM Corporation.
10 #include <linux/kexec.h>
11 #include <linux/reboot.h>
12 #include <linux/threads.h>
13 #include <linux/memblock.h>
15 #include <linux/irq.h>
16 #include <linux/ftrace.h>
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>
25 void machine_kexec_mask_interrupts(void) {
27 struct irq_desc *desc;
29 for_each_irq_desc(i, desc) {
30 struct irq_chip *chip;
32 chip = irq_desc_get_chip(desc);
36 if (chip->irq_eoi && irqd_irq_inprogress(&desc->irq_data))
37 chip->irq_eoi(&desc->irq_data);
40 chip->irq_mask(&desc->irq_data);
42 if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
43 chip->irq_disable(&desc->irq_data);
47 void machine_crash_shutdown(struct pt_regs *regs)
49 default_machine_crash_shutdown(regs);
52 void machine_kexec_cleanup(struct kimage *image)
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.
60 void machine_kexec(struct kimage *image)
62 int save_ftrace_enabled;
64 save_ftrace_enabled = __ftrace_enabled_save();
65 this_cpu_disable_ftrace();
67 if (ppc_md.machine_kexec)
68 ppc_md.machine_kexec(image);
70 default_machine_kexec(image);
72 this_cpu_enable_ftrace();
73 __ftrace_enabled_restore(save_ftrace_enabled);
75 /* Fall back to normal restart if we're still alive. */
76 machine_restart(NULL);
80 void __init reserve_crashkernel(void)
82 unsigned long long crash_size, crash_base, total_mem_sz;
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;
94 if (crashk_res.end == crashk_res.start) {
95 crashk_res.start = crashk_res.end = 0;
99 /* We might have got these values via the command line or the
100 * device tree, either way sanitise them now. */
102 crash_size = resource_size(&crashk_res);
104 #ifndef CONFIG_NONSTATIC_KERNEL
105 if (crashk_res.start != KDUMP_KERNELBASE)
106 printk("Crash kernel location must be 0x%x\n",
109 crashk_res.start = KDUMP_KERNELBASE;
111 if (!crashk_res.start) {
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.
122 if (firmware_has_feature(FW_FEATURE_LPAR))
123 crashk_res.start = min_t(u64, ppc64_rma_size / 2, SZ_512M);
125 crashk_res.start = min_t(u64, ppc64_rma_size / 2, SZ_128M);
127 crashk_res.start = KDUMP_KERNELBASE;
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",
135 crashk_res.start = crash_base;
139 crash_size = PAGE_ALIGN(crash_size);
140 crashk_res.end = crashk_res.start + crash_size - 1;
142 /* The crash region must not overlap the current kernel */
143 if (overlaps_crashkernel(__pa(_stext), _end - _stext)) {
145 "Crash kernel can not overlap current kernel\n");
146 crashk_res.start = crashk_res.end = 0;
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",
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));
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;
172 int __init overlaps_crashkernel(unsigned long start, unsigned long size)
174 return (start + size) > crashk_res.start && start <= crashk_res.end;
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;
183 static struct property kernel_end_prop = {
184 .name = "linux,kernel-end",
185 .length = sizeof(phys_addr_t),
186 .value = &kernel_end,
189 static struct property crashk_base_prop = {
190 .name = "linux,crashkernel-base",
191 .length = sizeof(phys_addr_t),
192 .value = &crashk_base
195 static struct property crashk_size_prop = {
196 .name = "linux,crashkernel-size",
197 .length = sizeof(phys_addr_t),
198 .value = &crashk_size,
201 static struct property memory_limit_prop = {
202 .name = "linux,memory-limit",
203 .length = sizeof(unsigned long long),
207 #define cpu_to_be_ulong __PASTE(cpu_to_be, BITS_PER_LONG)
209 static void __init export_crashk_values(struct device_node *node)
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));
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);
226 * memory_limit is required by the kexec-tools to limit the
227 * crash regions to the actual memory used.
229 mem_limit = cpu_to_be_ulong(memory_limit);
230 of_update_property(node, &memory_limit_prop);
233 static int __init kexec_setup(void)
235 struct device_node *node;
237 node = of_find_node_by_path("/chosen");
241 /* remove any stale properties so ours can be found */
242 of_remove_property(node, of_find_property(node, kernel_end_prop.name, NULL));
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);
248 export_crashk_values(node);
253 late_initcall(kexec_setup);