machine_kexec.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/lmb.h>
  15. #include <linux/of.h>
  16. #include <asm/machdep.h>
  17. #include <asm/prom.h>
  18. #include <asm/sections.h>
  19. void machine_crash_shutdown(struct pt_regs *regs)
  20. {
  21. if (ppc_md.machine_crash_shutdown)
  22. ppc_md.machine_crash_shutdown(regs);
  23. else
  24. default_machine_crash_shutdown(regs);
  25. }
  26. /*
  27. * Do what every setup is needed on image and the
  28. * reboot code buffer to allow us to avoid allocations
  29. * later.
  30. */
  31. int machine_kexec_prepare(struct kimage *image)
  32. {
  33. if (ppc_md.machine_kexec_prepare)
  34. return ppc_md.machine_kexec_prepare(image);
  35. else
  36. return default_machine_kexec_prepare(image);
  37. }
  38. void machine_kexec_cleanup(struct kimage *image)
  39. {
  40. if (ppc_md.machine_kexec_cleanup)
  41. ppc_md.machine_kexec_cleanup(image);
  42. }
  43. /*
  44. * Do not allocate memory (or fail in any way) in machine_kexec().
  45. * We are past the point of no return, committed to rebooting now.
  46. */
  47. void machine_kexec(struct kimage *image)
  48. {
  49. if (ppc_md.machine_kexec)
  50. ppc_md.machine_kexec(image);
  51. else
  52. default_machine_kexec(image);
  53. /* Fall back to normal restart if we're still alive. */
  54. machine_restart(NULL);
  55. for(;;);
  56. }
  57. void __init reserve_crashkernel(void)
  58. {
  59. unsigned long long crash_size, crash_base;
  60. int ret;
  61. /* this is necessary because of lmb_phys_mem_size() */
  62. lmb_analyze();
  63. /* use common parsing */
  64. ret = parse_crashkernel(boot_command_line, lmb_phys_mem_size(),
  65. &crash_size, &crash_base);
  66. if (ret == 0 && crash_size > 0) {
  67. crashk_res.start = crash_base;
  68. crashk_res.end = crash_base + crash_size - 1;
  69. }
  70. if (crashk_res.end == crashk_res.start) {
  71. crashk_res.start = crashk_res.end = 0;
  72. return;
  73. }
  74. /* We might have got these values via the command line or the
  75. * device tree, either way sanitise them now. */
  76. crash_size = crashk_res.end - crashk_res.start + 1;
  77. #ifndef CONFIG_RELOCATABLE
  78. if (crashk_res.start != KDUMP_KERNELBASE)
  79. printk("Crash kernel location must be 0x%x\n",
  80. KDUMP_KERNELBASE);
  81. crashk_res.start = KDUMP_KERNELBASE;
  82. #else
  83. if (!crashk_res.start) {
  84. /*
  85. * unspecified address, choose a region of specified size
  86. * can overlap with initrd (ignoring corruption when retained)
  87. * ppc64 requires kernel and some stacks to be in first segemnt
  88. */
  89. crashk_res.start = KDUMP_KERNELBASE;
  90. }
  91. crash_base = PAGE_ALIGN(crashk_res.start);
  92. if (crash_base != crashk_res.start) {
  93. printk("Crash kernel base must be aligned to 0x%lx\n",
  94. PAGE_SIZE);
  95. crashk_res.start = crash_base;
  96. }
  97. #endif
  98. crash_size = PAGE_ALIGN(crash_size);
  99. crashk_res.end = crashk_res.start + crash_size - 1;
  100. /* The crash region must not overlap the current kernel */
  101. if (overlaps_crashkernel(__pa(_stext), _end - _stext)) {
  102. printk(KERN_WARNING
  103. "Crash kernel can not overlap current kernel\n");
  104. crashk_res.start = crashk_res.end = 0;
  105. return;
  106. }
  107. /* Crash kernel trumps memory limit */
  108. if (memory_limit && memory_limit <= crashk_res.end) {
  109. memory_limit = crashk_res.end + 1;
  110. printk("Adjusted memory limit for crashkernel, now 0x%llx\n",
  111. (unsigned long long)memory_limit);
  112. }
  113. printk(KERN_INFO "Reserving %ldMB of memory at %ldMB "
  114. "for crashkernel (System RAM: %ldMB)\n",
  115. (unsigned long)(crash_size >> 20),
  116. (unsigned long)(crashk_res.start >> 20),
  117. (unsigned long)(lmb_phys_mem_size() >> 20));
  118. lmb_reserve(crashk_res.start, crash_size);
  119. }
  120. int overlaps_crashkernel(unsigned long start, unsigned long size)
  121. {
  122. return (start + size) > crashk_res.start && start <= crashk_res.end;
  123. }
  124. /* Values we need to export to the second kernel via the device tree. */
  125. static unsigned long kernel_end;
  126. static unsigned long crashk_size;
  127. static struct property kernel_end_prop = {
  128. .name = "linux,kernel-end",
  129. .length = sizeof(unsigned long),
  130. .value = &kernel_end,
  131. };
  132. static struct property crashk_base_prop = {
  133. .name = "linux,crashkernel-base",
  134. .length = sizeof(unsigned long),
  135. .value = &crashk_res.start,
  136. };
  137. static struct property crashk_size_prop = {
  138. .name = "linux,crashkernel-size",
  139. .length = sizeof(unsigned long),
  140. .value = &crashk_size,
  141. };
  142. static void __init export_crashk_values(struct device_node *node)
  143. {
  144. struct property *prop;
  145. /* There might be existing crash kernel properties, but we can't
  146. * be sure what's in them, so remove them. */
  147. prop = of_find_property(node, "linux,crashkernel-base", NULL);
  148. if (prop)
  149. prom_remove_property(node, prop);
  150. prop = of_find_property(node, "linux,crashkernel-size", NULL);
  151. if (prop)
  152. prom_remove_property(node, prop);
  153. if (crashk_res.start != 0) {
  154. prom_add_property(node, &crashk_base_prop);
  155. crashk_size = crashk_res.end - crashk_res.start + 1;
  156. prom_add_property(node, &crashk_size_prop);
  157. }
  158. }
  159. static int __init kexec_setup(void)
  160. {
  161. struct device_node *node;
  162. struct property *prop;
  163. node = of_find_node_by_path("/chosen");
  164. if (!node)
  165. return -ENOENT;
  166. /* remove any stale properties so ours can be found */
  167. prop = of_find_property(node, kernel_end_prop.name, NULL);
  168. if (prop)
  169. prom_remove_property(node, prop);
  170. /* information needed by userspace when using default_machine_kexec */
  171. kernel_end = __pa(_end);
  172. prom_add_property(node, &kernel_end_prop);
  173. export_crashk_values(node);
  174. of_node_put(node);
  175. return 0;
  176. }
  177. late_initcall(kexec_setup);