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