riscv: rv32i,rv32e,rv64i and rv64e CPUs
cover riscv: rv32i,rv32e,rv64i and rv64e CPUs
Hi,
This series adds canonical/bare bones RISC-V CPUs in QEMU. The idea is to allow users to create a CPU from scratch without having to deal/disable existing defaults.
A bare-bones CPU will avoid scenarios like the one described here:
https://lore.kernel.org/qemu-riscv/258be47f-97be-4308-bed5-dc34ef7ff954@Spark/
Where one has to disable a bunch of defaults from the rv32 CPU to be able to use the desired configuration. After this series, the case from the link above:
-cpu rv32,g=false,f=false,v=false,d=false,e=false,h=false,(... desired setup)
Will be expressed as:
-cpu rv32i,(... desired setup)
Note that the idea isn't new. The rv64i CPU was already presented in the rva22u64 profile series [1]. That series didn't make it for 8.2, so I'm picking patches 1-4 (already reviewed and acked) and re-posting for this work. In case this series is accepted first I'll rebase and re-send the profile series.
I'm also adding RVE CPUs, rv32e and rv64e. The reason is that we can't enable I and E at the same time, and all default CPUs has I by default, so we would need to do something like 'r32i,i=false,e=true' to have a base RVE 32 bit CPU.
[1] https://lore.kernel.org/qemu-riscv/[email protected]/
Daniel Henrique Barboza (6):
target/riscv: create TYPE_RISCV_VENDOR_CPU target/riscv/tcg: do not use "!generic" CPU checks target/riscv/tcg: update priv_ver on user_set extensions target/riscv: add rv64i CPU
target/riscv: add rv32i CPU
target/riscv: add rv32e/rv64e CPUs
target/riscv/cpu-qom.h | 6 ++ target/riscv/cpu.c | 145 ++++++++++++++++++++++++++++++++++--- target/riscv/tcg/tcg-cpu.c | 45 +++++++++++- 3 files changed, 183 insertions(+), 13 deletions(-)
Signature · 1 line
2.41.0
PATCH 1/6 target/riscv: create TYPE_RISCV_VENDOR_CPU
We want to add a new CPU type for bare CPUs that will inherit specific traits of the 2 existing types:
- it will allow for extensions to be enabled/disabled, like generic CPUs;
- it will NOT inherit defaults, like vendor CPUs.
We can make this conditions met by adding an explicit type for the existing vendor CPUs and change the existing logic to not imply that "not generic" means vendor CPUs.
Let's add the "vendor" CPU type first.
--- target/riscv/cpu-qom.h | 1 + target/riscv/cpu.c | 30 +++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 9 deletions(-)
target/riscv/cpu-qom.h +1 -0
@@ -23,6 +23,7 @@#define TYPE_RISCV_CPU "riscv-cpu"#define TYPE_RISCV_DYNAMIC_CPU "riscv-dynamic-cpu" +#define TYPE_RISCV_VENDOR_CPU "riscv-vendor-cpu"#define RISCV_CPU_TYPE_SUFFIX "-" TYPE_RISCV_CPU#define RISCV_CPU_TYPE_NAME (name) (name RISCV_CPU_TYPE_SUFFIX)
target/riscv/cpu.c +21 -9
@@ -1778,6 +1778,13 @@ void riscv_cpu_list(void) .instance_init = initfn \ } +#define DEFINE_VENDOR_CPU (type_name, initfn) \ + { \ + .name = type_name, \ + .parent = TYPE_RISCV_VENDOR_CPU, \ + .instance_init = initfn \ + } +static const TypeInfo riscv_cpu_type_infos[] = { { .name =TYPE_RISCV_CPU ,
@@ -1795,21 +1802,26 @@ static const TypeInfo riscv_cpu_type_infos[] = { .parent = TYPE_RISCV_CPU, .abstract = true, }, + { + .name = TYPE_RISCV_VENDOR_CPU, + .parent = TYPE_RISCV_CPU, + .abstract = true, + }, DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_ANY, riscv_any_cpu_init), DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_MAX, riscv_max_cpu_init), #if defined(TARGET_RISCV32) DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_BASE32, rv32_base_cpu_init), - DEFINE_CPU(TYPE_RISCV_CPU_IBEX, rv32_ibex_cpu_init), - DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E31, rv32_sifive_e_cpu_init), - DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_E34, rv32_imafcu_nommu_cpu_init), - DEFINE_CPU(TYPE_RISCV_CPU_SIFIVE_U34, rv32_sifive_u_cpu_init), + DEFINE_VENDOR_CPU(TYPE_RISCV_CPU_IBEX, rv32_ibex_cpu_init), + DEFINE_VENDOR_CPU(TYPE_RISCV_CPU_SIFIVE_E31, rv32_sifive_e_cpu_init), + DEFINE_VENDOR_CPU(TYPE_RISCV_CPU_SIFIVE_E34, rv32_imafcu_nommu_cpu_init), + DEFINE_VENDOR_CPU(TYPE_RISCV_CPU_SIFIVE_U34, rv32_sifive_u_cpu_init), #elif defined(TARGET_RISCV64) DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_BASE64, rv64_base_cpu_init), -DEFINE_CPU (TYPE_RISCV_CPU_SIFIVE_E51 , rv64_sifive_e_cpu_init), -DEFINE_CPU (TYPE_RISCV_CPU_SIFIVE_U54 , rv64_sifive_u_cpu_init), -DEFINE_CPU (TYPE_RISCV_CPU_SHAKTI_C , rv64_sifive_u_cpu_init), -DEFINE_CPU (TYPE_RISCV_CPU_THEAD_C906 , rv64_thead_c906_cpu_init), -DEFINE_CPU (TYPE_RISCV_CPU_VEYRON_V1 , rv64_veyron_v1_cpu_init), + DEFINE_VENDOR_CPU(TYPE_RISCV_CPU_SIFIVE_E51, rv64_sifive_e_cpu_init), + DEFINE_VENDOR_CPU(TYPE_RISCV_CPU_SIFIVE_U54, rv64_sifive_u_cpu_init), + DEFINE_VENDOR_CPU(TYPE_RISCV_CPU_SHAKTI_C, rv64_sifive_u_cpu_init), + DEFINE_VENDOR_CPU(TYPE_RISCV_CPU_THEAD_C906, rv64_thead_c906_cpu_init), + DEFINE_VENDOR_CPU(TYPE_RISCV_CPU_VEYRON_V1, rv64_veyron_v1_cpu_init), DEFINE_DYNAMIC_CPU(TYPE_RISCV_CPU_BASE128, rv128_base_cpu_init), #endif };
Signature · 1 line
2.41.0
PATCH 2/6 target/riscv/tcg: do not use "!generic" CPU checks
In reply to Daniel Henrique BarbozaOur current logic in get/setters of MISA and multi-letter extensions works because we have only 2 CPU types, generic and vendor, and by using "!generic" we're implying that we're talking about vendor CPUs. When adding a third CPU type this logic will break so let's handle it beforehand.
In set_misa_ext_cfg() and set_multi_ext_cfg(), check for "vendor" cpu instead of "not generic". The "generic CPU" checks remaining are from riscv_cpu_add_misa_properties() and cpu_add_multi_ext_prop() before applying default values for the extensions.
This leaves us with:
- vendor CPUs will not allow extension enablement, all other CPUs will;
- generic CPUs will inherit default values for extensions, all others won't.
And now we can add a new, third CPU type, that will allow extensions to be enabled and will not inherit defaults, without changing the existing logic.
--- target/riscv/tcg/tcg-cpu.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
target/riscv/tcg/tcg-cpu.c +9 -4
@@ -654,6 +654,11 @@ static bool riscv_cpu_is_generic(Object *cpu_obj)return object_dynamic_cast (cpu_obj,TYPE_RISCV_DYNAMIC_CPU ) !=NULL ; } +static bool riscv_cpu_is_vendor (Object *cpu_obj) +{ +return object_dynamic_cast (cpu_obj,TYPE_RISCV_VENDOR_CPU ) !=NULL ; +} +/* * We'll get here via the following path: *
@@ -722,7 +727,7 @@ static void cpu_set_misa_ext_cfg(Object *obj, Visitor *v, const char *name,target_ulong misa_bit = misa_ext_cfg->misa_bit;RISCVCPU *cpu =RISCV_CPU (obj);CPURISCVState *env = &cpu->env; -bool generic_cpu =riscv_cpu_is_generic (obj); +bool vendor_cpu =riscv_cpu_is_vendor (obj);bool prev_val, value;if (!visit_type_bool (v, name, &value, errp)) {
@@ -736,7 +741,7 @@ static void cpu_set_misa_ext_cfg(Object *obj, Visitor *v, const char *name, }if (value) { -if (!generic_cpu) { +if (vendor_cpu) { g_autofree char *cpuname = riscv_cpu_get_name(cpu);error_setg (errp,"'%s' CPU does not allow enabling extensions" , cpuname);
@@ -841,7 +846,7 @@ static void cpu_set_multi_ext_cfg(Object *obj, Visitor *v, const char *name, {const RISCVCPUMultiExtConfig *multi_ext_cfg = opaque;RISCVCPU *cpu =RISCV_CPU (obj); -bool generic_cpu =riscv_cpu_is_generic (obj); +bool vendor_cpu =riscv_cpu_is_vendor (obj);bool prev_val, value;if (!visit_type_bool (v, name, &value, errp)) {
@@ -865,7 +870,7 @@ static void cpu_set_multi_ext_cfg(Object *obj, Visitor *v, const char *name,return ; } -if (value && !generic_cpu) { +if (value && vendor_cpu) { g_autofree char *cpuname = riscv_cpu_get_name(cpu);error_setg (errp,"'%s' CPU does not allow enabling extensions" , cpuname);
Signature · 1 line
2.41.0
PATCH 3/6 target/riscv/tcg: update priv_ver on user_set extensions
In reply to Daniel Henrique BarbozaWe'll add a new bare CPU type that won't have any default priv_ver. This means that the CPU will default to priv_ver = 0, i.e. 1.10.0.
At the same we'll allow these CPUs to enable extensions at will, but then, if the extension has a priv_ver newer than 1.10, we'll end up disabling it. Users will then need to manually set priv_ver to something other than 1.10 to enable the extensions they want, which is not ideal.
Change the setter() of extensions to allow user enabled extensions to bump the priv_ver of the CPU. This will make it convenient for users to enable extensions for CPUs that doesn't set a default priv_ver.
This change does not affect any existing CPU: vendor CPUs does not allow extensions to be enabled, and generic CPUs are already set to priv_ver LATEST.
--- target/riscv/tcg/tcg-cpu.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
target/riscv/tcg/tcg-cpu.c +32 -0
@@ -114,6 +114,26 @@ static int cpu_cfg_ext_get_min_version(uint32_t ext_offset)g_assert_not_reached (); } +static void cpu_validate_multi_ext_priv_ver (CPURISCVState *env, +uint32_t ext_offset) +{ +int ext_priv_ver; + +if (env->priv_ver ==PRIV_VERSION_LATEST ) { +return ; + } + + ext_priv_ver =cpu_cfg_ext_get_min_version (ext_offset); + +if (env->priv_ver < ext_priv_ver) { +/* +* Note: the 'priv_spec' command line option, if present, +* will take precedence over this priv_ver bump. +*/ + env->priv_ver = ext_priv_ver; + } +} +static void cpu_cfg_ext_auto_update (RISCVCPU *cpu,uint32_t ext_offset,bool value) {
@@ -748,6 +768,14 @@ static void cpu_set_misa_ext_cfg(Object *obj, Visitor *v, const char *name,return ; } +if (misa_bit ==RVH && env->priv_ver <PRIV_VERSION_1_12_0 ) { +/* +* Note: the 'priv_spec' command line option, if present, +* will take precedence over this priv_ver bump. +*/ + env->priv_ver =PRIV_VERSION_1_12_0 ; + } + env->misa_ext |= misa_bit; env->misa_ext_mask |= misa_bit; }else {
@@ -877,6 +905,10 @@ static void cpu_set_multi_ext_cfg(Object *obj, Visitor *v, const char *name,return ; } +if (value) { +cpu_validate_multi_ext_priv_ver (&cpu->env, multi_ext_cfg->offset); + } +isa_ext_update_enabled (cpu, multi_ext_cfg->offset, value); }
Signature · 1 line
2.41.0
PATCH 4/6 target/riscv: add rv64i CPU
In reply to Daniel Henrique BarbozaWe don't have any form of a 'bare bones' CPU. rv64, our default CPUs, comes with a lot of defaults. This is fine for most regular uses but it's not suitable when more control of what is actually loaded in the CPU is required.
A bare-bones CPU would be annoying to deal with if not by profile support, a way to load a multitude of extensions with a single flag. Profile support is going to be implemented shortly, so let's add a CPU for it.
The new 'rv64i' CPU will have only RVI loaded. It is inspired in the profile specification that dictates, for RVA22U64 [1]:
"RVA22U64 Mandatory Base
RV64I is the mandatory base ISA for RVA22U64"
And so it seems that RV64I is the mandatory base ISA for all profiles listed in [1], making it an ideal CPU to use with profile support.
rv64i is a CPU of type TYPE_RISCV_BARE_CPU. It has a mix of features from pre-existent CPUs:
- it allows extensions to be enabled, like generic CPUs;
- it will not inherit extension defaults, like vendor CPUs.
This is the minimum extension set to boot OpenSBI and buildroot using rv64i:
./build/qemu-system-riscv64 -nographic -M virt \
-cpu rv64i,sv39=true,g=true,c=true,s=true,u=trueOur minimal riscv,isa in this case will be:
# cat /proc/device-tree/cpus/cpu@0/riscv,isa rv64imafdc_zicntr_zicsr_zifencei_zihpm_zca_zcd#
[1] https://github.com/riscv/riscv-profiles/blob/main/profiles.adoc
--- target/riscv/cpu-qom.h | 2 ++ target/riscv/cpu.c | 46 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+)
target/riscv/cpu-qom.h +2 -0
@@ -24,6 +24,7 @@#define TYPE_RISCV_CPU "riscv-cpu"#define TYPE_RISCV_DYNAMIC_CPU "riscv-dynamic-cpu"#define TYPE_RISCV_VENDOR_CPU "riscv-vendor-cpu" +#define TYPE_RISCV_BARE_CPU "riscv-bare-cpu"#define RISCV_CPU_TYPE_SUFFIX "-" TYPE_RISCV_CPU#define RISCV_CPU_TYPE_NAME (name) (name RISCV_CPU_TYPE_SUFFIX)
@@ -33,6 +34,7 @@#define TYPE_RISCV_CPU_BASE32 RISCV_CPU_TYPE_NAME("rv32")#define TYPE_RISCV_CPU_BASE64 RISCV_CPU_TYPE_NAME("rv64")#define TYPE_RISCV_CPU_BASE128 RISCV_CPU_TYPE_NAME("x-rv128") +#define TYPE_RISCV_CPU_RV64I RISCV_CPU_TYPE_NAME("rv64i")#define TYPE_RISCV_CPU_IBEX RISCV_CPU_TYPE_NAME("lowrisc-ibex")#define TYPE_RISCV_CPU_SHAKTI_C RISCV_CPU_TYPE_NAME("shakti-c")#define TYPE_RISCV_CPU_SIFIVE_E31 RISCV_CPU_TYPE_NAME("sifive-e31")
target/riscv/cpu.c +46 -0
76 lines not on this page · raw message
Signature · 1 line
2.41.0
PATCH 5/6 target/riscv: add rv32i CPU
In reply to Daniel Henrique BarbozaAdd a bare bones 32 bit CPU, like we already did with rv64i, to ease the pain of users trying to build a CPU from scratch and having to disable the defaults we have with the regular rv32 CPU.
See:
https://lore.kernel.org/qemu-riscv/258be47f-97be-4308-bed5-dc34ef7ff954@Spark/
For a use case where the existence of rv32i would make things simpler.
--- target/riscv/cpu-qom.h | 1 + target/riscv/cpu.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+)
target/riscv/cpu-qom.h +1 -0
@@ -34,6 +34,7 @@#define TYPE_RISCV_CPU_BASE32 RISCV_CPU_TYPE_NAME("rv32")#define TYPE_RISCV_CPU_BASE64 RISCV_CPU_TYPE_NAME("rv64")#define TYPE_RISCV_CPU_BASE128 RISCV_CPU_TYPE_NAME("x-rv128") +#define TYPE_RISCV_CPU_RV32I RISCV_CPU_TYPE_NAME("rv32i")#define TYPE_RISCV_CPU_RV64I RISCV_CPU_TYPE_NAME("rv64i")#define TYPE_RISCV_CPU_IBEX RISCV_CPU_TYPE_NAME("lowrisc-ibex")#define TYPE_RISCV_CPU_SHAKTI_C RISCV_CPU_TYPE_NAME("shakti-c")
target/riscv/cpu.c +23 -0
@@ -666,6 +666,28 @@ static void rv32_imafcu_nommu_cpu_init(Object *obj) cpu->cfg.ext_zicsr = true; cpu->cfg.pmp = true; } + +static void rv32i_bare_cpu_init (Object *obj) +{ +CPURISCVState *env = &RISCV_CPU (obj)->env; +riscv_cpu_set_misa (env,MXL_RV32 ,RVI ); + +/* Remove the defaults from the parent class */ +RISCV_CPU (obj)->cfg.ext_zicntr = false; +RISCV_CPU (obj)->cfg.ext_zihpm = false; + +/* Set to QEMU's first supported priv version */ + env->priv_ver =PRIV_VERSION_1_10_0 ; + +/* +* Support all available satp_mode settings. The default +* value will be set to MBARE if the user doesn't set +* satp_mode manually (see set_satp_mode_default()). +*/ +#ifndef CONFIG_USER_ONLY +set_satp_mode_max_supported (RISCV_CPU (obj),VM_1_10_SV32 ); +#endif +}#endif static ObjectClass *riscv_cpu_class_by_name (const char *cpu_model)
@@ -1860,6 +1882,7 @@ static const TypeInfo riscv_cpu_type_infos[] = { DEFINE_VENDOR_CPU(TYPE_RISCV_CPU_SIFIVE_E31, rv32_sifive_e_cpu_init), DEFINE_VENDOR_CPU(TYPE_RISCV_CPU_SIFIVE_E34, rv32_imafcu_nommu_cpu_init), DEFINE_VENDOR_CPU(TYPE_RISCV_CPU_SIFIVE_U34, rv32_sifive_u_cpu_init), + DEFINE_BARE_CPU(TYPE_RISCV_CPU_RV32I, rv32i_bare_cpu_init), #elif defined(TARGET_RISCV64)DEFINE_DYNAMIC_CPU (TYPE_RISCV_CPU_BASE64 , rv64_base_cpu_init), DEFINE_VENDOR_CPU(TYPE_RISCV_CPU_SIFIVE_E51, rv64_sifive_e_cpu_init),
Signature · 1 line
2.41.0
PATCH 6/6 target/riscv: add rv32e/rv64e CPUs
In reply to Daniel Henrique BarbozaIn our internals we'll never allow RVI and RVE to be enabled at the same time, and we require either RVI or RVE to be enabled to proceed with machine boot. And all CPUs we have enables RVI by default.
This means that if one wants to create an embedded CPU he'll need to disable RVI first then enable RVE, e.g.:
-cpu rv64i,i=false,e=true
Let's add two RVE CPUs to ease the burder when working with embedded CPUs in QEMU.
--- target/riscv/cpu-qom.h | 2 ++ target/riscv/cpu.c | 46 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+)
target/riscv/cpu-qom.h +2 -0
@@ -36,6 +36,8 @@#define TYPE_RISCV_CPU_BASE128 RISCV_CPU_TYPE_NAME("x-rv128")#define TYPE_RISCV_CPU_RV32I RISCV_CPU_TYPE_NAME("rv32i")#define TYPE_RISCV_CPU_RV64I RISCV_CPU_TYPE_NAME("rv64i") +#define TYPE_RISCV_CPU_RV32E RISCV_CPU_TYPE_NAME("rv32e") +#define TYPE_RISCV_CPU_RV64E RISCV_CPU_TYPE_NAME("rv64e")#define TYPE_RISCV_CPU_IBEX RISCV_CPU_TYPE_NAME("lowrisc-ibex")#define TYPE_RISCV_CPU_SHAKTI_C RISCV_CPU_TYPE_NAME("shakti-c")#define TYPE_RISCV_CPU_SIFIVE_E31 RISCV_CPU_TYPE_NAME("sifive-e31")
target/riscv/cpu.c +46 -0
70 lines not on this page · raw message
Signature · 1 line
2.41.0