blob: 43f0a3ebf3909d6c85daa059cdd7e1acbb2a50bc [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
louis.wang8252ca82021-02-24 13:25:53 +01002#include <linux/ftrace.h>
Russell Kinge8ce0eb2011-08-26 20:28:52 +01003#include <linux/init.h>
Lorenzo Pieralisi76045372013-05-16 10:34:30 +01004#include <linux/slab.h>
Ingo Molnar589ee622017-02-04 00:16:44 +01005#include <linux/mm_types.h>
Mike Rapoport65fddcf2020-06-08 21:32:42 -07006#include <linux/pgtable.h>
Russell Kinge8ce0eb2011-08-26 20:28:52 +01007
Russell King26602162018-05-10 13:00:43 +01008#include <asm/bugs.h>
Lorenzo Pieralisi76045372013-05-16 10:34:30 +01009#include <asm/cacheflush.h>
Will Deacone6eadc62011-11-15 11:11:19 +000010#include <asm/idmap.h>
Russell Kinge8ce0eb2011-08-26 20:28:52 +010011#include <asm/memory.h>
Lorenzo Pieralisi76045372013-05-16 10:34:30 +010012#include <asm/smp_plat.h>
Russell Kinge8ce0eb2011-08-26 20:28:52 +010013#include <asm/suspend.h>
14#include <asm/tlbflush.h>
15
Nicolas Pitre71a89862013-07-18 16:50:59 -040016extern int __cpu_suspend(unsigned long, int (*)(unsigned long), u32 cpuid);
Russell King62b2d072011-08-31 23:26:18 +010017extern void cpu_resume_mmu(void);
Russell Kinge8ce0eb2011-08-26 20:28:52 +010018
Will Deaconaa1aadc2012-02-23 13:51:38 +000019#ifdef CONFIG_MMU
Will Deaconaa1aadc2012-02-23 13:51:38 +000020int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
21{
22 struct mm_struct *mm = current->active_mm;
Nicolas Pitre71a89862013-07-18 16:50:59 -040023 u32 __mpidr = cpu_logical_map(smp_processor_id());
Will Deaconaa1aadc2012-02-23 13:51:38 +000024 int ret;
25
26 if (!idmap_pgd)
27 return -EINVAL;
28
29 /*
louis.wang8252ca82021-02-24 13:25:53 +010030 * Function graph tracer state gets incosistent when the kernel
31 * calls functions that never return (aka suspend finishers) hence
32 * disable graph tracing during their execution.
33 */
34 pause_graph_tracing();
35
36 /*
Will Deaconaa1aadc2012-02-23 13:51:38 +000037 * Provide a temporary page table with an identity mapping for
38 * the MMU-enable code, required for resuming. On successful
39 * resume (indicated by a zero return code), we need to switch
40 * back to the correct page tables.
41 */
Nicolas Pitre71a89862013-07-18 16:50:59 -040042 ret = __cpu_suspend(arg, fn, __mpidr);
louis.wang8252ca82021-02-24 13:25:53 +010043
44 unpause_graph_tracing();
45
Will Deaconaa1aadc2012-02-23 13:51:38 +000046 if (ret == 0) {
47 cpu_switch_mm(mm->pgd, mm);
48 local_flush_bp_all();
49 local_flush_tlb_all();
Russell King26602162018-05-10 13:00:43 +010050 check_other_bugs();
Will Deaconaa1aadc2012-02-23 13:51:38 +000051 }
52
53 return ret;
54}
55#else
56int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
57{
Nicolas Pitre71a89862013-07-18 16:50:59 -040058 u32 __mpidr = cpu_logical_map(smp_processor_id());
louis.wang8252ca82021-02-24 13:25:53 +010059 int ret;
60
61 pause_graph_tracing();
62 ret = __cpu_suspend(arg, fn, __mpidr);
63 unpause_graph_tracing();
64
65 return ret;
Will Deaconaa1aadc2012-02-23 13:51:38 +000066}
67#define idmap_pgd NULL
68#endif
69
Russell Kinge8ce0eb2011-08-26 20:28:52 +010070/*
Russell Kingabda1bd2011-09-01 11:52:33 +010071 * This is called by __cpu_suspend() to save the state, and do whatever
72 * flushing is required to ensure that when the CPU goes to sleep we have
73 * the necessary data available when the caches are not searched.
74 */
75void __cpu_suspend_save(u32 *ptr, u32 ptrsz, u32 sp, u32 *save_ptr)
76{
Lorenzo Pieralisidbee0c62012-09-07 11:06:57 +053077 u32 *ctx = ptr;
78
Russell Kingabda1bd2011-09-01 11:52:33 +010079 *save_ptr = virt_to_phys(ptr);
80
81 /* This must correspond to the LDM in cpu_resume() assembly */
Will Deacone6eadc62011-11-15 11:11:19 +000082 *ptr++ = virt_to_phys(idmap_pgd);
Russell Kingabda1bd2011-09-01 11:52:33 +010083 *ptr++ = sp;
84 *ptr++ = virt_to_phys(cpu_do_resume);
85
86 cpu_do_suspend(ptr);
87
Lorenzo Pieralisidbee0c62012-09-07 11:06:57 +053088 flush_cache_louis();
89
90 /*
91 * flush_cache_louis does not guarantee that
92 * save_ptr and ptr are cleaned to main memory,
93 * just up to the Level of Unification Inner Shareable.
94 * Since the context pointer and context itself
95 * are to be retrieved with the MMU off that
96 * data must be cleaned from all cache levels
97 * to main memory using "area" cache primitives.
98 */
99 __cpuc_flush_dcache_area(ctx, ptrsz);
100 __cpuc_flush_dcache_area(save_ptr, sizeof(*save_ptr));
101
Russell King8e6f83b2011-09-01 11:57:59 +0100102 outer_clean_range(*save_ptr, *save_ptr + ptrsz);
103 outer_clean_range(virt_to_phys(save_ptr),
104 virt_to_phys(save_ptr) + sizeof(*save_ptr));
Russell Kingabda1bd2011-09-01 11:52:33 +0100105}
Lorenzo Pieralisi76045372013-05-16 10:34:30 +0100106
107extern struct sleep_save_sp sleep_save_sp;
108
109static int cpu_suspend_alloc_sp(void)
110{
111 void *ctx_ptr;
112 /* ctx_ptr is an array of physical addresses */
113 ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(u32), GFP_KERNEL);
114
115 if (WARN_ON(!ctx_ptr))
116 return -ENOMEM;
117 sleep_save_sp.save_ptr_stash = ctx_ptr;
118 sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr);
119 sync_cache_w(&sleep_save_sp);
120 return 0;
121}
122early_initcall(cpu_suspend_alloc_sp);