Skip to content

Commit 4bf57af

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

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

bsp/cvitek/c906_little/Kconfig

Lines changed: 1 addition & 0 deletions
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
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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/27 zdtyuiop4444 Add Icache operation
9+
* 2024/11/26 zdtyuiop4444 The first version
10+
*/
11+
12+
#include "cache.h"
13+
14+
inline void rt_hw_cpu_dcache_enable(void)
15+
{
16+
asm volatile("csrs mhcr, %0;" ::"rI"(0x2));
17+
}
18+
19+
inline void rt_hw_cpu_dcache_disable(void)
20+
{
21+
asm volatile("csrc mhcr, %0;" ::"rI"(0x2));
22+
}
23+
24+
inline void inv_dcache_range(uintptr_t start, size_t size) {
25+
CACHE_OP_RANGE(DCACHE_IPA_A0, start, size);
26+
}
27+
28+
inline void inv_icache_range(uintptr_t start, size_t size) {
29+
CACHE_OP_RANGE(ICACHE_IPA_A0, start, size);
30+
}
31+
32+
inline void flush_dcache_range(uintptr_t start, size_t size) {
33+
CACHE_OP_RANGE(DCACHE_CIPA_A0, start, size);
34+
}
35+
36+
inline void rt_hw_cpu_dcache_ops(int ops, void* addr, int size)
37+
{
38+
switch (ops)
39+
{
40+
case RT_HW_CACHE_FLUSH:
41+
flush_dcache_range(addr, size);
42+
break;
43+
case RT_HW_CACHE_INVALIDATE:
44+
inv_dcache_range(addr, size);
45+
break;
46+
default:
47+
break;
48+
}
49+
}
50+
51+
inline void rt_hw_cpu_icache_enable(void)
52+
{
53+
asm volatile("csrs mhcr, %0;" ::"rI"(0x1));
54+
}
55+
56+
inline void rt_hw_cpu_icache_disable(void)
57+
{
58+
asm volatile("csrc mhcr, %0;" ::"rI"(0x1));
59+
}
60+
61+
inline void rt_hw_cpu_icache_ops(int ops, void* addr, int size)
62+
{
63+
switch (ops)
64+
{
65+
case RT_HW_CACHE_INVALIDATE:
66+
inv_icache_range(addr, size);
67+
break;
68+
default:
69+
break;
70+
}
71+
}
Lines changed: 54 additions & 0 deletions
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/27 zdtyuiop4444 Add Icache operation
9+
* 2024/11/26 zdtyuiop4444 The first version
10+
*/
11+
12+
#ifndef __CACHE_H__
13+
#define __CACHE_H__
14+
15+
#include <rthw.h>
16+
17+
#define L1_CACHE_BYTES 64
18+
#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
19+
20+
/*
21+
* dcache.ipa rs1 (invalidate)
22+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
23+
* 0000001 01010 rs1 000 00000 0001011
24+
*
25+
* dcache.cpa rs1 (clean)
26+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
27+
* 0000001 01001 rs1 000 00000 0001011
28+
*
29+
* dcache.cipa rs1 (clean then invalidate)
30+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
31+
* 0000001 01011 rs1 000 00000 0001011
32+
*
33+
* icache.ipa rs1 (invalidate)
34+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
35+
* 0000001 11000 rs1 000 00000 0001011
36+
*
37+
* sync.s
38+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
39+
* 0000000 11001 00000 000 00000 0001011
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__ */

0 commit comments

Comments
 (0)