blob: 1d648d136413dc8a48b997f634da9b4466e18918 [file] [log] [blame]
Jamie Iles1b8873a2010-02-02 20:25:44 +01001#undef DEBUG
2
3/*
4 * ARM performance counter support.
5 *
6 * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
Will Deacon43eab872010-11-13 19:04:32 +00007 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
Jean PIHET796d1292010-01-26 18:51:05 +01008 *
Jamie Iles1b8873a2010-02-02 20:25:44 +01009 * This code is based on the sparc64 perf event code, which is in turn based
10 * on the x86 code. Callchain code is based on the ARM OProfile backtrace
11 * code.
12 */
13#define pr_fmt(fmt) "hw perfevents: " fmt
14
15#include <linux/interrupt.h>
16#include <linux/kernel.h>
Will Deacon181193f2010-04-30 11:32:44 +010017#include <linux/module.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010018#include <linux/perf_event.h>
Will Deacon49c006b2010-04-29 17:13:24 +010019#include <linux/platform_device.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010020#include <linux/spinlock.h>
21#include <linux/uaccess.h>
22
23#include <asm/cputype.h>
24#include <asm/irq.h>
25#include <asm/irq_regs.h>
26#include <asm/pmu.h>
27#include <asm/stacktrace.h>
28
Jamie Iles1b8873a2010-02-02 20:25:44 +010029/*
30 * Hardware lock to serialize accesses to PMU registers. Needed for the
31 * read/modify/write sequences.
32 */
Will Deacon961ec6da2010-12-02 18:01:49 +010033static DEFINE_RAW_SPINLOCK(pmu_lock);
Jamie Iles1b8873a2010-02-02 20:25:44 +010034
35/*
Will Deaconecf5a892011-07-19 22:43:28 +010036 * ARMv6 supports a maximum of 3 events, starting from index 0. If we add
Jamie Iles1b8873a2010-02-02 20:25:44 +010037 * another platform that supports more, we need to increase this to be the
38 * largest of all platforms.
Jean PIHET796d1292010-01-26 18:51:05 +010039 *
40 * ARMv7 supports up to 32 events:
41 * cycle counter CCNT + 31 events counters CNT0..30.
42 * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
Jamie Iles1b8873a2010-02-02 20:25:44 +010043 */
Will Deaconecf5a892011-07-19 22:43:28 +010044#define ARMPMU_MAX_HWEVENTS 32
Jamie Iles1b8873a2010-02-02 20:25:44 +010045
46/* The events for a given CPU. */
47struct cpu_hw_events {
48 /*
Will Deaconecf5a892011-07-19 22:43:28 +010049 * The events that are active on the CPU for the given index.
Jamie Iles1b8873a2010-02-02 20:25:44 +010050 */
51 struct perf_event *events[ARMPMU_MAX_HWEVENTS];
52
53 /*
54 * A 1 bit for an index indicates that the counter is being used for
55 * an event. A 0 means that the counter can be used.
56 */
57 unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
Jamie Iles1b8873a2010-02-02 20:25:44 +010058};
Will Deacon4d6b7a72010-11-30 18:15:53 +010059static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
Will Deacon181193f2010-04-30 11:32:44 +010060
Jamie Iles1b8873a2010-02-02 20:25:44 +010061struct arm_pmu {
Will Deacon181193f2010-04-30 11:32:44 +010062 enum arm_perf_pmu_ids id;
Will Deacon0b390e22011-07-27 15:18:59 +010063 cpumask_t active_irqs;
Will Deacon62994832010-11-13 18:45:27 +000064 const char *name;
Jamie Iles1b8873a2010-02-02 20:25:44 +010065 irqreturn_t (*handle_irq)(int irq_num, void *dev);
66 void (*enable)(struct hw_perf_event *evt, int idx);
67 void (*disable)(struct hw_perf_event *evt, int idx);
Jamie Iles1b8873a2010-02-02 20:25:44 +010068 int (*get_event_idx)(struct cpu_hw_events *cpuc,
69 struct hw_perf_event *hwc);
Will Deacon05d22fd2011-07-19 11:57:30 +010070 int (*set_event_filter)(struct hw_perf_event *evt,
71 struct perf_event_attr *attr);
Jamie Iles1b8873a2010-02-02 20:25:44 +010072 u32 (*read_counter)(int idx);
73 void (*write_counter)(int idx, u32 val);
74 void (*start)(void);
75 void (*stop)(void);
Will Deacon574b69c2011-03-25 13:13:34 +010076 void (*reset)(void *);
Will Deacon84fee972010-11-13 17:13:56 +000077 const unsigned (*cache_map)[PERF_COUNT_HW_CACHE_MAX]
78 [PERF_COUNT_HW_CACHE_OP_MAX]
79 [PERF_COUNT_HW_CACHE_RESULT_MAX];
80 const unsigned (*event_map)[PERF_COUNT_HW_MAX];
81 u32 raw_event_mask;
Jamie Iles1b8873a2010-02-02 20:25:44 +010082 int num_events;
Mark Rutland03b78982011-04-27 11:20:11 +010083 atomic_t active_events;
84 struct mutex reserve_mutex;
Jamie Iles1b8873a2010-02-02 20:25:44 +010085 u64 max_period;
Mark Rutlanda9356a02011-05-04 09:23:15 +010086 struct platform_device *plat_device;
Jamie Iles1b8873a2010-02-02 20:25:44 +010087};
88
89/* Set at runtime when we know what CPU type we are. */
Mark Rutlanda6c93af2011-04-15 11:14:38 +010090static struct arm_pmu *armpmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +010091
Will Deacon181193f2010-04-30 11:32:44 +010092enum arm_perf_pmu_ids
93armpmu_get_pmu_id(void)
94{
95 int id = -ENODEV;
96
97 if (armpmu != NULL)
98 id = armpmu->id;
99
100 return id;
101}
102EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
103
Will Deacon929f5192010-04-30 11:34:26 +0100104int
105armpmu_get_max_events(void)
106{
107 int max_events = 0;
108
109 if (armpmu != NULL)
110 max_events = armpmu->num_events;
111
112 return max_events;
113}
114EXPORT_SYMBOL_GPL(armpmu_get_max_events);
115
Matt Fleming3bf101b2010-09-27 20:22:24 +0100116int perf_num_counters(void)
117{
118 return armpmu_get_max_events();
119}
120EXPORT_SYMBOL_GPL(perf_num_counters);
121
Jamie Iles1b8873a2010-02-02 20:25:44 +0100122#define HW_OP_UNSUPPORTED 0xFFFF
123
124#define C(_x) \
125 PERF_COUNT_HW_CACHE_##_x
126
127#define CACHE_OP_UNSUPPORTED 0xFFFF
128
Jamie Iles1b8873a2010-02-02 20:25:44 +0100129static int
130armpmu_map_cache_event(u64 config)
131{
132 unsigned int cache_type, cache_op, cache_result, ret;
133
134 cache_type = (config >> 0) & 0xff;
135 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
136 return -EINVAL;
137
138 cache_op = (config >> 8) & 0xff;
139 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
140 return -EINVAL;
141
142 cache_result = (config >> 16) & 0xff;
143 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
144 return -EINVAL;
145
Will Deacon84fee972010-11-13 17:13:56 +0000146 ret = (int)(*armpmu->cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100147
148 if (ret == CACHE_OP_UNSUPPORTED)
149 return -ENOENT;
150
151 return ret;
152}
153
154static int
Will Deacon84fee972010-11-13 17:13:56 +0000155armpmu_map_event(u64 config)
156{
157 int mapping = (*armpmu->event_map)[config];
158 return mapping == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : mapping;
159}
160
161static int
162armpmu_map_raw_event(u64 config)
163{
164 return (int)(config & armpmu->raw_event_mask);
165}
166
167static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100168armpmu_event_set_period(struct perf_event *event,
169 struct hw_perf_event *hwc,
170 int idx)
171{
Peter Zijlstrae7850592010-05-21 14:43:08 +0200172 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100173 s64 period = hwc->sample_period;
174 int ret = 0;
175
176 if (unlikely(left <= -period)) {
177 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200178 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100179 hwc->last_period = period;
180 ret = 1;
181 }
182
183 if (unlikely(left <= 0)) {
184 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200185 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100186 hwc->last_period = period;
187 ret = 1;
188 }
189
190 if (left > (s64)armpmu->max_period)
191 left = armpmu->max_period;
192
Peter Zijlstrae7850592010-05-21 14:43:08 +0200193 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100194
195 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
196
197 perf_event_update_userpage(event);
198
199 return ret;
200}
201
202static u64
203armpmu_event_update(struct perf_event *event,
204 struct hw_perf_event *hwc,
Will Deacona7378232011-03-25 17:12:37 +0100205 int idx, int overflow)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100206{
Will Deacona7378232011-03-25 17:12:37 +0100207 u64 delta, prev_raw_count, new_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100208
209again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200210 prev_raw_count = local64_read(&hwc->prev_count);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100211 new_raw_count = armpmu->read_counter(idx);
212
Peter Zijlstrae7850592010-05-21 14:43:08 +0200213 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100214 new_raw_count) != prev_raw_count)
215 goto again;
216
Will Deacona7378232011-03-25 17:12:37 +0100217 new_raw_count &= armpmu->max_period;
218 prev_raw_count &= armpmu->max_period;
219
220 if (overflow)
Will Deacon67597882011-04-05 14:01:24 +0100221 delta = armpmu->max_period - prev_raw_count + new_raw_count + 1;
Will Deacona7378232011-03-25 17:12:37 +0100222 else
223 delta = new_raw_count - prev_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100224
Peter Zijlstrae7850592010-05-21 14:43:08 +0200225 local64_add(delta, &event->count);
226 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100227
228 return new_raw_count;
229}
230
231static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100232armpmu_read(struct perf_event *event)
233{
234 struct hw_perf_event *hwc = &event->hw;
235
236 /* Don't read disabled counters! */
237 if (hwc->idx < 0)
238 return;
239
Will Deacona7378232011-03-25 17:12:37 +0100240 armpmu_event_update(event, hwc, hwc->idx, 0);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100241}
242
243static void
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200244armpmu_stop(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100245{
246 struct hw_perf_event *hwc = &event->hw;
247
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200248 /*
249 * ARM pmu always has to update the counter, so ignore
250 * PERF_EF_UPDATE, see comments in armpmu_start().
251 */
252 if (!(hwc->state & PERF_HES_STOPPED)) {
253 armpmu->disable(hwc, hwc->idx);
254 barrier(); /* why? */
Will Deacona7378232011-03-25 17:12:37 +0100255 armpmu_event_update(event, hwc, hwc->idx, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200256 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
257 }
258}
259
260static void
261armpmu_start(struct perf_event *event, int flags)
262{
263 struct hw_perf_event *hwc = &event->hw;
264
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200265 /*
266 * ARM pmu always has to reprogram the period, so ignore
267 * PERF_EF_RELOAD, see the comment below.
268 */
269 if (flags & PERF_EF_RELOAD)
270 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
271
272 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100273 /*
274 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200275 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100276 * may have been left counting. If we don't do this step then we may
277 * get an interrupt too soon or *way* too late if the overflow has
278 * happened since disabling.
279 */
280 armpmu_event_set_period(event, hwc, hwc->idx);
281 armpmu->enable(hwc, hwc->idx);
282}
283
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200284static void
285armpmu_del(struct perf_event *event, int flags)
286{
287 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
288 struct hw_perf_event *hwc = &event->hw;
289 int idx = hwc->idx;
290
291 WARN_ON(idx < 0);
292
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200293 armpmu_stop(event, PERF_EF_UPDATE);
294 cpuc->events[idx] = NULL;
295 clear_bit(idx, cpuc->used_mask);
296
297 perf_event_update_userpage(event);
298}
299
Jamie Iles1b8873a2010-02-02 20:25:44 +0100300static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200301armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100302{
303 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
304 struct hw_perf_event *hwc = &event->hw;
305 int idx;
306 int err = 0;
307
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200308 perf_pmu_disable(event->pmu);
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200309
Jamie Iles1b8873a2010-02-02 20:25:44 +0100310 /* If we don't have a space for the counter then finish early. */
311 idx = armpmu->get_event_idx(cpuc, hwc);
312 if (idx < 0) {
313 err = idx;
314 goto out;
315 }
316
317 /*
318 * If there is an event in the counter we are going to use then make
319 * sure it is disabled.
320 */
321 event->hw.idx = idx;
322 armpmu->disable(hwc, idx);
323 cpuc->events[idx] = event;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100324
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200325 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
326 if (flags & PERF_EF_START)
327 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100328
329 /* Propagate our changes to the userspace mapping. */
330 perf_event_update_userpage(event);
331
332out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200333 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100334 return err;
335}
336
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200337static struct pmu pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100338
339static int
340validate_event(struct cpu_hw_events *cpuc,
341 struct perf_event *event)
342{
343 struct hw_perf_event fake_event = event->hw;
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100344 struct pmu *leader_pmu = event->group_leader->pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100345
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100346 if (event->pmu != leader_pmu || event->state <= PERF_EVENT_STATE_OFF)
Will Deacon65b47112010-09-02 09:32:08 +0100347 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100348
349 return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
350}
351
352static int
353validate_group(struct perf_event *event)
354{
355 struct perf_event *sibling, *leader = event->group_leader;
356 struct cpu_hw_events fake_pmu;
357
358 memset(&fake_pmu, 0, sizeof(fake_pmu));
359
360 if (!validate_event(&fake_pmu, leader))
361 return -ENOSPC;
362
363 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
364 if (!validate_event(&fake_pmu, sibling))
365 return -ENOSPC;
366 }
367
368 if (!validate_event(&fake_pmu, event))
369 return -ENOSPC;
370
371 return 0;
372}
373
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530374static irqreturn_t armpmu_platform_irq(int irq, void *dev)
375{
Mark Rutlanda9356a02011-05-04 09:23:15 +0100376 struct platform_device *plat_device = armpmu->plat_device;
377 struct arm_pmu_platdata *plat = dev_get_platdata(&plat_device->dev);
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530378
379 return plat->handle_irq(irq, dev, armpmu->handle_irq);
380}
381
Will Deacon0b390e22011-07-27 15:18:59 +0100382static void
383armpmu_release_hardware(void)
384{
385 int i, irq, irqs;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100386 struct platform_device *pmu_device = armpmu->plat_device;
Will Deacon0b390e22011-07-27 15:18:59 +0100387
388 irqs = min(pmu_device->num_resources, num_possible_cpus());
389
390 for (i = 0; i < irqs; ++i) {
391 if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs))
392 continue;
393 irq = platform_get_irq(pmu_device, i);
394 if (irq >= 0)
395 free_irq(irq, NULL);
396 }
397
398 armpmu->stop();
399 release_pmu(ARM_PMU_DEVICE_CPU);
400}
401
Jamie Iles1b8873a2010-02-02 20:25:44 +0100402static int
403armpmu_reserve_hardware(void)
404{
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530405 struct arm_pmu_platdata *plat;
406 irq_handler_t handle_irq;
Will Deaconb0e89592011-07-26 22:10:28 +0100407 int i, err, irq, irqs;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100408 struct platform_device *pmu_device = armpmu->plat_device;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100409
Will Deaconb0e89592011-07-26 22:10:28 +0100410 err = reserve_pmu(ARM_PMU_DEVICE_CPU);
411 if (err) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100412 pr_warning("unable to reserve pmu\n");
Will Deaconb0e89592011-07-26 22:10:28 +0100413 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100414 }
415
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530416 plat = dev_get_platdata(&pmu_device->dev);
417 if (plat && plat->handle_irq)
418 handle_irq = armpmu_platform_irq;
419 else
420 handle_irq = armpmu->handle_irq;
421
Will Deacon0b390e22011-07-27 15:18:59 +0100422 irqs = min(pmu_device->num_resources, num_possible_cpus());
Will Deaconb0e89592011-07-26 22:10:28 +0100423 if (irqs < 1) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100424 pr_err("no irqs for PMUs defined\n");
425 return -ENODEV;
426 }
427
Will Deaconb0e89592011-07-26 22:10:28 +0100428 for (i = 0; i < irqs; ++i) {
Will Deacon0b390e22011-07-27 15:18:59 +0100429 err = 0;
Will Deacon49c006b2010-04-29 17:13:24 +0100430 irq = platform_get_irq(pmu_device, i);
431 if (irq < 0)
432 continue;
433
Will Deaconb0e89592011-07-26 22:10:28 +0100434 /*
435 * If we have a single PMU interrupt that we can't shift,
436 * assume that we're running on a uniprocessor machine and
Will Deacon0b390e22011-07-27 15:18:59 +0100437 * continue. Otherwise, continue without this interrupt.
Will Deaconb0e89592011-07-26 22:10:28 +0100438 */
Will Deacon0b390e22011-07-27 15:18:59 +0100439 if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
440 pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
441 irq, i);
442 continue;
Will Deaconb0e89592011-07-26 22:10:28 +0100443 }
444
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530445 err = request_irq(irq, handle_irq,
Will Deaconddee87f2010-02-25 15:04:14 +0100446 IRQF_DISABLED | IRQF_NOBALANCING,
Will Deaconb0e89592011-07-26 22:10:28 +0100447 "arm-pmu", NULL);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100448 if (err) {
Will Deaconb0e89592011-07-26 22:10:28 +0100449 pr_err("unable to request IRQ%d for ARM PMU counters\n",
450 irq);
Will Deacon0b390e22011-07-27 15:18:59 +0100451 armpmu_release_hardware();
452 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100453 }
Will Deacon0b390e22011-07-27 15:18:59 +0100454
455 cpumask_set_cpu(i, &armpmu->active_irqs);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100456 }
457
Will Deacon0b390e22011-07-27 15:18:59 +0100458 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100459}
460
Jamie Iles1b8873a2010-02-02 20:25:44 +0100461static void
462hw_perf_event_destroy(struct perf_event *event)
463{
Mark Rutland03b78982011-04-27 11:20:11 +0100464 atomic_t *active_events = &armpmu->active_events;
465 struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
466
467 if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100468 armpmu_release_hardware();
Mark Rutland03b78982011-04-27 11:20:11 +0100469 mutex_unlock(pmu_reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100470 }
471}
472
473static int
Will Deacon05d22fd2011-07-19 11:57:30 +0100474event_requires_mode_exclusion(struct perf_event_attr *attr)
475{
476 return attr->exclude_idle || attr->exclude_user ||
477 attr->exclude_kernel || attr->exclude_hv;
478}
479
480static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100481__hw_perf_event_init(struct perf_event *event)
482{
483 struct hw_perf_event *hwc = &event->hw;
484 int mapping, err;
485
486 /* Decode the generic type into an ARM event identifier. */
487 if (PERF_TYPE_HARDWARE == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000488 mapping = armpmu_map_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100489 } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
490 mapping = armpmu_map_cache_event(event->attr.config);
491 } else if (PERF_TYPE_RAW == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000492 mapping = armpmu_map_raw_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100493 } else {
494 pr_debug("event type %x not supported\n", event->attr.type);
495 return -EOPNOTSUPP;
496 }
497
498 if (mapping < 0) {
499 pr_debug("event %x:%llx not supported\n", event->attr.type,
500 event->attr.config);
501 return mapping;
502 }
503
504 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100505 * We don't assign an index until we actually place the event onto
506 * hardware. Use -1 to signify that we haven't decided where to put it
507 * yet. For SMP systems, each core has it's own PMU so we can't do any
508 * clever allocation or constraints checking at this point.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100509 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100510 hwc->idx = -1;
511 hwc->config_base = 0;
512 hwc->config = 0;
513 hwc->event_base = 0;
514
515 /*
516 * Check whether we need to exclude the counter from certain modes.
517 */
518 if ((!armpmu->set_event_filter ||
519 armpmu->set_event_filter(hwc, &event->attr)) &&
520 event_requires_mode_exclusion(&event->attr)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100521 pr_debug("ARM performance counters do not support "
522 "mode exclusion\n");
523 return -EPERM;
524 }
525
526 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100527 * Store the event encoding into the config_base field.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100528 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100529 hwc->config_base |= (unsigned long)mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100530
531 if (!hwc->sample_period) {
532 hwc->sample_period = armpmu->max_period;
533 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200534 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100535 }
536
537 err = 0;
538 if (event->group_leader != event) {
539 err = validate_group(event);
540 if (err)
541 return -EINVAL;
542 }
543
544 return err;
545}
546
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200547static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100548{
549 int err = 0;
Mark Rutland03b78982011-04-27 11:20:11 +0100550 atomic_t *active_events = &armpmu->active_events;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100551
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200552 switch (event->attr.type) {
553 case PERF_TYPE_RAW:
554 case PERF_TYPE_HARDWARE:
555 case PERF_TYPE_HW_CACHE:
556 break;
557
558 default:
559 return -ENOENT;
560 }
561
Jamie Iles1b8873a2010-02-02 20:25:44 +0100562 event->destroy = hw_perf_event_destroy;
563
Mark Rutland03b78982011-04-27 11:20:11 +0100564 if (!atomic_inc_not_zero(active_events)) {
565 mutex_lock(&armpmu->reserve_mutex);
566 if (atomic_read(active_events) == 0)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100567 err = armpmu_reserve_hardware();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100568
569 if (!err)
Mark Rutland03b78982011-04-27 11:20:11 +0100570 atomic_inc(active_events);
571 mutex_unlock(&armpmu->reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100572 }
573
574 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200575 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100576
577 err = __hw_perf_event_init(event);
578 if (err)
579 hw_perf_event_destroy(event);
580
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200581 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100582}
583
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200584static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100585{
586 /* Enable all of the perf events on hardware. */
Will Deaconf4f38432011-07-01 14:38:12 +0100587 int idx, enabled = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100588 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
589
Will Deaconecf5a892011-07-19 22:43:28 +0100590 for (idx = 0; idx < armpmu->num_events; ++idx) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100591 struct perf_event *event = cpuc->events[idx];
592
593 if (!event)
594 continue;
595
596 armpmu->enable(&event->hw, idx);
Will Deaconf4f38432011-07-01 14:38:12 +0100597 enabled = 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100598 }
599
Will Deaconf4f38432011-07-01 14:38:12 +0100600 if (enabled)
601 armpmu->start();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100602}
603
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200604static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100605{
Mark Rutland48957152011-04-27 10:31:51 +0100606 armpmu->stop();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100607}
608
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200609static struct pmu pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200610 .pmu_enable = armpmu_enable,
611 .pmu_disable = armpmu_disable,
612 .event_init = armpmu_event_init,
613 .add = armpmu_add,
614 .del = armpmu_del,
615 .start = armpmu_start,
616 .stop = armpmu_stop,
617 .read = armpmu_read,
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200618};
619
Mark Rutland03b78982011-04-27 11:20:11 +0100620static void __init armpmu_init(struct arm_pmu *armpmu)
621{
622 atomic_set(&armpmu->active_events, 0);
623 mutex_init(&armpmu->reserve_mutex);
624}
625
Will Deacon43eab872010-11-13 19:04:32 +0000626/* Include the PMU-specific implementations. */
627#include "perf_event_xscale.c"
628#include "perf_event_v6.c"
629#include "perf_event_v7.c"
Will Deacon49e6a322010-04-30 11:33:33 +0100630
Will Deacon574b69c2011-03-25 13:13:34 +0100631/*
632 * Ensure the PMU has sane values out of reset.
633 * This requires SMP to be available, so exists as a separate initcall.
634 */
635static int __init
636armpmu_reset(void)
637{
638 if (armpmu && armpmu->reset)
639 return on_each_cpu(armpmu->reset, NULL, 1);
640 return 0;
641}
642arch_initcall(armpmu_reset);
643
Will Deaconb0e89592011-07-26 22:10:28 +0100644/*
645 * PMU platform driver and devicetree bindings.
646 */
647static struct of_device_id armpmu_of_device_ids[] = {
648 {.compatible = "arm,cortex-a9-pmu"},
649 {.compatible = "arm,cortex-a8-pmu"},
650 {.compatible = "arm,arm1136-pmu"},
651 {.compatible = "arm,arm1176-pmu"},
652 {},
653};
654
655static struct platform_device_id armpmu_plat_device_ids[] = {
656 {.name = "arm-pmu"},
657 {},
658};
659
660static int __devinit armpmu_device_probe(struct platform_device *pdev)
661{
Mark Rutlanda9356a02011-05-04 09:23:15 +0100662 armpmu->plat_device = pdev;
Will Deaconb0e89592011-07-26 22:10:28 +0100663 return 0;
664}
665
666static struct platform_driver armpmu_driver = {
667 .driver = {
668 .name = "arm-pmu",
669 .of_match_table = armpmu_of_device_ids,
670 },
671 .probe = armpmu_device_probe,
672 .id_table = armpmu_plat_device_ids,
673};
674
675static int __init register_pmu_driver(void)
676{
677 return platform_driver_register(&armpmu_driver);
678}
679device_initcall(register_pmu_driver);
680
681/*
682 * CPU PMU identification and registration.
683 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100684static int __init
685init_hw_perf_events(void)
686{
687 unsigned long cpuid = read_cpuid_id();
688 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
689 unsigned long part_number = (cpuid & 0xFFF0);
690
Will Deacon49e6a322010-04-30 11:33:33 +0100691 /* ARM Ltd CPUs. */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100692 if (0x41 == implementor) {
693 switch (part_number) {
694 case 0xB360: /* ARM1136 */
695 case 0xB560: /* ARM1156 */
696 case 0xB760: /* ARM1176 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000697 armpmu = armv6pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100698 break;
699 case 0xB020: /* ARM11mpcore */
Will Deacon3cb314b2010-11-13 17:37:46 +0000700 armpmu = armv6mpcore_pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100701 break;
Jean PIHET796d1292010-01-26 18:51:05 +0100702 case 0xC080: /* Cortex-A8 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000703 armpmu = armv7_a8_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100704 break;
705 case 0xC090: /* Cortex-A9 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000706 armpmu = armv7_a9_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100707 break;
Will Deacon0c205cb2011-06-03 17:40:15 +0100708 case 0xC050: /* Cortex-A5 */
709 armpmu = armv7_a5_pmu_init();
710 break;
Will Deacon14abd032011-01-19 14:24:38 +0000711 case 0xC0F0: /* Cortex-A15 */
712 armpmu = armv7_a15_pmu_init();
713 break;
Will Deacon49e6a322010-04-30 11:33:33 +0100714 }
715 /* Intel CPUs [xscale]. */
716 } else if (0x69 == implementor) {
717 part_number = (cpuid >> 13) & 0x7;
718 switch (part_number) {
719 case 1:
Will Deacon3cb314b2010-11-13 17:37:46 +0000720 armpmu = xscale1pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100721 break;
722 case 2:
Will Deacon3cb314b2010-11-13 17:37:46 +0000723 armpmu = xscale2pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100724 break;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100725 }
726 }
727
Will Deacon49e6a322010-04-30 11:33:33 +0100728 if (armpmu) {
Jean PIHET796d1292010-01-26 18:51:05 +0100729 pr_info("enabled with %s PMU driver, %d counters available\n",
Will Deacon62994832010-11-13 18:45:27 +0000730 armpmu->name, armpmu->num_events);
Mark Rutland03b78982011-04-27 11:20:11 +0100731 armpmu_init(armpmu);
Mark Rutland48957152011-04-27 10:31:51 +0100732 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
Will Deacon49e6a322010-04-30 11:33:33 +0100733 } else {
734 pr_info("no hardware support available\n");
Will Deacon49e6a322010-04-30 11:33:33 +0100735 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100736
737 return 0;
738}
Peter Zijlstra004417a2010-11-25 18:38:29 +0100739early_initcall(init_hw_perf_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100740
741/*
742 * Callchain handling code.
743 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100744
745/*
746 * The registers we're interested in are at the end of the variable
747 * length saved register structure. The fp points at the end of this
748 * structure so the address of this struct is:
749 * (struct frame_tail *)(xxx->fp)-1
750 *
751 * This code has been adapted from the ARM OProfile support.
752 */
753struct frame_tail {
Will Deacon4d6b7a72010-11-30 18:15:53 +0100754 struct frame_tail __user *fp;
755 unsigned long sp;
756 unsigned long lr;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100757} __attribute__((packed));
758
759/*
760 * Get the return address for a single stackframe and return a pointer to the
761 * next frame tail.
762 */
Will Deacon4d6b7a72010-11-30 18:15:53 +0100763static struct frame_tail __user *
764user_backtrace(struct frame_tail __user *tail,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100765 struct perf_callchain_entry *entry)
766{
767 struct frame_tail buftail;
768
769 /* Also check accessibility of one struct frame_tail beyond */
770 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
771 return NULL;
772 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
773 return NULL;
774
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200775 perf_callchain_store(entry, buftail.lr);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100776
777 /*
778 * Frame pointers should strictly progress back up the stack
779 * (towards higher addresses).
780 */
Rabin Vincentcb061992011-02-09 11:35:12 +0100781 if (tail + 1 >= buftail.fp)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100782 return NULL;
783
784 return buftail.fp - 1;
785}
786
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200787void
788perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100789{
Will Deacon4d6b7a72010-11-30 18:15:53 +0100790 struct frame_tail __user *tail;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100791
Jamie Iles1b8873a2010-02-02 20:25:44 +0100792
Will Deacon4d6b7a72010-11-30 18:15:53 +0100793 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100794
Sonny Rao860ad782011-04-18 22:12:59 +0100795 while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
796 tail && !((unsigned long)tail & 0x3))
Jamie Iles1b8873a2010-02-02 20:25:44 +0100797 tail = user_backtrace(tail, entry);
798}
799
800/*
801 * Gets called by walk_stackframe() for every stackframe. This will be called
802 * whist unwinding the stackframe and is like a subroutine return so we use
803 * the PC.
804 */
805static int
806callchain_trace(struct stackframe *fr,
807 void *data)
808{
809 struct perf_callchain_entry *entry = data;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200810 perf_callchain_store(entry, fr->pc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100811 return 0;
812}
813
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200814void
815perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100816{
817 struct stackframe fr;
818
Jamie Iles1b8873a2010-02-02 20:25:44 +0100819 fr.fp = regs->ARM_fp;
820 fr.sp = regs->ARM_sp;
821 fr.lr = regs->ARM_lr;
822 fr.pc = regs->ARM_pc;
823 walk_stackframe(&fr, callchain_trace, entry);
824}