machine_kexec.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * machine_kexec.c - handle transition of Linux booting another kernel
  3. */
  4. #include <linux/mm.h>
  5. #include <linux/kexec.h>
  6. #include <linux/delay.h>
  7. #include <linux/reboot.h>
  8. #include <linux/io.h>
  9. #include <linux/irq.h>
  10. #include <linux/memblock.h>
  11. #include <asm/pgtable.h>
  12. #include <linux/of_fdt.h>
  13. #include <asm/pgalloc.h>
  14. #include <asm/mmu_context.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/fncpy.h>
  17. #include <asm/mach-types.h>
  18. #include <asm/smp_plat.h>
  19. #include <asm/system_misc.h>
  20. extern void relocate_new_kernel(void);
  21. extern const unsigned int relocate_new_kernel_size;
  22. extern unsigned long kexec_start_address;
  23. extern unsigned long kexec_indirection_page;
  24. extern unsigned long kexec_mach_type;
  25. extern unsigned long kexec_boot_atags;
  26. static atomic_t waiting_for_crash_ipi;
  27. /*
  28. * Provide a dummy crash_notes definition while crash dump arrives to arm.
  29. * This prevents breakage of crash_notes attribute in kernel/ksysfs.c.
  30. */
  31. int machine_kexec_prepare(struct kimage *image)
  32. {
  33. struct kexec_segment *current_segment;
  34. __be32 header;
  35. int i, err;
  36. /*
  37. * Validate that if the current HW supports SMP, then the SW supports
  38. * and implements CPU hotplug for the current HW. If not, we won't be
  39. * able to kexec reliably, so fail the prepare operation.
  40. */
  41. if (num_possible_cpus() > 1 && !platform_can_cpu_hotplug())
  42. return -EINVAL;
  43. /*
  44. * No segment at default ATAGs address. try to locate
  45. * a dtb using magic.
  46. */
  47. for (i = 0; i < image->nr_segments; i++) {
  48. current_segment = &image->segment[i];
  49. if (!memblock_is_region_memory(current_segment->mem,
  50. current_segment->memsz))
  51. return -EINVAL;
  52. err = get_user(header, (__be32*)current_segment->buf);
  53. if (err)
  54. return err;
  55. if (be32_to_cpu(header) == OF_DT_HEADER)
  56. kexec_boot_atags = current_segment->mem;
  57. }
  58. return 0;
  59. }
  60. void machine_kexec_cleanup(struct kimage *image)
  61. {
  62. }
  63. void machine_crash_nonpanic_core(void *unused)
  64. {
  65. struct pt_regs regs;
  66. crash_setup_regs(&regs, NULL);
  67. printk(KERN_DEBUG "CPU %u will stop doing anything useful since another CPU has crashed\n",
  68. smp_processor_id());
  69. crash_save_cpu(&regs, smp_processor_id());
  70. flush_cache_all();
  71. set_cpu_online(smp_processor_id(), false);
  72. atomic_dec(&waiting_for_crash_ipi);
  73. while (1)
  74. cpu_relax();
  75. }
  76. static void machine_kexec_mask_interrupts(void)
  77. {
  78. unsigned int i;
  79. struct irq_desc *desc;
  80. for_each_irq_desc(i, desc) {
  81. struct irq_chip *chip;
  82. chip = irq_desc_get_chip(desc);
  83. if (!chip)
  84. continue;
  85. if (chip->irq_eoi && irqd_irq_inprogress(&desc->irq_data))
  86. chip->irq_eoi(&desc->irq_data);
  87. if (chip->irq_mask)
  88. chip->irq_mask(&desc->irq_data);
  89. if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
  90. chip->irq_disable(&desc->irq_data);
  91. }
  92. }
  93. void machine_crash_shutdown(struct pt_regs *regs)
  94. {
  95. unsigned long msecs;
  96. local_irq_disable();
  97. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  98. smp_call_function(machine_crash_nonpanic_core, NULL, false);
  99. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  100. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  101. mdelay(1);
  102. msecs--;
  103. }
  104. if (atomic_read(&waiting_for_crash_ipi) > 0)
  105. printk(KERN_WARNING "Non-crashing CPUs did not react to IPI\n");
  106. crash_save_cpu(regs, smp_processor_id());
  107. machine_kexec_mask_interrupts();
  108. printk(KERN_INFO "Loading crashdump kernel...\n");
  109. }
  110. /*
  111. * Function pointer to optional machine-specific reinitialization
  112. */
  113. void (*kexec_reinit)(void);
  114. void machine_kexec(struct kimage *image)
  115. {
  116. unsigned long page_list;
  117. unsigned long reboot_code_buffer_phys;
  118. unsigned long reboot_entry = (unsigned long)relocate_new_kernel;
  119. unsigned long reboot_entry_phys;
  120. void *reboot_code_buffer;
  121. /*
  122. * This can only happen if machine_shutdown() failed to disable some
  123. * CPU, and that can only happen if the checks in
  124. * machine_kexec_prepare() were not correct. If this fails, we can't
  125. * reliably kexec anyway, so BUG_ON is appropriate.
  126. */
  127. BUG_ON(num_online_cpus() > 1);
  128. page_list = image->head & PAGE_MASK;
  129. /* we need both effective and real address here */
  130. reboot_code_buffer_phys =
  131. page_to_pfn(image->control_code_page) << PAGE_SHIFT;
  132. reboot_code_buffer = page_address(image->control_code_page);
  133. /* Prepare parameters for reboot_code_buffer*/
  134. kexec_start_address = image->start;
  135. kexec_indirection_page = page_list;
  136. kexec_mach_type = machine_arch_type;
  137. if (!kexec_boot_atags)
  138. kexec_boot_atags = image->start - KEXEC_ARM_ZIMAGE_OFFSET + KEXEC_ARM_ATAGS_OFFSET;
  139. /* copy our kernel relocation code to the control code page */
  140. reboot_entry = fncpy(reboot_code_buffer,
  141. reboot_entry,
  142. relocate_new_kernel_size);
  143. reboot_entry_phys = (unsigned long)reboot_entry +
  144. (reboot_code_buffer_phys - (unsigned long)reboot_code_buffer);
  145. printk(KERN_INFO "Bye!\n");
  146. if (kexec_reinit)
  147. kexec_reinit();
  148. soft_restart(reboot_entry_phys);
  149. }