// SPDX-License-Identifier: GPL-2.0-only1/*2* Hibernation support specific for ARM3*4* Derived from work on ARM hibernation support by:5*6* Ubuntu project, hibernation support for mach-dove7* Copyright (C) 2010 Nokia Corporation (Hiroshi Doyu)8* Copyright (C) 2010 Texas Instruments, Inc. (Teerth Reddy et al.)9* https://lkml.org/lkml/2010/6/18/410* https://lists.linux-foundation.org/pipermail/linux-pm/2010-June/027422.html11* https://patchwork.kernel.org/patch/96442/12*13* Copyright (C) 2006 Rafael J. Wysocki <[email protected]>14*/1516#include <linux/mm.h>17#include <linux/suspend.h>18#include <asm/system_misc.h>19#include <asm/idmap.h>20#include <asm/suspend.h>21#include <asm/page.h>22#include <asm/sections.h>23#include "reboot.h"2425int pfn_is_nosave(unsigned long pfn)26{27unsigned long nosave_begin_pfn = virt_to_pfn(&__nosave_begin);28unsigned long nosave_end_pfn = virt_to_pfn(&__nosave_end - 1);2930return (pfn >= nosave_begin_pfn) && (pfn <= nosave_end_pfn);31}3233void notrace save_processor_state(void)34{35WARN_ON(num_online_cpus() != 1);36local_fiq_disable();37}3839void notrace restore_processor_state(void)40{41local_fiq_enable();42}4344/*45* Snapshot kernel memory and reset the system.46*47* swsusp_save() is executed in the suspend finisher so that the CPU48* context pointer and memory are part of the saved image, which is49* required by the resume kernel image to restart execution from50* swsusp_arch_suspend().51*52* soft_restart is not technically needed, but is used to get success53* returned from cpu_suspend.54*55* When soft reboot completes, the hibernation snapshot is written out.56*/57static int notrace arch_save_image(unsigned long unused)58{59int ret;6061ret = swsusp_save();62if (ret == 0)63_soft_restart(virt_to_idmap(cpu_resume), false);64return ret;65}6667/*68* Save the current CPU state before suspend / poweroff.69*/70int notrace swsusp_arch_suspend(void)71{72return cpu_suspend(0, arch_save_image);73}7475/*76* Restore page contents for physical pages that were in use during loading77* hibernation image. Switch to idmap_pgd so the physical page tables78* are overwritten with the same contents.79*/80static void notrace arch_restore_image(void *unused)81{82struct pbe *pbe;8384cpu_switch_mm(idmap_pgd, &init_mm);85for (pbe = restore_pblist; pbe; pbe = pbe->next)86copy_page(pbe->orig_address, pbe->address);8788_soft_restart(virt_to_idmap(cpu_resume), false);89}9091static u64 resume_stack[PAGE_SIZE/2/sizeof(u64)] __nosavedata;9293/*94* Resume from the hibernation image.95* Due to the kernel heap / data restore, stack contents change underneath96* and that would make function calls impossible; switch to a temporary97* stack within the nosave region to avoid that problem.98*/99int swsusp_arch_resume(void)100{101call_with_stack(arch_restore_image, 0,102resume_stack + ARRAY_SIZE(resume_stack));103return 0;104}105106107