machine_kexec.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * machine_kexec.c - handle transition of Linux booting another kernel
  4. * Copyright (C) 2002-2003 Eric Biederman <[email protected]>
  5. *
  6. * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz
  7. * LANDISK/sh4 supported by kogiidena
  8. */
  9. #include <linux/mm.h>
  10. #include <linux/kexec.h>
  11. #include <linux/delay.h>
  12. #include <linux/reboot.h>
  13. #include <linux/numa.h>
  14. #include <linux/ftrace.h>
  15. #include <linux/suspend.h>
  16. #include <linux/memblock.h>
  17. #include <asm/mmu_context.h>
  18. #include <asm/io.h>
  19. #include <asm/cacheflush.h>
  20. #include <asm/sh_bios.h>
  21. #include <asm/reboot.h>
  22. typedef void (*relocate_new_kernel_t)(unsigned long indirection_page,
  23. unsigned long reboot_code_buffer,
  24. unsigned long start_address);
  25. extern const unsigned char relocate_new_kernel[];
  26. extern const unsigned int relocate_new_kernel_size;
  27. extern void *vbr_base;
  28. void native_machine_crash_shutdown(struct pt_regs *regs)
  29. {
  30. /* Nothing to do for UP, but definitely broken for SMP.. */
  31. }
  32. /*
  33. * Do what every setup is needed on image and the
  34. * reboot code buffer to allow us to avoid allocations
  35. * later.
  36. */
  37. int machine_kexec_prepare(struct kimage *image)
  38. {
  39. return 0;
  40. }
  41. void machine_kexec_cleanup(struct kimage *image)
  42. {
  43. }
  44. static void kexec_info(struct kimage *image)
  45. {
  46. int i;
  47. printk("kexec information\n");
  48. for (i = 0; i < image->nr_segments; i++) {
  49. printk(" segment[%d]: 0x%08x - 0x%08x (0x%08x)\n",
  50. i,
  51. (unsigned int)image->segment[i].mem,
  52. (unsigned int)image->segment[i].mem +
  53. image->segment[i].memsz,
  54. (unsigned int)image->segment[i].memsz);
  55. }
  56. printk(" start : 0x%08x\n\n", (unsigned int)image->start);
  57. }
  58. /*
  59. * Do not allocate memory (or fail in any way) in machine_kexec().
  60. * We are past the point of no return, committed to rebooting now.
  61. */
  62. void machine_kexec(struct kimage *image)
  63. {
  64. unsigned long page_list;
  65. unsigned long reboot_code_buffer;
  66. relocate_new_kernel_t rnk;
  67. unsigned long entry;
  68. unsigned long *ptr;
  69. int save_ftrace_enabled;
  70. /*
  71. * Nicked from the mips version of machine_kexec():
  72. * The generic kexec code builds a page list with physical
  73. * addresses. Use phys_to_virt() to convert them to virtual.
  74. */
  75. for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE);
  76. ptr = (entry & IND_INDIRECTION) ?
  77. phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
  78. if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
  79. *ptr & IND_DESTINATION)
  80. *ptr = (unsigned long) phys_to_virt(*ptr);
  81. }
  82. #ifdef CONFIG_KEXEC_JUMP
  83. if (image->preserve_context)
  84. save_processor_state();
  85. #endif
  86. save_ftrace_enabled = __ftrace_enabled_save();
  87. /* Interrupts aren't acceptable while we reboot */
  88. local_irq_disable();
  89. page_list = image->head;
  90. /* we need both effective and real address here */
  91. reboot_code_buffer =
  92. (unsigned long)page_address(image->control_code_page);
  93. /* copy our kernel relocation code to the control code page */
  94. memcpy((void *)reboot_code_buffer, relocate_new_kernel,
  95. relocate_new_kernel_size);
  96. kexec_info(image);
  97. flush_cache_all();
  98. sh_bios_vbr_reload();
  99. /* now call it */
  100. rnk = (relocate_new_kernel_t) reboot_code_buffer;
  101. (*rnk)(page_list, reboot_code_buffer,
  102. (unsigned long)phys_to_virt(image->start));
  103. #ifdef CONFIG_KEXEC_JUMP
  104. asm volatile("ldc %0, vbr" : : "r" (&vbr_base) : "memory");
  105. if (image->preserve_context)
  106. restore_processor_state();
  107. /* Convert page list back to physical addresses, what a mess. */
  108. for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE);
  109. ptr = (*ptr & IND_INDIRECTION) ?
  110. phys_to_virt(*ptr & PAGE_MASK) : ptr + 1) {
  111. if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||
  112. *ptr & IND_DESTINATION)
  113. *ptr = virt_to_phys(*ptr);
  114. }
  115. #endif
  116. __ftrace_enabled_restore(save_ftrace_enabled);
  117. }
  118. void arch_crash_save_vmcoreinfo(void)
  119. {
  120. #ifdef CONFIG_NUMA
  121. VMCOREINFO_SYMBOL(node_data);
  122. VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
  123. #endif
  124. #ifdef CONFIG_X2TLB
  125. VMCOREINFO_CONFIG(X2TLB);
  126. #endif
  127. }
  128. void __init reserve_crashkernel(void)
  129. {
  130. unsigned long long crash_size, crash_base;
  131. int ret;
  132. ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
  133. &crash_size, &crash_base);
  134. if (ret == 0 && crash_size > 0) {
  135. crashk_res.start = crash_base;
  136. crashk_res.end = crash_base + crash_size - 1;
  137. }
  138. if (crashk_res.end == crashk_res.start)
  139. goto disable;
  140. crash_size = PAGE_ALIGN(resource_size(&crashk_res));
  141. if (!crashk_res.start) {
  142. unsigned long max = memblock_end_of_DRAM() - memory_limit;
  143. crashk_res.start = memblock_phys_alloc_range(crash_size,
  144. PAGE_SIZE, 0, max);
  145. if (!crashk_res.start) {
  146. pr_err("crashkernel allocation failed\n");
  147. goto disable;
  148. }
  149. } else {
  150. ret = memblock_reserve(crashk_res.start, crash_size);
  151. if (unlikely(ret < 0)) {
  152. pr_err("crashkernel reservation failed - "
  153. "memory is in use\n");
  154. goto disable;
  155. }
  156. }
  157. crashk_res.end = crashk_res.start + crash_size - 1;
  158. /*
  159. * Crash kernel trumps memory limit
  160. */
  161. if ((memblock_end_of_DRAM() - memory_limit) <= crashk_res.end) {
  162. memory_limit = 0;
  163. pr_info("Disabled memory limit for crashkernel\n");
  164. }
  165. pr_info("Reserving %ldMB of memory at 0x%08lx "
  166. "for crashkernel (System RAM: %ldMB)\n",
  167. (unsigned long)(crash_size >> 20),
  168. (unsigned long)(crashk_res.start),
  169. (unsigned long)(memblock_phys_mem_size() >> 20));
  170. return;
  171. disable:
  172. crashk_res.start = crashk_res.end = 0;
  173. }