Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 1 | /* |
| 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 Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 8 | #include <linux/compiler.h> |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 9 | #include <linux/kexec.h> |
| 10 | #include <linux/mm.h> |
| 11 | #include <linux/delay.h> |
Dengcheng Zhu | 2fe8ea3 | 2018-09-11 14:49:24 -0700 | [diff] [blame^] | 12 | #include <linux/libfdt.h> |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 13 | |
| 14 | #include <asm/cacheflush.h> |
| 15 | #include <asm/page.h> |
| 16 | |
Tobias Klauser | c5a69d5 | 2007-02-17 20:11:19 +0100 | [diff] [blame] | 17 | extern const unsigned char relocate_new_kernel[]; |
Ralf Baechle | 1065932 | 2007-07-31 15:16:32 +0100 | [diff] [blame] | 18 | extern const size_t relocate_new_kernel_size; |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 19 | |
| 20 | extern unsigned long kexec_start_address; |
| 21 | extern unsigned long kexec_indirection_page; |
| 22 | |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame] | 23 | static unsigned long reboot_code_buffer; |
| 24 | |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 25 | #ifdef CONFIG_SMP |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame] | 26 | static void (*relocated_kexec_smp_wait)(void *); |
| 27 | |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 28 | atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0); |
Hidehiro Kawai | 54c721b | 2016-10-11 13:54:26 -0700 | [diff] [blame] | 29 | void (*_crash_smp_send_stop)(void) = NULL; |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 30 | #endif |
| 31 | |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame] | 32 | void (*_machine_kexec_shutdown)(void) = NULL; |
| 33 | void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL; |
| 34 | |
Marcin Nowakowski | 856b0f5 | 2016-11-23 14:43:51 +0100 | [diff] [blame] | 35 | static 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 Zhu | 2fe8ea3 | 2018-09-11 14:49:24 -0700 | [diff] [blame^] | 55 | #ifdef CONFIG_UHI_BOOT |
| 56 | |
| 57 | static 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 | |
| 87 | int (*_machine_kexec_prepare)(struct kimage *) = uhi_machine_kexec_prepare; |
| 88 | |
| 89 | #else |
| 90 | |
| 91 | int (*_machine_kexec_prepare)(struct kimage *) = NULL; |
| 92 | |
| 93 | #endif /* CONFIG_UHI_BOOT */ |
| 94 | |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 95 | int |
| 96 | machine_kexec_prepare(struct kimage *kimage) |
| 97 | { |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame] | 98 | #ifdef CONFIG_SMP |
| 99 | if (!kexec_nonboot_cpu_func()) |
| 100 | return -EINVAL; |
| 101 | #endif |
| 102 | |
Marcin Nowakowski | 856b0f5 | 2016-11-23 14:43:51 +0100 | [diff] [blame] | 103 | kexec_image_info(kimage); |
| 104 | |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 105 | if (_machine_kexec_prepare) |
| 106 | return _machine_kexec_prepare(kimage); |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame] | 107 | |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | machine_kexec_cleanup(struct kimage *kimage) |
| 113 | { |
| 114 | } |
| 115 | |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame] | 116 | #ifdef CONFIG_SMP |
| 117 | static 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 Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 137 | void |
| 138 | machine_shutdown(void) |
| 139 | { |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 140 | if (_machine_kexec_shutdown) |
| 141 | _machine_kexec_shutdown(); |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame] | 142 | |
| 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 Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void |
| 154 | machine_crash_shutdown(struct pt_regs *regs) |
| 155 | { |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 156 | if (_machine_crash_shutdown) |
| 157 | _machine_crash_shutdown(regs); |
| 158 | else |
| 159 | default_machine_crash_shutdown(regs); |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame] | 162 | #ifdef CONFIG_SMP |
| 163 | void 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 | |
| 172 | void 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 Baechle | 1065932 | 2007-07-31 15:16:32 +0100 | [diff] [blame] | 199 | |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 200 | void |
| 201 | machine_kexec(struct kimage *image) |
| 202 | { |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 203 | unsigned long entry; |
| 204 | unsigned long *ptr; |
| 205 | |
| 206 | reboot_code_buffer = |
| 207 | (unsigned long)page_address(image->control_code_page); |
| 208 | |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 209 | kexec_start_address = |
| 210 | (unsigned long) phys_to_virt(image->start); |
| 211 | |
Yang Wei | 91ffaa2 | 2014-07-31 19:42:29 +0800 | [diff] [blame] | 212 | 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 Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 218 | |
| 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 Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 226 | * phys_to_virt() call. |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 227 | */ |
| 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 Baechle | 1065932 | 2007-07-31 15:16:32 +0100 | [diff] [blame] | 233 | *ptr = (unsigned long) phys_to_virt(*ptr); |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Dengcheng Zhu | dc57aaf | 2018-09-11 14:49:20 -0700 | [diff] [blame] | 236 | /* Mark offline BEFORE disabling local irq. */ |
| 237 | set_cpu_online(smp_processor_id(), false); |
| 238 | |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 239 | /* |
| 240 | * we do not want to be bothered. |
| 241 | */ |
| 242 | local_irq_disable(); |
| 243 | |
Ralf Baechle | 1065932 | 2007-07-31 15:16:32 +0100 | [diff] [blame] | 244 | printk("Will call new kernel at %08lx\n", image->start); |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 245 | printk("Bye ...\n"); |
Dengcheng Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame] | 246 | /* Make reboot code buffer available to the boot CPU. */ |
Nicolas Schichan | 97ce9a8 | 2007-08-20 15:57:38 +0200 | [diff] [blame] | 247 | __flush_cache_all(); |
Ralf Baechle | 7aa1c8f | 2012-10-11 18:14:58 +0200 | [diff] [blame] | 248 | #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 Zhu | 62cac48 | 2018-09-11 14:49:21 -0700 | [diff] [blame] | 255 | kexec_reboot(); |
Nicolas Schichan | 583bb86 | 2006-10-18 15:14:55 +0200 | [diff] [blame] | 256 | } |