Skip to contentSkip to conversation

Sscof extension support

PATCH New Atish Patra 4 participants 27 messages Nov 8, 2021 – Nov 11, 2021 RISC-V

lore
Share & tools
lore
#0 lore raw

v5 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 v4->v5:
1. Preserved the Reviewed-by tag.
2. Fix a typo in patch 10 around variable declaration.
3. Created a separate patch for a bug fix.

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_v5

Atish Patra (12):
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
lib: sbi: Fix initial value mask while updating the counters

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            | 229 ++++++++++++++++++++++++++---------
lib/utils/fdt/fdt_pmu.c      |  16 ++-
9 files changed, 290 insertions(+), 92 deletions(-)

--
2.31.1

#1 lore raw

PATCH v5 1/12 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
index e1d0b463c668..14caa95d66de 100644
@@ -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
Applied
#2 lore raw

PATCH v5 1/12 riscv: Add new CSRs introduced by Sscofpmf[1] extension

Quoted 8 lines · Atish Patra

[1] https://drive.google.com/file/d/1KcjgbLM5L1ZKY8934aJl8aQwGlMz6Cbo/view

---

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 56 lines
 include/sbi/riscv_encoding.h | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
include/sbi/riscv_encoding.h +34 -1
index e1d0b463c668..14caa95d66de 100644
@@ -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
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#3 lore raw

PATCH v5 2/12 lib: sbi: Update csr_read/write_num for PMU

In reply to Atish Patra

The 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
index 4c24a5132195..ea6dd0837774 100644
@@ -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
Applied
#4 lore raw

PATCH v5 2/12 lib: sbi: Update csr_read/write_num for PMU

Quoted 10 lines · Atish Patra

The 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.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 50 lines
---
 lib/sbi/riscv_asm.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
lib/sbi/riscv_asm.c +13 -1
index 4c24a5132195..ea6dd0837774 100644
@@ -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)
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#5 lore raw

PATCH v5 3/12 lib: sbi: Detect Sscofpmf extension at run time

In reply to Atish Patra

Sscofpmf 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
index 0c18ef9325b2..d2db9d6e1fa1 100644
@@ -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
index 6254452e11bb..1d73cd73b166 100644
@@ -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
Applied
#6 lore raw

PATCH v5 3/12 lib: sbi: Detect Sscofpmf extension at run time

Quoted 6 lines · Atish Patra

Sscofpmf 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.

Quoted 6 lines

Add a dynamic detection mechanism and boot time print message if sscofpmf is present.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 59 lines
---
 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
index 0c18ef9325b2..d2db9d6e1fa1 100644
@@ -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 -1
index 6254452e11bb..1d73cd73b166 100644
@@ -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)
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#7 lore raw

PATCH v5 4/12 lib: sbi: Delegate PMU counter overflow interrupt to S mode

In reply to Atish Patra

OpenSBI 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
index 14caa95d66de..ca6343537eba 100644
@@ -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
index 1d73cd73b166..fccd19b7eb31 100644
@@ -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
Applied
#8 lore raw

PATCH v5 4/12 lib: sbi: Delegate PMU counter overflow interrupt to S mode

Quoted 8 lines · Atish Patra

OpenSBI doesn't handle PMU counters for now.

Delegate the overflow counter to S-mode always.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 47 lines
---
 include/sbi/riscv_encoding.h | 2 ++
 lib/sbi/sbi_hart.c           | 3 +++
 2 files changed, 5 insertions(+)
include/sbi/riscv_encoding.h +2 -0
index 14caa95d66de..ca6343537eba 100644
@@ -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 -1
index 1d73cd73b166..fccd19b7eb31 100644
@@ -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))
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#9 lore raw

PATCH v5 5/12 lib: sbi: Support sscofpmf extension in OpenSBI

In reply to 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.

---
 include/sbi/riscv_encoding.h | 25 +++++++++++
 lib/sbi/sbi_pmu.c            | 85 ++++++++++++++++++++++++++++++------
 2 files changed, 97 insertions(+), 13 deletions(-)
include/sbi/riscv_encoding.h +25 -0
index ca6343537eba..8a29f8e05c9a 100644
@@ -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 +72 -13

135 lines not on this page · raw message

Signature · 1 line
2.31.1
Applied
#10 lore raw

PATCH v5 5/12 lib: sbi: Support sscofpmf extension in OpenSBI

Quoted 4 lines · Atish Patra

This patch adds sscofpmf extension in pmu module which includes following things.

Quoted 8 lines

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.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 195 lines
---
 include/sbi/riscv_encoding.h | 25 +++++++++++
 lib/sbi/sbi_pmu.c            | 85 ++++++++++++++++++++++++++++++------
 2 files changed, 97 insertions(+), 13 deletions(-)
include/sbi/riscv_encoding.h +25 -0
index ca6343537eba..8a29f8e05c9a 100644
@@ -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 +72 -14
index c276a46e2876..eb0c2ff5d258 100644
@@ -226,6 +226,42 @@ 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
@@ -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;
 }
 
@@ -362,8 +401,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 +427,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 +485,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 +548,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

#11 lore raw

PATCH v5 6/12 lib: sbi: Always enable access for all counters

In reply to Atish Patra

OpenSBI doesn'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 doesn'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
index fccd19b7eb31..69b57a781cfd 100644
@@ -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
index eb0c2ff5d258..e2373ff6d9f7 100644
@@ -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
@@ -449,7 +443,6 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lo
 	unsigned 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);
 
@@ -474,8 +467,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;
 			}
@@ -647,7 +639,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
Applied
#12 lore raw

PATCH v5 6/12 lib: sbi: Always enable access for all counters

Quoted 9 lines · Atish Patra

OpenSBI doesn'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 doesn't increment. As a result, we don't have to enable/disable mcounteren at every start/stop.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 120 lines
---
 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
index fccd19b7eb31..69b57a781cfd 100644
@@ -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 -13
index eb0c2ff5d258..e2373ff6d9f7 100644
@@ -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
@@ -449,7 +443,6 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lo
        unsigned 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);
 
@@ -474,8 +467,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;
                        }
@@ -647,7 +639,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);
 }
 
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#13 lore raw

PATCH v5 7/12 lib: sbi: Allow programmable counters to monitor cycle/instret events

In reply to Atish Patra

A 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
index b3010cc5c1ce..f0185566cd6d 100644
@@ -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
Applied
#14 lore raw

PATCH v5 7/12 lib: sbi: Allow programmable counters to monitor cycle/instret events

Quoted 9 lines · Atish Patra

A 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.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 145 lines
---
 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
index b3010cc5c1ce..f0185566cd6d 100644
@@ -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 -17
index e2373ff6d9f7..818b87555e69 100644
@@ -175,9 +175,7 @@ static int pmu_add_hw_event_map(u32 eidx_start, u32 eidx_end, u32 cmap,
        struct sbi_pmu_hw_event *event = &hw_event_map[num_hw_events];
 
        /* The first two counters are reserved by priv spec */
-       if ((eidx_start == SBI_PMU_HW_CPU_CYCLES && cmap != 0x1) ||
-           (eidx_start == SBI_PMU_HW_INSTRUCTIONS && cmap != 0x4) ||
-           (eidx_start > SBI_PMU_HW_INSTRUCTIONS && (cmap & 0x07)))
+       if (eidx_start > SBI_PMU_HW_INSTRUCTIONS && (cmap & SBI_PMU_FIXED_CTR_MASK))
                return SBI_EDENIED;
 
        if (num_hw_events >= SBI_PMU_HW_EVENT_MAX - 1) {
@@ -188,8 +186,6 @@ static int pmu_add_hw_event_map(u32 eidx_start, u32 eidx_end, u32 cmap,
 
        event->start_idx = eidx_start;
        event->end_idx = eidx_end;
-       event->counters = cmap;
-       event->select = select;
 
        /* Sanity check */
        for (i = 0; i < num_hw_events; i++) {
@@ -199,11 +195,19 @@ static int pmu_add_hw_event_map(u32 eidx_start, u32 eidx_end, u32 cmap,
                else
                        is_overlap = pmu_event_range_overlap(&hw_event_map[i], event);
                if (is_overlap)
-                       return SBI_EINVALID_ADDR;
+                       goto reset_event;
        }
+
+       event->counters = cmap;
+       event->select = select;
        num_hw_events++;
 
        return 0;
+
+reset_event:
+       event->start_idx = 0;
+       event->end_idx = 0;
+       return SBI_EINVAL;
 }
 
 /**
@@ -437,23 +441,37 @@ static int pmu_update_hw_mhpmevent(struct sbi_pmu_hw_event *hw_evt, int ctr_idx,
        return 0;
 }
 
+static int pmu_ctr_find_fixed_fw(unsigned long evt_idx_code)
+{
+       /* Non-programmables counters are enabled always. No need to do lookup */
+       if (evt_idx_code == SBI_PMU_HW_CPU_CYCLES)
+               return 0;
+       else if (evt_idx_code == SBI_PMU_HW_INSTRUCTIONS)
+               return 2;
+       else
+               return SBI_EINVAL;
+}
+
 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;
-       int i, ret = 0, ctr_idx = SBI_ENOTSUPP;
+       int i, ret = 0, fixed_ctr, ctr_idx = SBI_ENOTSUPP;
        struct sbi_pmu_hw_event *temp;
        unsigned long mctr_inhbt = csr_read(CSR_MCOUNTINHIBIT);
-       int evt_idx_code = get_cidx_code(event_idx);
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
 
        if (cbase > num_hw_ctrs)
                return SBI_EINVAL;
 
-       /* Non-programmables counters are enabled always. No need to do lookup */
-       if (evt_idx_code == SBI_PMU_HW_CPU_CYCLES)
-               return 0;
-       else if (evt_idx_code == SBI_PMU_HW_INSTRUCTIONS)
-               return 2;
+       /**
+        * If Sscof is present try to find the programmable counter for
+        * cycle/instret as well.
+        */
+       fixed_ctr = pmu_ctr_find_fixed_fw(event_idx);
+       if (fixed_ctr >= 0 &&
+           !sbi_hart_has_feature(scratch, SBI_HART_HAS_SSCOFPMF))
+               return fixed_ctr;
 
        for (i = 0; i < num_hw_events; i++) {
                temp = &hw_event_map[i];
@@ -465,7 +483,9 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lo
                if ((event_idx == SBI_PMU_EVENT_RAW_IDX) && temp->select != data)
                        continue;
 
-               ctr_mask = temp->counters & (cmask << cbase);
+               /* Fixed counters should not be part of the search */
+               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;
@@ -474,8 +494,16 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lo
                }
        }
 
-       if (ctr_idx == SBI_ENOTSUPP)
-               return SBI_EFAIL;
+       if (ctr_idx == SBI_ENOTSUPP) {
+               /**
+                * We can't find any programmable counters for cycle/instret.
+                * Return the fixed counter as they are mandatory anyways.
+                */
+               if (fixed_ctr >= 0)
+                       return fixed_ctr;
+               else
+                       return SBI_EFAIL;
+       }
 
        ret = pmu_update_hw_mhpmevent(temp, ctr_idx, flags, event_idx, data);
 
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#15 lore raw

PATCH v5 8/12 lib: sbi: Reset the mhpmevent value upon counter reset

In reply to Atish Patra

The 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
index 818b87555e69..1bb3e49412b7 100644
@@ -369,6 +369,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)
 {
@@ -392,8 +406,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
Applied
#16 lore raw

PATCH v5 8/12 lib: sbi: Reset the mhpmevent value upon counter reset

Quoted 8 lines · Atish Patra

The 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.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 49 lines
---
 lib/sbi/sbi_pmu.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
lib/sbi/sbi_pmu.c +17 -2
index 818b87555e69..1bb3e49412b7 100644
@@ -369,6 +369,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)
 {
@@ -392,8 +406,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;
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#17 lore raw

PATCH v5 9/12 lib: sbi: Counter info width should be zero indexed

In reply to Atish Patra

The mhpm bits represent the number of bits available in mhpmcounter while counter width describes a zero indexed value. Fix the counter width calculation.

Fixes 13d40f21d588 ("lib: sbi: Add PMU support")
---
 lib/sbi/sbi_pmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_pmu.c +1 -1
index 1bb3e49412b7..da8a37b1d34b 100644
@@ -649,7 +649,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
Applied
#18 lore raw

PATCH v5 9/12 lib: sbi: Counter info width should be zero indexed

Quoted 10 lines · Atish Patra

The mhpm bits represent the number of bits available in mhpmcounter while counter width describes a zero indexed value. Fix the counter width calculation.

Fixes 13d40f21d588 ("lib: sbi: Add PMU support")

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 25 lines
---
 lib/sbi/sbi_pmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_pmu.c +1 -2
index 1bb3e49412b7..da8a37b1d34b 100644
@@ -649,7 +649,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;
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#19 lore raw

PATCH v5 10/12 lib: sbi: Enable PMU extension for platforms without mcountinhibit

In reply to 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.

---
 lib/sbi/sbi_ecall_pmu.c | 10 ++-------
 lib/sbi/sbi_pmu.c       | 50 ++++++++++++++++++++++++++++-------------
 lib/utils/fdt/fdt_pmu.c |  4 ++++
 3 files changed, 41 insertions(+), 23 deletions(-)
lib/sbi/sbi_ecall_pmu.c +2 -8
index 39d3857680c1..9ee9e81802e7 100644
@@ -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 +35 -15

105 lines not on this page · raw message

lib/utils/fdt/fdt_pmu.c +4 -0
index 9ebf1c225017..09c83c816f23 100644
@@ -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
Applied
#20 lore raw

PATCH v5 10/12 lib: sbi: Enable PMU extension for platforms without mcountinhibit

Quoted 13 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.

Quoted 3 lines

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 181 lines
---
 lib/sbi/sbi_ecall_pmu.c | 10 ++-------
 lib/sbi/sbi_pmu.c       | 50 ++++++++++++++++++++++++++++-------------
 lib/utils/fdt/fdt_pmu.c |  4 ++++
 3 files changed, 41 insertions(+), 23 deletions(-)
lib/sbi/sbi_ecall_pmu.c +2 -8
index 39d3857680c1..9ee9e81802e7 100644
@@ -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 +35 -15
index da8a37b1d34b..e0303a7b19e6 100644
@@ -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);
 
@@ -346,7 +355,13 @@ int sbi_pmu_ctr_start(unsigned long cbase, unsigned long cmask,
 
 static int pmu_ctr_stop_hw(uint32_t cidx)
 {
-       unsigned long mctr_inhbt = csr_read(CSR_MCOUNTINHIBIT);
+       struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+       unsigned long mctr_inhbt;
+
+       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)
@@ -474,7 +489,8 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lo
        unsigned 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)
@@ -489,6 +505,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) ||
@@ -503,10 +521,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;
                }
        }
 
@@ -678,11 +704,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);
 }
@@ -692,10 +716,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 +4 -1
index 9ebf1c225017..09c83c816f23 100644
@@ -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;
 }
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#21 lore raw

PATCH v5 11/12 lib: utils: Rename the prefix in PMU DT properties

In reply to 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.

---
 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
index 6533c99f8997..a60b75cf9efa 100644
@@ -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
index 09c83c816f23..529ee420554f 100644
@@ -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
Applied
#22 lore raw

PATCH v5 11/12 lib: utils: Rename the prefix in PMU DT properties

Quoted 8 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.

Applied this patch to the riscv/opensbi repo.

Thanks,
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
index 6533c99f8997..a60b75cf9efa 100644
@@ -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
index 09c83c816f23..529ee420554f 100644
@@ -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

#23 lore raw

PATCH v5 12/12 lib: sbi: Fix initial value mask while updating the counters

In reply to Atish Patra

The first 32 bits of the initial value for the counter should be preserved while updating the mhpmcounter for 32bit.

Fixes 13d40f21d588e ("lib: sbi: Add PMU support")
---
 lib/sbi/sbi_pmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_pmu.c +1 -1
index e0303a7b19e6..a0eb3592967d 100644
@@ -270,7 +270,7 @@ 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);
Signature · 1 line
2.31.1
#24 lore raw

PATCH v5 12/12 lib: sbi: Fix initial value mask while updating the counters

Quoted 11 lines · Atish Patra

The first 32 bits of the initial value for the counter should be preserved while updating the mhpmcounter for 32bit.

Fixes 13d40f21d588e ("lib: sbi: Add PMU support")
---
 lib/sbi/sbi_pmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#25 lore raw

PATCH v5 12/12 lib: sbi: Fix initial value mask while updating the counters

In reply to Atish Patra
Quoted 7 lines · atish patra atish.patra at wdc.com

The first 32 bits of the initial value for the counter should be preserved while updating the mhpmcounter for 32bit.

Fixes 13d40f21d588e ("lib: sbi: Add PMU support")

---

Looks good to me.

Quoted 24 lines

lib/sbi/sbi_pmu.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_pmu.c +1 -2
index e0303a7b19e6..a0eb3592967d 100644
@@ -270,7 +270,7 @@ 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);
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Applied
#26 lore raw

PATCH v5 12/12 lib: sbi: Fix initial value mask while updating the counters

In reply to Atish Patra
Quoted 7 lines · Atish Patra

The first 32 bits of the initial value for the counter should be preserved while updating the mhpmcounter for 32bit.

Fixes 13d40f21d588e ("lib: sbi: Add PMU support")

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 25 lines
---
 lib/sbi/sbi_pmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_pmu.c +1 -2
index e0303a7b19e6..a0eb3592967d 100644
@@ -270,7 +270,7 @@ 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);
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi