blob: 557e128e4df08ce711d4bab89952eae0f1d6d7ea [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
Jamie Iles1b8873a2010-02-02 20:25:44 +010014#include <linux/kernel.h>
Will Deacon49c006b2010-04-29 17:13:24 +010015#include <linux/platform_device.h>
Jon Hunter7be29582012-05-31 13:05:20 -050016#include <linux/pm_runtime.h>
Stephen Boydbbd64552014-02-07 21:01:19 +000017#include <linux/irq.h>
18#include <linux/irqdesc.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010019
Jamie Iles1b8873a2010-02-02 20:25:44 +010020#include <asm/irq_regs.h>
21#include <asm/pmu.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010022
Jamie Iles1b8873a2010-02-02 20:25:44 +010023static int
Mark Rutlande1f431b2011-04-28 15:47:10 +010024armpmu_map_cache_event(const unsigned (*cache_map)
25 [PERF_COUNT_HW_CACHE_MAX]
26 [PERF_COUNT_HW_CACHE_OP_MAX]
27 [PERF_COUNT_HW_CACHE_RESULT_MAX],
28 u64 config)
Jamie Iles1b8873a2010-02-02 20:25:44 +010029{
30 unsigned int cache_type, cache_op, cache_result, ret;
31
32 cache_type = (config >> 0) & 0xff;
33 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
34 return -EINVAL;
35
36 cache_op = (config >> 8) & 0xff;
37 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
38 return -EINVAL;
39
40 cache_result = (config >> 16) & 0xff;
41 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
42 return -EINVAL;
43
Mark Rutlande1f431b2011-04-28 15:47:10 +010044 ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +010045
46 if (ret == CACHE_OP_UNSUPPORTED)
47 return -ENOENT;
48
49 return ret;
50}
51
52static int
Will Deacon6dbc0022012-07-29 12:36:28 +010053armpmu_map_hw_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
Will Deacon84fee972010-11-13 17:13:56 +000054{
Stephen Boydd9f96632013-08-08 18:41:59 +010055 int mapping;
56
57 if (config >= PERF_COUNT_HW_MAX)
58 return -EINVAL;
59
60 mapping = (*event_map)[config];
Mark Rutlande1f431b2011-04-28 15:47:10 +010061 return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
Will Deacon84fee972010-11-13 17:13:56 +000062}
63
64static int
Mark Rutlande1f431b2011-04-28 15:47:10 +010065armpmu_map_raw_event(u32 raw_event_mask, u64 config)
Will Deacon84fee972010-11-13 17:13:56 +000066{
Mark Rutlande1f431b2011-04-28 15:47:10 +010067 return (int)(config & raw_event_mask);
68}
69
Will Deacon6dbc0022012-07-29 12:36:28 +010070int
71armpmu_map_event(struct perf_event *event,
72 const unsigned (*event_map)[PERF_COUNT_HW_MAX],
73 const unsigned (*cache_map)
74 [PERF_COUNT_HW_CACHE_MAX]
75 [PERF_COUNT_HW_CACHE_OP_MAX]
76 [PERF_COUNT_HW_CACHE_RESULT_MAX],
77 u32 raw_event_mask)
Mark Rutlande1f431b2011-04-28 15:47:10 +010078{
79 u64 config = event->attr.config;
Mark Rutland67b43052012-09-12 10:53:23 +010080 int type = event->attr.type;
Mark Rutlande1f431b2011-04-28 15:47:10 +010081
Mark Rutland67b43052012-09-12 10:53:23 +010082 if (type == event->pmu->type)
83 return armpmu_map_raw_event(raw_event_mask, config);
84
85 switch (type) {
Mark Rutlande1f431b2011-04-28 15:47:10 +010086 case PERF_TYPE_HARDWARE:
Will Deacon6dbc0022012-07-29 12:36:28 +010087 return armpmu_map_hw_event(event_map, config);
Mark Rutlande1f431b2011-04-28 15:47:10 +010088 case PERF_TYPE_HW_CACHE:
89 return armpmu_map_cache_event(cache_map, config);
90 case PERF_TYPE_RAW:
91 return armpmu_map_raw_event(raw_event_mask, config);
92 }
93
94 return -ENOENT;
Will Deacon84fee972010-11-13 17:13:56 +000095}
96
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +010097int armpmu_event_set_period(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +010098{
Mark Rutland8a16b342011-04-28 16:27:54 +010099 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100100 struct hw_perf_event *hwc = &event->hw;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200101 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100102 s64 period = hwc->sample_period;
103 int ret = 0;
104
105 if (unlikely(left <= -period)) {
106 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200107 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100108 hwc->last_period = period;
109 ret = 1;
110 }
111
112 if (unlikely(left <= 0)) {
113 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200114 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100115 hwc->last_period = period;
116 ret = 1;
117 }
118
Daniel Thompson2d9ed742015-01-05 15:58:54 +0100119 /*
120 * Limit the maximum period to prevent the counter value
121 * from overtaking the one we are about to program. In
122 * effect we are reducing max_period to account for
123 * interrupt latency (and we are being very conservative).
124 */
125 if (left > (armpmu->max_period >> 1))
126 left = armpmu->max_period >> 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100127
Peter Zijlstrae7850592010-05-21 14:43:08 +0200128 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100129
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100130 armpmu->write_counter(event, (u64)(-left) & 0xffffffff);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100131
132 perf_event_update_userpage(event);
133
134 return ret;
135}
136
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100137u64 armpmu_event_update(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100138{
Mark Rutland8a16b342011-04-28 16:27:54 +0100139 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100140 struct hw_perf_event *hwc = &event->hw;
Will Deacona7378232011-03-25 17:12:37 +0100141 u64 delta, prev_raw_count, new_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100142
143again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200144 prev_raw_count = local64_read(&hwc->prev_count);
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100145 new_raw_count = armpmu->read_counter(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100146
Peter Zijlstrae7850592010-05-21 14:43:08 +0200147 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100148 new_raw_count) != prev_raw_count)
149 goto again;
150
Will Deacon57273472012-03-06 17:33:17 +0100151 delta = (new_raw_count - prev_raw_count) & armpmu->max_period;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100152
Peter Zijlstrae7850592010-05-21 14:43:08 +0200153 local64_add(delta, &event->count);
154 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100155
156 return new_raw_count;
157}
158
159static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100160armpmu_read(struct perf_event *event)
161{
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100162 armpmu_event_update(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100163}
164
165static void
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200166armpmu_stop(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100167{
Mark Rutland8a16b342011-04-28 16:27:54 +0100168 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100169 struct hw_perf_event *hwc = &event->hw;
170
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200171 /*
172 * ARM pmu always has to update the counter, so ignore
173 * PERF_EF_UPDATE, see comments in armpmu_start().
174 */
175 if (!(hwc->state & PERF_HES_STOPPED)) {
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100176 armpmu->disable(event);
177 armpmu_event_update(event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200178 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
179 }
180}
181
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100182static void armpmu_start(struct perf_event *event, int flags)
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200183{
Mark Rutland8a16b342011-04-28 16:27:54 +0100184 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200185 struct hw_perf_event *hwc = &event->hw;
186
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200187 /*
188 * ARM pmu always has to reprogram the period, so ignore
189 * PERF_EF_RELOAD, see the comment below.
190 */
191 if (flags & PERF_EF_RELOAD)
192 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
193
194 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100195 /*
196 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200197 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100198 * may have been left counting. If we don't do this step then we may
199 * get an interrupt too soon or *way* too late if the overflow has
200 * happened since disabling.
201 */
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100202 armpmu_event_set_period(event);
203 armpmu->enable(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100204}
205
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200206static void
207armpmu_del(struct perf_event *event, int flags)
208{
Mark Rutland8a16b342011-04-28 16:27:54 +0100209 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland11679252014-05-13 19:36:31 +0100210 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200211 struct hw_perf_event *hwc = &event->hw;
212 int idx = hwc->idx;
213
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200214 armpmu_stop(event, PERF_EF_UPDATE);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100215 hw_events->events[idx] = NULL;
216 clear_bit(idx, hw_events->used_mask);
Stephen Boydeab443e2014-02-07 21:01:22 +0000217 if (armpmu->clear_event_idx)
218 armpmu->clear_event_idx(hw_events, event);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200219
220 perf_event_update_userpage(event);
221}
222
Jamie Iles1b8873a2010-02-02 20:25:44 +0100223static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200224armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100225{
Mark Rutland8a16b342011-04-28 16:27:54 +0100226 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland11679252014-05-13 19:36:31 +0100227 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100228 struct hw_perf_event *hwc = &event->hw;
229 int idx;
230 int err = 0;
231
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200232 perf_pmu_disable(event->pmu);
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200233
Jamie Iles1b8873a2010-02-02 20:25:44 +0100234 /* If we don't have a space for the counter then finish early. */
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100235 idx = armpmu->get_event_idx(hw_events, event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100236 if (idx < 0) {
237 err = idx;
238 goto out;
239 }
240
241 /*
242 * If there is an event in the counter we are going to use then make
243 * sure it is disabled.
244 */
245 event->hw.idx = idx;
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100246 armpmu->disable(event);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100247 hw_events->events[idx] = event;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100248
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200249 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
250 if (flags & PERF_EF_START)
251 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100252
253 /* Propagate our changes to the userspace mapping. */
254 perf_event_update_userpage(event);
255
256out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200257 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100258 return err;
259}
260
Jamie Iles1b8873a2010-02-02 20:25:44 +0100261static int
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100262validate_event(struct pmu_hw_events *hw_events,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100263 struct perf_event *event)
264{
Mark Rutland8a16b342011-04-28 16:27:54 +0100265 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100266
Will Deaconc95eb312013-08-07 23:39:41 +0100267 if (is_software_event(event))
268 return 1;
269
Will Deacon2dfcb802013-10-09 13:51:29 +0100270 if (event->state < PERF_EVENT_STATE_OFF)
Will Deaconcb2d8b32013-04-12 19:04:19 +0100271 return 1;
272
273 if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
Will Deacon65b47112010-09-02 09:32:08 +0100274 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100275
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100276 return armpmu->get_event_idx(hw_events, event) >= 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100277}
278
279static int
280validate_group(struct perf_event *event)
281{
282 struct perf_event *sibling, *leader = event->group_leader;
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100283 struct pmu_hw_events fake_pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100284
Will Deaconbce34d12011-11-17 15:05:14 +0000285 /*
286 * Initialise the fake PMU. We only need to populate the
287 * used_mask for the purposes of validation.
288 */
Mark Rutlanda4560842014-05-13 19:08:19 +0100289 memset(&fake_pmu.used_mask, 0, sizeof(fake_pmu.used_mask));
Jamie Iles1b8873a2010-02-02 20:25:44 +0100290
291 if (!validate_event(&fake_pmu, leader))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100292 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100293
294 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
295 if (!validate_event(&fake_pmu, sibling))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100296 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100297 }
298
299 if (!validate_event(&fake_pmu, event))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100300 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100301
302 return 0;
303}
304
Sudeep KarkadaNagesha051f1b12012-07-31 10:34:25 +0100305static irqreturn_t armpmu_dispatch_irq(int irq, void *dev)
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530306{
Stephen Boydbbd64552014-02-07 21:01:19 +0000307 struct arm_pmu *armpmu;
308 struct platform_device *plat_device;
309 struct arm_pmu_platdata *plat;
Will Deacon5f5092e2014-02-11 18:08:41 +0000310 int ret;
311 u64 start_clock, finish_clock;
Stephen Boydbbd64552014-02-07 21:01:19 +0000312
Mark Rutland5ebd9202014-05-13 19:46:10 +0100313 /*
314 * we request the IRQ with a (possibly percpu) struct arm_pmu**, but
315 * the handlers expect a struct arm_pmu*. The percpu_irq framework will
316 * do any necessary shifting, we just need to perform the first
317 * dereference.
318 */
319 armpmu = *(void **)dev;
Stephen Boydbbd64552014-02-07 21:01:19 +0000320 plat_device = armpmu->plat_device;
321 plat = dev_get_platdata(&plat_device->dev);
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530322
Will Deacon5f5092e2014-02-11 18:08:41 +0000323 start_clock = sched_clock();
Sudeep KarkadaNagesha051f1b12012-07-31 10:34:25 +0100324 if (plat && plat->handle_irq)
Mark Rutland5ebd9202014-05-13 19:46:10 +0100325 ret = plat->handle_irq(irq, armpmu, armpmu->handle_irq);
Sudeep KarkadaNagesha051f1b12012-07-31 10:34:25 +0100326 else
Mark Rutland5ebd9202014-05-13 19:46:10 +0100327 ret = armpmu->handle_irq(irq, armpmu);
Will Deacon5f5092e2014-02-11 18:08:41 +0000328 finish_clock = sched_clock();
329
330 perf_sample_event_took(finish_clock - start_clock);
331 return ret;
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530332}
333
Will Deacon0b390e22011-07-27 15:18:59 +0100334static void
Mark Rutland8a16b342011-04-28 16:27:54 +0100335armpmu_release_hardware(struct arm_pmu *armpmu)
Will Deacon0b390e22011-07-27 15:18:59 +0100336{
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100337 armpmu->free_irq(armpmu);
Sudeep KarkadaNagesha051f1b12012-07-31 10:34:25 +0100338 pm_runtime_put_sync(&armpmu->plat_device->dev);
Will Deacon0b390e22011-07-27 15:18:59 +0100339}
340
Jamie Iles1b8873a2010-02-02 20:25:44 +0100341static int
Mark Rutland8a16b342011-04-28 16:27:54 +0100342armpmu_reserve_hardware(struct arm_pmu *armpmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100343{
Sudeep KarkadaNagesha051f1b12012-07-31 10:34:25 +0100344 int err;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100345 struct platform_device *pmu_device = armpmu->plat_device;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100346
Will Deacone5a21322011-11-22 18:01:46 +0000347 if (!pmu_device)
348 return -ENODEV;
349
Jon Hunter7be29582012-05-31 13:05:20 -0500350 pm_runtime_get_sync(&pmu_device->dev);
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100351 err = armpmu->request_irq(armpmu, armpmu_dispatch_irq);
Sudeep KarkadaNagesha051f1b12012-07-31 10:34:25 +0100352 if (err) {
353 armpmu_release_hardware(armpmu);
354 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100355 }
356
Will Deacon0b390e22011-07-27 15:18:59 +0100357 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100358}
359
Jamie Iles1b8873a2010-02-02 20:25:44 +0100360static void
361hw_perf_event_destroy(struct perf_event *event)
362{
Mark Rutland8a16b342011-04-28 16:27:54 +0100363 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland03b78982011-04-27 11:20:11 +0100364 atomic_t *active_events = &armpmu->active_events;
365 struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
366
367 if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
Mark Rutland8a16b342011-04-28 16:27:54 +0100368 armpmu_release_hardware(armpmu);
Mark Rutland03b78982011-04-27 11:20:11 +0100369 mutex_unlock(pmu_reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100370 }
371}
372
373static int
Will Deacon05d22fd2011-07-19 11:57:30 +0100374event_requires_mode_exclusion(struct perf_event_attr *attr)
375{
376 return attr->exclude_idle || attr->exclude_user ||
377 attr->exclude_kernel || attr->exclude_hv;
378}
379
380static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100381__hw_perf_event_init(struct perf_event *event)
382{
Mark Rutland8a16b342011-04-28 16:27:54 +0100383 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100384 struct hw_perf_event *hwc = &event->hw;
Mark Rutland9dcbf462013-01-18 16:10:06 +0000385 int mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100386
Mark Rutlande1f431b2011-04-28 15:47:10 +0100387 mapping = armpmu->map_event(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100388
389 if (mapping < 0) {
390 pr_debug("event %x:%llx not supported\n", event->attr.type,
391 event->attr.config);
392 return mapping;
393 }
394
395 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100396 * We don't assign an index until we actually place the event onto
397 * hardware. Use -1 to signify that we haven't decided where to put it
398 * yet. For SMP systems, each core has it's own PMU so we can't do any
399 * clever allocation or constraints checking at this point.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100400 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100401 hwc->idx = -1;
402 hwc->config_base = 0;
403 hwc->config = 0;
404 hwc->event_base = 0;
405
406 /*
407 * Check whether we need to exclude the counter from certain modes.
408 */
409 if ((!armpmu->set_event_filter ||
410 armpmu->set_event_filter(hwc, &event->attr)) &&
411 event_requires_mode_exclusion(&event->attr)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100412 pr_debug("ARM performance counters do not support "
413 "mode exclusion\n");
Will Deaconfdeb8e32012-07-04 18:15:42 +0100414 return -EOPNOTSUPP;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100415 }
416
417 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100418 * Store the event encoding into the config_base field.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100419 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100420 hwc->config_base |= (unsigned long)mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100421
Vince Weaveredcb4d32014-05-16 17:15:49 -0400422 if (!is_sampling_event(event)) {
Will Deacon57273472012-03-06 17:33:17 +0100423 /*
424 * For non-sampling runs, limit the sample_period to half
425 * of the counter width. That way, the new counter value
426 * is far less likely to overtake the previous one unless
427 * you have some serious IRQ latency issues.
428 */
429 hwc->sample_period = armpmu->max_period >> 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100430 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200431 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100432 }
433
Jamie Iles1b8873a2010-02-02 20:25:44 +0100434 if (event->group_leader != event) {
Chen Gange595ede2013-02-28 17:51:29 +0100435 if (validate_group(event) != 0)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100436 return -EINVAL;
437 }
438
Mark Rutland9dcbf462013-01-18 16:10:06 +0000439 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100440}
441
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200442static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100443{
Mark Rutland8a16b342011-04-28 16:27:54 +0100444 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100445 int err = 0;
Mark Rutland03b78982011-04-27 11:20:11 +0100446 atomic_t *active_events = &armpmu->active_events;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100447
Stephane Eranian2481c5f2012-02-09 23:20:59 +0100448 /* does not support taken branch sampling */
449 if (has_branch_stack(event))
450 return -EOPNOTSUPP;
451
Mark Rutlande1f431b2011-04-28 15:47:10 +0100452 if (armpmu->map_event(event) == -ENOENT)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200453 return -ENOENT;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200454
Jamie Iles1b8873a2010-02-02 20:25:44 +0100455 event->destroy = hw_perf_event_destroy;
456
Mark Rutland03b78982011-04-27 11:20:11 +0100457 if (!atomic_inc_not_zero(active_events)) {
458 mutex_lock(&armpmu->reserve_mutex);
459 if (atomic_read(active_events) == 0)
Mark Rutland8a16b342011-04-28 16:27:54 +0100460 err = armpmu_reserve_hardware(armpmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100461
462 if (!err)
Mark Rutland03b78982011-04-27 11:20:11 +0100463 atomic_inc(active_events);
464 mutex_unlock(&armpmu->reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100465 }
466
467 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200468 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100469
470 err = __hw_perf_event_init(event);
471 if (err)
472 hw_perf_event_destroy(event);
473
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200474 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100475}
476
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200477static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100478{
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100479 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Mark Rutland11679252014-05-13 19:36:31 +0100480 struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
Mark Rutland7325eae2011-08-23 11:59:49 +0100481 int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100482
Will Deaconf4f38432011-07-01 14:38:12 +0100483 if (enabled)
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100484 armpmu->start(armpmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100485}
486
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200487static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100488{
Mark Rutland8a16b342011-04-28 16:27:54 +0100489 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Sudeep KarkadaNageshaed6f2a52012-07-30 12:00:02 +0100490 armpmu->stop(armpmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100491}
492
Rafael J. Wysockibf7c5442014-12-13 00:42:49 +0100493#ifdef CONFIG_PM
Jon Hunter7be29582012-05-31 13:05:20 -0500494static int armpmu_runtime_resume(struct device *dev)
495{
496 struct arm_pmu_platdata *plat = dev_get_platdata(dev);
497
498 if (plat && plat->runtime_resume)
499 return plat->runtime_resume(dev);
500
501 return 0;
502}
503
504static int armpmu_runtime_suspend(struct device *dev)
505{
506 struct arm_pmu_platdata *plat = dev_get_platdata(dev);
507
508 if (plat && plat->runtime_suspend)
509 return plat->runtime_suspend(dev);
510
511 return 0;
512}
513#endif
514
Will Deacon6dbc0022012-07-29 12:36:28 +0100515const struct dev_pm_ops armpmu_dev_pm_ops = {
516 SET_RUNTIME_PM_OPS(armpmu_runtime_suspend, armpmu_runtime_resume, NULL)
517};
518
Stephen Boyd44d6b1f2013-03-05 03:54:06 +0100519static void armpmu_init(struct arm_pmu *armpmu)
Mark Rutland03b78982011-04-27 11:20:11 +0100520{
521 atomic_set(&armpmu->active_events, 0);
522 mutex_init(&armpmu->reserve_mutex);
Mark Rutland8a16b342011-04-28 16:27:54 +0100523
524 armpmu->pmu = (struct pmu) {
525 .pmu_enable = armpmu_enable,
526 .pmu_disable = armpmu_disable,
527 .event_init = armpmu_event_init,
528 .add = armpmu_add,
529 .del = armpmu_del,
530 .start = armpmu_start,
531 .stop = armpmu_stop,
532 .read = armpmu_read,
533 };
534}
535
Will Deacon03052302012-09-21 14:23:47 +0100536int armpmu_register(struct arm_pmu *armpmu, int type)
Mark Rutland8a16b342011-04-28 16:27:54 +0100537{
538 armpmu_init(armpmu);
Jon Hunter2ac29a12012-10-25 21:23:18 +0100539 pm_runtime_enable(&armpmu->plat_device->dev);
Will Deacon04236f92012-07-28 17:42:22 +0100540 pr_info("enabled with %s PMU driver, %d counters available\n",
541 armpmu->name, armpmu->num_events);
Will Deacon03052302012-09-21 14:23:47 +0100542 return perf_pmu_register(&armpmu->pmu, armpmu->name, type);
Mark Rutland03b78982011-04-27 11:20:11 +0100543}
544