Sscof extension support
v4 cover Sscof extension support
The Sscofpmf extension [1] ('Ss' for Privileged arch and Supervisor-level extensions, and 'cofpmf' for Count OverFlow and Privilege Mode Filtering) allows the perf like tool to handle overflow interrupts and filtering support.
This series implements all the bits required to support "Sscof" extension and few other fixes. This series can be found at github[4] as well.
It has been tested with Linux kernel patches[3] for different hardware/firmware events on Qemu.
This patch requires QEMU support series[2] to work. As the Qemu did not support mhpmevent/mhpmcounter before this series, these changes are not backward compatible to avoid code churn. Anybody who wants to use perf must include Qemu, OpenSBI, Linux kernel patches.
Tested on both Qemu and HiFive unmatched.
Changes from v3->v4:
1. Replace all instances of sscof to Sscofpmf.
2. Addressed all the nit comments from Anup & Bin.
Changes from v2->v3:
1. Rename the DT prefix to riscv instead of PMU.
Changes from v1->v2:
1. Addressed all the comments on v1.
2. Relaxed the mcountinhibit restriction for PMU extension enablement.
3. Delete the interrupt node in DT if sscof extension is not present.
[1] https://drive.google.com/file/d/1KcjgbLM5L1ZKY8934aJl8aQwGlMz6Cbo/view
[2] https://github.com/atishp04/qemu/tree/riscv_pmu_v3
[3] https://github.com/atishp04/linux/tree/sbi_pmu_v4
[4] https://github.com/atishp04/opensbi/tree/pmu_sscofpmf_v4
Atish Patra (11):
riscv: Add new CSRs introduced by Sscofpmf[1] extension
lib: sbi: Update csr_read/write_num for PMU
lib: sbi: Detect Sscofpmf extension at run time
lib: sbi: Delegate PMU counter overflow interrupt to S mode
lib: sbi: Support sscofpmf extension in OpenSBI
lib: sbi: Always enable access for all counters
lib: sbi: Allow programmable counters to monitor cycle/instret events
lib: sbi: Reset the mhpmevent value upon counter reset
lib: sbi: Counter info width should be zero indexed
lib: sbi: Enable PMU extension for platforms without mcountinhibit
lib: utils: Rename the prefix in PMU DT properties
docs/pmu_support.md | 12 +- include/sbi/riscv_encoding.h | 61 ++++++++++ include/sbi/sbi_hart.h | 4 +- include/sbi/sbi_pmu.h | 1 + lib/sbi/riscv_asm.c | 13 ++ lib/sbi/sbi_ecall_pmu.c | 10 +- lib/sbi/sbi_hart.c | 36 +++--- lib/sbi/sbi_pmu.c | 228 ++++++++++++++++++++++++++--------- lib/utils/fdt/fdt_pmu.c | 16 ++- 9 files changed, 289 insertions(+), 92 deletions(-)
--
2.31.1
PATCH v4 1/11 riscv: Add new CSRs introduced by Sscofpmf[1] extension
[1] https://drive.google.com/file/d/1KcjgbLM5L1ZKY8934aJl8aQwGlMz6Cbo/view
--- include/sbi/riscv_encoding.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
include/sbi/riscv_encoding.h +34 -0
@@ -516,6 +516,40 @@#define CSR_MHPMEVENT30 0x33e#define CSR_MHPMEVENT31 0x33f +/* For RV32 */ +#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 + +/* Counter Overflow CSR */ +#define CSR_SCOUNTOVF 0xda0 +/* Debug/Trace Registers */ #define CSR_TSELECT 0x7a0#define CSR_TDATA1 0x7a1
Signature · 1 line
2.31.1
PATCH v4 1/11 riscv: Add new CSRs introduced by Sscofpmf[1] extension
? 2021-11-05???? 15:05 -0700?Atish Patra???
Quoted 6 lines
[1]
https://drive.google.com/file/d/1KcjgbLM5L1ZKY8934aJl8aQwGlMz6Cbo/viewReviewed-by Bin Meng <[email protected]>Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>
Quoted 54 lines
---
?include/sbi/riscv_encoding.h | 34 ++++++++++++++++++++++++++++++++++ ?1 file changed, 34 insertions(+)include/sbi/riscv_encoding.h +0
-0@@ -516,6 +516,40 @@?#define CSR_MHPMEVENT30????????????????????????0x33e ?#define CSR_MHPMEVENT31????????????????????????0x33f ?
include/sbi/riscv_encoding.h +34
-0+/* For RV32 */ +#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 + +/* Counter Overflow CSR */ +#define CSR_SCOUNTOVF ??????????????????0xda0 +?/* Debug/Trace Registers */
?#define CSR_TSELECT????????????????????0x7a0
?#define CSR_TDATA1?????????????????????0x7a1Signature · 1 line
2.31.1
PATCH v4 2/11 lib: sbi: Update csr_read/write_num for PMU
In reply to Atish PatraThe Sscofpmf extension introduces mhpmevent[h] csrs to handle filtering /overflow bits in RV32. There is no way to read/write mcountinhibit using mcountinhibit csr using a variable.
Updated the support to read/write mhpmevent[h] and mcountinhibit csr.
--- lib/sbi/riscv_asm.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
lib/sbi/riscv_asm.c +13 -0
@@ -124,6 +124,11 @@ unsigned long csr_read_num(int csr_num) switchcase_csr_read_4(CSR_MHPMCOUNTER4, ret) switchcase_csr_read_8(CSR_MHPMCOUNTER8, ret) switchcase_csr_read_16(CSR_MHPMCOUNTER16, ret) + switchcase_csr_read(CSR_MCOUNTINHIBIT, ret) + switchcase_csr_read(CSR_MHPMEVENT3, ret) + switchcase_csr_read_4(CSR_MHPMEVENT4, ret) + switchcase_csr_read_8(CSR_MHPMEVENT8, ret) + switchcase_csr_read_16(CSR_MHPMEVENT16, ret)#if __riscv_xlen ==32 switchcase_csr_read(CSR_MCYCLEH, ret)switchcase_csr_read (CSR_MINSTRETH , ret)
@@ -131,6 +136,10 @@ unsigned long csr_read_num(int csr_num) switchcase_csr_read_4(CSR_MHPMCOUNTER4H, ret) switchcase_csr_read_8(CSR_MHPMCOUNTER8H, ret) switchcase_csr_read_16(CSR_MHPMCOUNTER16H, ret) + switchcase_csr_read(CSR_MHPMEVENT3H, ret) + switchcase_csr_read_4(CSR_MHPMEVENT4H, ret) + switchcase_csr_read_8(CSR_MHPMEVENT8H, ret) + switchcase_csr_read_16(CSR_MHPMEVENT16H, ret)#endif default :
@@ -189,6 +198,10 @@ void csr_write_num(int csr_num, unsigned long val) switchcase_csr_write_4(CSR_MHPMCOUNTER4H, val) switchcase_csr_write_8(CSR_MHPMCOUNTER8H, val) switchcase_csr_write_16(CSR_MHPMCOUNTER16H, val) + switchcase_csr_write(CSR_MHPMEVENT3H, val) + switchcase_csr_write_4(CSR_MHPMEVENT4H, val) + switchcase_csr_write_8(CSR_MHPMEVENT8H, val) + switchcase_csr_write_16(CSR_MHPMEVENT16H, val)#endif switchcase_csr_write(CSR_MCOUNTINHIBIT, val)switchcase_csr_write (CSR_MHPMEVENT3 , val)
Signature · 1 line
2.31.1
PATCH v4 3/11 lib: sbi: Detect Sscofpmf extension at run time
In reply to Atish PatraSscofpmf ISA extension introduces PMU counter overflow and filtering support. It introduces a read only scountovf csr that can be used to detect if a hart supports this extension at runtime. However, this feature is only useful if the hart already supports mcounteren and mcountinhibit.
Add a dynamic detection mechanism and boot time print message if sscofpmf is present.
--- include/sbi/sbi_hart.h | 4 +++- lib/sbi/sbi_hart.c | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)
include/sbi/sbi_hart.h +3 -1
@@ -20,8 +20,10 @@ enum sbi_hart_features {SBI_HART_HAS_MCOUNTEREN = (1 <<1 ),/** Hart has counter inhibit CSR */ SBI_HART_HAS_MCOUNTINHIBIT = (1 <<2 ), +/** Hart has sscofpmf extension */ +SBI_HART_HAS_SSCOFPMF = (1 <<3 ),/** HART has timer csr implementation in hardware */ -SBI_HART_HAS_TIME = (1 <<3 ), +SBI_HART_HAS_TIME = (1 <<4 ),/** Last index of Hart features*/ SBI_HART_HAS_LAST_FEATURE = SBI_HART_HAS_TIME,
lib/sbi/sbi_hart.c +12 -0
@@ -288,6 +288,9 @@ static inline char *sbi_hart_feature_id2string(unsigned long feature)case SBI_HART_HAS_MCOUNTINHIBIT : fstr ="mcountinhibit" ;break ; +case SBI_HART_HAS_SSCOFPMF : + fstr ="sscofpmf" ; +break ;case SBI_HART_HAS_TIME : fstr ="time" ;break ;
@@ -497,6 +500,15 @@ __mhpm_skip: hfeatures->features |=SBI_HART_HAS_MCOUNTINHIBIT ; } +/* Counter overflow/filtering is not useful without mcounter/inhibit */ +if (hfeatures->features &SBI_HART_HAS_MCOUNTINHIBIT && + hfeatures->features &SBI_HART_HAS_MCOUNTEREN ) { +/* Detect if hart supports sscofpmf */ +csr_read_allowed (CSR_SCOUNTOVF , (unsigned long )&trap); +if (!trap.cause) + hfeatures->features |=SBI_HART_HAS_SSCOFPMF ; + } +/* Detect if hart supports time CSR */ csr_read_allowed (CSR_TIME , (unsigned long )&trap);if (!trap.cause)
Signature · 1 line
2.31.1
PATCH v4 3/11 lib: sbi: Detect Sscofpmf extension at run time
Quoted 9 lines · Atish Patra
Sscofpmf ISA extension introduces PMU counter overflow and filtering support. It introduces a read only
scountovfcsr that can be used to detect if a hart supports this extension at runtime. However, this feature is only useful if the hart already supports mcounteren and mcountinhibit.Add a dynamic detection mechanism and boot time print message if sscofpmf is present.
Quoted 7 lines
Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- include/sbi/sbi_hart.h | 4 +++- lib/sbi/sbi_hart.c | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-)
PATCH v4 4/11 lib: sbi: Delegate PMU counter overflow interrupt to S mode
In reply to Atish PatraOpenSBI doesn't handle PMU counters for now.
Delegate the overflow counter to S-mode always.
--- include/sbi/riscv_encoding.h | 2 ++ lib/sbi/sbi_hart.c | 3 +++ 2 files changed, 5 insertions(+)
include/sbi/riscv_encoding.h +2 -0
@@ -86,6 +86,7 @@#define IRQ_VS_EXT 10#define IRQ_M_EXT 11#define IRQ_S_GEXT 12 +#define IRQ_PMU_OVF 13#define MIP_SSIP (_UL(1) << IRQ_S_SOFT)#define MIP_VSSIP (_UL(1) << IRQ_VS_SOFT)
@@ -97,6 +98,7 @@#define MIP_VSEIP (_UL(1) << IRQ_VS_EXT)#define MIP_MEIP (_UL(1) << IRQ_M_EXT)#define MIP_SGEIP (_UL(1) << IRQ_S_GEXT) +#define MIP_LCOFIP (_UL(1) << IRQ_PMU_OVF)#define SIP_SSIP MIP_SSIP#define SIP_STIP MIP_STIP
lib/sbi/sbi_hart.c +3 -0
@@ -115,6 +115,9 @@ static int delegate_traps(struct sbi_scratch *scratch)/* Send M-mode interrupts and most exceptions to S-mode */ interrupts =MIP_SSIP |MIP_STIP |MIP_SEIP ; +if (sbi_hart_has_feature (scratch,SBI_HART_HAS_SSCOFPMF )) + interrupts |=MIP_LCOFIP ; + exceptions = (1U <<CAUSE_MISALIGNED_FETCH ) | (1U <<CAUSE_BREAKPOINT ) | (1U <<CAUSE_USER_ECALL );if (sbi_platform_has_mfaults_delegation (plat))
Signature · 1 line
2.31.1
PATCH v4 5/11 lib: sbi: Support sscofpmf extension in OpenSBI
In reply to Atish PatraThis patch adds sscofpmf extension in pmu module which includes following things.
1. Enable overflow irq when starting a counter.
2. Setting the correct event filters passed from supervisor.
3. Delegating the overflow interrupt to the supervisor.
4. Add RV32 support for sscofpmf.
--- include/sbi/riscv_encoding.h | 25 ++++++++++ lib/sbi/sbi_pmu.c | 88 ++++++++++++++++++++++++++++++------ 2 files changed, 98 insertions(+), 15 deletions(-)
include/sbi/riscv_encoding.h +25 -0
@@ -173,6 +173,31 @@#define HGATP_MODE_SHIFT HGATP32_MODE_SHIFT#endif +#if __riscv_xlen ==64 +#define MHPMEVENT_OF (_UL(1) << 63) +#define MHPMEVENT_MINH (_UL(1) << 62) +#define MHPMEVENT_SINH (_UL(1) << 61) +#define MHPMEVENT_UINH (_UL(1) << 60) +#define MHPMEVENT_VSINH (_UL(1) << 59) +#define MHPMEVENT_VUINH (_UL(1) << 58) +#else +#define MHPMEVENTH_OF (_UL(1) << 31) +#define MHPMEVENTH_MINH (_ULL(1) << 30) +#define MHPMEVENTH_SINH (_ULL(1) << 29) +#define MHPMEVENTH_UINH (_ULL(1) << 28) +#define MHPMEVENTH_VSINH (_ULL(1) << 27) +#define MHPMEVENTH_VUINH (_ULL(1) << 26) + +#define MHPMEVENT_MINH (MHPMEVENTH_MINH << 32) +#define MHPMEVENT_SINH (MHPMEVENTH_SINH << 32) +#define MHPMEVENT_UINH (MHPMEVENTH_UINH << 32) +#define MHPMEVENT_VSINH (MHPMEVENTH_VSINH << 32) +#define MHPMEVENT_VUINH (MHPMEVENTH_VUINH << 32) + +#endif + +#define MHPMEVENT_SSCOF_MASK _ULL(0xFFFF000000000000) +/* ===== User-level CSRs ===== */ /* User Trap Setup (N-extension) */
lib/sbi/sbi_pmu.c +73 -15
148 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v4 5/11 lib: sbi: Support sscofpmf extension in OpenSBI
Quoted 10 lines · Atish Patra
This patch adds sscofpmf extension in pmu module which includes following things.
1. Enable overflow irq when starting a counter.
2. Setting the correct event filters passed from supervisor.
3. Delegating the overflow interrupt to the supervisor.
4. Add RV32 support for sscofpmf.Signed-off-by Atish Patra <[email protected]>
I had already reviewed this patch. Please preserve reviewed-by tags.
Regards,
Anup
Quoted 209 lines
--- include/sbi/riscv_encoding.h | 25 ++++++++++ lib/sbi/sbi_pmu.c | 88 ++++++++++++++++++++++++++++++------ 2 files changed, 98 insertions(+), 15 deletions(-)include/sbi/riscv_encoding.h +25
-0@@ -173,6 +173,31 @@#define HGATP_MODE_SHIFT HGATP32_MODE_SHIFT#endif +#if __riscv_xlen ==64 +#define MHPMEVENT_OF (_UL(1) << 63) +#define MHPMEVENT_MINH (_UL(1) << 62) +#define MHPMEVENT_SINH (_UL(1) << 61) +#define MHPMEVENT_UINH (_UL(1) << 60) +#define MHPMEVENT_VSINH (_UL(1) << 59) +#define MHPMEVENT_VUINH (_UL(1) << 58) +#else +#define MHPMEVENTH_OF (_UL(1) << 31) +#define MHPMEVENTH_MINH (_ULL(1) << 30) +#define MHPMEVENTH_SINH (_ULL(1) << 29) +#define MHPMEVENTH_UINH (_ULL(1) << 28) +#define MHPMEVENTH_VSINH (_ULL(1) << 27) +#define MHPMEVENTH_VUINH (_ULL(1) << 26) + +#define MHPMEVENT_MINH (MHPMEVENTH_MINH << 32) +#define MHPMEVENT_SINH (MHPMEVENTH_SINH << 32) +#define MHPMEVENT_UINH (MHPMEVENTH_UINH << 32) +#define MHPMEVENT_VSINH (MHPMEVENTH_VSINH << 32) +#define MHPMEVENT_VUINH (MHPMEVENTH_VUINH << 32) + +#endif + +#define MHPMEVENT_SSCOF_MASK _ULL(0xFFFF000000000000) +/* ===== User-level CSRs ===== */ /* User Trap Setup (N-extension) */ lib/sbi/sbi_pmu.c +73
-16@@ -226,11 +226,47 @@ int sbi_pmu_add_raw_event_counter_map(uint64_t select, u32 cmap) SBI_PMU_EVENT_RAW_IDX, cmap, select); } +static int pmu_ctr_enable_irq_hw (int ctr_idx) +{ +unsigned long mhpmevent_csr; +unsigned long mhpmevent_curr; +unsigned long mip_val; +unsigned long of_mask; + +if (ctr_idx <3 || ctr_idx >=SBI_PMU_HW_CTR_MAX ) +return SBI_EFAIL ; + +#if __riscv_xlen ==32 + mhpmevent_csr =CSR_MHPMEVENT3H + ctr_idx -3 ; + of_mask = ~MHPMEVENTH_OF ; +#else + mhpmevent_csr =CSR_MHPMEVENT3 + ctr_idx -3 ; + of_mask = ~MHPMEVENT_OF ; +#endif + + mhpmevent_curr =csr_read_num (mhpmevent_csr); + mip_val =csr_read (CSR_MIP ); +/** +* Clear out the OF bit so that next interrupt can be enabled. +* This should be done only when the corresponding overflow interrupt +* bit is cleared. That indicates that software has already handled the +* previous interrupts or the hardware yet to set an overflow interrupt. +* Otherwise, there will be race conditions where we may clear the bit +* the software is yet to handle the interrupt. +*/ +if (!(mip_val &MIP_LCOFIP )) { + mhpmevent_curr &= of_mask; +csr_write_num (mhpmevent_csr, mhpmevent_curr); + } + +return 0 ; +} +static void pmu_ctr_write_hw (uint32_t cidx,uint64_t ival) {#if __riscv_xlen ==32 csr_write_num (CSR_MCYCLE + cidx,0 ); -csr_write_num (CSR_MCYCLE + cidx, ival &0xFFFF ); +csr_write_num (CSR_MCYCLE + cidx, ival &0xFFFFFFFF );csr_write_num (CSR_MCYCLEH + cidx, ival >>BITS_PER_LONG );#else csr_write_num (CSR_MCYCLE + cidx, ival);@@ -241,6 +277,7 @@ static int pmu_ctr_start_hw(uint32_t cidx, uint64_t ival, bool ival_update) {unsigned long mctr_en =csr_read (CSR_MCOUNTEREN );unsigned long mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT ); +struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();/* Make sure the counter index lies within the range and is not TM bit */ if (cidx > num_hw_ctrs || cidx ==1 )@@ -252,12 +289,14 @@ static int pmu_ctr_start_hw(uint32_t cidx, uint64_t ival, bool ival_update)__set_bit (cidx, &mctr_en);__clear_bit (cidx, &mctr_inhbt); -if (ival_update) -pmu_ctr_write_hw (cidx, ival); - +if (sbi_hart_has_feature (scratch,SBI_HART_HAS_SSCOFPMF )) +pmu_ctr_enable_irq_hw (cidx);csr_write (CSR_MCOUNTEREN , mctr_en);csr_write (CSR_MCOUNTINHIBIT , mctr_inhbt); +if (ival_update) +pmu_ctr_write_hw (cidx, ival); +return 0 ; }@@ -326,7 +365,6 @@ static int pmu_ctr_stop_hw(uint32_t cidx)static int pmu_ctr_stop_fw (uint32_t cidx,uint32_t fw_evt_code) {u32 hartid =current_hartid (); - fw_event_map[hartid][fw_evt_code].bStarted = FALSE;return 0 ;@@ -362,8 +400,21 @@ int sbi_pmu_ctr_stop(unsigned long cbase, unsigned long cmask,return ret; } +static void pmu_update_inhibit_flags (unsigned long flags,uint64_t *mhpmevent_val) +{ +if (flags &SBI_PMU_CFG_FLAG_SET_VUINH ) + *mhpmevent_val |=MHPMEVENT_VUINH ; +if (flags &SBI_PMU_CFG_FLAG_SET_VSINH ) + *mhpmevent_val |=MHPMEVENT_VSINH ; +if (flags &SBI_PMU_CFG_FLAG_SET_UINH ) + *mhpmevent_val |=MHPMEVENT_UINH ; +if (flags &SBI_PMU_CFG_FLAG_SET_SINH ) + *mhpmevent_val |=MHPMEVENT_SINH ; +} +static int pmu_update_hw_mhpmevent (struct sbi_pmu_hw_event *hw_evt,int ctr_idx, -unsigned long eindex,uint64_t data) +unsigned long flags,unsigned long eindex, +uint64_t data) {struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();const struct sbi_platform *plat =sbi_platform_ptr (scratch);@@ -375,17 +426,23 @@ static int pmu_update_hw_mhpmevent(struct sbi_pmu_hw_event *hw_evt, int ctr_idx,if (!mhpmevent_val || ctr_idx <3 || ctr_idx >=SBI_PMU_HW_CTR_MAX )return SBI_EFAIL ; -/* TODO: The upper 16 bits of mhpmevent is reserved by sscofpmf extension. -* Update those bits based on the flags received from supervisor. -* The OVF bit also should be cleared here in case it was not cleared -* during event stop. -*/ -csr_write_num (CSR_MCOUNTINHIBIT + ctr_idx, mhpmevent_val); +/* Always clear the OVF bit and inhibit countin of events in M-mode */ + mhpmevent_val = (mhpmevent_val & ~MHPMEVENT_SSCOF_MASK ) |MHPMEVENT_MINH ; + +/* Update the inhibit flags based on inhibit flags received from supervisor */ +pmu_update_inhibit_flags (flags, &mhpmevent_val); + +#if __riscv_xlen ==32 +csr_write_num (CSR_MHPMEVENT3 + ctr_idx -3 , mhpmevent_val &0xFFFFFFFF ); +csr_write_num (CSR_MHPMEVENT3H + ctr_idx -3 , mhpmevent_val >>BITS_PER_LONG ); +#else +csr_write_num (CSR_MHPMEVENT3 + ctr_idx -3 , mhpmevent_val); +#endif return 0 ; } -static int pmu_ctr_find_hw (unsigned long cbase,unsigned long cmask, +static int pmu_ctr_find_hw (unsigned long cbase,unsigned long cmask,unsigned long flags,unsigned long event_idx,uint64_t data) {unsigned long ctr_mask;@@ -427,7 +484,7 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask,if (ctr_idx ==SBI_ENOTSUPP )return SBI_EFAIL ; - ret =pmu_update_hw_mhpmevent (temp, ctr_idx, event_idx, data); + ret =pmu_update_hw_mhpmevent (temp, ctr_idx, flags, event_idx, data);if (!ret) ret = ctr_idx;@@ -490,7 +547,8 @@ int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask,/* Any firmware counter can be used track any firmware event */ ctr_idx =pmu_ctr_find_fw (cidx_base, cidx_mask, hartid); }else { - ctr_idx =pmu_ctr_find_hw (cidx_base, cidx_mask, event_idx, event_data); + ctr_idx =pmu_ctr_find_hw (cidx_base, cidx_mask, flags, event_idx, + event_data); }if (ctr_idx <0 ) --2.31.1
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v4 5/11 lib: sbi: Support sscofpmf extension in OpenSBI
In reply to Atish PatraQuoted 91 lines · Atish Patra
This patch adds sscofpmf extension in pmu module which includes following things.
1. Enable overflow irq when starting a counter.
2. Setting the correct event filters passed from supervisor.
3. Delegating the overflow interrupt to the supervisor.
4. Add RV32 support for sscofpmf.Signed-off-by Atish Patra <[email protected]>--- include/sbi/riscv_encoding.h | 25 ++++++++++ lib/sbi/sbi_pmu.c | 88 ++++++++++++++++++++++++++++++------ 2 files changed, 98 insertions(+), 15 deletions(-)include/sbi/riscv_encoding.h +25
-0@@ -173,6 +173,31 @@#define HGATP_MODE_SHIFT HGATP32_MODE_SHIFT#endif +#if __riscv_xlen ==64 +#define MHPMEVENT_OF (_UL(1) << 63) +#define MHPMEVENT_MINH (_UL(1) << 62) +#define MHPMEVENT_SINH (_UL(1) << 61) +#define MHPMEVENT_UINH (_UL(1) << 60) +#define MHPMEVENT_VSINH (_UL(1) << 59) +#define MHPMEVENT_VUINH (_UL(1) << 58) +#else +#define MHPMEVENTH_OF (_UL(1) << 31) +#define MHPMEVENTH_MINH (_ULL(1) << 30) +#define MHPMEVENTH_SINH (_ULL(1) << 29) +#define MHPMEVENTH_UINH (_ULL(1) << 28) +#define MHPMEVENTH_VSINH (_ULL(1) << 27) +#define MHPMEVENTH_VUINH (_ULL(1) << 26) + +#define MHPMEVENT_MINH (MHPMEVENTH_MINH << 32) +#define MHPMEVENT_SINH (MHPMEVENTH_SINH << 32) +#define MHPMEVENT_UINH (MHPMEVENTH_UINH << 32) +#define MHPMEVENT_VSINH (MHPMEVENTH_VSINH << 32) +#define MHPMEVENT_VUINH (MHPMEVENTH_VUINH << 32) + +#endif + +#define MHPMEVENT_SSCOF_MASK _ULL(0xFFFF000000000000) +/* ===== User-level CSRs ===== */ /* User Trap Setup (N-extension) */ lib/sbi/sbi_pmu.c +32
-0@@ -226,11 +226,47 @@ int sbi_pmu_add_raw_event_counter_map(uint64_t select, u32 cmap) SBI_PMU_EVENT_RAW_IDX, cmap, select); } +static int pmu_ctr_enable_irq_hw (int ctr_idx) +{ +unsigned long mhpmevent_csr; +unsigned long mhpmevent_curr; +unsigned long mip_val; +unsigned long of_mask; + +if (ctr_idx <3 || ctr_idx >=SBI_PMU_HW_CTR_MAX ) +return SBI_EFAIL ; + +#if __riscv_xlen ==32 + mhpmevent_csr =CSR_MHPMEVENT3H + ctr_idx -3 ; + of_mask = ~MHPMEVENTH_OF ; +#else + mhpmevent_csr =CSR_MHPMEVENT3 + ctr_idx -3 ; + of_mask = ~MHPMEVENT_OF ; +#endif + + mhpmevent_curr =csr_read_num (mhpmevent_csr); + mip_val =csr_read (CSR_MIP ); +/** +* Clear out the OF bit so that next interrupt can be enabled. +* This should be done only when the corresponding overflow interrupt +* bit is cleared. That indicates that software has already handled the +* previous interrupts or the hardware yet to set an overflow interrupt. +* Otherwise, there will be race conditions where we may clear the bit +* the software is yet to handle the interrupt. +*/ +if (!(mip_val &MIP_LCOFIP )) { + mhpmevent_curr &= of_mask; +csr_write_num (mhpmevent_csr, mhpmevent_curr); + }
lib/sbi/sbi_pmu.c int sbi_pmu_add_raw_event_counter_map(uint64_t select, u32 cmap) · line 269
Quoted 10 lines
lib/sbi/sbi_pmu.c +5
-1+ +return 0 ; +} +static void pmu_ctr_write_hw (uint32_t cidx,uint64_t ival) {#if __riscv_xlen ==32 csr_write_num (CSR_MCYCLE + cidx,0 ); -csr_write_num (CSR_MCYCLE + cidx, ival &0xFFFF ); +csr_write_num (CSR_MCYCLE + cidx, ival &0xFFFFFFFF );
comments from last version "Sounds like an irrelevant bug fix." not addressed?
Quoted 30 lines
lib/sbi/sbi_pmu.c +6
-3csr_write_num (CSR_MCYCLEH + cidx, ival >>BITS_PER_LONG );#else csr_write_num (CSR_MCYCLE + cidx, ival);@@ -241,6 +277,7 @@ static int pmu_ctr_start_hw(uint32_t cidx, uint64_t ival, bool ival_update) {unsigned long mctr_en =csr_read (CSR_MCOUNTEREN );unsigned long mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT ); +struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();/* Make sure the counter index lies within the range and is not TM bit */ if (cidx > num_hw_ctrs || cidx ==1 )@@ -252,12 +289,14 @@ static int pmu_ctr_start_hw(uint32_t cidx, uint64_t ival, bool ival_update)__set_bit (cidx, &mctr_en);__clear_bit (cidx, &mctr_inhbt); -if (ival_update) -pmu_ctr_write_hw (cidx, ival); - +if (sbi_hart_has_feature (scratch,SBI_HART_HAS_SSCOFPMF )) +pmu_ctr_enable_irq_hw (cidx);csr_write (CSR_MCOUNTEREN , mctr_en);csr_write (CSR_MCOUNTINHIBIT , mctr_inhbt); +if (ival_update) +pmu_ctr_write_hw (cidx, ival); +return 0 ; }
lib/sbi/sbi_pmu.c static int pmu_ctr_stop_hw(uint32_t cidx) · line 329
Quoted 4 lines
lib/sbi/sbi_pmu.c +0
-1@@ -326,7 +365,6 @@ static int pmu_ctr_stop_hw(uint32_t cidx)static int pmu_ctr_stop_fw (uint32_t cidx,uint32_t fw_evt_code) {u32 hartid =current_hartid (); -
unnecessary removal
Quoted 68 lines
lib/sbi/sbi_pmu.c +28
-9fw_event_map[hartid][fw_evt_code].bStarted = FALSE;return 0 ;@@ -362,8 +400,21 @@ int sbi_pmu_ctr_stop(unsigned long cbase, unsigned long cmask,return ret; } +static void pmu_update_inhibit_flags (unsigned long flags,uint64_t *mhpmevent_val) +{ +if (flags &SBI_PMU_CFG_FLAG_SET_VUINH ) + *mhpmevent_val |=MHPMEVENT_VUINH ; +if (flags &SBI_PMU_CFG_FLAG_SET_VSINH ) + *mhpmevent_val |=MHPMEVENT_VSINH ; +if (flags &SBI_PMU_CFG_FLAG_SET_UINH ) + *mhpmevent_val |=MHPMEVENT_UINH ; +if (flags &SBI_PMU_CFG_FLAG_SET_SINH ) + *mhpmevent_val |=MHPMEVENT_SINH ; +} +static int pmu_update_hw_mhpmevent (struct sbi_pmu_hw_event *hw_evt,int ctr_idx, -unsigned long eindex,uint64_t data) +unsigned long flags,unsigned long eindex, +uint64_t data) {struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();const struct sbi_platform *plat =sbi_platform_ptr (scratch);@@ -375,17 +426,23 @@ static int pmu_update_hw_mhpmevent(struct sbi_pmu_hw_event *hw_evt, int ctr_idx,if (!mhpmevent_val || ctr_idx <3 || ctr_idx >=SBI_PMU_HW_CTR_MAX )return SBI_EFAIL ; -/* TODO: The upper 16 bits of mhpmevent is reserved by sscofpmf extension. -* Update those bits based on the flags received from supervisor. -* The OVF bit also should be cleared here in case it was not cleared -* during event stop. -*/ -csr_write_num (CSR_MCOUNTINHIBIT + ctr_idx, mhpmevent_val); +/* Always clear the OVF bit and inhibit countin of events in M-mode */ + mhpmevent_val = (mhpmevent_val & ~MHPMEVENT_SSCOF_MASK ) |MHPMEVENT_MINH ; + +/* Update the inhibit flags based on inhibit flags received from supervisor */ +pmu_update_inhibit_flags (flags, &mhpmevent_val); + +#if __riscv_xlen ==32 +csr_write_num (CSR_MHPMEVENT3 + ctr_idx -3 , mhpmevent_val &0xFFFFFFFF ); +csr_write_num (CSR_MHPMEVENT3H + ctr_idx -3 , mhpmevent_val >>BITS_PER_LONG ); +#else +csr_write_num (CSR_MHPMEVENT3 + ctr_idx -3 , mhpmevent_val); +#endif return 0 ; } -static int pmu_ctr_find_hw (unsigned long cbase,unsigned long cmask, +static int pmu_ctr_find_hw (unsigned long cbase,unsigned long cmask,unsigned long flags,unsigned long event_idx,uint64_t data) {unsigned long ctr_mask;@@ -427,7 +484,7 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask,if (ctr_idx ==SBI_ENOTSUPP )return SBI_EFAIL ; - ret =pmu_update_hw_mhpmevent (temp, ctr_idx, event_idx, data); + ret =pmu_update_hw_mhpmevent (temp, ctr_idx, flags, event_idx, data);if (!ret) ret = ctr_idx;
lib/sbi/sbi_pmu.c int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask, · line 554
Quoted 9 lines
lib/sbi/sbi_pmu.c +2
-1@@ -490,7 +547,8 @@ int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask,/* Any firmware counter can be used track any firmware event */ ctr_idx =pmu_ctr_find_fw (cidx_base, cidx_mask, hartid); }else { - ctr_idx =pmu_ctr_find_hw (cidx_base, cidx_mask, event_idx, event_data); + ctr_idx =pmu_ctr_find_hw (cidx_base, cidx_mask, flags, event_idx, + event_data); }if (ctr_idx <0 )
Otherwise,
PATCH v4 5/11 lib: sbi: Support sscofpmf extension in OpenSBI
Quoted 97 lines · Bin Meng
Quoted 95 lines · Atish Patra
This patch adds sscofpmf extension in pmu module which includes following things.
1. Enable overflow irq when starting a counter.
2. Setting the correct event filters passed from supervisor.
3. Delegating the overflow interrupt to the supervisor.
4. Add RV32 support for sscofpmf.Signed-off-by Atish Patra <[email protected]>--- include/sbi/riscv_encoding.h | 25 ++++++++++ lib/sbi/sbi_pmu.c | 88 ++++++++++++++++++++++++++++++------ 2 files changed, 98 insertions(+), 15 deletions(-)include/sbi/riscv_encoding.h +25
-0@@ -173,6 +173,31 @@#define HGATP_MODE_SHIFT HGATP32_MODE_SHIFT#endif +#if __riscv_xlen ==64 +#define MHPMEVENT_OF (_UL(1) << 63) +#define MHPMEVENT_MINH (_UL(1) << 62) +#define MHPMEVENT_SINH (_UL(1) << 61) +#define MHPMEVENT_UINH (_UL(1) << 60) +#define MHPMEVENT_VSINH (_UL(1) << 59) +#define MHPMEVENT_VUINH (_UL(1) << 58) +#else +#define MHPMEVENTH_OF (_UL(1) << 31) +#define MHPMEVENTH_MINH (_ULL(1) << 30) +#define MHPMEVENTH_SINH (_ULL(1) << 29) +#define MHPMEVENTH_UINH (_ULL(1) << 28) +#define MHPMEVENTH_VSINH (_ULL(1) << 27) +#define MHPMEVENTH_VUINH (_ULL(1) << 26) + +#define MHPMEVENT_MINH (MHPMEVENTH_MINH << 32) +#define MHPMEVENT_SINH (MHPMEVENTH_SINH << 32) +#define MHPMEVENT_UINH (MHPMEVENTH_UINH << 32) +#define MHPMEVENT_VSINH (MHPMEVENTH_VSINH << 32) +#define MHPMEVENT_VUINH (MHPMEVENTH_VUINH << 32) + +#endif + +#define MHPMEVENT_SSCOF_MASK _ULL(0xFFFF000000000000) +/* ===== User-level CSRs ===== */ /* User Trap Setup (N-extension) */ lib/sbi/sbi_pmu.c +36
-0@@ -226,11 +226,47 @@ int sbi_pmu_add_raw_event_counter_map(uint64_t select, u32 cmap) SBI_PMU_EVENT_RAW_IDX, cmap, select); } +static int pmu_ctr_enable_irq_hw (int ctr_idx) +{ +unsigned long mhpmevent_csr; +unsigned long mhpmevent_curr; +unsigned long mip_val; +unsigned long of_mask; + +if (ctr_idx <3 || ctr_idx >=SBI_PMU_HW_CTR_MAX ) +return SBI_EFAIL ; + +#if __riscv_xlen ==32 + mhpmevent_csr =CSR_MHPMEVENT3H + ctr_idx -3 ; + of_mask = ~MHPMEVENTH_OF ; +#else + mhpmevent_csr =CSR_MHPMEVENT3 + ctr_idx -3 ; + of_mask = ~MHPMEVENT_OF ; +#endif + + mhpmevent_curr =csr_read_num (mhpmevent_csr); + mip_val =csr_read (CSR_MIP ); +/** +* Clear out the OF bit so that next interrupt can be enabled. +* This should be done only when the corresponding overflow interrupt +* bit is cleared. That indicates that software has already handled the +* previous interrupts or the hardware yet to set an overflow interrupt. +* Otherwise, there will be race conditions where we may clear the bit +* the software is yet to handle the interrupt. +*/ +if (!(mip_val &MIP_LCOFIP )) { + mhpmevent_curr &= of_mask; +csr_write_num (mhpmevent_csr, mhpmevent_curr); + } + +return 0 ; +} +
Quoted 8 lines
lib/sbi/sbi_pmu.c int sbi_pmu_add_raw_event_counter_map(uint64_t select, u32 cmap) · line 269
Quoted 6 lines
lib/sbi/sbi_pmu.c +1
-1static void pmu_ctr_write_hw (uint32_t cidx,uint64_t ival) {#if __riscv_xlen ==32 csr_write_num (CSR_MCYCLE + cidx,0 ); -csr_write_num (CSR_MCYCLE + cidx, ival &0xFFFF ); +csr_write_num (CSR_MCYCLE + cidx, ival &0xFFFFFFFF );comments from last version "Sounds like an irrelevant bug fix." not addressed?
Sorry. I missed it last time. Moved to a separate patch in the next version.
Quoted 113 lines
Quoted 31 lines
lib/sbi/sbi_pmu.c +6
-3csr_write_num (CSR_MCYCLEH + cidx, ival >>BITS_PER_LONG );#else csr_write_num (CSR_MCYCLE + cidx, ival);@@ -241,6 +277,7 @@ static int pmu_ctr_start_hw(uint32_t cidx, uint64_t ival, bool ival_update) {unsigned long mctr_en =csr_read (CSR_MCOUNTEREN );unsigned long mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT ); +struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();/* Make sure the counter index lies within the range and is not TM bit */ if (cidx > num_hw_ctrs || cidx ==1 )@@ -252,12 +289,14 @@ static int pmu_ctr_start_hw(uint32_t cidx, uint64_t ival, bool ival_update)__set_bit (cidx, &mctr_en);__clear_bit (cidx, &mctr_inhbt); -if (ival_update) -pmu_ctr_write_hw (cidx, ival); - +if (sbi_hart_has_feature (scratch,SBI_HART_HAS_SSCOFPMF )) +pmu_ctr_enable_irq_hw (cidx);csr_write (CSR_MCOUNTEREN , mctr_en);csr_write (CSR_MCOUNTINHIBIT , mctr_inhbt); +if (ival_update) +pmu_ctr_write_hw (cidx, ival); +return 0 ; }lib/sbi/sbi_pmu.c static int pmu_ctr_stop_hw(uint32_t cidx) · line 329
Quoted 4 lines
lib/sbi/sbi_pmu.c +0
-1@@ -326,7 +365,6 @@ static int pmu_ctr_stop_hw(uint32_t cidx)static int pmu_ctr_stop_fw (uint32_t cidx,uint32_t fw_evt_code) {u32 hartid =current_hartid (); -unnecessary removal
Quoted 75 lines
lib/sbi/sbi_pmu.c +30
-10fw_event_map[hartid][fw_evt_code].bStarted = FALSE;return 0 ;@@ -362,8 +400,21 @@ int sbi_pmu_ctr_stop(unsigned long cbase, unsigned long cmask,return ret; } +static void pmu_update_inhibit_flags (unsigned long flags,uint64_t *mhpmevent_val) +{ +if (flags &SBI_PMU_CFG_FLAG_SET_VUINH ) + *mhpmevent_val |=MHPMEVENT_VUINH ; +if (flags &SBI_PMU_CFG_FLAG_SET_VSINH ) + *mhpmevent_val |=MHPMEVENT_VSINH ; +if (flags &SBI_PMU_CFG_FLAG_SET_UINH ) + *mhpmevent_val |=MHPMEVENT_UINH ; +if (flags &SBI_PMU_CFG_FLAG_SET_SINH ) + *mhpmevent_val |=MHPMEVENT_SINH ; +} +static int pmu_update_hw_mhpmevent (struct sbi_pmu_hw_event *hw_evt,int ctr_idx, -unsigned long eindex,uint64_t data) +unsigned long flags,unsigned long eindex, +uint64_t data) {struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();const struct sbi_platform *plat =sbi_platform_ptr (scratch);@@ -375,17 +426,23 @@ static int pmu_update_hw_mhpmevent(struct sbi_pmu_hw_event *hw_evt, int ctr_idx,if (!mhpmevent_val || ctr_idx <3 || ctr_idx >=SBI_PMU_HW_CTR_MAX )return SBI_EFAIL ; -/* TODO: The upper 16 bits of mhpmevent is reserved by sscofpmf extension. -* Update those bits based on the flags received from supervisor. -* The OVF bit also should be cleared here in case it was not cleared -* during event stop. -*/ -csr_write_num (CSR_MCOUNTINHIBIT + ctr_idx, mhpmevent_val); +/* Always clear the OVF bit and inhibit countin of events in M-mode */ + mhpmevent_val = (mhpmevent_val & ~MHPMEVENT_SSCOF_MASK ) |MHPMEVENT_MINH ; + +/* Update the inhibit flags based on inhibit flags received from supervisor */ +pmu_update_inhibit_flags (flags, &mhpmevent_val); + +#if __riscv_xlen ==32 +csr_write_num (CSR_MHPMEVENT3 + ctr_idx -3 , mhpmevent_val &0xFFFFFFFF ); +csr_write_num (CSR_MHPMEVENT3H + ctr_idx -3 , mhpmevent_val >>BITS_PER_LONG ); +#else +csr_write_num (CSR_MHPMEVENT3 + ctr_idx -3 , mhpmevent_val); +#endif return 0 ; } -static int pmu_ctr_find_hw (unsigned long cbase,unsigned long cmask, +static int pmu_ctr_find_hw (unsigned long cbase,unsigned long cmask,unsigned long flags,unsigned long event_idx,uint64_t data) {unsigned long ctr_mask;@@ -427,7 +484,7 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask,if (ctr_idx ==SBI_ENOTSUPP )return SBI_EFAIL ; - ret =pmu_update_hw_mhpmevent (temp, ctr_idx, event_idx, data); + ret =pmu_update_hw_mhpmevent (temp, ctr_idx, flags, event_idx, data);if (!ret) ret = ctr_idx;@@ -490,7 +547,8 @@ int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask,/* Any firmware counter can be used track any firmware event */ ctr_idx =pmu_ctr_find_fw (cidx_base, cidx_mask, hartid); }else { - ctr_idx =pmu_ctr_find_hw (cidx_base, cidx_mask, event_idx, event_data); + ctr_idx =pmu_ctr_find_hw (cidx_base, cidx_mask, flags, event_idx, + event_data); }
Quoted 6 lines
lib/sbi/sbi_pmu.c int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask, · line 554
Quoted 2 lines
lib/sbi/sbi_pmu.c +0
-0if (ctr_idx <0 )Otherwise,
Reviewed-by Bin Meng <[email protected]>
Thanks for the detailed review.
Quoted 4 lines
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
Signature · 2 lines
Regards, Atish
PATCH v4 6/11 lib: sbi: Always enable access for all counters
In reply to Atish PatraOpenSBI don't use any counters for its own usage. Thus, all the counters can be made accessible for lower privilege mode always. However, the mcountinhibit must be set so that the counter don't increment. As a result, we don't have to enable/disable mcounteren at every start/stop.
--- lib/sbi/sbi_hart.c | 21 +++++++-------------- lib/sbi/sbi_pmu.c | 16 ++++------------ 2 files changed, 11 insertions(+), 26 deletions(-)
lib/sbi/sbi_hart.c +7 -14
@@ -56,20 +56,13 @@ static void mstatus_init(struct sbi_scratch *scratch) sbi_hart_has_feature(scratch, SBI_HART_HAS_SCOUNTEREN))csr_write (CSR_SCOUNTEREN ,7 ); -if (sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTEREN )) { -if (sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) -/** -* Just enable the default counters (CY, TM, IR) because -* some OS (e.g FreeBSD) expect them to be enabled. -* -* All other counters will be enabled at runtime after -* S-mode request. -*/ -csr_write (CSR_MCOUNTEREN ,7 ); -else -/* Supervisor mode usage are enabled by default */ -csr_write (CSR_MCOUNTEREN ,-1 ); - } +/** +* OpenSBI doesn't use any PMU counters in M-mode. +* Supervisor mode usage for all counters are enabled by default +* But counters will not run until mcountinhibit is set. +*/ +if (sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTEREN )) +csr_write (CSR_MCOUNTEREN ,-1 );/* All programmable counters will start running at runtime after S-mode request */ if (sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT ))
lib/sbi/sbi_pmu.c +4 -12
@@ -275,7 +275,6 @@ static void pmu_ctr_write_hw(uint32_t cidx, uint64_t ival)static int pmu_ctr_start_hw (uint32_t cidx,uint64_t ival,bool ival_update) { -unsigned long mctr_en =csr_read (CSR_MCOUNTEREN );unsigned long mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT );struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();
@@ -283,15 +282,13 @@ static int pmu_ctr_start_hw(uint32_t cidx, uint64_t ival, bool ival_update)if (cidx > num_hw_ctrs || cidx ==1 )return SBI_EINVAL ; -if (__test_bit (cidx, &mctr_en) && !__test_bit (cidx, &mctr_inhbt)) +if (!__test_bit (cidx, &mctr_inhbt))return SBI_EALREADY_STARTED ; -__set_bit (cidx, &mctr_en);__clear_bit (cidx, &mctr_inhbt);if (sbi_hart_has_feature (scratch,SBI_HART_HAS_SSCOFPMF ))pmu_ctr_enable_irq_hw (cidx); -csr_write (CSR_MCOUNTEREN , mctr_en);csr_write (CSR_MCOUNTINHIBIT , mctr_inhbt);if (ival_update)
@@ -345,17 +342,14 @@ int sbi_pmu_ctr_start(unsigned long cbase, unsigned long cmask,static int pmu_ctr_stop_hw (uint32_t cidx) { -unsigned long mctr_en =csr_read (CSR_MCOUNTEREN );unsigned long mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT );/* Make sure the counter index lies within the range and is not TM bit */ if (cidx > num_hw_ctrs || cidx ==1 )return SBI_EINVAL ; -if (__test_bit (cidx, &mctr_en) && !__test_bit (cidx, &mctr_inhbt)) { +if (!__test_bit (cidx, &mctr_inhbt)) {__set_bit (cidx, &mctr_inhbt); -__clear_bit (cidx, &mctr_en); -csr_write (CSR_MCOUNTEREN , mctr_en);csr_write (CSR_MCOUNTINHIBIT , mctr_inhbt);return 0 ; }else
@@ -448,7 +442,6 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lounsigned long ctr_mask;int i, ret =0 , ctr_idx =SBI_ENOTSUPP ;struct sbi_pmu_hw_event *temp; -unsigned long mctr_en =csr_read (CSR_MCOUNTEREN );unsigned long mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT );int evt_idx_code =get_cidx_code (event_idx);
@@ -473,8 +466,7 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lo ctr_mask = temp->counters & (cmask << cbase);for_each_set_bit_from (cbase, &ctr_mask,SBI_PMU_HW_CTR_MAX ) { -if (!__test_bit (cbase, &mctr_en) && -__test_bit (cbase, &mctr_inhbt)) { +if (__test_bit (cbase, &mctr_inhbt)) { ctr_idx = cbase;break ; }
@@ -646,7 +638,7 @@ void sbi_pmu_exit(struct sbi_scratch *scratch)return ;csr_write (CSR_MCOUNTINHIBIT ,0xFFFFFFF8 ); -csr_write (CSR_MCOUNTEREN ,7 ); +csr_write (CSR_MCOUNTEREN ,-1 );pmu_reset_event_map (hartid); }
Signature · 1 line
2.31.1
PATCH v4 6/11 lib: sbi: Always enable access for all counters
Quoted 2 lines · Atish Patra
OpenSBI don't use any counters for its own usage. Thus, all the counters
This was wrongly replaced. Should be "OpenSBI doesn't". I was asking below change:
Quoted 2 lines
can be made accessible for lower privilege mode always. However, the mcountinhibit must be set so that the counter don't increment.
the counter doesn't increment
Quoted 10 lines
As a result, we don't have to enable/disable mcounteren at every start/stop.
Reviewed-by Bin Meng <[email protected]>Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- lib/sbi/sbi_hart.c | 21 +++++++-------------- lib/sbi/sbi_pmu.c | 16 ++++------------ 2 files changed, 11 insertions(+), 26 deletions(-)
Regards,
Bin
PATCH v4 7/11 lib: sbi: Allow programmable counters to monitor cycle/instret events
In reply to Atish PatraA platform may use programmable counters for cycle/instret events. The priv spec allows that provided that cycle/instret also report those events in addition to the programmable counters. We should allow that functionality in OpenSBI.
--- include/sbi/sbi_pmu.h | 1 + lib/sbi/sbi_pmu.c | 60 +++++++++++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 16 deletions(-)
include/sbi/sbi_pmu.h +1 -0
@@ -26,6 +26,7 @@#define SBI_PMU_FW_CTR_MAX 16#define SBI_PMU_HW_CTR_MAX 32#define SBI_PMU_CTR_MAX (SBI_PMU_HW_CTR_MAX + SBI_PMU_FW_CTR_MAX) +#define SBI_PMU_FIXED_CTR_MASK 0x07/** Initialize PMU */ int sbi_pmu_init (struct sbi_scratch *scratch,bool cold_boot);
lib/sbi/sbi_pmu.c +44 -16
110 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v4 8/11 lib: sbi: Reset the mhpmevent value upon counter reset
In reply to Atish PatraThe hardware solely relies on the event selector value in mhpmevent to figure out what event to monitor using that counter. It should be reset when counter reset happens.
--- lib/sbi/sbi_pmu.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-)
lib/sbi/sbi_pmu.c +17 -1
@@ -368,6 +368,20 @@ static int pmu_ctr_stop_fw(uint32_t cidx, uint32_t fw_evt_code)return 0 ; } +static int pmu_reset_hw_mhpmevent (int ctr_idx) +{ +if (ctr_idx <3 || ctr_idx >=SBI_PMU_HW_CTR_MAX ) +return SBI_EFAIL ; +#if __riscv_xlen ==32 +csr_write_num (CSR_MHPMEVENT3 + ctr_idx -3 ,0 ); +csr_write_num (CSR_MHPMEVENT3H + ctr_idx -3 ,0 ); +#else +csr_write_num (CSR_MHPMEVENT3 + ctr_idx -3 ,0 ); +#endif + +return 0 ; +} +int sbi_pmu_ctr_stop (unsigned long cbase,unsigned long cmask,unsigned long flag) {
@@ -391,8 +405,10 @@ int sbi_pmu_ctr_stop(unsigned long cbase, unsigned long cmask,else ret =pmu_ctr_stop_hw (cbase); -if (!ret && (flag &SBI_PMU_STOP_FLAG_RESET )) +if (flag &SBI_PMU_STOP_FLAG_RESET ) { active_events[hartid][cbase] =SBI_PMU_EVENT_IDX_INVALID ; +pmu_reset_hw_mhpmevent (cbase); + } }return ret;
Signature · 1 line
2.31.1
PATCH v4 9/11 lib: sbi: Counter info width should be zero indexed
In reply to Atish PatraThe mhpm bits represent the number of bits available in mhpmcounter while counter width describes a zero indexed value. Fix the counter width calculation.
--- lib/sbi/sbi_pmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_pmu.c +1 -1
@@ -648,7 +648,7 @@ int sbi_pmu_ctr_get_info(uint32_t cidx, unsigned long *ctr_info)if (cidx ==0 || cidx ==2 ) cinfo.width =63 ;else - cinfo.width =sbi_hart_mhpm_bits (scratch); + cinfo.width =sbi_hart_mhpm_bits (scratch) -1 ; }else {/* it's a firmware counter */ cinfo.type =SBI_PMU_CTR_TYPE_FW ;
Signature · 1 line
2.31.1
PATCH v4 10/11 lib: sbi: Enable PMU extension for platforms without mcountinhibit
In reply to Atish PatraSome platforms such as hifive unmatched doesn't implement mcountinhibit csr. However, it has hardware events that can be monitored using 2 hpmcounter it has (i.e. mhpmcounter3 & mhpmcounter4).
Currently, PMU extension disabled if mcountinhibit is absent. That's not really necessary as long as the supervisor OS keeps track of the delta value of the counters. Without mcountinhibit, the delta value won't be entirely accurate because the counters are freely running. However, that should be fine to produce an approximate counter value which can help performance analysis. Perf sampling won't work though as sscof extension is not present in hifive unmatched.
--- lib/sbi/sbi_ecall_pmu.c | 10 ++------- lib/sbi/sbi_pmu.c | 48 +++++++++++++++++++++++++++++------------ lib/utils/fdt/fdt_pmu.c | 4 ++++ 3 files changed, 40 insertions(+), 22 deletions(-)
lib/sbi/sbi_ecall_pmu.c +2 -8
@@ -74,14 +74,8 @@ static int sbi_ecall_pmu_handler(unsigned long extid, unsigned long funcid,static int sbi_ecall_pmu_probe (unsigned long extid,unsigned long *out_val) { -struct sbi_scratch *scratch =sbi_scratch_thishart_ptr (); - -/* SBI PMU extension is useless without mcount inhibit features */ -if (sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) - *out_val =1 ; -else - *out_val =0 ; - +/* PMU extension is always enabled */ + *out_val =1 ;return 0 ; }
lib/sbi/sbi_pmu.c +34 -14
105 lines not on this page · raw message
lib/utils/fdt/fdt_pmu.c +4 -0
@@ -9,6 +9,7 @@*/ #include <libfdt.h> +#include <sbi/sbi_hart.h> #include <sbi/sbi_error.h> #include <sbi/sbi_pmu.h> #include <sbi_utils/fdt/fdt_helper.h>
@@ -40,6 +41,7 @@ uint64_t fdt_pmu_get_select_value(uint32_t event_idx)int fdt_pmu_fixup (void *fdt) {int pmu_offset; +struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();if (!fdt)return SBI_EINVAL ;
@@ -51,6 +53,8 @@ int fdt_pmu_fixup(void *fdt)fdt_delprop (fdt, pmu_offset,"pmu,event-to-mhpmcounters" );fdt_delprop (fdt, pmu_offset,"pmu,event-to-mhpmevent" );fdt_delprop (fdt, pmu_offset,"pmu,raw-event-to-mhpmcounters" ); +if (!sbi_hart_has_feature (scratch,SBI_HART_HAS_SSCOFPMF )) +fdt_delprop (fdt, pmu_offset,"interrupts-extended" );return 0 ; }
Signature · 1 line
2.31.1
PATCH v4 10/11 lib: sbi: Enable PMU extension for platforms without mcountinhibit
Quoted 78 lines · Atish Patra
Some platforms such as hifive unmatched doesn't implement mcountinhibit csr. However, it has hardware events that can be monitored using 2 hpmcounter it has (i.e. mhpmcounter3 & mhpmcounter4).
Currently, PMU extension disabled if mcountinhibit is absent. That's not really necessary as long as the supervisor OS keeps track of the delta value of the counters. Without mcountinhibit, the delta value won't be entirely accurate because the counters are freely running. However, that should be fine to produce an approximate counter value which can help performance analysis. Perf sampling won't work though as sscof extension is not present in hifive unmatched.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- lib/sbi/sbi_ecall_pmu.c | 10 ++------- lib/sbi/sbi_pmu.c | 48 +++++++++++++++++++++++++++++------------ lib/utils/fdt/fdt_pmu.c | 4 ++++ 3 files changed, 40 insertions(+), 22 deletions(-)lib/sbi/sbi_ecall_pmu.c +2
-8@@ -74,14 +74,8 @@ static int sbi_ecall_pmu_handler(unsigned long extid, unsigned long funcid,static int sbi_ecall_pmu_probe (unsigned long extid,unsigned long *out_val) { -struct sbi_scratch *scratch =sbi_scratch_thishart_ptr (); - -/* SBI PMU extension is useless without mcount inhibit features */ -if (sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) - *out_val =1 ; -else - *out_val =0 ; - +/* PMU extension is always enabled */ + *out_val =1 ;return 0 ; }lib/sbi/sbi_pmu.c +10
-1@@ -279,13 +279,21 @@ static void pmu_ctr_write_hw(uint32_t cidx, uint64_t ival)static int pmu_ctr_start_hw (uint32_t cidx,uint64_t ival,bool ival_update) { -unsigned long mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT );struct sbi_scratch *scratch =sbi_scratch_thishart_ptr (); +unsigned long mctr_inhbt;/* Make sure the counter index lies within the range and is not TM bit */ if (cidx > num_hw_ctrs || cidx ==1 )return SBI_EINVAL ; +if (!sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) + goto skip_inhibit_update; + +/* +* Some of the hardware may not support mcountinhibit but perf stat +* still can work if supervisor mode programs the initial value. +*/ + mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT );if (!__test_bit (cidx, &mctr_inhbt))return SBI_EALREADY_STARTED ;@@ -295,6 +303,7 @@ static int pmu_ctr_start_hw(uint32_t cidx, uint64_t ival, bool ival_update)pmu_ctr_enable_irq_hw (cidx);csr_write (CSR_MCOUNTINHIBIT , mctr_inhbt); +skip_inhibit_update:if (ival_update)pmu_ctr_write_hw (cidx, ival);
lib/sbi/sbi_pmu.c int sbi_pmu_ctr_start(unsigned long cbase, unsigned long cmask, · line 359
Quoted 5 lines
lib/sbi/sbi_pmu.c +1
-0@@ -346,8 +355,14 @@ int sbi_pmu_ctr_start(unsigned long cbase, unsigned long cmask,static int pmu_ctr_stop_hw (uint32_t cidx) { +struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();unsigned long mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT );
The csr_read() should be dropped otherwise it will cause illegal instruction.
Quoted 97 lines
lib/sbi/sbi_pmu.c +23
-13+if (!sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) +return 0 ; + + mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT ); +/* Make sure the counter index lies within the range and is not TM bit */ if (cidx > num_hw_ctrs || cidx ==1 )return SBI_EINVAL ;@@ -473,7 +488,8 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lounsigned long ctr_mask;int i, ret =0 , fixed_ctr, ctr_idx =SBI_ENOTSUPP ;struct sbi_pmu_hw_event *temp; -unsigned long mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT ); +unsigned long mctr_inhbt; +u32 hartid =current_hartid ();struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();if (cbase > num_hw_ctrs)@@ -488,6 +504,8 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lo !sbi_hart_has_feature(scratch, SBI_HART_HAS_SSCOFPMF))return fixed_ctr; +if (sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) + mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT );for (i =0 ; i < num_hw_events; i++) { temp = &hw_event_map[i]; if ((temp->start_idx > event_idx && event_idx < temp->end_idx) ||@@ -502,10 +520,18 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lo ctr_mask = temp->counters & (cmask << cbase) & (~SBI_PMU_FIXED_CTR_MASK );for_each_set_bit_from (cbase, &ctr_mask,SBI_PMU_HW_CTR_MAX ) { -if (__test_bit (cbase, &mctr_inhbt)) { - ctr_idx = cbase; -break ; - } +/** +* Some of the platform may not support mcountinhibit. +* Checking the active_events is enough for them +*/ +if (active_events[hartid][cbase] !=SBI_PMU_EVENT_IDX_INVALID ) +continue ; +/* If mcountinhibit is supported, the bit must be enabled */ +if ((sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) && + !__test_bit (cbase, &mctr_inhbt)) +continue ; +/* We found a valid counter that is not started yet */ + ctr_idx = cbase; } }@@ -677,11 +703,9 @@ void sbi_pmu_exit(struct sbi_scratch *scratch) {u32 hartid =current_hartid (); -/* SBI PMU is not supported if mcountinhibit is not available */ -if (!sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) -return ; +if (sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) +csr_write (CSR_MCOUNTINHIBIT ,0xFFFFFFF8 ); -csr_write (CSR_MCOUNTINHIBIT ,0xFFFFFFF8 );csr_write (CSR_MCOUNTEREN ,-1 );pmu_reset_event_map (hartid); }@@ -691,10 +715,6 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot)const struct sbi_platform *plat;u32 hartid =current_hartid (); -/* SBI PMU is not supported if mcountinhibit is not available */ -if (!sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) -return 0 ; -if (cold_boot) { plat =sbi_platform_ptr (scratch);/* Initialize hw pmu events */ lib/utils/fdt/fdt_pmu.c +2
-0@@ -9,6 +9,7 @@*/ #include <libfdt.h> +#include <sbi/sbi_hart.h> #include <sbi/sbi_error.h> #include <sbi/sbi_pmu.h> #include <sbi_utils/fdt/fdt_helper.h> @@ -40,6 +41,7 @@ uint64_t fdt_pmu_get_select_value(uint32_t event_idx)int fdt_pmu_fixup (void *fdt) {int pmu_offset; +struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();if (!fdt)return SBI_EINVAL ;
lib/utils/fdt/fdt_pmu.c int fdt_pmu_fixup(void *fdt) · line 60
Quoted 8 lines
lib/utils/fdt/fdt_pmu.c +2
-0@@ -51,6 +53,8 @@ int fdt_pmu_fixup(void *fdt)fdt_delprop (fdt, pmu_offset,"pmu,event-to-mhpmcounters" );fdt_delprop (fdt, pmu_offset,"pmu,event-to-mhpmevent" );fdt_delprop (fdt, pmu_offset,"pmu,raw-event-to-mhpmcounters" ); +if (!sbi_hart_has_feature (scratch,SBI_HART_HAS_SSCOFPMF )) +fdt_delprop (fdt, pmu_offset,"interrupts-extended" );return 0 ; }
Otherwise,
Regards,
Bin
PATCH v4 10/11 lib: sbi: Enable PMU extension for platforms without mcountinhibit
Quoted 79 lines · Bin Meng
Quoted 77 lines · Atish Patra
Some platforms such as hifive unmatched doesn't implement mcountinhibit csr. However, it has hardware events that can be monitored using 2 hpmcounter it has (i.e. mhpmcounter3 & mhpmcounter4).
Currently, PMU extension disabled if mcountinhibit is absent. That's not really necessary as long as the supervisor OS keeps track of the delta value of the counters. Without mcountinhibit, the delta value won't be entirely accurate because the counters are freely running. However, that should be fine to produce an approximate counter value which can help performance analysis. Perf sampling won't work though as sscof extension is not present in hifive unmatched.
Reviewed-by Anup Patel <[email protected]>Signed-off-by Atish Patra <[email protected]>--- lib/sbi/sbi_ecall_pmu.c | 10 ++------- lib/sbi/sbi_pmu.c | 48 +++++++++++++++++++++++++++++------------ lib/utils/fdt/fdt_pmu.c | 4 ++++ 3 files changed, 40 insertions(+), 22 deletions(-)lib/sbi/sbi_ecall_pmu.c +2
-8@@ -74,14 +74,8 @@ static int sbi_ecall_pmu_handler(unsigned long extid, unsigned long funcid,static int sbi_ecall_pmu_probe (unsigned long extid,unsigned long *out_val) { -struct sbi_scratch *scratch =sbi_scratch_thishart_ptr (); - -/* SBI PMU extension is useless without mcount inhibit features */ -if (sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) - *out_val =1 ; -else - *out_val =0 ; - +/* PMU extension is always enabled */ + *out_val =1 ;return 0 ; }lib/sbi/sbi_pmu.c +10
-1@@ -279,13 +279,21 @@ static void pmu_ctr_write_hw(uint32_t cidx, uint64_t ival)static int pmu_ctr_start_hw (uint32_t cidx,uint64_t ival,bool ival_update) { -unsigned long mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT );struct sbi_scratch *scratch =sbi_scratch_thishart_ptr (); +unsigned long mctr_inhbt;/* Make sure the counter index lies within the range and is not TM bit */ if (cidx > num_hw_ctrs || cidx ==1 )return SBI_EINVAL ; +if (!sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) + goto skip_inhibit_update; + +/* +* Some of the hardware may not support mcountinhibit but perf stat +* still can work if supervisor mode programs the initial value. +*/ + mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT );if (!__test_bit (cidx, &mctr_inhbt))return SBI_EALREADY_STARTED ;@@ -295,6 +303,7 @@ static int pmu_ctr_start_hw(uint32_t cidx, uint64_t ival, bool ival_update)pmu_ctr_enable_irq_hw (cidx);csr_write (CSR_MCOUNTINHIBIT , mctr_inhbt); +skip_inhibit_update:if (ival_update)pmu_ctr_write_hw (cidx, ival);
Quoted 9 lines
lib/sbi/sbi_pmu.c int sbi_pmu_ctr_start(unsigned long cbase, unsigned long cmask, · line 359
Quoted 6 lines
lib/sbi/sbi_pmu.c +1
-0@@ -346,8 +355,14 @@ int sbi_pmu_ctr_start(unsigned long cbase, unsigned long cmask,static int pmu_ctr_stop_hw (uint32_t cidx) { +struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();unsigned long mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT );The
csr_read()should be dropped otherwise it will cause illegal instruction.
Arrgh. This was supposed to be a variable declaration only. I got it wrong during
copy/paste. My bad. Will send the new version shortly.
Quoted 116 lines
Quoted 97 lines
lib/sbi/sbi_pmu.c +23
-13+if (!sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) +return 0 ; + + mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT ); +/* Make sure the counter index lies within the range and is not TM bit */ if (cidx > num_hw_ctrs || cidx ==1 )return SBI_EINVAL ;@@ -473,7 +488,8 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lounsigned long ctr_mask;int i, ret =0 , fixed_ctr, ctr_idx =SBI_ENOTSUPP ;struct sbi_pmu_hw_event *temp; -unsigned long mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT ); +unsigned long mctr_inhbt; +u32 hartid =current_hartid ();struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();if (cbase > num_hw_ctrs)@@ -488,6 +504,8 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lo !sbi_hart_has_feature(scratch, SBI_HART_HAS_SSCOFPMF))return fixed_ctr; +if (sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) + mctr_inhbt =csr_read (CSR_MCOUNTINHIBIT );for (i =0 ; i < num_hw_events; i++) { temp = &hw_event_map[i]; if ((temp->start_idx > event_idx && event_idx < temp->end_idx) ||@@ -502,10 +520,18 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lo ctr_mask = temp->counters & (cmask << cbase) & (~SBI_PMU_FIXED_CTR_MASK );for_each_set_bit_from (cbase, &ctr_mask,SBI_PMU_HW_CTR_MAX ) { -if (__test_bit (cbase, &mctr_inhbt)) { - ctr_idx = cbase; -break ; - } +/** +* Some of the platform may not support mcountinhibit. +* Checking the active_events is enough for them +*/ +if (active_events[hartid][cbase] !=SBI_PMU_EVENT_IDX_INVALID ) +continue ; +/* If mcountinhibit is supported, the bit must be enabled */ +if ((sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) && + !__test_bit (cbase, &mctr_inhbt)) +continue ; +/* We found a valid counter that is not started yet */ + ctr_idx = cbase; } }@@ -677,11 +703,9 @@ void sbi_pmu_exit(struct sbi_scratch *scratch) {u32 hartid =current_hartid (); -/* SBI PMU is not supported if mcountinhibit is not available */ -if (!sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) -return ; +if (sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) +csr_write (CSR_MCOUNTINHIBIT ,0xFFFFFFF8 ); -csr_write (CSR_MCOUNTINHIBIT ,0xFFFFFFF8 );csr_write (CSR_MCOUNTEREN ,-1 );pmu_reset_event_map (hartid); }@@ -691,10 +715,6 @@ int sbi_pmu_init(struct sbi_scratch *scratch, bool cold_boot)const struct sbi_platform *plat;u32 hartid =current_hartid (); -/* SBI PMU is not supported if mcountinhibit is not available */ -if (!sbi_hart_has_feature (scratch,SBI_HART_HAS_MCOUNTINHIBIT )) -return 0 ; -if (cold_boot) { plat =sbi_platform_ptr (scratch);/* Initialize hw pmu events */ lib/utils/fdt/fdt_pmu.c +2
-0@@ -9,6 +9,7 @@*/ #include <libfdt.h> +#include <sbi/sbi_hart.h> #include <sbi/sbi_error.h> #include <sbi/sbi_pmu.h> #include <sbi_utils/fdt/fdt_helper.h> @@ -40,6 +41,7 @@ uint64_t fdt_pmu_get_select_value(uint32_t event_idx)int fdt_pmu_fixup (void *fdt) {int pmu_offset; +struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();if (!fdt)return SBI_EINVAL ;lib/utils/fdt/fdt_pmu.c int fdt_pmu_fixup(void *fdt) · line 60
Quoted 8 lines
lib/utils/fdt/fdt_pmu.c +2
-0@@ -51,6 +53,8 @@ int fdt_pmu_fixup(void *fdt)fdt_delprop (fdt, pmu_offset,"pmu,event-to-mhpmcounters" );fdt_delprop (fdt, pmu_offset,"pmu,event-to-mhpmevent" );fdt_delprop (fdt, pmu_offset,"pmu,raw-event-to-mhpmcounters" ); +if (!sbi_hart_has_feature (scratch,SBI_HART_HAS_SSCOFPMF )) +fdt_delprop (fdt, pmu_offset,"interrupts-extended" );return 0 ; }Otherwise,
Reviewed-by Bin Meng <[email protected]>Regards,
Bin--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
Signature · 2 lines
Regards, Atish
PATCH v4 11/11 lib: utils: Rename the prefix in PMU DT properties
In reply to Atish PatraAs per the DT schema rules, the prefix should be vendor. As the PMU properties are generic for all vendors, change the prefix to riscv instead of pmu.
--- docs/pmu_support.md | 12 ++++++------ lib/utils/fdt/fdt_pmu.c | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-)
docs/pmu_support.md +6 -6
@@ -31,7 +31,7 @@ bindings in details. * **compatible** (Mandatory) - The compatible string of SBI PMU device tree node. This DT property must have the value **riscv,pmu**. -* **pmu,event-to-mhpmevent**(Optional) - It represents an ONE-to-ONE mapping +* **riscv,event-to-mhpmevent**(Optional) - It represents an ONE-to-ONE mapping between a PMU event and the event selector value that platform expects to be written to the MHPMEVENTx CSR for that event. The mapping is encoded in a table format where each row represents an event. The first column represent the
@@ -39,7 +39,7 @@ event idx where the 2nd & 3rd column represent the event selector value that should be encoded in the expected value to be written in MHPMEVENTx. This property shouldn't encode any raw hardware event. -* **pmu,event-to-mhpmcounters**(Optional) - It represents a MANY-to-MANY +* **riscv,event-to-mhpmcounters**(Optional) - It represents a MANY-to-MANY mapping between a range of events and all the MHPMCOUNTERx in a bitmap format that can be used to monitor these range of events. The information is encoded in a table format where each row represent a certain range of events and
@@ -49,7 +49,7 @@ represent a bitmap of all the MHPMCOUNTERx. This property is mandatory if event-to-mhpmevent is present. Otherwise, it can be omitted. This property shouldn't encode any raw event. -* **pmu,raw-event-to-mhpmcounters**(Optional) - It represents an ONE-to-MANY +* **riscv,raw-event-to-mhpmcounters**(Optional) - It represents an ONE-to-MANY mapping between a raw event and all the MHPMCOUNTERx in a bitmap format that can be used to monitor that raw event. The information is encoded in a table format where each raw represent a specific raw event. The first column stores the
@@ -67,12 +67,12 @@ pmu { compatible = "riscv,pmu"; interrupts = <0x100>; interrupt-parent = <&plic> - pmu,event-to-mhpmevent = <0x0000B 0x0000 0x0001>, - pmu,event-to-mhpmcounters = <0x00001 0x00001 0x00000001>, + riscv,event-to-mhpmevent = <0x0000B 0x0000 0x0001>, + riscv,event-to-mhpmcounters = <0x00001 0x00001 0x00000001>, <0x00002 0x00002 0x00000004>, <0x00003 0x0000A 0x00000ff8>, <0x10000 0x10033 0x000ff000>, - pmu,raw-event-to-mhpmcounters = <0x0000 0x0002 0x00000f8>, + riscv,raw-event-to-mhpmcounters = <0x0000 0x0002 0x00000f8>, <0x0000 0x0003 0x00000ff0>, };
lib/utils/fdt/fdt_pmu.c +6 -6
@@ -50,9 +50,9 @@ int fdt_pmu_fixup(void *fdt)if (pmu_offset <0 )return SBI_EFAIL ; -fdt_delprop (fdt, pmu_offset,"pmu,event-to-mhpmcounters" ); -fdt_delprop (fdt, pmu_offset,"pmu,event-to-mhpmevent" ); -fdt_delprop (fdt, pmu_offset,"pmu,raw-event-to-mhpmcounters" ); +fdt_delprop (fdt, pmu_offset,"riscv,event-to-mhpmcounters" ); +fdt_delprop (fdt, pmu_offset,"riscv,event-to-mhpmevent" ); +fdt_delprop (fdt, pmu_offset,"riscv,raw-event-to-mhpmcounters" );if (!sbi_hart_has_feature (scratch,SBI_HART_HAS_SSCOFPMF ))fdt_delprop (fdt, pmu_offset,"interrupts-extended" );
@@ -75,7 +75,7 @@ int fdt_pmu_setup(void *fdt)if (pmu_offset <0 )return SBI_EFAIL ; - event_ctr_map =fdt_getprop (fdt, pmu_offset,"pmu,event-to-mhpmcounters" , &len); + event_ctr_map =fdt_getprop (fdt, pmu_offset,"riscv,event-to-mhpmcounters" , &len);if (!event_ctr_map || len <8 )return SBI_EFAIL ; len = len / (sizeof (u32) *3 );
@@ -86,7 +86,7 @@ int fdt_pmu_setup(void *fdt)sbi_pmu_add_hw_event_counter_map (event_idx_start, event_idx_end, ctr_map); } - event_val =fdt_getprop (fdt, pmu_offset,"pmu,event-to-mhpmevent" , &len); + event_val =fdt_getprop (fdt, pmu_offset,"riscv,event-to-mhpmevent" , &len);if (!event_val || len <8 )return SBI_EFAIL ; len = len / (sizeof (u32) *3 );
@@ -98,7 +98,7 @@ int fdt_pmu_setup(void *fdt) hw_event_count++; } - event_val =fdt_getprop (fdt, pmu_offset,"pmu,raw-event-to-mhpmcounters" , &len); + event_val =fdt_getprop (fdt, pmu_offset,"riscv,raw-event-to-mhpmcounters" , &len);if (!event_val || len <8 )return SBI_EFAIL ; len = len / (sizeof (u32) *3 );
Signature · 1 line
2.31.1
PATCH v4 11/11 lib: utils: Rename the prefix in PMU DT properties
Quoted 6 lines · Atish Patra
As per the DT schema rules, the prefix should be vendor. As the PMU properties are generic for all vendors, change the prefix to riscv instead of pmu.
Signed-off-by Atish Patra <[email protected]>
I had already reviewed this patch. Please preserve reviewed-by tags.
Regards,
Anup
Quoted 104 lines
--- docs/pmu_support.md | 12 ++++++------ lib/utils/fdt/fdt_pmu.c | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-)docs/pmu_support.md +6
-6@@ -31,7 +31,7 @@ bindings in details. * **compatible** (Mandatory) - The compatible string of SBI PMU device tree node. This DT property must have the value **riscv,pmu**. -* **pmu,event-to-mhpmevent**(Optional) - It represents an ONE-to-ONE mapping +* **riscv,event-to-mhpmevent**(Optional) - It represents an ONE-to-ONE mapping between a PMU event and the event selector value that platform expects to be written to the MHPMEVENTx CSR for that event. The mapping is encoded in a table format where each row represents an event. The first column represent the@@ -39,7 +39,7 @@ event idx where the 2nd & 3rd column represent the event selector value that should be encoded in the expected value to be written in MHPMEVENTx. This property shouldn't encode any raw hardware event. -* **pmu,event-to-mhpmcounters**(Optional) - It represents a MANY-to-MANY +* **riscv,event-to-mhpmcounters**(Optional) - It represents a MANY-to-MANY mapping between a range of events and all the MHPMCOUNTERx in a bitmap format that can be used to monitor these range of events. The information is encoded in a table format where each row represent a certain range of events and@@ -49,7 +49,7 @@ represent a bitmap of all the MHPMCOUNTERx. This property is mandatory if event-to-mhpmevent is present. Otherwise, it can be omitted. This property shouldn't encode any raw event. -* **pmu,raw-event-to-mhpmcounters**(Optional) - It represents an ONE-to-MANY +* **riscv,raw-event-to-mhpmcounters**(Optional) - It represents an ONE-to-MANY mapping between a raw event and all the MHPMCOUNTERx in a bitmap format that can be used to monitor that raw event. The information is encoded in a table format where each raw represent a specific raw event. The first column stores the@@ -67,12 +67,12 @@ pmu { compatible = "riscv,pmu"; interrupts = <0x100>; interrupt-parent = <&plic> - pmu,event-to-mhpmevent = <0x0000B 0x0000 0x0001>, - pmu,event-to-mhpmcounters = <0x00001 0x00001 0x00000001>, + riscv,event-to-mhpmevent = <0x0000B 0x0000 0x0001>, + riscv,event-to-mhpmcounters = <0x00001 0x00001 0x00000001>, <0x00002 0x00002 0x00000004>, <0x00003 0x0000A 0x00000ff8>, <0x10000 0x10033 0x000ff000>, - pmu,raw-event-to-mhpmcounters = <0x0000 0x0002 0x00000f8>, + riscv,raw-event-to-mhpmcounters = <0x0000 0x0002 0x00000f8>, <0x0000 0x0003 0x00000ff0>, };lib/utils/fdt/fdt_pmu.c +6
-7@@ -50,9 +50,9 @@ int fdt_pmu_fixup(void *fdt)if (pmu_offset <0 )return SBI_EFAIL ; -fdt_delprop (fdt, pmu_offset,"pmu,event-to-mhpmcounters" ); -fdt_delprop (fdt, pmu_offset,"pmu,event-to-mhpmevent" ); -fdt_delprop (fdt, pmu_offset,"pmu,raw-event-to-mhpmcounters" ); +fdt_delprop (fdt, pmu_offset,"riscv,event-to-mhpmcounters" ); +fdt_delprop (fdt, pmu_offset,"riscv,event-to-mhpmevent" ); +fdt_delprop (fdt, pmu_offset,"riscv,raw-event-to-mhpmcounters" );if (!sbi_hart_has_feature (scratch,SBI_HART_HAS_SSCOFPMF ))fdt_delprop (fdt, pmu_offset,"interrupts-extended" );@@ -75,7 +75,7 @@ int fdt_pmu_setup(void *fdt)if (pmu_offset <0 )return SBI_EFAIL ; - event_ctr_map =fdt_getprop (fdt, pmu_offset,"pmu,event-to-mhpmcounters" , &len); + event_ctr_map =fdt_getprop (fdt, pmu_offset,"riscv,event-to-mhpmcounters" , &len);if (!event_ctr_map || len <8 )return SBI_EFAIL ; len = len / (sizeof (u32) *3 );@@ -86,7 +86,7 @@ int fdt_pmu_setup(void *fdt)sbi_pmu_add_hw_event_counter_map (event_idx_start, event_idx_end, ctr_map); } - event_val =fdt_getprop (fdt, pmu_offset,"pmu,event-to-mhpmevent" , &len); + event_val =fdt_getprop (fdt, pmu_offset,"riscv,event-to-mhpmevent" , &len);if (!event_val || len <8 )return SBI_EFAIL ; len = len / (sizeof (u32) *3 );@@ -98,7 +98,7 @@ int fdt_pmu_setup(void *fdt) hw_event_count++; } - event_val =fdt_getprop (fdt, pmu_offset,"pmu,raw-event-to-mhpmcounters" , &len); + event_val =fdt_getprop (fdt, pmu_offset,"riscv,raw-event-to-mhpmcounters" , &len);if (!event_val || len <8 )return SBI_EFAIL ; len = len / (sizeof (u32) *3 ); --2.31.1
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
PATCH v4 11/11 lib: utils: Rename the prefix in PMU DT properties
In reply to Atish PatraQuoted 11 lines · Atish Patra
As per the DT schema rules, the prefix should be vendor. As the PMU properties are generic for all vendors, change the prefix to riscv instead of pmu.
Signed-off-by Atish Patra <[email protected]>--- docs/pmu_support.md | 12 ++++++------ lib/utils/fdt/fdt_pmu.c | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-)