1 // SPDX-License-Identifier: GPL-2.0-only
3 * machine_kexec.c for kexec
4 * Created by <nschichan@corp.free.fr> on Thu Oct 12 15:15:06 2006
6 #include <linux/compiler.h>
7 #include <linux/kexec.h>
9 #include <linux/delay.h>
10 #include <linux/libfdt.h>
11 #include <linux/reboot.h>
13 #include <asm/cacheflush.h>
16 extern const unsigned char relocate_new_kernel
[];
17 extern const size_t relocate_new_kernel_size
;
19 extern unsigned long kexec_start_address
;
20 extern unsigned long kexec_indirection_page
;
22 static unsigned long reboot_code_buffer
;
25 static void (*relocated_kexec_smp_wait
)(void *);
27 atomic_t kexec_ready_to_reboot
= ATOMIC_INIT(0);
28 void (*_crash_smp_send_stop
)(void) = NULL
;
31 void (*_machine_kexec_shutdown
)(void) = NULL
;
32 void (*_machine_crash_shutdown
)(struct pt_regs
*regs
) = NULL
;
34 static void kexec_image_info(const struct kimage
*kimage
)
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
);
44 for (i
= 0; i
< kimage
->nr_segments
; i
++) {
45 pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
47 kimage
->segment
[i
].mem
,
48 kimage
->segment
[i
].mem
+ kimage
->segment
[i
].memsz
,
49 (unsigned long)kimage
->segment
[i
].memsz
,
50 (unsigned long)kimage
->segment
[i
].memsz
/ PAGE_SIZE
);
54 #ifdef CONFIG_UHI_BOOT
56 static int uhi_machine_kexec_prepare(struct kimage
*kimage
)
61 * In case DTB file is not passed to the new kernel, a flat device
62 * tree will be created by kexec tool. It holds modified command
63 * line for the new kernel.
65 for (i
= 0; i
< kimage
->nr_segments
; i
++) {
66 struct fdt_header fdt
;
68 if (kimage
->segment
[i
].memsz
<= sizeof(fdt
))
71 if (copy_from_user(&fdt
, kimage
->segment
[i
].buf
, sizeof(fdt
)))
74 if (fdt_check_header(&fdt
))
78 kexec_args
[1] = (unsigned long)
79 phys_to_virt((unsigned long)kimage
->segment
[i
].mem
);
86 int (*_machine_kexec_prepare
)(struct kimage
*) = uhi_machine_kexec_prepare
;
90 int (*_machine_kexec_prepare
)(struct kimage
*) = NULL
;
92 #endif /* CONFIG_UHI_BOOT */
95 machine_kexec_prepare(struct kimage
*kimage
)
98 if (!kexec_nonboot_cpu_func())
102 kexec_image_info(kimage
);
104 if (_machine_kexec_prepare
)
105 return _machine_kexec_prepare(kimage
);
111 machine_kexec_cleanup(struct kimage
*kimage
)
116 static void kexec_shutdown_secondary(void *param
)
118 int cpu
= smp_processor_id();
120 if (!cpu_online(cpu
))
123 /* We won't be sent IPIs any more. */
124 set_cpu_online(cpu
, false);
127 while (!atomic_read(&kexec_ready_to_reboot
))
137 machine_shutdown(void)
139 if (_machine_kexec_shutdown
)
140 _machine_kexec_shutdown();
143 smp_call_function(kexec_shutdown_secondary
, NULL
, 0);
145 while (num_online_cpus() > 1) {
153 machine_crash_shutdown(struct pt_regs
*regs
)
155 if (_machine_crash_shutdown
)
156 _machine_crash_shutdown(regs
);
158 default_machine_crash_shutdown(regs
);
162 void kexec_nonboot_cpu_jump(void)
164 local_flush_icache_range((unsigned long)relocated_kexec_smp_wait
,
165 reboot_code_buffer
+ relocate_new_kernel_size
);
167 relocated_kexec_smp_wait(NULL
);
171 void kexec_reboot(void)
173 void (*do_kexec
)(void) __noreturn
;
176 * We know we were online, and there will be no incoming IPIs at
177 * this point. Mark online again before rebooting so that the crash
178 * analysis tool will see us correctly.
180 set_cpu_online(smp_processor_id(), true);
182 /* Ensure remote CPUs observe that we're online before rebooting. */
183 smp_mb__after_atomic();
186 if (smp_processor_id() > 0) {
188 * Instead of cpu_relax() or wait, this is needed for kexec
189 * smp reboot. Kdump usually doesn't require an smp new
190 * kernel, but kexec may do.
199 * Make sure we get correct instructions written by the
200 * machine_kexec() CPU.
202 local_flush_icache_range(reboot_code_buffer
,
203 reboot_code_buffer
+ relocate_new_kernel_size
);
205 do_kexec
= (void *)reboot_code_buffer
;
210 machine_kexec(struct kimage
*image
)
216 (unsigned long)page_address(image
->control_code_page
);
218 kexec_start_address
=
219 (unsigned long) phys_to_virt(image
->start
);
221 if (image
->type
== KEXEC_TYPE_DEFAULT
) {
222 kexec_indirection_page
=
223 (unsigned long) phys_to_virt(image
->head
& PAGE_MASK
);
225 kexec_indirection_page
= (unsigned long)&image
->head
;
228 memcpy((void*)reboot_code_buffer
, relocate_new_kernel
,
229 relocate_new_kernel_size
);
232 * The generic kexec code builds a page list with physical
233 * addresses. they are directly accessible through KSEG0 (or
234 * CKSEG0 or XPHYS if on 64bit system), hence the
235 * phys_to_virt() call.
237 for (ptr
= &image
->head
; (entry
= *ptr
) && !(entry
&IND_DONE
);
238 ptr
= (entry
& IND_INDIRECTION
) ?
239 phys_to_virt(entry
& PAGE_MASK
) : ptr
+ 1) {
240 if (*ptr
& IND_SOURCE
|| *ptr
& IND_INDIRECTION
||
241 *ptr
& IND_DESTINATION
)
242 *ptr
= (unsigned long) phys_to_virt(*ptr
);
245 /* Mark offline BEFORE disabling local irq. */
246 set_cpu_online(smp_processor_id(), false);
249 * we do not want to be bothered.
253 printk("Will call new kernel at %08lx\n", image
->start
);
255 /* Make reboot code buffer available to the boot CPU. */
258 /* All secondary cpus now may jump to kexec_wait cycle */
259 relocated_kexec_smp_wait
= reboot_code_buffer
+
260 (void *)(kexec_smp_wait
- relocate_new_kernel
);
262 atomic_set(&kexec_ready_to_reboot
, 1);