crash.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Architecture specific (PPC64) functions for kexec based crash dumps.
  3. *
  4. * Copyright (C) 2005, IBM Corp.
  5. *
  6. * Created by: Haren Myneni
  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. */
  12. #undef DEBUG
  13. #include <linux/kernel.h>
  14. #include <linux/smp.h>
  15. #include <linux/reboot.h>
  16. #include <linux/kexec.h>
  17. #include <linux/bootmem.h>
  18. #include <linux/crash_dump.h>
  19. #include <linux/delay.h>
  20. #include <linux/elf.h>
  21. #include <linux/elfcore.h>
  22. #include <linux/init.h>
  23. #include <linux/irq.h>
  24. #include <linux/types.h>
  25. #include <asm/processor.h>
  26. #include <asm/machdep.h>
  27. #include <asm/kexec.h>
  28. #include <asm/kdump.h>
  29. #include <asm/lmb.h>
  30. #include <asm/firmware.h>
  31. #include <asm/smp.h>
  32. #ifdef DEBUG
  33. #include <asm/udbg.h>
  34. #define DBG(fmt...) udbg_printf(fmt)
  35. #else
  36. #define DBG(fmt...)
  37. #endif
  38. /* This keeps a track of which one is crashing cpu. */
  39. int crashing_cpu = -1;
  40. static cpumask_t cpus_in_crash = CPU_MASK_NONE;
  41. cpumask_t cpus_in_sr = CPU_MASK_NONE;
  42. #ifdef CONFIG_SMP
  43. static atomic_t enter_on_soft_reset = ATOMIC_INIT(0);
  44. void crash_ipi_callback(struct pt_regs *regs)
  45. {
  46. int cpu = smp_processor_id();
  47. if (!cpu_online(cpu))
  48. return;
  49. hard_irq_disable();
  50. if (!cpu_isset(cpu, cpus_in_crash))
  51. crash_save_cpu(regs, cpu);
  52. cpu_set(cpu, cpus_in_crash);
  53. /*
  54. * Entered via soft-reset - could be the kdump
  55. * process is invoked using soft-reset or user activated
  56. * it if some CPU did not respond to an IPI.
  57. * For soft-reset, the secondary CPU can enter this func
  58. * twice. 1 - using IPI, and 2. soft-reset.
  59. * Tell the kexec CPU that entered via soft-reset and ready
  60. * to go down.
  61. */
  62. if (cpu_isset(cpu, cpus_in_sr)) {
  63. cpu_clear(cpu, cpus_in_sr);
  64. atomic_inc(&enter_on_soft_reset);
  65. }
  66. /*
  67. * Starting the kdump boot.
  68. * This barrier is needed to make sure that all CPUs are stopped.
  69. * If not, soft-reset will be invoked to bring other CPUs.
  70. */
  71. while (!cpu_isset(crashing_cpu, cpus_in_crash))
  72. cpu_relax();
  73. if (ppc_md.kexec_cpu_down)
  74. ppc_md.kexec_cpu_down(1, 1);
  75. #ifdef CONFIG_PPC64
  76. kexec_smp_wait();
  77. #else
  78. for (;;); /* FIXME */
  79. #endif
  80. /* NOTREACHED */
  81. }
  82. /*
  83. * Wait until all CPUs are entered via soft-reset.
  84. */
  85. static void crash_soft_reset_check(int cpu)
  86. {
  87. unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  88. cpu_clear(cpu, cpus_in_sr);
  89. while (atomic_read(&enter_on_soft_reset) != ncpus)
  90. cpu_relax();
  91. }
  92. static void crash_kexec_prepare_cpus(int cpu)
  93. {
  94. unsigned int msecs;
  95. unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  96. crash_send_ipi(crash_ipi_callback);
  97. smp_wmb();
  98. /*
  99. * FIXME: Until we will have the way to stop other CPUSs reliabally,
  100. * the crash CPU will send an IPI and wait for other CPUs to
  101. * respond.
  102. * Delay of at least 10 seconds.
  103. */
  104. printk(KERN_EMERG "Sending IPI to other cpus...\n");
  105. msecs = 10000;
  106. while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) {
  107. cpu_relax();
  108. mdelay(1);
  109. }
  110. /* Would it be better to replace the trap vector here? */
  111. /*
  112. * FIXME: In case if we do not get all CPUs, one possibility: ask the
  113. * user to do soft reset such that we get all.
  114. * Soft-reset will be used until better mechanism is implemented.
  115. */
  116. if (cpus_weight(cpus_in_crash) < ncpus) {
  117. printk(KERN_EMERG "done waiting: %d cpu(s) not responding\n",
  118. ncpus - cpus_weight(cpus_in_crash));
  119. printk(KERN_EMERG "Activate soft-reset to stop other cpu(s)\n");
  120. cpus_in_sr = CPU_MASK_NONE;
  121. atomic_set(&enter_on_soft_reset, 0);
  122. while (cpus_weight(cpus_in_crash) < ncpus)
  123. cpu_relax();
  124. }
  125. /*
  126. * Make sure all CPUs are entered via soft-reset if the kdump is
  127. * invoked using soft-reset.
  128. */
  129. if (cpu_isset(cpu, cpus_in_sr))
  130. crash_soft_reset_check(cpu);
  131. /* Leave the IPI callback set */
  132. }
  133. /*
  134. * This function will be called by secondary cpus or by kexec cpu
  135. * if soft-reset is activated to stop some CPUs.
  136. */
  137. void crash_kexec_secondary(struct pt_regs *regs)
  138. {
  139. int cpu = smp_processor_id();
  140. unsigned long flags;
  141. int msecs = 5;
  142. local_irq_save(flags);
  143. /* Wait 5ms if the kexec CPU is not entered yet. */
  144. while (crashing_cpu < 0) {
  145. if (--msecs < 0) {
  146. /*
  147. * Either kdump image is not loaded or
  148. * kdump process is not started - Probably xmon
  149. * exited using 'x'(exit and recover) or
  150. * kexec_should_crash() failed for all running tasks.
  151. */
  152. cpu_clear(cpu, cpus_in_sr);
  153. local_irq_restore(flags);
  154. return;
  155. }
  156. mdelay(1);
  157. cpu_relax();
  158. }
  159. if (cpu == crashing_cpu) {
  160. /*
  161. * Panic CPU will enter this func only via soft-reset.
  162. * Wait until all secondary CPUs entered and
  163. * then start kexec boot.
  164. */
  165. crash_soft_reset_check(cpu);
  166. cpu_set(crashing_cpu, cpus_in_crash);
  167. if (ppc_md.kexec_cpu_down)
  168. ppc_md.kexec_cpu_down(1, 0);
  169. machine_kexec(kexec_crash_image);
  170. /* NOTREACHED */
  171. }
  172. crash_ipi_callback(regs);
  173. }
  174. #else
  175. static void crash_kexec_prepare_cpus(int cpu)
  176. {
  177. /*
  178. * move the secondarys to us so that we can copy
  179. * the new kernel 0-0x100 safely
  180. *
  181. * do this if kexec in setup.c ?
  182. */
  183. #ifdef CONFIG_PPC64
  184. smp_release_cpus();
  185. #else
  186. /* FIXME */
  187. #endif
  188. }
  189. void crash_kexec_secondary(struct pt_regs *regs)
  190. {
  191. cpus_in_sr = CPU_MASK_NONE;
  192. }
  193. #endif
  194. #ifdef CONFIG_SPU_BASE
  195. #include <asm/spu.h>
  196. #include <asm/spu_priv1.h>
  197. struct crash_spu_info {
  198. struct spu *spu;
  199. u32 saved_spu_runcntl_RW;
  200. u32 saved_spu_status_R;
  201. u32 saved_spu_npc_RW;
  202. u64 saved_mfc_sr1_RW;
  203. u64 saved_mfc_dar;
  204. u64 saved_mfc_dsisr;
  205. };
  206. #define CRASH_NUM_SPUS 16 /* Enough for current hardware */
  207. static struct crash_spu_info crash_spu_info[CRASH_NUM_SPUS];
  208. static void crash_kexec_stop_spus(void)
  209. {
  210. struct spu *spu;
  211. int i;
  212. u64 tmp;
  213. for (i = 0; i < CRASH_NUM_SPUS; i++) {
  214. if (!crash_spu_info[i].spu)
  215. continue;
  216. spu = crash_spu_info[i].spu;
  217. crash_spu_info[i].saved_spu_runcntl_RW =
  218. in_be32(&spu->problem->spu_runcntl_RW);
  219. crash_spu_info[i].saved_spu_status_R =
  220. in_be32(&spu->problem->spu_status_R);
  221. crash_spu_info[i].saved_spu_npc_RW =
  222. in_be32(&spu->problem->spu_npc_RW);
  223. crash_spu_info[i].saved_mfc_dar = spu_mfc_dar_get(spu);
  224. crash_spu_info[i].saved_mfc_dsisr = spu_mfc_dsisr_get(spu);
  225. tmp = spu_mfc_sr1_get(spu);
  226. crash_spu_info[i].saved_mfc_sr1_RW = tmp;
  227. tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
  228. spu_mfc_sr1_set(spu, tmp);
  229. __delay(200);
  230. }
  231. }
  232. void crash_register_spus(struct list_head *list)
  233. {
  234. struct spu *spu;
  235. list_for_each_entry(spu, list, full_list) {
  236. if (WARN_ON(spu->number >= CRASH_NUM_SPUS))
  237. continue;
  238. crash_spu_info[spu->number].spu = spu;
  239. }
  240. }
  241. #else
  242. static inline void crash_kexec_stop_spus(void)
  243. {
  244. }
  245. #endif /* CONFIG_SPU_BASE */
  246. void default_machine_crash_shutdown(struct pt_regs *regs)
  247. {
  248. unsigned int irq;
  249. /*
  250. * This function is only called after the system
  251. * has panicked or is otherwise in a critical state.
  252. * The minimum amount of code to allow a kexec'd kernel
  253. * to run successfully needs to happen here.
  254. *
  255. * In practice this means stopping other cpus in
  256. * an SMP system.
  257. * The kernel is broken so disable interrupts.
  258. */
  259. hard_irq_disable();
  260. for_each_irq(irq) {
  261. struct irq_desc *desc = irq_desc + irq;
  262. if (desc->status & IRQ_INPROGRESS)
  263. desc->chip->eoi(irq);
  264. if (!(desc->status & IRQ_DISABLED))
  265. desc->chip->disable(irq);
  266. }
  267. /*
  268. * Make a note of crashing cpu. Will be used in machine_kexec
  269. * such that another IPI will not be sent.
  270. */
  271. crashing_cpu = smp_processor_id();
  272. crash_save_cpu(regs, crashing_cpu);
  273. crash_kexec_prepare_cpus(crashing_cpu);
  274. cpu_set(crashing_cpu, cpus_in_crash);
  275. crash_kexec_stop_spus();
  276. if (ppc_md.kexec_cpu_down)
  277. ppc_md.kexec_cpu_down(1, 0);
  278. }