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 arch/dreamcast/include/cache.h
4
5 Copyright (C) 2001 Megan Potter
6 Copyright (C) 2014, 2016, 2023 Ruslan Rostovtsev
7 Copyright (C) 2023 Andy Barajas
8 Copyright (C) 2025 Eric Fradella
9 Copyright (C) 2026 Falco Girgis
10*/
11
12/** \file arch/cache.h
13 \brief Cache management functionality.
14 \ingroup system_cache
15
16 This file contains definitions for functions that manage the cache in the
17 Dreamcast, including functions to flush, invalidate, purge, prefetch and
18 allocate the caches.
19
20 \author Megan Potter
21 \author Ruslan Rostovtsev
22 \author Andy Barajas
23 \author Falco Girgis
24*/
25
26#ifndef __ARCH_CACHE_H
27#define __ARCH_CACHE_H
28
29#include <kos/cdefs.h>
30__BEGIN_DECLS
31
32#include <stdint.h>
33
34/** \defgroup system_cache Cache
35 \brief Driver and API for managing the SH4's cache
36 \ingroup system
37
38 @{
39*/
40
41/** \brief Level 1 instruction cache size.
42
43 The capacity of the L1 instruction cache in bytes.
44*/
45#define CACHE_L1_ICACHE_SIZE 8 * 1024
46
47/** \brief Level 1 instruction cache associativity.
48
49 Number of ways in the L1 instruction cache.
50*/
51#define CACHE_L1_ICACHE_ASSOC 1
52
53/** \brief L1 instruction cache line size.
54
55 The size of each cache line in the L1 instruction cache.
56*/
57#define CACHE_L1_ICACHE_LINESIZE 32
58
59/** \brief Level 1 data cache size.
60
61 The capacity of the L1 data cache in bytes.
62*/
63#define CACHE_L1_DCACHE_SIZE 16 * 1024
64
65/** \brief Level 1 data cache associativity.
66
67 Number of ways in the L1 data cache.
68*/
69#define CACHE_L1_DCACHE_ASSOC 1
70
71/** \brief L1 data cache line size.
72
73 The size of each cache line in the L1 data cache.
74*/
75#define CACHE_L1_DCACHE_LINESIZE 32
76
77/** \brief Level 2 cache size.
78
79 The capacity of the L2 cache in bytes.
80*/
81#define CACHE_L2_CACHE_SIZE 0
82
83/** \brief Level 2 cache associativity.
84
85 Number of ways in the L2 cache.
86*/
87#define CACHE_L2_CACHE_ASSOC 0
88
89/** \brief Level 2 cache line size.
90
91 The size of each cache line in the L2 cache.
92*/
93#define CACHE_L2_CACHE_LINESIZE 0
94
95/** \brief Invalidate the instruction cache.
96
97 This instruction invalidates a range of the instruction cache.
98
99 \warning
100 If you care aobut the contents of the cache that have not been written
101 back yet, use icache_flush_range() instead!
102
103 \param start The physical address to begin invalidation at.
104 \param count The number of bytes to invalidate.
105*/
106void icache_inval_range(uintptr_t start, size_t count);
107
108/** \brief Flush the instruction cache.
109
110 This function flushes a range of the instruction cache.
111
112 \param start The physical address to begin flushing at.
113 \param count The number of bytes to flush.
114*/
115void icache_flush_range(uintptr_t start, size_t count);
116
117/** \brief Invalidate the data/operand cache.
118
119 This function invalidates a range of the data/operand cache. If you care
120 about the contents of the cache that have not been written back yet, use
121 dcache_flush_range() before using this function.
122
123 \param start The physical address to begin invalidating at.
124 \param count The number of bytes to invalidate.
125*/
126void dcache_inval_range(uintptr_t start, size_t count);
127
128/** \brief Flush the data/operand cache.
129
130 This function flushes a range of the data/operand cache, forcing a write-
131 back on all of the data in the specified range. This does not invalidate
132 the cache in the process (meaning the blocks will still be in the cache,
133 just not marked as dirty after this has completed). If you wish to
134 invalidate the cache as well, call dcache_inval_range() after calling this
135 function or use dcache_purge_range() instead of dcache_flush_range().
136
137 \param start The physical address to begin flushing at.
138 \param count The number of bytes to flush.
139*/
140void dcache_flush_range(uintptr_t start, size_t count);
141
142/** \brief Flush all the data/operand cache.
143
144 This function flushes all the data/operand cache, forcing a write-
145 back on all of the cache blocks that are marked as dirty.
146
147 \note
148 dcache_flush_range() is faster than dcache_flush_all() if the count
149 param is 66560 or less.
150*/
152
153/** \brief Purge the data/operand cache.
154
155 This function flushes a range of the data/operand cache, forcing a write-
156 back and then invalidates all of the data in the specified range.
157
158 \param start The physical address to begin purging at.
159 \param count The number of bytes to purge.
160*/
161void dcache_purge_range(uintptr_t start, size_t count);
162
163/** \brief Purge all the data/operand cache.
164
165 This function flushes the entire data/operand cache, ensuring that all
166 cache blocks marked as dirty are written back to memory and all cache
167 entries are invalidated. It does not require an additional buffer and is
168 preferred when memory resources are constrained.
169
170 \note
171 dcache_purge_range() is faster than dcache_purge_all() if the count
172 param is 39936 or less.
173*/
175
176/** \brief Purge all the data/operand cache with buffer.
177
178 This function performs a purge of all data/operand cache blocks by
179 utilizing an external buffer to speed up the write-back and invalidation
180 process. It is always faster than dcache_purge_all() and is recommended
181 where maximum speed is required.
182
183 \note While this function offers a superior purge rate, it does require
184 the use of a temporary buffer. So use this function if you have an extra
185 8/16 kb of memory laying around that you can utilize for no other purpose
186 than for this function.
187
188 \param start The physical address for temporary buffer (32-byte
189 aligned)
190 \param count The size of the temporary buffer, which can be
191 either 8 KB or 16 KB, depending on cache
192 configuration - 8 KB buffer with OCRAM enabled,
193 otherwise 16 KB.
194
195*/
196void dcache_purge_all_with_buffer(uintptr_t start, size_t count);
197
198/** \brief Prefetch one block to the data/operand cache.
199
200 This function prefetch a block of the data/operand cache.
201
202 \param src The physical address to prefetch.
203*/
204static __always_inline void dcache_pref_block(const void *src) {
205 __asm__ __volatile__("pref @%0\n"
206 : /* No outputs */
207 : "r" (src)
208 : /* No clobbers */
209 );
210}
211
212/** \brief Write-back Store Queue buffer to external memory
213
214 This function initiates write-back for one Store Queue.
215
216 \param src The SQ mapped address to write-back.
217*/
218static __always_inline void dcache_wback_sq(void *src) {
219 __asm__ __volatile__("pref @%0\n"
220 : /* No outputs */
221 : "r" (src)
222 : "memory"
223 );
224}
225
226/** \brief Allocate one block of the data/operand cache.
227
228 This function allocate a block of the data/operand cache.
229
230 \param src The address to allocate (32-byte aligned)
231 \param value The value written to first 4-byte.
232*/
233static __always_inline void dcache_alloc_block(void *src, uint32_t value) {
234 uint32_t *src32 = (uint32_t *)src;
235
236 __asm__ ("movca.l r0, @%8\n\t"
237 : "=m"(src32[0]),
238 "=m"(src32[1]),
239 "=m"(src32[2]),
240 "=m"(src32[3]),
241 "=m"(src32[4]),
242 "=m"(src32[5]),
243 "=m"(src32[6]),
244 "=m"(src32[7])
245 : "r" (src32), "z" (value)
246 );
247}
248
249/** @} */
250
251__END_DECLS
252
253#endif /* __ARCH_CACHE_H */
Various common macros used throughout the codebase.
static __always_inline void dcache_alloc_block(void *src, uint32_t value)
Allocate one block of the data/operand cache.
Definition cache.h:233
void dcache_inval_range(uintptr_t start, size_t count)
Invalidate the data/operand cache.
void dcache_purge_all(void)
Purge all the data/operand cache.
void icache_flush_range(uintptr_t start, size_t count)
Flush the instruction cache.
static __always_inline void dcache_pref_block(const void *src)
Prefetch one block to the data/operand cache.
Definition cache.h:204
void icache_inval_range(uintptr_t start, size_t count)
Invalidate the instruction cache.
void dcache_purge_range(uintptr_t start, size_t count)
Purge the data/operand cache.
static __always_inline void dcache_wback_sq(void *src)
Write-back Store Queue buffer to external memory.
Definition cache.h:218
void dcache_flush_all(void)
Flush all the data/operand cache.
void dcache_purge_all_with_buffer(uintptr_t start, size_t count)
Purge all the data/operand cache with buffer.
void dcache_flush_range(uintptr_t start, size_t count)
Flush the data/operand cache.
static void start(char *fn)
Definition songmenu.c:239