TianoCore EDK2 master
Loading...
Searching...
No Matches
RiscVCache.c
Go to the documentation of this file.
1
10#include <Base.h>
11#include <Library/BaseLib.h>
12#include <Library/DebugLib.h>
13#include <Library/PcdLib.h>
14
15//
16// TODO: Grab cache block size and make Cache Management Operation
17// enabling decision based on RISC-V CPU HOB in
18// future when it is available and convert PcdRiscVFeatureOverride
19// PCD to a pointer that contains pointer to bitmap structure
20// which can be operated more elegantly.
21//
22#define RISCV_CACHE_BLOCK_SIZE 64
23#define RISCV_CPU_FEATURE_CMO_BITMASK 0x1
24
25typedef enum {
26 CacheOpClean,
27 CacheOpFlush,
28 CacheOpInvld,
29} CACHE_OP;
30
37BOOLEAN
39 VOID
40 )
41{
42 // If CMO is disabled in HW, skip Override check
43 // Otherwise this PCD can override settings
44 return ((PcdGet64 (PcdRiscVFeatureOverride) & RISCV_CPU_FEATURE_CMO_BITMASK) != 0);
45}
46
63VOID
65 IN VOID *Address,
66 IN UINTN Length,
67 IN CACHE_OP Op
68 )
69{
70 UINTN CacheLineSize;
71 UINTN Start;
72 UINTN End;
73
74 if (Length == 0) {
75 return;
76 }
77
78 if ((Op != CacheOpInvld) && (Op != CacheOpFlush) && (Op != CacheOpClean)) {
79 return;
80 }
81
82 ASSERT ((Length - 1) <= (MAX_ADDRESS - (UINTN)Address));
83
84 CacheLineSize = RISCV_CACHE_BLOCK_SIZE;
85
86 Start = (UINTN)Address;
87 //
88 // Calculate the cache line alignment
89 //
90 End = (Start + Length + (CacheLineSize - 1)) & ~(CacheLineSize - 1);
91 Start &= ~((UINTN)CacheLineSize - 1);
92
93 DEBUG (
94 (DEBUG_VERBOSE,
95 "CacheOpCacheRange: Performing Cache Management Operation %d \n", Op)
96 );
97
98 do {
99 switch (Op) {
100 case CacheOpInvld:
101 RiscVCpuCacheInvalCmoAsm (Start);
102 break;
103 case CacheOpFlush:
104 RiscVCpuCacheFlushCmoAsm (Start);
105 break;
106 case CacheOpClean:
107 RiscVCpuCacheCleanCmoAsm (Start);
108 break;
109 default:
110 break;
111 }
112
113 Start = Start + CacheLineSize;
114 } while (Start != End);
115}
116
125VOID
126EFIAPI
128 VOID
129 )
130{
131 RiscVInvalidateInstCacheFenceAsm ();
132}
133
153VOID *
154EFIAPI
156 IN VOID *Address,
157 IN UINTN Length
158 )
159{
160 DEBUG (
161 (DEBUG_VERBOSE,
162 "InvalidateInstructionCacheRange: RISC-V unsupported function.\n"
163 "Invalidating the whole instruction cache instead.\n"
164 )
165 );
167 return Address;
168}
169
180VOID
181EFIAPI
183 VOID
184 )
185{
186 ASSERT (FALSE);
187 DEBUG ((
188 DEBUG_ERROR,
189 "WriteBackInvalidateDataCache: RISC-V unsupported function.\n"
190 ));
191}
192
219VOID *
220EFIAPI
222 IN VOID *Address,
223 IN UINTN Length
224 )
225{
226 if (RiscVIsCMOEnabled ()) {
227 CacheOpCacheRange (Address, Length, CacheOpFlush);
228 } else {
229 ASSERT (FALSE);
230 }
231
232 return Address;
233}
234
245VOID
246EFIAPI
248 VOID
249 )
250{
251 ASSERT (FALSE);
252}
253
276VOID *
277EFIAPI
279 IN VOID *Address,
280 IN UINTN Length
281 )
282{
283 if (RiscVIsCMOEnabled ()) {
284 CacheOpCacheRange (Address, Length, CacheOpClean);
285 } else {
286 ASSERT (FALSE);
287 }
288
289 return Address;
290}
291
303VOID
304EFIAPI
306 VOID
307 )
308{
309 RiscVInvalidateDataCacheFenceAsm ();
310}
311
336VOID *
337EFIAPI
339 IN VOID *Address,
340 IN UINTN Length
341 )
342{
343 if (RiscVIsCMOEnabled ()) {
344 CacheOpCacheRange (Address, Length, CacheOpInvld);
345 } else {
346 DEBUG (
347 (DEBUG_VERBOSE,
348 "InvalidateDataCacheRange: Zicbom not supported.\n"
349 "Invalidating the whole Data cache instead.\n")
350 );
352 }
353
354 return Address;
355}
UINT64 UINTN
#define MAX_ADDRESS
#define STATIC
Definition: Base.h:264
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define PcdGet64(TokenName)
Definition: PcdLib.h:375
VOID EFIAPI InvalidateDataCache(VOID)
Definition: RiscVCache.c:305
VOID *EFIAPI WriteBackDataCacheRange(IN VOID *Address, IN UINTN Length)
Definition: RiscVCache.c:278
VOID *EFIAPI InvalidateDataCacheRange(IN VOID *Address, IN UINTN Length)
Definition: RiscVCache.c:338
VOID *EFIAPI WriteBackInvalidateDataCacheRange(IN VOID *Address, IN UINTN Length)
Definition: RiscVCache.c:221
VOID EFIAPI InvalidateInstructionCache(VOID)
Definition: RiscVCache.c:127
VOID EFIAPI WriteBackInvalidateDataCache(VOID)
Definition: RiscVCache.c:182
VOID EFIAPI WriteBackDataCache(VOID)
Definition: RiscVCache.c:247
STATIC BOOLEAN RiscVIsCMOEnabled(VOID)
Definition: RiscVCache.c:38
STATIC VOID CacheOpCacheRange(IN VOID *Address, IN UINTN Length, IN CACHE_OP Op)
Definition: RiscVCache.c:64
VOID *EFIAPI InvalidateInstructionCacheRange(IN VOID *Address, IN UINTN Length)
Definition: RiscVCache.c:155