Path: blob/master/arch/loongarch/kernel/machine_kexec.c
29521 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;4041static void kexec_image_info(const struct kimage *kimage)42{43unsigned long i;4445pr_debug("kexec kimage info:\n");46pr_debug("\ttype: %d\n", kimage->type);47pr_debug("\tstart: %lx\n", kimage->start);48pr_debug("\thead: %lx\n", kimage->head);49pr_debug("\tnr_segments: %lu\n", kimage->nr_segments);5051for (i = 0; i < kimage->nr_segments; i++) {52pr_debug("\t segment[%lu]: %016lx - %016lx", i,53kimage->segment[i].mem,54kimage->segment[i].mem + kimage->segment[i].memsz);55pr_debug("\t\t0x%lx bytes, %lu pages\n",56(unsigned long)kimage->segment[i].memsz,57(unsigned long)kimage->segment[i].memsz / PAGE_SIZE);58}59}6061int machine_kexec_prepare(struct kimage *kimage)62{63int i;64char *bootloader = "kexec";65void *cmdline_ptr = (void *)KEXEC_CMDLINE_ADDR;6667kexec_image_info(kimage);6869kimage->arch.efi_boot = fw_arg0;70kimage->arch.systable_ptr = fw_arg2;7172if (kimage->file_mode == 1) {73/*74* kimage->cmdline_buf will be released in kexec_file_load, so copy75* to the KEXEC_CMDLINE_ADDR safe area.76*/77memcpy((void *)KEXEC_CMDLINE_ADDR, (void *)kimage->arch.cmdline_ptr,78strlen((char *)kimage->arch.cmdline_ptr) + 1);79kimage->arch.cmdline_ptr = (unsigned long)KEXEC_CMDLINE_ADDR;80} else {81/* Find the command line */82for (i = 0; i < kimage->nr_segments; i++) {83if (!strncmp(bootloader, (char __user *)kimage->segment[i].buf, strlen(bootloader))) {84if (!copy_from_user(cmdline_ptr, kimage->segment[i].buf, COMMAND_LINE_SIZE))85kimage->arch.cmdline_ptr = (unsigned long)cmdline_ptr;86break;87}88}8990if (!kimage->arch.cmdline_ptr) {91pr_err("Command line not included in the provided image\n");92return -EINVAL;93}94}9596/* kexec/kdump need a safe page to save reboot_code_buffer */97kimage->control_code_page = virt_to_page((void *)KEXEC_CONTROL_CODE);9899reboot_code_buffer = (unsigned long)page_address(kimage->control_code_page);100memcpy((void *)reboot_code_buffer, relocate_new_kernel, relocate_new_kernel_size);101102#ifdef CONFIG_SMP103/* All secondary cpus now may jump to kexec_smp_wait cycle */104relocated_kexec_smp_wait = reboot_code_buffer + (void *)(kexec_smp_wait - relocate_new_kernel);105#endif106107return 0;108}109110void machine_kexec_cleanup(struct kimage *kimage)111{112}113114void kexec_reboot(void)115{116do_kexec_t do_kexec = NULL;117118/*119* We know we were online, and there will be no incoming IPIs at120* this point. Mark online again before rebooting so that the crash121* analysis tool will see us correctly.122*/123set_cpu_online(smp_processor_id(), true);124125/* Ensure remote CPUs observe that we're online before rebooting. */126smp_mb__after_atomic();127128/*129* Make sure we get correct instructions written by the130* machine_kexec_prepare() CPU.131*/132__asm__ __volatile__ ("\tibar 0\n"::);133134#ifdef CONFIG_SMP135/* All secondary cpus go to kexec_smp_wait */136if (smp_processor_id() > 0) {137relocated_kexec_smp_wait(NULL);138BUG();139}140#endif141142do_kexec = (void *)reboot_code_buffer;143do_kexec(efi_boot, cmdline_ptr, systable_ptr, start_addr, first_ind_entry);144145BUG();146}147148149#ifdef CONFIG_SMP150static void kexec_shutdown_secondary(void *regs)151{152int cpu = smp_processor_id();153154if (!cpu_online(cpu))155return;156157/* We won't be sent IPIs any more. */158set_cpu_online(cpu, false);159160local_irq_disable();161while (!atomic_read(&kexec_ready_to_reboot))162cpu_relax();163164kexec_reboot();165}166167static void crash_shutdown_secondary(void *passed_regs)168{169int cpu = smp_processor_id();170struct pt_regs *regs = passed_regs;171172/*173* If we are passed registers, use those. Otherwise get the174* regs from the last interrupt, which should be correct, as175* we are in an interrupt. But if the regs are not there,176* pull them from the top of the stack. They are probably177* wrong, but we need something to keep from crashing again.178*/179if (!regs)180regs = get_irq_regs();181if (!regs)182regs = task_pt_regs(current);183184if (!cpu_online(cpu))185return;186187/* We won't be sent IPIs any more. */188set_cpu_online(cpu, false);189190local_irq_disable();191if (!cpumask_test_cpu(cpu, &cpus_in_crash))192crash_save_cpu(regs, cpu);193cpumask_set_cpu(cpu, &cpus_in_crash);194195while (!atomic_read(&kexec_ready_to_reboot))196cpu_relax();197198kexec_reboot();199}200201void crash_smp_send_stop(void)202{203unsigned int ncpus;204unsigned long timeout;205static int cpus_stopped;206207/*208* This function can be called twice in panic path, but obviously209* we should execute this only once.210*/211if (cpus_stopped)212return;213214cpus_stopped = 1;215216/* Excluding the panic cpu */217ncpus = num_online_cpus() - 1;218219smp_call_function(crash_shutdown_secondary, NULL, 0);220smp_wmb();221222/*223* The crash CPU sends an IPI and wait for other CPUs to224* respond. Delay of at least 10 seconds.225*/226timeout = MSEC_PER_SEC * 10;227pr_emerg("Sending IPI to other cpus...\n");228while ((cpumask_weight(&cpus_in_crash) < ncpus) && timeout--) {229mdelay(1);230cpu_relax();231}232}233#endif /* defined(CONFIG_SMP) */234235void machine_shutdown(void)236{237#ifdef CONFIG_SMP238int cpu;239240/* All CPUs go to reboot_code_buffer */241for_each_possible_cpu(cpu)242if (!cpu_online(cpu))243cpu_device_up(get_cpu_device(cpu));244245smp_call_function(kexec_shutdown_secondary, NULL, 0);246#endif247}248249void machine_crash_shutdown(struct pt_regs *regs)250{251int crashing_cpu;252253local_irq_disable();254255crashing_cpu = smp_processor_id();256crash_save_cpu(regs, crashing_cpu);257258#ifdef CONFIG_SMP259crash_smp_send_stop();260#endif261cpumask_set_cpu(crashing_cpu, &cpus_in_crash);262263pr_info("Starting crashdump kernel...\n");264}265266void machine_kexec(struct kimage *image)267{268unsigned long entry, *ptr;269struct kimage_arch *internal = &image->arch;270271efi_boot = internal->efi_boot;272cmdline_ptr = internal->cmdline_ptr;273systable_ptr = internal->systable_ptr;274275start_addr = (unsigned long)phys_to_virt(image->start);276277first_ind_entry = (image->type == KEXEC_TYPE_DEFAULT) ?278(unsigned long)phys_to_virt(image->head & PAGE_MASK) : 0;279280/*281* The generic kexec code builds a page list with physical282* addresses. they are directly accessible through XKPRANGE283* hence the phys_to_virt() call.284*/285for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE);286ptr = (entry & IND_INDIRECTION) ?287phys_to_virt(entry & PAGE_MASK) : ptr + 1) {288if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||289*ptr & IND_DESTINATION)290*ptr = (unsigned long) phys_to_virt(*ptr);291}292293/* Mark offline before disabling local irq. */294set_cpu_online(smp_processor_id(), false);295296/* We do not want to be bothered. */297local_irq_disable();298299pr_notice("EFI boot flag: 0x%lx\n", efi_boot);300pr_notice("Command line addr: 0x%lx\n", cmdline_ptr);301pr_notice("Command line string: %s\n", (char *)cmdline_ptr);302pr_notice("System table addr: 0x%lx\n", systable_ptr);303pr_notice("We will call new kernel at 0x%lx\n", start_addr);304pr_notice("Bye ...\n");305306/* Make reboot code buffer available to the boot CPU. */307flush_cache_all();308309#ifdef CONFIG_SMP310atomic_set(&kexec_ready_to_reboot, 1);311#endif312313kexec_reboot();314}315316317