Improve PMU support
Subsystems (2)
v10 cover Improve PMU support
The latest version of the SBI specification includes a Performance Monitoring Unit(PMU) extension[1] which allows the supervisor to start/stop/configure various PMU events. The Sscofpmf ('Ss' for Privileged arch and Supervisor-level extensions, and 'cofpmf' for Count OverFlow and Privilege Mode Filtering) extension[2] allows the perf like tool to handle overflow interrupts and filtering support.
This series implements full PMU infrastructure to support PMU in virt machine. This will allow us to add any PMU events in future.
Currently, this series enables the following omu events.
1. cycle count
2. instruction count
3. DTLB load/store miss
4. ITLB prefetch miss
The first two are computed using host ticks while last three are counted during cpu_tlb_fill. We can do both sampling and count from guest userspace. This series has been tested on both RV64 and RV32. Both Linux[3] and Opensbi[4] patches are required to get the perf working.
Here is an output of perf stat/report while running hackbench with latest OpenSBI & Linux kernel.
Perf stat:
==========
[root@fedora-riscv ~]# perf stat -e cycles -e instructions -e dTLB-load-misses -e dTLB-store-misses -e iTLB-load-misses \
Quoted 1 line
perf bench sched messaging -g 1 -l 10
# Running 'sched/messaging' benchmark:
# 20 sender and receiver processes per group
# 1 groups == 40 processes run
Total time: 0.265 [sec]
Performance counter stats for 'perf bench sched messaging -g 1 -l 10':
4,167,825,362 cycles
4,166,609,256 instructions # 1.00 insn per cycle
3,092,026 dTLB-load-misses
258,280 dTLB-store-misses
2,068,966 iTLB-load-misses 0.585791767 seconds time elapsed
0.373802000 seconds user
1.042359000 seconds sysPerf record:
============
[root@fedora-riscv ~]# perf record -e cycles -e instructions \
Quoted 2 lines
-e dTLB-load-misses -e dTLB-store-misses -e iTLB-load-misses -c 10000 \
perf bench sched messaging -g 1 -l 10
# Running 'sched/messaging' benchmark:
# 20 sender and receiver processes per group
# 1 groups == 40 processes run
Total time: 1.397 [sec]
[ perf record: Woken up 10 times to write data ]
Check IO/CPU overload!
[ perf record: Captured and wrote 8.211 MB perf.data (214486 samples) ]
[root@fedora-riscv riscv]# perf report
Available samples
107K cycles ◆ 107K instructions ▒ 250 dTLB-load-misses ▒ 13 dTLB-store-misses ▒
172 iTLB-load-misses
..
Changes from v8->v9:
1. Added the write_done flags to the vmstate.
2. Fixed the hpmcounter read access from M-mode.
Changes from v7->v8:
1. Removeding ordering constraints for mhpmcounter & mhpmevent.
Changes from v6->v7:
1. Fixed all the compilation errors for the usermode.
Changes from v5->v6:
1. Fixed compilation issue with PATCH 1.
2. Addressed other comments.
Changes from v4->v5:
1. Rebased on top of the -next with following patches.
- isa extension
- priv 1.12 spec
2. Addressed all the comments on v4
3. Removed additional isa-ext DT node in favor of riscv,isa string update
Changes from v3->v4:
1. Removed the dummy events from pmu DT node.
2. Fixed pmu_avail_counters mask generation.
3. Added a patch to simplify the predicate function for counters.
Changes from v2->v3:
1. Addressed all the comments on PATCH1-4.
2. Split patch1 into two separate patches.
3. Added explicit comments to explain the event types in DT node.
4. Rebased on latest Qemu.
Changes from v1->v2:
1. Dropped the ACks from v1 as signficant changes happened after v1.
2. sscofpmf support.
3. A generic counter management framework.
[1] https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/riscv-sbi.adoc
[2] https://drive.google.com/file/d/171j4jFjIkKdj5LWcExphq4xG_2sihbfd/edit
[3] https://github.com/atishp04/qemu/tree/riscv_pmu_v10
Atish Patra (12):
target/riscv: Fix PMU CSR predicate function target/riscv: Implement PMU CSR predicate function for S-mode target/riscv: pmu: Rename the counters extension to pmu target/riscv: pmu: Make number of counters configurable target/riscv: Implement mcountinhibit CSR target/riscv: Add support for hpmcounters/hpmevents target/riscv: Support mcycle/minstret write operation target/riscv: Add sscofpmf extension support target/riscv: Simplify counter predicate function target/riscv: Add few cache related PMU events hw/riscv: virt: Add PMU DT node to the device tree target/riscv: Update the privilege field for sscofpmf CSRs
hw/riscv/virt.c | 28 ++ target/riscv/cpu.c | 15 +- target/riscv/cpu.h | 49 ++- target/riscv/cpu_bits.h | 59 +++ target/riscv/cpu_helper.c | 25 ++ target/riscv/csr.c | 892 ++++++++++++++++++++++++++++---------- target/riscv/machine.c | 26 ++ target/riscv/meson.build | 3 +- target/riscv/pmu.c | 442 +++++++++++++++++++ target/riscv/pmu.h | 36 ++ 10 files changed, 1339 insertions(+), 236 deletions(-)
create mode 100644 target/riscv/pmu.c
create mode 100644 target/riscv/pmu.h
--
2.25.1
PATCH v10 1/12 target/riscv: Fix PMU CSR predicate function
From: Atish Patra <[email protected]>
The predicate function calculates the counter index incorrectly for hpmcounterx. Fix the counter index to reflect correct CSR number.
--- target/riscv/csr.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
target/riscv/csr.c +7 -4
@@ -72,6 +72,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno)#if !defined(CONFIG_USER_ONLY )CPUState *cs =env_cpu (env);RISCVCPU *cpu =RISCV_CPU (cs); +int ctr_index;if (!cpu->cfg.ext_counters) {/* The Counters extensions is not enabled */
@@ -99,8 +100,9 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: -if (!get_field (env->hcounteren,1 << (csrno -CSR_HPMCOUNTER3 )) && -get_field (env->mcounteren,1 << (csrno -CSR_HPMCOUNTER3 ))) { + ctr_index = csrno -CSR_CYCLE ; +if (!get_field (env->hcounteren,1 << ctr_index) && +get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; }break ;
@@ -126,8 +128,9 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: -if (!get_field (env->hcounteren,1 << (csrno -CSR_HPMCOUNTER3H )) && -get_field (env->mcounteren,1 << (csrno -CSR_HPMCOUNTER3H ))) { + ctr_index = csrno -CSR_CYCLEH ; +if (!get_field (env->hcounteren,1 << ctr_index) && +get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; }break ;
Signature · 1 line
2.25.1
PATCH v10 2/12 target/riscv: Implement PMU CSR predicate function for S-mode
In reply to Atish PatraFrom: Atish Patra <[email protected]>
Currently, the predicate function for PMU related CSRs only works if virtualization is enabled. It also does not check mcounteren bits before before cycle/minstret/hpmcounterx access.
Support supervisor mode access in the predicate function as well.
--- target/riscv/csr.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+)
target/riscv/csr.c +51 -0
@@ -79,6 +79,57 @@ static RISCVException ctr(CPURISCVState *env, int csrno)return RISCV_EXCP_ILLEGAL_INST ; } +if (env->priv ==PRV_S ) { +switch (csrno) { +case CSR_CYCLE : +if (!get_field (env->mcounteren,COUNTEREN_CY )) { +return RISCV_EXCP_ILLEGAL_INST ; + } +break ; +case CSR_TIME : +if (!get_field (env->mcounteren,COUNTEREN_TM )) { +return RISCV_EXCP_ILLEGAL_INST ; + } +break ; +case CSR_INSTRET : +if (!get_field (env->mcounteren,COUNTEREN_IR )) { +return RISCV_EXCP_ILLEGAL_INST ; + } +break ; + case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: + ctr_index = csrno -CSR_CYCLE ; +if (!get_field (env->mcounteren,1 << ctr_index)) { +return RISCV_EXCP_ILLEGAL_INST ; + } +break ; + } +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { +switch (csrno) { +case CSR_CYCLEH : +if (!get_field (env->mcounteren,COUNTEREN_CY )) { +return RISCV_EXCP_ILLEGAL_INST ; + } +break ; +case CSR_TIMEH : +if (!get_field (env->mcounteren,COUNTEREN_TM )) { +return RISCV_EXCP_ILLEGAL_INST ; + } +break ; +case CSR_INSTRETH : +if (!get_field (env->mcounteren,COUNTEREN_IR )) { +return RISCV_EXCP_ILLEGAL_INST ; + } +break ; + case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: + ctr_index = csrno -CSR_CYCLEH ; +if (!get_field (env->mcounteren,1 << ctr_index)) { +return RISCV_EXCP_ILLEGAL_INST ; + } +break ; + } + } + } +if (riscv_cpu_virt_enabled (env)) {switch (csrno) {case CSR_CYCLE :
Signature · 1 line
2.25.1
PATCH v10 3/12 target/riscv: pmu: Rename the counters extension to pmu
In reply to Atish PatraFrom: Atish Patra <[email protected]>
The PMU counters are supported via cpu config "Counters" which doesn't indicate the correct purpose of those counters.
Rename the config property to pmu to indicate that these counters are performance monitoring counters. This aligns with cpu options for ARM architecture as well.
--- target/riscv/cpu.c | 4 ++-- target/riscv/cpu.h | 2 +- target/riscv/csr.c | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-)
target/riscv/cpu.c +2 -2
@@ -851,7 +851,7 @@ static void riscv_cpu_init(Object *obj) {RISCVCPU *cpu =RISCV_CPU (obj); - cpu->cfg.ext_counters = true; + cpu->cfg.ext_pmu = true; cpu->cfg.ext_ifencei = true; cpu->cfg.ext_icsr = true; cpu->cfg.mmu = true;
@@ -879,7 +879,7 @@ static Property riscv_cpu_extensions[] = {DEFINE_PROP_BOOL ("u" ,RISCVCPU , cfg.ext_u, true),DEFINE_PROP_BOOL ("v" ,RISCVCPU , cfg.ext_v, false),DEFINE_PROP_BOOL ("h" ,RISCVCPU , cfg.ext_h, true), -DEFINE_PROP_BOOL ("Counters" ,RISCVCPU , cfg.ext_counters, true), +DEFINE_PROP_BOOL ("pmu" ,RISCVCPU , cfg.ext_pmu, true),DEFINE_PROP_BOOL ("Zifencei" ,RISCVCPU , cfg.ext_ifencei, true),DEFINE_PROP_BOOL ("Zicsr" ,RISCVCPU , cfg.ext_icsr, true), DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),
target/riscv/cpu.h +1 -1
@@ -397,7 +397,7 @@ struct RISCVCPUConfig {bool ext_zksed;bool ext_zksh;bool ext_zkt; -bool ext_counters; +bool ext_pmu;bool ext_ifencei;bool ext_icsr;bool ext_svinval;
target/riscv/csr.c +2 -2
@@ -74,8 +74,8 @@ static RISCVException ctr(CPURISCVState *env, int csrno)RISCVCPU *cpu =RISCV_CPU (cs);int ctr_index; -if (!cpu->cfg.ext_counters) { -/* The Counters extensions is not enabled */ +if (!cpu->cfg.ext_pmu) { +/* The PMU extension is not enabled */ return RISCV_EXCP_ILLEGAL_INST ; }
Signature · 1 line
2.25.1
PATCH v10 4/12 target/riscv: pmu: Make number of counters configurable
In reply to Atish PatraThe RISC-V privilege specification provides flexibility to implement any number of counters from 29 programmable counters. However, the QEMU implements all the counters.
Make it configurable through pmu config parameter which now will indicate how many programmable counters should be implemented by the cpu.
--- target/riscv/cpu.c | 3 +- target/riscv/cpu.h | 2 +- target/riscv/csr.c | 94 ++++++++++++++++++++++++++++++---------------- 3 files changed, 63 insertions(+), 36 deletions(-)
target/riscv/cpu.c +1 -2
@@ -851,7 +851,6 @@ static void riscv_cpu_init(Object *obj) {RISCVCPU *cpu =RISCV_CPU (obj); - cpu->cfg.ext_pmu = true; cpu->cfg.ext_ifencei = true; cpu->cfg.ext_icsr = true; cpu->cfg.mmu = true;
@@ -879,7 +878,7 @@ static Property riscv_cpu_extensions[] = {DEFINE_PROP_BOOL ("u" ,RISCVCPU , cfg.ext_u, true),DEFINE_PROP_BOOL ("v" ,RISCVCPU , cfg.ext_v, false),DEFINE_PROP_BOOL ("h" ,RISCVCPU , cfg.ext_h, true), -DEFINE_PROP_BOOL ("pmu" ,RISCVCPU , cfg.ext_pmu, true), +DEFINE_PROP_UINT8 ("pmu-num" ,RISCVCPU , cfg.pmu_num,16 ),DEFINE_PROP_BOOL ("Zifencei" ,RISCVCPU , cfg.ext_ifencei, true),DEFINE_PROP_BOOL ("Zicsr" ,RISCVCPU , cfg.ext_icsr, true), DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),
target/riscv/cpu.h +1 -1
@@ -397,7 +397,6 @@ struct RISCVCPUConfig {bool ext_zksed;bool ext_zksh;bool ext_zkt; -bool ext_pmu;bool ext_ifencei;bool ext_icsr;bool ext_svinval;
@@ -421,6 +420,7 @@ struct RISCVCPUConfig {/* Vendor-specific custom extensions */ bool ext_XVentanaCondOps; +uint8_t pmu_num;char *priv_spec;char *user_spec;char *bext_spec;
target/riscv/csr.c +61 -33
125 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v10 04/12] target/riscv: pmu: Make number of counters configurable
Quoted 30 lines · Atish Patra
The RISC-V privilege specification provides flexibility to implement any number of counters from 29 programmable counters. However, the QEMU implements all the counters.
Make it configurable through pmu config parameter which now will indicate how many programmable counters should be implemented by the cpu.
Reviewed-by Bin Meng <[email protected]>Reviewed-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>--- target/riscv/cpu.c | 3 +- target/riscv/cpu.h | 2 +- target/riscv/csr.c | 94 ++++++++++++++++++++++++++++++---------------- 3 files changed, 63 insertions(+), 36 deletions(-)target/riscv/cpu.c +0
-1@@ -851,7 +851,6 @@ static void riscv_cpu_init(Object *obj) {RISCVCPU *cpu =RISCV_CPU (obj); - cpu->cfg.ext_pmu = true; cpu->cfg.ext_ifencei = true; cpu->cfg.ext_icsr = true; cpu->cfg.mmu = true;
target/riscv/cpu.c static Property riscv_cpu_extensions[] = { · line 881
Quoted 5 lines
target/riscv/cpu.c +1
-1@@ -879,7 +878,7 @@ static Property riscv_cpu_extensions[] = {DEFINE_PROP_BOOL ("u" ,RISCVCPU , cfg.ext_u, true),DEFINE_PROP_BOOL ("v" ,RISCVCPU , cfg.ext_v, false),DEFINE_PROP_BOOL ("h" ,RISCVCPU , cfg.ext_h, true), - DEFINE_PROP_BOOL("pmu", RISCVCPU, cfg.ext_pmu, true), + DEFINE_PROP_UINT8("pmu-num", RISCVCPU, cfg.pmu_num, 16),
I think It's better to add a check on cfg.pmu_num to <= 29.
Quoted 34 lines
target/riscv/cpu.c +0
-0DEFINE_PROP_BOOL ("Zifencei" ,RISCVCPU , cfg.ext_ifencei, true),DEFINE_PROP_BOOL ("Zicsr" ,RISCVCPU , cfg.ext_icsr, true), DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),target/riscv/cpu.h +1
-1@@ -397,7 +397,6 @@ struct RISCVCPUConfig {bool ext_zksed;bool ext_zksh;bool ext_zkt; -bool ext_pmu;bool ext_ifencei;bool ext_icsr;bool ext_svinval;@@ -421,6 +420,7 @@ struct RISCVCPUConfig {/* Vendor-specific custom extensions */ bool ext_XVentanaCondOps; +uint8_t pmu_num;char *priv_spec;char *user_spec;char *bext_spec;target/riscv/csr.c +2
-0@@ -73,9 +73,17 @@ static RISCVException ctr(CPURISCVState *env, int csrno)CPUState *cs =env_cpu (env);RISCVCPU *cpu =RISCV_CPU (cs);int ctr_index; +int base_csrno =CSR_HPMCOUNTER3 ; +bool rv32 =riscv_cpu_mxl (env) ==MXL_RV32 ? true : false;
target/riscv/csr.c static RISCVException ctr(CPURISCVState *env, int csrno) · line 86
Quoted 10 lines
target/riscv/csr.c +8
-2-if (!cpu->cfg.ext_pmu) { -/* The PMU extension is not enabled */ +if (rv32 && csrno >=CSR_CYCLEH ) { +/* Offset for RV32 hpmcounternh counters */ + base_csrno +=0x80 ; + } + ctr_index = csrno - base_csrno; + +if (!cpu->cfg.pmu_num || ctr_index >= (cpu->cfg.pmu_num)) { +/* No counter is enabled in PMU or the counter is out of range */
I seems unnecessary to add '!cpu->cfg.pmu_num ' here, 'ctr_index >= (cpu->cfg.pmu_num)' is true
when cpu->cfg.pmu_num is zero if the problem for base_csrno is fixed.
Ragards,
Weiwei Li
Quoted 113 lines
target/riscv/csr.c +51
-31return RISCV_EXCP_ILLEGAL_INST ; }@@ -103,7 +111,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; } -if (riscv_cpu_mxl (env) ==MXL_RV32 ) { +if (rv32) {switch (csrno) {case CSR_CYCLEH :if (!get_field (env->mcounteren,COUNTEREN_CY )) {@@ -158,7 +166,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; } -if (riscv_cpu_mxl (env) ==MXL_RV32 ) { +if (rv32) {switch (csrno) {case CSR_CYCLEH : if (!get_field(env->hcounteren, COUNTEREN_CY) &&@@ -202,6 +210,26 @@ static RISCVException ctr32(CPURISCVState *env, int csrno) }#if !defined(CONFIG_USER_ONLY ) +static RISCVException mctr (CPURISCVState *env,int csrno) +{ +CPUState *cs =env_cpu (env); +RISCVCPU *cpu =RISCV_CPU (cs); +int ctr_index; +int base_csrno =CSR_MHPMCOUNTER3 ; + +if ((riscv_cpu_mxl (env) ==MXL_RV32 ) && csrno >=CSR_MCYCLEH ) { +/* Offset for RV32 mhpmcounternh counters */ + base_csrno +=0x80 ; + } + ctr_index = csrno - base_csrno; +if (!cpu->cfg.pmu_num || ctr_index >= cpu->cfg.pmu_num) { +/* The PMU is not enabled or counter is out of range*/ +return RISCV_EXCP_ILLEGAL_INST ; + } + +return RISCV_EXCP_NONE ; +} +static RISCVException any (CPURISCVState *env,int csrno) {return RISCV_EXCP_NONE ;@@ -3687,35 +3715,35 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_HPMCOUNTER30] = { "hpmcounter30", ctr, read_zero }, [CSR_HPMCOUNTER31] = { "hpmcounter31", ctr, read_zero }, - [CSR_MHPMCOUNTER3] = { "mhpmcounter3", any, read_zero }, - [CSR_MHPMCOUNTER4] = { "mhpmcounter4", any, read_zero }, - [CSR_MHPMCOUNTER5] = { "mhpmcounter5", any, read_zero }, - [CSR_MHPMCOUNTER6] = { "mhpmcounter6", any, read_zero }, - [CSR_MHPMCOUNTER7] = { "mhpmcounter7", any, read_zero }, - [CSR_MHPMCOUNTER8] = { "mhpmcounter8", any, read_zero }, - [CSR_MHPMCOUNTER9] = { "mhpmcounter9", any, read_zero }, - [CSR_MHPMCOUNTER10] = { "mhpmcounter10", any, read_zero }, - [CSR_MHPMCOUNTER11] = { "mhpmcounter11", any, read_zero }, - [CSR_MHPMCOUNTER12] = { "mhpmcounter12", any, read_zero }, - [CSR_MHPMCOUNTER13] = { "mhpmcounter13", any, read_zero }, - [CSR_MHPMCOUNTER14] = { "mhpmcounter14", any, read_zero }, - [CSR_MHPMCOUNTER15] = { "mhpmcounter15", any, read_zero }, - [CSR_MHPMCOUNTER16] = { "mhpmcounter16", any, read_zero }, - [CSR_MHPMCOUNTER17] = { "mhpmcounter17", any, read_zero }, - [CSR_MHPMCOUNTER18] = { "mhpmcounter18", any, read_zero }, - [CSR_MHPMCOUNTER19] = { "mhpmcounter19", any, read_zero }, - [CSR_MHPMCOUNTER20] = { "mhpmcounter20", any, read_zero }, - [CSR_MHPMCOUNTER21] = { "mhpmcounter21", any, read_zero }, - [CSR_MHPMCOUNTER22] = { "mhpmcounter22", any, read_zero }, - [CSR_MHPMCOUNTER23] = { "mhpmcounter23", any, read_zero }, - [CSR_MHPMCOUNTER24] = { "mhpmcounter24", any, read_zero }, - [CSR_MHPMCOUNTER25] = { "mhpmcounter25", any, read_zero }, - [CSR_MHPMCOUNTER26] = { "mhpmcounter26", any, read_zero }, - [CSR_MHPMCOUNTER27] = { "mhpmcounter27", any, read_zero }, - [CSR_MHPMCOUNTER28] = { "mhpmcounter28", any, read_zero }, - [CSR_MHPMCOUNTER29] = { "mhpmcounter29", any, read_zero }, - [CSR_MHPMCOUNTER30] = { "mhpmcounter30", any, read_zero }, - [CSR_MHPMCOUNTER31] = { "mhpmcounter31", any, read_zero }, + [CSR_MHPMCOUNTER3] = { "mhpmcounter3", mctr, read_zero }, + [CSR_MHPMCOUNTER4] = { "mhpmcounter4", mctr, read_zero }, + [CSR_MHPMCOUNTER5] = { "mhpmcounter5", mctr, read_zero }, + [CSR_MHPMCOUNTER6] = { "mhpmcounter6", mctr, read_zero }, + [CSR_MHPMCOUNTER7] = { "mhpmcounter7", mctr, read_zero }, + [CSR_MHPMCOUNTER8] = { "mhpmcounter8", mctr, read_zero }, + [CSR_MHPMCOUNTER9] = { "mhpmcounter9", mctr, read_zero }, + [CSR_MHPMCOUNTER10] = { "mhpmcounter10", mctr, read_zero }, + [CSR_MHPMCOUNTER11] = { "mhpmcounter11", mctr, read_zero }, + [CSR_MHPMCOUNTER12] = { "mhpmcounter12", mctr, read_zero }, + [CSR_MHPMCOUNTER13] = { "mhpmcounter13", mctr, read_zero }, + [CSR_MHPMCOUNTER14] = { "mhpmcounter14", mctr, read_zero }, + [CSR_MHPMCOUNTER15] = { "mhpmcounter15", mctr, read_zero }, + [CSR_MHPMCOUNTER16] = { "mhpmcounter16", mctr, read_zero }, + [CSR_MHPMCOUNTER17] = { "mhpmcounter17", mctr, read_zero }, + [CSR_MHPMCOUNTER18] = { "mhpmcounter18", mctr, read_zero }, + [CSR_MHPMCOUNTER19] = { "mhpmcounter19", mctr, read_zero }, + [CSR_MHPMCOUNTER20] = { "mhpmcounter20", mctr, read_zero }, + [CSR_MHPMCOUNTER21] = { "mhpmcounter21", mctr, read_zero }, + [CSR_MHPMCOUNTER22] = { "mhpmcounter22", mctr, read_zero }, + [CSR_MHPMCOUNTER23] = { "mhpmcounter23", mctr, read_zero }, + [CSR_MHPMCOUNTER24] = { "mhpmcounter24", mctr, read_zero }, + [CSR_MHPMCOUNTER25] = { "mhpmcounter25", mctr, read_zero }, + [CSR_MHPMCOUNTER26] = { "mhpmcounter26", mctr, read_zero }, + [CSR_MHPMCOUNTER27] = { "mhpmcounter27", mctr, read_zero }, + [CSR_MHPMCOUNTER28] = { "mhpmcounter28", mctr, read_zero }, + [CSR_MHPMCOUNTER29] = { "mhpmcounter29", mctr, read_zero }, + [CSR_MHPMCOUNTER30] = { "mhpmcounter30", mctr, read_zero }, + [CSR_MHPMCOUNTER31] = { "mhpmcounter31", mctr, read_zero }, [CSR_MHPMEVENT3] = { "mhpmevent3", any, read_zero }, [CSR_MHPMEVENT4] = { "mhpmevent4", any, read_zero },
Re: [PATCH v10 04/12] target/riscv: pmu: Make number of counters configurable
Quoted 30 lines · Weiwei Li
Quoted 28 lines · Atish Patra
The RISC-V privilege specification provides flexibility to implement any number of counters from 29 programmable counters. However, the QEMU implements all the counters.
Make it configurable through pmu config parameter which now will indicate how many programmable counters should be implemented by the cpu.
Reviewed-by Bin Meng <[email protected]>Reviewed-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>---
target/riscv/cpu.c | 3 +- target/riscv/cpu.h | 2 +- target/riscv/csr.c | 94 ++++++++++++++++++++++++++++++---------------- 3 files changed, 63 insertions(+), 36 deletions(-)target/riscv/cpu.c +0
-0@@ -851,7 +851,6 @@ static void riscv_cpu_init(Object *obj) { RISCVCPU *cpu = RISCV_CPU(obj); - cpu->cfg.ext_pmu = true; cpu->cfg.ext_ifencei = true; cpu->cfg.ext_icsr = true; cpu->cfg.mmu = true;
Quoted 9 lines
target/riscv/cpu.c static Property riscv_cpu_extensions[] = { · line 881
Quoted 6 lines
target/riscv/cpu.c +1
-1@@ -879,7 +878,7 @@ static Property riscv_cpu_extensions[] = { DEFINE_PROP_BOOL("u", RISCVCPU, cfg.ext_u, true), DEFINE_PROP_BOOL("v", RISCVCPU, cfg.ext_v, false), DEFINE_PROP_BOOL("h", RISCVCPU, cfg.ext_h, true), - DEFINE_PROP_BOOL("pmu", RISCVCPU, cfg.ext_pmu, true), + DEFINE_PROP_UINT8("pmu-num", RISCVCPU, cfg.pmu_num, 16),I think It's better to add a check on cfg.pmu_num to <= 29.
OK, I find this check in the following patch.
Quoted 38 lines
Quoted 38 lines
target/riscv/cpu.c +0
-0DEFINE_PROP_BOOL("Zifencei", RISCVCPU, cfg.ext_ifencei, true), DEFINE_PROP_BOOL("Zicsr", RISCVCPU, cfg.ext_icsr, true), DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),target/riscv/cpu.h +0
-1@@ -397,7 +397,6 @@ struct RISCVCPUConfig { bool ext_zksed; bool ext_zksh; bool ext_zkt; - bool ext_pmu; bool ext_ifencei; bool ext_icsr; bool ext_svinval;@@ -421,6 +420,7 @@ struct RISCVCPUConfig { /* Vendor-specific custom extensions */ bool ext_XVentanaCondOps; + uint8_t pmu_num; char *priv_spec; char *user_spec; char *bext_spec;target/riscv/csr.c +5
-1@@ -73,9 +73,17 @@ static RISCVException ctr(CPURISCVState *env, int csrno) CPUState *cs = env_cpu(env); RISCVCPU *cpu = RISCV_CPU(cs); int ctr_index; + int base_csrno = CSR_HPMCOUNTER3; + bool rv32 = riscv_cpu_mxl(env) == MXL_RV32 ? true : false; - if (!cpu->cfg.ext_pmu) { - /* The PMU extension is not enabled */ + if (rv32 && csrno >= CSR_CYCLEH) { + /* Offset for RV32 hpmcounternh counters */ + base_csrno += 0x80;
Quoted 8 lines
Quoted 6 lines
target/riscv/csr.c +5
-0+ } + ctr_index = csrno - base_csrno; + + if (!cpu->cfg.pmu_num || ctr_index >= (cpu->cfg.pmu_num)) { + /* No counter is enabled in PMU or the counter is out ofrange */
I seems unnecessary to add '!cpu->cfg.pmu_num ' here, '
ctr_index>= (cpu->cfg.pmu_num)' is true
Typo. I -> It
Quoted 119 lines
when
cpu->cfg.pmu_numis zero if the problem forbase_csrnois fixed.Ragards,
Weiwei Li
Quoted 111 lines
return
RISCV_EXCP_ILLEGAL_INST;
}
@@ -103,7 +111,7 @@ static RISCVExceptionctr(CPURISCVState *env, int csrno)}
break;
}
- if (riscv_cpu_mxl(env)==MXL_RV32) {
+ if (rv32) {
switch (csrno) {
caseCSR_CYCLEH:
if (!get_field(env->mcounteren,COUNTEREN_CY)) {target/riscv/csr.c +1
-1@@ -158,7 +166,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno) } break; } - if (riscv_cpu_mxl(env) == MXL_RV32) { + if (rv32) { switch (csrno) {case
CSR_CYCLEH:
if (!get_field(env->hcounteren,COUNTEREN_CY) &&target/riscv/csr.c +49
-28@@ -202,6 +210,26 @@ static RISCVException ctr32(CPURISCVState *env, int csrno) } #if !defined(CONFIG_USER_ONLY) +static RISCVException mctr (CPURISCVState *env,int csrno) +{ + CPUState *cs = env_cpu(env); + RISCVCPU *cpu = RISCV_CPU(cs); + int ctr_index; + int base_csrno = CSR_MHPMCOUNTER3; + + if ((riscv_cpu_mxl(env) == MXL_RV32) && csrno >= CSR_MCYCLEH) { + /* Offset for RV32 mhpmcounternh counters */ + base_csrno += 0x80; + } + ctr_index = csrno - base_csrno; + if (!cpu->cfg.pmu_num || ctr_index >= cpu->cfg.pmu_num) { + /* The PMU is not enabled or counter is out of range*/ + return RISCV_EXCP_ILLEGAL_INST; + } + + return RISCV_EXCP_NONE; +} + static RISCVException any(CPURISCVState *env, int csrno) { return RISCV_EXCP_NONE;@@ -3687,35 +3715,35 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_HPMCOUNTER30] = { "hpmcounter30", ctr, read_zero }, [CSR_HPMCOUNTER31] = { "hpmcounter31", ctr, read_zero }, - [CSR_MHPMCOUNTER3] = { "mhpmcounter3", any, read_zero }, - [CSR_MHPMCOUNTER4] = { "mhpmcounter4", any, read_zero }, - [CSR_MHPMCOUNTER5] = { "mhpmcounter5", any, read_zero }, - [CSR_MHPMCOUNTER6] = { "mhpmcounter6", any, read_zero }, - [CSR_MHPMCOUNTER7] = { "mhpmcounter7", any, read_zero }, - [CSR_MHPMCOUNTER8] = { "mhpmcounter8", any, read_zero }, - [CSR_MHPMCOUNTER9] = { "mhpmcounter9", any, read_zero }, - [CSR_MHPMCOUNTER10] = { "mhpmcounter10", any, read_zero }, - [CSR_MHPMCOUNTER11] = { "mhpmcounter11", any, read_zero }, - [CSR_MHPMCOUNTER12] = { "mhpmcounter12", any, read_zero }, - [CSR_MHPMCOUNTER13] = { "mhpmcounter13", any, read_zero }, - [CSR_MHPMCOUNTER14] = { "mhpmcounter14", any, read_zero }, - [CSR_MHPMCOUNTER15] = { "mhpmcounter15", any, read_zero }, - [CSR_MHPMCOUNTER16] = { "mhpmcounter16", any, read_zero }, - [CSR_MHPMCOUNTER17] = { "mhpmcounter17", any, read_zero }, - [CSR_MHPMCOUNTER18] = { "mhpmcounter18", any, read_zero }, - [CSR_MHPMCOUNTER19] = { "mhpmcounter19", any, read_zero }, - [CSR_MHPMCOUNTER20] = { "mhpmcounter20", any, read_zero }, - [CSR_MHPMCOUNTER21] = { "mhpmcounter21", any, read_zero }, - [CSR_MHPMCOUNTER22] = { "mhpmcounter22", any, read_zero }, - [CSR_MHPMCOUNTER23] = { "mhpmcounter23", any, read_zero }, - [CSR_MHPMCOUNTER24] = { "mhpmcounter24", any, read_zero }, - [CSR_MHPMCOUNTER25] = { "mhpmcounter25", any, read_zero }, - [CSR_MHPMCOUNTER26] = { "mhpmcounter26", any, read_zero }, - [CSR_MHPMCOUNTER27] = { "mhpmcounter27", any, read_zero }, - [CSR_MHPMCOUNTER28] = { "mhpmcounter28", any, read_zero }, - [CSR_MHPMCOUNTER29] = { "mhpmcounter29", any, read_zero }, - [CSR_MHPMCOUNTER30] = { "mhpmcounter30", any, read_zero }, - [CSR_MHPMCOUNTER31] = { "mhpmcounter31", any, read_zero }, + [CSR_MHPMCOUNTER3] = { "mhpmcounter3", mctr, read_zero }, + [CSR_MHPMCOUNTER4] = { "mhpmcounter4", mctr, read_zero }, + [CSR_MHPMCOUNTER5] = { "mhpmcounter5", mctr, read_zero }, + [CSR_MHPMCOUNTER6] = { "mhpmcounter6", mctr, read_zero }, + [CSR_MHPMCOUNTER7] = { "mhpmcounter7", mctr, read_zero }, + [CSR_MHPMCOUNTER8] = { "mhpmcounter8", mctr, read_zero }, + [CSR_MHPMCOUNTER9] = { "mhpmcounter9", mctr, read_zero }, + [CSR_MHPMCOUNTER10] = { "mhpmcounter10", mctr, read_zero }, + [CSR_MHPMCOUNTER11] = { "mhpmcounter11", mctr, read_zero }, + [CSR_MHPMCOUNTER12] = { "mhpmcounter12", mctr, read_zero }, + [CSR_MHPMCOUNTER13] = { "mhpmcounter13", mctr, read_zero }, + [CSR_MHPMCOUNTER14] = { "mhpmcounter14", mctr, read_zero }, + [CSR_MHPMCOUNTER15] = { "mhpmcounter15", mctr, read_zero }, + [CSR_MHPMCOUNTER16] = { "mhpmcounter16", mctr, read_zero }, + [CSR_MHPMCOUNTER17] = { "mhpmcounter17", mctr, read_zero }, + [CSR_MHPMCOUNTER18] = { "mhpmcounter18", mctr, read_zero }, + [CSR_MHPMCOUNTER19] = { "mhpmcounter19", mctr, read_zero }, + [CSR_MHPMCOUNTER20] = { "mhpmcounter20", mctr, read_zero }, + [CSR_MHPMCOUNTER21] = { "mhpmcounter21", mctr, read_zero }, + [CSR_MHPMCOUNTER22] = { "mhpmcounter22", mctr, read_zero }, + [CSR_MHPMCOUNTER23] = { "mhpmcounter23", mctr, read_zero }, + [CSR_MHPMCOUNTER24] = { "mhpmcounter24", mctr, read_zero }, + [CSR_MHPMCOUNTER25] = { "mhpmcounter25", mctr, read_zero }, + [CSR_MHPMCOUNTER26] = { "mhpmcounter26", mctr, read_zero }, + [CSR_MHPMCOUNTER27] = { "mhpmcounter27", mctr, read_zero }, + [CSR_MHPMCOUNTER28] = { "mhpmcounter28", mctr, read_zero }, + [CSR_MHPMCOUNTER29] = { "mhpmcounter29", mctr, read_zero }, + [CSR_MHPMCOUNTER30] = { "mhpmcounter30", mctr, read_zero }, + [CSR_MHPMCOUNTER31] = { "mhpmcounter31", mctr, read_zero }, [CSR_MHPMEVENT3] = { "mhpmevent3", any, read_zero }, [CSR_MHPMEVENT4] = { "mhpmevent4", any, read_zero },
Re: [PATCH v10 04/12] target/riscv: pmu: Make number of counters configurable
Quoted 43 lines · Weiwei Li
Quoted 31 lines · Weiwei Li
Quoted 29 lines · Atish Patra
The RISC-V privilege specification provides flexibility to implement any number of counters from 29 programmable counters. However, the QEMU implements all the counters.
Make it configurable through pmu config parameter which now will indicate
how many programmable counters should be implemented by the cpu.Reviewed-by Bin Meng <[email protected]>Reviewed-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>--- target/riscv/cpu.c | 3 +- target/riscv/cpu.h | 2 +- target/riscv/csr.c | 94 ++++++++++++++++++++++++++++++---------------- 3 files changed, 63 insertions(+), 36 deletions(-)target/riscv/cpu.c +0
-0@@ -851,7 +851,6 @@ static void riscv_cpu_init(Object *obj) {RISCVCPU *cpu =RISCV_CPU (obj); - cpu->cfg.ext_pmu = true; cpu->cfg.ext_ifencei = true; cpu->cfg.ext_icsr = true; cpu->cfg.mmu = true;Quoted 9 lines
target/riscv/cpu.c static Property riscv_cpu_extensions[] = { · line 881
Quoted 6 lines
target/riscv/cpu.c +1
-1@@ -879,7 +878,7 @@ static Property riscv_cpu_extensions[] = {DEFINE_PROP_BOOL ("u" ,RISCVCPU , cfg.ext_u, true),DEFINE_PROP_BOOL ("v" ,RISCVCPU , cfg.ext_v, false),DEFINE_PROP_BOOL ("h" ,RISCVCPU , cfg.ext_h, true), - DEFINE_PROP_BOOL("pmu", RISCVCPU, cfg.ext_pmu, true), + DEFINE_PROP_UINT8("pmu-num", RISCVCPU, cfg.pmu_num, 16),I think It's better to add a check on cfg.pmu_num to <= 29.
Quoted 48 lines
OK, I find this check in the following patch.
Quoted 47 lines
Quoted 43 lines
target/riscv/cpu.c +0
-0DEFINE_PROP_BOOL ("Zifencei" ,RISCVCPU , cfg.ext_ifencei, true),DEFINE_PROP_BOOL ("Zicsr" ,RISCVCPU , cfg.ext_icsr, true), DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),target/riscv/cpu.h +0
-1@@ -397,7 +397,6 @@ struct RISCVCPUConfig {bool ext_zksed;bool ext_zksh;bool ext_zkt; -bool ext_pmu;bool ext_ifencei;bool ext_icsr;bool ext_svinval;@@ -421,6 +420,7 @@ struct RISCVCPUConfig {/* Vendor-specific custom extensions */ bool ext_XVentanaCondOps; + uint8_t pmu_num;char *priv_spec;char *user_spec;char *bext_spec;target/riscv/csr.c +10
-1@@ -73,9 +73,17 @@ static RISCVException ctr(CPURISCVState *env, int csrno) CPUState *cs = env_cpu(env);RISCVCPU *cpu =RISCV_CPU (cs);int ctr_index; +int base_csrno =CSR_HPMCOUNTER3 ; +bool rv32 =riscv_cpu_mxl (env) ==MXL_RV32 ? true : false; - if (!cpu->cfg.ext_pmu) { -/* The PMU extension is not enabled */ +if (rv32 && csrno >=CSR_CYCLEH ) { +/* Offset for RV32 hpmcounternh counters */ + base_csrno +=0x80 ; + } + ctr_index = csrno - base_csrno; + +if (!cpu->cfg.pmu_num || ctr_index >= (cpu->cfg.pmu_num)) { +/* No counter is enabled in PMU or the counter is out of Quoted 1 line
range */
I seems unnecessary to add '!cpu->cfg.pmu_num ' here, '
ctr_index>= (cpu->cfg.pmu_num)' is true
The check is improved in the following patches as well.
Quoted 122 lines
Typo. I -> It
Quoted 120 lines
when
cpu->cfg.pmu_numis zero if the problem forbase_csrnois fixed.Ragards,
Weiwei Li
Quoted 112 lines
return RISCV_EXCP_ILLEGAL_INST; } @@ -103,7 +111,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno) } break; }target/riscv/csr.c +2
-2-if (riscv_cpu_mxl (env) ==MXL_RV32 ) { +if (rv32) {switch (csrno) {case CSR_CYCLEH :if (!get_field (env->mcounteren,COUNTEREN_CY )) {@@ -158,7 +166,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; } -if (riscv_cpu_mxl (env) ==MXL_RV32 ) { +if (rv32) {switch (csrno) {case CSR_CYCLEH :if (!get_field(env->hcounteren, COUNTEREN_CY) &&target/riscv/csr.c +49
-28@@ -202,6 +210,26 @@ static RISCVException ctr32(CPURISCVState *env, int csrno) }#if !defined(CONFIG_USER_ONLY ) +static RISCVException mctr (CPURISCVState *env,int csrno) +{ +CPUState *cs =env_cpu (env); +RISCVCPU *cpu =RISCV_CPU (cs); +int ctr_index; +int base_csrno =CSR_MHPMCOUNTER3 ; + +if ((riscv_cpu_mxl (env) ==MXL_RV32 ) && csrno >=CSR_MCYCLEH ) { +/* Offset for RV32 mhpmcounternh counters */ + base_csrno +=0x80 ; + } + ctr_index = csrno - base_csrno; +if (!cpu->cfg.pmu_num || ctr_index >= cpu->cfg.pmu_num) { +/* The PMU is not enabled or counter is out of range*/ +return RISCV_EXCP_ILLEGAL_INST ; + } + +return RISCV_EXCP_NONE ; +} +static RISCVException any (CPURISCVState *env,int csrno) {return RISCV_EXCP_NONE ;@@ -3687,35 +3715,35 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_HPMCOUNTER30] = { "hpmcounter30", ctr, read_zero }, [CSR_HPMCOUNTER31] = { "hpmcounter31", ctr, read_zero }, - [CSR_MHPMCOUNTER3] = { "mhpmcounter3", any, read_zero }, - [CSR_MHPMCOUNTER4] = { "mhpmcounter4", any, read_zero }, - [CSR_MHPMCOUNTER5] = { "mhpmcounter5", any, read_zero }, - [CSR_MHPMCOUNTER6] = { "mhpmcounter6", any, read_zero }, - [CSR_MHPMCOUNTER7] = { "mhpmcounter7", any, read_zero }, - [CSR_MHPMCOUNTER8] = { "mhpmcounter8", any, read_zero }, - [CSR_MHPMCOUNTER9] = { "mhpmcounter9", any, read_zero }, - [CSR_MHPMCOUNTER10] = { "mhpmcounter10", any, read_zero }, - [CSR_MHPMCOUNTER11] = { "mhpmcounter11", any, read_zero }, - [CSR_MHPMCOUNTER12] = { "mhpmcounter12", any, read_zero }, - [CSR_MHPMCOUNTER13] = { "mhpmcounter13", any, read_zero }, - [CSR_MHPMCOUNTER14] = { "mhpmcounter14", any, read_zero }, - [CSR_MHPMCOUNTER15] = { "mhpmcounter15", any, read_zero }, - [CSR_MHPMCOUNTER16] = { "mhpmcounter16", any, read_zero }, - [CSR_MHPMCOUNTER17] = { "mhpmcounter17", any, read_zero }, - [CSR_MHPMCOUNTER18] = { "mhpmcounter18", any, read_zero }, - [CSR_MHPMCOUNTER19] = { "mhpmcounter19", any, read_zero }, - [CSR_MHPMCOUNTER20] = { "mhpmcounter20", any, read_zero }, - [CSR_MHPMCOUNTER21] = { "mhpmcounter21", any, read_zero }, - [CSR_MHPMCOUNTER22] = { "mhpmcounter22", any, read_zero }, - [CSR_MHPMCOUNTER23] = { "mhpmcounter23", any, read_zero }, - [CSR_MHPMCOUNTER24] = { "mhpmcounter24", any, read_zero }, - [CSR_MHPMCOUNTER25] = { "mhpmcounter25", any, read_zero }, - [CSR_MHPMCOUNTER26] = { "mhpmcounter26", any, read_zero }, - [CSR_MHPMCOUNTER27] = { "mhpmcounter27", any, read_zero }, - [CSR_MHPMCOUNTER28] = { "mhpmcounter28", any, read_zero }, - [CSR_MHPMCOUNTER29] = { "mhpmcounter29", any, read_zero }, - [CSR_MHPMCOUNTER30] = { "mhpmcounter30", any, read_zero }, - [CSR_MHPMCOUNTER31] = { "mhpmcounter31", any, read_zero }, + [CSR_MHPMCOUNTER3] = { "mhpmcounter3", mctr, read_zero }, + [CSR_MHPMCOUNTER4] = { "mhpmcounter4", mctr, read_zero }, + [CSR_MHPMCOUNTER5] = { "mhpmcounter5", mctr, read_zero }, + [CSR_MHPMCOUNTER6] = { "mhpmcounter6", mctr, read_zero }, + [CSR_MHPMCOUNTER7] = { "mhpmcounter7", mctr, read_zero }, + [CSR_MHPMCOUNTER8] = { "mhpmcounter8", mctr, read_zero }, + [CSR_MHPMCOUNTER9] = { "mhpmcounter9", mctr, read_zero }, + [CSR_MHPMCOUNTER10] = { "mhpmcounter10", mctr, read_zero }, + [CSR_MHPMCOUNTER11] = { "mhpmcounter11", mctr, read_zero }, + [CSR_MHPMCOUNTER12] = { "mhpmcounter12", mctr, read_zero }, + [CSR_MHPMCOUNTER13] = { "mhpmcounter13", mctr, read_zero }, + [CSR_MHPMCOUNTER14] = { "mhpmcounter14", mctr, read_zero }, + [CSR_MHPMCOUNTER15] = { "mhpmcounter15", mctr, read_zero }, + [CSR_MHPMCOUNTER16] = { "mhpmcounter16", mctr, read_zero }, + [CSR_MHPMCOUNTER17] = { "mhpmcounter17", mctr, read_zero }, + [CSR_MHPMCOUNTER18] = { "mhpmcounter18", mctr, read_zero }, + [CSR_MHPMCOUNTER19] = { "mhpmcounter19", mctr, read_zero }, + [CSR_MHPMCOUNTER20] = { "mhpmcounter20", mctr, read_zero }, + [CSR_MHPMCOUNTER21] = { "mhpmcounter21", mctr, read_zero }, + [CSR_MHPMCOUNTER22] = { "mhpmcounter22", mctr, read_zero }, + [CSR_MHPMCOUNTER23] = { "mhpmcounter23", mctr, read_zero }, + [CSR_MHPMCOUNTER24] = { "mhpmcounter24", mctr, read_zero }, + [CSR_MHPMCOUNTER25] = { "mhpmcounter25", mctr, read_zero }, + [CSR_MHPMCOUNTER26] = { "mhpmcounter26", mctr, read_zero }, + [CSR_MHPMCOUNTER27] = { "mhpmcounter27", mctr, read_zero }, + [CSR_MHPMCOUNTER28] = { "mhpmcounter28", mctr, read_zero }, + [CSR_MHPMCOUNTER29] = { "mhpmcounter29", mctr, read_zero }, + [CSR_MHPMCOUNTER30] = { "mhpmcounter30", mctr, read_zero }, + [CSR_MHPMCOUNTER31] = { "mhpmcounter31", mctr, read_zero }, [CSR_MHPMEVENT3] = { "mhpmevent3", any, read_zero }, [CSR_MHPMEVENT4] = { "mhpmevent4", any, read_zero },
Re: [PATCH v10 04/12] target/riscv: pmu: Make number of counters configurable
Quoted 88 lines · Atish Kumar Patra
Quoted 40 lines · Weiwei Li
Quoted 29 lines · Weiwei Li
Quoted 28 lines · Atish Patra
The RISC-V privilege specification provides flexibility to implement any number of counters from 29 programmable counters. However, the QEMU implements all the counters.
Make it configurable through pmu config parameter which now will indicate
how many programmable counters should be implemented by the cpu.Reviewed-by Bin Meng <[email protected]>Reviewed-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>--- target/riscv/cpu.c | 3 +- target/riscv/cpu.h | 2 +- target/riscv/csr.c | 94 ++++++++++++++++++++++++++++++---------------- 3 files changed, 63 insertions(+), 36 deletions(-)target/riscv/cpu.c +0
-0@@ -851,7 +851,6 @@ static void riscv_cpu_init(Object *obj) {RISCVCPU *cpu =RISCV_CPU (obj); - cpu->cfg.ext_pmu = true; cpu->cfg.ext_ifencei = true; cpu->cfg.ext_icsr = true;Quoted 9 lines
target/riscv/cpu.c static Property riscv_cpu_extensions[] = { · line 881
Quoted 7 lines
target/riscv/cpu.c +1
-1cpu->cfg.mmu = true;@@ -879,7 +878,7 @@ static Property riscv_cpu_extensions[] = {DEFINE_PROP_BOOL ("u" ,RISCVCPU , cfg.ext_u, true),DEFINE_PROP_BOOL ("v" ,RISCVCPU , cfg.ext_v, false),DEFINE_PROP_BOOL ("h" ,RISCVCPU , cfg.ext_h, true), - DEFINE_PROP_BOOL("pmu", RISCVCPU, cfg.ext_pmu, true), + DEFINE_PROP_UINT8("pmu-num", RISCVCPU, cfg.pmu_num, 16),I think It's better to add a check on cfg.pmu_num to <= 29.
Quoted 47 lines
OK, I find this check in the following patch.
Quoted 46 lines
Quoted 43 lines
target/riscv/cpu.c +0
-0DEFINE_PROP_BOOL ("Zifencei" ,RISCVCPU , cfg.ext_ifencei, true),DEFINE_PROP_BOOL ("Zicsr" ,RISCVCPU , cfg.ext_icsr, true), DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),target/riscv/cpu.h +0
-1@@ -397,7 +397,6 @@ struct RISCVCPUConfig {bool ext_zksed;bool ext_zksh;bool ext_zkt; -bool ext_pmu;bool ext_ifencei;bool ext_icsr;bool ext_svinval;@@ -421,6 +420,7 @@ struct RISCVCPUConfig {/* Vendor-specific custom extensions */ bool ext_XVentanaCondOps; + uint8_t pmu_num;char *priv_spec;char *user_spec;char *bext_spec;target/riscv/csr.c +10
-1@@ -73,9 +73,17 @@ static RISCVException ctr(CPURISCVState *env, int csrno) CPUState *cs = env_cpu(env);RISCVCPU *cpu =RISCV_CPU (cs);int ctr_index; +int base_csrno =CSR_HPMCOUNTER3 ; +bool rv32 =riscv_cpu_mxl (env) ==MXL_RV32 ? true : false; - if (!cpu->cfg.ext_pmu) { -/* The PMU extension is not enabled */ +if (rv32 && csrno >=CSR_CYCLEH ) { +/* Offset for RV32 hpmcounternh counters */ + base_csrno +=0x80 ; + } + ctr_index = csrno - base_csrno; + +if (!cpu->cfg.pmu_num || ctr_index >= (cpu->cfg.pmu_num)) { +/* No counter is enabled in PMU or the counter is out of Quoted 1 line
range */
I seems unnecessary to add '!cpu->cfg.pmu_num ' here, '
ctr_index>= (cpu->cfg.pmu_num)' is true
Quoted 2 lines
The check is improved in the following patches as well.
Do you mean 'if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs & ctr_mask)))' in patch 9 ?
In this condition, '!cpu->cfg.pmu_num' seems unnecessary too.
Regards,
Weiwei Li
Quoted 119 lines
Quoted 119 lines
Typo. I -> It
Quoted 118 lines
when
cpu->cfg.pmu_numis zero if the problem forbase_csrnois fixed.Ragards,
Weiwei Li
Quoted 112 lines
return RISCV_EXCP_ILLEGAL_INST; } @@ -103,7 +111,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno) } break; }target/riscv/csr.c +2
-2-if (riscv_cpu_mxl (env) ==MXL_RV32 ) { +if (rv32) {switch (csrno) {case CSR_CYCLEH :if (!get_field (env->mcounteren,COUNTEREN_CY )) {@@ -158,7 +166,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; } -if (riscv_cpu_mxl (env) ==MXL_RV32 ) { +if (rv32) {switch (csrno) {case CSR_CYCLEH :if (!get_field(env->hcounteren, COUNTEREN_CY) &&target/riscv/csr.c +49
-28@@ -202,6 +210,26 @@ static RISCVException ctr32(CPURISCVState *env, int csrno) }#if !defined(CONFIG_USER_ONLY ) +static RISCVException mctr (CPURISCVState *env,int csrno) +{ +CPUState *cs =env_cpu (env); +RISCVCPU *cpu =RISCV_CPU (cs); +int ctr_index; +int base_csrno =CSR_MHPMCOUNTER3 ; + +if ((riscv_cpu_mxl (env) ==MXL_RV32 ) && csrno >=CSR_MCYCLEH ) { +/* Offset for RV32 mhpmcounternh counters */ + base_csrno +=0x80 ; + } + ctr_index = csrno - base_csrno; +if (!cpu->cfg.pmu_num || ctr_index >= cpu->cfg.pmu_num) { +/* The PMU is not enabled or counter is out of range*/ +return RISCV_EXCP_ILLEGAL_INST ; + } + +return RISCV_EXCP_NONE ; +} +static RISCVException any (CPURISCVState *env,int csrno) {return RISCV_EXCP_NONE ;@@ -3687,35 +3715,35 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_HPMCOUNTER30] = { "hpmcounter30", ctr, read_zero }, [CSR_HPMCOUNTER31] = { "hpmcounter31", ctr, read_zero }, - [CSR_MHPMCOUNTER3] = { "mhpmcounter3", any, read_zero }, - [CSR_MHPMCOUNTER4] = { "mhpmcounter4", any, read_zero }, - [CSR_MHPMCOUNTER5] = { "mhpmcounter5", any, read_zero }, - [CSR_MHPMCOUNTER6] = { "mhpmcounter6", any, read_zero }, - [CSR_MHPMCOUNTER7] = { "mhpmcounter7", any, read_zero }, - [CSR_MHPMCOUNTER8] = { "mhpmcounter8", any, read_zero }, - [CSR_MHPMCOUNTER9] = { "mhpmcounter9", any, read_zero }, - [CSR_MHPMCOUNTER10] = { "mhpmcounter10", any, read_zero }, - [CSR_MHPMCOUNTER11] = { "mhpmcounter11", any, read_zero }, - [CSR_MHPMCOUNTER12] = { "mhpmcounter12", any, read_zero }, - [CSR_MHPMCOUNTER13] = { "mhpmcounter13", any, read_zero }, - [CSR_MHPMCOUNTER14] = { "mhpmcounter14", any, read_zero }, - [CSR_MHPMCOUNTER15] = { "mhpmcounter15", any, read_zero }, - [CSR_MHPMCOUNTER16] = { "mhpmcounter16", any, read_zero }, - [CSR_MHPMCOUNTER17] = { "mhpmcounter17", any, read_zero }, - [CSR_MHPMCOUNTER18] = { "mhpmcounter18", any, read_zero }, - [CSR_MHPMCOUNTER19] = { "mhpmcounter19", any, read_zero }, - [CSR_MHPMCOUNTER20] = { "mhpmcounter20", any, read_zero }, - [CSR_MHPMCOUNTER21] = { "mhpmcounter21", any, read_zero }, - [CSR_MHPMCOUNTER22] = { "mhpmcounter22", any, read_zero }, - [CSR_MHPMCOUNTER23] = { "mhpmcounter23", any, read_zero }, - [CSR_MHPMCOUNTER24] = { "mhpmcounter24", any, read_zero }, - [CSR_MHPMCOUNTER25] = { "mhpmcounter25", any, read_zero }, - [CSR_MHPMCOUNTER26] = { "mhpmcounter26", any, read_zero }, - [CSR_MHPMCOUNTER27] = { "mhpmcounter27", any, read_zero }, - [CSR_MHPMCOUNTER28] = { "mhpmcounter28", any, read_zero }, - [CSR_MHPMCOUNTER29] = { "mhpmcounter29", any, read_zero }, - [CSR_MHPMCOUNTER30] = { "mhpmcounter30", any, read_zero }, - [CSR_MHPMCOUNTER31] = { "mhpmcounter31", any, read_zero }, + [CSR_MHPMCOUNTER3] = { "mhpmcounter3", mctr, read_zero }, + [CSR_MHPMCOUNTER4] = { "mhpmcounter4", mctr, read_zero }, + [CSR_MHPMCOUNTER5] = { "mhpmcounter5", mctr, read_zero }, + [CSR_MHPMCOUNTER6] = { "mhpmcounter6", mctr, read_zero }, + [CSR_MHPMCOUNTER7] = { "mhpmcounter7", mctr, read_zero }, + [CSR_MHPMCOUNTER8] = { "mhpmcounter8", mctr, read_zero }, + [CSR_MHPMCOUNTER9] = { "mhpmcounter9", mctr, read_zero }, + [CSR_MHPMCOUNTER10] = { "mhpmcounter10", mctr, read_zero }, + [CSR_MHPMCOUNTER11] = { "mhpmcounter11", mctr, read_zero }, + [CSR_MHPMCOUNTER12] = { "mhpmcounter12", mctr, read_zero }, + [CSR_MHPMCOUNTER13] = { "mhpmcounter13", mctr, read_zero }, + [CSR_MHPMCOUNTER14] = { "mhpmcounter14", mctr, read_zero }, + [CSR_MHPMCOUNTER15] = { "mhpmcounter15", mctr, read_zero }, + [CSR_MHPMCOUNTER16] = { "mhpmcounter16", mctr, read_zero }, + [CSR_MHPMCOUNTER17] = { "mhpmcounter17", mctr, read_zero }, + [CSR_MHPMCOUNTER18] = { "mhpmcounter18", mctr, read_zero }, + [CSR_MHPMCOUNTER19] = { "mhpmcounter19", mctr, read_zero }, + [CSR_MHPMCOUNTER20] = { "mhpmcounter20", mctr, read_zero }, + [CSR_MHPMCOUNTER21] = { "mhpmcounter21", mctr, read_zero }, + [CSR_MHPMCOUNTER22] = { "mhpmcounter22", mctr, read_zero }, + [CSR_MHPMCOUNTER23] = { "mhpmcounter23", mctr, read_zero }, + [CSR_MHPMCOUNTER24] = { "mhpmcounter24", mctr, read_zero }, + [CSR_MHPMCOUNTER25] = { "mhpmcounter25", mctr, read_zero }, + [CSR_MHPMCOUNTER26] = { "mhpmcounter26", mctr, read_zero }, + [CSR_MHPMCOUNTER27] = { "mhpmcounter27", mctr, read_zero }, + [CSR_MHPMCOUNTER28] = { "mhpmcounter28", mctr, read_zero }, + [CSR_MHPMCOUNTER29] = { "mhpmcounter29", mctr, read_zero }, + [CSR_MHPMCOUNTER30] = { "mhpmcounter30", mctr, read_zero }, + [CSR_MHPMCOUNTER31] = { "mhpmcounter31", mctr, read_zero }, [CSR_MHPMEVENT3] = { "mhpmevent3", any, read_zero }, [CSR_MHPMEVENT4] = { "mhpmevent4", any, read_zero },
Re: [PATCH v10 04/12] target/riscv: pmu: Make number of counters configurable
Quoted 91 lines · Weiwei Li
Quoted 88 lines · Atish Kumar Patra
Quoted 87 lines · Weiwei Li
Quoted 29 lines · Weiwei Li
Quoted 28 lines · Atish Patra
The RISC-V privilege specification provides flexibility to implement any number of counters from 29 programmable counters. However, the QEMU implements all the counters.
Make it configurable through pmu config parameter which now will indicate
how many programmable counters should be implemented by the cpu.Reviewed-by Bin Meng <[email protected]>Reviewed-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>--- target/riscv/cpu.c | 3 +- target/riscv/cpu.h | 2 +- target/riscv/csr.c | 94 ++++++++++++++++++++++++++++++---------------- 3 files changed, 63 insertions(+), 36 deletions(-)target/riscv/cpu.c +0
-0@@ -851,7 +851,6 @@ static void riscv_cpu_init(Object *obj) {RISCVCPU *cpu =RISCV_CPU (obj); - cpu->cfg.ext_pmu = true; cpu->cfg.ext_ifencei = true; cpu->cfg.ext_icsr = true;Quoted 9 lines
target/riscv/cpu.c static Property riscv_cpu_extensions[] = { · line 881
Quoted 7 lines
target/riscv/cpu.c +1
-1cpu->cfg.mmu = true;@@ -879,7 +878,7 @@ static Property riscv_cpu_extensions[] = {DEFINE_PROP_BOOL ("u" ,RISCVCPU , cfg.ext_u, true),DEFINE_PROP_BOOL ("v" ,RISCVCPU , cfg.ext_v, false),DEFINE_PROP_BOOL ("h" ,RISCVCPU , cfg.ext_h, true), - DEFINE_PROP_BOOL("pmu", RISCVCPU, cfg.ext_pmu, true), + DEFINE_PROP_UINT8("pmu-num", RISCVCPU, cfg.pmu_num, 16),I think It's better to add a check on cfg.pmu_num to <= 29.
OK, I find this check in the following patch.
Quoted 46 lines
Quoted 43 lines
target/riscv/cpu.c +0
-0DEFINE_PROP_BOOL ("Zifencei" ,RISCVCPU , cfg.ext_ifencei, true),DEFINE_PROP_BOOL ("Zicsr" ,RISCVCPU , cfg.ext_icsr, true), DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),target/riscv/cpu.h +0
-1@@ -397,7 +397,6 @@ struct RISCVCPUConfig {bool ext_zksed;bool ext_zksh;bool ext_zkt; -bool ext_pmu;bool ext_ifencei;bool ext_icsr;bool ext_svinval;@@ -421,6 +420,7 @@ struct RISCVCPUConfig {/* Vendor-specific custom extensions */ bool ext_XVentanaCondOps; + uint8_t pmu_num;char *priv_spec;char *user_spec;char *bext_spec;target/riscv/csr.c +10
-1@@ -73,9 +73,17 @@ static RISCVException ctr(CPURISCVState *env, int csrno) CPUState *cs = env_cpu(env);RISCVCPU *cpu =RISCV_CPU (cs);int ctr_index; +int base_csrno =CSR_HPMCOUNTER3 ; +bool rv32 =riscv_cpu_mxl (env) ==MXL_RV32 ? true : false; - if (!cpu->cfg.ext_pmu) { -/* The PMU extension is not enabled */ +if (rv32 && csrno >=CSR_CYCLEH ) { +/* Offset for RV32 hpmcounternh counters */ + base_csrno +=0x80 ; + } + ctr_index = csrno - base_csrno; + +if (!cpu->cfg.pmu_num || ctr_index >= (cpu->cfg.pmu_num)) { +/* No counter is enabled in PMU or the counter is out of Quoted 1 line
range */
I seems unnecessary to add '!cpu->cfg.pmu_num ' here, '
ctr_index>= (cpu->cfg.pmu_num)' is true
Quoted 7 lines
Quoted 2 lines
The check is improved in the following patches as well.
Do you mean 'if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs &
ctr_mask)))' in patch 9 ?In this condition, '!cpu->cfg.pmu_num' seems unnecessary too.
Yes. I will remove it.
Quoted 125 lines
Regards,
Weiwei Li
Quoted 119 lines
Quoted 119 lines
Typo. I -> It
Quoted 118 lines
when
cpu->cfg.pmu_numis zero if the problem forbase_csrnois fixed.Ragards,
Weiwei Li
Quoted 112 lines
return RISCV_EXCP_ILLEGAL_INST; } @@ -103,7 +111,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno) } break; }target/riscv/csr.c +2
-2-if (riscv_cpu_mxl (env) ==MXL_RV32 ) { +if (rv32) {switch (csrno) {case CSR_CYCLEH :if (!get_field (env->mcounteren,COUNTEREN_CY )) {@@ -158,7 +166,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; } -if (riscv_cpu_mxl (env) ==MXL_RV32 ) { +if (rv32) {switch (csrno) {case CSR_CYCLEH :if (!get_field(env->hcounteren, COUNTEREN_CY) &&target/riscv/csr.c +49
-28@@ -202,6 +210,26 @@ static RISCVException ctr32(CPURISCVState *env, int csrno) }#if !defined(CONFIG_USER_ONLY ) +static RISCVException mctr (CPURISCVState *env,int csrno) +{ +CPUState *cs =env_cpu (env); +RISCVCPU *cpu =RISCV_CPU (cs); +int ctr_index; +int base_csrno =CSR_MHPMCOUNTER3 ; + +if ((riscv_cpu_mxl (env) ==MXL_RV32 ) && csrno >=CSR_MCYCLEH ) { +/* Offset for RV32 mhpmcounternh counters */ + base_csrno +=0x80 ; + } + ctr_index = csrno - base_csrno; +if (!cpu->cfg.pmu_num || ctr_index >= cpu->cfg.pmu_num) { +/* The PMU is not enabled or counter is out of range*/ +return RISCV_EXCP_ILLEGAL_INST ; + } + +return RISCV_EXCP_NONE ; +} +static RISCVException any (CPURISCVState *env,int csrno) {return RISCV_EXCP_NONE ;@@ -3687,35 +3715,35 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_HPMCOUNTER30] = { "hpmcounter30", ctr, read_zero }, [CSR_HPMCOUNTER31] = { "hpmcounter31", ctr, read_zero }, - [CSR_MHPMCOUNTER3] = { "mhpmcounter3", any, read_zero }, - [CSR_MHPMCOUNTER4] = { "mhpmcounter4", any, read_zero }, - [CSR_MHPMCOUNTER5] = { "mhpmcounter5", any, read_zero }, - [CSR_MHPMCOUNTER6] = { "mhpmcounter6", any, read_zero }, - [CSR_MHPMCOUNTER7] = { "mhpmcounter7", any, read_zero }, - [CSR_MHPMCOUNTER8] = { "mhpmcounter8", any, read_zero }, - [CSR_MHPMCOUNTER9] = { "mhpmcounter9", any, read_zero }, - [CSR_MHPMCOUNTER10] = { "mhpmcounter10", any, read_zero }, - [CSR_MHPMCOUNTER11] = { "mhpmcounter11", any, read_zero }, - [CSR_MHPMCOUNTER12] = { "mhpmcounter12", any, read_zero }, - [CSR_MHPMCOUNTER13] = { "mhpmcounter13", any, read_zero }, - [CSR_MHPMCOUNTER14] = { "mhpmcounter14", any, read_zero }, - [CSR_MHPMCOUNTER15] = { "mhpmcounter15", any, read_zero }, - [CSR_MHPMCOUNTER16] = { "mhpmcounter16", any, read_zero }, - [CSR_MHPMCOUNTER17] = { "mhpmcounter17", any, read_zero }, - [CSR_MHPMCOUNTER18] = { "mhpmcounter18", any, read_zero }, - [CSR_MHPMCOUNTER19] = { "mhpmcounter19", any, read_zero }, - [CSR_MHPMCOUNTER20] = { "mhpmcounter20", any, read_zero }, - [CSR_MHPMCOUNTER21] = { "mhpmcounter21", any, read_zero }, - [CSR_MHPMCOUNTER22] = { "mhpmcounter22", any, read_zero }, - [CSR_MHPMCOUNTER23] = { "mhpmcounter23", any, read_zero }, - [CSR_MHPMCOUNTER24] = { "mhpmcounter24", any, read_zero }, - [CSR_MHPMCOUNTER25] = { "mhpmcounter25", any, read_zero }, - [CSR_MHPMCOUNTER26] = { "mhpmcounter26", any, read_zero }, - [CSR_MHPMCOUNTER27] = { "mhpmcounter27", any, read_zero }, - [CSR_MHPMCOUNTER28] = { "mhpmcounter28", any, read_zero }, - [CSR_MHPMCOUNTER29] = { "mhpmcounter29", any, read_zero }, - [CSR_MHPMCOUNTER30] = { "mhpmcounter30", any, read_zero }, - [CSR_MHPMCOUNTER31] = { "mhpmcounter31", any, read_zero }, + [CSR_MHPMCOUNTER3] = { "mhpmcounter3", mctr, read_zero }, + [CSR_MHPMCOUNTER4] = { "mhpmcounter4", mctr, read_zero }, + [CSR_MHPMCOUNTER5] = { "mhpmcounter5", mctr, read_zero }, + [CSR_MHPMCOUNTER6] = { "mhpmcounter6", mctr, read_zero }, + [CSR_MHPMCOUNTER7] = { "mhpmcounter7", mctr, read_zero }, + [CSR_MHPMCOUNTER8] = { "mhpmcounter8", mctr, read_zero }, + [CSR_MHPMCOUNTER9] = { "mhpmcounter9", mctr, read_zero }, + [CSR_MHPMCOUNTER10] = { "mhpmcounter10", mctr, read_zero }, + [CSR_MHPMCOUNTER11] = { "mhpmcounter11", mctr, read_zero }, + [CSR_MHPMCOUNTER12] = { "mhpmcounter12", mctr, read_zero }, + [CSR_MHPMCOUNTER13] = { "mhpmcounter13", mctr, read_zero }, + [CSR_MHPMCOUNTER14] = { "mhpmcounter14", mctr, read_zero }, + [CSR_MHPMCOUNTER15] = { "mhpmcounter15", mctr, read_zero }, + [CSR_MHPMCOUNTER16] = { "mhpmcounter16", mctr, read_zero }, + [CSR_MHPMCOUNTER17] = { "mhpmcounter17", mctr, read_zero }, + [CSR_MHPMCOUNTER18] = { "mhpmcounter18", mctr, read_zero }, + [CSR_MHPMCOUNTER19] = { "mhpmcounter19", mctr, read_zero }, + [CSR_MHPMCOUNTER20] = { "mhpmcounter20", mctr, read_zero }, + [CSR_MHPMCOUNTER21] = { "mhpmcounter21", mctr, read_zero }, + [CSR_MHPMCOUNTER22] = { "mhpmcounter22", mctr, read_zero }, + [CSR_MHPMCOUNTER23] = { "mhpmcounter23", mctr, read_zero }, + [CSR_MHPMCOUNTER24] = { "mhpmcounter24", mctr, read_zero }, + [CSR_MHPMCOUNTER25] = { "mhpmcounter25", mctr, read_zero }, + [CSR_MHPMCOUNTER26] = { "mhpmcounter26", mctr, read_zero }, + [CSR_MHPMCOUNTER27] = { "mhpmcounter27", mctr, read_zero }, + [CSR_MHPMCOUNTER28] = { "mhpmcounter28", mctr, read_zero }, + [CSR_MHPMCOUNTER29] = { "mhpmcounter29", mctr, read_zero }, + [CSR_MHPMCOUNTER30] = { "mhpmcounter30", mctr, read_zero }, + [CSR_MHPMCOUNTER31] = { "mhpmcounter31", mctr, read_zero }, [CSR_MHPMEVENT3] = { "mhpmevent3", any, read_zero }, [CSR_MHPMEVENT4] = { "mhpmevent4", any, read_zero },
Signature · 2 lines
Regards, Atish
PATCH v10 5/12 target/riscv: Implement mcountinhibit CSR
In reply to Atish PatraFrom: Atish Patra <[email protected]>
As per the privilege specification v1.11, mcountinhibit allows to start/stop a pmu counter selectively.
--- target/riscv/cpu.h | 2 ++ target/riscv/cpu_bits.h | 4 ++++ target/riscv/csr.c | 25 +++++++++++++++++++++++++ target/riscv/machine.c | 1 + 4 files changed, 32 insertions(+)
target/riscv/cpu.h +2 -0
@@ -275,6 +275,8 @@ struct CPUArchState {target_ulong scounteren;target_ulong mcounteren; +target_ulong mcountinhibit; +target_ulong sscratch;target_ulong mscratch;
target/riscv/cpu_bits.h +4 -0
@@ -367,6 +367,10 @@#define CSR_MHPMCOUNTER29 0xb1d#define CSR_MHPMCOUNTER30 0xb1e#define CSR_MHPMCOUNTER31 0xb1f + +/* Machine counter-inhibit register */ +#define CSR_MCOUNTINHIBIT 0x320 +#define CSR_MHPMEVENT3 0x323#define CSR_MHPMEVENT4 0x324#define CSR_MHPMEVENT5 0x325
target/riscv/csr.c +25 -0
@@ -1475,6 +1475,28 @@ static RISCVException write_mtvec(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +static RISCVException read_mcountinhibit (CPURISCVState *env,int csrno, +target_ulong *val) +{ +if (env->priv_ver <PRIV_VERSION_1_11_0 ) { +return RISCV_EXCP_ILLEGAL_INST ; + } + + *val = env->mcountinhibit; +return RISCV_EXCP_NONE ; +} + +static RISCVException write_mcountinhibit (CPURISCVState *env,int csrno, +target_ulong val) +{ +if (env->priv_ver <PRIV_VERSION_1_11_0 ) { +return RISCV_EXCP_ILLEGAL_INST ; + } + + env->mcountinhibit = val; +return RISCV_EXCP_NONE ; +} +static RISCVException read_mcounteren (CPURISCVState *env,int csrno,target_ulong *val) {
@@ -3745,6 +3767,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MHPMCOUNTER30] = { "mhpmcounter30", mctr, read_zero }, [CSR_MHPMCOUNTER31] = { "mhpmcounter31", mctr, read_zero }, + [CSR_MCOUNTINHIBIT] = { "mcountinhibit", any, read_mcountinhibit, + write_mcountinhibit }, + [CSR_MHPMEVENT3] = { "mhpmevent3", any, read_zero }, [CSR_MHPMEVENT4] = { "mhpmevent4", any, read_zero }, [CSR_MHPMEVENT5] = { "mhpmevent5", any, read_zero },
target/riscv/machine.c +1 -0
@@ -330,6 +330,7 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_UINTTL (env.siselect,RISCVCPU ),VMSTATE_UINTTL (env.scounteren,RISCVCPU ),VMSTATE_UINTTL (env.mcounteren,RISCVCPU ), +VMSTATE_UINTTL (env.mcountinhibit,RISCVCPU ),VMSTATE_UINTTL (env.sscratch,RISCVCPU ),VMSTATE_UINTTL (env.mscratch,RISCVCPU ), VMSTATE_UINT64(env.mfromhost, RISCVCPU),
Signature · 1 line
2.25.1
Re: [PATCH v10 05/12] target/riscv: Implement mcountinhibit CSR
Quoted 49 lines · Atish Patra
From: Atish Patra <[email protected]>
As per the privilege specification v1.11, mcountinhibit allows to start/stop a pmu counter selectively.
Reviewed-by Bin Meng <[email protected]>Reviewed-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>--- target/riscv/cpu.h | 2 ++ target/riscv/cpu_bits.h | 4 ++++ target/riscv/csr.c | 25 +++++++++++++++++++++++++ target/riscv/machine.c | 1 + 4 files changed, 32 insertions(+)target/riscv/cpu.h +2
-0@@ -275,6 +275,8 @@ struct CPUArchState {target_ulong scounteren;target_ulong mcounteren; +target_ulong mcountinhibit; +target_ulong sscratch;target_ulong mscratch;target/riscv/cpu_bits.h +4
-0@@ -367,6 +367,10 @@#define CSR_MHPMCOUNTER29 0xb1d#define CSR_MHPMCOUNTER30 0xb1e#define CSR_MHPMCOUNTER31 0xb1f + +/* Machine counter-inhibit register */ +#define CSR_MCOUNTINHIBIT 0x320 +#define CSR_MHPMEVENT3 0x323#define CSR_MHPMEVENT4 0x324#define CSR_MHPMEVENT5 0x325target/riscv/csr.c +0
-0
target/riscv/csr.c static RISCVException write_mtvec(CPURISCVState *env, int csrno, · line 1484
Quoted 10 lines
target/riscv/csr.c +7
-0@@ -1475,6 +1475,28 @@ static RISCVException write_mtvec(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; } +static RISCVException read_mcountinhibit (CPURISCVState *env,int csrno, +target_ulong *val) +{ +if (env->priv_ver <PRIV_VERSION_1_11_0 ) { +return RISCV_EXCP_ILLEGAL_INST ; + } +
This seems can be done by add .min_priv_ver=PRIV_VERSION_1_11_0 in csr_ops table.
Regards,
Weiwei Li
Quoted 40 lines
target/riscv/csr.c +18
-0+ *val = env->mcountinhibit; +return RISCV_EXCP_NONE ; +} + +static RISCVException write_mcountinhibit (CPURISCVState *env,int csrno, +target_ulong val) +{ +if (env->priv_ver <PRIV_VERSION_1_11_0 ) { +return RISCV_EXCP_ILLEGAL_INST ; + } + + env->mcountinhibit = val; +return RISCV_EXCP_NONE ; +} +static RISCVException read_mcounteren (CPURISCVState *env,int csrno,target_ulong *val) {@@ -3745,6 +3767,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MHPMCOUNTER30] = { "mhpmcounter30", mctr, read_zero }, [CSR_MHPMCOUNTER31] = { "mhpmcounter31", mctr, read_zero }, + [CSR_MCOUNTINHIBIT] = { "mcountinhibit", any, read_mcountinhibit, + write_mcountinhibit }, + [CSR_MHPMEVENT3] = { "mhpmevent3", any, read_zero }, [CSR_MHPMEVENT4] = { "mhpmevent4", any, read_zero }, [CSR_MHPMEVENT5] = { "mhpmevent5", any, read_zero },target/riscv/machine.c +1
-0@@ -330,6 +330,7 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_UINTTL (env.siselect,RISCVCPU ),VMSTATE_UINTTL (env.scounteren,RISCVCPU ),VMSTATE_UINTTL (env.mcounteren,RISCVCPU ), +VMSTATE_UINTTL (env.mcountinhibit,RISCVCPU ),VMSTATE_UINTTL (env.sscratch,RISCVCPU ),VMSTATE_UINTTL (env.mscratch,RISCVCPU ), VMSTATE_UINT64(env.mfromhost, RISCVCPU),
Re: [PATCH v10 05/12] target/riscv: Implement mcountinhibit CSR
Quoted 55 lines · Weiwei Li
Quoted 52 lines · Atish Patra
From: Atish Patra <[email protected]>
As per the privilege specification v1.11, mcountinhibit allows to start/stop a pmu counter selectively.
Reviewed-by Bin Meng <[email protected]>Reviewed-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>--- target/riscv/cpu.h | 2 ++ target/riscv/cpu_bits.h | 4 ++++ target/riscv/csr.c | 25 +++++++++++++++++++++++++ target/riscv/machine.c | 1 + 4 files changed, 32 insertions(+)target/riscv/cpu.h +2
-0@@ -275,6 +275,8 @@ struct CPUArchState {target_ulong scounteren;target_ulong mcounteren; +target_ulong mcountinhibit; +target_ulong sscratch;target_ulong mscratch;target/riscv/cpu_bits.h +4
-0@@ -367,6 +367,10 @@#define CSR_MHPMCOUNTER29 0xb1d#define CSR_MHPMCOUNTER30 0xb1e#define CSR_MHPMCOUNTER31 0xb1f + +/* Machine counter-inhibit register */ +#define CSR_MCOUNTINHIBIT 0x320 +#define CSR_MHPMEVENT3 0x323#define CSR_MHPMEVENT4 0x324#define CSR_MHPMEVENT5 0x325target/riscv/csr.c +0
-0@@ -1475,6 +1475,28 @@ static RISCVException write_mtvec(CPURISCVState *env, int csrno,return RISCV_EXCP_NONE ; }
Quoted 11 lines
target/riscv/csr.c static RISCVException write_mtvec(CPURISCVState *env, int csrno, · line 1484
Quoted 7 lines
target/riscv/csr.c +7
-0+static RISCVException read_mcountinhibit (CPURISCVState *env,int csrno, +target_ulong *val) +{ +if (env->priv_ver <PRIV_VERSION_1_11_0 ) { +return RISCV_EXCP_ILLEGAL_INST ; + } +This seems can be done by add .min_priv_ver=PRIV_VERSION_1_11_0 in
csr_opstable.
Yes. This can be dropped from both read/write_mcountihibit with min_priv_ver. Thanks.
Quoted 45 lines
Regards,
Weiwei Li
Quoted 40 lines
target/riscv/csr.c +18
-0+ *val = env->mcountinhibit; +return RISCV_EXCP_NONE ; +} + +static RISCVException write_mcountinhibit (CPURISCVState *env,int csrno, +target_ulong val) +{ +if (env->priv_ver <PRIV_VERSION_1_11_0 ) { +return RISCV_EXCP_ILLEGAL_INST ; + } + + env->mcountinhibit = val; +return RISCV_EXCP_NONE ; +} +static RISCVException read_mcounteren (CPURISCVState *env,int csrno,target_ulong *val) {@@ -3745,6 +3767,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MHPMCOUNTER30] = { "mhpmcounter30", mctr, read_zero }, [CSR_MHPMCOUNTER31] = { "mhpmcounter31", mctr, read_zero }, + [CSR_MCOUNTINHIBIT] = { "mcountinhibit", any, read_mcountinhibit, + write_mcountinhibit }, + [CSR_MHPMEVENT3] = { "mhpmevent3", any, read_zero }, [CSR_MHPMEVENT4] = { "mhpmevent4", any, read_zero }, [CSR_MHPMEVENT5] = { "mhpmevent5", any, read_zero },target/riscv/machine.c +1
-0@@ -330,6 +330,7 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_UINTTL (env.siselect,RISCVCPU ),VMSTATE_UINTTL (env.scounteren,RISCVCPU ),VMSTATE_UINTTL (env.mcounteren,RISCVCPU ), +VMSTATE_UINTTL (env.mcountinhibit,RISCVCPU ),VMSTATE_UINTTL (env.sscratch,RISCVCPU ),VMSTATE_UINTTL (env.mscratch,RISCVCPU ), VMSTATE_UINT64(env.mfromhost, RISCVCPU),
PATCH v10 6/12 target/riscv: Add support for hpmcounters/hpmevents
In reply to Atish PatraFrom: Atish Patra <[email protected]>
With SBI PMU extension, user can use any of the available hpmcounters to track any perf events based on the value written to mhpmevent csr. Add read/write functionality for these csrs.
--- target/riscv/cpu.h | 11 + target/riscv/csr.c | 469 ++++++++++++++++++++++++++++------------- target/riscv/machine.c | 3 + 3 files changed, 331 insertions(+), 152 deletions(-)
target/riscv/cpu.h +11 -0
@@ -117,6 +117,8 @@ typedef struct CPUArchState CPURISCVState;#endif #define RV_VLEN_MAX 1024 +#define RV_MAX_MHPMEVENTS 29 +#define RV_MAX_MHPMCOUNTERS 32 FIELD(VTYPE, VLMUL, 0, 3)FIELD (VTYPE ,VSEW ,3 ,3 )
@@ -277,6 +279,15 @@ struct CPUArchState {target_ulong mcountinhibit; +/* PMU counter configured values */ +target_ulong mhpmcounter_val[RV_MAX_MHPMCOUNTERS ]; + +/* for RV32 */ +target_ulong mhpmcounterh_val[RV_MAX_MHPMCOUNTERS ]; + +/* PMU event selector configured values */ +target_ulong mhpmevent_val[RV_MAX_MHPMEVENTS ]; +target_ulong sscratch;target_ulong mscratch;
target/riscv/csr.c +317 -152
486 lines not on this page · raw message
target/riscv/machine.c +3 -0
@@ -331,6 +331,9 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_UINTTL (env.scounteren,RISCVCPU ),VMSTATE_UINTTL (env.mcounteren,RISCVCPU ),VMSTATE_UINTTL (env.mcountinhibit,RISCVCPU ), +VMSTATE_UINTTL_ARRAY (env.mhpmcounter_val,RISCVCPU ,RV_MAX_MHPMCOUNTERS ), +VMSTATE_UINTTL_ARRAY (env.mhpmcounterh_val,RISCVCPU ,RV_MAX_MHPMCOUNTERS ), +VMSTATE_UINTTL_ARRAY (env.mhpmevent_val,RISCVCPU ,RV_MAX_MHPMEVENTS ),VMSTATE_UINTTL (env.sscratch,RISCVCPU ),VMSTATE_UINTTL (env.mscratch,RISCVCPU ), VMSTATE_UINT64(env.mfromhost, RISCVCPU),
Signature · 1 line
2.25.1
PATCH v10 7/12 target/riscv: Support mcycle/minstret write operation
In reply to Atish PatraFrom: Atish Patra <[email protected]>
mcycle/minstret are actually WARL registers and can be written with any given value. With SBI PMU extension, it will be used to store a initial value provided from supervisor OS. The Qemu also need prohibit the counter increment if mcountinhibit is set.
Support mcycle/minstret through generic counter infrastructure.
--- target/riscv/cpu.h | 23 ++++-- target/riscv/csr.c | 155 ++++++++++++++++++++++++++++----------- target/riscv/machine.c | 25 ++++++- target/riscv/meson.build | 3 +- target/riscv/pmu.c | 32 ++++++++ target/riscv/pmu.h | 28 +++++++ 6 files changed, 213 insertions(+), 53 deletions(-) create mode 100644 target/riscv/pmu.c create mode 100644 target/riscv/pmu.h
target/riscv/cpu.h +16 -7
@@ -117,7 +117,7 @@ typedef struct CPUArchState CPURISCVState;#endif #define RV_VLEN_MAX 1024 -#define RV_MAX_MHPMEVENTS 29 +#define RV_MAX_MHPMEVENTS 32#define RV_MAX_MHPMCOUNTERS 32FIELD (VTYPE ,VLMUL ,0 ,3 )
@@ -127,6 +127,18 @@ FIELD(VTYPE, VMA, 7, 1) FIELD(VTYPE, VEDIV, 8, 2) FIELD(VTYPE, RESERVED, 10, sizeof(target_ulong) * 8 - 11) +typedef struct PMUCTRState { +/* Current value of a counter */ +target_ulong mhpmcounter_val; +/* Current value of a counter in RV32*/ +target_ulong mhpmcounterh_val; +/* Snapshot values of counter */ +target_ulong mhpmcounter_prev; +/* Snapshort value of a counter in RV32 */ +target_ulong mhpmcounterh_prev; +bool started; +}PMUCTRState ; +struct CPUArchState {target_ulong gpr[32 ];target_ulong gprh[32 ];/* 64 top bits of the 128-bit registers */
@@ -279,13 +291,10 @@ struct CPUArchState {target_ulong mcountinhibit; -/* PMU counter configured values */ -target_ulong mhpmcounter_val[RV_MAX_MHPMCOUNTERS ]; - -/* for RV32 */ -target_ulong mhpmcounterh_val[RV_MAX_MHPMCOUNTERS ]; +/* PMU counter state */ +PMUCTRState pmu_ctrs[RV_MAX_MHPMCOUNTERS ]; -/* PMU event selector configured values */ +/* PMU event selector configured values. First three are unused*/ target_ulong mhpmevent_val[RV_MAX_MHPMEVENTS ];target_ulong sscratch;
target/riscv/csr.c +112 -43
256 lines not on this page · raw message
target/riscv/machine.c +23 -2
@@ -279,7 +279,28 @@ static const VMStateDescription vmstate_envcfg = {VMSTATE_UINT64 (env.menvcfg,RISCVCPU ),VMSTATE_UINTTL (env.senvcfg,RISCVCPU ),VMSTATE_UINT64 (env.henvcfg,RISCVCPU ), + VMSTATE_END_OF_LIST() + } +}; + +static bool pmu_needed (void *opaque) +{ +RISCVCPU *cpu = opaque; +return cpu->cfg.pmu_num; +} + +static const VMStateDescription vmstate_pmu_ctr_state = { + .name ="cpu/pmu" , + .version_id =1 , + .minimum_version_id =1 , + .needed = pmu_needed, + .fields = (VMStateField []) { +VMSTATE_UINTTL (mhpmcounter_val, PMUCTRState), +VMSTATE_UINTTL (mhpmcounterh_val, PMUCTRState), +VMSTATE_UINTTL (mhpmcounter_prev, PMUCTRState), +VMSTATE_UINTTL (mhpmcounterh_prev, PMUCTRState), +VMSTATE_BOOL (started, PMUCTRState),VMSTATE_END_OF_LIST () } };
@@ -331,8 +352,8 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_UINTTL (env.scounteren,RISCVCPU ),VMSTATE_UINTTL (env.mcounteren,RISCVCPU ),VMSTATE_UINTTL (env.mcountinhibit,RISCVCPU ), -VMSTATE_UINTTL_ARRAY (env.mhpmcounter_val,RISCVCPU ,RV_MAX_MHPMCOUNTERS ), -VMSTATE_UINTTL_ARRAY (env.mhpmcounterh_val,RISCVCPU ,RV_MAX_MHPMCOUNTERS ), +VMSTATE_STRUCT_ARRAY (env.pmu_ctrs,RISCVCPU ,RV_MAX_MHPMCOUNTERS ,0 , + vmstate_pmu_ctr_state, PMUCTRState),VMSTATE_UINTTL_ARRAY (env.mhpmevent_val,RISCVCPU ,RV_MAX_MHPMEVENTS ),VMSTATE_UINTTL (env.sscratch,RISCVCPU ), VMSTATE_UINTTL(env.mscratch, RISCVCPU),
target/riscv/meson.build +2 -1
@@ -30,7 +30,8 @@ riscv_softmmu_ss.add(files( 'pmp.c', 'debug.c', 'monitor.c', - 'machine.c' + 'machine.c', + 'pmu.c' )) target_arch += {'riscv': riscv_ss}
target/riscv/pmu.c new file +32 -0
@@ -0,0 +1,32 @@ +/* +* RISC-V PMU file. +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "qemu/osdep.h" +#include "cpu.h" +#include "pmu.h" + +bool riscv_pmu_ctr_monitor_instructions (CPURISCVState *env, +uint32_t target_ctr) +{ +return (target_ctr ==0 ) ? true : false; +} + +bool riscv_pmu_ctr_monitor_cycles (CPURISCVState *env,uint32_t target_ctr) +{ +return (target_ctr ==2 ) ? true : false; +}
target/riscv/pmu.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* RISC-V PMU header file. +* +* Copyright (c) 2021 Western Digital Corporation or its affiliates. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "cpu.h" +#include "qemu/main-loop.h" +#include "exec/exec-all.h" + +bool riscv_pmu_ctr_monitor_instructions (CPURISCVState *env, +uint32_t target_ctr); +bool riscv_pmu_ctr_monitor_cycles (CPURISCVState *env, +uint32_t target_ctr);
Signature · 1 line
2.25.1
PATCH v10 8/12 target/riscv: Add sscofpmf extension support
In reply to Atish PatraThe Sscofpmf ('Ss' for Privileged arch and Supervisor-level extensions, and 'cofpmf' for Count OverFlow and Privilege Mode Filtering) extension allows the perf to handle overflow interrupts and filtering support. This patch provides a framework for programmable counters to leverage the extension. As the extension doesn't have any provision for the overflow bit for fixed counters, the fixed events can also be monitoring using programmable counters. The underlying counters for cycle and instruction counters are always running. Thus, a separate timer device is programmed to handle the overflow.
--- target/riscv/cpu.c | 11 ++ target/riscv/cpu.h | 25 +++ target/riscv/cpu_bits.h | 55 +++++++ target/riscv/csr.c | 165 ++++++++++++++++++- target/riscv/machine.c | 1 + target/riscv/pmu.c | 357 +++++++++++++++++++++++++++++++++++++++- target/riscv/pmu.h | 7 + 7 files changed, 610 insertions(+), 11 deletions(-)
target/riscv/cpu.c +11 -0
@@ -22,6 +22,7 @@#include "qemu/ctype.h" #include "qemu/log.h" #include "cpu.h" +#include "pmu.h" #include "internals.h" #include "exec/exec-all.h" #include "qapi/error.h"
@@ -775,6 +776,15 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)set_misa (env, env->misa_mxl, ext); } +#ifndef CONFIG_USER_ONLY +if (cpu->cfg.pmu_num) { +if (!riscv_pmu_init (cpu, cpu->cfg.pmu_num) && cpu->cfg.ext_sscofpmf) { + cpu->pmu_timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , + riscv_pmu_timer_cb, cpu); + } + } +#endif +riscv_cpu_register_gdb_regs_for_features (cs);qemu_init_vcpu (cs);
@@ -879,6 +889,7 @@ static Property riscv_cpu_extensions[] = {DEFINE_PROP_BOOL ("v" ,RISCVCPU , cfg.ext_v, false),DEFINE_PROP_BOOL ("h" ,RISCVCPU , cfg.ext_h, true),DEFINE_PROP_UINT8 ("pmu-num" ,RISCVCPU , cfg.pmu_num,16 ), +DEFINE_PROP_BOOL ("sscofpmf" ,RISCVCPU , cfg.ext_sscofpmf, false),DEFINE_PROP_BOOL ("Zifencei" ,RISCVCPU , cfg.ext_ifencei, true),DEFINE_PROP_BOOL ("Zicsr" ,RISCVCPU , cfg.ext_icsr, true), DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),
target/riscv/cpu.h +25 -0
@@ -137,6 +137,8 @@ typedef struct PMUCTRState {/* Snapshort value of a counter in RV32 */ target_ulong mhpmcounterh_prev;bool started; +/* Value beyond UINT32_MAX/UINT64_MAX before overflow interrupt trigger */ +target_ulong irq_overflow_left; } PMUCTRState;struct CPUArchState {
@@ -297,6 +299,9 @@ struct CPUArchState {/* PMU event selector configured values. First three are unused*/ target_ulong mhpmevent_val[RV_MAX_MHPMEVENTS ]; +/* PMU event selector configured values for RV32*/ +target_ulong mhpmeventh_val[RV_MAX_MHPMEVENTS ]; +target_ulong sscratch;target_ulong mscratch;
@@ -433,6 +438,7 @@ struct RISCVCPUConfig {bool ext_zve32f;bool ext_zve64f;bool ext_zmmul; +bool ext_sscofpmf;bool rvv_ta_all_1s;uint32_t mvendorid;
@@ -479,6 +485,12 @@ struct ArchCPU {/* Configuration Settings */ RISCVCPUConfig cfg; + +QEMUTimer *pmu_timer; +/* A bitmask of Available programmable counters */ +uint32_t pmu_avail_ctrs; +/* Mapping of events to counters */ +GHashTable *pmu_event_ctr_map; };static inline int riscv_has_ext (CPURISCVState *env,target_ulong ext)
@@ -738,6 +750,19 @@ enum { CSR_TABLE_SIZE = 0x1000 }; +/** +* The event id are encoded based on the encoding specified in the +* SBI specification v0.3 +*/ + +enum riscv_pmu_event_idx { +RISCV_PMU_EVENT_HW_CPU_CYCLES =0x01 , +RISCV_PMU_EVENT_HW_INSTRUCTIONS =0x02 , +RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS =0x10019 , +RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS =0x1001B , +RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS =0x10021 , +}; +/* CSR function table */ extern riscv_csr_operations csr_ops[CSR_TABLE_SIZE ];
target/riscv/cpu_bits.h +55 -0
88 lines not on this page · raw message
target/riscv/csr.c +156 -9
271 lines not on this page · raw message
target/riscv/machine.c +1 -0
@@ -355,6 +355,7 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_STRUCT_ARRAY (env.pmu_ctrs,RISCVCPU ,RV_MAX_MHPMCOUNTERS ,0 , vmstate_pmu_ctr_state, PMUCTRState),VMSTATE_UINTTL_ARRAY (env.mhpmevent_val,RISCVCPU ,RV_MAX_MHPMEVENTS ), +VMSTATE_UINTTL_ARRAY (env.mhpmeventh_val,RISCVCPU ,RV_MAX_MHPMEVENTS ),VMSTATE_UINTTL (env.sscratch,RISCVCPU ),VMSTATE_UINTTL (env.mscratch,RISCVCPU ), VMSTATE_UINT64(env.mfromhost, RISCVCPU),
target/riscv/pmu.c +355 -2
369 lines not on this page · raw message
target/riscv/pmu.h +7 -0
@@ -26,3 +26,10 @@ bool riscv_pmu_ctr_monitor_instructions(CPURISCVState *env, uint32_t target_ctr);bool riscv_pmu_ctr_monitor_cycles (CPURISCVState *env,uint32_t target_ctr); +void riscv_pmu_timer_cb (void *priv); +int riscv_pmu_init (RISCVCPU *cpu,int num_counters); +int riscv_pmu_update_event_map (CPURISCVState *env,uint64_t value, +uint32_t ctr_idx); +int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx); +int riscv_pmu_setup_timer (CPURISCVState *env,uint64_t value, +uint32_t ctr_idx);
Signature · 1 line
2.25.1
Re: [PATCH v10 08/12] target/riscv: Add sscofpmf extension support
Quoted 623 lines · Atish Patra
The Sscofpmf ('Ss' for Privileged arch and Supervisor-level extensions, and 'cofpmf' for Count OverFlow and Privilege Mode Filtering) extension allows the perf to handle overflow interrupts and filtering support. This patch provides a framework for programmable counters to leverage the extension. As the extension doesn't have any provision for the overflow bit for fixed counters, the fixed events can also be monitoring using programmable counters. The underlying counters for cycle and instruction counters are always running. Thus, a separate timer device is programmed to handle the overflow.
Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>--- target/riscv/cpu.c | 11 ++ target/riscv/cpu.h | 25 +++ target/riscv/cpu_bits.h | 55 +++++++ target/riscv/csr.c | 165 ++++++++++++++++++- target/riscv/machine.c | 1 + target/riscv/pmu.c | 357 +++++++++++++++++++++++++++++++++++++++- target/riscv/pmu.h | 7 + 7 files changed, 610 insertions(+), 11 deletions(-)target/riscv/cpu.c +11
-0@@ -22,6 +22,7 @@#include "qemu/ctype.h" #include "qemu/log.h" #include "cpu.h" +#include "pmu.h" #include "internals.h" #include "exec/exec-all.h" #include "qapi/error.h" @@ -775,6 +776,15 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)set_misa (env, env->misa_mxl, ext); } +#ifndef CONFIG_USER_ONLY +if (cpu->cfg.pmu_num) { +if (!riscv_pmu_init (cpu, cpu->cfg.pmu_num) && cpu->cfg.ext_sscofpmf) { + cpu->pmu_timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , + riscv_pmu_timer_cb, cpu); + } + } +#endif +riscv_cpu_register_gdb_regs_for_features (cs);qemu_init_vcpu (cs);@@ -879,6 +889,7 @@ static Property riscv_cpu_extensions[] = {DEFINE_PROP_BOOL ("v" ,RISCVCPU , cfg.ext_v, false),DEFINE_PROP_BOOL ("h" ,RISCVCPU , cfg.ext_h, true),DEFINE_PROP_UINT8 ("pmu-num" ,RISCVCPU , cfg.pmu_num,16 ), +DEFINE_PROP_BOOL ("sscofpmf" ,RISCVCPU , cfg.ext_sscofpmf, false),DEFINE_PROP_BOOL ("Zifencei" ,RISCVCPU , cfg.ext_ifencei, true),DEFINE_PROP_BOOL ("Zicsr" ,RISCVCPU , cfg.ext_icsr, true), DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),target/riscv/cpu.h +25
-0@@ -137,6 +137,8 @@ typedef struct PMUCTRState {/* Snapshort value of a counter in RV32 */ target_ulong mhpmcounterh_prev;bool started; +/* Value beyond UINT32_MAX/UINT64_MAX before overflow interrupt trigger */ +target_ulong irq_overflow_left; } PMUCTRState;struct CPUArchState {@@ -297,6 +299,9 @@ struct CPUArchState {/* PMU event selector configured values. First three are unused*/ target_ulong mhpmevent_val[RV_MAX_MHPMEVENTS ]; +/* PMU event selector configured values for RV32*/ +target_ulong mhpmeventh_val[RV_MAX_MHPMEVENTS ]; +target_ulong sscratch;target_ulong mscratch;@@ -433,6 +438,7 @@ struct RISCVCPUConfig {bool ext_zve32f;bool ext_zve64f;bool ext_zmmul; +bool ext_sscofpmf;bool rvv_ta_all_1s;uint32_t mvendorid;@@ -479,6 +485,12 @@ struct ArchCPU {/* Configuration Settings */ RISCVCPUConfig cfg; + +QEMUTimer *pmu_timer; +/* A bitmask of Available programmable counters */ +uint32_t pmu_avail_ctrs; +/* Mapping of events to counters */ +GHashTable *pmu_event_ctr_map; };static inline int riscv_has_ext (CPURISCVState *env,target_ulong ext)@@ -738,6 +750,19 @@ enum { CSR_TABLE_SIZE = 0x1000 }; +/** +* The event id are encoded based on the encoding specified in the +* SBI specification v0.3 +*/ + +enum riscv_pmu_event_idx { +RISCV_PMU_EVENT_HW_CPU_CYCLES =0x01 , +RISCV_PMU_EVENT_HW_INSTRUCTIONS =0x02 , +RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS =0x10019 , +RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS =0x1001B , +RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS =0x10021 , +}; +/* CSR function table */ extern riscv_csr_operations csr_ops[CSR_TABLE_SIZE ];target/riscv/cpu_bits.h +55
-0@@ -400,6 +400,37 @@#define CSR_MHPMEVENT29 0x33d#define CSR_MHPMEVENT30 0x33e#define CSR_MHPMEVENT31 0x33f + +#define CSR_MHPMEVENT3H 0x723 +#define CSR_MHPMEVENT4H 0x724 +#define CSR_MHPMEVENT5H 0x725 +#define CSR_MHPMEVENT6H 0x726 +#define CSR_MHPMEVENT7H 0x727 +#define CSR_MHPMEVENT8H 0x728 +#define CSR_MHPMEVENT9H 0x729 +#define CSR_MHPMEVENT10H 0x72a +#define CSR_MHPMEVENT11H 0x72b +#define CSR_MHPMEVENT12H 0x72c +#define CSR_MHPMEVENT13H 0x72d +#define CSR_MHPMEVENT14H 0x72e +#define CSR_MHPMEVENT15H 0x72f +#define CSR_MHPMEVENT16H 0x730 +#define CSR_MHPMEVENT17H 0x731 +#define CSR_MHPMEVENT18H 0x732 +#define CSR_MHPMEVENT19H 0x733 +#define CSR_MHPMEVENT20H 0x734 +#define CSR_MHPMEVENT21H 0x735 +#define CSR_MHPMEVENT22H 0x736 +#define CSR_MHPMEVENT23H 0x737 +#define CSR_MHPMEVENT24H 0x738 +#define CSR_MHPMEVENT25H 0x739 +#define CSR_MHPMEVENT26H 0x73a +#define CSR_MHPMEVENT27H 0x73b +#define CSR_MHPMEVENT28H 0x73c +#define CSR_MHPMEVENT29H 0x73d +#define CSR_MHPMEVENT30H 0x73e +#define CSR_MHPMEVENT31H 0x73f +#define CSR_MHPMCOUNTER3H 0xb83#define CSR_MHPMCOUNTER4H 0xb84#define CSR_MHPMCOUNTER5H 0xb85@@ -461,6 +492,7 @@#define CSR_VSMTE 0x2c0#define CSR_VSPMMASK 0x2c1#define CSR_VSPMBASE 0x2c2 +#define CSR_SCOUNTOVF 0xda0/* Crypto Extension */ #define CSR_SEED 0x015@@ -638,6 +670,7 @@ typedef enum RISCVException {#define IRQ_VS_EXT 10#define IRQ_M_EXT 11#define IRQ_S_GEXT 12 +#define IRQ_PMU_OVF 13#define IRQ_LOCAL_MAX 16#define IRQ_LOCAL_GUEST_MAX (TARGET_LONG_BITS - 1)@@ -655,11 +688,13 @@ typedef enum RISCVException {#define MIP_VSEIP (1 << IRQ_VS_EXT)#define MIP_MEIP (1 << IRQ_M_EXT)#define MIP_SGEIP (1 << IRQ_S_GEXT) +#define MIP_LCOFIP (1 << IRQ_PMU_OVF)/* sip masks */ #define SIP_SSIP MIP_SSIP#define SIP_STIP MIP_STIP#define SIP_SEIP MIP_SEIP +#define SIP_LCOFIP MIP_LCOFIP/* MIE masks */ #define MIE_SEIE (1 << IRQ_S_EXT)@@ -813,4 +848,24 @@ typedef enum RISCVException {#define SEED_OPST_WAIT (0b01 << 30)#define SEED_OPST_ES16 (0b10 << 30)#define SEED_OPST_DEAD (0b11 << 30) +/* PMU related bits */ +#define MIE_LCOFIE (1 << IRQ_PMU_OVF) + +#define MHPMEVENT_BIT_OF BIT_ULL(63) +#define MHPMEVENTH_BIT_OF BIT(31) +#define MHPMEVENT_BIT_MINH BIT_ULL(62) +#define MHPMEVENTH_BIT_MINH BIT(30) +#define MHPMEVENT_BIT_SINH BIT_ULL(61) +#define MHPMEVENTH_BIT_SINH BIT(29) +#define MHPMEVENT_BIT_UINH BIT_ULL(60) +#define MHPMEVENTH_BIT_UINH BIT(28) +#define MHPMEVENT_BIT_VSINH BIT_ULL(59) +#define MHPMEVENTH_BIT_VSINH BIT(27) +#define MHPMEVENT_BIT_VUINH BIT_ULL(58) +#define MHPMEVENTH_BIT_VUINH BIT(26) + +#define MHPMEVENT_SSCOF_MASK _ULL(0xFFFF000000000000) +#define MHPMEVENT_IDX_MASK 0xFFFFF +#define MHPMEVENT_SSCOF_RESVD 16 +#endif target/riscv/csr.c +156
-9@@ -74,7 +74,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno)CPUState *cs =env_cpu (env);RISCVCPU *cpu =RISCV_CPU (cs);int ctr_index; -int base_csrno =CSR_HPMCOUNTER3 ; +int base_csrno =CSR_CYCLE ;bool rv32 =riscv_cpu_mxl (env) ==MXL_RV32 ? true : false;if (rv32 && csrno >=CSR_CYCLEH ) {@@ -83,11 +83,18 @@ static RISCVException ctr(CPURISCVState *env, int csrno) } ctr_index = csrno - base_csrno; -if (!cpu->cfg.pmu_num || ctr_index >= (cpu->cfg.pmu_num)) { +if ((csrno >=CSR_CYCLE && csrno <=CSR_INSTRET ) || + (csrno >=CSR_CYCLEH && csrno <=CSR_INSTRETH )) { + goto skip_ext_pmu_check; + } + +if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs &BIT (ctr_index)))) {/* No counter is enabled in PMU or the counter is out of range */ return RISCV_EXCP_ILLEGAL_INST ; } +skip_ext_pmu_check: +if (env->priv ==PRV_S ) {switch (csrno) {case CSR_CYCLE :@@ -106,7 +113,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: - ctr_index = csrno -CSR_CYCLE ;if (!get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_ILLEGAL_INST ; }@@ -130,7 +136,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: - ctr_index = csrno -CSR_CYCLEH ;if (!get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_ILLEGAL_INST ; }@@ -160,7 +165,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: - ctr_index = csrno -CSR_CYCLE ;if (!get_field (env->hcounteren,1 << ctr_index) &&get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ;@@ -188,7 +192,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: - ctr_index = csrno -CSR_CYCLEH ;if (!get_field (env->hcounteren,1 << ctr_index) &&get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ;@@ -240,6 +243,18 @@ static RISCVException mctr32(CPURISCVState *env, int csrno)return mctr (env, csrno); } +static RISCVException sscofpmf (CPURISCVState *env,int csrno) +{ +CPUState *cs =env_cpu (env); +RISCVCPU *cpu =RISCV_CPU (cs); + +if (!cpu->cfg.ext_sscofpmf) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return RISCV_EXCP_NONE ; +} +static RISCVException any (CPURISCVState *env,int csrno) {return RISCV_EXCP_NONE ;@@ -663,9 +678,38 @@ static int read_mhpmevent(CPURISCVState *env, int csrno, target_ulong *val)static int write_mhpmevent (CPURISCVState *env,int csrno,target_ulong val) {int evt_index = csrno -CSR_MCOUNTINHIBIT ; +uint64_t mhpmevt_val = val; env->mhpmevent_val[evt_index] = val; +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpmevt_val = mhpmevt_val | ((uint64_t )env->mhpmeventh_val[evt_index] <<32 ); + } +riscv_pmu_update_event_map (env, mhpmevt_val, evt_index); + +return RISCV_EXCP_NONE ; +} + +static int read_mhpmeventh (CPURISCVState *env,int csrno,target_ulong *val) +{ +int evt_index = csrno -CSR_MHPMEVENT3H +3 ; + + *val = env->mhpmeventh_val[evt_index]; + +return RISCV_EXCP_NONE ; +} + +static int write_mhpmeventh (CPURISCVState *env,int csrno,target_ulong val) +{ +int evt_index = csrno -CSR_MHPMEVENT3H +3 ; +uint64_t mhpmevth_val = val; +uint64_t mhpmevt_val = env->mhpmevent_val[evt_index]; + + mhpmevt_val = mhpmevt_val | (mhpmevth_val <<32 ); + env->mhpmeventh_val[evt_index] = val; + +riscv_pmu_update_event_map (env, mhpmevt_val, evt_index); +return RISCV_EXCP_NONE ; }@@ -673,12 +717,20 @@ static int write_mhpmcounter(CPURISCVState *env, int csrno, target_ulong val) {int ctr_idx = csrno -CSR_MCYCLE ;PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +uint64_t mhpmctr_val = val; counter->mhpmcounter_val = val;if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) ||riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { counter->mhpmcounter_prev =get_ticks (false); - }else { +if (ctr_idx >2 ) { +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpmctr_val = mhpmctr_val | + ((uint64_t )counter->mhpmcounterh_val <<32 ); + } +riscv_pmu_setup_timer (env, mhpmctr_val, ctr_idx); + } + }else {/* Other counters can keep incrementing from the given value */ counter->mhpmcounter_prev = val; }@@ -690,11 +742,17 @@ static int write_mhpmcounterh(CPURISCVState *env, int csrno, target_ulong val) {int ctr_idx = csrno -CSR_MCYCLEH ;PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +uint64_t mhpmctr_val = counter->mhpmcounter_val; +uint64_t mhpmctrh_val = val; counter->mhpmcounterh_val = val; + mhpmctr_val = mhpmctr_val | (mhpmctrh_val <<32 );if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) ||riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { counter->mhpmcounterh_prev =get_ticks (true); +if (ctr_idx >2 ) { +riscv_pmu_setup_timer (env, mhpmctr_val, ctr_idx); + } }else { counter->mhpmcounterh_prev = val; }@@ -770,6 +828,32 @@ static int read_hpmcounterh(CPURISCVState *env, int csrno, target_ulong *val)return riscv_pmu_read_ctr (env, val, true, ctr_index); } +static int read_scountovf (CPURISCVState *env,int csrno,target_ulong *val) +{ +int mhpmevt_start =CSR_MHPMEVENT3 -CSR_MCOUNTINHIBIT ; +int i; + *val =0 ; +target_ulong *mhpm_evt_val; +uint64_t of_bit_mask; + +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpm_evt_val = env->mhpmeventh_val; + of_bit_mask =MHPMEVENTH_BIT_OF ; + }else { + mhpm_evt_val = env->mhpmevent_val; + of_bit_mask =MHPMEVENT_BIT_OF ; + } + +for (i = mhpmevt_start; i <RV_MAX_MHPMEVENTS ; i++) { +if ((get_field (env->mcounteren,BIT (i))) && + (mhpm_evt_val[i] & of_bit_mask)) { + *val |=BIT (i); + } + } + +return RISCV_EXCP_NONE ; +} +static RISCVException read_time (CPURISCVState *env,int csrno,target_ulong *val) {@@ -799,7 +883,8 @@ static RISCVException read_timeh(CPURISCVState *env, int csrno,/* Machine constants */ #define M_MODE_INTERRUPTS ((uint64_t)(MIP_MSIP | MIP_MTIP | MIP_MEIP)) -#define S_MODE_INTERRUPTS ((uint64_t)(MIP_SSIP | MIP_STIP | MIP_SEIP)) +#define S_MODE_INTERRUPTS ((uint64_t)(MIP_SSIP | MIP_STIP | MIP_SEIP | \ + MIP_LCOFIP))#define VS_MODE_INTERRUPTS ((uint64_t)(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP))#define HS_MODE_INTERRUPTS ((uint64_t)(MIP_SGEIP | VS_MODE_INTERRUPTS))@@ -840,7 +925,8 @@ static const target_ulong vs_delegable_excps = DELEGABLE_EXCPS &static const target_ulong sstatus_v1_10_mask =SSTATUS_SIE |SSTATUS_SPIE |SSTATUS_UIE |SSTATUS_UPIE |SSTATUS_SPP |SSTATUS_FS |SSTATUS_XS |SSTATUS_SUM |SSTATUS_MXR |SSTATUS_VS ; -static const target_ulong sip_writable_mask =SIP_SSIP |MIP_USIP |MIP_UEIP ; +static const target_ulong sip_writable_mask =SIP_SSIP |MIP_USIP |MIP_UEIP | +SIP_LCOFIP ;static const target_ulong hip_writable_mask =MIP_VSSIP ;static const target_ulong hvip_writable_mask =MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ;static const target_ulong vsip_writable_mask =MIP_VSSIP ;@@ -4005,6 +4091,65 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MHPMEVENT31] = { "mhpmevent31", any, read_mhpmevent, write_mhpmevent }, + [CSR_MHPMEVENT3H] = { "mhpmevent3h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT4H] = { "mhpmevent4h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT5H] = { "mhpmevent5h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT6H] = { "mhpmevent6h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT7H] = { "mhpmevent7h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT8H] = { "mhpmevent8h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT9H] = { "mhpmevent9h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT10H] = { "mhpmevent10h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT11H] = { "mhpmevent11h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT12H] = { "mhpmevent12h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT13H] = { "mhpmevent13h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT14H] = { "mhpmevent14h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT15H] = { "mhpmevent15h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT16H] = { "mhpmevent16h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT17H] = { "mhpmevent17h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT18H] = { "mhpmevent18h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT19H] = { "mhpmevent19h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT20H] = { "mhpmevent20h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT21H] = { "mhpmevent21h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT22H] = { "mhpmevent22h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT23H] = { "mhpmevent23h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT24H] = { "mhpmevent24h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT25H] = { "mhpmevent25h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT26H] = { "mhpmevent26h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT27H] = { "mhpmevent27h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT28H] = { "mhpmevent28h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT29H] = { "mhpmevent29h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT30H] = { "mhpmevent30h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT31H] = { "mhpmevent31h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_HPMCOUNTER3H] = { "hpmcounter3h", ctr32, read_hpmcounterh }, [CSR_HPMCOUNTER4H] = { "hpmcounter4h", ctr32, read_hpmcounterh }, [CSR_HPMCOUNTER5H] = { "hpmcounter5h", ctr32, read_hpmcounterh },@@ -4093,5 +4238,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { write_mhpmcounterh }, [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", mctr32, read_hpmcounterh, write_mhpmcounterh }, + [CSR_SCOUNTOVF] = { "scountovf", sscofpmf, read_scountovf }, + #endif /* !CONFIG_USER_ONLY */ };target/riscv/machine.c +1
-0@@ -355,6 +355,7 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_STRUCT_ARRAY (env.pmu_ctrs,RISCVCPU ,RV_MAX_MHPMCOUNTERS ,0 , vmstate_pmu_ctr_state, PMUCTRState),VMSTATE_UINTTL_ARRAY (env.mhpmevent_val,RISCVCPU ,RV_MAX_MHPMEVENTS ), +VMSTATE_UINTTL_ARRAY (env.mhpmeventh_val,RISCVCPU ,RV_MAX_MHPMEVENTS ),VMSTATE_UINTTL (env.sscratch,RISCVCPU ),VMSTATE_UINTTL (env.mscratch,RISCVCPU ), VMSTATE_UINT64(env.mfromhost, RISCVCPU),target/riscv/pmu.c +94
-0@@ -19,14 +19,367 @@#include "qemu/osdep.h" #include "cpu.h" #include "pmu.h" +#include "sysemu/cpu-timers.h" + +#define RISCV_TIMEBASE_FREQ 1000000000/* 1Ghz */ +#define MAKE_32BIT_MASK (shift, length) \ + (((uint32_t)(~0UL) >> (32 - (length))) << (shift)) + +static bool riscv_pmu_counter_valid (RISCVCPU *cpu,uint32_t ctr_idx) +{ +if (ctr_idx <3 || ctr_idx >=RV_MAX_MHPMCOUNTERS || + !(cpu->pmu_avail_ctrs &BIT (ctr_idx))) { +return false; + }else { +return true; + } +} + +static bool riscv_pmu_counter_enabled (RISCVCPU *cpu,uint32_t ctr_idx) +{ +CPURISCVState *env = &cpu->env; + +if (riscv_pmu_counter_valid (cpu, ctr_idx) && + !get_field (env->mcountinhibit,BIT (ctr_idx))) { +return true; + }else { +return false; + } +} + +static int riscv_pmu_incr_ctr_rv32 (RISCVCPU *cpu,uint32_t ctr_idx) +{ +CPURISCVState *env = &cpu->env; +target_ulong max_val =UINT32_MAX ; +PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +bool virt_on =riscv_cpu_virt_enabled (env); + +/* Privilege mode filtering */ +if ((env->priv ==PRV_M && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_MINH )) || + (env->priv ==PRV_S && virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_VSINH )) || + (env->priv ==PRV_U && virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_VUINH )) || + (env->priv ==PRV_S && !virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_SINH )) || + (env->priv ==PRV_U && !virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_UINH ))) { +return 0 ; + } + +/* Handle the overflow scenario */ +if (counter->mhpmcounter_val == max_val) { +if (counter->mhpmcounterh_val == max_val) { + counter->mhpmcounter_val =0 ; + counter->mhpmcounterh_val =0 ; +/* Generate interrupt only if OF bit is clear */ +if (!(env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_OF )) { + env->mhpmeventh_val[ctr_idx] |=MHPMEVENTH_BIT_OF ; +riscv_cpu_update_mip (cpu,MIP_LCOFIP ,BOOL_TO_MASK (1 )); + } + }else { + counter->mhpmcounterh_val++; + } + }else { + counter->mhpmcounter_val++; + } + +return 0 ; +} + +static int riscv_pmu_incr_ctr_rv64 (RISCVCPU *cpu,uint32_t ctr_idx) +{ +CPURISCVState *env = &cpu->env; +PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +uint64_t max_val =UINT64_MAX ; +bool virt_on =riscv_cpu_virt_enabled (env); + +/* Privilege mode filtering */ +if ((env->priv ==PRV_M && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_MINH )) || + (env->priv ==PRV_S && virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_VSINH )) || + (env->priv ==PRV_U && virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_VUINH )) || + (env->priv ==PRV_S && !virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_SINH )) || + (env->priv ==PRV_U && !virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_UINH ))) { +return 0 ; + } + +/* Handle the overflow scenario */ +if (counter->mhpmcounter_val == max_val) { + counter->mhpmcounter_val =0 ; +/* Generate interrupt only if OF bit is clear */
target/riscv/pmu.c line 125
Quoted 10 lines
target/riscv/pmu.c +10
-0+if (!(env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_OF )) { + env->mhpmevent_val[ctr_idx] |=MHPMEVENT_BIT_OF ; +riscv_cpu_update_mip (cpu,MIP_LCOFIP ,BOOL_TO_MASK (1 )); + } + }else { + counter->mhpmcounter_val++; + } +return 0 ; +} +
Why not use a uint64_t mhpmevent_val and mhpmcounter_val to store the total register for both RV32 and RV64?
If so, It seems that what should be affected is only the read/write function for mhpmeventh/mhpmcounterh csrs.
The other process can taken them the same for both RV32/RV64.
Regards,
Weiwei Li
Quoted 277 lines
target/riscv/pmu.c +251
-2+int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx) +{ +uint32_t ctr_idx; +int ret; +CPURISCVState *env = &cpu->env; +gpointer value; + + value =g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx)); +if (!value) { +return -1 ; + } + + ctr_idx =GPOINTER_TO_UINT (value); +if (!riscv_pmu_counter_enabled (cpu, ctr_idx) || +get_field (env->mcountinhibit,BIT (ctr_idx))) { +return -1 ; + } + +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + ret =riscv_pmu_incr_ctr_rv32 (cpu, ctr_idx); + }else { + ret =riscv_pmu_incr_ctr_rv64 (cpu, ctr_idx); + } + +return ret; +}bool riscv_pmu_ctr_monitor_instructions (CPURISCVState *env,uint32_t target_ctr) { -return (target_ctr ==0 ) ? true : false; +RISCVCPU *cpu; +uint32_t event_idx; +uint32_t ctr_idx; + +/* Fixed instret counter */ +if (target_ctr ==2 ) { +return true; + } + + cpu =RISCV_CPU (env_cpu (env)); + event_idx =RISCV_PMU_EVENT_HW_INSTRUCTIONS ; + ctr_idx =GPOINTER_TO_UINT (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx))); +if (!ctr_idx) { +return false; + } + +return target_ctr == ctr_idx ? true : false; }bool riscv_pmu_ctr_monitor_cycles (CPURISCVState *env,uint32_t target_ctr) { -return (target_ctr ==2 ) ? true : false; +RISCVCPU *cpu; +uint32_t event_idx; +uint32_t ctr_idx; + +/* Fixed mcycle counter */ +if (target_ctr ==0 ) { +return true; + } + + cpu =RISCV_CPU (env_cpu (env)); + event_idx =RISCV_PMU_EVENT_HW_CPU_CYCLES ; + ctr_idx =GPOINTER_TO_UINT (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx))); + +/* Counter zero is not used for event_ctr_map */ +if (!ctr_idx) { +return false; + } + +return (target_ctr == ctr_idx) ? true : false; +} + +static gboolean pmu_remove_event_map (gpointer key,gpointer value, +gpointer udata) +{ +return (GPOINTER_TO_UINT (value) ==GPOINTER_TO_UINT (udata)) ? true : false; +} + +static int64_t pmu_icount_ticks_to_ns (int64_t value) +{ +int64_t ret =0 ; + +if (icount_enabled ()) { + ret =icount_to_ns (value); + }else { + ret = (NANOSECONDS_PER_SECOND /RISCV_TIMEBASE_FREQ ) * value; + } + +return ret; +} + +int riscv_pmu_update_event_map (CPURISCVState *env,uint64_t value, +uint32_t ctr_idx) +{ +uint32_t event_idx; +RISCVCPU *cpu =RISCV_CPU (env_cpu (env)); + +if (!riscv_pmu_counter_valid (cpu, ctr_idx)) { +return -1 ; + } + +/** +* Expected mhpmevent value is zero for reset case. Remove the current +* mapping. +*/ +if (!value) { +g_hash_table_foreach_remove (cpu->pmu_event_ctr_map, + pmu_remove_event_map, +GUINT_TO_POINTER (ctr_idx)); +return 0 ; + } + + event_idx = value &MHPMEVENT_IDX_MASK ; +if (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx))) { +return 0 ; + } + +switch (event_idx) { +case RISCV_PMU_EVENT_HW_CPU_CYCLES : +case RISCV_PMU_EVENT_HW_INSTRUCTIONS : +case RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS : +case RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS : +case RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS : +break ; +default : +/* We don't support any raw events right now */ +return -1 ; + } +g_hash_table_insert (cpu->pmu_event_ctr_map,GUINT_TO_POINTER (event_idx), +GUINT_TO_POINTER (ctr_idx)); + +return 0 ; +} + +static void pmu_timer_trigger_irq (RISCVCPU *cpu, +enum riscv_pmu_event_idx evt_idx) +{ +uint32_t ctr_idx; +CPURISCVState *env = &cpu->env; +PMUCTRState *counter; +target_ulong *mhpmevent_val; +uint64_t of_bit_mask; +int64_t irq_trigger_at; + +if (evt_idx !=RISCV_PMU_EVENT_HW_CPU_CYCLES && + evt_idx !=RISCV_PMU_EVENT_HW_INSTRUCTIONS ) { +return ; + } + + ctr_idx =GPOINTER_TO_UINT (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (evt_idx))); +if (!riscv_pmu_counter_enabled (cpu, ctr_idx)) { +return ; + } + +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpmevent_val = &env->mhpmeventh_val[ctr_idx]; + of_bit_mask =MHPMEVENTH_BIT_OF ; + }else { + mhpmevent_val = &env->mhpmevent_val[ctr_idx]; + of_bit_mask =MHPMEVENT_BIT_OF ; + } + + counter = &env->pmu_ctrs[ctr_idx]; +if (counter->irq_overflow_left >0 ) { + irq_trigger_at =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + + counter->irq_overflow_left; +timer_mod_anticipate_ns (cpu->pmu_timer, irq_trigger_at); + counter->irq_overflow_left =0 ; +return ; + } + +if (cpu->pmu_avail_ctrs &BIT (ctr_idx)) { +/* Generate interrupt only if OF bit is clear */ +if (!(*mhpmevent_val & of_bit_mask)) { + *mhpmevent_val |= of_bit_mask; +riscv_cpu_update_mip (cpu,MIP_LCOFIP ,BOOL_TO_MASK (1 )); + } + } +} + +/* Timer callback for instret and cycle counter overflow */ +void riscv_pmu_timer_cb (void *priv) +{ +RISCVCPU *cpu = priv; + +/* Timer event was triggered only for these events */ +pmu_timer_trigger_irq (cpu,RISCV_PMU_EVENT_HW_CPU_CYCLES ); +pmu_timer_trigger_irq (cpu,RISCV_PMU_EVENT_HW_INSTRUCTIONS ); +} + +int riscv_pmu_setup_timer (CPURISCVState *env,uint64_t value,uint32_t ctr_idx) +{ +uint64_t overflow_delta, overflow_at; +int64_t overflow_ns, overflow_left =0 ; +RISCVCPU *cpu =RISCV_CPU (env_cpu (env)); +PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; + +if (!riscv_pmu_counter_valid (cpu, ctr_idx) || !cpu->cfg.ext_sscofpmf) { +return -1 ; + } + +if (value) { + overflow_delta =UINT64_MAX - value +1 ; + }else { + overflow_delta =UINT64_MAX ; + } + +/** +* QEMU supports only int64_t timers while RISC-V counters are uint64_t. +* Compute the leftover and save it so that it can be reprogrammed again +* when timer expires. +*/ +if (overflow_delta >INT64_MAX ) { + overflow_left = overflow_delta -INT64_MAX ; + } + +if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) || +riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { + overflow_ns =pmu_icount_ticks_to_ns ((int64_t )overflow_delta); + overflow_left =pmu_icount_ticks_to_ns (overflow_left) ; + }else { +return -1 ; + } + overflow_at = (uint64_t )qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + overflow_ns; + +if (overflow_at >INT64_MAX ) { + overflow_left += overflow_at -INT64_MAX ; + counter->irq_overflow_left = overflow_left; + overflow_at =INT64_MAX ; + } +timer_mod_anticipate_ns (cpu->pmu_timer, overflow_at); + +return 0 ; +} + + +int riscv_pmu_init (RISCVCPU *cpu,int num_counters) +{ +if (num_counters > (RV_MAX_MHPMCOUNTERS -3 )) { +return -1 ; + } + + cpu->pmu_event_ctr_map =g_hash_table_new (g_direct_hash, g_direct_equal); +if (!cpu->pmu_event_ctr_map) { +/* PMU support can not be enabled */ +qemu_log_mask (LOG_UNIMP ,"PMU events can't be supported\n" ); + cpu->cfg.pmu_num =0 ; +return -1 ; + } + +/* Create a bitmask of available programmable counters */ + cpu->pmu_avail_ctrs =MAKE_32BIT_MASK (3 , num_counters); + +return 0 ; }target/riscv/pmu.h +7
-0@@ -26,3 +26,10 @@ bool riscv_pmu_ctr_monitor_instructions(CPURISCVState *env, uint32_t target_ctr);bool riscv_pmu_ctr_monitor_cycles (CPURISCVState *env,uint32_t target_ctr); +void riscv_pmu_timer_cb (void *priv); +int riscv_pmu_init (RISCVCPU *cpu,int num_counters); +int riscv_pmu_update_event_map (CPURISCVState *env,uint64_t value, +uint32_t ctr_idx); +int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx); +int riscv_pmu_setup_timer (CPURISCVState *env,uint64_t value, +uint32_t ctr_idx);
Re: [PATCH v10 08/12] target/riscv: Add sscofpmf extension support
In reply to Atish PatraQuoted 416 lines · Atish Patra
The Sscofpmf ('Ss' for Privileged arch and Supervisor-level extensions, and 'cofpmf' for Count OverFlow and Privilege Mode Filtering) extension allows the perf to handle overflow interrupts and filtering support. This patch provides a framework for programmable counters to leverage the extension. As the extension doesn't have any provision for the overflow bit for fixed counters, the fixed events can also be monitoring using programmable counters. The underlying counters for cycle and instruction counters are always running. Thus, a separate timer device is programmed to handle the overflow.
Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>--- target/riscv/cpu.c | 11 ++ target/riscv/cpu.h | 25 +++ target/riscv/cpu_bits.h | 55 +++++++ target/riscv/csr.c | 165 ++++++++++++++++++- target/riscv/machine.c | 1 + target/riscv/pmu.c | 357 +++++++++++++++++++++++++++++++++++++++- target/riscv/pmu.h | 7 + 7 files changed, 610 insertions(+), 11 deletions(-)target/riscv/cpu.c +11
-0@@ -22,6 +22,7 @@#include "qemu/ctype.h" #include "qemu/log.h" #include "cpu.h" +#include "pmu.h" #include "internals.h" #include "exec/exec-all.h" #include "qapi/error.h" @@ -775,6 +776,15 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)set_misa (env, env->misa_mxl, ext); } +#ifndef CONFIG_USER_ONLY +if (cpu->cfg.pmu_num) { +if (!riscv_pmu_init (cpu, cpu->cfg.pmu_num) && cpu->cfg.ext_sscofpmf) { + cpu->pmu_timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , + riscv_pmu_timer_cb, cpu); + } + } +#endif +riscv_cpu_register_gdb_regs_for_features (cs);qemu_init_vcpu (cs);@@ -879,6 +889,7 @@ static Property riscv_cpu_extensions[] = {DEFINE_PROP_BOOL ("v" ,RISCVCPU , cfg.ext_v, false),DEFINE_PROP_BOOL ("h" ,RISCVCPU , cfg.ext_h, true),DEFINE_PROP_UINT8 ("pmu-num" ,RISCVCPU , cfg.pmu_num,16 ), +DEFINE_PROP_BOOL ("sscofpmf" ,RISCVCPU , cfg.ext_sscofpmf, false),DEFINE_PROP_BOOL ("Zifencei" ,RISCVCPU , cfg.ext_ifencei, true),DEFINE_PROP_BOOL ("Zicsr" ,RISCVCPU , cfg.ext_icsr, true), DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),target/riscv/cpu.h +25
-0@@ -137,6 +137,8 @@ typedef struct PMUCTRState {/* Snapshort value of a counter in RV32 */ target_ulong mhpmcounterh_prev;bool started; +/* Value beyond UINT32_MAX/UINT64_MAX before overflow interrupt trigger */ +target_ulong irq_overflow_left; } PMUCTRState;struct CPUArchState {@@ -297,6 +299,9 @@ struct CPUArchState {/* PMU event selector configured values. First three are unused*/ target_ulong mhpmevent_val[RV_MAX_MHPMEVENTS ]; +/* PMU event selector configured values for RV32*/ +target_ulong mhpmeventh_val[RV_MAX_MHPMEVENTS ]; +target_ulong sscratch;target_ulong mscratch;@@ -433,6 +438,7 @@ struct RISCVCPUConfig {bool ext_zve32f;bool ext_zve64f;bool ext_zmmul; +bool ext_sscofpmf;bool rvv_ta_all_1s;uint32_t mvendorid;@@ -479,6 +485,12 @@ struct ArchCPU {/* Configuration Settings */ RISCVCPUConfig cfg; + +QEMUTimer *pmu_timer; +/* A bitmask of Available programmable counters */ +uint32_t pmu_avail_ctrs; +/* Mapping of events to counters */ +GHashTable *pmu_event_ctr_map; };static inline int riscv_has_ext (CPURISCVState *env,target_ulong ext)@@ -738,6 +750,19 @@ enum { CSR_TABLE_SIZE = 0x1000 }; +/** +* The event id are encoded based on the encoding specified in the +* SBI specification v0.3 +*/ + +enum riscv_pmu_event_idx { +RISCV_PMU_EVENT_HW_CPU_CYCLES =0x01 , +RISCV_PMU_EVENT_HW_INSTRUCTIONS =0x02 , +RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS =0x10019 , +RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS =0x1001B , +RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS =0x10021 , +}; +/* CSR function table */ extern riscv_csr_operations csr_ops[CSR_TABLE_SIZE ];target/riscv/cpu_bits.h +55
-0@@ -400,6 +400,37 @@#define CSR_MHPMEVENT29 0x33d#define CSR_MHPMEVENT30 0x33e#define CSR_MHPMEVENT31 0x33f + +#define CSR_MHPMEVENT3H 0x723 +#define CSR_MHPMEVENT4H 0x724 +#define CSR_MHPMEVENT5H 0x725 +#define CSR_MHPMEVENT6H 0x726 +#define CSR_MHPMEVENT7H 0x727 +#define CSR_MHPMEVENT8H 0x728 +#define CSR_MHPMEVENT9H 0x729 +#define CSR_MHPMEVENT10H 0x72a +#define CSR_MHPMEVENT11H 0x72b +#define CSR_MHPMEVENT12H 0x72c +#define CSR_MHPMEVENT13H 0x72d +#define CSR_MHPMEVENT14H 0x72e +#define CSR_MHPMEVENT15H 0x72f +#define CSR_MHPMEVENT16H 0x730 +#define CSR_MHPMEVENT17H 0x731 +#define CSR_MHPMEVENT18H 0x732 +#define CSR_MHPMEVENT19H 0x733 +#define CSR_MHPMEVENT20H 0x734 +#define CSR_MHPMEVENT21H 0x735 +#define CSR_MHPMEVENT22H 0x736 +#define CSR_MHPMEVENT23H 0x737 +#define CSR_MHPMEVENT24H 0x738 +#define CSR_MHPMEVENT25H 0x739 +#define CSR_MHPMEVENT26H 0x73a +#define CSR_MHPMEVENT27H 0x73b +#define CSR_MHPMEVENT28H 0x73c +#define CSR_MHPMEVENT29H 0x73d +#define CSR_MHPMEVENT30H 0x73e +#define CSR_MHPMEVENT31H 0x73f +#define CSR_MHPMCOUNTER3H 0xb83#define CSR_MHPMCOUNTER4H 0xb84#define CSR_MHPMCOUNTER5H 0xb85@@ -461,6 +492,7 @@#define CSR_VSMTE 0x2c0#define CSR_VSPMMASK 0x2c1#define CSR_VSPMBASE 0x2c2 +#define CSR_SCOUNTOVF 0xda0/* Crypto Extension */ #define CSR_SEED 0x015@@ -638,6 +670,7 @@ typedef enum RISCVException {#define IRQ_VS_EXT 10#define IRQ_M_EXT 11#define IRQ_S_GEXT 12 +#define IRQ_PMU_OVF 13#define IRQ_LOCAL_MAX 16#define IRQ_LOCAL_GUEST_MAX (TARGET_LONG_BITS - 1)@@ -655,11 +688,13 @@ typedef enum RISCVException {#define MIP_VSEIP (1 << IRQ_VS_EXT)#define MIP_MEIP (1 << IRQ_M_EXT)#define MIP_SGEIP (1 << IRQ_S_GEXT) +#define MIP_LCOFIP (1 << IRQ_PMU_OVF)/* sip masks */ #define SIP_SSIP MIP_SSIP#define SIP_STIP MIP_STIP#define SIP_SEIP MIP_SEIP +#define SIP_LCOFIP MIP_LCOFIP/* MIE masks */ #define MIE_SEIE (1 << IRQ_S_EXT)@@ -813,4 +848,24 @@ typedef enum RISCVException {#define SEED_OPST_WAIT (0b01 << 30)#define SEED_OPST_ES16 (0b10 << 30)#define SEED_OPST_DEAD (0b11 << 30) +/* PMU related bits */ +#define MIE_LCOFIE (1 << IRQ_PMU_OVF) + +#define MHPMEVENT_BIT_OF BIT_ULL(63) +#define MHPMEVENTH_BIT_OF BIT(31) +#define MHPMEVENT_BIT_MINH BIT_ULL(62) +#define MHPMEVENTH_BIT_MINH BIT(30) +#define MHPMEVENT_BIT_SINH BIT_ULL(61) +#define MHPMEVENTH_BIT_SINH BIT(29) +#define MHPMEVENT_BIT_UINH BIT_ULL(60) +#define MHPMEVENTH_BIT_UINH BIT(28) +#define MHPMEVENT_BIT_VSINH BIT_ULL(59) +#define MHPMEVENTH_BIT_VSINH BIT(27) +#define MHPMEVENT_BIT_VUINH BIT_ULL(58) +#define MHPMEVENTH_BIT_VUINH BIT(26) + +#define MHPMEVENT_SSCOF_MASK _ULL(0xFFFF000000000000) +#define MHPMEVENT_IDX_MASK 0xFFFFF +#define MHPMEVENT_SSCOF_RESVD 16 +#endif target/riscv/csr.c +91
-7@@ -74,7 +74,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno)CPUState *cs =env_cpu (env);RISCVCPU *cpu =RISCV_CPU (cs);int ctr_index; -int base_csrno =CSR_HPMCOUNTER3 ; +int base_csrno =CSR_CYCLE ;bool rv32 =riscv_cpu_mxl (env) ==MXL_RV32 ? true : false;if (rv32 && csrno >=CSR_CYCLEH ) {@@ -83,11 +83,18 @@ static RISCVException ctr(CPURISCVState *env, int csrno) } ctr_index = csrno - base_csrno; -if (!cpu->cfg.pmu_num || ctr_index >= (cpu->cfg.pmu_num)) { +if ((csrno >=CSR_CYCLE && csrno <=CSR_INSTRET ) || + (csrno >=CSR_CYCLEH && csrno <=CSR_INSTRETH )) { + goto skip_ext_pmu_check; + } + +if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs &BIT (ctr_index)))) {/* No counter is enabled in PMU or the counter is out of range */ return RISCV_EXCP_ILLEGAL_INST ; } +skip_ext_pmu_check: +if (env->priv ==PRV_S ) {switch (csrno) {case CSR_CYCLE :@@ -106,7 +113,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: - ctr_index = csrno -CSR_CYCLE ;if (!get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_ILLEGAL_INST ; }@@ -130,7 +136,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: - ctr_index = csrno -CSR_CYCLEH ;if (!get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_ILLEGAL_INST ; }@@ -160,7 +165,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: - ctr_index = csrno -CSR_CYCLE ;if (!get_field (env->hcounteren,1 << ctr_index) &&get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ;@@ -188,7 +192,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: - ctr_index = csrno -CSR_CYCLEH ;if (!get_field (env->hcounteren,1 << ctr_index) &&get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ;@@ -240,6 +243,18 @@ static RISCVException mctr32(CPURISCVState *env, int csrno)return mctr (env, csrno); } +static RISCVException sscofpmf (CPURISCVState *env,int csrno) +{ +CPUState *cs =env_cpu (env); +RISCVCPU *cpu =RISCV_CPU (cs); + +if (!cpu->cfg.ext_sscofpmf) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return RISCV_EXCP_NONE ; +} +static RISCVException any (CPURISCVState *env,int csrno) {return RISCV_EXCP_NONE ;@@ -663,9 +678,38 @@ static int read_mhpmevent(CPURISCVState *env, int csrno, target_ulong *val)static int write_mhpmevent (CPURISCVState *env,int csrno,target_ulong val) {int evt_index = csrno -CSR_MCOUNTINHIBIT ; +uint64_t mhpmevt_val = val; env->mhpmevent_val[evt_index] = val; +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpmevt_val = mhpmevt_val | ((uint64_t )env->mhpmeventh_val[evt_index] <<32 ); + } +riscv_pmu_update_event_map (env, mhpmevt_val, evt_index); + +return RISCV_EXCP_NONE ; +} + +static int read_mhpmeventh (CPURISCVState *env,int csrno,target_ulong *val) +{ +int evt_index = csrno -CSR_MHPMEVENT3H +3 ; + + *val = env->mhpmeventh_val[evt_index]; + +return RISCV_EXCP_NONE ; +} + +static int write_mhpmeventh (CPURISCVState *env,int csrno,target_ulong val) +{ +int evt_index = csrno -CSR_MHPMEVENT3H +3 ; +uint64_t mhpmevth_val = val; +uint64_t mhpmevt_val = env->mhpmevent_val[evt_index]; + + mhpmevt_val = mhpmevt_val | (mhpmevth_val <<32 ); + env->mhpmeventh_val[evt_index] = val; + +riscv_pmu_update_event_map (env, mhpmevt_val, evt_index); +return RISCV_EXCP_NONE ; }@@ -673,12 +717,20 @@ static int write_mhpmcounter(CPURISCVState *env, int csrno, target_ulong val) {int ctr_idx = csrno -CSR_MCYCLE ;PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +uint64_t mhpmctr_val = val; counter->mhpmcounter_val = val;if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) ||riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { counter->mhpmcounter_prev =get_ticks (false); - }else { +if (ctr_idx >2 ) { +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpmctr_val = mhpmctr_val | + ((uint64_t )counter->mhpmcounterh_val <<32 ); + } +riscv_pmu_setup_timer (env, mhpmctr_val, ctr_idx); + } + }else {/* Other counters can keep incrementing from the given value */ counter->mhpmcounter_prev = val; }@@ -690,11 +742,17 @@ static int write_mhpmcounterh(CPURISCVState *env, int csrno, target_ulong val) {int ctr_idx = csrno -CSR_MCYCLEH ;PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +uint64_t mhpmctr_val = counter->mhpmcounter_val; +uint64_t mhpmctrh_val = val; counter->mhpmcounterh_val = val; + mhpmctr_val = mhpmctr_val | (mhpmctrh_val <<32 );if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) ||riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { counter->mhpmcounterh_prev =get_ticks (true); +if (ctr_idx >2 ) { +riscv_pmu_setup_timer (env, mhpmctr_val, ctr_idx); + } }else { counter->mhpmcounterh_prev = val; }@@ -770,6 +828,32 @@ static int read_hpmcounterh(CPURISCVState *env, int csrno, target_ulong *val)return riscv_pmu_read_ctr (env, val, true, ctr_index); } +static int read_scountovf (CPURISCVState *env,int csrno,target_ulong *val) +{ +int mhpmevt_start =CSR_MHPMEVENT3 -CSR_MCOUNTINHIBIT ; +int i; + *val =0 ; +target_ulong *mhpm_evt_val; +uint64_t of_bit_mask; + +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpm_evt_val = env->mhpmeventh_val; + of_bit_mask =MHPMEVENTH_BIT_OF ; + }else { + mhpm_evt_val = env->mhpmevent_val; + of_bit_mask =MHPMEVENT_BIT_OF ; + } + +for (i = mhpmevt_start; i <RV_MAX_MHPMEVENTS ; i++) { +if ((get_field (env->mcounteren,BIT (i))) && + (mhpm_evt_val[i] & of_bit_mask)) { + *val |=BIT (i); + } + } + +return RISCV_EXCP_NONE ; +} +static RISCVException read_time (CPURISCVState *env,int csrno,target_ulong *val) {
target/riscv/csr.c static RISCVException read_timeh(CPURISCVState *env, int csrno, · line 887
Quoted 6 lines
target/riscv/csr.c +2
-1@@ -799,7 +883,8 @@ static RISCVException read_timeh(CPURISCVState *env, int csrno,/* Machine constants */ #define M_MODE_INTERRUPTS ((uint64_t)(MIP_MSIP | MIP_MTIP | MIP_MEIP)) -#define S_MODE_INTERRUPTS ((uint64_t)(MIP_SSIP | MIP_STIP | MIP_SEIP)) +#define S_MODE_INTERRUPTS ((uint64_t)(MIP_SSIP | MIP_STIP | MIP_SEIP | \ + MIP_LCOFIP))
It seems a problem here. S_MODE_INTERRUPTS will be used in delegable_ints, and then be used not
only in rmw_mip64 but also in rmw_mideleg64, so if we add MIP_LCOFIP here, this bit will also added
into mideleg which is not stated in the spec for sscofpmf.
And if MIP_LCOFIP is not a bit in mideleg, the following modification for 'sip_writable_mask' will not work.
Regards,
Weiwei Li
Quoted 488 lines
target/riscv/csr.c +63
-1#define VS_MODE_INTERRUPTS ((uint64_t)(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP))#define HS_MODE_INTERRUPTS ((uint64_t)(MIP_SGEIP | VS_MODE_INTERRUPTS))@@ -840,7 +925,8 @@ static const target_ulong vs_delegable_excps = DELEGABLE_EXCPS &static const target_ulong sstatus_v1_10_mask =SSTATUS_SIE |SSTATUS_SPIE |SSTATUS_UIE |SSTATUS_UPIE |SSTATUS_SPP |SSTATUS_FS |SSTATUS_XS |SSTATUS_SUM |SSTATUS_MXR |SSTATUS_VS ; -static const target_ulong sip_writable_mask =SIP_SSIP |MIP_USIP |MIP_UEIP ; +static const target_ulong sip_writable_mask =SIP_SSIP |MIP_USIP |MIP_UEIP | +SIP_LCOFIP ;static const target_ulong hip_writable_mask =MIP_VSSIP ;static const target_ulong hvip_writable_mask =MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ;static const target_ulong vsip_writable_mask =MIP_VSSIP ;@@ -4005,6 +4091,65 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MHPMEVENT31] = { "mhpmevent31", any, read_mhpmevent, write_mhpmevent }, + [CSR_MHPMEVENT3H] = { "mhpmevent3h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT4H] = { "mhpmevent4h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT5H] = { "mhpmevent5h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT6H] = { "mhpmevent6h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT7H] = { "mhpmevent7h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT8H] = { "mhpmevent8h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT9H] = { "mhpmevent9h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT10H] = { "mhpmevent10h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT11H] = { "mhpmevent11h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT12H] = { "mhpmevent12h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT13H] = { "mhpmevent13h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT14H] = { "mhpmevent14h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT15H] = { "mhpmevent15h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT16H] = { "mhpmevent16h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT17H] = { "mhpmevent17h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT18H] = { "mhpmevent18h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT19H] = { "mhpmevent19h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT20H] = { "mhpmevent20h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT21H] = { "mhpmevent21h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT22H] = { "mhpmevent22h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT23H] = { "mhpmevent23h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT24H] = { "mhpmevent24h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT25H] = { "mhpmevent25h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT26H] = { "mhpmevent26h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT27H] = { "mhpmevent27h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT28H] = { "mhpmevent28h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT29H] = { "mhpmevent29h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT30H] = { "mhpmevent30h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT31H] = { "mhpmevent31h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_HPMCOUNTER3H] = { "hpmcounter3h", ctr32, read_hpmcounterh }, [CSR_HPMCOUNTER4H] = { "hpmcounter4h", ctr32, read_hpmcounterh }, [CSR_HPMCOUNTER5H] = { "hpmcounter5h", ctr32, read_hpmcounterh },@@ -4093,5 +4238,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { write_mhpmcounterh }, [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", mctr32, read_hpmcounterh, write_mhpmcounterh }, + [CSR_SCOUNTOVF] = { "scountovf", sscofpmf, read_scountovf }, + #endif /* !CONFIG_USER_ONLY */ };target/riscv/machine.c +1
-0@@ -355,6 +355,7 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_STRUCT_ARRAY (env.pmu_ctrs,RISCVCPU ,RV_MAX_MHPMCOUNTERS ,0 , vmstate_pmu_ctr_state, PMUCTRState),VMSTATE_UINTTL_ARRAY (env.mhpmevent_val,RISCVCPU ,RV_MAX_MHPMEVENTS ), +VMSTATE_UINTTL_ARRAY (env.mhpmeventh_val,RISCVCPU ,RV_MAX_MHPMEVENTS ),VMSTATE_UINTTL (env.sscratch,RISCVCPU ),VMSTATE_UINTTL (env.mscratch,RISCVCPU ), VMSTATE_UINT64(env.mfromhost, RISCVCPU),target/riscv/pmu.c +355
-2@@ -19,14 +19,367 @@#include "qemu/osdep.h" #include "cpu.h" #include "pmu.h" +#include "sysemu/cpu-timers.h" + +#define RISCV_TIMEBASE_FREQ 1000000000/* 1Ghz */ +#define MAKE_32BIT_MASK (shift, length) \ + (((uint32_t)(~0UL) >> (32 - (length))) << (shift)) + +static bool riscv_pmu_counter_valid (RISCVCPU *cpu,uint32_t ctr_idx) +{ +if (ctr_idx <3 || ctr_idx >=RV_MAX_MHPMCOUNTERS || + !(cpu->pmu_avail_ctrs &BIT (ctr_idx))) { +return false; + }else { +return true; + } +} + +static bool riscv_pmu_counter_enabled (RISCVCPU *cpu,uint32_t ctr_idx) +{ +CPURISCVState *env = &cpu->env; + +if (riscv_pmu_counter_valid (cpu, ctr_idx) && + !get_field (env->mcountinhibit,BIT (ctr_idx))) { +return true; + }else { +return false; + } +} + +static int riscv_pmu_incr_ctr_rv32 (RISCVCPU *cpu,uint32_t ctr_idx) +{ +CPURISCVState *env = &cpu->env; +target_ulong max_val =UINT32_MAX ; +PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +bool virt_on =riscv_cpu_virt_enabled (env); + +/* Privilege mode filtering */ +if ((env->priv ==PRV_M && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_MINH )) || + (env->priv ==PRV_S && virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_VSINH )) || + (env->priv ==PRV_U && virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_VUINH )) || + (env->priv ==PRV_S && !virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_SINH )) || + (env->priv ==PRV_U && !virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_UINH ))) { +return 0 ; + } + +/* Handle the overflow scenario */ +if (counter->mhpmcounter_val == max_val) { +if (counter->mhpmcounterh_val == max_val) { + counter->mhpmcounter_val =0 ; + counter->mhpmcounterh_val =0 ; +/* Generate interrupt only if OF bit is clear */ +if (!(env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_OF )) { + env->mhpmeventh_val[ctr_idx] |=MHPMEVENTH_BIT_OF ; +riscv_cpu_update_mip (cpu,MIP_LCOFIP ,BOOL_TO_MASK (1 )); + } + }else { + counter->mhpmcounterh_val++; + } + }else { + counter->mhpmcounter_val++; + } + +return 0 ; +} + +static int riscv_pmu_incr_ctr_rv64 (RISCVCPU *cpu,uint32_t ctr_idx) +{ +CPURISCVState *env = &cpu->env; +PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +uint64_t max_val =UINT64_MAX ; +bool virt_on =riscv_cpu_virt_enabled (env); + +/* Privilege mode filtering */ +if ((env->priv ==PRV_M && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_MINH )) || + (env->priv ==PRV_S && virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_VSINH )) || + (env->priv ==PRV_U && virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_VUINH )) || + (env->priv ==PRV_S && !virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_SINH )) || + (env->priv ==PRV_U && !virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_UINH ))) { +return 0 ; + } + +/* Handle the overflow scenario */ +if (counter->mhpmcounter_val == max_val) { + counter->mhpmcounter_val =0 ; +/* Generate interrupt only if OF bit is clear */ +if (!(env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_OF )) { + env->mhpmevent_val[ctr_idx] |=MHPMEVENT_BIT_OF ; +riscv_cpu_update_mip (cpu,MIP_LCOFIP ,BOOL_TO_MASK (1 )); + } + }else { + counter->mhpmcounter_val++; + } +return 0 ; +} + +int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx) +{ +uint32_t ctr_idx; +int ret; +CPURISCVState *env = &cpu->env; +gpointer value; + + value =g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx)); +if (!value) { +return -1 ; + } + + ctr_idx =GPOINTER_TO_UINT (value); +if (!riscv_pmu_counter_enabled (cpu, ctr_idx) || +get_field (env->mcountinhibit,BIT (ctr_idx))) { +return -1 ; + } + +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + ret =riscv_pmu_incr_ctr_rv32 (cpu, ctr_idx); + }else { + ret =riscv_pmu_incr_ctr_rv64 (cpu, ctr_idx); + } + +return ret; +}bool riscv_pmu_ctr_monitor_instructions (CPURISCVState *env,uint32_t target_ctr) { -return (target_ctr ==0 ) ? true : false; +RISCVCPU *cpu; +uint32_t event_idx; +uint32_t ctr_idx; + +/* Fixed instret counter */ +if (target_ctr ==2 ) { +return true; + } + + cpu =RISCV_CPU (env_cpu (env)); + event_idx =RISCV_PMU_EVENT_HW_INSTRUCTIONS ; + ctr_idx =GPOINTER_TO_UINT (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx))); +if (!ctr_idx) { +return false; + } + +return target_ctr == ctr_idx ? true : false; }bool riscv_pmu_ctr_monitor_cycles (CPURISCVState *env,uint32_t target_ctr) { -return (target_ctr ==2 ) ? true : false; +RISCVCPU *cpu; +uint32_t event_idx; +uint32_t ctr_idx; + +/* Fixed mcycle counter */ +if (target_ctr ==0 ) { +return true; + } + + cpu =RISCV_CPU (env_cpu (env)); + event_idx =RISCV_PMU_EVENT_HW_CPU_CYCLES ; + ctr_idx =GPOINTER_TO_UINT (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx))); + +/* Counter zero is not used for event_ctr_map */ +if (!ctr_idx) { +return false; + } + +return (target_ctr == ctr_idx) ? true : false; +} + +static gboolean pmu_remove_event_map (gpointer key,gpointer value, +gpointer udata) +{ +return (GPOINTER_TO_UINT (value) ==GPOINTER_TO_UINT (udata)) ? true : false; +} + +static int64_t pmu_icount_ticks_to_ns (int64_t value) +{ +int64_t ret =0 ; + +if (icount_enabled ()) { + ret =icount_to_ns (value); + }else { + ret = (NANOSECONDS_PER_SECOND /RISCV_TIMEBASE_FREQ ) * value; + } + +return ret; +} + +int riscv_pmu_update_event_map (CPURISCVState *env,uint64_t value, +uint32_t ctr_idx) +{ +uint32_t event_idx; +RISCVCPU *cpu =RISCV_CPU (env_cpu (env)); + +if (!riscv_pmu_counter_valid (cpu, ctr_idx)) { +return -1 ; + } + +/** +* Expected mhpmevent value is zero for reset case. Remove the current +* mapping. +*/ +if (!value) { +g_hash_table_foreach_remove (cpu->pmu_event_ctr_map, + pmu_remove_event_map, +GUINT_TO_POINTER (ctr_idx)); +return 0 ; + } + + event_idx = value &MHPMEVENT_IDX_MASK ; +if (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx))) { +return 0 ; + } + +switch (event_idx) { +case RISCV_PMU_EVENT_HW_CPU_CYCLES : +case RISCV_PMU_EVENT_HW_INSTRUCTIONS : +case RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS : +case RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS : +case RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS : +break ; +default : +/* We don't support any raw events right now */ +return -1 ; + } +g_hash_table_insert (cpu->pmu_event_ctr_map,GUINT_TO_POINTER (event_idx), +GUINT_TO_POINTER (ctr_idx)); + +return 0 ; +} + +static void pmu_timer_trigger_irq (RISCVCPU *cpu, +enum riscv_pmu_event_idx evt_idx) +{ +uint32_t ctr_idx; +CPURISCVState *env = &cpu->env; +PMUCTRState *counter; +target_ulong *mhpmevent_val; +uint64_t of_bit_mask; +int64_t irq_trigger_at; + +if (evt_idx !=RISCV_PMU_EVENT_HW_CPU_CYCLES && + evt_idx !=RISCV_PMU_EVENT_HW_INSTRUCTIONS ) { +return ; + } + + ctr_idx =GPOINTER_TO_UINT (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (evt_idx))); +if (!riscv_pmu_counter_enabled (cpu, ctr_idx)) { +return ; + } + +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpmevent_val = &env->mhpmeventh_val[ctr_idx]; + of_bit_mask =MHPMEVENTH_BIT_OF ; + }else { + mhpmevent_val = &env->mhpmevent_val[ctr_idx]; + of_bit_mask =MHPMEVENT_BIT_OF ; + } + + counter = &env->pmu_ctrs[ctr_idx]; +if (counter->irq_overflow_left >0 ) { + irq_trigger_at =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + + counter->irq_overflow_left; +timer_mod_anticipate_ns (cpu->pmu_timer, irq_trigger_at); + counter->irq_overflow_left =0 ; +return ; + } + +if (cpu->pmu_avail_ctrs &BIT (ctr_idx)) { +/* Generate interrupt only if OF bit is clear */ +if (!(*mhpmevent_val & of_bit_mask)) { + *mhpmevent_val |= of_bit_mask; +riscv_cpu_update_mip (cpu,MIP_LCOFIP ,BOOL_TO_MASK (1 )); + } + } +} + +/* Timer callback for instret and cycle counter overflow */ +void riscv_pmu_timer_cb (void *priv) +{ +RISCVCPU *cpu = priv; + +/* Timer event was triggered only for these events */ +pmu_timer_trigger_irq (cpu,RISCV_PMU_EVENT_HW_CPU_CYCLES ); +pmu_timer_trigger_irq (cpu,RISCV_PMU_EVENT_HW_INSTRUCTIONS ); +} + +int riscv_pmu_setup_timer (CPURISCVState *env,uint64_t value,uint32_t ctr_idx) +{ +uint64_t overflow_delta, overflow_at; +int64_t overflow_ns, overflow_left =0 ; +RISCVCPU *cpu =RISCV_CPU (env_cpu (env)); +PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; + +if (!riscv_pmu_counter_valid (cpu, ctr_idx) || !cpu->cfg.ext_sscofpmf) { +return -1 ; + } + +if (value) { + overflow_delta =UINT64_MAX - value +1 ; + }else { + overflow_delta =UINT64_MAX ; + } + +/** +* QEMU supports only int64_t timers while RISC-V counters are uint64_t. +* Compute the leftover and save it so that it can be reprogrammed again +* when timer expires. +*/ +if (overflow_delta >INT64_MAX ) { + overflow_left = overflow_delta -INT64_MAX ; + } + +if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) || +riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { + overflow_ns =pmu_icount_ticks_to_ns ((int64_t )overflow_delta); + overflow_left =pmu_icount_ticks_to_ns (overflow_left) ; + }else { +return -1 ; + } + overflow_at = (uint64_t )qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + overflow_ns; + +if (overflow_at >INT64_MAX ) { + overflow_left += overflow_at -INT64_MAX ; + counter->irq_overflow_left = overflow_left; + overflow_at =INT64_MAX ; + } +timer_mod_anticipate_ns (cpu->pmu_timer, overflow_at); + +return 0 ; +} + + +int riscv_pmu_init (RISCVCPU *cpu,int num_counters) +{ +if (num_counters > (RV_MAX_MHPMCOUNTERS -3 )) { +return -1 ; + } + + cpu->pmu_event_ctr_map =g_hash_table_new (g_direct_hash, g_direct_equal); +if (!cpu->pmu_event_ctr_map) { +/* PMU support can not be enabled */ +qemu_log_mask (LOG_UNIMP ,"PMU events can't be supported\n" ); + cpu->cfg.pmu_num =0 ; +return -1 ; + } + +/* Create a bitmask of available programmable counters */ + cpu->pmu_avail_ctrs =MAKE_32BIT_MASK (3 , num_counters); + +return 0 ; }target/riscv/pmu.h +7
-0@@ -26,3 +26,10 @@ bool riscv_pmu_ctr_monitor_instructions(CPURISCVState *env, uint32_t target_ctr);bool riscv_pmu_ctr_monitor_cycles (CPURISCVState *env,uint32_t target_ctr); +void riscv_pmu_timer_cb (void *priv); +int riscv_pmu_init (RISCVCPU *cpu,int num_counters); +int riscv_pmu_update_event_map (CPURISCVState *env,uint64_t value, +uint32_t ctr_idx); +int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx); +int riscv_pmu_setup_timer (CPURISCVState *env,uint64_t value, +uint32_t ctr_idx);
Re: [PATCH v10 08/12] target/riscv: Add sscofpmf extension support
Quoted 429 lines · Weiwei Li
在 2022/6/21 上午7:15, Atish Patra 写道:
The Sscofpmf ('Ss' for Privileged arch and Supervisor-level extensions, and 'cofpmf' for Count OverFlow and Privilege Mode Filtering) extension allows the perf to handle overflow interrupts and filtering support. This patch provides a framework for programmable counters to leverage the extension. As the extension doesn't have any provision for the overflow bit for fixed counters, the fixed events can also be monitoring using programmable counters. The underlying counters for cycle and instruction counters are always running. Thus, a separate timer device is programmed to handle the overflow.
Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>--- target/riscv/cpu.c | 11 ++ target/riscv/cpu.h | 25 +++ target/riscv/cpu_bits.h | 55 +++++++ target/riscv/csr.c | 165 ++++++++++++++++++- target/riscv/machine.c | 1 + target/riscv/pmu.c | 357 +++++++++++++++++++++++++++++++++++++++- target/riscv/pmu.h | 7 + 7 files changed, 610 insertions(+), 11 deletions(-)target/riscv/cpu.c +11
-0@@ -22,6 +22,7 @@#include "qemu/ctype.h" #include "qemu/log.h" #include "cpu.h" +#include "pmu.h" #include "internals.h" #include "exec/exec-all.h" #include "qapi/error.h" @@ -775,6 +776,15 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)set_misa (env, env->misa_mxl, ext); } +#ifndef CONFIG_USER_ONLY +if (cpu->cfg.pmu_num) { +if (!riscv_pmu_init (cpu, cpu->cfg.pmu_num) && cpu->cfg.ext_sscofpmf) { + cpu->pmu_timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , + riscv_pmu_timer_cb, cpu); + } + } +#endif +riscv_cpu_register_gdb_regs_for_features (cs);qemu_init_vcpu (cs);@@ -879,6 +889,7 @@ static Property riscv_cpu_extensions[] = {DEFINE_PROP_BOOL ("v" ,RISCVCPU , cfg.ext_v, false),DEFINE_PROP_BOOL ("h" ,RISCVCPU , cfg.ext_h, true),DEFINE_PROP_UINT8 ("pmu-num" ,RISCVCPU , cfg.pmu_num,16 ), +DEFINE_PROP_BOOL ("sscofpmf" ,RISCVCPU , cfg.ext_sscofpmf, false),DEFINE_PROP_BOOL ("Zifencei" ,RISCVCPU , cfg.ext_ifencei, true),DEFINE_PROP_BOOL ("Zicsr" ,RISCVCPU , cfg.ext_icsr, true), DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),target/riscv/cpu.h +25
-0@@ -137,6 +137,8 @@ typedef struct PMUCTRState {/* Snapshort value of a counter in RV32 */ target_ulong mhpmcounterh_prev;bool started; +/* Value beyond UINT32_MAX/UINT64_MAX before overflow interrupt trigger */ +target_ulong irq_overflow_left; } PMUCTRState;struct CPUArchState {@@ -297,6 +299,9 @@ struct CPUArchState {/* PMU event selector configured values. First three are unused*/ target_ulong mhpmevent_val[RV_MAX_MHPMEVENTS ]; +/* PMU event selector configured values for RV32*/ +target_ulong mhpmeventh_val[RV_MAX_MHPMEVENTS ]; +target_ulong sscratch;target_ulong mscratch;@@ -433,6 +438,7 @@ struct RISCVCPUConfig {bool ext_zve32f;bool ext_zve64f;bool ext_zmmul; +bool ext_sscofpmf;bool rvv_ta_all_1s;uint32_t mvendorid;@@ -479,6 +485,12 @@ struct ArchCPU {/* Configuration Settings */ RISCVCPUConfig cfg; + +QEMUTimer *pmu_timer; +/* A bitmask of Available programmable counters */ +uint32_t pmu_avail_ctrs; +/* Mapping of events to counters */ +GHashTable *pmu_event_ctr_map; };static inline int riscv_has_ext (CPURISCVState *env,target_ulong ext)@@ -738,6 +750,19 @@ enum { CSR_TABLE_SIZE = 0x1000 }; +/** +* The event id are encoded based on the encoding specified in the +* SBI specification v0.3 +*/ + +enum riscv_pmu_event_idx { +RISCV_PMU_EVENT_HW_CPU_CYCLES =0x01 , +RISCV_PMU_EVENT_HW_INSTRUCTIONS =0x02 , +RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS =0x10019 , +RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS =0x1001B , +RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS =0x10021 , +}; +/* CSR function table */ extern riscv_csr_operations csr_ops[CSR_TABLE_SIZE ];target/riscv/cpu_bits.h +55
-0@@ -400,6 +400,37 @@#define CSR_MHPMEVENT29 0x33d#define CSR_MHPMEVENT30 0x33e#define CSR_MHPMEVENT31 0x33f + +#define CSR_MHPMEVENT3H 0x723 +#define CSR_MHPMEVENT4H 0x724 +#define CSR_MHPMEVENT5H 0x725 +#define CSR_MHPMEVENT6H 0x726 +#define CSR_MHPMEVENT7H 0x727 +#define CSR_MHPMEVENT8H 0x728 +#define CSR_MHPMEVENT9H 0x729 +#define CSR_MHPMEVENT10H 0x72a +#define CSR_MHPMEVENT11H 0x72b +#define CSR_MHPMEVENT12H 0x72c +#define CSR_MHPMEVENT13H 0x72d +#define CSR_MHPMEVENT14H 0x72e +#define CSR_MHPMEVENT15H 0x72f +#define CSR_MHPMEVENT16H 0x730 +#define CSR_MHPMEVENT17H 0x731 +#define CSR_MHPMEVENT18H 0x732 +#define CSR_MHPMEVENT19H 0x733 +#define CSR_MHPMEVENT20H 0x734 +#define CSR_MHPMEVENT21H 0x735 +#define CSR_MHPMEVENT22H 0x736 +#define CSR_MHPMEVENT23H 0x737 +#define CSR_MHPMEVENT24H 0x738 +#define CSR_MHPMEVENT25H 0x739 +#define CSR_MHPMEVENT26H 0x73a +#define CSR_MHPMEVENT27H 0x73b +#define CSR_MHPMEVENT28H 0x73c +#define CSR_MHPMEVENT29H 0x73d +#define CSR_MHPMEVENT30H 0x73e +#define CSR_MHPMEVENT31H 0x73f +#define CSR_MHPMCOUNTER3H 0xb83#define CSR_MHPMCOUNTER4H 0xb84#define CSR_MHPMCOUNTER5H 0xb85@@ -461,6 +492,7 @@#define CSR_VSMTE 0x2c0#define CSR_VSPMMASK 0x2c1#define CSR_VSPMBASE 0x2c2 +#define CSR_SCOUNTOVF 0xda0/* Crypto Extension */ #define CSR_SEED 0x015@@ -638,6 +670,7 @@ typedef enum RISCVException {#define IRQ_VS_EXT 10#define IRQ_M_EXT 11#define IRQ_S_GEXT 12 +#define IRQ_PMU_OVF 13#define IRQ_LOCAL_MAX 16#define IRQ_LOCAL_GUEST_MAX (TARGET_LONG_BITS - 1)@@ -655,11 +688,13 @@ typedef enum RISCVException {#define MIP_VSEIP (1 << IRQ_VS_EXT)#define MIP_MEIP (1 << IRQ_M_EXT)#define MIP_SGEIP (1 << IRQ_S_GEXT) +#define MIP_LCOFIP (1 << IRQ_PMU_OVF)/* sip masks */ #define SIP_SSIP MIP_SSIP#define SIP_STIP MIP_STIP#define SIP_SEIP MIP_SEIP +#define SIP_LCOFIP MIP_LCOFIP/* MIE masks */ #define MIE_SEIE (1 << IRQ_S_EXT)@@ -813,4 +848,24 @@ typedef enum RISCVException {#define SEED_OPST_WAIT (0b01 << 30)#define SEED_OPST_ES16 (0b10 << 30)#define SEED_OPST_DEAD (0b11 << 30) +/* PMU related bits */ +#define MIE_LCOFIE (1 << IRQ_PMU_OVF) + +#define MHPMEVENT_BIT_OF BIT_ULL(63) +#define MHPMEVENTH_BIT_OF BIT(31) +#define MHPMEVENT_BIT_MINH BIT_ULL(62) +#define MHPMEVENTH_BIT_MINH BIT(30) +#define MHPMEVENT_BIT_SINH BIT_ULL(61) +#define MHPMEVENTH_BIT_SINH BIT(29) +#define MHPMEVENT_BIT_UINH BIT_ULL(60) +#define MHPMEVENTH_BIT_UINH BIT(28) +#define MHPMEVENT_BIT_VSINH BIT_ULL(59) +#define MHPMEVENTH_BIT_VSINH BIT(27) +#define MHPMEVENT_BIT_VUINH BIT_ULL(58) +#define MHPMEVENTH_BIT_VUINH BIT(26) + +#define MHPMEVENT_SSCOF_MASK _ULL(0xFFFF000000000000) +#define MHPMEVENT_IDX_MASK 0xFFFFF +#define MHPMEVENT_SSCOF_RESVD 16 +#endif target/riscv/csr.c +93
-8@@ -74,7 +74,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno)CPUState *cs =env_cpu (env);RISCVCPU *cpu =RISCV_CPU (cs);int ctr_index; -int base_csrno =CSR_HPMCOUNTER3 ; +int base_csrno =CSR_CYCLE ;bool rv32 =riscv_cpu_mxl (env) ==MXL_RV32 ? true : false;if (rv32 && csrno >=CSR_CYCLEH ) {@@ -83,11 +83,18 @@ static RISCVException ctr(CPURISCVState *env, int csrno) } ctr_index = csrno - base_csrno; -if (!cpu->cfg.pmu_num || ctr_index >= (cpu->cfg.pmu_num)) { +if ((csrno >=CSR_CYCLE && csrno <=CSR_INSTRET ) || + (csrno >=CSR_CYCLEH && csrno <=CSR_INSTRETH )) { + goto skip_ext_pmu_check; + } + +if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs &BIT (ctr_index)))) {/* No counter is enabled in PMU or the counter is out of range */ return RISCV_EXCP_ILLEGAL_INST ; } +skip_ext_pmu_check: +if (env->priv ==PRV_S ) {switch (csrno) {case CSR_CYCLE :@@ -106,7 +113,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: - ctr_index = csrno -CSR_CYCLE ;if (!get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_ILLEGAL_INST ; }@@ -130,7 +136,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: - ctr_index = csrno -CSR_CYCLEH ;if (!get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_ILLEGAL_INST ; }@@ -160,7 +165,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: - ctr_index = csrno -CSR_CYCLE ;if (!get_field (env->hcounteren,1 << ctr_index) &&get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ;@@ -188,7 +192,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: - ctr_index = csrno -CSR_CYCLEH ;if (!get_field (env->hcounteren,1 << ctr_index) &&get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ;@@ -240,6 +243,18 @@ static RISCVException mctr32(CPURISCVState *env, int csrno)return mctr (env, csrno); } +static RISCVException sscofpmf (CPURISCVState *env,int csrno) +{ +CPUState *cs =env_cpu (env); +RISCVCPU *cpu =RISCV_CPU (cs); + +if (!cpu->cfg.ext_sscofpmf) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return RISCV_EXCP_NONE ; +} +static RISCVException any (CPURISCVState *env,int csrno) {return RISCV_EXCP_NONE ;@@ -663,9 +678,38 @@ static int read_mhpmevent(CPURISCVState *env, int csrno, target_ulong *val)static int write_mhpmevent (CPURISCVState *env,int csrno,target_ulong val) {int evt_index = csrno -CSR_MCOUNTINHIBIT ; +uint64_t mhpmevt_val = val; env->mhpmevent_val[evt_index] = val; +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpmevt_val = mhpmevt_val | ((uint64_t )env->mhpmeventh_val[evt_index] <<32 ); + } +riscv_pmu_update_event_map (env, mhpmevt_val, evt_index); + +return RISCV_EXCP_NONE ; +} + +static int read_mhpmeventh (CPURISCVState *env,int csrno,target_ulong *val) +{ +int evt_index = csrno -CSR_MHPMEVENT3H +3 ; + + *val = env->mhpmeventh_val[evt_index]; + +return RISCV_EXCP_NONE ; +} + +static int write_mhpmeventh (CPURISCVState *env,int csrno,target_ulong val) +{ +int evt_index = csrno -CSR_MHPMEVENT3H +3 ; +uint64_t mhpmevth_val = val; +uint64_t mhpmevt_val = env->mhpmevent_val[evt_index]; + + mhpmevt_val = mhpmevt_val | (mhpmevth_val <<32 ); + env->mhpmeventh_val[evt_index] = val; + +riscv_pmu_update_event_map (env, mhpmevt_val, evt_index); +return RISCV_EXCP_NONE ; }@@ -673,12 +717,20 @@ static int write_mhpmcounter(CPURISCVState *env, int csrno, target_ulong val) {int ctr_idx = csrno -CSR_MCYCLE ;PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +uint64_t mhpmctr_val = val; counter->mhpmcounter_val = val;if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) ||riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { counter->mhpmcounter_prev =get_ticks (false); - }else { +if (ctr_idx >2 ) { +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpmctr_val = mhpmctr_val | + ((uint64_t )counter->mhpmcounterh_val <<32 ); + } +riscv_pmu_setup_timer (env, mhpmctr_val, ctr_idx); + } + }else {/* Other counters can keep incrementing from the given value */ counter->mhpmcounter_prev = val; }@@ -690,11 +742,17 @@ static int write_mhpmcounterh(CPURISCVState *env, int csrno, target_ulong val) {int ctr_idx = csrno -CSR_MCYCLEH ;PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +uint64_t mhpmctr_val = counter->mhpmcounter_val; +uint64_t mhpmctrh_val = val; counter->mhpmcounterh_val = val; + mhpmctr_val = mhpmctr_val | (mhpmctrh_val <<32 );if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) ||riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { counter->mhpmcounterh_prev =get_ticks (true); +if (ctr_idx >2 ) { +riscv_pmu_setup_timer (env, mhpmctr_val, ctr_idx); + } }else { counter->mhpmcounterh_prev = val; }@@ -770,6 +828,32 @@ static int read_hpmcounterh(CPURISCVState *env, int csrno, target_ulong *val)return riscv_pmu_read_ctr (env, val, true, ctr_index); } +static int read_scountovf (CPURISCVState *env,int csrno,target_ulong *val) +{ +int mhpmevt_start =CSR_MHPMEVENT3 -CSR_MCOUNTINHIBIT ; +int i; + *val =0 ; +target_ulong *mhpm_evt_val; +uint64_t of_bit_mask; + +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpm_evt_val = env->mhpmeventh_val; + of_bit_mask =MHPMEVENTH_BIT_OF ; + }else { + mhpm_evt_val = env->mhpmevent_val; + of_bit_mask =MHPMEVENT_BIT_OF ; + } + +for (i = mhpmevt_start; i <RV_MAX_MHPMEVENTS ; i++) { +if ((get_field (env->mcounteren,BIT (i))) && + (mhpm_evt_val[i] & of_bit_mask)) { + *val |=BIT (i); + } + } + + return RISCV_EXCP_NONE;o +} +static RISCVException read_time (CPURISCVState *env,int csrno,target_ulong *val) {@@ -799,7 +883,8 @@ static RISCVException read_timeh(CPURISCVState *env, int csrno,/* Machine constants */ #define M_MODE_INTERRUPTS ((uint64_t)(MIP_MSIP | MIP_MTIP | MIP_MEIP)) -#define S_MODE_INTERRUPTS ((uint64_t)(MIP_SSIP | MIP_STIP | MIP_SEIP)) +#define S_MODE_INTERRUPTS ((uint64_t)(MIP_SSIP | MIP_STIP | MIP_SEIP | \ + MIP_LCOFIP))It seems a problem here.
S_MODE_INTERRUPTSwill be used indelegable_ints, and then be used not
Quoted 4 lines
only in
rmw_mip64but also inrmw_mideleg64, so if we addMIP_LCOFIPhere, this bit will also addedinto mideleg which is not stated in the spec for sscofpmf.
Here is the snippet from the sscofpmf spec which says counter overflow interrupt can be delegated to S-mode.
"Generation of a "count overflow interrupt request" by an hpmcounter sets the LCOFIP bit in the
mip/sip registers and sets the associated OF bit. The mideleg register controls the delegation of
this interrupt to S-mode versus M-mode."
Quoted 494 lines
And if
MIP_LCOFIPis not a bit in mideleg, the following modification for 'sip_writable_mask' will not work.Regards,
Weiwei Li
#define
VS_MODE_INTERRUPTS((uint64_t)(MIP_VSSIP |MIP_VSTIP|MIP_VSEIP)) #defineHS_MODE_INTERRUPTS((uint64_t)(MIP_SGEIP |VS_MODE_INTERRUPTS))target/riscv/csr.c +63
-1@@ -840,7 +925,8 @@ static const target_ulong vs_delegable_excps = DELEGABLE_EXCPS &static const target_ulong sstatus_v1_10_mask =SSTATUS_SIE |SSTATUS_SPIE |SSTATUS_UIE |SSTATUS_UPIE |SSTATUS_SPP |SSTATUS_FS |SSTATUS_XS |SSTATUS_SUM |SSTATUS_MXR |SSTATUS_VS ; -static const target_ulong sip_writable_mask =SIP_SSIP |MIP_USIP |MIP_UEIP ; +static const target_ulong sip_writable_mask =SIP_SSIP |MIP_USIP |MIP_UEIP | +SIP_LCOFIP ;static const target_ulong hip_writable_mask =MIP_VSSIP ;static const target_ulong hvip_writable_mask =MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ;static const target_ulong vsip_writable_mask =MIP_VSSIP ;@@ -4005,6 +4091,65 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MHPMEVENT31] = { "mhpmevent31", any, read_mhpmevent, write_mhpmevent }, + [CSR_MHPMEVENT3H] = { "mhpmevent3h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT4H] = { "mhpmevent4h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT5H] = { "mhpmevent5h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT6H] = { "mhpmevent6h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT7H] = { "mhpmevent7h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT8H] = { "mhpmevent8h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT9H] = { "mhpmevent9h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT10H] = { "mhpmevent10h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT11H] = { "mhpmevent11h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT12H] = { "mhpmevent12h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT13H] = { "mhpmevent13h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT14H] = { "mhpmevent14h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT15H] = { "mhpmevent15h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT16H] = { "mhpmevent16h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT17H] = { "mhpmevent17h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT18H] = { "mhpmevent18h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT19H] = { "mhpmevent19h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT20H] = { "mhpmevent20h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT21H] = { "mhpmevent21h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT22H] = { "mhpmevent22h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT23H] = { "mhpmevent23h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT24H] = { "mhpmevent24h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT25H] = { "mhpmevent25h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT26H] = { "mhpmevent26h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT27H] = { "mhpmevent27h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT28H] = { "mhpmevent28h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT29H] = { "mhpmevent29h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT30H] = { "mhpmevent30h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT31H] = { "mhpmevent31h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_HPMCOUNTER3H] = { "hpmcounter3h", ctr32, read_hpmcounterh }, [CSR_HPMCOUNTER4H] = { "hpmcounter4h", ctr32, read_hpmcounterh }, [CSR_HPMCOUNTER5H] = { "hpmcounter5h", ctr32, read_hpmcounterh },@@ -4093,5 +4238,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { write_mhpmcounterh }, [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", mctr32, read_hpmcounterh, write_mhpmcounterh }, + [CSR_SCOUNTOVF] = { "scountovf", sscofpmf, read_scountovf }, + #endif /* !CONFIG_USER_ONLY */ };target/riscv/machine.c +1
-0@@ -355,6 +355,7 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_STRUCT_ARRAY (env.pmu_ctrs,RISCVCPU ,RV_MAX_MHPMCOUNTERS ,0 , vmstate_pmu_ctr_state, PMUCTRState),VMSTATE_UINTTL_ARRAY (env.mhpmevent_val,RISCVCPU ,RV_MAX_MHPMEVENTS ), +VMSTATE_UINTTL_ARRAY (env.mhpmeventh_val,RISCVCPU ,RV_MAX_MHPMEVENTS ),VMSTATE_UINTTL (env.sscratch,RISCVCPU ),VMSTATE_UINTTL (env.mscratch,RISCVCPU ), VMSTATE_UINT64(env.mfromhost, RISCVCPU),target/riscv/pmu.c +355
-2@@ -19,14 +19,367 @@#include "qemu/osdep.h" #include "cpu.h" #include "pmu.h" +#include "sysemu/cpu-timers.h" + +#define RISCV_TIMEBASE_FREQ 1000000000/* 1Ghz */ +#define MAKE_32BIT_MASK (shift, length) \ + (((uint32_t)(~0UL) >> (32 - (length))) << (shift)) + +static bool riscv_pmu_counter_valid (RISCVCPU *cpu,uint32_t ctr_idx) +{ +if (ctr_idx <3 || ctr_idx >=RV_MAX_MHPMCOUNTERS || + !(cpu->pmu_avail_ctrs &BIT (ctr_idx))) { +return false; + }else { +return true; + } +} + +static bool riscv_pmu_counter_enabled (RISCVCPU *cpu,uint32_t ctr_idx) +{ +CPURISCVState *env = &cpu->env; + +if (riscv_pmu_counter_valid (cpu, ctr_idx) && + !get_field (env->mcountinhibit,BIT (ctr_idx))) { +return true; + }else { +return false; + } +} + +static int riscv_pmu_incr_ctr_rv32 (RISCVCPU *cpu,uint32_t ctr_idx) +{ +CPURISCVState *env = &cpu->env; +target_ulong max_val =UINT32_MAX ; +PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +bool virt_on =riscv_cpu_virt_enabled (env); + +/* Privilege mode filtering */ +if ((env->priv ==PRV_M && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_MINH )) || + (env->priv ==PRV_S && virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_VSINH )) || + (env->priv ==PRV_U && virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_VUINH )) || + (env->priv ==PRV_S && !virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_SINH )) || + (env->priv ==PRV_U && !virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_UINH ))) { +return 0 ; + } + +/* Handle the overflow scenario */ +if (counter->mhpmcounter_val == max_val) { +if (counter->mhpmcounterh_val == max_val) { + counter->mhpmcounter_val =0 ; + counter->mhpmcounterh_val =0 ; +/* Generate interrupt only if OF bit is clear */ +if (!(env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_OF )) { + env->mhpmeventh_val[ctr_idx] |=MHPMEVENTH_BIT_OF ; +riscv_cpu_update_mip (cpu,MIP_LCOFIP ,BOOL_TO_MASK (1 )); + } + }else { + counter->mhpmcounterh_val++; + } + }else { + counter->mhpmcounter_val++; + } + +return 0 ; +} + +static int riscv_pmu_incr_ctr_rv64 (RISCVCPU *cpu,uint32_t ctr_idx) +{ +CPURISCVState *env = &cpu->env; +PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +uint64_t max_val =UINT64_MAX ; +bool virt_on =riscv_cpu_virt_enabled (env); + +/* Privilege mode filtering */ +if ((env->priv ==PRV_M && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_MINH )) || + (env->priv ==PRV_S && virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_VSINH )) || + (env->priv ==PRV_U && virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_VUINH )) || + (env->priv ==PRV_S && !virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_SINH )) || + (env->priv ==PRV_U && !virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_UINH ))) { +return 0 ; + } + +/* Handle the overflow scenario */ +if (counter->mhpmcounter_val == max_val) { + counter->mhpmcounter_val =0 ; +/* Generate interrupt only if OF bit is clear */ +if (!(env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_OF )) { + env->mhpmevent_val[ctr_idx] |=MHPMEVENT_BIT_OF ; +riscv_cpu_update_mip (cpu,MIP_LCOFIP ,BOOL_TO_MASK (1 )); + } + }else { + counter->mhpmcounter_val++; + } +return 0 ; +} + +int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx) +{ +uint32_t ctr_idx; +int ret; +CPURISCVState *env = &cpu->env; +gpointer value; + + value =g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx)); +if (!value) { +return -1 ; + } + + ctr_idx =GPOINTER_TO_UINT (value); +if (!riscv_pmu_counter_enabled (cpu, ctr_idx) || +get_field (env->mcountinhibit,BIT (ctr_idx))) { +return -1 ; + } + +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + ret =riscv_pmu_incr_ctr_rv32 (cpu, ctr_idx); + }else { + ret =riscv_pmu_incr_ctr_rv64 (cpu, ctr_idx); + } + +return ret; +}bool riscv_pmu_ctr_monitor_instructions (CPURISCVState *env,uint32_t target_ctr) { -return (target_ctr ==0 ) ? true : false; +RISCVCPU *cpu; +uint32_t event_idx; +uint32_t ctr_idx; + +/* Fixed instret counter */ +if (target_ctr ==2 ) { +return true; + } + + cpu =RISCV_CPU (env_cpu (env)); + event_idx =RISCV_PMU_EVENT_HW_INSTRUCTIONS ; + ctr_idx =GPOINTER_TO_UINT (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx))); +if (!ctr_idx) { +return false; + } + +return target_ctr == ctr_idx ? true : false; }bool riscv_pmu_ctr_monitor_cycles (CPURISCVState *env,uint32_t target_ctr) { -return (target_ctr ==2 ) ? true : false; +RISCVCPU *cpu; +uint32_t event_idx; +uint32_t ctr_idx; + +/* Fixed mcycle counter */ +if (target_ctr ==0 ) { +return true; + } + + cpu =RISCV_CPU (env_cpu (env)); + event_idx =RISCV_PMU_EVENT_HW_CPU_CYCLES ; + ctr_idx =GPOINTER_TO_UINT (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx))); + +/* Counter zero is not used for event_ctr_map */ +if (!ctr_idx) { +return false; + } + +return (target_ctr == ctr_idx) ? true : false; +} + +static gboolean pmu_remove_event_map (gpointer key,gpointer value, +gpointer udata) +{ +return (GPOINTER_TO_UINT (value) ==GPOINTER_TO_UINT (udata)) ? true : false; +} + +static int64_t pmu_icount_ticks_to_ns (int64_t value) +{ +int64_t ret =0 ; + +if (icount_enabled ()) { + ret =icount_to_ns (value); + }else { + ret = (NANOSECONDS_PER_SECOND /RISCV_TIMEBASE_FREQ ) * value; + } + +return ret; +} + +int riscv_pmu_update_event_map (CPURISCVState *env,uint64_t value, +uint32_t ctr_idx) +{ +uint32_t event_idx; +RISCVCPU *cpu =RISCV_CPU (env_cpu (env)); + +if (!riscv_pmu_counter_valid (cpu, ctr_idx)) { +return -1 ; + } + +/** +* Expected mhpmevent value is zero for reset case. Remove the current +* mapping. +*/ +if (!value) { +g_hash_table_foreach_remove (cpu->pmu_event_ctr_map, + pmu_remove_event_map, +GUINT_TO_POINTER (ctr_idx)); +return 0 ; + } + + event_idx = value &MHPMEVENT_IDX_MASK ; +if (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx))) { +return 0 ; + } + +switch (event_idx) { +case RISCV_PMU_EVENT_HW_CPU_CYCLES : +case RISCV_PMU_EVENT_HW_INSTRUCTIONS : +case RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS : +case RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS : +case RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS : +break ; +default : +/* We don't support any raw events right now */ +return -1 ; + } +g_hash_table_insert (cpu->pmu_event_ctr_map,GUINT_TO_POINTER (event_idx), +GUINT_TO_POINTER (ctr_idx)); + +return 0 ; +} + +static void pmu_timer_trigger_irq (RISCVCPU *cpu, +enum riscv_pmu_event_idx evt_idx) +{ +uint32_t ctr_idx; +CPURISCVState *env = &cpu->env; +PMUCTRState *counter; +target_ulong *mhpmevent_val; +uint64_t of_bit_mask; +int64_t irq_trigger_at; + +if (evt_idx !=RISCV_PMU_EVENT_HW_CPU_CYCLES && + evt_idx !=RISCV_PMU_EVENT_HW_INSTRUCTIONS ) { +return ; + } + + ctr_idx =GPOINTER_TO_UINT (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (evt_idx))); +if (!riscv_pmu_counter_enabled (cpu, ctr_idx)) { +return ; + } + +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpmevent_val = &env->mhpmeventh_val[ctr_idx]; + of_bit_mask =MHPMEVENTH_BIT_OF ; + }else { + mhpmevent_val = &env->mhpmevent_val[ctr_idx]; + of_bit_mask =MHPMEVENT_BIT_OF ; + } + + counter = &env->pmu_ctrs[ctr_idx]; +if (counter->irq_overflow_left >0 ) { + irq_trigger_at =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + + counter->irq_overflow_left; +timer_mod_anticipate_ns (cpu->pmu_timer, irq_trigger_at); + counter->irq_overflow_left =0 ; +return ; + } + +if (cpu->pmu_avail_ctrs &BIT (ctr_idx)) { +/* Generate interrupt only if OF bit is clear */ +if (!(*mhpmevent_val & of_bit_mask)) { + *mhpmevent_val |= of_bit_mask; +riscv_cpu_update_mip (cpu,MIP_LCOFIP ,BOOL_TO_MASK (1 )); + } + } +} + +/* Timer callback for instret and cycle counter overflow */ +void riscv_pmu_timer_cb (void *priv) +{ +RISCVCPU *cpu = priv; + +/* Timer event was triggered only for these events */ +pmu_timer_trigger_irq (cpu,RISCV_PMU_EVENT_HW_CPU_CYCLES ); +pmu_timer_trigger_irq (cpu,RISCV_PMU_EVENT_HW_INSTRUCTIONS ); +} + +int riscv_pmu_setup_timer (CPURISCVState *env,uint64_t value,uint32_t ctr_idx) +{ +uint64_t overflow_delta, overflow_at; +int64_t overflow_ns, overflow_left =0 ; +RISCVCPU *cpu =RISCV_CPU (env_cpu (env)); +PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; + +if (!riscv_pmu_counter_valid (cpu, ctr_idx) || !cpu->cfg.ext_sscofpmf) { +return -1 ; + } + +if (value) { + overflow_delta =UINT64_MAX - value +1 ; + }else { + overflow_delta =UINT64_MAX ; + } + +/** +* QEMU supports only int64_t timers while RISC-V counters are uint64_t. +* Compute the leftover and save it so that it can be reprogrammed again +* when timer expires. +*/ +if (overflow_delta >INT64_MAX ) { + overflow_left = overflow_delta -INT64_MAX ; + } + +if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) || +riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { + overflow_ns =pmu_icount_ticks_to_ns ((int64_t )overflow_delta); + overflow_left =pmu_icount_ticks_to_ns (overflow_left) ; + }else { +return -1 ; + } + overflow_at = (uint64_t )qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + overflow_ns; + +if (overflow_at >INT64_MAX ) { + overflow_left += overflow_at -INT64_MAX ; + counter->irq_overflow_left = overflow_left; + overflow_at =INT64_MAX ; + } +timer_mod_anticipate_ns (cpu->pmu_timer, overflow_at); + +return 0 ; +} + + +int riscv_pmu_init (RISCVCPU *cpu,int num_counters) +{ +if (num_counters > (RV_MAX_MHPMCOUNTERS -3 )) { +return -1 ; + } + + cpu->pmu_event_ctr_map =g_hash_table_new (g_direct_hash, g_direct_equal); +if (!cpu->pmu_event_ctr_map) { +/* PMU support can not be enabled */ +qemu_log_mask (LOG_UNIMP ,"PMU events can't be supported\n" ); + cpu->cfg.pmu_num =0 ; +return -1 ; + } + +/* Create a bitmask of available programmable counters */ + cpu->pmu_avail_ctrs =MAKE_32BIT_MASK (3 , num_counters); + +return 0 ; }target/riscv/pmu.h +7
-0@@ -26,3 +26,10 @@ bool riscv_pmu_ctr_monitor_instructions(CPURISCVState *env, uint32_t target_ctr);bool riscv_pmu_ctr_monitor_cycles (CPURISCVState *env,uint32_t target_ctr); +void riscv_pmu_timer_cb (void *priv); +int riscv_pmu_init (RISCVCPU *cpu,int num_counters); +int riscv_pmu_update_event_map (CPURISCVState *env,uint64_t value, +uint32_t ctr_idx); +int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx); +int riscv_pmu_setup_timer (CPURISCVState *env,uint64_t value, +uint32_t ctr_idx);
Re: [PATCH v10 08/12] target/riscv: Add sscofpmf extension support
Quoted 436 lines · Atish Kumar Patra
Quoted 428 lines · Weiwei Li
在 2022/6/21 上午7:15, Atish Patra 写道:
The Sscofpmf ('Ss' for Privileged arch and Supervisor-level extensions, and 'cofpmf' for Count OverFlow and Privilege Mode Filtering) extension allows the perf to handle overflow interrupts and filtering support. This patch provides a framework for programmable counters to leverage the extension. As the extension doesn't have any provision for the overflow bit for fixed counters, the fixed events can also be monitoring using programmable counters. The underlying counters for cycle and instruction counters are always running. Thus, a separate timer device is programmed to handle the overflow.
Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>--- target/riscv/cpu.c | 11 ++ target/riscv/cpu.h | 25 +++ target/riscv/cpu_bits.h | 55 +++++++ target/riscv/csr.c | 165 ++++++++++++++++++- target/riscv/machine.c | 1 + target/riscv/pmu.c | 357 +++++++++++++++++++++++++++++++++++++++- target/riscv/pmu.h | 7 + 7 files changed, 610 insertions(+), 11 deletions(-)target/riscv/cpu.c +11
-0@@ -22,6 +22,7 @@#include "qemu/ctype.h" #include "qemu/log.h" #include "cpu.h" +#include "pmu.h" #include "internals.h" #include "exec/exec-all.h" #include "qapi/error.h" @@ -775,6 +776,15 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)set_misa (env, env->misa_mxl, ext); } +#ifndef CONFIG_USER_ONLY +if (cpu->cfg.pmu_num) { +if (!riscv_pmu_init (cpu, cpu->cfg.pmu_num) && cpu->cfg.ext_sscofpmf) { + cpu->pmu_timer =timer_new_ns (QEMU_CLOCK_VIRTUAL , + riscv_pmu_timer_cb, cpu); + } + } +#endif +riscv_cpu_register_gdb_regs_for_features (cs);qemu_init_vcpu (cs);@@ -879,6 +889,7 @@ static Property riscv_cpu_extensions[] = {DEFINE_PROP_BOOL ("v" ,RISCVCPU , cfg.ext_v, false),DEFINE_PROP_BOOL ("h" ,RISCVCPU , cfg.ext_h, true),DEFINE_PROP_UINT8 ("pmu-num" ,RISCVCPU , cfg.pmu_num,16 ), +DEFINE_PROP_BOOL ("sscofpmf" ,RISCVCPU , cfg.ext_sscofpmf, false),DEFINE_PROP_BOOL ("Zifencei" ,RISCVCPU , cfg.ext_ifencei, true),DEFINE_PROP_BOOL ("Zicsr" ,RISCVCPU , cfg.ext_icsr, true), DEFINE_PROP_BOOL("Zfh", RISCVCPU, cfg.ext_zfh, false),target/riscv/cpu.h +25
-0@@ -137,6 +137,8 @@ typedef struct PMUCTRState {/* Snapshort value of a counter in RV32 */ target_ulong mhpmcounterh_prev;bool started; +/* Value beyond UINT32_MAX/UINT64_MAX before overflow interrupt trigger */ +target_ulong irq_overflow_left; } PMUCTRState;struct CPUArchState {@@ -297,6 +299,9 @@ struct CPUArchState {/* PMU event selector configured values. First three are unused*/ target_ulong mhpmevent_val[RV_MAX_MHPMEVENTS ]; +/* PMU event selector configured values for RV32*/ +target_ulong mhpmeventh_val[RV_MAX_MHPMEVENTS ]; +target_ulong sscratch;target_ulong mscratch;@@ -433,6 +438,7 @@ struct RISCVCPUConfig {bool ext_zve32f;bool ext_zve64f;bool ext_zmmul; +bool ext_sscofpmf;bool rvv_ta_all_1s;uint32_t mvendorid;@@ -479,6 +485,12 @@ struct ArchCPU {/* Configuration Settings */ RISCVCPUConfig cfg; + +QEMUTimer *pmu_timer; +/* A bitmask of Available programmable counters */ +uint32_t pmu_avail_ctrs; +/* Mapping of events to counters */ +GHashTable *pmu_event_ctr_map; };static inline int riscv_has_ext (CPURISCVState *env,target_ulong ext)@@ -738,6 +750,19 @@ enum { CSR_TABLE_SIZE = 0x1000 }; +/** +* The event id are encoded based on the encoding specified in the +* SBI specification v0.3 +*/ + +enum riscv_pmu_event_idx { +RISCV_PMU_EVENT_HW_CPU_CYCLES =0x01 , +RISCV_PMU_EVENT_HW_INSTRUCTIONS =0x02 , +RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS =0x10019 , +RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS =0x1001B , +RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS =0x10021 , +}; +/* CSR function table */ extern riscv_csr_operations csr_ops[CSR_TABLE_SIZE ];target/riscv/cpu_bits.h +55
-0@@ -400,6 +400,37 @@#define CSR_MHPMEVENT29 0x33d#define CSR_MHPMEVENT30 0x33e#define CSR_MHPMEVENT31 0x33f + +#define CSR_MHPMEVENT3H 0x723 +#define CSR_MHPMEVENT4H 0x724 +#define CSR_MHPMEVENT5H 0x725 +#define CSR_MHPMEVENT6H 0x726 +#define CSR_MHPMEVENT7H 0x727 +#define CSR_MHPMEVENT8H 0x728 +#define CSR_MHPMEVENT9H 0x729 +#define CSR_MHPMEVENT10H 0x72a +#define CSR_MHPMEVENT11H 0x72b +#define CSR_MHPMEVENT12H 0x72c +#define CSR_MHPMEVENT13H 0x72d +#define CSR_MHPMEVENT14H 0x72e +#define CSR_MHPMEVENT15H 0x72f +#define CSR_MHPMEVENT16H 0x730 +#define CSR_MHPMEVENT17H 0x731 +#define CSR_MHPMEVENT18H 0x732 +#define CSR_MHPMEVENT19H 0x733 +#define CSR_MHPMEVENT20H 0x734 +#define CSR_MHPMEVENT21H 0x735 +#define CSR_MHPMEVENT22H 0x736 +#define CSR_MHPMEVENT23H 0x737 +#define CSR_MHPMEVENT24H 0x738 +#define CSR_MHPMEVENT25H 0x739 +#define CSR_MHPMEVENT26H 0x73a +#define CSR_MHPMEVENT27H 0x73b +#define CSR_MHPMEVENT28H 0x73c +#define CSR_MHPMEVENT29H 0x73d +#define CSR_MHPMEVENT30H 0x73e +#define CSR_MHPMEVENT31H 0x73f +#define CSR_MHPMCOUNTER3H 0xb83#define CSR_MHPMCOUNTER4H 0xb84#define CSR_MHPMCOUNTER5H 0xb85@@ -461,6 +492,7 @@#define CSR_VSMTE 0x2c0#define CSR_VSPMMASK 0x2c1#define CSR_VSPMBASE 0x2c2 +#define CSR_SCOUNTOVF 0xda0/* Crypto Extension */ #define CSR_SEED 0x015@@ -638,6 +670,7 @@ typedef enum RISCVException {#define IRQ_VS_EXT 10#define IRQ_M_EXT 11#define IRQ_S_GEXT 12 +#define IRQ_PMU_OVF 13#define IRQ_LOCAL_MAX 16#define IRQ_LOCAL_GUEST_MAX (TARGET_LONG_BITS - 1)@@ -655,11 +688,13 @@ typedef enum RISCVException {#define MIP_VSEIP (1 << IRQ_VS_EXT)#define MIP_MEIP (1 << IRQ_M_EXT)#define MIP_SGEIP (1 << IRQ_S_GEXT) +#define MIP_LCOFIP (1 << IRQ_PMU_OVF)/* sip masks */ #define SIP_SSIP MIP_SSIP#define SIP_STIP MIP_STIP#define SIP_SEIP MIP_SEIP +#define SIP_LCOFIP MIP_LCOFIP/* MIE masks */ #define MIE_SEIE (1 << IRQ_S_EXT)@@ -813,4 +848,24 @@ typedef enum RISCVException {#define SEED_OPST_WAIT (0b01 << 30)#define SEED_OPST_ES16 (0b10 << 30)#define SEED_OPST_DEAD (0b11 << 30) +/* PMU related bits */ +#define MIE_LCOFIE (1 << IRQ_PMU_OVF) + +#define MHPMEVENT_BIT_OF BIT_ULL(63) +#define MHPMEVENTH_BIT_OF BIT(31) +#define MHPMEVENT_BIT_MINH BIT_ULL(62) +#define MHPMEVENTH_BIT_MINH BIT(30) +#define MHPMEVENT_BIT_SINH BIT_ULL(61) +#define MHPMEVENTH_BIT_SINH BIT(29) +#define MHPMEVENT_BIT_UINH BIT_ULL(60) +#define MHPMEVENTH_BIT_UINH BIT(28) +#define MHPMEVENT_BIT_VSINH BIT_ULL(59) +#define MHPMEVENTH_BIT_VSINH BIT(27) +#define MHPMEVENT_BIT_VUINH BIT_ULL(58) +#define MHPMEVENTH_BIT_VUINH BIT(26) + +#define MHPMEVENT_SSCOF_MASK _ULL(0xFFFF000000000000) +#define MHPMEVENT_IDX_MASK 0xFFFFF +#define MHPMEVENT_SSCOF_RESVD 16 +#endif target/riscv/csr.c +93
-8@@ -74,7 +74,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno)CPUState *cs =env_cpu (env);RISCVCPU *cpu =RISCV_CPU (cs);int ctr_index; -int base_csrno =CSR_HPMCOUNTER3 ; +int base_csrno =CSR_CYCLE ;bool rv32 =riscv_cpu_mxl (env) ==MXL_RV32 ? true : false;if (rv32 && csrno >=CSR_CYCLEH ) {@@ -83,11 +83,18 @@ static RISCVException ctr(CPURISCVState *env, int csrno) } ctr_index = csrno - base_csrno; -if (!cpu->cfg.pmu_num || ctr_index >= (cpu->cfg.pmu_num)) { +if ((csrno >=CSR_CYCLE && csrno <=CSR_INSTRET ) || + (csrno >=CSR_CYCLEH && csrno <=CSR_INSTRETH )) { + goto skip_ext_pmu_check; + } + +if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs &BIT (ctr_index)))) {/* No counter is enabled in PMU or the counter is out of range */ return RISCV_EXCP_ILLEGAL_INST ; } +skip_ext_pmu_check: +if (env->priv ==PRV_S ) {switch (csrno) {case CSR_CYCLE :@@ -106,7 +113,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: - ctr_index = csrno -CSR_CYCLE ;if (!get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_ILLEGAL_INST ; }@@ -130,7 +136,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: - ctr_index = csrno -CSR_CYCLEH ;if (!get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_ILLEGAL_INST ; }@@ -160,7 +165,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: - ctr_index = csrno -CSR_CYCLE ;if (!get_field (env->hcounteren,1 << ctr_index) &&get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ;@@ -188,7 +192,6 @@ static RISCVException ctr(CPURISCVState *env, int csrno) }break ; case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: - ctr_index = csrno -CSR_CYCLEH ;if (!get_field (env->hcounteren,1 << ctr_index) &&get_field (env->mcounteren,1 << ctr_index)) {return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ;@@ -240,6 +243,18 @@ static RISCVException mctr32(CPURISCVState *env, int csrno)return mctr (env, csrno); } +static RISCVException sscofpmf (CPURISCVState *env,int csrno) +{ +CPUState *cs =env_cpu (env); +RISCVCPU *cpu =RISCV_CPU (cs); + +if (!cpu->cfg.ext_sscofpmf) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +return RISCV_EXCP_NONE ; +} +static RISCVException any (CPURISCVState *env,int csrno) {return RISCV_EXCP_NONE ;@@ -663,9 +678,38 @@ static int read_mhpmevent(CPURISCVState *env, int csrno, target_ulong *val)static int write_mhpmevent (CPURISCVState *env,int csrno,target_ulong val) {int evt_index = csrno -CSR_MCOUNTINHIBIT ; +uint64_t mhpmevt_val = val; env->mhpmevent_val[evt_index] = val; +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpmevt_val = mhpmevt_val | ((uint64_t )env->mhpmeventh_val[evt_index] <<32 ); + } +riscv_pmu_update_event_map (env, mhpmevt_val, evt_index); + +return RISCV_EXCP_NONE ; +} + +static int read_mhpmeventh (CPURISCVState *env,int csrno,target_ulong *val) +{ +int evt_index = csrno -CSR_MHPMEVENT3H +3 ; + + *val = env->mhpmeventh_val[evt_index]; + +return RISCV_EXCP_NONE ; +} + +static int write_mhpmeventh (CPURISCVState *env,int csrno,target_ulong val) +{ +int evt_index = csrno -CSR_MHPMEVENT3H +3 ; +uint64_t mhpmevth_val = val; +uint64_t mhpmevt_val = env->mhpmevent_val[evt_index]; + + mhpmevt_val = mhpmevt_val | (mhpmevth_val <<32 ); + env->mhpmeventh_val[evt_index] = val; + +riscv_pmu_update_event_map (env, mhpmevt_val, evt_index); +return RISCV_EXCP_NONE ; }@@ -673,12 +717,20 @@ static int write_mhpmcounter(CPURISCVState *env, int csrno, target_ulong val) {int ctr_idx = csrno -CSR_MCYCLE ;PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +uint64_t mhpmctr_val = val; counter->mhpmcounter_val = val;if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) ||riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { counter->mhpmcounter_prev =get_ticks (false); - }else { +if (ctr_idx >2 ) { +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpmctr_val = mhpmctr_val | + ((uint64_t )counter->mhpmcounterh_val <<32 ); + } +riscv_pmu_setup_timer (env, mhpmctr_val, ctr_idx); + } + }else {/* Other counters can keep incrementing from the given value */ counter->mhpmcounter_prev = val; }@@ -690,11 +742,17 @@ static int write_mhpmcounterh(CPURISCVState *env, int csrno, target_ulong val) {int ctr_idx = csrno -CSR_MCYCLEH ;PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +uint64_t mhpmctr_val = counter->mhpmcounter_val; +uint64_t mhpmctrh_val = val; counter->mhpmcounterh_val = val; + mhpmctr_val = mhpmctr_val | (mhpmctrh_val <<32 );if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) ||riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { counter->mhpmcounterh_prev =get_ticks (true); +if (ctr_idx >2 ) { +riscv_pmu_setup_timer (env, mhpmctr_val, ctr_idx); + } }else { counter->mhpmcounterh_prev = val; }@@ -770,6 +828,32 @@ static int read_hpmcounterh(CPURISCVState *env, int csrno, target_ulong *val)return riscv_pmu_read_ctr (env, val, true, ctr_index); } +static int read_scountovf (CPURISCVState *env,int csrno,target_ulong *val) +{ +int mhpmevt_start =CSR_MHPMEVENT3 -CSR_MCOUNTINHIBIT ; +int i; + *val =0 ; +target_ulong *mhpm_evt_val; +uint64_t of_bit_mask; + +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpm_evt_val = env->mhpmeventh_val; + of_bit_mask =MHPMEVENTH_BIT_OF ; + }else { + mhpm_evt_val = env->mhpmevent_val; + of_bit_mask =MHPMEVENT_BIT_OF ; + } + +for (i = mhpmevt_start; i <RV_MAX_MHPMEVENTS ; i++) { +if ((get_field (env->mcounteren,BIT (i))) && + (mhpm_evt_val[i] & of_bit_mask)) { + *val |=BIT (i); + } + } + + return RISCV_EXCP_NONE;o +} +static RISCVException read_time (CPURISCVState *env,int csrno,target_ulong *val) {@@ -799,7 +883,8 @@ static RISCVException read_timeh(CPURISCVState *env, int csrno,/* Machine constants */ #define M_MODE_INTERRUPTS ((uint64_t)(MIP_MSIP | MIP_MTIP | MIP_MEIP)) -#define S_MODE_INTERRUPTS ((uint64_t)(MIP_SSIP | MIP_STIP | MIP_SEIP)) +#define S_MODE_INTERRUPTS ((uint64_t)(MIP_SSIP | MIP_STIP | MIP_SEIP | \ + MIP_LCOFIP))It seems a problem here.
S_MODE_INTERRUPTSwill be used indelegable_ints, and then be used notQuoted 4 lines
only in
rmw_mip64but also inrmw_mideleg64, so if we addMIP_LCOFIPhere, this bit will also addedinto mideleg which is not stated in the spec for sscofpmf.
Here is the snippet from the sscofpmf spec which says counter overflow interrupt can be delegated to S-mode.
Quoted 5 lines
"Generation of a "count overflow interrupt request" by an hpmcounter sets the LCOFIP bit in the
mip/sip registers and sets the associated OF bit. The mideleg register controls the delegation of
this interrupt to S-mode versus M-mode."
OK. Thanks.
Regards,
Weiwei Li
Quoted 494 lines
Quoted 494 lines
And if
MIP_LCOFIPis not a bit in mideleg, the following modification for 'sip_writable_mask' will not work.Regards,
Weiwei Li
#define VS_MODE_INTERRUPTS ((uint64_t)(MIP_VSSIP | MIP_VSTIP | MIP_VSEIP)) #define HS_MODE_INTERRUPTS ((uint64_t)(MIP_SGEIP | VS_MODE_INTERRUPTS))target/riscv/csr.c +63
-1@@ -840,7 +925,8 @@ static const target_ulong vs_delegable_excps = DELEGABLE_EXCPS &static const target_ulong sstatus_v1_10_mask =SSTATUS_SIE |SSTATUS_SPIE |SSTATUS_UIE |SSTATUS_UPIE |SSTATUS_SPP |SSTATUS_FS |SSTATUS_XS |SSTATUS_SUM |SSTATUS_MXR |SSTATUS_VS ; -static const target_ulong sip_writable_mask =SIP_SSIP |MIP_USIP |MIP_UEIP ; +static const target_ulong sip_writable_mask =SIP_SSIP |MIP_USIP |MIP_UEIP | +SIP_LCOFIP ;static const target_ulong hip_writable_mask =MIP_VSSIP ;static const target_ulong hvip_writable_mask =MIP_VSSIP |MIP_VSTIP |MIP_VSEIP ;static const target_ulong vsip_writable_mask =MIP_VSSIP ;@@ -4005,6 +4091,65 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { [CSR_MHPMEVENT31] = { "mhpmevent31", any, read_mhpmevent, write_mhpmevent }, + [CSR_MHPMEVENT3H] = { "mhpmevent3h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT4H] = { "mhpmevent4h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT5H] = { "mhpmevent5h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT6H] = { "mhpmevent6h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT7H] = { "mhpmevent7h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT8H] = { "mhpmevent8h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT9H] = { "mhpmevent9h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT10H] = { "mhpmevent10h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT11H] = { "mhpmevent11h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT12H] = { "mhpmevent12h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT13H] = { "mhpmevent13h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT14H] = { "mhpmevent14h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT15H] = { "mhpmevent15h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT16H] = { "mhpmevent16h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT17H] = { "mhpmevent17h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT18H] = { "mhpmevent18h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT19H] = { "mhpmevent19h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT20H] = { "mhpmevent20h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT21H] = { "mhpmevent21h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT22H] = { "mhpmevent22h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT23H] = { "mhpmevent23h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT24H] = { "mhpmevent24h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT25H] = { "mhpmevent25h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT26H] = { "mhpmevent26h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT27H] = { "mhpmevent27h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT28H] = { "mhpmevent28h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT29H] = { "mhpmevent29h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT30H] = { "mhpmevent30h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_MHPMEVENT31H] = { "mhpmevent31h", sscofpmf, read_mhpmeventh, + write_mhpmeventh}, + [CSR_HPMCOUNTER3H] = { "hpmcounter3h", ctr32, read_hpmcounterh }, [CSR_HPMCOUNTER4H] = { "hpmcounter4h", ctr32, read_hpmcounterh }, [CSR_HPMCOUNTER5H] = { "hpmcounter5h", ctr32, read_hpmcounterh },@@ -4093,5 +4238,7 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { write_mhpmcounterh }, [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", mctr32, read_hpmcounterh, write_mhpmcounterh }, + [CSR_SCOUNTOVF] = { "scountovf", sscofpmf, read_scountovf }, + #endif /* !CONFIG_USER_ONLY */ };target/riscv/machine.c +1
-0@@ -355,6 +355,7 @@ const VMStateDescription vmstate_riscv_cpu = {VMSTATE_STRUCT_ARRAY (env.pmu_ctrs,RISCVCPU ,RV_MAX_MHPMCOUNTERS ,0 , vmstate_pmu_ctr_state, PMUCTRState),VMSTATE_UINTTL_ARRAY (env.mhpmevent_val,RISCVCPU ,RV_MAX_MHPMEVENTS ), +VMSTATE_UINTTL_ARRAY (env.mhpmeventh_val,RISCVCPU ,RV_MAX_MHPMEVENTS ),VMSTATE_UINTTL (env.sscratch,RISCVCPU ),VMSTATE_UINTTL (env.mscratch,RISCVCPU ), VMSTATE_UINT64(env.mfromhost, RISCVCPU),target/riscv/pmu.c +355
-2@@ -19,14 +19,367 @@#include "qemu/osdep.h" #include "cpu.h" #include "pmu.h" +#include "sysemu/cpu-timers.h" + +#define RISCV_TIMEBASE_FREQ 1000000000/* 1Ghz */ +#define MAKE_32BIT_MASK (shift, length) \ + (((uint32_t)(~0UL) >> (32 - (length))) << (shift)) + +static bool riscv_pmu_counter_valid (RISCVCPU *cpu,uint32_t ctr_idx) +{ +if (ctr_idx <3 || ctr_idx >=RV_MAX_MHPMCOUNTERS || + !(cpu->pmu_avail_ctrs &BIT (ctr_idx))) { +return false; + }else { +return true; + } +} + +static bool riscv_pmu_counter_enabled (RISCVCPU *cpu,uint32_t ctr_idx) +{ +CPURISCVState *env = &cpu->env; + +if (riscv_pmu_counter_valid (cpu, ctr_idx) && + !get_field (env->mcountinhibit,BIT (ctr_idx))) { +return true; + }else { +return false; + } +} + +static int riscv_pmu_incr_ctr_rv32 (RISCVCPU *cpu,uint32_t ctr_idx) +{ +CPURISCVState *env = &cpu->env; +target_ulong max_val =UINT32_MAX ; +PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +bool virt_on =riscv_cpu_virt_enabled (env); + +/* Privilege mode filtering */ +if ((env->priv ==PRV_M && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_MINH )) || + (env->priv ==PRV_S && virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_VSINH )) || + (env->priv ==PRV_U && virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_VUINH )) || + (env->priv ==PRV_S && !virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_SINH )) || + (env->priv ==PRV_U && !virt_on && + (env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_UINH ))) { +return 0 ; + } + +/* Handle the overflow scenario */ +if (counter->mhpmcounter_val == max_val) { +if (counter->mhpmcounterh_val == max_val) { + counter->mhpmcounter_val =0 ; + counter->mhpmcounterh_val =0 ; +/* Generate interrupt only if OF bit is clear */ +if (!(env->mhpmeventh_val[ctr_idx] &MHPMEVENTH_BIT_OF )) { + env->mhpmeventh_val[ctr_idx] |=MHPMEVENTH_BIT_OF ; +riscv_cpu_update_mip (cpu,MIP_LCOFIP ,BOOL_TO_MASK (1 )); + } + }else { + counter->mhpmcounterh_val++; + } + }else { + counter->mhpmcounter_val++; + } + +return 0 ; +} + +static int riscv_pmu_incr_ctr_rv64 (RISCVCPU *cpu,uint32_t ctr_idx) +{ +CPURISCVState *env = &cpu->env; +PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; +uint64_t max_val =UINT64_MAX ; +bool virt_on =riscv_cpu_virt_enabled (env); + +/* Privilege mode filtering */ +if ((env->priv ==PRV_M && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_MINH )) || + (env->priv ==PRV_S && virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_VSINH )) || + (env->priv ==PRV_U && virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_VUINH )) || + (env->priv ==PRV_S && !virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_SINH )) || + (env->priv ==PRV_U && !virt_on && + (env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_UINH ))) { +return 0 ; + } + +/* Handle the overflow scenario */ +if (counter->mhpmcounter_val == max_val) { + counter->mhpmcounter_val =0 ; +/* Generate interrupt only if OF bit is clear */ +if (!(env->mhpmevent_val[ctr_idx] &MHPMEVENT_BIT_OF )) { + env->mhpmevent_val[ctr_idx] |=MHPMEVENT_BIT_OF ; +riscv_cpu_update_mip (cpu,MIP_LCOFIP ,BOOL_TO_MASK (1 )); + } + }else { + counter->mhpmcounter_val++; + } +return 0 ; +} + +int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx) +{ +uint32_t ctr_idx; +int ret; +CPURISCVState *env = &cpu->env; +gpointer value; + + value =g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx)); +if (!value) { +return -1 ; + } + + ctr_idx =GPOINTER_TO_UINT (value); +if (!riscv_pmu_counter_enabled (cpu, ctr_idx) || +get_field (env->mcountinhibit,BIT (ctr_idx))) { +return -1 ; + } + +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + ret =riscv_pmu_incr_ctr_rv32 (cpu, ctr_idx); + }else { + ret =riscv_pmu_incr_ctr_rv64 (cpu, ctr_idx); + } + +return ret; +}bool riscv_pmu_ctr_monitor_instructions (CPURISCVState *env,uint32_t target_ctr) { -return (target_ctr ==0 ) ? true : false; +RISCVCPU *cpu; +uint32_t event_idx; +uint32_t ctr_idx; + +/* Fixed instret counter */ +if (target_ctr ==2 ) { +return true; + } + + cpu =RISCV_CPU (env_cpu (env)); + event_idx =RISCV_PMU_EVENT_HW_INSTRUCTIONS ; + ctr_idx =GPOINTER_TO_UINT (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx))); +if (!ctr_idx) { +return false; + } + +return target_ctr == ctr_idx ? true : false; }bool riscv_pmu_ctr_monitor_cycles (CPURISCVState *env,uint32_t target_ctr) { -return (target_ctr ==2 ) ? true : false; +RISCVCPU *cpu; +uint32_t event_idx; +uint32_t ctr_idx; + +/* Fixed mcycle counter */ +if (target_ctr ==0 ) { +return true; + } + + cpu =RISCV_CPU (env_cpu (env)); + event_idx =RISCV_PMU_EVENT_HW_CPU_CYCLES ; + ctr_idx =GPOINTER_TO_UINT (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx))); + +/* Counter zero is not used for event_ctr_map */ +if (!ctr_idx) { +return false; + } + +return (target_ctr == ctr_idx) ? true : false; +} + +static gboolean pmu_remove_event_map (gpointer key,gpointer value, +gpointer udata) +{ +return (GPOINTER_TO_UINT (value) ==GPOINTER_TO_UINT (udata)) ? true : false; +} + +static int64_t pmu_icount_ticks_to_ns (int64_t value) +{ +int64_t ret =0 ; + +if (icount_enabled ()) { + ret =icount_to_ns (value); + }else { + ret = (NANOSECONDS_PER_SECOND /RISCV_TIMEBASE_FREQ ) * value; + } + +return ret; +} + +int riscv_pmu_update_event_map (CPURISCVState *env,uint64_t value, +uint32_t ctr_idx) +{ +uint32_t event_idx; +RISCVCPU *cpu =RISCV_CPU (env_cpu (env)); + +if (!riscv_pmu_counter_valid (cpu, ctr_idx)) { +return -1 ; + } + +/** +* Expected mhpmevent value is zero for reset case. Remove the current +* mapping. +*/ +if (!value) { +g_hash_table_foreach_remove (cpu->pmu_event_ctr_map, + pmu_remove_event_map, +GUINT_TO_POINTER (ctr_idx)); +return 0 ; + } + + event_idx = value &MHPMEVENT_IDX_MASK ; +if (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (event_idx))) { +return 0 ; + } + +switch (event_idx) { +case RISCV_PMU_EVENT_HW_CPU_CYCLES : +case RISCV_PMU_EVENT_HW_INSTRUCTIONS : +case RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS : +case RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS : +case RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS : +break ; +default : +/* We don't support any raw events right now */ +return -1 ; + } +g_hash_table_insert (cpu->pmu_event_ctr_map,GUINT_TO_POINTER (event_idx), +GUINT_TO_POINTER (ctr_idx)); + +return 0 ; +} + +static void pmu_timer_trigger_irq (RISCVCPU *cpu, +enum riscv_pmu_event_idx evt_idx) +{ +uint32_t ctr_idx; +CPURISCVState *env = &cpu->env; +PMUCTRState *counter; +target_ulong *mhpmevent_val; +uint64_t of_bit_mask; +int64_t irq_trigger_at; + +if (evt_idx !=RISCV_PMU_EVENT_HW_CPU_CYCLES && + evt_idx !=RISCV_PMU_EVENT_HW_INSTRUCTIONS ) { +return ; + } + + ctr_idx =GPOINTER_TO_UINT (g_hash_table_lookup (cpu->pmu_event_ctr_map, +GUINT_TO_POINTER (evt_idx))); +if (!riscv_pmu_counter_enabled (cpu, ctr_idx)) { +return ; + } + +if (riscv_cpu_mxl (env) ==MXL_RV32 ) { + mhpmevent_val = &env->mhpmeventh_val[ctr_idx]; + of_bit_mask =MHPMEVENTH_BIT_OF ; + }else { + mhpmevent_val = &env->mhpmevent_val[ctr_idx]; + of_bit_mask =MHPMEVENT_BIT_OF ; + } + + counter = &env->pmu_ctrs[ctr_idx]; +if (counter->irq_overflow_left >0 ) { + irq_trigger_at =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + + counter->irq_overflow_left; +timer_mod_anticipate_ns (cpu->pmu_timer, irq_trigger_at); + counter->irq_overflow_left =0 ; +return ; + } + +if (cpu->pmu_avail_ctrs &BIT (ctr_idx)) { +/* Generate interrupt only if OF bit is clear */ +if (!(*mhpmevent_val & of_bit_mask)) { + *mhpmevent_val |= of_bit_mask; +riscv_cpu_update_mip (cpu,MIP_LCOFIP ,BOOL_TO_MASK (1 )); + } + } +} + +/* Timer callback for instret and cycle counter overflow */ +void riscv_pmu_timer_cb (void *priv) +{ +RISCVCPU *cpu = priv; + +/* Timer event was triggered only for these events */ +pmu_timer_trigger_irq (cpu,RISCV_PMU_EVENT_HW_CPU_CYCLES ); +pmu_timer_trigger_irq (cpu,RISCV_PMU_EVENT_HW_INSTRUCTIONS ); +} + +int riscv_pmu_setup_timer (CPURISCVState *env,uint64_t value,uint32_t ctr_idx) +{ +uint64_t overflow_delta, overflow_at; +int64_t overflow_ns, overflow_left =0 ; +RISCVCPU *cpu =RISCV_CPU (env_cpu (env)); +PMUCTRState *counter = &env->pmu_ctrs[ctr_idx]; + +if (!riscv_pmu_counter_valid (cpu, ctr_idx) || !cpu->cfg.ext_sscofpmf) { +return -1 ; + } + +if (value) { + overflow_delta =UINT64_MAX - value +1 ; + }else { + overflow_delta =UINT64_MAX ; + } + +/** +* QEMU supports only int64_t timers while RISC-V counters are uint64_t. +* Compute the leftover and save it so that it can be reprogrammed again +* when timer expires. +*/ +if (overflow_delta >INT64_MAX ) { + overflow_left = overflow_delta -INT64_MAX ; + } + +if (riscv_pmu_ctr_monitor_cycles (env, ctr_idx) || +riscv_pmu_ctr_monitor_instructions (env, ctr_idx)) { + overflow_ns =pmu_icount_ticks_to_ns ((int64_t )overflow_delta); + overflow_left =pmu_icount_ticks_to_ns (overflow_left) ; + }else { +return -1 ; + } + overflow_at = (uint64_t )qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) + overflow_ns; + +if (overflow_at >INT64_MAX ) { + overflow_left += overflow_at -INT64_MAX ; + counter->irq_overflow_left = overflow_left; + overflow_at =INT64_MAX ; + } +timer_mod_anticipate_ns (cpu->pmu_timer, overflow_at); + +return 0 ; +} + + +int riscv_pmu_init (RISCVCPU *cpu,int num_counters) +{ +if (num_counters > (RV_MAX_MHPMCOUNTERS -3 )) { +return -1 ; + } + + cpu->pmu_event_ctr_map =g_hash_table_new (g_direct_hash, g_direct_equal); +if (!cpu->pmu_event_ctr_map) { +/* PMU support can not be enabled */ +qemu_log_mask (LOG_UNIMP ,"PMU events can't be supported\n" ); + cpu->cfg.pmu_num =0 ; +return -1 ; + } + +/* Create a bitmask of available programmable counters */ + cpu->pmu_avail_ctrs =MAKE_32BIT_MASK (3 , num_counters); + +return 0 ; }target/riscv/pmu.h +7
-0@@ -26,3 +26,10 @@ bool riscv_pmu_ctr_monitor_instructions(CPURISCVState *env, uint32_t target_ctr);bool riscv_pmu_ctr_monitor_cycles (CPURISCVState *env,uint32_t target_ctr); +void riscv_pmu_timer_cb (void *priv); +int riscv_pmu_init (RISCVCPU *cpu,int num_counters); +int riscv_pmu_update_event_map (CPURISCVState *env,uint64_t value, +uint32_t ctr_idx); +int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx); +int riscv_pmu_setup_timer (CPURISCVState *env,uint64_t value, +uint32_t ctr_idx);
Re: [PATCH v10 08/12] target/riscv: Add sscofpmf extension support
In reply to Atish PatraAm Dienstag, 21. Juni 2022, 01:15:58 CEST schrieb Atish Patra:
Quoted 10 lines
The Sscofpmf ('Ss' for Privileged arch and Supervisor-level extensions, and 'cofpmf' for Count OverFlow and Privilege Mode Filtering) extension allows the perf to handle overflow interrupts and filtering support. This patch provides a framework for programmable counters to leverage the extension. As the extension doesn't have any provision for the overflow bit for fixed counters, the fixed events can also be monitoring using programmable counters. The underlying counters for cycle and instruction counters are always running. Thus, a separate timer device is programmed to handle the overflow.
Quoted 2 lines
Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>
it looks like patches 1-7 already landed in Qemu though I didn't see any "applied" messages, so it took me a bit to realize that :-) .
In any case, I ran Atish's sample from the cover-letter with a matching kernel nad got similar results as shown in the cover-letter.
Re: [PATCH v10 08/12] target/riscv: Add sscofpmf extension support
Quoted 12 lines · Heiko Stübner
Am Dienstag, 21. Juni 2022, 01:15:58 CEST schrieb Atish Patra:
Quoted 10 lines
The Sscofpmf ('Ss' for Privileged arch and Supervisor-level extensions, and 'cofpmf' for Count OverFlow and Privilege Mode Filtering) extension allows the perf to handle overflow interrupts and filtering support. This patch provides a framework for programmable counters to leverage the extension. As the extension doesn't have any provision for the overflow bit for fixed counters, the fixed events can also be monitoring using programmable counters. The underlying counters for cycle and instruction counters are always running. Thus, a separate timer device is programmed to handle the overflow.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>it looks like patches 1-7 already landed in Qemu though I didn't see any "applied" messages, so it took me a bit to realize that :-) .
Argh, sorry! I must have forgotten to mention that.
Alistair
Quoted 9 lines
In any case, I ran Atish's sample from the cover-letter with a matching kernel nad got similar results as shown in the cover-letter.
Tested-by Heiko Stuebner <[email protected]>
PATCH v10 9/12 target/riscv: Simplify counter predicate function
In reply to Atish PatraAll the hpmcounters and the fixed counters (CY, IR, TM) can be represented as a unified counter. Thus, the predicate function doesn't need handle each case separately.
Simplify the predicate function so that we just handle things differently between RV32/RV64 and S/HS mode.
--- target/riscv/csr.c | 112 +++++---------------------------------------- 1 file changed, 11 insertions(+), 101 deletions(-)
target/riscv/csr.c +11 -101
@@ -74,6 +74,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno)CPUState *cs =env_cpu (env);RISCVCPU *cpu =RISCV_CPU (cs);int ctr_index; +target_ulong ctr_mask;int base_csrno =CSR_CYCLE ;bool rv32 =riscv_cpu_mxl (env) ==MXL_RV32 ? true : false;
@@ -82,122 +83,31 @@ static RISCVException ctr(CPURISCVState *env, int csrno) base_csrno +=0x80 ; } ctr_index = csrno - base_csrno; + ctr_mask =BIT (ctr_index);if ((csrno >=CSR_CYCLE && csrno <=CSR_INSTRET ) || (csrno >=CSR_CYCLEH && csrno <=CSR_INSTRETH )) { goto skip_ext_pmu_check; } -if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs &BIT (ctr_index)))) { +if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs & ctr_mask))) {/* No counter is enabled in PMU or the counter is out of range */ return RISCV_EXCP_ILLEGAL_INST ; } skip_ext_pmu_check: -if (env->priv ==PRV_S ) { -switch (csrno) { -case CSR_CYCLE : -if (!get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_TIME : -if (!get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_INSTRET : -if (!get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: -if (!get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - } -if (rv32) { -switch (csrno) { -case CSR_CYCLEH : -if (!get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_TIMEH : -if (!get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_INSTRETH : -if (!get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: -if (!get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - } - } +if (((env->priv ==PRV_S ) && (!get_field (env->mcounteren, ctr_mask))) || + ((env->priv ==PRV_U ) && (!get_field (env->scounteren, ctr_mask)))) { +return RISCV_EXCP_ILLEGAL_INST ; }if (riscv_cpu_virt_enabled (env)) { -switch (csrno) { -case CSR_CYCLE : -if (!get_field (env->hcounteren,COUNTEREN_CY ) && -get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_TIME : -if (!get_field (env->hcounteren,COUNTEREN_TM ) && -get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_INSTRET : -if (!get_field (env->hcounteren,COUNTEREN_IR ) && -get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: -if (!get_field (env->hcounteren,1 << ctr_index) && -get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - } -if (rv32) { -switch (csrno) { -case CSR_CYCLEH : -if (!get_field (env->hcounteren,COUNTEREN_CY ) && -get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_TIMEH : -if (!get_field (env->hcounteren,COUNTEREN_TM ) && -get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_INSTRETH : -if (!get_field (env->hcounteren,COUNTEREN_IR ) && -get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: -if (!get_field (env->hcounteren,1 << ctr_index) && -get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - } +if (!get_field (env->mcounteren, ctr_mask)) { +/* The bit must be set in mcountern for HS mode access */ +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; + }else if (!get_field (env->hcounteren, ctr_mask)) { +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; } }#endif
Signature · 1 line
2.25.1
Re: [PATCH v10 09/12] target/riscv: Simplify counter predicate function
Quoted 86 lines · Atish Patra
All the hpmcounters and the fixed counters (CY, IR, TM) can be represented as a unified counter. Thus, the predicate function doesn't need handle each case separately.
Simplify the predicate function so that we just handle things differently between RV32/RV64 and S/HS mode.
Reviewed-by Bin Meng <[email protected]>Acked-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>--- target/riscv/csr.c | 112 +++++---------------------------------------- 1 file changed, 11 insertions(+), 101 deletions(-)target/riscv/csr.c +3
-42@@ -74,6 +74,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno)CPUState *cs =env_cpu (env);RISCVCPU *cpu =RISCV_CPU (cs);int ctr_index; +target_ulong ctr_mask;int base_csrno =CSR_CYCLE ;bool rv32 =riscv_cpu_mxl (env) ==MXL_RV32 ? true : false;@@ -82,122 +83,31 @@ static RISCVException ctr(CPURISCVState *env, int csrno) base_csrno +=0x80 ; } ctr_index = csrno - base_csrno; + ctr_mask =BIT (ctr_index);if ((csrno >=CSR_CYCLE && csrno <=CSR_INSTRET ) || (csrno >=CSR_CYCLEH && csrno <=CSR_INSTRETH )) { goto skip_ext_pmu_check; } -if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs &BIT (ctr_index)))) { +if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs & ctr_mask))) {/* No counter is enabled in PMU or the counter is out of range */ return RISCV_EXCP_ILLEGAL_INST ; } skip_ext_pmu_check: -if (env->priv ==PRV_S ) { -switch (csrno) { -case CSR_CYCLE : -if (!get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_TIME : -if (!get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_INSTRET : -if (!get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: -if (!get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - } -if (rv32) { -switch (csrno) { -case CSR_CYCLEH : -if (!get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_TIMEH : -if (!get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_INSTRETH : -if (!get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H:
target/riscv/csr.c static RISCVException ctr(CPURISCVState *env, int csrno) · line 103
Quoted 10 lines
target/riscv/csr.c +3
-6-if (!get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - } - } +if (((env->priv ==PRV_S ) && (!get_field (env->mcounteren, ctr_mask))) || + ((env->priv ==PRV_U ) && (!get_field (env->scounteren, ctr_mask)))) { +return RISCV_EXCP_ILLEGAL_INST ; }
Sorry. I didn't realize this simplification and sent a similar patch to fix the problems in Xcounteren
related check I found when I tried to learn the patchset for state enable extension two days ago.
I think there are several difference between our understanding, following is my modifications:
+ if (csrno <= CSR_HPMCOUNTER31 && csrno >= CSR_CYCLE) {
+ field = 1 << (csrno - CSR_CYCLE);
+ } else if (riscv_cpu_mxl(env) == MXL_RV32 && csrno <= CSR_HPMCOUNTER31H &&
+ csrno >= CSR_CYCLEH) {
+ field = 1 << (csrno - CSR_CYCLEH);
+ }
+
+ if (env->priv < PRV_M && !get_field(env->mcounteren, field)) {
+ return RISCV_EXCP_ILLEGAL_INST;
+ }
+
+ if (riscv_cpu_virt_enabled(env) && !get_field(env->hcounteren, field)) {
+ return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
+ }
+
+ if (riscv_has_ext(env, RVS) && env->priv == PRV_U &&
+ !get_field(env->scounteren, field)) {
+ if (riscv_cpu_virt_enabled(env)) {
+ return RISCV_EXCP_VIRT_INSTRUCTION_FAULT;
+ } else {
+ return RISCV_EXCP_ILLEGAL_INST;
}
}1) For any less-privileged mode under M, illegal exception is raised if matching bit in mcounteren is zero.
2) For VS/VU mode('H' extension is supported implicitly), virtual instruction exception is raised if matching bit in hcounteren is zero.
3) scounteren csr only works in U/VU mode when 'S' extension is supported:
For U mode, illegal exception is raised if matching bit in scounteren is zero. For VU mode, virtual instruction exception exception is raised if matching bit
in scounteren is zero.
Regards,
Weiwei Li
Quoted 63 lines
target/riscv/csr.c +5
-53if (riscv_cpu_virt_enabled (env)) { -switch (csrno) { -case CSR_CYCLE : -if (!get_field (env->hcounteren,COUNTEREN_CY ) && -get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_TIME : -if (!get_field (env->hcounteren,COUNTEREN_TM ) && -get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_INSTRET : -if (!get_field (env->hcounteren,COUNTEREN_IR ) && -get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: -if (!get_field (env->hcounteren,1 << ctr_index) && -get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - } -if (rv32) { -switch (csrno) { -case CSR_CYCLEH : -if (!get_field (env->hcounteren,COUNTEREN_CY ) && -get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_TIMEH : -if (!get_field (env->hcounteren,COUNTEREN_TM ) && -get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_INSTRETH : -if (!get_field (env->hcounteren,COUNTEREN_IR ) && -get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: -if (!get_field (env->hcounteren,1 << ctr_index) && -get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - } +if (!get_field (env->mcounteren, ctr_mask)) { +/* The bit must be set in mcountern for HS mode access */ +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; + }else if (!get_field (env->hcounteren, ctr_mask)) { +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; } }#endif
Re: [PATCH v10 09/12] target/riscv: Simplify counter predicate function
Quoted 132 lines · Weiwei Li
在 2022/6/21 上午7:15, Atish Patra 写道:
All the hpmcounters and the fixed counters (CY, IR, TM) can be represented as a unified counter. Thus, the predicate function doesn't need handle each case separately.
Simplify the predicate function so that we just handle things differently between RV32/RV64 and S/HS mode.
Reviewed-by Bin Meng <[email protected]>Acked-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>--- target/riscv/csr.c | 112 +++++---------------------------------------- 1 file changed, 11 insertions(+), 101 deletions(-)target/riscv/csr.c +6
-48@@ -74,6 +74,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno)CPUState *cs =env_cpu (env);RISCVCPU *cpu =RISCV_CPU (cs);int ctr_index; +target_ulong ctr_mask;int base_csrno =CSR_CYCLE ;bool rv32 =riscv_cpu_mxl (env) ==MXL_RV32 ? true : false;@@ -82,122 +83,31 @@ static RISCVException ctr(CPURISCVState *env, int csrno) base_csrno +=0x80 ; } ctr_index = csrno - base_csrno; + ctr_mask =BIT (ctr_index);if ((csrno >=CSR_CYCLE && csrno <=CSR_INSTRET ) || (csrno >=CSR_CYCLEH && csrno <=CSR_INSTRETH )) { goto skip_ext_pmu_check; } -if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs &BIT (ctr_index)))) { +if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs & ctr_mask))) {/* No counter is enabled in PMU or the counter is out of range */ return RISCV_EXCP_ILLEGAL_INST ; } skip_ext_pmu_check: -if (env->priv ==PRV_S ) { -switch (csrno) { -case CSR_CYCLE : -if (!get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_TIME : -if (!get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_INSTRET : -if (!get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: -if (!get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - } -if (rv32) { -switch (csrno) { -case CSR_CYCLEH : -if (!get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_TIMEH : -if (!get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_INSTRETH : -if (!get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: -if (!get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - } - } +if (((env->priv ==PRV_S ) && (!get_field (env->mcounteren, ctr_mask))) || + ((env->priv ==PRV_U ) && (!get_field (env->scounteren, ctr_mask)))) { +return RISCV_EXCP_ILLEGAL_INST ; }Sorry. I didn't realize this simplification and sent a similar patch to fix the problems in Xcounteren
related check I found when I tried to learn the patchset for state enable extension two days ago.
I think there are several difference between our understanding, following is my modifications:
target/riscv/csr.c +21
-0+if (csrno <=CSR_HPMCOUNTER31 && csrno >=CSR_CYCLE ) { + field =1 << (csrno -CSR_CYCLE ); + }else if (riscv_cpu_mxl (env) ==MXL_RV32 && csrno <=CSR_HPMCOUNTER31H && + csrno >=CSR_CYCLEH ) { + field =1 << (csrno -CSR_CYCLEH ); + } + +if (env->priv <PRV_M && !get_field (env->mcounteren, field)) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +if (riscv_cpu_virt_enabled (env) && !get_field (env->hcounteren, field)) { +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; + } + +if (riscv_has_ext (env,RVS ) && env->priv ==PRV_U && + !get_field (env->scounteren, field)) { +if (riscv_cpu_virt_enabled (env)) { +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; + }else { +return RISCV_EXCP_ILLEGAL_INST ; } }
Quoted 3 lines
1) For any less-privileged mode under M, illegal exception is raised if matching bit in mcounteren is zero.
As per the priv spec, in the section 3.1.11
"When one of these bits is set, access to the corresponding register is permitted in the next implemented privilege mode (S-mode if implemented, otherwise U-mode)."
mcounteren controls the access for U-mode only if the next implemented mode is U (riscv_has_ext(env, RVS) must be false).
I did not add the additional check as the ctr is defined only for !CONFIG_USER_ONLY.
Quoted 4 lines
2) For VS/VU mode('H' extension is supported implicitly), virtual instruction exception is raised if matching bit in hcounteren is zero.
3) scounteren csr only works in U/VU mode when 'S' extension is supported:
Yes. But we don't need additional check for 'S' extension as it will be done by the predicate function "smode"
Quoted 70 lines
For U mode, illegal exception is raised if matching bit in scounteren is zero. For VU mode, virtual instruction exception exception is raised if matching bit
in scounteren is zero.Regards,
Weiwei Liif (riscv_cpu_virt_enabled(env)) {target/riscv/csr.c +5
-53-switch (csrno) { -case CSR_CYCLE : -if (!get_field (env->hcounteren,COUNTEREN_CY ) && -get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_TIME : -if (!get_field (env->hcounteren,COUNTEREN_TM ) && -get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_INSTRET : -if (!get_field (env->hcounteren,COUNTEREN_IR ) && -get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: -if (!get_field (env->hcounteren,1 << ctr_index) && -get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - } -if (rv32) { -switch (csrno) { -case CSR_CYCLEH : -if (!get_field (env->hcounteren,COUNTEREN_CY ) && -get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_TIMEH : -if (!get_field (env->hcounteren,COUNTEREN_TM ) && -get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_INSTRETH : -if (!get_field (env->hcounteren,COUNTEREN_IR ) && -get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: -if (!get_field (env->hcounteren,1 << ctr_index) && -get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - } +if (!get_field (env->mcounteren, ctr_mask)) { +/* The bit must be set in mcountern for HS mode access */ +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; + }else if (!get_field (env->hcounteren, ctr_mask)) { +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; } }#endif
Re: [PATCH v10 09/12] target/riscv: Simplify counter predicate function
Quoted 140 lines · Atish Kumar Patra
Quoted 131 lines · Weiwei Li
在 2022/6/21 上午7:15, Atish Patra 写道:
All the hpmcounters and the fixed counters (CY, IR, TM) can be represented as a unified counter. Thus, the predicate function doesn't need handle each case separately.
Simplify the predicate function so that we just handle things differently between RV32/RV64 and S/HS mode.
Reviewed-by Bin Meng <[email protected]>Acked-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>--- target/riscv/csr.c | 112 +++++---------------------------------------- 1 file changed, 11 insertions(+), 101 deletions(-)target/riscv/csr.c +6
-48@@ -74,6 +74,7 @@ static RISCVException ctr(CPURISCVState *env, int csrno)CPUState *cs =env_cpu (env);RISCVCPU *cpu =RISCV_CPU (cs);int ctr_index; +target_ulong ctr_mask;int base_csrno =CSR_CYCLE ;bool rv32 =riscv_cpu_mxl (env) ==MXL_RV32 ? true : false;@@ -82,122 +83,31 @@ static RISCVException ctr(CPURISCVState *env, int csrno) base_csrno +=0x80 ; } ctr_index = csrno - base_csrno; + ctr_mask =BIT (ctr_index);if ((csrno >=CSR_CYCLE && csrno <=CSR_INSTRET ) || (csrno >=CSR_CYCLEH && csrno <=CSR_INSTRETH )) { goto skip_ext_pmu_check; } -if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs &BIT (ctr_index)))) { +if ((!cpu->cfg.pmu_num || !(cpu->pmu_avail_ctrs & ctr_mask))) {/* No counter is enabled in PMU or the counter is out of range */ return RISCV_EXCP_ILLEGAL_INST ; } skip_ext_pmu_check: -if (env->priv ==PRV_S ) { -switch (csrno) { -case CSR_CYCLE : -if (!get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_TIME : -if (!get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_INSTRET : -if (!get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: -if (!get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - } -if (rv32) { -switch (csrno) { -case CSR_CYCLEH : -if (!get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_TIMEH : -if (!get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; -case CSR_INSTRETH : -if (!get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: -if (!get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_ILLEGAL_INST ; - } -break ; - } - } +if (((env->priv ==PRV_S ) && (!get_field (env->mcounteren, ctr_mask))) || + ((env->priv ==PRV_U ) && (!get_field (env->scounteren, ctr_mask)))) { +return RISCV_EXCP_ILLEGAL_INST ; }Sorry. I didn't realize this simplification and sent a similar patch to fix the problems in Xcounteren
related check I found when I tried to learn the patchset for state enable extension two days ago.
I think there are several difference between our understanding, following is my modifications:
target/riscv/csr.c +21
-0+if (csrno <=CSR_HPMCOUNTER31 && csrno >=CSR_CYCLE ) { + field =1 << (csrno -CSR_CYCLE ); + }else if (riscv_cpu_mxl (env) ==MXL_RV32 && csrno <=CSR_HPMCOUNTER31H && + csrno >=CSR_CYCLEH ) { + field =1 << (csrno -CSR_CYCLEH ); + } + +if (env->priv <PRV_M && !get_field (env->mcounteren, field)) { +return RISCV_EXCP_ILLEGAL_INST ; + } + +if (riscv_cpu_virt_enabled (env) && !get_field (env->hcounteren, field)) { +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; + } + +if (riscv_has_ext (env,RVS ) && env->priv ==PRV_U && + !get_field (env->scounteren, field)) { +if (riscv_cpu_virt_enabled (env)) { +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; + }else { +return RISCV_EXCP_ILLEGAL_INST ; } }Quoted 3 lines
1) For any less-privileged mode under M, illegal exception is raised if matching bit in mcounteren is zero.
As per the priv spec, in the section 3.1.11
"When one of these bits is set, access to the corresponding register is permitted in the next implemented privilege mode (S-mode if implemented, otherwise U-mode)."
Quoted 4 lines
mcounteren controls the access for U-mode only if the next implemented mode is U (
riscv_has_ext(env, RVS)must be false).
I did not add the additional check as the ctr is defined only for !CONFIG_USER_ONLY.
In section 3.1.11, It also have description like this:
"In systems with U-mode, the mcounteren must be implemented, but all fields are WARL and may be read-only zero, indicating reads to the corresponding counter will cause an illegal instruction exception when executing in a less-privileged mode."
And !CONFIG_USER_ONLY is defined for QEMU system emulation, it doesn't means current priv
cannot be PRV_U mode.
Quoted 7 lines
Quoted 5 lines
2) For VS/VU mode('H' extension is supported implicitly), virtual instruction exception is raised if matching bit in hcounteren is zero.
3) scounteren csr only works in U/VU mode when 'S' extension is supported:
Yes. But we don't need additional check for 'S' extension as it will be done by the predicate function "smode"
This is the question, smode can only guard the read/write of scounteren. If 'S' extension is not implemented,
scounteren will be zero. and If check is done as following:
+ if (((env->priv == PRV_S) && (!get_field(env->mcounteren, ctr_mask))) ||
+ ((env->priv == PRV_U) && (!get_field(env->scounteren, ctr_mask)))) {
+ return RISCV_EXCP_ILLEGAL_INST;
}any access from PRV_U will trigger illegal instruction exception. From above spec, this kind of access will be controlled by mcounteren, and will be legal if matching bit in mcounteren is 1.
Regards,
Weiwei Li
Quoted 71 lines
Quoted 71 lines
For U mode, illegal exception is raised if matching bit in scounteren is zero. For VU mode, virtual instruction exception exception is raised if matching bit
in scounteren is zero.Regards,
Weiwei Liif (riscv_cpu_virt_enabled(env)) {target/riscv/csr.c +5
-53-switch (csrno) { -case CSR_CYCLE : -if (!get_field (env->hcounteren,COUNTEREN_CY ) && -get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_TIME : -if (!get_field (env->hcounteren,COUNTEREN_TM ) && -get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_INSTRET : -if (!get_field (env->hcounteren,COUNTEREN_IR ) && -get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - case CSR_HPMCOUNTER3...CSR_HPMCOUNTER31: -if (!get_field (env->hcounteren,1 << ctr_index) && -get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - } -if (rv32) { -switch (csrno) { -case CSR_CYCLEH : -if (!get_field (env->hcounteren,COUNTEREN_CY ) && -get_field (env->mcounteren,COUNTEREN_CY )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_TIMEH : -if (!get_field (env->hcounteren,COUNTEREN_TM ) && -get_field (env->mcounteren,COUNTEREN_TM )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; -case CSR_INSTRETH : -if (!get_field (env->hcounteren,COUNTEREN_IR ) && -get_field (env->mcounteren,COUNTEREN_IR )) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - case CSR_HPMCOUNTER3H...CSR_HPMCOUNTER31H: -if (!get_field (env->hcounteren,1 << ctr_index) && -get_field (env->mcounteren,1 << ctr_index)) { -return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; - } -break ; - } +if (!get_field (env->mcounteren, ctr_mask)) { +/* The bit must be set in mcountern for HS mode access */ +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; + }else if (!get_field (env->hcounteren, ctr_mask)) { +return RISCV_EXCP_VIRT_INSTRUCTION_FAULT ; } }#endif
Re: [PATCH v10 09/12] target/riscv: Simplify counter predicate function
In reply to Atish PatraAm Dienstag, 21. Juni 2022, 01:15:59 CEST schrieb Atish Patra:
Quoted 10 lines
All the hpmcounters and the fixed counters (CY, IR, TM) can be represented as a unified counter. Thus, the predicate function doesn't need handle each case separately.
Simplify the predicate function so that we just handle things differently between RV32/RV64 and S/HS mode.
Reviewed-by Bin Meng <[email protected]>Acked-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>
PATCH v10 10/12 target/riscv: Add few cache related PMU events
In reply to Atish PatraFrom: Atish Patra <[email protected]>
Qemu can monitor the following cache related PMU events through tlb_fill functions.
1. DTLB load/store miss
3. ITLB prefetch miss
Increment the PMU counter in tlb_fill function.
--- target/riscv/cpu_helper.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
target/riscv/cpu_helper.c +25 -0
@@ -21,10 +21,12 @@#include "qemu/log.h" #include "qemu/main-loop.h" #include "cpu.h" +#include "pmu.h" #include "exec/exec-all.h" #include "tcg/tcg-op.h" #include "trace.h" #include "semihosting/common-semi.h" +#include "cpu_bits.h" int riscv_cpu_mmu_index (CPURISCVState *env,bool ifetch) {
@@ -1180,6 +1182,28 @@ void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,cpu_loop_exit_restore (cs, retaddr); } + +static void pmu_tlb_fill_incr_ctr (RISCVCPU *cpu,MMUAccessType access_type) +{ +enum riscv_pmu_event_idx pmu_event_type; + +switch (access_type) { +case MMU_INST_FETCH : + pmu_event_type =RISCV_PMU_EVENT_CACHE_ITLB_PREFETCH_MISS ; +break ; +case MMU_DATA_LOAD : + pmu_event_type =RISCV_PMU_EVENT_CACHE_DTLB_READ_MISS ; +break ; +case MMU_DATA_STORE : + pmu_event_type =RISCV_PMU_EVENT_CACHE_DTLB_WRITE_MISS ; +break ; +default : +return ; + } + +riscv_pmu_incr_ctr (cpu, pmu_event_type); +} +bool riscv_cpu_tlb_fill (CPUState *cs,vaddr address,int size,MMUAccessType access_type,int mmu_idx,bool probe,uintptr_t retaddr)
@@ -1276,6 +1300,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size, } } }else { +pmu_tlb_fill_incr_ctr (cpu, access_type);/* Single stage lookup */ ret =get_physical_address (env, &pa, &prot, address,NULL , access_type, mmu_idx, true, false, false);
Signature · 1 line
2.25.1
Re: [PATCH v10 10/12] target/riscv: Add few cache related PMU events
Am Dienstag, 21. Juni 2022, 01:16:00 CEST schrieb Atish Patra:
Quoted 5 lines
From: Atish Patra <[email protected]>
Qemu can monitor the following cache related PMU events through
tlb_fillfunctions.
Quoted 8 lines
1. DTLB load/store miss
3. ITLB prefetch missIncrement the PMU counter in
tlb_fillfunction.Reviewed-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>
PATCH v10 11/12 hw/riscv: virt: Add PMU DT node to the device tree
In reply to Atish PatraQemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.
Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.
--- hw/riscv/virt.c | 28 +++++++++++++++++++++++ target/riscv/cpu.c | 1 + target/riscv/pmu.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++ target/riscv/pmu.h | 1 + 4 files changed, 87 insertions(+)
hw/riscv/virt.c +28 -0
@@ -29,6 +29,7 @@#include "hw/char/serial.h" #include "target/riscv/cpu.h" #include "hw/core/sysbus-fdt.h" +#include "target/riscv/pmu.h" #include "hw/riscv/riscv_hart.h" #include "hw/riscv/virt.h" #include "hw/riscv/boot.h"
@@ -714,6 +715,32 @@ static void create_fdt_socket_aplic(RISCVVirtState *s, aplic_phandles[socket] = aplic_s_phandle; } +static void create_fdt_socket_pmu (RISCVVirtState *s, +int socket,uint32_t *phandle, +uint32_t *intc_phandles) +{ +int cpu; +char *pmu_name; +uint32_t *pmu_cells; +MachineState *mc =MACHINE (s); +RISCVCPU hart = s->soc[socket].harts[0 ]; + + pmu_cells =g_new0 (uint32_t, s->soc[socket].num_harts *2 ); + +for (cpu =0 ; cpu < s->soc[socket].num_harts; cpu++) { + pmu_cells[cpu *2 +0 ] =cpu_to_be32 (intc_phandles[cpu]); + pmu_cells[cpu *2 +1 ] =cpu_to_be32 (IRQ_PMU_OVF ); + } + + pmu_name =g_strdup_printf ("/soc/pmu" ); +qemu_fdt_add_subnode (mc->fdt, pmu_name); +qemu_fdt_setprop_string (mc->fdt, pmu_name,"compatible" ,"riscv,pmu" ); +riscv_pmu_generate_fdt_node (mc->fdt, hart.cfg.pmu_num, pmu_name); + +g_free (pmu_name); +g_free (pmu_cells); +} + static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap, bool is_32_bit, uint32_t *phandle, uint32_t *irq_mmio_phandle,
@@ -759,6 +786,7 @@ static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap, &intc_phandles[phandle_pos]); } } +create_fdt_socket_pmu (s, socket, phandle, intc_phandles); }if (s->aia_type ==VIRT_AIA_TYPE_APLIC_IMSIC ) {
target/riscv/cpu.c +1 -0
@@ -1110,6 +1110,7 @@ static void riscv_isa_string_ext(RISCVCPU *cpu, char **isa_str, int max_str_len)ISA_EDATA_ENTRY (zve64f, ext_zve64f),ISA_EDATA_ENTRY (zhinx, ext_zhinx),ISA_EDATA_ENTRY (zhinxmin, ext_zhinxmin), +ISA_EDATA_ENTRY (sscofpmf, ext_sscofpmf),ISA_EDATA_ENTRY (svinval, ext_svinval),ISA_EDATA_ENTRY (svnapot, ext_svnapot), ISA_EDATA_ENTRY(svpbmt, ext_svpbmt),
target/riscv/pmu.c +57 -0
68 lines not on this page · raw message
target/riscv/pmu.h +1 -0
@@ -31,5 +31,6 @@ int riscv_pmu_init(RISCVCPU *cpu, int num_counters);int riscv_pmu_update_event_map (CPURISCVState *env,uint64_t value,uint32_t ctr_idx);int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx); +void riscv_pmu_generate_fdt_node (void *fdt,int num_counters,char *pmu_name);int riscv_pmu_setup_timer (CPURISCVState *env,uint64_t value,uint32_t ctr_idx);
Signature · 1 line
2.25.1
Re: [PATCH v10 11/12] hw/riscv: virt: Add PMU DT node to the device tree
Hi Atish,
Am Dienstag, 21. Juni 2022, 01:16:01 CEST schrieb Atish Patra:
Quoted 21 lines
Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.
Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.
Acked-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>---
hw/riscv/virt.c +10
-0+static void create_fdt_socket_pmu (RISCVVirtState *s, +int socket,uint32_t *phandle, +uint32_t *intc_phandles) +{ +int cpu; +char *pmu_name; +uint32_t *pmu_cells; +MachineState *mc =MACHINE (s); +RISCVCPU hart = s->soc[socket].harts[0 ]; +
hw/riscv/virt.c static void create_fdt_socket_aplic(RISCVVirtState *s, · line 737
Quoted 10 lines
hw/riscv/virt.c +10
-0+ pmu_cells =g_new0 (uint32_t, s->soc[socket].num_harts *2 ); + +for (cpu =0 ; cpu < s->soc[socket].num_harts; cpu++) { + pmu_cells[cpu *2 +0 ] =cpu_to_be32 (intc_phandles[cpu]); + pmu_cells[cpu *2 +1 ] =cpu_to_be32 (IRQ_PMU_OVF ); + } + + pmu_name =g_strdup_printf ("/soc/pmu" ); +qemu_fdt_add_subnode (mc->fdt, pmu_name); +qemu_fdt_setprop_string (mc->fdt, pmu_name,"compatible" ,"riscv,pmu" );
Where is the binding document for this?
As the comment below states the "riscv,event-to-mhpmcounters" property is opensbi-specific and gets removed in the opensbi stage, but that still leaves the pmu node in it and from the version I found, Rob wasn't overly happy with the compatible [0]. Did this get addressed?
Thanks
Heiko
Quoted 114 lines
hw/riscv/virt.c +7
-0+riscv_pmu_generate_fdt_node (mc->fdt, hart.cfg.pmu_num, pmu_name); + +g_free (pmu_name); +g_free (pmu_cells); +} + static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap, bool is_32_bit, uint32_t *phandle, uint32_t *irq_mmio_phandle,@@ -759,6 +786,7 @@ static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap, &intc_phandles[phandle_pos]); } } +create_fdt_socket_pmu (s, socket, phandle, intc_phandles); }if (s->aia_type ==VIRT_AIA_TYPE_APLIC_IMSIC ) {target/riscv/cpu.c +1
-0@@ -1110,6 +1110,7 @@ static void riscv_isa_string_ext(RISCVCPU *cpu, char **isa_str, int max_str_len)ISA_EDATA_ENTRY (zve64f, ext_zve64f),ISA_EDATA_ENTRY (zhinx, ext_zhinx),ISA_EDATA_ENTRY (zhinxmin, ext_zhinxmin), +ISA_EDATA_ENTRY (sscofpmf, ext_sscofpmf),ISA_EDATA_ENTRY (svinval, ext_svinval),ISA_EDATA_ENTRY (svnapot, ext_svnapot), ISA_EDATA_ENTRY(svpbmt, ext_svpbmt),target/riscv/pmu.c +57
-0@@ -20,11 +20,68 @@#include "cpu.h" #include "pmu.h" #include "sysemu/cpu-timers.h" +#include "sysemu/device_tree.h" #define RISCV_TIMEBASE_FREQ 1000000000/* 1Ghz */ #define MAKE_32BIT_MASK (shift, length) \ (((uint32_t)(~0UL) >> (32 - (length))) << (shift)) +/** +* To keep it simple, any event can be mapped to any programmable counters in +* QEMU. The generic cycle & instruction count events can also be monitored +* using programmable counters. In that case, mcycle & minstret must continue +* to provide the correct value as well. Heterogeneous PMU per hart is not +* supported yet. Thus, number of counters are same across all harts. +*/ +void riscv_pmu_generate_fdt_node (void *fdt,int num_ctrs,char *pmu_name) +{ +uint32_t fdt_event_ctr_map[20 ] = {}; +uint32_t cmask; + +/* All the programmable counters can map to any event */ + cmask =MAKE_32BIT_MASK (3 , num_ctrs); + +/** +* The event encoding is specified in the SBI specification +* Event idx is a 20bits wide number encoded as follows: +* event_idx[19:16] = type +* event_idx[15:0] = code +* The code field in cache events are encoded as follows: +* event_idx.code[15:3] = cache_id +* event_idx.code[2:1] = op_id +* event_idx.code[0:0] = result_id +*/ + +/* SBI_PMU_HW_CPU_CYCLES: 0x01 : type(0x00) */ + fdt_event_ctr_map[0 ] =cpu_to_be32 (0x00000001 ); + fdt_event_ctr_map[1 ] =cpu_to_be32 (0x00000001 ); + fdt_event_ctr_map[2 ] =cpu_to_be32 (cmask |1 <<0 ); + +/* SBI_PMU_HW_INSTRUCTIONS: 0x02 : type(0x00) */ + fdt_event_ctr_map[3 ] =cpu_to_be32 (0x00000002 ); + fdt_event_ctr_map[4 ] =cpu_to_be32 (0x00000002 ); + fdt_event_ctr_map[5 ] =cpu_to_be32 (cmask |1 <<2 ); + +/* SBI_PMU_HW_CACHE_DTLB : 0x03 READ : 0x00 MISS : 0x00 type(0x01) */ + fdt_event_ctr_map[6 ] =cpu_to_be32 (0x00010019 ); + fdt_event_ctr_map[7 ] =cpu_to_be32 (0x00010019 ); + fdt_event_ctr_map[8 ] =cpu_to_be32 (cmask); + +/* SBI_PMU_HW_CACHE_DTLB : 0x03 WRITE : 0x01 MISS : 0x00 type(0x01) */ + fdt_event_ctr_map[9 ] =cpu_to_be32 (0x0001001B ); + fdt_event_ctr_map[10 ] =cpu_to_be32 (0x0001001B ); + fdt_event_ctr_map[11 ] =cpu_to_be32 (cmask); + +/* SBI_PMU_HW_CACHE_ITLB : 0x04 READ : 0x00 MISS : 0x00 type(0x01) */ + fdt_event_ctr_map[12 ] =cpu_to_be32 (0x00010021 ); + fdt_event_ctr_map[13 ] =cpu_to_be32 (0x00010021 ); + fdt_event_ctr_map[14 ] =cpu_to_be32 (cmask); + +/* This a OpenSBI specific DT property documented in OpenSBI docs */ +qemu_fdt_setprop (fdt, pmu_name,"riscv,event-to-mhpmcounters" , + fdt_event_ctr_map,sizeof (fdt_event_ctr_map)); +} +static bool riscv_pmu_counter_valid (RISCVCPU *cpu,uint32_t ctr_idx) { if (ctr_idx < 3 || ctr_idx >= RV_MAX_MHPMCOUNTERS ||target/riscv/pmu.h +1
-0@@ -31,5 +31,6 @@ int riscv_pmu_init(RISCVCPU *cpu, int num_counters);int riscv_pmu_update_event_map (CPURISCVState *env,uint64_t value,uint32_t ctr_idx);int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx); +void riscv_pmu_generate_fdt_node (void *fdt,int num_counters,char *pmu_name);int riscv_pmu_setup_timer (CPURISCVState *env,uint64_t value,uint32_t ctr_idx);
Re: [PATCH v10 11/12] hw/riscv: virt: Add PMU DT node to the device tree
Quoted 38 lines · Heiko Stübner
Hi Atish,
Am Dienstag, 21. Juni 2022, 01:16:01 CEST schrieb Atish Patra:
Quoted 21 lines
Qemu virt machine can support few cache events and cycle/instret counters. It also supports counter overflow for these events.
Add a DT node so that OpenSBI/Linux kernel is aware of the virt machine capabilities. There are some dummy nodes added for testing as well.
Acked-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>Signed-off-by Atish Patra <[email protected]>---
hw/riscv/virt.c +10
-0+static void create_fdt_socket_pmu (RISCVVirtState *s, +int socket,uint32_t *phandle, +uint32_t *intc_phandles) +{ +int cpu; +char *pmu_name; +uint32_t *pmu_cells; +MachineState *mc =MACHINE (s); +RISCVCPU hart = s->soc[socket].harts[0 ]; +hw/riscv/virt.c static void create_fdt_socket_aplic(RISCVVirtState *s, · line 737
Quoted 10 lines
hw/riscv/virt.c +10
-0+ pmu_cells =g_new0 (uint32_t, s->soc[socket].num_harts *2 ); + +for (cpu =0 ; cpu < s->soc[socket].num_harts; cpu++) { + pmu_cells[cpu *2 +0 ] =cpu_to_be32 (intc_phandles[cpu]); + pmu_cells[cpu *2 +1 ] =cpu_to_be32 (IRQ_PMU_OVF ); + } + + pmu_name =g_strdup_printf ("/soc/pmu" ); +qemu_fdt_add_subnode (mc->fdt, pmu_name); +qemu_fdt_setprop_string (mc->fdt, pmu_name,"compatible" ,"riscv,pmu" );Where is the binding document for this?
Quoted 5 lines
As the comment below states the "riscv,event-to-mhpmcounters" property is opensbi-specific and gets removed in the opensbi stage, but that still leaves the pmu node in it and from the version I found, Rob wasn't overly happy with the compatible [0]. Did this get addressed?
This is OpenSBI specific binding.
https://github.com/riscv-software-src/opensbi/blob/master/docs/pmu_support.md
Linux kernel doesn't use binding anymore. The earlier version patches relied on the DT binding.
However, based on the feedback it was removed.
OpenSBI should delete the node and the interrupt-extended property deletion is necessary at this point.
Quoted 128 lines
Thanks
Heiko[0] https://lore.kernel.org/all/[email protected]/
Quoted 114 lines
hw/riscv/virt.c +7
-0+riscv_pmu_generate_fdt_node (mc->fdt, hart.cfg.pmu_num, pmu_name); + +g_free (pmu_name); +g_free (pmu_cells); +} + static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap, bool is_32_bit, uint32_t *phandle, uint32_t *irq_mmio_phandle,@@ -759,6 +786,7 @@ static void create_fdt_sockets(RISCVVirtState *s, const MemMapEntry *memmap, &intc_phandles[phandle_pos]); } } +create_fdt_socket_pmu (s, socket, phandle, intc_phandles); }if (s->aia_type ==VIRT_AIA_TYPE_APLIC_IMSIC ) {target/riscv/cpu.c +1
-0@@ -1110,6 +1110,7 @@ static void riscv_isa_string_ext(RISCVCPU *cpu, char **isa_str, int max_str_len)ISA_EDATA_ENTRY (zve64f, ext_zve64f),ISA_EDATA_ENTRY (zhinx, ext_zhinx),ISA_EDATA_ENTRY (zhinxmin, ext_zhinxmin), +ISA_EDATA_ENTRY (sscofpmf, ext_sscofpmf),ISA_EDATA_ENTRY (svinval, ext_svinval),ISA_EDATA_ENTRY (svnapot, ext_svnapot), ISA_EDATA_ENTRY(svpbmt, ext_svpbmt),target/riscv/pmu.c +57
-0@@ -20,11 +20,68 @@#include "cpu.h" #include "pmu.h" #include "sysemu/cpu-timers.h" +#include "sysemu/device_tree.h" #define RISCV_TIMEBASE_FREQ 1000000000/* 1Ghz */ #define MAKE_32BIT_MASK (shift, length) \ (((uint32_t)(~0UL) >> (32 - (length))) << (shift)) +/** +* To keep it simple, any event can be mapped to any programmable counters in +* QEMU. The generic cycle & instruction count events can also be monitored +* using programmable counters. In that case, mcycle & minstret must continue +* to provide the correct value as well. Heterogeneous PMU per hart is not +* supported yet. Thus, number of counters are same across all harts. +*/ +void riscv_pmu_generate_fdt_node (void *fdt,int num_ctrs,char *pmu_name) +{ +uint32_t fdt_event_ctr_map[20 ] = {}; +uint32_t cmask; + +/* All the programmable counters can map to any event */ + cmask =MAKE_32BIT_MASK (3 , num_ctrs); + +/** +* The event encoding is specified in the SBI specification +* Event idx is a 20bits wide number encoded as follows: +* event_idx[19:16] = type +* event_idx[15:0] = code +* The code field in cache events are encoded as follows: +* event_idx.code[15:3] = cache_id +* event_idx.code[2:1] = op_id +* event_idx.code[0:0] = result_id +*/ + +/* SBI_PMU_HW_CPU_CYCLES: 0x01 : type(0x00) */ + fdt_event_ctr_map[0 ] =cpu_to_be32 (0x00000001 ); + fdt_event_ctr_map[1 ] =cpu_to_be32 (0x00000001 ); + fdt_event_ctr_map[2 ] =cpu_to_be32 (cmask |1 <<0 ); + +/* SBI_PMU_HW_INSTRUCTIONS: 0x02 : type(0x00) */ + fdt_event_ctr_map[3 ] =cpu_to_be32 (0x00000002 ); + fdt_event_ctr_map[4 ] =cpu_to_be32 (0x00000002 ); + fdt_event_ctr_map[5 ] =cpu_to_be32 (cmask |1 <<2 ); + +/* SBI_PMU_HW_CACHE_DTLB : 0x03 READ : 0x00 MISS : 0x00 type(0x01) */ + fdt_event_ctr_map[6 ] =cpu_to_be32 (0x00010019 ); + fdt_event_ctr_map[7 ] =cpu_to_be32 (0x00010019 ); + fdt_event_ctr_map[8 ] =cpu_to_be32 (cmask); + +/* SBI_PMU_HW_CACHE_DTLB : 0x03 WRITE : 0x01 MISS : 0x00 type(0x01) */ + fdt_event_ctr_map[9 ] =cpu_to_be32 (0x0001001B ); + fdt_event_ctr_map[10 ] =cpu_to_be32 (0x0001001B ); + fdt_event_ctr_map[11 ] =cpu_to_be32 (cmask); + +/* SBI_PMU_HW_CACHE_ITLB : 0x04 READ : 0x00 MISS : 0x00 type(0x01) */ + fdt_event_ctr_map[12 ] =cpu_to_be32 (0x00010021 ); + fdt_event_ctr_map[13 ] =cpu_to_be32 (0x00010021 ); + fdt_event_ctr_map[14 ] =cpu_to_be32 (cmask); + +/* This a OpenSBI specific DT property documented in OpenSBI docs */ +qemu_fdt_setprop (fdt, pmu_name,"riscv,event-to-mhpmcounters" , + fdt_event_ctr_map,sizeof (fdt_event_ctr_map)); +} +static bool riscv_pmu_counter_valid (RISCVCPU *cpu,uint32_t ctr_idx) { if (ctr_idx < 3 || ctr_idx >= RV_MAX_MHPMCOUNTERS ||target/riscv/pmu.h +1
-0@@ -31,5 +31,6 @@ int riscv_pmu_init(RISCVCPU *cpu, int num_counters);int riscv_pmu_update_event_map (CPURISCVState *env,uint64_t value,uint32_t ctr_idx);int riscv_pmu_incr_ctr (RISCVCPU *cpu,enum riscv_pmu_event_idx event_idx); +void riscv_pmu_generate_fdt_node (void *fdt,int num_counters,char *pmu_name);int riscv_pmu_setup_timer (CPURISCVState *env,uint64_t value,uint32_t ctr_idx);
Signature · 2 lines
Regards, Atish
PATCH v10 12/12 target/riscv: Update the privilege field for sscofpmf CSRs
In reply to Atish PatraThe sscofpmf extension was ratified as a part of priv spec v1.12. Mark the csr_ops accordingly.
--- target/riscv/csr.c | 90 ++++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 30 deletions(-)
target/riscv/csr.c +60 -30
@@ -4002,63 +4002,92 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { write_mhpmevent }, [CSR_MHPMEVENT3H] = { "mhpmevent3h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT4H] = { "mhpmevent4h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT5H] = { "mhpmevent5h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT6H] = { "mhpmevent6h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT7H] = { "mhpmevent7h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT8H] = { "mhpmevent8h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT9H] = { "mhpmevent9h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT10H] = { "mhpmevent10h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT11H] = { "mhpmevent11h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT12H] = { "mhpmevent12h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT13H] = { "mhpmevent13h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT14H] = { "mhpmevent14h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT15H] = { "mhpmevent15h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT16H] = { "mhpmevent16h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT17H] = { "mhpmevent17h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT18H] = { "mhpmevent18h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT19H] = { "mhpmevent19h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT20H] = { "mhpmevent20h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT21H] = { "mhpmevent21h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT22H] = { "mhpmevent22h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT23H] = { "mhpmevent23h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT24H] = { "mhpmevent24h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT25H] = { "mhpmevent25h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT26H] = { "mhpmevent26h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT27H] = { "mhpmevent27h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT28H] = { "mhpmevent28h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT29H] = { "mhpmevent29h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT30H] = { "mhpmevent30h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT31H] = { "mhpmevent31h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_HPMCOUNTER3H] = { "hpmcounter3h", ctr32, read_hpmcounterh }, [CSR_HPMCOUNTER4H] = { "hpmcounter4h", ctr32, read_hpmcounterh },
@@ -4148,7 +4177,8 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { write_mhpmcounterh }, [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", mctr32, read_hpmcounterh, write_mhpmcounterh }, - [CSR_SCOUNTOVF] = { "scountovf", sscofpmf, read_scountovf }, + [CSR_SCOUNTOVF] = { "scountovf", sscofpmf, read_scountovf, + .min_priv_ver = PRIV_VERSION_1_12_0 }, #endif /* !CONFIG_USER_ONLY */ };
Signature · 1 line
2.25.1
Re: [PATCH v10 12/12] target/riscv: Update the privilege field for sscofpmf CSRs
Am Dienstag, 21. Juni 2022, 01:16:02 CEST schrieb Atish Patra:
Quoted 5 lines
The sscofpmf extension was ratified as a part of priv spec v1.12. Mark the
csr_opsaccordingly.Reviewed-by Alistair Francis <[email protected]>Signed-off-by Atish Patra <[email protected]>
Quoted 141 lines
--- target/riscv/csr.c | 90 ++++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 30 deletions(-)target/riscv/csr.c +60
-30@@ -4002,63 +4002,92 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { write_mhpmevent }, [CSR_MHPMEVENT3H] = { "mhpmevent3h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT4H] = { "mhpmevent4h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT5H] = { "mhpmevent5h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT6H] = { "mhpmevent6h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT7H] = { "mhpmevent7h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT8H] = { "mhpmevent8h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT9H] = { "mhpmevent9h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT10H] = { "mhpmevent10h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT11H] = { "mhpmevent11h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT12H] = { "mhpmevent12h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT13H] = { "mhpmevent13h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT14H] = { "mhpmevent14h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT15H] = { "mhpmevent15h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT16H] = { "mhpmevent16h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT17H] = { "mhpmevent17h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT18H] = { "mhpmevent18h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT19H] = { "mhpmevent19h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT20H] = { "mhpmevent20h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT21H] = { "mhpmevent21h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT22H] = { "mhpmevent22h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT23H] = { "mhpmevent23h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT24H] = { "mhpmevent24h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT25H] = { "mhpmevent25h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT26H] = { "mhpmevent26h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT27H] = { "mhpmevent27h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT28H] = { "mhpmevent28h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT29H] = { "mhpmevent29h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT30H] = { "mhpmevent30h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_MHPMEVENT31H] = { "mhpmevent31h", sscofpmf, read_mhpmeventh, - write_mhpmeventh}, + write_mhpmeventh, + .min_priv_ver = PRIV_VERSION_1_12_0 }, [CSR_HPMCOUNTER3H] = { "hpmcounter3h", ctr32, read_hpmcounterh }, [CSR_HPMCOUNTER4H] = { "hpmcounter4h", ctr32, read_hpmcounterh },@@ -4148,7 +4177,8 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] = { write_mhpmcounterh }, [CSR_MHPMCOUNTER31H] = { "mhpmcounter31h", mctr32, read_hpmcounterh, write_mhpmcounterh }, - [CSR_SCOUNTOVF] = { "scountovf", sscofpmf, read_scountovf }, + [CSR_SCOUNTOVF] = { "scountovf", sscofpmf, read_scountovf, + .min_priv_ver = PRIV_VERSION_1_12_0 }, #endif /* !CONFIG_USER_ONLY */ };