blob: de314eac6944f69f436e1184fb4fc0ba519ad110 [file] [log] [blame]
Jamie Iles1b8873a2010-02-02 20:25:44 +01001#undef DEBUG
2
3/*
4 * ARM performance counter support.
5 *
6 * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
Will Deacon43eab872010-11-13 19:04:32 +00007 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
Jean PIHET796d1292010-01-26 18:51:05 +01008 *
Jamie Iles1b8873a2010-02-02 20:25:44 +01009 * This code is based on the sparc64 perf event code, which is in turn based
10 * on the x86 code. Callchain code is based on the ARM OProfile backtrace
11 * code.
12 */
13#define pr_fmt(fmt) "hw perfevents: " fmt
14
15#include <linux/interrupt.h>
16#include <linux/kernel.h>
Will Deacon181193f2010-04-30 11:32:44 +010017#include <linux/module.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010018#include <linux/perf_event.h>
Will Deacon49c006b2010-04-29 17:13:24 +010019#include <linux/platform_device.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010020#include <linux/spinlock.h>
21#include <linux/uaccess.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070022#include <linux/irq.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010023
24#include <asm/cputype.h>
25#include <asm/irq.h>
26#include <asm/irq_regs.h>
27#include <asm/pmu.h>
28#include <asm/stacktrace.h>
Ashwin Chaugule464983a2011-11-21 14:51:51 -050029#include <linux/cpu_pm.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010030
Will Deacon49c006b2010-04-29 17:13:24 +010031static struct platform_device *pmu_device;
Jamie Iles1b8873a2010-02-02 20:25:44 +010032
33/*
34 * Hardware lock to serialize accesses to PMU registers. Needed for the
35 * read/modify/write sequences.
36 */
Will Deacon961ec6d2010-12-02 18:01:49 +010037static DEFINE_RAW_SPINLOCK(pmu_lock);
Jamie Iles1b8873a2010-02-02 20:25:44 +010038
39/*
40 * ARMv6 supports a maximum of 3 events, starting from index 1. If we add
41 * another platform that supports more, we need to increase this to be the
42 * largest of all platforms.
Jean PIHET796d1292010-01-26 18:51:05 +010043 *
44 * ARMv7 supports up to 32 events:
45 * cycle counter CCNT + 31 events counters CNT0..30.
46 * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
Jamie Iles1b8873a2010-02-02 20:25:44 +010047 */
Jean PIHET796d1292010-01-26 18:51:05 +010048#define ARMPMU_MAX_HWEVENTS 33
Jamie Iles1b8873a2010-02-02 20:25:44 +010049
50/* The events for a given CPU. */
51struct cpu_hw_events {
52 /*
53 * The events that are active on the CPU for the given index. Index 0
54 * is reserved.
55 */
56 struct perf_event *events[ARMPMU_MAX_HWEVENTS];
57
58 /*
59 * A 1 bit for an index indicates that the counter is being used for
60 * an event. A 0 means that the counter can be used.
61 */
62 unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
63
64 /*
65 * A 1 bit for an index indicates that the counter is actively being
66 * used.
67 */
68 unsigned long active_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
69};
Will Deacon4d6b7a72010-11-30 18:15:53 +010070static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
Will Deacon181193f2010-04-30 11:32:44 +010071
Jamie Iles1b8873a2010-02-02 20:25:44 +010072struct arm_pmu {
Will Deacon181193f2010-04-30 11:32:44 +010073 enum arm_perf_pmu_ids id;
Will Deacon62994832010-11-13 18:45:27 +000074 const char *name;
Jamie Iles1b8873a2010-02-02 20:25:44 +010075 irqreturn_t (*handle_irq)(int irq_num, void *dev);
76 void (*enable)(struct hw_perf_event *evt, int idx);
77 void (*disable)(struct hw_perf_event *evt, int idx);
Jamie Iles1b8873a2010-02-02 20:25:44 +010078 int (*get_event_idx)(struct cpu_hw_events *cpuc,
79 struct hw_perf_event *hwc);
80 u32 (*read_counter)(int idx);
81 void (*write_counter)(int idx, u32 val);
Ashwin Chaugule133ddac2011-11-28 11:39:38 -050082 int (*set_event_filter) (struct hw_perf_event *evt,
83 struct perf_event_attr *attr);
Jamie Iles1b8873a2010-02-02 20:25:44 +010084 void (*start)(void);
85 void (*stop)(void);
Will Deacon574b69c2011-03-25 13:13:34 +010086 void (*reset)(void *);
Ashwin Chaugule14da0032011-11-10 13:55:16 -050087 unsigned (*cache_map)[PERF_COUNT_HW_CACHE_MAX]
Will Deacon84fee972010-11-13 17:13:56 +000088 [PERF_COUNT_HW_CACHE_OP_MAX]
89 [PERF_COUNT_HW_CACHE_RESULT_MAX];
90 const unsigned (*event_map)[PERF_COUNT_HW_MAX];
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -050091 int (*request_pmu_irq)(int irq, irq_handler_t *irq_h);
92 void (*free_pmu_irq)(int irq);
Will Deacon84fee972010-11-13 17:13:56 +000093 u32 raw_event_mask;
Jamie Iles1b8873a2010-02-02 20:25:44 +010094 int num_events;
95 u64 max_period;
96};
97
98/* Set at runtime when we know what CPU type we are. */
99static const struct arm_pmu *armpmu;
100
Will Deacon181193f2010-04-30 11:32:44 +0100101enum arm_perf_pmu_ids
102armpmu_get_pmu_id(void)
103{
104 int id = -ENODEV;
105
106 if (armpmu != NULL)
107 id = armpmu->id;
108
109 return id;
110}
111EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
112
Will Deacon929f5192010-04-30 11:34:26 +0100113int
114armpmu_get_max_events(void)
115{
116 int max_events = 0;
117
118 if (armpmu != NULL)
119 max_events = armpmu->num_events;
120
121 return max_events;
122}
123EXPORT_SYMBOL_GPL(armpmu_get_max_events);
124
Matt Fleming3bf101b2010-09-27 20:22:24 +0100125int perf_num_counters(void)
126{
127 return armpmu_get_max_events();
128}
129EXPORT_SYMBOL_GPL(perf_num_counters);
130
Jamie Iles1b8873a2010-02-02 20:25:44 +0100131#define HW_OP_UNSUPPORTED 0xFFFF
132
133#define C(_x) \
134 PERF_COUNT_HW_CACHE_##_x
135
136#define CACHE_OP_UNSUPPORTED 0xFFFF
137
Jamie Iles1b8873a2010-02-02 20:25:44 +0100138static int
139armpmu_map_cache_event(u64 config)
140{
141 unsigned int cache_type, cache_op, cache_result, ret;
142
143 cache_type = (config >> 0) & 0xff;
144 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
145 return -EINVAL;
146
147 cache_op = (config >> 8) & 0xff;
148 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
149 return -EINVAL;
150
151 cache_result = (config >> 16) & 0xff;
152 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
153 return -EINVAL;
154
Will Deacon84fee972010-11-13 17:13:56 +0000155 ret = (int)(*armpmu->cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100156
157 if (ret == CACHE_OP_UNSUPPORTED)
158 return -ENOENT;
159
160 return ret;
161}
162
163static int
Will Deacon84fee972010-11-13 17:13:56 +0000164armpmu_map_event(u64 config)
165{
166 int mapping = (*armpmu->event_map)[config];
167 return mapping == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : mapping;
168}
169
170static int
171armpmu_map_raw_event(u64 config)
172{
173 return (int)(config & armpmu->raw_event_mask);
174}
175
176static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100177armpmu_event_set_period(struct perf_event *event,
178 struct hw_perf_event *hwc,
179 int idx)
180{
Peter Zijlstrae7850592010-05-21 14:43:08 +0200181 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100182 s64 period = hwc->sample_period;
183 int ret = 0;
184
185 if (unlikely(left <= -period)) {
186 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200187 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100188 hwc->last_period = period;
189 ret = 1;
190 }
191
192 if (unlikely(left <= 0)) {
193 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200194 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100195 hwc->last_period = period;
196 ret = 1;
197 }
198
199 if (left > (s64)armpmu->max_period)
200 left = armpmu->max_period;
201
Peter Zijlstrae7850592010-05-21 14:43:08 +0200202 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100203
204 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
205
206 perf_event_update_userpage(event);
207
208 return ret;
209}
210
211static u64
212armpmu_event_update(struct perf_event *event,
213 struct hw_perf_event *hwc,
Will Deacona7378232011-03-25 17:12:37 +0100214 int idx, int overflow)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100215{
Will Deacona7378232011-03-25 17:12:37 +0100216 u64 delta, prev_raw_count, new_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100217
218again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200219 prev_raw_count = local64_read(&hwc->prev_count);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100220 new_raw_count = armpmu->read_counter(idx);
221
Peter Zijlstrae7850592010-05-21 14:43:08 +0200222 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100223 new_raw_count) != prev_raw_count)
224 goto again;
225
Will Deacona7378232011-03-25 17:12:37 +0100226 new_raw_count &= armpmu->max_period;
227 prev_raw_count &= armpmu->max_period;
228
229 if (overflow)
Will Deacon67597882011-04-05 14:01:24 +0100230 delta = armpmu->max_period - prev_raw_count + new_raw_count + 1;
Will Deacona7378232011-03-25 17:12:37 +0100231 else
232 delta = new_raw_count - prev_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100233
Peter Zijlstrae7850592010-05-21 14:43:08 +0200234 local64_add(delta, &event->count);
235 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100236
237 return new_raw_count;
238}
239
240static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100241armpmu_read(struct perf_event *event)
242{
243 struct hw_perf_event *hwc = &event->hw;
244
245 /* Don't read disabled counters! */
246 if (hwc->idx < 0)
247 return;
Will Deacona7378232011-03-25 17:12:37 +0100248 armpmu_event_update(event, hwc, hwc->idx, 0);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100249}
250
251static void
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200252armpmu_stop(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100253{
254 struct hw_perf_event *hwc = &event->hw;
255
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200256 if (!armpmu)
257 return;
258
259 /*
260 * ARM pmu always has to update the counter, so ignore
261 * PERF_EF_UPDATE, see comments in armpmu_start().
262 */
263 if (!(hwc->state & PERF_HES_STOPPED)) {
264 armpmu->disable(hwc, hwc->idx);
265 barrier(); /* why? */
Will Deacona7378232011-03-25 17:12:37 +0100266 armpmu_event_update(event, hwc, hwc->idx, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200267 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
268 }
269}
270
271static void
272armpmu_start(struct perf_event *event, int flags)
273{
274 struct hw_perf_event *hwc = &event->hw;
275
276 if (!armpmu)
277 return;
278
279 /*
280 * ARM pmu always has to reprogram the period, so ignore
281 * PERF_EF_RELOAD, see the comment below.
282 */
283 if (flags & PERF_EF_RELOAD)
284 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
285
286 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100287 /*
288 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200289 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100290 * may have been left counting. If we don't do this step then we may
291 * get an interrupt too soon or *way* too late if the overflow has
292 * happened since disabling.
293 */
294 armpmu_event_set_period(event, hwc, hwc->idx);
295 armpmu->enable(hwc, hwc->idx);
296}
297
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200298static void
299armpmu_del(struct perf_event *event, int flags)
300{
301 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
302 struct hw_perf_event *hwc = &event->hw;
303 int idx = hwc->idx;
304
305 WARN_ON(idx < 0);
306
307 clear_bit(idx, cpuc->active_mask);
308 armpmu_stop(event, PERF_EF_UPDATE);
309 cpuc->events[idx] = NULL;
310 clear_bit(idx, cpuc->used_mask);
311
312 perf_event_update_userpage(event);
313}
314
Jamie Iles1b8873a2010-02-02 20:25:44 +0100315static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200316armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100317{
318 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
319 struct hw_perf_event *hwc = &event->hw;
320 int idx;
321 int err = 0;
322
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200323 perf_pmu_disable(event->pmu);
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200324
Jamie Iles1b8873a2010-02-02 20:25:44 +0100325 /* If we don't have a space for the counter then finish early. */
326 idx = armpmu->get_event_idx(cpuc, hwc);
327 if (idx < 0) {
328 err = idx;
329 goto out;
330 }
331
332 /*
333 * If there is an event in the counter we are going to use then make
334 * sure it is disabled.
335 */
336 event->hw.idx = idx;
337 armpmu->disable(hwc, idx);
338 cpuc->events[idx] = event;
339 set_bit(idx, cpuc->active_mask);
340
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200341 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
342 if (flags & PERF_EF_START)
343 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100344
345 /* Propagate our changes to the userspace mapping. */
346 perf_event_update_userpage(event);
347
348out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200349 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100350 return err;
351}
352
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200353static struct pmu pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100354
355static int
356validate_event(struct cpu_hw_events *cpuc,
357 struct perf_event *event)
358{
359 struct hw_perf_event fake_event = event->hw;
360
Will Deacon65b47112010-09-02 09:32:08 +0100361 if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF)
362 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100363
364 return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
365}
366
367static int
368validate_group(struct perf_event *event)
369{
370 struct perf_event *sibling, *leader = event->group_leader;
371 struct cpu_hw_events fake_pmu;
372
373 memset(&fake_pmu, 0, sizeof(fake_pmu));
374
375 if (!validate_event(&fake_pmu, leader))
376 return -ENOSPC;
377
378 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
379 if (!validate_event(&fake_pmu, sibling))
380 return -ENOSPC;
381 }
382
383 if (!validate_event(&fake_pmu, event))
384 return -ENOSPC;
385
386 return 0;
387}
388
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530389static irqreturn_t armpmu_platform_irq(int irq, void *dev)
390{
391 struct arm_pmu_platdata *plat = dev_get_platdata(&pmu_device->dev);
392
393 return plat->handle_irq(irq, dev, armpmu->handle_irq);
394}
395
Jamie Iles1b8873a2010-02-02 20:25:44 +0100396static int
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500397armpmu_generic_request_irq(int irq, irq_handler_t *handle_irq)
398{
399 return request_irq(irq, *handle_irq,
400 IRQF_DISABLED | IRQF_NOBALANCING,
401 "armpmu", NULL);
402}
403
404static void
405armpmu_generic_free_irq(int irq)
406{
407 if (irq >= 0)
408 free_irq(irq, NULL);
409}
410
411static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100412armpmu_reserve_hardware(void)
413{
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530414 struct arm_pmu_platdata *plat;
415 irq_handler_t handle_irq;
Will Deacon49c006b2010-04-29 17:13:24 +0100416 int i, err = -ENODEV, irq;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100417
Will Deacon49c006b2010-04-29 17:13:24 +0100418 pmu_device = reserve_pmu(ARM_PMU_DEVICE_CPU);
419 if (IS_ERR(pmu_device)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100420 pr_warning("unable to reserve pmu\n");
Will Deacon49c006b2010-04-29 17:13:24 +0100421 return PTR_ERR(pmu_device);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100422 }
423
Will Deacon49c006b2010-04-29 17:13:24 +0100424 init_pmu(ARM_PMU_DEVICE_CPU);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100425
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530426 plat = dev_get_platdata(&pmu_device->dev);
427 if (plat && plat->handle_irq)
428 handle_irq = armpmu_platform_irq;
429 else
430 handle_irq = armpmu->handle_irq;
431
Will Deacon49c006b2010-04-29 17:13:24 +0100432 if (pmu_device->num_resources < 1) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100433 pr_err("no irqs for PMUs defined\n");
434 return -ENODEV;
435 }
436
Will Deacon49c006b2010-04-29 17:13:24 +0100437 for (i = 0; i < pmu_device->num_resources; ++i) {
438 irq = platform_get_irq(pmu_device, i);
439 if (irq < 0)
440 continue;
441
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500442 err = armpmu->request_pmu_irq(irq, &handle_irq);
443
Jamie Iles1b8873a2010-02-02 20:25:44 +0100444 if (err) {
Will Deacon49c006b2010-04-29 17:13:24 +0100445 pr_warning("unable to request IRQ%d for ARM perf "
446 "counters\n", irq);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100447 break;
448 }
449 }
450
451 if (err) {
Will Deacon49c006b2010-04-29 17:13:24 +0100452 for (i = i - 1; i >= 0; --i) {
453 irq = platform_get_irq(pmu_device, i);
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500454
455 armpmu->free_pmu_irq(irq);
Will Deacon49c006b2010-04-29 17:13:24 +0100456 }
457 release_pmu(pmu_device);
458 pmu_device = NULL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100459 }
460
461 return err;
462}
463
464static void
465armpmu_release_hardware(void)
466{
Will Deacon49c006b2010-04-29 17:13:24 +0100467 int i, irq;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100468
Will Deacon49c006b2010-04-29 17:13:24 +0100469 for (i = pmu_device->num_resources - 1; i >= 0; --i) {
470 irq = platform_get_irq(pmu_device, i);
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500471 armpmu->free_pmu_irq(irq);
Will Deacon49c006b2010-04-29 17:13:24 +0100472 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100473 armpmu->stop();
474
Will Deacon49c006b2010-04-29 17:13:24 +0100475 release_pmu(pmu_device);
476 pmu_device = NULL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100477}
478
Ashwin Chaugule133ddac2011-11-28 11:39:38 -0500479static int
480event_requires_mode_exclusion(struct perf_event_attr *attr)
481{
482 return attr->exclude_idle || attr->exclude_user ||
483 attr->exclude_kernel || attr->exclude_hv;
484}
485
Jamie Iles1b8873a2010-02-02 20:25:44 +0100486static atomic_t active_events = ATOMIC_INIT(0);
487static DEFINE_MUTEX(pmu_reserve_mutex);
488
489static void
490hw_perf_event_destroy(struct perf_event *event)
491{
492 if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
493 armpmu_release_hardware();
494 mutex_unlock(&pmu_reserve_mutex);
495 }
496}
497
498static int
499__hw_perf_event_init(struct perf_event *event)
500{
501 struct hw_perf_event *hwc = &event->hw;
502 int mapping, err;
503
504 /* Decode the generic type into an ARM event identifier. */
505 if (PERF_TYPE_HARDWARE == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000506 mapping = armpmu_map_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100507 } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
508 mapping = armpmu_map_cache_event(event->attr.config);
509 } else if (PERF_TYPE_RAW == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000510 mapping = armpmu_map_raw_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100511 } else {
512 pr_debug("event type %x not supported\n", event->attr.type);
513 return -EOPNOTSUPP;
514 }
515
516 if (mapping < 0) {
517 pr_debug("event %x:%llx not supported\n", event->attr.type,
518 event->attr.config);
519 return mapping;
520 }
521
Jamie Iles1b8873a2010-02-02 20:25:44 +0100522
523 /*
524 * We don't assign an index until we actually place the event onto
525 * hardware. Use -1 to signify that we haven't decided where to put it
526 * yet. For SMP systems, each core has it's own PMU so we can't do any
527 * clever allocation or constraints checking at this point.
528 */
529 hwc->idx = -1;
530
531 /*
532 * Store the event encoding into the config_base field. config and
533 * event_base are unused as the only 2 things we need to know are
534 * the event mapping and the counter to use. The counter to use is
535 * also the indx and the config_base is the event type.
536 */
Ashwin Chaugule133ddac2011-11-28 11:39:38 -0500537 hwc->config_base = 0;
538 hwc->config = 0;
539 hwc->event_base = 0;
540
541 if ((!armpmu->set_event_filter ||
542 armpmu->set_event_filter(hwc, &event->attr)) &&
543 event_requires_mode_exclusion(&event->attr)) {
544 pr_debug("ARM performance counters do not support "
545 "mode exclusion\n");
546 return -EPERM;
547 }
548
549 hwc->config_base |= (unsigned long)mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100550
551 if (!hwc->sample_period) {
552 hwc->sample_period = armpmu->max_period;
Ashwin Chaugule133ddac2011-11-28 11:39:38 -0500553 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200554 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100555 }
556
557 err = 0;
558 if (event->group_leader != event) {
559 err = validate_group(event);
560 if (err)
561 return -EINVAL;
562 }
563
564 return err;
565}
566
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200567static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100568{
569 int err = 0;
570
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200571 switch (event->attr.type) {
572 case PERF_TYPE_RAW:
573 case PERF_TYPE_HARDWARE:
574 case PERF_TYPE_HW_CACHE:
575 break;
576
577 default:
578 return -ENOENT;
579 }
580
Jamie Iles1b8873a2010-02-02 20:25:44 +0100581 if (!armpmu)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200582 return -ENODEV;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100583
584 event->destroy = hw_perf_event_destroy;
585
586 if (!atomic_inc_not_zero(&active_events)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100587 mutex_lock(&pmu_reserve_mutex);
588 if (atomic_read(&active_events) == 0) {
589 err = armpmu_reserve_hardware();
590 }
591
592 if (!err)
593 atomic_inc(&active_events);
594 mutex_unlock(&pmu_reserve_mutex);
595 }
596
597 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200598 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100599
600 err = __hw_perf_event_init(event);
601 if (err)
602 hw_perf_event_destroy(event);
603
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200604 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100605}
606
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200607static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100608{
609 /* Enable all of the perf events on hardware. */
Will Deaconf4f38432011-07-01 14:38:12 +0100610 int idx, enabled = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100611 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
612
613 if (!armpmu)
614 return;
615
616 for (idx = 0; idx <= armpmu->num_events; ++idx) {
617 struct perf_event *event = cpuc->events[idx];
618
619 if (!event)
620 continue;
621
622 armpmu->enable(&event->hw, idx);
Will Deaconf4f38432011-07-01 14:38:12 +0100623 enabled = 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100624 }
625
Will Deaconf4f38432011-07-01 14:38:12 +0100626 if (enabled)
627 armpmu->start();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100628}
629
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200630static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100631{
632 if (armpmu)
633 armpmu->stop();
634}
635
Ashwin Chauguleb5ca6872011-12-09 14:04:35 -0500636static void armpmu_update_counters(void)
637{
638 int idx;
639 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
640
641 if (!armpmu)
642 return;
643
644 for (idx = 0; idx <= armpmu->num_events; ++idx) {
645 struct perf_event *event = cpuc->events[idx];
646
647 if (!event)
648 continue;
649
650 armpmu_read(event);
651 }
652}
653
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200654static struct pmu pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200655 .pmu_enable = armpmu_enable,
656 .pmu_disable = armpmu_disable,
657 .event_init = armpmu_event_init,
658 .add = armpmu_add,
659 .del = armpmu_del,
660 .start = armpmu_start,
661 .stop = armpmu_stop,
662 .read = armpmu_read,
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200663};
664
Will Deacon43eab872010-11-13 19:04:32 +0000665/* Include the PMU-specific implementations. */
666#include "perf_event_xscale.c"
667#include "perf_event_v6.c"
668#include "perf_event_v7.c"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669#include "perf_event_msm.c"
670#include "perf_event_msm_l2.c"
671#include "perf_event_msm_krait.c"
672#include "perf_event_msm_krait_l2.c"
Will Deacon49e6a322010-04-30 11:33:33 +0100673
Ashwin Chaugule464983a2011-11-21 14:51:51 -0500674static int perf_cpu_pm_notifier(struct notifier_block *self, unsigned long cmd,
675 void *v)
676{
677 switch (cmd) {
678 case CPU_PM_ENTER:
Ashwin Chauguleb5ca6872011-12-09 14:04:35 -0500679 armpmu_update_counters();
Ashwin Chaugule464983a2011-11-21 14:51:51 -0500680 perf_pmu_disable(&pmu);
681 break;
682
683 case CPU_PM_ENTER_FAILED:
684 case CPU_PM_EXIT:
685 if (armpmu && armpmu->reset)
686 armpmu->reset(NULL);
687 perf_pmu_enable(&pmu);
688
689 break;
690 }
691
692 return NOTIFY_OK;
693}
694
695static struct notifier_block perf_cpu_pm_notifier_block = {
696 .notifier_call = perf_cpu_pm_notifier,
697};
698
Will Deacon574b69c2011-03-25 13:13:34 +0100699/*
700 * Ensure the PMU has sane values out of reset.
701 * This requires SMP to be available, so exists as a separate initcall.
702 */
703static int __init
704armpmu_reset(void)
705{
706 if (armpmu && armpmu->reset)
707 return on_each_cpu(armpmu->reset, NULL, 1);
708 return 0;
709}
710arch_initcall(armpmu_reset);
711
Jamie Iles1b8873a2010-02-02 20:25:44 +0100712static int __init
713init_hw_perf_events(void)
714{
715 unsigned long cpuid = read_cpuid_id();
716 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
717 unsigned long part_number = (cpuid & 0xFFF0);
718
Will Deacon49e6a322010-04-30 11:33:33 +0100719 /* ARM Ltd CPUs. */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100720 if (0x41 == implementor) {
721 switch (part_number) {
722 case 0xB360: /* ARM1136 */
723 case 0xB560: /* ARM1156 */
724 case 0xB760: /* ARM1176 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000725 armpmu = armv6pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100726 break;
727 case 0xB020: /* ARM11mpcore */
Will Deacon3cb314b2010-11-13 17:37:46 +0000728 armpmu = armv6mpcore_pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100729 break;
Jean PIHET796d1292010-01-26 18:51:05 +0100730 case 0xC080: /* Cortex-A8 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000731 armpmu = armv7_a8_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100732 break;
733 case 0xC090: /* Cortex-A9 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000734 armpmu = armv7_a9_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100735 break;
Will Deaconacdc46f2011-11-09 13:01:31 +0530736 case 0xC050: /* Cortex-A5 */
737 armpmu = armv7_a5_pmu_init();
738 break;
Will Deacon903bfa52011-11-09 13:01:57 +0530739 case 0xC0F0: /* Cortex-A15 */
740 armpmu = armv7_a15_pmu_init();
741 break;
Will Deacon49e6a322010-04-30 11:33:33 +0100742 }
743 /* Intel CPUs [xscale]. */
744 } else if (0x69 == implementor) {
745 part_number = (cpuid >> 13) & 0x7;
746 switch (part_number) {
747 case 1:
Will Deacon3cb314b2010-11-13 17:37:46 +0000748 armpmu = xscale1pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100749 break;
750 case 2:
Will Deacon3cb314b2010-11-13 17:37:46 +0000751 armpmu = xscale2pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100752 break;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100753 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754 /* Qualcomm CPUs */
755 } else if (0x51 == implementor) {
756 switch (part_number) {
757 case 0x00F0: /* 8x50 & 7x30*/
758 armpmu = armv7_scorpion_pmu_init();
759 break;
760 case 0x02D0: /* 8x60 */
761 armpmu = armv7_scorpionmp_pmu_init();
762 scorpionmp_l2_pmu_init();
763 break;
764 case 0x0490: /* 8960 sim */
765 case 0x04D0: /* 8960 */
Neil Leedered415112012-02-09 13:34:09 -0500766 case 0x06F0: /* 8064 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767 armpmu = armv7_krait_pmu_init();
768 krait_l2_pmu_init();
769 break;
770 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100771 }
772
Will Deacon49e6a322010-04-30 11:33:33 +0100773 if (armpmu) {
Jean PIHET796d1292010-01-26 18:51:05 +0100774 pr_info("enabled with %s PMU driver, %d counters available\n",
Will Deacon62994832010-11-13 18:45:27 +0000775 armpmu->name, armpmu->num_events);
Will Deacon49e6a322010-04-30 11:33:33 +0100776 } else {
777 pr_info("no hardware support available\n");
Will Deacon49e6a322010-04-30 11:33:33 +0100778 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100779
Peter Zijlstra2e80a822010-11-17 23:17:36 +0100780 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200781
Ashwin Chaugule464983a2011-11-21 14:51:51 -0500782 cpu_pm_register_notifier(&perf_cpu_pm_notifier_block);
783
Jamie Iles1b8873a2010-02-02 20:25:44 +0100784 return 0;
785}
Peter Zijlstra004417a2010-11-25 18:38:29 +0100786early_initcall(init_hw_perf_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100787
788/*
789 * Callchain handling code.
790 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100791
792/*
793 * The registers we're interested in are at the end of the variable
794 * length saved register structure. The fp points at the end of this
795 * structure so the address of this struct is:
796 * (struct frame_tail *)(xxx->fp)-1
797 *
798 * This code has been adapted from the ARM OProfile support.
799 */
800struct frame_tail {
Will Deacon4d6b7a72010-11-30 18:15:53 +0100801 struct frame_tail __user *fp;
802 unsigned long sp;
803 unsigned long lr;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100804} __attribute__((packed));
805
806/*
807 * Get the return address for a single stackframe and return a pointer to the
808 * next frame tail.
809 */
Will Deacon4d6b7a72010-11-30 18:15:53 +0100810static struct frame_tail __user *
811user_backtrace(struct frame_tail __user *tail,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100812 struct perf_callchain_entry *entry)
813{
814 struct frame_tail buftail;
815
816 /* Also check accessibility of one struct frame_tail beyond */
817 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
818 return NULL;
819 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
820 return NULL;
821
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200822 perf_callchain_store(entry, buftail.lr);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100823
824 /*
825 * Frame pointers should strictly progress back up the stack
826 * (towards higher addresses).
827 */
Rabin Vincentcb061992011-02-09 11:35:12 +0100828 if (tail + 1 >= buftail.fp)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100829 return NULL;
830
831 return buftail.fp - 1;
832}
833
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200834void
835perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100836{
Will Deacon4d6b7a72010-11-30 18:15:53 +0100837 struct frame_tail __user *tail;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100838
Jamie Iles1b8873a2010-02-02 20:25:44 +0100839
Will Deacon4d6b7a72010-11-30 18:15:53 +0100840 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100841
Sonny Rao860ad782011-04-18 22:12:59 +0100842 while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
843 tail && !((unsigned long)tail & 0x3))
Jamie Iles1b8873a2010-02-02 20:25:44 +0100844 tail = user_backtrace(tail, entry);
845}
846
847/*
848 * Gets called by walk_stackframe() for every stackframe. This will be called
849 * whist unwinding the stackframe and is like a subroutine return so we use
850 * the PC.
851 */
852static int
853callchain_trace(struct stackframe *fr,
854 void *data)
855{
856 struct perf_callchain_entry *entry = data;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200857 perf_callchain_store(entry, fr->pc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100858 return 0;
859}
860
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200861void
862perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100863{
864 struct stackframe fr;
865
Jamie Iles1b8873a2010-02-02 20:25:44 +0100866 fr.fp = regs->ARM_fp;
867 fr.sp = regs->ARM_sp;
868 fr.lr = regs->ARM_lr;
869 fr.pc = regs->ARM_pc;
870 walk_stackframe(&fr, callchain_trace, entry);
871}