Path: blob/master/arch/loongarch/kernel/machine_kexec.c
26424 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;7172/* Find the command line */73for (i = 0; i < kimage->nr_segments; i++) {74if (!strncmp(bootloader, (char __user *)kimage->segment[i].buf, strlen(bootloader))) {75if (!copy_from_user(cmdline_ptr, kimage->segment[i].buf, COMMAND_LINE_SIZE))76kimage->arch.cmdline_ptr = (unsigned long)cmdline_ptr;77break;78}79}8081if (!kimage->arch.cmdline_ptr) {82pr_err("Command line not included in the provided image\n");83return -EINVAL;84}8586/* kexec/kdump need a safe page to save reboot_code_buffer */87kimage->control_code_page = virt_to_page((void *)KEXEC_CONTROL_CODE);8889reboot_code_buffer = (unsigned long)page_address(kimage->control_code_page);90memcpy((void *)reboot_code_buffer, relocate_new_kernel, relocate_new_kernel_size);9192#ifdef CONFIG_SMP93/* All secondary cpus now may jump to kexec_smp_wait cycle */94relocated_kexec_smp_wait = reboot_code_buffer + (void *)(kexec_smp_wait - relocate_new_kernel);95#endif9697return 0;98}99100void machine_kexec_cleanup(struct kimage *kimage)101{102}103104void kexec_reboot(void)105{106do_kexec_t do_kexec = NULL;107108/*109* We know we were online, and there will be no incoming IPIs at110* this point. Mark online again before rebooting so that the crash111* analysis tool will see us correctly.112*/113set_cpu_online(smp_processor_id(), true);114115/* Ensure remote CPUs observe that we're online before rebooting. */116smp_mb__after_atomic();117118/*119* Make sure we get correct instructions written by the120* machine_kexec_prepare() CPU.121*/122__asm__ __volatile__ ("\tibar 0\n"::);123124#ifdef CONFIG_SMP125/* All secondary cpus go to kexec_smp_wait */126if (smp_processor_id() > 0) {127relocated_kexec_smp_wait(NULL);128BUG();129}130#endif131132do_kexec = (void *)reboot_code_buffer;133do_kexec(efi_boot, cmdline_ptr, systable_ptr, start_addr, first_ind_entry);134135BUG();136}137138139#ifdef CONFIG_SMP140static void kexec_shutdown_secondary(void *regs)141{142int cpu = smp_processor_id();143144if (!cpu_online(cpu))145return;146147/* We won't be sent IPIs any more. */148set_cpu_online(cpu, false);149150local_irq_disable();151while (!atomic_read(&kexec_ready_to_reboot))152cpu_relax();153154kexec_reboot();155}156157static void crash_shutdown_secondary(void *passed_regs)158{159int cpu = smp_processor_id();160struct pt_regs *regs = passed_regs;161162/*163* If we are passed registers, use those. Otherwise get the164* regs from the last interrupt, which should be correct, as165* we are in an interrupt. But if the regs are not there,166* pull them from the top of the stack. They are probably167* wrong, but we need something to keep from crashing again.168*/169if (!regs)170regs = get_irq_regs();171if (!regs)172regs = task_pt_regs(current);173174if (!cpu_online(cpu))175return;176177/* We won't be sent IPIs any more. */178set_cpu_online(cpu, false);179180local_irq_disable();181if (!cpumask_test_cpu(cpu, &cpus_in_crash))182crash_save_cpu(regs, cpu);183cpumask_set_cpu(cpu, &cpus_in_crash);184185while (!atomic_read(&kexec_ready_to_reboot))186cpu_relax();187188kexec_reboot();189}190191void crash_smp_send_stop(void)192{193unsigned int ncpus;194unsigned long timeout;195static int cpus_stopped;196197/*198* This function can be called twice in panic path, but obviously199* we should execute this only once.200*/201if (cpus_stopped)202return;203204cpus_stopped = 1;205206/* Excluding the panic cpu */207ncpus = num_online_cpus() - 1;208209smp_call_function(crash_shutdown_secondary, NULL, 0);210smp_wmb();211212/*213* The crash CPU sends an IPI and wait for other CPUs to214* respond. Delay of at least 10 seconds.215*/216timeout = MSEC_PER_SEC * 10;217pr_emerg("Sending IPI to other cpus...\n");218while ((cpumask_weight(&cpus_in_crash) < ncpus) && timeout--) {219mdelay(1);220cpu_relax();221}222}223#endif /* defined(CONFIG_SMP) */224225void machine_shutdown(void)226{227#ifdef CONFIG_SMP228int cpu;229230/* All CPUs go to reboot_code_buffer */231for_each_possible_cpu(cpu)232if (!cpu_online(cpu))233cpu_device_up(get_cpu_device(cpu));234235smp_call_function(kexec_shutdown_secondary, NULL, 0);236#endif237}238239void machine_crash_shutdown(struct pt_regs *regs)240{241int crashing_cpu;242243local_irq_disable();244245crashing_cpu = smp_processor_id();246crash_save_cpu(regs, crashing_cpu);247248#ifdef CONFIG_SMP249crash_smp_send_stop();250#endif251cpumask_set_cpu(crashing_cpu, &cpus_in_crash);252253pr_info("Starting crashdump kernel...\n");254}255256void machine_kexec(struct kimage *image)257{258unsigned long entry, *ptr;259struct kimage_arch *internal = &image->arch;260261efi_boot = internal->efi_boot;262cmdline_ptr = internal->cmdline_ptr;263systable_ptr = internal->systable_ptr;264265start_addr = (unsigned long)phys_to_virt(image->start);266267first_ind_entry = (image->type == KEXEC_TYPE_DEFAULT) ?268(unsigned long)phys_to_virt(image->head & PAGE_MASK) : 0;269270/*271* The generic kexec code builds a page list with physical272* addresses. they are directly accessible through XKPRANGE273* hence the phys_to_virt() call.274*/275for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE);276ptr = (entry & IND_INDIRECTION) ?277phys_to_virt(entry & PAGE_MASK) : ptr + 1) {278if (*ptr & IND_SOURCE || *ptr & IND_INDIRECTION ||279*ptr & IND_DESTINATION)280*ptr = (unsigned long) phys_to_virt(*ptr);281}282283/* Mark offline before disabling local irq. */284set_cpu_online(smp_processor_id(), false);285286/* We do not want to be bothered. */287local_irq_disable();288289pr_notice("EFI boot flag 0x%lx\n", efi_boot);290pr_notice("Command line at 0x%lx\n", cmdline_ptr);291pr_notice("System table at 0x%lx\n", systable_ptr);292pr_notice("We will call new kernel at 0x%lx\n", start_addr);293pr_notice("Bye ...\n");294295/* Make reboot code buffer available to the boot CPU. */296flush_cache_all();297298#ifdef CONFIG_SMP299atomic_set(&kexec_ready_to_reboot, 1);300#endif301302kexec_reboot();303}304305306