blob: b13bf23ceba38f5e60107460685831192162687d [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
10 * on the x86 code. Callchain code is based on the ARM OProfile backtrace
11 * code.
12 */
13#define pr_fmt(fmt) "hw perfevents: " fmt
14
15#include <linux/interrupt.h>
16#include <linux/kernel.h>
Will Deacon181193f2010-04-30 11:32:44 +010017#include <linux/module.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010018#include <linux/perf_event.h>
Will Deacon49c006b2010-04-29 17:13:24 +010019#include <linux/platform_device.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010020#include <linux/spinlock.h>
21#include <linux/uaccess.h>
22
23#include <asm/cputype.h>
24#include <asm/irq.h>
25#include <asm/irq_regs.h>
26#include <asm/pmu.h>
27#include <asm/stacktrace.h>
28
Jamie Iles1b8873a2010-02-02 20:25:44 +010029/*
Will Deaconecf5a892011-07-19 22:43:28 +010030 * ARMv6 supports a maximum of 3 events, starting from index 0. If we add
Jamie Iles1b8873a2010-02-02 20:25:44 +010031 * another platform that supports more, we need to increase this to be the
32 * largest of all platforms.
Jean PIHET796d1292010-01-26 18:51:05 +010033 *
34 * ARMv7 supports up to 32 events:
35 * cycle counter CCNT + 31 events counters CNT0..30.
36 * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
Jamie Iles1b8873a2010-02-02 20:25:44 +010037 */
Will Deaconecf5a892011-07-19 22:43:28 +010038#define ARMPMU_MAX_HWEVENTS 32
Jamie Iles1b8873a2010-02-02 20:25:44 +010039
40/* The events for a given CPU. */
41struct cpu_hw_events {
42 /*
Will Deaconecf5a892011-07-19 22:43:28 +010043 * The events that are active on the CPU for the given index.
Jamie Iles1b8873a2010-02-02 20:25:44 +010044 */
45 struct perf_event *events[ARMPMU_MAX_HWEVENTS];
46
47 /*
48 * A 1 bit for an index indicates that the counter is being used for
49 * an event. A 0 means that the counter can be used.
50 */
51 unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
Mark Rutland0f78d2d2011-04-28 10:17:04 +010052
53 /*
54 * Hardware lock to serialize accesses to PMU registers. Needed for the
55 * read/modify/write sequences.
56 */
57 raw_spinlock_t pmu_lock;
Jamie Iles1b8873a2010-02-02 20:25:44 +010058};
Will Deacon4d6b7a72010-11-30 18:15:53 +010059static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
Will Deacon181193f2010-04-30 11:32:44 +010060
Jamie Iles1b8873a2010-02-02 20:25:44 +010061struct arm_pmu {
Will Deacon181193f2010-04-30 11:32:44 +010062 enum arm_perf_pmu_ids id;
Mark Rutland7ae18a52011-06-06 10:37:50 +010063 enum arm_pmu_type type;
Will Deacon0b390e22011-07-27 15:18:59 +010064 cpumask_t active_irqs;
Will Deacon62994832010-11-13 18:45:27 +000065 const char *name;
Jamie Iles1b8873a2010-02-02 20:25:44 +010066 irqreturn_t (*handle_irq)(int irq_num, void *dev);
67 void (*enable)(struct hw_perf_event *evt, int idx);
68 void (*disable)(struct hw_perf_event *evt, int idx);
Jamie Iles1b8873a2010-02-02 20:25:44 +010069 int (*get_event_idx)(struct cpu_hw_events *cpuc,
70 struct hw_perf_event *hwc);
Will Deacon05d22fd2011-07-19 11:57:30 +010071 int (*set_event_filter)(struct hw_perf_event *evt,
72 struct perf_event_attr *attr);
Jamie Iles1b8873a2010-02-02 20:25:44 +010073 u32 (*read_counter)(int idx);
74 void (*write_counter)(int idx, u32 val);
75 void (*start)(void);
76 void (*stop)(void);
Will Deacon574b69c2011-03-25 13:13:34 +010077 void (*reset)(void *);
Mark Rutlande1f431b2011-04-28 15:47:10 +010078 int (*map_event)(struct perf_event *event);
Jamie Iles1b8873a2010-02-02 20:25:44 +010079 int num_events;
Mark Rutland03b78982011-04-27 11:20:11 +010080 atomic_t active_events;
81 struct mutex reserve_mutex;
Jamie Iles1b8873a2010-02-02 20:25:44 +010082 u64 max_period;
Mark Rutlanda9356a02011-05-04 09:23:15 +010083 struct platform_device *plat_device;
Mark Rutland92f701e2011-05-04 09:23:51 +010084 struct cpu_hw_events *(*get_hw_events)(void);
Jamie Iles1b8873a2010-02-02 20:25:44 +010085};
86
87/* Set at runtime when we know what CPU type we are. */
Mark Rutlanda6c93af2011-04-15 11:14:38 +010088static struct arm_pmu *armpmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +010089
Will Deacon181193f2010-04-30 11:32:44 +010090enum arm_perf_pmu_ids
91armpmu_get_pmu_id(void)
92{
93 int id = -ENODEV;
94
95 if (armpmu != NULL)
96 id = armpmu->id;
97
98 return id;
99}
100EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
101
Will Deacon929f5192010-04-30 11:34:26 +0100102int
103armpmu_get_max_events(void)
104{
105 int max_events = 0;
106
107 if (armpmu != NULL)
108 max_events = armpmu->num_events;
109
110 return max_events;
111}
112EXPORT_SYMBOL_GPL(armpmu_get_max_events);
113
Matt Fleming3bf101b2010-09-27 20:22:24 +0100114int perf_num_counters(void)
115{
116 return armpmu_get_max_events();
117}
118EXPORT_SYMBOL_GPL(perf_num_counters);
119
Jamie Iles1b8873a2010-02-02 20:25:44 +0100120#define HW_OP_UNSUPPORTED 0xFFFF
121
122#define C(_x) \
123 PERF_COUNT_HW_CACHE_##_x
124
125#define CACHE_OP_UNSUPPORTED 0xFFFF
126
Jamie Iles1b8873a2010-02-02 20:25:44 +0100127static int
Mark Rutlande1f431b2011-04-28 15:47:10 +0100128armpmu_map_cache_event(const unsigned (*cache_map)
129 [PERF_COUNT_HW_CACHE_MAX]
130 [PERF_COUNT_HW_CACHE_OP_MAX]
131 [PERF_COUNT_HW_CACHE_RESULT_MAX],
132 u64 config)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100133{
134 unsigned int cache_type, cache_op, cache_result, ret;
135
136 cache_type = (config >> 0) & 0xff;
137 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
138 return -EINVAL;
139
140 cache_op = (config >> 8) & 0xff;
141 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
142 return -EINVAL;
143
144 cache_result = (config >> 16) & 0xff;
145 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
146 return -EINVAL;
147
Mark Rutlande1f431b2011-04-28 15:47:10 +0100148 ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100149
150 if (ret == CACHE_OP_UNSUPPORTED)
151 return -ENOENT;
152
153 return ret;
154}
155
156static int
Mark Rutlande1f431b2011-04-28 15:47:10 +0100157armpmu_map_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
Will Deacon84fee972010-11-13 17:13:56 +0000158{
Mark Rutlande1f431b2011-04-28 15:47:10 +0100159 int mapping = (*event_map)[config];
160 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
169static int map_cpu_event(struct perf_event *event,
170 const unsigned (*event_map)[PERF_COUNT_HW_MAX],
171 const unsigned (*cache_map)
172 [PERF_COUNT_HW_CACHE_MAX]
173 [PERF_COUNT_HW_CACHE_OP_MAX]
174 [PERF_COUNT_HW_CACHE_RESULT_MAX],
175 u32 raw_event_mask)
176{
177 u64 config = event->attr.config;
178
179 switch (event->attr.type) {
180 case PERF_TYPE_HARDWARE:
181 return armpmu_map_event(event_map, config);
182 case PERF_TYPE_HW_CACHE:
183 return armpmu_map_cache_event(cache_map, config);
184 case PERF_TYPE_RAW:
185 return armpmu_map_raw_event(raw_event_mask, config);
186 }
187
188 return -ENOENT;
Will Deacon84fee972010-11-13 17:13:56 +0000189}
190
191static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100192armpmu_event_set_period(struct perf_event *event,
193 struct hw_perf_event *hwc,
194 int idx)
195{
Peter Zijlstrae7850592010-05-21 14:43:08 +0200196 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100197 s64 period = hwc->sample_period;
198 int ret = 0;
199
200 if (unlikely(left <= -period)) {
201 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200202 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100203 hwc->last_period = period;
204 ret = 1;
205 }
206
207 if (unlikely(left <= 0)) {
208 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200209 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100210 hwc->last_period = period;
211 ret = 1;
212 }
213
214 if (left > (s64)armpmu->max_period)
215 left = armpmu->max_period;
216
Peter Zijlstrae7850592010-05-21 14:43:08 +0200217 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100218
219 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
220
221 perf_event_update_userpage(event);
222
223 return ret;
224}
225
226static u64
227armpmu_event_update(struct perf_event *event,
228 struct hw_perf_event *hwc,
Will Deacona7378232011-03-25 17:12:37 +0100229 int idx, int overflow)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100230{
Will Deacona7378232011-03-25 17:12:37 +0100231 u64 delta, prev_raw_count, new_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100232
233again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200234 prev_raw_count = local64_read(&hwc->prev_count);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100235 new_raw_count = armpmu->read_counter(idx);
236
Peter Zijlstrae7850592010-05-21 14:43:08 +0200237 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100238 new_raw_count) != prev_raw_count)
239 goto again;
240
Will Deacona7378232011-03-25 17:12:37 +0100241 new_raw_count &= armpmu->max_period;
242 prev_raw_count &= armpmu->max_period;
243
244 if (overflow)
Will Deacon67597882011-04-05 14:01:24 +0100245 delta = armpmu->max_period - prev_raw_count + new_raw_count + 1;
Will Deacona7378232011-03-25 17:12:37 +0100246 else
247 delta = new_raw_count - prev_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100248
Peter Zijlstrae7850592010-05-21 14:43:08 +0200249 local64_add(delta, &event->count);
250 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100251
252 return new_raw_count;
253}
254
255static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100256armpmu_read(struct perf_event *event)
257{
258 struct hw_perf_event *hwc = &event->hw;
259
260 /* Don't read disabled counters! */
261 if (hwc->idx < 0)
262 return;
263
Will Deacona7378232011-03-25 17:12:37 +0100264 armpmu_event_update(event, hwc, hwc->idx, 0);
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{
270 struct hw_perf_event *hwc = &event->hw;
271
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200272 /*
273 * ARM pmu always has to update the counter, so ignore
274 * PERF_EF_UPDATE, see comments in armpmu_start().
275 */
276 if (!(hwc->state & PERF_HES_STOPPED)) {
277 armpmu->disable(hwc, hwc->idx);
278 barrier(); /* why? */
Will Deacona7378232011-03-25 17:12:37 +0100279 armpmu_event_update(event, hwc, hwc->idx, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200280 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
281 }
282}
283
284static void
285armpmu_start(struct perf_event *event, int flags)
286{
287 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 */
304 armpmu_event_set_period(event, hwc, hwc->idx);
305 armpmu->enable(hwc, hwc->idx);
306}
307
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200308static void
309armpmu_del(struct perf_event *event, int flags)
310{
Mark Rutland92f701e2011-05-04 09:23:51 +0100311 struct cpu_hw_events *cpuc = armpmu->get_hw_events();
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200312 struct hw_perf_event *hwc = &event->hw;
313 int idx = hwc->idx;
314
315 WARN_ON(idx < 0);
316
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200317 armpmu_stop(event, PERF_EF_UPDATE);
318 cpuc->events[idx] = NULL;
319 clear_bit(idx, cpuc->used_mask);
320
321 perf_event_update_userpage(event);
322}
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 Rutland92f701e2011-05-04 09:23:51 +0100327 struct cpu_hw_events *cpuc = armpmu->get_hw_events();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100328 struct hw_perf_event *hwc = &event->hw;
329 int idx;
330 int err = 0;
331
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200332 perf_pmu_disable(event->pmu);
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200333
Jamie Iles1b8873a2010-02-02 20:25:44 +0100334 /* If we don't have a space for the counter then finish early. */
335 idx = armpmu->get_event_idx(cpuc, hwc);
336 if (idx < 0) {
337 err = idx;
338 goto out;
339 }
340
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;
346 armpmu->disable(hwc, idx);
347 cpuc->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
356out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200357 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100358 return err;
359}
360
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200361static struct pmu pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100362
363static int
364validate_event(struct cpu_hw_events *cpuc,
365 struct perf_event *event)
366{
367 struct hw_perf_event fake_event = event->hw;
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100368 struct pmu *leader_pmu = event->group_leader->pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100369
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100370 if (event->pmu != leader_pmu || event->state <= PERF_EVENT_STATE_OFF)
Will Deacon65b47112010-09-02 09:32:08 +0100371 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100372
373 return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
374}
375
376static int
377validate_group(struct perf_event *event)
378{
379 struct perf_event *sibling, *leader = event->group_leader;
380 struct cpu_hw_events fake_pmu;
381
382 memset(&fake_pmu, 0, sizeof(fake_pmu));
383
384 if (!validate_event(&fake_pmu, leader))
385 return -ENOSPC;
386
387 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
388 if (!validate_event(&fake_pmu, sibling))
389 return -ENOSPC;
390 }
391
392 if (!validate_event(&fake_pmu, event))
393 return -ENOSPC;
394
395 return 0;
396}
397
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530398static irqreturn_t armpmu_platform_irq(int irq, void *dev)
399{
Mark Rutlanda9356a02011-05-04 09:23:15 +0100400 struct platform_device *plat_device = armpmu->plat_device;
401 struct arm_pmu_platdata *plat = dev_get_platdata(&plat_device->dev);
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530402
403 return plat->handle_irq(irq, dev, armpmu->handle_irq);
404}
405
Will Deacon0b390e22011-07-27 15:18:59 +0100406static void
407armpmu_release_hardware(void)
408{
409 int i, irq, irqs;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100410 struct platform_device *pmu_device = armpmu->plat_device;
Will Deacon0b390e22011-07-27 15:18:59 +0100411
412 irqs = min(pmu_device->num_resources, num_possible_cpus());
413
414 for (i = 0; i < irqs; ++i) {
415 if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs))
416 continue;
417 irq = platform_get_irq(pmu_device, i);
418 if (irq >= 0)
419 free_irq(irq, NULL);
420 }
421
Mark Rutland7ae18a52011-06-06 10:37:50 +0100422 release_pmu(armpmu->type);
Will Deacon0b390e22011-07-27 15:18:59 +0100423}
424
Jamie Iles1b8873a2010-02-02 20:25:44 +0100425static int
426armpmu_reserve_hardware(void)
427{
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530428 struct arm_pmu_platdata *plat;
429 irq_handler_t handle_irq;
Will Deaconb0e89592011-07-26 22:10:28 +0100430 int i, err, irq, irqs;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100431 struct platform_device *pmu_device = armpmu->plat_device;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100432
Mark Rutland7ae18a52011-06-06 10:37:50 +0100433 err = reserve_pmu(armpmu->type);
Will Deaconb0e89592011-07-26 22:10:28 +0100434 if (err) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100435 pr_warning("unable to reserve pmu\n");
Will Deaconb0e89592011-07-26 22:10:28 +0100436 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100437 }
438
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530439 plat = dev_get_platdata(&pmu_device->dev);
440 if (plat && plat->handle_irq)
441 handle_irq = armpmu_platform_irq;
442 else
443 handle_irq = armpmu->handle_irq;
444
Will Deacon0b390e22011-07-27 15:18:59 +0100445 irqs = min(pmu_device->num_resources, num_possible_cpus());
Will Deaconb0e89592011-07-26 22:10:28 +0100446 if (irqs < 1) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100447 pr_err("no irqs for PMUs defined\n");
448 return -ENODEV;
449 }
450
Will Deaconb0e89592011-07-26 22:10:28 +0100451 for (i = 0; i < irqs; ++i) {
Will Deacon0b390e22011-07-27 15:18:59 +0100452 err = 0;
Will Deacon49c006b2010-04-29 17:13:24 +0100453 irq = platform_get_irq(pmu_device, i);
454 if (irq < 0)
455 continue;
456
Will Deaconb0e89592011-07-26 22:10:28 +0100457 /*
458 * If we have a single PMU interrupt that we can't shift,
459 * assume that we're running on a uniprocessor machine and
Will Deacon0b390e22011-07-27 15:18:59 +0100460 * continue. Otherwise, continue without this interrupt.
Will Deaconb0e89592011-07-26 22:10:28 +0100461 */
Will Deacon0b390e22011-07-27 15:18:59 +0100462 if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
463 pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
464 irq, i);
465 continue;
Will Deaconb0e89592011-07-26 22:10:28 +0100466 }
467
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530468 err = request_irq(irq, handle_irq,
Will Deaconddee87f2010-02-25 15:04:14 +0100469 IRQF_DISABLED | IRQF_NOBALANCING,
Will Deaconb0e89592011-07-26 22:10:28 +0100470 "arm-pmu", NULL);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100471 if (err) {
Will Deaconb0e89592011-07-26 22:10:28 +0100472 pr_err("unable to request IRQ%d for ARM PMU counters\n",
473 irq);
Will Deacon0b390e22011-07-27 15:18:59 +0100474 armpmu_release_hardware();
475 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100476 }
Will Deacon0b390e22011-07-27 15:18:59 +0100477
478 cpumask_set_cpu(i, &armpmu->active_irqs);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100479 }
480
Will Deacon0b390e22011-07-27 15:18:59 +0100481 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100482}
483
Jamie Iles1b8873a2010-02-02 20:25:44 +0100484static void
485hw_perf_event_destroy(struct perf_event *event)
486{
Mark Rutland03b78982011-04-27 11:20:11 +0100487 atomic_t *active_events = &armpmu->active_events;
488 struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
489
490 if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100491 armpmu_release_hardware();
Mark Rutland03b78982011-04-27 11:20:11 +0100492 mutex_unlock(pmu_reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100493 }
494}
495
496static int
Will Deacon05d22fd2011-07-19 11:57:30 +0100497event_requires_mode_exclusion(struct perf_event_attr *attr)
498{
499 return attr->exclude_idle || attr->exclude_user ||
500 attr->exclude_kernel || attr->exclude_hv;
501}
502
503static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100504__hw_perf_event_init(struct perf_event *event)
505{
506 struct hw_perf_event *hwc = &event->hw;
507 int mapping, err;
508
Mark Rutlande1f431b2011-04-28 15:47:10 +0100509 mapping = armpmu->map_event(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100510
511 if (mapping < 0) {
512 pr_debug("event %x:%llx not supported\n", event->attr.type,
513 event->attr.config);
514 return mapping;
515 }
516
517 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100518 * We don't assign an index until we actually place the event onto
519 * hardware. Use -1 to signify that we haven't decided where to put it
520 * yet. For SMP systems, each core has it's own PMU so we can't do any
521 * clever allocation or constraints checking at this point.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100522 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100523 hwc->idx = -1;
524 hwc->config_base = 0;
525 hwc->config = 0;
526 hwc->event_base = 0;
527
528 /*
529 * Check whether we need to exclude the counter from certain modes.
530 */
531 if ((!armpmu->set_event_filter ||
532 armpmu->set_event_filter(hwc, &event->attr)) &&
533 event_requires_mode_exclusion(&event->attr)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100534 pr_debug("ARM performance counters do not support "
535 "mode exclusion\n");
536 return -EPERM;
537 }
538
539 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100540 * Store the event encoding into the config_base field.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100541 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100542 hwc->config_base |= (unsigned long)mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100543
544 if (!hwc->sample_period) {
545 hwc->sample_period = armpmu->max_period;
546 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200547 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100548 }
549
550 err = 0;
551 if (event->group_leader != event) {
552 err = validate_group(event);
553 if (err)
554 return -EINVAL;
555 }
556
557 return err;
558}
559
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200560static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100561{
562 int err = 0;
Mark Rutland03b78982011-04-27 11:20:11 +0100563 atomic_t *active_events = &armpmu->active_events;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100564
Mark Rutlande1f431b2011-04-28 15:47:10 +0100565 if (armpmu->map_event(event) == -ENOENT)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200566 return -ENOENT;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200567
Jamie Iles1b8873a2010-02-02 20:25:44 +0100568 event->destroy = hw_perf_event_destroy;
569
Mark Rutland03b78982011-04-27 11:20:11 +0100570 if (!atomic_inc_not_zero(active_events)) {
571 mutex_lock(&armpmu->reserve_mutex);
572 if (atomic_read(active_events) == 0)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100573 err = armpmu_reserve_hardware();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100574
575 if (!err)
Mark Rutland03b78982011-04-27 11:20:11 +0100576 atomic_inc(active_events);
577 mutex_unlock(&armpmu->reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100578 }
579
580 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200581 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100582
583 err = __hw_perf_event_init(event);
584 if (err)
585 hw_perf_event_destroy(event);
586
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200587 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100588}
589
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200590static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100591{
592 /* Enable all of the perf events on hardware. */
Will Deaconf4f38432011-07-01 14:38:12 +0100593 int idx, enabled = 0;
Mark Rutland92f701e2011-05-04 09:23:51 +0100594 struct cpu_hw_events *cpuc = armpmu->get_hw_events();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100595
Will Deaconecf5a892011-07-19 22:43:28 +0100596 for (idx = 0; idx < armpmu->num_events; ++idx) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100597 struct perf_event *event = cpuc->events[idx];
598
599 if (!event)
600 continue;
601
602 armpmu->enable(&event->hw, idx);
Will Deaconf4f38432011-07-01 14:38:12 +0100603 enabled = 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100604 }
605
Will Deaconf4f38432011-07-01 14:38:12 +0100606 if (enabled)
607 armpmu->start();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100608}
609
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200610static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100611{
Mark Rutland48957152011-04-27 10:31:51 +0100612 armpmu->stop();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100613}
614
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200615static struct pmu pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200616 .pmu_enable = armpmu_enable,
617 .pmu_disable = armpmu_disable,
618 .event_init = armpmu_event_init,
619 .add = armpmu_add,
620 .del = armpmu_del,
621 .start = armpmu_start,
622 .stop = armpmu_stop,
623 .read = armpmu_read,
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200624};
625
Mark Rutland03b78982011-04-27 11:20:11 +0100626static void __init armpmu_init(struct arm_pmu *armpmu)
627{
628 atomic_set(&armpmu->active_events, 0);
629 mutex_init(&armpmu->reserve_mutex);
630}
631
Will Deacon43eab872010-11-13 19:04:32 +0000632/* Include the PMU-specific implementations. */
633#include "perf_event_xscale.c"
634#include "perf_event_v6.c"
635#include "perf_event_v7.c"
Will Deacon49e6a322010-04-30 11:33:33 +0100636
Will Deacon574b69c2011-03-25 13:13:34 +0100637/*
638 * Ensure the PMU has sane values out of reset.
639 * This requires SMP to be available, so exists as a separate initcall.
640 */
641static int __init
642armpmu_reset(void)
643{
644 if (armpmu && armpmu->reset)
645 return on_each_cpu(armpmu->reset, NULL, 1);
646 return 0;
647}
648arch_initcall(armpmu_reset);
649
Will Deaconb0e89592011-07-26 22:10:28 +0100650/*
651 * PMU platform driver and devicetree bindings.
652 */
653static struct of_device_id armpmu_of_device_ids[] = {
654 {.compatible = "arm,cortex-a9-pmu"},
655 {.compatible = "arm,cortex-a8-pmu"},
656 {.compatible = "arm,arm1136-pmu"},
657 {.compatible = "arm,arm1176-pmu"},
658 {},
659};
660
661static struct platform_device_id armpmu_plat_device_ids[] = {
662 {.name = "arm-pmu"},
663 {},
664};
665
666static int __devinit armpmu_device_probe(struct platform_device *pdev)
667{
Mark Rutlanda9356a02011-05-04 09:23:15 +0100668 armpmu->plat_device = pdev;
Will Deaconb0e89592011-07-26 22:10:28 +0100669 return 0;
670}
671
672static struct platform_driver armpmu_driver = {
673 .driver = {
674 .name = "arm-pmu",
675 .of_match_table = armpmu_of_device_ids,
676 },
677 .probe = armpmu_device_probe,
678 .id_table = armpmu_plat_device_ids,
679};
680
681static int __init register_pmu_driver(void)
682{
683 return platform_driver_register(&armpmu_driver);
684}
685device_initcall(register_pmu_driver);
686
Mark Rutland92f701e2011-05-04 09:23:51 +0100687static struct cpu_hw_events *armpmu_get_cpu_events(void)
688{
689 return &__get_cpu_var(cpu_hw_events);
690}
691
692static void __init cpu_pmu_init(struct arm_pmu *armpmu)
693{
Mark Rutland0f78d2d2011-04-28 10:17:04 +0100694 int cpu;
695 for_each_possible_cpu(cpu) {
696 struct cpu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
697 raw_spin_lock_init(&events->pmu_lock);
698 }
Mark Rutland92f701e2011-05-04 09:23:51 +0100699 armpmu->get_hw_events = armpmu_get_cpu_events;
Mark Rutland7ae18a52011-06-06 10:37:50 +0100700 armpmu->type = ARM_PMU_DEVICE_CPU;
Mark Rutland92f701e2011-05-04 09:23:51 +0100701}
702
Will Deaconb0e89592011-07-26 22:10:28 +0100703/*
704 * CPU PMU identification and registration.
705 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100706static int __init
707init_hw_perf_events(void)
708{
709 unsigned long cpuid = read_cpuid_id();
710 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
711 unsigned long part_number = (cpuid & 0xFFF0);
712
Will Deacon49e6a322010-04-30 11:33:33 +0100713 /* ARM Ltd CPUs. */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100714 if (0x41 == implementor) {
715 switch (part_number) {
716 case 0xB360: /* ARM1136 */
717 case 0xB560: /* ARM1156 */
718 case 0xB760: /* ARM1176 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000719 armpmu = armv6pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100720 break;
721 case 0xB020: /* ARM11mpcore */
Will Deacon3cb314b2010-11-13 17:37:46 +0000722 armpmu = armv6mpcore_pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100723 break;
Jean PIHET796d1292010-01-26 18:51:05 +0100724 case 0xC080: /* Cortex-A8 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000725 armpmu = armv7_a8_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100726 break;
727 case 0xC090: /* Cortex-A9 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000728 armpmu = armv7_a9_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100729 break;
Will Deacon0c205cb2011-06-03 17:40:15 +0100730 case 0xC050: /* Cortex-A5 */
731 armpmu = armv7_a5_pmu_init();
732 break;
Will Deacon14abd032011-01-19 14:24:38 +0000733 case 0xC0F0: /* Cortex-A15 */
734 armpmu = armv7_a15_pmu_init();
735 break;
Will Deacon49e6a322010-04-30 11:33:33 +0100736 }
737 /* Intel CPUs [xscale]. */
738 } else if (0x69 == implementor) {
739 part_number = (cpuid >> 13) & 0x7;
740 switch (part_number) {
741 case 1:
Will Deacon3cb314b2010-11-13 17:37:46 +0000742 armpmu = xscale1pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100743 break;
744 case 2:
Will Deacon3cb314b2010-11-13 17:37:46 +0000745 armpmu = xscale2pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100746 break;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100747 }
748 }
749
Will Deacon49e6a322010-04-30 11:33:33 +0100750 if (armpmu) {
Jean PIHET796d1292010-01-26 18:51:05 +0100751 pr_info("enabled with %s PMU driver, %d counters available\n",
Will Deacon62994832010-11-13 18:45:27 +0000752 armpmu->name, armpmu->num_events);
Mark Rutland92f701e2011-05-04 09:23:51 +0100753 cpu_pmu_init(armpmu);
Mark Rutland03b78982011-04-27 11:20:11 +0100754 armpmu_init(armpmu);
Mark Rutland48957152011-04-27 10:31:51 +0100755 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
Will Deacon49e6a322010-04-30 11:33:33 +0100756 } else {
757 pr_info("no hardware support available\n");
Will Deacon49e6a322010-04-30 11:33:33 +0100758 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100759
760 return 0;
761}
Peter Zijlstra004417a2010-11-25 18:38:29 +0100762early_initcall(init_hw_perf_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100763
764/*
765 * Callchain handling code.
766 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100767
768/*
769 * The registers we're interested in are at the end of the variable
770 * length saved register structure. The fp points at the end of this
771 * structure so the address of this struct is:
772 * (struct frame_tail *)(xxx->fp)-1
773 *
774 * This code has been adapted from the ARM OProfile support.
775 */
776struct frame_tail {
Will Deacon4d6b7a72010-11-30 18:15:53 +0100777 struct frame_tail __user *fp;
778 unsigned long sp;
779 unsigned long lr;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100780} __attribute__((packed));
781
782/*
783 * Get the return address for a single stackframe and return a pointer to the
784 * next frame tail.
785 */
Will Deacon4d6b7a72010-11-30 18:15:53 +0100786static struct frame_tail __user *
787user_backtrace(struct frame_tail __user *tail,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100788 struct perf_callchain_entry *entry)
789{
790 struct frame_tail buftail;
791
792 /* Also check accessibility of one struct frame_tail beyond */
793 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
794 return NULL;
795 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
796 return NULL;
797
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200798 perf_callchain_store(entry, buftail.lr);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100799
800 /*
801 * Frame pointers should strictly progress back up the stack
802 * (towards higher addresses).
803 */
Rabin Vincentcb061992011-02-09 11:35:12 +0100804 if (tail + 1 >= buftail.fp)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100805 return NULL;
806
807 return buftail.fp - 1;
808}
809
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200810void
811perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100812{
Will Deacon4d6b7a72010-11-30 18:15:53 +0100813 struct frame_tail __user *tail;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100814
Jamie Iles1b8873a2010-02-02 20:25:44 +0100815
Will Deacon4d6b7a72010-11-30 18:15:53 +0100816 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100817
Sonny Rao860ad782011-04-18 22:12:59 +0100818 while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
819 tail && !((unsigned long)tail & 0x3))
Jamie Iles1b8873a2010-02-02 20:25:44 +0100820 tail = user_backtrace(tail, entry);
821}
822
823/*
824 * Gets called by walk_stackframe() for every stackframe. This will be called
825 * whist unwinding the stackframe and is like a subroutine return so we use
826 * the PC.
827 */
828static int
829callchain_trace(struct stackframe *fr,
830 void *data)
831{
832 struct perf_callchain_entry *entry = data;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200833 perf_callchain_store(entry, fr->pc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100834 return 0;
835}
836
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200837void
838perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100839{
840 struct stackframe fr;
841
Jamie Iles1b8873a2010-02-02 20:25:44 +0100842 fr.fp = regs->ARM_fp;
843 fr.sp = regs->ARM_sp;
844 fr.lr = regs->ARM_lr;
845 fr.pc = regs->ARM_pc;
846 walk_stackframe(&fr, callchain_trace, entry);
847}