Skip to content

Commit a181fb4

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

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 rt_hw_cpu_dcache_ops(int ops, void* addr, int size)
24+
{
25+
switch (ops)
26+
{
27+
case RT_HW_CACHE_FLUSH:
28+
CACHE_OP_RANGE(DCACHE_CIPA_A0, start, size);
29+
break;
30+
case RT_HW_CACHE_INVALIDATE:
31+
CACHE_OP_RANGE(DCACHE_IPA_A0, start, size);
32+
break;
33+
default:
34+
break;
35+
}
36+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
* sync.s
33+
* | 31 - 25 | 24 - 20 | 19 - 15 | 14 - 12 | 11 - 7 | 6 - 0 |
34+
* 0000000 11001 00000 000 00000 0001011
35+
*/
36+
#define DCACHE_IPA_A0 ".long 0x02a5000b"
37+
#define DCACHE_CPA_A0 ".long 0x0295000b"
38+
#define DCACHE_CIPA_A0 ".long 0x02b5000b"
39+
40+
#define SYNC_S ".long 0x0190000b"
41+
42+
#define CACHE_OP_RANGE(OP, start, size) \
43+
register unsigned long i asm("a0") = start & ~(L1_CACHE_BYTES - 1); \
44+
for (; i < ALIGN(start + size, L1_CACHE_BYTES); i += L1_CACHE_BYTES) \
45+
__asm__ __volatile__(OP); \
46+
__asm__ __volatile__(SYNC_S)
47+
48+
#endif /* __CACHE_H__ */

0 commit comments

Comments
 (0)