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

source code of linux/arch/powerpc/kexec/core.c