Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2010 |
| 4 | * Texas Instruments, <www.ti.com> |
| 5 | * Aneesh V <aneesh@ti.com> |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 6 | */ |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 7 | #include <cpu_func.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 8 | #include <asm/cache.h> |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 9 | #include <linux/types.h> |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 10 | #include <asm/armv7.h> |
| 11 | #include <asm/utils.h> |
| 12 | |
Hans de Goede | ba3bf9b | 2016-04-09 13:53:49 +0200 | [diff] [blame] | 13 | #define ARMV7_DCACHE_INVAL_RANGE 1 |
| 14 | #define ARMV7_DCACHE_CLEAN_INVAL_RANGE 2 |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 15 | |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 16 | #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) |
Hans de Goede | 076e841 | 2016-04-09 13:53:48 +0200 | [diff] [blame] | 17 | |
| 18 | /* Asm functions from cache_v7_asm.S */ |
| 19 | void v7_flush_dcache_all(void); |
Hans de Goede | ba3bf9b | 2016-04-09 13:53:49 +0200 | [diff] [blame] | 20 | void v7_invalidate_dcache_all(void); |
Hans de Goede | 076e841 | 2016-04-09 13:53:48 +0200 | [diff] [blame] | 21 | |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 22 | static u32 get_ccsidr(void) |
| 23 | { |
| 24 | u32 ccsidr; |
| 25 | |
| 26 | /* Read current CP15 Cache Size ID Register */ |
| 27 | asm volatile ("mrc p15, 1, %0, c0, c0, 0" : "=r" (ccsidr)); |
| 28 | return ccsidr; |
| 29 | } |
| 30 | |
Thierry Reding | 0c59738 | 2014-08-26 17:34:20 +0200 | [diff] [blame] | 31 | static void v7_dcache_clean_inval_range(u32 start, u32 stop, u32 line_len) |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 32 | { |
| 33 | u32 mva; |
| 34 | |
| 35 | /* Align start to cache line boundary */ |
| 36 | start &= ~(line_len - 1); |
| 37 | for (mva = start; mva < stop; mva = mva + line_len) { |
| 38 | /* DCCIMVAC - Clean & Invalidate data cache by MVA to PoC */ |
| 39 | asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva)); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | static void v7_dcache_inval_range(u32 start, u32 stop, u32 line_len) |
| 44 | { |
| 45 | u32 mva; |
| 46 | |
Simon Glass | b7ba957 | 2016-06-19 19:43:02 -0600 | [diff] [blame] | 47 | if (!check_cache_range(start, stop)) |
| 48 | return; |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 49 | |
| 50 | for (mva = start; mva < stop; mva = mva + line_len) { |
| 51 | /* DCIMVAC - Invalidate data cache by MVA to PoC */ |
| 52 | asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (mva)); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static void v7_dcache_maint_range(u32 start, u32 stop, u32 range_op) |
| 57 | { |
| 58 | u32 line_len, ccsidr; |
| 59 | |
| 60 | ccsidr = get_ccsidr(); |
| 61 | line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >> |
| 62 | CCSIDR_LINE_SIZE_OFFSET) + 2; |
| 63 | /* Converting from words to bytes */ |
| 64 | line_len += 2; |
| 65 | /* converting from log2(linelen) to linelen */ |
| 66 | line_len = 1 << line_len; |
| 67 | |
| 68 | switch (range_op) { |
| 69 | case ARMV7_DCACHE_CLEAN_INVAL_RANGE: |
| 70 | v7_dcache_clean_inval_range(start, stop, line_len); |
| 71 | break; |
| 72 | case ARMV7_DCACHE_INVAL_RANGE: |
| 73 | v7_dcache_inval_range(start, stop, line_len); |
| 74 | break; |
| 75 | } |
| 76 | |
Aneesh V | 517912b | 2011-08-11 04:35:44 +0000 | [diff] [blame] | 77 | /* DSB to make sure the operation is complete */ |
Tom Rini | 3b787ef | 2016-08-01 18:54:53 -0400 | [diff] [blame] | 78 | dsb(); |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* Invalidate TLB */ |
| 82 | static void v7_inval_tlb(void) |
| 83 | { |
| 84 | /* Invalidate entire unified TLB */ |
| 85 | asm volatile ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0)); |
| 86 | /* Invalidate entire data TLB */ |
| 87 | asm volatile ("mcr p15, 0, %0, c8, c6, 0" : : "r" (0)); |
| 88 | /* Invalidate entire instruction TLB */ |
| 89 | asm volatile ("mcr p15, 0, %0, c8, c5, 0" : : "r" (0)); |
| 90 | /* Full system DSB - make sure that the invalidation is complete */ |
Tom Rini | 3b787ef | 2016-08-01 18:54:53 -0400 | [diff] [blame] | 91 | dsb(); |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 92 | /* Full system ISB - make sure the instruction stream sees it */ |
Tom Rini | 3b787ef | 2016-08-01 18:54:53 -0400 | [diff] [blame] | 93 | isb(); |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void invalidate_dcache_all(void) |
| 97 | { |
Hans de Goede | ba3bf9b | 2016-04-09 13:53:49 +0200 | [diff] [blame] | 98 | v7_invalidate_dcache_all(); |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 99 | |
| 100 | v7_outer_cache_inval_all(); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Performs a clean & invalidation of the entire data cache |
| 105 | * at all levels |
| 106 | */ |
| 107 | void flush_dcache_all(void) |
| 108 | { |
Hans de Goede | 076e841 | 2016-04-09 13:53:48 +0200 | [diff] [blame] | 109 | v7_flush_dcache_all(); |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 110 | |
| 111 | v7_outer_cache_flush_all(); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Invalidates range in all levels of D-cache/unified cache used: |
| 116 | * Affects the range [start, stop - 1] |
| 117 | */ |
| 118 | void invalidate_dcache_range(unsigned long start, unsigned long stop) |
| 119 | { |
Marek Vasut | c28ad23 | 2015-07-27 22:34:17 +0200 | [diff] [blame] | 120 | check_cache_range(start, stop); |
| 121 | |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 122 | v7_dcache_maint_range(start, stop, ARMV7_DCACHE_INVAL_RANGE); |
| 123 | |
| 124 | v7_outer_cache_inval_range(start, stop); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Flush range(clean & invalidate) from all levels of D-cache/unified |
| 129 | * cache used: |
| 130 | * Affects the range [start, stop - 1] |
| 131 | */ |
| 132 | void flush_dcache_range(unsigned long start, unsigned long stop) |
| 133 | { |
Marek Vasut | c28ad23 | 2015-07-27 22:34:17 +0200 | [diff] [blame] | 134 | check_cache_range(start, stop); |
| 135 | |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 136 | v7_dcache_maint_range(start, stop, ARMV7_DCACHE_CLEAN_INVAL_RANGE); |
| 137 | |
| 138 | v7_outer_cache_flush_range(start, stop); |
| 139 | } |
| 140 | |
| 141 | void arm_init_before_mmu(void) |
| 142 | { |
| 143 | v7_outer_cache_enable(); |
| 144 | invalidate_dcache_all(); |
| 145 | v7_inval_tlb(); |
| 146 | } |
| 147 | |
Simon Glass | a4f2079 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 148 | void mmu_page_table_flush(unsigned long start, unsigned long stop) |
| 149 | { |
| 150 | flush_dcache_range(start, stop); |
| 151 | v7_inval_tlb(); |
| 152 | } |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 153 | #else /* #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */ |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 154 | void invalidate_dcache_all(void) |
| 155 | { |
| 156 | } |
| 157 | |
| 158 | void flush_dcache_all(void) |
| 159 | { |
| 160 | } |
| 161 | |
Daniel Allred | 4631f7d | 2016-06-27 09:19:16 -0500 | [diff] [blame] | 162 | void invalidate_dcache_range(unsigned long start, unsigned long stop) |
| 163 | { |
| 164 | } |
| 165 | |
| 166 | void flush_dcache_range(unsigned long start, unsigned long stop) |
| 167 | { |
| 168 | } |
| 169 | |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 170 | void arm_init_before_mmu(void) |
| 171 | { |
| 172 | } |
| 173 | |
Simon Glass | a4f2079 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 174 | void mmu_page_table_flush(unsigned long start, unsigned long stop) |
| 175 | { |
| 176 | } |
| 177 | |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 178 | #endif /* #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */ |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 179 | |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 180 | #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 181 | /* Invalidate entire I-cache and branch predictor array */ |
| 182 | void invalidate_icache_all(void) |
| 183 | { |
| 184 | /* |
| 185 | * Invalidate all instruction caches to PoU. |
| 186 | * Also flushes branch target cache. |
| 187 | */ |
| 188 | asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0)); |
| 189 | |
| 190 | /* Invalidate entire branch predictor array */ |
| 191 | asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0)); |
| 192 | |
| 193 | /* Full system DSB - make sure that the invalidation is complete */ |
Tom Rini | 3b787ef | 2016-08-01 18:54:53 -0400 | [diff] [blame] | 194 | dsb(); |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 195 | |
| 196 | /* ISB - make sure the instruction stream sees it */ |
Tom Rini | 3b787ef | 2016-08-01 18:54:53 -0400 | [diff] [blame] | 197 | isb(); |
Aneesh V | 960f5c0 | 2011-06-16 23:30:47 +0000 | [diff] [blame] | 198 | } |
| 199 | #else |
| 200 | void invalidate_icache_all(void) |
| 201 | { |
| 202 | } |
| 203 | #endif |
| 204 | |
Jeroen Hofstee | d746077 | 2014-06-23 22:07:04 +0200 | [diff] [blame] | 205 | /* Stub implementations for outer cache operations */ |
| 206 | __weak void v7_outer_cache_enable(void) {} |
| 207 | __weak void v7_outer_cache_disable(void) {} |
| 208 | __weak void v7_outer_cache_flush_all(void) {} |
| 209 | __weak void v7_outer_cache_inval_all(void) {} |
| 210 | __weak void v7_outer_cache_flush_range(u32 start, u32 end) {} |
| 211 | __weak void v7_outer_cache_inval_range(u32 start, u32 end) {} |