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