blob: c9982829f217e47a9fea201f708ebf9315837c68 [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>
29
Will Deacon49c006b2010-04-29 17:13:24 +010030static struct platform_device *pmu_device;
Jamie Iles1b8873a2010-02-02 20:25:44 +010031
32/*
33 * Hardware lock to serialize accesses to PMU registers. Needed for the
34 * read/modify/write sequences.
35 */
Will Deacon961ec6d2010-12-02 18:01:49 +010036static DEFINE_RAW_SPINLOCK(pmu_lock);
Jamie Iles1b8873a2010-02-02 20:25:44 +010037
38/*
39 * ARMv6 supports a maximum of 3 events, starting from index 1. If we add
40 * another platform that supports more, we need to increase this to be the
41 * largest of all platforms.
Jean PIHET796d1292010-01-26 18:51:05 +010042 *
43 * ARMv7 supports up to 32 events:
44 * cycle counter CCNT + 31 events counters CNT0..30.
45 * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
Jamie Iles1b8873a2010-02-02 20:25:44 +010046 */
Jean PIHET796d1292010-01-26 18:51:05 +010047#define ARMPMU_MAX_HWEVENTS 33
Jamie Iles1b8873a2010-02-02 20:25:44 +010048
49/* The events for a given CPU. */
50struct cpu_hw_events {
51 /*
52 * The events that are active on the CPU for the given index. Index 0
53 * is reserved.
54 */
55 struct perf_event *events[ARMPMU_MAX_HWEVENTS];
56
57 /*
58 * A 1 bit for an index indicates that the counter is being used for
59 * an event. A 0 means that the counter can be used.
60 */
61 unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
62
63 /*
64 * A 1 bit for an index indicates that the counter is actively being
65 * used.
66 */
67 unsigned long active_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
68};
Will Deacon4d6b7a72010-11-30 18:15:53 +010069static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
Will Deacon181193f2010-04-30 11:32:44 +010070
Jamie Iles1b8873a2010-02-02 20:25:44 +010071struct arm_pmu {
Will Deacon181193f2010-04-30 11:32:44 +010072 enum arm_perf_pmu_ids id;
Will Deacon62994832010-11-13 18:45:27 +000073 const char *name;
Jamie Iles1b8873a2010-02-02 20:25:44 +010074 irqreturn_t (*handle_irq)(int irq_num, void *dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075#ifdef CONFIG_SMP
76 void (*secondary_enable)(unsigned int irq);
77 void (*secondary_disable)(unsigned int irq);
78#endif
Jamie Iles1b8873a2010-02-02 20:25:44 +010079 void (*enable)(struct hw_perf_event *evt, int idx);
80 void (*disable)(struct hw_perf_event *evt, int idx);
Jamie Iles1b8873a2010-02-02 20:25:44 +010081 int (*get_event_idx)(struct cpu_hw_events *cpuc,
82 struct hw_perf_event *hwc);
83 u32 (*read_counter)(int idx);
84 void (*write_counter)(int idx, u32 val);
85 void (*start)(void);
86 void (*stop)(void);
Will Deacon574b69c2011-03-25 13:13:34 +010087 void (*reset)(void *);
Will Deacon84fee972010-11-13 17:13:56 +000088 const unsigned (*cache_map)[PERF_COUNT_HW_CACHE_MAX]
89 [PERF_COUNT_HW_CACHE_OP_MAX]
90 [PERF_COUNT_HW_CACHE_RESULT_MAX];
91 const unsigned (*event_map)[PERF_COUNT_HW_MAX];
92 u32 raw_event_mask;
Jamie Iles1b8873a2010-02-02 20:25:44 +010093 int num_events;
94 u64 max_period;
95};
96
97/* Set at runtime when we know what CPU type we are. */
98static const struct arm_pmu *armpmu;
99
Will Deacon181193f2010-04-30 11:32:44 +0100100enum arm_perf_pmu_ids
101armpmu_get_pmu_id(void)
102{
103 int id = -ENODEV;
104
105 if (armpmu != NULL)
106 id = armpmu->id;
107
108 return id;
109}
110EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
111
Will Deacon929f5192010-04-30 11:34:26 +0100112int
113armpmu_get_max_events(void)
114{
115 int max_events = 0;
116
117 if (armpmu != NULL)
118 max_events = armpmu->num_events;
119
120 return max_events;
121}
122EXPORT_SYMBOL_GPL(armpmu_get_max_events);
123
Matt Fleming3bf101b2010-09-27 20:22:24 +0100124int perf_num_counters(void)
125{
126 return armpmu_get_max_events();
127}
128EXPORT_SYMBOL_GPL(perf_num_counters);
129
Jamie Iles1b8873a2010-02-02 20:25:44 +0100130#define HW_OP_UNSUPPORTED 0xFFFF
131
132#define C(_x) \
133 PERF_COUNT_HW_CACHE_##_x
134
135#define CACHE_OP_UNSUPPORTED 0xFFFF
136
Jamie Iles1b8873a2010-02-02 20:25:44 +0100137static int
138armpmu_map_cache_event(u64 config)
139{
140 unsigned int cache_type, cache_op, cache_result, ret;
141
142 cache_type = (config >> 0) & 0xff;
143 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
144 return -EINVAL;
145
146 cache_op = (config >> 8) & 0xff;
147 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
148 return -EINVAL;
149
150 cache_result = (config >> 16) & 0xff;
151 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
152 return -EINVAL;
153
Will Deacon84fee972010-11-13 17:13:56 +0000154 ret = (int)(*armpmu->cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100155
156 if (ret == CACHE_OP_UNSUPPORTED)
157 return -ENOENT;
158
159 return ret;
160}
161
162static int
Will Deacon84fee972010-11-13 17:13:56 +0000163armpmu_map_event(u64 config)
164{
165 int mapping = (*armpmu->event_map)[config];
166 return mapping == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : mapping;
167}
168
169static int
170armpmu_map_raw_event(u64 config)
171{
172 return (int)(config & armpmu->raw_event_mask);
173}
174
175static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100176armpmu_event_set_period(struct perf_event *event,
177 struct hw_perf_event *hwc,
178 int idx)
179{
Peter Zijlstrae7850592010-05-21 14:43:08 +0200180 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100181 s64 period = hwc->sample_period;
182 int ret = 0;
183
184 if (unlikely(left <= -period)) {
185 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200186 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100187 hwc->last_period = period;
188 ret = 1;
189 }
190
191 if (unlikely(left <= 0)) {
192 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200193 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100194 hwc->last_period = period;
195 ret = 1;
196 }
197
198 if (left > (s64)armpmu->max_period)
199 left = armpmu->max_period;
200
Peter Zijlstrae7850592010-05-21 14:43:08 +0200201 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100202
203 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
204
205 perf_event_update_userpage(event);
206
207 return ret;
208}
209
210static u64
211armpmu_event_update(struct perf_event *event,
212 struct hw_perf_event *hwc,
Will Deacona7378232011-03-25 17:12:37 +0100213 int idx, int overflow)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100214{
Will Deacona7378232011-03-25 17:12:37 +0100215 u64 delta, prev_raw_count, new_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100216
217again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200218 prev_raw_count = local64_read(&hwc->prev_count);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100219 new_raw_count = armpmu->read_counter(idx);
220
Peter Zijlstrae7850592010-05-21 14:43:08 +0200221 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100222 new_raw_count) != prev_raw_count)
223 goto again;
224
Will Deacona7378232011-03-25 17:12:37 +0100225 new_raw_count &= armpmu->max_period;
226 prev_raw_count &= armpmu->max_period;
227
228 if (overflow)
Will Deacon67597882011-04-05 14:01:24 +0100229 delta = armpmu->max_period - prev_raw_count + new_raw_count + 1;
Will Deacona7378232011-03-25 17:12:37 +0100230 else
231 delta = new_raw_count - prev_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100232
Peter Zijlstrae7850592010-05-21 14:43:08 +0200233 local64_add(delta, &event->count);
234 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100235
236 return new_raw_count;
237}
238
239static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100240armpmu_read(struct perf_event *event)
241{
242 struct hw_perf_event *hwc = &event->hw;
243
244 /* Don't read disabled counters! */
245 if (hwc->idx < 0)
246 return;
247
Will Deacona7378232011-03-25 17:12:37 +0100248 armpmu_event_update(event, hwc, hwc->idx, 0);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100249}
250
251static void
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200252armpmu_stop(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100253{
254 struct hw_perf_event *hwc = &event->hw;
255
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200256 if (!armpmu)
257 return;
258
259 /*
260 * ARM pmu always has to update the counter, so ignore
261 * PERF_EF_UPDATE, see comments in armpmu_start().
262 */
263 if (!(hwc->state & PERF_HES_STOPPED)) {
264 armpmu->disable(hwc, hwc->idx);
265 barrier(); /* why? */
Will Deacona7378232011-03-25 17:12:37 +0100266 armpmu_event_update(event, hwc, hwc->idx, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200267 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
268 }
269}
270
271static void
272armpmu_start(struct perf_event *event, int flags)
273{
274 struct hw_perf_event *hwc = &event->hw;
275
276 if (!armpmu)
277 return;
278
279 /*
280 * ARM pmu always has to reprogram the period, so ignore
281 * PERF_EF_RELOAD, see the comment below.
282 */
283 if (flags & PERF_EF_RELOAD)
284 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
285
286 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100287 /*
288 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200289 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100290 * may have been left counting. If we don't do this step then we may
291 * get an interrupt too soon or *way* too late if the overflow has
292 * happened since disabling.
293 */
294 armpmu_event_set_period(event, hwc, hwc->idx);
295 armpmu->enable(hwc, hwc->idx);
296}
297
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200298static void
299armpmu_del(struct perf_event *event, int flags)
300{
301 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
302 struct hw_perf_event *hwc = &event->hw;
303 int idx = hwc->idx;
304
305 WARN_ON(idx < 0);
306
307 clear_bit(idx, cpuc->active_mask);
308 armpmu_stop(event, PERF_EF_UPDATE);
309 cpuc->events[idx] = NULL;
310 clear_bit(idx, cpuc->used_mask);
311
312 perf_event_update_userpage(event);
313}
314
Jamie Iles1b8873a2010-02-02 20:25:44 +0100315static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200316armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100317{
318 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
319 struct hw_perf_event *hwc = &event->hw;
320 int idx;
321 int err = 0;
322
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200323 perf_pmu_disable(event->pmu);
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200324
Jamie Iles1b8873a2010-02-02 20:25:44 +0100325 /* If we don't have a space for the counter then finish early. */
326 idx = armpmu->get_event_idx(cpuc, hwc);
327 if (idx < 0) {
328 err = idx;
329 goto out;
330 }
331
332 /*
333 * If there is an event in the counter we are going to use then make
334 * sure it is disabled.
335 */
336 event->hw.idx = idx;
337 armpmu->disable(hwc, idx);
338 cpuc->events[idx] = event;
339 set_bit(idx, cpuc->active_mask);
340
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200341 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
342 if (flags & PERF_EF_START)
343 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100344
345 /* Propagate our changes to the userspace mapping. */
346 perf_event_update_userpage(event);
347
348out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200349 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100350 return err;
351}
352
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200353static struct pmu pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100354
355static int
356validate_event(struct cpu_hw_events *cpuc,
357 struct perf_event *event)
358{
359 struct hw_perf_event fake_event = event->hw;
360
Will Deacon65b47112010-09-02 09:32:08 +0100361 if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF)
362 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100363
364 return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
365}
366
367static int
368validate_group(struct perf_event *event)
369{
370 struct perf_event *sibling, *leader = event->group_leader;
371 struct cpu_hw_events fake_pmu;
372
373 memset(&fake_pmu, 0, sizeof(fake_pmu));
374
375 if (!validate_event(&fake_pmu, leader))
376 return -ENOSPC;
377
378 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
379 if (!validate_event(&fake_pmu, sibling))
380 return -ENOSPC;
381 }
382
383 if (!validate_event(&fake_pmu, event))
384 return -ENOSPC;
385
386 return 0;
387}
388
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530389static irqreturn_t armpmu_platform_irq(int irq, void *dev)
390{
391 struct arm_pmu_platdata *plat = dev_get_platdata(&pmu_device->dev);
392
393 return plat->handle_irq(irq, dev, armpmu->handle_irq);
394}
395
Jamie Iles1b8873a2010-02-02 20:25:44 +0100396static int
397armpmu_reserve_hardware(void)
398{
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530399 struct arm_pmu_platdata *plat;
400 irq_handler_t handle_irq;
Will Deacon49c006b2010-04-29 17:13:24 +0100401 int i, err = -ENODEV, irq;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100402
Will Deacon49c006b2010-04-29 17:13:24 +0100403 pmu_device = reserve_pmu(ARM_PMU_DEVICE_CPU);
404 if (IS_ERR(pmu_device)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100405 pr_warning("unable to reserve pmu\n");
Will Deacon49c006b2010-04-29 17:13:24 +0100406 return PTR_ERR(pmu_device);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100407 }
408
Will Deacon49c006b2010-04-29 17:13:24 +0100409 init_pmu(ARM_PMU_DEVICE_CPU);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100410
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530411 plat = dev_get_platdata(&pmu_device->dev);
412 if (plat && plat->handle_irq)
413 handle_irq = armpmu_platform_irq;
414 else
415 handle_irq = armpmu->handle_irq;
416
Will Deacon49c006b2010-04-29 17:13:24 +0100417 if (pmu_device->num_resources < 1) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100418 pr_err("no irqs for PMUs defined\n");
419 return -ENODEV;
420 }
421
Will Deacon49c006b2010-04-29 17:13:24 +0100422 for (i = 0; i < pmu_device->num_resources; ++i) {
423 irq = platform_get_irq(pmu_device, i);
424 if (irq < 0)
425 continue;
426
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530427 err = request_irq(irq, handle_irq,
Will Deaconddee87f2010-02-25 15:04:14 +0100428 IRQF_DISABLED | IRQF_NOBALANCING,
429 "armpmu", NULL);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100430 if (err) {
Will Deacon49c006b2010-04-29 17:13:24 +0100431 pr_warning("unable to request IRQ%d for ARM perf "
432 "counters\n", irq);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100433 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700434#ifdef CONFIG_SMP
435 } else if (armpmu->secondary_enable) {
436 armpmu->secondary_enable(irq);
437#endif
Jamie Iles1b8873a2010-02-02 20:25:44 +0100438 }
439 }
440
441 if (err) {
Will Deacon49c006b2010-04-29 17:13:24 +0100442 for (i = i - 1; i >= 0; --i) {
443 irq = platform_get_irq(pmu_device, i);
444 if (irq >= 0)
445 free_irq(irq, NULL);
446 }
447 release_pmu(pmu_device);
448 pmu_device = NULL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100449 }
450
451 return err;
452}
453
454static void
455armpmu_release_hardware(void)
456{
Will Deacon49c006b2010-04-29 17:13:24 +0100457 int i, irq;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100458
Will Deacon49c006b2010-04-29 17:13:24 +0100459 for (i = pmu_device->num_resources - 1; i >= 0; --i) {
460 irq = platform_get_irq(pmu_device, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461 if (irq >= 0) {
Will Deacon49c006b2010-04-29 17:13:24 +0100462 free_irq(irq, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700463#ifdef CONFIG_SMP
464 if (armpmu->secondary_disable)
465 armpmu->secondary_disable(irq);
466#endif
467 }
Will Deacon49c006b2010-04-29 17:13:24 +0100468 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100469 armpmu->stop();
470
Will Deacon49c006b2010-04-29 17:13:24 +0100471 release_pmu(pmu_device);
472 pmu_device = NULL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100473}
474
475static atomic_t active_events = ATOMIC_INIT(0);
476static DEFINE_MUTEX(pmu_reserve_mutex);
477
478static void
479hw_perf_event_destroy(struct perf_event *event)
480{
481 if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
482 armpmu_release_hardware();
483 mutex_unlock(&pmu_reserve_mutex);
484 }
485}
486
487static int
488__hw_perf_event_init(struct perf_event *event)
489{
490 struct hw_perf_event *hwc = &event->hw;
491 int mapping, err;
492
493 /* Decode the generic type into an ARM event identifier. */
494 if (PERF_TYPE_HARDWARE == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000495 mapping = armpmu_map_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100496 } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
497 mapping = armpmu_map_cache_event(event->attr.config);
498 } else if (PERF_TYPE_RAW == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000499 mapping = armpmu_map_raw_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100500 } else {
501 pr_debug("event type %x not supported\n", event->attr.type);
502 return -EOPNOTSUPP;
503 }
504
505 if (mapping < 0) {
506 pr_debug("event %x:%llx not supported\n", event->attr.type,
507 event->attr.config);
508 return mapping;
509 }
510
511 /*
512 * Check whether we need to exclude the counter from certain modes.
513 * The ARM performance counters are on all of the time so if someone
514 * has asked us for some excludes then we have to fail.
515 */
516 if (event->attr.exclude_kernel || event->attr.exclude_user ||
517 event->attr.exclude_hv || event->attr.exclude_idle) {
518 pr_debug("ARM performance counters do not support "
519 "mode exclusion\n");
520 return -EPERM;
521 }
522
523 /*
524 * We don't assign an index until we actually place the event onto
525 * hardware. Use -1 to signify that we haven't decided where to put it
526 * yet. For SMP systems, each core has it's own PMU so we can't do any
527 * clever allocation or constraints checking at this point.
528 */
529 hwc->idx = -1;
530
531 /*
532 * Store the event encoding into the config_base field. config and
533 * event_base are unused as the only 2 things we need to know are
534 * the event mapping and the counter to use. The counter to use is
535 * also the indx and the config_base is the event type.
536 */
537 hwc->config_base = (unsigned long)mapping;
538 hwc->config = 0;
539 hwc->event_base = 0;
540
541 if (!hwc->sample_period) {
542 hwc->sample_period = armpmu->max_period;
543 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200544 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100545 }
546
547 err = 0;
548 if (event->group_leader != event) {
549 err = validate_group(event);
550 if (err)
551 return -EINVAL;
552 }
553
554 return err;
555}
556
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200557static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100558{
559 int err = 0;
560
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200561 switch (event->attr.type) {
562 case PERF_TYPE_RAW:
563 case PERF_TYPE_HARDWARE:
564 case PERF_TYPE_HW_CACHE:
565 break;
566
567 default:
568 return -ENOENT;
569 }
570
Jamie Iles1b8873a2010-02-02 20:25:44 +0100571 if (!armpmu)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200572 return -ENODEV;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100573
574 event->destroy = hw_perf_event_destroy;
575
576 if (!atomic_inc_not_zero(&active_events)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100577 mutex_lock(&pmu_reserve_mutex);
578 if (atomic_read(&active_events) == 0) {
579 err = armpmu_reserve_hardware();
580 }
581
582 if (!err)
583 atomic_inc(&active_events);
584 mutex_unlock(&pmu_reserve_mutex);
585 }
586
587 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200588 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100589
590 err = __hw_perf_event_init(event);
591 if (err)
592 hw_perf_event_destroy(event);
593
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200594 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100595}
596
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200597static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100598{
599 /* Enable all of the perf events on hardware. */
Will Deaconf4f38432011-07-01 14:38:12 +0100600 int idx, enabled = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100601 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
602
603 if (!armpmu)
604 return;
605
606 for (idx = 0; idx <= armpmu->num_events; ++idx) {
607 struct perf_event *event = cpuc->events[idx];
608
609 if (!event)
610 continue;
611
612 armpmu->enable(&event->hw, idx);
Will Deaconf4f38432011-07-01 14:38:12 +0100613 enabled = 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100614 }
615
Will Deaconf4f38432011-07-01 14:38:12 +0100616 if (enabled)
617 armpmu->start();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100618}
619
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200620static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100621{
622 if (armpmu)
623 armpmu->stop();
624}
625
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200626static struct pmu pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200627 .pmu_enable = armpmu_enable,
628 .pmu_disable = armpmu_disable,
629 .event_init = armpmu_event_init,
630 .add = armpmu_add,
631 .del = armpmu_del,
632 .start = armpmu_start,
633 .stop = armpmu_stop,
634 .read = armpmu_read,
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200635};
636
Will Deacon43eab872010-11-13 19:04:32 +0000637/* Include the PMU-specific implementations. */
638#include "perf_event_xscale.c"
639#include "perf_event_v6.c"
640#include "perf_event_v7.c"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641#include "perf_event_msm.c"
642#include "perf_event_msm_l2.c"
643#include "perf_event_msm_krait.c"
644#include "perf_event_msm_krait_l2.c"
Will Deacon49e6a322010-04-30 11:33:33 +0100645
Will Deacon574b69c2011-03-25 13:13:34 +0100646/*
647 * Ensure the PMU has sane values out of reset.
648 * This requires SMP to be available, so exists as a separate initcall.
649 */
650static int __init
651armpmu_reset(void)
652{
653 if (armpmu && armpmu->reset)
654 return on_each_cpu(armpmu->reset, NULL, 1);
655 return 0;
656}
657arch_initcall(armpmu_reset);
658
Jamie Iles1b8873a2010-02-02 20:25:44 +0100659static int __init
660init_hw_perf_events(void)
661{
662 unsigned long cpuid = read_cpuid_id();
663 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
664 unsigned long part_number = (cpuid & 0xFFF0);
665
Will Deacon49e6a322010-04-30 11:33:33 +0100666 /* ARM Ltd CPUs. */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100667 if (0x41 == implementor) {
668 switch (part_number) {
669 case 0xB360: /* ARM1136 */
670 case 0xB560: /* ARM1156 */
671 case 0xB760: /* ARM1176 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000672 armpmu = armv6pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100673 break;
674 case 0xB020: /* ARM11mpcore */
Will Deacon3cb314b2010-11-13 17:37:46 +0000675 armpmu = armv6mpcore_pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100676 break;
Jean PIHET796d1292010-01-26 18:51:05 +0100677 case 0xC080: /* Cortex-A8 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000678 armpmu = armv7_a8_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100679 break;
680 case 0xC090: /* Cortex-A9 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000681 armpmu = armv7_a9_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100682 break;
Will Deacon49e6a322010-04-30 11:33:33 +0100683 }
684 /* Intel CPUs [xscale]. */
685 } else if (0x69 == implementor) {
686 part_number = (cpuid >> 13) & 0x7;
687 switch (part_number) {
688 case 1:
Will Deacon3cb314b2010-11-13 17:37:46 +0000689 armpmu = xscale1pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100690 break;
691 case 2:
Will Deacon3cb314b2010-11-13 17:37:46 +0000692 armpmu = xscale2pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100693 break;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100694 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695 /* Qualcomm CPUs */
696 } else if (0x51 == implementor) {
697 switch (part_number) {
698 case 0x00F0: /* 8x50 & 7x30*/
699 armpmu = armv7_scorpion_pmu_init();
700 break;
701 case 0x02D0: /* 8x60 */
702 armpmu = armv7_scorpionmp_pmu_init();
703 scorpionmp_l2_pmu_init();
704 break;
705 case 0x0490: /* 8960 sim */
706 case 0x04D0: /* 8960 */
707 armpmu = armv7_krait_pmu_init();
708 krait_l2_pmu_init();
709 break;
710 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100711 }
712
Will Deacon49e6a322010-04-30 11:33:33 +0100713 if (armpmu) {
Jean PIHET796d1292010-01-26 18:51:05 +0100714 pr_info("enabled with %s PMU driver, %d counters available\n",
Will Deacon62994832010-11-13 18:45:27 +0000715 armpmu->name, armpmu->num_events);
Will Deacon49e6a322010-04-30 11:33:33 +0100716 } else {
717 pr_info("no hardware support available\n");
Will Deacon49e6a322010-04-30 11:33:33 +0100718 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100719
Peter Zijlstra2e80a822010-11-17 23:17:36 +0100720 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200721
Jamie Iles1b8873a2010-02-02 20:25:44 +0100722 return 0;
723}
Peter Zijlstra004417a2010-11-25 18:38:29 +0100724early_initcall(init_hw_perf_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100725
726/*
727 * Callchain handling code.
728 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100729
730/*
731 * The registers we're interested in are at the end of the variable
732 * length saved register structure. The fp points at the end of this
733 * structure so the address of this struct is:
734 * (struct frame_tail *)(xxx->fp)-1
735 *
736 * This code has been adapted from the ARM OProfile support.
737 */
738struct frame_tail {
Will Deacon4d6b7a72010-11-30 18:15:53 +0100739 struct frame_tail __user *fp;
740 unsigned long sp;
741 unsigned long lr;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100742} __attribute__((packed));
743
744/*
745 * Get the return address for a single stackframe and return a pointer to the
746 * next frame tail.
747 */
Will Deacon4d6b7a72010-11-30 18:15:53 +0100748static struct frame_tail __user *
749user_backtrace(struct frame_tail __user *tail,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100750 struct perf_callchain_entry *entry)
751{
752 struct frame_tail buftail;
753
754 /* Also check accessibility of one struct frame_tail beyond */
755 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
756 return NULL;
757 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
758 return NULL;
759
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200760 perf_callchain_store(entry, buftail.lr);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100761
762 /*
763 * Frame pointers should strictly progress back up the stack
764 * (towards higher addresses).
765 */
Rabin Vincentcb061992011-02-09 11:35:12 +0100766 if (tail + 1 >= buftail.fp)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100767 return NULL;
768
769 return buftail.fp - 1;
770}
771
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200772void
773perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100774{
Will Deacon4d6b7a72010-11-30 18:15:53 +0100775 struct frame_tail __user *tail;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100776
Jamie Iles1b8873a2010-02-02 20:25:44 +0100777
Will Deacon4d6b7a72010-11-30 18:15:53 +0100778 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100779
Sonny Rao860ad782011-04-18 22:12:59 +0100780 while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
781 tail && !((unsigned long)tail & 0x3))
Jamie Iles1b8873a2010-02-02 20:25:44 +0100782 tail = user_backtrace(tail, entry);
783}
784
785/*
786 * Gets called by walk_stackframe() for every stackframe. This will be called
787 * whist unwinding the stackframe and is like a subroutine return so we use
788 * the PC.
789 */
790static int
791callchain_trace(struct stackframe *fr,
792 void *data)
793{
794 struct perf_callchain_entry *entry = data;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200795 perf_callchain_store(entry, fr->pc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100796 return 0;
797}
798
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200799void
800perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100801{
802 struct stackframe fr;
803
Jamie Iles1b8873a2010-02-02 20:25:44 +0100804 fr.fp = regs->ARM_fp;
805 fr.sp = regs->ARM_sp;
806 fr.lr = regs->ARM_lr;
807 fr.pc = regs->ARM_pc;
808 walk_stackframe(&fr, callchain_trace, entry);
809}