blob: aaa631b8cbc2063994973e9e95229aa050cfebe6 [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/*
Will Deaconecf5a892011-07-19 22:43:28 +010030 * ARMv6 supports a maximum of 3 events, starting from index 0. If we add
Jamie Iles1b8873a2010-02-02 20:25:44 +010031 * another platform that supports more, we need to increase this to be the
32 * largest of all platforms.
Jean PIHET796d1292010-01-26 18:51:05 +010033 *
34 * ARMv7 supports up to 32 events:
35 * cycle counter CCNT + 31 events counters CNT0..30.
36 * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
Jamie Iles1b8873a2010-02-02 20:25:44 +010037 */
Will Deaconecf5a892011-07-19 22:43:28 +010038#define ARMPMU_MAX_HWEVENTS 32
Jamie Iles1b8873a2010-02-02 20:25:44 +010039
Mark Rutland3fc2c832011-06-24 11:30:59 +010040static DEFINE_PER_CPU(struct perf_event * [ARMPMU_MAX_HWEVENTS], hw_events);
41static DEFINE_PER_CPU(unsigned long [BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)], used_mask);
Mark Rutland8be3f9a2011-05-17 11:20:11 +010042static DEFINE_PER_CPU(struct pmu_hw_events, cpu_hw_events);
Will Deacon181193f2010-04-30 11:32:44 +010043
Mark Rutland8a16b342011-04-28 16:27:54 +010044#define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu))
45
Jamie Iles1b8873a2010-02-02 20:25:44 +010046/* Set at runtime when we know what CPU type we are. */
Mark Rutland8be3f9a2011-05-17 11:20:11 +010047static struct arm_pmu *cpu_pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +010048
Will Deacon181193f2010-04-30 11:32:44 +010049enum arm_perf_pmu_ids
50armpmu_get_pmu_id(void)
51{
52 int id = -ENODEV;
53
Mark Rutland8be3f9a2011-05-17 11:20:11 +010054 if (cpu_pmu != NULL)
55 id = cpu_pmu->id;
Will Deacon181193f2010-04-30 11:32:44 +010056
57 return id;
58}
59EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
60
Will Deacon929f5192010-04-30 11:34:26 +010061int
62armpmu_get_max_events(void)
63{
64 int max_events = 0;
65
Mark Rutland8be3f9a2011-05-17 11:20:11 +010066 if (cpu_pmu != NULL)
67 max_events = cpu_pmu->num_events;
Will Deacon929f5192010-04-30 11:34:26 +010068
69 return max_events;
70}
71EXPORT_SYMBOL_GPL(armpmu_get_max_events);
72
Matt Fleming3bf101b2010-09-27 20:22:24 +010073int perf_num_counters(void)
74{
75 return armpmu_get_max_events();
76}
77EXPORT_SYMBOL_GPL(perf_num_counters);
78
Jamie Iles1b8873a2010-02-02 20:25:44 +010079#define HW_OP_UNSUPPORTED 0xFFFF
80
81#define C(_x) \
82 PERF_COUNT_HW_CACHE_##_x
83
84#define CACHE_OP_UNSUPPORTED 0xFFFF
85
Jamie Iles1b8873a2010-02-02 20:25:44 +010086static int
Mark Rutlande1f431b2011-04-28 15:47:10 +010087armpmu_map_cache_event(const unsigned (*cache_map)
88 [PERF_COUNT_HW_CACHE_MAX]
89 [PERF_COUNT_HW_CACHE_OP_MAX]
90 [PERF_COUNT_HW_CACHE_RESULT_MAX],
91 u64 config)
Jamie Iles1b8873a2010-02-02 20:25:44 +010092{
93 unsigned int cache_type, cache_op, cache_result, ret;
94
95 cache_type = (config >> 0) & 0xff;
96 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
97 return -EINVAL;
98
99 cache_op = (config >> 8) & 0xff;
100 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
101 return -EINVAL;
102
103 cache_result = (config >> 16) & 0xff;
104 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
105 return -EINVAL;
106
Mark Rutlande1f431b2011-04-28 15:47:10 +0100107 ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100108
109 if (ret == CACHE_OP_UNSUPPORTED)
110 return -ENOENT;
111
112 return ret;
113}
114
115static int
Mark Rutlande1f431b2011-04-28 15:47:10 +0100116armpmu_map_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
Will Deacon84fee972010-11-13 17:13:56 +0000117{
Mark Rutlande1f431b2011-04-28 15:47:10 +0100118 int mapping = (*event_map)[config];
119 return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
Will Deacon84fee972010-11-13 17:13:56 +0000120}
121
122static int
Mark Rutlande1f431b2011-04-28 15:47:10 +0100123armpmu_map_raw_event(u32 raw_event_mask, u64 config)
Will Deacon84fee972010-11-13 17:13:56 +0000124{
Mark Rutlande1f431b2011-04-28 15:47:10 +0100125 return (int)(config & raw_event_mask);
126}
127
128static int map_cpu_event(struct perf_event *event,
129 const unsigned (*event_map)[PERF_COUNT_HW_MAX],
130 const unsigned (*cache_map)
131 [PERF_COUNT_HW_CACHE_MAX]
132 [PERF_COUNT_HW_CACHE_OP_MAX]
133 [PERF_COUNT_HW_CACHE_RESULT_MAX],
134 u32 raw_event_mask)
135{
136 u64 config = event->attr.config;
137
138 switch (event->attr.type) {
139 case PERF_TYPE_HARDWARE:
140 return armpmu_map_event(event_map, config);
141 case PERF_TYPE_HW_CACHE:
142 return armpmu_map_cache_event(cache_map, config);
143 case PERF_TYPE_RAW:
144 return armpmu_map_raw_event(raw_event_mask, config);
145 }
146
147 return -ENOENT;
Will Deacon84fee972010-11-13 17:13:56 +0000148}
149
Mark Rutland0ce47082011-05-19 10:07:57 +0100150int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100151armpmu_event_set_period(struct perf_event *event,
152 struct hw_perf_event *hwc,
153 int idx)
154{
Mark Rutland8a16b342011-04-28 16:27:54 +0100155 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Peter Zijlstrae7850592010-05-21 14:43:08 +0200156 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100157 s64 period = hwc->sample_period;
158 int ret = 0;
159
160 if (unlikely(left <= -period)) {
161 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200162 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100163 hwc->last_period = period;
164 ret = 1;
165 }
166
167 if (unlikely(left <= 0)) {
168 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200169 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100170 hwc->last_period = period;
171 ret = 1;
172 }
173
174 if (left > (s64)armpmu->max_period)
175 left = armpmu->max_period;
176
Peter Zijlstrae7850592010-05-21 14:43:08 +0200177 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100178
179 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
180
181 perf_event_update_userpage(event);
182
183 return ret;
184}
185
Mark Rutland0ce47082011-05-19 10:07:57 +0100186u64
Jamie Iles1b8873a2010-02-02 20:25:44 +0100187armpmu_event_update(struct perf_event *event,
188 struct hw_perf_event *hwc,
Will Deacona7378232011-03-25 17:12:37 +0100189 int idx, int overflow)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100190{
Mark Rutland8a16b342011-04-28 16:27:54 +0100191 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Will Deacona7378232011-03-25 17:12:37 +0100192 u64 delta, prev_raw_count, new_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100193
194again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200195 prev_raw_count = local64_read(&hwc->prev_count);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100196 new_raw_count = armpmu->read_counter(idx);
197
Peter Zijlstrae7850592010-05-21 14:43:08 +0200198 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100199 new_raw_count) != prev_raw_count)
200 goto again;
201
Will Deacona7378232011-03-25 17:12:37 +0100202 new_raw_count &= armpmu->max_period;
203 prev_raw_count &= armpmu->max_period;
204
205 if (overflow)
Will Deacon67597882011-04-05 14:01:24 +0100206 delta = armpmu->max_period - prev_raw_count + new_raw_count + 1;
Will Deacona7378232011-03-25 17:12:37 +0100207 else
208 delta = new_raw_count - prev_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100209
Peter Zijlstrae7850592010-05-21 14:43:08 +0200210 local64_add(delta, &event->count);
211 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100212
213 return new_raw_count;
214}
215
216static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100217armpmu_read(struct perf_event *event)
218{
219 struct hw_perf_event *hwc = &event->hw;
220
221 /* Don't read disabled counters! */
222 if (hwc->idx < 0)
223 return;
224
Will Deacona7378232011-03-25 17:12:37 +0100225 armpmu_event_update(event, hwc, hwc->idx, 0);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100226}
227
228static void
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200229armpmu_stop(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100230{
Mark Rutland8a16b342011-04-28 16:27:54 +0100231 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100232 struct hw_perf_event *hwc = &event->hw;
233
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200234 /*
235 * ARM pmu always has to update the counter, so ignore
236 * PERF_EF_UPDATE, see comments in armpmu_start().
237 */
238 if (!(hwc->state & PERF_HES_STOPPED)) {
239 armpmu->disable(hwc, hwc->idx);
240 barrier(); /* why? */
Will Deacona7378232011-03-25 17:12:37 +0100241 armpmu_event_update(event, hwc, hwc->idx, 0);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200242 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
243 }
244}
245
246static void
247armpmu_start(struct perf_event *event, int flags)
248{
Mark Rutland8a16b342011-04-28 16:27:54 +0100249 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200250 struct hw_perf_event *hwc = &event->hw;
251
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200252 /*
253 * ARM pmu always has to reprogram the period, so ignore
254 * PERF_EF_RELOAD, see the comment below.
255 */
256 if (flags & PERF_EF_RELOAD)
257 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
258
259 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100260 /*
261 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200262 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100263 * may have been left counting. If we don't do this step then we may
264 * get an interrupt too soon or *way* too late if the overflow has
265 * happened since disabling.
266 */
267 armpmu_event_set_period(event, hwc, hwc->idx);
268 armpmu->enable(hwc, hwc->idx);
269}
270
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200271static void
272armpmu_del(struct perf_event *event, int flags)
273{
Mark Rutland8a16b342011-04-28 16:27:54 +0100274 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100275 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200276 struct hw_perf_event *hwc = &event->hw;
277 int idx = hwc->idx;
278
279 WARN_ON(idx < 0);
280
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200281 armpmu_stop(event, PERF_EF_UPDATE);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100282 hw_events->events[idx] = NULL;
283 clear_bit(idx, hw_events->used_mask);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200284
285 perf_event_update_userpage(event);
286}
287
Jamie Iles1b8873a2010-02-02 20:25:44 +0100288static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200289armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100290{
Mark Rutland8a16b342011-04-28 16:27:54 +0100291 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100292 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100293 struct hw_perf_event *hwc = &event->hw;
294 int idx;
295 int err = 0;
296
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200297 perf_pmu_disable(event->pmu);
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200298
Jamie Iles1b8873a2010-02-02 20:25:44 +0100299 /* If we don't have a space for the counter then finish early. */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100300 idx = armpmu->get_event_idx(hw_events, hwc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100301 if (idx < 0) {
302 err = idx;
303 goto out;
304 }
305
306 /*
307 * If there is an event in the counter we are going to use then make
308 * sure it is disabled.
309 */
310 event->hw.idx = idx;
311 armpmu->disable(hwc, idx);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100312 hw_events->events[idx] = event;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100313
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200314 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
315 if (flags & PERF_EF_START)
316 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100317
318 /* Propagate our changes to the userspace mapping. */
319 perf_event_update_userpage(event);
320
321out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200322 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100323 return err;
324}
325
Jamie Iles1b8873a2010-02-02 20:25:44 +0100326static int
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100327validate_event(struct pmu_hw_events *hw_events,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100328 struct perf_event *event)
329{
Mark Rutland8a16b342011-04-28 16:27:54 +0100330 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100331 struct hw_perf_event fake_event = event->hw;
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100332 struct pmu *leader_pmu = event->group_leader->pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100333
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100334 if (event->pmu != leader_pmu || event->state <= PERF_EVENT_STATE_OFF)
Will Deacon65b47112010-09-02 09:32:08 +0100335 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100336
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100337 return armpmu->get_event_idx(hw_events, &fake_event) >= 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100338}
339
340static int
341validate_group(struct perf_event *event)
342{
343 struct perf_event *sibling, *leader = event->group_leader;
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100344 struct pmu_hw_events fake_pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100345
346 memset(&fake_pmu, 0, sizeof(fake_pmu));
347
348 if (!validate_event(&fake_pmu, leader))
349 return -ENOSPC;
350
351 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
352 if (!validate_event(&fake_pmu, sibling))
353 return -ENOSPC;
354 }
355
356 if (!validate_event(&fake_pmu, event))
357 return -ENOSPC;
358
359 return 0;
360}
361
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530362static irqreturn_t armpmu_platform_irq(int irq, void *dev)
363{
Mark Rutland8a16b342011-04-28 16:27:54 +0100364 struct arm_pmu *armpmu = (struct arm_pmu *) dev;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100365 struct platform_device *plat_device = armpmu->plat_device;
366 struct arm_pmu_platdata *plat = dev_get_platdata(&plat_device->dev);
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530367
368 return plat->handle_irq(irq, dev, armpmu->handle_irq);
369}
370
Will Deacon0b390e22011-07-27 15:18:59 +0100371static void
Mark Rutland8a16b342011-04-28 16:27:54 +0100372armpmu_release_hardware(struct arm_pmu *armpmu)
Will Deacon0b390e22011-07-27 15:18:59 +0100373{
374 int i, irq, irqs;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100375 struct platform_device *pmu_device = armpmu->plat_device;
Will Deacon0b390e22011-07-27 15:18:59 +0100376
377 irqs = min(pmu_device->num_resources, num_possible_cpus());
378
379 for (i = 0; i < irqs; ++i) {
380 if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs))
381 continue;
382 irq = platform_get_irq(pmu_device, i);
383 if (irq >= 0)
Mark Rutland8a16b342011-04-28 16:27:54 +0100384 free_irq(irq, armpmu);
Will Deacon0b390e22011-07-27 15:18:59 +0100385 }
386
Mark Rutland7ae18a52011-06-06 10:37:50 +0100387 release_pmu(armpmu->type);
Will Deacon0b390e22011-07-27 15:18:59 +0100388}
389
Jamie Iles1b8873a2010-02-02 20:25:44 +0100390static int
Mark Rutland8a16b342011-04-28 16:27:54 +0100391armpmu_reserve_hardware(struct arm_pmu *armpmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100392{
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530393 struct arm_pmu_platdata *plat;
394 irq_handler_t handle_irq;
Will Deaconb0e89592011-07-26 22:10:28 +0100395 int i, err, irq, irqs;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100396 struct platform_device *pmu_device = armpmu->plat_device;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100397
Mark Rutland7ae18a52011-06-06 10:37:50 +0100398 err = reserve_pmu(armpmu->type);
Will Deaconb0e89592011-07-26 22:10:28 +0100399 if (err) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100400 pr_warning("unable to reserve pmu\n");
Will Deaconb0e89592011-07-26 22:10:28 +0100401 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100402 }
403
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530404 plat = dev_get_platdata(&pmu_device->dev);
405 if (plat && plat->handle_irq)
406 handle_irq = armpmu_platform_irq;
407 else
408 handle_irq = armpmu->handle_irq;
409
Will Deacon0b390e22011-07-27 15:18:59 +0100410 irqs = min(pmu_device->num_resources, num_possible_cpus());
Will Deaconb0e89592011-07-26 22:10:28 +0100411 if (irqs < 1) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100412 pr_err("no irqs for PMUs defined\n");
413 return -ENODEV;
414 }
415
Will Deaconb0e89592011-07-26 22:10:28 +0100416 for (i = 0; i < irqs; ++i) {
Will Deacon0b390e22011-07-27 15:18:59 +0100417 err = 0;
Will Deacon49c006b2010-04-29 17:13:24 +0100418 irq = platform_get_irq(pmu_device, i);
419 if (irq < 0)
420 continue;
421
Will Deaconb0e89592011-07-26 22:10:28 +0100422 /*
423 * If we have a single PMU interrupt that we can't shift,
424 * assume that we're running on a uniprocessor machine and
Will Deacon0b390e22011-07-27 15:18:59 +0100425 * continue. Otherwise, continue without this interrupt.
Will Deaconb0e89592011-07-26 22:10:28 +0100426 */
Will Deacon0b390e22011-07-27 15:18:59 +0100427 if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
428 pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
429 irq, i);
430 continue;
Will Deaconb0e89592011-07-26 22:10:28 +0100431 }
432
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530433 err = request_irq(irq, handle_irq,
Will Deaconddee87f2010-02-25 15:04:14 +0100434 IRQF_DISABLED | IRQF_NOBALANCING,
Mark Rutland8a16b342011-04-28 16:27:54 +0100435 "arm-pmu", armpmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100436 if (err) {
Will Deaconb0e89592011-07-26 22:10:28 +0100437 pr_err("unable to request IRQ%d for ARM PMU counters\n",
438 irq);
Mark Rutland8a16b342011-04-28 16:27:54 +0100439 armpmu_release_hardware(armpmu);
Will Deacon0b390e22011-07-27 15:18:59 +0100440 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100441 }
Will Deacon0b390e22011-07-27 15:18:59 +0100442
443 cpumask_set_cpu(i, &armpmu->active_irqs);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100444 }
445
Will Deacon0b390e22011-07-27 15:18:59 +0100446 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100447}
448
Jamie Iles1b8873a2010-02-02 20:25:44 +0100449static void
450hw_perf_event_destroy(struct perf_event *event)
451{
Mark Rutland8a16b342011-04-28 16:27:54 +0100452 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland03b78982011-04-27 11:20:11 +0100453 atomic_t *active_events = &armpmu->active_events;
454 struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
455
456 if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
Mark Rutland8a16b342011-04-28 16:27:54 +0100457 armpmu_release_hardware(armpmu);
Mark Rutland03b78982011-04-27 11:20:11 +0100458 mutex_unlock(pmu_reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100459 }
460}
461
462static int
Will Deacon05d22fd2011-07-19 11:57:30 +0100463event_requires_mode_exclusion(struct perf_event_attr *attr)
464{
465 return attr->exclude_idle || attr->exclude_user ||
466 attr->exclude_kernel || attr->exclude_hv;
467}
468
469static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100470__hw_perf_event_init(struct perf_event *event)
471{
Mark Rutland8a16b342011-04-28 16:27:54 +0100472 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100473 struct hw_perf_event *hwc = &event->hw;
474 int mapping, err;
475
Mark Rutlande1f431b2011-04-28 15:47:10 +0100476 mapping = armpmu->map_event(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100477
478 if (mapping < 0) {
479 pr_debug("event %x:%llx not supported\n", event->attr.type,
480 event->attr.config);
481 return mapping;
482 }
483
484 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100485 * We don't assign an index until we actually place the event onto
486 * hardware. Use -1 to signify that we haven't decided where to put it
487 * yet. For SMP systems, each core has it's own PMU so we can't do any
488 * clever allocation or constraints checking at this point.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100489 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100490 hwc->idx = -1;
491 hwc->config_base = 0;
492 hwc->config = 0;
493 hwc->event_base = 0;
494
495 /*
496 * Check whether we need to exclude the counter from certain modes.
497 */
498 if ((!armpmu->set_event_filter ||
499 armpmu->set_event_filter(hwc, &event->attr)) &&
500 event_requires_mode_exclusion(&event->attr)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100501 pr_debug("ARM performance counters do not support "
502 "mode exclusion\n");
503 return -EPERM;
504 }
505
506 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100507 * Store the event encoding into the config_base field.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100508 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100509 hwc->config_base |= (unsigned long)mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100510
511 if (!hwc->sample_period) {
512 hwc->sample_period = armpmu->max_period;
513 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200514 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100515 }
516
517 err = 0;
518 if (event->group_leader != event) {
519 err = validate_group(event);
520 if (err)
521 return -EINVAL;
522 }
523
524 return err;
525}
526
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200527static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100528{
Mark Rutland8a16b342011-04-28 16:27:54 +0100529 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100530 int err = 0;
Mark Rutland03b78982011-04-27 11:20:11 +0100531 atomic_t *active_events = &armpmu->active_events;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100532
Mark Rutlande1f431b2011-04-28 15:47:10 +0100533 if (armpmu->map_event(event) == -ENOENT)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200534 return -ENOENT;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200535
Jamie Iles1b8873a2010-02-02 20:25:44 +0100536 event->destroy = hw_perf_event_destroy;
537
Mark Rutland03b78982011-04-27 11:20:11 +0100538 if (!atomic_inc_not_zero(active_events)) {
539 mutex_lock(&armpmu->reserve_mutex);
540 if (atomic_read(active_events) == 0)
Mark Rutland8a16b342011-04-28 16:27:54 +0100541 err = armpmu_reserve_hardware(armpmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100542
543 if (!err)
Mark Rutland03b78982011-04-27 11:20:11 +0100544 atomic_inc(active_events);
545 mutex_unlock(&armpmu->reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100546 }
547
548 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200549 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100550
551 err = __hw_perf_event_init(event);
552 if (err)
553 hw_perf_event_destroy(event);
554
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200555 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100556}
557
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200558static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100559{
560 /* Enable all of the perf events on hardware. */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100561 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Will Deaconf4f38432011-07-01 14:38:12 +0100562 int idx, enabled = 0;
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100563 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100564
Will Deaconecf5a892011-07-19 22:43:28 +0100565 for (idx = 0; idx < armpmu->num_events; ++idx) {
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100566 struct perf_event *event = hw_events->events[idx];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100567
568 if (!event)
569 continue;
570
571 armpmu->enable(&event->hw, idx);
Will Deaconf4f38432011-07-01 14:38:12 +0100572 enabled = 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100573 }
574
Will Deaconf4f38432011-07-01 14:38:12 +0100575 if (enabled)
576 armpmu->start();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100577}
578
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200579static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100580{
Mark Rutland8a16b342011-04-28 16:27:54 +0100581 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Mark Rutland48957152011-04-27 10:31:51 +0100582 armpmu->stop();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100583}
584
Mark Rutland03b78982011-04-27 11:20:11 +0100585static void __init armpmu_init(struct arm_pmu *armpmu)
586{
587 atomic_set(&armpmu->active_events, 0);
588 mutex_init(&armpmu->reserve_mutex);
Mark Rutland8a16b342011-04-28 16:27:54 +0100589
590 armpmu->pmu = (struct pmu) {
591 .pmu_enable = armpmu_enable,
592 .pmu_disable = armpmu_disable,
593 .event_init = armpmu_event_init,
594 .add = armpmu_add,
595 .del = armpmu_del,
596 .start = armpmu_start,
597 .stop = armpmu_stop,
598 .read = armpmu_read,
599 };
600}
601
Mark Rutland0ce47082011-05-19 10:07:57 +0100602int __init armpmu_register(struct arm_pmu *armpmu, char *name, int type)
Mark Rutland8a16b342011-04-28 16:27:54 +0100603{
604 armpmu_init(armpmu);
605 return perf_pmu_register(&armpmu->pmu, name, type);
Mark Rutland03b78982011-04-27 11:20:11 +0100606}
607
Will Deacon43eab872010-11-13 19:04:32 +0000608/* Include the PMU-specific implementations. */
609#include "perf_event_xscale.c"
610#include "perf_event_v6.c"
611#include "perf_event_v7.c"
Will Deacon49e6a322010-04-30 11:33:33 +0100612
Will Deacon574b69c2011-03-25 13:13:34 +0100613/*
614 * Ensure the PMU has sane values out of reset.
615 * This requires SMP to be available, so exists as a separate initcall.
616 */
617static int __init
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100618cpu_pmu_reset(void)
Will Deacon574b69c2011-03-25 13:13:34 +0100619{
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100620 if (cpu_pmu && cpu_pmu->reset)
621 return on_each_cpu(cpu_pmu->reset, NULL, 1);
Will Deacon574b69c2011-03-25 13:13:34 +0100622 return 0;
623}
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100624arch_initcall(cpu_pmu_reset);
Will Deacon574b69c2011-03-25 13:13:34 +0100625
Will Deaconb0e89592011-07-26 22:10:28 +0100626/*
627 * PMU platform driver and devicetree bindings.
628 */
629static struct of_device_id armpmu_of_device_ids[] = {
630 {.compatible = "arm,cortex-a9-pmu"},
631 {.compatible = "arm,cortex-a8-pmu"},
632 {.compatible = "arm,arm1136-pmu"},
633 {.compatible = "arm,arm1176-pmu"},
634 {},
635};
636
637static struct platform_device_id armpmu_plat_device_ids[] = {
638 {.name = "arm-pmu"},
639 {},
640};
641
642static int __devinit armpmu_device_probe(struct platform_device *pdev)
643{
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100644 cpu_pmu->plat_device = pdev;
Will Deaconb0e89592011-07-26 22:10:28 +0100645 return 0;
646}
647
648static struct platform_driver armpmu_driver = {
649 .driver = {
650 .name = "arm-pmu",
651 .of_match_table = armpmu_of_device_ids,
652 },
653 .probe = armpmu_device_probe,
654 .id_table = armpmu_plat_device_ids,
655};
656
657static int __init register_pmu_driver(void)
658{
659 return platform_driver_register(&armpmu_driver);
660}
661device_initcall(register_pmu_driver);
662
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100663static struct pmu_hw_events *armpmu_get_cpu_events(void)
Mark Rutland92f701e2011-05-04 09:23:51 +0100664{
665 return &__get_cpu_var(cpu_hw_events);
666}
667
668static void __init cpu_pmu_init(struct arm_pmu *armpmu)
669{
Mark Rutland0f78d2d2011-04-28 10:17:04 +0100670 int cpu;
671 for_each_possible_cpu(cpu) {
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100672 struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
Mark Rutland3fc2c832011-06-24 11:30:59 +0100673 events->events = per_cpu(hw_events, cpu);
674 events->used_mask = per_cpu(used_mask, cpu);
Mark Rutland0f78d2d2011-04-28 10:17:04 +0100675 raw_spin_lock_init(&events->pmu_lock);
676 }
Mark Rutland92f701e2011-05-04 09:23:51 +0100677 armpmu->get_hw_events = armpmu_get_cpu_events;
Mark Rutland7ae18a52011-06-06 10:37:50 +0100678 armpmu->type = ARM_PMU_DEVICE_CPU;
Mark Rutland92f701e2011-05-04 09:23:51 +0100679}
680
Will Deaconb0e89592011-07-26 22:10:28 +0100681/*
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 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100697 cpu_pmu = armv6pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100698 break;
699 case 0xB020: /* ARM11mpcore */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100700 cpu_pmu = armv6mpcore_pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100701 break;
Jean PIHET796d1292010-01-26 18:51:05 +0100702 case 0xC080: /* Cortex-A8 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100703 cpu_pmu = armv7_a8_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100704 break;
705 case 0xC090: /* Cortex-A9 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100706 cpu_pmu = 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 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100709 cpu_pmu = armv7_a5_pmu_init();
Will Deacon0c205cb2011-06-03 17:40:15 +0100710 break;
Will Deacon14abd032011-01-19 14:24:38 +0000711 case 0xC0F0: /* Cortex-A15 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100712 cpu_pmu = armv7_a15_pmu_init();
Will Deacon14abd032011-01-19 14:24:38 +0000713 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:
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100720 cpu_pmu = xscale1pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100721 break;
722 case 2:
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100723 cpu_pmu = xscale2pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100724 break;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100725 }
726 }
727
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100728 if (cpu_pmu) {
Jean PIHET796d1292010-01-26 18:51:05 +0100729 pr_info("enabled with %s PMU driver, %d counters available\n",
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100730 cpu_pmu->name, cpu_pmu->num_events);
731 cpu_pmu_init(cpu_pmu);
732 armpmu_register(cpu_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}