1 /* SPDX-License-Identifier: BSD-3-Clause */
3 * cache.c: Cache maintenance routines for ARMv7-A and ARMv7-R
5 * Reference: ARM Architecture Reference Manual, ARMv7-A and ARMv7-R edition
10 #include <arch/cache.h>
11 #include <program_loading.h>
13 void tlb_invalidate_all(void)
15 /* TLBIALL includes dTLB and iTLB on systems that have them. */
30 unsigned int dcache_line_bytes(void)
33 static unsigned int line_bytes
= 0;
38 ccsidr
= read_ccsidr();
39 /* [2:0] - Indicates (Log2(number of words in cache line)) - 2 */
40 line_bytes
= 1 << ((ccsidr
& 0x7) + 2); /* words per line */
41 line_bytes
*= sizeof(unsigned int); /* bytes per line */
47 * Do a dcache operation by modified virtual address. This is useful for
48 * maintaining coherency in drivers which do DMA transfers and only need to
49 * perform cache maintenance on a particular memory range rather than the
52 static void dcache_op_mva(void const *addr
, size_t len
, enum dcache_op op
)
54 unsigned long line
, linesize
;
56 linesize
= dcache_line_bytes();
57 line
= (uint32_t)addr
& ~(linesize
- 1);
60 while ((void *)line
< addr
+ len
) {
79 void dcache_clean_by_mva(void const *addr
, size_t len
)
81 dcache_op_mva(addr
, len
, OP_DCCMVAC
);
84 void dcache_clean_invalidate_by_mva(void const *addr
, size_t len
)
86 dcache_op_mva(addr
, len
, OP_DCCIMVAC
);
89 void dcache_invalidate_by_mva(void const *addr
, size_t len
)
91 dcache_op_mva(addr
, len
, OP_DCIMVAC
);
95 * CAUTION: This implementation assumes that coreboot never uses non-identity
96 * page tables for pages containing executed code. If you ever want to violate
97 * this assumption, have fun figuring out the associated problems on your own.
99 void dcache_mmu_disable(void)
103 dcache_clean_invalidate_all();
104 sctlr
= read_sctlr();
105 sctlr
&= ~(SCTLR_C
| SCTLR_M
);
109 void dcache_mmu_enable(void)
113 sctlr
= read_sctlr();
114 sctlr
|= SCTLR_C
| SCTLR_M
;
118 void cache_sync_instructions(void)
122 sctlr
= read_sctlr();
126 else if (sctlr
& SCTLR_I
)
127 dcache_clean_invalidate_all();
129 iciallu(); /* includes BPIALLU (architecturally) */
135 * For each segment of a program loaded this function is called
136 * to invalidate caches for the addresses of the loaded segment
138 void arch_segment_loaded(uintptr_t start
, size_t size
, int flags
)
140 cache_sync_instructions();