[PATCH 3/8] perf/arm_pmu: Register ACPI SPE and TRBE devices with software nodes

Leo Yan leo.yan at arm.com
Mon Nov 16 10:21:30 PST 2026


From: "Johnny.Fan" <johnny.fan at cixtech.com>

The MADT describes SPE and TRBE interrupts per CPU, without providing an
ACPI namespace device for each group of compatible CPUs. The existing
registration only supports a homogeneous system.

Group initialized CPUs by MIDR and register one platform device per
group through a common descriptor-based entry point. Keep each CPU's GSI
and CPU number in a mapping record until IRQ resource setup. Deduplicate
shared GSIs and use arm_acpi_pmu_irq_matches() to require the same
mapped IRQ within each group. Warn and exclude CPUs with inconsistent
PPIs.

Associate a software node containing the group's IRQ affinity so drivers
can obtain the CPU mask through platform_get_irq_affinity() without
ACPI-specific handling or a processor-container companion. Create IRQ
resources first, then use arm_acpi_register_sw_node_irq_affinity() to
store the filtered group's PPI affinity at index zero.

Although SPE and TRBE need only one shared PPI per group, introduce
nr_irqs and the irq_maps[] array now to simplify adding CPU PMU support
with per-CPU SPIs in a later patch.

Signed-off-by: Johnny.Fan <johnny.fan at cixtech.com>
Co-developed-by: Leo Yan <leo.yan at arm.com>
Signed-off-by: Leo Yan <leo.yan at arm.com>
---
 drivers/perf/arm_pmu_acpi.c | 350 ++++++++++++++++++++++++++++++++++----------
 1 file changed, 274 insertions(+), 76 deletions(-)

diff --git a/drivers/perf/arm_pmu_acpi.c b/drivers/perf/arm_pmu_acpi.c
index ca6bf8f86cfe9be0dc351e41bd620d1d37028dfa..7ae291acb04526d820bdfb9640aaa8352449be54 100644
--- a/drivers/perf/arm_pmu_acpi.c
+++ b/drivers/perf/arm_pmu_acpi.c
@@ -7,11 +7,15 @@
 
 #include <linux/acpi.h>
 #include <linux/cpumask.h>
+#include <linux/err.h>
 #include <linux/init.h>
 #include <linux/irq.h>
 #include <linux/irqdesc.h>
 #include <linux/percpu.h>
 #include <linux/perf/arm_pmu.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/slab.h>
 
 #include <asm/cpu.h>
 #include <asm/cputype.h>
@@ -68,92 +72,297 @@ static void arm_pmu_acpi_unregister_irq(int cpu)
 		acpi_unregister_gsi(gsi);
 }
 
-static int __maybe_unused
-arm_acpi_register_pmu_device(struct platform_device *pdev, u8 len,
-			     u16 (*parse_gsi)(struct acpi_madt_generic_interrupt *))
+struct arm_pmu_acpi_device_info {
+	const char *name;
+	u8 len;
+	u32 (*parse_gsi)(struct acpi_madt_generic_interrupt *gicc);
+};
+
+struct arm_pmu_acpi_irq_map {
+	u32 gsi;
+	int cpu;
+	int irq;	/* Nonzero only for mappings created by this group */
+};
+
+struct arm_pmu_acpi_group {
+	struct cpumask cpus;
+	int nr_irqs;
+	struct arm_pmu_acpi_irq_map irq_maps[];
+};
+
+static struct arm_pmu_acpi_group *
+arm_acpi_alloc_pmu_group(struct arm_pmu_acpi_group **groups, int *nr_groups,
+			 int cpu)
 {
-	int cpu, this_hetid, hetid, irq, ret;
-	u16 this_gsi = 0, gsi = 0;
+	struct arm_pmu_acpi_group *group;
+	unsigned long midr;
+	int i;
 
-	/*
-	 * Ensure that platform device must have IORESOURCE_IRQ
-	 * resource to hold gsi interrupt.
-	 */
-	if (pdev->num_resources != 1)
-		return -ENXIO;
+	midr = per_cpu(cpu_data, cpu).reg_midr;
 
-	if (pdev->resource[0].flags != IORESOURCE_IRQ)
-		return -ENXIO;
+	for (i = 0; i < *nr_groups; i++) {
+		int other;
+
+		group = groups[i];
+		other = cpumask_first(&group->cpus);
+		if (midr != per_cpu(cpu_data, other).reg_midr)
+			continue;
+
+		return group;
+	}
+
+	group = kzalloc_flex(*group, irq_maps, nr_cpu_ids, GFP_KERNEL);
+	if (!group)
+		return ERR_PTR(-ENOMEM);
+	groups[(*nr_groups)++] = group;
+
+	return group;
+}
+
+static void arm_acpi_free_pmu_groups(struct arm_pmu_acpi_group **groups,
+				     int nr_groups)
+{
+	int i;
+
+	for (i = 0; i < nr_groups; i++)
+		kfree(groups[i]);
+
+	kfree(groups);
+}
+
+static struct arm_pmu_acpi_group **
+arm_acpi_alloc_pmu_groups(const struct arm_pmu_acpi_device_info *info,
+			 int *nr_groups_out)
+{
+	struct arm_pmu_acpi_group **groups;
+	int cpu, nr_groups = 0;
+
+	*nr_groups_out = 0;
+
+	groups = kcalloc(num_possible_cpus(), sizeof(*groups), GFP_KERNEL);
+	if (!groups)
+		return NULL;
 
-	/*
-	 * Sanity check all the GICC tables for the same interrupt
-	 * number. For now, only support homogeneous ACPI machines.
-	 */
 	for_each_possible_cpu(cpu) {
 		struct acpi_madt_generic_interrupt *gicc;
+		struct arm_pmu_acpi_group *group;
+		u32 gsi;
+
+		/* CPUs that have never booted have no saved MIDR */
+		if (!per_cpu(cpu_data, cpu).reg_midr)
+			continue;
 
 		gicc = acpi_cpu_get_madt_gicc(cpu);
-		if (gicc->header.length < len)
-			return gsi ? -ENXIO : 0;
-
-		this_gsi = parse_gsi(gicc);
-		this_hetid = find_acpi_cpu_topology_hetero_id(cpu);
-		if (!gsi) {
-			hetid = this_hetid;
-			gsi = this_gsi;
-		} else if (hetid != this_hetid || gsi != this_gsi) {
-			pr_warn("ACPI: %s: must be homogeneous\n", pdev->name);
-			return -ENXIO;
+		if (gicc->header.length < info->len) {
+			pr_warn("ACPI: %s: miss GICC entries\n", info->name);
+			goto out_free;
 		}
+		gsi = info->parse_gsi(gicc);
+		if (!gsi)
+			continue;
+
+		group = arm_acpi_alloc_pmu_group(groups, &nr_groups, cpu);
+		if (IS_ERR(group))
+			goto out_free;
+
+		if (gsi) {
+			struct arm_pmu_acpi_irq_map *map =
+				&group->irq_maps[group->nr_irqs++];
+
+			map->gsi = gsi;
+			map->cpu = cpu;
+		}
+		cpumask_set_cpu(cpu, &group->cpus);
 	}
 
-	if (!this_gsi)
+	*nr_groups_out = nr_groups;
+	return groups;
+
+out_free:
+	arm_acpi_free_pmu_groups(groups, nr_groups);
+	return NULL;
+}
+
+static void arm_acpi_unregister_irq_resource(struct arm_pmu_acpi_group *group)
+{
+	int i;
+
+	/* Leave mappings reused from another group or device intact */
+	for (i = 0; i < group->nr_irqs; i++) {
+		struct arm_pmu_acpi_irq_map *map = &group->irq_maps[i];
+
+		if (!map->irq)
+			continue;
+
+		acpi_unregister_gsi(map->gsi);
+		map->irq = 0;
+	}
+}
+
+static bool arm_acpi_pmu_irq_matches(unsigned int irq, unsigned int other_irq)
+{
+	return irq == other_irq;
+}
+
+static int arm_acpi_map_gsi_irq(u32 gsi, int *irq)
+{
+	unsigned int existing_irq;
+
+	if (!acpi_gsi_to_irq(gsi, &existing_irq)) {
+		*irq = existing_irq;
+		return 0;
+	}
+
+	/* PMU overflow is level-sensitive; ignore the MADT edge flag */
+	*irq = acpi_register_gsi(NULL, gsi, ACPI_LEVEL_SENSITIVE, ACPI_ACTIVE_HIGH);
+	return *irq;
+}
+
+static int arm_acpi_register_irq_resource(struct platform_device *pdev,
+					  struct arm_pmu_acpi_group *group)
+{
+	struct resource *res;
+	int i, j, nr_irqs = group->nr_irqs, ret;
+
+	if (!nr_irqs)
 		return 0;
 
-	irq = acpi_register_gsi(NULL, gsi, ACPI_LEVEL_SENSITIVE, ACPI_ACTIVE_HIGH);
-	if (irq < 0) {
-		pr_warn("ACPI: %s Unable to register interrupt: %d\n", pdev->name, gsi);
-		return -ENXIO;
+	res = kcalloc(nr_irqs, sizeof(*res), GFP_KERNEL);
+	if (!res)
+		return -ENOMEM;
+
+	group->nr_irqs = 0;
+	for (i = 0; i < nr_irqs; i++) {
+		struct arm_pmu_acpi_irq_map map = group->irq_maps[i];
+		int irq;
+
+		/* Shared GSIs need one resource */
+		for (j = 0; j < group->nr_irqs; j++) {
+			if (group->irq_maps[j].gsi == map.gsi)
+				break;
+		}
+		if (j < group->nr_irqs)
+			continue;
+
+		/*
+		 * On success, irq contains the mapped IRQ, whether new or
+		 * existing. The return value is positive for a new mapping
+		 * and zero for reuse.
+		 */
+		ret = arm_acpi_map_gsi_irq(map.gsi, &irq);
+		if (ret < 0)
+			goto out_free;
+
+		/* A group must use the same PPI */
+		if (group->nr_irqs && !arm_acpi_pmu_irq_matches(irq, res[0].start)) {
+			pr_warn("ACPI: %s: CPU%d has inconsistent PPI in group, ignoring CPU\n",
+				pdev->name, map.cpu);
+			cpumask_clear_cpu(map.cpu, &group->cpus);
+			/* Leave reused mappings intact */
+			if (ret > 0)
+				acpi_unregister_gsi(map.gsi);
+			continue;
+		}
+
+		map.irq = ret;
+		group->irq_maps[group->nr_irqs] = map;
+		res[group->nr_irqs++] = DEFINE_RES_IRQ(irq);
 	}
 
-	pdev->resource[0].start = irq;
-	ret = platform_device_register(pdev);
+	ret = platform_device_add_resources(pdev, res, group->nr_irqs);
+
+out_free:
+	kfree(res);
+	return ret;
+}
+
+static int arm_acpi_register_sw_node_irq_affinity(struct platform_device *pdev,
+						 struct arm_pmu_acpi_group *group)
+{
+	int ret;
+
+	/* A MIDR group may span several processor containers */
+	ret = device_create_managed_software_node(&pdev->dev, NULL, NULL);
 	if (ret)
-		acpi_unregister_gsi(gsi);
+		return ret;
+
+	return software_node_add_irq_affinity(dev_fwnode(&pdev->dev), 0,
+					      &group->cpus);
+}
+
+static int arm_acpi_register_pmu_device(struct arm_pmu_acpi_group *group,
+					const char *name, int id)
+{
+	struct platform_device *pdev;
+	int ret;
+
+	pdev = platform_device_alloc(name, id);
+	if (!pdev)
+		return -ENOMEM;
+
+	ret = arm_acpi_register_irq_resource(pdev, group);
+	if (ret)
+		goto out_put;
+
+	ret = arm_acpi_register_sw_node_irq_affinity(pdev, group);
+	if (ret)
+		goto out_put;
 
+	ret = platform_device_add(pdev);
+	if (ret)
+		goto out_put;
+
+	return 0;
+
+out_put:
+	platform_device_put(pdev);
+	arm_acpi_unregister_irq_resource(group);
 	return ret;
 }
 
-#if IS_ENABLED(CONFIG_ARM_SPE_PMU)
-static struct resource spe_resources[] = {
-	{
-		/* irq */
-		.flags          = IORESOURCE_IRQ,
+static int __maybe_unused
+arm_acpi_register_pmu_devices(const struct arm_pmu_acpi_device_info *info)
+{
+	struct arm_pmu_acpi_group **groups;
+	int i, nr_groups, nr_devices = 0, last_err = 0, ret;
+
+	groups = arm_acpi_alloc_pmu_groups(info, &nr_groups);
+	if (!groups)
+		return -ENOMEM;
+
+	for (i = 0; i < nr_groups; i++) {
+		if (!groups[i])
+			continue;
+
+		ret = arm_acpi_register_pmu_device(groups[i], info->name, i);
+		if (ret) {
+			pr_warn("ACPI: %s: %*pbl: add device failed (%d)\n",
+				info->name, cpumask_pr_args(&groups[i]->cpus), ret);
+			last_err = ret;
+		} else {
+			nr_devices++;
+		}
 	}
-};
 
-static struct platform_device spe_dev = {
-	.name = ARMV8_SPE_PDEV_NAME,
-	.id = -1,
-	.resource = spe_resources,
-	.num_resources = ARRAY_SIZE(spe_resources)
-};
+	arm_acpi_free_pmu_groups(groups, nr_groups);
+	return nr_devices ? 0 : last_err;
+}
 
-static u16 arm_spe_parse_gsi(struct acpi_madt_generic_interrupt *gicc)
+#if IS_ENABLED(CONFIG_ARM_SPE_PMU)
+static u32 arm_spe_parse_gsi(struct acpi_madt_generic_interrupt *gicc)
 {
 	return gicc->spe_interrupt;
 }
 
-/*
- * For lack of a better place, hook the normal PMU MADT walk
- * and create a SPE device if we detect a recent MADT with
- * a homogeneous PPI mapping.
- */
 static void arm_spe_acpi_register_device(void)
 {
-	int ret = arm_acpi_register_pmu_device(&spe_dev, ACPI_MADT_GICC_SPE,
-					       arm_spe_parse_gsi);
-	if (ret)
+	static const struct arm_pmu_acpi_device_info info = {
+		.name = ARMV8_SPE_PDEV_NAME,
+		.len = ACPI_MADT_GICC_SPE,
+		.parse_gsi = arm_spe_parse_gsi,
+	};
+
+	if (arm_acpi_register_pmu_devices(&info))
 		pr_warn("ACPI: SPE: Unable to register device\n");
 }
 #else
@@ -163,36 +372,25 @@ static inline void arm_spe_acpi_register_device(void)
 #endif /* CONFIG_ARM_SPE_PMU */
 
 #if IS_ENABLED(CONFIG_CORESIGHT_TRBE)
-static struct resource trbe_resources[] = {
-	{
-		/* irq */
-		.flags          = IORESOURCE_IRQ,
-	}
-};
-
-static struct platform_device trbe_dev = {
-	.name = ARMV8_TRBE_PDEV_NAME,
-	.id = -1,
-	.resource = trbe_resources,
-	.num_resources = ARRAY_SIZE(trbe_resources)
-};
-
-static u16 arm_trbe_parse_gsi(struct acpi_madt_generic_interrupt *gicc)
+static u32 arm_trbe_parse_gsi(struct acpi_madt_generic_interrupt *gicc)
 {
 	return gicc->trbe_interrupt;
 }
 
 static void arm_trbe_acpi_register_device(void)
 {
-	int ret = arm_acpi_register_pmu_device(&trbe_dev, ACPI_MADT_GICC_TRBE,
-					       arm_trbe_parse_gsi);
-	if (ret)
+	static const struct arm_pmu_acpi_device_info info = {
+		.name = ARMV8_TRBE_PDEV_NAME,
+		.len = ACPI_MADT_GICC_TRBE,
+		.parse_gsi = arm_trbe_parse_gsi,
+	};
+
+	if (arm_acpi_register_pmu_devices(&info))
 		pr_warn("ACPI: TRBE: Unable to register device\n");
 }
 #else
 static inline void arm_trbe_acpi_register_device(void)
 {
-
 }
 #endif /* CONFIG_CORESIGHT_TRBE */
 

-- 
2.34.1




More information about the linux-arm-kernel mailing list