blob: d2dc5e60c11da9cb32617ec4a7104afeb65ddb39 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
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>
16#include <asm/unwind.h>
17
18#include "proc-macros.S"
19
20/*
21 * v7_flush_icache_all()
22 *
23 * Flush the whole I-cache.
24 *
25 * Registers:
26 * r0 - set to 0
27 */
28ENTRY(v7_flush_icache_all)
29 mov r0, #0
30 ALT_SMP(mcr p15, 0, r0, c7, c1, 0) @ invalidate I-cache inner shareable
31 ALT_UP(mcr p15, 0, r0, c7, c5, 0) @ I+BTB cache invalidate
32 mov pc, lr
33ENDPROC(v7_flush_icache_all)
34
35
36/*
37 * v7_invalidate_dcache_all()
38 *
39 * invalidateh the whole D-cache.
40 *
41 * Corrupted registers: r0-r7, r9-r11 (r6 only in Thumb mode)
42 *
43 * - mm - mm_struct describing address space
44 *
45 * created by xuzhiguo to fix a bug on D cache when zx297510 power on reset
46 */
47
48ENTRY(v7_invalidate_dcache_all)
49 dmb @ ensure ordering with previous memory accesses
50 mrc p15, 1, r0, c0, c0, 1 @ read clidr
51 ands r3, r0, #0x7000000 @ extract loc from clidr
52 mov r3, r3, lsr #23 @ left align loc bit field
53 beq finished0 @ if loc is 0, then no need to clean
54 mov r10, #0 @ start clean at cache level 0
55loop01:
56 add r2, r10, r10, lsr #1 @ work out 3x current cache level
57 mov r1, r0, lsr r2 @ extract cache type bits from clidr
58 and r1, r1, #7 @ mask of the bits for current cache only
59 cmp r1, #2 @ see what cache we have at this level
60 blt skip0 @ skip if no cache, or just i-cache
61#ifdef CONFIG_PREEMPT
62 save_and_disable_irqs_notrace r9 @ make cssr&csidr read atomic
63#endif
64 mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
65 isb @ isb to sych the new cssr&csidr
66 mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
67#ifdef CONFIG_PREEMPT
68 restore_irqs_notrace r9
69#endif
70 and r2, r1, #7 @ extract the length of the cache lines
71 add r2, r2, #4 @ add 4 (line length offset)
72 ldr r4, =0x3ff
73 ands r4, r4, r1, lsr #3 @ find maximum number on the way size
74 clz r5, r4 @ find bit position of way size increment
75 ldr r7, =0x7fff
76 ands r7, r7, r1, lsr #13 @ extract max number of the index size
77loop02:
78 mov r9, r4 @ create working copy of max way size
79loop03:
80 ARM( orr r11, r10, r9, lsl r5 ) @ factor way and cache number into r11
81 THUMB( lsl r6, r9, r5 )
82 THUMB( orr r11, r10, r6 ) @ factor way and cache number into r11
83 ARM( orr r11, r11, r7, lsl r2 ) @ factor index number into r11
84 THUMB( lsl r6, r7, r2 )
85 THUMB( orr r11, r11, r6 ) @ factor index number into r11
86 @mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way
87 mcr p15, 0, r11, c7, c6, 2 @ invalidate by set/way on zx297510 only
88 subs r9, r9, #1 @ decrement the way
89 bge loop03
90 subs r7, r7, #1 @ decrement the index
91 bge loop02
92skip0:
93 add r10, r10, #2 @ increment cache number
94 cmp r3, r10
95 bgt loop01
96finished0:
97 mov r10, #0 @ swith back to cache level 0
98 mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
99 dsb
100 isb
101 mov pc, lr
102ENDPROC(v7_invalidate_dcache_all)
103
104/*
105 * v7_flush_dcache_all()
106 *
107 * Flush the whole D-cache.
108 *
109 * Corrupted registers: r0-r7, r9-r11 (r6 only in Thumb mode)
110 *
111 * - mm - mm_struct describing address space
112 */
113ENTRY(v7_flush_dcache_all)
114 dmb @ ensure ordering with previous memory accesses
115 mrc p15, 1, r0, c0, c0, 1 @ read clidr
116 ands r3, r0, #0x7000000 @ extract loc from clidr
117 mov r3, r3, lsr #23 @ left align loc bit field
118 beq finished @ if loc is 0, then no need to clean
119 mov r10, #0 @ start clean at cache level 0
120loop1:
121 add r2, r10, r10, lsr #1 @ work out 3x current cache level
122 mov r1, r0, lsr r2 @ extract cache type bits from clidr
123 and r1, r1, #7 @ mask of the bits for current cache only
124 cmp r1, #2 @ see what cache we have at this level
125 blt skip @ skip if no cache, or just i-cache
126#ifdef CONFIG_PREEMPT
127 save_and_disable_irqs_notrace r9 @ make cssr&csidr read atomic
128#endif
129 mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
130 isb @ isb to sych the new cssr&csidr
131 mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
132#ifdef CONFIG_PREEMPT
133 restore_irqs_notrace r9
134#endif
135 and r2, r1, #7 @ extract the length of the cache lines
136 add r2, r2, #4 @ add 4 (line length offset)
137 ldr r4, =0x3ff
138 ands r4, r4, r1, lsr #3 @ find maximum number on the way size
139 clz r5, r4 @ find bit position of way size increment
140 ldr r7, =0x7fff
141 ands r7, r7, r1, lsr #13 @ extract max number of the index size
142loop2:
143 mov r9, r4 @ create working copy of max way size
144loop3:
145 ARM( orr r11, r10, r9, lsl r5 ) @ factor way and cache number into r11
146 THUMB( lsl r6, r9, r5 )
147 THUMB( orr r11, r10, r6 ) @ factor way and cache number into r11
148 ARM( orr r11, r11, r7, lsl r2 ) @ factor index number into r11
149 THUMB( lsl r6, r7, r2 )
150 THUMB( orr r11, r11, r6 ) @ factor index number into r11
151 mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way
152 subs r9, r9, #1 @ decrement the way
153 bge loop3
154 subs r7, r7, #1 @ decrement the index
155 bge loop2
156skip:
157 add r10, r10, #2 @ increment cache number
158 cmp r3, r10
159 bgt loop1
160finished:
161 mov r10, #0 @ swith back to cache level 0
162 mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
163 dsb
164 isb
165 mov pc, lr
166ENDPROC(v7_flush_dcache_all)
167
168/*
169 * v7_flush_cache_all()
170 *
171 * Flush the entire cache system.
172 * The data cache flush is now achieved using atomic clean / invalidates
173 * working outwards from L1 cache. This is done using Set/Way based cache
174 * maintenance instructions.
175 * The instruction cache can still be invalidated back to the point of
176 * unification in a single instruction.
177 *
178 */
179ENTRY(v7_flush_kern_cache_all)
180 ARM( stmfd sp!, {r4-r5, r7, r9-r11, lr} )
181 THUMB( stmfd sp!, {r4-r7, r9-r11, lr} )
182 bl v7_flush_dcache_all
183 mov r0, #0
184 ALT_SMP(mcr p15, 0, r0, c7, c1, 0) @ invalidate I-cache inner shareable
185 ALT_UP(mcr p15, 0, r0, c7, c5, 0) @ I+BTB cache invalidate
186 ARM( ldmfd sp!, {r4-r5, r7, r9-r11, lr} )
187 THUMB( ldmfd sp!, {r4-r7, r9-r11, lr} )
188 mov pc, lr
189ENDPROC(v7_flush_kern_cache_all)
190
191/*
192 * v7_flush_cache_all()
193 *
194 * Flush all TLB entries in a particular address space
195 *
196 * - mm - mm_struct describing address space
197 */
198ENTRY(v7_flush_user_cache_all)
199 /*FALLTHROUGH*/
200
201/*
202 * v7_flush_cache_range(start, end, flags)
203 *
204 * Flush a range of TLB entries in the specified address space.
205 *
206 * - start - start address (may not be aligned)
207 * - end - end address (exclusive, may not be aligned)
208 * - flags - vm_area_struct flags describing address space
209 *
210 * It is assumed that:
211 * - we have a VIPT cache.
212 */
213ENTRY(v7_flush_user_cache_range)
214 mov pc, lr
215ENDPROC(v7_flush_user_cache_all)
216ENDPROC(v7_flush_user_cache_range)
217
218/*
219 * v7_coherent_kern_range(start,end)
220 *
221 * Ensure that the I and D caches are coherent within specified
222 * region. This is typically used when code has been written to
223 * a memory region, and will be executed.
224 *
225 * - start - virtual start address of region
226 * - end - virtual end address of region
227 *
228 * It is assumed that:
229 * - the Icache does not read data from the write buffer
230 */
231ENTRY(v7_coherent_kern_range)
232 /* FALLTHROUGH */
233
234/*
235 * v7_coherent_user_range(start,end)
236 *
237 * Ensure that the I and D caches are coherent within specified
238 * region. This is typically used when code has been written to
239 * a memory region, and will be executed.
240 *
241 * - start - virtual start address of region
242 * - end - virtual end address of region
243 *
244 * It is assumed that:
245 * - the Icache does not read data from the write buffer
246 */
247ENTRY(v7_coherent_user_range)
248 UNWIND(.fnstart )
249 dcache_line_size r2, r3
250 sub r3, r2, #1
251 bic r12, r0, r3
252#ifdef CONFIG_ARM_ERRATA_764369
253 ALT_SMP(W(dsb))
254 ALT_UP(W(nop))
255#endif
2561:
257 USER( mcr p15, 0, r12, c7, c11, 1 ) @ clean D line to the point of unification
258 add r12, r12, r2
259 cmp r12, r1
260 blo 1b
261 dsb
262 icache_line_size r2, r3
263 sub r3, r2, #1
264 bic r12, r0, r3
2652:
266 USER( mcr p15, 0, r12, c7, c5, 1 ) @ invalidate I line
267 add r12, r12, r2
268 cmp r12, r1
269 blo 2b
2703:
271 mov r0, #0
272 ALT_SMP(mcr p15, 0, r0, c7, c1, 6) @ invalidate BTB Inner Shareable
273 ALT_UP(mcr p15, 0, r0, c7, c5, 6) @ invalidate BTB
274 dsb
275 isb
276 mov pc, lr
277
278/*
279 * Fault handling for the cache operation above. If the virtual address in r0
280 * isn't mapped, just try the next page.
281 */
2829001:
283#ifdef CONFIG_ARM_ERRATA_775420
284 dsb
285#endif
286 mov r12, r12, lsr #12
287 mov r12, r12, lsl #12
288 add r12, r12, #4096
289 b 3b
290 UNWIND(.fnend )
291ENDPROC(v7_coherent_kern_range)
292ENDPROC(v7_coherent_user_range)
293
294/*
295 * v7_flush_kern_dcache_area(void *addr, size_t size)
296 *
297 * Ensure that the data held in the page kaddr is written back
298 * to the page in question.
299 *
300 * - addr - kernel address
301 * - size - region size
302 */
303ENTRY(v7_flush_kern_dcache_area)
304 dcache_line_size r2, r3
305 add r1, r0, r1
306 sub r3, r2, #1
307 bic r0, r0, r3
308#ifdef CONFIG_ARM_ERRATA_764369
309 ALT_SMP(W(dsb))
310 ALT_UP(W(nop))
311#endif
3121:
313 mcr p15, 0, r0, c7, c14, 1 @ clean & invalidate D line / unified line
314 add r0, r0, r2
315 cmp r0, r1
316 blo 1b
317 dsb
318 mov pc, lr
319ENDPROC(v7_flush_kern_dcache_area)
320
321/*
322 * v7_dma_inv_range(start,end)
323 *
324 * Invalidate the data cache within the specified region; we will
325 * be performing a DMA operation in this region and we want to
326 * purge old data in the cache.
327 *
328 * - start - virtual start address of region
329 * - end - virtual end address of region
330 */
331v7_dma_inv_range:
332 dcache_line_size r2, r3
333 sub r3, r2, #1
334 tst r0, r3
335 bic r0, r0, r3
336#ifdef CONFIG_ARM_ERRATA_764369
337 ALT_SMP(W(dsb))
338 ALT_UP(W(nop))
339#endif
340 mcrne p15, 0, r0, c7, c14, 1 @ clean & invalidate D / U line
341
342 tst r1, r3
343 bic r1, r1, r3
344 mcrne p15, 0, r1, c7, c14, 1 @ clean & invalidate D / U line
3451:
346 mcr p15, 0, r0, c7, c6, 1 @ invalidate D / U line
347 add r0, r0, r2
348 cmp r0, r1
349 blo 1b
350 dsb
351 mov pc, lr
352ENDPROC(v7_dma_inv_range)
353
354/*
355 * v7_dma_clean_range(start,end)
356 * - start - virtual start address of region
357 * - end - virtual end address of region
358 */
359v7_dma_clean_range:
360 dcache_line_size r2, r3
361 sub r3, r2, #1
362 bic r0, r0, r3
363#ifdef CONFIG_ARM_ERRATA_764369
364 ALT_SMP(W(dsb))
365 ALT_UP(W(nop))
366#endif
3671:
368 mcr p15, 0, r0, c7, c10, 1 @ clean D / U line
369 add r0, r0, r2
370 cmp r0, r1
371 blo 1b
372 dsb
373 mov pc, lr
374ENDPROC(v7_dma_clean_range)
375
376/*
377 * v7_dma_flush_range(start,end)
378 * - start - virtual start address of region
379 * - end - virtual end address of region
380 */
381ENTRY(v7_dma_flush_range)
382 dcache_line_size r2, r3
383 sub r3, r2, #1
384 bic r0, r0, r3
385#ifdef CONFIG_ARM_ERRATA_764369
386 ALT_SMP(W(dsb))
387 ALT_UP(W(nop))
388#endif
3891:
390 mcr p15, 0, r0, c7, c14, 1 @ clean & invalidate D / U line
391 add r0, r0, r2
392 cmp r0, r1
393 blo 1b
394 dsb
395 mov pc, lr
396ENDPROC(v7_dma_flush_range)
397
398/*
399 * dma_map_area(start, size, dir)
400 * - start - kernel virtual start address
401 * - size - size of region
402 * - dir - DMA direction
403 */
404ENTRY(v7_dma_map_area)
405 add r1, r1, r0
406 teq r2, #DMA_FROM_DEVICE
407 beq v7_dma_inv_range
408 b v7_dma_clean_range
409ENDPROC(v7_dma_map_area)
410
411/*
412 * dma_unmap_area(start, size, dir)
413 * - start - kernel virtual start address
414 * - size - size of region
415 * - dir - DMA direction
416 */
417ENTRY(v7_dma_unmap_area)
418 add r1, r1, r0
419 teq r2, #DMA_TO_DEVICE
420 bne v7_dma_inv_range
421 mov pc, lr
422ENDPROC(v7_dma_unmap_area)
423
424 __INITDATA
425
426 @ define struct cpu_cache_fns (see <asm/cacheflush.h> and proc-macros.S)
427 define_cache_functions v7