blob: dd271c5bcecdbd42a33c04d6baf36ff555116c31 [file] [log] [blame]
David Hendricks2fba5e22013-03-14 19:06:11 -07001/*
2 * This file is part of the coreboot project.
3 *
Deepa Dinamanie1977482015-01-28 14:15:56 -08004 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
David Hendricks2fba5e22013-03-14 19:06:11 -07005 * Copyright 2013 Google Inc.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
David Hendricks08e36562013-03-25 15:02:29 -070029 *
Gabe Black51edd542013-09-30 23:00:33 -070030 * cache.h: Cache maintenance API for ARM
David Hendricks2fba5e22013-03-14 19:06:11 -070031 */
32
Gabe Black51edd542013-09-30 23:00:33 -070033#ifndef ARM_CACHE_H
34#define ARM_CACHE_H
David Hendricks2fba5e22013-03-14 19:06:11 -070035
Julius Wernerf09f2242013-08-28 14:43:14 -070036#include <stddef.h>
Gabe Blackc0f82d22013-07-24 03:50:18 -070037#include <stdint.h>
38
David Hendricks2fba5e22013-03-14 19:06:11 -070039/* SCTLR bits */
40#define SCTLR_M (1 << 0) /* MMU enable */
41#define SCTLR_A (1 << 1) /* Alignment check enable */
42#define SCTLR_C (1 << 2) /* Data/unified cache enable */
43/* Bits 4:3 are reserved */
44#define SCTLR_CP15BEN (1 << 5) /* CP15 barrier enable */
45/* Bit 6 is reserved */
46#define SCTLR_B (1 << 7) /* Endianness */
47/* Bits 9:8 */
48#define SCTLR_SW (1 << 10) /* SWP and SWPB enable */
49#define SCTLR_Z (1 << 11) /* Branch prediction enable */
50#define SCTLR_I (1 << 12) /* Instruction cache enable */
51#define SCTLR_V (1 << 13) /* Low/high exception vectors */
52#define SCTLR_RR (1 << 14) /* Round Robin select */
53/* Bits 16:15 are reserved */
54#define SCTLR_HA (1 << 17) /* Hardware Access flag enable */
55/* Bit 18 is reserved */
56/* Bits 20:19 reserved virtualization not supported */
57#define SCTLR_WXN (1 << 19) /* Write permission implies XN */
58#define SCTLR_UWXN (1 << 20) /* Unprivileged write permission
59 implies PL1 XN */
60#define SCTLR_FI (1 << 21) /* Fast interrupt config enable */
61#define SCTLR_U (1 << 22) /* Unaligned access behavior */
62#define SCTLR_VE (1 << 24) /* Interrupt vectors enable */
63#define SCTLR_EE (1 << 25) /* Exception endianness */
64/* Bit 26 is reserved */
65#define SCTLR_NMFI (1 << 27) /* Non-maskable FIQ support */
66#define SCTLR_TRE (1 << 28) /* TEX remap enable */
67#define SCTLR_AFE (1 << 29) /* Access flag enable */
68#define SCTLR_TE (1 << 30) /* Thumb exception enable */
69/* Bit 31 is reserved */
70
71/*
72 * Sync primitives
73 */
74
75/* data memory barrier */
76static inline void dmb(void)
77{
78 asm volatile ("dmb" : : : "memory");
79}
80
81/* data sync barrier */
82static inline void dsb(void)
83{
84 asm volatile ("dsb" : : : "memory");
85}
86
87/* instruction sync barrier */
88static inline void isb(void)
89{
90 asm volatile ("isb" : : : "memory");
91}
92
93/*
94 * Low-level TLB maintenance operations
95 */
96
David Hendricks2fba5e22013-03-14 19:06:11 -070097/* invalidate entire unified TLB */
98static inline void tlbiall(void)
99{
David Hendricks08e36562013-03-25 15:02:29 -0700100 asm volatile ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0) : "memory");
101}
102
David Hendricksb98ab4a2013-08-16 12:17:50 -0700103/* invalidate unified TLB by MVA, all ASID */
104static inline void tlbimvaa(unsigned long mva)
105{
106 asm volatile ("mcr p15, 0, %0, c8, c7, 3" : : "r" (mva) : "memory");
107}
108
David Hendricks08e36562013-03-25 15:02:29 -0700109/* write data access control register (DACR) */
110static inline void write_dacr(uint32_t val)
111{
112 asm volatile ("mcr p15, 0, %0, c3, c0, 0" : : "r" (val));
113}
114
Daisuke Nojirif574a322014-02-27 14:56:39 -0800115/* read memory model feature register 0 (MMFR0) */
116static inline uint32_t read_mmfr0(void)
117{
118 uint32_t mmfr;
119 asm volatile ("mrc p15, 0, %0, c0, c1, 4" : "=r" (mmfr));
120 return mmfr;
121}
122/* read MAIR0 (memory address indirection register 0) */
123static inline uint32_t read_mair0(void)
124{
125 uint32_t mair;
126 asm volatile ("mrc p15, 0, %0, c10, c2, 0" : "=r" (mair));
127 return mair;
128}
129/* write MAIR0 (memory address indirection register 0) */
130static inline void write_mair0(uint32_t val)
131{
132 asm volatile ("mcr p15, 0, %0, c10, c2, 0" : : "r" (val));
133}
David Hendricks08e36562013-03-25 15:02:29 -0700134/* write translation table base register 0 (TTBR0) */
135static inline void write_ttbr0(uint32_t val)
136{
Martin Roth898a7752017-06-01 11:39:59 -0600137 if (IS_ENABLED(CONFIG_ARM_LPAE))
138 asm volatile ("mcrr p15, 0, %[val], %[zero], c2" : :
Daisuke Nojirif574a322014-02-27 14:56:39 -0800139 [val] "r" (val), [zero] "r" (0));
Martin Roth898a7752017-06-01 11:39:59 -0600140 else
141 asm volatile ("mcr p15, 0, %0, c2, c0, 0" : : "r" (val) : "memory");
David Hendricks08e36562013-03-25 15:02:29 -0700142}
143
144/* read translation table base control register (TTBCR) */
145static inline uint32_t read_ttbcr(void)
146{
147 uint32_t val = 0;
148 asm volatile ("mrc p15, 0, %0, c2, c0, 2" : "=r" (val));
149 return val;
150}
151
152/* write translation table base control register (TTBCR) */
153static inline void write_ttbcr(uint32_t val)
154{
155 asm volatile ("mcr p15, 0, %0, c2, c0, 2" : : "r" (val) : "memory");
David Hendricks2fba5e22013-03-14 19:06:11 -0700156}
157
158/*
159 * Low-level cache maintenance operations
160 */
161
162/* branch predictor invalidate all */
163static inline void bpiall(void)
164{
165 asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0));
166}
167
168/* data cache clean and invalidate by MVA to PoC */
169static inline void dccimvac(unsigned long mva)
170{
David Hendricks08e36562013-03-25 15:02:29 -0700171 asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva) : "memory");
David Hendricks2fba5e22013-03-14 19:06:11 -0700172}
173
174/* data cache invalidate by set/way */
175static inline void dccisw(uint32_t val)
176{
David Hendricks08e36562013-03-25 15:02:29 -0700177 asm volatile ("mcr p15, 0, %0, c7, c14, 2" : : "r" (val) : "memory");
David Hendricks2fba5e22013-03-14 19:06:11 -0700178}
179
180/* data cache clean by MVA to PoC */
181static inline void dccmvac(unsigned long mva)
182{
David Hendricks08e36562013-03-25 15:02:29 -0700183 asm volatile ("mcr p15, 0, %0, c7, c10, 1" : : "r" (mva) : "memory");
David Hendricks2fba5e22013-03-14 19:06:11 -0700184}
185
David Hendricksb98ab4a2013-08-16 12:17:50 -0700186/* data cache clean by set/way */
187static inline void dccsw(uint32_t val)
188{
189 asm volatile ("mcr p15, 0, %0, c7, c10, 2" : : "r" (val) : "memory");
190}
191
David Hendricks2fba5e22013-03-14 19:06:11 -0700192/* data cache invalidate by MVA to PoC */
193static inline void dcimvac(unsigned long mva)
194{
David Hendricks08e36562013-03-25 15:02:29 -0700195 asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (mva) : "memory");
196}
197
198/* data cache invalidate by set/way */
199static inline void dcisw(uint32_t val)
200{
201 asm volatile ("mcr p15, 0, %0, c7, c6, 2" : : "r" (val) : "memory");
David Hendricks2fba5e22013-03-14 19:06:11 -0700202}
203
204/* instruction cache invalidate all by PoU */
205static inline void iciallu(void)
206{
207 asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
208}
209
210/*
211 * Cache co-processor (CP15) access functions
212 */
213
214/* read cache level ID register (CLIDR) */
215static inline uint32_t read_clidr(void)
216{
217 uint32_t val = 0;
218 asm volatile ("mrc p15, 1, %0, c0, c0, 1" : "=r" (val));
219 return val;
220}
221
222/* read cache size ID register register (CCSIDR) */
223static inline uint32_t read_ccsidr(void)
224{
225 uint32_t val = 0;
226 asm volatile ("mrc p15, 1, %0, c0, c0, 0" : "=r" (val));
227 return val;
228}
229
230/* read cache size selection register (CSSELR) */
231static inline uint32_t read_csselr(void)
232{
233 uint32_t val = 0;
234 asm volatile ("mrc p15, 2, %0, c0, c0, 0" : "=r" (val));
235 return val;
236}
237
238/* write to cache size selection register (CSSELR) */
239static inline void write_csselr(uint32_t val)
240{
241 /*
242 * Bits [3:1] - Cache level + 1 (0b000 = L1, 0b110 = L7, 0b111 is rsvd)
243 * Bit 0 - 0 = data or unified cache, 1 = instruction cache
244 */
245 asm volatile ("mcr p15, 2, %0, c0, c0, 0" : : "r" (val));
246 isb(); /* ISB to sync the change to CCSIDR */
247}
248
David Hendricks6119bea2013-03-29 13:24:29 -0700249/* read L2 control register (L2CTLR) */
250static inline uint32_t read_l2ctlr(void)
David Hendricks2fba5e22013-03-14 19:06:11 -0700251{
David Hendricks6119bea2013-03-29 13:24:29 -0700252 uint32_t val = 0;
253 asm volatile ("mrc p15, 1, %0, c9, c0, 2" : "=r" (val));
254 return val;
255}
256
257/* write L2 control register (L2CTLR) */
258static inline void write_l2ctlr(uint32_t val)
259{
260 /*
261 * Note: L2CTLR can only be written when the L2 memory system
262 * is idle, ie before the MMU is enabled.
263 */
264 asm volatile("mcr p15, 1, %0, c9, c0, 2" : : "r" (val) : "memory" );
265 isb();
266}
267
David Hendricks36e62c22013-08-06 17:32:41 -0700268/* read L2 Auxiliary Control Register (L2ACTLR) */
269static inline uint32_t read_l2actlr(void)
270{
271 uint32_t val = 0;
272 asm volatile ("mrc p15, 1, %0, c15, c0, 0" : "=r" (val));
273 return val;
274}
275
276/* write L2 Auxiliary Control Register (L2ACTLR) */
277static inline void write_l2actlr(uint32_t val)
278{
279 asm volatile ("mcr p15, 1, %0, c15, c0, 0" : : "r" (val) : "memory" );
280 isb();
281}
282
David Hendricks6119bea2013-03-29 13:24:29 -0700283/* read system control register (SCTLR) */
284static inline uint32_t read_sctlr(void)
285{
286 uint32_t val;
David Hendricks08e36562013-03-25 15:02:29 -0700287 asm volatile ("mrc p15, 0, %0, c1, c0, 0" : "=r" (val));
David Hendricks2fba5e22013-03-14 19:06:11 -0700288 return val;
289}
290
291/* write system control register (SCTLR) */
David Hendricks08e36562013-03-25 15:02:29 -0700292static inline void write_sctlr(uint32_t val)
David Hendricks2fba5e22013-03-14 19:06:11 -0700293{
294 asm volatile ("mcr p15, 0, %0, c1, c0, 0" : : "r" (val) : "cc");
295 isb();
296}
297
Julius Wernerb2b71322014-10-16 10:23:36 -0700298/* read data fault address register (DFAR) */
299static inline uint32_t read_dfar(void)
300{
301 uint32_t val;
302 asm volatile ("mrc p15, 0, %0, c6, c0, 0" : "=r" (val));
303 return val;
304}
305
306/* read data fault status register (DFSR) */
307static inline uint32_t read_dfsr(void)
308{
309 uint32_t val;
310 asm volatile ("mrc p15, 0, %0, c5, c0, 0" : "=r" (val));
311 return val;
312}
313
314/* read instruction fault address register (IFAR) */
315static inline uint32_t read_ifar(void)
316{
317 uint32_t val;
318 asm volatile ("mrc p15, 0, %0, c6, c0, 2" : "=r" (val));
319 return val;
320}
321
322/* read instruction fault status register (IFSR) */
323static inline uint32_t read_ifsr(void)
324{
325 uint32_t val;
326 asm volatile ("mrc p15, 0, %0, c5, c0, 1" : "=r" (val));
327 return val;
328}
329
330/* read auxiliary data fault status register (ADFSR) */
331static inline uint32_t read_adfsr(void)
332{
333 uint32_t val;
334 asm volatile ("mrc p15, 0, %0, c5, c1, 0" : "=r" (val));
335 return val;
336}
337
338/* read auxiliary instruction fault status register (AIFSR) */
339static inline uint32_t read_aifsr(void)
340{
341 uint32_t val;
342 asm volatile ("mrc p15, 0, %0, c5, c1, 1" : "=r" (val));
343 return val;
344}
345
David Hendricks2fba5e22013-03-14 19:06:11 -0700346/*
347 * Cache maintenance API
348 */
349
David Hendricks08e36562013-03-25 15:02:29 -0700350/* dcache clean and invalidate all (on current level given by CCSELR) */
David Hendricks2fba5e22013-03-14 19:06:11 -0700351void dcache_clean_invalidate_all(void);
352
David Hendricks08e36562013-03-25 15:02:29 -0700353/* dcache clean by modified virtual address to PoC */
Julius Wernerf09f2242013-08-28 14:43:14 -0700354void dcache_clean_by_mva(void const *addr, size_t len);
David Hendricks2fba5e22013-03-14 19:06:11 -0700355
David Hendricks08e36562013-03-25 15:02:29 -0700356/* dcache clean and invalidate by modified virtual address to PoC */
Julius Wernerf09f2242013-08-28 14:43:14 -0700357void dcache_clean_invalidate_by_mva(void const *addr, size_t len);
David Hendricks2fba5e22013-03-14 19:06:11 -0700358
Hung-Te Lincb0aeef2013-07-08 12:27:13 +0800359/* dcache invalidate by modified virtual address to PoC */
Julius Wernerf09f2242013-08-28 14:43:14 -0700360void dcache_invalidate_by_mva(void const *addr, size_t len);
Hung-Te Lincb0aeef2013-07-08 12:27:13 +0800361
David Hendricksb98ab4a2013-08-16 12:17:50 -0700362void dcache_clean_all(void);
363
David Hendricks08e36562013-03-25 15:02:29 -0700364/* dcache invalidate all (on current level given by CCSELR) */
365void dcache_invalidate_all(void);
366
Gabe Blackd40be112013-10-09 23:45:07 -0700367/* returns number of bytes per cache line */
368unsigned int dcache_line_bytes(void);
369
David Hendricks08e36562013-03-25 15:02:29 -0700370/* dcache and MMU disable */
371void dcache_mmu_disable(void);
372
373/* dcache and MMU enable */
374void dcache_mmu_enable(void);
375
Julius Wernerfd9defc2014-01-21 20:11:22 -0800376/* perform all icache/dcache maintenance needed after loading new code */
377void cache_sync_instructions(void);
David Hendricks2fba5e22013-03-14 19:06:11 -0700378
David Hendricks08e36562013-03-25 15:02:29 -0700379/* tlb invalidate all */
380void tlb_invalidate_all(void);
381
382/*
383 * Generalized setup/init functions
384 */
385
Julius Werner03a0a652015-09-29 17:28:15 -0700386/* MMU initialization (set page table base, permissions, initialize subtable
387 * buffer, etc.). Must only be called ONCE PER BOOT, before any mappings. */
David Hendricks08e36562013-03-25 15:02:29 -0700388void mmu_init(void);
389
390enum dcache_policy {
391 DCACHE_OFF,
392 DCACHE_WRITEBACK,
393 DCACHE_WRITETHROUGH,
394};
395
David Hendricksb98ab4a2013-08-16 12:17:50 -0700396/* disable the mmu for a range. Primarily useful to lock out address 0. */
Julius Werner108548a2014-10-09 17:31:45 -0700397void mmu_disable_range(u32 start_mb, u32 size_mb);
David Hendricks08e36562013-03-25 15:02:29 -0700398/* mmu range configuration (set dcache policy) */
Julius Werner108548a2014-10-09 17:31:45 -0700399void mmu_config_range(u32 start_mb, u32 size_mb, enum dcache_policy policy);
400
401/* Reconfigure memory mappings at the fine-grained (4K) page level. Must be
402 * called on a range contained within a single, already mapped block/superpage.
403 * Careful: Do NOT map over this address range with mmu_config_range() again
404 * later, or you will leak resources and may desync your TLB! */
405void mmu_config_range_kb(u32 start_kb, u32 size_kb, enum dcache_policy policy);
Deepa Dinamanie1977482015-01-28 14:15:56 -0800406void mmu_disable_range_kb(u32 start_kb, u32 size_kb);
David Hendricks2fba5e22013-03-14 19:06:11 -0700407
Gabe Black51edd542013-09-30 23:00:33 -0700408#endif /* ARM_CACHE_H */