Skip to contentSkip to conversation

SBI PMU improvements

PATCH Superseded Atish Patra 3 participants 6 messages Dec 7, 2023 – Dec 8, 2023

lore
Share & tools
lore
#0 lore raw

v2 cover SBI PMU improvements

Here are two small patches for SBI PMU.

The 1st patch adds SBI PMU snapshot definitions and some boilerplate code while the 2nd patches fixes an issue with discontigious counters.

Changes from v1->v2:
1. Removed additional check for counter1.
2. Return error in counter start/stop functions if snapshot flag is set.

The patches can also be found at
https://github.com/atishp04/opensbi/tree/pmu_fixes_v2

Atish Patra (2):
lib: sbi_pmu: Add PMU snapshot definitions
lib: sbi_pmu: Fix the counter info function

include/sbi/sbi_ecall_interface.h |  6 +++++-
include/sbi/sbi_error.h           |  1 +
include/sbi/sbi_pmu.h             |  1 +
lib/sbi/sbi_ecall_pmu.c           |  2 ++
lib/sbi/sbi_pmu.c                 | 14 ++++++++++++--
5 files changed, 21 insertions(+), 3 deletions(-)

--
2.34.1

#1 lore raw

PATCH v2 1/2 lib: sbi_pmu: Add PMU snapshot definitions

OpenSBI doesn't support SBI PMU snapshot yet as there is not much benefit unless the multiple counters overflow at the same time.

Just add the definition and return not supported error at this moment. The default returned error is also not supported. Thus, no functional change intended.

---
 include/sbi/sbi_ecall_interface.h | 6 +++++-
 include/sbi/sbi_error.h           | 1 +
 lib/sbi/sbi_ecall_pmu.c           | 2 ++
 lib/sbi/sbi_pmu.c                 | 6 ++++++
 4 files changed, 14 insertions(+), 1 deletion(-)
include/sbi/sbi_ecall_interface.h +5 -1
index 1fe469e37078..d8c646d7b073 100644
@@ -103,6 +103,7 @@
 #define SBI_EXT_PMU_COUNTER_STOP	0x4
 #define SBI_EXT_PMU_COUNTER_FW_READ	0x5
 #define SBI_EXT_PMU_COUNTER_FW_READ_HI	0x6
+#define SBI_EXT_PMU_SNAPSHOT_SET_SHMEM	0x7
 
 /** General pmu event codes specified in SBI PMU extension */
 enum sbi_pmu_hw_generic_events_t {
@@ -241,9 +242,11 @@ enum sbi_pmu_ctr_type {
 
 /* Flags defined for counter start function */
 #define SBI_PMU_START_FLAG_SET_INIT_VALUE (1 << 0)
+#define SBI_PMU_START_FLAG_INIT_FROM_SNAPSHOT (1 << 1)
 
 /* Flags defined for counter stop function */
 #define SBI_PMU_STOP_FLAG_RESET (1 << 0)
+#define SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT (1 << 1)
 
 /* SBI function IDs for DBCN extension */
 #define SBI_EXT_DBCN_CONSOLE_WRITE		0x0
@@ -309,8 +312,9 @@ enum sbi_cppc_reg_id {
 #define SBI_ERR_ALREADY_AVAILABLE		-6
 #define SBI_ERR_ALREADY_STARTED			-7
 #define SBI_ERR_ALREADY_STOPPED			-8
+#define SBI_ERR_NO_SHMEM			-9
 
-#define SBI_LAST_ERR				SBI_ERR_ALREADY_STOPPED
+#define SBI_LAST_ERR				SBI_ERR_NO_SHMEM
 
 /* clang-format on */
 
include/sbi/sbi_error.h +1 -0
index dd65e14b6fcd..7f97506d8bd5 100644
@@ -23,6 +23,7 @@
 #define SBI_EALREADY		SBI_ERR_ALREADY_AVAILABLE
 #define SBI_EALREADY_STARTED	SBI_ERR_ALREADY_STARTED
 #define SBI_EALREADY_STOPPED	SBI_ERR_ALREADY_STOPPED
+#define SBI_ENO_SHMEM		SBI_ERR_NO_SHMEM
 
 #define SBI_ENODEV		-1000
 #define SBI_ENOSYS		-1001
lib/sbi/sbi_ecall_pmu.c +2 -0
index 1d5d512eeed7..c58591106d09 100644
@@ -74,6 +74,8 @@ static int sbi_ecall_pmu_handler(unsigned long extid, unsigned long funcid,
 	case SBI_EXT_PMU_COUNTER_STOP:
 		ret = sbi_pmu_ctr_stop(regs->a0, regs->a1, regs->a2);
 		break;
+	case SBI_EXT_PMU_SNAPSHOT_SET_SHMEM:
+		/* fallthrough as OpenSBI doesn't support snapshot yet */
 	default:
 		ret = SBI_ENOTSUPP;
 	}
lib/sbi/sbi_pmu.c +6 -0
index 185068bb1f43..1ac8562a9c99 100644
@@ -442,6 +442,9 @@ int sbi_pmu_ctr_start(unsigned long cbase, unsigned long cmask,
 	if ((cbase + sbi_fls(cmask)) >= total_ctrs)
 		return ret;
 
+	if (flags & SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT)
+		return SBI_ENO_SHMEM;
+
 	if (flags & SBI_PMU_START_FLAG_SET_INIT_VALUE)
 		bUpdate = true;
 
@@ -540,6 +543,9 @@ int sbi_pmu_ctr_stop(unsigned long cbase, unsigned long cmask,
 	if ((cbase + sbi_fls(cmask)) >= total_ctrs)
 		return SBI_EINVAL;
 
+	if (flag & SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT)
+		return SBI_ENO_SHMEM;
+
 	for_each_set_bit(i, &cmask, BITS_PER_LONG) {
 		cidx = i + cbase;
 		event_idx_type = pmu_ctr_validate(phs, cidx, &event_code);
Signature · 1 line
2.34.1
Applied
#2 lore raw

PATCH v2 1/2 lib: sbi_pmu: Add PMU snapshot definitions

Quoted 10 lines · On Fri, Dec 8, 2023 at 3:54?AM Atish Patra <[email protected]> wrote:

OpenSBI doesn't support SBI PMU snapshot yet as there is not much benefit unless the multiple counters overflow at the same time.

Just add the definition and return not supported error at this moment. The default returned error is also not supported. Thus, no functional change intended.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 94 lines
---
 include/sbi/sbi_ecall_interface.h | 6 +++++-
 include/sbi/sbi_error.h           | 1 +
 lib/sbi/sbi_ecall_pmu.c           | 2 ++
 lib/sbi/sbi_pmu.c                 | 6 ++++++
 4 files changed, 14 insertions(+), 1 deletion(-)
include/sbi/sbi_ecall_interface.h +5 -1
index 1fe469e37078..d8c646d7b073 100644
@@ -103,6 +103,7 @@
 #define SBI_EXT_PMU_COUNTER_STOP       0x4
 #define SBI_EXT_PMU_COUNTER_FW_READ    0x5
 #define SBI_EXT_PMU_COUNTER_FW_READ_HI 0x6
+#define SBI_EXT_PMU_SNAPSHOT_SET_SHMEM 0x7
 
 /** General pmu event codes specified in SBI PMU extension */
 enum sbi_pmu_hw_generic_events_t {
@@ -241,9 +242,11 @@ enum sbi_pmu_ctr_type {
 
 /* Flags defined for counter start function */
 #define SBI_PMU_START_FLAG_SET_INIT_VALUE (1 << 0)
+#define SBI_PMU_START_FLAG_INIT_FROM_SNAPSHOT (1 << 1)
 
 /* Flags defined for counter stop function */
 #define SBI_PMU_STOP_FLAG_RESET (1 << 0)
+#define SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT (1 << 1)
 
 /* SBI function IDs for DBCN extension */
 #define SBI_EXT_DBCN_CONSOLE_WRITE             0x0
@@ -309,8 +312,9 @@ enum sbi_cppc_reg_id {
 #define SBI_ERR_ALREADY_AVAILABLE              -6
 #define SBI_ERR_ALREADY_STARTED                        -7
 #define SBI_ERR_ALREADY_STOPPED                        -8
+#define SBI_ERR_NO_SHMEM                       -9
 
-#define SBI_LAST_ERR                           SBI_ERR_ALREADY_STOPPED
+#define SBI_LAST_ERR                           SBI_ERR_NO_SHMEM
 
 /* clang-format on */
 
include/sbi/sbi_error.h +1 -0
index dd65e14b6fcd..7f97506d8bd5 100644
@@ -23,6 +23,7 @@
 #define SBI_EALREADY           SBI_ERR_ALREADY_AVAILABLE
 #define SBI_EALREADY_STARTED   SBI_ERR_ALREADY_STARTED
 #define SBI_EALREADY_STOPPED   SBI_ERR_ALREADY_STOPPED
+#define SBI_ENO_SHMEM          SBI_ERR_NO_SHMEM
 
 #define SBI_ENODEV             -1000
 #define SBI_ENOSYS             -1001
lib/sbi/sbi_ecall_pmu.c +2 -0
index 1d5d512eeed7..c58591106d09 100644
@@ -74,6 +74,8 @@ static int sbi_ecall_pmu_handler(unsigned long extid, unsigned long funcid,
        case SBI_EXT_PMU_COUNTER_STOP:
                ret = sbi_pmu_ctr_stop(regs->a0, regs->a1, regs->a2);
                break;
+       case SBI_EXT_PMU_SNAPSHOT_SET_SHMEM:
+               /* fallthrough as OpenSBI doesn't support snapshot yet */
        default:
                ret = SBI_ENOTSUPP;
        }
lib/sbi/sbi_pmu.c +6 -1
index 185068bb1f43..1ac8562a9c99 100644
@@ -442,6 +442,9 @@ int sbi_pmu_ctr_start(unsigned long cbase, unsigned long cmask,
        if ((cbase + sbi_fls(cmask)) >= total_ctrs)
                return ret;
 
+       if (flags & SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT)
+               return SBI_ENO_SHMEM;
+
        if (flags & SBI_PMU_START_FLAG_SET_INIT_VALUE)
                bUpdate = true;
 
@@ -540,6 +543,9 @@ int sbi_pmu_ctr_stop(unsigned long cbase, unsigned long cmask,
        if ((cbase + sbi_fls(cmask)) >= total_ctrs)
                return SBI_EINVAL;
 
+       if (flag & SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT)
+               return SBI_ENO_SHMEM;
+
        for_each_set_bit(i, &cmask, BITS_PER_LONG) {
                cidx = i + cbase;
                event_idx_type = pmu_ctr_validate(phs, cidx, &event_code);
--

2.34.1

#3 lore raw

PATCH v2 2/2 lib: sbi_pmu: Fix the counter info function

In reply to Atish Patra

The counter info should only return valid hardware counters for the ones set in the counter mask. Otherwise, it will report incorrect number of hardware counters to the supervisor if the platform has discontiguous counters.

Fixes c744ed77b18c ("lib: sbi_pmu: Enable noncontigous hpm event and counters")
---
 include/sbi/sbi_pmu.h | 1 +
 lib/sbi/sbi_pmu.c     | 8 ++++++--
 2 files changed, 7 insertions(+), 2 deletions(-)
include/sbi/sbi_pmu.h +1 -0
index 16f687733baa..bd0392e5fe5a 100644
@@ -23,6 +23,7 @@ struct sbi_scratch;
 #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
+#define SBI_PMU_CY_IR_MASK	0x05
 
 struct sbi_pmu_device {
 	/** Name of the PMU platform device */
lib/sbi/sbi_pmu.c +6 -2
index 1ac8562a9c99..3f1d1dda24fc 100644
@@ -891,13 +891,17 @@ int sbi_pmu_ctr_get_info(uint32_t cidx, unsigned long *ctr_info)
 	int width;
 	union sbi_pmu_ctr_info cinfo = {0};
 	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+	unsigned long counter_mask = (unsigned long)sbi_hart_mhpm_mask(scratch) |
+				     SBI_PMU_CY_IR_MASK;
 
-	/* Sanity check. Counter1 is not mapped at all */
-	if (cidx >= total_ctrs || cidx == 1)
+	/* Sanity check */
+	if (cidx >= total_ctrs)
 		return SBI_EINVAL;
 
 	/* We have 31 HW counters with 31 being the last index(MHPMCOUNTER31) */
 	if (cidx < num_hw_ctrs) {
+		if (!(__test_bit(cidx, &counter_mask)))
+			return SBI_EINVAL;
 		cinfo.type = SBI_PMU_CTR_TYPE_HW;
 		cinfo.csr = CSR_CYCLE + cidx;
 		/* mcycle & minstret are always 64 bit */
Signature · 1 line
2.34.1
#4 lore raw

PATCH v2 2/2 lib: sbi_pmu: Fix the counter info function

? 2023-12-07???? 14:23 -0800?Atish Patra???

Quoted 8 lines

The counter info should only return valid hardware counters for the ones set in the counter mask. Otherwise, it will report incorrect number of hardware counters to the supervisor if the platform has discontiguous counters.

Fixes c744ed77b18c ("lib: sbi_pmu: Enable noncontigous hpm event and counters")

Look good to me.

Quoted 45 lines

---
?include/sbi/sbi_pmu.h | 1 +
?lib/sbi/sbi_pmu.c???? | 8 ++++++--
?2 files changed, 7 insertions(+), 2 deletions(-)

include/sbi/sbi_pmu.h +0 -0
index 16f687733baa..bd0392e5fe5a 100644
@@ -23,6 +23,7 @@ struct sbi_scratch;

?#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
+#define SBI_PMU_CY_IR_MASK 0x05
?
?struct sbi_pmu_device {
? /** Name of the PMU platform device */

lib/sbi/sbi_pmu.c +2 -0
index 1ac8562a9c99..3f1d1dda24fc 100644
@@ -891,13 +891,17 @@ int sbi_pmu_ctr_get_info(uint32_t cidx, unsigned long *ctr_info)
 ?	int width;
 ?	union sbi_pmu_ctr_info cinfo = {0};
 ?	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+	unsigned long counter_mask = (unsigned long)sbi_hart_mhpm_mask(scratch) |
+				???? SBI_PMU_CY_IR_MASK;

?

lib/sbi/sbi_pmu.c +2 -2
-	/* Sanity check. Counter1 is not mapped at all */
-	if (cidx >= total_ctrs || cidx == 1)
+	/* Sanity check */
+	if (cidx >= total_ctrs)

? return SBI_EINVAL;
?
? /* We have 31 HW counters with 31 being the last index(MHPMCOUNTER31) */
? if (cidx < num_hw_ctrs) {

lib/sbi/sbi_pmu.c +2 -0
+		if (!(__test_bit(cidx, &counter_mask)))
+			return SBI_EINVAL;

? cinfo.type = SBI_PMU_CTR_TYPE_HW;
? cinfo.csr = CSR_CYCLE + cidx;
? /* mcycle & minstret are always 64 bit */

Signature · 1 line
2.34.1
Applied
#5 lore raw

PATCH v2 2/2 lib: sbi_pmu: Fix the counter info function

In reply to Atish Patra
Quoted 9 lines · On Fri, Dec 8, 2023 at 3:54?AM Atish Patra <[email protected]> wrote:

The counter info should only return valid hardware counters for the ones set in the counter mask. Otherwise, it will report incorrect number of hardware counters to the supervisor if the platform has discontiguous counters.

Fixes c744ed77b18c ("lib: sbi_pmu: Enable noncontigous hpm event and counters")

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 44 lines
---
 include/sbi/sbi_pmu.h | 1 +
 lib/sbi/sbi_pmu.c     | 8 ++++++--
 2 files changed, 7 insertions(+), 2 deletions(-)
include/sbi/sbi_pmu.h +1 -0
index 16f687733baa..bd0392e5fe5a 100644
@@ -23,6 +23,7 @@ struct sbi_scratch;
 #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
+#define SBI_PMU_CY_IR_MASK     0x05
 
 struct sbi_pmu_device {
        /** Name of the PMU platform device */
lib/sbi/sbi_pmu.c +6 -3
index 1ac8562a9c99..3f1d1dda24fc 100644
@@ -891,13 +891,17 @@ int sbi_pmu_ctr_get_info(uint32_t cidx, unsigned long *ctr_info)
        int width;
        union sbi_pmu_ctr_info cinfo = {0};
        struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+       unsigned long counter_mask = (unsigned long)sbi_hart_mhpm_mask(scratch) |
+                                    SBI_PMU_CY_IR_MASK;
 
-       /* Sanity check. Counter1 is not mapped at all */
-       if (cidx >= total_ctrs || cidx == 1)
+       /* Sanity check */
+       if (cidx >= total_ctrs)
                return SBI_EINVAL;
 
        /* We have 31 HW counters with 31 being the last index(MHPMCOUNTER31) */
        if (cidx < num_hw_ctrs) {
+               if (!(__test_bit(cidx, &counter_mask)))
+                       return SBI_EINVAL;
                cinfo.type = SBI_PMU_CTR_TYPE_HW;
                cinfo.csr = CSR_CYCLE + cidx;
                /* mcycle & minstret are always 64 bit */
--

2.34.1