blob: 7539ec27506585f3dd42b04f785c5edba39d1614 [file] [log] [blame]
Catalin Marinasbbe88882007-05-08 22:27:46 +01001/*
2 * linux/arch/arm/mm/cache-v7.S
3 *
4 * Copyright (C) 2001 Deep Blue Solutions Ltd.
5 * Copyright (C) 2005 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 is the "shell" of the ARMv7 processor support.
12 */
13#include <linux/linkage.h>
14#include <linux/init.h>
15#include <asm/assembler.h>
Will Deaconc5102f52012-04-27 13:08:53 +010016#include <asm/errno.h>
Catalin Marinas32cfb1b2009-10-06 17:57:09 +010017#include <asm/unwind.h>
Catalin Marinasbbe88882007-05-08 22:27:46 +010018
19#include "proc-macros.S"
20
21/*
Tony Lindgren81d11952010-09-21 17:16:40 +010022 * v7_flush_icache_all()
23 *
24 * Flush the whole I-cache.
25 *
26 * Registers:
27 * r0 - set to 0
28 */
29ENTRY(v7_flush_icache_all)
30 mov r0, #0
31 ALT_SMP(mcr p15, 0, r0, c7, c1, 0) @ invalidate I-cache inner shareable
32 ALT_UP(mcr p15, 0, r0, c7, c5, 0) @ I+BTB cache invalidate
33 mov pc, lr
34ENDPROC(v7_flush_icache_all)
35
Lorenzo Pieralisi031bd872012-09-06 18:35:13 +053036 /*
37 * v7_flush_dcache_louis()
38 *
39 * Flush the D-cache up to the Level of Unification Inner Shareable
40 *
41 * Corrupted registers: r0-r7, r9-r11 (r6 only in Thumb mode)
42 */
43
44ENTRY(v7_flush_dcache_louis)
45 dmb @ ensure ordering with previous memory accesses
46 mrc p15, 1, r0, c0, c0, 1 @ read clidr, r0 = clidr
Will Deacond056a692012-12-19 15:01:08 +010047 ALT_SMP(ands r3, r0, #(7 << 21)) @ extract LoUIS from clidr
48 ALT_UP(ands r3, r0, #(7 << 27)) @ extract LoUU from clidr
49 ALT_SMP(mov r3, r3, lsr #20) @ r3 = LoUIS * 2
50 ALT_UP(mov r3, r3, lsr #26) @ r3 = LoUU * 2
Lorenzo Pieralisi031bd872012-09-06 18:35:13 +053051 moveq pc, lr @ return if level == 0
52 mov r10, #0 @ r10 (starting level) = 0
Lorenzo Pieralisi3287be82012-09-18 16:29:44 +010053 b flush_levels @ start flushing cache levels
Lorenzo Pieralisi031bd872012-09-06 18:35:13 +053054ENDPROC(v7_flush_dcache_louis)
55
Tony Lindgren81d11952010-09-21 17:16:40 +010056/*
Catalin Marinasbbe88882007-05-08 22:27:46 +010057 * v7_flush_dcache_all()
58 *
59 * Flush the whole D-cache.
60 *
Catalin Marinas347c8b72009-07-24 12:32:56 +010061 * Corrupted registers: r0-r7, r9-r11 (r6 only in Thumb mode)
Catalin Marinasbbe88882007-05-08 22:27:46 +010062 *
63 * - mm - mm_struct describing address space
64 */
65ENTRY(v7_flush_dcache_all)
Catalin Marinasc30c2f92008-11-06 13:23:07 +000066 dmb @ ensure ordering with previous memory accesses
Catalin Marinasbbe88882007-05-08 22:27:46 +010067 mrc p15, 1, r0, c0, c0, 1 @ read clidr
68 ands r3, r0, #0x7000000 @ extract loc from clidr
69 mov r3, r3, lsr #23 @ left align loc bit field
70 beq finished @ if loc is 0, then no need to clean
71 mov r10, #0 @ start clean at cache level 0
Lorenzo Pieralisi3287be82012-09-18 16:29:44 +010072flush_levels:
Catalin Marinasbbe88882007-05-08 22:27:46 +010073 add r2, r10, r10, lsr #1 @ work out 3x current cache level
74 mov r1, r0, lsr r2 @ extract cache type bits from clidr
75 and r1, r1, #7 @ mask of the bits for current cache only
76 cmp r1, #2 @ see what cache we have at this level
77 blt skip @ skip if no cache, or just i-cache
Stephen Boydb46c0f72012-02-07 19:42:07 +010078#ifdef CONFIG_PREEMPT
Rabin Vincent8e43a902012-02-15 16:01:42 +010079 save_and_disable_irqs_notrace r9 @ make cssr&csidr read atomic
Stephen Boydb46c0f72012-02-07 19:42:07 +010080#endif
Catalin Marinasbbe88882007-05-08 22:27:46 +010081 mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
82 isb @ isb to sych the new cssr&csidr
83 mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
Stephen Boydb46c0f72012-02-07 19:42:07 +010084#ifdef CONFIG_PREEMPT
85 restore_irqs_notrace r9
86#endif
Catalin Marinasbbe88882007-05-08 22:27:46 +010087 and r2, r1, #7 @ extract the length of the cache lines
88 add r2, r2, #4 @ add 4 (line length offset)
89 ldr r4, =0x3ff
90 ands r4, r4, r1, lsr #3 @ find maximum number on the way size
91 clz r5, r4 @ find bit position of way size increment
92 ldr r7, =0x7fff
93 ands r7, r7, r1, lsr #13 @ extract max number of the index size
Lorenzo Pieralisi3287be82012-09-18 16:29:44 +010094loop1:
Catalin Marinasbbe88882007-05-08 22:27:46 +010095 mov r9, r4 @ create working copy of max way size
Lorenzo Pieralisi3287be82012-09-18 16:29:44 +010096loop2:
Catalin Marinas347c8b72009-07-24 12:32:56 +010097 ARM( orr r11, r10, r9, lsl r5 ) @ factor way and cache number into r11
98 THUMB( lsl r6, r9, r5 )
99 THUMB( orr r11, r10, r6 ) @ factor way and cache number into r11
100 ARM( orr r11, r11, r7, lsl r2 ) @ factor index number into r11
101 THUMB( lsl r6, r7, r2 )
102 THUMB( orr r11, r11, r6 ) @ factor index number into r11
Catalin Marinasbbe88882007-05-08 22:27:46 +0100103 mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way
104 subs r9, r9, #1 @ decrement the way
Catalin Marinasbbe88882007-05-08 22:27:46 +0100105 bge loop2
Lorenzo Pieralisi3287be82012-09-18 16:29:44 +0100106 subs r7, r7, #1 @ decrement the index
107 bge loop1
Catalin Marinasbbe88882007-05-08 22:27:46 +0100108skip:
109 add r10, r10, #2 @ increment cache number
110 cmp r3, r10
Lorenzo Pieralisi3287be82012-09-18 16:29:44 +0100111 bgt flush_levels
Catalin Marinasbbe88882007-05-08 22:27:46 +0100112finished:
113 mov r10, #0 @ swith back to cache level 0
114 mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
Catalin Marinasc30c2f92008-11-06 13:23:07 +0000115 dsb
Catalin Marinasbbe88882007-05-08 22:27:46 +0100116 isb
117 mov pc, lr
Catalin Marinas93ed3972008-08-28 11:22:32 +0100118ENDPROC(v7_flush_dcache_all)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100119
120/*
121 * v7_flush_cache_all()
122 *
123 * Flush the entire cache system.
124 * The data cache flush is now achieved using atomic clean / invalidates
125 * working outwards from L1 cache. This is done using Set/Way based cache
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300126 * maintenance instructions.
Catalin Marinasbbe88882007-05-08 22:27:46 +0100127 * The instruction cache can still be invalidated back to the point of
128 * unification in a single instruction.
129 *
130 */
131ENTRY(v7_flush_kern_cache_all)
Catalin Marinas347c8b72009-07-24 12:32:56 +0100132 ARM( stmfd sp!, {r4-r5, r7, r9-r11, lr} )
133 THUMB( stmfd sp!, {r4-r7, r9-r11, lr} )
Catalin Marinasbbe88882007-05-08 22:27:46 +0100134 bl v7_flush_dcache_all
135 mov r0, #0
Russell Kingf00ec482010-09-04 10:47:48 +0100136 ALT_SMP(mcr p15, 0, r0, c7, c1, 0) @ invalidate I-cache inner shareable
137 ALT_UP(mcr p15, 0, r0, c7, c5, 0) @ I+BTB cache invalidate
Catalin Marinas347c8b72009-07-24 12:32:56 +0100138 ARM( ldmfd sp!, {r4-r5, r7, r9-r11, lr} )
139 THUMB( ldmfd sp!, {r4-r7, r9-r11, lr} )
Catalin Marinasbbe88882007-05-08 22:27:46 +0100140 mov pc, lr
Catalin Marinas93ed3972008-08-28 11:22:32 +0100141ENDPROC(v7_flush_kern_cache_all)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100142
Lorenzo Pieralisi031bd872012-09-06 18:35:13 +0530143 /*
144 * v7_flush_kern_cache_louis(void)
145 *
146 * Flush the data cache up to Level of Unification Inner Shareable.
147 * Invalidate the I-cache to the point of unification.
148 */
149ENTRY(v7_flush_kern_cache_louis)
150 ARM( stmfd sp!, {r4-r5, r7, r9-r11, lr} )
151 THUMB( stmfd sp!, {r4-r7, r9-r11, lr} )
152 bl v7_flush_dcache_louis
153 mov r0, #0
154 ALT_SMP(mcr p15, 0, r0, c7, c1, 0) @ invalidate I-cache inner shareable
155 ALT_UP(mcr p15, 0, r0, c7, c5, 0) @ I+BTB cache invalidate
156 ARM( ldmfd sp!, {r4-r5, r7, r9-r11, lr} )
157 THUMB( ldmfd sp!, {r4-r7, r9-r11, lr} )
158 mov pc, lr
159ENDPROC(v7_flush_kern_cache_louis)
160
Catalin Marinasbbe88882007-05-08 22:27:46 +0100161/*
162 * v7_flush_cache_all()
163 *
164 * Flush all TLB entries in a particular address space
165 *
166 * - mm - mm_struct describing address space
167 */
168ENTRY(v7_flush_user_cache_all)
169 /*FALLTHROUGH*/
170
171/*
172 * v7_flush_cache_range(start, end, flags)
173 *
174 * Flush a range of TLB entries in the specified address space.
175 *
176 * - start - start address (may not be aligned)
177 * - end - end address (exclusive, may not be aligned)
178 * - flags - vm_area_struct flags describing address space
179 *
180 * It is assumed that:
181 * - we have a VIPT cache.
182 */
183ENTRY(v7_flush_user_cache_range)
184 mov pc, lr
Catalin Marinas93ed3972008-08-28 11:22:32 +0100185ENDPROC(v7_flush_user_cache_all)
186ENDPROC(v7_flush_user_cache_range)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100187
188/*
189 * v7_coherent_kern_range(start,end)
190 *
191 * Ensure that the I and D caches are coherent within specified
192 * region. This is typically used when code has been written to
193 * a memory region, and will be executed.
194 *
195 * - start - virtual start address of region
196 * - end - virtual end address of region
197 *
198 * It is assumed that:
199 * - the Icache does not read data from the write buffer
200 */
201ENTRY(v7_coherent_kern_range)
202 /* FALLTHROUGH */
203
204/*
205 * v7_coherent_user_range(start,end)
206 *
207 * Ensure that the I and D caches are coherent within specified
208 * region. This is typically used when code has been written to
209 * a memory region, and will be executed.
210 *
211 * - start - virtual start address of region
212 * - end - virtual end address of region
213 *
214 * It is assumed that:
215 * - the Icache does not read data from the write buffer
216 */
217ENTRY(v7_coherent_user_range)
Catalin Marinas32cfb1b2009-10-06 17:57:09 +0100218 UNWIND(.fnstart )
Catalin Marinasbbe88882007-05-08 22:27:46 +0100219 dcache_line_size r2, r3
220 sub r3, r2, #1
Catalin Marinasda30e0a2010-12-07 16:56:29 +0100221 bic r12, r0, r3
Will Deaconf630c1b2011-09-15 11:45:15 +0100222#ifdef CONFIG_ARM_ERRATA_764369
223 ALT_SMP(W(dsb))
224 ALT_UP(W(nop))
225#endif
Catalin Marinas32cfb1b2009-10-06 17:57:09 +01002261:
Catalin Marinasda30e0a2010-12-07 16:56:29 +0100227 USER( mcr p15, 0, r12, c7, c11, 1 ) @ clean D line to the point of unification
228 add r12, r12, r2
229 cmp r12, r1
Catalin Marinasbbe88882007-05-08 22:27:46 +0100230 blo 1b
Catalin Marinasda30e0a2010-12-07 16:56:29 +0100231 dsb
232 icache_line_size r2, r3
233 sub r3, r2, #1
234 bic r12, r0, r3
2352:
236 USER( mcr p15, 0, r12, c7, c5, 1 ) @ invalidate I line
237 add r12, r12, r2
238 cmp r12, r1
239 blo 2b
Catalin Marinasbbe88882007-05-08 22:27:46 +0100240 mov r0, #0
Russell Kingf00ec482010-09-04 10:47:48 +0100241 ALT_SMP(mcr p15, 0, r0, c7, c1, 6) @ invalidate BTB Inner Shareable
242 ALT_UP(mcr p15, 0, r0, c7, c5, 6) @ invalidate BTB
Catalin Marinasbbe88882007-05-08 22:27:46 +0100243 dsb
244 isb
245 mov pc, lr
Catalin Marinas32cfb1b2009-10-06 17:57:09 +0100246
247/*
248 * Fault handling for the cache operation above. If the virtual address in r0
Will Deaconc5102f52012-04-27 13:08:53 +0100249 * isn't mapped, fail with -EFAULT.
Catalin Marinas32cfb1b2009-10-06 17:57:09 +0100250 */
2519001:
Simon Horman7253b852012-09-28 02:12:45 +0100252#ifdef CONFIG_ARM_ERRATA_775420
253 dsb
254#endif
Will Deaconc5102f52012-04-27 13:08:53 +0100255 mov r0, #-EFAULT
256 mov pc, lr
Catalin Marinas32cfb1b2009-10-06 17:57:09 +0100257 UNWIND(.fnend )
Catalin Marinas93ed3972008-08-28 11:22:32 +0100258ENDPROC(v7_coherent_kern_range)
259ENDPROC(v7_coherent_user_range)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100260
261/*
Russell King2c9b9c82009-11-26 12:56:21 +0000262 * v7_flush_kern_dcache_area(void *addr, size_t size)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100263 *
264 * Ensure that the data held in the page kaddr is written back
265 * to the page in question.
266 *
Russell King2c9b9c82009-11-26 12:56:21 +0000267 * - addr - kernel address
268 * - size - region size
Catalin Marinasbbe88882007-05-08 22:27:46 +0100269 */
Russell King2c9b9c82009-11-26 12:56:21 +0000270ENTRY(v7_flush_kern_dcache_area)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100271 dcache_line_size r2, r3
Russell King2c9b9c82009-11-26 12:56:21 +0000272 add r1, r0, r1
Will Deacona248b132011-05-26 11:20:19 +0100273 sub r3, r2, #1
274 bic r0, r0, r3
Will Deaconf630c1b2011-09-15 11:45:15 +0100275#ifdef CONFIG_ARM_ERRATA_764369
276 ALT_SMP(W(dsb))
277 ALT_UP(W(nop))
278#endif
Catalin Marinasbbe88882007-05-08 22:27:46 +01002791:
280 mcr p15, 0, r0, c7, c14, 1 @ clean & invalidate D line / unified line
281 add r0, r0, r2
282 cmp r0, r1
283 blo 1b
284 dsb
285 mov pc, lr
Russell King2c9b9c82009-11-26 12:56:21 +0000286ENDPROC(v7_flush_kern_dcache_area)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100287
288/*
289 * v7_dma_inv_range(start,end)
290 *
291 * Invalidate the data cache within the specified region; we will
292 * be performing a DMA operation in this region and we want to
293 * purge old data in the cache.
294 *
295 * - start - virtual start address of region
296 * - end - virtual end address of region
297 */
Russell King702b94b2009-11-26 16:24:19 +0000298v7_dma_inv_range:
Catalin Marinasbbe88882007-05-08 22:27:46 +0100299 dcache_line_size r2, r3
300 sub r3, r2, #1
301 tst r0, r3
302 bic r0, r0, r3
Will Deaconf630c1b2011-09-15 11:45:15 +0100303#ifdef CONFIG_ARM_ERRATA_764369
304 ALT_SMP(W(dsb))
305 ALT_UP(W(nop))
306#endif
Catalin Marinasbbe88882007-05-08 22:27:46 +0100307 mcrne p15, 0, r0, c7, c14, 1 @ clean & invalidate D / U line
308
309 tst r1, r3
310 bic r1, r1, r3
311 mcrne p15, 0, r1, c7, c14, 1 @ clean & invalidate D / U line
3121:
313 mcr p15, 0, r0, c7, c6, 1 @ invalidate D / U line
314 add r0, r0, r2
315 cmp r0, r1
316 blo 1b
317 dsb
318 mov pc, lr
Catalin Marinas93ed3972008-08-28 11:22:32 +0100319ENDPROC(v7_dma_inv_range)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100320
321/*
322 * v7_dma_clean_range(start,end)
323 * - start - virtual start address of region
324 * - end - virtual end address of region
325 */
Russell King702b94b2009-11-26 16:24:19 +0000326v7_dma_clean_range:
Catalin Marinasbbe88882007-05-08 22:27:46 +0100327 dcache_line_size r2, r3
328 sub r3, r2, #1
329 bic r0, r0, r3
Will Deaconf630c1b2011-09-15 11:45:15 +0100330#ifdef CONFIG_ARM_ERRATA_764369
331 ALT_SMP(W(dsb))
332 ALT_UP(W(nop))
333#endif
Catalin Marinasbbe88882007-05-08 22:27:46 +01003341:
335 mcr p15, 0, r0, c7, c10, 1 @ clean D / U line
336 add r0, r0, r2
337 cmp r0, r1
338 blo 1b
339 dsb
340 mov pc, lr
Catalin Marinas93ed3972008-08-28 11:22:32 +0100341ENDPROC(v7_dma_clean_range)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100342
343/*
344 * v7_dma_flush_range(start,end)
345 * - start - virtual start address of region
346 * - end - virtual end address of region
347 */
348ENTRY(v7_dma_flush_range)
349 dcache_line_size r2, r3
350 sub r3, r2, #1
351 bic r0, r0, r3
Will Deaconf630c1b2011-09-15 11:45:15 +0100352#ifdef CONFIG_ARM_ERRATA_764369
353 ALT_SMP(W(dsb))
354 ALT_UP(W(nop))
355#endif
Catalin Marinasbbe88882007-05-08 22:27:46 +01003561:
357 mcr p15, 0, r0, c7, c14, 1 @ clean & invalidate D / U line
358 add r0, r0, r2
359 cmp r0, r1
360 blo 1b
361 dsb
362 mov pc, lr
Catalin Marinas93ed3972008-08-28 11:22:32 +0100363ENDPROC(v7_dma_flush_range)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100364
Russell Kinga9c91472009-11-26 16:19:58 +0000365/*
366 * dma_map_area(start, size, dir)
367 * - start - kernel virtual start address
368 * - size - size of region
369 * - dir - DMA direction
370 */
371ENTRY(v7_dma_map_area)
372 add r1, r1, r0
Russell King2ffe2da2009-10-31 16:52:16 +0000373 teq r2, #DMA_FROM_DEVICE
374 beq v7_dma_inv_range
375 b v7_dma_clean_range
Russell Kinga9c91472009-11-26 16:19:58 +0000376ENDPROC(v7_dma_map_area)
377
378/*
379 * dma_unmap_area(start, size, dir)
380 * - start - kernel virtual start address
381 * - size - size of region
382 * - dir - DMA direction
383 */
384ENTRY(v7_dma_unmap_area)
Russell King2ffe2da2009-10-31 16:52:16 +0000385 add r1, r1, r0
386 teq r2, #DMA_TO_DEVICE
387 bne v7_dma_inv_range
Russell Kinga9c91472009-11-26 16:19:58 +0000388 mov pc, lr
389ENDPROC(v7_dma_unmap_area)
390
Catalin Marinasbbe88882007-05-08 22:27:46 +0100391 __INITDATA
392
Dave Martin455a01e2011-06-23 17:16:25 +0100393 @ define struct cpu_cache_fns (see <asm/cacheflush.h> and proc-macros.S)
394 define_cache_functions v7