coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
cache.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * cache.c: Cache maintenance routines for ARMv7-A and ARMv7-R
4  *
5  * Reference: ARM Architecture Reference Manual, ARMv7-A and ARMv7-R edition
6  */
7 
8 #include <stdint.h>
9 
10 #include <arch/cache.h>
11 #include <program_loading.h>
12 
14 {
15  /* TLBIALL includes dTLB and iTLB on systems that have them. */
16  tlbiall();
17  dsb();
18  isb();
19 }
20 
21 enum dcache_op {
28 };
29 
30 unsigned int dcache_line_bytes(void)
31 {
32  uint32_t ccsidr;
33  static unsigned int line_bytes = 0;
34 
35  if (line_bytes)
36  return line_bytes;
37 
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 */
42 
43  return line_bytes;
44 }
45 
46 /*
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
50  * entire cache.
51  */
52 static void dcache_op_mva(void const *addr, size_t len, enum dcache_op op)
53 {
54  unsigned long line, linesize;
55 
56  linesize = dcache_line_bytes();
57  line = (uint32_t)addr & ~(linesize - 1);
58 
59  dsb();
60  while ((void *)line < addr + len) {
61  switch (op) {
62  case OP_DCCIMVAC:
63  dccimvac(line);
64  break;
65  case OP_DCCMVAC:
66  dccmvac(line);
67  break;
68  case OP_DCIMVAC:
69  dcimvac(line);
70  break;
71  default:
72  break;
73  }
74  line += linesize;
75  }
76  isb();
77 }
78 
79 void dcache_clean_by_mva(void const *addr, size_t len)
80 {
82 }
83 
84 void dcache_clean_invalidate_by_mva(void const *addr, size_t len)
85 {
87 }
88 
89 void dcache_invalidate_by_mva(void const *addr, size_t len)
90 {
92 }
93 
94 /*
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.
98  */
100 {
101  uint32_t sctlr;
102 
104  sctlr = read_sctlr();
105  sctlr &= ~(SCTLR_C | SCTLR_M);
106  write_sctlr(sctlr);
107 }
108 
110 {
111  uint32_t sctlr;
112 
113  sctlr = read_sctlr();
114  sctlr |= SCTLR_C | SCTLR_M;
115  write_sctlr(sctlr);
116 }
117 
119 {
120  uint32_t sctlr;
121 
122  sctlr = read_sctlr();
123 
124  if (sctlr & SCTLR_C)
126  else if (sctlr & SCTLR_I)
128 
129  iciallu(); /* includes BPIALLU (architecturally) */
130  dsb();
131  isb();
132 }
133 
134 /*
135  * For each segment of a program loaded this function is called
136  * to invalidate caches for the addresses of the loaded segment
137  */
138 void arch_segment_loaded(uintptr_t start, size_t size, int flags)
139 {
141 }
void cache_sync_instructions(void)
Definition: cache.c:57
void dcache_invalidate_by_mva(void const *addr, size_t len)
Definition: cache.c:45
void dcache_clean_all(void)
Definition: cache.c:14
void dcache_clean_by_mva(void const *addr, size_t len)
Definition: cache.c:37
void dcache_mmu_enable(void)
Definition: cache.c:53
void dcache_clean_invalidate_by_mva(void const *addr, size_t len)
Definition: cache.c:41
unsigned int dcache_line_bytes(void)
Definition: cache.c:26
void dcache_mmu_disable(void)
Definition: cache.c:49
void dcache_clean_invalidate_all(void)
Definition: cache.c:18
void tlb_invalidate_all(void)
Definition: cache.c:10
void arch_segment_loaded(uintptr_t start, size_t size, int flags)
Definition: cache.c:138
dcache_op
Definition: cache.c:21
@ OP_DCIMVAC
Definition: cache.c:27
@ OP_DCCSW
Definition: cache.c:22
@ OP_DCCIMVAC
Definition: cache.c:25
@ OP_DCCISW
Definition: cache.c:23
@ OP_DCCMVAC
Definition: cache.c:26
@ OP_DCISW
Definition: cache.c:24
static void dcache_op_mva(void const *addr, size_t len, enum dcache_op op)
Definition: cache.c:52
static void tlbiall(void)
Definition: cache.h:70
#define SCTLR_I
Definition: cache.h:23
static void dccimvac(unsigned long mva)
Definition: cache.h:141
static uint32_t read_ccsidr(void)
Definition: cache.h:195
static void write_sctlr(uint32_t val)
Definition: cache.h:264
static void dccmvac(unsigned long mva)
Definition: cache.h:153
#define SCTLR_C
Definition: cache.h:15
#define SCTLR_M
Definition: cache.h:13
static void iciallu(void)
Definition: cache.h:177
static void dcimvac(unsigned long mva)
Definition: cache.h:165
static uint32_t read_sctlr(void)
Definition: cache.h:256
#define isb()
Definition: barrier.h:15
#define dsb()
Definition: barrier.h:16
static u32 addr
Definition: cirrus.c:14
unsigned int uint32_t
Definition: stdint.h:14
unsigned long uintptr_t
Definition: stdint.h:21