2 * ACPI probing code for ARM performance counters.
4 * Copyright (C) 2017 ARM Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/acpi.h>
12 #include <linux/cpumask.h>
13 #include <linux/init.h>
14 #include <linux/percpu.h>
15 #include <linux/perf/arm_pmu.h>
17 #include <asm/cputype.h>
19 static DEFINE_PER_CPU(struct arm_pmu
*, probed_pmus
);
20 static DEFINE_PER_CPU(int, pmu_irqs
);
22 static int arm_pmu_acpi_register_irq(int cpu
)
24 struct acpi_madt_generic_interrupt
*gicc
;
27 gicc
= acpi_cpu_get_madt_gicc(cpu
);
31 gsi
= gicc
->performance_interrupt
;
32 if (gicc
->flags
& ACPI_MADT_PERFORMANCE_IRQ_MODE
)
33 trigger
= ACPI_EDGE_SENSITIVE
;
35 trigger
= ACPI_LEVEL_SENSITIVE
;
38 * Helpfully, the MADT GICC doesn't have a polarity flag for the
39 * "performance interrupt". Luckily, on compliant GICs the polarity is
40 * a fixed value in HW (for both SPIs and PPIs) that we cannot change
43 * Here we pass in ACPI_ACTIVE_HIGH to keep the core code happy. This
44 * may not match the real polarity, but that should not matter.
46 * Other interrupt controllers are not supported with ACPI.
48 return acpi_register_gsi(NULL
, gsi
, trigger
, ACPI_ACTIVE_HIGH
);
51 static void arm_pmu_acpi_unregister_irq(int cpu
)
53 struct acpi_madt_generic_interrupt
*gicc
;
56 gicc
= acpi_cpu_get_madt_gicc(cpu
);
60 gsi
= gicc
->performance_interrupt
;
61 acpi_unregister_gsi(gsi
);
64 static int arm_pmu_acpi_parse_irqs(void)
66 int irq
, cpu
, irq_cpu
, err
;
68 for_each_possible_cpu(cpu
) {
69 irq
= arm_pmu_acpi_register_irq(cpu
);
72 pr_warn("Unable to parse ACPI PMU IRQ for CPU%d: %d\n",
75 } else if (irq
== 0) {
76 pr_warn("No ACPI PMU IRQ for CPU%d\n", cpu
);
79 per_cpu(pmu_irqs
, cpu
) = irq
;
85 for_each_possible_cpu(cpu
) {
86 irq
= per_cpu(pmu_irqs
, cpu
);
90 arm_pmu_acpi_unregister_irq(cpu
);
93 * Blat all copies of the IRQ so that we only unregister the
94 * corresponding GSI once (e.g. when we have PPIs).
96 for_each_possible_cpu(irq_cpu
) {
97 if (per_cpu(pmu_irqs
, irq_cpu
) == irq
)
98 per_cpu(pmu_irqs
, irq_cpu
) = 0;
105 static struct arm_pmu
*arm_pmu_acpi_find_alloc_pmu(void)
107 unsigned long cpuid
= read_cpuid_id();
111 for_each_possible_cpu(cpu
) {
112 pmu
= per_cpu(probed_pmus
, cpu
);
113 if (!pmu
|| pmu
->acpi_cpuid
!= cpuid
)
119 pmu
= armpmu_alloc();
121 pr_warn("Unable to allocate PMU for CPU%d\n",
126 pmu
->acpi_cpuid
= cpuid
;
132 * This must run before the common arm_pmu hotplug logic, so that we can
133 * associate a CPU and its interrupt before the common code tries to manage the
134 * affinity and so on.
136 * Note that hotplug events are serialized, so we cannot race with another CPU
137 * coming up. The perf core won't open events while a hotplug event is in
140 static int arm_pmu_acpi_cpu_starting(unsigned int cpu
)
143 struct pmu_hw_events __percpu
*hw_events
;
146 /* If we've already probed this CPU, we have nothing to do */
147 if (per_cpu(probed_pmus
, cpu
))
150 irq
= per_cpu(pmu_irqs
, cpu
);
152 pmu
= arm_pmu_acpi_find_alloc_pmu();
156 cpumask_set_cpu(cpu
, &pmu
->supported_cpus
);
158 per_cpu(probed_pmus
, cpu
) = pmu
;
161 * Log and request the IRQ so the core arm_pmu code can manage it. In
162 * some situations (e.g. mismatched PPIs), we may fail to request the
163 * IRQ. However, it may be too late for us to do anything about it.
164 * The common ARM PMU code will log a warning in this case.
166 hw_events
= pmu
->hw_events
;
167 per_cpu(hw_events
->irq
, cpu
) = irq
;
168 armpmu_request_irq(pmu
, cpu
);
171 * Ideally, we'd probe the PMU here when we find the first matching
172 * CPU. We can't do that for several reasons; see the comment in
173 * arm_pmu_acpi_init().
175 * So for the time being, we're done.
180 int arm_pmu_acpi_probe(armpmu_init_fn init_fn
)
189 * Initialise and register the set of PMUs which we know about right
190 * now. Ideally we'd do this in arm_pmu_acpi_cpu_starting() so that we
191 * could handle late hotplug, but this may lead to deadlock since we
192 * might try to register a hotplug notifier instance from within a
195 * There's also the problem of having access to the right init_fn,
196 * without tying this too deeply into the "real" PMU driver.
198 * For the moment, as with the platform/DT case, we need at least one
199 * of a PMU's CPUs to be online at probe time.
201 for_each_possible_cpu(cpu
) {
202 struct arm_pmu
*pmu
= per_cpu(probed_pmus
, cpu
);
205 if (!pmu
|| pmu
->name
)
209 if (ret
== -ENODEV
) {
210 /* PMU not handled by this driver, or not present */
213 pr_warn("Unable to initialise PMU for CPU%d\n", cpu
);
217 base_name
= pmu
->name
;
218 pmu
->name
= kasprintf(GFP_KERNEL
, "%s_%d", base_name
, pmu_idx
++);
220 pr_warn("Unable to allocate PMU name for CPU%d\n", cpu
);
224 ret
= armpmu_register(pmu
);
226 pr_warn("Failed to register PMU for CPU%d\n", cpu
);
234 static int arm_pmu_acpi_init(void)
242 * We can't request IRQs yet, since we don't know the cookie value
243 * until we know which CPUs share the same logical PMU. We'll handle
244 * that in arm_pmu_acpi_cpu_starting().
246 ret
= arm_pmu_acpi_parse_irqs();
250 ret
= cpuhp_setup_state(CPUHP_AP_PERF_ARM_ACPI_STARTING
,
251 "perf/arm/pmu_acpi:starting",
252 arm_pmu_acpi_cpu_starting
, NULL
);
256 subsys_initcall(arm_pmu_acpi_init
)