#include <linux/kexec.h>
#include <asm/kexec.h>
#include <linux/smp.h>
#include <asm/cacheflush.h>
#include <asm/barrier.h>
#include <asm/page.h>
#include <linux/libfdt.h>
#include <asm/set_memory.h>
#include <linux/compiler.h>
#include <linux/cpu.h>
#include <linux/reboot.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
int
machine_kexec_prepare(struct kimage *image)
{
struct kimage_arch *internal = &image->arch;
struct fdt_header fdt = {0};
void *control_code_buffer = NULL;
unsigned int control_code_buffer_sz = 0;
int i = 0;
for (i = 0; i < image->nr_segments; i++) {
if (image->segment[i].memsz <= sizeof(fdt))
continue;
if (image->file_mode)
memcpy(&fdt, image->segment[i].buf, sizeof(fdt));
else if (copy_from_user(&fdt, image->segment[i].buf, sizeof(fdt)))
continue;
if (fdt_check_header(&fdt))
continue;
internal->fdt_addr = (unsigned long) image->segment[i].mem;
break;
}
if (!internal->fdt_addr) {
pr_err("Device tree not included in the provided image\n");
return -EINVAL;
}
if (image->type != KEXEC_TYPE_CRASH) {
control_code_buffer = page_address(image->control_code_page);
control_code_buffer_sz = page_size(image->control_code_page);
if (unlikely(riscv_kexec_relocate_size > control_code_buffer_sz)) {
pr_err("Relocation code doesn't fit within a control page\n");
return -EINVAL;
}
memcpy(control_code_buffer, riscv_kexec_relocate,
riscv_kexec_relocate_size);
set_memory_x((unsigned long) control_code_buffer, 1);
}
return 0;
}
void
machine_kexec_cleanup(struct kimage *image)
{
}
void machine_shutdown(void)
{
local_irq_disable();
#if defined(CONFIG_HOTPLUG_CPU)
smp_shutdown_nonboot_cpus(smp_processor_id());
#endif
}
void
machine_crash_shutdown(struct pt_regs *regs)
{
local_irq_disable();
crash_smp_send_stop();
crash_save_cpu(regs, smp_processor_id());
machine_kexec_mask_interrupts();
pr_info("Starting crashdump kernel...\n");
}
void __noreturn
machine_kexec(struct kimage *image)
{
struct kimage_arch *internal = &image->arch;
unsigned long jump_addr = (unsigned long) image->start;
unsigned long first_ind_entry = (unsigned long) &image->head;
unsigned long this_cpu_id = __smp_processor_id();
unsigned long this_hart_id = cpuid_to_hartid_map(this_cpu_id);
unsigned long fdt_addr = internal->fdt_addr;
void *control_code_buffer = page_address(image->control_code_page);
riscv_kexec_method kexec_method = NULL;
#ifdef CONFIG_SMP
WARN(smp_crash_stop_failed(),
"Some CPUs may be stale, kdump will be unreliable.\n");
#endif
if (image->type != KEXEC_TYPE_CRASH)
kexec_method = control_code_buffer;
else
kexec_method = (riscv_kexec_method) &riscv_kexec_norelocate;
pr_notice("Will call new kernel at %08lx from hart id %lx\n",
jump_addr, this_hart_id);
pr_notice("FDT image at %08lx\n", fdt_addr);
local_flush_icache_all();
pr_notice("Bye...\n");
kexec_method(first_ind_entry, jump_addr, fdt_addr,
this_hart_id, kernel_map.va_pa_offset);
unreachable();
}