SyterKit 0.4.0.x
SyterKit is a bare-metal framework
Loading...
Searching...
No Matches
cache.h
Go to the documentation of this file.
1
8/* SPDX-License-Identifier: GPL-2.0+ */
9
10#ifndef __CACHE_H__
11#define __CACHE_H__
12
13#include <stdint.h>
14#include "barrier.h"
15
21static inline void arm32_dcache_enable(void);
22
28static inline void arm32_dcache_disable(void);
29
35static inline void arm32_icache_enable(void);
36
42static inline void arm32_icache_disable(void);
43
57static inline void flush_dcache_range(uint64_t start, uint64_t end);
58
73static inline void invalidate_dcache_range(uint64_t start, uint64_t end);
74
81static inline void flush_dcache_all();
82
90static inline void invalidate_dcache_all();
91
99static inline void data_sync_barrier(void) {
100 dsb();
101}
102
103/* Function implementations */
104
105static inline void arm32_dcache_enable(void) {
107 __asm__ __volatile__("mrc p15, 0, %0, c1, c0, 0"
108 : "=r"(value)
109 :
110 : "memory");
111 value |= (1 << 2);
112 __asm__ __volatile__("mcr p15, 0, %0, c1, c0, 0"
113 :
114 : "r"(value)
115 : "memory");
116 __asm__ __volatile__("mrc p15, 0, %0, c1, c0, 0"
117 : "=r"(value)
118 :
119 : "memory");
120}
121
122static inline void arm32_dcache_disable(void) {
124 __asm__ __volatile__("mrc p15, 0, %0, c1, c0, 0"
125 : "=r"(value)
126 :
127 : "memory");
128 value &= ~(1 << 2);
129 __asm__ __volatile__("mcr p15, 0, %0, c1, c0, 0"
130 :
131 : "r"(value)
132 : "memory");
133 __asm__ __volatile__("mrc p15, 0, %0, c1, c0, 0"
134 : "=r"(value)
135 :
136 : "memory");
137}
138
139static inline void arm32_icache_enable(void) {
141 __asm__ __volatile__("mrc p15, 0, %0, c1, c0, 0"
142 : "=r"(value)
143 :
144 : "memory");
145 value |= (1 << 12);
146 __asm__ __volatile__("mcr p15, 0, %0, c1, c0, 0"
147 :
148 : "r"(value)
149 : "memory");
150 __asm__ __volatile__("mrc p15, 0, %0, c1, c0, 0"
151 : "=r"(value)
152 :
153 : "memory");
154}
155
156static inline void arm32_icache_disable(void) {
158 __asm__ __volatile__("mrc p15, 0, %0, c1, c0, 0"
159 : "=r"(value)
160 :
161 : "memory");
162 value &= ~(1 << 12);
163 __asm__ __volatile__("mcr p15, 0, %0, c1, c0, 0"
164 :
165 : "r"(value)
166 : "memory");
167 __asm__ __volatile__("mrc p15, 0, %0, c1, c0, 0"
168 : "=r"(value)
169 :
170 : "memory");
171}
172
173static inline void flush_dcache_range(uint64_t start, uint64_t end) {
174 /* Ensure addresses are cache line aligned */
175 uint32_t line_size = 32; /* Assume 32-byte cache line size, may need adjustment for specific processors */
176 uint32_t aligned_start = (uint32_t) start & ~(line_size - 1);
177 uint32_t aligned_end = ((uint32_t) end + line_size - 1) & ~(line_size - 1);
178 uint32_t addr;
179
180 /* Iterate through cache lines and flush */
181 for (addr = aligned_start; addr < aligned_end; addr += line_size) {
182 __asm__ __volatile__(
183 "mcr p15, 0, %0, c7, c14, 1" /* Clean data cache line */
184 :
185 : "r"(addr)
186 : "memory");
187 }
188
189 /* Ensure all operations are complete */
191}
192
193static inline void invalidate_dcache_range(uint64_t start, uint64_t end) {
194 /* Ensure addresses are cache line aligned */
195 uint32_t line_size = 32; /* Assume 32-byte cache line size */
196 uint32_t aligned_start = (uint32_t) start & ~(line_size - 1);
197 uint32_t aligned_end = ((uint32_t) end + line_size - 1) & ~(line_size - 1);
198 uint32_t addr;
199
200 /* Iterate through cache lines and invalidate */
201 for (addr = aligned_start; addr < aligned_end; addr += line_size) {
202 __asm__ __volatile__(
203 "mcr p15, 0, %0, c7, c6, 1" /* Invalidate data cache line */
204 :
205 : "r"(addr)
206 : "memory");
207 }
208
209 /* Ensure all operations are complete */
211}
212
213static inline void flush_dcache_all() {
214 /* Ensure all operations are complete */
216}
217
218static inline void invalidate_dcache_all() {
219 /* Ensure all operations are complete */
221}
222
223#endif /* __CACHE_H__ */
#define dsb()
Definition barrier.h:41
static void invalidate_dcache_all()
Invalidate the entire data cache.
Definition cache.h:218
static void flush_dcache_range(uint64_t start, uint64_t end)
Flush a range of addresses from the data cache.
Definition cache.h:173
static void arm32_dcache_enable(void)
Enable the ARM32 data cache.
Definition cache.h:105
static void arm32_icache_enable(void)
Enable the ARM32 instruction cache.
Definition cache.h:139
static void arm32_icache_disable(void)
Disable the ARM32 instruction cache.
Definition cache.h:156
static void invalidate_dcache_range(uint64_t start, uint64_t end)
Invalidate a range of addresses in the data cache.
Definition cache.h:193
static void data_sync_barrier(void)
Insert a data synchronization barrier.
Definition cache.h:99
static void flush_dcache_all()
Flush (clean) the entire data cache.
Definition cache.h:213
static void arm32_dcache_disable(void)
Disable the ARM32 data cache.
Definition cache.h:122
u64_t uint64_t
Definition stdint.h:16
u32_t uint32_t
Definition stdint.h:13
static uint8_t value
Definition io.h:144
Memory barrier definitions for RISC-V architecture.