drm/rockchip: vop2: Fix the windows switch between different layers
[drm/drm-misc.git] / arch / riscv / kernel / machine_kexec.c
blob3c830a6f7ef464ba2e10e3ffad6c53d0bae3c4ce
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2019 FORTH-ICS/CARV
4 * Nick Kossifidis <mick@ics.forth.gr>
5 */
7 #include <linux/kexec.h>
8 #include <asm/kexec.h> /* For riscv_kexec_* symbol defines */
9 #include <linux/smp.h> /* For smp_send_stop () */
10 #include <asm/cacheflush.h> /* For local_flush_icache_all() */
11 #include <asm/barrier.h> /* For smp_wmb() */
12 #include <asm/page.h> /* For PAGE_MASK */
13 #include <linux/libfdt.h> /* For fdt_check_header() */
14 #include <asm/set_memory.h> /* For set_memory_x() */
15 #include <linux/compiler.h> /* For unreachable() */
16 #include <linux/cpu.h> /* For cpu_down() */
17 #include <linux/reboot.h>
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
22 * machine_kexec_prepare - Initialize kexec
24 * This function is called from do_kexec_load, when the user has
25 * provided us with an image to be loaded. Its goal is to validate
26 * the image and prepare the control code buffer as needed.
27 * Note that kimage_alloc_init has already been called and the
28 * control buffer has already been allocated.
30 int
31 machine_kexec_prepare(struct kimage *image)
33 struct kimage_arch *internal = &image->arch;
34 struct fdt_header fdt = {0};
35 void *control_code_buffer = NULL;
36 unsigned int control_code_buffer_sz = 0;
37 int i = 0;
39 /* Find the Flattened Device Tree and save its physical address */
40 for (i = 0; i < image->nr_segments; i++) {
41 if (image->segment[i].memsz <= sizeof(fdt))
42 continue;
44 if (image->file_mode)
45 memcpy(&fdt, image->segment[i].buf, sizeof(fdt));
46 else if (copy_from_user(&fdt, image->segment[i].buf, sizeof(fdt)))
47 continue;
49 if (fdt_check_header(&fdt))
50 continue;
52 internal->fdt_addr = (unsigned long) image->segment[i].mem;
53 break;
56 if (!internal->fdt_addr) {
57 pr_err("Device tree not included in the provided image\n");
58 return -EINVAL;
61 /* Copy the assembler code for relocation to the control page */
62 if (image->type != KEXEC_TYPE_CRASH) {
63 control_code_buffer = page_address(image->control_code_page);
64 control_code_buffer_sz = page_size(image->control_code_page);
66 if (unlikely(riscv_kexec_relocate_size > control_code_buffer_sz)) {
67 pr_err("Relocation code doesn't fit within a control page\n");
68 return -EINVAL;
71 memcpy(control_code_buffer, riscv_kexec_relocate,
72 riscv_kexec_relocate_size);
74 /* Mark the control page executable */
75 set_memory_x((unsigned long) control_code_buffer, 1);
78 return 0;
83 * machine_kexec_cleanup - Cleanup any leftovers from
84 * machine_kexec_prepare
86 * This function is called by kimage_free to handle any arch-specific
87 * allocations done on machine_kexec_prepare. Since we didn't do any
88 * allocations there, this is just an empty function. Note that the
89 * control buffer is freed by kimage_free.
91 void
92 machine_kexec_cleanup(struct kimage *image)
98 * machine_shutdown - Prepare for a kexec reboot
100 * This function is called by kernel_kexec just before machine_kexec
101 * below. Its goal is to prepare the rest of the system (the other
102 * harts and possibly devices etc) for a kexec reboot.
104 void machine_shutdown(void)
107 * No more interrupts on this hart
108 * until we are back up.
110 local_irq_disable();
112 #if defined(CONFIG_HOTPLUG_CPU)
113 smp_shutdown_nonboot_cpus(smp_processor_id());
114 #endif
117 static void machine_kexec_mask_interrupts(void)
119 unsigned int i;
120 struct irq_desc *desc;
122 for_each_irq_desc(i, desc) {
123 struct irq_chip *chip;
125 chip = irq_desc_get_chip(desc);
126 if (!chip)
127 continue;
129 if (chip->irq_eoi && irqd_irq_inprogress(&desc->irq_data))
130 chip->irq_eoi(&desc->irq_data);
132 if (chip->irq_mask)
133 chip->irq_mask(&desc->irq_data);
135 if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
136 chip->irq_disable(&desc->irq_data);
141 * machine_crash_shutdown - Prepare to kexec after a kernel crash
143 * This function is called by crash_kexec just before machine_kexec
144 * and its goal is to shutdown non-crashing cpus and save registers.
146 void
147 machine_crash_shutdown(struct pt_regs *regs)
149 local_irq_disable();
151 /* shutdown non-crashing cpus */
152 crash_smp_send_stop();
154 crash_save_cpu(regs, smp_processor_id());
155 machine_kexec_mask_interrupts();
157 pr_info("Starting crashdump kernel...\n");
161 * machine_kexec - Jump to the loaded kimage
163 * This function is called by kernel_kexec which is called by the
164 * reboot system call when the reboot cmd is LINUX_REBOOT_CMD_KEXEC,
165 * or by crash_kernel which is called by the kernel's arch-specific
166 * trap handler in case of a kernel panic. It's the final stage of
167 * the kexec process where the pre-loaded kimage is ready to be
168 * executed. We assume at this point that all other harts are
169 * suspended and this hart will be the new boot hart.
171 void __noreturn
172 machine_kexec(struct kimage *image)
174 struct kimage_arch *internal = &image->arch;
175 unsigned long jump_addr = (unsigned long) image->start;
176 unsigned long first_ind_entry = (unsigned long) &image->head;
177 unsigned long this_cpu_id = __smp_processor_id();
178 unsigned long this_hart_id = cpuid_to_hartid_map(this_cpu_id);
179 unsigned long fdt_addr = internal->fdt_addr;
180 void *control_code_buffer = page_address(image->control_code_page);
181 riscv_kexec_method kexec_method = NULL;
183 #ifdef CONFIG_SMP
184 WARN(smp_crash_stop_failed(),
185 "Some CPUs may be stale, kdump will be unreliable.\n");
186 #endif
188 if (image->type != KEXEC_TYPE_CRASH)
189 kexec_method = control_code_buffer;
190 else
191 kexec_method = (riscv_kexec_method) &riscv_kexec_norelocate;
193 pr_notice("Will call new kernel at %08lx from hart id %lx\n",
194 jump_addr, this_hart_id);
195 pr_notice("FDT image at %08lx\n", fdt_addr);
197 /* Make sure the relocation code is visible to the hart */
198 local_flush_icache_all();
200 /* Jump to the relocation code */
201 pr_notice("Bye...\n");
202 kexec_method(first_ind_entry, jump_addr, fdt_addr,
203 this_hart_id, kernel_map.va_pa_offset);
204 unreachable();