3 * David Feng <fenghua@phytium.com.cn>
5 * This file is based on sample code from ARMv8 ARM.
7 * SPDX-License-Identifier: GPL-2.0+
10 #include <asm-offsets.h>
12 #include <asm/macro.h>
13 #include <asm/system.h>
14 #include <linux/linkage.h>
17 * void __asm_dcache_level(level)
19 * flush or invalidate one level cache.
22 * x1: 0 clean & invalidate, 1 invalidate only
25 ENTRY(__asm_dcache_level)
27 msr csselr_el1, x12 /* select cache level */
28 isb /* sync change of cssidr_el1 */
29 mrs x6, ccsidr_el1 /* read the new cssidr_el1 */
30 and x2, x6, #7 /* x2 <- log2(cache line size)-4 */
31 add x2, x2, #4 /* x2 <- log2(cache line size) */
33 and x3, x3, x6, lsr #3 /* x3 <- max number of #ways */
34 clz w5, w3 /* bit position of #ways */
36 and x4, x4, x6, lsr #13 /* x4 <- max number of #sets */
37 /* x12 <- cache level << 1 */
38 /* x2 <- line length offset */
39 /* x3 <- number of cache ways - 1 */
40 /* x4 <- number of cache sets - 1 */
41 /* x5 <- bit position of #ways */
44 mov x6, x3 /* x6 <- working copy of #ways */
47 orr x9, x12, x7 /* map way and level to cisw value */
49 orr x9, x9, x7 /* map set number to cisw value */
53 1: dc cisw, x9 /* clean & invalidate by set/way */
54 2: subs x6, x6, #1 /* decrement the way */
56 subs x4, x4, #1 /* decrement the set */
60 ENDPROC(__asm_dcache_level)
63 * void __asm_flush_dcache_all(int invalidate_only)
65 * x0: 0 clean & invalidate, 1 invalidate only
67 * flush or invalidate all data cache by SET/WAY.
69 ENTRY(__asm_dcache_all)
72 mrs x10, clidr_el1 /* read clidr_el1 */
74 and x11, x11, #0x7 /* x11 <- loc */
75 cbz x11, finished /* if loc is 0, exit */
77 mov x0, #0 /* start flush at cache level 0 */
78 /* x0 <- cache level */
79 /* x10 <- clidr_el1 */
81 /* x15 <- return address */
85 add x12, x12, x0 /* x0 <- tripled cache level */
87 and x12, x12, #7 /* x12 <- cache type */
89 b.lt skip /* skip if no cache or icache */
90 bl __asm_dcache_level /* x1 = 0 flush, 1 invalidate */
92 add x0, x0, #1 /* increment cache level */
97 msr csselr_el1, x0 /* restore csselr_el1 */
104 ENDPROC(__asm_dcache_all)
106 ENTRY(__asm_flush_dcache_all)
109 ENDPROC(__asm_flush_dcache_all)
111 ENTRY(__asm_invalidate_dcache_all)
114 ENDPROC(__asm_invalidate_dcache_all)
117 * void __asm_flush_dcache_range(start, end)
119 * clean & invalidate data cache in the range
124 ENTRY(__asm_flush_dcache_range)
129 lsl x2, x2, x3 /* cache line size */
131 /* x2 <- minimal cache line size in cache system */
134 1: dc civac, x0 /* clean & invalidate data or unified cache */
140 ENDPROC(__asm_flush_dcache_range)
142 * void __asm_invalidate_dcache_range(start, end)
144 * invalidate data cache in the range
149 ENTRY(__asm_invalidate_dcache_range)
151 ubfm x3, x3, #16, #19
153 lsl x2, x2, x3 /* cache line size */
155 /* x2 <- minimal cache line size in cache system */
158 1: dc ivac, x0 /* invalidate data or unified cache */
164 ENDPROC(__asm_invalidate_dcache_range)
167 * void __asm_invalidate_icache_all(void)
169 * invalidate all tlb entries.
171 ENTRY(__asm_invalidate_icache_all)
175 ENDPROC(__asm_invalidate_icache_all)
177 ENTRY(__asm_invalidate_l3_dcache)
178 mov x0, #0 /* return status as success */
180 ENDPROC(__asm_invalidate_l3_dcache)
181 .weak __asm_invalidate_l3_dcache
183 ENTRY(__asm_flush_l3_dcache)
184 mov x0, #0 /* return status as success */
186 ENDPROC(__asm_flush_l3_dcache)
187 .weak __asm_flush_l3_dcache
189 ENTRY(__asm_invalidate_l3_icache)
190 mov x0, #0 /* return status as success */
192 ENDPROC(__asm_invalidate_l3_icache)
193 .weak __asm_invalidate_l3_icache
196 * void __asm_switch_ttbr(ulong new_ttbr)
198 * Safely switches to a new page table.
200 ENTRY(__asm_switch_ttbr)
201 /* x2 = SCTLR (alive throghout the function) */
202 switch_el x4, 3f, 2f, 1f
210 /* Unset CR_M | CR_C | CR_I from SCTLR to disable all caches */
211 movn x1, #(CR_M | CR_C | CR_I)
213 switch_el x4, 3f, 2f, 1f
221 /* This call only clobbers x30 (lr) and x9 (unused) */
223 bl __asm_invalidate_tlb_all
225 /* From here on we're running safely with caches disabled */
227 /* Set TTBR to our first argument */
228 switch_el x4, 3f, 2f, 1f
236 /* Restore original SCTLR and thus enable caches again */
237 switch_el x4, 3f, 2f, 1f
246 ENDPROC(__asm_switch_ttbr)