0001
0002
0003
0004
0005
0006 #include <linux/compiler.h>
0007 #include <linux/kexec.h>
0008 #include <linux/mm.h>
0009 #include <linux/delay.h>
0010 #include <linux/libfdt.h>
0011
0012 #include <asm/cacheflush.h>
0013 #include <asm/page.h>
0014
0015 extern const unsigned char relocate_new_kernel[];
0016 extern const size_t relocate_new_kernel_size;
0017
0018 extern unsigned long kexec_start_address;
0019 extern unsigned long kexec_indirection_page;
0020
0021 static unsigned long reboot_code_buffer;
0022
0023 #ifdef CONFIG_SMP
0024 static void (*relocated_kexec_smp_wait)(void *);
0025
0026 atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0);
0027 void (*_crash_smp_send_stop)(void) = NULL;
0028 #endif
0029
0030 void (*_machine_kexec_shutdown)(void) = NULL;
0031 void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL;
0032
0033 static void kexec_image_info(const struct kimage *kimage)
0034 {
0035 unsigned long i;
0036
0037 pr_debug("kexec kimage info:\n");
0038 pr_debug(" type: %d\n", kimage->type);
0039 pr_debug(" start: %lx\n", kimage->start);
0040 pr_debug(" head: %lx\n", kimage->head);
0041 pr_debug(" nr_segments: %lu\n", kimage->nr_segments);
0042
0043 for (i = 0; i < kimage->nr_segments; i++) {
0044 pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
0045 i,
0046 kimage->segment[i].mem,
0047 kimage->segment[i].mem + kimage->segment[i].memsz,
0048 (unsigned long)kimage->segment[i].memsz,
0049 (unsigned long)kimage->segment[i].memsz / PAGE_SIZE);
0050 }
0051 }
0052
0053 #ifdef CONFIG_UHI_BOOT
0054
0055 static int uhi_machine_kexec_prepare(struct kimage *kimage)
0056 {
0057 int i;
0058
0059
0060
0061
0062
0063
0064 for (i = 0; i < kimage->nr_segments; i++) {
0065 struct fdt_header fdt;
0066
0067 if (kimage->segment[i].memsz <= sizeof(fdt))
0068 continue;
0069
0070 if (copy_from_user(&fdt, kimage->segment[i].buf, sizeof(fdt)))
0071 continue;
0072
0073 if (fdt_check_header(&fdt))
0074 continue;
0075
0076 kexec_args[0] = -2;
0077 kexec_args[1] = (unsigned long)
0078 phys_to_virt((unsigned long)kimage->segment[i].mem);
0079 break;
0080 }
0081
0082 return 0;
0083 }
0084
0085 int (*_machine_kexec_prepare)(struct kimage *) = uhi_machine_kexec_prepare;
0086
0087 #else
0088
0089 int (*_machine_kexec_prepare)(struct kimage *) = NULL;
0090
0091 #endif
0092
0093 int
0094 machine_kexec_prepare(struct kimage *kimage)
0095 {
0096 #ifdef CONFIG_SMP
0097 if (!kexec_nonboot_cpu_func())
0098 return -EINVAL;
0099 #endif
0100
0101 kexec_image_info(kimage);
0102
0103 if (_machine_kexec_prepare)
0104 return _machine_kexec_prepare(kimage);
0105
0106 return 0;
0107 }
0108
0109 void
0110 machine_kexec_cleanup(struct kimage *kimage)
0111 {
0112 }
0113
0114 #ifdef CONFIG_SMP
0115 static void kexec_shutdown_secondary(void *param)
0116 {
0117 int cpu = smp_processor_id();
0118
0119 if (!cpu_online(cpu))
0120 return;
0121
0122
0123 set_cpu_online(cpu, false);
0124
0125 local_irq_disable();
0126 while (!atomic_read(&kexec_ready_to_reboot))
0127 cpu_relax();
0128
0129 kexec_reboot();
0130
0131
0132 }
0133 #endif
0134
0135 void
0136 machine_shutdown(void)
0137 {
0138 if (_machine_kexec_shutdown)
0139 _machine_kexec_shutdown();
0140
0141 #ifdef CONFIG_SMP
0142 smp_call_function(kexec_shutdown_secondary, NULL, 0);
0143
0144 while (num_online_cpus() > 1) {
0145 cpu_relax();
0146 mdelay(1);
0147 }
0148 #endif
0149 }
0150
0151 void
0152 machine_crash_shutdown(struct pt_regs *regs)
0153 {
0154 if (_machine_crash_shutdown)
0155 _machine_crash_shutdown(regs);
0156 else
0157 default_machine_crash_shutdown(regs);
0158 }
0159
0160 #ifdef CONFIG_SMP
0161 void kexec_nonboot_cpu_jump(void)
0162 {
0163 local_flush_icache_range((unsigned long)relocated_kexec_smp_wait,
0164 reboot_code_buffer + relocate_new_kernel_size);
0165
0166 relocated_kexec_smp_wait(NULL);
0167 }
0168 #endif
0169
0170 void kexec_reboot(void)
0171 {
0172 void (*do_kexec)(void) __noreturn;
0173
0174
0175
0176
0177
0178
0179 set_cpu_online(smp_processor_id(), true);
0180
0181
0182 smp_mb__after_atomic();
0183
0184 #ifdef CONFIG_SMP
0185 if (smp_processor_id() > 0) {
0186
0187
0188
0189
0190
0191 kexec_nonboot_cpu();
0192
0193
0194 }
0195 #endif
0196
0197
0198
0199
0200
0201 local_flush_icache_range(reboot_code_buffer,
0202 reboot_code_buffer + relocate_new_kernel_size);
0203
0204 do_kexec = (void *)reboot_code_buffer;
0205 do_kexec();
0206 }
0207
0208 void
0209 machine_kexec(struct kimage *image)
0210 {
0211 unsigned long entry;
0212 unsigned long *ptr;
0213
0214 reboot_code_buffer =
0215 (unsigned long)page_address(image->control_code_page);
0216
0217 kexec_start_address =
0218 (unsigned long) phys_to_virt(image->start);
0219
0220 if (image->type == KEXEC_TYPE_DEFAULT) {
0221 kexec_indirection_page =
0222 (unsigned long) phys_to_virt(image->head & PAGE_MASK);
0223 } else {
0224 kexec_indirection_page = (unsigned long)&image->head;
0225 }
0226
0227 memcpy((void*)reboot_code_buffer, relocate_new_kernel,
0228 relocate_new_kernel_size);
0229
0230
0231
0232
0233
0234
0235
0236 for (ptr = &image->head; (entry = *ptr) && !(entry &IND_DONE);
0237 ptr = (entry & IND_INDIRECTION) ?
0238 phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
0239 if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
0240 *ptr & IND_DESTINATION)
0241 *ptr = (unsigned long) phys_to_virt(*ptr);
0242 }
0243
0244
0245 set_cpu_online(smp_processor_id(), false);
0246
0247
0248
0249
0250 local_irq_disable();
0251
0252 printk("Will call new kernel at %08lx\n", image->start);
0253 printk("Bye ...\n");
0254
0255 __flush_cache_all();
0256 #ifdef CONFIG_SMP
0257
0258 relocated_kexec_smp_wait = reboot_code_buffer +
0259 (void *)(kexec_smp_wait - relocate_new_kernel);
0260 smp_wmb();
0261 atomic_set(&kexec_ready_to_reboot, 1);
0262 #endif
0263 kexec_reboot();
0264 }