[PATCH v5 5/7] perf: arm_pmuv3: Add pmuv3_event_implemented()
From: Amir Ayupov
Date: Fri Nov 20 2026 - 10:28:43 EST
Drivers outside the PMU driver may need to know whether the local CPU's
PMU implements a common event before relying on it, for example a trace
unit driver checking that the PMU counts its external outputs as
TRCEXTOUTn.
Add pmuv3_event_implemented() to the arm64 asm/arm_pmuv3.h, next to
read_pmceid0() and read_pmceid1(). It reads PMCEID0/1 on the calling CPU:
the first ARMV8_PMUV3_MAX_COMMON_EVENTS events are reported in bits
[31:0], and as many from ARMV8_PMUV3_EXT_COMMON_EVENT_BASE in bits
[63:32] from PMUv3p1.
Move ARMV8_PMUV3_MAX_COMMON_EVENTS and ARMV8_PMUV3_EXT_COMMON_EVENT_BASE
next to the event numbers in linux/perf/arm_pmuv3.h, so that both are
available without CONFIG_ARM_PMU, and include it from
linux/perf/arm_pmu.h, which sizes the PMCEID bitmaps with the former.
Suggested-by: James Clark <james.clark@xxxxxxxxxx>
Assisted-by: Devmate:Opus-5.5
Reviewed-by: James Clark <james.clark@xxxxxxxxxx>
Tested-by: Leo Yan <leo.yan@xxxxxxx>
Signed-off-by: Amir Ayupov <aaupov@xxxxxx>
---
Will, Mark: could you Ack this so Suzuki can take it through the
CoreSight tree with the rest of the series?
arch/arm64/include/asm/arm_pmuv3.h | 20 ++++++++++++++++++++
include/linux/perf/arm_pmu.h | 3 +--
include/linux/perf/arm_pmuv3.h | 8 ++++++++
3 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/arch/arm64/include/asm/arm_pmuv3.h b/arch/arm64/include/asm/arm_pmuv3.h
index 8a777dec8d88ab..f6882771ec7970 100644
--- a/arch/arm64/include/asm/arm_pmuv3.h
+++ b/arch/arm64/include/asm/arm_pmuv3.h
@@ -173,6 +173,26 @@ static inline bool pmuv3_implemented(int pmuver)
pmuver == ID_AA64DFR0_EL1_PMUVer_NI);
}
+/*
+ * Whether this CPU's PMU implements a common event, as reported by
+ * PMCEID{0,1}: events 0x0000-0x003f in bits [31:0], and from PMUv3p1 events
+ * 0x4000-0x403f in bits [63:32], which read as zero before PMUv3p1.
+ * Must run on the CPU whose PMU is queried.
+ */
+static inline bool pmuv3_event_implemented(u16 event)
+{
+ bool ext = event >= ARMV8_PMUV3_EXT_COMMON_EVENT_BASE;
+ u16 id = ext ? event - ARMV8_PMUV3_EXT_COMMON_EVENT_BASE : event;
+ u64 pmceid;
+
+ if (id >= ARMV8_PMUV3_MAX_COMMON_EVENTS ||
+ !pmuv3_implemented(read_pmuver()))
+ return false;
+
+ pmceid = id < 32 ? read_pmceid0() : read_pmceid1();
+ return pmceid & BIT_ULL(id % 32 + (ext ? 32 : 0));
+}
+
static inline bool is_pmuv3p4(int pmuver)
{
return pmuver >= ID_AA64DFR0_EL1_PMUVer_V3P4;
diff --git a/include/linux/perf/arm_pmu.h b/include/linux/perf/arm_pmu.h
index 02d2c7f45b527d..a90867c9774c74 100644
--- a/include/linux/perf/arm_pmu.h
+++ b/include/linux/perf/arm_pmu.h
@@ -10,6 +10,7 @@
#include <linux/interrupt.h>
#include <linux/perf_event.h>
+#include <linux/perf/arm_pmuv3.h>
#include <linux/platform_device.h>
#include <linux/sysfs.h>
#include <asm/cputype.h>
@@ -122,9 +123,7 @@ struct arm_pmu {
bool avoid_pmccntr;
u64 reg_pmmir;
u64 reg_brbidr;
-#define ARMV8_PMUV3_MAX_COMMON_EVENTS 0x40
DECLARE_BITMAP(pmceid_bitmap, ARMV8_PMUV3_MAX_COMMON_EVENTS);
-#define ARMV8_PMUV3_EXT_COMMON_EVENT_BASE 0x4000
DECLARE_BITMAP(pmceid_ext_bitmap, ARMV8_PMUV3_MAX_COMMON_EVENTS);
/* Only to be used by ACPI probing code */
diff --git a/include/linux/perf/arm_pmuv3.h b/include/linux/perf/arm_pmuv3.h
index d698efba28a276..5bab0f952fd59c 100644
--- a/include/linux/perf/arm_pmuv3.h
+++ b/include/linux/perf/arm_pmuv3.h
@@ -78,6 +78,14 @@
#define ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND 0x003E
#define ARMV8_PMUV3_PERFCTR_STALL_SLOT 0x003F
+/*
+ * PMCEID{0,1} report ARMV8_PMUV3_MAX_COMMON_EVENTS common events from 0 in
+ * bits [31:0], and as many from ARMV8_PMUV3_EXT_COMMON_EVENT_BASE in bits
+ * [63:32] from PMUv3p1.
+ */
+#define ARMV8_PMUV3_MAX_COMMON_EVENTS 0x40
+#define ARMV8_PMUV3_EXT_COMMON_EVENT_BASE 0x4000
+
/* Statistical profiling extension microarchitectural events */
#define ARMV8_SPE_PERFCTR_SAMPLE_POP 0x4000
#define ARMV8_SPE_PERFCTR_SAMPLE_FEED 0x4001
--
2.52.1