blob: 72118e6f9122b54bf90fa70948f050ba47ba9eea [file] [log] [blame]
Jamie Iles1b8873a2010-02-02 20:25:44 +01001#undef DEBUG
2
3/*
4 * ARM performance counter support.
5 *
6 * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
Will Deacon43eab872010-11-13 19:04:32 +00007 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
Jean PIHET796d1292010-01-26 18:51:05 +01008 *
Jamie Iles1b8873a2010-02-02 20:25:44 +01009 * This code is based on the sparc64 perf event code, which is in turn based
Mark Rutlandd39976f2014-09-29 17:15:32 +010010 * on the x86 code.
Jamie Iles1b8873a2010-02-02 20:25:44 +010011 */
12#define pr_fmt(fmt) "hw perfevents: " fmt
13
Mark Rutland74cf0bc2015-05-26 17:23:39 +010014#include <linux/bitmap.h>
Mark Rutlandcc88116d2015-05-13 17:12:25 +010015#include <linux/cpumask.h>
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +000016#include <linux/cpu_pm.h>
Mark Rutland74cf0bc2015-05-26 17:23:39 +010017#include <linux/export.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010018#include <linux/kernel.h>
Mark Rutlandfa8ad782015-07-06 12:23:53 +010019#include <linux/perf/arm_pmu.h>
Mark Rutland74cf0bc2015-05-26 17:23:39 +010020#include <linux/slab.h>
Ingo Molnare6017572017-02-01 16:36:40 +010021#include <linux/sched/clock.h>
Mark Rutland74cf0bc2015-05-26 17:23:39 +010022#include <linux/spinlock.h>
Stephen Boydbbd64552014-02-07 21:01:19 +000023#include <linux/irq.h>
24#include <linux/irqdesc.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010025
Jamie Iles1b8873a2010-02-02 20:25:44 +010026#include <asm/irq_regs.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010027
Jamie Iles1b8873a2010-02-02 20:25:44 +010028static int
Mark Rutlande1f431b2011-04-28 15:47:10 +010029armpmu_map_cache_event(const unsigned (*cache_map)
30 [PERF_COUNT_HW_CACHE_MAX]
31 [PERF_COUNT_HW_CACHE_OP_MAX]
32 [PERF_COUNT_HW_CACHE_RESULT_MAX],
33 u64 config)
Jamie Iles1b8873a2010-02-02 20:25:44 +010034{
35 unsigned int cache_type, cache_op, cache_result, ret;
36
37 cache_type = (config >> 0) & 0xff;
38 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
39 return -EINVAL;
40
41 cache_op = (config >> 8) & 0xff;
42 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
43 return -EINVAL;
44
45 cache_result = (config >> 16) & 0xff;
46 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
47 return -EINVAL;
48
Will Deacon6c833bb2017-08-08 16:58:33 +010049 if (!cache_map)
50 return -ENOENT;
51
Mark Rutlande1f431b2011-04-28 15:47:10 +010052 ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +010053
54 if (ret == CACHE_OP_UNSUPPORTED)
55 return -ENOENT;
56
57 return ret;
58}
59
60static int
Will Deacon6dbc0022012-07-29 12:36:28 +010061armpmu_map_hw_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
Will Deacon84fee972010-11-13 17:13:56 +000062{
Stephen Boydd9f96632013-08-08 18:41:59 +010063 int mapping;
64
65 if (config >= PERF_COUNT_HW_MAX)
66 return -EINVAL;
67
Will Deacon6c833bb2017-08-08 16:58:33 +010068 if (!event_map)
69 return -ENOENT;
70
Stephen Boydd9f96632013-08-08 18:41:59 +010071 mapping = (*event_map)[config];
Mark Rutlande1f431b2011-04-28 15:47:10 +010072 return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
Will Deacon84fee972010-11-13 17:13:56 +000073}
74
75static int
Mark Rutlande1f431b2011-04-28 15:47:10 +010076armpmu_map_raw_event(u32 raw_event_mask, u64 config)
Will Deacon84fee972010-11-13 17:13:56 +000077{
Mark Rutlande1f431b2011-04-28 15:47:10 +010078 return (int)(config & raw_event_mask);
79}
80
Will Deacon6dbc0022012-07-29 12:36:28 +010081int
82armpmu_map_event(struct perf_event *event,
83 const unsigned (*event_map)[PERF_COUNT_HW_MAX],
84 const unsigned (*cache_map)
85 [PERF_COUNT_HW_CACHE_MAX]
86 [PERF_COUNT_HW_CACHE_OP_MAX]
87 [PERF_COUNT_HW_CACHE_RESULT_MAX],
88 u32 raw_event_mask)
Mark Rutlande1f431b2011-04-28 15:47:10 +010089{
90 u64 config = event->attr.config;
Mark Rutland67b43052012-09-12 10:53:23 +010091 int type = event->attr.type;
Mark Rutlande1f431b2011-04-28 15:47:10 +010092
Mark Rutland67b43052012-09-12 10:53:23 +010093 if (type == event->pmu->type)
94 return armpmu_map_raw_event(raw_event_mask, config);
95
96 switch (type) {
Mark Rutlande1f431b2011-04-28 15:47:10 +010097 case PERF_TYPE_HARDWARE:
Will Deacon6dbc0022012-07-29 12:36:28 +010098 return armpmu_map_hw_event(event_map, config);
Mark Rutlande1f431b2011-04-28 15:47:10 +010099 case PERF_TYPE_HW_CACHE:
100 return armpmu_map_cache_event(cache_map, config);
101 case PERF_TYPE_RAW:
102 return armpmu_map_raw_event(raw_event_mask, config);
103 }
104
105 return -ENOENT;
Will Deacon84fee972010-11-13 17:13:56 +0000106}
107
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100108int armpmu_event_set_period(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100109{
Mark Rutland8a16b342011-04-28 16:27:54 +0100110 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100111 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200112 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100113 s64 period = hwc->sample_period;
114 int ret = 0;
115
116 if (unlikely(left <= -period)) {
117 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200118 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100119 hwc->last_period = period;
120 ret = 1;
121 }
122
123 if (unlikely(left <= 0)) {
124 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200125 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100126 hwc->last_period = period;
127 ret = 1;
128 }
129
Daniel Thompson2d9ed742015-01-05 15:58:54 +0100130 /*
131 * Limit the maximum period to prevent the counter value
132 * from overtaking the one we are about to program. In
133 * effect we are reducing max_period to account for
134 * interrupt latency (and we are being very conservative).
135 */
136 if (left > (armpmu->max_period >> 1))
137 left = armpmu->max_period >> 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100138
Peter Zijlstrae7850592010-05-21 14:43:08 +0200139 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100140
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100141 armpmu->write_counter(event, (u64)(-left) & 0xffffffff);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100142
143 perf_event_update_userpage(event);
144
145 return ret;
146}
147
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100148u64 armpmu_event_update(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100149{
Mark Rutland8a16b342011-04-28 16:27:54 +0100150 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100151 struct hw_perf_event *hwc = &event->hw;
Will Deacona7378232011-03-25 17:12:37 +0100152 u64 delta, prev_raw_count, new_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100153
154again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200155 prev_raw_count = local64_read(&hwc->prev_count);
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100156 new_raw_count = armpmu->read_counter(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100157
Peter Zijlstrae7850592010-05-21 14:43:08 +0200158 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100159 new_raw_count) != prev_raw_count)
160 goto again;
161
Will Deacon57273472012-03-06 17:33:17 +0100162 delta = (new_raw_count - prev_raw_count) & armpmu->max_period;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100163
Peter Zijlstrae7850592010-05-21 14:43:08 +0200164 local64_add(delta, &event->count);
165 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100166
167 return new_raw_count;
168}
169
170static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100171armpmu_read(struct perf_event *event)
172{
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100173 armpmu_event_update(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100174}
175
176static void
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200177armpmu_stop(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100178{
Mark Rutland8a16b342011-04-28 16:27:54 +0100179 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100180 struct hw_perf_event *hwc = &event->hw;
181
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200182 /*
183 * ARM pmu always has to update the counter, so ignore
184 * PERF_EF_UPDATE, see comments in armpmu_start().
185 */
186 if (!(hwc->state & PERF_HES_STOPPED)) {
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100187 armpmu->disable(event);
188 armpmu_event_update(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200189 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
190 }
191}
192
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100193static void armpmu_start(struct perf_event *event, int flags)
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200194{
Mark Rutland8a16b342011-04-28 16:27:54 +0100195 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200196 struct hw_perf_event *hwc = &event->hw;
197
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200198 /*
199 * ARM pmu always has to reprogram the period, so ignore
200 * PERF_EF_RELOAD, see the comment below.
201 */
202 if (flags & PERF_EF_RELOAD)
203 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
204
205 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100206 /*
207 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200208 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100209 * may have been left counting. If we don't do this step then we may
210 * get an interrupt too soon or *way* too late if the overflow has
211 * happened since disabling.
212 */
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100213 armpmu_event_set_period(event);
214 armpmu->enable(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100215}
216
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200217static void
218armpmu_del(struct perf_event *event, int flags)
219{
Mark Rutland8a16b342011-04-28 16:27:54 +0100220 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland11679252014-05-13 19:36:31 +0100221 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200222 struct hw_perf_event *hwc = &event->hw;
223 int idx = hwc->idx;
224
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200225 armpmu_stop(event, PERF_EF_UPDATE);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100226 hw_events->events[idx] = NULL;
227 clear_bit(idx, hw_events->used_mask);
Stephen Boydeab443e2014-02-07 21:01:22 +0000228 if (armpmu->clear_event_idx)
229 armpmu->clear_event_idx(hw_events, event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200230
231 perf_event_update_userpage(event);
232}
233
Jamie Iles1b8873a2010-02-02 20:25:44 +0100234static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200235armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100236{
Mark Rutland8a16b342011-04-28 16:27:54 +0100237 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland11679252014-05-13 19:36:31 +0100238 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100239 struct hw_perf_event *hwc = &event->hw;
240 int idx;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100241
Mark Rutlandcc88116d2015-05-13 17:12:25 +0100242 /* An event following a process won't be stopped earlier */
243 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
244 return -ENOENT;
245
Jamie Iles1b8873a2010-02-02 20:25:44 +0100246 /* If we don't have a space for the counter then finish early. */
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100247 idx = armpmu->get_event_idx(hw_events, event);
Mark Rutlanda9e469d2017-04-11 09:39:44 +0100248 if (idx < 0)
249 return idx;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100250
251 /*
252 * If there is an event in the counter we are going to use then make
253 * sure it is disabled.
254 */
255 event->hw.idx = idx;
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100256 armpmu->disable(event);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100257 hw_events->events[idx] = event;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100258
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200259 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
260 if (flags & PERF_EF_START)
261 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100262
263 /* Propagate our changes to the userspace mapping. */
264 perf_event_update_userpage(event);
265
Mark Rutlanda9e469d2017-04-11 09:39:44 +0100266 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100267}
268
Jamie Iles1b8873a2010-02-02 20:25:44 +0100269static int
Suzuki K. Poulosee4298172015-03-17 18:14:58 +0000270validate_event(struct pmu *pmu, struct pmu_hw_events *hw_events,
271 struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100272{
Suzuki K. Poulosee4298172015-03-17 18:14:58 +0000273 struct arm_pmu *armpmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100274
Will Deaconc95eb312013-08-07 23:39:41 +0100275 if (is_software_event(event))
276 return 1;
277
Suzuki K. Poulosee4298172015-03-17 18:14:58 +0000278 /*
279 * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
280 * core perf code won't check that the pmu->ctx == leader->ctx
281 * until after pmu->event_init(event).
282 */
283 if (event->pmu != pmu)
284 return 0;
285
Will Deacon2dfcb802013-10-09 13:51:29 +0100286 if (event->state < PERF_EVENT_STATE_OFF)
Will Deaconcb2d8b32013-04-12 19:04:19 +0100287 return 1;
288
289 if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
Will Deacon65b47112010-09-02 09:32:08 +0100290 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100291
Suzuki K. Poulosee4298172015-03-17 18:14:58 +0000292 armpmu = to_arm_pmu(event->pmu);
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100293 return armpmu->get_event_idx(hw_events, event) >= 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100294}
295
296static int
297validate_group(struct perf_event *event)
298{
299 struct perf_event *sibling, *leader = event->group_leader;
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100300 struct pmu_hw_events fake_pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100301
Will Deaconbce34d12011-11-17 15:05:14 +0000302 /*
303 * Initialise the fake PMU. We only need to populate the
304 * used_mask for the purposes of validation.
305 */
Mark Rutlanda4560842014-05-13 19:08:19 +0100306 memset(&fake_pmu.used_mask, 0, sizeof(fake_pmu.used_mask));
Jamie Iles1b8873a2010-02-02 20:25:44 +0100307
Suzuki K. Poulosee4298172015-03-17 18:14:58 +0000308 if (!validate_event(event->pmu, &fake_pmu, leader))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100309 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100310
311 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
Suzuki K. Poulosee4298172015-03-17 18:14:58 +0000312 if (!validate_event(event->pmu, &fake_pmu, sibling))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100313 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100314 }
315
Suzuki K. Poulosee4298172015-03-17 18:14:58 +0000316 if (!validate_event(event->pmu, &fake_pmu, event))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100317 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100318
319 return 0;
320}
321
Sudeep KarkadaNagesha051f1b12012-07-31 10:34:25 +0100322static irqreturn_t armpmu_dispatch_irq(int irq, void *dev)
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530323{
Stephen Boydbbd64552014-02-07 21:01:19 +0000324 struct arm_pmu *armpmu;
Will Deacon5f5092e2014-02-11 18:08:41 +0000325 int ret;
326 u64 start_clock, finish_clock;
Stephen Boydbbd64552014-02-07 21:01:19 +0000327
Mark Rutland5ebd9202014-05-13 19:46:10 +0100328 /*
329 * we request the IRQ with a (possibly percpu) struct arm_pmu**, but
330 * the handlers expect a struct arm_pmu*. The percpu_irq framework will
331 * do any necessary shifting, we just need to perform the first
332 * dereference.
333 */
334 armpmu = *(void **)dev;
Mark Rutland76541372017-04-11 09:39:49 +0100335
Will Deacon5f5092e2014-02-11 18:08:41 +0000336 start_clock = sched_clock();
Mark Rutlandc0248c92018-02-05 16:41:56 +0000337 ret = armpmu->handle_irq(irq, armpmu);
Will Deacon5f5092e2014-02-11 18:08:41 +0000338 finish_clock = sched_clock();
339
340 perf_sample_event_took(finish_clock - start_clock);
341 return ret;
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530342}
343
Jamie Iles1b8873a2010-02-02 20:25:44 +0100344static int
Will Deacon05d22fd2011-07-19 11:57:30 +0100345event_requires_mode_exclusion(struct perf_event_attr *attr)
346{
347 return attr->exclude_idle || attr->exclude_user ||
348 attr->exclude_kernel || attr->exclude_hv;
349}
350
351static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100352__hw_perf_event_init(struct perf_event *event)
353{
Mark Rutland8a16b342011-04-28 16:27:54 +0100354 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100355 struct hw_perf_event *hwc = &event->hw;
Mark Rutland9dcbf462013-01-18 16:10:06 +0000356 int mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100357
Mark Rutlande1f431b2011-04-28 15:47:10 +0100358 mapping = armpmu->map_event(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100359
360 if (mapping < 0) {
361 pr_debug("event %x:%llx not supported\n", event->attr.type,
362 event->attr.config);
363 return mapping;
364 }
365
366 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100367 * We don't assign an index until we actually place the event onto
368 * hardware. Use -1 to signify that we haven't decided where to put it
369 * yet. For SMP systems, each core has it's own PMU so we can't do any
370 * clever allocation or constraints checking at this point.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100371 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100372 hwc->idx = -1;
373 hwc->config_base = 0;
374 hwc->config = 0;
375 hwc->event_base = 0;
376
377 /*
378 * Check whether we need to exclude the counter from certain modes.
379 */
380 if ((!armpmu->set_event_filter ||
381 armpmu->set_event_filter(hwc, &event->attr)) &&
382 event_requires_mode_exclusion(&event->attr)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100383 pr_debug("ARM performance counters do not support "
384 "mode exclusion\n");
Will Deaconfdeb8e32012-07-04 18:15:42 +0100385 return -EOPNOTSUPP;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100386 }
387
388 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100389 * Store the event encoding into the config_base field.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100390 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100391 hwc->config_base |= (unsigned long)mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100392
Vince Weaveredcb4d32014-05-16 17:15:49 -0400393 if (!is_sampling_event(event)) {
Will Deacon57273472012-03-06 17:33:17 +0100394 /*
395 * For non-sampling runs, limit the sample_period to half
396 * of the counter width. That way, the new counter value
397 * is far less likely to overtake the previous one unless
398 * you have some serious IRQ latency issues.
399 */
400 hwc->sample_period = armpmu->max_period >> 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100401 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200402 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100403 }
404
Jamie Iles1b8873a2010-02-02 20:25:44 +0100405 if (event->group_leader != event) {
Chen Gange595ede2013-02-28 17:51:29 +0100406 if (validate_group(event) != 0)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100407 return -EINVAL;
408 }
409
Mark Rutland9dcbf462013-01-18 16:10:06 +0000410 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100411}
412
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200413static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100414{
Mark Rutland8a16b342011-04-28 16:27:54 +0100415 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100416
Mark Rutlandcc88116d2015-05-13 17:12:25 +0100417 /*
418 * Reject CPU-affine events for CPUs that are of a different class to
419 * that which this PMU handles. Process-following events (where
420 * event->cpu == -1) can be migrated between CPUs, and thus we have to
421 * reject them later (in armpmu_add) if they're scheduled on a
422 * different class of CPU.
423 */
424 if (event->cpu != -1 &&
425 !cpumask_test_cpu(event->cpu, &armpmu->supported_cpus))
426 return -ENOENT;
427
Stephane Eranian2481c5f2012-02-09 23:20:59 +0100428 /* does not support taken branch sampling */
429 if (has_branch_stack(event))
430 return -EOPNOTSUPP;
431
Mark Rutlande1f431b2011-04-28 15:47:10 +0100432 if (armpmu->map_event(event) == -ENOENT)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200433 return -ENOENT;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200434
Mark Rutlandc09adab2017-03-10 10:46:15 +0000435 return __hw_perf_event_init(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100436}
437
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200438static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100439{
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100440 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Mark Rutland11679252014-05-13 19:36:31 +0100441 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
Mark Rutland7325eae2011-08-23 11:59:49 +0100442 int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100443
Mark Rutlandcc88116d2015-05-13 17:12:25 +0100444 /* For task-bound events we may be called on other CPUs */
445 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
446 return;
447
Will Deaconf4f38432011-07-01 14:38:12 +0100448 if (enabled)
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100449 armpmu->start(armpmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100450}
451
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200452static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100453{
Mark Rutland8a16b342011-04-28 16:27:54 +0100454 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Mark Rutlandcc88116d2015-05-13 17:12:25 +0100455
456 /* For task-bound events we may be called on other CPUs */
457 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
458 return;
459
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100460 armpmu->stop(armpmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100461}
462
Mark Rutlandc904e322015-05-13 17:12:26 +0100463/*
464 * In heterogeneous systems, events are specific to a particular
465 * microarchitecture, and aren't suitable for another. Thus, only match CPUs of
466 * the same microarchitecture.
467 */
468static int armpmu_filter_match(struct perf_event *event)
469{
470 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
471 unsigned int cpu = smp_processor_id();
472 return cpumask_test_cpu(cpu, &armpmu->supported_cpus);
473}
474
Mark Rutland48538b52016-09-09 14:08:30 +0100475static ssize_t armpmu_cpumask_show(struct device *dev,
476 struct device_attribute *attr, char *buf)
477{
478 struct arm_pmu *armpmu = to_arm_pmu(dev_get_drvdata(dev));
479 return cpumap_print_to_pagebuf(true, buf, &armpmu->supported_cpus);
480}
481
482static DEVICE_ATTR(cpus, S_IRUGO, armpmu_cpumask_show, NULL);
483
484static struct attribute *armpmu_common_attrs[] = {
485 &dev_attr_cpus.attr,
486 NULL,
487};
488
489static struct attribute_group armpmu_common_attr_group = {
490 .attrs = armpmu_common_attrs,
491};
492
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100493/* Set at runtime when we know what CPU type we are. */
494static struct arm_pmu *__oprofile_cpu_pmu;
495
496/*
497 * Despite the names, these two functions are CPU-specific and are used
498 * by the OProfile/perf code.
499 */
500const char *perf_pmu_name(void)
501{
502 if (!__oprofile_cpu_pmu)
503 return NULL;
504
505 return __oprofile_cpu_pmu->name;
506}
507EXPORT_SYMBOL_GPL(perf_pmu_name);
508
509int perf_num_counters(void)
510{
511 int max_events = 0;
512
513 if (__oprofile_cpu_pmu != NULL)
514 max_events = __oprofile_cpu_pmu->num_events;
515
516 return max_events;
517}
518EXPORT_SYMBOL_GPL(perf_num_counters);
519
Mark Rutland45736a72017-04-11 09:39:55 +0100520void armpmu_free_irq(struct arm_pmu *armpmu, int cpu)
Mark Rutland0e2663d2017-04-11 09:39:51 +0100521{
522 struct pmu_hw_events __percpu *hw_events = armpmu->hw_events;
523 int irq = per_cpu(hw_events->irq, cpu);
524
525 if (!cpumask_test_and_clear_cpu(cpu, &armpmu->active_irqs))
526 return;
527
Julien Thierry611479c2017-10-13 12:26:45 +0100528 if (irq_is_percpu_devid(irq)) {
Mark Rutland0e2663d2017-04-11 09:39:51 +0100529 free_percpu_irq(irq, &hw_events->percpu_pmu);
530 cpumask_clear(&armpmu->active_irqs);
531 return;
532 }
533
534 free_irq(irq, per_cpu_ptr(&hw_events->percpu_pmu, cpu));
535}
536
Mark Rutland45736a72017-04-11 09:39:55 +0100537int armpmu_request_irq(struct arm_pmu *armpmu, int cpu)
Mark Rutland0e2663d2017-04-11 09:39:51 +0100538{
539 int err = 0;
Mark Rutland3cf6111022017-04-11 09:39:50 +0100540 struct pmu_hw_events __percpu *hw_events = armpmu->hw_events;
Mark Rutland0e2663d2017-04-11 09:39:51 +0100541 const irq_handler_t handler = armpmu_dispatch_irq;
542 int irq = per_cpu(hw_events->irq, cpu);
543 if (!irq)
544 return 0;
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100545
Mark Rutland43fc9a22018-02-05 16:41:59 +0000546 if (!irq_is_percpu_devid(irq)) {
Will Deacona3287c42017-07-25 16:30:34 +0100547 unsigned long irq_flags;
548
549 err = irq_force_affinity(irq, cpumask_of(cpu));
550
551 if (err && num_possible_cpus() > 1) {
552 pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
553 irq, cpu);
554 goto err_out;
555 }
556
Mark Rutlandc0248c92018-02-05 16:41:56 +0000557 irq_flags = IRQF_PERCPU |
558 IRQF_NOBALANCING |
559 IRQF_NO_THREAD;
Will Deacona3287c42017-07-25 16:30:34 +0100560
Mark Rutland6de3f792018-02-05 16:42:00 +0000561 irq_set_status_flags(irq, IRQ_NOAUTOEN);
Will Deacona3287c42017-07-25 16:30:34 +0100562 err = request_irq(irq, handler, irq_flags, "arm-pmu",
Mark Rutland0e2663d2017-04-11 09:39:51 +0100563 per_cpu_ptr(&hw_events->percpu_pmu, cpu));
Mark Rutland43fc9a22018-02-05 16:41:59 +0000564 } else if (cpumask_empty(&armpmu->active_irqs)) {
565 err = request_percpu_irq(irq, handler, "arm-pmu",
566 &hw_events->percpu_pmu);
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100567 }
Mark Rutland0e2663d2017-04-11 09:39:51 +0100568
Will Deacona3287c42017-07-25 16:30:34 +0100569 if (err)
570 goto err_out;
Mark Rutland0e2663d2017-04-11 09:39:51 +0100571
572 cpumask_set_cpu(cpu, &armpmu->active_irqs);
Mark Rutland0e2663d2017-04-11 09:39:51 +0100573 return 0;
Will Deacona3287c42017-07-25 16:30:34 +0100574
575err_out:
576 pr_err("unable to request IRQ%d for ARM PMU counters\n", irq);
577 return err;
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100578}
579
Mark Rutlandc09adab2017-03-10 10:46:15 +0000580static int armpmu_get_cpu_irq(struct arm_pmu *pmu, int cpu)
581{
582 struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
583 return per_cpu(hw_events->irq, cpu);
584}
585
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100586/*
587 * PMU hardware loses all context when a CPU goes offline.
588 * When a CPU is hotplugged back in, since some hardware registers are
589 * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
590 * junk values out of them.
591 */
Sebastian Andrzej Siewior6e103c02016-08-17 19:14:20 +0200592static int arm_perf_starting_cpu(unsigned int cpu, struct hlist_node *node)
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100593{
Sebastian Andrzej Siewior6e103c02016-08-17 19:14:20 +0200594 struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
Mark Rutlandc09adab2017-03-10 10:46:15 +0000595 int irq;
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100596
Sebastian Andrzej Siewior6e103c02016-08-17 19:14:20 +0200597 if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
598 return 0;
599 if (pmu->reset)
600 pmu->reset(pmu);
Mark Rutlandc09adab2017-03-10 10:46:15 +0000601
602 irq = armpmu_get_cpu_irq(pmu, cpu);
603 if (irq) {
Mark Rutland6de3f792018-02-05 16:42:00 +0000604 if (irq_is_percpu_devid(irq))
Mark Rutlandc09adab2017-03-10 10:46:15 +0000605 enable_percpu_irq(irq, IRQ_TYPE_NONE);
Mark Rutland6de3f792018-02-05 16:42:00 +0000606 else
607 enable_irq(irq);
Mark Rutlandc09adab2017-03-10 10:46:15 +0000608 }
609
610 return 0;
611}
612
613static int arm_perf_teardown_cpu(unsigned int cpu, struct hlist_node *node)
614{
615 struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
616 int irq;
617
618 if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
619 return 0;
620
621 irq = armpmu_get_cpu_irq(pmu, cpu);
Mark Rutland6de3f792018-02-05 16:42:00 +0000622 if (irq) {
623 if (irq_is_percpu_devid(irq))
624 disable_percpu_irq(irq);
625 else
626 disable_irq(irq);
627 }
Mark Rutlandc09adab2017-03-10 10:46:15 +0000628
Thomas Gleixner7d88eb62016-07-13 17:16:36 +0000629 return 0;
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100630}
631
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +0000632#ifdef CONFIG_CPU_PM
633static void cpu_pm_pmu_setup(struct arm_pmu *armpmu, unsigned long cmd)
634{
635 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
636 struct perf_event *event;
637 int idx;
638
639 for (idx = 0; idx < armpmu->num_events; idx++) {
640 /*
641 * If the counter is not used skip it, there is no
642 * need of stopping/restarting it.
643 */
644 if (!test_bit(idx, hw_events->used_mask))
645 continue;
646
647 event = hw_events->events[idx];
648
649 switch (cmd) {
650 case CPU_PM_ENTER:
651 /*
652 * Stop and update the counter
653 */
654 armpmu_stop(event, PERF_EF_UPDATE);
655 break;
656 case CPU_PM_EXIT:
657 case CPU_PM_ENTER_FAILED:
Lorenzo Pieralisicbcc72e2016-04-21 10:24:34 +0100658 /*
659 * Restore and enable the counter.
660 * armpmu_start() indirectly calls
661 *
662 * perf_event_update_userpage()
663 *
664 * that requires RCU read locking to be functional,
665 * wrap the call within RCU_NONIDLE to make the
666 * RCU subsystem aware this cpu is not idle from
667 * an RCU perspective for the armpmu_start() call
668 * duration.
669 */
670 RCU_NONIDLE(armpmu_start(event, PERF_EF_RELOAD));
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +0000671 break;
672 default:
673 break;
674 }
675 }
676}
677
678static int cpu_pm_pmu_notify(struct notifier_block *b, unsigned long cmd,
679 void *v)
680{
681 struct arm_pmu *armpmu = container_of(b, struct arm_pmu, cpu_pm_nb);
682 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
683 int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
684
685 if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
686 return NOTIFY_DONE;
687
688 /*
689 * Always reset the PMU registers on power-up even if
690 * there are no events running.
691 */
692 if (cmd == CPU_PM_EXIT && armpmu->reset)
693 armpmu->reset(armpmu);
694
695 if (!enabled)
696 return NOTIFY_OK;
697
698 switch (cmd) {
699 case CPU_PM_ENTER:
700 armpmu->stop(armpmu);
701 cpu_pm_pmu_setup(armpmu, cmd);
702 break;
703 case CPU_PM_EXIT:
704 cpu_pm_pmu_setup(armpmu, cmd);
705 case CPU_PM_ENTER_FAILED:
706 armpmu->start(armpmu);
707 break;
708 default:
709 return NOTIFY_DONE;
710 }
711
712 return NOTIFY_OK;
713}
714
715static int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu)
716{
717 cpu_pmu->cpu_pm_nb.notifier_call = cpu_pm_pmu_notify;
718 return cpu_pm_register_notifier(&cpu_pmu->cpu_pm_nb);
719}
720
721static void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu)
722{
723 cpu_pm_unregister_notifier(&cpu_pmu->cpu_pm_nb);
724}
725#else
726static inline int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu) { return 0; }
727static inline void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu) { }
728#endif
729
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100730static int cpu_pmu_init(struct arm_pmu *cpu_pmu)
731{
732 int err;
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100733
Mark Rutlandc09adab2017-03-10 10:46:15 +0000734 err = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_STARTING,
735 &cpu_pmu->node);
Sebastian Andrzej Siewior6e103c02016-08-17 19:14:20 +0200736 if (err)
Mark Rutland2681f012017-03-10 10:46:13 +0000737 goto out;
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100738
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +0000739 err = cpu_pm_pmu_register(cpu_pmu);
740 if (err)
741 goto out_unregister;
742
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100743 return 0;
744
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +0000745out_unregister:
Sebastian Andrzej Siewior6e103c02016-08-17 19:14:20 +0200746 cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
747 &cpu_pmu->node);
Mark Rutland2681f012017-03-10 10:46:13 +0000748out:
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100749 return err;
750}
751
752static void cpu_pmu_destroy(struct arm_pmu *cpu_pmu)
753{
Lorenzo Pieralisida4e4f12016-02-23 18:22:39 +0000754 cpu_pm_pmu_unregister(cpu_pmu);
Sebastian Andrzej Siewior6e103c02016-08-17 19:14:20 +0200755 cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
756 &cpu_pmu->node);
Mark Rutland74cf0bc2015-05-26 17:23:39 +0100757}
758
Mark Rutland0dc1a182018-02-05 16:41:58 +0000759static struct arm_pmu *__armpmu_alloc(gfp_t flags)
Mark Rutland2681f012017-03-10 10:46:13 +0000760{
761 struct arm_pmu *pmu;
762 int cpu;
763
Mark Rutland0dc1a182018-02-05 16:41:58 +0000764 pmu = kzalloc(sizeof(*pmu), flags);
Mark Rutland2681f012017-03-10 10:46:13 +0000765 if (!pmu) {
766 pr_info("failed to allocate PMU device!\n");
767 goto out;
768 }
769
Mark Rutland0dc1a182018-02-05 16:41:58 +0000770 pmu->hw_events = alloc_percpu_gfp(struct pmu_hw_events, flags);
Mark Rutland2681f012017-03-10 10:46:13 +0000771 if (!pmu->hw_events) {
772 pr_info("failed to allocate per-cpu PMU data.\n");
773 goto out_free_pmu;
774 }
775
Mark Rutland70cd9082017-04-11 09:39:46 +0100776 pmu->pmu = (struct pmu) {
777 .pmu_enable = armpmu_enable,
778 .pmu_disable = armpmu_disable,
779 .event_init = armpmu_event_init,
780 .add = armpmu_add,
781 .del = armpmu_del,
782 .start = armpmu_start,
783 .stop = armpmu_stop,
784 .read = armpmu_read,
785 .filter_match = armpmu_filter_match,
786 .attr_groups = pmu->attr_groups,
787 /*
788 * This is a CPU PMU potentially in a heterogeneous
789 * configuration (e.g. big.LITTLE). This is not an uncore PMU,
790 * and we have taken ctx sharing into account (e.g. with our
791 * pmu::filter_match callback and pmu::event_init group
792 * validation).
793 */
794 .capabilities = PERF_PMU_CAP_HETEROGENEOUS_CPUS,
795 };
796
797 pmu->attr_groups[ARMPMU_ATTR_GROUP_COMMON] =
798 &armpmu_common_attr_group;
799
Mark Rutland2681f012017-03-10 10:46:13 +0000800 for_each_possible_cpu(cpu) {
801 struct pmu_hw_events *events;
802
803 events = per_cpu_ptr(pmu->hw_events, cpu);
804 raw_spin_lock_init(&events->pmu_lock);
805 events->percpu_pmu = pmu;
806 }
807
808 return pmu;
809
810out_free_pmu:
811 kfree(pmu);
812out:
813 return NULL;
814}
815
Mark Rutland0dc1a182018-02-05 16:41:58 +0000816struct arm_pmu *armpmu_alloc(void)
817{
818 return __armpmu_alloc(GFP_KERNEL);
819}
820
821struct arm_pmu *armpmu_alloc_atomic(void)
822{
823 return __armpmu_alloc(GFP_ATOMIC);
824}
825
826
Mark Rutland18bfcfe2017-04-11 09:39:53 +0100827void armpmu_free(struct arm_pmu *pmu)
Mark Rutland2681f012017-03-10 10:46:13 +0000828{
829 free_percpu(pmu->hw_events);
830 kfree(pmu);
831}
832
Mark Rutland74a2b3e2017-04-11 09:39:47 +0100833int armpmu_register(struct arm_pmu *pmu)
834{
835 int ret;
836
837 ret = cpu_pmu_init(pmu);
838 if (ret)
839 return ret;
840
841 ret = perf_pmu_register(&pmu->pmu, pmu->name, -1);
842 if (ret)
843 goto out_destroy;
844
845 if (!__oprofile_cpu_pmu)
846 __oprofile_cpu_pmu = pmu;
847
848 pr_info("enabled with %s PMU driver, %d counters available\n",
849 pmu->name, pmu->num_events);
850
851 return 0;
852
853out_destroy:
854 cpu_pmu_destroy(pmu);
855 return ret;
856}
857
Sebastian Andrzej Siewior37b502f2016-07-20 09:51:11 +0200858static int arm_pmu_hp_init(void)
859{
860 int ret;
861
Sebastian Andrzej Siewior6e103c02016-08-17 19:14:20 +0200862 ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +0100863 "perf/arm/pmu:starting",
Mark Rutlandc09adab2017-03-10 10:46:15 +0000864 arm_perf_starting_cpu,
865 arm_perf_teardown_cpu);
Sebastian Andrzej Siewior37b502f2016-07-20 09:51:11 +0200866 if (ret)
867 pr_err("CPU hotplug notifier for ARM PMU could not be registered: %d\n",
868 ret);
869 return ret;
870}
871subsys_initcall(arm_pmu_hp_init);