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 Copyright (C) 2001 Megan Potter
5 Copyright (C) 2014, 2016, 2023 Ruslan Rostovtsev
6 Copyright (C) 2023 Andy Barajas
7*/
8
9/** \file arch/cache.h
10 \brief Cache management functionality.
11 \ingroup system_cache
12
13 This file contains definitions for functions that manage the cache in the
14 Dreamcast, including functions to flush, invalidate, purge, prefetch and
15 allocate the caches.
16
17 \author Megan Potter
18 \author Ruslan Rostovtsev
19 \author Andy Barajas
20*/
21
22#ifndef __ARCH_CACHE_H
23#define __ARCH_CACHE_H
24
25#include <sys/cdefs.h>
26__BEGIN_DECLS
27
28#include <stdint.h>
29
30/** \defgroup system_cache Cache
31 \brief Driver and API for managing the SH4's cache
32 \ingroup system
33
34 @{
35*/
36
37/** \brief SH4 cache block size.
38
39 The size of a cache block.
40*/
41#define CPU_CACHE_BLOCK_SIZE 32
42
43/** \brief Flush the instruction cache.
44
45 This function flushes a range of the instruction cache.
46
47 \param start The physical address to begin flushing at.
48 \param count The number of bytes to flush.
49*/
50void icache_flush_range(uintptr_t start, size_t count);
51
52/** \brief Invalidate the data/operand cache.
53
54 This function invalidates a range of the data/operand cache. If you care
55 about the contents of the cache that have not been written back yet, use
56 dcache_flush_range() before using this function.
57
58 \param start The physical address to begin invalidating at.
59 \param count The number of bytes to invalidate.
60*/
61void dcache_inval_range(uintptr_t start, size_t count);
62
63/** \brief Flush the data/operand cache.
64
65 This function flushes a range of the data/operand cache, forcing a write-
66 back on all of the data in the specified range. This does not invalidate
67 the cache in the process (meaning the blocks will still be in the cache,
68 just not marked as dirty after this has completed). If you wish to
69 invalidate the cache as well, call dcache_inval_range() after calling this
70 function or use dcache_purge_range() instead of dcache_flush_range().
71
72 \param start The physical address to begin flushing at.
73 \param count The number of bytes to flush.
74*/
75void dcache_flush_range(uintptr_t start, size_t count);
76
77/** \brief Flush all the data/operand cache.
78
79 This function flushes all the data/operand cache, forcing a write-
80 back on all of the cache blocks that are marked as dirty.
81
82 \note
83 dcache_flush_range() is faster than dcache_flush_all() if the count
84 param is 66560 or less.
85*/
87
88/** \brief Purge the data/operand cache.
89
90 This function flushes a range of the data/operand cache, forcing a write-
91 back and then invalidates all of the data in the specified range.
92
93 \param start The physical address to begin purging at.
94 \param count The number of bytes to purge.
95*/
96void dcache_purge_range(uintptr_t start, size_t count);
97
98/** \brief Purge all the data/operand cache.
99
100 This function flushes the entire data/operand cache, ensuring that all
101 cache blocks marked as dirty are written back to memory and all cache
102 entries are invalidated. It does not require an additional buffer and is
103 preferred when memory resources are constrained.
104
105 \note
106 dcache_purge_range() is faster than dcache_purge_all() if the count
107 param is 39936 or less.
108*/
110
111/** \brief Purge all the data/operand cache with buffer.
112
113 This function performs a purge of all data/operand cache blocks by
114 utilizing an external buffer to speed up the write-back and invalidation
115 process. It is always faster than dcache_purge_all() and is recommended
116 where maximum speed is required.
117
118 \note While this function offers a superior purge rate, it does require
119 the use of a temporary buffer. So use this function if you have an extra
120 8/16 kb of memory laying around that you can utilize for no other purpose
121 than for this function.
122
123 \param start The physical address for temporary buffer (32-byte
124 aligned)
125 \param count The size of the temporary buffer, which can be
126 either 8 KB or 16 KB, depending on cache
127 configuration - 8 KB buffer with OCRAM enabled,
128 otherwise 16 KB.
129
130*/
131void dcache_purge_all_with_buffer(uintptr_t start, size_t count);
132
133/** \brief Prefetch one block to the data/operand cache.
134
135 This function prefetch a block of the data/operand cache.
136
137 \param src The physical address to prefetch.
138*/
139static __always_inline void dcache_pref_block(const void *src) {
140 __asm__ __volatile__("pref @%0\n"
141 : /* No outputs */
142 : "r" (src)
143 : /* No clobbers */
144 );
145}
146
147/** \brief Write-back Store Queue buffer to external memory
148
149 This function initiates write-back for one Store Queue.
150
151 \param src The SQ mapped address to write-back.
152*/
153static __always_inline void dcache_wback_sq(void *src) {
154 __asm__ __volatile__("pref @%0\n"
155 : /* No outputs */
156 : "r" (src)
157 : "memory"
158 );
159}
160
161/** \brief Allocate one block of the data/operand cache.
162
163 This function allocate a block of the data/operand cache.
164
165 \param src The address to allocate (32-byte aligned)
166 \param value The value written to first 4-byte.
167*/
168static __always_inline void dcache_alloc_block(void *src, uint32_t value) {
169 uint32_t *src32 = (uint32_t *)src;
170
171 __asm__ ("movca.l r0, @%8\n\t"
172 : "=m"(src32[0]),
173 "=m"(src32[1]),
174 "=m"(src32[2]),
175 "=m"(src32[3]),
176 "=m"(src32[4]),
177 "=m"(src32[5]),
178 "=m"(src32[6]),
179 "=m"(src32[7])
180 : "r" (src32), "z" (value)
181 );
182}
183
184/** @} */
185
186__END_DECLS
187
188#endif /* __ARCH_CACHE_H */
#define __always_inline
Ask the compiler to always inline a given function.
Definition cdefs.h:166
static __always_inline void dcache_alloc_block(void *src, uint32_t value)
Allocate one block of the data/operand cache.
Definition cache.h:168
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:139
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:153
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.