• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <linux/init.h>
2 #include <linux/slab.h>
3 
4 #include <asm/bugs.h>
5 #include <asm/cacheflush.h>
6 #include <asm/idmap.h>
7 #include <asm/pgalloc.h>
8 #include <asm/pgtable.h>
9 #include <asm/memory.h>
10 #include <asm/smp_plat.h>
11 #include <asm/suspend.h>
12 #include <asm/tlbflush.h>
13 
14 extern int __cpu_suspend(unsigned long, int (*)(unsigned long), u32 cpuid);
15 extern void cpu_resume_mmu(void);
16 
17 #ifdef CONFIG_MMU
cpu_suspend(unsigned long arg,int (* fn)(unsigned long))18 int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
19 {
20 	struct mm_struct *mm = current->active_mm;
21 	u32 __mpidr = cpu_logical_map(smp_processor_id());
22 	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 	 */
33 	ret = __cpu_suspend(arg, fn, __mpidr);
34 	if (ret == 0) {
35 		cpu_switch_mm(mm->pgd, mm);
36 		local_flush_bp_all();
37 		local_flush_tlb_all();
38 		check_other_bugs();
39 	}
40 
41 	return ret;
42 }
43 #else
cpu_suspend(unsigned long arg,int (* fn)(unsigned long))44 int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
45 {
46 	u32 __mpidr = cpu_logical_map(smp_processor_id());
47 	return __cpu_suspend(arg, fn, __mpidr);
48 }
49 #define	idmap_pgd	NULL
50 #endif
51 
52 /*
53  * This is called by __cpu_suspend() to save the state, and do whatever
54  * flushing is required to ensure that when the CPU goes to sleep we have
55  * the necessary data available when the caches are not searched.
56  */
__cpu_suspend_save(u32 * ptr,u32 ptrsz,u32 sp,u32 * save_ptr)57 void __cpu_suspend_save(u32 *ptr, u32 ptrsz, u32 sp, u32 *save_ptr)
58 {
59 	u32 *ctx = ptr;
60 
61 	*save_ptr = virt_to_phys(ptr);
62 
63 	/* This must correspond to the LDM in cpu_resume() assembly */
64 	*ptr++ = virt_to_phys(idmap_pgd);
65 	*ptr++ = sp;
66 	*ptr++ = virt_to_phys(cpu_do_resume);
67 
68 	cpu_do_suspend(ptr);
69 
70 	flush_cache_louis();
71 
72 	/*
73 	 * flush_cache_louis does not guarantee that
74 	 * save_ptr and ptr are cleaned to main memory,
75 	 * just up to the Level of Unification Inner Shareable.
76 	 * Since the context pointer and context itself
77 	 * are to be retrieved with the MMU off that
78 	 * data must be cleaned from all cache levels
79 	 * to main memory using "area" cache primitives.
80 	*/
81 	__cpuc_flush_dcache_area(ctx, ptrsz);
82 	__cpuc_flush_dcache_area(save_ptr, sizeof(*save_ptr));
83 
84 	outer_clean_range(*save_ptr, *save_ptr + ptrsz);
85 	outer_clean_range(virt_to_phys(save_ptr),
86 			  virt_to_phys(save_ptr) + sizeof(*save_ptr));
87 }
88 
89 extern struct sleep_save_sp sleep_save_sp;
90 
cpu_suspend_alloc_sp(void)91 static int cpu_suspend_alloc_sp(void)
92 {
93 	void *ctx_ptr;
94 	/* ctx_ptr is an array of physical addresses */
95 	ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(u32), GFP_KERNEL);
96 
97 	if (WARN_ON(!ctx_ptr))
98 		return -ENOMEM;
99 	sleep_save_sp.save_ptr_stash = ctx_ptr;
100 	sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr);
101 	sync_cache_w(&sleep_save_sp);
102 	return 0;
103 }
104 early_initcall(cpu_suspend_alloc_sp);
105