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