Skip to content

Commit 6cbb2c3

Browse files
zdtyuiop4444Rbb666
authored andcommitted
[bsp][cvitek] add cache opration functions for cache coherence
By default, the small core enables D-Cache without ensuring cache coherence. Therefore, when using shared memory, inconsistencies can occur in the data read by the small core and the big core. Solution: Migrate cache-related functions from the official duo-buildroot-sdk library to implement cache-related operations in rthw.h. This allows you to either disable D-Cache or call the flush_dcache_range function before reading and after writing for synchronization. It is recommended to use the flush_dcache_range function, as disabling D-Cache can have a significant performance impact. Signed-off-by: zdtyuiop4444 <ign7798540@gmail.com>
1 parent 95064ed commit 6cbb2c3

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

bsp/cvitek/c906_little/Kconfig

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ config BSP_USING_C906_LITTLE
1414
bool
1515
select ARCH_RISCV64
1616
select ARCH_RISCV_FPU_D
17+
select RT_USING_CACHE
1718
select RT_USING_COMPONENTS_INIT
1819
select RT_USING_USER_MAIN
1920
default y

bsp/cvitek/c906_little/board/cache.c

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2006-2024, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2024/11/26 zdtyuiop4444 The first version
9+
*/
10+
11+
#include "cache.h"
12+
13+
inline void rt_hw_cpu_dcache_enable(void)
14+
{
15+
asm volatile("csrs mhcr, %0;" ::"rI"(0x2));
16+
}
17+
18+
inline void rt_hw_cpu_dcache_disable(void)
19+
{
20+
asm volatile("csrc mhcr, %0;" ::"rI"(0x2));
21+
}
22+
23+
inline void inv_dcache_range(uintptr_t start, size_t size) {
24+
CACHE_OP_RANGE(DCACHE_IPA_A0, start, size);
25+
}
26+
27+
inline void flush_dcache_range(uintptr_t start, size_t size) {
28+
CACHE_OP_RANGE(DCACHE_CIPA_A0, start, size);
29+
}
30+
31+
inline void rt_hw_cpu_dcache_ops(int ops, void* addr, int size)
32+
{
33+
switch (ops)
34+
{
35+
case RT_HW_CACHE_FLUSH:
36+
flush_dcache_range(addr, size);
37+
break;
38+
case RT_HW_CACHE_INVALIDATE:
39+
inv_dcache_range(addr, size);
40+
break;
41+
default:
42+
break;
43+
}
44+
}
45+
46+
inline void rt_hw_cpu_icache_enable(void)
47+
{
48+
asm volatile("csrs mhcr, %0;" ::"rI"(0x1));
49+
}
50+
51+
inline void rt_hw_cpu_icache_disable(void)
52+
{
53+
asm volatile("csrc mhcr, %0;" ::"rI"(0x1));
54+
}
55+
56+
inline void inv_icache_range(uintptr_t start, size_t size) {
57+
CACHE_OP_RANGE(ICACHE_IPA_A0, start, size);
58+
}
59+
60+
inline void rt_hw_cpu_icache_ops(int ops, void* addr, int size)
61+
{
62+
switch (ops)
63+
{
64+
case RT_HW_CACHE_INVALIDATE:
65+
inv_icache_range(addr, size);
66+
break;
67+
default:
68+
break;
69+
}
70+
}

bsp/cvitek/c906_little/board/cache.h

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2006-2024, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2024/11/26 zdtyuiop4444 The first version
9+
*/
10+
11+
#ifndef __CACHE_H__
12+
#define __CACHE_H__
13+
14+
#include <rthw.h>
15+
16+
#define L1_CACHE_BYTES 64
17+
#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
18+
19+
/*
20+
* dcache.ipa rs1 (invalidate)
21+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
22+
* 0000001 01010 rs1 000 00000 0001011
23+
*
24+
* dcache.cpa rs1 (clean)
25+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
26+
* 0000001 01001 rs1 000 00000 0001011
27+
*
28+
* dcache.cipa rs1 (clean then invalidate)
29+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
30+
* 0000001 01011 rs1 000 00000 0001011
31+
*
32+
* icache.ipa rs1 (invalidate)
33+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
34+
* 0000001 11000 rs1 000 00000 0001011
35+
*
36+
* sync.s
37+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
38+
* 0000000 11001 00000 000 00000 0001011
39+
*/
40+
41+
#define DCACHE_IPA_A0 ".long 0x02a5000b"
42+
#define DCACHE_CPA_A0 ".long 0x0295000b"
43+
#define DCACHE_CIPA_A0 ".long 0x02b5000b"
44+
#define ICACHE_IPA_A0 ".long 0x0385000b"
45+
46+
#define SYNC_S ".long 0x0190000b"
47+
48+
#define CACHE_OP_RANGE(OP, start, size) \
49+
register unsigned long i asm("a0") = start & ~(L1_CACHE_BYTES - 1); \
50+
for (; i < ALIGN(start + size, L1_CACHE_BYTES); i += L1_CACHE_BYTES) \
51+
__asm__ __volatile__(OP); \
52+
__asm__ __volatile__(SYNC_S)
53+
54+
#endif /* __CACHE_H__ */

bsp/cvitek/c906_little/rtconfig.h

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
#define RT_BACKTRACE_LEVEL_MAX_NR 32
121121
/* end of RT-Thread Kernel */
122122
#define ARCH_CPU_64BIT
123+
#define RT_USING_CACHE
123124
#define ARCH_RISCV
124125
#define ARCH_RISCV_FPU
125126
#define ARCH_RISCV_FPU_D

0 commit comments

Comments
 (0)