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