Skip to contentSkip to conversation

SBI PMU firmware counters and events improvement

PATCH Superseded Mayuresh Chitale 1 participant 4 messages Feb 7, 2023

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover SBI PMU firmware counters and events improvement

This patchset implements the improvements described in the following proposal from Anup Patel:

https://lists.riscv.org/g/tech-prs/message/102

Mayuresh Chitale (3):
lib: sbi_pmu: add callback for counter width
lib: sbi_pmu: Implement sbi_pmu_counter_fw_read_hi
lib: sbi_pmu: Reserve space for implementation specific firmware
events

 include/sbi/sbi_ecall_interface.h | 7 ++++++-
 include/sbi/sbi_pmu.h             | 5 +++++
 lib/sbi/sbi_ecall_pmu.c           | 8 ++++++++
 lib/sbi/sbi_pmu.c                 | 6 ++++++
 4 files changed, 25 insertions(+), 1 deletion(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 lib: sbi_pmu: add callback for counter width

This patch adds a callback to fetch the number of bits implemented for a custom firmware counter. If the callback fails or is not implemented then width defaults to 63.

---
 include/sbi/sbi_pmu.h | 5 +++++
 lib/sbi/sbi_pmu.c     | 6 ++++++
 2 files changed, 11 insertions(+)
include/sbi/sbi_pmu.h +5 -0
index c365243..b3b75c1 100644
@@ -41,6 +41,11 @@ struct sbi_pmu_device {
 	bool (*fw_counter_match_code)(uint32_t counter_index,
 				      uint32_t event_idx_code);
 
+	/**
+	 * Fetch the max width of this counter in number of bits.
+	 */
+	int (*fw_counter_width)(void);
+
 	/**
 	 * Read value of custom firmware counter
 	 * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
lib/sbi/sbi_pmu.c +6 -0
index 154dbda..a99c045 100644
@@ -761,6 +761,7 @@ unsigned long sbi_pmu_num_ctr(void)
 
 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();
 
@@ -782,6 +783,11 @@ int sbi_pmu_ctr_get_info(uint32_t cidx, unsigned long *ctr_info)
 		cinfo.type = SBI_PMU_CTR_TYPE_FW;
 		/* Firmware counters are always 64 bits wide */
 		cinfo.width = 63;
+		if (pmu_dev && pmu_dev->fw_counter_width) {
+			width = pmu_dev->fw_counter_width();
+			if (width)
+				cinfo.width = width - 1;
+		}
 	}
 
 	*ctr_info = cinfo.value;
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/3 lib: sbi_pmu: Implement sbi_pmu_counter_fw_read_hi

In reply to Mayuresh Chitale

To support 64 bit firmware counters on RV32 systems, we implement sbi_pmu_counter_fw_read_hi() which returns the upper 32 bits of the firmware counter value. On RV64 (or higher) systems, this function will always return zero.

---
 include/sbi/sbi_ecall_interface.h | 1 +
 lib/sbi/sbi_ecall_pmu.c           | 8 ++++++++
 2 files changed, 9 insertions(+)
include/sbi/sbi_ecall_interface.h +1 -0
index a3f2bf4..f5d6667 100644
@@ -99,6 +99,7 @@
 #define SBI_EXT_PMU_COUNTER_START	0x3
 #define SBI_EXT_PMU_COUNTER_STOP	0x4
 #define SBI_EXT_PMU_COUNTER_FW_READ	0x5
+#define SBI_EXT_PMU_COUNTER_FW_READ_HI	0x6
 
 /** General pmu event codes specified in SBI PMU extension */
 enum sbi_pmu_hw_generic_events_t {
lib/sbi/sbi_ecall_pmu.c +8 -0
index 826c8a8..2d1e669 100644
@@ -54,6 +54,14 @@ static int sbi_ecall_pmu_handler(unsigned long extid, unsigned long funcid,
 		ret = sbi_pmu_ctr_fw_read(regs->a0, &temp);
 		*out_val = temp;
 		break;
+	case SBI_EXT_PMU_COUNTER_FW_READ_HI:
+#if __riscv_xlen == 32
+		ret = sbi_pmu_ctr_fw_read(regs->a0, &temp);
+		*out_val = temp >> 32
+#else
+		*out_val = 0;
+#endif
+		break;
 	case SBI_EXT_PMU_COUNTER_START:
 
 #if __riscv_xlen == 32
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/3 lib: sbi_pmu: Reserve space for implementation specific firmware events

In reply to Mayuresh Chitale

We reserve space for SBI implementation specific custom firmware events which can be used by M-mode firmwares and HS-mode hypervisors for their own use. This reserved space is intentionally large to ensure that SBI implementation has enough space to accommodate platform specific firmware events as well.

---
 include/sbi/sbi_ecall_interface.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
include/sbi/sbi_ecall_interface.h +5 -1
index f5d6667..32923a8 100644
@@ -182,7 +182,11 @@ enum sbi_pmu_fw_event_code_id {
 	SBI_PMU_FW_HFENCE_VVMA_RCVD	= 19,
 	SBI_PMU_FW_HFENCE_VVMA_ASID_SENT = 20,
 	SBI_PMU_FW_HFENCE_VVMA_ASID_RCVD = 21,
-	SBI_PMU_FW_MAX,
+	/*
+	 * Event codes from 22 to 127 are reserved for
+	 * SBI implementation specific custom firmware events.
+	 */
+	SBI_PMU_FW_MAX = 128,
 };
 
 /** SBI PMU event idx type */
Signature · 1 line
2.34.1