blob: d507fe148e00f3054e44616d6798eb3c261102db [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
Will Deacon49c006b2010-04-29 17:13:24 +010029static struct platform_device *pmu_device;
Jamie Iles1b8873a2010-02-02 20:25:44 +010030
31/*
32 * Hardware lock to serialize accesses to PMU registers. Needed for the
33 * read/modify/write sequences.
34 */
Will Deacon961ec6da2010-12-02 18:01:49 +010035static DEFINE_RAW_SPINLOCK(pmu_lock);
Jamie Iles1b8873a2010-02-02 20:25:44 +010036
37/*
38 * ARMv6 supports a maximum of 3 events, starting from index 1. If we add
39 * another platform that supports more, we need to increase this to be the
40 * largest of all platforms.
Jean PIHET796d1292010-01-26 18:51:05 +010041 *
42 * ARMv7 supports up to 32 events:
43 * cycle counter CCNT + 31 events counters CNT0..30.
44 * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
Jamie Iles1b8873a2010-02-02 20:25:44 +010045 */
Jean PIHET796d1292010-01-26 18:51:05 +010046#define ARMPMU_MAX_HWEVENTS 33
Jamie Iles1b8873a2010-02-02 20:25:44 +010047
48/* The events for a given CPU. */
49struct cpu_hw_events {
50 /*
51 * The events that are active on the CPU for the given index. Index 0
52 * is reserved.
53 */
54 struct perf_event *events[ARMPMU_MAX_HWEVENTS];
55
56 /*
57 * A 1 bit for an index indicates that the counter is being used for
58 * an event. A 0 means that the counter can be used.
59 */
60 unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
61
62 /*
63 * A 1 bit for an index indicates that the counter is actively being
64 * used.
65 */
66 unsigned long active_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
67};
Will Deacon4d6b7a72010-11-30 18:15:53 +010068static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
Will Deacon181193f2010-04-30 11:32:44 +010069
Jamie Iles1b8873a2010-02-02 20:25:44 +010070struct arm_pmu {
Will Deacon181193f2010-04-30 11:32:44 +010071 enum arm_perf_pmu_ids id;
Will Deacon0b390e22011-07-27 15:18:59 +010072 cpumask_t active_irqs;
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);
75 void (*enable)(struct hw_perf_event *evt, int idx);
76 void (*disable)(struct hw_perf_event *evt, int idx);
Jamie Iles1b8873a2010-02-02 20:25:44 +010077 int (*get_event_idx)(struct cpu_hw_events *cpuc,
78 struct hw_perf_event *hwc);
79 u32 (*read_counter)(int idx);
80 void (*write_counter)(int idx, u32 val);
81 void (*start)(void);
82 void (*stop)(void);
Will Deacon574b69c2011-03-25 13:13:34 +010083 void (*reset)(void *);
Will Deacon84fee972010-11-13 17:13:56 +000084 const unsigned (*cache_map)[PERF_COUNT_HW_CACHE_MAX]
85 [PERF_COUNT_HW_CACHE_OP_MAX]
86 [PERF_COUNT_HW_CACHE_RESULT_MAX];
87 const unsigned (*event_map)[PERF_COUNT_HW_MAX];
88 u32 raw_event_mask;
Jamie Iles1b8873a2010-02-02 20:25:44 +010089 int num_events;
90 u64 max_period;
91};
92
93/* Set at runtime when we know what CPU type we are. */
Mark Rutlanda6c93af2011-04-15 11:14:38 +010094static struct arm_pmu *armpmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +010095
Will Deacon181193f2010-04-30 11:32:44 +010096enum arm_perf_pmu_ids
97armpmu_get_pmu_id(void)
98{
99 int id = -ENODEV;
100
101 if (armpmu != NULL)
102 id = armpmu->id;
103
104 return id;
105}
106EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
107
Will Deacon929f5192010-04-30 11:34:26 +0100108int
109armpmu_get_max_events(void)
110{
111 int max_events = 0;
112
113 if (armpmu != NULL)
114 max_events = armpmu->num_events;
115
116 return max_events;
117}
118EXPORT_SYMBOL_GPL(armpmu_get_max_events);
119
Matt Fleming3bf101b2010-09-27 20:22:24 +0100120int perf_num_counters(void)
121{
122 return armpmu_get_max_events();
123}
124EXPORT_SYMBOL_GPL(perf_num_counters);
125
Jamie Iles1b8873a2010-02-02 20:25:44 +0100126#define HW_OP_UNSUPPORTED 0xFFFF
127
128#define C(_x) \
129 PERF_COUNT_HW_CACHE_##_x
130
131#define CACHE_OP_UNSUPPORTED 0xFFFF
132
Jamie Iles1b8873a2010-02-02 20:25:44 +0100133static int
134armpmu_map_cache_event(u64 config)
135{
136 unsigned int cache_type, cache_op, cache_result, ret;
137
138 cache_type = (config >> 0) & 0xff;
139 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
140 return -EINVAL;
141
142 cache_op = (config >> 8) & 0xff;
143 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
144 return -EINVAL;
145
146 cache_result = (config >> 16) & 0xff;
147 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
148 return -EINVAL;
149
Will Deacon84fee972010-11-13 17:13:56 +0000150 ret = (int)(*armpmu->cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100151
152 if (ret == CACHE_OP_UNSUPPORTED)
153 return -ENOENT;
154
155 return ret;
156}
157
158static int
Will Deacon84fee972010-11-13 17:13:56 +0000159armpmu_map_event(u64 config)
160{
161 int mapping = (*armpmu->event_map)[config];
162 return mapping == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : mapping;
163}
164
165static int
166armpmu_map_raw_event(u64 config)
167{
168 return (int)(config & armpmu->raw_event_mask);
169}
170
171static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100172armpmu_event_set_period(struct perf_event *event,
173 struct hw_perf_event *hwc,
174 int idx)
175{
Peter Zijlstrae7850592010-05-21 14:43:08 +0200176 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100177 s64 period = hwc->sample_period;
178 int ret = 0;
179
180 if (unlikely(left <= -period)) {
181 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200182 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100183 hwc->last_period = period;
184 ret = 1;
185 }
186
187 if (unlikely(left <= 0)) {
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 (left > (s64)armpmu->max_period)
195 left = armpmu->max_period;
196
Peter Zijlstrae7850592010-05-21 14:43:08 +0200197 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100198
199 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
200
201 perf_event_update_userpage(event);
202
203 return ret;
204}
205
206static u64
207armpmu_event_update(struct perf_event *event,
208 struct hw_perf_event *hwc,
Will Deacona7378232011-03-25 17:12:37 +0100209 int idx, int overflow)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100210{
Will Deacona7378232011-03-25 17:12:37 +0100211 u64 delta, prev_raw_count, new_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100212
213again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200214 prev_raw_count = local64_read(&hwc->prev_count);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100215 new_raw_count = armpmu->read_counter(idx);
216
Peter Zijlstrae7850592010-05-21 14:43:08 +0200217 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100218 new_raw_count) != prev_raw_count)
219 goto again;
220
Will Deacona7378232011-03-25 17:12:37 +0100221 new_raw_count &= armpmu->max_period;
222 prev_raw_count &= armpmu->max_period;
223
224 if (overflow)
Will Deacon67597882011-04-05 14:01:24 +0100225 delta = armpmu->max_period - prev_raw_count + new_raw_count + 1;
Will Deacona7378232011-03-25 17:12:37 +0100226 else
227 delta = new_raw_count - prev_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100228
Peter Zijlstrae7850592010-05-21 14:43:08 +0200229 local64_add(delta, &event->count);
230 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100231
232 return new_raw_count;
233}
234
235static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100236armpmu_read(struct perf_event *event)
237{
238 struct hw_perf_event *hwc = &event->hw;
239
240 /* Don't read disabled counters! */
241 if (hwc->idx < 0)
242 return;
243
Will Deacona7378232011-03-25 17:12:37 +0100244 armpmu_event_update(event, hwc, hwc->idx, 0);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100245}
246
247static void
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200248armpmu_stop(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100249{
250 struct hw_perf_event *hwc = &event->hw;
251
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200252 if (!armpmu)
253 return;
254
255 /*
256 * ARM pmu always has to update the counter, so ignore
257 * PERF_EF_UPDATE, see comments in armpmu_start().
258 */
259 if (!(hwc->state & PERF_HES_STOPPED)) {
260 armpmu->disable(hwc, hwc->idx);
261 barrier(); /* why? */
Will Deacona7378232011-03-25 17:12:37 +0100262 armpmu_event_update(event, hwc, hwc->idx, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200263 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
264 }
265}
266
267static void
268armpmu_start(struct perf_event *event, int flags)
269{
270 struct hw_perf_event *hwc = &event->hw;
271
272 if (!armpmu)
273 return;
274
275 /*
276 * ARM pmu always has to reprogram the period, so ignore
277 * PERF_EF_RELOAD, see the comment below.
278 */
279 if (flags & PERF_EF_RELOAD)
280 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
281
282 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100283 /*
284 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200285 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100286 * may have been left counting. If we don't do this step then we may
287 * get an interrupt too soon or *way* too late if the overflow has
288 * happened since disabling.
289 */
290 armpmu_event_set_period(event, hwc, hwc->idx);
291 armpmu->enable(hwc, hwc->idx);
292}
293
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200294static void
295armpmu_del(struct perf_event *event, int flags)
296{
297 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
298 struct hw_perf_event *hwc = &event->hw;
299 int idx = hwc->idx;
300
301 WARN_ON(idx < 0);
302
303 clear_bit(idx, cpuc->active_mask);
304 armpmu_stop(event, PERF_EF_UPDATE);
305 cpuc->events[idx] = NULL;
306 clear_bit(idx, cpuc->used_mask);
307
308 perf_event_update_userpage(event);
309}
310
Jamie Iles1b8873a2010-02-02 20:25:44 +0100311static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200312armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100313{
314 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
315 struct hw_perf_event *hwc = &event->hw;
316 int idx;
317 int err = 0;
318
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200319 perf_pmu_disable(event->pmu);
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200320
Jamie Iles1b8873a2010-02-02 20:25:44 +0100321 /* If we don't have a space for the counter then finish early. */
322 idx = armpmu->get_event_idx(cpuc, hwc);
323 if (idx < 0) {
324 err = idx;
325 goto out;
326 }
327
328 /*
329 * If there is an event in the counter we are going to use then make
330 * sure it is disabled.
331 */
332 event->hw.idx = idx;
333 armpmu->disable(hwc, idx);
334 cpuc->events[idx] = event;
335 set_bit(idx, cpuc->active_mask);
336
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200337 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
338 if (flags & PERF_EF_START)
339 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100340
341 /* Propagate our changes to the userspace mapping. */
342 perf_event_update_userpage(event);
343
344out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200345 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100346 return err;
347}
348
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200349static struct pmu pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100350
351static int
352validate_event(struct cpu_hw_events *cpuc,
353 struct perf_event *event)
354{
355 struct hw_perf_event fake_event = event->hw;
356
Will Deacon65b47112010-09-02 09:32:08 +0100357 if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF)
358 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100359
360 return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
361}
362
363static int
364validate_group(struct perf_event *event)
365{
366 struct perf_event *sibling, *leader = event->group_leader;
367 struct cpu_hw_events fake_pmu;
368
369 memset(&fake_pmu, 0, sizeof(fake_pmu));
370
371 if (!validate_event(&fake_pmu, leader))
372 return -ENOSPC;
373
374 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
375 if (!validate_event(&fake_pmu, sibling))
376 return -ENOSPC;
377 }
378
379 if (!validate_event(&fake_pmu, event))
380 return -ENOSPC;
381
382 return 0;
383}
384
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530385static irqreturn_t armpmu_platform_irq(int irq, void *dev)
386{
387 struct arm_pmu_platdata *plat = dev_get_platdata(&pmu_device->dev);
388
389 return plat->handle_irq(irq, dev, armpmu->handle_irq);
390}
391
Will Deacon0b390e22011-07-27 15:18:59 +0100392static void
393armpmu_release_hardware(void)
394{
395 int i, irq, irqs;
396
397 irqs = min(pmu_device->num_resources, num_possible_cpus());
398
399 for (i = 0; i < irqs; ++i) {
400 if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs))
401 continue;
402 irq = platform_get_irq(pmu_device, i);
403 if (irq >= 0)
404 free_irq(irq, NULL);
405 }
406
407 armpmu->stop();
408 release_pmu(ARM_PMU_DEVICE_CPU);
409}
410
Jamie Iles1b8873a2010-02-02 20:25:44 +0100411static int
412armpmu_reserve_hardware(void)
413{
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530414 struct arm_pmu_platdata *plat;
415 irq_handler_t handle_irq;
Will Deaconb0e89592011-07-26 22:10:28 +0100416 int i, err, irq, irqs;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100417
Will Deaconb0e89592011-07-26 22:10:28 +0100418 err = reserve_pmu(ARM_PMU_DEVICE_CPU);
419 if (err) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100420 pr_warning("unable to reserve pmu\n");
Will Deaconb0e89592011-07-26 22:10:28 +0100421 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100422 }
423
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530424 plat = dev_get_platdata(&pmu_device->dev);
425 if (plat && plat->handle_irq)
426 handle_irq = armpmu_platform_irq;
427 else
428 handle_irq = armpmu->handle_irq;
429
Will Deacon0b390e22011-07-27 15:18:59 +0100430 irqs = min(pmu_device->num_resources, num_possible_cpus());
Will Deaconb0e89592011-07-26 22:10:28 +0100431 if (irqs < 1) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100432 pr_err("no irqs for PMUs defined\n");
433 return -ENODEV;
434 }
435
Will Deaconb0e89592011-07-26 22:10:28 +0100436 for (i = 0; i < irqs; ++i) {
Will Deacon0b390e22011-07-27 15:18:59 +0100437 err = 0;
Will Deacon49c006b2010-04-29 17:13:24 +0100438 irq = platform_get_irq(pmu_device, i);
439 if (irq < 0)
440 continue;
441
Will Deaconb0e89592011-07-26 22:10:28 +0100442 /*
443 * If we have a single PMU interrupt that we can't shift,
444 * assume that we're running on a uniprocessor machine and
Will Deacon0b390e22011-07-27 15:18:59 +0100445 * continue. Otherwise, continue without this interrupt.
Will Deaconb0e89592011-07-26 22:10:28 +0100446 */
Will Deacon0b390e22011-07-27 15:18:59 +0100447 if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
448 pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
449 irq, i);
450 continue;
Will Deaconb0e89592011-07-26 22:10:28 +0100451 }
452
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530453 err = request_irq(irq, handle_irq,
Will Deaconddee87f2010-02-25 15:04:14 +0100454 IRQF_DISABLED | IRQF_NOBALANCING,
Will Deaconb0e89592011-07-26 22:10:28 +0100455 "arm-pmu", NULL);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100456 if (err) {
Will Deaconb0e89592011-07-26 22:10:28 +0100457 pr_err("unable to request IRQ%d for ARM PMU counters\n",
458 irq);
Will Deacon0b390e22011-07-27 15:18:59 +0100459 armpmu_release_hardware();
460 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100461 }
Will Deacon0b390e22011-07-27 15:18:59 +0100462
463 cpumask_set_cpu(i, &armpmu->active_irqs);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100464 }
465
Will Deacon0b390e22011-07-27 15:18:59 +0100466 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100467}
468
469static atomic_t active_events = ATOMIC_INIT(0);
470static DEFINE_MUTEX(pmu_reserve_mutex);
471
472static void
473hw_perf_event_destroy(struct perf_event *event)
474{
475 if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
476 armpmu_release_hardware();
477 mutex_unlock(&pmu_reserve_mutex);
478 }
479}
480
481static int
482__hw_perf_event_init(struct perf_event *event)
483{
484 struct hw_perf_event *hwc = &event->hw;
485 int mapping, err;
486
487 /* Decode the generic type into an ARM event identifier. */
488 if (PERF_TYPE_HARDWARE == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000489 mapping = armpmu_map_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100490 } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
491 mapping = armpmu_map_cache_event(event->attr.config);
492 } else if (PERF_TYPE_RAW == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000493 mapping = armpmu_map_raw_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100494 } else {
495 pr_debug("event type %x not supported\n", event->attr.type);
496 return -EOPNOTSUPP;
497 }
498
499 if (mapping < 0) {
500 pr_debug("event %x:%llx not supported\n", event->attr.type,
501 event->attr.config);
502 return mapping;
503 }
504
505 /*
506 * Check whether we need to exclude the counter from certain modes.
507 * The ARM performance counters are on all of the time so if someone
508 * has asked us for some excludes then we have to fail.
509 */
510 if (event->attr.exclude_kernel || event->attr.exclude_user ||
511 event->attr.exclude_hv || event->attr.exclude_idle) {
512 pr_debug("ARM performance counters do not support "
513 "mode exclusion\n");
514 return -EPERM;
515 }
516
517 /*
518 * 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.
522 */
523 hwc->idx = -1;
524
525 /*
526 * Store the event encoding into the config_base field. config and
527 * event_base are unused as the only 2 things we need to know are
528 * the event mapping and the counter to use. The counter to use is
529 * also the indx and the config_base is the event type.
530 */
531 hwc->config_base = (unsigned long)mapping;
532 hwc->config = 0;
533 hwc->event_base = 0;
534
535 if (!hwc->sample_period) {
536 hwc->sample_period = armpmu->max_period;
537 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200538 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100539 }
540
541 err = 0;
542 if (event->group_leader != event) {
543 err = validate_group(event);
544 if (err)
545 return -EINVAL;
546 }
547
548 return err;
549}
550
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200551static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100552{
553 int err = 0;
554
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200555 switch (event->attr.type) {
556 case PERF_TYPE_RAW:
557 case PERF_TYPE_HARDWARE:
558 case PERF_TYPE_HW_CACHE:
559 break;
560
561 default:
562 return -ENOENT;
563 }
564
Jamie Iles1b8873a2010-02-02 20:25:44 +0100565 if (!armpmu)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200566 return -ENODEV;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100567
568 event->destroy = hw_perf_event_destroy;
569
570 if (!atomic_inc_not_zero(&active_events)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100571 mutex_lock(&pmu_reserve_mutex);
572 if (atomic_read(&active_events) == 0) {
573 err = armpmu_reserve_hardware();
574 }
575
576 if (!err)
577 atomic_inc(&active_events);
578 mutex_unlock(&pmu_reserve_mutex);
579 }
580
581 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200582 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100583
584 err = __hw_perf_event_init(event);
585 if (err)
586 hw_perf_event_destroy(event);
587
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200588 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100589}
590
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200591static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100592{
593 /* Enable all of the perf events on hardware. */
Will Deaconf4f38432011-07-01 14:38:12 +0100594 int idx, enabled = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100595 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
596
597 if (!armpmu)
598 return;
599
600 for (idx = 0; idx <= armpmu->num_events; ++idx) {
601 struct perf_event *event = cpuc->events[idx];
602
603 if (!event)
604 continue;
605
606 armpmu->enable(&event->hw, idx);
Will Deaconf4f38432011-07-01 14:38:12 +0100607 enabled = 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100608 }
609
Will Deaconf4f38432011-07-01 14:38:12 +0100610 if (enabled)
611 armpmu->start();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100612}
613
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200614static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100615{
616 if (armpmu)
617 armpmu->stop();
618}
619
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200620static struct pmu pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200621 .pmu_enable = armpmu_enable,
622 .pmu_disable = armpmu_disable,
623 .event_init = armpmu_event_init,
624 .add = armpmu_add,
625 .del = armpmu_del,
626 .start = armpmu_start,
627 .stop = armpmu_stop,
628 .read = armpmu_read,
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200629};
630
Will Deacon43eab872010-11-13 19:04:32 +0000631/* Include the PMU-specific implementations. */
632#include "perf_event_xscale.c"
633#include "perf_event_v6.c"
634#include "perf_event_v7.c"
Will Deacon49e6a322010-04-30 11:33:33 +0100635
Will Deacon574b69c2011-03-25 13:13:34 +0100636/*
637 * Ensure the PMU has sane values out of reset.
638 * This requires SMP to be available, so exists as a separate initcall.
639 */
640static int __init
641armpmu_reset(void)
642{
643 if (armpmu && armpmu->reset)
644 return on_each_cpu(armpmu->reset, NULL, 1);
645 return 0;
646}
647arch_initcall(armpmu_reset);
648
Will Deaconb0e89592011-07-26 22:10:28 +0100649/*
650 * PMU platform driver and devicetree bindings.
651 */
652static struct of_device_id armpmu_of_device_ids[] = {
653 {.compatible = "arm,cortex-a9-pmu"},
654 {.compatible = "arm,cortex-a8-pmu"},
655 {.compatible = "arm,arm1136-pmu"},
656 {.compatible = "arm,arm1176-pmu"},
657 {},
658};
659
660static struct platform_device_id armpmu_plat_device_ids[] = {
661 {.name = "arm-pmu"},
662 {},
663};
664
665static int __devinit armpmu_device_probe(struct platform_device *pdev)
666{
667 pmu_device = pdev;
668 return 0;
669}
670
671static struct platform_driver armpmu_driver = {
672 .driver = {
673 .name = "arm-pmu",
674 .of_match_table = armpmu_of_device_ids,
675 },
676 .probe = armpmu_device_probe,
677 .id_table = armpmu_plat_device_ids,
678};
679
680static int __init register_pmu_driver(void)
681{
682 return platform_driver_register(&armpmu_driver);
683}
684device_initcall(register_pmu_driver);
685
686/*
687 * CPU PMU identification and registration.
688 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100689static int __init
690init_hw_perf_events(void)
691{
692 unsigned long cpuid = read_cpuid_id();
693 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
694 unsigned long part_number = (cpuid & 0xFFF0);
695
Will Deacon49e6a322010-04-30 11:33:33 +0100696 /* ARM Ltd CPUs. */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100697 if (0x41 == implementor) {
698 switch (part_number) {
699 case 0xB360: /* ARM1136 */
700 case 0xB560: /* ARM1156 */
701 case 0xB760: /* ARM1176 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000702 armpmu = armv6pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100703 break;
704 case 0xB020: /* ARM11mpcore */
Will Deacon3cb314b2010-11-13 17:37:46 +0000705 armpmu = armv6mpcore_pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100706 break;
Jean PIHET796d1292010-01-26 18:51:05 +0100707 case 0xC080: /* Cortex-A8 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000708 armpmu = armv7_a8_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100709 break;
710 case 0xC090: /* Cortex-A9 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000711 armpmu = armv7_a9_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100712 break;
Will Deacon0c205cb2011-06-03 17:40:15 +0100713 case 0xC050: /* Cortex-A5 */
714 armpmu = armv7_a5_pmu_init();
715 break;
Will Deacon14abd032011-01-19 14:24:38 +0000716 case 0xC0F0: /* Cortex-A15 */
717 armpmu = armv7_a15_pmu_init();
718 break;
Will Deacon49e6a322010-04-30 11:33:33 +0100719 }
720 /* Intel CPUs [xscale]. */
721 } else if (0x69 == implementor) {
722 part_number = (cpuid >> 13) & 0x7;
723 switch (part_number) {
724 case 1:
Will Deacon3cb314b2010-11-13 17:37:46 +0000725 armpmu = xscale1pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100726 break;
727 case 2:
Will Deacon3cb314b2010-11-13 17:37:46 +0000728 armpmu = xscale2pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100729 break;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100730 }
731 }
732
Will Deacon49e6a322010-04-30 11:33:33 +0100733 if (armpmu) {
Jean PIHET796d1292010-01-26 18:51:05 +0100734 pr_info("enabled with %s PMU driver, %d counters available\n",
Will Deacon62994832010-11-13 18:45:27 +0000735 armpmu->name, armpmu->num_events);
Will Deacon49e6a322010-04-30 11:33:33 +0100736 } else {
737 pr_info("no hardware support available\n");
Will Deacon49e6a322010-04-30 11:33:33 +0100738 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100739
Peter Zijlstra2e80a822010-11-17 23:17:36 +0100740 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200741
Jamie Iles1b8873a2010-02-02 20:25:44 +0100742 return 0;
743}
Peter Zijlstra004417a2010-11-25 18:38:29 +0100744early_initcall(init_hw_perf_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100745
746/*
747 * Callchain handling code.
748 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100749
750/*
751 * The registers we're interested in are at the end of the variable
752 * length saved register structure. The fp points at the end of this
753 * structure so the address of this struct is:
754 * (struct frame_tail *)(xxx->fp)-1
755 *
756 * This code has been adapted from the ARM OProfile support.
757 */
758struct frame_tail {
Will Deacon4d6b7a72010-11-30 18:15:53 +0100759 struct frame_tail __user *fp;
760 unsigned long sp;
761 unsigned long lr;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100762} __attribute__((packed));
763
764/*
765 * Get the return address for a single stackframe and return a pointer to the
766 * next frame tail.
767 */
Will Deacon4d6b7a72010-11-30 18:15:53 +0100768static struct frame_tail __user *
769user_backtrace(struct frame_tail __user *tail,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100770 struct perf_callchain_entry *entry)
771{
772 struct frame_tail buftail;
773
774 /* Also check accessibility of one struct frame_tail beyond */
775 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
776 return NULL;
777 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
778 return NULL;
779
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200780 perf_callchain_store(entry, buftail.lr);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100781
782 /*
783 * Frame pointers should strictly progress back up the stack
784 * (towards higher addresses).
785 */
Rabin Vincentcb061992011-02-09 11:35:12 +0100786 if (tail + 1 >= buftail.fp)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100787 return NULL;
788
789 return buftail.fp - 1;
790}
791
Frederic Weisbecker56962b4442010-06-30 23:03:51 +0200792void
793perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100794{
Will Deacon4d6b7a72010-11-30 18:15:53 +0100795 struct frame_tail __user *tail;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100796
Jamie Iles1b8873a2010-02-02 20:25:44 +0100797
Will Deacon4d6b7a72010-11-30 18:15:53 +0100798 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100799
Sonny Rao860ad782011-04-18 22:12:59 +0100800 while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
801 tail && !((unsigned long)tail & 0x3))
Jamie Iles1b8873a2010-02-02 20:25:44 +0100802 tail = user_backtrace(tail, entry);
803}
804
805/*
806 * Gets called by walk_stackframe() for every stackframe. This will be called
807 * whist unwinding the stackframe and is like a subroutine return so we use
808 * the PC.
809 */
810static int
811callchain_trace(struct stackframe *fr,
812 void *data)
813{
814 struct perf_callchain_entry *entry = data;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200815 perf_callchain_store(entry, fr->pc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100816 return 0;
817}
818
Frederic Weisbecker56962b4442010-06-30 23:03:51 +0200819void
820perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100821{
822 struct stackframe fr;
823
Jamie Iles1b8873a2010-02-02 20:25:44 +0100824 fr.fp = regs->ARM_fp;
825 fr.sp = regs->ARM_sp;
826 fr.lr = regs->ARM_lr;
827 fr.pc = regs->ARM_pc;
828 walk_stackframe(&fr, callchain_trace, entry);
829}