blob: 6a7158b3218ffeff439c2d2f9324a66af2ad3497 [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);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076#ifdef CONFIG_SMP
77 void (*secondary_enable)(unsigned int irq);
78 void (*secondary_disable)(unsigned int irq);
79#endif
Jamie Iles1b8873a2010-02-02 20:25:44 +010080 void (*enable)(struct hw_perf_event *evt, int idx);
81 void (*disable)(struct hw_perf_event *evt, int idx);
Jamie Iles1b8873a2010-02-02 20:25:44 +010082 int (*get_event_idx)(struct cpu_hw_events *cpuc,
83 struct hw_perf_event *hwc);
84 u32 (*read_counter)(int idx);
85 void (*write_counter)(int idx, u32 val);
Ashwin Chaugule133ddac2011-11-28 11:39:38 -050086 int (*set_event_filter) (struct hw_perf_event *evt,
87 struct perf_event_attr *attr);
Jamie Iles1b8873a2010-02-02 20:25:44 +010088 void (*start)(void);
89 void (*stop)(void);
Will Deacon574b69c2011-03-25 13:13:34 +010090 void (*reset)(void *);
Ashwin Chaugule14da0032011-11-10 13:55:16 -050091 unsigned (*cache_map)[PERF_COUNT_HW_CACHE_MAX]
Will Deacon84fee972010-11-13 17:13:56 +000092 [PERF_COUNT_HW_CACHE_OP_MAX]
93 [PERF_COUNT_HW_CACHE_RESULT_MAX];
94 const unsigned (*event_map)[PERF_COUNT_HW_MAX];
95 u32 raw_event_mask;
Jamie Iles1b8873a2010-02-02 20:25:44 +010096 int num_events;
97 u64 max_period;
98};
99
100/* Set at runtime when we know what CPU type we are. */
101static const struct arm_pmu *armpmu;
102
Will Deacon181193f2010-04-30 11:32:44 +0100103enum arm_perf_pmu_ids
104armpmu_get_pmu_id(void)
105{
106 int id = -ENODEV;
107
108 if (armpmu != NULL)
109 id = armpmu->id;
110
111 return id;
112}
113EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
114
Will Deacon929f5192010-04-30 11:34:26 +0100115int
116armpmu_get_max_events(void)
117{
118 int max_events = 0;
119
120 if (armpmu != NULL)
121 max_events = armpmu->num_events;
122
123 return max_events;
124}
125EXPORT_SYMBOL_GPL(armpmu_get_max_events);
126
Matt Fleming3bf101b2010-09-27 20:22:24 +0100127int perf_num_counters(void)
128{
129 return armpmu_get_max_events();
130}
131EXPORT_SYMBOL_GPL(perf_num_counters);
132
Jamie Iles1b8873a2010-02-02 20:25:44 +0100133#define HW_OP_UNSUPPORTED 0xFFFF
134
135#define C(_x) \
136 PERF_COUNT_HW_CACHE_##_x
137
138#define CACHE_OP_UNSUPPORTED 0xFFFF
139
Jamie Iles1b8873a2010-02-02 20:25:44 +0100140static int
141armpmu_map_cache_event(u64 config)
142{
143 unsigned int cache_type, cache_op, cache_result, ret;
144
145 cache_type = (config >> 0) & 0xff;
146 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
147 return -EINVAL;
148
149 cache_op = (config >> 8) & 0xff;
150 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
151 return -EINVAL;
152
153 cache_result = (config >> 16) & 0xff;
154 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
155 return -EINVAL;
156
Will Deacon84fee972010-11-13 17:13:56 +0000157 ret = (int)(*armpmu->cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100158
159 if (ret == CACHE_OP_UNSUPPORTED)
160 return -ENOENT;
161
162 return ret;
163}
164
165static int
Will Deacon84fee972010-11-13 17:13:56 +0000166armpmu_map_event(u64 config)
167{
168 int mapping = (*armpmu->event_map)[config];
169 return mapping == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : mapping;
170}
171
172static int
173armpmu_map_raw_event(u64 config)
174{
175 return (int)(config & armpmu->raw_event_mask);
176}
177
178static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100179armpmu_event_set_period(struct perf_event *event,
180 struct hw_perf_event *hwc,
181 int idx)
182{
Peter Zijlstrae7850592010-05-21 14:43:08 +0200183 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100184 s64 period = hwc->sample_period;
185 int ret = 0;
186
187 if (unlikely(left <= -period)) {
188 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200189 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100190 hwc->last_period = period;
191 ret = 1;
192 }
193
194 if (unlikely(left <= 0)) {
195 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200196 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100197 hwc->last_period = period;
198 ret = 1;
199 }
200
201 if (left > (s64)armpmu->max_period)
202 left = armpmu->max_period;
203
Peter Zijlstrae7850592010-05-21 14:43:08 +0200204 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100205
206 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
207
208 perf_event_update_userpage(event);
209
210 return ret;
211}
212
213static u64
214armpmu_event_update(struct perf_event *event,
215 struct hw_perf_event *hwc,
Will Deacona7378232011-03-25 17:12:37 +0100216 int idx, int overflow)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100217{
Will Deacona7378232011-03-25 17:12:37 +0100218 u64 delta, prev_raw_count, new_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100219
220again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200221 prev_raw_count = local64_read(&hwc->prev_count);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100222 new_raw_count = armpmu->read_counter(idx);
223
Peter Zijlstrae7850592010-05-21 14:43:08 +0200224 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100225 new_raw_count) != prev_raw_count)
226 goto again;
227
Will Deacona7378232011-03-25 17:12:37 +0100228 new_raw_count &= armpmu->max_period;
229 prev_raw_count &= armpmu->max_period;
230
231 if (overflow)
Will Deacon67597882011-04-05 14:01:24 +0100232 delta = armpmu->max_period - prev_raw_count + new_raw_count + 1;
Will Deacona7378232011-03-25 17:12:37 +0100233 else
234 delta = new_raw_count - prev_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100235
Peter Zijlstrae7850592010-05-21 14:43:08 +0200236 local64_add(delta, &event->count);
237 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100238
239 return new_raw_count;
240}
241
242static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100243armpmu_read(struct perf_event *event)
244{
245 struct hw_perf_event *hwc = &event->hw;
246
247 /* Don't read disabled counters! */
248 if (hwc->idx < 0)
249 return;
Will Deacona7378232011-03-25 17:12:37 +0100250 armpmu_event_update(event, hwc, hwc->idx, 0);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100251}
252
253static void
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200254armpmu_stop(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100255{
256 struct hw_perf_event *hwc = &event->hw;
257
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200258 if (!armpmu)
259 return;
260
261 /*
262 * ARM pmu always has to update the counter, so ignore
263 * PERF_EF_UPDATE, see comments in armpmu_start().
264 */
265 if (!(hwc->state & PERF_HES_STOPPED)) {
266 armpmu->disable(hwc, hwc->idx);
267 barrier(); /* why? */
Will Deacona7378232011-03-25 17:12:37 +0100268 armpmu_event_update(event, hwc, hwc->idx, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200269 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
270 }
271}
272
273static void
274armpmu_start(struct perf_event *event, int flags)
275{
276 struct hw_perf_event *hwc = &event->hw;
277
278 if (!armpmu)
279 return;
280
281 /*
282 * ARM pmu always has to reprogram the period, so ignore
283 * PERF_EF_RELOAD, see the comment below.
284 */
285 if (flags & PERF_EF_RELOAD)
286 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
287
288 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100289 /*
290 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200291 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100292 * may have been left counting. If we don't do this step then we may
293 * get an interrupt too soon or *way* too late if the overflow has
294 * happened since disabling.
295 */
296 armpmu_event_set_period(event, hwc, hwc->idx);
297 armpmu->enable(hwc, hwc->idx);
298}
299
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200300static void
301armpmu_del(struct perf_event *event, int flags)
302{
303 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
304 struct hw_perf_event *hwc = &event->hw;
305 int idx = hwc->idx;
306
307 WARN_ON(idx < 0);
308
309 clear_bit(idx, cpuc->active_mask);
310 armpmu_stop(event, PERF_EF_UPDATE);
311 cpuc->events[idx] = NULL;
312 clear_bit(idx, cpuc->used_mask);
313
314 perf_event_update_userpage(event);
315}
316
Jamie Iles1b8873a2010-02-02 20:25:44 +0100317static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200318armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100319{
320 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
321 struct hw_perf_event *hwc = &event->hw;
322 int idx;
323 int err = 0;
324
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200325 perf_pmu_disable(event->pmu);
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200326
Jamie Iles1b8873a2010-02-02 20:25:44 +0100327 /* If we don't have a space for the counter then finish early. */
328 idx = armpmu->get_event_idx(cpuc, hwc);
329 if (idx < 0) {
330 err = idx;
331 goto out;
332 }
333
334 /*
335 * If there is an event in the counter we are going to use then make
336 * sure it is disabled.
337 */
338 event->hw.idx = idx;
339 armpmu->disable(hwc, idx);
340 cpuc->events[idx] = event;
341 set_bit(idx, cpuc->active_mask);
342
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200343 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
344 if (flags & PERF_EF_START)
345 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100346
347 /* Propagate our changes to the userspace mapping. */
348 perf_event_update_userpage(event);
349
350out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200351 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100352 return err;
353}
354
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200355static struct pmu pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100356
357static int
358validate_event(struct cpu_hw_events *cpuc,
359 struct perf_event *event)
360{
361 struct hw_perf_event fake_event = event->hw;
362
Will Deacon65b47112010-09-02 09:32:08 +0100363 if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF)
364 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100365
366 return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
367}
368
369static int
370validate_group(struct perf_event *event)
371{
372 struct perf_event *sibling, *leader = event->group_leader;
373 struct cpu_hw_events fake_pmu;
374
375 memset(&fake_pmu, 0, sizeof(fake_pmu));
376
377 if (!validate_event(&fake_pmu, leader))
378 return -ENOSPC;
379
380 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
381 if (!validate_event(&fake_pmu, sibling))
382 return -ENOSPC;
383 }
384
385 if (!validate_event(&fake_pmu, event))
386 return -ENOSPC;
387
388 return 0;
389}
390
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530391static irqreturn_t armpmu_platform_irq(int irq, void *dev)
392{
393 struct arm_pmu_platdata *plat = dev_get_platdata(&pmu_device->dev);
394
395 return plat->handle_irq(irq, dev, armpmu->handle_irq);
396}
397
Jamie Iles1b8873a2010-02-02 20:25:44 +0100398static int
399armpmu_reserve_hardware(void)
400{
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530401 struct arm_pmu_platdata *plat;
402 irq_handler_t handle_irq;
Will Deacon49c006b2010-04-29 17:13:24 +0100403 int i, err = -ENODEV, irq;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100404
Will Deacon49c006b2010-04-29 17:13:24 +0100405 pmu_device = reserve_pmu(ARM_PMU_DEVICE_CPU);
406 if (IS_ERR(pmu_device)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100407 pr_warning("unable to reserve pmu\n");
Will Deacon49c006b2010-04-29 17:13:24 +0100408 return PTR_ERR(pmu_device);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100409 }
410
Will Deacon49c006b2010-04-29 17:13:24 +0100411 init_pmu(ARM_PMU_DEVICE_CPU);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100412
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530413 plat = dev_get_platdata(&pmu_device->dev);
414 if (plat && plat->handle_irq)
415 handle_irq = armpmu_platform_irq;
416 else
417 handle_irq = armpmu->handle_irq;
418
Will Deacon49c006b2010-04-29 17:13:24 +0100419 if (pmu_device->num_resources < 1) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100420 pr_err("no irqs for PMUs defined\n");
421 return -ENODEV;
422 }
423
Will Deacon49c006b2010-04-29 17:13:24 +0100424 for (i = 0; i < pmu_device->num_resources; ++i) {
425 irq = platform_get_irq(pmu_device, i);
426 if (irq < 0)
427 continue;
428
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530429 err = request_irq(irq, handle_irq,
Will Deaconddee87f2010-02-25 15:04:14 +0100430 IRQF_DISABLED | IRQF_NOBALANCING,
431 "armpmu", NULL);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100432 if (err) {
Will Deacon49c006b2010-04-29 17:13:24 +0100433 pr_warning("unable to request IRQ%d for ARM perf "
434 "counters\n", irq);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100435 break;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436#ifdef CONFIG_SMP
437 } else if (armpmu->secondary_enable) {
438 armpmu->secondary_enable(irq);
439#endif
Jamie Iles1b8873a2010-02-02 20:25:44 +0100440 }
441 }
442
443 if (err) {
Will Deacon49c006b2010-04-29 17:13:24 +0100444 for (i = i - 1; i >= 0; --i) {
445 irq = platform_get_irq(pmu_device, i);
446 if (irq >= 0)
447 free_irq(irq, NULL);
448 }
449 release_pmu(pmu_device);
450 pmu_device = NULL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100451 }
452
453 return err;
454}
455
456static void
457armpmu_release_hardware(void)
458{
Will Deacon49c006b2010-04-29 17:13:24 +0100459 int i, irq;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100460
Will Deacon49c006b2010-04-29 17:13:24 +0100461 for (i = pmu_device->num_resources - 1; i >= 0; --i) {
462 irq = platform_get_irq(pmu_device, i);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700463 if (irq >= 0) {
Will Deacon49c006b2010-04-29 17:13:24 +0100464 free_irq(irq, NULL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465#ifdef CONFIG_SMP
466 if (armpmu->secondary_disable)
467 armpmu->secondary_disable(irq);
468#endif
469 }
Will Deacon49c006b2010-04-29 17:13:24 +0100470 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100471 armpmu->stop();
472
Will Deacon49c006b2010-04-29 17:13:24 +0100473 release_pmu(pmu_device);
474 pmu_device = NULL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100475}
476
Ashwin Chaugule133ddac2011-11-28 11:39:38 -0500477static int
478event_requires_mode_exclusion(struct perf_event_attr *attr)
479{
480 return attr->exclude_idle || attr->exclude_user ||
481 attr->exclude_kernel || attr->exclude_hv;
482}
483
Jamie Iles1b8873a2010-02-02 20:25:44 +0100484static atomic_t active_events = ATOMIC_INIT(0);
485static DEFINE_MUTEX(pmu_reserve_mutex);
486
487static void
488hw_perf_event_destroy(struct perf_event *event)
489{
490 if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
491 armpmu_release_hardware();
492 mutex_unlock(&pmu_reserve_mutex);
493 }
494}
495
496static int
497__hw_perf_event_init(struct perf_event *event)
498{
499 struct hw_perf_event *hwc = &event->hw;
500 int mapping, err;
501
502 /* Decode the generic type into an ARM event identifier. */
503 if (PERF_TYPE_HARDWARE == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000504 mapping = armpmu_map_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100505 } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
506 mapping = armpmu_map_cache_event(event->attr.config);
507 } else if (PERF_TYPE_RAW == event->attr.type) {
Will Deacon84fee972010-11-13 17:13:56 +0000508 mapping = armpmu_map_raw_event(event->attr.config);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100509 } else {
510 pr_debug("event type %x not supported\n", event->attr.type);
511 return -EOPNOTSUPP;
512 }
513
514 if (mapping < 0) {
515 pr_debug("event %x:%llx not supported\n", event->attr.type,
516 event->attr.config);
517 return mapping;
518 }
519
Jamie Iles1b8873a2010-02-02 20:25:44 +0100520
521 /*
522 * We don't assign an index until we actually place the event onto
523 * hardware. Use -1 to signify that we haven't decided where to put it
524 * yet. For SMP systems, each core has it's own PMU so we can't do any
525 * clever allocation or constraints checking at this point.
526 */
527 hwc->idx = -1;
528
529 /*
530 * Store the event encoding into the config_base field. config and
531 * event_base are unused as the only 2 things we need to know are
532 * the event mapping and the counter to use. The counter to use is
533 * also the indx and the config_base is the event type.
534 */
Ashwin Chaugule133ddac2011-11-28 11:39:38 -0500535 hwc->config_base = 0;
536 hwc->config = 0;
537 hwc->event_base = 0;
538
539 if ((!armpmu->set_event_filter ||
540 armpmu->set_event_filter(hwc, &event->attr)) &&
541 event_requires_mode_exclusion(&event->attr)) {
542 pr_debug("ARM performance counters do not support "
543 "mode exclusion\n");
544 return -EPERM;
545 }
546
547 hwc->config_base |= (unsigned long)mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100548
549 if (!hwc->sample_period) {
550 hwc->sample_period = armpmu->max_period;
Ashwin Chaugule133ddac2011-11-28 11:39:38 -0500551 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200552 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100553 }
554
555 err = 0;
556 if (event->group_leader != event) {
557 err = validate_group(event);
558 if (err)
559 return -EINVAL;
560 }
561
562 return err;
563}
564
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200565static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100566{
567 int err = 0;
568
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200569 switch (event->attr.type) {
570 case PERF_TYPE_RAW:
571 case PERF_TYPE_HARDWARE:
572 case PERF_TYPE_HW_CACHE:
573 break;
574
575 default:
576 return -ENOENT;
577 }
578
Jamie Iles1b8873a2010-02-02 20:25:44 +0100579 if (!armpmu)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200580 return -ENODEV;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100581
582 event->destroy = hw_perf_event_destroy;
583
584 if (!atomic_inc_not_zero(&active_events)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100585 mutex_lock(&pmu_reserve_mutex);
586 if (atomic_read(&active_events) == 0) {
587 err = armpmu_reserve_hardware();
588 }
589
590 if (!err)
591 atomic_inc(&active_events);
592 mutex_unlock(&pmu_reserve_mutex);
593 }
594
595 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200596 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100597
598 err = __hw_perf_event_init(event);
599 if (err)
600 hw_perf_event_destroy(event);
601
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200602 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100603}
604
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200605static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100606{
607 /* Enable all of the perf events on hardware. */
Will Deaconf4f38432011-07-01 14:38:12 +0100608 int idx, enabled = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100609 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
610
611 if (!armpmu)
612 return;
613
614 for (idx = 0; idx <= armpmu->num_events; ++idx) {
615 struct perf_event *event = cpuc->events[idx];
616
617 if (!event)
618 continue;
619
620 armpmu->enable(&event->hw, idx);
Will Deaconf4f38432011-07-01 14:38:12 +0100621 enabled = 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100622 }
623
Will Deaconf4f38432011-07-01 14:38:12 +0100624 if (enabled)
625 armpmu->start();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100626}
627
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200628static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100629{
630 if (armpmu)
631 armpmu->stop();
632}
633
Ashwin Chauguleb5ca6872011-12-09 14:04:35 -0500634static void armpmu_update_counters(void)
635{
636 int idx;
637 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
638
639 if (!armpmu)
640 return;
641
642 for (idx = 0; idx <= armpmu->num_events; ++idx) {
643 struct perf_event *event = cpuc->events[idx];
644
645 if (!event)
646 continue;
647
648 armpmu_read(event);
649 }
650}
651
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200652static struct pmu pmu = {
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200653 .pmu_enable = armpmu_enable,
654 .pmu_disable = armpmu_disable,
655 .event_init = armpmu_event_init,
656 .add = armpmu_add,
657 .del = armpmu_del,
658 .start = armpmu_start,
659 .stop = armpmu_stop,
660 .read = armpmu_read,
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200661};
662
Will Deacon43eab872010-11-13 19:04:32 +0000663/* Include the PMU-specific implementations. */
664#include "perf_event_xscale.c"
665#include "perf_event_v6.c"
666#include "perf_event_v7.c"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667#include "perf_event_msm.c"
668#include "perf_event_msm_l2.c"
669#include "perf_event_msm_krait.c"
670#include "perf_event_msm_krait_l2.c"
Will Deacon49e6a322010-04-30 11:33:33 +0100671
Ashwin Chaugule464983a2011-11-21 14:51:51 -0500672static int perf_cpu_pm_notifier(struct notifier_block *self, unsigned long cmd,
673 void *v)
674{
675 switch (cmd) {
676 case CPU_PM_ENTER:
Ashwin Chauguleb5ca6872011-12-09 14:04:35 -0500677 armpmu_update_counters();
Ashwin Chaugule464983a2011-11-21 14:51:51 -0500678 perf_pmu_disable(&pmu);
679 break;
680
681 case CPU_PM_ENTER_FAILED:
682 case CPU_PM_EXIT:
683 if (armpmu && armpmu->reset)
684 armpmu->reset(NULL);
685 perf_pmu_enable(&pmu);
686
687 break;
688 }
689
690 return NOTIFY_OK;
691}
692
693static struct notifier_block perf_cpu_pm_notifier_block = {
694 .notifier_call = perf_cpu_pm_notifier,
695};
696
Will Deacon574b69c2011-03-25 13:13:34 +0100697/*
698 * Ensure the PMU has sane values out of reset.
699 * This requires SMP to be available, so exists as a separate initcall.
700 */
701static int __init
702armpmu_reset(void)
703{
704 if (armpmu && armpmu->reset)
705 return on_each_cpu(armpmu->reset, NULL, 1);
706 return 0;
707}
708arch_initcall(armpmu_reset);
709
Jamie Iles1b8873a2010-02-02 20:25:44 +0100710static int __init
711init_hw_perf_events(void)
712{
713 unsigned long cpuid = read_cpuid_id();
714 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
715 unsigned long part_number = (cpuid & 0xFFF0);
716
Will Deacon49e6a322010-04-30 11:33:33 +0100717 /* ARM Ltd CPUs. */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100718 if (0x41 == implementor) {
719 switch (part_number) {
720 case 0xB360: /* ARM1136 */
721 case 0xB560: /* ARM1156 */
722 case 0xB760: /* ARM1176 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000723 armpmu = armv6pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100724 break;
725 case 0xB020: /* ARM11mpcore */
Will Deacon3cb314b2010-11-13 17:37:46 +0000726 armpmu = armv6mpcore_pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100727 break;
Jean PIHET796d1292010-01-26 18:51:05 +0100728 case 0xC080: /* Cortex-A8 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000729 armpmu = armv7_a8_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100730 break;
731 case 0xC090: /* Cortex-A9 */
Will Deacon3cb314b2010-11-13 17:37:46 +0000732 armpmu = armv7_a9_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100733 break;
Will Deaconacdc46f2011-11-09 13:01:31 +0530734 case 0xC050: /* Cortex-A5 */
735 armpmu = armv7_a5_pmu_init();
736 break;
Will Deacon903bfa52011-11-09 13:01:57 +0530737 case 0xC0F0: /* Cortex-A15 */
738 armpmu = armv7_a15_pmu_init();
739 break;
Will Deacon49e6a322010-04-30 11:33:33 +0100740 }
741 /* Intel CPUs [xscale]. */
742 } else if (0x69 == implementor) {
743 part_number = (cpuid >> 13) & 0x7;
744 switch (part_number) {
745 case 1:
Will Deacon3cb314b2010-11-13 17:37:46 +0000746 armpmu = xscale1pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100747 break;
748 case 2:
Will Deacon3cb314b2010-11-13 17:37:46 +0000749 armpmu = xscale2pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100750 break;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100751 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752 /* Qualcomm CPUs */
753 } else if (0x51 == implementor) {
754 switch (part_number) {
755 case 0x00F0: /* 8x50 & 7x30*/
756 armpmu = armv7_scorpion_pmu_init();
757 break;
758 case 0x02D0: /* 8x60 */
759 armpmu = armv7_scorpionmp_pmu_init();
760 scorpionmp_l2_pmu_init();
761 break;
762 case 0x0490: /* 8960 sim */
763 case 0x04D0: /* 8960 */
764 armpmu = armv7_krait_pmu_init();
765 krait_l2_pmu_init();
766 break;
767 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100768 }
769
Will Deacon49e6a322010-04-30 11:33:33 +0100770 if (armpmu) {
Jean PIHET796d1292010-01-26 18:51:05 +0100771 pr_info("enabled with %s PMU driver, %d counters available\n",
Will Deacon62994832010-11-13 18:45:27 +0000772 armpmu->name, armpmu->num_events);
Will Deacon49e6a322010-04-30 11:33:33 +0100773 } else {
774 pr_info("no hardware support available\n");
Will Deacon49e6a322010-04-30 11:33:33 +0100775 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100776
Peter Zijlstra2e80a822010-11-17 23:17:36 +0100777 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200778
Ashwin Chaugule464983a2011-11-21 14:51:51 -0500779 cpu_pm_register_notifier(&perf_cpu_pm_notifier_block);
780
Jamie Iles1b8873a2010-02-02 20:25:44 +0100781 return 0;
782}
Peter Zijlstra004417a2010-11-25 18:38:29 +0100783early_initcall(init_hw_perf_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100784
785/*
786 * Callchain handling code.
787 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100788
789/*
790 * The registers we're interested in are at the end of the variable
791 * length saved register structure. The fp points at the end of this
792 * structure so the address of this struct is:
793 * (struct frame_tail *)(xxx->fp)-1
794 *
795 * This code has been adapted from the ARM OProfile support.
796 */
797struct frame_tail {
Will Deacon4d6b7a72010-11-30 18:15:53 +0100798 struct frame_tail __user *fp;
799 unsigned long sp;
800 unsigned long lr;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100801} __attribute__((packed));
802
803/*
804 * Get the return address for a single stackframe and return a pointer to the
805 * next frame tail.
806 */
Will Deacon4d6b7a72010-11-30 18:15:53 +0100807static struct frame_tail __user *
808user_backtrace(struct frame_tail __user *tail,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100809 struct perf_callchain_entry *entry)
810{
811 struct frame_tail buftail;
812
813 /* Also check accessibility of one struct frame_tail beyond */
814 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
815 return NULL;
816 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
817 return NULL;
818
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200819 perf_callchain_store(entry, buftail.lr);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100820
821 /*
822 * Frame pointers should strictly progress back up the stack
823 * (towards higher addresses).
824 */
Rabin Vincentcb061992011-02-09 11:35:12 +0100825 if (tail + 1 >= buftail.fp)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100826 return NULL;
827
828 return buftail.fp - 1;
829}
830
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200831void
832perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100833{
Will Deacon4d6b7a72010-11-30 18:15:53 +0100834 struct frame_tail __user *tail;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100835
Jamie Iles1b8873a2010-02-02 20:25:44 +0100836
Will Deacon4d6b7a72010-11-30 18:15:53 +0100837 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100838
Sonny Rao860ad782011-04-18 22:12:59 +0100839 while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
840 tail && !((unsigned long)tail & 0x3))
Jamie Iles1b8873a2010-02-02 20:25:44 +0100841 tail = user_backtrace(tail, entry);
842}
843
844/*
845 * Gets called by walk_stackframe() for every stackframe. This will be called
846 * whist unwinding the stackframe and is like a subroutine return so we use
847 * the PC.
848 */
849static int
850callchain_trace(struct stackframe *fr,
851 void *data)
852{
853 struct perf_callchain_entry *entry = data;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200854 perf_callchain_store(entry, fr->pc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100855 return 0;
856}
857
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200858void
859perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100860{
861 struct stackframe fr;
862
Jamie Iles1b8873a2010-02-02 20:25:44 +0100863 fr.fp = regs->ARM_fp;
864 fr.sp = regs->ARM_sp;
865 fr.lr = regs->ARM_lr;
866 fr.pc = regs->ARM_pc;
867 walk_stackframe(&fr, callchain_trace, entry);
868}