Skip to content

Commit 49b6614

Browse files
authored
[libcpu]添加对riscv vector的支持 (#9531)
[libcpu]添加对riscv vector的支持
1 parent 47d9413 commit 49b6614

File tree

5 files changed

+183
-3
lines changed

5 files changed

+183
-3
lines changed

libcpu/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,18 @@ config ARCH_RISCV_FPU
248248
config ARCH_RISCV_VECTOR
249249
bool
250250

251+
if ARCH_RISCV_VECTOR
252+
choice
253+
prompt "RISCV Vector Vlen"
254+
default ARCH_VECTOR_VLEN_128
255+
256+
config ARCH_VECTOR_VLEN_128
257+
bool "128"
258+
config ARCH_VECTOR_VLEN_256
259+
bool "256"
260+
endchoice
261+
endif
262+
251263
config ARCH_RISCV_FPU_S
252264
select ARCH_RISCV_FPU
253265
bool

libcpu/risc-v/common64/stackframe.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include <rtconfig.h>
1818
#include "encoding.h"
19-
#include "ext_context.h"
2019

2120
/* bytes of register width */
2221
#ifdef ARCH_CPU_64BIT
@@ -30,6 +29,8 @@
3029
#error "Not supported XLEN"
3130
#endif
3231

32+
#include "ext_context.h"
33+
3334
/* 33 general register + 1 padding */
3435
#define CTX_GENERAL_REG_NR 34
3536

libcpu/risc-v/t-head/c908/SConscript

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ cwd = GetCurrentDir()
55
src = Glob('*.c') + Glob('*.cpp') + Glob('*_gcc.S')
66
CPPPATH = [cwd]
77

8-
if not GetDepend('ARCH_USING_ASID'):
9-
SrcRemove(src, ['asid.c'])
8+
if GetDepend('ARCH_RISCV_VECTOR'):
9+
CPPPATH += [cwd + '/../../vector/rvv-1.0']
1010

1111
group = DefineGroup('libcpu', src, depend = [''], CPPPATH = CPPPATH)
1212

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
* 2022-10-10 RT-Thread the first version,
9+
* compatible to riscv-v-spec-1.0
10+
*/
11+
#ifndef __RVV_CONTEXT_1_0_H__
12+
#define __RVV_CONTEXT_1_0_H__
13+
14+
#if defined(ARCH_VECTOR_VLEN_128)
15+
#define CTX_VECTOR_REGS 64
16+
#elif defined(ARCH_VECTOR_VLEN_256)
17+
#define CTX_VECTOR_REGS 128
18+
#else
19+
#error "No supported VLEN"
20+
#endif /* VLEN */
21+
22+
#define CTX_VECTOR_REG_NR (CTX_VECTOR_REGS + 4)
23+
24+
#ifdef __ASSEMBLY__
25+
26+
/**
27+
* ==================================
28+
* VECTOR EXTENSION
29+
* ==================================
30+
*/
31+
32+
#define VEC_FRAME_VSTART (0 * REGBYTES)
33+
#define VEC_FRAME_VTYPE (1 * REGBYTES)
34+
#define VEC_FRAME_VL (2 * REGBYTES)
35+
#define VEC_FRAME_VCSR (3 * REGBYTES)
36+
#define VEC_FRAME_V0 (4 * REGBYTES)
37+
38+
.macro GET_VEC_FRAME_LEN, xreg
39+
csrr \xreg, vlenb
40+
slli \xreg, \xreg, 5
41+
addi \xreg, \xreg, 4 * REGBYTES
42+
.endm
43+
44+
/**
45+
* @brief save vector extension hardware state
46+
*
47+
* @param dst register storing bottom of storage block
48+
*
49+
*/
50+
.macro SAVE_VECTOR, dst
51+
mv t1, \dst
52+
53+
csrr t0, vstart
54+
STORE t0, VEC_FRAME_VSTART(t1)
55+
csrr t0, vtype
56+
STORE t0, VEC_FRAME_VTYPE(t1)
57+
csrr t0, vl
58+
STORE t0, VEC_FRAME_VL(t1)
59+
csrr t0, vcsr
60+
STORE t0, VEC_FRAME_VCSR(t1)
61+
62+
addi t1, t1, VEC_FRAME_V0
63+
64+
// config vector setting,
65+
// t2 is updated to length of a vector group in bytes
66+
VEC_CONFIG_SETVLI(t2, x0, VEC_IMM_SEW_8, VEC_IMM_LMUL_8)
67+
68+
vse8.v v0, (t1)
69+
add t1, t1, t2
70+
vse8.v v8, (t1)
71+
add t1, t1, t2
72+
vse8.v v16, (t1)
73+
add t1, t1, t2
74+
vse8.v v24, (t1)
75+
.endm
76+
77+
/**
78+
* @brief restore vector extension hardware states
79+
*
80+
* @param dst register storing bottom of storage block
81+
*
82+
*/
83+
.macro RESTORE_VECTOR, dst
84+
// restore vector registers first since it will modify vector states
85+
mv t0, \dst
86+
addi t1, t0, VEC_FRAME_V0
87+
88+
VEC_CONFIG_SETVLI(t2, x0, VEC_IMM_SEW_8, VEC_IMM_LMUL_8)
89+
90+
vle8.v v0, (t1)
91+
add t1, t1, t2
92+
vle8.v v8, (t1)
93+
add t1, t1, t2
94+
vle8.v v16, (t1)
95+
add t1, t1, t2
96+
vle8.v v24, (t1)
97+
98+
mv t1, t0
99+
100+
LOAD t0, VEC_FRAME_VSTART(t1)
101+
csrw vstart, t0
102+
LOAD t0, VEC_FRAME_VCSR(t1)
103+
csrw vcsr, t0
104+
105+
LOAD t0, VEC_FRAME_VTYPE(t1)
106+
LOAD t3, VEC_FRAME_VL(t1)
107+
VEC_CONFIG_SET_VL_VTYPE(t3, t0)
108+
.endm
109+
110+
#endif
111+
112+
#endif /* __RVV_CONTEXT_H__ */
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2022-10-10 RT-Thread the first version,
9+
* compatible to riscv-v-spec-1.0
10+
*/
11+
12+
#ifndef __VECTOR_ENCODING_1_0_H__
13+
#define __VECTOR_ENCODING_1_0_H__
14+
15+
/* mstatus/sstatus */
16+
#define MSTATUS_VS 0x00000600
17+
#define SSTATUS_VS 0x00000600 /* Vector Status */
18+
#define SSTATUS_VS_INITIAL 0x00000200
19+
#define SSTATUS_VS_CLEAN 0x00000400
20+
#define SSTATUS_VS_DIRTY 0x00000600
21+
22+
#ifdef __ASSEMBLY__
23+
24+
/**
25+
* assembler names used for vset{i}vli vtypei immediate
26+
*/
27+
28+
#define VEC_IMM_SEW_8 e8
29+
#define VEC_IMM_SEW_16 e16
30+
#define VEC_IMM_SEW_32 e32
31+
#define VEC_IMM_SEW_64 e64
32+
/* group setting, encoding by multiplier */
33+
#define VEC_IMM_LMUL_F8 mf8
34+
#define VEC_IMM_LMUL_F4 mf4
35+
#define VEC_IMM_LMUL_F2 mf2
36+
#define VEC_IMM_LMUL_1 m1
37+
#define VEC_IMM_LMUL_2 m2
38+
#define VEC_IMM_LMUL_4 m4
39+
#define VEC_IMM_LMUL_8 m8
40+
/* TAIL & MASK agnostic bits */
41+
#define VEC_IMM_TAIL_AGNOSTIC ta
42+
#define VEC_IMM_MASK_AGNOSTIC ma
43+
#define VEC_IMM_TAMA VEC_IMM_TAIL_AGNOSTIC, VEC_IMM_MASK_AGNOSTIC
44+
#define VEC_IMM_TAMU VEC_IMM_TAIL_AGNOSTIC
45+
#define VEC_IMM_TUMA VEC_IMM_MASK_AGNOSTIC
46+
47+
/**
48+
* configuration setting instruction
49+
*/
50+
#define VEC_CONFIG_SETVLI(xVl, xAvl, vtype...) vsetvli xVl, xAvl, ##vtype
51+
#define VEC_CONFIG_SET_VL_VTYPE(xVl, xVtype) vsetvl x0, xVl, xVtype
52+
53+
#endif
54+
55+
#endif /* __VECTOR_ENCODING_H__ */

0 commit comments

Comments
 (0)