| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * machine_kexec.c for kexec |
|---|
| 3 | 4 | * 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 | 5 | */ |
|---|
| 8 | 6 | #include <linux/compiler.h> |
|---|
| 9 | 7 | #include <linux/kexec.h> |
|---|
| 10 | 8 | #include <linux/mm.h> |
|---|
| 11 | 9 | #include <linux/delay.h> |
|---|
| 10 | +#include <linux/libfdt.h> |
|---|
| 12 | 11 | |
|---|
| 13 | 12 | #include <asm/cacheflush.h> |
|---|
| 14 | 13 | #include <asm/page.h> |
|---|
| .. | .. |
|---|
| 19 | 18 | extern unsigned long kexec_start_address; |
|---|
| 20 | 19 | extern unsigned long kexec_indirection_page; |
|---|
| 21 | 20 | |
|---|
| 22 | | -int (*_machine_kexec_prepare)(struct kimage *) = NULL; |
|---|
| 23 | | -void (*_machine_kexec_shutdown)(void) = NULL; |
|---|
| 24 | | -void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL; |
|---|
| 21 | +static unsigned long reboot_code_buffer; |
|---|
| 22 | + |
|---|
| 25 | 23 | #ifdef CONFIG_SMP |
|---|
| 26 | | -void (*relocated_kexec_smp_wait) (void *); |
|---|
| 24 | +static void (*relocated_kexec_smp_wait)(void *); |
|---|
| 25 | + |
|---|
| 27 | 26 | atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0); |
|---|
| 28 | 27 | void (*_crash_smp_send_stop)(void) = NULL; |
|---|
| 29 | 28 | #endif |
|---|
| 29 | + |
|---|
| 30 | +void (*_machine_kexec_shutdown)(void) = NULL; |
|---|
| 31 | +void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL; |
|---|
| 30 | 32 | |
|---|
| 31 | 33 | static void kexec_image_info(const struct kimage *kimage) |
|---|
| 32 | 34 | { |
|---|
| .. | .. |
|---|
| 48 | 50 | } |
|---|
| 49 | 51 | } |
|---|
| 50 | 52 | |
|---|
| 53 | +#ifdef CONFIG_UHI_BOOT |
|---|
| 54 | + |
|---|
| 55 | +static int uhi_machine_kexec_prepare(struct kimage *kimage) |
|---|
| 56 | +{ |
|---|
| 57 | + int i; |
|---|
| 58 | + |
|---|
| 59 | + /* |
|---|
| 60 | + * In case DTB file is not passed to the new kernel, a flat device |
|---|
| 61 | + * tree will be created by kexec tool. It holds modified command |
|---|
| 62 | + * line for the new kernel. |
|---|
| 63 | + */ |
|---|
| 64 | + for (i = 0; i < kimage->nr_segments; i++) { |
|---|
| 65 | + struct fdt_header fdt; |
|---|
| 66 | + |
|---|
| 67 | + if (kimage->segment[i].memsz <= sizeof(fdt)) |
|---|
| 68 | + continue; |
|---|
| 69 | + |
|---|
| 70 | + if (copy_from_user(&fdt, kimage->segment[i].buf, sizeof(fdt))) |
|---|
| 71 | + continue; |
|---|
| 72 | + |
|---|
| 73 | + if (fdt_check_header(&fdt)) |
|---|
| 74 | + continue; |
|---|
| 75 | + |
|---|
| 76 | + kexec_args[0] = -2; |
|---|
| 77 | + kexec_args[1] = (unsigned long) |
|---|
| 78 | + phys_to_virt((unsigned long)kimage->segment[i].mem); |
|---|
| 79 | + break; |
|---|
| 80 | + } |
|---|
| 81 | + |
|---|
| 82 | + return 0; |
|---|
| 83 | +} |
|---|
| 84 | + |
|---|
| 85 | +int (*_machine_kexec_prepare)(struct kimage *) = uhi_machine_kexec_prepare; |
|---|
| 86 | + |
|---|
| 87 | +#else |
|---|
| 88 | + |
|---|
| 89 | +int (*_machine_kexec_prepare)(struct kimage *) = NULL; |
|---|
| 90 | + |
|---|
| 91 | +#endif /* CONFIG_UHI_BOOT */ |
|---|
| 92 | + |
|---|
| 51 | 93 | int |
|---|
| 52 | 94 | machine_kexec_prepare(struct kimage *kimage) |
|---|
| 53 | 95 | { |
|---|
| 96 | +#ifdef CONFIG_SMP |
|---|
| 97 | + if (!kexec_nonboot_cpu_func()) |
|---|
| 98 | + return -EINVAL; |
|---|
| 99 | +#endif |
|---|
| 100 | + |
|---|
| 54 | 101 | kexec_image_info(kimage); |
|---|
| 55 | 102 | |
|---|
| 56 | 103 | if (_machine_kexec_prepare) |
|---|
| 57 | 104 | return _machine_kexec_prepare(kimage); |
|---|
| 105 | + |
|---|
| 58 | 106 | return 0; |
|---|
| 59 | 107 | } |
|---|
| 60 | 108 | |
|---|
| .. | .. |
|---|
| 63 | 111 | { |
|---|
| 64 | 112 | } |
|---|
| 65 | 113 | |
|---|
| 114 | +#ifdef CONFIG_SMP |
|---|
| 115 | +static void kexec_shutdown_secondary(void *param) |
|---|
| 116 | +{ |
|---|
| 117 | + int cpu = smp_processor_id(); |
|---|
| 118 | + |
|---|
| 119 | + if (!cpu_online(cpu)) |
|---|
| 120 | + return; |
|---|
| 121 | + |
|---|
| 122 | + /* We won't be sent IPIs any more. */ |
|---|
| 123 | + set_cpu_online(cpu, false); |
|---|
| 124 | + |
|---|
| 125 | + local_irq_disable(); |
|---|
| 126 | + while (!atomic_read(&kexec_ready_to_reboot)) |
|---|
| 127 | + cpu_relax(); |
|---|
| 128 | + |
|---|
| 129 | + kexec_reboot(); |
|---|
| 130 | + |
|---|
| 131 | + /* NOTREACHED */ |
|---|
| 132 | +} |
|---|
| 133 | +#endif |
|---|
| 134 | + |
|---|
| 66 | 135 | void |
|---|
| 67 | 136 | machine_shutdown(void) |
|---|
| 68 | 137 | { |
|---|
| 69 | 138 | if (_machine_kexec_shutdown) |
|---|
| 70 | 139 | _machine_kexec_shutdown(); |
|---|
| 140 | + |
|---|
| 141 | +#ifdef CONFIG_SMP |
|---|
| 142 | + smp_call_function(kexec_shutdown_secondary, NULL, 0); |
|---|
| 143 | + |
|---|
| 144 | + while (num_online_cpus() > 1) { |
|---|
| 145 | + cpu_relax(); |
|---|
| 146 | + mdelay(1); |
|---|
| 147 | + } |
|---|
| 148 | +#endif |
|---|
| 71 | 149 | } |
|---|
| 72 | 150 | |
|---|
| 73 | 151 | void |
|---|
| .. | .. |
|---|
| 79 | 157 | default_machine_crash_shutdown(regs); |
|---|
| 80 | 158 | } |
|---|
| 81 | 159 | |
|---|
| 82 | | -typedef void (*noretfun_t)(void) __noreturn; |
|---|
| 160 | +#ifdef CONFIG_SMP |
|---|
| 161 | +void kexec_nonboot_cpu_jump(void) |
|---|
| 162 | +{ |
|---|
| 163 | + local_flush_icache_range((unsigned long)relocated_kexec_smp_wait, |
|---|
| 164 | + reboot_code_buffer + relocate_new_kernel_size); |
|---|
| 165 | + |
|---|
| 166 | + relocated_kexec_smp_wait(NULL); |
|---|
| 167 | +} |
|---|
| 168 | +#endif |
|---|
| 169 | + |
|---|
| 170 | +void kexec_reboot(void) |
|---|
| 171 | +{ |
|---|
| 172 | + void (*do_kexec)(void) __noreturn; |
|---|
| 173 | + |
|---|
| 174 | + /* |
|---|
| 175 | + * We know we were online, and there will be no incoming IPIs at |
|---|
| 176 | + * this point. Mark online again before rebooting so that the crash |
|---|
| 177 | + * analysis tool will see us correctly. |
|---|
| 178 | + */ |
|---|
| 179 | + set_cpu_online(smp_processor_id(), true); |
|---|
| 180 | + |
|---|
| 181 | + /* Ensure remote CPUs observe that we're online before rebooting. */ |
|---|
| 182 | + smp_mb__after_atomic(); |
|---|
| 183 | + |
|---|
| 184 | +#ifdef CONFIG_SMP |
|---|
| 185 | + if (smp_processor_id() > 0) { |
|---|
| 186 | + /* |
|---|
| 187 | + * Instead of cpu_relax() or wait, this is needed for kexec |
|---|
| 188 | + * smp reboot. Kdump usually doesn't require an smp new |
|---|
| 189 | + * kernel, but kexec may do. |
|---|
| 190 | + */ |
|---|
| 191 | + kexec_nonboot_cpu(); |
|---|
| 192 | + |
|---|
| 193 | + /* NOTREACHED */ |
|---|
| 194 | + } |
|---|
| 195 | +#endif |
|---|
| 196 | + |
|---|
| 197 | + /* |
|---|
| 198 | + * Make sure we get correct instructions written by the |
|---|
| 199 | + * machine_kexec() CPU. |
|---|
| 200 | + */ |
|---|
| 201 | + local_flush_icache_range(reboot_code_buffer, |
|---|
| 202 | + reboot_code_buffer + relocate_new_kernel_size); |
|---|
| 203 | + |
|---|
| 204 | + do_kexec = (void *)reboot_code_buffer; |
|---|
| 205 | + do_kexec(); |
|---|
| 206 | +} |
|---|
| 83 | 207 | |
|---|
| 84 | 208 | void |
|---|
| 85 | 209 | machine_kexec(struct kimage *image) |
|---|
| 86 | 210 | { |
|---|
| 87 | | - unsigned long reboot_code_buffer; |
|---|
| 88 | 211 | unsigned long entry; |
|---|
| 89 | 212 | unsigned long *ptr; |
|---|
| 90 | 213 | |
|---|
| .. | .. |
|---|
| 128 | 251 | |
|---|
| 129 | 252 | printk("Will call new kernel at %08lx\n", image->start); |
|---|
| 130 | 253 | printk("Bye ...\n"); |
|---|
| 254 | + /* Make reboot code buffer available to the boot CPU. */ |
|---|
| 131 | 255 | __flush_cache_all(); |
|---|
| 132 | 256 | #ifdef CONFIG_SMP |
|---|
| 133 | 257 | /* All secondary cpus now may jump to kexec_wait cycle */ |
|---|
| .. | .. |
|---|
| 136 | 260 | smp_wmb(); |
|---|
| 137 | 261 | atomic_set(&kexec_ready_to_reboot, 1); |
|---|
| 138 | 262 | #endif |
|---|
| 139 | | - ((noretfun_t) reboot_code_buffer)(); |
|---|
| 263 | + kexec_reboot(); |
|---|
| 140 | 264 | } |
|---|