blob: 93936dce04d65d606accb838009a1d62ebb1169e [file] [log] [blame]
Nicolas Schichan583bb862006-10-18 15:14:55 +02001/*
2 * machine_kexec.c for kexec
3 * Created by <nschichan@corp.free.fr> on Thu Oct 12 15:15:06 2006
4 *
5 * This source code is licensed under the GNU General Public License,
6 * Version 2. See the file COPYING for more details.
7 */
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +02008#include <linux/compiler.h>
Nicolas Schichan583bb862006-10-18 15:14:55 +02009#include <linux/kexec.h>
10#include <linux/mm.h>
11#include <linux/delay.h>
Dengcheng Zhu2fe8ea32018-09-11 14:49:24 -070012#include <linux/libfdt.h>
Nicolas Schichan583bb862006-10-18 15:14:55 +020013
14#include <asm/cacheflush.h>
15#include <asm/page.h>
16
Tobias Klauserc5a69d52007-02-17 20:11:19 +010017extern const unsigned char relocate_new_kernel[];
Ralf Baechle10659322007-07-31 15:16:32 +010018extern const size_t relocate_new_kernel_size;
Nicolas Schichan583bb862006-10-18 15:14:55 +020019
20extern unsigned long kexec_start_address;
21extern unsigned long kexec_indirection_page;
22
Dengcheng Zhu62cac482018-09-11 14:49:21 -070023static unsigned long reboot_code_buffer;
24
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020025#ifdef CONFIG_SMP
Dengcheng Zhu62cac482018-09-11 14:49:21 -070026static void (*relocated_kexec_smp_wait)(void *);
27
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020028atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0);
Hidehiro Kawai54c721b2016-10-11 13:54:26 -070029void (*_crash_smp_send_stop)(void) = NULL;
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020030#endif
31
Dengcheng Zhu62cac482018-09-11 14:49:21 -070032void (*_machine_kexec_shutdown)(void) = NULL;
33void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL;
34
Marcin Nowakowski856b0f52016-11-23 14:43:51 +010035static void kexec_image_info(const struct kimage *kimage)
36{
37 unsigned long i;
38
39 pr_debug("kexec kimage info:\n");
40 pr_debug(" type: %d\n", kimage->type);
41 pr_debug(" start: %lx\n", kimage->start);
42 pr_debug(" head: %lx\n", kimage->head);
43 pr_debug(" nr_segments: %lu\n", kimage->nr_segments);
44
45 for (i = 0; i < kimage->nr_segments; i++) {
46 pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
47 i,
48 kimage->segment[i].mem,
49 kimage->segment[i].mem + kimage->segment[i].memsz,
50 (unsigned long)kimage->segment[i].memsz,
51 (unsigned long)kimage->segment[i].memsz / PAGE_SIZE);
52 }
53}
54
Dengcheng Zhu2fe8ea32018-09-11 14:49:24 -070055#ifdef CONFIG_UHI_BOOT
56
57static int uhi_machine_kexec_prepare(struct kimage *kimage)
58{
59 int i;
60
61 /*
62 * In case DTB file is not passed to the new kernel, a flat device
63 * tree will be created by kexec tool. It holds modified command
64 * line for the new kernel.
65 */
66 for (i = 0; i < kimage->nr_segments; i++) {
67 struct fdt_header fdt;
68
69 if (kimage->segment[i].memsz <= sizeof(fdt))
70 continue;
71
72 if (copy_from_user(&fdt, kimage->segment[i].buf, sizeof(fdt)))
73 continue;
74
75 if (fdt_check_header(&fdt))
76 continue;
77
78 kexec_args[0] = -2;
79 kexec_args[1] = (unsigned long)
80 phys_to_virt((unsigned long)kimage->segment[i].mem);
81 break;
82 }
83
84 return 0;
85}
86
87int (*_machine_kexec_prepare)(struct kimage *) = uhi_machine_kexec_prepare;
88
89#else
90
91int (*_machine_kexec_prepare)(struct kimage *) = NULL;
92
93#endif /* CONFIG_UHI_BOOT */
94
Nicolas Schichan583bb862006-10-18 15:14:55 +020095int
96machine_kexec_prepare(struct kimage *kimage)
97{
Dengcheng Zhu62cac482018-09-11 14:49:21 -070098#ifdef CONFIG_SMP
99 if (!kexec_nonboot_cpu_func())
100 return -EINVAL;
101#endif
102
Marcin Nowakowski856b0f52016-11-23 14:43:51 +0100103 kexec_image_info(kimage);
104
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200105 if (_machine_kexec_prepare)
106 return _machine_kexec_prepare(kimage);
Dengcheng Zhu62cac482018-09-11 14:49:21 -0700107
Nicolas Schichan583bb862006-10-18 15:14:55 +0200108 return 0;
109}
110
111void
112machine_kexec_cleanup(struct kimage *kimage)
113{
114}
115
Dengcheng Zhu62cac482018-09-11 14:49:21 -0700116#ifdef CONFIG_SMP
117static void kexec_shutdown_secondary(void *param)
118{
119 int cpu = smp_processor_id();
120
121 if (!cpu_online(cpu))
122 return;
123
124 /* We won't be sent IPIs any more. */
125 set_cpu_online(cpu, false);
126
127 local_irq_disable();
128 while (!atomic_read(&kexec_ready_to_reboot))
129 cpu_relax();
130
131 kexec_reboot();
132
133 /* NOTREACHED */
134}
135#endif
136
Nicolas Schichan583bb862006-10-18 15:14:55 +0200137void
138machine_shutdown(void)
139{
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200140 if (_machine_kexec_shutdown)
141 _machine_kexec_shutdown();
Dengcheng Zhu62cac482018-09-11 14:49:21 -0700142
143#ifdef CONFIG_SMP
144 smp_call_function(kexec_shutdown_secondary, NULL, 0);
145
146 while (num_online_cpus() > 1) {
147 cpu_relax();
148 mdelay(1);
149 }
150#endif
Nicolas Schichan583bb862006-10-18 15:14:55 +0200151}
152
153void
154machine_crash_shutdown(struct pt_regs *regs)
155{
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200156 if (_machine_crash_shutdown)
157 _machine_crash_shutdown(regs);
158 else
159 default_machine_crash_shutdown(regs);
Nicolas Schichan583bb862006-10-18 15:14:55 +0200160}
161
Dengcheng Zhu62cac482018-09-11 14:49:21 -0700162#ifdef CONFIG_SMP
163void kexec_nonboot_cpu_jump(void)
164{
165 local_flush_icache_range((unsigned long)relocated_kexec_smp_wait,
166 reboot_code_buffer + relocate_new_kernel_size);
167
168 relocated_kexec_smp_wait(NULL);
169}
170#endif
171
172void kexec_reboot(void)
173{
174 void (*do_kexec)(void) __noreturn;
175
Dengcheng Zhu82689ac62018-09-26 19:49:10 +0000176 /*
177 * We know we were online, and there will be no incoming IPIs at
178 * this point. Mark online again before rebooting so that the crash
179 * analysis tool will see us correctly.
180 */
181 set_cpu_online(smp_processor_id(), true);
182
183 /* Ensure remote CPUs observe that we're online before rebooting. */
184 smp_mb__after_atomic();
185
Dengcheng Zhu62cac482018-09-11 14:49:21 -0700186#ifdef CONFIG_SMP
187 if (smp_processor_id() > 0) {
188 /*
189 * Instead of cpu_relax() or wait, this is needed for kexec
190 * smp reboot. Kdump usually doesn't require an smp new
191 * kernel, but kexec may do.
192 */
193 kexec_nonboot_cpu();
194
195 /* NOTREACHED */
196 }
197#endif
198
199 /*
200 * Make sure we get correct instructions written by the
201 * machine_kexec() CPU.
202 */
203 local_flush_icache_range(reboot_code_buffer,
204 reboot_code_buffer + relocate_new_kernel_size);
205
206 do_kexec = (void *)reboot_code_buffer;
207 do_kexec();
208}
Ralf Baechle10659322007-07-31 15:16:32 +0100209
Nicolas Schichan583bb862006-10-18 15:14:55 +0200210void
211machine_kexec(struct kimage *image)
212{
Nicolas Schichan583bb862006-10-18 15:14:55 +0200213 unsigned long entry;
214 unsigned long *ptr;
215
216 reboot_code_buffer =
217 (unsigned long)page_address(image->control_code_page);
218
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200219 kexec_start_address =
220 (unsigned long) phys_to_virt(image->start);
221
Yang Wei91ffaa22014-07-31 19:42:29 +0800222 if (image->type == KEXEC_TYPE_DEFAULT) {
223 kexec_indirection_page =
224 (unsigned long) phys_to_virt(image->head & PAGE_MASK);
225 } else {
226 kexec_indirection_page = (unsigned long)&image->head;
227 }
Nicolas Schichan583bb862006-10-18 15:14:55 +0200228
229 memcpy((void*)reboot_code_buffer, relocate_new_kernel,
230 relocate_new_kernel_size);
231
232 /*
233 * The generic kexec code builds a page list with physical
234 * addresses. they are directly accessible through KSEG0 (or
235 * CKSEG0 or XPHYS if on 64bit system), hence the
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200236 * phys_to_virt() call.
Nicolas Schichan583bb862006-10-18 15:14:55 +0200237 */
238 for (ptr = &image->head; (entry = *ptr) && !(entry &IND_DONE);
239 ptr = (entry & IND_INDIRECTION) ?
240 phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
241 if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
242 *ptr & IND_DESTINATION)
Ralf Baechle10659322007-07-31 15:16:32 +0100243 *ptr = (unsigned long) phys_to_virt(*ptr);
Nicolas Schichan583bb862006-10-18 15:14:55 +0200244 }
245
Dengcheng Zhudc57aaf2018-09-11 14:49:20 -0700246 /* Mark offline BEFORE disabling local irq. */
247 set_cpu_online(smp_processor_id(), false);
248
Nicolas Schichan583bb862006-10-18 15:14:55 +0200249 /*
250 * we do not want to be bothered.
251 */
252 local_irq_disable();
253
Ralf Baechle10659322007-07-31 15:16:32 +0100254 printk("Will call new kernel at %08lx\n", image->start);
Nicolas Schichan583bb862006-10-18 15:14:55 +0200255 printk("Bye ...\n");
Dengcheng Zhu62cac482018-09-11 14:49:21 -0700256 /* Make reboot code buffer available to the boot CPU. */
Nicolas Schichan97ce9a82007-08-20 15:57:38 +0200257 __flush_cache_all();
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200258#ifdef CONFIG_SMP
259 /* All secondary cpus now may jump to kexec_wait cycle */
260 relocated_kexec_smp_wait = reboot_code_buffer +
261 (void *)(kexec_smp_wait - relocate_new_kernel);
262 smp_wmb();
263 atomic_set(&kexec_ready_to_reboot, 1);
264#endif
Dengcheng Zhu62cac482018-09-11 14:49:21 -0700265 kexec_reboot();
Nicolas Schichan583bb862006-10-18 15:14:55 +0200266}