machine_kexec.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. */
  8. #include <linux/compiler.h>
  9. #include <linux/kexec.h>
  10. #include <linux/mm.h>
  11. #include <linux/delay.h>
  12. #include <linux/libfdt.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/page.h>
  15. extern const unsigned char relocate_new_kernel[];
  16. extern const size_t relocate_new_kernel_size;
  17. extern unsigned long kexec_start_address;
  18. extern unsigned long kexec_indirection_page;
  19. static unsigned long reboot_code_buffer;
  20. #ifdef CONFIG_SMP
  21. static void (*relocated_kexec_smp_wait)(void *);
  22. atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0);
  23. void (*_crash_smp_send_stop)(void) = NULL;
  24. #endif
  25. void (*_machine_kexec_shutdown)(void) = NULL;
  26. void (*_machine_crash_shutdown)(struct pt_regs *regs) = NULL;
  27. static void kexec_image_info(const struct kimage *kimage)
  28. {
  29. unsigned long i;
  30. pr_debug("kexec kimage info:\n");
  31. pr_debug(" type: %d\n", kimage->type);
  32. pr_debug(" start: %lx\n", kimage->start);
  33. pr_debug(" head: %lx\n", kimage->head);
  34. pr_debug(" nr_segments: %lu\n", kimage->nr_segments);
  35. for (i = 0; i < kimage->nr_segments; i++) {
  36. pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
  37. i,
  38. kimage->segment[i].mem,
  39. kimage->segment[i].mem + kimage->segment[i].memsz,
  40. (unsigned long)kimage->segment[i].memsz,
  41. (unsigned long)kimage->segment[i].memsz / PAGE_SIZE);
  42. }
  43. }
  44. #ifdef CONFIG_UHI_BOOT
  45. static int uhi_machine_kexec_prepare(struct kimage *kimage)
  46. {
  47. int i;
  48. /*
  49. * In case DTB file is not passed to the new kernel, a flat device
  50. * tree will be created by kexec tool. It holds modified command
  51. * line for the new kernel.
  52. */
  53. for (i = 0; i < kimage->nr_segments; i++) {
  54. struct fdt_header fdt;
  55. if (kimage->segment[i].memsz <= sizeof(fdt))
  56. continue;
  57. if (copy_from_user(&fdt, kimage->segment[i].buf, sizeof(fdt)))
  58. continue;
  59. if (fdt_check_header(&fdt))
  60. continue;
  61. kexec_args[0] = -2;
  62. kexec_args[1] = (unsigned long)
  63. phys_to_virt((unsigned long)kimage->segment[i].mem);
  64. break;
  65. }
  66. return 0;
  67. }
  68. int (*_machine_kexec_prepare)(struct kimage *) = uhi_machine_kexec_prepare;
  69. #else
  70. int (*_machine_kexec_prepare)(struct kimage *) = NULL;
  71. #endif /* CONFIG_UHI_BOOT */
  72. int
  73. machine_kexec_prepare(struct kimage *kimage)
  74. {
  75. #ifdef CONFIG_SMP
  76. if (!kexec_nonboot_cpu_func())
  77. return -EINVAL;
  78. #endif
  79. kexec_image_info(kimage);
  80. if (_machine_kexec_prepare)
  81. return _machine_kexec_prepare(kimage);
  82. return 0;
  83. }
  84. void
  85. machine_kexec_cleanup(struct kimage *kimage)
  86. {
  87. }
  88. #ifdef CONFIG_SMP
  89. static void kexec_shutdown_secondary(void *param)
  90. {
  91. int cpu = smp_processor_id();
  92. if (!cpu_online(cpu))
  93. return;
  94. /* We won't be sent IPIs any more. */
  95. set_cpu_online(cpu, false);
  96. local_irq_disable();
  97. while (!atomic_read(&kexec_ready_to_reboot))
  98. cpu_relax();
  99. kexec_reboot();
  100. /* NOTREACHED */
  101. }
  102. #endif
  103. void
  104. machine_shutdown(void)
  105. {
  106. if (_machine_kexec_shutdown)
  107. _machine_kexec_shutdown();
  108. #ifdef CONFIG_SMP
  109. smp_call_function(kexec_shutdown_secondary, NULL, 0);
  110. while (num_online_cpus() > 1) {
  111. cpu_relax();
  112. mdelay(1);
  113. }
  114. #endif
  115. }
  116. void
  117. machine_crash_shutdown(struct pt_regs *regs)
  118. {
  119. if (_machine_crash_shutdown)
  120. _machine_crash_shutdown(regs);
  121. else
  122. default_machine_crash_shutdown(regs);
  123. }
  124. #ifdef CONFIG_SMP
  125. void kexec_nonboot_cpu_jump(void)
  126. {
  127. local_flush_icache_range((unsigned long)relocated_kexec_smp_wait,
  128. reboot_code_buffer + relocate_new_kernel_size);
  129. relocated_kexec_smp_wait(NULL);
  130. }
  131. #endif
  132. void kexec_reboot(void)
  133. {
  134. void (*do_kexec)(void) __noreturn;
  135. /*
  136. * We know we were online, and there will be no incoming IPIs at
  137. * this point. Mark online again before rebooting so that the crash
  138. * analysis tool will see us correctly.
  139. */
  140. set_cpu_online(smp_processor_id(), true);
  141. /* Ensure remote CPUs observe that we're online before rebooting. */
  142. smp_mb__after_atomic();
  143. #ifdef CONFIG_SMP
  144. if (smp_processor_id() > 0) {
  145. /*
  146. * Instead of cpu_relax() or wait, this is needed for kexec
  147. * smp reboot. Kdump usually doesn't require an smp new
  148. * kernel, but kexec may do.
  149. */
  150. kexec_nonboot_cpu();
  151. /* NOTREACHED */
  152. }
  153. #endif
  154. /*
  155. * Make sure we get correct instructions written by the
  156. * machine_kexec() CPU.
  157. */
  158. local_flush_icache_range(reboot_code_buffer,
  159. reboot_code_buffer + relocate_new_kernel_size);
  160. do_kexec = (void *)reboot_code_buffer;
  161. do_kexec();
  162. }
  163. void
  164. machine_kexec(struct kimage *image)
  165. {
  166. unsigned long entry;
  167. unsigned long *ptr;
  168. reboot_code_buffer =
  169. (unsigned long)page_address(image->control_code_page);
  170. kexec_start_address =
  171. (unsigned long) phys_to_virt(image->start);
  172. 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. }
  178. memcpy((void*)reboot_code_buffer, relocate_new_kernel,
  179. relocate_new_kernel_size);
  180. /*
  181. * The generic kexec code builds a page list with physical
  182. * addresses. they are directly accessible through KSEG0 (or
  183. * CKSEG0 or XPHYS if on 64bit system), hence the
  184. * phys_to_virt() call.
  185. */
  186. for (ptr = &image->head; (entry = *ptr) && !(entry &IND_DONE);
  187. ptr = (entry & IND_INDIRECTION) ?
  188. phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
  189. if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
  190. *ptr & IND_DESTINATION)
  191. *ptr = (unsigned long) phys_to_virt(*ptr);
  192. }
  193. /* Mark offline BEFORE disabling local irq. */
  194. set_cpu_online(smp_processor_id(), false);
  195. /*
  196. * we do not want to be bothered.
  197. */
  198. local_irq_disable();
  199. printk("Will call new kernel at %08lx\n", image->start);
  200. printk("Bye ...\n");
  201. /* Make reboot code buffer available to the boot CPU. */
  202. __flush_cache_all();
  203. #ifdef CONFIG_SMP
  204. /* All secondary cpus now may jump to kexec_wait cycle */
  205. relocated_kexec_smp_wait = reboot_code_buffer +
  206. (void *)(kexec_smp_wait - relocate_new_kernel);
  207. smp_wmb();
  208. atomic_set(&kexec_ready_to_reboot, 1);
  209. #endif
  210. kexec_reboot();
  211. }