Skip to content

Commit 8b397b5

Browse files
committed
[bsp][cvitek] add dcache opration functions for cache coherence
1 parent 9afe6a5 commit 8b397b5

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef __ARCH_HELPERS_H__
2+
#define __ARCH_HELPERS_H__
3+
#include <stdint.h>
4+
#include <sys/types.h>
5+
6+
void flush_dcache_range(uintptr_t addr, size_t size);
7+
void clean_dcache_range(uintptr_t addr, size_t size);
8+
void inv_dcache_range(uintptr_t addr, size_t size);
9+
void enable_dcache(void);
10+
void disable_dcache(void);
11+
12+
#endif /* __ARCH_HELPERS_H__ */
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// #include <platform.h>
2+
#include "arch_helpers.h"
3+
4+
// typedef uint64_t phys_addr_t;
5+
// typedef uintptr_t size_t;
6+
7+
#define L1_CACHE_BYTES 64
8+
#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
9+
10+
/*
11+
* dcache.ipa rs1 (invalidate)
12+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
13+
* 0000001 01010 rs1 000 00000 0001011
14+
*
15+
* dcache.cpa rs1 (clean)
16+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
17+
* 0000001 01001 rs1 000 00000 0001011
18+
*
19+
* dcache.cipa rs1 (clean then invalidate)
20+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
21+
* 0000001 01011 rs1 000 00000 0001011
22+
*
23+
* sync.s
24+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
25+
* 0000000 11001 00000 000 00000 0001011
26+
*/
27+
#define DCACHE_IPA_A0 ".long 0x02a5000b"
28+
#define DCACHE_CPA_A0 ".long 0x0295000b"
29+
#define DCACHE_CIPA_A0 ".long 0x02b5000b"
30+
31+
#define SYNC_S ".long 0x0190000b"
32+
33+
#define CACHE_OP_RANGE(OP, start, size) \
34+
register unsigned long i asm("a0") = start & ~(L1_CACHE_BYTES - 1); \
35+
for (; i < ALIGN(start + size, L1_CACHE_BYTES); i += L1_CACHE_BYTES) \
36+
__asm__ __volatile__(OP); \
37+
__asm__ __volatile__(SYNC_S)
38+
39+
// void c900_cache_invalidate(phys_addr_t start, size_t size)
40+
inline void inv_dcache_range(uintptr_t start, size_t size) {
41+
CACHE_OP_RANGE(DCACHE_IPA_A0, start, size);
42+
}
43+
44+
// void c900_cache_clean(phys_addr_t start, size_t size)
45+
inline void clean_dcache_range(uintptr_t start, size_t size) {
46+
CACHE_OP_RANGE(DCACHE_CPA_A0, start, size);
47+
}
48+
49+
// void c900_cache_flush(phys_addr_t start, size_t size)
50+
inline void flush_dcache_range(uintptr_t start, size_t size) {
51+
CACHE_OP_RANGE(DCACHE_CIPA_A0, start, size);
52+
}
53+
54+
inline void enable_dcache(void) { asm volatile("csrs mhcr, %0;" ::"rI"(0x2)); }
55+
56+
inline void disable_dcache(void) { asm volatile("csrc mhcr, %0;" ::"rI"(0x2)); }

0 commit comments

Comments
 (0)