Debian Codegraph @

payloads/libpayload/include/arm/arch/cache.h

raw symbols SCIP
1 /*
2 *
3 * Copyright 2013 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * cache.h: Cache maintenance API for ARM
29 */
30
31 #ifndef ARM_CACHE_H
32 #define ARM_CACHE_H
33
34 #include <stddef.h>
35 #include <stdint.h>
36
37 /* SCTLR bits */
38 #define SCTLR_M (1 << 0) /* MMU enable */
39 #define SCTLR_A (1 << 1) /* Alignment check enable */
40 #define SCTLR_C (1 << 2) /* Data/unified cache enable */
41 /* Bits 4:3 are reserved */
42 #define SCTLR_CP15BEN (1 << 5) /* CP15 barrier enable */
43 /* Bit 6 is reserved */
44 #define SCTLR_B (1 << 7) /* Endianness */
45 /* Bits 9:8 */
46 #define SCTLR_SW (1 << 10) /* SWP and SWPB enable */
47 #define SCTLR_Z (1 << 11) /* Branch prediction enable */
48 #define SCTLR_I (1 << 12) /* Instruction cache enable */
49 #define SCTLR_V (1 << 13) /* Low/high exception vectors */
50 #define SCTLR_RR (1 << 14) /* Round Robin select */
51 /* Bits 16:15 are reserved */
52 #define SCTLR_HA (1 << 17) /* Hardware Access flag enable */
53 /* Bit 18 is reserved */
54 /* Bits 20:19 reserved virtualization not supported */
55 #define SCTLR_WXN (1 << 19) /* Write permission implies XN */
56 #define SCTLR_UWXN (1 << 20) /* Unprivileged write permission
57 implies PL1 XN */
58 #define SCTLR_FI (1 << 21) /* Fast interrupt config enable */
59 #define SCTLR_U (1 << 22) /* Unaligned access behavior */
60 #define SCTLR_VE (1 << 24) /* Interrupt vectors enable */
61 #define SCTLR_EE (1 << 25) /* Exception endianness */
62 /* Bit 26 is reserved */
63 #define SCTLR_NMFI (1 << 27) /* Non-maskable FIQ support */
64 #define SCTLR_TRE (1 << 28) /* TEX remap enable */
65 #define SCTLR_AFE (1 << 29) /* Access flag enable */
66 #define SCTLR_TE (1 << 30) /* Thumb exception enable */
67 /* Bit 31 is reserved */
68
69 /*
70 * Sync primitives
71 */
72 /* data memory barrier */
73 #define dmb() asm volatile ("dmb" : : : "memory")
74 /* data sync barrier */
75 #define dsb() asm volatile ("dsb" : : : "memory")
76 /* instruction sync barrier */
77 #define isb() asm volatile ("isb" : : : "memory")
78
79 /*
80 * Low-level TLB maintenance operations
81 */
82
83 /* invalidate entire unified TLB */
84 static inline void tlbiall(void)
85 {
86 asm volatile ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0) : "memory");
87 }
88
89 /* invalidate unified TLB by MVA, all ASID */
90 static inline void tlbimvaa(unsigned long mva)
91 {
92 asm volatile ("mcr p15, 0, %0, c8, c7, 3" : : "r" (mva) : "memory");
93 }
94
95 /* write data access control register (DACR) */
96 static inline void write_dacr(uint32_t val)
97 {
98 asm volatile ("mcr p15, 0, %0, c3, c0, 0" : : "r" (val));
99 }
100
101 /* write translation table base register 0 (TTBR0) */
102 static inline void write_ttbr0(uint32_t val)
103 {
104 asm volatile ("mcr p15, 0, %0, c2, c0, 0" : : "r" (val) : "memory");
105 }
106
107 /* read translation table base register 0 (TTBR0) */
108 static inline uint64_t read_ttbr0(void)
109 {
110 uint32_t low, high;
111 asm volatile ("mrrc p15, 0, %[low], %[high], c2" :
112 [low] "=r" (low), [high] "=r" (high));
113 return ((uint64_t)high << 32) | low;
114 }
115
116 /* read translation table base control register (TTBCR) */
117 static inline uint32_t read_ttbcr(void)
118 {
119 uint32_t val = 0;
120 asm volatile ("mrc p15, 0, %0, c2, c0, 2" : "=r" (val));
121 return val;
122 }
123
124 /* write translation table base control register (TTBCR) */
125 static inline void write_ttbcr(uint32_t val)
126 {
127 asm volatile ("mcr p15, 0, %0, c2, c0, 2" : : "r" (val) : "memory");
128 }
129
130 /*
131 * Low-level cache maintenance operations
132 */
133
134 /* branch predictor invalidate all */
135 static inline void bpiall(void)
136 {
137 asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0));
138 }
139
140 /* data cache clean and invalidate by MVA to PoC */
141 static inline void dccimvac(unsigned long mva)
142 {
143 asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva) : "memory");
144 }
145
146 /* data cache invalidate by set/way */
147 static inline void dccisw(uint32_t val)
148 {
149 asm volatile ("mcr p15, 0, %0, c7, c14, 2" : : "r" (val) : "memory");
150 }
151
152 /* data cache clean by MVA to PoC */
153 static inline void dccmvac(unsigned long mva)
154 {
155 asm volatile ("mcr p15, 0, %0, c7, c10, 1" : : "r" (mva) : "memory");
156 }
157
158 /* data cache clean by set/way */
159 static inline void dccsw(uint32_t val)
160 {
161 asm volatile ("mcr p15, 0, %0, c7, c10, 2" : : "r" (val) : "memory");
162 }
163
164 /* data cache invalidate by MVA to PoC */
165 static inline void dcimvac(unsigned long mva)
166 {
167 asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (mva) : "memory");
168 }
169
170 /* data cache invalidate by set/way */
171 static inline void dcisw(uint32_t val)
172 {
173 asm volatile ("mcr p15, 0, %0, c7, c6, 2" : : "r" (val) : "memory");
174 }
175
176 /* instruction cache invalidate all by PoU */
177 static inline void iciallu(void)
178 {
179 asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
180 }
181
182 /*
183 * Cache co-processor (CP15) access functions
184 */
185
186 /* read cache level ID register (CLIDR) */
187 static inline uint32_t read_clidr(void)
188 {
189 uint32_t val = 0;
190 asm volatile ("mrc p15, 1, %0, c0, c0, 1" : "=r" (val));
191 return val;
192 }
193
194 /* read cache size ID register register (CCSIDR) */
195 static inline uint32_t read_ccsidr(void)
196 {
197 uint32_t val = 0;
198 asm volatile ("mrc p15, 1, %0, c0, c0, 0" : "=r" (val));
199 return val;
200 }
201
202 /* read cache size selection register (CSSELR) */
203 static inline uint32_t read_csselr(void)
204 {
205 uint32_t val = 0;
206 asm volatile ("mrc p15, 2, %0, c0, c0, 0" : "=r" (val));
207 return val;
208 }
209
210 /* write to cache size selection register (CSSELR) */
211 static inline void write_csselr(uint32_t val)
212 {
213 /*
214 * Bits [3:1] - Cache level + 1 (0b000 = L1, 0b110 = L7, 0b111 is rsvd)
215 * Bit 0 - 0 = data or unified cache, 1 = instruction cache
216 */
217 asm volatile ("mcr p15, 2, %0, c0, c0, 0" : : "r" (val));
218 isb(); /* ISB to sync the change to CCSIDR */
219 }
220
221 /* read L2 control register (L2CTLR) */
222 static inline uint32_t read_l2ctlr(void)
223 {
224 uint32_t val = 0;
225 asm volatile ("mrc p15, 1, %0, c9, c0, 2" : "=r" (val));
226 return val;
227 }
228
229 /* write L2 control register (L2CTLR) */
230 static inline void write_l2ctlr(uint32_t val)
231 {
232 /*
233 * Note: L2CTLR can only be written when the L2 memory system
234 * is idle, ie before the MMU is enabled.
235 */
236 asm volatile("mcr p15, 1, %0, c9, c0, 2" : : "r" (val) : "memory" );
237 isb();
238 }
239
240 /* read L2 Auxiliary Control Register (L2ACTLR) */
241 static inline uint32_t read_l2actlr(void)
242 {
243 uint32_t val = 0;
244 asm volatile ("mrc p15, 1, %0, c15, c0, 0" : "=r" (val));
245 return val;
246 }
247
248 /* write L2 Auxiliary Control Register (L2ACTLR) */
249 static inline void write_l2actlr(uint32_t val)
250 {
251 asm volatile ("mcr p15, 1, %0, c15, c0, 0" : : "r" (val) : "memory" );
252 isb();
253 }
254
255 /* read system control register (SCTLR) */
256 static inline uint32_t read_sctlr(void)
257 {
258 uint32_t val;
259 asm volatile ("mrc p15, 0, %0, c1, c0, 0" : "=r" (val));
260 return val;
261 }
262
263 /* write system control register (SCTLR) */
264 static inline void write_sctlr(uint32_t val)
265 {
266 asm volatile ("mcr p15, 0, %0, c1, c0, 0" : : "r" (val) : "cc");
267 isb();
268 }
269
270 /* read data fault address register (DFAR) */
271 static inline uint32_t read_dfar(void)
272 {
273 uint32_t val;
274 asm volatile ("mrc p15, 0, %0, c6, c0, 0" : "=r" (val));
275 return val;
276 }
277
278 /* read data fault status register (DFSR) */
279 static inline uint32_t read_dfsr(void)
280 {
281 uint32_t val;
282 asm volatile ("mrc p15, 0, %0, c5, c0, 0" : "=r" (val));
283 return val;
284 }
285
286 /* read instruction fault address register (IFAR) */
287 static inline uint32_t read_ifar(void)
288 {
289 uint32_t val;
290 asm volatile ("mrc p15, 0, %0, c6, c0, 2" : "=r" (val));
291 return val;
292 }
293
294 /* read instruction fault status register (IFSR) */
295 static inline uint32_t read_ifsr(void)
296 {
297 uint32_t val;
298 asm volatile ("mrc p15, 0, %0, c5, c0, 1" : "=r" (val));
299 return val;
300 }
301
302 /* read auxiliary data fault status register (ADFSR) */
303 static inline uint32_t read_adfsr(void)
304 {
305 uint32_t val;
306 asm volatile ("mrc p15, 0, %0, c5, c1, 0" : "=r" (val));
307 return val;
308 }
309
310 /* read auxiliary instruction fault status register (AIFSR) */
311 static inline uint32_t read_aifsr(void)
312 {
313 uint32_t val;
314 asm volatile ("mrc p15, 0, %0, c5, c1, 1" : "=r" (val));
315 return val;
316 }
317
318 /*
319 * Cache maintenance API
320 */
321
322 /* dcache clean and invalidate all (on current level given by CCSELR) */
323 void dcache_clean_invalidate_all(void);
324
325 /* dcache clean by modified virtual address to PoC */
326 void dcache_clean_by_mva(void const *addr, size_t len);
327
328 /* dcache clean and invalidate by modified virtual address to PoC */
329 void dcache_clean_invalidate_by_mva(void const *addr, size_t len);
330
331 /* dcache invalidate by modified virtual address to PoC */
332 void dcache_invalidate_by_mva(void const *addr, size_t len);
333
334 void dcache_clean_all(void);
335
336 /* dcache invalidate all (on current level given by CCSELR) */
337 void dcache_invalidate_all(void);
338
339 /* returns number of bytes per cache line */
340 unsigned int dcache_line_bytes(void);
341
342 /* dcache and MMU disable */
343 void dcache_mmu_disable(void);
344
345 /* dcache and MMU enable */
346 void dcache_mmu_enable(void);
347
348 /* perform all icache/dcache maintenance needed after loading new code */
349 void cache_sync_instructions(void);
350
351 /* tlb invalidate all */
352 void tlb_invalidate_all(void);
353
354 /*
355 * Generalized setup/init functions
356 */
357
358 /* mmu initialization (set page table address, set permissions, etc) */
359 void mmu_init(void);
360
361 enum dcache_policy {
362 DCACHE_OFF,
363 DCACHE_WRITEBACK,
364 DCACHE_WRITETHROUGH,
365 };
366
367 /* disable the mmu for a range. Primarily useful to lock out address 0. */
368 void mmu_disable_range(unsigned long start_mb, unsigned long size_mb);
369 /* mmu range configuration (set dcache policy) */
370 void mmu_config_range(unsigned long start_mb, unsigned long size_mb,
371 enum dcache_policy policy);
372
373 #endif /* ARM_CACHE_H */