GNU Linux-libre 4.19.286-gnu1
[releases.git] / arch / arm / kernel / machine_kexec.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * machine_kexec.c - handle transition of Linux booting another kernel
4  */
5
6 #include <linux/mm.h>
7 #include <linux/kexec.h>
8 #include <linux/delay.h>
9 #include <linux/reboot.h>
10 #include <linux/io.h>
11 #include <linux/irq.h>
12 #include <linux/memblock.h>
13 #include <asm/pgtable.h>
14 #include <linux/of_fdt.h>
15 #include <asm/pgalloc.h>
16 #include <asm/mmu_context.h>
17 #include <asm/cacheflush.h>
18 #include <asm/kexec-internal.h>
19 #include <asm/fncpy.h>
20 #include <asm/mach-types.h>
21 #include <asm/smp_plat.h>
22 #include <asm/system_misc.h>
23 #include <asm/set_memory.h>
24
25 extern void relocate_new_kernel(void);
26 extern const unsigned int relocate_new_kernel_size;
27
28 static atomic_t waiting_for_crash_ipi;
29
30 /*
31  * Provide a dummy crash_notes definition while crash dump arrives to arm.
32  * This prevents breakage of crash_notes attribute in kernel/ksysfs.c.
33  */
34
35 int machine_kexec_prepare(struct kimage *image)
36 {
37         struct kexec_segment *current_segment;
38         __be32 header;
39         int i, err;
40
41         image->arch.kernel_r2 = image->start - KEXEC_ARM_ZIMAGE_OFFSET
42                                      + KEXEC_ARM_ATAGS_OFFSET;
43
44         /*
45          * Validate that if the current HW supports SMP, then the SW supports
46          * and implements CPU hotplug for the current HW. If not, we won't be
47          * able to kexec reliably, so fail the prepare operation.
48          */
49         if (num_possible_cpus() > 1 && platform_can_secondary_boot() &&
50             !platform_can_cpu_hotplug())
51                 return -EINVAL;
52
53         /*
54          * No segment at default ATAGs address. try to locate
55          * a dtb using magic.
56          */
57         for (i = 0; i < image->nr_segments; i++) {
58                 current_segment = &image->segment[i];
59
60                 if (!memblock_is_region_memory(idmap_to_phys(current_segment->mem),
61                                                current_segment->memsz))
62                         return -EINVAL;
63
64                 err = get_user(header, (__be32*)current_segment->buf);
65                 if (err)
66                         return err;
67
68                 if (header == cpu_to_be32(OF_DT_HEADER))
69                         image->arch.kernel_r2 = current_segment->mem;
70         }
71         return 0;
72 }
73
74 void machine_kexec_cleanup(struct kimage *image)
75 {
76 }
77
78 void machine_crash_nonpanic_core(void *unused)
79 {
80         struct pt_regs regs;
81
82         crash_setup_regs(&regs, get_irq_regs());
83         printk(KERN_DEBUG "CPU %u will stop doing anything useful since another CPU has crashed\n",
84                smp_processor_id());
85         crash_save_cpu(&regs, smp_processor_id());
86         flush_cache_all();
87
88         set_cpu_online(smp_processor_id(), false);
89         atomic_dec(&waiting_for_crash_ipi);
90
91         while (1) {
92                 cpu_relax();
93                 wfe();
94         }
95 }
96
97 void crash_smp_send_stop(void)
98 {
99         static int cpus_stopped;
100         unsigned long msecs;
101
102         if (cpus_stopped)
103                 return;
104
105         atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
106         smp_call_function(machine_crash_nonpanic_core, NULL, false);
107         msecs = 1000; /* Wait at most a second for the other cpus to stop */
108         while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
109                 mdelay(1);
110                 msecs--;
111         }
112         if (atomic_read(&waiting_for_crash_ipi) > 0)
113                 pr_warn("Non-crashing CPUs did not react to IPI\n");
114
115         cpus_stopped = 1;
116 }
117
118 static void machine_kexec_mask_interrupts(void)
119 {
120         unsigned int i;
121         struct irq_desc *desc;
122
123         for_each_irq_desc(i, desc) {
124                 struct irq_chip *chip;
125
126                 chip = irq_desc_get_chip(desc);
127                 if (!chip)
128                         continue;
129
130                 if (chip->irq_eoi && irqd_irq_inprogress(&desc->irq_data))
131                         chip->irq_eoi(&desc->irq_data);
132
133                 if (chip->irq_mask)
134                         chip->irq_mask(&desc->irq_data);
135
136                 if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
137                         chip->irq_disable(&desc->irq_data);
138         }
139 }
140
141 void machine_crash_shutdown(struct pt_regs *regs)
142 {
143         local_irq_disable();
144         crash_smp_send_stop();
145
146         crash_save_cpu(regs, smp_processor_id());
147         machine_kexec_mask_interrupts();
148
149         pr_info("Loading crashdump kernel...\n");
150 }
151
152 /*
153  * Function pointer to optional machine-specific reinitialization
154  */
155 void (*kexec_reinit)(void);
156
157 void machine_kexec(struct kimage *image)
158 {
159         unsigned long page_list, reboot_entry_phys;
160         struct kexec_relocate_data *data;
161         void (*reboot_entry)(void);
162         void *reboot_code_buffer;
163
164         /*
165          * This can only happen if machine_shutdown() failed to disable some
166          * CPU, and that can only happen if the checks in
167          * machine_kexec_prepare() were not correct. If this fails, we can't
168          * reliably kexec anyway, so BUG_ON is appropriate.
169          */
170         BUG_ON(num_online_cpus() > 1);
171
172         page_list = image->head & PAGE_MASK;
173
174         reboot_code_buffer = page_address(image->control_code_page);
175
176         /* copy our kernel relocation code to the control code page */
177         reboot_entry = fncpy(reboot_code_buffer,
178                              &relocate_new_kernel,
179                              relocate_new_kernel_size);
180
181         data = reboot_code_buffer + relocate_new_kernel_size;
182         data->kexec_start_address = image->start;
183         data->kexec_indirection_page = page_list;
184         data->kexec_mach_type = machine_arch_type;
185         data->kexec_r2 = image->arch.kernel_r2;
186
187         /* get the identity mapping physical address for the reboot code */
188         reboot_entry_phys = virt_to_idmap(reboot_entry);
189
190         pr_info("Bye!\n");
191
192         if (kexec_reinit)
193                 kexec_reinit();
194
195         soft_restart(reboot_entry_phys);
196 }
197
198 void arch_crash_save_vmcoreinfo(void)
199 {
200 #ifdef CONFIG_ARM_LPAE
201         VMCOREINFO_CONFIG(ARM_LPAE);
202 #endif
203 }