blob: 438482ff749805ede01bd0d6978bcf59f0108367 [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/*
Will Deaconecf5a892011-07-19 22:43:28 +010038 * ARMv6 supports a maximum of 3 events, starting from index 0. If we add
Jamie Iles1b8873a2010-02-02 20:25:44 +010039 * 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 */
Will Deaconecf5a892011-07-19 22:43:28 +010046#define ARMPMU_MAX_HWEVENTS 32
Jamie Iles1b8873a2010-02-02 20:25:44 +010047
48/* The events for a given CPU. */
49struct cpu_hw_events {
50 /*
Will Deaconecf5a892011-07-19 22:43:28 +010051 * The events that are active on the CPU for the given index.
Jamie Iles1b8873a2010-02-02 20:25:44 +010052 */
53 struct perf_event *events[ARMPMU_MAX_HWEVENTS];
54
55 /*
56 * A 1 bit for an index indicates that the counter is being used for
57 * an event. A 0 means that the counter can be used.
58 */
59 unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
Jamie Iles1b8873a2010-02-02 20:25:44 +010060};
Will Deacon4d6b7a72010-11-30 18:15:53 +010061static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
Will Deacon181193f2010-04-30 11:32:44 +010062
Jamie Iles1b8873a2010-02-02 20:25:44 +010063struct arm_pmu {
Will Deacon181193f2010-04-30 11:32:44 +010064 enum arm_perf_pmu_ids id;
Will Deacon0b390e22011-07-27 15:18:59 +010065 cpumask_t active_irqs;
Will Deacon62994832010-11-13 18:45:27 +000066 const char *name;
Jamie Iles1b8873a2010-02-02 20:25:44 +010067 irqreturn_t (*handle_irq)(int irq_num, void *dev);
68 void (*enable)(struct hw_perf_event *evt, int idx);
69 void (*disable)(struct hw_perf_event *evt, int idx);
Jamie Iles1b8873a2010-02-02 20:25:44 +010070 int (*get_event_idx)(struct cpu_hw_events *cpuc,
71 struct hw_perf_event *hwc);
Will Deacon05d22fd2011-07-19 11:57:30 +010072 int (*set_event_filter)(struct hw_perf_event *evt,
73 struct perf_event_attr *attr);
Jamie Iles1b8873a2010-02-02 20:25:44 +010074 u32 (*read_counter)(int idx);
75 void (*write_counter)(int idx, u32 val);
76 void (*start)(void);
77 void (*stop)(void);
Will Deacon574b69c2011-03-25 13:13:34 +010078 void (*reset)(void *);
Will Deacon84fee972010-11-13 17:13:56 +000079 const unsigned (*cache_map)[PERF_COUNT_HW_CACHE_MAX]
80 [PERF_COUNT_HW_CACHE_OP_MAX]
81 [PERF_COUNT_HW_CACHE_RESULT_MAX];
82 const unsigned (*event_map)[PERF_COUNT_HW_MAX];
83 u32 raw_event_mask;
Jamie Iles1b8873a2010-02-02 20:25:44 +010084 int num_events;
85 u64 max_period;
86};
87
88/* Set at runtime when we know what CPU type we are. */
Mark Rutlanda6c93af2011-04-15 11:14:38 +010089static struct arm_pmu *armpmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +010090
Will Deacon181193f2010-04-30 11:32:44 +010091enum arm_perf_pmu_ids
92armpmu_get_pmu_id(void)
93{
94 int id = -ENODEV;
95
96 if (armpmu != NULL)
97 id = armpmu->id;
98
99 return id;
100}
101EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
102
Will Deacon929f5192010-04-30 11:34:26 +0100103int
104armpmu_get_max_events(void)
105{
106 int max_events = 0;
107
108 if (armpmu != NULL)
109 max_events = armpmu->num_events;
110
111 return max_events;
112}
113EXPORT_SYMBOL_GPL(armpmu_get_max_events);
114
Matt Fleming3bf101b2010-09-27 20:22:24 +0100115int perf_num_counters(void)
116{
117 return armpmu_get_max_events();
118}
119EXPORT_SYMBOL_GPL(perf_num_counters);
120
Jamie Iles1b8873a2010-02-02 20:25:44 +0100121#define HW_OP_UNSUPPORTED 0xFFFF
122
123#define C(_x) \
124 PERF_COUNT_HW_CACHE_##_x
125
126#define CACHE_OP_UNSUPPORTED 0xFFFF
127
Jamie Iles1b8873a2010-02-02 20:25:44 +0100128static int
129armpmu_map_cache_event(u64 config)
130{
131 unsigned int cache_type, cache_op, cache_result, ret;
132
133 cache_type = (config >> 0) & 0xff;
134 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
135 return -EINVAL;
136
137 cache_op = (config >> 8) & 0xff;
138 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
139 return -EINVAL;
140
141 cache_result = (config >> 16) & 0xff;
142 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
143 return -EINVAL;
144
Will Deacon84fee972010-11-13 17:13:56 +0000145 ret = (int)(*armpmu->cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100146
147 if (ret == CACHE_OP_UNSUPPORTED)
148 return -ENOENT;
149
150 return ret;
151}
152
153static int
Will Deacon84fee972010-11-13 17:13:56 +0000154armpmu_map_event(u64 config)
155{
156 int mapping = (*armpmu->event_map)[config];
157 return mapping == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : mapping;
158}
159
160static int
161armpmu_map_raw_event(u64 config)
162{
163 return (int)(config & armpmu->raw_event_mask);
164}
165
166static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100167armpmu_event_set_period(struct perf_event *event,
168 struct hw_perf_event *hwc,
169 int idx)
170{
Peter Zijlstrae7850592010-05-21 14:43:08 +0200171 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100172 s64 period = hwc->sample_period;
173 int ret = 0;
174
175 if (unlikely(left <= -period)) {
176 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200177 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100178 hwc->last_period = period;
179 ret = 1;
180 }
181
182 if (unlikely(left <= 0)) {
183 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200184 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100185 hwc->last_period = period;
186 ret = 1;
187 }
188
189 if (left > (s64)armpmu->max_period)
190 left = armpmu->max_period;
191
Peter Zijlstrae7850592010-05-21 14:43:08 +0200192 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100193
194 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
195
196 perf_event_update_userpage(event);
197
198 return ret;
199}
200
201static u64
202armpmu_event_update(struct perf_event *event,
203 struct hw_perf_event *hwc,
Will Deacona7378232011-03-25 17:12:37 +0100204 int idx, int overflow)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100205{
Will Deacona7378232011-03-25 17:12:37 +0100206 u64 delta, prev_raw_count, new_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100207
208again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200209 prev_raw_count = local64_read(&hwc->prev_count);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100210 new_raw_count = armpmu->read_counter(idx);
211
Peter Zijlstrae7850592010-05-21 14:43:08 +0200212 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100213 new_raw_count) != prev_raw_count)
214 goto again;
215
Will Deacona7378232011-03-25 17:12:37 +0100216 new_raw_count &= armpmu->max_period;
217 prev_raw_count &= armpmu->max_period;
218
219 if (overflow)
Will Deacon67597882011-04-05 14:01:24 +0100220 delta = armpmu->max_period - prev_raw_count + new_raw_count + 1;
Will Deacona7378232011-03-25 17:12:37 +0100221 else
222 delta = new_raw_count - prev_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100223
Peter Zijlstrae7850592010-05-21 14:43:08 +0200224 local64_add(delta, &event->count);
225 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100226
227 return new_raw_count;
228}
229
230static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100231armpmu_read(struct perf_event *event)
232{
233 struct hw_perf_event *hwc = &event->hw;
234
235 /* Don't read disabled counters! */
236 if (hwc->idx < 0)
237 return;
238
Will Deacona7378232011-03-25 17:12:37 +0100239 armpmu_event_update(event, hwc, hwc->idx, 0);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100240}
241
242static void
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200243armpmu_stop(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100244{
245 struct hw_perf_event *hwc = &event->hw;
246
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200247 /*
248 * ARM pmu always has to update the counter, so ignore
249 * PERF_EF_UPDATE, see comments in armpmu_start().
250 */
251 if (!(hwc->state & PERF_HES_STOPPED)) {
252 armpmu->disable(hwc, hwc->idx);
253 barrier(); /* why? */
Will Deacona7378232011-03-25 17:12:37 +0100254 armpmu_event_update(event, hwc, hwc->idx, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200255 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
256 }
257}
258
259static void
260armpmu_start(struct perf_event *event, int flags)
261{
262 struct hw_perf_event *hwc = &event->hw;
263
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200264 /*
265 * ARM pmu always has to reprogram the period, so ignore
266 * PERF_EF_RELOAD, see the comment below.
267 */
268 if (flags & PERF_EF_RELOAD)
269 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
270
271 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100272 /*
273 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200274 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100275 * may have been left counting. If we don't do this step then we may
276 * get an interrupt too soon or *way* too late if the overflow has
277 * happened since disabling.
278 */
279 armpmu_event_set_period(event, hwc, hwc->idx);
280 armpmu->enable(hwc, hwc->idx);
281}
282
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200283static void
284armpmu_del(struct perf_event *event, int flags)
285{
286 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
287 struct hw_perf_event *hwc = &event->hw;
288 int idx = hwc->idx;
289
290 WARN_ON(idx < 0);
291
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200292 armpmu_stop(event, PERF_EF_UPDATE);
293 cpuc->events[idx] = NULL;
294 clear_bit(idx, cpuc->used_mask);
295
296 perf_event_update_userpage(event);
297}
298
Jamie Iles1b8873a2010-02-02 20:25:44 +0100299static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200300armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100301{
302 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
303 struct hw_perf_event *hwc = &event->hw;
304 int idx;
305 int err = 0;
306
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200307 perf_pmu_disable(event->pmu);
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200308
Jamie Iles1b8873a2010-02-02 20:25:44 +0100309 /* If we don't have a space for the counter then finish early. */
310 idx = armpmu->get_event_idx(cpuc, hwc);
311 if (idx < 0) {
312 err = idx;
313 goto out;
314 }
315
316 /*
317 * If there is an event in the counter we are going to use then make
318 * sure it is disabled.
319 */
320 event->hw.idx = idx;
321 armpmu->disable(hwc, idx);
322 cpuc->events[idx] = event;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100323
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200324 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
325 if (flags & PERF_EF_START)
326 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100327
328 /* Propagate our changes to the userspace mapping. */
329 perf_event_update_userpage(event);
330
331out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200332 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100333 return err;
334}
335
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200336static struct pmu pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100337
338static int
339validate_event(struct cpu_hw_events *cpuc,
340 struct perf_event *event)
341{
342 struct hw_perf_event fake_event = event->hw;
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100343 struct pmu *leader_pmu = event->group_leader->pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100344
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100345 if (event->pmu != leader_pmu || event->state <= PERF_EVENT_STATE_OFF)
Will Deacon65b47112010-09-02 09:32:08 +0100346 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100347
348 return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
349}
350
351static int
352validate_group(struct perf_event *event)
353{
354 struct perf_event *sibling, *leader = event->group_leader;
355 struct cpu_hw_events fake_pmu;
356
357 memset(&fake_pmu, 0, sizeof(fake_pmu));
358
359 if (!validate_event(&fake_pmu, leader))
360 return -ENOSPC;
361
362 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
363 if (!validate_event(&fake_pmu, sibling))
364 return -ENOSPC;
365 }
366
367 if (!validate_event(&fake_pmu, event))
368 return -ENOSPC;
369
370 return 0;
371}
372
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530373static irqreturn_t armpmu_platform_irq(int irq, void *dev)
374{
375 struct arm_pmu_platdata *plat = dev_get_platdata(&pmu_device->dev);
376
377 return plat->handle_irq(irq, dev, armpmu->handle_irq);
378}
379
Will Deacon0b390e22011-07-27 15:18:59 +0100380static void
381armpmu_release_hardware(void)
382{
383 int i, irq, irqs;
384
385 irqs = min(pmu_device->num_resources, num_possible_cpus());
386
387 for (i = 0; i < irqs; ++i) {
388 if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs))
389 continue;
390 irq = platform_get_irq(pmu_device, i);
391 if (irq >= 0)
392 free_irq(irq, NULL);
393 }
394
395 armpmu->stop();
396 release_pmu(ARM_PMU_DEVICE_CPU);
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 Deaconb0e89592011-07-26 22:10:28 +0100404 int i, err, irq, irqs;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100405
Will Deaconb0e89592011-07-26 22:10:28 +0100406 err = reserve_pmu(ARM_PMU_DEVICE_CPU);
407 if (err) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100408 pr_warning("unable to reserve pmu\n");
Will Deaconb0e89592011-07-26 22:10:28 +0100409 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100410 }
411
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530412 plat = dev_get_platdata(&pmu_device->dev);
413 if (plat && plat->handle_irq)
414 handle_irq = armpmu_platform_irq;
415 else
416 handle_irq = armpmu->handle_irq;
417
Will Deacon0b390e22011-07-27 15:18:59 +0100418 irqs = min(pmu_device->num_resources, num_possible_cpus());
Will Deaconb0e89592011-07-26 22:10:28 +0100419 if (irqs < 1) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100420 pr_err("no irqs for PMUs defined\n");
421 return -ENODEV;
422 }
423
Will Deaconb0e89592011-07-26 22:10:28 +0100424 for (i = 0; i < irqs; ++i) {
Will Deacon0b390e22011-07-27 15:18:59 +0100425 err = 0;
Will Deacon49c006b2010-04-29 17:13:24 +0100426 irq = platform_get_irq(pmu_device, i);
427 if (irq < 0)
428 continue;
429
Will Deaconb0e89592011-07-26 22:10:28 +0100430 /*
431 * If we have a single PMU interrupt that we can't shift,
432 * assume that we're running on a uniprocessor machine and
Will Deacon0b390e22011-07-27 15:18:59 +0100433 * continue. Otherwise, continue without this interrupt.
Will Deaconb0e89592011-07-26 22:10:28 +0100434 */
Will Deacon0b390e22011-07-27 15:18:59 +0100435 if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
436 pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
437 irq, i);
438 continue;
Will Deaconb0e89592011-07-26 22:10:28 +0100439 }
440
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530441 err = request_irq(irq, handle_irq,
Will Deaconddee87f2010-02-25 15:04:14 +0100442 IRQF_DISABLED | IRQF_NOBALANCING,
Will Deaconb0e89592011-07-26 22:10:28 +0100443 "arm-pmu", NULL);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100444 if (err) {
Will Deaconb0e89592011-07-26 22:10:28 +0100445 pr_err("unable to request IRQ%d for ARM PMU counters\n",
446 irq);
Will Deacon0b390e22011-07-27 15:18:59 +0100447 armpmu_release_hardware();
448 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100449 }
Will Deacon0b390e22011-07-27 15:18:59 +0100450
451 cpumask_set_cpu(i, &armpmu->active_irqs);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100452 }
453
Will Deacon0b390e22011-07-27 15:18:59 +0100454 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100455}
456
457static atomic_t active_events = ATOMIC_INIT(0);
458static DEFINE_MUTEX(pmu_reserve_mutex);
459
460static void
461hw_perf_event_destroy(struct perf_event *event)
462{
463 if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
464 armpmu_release_hardware();
465 mutex_unlock(&pmu_reserve_mutex);
466 }
467}
468
469static int
Will Deacon05d22fd2011-07-19 11:57:30 +0100470event_requires_mode_exclusion(struct perf_event_attr *attr)
471{
472 return attr->exclude_idle || attr->exclude_user ||
473 attr->exclude_kernel || attr->exclude_hv;
474}
475
476static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100477__hw_perf_event_init(struct perf_event *event)
478{
479 struct hw_perf_event *hwc = &event->hw;
480 int mapping, err;
481
482 /* Decode the generic type into an ARM event identifier. */
483 if (PERF_TYPE_HARDWARE == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000484 mapping = armpmu_map_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100485 } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
486 mapping = armpmu_map_cache_event(event->attr.config);
487 } else if (PERF_TYPE_RAW == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000488 mapping = armpmu_map_raw_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100489 } else {
490 pr_debug("event type %x not supported\n", event->attr.type);
491 return -EOPNOTSUPP;
492 }
493
494 if (mapping < 0) {
495 pr_debug("event %x:%llx not supported\n", event->attr.type,
496 event->attr.config);
497 return mapping;
498 }
499
500 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100501 * We don't assign an index until we actually place the event onto
502 * hardware. Use -1 to signify that we haven't decided where to put it
503 * yet. For SMP systems, each core has it's own PMU so we can't do any
504 * clever allocation or constraints checking at this point.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100505 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100506 hwc->idx = -1;
507 hwc->config_base = 0;
508 hwc->config = 0;
509 hwc->event_base = 0;
510
511 /*
512 * Check whether we need to exclude the counter from certain modes.
513 */
514 if ((!armpmu->set_event_filter ||
515 armpmu->set_event_filter(hwc, &event->attr)) &&
516 event_requires_mode_exclusion(&event->attr)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100517 pr_debug("ARM performance counters do not support "
518 "mode exclusion\n");
519 return -EPERM;
520 }
521
522 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100523 * Store the event encoding into the config_base field.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100524 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100525 hwc->config_base |= (unsigned long)mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100526
527 if (!hwc->sample_period) {
528 hwc->sample_period = armpmu->max_period;
529 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200530 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100531 }
532
533 err = 0;
534 if (event->group_leader != event) {
535 err = validate_group(event);
536 if (err)
537 return -EINVAL;
538 }
539
540 return err;
541}
542
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200543static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100544{
545 int err = 0;
546
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200547 switch (event->attr.type) {
548 case PERF_TYPE_RAW:
549 case PERF_TYPE_HARDWARE:
550 case PERF_TYPE_HW_CACHE:
551 break;
552
553 default:
554 return -ENOENT;
555 }
556
Jamie Iles1b8873a2010-02-02 20:25:44 +0100557 event->destroy = hw_perf_event_destroy;
558
559 if (!atomic_inc_not_zero(&active_events)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100560 mutex_lock(&pmu_reserve_mutex);
561 if (atomic_read(&active_events) == 0) {
562 err = armpmu_reserve_hardware();
563 }
564
565 if (!err)
566 atomic_inc(&active_events);
567 mutex_unlock(&pmu_reserve_mutex);
568 }
569
570 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200571 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100572
573 err = __hw_perf_event_init(event);
574 if (err)
575 hw_perf_event_destroy(event);
576
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200577 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100578}
579
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200580static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100581{
582 /* Enable all of the perf events on hardware. */
Will Deaconf4f38432011-07-01 14:38:12 +0100583 int idx, enabled = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100584 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
585
Will Deaconecf5a892011-07-19 22:43:28 +0100586 for (idx = 0; idx < armpmu->num_events; ++idx) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100587 struct perf_event *event = cpuc->events[idx];
588
589 if (!event)
590 continue;
591
592 armpmu->enable(&event->hw, idx);
Will Deaconf4f38432011-07-01 14:38:12 +0100593 enabled = 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100594 }
595
Will Deaconf4f38432011-07-01 14:38:12 +0100596 if (enabled)
597 armpmu->start();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100598}
599
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200600static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100601{
Mark Rutland48957152011-04-27 10:31:51 +0100602 armpmu->stop();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100603}
604
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200605static struct pmu pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200606 .pmu_enable = armpmu_enable,
607 .pmu_disable = armpmu_disable,
608 .event_init = armpmu_event_init,
609 .add = armpmu_add,
610 .del = armpmu_del,
611 .start = armpmu_start,
612 .stop = armpmu_stop,
613 .read = armpmu_read,
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200614};
615
Will Deacon43eab872010-11-13 19:04:32 +0000616/* Include the PMU-specific implementations. */
617#include "perf_event_xscale.c"
618#include "perf_event_v6.c"
619#include "perf_event_v7.c"
Will Deacon49e6a322010-04-30 11:33:33 +0100620
Will Deacon574b69c2011-03-25 13:13:34 +0100621/*
622 * Ensure the PMU has sane values out of reset.
623 * This requires SMP to be available, so exists as a separate initcall.
624 */
625static int __init
626armpmu_reset(void)
627{
628 if (armpmu && armpmu->reset)
629 return on_each_cpu(armpmu->reset, NULL, 1);
630 return 0;
631}
632arch_initcall(armpmu_reset);
633
Will Deaconb0e89592011-07-26 22:10:28 +0100634/*
635 * PMU platform driver and devicetree bindings.
636 */
637static struct of_device_id armpmu_of_device_ids[] = {
638 {.compatible = "arm,cortex-a9-pmu"},
639 {.compatible = "arm,cortex-a8-pmu"},
640 {.compatible = "arm,arm1136-pmu"},
641 {.compatible = "arm,arm1176-pmu"},
642 {},
643};
644
645static struct platform_device_id armpmu_plat_device_ids[] = {
646 {.name = "arm-pmu"},
647 {},
648};
649
650static int __devinit armpmu_device_probe(struct platform_device *pdev)
651{
652 pmu_device = pdev;
653 return 0;
654}
655
656static struct platform_driver armpmu_driver = {
657 .driver = {
658 .name = "arm-pmu",
659 .of_match_table = armpmu_of_device_ids,
660 },
661 .probe = armpmu_device_probe,
662 .id_table = armpmu_plat_device_ids,
663};
664
665static int __init register_pmu_driver(void)
666{
667 return platform_driver_register(&armpmu_driver);
668}
669device_initcall(register_pmu_driver);
670
671/*
672 * CPU PMU identification and registration.
673 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100674static int __init
675init_hw_perf_events(void)
676{
677 unsigned long cpuid = read_cpuid_id();
678 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
679 unsigned long part_number = (cpuid & 0xFFF0);
680
Will Deacon49e6a322010-04-30 11:33:33 +0100681 /* ARM Ltd CPUs. */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100682 if (0x41 == implementor) {
683 switch (part_number) {
684 case 0xB360: /* ARM1136 */
685 case 0xB560: /* ARM1156 */
686 case 0xB760: /* ARM1176 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000687 armpmu = armv6pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100688 break;
689 case 0xB020: /* ARM11mpcore */
Will Deacon3cb314b2010-11-13 17:37:46 +0000690 armpmu = armv6mpcore_pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100691 break;
Jean PIHET796d1292010-01-26 18:51:05 +0100692 case 0xC080: /* Cortex-A8 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000693 armpmu = armv7_a8_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100694 break;
695 case 0xC090: /* Cortex-A9 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000696 armpmu = armv7_a9_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100697 break;
Will Deacon0c205cb2011-06-03 17:40:15 +0100698 case 0xC050: /* Cortex-A5 */
699 armpmu = armv7_a5_pmu_init();
700 break;
Will Deacon14abd032011-01-19 14:24:38 +0000701 case 0xC0F0: /* Cortex-A15 */
702 armpmu = armv7_a15_pmu_init();
703 break;
Will Deacon49e6a322010-04-30 11:33:33 +0100704 }
705 /* Intel CPUs [xscale]. */
706 } else if (0x69 == implementor) {
707 part_number = (cpuid >> 13) & 0x7;
708 switch (part_number) {
709 case 1:
Will Deacon3cb314b2010-11-13 17:37:46 +0000710 armpmu = xscale1pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100711 break;
712 case 2:
Will Deacon3cb314b2010-11-13 17:37:46 +0000713 armpmu = xscale2pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100714 break;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100715 }
716 }
717
Will Deacon49e6a322010-04-30 11:33:33 +0100718 if (armpmu) {
Jean PIHET796d1292010-01-26 18:51:05 +0100719 pr_info("enabled with %s PMU driver, %d counters available\n",
Will Deacon62994832010-11-13 18:45:27 +0000720 armpmu->name, armpmu->num_events);
Mark Rutland48957152011-04-27 10:31:51 +0100721 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
Will Deacon49e6a322010-04-30 11:33:33 +0100722 } else {
723 pr_info("no hardware support available\n");
Will Deacon49e6a322010-04-30 11:33:33 +0100724 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100725
726 return 0;
727}
Peter Zijlstra004417a2010-11-25 18:38:29 +0100728early_initcall(init_hw_perf_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100729
730/*
731 * Callchain handling code.
732 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100733
734/*
735 * The registers we're interested in are at the end of the variable
736 * length saved register structure. The fp points at the end of this
737 * structure so the address of this struct is:
738 * (struct frame_tail *)(xxx->fp)-1
739 *
740 * This code has been adapted from the ARM OProfile support.
741 */
742struct frame_tail {
Will Deacon4d6b7a72010-11-30 18:15:53 +0100743 struct frame_tail __user *fp;
744 unsigned long sp;
745 unsigned long lr;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100746} __attribute__((packed));
747
748/*
749 * Get the return address for a single stackframe and return a pointer to the
750 * next frame tail.
751 */
Will Deacon4d6b7a72010-11-30 18:15:53 +0100752static struct frame_tail __user *
753user_backtrace(struct frame_tail __user *tail,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100754 struct perf_callchain_entry *entry)
755{
756 struct frame_tail buftail;
757
758 /* Also check accessibility of one struct frame_tail beyond */
759 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
760 return NULL;
761 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
762 return NULL;
763
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200764 perf_callchain_store(entry, buftail.lr);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100765
766 /*
767 * Frame pointers should strictly progress back up the stack
768 * (towards higher addresses).
769 */
Rabin Vincentcb061992011-02-09 11:35:12 +0100770 if (tail + 1 >= buftail.fp)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100771 return NULL;
772
773 return buftail.fp - 1;
774}
775
Frederic Weisbecker56962b4442010-06-30 23:03:51 +0200776void
777perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100778{
Will Deacon4d6b7a72010-11-30 18:15:53 +0100779 struct frame_tail __user *tail;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100780
Jamie Iles1b8873a2010-02-02 20:25:44 +0100781
Will Deacon4d6b7a72010-11-30 18:15:53 +0100782 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100783
Sonny Rao860ad782011-04-18 22:12:59 +0100784 while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
785 tail && !((unsigned long)tail & 0x3))
Jamie Iles1b8873a2010-02-02 20:25:44 +0100786 tail = user_backtrace(tail, entry);
787}
788
789/*
790 * Gets called by walk_stackframe() for every stackframe. This will be called
791 * whist unwinding the stackframe and is like a subroutine return so we use
792 * the PC.
793 */
794static int
795callchain_trace(struct stackframe *fr,
796 void *data)
797{
798 struct perf_callchain_entry *entry = data;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200799 perf_callchain_store(entry, fr->pc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100800 return 0;
801}
802
Frederic Weisbecker56962b4442010-06-30 23:03:51 +0200803void
804perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100805{
806 struct stackframe fr;
807
Jamie Iles1b8873a2010-02-02 20:25:44 +0100808 fr.fp = regs->ARM_fp;
809 fr.sp = regs->ARM_sp;
810 fr.lr = regs->ARM_lr;
811 fr.pc = regs->ARM_pc;
812 walk_stackframe(&fr, callchain_trace, entry);
813}