1*fb4a9602SLorenzo Pieralisi #include <linux/percpu.h> 295322526SLorenzo Pieralisi #include <linux/slab.h> 395322526SLorenzo Pieralisi #include <asm/cacheflush.h> 495322526SLorenzo Pieralisi #include <asm/cpu_ops.h> 595322526SLorenzo Pieralisi #include <asm/debug-monitors.h> 695322526SLorenzo Pieralisi #include <asm/pgtable.h> 795322526SLorenzo Pieralisi #include <asm/memory.h> 895322526SLorenzo Pieralisi #include <asm/smp_plat.h> 995322526SLorenzo Pieralisi #include <asm/suspend.h> 1095322526SLorenzo Pieralisi #include <asm/tlbflush.h> 1195322526SLorenzo Pieralisi 1295322526SLorenzo Pieralisi extern int __cpu_suspend(unsigned long); 1395322526SLorenzo Pieralisi /* 1495322526SLorenzo Pieralisi * This is called by __cpu_suspend() to save the state, and do whatever 1595322526SLorenzo Pieralisi * flushing is required to ensure that when the CPU goes to sleep we have 1695322526SLorenzo Pieralisi * the necessary data available when the caches are not searched. 1795322526SLorenzo Pieralisi * 1895322526SLorenzo Pieralisi * @arg: Argument to pass to suspend operations 1995322526SLorenzo Pieralisi * @ptr: CPU context virtual address 2095322526SLorenzo Pieralisi * @save_ptr: address of the location where the context physical address 2195322526SLorenzo Pieralisi * must be saved 2295322526SLorenzo Pieralisi */ 2395322526SLorenzo Pieralisi int __cpu_suspend_finisher(unsigned long arg, struct cpu_suspend_ctx *ptr, 2495322526SLorenzo Pieralisi phys_addr_t *save_ptr) 2595322526SLorenzo Pieralisi { 2695322526SLorenzo Pieralisi int cpu = smp_processor_id(); 2795322526SLorenzo Pieralisi 2895322526SLorenzo Pieralisi *save_ptr = virt_to_phys(ptr); 2995322526SLorenzo Pieralisi 3095322526SLorenzo Pieralisi cpu_do_suspend(ptr); 3195322526SLorenzo Pieralisi /* 3295322526SLorenzo Pieralisi * Only flush the context that must be retrieved with the MMU 3395322526SLorenzo Pieralisi * off. VA primitives ensure the flush is applied to all 3495322526SLorenzo Pieralisi * cache levels so context is pushed to DRAM. 3595322526SLorenzo Pieralisi */ 3695322526SLorenzo Pieralisi __flush_dcache_area(ptr, sizeof(*ptr)); 3795322526SLorenzo Pieralisi __flush_dcache_area(save_ptr, sizeof(*save_ptr)); 3895322526SLorenzo Pieralisi 3995322526SLorenzo Pieralisi return cpu_ops[cpu]->cpu_suspend(arg); 4095322526SLorenzo Pieralisi } 4195322526SLorenzo Pieralisi 4265c021bbSLorenzo Pieralisi /* 4365c021bbSLorenzo Pieralisi * This hook is provided so that cpu_suspend code can restore HW 4465c021bbSLorenzo Pieralisi * breakpoints as early as possible in the resume path, before reenabling 4565c021bbSLorenzo Pieralisi * debug exceptions. Code cannot be run from a CPU PM notifier since by the 4665c021bbSLorenzo Pieralisi * time the notifier runs debug exceptions might have been enabled already, 4765c021bbSLorenzo Pieralisi * with HW breakpoints registers content still in an unknown state. 4865c021bbSLorenzo Pieralisi */ 4965c021bbSLorenzo Pieralisi void (*hw_breakpoint_restore)(void *); 5065c021bbSLorenzo Pieralisi void __init cpu_suspend_set_dbg_restorer(void (*hw_bp_restore)(void *)) 5165c021bbSLorenzo Pieralisi { 5265c021bbSLorenzo Pieralisi /* Prevent multiple restore hook initializations */ 5365c021bbSLorenzo Pieralisi if (WARN_ON(hw_breakpoint_restore)) 5465c021bbSLorenzo Pieralisi return; 5565c021bbSLorenzo Pieralisi hw_breakpoint_restore = hw_bp_restore; 5665c021bbSLorenzo Pieralisi } 5765c021bbSLorenzo Pieralisi 5895322526SLorenzo Pieralisi /** 5995322526SLorenzo Pieralisi * cpu_suspend 6095322526SLorenzo Pieralisi * 6195322526SLorenzo Pieralisi * @arg: argument to pass to the finisher function 6295322526SLorenzo Pieralisi */ 6395322526SLorenzo Pieralisi int cpu_suspend(unsigned long arg) 6495322526SLorenzo Pieralisi { 6595322526SLorenzo Pieralisi struct mm_struct *mm = current->active_mm; 6695322526SLorenzo Pieralisi int ret, cpu = smp_processor_id(); 6795322526SLorenzo Pieralisi unsigned long flags; 6895322526SLorenzo Pieralisi 6995322526SLorenzo Pieralisi /* 7095322526SLorenzo Pieralisi * If cpu_ops have not been registered or suspend 7195322526SLorenzo Pieralisi * has not been initialized, cpu_suspend call fails early. 7295322526SLorenzo Pieralisi */ 7395322526SLorenzo Pieralisi if (!cpu_ops[cpu] || !cpu_ops[cpu]->cpu_suspend) 7495322526SLorenzo Pieralisi return -EOPNOTSUPP; 7595322526SLorenzo Pieralisi 7695322526SLorenzo Pieralisi /* 7795322526SLorenzo Pieralisi * From this point debug exceptions are disabled to prevent 7895322526SLorenzo Pieralisi * updates to mdscr register (saved and restored along with 7995322526SLorenzo Pieralisi * general purpose registers) from kernel debuggers. 8095322526SLorenzo Pieralisi */ 8195322526SLorenzo Pieralisi local_dbg_save(flags); 8295322526SLorenzo Pieralisi 8395322526SLorenzo Pieralisi /* 8495322526SLorenzo Pieralisi * mm context saved on the stack, it will be restored when 8595322526SLorenzo Pieralisi * the cpu comes out of reset through the identity mapped 8695322526SLorenzo Pieralisi * page tables, so that the thread address space is properly 8795322526SLorenzo Pieralisi * set-up on function return. 8895322526SLorenzo Pieralisi */ 8995322526SLorenzo Pieralisi ret = __cpu_suspend(arg); 9095322526SLorenzo Pieralisi if (ret == 0) { 9195322526SLorenzo Pieralisi cpu_switch_mm(mm->pgd, mm); 9295322526SLorenzo Pieralisi flush_tlb_all(); 93*fb4a9602SLorenzo Pieralisi 94*fb4a9602SLorenzo Pieralisi /* 95*fb4a9602SLorenzo Pieralisi * Restore per-cpu offset before any kernel 96*fb4a9602SLorenzo Pieralisi * subsystem relying on it has a chance to run. 97*fb4a9602SLorenzo Pieralisi */ 98*fb4a9602SLorenzo Pieralisi set_my_cpu_offset(per_cpu_offset(cpu)); 99*fb4a9602SLorenzo Pieralisi 10065c021bbSLorenzo Pieralisi /* 10165c021bbSLorenzo Pieralisi * Restore HW breakpoint registers to sane values 10265c021bbSLorenzo Pieralisi * before debug exceptions are possibly reenabled 10365c021bbSLorenzo Pieralisi * through local_dbg_restore. 10465c021bbSLorenzo Pieralisi */ 10565c021bbSLorenzo Pieralisi if (hw_breakpoint_restore) 10665c021bbSLorenzo Pieralisi hw_breakpoint_restore(NULL); 10795322526SLorenzo Pieralisi } 10895322526SLorenzo Pieralisi 10995322526SLorenzo Pieralisi /* 11095322526SLorenzo Pieralisi * Restore pstate flags. OS lock and mdscr have been already 11195322526SLorenzo Pieralisi * restored, so from this point onwards, debugging is fully 11295322526SLorenzo Pieralisi * renabled if it was enabled when core started shutdown. 11395322526SLorenzo Pieralisi */ 11495322526SLorenzo Pieralisi local_dbg_restore(flags); 11595322526SLorenzo Pieralisi 11695322526SLorenzo Pieralisi return ret; 11795322526SLorenzo Pieralisi } 11895322526SLorenzo Pieralisi 11995322526SLorenzo Pieralisi extern struct sleep_save_sp sleep_save_sp; 12095322526SLorenzo Pieralisi extern phys_addr_t sleep_idmap_phys; 12195322526SLorenzo Pieralisi 12295322526SLorenzo Pieralisi static int cpu_suspend_init(void) 12395322526SLorenzo Pieralisi { 12495322526SLorenzo Pieralisi void *ctx_ptr; 12595322526SLorenzo Pieralisi 12695322526SLorenzo Pieralisi /* ctx_ptr is an array of physical addresses */ 12795322526SLorenzo Pieralisi ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(phys_addr_t), GFP_KERNEL); 12895322526SLorenzo Pieralisi 12995322526SLorenzo Pieralisi if (WARN_ON(!ctx_ptr)) 13095322526SLorenzo Pieralisi return -ENOMEM; 13195322526SLorenzo Pieralisi 13295322526SLorenzo Pieralisi sleep_save_sp.save_ptr_stash = ctx_ptr; 13395322526SLorenzo Pieralisi sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr); 13495322526SLorenzo Pieralisi sleep_idmap_phys = virt_to_phys(idmap_pg_dir); 13595322526SLorenzo Pieralisi __flush_dcache_area(&sleep_save_sp, sizeof(struct sleep_save_sp)); 13695322526SLorenzo Pieralisi __flush_dcache_area(&sleep_idmap_phys, sizeof(sleep_idmap_phys)); 13795322526SLorenzo Pieralisi 13895322526SLorenzo Pieralisi return 0; 13995322526SLorenzo Pieralisi } 14095322526SLorenzo Pieralisi early_initcall(cpu_suspend_init); 141