KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
cache.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 include/kos/cache.h
4 Copyright (C) 2001 Megan Potter
5 Copyright (C) 2014, 2016, 2023 Ruslan Rostovtsev
6 Copyright (C) 2023 Andy Barajas
7 Copyright (C) 2025 Eric Fradella
8 Copyright (C) 2026 Falco Girgis
9*/
10
11/** \file include/kos/cache.h
12 \brief Cache management functionality.
13 \ingroup system_cache
14
15 This file contains definitions for functions that manage the caches,
16 including functions to flush, invalidate, purge, prefetch and allocate
17 individual cache lines or address ranges.
18
19 \author Megan Potter
20 \author Ruslan Rostovtsev
21 \author Andy Barajas
22 \author Falco Girgis
23*/
24
25#ifndef __KOS_CACHE_H
26#define __KOS_CACHE_H
27
28#include <kos/cdefs.h>
29__BEGIN_DECLS
30
31#include <arch/cache.h>
32
33#include <stdint.h>
34
35/** \defgroup system_cache Cache
36 \brief Driver and API for managing the SH4's cache
37 \ingroup system
38
39 @{
40*/
41
42/** \brief Level 1 instruction cache size.
43
44 The capacity of the L1 instruction cache in bytes.
45*/
46#define CACHE_L1_ICACHE_SIZE \
47 ARCH_CACHE_L1_ICACHE_SIZE
48
49/** \brief Level 1 instruction cache associativity.
50
51 Number of ways in the L1 instruction cache.
52*/
53#define CACHE_L1_ICACHE_ASSOC \
54 ARCH_CACHE_L1_ICACHE_ASSOC
55
56/** \brief L1 instruction cache line size.
57
58 The size of each cache line in the L1 instruction cache.
59*/
60#define CACHE_L1_ICACHE_LINESIZE \
61 ARCH_CACHE_L1_ICACHE_LINESIZE
62
63/** \brief Level 1 data cache size.
64
65 The capacity of the L1 data cache in bytes.
66*/
67#define CACHE_L1_DCACHE_SIZE \
68 ARCH_CACHE_L1_DCACHE_SIZE
69
70/** \brief Level 1 data cache associativity.
71
72 Number of ways in the L1 data cache.
73*/
74#define CACHE_L1_DCACHE_ASSOC \
75 ARCH_CACHE_L1_DCACHE_ASSOC
76
77/** \brief L1 data cache line size.
78
79 The size of each cache line in the L1 data cache.
80*/
81#define CACHE_L1_DCACHE_LINESIZE \
82 ARCH_CACHE_L1_DCACHE_LINESIZE
83
84/** \brief Level 2 cache size.
85
86 The capacity of the L2 cache in bytes.
87*/
88#define CACHE_L2_CACHE_SIZE \
89 ARCH_CACHE_L2_CACHE_SIZE
90
91/** \brief Level 2 cache associativity.
92
93 Number of ways in the L2 cache.
94*/
95#define CACHE_L2_CACHE_ASSOC \
96 ARCH_CACHE_L2_CACHE_ASSOC
97
98/** \brief Level 2 cache line size.
99
100 The size of each cache line in the L2 cache.
101*/
102#define CACHE_L2_CACHE_LINESIZE \
103 ARCH_CACHE_L2_CACHE_LINESIZE
104
105/** \brief Invalidate the instruction cache.
106
107 This instruction invalidates a range of the instruction cache.
108
109 \param start The physical address to begin invalidation at.
110 \param count The number of bytes to invalidate.
111*/
112static inline void icache_inval_range(uintptr_t start, size_t count) {
114}
115
116/** \brief Synchronize the instruction cache.
117
118 This function ensures that the instruction cache is synchronized with the
119 data/operand cache. It is functionally the same as calling
120 dcache_wback_range() followed by icache_inval_range(), but may be
121 implemented in a more optimized way.
122
123 \param start The physical address to begin invalidation at.
124 \param count The number of bytes to invalidate.
125*/
126static inline void icache_sync_range(uintptr_t start, size_t count) {
128}
129
130/** \brief Invalidate the data/operand cache.
131
132 This function invalidates a range of the data/operand cache. If you care
133 about the contents of the cache that have not been written back yet, use
134 dcache_wback_range() before using this function.
135
136 \param start The physical address to begin invalidating at.
137 \param count The number of bytes to invalidate.
138*/
139static inline void dcache_inval_range(uintptr_t start, size_t count) {
141}
142
143/** \brief Perform a write-back on the data/operand cache.
144
145 This function flushes a range of the data/operand cache, forcing a write-
146 back on all of the data in the specified range. This does not invalidate
147 the cache in the process (meaning the blocks will still be in the cache,
148 just not marked as dirty after this has completed). If you wish to
149 invalidate the cache as well, call dcache_inval_range() after calling this
150 function or use dcache_purge_range() instead of dcache_wback_range().
151
152 \param start The physical address to begin flushing at.
153 \param count The number of bytes to write back.
154*/
155static inline void dcache_wback_range(uintptr_t start, size_t count) {
157}
158
159/** \brief Perform a write-back on the the whole data/operand cache.
160
161 This function flushes all the data/operand cache, forcing a write-
162 back on all of the cache blocks that are marked as dirty.
163*/
164static inline void dcache_wback_all(void) {
166}
167
168/** \brief Purge the data/operand cache.
169
170 This function flushes a range of the data/operand cache, forcing a write-
171 back and then invalidates all of the data in the specified range.
172
173 \param start The physical address to begin purging at.
174 \param count The number of bytes to purge.
175*/
176static inline void dcache_purge_range(uintptr_t start, size_t count) {
178}
179
180/** \brief Purge all the data/operand cache.
181
182 This function flushes the entire data/operand cache, ensuring that all
183 cache blocks marked as dirty are written back to memory and all cache
184 entries are invalidated. It does not require an additional buffer and is
185 preferred when memory resources are constrained.
186*/
187static inline void dcache_purge_all(void) {
189}
190
191/** \brief Prefetch one block to the data/operand cache.
192
193 This function prefetch a block of the data/operand cache.
194
195 \param src The physical address to prefetch.
196*/
197static inline void dcache_pref_line(const void *src) {
199}
200
201/** \brief Allocate one cache line of the data/operand cache.
202
203 This function allocates a cache line of the data/operand cache.
204 The initial content of the allocated cache line is undefined.
205
206 \param src The address to allocate (32-byte aligned)
207*/
208static inline void dcache_alloc_line(void *src) {
210}
211
212/** \brief Zero-allocate one cache line of the data/operand cache.
213
214 This function allocates a cache line of the data/operand cache.
215 The allocated cache line will be zeroed.
216
217 \param src The address to allocate (32-byte aligned)
218*/
219static inline void dcache_zero_alloc_line(void *src) {
221}
222
223/** \brief Allocate one cache line of the data/operand cache with a value.
224
225 This function allocates a cache line of the data/operand cache.
226 The specified value is written at the beginning of the cache line.
227 The initial content of the rest of the cache line is undefined.
228
229 \param src The address to allocate (32-byte aligned)
230 \param value The value written to the beginning of the cache line.
231*/
232static inline void dcache_alloc_line_with_value(void *src, uintptr_t value) {
234}
235
236/** \brief Invalidate one cache line of the data/operand cache.
237
238 This function invalidates a cache line of the data/operand cache.
239 The data inside the cache line is not written back to memory, and
240 the cache line is marked as free.
241
242 \param src The address to invalidate
243*/
244static inline void dcache_inval_line(void *src) {
246}
247
248/** \brief Purge one cache line of the data/operand cache.
249
250 This function purges a cache line of the data/operand cache.
251 If the cache line is dirty, the data is written back to memory, and
252 then the cache line is marked as free.
253
254 \param src The address to purge
255*/
256static inline void dcache_purge_line(void *src) {
258}
259
260/** \brief Write-back one cache line of the data/operand cache.
261
262 This function flushes a cache line of the data/operand cache.
263 If the cache line is dirty, the data is written back to memory.
264 The cache line is not invalidated.
265
266 \param src The address to flush
267*/
268static inline void dcache_wback_line(void *src) {
270}
271
272/** \cond */
273__depr("icache_flush_range() has been renamed to icache_sync_range()")
274static inline void icache_flush_range(uintptr_t start, size_t count) {
275 icache_sync_range(start, count);
276}
277
278__depr("dcache_flush_range() has been renamed to dcache_wback_range()")
279static inline void dcache_flush_range(uintptr_t start, size_t count) {
281}
282
283__depr("dcache_flush_all() has been renamed to dcache_wback_all()")
284static inline void dcache_flush_all(void) {
286}
287
288__depr("dcache_pref_block() has been renamed to dcache_pref_line()")
289static inline void dcache_pref_block(const void *src) {
290 dcache_pref_line(src);
291}
292
293__depr("dcache_alloc_block() has been renamed to dcache_alloc_line_with_value()")
294static inline void dcache_alloc_block(void *src, uint32_t value) {
296}
297/** \endcond */
298
299/** @} */
300
301__END_DECLS
302
303#endif /* __KOS_CACHE_H */
Various common macros used throughout the codebase.
static void dcache_inval_line(void *src)
Invalidate one cache line of the data/operand cache.
Definition cache.h:244
static void icache_sync_range(uintptr_t start, size_t count)
Synchronize the instruction cache.
Definition cache.h:126
static void dcache_alloc_line(void *src)
Allocate one cache line of the data/operand cache.
Definition cache.h:208
static void dcache_purge_line(void *src)
Purge one cache line of the data/operand cache.
Definition cache.h:256
static void dcache_pref_line(const void *src)
Prefetch one block to the data/operand cache.
Definition cache.h:197
static void dcache_zero_alloc_line(void *src)
Zero-allocate one cache line of the data/operand cache.
Definition cache.h:219
static void dcache_alloc_line_with_value(void *src, uintptr_t value)
Allocate one cache line of the data/operand cache with a value.
Definition cache.h:232
static void dcache_wback_all(void)
Perform a write-back on the the whole data/operand cache.
Definition cache.h:164
static void dcache_purge_all(void)
Purge all the data/operand cache.
Definition cache.h:187
static void dcache_inval_range(uintptr_t start, size_t count)
Invalidate the data/operand cache.
Definition cache.h:139
static void dcache_wback_range(uintptr_t start, size_t count)
Perform a write-back on the data/operand cache.
Definition cache.h:155
static void dcache_purge_range(uintptr_t start, size_t count)
Purge the data/operand cache.
Definition cache.h:176
static void icache_inval_range(uintptr_t start, size_t count)
Invalidate the instruction cache.
Definition cache.h:112
static void dcache_wback_line(void *src)
Write-back one cache line of the data/operand cache.
Definition cache.h:268
#define inline
Definition cdefs.h:107
Cache management functionality.
void arch_icache_sync_range(uintptr_t start, size_t count)
void arch_icache_inval_range(uintptr_t start, size_t count)
static void arch_dcache_wback_range(uintptr_t start, size_t count)
Definition cache.h:167
static void arch_dcache_purge_range(uintptr_t start, size_t count)
Definition cache.h:203
static void arch_dcache_alloc_line(void *src)
Definition cache.h:83
static void arch_dcache_alloc_line_with_value(void *src, uintptr_t value)
Definition cache.h:67
static void arch_dcache_wback_all(void)
Definition cache.h:159
static void arch_dcache_inval_line(void *src)
Definition cache.h:102
static void arch_dcache_wback_line(void *src)
Definition cache.h:134
static void arch_dcache_zero_alloc_line(void *src)
Definition cache.h:94
static void arch_dcache_purge_line(void *src)
Definition cache.h:118
static void arch_dcache_inval_range(uintptr_t start, size_t count)
Definition cache.h:150
static void arch_dcache_pref_line(const void *src)
Definition cache.h:63
static void arch_dcache_purge_all(void)
Definition cache.h:182
static void start(char *fn)
Definition songmenu.c:239