Skip to content

Commit f44215e

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 f44215e

File tree

9 files changed

+155
-2
lines changed

9 files changed

+155
-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: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
__asm__ volatile(".insn 0x500F\n");
24+
25+
return 0;
26+
}
27+
28+
int arch_dcache_invd_range(void *addr, size_t size)
29+
{
30+
__asm__ volatile(
31+
"mv a0, %1\n"
32+
"j 2f\n"
33+
"3:\n"
34+
".insn 0x5500F\n" /* 0x500f | (a0 << 15) */
35+
"add a0, a0, %0\n"
36+
"2:\n"
37+
"bltu a0, %2, 3b\n"
38+
: : "r"(CONFIG_DCACHE_LINE_SIZE),
39+
"r"((unsigned int)(addr) & ~((CONFIG_DCACHE_LINE_SIZE) - 1UL)),
40+
"r"((unsigned int)(addr) + (size))
41+
: "a0");
42+
43+
return 0;
44+
}
45+
46+
47+
int arch_dcache_flush_all(void)
48+
{
49+
/* VexRiscv cache is write-through */
50+
return 0;
51+
}
52+
53+
int arch_dcache_flush_range(void *addr __unused, size_t size __unused)
54+
{
55+
return 0;
56+
}
57+
58+
int arch_dcache_flush_and_invd_all(void)
59+
{
60+
return arch_dcache_invd_all();
61+
}
62+
63+
int arch_dcache_flush_and_invd_range(void *addr, size_t size)
64+
{
65+
return arch_dcache_invd_range(addr, size);
66+
}
67+
#endif /* CONFIG_DCACHE */
68+
69+
#ifdef CONFIG_ICACHE
70+
void arch_icache_enable(void)
71+
{
72+
/* Nothing */
73+
}
74+
75+
void arch_icache_disable(void)
76+
{
77+
/* Nothing */
78+
}
79+
80+
int arch_icache_flush_all(void)
81+
{
82+
__asm__ volatile("fence.i\n");
83+
84+
return 0;
85+
}
86+
87+
int arch_icache_invd_all(void)
88+
{
89+
return arch_icache_flush_all();
90+
}
91+
92+
int arch_icache_invd_range(void *addr_in __unused, size_t size __unused)
93+
{
94+
return arch_icache_flush_all();
95+
}
96+
97+
int arch_icache_flush_and_invd_all(void)
98+
{
99+
return arch_icache_flush_all();
100+
}
101+
102+
int arch_icache_flush_range(void *addr __unused, size_t size __unused)
103+
{
104+
return arch_icache_flush_all();
105+
}
106+
107+
int arch_icache_flush_and_invd_range(void *addr __unused, size_t size __unused)
108+
{
109+
return arch_icache_flush_all();
110+
}
111+
#endif /* CONFIG_ICACHE */
112+
113+
void arch_cache_init(void)
114+
{
115+
/* Nothing */
116+
}

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)