blob: 2d60f19032068e62ef38e53f3e1a154118c9dcac [file] [log] [blame]
Russell Kinge8ce0eb2011-08-26 20:28:52 +01001#include <linux/init.h>
2
3#include <asm/pgalloc.h>
4#include <asm/pgtable.h>
5#include <asm/memory.h>
6#include <asm/suspend.h>
7#include <asm/tlbflush.h>
8
9static pgd_t *suspend_pgd;
10
Russell Kingabda1bd2011-09-01 11:52:33 +010011extern int __cpu_suspend(unsigned long, int (*)(unsigned long));
Russell King62b2d072011-08-31 23:26:18 +010012extern void cpu_resume_mmu(void);
Russell Kinge8ce0eb2011-08-26 20:28:52 +010013
14/*
Russell Kingabda1bd2011-09-01 11:52:33 +010015 * This is called by __cpu_suspend() to save the state, and do whatever
16 * flushing is required to ensure that when the CPU goes to sleep we have
17 * the necessary data available when the caches are not searched.
18 */
19void __cpu_suspend_save(u32 *ptr, u32 ptrsz, u32 sp, u32 *save_ptr)
20{
21 *save_ptr = virt_to_phys(ptr);
22
23 /* This must correspond to the LDM in cpu_resume() assembly */
24 *ptr++ = virt_to_phys(suspend_pgd);
25 *ptr++ = sp;
26 *ptr++ = virt_to_phys(cpu_do_resume);
27
28 cpu_do_suspend(ptr);
29
30 flush_cache_all();
31}
32
33/*
Russell Kinge8ce0eb2011-08-26 20:28:52 +010034 * Hide the first two arguments to __cpu_suspend - these are an implementation
35 * detail which platform code shouldn't have to know about.
36 */
37int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
38{
39 struct mm_struct *mm = current->active_mm;
40 int ret;
41
42 if (!suspend_pgd)
43 return -EINVAL;
44
45 /*
Russell Kingde8e71c2011-08-27 22:39:09 +010046 * Provide a temporary page table with an identity mapping for
47 * the MMU-enable code, required for resuming. On successful
48 * resume (indicated by a zero return code), we need to switch
49 * back to the correct page tables.
Russell Kinge8ce0eb2011-08-26 20:28:52 +010050 */
Russell Kingabda1bd2011-09-01 11:52:33 +010051 ret = __cpu_suspend(arg, fn);
Russell Kingde8e71c2011-08-27 22:39:09 +010052 if (ret == 0) {
53 cpu_switch_mm(mm->pgd, mm);
54 local_flush_tlb_all();
55 }
Russell Kinge8ce0eb2011-08-26 20:28:52 +010056
57 return ret;
58}
59
60static int __init cpu_suspend_init(void)
61{
62 suspend_pgd = pgd_alloc(&init_mm);
63 if (suspend_pgd) {
Russell King62b2d072011-08-31 23:26:18 +010064 unsigned long addr = virt_to_phys(cpu_resume_mmu);
Russell Kinge8ce0eb2011-08-26 20:28:52 +010065 identity_mapping_add(suspend_pgd, addr, addr + SECTION_SIZE);
66 }
67 return suspend_pgd ? 0 : -ENOMEM;
68}
69core_initcall(cpu_suspend_init);