1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * linux/arch/arm/mm/cache-v7m.S
5 * Based on linux/arch/arm/mm/cache-v7.S
7 * Copyright (C) 2001 Deep Blue Solutions Ltd.
8 * Copyright (C) 2005 ARM Ltd.
10 * This is the "shell" of the ARMv7M processor support.
12 #include <linux/linkage.h>
13 #include <linux/init.h>
14 #include <asm/assembler.h>
15 #include <asm/errno.h>
16 #include <asm/unwind.h>
19 #include "proc-macros.S"
21 /* Generic V7M read/write macros for memory mapped cache operations */
22 .macro v7m_cache_read, rt, reg
23 movw \rt, #:lower16:BASEADDR_V7M_SCB + \reg
24 movt \rt, #:upper16:BASEADDR_V7M_SCB + \reg
28 .macro v7m_cacheop, rt, tmp, op, c = al
29 movw\c \tmp, #:lower16:BASEADDR_V7M_SCB + \op
30 movt\c \tmp, #:upper16:BASEADDR_V7M_SCB + \op
35 .macro read_ccsidr, rt
36 v7m_cache_read \rt, V7M_SCB_CCSIDR
40 v7m_cache_read \rt, V7M_SCB_CLIDR
43 .macro write_csselr, rt, tmp
44 v7m_cacheop \rt, \tmp, V7M_SCB_CSSELR
48 * dcisw: Invalidate data cache by set/way
51 v7m_cacheop \rt, \tmp, V7M_SCB_DCISW
55 * dccisw: Clean and invalidate data cache by set/way
57 .macro dccisw, rt, tmp
58 v7m_cacheop \rt, \tmp, V7M_SCB_DCCISW
62 * dccimvac: Clean and invalidate data cache line by MVA to PoC.
64 .irp c,,eq,ne,cs,cc,mi,pl,vs,vc,hi,ls,ge,lt,gt,le,hs,lo
65 .macro dccimvac\c, rt, tmp
66 v7m_cacheop \rt, \tmp, V7M_SCB_DCCIMVAC, \c
71 * dcimvac: Invalidate data cache line by MVA to PoC
73 .irp c,,eq,ne,cs,cc,mi,pl,vs,vc,hi,ls,ge,lt,gt,le,hs,lo
74 .macro dcimvac\c, rt, tmp
75 v7m_cacheop \rt, \tmp, V7M_SCB_DCIMVAC, \c
80 * dccmvau: Clean data cache line by MVA to PoU
82 .macro dccmvau, rt, tmp
83 v7m_cacheop \rt, \tmp, V7M_SCB_DCCMVAU
87 * dccmvac: Clean data cache line by MVA to PoC
89 .macro dccmvac, rt, tmp
90 v7m_cacheop \rt, \tmp, V7M_SCB_DCCMVAC
94 * icimvau: Invalidate instruction caches by MVA to PoU
96 .macro icimvau, rt, tmp
97 v7m_cacheop \rt, \tmp, V7M_SCB_ICIMVAU
101 * Invalidate the icache, inner shareable if SMP, invalidate BTB for UP.
102 * rt data ignored by ICIALLU(IS), so can be used for the address
104 .macro invalidate_icache, rt
105 v7m_cacheop \rt, \rt, V7M_SCB_ICIALLU
110 * Invalidate the BTB, inner shareable if SMP.
111 * rt data ignored by BPIALL, so it can be used for the address
113 .macro invalidate_bp, rt
114 v7m_cacheop \rt, \rt, V7M_SCB_BPIALL
118 ENTRY(v7m_invalidate_l1)
125 and r2, r1, r0, lsr #13
129 and r3, r1, r0, lsr #3 @ NumWays - 1
130 add r2, r2, #1 @ NumSets
133 add r0, r0, #4 @ SetShift
135 clz r1, r3 @ WayShift
136 add r4, r3, #1 @ NumWays
137 1: sub r2, r2, #1 @ NumSets--
138 mov r3, r4 @ Temp = NumWays
139 2: subs r3, r3, #1 @ Temp--
142 orr r5, r5, r6 @ Reg = (Temp<<WayShift)|(NumSets<<SetShift)
150 ENDPROC(v7m_invalidate_l1)
153 * v7m_flush_icache_all()
155 * Flush the whole I-cache.
160 ENTRY(v7m_flush_icache_all)
163 ENDPROC(v7m_flush_icache_all)
166 * v7m_flush_dcache_all()
168 * Flush the whole D-cache.
170 * Corrupted registers: r0-r7, r9-r11
172 ENTRY(v7m_flush_dcache_all)
173 dmb @ ensure ordering with previous memory accesses
175 mov r3, r0, lsr #23 @ move LoC into position
176 ands r3, r3, #7 << 1 @ extract LoC*2 from clidr
177 beq finished @ if loc is 0, then no need to clean
179 mov r10, #0 @ start clean at cache level 0
181 add r2, r10, r10, lsr #1 @ work out 3x current cache level
182 mov r1, r0, lsr r2 @ extract cache type bits from clidr
183 and r1, r1, #7 @ mask of the bits for current cache only
184 cmp r1, #2 @ see what cache we have at this level
185 blt skip @ skip if no cache, or just i-cache
186 #ifdef CONFIG_PREEMPTION
187 save_and_disable_irqs_notrace r9 @ make cssr&csidr read atomic
189 write_csselr r10, r1 @ set current cache level
190 isb @ isb to sych the new cssr&csidr
191 read_ccsidr r1 @ read the new csidr
192 #ifdef CONFIG_PREEMPTION
193 restore_irqs_notrace r9
195 and r2, r1, #7 @ extract the length of the cache lines
196 add r2, r2, #4 @ add 4 (line length offset)
198 ands r4, r4, r1, lsr #3 @ find maximum number on the way size
199 clz r5, r4 @ find bit position of way size increment
201 ands r7, r7, r1, lsr #13 @ extract max number of the index size
203 mov r9, r7 @ create working copy of max index
206 orr r11, r10, r6 @ factor way and cache number into r11
208 orr r11, r11, r6 @ factor index number into r11
209 dccisw r11, r6 @ clean/invalidate by set/way
210 subs r9, r9, #1 @ decrement the index
212 subs r4, r4, #1 @ decrement the way
215 add r10, r10, #2 @ increment cache number
219 mov r10, #0 @ switch back to cache level 0
220 write_csselr r10, r3 @ select current cache level in cssr
224 ENDPROC(v7m_flush_dcache_all)
227 * v7m_flush_cache_all()
229 * Flush the entire cache system.
230 * The data cache flush is now achieved using atomic clean / invalidates
231 * working outwards from L1 cache. This is done using Set/Way based cache
232 * maintenance instructions.
233 * The instruction cache can still be invalidated back to the point of
234 * unification in a single instruction.
237 ENTRY(v7m_flush_kern_cache_all)
238 stmfd sp!, {r4-r7, r9-r11, lr}
239 bl v7m_flush_dcache_all
241 ldmfd sp!, {r4-r7, r9-r11, lr}
243 ENDPROC(v7m_flush_kern_cache_all)
246 * v7m_flush_cache_all()
248 * Flush all TLB entries in a particular address space
250 * - mm - mm_struct describing address space
252 ENTRY(v7m_flush_user_cache_all)
256 * v7m_flush_cache_range(start, end, flags)
258 * Flush a range of TLB entries in the specified address space.
260 * - start - start address (may not be aligned)
261 * - end - end address (exclusive, may not be aligned)
262 * - flags - vm_area_struct flags describing address space
264 * It is assumed that:
265 * - we have a VIPT cache.
267 ENTRY(v7m_flush_user_cache_range)
269 ENDPROC(v7m_flush_user_cache_all)
270 ENDPROC(v7m_flush_user_cache_range)
273 * v7m_coherent_kern_range(start,end)
275 * Ensure that the I and D caches are coherent within specified
276 * region. This is typically used when code has been written to
277 * a memory region, and will be executed.
279 * - start - virtual start address of region
280 * - end - virtual end address of region
282 * It is assumed that:
283 * - the Icache does not read data from the write buffer
285 ENTRY(v7m_coherent_kern_range)
289 * v7m_coherent_user_range(start,end)
291 * Ensure that the I and D caches are coherent within specified
292 * region. This is typically used when code has been written to
293 * a memory region, and will be executed.
295 * - start - virtual start address of region
296 * - end - virtual end address of region
298 * It is assumed that:
299 * - the Icache does not read data from the write buffer
301 ENTRY(v7m_coherent_user_range)
303 dcache_line_size r2, r3
308 * We use open coded version of dccmvau otherwise USER() would
309 * point at movw instruction.
316 icache_line_size r2, r3
329 ENDPROC(v7m_coherent_kern_range)
330 ENDPROC(v7m_coherent_user_range)
333 * v7m_flush_kern_dcache_area(void *addr, size_t size)
335 * Ensure that the data held in the page kaddr is written back
336 * to the page in question.
338 * - addr - kernel address
339 * - size - region size
341 ENTRY(v7m_flush_kern_dcache_area)
342 dcache_line_size r2, r3
347 dccimvac r0, r3 @ clean & invalidate D line / unified line
353 ENDPROC(v7m_flush_kern_dcache_area)
356 * v7m_dma_inv_range(start,end)
358 * Invalidate the data cache within the specified region; we will
359 * be performing a DMA operation in this region and we want to
360 * purge old data in the cache.
362 * - start - virtual start address of region
363 * - end - virtual end address of region
366 dcache_line_size r2, r3
372 subne r3, r2, #1 @ restore r3, corrupted by v7m's dccimvac
384 ENDPROC(v7m_dma_inv_range)
387 * v7m_dma_clean_range(start,end)
388 * - start - virtual start address of region
389 * - end - virtual end address of region
392 dcache_line_size r2, r3
396 dccmvac r0, r3 @ clean D / U line
402 ENDPROC(v7m_dma_clean_range)
405 * v7m_dma_flush_range(start,end)
406 * - start - virtual start address of region
407 * - end - virtual end address of region
409 ENTRY(v7m_dma_flush_range)
410 dcache_line_size r2, r3
414 dccimvac r0, r3 @ clean & invalidate D / U line
420 ENDPROC(v7m_dma_flush_range)
423 * dma_map_area(start, size, dir)
424 * - start - kernel virtual start address
425 * - size - size of region
426 * - dir - DMA direction
428 ENTRY(v7m_dma_map_area)
430 teq r2, #DMA_FROM_DEVICE
431 beq v7m_dma_inv_range
432 b v7m_dma_clean_range
433 ENDPROC(v7m_dma_map_area)
436 * dma_unmap_area(start, size, dir)
437 * - start - kernel virtual start address
438 * - size - size of region
439 * - dir - DMA direction
441 ENTRY(v7m_dma_unmap_area)
443 teq r2, #DMA_TO_DEVICE
444 bne v7m_dma_inv_range
446 ENDPROC(v7m_dma_unmap_area)
448 .globl v7m_flush_kern_cache_louis
449 .equ v7m_flush_kern_cache_louis, v7m_flush_kern_cache_all
453 @ define struct cpu_cache_fns (see <asm/cacheflush.h> and proc-macros.S)
454 define_cache_functions v7m