^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * kexec for arm64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) Linaro.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) Huawei Futurewei Technologies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kexec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/page-flags.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/cpu_ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/daifflags.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/mmu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/mmu_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "cpu-reset.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* Global variables for the arm64_relocate_new_kernel routine. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) extern const unsigned char arm64_relocate_new_kernel[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) extern const unsigned long arm64_relocate_new_kernel_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * kexec_image_info - For debugging output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define kexec_image_info(_i) _kexec_image_info(__func__, __LINE__, _i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static void _kexec_image_info(const char *func, int line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) const struct kimage *kimage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) pr_debug("%s:%d:\n", func, line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) pr_debug(" kexec kimage info:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) pr_debug(" type: %d\n", kimage->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) pr_debug(" start: %lx\n", kimage->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) pr_debug(" head: %lx\n", kimage->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) pr_debug(" nr_segments: %lu\n", kimage->nr_segments);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) for (i = 0; i < kimage->nr_segments; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) kimage->segment[i].mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) kimage->segment[i].mem + kimage->segment[i].memsz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) kimage->segment[i].memsz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) kimage->segment[i].memsz / PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) void machine_kexec_cleanup(struct kimage *kimage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* Empty routine needed to avoid build errors. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * machine_kexec_prepare - Prepare for a kexec reboot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * Called from the core kexec code when a kernel image is loaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * Forbid loading a kexec kernel if we have no way of hotplugging cpus or cpus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * are stuck in the kernel. This avoids a panic once we hit machine_kexec().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int machine_kexec_prepare(struct kimage *kimage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) kexec_image_info(kimage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (kimage->type != KEXEC_TYPE_CRASH && cpus_are_stuck_in_kernel()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) pr_err("Can't kexec: CPUs are stuck in the kernel.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * kexec_list_flush - Helper to flush the kimage list and source pages to PoC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) static void kexec_list_flush(struct kimage *kimage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) kimage_entry_t *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) for (entry = &kimage->head; ; entry++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) unsigned int flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) void *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* flush the list entries. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) __flush_dcache_area(entry, sizeof(kimage_entry_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) flag = *entry & IND_FLAGS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (flag == IND_DONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) addr = phys_to_virt(*entry & PAGE_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) switch (flag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) case IND_INDIRECTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* Set entry point just before the new list page. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) entry = (kimage_entry_t *)addr - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) case IND_SOURCE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* flush the source pages. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) __flush_dcache_area(addr, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) case IND_DESTINATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * kexec_segment_flush - Helper to flush the kimage segments to PoC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static void kexec_segment_flush(const struct kimage *kimage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) pr_debug("%s:\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) for (i = 0; i < kimage->nr_segments; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) pr_debug(" segment[%lu]: %016lx - %016lx, 0x%lx bytes, %lu pages\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) kimage->segment[i].mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) kimage->segment[i].mem + kimage->segment[i].memsz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) kimage->segment[i].memsz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) kimage->segment[i].memsz / PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) __flush_dcache_area(phys_to_virt(kimage->segment[i].mem),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) kimage->segment[i].memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * machine_kexec - Do the kexec reboot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * Called from the core kexec code for a sys_reboot with LINUX_REBOOT_CMD_KEXEC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) void machine_kexec(struct kimage *kimage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) phys_addr_t reboot_code_buffer_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) void *reboot_code_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) bool in_kexec_crash = (kimage == kexec_crash_image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) bool stuck_cpus = cpus_are_stuck_in_kernel();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * New cpus may have become stuck_in_kernel after we loaded the image.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) BUG_ON(!in_kexec_crash && (stuck_cpus || (num_online_cpus() > 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) WARN(in_kexec_crash && (stuck_cpus || smp_crash_stop_failed()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) "Some CPUs may be stale, kdump will be unreliable.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) reboot_code_buffer_phys = page_to_phys(kimage->control_code_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) reboot_code_buffer = phys_to_virt(reboot_code_buffer_phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) kexec_image_info(kimage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * Copy arm64_relocate_new_kernel to the reboot_code_buffer for use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * after the kernel is shut down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) memcpy(reboot_code_buffer, arm64_relocate_new_kernel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) arm64_relocate_new_kernel_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /* Flush the reboot_code_buffer in preparation for its execution. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) __flush_dcache_area(reboot_code_buffer, arm64_relocate_new_kernel_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * Although we've killed off the secondary CPUs, we don't update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * the online mask if we're handling a crash kernel and consequently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * need to avoid flush_icache_range(), which will attempt to IPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * the offline CPUs. Therefore, we must use the __* variant here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) __flush_icache_range((uintptr_t)reboot_code_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) (uintptr_t)reboot_code_buffer +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) arm64_relocate_new_kernel_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* Flush the kimage list and its buffers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) kexec_list_flush(kimage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /* Flush the new image if already in place. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if ((kimage != kexec_crash_image) && (kimage->head & IND_DONE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) kexec_segment_flush(kimage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) pr_info("Bye!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) local_daif_mask();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * cpu_soft_restart will shutdown the MMU, disable data caches, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * transfer control to the reboot_code_buffer which contains a copy of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * the arm64_relocate_new_kernel routine. arm64_relocate_new_kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * uses physical addressing to relocate the new image to its final
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * position and transfers control to the image entry point when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * relocation is complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * In kexec case, kimage->start points to purgatory assuming that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * kernel entry and dtb address are embedded in purgatory by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * userspace (kexec-tools).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * In kexec_file case, the kernel starts directly without purgatory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) cpu_soft_restart(reboot_code_buffer_phys, kimage->head, kimage->start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) #ifdef CONFIG_KEXEC_FILE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) kimage->arch.dtb_mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) BUG(); /* Should never get here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static void machine_kexec_mask_interrupts(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct irq_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) for_each_irq_desc(i, desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct irq_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) chip = irq_desc_get_chip(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * First try to remove the active state. If this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * fails, try to EOI the interrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) ret = irq_set_irqchip_state(i, IRQCHIP_STATE_ACTIVE, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (ret && irqd_irq_inprogress(&desc->irq_data) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) chip->irq_eoi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) chip->irq_eoi(&desc->irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (chip->irq_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) chip->irq_mask(&desc->irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (chip->irq_disable && !irqd_irq_disabled(&desc->irq_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) chip->irq_disable(&desc->irq_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * machine_crash_shutdown - shutdown non-crashing cpus and save registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) void machine_crash_shutdown(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) /* shutdown non-crashing cpus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) crash_smp_send_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /* for crashing cpu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) crash_save_cpu(regs, smp_processor_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) machine_kexec_mask_interrupts();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) pr_info("Starting crashdump kernel...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) void arch_kexec_protect_crashkres(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) kexec_segment_flush(kexec_crash_image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) for (i = 0; i < kexec_crash_image->nr_segments; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) set_memory_valid(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) __phys_to_virt(kexec_crash_image->segment[i].mem),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) void arch_kexec_unprotect_crashkres(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) for (i = 0; i < kexec_crash_image->nr_segments; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) set_memory_valid(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) __phys_to_virt(kexec_crash_image->segment[i].mem),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) kexec_crash_image->segment[i].memsz >> PAGE_SHIFT, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) #ifdef CONFIG_HIBERNATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * To preserve the crash dump kernel image, the relevant memory segments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * should be mapped again around the hibernation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) void crash_prepare_suspend(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (kexec_crash_image)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) arch_kexec_unprotect_crashkres();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) void crash_post_resume(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (kexec_crash_image)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) arch_kexec_protect_crashkres();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * crash_is_nosave
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * Return true only if a page is part of reserved memory for crash dump kernel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * but does not hold any data of loaded kernel image.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * Note that all the pages in crash dump kernel memory have been initially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * marked as Reserved as memory was allocated via memblock_reserve().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * In hibernation, the pages which are Reserved and yet "nosave" are excluded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * from the hibernation iamge. crash_is_nosave() does thich check for crash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * dump kernel and will reduce the total size of hibernation image.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) bool crash_is_nosave(unsigned long pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) phys_addr_t addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (!crashk_res.end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /* in reserved memory? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) addr = __pfn_to_phys(pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if ((addr < crashk_res.start) || (crashk_res.end < addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (!kexec_crash_image)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /* not part of loaded kernel image? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) for (i = 0; i < kexec_crash_image->nr_segments; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (addr >= kexec_crash_image->segment[i].mem &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) addr < (kexec_crash_image->segment[i].mem +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) kexec_crash_image->segment[i].memsz))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) void crash_free_reserved_phys_range(unsigned long begin, unsigned long end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) for (addr = begin; addr < end; addr += PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) page = phys_to_page(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) free_reserved_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) #endif /* CONFIG_HIBERNATION */