crash.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 <linux/memblock.h>
  26. #include <asm/processor.h>
  27. #include <asm/machdep.h>
  28. #include <asm/kexec.h>
  29. #include <asm/kdump.h>
  30. #include <asm/prom.h>
  31. #include <asm/firmware.h>
  32. #include <asm/smp.h>
  33. #include <asm/system.h>
  34. #include <asm/setjmp.h>
  35. #ifdef DEBUG
  36. #include <asm/udbg.h>
  37. #define DBG(fmt...) udbg_printf(fmt)
  38. #else
  39. #define DBG(fmt...)
  40. #endif
  41. /* This keeps a track of which one is crashing cpu. */
  42. int crashing_cpu = -1;
  43. static cpumask_t cpus_in_crash = CPU_MASK_NONE;
  44. cpumask_t cpus_in_sr = CPU_MASK_NONE;
  45. #define CRASH_HANDLER_MAX 2
  46. /* NULL terminated list of shutdown handles */
  47. static crash_shutdown_t crash_shutdown_handles[CRASH_HANDLER_MAX+1];
  48. static DEFINE_SPINLOCK(crash_handlers_lock);
  49. #ifdef CONFIG_SMP
  50. static atomic_t enter_on_soft_reset = ATOMIC_INIT(0);
  51. void crash_ipi_callback(struct pt_regs *regs)
  52. {
  53. int cpu = smp_processor_id();
  54. if (!cpu_online(cpu))
  55. return;
  56. hard_irq_disable();
  57. if (!cpu_isset(cpu, cpus_in_crash))
  58. crash_save_cpu(regs, cpu);
  59. cpu_set(cpu, cpus_in_crash);
  60. /*
  61. * Entered via soft-reset - could be the kdump
  62. * process is invoked using soft-reset or user activated
  63. * it if some CPU did not respond to an IPI.
  64. * For soft-reset, the secondary CPU can enter this func
  65. * twice. 1 - using IPI, and 2. soft-reset.
  66. * Tell the kexec CPU that entered via soft-reset and ready
  67. * to go down.
  68. */
  69. if (cpu_isset(cpu, cpus_in_sr)) {
  70. cpu_clear(cpu, cpus_in_sr);
  71. atomic_inc(&enter_on_soft_reset);
  72. }
  73. /*
  74. * Starting the kdump boot.
  75. * This barrier is needed to make sure that all CPUs are stopped.
  76. * If not, soft-reset will be invoked to bring other CPUs.
  77. */
  78. while (!cpu_isset(crashing_cpu, cpus_in_crash))
  79. cpu_relax();
  80. if (ppc_md.kexec_cpu_down)
  81. ppc_md.kexec_cpu_down(1, 1);
  82. #ifdef CONFIG_PPC64
  83. kexec_smp_wait();
  84. #else
  85. for (;;); /* FIXME */
  86. #endif
  87. /* NOTREACHED */
  88. }
  89. /*
  90. * Wait until all CPUs are entered via soft-reset.
  91. */
  92. static void crash_soft_reset_check(int cpu)
  93. {
  94. unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  95. cpu_clear(cpu, cpus_in_sr);
  96. while (atomic_read(&enter_on_soft_reset) != ncpus)
  97. cpu_relax();
  98. }
  99. static void crash_kexec_prepare_cpus(int cpu)
  100. {
  101. unsigned int msecs;
  102. unsigned int ncpus = num_online_cpus() - 1;/* Excluding the panic cpu */
  103. crash_send_ipi(crash_ipi_callback);
  104. smp_wmb();
  105. /*
  106. * FIXME: Until we will have the way to stop other CPUSs reliabally,
  107. * the crash CPU will send an IPI and wait for other CPUs to
  108. * respond.
  109. * Delay of at least 10 seconds.
  110. */
  111. printk(KERN_EMERG "Sending IPI to other cpus...\n");
  112. msecs = 10000;
  113. while ((cpus_weight(cpus_in_crash) < ncpus) && (--msecs > 0)) {
  114. cpu_relax();
  115. mdelay(1);
  116. }
  117. /* Would it be better to replace the trap vector here? */
  118. /*
  119. * FIXME: In case if we do not get all CPUs, one possibility: ask the
  120. * user to do soft reset such that we get all.
  121. * Soft-reset will be used until better mechanism is implemented.
  122. */
  123. if (cpus_weight(cpus_in_crash) < ncpus) {
  124. printk(KERN_EMERG "done waiting: %d cpu(s) not responding\n",
  125. ncpus - cpus_weight(cpus_in_crash));
  126. printk(KERN_EMERG "Activate soft-reset to stop other cpu(s)\n");
  127. cpus_in_sr = CPU_MASK_NONE;
  128. atomic_set(&enter_on_soft_reset, 0);
  129. while (cpus_weight(cpus_in_crash) < ncpus)
  130. cpu_relax();
  131. }
  132. /*
  133. * Make sure all CPUs are entered via soft-reset if the kdump is
  134. * invoked using soft-reset.
  135. */
  136. if (cpu_isset(cpu, cpus_in_sr))
  137. crash_soft_reset_check(cpu);
  138. /* Leave the IPI callback set */
  139. }
  140. /* wait for all the CPUs to hit real mode but timeout if they don't come in */
  141. #ifdef CONFIG_PPC_STD_MMU_64
  142. static void crash_kexec_wait_realmode(int cpu)
  143. {
  144. unsigned int msecs;
  145. int i;
  146. msecs = 10000;
  147. for (i=0; i < NR_CPUS && msecs > 0; i++) {
  148. if (i == cpu)
  149. continue;
  150. while (paca[i].kexec_state < KEXEC_STATE_REAL_MODE) {
  151. barrier();
  152. if (!cpu_possible(i)) {
  153. break;
  154. }
  155. if (!cpu_online(i)) {
  156. break;
  157. }
  158. msecs--;
  159. mdelay(1);
  160. }
  161. }
  162. mb();
  163. }
  164. #endif
  165. /*
  166. * This function will be called by secondary cpus or by kexec cpu
  167. * if soft-reset is activated to stop some CPUs.
  168. */
  169. void crash_kexec_secondary(struct pt_regs *regs)
  170. {
  171. int cpu = smp_processor_id();
  172. unsigned long flags;
  173. int msecs = 5;
  174. local_irq_save(flags);
  175. /* Wait 5ms if the kexec CPU is not entered yet. */
  176. while (crashing_cpu < 0) {
  177. if (--msecs < 0) {
  178. /*
  179. * Either kdump image is not loaded or
  180. * kdump process is not started - Probably xmon
  181. * exited using 'x'(exit and recover) or
  182. * kexec_should_crash() failed for all running tasks.
  183. */
  184. cpu_clear(cpu, cpus_in_sr);
  185. local_irq_restore(flags);
  186. return;
  187. }
  188. mdelay(1);
  189. cpu_relax();
  190. }
  191. if (cpu == crashing_cpu) {
  192. /*
  193. * Panic CPU will enter this func only via soft-reset.
  194. * Wait until all secondary CPUs entered and
  195. * then start kexec boot.
  196. */
  197. crash_soft_reset_check(cpu);
  198. cpu_set(crashing_cpu, cpus_in_crash);
  199. if (ppc_md.kexec_cpu_down)
  200. ppc_md.kexec_cpu_down(1, 0);
  201. machine_kexec(kexec_crash_image);
  202. /* NOTREACHED */
  203. }
  204. crash_ipi_callback(regs);
  205. }
  206. #else
  207. static void crash_kexec_prepare_cpus(int cpu)
  208. {
  209. /*
  210. * move the secondarys to us so that we can copy
  211. * the new kernel 0-0x100 safely
  212. *
  213. * do this if kexec in setup.c ?
  214. */
  215. #ifdef CONFIG_PPC64
  216. smp_release_cpus();
  217. #else
  218. /* FIXME */
  219. #endif
  220. }
  221. void crash_kexec_secondary(struct pt_regs *regs)
  222. {
  223. cpus_in_sr = CPU_MASK_NONE;
  224. }
  225. #endif
  226. #ifdef CONFIG_SPU_BASE
  227. #include <asm/spu.h>
  228. #include <asm/spu_priv1.h>
  229. struct crash_spu_info {
  230. struct spu *spu;
  231. u32 saved_spu_runcntl_RW;
  232. u32 saved_spu_status_R;
  233. u32 saved_spu_npc_RW;
  234. u64 saved_mfc_sr1_RW;
  235. u64 saved_mfc_dar;
  236. u64 saved_mfc_dsisr;
  237. };
  238. #define CRASH_NUM_SPUS 16 /* Enough for current hardware */
  239. static struct crash_spu_info crash_spu_info[CRASH_NUM_SPUS];
  240. static void crash_kexec_stop_spus(void)
  241. {
  242. struct spu *spu;
  243. int i;
  244. u64 tmp;
  245. for (i = 0; i < CRASH_NUM_SPUS; i++) {
  246. if (!crash_spu_info[i].spu)
  247. continue;
  248. spu = crash_spu_info[i].spu;
  249. crash_spu_info[i].saved_spu_runcntl_RW =
  250. in_be32(&spu->problem->spu_runcntl_RW);
  251. crash_spu_info[i].saved_spu_status_R =
  252. in_be32(&spu->problem->spu_status_R);
  253. crash_spu_info[i].saved_spu_npc_RW =
  254. in_be32(&spu->problem->spu_npc_RW);
  255. crash_spu_info[i].saved_mfc_dar = spu_mfc_dar_get(spu);
  256. crash_spu_info[i].saved_mfc_dsisr = spu_mfc_dsisr_get(spu);
  257. tmp = spu_mfc_sr1_get(spu);
  258. crash_spu_info[i].saved_mfc_sr1_RW = tmp;
  259. tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
  260. spu_mfc_sr1_set(spu, tmp);
  261. __delay(200);
  262. }
  263. }
  264. void crash_register_spus(struct list_head *list)
  265. {
  266. struct spu *spu;
  267. list_for_each_entry(spu, list, full_list) {
  268. if (WARN_ON(spu->number >= CRASH_NUM_SPUS))
  269. continue;
  270. crash_spu_info[spu->number].spu = spu;
  271. }
  272. }
  273. #else
  274. static inline void crash_kexec_stop_spus(void)
  275. {
  276. }
  277. #endif /* CONFIG_SPU_BASE */
  278. /*
  279. * Register a function to be called on shutdown. Only use this if you
  280. * can't reset your device in the second kernel.
  281. */
  282. int crash_shutdown_register(crash_shutdown_t handler)
  283. {
  284. unsigned int i, rc;
  285. spin_lock(&crash_handlers_lock);
  286. for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
  287. if (!crash_shutdown_handles[i]) {
  288. /* Insert handle at first empty entry */
  289. crash_shutdown_handles[i] = handler;
  290. rc = 0;
  291. break;
  292. }
  293. if (i == CRASH_HANDLER_MAX) {
  294. printk(KERN_ERR "Crash shutdown handles full, "
  295. "not registered.\n");
  296. rc = 1;
  297. }
  298. spin_unlock(&crash_handlers_lock);
  299. return rc;
  300. }
  301. EXPORT_SYMBOL(crash_shutdown_register);
  302. int crash_shutdown_unregister(crash_shutdown_t handler)
  303. {
  304. unsigned int i, rc;
  305. spin_lock(&crash_handlers_lock);
  306. for (i = 0 ; i < CRASH_HANDLER_MAX; i++)
  307. if (crash_shutdown_handles[i] == handler)
  308. break;
  309. if (i == CRASH_HANDLER_MAX) {
  310. printk(KERN_ERR "Crash shutdown handle not found\n");
  311. rc = 1;
  312. } else {
  313. /* Shift handles down */
  314. for (; crash_shutdown_handles[i]; i++)
  315. crash_shutdown_handles[i] =
  316. crash_shutdown_handles[i+1];
  317. rc = 0;
  318. }
  319. spin_unlock(&crash_handlers_lock);
  320. return rc;
  321. }
  322. EXPORT_SYMBOL(crash_shutdown_unregister);
  323. static unsigned long crash_shutdown_buf[JMP_BUF_LEN];
  324. static int crash_shutdown_cpu = -1;
  325. static int handle_fault(struct pt_regs *regs)
  326. {
  327. if (crash_shutdown_cpu == smp_processor_id())
  328. longjmp(crash_shutdown_buf, 1);
  329. return 0;
  330. }
  331. void default_machine_crash_shutdown(struct pt_regs *regs)
  332. {
  333. unsigned int i;
  334. int (*old_handler)(struct pt_regs *regs);
  335. /*
  336. * This function is only called after the system
  337. * has panicked or is otherwise in a critical state.
  338. * The minimum amount of code to allow a kexec'd kernel
  339. * to run successfully needs to happen here.
  340. *
  341. * In practice this means stopping other cpus in
  342. * an SMP system.
  343. * The kernel is broken so disable interrupts.
  344. */
  345. hard_irq_disable();
  346. /*
  347. * Make a note of crashing cpu. Will be used in machine_kexec
  348. * such that another IPI will not be sent.
  349. */
  350. crashing_cpu = smp_processor_id();
  351. crash_save_cpu(regs, crashing_cpu);
  352. crash_kexec_prepare_cpus(crashing_cpu);
  353. cpu_set(crashing_cpu, cpus_in_crash);
  354. #if defined(CONFIG_PPC_STD_MMU_64) && defined(CONFIG_SMP)
  355. crash_kexec_wait_realmode(crashing_cpu);
  356. #endif
  357. for_each_irq(i) {
  358. struct irq_desc *desc = irq_to_desc(i);
  359. if (!desc || !desc->chip || !desc->chip->eoi)
  360. continue;
  361. if (desc->status & IRQ_INPROGRESS)
  362. desc->chip->eoi(i);
  363. if (!(desc->status & IRQ_DISABLED))
  364. desc->chip->shutdown(i);
  365. }
  366. /*
  367. * Call registered shutdown routines savely. Swap out
  368. * __debugger_fault_handler, and replace on exit.
  369. */
  370. old_handler = __debugger_fault_handler;
  371. __debugger_fault_handler = handle_fault;
  372. crash_shutdown_cpu = smp_processor_id();
  373. for (i = 0; crash_shutdown_handles[i]; i++) {
  374. if (setjmp(crash_shutdown_buf) == 0) {
  375. /*
  376. * Insert syncs and delay to ensure
  377. * instructions in the dangerous region don't
  378. * leak away from this protected region.
  379. */
  380. asm volatile("sync; isync");
  381. /* dangerous region */
  382. crash_shutdown_handles[i]();
  383. asm volatile("sync; isync");
  384. }
  385. }
  386. crash_shutdown_cpu = -1;
  387. __debugger_fault_handler = old_handler;
  388. crash_kexec_stop_spus();
  389. if (ppc_md.kexec_cpu_down)
  390. ppc_md.kexec_cpu_down(1, 0);
  391. }