[PATCH v10 10/33] perf: arm_pmuv3: Expose number of general-purpose counters in sysfs

From: Colton Lewis

Date: Thu Nov 19 2026 - 13:53:31 EST


Expose the number of general-purpose event counters implemented by each
ARMv8 PMUv3 instance at
/sys/bus/event_source/devices/<pmu>/caps/num_counters.

This allows userspace (such as VMMs configuring
KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS for a Partitioned PMU on
heterogeneous systems) to query the maximum number of general-purpose
counters for a specific PMU without reading PMCR_EL0.N directly from
EL0.

Suggested-by: James Clark <james.clark@xxxxxxxxxx>
Signed-off-by: Colton Lewis <coltonlewis@xxxxxxxxxx>
---
drivers/perf/arm_pmuv3.c | 14 ++++++++++++++
1 file changed, 14 insertions(+)

diff --git a/drivers/perf/arm_pmuv3.c b/drivers/perf/arm_pmuv3.c
index 667ce27d2347c..1676f13cc7c38 100644
--- a/drivers/perf/arm_pmuv3.c
+++ b/drivers/perf/arm_pmuv3.c
@@ -457,12 +457,26 @@ static ssize_t branches_show(struct device *dev,

static DEVICE_ATTR_RO(branches);

+static ssize_t num_counters_show(struct device *dev,
+ struct device_attribute *attr, char *page)
+{
+ struct pmu *pmu = dev_get_drvdata(dev);
+ struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
+ int nr_cntrs = bitmap_weight(cpu_pmu->cntr_mask,
+ ARMV8_PMU_MAX_GENERAL_COUNTERS);
+
+ return sysfs_emit(page, "%d\n", nr_cntrs);
+}
+
+static DEVICE_ATTR_RO(num_counters);
+
static struct attribute *armv8_pmuv3_caps_attrs[] = {
&dev_attr_branches.attr,
&dev_attr_slots.attr,
&dev_attr_bus_slots.attr,
&dev_attr_bus_width.attr,
&dev_attr_threshold_max.attr,
+ &dev_attr_num_counters.attr,
NULL,
};

--
2.56.1.708.g77570a6a1d-goog