Russell King | e8ce0eb | 2011-08-26 20:28:52 +0100 | [diff] [blame] | 1 | #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 | |
| 9 | static pgd_t *suspend_pgd; |
| 10 | |
Russell King | abda1bd | 2011-09-01 11:52:33 +0100 | [diff] [blame] | 11 | extern int __cpu_suspend(unsigned long, int (*)(unsigned long)); |
Russell King | 62b2d07 | 2011-08-31 23:26:18 +0100 | [diff] [blame] | 12 | extern void cpu_resume_mmu(void); |
Russell King | e8ce0eb | 2011-08-26 20:28:52 +0100 | [diff] [blame] | 13 | |
| 14 | /* |
Russell King | abda1bd | 2011-09-01 11:52:33 +0100 | [diff] [blame] | 15 | * 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 | */ |
| 19 | void __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(); |
Russell King | 8e6f83b | 2011-09-01 11:57:59 +0100 | [diff] [blame^] | 31 | outer_clean_range(*save_ptr, *save_ptr + ptrsz); |
| 32 | outer_clean_range(virt_to_phys(save_ptr), |
| 33 | virt_to_phys(save_ptr) + sizeof(*save_ptr)); |
Russell King | abda1bd | 2011-09-01 11:52:33 +0100 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /* |
Russell King | e8ce0eb | 2011-08-26 20:28:52 +0100 | [diff] [blame] | 37 | * Hide the first two arguments to __cpu_suspend - these are an implementation |
| 38 | * detail which platform code shouldn't have to know about. |
| 39 | */ |
| 40 | int cpu_suspend(unsigned long arg, int (*fn)(unsigned long)) |
| 41 | { |
| 42 | struct mm_struct *mm = current->active_mm; |
| 43 | int ret; |
| 44 | |
| 45 | if (!suspend_pgd) |
| 46 | return -EINVAL; |
| 47 | |
| 48 | /* |
Russell King | de8e71c | 2011-08-27 22:39:09 +0100 | [diff] [blame] | 49 | * Provide a temporary page table with an identity mapping for |
| 50 | * the MMU-enable code, required for resuming. On successful |
| 51 | * resume (indicated by a zero return code), we need to switch |
| 52 | * back to the correct page tables. |
Russell King | e8ce0eb | 2011-08-26 20:28:52 +0100 | [diff] [blame] | 53 | */ |
Russell King | abda1bd | 2011-09-01 11:52:33 +0100 | [diff] [blame] | 54 | ret = __cpu_suspend(arg, fn); |
Russell King | de8e71c | 2011-08-27 22:39:09 +0100 | [diff] [blame] | 55 | if (ret == 0) { |
| 56 | cpu_switch_mm(mm->pgd, mm); |
| 57 | local_flush_tlb_all(); |
| 58 | } |
Russell King | e8ce0eb | 2011-08-26 20:28:52 +0100 | [diff] [blame] | 59 | |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | static int __init cpu_suspend_init(void) |
| 64 | { |
| 65 | suspend_pgd = pgd_alloc(&init_mm); |
| 66 | if (suspend_pgd) { |
Russell King | 62b2d07 | 2011-08-31 23:26:18 +0100 | [diff] [blame] | 67 | unsigned long addr = virt_to_phys(cpu_resume_mmu); |
Russell King | e8ce0eb | 2011-08-26 20:28:52 +0100 | [diff] [blame] | 68 | identity_mapping_add(suspend_pgd, addr, addr + SECTION_SIZE); |
| 69 | } |
| 70 | return suspend_pgd ? 0 : -ENOMEM; |
| 71 | } |
| 72 | core_initcall(cpu_suspend_init); |