1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ftrace.h>
3 #include <linux/init.h>
4 #include <linux/slab.h>
5 #include <linux/mm_types.h>
6 #include <linux/pgtable.h>
9 #include <asm/cacheflush.h>
10 #include <asm/idmap.h>
12 #include <asm/smp_plat.h>
13 #include <asm/suspend.h>
14 #include <asm/tlbflush.h>
15 #include <asm/uaccess.h>
17 extern int __cpu_suspend(unsigned long, int (*)(unsigned long), u32 cpuid);
18 extern void cpu_resume_mmu(void);
21 int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
23 struct mm_struct *mm = current->active_mm;
24 u32 __mpidr = cpu_logical_map(smp_processor_id());
31 * Needed for the MMU disabling/enabing code to be able to run from
34 if (IS_ENABLED(CONFIG_CPU_TTBR0_PAN))
35 uaccess_save_and_enable();
38 * Function graph tracer state gets incosistent when the kernel
39 * calls functions that never return (aka suspend finishers) hence
40 * disable graph tracing during their execution.
42 pause_graph_tracing();
45 * Provide a temporary page table with an identity mapping for
46 * the MMU-enable code, required for resuming. On successful
47 * resume (indicated by a zero return code), we need to switch
48 * back to the correct page tables.
50 ret = __cpu_suspend(arg, fn, __mpidr);
52 unpause_graph_tracing();
55 cpu_switch_mm(mm->pgd, mm);
57 local_flush_tlb_all();
64 int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
66 u32 __mpidr = cpu_logical_map(smp_processor_id());
69 pause_graph_tracing();
70 ret = __cpu_suspend(arg, fn, __mpidr);
71 unpause_graph_tracing();
75 #define idmap_pgd NULL
79 * This is called by __cpu_suspend() to save the state, and do whatever
80 * flushing is required to ensure that when the CPU goes to sleep we have
81 * the necessary data available when the caches are not searched.
83 void __cpu_suspend_save(u32 *ptr, u32 ptrsz, u32 sp, u32 *save_ptr)
87 *save_ptr = virt_to_phys(ptr);
89 /* This must correspond to the LDM in cpu_resume() assembly */
90 *ptr++ = virt_to_phys(idmap_pgd);
92 *ptr++ = virt_to_phys(cpu_do_resume);
99 * flush_cache_louis does not guarantee that
100 * save_ptr and ptr are cleaned to main memory,
101 * just up to the Level of Unification Inner Shareable.
102 * Since the context pointer and context itself
103 * are to be retrieved with the MMU off that
104 * data must be cleaned from all cache levels
105 * to main memory using "area" cache primitives.
107 __cpuc_flush_dcache_area(ctx, ptrsz);
108 __cpuc_flush_dcache_area(save_ptr, sizeof(*save_ptr));
110 outer_clean_range(*save_ptr, *save_ptr + ptrsz);
111 outer_clean_range(virt_to_phys(save_ptr),
112 virt_to_phys(save_ptr) + sizeof(*save_ptr));
115 extern struct sleep_save_sp sleep_save_sp;
117 static int cpu_suspend_alloc_sp(void)
120 /* ctx_ptr is an array of physical addresses */
121 ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(u32), GFP_KERNEL);
123 if (WARN_ON(!ctx_ptr))
125 sleep_save_sp.save_ptr_stash = ctx_ptr;
126 sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr);
127 sync_cache_w(&sleep_save_sp);
130 early_initcall(cpu_suspend_alloc_sp);