blob: ef794c799cb660006f2ad19bd93075444bf3ed3b [file] [log] [blame]
Russell Kinge8ce0eb2011-08-26 20:28:52 +01001#include <linux/init.h>
Lorenzo Pieralisi76045372013-05-16 10:34:30 +01002#include <linux/slab.h>
Ingo Molnar589ee622017-02-04 00:16:44 +01003#include <linux/mm_types.h>
Russell Kinge8ce0eb2011-08-26 20:28:52 +01004
Lorenzo Pieralisi76045372013-05-16 10:34:30 +01005#include <asm/cacheflush.h>
Will Deacone6eadc62011-11-15 11:11:19 +00006#include <asm/idmap.h>
Russell Kinge8ce0eb2011-08-26 20:28:52 +01007#include <asm/pgalloc.h>
8#include <asm/pgtable.h>
9#include <asm/memory.h>
Lorenzo Pieralisi76045372013-05-16 10:34:30 +010010#include <asm/smp_plat.h>
Russell Kinge8ce0eb2011-08-26 20:28:52 +010011#include <asm/suspend.h>
12#include <asm/tlbflush.h>
13
Nicolas Pitre71a89862013-07-18 16:50:59 -040014extern int __cpu_suspend(unsigned long, int (*)(unsigned long), u32 cpuid);
Russell King62b2d072011-08-31 23:26:18 +010015extern void cpu_resume_mmu(void);
Russell Kinge8ce0eb2011-08-26 20:28:52 +010016
Will Deaconaa1aadc2012-02-23 13:51:38 +000017#ifdef CONFIG_MMU
Will Deaconaa1aadc2012-02-23 13:51:38 +000018int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
19{
20 struct mm_struct *mm = current->active_mm;
Nicolas Pitre71a89862013-07-18 16:50:59 -040021 u32 __mpidr = cpu_logical_map(smp_processor_id());
Will Deaconaa1aadc2012-02-23 13:51:38 +000022 int ret;
23
24 if (!idmap_pgd)
25 return -EINVAL;
26
27 /*
28 * Provide a temporary page table with an identity mapping for
29 * the MMU-enable code, required for resuming. On successful
30 * resume (indicated by a zero return code), we need to switch
31 * back to the correct page tables.
32 */
Nicolas Pitre71a89862013-07-18 16:50:59 -040033 ret = __cpu_suspend(arg, fn, __mpidr);
Will Deaconaa1aadc2012-02-23 13:51:38 +000034 if (ret == 0) {
35 cpu_switch_mm(mm->pgd, mm);
36 local_flush_bp_all();
37 local_flush_tlb_all();
38 }
39
40 return ret;
41}
42#else
43int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
44{
Nicolas Pitre71a89862013-07-18 16:50:59 -040045 u32 __mpidr = cpu_logical_map(smp_processor_id());
46 return __cpu_suspend(arg, fn, __mpidr);
Will Deaconaa1aadc2012-02-23 13:51:38 +000047}
48#define idmap_pgd NULL
49#endif
50
Russell Kinge8ce0eb2011-08-26 20:28:52 +010051/*
Russell Kingabda1bd2011-09-01 11:52:33 +010052 * This is called by __cpu_suspend() to save the state, and do whatever
53 * flushing is required to ensure that when the CPU goes to sleep we have
54 * the necessary data available when the caches are not searched.
55 */
56void __cpu_suspend_save(u32 *ptr, u32 ptrsz, u32 sp, u32 *save_ptr)
57{
Lorenzo Pieralisidbee0c62012-09-07 11:06:57 +053058 u32 *ctx = ptr;
59
Russell Kingabda1bd2011-09-01 11:52:33 +010060 *save_ptr = virt_to_phys(ptr);
61
62 /* This must correspond to the LDM in cpu_resume() assembly */
Will Deacone6eadc62011-11-15 11:11:19 +000063 *ptr++ = virt_to_phys(idmap_pgd);
Russell Kingabda1bd2011-09-01 11:52:33 +010064 *ptr++ = sp;
65 *ptr++ = virt_to_phys(cpu_do_resume);
66
67 cpu_do_suspend(ptr);
68
Lorenzo Pieralisidbee0c62012-09-07 11:06:57 +053069 flush_cache_louis();
70
71 /*
72 * flush_cache_louis does not guarantee that
73 * save_ptr and ptr are cleaned to main memory,
74 * just up to the Level of Unification Inner Shareable.
75 * Since the context pointer and context itself
76 * are to be retrieved with the MMU off that
77 * data must be cleaned from all cache levels
78 * to main memory using "area" cache primitives.
79 */
80 __cpuc_flush_dcache_area(ctx, ptrsz);
81 __cpuc_flush_dcache_area(save_ptr, sizeof(*save_ptr));
82
Russell King8e6f83b2011-09-01 11:57:59 +010083 outer_clean_range(*save_ptr, *save_ptr + ptrsz);
84 outer_clean_range(virt_to_phys(save_ptr),
85 virt_to_phys(save_ptr) + sizeof(*save_ptr));
Russell Kingabda1bd2011-09-01 11:52:33 +010086}
Lorenzo Pieralisi76045372013-05-16 10:34:30 +010087
88extern struct sleep_save_sp sleep_save_sp;
89
90static int cpu_suspend_alloc_sp(void)
91{
92 void *ctx_ptr;
93 /* ctx_ptr is an array of physical addresses */
94 ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(u32), GFP_KERNEL);
95
96 if (WARN_ON(!ctx_ptr))
97 return -ENOMEM;
98 sleep_save_sp.save_ptr_stash = ctx_ptr;
99 sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr);
100 sync_cache_w(&sleep_save_sp);
101 return 0;
102}
103early_initcall(cpu_suspend_alloc_sp);