1793b08e2SChristophe Leroy // SPDX-License-Identifier: GPL-2.0-only
2793b08e2SChristophe Leroy /*
3793b08e2SChristophe Leroy * Code to handle transition of Linux booting another kernel.
4793b08e2SChristophe Leroy *
5793b08e2SChristophe Leroy * Copyright (C) 2002-2003 Eric Biederman <ebiederm@xmission.com>
6793b08e2SChristophe Leroy * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz
7793b08e2SChristophe Leroy * Copyright (C) 2005 IBM Corporation.
8793b08e2SChristophe Leroy */
9793b08e2SChristophe Leroy
10793b08e2SChristophe Leroy #include <linux/kexec.h>
11793b08e2SChristophe Leroy #include <linux/reboot.h>
12793b08e2SChristophe Leroy #include <linux/threads.h>
13793b08e2SChristophe Leroy #include <linux/memblock.h>
14793b08e2SChristophe Leroy #include <linux/of.h>
15793b08e2SChristophe Leroy #include <linux/irq.h>
16793b08e2SChristophe Leroy #include <linux/ftrace.h>
17793b08e2SChristophe Leroy
18793b08e2SChristophe Leroy #include <asm/kdump.h>
19793b08e2SChristophe Leroy #include <asm/machdep.h>
20793b08e2SChristophe Leroy #include <asm/pgalloc.h>
21793b08e2SChristophe Leroy #include <asm/sections.h>
22113fe88eSChristophe Leroy #include <asm/setup.h>
2346d60bdbSChristophe Leroy #include <asm/firmware.h>
24793b08e2SChristophe Leroy
machine_kexec_mask_interrupts(void)25793b08e2SChristophe Leroy void machine_kexec_mask_interrupts(void) {
26793b08e2SChristophe Leroy unsigned int i;
27793b08e2SChristophe Leroy struct irq_desc *desc;
28793b08e2SChristophe Leroy
29793b08e2SChristophe Leroy for_each_irq_desc(i, desc) {
30793b08e2SChristophe Leroy struct irq_chip *chip;
31793b08e2SChristophe Leroy
32793b08e2SChristophe Leroy chip = irq_desc_get_chip(desc);
33793b08e2SChristophe Leroy if (!chip)
34793b08e2SChristophe Leroy continue;
35793b08e2SChristophe Leroy
36793b08e2SChristophe Leroy if (chip->irq_eoi && irqd_irq_inprogress(&desc->irq_data))
37793b08e2SChristophe Leroy chip->irq_eoi(&desc->irq_data);
38793b08e2SChristophe Leroy
39793b08e2SChristophe Leroy if (chip->irq_mask)
40793b08e2SChristophe Leroy chip->irq_mask(&desc->irq_data);
41793b08e2SChristophe Leroy
42793b08e2SChristophe Leroy if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
43793b08e2SChristophe Leroy chip->irq_disable(&desc->irq_data);
44793b08e2SChristophe Leroy }
45793b08e2SChristophe Leroy }
46793b08e2SChristophe Leroy
47*5c4233ccSHari Bathini #ifdef CONFIG_CRASH_DUMP
machine_crash_shutdown(struct pt_regs * regs)48793b08e2SChristophe Leroy void machine_crash_shutdown(struct pt_regs *regs)
49793b08e2SChristophe Leroy {
50793b08e2SChristophe Leroy default_machine_crash_shutdown(regs);
51793b08e2SChristophe Leroy }
52*5c4233ccSHari Bathini #endif
53793b08e2SChristophe Leroy
machine_kexec_cleanup(struct kimage * image)54793b08e2SChristophe Leroy void machine_kexec_cleanup(struct kimage *image)
55793b08e2SChristophe Leroy {
56793b08e2SChristophe Leroy }
57793b08e2SChristophe Leroy
58793b08e2SChristophe Leroy /*
59793b08e2SChristophe Leroy * Do not allocate memory (or fail in any way) in machine_kexec().
60793b08e2SChristophe Leroy * We are past the point of no return, committed to rebooting now.
61793b08e2SChristophe Leroy */
machine_kexec(struct kimage * image)62793b08e2SChristophe Leroy void machine_kexec(struct kimage *image)
63793b08e2SChristophe Leroy {
64793b08e2SChristophe Leroy int save_ftrace_enabled;
65793b08e2SChristophe Leroy
66793b08e2SChristophe Leroy save_ftrace_enabled = __ftrace_enabled_save();
67793b08e2SChristophe Leroy this_cpu_disable_ftrace();
68793b08e2SChristophe Leroy
69793b08e2SChristophe Leroy if (ppc_md.machine_kexec)
70793b08e2SChristophe Leroy ppc_md.machine_kexec(image);
71793b08e2SChristophe Leroy else
72793b08e2SChristophe Leroy default_machine_kexec(image);
73793b08e2SChristophe Leroy
74793b08e2SChristophe Leroy this_cpu_enable_ftrace();
75793b08e2SChristophe Leroy __ftrace_enabled_restore(save_ftrace_enabled);
76793b08e2SChristophe Leroy
77793b08e2SChristophe Leroy /* Fall back to normal restart if we're still alive. */
78793b08e2SChristophe Leroy machine_restart(NULL);
79793b08e2SChristophe Leroy for(;;);
80793b08e2SChristophe Leroy }
81793b08e2SChristophe Leroy
82*5c4233ccSHari Bathini #ifdef CONFIG_CRASH_RESERVE
reserve_crashkernel(void)83793b08e2SChristophe Leroy void __init reserve_crashkernel(void)
84793b08e2SChristophe Leroy {
85be5470e0SPingfan Liu unsigned long long crash_size, crash_base, total_mem_sz;
86793b08e2SChristophe Leroy int ret;
87793b08e2SChristophe Leroy
88be5470e0SPingfan Liu total_mem_sz = memory_limit ? memory_limit : memblock_phys_mem_size();
89793b08e2SChristophe Leroy /* use common parsing */
90be5470e0SPingfan Liu ret = parse_crashkernel(boot_command_line, total_mem_sz,
91a9e1a3d8SBaoquan He &crash_size, &crash_base, NULL, NULL);
92793b08e2SChristophe Leroy if (ret == 0 && crash_size > 0) {
93793b08e2SChristophe Leroy crashk_res.start = crash_base;
94793b08e2SChristophe Leroy crashk_res.end = crash_base + crash_size - 1;
95793b08e2SChristophe Leroy }
96793b08e2SChristophe Leroy
97793b08e2SChristophe Leroy if (crashk_res.end == crashk_res.start) {
98793b08e2SChristophe Leroy crashk_res.start = crashk_res.end = 0;
99793b08e2SChristophe Leroy return;
100793b08e2SChristophe Leroy }
101793b08e2SChristophe Leroy
102793b08e2SChristophe Leroy /* We might have got these values via the command line or the
103793b08e2SChristophe Leroy * device tree, either way sanitise them now. */
104793b08e2SChristophe Leroy
105793b08e2SChristophe Leroy crash_size = resource_size(&crashk_res);
106793b08e2SChristophe Leroy
107793b08e2SChristophe Leroy #ifndef CONFIG_NONSTATIC_KERNEL
108793b08e2SChristophe Leroy if (crashk_res.start != KDUMP_KERNELBASE)
109793b08e2SChristophe Leroy printk("Crash kernel location must be 0x%x\n",
110793b08e2SChristophe Leroy KDUMP_KERNELBASE);
111793b08e2SChristophe Leroy
112793b08e2SChristophe Leroy crashk_res.start = KDUMP_KERNELBASE;
113793b08e2SChristophe Leroy #else
114793b08e2SChristophe Leroy if (!crashk_res.start) {
115793b08e2SChristophe Leroy #ifdef CONFIG_PPC64
116793b08e2SChristophe Leroy /*
1177c5ed82bSSourabh Jain * On the LPAR platform place the crash kernel to mid of
118bd7dc90eSHari Bathini * RMA size (max. of 512MB) to ensure the crash kernel
1197c5ed82bSSourabh Jain * gets enough space to place itself and some stack to be
1207c5ed82bSSourabh Jain * in the first segment. At the same time normal kernel
1217c5ed82bSSourabh Jain * also get enough space to allocate memory for essential
1227c5ed82bSSourabh Jain * system resource in the first segment. Keep the crash
1237c5ed82bSSourabh Jain * kernel starts at 128MB offset on other platforms.
124793b08e2SChristophe Leroy */
1257c5ed82bSSourabh Jain if (firmware_has_feature(FW_FEATURE_LPAR))
126bd7dc90eSHari Bathini crashk_res.start = min_t(u64, ppc64_rma_size / 2, SZ_512M);
1277c5ed82bSSourabh Jain else
128bd7dc90eSHari Bathini crashk_res.start = min_t(u64, ppc64_rma_size / 2, SZ_128M);
129793b08e2SChristophe Leroy #else
130793b08e2SChristophe Leroy crashk_res.start = KDUMP_KERNELBASE;
131793b08e2SChristophe Leroy #endif
132793b08e2SChristophe Leroy }
133793b08e2SChristophe Leroy
134793b08e2SChristophe Leroy crash_base = PAGE_ALIGN(crashk_res.start);
135793b08e2SChristophe Leroy if (crash_base != crashk_res.start) {
136793b08e2SChristophe Leroy printk("Crash kernel base must be aligned to 0x%lx\n",
137793b08e2SChristophe Leroy PAGE_SIZE);
138793b08e2SChristophe Leroy crashk_res.start = crash_base;
139793b08e2SChristophe Leroy }
140793b08e2SChristophe Leroy
141793b08e2SChristophe Leroy #endif
142793b08e2SChristophe Leroy crash_size = PAGE_ALIGN(crash_size);
143793b08e2SChristophe Leroy crashk_res.end = crashk_res.start + crash_size - 1;
144793b08e2SChristophe Leroy
145793b08e2SChristophe Leroy /* The crash region must not overlap the current kernel */
146793b08e2SChristophe Leroy if (overlaps_crashkernel(__pa(_stext), _end - _stext)) {
147793b08e2SChristophe Leroy printk(KERN_WARNING
148793b08e2SChristophe Leroy "Crash kernel can not overlap current kernel\n");
149793b08e2SChristophe Leroy crashk_res.start = crashk_res.end = 0;
150793b08e2SChristophe Leroy return;
151793b08e2SChristophe Leroy }
152793b08e2SChristophe Leroy
153793b08e2SChristophe Leroy /* Crash kernel trumps memory limit */
154793b08e2SChristophe Leroy if (memory_limit && memory_limit <= crashk_res.end) {
155793b08e2SChristophe Leroy memory_limit = crashk_res.end + 1;
156be5470e0SPingfan Liu total_mem_sz = memory_limit;
157793b08e2SChristophe Leroy printk("Adjusted memory limit for crashkernel, now 0x%llx\n",
158793b08e2SChristophe Leroy memory_limit);
159793b08e2SChristophe Leroy }
160793b08e2SChristophe Leroy
161793b08e2SChristophe Leroy printk(KERN_INFO "Reserving %ldMB of memory at %ldMB "
162793b08e2SChristophe Leroy "for crashkernel (System RAM: %ldMB)\n",
163793b08e2SChristophe Leroy (unsigned long)(crash_size >> 20),
164793b08e2SChristophe Leroy (unsigned long)(crashk_res.start >> 20),
165be5470e0SPingfan Liu (unsigned long)(total_mem_sz >> 20));
166793b08e2SChristophe Leroy
167793b08e2SChristophe Leroy if (!memblock_is_region_memory(crashk_res.start, crash_size) ||
168793b08e2SChristophe Leroy memblock_reserve(crashk_res.start, crash_size)) {
169793b08e2SChristophe Leroy pr_err("Failed to reserve memory for crashkernel!\n");
170793b08e2SChristophe Leroy crashk_res.start = crashk_res.end = 0;
171793b08e2SChristophe Leroy return;
172793b08e2SChristophe Leroy }
173793b08e2SChristophe Leroy }
174793b08e2SChristophe Leroy
overlaps_crashkernel(unsigned long start,unsigned long size)175d276960dSNick Child int __init overlaps_crashkernel(unsigned long start, unsigned long size)
176793b08e2SChristophe Leroy {
177793b08e2SChristophe Leroy return (start + size) > crashk_res.start && start <= crashk_res.end;
178793b08e2SChristophe Leroy }
179793b08e2SChristophe Leroy
180793b08e2SChristophe Leroy /* Values we need to export to the second kernel via the device tree. */
181793b08e2SChristophe Leroy static phys_addr_t kernel_end;
182793b08e2SChristophe Leroy static phys_addr_t crashk_base;
183793b08e2SChristophe Leroy static phys_addr_t crashk_size;
184793b08e2SChristophe Leroy static unsigned long long mem_limit;
185793b08e2SChristophe Leroy
186793b08e2SChristophe Leroy static struct property kernel_end_prop = {
187793b08e2SChristophe Leroy .name = "linux,kernel-end",
188793b08e2SChristophe Leroy .length = sizeof(phys_addr_t),
189793b08e2SChristophe Leroy .value = &kernel_end,
190793b08e2SChristophe Leroy };
191793b08e2SChristophe Leroy
192793b08e2SChristophe Leroy static struct property crashk_base_prop = {
193793b08e2SChristophe Leroy .name = "linux,crashkernel-base",
194793b08e2SChristophe Leroy .length = sizeof(phys_addr_t),
195793b08e2SChristophe Leroy .value = &crashk_base
196793b08e2SChristophe Leroy };
197793b08e2SChristophe Leroy
198793b08e2SChristophe Leroy static struct property crashk_size_prop = {
199793b08e2SChristophe Leroy .name = "linux,crashkernel-size",
200793b08e2SChristophe Leroy .length = sizeof(phys_addr_t),
201793b08e2SChristophe Leroy .value = &crashk_size,
202793b08e2SChristophe Leroy };
203793b08e2SChristophe Leroy
204793b08e2SChristophe Leroy static struct property memory_limit_prop = {
205793b08e2SChristophe Leroy .name = "linux,memory-limit",
206793b08e2SChristophe Leroy .length = sizeof(unsigned long long),
207793b08e2SChristophe Leroy .value = &mem_limit,
208793b08e2SChristophe Leroy };
209793b08e2SChristophe Leroy
210793b08e2SChristophe Leroy #define cpu_to_be_ulong __PASTE(cpu_to_be, BITS_PER_LONG)
211793b08e2SChristophe Leroy
export_crashk_values(struct device_node * node)212793b08e2SChristophe Leroy static void __init export_crashk_values(struct device_node *node)
213793b08e2SChristophe Leroy {
214793b08e2SChristophe Leroy /* There might be existing crash kernel properties, but we can't
215793b08e2SChristophe Leroy * be sure what's in them, so remove them. */
216793b08e2SChristophe Leroy of_remove_property(node, of_find_property(node,
217793b08e2SChristophe Leroy "linux,crashkernel-base", NULL));
218793b08e2SChristophe Leroy of_remove_property(node, of_find_property(node,
219793b08e2SChristophe Leroy "linux,crashkernel-size", NULL));
220793b08e2SChristophe Leroy
221793b08e2SChristophe Leroy if (crashk_res.start != 0) {
222793b08e2SChristophe Leroy crashk_base = cpu_to_be_ulong(crashk_res.start),
223793b08e2SChristophe Leroy of_add_property(node, &crashk_base_prop);
224793b08e2SChristophe Leroy crashk_size = cpu_to_be_ulong(resource_size(&crashk_res));
225793b08e2SChristophe Leroy of_add_property(node, &crashk_size_prop);
226793b08e2SChristophe Leroy }
227793b08e2SChristophe Leroy
228793b08e2SChristophe Leroy /*
229793b08e2SChristophe Leroy * memory_limit is required by the kexec-tools to limit the
230793b08e2SChristophe Leroy * crash regions to the actual memory used.
231793b08e2SChristophe Leroy */
232793b08e2SChristophe Leroy mem_limit = cpu_to_be_ulong(memory_limit);
233793b08e2SChristophe Leroy of_update_property(node, &memory_limit_prop);
234793b08e2SChristophe Leroy }
235793b08e2SChristophe Leroy
kexec_setup(void)236793b08e2SChristophe Leroy static int __init kexec_setup(void)
237793b08e2SChristophe Leroy {
238793b08e2SChristophe Leroy struct device_node *node;
239793b08e2SChristophe Leroy
240793b08e2SChristophe Leroy node = of_find_node_by_path("/chosen");
241793b08e2SChristophe Leroy if (!node)
242793b08e2SChristophe Leroy return -ENOENT;
243793b08e2SChristophe Leroy
244793b08e2SChristophe Leroy /* remove any stale properties so ours can be found */
245793b08e2SChristophe Leroy of_remove_property(node, of_find_property(node, kernel_end_prop.name, NULL));
246793b08e2SChristophe Leroy
247793b08e2SChristophe Leroy /* information needed by userspace when using default_machine_kexec */
248793b08e2SChristophe Leroy kernel_end = cpu_to_be_ulong(__pa(_end));
249793b08e2SChristophe Leroy of_add_property(node, &kernel_end_prop);
250793b08e2SChristophe Leroy
251793b08e2SChristophe Leroy export_crashk_values(node);
252793b08e2SChristophe Leroy
253793b08e2SChristophe Leroy of_node_put(node);
254793b08e2SChristophe Leroy return 0;
255793b08e2SChristophe Leroy }
256793b08e2SChristophe Leroy late_initcall(kexec_setup);
257*5c4233ccSHari Bathini #endif /* CONFIG_CRASH_RESERVE */
258