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