riscv: zicntr/zihpm flags and disable support
v2 cover riscv: zicntr/zihpm flags and disable support
Hi,
This is a follow-up of the work done in [1] after review comments made in the rva22u64 profile support review [2].
zicntr and zihpm are already implemented by QEMU before they were added as discrete extensions by RVI. This puts QEMU in a weird spot because it has enabled default extensions that aren't togglable on/off via user flags and aren't reported in riscv,isa as present.
There's no reason to no treat both zicntr and zihpm as regular extensions, reporting their existence in riscv,isa DT and allowing users to disable them. This will also benefit us in the incoming profile support, since disabling a profile should disable all its mandatory extensions, and for the rva22u64 profile this means that both zicntr and zihpm should be disabled as well.
Let's add user flags and disable support for both in all accelerators (TCG and KVM). FWIW Linux boot doesn't seem to care about the lack of both zicntr and zihpm when running TCG, although attempting to use the timers will result in SIGILL.
[1] https://lore.kernel.org/qemu-riscv/[email protected]/
[2] https://lore.kernel.org/qemu-riscv/20231017-e7a4712137165b59844499e3@orel/T/#m3bb0e3c9b00d9edd168da9f7de0bc26df4f7d6ab
Daniel Henrique Barboza (6):
target/riscv/cpu.c: add zicntr extension flag target/riscv/tcg: add ext_zicntr disable support target/riscv/kvm: add zicntr reg target/riscv/cpu.c: add zihpm extension flag target/riscv/tcg: add ext_zihpm disable support target/riscv/kvm: add zihpm reg
target/riscv/cpu.c | 15 +++++++++++++++ target/riscv/cpu_cfg.h | 2 ++ target/riscv/csr.c | 4 ++++ target/riscv/kvm/kvm-cpu.c | 2 ++ target/riscv/tcg/tcg-cpu.c | 21 +++++++++++++++++++++ 5 files changed, 44 insertions(+)
Signature · 1 line
2.41.0
PATCH v2 1/6 target/riscv/cpu.c: add zicntr extension flag
zicntr is the Base Counters and Timers extension described in chapter 12 of the unprivileged spec. It describes support for RDCYCLE, RDTIME and RDINSTRET.
QEMU already implements it way before it was a discrete extension. zicntr is part of the RVA22 profile, so let's add it to QEMU to make the future profile implementation flag complete.
Given than it represents an already existing feature, default it to 'true' for all CPUs. Accelerators are responsible for disabling them if the user wants to.
--- target/riscv/cpu.c | 12 ++++++++++++ target/riscv/cpu_cfg.h | 1 + 2 files changed, 13 insertions(+)
target/riscv/cpu.c +12 -0
@@ -79,6 +79,7 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (zicbom,PRIV_VERSION_1_12_0 , ext_zicbom),ISA_EXT_DATA_ENTRY (zicboz,PRIV_VERSION_1_12_0 , ext_zicboz),ISA_EXT_DATA_ENTRY (zicond,PRIV_VERSION_1_12_0 , ext_zicond), +ISA_EXT_DATA_ENTRY (zicntr,PRIV_VERSION_1_12_0 , ext_zicntr),ISA_EXT_DATA_ENTRY (zicsr,PRIV_VERSION_1_10_0 , ext_zicsr),ISA_EXT_DATA_ENTRY (zifencei,PRIV_VERSION_1_10_0 , ext_zifencei), ISA_EXT_DATA_ENTRY(zihintntl, PRIV_VERSION_1_10_0, ext_zihintntl),
@@ -1175,6 +1176,15 @@ static void riscv_cpu_init(Object *obj)qdev_init_gpio_in (DEVICE (obj), riscv_cpu_set_irq,IRQ_LOCAL_MAX +IRQ_LOCAL_GUEST_MAX );#endif /* CONFIG_USER_ONLY */ + +/* +* The timer and performance counters extensions were supported +* in QEMU before they were added as discrete extensions in the +* ISA. To keep compatibility we'll always default them to 'true' +* for all CPUs. Each accelerator will decide what to do when +* users disable them. +*/ +RISCV_CPU (obj)->cfg.ext_zicntr = true; } typedef struct misa_ext_info {
@@ -1263,6 +1273,8 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {MULTI_EXT_CFG_BOOL ("svnapot" , ext_svnapot, false),MULTI_EXT_CFG_BOOL ("svpbmt" , ext_svpbmt, false), +MULTI_EXT_CFG_BOOL ("zicntr" , ext_zicntr, true), +MULTI_EXT_CFG_BOOL ("zba" , ext_zba, true),MULTI_EXT_CFG_BOOL ("zbb" , ext_zbb, true), MULTI_EXT_CFG_BOOL("zbc", ext_zbc, true),
target/riscv/cpu_cfg.h +1 -0
@@ -62,6 +62,7 @@ struct RISCVCPUConfig {bool ext_zksh;bool ext_zkt;bool ext_zifencei; +bool ext_zicntr;bool ext_zicsr;bool ext_zicbom;bool ext_zicboz;
Signature · 1 line
2.41.0
PATCH v2 2/6 target/riscv/tcg: add ext_zicntr disable support
In reply to Daniel Henrique BarbozaSupport for the zicntr counters are already in place. We need a way to disable them if the user wants to. This is done by restricting access to the CYCLE, TIME, and INSTRET counters via the 'ctr()' predicate when we're about to access them.
Disabling zicntr happens via the command line or if its dependency, zicsr, happens to be disabled. We'll check for zicsr during realize() and, in case it's absent, disable zicntr. However, if the user was explicit about having zicntr support, error out instead of disabling it.
--- target/riscv/csr.c | 4 ++++ target/riscv/tcg/tcg-cpu.c | 8 ++++++++ 2 files changed, 12 insertions(+)
target/riscv/csr.c +4 -0
@@ -122,6 +122,10 @@ static RISCVException ctr(CPURISCVState *env, int csrno)if ((csrno >=CSR_CYCLE && csrno <=CSR_INSTRET ) || (csrno >=CSR_CYCLEH && csrno <=CSR_INSTRETH )) { +if (!riscv_cpu_cfg (env)->ext_zicntr) { +return RISCV_EXCP_ILLEGAL_INST ; + } + goto skip_ext_pmu_check; }
target/riscv/tcg/tcg-cpu.c +8 -0
@@ -541,6 +541,14 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)cpu_cfg_ext_auto_update (cpu,CPU_CFG_OFFSET (ext_zksh), true); } +if (cpu->cfg.ext_zicntr && !cpu->cfg.ext_zicsr) { +if (cpu_cfg_ext_is_user_set (CPU_CFG_OFFSET (ext_zicntr))) { +error_setg (errp,"zicntr requires zicsr" ); +return ; + } + cpu->cfg.ext_zicntr = false; + } +/* * Disable isa extensions based on priv spec after we * validated and set everything we need.
Signature · 1 line
2.41.0
Re: [PATCH v2 2/6] target/riscv/tcg: add ext_zicntr disable support
Quoted 6 lines · Daniel Henrique Barboza
Support for the zicntr counters are already in place. We need a way to disable them if the user wants to. This is done by restricting access to the CYCLE, TIME, and INSTRET counters via the '
ctr()' predicate when we're about to access them.
Quoted 6 lines
Disabling zicntr happens via the command line or if its dependency, zicsr, happens to be disabled. We'll check for zicsr during
realize()and, in case it's absent, disable zicntr. However, if the user was explicit about having zicntr support, error out instead of disabling it.Signed-off-by Daniel Henrique Barboza <[email protected]>
This should come before we expose the property to users though
Alistair
Quoted 43 lines
--- target/riscv/csr.c | 4 ++++ target/riscv/tcg/tcg-cpu.c | 8 ++++++++ 2 files changed, 12 insertions(+)target/riscv/csr.c +4
-0@@ -122,6 +122,10 @@ static RISCVException ctr(CPURISCVState *env, int csrno)if ((csrno >=CSR_CYCLE && csrno <=CSR_INSTRET ) || (csrno >=CSR_CYCLEH && csrno <=CSR_INSTRETH )) { +if (!riscv_cpu_cfg (env)->ext_zicntr) { +return RISCV_EXCP_ILLEGAL_INST ; + } + goto skip_ext_pmu_check; }target/riscv/tcg/tcg-cpu.c +8
-1@@ -541,6 +541,14 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)cpu_cfg_ext_auto_update (cpu,CPU_CFG_OFFSET (ext_zksh), true); } +if (cpu->cfg.ext_zicntr && !cpu->cfg.ext_zicsr) { +if (cpu_cfg_ext_is_user_set (CPU_CFG_OFFSET (ext_zicntr))) { +error_setg (errp,"zicntr requires zicsr" ); +return ; + } + cpu->cfg.ext_zicntr = false; + } +/* * Disable isa extensions based on priv spec after we * validated and set everything we need. -- 2.41.0
Re: [PATCH v2 2/6] target/riscv/tcg: add ext_zicntr disable support
Quoted 13 lines · Alistair Francis
Quoted 11 lines · Daniel Henrique Barboza
Support for the zicntr counters are already in place. We need a way to disable them if the user wants to. This is done by restricting access to the CYCLE, TIME, and INSTRET counters via the '
ctr()' predicate when we're about to access them.Disabling zicntr happens via the command line or if its dependency, zicsr, happens to be disabled. We'll check for zicsr during
realize()and, in case it's absent, disable zicntr. However, if the user was explicit about having zicntr support, error out instead of disabling it.
Quoted 5 lines
Quoted 1 line
Signed-off-by Daniel Henrique Barboza <[email protected]>Reviewed-by Alistair Francis <[email protected]>This should come before we expose the property to users though
I'll merge this patch with patch 1. Same thing with patch 4 and 5.
Thanks,
Daniel
Quoted 46 lines
Alistair
Quoted 43 lines
--- target/riscv/csr.c | 4 ++++ target/riscv/tcg/tcg-cpu.c | 8 ++++++++ 2 files changed, 12 insertions(+)target/riscv/csr.c +4
-0@@ -122,6 +122,10 @@ static RISCVException ctr(CPURISCVState *env, int csrno)if ((csrno >=CSR_CYCLE && csrno <=CSR_INSTRET ) || (csrno >=CSR_CYCLEH && csrno <=CSR_INSTRETH )) { +if (!riscv_cpu_cfg (env)->ext_zicntr) { +return RISCV_EXCP_ILLEGAL_INST ; + } + goto skip_ext_pmu_check; }target/riscv/tcg/tcg-cpu.c +8
-1@@ -541,6 +541,14 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)cpu_cfg_ext_auto_update (cpu,CPU_CFG_OFFSET (ext_zksh), true); } +if (cpu->cfg.ext_zicntr && !cpu->cfg.ext_zicsr) { +if (cpu_cfg_ext_is_user_set (CPU_CFG_OFFSET (ext_zicntr))) { +error_setg (errp,"zicntr requires zicsr" ); +return ; + } + cpu->cfg.ext_zicntr = false; + } +/* * Disable isa extensions based on priv spec after we * validated and set everything we need. -- 2.41.0
PATCH v2 3/6 target/riscv/kvm: add zicntr reg
In reply to Daniel Henrique BarbozaAdd zicntr support in the KVM driver now that QEMU supports it.
This reg was added in Linux 6.6.
--- target/riscv/kvm/kvm-cpu.c | 1 + 1 file changed, 1 insertion(+)
target/riscv/kvm/kvm-cpu.c +1 -0
@@ -215,6 +215,7 @@ static void kvm_riscv_update_cpu_misa_ext(RISCVCPU *cpu, CPUState *cs)static KVMCPUConfig kvm_multi_ext_cfgs[] = {KVM_EXT_CFG ("zicbom" , ext_zicbom,KVM_RISCV_ISA_EXT_ZICBOM ),KVM_EXT_CFG ("zicboz" , ext_zicboz,KVM_RISCV_ISA_EXT_ZICBOZ ), +KVM_EXT_CFG ("zicntr" , ext_zicntr,KVM_RISCV_ISA_EXT_ZICNTR ),KVM_EXT_CFG ("zihintpause" , ext_zihintpause,KVM_RISCV_ISA_EXT_ZIHINTPAUSE ),KVM_EXT_CFG ("zbb" , ext_zbb,KVM_RISCV_ISA_EXT_ZBB ),KVM_EXT_CFG ("ssaia" , ext_ssaia,KVM_RISCV_ISA_EXT_SSAIA ),
Signature · 1 line
2.41.0
Re: [PATCH v2 3/6] target/riscv/kvm: add zicntr reg
Quoted 6 lines · Daniel Henrique Barboza
Add zicntr support in the KVM driver now that QEMU supports it.
This reg was added in Linux 6.6.
Signed-off-by Daniel Henrique Barboza <[email protected]>
Alistair
Quoted 20 lines
--- target/riscv/kvm/kvm-cpu.c | 1 + 1 file changed, 1 insertion(+)target/riscv/kvm/kvm-cpu.c +1
-1@@ -215,6 +215,7 @@ static void kvm_riscv_update_cpu_misa_ext(RISCVCPU *cpu, CPUState *cs)static KVMCPUConfig kvm_multi_ext_cfgs[] = {KVM_EXT_CFG ("zicbom" , ext_zicbom,KVM_RISCV_ISA_EXT_ZICBOM ),KVM_EXT_CFG ("zicboz" , ext_zicboz,KVM_RISCV_ISA_EXT_ZICBOZ ), +KVM_EXT_CFG ("zicntr" , ext_zicntr,KVM_RISCV_ISA_EXT_ZICNTR ),KVM_EXT_CFG ("zihintpause" , ext_zihintpause,KVM_RISCV_ISA_EXT_ZIHINTPAUSE ),KVM_EXT_CFG ("zbb" , ext_zbb,KVM_RISCV_ISA_EXT_ZBB ),KVM_EXT_CFG ("ssaia" , ext_ssaia,KVM_RISCV_ISA_EXT_SSAIA ), --2.41.0
PATCH v2 4/6 target/riscv/cpu.c: add zihpm extension flag
In reply to Daniel Henrique Barbozazihpm is the Hardware Performance Counters extension described in chapter 12 of the unprivileged spec. It describes support for 29 unprivileged performance counters, hpmcounter3-hpmcounter31.
As with zicntr, QEMU already implements zihpm before it was even an extension. zihpm is also part of the RVA22 profile, so add it to QEMU to complement the future profile implementation.
Default it to 'true' for all existing CPUs since it was always present in the code.
--- target/riscv/cpu.c | 3 +++ target/riscv/cpu_cfg.h | 1 + 2 files changed, 4 insertions(+)
target/riscv/cpu.c +3 -0
@@ -84,6 +84,7 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (zifencei,PRIV_VERSION_1_10_0 , ext_zifencei),ISA_EXT_DATA_ENTRY (zihintntl,PRIV_VERSION_1_10_0 , ext_zihintntl),ISA_EXT_DATA_ENTRY (zihintpause,PRIV_VERSION_1_10_0 , ext_zihintpause), +ISA_EXT_DATA_ENTRY (zihpm,PRIV_VERSION_1_12_0 , ext_zihpm),ISA_EXT_DATA_ENTRY (zmmul,PRIV_VERSION_1_12_0 , ext_zmmul),ISA_EXT_DATA_ENTRY (zawrs,PRIV_VERSION_1_12_0 , ext_zawrs), ISA_EXT_DATA_ENTRY(zfa, PRIV_VERSION_1_12_0, ext_zfa),
@@ -1185,6 +1186,7 @@ static void riscv_cpu_init(Object *obj)* users disable them. */ RISCV_CPU (obj)->cfg.ext_zicntr = true; +RISCV_CPU (obj)->cfg.ext_zihpm = true; } typedef struct misa_ext_info {
@@ -1274,6 +1276,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {MULTI_EXT_CFG_BOOL ("svpbmt" , ext_svpbmt, false),MULTI_EXT_CFG_BOOL ("zicntr" , ext_zicntr, true), +MULTI_EXT_CFG_BOOL ("zihpm" , ext_zihpm, true),MULTI_EXT_CFG_BOOL ("zba" , ext_zba, true), MULTI_EXT_CFG_BOOL("zbb", ext_zbb, true),
target/riscv/cpu_cfg.h +1 -0
@@ -69,6 +69,7 @@ struct RISCVCPUConfig {bool ext_zicond;bool ext_zihintntl;bool ext_zihintpause; +bool ext_zihpm;bool ext_smstateen;bool ext_sstc;bool ext_svadu;
Signature · 1 line
2.41.0
Re: [PATCH v2 4/6] target/riscv/cpu.c: add zihpm extension flag
Quoted 39 lines · Daniel Henrique Barboza
zihpm is the Hardware Performance Counters extension described in chapter 12 of the unprivileged spec. It describes support for 29 unprivileged performance counters, hpmcounter3-hpmcounter31.
As with zicntr, QEMU already implements zihpm before it was even an extension. zihpm is also part of the RVA22 profile, so add it to QEMU to complement the future profile implementation.
Default it to 'true' for all existing CPUs since it was always present in the code.
Signed-off-by Daniel Henrique Barboza <[email protected]>--- target/riscv/cpu.c | 3 +++ target/riscv/cpu_cfg.h | 1 + 2 files changed, 4 insertions(+)target/riscv/cpu.c +2
-0@@ -84,6 +84,7 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (zifencei,PRIV_VERSION_1_10_0 , ext_zifencei),ISA_EXT_DATA_ENTRY (zihintntl,PRIV_VERSION_1_10_0 , ext_zihintntl),ISA_EXT_DATA_ENTRY (zihintpause,PRIV_VERSION_1_10_0 , ext_zihintpause), +ISA_EXT_DATA_ENTRY (zihpm,PRIV_VERSION_1_12_0 , ext_zihpm),ISA_EXT_DATA_ENTRY (zmmul,PRIV_VERSION_1_12_0 , ext_zmmul),ISA_EXT_DATA_ENTRY (zawrs,PRIV_VERSION_1_12_0 , ext_zawrs), ISA_EXT_DATA_ENTRY(zfa, PRIV_VERSION_1_12_0, ext_zfa),@@ -1185,6 +1186,7 @@ static void riscv_cpu_init(Object *obj)* users disable them. */ RISCV_CPU (obj)->cfg.ext_zicntr = true; +RISCV_CPU (obj)->cfg.ext_zihpm = true; } typedef struct misa_ext_info {
target/riscv/cpu.c const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = { · line 1279
Quoted 4 lines
target/riscv/cpu.c +1
-0@@ -1274,6 +1276,7 @@ const RISCVCPUMultiExtConfig riscv_cpu_extensions[] = {MULTI_EXT_CFG_BOOL ("svpbmt" , ext_svpbmt, false),MULTI_EXT_CFG_BOOL ("zicntr" , ext_zicntr, true), + MULTI_EXT_CFG_BOOL("zihpm", ext_zihpm, true),
Same here. Just make this change after or with the logic to disable the extension
Alistair
Quoted 19 lines
target/riscv/cpu.c +0
-0MULTI_EXT_CFG_BOOL ("zba" , ext_zba, true), MULTI_EXT_CFG_BOOL("zbb", ext_zbb, true),target/riscv/cpu_cfg.h +1
-1@@ -69,6 +69,7 @@ struct RISCVCPUConfig {bool ext_zicond;bool ext_zihintntl;bool ext_zihintpause; +bool ext_zihpm;bool ext_smstateen;bool ext_sstc;bool ext_svadu; --2.41.0
PATCH v2 5/6 target/riscv/tcg: add ext_zihpm disable support
In reply to Daniel Henrique BarbozaDisabling ext_zihpm does nothing at this moment. Add support to disable the hpmcounter3-hpmcounter31 counters if the user disables zihpm.
There is already code in place in target/riscv/csr.c in all predicates for these counters (ctr() and mctr()) that disables them if cpu->cfg.pmu_num is zero. Thus, setting cpu->cfg.pmu_num to zero if 'zihpm=false' is enough to disable the extension.
Set cpu->pmu_avail_ctrs mask to zero as well since this is also checked to verify if the counters exist.
--- target/riscv/tcg/tcg-cpu.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
target/riscv/tcg/tcg-cpu.c +13 -0
@@ -549,6 +549,19 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) cpu->cfg.ext_zicntr = false; } +if (cpu->cfg.ext_zihpm && !cpu->cfg.ext_zicsr) { +if (cpu_cfg_ext_is_user_set (CPU_CFG_OFFSET (ext_zihpm))) { +error_setg (errp,"zihpm requires zicsr" ); +return ; + } + cpu->cfg.ext_zihpm = false; + } + +if (!cpu->cfg.ext_zihpm) { + cpu->cfg.pmu_num =0 ; + cpu->pmu_avail_ctrs =0 ; + } +/* * Disable isa extensions based on priv spec after we * validated and set everything we need.
Signature · 1 line
2.41.0
PATCH v2 6/6 target/riscv/kvm: add zihpm reg
In reply to Daniel Henrique BarbozaAdd zihpm support in the KVM driver now that QEMU supports it.
This reg was added in Linux 6.6.
--- target/riscv/kvm/kvm-cpu.c | 1 + 1 file changed, 1 insertion(+)
target/riscv/kvm/kvm-cpu.c +1 -0
@@ -217,6 +217,7 @@ static KVMCPUConfig kvm_multi_ext_cfgs[] = {KVM_EXT_CFG ("zicboz" , ext_zicboz,KVM_RISCV_ISA_EXT_ZICBOZ ),KVM_EXT_CFG ("zicntr" , ext_zicntr,KVM_RISCV_ISA_EXT_ZICNTR ),KVM_EXT_CFG ("zihintpause" , ext_zihintpause,KVM_RISCV_ISA_EXT_ZIHINTPAUSE ), +KVM_EXT_CFG ("zihpm" , ext_zihpm,KVM_RISCV_ISA_EXT_ZIHPM ),KVM_EXT_CFG ("zbb" , ext_zbb,KVM_RISCV_ISA_EXT_ZBB ),KVM_EXT_CFG ("ssaia" , ext_ssaia,KVM_RISCV_ISA_EXT_SSAIA ), KVM_EXT_CFG("sstc", ext_sstc, KVM_RISCV_ISA_EXT_SSTC),
Signature · 1 line
2.41.0
Re: [PATCH v2 6/6] target/riscv/kvm: add zihpm reg
Quoted 6 lines · Daniel Henrique Barboza
Add zihpm support in the KVM driver now that QEMU supports it.
This reg was added in Linux 6.6.
Signed-off-by Daniel Henrique Barboza <[email protected]>
Alistair
Quoted 20 lines
--- target/riscv/kvm/kvm-cpu.c | 1 + 1 file changed, 1 insertion(+)target/riscv/kvm/kvm-cpu.c +1
-1@@ -217,6 +217,7 @@ static KVMCPUConfig kvm_multi_ext_cfgs[] = {KVM_EXT_CFG ("zicboz" , ext_zicboz,KVM_RISCV_ISA_EXT_ZICBOZ ),KVM_EXT_CFG ("zicntr" , ext_zicntr,KVM_RISCV_ISA_EXT_ZICNTR ),KVM_EXT_CFG ("zihintpause" , ext_zihintpause,KVM_RISCV_ISA_EXT_ZIHINTPAUSE ), +KVM_EXT_CFG ("zihpm" , ext_zihpm,KVM_RISCV_ISA_EXT_ZIHPM ),KVM_EXT_CFG ("zbb" , ext_zbb,KVM_RISCV_ISA_EXT_ZBB ),KVM_EXT_CFG ("ssaia" , ext_ssaia,KVM_RISCV_ISA_EXT_SSAIA ), KVM_EXT_CFG("sstc", ext_sstc, KVM_RISCV_ISA_EXT_SSTC), --2.41.0