machine_kexec.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/mach-types.h>
  17. #include <asm/system_misc.h>
  18. extern const unsigned char relocate_new_kernel[];
  19. extern const unsigned int relocate_new_kernel_size;
  20. extern unsigned long kexec_start_address;
  21. extern unsigned long kexec_indirection_page;
  22. extern unsigned long kexec_mach_type;
  23. extern unsigned long kexec_boot_atags;
  24. static atomic_t waiting_for_crash_ipi;
  25. /*
  26. * Provide a dummy crash_notes definition while crash dump arrives to arm.
  27. * This prevents breakage of crash_notes attribute in kernel/ksysfs.c.
  28. */
  29. int machine_kexec_prepare(struct kimage *image)
  30. {
  31. struct kexec_segment *current_segment;
  32. __be32 header;
  33. int i, err;
  34. /*
  35. * No segment at default ATAGs address. try to locate
  36. * a dtb using magic.
  37. */
  38. for (i = 0; i < image->nr_segments; i++) {
  39. current_segment = &image->segment[i];
  40. if (!memblock_is_region_memory(current_segment->mem,
  41. current_segment->memsz))
  42. return -EINVAL;
  43. err = get_user(header, (__be32*)current_segment->buf);
  44. if (err)
  45. return err;
  46. if (be32_to_cpu(header) == OF_DT_HEADER)
  47. kexec_boot_atags = current_segment->mem;
  48. }
  49. return 0;
  50. }
  51. void machine_kexec_cleanup(struct kimage *image)
  52. {
  53. }
  54. void machine_crash_nonpanic_core(void *unused)
  55. {
  56. struct pt_regs regs;
  57. crash_setup_regs(&regs, NULL);
  58. printk(KERN_DEBUG "CPU %u will stop doing anything useful since another CPU has crashed\n",
  59. smp_processor_id());
  60. crash_save_cpu(&regs, smp_processor_id());
  61. flush_cache_all();
  62. atomic_dec(&waiting_for_crash_ipi);
  63. while (1)
  64. cpu_relax();
  65. }
  66. static void machine_kexec_mask_interrupts(void)
  67. {
  68. unsigned int i;
  69. struct irq_desc *desc;
  70. for_each_irq_desc(i, desc) {
  71. struct irq_chip *chip;
  72. chip = irq_desc_get_chip(desc);
  73. if (!chip)
  74. continue;
  75. if (chip->irq_eoi && irqd_irq_inprogress(&desc->irq_data))
  76. chip->irq_eoi(&desc->irq_data);
  77. if (chip->irq_mask)
  78. chip->irq_mask(&desc->irq_data);
  79. if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
  80. chip->irq_disable(&desc->irq_data);
  81. }
  82. }
  83. void machine_crash_shutdown(struct pt_regs *regs)
  84. {
  85. unsigned long msecs;
  86. local_irq_disable();
  87. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  88. smp_call_function(machine_crash_nonpanic_core, NULL, false);
  89. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  90. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  91. mdelay(1);
  92. msecs--;
  93. }
  94. if (atomic_read(&waiting_for_crash_ipi) > 0)
  95. printk(KERN_WARNING "Non-crashing CPUs did not react to IPI\n");
  96. crash_save_cpu(regs, smp_processor_id());
  97. machine_kexec_mask_interrupts();
  98. printk(KERN_INFO "Loading crashdump kernel...\n");
  99. }
  100. /*
  101. * Function pointer to optional machine-specific reinitialization
  102. */
  103. void (*kexec_reinit)(void);
  104. void machine_kexec(struct kimage *image)
  105. {
  106. unsigned long page_list;
  107. unsigned long reboot_code_buffer_phys;
  108. void *reboot_code_buffer;
  109. page_list = image->head & PAGE_MASK;
  110. /* we need both effective and real address here */
  111. reboot_code_buffer_phys =
  112. page_to_pfn(image->control_code_page) << PAGE_SHIFT;
  113. reboot_code_buffer = page_address(image->control_code_page);
  114. /* Prepare parameters for reboot_code_buffer*/
  115. kexec_start_address = image->start;
  116. kexec_indirection_page = page_list;
  117. kexec_mach_type = machine_arch_type;
  118. if (!kexec_boot_atags)
  119. kexec_boot_atags = image->start - KEXEC_ARM_ZIMAGE_OFFSET + KEXEC_ARM_ATAGS_OFFSET;
  120. /* copy our kernel relocation code to the control code page */
  121. memcpy(reboot_code_buffer,
  122. relocate_new_kernel, relocate_new_kernel_size);
  123. flush_icache_range((unsigned long) reboot_code_buffer,
  124. (unsigned long) reboot_code_buffer + KEXEC_CONTROL_PAGE_SIZE);
  125. printk(KERN_INFO "Bye!\n");
  126. if (kexec_reinit)
  127. kexec_reinit();
  128. soft_restart(reboot_code_buffer_phys);
  129. }