Skip to contentSkip to conversation

lib: sbi: sbi_pmu: Fix multiple start and stop operations of FW counters

PATCH Applied James Raphael Tiovalen 2 participants 2 messages Dec 13, 2025 – Dec 27, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: sbi: sbi_pmu: Fix multiple start and stop operations of FW counters

Currently, OpenSBI returns SBI_ERR_ALREADY_STARTED when attempting to start a HW counter that is already started and SBI_ERR_ALREADY_STOPPED when attempting to stop a HW counter that is already stopped. However, this is not yet implemented for FW counters.

Add the necessary checks to return the same error codes when attempting the same actions on FW counters.

---
 lib/sbi/sbi_pmu.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/sbi/sbi_pmu.c +6 -0
index e24e485d..0218c921 100644
@@ -468,6 +468,9 @@ static int pmu_ctr_start_fw(struct sbi_pmu_hart_state *phs,
 						 cidx - num_hw_ctrs,
 						 event_data);
 	} else {
+		if (phs->fw_counters_started & BIT(cidx - num_hw_ctrs))
+			return SBI_EALREADY_STARTED;
+
 		if (ival_update)
 			phs->fw_counters_data[cidx - num_hw_ctrs] = ival;
 	}
@@ -629,6 +632,9 @@ static int pmu_ctr_stop_fw(struct sbi_pmu_hart_state *phs,
 			return ret;
 	}
 
+	if (!(phs->fw_counters_started & BIT(cidx - num_hw_ctrs)))
+			return SBI_EALREADY_STOPPED;
+
 	phs->fw_counters_started &= ~BIT(cidx - num_hw_ctrs);
 
 	return 0;
Signature · 7 lines
2.43.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v1 1/1 Applied
#1 lore raw
Quoted 19 lines · James Raphael Tiovalen

Currently, OpenSBI returns SBI_ERR_ALREADY_STARTED when attempting to start a HW counter that is already started and SBI_ERR_ALREADY_STOPPED when attempting to stop a HW counter that is already stopped. However, this is not yet implemented for FW counters.

Add the necessary checks to return the same error codes when attempting the same actions on FW counters.

---
 lib/sbi/sbi_pmu.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/sbi/sbi_pmu.c +0 -0
index e24e485d..0218c921 100644

lib/sbi/sbi_pmu.c static int pmu_ctr_start_fw(struct sbi_pmu_hart_state *phs, · line 473

Quoted 6 lines
lib/sbi/sbi_pmu.c +3 -0
@@ -468,6 +468,9 @@ static int pmu_ctr_start_fw(struct sbi_pmu_hart_state *phs,
                                                 cidx - num_hw_ctrs,
                                                 event_data);
        } else {
+               if (phs->fw_counters_started & BIT(cidx - num_hw_ctrs))
+                       return SBI_EALREADY_STARTED;
+

This check should also be done for platform firmware counters hence it should be just before this if-else block.

lib/sbi/sbi_pmu.c static int pmu_ctr_stop_fw(struct sbi_pmu_hart_state *phs, · line 637

Quoted 10 lines
lib/sbi/sbi_pmu.c +3 -0
                if (ival_update)
                        phs->fw_counters_data[cidx - num_hw_ctrs] = ival;
        }
@@ -629,6 +632,9 @@ static int pmu_ctr_stop_fw(struct sbi_pmu_hart_state *phs,
                        return ret;
        }
 
+       if (!(phs->fw_counters_started & BIT(cidx - num_hw_ctrs)))
+                       return SBI_EALREADY_STOPPED;
+

As comment as above, this check should be before the previous if block.

Quoted 11 lines
lib/sbi/sbi_pmu.c +0 -1
        phs->fw_counters_started &= ~BIT(cidx - num_hw_ctrs);
 
        return 0;
--

2.43.0

--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi

I have taken care of the above comments at the time of merging this patch.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi