blob: ed47fbbb4b05783e15174885d5dc90f2fb43cf98 [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/*
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +000029 * 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 */
38ENTRY(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 */
51ENTRY(__flush_cache_user_range)
Catalin Marinas39bc88e2016-09-02 14:54:03 +010052 uaccess_ttbr0_enable x2, x3
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +000053 dcache_line_size x2, x3
54 sub x3, x2, #1
55 bic x4, x0, x3
561:
Andre Przywara290622e2016-06-28 18:07:28 +010057user_alt 9f, "dc cvau, x4", "dc civac, x4", ARM64_WORKAROUND_CLEAN_CACHE
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +000058 add x4, x4, x2
59 cmp x4, x1
60 b.lo 1b
Will Deacondc60b772014-05-02 16:24:15 +010061 dsb ish
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +000062
63 icache_line_size x2, x3
64 sub x3, x2, #1
65 bic x4, x0, x3
661:
67USER(9f, ic ivau, x4 ) // invalidate I line PoU
68 add x4, x4, x2
69 cmp x4, x1
70 b.lo 1b
Will Deacondc60b772014-05-02 16:24:15 +010071 dsb ish
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +000072 isb
Vladimir Murzina2d25a52014-12-01 10:53:08 +000073 mov x0, #0
Catalin Marinas39bc88e2016-09-02 14:54:03 +0100741:
75 uaccess_ttbr0_disable x1
Vladimir Murzina2d25a52014-12-01 10:53:08 +000076 ret
779:
78 mov x0, #-EFAULT
Catalin Marinas39bc88e2016-09-02 14:54:03 +010079 b 1b
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +000080ENDPROC(flush_icache_range)
81ENDPROC(__flush_cache_user_range)
82
83/*
Jingoo Han03324e62014-01-21 01:17:47 +000084 * __flush_dcache_area(kaddr, size)
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +000085 *
Ashok Kumar0a287142015-12-17 01:38:32 -080086 * Ensure that any D-cache lines for the interval [kaddr, kaddr+size)
87 * are cleaned and invalidated to the PoC.
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +000088 *
89 * - kaddr - kernel address
90 * - size - size in question
91 */
92ENTRY(__flush_dcache_area)
Ashok Kumar0a287142015-12-17 01:38:32 -080093 dcache_by_line_op civac, sy, x0, x1, x2, x3
Catalin Marinasf1a0c4a2012-03-05 11:49:28 +000094 ret
Ard Biesheuvel20791842015-10-08 20:02:03 +010095ENDPIPROC(__flush_dcache_area)
Catalin Marinas73635902013-05-21 17:35:19 +010096
97/*
Ashok Kumar0a287142015-12-17 01:38:32 -080098 * __clean_dcache_area_pou(kaddr, size)
99 *
100 * Ensure that any D-cache lines for the interval [kaddr, kaddr+size)
101 * are cleaned to the PoU.
102 *
103 * - kaddr - kernel address
104 * - size - size in question
105 */
106ENTRY(__clean_dcache_area_pou)
107 dcache_by_line_op cvau, ish, x0, x1, x2, x3
108 ret
109ENDPROC(__clean_dcache_area_pou)
110
111/*
Robin Murphyd46befe2017-07-25 11:55:39 +0100112 * __inval_dcache_area(kaddr, size)
113 *
114 * Ensure that any D-cache lines for the interval [kaddr, kaddr+size)
115 * are invalidated. Any partial lines at the ends of the interval are
116 * also cleaned to PoC to prevent data loss.
117 *
118 * - kaddr - kernel address
119 * - size - size in question
120 */
121ENTRY(__inval_dcache_area)
122 /* FALLTHROUGH */
123
124/*
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900125 * __dma_inv_area(start, size)
126 * - start - virtual start address of region
127 * - size - size in question
128 */
129__dma_inv_area:
130 add x1, x1, x0
Catalin Marinas73635902013-05-21 17:35:19 +0100131 dcache_line_size x2, x3
132 sub x3, x2, #1
Catalin Marinasebf81a92014-04-01 18:32:55 +0100133 tst x1, x3 // end cache line aligned?
Catalin Marinas73635902013-05-21 17:35:19 +0100134 bic x1, x1, x3
Catalin Marinasebf81a92014-04-01 18:32:55 +0100135 b.eq 1f
136 dc civac, x1 // clean & invalidate D / U line
1371: tst x0, x3 // start cache line aligned?
138 bic x0, x0, x3
139 b.eq 2f
140 dc civac, x0 // clean & invalidate D / U line
141 b 3f
1422: dc ivac, x0 // invalidate D / U line
1433: add x0, x0, x2
Catalin Marinas73635902013-05-21 17:35:19 +0100144 cmp x0, x1
Catalin Marinasebf81a92014-04-01 18:32:55 +0100145 b.lo 2b
Catalin Marinas73635902013-05-21 17:35:19 +0100146 dsb sy
147 ret
Robin Murphyd46befe2017-07-25 11:55:39 +0100148ENDPIPROC(__inval_dcache_area)
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900149ENDPROC(__dma_inv_area)
Catalin Marinas73635902013-05-21 17:35:19 +0100150
151/*
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900152 * __clean_dcache_area_poc(kaddr, size)
153 *
154 * Ensure that any D-cache lines for the interval [kaddr, kaddr+size)
155 * are cleaned to the PoC.
156 *
157 * - kaddr - kernel address
158 * - size - size in question
Catalin Marinas73635902013-05-21 17:35:19 +0100159 */
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900160ENTRY(__clean_dcache_area_poc)
161 /* FALLTHROUGH */
Catalin Marinas73635902013-05-21 17:35:19 +0100162
163/*
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900164 * __dma_clean_area(start, size)
Catalin Marinas73635902013-05-21 17:35:19 +0100165 * - start - virtual start address of region
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900166 * - size - size in question
Catalin Marinas73635902013-05-21 17:35:19 +0100167 */
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900168__dma_clean_area:
169 dcache_by_line_op cvac, sy, x0, x1, x2, x3
Catalin Marinas73635902013-05-21 17:35:19 +0100170 ret
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900171ENDPIPROC(__clean_dcache_area_poc)
172ENDPROC(__dma_clean_area)
173
174/*
175 * __dma_flush_area(start, size)
176 *
177 * clean & invalidate D / U line
178 *
179 * - start - virtual start address of region
180 * - size - size in question
181 */
182ENTRY(__dma_flush_area)
183 dcache_by_line_op civac, sy, x0, x1, x2, x3
184 ret
185ENDPIPROC(__dma_flush_area)
Catalin Marinas73635902013-05-21 17:35:19 +0100186
187/*
188 * __dma_map_area(start, size, dir)
189 * - start - kernel virtual start address
190 * - size - size of region
191 * - dir - DMA direction
192 */
193ENTRY(__dma_map_area)
Catalin Marinas73635902013-05-21 17:35:19 +0100194 cmp w2, #DMA_FROM_DEVICE
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900195 b.eq __dma_inv_area
196 b __dma_clean_area
Ard Biesheuvel20791842015-10-08 20:02:03 +0100197ENDPIPROC(__dma_map_area)
Catalin Marinas73635902013-05-21 17:35:19 +0100198
199/*
200 * __dma_unmap_area(start, size, dir)
201 * - start - kernel virtual start address
202 * - size - size of region
203 * - dir - DMA direction
204 */
205ENTRY(__dma_unmap_area)
Catalin Marinas73635902013-05-21 17:35:19 +0100206 cmp w2, #DMA_TO_DEVICE
Kwangwoo Leed34fdb72016-08-02 09:50:50 +0900207 b.ne __dma_inv_area
Catalin Marinas73635902013-05-21 17:35:19 +0100208 ret
Ard Biesheuvel20791842015-10-08 20:02:03 +0100209ENDPIPROC(__dma_unmap_area)