blob: b59ad93f2ebd9e5ec9eaa93ba7f2a311e58263d9 [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>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022#include <linux/irq.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010023
24#include <asm/cputype.h>
25#include <asm/irq.h>
26#include <asm/irq_regs.h>
27#include <asm/pmu.h>
28#include <asm/stacktrace.h>
Ashwin Chaugule464983a2011-11-21 14:51:51 -050029#include <linux/cpu_pm.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010030
Will Deacon49c006b2010-04-29 17:13:24 +010031static struct platform_device *pmu_device;
Jamie Iles1b8873a2010-02-02 20:25:44 +010032
33/*
34 * Hardware lock to serialize accesses to PMU registers. Needed for the
35 * read/modify/write sequences.
36 */
Will Deacon961ec6d2010-12-02 18:01:49 +010037static DEFINE_RAW_SPINLOCK(pmu_lock);
Jamie Iles1b8873a2010-02-02 20:25:44 +010038
39/*
40 * ARMv6 supports a maximum of 3 events, starting from index 1. If we add
41 * another platform that supports more, we need to increase this to be the
42 * largest of all platforms.
Jean PIHET796d1292010-01-26 18:51:05 +010043 *
44 * ARMv7 supports up to 32 events:
45 * cycle counter CCNT + 31 events counters CNT0..30.
46 * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
Jamie Iles1b8873a2010-02-02 20:25:44 +010047 */
Jean PIHET796d1292010-01-26 18:51:05 +010048#define ARMPMU_MAX_HWEVENTS 33
Jamie Iles1b8873a2010-02-02 20:25:44 +010049
50/* The events for a given CPU. */
51struct cpu_hw_events {
52 /*
53 * The events that are active on the CPU for the given index. Index 0
54 * is reserved.
55 */
56 struct perf_event *events[ARMPMU_MAX_HWEVENTS];
57
58 /*
59 * A 1 bit for an index indicates that the counter is being used for
60 * an event. A 0 means that the counter can be used.
61 */
62 unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
63
64 /*
65 * A 1 bit for an index indicates that the counter is actively being
66 * used.
67 */
68 unsigned long active_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
69};
Will Deacon4d6b7a72010-11-30 18:15:53 +010070static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
Will Deacon181193f2010-04-30 11:32:44 +010071
Jamie Iles1b8873a2010-02-02 20:25:44 +010072struct arm_pmu {
Will Deacon181193f2010-04-30 11:32:44 +010073 enum arm_perf_pmu_ids id;
Will Deacon62994832010-11-13 18:45:27 +000074 const char *name;
Jamie Iles1b8873a2010-02-02 20:25:44 +010075 irqreturn_t (*handle_irq)(int irq_num, void *dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076#ifdef CONFIG_SMP
77 void (*secondary_enable)(unsigned int irq);
78 void (*secondary_disable)(unsigned int irq);
79#endif
Jamie Iles1b8873a2010-02-02 20:25:44 +010080 void (*enable)(struct hw_perf_event *evt, int idx);
81 void (*disable)(struct hw_perf_event *evt, int idx);
Jamie Iles1b8873a2010-02-02 20:25:44 +010082 int (*get_event_idx)(struct cpu_hw_events *cpuc,
83 struct hw_perf_event *hwc);
84 u32 (*read_counter)(int idx);
85 void (*write_counter)(int idx, u32 val);
Ashwin Chaugule133ddac2011-11-28 11:39:38 -050086 int (*set_event_filter) (struct hw_perf_event *evt,
87 struct perf_event_attr *attr);
Jamie Iles1b8873a2010-02-02 20:25:44 +010088 void (*start)(void);
89 void (*stop)(void);
Will Deacon574b69c2011-03-25 13:13:34 +010090 void (*reset)(void *);
Ashwin Chaugule14da0032011-11-10 13:55:16 -050091 unsigned (*cache_map)[PERF_COUNT_HW_CACHE_MAX]
Will Deacon84fee972010-11-13 17:13:56 +000092 [PERF_COUNT_HW_CACHE_OP_MAX]
93 [PERF_COUNT_HW_CACHE_RESULT_MAX];
94 const unsigned (*event_map)[PERF_COUNT_HW_MAX];
95 u32 raw_event_mask;
Jamie Iles1b8873a2010-02-02 20:25:44 +010096 int num_events;
97 u64 max_period;
98};
99
100/* Set at runtime when we know what CPU type we are. */
101static const struct arm_pmu *armpmu;
102
Will Deacon181193f2010-04-30 11:32:44 +0100103enum arm_perf_pmu_ids
104armpmu_get_pmu_id(void)
105{
106 int id = -ENODEV;
107
108 if (armpmu != NULL)
109 id = armpmu->id;
110
111 return id;
112}
113EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
114
Will Deacon929f5192010-04-30 11:34:26 +0100115int
116armpmu_get_max_events(void)
117{
118 int max_events = 0;
119
120 if (armpmu != NULL)
121 max_events = armpmu->num_events;
122
123 return max_events;
124}
125EXPORT_SYMBOL_GPL(armpmu_get_max_events);
126
Matt Fleming3bf101b2010-09-27 20:22:24 +0100127int perf_num_counters(void)
128{
129 return armpmu_get_max_events();
130}
131EXPORT_SYMBOL_GPL(perf_num_counters);
132
Jamie Iles1b8873a2010-02-02 20:25:44 +0100133#define HW_OP_UNSUPPORTED 0xFFFF
134
135#define C(_x) \
136 PERF_COUNT_HW_CACHE_##_x
137
138#define CACHE_OP_UNSUPPORTED 0xFFFF
139
Jamie Iles1b8873a2010-02-02 20:25:44 +0100140static int
141armpmu_map_cache_event(u64 config)
142{
143 unsigned int cache_type, cache_op, cache_result, ret;
144
145 cache_type = (config >> 0) & 0xff;
146 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
147 return -EINVAL;
148
149 cache_op = (config >> 8) & 0xff;
150 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
151 return -EINVAL;
152
153 cache_result = (config >> 16) & 0xff;
154 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
155 return -EINVAL;
156
Will Deacon84fee972010-11-13 17:13:56 +0000157 ret = (int)(*armpmu->cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100158
159 if (ret == CACHE_OP_UNSUPPORTED)
160 return -ENOENT;
161
162 return ret;
163}
164
165static int
Will Deacon84fee972010-11-13 17:13:56 +0000166armpmu_map_event(u64 config)
167{
168 int mapping = (*armpmu->event_map)[config];
169 return mapping == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : mapping;
170}
171
172static int
173armpmu_map_raw_event(u64 config)
174{
175 return (int)(config & armpmu->raw_event_mask);
176}
177
178static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100179armpmu_event_set_period(struct perf_event *event,
180 struct hw_perf_event *hwc,
181 int idx)
182{
Peter Zijlstrae7850592010-05-21 14:43:08 +0200183 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100184 s64 period = hwc->sample_period;
185 int ret = 0;
186
187 if (unlikely(left <= -period)) {
188 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200189 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100190 hwc->last_period = period;
191 ret = 1;
192 }
193
194 if (unlikely(left <= 0)) {
195 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200196 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100197 hwc->last_period = period;
198 ret = 1;
199 }
200
201 if (left > (s64)armpmu->max_period)
202 left = armpmu->max_period;
203
Peter Zijlstrae7850592010-05-21 14:43:08 +0200204 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100205
206 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
207
208 perf_event_update_userpage(event);
209
210 return ret;
211}
212
213static u64
214armpmu_event_update(struct perf_event *event,
215 struct hw_perf_event *hwc,
Will Deacona7378232011-03-25 17:12:37 +0100216 int idx, int overflow)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100217{
Will Deacona7378232011-03-25 17:12:37 +0100218 u64 delta, prev_raw_count, new_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100219
220again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200221 prev_raw_count = local64_read(&hwc->prev_count);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100222 new_raw_count = armpmu->read_counter(idx);
223
Peter Zijlstrae7850592010-05-21 14:43:08 +0200224 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100225 new_raw_count) != prev_raw_count)
226 goto again;
227
Will Deacona7378232011-03-25 17:12:37 +0100228 new_raw_count &= armpmu->max_period;
229 prev_raw_count &= armpmu->max_period;
230
231 if (overflow)
Will Deacon67597882011-04-05 14:01:24 +0100232 delta = armpmu->max_period - prev_raw_count + new_raw_count + 1;
Will Deacona7378232011-03-25 17:12:37 +0100233 else
234 delta = new_raw_count - prev_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100235
Peter Zijlstrae7850592010-05-21 14:43:08 +0200236 local64_add(delta, &event->count);
237 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100238
239 return new_raw_count;
240}
241
242static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100243armpmu_read(struct perf_event *event)
244{
245 struct hw_perf_event *hwc = &event->hw;
246
247 /* Don't read disabled counters! */
248 if (hwc->idx < 0)
249 return;
250
Will Deacona7378232011-03-25 17:12:37 +0100251 armpmu_event_update(event, hwc, hwc->idx, 0);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100252}
253
254static void
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200255armpmu_stop(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100256{
257 struct hw_perf_event *hwc = &event->hw;
258
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200259 if (!armpmu)
260 return;
261
262 /*
263 * ARM pmu always has to update the counter, so ignore
264 * PERF_EF_UPDATE, see comments in armpmu_start().
265 */
266 if (!(hwc->state & PERF_HES_STOPPED)) {
267 armpmu->disable(hwc, hwc->idx);
268 barrier(); /* why? */
Will Deacona7378232011-03-25 17:12:37 +0100269 armpmu_event_update(event, hwc, hwc->idx, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200270 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
271 }
272}
273
274static void
275armpmu_start(struct perf_event *event, int flags)
276{
277 struct hw_perf_event *hwc = &event->hw;
278
279 if (!armpmu)
280 return;
281
282 /*
283 * ARM pmu always has to reprogram the period, so ignore
284 * PERF_EF_RELOAD, see the comment below.
285 */
286 if (flags & PERF_EF_RELOAD)
287 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
288
289 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100290 /*
291 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200292 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100293 * may have been left counting. If we don't do this step then we may
294 * get an interrupt too soon or *way* too late if the overflow has
295 * happened since disabling.
296 */
297 armpmu_event_set_period(event, hwc, hwc->idx);
298 armpmu->enable(hwc, hwc->idx);
299}
300
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200301static void
302armpmu_del(struct perf_event *event, int flags)
303{
304 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
305 struct hw_perf_event *hwc = &event->hw;
306 int idx = hwc->idx;
307
308 WARN_ON(idx < 0);
309
310 clear_bit(idx, cpuc->active_mask);
311 armpmu_stop(event, PERF_EF_UPDATE);
312 cpuc->events[idx] = NULL;
313 clear_bit(idx, cpuc->used_mask);
314
315 perf_event_update_userpage(event);
316}
317
Jamie Iles1b8873a2010-02-02 20:25:44 +0100318static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200319armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100320{
321 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
322 struct hw_perf_event *hwc = &event->hw;
323 int idx;
324 int err = 0;
325
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200326 perf_pmu_disable(event->pmu);
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200327
Jamie Iles1b8873a2010-02-02 20:25:44 +0100328 /* If we don't have a space for the counter then finish early. */
329 idx = armpmu->get_event_idx(cpuc, hwc);
330 if (idx < 0) {
331 err = idx;
332 goto out;
333 }
334
335 /*
336 * If there is an event in the counter we are going to use then make
337 * sure it is disabled.
338 */
339 event->hw.idx = idx;
340 armpmu->disable(hwc, idx);
341 cpuc->events[idx] = event;
342 set_bit(idx, cpuc->active_mask);
343
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200344 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
345 if (flags & PERF_EF_START)
346 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100347
348 /* Propagate our changes to the userspace mapping. */
349 perf_event_update_userpage(event);
350
351out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200352 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100353 return err;
354}
355
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200356static struct pmu pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100357
358static int
359validate_event(struct cpu_hw_events *cpuc,
360 struct perf_event *event)
361{
362 struct hw_perf_event fake_event = event->hw;
363
Will Deacon65b47112010-09-02 09:32:08 +0100364 if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF)
365 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100366
367 return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
368}
369
370static int
371validate_group(struct perf_event *event)
372{
373 struct perf_event *sibling, *leader = event->group_leader;
374 struct cpu_hw_events fake_pmu;
375
376 memset(&fake_pmu, 0, sizeof(fake_pmu));
377
378 if (!validate_event(&fake_pmu, leader))
379 return -ENOSPC;
380
381 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
382 if (!validate_event(&fake_pmu, sibling))
383 return -ENOSPC;
384 }
385
386 if (!validate_event(&fake_pmu, event))
387 return -ENOSPC;
388
389 return 0;
390}
391
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530392static irqreturn_t armpmu_platform_irq(int irq, void *dev)
393{
394 struct arm_pmu_platdata *plat = dev_get_platdata(&pmu_device->dev);
395
396 return plat->handle_irq(irq, dev, armpmu->handle_irq);
397}
398
Jamie Iles1b8873a2010-02-02 20:25:44 +0100399static int
400armpmu_reserve_hardware(void)
401{
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530402 struct arm_pmu_platdata *plat;
403 irq_handler_t handle_irq;
Will Deacon49c006b2010-04-29 17:13:24 +0100404 int i, err = -ENODEV, irq;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100405
Will Deacon49c006b2010-04-29 17:13:24 +0100406 pmu_device = reserve_pmu(ARM_PMU_DEVICE_CPU);
407 if (IS_ERR(pmu_device)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100408 pr_warning("unable to reserve pmu\n");
Will Deacon49c006b2010-04-29 17:13:24 +0100409 return PTR_ERR(pmu_device);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100410 }
411
Will Deacon49c006b2010-04-29 17:13:24 +0100412 init_pmu(ARM_PMU_DEVICE_CPU);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100413
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530414 plat = dev_get_platdata(&pmu_device->dev);
415 if (plat && plat->handle_irq)
416 handle_irq = armpmu_platform_irq;
417 else
418 handle_irq = armpmu->handle_irq;
419
Will Deacon49c006b2010-04-29 17:13:24 +0100420 if (pmu_device->num_resources < 1) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100421 pr_err("no irqs for PMUs defined\n");
422 return -ENODEV;
423 }
424
Will Deacon49c006b2010-04-29 17:13:24 +0100425 for (i = 0; i < pmu_device->num_resources; ++i) {
426 irq = platform_get_irq(pmu_device, i);
427 if (irq < 0)
428 continue;
429
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530430 err = request_irq(irq, handle_irq,
Will Deaconddee87f2010-02-25 15:04:14 +0100431 IRQF_DISABLED | IRQF_NOBALANCING,
432 "armpmu", NULL);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100433 if (err) {
Will Deacon49c006b2010-04-29 17:13:24 +0100434 pr_warning("unable to request IRQ%d for ARM perf "
435 "counters\n", irq);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100436 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437#ifdef CONFIG_SMP
438 } else if (armpmu->secondary_enable) {
439 armpmu->secondary_enable(irq);
440#endif
Jamie Iles1b8873a2010-02-02 20:25:44 +0100441 }
442 }
443
444 if (err) {
Will Deacon49c006b2010-04-29 17:13:24 +0100445 for (i = i - 1; i >= 0; --i) {
446 irq = platform_get_irq(pmu_device, i);
447 if (irq >= 0)
448 free_irq(irq, NULL);
449 }
450 release_pmu(pmu_device);
451 pmu_device = NULL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100452 }
453
454 return err;
455}
456
457static void
458armpmu_release_hardware(void)
459{
Will Deacon49c006b2010-04-29 17:13:24 +0100460 int i, irq;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100461
Will Deacon49c006b2010-04-29 17:13:24 +0100462 for (i = pmu_device->num_resources - 1; i >= 0; --i) {
463 irq = platform_get_irq(pmu_device, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 if (irq >= 0) {
Will Deacon49c006b2010-04-29 17:13:24 +0100465 free_irq(irq, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466#ifdef CONFIG_SMP
467 if (armpmu->secondary_disable)
468 armpmu->secondary_disable(irq);
469#endif
470 }
Will Deacon49c006b2010-04-29 17:13:24 +0100471 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100472 armpmu->stop();
473
Will Deacon49c006b2010-04-29 17:13:24 +0100474 release_pmu(pmu_device);
475 pmu_device = NULL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100476}
477
Ashwin Chaugule133ddac2011-11-28 11:39:38 -0500478static int
479event_requires_mode_exclusion(struct perf_event_attr *attr)
480{
481 return attr->exclude_idle || attr->exclude_user ||
482 attr->exclude_kernel || attr->exclude_hv;
483}
484
Jamie Iles1b8873a2010-02-02 20:25:44 +0100485static atomic_t active_events = ATOMIC_INIT(0);
486static DEFINE_MUTEX(pmu_reserve_mutex);
487
488static void
489hw_perf_event_destroy(struct perf_event *event)
490{
491 if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
492 armpmu_release_hardware();
493 mutex_unlock(&pmu_reserve_mutex);
494 }
495}
496
497static int
498__hw_perf_event_init(struct perf_event *event)
499{
500 struct hw_perf_event *hwc = &event->hw;
501 int mapping, err;
502
503 /* Decode the generic type into an ARM event identifier. */
504 if (PERF_TYPE_HARDWARE == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000505 mapping = armpmu_map_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100506 } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
507 mapping = armpmu_map_cache_event(event->attr.config);
508 } else if (PERF_TYPE_RAW == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000509 mapping = armpmu_map_raw_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100510 } else {
511 pr_debug("event type %x not supported\n", event->attr.type);
512 return -EOPNOTSUPP;
513 }
514
515 if (mapping < 0) {
516 pr_debug("event %x:%llx not supported\n", event->attr.type,
517 event->attr.config);
518 return mapping;
519 }
520
Jamie Iles1b8873a2010-02-02 20:25:44 +0100521
522 /*
523 * We don't assign an index until we actually place the event onto
524 * hardware. Use -1 to signify that we haven't decided where to put it
525 * yet. For SMP systems, each core has it's own PMU so we can't do any
526 * clever allocation or constraints checking at this point.
527 */
528 hwc->idx = -1;
529
530 /*
531 * Store the event encoding into the config_base field. config and
532 * event_base are unused as the only 2 things we need to know are
533 * the event mapping and the counter to use. The counter to use is
534 * also the indx and the config_base is the event type.
535 */
Ashwin Chaugule133ddac2011-11-28 11:39:38 -0500536 hwc->config_base = 0;
537 hwc->config = 0;
538 hwc->event_base = 0;
539
540 if ((!armpmu->set_event_filter ||
541 armpmu->set_event_filter(hwc, &event->attr)) &&
542 event_requires_mode_exclusion(&event->attr)) {
543 pr_debug("ARM performance counters do not support "
544 "mode exclusion\n");
545 return -EPERM;
546 }
547
548 hwc->config_base |= (unsigned long)mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100549
550 if (!hwc->sample_period) {
551 hwc->sample_period = armpmu->max_period;
Ashwin Chaugule133ddac2011-11-28 11:39:38 -0500552 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200553 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100554 }
555
556 err = 0;
557 if (event->group_leader != event) {
558 err = validate_group(event);
559 if (err)
560 return -EINVAL;
561 }
562
563 return err;
564}
565
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200566static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100567{
568 int err = 0;
569
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200570 switch (event->attr.type) {
571 case PERF_TYPE_RAW:
572 case PERF_TYPE_HARDWARE:
573 case PERF_TYPE_HW_CACHE:
574 break;
575
576 default:
577 return -ENOENT;
578 }
579
Jamie Iles1b8873a2010-02-02 20:25:44 +0100580 if (!armpmu)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200581 return -ENODEV;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100582
583 event->destroy = hw_perf_event_destroy;
584
585 if (!atomic_inc_not_zero(&active_events)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100586 mutex_lock(&pmu_reserve_mutex);
587 if (atomic_read(&active_events) == 0) {
588 err = armpmu_reserve_hardware();
589 }
590
591 if (!err)
592 atomic_inc(&active_events);
593 mutex_unlock(&pmu_reserve_mutex);
594 }
595
596 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200597 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100598
599 err = __hw_perf_event_init(event);
600 if (err)
601 hw_perf_event_destroy(event);
602
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200603 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100604}
605
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200606static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100607{
608 /* Enable all of the perf events on hardware. */
Will Deaconf4f38432011-07-01 14:38:12 +0100609 int idx, enabled = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100610 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
611
612 if (!armpmu)
613 return;
614
615 for (idx = 0; idx <= armpmu->num_events; ++idx) {
616 struct perf_event *event = cpuc->events[idx];
617
618 if (!event)
619 continue;
620
621 armpmu->enable(&event->hw, idx);
Will Deaconf4f38432011-07-01 14:38:12 +0100622 enabled = 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100623 }
624
Will Deaconf4f38432011-07-01 14:38:12 +0100625 if (enabled)
626 armpmu->start();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100627}
628
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200629static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100630{
631 if (armpmu)
632 armpmu->stop();
633}
634
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200635static struct pmu pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200636 .pmu_enable = armpmu_enable,
637 .pmu_disable = armpmu_disable,
638 .event_init = armpmu_event_init,
639 .add = armpmu_add,
640 .del = armpmu_del,
641 .start = armpmu_start,
642 .stop = armpmu_stop,
643 .read = armpmu_read,
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200644};
645
Will Deacon43eab872010-11-13 19:04:32 +0000646/* Include the PMU-specific implementations. */
647#include "perf_event_xscale.c"
648#include "perf_event_v6.c"
649#include "perf_event_v7.c"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650#include "perf_event_msm.c"
651#include "perf_event_msm_l2.c"
652#include "perf_event_msm_krait.c"
653#include "perf_event_msm_krait_l2.c"
Will Deacon49e6a322010-04-30 11:33:33 +0100654
Ashwin Chaugule464983a2011-11-21 14:51:51 -0500655static int perf_cpu_pm_notifier(struct notifier_block *self, unsigned long cmd,
656 void *v)
657{
658 switch (cmd) {
659 case CPU_PM_ENTER:
660 perf_pmu_disable(&pmu);
661 break;
662
663 case CPU_PM_ENTER_FAILED:
664 case CPU_PM_EXIT:
665 if (armpmu && armpmu->reset)
666 armpmu->reset(NULL);
667 perf_pmu_enable(&pmu);
668
669 break;
670 }
671
672 return NOTIFY_OK;
673}
674
675static struct notifier_block perf_cpu_pm_notifier_block = {
676 .notifier_call = perf_cpu_pm_notifier,
677};
678
Will Deacon574b69c2011-03-25 13:13:34 +0100679/*
680 * Ensure the PMU has sane values out of reset.
681 * This requires SMP to be available, so exists as a separate initcall.
682 */
683static int __init
684armpmu_reset(void)
685{
686 if (armpmu && armpmu->reset)
687 return on_each_cpu(armpmu->reset, NULL, 1);
688 return 0;
689}
690arch_initcall(armpmu_reset);
691
Jamie Iles1b8873a2010-02-02 20:25:44 +0100692static int __init
693init_hw_perf_events(void)
694{
695 unsigned long cpuid = read_cpuid_id();
696 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
697 unsigned long part_number = (cpuid & 0xFFF0);
698
Will Deacon49e6a322010-04-30 11:33:33 +0100699 /* ARM Ltd CPUs. */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100700 if (0x41 == implementor) {
701 switch (part_number) {
702 case 0xB360: /* ARM1136 */
703 case 0xB560: /* ARM1156 */
704 case 0xB760: /* ARM1176 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000705 armpmu = armv6pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100706 break;
707 case 0xB020: /* ARM11mpcore */
Will Deacon3cb314b2010-11-13 17:37:46 +0000708 armpmu = armv6mpcore_pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100709 break;
Jean PIHET796d1292010-01-26 18:51:05 +0100710 case 0xC080: /* Cortex-A8 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000711 armpmu = armv7_a8_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100712 break;
713 case 0xC090: /* Cortex-A9 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000714 armpmu = armv7_a9_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100715 break;
Will Deaconacdc46f2011-11-09 13:01:31 +0530716 case 0xC050: /* Cortex-A5 */
717 armpmu = armv7_a5_pmu_init();
718 break;
Will Deacon903bfa52011-11-09 13:01:57 +0530719 case 0xC0F0: /* Cortex-A15 */
720 armpmu = armv7_a15_pmu_init();
721 break;
Will Deacon49e6a322010-04-30 11:33:33 +0100722 }
723 /* Intel CPUs [xscale]. */
724 } else if (0x69 == implementor) {
725 part_number = (cpuid >> 13) & 0x7;
726 switch (part_number) {
727 case 1:
Will Deacon3cb314b2010-11-13 17:37:46 +0000728 armpmu = xscale1pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100729 break;
730 case 2:
Will Deacon3cb314b2010-11-13 17:37:46 +0000731 armpmu = xscale2pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100732 break;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100733 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734 /* Qualcomm CPUs */
735 } else if (0x51 == implementor) {
736 switch (part_number) {
737 case 0x00F0: /* 8x50 & 7x30*/
738 armpmu = armv7_scorpion_pmu_init();
739 break;
740 case 0x02D0: /* 8x60 */
741 armpmu = armv7_scorpionmp_pmu_init();
742 scorpionmp_l2_pmu_init();
743 break;
744 case 0x0490: /* 8960 sim */
745 case 0x04D0: /* 8960 */
746 armpmu = armv7_krait_pmu_init();
747 krait_l2_pmu_init();
748 break;
749 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100750 }
751
Will Deacon49e6a322010-04-30 11:33:33 +0100752 if (armpmu) {
Jean PIHET796d1292010-01-26 18:51:05 +0100753 pr_info("enabled with %s PMU driver, %d counters available\n",
Will Deacon62994832010-11-13 18:45:27 +0000754 armpmu->name, armpmu->num_events);
Will Deacon49e6a322010-04-30 11:33:33 +0100755 } else {
756 pr_info("no hardware support available\n");
Will Deacon49e6a322010-04-30 11:33:33 +0100757 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100758
Peter Zijlstra2e80a822010-11-17 23:17:36 +0100759 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200760
Ashwin Chaugule464983a2011-11-21 14:51:51 -0500761 cpu_pm_register_notifier(&perf_cpu_pm_notifier_block);
762
Jamie Iles1b8873a2010-02-02 20:25:44 +0100763 return 0;
764}
Peter Zijlstra004417a2010-11-25 18:38:29 +0100765early_initcall(init_hw_perf_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100766
767/*
768 * Callchain handling code.
769 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100770
771/*
772 * The registers we're interested in are at the end of the variable
773 * length saved register structure. The fp points at the end of this
774 * structure so the address of this struct is:
775 * (struct frame_tail *)(xxx->fp)-1
776 *
777 * This code has been adapted from the ARM OProfile support.
778 */
779struct frame_tail {
Will Deacon4d6b7a72010-11-30 18:15:53 +0100780 struct frame_tail __user *fp;
781 unsigned long sp;
782 unsigned long lr;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100783} __attribute__((packed));
784
785/*
786 * Get the return address for a single stackframe and return a pointer to the
787 * next frame tail.
788 */
Will Deacon4d6b7a72010-11-30 18:15:53 +0100789static struct frame_tail __user *
790user_backtrace(struct frame_tail __user *tail,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100791 struct perf_callchain_entry *entry)
792{
793 struct frame_tail buftail;
794
795 /* Also check accessibility of one struct frame_tail beyond */
796 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
797 return NULL;
798 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
799 return NULL;
800
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200801 perf_callchain_store(entry, buftail.lr);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100802
803 /*
804 * Frame pointers should strictly progress back up the stack
805 * (towards higher addresses).
806 */
Rabin Vincentcb061992011-02-09 11:35:12 +0100807 if (tail + 1 >= buftail.fp)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100808 return NULL;
809
810 return buftail.fp - 1;
811}
812
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200813void
814perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100815{
Will Deacon4d6b7a72010-11-30 18:15:53 +0100816 struct frame_tail __user *tail;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100817
Jamie Iles1b8873a2010-02-02 20:25:44 +0100818
Will Deacon4d6b7a72010-11-30 18:15:53 +0100819 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100820
Sonny Rao860ad782011-04-18 22:12:59 +0100821 while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
822 tail && !((unsigned long)tail & 0x3))
Jamie Iles1b8873a2010-02-02 20:25:44 +0100823 tail = user_backtrace(tail, entry);
824}
825
826/*
827 * Gets called by walk_stackframe() for every stackframe. This will be called
828 * whist unwinding the stackframe and is like a subroutine return so we use
829 * the PC.
830 */
831static int
832callchain_trace(struct stackframe *fr,
833 void *data)
834{
835 struct perf_callchain_entry *entry = data;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200836 perf_callchain_store(entry, fr->pc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100837 return 0;
838}
839
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200840void
841perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100842{
843 struct stackframe fr;
844
Jamie Iles1b8873a2010-02-02 20:25:44 +0100845 fr.fp = regs->ARM_fp;
846 fr.sp = regs->ARM_sp;
847 fr.lr = regs->ARM_lr;
848 fr.pc = regs->ARM_pc;
849 walk_stackframe(&fr, callchain_trace, entry);
850}