blob: 000fb44654147b32b3109be398e12a0a51b2854a [file] [log] [blame]
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +00001/*
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 Murzina2d25a52014-12-01 10:53:08 +000020#include <linux/errno.h>
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +000021#include <linux/linkage.h>
22#include <linux/init.h>
23#include <asm/assembler.h>
Andre Przywara301bcfa2014-11-14 15:54:10 +000024#include <asm/cpufeature.h>
Marc Zyngier8d883b22015-06-01 10:47:41 +010025#include <asm/alternative.h>
Al Virob4b86642016-12-26 04:10:19 -050026#include <asm/asm-uaccess.h>
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +000027
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +000028/*
Rohit Vaswani208984f2016-01-04 14:08:32 -080029 * __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
42loop1:
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
60loop2:
61 mov x9, x4 // create working copy of max way size
62loop3:
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
72skip:
73 add x10, x10, #2 // increment cache number
74 cmp x3, x10
75 b.gt loop1
76finished:
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
82ENDPROC(__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 */
93ENTRY(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
99ENDPROC(flush_cache_all)
100
101/*
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +0000102 * 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 Deacon3b8c9f12018-06-11 14:22:09 +0100111ENTRY(__flush_icache_range)
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +0000112 /* 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 */
124ENTRY(__flush_cache_user_range)
Christoffer Dall448fadc2018-01-09 11:51:58 +0100125 uaccess_ttbr0_enable x2, x3, x4
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -0600126alternative_if ARM64_HAS_CACHE_IDC
127 dsb ishst
128 b 7f
129alternative_else_nop_endif
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +0000130 dcache_line_size x2, x3
131 sub x3, x2, #1
132 bic x4, x0, x3
1331:
Andre Przywara290622e2016-06-28 18:07:28 +0100134user_alt 9f, "dc cvau, x4", "dc civac, x4", ARM64_WORKAROUND_CLEAN_CACHE
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +0000135 add x4, x4, x2
136 cmp x4, x1
137 b.lo 1b
Will Deacondc60b772014-05-02 16:24:15 +0100138 dsb ish
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +0000139
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001407:
141alternative_if ARM64_HAS_CACHE_DIC
142 isb
143 b 8f
144alternative_else_nop_endif
Marc Zyngier4fee9472017-10-23 17:11:16 +0100145 invalidate_icache_by_line x0, x1, x2, x3, 9f
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -06001468: mov x0, #0
Catalin Marinas39bc88e2016-09-02 14:54:03 +01001471:
Christoffer Dall0482b502018-01-17 12:35:27 +0100148 uaccess_ttbr0_disable x1, x2
Vladimir Murzina2d25a52014-12-01 10:53:08 +0000149 ret
1509:
151 mov x0, #-EFAULT
Catalin Marinas39bc88e2016-09-02 14:54:03 +0100152 b 1b
Will Deacon3b8c9f12018-06-11 14:22:09 +0100153ENDPROC(__flush_icache_range)
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +0000154ENDPROC(__flush_cache_user_range)
155
156/*
Marc Zyngier4fee9472017-10-23 17:11:16 +0100157 * 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 */
164ENTRY(invalidate_icache_range)
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -0600165alternative_if ARM64_HAS_CACHE_DIC
166 mov x0, xzr
167 isb
168 ret
169alternative_else_nop_endif
170
Christoffer Dall448fadc2018-01-09 11:51:58 +0100171 uaccess_ttbr0_enable x2, x3, x4
Marc Zyngier4fee9472017-10-23 17:11:16 +0100172
173 invalidate_icache_by_line x0, x1, x2, x3, 2f
174 mov x0, xzr
1751:
Christoffer Dall0482b502018-01-17 12:35:27 +0100176 uaccess_ttbr0_disable x1, x2
Marc Zyngier4fee9472017-10-23 17:11:16 +0100177 ret
1782:
179 mov x0, #-EFAULT
180 b 1b
181ENDPROC(invalidate_icache_range)
182
183/*
Jingoo Han03324e62014-01-21 01:17:47 +0000184 * __flush_dcache_area(kaddr, size)
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +0000185 *
Ashok Kumar0a287142015-12-17 01:38:32 -0800186 * Ensure that any D-cache lines for the interval [kaddr, kaddr+size)
187 * are cleaned and invalidated to the PoC.
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +0000188 *
189 * - kaddr - kernel address
190 * - size - size in question
191 */
192ENTRY(__flush_dcache_area)
Ashok Kumar0a287142015-12-17 01:38:32 -0800193 dcache_by_line_op civac, sy, x0, x1, x2, x3
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +0000194 ret
Ard Biesheuvel20791842015-10-08 20:02:03 +0100195ENDPIPROC(__flush_dcache_area)
Catalin Marinas73635902013-05-21 17:35:19 +0100196
197/*
Ashok Kumar0a287142015-12-17 01:38:32 -0800198 * __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 */
206ENTRY(__clean_dcache_area_pou)
Shanker Donthineni6ae4b6e2018-03-07 09:00:08 -0600207alternative_if ARM64_HAS_CACHE_IDC
208 dsb ishst
209 ret
210alternative_else_nop_endif
Ashok Kumar0a287142015-12-17 01:38:32 -0800211 dcache_by_line_op cvau, ish, x0, x1, x2, x3
212 ret
213ENDPROC(__clean_dcache_area_pou)
214
215/*
Robin Murphyd46befe2017-07-25 11:55:39 +0100216 * __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 */
225ENTRY(__inval_dcache_area)
226 /* FALLTHROUGH */
227
228/*
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900229 * __dma_inv_area(start, size)
230 * - start - virtual start address of region
231 * - size - size in question
232 */
Swathi Sridhar4008eb42018-07-17 15:34:46 -0700233ENTRY(__dma_inv_area)
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900234 add x1, x1, x0
Catalin Marinas73635902013-05-21 17:35:19 +0100235 dcache_line_size x2, x3
236 sub x3, x2, #1
Catalin Marinasebf81a92014-04-01 18:32:55 +0100237 tst x1, x3 // end cache line aligned?
Catalin Marinas73635902013-05-21 17:35:19 +0100238 bic x1, x1, x3
Catalin Marinasebf81a92014-04-01 18:32:55 +0100239 b.eq 1f
240 dc civac, x1 // clean & invalidate D / U line
2411: 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
2462: dc ivac, x0 // invalidate D / U line
2473: add x0, x0, x2
Catalin Marinas73635902013-05-21 17:35:19 +0100248 cmp x0, x1
Catalin Marinasebf81a92014-04-01 18:32:55 +0100249 b.lo 2b
Catalin Marinas73635902013-05-21 17:35:19 +0100250 dsb sy
251 ret
Robin Murphyd46befe2017-07-25 11:55:39 +0100252ENDPIPROC(__inval_dcache_area)
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900253ENDPROC(__dma_inv_area)
Catalin Marinas73635902013-05-21 17:35:19 +0100254
255/*
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900256 * __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 Marinas73635902013-05-21 17:35:19 +0100263 */
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900264ENTRY(__clean_dcache_area_poc)
265 /* FALLTHROUGH */
Catalin Marinas73635902013-05-21 17:35:19 +0100266
267/*
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900268 * __dma_clean_area(start, size)
Catalin Marinas73635902013-05-21 17:35:19 +0100269 * - start - virtual start address of region
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900270 * - size - size in question
Catalin Marinas73635902013-05-21 17:35:19 +0100271 */
Swathi Sridhar4008eb42018-07-17 15:34:46 -0700272ENTRY(__dma_clean_area)
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900273 dcache_by_line_op cvac, sy, x0, x1, x2, x3
Catalin Marinas73635902013-05-21 17:35:19 +0100274 ret
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900275ENDPIPROC(__clean_dcache_area_poc)
276ENDPROC(__dma_clean_area)
277
278/*
Robin Murphyd50e0712017-07-25 11:55:42 +0100279 * __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 */
287ENTRY(__clean_dcache_area_pop)
Will Deacondfbf8c92018-12-10 13:39:48 +0000288 alternative_if_not ARM64_HAS_DCPOP
289 b __clean_dcache_area_poc
290 alternative_else_nop_endif
Robin Murphyd50e0712017-07-25 11:55:42 +0100291 dcache_by_line_op cvap, sy, x0, x1, x2, x3
292 ret
293ENDPIPROC(__clean_dcache_area_pop)
294
295/*
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900296 * __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 */
303ENTRY(__dma_flush_area)
304 dcache_by_line_op civac, sy, x0, x1, x2, x3
305 ret
306ENDPIPROC(__dma_flush_area)
Catalin Marinas73635902013-05-21 17:35:19 +0100307
308/*
309 * __dma_map_area(start, size, dir)
310 * - start - kernel virtual start address
311 * - size - size of region
312 * - dir - DMA direction
313 */
314ENTRY(__dma_map_area)
Catalin Marinas73635902013-05-21 17:35:19 +0100315 cmp w2, #DMA_FROM_DEVICE
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900316 b.eq __dma_inv_area
317 b __dma_clean_area
Ard Biesheuvel20791842015-10-08 20:02:03 +0100318ENDPIPROC(__dma_map_area)
Catalin Marinas73635902013-05-21 17:35:19 +0100319
320/*
321 * __dma_unmap_area(start, size, dir)
322 * - start - kernel virtual start address
323 * - size - size of region
324 * - dir - DMA direction
325 */
326ENTRY(__dma_unmap_area)
Catalin Marinas73635902013-05-21 17:35:19 +0100327 cmp w2, #DMA_TO_DEVICE
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900328 b.ne __dma_inv_area
Catalin Marinas73635902013-05-21 17:35:19 +0100329 ret
Ard Biesheuvel20791842015-10-08 20:02:03 +0100330ENDPIPROC(__dma_unmap_area)