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