]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/arm64/kernel/machine_kexec.c
arm64: hibernate: preserve kdump image around hibernation
[karo-tx-linux.git] / arch / arm64 / kernel / machine_kexec.c
1 /*
2  * kexec for arm64
3  *
4  * Copyright (C) Linaro.
5  * Copyright (C) Huawei Futurewei Technologies.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/kexec.h>
13 #include <linux/page-flags.h>
14 #include <linux/smp.h>
15
16 #include <asm/cacheflush.h>
17 #include <asm/cpu_ops.h>
18 #include <asm/mmu.h>
19 #include <asm/mmu_context.h>
20 #include <asm/page.h>
21
22 #include "cpu-reset.h"
23
24 /* Global variables for the arm64_relocate_new_kernel routine. */
25 extern const unsigned char arm64_relocate_new_kernel[];
26 extern const unsigned long arm64_relocate_new_kernel_size;
27
28 /**
29  * kexec_image_info - For debugging output.
30  */
31 #define kexec_image_info(_i) _kexec_image_info(__func__, __LINE__, _i)
32 static void _kexec_image_info(const char *func, int line,
33         const struct kimage *kimage)
34 {
35         unsigned long i;
36
37         pr_debug("%s:%d:\n", func, line);
38         pr_debug("  kexec kimage info:\n");
39         pr_debug("    type:        %d\n", kimage->type);
40         pr_debug("    start:       %lx\n", kimage->start);
41         pr_debug("    head:        %lx\n", kimage->head);
42         pr_debug("    nr_segments: %lu\n", kimage->nr_segments);
43
44         for (i = 0; i < kimage->nr_segments; i++) {
45                 pr_debug("      segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
46                         i,
47                         kimage->segment[i].mem,
48                         kimage->segment[i].mem + kimage->segment[i].memsz,
49                         kimage->segment[i].memsz,
50                         kimage->segment[i].memsz /  PAGE_SIZE);
51         }
52 }
53
54 void machine_kexec_cleanup(struct kimage *kimage)
55 {
56         /* Empty routine needed to avoid build errors. */
57 }
58
59 /**
60  * machine_kexec_prepare - Prepare for a kexec reboot.
61  *
62  * Called from the core kexec code when a kernel image is loaded.
63  * Forbid loading a kexec kernel if we have no way of hotplugging cpus or cpus
64  * are stuck in the kernel. This avoids a panic once we hit machine_kexec().
65  */
66 int machine_kexec_prepare(struct kimage *kimage)
67 {
68         kexec_image_info(kimage);
69
70         if (kimage->type != KEXEC_TYPE_CRASH && cpus_are_stuck_in_kernel()) {
71                 pr_err("Can't kexec: CPUs are stuck in the kernel.\n");
72                 return -EBUSY;
73         }
74
75         return 0;
76 }
77
78 /**
79  * kexec_list_flush - Helper to flush the kimage list and source pages to PoC.
80  */
81 static void kexec_list_flush(struct kimage *kimage)
82 {
83         kimage_entry_t *entry;
84
85         for (entry = &kimage->head; ; entry++) {
86                 unsigned int flag;
87                 void *addr;
88
89                 /* flush the list entries. */
90                 __flush_dcache_area(entry, sizeof(kimage_entry_t));
91
92                 flag = *entry & IND_FLAGS;
93                 if (flag == IND_DONE)
94                         break;
95
96                 addr = phys_to_virt(*entry & PAGE_MASK);
97
98                 switch (flag) {
99                 case IND_INDIRECTION:
100                         /* Set entry point just before the new list page. */
101                         entry = (kimage_entry_t *)addr - 1;
102                         break;
103                 case IND_SOURCE:
104                         /* flush the source pages. */
105                         __flush_dcache_area(addr, PAGE_SIZE);
106                         break;
107                 case IND_DESTINATION:
108                         break;
109                 default:
110                         BUG();
111                 }
112         }
113 }
114
115 /**
116  * kexec_segment_flush - Helper to flush the kimage segments to PoC.
117  */
118 static void kexec_segment_flush(const struct kimage *kimage)
119 {
120         unsigned long i;
121
122         pr_debug("%s:\n", __func__);
123
124         for (i = 0; i < kimage->nr_segments; i++) {
125                 pr_debug("  segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
126                         i,
127                         kimage->segment[i].mem,
128                         kimage->segment[i].mem + kimage->segment[i].memsz,
129                         kimage->segment[i].memsz,
130                         kimage->segment[i].memsz /  PAGE_SIZE);
131
132                 __flush_dcache_area(phys_to_virt(kimage->segment[i].mem),
133                         kimage->segment[i].memsz);
134         }
135 }
136
137 /**
138  * machine_kexec - Do the kexec reboot.
139  *
140  * Called from the core kexec code for a sys_reboot with LINUX_REBOOT_CMD_KEXEC.
141  */
142 void machine_kexec(struct kimage *kimage)
143 {
144         phys_addr_t reboot_code_buffer_phys;
145         void *reboot_code_buffer;
146
147         /*
148          * New cpus may have become stuck_in_kernel after we loaded the image.
149          */
150         BUG_ON(cpus_are_stuck_in_kernel() || (num_online_cpus() > 1));
151
152         reboot_code_buffer_phys = page_to_phys(kimage->control_code_page);
153         reboot_code_buffer = phys_to_virt(reboot_code_buffer_phys);
154
155         kexec_image_info(kimage);
156
157         pr_debug("%s:%d: control_code_page:        %p\n", __func__, __LINE__,
158                 kimage->control_code_page);
159         pr_debug("%s:%d: reboot_code_buffer_phys:  %pa\n", __func__, __LINE__,
160                 &reboot_code_buffer_phys);
161         pr_debug("%s:%d: reboot_code_buffer:       %p\n", __func__, __LINE__,
162                 reboot_code_buffer);
163         pr_debug("%s:%d: relocate_new_kernel:      %p\n", __func__, __LINE__,
164                 arm64_relocate_new_kernel);
165         pr_debug("%s:%d: relocate_new_kernel_size: 0x%lx(%lu) bytes\n",
166                 __func__, __LINE__, arm64_relocate_new_kernel_size,
167                 arm64_relocate_new_kernel_size);
168
169         /*
170          * Copy arm64_relocate_new_kernel to the reboot_code_buffer for use
171          * after the kernel is shut down.
172          */
173         memcpy(reboot_code_buffer, arm64_relocate_new_kernel,
174                 arm64_relocate_new_kernel_size);
175
176         /* Flush the reboot_code_buffer in preparation for its execution. */
177         __flush_dcache_area(reboot_code_buffer, arm64_relocate_new_kernel_size);
178         flush_icache_range((uintptr_t)reboot_code_buffer,
179                 arm64_relocate_new_kernel_size);
180
181         /* Flush the kimage list and its buffers. */
182         kexec_list_flush(kimage);
183
184         /* Flush the new image if already in place. */
185         if ((kimage != kexec_crash_image) && (kimage->head & IND_DONE))
186                 kexec_segment_flush(kimage);
187
188         pr_info("Bye!\n");
189
190         /* Disable all DAIF exceptions. */
191         asm volatile ("msr daifset, #0xf" : : : "memory");
192
193         /*
194          * cpu_soft_restart will shutdown the MMU, disable data caches, then
195          * transfer control to the reboot_code_buffer which contains a copy of
196          * the arm64_relocate_new_kernel routine.  arm64_relocate_new_kernel
197          * uses physical addressing to relocate the new image to its final
198          * position and transfers control to the image entry point when the
199          * relocation is complete.
200          */
201
202         cpu_soft_restart(1, reboot_code_buffer_phys, kimage->head,
203                 kimage->start, 0);
204
205         BUG(); /* Should never get here. */
206 }
207
208 void machine_crash_shutdown(struct pt_regs *regs)
209 {
210         /* Empty routine needed to avoid build errors. */
211 }
212
213 void arch_kexec_protect_crashkres(void)
214 {
215         int i;
216
217         kexec_segment_flush(kexec_crash_image);
218
219         for (i = 0; i < kexec_crash_image->nr_segments; i++)
220                 set_memory_valid(
221                         __phys_to_virt(kexec_crash_image->segment[i].mem),
222                         kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 0);
223 }
224
225 void arch_kexec_unprotect_crashkres(void)
226 {
227         int i;
228
229         for (i = 0; i < kexec_crash_image->nr_segments; i++)
230                 set_memory_valid(
231                         __phys_to_virt(kexec_crash_image->segment[i].mem),
232                         kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 1);
233 }
234
235 #ifdef CONFIG_HIBERNATION
236 /*
237  * To preserve the crash dump kernel image, the relevant memory segments
238  * should be mapped again around the hibernation.
239  */
240 void crash_prepare_suspend(void)
241 {
242         if (kexec_crash_image)
243                 arch_kexec_unprotect_crashkres();
244 }
245
246 void crash_post_resume(void)
247 {
248         if (kexec_crash_image)
249                 arch_kexec_protect_crashkres();
250 }
251
252 /*
253  * crash_is_nosave
254  *
255  * Return true only if a page is part of reserved memory for crash dump kernel,
256  * but does not hold any data of loaded kernel image.
257  *
258  * Note that all the pages in crash dump kernel memory have been initially
259  * marked as Reserved in kexec_reserve_crashkres_pages().
260  *
261  * In hibernation, the pages which are Reserved and yet "nosave" are excluded
262  * from the hibernation iamge. crash_is_nosave() does thich check for crash
263  * dump kernel and will reduce the total size of hibernation image.
264  */
265
266 bool crash_is_nosave(unsigned long pfn)
267 {
268         int i;
269         phys_addr_t addr;
270
271         if (!crashk_res.end)
272                 return false;
273
274         /* in reserved memory? */
275         addr = __pfn_to_phys(pfn);
276         if ((addr < crashk_res.start) || (crashk_res.end < addr))
277                 return false;
278
279         if (!kexec_crash_image)
280                 return true;
281
282         /* not part of loaded kernel image? */
283         for (i = 0; i < kexec_crash_image->nr_segments; i++)
284                 if (addr >= kexec_crash_image->segment[i].mem &&
285                                 addr < (kexec_crash_image->segment[i].mem +
286                                         kexec_crash_image->segment[i].memsz))
287                         return false;
288
289         return true;
290 }
291
292 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
293 {
294         unsigned long addr;
295         struct page *page;
296
297         for (addr = begin; addr < end; addr += PAGE_SIZE) {
298                 page = phys_to_page(addr);
299                 ClearPageReserved(page);
300                 free_reserved_page(page);
301         }
302 }
303 #endif /* CONFIG_HIBERNATION */