blob: 14ced77c5ef6b7aab3d5821910f96a868baa3c73 [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>
12
13#include <asm/cacheflush.h>
14#include <asm/page.h>
15
Tobias Klauserc5a69d52007-02-17 20:11:19 +010016extern const unsigned char relocate_new_kernel[];
Ralf Baechle10659322007-07-31 15:16:32 +010017extern const size_t relocate_new_kernel_size;
Nicolas Schichan583bb862006-10-18 15:14:55 +020018
19extern unsigned long kexec_start_address;
20extern unsigned long kexec_indirection_page;
21
Dengcheng Zhu62cac482018-09-11 14:49:21 -070022static unsigned long reboot_code_buffer;
23
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020024#ifdef CONFIG_SMP
Dengcheng Zhu62cac482018-09-11 14:49:21 -070025static void (*relocated_kexec_smp_wait)(void *);
26
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020027atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0);
Hidehiro Kawai54c721b2016-10-11 13:54:26 -070028void (*_crash_smp_send_stop)(void) = NULL;
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020029#endif
30
Dengcheng Zhu62cac482018-09-11 14:49:21 -070031int (*_machine_kexec_prepare)(struct kimage *) = NULL;
32void (*_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
Nicolas Schichan583bb862006-10-18 15:14:55 +020055int
56machine_kexec_prepare(struct kimage *kimage)
57{
Dengcheng Zhu62cac482018-09-11 14:49:21 -070058#ifdef CONFIG_SMP
59 if (!kexec_nonboot_cpu_func())
60 return -EINVAL;
61#endif
62
Marcin Nowakowski856b0f52016-11-23 14:43:51 +010063 kexec_image_info(kimage);
64
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +020065 if (_machine_kexec_prepare)
66 return _machine_kexec_prepare(kimage);
Dengcheng Zhu62cac482018-09-11 14:49:21 -070067
Nicolas Schichan583bb862006-10-18 15:14:55 +020068 return 0;
69}
70
71void
72machine_kexec_cleanup(struct kimage *kimage)
73{
74}
75
Dengcheng Zhu62cac482018-09-11 14:49:21 -070076#ifdef CONFIG_SMP
77static void kexec_shutdown_secondary(void *param)
78{
79 int cpu = smp_processor_id();
80
81 if (!cpu_online(cpu))
82 return;
83
84 /* We won't be sent IPIs any more. */
85 set_cpu_online(cpu, false);
86
87 local_irq_disable();
88 while (!atomic_read(&kexec_ready_to_reboot))
89 cpu_relax();
90
91 kexec_reboot();
92
93 /* NOTREACHED */
94}
95#endif
96
Nicolas Schichan583bb862006-10-18 15:14:55 +020097void
98machine_shutdown(void)
99{
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200100 if (_machine_kexec_shutdown)
101 _machine_kexec_shutdown();
Dengcheng Zhu62cac482018-09-11 14:49:21 -0700102
103#ifdef CONFIG_SMP
104 smp_call_function(kexec_shutdown_secondary, NULL, 0);
105
106 while (num_online_cpus() > 1) {
107 cpu_relax();
108 mdelay(1);
109 }
110#endif
Nicolas Schichan583bb862006-10-18 15:14:55 +0200111}
112
113void
114machine_crash_shutdown(struct pt_regs *regs)
115{
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200116 if (_machine_crash_shutdown)
117 _machine_crash_shutdown(regs);
118 else
119 default_machine_crash_shutdown(regs);
Nicolas Schichan583bb862006-10-18 15:14:55 +0200120}
121
Dengcheng Zhu62cac482018-09-11 14:49:21 -0700122#ifdef CONFIG_SMP
123void kexec_nonboot_cpu_jump(void)
124{
125 local_flush_icache_range((unsigned long)relocated_kexec_smp_wait,
126 reboot_code_buffer + relocate_new_kernel_size);
127
128 relocated_kexec_smp_wait(NULL);
129}
130#endif
131
132void kexec_reboot(void)
133{
134 void (*do_kexec)(void) __noreturn;
135
136#ifdef CONFIG_SMP
137 if (smp_processor_id() > 0) {
138 /*
139 * Instead of cpu_relax() or wait, this is needed for kexec
140 * smp reboot. Kdump usually doesn't require an smp new
141 * kernel, but kexec may do.
142 */
143 kexec_nonboot_cpu();
144
145 /* NOTREACHED */
146 }
147#endif
148
149 /*
150 * Make sure we get correct instructions written by the
151 * machine_kexec() CPU.
152 */
153 local_flush_icache_range(reboot_code_buffer,
154 reboot_code_buffer + relocate_new_kernel_size);
155
156 do_kexec = (void *)reboot_code_buffer;
157 do_kexec();
158}
Ralf Baechle10659322007-07-31 15:16:32 +0100159
Nicolas Schichan583bb862006-10-18 15:14:55 +0200160void
161machine_kexec(struct kimage *image)
162{
Nicolas Schichan583bb862006-10-18 15:14:55 +0200163 unsigned long entry;
164 unsigned long *ptr;
165
166 reboot_code_buffer =
167 (unsigned long)page_address(image->control_code_page);
168
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200169 kexec_start_address =
170 (unsigned long) phys_to_virt(image->start);
171
Yang Wei91ffaa22014-07-31 19:42:29 +0800172 if (image->type == KEXEC_TYPE_DEFAULT) {
173 kexec_indirection_page =
174 (unsigned long) phys_to_virt(image->head & PAGE_MASK);
175 } else {
176 kexec_indirection_page = (unsigned long)&image->head;
177 }
Nicolas Schichan583bb862006-10-18 15:14:55 +0200178
179 memcpy((void*)reboot_code_buffer, relocate_new_kernel,
180 relocate_new_kernel_size);
181
182 /*
183 * The generic kexec code builds a page list with physical
184 * addresses. they are directly accessible through KSEG0 (or
185 * CKSEG0 or XPHYS if on 64bit system), hence the
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200186 * phys_to_virt() call.
Nicolas Schichan583bb862006-10-18 15:14:55 +0200187 */
188 for (ptr = &image->head; (entry = *ptr) && !(entry &IND_DONE);
189 ptr = (entry & IND_INDIRECTION) ?
190 phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
191 if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
192 *ptr & IND_DESTINATION)
Ralf Baechle10659322007-07-31 15:16:32 +0100193 *ptr = (unsigned long) phys_to_virt(*ptr);
Nicolas Schichan583bb862006-10-18 15:14:55 +0200194 }
195
Dengcheng Zhudc57aaf2018-09-11 14:49:20 -0700196 /* Mark offline BEFORE disabling local irq. */
197 set_cpu_online(smp_processor_id(), false);
198
Nicolas Schichan583bb862006-10-18 15:14:55 +0200199 /*
200 * we do not want to be bothered.
201 */
202 local_irq_disable();
203
Ralf Baechle10659322007-07-31 15:16:32 +0100204 printk("Will call new kernel at %08lx\n", image->start);
Nicolas Schichan583bb862006-10-18 15:14:55 +0200205 printk("Bye ...\n");
Dengcheng Zhu62cac482018-09-11 14:49:21 -0700206 /* Make reboot code buffer available to the boot CPU. */
Nicolas Schichan97ce9a82007-08-20 15:57:38 +0200207 __flush_cache_all();
Ralf Baechle7aa1c8f2012-10-11 18:14:58 +0200208#ifdef CONFIG_SMP
209 /* All secondary cpus now may jump to kexec_wait cycle */
210 relocated_kexec_smp_wait = reboot_code_buffer +
211 (void *)(kexec_smp_wait - relocate_new_kernel);
212 smp_wmb();
213 atomic_set(&kexec_ready_to_reboot, 1);
214#endif
Dengcheng Zhu62cac482018-09-11 14:49:21 -0700215 kexec_reboot();
Nicolas Schichan583bb862006-10-18 15:14:55 +0200216}