Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Cache maintenance |
| 3 | * |
| 4 | * Copyright (C) 2001 Deep Blue Solutions Ltd. |
| 5 | * Copyright (C) 2012 ARM Ltd. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
Vladimir Murzin | a2d25a5 | 2014-12-01 10:53:08 +0000 | [diff] [blame] | 20 | #include <linux/errno.h> |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 21 | #include <linux/linkage.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <asm/assembler.h> |
Andre Przywara | 301bcfa | 2014-11-14 15:54:10 +0000 | [diff] [blame] | 24 | #include <asm/cpufeature.h> |
Marc Zyngier | 8d883b2 | 2015-06-01 10:47:41 +0100 | [diff] [blame] | 25 | #include <asm/alternative.h> |
Al Viro | b4b8664 | 2016-12-26 04:10:19 -0500 | [diff] [blame] | 26 | #include <asm/asm-uaccess.h> |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 27 | |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 28 | /* |
Rohit Vaswani | 208984f | 2016-01-04 14:08:32 -0800 | [diff] [blame] | 29 | * __flush_dcache_all() |
| 30 | * |
| 31 | * Flush the whole D-cache. |
| 32 | * |
| 33 | * Corrupted registers: x0-x7, x9-x11 |
| 34 | */ |
| 35 | __flush_dcache_all: |
| 36 | dmb sy // ensure ordering with previous memory accesses |
| 37 | mrs x0, clidr_el1 // read clidr |
| 38 | and x3, x0, #0x7000000 // extract loc from clidr |
| 39 | lsr x3, x3, #23 // left align loc bit field |
| 40 | cbz x3, finished // if loc is 0, then no need to clean |
| 41 | mov x10, #0 // start clean at cache level 0 |
| 42 | loop1: |
| 43 | add x2, x10, x10, lsr #1 // work out 3x current cache level |
| 44 | lsr x1, x0, x2 // extract cache type bits from clidr |
| 45 | and x1, x1, #7 // mask of the bits for current cache only |
| 46 | cmp x1, #2 // see what cache we have at this level |
| 47 | b.lt skip // skip if no cache, or just i-cache |
| 48 | save_and_disable_irqs x9 // make CSSELR and CCSIDR access atomic |
| 49 | msr csselr_el1, x10 // select current cache level in csselr |
| 50 | isb // isb to sych the new cssr&csidr |
| 51 | mrs x1, ccsidr_el1 // read the new ccsidr |
| 52 | restore_irqs x9 |
| 53 | and x2, x1, #7 // extract the length of the cache lines |
| 54 | add x2, x2, #4 // add 4 (line length offset) |
| 55 | mov x4, #0x3ff |
| 56 | and x4, x4, x1, lsr #3 // find maximum number on the way size |
| 57 | clz w5, w4 // find bit position of way size increment |
| 58 | mov x7, #0x7fff |
| 59 | and x7, x7, x1, lsr #13 // extract max number of the index size |
| 60 | loop2: |
| 61 | mov x9, x4 // create working copy of max way size |
| 62 | loop3: |
| 63 | lsl x6, x9, x5 |
| 64 | orr x11, x10, x6 // factor way and cache number into x11 |
| 65 | lsl x6, x7, x2 |
| 66 | orr x11, x11, x6 // factor index number into x11 |
| 67 | dc cisw, x11 // clean & invalidate by set/way |
| 68 | subs x9, x9, #1 // decrement the way |
| 69 | b.ge loop3 |
| 70 | subs x7, x7, #1 // decrement the index |
| 71 | b.ge loop2 |
| 72 | skip: |
| 73 | add x10, x10, #2 // increment cache number |
| 74 | cmp x3, x10 |
| 75 | b.gt loop1 |
| 76 | finished: |
| 77 | mov x10, #0 // swith back to cache level 0 |
| 78 | msr csselr_el1, x10 // select current cache level in csselr |
| 79 | dsb sy |
| 80 | isb |
| 81 | ret |
| 82 | ENDPROC(__flush_dcache_all) |
| 83 | |
| 84 | /* |
| 85 | * flush_cache_all() |
| 86 | * |
| 87 | * Flush the entire cache system. The data cache flush is now achieved |
| 88 | * using atomic clean / invalidates working outwards from L1 cache. This |
| 89 | * is done using Set/Way based cache maintenance instructions. The |
| 90 | * instruction cache can still be invalidated back to the point of |
| 91 | * unification in a single instruction. |
| 92 | */ |
| 93 | ENTRY(flush_cache_all) |
| 94 | mov x12, lr |
| 95 | bl __flush_dcache_all |
| 96 | mov x0, #0 |
| 97 | ic ialluis // I+BTB cache invalidate |
| 98 | ret x12 |
| 99 | ENDPROC(flush_cache_all) |
| 100 | |
| 101 | /* |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 102 | * flush_icache_range(start,end) |
| 103 | * |
| 104 | * Ensure that the I and D caches are coherent within specified region. |
| 105 | * This is typically used when code has been written to a memory region, |
| 106 | * and will be executed. |
| 107 | * |
| 108 | * - start - virtual start address of region |
| 109 | * - end - virtual end address of region |
| 110 | */ |
Will Deacon | 3b8c9f1 | 2018-06-11 14:22:09 +0100 | [diff] [blame] | 111 | ENTRY(__flush_icache_range) |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 112 | /* FALLTHROUGH */ |
| 113 | |
| 114 | /* |
| 115 | * __flush_cache_user_range(start,end) |
| 116 | * |
| 117 | * Ensure that the I and D caches are coherent within specified region. |
| 118 | * This is typically used when code has been written to a memory region, |
| 119 | * and will be executed. |
| 120 | * |
| 121 | * - start - virtual start address of region |
| 122 | * - end - virtual end address of region |
| 123 | */ |
| 124 | ENTRY(__flush_cache_user_range) |
Christoffer Dall | 448fadc | 2018-01-09 11:51:58 +0100 | [diff] [blame] | 125 | uaccess_ttbr0_enable x2, x3, x4 |
Shanker Donthineni | 6ae4b6e | 2018-03-07 09:00:08 -0600 | [diff] [blame] | 126 | alternative_if ARM64_HAS_CACHE_IDC |
| 127 | dsb ishst |
| 128 | b 7f |
| 129 | alternative_else_nop_endif |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 130 | dcache_line_size x2, x3 |
| 131 | sub x3, x2, #1 |
| 132 | bic x4, x0, x3 |
| 133 | 1: |
Andre Przywara | 290622e | 2016-06-28 18:07:28 +0100 | [diff] [blame] | 134 | user_alt 9f, "dc cvau, x4", "dc civac, x4", ARM64_WORKAROUND_CLEAN_CACHE |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 135 | add x4, x4, x2 |
| 136 | cmp x4, x1 |
| 137 | b.lo 1b |
Will Deacon | dc60b77 | 2014-05-02 16:24:15 +0100 | [diff] [blame] | 138 | dsb ish |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 139 | |
Shanker Donthineni | 6ae4b6e | 2018-03-07 09:00:08 -0600 | [diff] [blame] | 140 | 7: |
| 141 | alternative_if ARM64_HAS_CACHE_DIC |
| 142 | isb |
| 143 | b 8f |
| 144 | alternative_else_nop_endif |
Marc Zyngier | 4fee947 | 2017-10-23 17:11:16 +0100 | [diff] [blame] | 145 | invalidate_icache_by_line x0, x1, x2, x3, 9f |
Shanker Donthineni | 6ae4b6e | 2018-03-07 09:00:08 -0600 | [diff] [blame] | 146 | 8: mov x0, #0 |
Catalin Marinas | 39bc88e | 2016-09-02 14:54:03 +0100 | [diff] [blame] | 147 | 1: |
Christoffer Dall | 0482b50 | 2018-01-17 12:35:27 +0100 | [diff] [blame] | 148 | uaccess_ttbr0_disable x1, x2 |
Vladimir Murzin | a2d25a5 | 2014-12-01 10:53:08 +0000 | [diff] [blame] | 149 | ret |
| 150 | 9: |
| 151 | mov x0, #-EFAULT |
Catalin Marinas | 39bc88e | 2016-09-02 14:54:03 +0100 | [diff] [blame] | 152 | b 1b |
Will Deacon | 3b8c9f1 | 2018-06-11 14:22:09 +0100 | [diff] [blame] | 153 | ENDPROC(__flush_icache_range) |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 154 | ENDPROC(__flush_cache_user_range) |
| 155 | |
| 156 | /* |
Marc Zyngier | 4fee947 | 2017-10-23 17:11:16 +0100 | [diff] [blame] | 157 | * invalidate_icache_range(start,end) |
| 158 | * |
| 159 | * Ensure that the I cache is invalid within specified region. |
| 160 | * |
| 161 | * - start - virtual start address of region |
| 162 | * - end - virtual end address of region |
| 163 | */ |
| 164 | ENTRY(invalidate_icache_range) |
Shanker Donthineni | 6ae4b6e | 2018-03-07 09:00:08 -0600 | [diff] [blame] | 165 | alternative_if ARM64_HAS_CACHE_DIC |
| 166 | mov x0, xzr |
| 167 | isb |
| 168 | ret |
| 169 | alternative_else_nop_endif |
| 170 | |
Christoffer Dall | 448fadc | 2018-01-09 11:51:58 +0100 | [diff] [blame] | 171 | uaccess_ttbr0_enable x2, x3, x4 |
Marc Zyngier | 4fee947 | 2017-10-23 17:11:16 +0100 | [diff] [blame] | 172 | |
| 173 | invalidate_icache_by_line x0, x1, x2, x3, 2f |
| 174 | mov x0, xzr |
| 175 | 1: |
Christoffer Dall | 0482b50 | 2018-01-17 12:35:27 +0100 | [diff] [blame] | 176 | uaccess_ttbr0_disable x1, x2 |
Marc Zyngier | 4fee947 | 2017-10-23 17:11:16 +0100 | [diff] [blame] | 177 | ret |
| 178 | 2: |
| 179 | mov x0, #-EFAULT |
| 180 | b 1b |
| 181 | ENDPROC(invalidate_icache_range) |
| 182 | |
| 183 | /* |
Jingoo Han | 03324e6 | 2014-01-21 01:17:47 +0000 | [diff] [blame] | 184 | * __flush_dcache_area(kaddr, size) |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 185 | * |
Ashok Kumar | 0a28714 | 2015-12-17 01:38:32 -0800 | [diff] [blame] | 186 | * Ensure that any D-cache lines for the interval [kaddr, kaddr+size) |
| 187 | * are cleaned and invalidated to the PoC. |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 188 | * |
| 189 | * - kaddr - kernel address |
| 190 | * - size - size in question |
| 191 | */ |
| 192 | ENTRY(__flush_dcache_area) |
Ashok Kumar | 0a28714 | 2015-12-17 01:38:32 -0800 | [diff] [blame] | 193 | dcache_by_line_op civac, sy, x0, x1, x2, x3 |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 194 | ret |
Ard Biesheuvel | 2079184 | 2015-10-08 20:02:03 +0100 | [diff] [blame] | 195 | ENDPIPROC(__flush_dcache_area) |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 196 | |
| 197 | /* |
Ashok Kumar | 0a28714 | 2015-12-17 01:38:32 -0800 | [diff] [blame] | 198 | * __clean_dcache_area_pou(kaddr, size) |
| 199 | * |
| 200 | * Ensure that any D-cache lines for the interval [kaddr, kaddr+size) |
| 201 | * are cleaned to the PoU. |
| 202 | * |
| 203 | * - kaddr - kernel address |
| 204 | * - size - size in question |
| 205 | */ |
| 206 | ENTRY(__clean_dcache_area_pou) |
Shanker Donthineni | 6ae4b6e | 2018-03-07 09:00:08 -0600 | [diff] [blame] | 207 | alternative_if ARM64_HAS_CACHE_IDC |
| 208 | dsb ishst |
| 209 | ret |
| 210 | alternative_else_nop_endif |
Ashok Kumar | 0a28714 | 2015-12-17 01:38:32 -0800 | [diff] [blame] | 211 | dcache_by_line_op cvau, ish, x0, x1, x2, x3 |
| 212 | ret |
| 213 | ENDPROC(__clean_dcache_area_pou) |
| 214 | |
| 215 | /* |
Robin Murphy | d46befe | 2017-07-25 11:55:39 +0100 | [diff] [blame] | 216 | * __inval_dcache_area(kaddr, size) |
| 217 | * |
| 218 | * Ensure that any D-cache lines for the interval [kaddr, kaddr+size) |
| 219 | * are invalidated. Any partial lines at the ends of the interval are |
| 220 | * also cleaned to PoC to prevent data loss. |
| 221 | * |
| 222 | * - kaddr - kernel address |
| 223 | * - size - size in question |
| 224 | */ |
| 225 | ENTRY(__inval_dcache_area) |
| 226 | /* FALLTHROUGH */ |
| 227 | |
| 228 | /* |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 229 | * __dma_inv_area(start, size) |
| 230 | * - start - virtual start address of region |
| 231 | * - size - size in question |
| 232 | */ |
Swathi Sridhar | 4008eb4 | 2018-07-17 15:34:46 -0700 | [diff] [blame] | 233 | ENTRY(__dma_inv_area) |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 234 | add x1, x1, x0 |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 235 | dcache_line_size x2, x3 |
| 236 | sub x3, x2, #1 |
Catalin Marinas | ebf81a9 | 2014-04-01 18:32:55 +0100 | [diff] [blame] | 237 | tst x1, x3 // end cache line aligned? |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 238 | bic x1, x1, x3 |
Catalin Marinas | ebf81a9 | 2014-04-01 18:32:55 +0100 | [diff] [blame] | 239 | b.eq 1f |
| 240 | dc civac, x1 // clean & invalidate D / U line |
| 241 | 1: tst x0, x3 // start cache line aligned? |
| 242 | bic x0, x0, x3 |
| 243 | b.eq 2f |
| 244 | dc civac, x0 // clean & invalidate D / U line |
| 245 | b 3f |
| 246 | 2: dc ivac, x0 // invalidate D / U line |
| 247 | 3: add x0, x0, x2 |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 248 | cmp x0, x1 |
Catalin Marinas | ebf81a9 | 2014-04-01 18:32:55 +0100 | [diff] [blame] | 249 | b.lo 2b |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 250 | dsb sy |
| 251 | ret |
Robin Murphy | d46befe | 2017-07-25 11:55:39 +0100 | [diff] [blame] | 252 | ENDPIPROC(__inval_dcache_area) |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 253 | ENDPROC(__dma_inv_area) |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 254 | |
| 255 | /* |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 256 | * __clean_dcache_area_poc(kaddr, size) |
| 257 | * |
| 258 | * Ensure that any D-cache lines for the interval [kaddr, kaddr+size) |
| 259 | * are cleaned to the PoC. |
| 260 | * |
| 261 | * - kaddr - kernel address |
| 262 | * - size - size in question |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 263 | */ |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 264 | ENTRY(__clean_dcache_area_poc) |
| 265 | /* FALLTHROUGH */ |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 266 | |
| 267 | /* |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 268 | * __dma_clean_area(start, size) |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 269 | * - start - virtual start address of region |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 270 | * - size - size in question |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 271 | */ |
Swathi Sridhar | 4008eb4 | 2018-07-17 15:34:46 -0700 | [diff] [blame] | 272 | ENTRY(__dma_clean_area) |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 273 | dcache_by_line_op cvac, sy, x0, x1, x2, x3 |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 274 | ret |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 275 | ENDPIPROC(__clean_dcache_area_poc) |
| 276 | ENDPROC(__dma_clean_area) |
| 277 | |
| 278 | /* |
Robin Murphy | d50e071 | 2017-07-25 11:55:42 +0100 | [diff] [blame] | 279 | * __clean_dcache_area_pop(kaddr, size) |
| 280 | * |
| 281 | * Ensure that any D-cache lines for the interval [kaddr, kaddr+size) |
| 282 | * are cleaned to the PoP. |
| 283 | * |
| 284 | * - kaddr - kernel address |
| 285 | * - size - size in question |
| 286 | */ |
| 287 | ENTRY(__clean_dcache_area_pop) |
Will Deacon | dfbf8c9 | 2018-12-10 13:39:48 +0000 | [diff] [blame] | 288 | alternative_if_not ARM64_HAS_DCPOP |
| 289 | b __clean_dcache_area_poc |
| 290 | alternative_else_nop_endif |
Robin Murphy | d50e071 | 2017-07-25 11:55:42 +0100 | [diff] [blame] | 291 | dcache_by_line_op cvap, sy, x0, x1, x2, x3 |
| 292 | ret |
| 293 | ENDPIPROC(__clean_dcache_area_pop) |
| 294 | |
| 295 | /* |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 296 | * __dma_flush_area(start, size) |
| 297 | * |
| 298 | * clean & invalidate D / U line |
| 299 | * |
| 300 | * - start - virtual start address of region |
| 301 | * - size - size in question |
| 302 | */ |
| 303 | ENTRY(__dma_flush_area) |
| 304 | dcache_by_line_op civac, sy, x0, x1, x2, x3 |
| 305 | ret |
| 306 | ENDPIPROC(__dma_flush_area) |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 307 | |
| 308 | /* |
| 309 | * __dma_map_area(start, size, dir) |
| 310 | * - start - kernel virtual start address |
| 311 | * - size - size of region |
| 312 | * - dir - DMA direction |
| 313 | */ |
| 314 | ENTRY(__dma_map_area) |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 315 | cmp w2, #DMA_FROM_DEVICE |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 316 | b.eq __dma_inv_area |
| 317 | b __dma_clean_area |
Ard Biesheuvel | 2079184 | 2015-10-08 20:02:03 +0100 | [diff] [blame] | 318 | ENDPIPROC(__dma_map_area) |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 319 | |
| 320 | /* |
| 321 | * __dma_unmap_area(start, size, dir) |
| 322 | * - start - kernel virtual start address |
| 323 | * - size - size of region |
| 324 | * - dir - DMA direction |
| 325 | */ |
| 326 | ENTRY(__dma_unmap_area) |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 327 | cmp w2, #DMA_TO_DEVICE |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 328 | b.ne __dma_inv_area |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 329 | ret |
Ard Biesheuvel | 2079184 | 2015-10-08 20:02:03 +0100 | [diff] [blame] | 330 | ENDPIPROC(__dma_unmap_area) |