machine_kexec.c 4.8 KB
Newer Older
R
Richard Purdie 已提交
1 2 3 4 5 6 7 8
/*
 * machine_kexec.c - handle transition of Linux booting another kernel
 */

#include <linux/mm.h>
#include <linux/kexec.h>
#include <linux/delay.h>
#include <linux/reboot.h>
9
#include <linux/io.h>
10
#include <linux/irq.h>
11
#include <linux/memblock.h>
R
Richard Purdie 已提交
12
#include <asm/pgtable.h>
13
#include <linux/of_fdt.h>
R
Richard Purdie 已提交
14 15 16
#include <asm/pgalloc.h>
#include <asm/mmu_context.h>
#include <asm/cacheflush.h>
17
#include <asm/fncpy.h>
R
Richard Purdie 已提交
18
#include <asm/mach-types.h>
19
#include <asm/smp_plat.h>
20
#include <asm/system_misc.h>
L
Laura Abbott 已提交
21
#include <asm/set_memory.h>
R
Richard Purdie 已提交
22

23
extern void relocate_new_kernel(void);
24
extern const unsigned int relocate_new_kernel_size;
R
Richard Purdie 已提交
25 26 27 28

extern unsigned long kexec_start_address;
extern unsigned long kexec_indirection_page;
extern unsigned long kexec_mach_type;
29
extern unsigned long kexec_boot_atags;
R
Richard Purdie 已提交
30

31 32
static atomic_t waiting_for_crash_ipi;

33
static unsigned long dt_mem;
R
Richard Purdie 已提交
34 35 36 37 38 39 40
/*
 * Provide a dummy crash_notes definition while crash dump arrives to arm.
 * This prevents breakage of crash_notes attribute in kernel/ksysfs.c.
 */

int machine_kexec_prepare(struct kimage *image)
{
41 42 43 44
	struct kexec_segment *current_segment;
	__be32 header;
	int i, err;

45 46 47 48 49
	/*
	 * Validate that if the current HW supports SMP, then the SW supports
	 * and implements CPU hotplug for the current HW. If not, we won't be
	 * able to kexec reliably, so fail the prepare operation.
	 */
50 51
	if (num_possible_cpus() > 1 && platform_can_secondary_boot() &&
	    !platform_can_cpu_hotplug())
52 53
		return -EINVAL;

54 55 56 57 58 59 60
	/*
	 * No segment at default ATAGs address. try to locate
	 * a dtb using magic.
	 */
	for (i = 0; i < image->nr_segments; i++) {
		current_segment = &image->segment[i];

61
		if (!memblock_is_region_memory(idmap_to_phys(current_segment->mem),
62 63
					       current_segment->memsz))
			return -EINVAL;
64

65 66 67 68 69
		err = get_user(header, (__be32*)current_segment->buf);
		if (err)
			return err;

		if (be32_to_cpu(header) == OF_DT_HEADER)
70
			dt_mem = current_segment->mem;
71
	}
R
Richard Purdie 已提交
72 73 74 75 76 77 78
	return 0;
}

void machine_kexec_cleanup(struct kimage *image)
{
}

79 80 81 82 83 84 85 86 87 88
void machine_crash_nonpanic_core(void *unused)
{
	struct pt_regs regs;

	crash_setup_regs(&regs, NULL);
	printk(KERN_DEBUG "CPU %u will stop doing anything useful since another CPU has crashed\n",
	       smp_processor_id());
	crash_save_cpu(&regs, smp_processor_id());
	flush_cache_all();

89
	set_cpu_online(smp_processor_id(), false);
90 91 92 93 94
	atomic_dec(&waiting_for_crash_ipi);
	while (1)
		cpu_relax();
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
static void machine_kexec_mask_interrupts(void)
{
	unsigned int i;
	struct irq_desc *desc;

	for_each_irq_desc(i, desc) {
		struct irq_chip *chip;

		chip = irq_desc_get_chip(desc);
		if (!chip)
			continue;

		if (chip->irq_eoi && irqd_irq_inprogress(&desc->irq_data))
			chip->irq_eoi(&desc->irq_data);

		if (chip->irq_mask)
			chip->irq_mask(&desc->irq_data);

		if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
			chip->irq_disable(&desc->irq_data);
	}
}

R
Richard Purdie 已提交
118 119
void machine_crash_shutdown(struct pt_regs *regs)
{
120 121
	unsigned long msecs;

122
	local_irq_disable();
123 124 125 126 127 128 129 130 131

	atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
	smp_call_function(machine_crash_nonpanic_core, NULL, false);
	msecs = 1000; /* Wait at most a second for the other cpus to stop */
	while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
		mdelay(1);
		msecs--;
	}
	if (atomic_read(&waiting_for_crash_ipi) > 0)
R
Russell King 已提交
132
		pr_warn("Non-crashing CPUs did not react to IPI\n");
133

134
	crash_save_cpu(regs, smp_processor_id());
135
	machine_kexec_mask_interrupts();
136

R
Russell King 已提交
137
	pr_info("Loading crashdump kernel...\n");
R
Richard Purdie 已提交
138 139
}

140 141 142 143 144
/*
 * Function pointer to optional machine-specific reinitialization
 */
void (*kexec_reinit)(void);

R
Richard Purdie 已提交
145 146
void machine_kexec(struct kimage *image)
{
147 148
	unsigned long page_list, reboot_entry_phys;
	void (*reboot_entry)(void);
R
Richard Purdie 已提交
149 150
	void *reboot_code_buffer;

151 152 153 154 155 156 157
	/*
	 * This can only happen if machine_shutdown() failed to disable some
	 * CPU, and that can only happen if the checks in
	 * machine_kexec_prepare() were not correct. If this fails, we can't
	 * reliably kexec anyway, so BUG_ON is appropriate.
	 */
	BUG_ON(num_online_cpus() > 1);
158 159 160

	page_list = image->head & PAGE_MASK;

R
Richard Purdie 已提交
161 162
	reboot_code_buffer = page_address(image->control_code_page);

163
	/* Prepare parameters for reboot_code_buffer*/
164
	set_kernel_text_rw();
165 166 167
	kexec_start_address = image->start;
	kexec_indirection_page = page_list;
	kexec_mach_type = machine_arch_type;
168 169
	kexec_boot_atags = dt_mem ?: image->start - KEXEC_ARM_ZIMAGE_OFFSET
				     + KEXEC_ARM_ATAGS_OFFSET;
170 171

	/* copy our kernel relocation code to the control code page */
172
	reboot_entry = fncpy(reboot_code_buffer,
173
			     &relocate_new_kernel,
174
			     relocate_new_kernel_size);
175 176 177

	/* get the identity mapping physical address for the reboot code */
	reboot_entry_phys = virt_to_idmap(reboot_entry);
178

R
Russell King 已提交
179
	pr_info("Bye!\n");
R
Richard Purdie 已提交
180

181 182
	if (kexec_reinit)
		kexec_reinit();
183

184
	soft_restart(reboot_entry_phys);
R
Richard Purdie 已提交
185
}
186 187 188 189 190 191 192

void arch_crash_save_vmcoreinfo(void)
{
#ifdef CONFIG_ARM_LPAE
	VMCOREINFO_CONFIG(ARM_LPAE);
#endif
}