Lorenzo Pieralisi | fb4a960 | 2014-01-24 10:56:19 +0000 | [diff] [blame] | 1 | #include <linux/percpu.h> |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 2 | #include <linux/slab.h> |
| 3 | #include <asm/cacheflush.h> |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 4 | #include <asm/debug-monitors.h> |
| 5 | #include <asm/pgtable.h> |
| 6 | #include <asm/memory.h> |
Lorenzo Pieralisi | f43c271 | 2014-12-19 17:03:47 +0000 | [diff] [blame] | 7 | #include <asm/mmu_context.h> |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 8 | #include <asm/smp_plat.h> |
| 9 | #include <asm/suspend.h> |
| 10 | #include <asm/tlbflush.h> |
| 11 | |
Lorenzo Pieralisi | 714f599 | 2014-08-07 14:54:50 +0100 | [diff] [blame] | 12 | extern int __cpu_suspend_enter(unsigned long arg, int (*fn)(unsigned long)); |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 13 | /* |
Lorenzo Pieralisi | 714f599 | 2014-08-07 14:54:50 +0100 | [diff] [blame] | 14 | * This is called by __cpu_suspend_enter() to save the state, and do whatever |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 15 | * flushing is required to ensure that when the CPU goes to sleep we have |
| 16 | * the necessary data available when the caches are not searched. |
| 17 | * |
Lorenzo Pieralisi | 714f599 | 2014-08-07 14:54:50 +0100 | [diff] [blame] | 18 | * ptr: CPU context virtual address |
| 19 | * save_ptr: address of the location where the context physical address |
| 20 | * must be saved |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 21 | */ |
Lorenzo Pieralisi | 714f599 | 2014-08-07 14:54:50 +0100 | [diff] [blame] | 22 | void notrace __cpu_suspend_save(struct cpu_suspend_ctx *ptr, |
| 23 | phys_addr_t *save_ptr) |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 24 | { |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 25 | *save_ptr = virt_to_phys(ptr); |
| 26 | |
| 27 | cpu_do_suspend(ptr); |
| 28 | /* |
| 29 | * Only flush the context that must be retrieved with the MMU |
| 30 | * off. VA primitives ensure the flush is applied to all |
| 31 | * cache levels so context is pushed to DRAM. |
| 32 | */ |
| 33 | __flush_dcache_area(ptr, sizeof(*ptr)); |
| 34 | __flush_dcache_area(save_ptr, sizeof(*save_ptr)); |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 35 | } |
| 36 | |
Lorenzo Pieralisi | 65c021b | 2014-01-10 13:15:05 +0000 | [diff] [blame] | 37 | /* |
| 38 | * This hook is provided so that cpu_suspend code can restore HW |
| 39 | * breakpoints as early as possible in the resume path, before reenabling |
| 40 | * debug exceptions. Code cannot be run from a CPU PM notifier since by the |
| 41 | * time the notifier runs debug exceptions might have been enabled already, |
| 42 | * with HW breakpoints registers content still in an unknown state. |
| 43 | */ |
| 44 | void (*hw_breakpoint_restore)(void *); |
| 45 | void __init cpu_suspend_set_dbg_restorer(void (*hw_bp_restore)(void *)) |
| 46 | { |
| 47 | /* Prevent multiple restore hook initializations */ |
| 48 | if (WARN_ON(hw_breakpoint_restore)) |
| 49 | return; |
| 50 | hw_breakpoint_restore = hw_bp_restore; |
| 51 | } |
| 52 | |
Lorenzo Pieralisi | 714f599 | 2014-08-07 14:54:50 +0100 | [diff] [blame] | 53 | /* |
Sudeep Holla | af391b1 | 2015-06-18 15:41:32 +0100 | [diff] [blame] | 54 | * cpu_suspend |
Lorenzo Pieralisi | 714f599 | 2014-08-07 14:54:50 +0100 | [diff] [blame] | 55 | * |
| 56 | * arg: argument to pass to the finisher function |
| 57 | * fn: finisher function pointer |
| 58 | * |
| 59 | */ |
Sudeep Holla | af391b1 | 2015-06-18 15:41:32 +0100 | [diff] [blame] | 60 | int cpu_suspend(unsigned long arg, int (*fn)(unsigned long)) |
Lorenzo Pieralisi | 714f599 | 2014-08-07 14:54:50 +0100 | [diff] [blame] | 61 | { |
| 62 | struct mm_struct *mm = current->active_mm; |
| 63 | int ret; |
| 64 | unsigned long flags; |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * From this point debug exceptions are disabled to prevent |
| 68 | * updates to mdscr register (saved and restored along with |
| 69 | * general purpose registers) from kernel debuggers. |
| 70 | */ |
| 71 | local_dbg_save(flags); |
| 72 | |
| 73 | /* |
| 74 | * mm context saved on the stack, it will be restored when |
| 75 | * the cpu comes out of reset through the identity mapped |
| 76 | * page tables, so that the thread address space is properly |
| 77 | * set-up on function return. |
| 78 | */ |
Lorenzo Pieralisi | 714f599 | 2014-08-07 14:54:50 +0100 | [diff] [blame] | 79 | ret = __cpu_suspend_enter(arg, fn); |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 80 | if (ret == 0) { |
Lorenzo Pieralisi | f43c271 | 2014-12-19 17:03:47 +0000 | [diff] [blame] | 81 | /* |
| 82 | * We are resuming from reset with TTBR0_EL1 set to the |
Lorenzo Pieralisi | e13d918 | 2015-10-27 17:29:10 +0000 | [diff] [blame^] | 83 | * idmap to enable the MMU; set the TTBR0 to the reserved |
| 84 | * page tables to prevent speculative TLB allocations, flush |
| 85 | * the local tlb and set the default tcr_el1.t0sz so that |
| 86 | * the TTBR0 address space set-up is properly restored. |
| 87 | * If the current active_mm != &init_mm we entered cpu_suspend |
| 88 | * with mappings in TTBR0 that must be restored, so we switch |
| 89 | * them back to complete the address space configuration |
| 90 | * restoration before returning. |
Lorenzo Pieralisi | f43c271 | 2014-12-19 17:03:47 +0000 | [diff] [blame] | 91 | */ |
Lorenzo Pieralisi | e13d918 | 2015-10-27 17:29:10 +0000 | [diff] [blame^] | 92 | cpu_set_reserved_ttbr0(); |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 93 | flush_tlb_all(); |
Lorenzo Pieralisi | e13d918 | 2015-10-27 17:29:10 +0000 | [diff] [blame^] | 94 | cpu_set_default_tcr_t0sz(); |
| 95 | |
| 96 | if (mm != &init_mm) |
| 97 | cpu_switch_mm(mm->pgd, mm); |
Lorenzo Pieralisi | fb4a960 | 2014-01-24 10:56:19 +0000 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * Restore per-cpu offset before any kernel |
| 101 | * subsystem relying on it has a chance to run. |
| 102 | */ |
Lorenzo Pieralisi | 714f599 | 2014-08-07 14:54:50 +0100 | [diff] [blame] | 103 | set_my_cpu_offset(per_cpu_offset(smp_processor_id())); |
Lorenzo Pieralisi | fb4a960 | 2014-01-24 10:56:19 +0000 | [diff] [blame] | 104 | |
Lorenzo Pieralisi | 65c021b | 2014-01-10 13:15:05 +0000 | [diff] [blame] | 105 | /* |
| 106 | * Restore HW breakpoint registers to sane values |
| 107 | * before debug exceptions are possibly reenabled |
| 108 | * through local_dbg_restore. |
| 109 | */ |
| 110 | if (hw_breakpoint_restore) |
| 111 | hw_breakpoint_restore(NULL); |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Restore pstate flags. OS lock and mdscr have been already |
| 116 | * restored, so from this point onwards, debugging is fully |
| 117 | * renabled if it was enabled when core started shutdown. |
| 118 | */ |
| 119 | local_dbg_restore(flags); |
| 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
Laura Abbott | c3684fb | 2014-11-21 21:50:40 +0000 | [diff] [blame] | 124 | struct sleep_save_sp sleep_save_sp; |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 125 | |
Lorenzo Pieralisi | 18ab7db | 2014-07-17 18:19:20 +0100 | [diff] [blame] | 126 | static int __init cpu_suspend_init(void) |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 127 | { |
| 128 | void *ctx_ptr; |
| 129 | |
| 130 | /* ctx_ptr is an array of physical addresses */ |
| 131 | ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(phys_addr_t), GFP_KERNEL); |
| 132 | |
| 133 | if (WARN_ON(!ctx_ptr)) |
| 134 | return -ENOMEM; |
| 135 | |
| 136 | sleep_save_sp.save_ptr_stash = ctx_ptr; |
| 137 | sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr); |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 138 | __flush_dcache_area(&sleep_save_sp, sizeof(struct sleep_save_sp)); |
Lorenzo Pieralisi | 9532252 | 2013-07-22 12:22:13 +0100 | [diff] [blame] | 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | early_initcall(cpu_suspend_init); |