Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/ftrace.h>
0003 #include <linux/init.h>
0004 #include <linux/slab.h>
0005 #include <linux/mm_types.h>
0006 #include <linux/pgtable.h>
0007 
0008 #include <asm/bugs.h>
0009 #include <asm/cacheflush.h>
0010 #include <asm/idmap.h>
0011 #include <asm/memory.h>
0012 #include <asm/smp_plat.h>
0013 #include <asm/suspend.h>
0014 #include <asm/tlbflush.h>
0015 
0016 extern int __cpu_suspend(unsigned long, int (*)(unsigned long), u32 cpuid);
0017 extern void cpu_resume_mmu(void);
0018 
0019 #ifdef CONFIG_MMU
0020 int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
0021 {
0022     struct mm_struct *mm = current->active_mm;
0023     u32 __mpidr = cpu_logical_map(smp_processor_id());
0024     int ret;
0025 
0026     if (!idmap_pgd)
0027         return -EINVAL;
0028 
0029     /*
0030      * Function graph tracer state gets incosistent when the kernel
0031      * calls functions that never return (aka suspend finishers) hence
0032      * disable graph tracing during their execution.
0033      */
0034     pause_graph_tracing();
0035 
0036     /*
0037      * Provide a temporary page table with an identity mapping for
0038      * the MMU-enable code, required for resuming.  On successful
0039      * resume (indicated by a zero return code), we need to switch
0040      * back to the correct page tables.
0041      */
0042     ret = __cpu_suspend(arg, fn, __mpidr);
0043 
0044     unpause_graph_tracing();
0045 
0046     if (ret == 0) {
0047         cpu_switch_mm(mm->pgd, mm);
0048         local_flush_bp_all();
0049         local_flush_tlb_all();
0050         check_other_bugs();
0051     }
0052 
0053     return ret;
0054 }
0055 #else
0056 int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
0057 {
0058     u32 __mpidr = cpu_logical_map(smp_processor_id());
0059     int ret;
0060 
0061     pause_graph_tracing();
0062     ret = __cpu_suspend(arg, fn, __mpidr);
0063     unpause_graph_tracing();
0064 
0065     return ret;
0066 }
0067 #define idmap_pgd   NULL
0068 #endif
0069 
0070 /*
0071  * This is called by __cpu_suspend() to save the state, and do whatever
0072  * flushing is required to ensure that when the CPU goes to sleep we have
0073  * the necessary data available when the caches are not searched.
0074  */
0075 void __cpu_suspend_save(u32 *ptr, u32 ptrsz, u32 sp, u32 *save_ptr)
0076 {
0077     u32 *ctx = ptr;
0078 
0079     *save_ptr = virt_to_phys(ptr);
0080 
0081     /* This must correspond to the LDM in cpu_resume() assembly */
0082     *ptr++ = virt_to_phys(idmap_pgd);
0083     *ptr++ = sp;
0084     *ptr++ = virt_to_phys(cpu_do_resume);
0085 
0086     cpu_do_suspend(ptr);
0087 
0088     flush_cache_louis();
0089 
0090     /*
0091      * flush_cache_louis does not guarantee that
0092      * save_ptr and ptr are cleaned to main memory,
0093      * just up to the Level of Unification Inner Shareable.
0094      * Since the context pointer and context itself
0095      * are to be retrieved with the MMU off that
0096      * data must be cleaned from all cache levels
0097      * to main memory using "area" cache primitives.
0098     */
0099     __cpuc_flush_dcache_area(ctx, ptrsz);
0100     __cpuc_flush_dcache_area(save_ptr, sizeof(*save_ptr));
0101 
0102     outer_clean_range(*save_ptr, *save_ptr + ptrsz);
0103     outer_clean_range(virt_to_phys(save_ptr),
0104               virt_to_phys(save_ptr) + sizeof(*save_ptr));
0105 }
0106 
0107 extern struct sleep_save_sp sleep_save_sp;
0108 
0109 static int cpu_suspend_alloc_sp(void)
0110 {
0111     void *ctx_ptr;
0112     /* ctx_ptr is an array of physical addresses */
0113     ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(u32), GFP_KERNEL);
0114 
0115     if (WARN_ON(!ctx_ptr))
0116         return -ENOMEM;
0117     sleep_save_sp.save_ptr_stash = ctx_ptr;
0118     sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr);
0119     sync_cache_w(&sleep_save_sp);
0120     return 0;
0121 }
0122 early_initcall(cpu_suspend_alloc_sp);