Skip to content

Commit 7ed98e0

Browse files
mrutland-armwildea01
authored andcommitted
drivers/perf: arm_pmu: manage interrupts per-cpu
When requesting or freeing interrupts, we use platform_get_irq() to find relevant irqs, backing this up with additional information in an optional irq_affinity table. This means that our irq request and free paths are tied to a platform_device, and our request path must jump through a number of hoops in order to determine the required affinity of each interrupt. Given that the affinity must be static, we can compute the affinity once up-front at probe time, simplifying the irq request and free paths. By recording interrupts in a per-cpu data structure, we simplify a few paths, and permit a subsequent rework of the request and free paths. Signed-off-by: Mark Rutland <mark.rutland@arm.com> [will: rename local nr_irqs variable to avoid conflict with global] Signed-off-by: Will Deacon <will.deacon@arm.com>
1 parent 2681f01 commit 7ed98e0

2 files changed

Lines changed: 166 additions & 151 deletions

File tree

‎drivers/perf/arm_pmu.c‎

Lines changed: 164 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -617,94 +617,76 @@ static void cpu_pmu_disable_percpu_irq(void *data)
617617

618618
static void cpu_pmu_free_irq(struct arm_pmu *cpu_pmu)
619619
{
620-
int i, irq, irqs;
621-
struct platform_device *pmu_device = cpu_pmu->plat_device;
620+
int cpu;
622621
struct pmu_hw_events __percpu *hw_events = cpu_pmu->hw_events;
623622

624-
irqs = min(pmu_device->num_resources, num_possible_cpus());
625-
626-
irq = platform_get_irq(pmu_device, 0);
627-
if (irq > 0 && irq_is_percpu(irq)) {
628-
on_each_cpu_mask(&cpu_pmu->supported_cpus,
629-
cpu_pmu_disable_percpu_irq, &irq, 1);
630-
free_percpu_irq(irq, &hw_events->percpu_pmu);
631-
} else {
632-
for (i = 0; i < irqs; ++i) {
633-
int cpu = i;
634-
635-
if (cpu_pmu->irq_affinity)
636-
cpu = cpu_pmu->irq_affinity[i];
637-
638-
if (!cpumask_test_and_clear_cpu(cpu, &cpu_pmu->active_irqs))
639-
continue;
640-
irq = platform_get_irq(pmu_device, i);
641-
if (irq > 0)
642-
free_irq(irq, per_cpu_ptr(&hw_events->percpu_pmu, cpu));
623+
for_each_cpu(cpu, &cpu_pmu->supported_cpus) {
624+
int irq = per_cpu(hw_events->irq, cpu);
625+
if (!irq)
626+
continue;
627+
628+
if (irq_is_percpu(irq)) {
629+
on_each_cpu_mask(&cpu_pmu->supported_cpus,
630+
cpu_pmu_disable_percpu_irq, &irq, 1);
631+
free_percpu_irq(irq, &hw_events->percpu_pmu);
632+
633+
break;
643634
}
635+
636+
if (!cpumask_test_and_clear_cpu(cpu, &cpu_pmu->active_irqs))
637+
continue;
638+
639+
free_irq(irq, per_cpu_ptr(&hw_events->percpu_pmu, cpu));
644640
}
645641
}
646642

647643
static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
648644
{
649-
int i, err, irq, irqs;
650-
struct platform_device *pmu_device = cpu_pmu->plat_device;
645+
int cpu, err;
651646
struct pmu_hw_events __percpu *hw_events = cpu_pmu->hw_events;
652647

653-
if (!pmu_device)
654-
return -ENODEV;
655-
656-
irqs = min(pmu_device->num_resources, num_possible_cpus());
657-
if (irqs < 1) {
658-
pr_warn_once("perf/ARM: No irqs for PMU defined, sampling events not supported\n");
659-
return 0;
660-
}
661-
662-
irq = platform_get_irq(pmu_device, 0);
663-
if (irq > 0 && irq_is_percpu(irq)) {
664-
err = request_percpu_irq(irq, handler, "arm-pmu",
665-
&hw_events->percpu_pmu);
666-
if (err) {
667-
pr_err("unable to request IRQ%d for ARM PMU counters\n",
668-
irq);
669-
return err;
670-
}
671-
672-
on_each_cpu_mask(&cpu_pmu->supported_cpus,
673-
cpu_pmu_enable_percpu_irq, &irq, 1);
674-
} else {
675-
for (i = 0; i < irqs; ++i) {
676-
int cpu = i;
677-
678-
err = 0;
679-
irq = platform_get_irq(pmu_device, i);
680-
if (irq < 0)
681-
continue;
682-
683-
if (cpu_pmu->irq_affinity)
684-
cpu = cpu_pmu->irq_affinity[i];
685-
686-
/*
687-
* If we have a single PMU interrupt that we can't shift,
688-
* assume that we're running on a uniprocessor machine and
689-
* continue. Otherwise, continue without this interrupt.
690-
*/
691-
if (irq_set_affinity(irq, cpumask_of(cpu)) && irqs > 1) {
692-
pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
693-
irq, cpu);
694-
continue;
695-
}
648+
for_each_cpu(cpu, &cpu_pmu->supported_cpus) {
649+
int irq = per_cpu(hw_events->irq, cpu);
650+
if (!irq)
651+
continue;
696652

697-
err = request_irq(irq, handler,
698-
IRQF_NOBALANCING | IRQF_NO_THREAD, "arm-pmu",
699-
per_cpu_ptr(&hw_events->percpu_pmu, cpu));
653+
if (irq_is_percpu(irq)) {
654+
err = request_percpu_irq(irq, handler, "arm-pmu",
655+
&hw_events->percpu_pmu);
700656
if (err) {
701657
pr_err("unable to request IRQ%d for ARM PMU counters\n",
702658
irq);
703659
return err;
704660
}
705661

706-
cpumask_set_cpu(cpu, &cpu_pmu->active_irqs);
662+
on_each_cpu_mask(&cpu_pmu->supported_cpus,
663+
cpu_pmu_enable_percpu_irq, &irq, 1);
664+
665+
break;
707666
}
667+
668+
/*
669+
* If we have a single PMU interrupt that we can't shift,
670+
* assume that we're running on a uniprocessor machine and
671+
* continue. Otherwise, continue without this interrupt.
672+
*/
673+
if (irq_set_affinity(irq, cpumask_of(cpu)) &&
674+
num_possible_cpus() > 1) {
675+
pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
676+
irq, cpu);
677+
continue;
678+
}
679+
680+
err = request_irq(irq, handler,
681+
IRQF_NOBALANCING | IRQF_NO_THREAD, "arm-pmu",
682+
per_cpu_ptr(&hw_events->percpu_pmu, cpu));
683+
if (err) {
684+
pr_err("unable to request IRQ%d for ARM PMU counters\n",
685+
irq);
686+
return err;
687+
}
688+
689+
cpumask_set_cpu(cpu, &cpu_pmu->active_irqs);
708690
}
709691

710692
return 0;
@@ -846,10 +828,6 @@ static int cpu_pmu_init(struct arm_pmu *cpu_pmu)
846828
on_each_cpu_mask(&cpu_pmu->supported_cpus, cpu_pmu->reset,
847829
cpu_pmu, 1);
848830

849-
/* If no interrupts available, set the corresponding capability flag */
850-
if (!platform_get_irq(cpu_pmu->plat_device, 0))
851-
cpu_pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
852-
853831
/*
854832
* This is a CPU PMU potentially in a heterogeneous configuration (e.g.
855833
* big.LITTLE). This is not an uncore PMU, and we have taken ctx
@@ -897,98 +875,133 @@ static int probe_current_pmu(struct arm_pmu *pmu,
897875
return ret;
898876
}
899877

900-
static int of_pmu_irq_cfg(struct arm_pmu *pmu)
878+
static int pmu_parse_percpu_irq(struct arm_pmu *pmu, int irq)
901879
{
902-
int *irqs, i = 0;
903-
bool using_spi = false;
904-
struct platform_device *pdev = pmu->plat_device;
880+
int cpu, ret;
881+
struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
905882

906-
irqs = kcalloc(pdev->num_resources, sizeof(*irqs), GFP_KERNEL);
907-
if (!irqs)
908-
return -ENOMEM;
883+
ret = irq_get_percpu_devid_partition(irq, &pmu->supported_cpus);
884+
if (ret)
885+
return ret;
909886

910-
do {
911-
struct device_node *dn;
912-
int cpu, irq;
887+
for_each_cpu(cpu, &pmu->supported_cpus)
888+
per_cpu(hw_events->irq, cpu) = irq;
913889

914-
/* See if we have an affinity entry */
915-
dn = of_parse_phandle(pdev->dev.of_node, "interrupt-affinity", i);
916-
if (!dn)
917-
break;
890+
return 0;
891+
}
918892

919-
/* Check the IRQ type and prohibit a mix of PPIs and SPIs */
920-
irq = platform_get_irq(pdev, i);
921-
if (irq > 0) {
922-
bool spi = !irq_is_percpu(irq);
923-
924-
if (i > 0 && spi != using_spi) {
925-
pr_err("PPI/SPI IRQ type mismatch for %s!\n",
926-
dn->name);
927-
of_node_put(dn);
928-
kfree(irqs);
929-
return -EINVAL;
930-
}
893+
static bool pmu_has_irq_affinity(struct device_node *node)
894+
{
895+
return !!of_find_property(node, "interrupt-affinity", NULL);
896+
}
931897

932-
using_spi = spi;
933-
}
898+
static int pmu_parse_irq_affinity(struct device_node *node, int i)
899+
{
900+
struct device_node *dn;
901+
int cpu;
934902

935-
/* Now look up the logical CPU number */
936-
for_each_possible_cpu(cpu) {
937-
struct device_node *cpu_dn;
903+
/*
904+
* If we don't have an interrupt-affinity property, we guess irq
905+
* affinity matches our logical CPU order, as we used to assume.
906+
* This is fragile, so we'll warn in pmu_parse_irqs().
907+
*/
908+
if (!pmu_has_irq_affinity(node))
909+
return i;
938910

939-
cpu_dn = of_cpu_device_node_get(cpu);
940-
of_node_put(cpu_dn);
911+
dn = of_parse_phandle(node, "interrupt-affinity", i);
912+
if (!dn) {
913+
pr_warn("failed to parse interrupt-affinity[%d] for %s\n",
914+
i, node->name);
915+
return -EINVAL;
916+
}
941917

942-
if (dn == cpu_dn)
943-
break;
944-
}
918+
/* Now look up the logical CPU number */
919+
for_each_possible_cpu(cpu) {
920+
struct device_node *cpu_dn;
921+
922+
cpu_dn = of_cpu_device_node_get(cpu);
923+
of_node_put(cpu_dn);
945924

946-
if (cpu >= nr_cpu_ids) {
947-
pr_warn("Failed to find logical CPU for %s\n",
948-
dn->name);
949-
of_node_put(dn);
950-
cpumask_setall(&pmu->supported_cpus);
925+
if (dn == cpu_dn)
951926
break;
952-
}
953-
of_node_put(dn);
927+
}
954928

955-
/* For SPIs, we need to track the affinity per IRQ */
956-
if (using_spi) {
957-
if (i >= pdev->num_resources)
958-
break;
929+
if (cpu >= nr_cpu_ids) {
930+
pr_warn("failed to find logical CPU for %s\n", dn->name);
931+
}
959932

960-
irqs[i] = cpu;
961-
}
933+
of_node_put(dn);
962934

963-
/* Keep track of the CPUs containing this PMU type */
964-
cpumask_set_cpu(cpu, &pmu->supported_cpus);
965-
i++;
966-
} while (1);
935+
return cpu;
936+
}
937+
938+
static int pmu_parse_irqs(struct arm_pmu *pmu)
939+
{
940+
int i = 0, irqs;
941+
struct platform_device *pdev = pmu->plat_device;
942+
struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
943+
944+
irqs = platform_irq_count(pdev);
945+
if (irqs < 0) {
946+
pr_err("unable to count PMU IRQs\n");
947+
return irqs;
948+
}
949+
950+
/*
951+
* In this case we have no idea which CPUs are covered by the PMU.
952+
* To match our prior behaviour, we assume all CPUs in this case.
953+
*/
954+
if (irqs == 0) {
955+
pr_warn("no irqs for PMU, sampling events not supported\n");
956+
pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
957+
cpumask_setall(&pmu->supported_cpus);
958+
return 0;
959+
}
967960

968-
/* If we didn't manage to parse anything, try the interrupt affinity */
969-
if (cpumask_weight(&pmu->supported_cpus) == 0) {
961+
if (irqs == 1) {
970962
int irq = platform_get_irq(pdev, 0);
963+
if (irq && irq_is_percpu(irq))
964+
return pmu_parse_percpu_irq(pmu, irq);
965+
}
971966

972-
if (irq > 0 && irq_is_percpu(irq)) {
973-
/* If using PPIs, check the affinity of the partition */
974-
int ret;
967+
if (!pmu_has_irq_affinity(pdev->dev.of_node)) {
968+
pr_warn("no interrupt-affinity property for %s, guessing.\n",
969+
of_node_full_name(pdev->dev.of_node));
970+
}
975971

976-
ret = irq_get_percpu_devid_partition(irq, &pmu->supported_cpus);
977-
if (ret) {
978-
kfree(irqs);
979-
return ret;
980-
}
981-
} else {
982-
/* Otherwise default to all CPUs */
983-
cpumask_setall(&pmu->supported_cpus);
972+
/*
973+
* Some platforms have all PMU IRQs OR'd into a single IRQ, with a
974+
* special platdata function that attempts to demux them.
975+
*/
976+
if (dev_get_platdata(&pdev->dev))
977+
cpumask_setall(&pmu->supported_cpus);
978+
979+
for (i = 0; i < irqs; i++) {
980+
int cpu, irq;
981+
982+
irq = platform_get_irq(pdev, i);
983+
if (WARN_ON(irq <= 0))
984+
continue;
985+
986+
if (irq_is_percpu(irq)) {
987+
pr_warn("multiple PPIs or mismatched SPI/PPI detected\n");
988+
return -EINVAL;
984989
}
985-
}
986990

987-
/* If we matched up the IRQ affinities, use them to route the SPIs */
988-
if (using_spi && i == pdev->num_resources)
989-
pmu->irq_affinity = irqs;
990-
else
991-
kfree(irqs);
991+
cpu = pmu_parse_irq_affinity(pdev->dev.of_node, i);
992+
if (cpu < 0)
993+
return cpu;
994+
if (cpu >= nr_cpu_ids)
995+
continue;
996+
997+
if (per_cpu(hw_events->irq, cpu)) {
998+
pr_warn("multiple PMU IRQs for the same CPU detected\n");
999+
return -EINVAL;
1000+
}
1001+
1002+
per_cpu(hw_events->irq, cpu) = irq;
1003+
cpumask_set_cpu(cpu, &pmu->supported_cpus);
1004+
}
9921005

9931006
return 0;
9941007
}
@@ -1050,6 +1063,10 @@ int arm_pmu_device_probe(struct platform_device *pdev,
10501063

10511064
pmu->plat_device = pdev;
10521065

1066+
ret = pmu_parse_irqs(pmu);
1067+
if (ret)
1068+
goto out_free;
1069+
10531070
if (node && (of_id = of_match_node(of_table, pdev->dev.of_node))) {
10541071
init_fn = of_id->data;
10551072

@@ -1062,9 +1079,7 @@ int arm_pmu_device_probe(struct platform_device *pdev,
10621079
pmu->secure_access = false;
10631080
}
10641081

1065-
ret = of_pmu_irq_cfg(pmu);
1066-
if (!ret)
1067-
ret = init_fn(pmu);
1082+
ret = init_fn(pmu);
10681083
} else if (probe_table) {
10691084
cpumask_setall(&pmu->supported_cpus);
10701085
ret = probe_current_pmu(pmu, probe_table);
@@ -1097,7 +1112,6 @@ int arm_pmu_device_probe(struct platform_device *pdev,
10971112
out_free:
10981113
pr_info("%s: failed to register PMU devices!\n",
10991114
of_node_full_name(node));
1100-
kfree(pmu->irq_affinity);
11011115
armpmu_free(pmu);
11021116
return ret;
11031117
}

0 commit comments

Comments
 (0)