Path: blob/master/arch/loongarch/kernel/machine_kexec.c
51070 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* machine_kexec.c for kexec3*4* Copyright (C) 2022 Loongson Technology Corporation Limited5*/6#include <linux/compiler.h>7#include <linux/cpu.h>8#include <linux/kexec.h>9#include <linux/crash_dump.h>10#include <linux/delay.h>11#include <linux/irq.h>12#include <linux/libfdt.h>13#include <linux/mm.h>14#include <linux/of_fdt.h>15#include <linux/reboot.h>16#include <linux/sched.h>17#include <linux/sched/task_stack.h>1819#include <asm/bootinfo.h>20#include <asm/cacheflush.h>21#include <asm/page.h>2223/* 0x100000 ~ 0x200000 is safe */24#define KEXEC_CONTROL_CODE TO_CACHE(0x100000UL)25#define KEXEC_CMDLINE_ADDR TO_CACHE(0x108000UL)2627static unsigned long reboot_code_buffer;28static cpumask_t cpus_in_crash = CPU_MASK_NONE;2930#ifdef CONFIG_SMP31static void (*relocated_kexec_smp_wait)(void *);32atomic_t kexec_ready_to_reboot = ATOMIC_INIT(0);33#endif3435static unsigned long efi_boot;36static unsigned long cmdline_ptr;37static unsigned long systable_ptr;38static unsigned long start_addr;39static unsigned long first_ind_entry;4041int machine_kexec_prepare(struct kimage *kimage)42{43int i;44char *bootloader = "kexec";45void *cmdline_ptr = (void *)KEXEC_CMDLINE_ADDR;4647kimage->arch.efi_boot = fw_arg0;48kimage->arch.systable_ptr = fw_arg2;4950if (kimage->file_mode == 1) {51/*52* kimage->cmdline_buf will be released in kexec_file_load, so copy53* to the KEXEC_CMDLINE_ADDR safe area.54*/55memcpy((void *)KEXEC_CMDLINE_ADDR, (void *)kimage->arch.cmdline_ptr,56strlen((char *)kimage->arch.cmdline_ptr) + 1);57kimage->arch.cmdline_ptr = (unsigned long)KEXEC_CMDLINE_ADDR;58} else {59/* Find the command line */60for (i = 0; i < kimage->nr_segments; i++) {61if (!strncmp(bootloader, (char __user *)kimage->segment[i].buf, strlen(bootloader))) {62if (!copy_from_user(cmdline_ptr, kimage->segment[i].buf, COMMAND_LINE_SIZE))63kimage->arch.cmdline_ptr = (unsigned long)cmdline_ptr;64break;65}66}6768if (!kimage->arch.cmdline_ptr) {69pr_err("Command line not included in the provided image\n");70return -EINVAL;71}72}7374/* kexec/kdump need a safe page to save reboot_code_buffer */75kimage->control_code_page = virt_to_page((void *)KEXEC_CONTROL_CODE);7677reboot_code_buffer = (unsigned long)page_address(kimage->control_code_page);78memcpy((void *)reboot_code_buffer, relocate_new_kernel, relocate_new_kernel_size);7980#ifdef CONFIG_SMP81/* All secondary cpus now may jump to kexec_smp_wait cycle */82relocated_kexec_smp_wait = reboot_code_buffer + (void *)(kexec_smp_wait - relocate_new_kernel);83#endif8485return 0;86}8788void machine_kexec_cleanup(struct kimage *kimage)89{90}9192void kexec_reboot(void)93{94do_kexec_t do_kexec = NULL;9596/*97* We know we were online, and there will be no incoming IPIs at98* this point. Mark online again before rebooting so that the crash99* analysis tool will see us correctly.100*/101set_cpu_online(smp_processor_id(), true);102103/* Ensure remote CPUs observe that we're online before rebooting. */104smp_mb__after_atomic();105106/*107* Make sure we get correct instructions written by the108* machine_kexec_prepare() CPU.109*/110__asm__ __volatile__ ("\tibar 0\n"::);111112#ifdef CONFIG_SMP113/* All secondary cpus go to kexec_smp_wait */114if (smp_processor_id() > 0) {115relocated_kexec_smp_wait(NULL);116BUG();117}118#endif119120do_kexec = (void *)reboot_code_buffer;121do_kexec(efi_boot, cmdline_ptr, systable_ptr, start_addr, first_ind_entry);122123BUG();124}125126127#ifdef CONFIG_SMP128static void kexec_shutdown_secondary(void *regs)129{130int cpu = smp_processor_id();131132if (!cpu_online(cpu))133return;134135/* We won't be sent IPIs any more. */136set_cpu_online(cpu, false);137138local_irq_disable();139while (!atomic_read(&kexec_ready_to_reboot))140cpu_relax();141142kexec_reboot();143}144145static void crash_shutdown_secondary(void *passed_regs)146{147int cpu = smp_processor_id();148struct pt_regs *regs = passed_regs;149150/*151* If we are passed registers, use those. Otherwise get the152* regs from the last interrupt, which should be correct, as153* we are in an interrupt. But if the regs are not there,154* pull them from the top of the stack. They are probably155* wrong, but we need something to keep from crashing again.156*/157if (!regs)158regs = get_irq_regs();159if (!regs)160regs = task_pt_regs(current);161162if (!cpu_online(cpu))163return;164165/* We won't be sent IPIs any more. */166set_cpu_online(cpu, false);167168local_irq_disable();169if (!cpumask_test_cpu(cpu, &cpus_in_crash))170crash_save_cpu(regs, cpu);171cpumask_set_cpu(cpu, &cpus_in_crash);172173while (!atomic_read(&kexec_ready_to_reboot))174cpu_relax();175176kexec_reboot();177}178179void crash_smp_send_stop(void)180{181unsigned int ncpus;182unsigned long timeout;183static int cpus_stopped;184185/*186* This function can be called twice in panic path, but obviously187* we should execute this only once.188*/189if (cpus_stopped)190return;191192cpus_stopped = 1;193194/* Excluding the panic cpu */195ncpus = num_online_cpus() - 1;196197smp_call_function(crash_shutdown_secondary, NULL, 0);198smp_wmb();199200/*201* The crash CPU sends an IPI and wait for other CPUs to202* respond. Delay of at least 10 seconds.203*/204timeout = MSEC_PER_SEC * 10;205pr_emerg("Sending IPI to other cpus...\n");206while ((cpumask_weight(&cpus_in_crash) < ncpus) && timeout--) {207mdelay(1);208cpu_relax();209}210}211#endif /* defined(CONFIG_SMP) */212213void machine_shutdown(void)214{215#ifdef CONFIG_SMP216int cpu;217218/* All CPUs go to reboot_code_buffer */219for_each_possible_cpu(cpu)220if (!cpu_online(cpu))221cpu_device_up(get_cpu_device(cpu));222223smp_call_function(kexec_shutdown_secondary, NULL, 0);224#endif225}226227void machine_crash_shutdown(struct pt_regs *regs)228{229int crashing_cpu;230231local_irq_disable();232233crashing_cpu = smp_processor_id();234crash_save_cpu(regs, crashing_cpu);235236#ifdef CONFIG_SMP237crash_smp_send_stop();238#endif239machine_kexec_mask_interrupts();240cpumask_set_cpu(crashing_cpu, &cpus_in_crash);241242pr_info("Starting crashdump kernel...\n");243}244245void machine_kexec(struct kimage *image)246{247unsigned long entry, *ptr;248struct kimage_arch *internal = &image->arch;249250efi_boot = internal->efi_boot;251cmdline_ptr = internal->cmdline_ptr;252systable_ptr = internal->systable_ptr;253254start_addr = (unsigned long)phys_to_virt(image->start);255256first_ind_entry = (image->type == KEXEC_TYPE_DEFAULT) ?257(unsigned long)phys_to_virt(image->head & PAGE_MASK) : 0;258259/*260* The generic kexec code builds a page list with physical261* addresses. they are directly accessible through XKPRANGE262* hence the phys_to_virt() call.263*/264for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE);265ptr = (entry & IND_INDIRECTION) ?266phys_to_virt(entry & PAGE_MASK) : ptr + 1) {267if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||268*ptr & IND_DESTINATION)269*ptr = (unsigned long) phys_to_virt(*ptr);270}271272/* Mark offline before disabling local irq. */273set_cpu_online(smp_processor_id(), false);274275/* We do not want to be bothered. */276local_irq_disable();277machine_kexec_mask_interrupts();278279pr_notice("EFI boot flag: 0x%lx\n", efi_boot);280pr_notice("Command line addr: 0x%lx\n", cmdline_ptr);281pr_notice("Command line string: %s\n", (char *)cmdline_ptr);282pr_notice("System table addr: 0x%lx\n", systable_ptr);283pr_notice("We will call new kernel at 0x%lx\n", start_addr);284pr_notice("Bye ...\n");285286/* Make reboot code buffer available to the boot CPU. */287flush_cache_all();288289#ifdef CONFIG_SMP290atomic_set(&kexec_ready_to_reboot, 1);291#endif292293kexec_reboot();294}295296297