Skip to contentSkip to conversation

arm_pmu: acpi: variant support and QCOM Falkor extensions

RFC In review Agustin Vega-Frias 3 participants 8 messages Jul 5, 2018 – Jul 25, 2018

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

v4 cover arm_pmu: acpi: variant support and QCOM Falkor extensions

This series is a complete re-design of V1 of the QCOM Falkor extensions [1], it introduces a probe table based on the HID of a device nested under the CPU device to allow variant detection and arm_pmu customization.

The first patch adds an additional section at the end of each ACPI probe table. This allows probe tables to be sentinel-delimited and better accommodate some APIs that require such tables.

The second patch adds the PMUv3 ACPI probe table and plumbing to allow drivers to plug into the ACPI PMUv3 probe sequence.

The third patch adds the PC capture extension applicable to Falkor and Saphira CPUs. This shows how an extension that uses sampling events hooks. A similar approach can be used to add RBB support and populate the sample branch stack from it.

The fourth patch adds the matrix-based events extension applicable to Falkor only.

If this found to be a reasonable extension approach other patches will be added to the series to build on the base QCOM extensions.

[1] https://lkml.org/lkml/2017/3/1/540

Changes since V3:
- Add PC capture support as first extension. Add matrix-based events on top.

Changes since V2:
- Address V2 comments, which resulted in removing all uses of the PMU lock.

Agustin Vega-Frias (3):
ACPI: add support for sentinel-delimited probe tables
arm_pmu: acpi: add support for CPU PMU variant detection
perf: qcom: Add Falkor CPU PMU IMPLEMENTATION DEFINED event support

 drivers/perf/Makefile             |   2 +-
 drivers/perf/arm_pmu_acpi.c       |  27 ++++
 drivers/perf/qcom_arm_pmu.c       | 310 ++++++++++++++++++++++++++++++++++++++
 include/asm-generic/vmlinux.lds.h |   4 +-
 include/linux/acpi.h              |  11 ++
 include/linux/perf/arm_pmu.h      |   1 +
 6 files changed, 353 insertions(+), 2 deletions(-)
 create mode 100644 drivers/perf/qcom_arm_pmu.c

--
Qualcomm Datacenter Technologies, Inc. on behalf of the Qualcomm Technologies, Inc. Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.

#1 lore raw

RFC v4 1/4 ACPI: add support for sentinel-delimited probe tables

Tables declared with the ACPI_PROBE_TABLE linker macro are typically traversed by using the start and end symbols created by the linker script. However, there are some APIs that use sentinel-delimited tables (e.g. acpi_match_device). To better support these APIs an additional section is added at the end of the probe table. This section can be used to add a sentinel for tables that require it.

---
 include/asm-generic/vmlinux.lds.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/asm-generic/vmlinux.lds.h +2 -1
index e373e2e..1ec0411 100644
@@ -228,7 +228,8 @@
 	. = ALIGN(8);							\
 	__##name##_acpi_probe_table = .;				\
 	KEEP(*(__##name##_acpi_probe_table))				\
-	__##name##_acpi_probe_table_end = .;
+	__##name##_acpi_probe_table_end = .;				\
+	KEEP(*(__##name##_acpi_probe_table_end))
 #else
 #define ACPI_PROBE_TABLE(name)
 #endif
Signature · 2 lines
Qualcomm Datacenter Technologies, Inc. on behalf of the Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
#2 lore raw

RFC v4 2/4 arm_pmu: acpi: add support for CPU PMU variant detection

In reply to Agustin Vega-Frias

DT allows CPU PMU variant detection via the PMU device compatible property. ACPI does not have an equivalent mechanism so we introduce a probe table to allow this via a device nested inside the CPU device in the DSDT:

Device (CPU0)

{
    Name (_HID, "ACPI0007" /* Processor Device */)
    ...
    Device (PMU0)
    {
        Name (_HID, "QCOM8150") /* Qualcomm Falkor PMU device */

        /*
         * The device might also contain _DSD properties to indicate other
         * IMPLEMENTATION DEFINED PMU features.
         */
        Name (_DSD, Package ()
        {
            ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
            Package ()
            {
                ...
            }
        })
    }
}

With this in place we can declare the variant:

    ACPI_DECLARE_PMU_VARIANT(qcom_falkor, "QCOM8150", falkor_pmu_init);

The init function is called after the default PMU initialization and is passed a pointer to the arm_pmu structure and a pointer to the PMU device. The init function can then override arm_pmu callbacks and attributes and query more properties from the PMU device.

---
 drivers/perf/arm_pmu_acpi.c       | 27 +++++++++++++++++++++++++++
 include/asm-generic/vmlinux.lds.h |  1 +
 include/linux/acpi.h              | 11 +++++++++++
 include/linux/perf/arm_pmu.h      |  1 +
 4 files changed, 40 insertions(+)
drivers/perf/arm_pmu_acpi.c +27 -0
index 0f19751..6b0ca71 100644
@@ -220,6 +220,26 @@ static int arm_pmu_acpi_cpu_starting(unsigned int cpu)
 	return 0;
 }
 
+/*
+ * Check if the given child device of the CPU device matches a PMU variant
+ * device declared with ACPI_DECLARE_PMU_VARIANT, if so, pass the arm_pmu
+ * structure and the matching device for further initialization.
+ */
+static int arm_pmu_variant_init(struct device *dev, void *data)
+{
+	extern struct acpi_device_id ACPI_PROBE_TABLE(pmu);
+	unsigned int cpu = *((unsigned int *)data);
+	const struct acpi_device_id *id;
+
+	id = acpi_match_device(&ACPI_PROBE_TABLE(pmu), dev);
+	if (id) {
+		armpmu_acpi_init_fn fn = (armpmu_acpi_init_fn)id->driver_data;
+
+		return fn(per_cpu(probed_pmus, cpu), dev);
+	}
+	return 0;
+}
+
 int arm_pmu_acpi_probe(armpmu_init_fn init_fn)
 {
 	int pmu_idx = 0;
@@ -240,6 +260,7 @@ int arm_pmu_acpi_probe(armpmu_init_fn init_fn)
 	 */
 	for_each_possible_cpu(cpu) {
 		struct arm_pmu *pmu = per_cpu(probed_pmus, cpu);
+		struct device *dev = get_cpu_device(cpu);
 		char *base_name;
 
 		if (!pmu || pmu->name)
@@ -254,6 +275,10 @@ int arm_pmu_acpi_probe(armpmu_init_fn init_fn)
 			return ret;
 		}
 
+		ret = device_for_each_child(dev, &cpu, arm_pmu_variant_init);
+		if (ret == -ENODEV)
+			pr_warn("Failed PMU re-init, fallback to plain PMUv3");
+
 		base_name = pmu->name;
 		pmu->name = kasprintf(GFP_KERNEL, "%s_%d", base_name, pmu_idx++);
 		if (!pmu->name) {
@@ -290,3 +315,5 @@ static int arm_pmu_acpi_init(void)
 	return ret;
 }
 subsys_initcall(arm_pmu_acpi_init)
+
+ACPI_DECLARE_PMU_SENTINEL();
include/asm-generic/vmlinux.lds.h +1 -0
index 1ec0411..03d1673 100644
@@ -609,6 +609,7 @@
 	IRQCHIP_OF_MATCH_TABLE()					\
 	ACPI_PROBE_TABLE(irqchip)					\
 	ACPI_PROBE_TABLE(timer)						\
+	ACPI_PROBE_TABLE(pmu)						\
 	EARLYCON_TABLE()
 
 #define INIT_TEXT							\
include/linux/acpi.h +11 -0
index e54f409..99c6698 100644
@@ -1158,6 +1158,17 @@ struct acpi_probe_entry {
 					  (&ACPI_PROBE_TABLE_END(t) -	\
 					   &ACPI_PROBE_TABLE(t)));	\
 	})
+
+#define ACPI_DECLARE_PMU_VARIANT(name, hid, init_fn)			\
+	static const struct acpi_device_id __acpi_probe_##name		\
+		__used __section(__pmu_acpi_probe_table)		\
+		= { .id = hid, .driver_data = (kernel_ulong_t)init_fn }
+
+#define ACPI_DECLARE_PMU_SENTINEL()					\
+	static const struct acpi_device_id __acpi_probe_sentinel	\
+		__used __section(__pmu_acpi_probe_table_end)		\
+		= { .id = "", .driver_data = 0 }
+
 #else
 static inline int acpi_dev_get_property(struct acpi_device *adev,
 					const char *name, acpi_object_type type,
include/linux/perf/arm_pmu.h +1 -0
index ad54444..b02ac52 100644
@@ -123,6 +123,7 @@ int armpmu_map_event(struct perf_event *event,
 		     u32 raw_event_mask);
 
 typedef int (*armpmu_init_fn)(struct arm_pmu *);
+typedef int (*armpmu_acpi_init_fn)(struct arm_pmu *, struct device *);
 
 struct pmu_probe_info {
 	unsigned int cpuid;
Signature · 2 lines
Qualcomm Datacenter Technologies, Inc. on behalf of the Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
#3 lore raw

RFC v4 3/4 perf: qcom: Add PC capture support to CPU PMU

In reply to Agustin Vega-Frias

PC capture is an IMPLEMENTATION DEFINED extension to the ARMv8 PMUv3 that allows more precise PC sampling by storing the PC in a separate system register when an event counter overflow occurs. Reduces skid and allows sampling when interrupts are disabled (since the PMI is a maskable interrupt in arm64). Note that there is only one PC capture register so we only allow one event at a time to use it.

Support for this extension is signaled by the presence of the Falkor or Saphira PMU device node under a CPU device node in the DSDT ACPI table containing the u8 _DSD property "qcom,pmu-pcc-support" set to non-zero. E.g.:

    Device (CPU0)
    {
        Name (_HID, "ACPI0007" /* Processor Device */)
        ...
        Device (PMU0)
        {
            Name (_HID, "QCOM8150") /* Qualcomm Falkor PMU device */
            Name (_DSD, Package () {
                ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
                Package () {
                    Package () {"qcom,pmu-pcc-support", 1},
                }
            })
        }
    }
---
 arch/arm64/include/asm/perf_event.h |  18 ++
 arch/arm64/kernel/perf_event.c      |  18 --
 drivers/perf/Makefile               |   2 +-
 drivers/perf/qcom_arm_pmu.c         | 398 ++++++++++++++++++++++++++++++++++++
 include/linux/perf_event.h          |   4 +-
 5 files changed, 420 insertions(+), 20 deletions(-)
 create mode 100644 drivers/perf/qcom_arm_pmu.c
arch/arm64/include/asm/perf_event.h +18 -0
index f9ccc36..76b95a3 100644
@@ -24,6 +24,24 @@
 #define	ARMV8_PMU_COUNTER_MASK	(ARMV8_PMU_MAX_COUNTERS - 1)
 
 /*
+ * Perf Events' indices
+ */
+#define	ARMV8_IDX_CYCLE_COUNTER	0
+#define	ARMV8_IDX_COUNTER0	1
+#define	ARMV8_IDX_COUNTER_LAST(cpu_pmu) \
+	(ARMV8_IDX_CYCLE_COUNTER + cpu_pmu->num_events - 1)
+
+/*
+ * ARMv8 low level PMU access
+ */
+
+/*
+ * Perf Event to low level counters mapping
+ */
+#define	ARMV8_IDX_TO_COUNTER(x)	\
+	(((x) - ARMV8_IDX_COUNTER0) & ARMV8_PMU_COUNTER_MASK)
+
+/*
  * Per-CPU PMCR: config reg
  */
 #define ARMV8_PMU_PMCR_E	(1 << 0) /* Enable all counters */
arch/arm64/kernel/perf_event.c +0 -18
index 33147aa..81b32fb 100644
@@ -457,24 +457,6 @@
 	.attrs = armv8_pmuv3_format_attrs,
 };
 
-/*
- * Perf Events' indices
- */
-#define	ARMV8_IDX_CYCLE_COUNTER	0
-#define	ARMV8_IDX_COUNTER0	1
-#define	ARMV8_IDX_COUNTER_LAST(cpu_pmu) \
-	(ARMV8_IDX_CYCLE_COUNTER + cpu_pmu->num_events - 1)
-
-/*
- * ARMv8 low level PMU access
- */
-
-/*
- * Perf Event to low level counters mapping
- */
-#define	ARMV8_IDX_TO_COUNTER(x)	\
-	(((x) - ARMV8_IDX_COUNTER0) & ARMV8_PMU_COUNTER_MASK)
-
 static inline u32 armv8pmu_pmcr_read(void)
 {
 	return read_sysreg(pmcr_el0);
drivers/perf/Makefile +1 -1
index b3902bd..a61afd9 100644
@@ -3,7 +3,7 @@ obj-$(CONFIG_ARM_CCI_PMU) += arm-cci.o
 obj-$(CONFIG_ARM_CCN) += arm-ccn.o
 obj-$(CONFIG_ARM_DSU_PMU) += arm_dsu_pmu.o
 obj-$(CONFIG_ARM_PMU) += arm_pmu.o arm_pmu_platform.o
-obj-$(CONFIG_ARM_PMU_ACPI) += arm_pmu_acpi.o
+obj-$(CONFIG_ARM_PMU_ACPI) += arm_pmu_acpi.o qcom_arm_pmu.o
 obj-$(CONFIG_HISI_PMU) += hisilicon/
 obj-$(CONFIG_QCOM_L2_PMU)	+= qcom_l2_pmu.o
 obj-$(CONFIG_QCOM_L3_PMU) += qcom_l3_pmu.o
drivers/perf/qcom_arm_pmu.c new file +398 -0

398 lines not on this page · raw message

include/linux/perf_event.h +3 -1
index 1fa1288..ae204d1 100644
@@ -677,8 +677,10 @@ struct perf_event {
 	u64				(*clock)(void);
 	perf_overflow_handler_t		overflow_handler;
 	void				*overflow_handler_context;
-#ifdef CONFIG_BPF_SYSCALL
+#if defined(CONFIG_BPF_SYSCALL) || defined(CONFIG_ARM_PMU_ACPI)
 	perf_overflow_handler_t		orig_overflow_handler;
+#endif
+#ifdef CONFIG_BPF_SYSCALL
 	struct bpf_prog			*prog;
 #endif
 
Signature · 2 lines
Qualcomm Datacenter Technologies, Inc. on behalf of the Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
#4 lore raw

RFC v4 4/4 perf: qcom: Add Falkor CPU PMU IMPLEMENTATION DEFINED event support

In reply to Agustin Vega-Frias

Selection of these events can be envisioned as indexing them from a 3D matrix:
- the first index selects a Region Event Selection Register (PMRESRx_EL0)
- the second index selects a group from which only one event at a time can be selected
- the third index selects the event

These events are encoded into perf_event_attr as:

  mbe      [config1:1   ]  (flag that indicates a matrix-based event)
  reg      [config:12-15]  (specifies the PMRESRx_EL0 instance)
  group    [config:0-3  ]  (specifies the event group)
  code     [config:4-11 ]  (specifies the event)

Events with the mbe flag set to zero are treated as common or raw PMUv3 events and are handled by the base PMUv3 driver code.

The first two indexes are set combining the RESR and group number with a base number and writing it into the architected PMXEVTYPER_EL0 register. The third index is set by writing the code into the bits corresponding with the group into the appropriate IMPLEMENTATION DEFINED PMRESRx_EL0 register.

Support for this extension is signaled by the presence of the Falkor PMU device node under each Falkor CPU device node in the DSDT ACPI table. E.g.:

    Device (CPU0)
    {
        Name (_HID, "ACPI0007" /* Processor Device */)
        ...
        Device (PMU0)
        {
            Name (_HID, "QCOM8150") /* Qualcomm Falkor PMU device */
            ...
        }
    }
---
 drivers/perf/qcom_arm_pmu.c | 306 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 287 insertions(+), 19 deletions(-)
drivers/perf/qcom_arm_pmu.c +287 -19
index 5893b4c..6dae1152 100644
@@ -22,6 +22,30 @@
  *   sampling when interrupts are disabled (the PMI is a maskable interrupt
  *   in arm64). Note that there is only one PC capture register so we only
  *   allow one event at a time to use it.
+ *
+ * - Matrix-based microarchitectural events support
+ *
+ *   Selection of these events can be envisioned as indexing them from
+ *   a 3D matrix:
+ *   - the first index selects a Region Event Selection Register (PMRESRx_EL0)
+ *   - the second index selects a group from which only one event at a time
+ *     can be selected
+ *   - the third index selects the event
+ *
+ *   These events are encoded into perf_event_attr as:
+ *     mbe      [config1:1   ]  (flag that indicates a matrix-based event)
+ *     reg      [config:12-15]  (specifies the PMRESRx_EL0 instance)
+ *     group    [config:0-3  ]  (specifies the event group)
+ *     code     [config:4-11 ]  (specifies the event)
+ *
+ *   Events with the mbe flag set to zero are treated as common or raw PMUv3
+ *   events and are handled by the base PMUv3 driver code.
+ *
+ *   The first two indexes are set combining the RESR and group number with a
+ *   base number and writing it into the architected PMXEVTYPER_EL0.evtCount.
+ *   The third index is set by writing the code into the bits corresponding
+ *   with the group into the appropriate IMPLEMENTATION DEFINED PMRESRx_EL0
+ *   register.
  */
 
 #include <linux/acpi.h>
@@ -61,8 +85,54 @@ struct pcc_ops {
 	void (*write_pmpccptcr0_el0)(u64 val);
 };
 
+/*
+ * Low-level MBE definitions
+ */
+
+#define pmresr0_el0         sys_reg(3, 5, 11, 3, 0)
+#define pmresr1_el0         sys_reg(3, 5, 11, 3, 2)
+#define pmresr2_el0         sys_reg(3, 5, 11, 3, 4)
+#define pmxevcntcr_el0      sys_reg(3, 5, 11, 0, 3)
+
+#define QC_EVT_MBE_SHIFT    1
+#define QC_EVT_REG_SHIFT    12
+#define QC_EVT_CODE_SHIFT   4
+#define QC_EVT_GRP_SHIFT    0
+#define QC_EVT_MBE_MASK     GENMASK(QC_EVT_MBE_SHIFT + 1,  QC_EVT_MBE_SHIFT)
+#define QC_EVT_REG_MASK     GENMASK(QC_EVT_REG_SHIFT + 3,  QC_EVT_REG_SHIFT)
+#define QC_EVT_CODE_MASK    GENMASK(QC_EVT_CODE_SHIFT + 7, QC_EVT_CODE_SHIFT)
+#define QC_EVT_GRP_MASK     GENMASK(QC_EVT_GRP_SHIFT + 3,  QC_EVT_GRP_SHIFT)
+#define QC_EVT_RG_MASK      (QC_EVT_REG_MASK | QC_EVT_GRP_MASK)
+#define QC_EVT_RG(event)    ((event)->attr.config & QC_EVT_RG_MASK)
+#define QC_EVT_MBE(event)						\
+	(((event)->attr.config1 & QC_EVT_MBE_MASK) >> QC_EVT_MBE_SHIFT)
+#define QC_EVT_REG(event)						\
+	(((event)->attr.config & QC_EVT_REG_MASK) >> QC_EVT_REG_SHIFT)
+#define QC_EVT_CODE(event)						\
+	(((event)->attr.config & QC_EVT_CODE_MASK) >> QC_EVT_CODE_SHIFT)
+#define QC_EVT_GROUP(event)						\
+	(((event)->attr.config & QC_EVT_GRP_MASK) >> QC_EVT_GRP_SHIFT)
+
+#define QC_MAX_GROUP        7
+#define QC_MAX_RESR         2
+#define QC_BITS_PER_GROUP   8
+#define QC_RESR_ENABLE      BIT_ULL(63)
+#define QC_RESR_EVT_BASE    0xd8
+
+struct mbe_ops {
+	/* Enable a MBE event */
+	void (*enable)(struct perf_event *event);
+	/* Enable a MBE event */
+	void (*disable)(struct perf_event *event);
+};
+
+/*
+ * Common state
+ */
+
 static struct arm_pmu *def_ops;
 static const struct pcc_ops *pcc_ops;
+static const struct mbe_ops *mbe_ops;
 
 /*
  * Low-level Falkor operations
@@ -92,12 +162,84 @@ static u64 falkor_read_pmpccptcr0_el0(void)
 	return read_sysreg_s(sys_reg(3, 5, 11, 4, 1));
 }
 
+static inline void falkor_write_pmresr(u64 reg, u64 val)
+{
+	switch (reg) {
+	case 0:
+		write_sysreg_s(val, pmresr0_el0);
+		return;
+	case 1:
+		write_sysreg_s(val, pmresr1_el0);
+		return;
+	default:
+		write_sysreg_s(val, pmresr2_el0);
+		return;
+	}
+}
+
+static inline u64 falkor_read_pmresr(u64 reg)
+{
+	switch (reg) {
+	case 0:
+		return read_sysreg_s(pmresr0_el0);
+	case 1:
+		return read_sysreg_s(pmresr1_el0);
+	default:
+		return read_sysreg_s(pmresr2_el0);
+	}
+}
+
+static void falkor_set_resr(u64 reg, u64 group, u64 code)
+{
+	u64 shift = group * QC_BITS_PER_GROUP;
+	u64 mask = GENMASK(shift + QC_BITS_PER_GROUP - 1, shift);
+	u64 val;
+
+	val = falkor_read_pmresr(reg) & ~mask;
+	val |= (code << shift);
+	val |= QC_RESR_ENABLE;
+	falkor_write_pmresr(reg, val);
+}
+
+static void falkor_clear_resr(u64 reg, u64 group)
+{
+	u32 shift = group * QC_BITS_PER_GROUP;
+	u64 mask = GENMASK(shift + QC_BITS_PER_GROUP - 1, shift);
+	u64 val = falkor_read_pmresr(reg) & ~mask;
+
+	falkor_write_pmresr(reg, val == QC_RESR_ENABLE ? 0 : val);
+}
+
+static void falkor_mbe_enable(struct perf_event *event)
+{
+	/* Program the appropriate PMRESRx_EL0 */
+	u64 reg = QC_EVT_REG(event);
+	u64 code = QC_EVT_CODE(event);
+	u64 group = QC_EVT_GROUP(event);
+
+	falkor_set_resr(reg, group, code);
+}
+
+static void falkor_mbe_disable(struct perf_event *event)
+{
+	/* De-program the appropriate PMRESRx_EL0 */
+	u64 reg = QC_EVT_REG(event);
+	u64 group = QC_EVT_GROUP(event);
+
+	falkor_clear_resr(reg, group);
+}
+
 static const struct pcc_ops falkor_pcc_ops = {
 	.read_pmpccptr_el0_pc = falkor_read_pmpccptr_el0_pc,
 	.read_pmpccptcr0_el0 = falkor_read_pmpccptcr0_el0,
 	.write_pmpccptcr0_el0 = falkor_write_pmpccptcr0_el0
 };
 
+static const struct mbe_ops falkor_mbe_ops = {
+	.enable = falkor_mbe_enable,
+	.disable = falkor_mbe_disable
+};
+
 /*
  * Low-level Saphira operations
  */
@@ -159,6 +301,18 @@ static bool has_pcc(struct perf_event *event)
 }
 
 /*
+ * Check if the given event uses MBE
+ */
+static bool has_mbe(struct perf_event *event)
+{
+	/* MBE not enabled */
+	if (!mbe_ops)
+		return false;
+
+	return QC_EVT_MBE(event);
+}
+
+/*
  * Check if the given event is for the raw or dynamic PMU type
  */
 static inline bool is_raw_or_dynamic(struct perf_event *event)
@@ -169,6 +323,56 @@ static inline bool is_raw_or_dynamic(struct perf_event *event)
 }
 
 /*
+ * Check if e1 and e2 have conflicting PCC settings
+ */
+static inline bool pcc_conflict(struct perf_event *e1, struct perf_event *e2)
+{
+	bool pcc1 = has_pcc(e1), pcc2= has_pcc(e2);
+
+	/* No conflict if none of the events is using PCC */
+	if (!pcc1 && !pcc2)
+		return false;
+
+	/* No conflict if one of the events is not using PCC */
+	if (pcc1 != pcc2)
+		return false;
+
+	pr_warn_ratelimited("PCC exclusion: conflicting events %llx %llx\n",
+			     e1->attr.config,
+			     e2->attr.config);
+	return true;
+}
+
+/*
+ * Check if e1 and e2 have conflicting MBE settings
+ */
+static inline bool mbe_conflict(struct perf_event *e1, struct perf_event *e2)
+{
+	bool mbe1 = has_mbe(e1), mbe2= has_mbe(e2);
+
+	/* No conflict if none of the events is using MBE */
+	if (!mbe1 && !mbe2)
+		return false;
+
+	/* No conflict if one of the events is not using MBE */
+	if (mbe1 != mbe2)
+		return false;
+
+	/* No conflict if using different reg or group */
+	if (QC_EVT_RG(e1) != QC_EVT_RG(e2))
+		return false;
+
+	/* Same mbe, reg and group is fine so long as code matches */
+	if (QC_EVT_CODE(e1) == QC_EVT_CODE(e2))
+		return false;
+
+	pr_warn_ratelimited("Group exclusion: conflicting events %llx %llx\n",
+			     e1->attr.config,
+			     e2->attr.config);
+	return true;
+}
+
+/*
  * Check if e1 and e2 conflict with each other
  *
  * e1 is an event that has extensions and we are checking against e2.
@@ -186,15 +390,7 @@ static inline bool events_conflict(struct perf_event *e1, struct perf_event *e2)
 	if ((e1->pmu != e2->pmu) && (type != PERF_TYPE_RAW) && (type != dynamic))
 		return false;
 
-	/* No conflict if using different pcc or if pcc is not enabled */
-	if (pcc_ops && is_sampling_event(e2) && (QC_EVT_PCC(e1) == QC_EVT_PCC(e2))) {
-		pr_debug_ratelimited("PCC exclusion: conflicting events %llx %llx\n",
-				     e1->attr.config,
-				     e2->attr.config);
-		return true;
-	}
-
-	return false;
+	return pcc_conflict(e1, e2) || mbe_conflict(e1, e2);;
 }
 
 /*
@@ -225,7 +421,10 @@ static void pcc_overflow_handler(struct perf_event *event,
  */
 static int qcom_arm_pmu_map_event(struct perf_event *event)
 {
-	if (is_raw_or_dynamic(event) && has_pcc(event)) {
+	if (!is_raw_or_dynamic(event))
+		goto done;
+
+	if (has_pcc(event) || has_mbe(event)) {
 		struct perf_event *leader;
 		struct perf_event *sibling;
 
@@ -239,6 +438,16 @@ static int qcom_arm_pmu_map_event(struct perf_event *event)
 				return -ENOENT;
 	}
 
+	if (has_mbe(event)) {
+		u64 reg = QC_EVT_REG(event);
+		u64 group = QC_EVT_GROUP(event);
+
+		if ((group > QC_MAX_GROUP) || (reg > QC_MAX_RESR))
+			return -ENOENT;
+		return QC_RESR_EVT_BASE + reg * 8 + group;
+	}
+
+done:
 	return def_ops->map_event(event);
 }
 
@@ -249,15 +458,20 @@ static int qcom_arm_pmu_get_event_idx(struct pmu_hw_events *cpuc, struct perf_ev
 {
 	int idx;
 
-	if (is_raw_or_dynamic(event) && has_pcc(event)) {
-		struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
-		int idx;
+	if (!is_raw_or_dynamic(event))
+		goto done;
 
+	if (has_pcc(event) || has_mbe(event)) {
 		/* Check for conflicts with existing events */
 		for_each_set_bit(idx, cpuc->used_mask, ARMPMU_MAX_HWEVENTS)
 			if (cpuc->events[idx] &&
 			    events_conflict(event, cpuc->events[idx]))
 				return -ENOENT;
+	}
+
+	if (has_pcc(event)) {
+		struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
+		int idx;
 
 		/*
 		 * PCC is requested for this event so we need to use an event
@@ -273,6 +487,7 @@ static int qcom_arm_pmu_get_event_idx(struct pmu_hw_events *cpuc, struct perf_ev
 		return -EAGAIN;
 	}
 
+done:
 	/* Let the original op handle the rest */
 	idx = def_ops->get_event_idx(cpuc, event);
 
@@ -303,6 +518,9 @@ static void qcom_arm_pmu_enable(struct perf_event *event)
 		WRITE_ONCE(event->overflow_handler, pcc_overflow_handler);
 	}
 
+	if (has_mbe(event))
+		mbe_ops->enable(event);
+
 	/* Let the original op handle the rest */
 	def_ops->enable(event);
 }
@@ -325,20 +543,59 @@ static void qcom_arm_pmu_disable(struct perf_event *event)
 		if (event->orig_overflow_handler)
 			WRITE_ONCE(event->overflow_handler, event->orig_overflow_handler);
 	}
+
+	if (has_mbe(event))
+		mbe_ops->disable(event);
+}
+
+/*
+ * Reset the PMU
+ */
+static void qcom_arm_pmu_falkor_reset(void *info)
+{
+	struct arm_pmu *pmu = (struct arm_pmu *)info;
+	u32 i, ctrs = pmu->num_events;
+
+	/* PMRESRx_EL0 regs are unknown at reset, except for the EN field */
+	for (i = 0; i <= QC_MAX_RESR; i++)
+		falkor_write_pmresr(i, 0);
+
+	/* PMXEVCNTCRx_EL0 regs are unknown at reset */
+	for (i = 0; i <= ctrs; i++) {
+		write_sysreg(i, pmselr_el0);
+		isb();
+		write_sysreg_s(0, pmxevcntcr_el0);
+	}
+
+	/* Let the original op handle the rest */
+	def_ops->reset(info);
 }
 
 PMU_FORMAT_ATTR(event, "config:0-15");
 PMU_FORMAT_ATTR(pcc,   "config1:0");
+PMU_FORMAT_ATTR(mbe,   "config1:1");
+PMU_FORMAT_ATTR(reg,   "config:12-15");
+PMU_FORMAT_ATTR(code,  "config:4-11");
+PMU_FORMAT_ATTR(group, "config:0-3");
 
-static struct attribute *pmu_formats[] = {
+static struct attribute *falkor_pmu_formats[] = {
+	&format_attr_pcc.attr,
 	&format_attr_event.attr,
+	&format_attr_mbe.attr,
+	&format_attr_reg.attr,
+	&format_attr_code.attr,
+	&format_attr_group.attr,
+	NULL,
+};
+
+static struct attribute *saphira_pmu_formats[] = {
 	&format_attr_pcc.attr,
+	&format_attr_event.attr,
 	NULL,
 };
 
 static struct attribute_group pmu_format_attr_group = {
 	.name = "format",
-	.attrs = pmu_formats,
 };
 
 static inline bool pcc_supported(struct device *dev)
@@ -376,12 +633,21 @@ static int qcom_pmu_init(struct arm_pmu *pmu, struct device *dev)
 
 static int qcom_falkor_pmu_init(struct arm_pmu *pmu, struct device *dev)
 {
-	if (pcc_supported(dev))
+	int result;
+
+	if (pcc_supported(dev)) {
+		pmu_format_attr_group.attrs = falkor_pmu_formats;
 		pcc_ops = &falkor_pcc_ops;
-	else
-		return -ENODEV;
+	} else {
+		pmu_format_attr_group.attrs = &falkor_pmu_formats[1];
+	}
 
-	return qcom_pmu_init(pmu, dev);
+	mbe_ops = &falkor_mbe_ops;
+
+	result = qcom_pmu_init(pmu, dev);
+	pmu->reset = qcom_arm_pmu_falkor_reset;
+
+	return result;
 }
 
 static int qcom_saphira_pmu_init(struct arm_pmu *pmu, struct device *dev)
@@ -391,6 +657,8 @@ static int qcom_saphira_pmu_init(struct arm_pmu *pmu, struct device *dev)
 	else
 		return -ENODEV;
 
+	pmu_format_attr_group.attrs = saphira_pmu_formats;
+
 	return qcom_pmu_init(pmu, dev);
 }
 
Signature · 2 lines
Qualcomm Datacenter Technologies, Inc. on behalf of the Qualcomm Technologies, Inc.
Qualcomm Technologies, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project.
#5 lore raw
In reply to Agustin Vega-Frias

Hi Agustin,

Quoted 16 lines · Agustin Vega-Frias

This series is a complete re-design of V1 of the QCOM Falkor extensions [1], it introduces a probe table based on the HID of a device nested under the CPU device to allow variant detection and arm_pmu customization.

The first patch adds an additional section at the end of each ACPI probe table. This allows probe tables to be sentinel-delimited and better accommodate some APIs that require such tables.

The second patch adds the PMUv3 ACPI probe table and plumbing to allow drivers to plug into the ACPI PMUv3 probe sequence.

The third patch adds the PC capture extension applicable to Falkor and Saphira CPUs. This shows how an extension that uses sampling events hooks. A similar approach can be used to add RBB support and populate the sample branch stack from it.

Quoted 5 lines

The fourth patch adds the matrix-based events extension applicable to Falkor only.

If this found to be a reasonable extension approach other patches will be added to the series to build on the base QCOM extensions.

I'm mostly ok with this approach, but I have a concern with the way in which the sysfs interface for carving up the config fields is implemented. If this is intended to be a strict extension to the armv8 pmu architecture, then I don't think you should be overriding the attr_groups entirely. Rather, you should be taking the attr_groups from pmuv3 and then extending them in a way which avoids overlapping field allocations by construction.

As it stands, you already have an overlap between the pcc bit and the chained counter bit which Suzuki has implemented and it will be very easy to introduce API breakage if we don't enforce this as part of the design.

Will

#6 lore raw

Hi Will,

On Fri, Jul 13, 2018 at 10:33 AM, Will Deacon <[email protected]> wrote:

[snip]

Quoted 7 lines

I'm mostly ok with this approach, but I have a concern with the way in which the sysfs interface for carving up the config fields is implemented. If this is intended to be a strict extension to the armv8 pmu architecture, then I don't think you should be overriding the attr_groups entirely. Rather, you should be taking the attr_groups from pmuv3 and then extending them in a way which avoids overlapping field allocations by construction.

Quoted 5 lines

As it stands, you already have an overlap between the pcc bit and the chained counter bit which Suzuki has implemented and it will be very easy to introduce API breakage if we don't enforce this as part of the design.

Will

FYI, I left Qualcomm on July 6, one of my former colleagues will submit new iterations of this series. I will continue to comment on this and future patchsets as a courtesy to my former colleagues and the community.

Thanks for pointing out the sysfs issue. My suggestion on how to address it is:

1. Reserve config and config1 for architectural format attributes and config2 for extension format attributes.
2. Add a struct attribute ** parameter to the extension init function so extensions can return the new attributes.
3. The extension framework code in arm_pmu_acpi.c can then allocate a new attribute array to contain the base and extension attributes and ensure all the new attributes are on config2.

Though a more elaborate approach can be implemented to find conflicts in bit usage within config fields, it would require much more code for a relatively simple problem. Thoughts?

Thanks,
Agustín

#7 lore raw
Quoted 28 lines · J. Agustín Vega-Frías
Quoted 7 lines · Will Deacon

I'm mostly ok with this approach, but I have a concern with the way in which the sysfs interface for carving up the config fields is implemented. If this is intended to be a strict extension to the armv8 pmu architecture, then I don't think you should be overriding the attr_groups entirely. Rather, you should be taking the attr_groups from pmuv3 and then extending them in a way which avoids overlapping field allocations by construction.

Quoted 5 lines

As it stands, you already have an overlap between the pcc bit and the chained counter bit which Suzuki has implemented and it will be very easy to introduce API breakage if we don't enforce this as part of the design.

Will

FYI, I left Qualcomm on July 6, one of my former colleagues will submit new iterations of this series. I will continue to comment on this and future patchsets as a courtesy to my former colleagues and the community.

Thanks for pointing out the sysfs issue. My suggestion on how to address it is:

1. Reserve config and config1 for architectural format attributes and config2 for extension format attributes.
2. Add a struct attribute ** parameter to the extension init function so extensions can return the new attributes.
3. The extension framework code in arm_pmu_acpi.c can then allocate a new attribute array to contain the base and extension attributes and ensure all the new attributes are on config2.

Quoted 3 lines

Though a more elaborate approach can be implemented to find conflicts in bit usage within config fields, it would require much more code for a relatively simple problem. Thoughts?

As long as you're happy to live inside config2, that sounds sensible to me.

Will