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