Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Cache maintenance |
| 4 | * |
| 5 | * Copyright (C) 2001 Deep Blue Solutions Ltd. |
| 6 | * Copyright (C) 2012 ARM Ltd. |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Vladimir Murzin | a2d25a5 | 2014-12-01 10:53:08 +0000 | [diff] [blame] | 9 | #include <linux/errno.h> |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 10 | #include <linux/linkage.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <asm/assembler.h> |
Andre Przywara | 301bcfa | 2014-11-14 15:54:10 +0000 | [diff] [blame] | 13 | #include <asm/cpufeature.h> |
Marc Zyngier | 8d883b2 | 2015-06-01 10:47:41 +0100 | [diff] [blame] | 14 | #include <asm/alternative.h> |
Al Viro | b4b8664 | 2016-12-26 04:10:19 -0500 | [diff] [blame] | 15 | #include <asm/asm-uaccess.h> |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 16 | |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 17 | /* |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 18 | * flush_icache_range(start,end) |
| 19 | * |
| 20 | * Ensure that the I and D caches are coherent within specified region. |
| 21 | * This is typically used when code has been written to a memory region, |
| 22 | * and will be executed. |
| 23 | * |
| 24 | * - start - virtual start address of region |
| 25 | * - end - virtual end address of region |
| 26 | */ |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 27 | SYM_FUNC_START(__flush_icache_range) |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 28 | /* FALLTHROUGH */ |
| 29 | |
| 30 | /* |
| 31 | * __flush_cache_user_range(start,end) |
| 32 | * |
| 33 | * Ensure that the I and D caches are coherent within specified region. |
| 34 | * This is typically used when code has been written to a memory region, |
| 35 | * and will be executed. |
| 36 | * |
| 37 | * - start - virtual start address of region |
| 38 | * - end - virtual end address of region |
| 39 | */ |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 40 | SYM_FUNC_START(__flush_cache_user_range) |
Christoffer Dall | 448fadc | 2018-01-09 11:51:58 +0100 | [diff] [blame] | 41 | uaccess_ttbr0_enable x2, x3, x4 |
Shanker Donthineni | 6ae4b6e | 2018-03-07 09:00:08 -0600 | [diff] [blame] | 42 | alternative_if ARM64_HAS_CACHE_IDC |
| 43 | dsb ishst |
| 44 | b 7f |
| 45 | alternative_else_nop_endif |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 46 | dcache_line_size x2, x3 |
| 47 | sub x3, x2, #1 |
| 48 | bic x4, x0, x3 |
| 49 | 1: |
Andre Przywara | 290622e | 2016-06-28 18:07:28 +0100 | [diff] [blame] | 50 | 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] | 51 | add x4, x4, x2 |
| 52 | cmp x4, x1 |
| 53 | b.lo 1b |
Will Deacon | dc60b77 | 2014-05-02 16:24:15 +0100 | [diff] [blame] | 54 | dsb ish |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 55 | |
Shanker Donthineni | 6ae4b6e | 2018-03-07 09:00:08 -0600 | [diff] [blame] | 56 | 7: |
| 57 | alternative_if ARM64_HAS_CACHE_DIC |
| 58 | isb |
| 59 | b 8f |
| 60 | alternative_else_nop_endif |
Marc Zyngier | 4fee947 | 2017-10-23 17:11:16 +0100 | [diff] [blame] | 61 | invalidate_icache_by_line x0, x1, x2, x3, 9f |
Shanker Donthineni | 6ae4b6e | 2018-03-07 09:00:08 -0600 | [diff] [blame] | 62 | 8: mov x0, #0 |
Catalin Marinas | 39bc88e | 2016-09-02 14:54:03 +0100 | [diff] [blame] | 63 | 1: |
Christoffer Dall | 0482b50 | 2018-01-17 12:35:27 +0100 | [diff] [blame] | 64 | uaccess_ttbr0_disable x1, x2 |
Vladimir Murzin | a2d25a5 | 2014-12-01 10:53:08 +0000 | [diff] [blame] | 65 | ret |
| 66 | 9: |
| 67 | mov x0, #-EFAULT |
Catalin Marinas | 39bc88e | 2016-09-02 14:54:03 +0100 | [diff] [blame] | 68 | b 1b |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 69 | SYM_FUNC_END(__flush_icache_range) |
| 70 | SYM_FUNC_END(__flush_cache_user_range) |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 71 | |
| 72 | /* |
Marc Zyngier | 4fee947 | 2017-10-23 17:11:16 +0100 | [diff] [blame] | 73 | * invalidate_icache_range(start,end) |
| 74 | * |
| 75 | * Ensure that the I cache is invalid within specified region. |
| 76 | * |
| 77 | * - start - virtual start address of region |
| 78 | * - end - virtual end address of region |
| 79 | */ |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 80 | SYM_FUNC_START(invalidate_icache_range) |
Shanker Donthineni | 6ae4b6e | 2018-03-07 09:00:08 -0600 | [diff] [blame] | 81 | alternative_if ARM64_HAS_CACHE_DIC |
| 82 | mov x0, xzr |
| 83 | isb |
| 84 | ret |
| 85 | alternative_else_nop_endif |
| 86 | |
Christoffer Dall | 448fadc | 2018-01-09 11:51:58 +0100 | [diff] [blame] | 87 | uaccess_ttbr0_enable x2, x3, x4 |
Marc Zyngier | 4fee947 | 2017-10-23 17:11:16 +0100 | [diff] [blame] | 88 | |
| 89 | invalidate_icache_by_line x0, x1, x2, x3, 2f |
| 90 | mov x0, xzr |
| 91 | 1: |
Christoffer Dall | 0482b50 | 2018-01-17 12:35:27 +0100 | [diff] [blame] | 92 | uaccess_ttbr0_disable x1, x2 |
Marc Zyngier | 4fee947 | 2017-10-23 17:11:16 +0100 | [diff] [blame] | 93 | ret |
| 94 | 2: |
| 95 | mov x0, #-EFAULT |
| 96 | b 1b |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 97 | SYM_FUNC_END(invalidate_icache_range) |
Marc Zyngier | 4fee947 | 2017-10-23 17:11:16 +0100 | [diff] [blame] | 98 | |
| 99 | /* |
Jingoo Han | 03324e6 | 2014-01-21 01:17:47 +0000 | [diff] [blame] | 100 | * __flush_dcache_area(kaddr, size) |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 101 | * |
Ashok Kumar | 0a28714 | 2015-12-17 01:38:32 -0800 | [diff] [blame] | 102 | * Ensure that any D-cache lines for the interval [kaddr, kaddr+size) |
| 103 | * are cleaned and invalidated to the PoC. |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 104 | * |
| 105 | * - kaddr - kernel address |
| 106 | * - size - size in question |
| 107 | */ |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 108 | SYM_FUNC_START_PI(__flush_dcache_area) |
Ashok Kumar | 0a28714 | 2015-12-17 01:38:32 -0800 | [diff] [blame] | 109 | dcache_by_line_op civac, sy, x0, x1, x2, x3 |
Catalin Marinas | f1a0c4a | 2012-03-05 11:49:28 +0000 | [diff] [blame] | 110 | ret |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 111 | SYM_FUNC_END_PI(__flush_dcache_area) |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 112 | |
| 113 | /* |
Ashok Kumar | 0a28714 | 2015-12-17 01:38:32 -0800 | [diff] [blame] | 114 | * __clean_dcache_area_pou(kaddr, size) |
| 115 | * |
| 116 | * Ensure that any D-cache lines for the interval [kaddr, kaddr+size) |
| 117 | * are cleaned to the PoU. |
| 118 | * |
| 119 | * - kaddr - kernel address |
| 120 | * - size - size in question |
| 121 | */ |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 122 | SYM_FUNC_START(__clean_dcache_area_pou) |
Shanker Donthineni | 6ae4b6e | 2018-03-07 09:00:08 -0600 | [diff] [blame] | 123 | alternative_if ARM64_HAS_CACHE_IDC |
| 124 | dsb ishst |
| 125 | ret |
| 126 | alternative_else_nop_endif |
Ashok Kumar | 0a28714 | 2015-12-17 01:38:32 -0800 | [diff] [blame] | 127 | dcache_by_line_op cvau, ish, x0, x1, x2, x3 |
| 128 | ret |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 129 | SYM_FUNC_END(__clean_dcache_area_pou) |
Ashok Kumar | 0a28714 | 2015-12-17 01:38:32 -0800 | [diff] [blame] | 130 | |
| 131 | /* |
Robin Murphy | d46befe | 2017-07-25 11:55:39 +0100 | [diff] [blame] | 132 | * __inval_dcache_area(kaddr, size) |
| 133 | * |
| 134 | * Ensure that any D-cache lines for the interval [kaddr, kaddr+size) |
| 135 | * are invalidated. Any partial lines at the ends of the interval are |
| 136 | * also cleaned to PoC to prevent data loss. |
| 137 | * |
| 138 | * - kaddr - kernel address |
| 139 | * - size - size in question |
| 140 | */ |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 141 | SYM_FUNC_START_LOCAL(__dma_inv_area) |
| 142 | SYM_FUNC_START_PI(__inval_dcache_area) |
Robin Murphy | d46befe | 2017-07-25 11:55:39 +0100 | [diff] [blame] | 143 | /* FALLTHROUGH */ |
| 144 | |
| 145 | /* |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 146 | * __dma_inv_area(start, size) |
| 147 | * - start - virtual start address of region |
| 148 | * - size - size in question |
| 149 | */ |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 150 | add x1, x1, x0 |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 151 | dcache_line_size x2, x3 |
| 152 | sub x3, x2, #1 |
Catalin Marinas | ebf81a9 | 2014-04-01 18:32:55 +0100 | [diff] [blame] | 153 | tst x1, x3 // end cache line aligned? |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 154 | bic x1, x1, x3 |
Catalin Marinas | ebf81a9 | 2014-04-01 18:32:55 +0100 | [diff] [blame] | 155 | b.eq 1f |
| 156 | dc civac, x1 // clean & invalidate D / U line |
| 157 | 1: tst x0, x3 // start cache line aligned? |
| 158 | bic x0, x0, x3 |
| 159 | b.eq 2f |
| 160 | dc civac, x0 // clean & invalidate D / U line |
| 161 | b 3f |
| 162 | 2: dc ivac, x0 // invalidate D / U line |
| 163 | 3: add x0, x0, x2 |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 164 | cmp x0, x1 |
Catalin Marinas | ebf81a9 | 2014-04-01 18:32:55 +0100 | [diff] [blame] | 165 | b.lo 2b |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 166 | dsb sy |
| 167 | ret |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 168 | SYM_FUNC_END_PI(__inval_dcache_area) |
| 169 | SYM_FUNC_END(__dma_inv_area) |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 170 | |
| 171 | /* |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 172 | * __clean_dcache_area_poc(kaddr, size) |
| 173 | * |
| 174 | * Ensure that any D-cache lines for the interval [kaddr, kaddr+size) |
| 175 | * are cleaned to the PoC. |
| 176 | * |
| 177 | * - kaddr - kernel address |
| 178 | * - size - size in question |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 179 | */ |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 180 | SYM_FUNC_START_LOCAL(__dma_clean_area) |
| 181 | SYM_FUNC_START_PI(__clean_dcache_area_poc) |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 182 | /* FALLTHROUGH */ |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 183 | |
| 184 | /* |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 185 | * __dma_clean_area(start, size) |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 186 | * - start - virtual start address of region |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 187 | * - size - size in question |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 188 | */ |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 189 | dcache_by_line_op cvac, sy, x0, x1, x2, x3 |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 190 | ret |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 191 | SYM_FUNC_END_PI(__clean_dcache_area_poc) |
| 192 | SYM_FUNC_END(__dma_clean_area) |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 193 | |
| 194 | /* |
Robin Murphy | d50e071 | 2017-07-25 11:55:42 +0100 | [diff] [blame] | 195 | * __clean_dcache_area_pop(kaddr, size) |
| 196 | * |
| 197 | * Ensure that any D-cache lines for the interval [kaddr, kaddr+size) |
| 198 | * are cleaned to the PoP. |
| 199 | * |
| 200 | * - kaddr - kernel address |
| 201 | * - size - size in question |
| 202 | */ |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 203 | SYM_FUNC_START_PI(__clean_dcache_area_pop) |
Will Deacon | 33309ec | 2018-12-10 13:39:48 +0000 | [diff] [blame] | 204 | alternative_if_not ARM64_HAS_DCPOP |
| 205 | b __clean_dcache_area_poc |
| 206 | alternative_else_nop_endif |
Robin Murphy | d50e071 | 2017-07-25 11:55:42 +0100 | [diff] [blame] | 207 | dcache_by_line_op cvap, sy, x0, x1, x2, x3 |
| 208 | ret |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 209 | SYM_FUNC_END_PI(__clean_dcache_area_pop) |
Robin Murphy | d50e071 | 2017-07-25 11:55:42 +0100 | [diff] [blame] | 210 | |
| 211 | /* |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 212 | * __dma_flush_area(start, size) |
| 213 | * |
| 214 | * clean & invalidate D / U line |
| 215 | * |
| 216 | * - start - virtual start address of region |
| 217 | * - size - size in question |
| 218 | */ |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 219 | SYM_FUNC_START_PI(__dma_flush_area) |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 220 | dcache_by_line_op civac, sy, x0, x1, x2, x3 |
| 221 | ret |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 222 | SYM_FUNC_END_PI(__dma_flush_area) |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 223 | |
| 224 | /* |
| 225 | * __dma_map_area(start, size, dir) |
| 226 | * - start - kernel virtual start address |
| 227 | * - size - size of region |
| 228 | * - dir - DMA direction |
| 229 | */ |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 230 | SYM_FUNC_START_PI(__dma_map_area) |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 231 | cmp w2, #DMA_FROM_DEVICE |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 232 | b.eq __dma_inv_area |
| 233 | b __dma_clean_area |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 234 | SYM_FUNC_END_PI(__dma_map_area) |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 235 | |
| 236 | /* |
| 237 | * __dma_unmap_area(start, size, dir) |
| 238 | * - start - kernel virtual start address |
| 239 | * - size - size of region |
| 240 | * - dir - DMA direction |
| 241 | */ |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 242 | SYM_FUNC_START_PI(__dma_unmap_area) |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 243 | cmp w2, #DMA_TO_DEVICE |
Kwangwoo Lee | d34fdb7 | 2016-08-02 09:50:50 +0900 | [diff] [blame] | 244 | b.ne __dma_inv_area |
Catalin Marinas | 7363590 | 2013-05-21 17:35:19 +0100 | [diff] [blame] | 245 | ret |
Mark Brown | f4659254a | 2020-01-06 19:58:18 +0000 | [diff] [blame] | 246 | SYM_FUNC_END_PI(__dma_unmap_area) |