blob: 09a1a36cff579c71370d29a0b230ec62b2942087 [file] [log] [blame]
Mark Rutland45736a72017-04-11 09:39:55 +01001/*
2 * ACPI probing code for ARM performance counters.
3 *
4 * Copyright (C) 2017 ARM Ltd.
5 *
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.
9 */
10
11#include <linux/acpi.h>
12#include <linux/cpumask.h>
13#include <linux/init.h>
Mark Rutland43fc9a22018-02-05 16:41:59 +000014#include <linux/irq.h>
15#include <linux/irqdesc.h>
Mark Rutland45736a72017-04-11 09:39:55 +010016#include <linux/percpu.h>
17#include <linux/perf/arm_pmu.h>
18
19#include <asm/cputype.h>
20
21static DEFINE_PER_CPU(struct arm_pmu *, probed_pmus);
22static DEFINE_PER_CPU(int, pmu_irqs);
23
24static int arm_pmu_acpi_register_irq(int cpu)
25{
26 struct acpi_madt_generic_interrupt *gicc;
27 int gsi, trigger;
28
29 gicc = acpi_cpu_get_madt_gicc(cpu);
30 if (WARN_ON(!gicc))
31 return -EINVAL;
32
33 gsi = gicc->performance_interrupt;
Wei Huang477c50e2017-05-30 11:56:22 +010034
35 /*
36 * Per the ACPI spec, the MADT cannot describe a PMU that doesn't
37 * have an interrupt. QEMU advertises this by using a GSI of zero,
38 * which is not known to be valid on any hardware despite being
39 * valid per the spec. Take the pragmatic approach and reject a
40 * GSI of zero for now.
41 */
42 if (!gsi)
43 return 0;
44
Mark Rutland45736a72017-04-11 09:39:55 +010045 if (gicc->flags & ACPI_MADT_PERFORMANCE_IRQ_MODE)
46 trigger = ACPI_EDGE_SENSITIVE;
47 else
48 trigger = ACPI_LEVEL_SENSITIVE;
49
50 /*
51 * Helpfully, the MADT GICC doesn't have a polarity flag for the
52 * "performance interrupt". Luckily, on compliant GICs the polarity is
53 * a fixed value in HW (for both SPIs and PPIs) that we cannot change
54 * from SW.
55 *
56 * Here we pass in ACPI_ACTIVE_HIGH to keep the core code happy. This
57 * may not match the real polarity, but that should not matter.
58 *
59 * Other interrupt controllers are not supported with ACPI.
60 */
61 return acpi_register_gsi(NULL, gsi, trigger, ACPI_ACTIVE_HIGH);
62}
63
64static void arm_pmu_acpi_unregister_irq(int cpu)
65{
66 struct acpi_madt_generic_interrupt *gicc;
67 int gsi;
68
69 gicc = acpi_cpu_get_madt_gicc(cpu);
70 if (!gicc)
71 return;
72
73 gsi = gicc->performance_interrupt;
74 acpi_unregister_gsi(gsi);
75}
76
77static int arm_pmu_acpi_parse_irqs(void)
78{
79 int irq, cpu, irq_cpu, err;
80
81 for_each_possible_cpu(cpu) {
82 irq = arm_pmu_acpi_register_irq(cpu);
83 if (irq < 0) {
84 err = irq;
85 pr_warn("Unable to parse ACPI PMU IRQ for CPU%d: %d\n",
86 cpu, err);
87 goto out_err;
88 } else if (irq == 0) {
89 pr_warn("No ACPI PMU IRQ for CPU%d\n", cpu);
90 }
91
92 per_cpu(pmu_irqs, cpu) = irq;
93 }
94
95 return 0;
96
97out_err:
98 for_each_possible_cpu(cpu) {
99 irq = per_cpu(pmu_irqs, cpu);
100 if (!irq)
101 continue;
102
103 arm_pmu_acpi_unregister_irq(cpu);
104
105 /*
106 * Blat all copies of the IRQ so that we only unregister the
107 * corresponding GSI once (e.g. when we have PPIs).
108 */
109 for_each_possible_cpu(irq_cpu) {
110 if (per_cpu(pmu_irqs, irq_cpu) == irq)
111 per_cpu(pmu_irqs, irq_cpu) = 0;
112 }
113 }
114
115 return err;
116}
117
118static struct arm_pmu *arm_pmu_acpi_find_alloc_pmu(void)
119{
120 unsigned long cpuid = read_cpuid_id();
121 struct arm_pmu *pmu;
122 int cpu;
123
124 for_each_possible_cpu(cpu) {
125 pmu = per_cpu(probed_pmus, cpu);
126 if (!pmu || pmu->acpi_cpuid != cpuid)
127 continue;
128
129 return pmu;
130 }
131
Mark Rutland0dc1a182018-02-05 16:41:58 +0000132 pmu = armpmu_alloc_atomic();
Mark Rutland45736a72017-04-11 09:39:55 +0100133 if (!pmu) {
134 pr_warn("Unable to allocate PMU for CPU%d\n",
135 smp_processor_id());
136 return NULL;
137 }
138
139 pmu->acpi_cpuid = cpuid;
140
141 return pmu;
142}
143
144/*
Mark Rutland43fc9a22018-02-05 16:41:59 +0000145 * Check whether the new IRQ is compatible with those already associated with
146 * the PMU (e.g. we don't have mismatched PPIs).
147 */
148static bool pmu_irq_matches(struct arm_pmu *pmu, int irq)
149{
150 struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
151 int cpu;
152
153 if (!irq)
154 return true;
155
156 for_each_cpu(cpu, &pmu->supported_cpus) {
157 int other_irq = per_cpu(hw_events->irq, cpu);
158 if (!other_irq)
159 continue;
160
161 if (irq == other_irq)
162 continue;
163 if (!irq_is_percpu_devid(irq) && !irq_is_percpu_devid(other_irq))
164 continue;
165
166 pr_warn("mismatched PPIs detected\n");
167 return false;
168 }
169
170 return true;
171}
172
173/*
Mark Rutland45736a72017-04-11 09:39:55 +0100174 * This must run before the common arm_pmu hotplug logic, so that we can
175 * associate a CPU and its interrupt before the common code tries to manage the
176 * affinity and so on.
177 *
178 * Note that hotplug events are serialized, so we cannot race with another CPU
179 * coming up. The perf core won't open events while a hotplug event is in
180 * progress.
181 */
182static int arm_pmu_acpi_cpu_starting(unsigned int cpu)
183{
184 struct arm_pmu *pmu;
185 struct pmu_hw_events __percpu *hw_events;
186 int irq;
187
188 /* If we've already probed this CPU, we have nothing to do */
189 if (per_cpu(probed_pmus, cpu))
190 return 0;
191
192 irq = per_cpu(pmu_irqs, cpu);
193
194 pmu = arm_pmu_acpi_find_alloc_pmu();
195 if (!pmu)
196 return -ENOMEM;
197
Mark Rutland45736a72017-04-11 09:39:55 +0100198 per_cpu(probed_pmus, cpu) = pmu;
199
Mark Rutland43fc9a22018-02-05 16:41:59 +0000200 if (pmu_irq_matches(pmu, irq)) {
201 hw_events = pmu->hw_events;
202 per_cpu(hw_events->irq, cpu) = irq;
203 }
204
205 cpumask_set_cpu(cpu, &pmu->supported_cpus);
206
Mark Rutland45736a72017-04-11 09:39:55 +0100207 /*
208 * Log and request the IRQ so the core arm_pmu code can manage it. In
209 * some situations (e.g. mismatched PPIs), we may fail to request the
210 * IRQ. However, it may be too late for us to do anything about it.
211 * The common ARM PMU code will log a warning in this case.
212 */
Mark Rutland45736a72017-04-11 09:39:55 +0100213 armpmu_request_irq(pmu, cpu);
214
215 /*
216 * Ideally, we'd probe the PMU here when we find the first matching
217 * CPU. We can't do that for several reasons; see the comment in
218 * arm_pmu_acpi_init().
219 *
220 * So for the time being, we're done.
221 */
222 return 0;
223}
224
225int arm_pmu_acpi_probe(armpmu_init_fn init_fn)
226{
227 int pmu_idx = 0;
228 int cpu, ret;
229
Mark Rutland45736a72017-04-11 09:39:55 +0100230 /*
231 * Initialise and register the set of PMUs which we know about right
232 * now. Ideally we'd do this in arm_pmu_acpi_cpu_starting() so that we
233 * could handle late hotplug, but this may lead to deadlock since we
234 * might try to register a hotplug notifier instance from within a
235 * hotplug notifier.
236 *
237 * There's also the problem of having access to the right init_fn,
238 * without tying this too deeply into the "real" PMU driver.
239 *
240 * For the moment, as with the platform/DT case, we need at least one
241 * of a PMU's CPUs to be online at probe time.
242 */
243 for_each_possible_cpu(cpu) {
244 struct arm_pmu *pmu = per_cpu(probed_pmus, cpu);
245 char *base_name;
246
247 if (!pmu || pmu->name)
248 continue;
249
250 ret = init_fn(pmu);
251 if (ret == -ENODEV) {
252 /* PMU not handled by this driver, or not present */
253 continue;
254 } else if (ret) {
255 pr_warn("Unable to initialise PMU for CPU%d\n", cpu);
256 return ret;
257 }
258
259 base_name = pmu->name;
260 pmu->name = kasprintf(GFP_KERNEL, "%s_%d", base_name, pmu_idx++);
261 if (!pmu->name) {
262 pr_warn("Unable to allocate PMU name for CPU%d\n", cpu);
263 return -ENOMEM;
264 }
265
266 ret = armpmu_register(pmu);
267 if (ret) {
268 pr_warn("Failed to register PMU for CPU%d\n", cpu);
Arvind Yadava88dc7b2017-09-20 12:26:38 +0530269 kfree(pmu->name);
Mark Rutland45736a72017-04-11 09:39:55 +0100270 return ret;
271 }
272 }
273
274 return 0;
275}
276
277static int arm_pmu_acpi_init(void)
278{
279 int ret;
280
281 if (acpi_disabled)
282 return 0;
283
284 /*
285 * We can't request IRQs yet, since we don't know the cookie value
286 * until we know which CPUs share the same logical PMU. We'll handle
287 * that in arm_pmu_acpi_cpu_starting().
288 */
289 ret = arm_pmu_acpi_parse_irqs();
290 if (ret)
291 return ret;
292
293 ret = cpuhp_setup_state(CPUHP_AP_PERF_ARM_ACPI_STARTING,
294 "perf/arm/pmu_acpi:starting",
295 arm_pmu_acpi_cpu_starting, NULL);
296
297 return ret;
298}
299subsys_initcall(arm_pmu_acpi_init)