blob: 35175eb2e8b04712d30e948411c6fcd2740da43d [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
176#ifdef CONFIG_SMP
177 if (smp_processor_id() > 0) {
178 /*
179 * Instead of cpu_relax() or wait, this is needed for kexec
180 * smp reboot. Kdump usually doesn't require an smp new
181 * kernel, but kexec may do.
182 */
183 kexec_nonboot_cpu();
184
185 /* NOTREACHED */
186 }
187#endif
188
189 /*
190 * Make sure we get correct instructions written by the
191 * machine_kexec() CPU.
192 */
193 local_flush_icache_range(reboot_code_buffer,
194 reboot_code_buffer + relocate_new_kernel_size);
195
196 do_kexec = (void *)reboot_code_buffer;
197 do_kexec();
198}
Ralf Baechle10659322007-07-31 15:16:32 +0100199
Nicolas Schichan583bb862006-10-18 15:14:55 +0200200void
201machine_kexec(struct kimage *image)
202{
Nicolas Schichan583bb862006-10-18 15:14:55 +0200203 unsigned long entry;
204 unsigned long *ptr;
205
206 reboot_code_buffer =
207 (unsigned long)page_address(image->control_code_page);
208
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200209 kexec_start_address =
210 (unsigned long) phys_to_virt(image->start);
211
Yang Wei91ffaa22014-07-31 19:42:29 +0800212 if (image->type == KEXEC_TYPE_DEFAULT) {
213 kexec_indirection_page =
214 (unsigned long) phys_to_virt(image->head & PAGE_MASK);
215 } else {
216 kexec_indirection_page = (unsigned long)&image->head;
217 }
Nicolas Schichan583bb862006-10-18 15:14:55 +0200218
219 memcpy((void*)reboot_code_buffer, relocate_new_kernel,
220 relocate_new_kernel_size);
221
222 /*
223 * The generic kexec code builds a page list with physical
224 * addresses. they are directly accessible through KSEG0 (or
225 * CKSEG0 or XPHYS if on 64bit system), hence the
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200226 * phys_to_virt() call.
Nicolas Schichan583bb862006-10-18 15:14:55 +0200227 */
228 for (ptr = &image->head; (entry = *ptr) && !(entry &IND_DONE);
229 ptr = (entry & IND_INDIRECTION) ?
230 phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
231 if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
232 *ptr & IND_DESTINATION)
Ralf Baechle10659322007-07-31 15:16:32 +0100233 *ptr = (unsigned long) phys_to_virt(*ptr);
Nicolas Schichan583bb862006-10-18 15:14:55 +0200234 }
235
Dengcheng Zhudc57aaf2018-09-11 14:49:20 -0700236 /* Mark offline BEFORE disabling local irq. */
237 set_cpu_online(smp_processor_id(), false);
238
Nicolas Schichan583bb862006-10-18 15:14:55 +0200239 /*
240 * we do not want to be bothered.
241 */
242 local_irq_disable();
243
Ralf Baechle10659322007-07-31 15:16:32 +0100244 printk("Will call new kernel at %08lx\n", image->start);
Nicolas Schichan583bb862006-10-18 15:14:55 +0200245 printk("Bye ...\n");
Dengcheng Zhu62cac482018-09-11 14:49:21 -0700246 /* Make reboot code buffer available to the boot CPU. */
Nicolas Schichan97ce9a82007-08-20 15:57:38 +0200247 __flush_cache_all();
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200248#ifdef CONFIG_SMP
249 /* All secondary cpus now may jump to kexec_wait cycle */
250 relocated_kexec_smp_wait = reboot_code_buffer +
251 (void *)(kexec_smp_wait - relocate_new_kernel);
252 smp_wmb();
253 atomic_set(&kexec_ready_to_reboot, 1);
254#endif
Dengcheng Zhu62cac482018-09-11 14:49:21 -0700255 kexec_reboot();
Nicolas Schichan583bb862006-10-18 15:14:55 +0200256}