Skip to contentSkip to conversation

SBI PMU firmware counters and events improvement

PATCH Superseded Mayuresh Chitale 2 participants 12 messages Feb 17, 2023 – Feb 22, 2023

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 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

Changes in v2:
- Update Risc V platform specific firmware event handling as per the SBI spec update: https://lists.riscv.org/g/tech-prs/message/211

Mayuresh Chitale (5):
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
lib: sbi_pmu: Update custom PMU device ops
lib: sbi_pmu: Add hartid parameter PMU device ops

 include/sbi/sbi_ecall_interface.h |  12 +++
 include/sbi/sbi_pmu.h             |  33 +++++---
 lib/sbi/sbi_ecall_pmu.c           |   8 ++
 lib/sbi/sbi_pmu.c                 | 120 +++++++++++++++++++-----------
 4 files changed, 121 insertions(+), 52 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/5 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 v2 1/5 lib: sbi_pmu: add callback for counter width

Quoted 55 lines · Mayuresh Chitale

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 -1
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;
--

2.34.1

Quoted 4 lines

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

Signature · 2 lines
Regards,
Atish
#3 lore raw

PATCH v2 2/5 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
#4 lore raw

PATCH v2 2/5 lib: sbi_pmu: Implement sbi_pmu_counter_fw_read_hi

Quoted 47 lines · 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 -1
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
--

2.34.1

Quoted 4 lines

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

Signature · 2 lines
Regards,
Atish
#5 lore raw

PATCH v2 3/5 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 | 11 +++++++++++
 1 file changed, 11 insertions(+)
include/sbi/sbi_ecall_interface.h +11 -0
index f5d6667..5732419 100644
@@ -183,6 +183,17 @@ enum sbi_pmu_fw_event_code_id {
 	SBI_PMU_FW_HFENCE_VVMA_ASID_SENT = 20,
 	SBI_PMU_FW_HFENCE_VVMA_ASID_RCVD = 21,
 	SBI_PMU_FW_MAX,
+	/*
+	 * Event codes 22 to 255 are reserved for future use.
+	 * Event codes 256 to 65534 are reserved for SBI implementation
+	 * specific custom firmware events.
+	 */
+	SBI_PMU_FW_RESERVED_MAX = 0xFFFE,
+	/*
+	* Event code 0xFFFF is used for platform specific firmware
+	* events where the event data contains any event specific information.
+	*/
+	SBI_PMU_FW_PLATFORM = 0xFFFF,
 };
 
 /** SBI PMU event idx type */
Signature · 1 line
2.34.1
#6 lore raw

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

Quoted 38 lines · 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 | 11 +++++++++++
 1 file changed, 11 insertions(+)
include/sbi/sbi_ecall_interface.h +11 -1
index f5d6667..5732419 100644
@@ -183,6 +183,17 @@ enum sbi_pmu_fw_event_code_id {
        SBI_PMU_FW_HFENCE_VVMA_ASID_SENT = 20,
        SBI_PMU_FW_HFENCE_VVMA_ASID_RCVD = 21,
        SBI_PMU_FW_MAX,
+       /*
+        * Event codes 22 to 255 are reserved for future use.
+        * Event codes 256 to 65534 are reserved for SBI implementation
+        * specific custom firmware events.
+        */
+       SBI_PMU_FW_RESERVED_MAX = 0xFFFE,
+       /*
+       * Event code 0xFFFF is used for platform specific firmware
+       * events where the event data contains any event specific information.
+       */
+       SBI_PMU_FW_PLATFORM = 0xFFFF,
 };
 
 /** SBI PMU event idx type */
--

2.34.1

Quoted 4 lines

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

Signature · 2 lines
Regards,
Atish
#7 lore raw

PATCH v2 4/5 lib: sbi_pmu: Update custom PMU device ops

In reply to Mayuresh Chitale

Update the implmentation of some of the PMU device operations to make them inline with the SBI spec updates for the platform specific firmware events.

---
 include/sbi/sbi_pmu.h |  16 ++++---
 lib/sbi/sbi_pmu.c     | 102 ++++++++++++++++++++++++++----------------
 2 files changed, 73 insertions(+), 45 deletions(-)
include/sbi/sbi_pmu.h +10 -6
index b3b75c1..467ec03 100644
@@ -30,16 +30,15 @@ struct sbi_pmu_device {
 
 	/**
 	 * Validate event code of custom firmware event
-	 * Note: SBI_PMU_FW_MAX <= event_idx_code
 	 */
-	int (*fw_event_validate_code)(uint32_t event_idx_code);
+	int (*fw_event_validate_code)(uint64_t event_idx_code);
 
 	/**
 	 * Match custom firmware counter with custom firmware event
 	 * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
 	 */
 	bool (*fw_counter_match_code)(uint32_t counter_index,
-				      uint32_t event_idx_code);
+				      uint64_t event_idx_code);
 
 	/**
 	 * Fetch the max width of this counter in number of bits.
@@ -52,14 +51,19 @@ struct sbi_pmu_device {
 	 */
 	uint64_t (*fw_counter_read_value)(uint32_t counter_index);
 
+	/**
+	 * Write value to custom firmware counter
+	 * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
+	 */
+	void (*fw_counter_write_value)(uint32_t counter_index,
+				       uint64_t value);
+
 	/**
 	 * Start custom firmware counter
-	 * Note: SBI_PMU_FW_MAX <= event_idx_code
 	 * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
 	 */
 	int (*fw_counter_start)(uint32_t counter_index,
-				uint32_t event_idx_code,
-				uint64_t init_val, bool init_val_update);
+				uint64_t event_idx_code);
 
 	/**
 	 * Stop custom firmware counter
lib/sbi/sbi_pmu.c +63 -39

204 lines not on this page · raw message

Signature · 1 line
2.34.1
#8 lore raw

PATCH v2 4/5 lib: sbi_pmu: Update custom PMU device ops

Quoted 2 lines · Mayuresh Chitale

Update the implmentation of some of the PMU device operations to make them

/s/implmentation/implementation

Quoted 2 lines

inline with the SBI spec updates for the platform specific firmware events.

This patch can be further split into smaller patches as it adds multiple things in a single patch.
The commit text needs to improvement as well to indicate the purpose more closely.

1. Allows event data for firmware events.
2. Changed the sbi_pmu_device functions to use uint64_t instead of uint32_t
3. Restrict the platform pmu device ops for SBI_PMU_FW_PLATFORM which is a behavioral change from existing
implemenation. Even though there are no actual uses for this feature upstream, this should be done in a separate patch
with appropriate commit message.

Quoted 283 lines
---
 include/sbi/sbi_pmu.h |  16 ++++---
 lib/sbi/sbi_pmu.c     | 102 ++++++++++++++++++++++++++----------------
 2 files changed, 73 insertions(+), 45 deletions(-)
include/sbi/sbi_pmu.h +10 -6
index b3b75c1..467ec03 100644
@@ -30,16 +30,15 @@ struct sbi_pmu_device {
 
        /**
         * Validate event code of custom firmware event
-        * Note: SBI_PMU_FW_MAX <= event_idx_code
         */
-       int (*fw_event_validate_code)(uint32_t event_idx_code);
+       int (*fw_event_validate_code)(uint64_t event_idx_code);
 
        /**
         * Match custom firmware counter with custom firmware event
         * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
         */
        bool (*fw_counter_match_code)(uint32_t counter_index,
-                                     uint32_t event_idx_code);
+                                     uint64_t event_idx_code);
 
        /**
         * Fetch the max width of this counter in number of bits.
@@ -52,14 +51,19 @@ struct sbi_pmu_device {
         */
        uint64_t (*fw_counter_read_value)(uint32_t counter_index);
 
+       /**
+        * Write value to custom firmware counter
+        * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
+        */
+       void (*fw_counter_write_value)(uint32_t counter_index,
+                                      uint64_t value);
+
        /**
         * Start custom firmware counter
-        * Note: SBI_PMU_FW_MAX <= event_idx_code
         * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
         */
        int (*fw_counter_start)(uint32_t counter_index,
-                               uint32_t event_idx_code,
-                               uint64_t init_val, bool init_val_update);
+                               uint64_t event_idx_code);
 
        /**
         * Stop custom firmware counter
lib/sbi/sbi_pmu.c +63 -40
index a99c045..05e2571 100644
@@ -65,8 +65,12 @@ static uint32_t active_events[SBI_HARTMASK_MAX_BITS][SBI_PMU_HW_CTR_MAX + SBI_PM
 #endif
 static unsigned long fw_counters_started[SBI_HARTMASK_MAX_BITS];
 
-/* Values of firmwares counters on each HART */
-static uint64_t fw_counters_value[SBI_HARTMASK_MAX_BITS][SBI_PMU_FW_CTR_MAX] = {0};
+/*
+ * Counter values for SBI firmware events and event codes for platform
+ * firmware events. Both are mutually exclusive and hence can optimally share
+ * the same memory.
+ */
+static uint64_t fw_counters_data[SBI_HARTMASK_MAX_BITS][SBI_PMU_FW_CTR_MAX] = {0};
 
 /* Maximum number of hardware events available */
 static uint32_t num_hw_events;
@@ -106,7 +110,7 @@ static bool pmu_event_select_overlap(struct sbi_pmu_hw_event *evt,
        return false;
 }
 
-static int pmu_event_validate(unsigned long event_idx)
+static int pmu_event_validate(unsigned long event_idx, uint64_t edata)
 {
        uint32_t event_idx_type = get_cidx_type(event_idx);
        uint32_t event_idx_code = get_cidx_code(event_idx);
@@ -118,9 +122,9 @@ static int pmu_event_validate(unsigned long event_idx)
                event_idx_code_max = SBI_PMU_HW_GENERAL_MAX;
                break;
        case SBI_PMU_EVENT_TYPE_FW:
-               if (SBI_PMU_FW_MAX <= event_idx_code &&
+               if (SBI_PMU_FW_PLATFORM == event_idx_code &&
                    pmu_dev && pmu_dev->fw_event_validate_code)
-                       return pmu_dev->fw_event_validate_code(event_idx_code);
+                       return pmu_dev->fw_event_validate_code(edata);
                else
                        event_idx_code_max = SBI_PMU_FW_MAX;
                break;
@@ -183,12 +187,14 @@ int sbi_pmu_ctr_fw_read(uint32_t cidx, uint64_t *cval)
        if (event_idx_type != SBI_PMU_EVENT_TYPE_FW)
                return SBI_EINVAL;
 
-       if (SBI_PMU_FW_MAX <= event_code &&
-           pmu_dev && pmu_dev->fw_counter_read_value)
-               fw_counters_value[hartid][cidx - num_hw_ctrs] =
-                       pmu_dev->fw_counter_read_value(cidx - num_hw_ctrs);
-
-       *cval = fw_counters_value[hartid][cidx - num_hw_ctrs];
+       if (SBI_PMU_FW_PLATFORM == event_code) {
+               if (pmu_dev && pmu_dev->fw_counter_read_value)
+                       *cval = pmu_dev->fw_counter_read_value(cidx -
+                                                              num_hw_ctrs);
+               else
+                       *cval = 0;
+       } else
+               *cval = fw_counters_data[hartid][cidx - num_hw_ctrs];
 
        return 0;
 }
@@ -357,22 +363,30 @@ int sbi_pmu_irq_bit(void)
 }
 
 static int pmu_ctr_start_fw(uint32_t cidx, uint32_t event_code,
-                           uint64_t ival, bool ival_update)
+                           uint64_t event_data, uint64_t ival,
+                           bool ival_update)
 {
-       int ret;
        u32 hartid = current_hartid();
 
-       if (SBI_PMU_FW_MAX <= event_code &&
-           pmu_dev && pmu_dev->fw_counter_start) {
-               ret = pmu_dev->fw_counter_start(cidx - num_hw_ctrs,
-                                               event_code,
-                                               ival, ival_update);
-               if (ret)
-                       return ret;
+       if (SBI_PMU_FW_PLATFORM == event_code) {
+               if (!pmu_dev ||
+                   !pmu_dev->fw_counter_write_value ||
+                   !pmu_dev->fw_counter_start) {
+                       return SBI_EINVAL;
+                   }
+
+               if (ival_update)
+                       pmu_dev->fw_counter_write_value(cidx - num_hw_ctrs,
+                                                       ival);
+
+               if (pmu_dev->fw_counter_start(hartid, cidx - num_hw_ctrs,
+                                             event_data))
+                       return SBI_EINVAL;
+       } else {
+               if (ival_update)
+                       fw_counters_data[hartid][cidx - num_hw_ctrs] = ival;
        }
 
-       if (ival_update)
-               fw_counters_value[hartid][cidx - num_hw_ctrs] = ival;
        fw_counters_started[hartid] |= BIT(cidx - num_hw_ctrs);
 
        return 0;
@@ -381,8 +395,10 @@ static int pmu_ctr_start_fw(uint32_t cidx, uint32_t event_code,
 int sbi_pmu_ctr_start(unsigned long cbase, unsigned long cmask,
                      unsigned long flags, uint64_t ival)
 {
+       u32 hartid = current_hartid();
        int event_idx_type;
        uint32_t event_code;
+       uint64_t edata;
        int ret = SBI_EINVAL;
        bool bUpdate = false;
        int i, cidx;
@@ -399,8 +415,13 @@ int sbi_pmu_ctr_start(unsigned long cbase, unsigned long cmask,
                if (event_idx_type < 0)
                        /* Continue the start operation for other counters */
                        continue;
-               else if (event_idx_type == SBI_PMU_EVENT_TYPE_FW)
-                       ret = pmu_ctr_start_fw(cidx, event_code, ival, bUpdate);
+               else if (event_idx_type == SBI_PMU_EVENT_TYPE_FW) {
+                       edata = (event_code == SBI_PMU_FW_PLATFORM) ?
+                                fw_counters_data[hartid][cidx - num_hw_ctrs]
+                                : 0x0;
+                       ret = pmu_ctr_start_fw(cidx, event_code, edata, ival,
+                                              bUpdate);
+               }
                else
                        ret = pmu_ctr_start_hw(cidx, ival, bUpdate);
        }
@@ -434,7 +455,7 @@ static int pmu_ctr_stop_fw(uint32_t cidx, uint32_t event_code)
 {
        int ret;
 
-       if (SBI_PMU_FW_MAX <= event_code &&
+       if (SBI_PMU_FW_PLATFORM == event_code &&
            pmu_dev && pmu_dev->fw_counter_stop) {
                ret = pmu_dev->fw_counter_stop(cidx - num_hw_ctrs);
                if (ret)
@@ -640,7 +661,7 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lo
  * check.
  */
 static int pmu_ctr_find_fw(unsigned long cbase, unsigned long cmask,
-                          uint32_t event_code, u32 hartid)
+                          uint32_t event_code, u32 hartid, uint32_t edata)
 {
        int i, cidx;
 
@@ -650,10 +671,11 @@ static int pmu_ctr_find_fw(unsigned long cbase, unsigned long cmask,
                        continue;
                if (active_events[hartid][i] != SBI_PMU_EVENT_IDX_INVALID)
                        continue;
-               if (SBI_PMU_FW_MAX <= event_code &&
+               if (SBI_PMU_FW_PLATFORM == event_code &&
                    pmu_dev && pmu_dev->fw_counter_match_code) {
-                       if (!pmu_dev->fw_counter_match_code(cidx - num_hw_ctrs,
-                                                           event_code))
+                       if (!pmu_dev->fw_counter_match_code(cidx -
+                                                           num_hw_ctrs,
+                                                           edata))
                                continue;
                }
 
@@ -675,7 +697,7 @@ int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask,
        if ((cidx_base + sbi_fls(cidx_mask)) >= total_ctrs)
                return SBI_EINVAL;
 
-       event_type = pmu_event_validate(event_idx);
+       event_type = pmu_event_validate(event_idx, event_data);
        if (event_type < 0)
                return SBI_EINVAL;
        event_code = get_cidx_code(event_idx);
@@ -693,7 +715,8 @@ int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask,
 
        if (event_type == SBI_PMU_EVENT_TYPE_FW) {
                /* Any firmware counter can be used track any firmware event */
-               ctr_idx = pmu_ctr_find_fw(cidx_base, cidx_mask, event_code, hartid);
+               ctr_idx = pmu_ctr_find_fw(cidx_base, cidx_mask, event_code,
+                                         hartid, event_data);
        } else {
                ctr_idx = pmu_ctr_find_hw(cidx_base, cidx_mask, flags, event_idx,
                                          event_data);
@@ -710,15 +733,16 @@ skip_match:
                if (flags & SBI_PMU_CFG_FLAG_AUTO_START)
                        pmu_ctr_start_hw(ctr_idx, 0, false);
        } else if (event_type == SBI_PMU_EVENT_TYPE_FW) {
+               if (event_code == SBI_PMU_FW_PLATFORM)
+                       fw_counters_data[hartid][ctr_idx - num_hw_ctrs] =
+                                                               event_data;
                if (flags & SBI_PMU_CFG_FLAG_CLEAR_VALUE)
-                       fw_counters_value[hartid][ctr_idx - num_hw_ctrs] = 0;
+                       fw_counters_data[hartid][ctr_idx - num_hw_ctrs] = 0;
                if (flags & SBI_PMU_CFG_FLAG_AUTO_START) {
-                       if (SBI_PMU_FW_MAX <= event_code &&
+                       if (SBI_PMU_FW_PLATFORM == event_code &&
                            pmu_dev && pmu_dev->fw_counter_start) {
-                               ret = pmu_dev->fw_counter_start(
-                                       ctr_idx - num_hw_ctrs, event_code,
-                                       fw_counters_value[hartid][ctr_idx - num_hw_ctrs],
-                                       true);
+                               ret = pmu_dev->fw_counter_start(ctr_idx - num_hw_ctrs,
+                                                       event_data);
                                if (ret)
                                        return ret;
                        }
@@ -743,7 +767,7 @@ int sbi_pmu_ctr_incr_fw(enum sbi_pmu_fw_event_code_id fw_id)
        for (cidx = num_hw_ctrs; cidx < total_ctrs; cidx++) {
                if (get_cidx_code(active_events[hartid][cidx]) == fw_id &&
                    (fw_counters_started[hartid] & BIT(cidx - num_hw_ctrs))) {
-                       fcounter = &fw_counters_value[hartid][cidx - num_hw_ctrs];
+                       fcounter = &fw_counters_data[hartid][cidx - num_hw_ctrs];
                        break;
                }
        }
@@ -803,7 +827,7 @@ static void pmu_reset_event_map(u32 hartid)
        for (j = 3; j < total_ctrs; j++)
                active_events[hartid][j] = SBI_PMU_EVENT_IDX_INVALID;
        for (j = 0; j < SBI_PMU_FW_CTR_MAX; j++)
-               fw_counters_value[hartid][j] = 0;
+               fw_counters_data[hartid][j] = 0;
        fw_counters_started[hartid] = 0;
 }
 
--

2.34.1

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

Signature · 2 lines
Regards,
Atish
#9 lore raw

PATCH v2 5/5 lib: sbi_pmu: Add hartid parameter PMU device ops

In reply to Mayuresh Chitale

Pass the hartid parameter to custom PMU device ops. It is required by platform event handler to be able to program correct registers for a given counter.

---
 include/sbi/sbi_pmu.h | 18 +++++++++++-------
 lib/sbi/sbi_pmu.c     | 28 +++++++++++++++++-----------
 2 files changed, 28 insertions(+), 18 deletions(-)
include/sbi/sbi_pmu.h +11 -7
index 467ec03..90a564f 100644
@@ -31,14 +31,15 @@ struct sbi_pmu_device {
 	/**
 	 * Validate event code of custom firmware event
 	 */
-	int (*fw_event_validate_code)(uint64_t event_idx_code);
+	int (*fw_event_validate_encoding)(uint64_t event_idx_code);
 
 	/**
 	 * Match custom firmware counter with custom firmware event
 	 * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
 	 */
-	bool (*fw_counter_match_code)(uint32_t counter_index,
-				      uint64_t event_idx_code);
+	bool (*fw_counter_match_encoding)(uint32_t hartid,
+					  uint32_t counter_index,
+					  uint64_t event_idx_code);
 
 	/**
 	 * Fetch the max width of this counter in number of bits.
@@ -49,27 +50,30 @@ struct sbi_pmu_device {
 	 * Read value of custom firmware counter
 	 * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
 	 */
-	uint64_t (*fw_counter_read_value)(uint32_t counter_index);
+	uint64_t (*fw_counter_read_value)(uint32_t hartid,
+					  uint32_t counter_index);
 
 	/**
 	 * Write value to custom firmware counter
 	 * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
 	 */
-	void (*fw_counter_write_value)(uint32_t counter_index,
+	void (*fw_counter_write_value)(uint32_t hartid,
+				       uint32_t counter_index,
 				       uint64_t value);
 
 	/**
 	 * Start custom firmware counter
 	 * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
 	 */
-	int (*fw_counter_start)(uint32_t counter_index,
+	int (*fw_counter_start)(uint32_t hartid,
+				uint32_t counter_index,
 				uint64_t event_idx_code);
 
 	/**
 	 * Stop custom firmware counter
 	 * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
 	 */
-	int (*fw_counter_stop)(uint32_t counter_index);
+	int (*fw_counter_stop)(uint32_t hartid, uint32_t counter_index);
 
 	/**
 	 * Custom enable irq for hardware counter
lib/sbi/sbi_pmu.c +17 -11
index 05e2571..4479aa4 100644
@@ -123,8 +123,8 @@ static int pmu_event_validate(unsigned long event_idx, uint64_t edata)
 		break;
 	case SBI_PMU_EVENT_TYPE_FW:
 		if (SBI_PMU_FW_PLATFORM == event_idx_code &&
-		    pmu_dev && pmu_dev->fw_event_validate_code)
-			return pmu_dev->fw_event_validate_code(edata);
+		    pmu_dev && pmu_dev->fw_event_validate_encoding)
+			return pmu_dev->fw_event_validate_encoding(edata);
 		else
 			event_idx_code_max = SBI_PMU_FW_MAX;
 		break;
@@ -189,7 +189,8 @@ int sbi_pmu_ctr_fw_read(uint32_t cidx, uint64_t *cval)
 
 	if (SBI_PMU_FW_PLATFORM == event_code) {
 		if (pmu_dev && pmu_dev->fw_counter_read_value)
-			*cval = pmu_dev->fw_counter_read_value(cidx -
+			*cval = pmu_dev->fw_counter_read_value(hartid,
+							       cidx -
 							       num_hw_ctrs);
 		else
 			*cval = 0;
@@ -376,7 +377,8 @@ static int pmu_ctr_start_fw(uint32_t cidx, uint32_t event_code,
 		    }
 
 		if (ival_update)
-			pmu_dev->fw_counter_write_value(cidx - num_hw_ctrs,
+			pmu_dev->fw_counter_write_value(hartid,
+							cidx - num_hw_ctrs,
 							ival);
 
 		if (pmu_dev->fw_counter_start(hartid, cidx - num_hw_ctrs,
@@ -453,11 +455,12 @@ static int pmu_ctr_stop_hw(uint32_t cidx)
 
 static int pmu_ctr_stop_fw(uint32_t cidx, uint32_t event_code)
 {
+	u32 hartid = current_hartid();
 	int ret;
 
 	if (SBI_PMU_FW_PLATFORM == event_code &&
 	    pmu_dev && pmu_dev->fw_counter_stop) {
-		ret = pmu_dev->fw_counter_stop(cidx - num_hw_ctrs);
+		ret = pmu_dev->fw_counter_stop(hartid, cidx - num_hw_ctrs);
 		if (ret)
 			return ret;
 	}
@@ -672,10 +675,11 @@ static int pmu_ctr_find_fw(unsigned long cbase, unsigned long cmask,
 		if (active_events[hartid][i] != SBI_PMU_EVENT_IDX_INVALID)
 			continue;
 		if (SBI_PMU_FW_PLATFORM == event_code &&
-		    pmu_dev && pmu_dev->fw_counter_match_code) {
-			if (!pmu_dev->fw_counter_match_code(cidx -
-							    num_hw_ctrs,
-							    edata))
+		    pmu_dev && pmu_dev->fw_counter_match_encoding) {
+			if (!pmu_dev->fw_counter_match_encoding(hartid,
+							        cidx -
+								num_hw_ctrs,
+								edata))
 				continue;
 		}
 
@@ -741,8 +745,10 @@ skip_match:
 		if (flags & SBI_PMU_CFG_FLAG_AUTO_START) {
 			if (SBI_PMU_FW_PLATFORM == event_code &&
 			    pmu_dev && pmu_dev->fw_counter_start) {
-				ret = pmu_dev->fw_counter_start(ctr_idx - num_hw_ctrs,
-							event_data);
+				ret = pmu_dev->fw_counter_start(hartid,
+								ctr_idx -
+								num_hw_ctrs,
+								event_data);
 				if (ret)
 					return ret;
 			}
Signature · 1 line
2.34.1
#10 lore raw

PATCH v2 5/5 lib: sbi_pmu: Add hartid parameter PMU device ops

Quoted 5 lines · Mayuresh Chitale

Pass the hartid parameter to custom PMU device ops. It is required by platform event handler to be able to program correct registers for a given counter.

This can be improved..something along the lines

"Platform event handler may leverage the hartid to program per hart specific registers
for a given counter."

Quoted 146 lines
---
 include/sbi/sbi_pmu.h | 18 +++++++++++-------
 lib/sbi/sbi_pmu.c     | 28 +++++++++++++++++-----------
 2 files changed, 28 insertions(+), 18 deletions(-)
include/sbi/sbi_pmu.h +11 -7
index 467ec03..90a564f 100644
@@ -31,14 +31,15 @@ struct sbi_pmu_device {
        /**
         * Validate event code of custom firmware event
         */
-       int (*fw_event_validate_code)(uint64_t event_idx_code);
+       int (*fw_event_validate_encoding)(uint64_t event_idx_code);
 
        /**
         * Match custom firmware counter with custom firmware event
         * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
         */
-       bool (*fw_counter_match_code)(uint32_t counter_index,
-                                     uint64_t event_idx_code);
+       bool (*fw_counter_match_encoding)(uint32_t hartid,
+                                         uint32_t counter_index,
+                                         uint64_t event_idx_code);
 
        /**
         * Fetch the max width of this counter in number of bits.
@@ -49,27 +50,30 @@ struct sbi_pmu_device {
         * Read value of custom firmware counter
         * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
         */
-       uint64_t (*fw_counter_read_value)(uint32_t counter_index);
+       uint64_t (*fw_counter_read_value)(uint32_t hartid,
+                                         uint32_t counter_index);
 
        /**
         * Write value to custom firmware counter
         * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
         */
-       void (*fw_counter_write_value)(uint32_t counter_index,
+       void (*fw_counter_write_value)(uint32_t hartid,
+                                      uint32_t counter_index,
                                       uint64_t value);
 
        /**
         * Start custom firmware counter
         * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
         */
-       int (*fw_counter_start)(uint32_t counter_index,
+       int (*fw_counter_start)(uint32_t hartid,
+                               uint32_t counter_index,
                                uint64_t event_idx_code);
 
        /**
         * Stop custom firmware counter
         * Note: 0 <= counter_index < SBI_PMU_FW_CTR_MAX
         */
-       int (*fw_counter_stop)(uint32_t counter_index);
+       int (*fw_counter_stop)(uint32_t hartid, uint32_t counter_index);
 
        /**
         * Custom enable irq for hardware counter
lib/sbi/sbi_pmu.c +17 -12
index 05e2571..4479aa4 100644
@@ -123,8 +123,8 @@ static int pmu_event_validate(unsigned long event_idx, uint64_t edata)
                break;
        case SBI_PMU_EVENT_TYPE_FW:
                if (SBI_PMU_FW_PLATFORM == event_idx_code &&
-                   pmu_dev && pmu_dev->fw_event_validate_code)
-                       return pmu_dev->fw_event_validate_code(edata);
+                   pmu_dev && pmu_dev->fw_event_validate_encoding)
+                       return pmu_dev->fw_event_validate_encoding(edata);
                else
                        event_idx_code_max = SBI_PMU_FW_MAX;
                break;
@@ -189,7 +189,8 @@ int sbi_pmu_ctr_fw_read(uint32_t cidx, uint64_t *cval)
 
        if (SBI_PMU_FW_PLATFORM == event_code) {
                if (pmu_dev && pmu_dev->fw_counter_read_value)
-                       *cval = pmu_dev->fw_counter_read_value(cidx -
+                       *cval = pmu_dev->fw_counter_read_value(hartid,
+                                                              cidx -
                                                               num_hw_ctrs);
                else
                        *cval = 0;
@@ -376,7 +377,8 @@ static int pmu_ctr_start_fw(uint32_t cidx, uint32_t event_code,
                    }
 
                if (ival_update)
-                       pmu_dev->fw_counter_write_value(cidx - num_hw_ctrs,
+                       pmu_dev->fw_counter_write_value(hartid,
+                                                       cidx - num_hw_ctrs,
                                                        ival);
 
                if (pmu_dev->fw_counter_start(hartid, cidx - num_hw_ctrs,
@@ -453,11 +455,12 @@ static int pmu_ctr_stop_hw(uint32_t cidx)
 
 static int pmu_ctr_stop_fw(uint32_t cidx, uint32_t event_code)
 {
+       u32 hartid = current_hartid();
        int ret;
 
        if (SBI_PMU_FW_PLATFORM == event_code &&
            pmu_dev && pmu_dev->fw_counter_stop) {
-               ret = pmu_dev->fw_counter_stop(cidx - num_hw_ctrs);
+               ret = pmu_dev->fw_counter_stop(hartid, cidx - num_hw_ctrs);
                if (ret)
                        return ret;
        }
@@ -672,10 +675,11 @@ static int pmu_ctr_find_fw(unsigned long cbase, unsigned long cmask,
                if (active_events[hartid][i] != SBI_PMU_EVENT_IDX_INVALID)
                        continue;
                if (SBI_PMU_FW_PLATFORM == event_code &&
-                   pmu_dev && pmu_dev->fw_counter_match_code) {
-                       if (!pmu_dev->fw_counter_match_code(cidx -
-                                                           num_hw_ctrs,
-                                                           edata))
+                   pmu_dev && pmu_dev->fw_counter_match_encoding) {
+                       if (!pmu_dev->fw_counter_match_encoding(hartid,
+                                                               cidx -
+                                                               num_hw_ctrs,
+                                                               edata))
                                continue;
                }
 
@@ -741,8 +745,10 @@ skip_match:
                if (flags & SBI_PMU_CFG_FLAG_AUTO_START) {
                        if (SBI_PMU_FW_PLATFORM == event_code &&
                            pmu_dev && pmu_dev->fw_counter_start) {
-                               ret = pmu_dev->fw_counter_start(ctr_idx - num_hw_ctrs,
-                                                       event_data);
+                               ret = pmu_dev->fw_counter_start(hartid,
+                                                               ctr_idx -
+                                                               num_hw_ctrs,
+                                                               event_data);
                                if (ret)
                                        return ret;
                        }
--

2.34.1

Quoted 4 lines

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

IMO, this patch can be applied before PATCH4.

Either Way,

Signature · 2 lines
Regards,
Atish
#11 lore raw

v2 cover SBI PMU firmware counters and events improvement

In reply to Mayuresh Chitale
Quoted 6 lines · Mayuresh Chitale

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

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

For the larger audience, there is a v2 available now.
https://lists.riscv.org/g/tech-prs/message/211

Quoted 26 lines

Changes in v2:
- Update Risc V platform specific firmware event handling as per the SBI spec update: https://lists.riscv.org/g/tech-prs/message/211

Mayuresh Chitale (5):
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
lib: sbi_pmu: Update custom PMU device ops
lib: sbi_pmu: Add hartid parameter PMU device ops

 include/sbi/sbi_ecall_interface.h |  12 +++
 include/sbi/sbi_pmu.h             |  33 +++++---
 lib/sbi/sbi_ecall_pmu.c           |   8 ++
 lib/sbi/sbi_pmu.c                 | 120 +++++++++++++++++++-----------
 4 files changed, 121 insertions(+), 52 deletions(-)

--
2.34.1

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

Signature · 2 lines
Regards,
Atish