machine_kexec.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. err = memblock_is_region_memory(current_segment->mem,
  41. current_segment->memsz);
  42. if (err)
  43. return - EINVAL;
  44. err = get_user(header, (__be32*)current_segment->buf);
  45. if (err)
  46. return err;
  47. if (be32_to_cpu(header) == OF_DT_HEADER)
  48. kexec_boot_atags = current_segment->mem;
  49. }
  50. return 0;
  51. }
  52. void machine_kexec_cleanup(struct kimage *image)
  53. {
  54. }
  55. void machine_crash_nonpanic_core(void *unused)
  56. {
  57. struct pt_regs regs;
  58. crash_setup_regs(&regs, NULL);
  59. printk(KERN_DEBUG "CPU %u will stop doing anything useful since another CPU has crashed\n",
  60. smp_processor_id());
  61. crash_save_cpu(&regs, smp_processor_id());
  62. flush_cache_all();
  63. atomic_dec(&waiting_for_crash_ipi);
  64. while (1)
  65. cpu_relax();
  66. }
  67. static void machine_kexec_mask_interrupts(void)
  68. {
  69. unsigned int i;
  70. struct irq_desc *desc;
  71. for_each_irq_desc(i, desc) {
  72. struct irq_chip *chip;
  73. chip = irq_desc_get_chip(desc);
  74. if (!chip)
  75. continue;
  76. if (chip->irq_eoi && irqd_irq_inprogress(&desc->irq_data))
  77. chip->irq_eoi(&desc->irq_data);
  78. if (chip->irq_mask)
  79. chip->irq_mask(&desc->irq_data);
  80. if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
  81. chip->irq_disable(&desc->irq_data);
  82. }
  83. }
  84. void machine_crash_shutdown(struct pt_regs *regs)
  85. {
  86. unsigned long msecs;
  87. local_irq_disable();
  88. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  89. smp_call_function(machine_crash_nonpanic_core, NULL, false);
  90. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  91. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  92. mdelay(1);
  93. msecs--;
  94. }
  95. if (atomic_read(&waiting_for_crash_ipi) > 0)
  96. printk(KERN_WARNING "Non-crashing CPUs did not react to IPI\n");
  97. crash_save_cpu(regs, smp_processor_id());
  98. machine_kexec_mask_interrupts();
  99. printk(KERN_INFO "Loading crashdump kernel...\n");
  100. }
  101. /*
  102. * Function pointer to optional machine-specific reinitialization
  103. */
  104. void (*kexec_reinit)(void);
  105. void machine_kexec(struct kimage *image)
  106. {
  107. unsigned long page_list;
  108. unsigned long reboot_code_buffer_phys;
  109. void *reboot_code_buffer;
  110. page_list = image->head & PAGE_MASK;
  111. /* we need both effective and real address here */
  112. reboot_code_buffer_phys =
  113. page_to_pfn(image->control_code_page) << PAGE_SHIFT;
  114. reboot_code_buffer = page_address(image->control_code_page);
  115. /* Prepare parameters for reboot_code_buffer*/
  116. kexec_start_address = image->start;
  117. kexec_indirection_page = page_list;
  118. kexec_mach_type = machine_arch_type;
  119. if (!kexec_boot_atags)
  120. kexec_boot_atags = image->start - KEXEC_ARM_ZIMAGE_OFFSET + KEXEC_ARM_ATAGS_OFFSET;
  121. /* copy our kernel relocation code to the control code page */
  122. memcpy(reboot_code_buffer,
  123. relocate_new_kernel, relocate_new_kernel_size);
  124. flush_icache_range((unsigned long) reboot_code_buffer,
  125. (unsigned long) reboot_code_buffer + KEXEC_CONTROL_PAGE_SIZE);
  126. printk(KERN_INFO "Bye!\n");
  127. if (kexec_reinit)
  128. kexec_reinit();
  129. soft_restart(reboot_code_buffer_phys);
  130. }