blob: e422f4c269a02bcce8d38fd800e5a42e54f5583b [file] [log] [blame]
Jamie Iles1b8873a2010-02-02 20:25:44 +01001#undef DEBUG
2
3/*
4 * ARM performance counter support.
5 *
6 * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
Will Deacon43eab872010-11-13 19:04:32 +00007 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
Jean PIHET796d1292010-01-26 18:51:05 +01008 *
Jamie Iles1b8873a2010-02-02 20:25:44 +01009 * This code is based on the sparc64 perf event code, which is in turn based
10 * on the x86 code. Callchain code is based on the ARM OProfile backtrace
11 * code.
12 */
13#define pr_fmt(fmt) "hw perfevents: " fmt
14
15#include <linux/interrupt.h>
16#include <linux/kernel.h>
Will Deacon181193f2010-04-30 11:32:44 +010017#include <linux/module.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010018#include <linux/perf_event.h>
Will Deacon49c006b2010-04-29 17:13:24 +010019#include <linux/platform_device.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010020#include <linux/spinlock.h>
21#include <linux/uaccess.h>
22
23#include <asm/cputype.h>
24#include <asm/irq.h>
25#include <asm/irq_regs.h>
26#include <asm/pmu.h>
27#include <asm/stacktrace.h>
28
Will Deacon49c006b2010-04-29 17:13:24 +010029static struct platform_device *pmu_device;
Jamie Iles1b8873a2010-02-02 20:25:44 +010030
31/*
32 * Hardware lock to serialize accesses to PMU registers. Needed for the
33 * read/modify/write sequences.
34 */
Will Deacon961ec6da2010-12-02 18:01:49 +010035static DEFINE_RAW_SPINLOCK(pmu_lock);
Jamie Iles1b8873a2010-02-02 20:25:44 +010036
37/*
38 * ARMv6 supports a maximum of 3 events, starting from index 1. If we add
39 * another platform that supports more, we need to increase this to be the
40 * largest of all platforms.
Jean PIHET796d1292010-01-26 18:51:05 +010041 *
42 * ARMv7 supports up to 32 events:
43 * cycle counter CCNT + 31 events counters CNT0..30.
44 * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
Jamie Iles1b8873a2010-02-02 20:25:44 +010045 */
Jean PIHET796d1292010-01-26 18:51:05 +010046#define ARMPMU_MAX_HWEVENTS 33
Jamie Iles1b8873a2010-02-02 20:25:44 +010047
48/* The events for a given CPU. */
49struct cpu_hw_events {
50 /*
51 * The events that are active on the CPU for the given index. Index 0
52 * is reserved.
53 */
54 struct perf_event *events[ARMPMU_MAX_HWEVENTS];
55
56 /*
57 * A 1 bit for an index indicates that the counter is being used for
58 * an event. A 0 means that the counter can be used.
59 */
60 unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
61
62 /*
63 * A 1 bit for an index indicates that the counter is actively being
64 * used.
65 */
66 unsigned long active_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
67};
Will Deacon4d6b7a72010-11-30 18:15:53 +010068static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
Will Deacon181193f2010-04-30 11:32:44 +010069
Jamie Iles1b8873a2010-02-02 20:25:44 +010070struct arm_pmu {
Will Deacon181193f2010-04-30 11:32:44 +010071 enum arm_perf_pmu_ids id;
Will Deacon62994832010-11-13 18:45:27 +000072 const char *name;
Jamie Iles1b8873a2010-02-02 20:25:44 +010073 irqreturn_t (*handle_irq)(int irq_num, void *dev);
74 void (*enable)(struct hw_perf_event *evt, int idx);
75 void (*disable)(struct hw_perf_event *evt, int idx);
Jamie Iles1b8873a2010-02-02 20:25:44 +010076 int (*get_event_idx)(struct cpu_hw_events *cpuc,
77 struct hw_perf_event *hwc);
78 u32 (*read_counter)(int idx);
79 void (*write_counter)(int idx, u32 val);
80 void (*start)(void);
81 void (*stop)(void);
Will Deacon574b69c2011-03-25 13:13:34 +010082 void (*reset)(void *);
Will Deacon84fee972010-11-13 17:13:56 +000083 const unsigned (*cache_map)[PERF_COUNT_HW_CACHE_MAX]
84 [PERF_COUNT_HW_CACHE_OP_MAX]
85 [PERF_COUNT_HW_CACHE_RESULT_MAX];
86 const unsigned (*event_map)[PERF_COUNT_HW_MAX];
87 u32 raw_event_mask;
Jamie Iles1b8873a2010-02-02 20:25:44 +010088 int num_events;
89 u64 max_period;
90};
91
92/* Set at runtime when we know what CPU type we are. */
93static const struct arm_pmu *armpmu;
94
Will Deacon181193f2010-04-30 11:32:44 +010095enum arm_perf_pmu_ids
96armpmu_get_pmu_id(void)
97{
98 int id = -ENODEV;
99
100 if (armpmu != NULL)
101 id = armpmu->id;
102
103 return id;
104}
105EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
106
Will Deacon929f5192010-04-30 11:34:26 +0100107int
108armpmu_get_max_events(void)
109{
110 int max_events = 0;
111
112 if (armpmu != NULL)
113 max_events = armpmu->num_events;
114
115 return max_events;
116}
117EXPORT_SYMBOL_GPL(armpmu_get_max_events);
118
Matt Fleming3bf101b2010-09-27 20:22:24 +0100119int perf_num_counters(void)
120{
121 return armpmu_get_max_events();
122}
123EXPORT_SYMBOL_GPL(perf_num_counters);
124
Jamie Iles1b8873a2010-02-02 20:25:44 +0100125#define HW_OP_UNSUPPORTED 0xFFFF
126
127#define C(_x) \
128 PERF_COUNT_HW_CACHE_##_x
129
130#define CACHE_OP_UNSUPPORTED 0xFFFF
131
Jamie Iles1b8873a2010-02-02 20:25:44 +0100132static int
133armpmu_map_cache_event(u64 config)
134{
135 unsigned int cache_type, cache_op, cache_result, ret;
136
137 cache_type = (config >> 0) & 0xff;
138 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
139 return -EINVAL;
140
141 cache_op = (config >> 8) & 0xff;
142 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
143 return -EINVAL;
144
145 cache_result = (config >> 16) & 0xff;
146 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
147 return -EINVAL;
148
Will Deacon84fee972010-11-13 17:13:56 +0000149 ret = (int)(*armpmu->cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100150
151 if (ret == CACHE_OP_UNSUPPORTED)
152 return -ENOENT;
153
154 return ret;
155}
156
157static int
Will Deacon84fee972010-11-13 17:13:56 +0000158armpmu_map_event(u64 config)
159{
160 int mapping = (*armpmu->event_map)[config];
161 return mapping == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : mapping;
162}
163
164static int
165armpmu_map_raw_event(u64 config)
166{
167 return (int)(config & armpmu->raw_event_mask);
168}
169
170static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100171armpmu_event_set_period(struct perf_event *event,
172 struct hw_perf_event *hwc,
173 int idx)
174{
Peter Zijlstrae7850592010-05-21 14:43:08 +0200175 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100176 s64 period = hwc->sample_period;
177 int ret = 0;
178
179 if (unlikely(left <= -period)) {
180 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200181 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100182 hwc->last_period = period;
183 ret = 1;
184 }
185
186 if (unlikely(left <= 0)) {
187 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200188 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100189 hwc->last_period = period;
190 ret = 1;
191 }
192
193 if (left > (s64)armpmu->max_period)
194 left = armpmu->max_period;
195
Peter Zijlstrae7850592010-05-21 14:43:08 +0200196 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100197
198 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
199
200 perf_event_update_userpage(event);
201
202 return ret;
203}
204
205static u64
206armpmu_event_update(struct perf_event *event,
207 struct hw_perf_event *hwc,
208 int idx)
209{
210 int shift = 64 - 32;
211 s64 prev_raw_count, new_raw_count;
Will Deacon446a5a82010-07-02 16:41:52 +0100212 u64 delta;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100213
214again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200215 prev_raw_count = local64_read(&hwc->prev_count);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100216 new_raw_count = armpmu->read_counter(idx);
217
Peter Zijlstrae7850592010-05-21 14:43:08 +0200218 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100219 new_raw_count) != prev_raw_count)
220 goto again;
221
222 delta = (new_raw_count << shift) - (prev_raw_count << shift);
223 delta >>= shift;
224
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
240 armpmu_event_update(event, hwc, hwc->idx);
241}
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 if (!armpmu)
249 return;
250
251 /*
252 * ARM pmu always has to update the counter, so ignore
253 * PERF_EF_UPDATE, see comments in armpmu_start().
254 */
255 if (!(hwc->state & PERF_HES_STOPPED)) {
256 armpmu->disable(hwc, hwc->idx);
257 barrier(); /* why? */
258 armpmu_event_update(event, hwc, hwc->idx);
259 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
260 }
261}
262
263static void
264armpmu_start(struct perf_event *event, int flags)
265{
266 struct hw_perf_event *hwc = &event->hw;
267
268 if (!armpmu)
269 return;
270
271 /*
272 * ARM pmu always has to reprogram the period, so ignore
273 * PERF_EF_RELOAD, see the comment below.
274 */
275 if (flags & PERF_EF_RELOAD)
276 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
277
278 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100279 /*
280 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200281 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100282 * may have been left counting. If we don't do this step then we may
283 * get an interrupt too soon or *way* too late if the overflow has
284 * happened since disabling.
285 */
286 armpmu_event_set_period(event, hwc, hwc->idx);
287 armpmu->enable(hwc, hwc->idx);
288}
289
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200290static void
291armpmu_del(struct perf_event *event, int flags)
292{
293 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
294 struct hw_perf_event *hwc = &event->hw;
295 int idx = hwc->idx;
296
297 WARN_ON(idx < 0);
298
299 clear_bit(idx, cpuc->active_mask);
300 armpmu_stop(event, PERF_EF_UPDATE);
301 cpuc->events[idx] = NULL;
302 clear_bit(idx, cpuc->used_mask);
303
304 perf_event_update_userpage(event);
305}
306
Jamie Iles1b8873a2010-02-02 20:25:44 +0100307static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200308armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100309{
310 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
311 struct hw_perf_event *hwc = &event->hw;
312 int idx;
313 int err = 0;
314
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200315 perf_pmu_disable(event->pmu);
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200316
Jamie Iles1b8873a2010-02-02 20:25:44 +0100317 /* If we don't have a space for the counter then finish early. */
318 idx = armpmu->get_event_idx(cpuc, hwc);
319 if (idx < 0) {
320 err = idx;
321 goto out;
322 }
323
324 /*
325 * If there is an event in the counter we are going to use then make
326 * sure it is disabled.
327 */
328 event->hw.idx = idx;
329 armpmu->disable(hwc, idx);
330 cpuc->events[idx] = event;
331 set_bit(idx, cpuc->active_mask);
332
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200333 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
334 if (flags & PERF_EF_START)
335 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100336
337 /* Propagate our changes to the userspace mapping. */
338 perf_event_update_userpage(event);
339
340out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200341 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100342 return err;
343}
344
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200345static struct pmu pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100346
347static int
348validate_event(struct cpu_hw_events *cpuc,
349 struct perf_event *event)
350{
351 struct hw_perf_event fake_event = event->hw;
352
Will Deacon65b47112010-09-02 09:32:08 +0100353 if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF)
354 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100355
356 return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
357}
358
359static int
360validate_group(struct perf_event *event)
361{
362 struct perf_event *sibling, *leader = event->group_leader;
363 struct cpu_hw_events fake_pmu;
364
365 memset(&fake_pmu, 0, sizeof(fake_pmu));
366
367 if (!validate_event(&fake_pmu, leader))
368 return -ENOSPC;
369
370 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
371 if (!validate_event(&fake_pmu, sibling))
372 return -ENOSPC;
373 }
374
375 if (!validate_event(&fake_pmu, event))
376 return -ENOSPC;
377
378 return 0;
379}
380
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530381static irqreturn_t armpmu_platform_irq(int irq, void *dev)
382{
383 struct arm_pmu_platdata *plat = dev_get_platdata(&pmu_device->dev);
384
385 return plat->handle_irq(irq, dev, armpmu->handle_irq);
386}
387
Jamie Iles1b8873a2010-02-02 20:25:44 +0100388static int
389armpmu_reserve_hardware(void)
390{
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530391 struct arm_pmu_platdata *plat;
392 irq_handler_t handle_irq;
Will Deacon49c006b2010-04-29 17:13:24 +0100393 int i, err = -ENODEV, irq;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100394
Will Deacon49c006b2010-04-29 17:13:24 +0100395 pmu_device = reserve_pmu(ARM_PMU_DEVICE_CPU);
396 if (IS_ERR(pmu_device)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100397 pr_warning("unable to reserve pmu\n");
Will Deacon49c006b2010-04-29 17:13:24 +0100398 return PTR_ERR(pmu_device);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100399 }
400
Will Deacon49c006b2010-04-29 17:13:24 +0100401 init_pmu(ARM_PMU_DEVICE_CPU);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100402
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530403 plat = dev_get_platdata(&pmu_device->dev);
404 if (plat && plat->handle_irq)
405 handle_irq = armpmu_platform_irq;
406 else
407 handle_irq = armpmu->handle_irq;
408
Will Deacon49c006b2010-04-29 17:13:24 +0100409 if (pmu_device->num_resources < 1) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100410 pr_err("no irqs for PMUs defined\n");
411 return -ENODEV;
412 }
413
Will Deacon49c006b2010-04-29 17:13:24 +0100414 for (i = 0; i < pmu_device->num_resources; ++i) {
415 irq = platform_get_irq(pmu_device, i);
416 if (irq < 0)
417 continue;
418
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530419 err = request_irq(irq, handle_irq,
Will Deaconddee87f2010-02-25 15:04:14 +0100420 IRQF_DISABLED | IRQF_NOBALANCING,
421 "armpmu", NULL);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100422 if (err) {
Will Deacon49c006b2010-04-29 17:13:24 +0100423 pr_warning("unable to request IRQ%d for ARM perf "
424 "counters\n", irq);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100425 break;
426 }
427 }
428
429 if (err) {
Will Deacon49c006b2010-04-29 17:13:24 +0100430 for (i = i - 1; i >= 0; --i) {
431 irq = platform_get_irq(pmu_device, i);
432 if (irq >= 0)
433 free_irq(irq, NULL);
434 }
435 release_pmu(pmu_device);
436 pmu_device = NULL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100437 }
438
439 return err;
440}
441
442static void
443armpmu_release_hardware(void)
444{
Will Deacon49c006b2010-04-29 17:13:24 +0100445 int i, irq;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100446
Will Deacon49c006b2010-04-29 17:13:24 +0100447 for (i = pmu_device->num_resources - 1; i >= 0; --i) {
448 irq = platform_get_irq(pmu_device, i);
449 if (irq >= 0)
450 free_irq(irq, NULL);
451 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100452 armpmu->stop();
453
Will Deacon49c006b2010-04-29 17:13:24 +0100454 release_pmu(pmu_device);
455 pmu_device = NULL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100456}
457
458static atomic_t active_events = ATOMIC_INIT(0);
459static DEFINE_MUTEX(pmu_reserve_mutex);
460
461static void
462hw_perf_event_destroy(struct perf_event *event)
463{
464 if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
465 armpmu_release_hardware();
466 mutex_unlock(&pmu_reserve_mutex);
467 }
468}
469
470static int
471__hw_perf_event_init(struct perf_event *event)
472{
473 struct hw_perf_event *hwc = &event->hw;
474 int mapping, err;
475
476 /* Decode the generic type into an ARM event identifier. */
477 if (PERF_TYPE_HARDWARE == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000478 mapping = armpmu_map_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100479 } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
480 mapping = armpmu_map_cache_event(event->attr.config);
481 } else if (PERF_TYPE_RAW == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000482 mapping = armpmu_map_raw_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100483 } else {
484 pr_debug("event type %x not supported\n", event->attr.type);
485 return -EOPNOTSUPP;
486 }
487
488 if (mapping < 0) {
489 pr_debug("event %x:%llx not supported\n", event->attr.type,
490 event->attr.config);
491 return mapping;
492 }
493
494 /*
495 * Check whether we need to exclude the counter from certain modes.
496 * The ARM performance counters are on all of the time so if someone
497 * has asked us for some excludes then we have to fail.
498 */
499 if (event->attr.exclude_kernel || event->attr.exclude_user ||
500 event->attr.exclude_hv || event->attr.exclude_idle) {
501 pr_debug("ARM performance counters do not support "
502 "mode exclusion\n");
503 return -EPERM;
504 }
505
506 /*
507 * We don't assign an index until we actually place the event onto
508 * hardware. Use -1 to signify that we haven't decided where to put it
509 * yet. For SMP systems, each core has it's own PMU so we can't do any
510 * clever allocation or constraints checking at this point.
511 */
512 hwc->idx = -1;
513
514 /*
515 * Store the event encoding into the config_base field. config and
516 * event_base are unused as the only 2 things we need to know are
517 * the event mapping and the counter to use. The counter to use is
518 * also the indx and the config_base is the event type.
519 */
520 hwc->config_base = (unsigned long)mapping;
521 hwc->config = 0;
522 hwc->event_base = 0;
523
524 if (!hwc->sample_period) {
525 hwc->sample_period = armpmu->max_period;
526 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200527 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100528 }
529
530 err = 0;
531 if (event->group_leader != event) {
532 err = validate_group(event);
533 if (err)
534 return -EINVAL;
535 }
536
537 return err;
538}
539
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200540static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100541{
542 int err = 0;
543
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200544 switch (event->attr.type) {
545 case PERF_TYPE_RAW:
546 case PERF_TYPE_HARDWARE:
547 case PERF_TYPE_HW_CACHE:
548 break;
549
550 default:
551 return -ENOENT;
552 }
553
Jamie Iles1b8873a2010-02-02 20:25:44 +0100554 if (!armpmu)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200555 return -ENODEV;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100556
557 event->destroy = hw_perf_event_destroy;
558
559 if (!atomic_inc_not_zero(&active_events)) {
Ingo Molnar1efeb082010-10-14 08:09:42 +0200560 if (atomic_read(&active_events) > armpmu->num_events) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100561 atomic_dec(&active_events);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200562 return -ENOSPC;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100563 }
564
565 mutex_lock(&pmu_reserve_mutex);
566 if (atomic_read(&active_events) == 0) {
567 err = armpmu_reserve_hardware();
568 }
569
570 if (!err)
571 atomic_inc(&active_events);
572 mutex_unlock(&pmu_reserve_mutex);
573 }
574
575 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200576 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100577
578 err = __hw_perf_event_init(event);
579 if (err)
580 hw_perf_event_destroy(event);
581
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200582 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100583}
584
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200585static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100586{
587 /* Enable all of the perf events on hardware. */
588 int idx;
589 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
590
591 if (!armpmu)
592 return;
593
594 for (idx = 0; idx <= armpmu->num_events; ++idx) {
595 struct perf_event *event = cpuc->events[idx];
596
597 if (!event)
598 continue;
599
600 armpmu->enable(&event->hw, idx);
601 }
602
603 armpmu->start();
604}
605
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200606static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100607{
608 if (armpmu)
609 armpmu->stop();
610}
611
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200612static struct pmu pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200613 .pmu_enable = armpmu_enable,
614 .pmu_disable = armpmu_disable,
615 .event_init = armpmu_event_init,
616 .add = armpmu_add,
617 .del = armpmu_del,
618 .start = armpmu_start,
619 .stop = armpmu_stop,
620 .read = armpmu_read,
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200621};
622
Will Deacon43eab872010-11-13 19:04:32 +0000623/* Include the PMU-specific implementations. */
624#include "perf_event_xscale.c"
625#include "perf_event_v6.c"
626#include "perf_event_v7.c"
Will Deacon49e6a322010-04-30 11:33:33 +0100627
Will Deacon574b69c2011-03-25 13:13:34 +0100628/*
629 * Ensure the PMU has sane values out of reset.
630 * This requires SMP to be available, so exists as a separate initcall.
631 */
632static int __init
633armpmu_reset(void)
634{
635 if (armpmu && armpmu->reset)
636 return on_each_cpu(armpmu->reset, NULL, 1);
637 return 0;
638}
639arch_initcall(armpmu_reset);
640
Jamie Iles1b8873a2010-02-02 20:25:44 +0100641static int __init
642init_hw_perf_events(void)
643{
644 unsigned long cpuid = read_cpuid_id();
645 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
646 unsigned long part_number = (cpuid & 0xFFF0);
647
Will Deacon49e6a322010-04-30 11:33:33 +0100648 /* ARM Ltd CPUs. */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100649 if (0x41 == implementor) {
650 switch (part_number) {
651 case 0xB360: /* ARM1136 */
652 case 0xB560: /* ARM1156 */
653 case 0xB760: /* ARM1176 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000654 armpmu = armv6pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100655 break;
656 case 0xB020: /* ARM11mpcore */
Will Deacon3cb314b2010-11-13 17:37:46 +0000657 armpmu = armv6mpcore_pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100658 break;
Jean PIHET796d1292010-01-26 18:51:05 +0100659 case 0xC080: /* Cortex-A8 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000660 armpmu = armv7_a8_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100661 break;
662 case 0xC090: /* Cortex-A9 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000663 armpmu = armv7_a9_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100664 break;
Will Deacon49e6a322010-04-30 11:33:33 +0100665 }
666 /* Intel CPUs [xscale]. */
667 } else if (0x69 == implementor) {
668 part_number = (cpuid >> 13) & 0x7;
669 switch (part_number) {
670 case 1:
Will Deacon3cb314b2010-11-13 17:37:46 +0000671 armpmu = xscale1pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100672 break;
673 case 2:
Will Deacon3cb314b2010-11-13 17:37:46 +0000674 armpmu = xscale2pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100675 break;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100676 }
677 }
678
Will Deacon49e6a322010-04-30 11:33:33 +0100679 if (armpmu) {
Jean PIHET796d1292010-01-26 18:51:05 +0100680 pr_info("enabled with %s PMU driver, %d counters available\n",
Will Deacon62994832010-11-13 18:45:27 +0000681 armpmu->name, armpmu->num_events);
Will Deacon49e6a322010-04-30 11:33:33 +0100682 } else {
683 pr_info("no hardware support available\n");
Will Deacon49e6a322010-04-30 11:33:33 +0100684 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100685
Peter Zijlstra2e80a822010-11-17 23:17:36 +0100686 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200687
Jamie Iles1b8873a2010-02-02 20:25:44 +0100688 return 0;
689}
Peter Zijlstra004417a2010-11-25 18:38:29 +0100690early_initcall(init_hw_perf_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100691
692/*
693 * Callchain handling code.
694 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100695
696/*
697 * The registers we're interested in are at the end of the variable
698 * length saved register structure. The fp points at the end of this
699 * structure so the address of this struct is:
700 * (struct frame_tail *)(xxx->fp)-1
701 *
702 * This code has been adapted from the ARM OProfile support.
703 */
704struct frame_tail {
Will Deacon4d6b7a72010-11-30 18:15:53 +0100705 struct frame_tail __user *fp;
706 unsigned long sp;
707 unsigned long lr;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100708} __attribute__((packed));
709
710/*
711 * Get the return address for a single stackframe and return a pointer to the
712 * next frame tail.
713 */
Will Deacon4d6b7a72010-11-30 18:15:53 +0100714static struct frame_tail __user *
715user_backtrace(struct frame_tail __user *tail,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100716 struct perf_callchain_entry *entry)
717{
718 struct frame_tail buftail;
719
720 /* Also check accessibility of one struct frame_tail beyond */
721 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
722 return NULL;
723 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
724 return NULL;
725
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200726 perf_callchain_store(entry, buftail.lr);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100727
728 /*
729 * Frame pointers should strictly progress back up the stack
730 * (towards higher addresses).
731 */
Rabin Vincentcb061992011-02-09 11:35:12 +0100732 if (tail + 1 >= buftail.fp)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100733 return NULL;
734
735 return buftail.fp - 1;
736}
737
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200738void
739perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100740{
Will Deacon4d6b7a72010-11-30 18:15:53 +0100741 struct frame_tail __user *tail;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100742
Jamie Iles1b8873a2010-02-02 20:25:44 +0100743
Will Deacon4d6b7a72010-11-30 18:15:53 +0100744 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100745
746 while (tail && !((unsigned long)tail & 0x3))
747 tail = user_backtrace(tail, entry);
748}
749
750/*
751 * Gets called by walk_stackframe() for every stackframe. This will be called
752 * whist unwinding the stackframe and is like a subroutine return so we use
753 * the PC.
754 */
755static int
756callchain_trace(struct stackframe *fr,
757 void *data)
758{
759 struct perf_callchain_entry *entry = data;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200760 perf_callchain_store(entry, fr->pc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100761 return 0;
762}
763
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200764void
765perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100766{
767 struct stackframe fr;
768
Jamie Iles1b8873a2010-02-02 20:25:44 +0100769 fr.fp = regs->ARM_fp;
770 fr.sp = regs->ARM_sp;
771 fr.lr = regs->ARM_lr;
772 fr.pc = regs->ARM_pc;
773 walk_stackframe(&fr, callchain_trace, entry);
774}