lib: sbi: Fix counter index calculation for SBI_PMU_CFG_FLAG_SKIP_MATCH
authorAlexandre Ghiti <alexghiti@rivosinc.com>
Thu, 13 Apr 2023 14:02:19 +0000 (16:02 +0200)
committerAnup Patel <anup@brainfault.org>
Mon, 17 Apr 2023 03:56:28 +0000 (09:26 +0530)
As per the SBI specification, we should "unconditionally select the first
counter from the set of counters specified by the counter_idx_base and
counter_idx_mask", so implement this behaviour.

Suggested-by: Atish Patra <atishp@atishpatra.org>
Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
lib/sbi/sbi_pmu.c

index f28c9f5e80a0d076dfdd8c59739ac65104ea2479..939f29d841e2d03c2896a018020c46ee0300b0db 100644 (file)
@@ -736,10 +736,15 @@ int sbi_pmu_ctr_cfg_match(unsigned long cidx_base, unsigned long cidx_mask,
                /* The caller wants to skip the match because it already knows the
                 * counter idx for the given event. Verify that the counter idx
                 * is still valid.
+                * As per the specification, we should "unconditionally select
+                * the first counter from the set of counters specified by the
+                * counter_idx_base and counter_idx_mask".
                 */
-               if (active_events[hartid][cidx_base] == SBI_PMU_EVENT_IDX_INVALID)
+               unsigned long cidx_first = cidx_base + sbi_ffs(cidx_mask);
+
+               if (active_events[hartid][cidx_first] == SBI_PMU_EVENT_IDX_INVALID)
                        return SBI_EINVAL;
-               ctr_idx = cidx_base;
+               ctr_idx = cidx_first;
                goto skip_match;
        }