1de818bd4SLorenzo Pieralisi #include <linux/ftrace.h> 2fb4a9602SLorenzo Pieralisi #include <linux/percpu.h> 395322526SLorenzo Pieralisi #include <linux/slab.h> 4d0854412SJames Morse #include <asm/alternative.h> 595322526SLorenzo Pieralisi #include <asm/cacheflush.h> 6d0854412SJames Morse #include <asm/cpufeature.h> 7*0fbeb318SJames Morse #include <asm/daifflags.h> 895322526SLorenzo Pieralisi #include <asm/debug-monitors.h> 9d0854412SJames Morse #include <asm/exec.h> 1095322526SLorenzo Pieralisi #include <asm/pgtable.h> 1195322526SLorenzo Pieralisi #include <asm/memory.h> 12f43c2718SLorenzo Pieralisi #include <asm/mmu_context.h> 1395322526SLorenzo Pieralisi #include <asm/smp_plat.h> 1495322526SLorenzo Pieralisi #include <asm/suspend.h> 1595322526SLorenzo Pieralisi 1695322526SLorenzo Pieralisi /* 17cabe1c81SJames Morse * This is allocated by cpu_suspend_init(), and used to store a pointer to 18cabe1c81SJames Morse * the 'struct sleep_stack_data' the contains a particular CPUs state. 1995322526SLorenzo Pieralisi */ 20cabe1c81SJames Morse unsigned long *sleep_save_stash; 2195322526SLorenzo Pieralisi 2265c021bbSLorenzo Pieralisi /* 2365c021bbSLorenzo Pieralisi * This hook is provided so that cpu_suspend code can restore HW 2465c021bbSLorenzo Pieralisi * breakpoints as early as possible in the resume path, before reenabling 2565c021bbSLorenzo Pieralisi * debug exceptions. Code cannot be run from a CPU PM notifier since by the 2665c021bbSLorenzo Pieralisi * time the notifier runs debug exceptions might have been enabled already, 2765c021bbSLorenzo Pieralisi * with HW breakpoints registers content still in an unknown state. 2865c021bbSLorenzo Pieralisi */ 29d7a83d12SWill Deacon static int (*hw_breakpoint_restore)(unsigned int); 30d7a83d12SWill Deacon void __init cpu_suspend_set_dbg_restorer(int (*hw_bp_restore)(unsigned int)) 3165c021bbSLorenzo Pieralisi { 3265c021bbSLorenzo Pieralisi /* Prevent multiple restore hook initializations */ 3365c021bbSLorenzo Pieralisi if (WARN_ON(hw_breakpoint_restore)) 3465c021bbSLorenzo Pieralisi return; 3565c021bbSLorenzo Pieralisi hw_breakpoint_restore = hw_bp_restore; 3665c021bbSLorenzo Pieralisi } 3765c021bbSLorenzo Pieralisi 38adc9b2dfSJames Morse void notrace __cpu_suspend_exit(void) 39714f5992SLorenzo Pieralisi { 40d7a83d12SWill Deacon unsigned int cpu = smp_processor_id(); 41d7a83d12SWill Deacon 42f43c2718SLorenzo Pieralisi /* 439e8e865bSMark Rutland * We are resuming from reset with the idmap active in TTBR0_EL1. 449e8e865bSMark Rutland * We must uninstall the idmap and restore the expected MMU 459e8e865bSMark Rutland * state before we can possibly return to userspace. 46f43c2718SLorenzo Pieralisi */ 479e8e865bSMark Rutland cpu_uninstall_idmap(); 48fb4a9602SLorenzo Pieralisi 49fb4a9602SLorenzo Pieralisi /* 50d0854412SJames Morse * PSTATE was not saved over suspend/resume, re-enable any detected 51d0854412SJames Morse * features that might not have been set correctly. 52d0854412SJames Morse */ 53d0854412SJames Morse asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN, 54d0854412SJames Morse CONFIG_ARM64_PAN)); 55d0854412SJames Morse uao_thread_switch(current); 56d0854412SJames Morse 57d0854412SJames Morse /* 5865c021bbSLorenzo Pieralisi * Restore HW breakpoint registers to sane values 5965c021bbSLorenzo Pieralisi * before debug exceptions are possibly reenabled 60*0fbeb318SJames Morse * by cpu_suspend()s local_daif_restore() call. 6165c021bbSLorenzo Pieralisi */ 6265c021bbSLorenzo Pieralisi if (hw_breakpoint_restore) 63d7a83d12SWill Deacon hw_breakpoint_restore(cpu); 6495322526SLorenzo Pieralisi } 6595322526SLorenzo Pieralisi 66adc9b2dfSJames Morse /* 67adc9b2dfSJames Morse * cpu_suspend 68adc9b2dfSJames Morse * 69adc9b2dfSJames Morse * arg: argument to pass to the finisher function 70adc9b2dfSJames Morse * fn: finisher function pointer 71adc9b2dfSJames Morse * 72adc9b2dfSJames Morse */ 73adc9b2dfSJames Morse int cpu_suspend(unsigned long arg, int (*fn)(unsigned long)) 74adc9b2dfSJames Morse { 75adc9b2dfSJames Morse int ret = 0; 76adc9b2dfSJames Morse unsigned long flags; 77adc9b2dfSJames Morse struct sleep_stack_data state; 78adc9b2dfSJames Morse 79adc9b2dfSJames Morse /* 80adc9b2dfSJames Morse * From this point debug exceptions are disabled to prevent 81adc9b2dfSJames Morse * updates to mdscr register (saved and restored along with 82adc9b2dfSJames Morse * general purpose registers) from kernel debuggers. 83adc9b2dfSJames Morse */ 84*0fbeb318SJames Morse flags = local_daif_save(); 85adc9b2dfSJames Morse 86adc9b2dfSJames Morse /* 87adc9b2dfSJames Morse * Function graph tracer state gets incosistent when the kernel 88adc9b2dfSJames Morse * calls functions that never return (aka suspend finishers) hence 89adc9b2dfSJames Morse * disable graph tracing during their execution. 90adc9b2dfSJames Morse */ 91adc9b2dfSJames Morse pause_graph_tracing(); 92adc9b2dfSJames Morse 93adc9b2dfSJames Morse if (__cpu_suspend_enter(&state)) { 94adc9b2dfSJames Morse /* Call the suspend finisher */ 95adc9b2dfSJames Morse ret = fn(arg); 96adc9b2dfSJames Morse 97adc9b2dfSJames Morse /* 98adc9b2dfSJames Morse * Never gets here, unless the suspend finisher fails. 99adc9b2dfSJames Morse * Successful cpu_suspend() should return from cpu_resume(), 100adc9b2dfSJames Morse * returning through this code path is considered an error 101adc9b2dfSJames Morse * If the return value is set to 0 force ret = -EOPNOTSUPP 102adc9b2dfSJames Morse * to make sure a proper error condition is propagated 103adc9b2dfSJames Morse */ 104adc9b2dfSJames Morse if (!ret) 105adc9b2dfSJames Morse ret = -EOPNOTSUPP; 106adc9b2dfSJames Morse } else { 107adc9b2dfSJames Morse __cpu_suspend_exit(); 108adc9b2dfSJames Morse } 109adc9b2dfSJames Morse 110de818bd4SLorenzo Pieralisi unpause_graph_tracing(); 111de818bd4SLorenzo Pieralisi 11295322526SLorenzo Pieralisi /* 11395322526SLorenzo Pieralisi * Restore pstate flags. OS lock and mdscr have been already 11495322526SLorenzo Pieralisi * restored, so from this point onwards, debugging is fully 11595322526SLorenzo Pieralisi * renabled if it was enabled when core started shutdown. 11695322526SLorenzo Pieralisi */ 117*0fbeb318SJames Morse local_daif_restore(flags); 11895322526SLorenzo Pieralisi 11995322526SLorenzo Pieralisi return ret; 12095322526SLorenzo Pieralisi } 12195322526SLorenzo Pieralisi 12218ab7db6SLorenzo Pieralisi static int __init cpu_suspend_init(void) 12395322526SLorenzo Pieralisi { 12495322526SLorenzo Pieralisi /* ctx_ptr is an array of physical addresses */ 125cabe1c81SJames Morse sleep_save_stash = kcalloc(mpidr_hash_size(), sizeof(*sleep_save_stash), 126cabe1c81SJames Morse GFP_KERNEL); 12795322526SLorenzo Pieralisi 128cabe1c81SJames Morse if (WARN_ON(!sleep_save_stash)) 12995322526SLorenzo Pieralisi return -ENOMEM; 13095322526SLorenzo Pieralisi 13195322526SLorenzo Pieralisi return 0; 13295322526SLorenzo Pieralisi } 13395322526SLorenzo Pieralisi early_initcall(cpu_suspend_init); 134