blob: 295cc7952d0edf9d622727357d1c3d607ac4aa0b [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Jamie Iles1b8873a2010-02-02 20:25:44 +01002#undef DEBUG
3
4/*
5 * ARM performance counter support.
6 *
7 * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
Will Deacon43eab872010-11-13 19:04:32 +00008 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
Jean PIHET796d1292010-01-26 18:51:05 +01009 *
Jamie Iles1b8873a2010-02-02 20:25:44 +010010 * This code is based on the sparc64 perf event code, which is in turn based
Mark Rutlandd39976f2014-09-29 17:15:32 +010011 * on the x86 code.
Jamie Iles1b8873a2010-02-02 20:25:44 +010012 */
13#define pr_fmt(fmt) "hw perfevents: " fmt
14
Mark Rutland74cf0bc2015-05-26 17:23:39 +010015#include <linux/bitmap.h>
Mark Rutlandcc88116d2015-05-13 17:12:25 +010016#include <linux/cpumask.h>
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +000017#include <linux/cpu_pm.h>
Mark Rutland74cf0bc2015-05-26 17:23:39 +010018#include <linux/export.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010019#include <linux/kernel.h>
Mark Rutlandfa8ad782015-07-06 12:23:53 +010020#include <linux/perf/arm_pmu.h>
Mark Rutland74cf0bc2015-05-26 17:23:39 +010021#include <linux/slab.h>
Ingo Molnare6017572017-02-01 16:36:40 +010022#include <linux/sched/clock.h>
Mark Rutland74cf0bc2015-05-26 17:23:39 +010023#include <linux/spinlock.h>
Stephen Boydbbd64552014-02-07 21:01:19 +000024#include <linux/irq.h>
25#include <linux/irqdesc.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010026
Jamie Iles1b8873a2010-02-02 20:25:44 +010027#include <asm/irq_regs.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010028
Julien Thierryf76b1302020-09-24 12:07:05 +010029static int armpmu_count_irq_users(const int irq);
30
31struct pmu_irq_ops {
32 void (*enable_pmuirq)(unsigned int irq);
33 void (*disable_pmuirq)(unsigned int irq);
34 void (*free_pmuirq)(unsigned int irq, int cpu, void __percpu *devid);
35};
36
37static void armpmu_free_pmuirq(unsigned int irq, int cpu, void __percpu *devid)
38{
39 free_irq(irq, per_cpu_ptr(devid, cpu));
40}
41
42static const struct pmu_irq_ops pmuirq_ops = {
43 .enable_pmuirq = enable_irq,
44 .disable_pmuirq = disable_irq_nosync,
45 .free_pmuirq = armpmu_free_pmuirq
46};
47
Julien Thierryd8f62672020-09-24 12:07:06 +010048static void armpmu_free_pmunmi(unsigned int irq, int cpu, void __percpu *devid)
49{
50 free_nmi(irq, per_cpu_ptr(devid, cpu));
51}
52
53static const struct pmu_irq_ops pmunmi_ops = {
54 .enable_pmuirq = enable_nmi,
55 .disable_pmuirq = disable_nmi_nosync,
56 .free_pmuirq = armpmu_free_pmunmi
57};
58
Julien Thierryf76b1302020-09-24 12:07:05 +010059static void armpmu_enable_percpu_pmuirq(unsigned int irq)
60{
61 enable_percpu_irq(irq, IRQ_TYPE_NONE);
62}
63
64static void armpmu_free_percpu_pmuirq(unsigned int irq, int cpu,
65 void __percpu *devid)
66{
67 if (armpmu_count_irq_users(irq) == 1)
68 free_percpu_irq(irq, devid);
69}
70
71static const struct pmu_irq_ops percpu_pmuirq_ops = {
72 .enable_pmuirq = armpmu_enable_percpu_pmuirq,
73 .disable_pmuirq = disable_percpu_irq,
74 .free_pmuirq = armpmu_free_percpu_pmuirq
75};
76
Julien Thierryd8f62672020-09-24 12:07:06 +010077static void armpmu_enable_percpu_pmunmi(unsigned int irq)
78{
79 if (!prepare_percpu_nmi(irq))
80 enable_percpu_nmi(irq, IRQ_TYPE_NONE);
81}
82
83static void armpmu_disable_percpu_pmunmi(unsigned int irq)
84{
85 disable_percpu_nmi(irq);
86 teardown_percpu_nmi(irq);
87}
88
89static void armpmu_free_percpu_pmunmi(unsigned int irq, int cpu,
90 void __percpu *devid)
91{
92 if (armpmu_count_irq_users(irq) == 1)
93 free_percpu_nmi(irq, devid);
94}
95
96static const struct pmu_irq_ops percpu_pmunmi_ops = {
97 .enable_pmuirq = armpmu_enable_percpu_pmunmi,
98 .disable_pmuirq = armpmu_disable_percpu_pmunmi,
99 .free_pmuirq = armpmu_free_percpu_pmunmi
100};
101
Mark Rutland84b4be52017-12-12 16:56:06 +0000102static DEFINE_PER_CPU(struct arm_pmu *, cpu_armpmu);
103static DEFINE_PER_CPU(int, cpu_irq);
Julien Thierryf76b1302020-09-24 12:07:05 +0100104static DEFINE_PER_CPU(const struct pmu_irq_ops *, cpu_irq_ops);
Mark Rutland84b4be52017-12-12 16:56:06 +0000105
Julien Thierryd8f62672020-09-24 12:07:06 +0100106static bool has_nmi;
107
Suzuki K Poulosee2da97d2018-07-10 09:58:00 +0100108static inline u64 arm_pmu_event_max_period(struct perf_event *event)
Suzuki K Poulose8d3e9942018-07-10 09:57:58 +0100109{
Suzuki K Poulosee2da97d2018-07-10 09:58:00 +0100110 if (event->hw.flags & ARMPMU_EVT_64BIT)
111 return GENMASK_ULL(63, 0);
112 else
113 return GENMASK_ULL(31, 0);
Suzuki K Poulose8d3e9942018-07-10 09:57:58 +0100114}
115
Jamie Iles1b8873a2010-02-02 20:25:44 +0100116static int
Mark Rutlande1f431b2011-04-28 15:47:10 +0100117armpmu_map_cache_event(const unsigned (*cache_map)
118 [PERF_COUNT_HW_CACHE_MAX]
119 [PERF_COUNT_HW_CACHE_OP_MAX]
120 [PERF_COUNT_HW_CACHE_RESULT_MAX],
121 u64 config)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100122{
123 unsigned int cache_type, cache_op, cache_result, ret;
124
125 cache_type = (config >> 0) & 0xff;
126 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
127 return -EINVAL;
128
129 cache_op = (config >> 8) & 0xff;
130 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
131 return -EINVAL;
132
133 cache_result = (config >> 16) & 0xff;
134 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
135 return -EINVAL;
136
Will Deacon6c833bb2017-08-08 16:58:33 +0100137 if (!cache_map)
138 return -ENOENT;
139
Mark Rutlande1f431b2011-04-28 15:47:10 +0100140 ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100141
142 if (ret == CACHE_OP_UNSUPPORTED)
143 return -ENOENT;
144
145 return ret;
146}
147
148static int
Will Deacon6dbc0022012-07-29 12:36:28 +0100149armpmu_map_hw_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
Will Deacon84fee972010-11-13 17:13:56 +0000150{
Stephen Boydd9f96632013-08-08 18:41:59 +0100151 int mapping;
152
153 if (config >= PERF_COUNT_HW_MAX)
154 return -EINVAL;
155
Will Deacon6c833bb2017-08-08 16:58:33 +0100156 if (!event_map)
157 return -ENOENT;
158
Stephen Boydd9f96632013-08-08 18:41:59 +0100159 mapping = (*event_map)[config];
Mark Rutlande1f431b2011-04-28 15:47:10 +0100160 return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
Will Deacon84fee972010-11-13 17:13:56 +0000161}
162
163static int
Mark Rutlande1f431b2011-04-28 15:47:10 +0100164armpmu_map_raw_event(u32 raw_event_mask, u64 config)
Will Deacon84fee972010-11-13 17:13:56 +0000165{
Mark Rutlande1f431b2011-04-28 15:47:10 +0100166 return (int)(config & raw_event_mask);
167}
168
Will Deacon6dbc0022012-07-29 12:36:28 +0100169int
170armpmu_map_event(struct perf_event *event,
171 const unsigned (*event_map)[PERF_COUNT_HW_MAX],
172 const unsigned (*cache_map)
173 [PERF_COUNT_HW_CACHE_MAX]
174 [PERF_COUNT_HW_CACHE_OP_MAX]
175 [PERF_COUNT_HW_CACHE_RESULT_MAX],
176 u32 raw_event_mask)
Mark Rutlande1f431b2011-04-28 15:47:10 +0100177{
178 u64 config = event->attr.config;
Mark Rutland67b43052012-09-12 10:53:23 +0100179 int type = event->attr.type;
Mark Rutlande1f431b2011-04-28 15:47:10 +0100180
Mark Rutland67b43052012-09-12 10:53:23 +0100181 if (type == event->pmu->type)
182 return armpmu_map_raw_event(raw_event_mask, config);
183
184 switch (type) {
Mark Rutlande1f431b2011-04-28 15:47:10 +0100185 case PERF_TYPE_HARDWARE:
Will Deacon6dbc0022012-07-29 12:36:28 +0100186 return armpmu_map_hw_event(event_map, config);
Mark Rutlande1f431b2011-04-28 15:47:10 +0100187 case PERF_TYPE_HW_CACHE:
188 return armpmu_map_cache_event(cache_map, config);
189 case PERF_TYPE_RAW:
190 return armpmu_map_raw_event(raw_event_mask, config);
191 }
192
193 return -ENOENT;
Will Deacon84fee972010-11-13 17:13:56 +0000194}
195
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100196int armpmu_event_set_period(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100197{
Mark Rutland8a16b342011-04-28 16:27:54 +0100198 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100199 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200200 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100201 s64 period = hwc->sample_period;
Suzuki K Poulose8d3e9942018-07-10 09:57:58 +0100202 u64 max_period;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100203 int ret = 0;
204
Suzuki K Poulosee2da97d2018-07-10 09:58:00 +0100205 max_period = arm_pmu_event_max_period(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100206 if (unlikely(left <= -period)) {
207 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200208 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100209 hwc->last_period = period;
210 ret = 1;
211 }
212
213 if (unlikely(left <= 0)) {
214 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200215 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100216 hwc->last_period = period;
217 ret = 1;
218 }
219
Daniel Thompson2d9ed742015-01-05 15:58:54 +0100220 /*
221 * Limit the maximum period to prevent the counter value
222 * from overtaking the one we are about to program. In
223 * effect we are reducing max_period to account for
224 * interrupt latency (and we are being very conservative).
225 */
Suzuki K Poulose8d3e9942018-07-10 09:57:58 +0100226 if (left > (max_period >> 1))
227 left = (max_period >> 1);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100228
Peter Zijlstrae7850592010-05-21 14:43:08 +0200229 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100230
Suzuki K Poulosee2da97d2018-07-10 09:58:00 +0100231 armpmu->write_counter(event, (u64)(-left) & max_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100232
233 perf_event_update_userpage(event);
234
235 return ret;
236}
237
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100238u64 armpmu_event_update(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100239{
Mark Rutland8a16b342011-04-28 16:27:54 +0100240 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100241 struct hw_perf_event *hwc = &event->hw;
Will Deacona7378232011-03-25 17:12:37 +0100242 u64 delta, prev_raw_count, new_raw_count;
Suzuki K Poulosee2da97d2018-07-10 09:58:00 +0100243 u64 max_period = arm_pmu_event_max_period(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100244
245again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200246 prev_raw_count = local64_read(&hwc->prev_count);
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100247 new_raw_count = armpmu->read_counter(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100248
Peter Zijlstrae7850592010-05-21 14:43:08 +0200249 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100250 new_raw_count) != prev_raw_count)
251 goto again;
252
Suzuki K Poulose8d3e9942018-07-10 09:57:58 +0100253 delta = (new_raw_count - prev_raw_count) & max_period;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100254
Peter Zijlstrae7850592010-05-21 14:43:08 +0200255 local64_add(delta, &event->count);
256 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100257
258 return new_raw_count;
259}
260
261static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100262armpmu_read(struct perf_event *event)
263{
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100264 armpmu_event_update(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100265}
266
267static void
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200268armpmu_stop(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100269{
Mark Rutland8a16b342011-04-28 16:27:54 +0100270 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100271 struct hw_perf_event *hwc = &event->hw;
272
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200273 /*
274 * ARM pmu always has to update the counter, so ignore
275 * PERF_EF_UPDATE, see comments in armpmu_start().
276 */
277 if (!(hwc->state & PERF_HES_STOPPED)) {
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100278 armpmu->disable(event);
279 armpmu_event_update(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200280 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
281 }
282}
283
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100284static void armpmu_start(struct perf_event *event, int flags)
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200285{
Mark Rutland8a16b342011-04-28 16:27:54 +0100286 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200287 struct hw_perf_event *hwc = &event->hw;
288
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200289 /*
290 * ARM pmu always has to reprogram the period, so ignore
291 * PERF_EF_RELOAD, see the comment below.
292 */
293 if (flags & PERF_EF_RELOAD)
294 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
295
296 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100297 /*
298 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200299 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100300 * may have been left counting. If we don't do this step then we may
301 * get an interrupt too soon or *way* too late if the overflow has
302 * happened since disabling.
303 */
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100304 armpmu_event_set_period(event);
305 armpmu->enable(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100306}
307
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200308static void
309armpmu_del(struct perf_event *event, int flags)
310{
Mark Rutland8a16b342011-04-28 16:27:54 +0100311 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland11679252014-05-13 19:36:31 +0100312 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200313 struct hw_perf_event *hwc = &event->hw;
314 int idx = hwc->idx;
315
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200316 armpmu_stop(event, PERF_EF_UPDATE);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100317 hw_events->events[idx] = NULL;
Suzuki K Poulose7dfc8db2018-07-10 09:58:01 +0100318 armpmu->clear_event_idx(hw_events, event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200319 perf_event_update_userpage(event);
Suzuki K Poulose7dfc8db2018-07-10 09:58:01 +0100320 /* Clear the allocated counter */
321 hwc->idx = -1;
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200322}
323
Jamie Iles1b8873a2010-02-02 20:25:44 +0100324static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200325armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100326{
Mark Rutland8a16b342011-04-28 16:27:54 +0100327 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland11679252014-05-13 19:36:31 +0100328 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100329 struct hw_perf_event *hwc = &event->hw;
330 int idx;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100331
Mark Rutlandcc88116d2015-05-13 17:12:25 +0100332 /* An event following a process won't be stopped earlier */
333 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
334 return -ENOENT;
335
Jamie Iles1b8873a2010-02-02 20:25:44 +0100336 /* If we don't have a space for the counter then finish early. */
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100337 idx = armpmu->get_event_idx(hw_events, event);
Mark Rutlanda9e469d2017-04-11 09:39:44 +0100338 if (idx < 0)
339 return idx;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100340
341 /*
342 * If there is an event in the counter we are going to use then make
343 * sure it is disabled.
344 */
345 event->hw.idx = idx;
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100346 armpmu->disable(event);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100347 hw_events->events[idx] = event;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100348
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200349 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
350 if (flags & PERF_EF_START)
351 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100352
353 /* Propagate our changes to the userspace mapping. */
354 perf_event_update_userpage(event);
355
Mark Rutlanda9e469d2017-04-11 09:39:44 +0100356 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100357}
358
Jamie Iles1b8873a2010-02-02 20:25:44 +0100359static int
Suzuki K. Poulosee4298172015-03-17 18:14:58 +0000360validate_event(struct pmu *pmu, struct pmu_hw_events *hw_events,
361 struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100362{
Suzuki K. Poulosee4298172015-03-17 18:14:58 +0000363 struct arm_pmu *armpmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100364
Will Deaconc95eb312013-08-07 23:39:41 +0100365 if (is_software_event(event))
366 return 1;
367
Suzuki K. Poulosee4298172015-03-17 18:14:58 +0000368 /*
369 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
370 * core perf code won't check that the pmu->ctx == leader->ctx
371 * until after pmu->event_init(event).
372 */
373 if (event->pmu != pmu)
374 return 0;
375
Will Deacon2dfcb802013-10-09 13:51:29 +0100376 if (event->state < PERF_EVENT_STATE_OFF)
Will Deaconcb2d8b32013-04-12 19:04:19 +0100377 return 1;
378
379 if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
Will Deacon65b47112010-09-02 09:32:08 +0100380 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100381
Suzuki K. Poulosee4298172015-03-17 18:14:58 +0000382 armpmu = to_arm_pmu(event->pmu);
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100383 return armpmu->get_event_idx(hw_events, event) >= 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100384}
385
386static int
387validate_group(struct perf_event *event)
388{
389 struct perf_event *sibling, *leader = event->group_leader;
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100390 struct pmu_hw_events fake_pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100391
Will Deaconbce34d12011-11-17 15:05:14 +0000392 /*
393 * Initialise the fake PMU. We only need to populate the
394 * used_mask for the purposes of validation.
395 */
Mark Rutlanda4560842014-05-13 19:08:19 +0100396 memset(&fake_pmu.used_mask, 0, sizeof(fake_pmu.used_mask));
Jamie Iles1b8873a2010-02-02 20:25:44 +0100397
Suzuki K. Poulosee4298172015-03-17 18:14:58 +0000398 if (!validate_event(event->pmu, &fake_pmu, leader))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100399 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100400
Peter Zijlstraedb39592018-03-15 17:36:56 +0100401 for_each_sibling_event(sibling, leader) {
Suzuki K. Poulosee4298172015-03-17 18:14:58 +0000402 if (!validate_event(event->pmu, &fake_pmu, sibling))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100403 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100404 }
405
Suzuki K. Poulosee4298172015-03-17 18:14:58 +0000406 if (!validate_event(event->pmu, &fake_pmu, event))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100407 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100408
409 return 0;
410}
411
Sudeep KarkadaNagesha051f1b12012-07-31 10:34:25 +0100412static irqreturn_t armpmu_dispatch_irq(int irq, void *dev)
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530413{
Stephen Boydbbd64552014-02-07 21:01:19 +0000414 struct arm_pmu *armpmu;
Will Deacon5f5092e2014-02-11 18:08:41 +0000415 int ret;
416 u64 start_clock, finish_clock;
Stephen Boydbbd64552014-02-07 21:01:19 +0000417
Mark Rutland5ebd9202014-05-13 19:46:10 +0100418 /*
419 * we request the IRQ with a (possibly percpu) struct arm_pmu**, but
420 * the handlers expect a struct arm_pmu*. The percpu_irq framework will
421 * do any necessary shifting, we just need to perform the first
422 * dereference.
423 */
424 armpmu = *(void **)dev;
Mark Rutland84b4be52017-12-12 16:56:06 +0000425 if (WARN_ON_ONCE(!armpmu))
426 return IRQ_NONE;
Mark Rutland76541372017-04-11 09:39:49 +0100427
Will Deacon5f5092e2014-02-11 18:08:41 +0000428 start_clock = sched_clock();
Mark Rutland0788f1e2018-05-10 11:35:15 +0100429 ret = armpmu->handle_irq(armpmu);
Will Deacon5f5092e2014-02-11 18:08:41 +0000430 finish_clock = sched_clock();
431
432 perf_sample_event_took(finish_clock - start_clock);
433 return ret;
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530434}
435
Jamie Iles1b8873a2010-02-02 20:25:44 +0100436static int
437__hw_perf_event_init(struct perf_event *event)
438{
Mark Rutland8a16b342011-04-28 16:27:54 +0100439 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100440 struct hw_perf_event *hwc = &event->hw;
Mark Rutland9dcbf462013-01-18 16:10:06 +0000441 int mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100442
Suzuki K Poulosee2da97d2018-07-10 09:58:00 +0100443 hwc->flags = 0;
Mark Rutlande1f431b2011-04-28 15:47:10 +0100444 mapping = armpmu->map_event(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100445
446 if (mapping < 0) {
447 pr_debug("event %x:%llx not supported\n", event->attr.type,
448 event->attr.config);
449 return mapping;
450 }
451
452 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100453 * We don't assign an index until we actually place the event onto
454 * hardware. Use -1 to signify that we haven't decided where to put it
455 * yet. For SMP systems, each core has it's own PMU so we can't do any
456 * clever allocation or constraints checking at this point.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100457 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100458 hwc->idx = -1;
459 hwc->config_base = 0;
460 hwc->config = 0;
461 hwc->event_base = 0;
462
463 /*
464 * Check whether we need to exclude the counter from certain modes.
465 */
Andrew Murray1d899c02019-01-10 13:53:27 +0000466 if (armpmu->set_event_filter &&
467 armpmu->set_event_filter(hwc, &event->attr)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100468 pr_debug("ARM performance counters do not support "
469 "mode exclusion\n");
Will Deaconfdeb8e32012-07-04 18:15:42 +0100470 return -EOPNOTSUPP;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100471 }
472
473 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100474 * Store the event encoding into the config_base field.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100475 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100476 hwc->config_base |= (unsigned long)mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100477
Vince Weaveredcb4d32014-05-16 17:15:49 -0400478 if (!is_sampling_event(event)) {
Will Deacon57273472012-03-06 17:33:17 +0100479 /*
480 * For non-sampling runs, limit the sample_period to half
481 * of the counter width. That way, the new counter value
482 * is far less likely to overtake the previous one unless
483 * you have some serious IRQ latency issues.
484 */
Suzuki K Poulosee2da97d2018-07-10 09:58:00 +0100485 hwc->sample_period = arm_pmu_event_max_period(event) >> 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100486 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200487 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100488 }
489
Jamie Iles1b8873a2010-02-02 20:25:44 +0100490 if (event->group_leader != event) {
Chen Gange595ede2013-02-28 17:51:29 +0100491 if (validate_group(event) != 0)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100492 return -EINVAL;
493 }
494
Mark Rutland9dcbf462013-01-18 16:10:06 +0000495 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100496}
497
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200498static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100499{
Mark Rutland8a16b342011-04-28 16:27:54 +0100500 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100501
Mark Rutlandcc88116d2015-05-13 17:12:25 +0100502 /*
503 * Reject CPU-affine events for CPUs that are of a different class to
504 * that which this PMU handles. Process-following events (where
505 * event->cpu == -1) can be migrated between CPUs, and thus we have to
506 * reject them later (in armpmu_add) if they're scheduled on a
507 * different class of CPU.
508 */
509 if (event->cpu != -1 &&
510 !cpumask_test_cpu(event->cpu, &armpmu->supported_cpus))
511 return -ENOENT;
512
Stephane Eranian2481c5f2012-02-09 23:20:59 +0100513 /* does not support taken branch sampling */
514 if (has_branch_stack(event))
515 return -EOPNOTSUPP;
516
Mark Rutlande1f431b2011-04-28 15:47:10 +0100517 if (armpmu->map_event(event) == -ENOENT)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200518 return -ENOENT;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200519
Mark Rutlandc09adab2017-03-10 10:46:15 +0000520 return __hw_perf_event_init(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100521}
522
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200523static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100524{
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100525 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Mark Rutland11679252014-05-13 19:36:31 +0100526 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
Mark Rutland7325eae2011-08-23 11:59:49 +0100527 int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100528
Mark Rutlandcc88116d2015-05-13 17:12:25 +0100529 /* For task-bound events we may be called on other CPUs */
530 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
531 return;
532
Will Deaconf4f38432011-07-01 14:38:12 +0100533 if (enabled)
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100534 armpmu->start(armpmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100535}
536
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200537static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100538{
Mark Rutland8a16b342011-04-28 16:27:54 +0100539 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Mark Rutlandcc88116d2015-05-13 17:12:25 +0100540
541 /* For task-bound events we may be called on other CPUs */
542 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
543 return;
544
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100545 armpmu->stop(armpmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100546}
547
Mark Rutlandc904e322015-05-13 17:12:26 +0100548/*
549 * In heterogeneous systems, events are specific to a particular
550 * microarchitecture, and aren't suitable for another. Thus, only match CPUs of
551 * the same microarchitecture.
552 */
553static int armpmu_filter_match(struct perf_event *event)
554{
555 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
556 unsigned int cpu = smp_processor_id();
Will Deaconca2b4972018-10-05 13:24:36 +0100557 int ret;
558
559 ret = cpumask_test_cpu(cpu, &armpmu->supported_cpus);
560 if (ret && armpmu->filter_match)
561 return armpmu->filter_match(event);
562
563 return ret;
Mark Rutlandc904e322015-05-13 17:12:26 +0100564}
565
YueHaibing29c04372021-05-28 09:41:30 +0800566static ssize_t cpus_show(struct device *dev,
567 struct device_attribute *attr, char *buf)
Mark Rutland48538b52016-09-09 14:08:30 +0100568{
569 struct arm_pmu *armpmu = to_arm_pmu(dev_get_drvdata(dev));
570 return cpumap_print_to_pagebuf(true, buf, &armpmu->supported_cpus);
571}
572
YueHaibing29c04372021-05-28 09:41:30 +0800573static DEVICE_ATTR_RO(cpus);
Mark Rutland48538b52016-09-09 14:08:30 +0100574
575static struct attribute *armpmu_common_attrs[] = {
576 &dev_attr_cpus.attr,
577 NULL,
578};
579
Rikard Falkebornf0c14042021-01-17 22:28:47 +0100580static const struct attribute_group armpmu_common_attr_group = {
Mark Rutland48538b52016-09-09 14:08:30 +0100581 .attrs = armpmu_common_attrs,
582};
583
Mark Rutland84b4be52017-12-12 16:56:06 +0000584static int armpmu_count_irq_users(const int irq)
585{
586 int cpu, count = 0;
587
588 for_each_possible_cpu(cpu) {
589 if (per_cpu(cpu_irq, cpu) == irq)
590 count++;
591 }
592
593 return count;
594}
595
Julien Thierryf76b1302020-09-24 12:07:05 +0100596static const struct pmu_irq_ops *armpmu_find_irq_ops(int irq)
597{
598 const struct pmu_irq_ops *ops = NULL;
599 int cpu;
600
601 for_each_possible_cpu(cpu) {
602 if (per_cpu(cpu_irq, cpu) != irq)
603 continue;
604
605 ops = per_cpu(cpu_irq_ops, cpu);
606 if (ops)
607 break;
608 }
609
610 return ops;
611}
612
Mark Rutland167e6142017-10-09 17:09:05 +0100613void armpmu_free_irq(int irq, int cpu)
Mark Rutland84b4be52017-12-12 16:56:06 +0000614{
615 if (per_cpu(cpu_irq, cpu) == 0)
616 return;
617 if (WARN_ON(irq != per_cpu(cpu_irq, cpu)))
618 return;
619
Julien Thierryf76b1302020-09-24 12:07:05 +0100620 per_cpu(cpu_irq_ops, cpu)->free_pmuirq(irq, cpu, &cpu_armpmu);
Mark Rutland84b4be52017-12-12 16:56:06 +0000621
622 per_cpu(cpu_irq, cpu) = 0;
Julien Thierryf76b1302020-09-24 12:07:05 +0100623 per_cpu(cpu_irq_ops, cpu) = NULL;
Mark Rutland84b4be52017-12-12 16:56:06 +0000624}
625
Mark Rutland167e6142017-10-09 17:09:05 +0100626int armpmu_request_irq(int irq, int cpu)
Mark Rutland0e2663d2017-04-11 09:39:51 +0100627{
628 int err = 0;
Mark Rutland0e2663d2017-04-11 09:39:51 +0100629 const irq_handler_t handler = armpmu_dispatch_irq;
Julien Thierryf76b1302020-09-24 12:07:05 +0100630 const struct pmu_irq_ops *irq_ops;
631
Mark Rutland0e2663d2017-04-11 09:39:51 +0100632 if (!irq)
633 return 0;
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100634
Mark Rutland43fc9a22018-02-05 16:41:59 +0000635 if (!irq_is_percpu_devid(irq)) {
Will Deacona3287c42017-07-25 16:30:34 +0100636 unsigned long irq_flags;
637
638 err = irq_force_affinity(irq, cpumask_of(cpu));
639
640 if (err && num_possible_cpus() > 1) {
641 pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
642 irq, cpu);
643 goto err_out;
644 }
645
Mark Rutlandc0248c92018-02-05 16:41:56 +0000646 irq_flags = IRQF_PERCPU |
Tian Tao3c1f2eb2021-06-02 09:00:41 +0800647 IRQF_NOBALANCING | IRQF_NO_AUTOEN |
Mark Rutlandc0248c92018-02-05 16:41:56 +0000648 IRQF_NO_THREAD;
Will Deacona3287c42017-07-25 16:30:34 +0100649
Julien Thierryd8f62672020-09-24 12:07:06 +0100650 err = request_nmi(irq, handler, irq_flags, "arm-pmu",
Mark Rutland84b4be52017-12-12 16:56:06 +0000651 per_cpu_ptr(&cpu_armpmu, cpu));
Julien Thierryf76b1302020-09-24 12:07:05 +0100652
Julien Thierryd8f62672020-09-24 12:07:06 +0100653 /* If cannot get an NMI, get a normal interrupt */
654 if (err) {
655 err = request_irq(irq, handler, irq_flags, "arm-pmu",
656 per_cpu_ptr(&cpu_armpmu, cpu));
657 irq_ops = &pmuirq_ops;
658 } else {
659 has_nmi = true;
660 irq_ops = &pmunmi_ops;
661 }
Mark Rutland84b4be52017-12-12 16:56:06 +0000662 } else if (armpmu_count_irq_users(irq) == 0) {
Julien Thierryd8f62672020-09-24 12:07:06 +0100663 err = request_percpu_nmi(irq, handler, "arm-pmu", &cpu_armpmu);
Julien Thierryf76b1302020-09-24 12:07:05 +0100664
Julien Thierryd8f62672020-09-24 12:07:06 +0100665 /* If cannot get an NMI, get a normal interrupt */
666 if (err) {
667 err = request_percpu_irq(irq, handler, "arm-pmu",
668 &cpu_armpmu);
669 irq_ops = &percpu_pmuirq_ops;
670 } else {
Junhao Hef265fd12021-05-11 20:27:32 +0800671 has_nmi = true;
Julien Thierryd8f62672020-09-24 12:07:06 +0100672 irq_ops = &percpu_pmunmi_ops;
673 }
Julien Thierryf76b1302020-09-24 12:07:05 +0100674 } else {
675 /* Per cpudevid irq was already requested by another CPU */
676 irq_ops = armpmu_find_irq_ops(irq);
677
678 if (WARN_ON(!irq_ops))
679 err = -EINVAL;
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100680 }
Mark Rutland0e2663d2017-04-11 09:39:51 +0100681
Will Deacona3287c42017-07-25 16:30:34 +0100682 if (err)
683 goto err_out;
Mark Rutland0e2663d2017-04-11 09:39:51 +0100684
Mark Rutland84b4be52017-12-12 16:56:06 +0000685 per_cpu(cpu_irq, cpu) = irq;
Julien Thierryf76b1302020-09-24 12:07:05 +0100686 per_cpu(cpu_irq_ops, cpu) = irq_ops;
Mark Rutland0e2663d2017-04-11 09:39:51 +0100687 return 0;
Will Deacona3287c42017-07-25 16:30:34 +0100688
689err_out:
690 pr_err("unable to request IRQ%d for ARM PMU counters\n", irq);
691 return err;
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100692}
693
Mark Rutlandc09adab2017-03-10 10:46:15 +0000694static int armpmu_get_cpu_irq(struct arm_pmu *pmu, int cpu)
695{
696 struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
697 return per_cpu(hw_events->irq, cpu);
698}
699
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100700/*
701 * PMU hardware loses all context when a CPU goes offline.
702 * When a CPU is hotplugged back in, since some hardware registers are
703 * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
704 * junk values out of them.
705 */
Sebastian Andrzej Siewior6e103c02016-08-17 19:14:20 +0200706static int arm_perf_starting_cpu(unsigned int cpu, struct hlist_node *node)
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100707{
Sebastian Andrzej Siewior6e103c02016-08-17 19:14:20 +0200708 struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
Mark Rutlandc09adab2017-03-10 10:46:15 +0000709 int irq;
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100710
Sebastian Andrzej Siewior6e103c02016-08-17 19:14:20 +0200711 if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
712 return 0;
713 if (pmu->reset)
714 pmu->reset(pmu);
Mark Rutlandc09adab2017-03-10 10:46:15 +0000715
Mark Rutland84b4be52017-12-12 16:56:06 +0000716 per_cpu(cpu_armpmu, cpu) = pmu;
717
Mark Rutlandc09adab2017-03-10 10:46:15 +0000718 irq = armpmu_get_cpu_irq(pmu, cpu);
Julien Thierryf76b1302020-09-24 12:07:05 +0100719 if (irq)
720 per_cpu(cpu_irq_ops, cpu)->enable_pmuirq(irq);
Mark Rutlandc09adab2017-03-10 10:46:15 +0000721
722 return 0;
723}
724
725static int arm_perf_teardown_cpu(unsigned int cpu, struct hlist_node *node)
726{
727 struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
728 int irq;
729
730 if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
731 return 0;
732
733 irq = armpmu_get_cpu_irq(pmu, cpu);
Julien Thierryf76b1302020-09-24 12:07:05 +0100734 if (irq)
735 per_cpu(cpu_irq_ops, cpu)->disable_pmuirq(irq);
Mark Rutlandc09adab2017-03-10 10:46:15 +0000736
Mark Rutland84b4be52017-12-12 16:56:06 +0000737 per_cpu(cpu_armpmu, cpu) = NULL;
738
Thomas Gleixner7d88eb62016-07-13 17:16:36 +0000739 return 0;
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100740}
741
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +0000742#ifdef CONFIG_CPU_PM
743static void cpu_pm_pmu_setup(struct arm_pmu *armpmu, unsigned long cmd)
744{
745 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
746 struct perf_event *event;
747 int idx;
748
749 for (idx = 0; idx < armpmu->num_events; idx++) {
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +0000750 event = hw_events->events[idx];
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100751 if (!event)
752 continue;
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +0000753
754 switch (cmd) {
755 case CPU_PM_ENTER:
756 /*
757 * Stop and update the counter
758 */
759 armpmu_stop(event, PERF_EF_UPDATE);
760 break;
761 case CPU_PM_EXIT:
762 case CPU_PM_ENTER_FAILED:
Lorenzo Pieralisicbcc72e2016-04-21 10:24:34 +0100763 /*
764 * Restore and enable the counter.
765 * armpmu_start() indirectly calls
766 *
767 * perf_event_update_userpage()
768 *
769 * that requires RCU read locking to be functional,
770 * wrap the call within RCU_NONIDLE to make the
771 * RCU subsystem aware this cpu is not idle from
772 * an RCU perspective for the armpmu_start() call
773 * duration.
774 */
775 RCU_NONIDLE(armpmu_start(event, PERF_EF_RELOAD));
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +0000776 break;
777 default:
778 break;
779 }
780 }
781}
782
783static int cpu_pm_pmu_notify(struct notifier_block *b, unsigned long cmd,
784 void *v)
785{
786 struct arm_pmu *armpmu = container_of(b, struct arm_pmu, cpu_pm_nb);
787 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
788 int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
789
790 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
791 return NOTIFY_DONE;
792
793 /*
794 * Always reset the PMU registers on power-up even if
795 * there are no events running.
796 */
797 if (cmd == CPU_PM_EXIT && armpmu->reset)
798 armpmu->reset(armpmu);
799
800 if (!enabled)
801 return NOTIFY_OK;
802
803 switch (cmd) {
804 case CPU_PM_ENTER:
805 armpmu->stop(armpmu);
806 cpu_pm_pmu_setup(armpmu, cmd);
807 break;
808 case CPU_PM_EXIT:
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +0000809 case CPU_PM_ENTER_FAILED:
Will Deacon0d7fd702019-07-29 11:43:48 +0100810 cpu_pm_pmu_setup(armpmu, cmd);
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +0000811 armpmu->start(armpmu);
812 break;
813 default:
814 return NOTIFY_DONE;
815 }
816
817 return NOTIFY_OK;
818}
819
820static int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu)
821{
822 cpu_pmu->cpu_pm_nb.notifier_call = cpu_pm_pmu_notify;
823 return cpu_pm_register_notifier(&cpu_pmu->cpu_pm_nb);
824}
825
826static void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu)
827{
828 cpu_pm_unregister_notifier(&cpu_pmu->cpu_pm_nb);
829}
830#else
831static inline int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu) { return 0; }
832static inline void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu) { }
833#endif
834
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100835static int cpu_pmu_init(struct arm_pmu *cpu_pmu)
836{
837 int err;
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100838
Mark Rutlandc09adab2017-03-10 10:46:15 +0000839 err = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_STARTING,
840 &cpu_pmu->node);
Sebastian Andrzej Siewior6e103c02016-08-17 19:14:20 +0200841 if (err)
Mark Rutland2681f012017-03-10 10:46:13 +0000842 goto out;
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100843
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +0000844 err = cpu_pm_pmu_register(cpu_pmu);
845 if (err)
846 goto out_unregister;
847
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100848 return 0;
849
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +0000850out_unregister:
Sebastian Andrzej Siewior6e103c02016-08-17 19:14:20 +0200851 cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
852 &cpu_pmu->node);
Mark Rutland2681f012017-03-10 10:46:13 +0000853out:
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100854 return err;
855}
856
857static void cpu_pmu_destroy(struct arm_pmu *cpu_pmu)
858{
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +0000859 cpu_pm_pmu_unregister(cpu_pmu);
Sebastian Andrzej Siewior6e103c02016-08-17 19:14:20 +0200860 cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
861 &cpu_pmu->node);
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100862}
863
Mark Rutland0dc1a182018-02-05 16:41:58 +0000864static struct arm_pmu *__armpmu_alloc(gfp_t flags)
Mark Rutland2681f012017-03-10 10:46:13 +0000865{
866 struct arm_pmu *pmu;
867 int cpu;
868
Mark Rutland0dc1a182018-02-05 16:41:58 +0000869 pmu = kzalloc(sizeof(*pmu), flags);
Junhao Hef265fd12021-05-11 20:27:32 +0800870 if (!pmu)
Mark Rutland2681f012017-03-10 10:46:13 +0000871 goto out;
Mark Rutland2681f012017-03-10 10:46:13 +0000872
Mark Rutland0dc1a182018-02-05 16:41:58 +0000873 pmu->hw_events = alloc_percpu_gfp(struct pmu_hw_events, flags);
Mark Rutland2681f012017-03-10 10:46:13 +0000874 if (!pmu->hw_events) {
875 pr_info("failed to allocate per-cpu PMU data.\n");
876 goto out_free_pmu;
877 }
878
Mark Rutland70cd9082017-04-11 09:39:46 +0100879 pmu->pmu = (struct pmu) {
880 .pmu_enable = armpmu_enable,
881 .pmu_disable = armpmu_disable,
882 .event_init = armpmu_event_init,
883 .add = armpmu_add,
884 .del = armpmu_del,
885 .start = armpmu_start,
886 .stop = armpmu_stop,
887 .read = armpmu_read,
888 .filter_match = armpmu_filter_match,
889 .attr_groups = pmu->attr_groups,
890 /*
891 * This is a CPU PMU potentially in a heterogeneous
892 * configuration (e.g. big.LITTLE). This is not an uncore PMU,
893 * and we have taken ctx sharing into account (e.g. with our
894 * pmu::filter_match callback and pmu::event_init group
895 * validation).
896 */
897 .capabilities = PERF_PMU_CAP_HETEROGENEOUS_CPUS,
898 };
899
900 pmu->attr_groups[ARMPMU_ATTR_GROUP_COMMON] =
901 &armpmu_common_attr_group;
902
Mark Rutland2681f012017-03-10 10:46:13 +0000903 for_each_possible_cpu(cpu) {
904 struct pmu_hw_events *events;
905
906 events = per_cpu_ptr(pmu->hw_events, cpu);
907 raw_spin_lock_init(&events->pmu_lock);
908 events->percpu_pmu = pmu;
909 }
910
911 return pmu;
912
913out_free_pmu:
914 kfree(pmu);
915out:
916 return NULL;
917}
918
Mark Rutland0dc1a182018-02-05 16:41:58 +0000919struct arm_pmu *armpmu_alloc(void)
920{
921 return __armpmu_alloc(GFP_KERNEL);
922}
923
924struct arm_pmu *armpmu_alloc_atomic(void)
925{
926 return __armpmu_alloc(GFP_ATOMIC);
927}
928
929
Mark Rutland18bfcfe2017-04-11 09:39:53 +0100930void armpmu_free(struct arm_pmu *pmu)
Mark Rutland2681f012017-03-10 10:46:13 +0000931{
932 free_percpu(pmu->hw_events);
933 kfree(pmu);
934}
935
Mark Rutland74a2b3e2017-04-11 09:39:47 +0100936int armpmu_register(struct arm_pmu *pmu)
937{
938 int ret;
939
940 ret = cpu_pmu_init(pmu);
941 if (ret)
942 return ret;
943
Andrew Murray1d899c02019-01-10 13:53:27 +0000944 if (!pmu->set_event_filter)
945 pmu->pmu.capabilities |= PERF_PMU_CAP_NO_EXCLUDE;
946
Mark Rutland74a2b3e2017-04-11 09:39:47 +0100947 ret = perf_pmu_register(&pmu->pmu, pmu->name, -1);
948 if (ret)
949 goto out_destroy;
950
Julien Thierryd8f62672020-09-24 12:07:06 +0100951 pr_info("enabled with %s PMU driver, %d counters available%s\n",
952 pmu->name, pmu->num_events,
953 has_nmi ? ", using NMIs" : "");
Mark Rutland74a2b3e2017-04-11 09:39:47 +0100954
Marc Zyngiere840f422021-09-19 14:09:49 +0100955 kvm_host_pmu_init(pmu);
956
Mark Rutland74a2b3e2017-04-11 09:39:47 +0100957 return 0;
958
959out_destroy:
960 cpu_pmu_destroy(pmu);
961 return ret;
962}
963
Sebastian Andrzej Siewior37b502f2016-07-20 09:51:11 +0200964static int arm_pmu_hp_init(void)
965{
966 int ret;
967
Sebastian Andrzej Siewior6e103c02016-08-17 19:14:20 +0200968 ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +0100969 "perf/arm/pmu:starting",
Mark Rutlandc09adab2017-03-10 10:46:15 +0000970 arm_perf_starting_cpu,
971 arm_perf_teardown_cpu);
Sebastian Andrzej Siewior37b502f2016-07-20 09:51:11 +0200972 if (ret)
973 pr_err("CPU hotplug notifier for ARM PMU could not be registered: %d\n",
974 ret);
975 return ret;
976}
977subsys_initcall(arm_pmu_hp_init);