Skip to content

Commit 65bbde2

Browse files
committed
arch: riscv: vexriscv: add VexRiscv cache driver
add driver for VexRiscv CPU cache controller. Signed-off-by: Fin Maaß <[email protected]>
1 parent b6a814e commit 65bbde2

File tree

9 files changed

+161
-2
lines changed

9 files changed

+161
-2
lines changed

arch/riscv/custom/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ add_subdirectory_ifdef(CONFIG_DT_HAS_OPENHWGROUP_CVA6_ENABLED openhwgroup/cva6)
66
add_subdirectory_ifdef(CONFIG_DT_HAS_NUCLEI_BUMBLEBEE_ENABLED nuclei)
77
add_subdirectory_ifdef(CONFIG_DT_HAS_OPENISA_RI5CY_ENABLED openisa/ri5cy)
88
add_subdirectory_ifdef(CONFIG_DT_HAS_OPENISA_ZERO_RI5CY_ENABLED openisa/zero_riscy)
9+
add_subdirectory_ifdef(CONFIG_DT_HAS_SPINALHDL_VEXRISCV_ENABLED vexriscv)
910
add_subdirectory_ifdef(CONFIG_DT_HAS_XUANTIE_E907_ENABLED thead)

arch/riscv/custom/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ rsource "andes/Kconfig"
77

88
endif # DT_HAS_ANDESTECH_ANDESCORE_V5_ENABLED
99

10+
if DT_HAS_SPINALHDL_VEXRISCV_ENABLED
11+
12+
rsource "vexriscv/Kconfig"
13+
14+
endif # DT_HAS_SPINALHDL_VEXRISCV_ENABLED
15+
1016
if DT_HAS_XUANTIE_E907_ENABLED
1117

1218
rsource "thead/Kconfig"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
zephyr_library_sources_ifdef(CONFIG_RISCV_CUSTOM_CSR_VEXRISCV_CACHE cache_vexriscv.c)

arch/riscv/custom/vexriscv/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config RISCV_CUSTOM_CSR_VEXRISCV_CACHE
5+
bool
6+
default y
7+
depends on ARCH_CACHE
8+
help
9+
This option enables cache support for VexRiscv family of CPUs.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/cache.h>
9+
10+
#ifdef CONFIG_DCACHE
11+
void arch_dcache_enable(void)
12+
{
13+
/* Nothing */
14+
}
15+
16+
void arch_dcache_disable(void)
17+
{
18+
/* Nothing */
19+
}
20+
21+
int arch_dcache_invd_all(void)
22+
{
23+
/* Invalidate whole data cache instruction: 0x500F
24+
* https://github.com/SpinalHDL/VexRiscv?tab=readme-ov-file#dbuscachedplugin
25+
*/
26+
__asm__ volatile(".insn 0x500F\n");
27+
28+
return 0;
29+
}
30+
31+
int arch_dcache_invd_range(void *addr, size_t size)
32+
{
33+
/* Invalidate cache line instruction: 0x500f | (rs1 << 15)
34+
* https://github.com/SpinalHDL/VexRiscv?tab=readme-ov-file#dbuscachedplugin
35+
*/
36+
__asm__ volatile(
37+
"mv a0, %1\n"
38+
"j 2f\n"
39+
"3:\n"
40+
".insn 0x5500F\n" /* 0x500f | (a0 << 15) */
41+
"add a0, a0, %0\n"
42+
"2:\n"
43+
"bltu a0, %2, 3b\n"
44+
: : "r"(CONFIG_DCACHE_LINE_SIZE),
45+
"r"((unsigned int)(addr) & ~((CONFIG_DCACHE_LINE_SIZE) - 1UL)),
46+
"r"((unsigned int)(addr) + (size))
47+
: "a0");
48+
49+
return 0;
50+
}
51+
52+
53+
int arch_dcache_flush_all(void)
54+
{
55+
/* VexRiscv cache is write-through */
56+
return 0;
57+
}
58+
59+
int arch_dcache_flush_range(void *addr __unused, size_t size __unused)
60+
{
61+
return 0;
62+
}
63+
64+
int arch_dcache_flush_and_invd_all(void)
65+
{
66+
return arch_dcache_invd_all();
67+
}
68+
69+
int arch_dcache_flush_and_invd_range(void *addr, size_t size)
70+
{
71+
return arch_dcache_invd_range(addr, size);
72+
}
73+
#endif /* CONFIG_DCACHE */
74+
75+
#ifdef CONFIG_ICACHE
76+
void arch_icache_enable(void)
77+
{
78+
/* Nothing */
79+
}
80+
81+
void arch_icache_disable(void)
82+
{
83+
/* Nothing */
84+
}
85+
86+
int arch_icache_flush_all(void)
87+
{
88+
__asm__ volatile("fence.i\n");
89+
90+
return 0;
91+
}
92+
93+
int arch_icache_invd_all(void)
94+
{
95+
return arch_icache_flush_all();
96+
}
97+
98+
int arch_icache_invd_range(void *addr_in __unused, size_t size __unused)
99+
{
100+
return arch_icache_flush_all();
101+
}
102+
103+
int arch_icache_flush_and_invd_all(void)
104+
{
105+
return arch_icache_flush_all();
106+
}
107+
108+
int arch_icache_flush_range(void *addr __unused, size_t size __unused)
109+
{
110+
return arch_icache_flush_all();
111+
}
112+
113+
int arch_icache_flush_and_invd_range(void *addr __unused, size_t size __unused)
114+
{
115+
return arch_icache_flush_all();
116+
}
117+
#endif /* CONFIG_ICACHE */
118+
119+
void arch_cache_init(void)
120+
{
121+
/* Nothing */
122+
}

dts/bindings/cpu/litex,vexriscv-standard.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ description: VexRiscv core with the standard configuration as used by LiteX
66

77
compatible: "litex,vexriscv-standard"
88

9-
include: riscv,cpus.yaml
9+
include: spinalhdl,vexriscv.yaml
10+
11+
properties:
12+
i-cache-line-size:
13+
default: 32
14+
d-cache-line-size:
15+
default: 32
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-FileCopyrightText: Copyright The Zephyr Project Contributors
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: VexRiscv core
5+
6+
compatible: "spinalhdl,vexriscv"
7+
8+
include: riscv,cpus.yaml

dts/riscv/riscv32-litex-vexriscv.dtsi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
cpu0: cpu@0 {
2424
clock-frequency = <100000000>;
25-
compatible = "litex,vexriscv-standard", "riscv";
25+
compatible = "litex,vexriscv-standard", "spinalhdl,vexriscv", "riscv";
2626
device_type = "cpu";
2727
reg = <0>;
2828
riscv,isa = "rv32im_zicsr_zifencei";

soc/litex/litex_vexriscv/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ config SOC_LITEX_VEXRISCV
88
select RISCV_ISA_EXT_M
99
select RISCV_ISA_EXT_ZICSR
1010
select RISCV_ISA_EXT_ZIFENCEI
11+
# There are varriants of the Vexriscv without cache, be able to set it
12+
select CPU_HAS_ICACHE if $(dt_node_int_prop_int,/cpus/cpu@0,i-cache-line-size) > 0
13+
select CPU_HAS_DCACHE if $(dt_node_int_prop_int,/cpus/cpu@0,d-cache-line-size) > 0
1114
imply XIP
1215

1316
if SOC_LITEX_VEXRISCV

0 commit comments

Comments
 (0)