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