machine_kexec.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Code to handle transition of Linux booting another kernel.
  3. *
  4. * Copyright (C) 2002-2003 Eric Biederman <ebiederm@xmission.com>
  5. * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz
  6. * Copyright (C) 2005 IBM Corporation.
  7. *
  8. * This source code is licensed under the GNU General Public License,
  9. * Version 2. See the file COPYING for more details.
  10. */
  11. #include <linux/kexec.h>
  12. #include <linux/reboot.h>
  13. #include <linux/threads.h>
  14. #include <linux/memblock.h>
  15. #include <linux/of.h>
  16. #include <linux/irq.h>
  17. #include <linux/ftrace.h>
  18. #include <asm/machdep.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/prom.h>
  21. #include <asm/sections.h>
  22. void machine_kexec_mask_interrupts(void) {
  23. unsigned int i;
  24. struct irq_desc *desc;
  25. for_each_irq_desc(i, desc) {
  26. struct irq_chip *chip;
  27. chip = irq_desc_get_chip(desc);
  28. if (!chip)
  29. continue;
  30. if (chip->irq_eoi && irqd_irq_inprogress(&desc->irq_data))
  31. chip->irq_eoi(&desc->irq_data);
  32. if (chip->irq_mask)
  33. chip->irq_mask(&desc->irq_data);
  34. if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
  35. chip->irq_disable(&desc->irq_data);
  36. }
  37. }
  38. void machine_crash_shutdown(struct pt_regs *regs)
  39. {
  40. default_machine_crash_shutdown(regs);
  41. }
  42. /*
  43. * Do what every setup is needed on image and the
  44. * reboot code buffer to allow us to avoid allocations
  45. * later.
  46. */
  47. int machine_kexec_prepare(struct kimage *image)
  48. {
  49. if (ppc_md.machine_kexec_prepare)
  50. return ppc_md.machine_kexec_prepare(image);
  51. else
  52. return default_machine_kexec_prepare(image);
  53. }
  54. void machine_kexec_cleanup(struct kimage *image)
  55. {
  56. }
  57. void arch_crash_save_vmcoreinfo(void)
  58. {
  59. #ifdef CONFIG_NEED_MULTIPLE_NODES
  60. VMCOREINFO_SYMBOL(node_data);
  61. VMCOREINFO_LENGTH(node_data, MAX_NUMNODES);
  62. #endif
  63. #ifndef CONFIG_NEED_MULTIPLE_NODES
  64. VMCOREINFO_SYMBOL(contig_page_data);
  65. #endif
  66. #if defined(CONFIG_PPC64) && defined(CONFIG_SPARSEMEM_VMEMMAP)
  67. VMCOREINFO_SYMBOL(vmemmap_list);
  68. VMCOREINFO_SYMBOL(mmu_vmemmap_psize);
  69. VMCOREINFO_SYMBOL(mmu_psize_defs);
  70. VMCOREINFO_STRUCT_SIZE(vmemmap_backing);
  71. VMCOREINFO_OFFSET(vmemmap_backing, list);
  72. VMCOREINFO_OFFSET(vmemmap_backing, phys);
  73. VMCOREINFO_OFFSET(vmemmap_backing, virt_addr);
  74. VMCOREINFO_STRUCT_SIZE(mmu_psize_def);
  75. VMCOREINFO_OFFSET(mmu_psize_def, shift);
  76. #endif
  77. }
  78. /*
  79. * Do not allocate memory (or fail in any way) in machine_kexec().
  80. * We are past the point of no return, committed to rebooting now.
  81. */
  82. void machine_kexec(struct kimage *image)
  83. {
  84. int save_ftrace_enabled;
  85. save_ftrace_enabled = __ftrace_enabled_save();
  86. if (ppc_md.machine_kexec)
  87. ppc_md.machine_kexec(image);
  88. else
  89. default_machine_kexec(image);
  90. __ftrace_enabled_restore(save_ftrace_enabled);
  91. /* Fall back to normal restart if we're still alive. */
  92. machine_restart(NULL);
  93. for(;;);
  94. }
  95. void __init reserve_crashkernel(void)
  96. {
  97. unsigned long long crash_size, crash_base;
  98. int ret;
  99. /* use common parsing */
  100. ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
  101. &crash_size, &crash_base);
  102. if (ret == 0 && crash_size > 0) {
  103. crashk_res.start = crash_base;
  104. crashk_res.end = crash_base + crash_size - 1;
  105. }
  106. if (crashk_res.end == crashk_res.start) {
  107. crashk_res.start = crashk_res.end = 0;
  108. return;
  109. }
  110. /* We might have got these values via the command line or the
  111. * device tree, either way sanitise them now. */
  112. crash_size = resource_size(&crashk_res);
  113. #ifndef CONFIG_NONSTATIC_KERNEL
  114. if (crashk_res.start != KDUMP_KERNELBASE)
  115. printk("Crash kernel location must be 0x%x\n",
  116. KDUMP_KERNELBASE);
  117. crashk_res.start = KDUMP_KERNELBASE;
  118. #else
  119. if (!crashk_res.start) {
  120. #ifdef CONFIG_PPC64
  121. /*
  122. * On 64bit we split the RMO in half but cap it at half of
  123. * a small SLB (128MB) since the crash kernel needs to place
  124. * itself and some stacks to be in the first segment.
  125. */
  126. crashk_res.start = min(0x8000000ULL, (ppc64_rma_size / 2));
  127. #else
  128. crashk_res.start = KDUMP_KERNELBASE;
  129. #endif
  130. }
  131. crash_base = PAGE_ALIGN(crashk_res.start);
  132. if (crash_base != crashk_res.start) {
  133. printk("Crash kernel base must be aligned to 0x%lx\n",
  134. PAGE_SIZE);
  135. crashk_res.start = crash_base;
  136. }
  137. #endif
  138. crash_size = PAGE_ALIGN(crash_size);
  139. crashk_res.end = crashk_res.start + crash_size - 1;
  140. /* The crash region must not overlap the current kernel */
  141. if (overlaps_crashkernel(__pa(_stext), _end - _stext)) {
  142. printk(KERN_WARNING
  143. "Crash kernel can not overlap current kernel\n");
  144. crashk_res.start = crashk_res.end = 0;
  145. return;
  146. }
  147. /* Crash kernel trumps memory limit */
  148. if (memory_limit && memory_limit <= crashk_res.end) {
  149. memory_limit = crashk_res.end + 1;
  150. printk("Adjusted memory limit for crashkernel, now 0x%llx\n",
  151. memory_limit);
  152. }
  153. printk(KERN_INFO "Reserving %ldMB of memory at %ldMB "
  154. "for crashkernel (System RAM: %ldMB)\n",
  155. (unsigned long)(crash_size >> 20),
  156. (unsigned long)(crashk_res.start >> 20),
  157. (unsigned long)(memblock_phys_mem_size() >> 20));
  158. memblock_reserve(crashk_res.start, crash_size);
  159. }
  160. int overlaps_crashkernel(unsigned long start, unsigned long size)
  161. {
  162. return (start + size) > crashk_res.start && start <= crashk_res.end;
  163. }
  164. /* Values we need to export to the second kernel via the device tree. */
  165. static phys_addr_t kernel_end;
  166. static phys_addr_t crashk_base;
  167. static phys_addr_t crashk_size;
  168. static unsigned long long mem_limit;
  169. static struct property kernel_end_prop = {
  170. .name = "linux,kernel-end",
  171. .length = sizeof(phys_addr_t),
  172. .value = &kernel_end,
  173. };
  174. static struct property crashk_base_prop = {
  175. .name = "linux,crashkernel-base",
  176. .length = sizeof(phys_addr_t),
  177. .value = &crashk_base
  178. };
  179. static struct property crashk_size_prop = {
  180. .name = "linux,crashkernel-size",
  181. .length = sizeof(phys_addr_t),
  182. .value = &crashk_size,
  183. };
  184. static struct property memory_limit_prop = {
  185. .name = "linux,memory-limit",
  186. .length = sizeof(unsigned long long),
  187. .value = &mem_limit,
  188. };
  189. #define cpu_to_be_ulong __PASTE(cpu_to_be, BITS_PER_LONG)
  190. static void __init export_crashk_values(struct device_node *node)
  191. {
  192. struct property *prop;
  193. /* There might be existing crash kernel properties, but we can't
  194. * be sure what's in them, so remove them. */
  195. prop = of_find_property(node, "linux,crashkernel-base", NULL);
  196. if (prop)
  197. of_remove_property(node, prop);
  198. prop = of_find_property(node, "linux,crashkernel-size", NULL);
  199. if (prop)
  200. of_remove_property(node, prop);
  201. if (crashk_res.start != 0) {
  202. crashk_base = cpu_to_be_ulong(crashk_res.start),
  203. of_add_property(node, &crashk_base_prop);
  204. crashk_size = cpu_to_be_ulong(resource_size(&crashk_res));
  205. of_add_property(node, &crashk_size_prop);
  206. }
  207. /*
  208. * memory_limit is required by the kexec-tools to limit the
  209. * crash regions to the actual memory used.
  210. */
  211. mem_limit = cpu_to_be_ulong(memory_limit);
  212. of_update_property(node, &memory_limit_prop);
  213. }
  214. static int __init kexec_setup(void)
  215. {
  216. struct device_node *node;
  217. struct property *prop;
  218. node = of_find_node_by_path("/chosen");
  219. if (!node)
  220. return -ENOENT;
  221. /* remove any stale properties so ours can be found */
  222. prop = of_find_property(node, kernel_end_prop.name, NULL);
  223. if (prop)
  224. of_remove_property(node, prop);
  225. /* information needed by userspace when using default_machine_kexec */
  226. kernel_end = cpu_to_be_ulong(__pa(_end));
  227. of_add_property(node, &kernel_end_prop);
  228. export_crashk_values(node);
  229. of_node_put(node);
  230. return 0;
  231. }
  232. late_initcall(kexec_setup);