^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) * Hibernation support specific for ARM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Derived from work on ARM hibernation support by:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Ubuntu project, hibernation support for mach-dove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Copyright (C) 2010 Nokia Corporation (Hiroshi Doyu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (C) 2010 Texas Instruments, Inc. (Teerth Reddy et al.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * https://lkml.org/lkml/2010/6/18/4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * https://lists.linux-foundation.org/pipermail/linux-pm/2010-June/027422.html
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * https://patchwork.kernel.org/patch/96442/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Copyright (C) 2006 Rafael J. Wysocki <rjw@sisk.pl>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/system_misc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/idmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <asm/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "reboot.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int pfn_is_nosave(unsigned long pfn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned long nosave_begin_pfn = virt_to_pfn(&__nosave_begin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) unsigned long nosave_end_pfn = virt_to_pfn(&__nosave_end - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return (pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) void notrace save_processor_state(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) WARN_ON(num_online_cpus() != 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) local_fiq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) void notrace restore_processor_state(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) local_fiq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Snapshot kernel memory and reset the system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * swsusp_save() is executed in the suspend finisher so that the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * context pointer and memory are part of the saved image, which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * required by the resume kernel image to restart execution from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * swsusp_arch_suspend().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * soft_restart is not technically needed, but is used to get success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * returned from cpu_suspend.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * When soft reboot completes, the hibernation snapshot is written out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static int notrace arch_save_image(unsigned long unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ret = swsusp_save();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) _soft_restart(virt_to_idmap(cpu_resume), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * Save the current CPU state before suspend / poweroff.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int notrace swsusp_arch_suspend(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return cpu_suspend(0, arch_save_image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^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) * Restore page contents for physical pages that were in use during loading
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * hibernation image. Switch to idmap_pgd so the physical page tables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * are overwritten with the same contents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static void notrace arch_restore_image(void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct pbe *pbe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) cpu_switch_mm(idmap_pgd, &init_mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) for (pbe = restore_pblist; pbe; pbe = pbe->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) copy_page(pbe->orig_address, pbe->address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) _soft_restart(virt_to_idmap(cpu_resume), false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static u64 resume_stack[PAGE_SIZE/2/sizeof(u64)] __nosavedata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * Resume from the hibernation image.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * Due to the kernel heap / data restore, stack contents change underneath
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * and that would make function calls impossible; switch to a temporary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * stack within the nosave region to avoid that problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int swsusp_arch_resume(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) call_with_stack(arch_restore_image, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) resume_stack + ARRAY_SIZE(resume_stack));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }