blob: 140b294bbd9b61fbefa1f79b9056be9b3c6c3d0a [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
47 ands r3, r0, #0xe00000 @ extract LoUIS from clidr
48 mov r3, r3, lsr #20 @ r3 = LoUIS * 2
49 moveq pc, lr @ return if level == 0
50 mov r10, #0 @ r10 (starting level) = 0
Lorenzo Pieralisi3287be82012-09-18 16:29:44 +010051 b flush_levels @ start flushing cache levels
Lorenzo Pieralisi031bd872012-09-06 18:35:13 +053052ENDPROC(v7_flush_dcache_louis)
53
Tony Lindgren81d11952010-09-21 17:16:40 +010054/*
Catalin Marinasbbe88882007-05-08 22:27:46 +010055 * v7_flush_dcache_all()
56 *
57 * Flush the whole D-cache.
58 *
Catalin Marinas347c8b72009-07-24 12:32:56 +010059 * Corrupted registers: r0-r7, r9-r11 (r6 only in Thumb mode)
Catalin Marinasbbe88882007-05-08 22:27:46 +010060 *
61 * - mm - mm_struct describing address space
62 */
63ENTRY(v7_flush_dcache_all)
Catalin Marinasc30c2f92008-11-06 13:23:07 +000064 dmb @ ensure ordering with previous memory accesses
Catalin Marinasbbe88882007-05-08 22:27:46 +010065 mrc p15, 1, r0, c0, c0, 1 @ read clidr
66 ands r3, r0, #0x7000000 @ extract loc from clidr
67 mov r3, r3, lsr #23 @ left align loc bit field
68 beq finished @ if loc is 0, then no need to clean
69 mov r10, #0 @ start clean at cache level 0
Lorenzo Pieralisi3287be82012-09-18 16:29:44 +010070flush_levels:
Catalin Marinasbbe88882007-05-08 22:27:46 +010071 add r2, r10, r10, lsr #1 @ work out 3x current cache level
72 mov r1, r0, lsr r2 @ extract cache type bits from clidr
73 and r1, r1, #7 @ mask of the bits for current cache only
74 cmp r1, #2 @ see what cache we have at this level
75 blt skip @ skip if no cache, or just i-cache
Stephen Boydb46c0f72012-02-07 19:42:07 +010076#ifdef CONFIG_PREEMPT
Rabin Vincent8e43a902012-02-15 16:01:42 +010077 save_and_disable_irqs_notrace r9 @ make cssr&csidr read atomic
Stephen Boydb46c0f72012-02-07 19:42:07 +010078#endif
Catalin Marinasbbe88882007-05-08 22:27:46 +010079 mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
80 isb @ isb to sych the new cssr&csidr
81 mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
Stephen Boydb46c0f72012-02-07 19:42:07 +010082#ifdef CONFIG_PREEMPT
83 restore_irqs_notrace r9
84#endif
Catalin Marinasbbe88882007-05-08 22:27:46 +010085 and r2, r1, #7 @ extract the length of the cache lines
86 add r2, r2, #4 @ add 4 (line length offset)
87 ldr r4, =0x3ff
88 ands r4, r4, r1, lsr #3 @ find maximum number on the way size
89 clz r5, r4 @ find bit position of way size increment
90 ldr r7, =0x7fff
91 ands r7, r7, r1, lsr #13 @ extract max number of the index size
Lorenzo Pieralisi3287be82012-09-18 16:29:44 +010092loop1:
Catalin Marinasbbe88882007-05-08 22:27:46 +010093 mov r9, r4 @ create working copy of max way size
Lorenzo Pieralisi3287be82012-09-18 16:29:44 +010094loop2:
Catalin Marinas347c8b72009-07-24 12:32:56 +010095 ARM( orr r11, r10, r9, lsl r5 ) @ factor way and cache number into r11
96 THUMB( lsl r6, r9, r5 )
97 THUMB( orr r11, r10, r6 ) @ factor way and cache number into r11
98 ARM( orr r11, r11, r7, lsl r2 ) @ factor index number into r11
99 THUMB( lsl r6, r7, r2 )
100 THUMB( orr r11, r11, r6 ) @ factor index number into r11
Catalin Marinasbbe88882007-05-08 22:27:46 +0100101 mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way
102 subs r9, r9, #1 @ decrement the way
Catalin Marinasbbe88882007-05-08 22:27:46 +0100103 bge loop2
Lorenzo Pieralisi3287be82012-09-18 16:29:44 +0100104 subs r7, r7, #1 @ decrement the index
105 bge loop1
Catalin Marinasbbe88882007-05-08 22:27:46 +0100106skip:
107 add r10, r10, #2 @ increment cache number
108 cmp r3, r10
Lorenzo Pieralisi3287be82012-09-18 16:29:44 +0100109 bgt flush_levels
Catalin Marinasbbe88882007-05-08 22:27:46 +0100110finished:
111 mov r10, #0 @ swith back to cache level 0
112 mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
Catalin Marinasc30c2f92008-11-06 13:23:07 +0000113 dsb
Catalin Marinasbbe88882007-05-08 22:27:46 +0100114 isb
115 mov pc, lr
Catalin Marinas93ed3972008-08-28 11:22:32 +0100116ENDPROC(v7_flush_dcache_all)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100117
118/*
119 * v7_flush_cache_all()
120 *
121 * Flush the entire cache system.
122 * The data cache flush is now achieved using atomic clean / invalidates
123 * working outwards from L1 cache. This is done using Set/Way based cache
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300124 * maintenance instructions.
Catalin Marinasbbe88882007-05-08 22:27:46 +0100125 * The instruction cache can still be invalidated back to the point of
126 * unification in a single instruction.
127 *
128 */
129ENTRY(v7_flush_kern_cache_all)
Catalin Marinas347c8b72009-07-24 12:32:56 +0100130 ARM( stmfd sp!, {r4-r5, r7, r9-r11, lr} )
131 THUMB( stmfd sp!, {r4-r7, r9-r11, lr} )
Catalin Marinasbbe88882007-05-08 22:27:46 +0100132 bl v7_flush_dcache_all
133 mov r0, #0
Russell Kingf00ec482010-09-04 10:47:48 +0100134 ALT_SMP(mcr p15, 0, r0, c7, c1, 0) @ invalidate I-cache inner shareable
135 ALT_UP(mcr p15, 0, r0, c7, c5, 0) @ I+BTB cache invalidate
Catalin Marinas347c8b72009-07-24 12:32:56 +0100136 ARM( ldmfd sp!, {r4-r5, r7, r9-r11, lr} )
137 THUMB( ldmfd sp!, {r4-r7, r9-r11, lr} )
Catalin Marinasbbe88882007-05-08 22:27:46 +0100138 mov pc, lr
Catalin Marinas93ed3972008-08-28 11:22:32 +0100139ENDPROC(v7_flush_kern_cache_all)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100140
Lorenzo Pieralisi031bd872012-09-06 18:35:13 +0530141 /*
142 * v7_flush_kern_cache_louis(void)
143 *
144 * Flush the data cache up to Level of Unification Inner Shareable.
145 * Invalidate the I-cache to the point of unification.
146 */
147ENTRY(v7_flush_kern_cache_louis)
148 ARM( stmfd sp!, {r4-r5, r7, r9-r11, lr} )
149 THUMB( stmfd sp!, {r4-r7, r9-r11, lr} )
150 bl v7_flush_dcache_louis
151 mov r0, #0
152 ALT_SMP(mcr p15, 0, r0, c7, c1, 0) @ invalidate I-cache inner shareable
153 ALT_UP(mcr p15, 0, r0, c7, c5, 0) @ I+BTB cache invalidate
154 ARM( ldmfd sp!, {r4-r5, r7, r9-r11, lr} )
155 THUMB( ldmfd sp!, {r4-r7, r9-r11, lr} )
156 mov pc, lr
157ENDPROC(v7_flush_kern_cache_louis)
158
Catalin Marinasbbe88882007-05-08 22:27:46 +0100159/*
160 * v7_flush_cache_all()
161 *
162 * Flush all TLB entries in a particular address space
163 *
164 * - mm - mm_struct describing address space
165 */
166ENTRY(v7_flush_user_cache_all)
167 /*FALLTHROUGH*/
168
169/*
170 * v7_flush_cache_range(start, end, flags)
171 *
172 * Flush a range of TLB entries in the specified address space.
173 *
174 * - start - start address (may not be aligned)
175 * - end - end address (exclusive, may not be aligned)
176 * - flags - vm_area_struct flags describing address space
177 *
178 * It is assumed that:
179 * - we have a VIPT cache.
180 */
181ENTRY(v7_flush_user_cache_range)
182 mov pc, lr
Catalin Marinas93ed3972008-08-28 11:22:32 +0100183ENDPROC(v7_flush_user_cache_all)
184ENDPROC(v7_flush_user_cache_range)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100185
186/*
187 * v7_coherent_kern_range(start,end)
188 *
189 * Ensure that the I and D caches are coherent within specified
190 * region. This is typically used when code has been written to
191 * a memory region, and will be executed.
192 *
193 * - start - virtual start address of region
194 * - end - virtual end address of region
195 *
196 * It is assumed that:
197 * - the Icache does not read data from the write buffer
198 */
199ENTRY(v7_coherent_kern_range)
200 /* FALLTHROUGH */
201
202/*
203 * v7_coherent_user_range(start,end)
204 *
205 * Ensure that the I and D caches are coherent within specified
206 * region. This is typically used when code has been written to
207 * a memory region, and will be executed.
208 *
209 * - start - virtual start address of region
210 * - end - virtual end address of region
211 *
212 * It is assumed that:
213 * - the Icache does not read data from the write buffer
214 */
215ENTRY(v7_coherent_user_range)
Catalin Marinas32cfb1b2009-10-06 17:57:09 +0100216 UNWIND(.fnstart )
Catalin Marinasbbe88882007-05-08 22:27:46 +0100217 dcache_line_size r2, r3
218 sub r3, r2, #1
Catalin Marinasda30e0a2010-12-07 16:56:29 +0100219 bic r12, r0, r3
Will Deaconf630c1b2011-09-15 11:45:15 +0100220#ifdef CONFIG_ARM_ERRATA_764369
221 ALT_SMP(W(dsb))
222 ALT_UP(W(nop))
223#endif
Catalin Marinas32cfb1b2009-10-06 17:57:09 +01002241:
Catalin Marinasda30e0a2010-12-07 16:56:29 +0100225 USER( mcr p15, 0, r12, c7, c11, 1 ) @ clean D line to the point of unification
226 add r12, r12, r2
227 cmp r12, r1
Catalin Marinasbbe88882007-05-08 22:27:46 +0100228 blo 1b
Catalin Marinasda30e0a2010-12-07 16:56:29 +0100229 dsb
230 icache_line_size r2, r3
231 sub r3, r2, #1
232 bic r12, r0, r3
2332:
234 USER( mcr p15, 0, r12, c7, c5, 1 ) @ invalidate I line
235 add r12, r12, r2
236 cmp r12, r1
237 blo 2b
Catalin Marinasbbe88882007-05-08 22:27:46 +0100238 mov r0, #0
Russell Kingf00ec482010-09-04 10:47:48 +0100239 ALT_SMP(mcr p15, 0, r0, c7, c1, 6) @ invalidate BTB Inner Shareable
240 ALT_UP(mcr p15, 0, r0, c7, c5, 6) @ invalidate BTB
Catalin Marinasbbe88882007-05-08 22:27:46 +0100241 dsb
242 isb
243 mov pc, lr
Catalin Marinas32cfb1b2009-10-06 17:57:09 +0100244
245/*
246 * Fault handling for the cache operation above. If the virtual address in r0
Will Deaconc5102f52012-04-27 13:08:53 +0100247 * isn't mapped, fail with -EFAULT.
Catalin Marinas32cfb1b2009-10-06 17:57:09 +0100248 */
2499001:
Will Deaconc5102f52012-04-27 13:08:53 +0100250 mov r0, #-EFAULT
251 mov pc, lr
Catalin Marinas32cfb1b2009-10-06 17:57:09 +0100252 UNWIND(.fnend )
Catalin Marinas93ed3972008-08-28 11:22:32 +0100253ENDPROC(v7_coherent_kern_range)
254ENDPROC(v7_coherent_user_range)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100255
256/*
Russell King2c9b9c82009-11-26 12:56:21 +0000257 * v7_flush_kern_dcache_area(void *addr, size_t size)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100258 *
259 * Ensure that the data held in the page kaddr is written back
260 * to the page in question.
261 *
Russell King2c9b9c82009-11-26 12:56:21 +0000262 * - addr - kernel address
263 * - size - region size
Catalin Marinasbbe88882007-05-08 22:27:46 +0100264 */
Russell King2c9b9c82009-11-26 12:56:21 +0000265ENTRY(v7_flush_kern_dcache_area)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100266 dcache_line_size r2, r3
Russell King2c9b9c82009-11-26 12:56:21 +0000267 add r1, r0, r1
Will Deacona248b132011-05-26 11:20:19 +0100268 sub r3, r2, #1
269 bic r0, r0, r3
Will Deaconf630c1b2011-09-15 11:45:15 +0100270#ifdef CONFIG_ARM_ERRATA_764369
271 ALT_SMP(W(dsb))
272 ALT_UP(W(nop))
273#endif
Catalin Marinasbbe88882007-05-08 22:27:46 +01002741:
275 mcr p15, 0, r0, c7, c14, 1 @ clean & invalidate D line / unified line
276 add r0, r0, r2
277 cmp r0, r1
278 blo 1b
279 dsb
280 mov pc, lr
Russell King2c9b9c82009-11-26 12:56:21 +0000281ENDPROC(v7_flush_kern_dcache_area)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100282
283/*
284 * v7_dma_inv_range(start,end)
285 *
286 * Invalidate the data cache within the specified region; we will
287 * be performing a DMA operation in this region and we want to
288 * purge old data in the cache.
289 *
290 * - start - virtual start address of region
291 * - end - virtual end address of region
292 */
Russell King702b94b2009-11-26 16:24:19 +0000293v7_dma_inv_range:
Catalin Marinasbbe88882007-05-08 22:27:46 +0100294 dcache_line_size r2, r3
295 sub r3, r2, #1
296 tst r0, r3
297 bic r0, r0, r3
Will Deaconf630c1b2011-09-15 11:45:15 +0100298#ifdef CONFIG_ARM_ERRATA_764369
299 ALT_SMP(W(dsb))
300 ALT_UP(W(nop))
301#endif
Catalin Marinasbbe88882007-05-08 22:27:46 +0100302 mcrne p15, 0, r0, c7, c14, 1 @ clean & invalidate D / U line
303
304 tst r1, r3
305 bic r1, r1, r3
306 mcrne p15, 0, r1, c7, c14, 1 @ clean & invalidate D / U line
3071:
308 mcr p15, 0, r0, c7, c6, 1 @ invalidate D / U line
309 add r0, r0, r2
310 cmp r0, r1
311 blo 1b
312 dsb
313 mov pc, lr
Catalin Marinas93ed3972008-08-28 11:22:32 +0100314ENDPROC(v7_dma_inv_range)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100315
316/*
317 * v7_dma_clean_range(start,end)
318 * - start - virtual start address of region
319 * - end - virtual end address of region
320 */
Russell King702b94b2009-11-26 16:24:19 +0000321v7_dma_clean_range:
Catalin Marinasbbe88882007-05-08 22:27:46 +0100322 dcache_line_size r2, r3
323 sub r3, r2, #1
324 bic r0, r0, r3
Will Deaconf630c1b2011-09-15 11:45:15 +0100325#ifdef CONFIG_ARM_ERRATA_764369
326 ALT_SMP(W(dsb))
327 ALT_UP(W(nop))
328#endif
Catalin Marinasbbe88882007-05-08 22:27:46 +01003291:
330 mcr p15, 0, r0, c7, c10, 1 @ clean D / U line
331 add r0, r0, r2
332 cmp r0, r1
333 blo 1b
334 dsb
335 mov pc, lr
Catalin Marinas93ed3972008-08-28 11:22:32 +0100336ENDPROC(v7_dma_clean_range)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100337
338/*
339 * v7_dma_flush_range(start,end)
340 * - start - virtual start address of region
341 * - end - virtual end address of region
342 */
343ENTRY(v7_dma_flush_range)
344 dcache_line_size r2, r3
345 sub r3, r2, #1
346 bic r0, r0, r3
Will Deaconf630c1b2011-09-15 11:45:15 +0100347#ifdef CONFIG_ARM_ERRATA_764369
348 ALT_SMP(W(dsb))
349 ALT_UP(W(nop))
350#endif
Catalin Marinasbbe88882007-05-08 22:27:46 +01003511:
352 mcr p15, 0, r0, c7, c14, 1 @ clean & invalidate D / U line
353 add r0, r0, r2
354 cmp r0, r1
355 blo 1b
356 dsb
357 mov pc, lr
Catalin Marinas93ed3972008-08-28 11:22:32 +0100358ENDPROC(v7_dma_flush_range)
Catalin Marinasbbe88882007-05-08 22:27:46 +0100359
Russell Kinga9c91472009-11-26 16:19:58 +0000360/*
361 * dma_map_area(start, size, dir)
362 * - start - kernel virtual start address
363 * - size - size of region
364 * - dir - DMA direction
365 */
366ENTRY(v7_dma_map_area)
367 add r1, r1, r0
Russell King2ffe2da2009-10-31 16:52:16 +0000368 teq r2, #DMA_FROM_DEVICE
369 beq v7_dma_inv_range
370 b v7_dma_clean_range
Russell Kinga9c91472009-11-26 16:19:58 +0000371ENDPROC(v7_dma_map_area)
372
373/*
374 * dma_unmap_area(start, size, dir)
375 * - start - kernel virtual start address
376 * - size - size of region
377 * - dir - DMA direction
378 */
379ENTRY(v7_dma_unmap_area)
Russell King2ffe2da2009-10-31 16:52:16 +0000380 add r1, r1, r0
381 teq r2, #DMA_TO_DEVICE
382 bne v7_dma_inv_range
Russell Kinga9c91472009-11-26 16:19:58 +0000383 mov pc, lr
384ENDPROC(v7_dma_unmap_area)
385
Catalin Marinasbbe88882007-05-08 22:27:46 +0100386 __INITDATA
387
Dave Martin455a01e2011-06-23 17:16:25 +0100388 @ define struct cpu_cache_fns (see <asm/cacheflush.h> and proc-macros.S)
389 define_cache_functions v7