Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Russell King | e8ce0eb | 2011-08-26 20:28:52 +0100 | [diff] [blame] | 2 | #include <linux/init.h> |
Lorenzo Pieralisi | 7604537 | 2013-05-16 10:34:30 +0100 | [diff] [blame] | 3 | #include <linux/slab.h> |
Ingo Molnar | 589ee62 | 2017-02-04 00:16:44 +0100 | [diff] [blame] | 4 | #include <linux/mm_types.h> |
Mike Rapoport | 65fddcf | 2020-06-08 21:32:42 -0700 | [diff] [blame] | 5 | #include <linux/pgtable.h> |
Russell King | e8ce0eb | 2011-08-26 20:28:52 +0100 | [diff] [blame] | 6 | |
Russell King | 2660216 | 2018-05-10 13:00:43 +0100 | [diff] [blame] | 7 | #include <asm/bugs.h> |
Lorenzo Pieralisi | 7604537 | 2013-05-16 10:34:30 +0100 | [diff] [blame] | 8 | #include <asm/cacheflush.h> |
Will Deacon | e6eadc6 | 2011-11-15 11:11:19 +0000 | [diff] [blame] | 9 | #include <asm/idmap.h> |
Russell King | e8ce0eb | 2011-08-26 20:28:52 +0100 | [diff] [blame] | 10 | #include <asm/pgalloc.h> |
Russell King | e8ce0eb | 2011-08-26 20:28:52 +0100 | [diff] [blame] | 11 | #include <asm/memory.h> |
Lorenzo Pieralisi | 7604537 | 2013-05-16 10:34:30 +0100 | [diff] [blame] | 12 | #include <asm/smp_plat.h> |
Russell King | e8ce0eb | 2011-08-26 20:28:52 +0100 | [diff] [blame] | 13 | #include <asm/suspend.h> |
| 14 | #include <asm/tlbflush.h> |
| 15 | |
Nicolas Pitre | 71a8986 | 2013-07-18 16:50:59 -0400 | [diff] [blame] | 16 | extern int __cpu_suspend(unsigned long, int (*)(unsigned long), u32 cpuid); |
Russell King | 62b2d07 | 2011-08-31 23:26:18 +0100 | [diff] [blame] | 17 | extern void cpu_resume_mmu(void); |
Russell King | e8ce0eb | 2011-08-26 20:28:52 +0100 | [diff] [blame] | 18 | |
Will Deacon | aa1aadc | 2012-02-23 13:51:38 +0000 | [diff] [blame] | 19 | #ifdef CONFIG_MMU |
Will Deacon | aa1aadc | 2012-02-23 13:51:38 +0000 | [diff] [blame] | 20 | int cpu_suspend(unsigned long arg, int (*fn)(unsigned long)) |
| 21 | { |
| 22 | struct mm_struct *mm = current->active_mm; |
Nicolas Pitre | 71a8986 | 2013-07-18 16:50:59 -0400 | [diff] [blame] | 23 | u32 __mpidr = cpu_logical_map(smp_processor_id()); |
Will Deacon | aa1aadc | 2012-02-23 13:51:38 +0000 | [diff] [blame] | 24 | int ret; |
| 25 | |
| 26 | if (!idmap_pgd) |
| 27 | return -EINVAL; |
| 28 | |
| 29 | /* |
| 30 | * Provide a temporary page table with an identity mapping for |
| 31 | * the MMU-enable code, required for resuming. On successful |
| 32 | * resume (indicated by a zero return code), we need to switch |
| 33 | * back to the correct page tables. |
| 34 | */ |
Nicolas Pitre | 71a8986 | 2013-07-18 16:50:59 -0400 | [diff] [blame] | 35 | ret = __cpu_suspend(arg, fn, __mpidr); |
Will Deacon | aa1aadc | 2012-02-23 13:51:38 +0000 | [diff] [blame] | 36 | if (ret == 0) { |
| 37 | cpu_switch_mm(mm->pgd, mm); |
| 38 | local_flush_bp_all(); |
| 39 | local_flush_tlb_all(); |
Russell King | 2660216 | 2018-05-10 13:00:43 +0100 | [diff] [blame] | 40 | check_other_bugs(); |
Will Deacon | aa1aadc | 2012-02-23 13:51:38 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | return ret; |
| 44 | } |
| 45 | #else |
| 46 | int cpu_suspend(unsigned long arg, int (*fn)(unsigned long)) |
| 47 | { |
Nicolas Pitre | 71a8986 | 2013-07-18 16:50:59 -0400 | [diff] [blame] | 48 | u32 __mpidr = cpu_logical_map(smp_processor_id()); |
| 49 | return __cpu_suspend(arg, fn, __mpidr); |
Will Deacon | aa1aadc | 2012-02-23 13:51:38 +0000 | [diff] [blame] | 50 | } |
| 51 | #define idmap_pgd NULL |
| 52 | #endif |
| 53 | |
Russell King | e8ce0eb | 2011-08-26 20:28:52 +0100 | [diff] [blame] | 54 | /* |
Russell King | abda1bd | 2011-09-01 11:52:33 +0100 | [diff] [blame] | 55 | * This is called by __cpu_suspend() to save the state, and do whatever |
| 56 | * flushing is required to ensure that when the CPU goes to sleep we have |
| 57 | * the necessary data available when the caches are not searched. |
| 58 | */ |
| 59 | void __cpu_suspend_save(u32 *ptr, u32 ptrsz, u32 sp, u32 *save_ptr) |
| 60 | { |
Lorenzo Pieralisi | dbee0c6 | 2012-09-07 11:06:57 +0530 | [diff] [blame] | 61 | u32 *ctx = ptr; |
| 62 | |
Russell King | abda1bd | 2011-09-01 11:52:33 +0100 | [diff] [blame] | 63 | *save_ptr = virt_to_phys(ptr); |
| 64 | |
| 65 | /* This must correspond to the LDM in cpu_resume() assembly */ |
Will Deacon | e6eadc6 | 2011-11-15 11:11:19 +0000 | [diff] [blame] | 66 | *ptr++ = virt_to_phys(idmap_pgd); |
Russell King | abda1bd | 2011-09-01 11:52:33 +0100 | [diff] [blame] | 67 | *ptr++ = sp; |
| 68 | *ptr++ = virt_to_phys(cpu_do_resume); |
| 69 | |
| 70 | cpu_do_suspend(ptr); |
| 71 | |
Lorenzo Pieralisi | dbee0c6 | 2012-09-07 11:06:57 +0530 | [diff] [blame] | 72 | flush_cache_louis(); |
| 73 | |
| 74 | /* |
| 75 | * flush_cache_louis does not guarantee that |
| 76 | * save_ptr and ptr are cleaned to main memory, |
| 77 | * just up to the Level of Unification Inner Shareable. |
| 78 | * Since the context pointer and context itself |
| 79 | * are to be retrieved with the MMU off that |
| 80 | * data must be cleaned from all cache levels |
| 81 | * to main memory using "area" cache primitives. |
| 82 | */ |
| 83 | __cpuc_flush_dcache_area(ctx, ptrsz); |
| 84 | __cpuc_flush_dcache_area(save_ptr, sizeof(*save_ptr)); |
| 85 | |
Russell King | 8e6f83b | 2011-09-01 11:57:59 +0100 | [diff] [blame] | 86 | outer_clean_range(*save_ptr, *save_ptr + ptrsz); |
| 87 | outer_clean_range(virt_to_phys(save_ptr), |
| 88 | virt_to_phys(save_ptr) + sizeof(*save_ptr)); |
Russell King | abda1bd | 2011-09-01 11:52:33 +0100 | [diff] [blame] | 89 | } |
Lorenzo Pieralisi | 7604537 | 2013-05-16 10:34:30 +0100 | [diff] [blame] | 90 | |
| 91 | extern struct sleep_save_sp sleep_save_sp; |
| 92 | |
| 93 | static int cpu_suspend_alloc_sp(void) |
| 94 | { |
| 95 | void *ctx_ptr; |
| 96 | /* ctx_ptr is an array of physical addresses */ |
| 97 | ctx_ptr = kcalloc(mpidr_hash_size(), sizeof(u32), GFP_KERNEL); |
| 98 | |
| 99 | if (WARN_ON(!ctx_ptr)) |
| 100 | return -ENOMEM; |
| 101 | sleep_save_sp.save_ptr_stash = ctx_ptr; |
| 102 | sleep_save_sp.save_ptr_stash_phys = virt_to_phys(ctx_ptr); |
| 103 | sync_cache_w(&sleep_save_sp); |
| 104 | return 0; |
| 105 | } |
| 106 | early_initcall(cpu_suspend_alloc_sp); |