blob: 7fdac82f76f3d4d9b0982156f317ccdb873c1b94 [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
Mark Rutland7325eae2011-08-23 11:59:49 +010015#include <linux/bitmap.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010016#include <linux/interrupt.h>
17#include <linux/kernel.h>
Paul Gortmakerecea4ab2011-07-22 10:58:34 -040018#include <linux/export.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010019#include <linux/perf_event.h>
Will Deacon49c006b2010-04-29 17:13:24 +010020#include <linux/platform_device.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010021#include <linux/spinlock.h>
22#include <linux/uaccess.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023#include <linux/irq.h>
Jamie Iles1b8873a2010-02-02 20:25:44 +010024
25#include <asm/cputype.h>
26#include <asm/irq.h>
27#include <asm/irq_regs.h>
28#include <asm/pmu.h>
29#include <asm/stacktrace.h>
30
Ashwin Chaugulef53fe442012-06-07 13:41:37 -040031#include <linux/cpu_pm.h>
32
Jamie Iles1b8873a2010-02-02 20:25:44 +010033/*
Will Deaconecf5a892011-07-19 22:43:28 +010034 * ARMv6 supports a maximum of 3 events, starting from index 0. If we add
Jamie Iles1b8873a2010-02-02 20:25:44 +010035 * another platform that supports more, we need to increase this to be the
36 * largest of all platforms.
Jean PIHET796d1292010-01-26 18:51:05 +010037 *
38 * ARMv7 supports up to 32 events:
39 * cycle counter CCNT + 31 events counters CNT0..30.
40 * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
Jamie Iles1b8873a2010-02-02 20:25:44 +010041 */
Will Deaconecf5a892011-07-19 22:43:28 +010042#define ARMPMU_MAX_HWEVENTS 32
Jamie Iles1b8873a2010-02-02 20:25:44 +010043
Mark Rutland3fc2c832011-06-24 11:30:59 +010044static DEFINE_PER_CPU(struct perf_event * [ARMPMU_MAX_HWEVENTS], hw_events);
45static DEFINE_PER_CPU(unsigned long [BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)], used_mask);
Mark Rutland8be3f9a2011-05-17 11:20:11 +010046static DEFINE_PER_CPU(struct pmu_hw_events, cpu_hw_events);
Will Deacon181193f2010-04-30 11:32:44 +010047
Mark Rutland8a16b342011-04-28 16:27:54 +010048#define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu))
49
Jamie Iles1b8873a2010-02-02 20:25:44 +010050/* Set at runtime when we know what CPU type we are. */
Mark Rutland8be3f9a2011-05-17 11:20:11 +010051static struct arm_pmu *cpu_pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +010052
Will Deacon181193f2010-04-30 11:32:44 +010053enum arm_perf_pmu_ids
54armpmu_get_pmu_id(void)
55{
56 int id = -ENODEV;
57
Mark Rutland8be3f9a2011-05-17 11:20:11 +010058 if (cpu_pmu != NULL)
59 id = cpu_pmu->id;
Will Deacon181193f2010-04-30 11:32:44 +010060
61 return id;
62}
63EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
64
Will Deaconfeb45d02011-11-14 10:33:05 +000065int perf_num_counters(void)
Will Deacon929f5192010-04-30 11:34:26 +010066{
67 int max_events = 0;
68
Mark Rutland8be3f9a2011-05-17 11:20:11 +010069 if (cpu_pmu != NULL)
70 max_events = cpu_pmu->num_events;
Will Deacon929f5192010-04-30 11:34:26 +010071
72 return max_events;
73}
Matt Fleming3bf101b2010-09-27 20:22:24 +010074EXPORT_SYMBOL_GPL(perf_num_counters);
75
Jamie Iles1b8873a2010-02-02 20:25:44 +010076#define HW_OP_UNSUPPORTED 0xFFFF
77
78#define C(_x) \
79 PERF_COUNT_HW_CACHE_##_x
80
81#define CACHE_OP_UNSUPPORTED 0xFFFF
82
Jamie Iles1b8873a2010-02-02 20:25:44 +010083static int
Steve Mucklef132c6c2012-06-06 18:30:57 -070084armpmu_map_cache_event(unsigned (*cache_map)
Mark Rutlande1f431b2011-04-28 15:47:10 +010085 [PERF_COUNT_HW_CACHE_MAX]
86 [PERF_COUNT_HW_CACHE_OP_MAX]
87 [PERF_COUNT_HW_CACHE_RESULT_MAX],
88 u64 config)
Jamie Iles1b8873a2010-02-02 20:25:44 +010089{
90 unsigned int cache_type, cache_op, cache_result, ret;
91
92 cache_type = (config >> 0) & 0xff;
93 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
94 return -EINVAL;
95
96 cache_op = (config >> 8) & 0xff;
97 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
98 return -EINVAL;
99
100 cache_result = (config >> 16) & 0xff;
101 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
102 return -EINVAL;
103
Mark Rutlande1f431b2011-04-28 15:47:10 +0100104 ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100105
106 if (ret == CACHE_OP_UNSUPPORTED)
107 return -ENOENT;
108
109 return ret;
110}
111
112static int
Mark Rutlande1f431b2011-04-28 15:47:10 +0100113armpmu_map_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
Will Deacon84fee972010-11-13 17:13:56 +0000114{
Mark Rutlande1f431b2011-04-28 15:47:10 +0100115 int mapping = (*event_map)[config];
116 return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
Will Deacon84fee972010-11-13 17:13:56 +0000117}
118
119static int
Mark Rutlande1f431b2011-04-28 15:47:10 +0100120armpmu_map_raw_event(u32 raw_event_mask, u64 config)
Will Deacon84fee972010-11-13 17:13:56 +0000121{
Mark Rutlande1f431b2011-04-28 15:47:10 +0100122 return (int)(config & raw_event_mask);
123}
124
125static int map_cpu_event(struct perf_event *event,
126 const unsigned (*event_map)[PERF_COUNT_HW_MAX],
Steve Mucklef132c6c2012-06-06 18:30:57 -0700127 unsigned (*cache_map)
Mark Rutlande1f431b2011-04-28 15:47:10 +0100128 [PERF_COUNT_HW_CACHE_MAX]
129 [PERF_COUNT_HW_CACHE_OP_MAX]
130 [PERF_COUNT_HW_CACHE_RESULT_MAX],
131 u32 raw_event_mask)
132{
133 u64 config = event->attr.config;
134
135 switch (event->attr.type) {
136 case PERF_TYPE_HARDWARE:
137 return armpmu_map_event(event_map, config);
138 case PERF_TYPE_HW_CACHE:
139 return armpmu_map_cache_event(cache_map, config);
140 case PERF_TYPE_RAW:
141 return armpmu_map_raw_event(raw_event_mask, config);
142 }
143
144 return -ENOENT;
Will Deacon84fee972010-11-13 17:13:56 +0000145}
146
Mark Rutland0ce47082011-05-19 10:07:57 +0100147int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100148armpmu_event_set_period(struct perf_event *event,
149 struct hw_perf_event *hwc,
150 int idx)
151{
Mark Rutland8a16b342011-04-28 16:27:54 +0100152 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Peter Zijlstrae7850592010-05-21 14:43:08 +0200153 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100154 s64 period = hwc->sample_period;
155 int ret = 0;
156
157 if (unlikely(left <= -period)) {
158 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200159 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100160 hwc->last_period = period;
161 ret = 1;
162 }
163
164 if (unlikely(left <= 0)) {
165 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200166 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100167 hwc->last_period = period;
168 ret = 1;
169 }
170
171 if (left > (s64)armpmu->max_period)
172 left = armpmu->max_period;
173
Peter Zijlstrae7850592010-05-21 14:43:08 +0200174 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100175
176 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
177
178 perf_event_update_userpage(event);
179
180 return ret;
181}
182
Mark Rutland0ce47082011-05-19 10:07:57 +0100183u64
Jamie Iles1b8873a2010-02-02 20:25:44 +0100184armpmu_event_update(struct perf_event *event,
185 struct hw_perf_event *hwc,
Will Deacon57273472012-03-06 17:33:17 +0100186 int idx)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100187{
Mark Rutland8a16b342011-04-28 16:27:54 +0100188 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Will Deacona7378232011-03-25 17:12:37 +0100189 u64 delta, prev_raw_count, new_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100190
191again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200192 prev_raw_count = local64_read(&hwc->prev_count);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100193 new_raw_count = armpmu->read_counter(idx);
194
Peter Zijlstrae7850592010-05-21 14:43:08 +0200195 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100196 new_raw_count) != prev_raw_count)
197 goto again;
198
Will Deacon57273472012-03-06 17:33:17 +0100199 delta = (new_raw_count - prev_raw_count) & armpmu->max_period;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100200
Peter Zijlstrae7850592010-05-21 14:43:08 +0200201 local64_add(delta, &event->count);
202 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100203
204 return new_raw_count;
205}
206
207static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100208armpmu_read(struct perf_event *event)
209{
210 struct hw_perf_event *hwc = &event->hw;
211
212 /* Don't read disabled counters! */
213 if (hwc->idx < 0)
214 return;
215
Will Deacon57273472012-03-06 17:33:17 +0100216 armpmu_event_update(event, hwc, hwc->idx);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100217}
218
219static void
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200220armpmu_stop(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100221{
Mark Rutland8a16b342011-04-28 16:27:54 +0100222 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100223 struct hw_perf_event *hwc = &event->hw;
224
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200225 /*
226 * ARM pmu always has to update the counter, so ignore
227 * PERF_EF_UPDATE, see comments in armpmu_start().
228 */
229 if (!(hwc->state & PERF_HES_STOPPED)) {
230 armpmu->disable(hwc, hwc->idx);
231 barrier(); /* why? */
Will Deacon57273472012-03-06 17:33:17 +0100232 armpmu_event_update(event, hwc, hwc->idx);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200233 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
234 }
235}
236
237static void
238armpmu_start(struct perf_event *event, int flags)
239{
Mark Rutland8a16b342011-04-28 16:27:54 +0100240 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200241 struct hw_perf_event *hwc = &event->hw;
242
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200243 /*
244 * ARM pmu always has to reprogram the period, so ignore
245 * PERF_EF_RELOAD, see the comment below.
246 */
247 if (flags & PERF_EF_RELOAD)
248 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
249
250 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100251 /*
252 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200253 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100254 * may have been left counting. If we don't do this step then we may
255 * get an interrupt too soon or *way* too late if the overflow has
256 * happened since disabling.
257 */
258 armpmu_event_set_period(event, hwc, hwc->idx);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700259 armpmu->enable(hwc, hwc->idx, event->cpu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100260}
261
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200262static void
263armpmu_del(struct perf_event *event, int flags)
264{
Mark Rutland8a16b342011-04-28 16:27:54 +0100265 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100266 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200267 struct hw_perf_event *hwc = &event->hw;
268 int idx = hwc->idx;
269
270 WARN_ON(idx < 0);
271
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200272 armpmu_stop(event, PERF_EF_UPDATE);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100273 hw_events->events[idx] = NULL;
274 clear_bit(idx, hw_events->used_mask);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200275
276 perf_event_update_userpage(event);
277}
278
Jamie Iles1b8873a2010-02-02 20:25:44 +0100279static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200280armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100281{
Mark Rutland8a16b342011-04-28 16:27:54 +0100282 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100283 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100284 struct hw_perf_event *hwc = &event->hw;
285 int idx;
286 int err = 0;
287
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200288 perf_pmu_disable(event->pmu);
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200289
Jamie Iles1b8873a2010-02-02 20:25:44 +0100290 /* If we don't have a space for the counter then finish early. */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100291 idx = armpmu->get_event_idx(hw_events, hwc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100292 if (idx < 0) {
293 err = idx;
294 goto out;
295 }
296
297 /*
298 * If there is an event in the counter we are going to use then make
299 * sure it is disabled.
300 */
301 event->hw.idx = idx;
302 armpmu->disable(hwc, idx);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100303 hw_events->events[idx] = event;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100304
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200305 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
306 if (flags & PERF_EF_START)
307 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100308
309 /* Propagate our changes to the userspace mapping. */
310 perf_event_update_userpage(event);
311
312out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200313 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100314 return err;
315}
316
Jamie Iles1b8873a2010-02-02 20:25:44 +0100317static int
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100318validate_event(struct pmu_hw_events *hw_events,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100319 struct perf_event *event)
320{
Mark Rutland8a16b342011-04-28 16:27:54 +0100321 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100322 struct hw_perf_event fake_event = event->hw;
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100323 struct pmu *leader_pmu = event->group_leader->pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100324
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100325 if (event->pmu != leader_pmu || event->state <= PERF_EVENT_STATE_OFF)
Will Deacon65b47112010-09-02 09:32:08 +0100326 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100327
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100328 return armpmu->get_event_idx(hw_events, &fake_event) >= 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100329}
330
331static int
332validate_group(struct perf_event *event)
333{
334 struct perf_event *sibling, *leader = event->group_leader;
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100335 struct pmu_hw_events fake_pmu;
Will Deaconbce34d12011-11-17 15:05:14 +0000336 DECLARE_BITMAP(fake_used_mask, ARMPMU_MAX_HWEVENTS);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100337
Will Deaconbce34d12011-11-17 15:05:14 +0000338 /*
339 * Initialise the fake PMU. We only need to populate the
340 * used_mask for the purposes of validation.
341 */
342 memset(fake_used_mask, 0, sizeof(fake_used_mask));
343 fake_pmu.used_mask = fake_used_mask;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100344
345 if (!validate_event(&fake_pmu, leader))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100346 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100347
348 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
349 if (!validate_event(&fake_pmu, sibling))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100350 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100351 }
352
353 if (!validate_event(&fake_pmu, event))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100354 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100355
356 return 0;
357}
358
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530359static irqreturn_t armpmu_platform_irq(int irq, void *dev)
360{
Mark Rutland8a16b342011-04-28 16:27:54 +0100361 struct arm_pmu *armpmu = (struct arm_pmu *) dev;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100362 struct platform_device *plat_device = armpmu->plat_device;
363 struct arm_pmu_platdata *plat = dev_get_platdata(&plat_device->dev);
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530364
365 return plat->handle_irq(irq, dev, armpmu->handle_irq);
366}
367
Steve Mucklef132c6c2012-06-06 18:30:57 -0700368int
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500369armpmu_generic_request_irq(int irq, irq_handler_t *handle_irq)
370{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700371 return request_irq(irq, *handle_irq,
372 IRQF_DISABLED | IRQF_NOBALANCING,
373 "armpmu", NULL);
374}
375
376void
377armpmu_generic_free_irq(int irq)
378{
379 if (irq >= 0)
380 free_irq(irq, NULL);
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500381}
382
Will Deacon0b390e22011-07-27 15:18:59 +0100383static void
Mark Rutland8a16b342011-04-28 16:27:54 +0100384armpmu_release_hardware(struct arm_pmu *armpmu)
Will Deacon0b390e22011-07-27 15:18:59 +0100385{
386 int i, irq, irqs;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100387 struct platform_device *pmu_device = armpmu->plat_device;
Ashwin Chaugulef53fe442012-06-07 13:41:37 -0400388
Will Deacon0b390e22011-07-27 15:18:59 +0100389 irqs = min(pmu_device->num_resources, num_possible_cpus());
390
391 for (i = 0; i < irqs; ++i) {
392 if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs))
393 continue;
394 irq = platform_get_irq(pmu_device, i);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700395 armpmu->free_pmu_irq(irq);
Will Deacon0b390e22011-07-27 15:18:59 +0100396 }
397
Mark Rutland7ae18a52011-06-06 10:37:50 +0100398 release_pmu(armpmu->type);
Will Deacon0b390e22011-07-27 15:18:59 +0100399}
400
Jamie Iles1b8873a2010-02-02 20:25:44 +0100401static int
Mark Rutland8a16b342011-04-28 16:27:54 +0100402armpmu_reserve_hardware(struct arm_pmu *armpmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100403{
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530404 struct arm_pmu_platdata *plat;
405 irq_handler_t handle_irq;
Will Deaconb0e89592011-07-26 22:10:28 +0100406 int i, err, irq, irqs;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100407 struct platform_device *pmu_device = armpmu->plat_device;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100408
Will Deacone5a21322011-11-22 18:01:46 +0000409 if (!pmu_device)
410 return -ENODEV;
411
Mark Rutland7ae18a52011-06-06 10:37:50 +0100412 err = reserve_pmu(armpmu->type);
Will Deaconb0e89592011-07-26 22:10:28 +0100413 if (err) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100414 pr_warning("unable to reserve pmu\n");
Will Deaconb0e89592011-07-26 22:10:28 +0100415 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100416 }
417
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530418 plat = dev_get_platdata(&pmu_device->dev);
419 if (plat && plat->handle_irq)
420 handle_irq = armpmu_platform_irq;
421 else
422 handle_irq = armpmu->handle_irq;
423
Will Deacon0b390e22011-07-27 15:18:59 +0100424 irqs = min(pmu_device->num_resources, num_possible_cpus());
Will Deaconb0e89592011-07-26 22:10:28 +0100425 if (irqs < 1) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100426 pr_err("no irqs for PMUs defined\n");
427 return -ENODEV;
428 }
429
Will Deaconb0e89592011-07-26 22:10:28 +0100430 for (i = 0; i < irqs; ++i) {
Will Deacon0b390e22011-07-27 15:18:59 +0100431 err = 0;
Will Deacon49c006b2010-04-29 17:13:24 +0100432 irq = platform_get_irq(pmu_device, i);
433 if (irq < 0)
434 continue;
435
Will Deaconb0e89592011-07-26 22:10:28 +0100436 /*
437 * If we have a single PMU interrupt that we can't shift,
438 * assume that we're running on a uniprocessor machine and
Will Deacon0b390e22011-07-27 15:18:59 +0100439 * continue. Otherwise, continue without this interrupt.
Will Deaconb0e89592011-07-26 22:10:28 +0100440 */
Will Deacon0b390e22011-07-27 15:18:59 +0100441 if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
442 pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
443 irq, i);
444 continue;
Will Deaconb0e89592011-07-26 22:10:28 +0100445 }
446
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500447 err = armpmu->request_pmu_irq(irq, &handle_irq);
448
Steve Mucklef132c6c2012-06-06 18:30:57 -0700449 if (err) {
450 pr_warning("unable to request IRQ%d for %s perf "
451 "counters\n", irq, armpmu->name);
452
453 armpmu_release_hardware(cpu_pmu);
454 return err;
455 }
456
Will Deacon0b390e22011-07-27 15:18:59 +0100457 cpumask_set_cpu(i, &armpmu->active_irqs);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100458 }
459
Will Deacon0b390e22011-07-27 15:18:59 +0100460 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100461}
462
Jamie Iles1b8873a2010-02-02 20:25:44 +0100463static void
464hw_perf_event_destroy(struct perf_event *event)
465{
Mark Rutland8a16b342011-04-28 16:27:54 +0100466 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland03b78982011-04-27 11:20:11 +0100467 atomic_t *active_events = &armpmu->active_events;
468 struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
469
470 if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
Mark Rutland8a16b342011-04-28 16:27:54 +0100471 armpmu_release_hardware(armpmu);
Mark Rutland03b78982011-04-27 11:20:11 +0100472 mutex_unlock(pmu_reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100473 }
474}
475
476static int
Will Deacon05d22fd2011-07-19 11:57:30 +0100477event_requires_mode_exclusion(struct perf_event_attr *attr)
478{
479 return attr->exclude_idle || attr->exclude_user ||
480 attr->exclude_kernel || attr->exclude_hv;
481}
482
483static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100484__hw_perf_event_init(struct perf_event *event)
485{
Mark Rutland8a16b342011-04-28 16:27:54 +0100486 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100487 struct hw_perf_event *hwc = &event->hw;
488 int mapping, err;
489
Mark Rutlande1f431b2011-04-28 15:47:10 +0100490 mapping = armpmu->map_event(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100491
492 if (mapping < 0) {
493 pr_debug("event %x:%llx not supported\n", event->attr.type,
494 event->attr.config);
495 return mapping;
496 }
497
498 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100499 * We don't assign an index until we actually place the event onto
500 * hardware. Use -1 to signify that we haven't decided where to put it
501 * yet. For SMP systems, each core has it's own PMU so we can't do any
502 * clever allocation or constraints checking at this point.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100503 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100504 hwc->idx = -1;
505 hwc->config_base = 0;
506 hwc->config = 0;
507 hwc->event_base = 0;
508
509 /*
510 * Check whether we need to exclude the counter from certain modes.
511 */
512 if ((!armpmu->set_event_filter ||
513 armpmu->set_event_filter(hwc, &event->attr)) &&
514 event_requires_mode_exclusion(&event->attr)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100515 pr_debug("ARM performance counters do not support "
516 "mode exclusion\n");
517 return -EPERM;
518 }
519
520 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100521 * Store the event encoding into the config_base field.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100522 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100523 hwc->config_base |= (unsigned long)mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100524
525 if (!hwc->sample_period) {
Will Deacon57273472012-03-06 17:33:17 +0100526 /*
527 * For non-sampling runs, limit the sample_period to half
528 * of the counter width. That way, the new counter value
529 * is far less likely to overtake the previous one unless
530 * you have some serious IRQ latency issues.
531 */
532 hwc->sample_period = armpmu->max_period >> 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100533 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200534 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100535 }
536
537 err = 0;
538 if (event->group_leader != event) {
539 err = validate_group(event);
540 if (err)
541 return -EINVAL;
542 }
543
544 return err;
545}
546
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200547static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100548{
Mark Rutland8a16b342011-04-28 16:27:54 +0100549 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100550 int err = 0;
Mark Rutland03b78982011-04-27 11:20:11 +0100551 atomic_t *active_events = &armpmu->active_events;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100552
Stephane Eranian2481c5f2012-02-09 23:20:59 +0100553 /* does not support taken branch sampling */
554 if (has_branch_stack(event))
555 return -EOPNOTSUPP;
556
Mark Rutlande1f431b2011-04-28 15:47:10 +0100557 if (armpmu->map_event(event) == -ENOENT)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200558 return -ENOENT;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200559
Jamie Iles1b8873a2010-02-02 20:25:44 +0100560 event->destroy = hw_perf_event_destroy;
561
Mark Rutland03b78982011-04-27 11:20:11 +0100562 if (!atomic_inc_not_zero(active_events)) {
563 mutex_lock(&armpmu->reserve_mutex);
564 if (atomic_read(active_events) == 0)
Mark Rutland8a16b342011-04-28 16:27:54 +0100565 err = armpmu_reserve_hardware(armpmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100566
567 if (!err)
Mark Rutland03b78982011-04-27 11:20:11 +0100568 atomic_inc(active_events);
569 mutex_unlock(&armpmu->reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100570 }
571
572 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200573 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100574
575 err = __hw_perf_event_init(event);
576 if (err)
577 hw_perf_event_destroy(event);
578
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200579 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100580}
581
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200582static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100583{
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100584 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100585 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
Mark Rutland7325eae2011-08-23 11:59:49 +0100586 int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100587
Will Deaconf4f38432011-07-01 14:38:12 +0100588 if (enabled)
589 armpmu->start();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100590}
591
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200592static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100593{
Mark Rutland8a16b342011-04-28 16:27:54 +0100594 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Mark Rutland48957152011-04-27 10:31:51 +0100595 armpmu->stop();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100596}
597
Ashwin Chaugule4a81cb82012-06-07 13:40:54 -0400598static void armpmu_init(struct arm_pmu *armpmu)
Mark Rutland03b78982011-04-27 11:20:11 +0100599{
600 atomic_set(&armpmu->active_events, 0);
601 mutex_init(&armpmu->reserve_mutex);
Mark Rutland8a16b342011-04-28 16:27:54 +0100602
603 armpmu->pmu = (struct pmu) {
604 .pmu_enable = armpmu_enable,
605 .pmu_disable = armpmu_disable,
606 .event_init = armpmu_event_init,
607 .add = armpmu_add,
608 .del = armpmu_del,
609 .start = armpmu_start,
610 .stop = armpmu_stop,
611 .read = armpmu_read,
612 };
613}
614
Ashwin Chaugule4a81cb82012-06-07 13:40:54 -0400615int armpmu_register(struct arm_pmu *armpmu, char *name, int type)
Mark Rutland8a16b342011-04-28 16:27:54 +0100616{
617 armpmu_init(armpmu);
618 return perf_pmu_register(&armpmu->pmu, name, type);
Mark Rutland03b78982011-04-27 11:20:11 +0100619}
620
Will Deacon43eab872010-11-13 19:04:32 +0000621/* Include the PMU-specific implementations. */
622#include "perf_event_xscale.c"
623#include "perf_event_v6.c"
624#include "perf_event_v7.c"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625#include "perf_event_msm_krait.c"
Ashwin Chaugule7cd836e2012-06-11 16:26:47 -0400626#include "perf_event_msm.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
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100633cpu_pmu_reset(void)
Will Deacon574b69c2011-03-25 13:13:34 +0100634{
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100635 if (cpu_pmu && cpu_pmu->reset)
636 return on_each_cpu(cpu_pmu->reset, NULL, 1);
Will Deacon574b69c2011-03-25 13:13:34 +0100637 return 0;
638}
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100639arch_initcall(cpu_pmu_reset);
Will Deacon574b69c2011-03-25 13:13:34 +0100640
Will Deaconb0e89592011-07-26 22:10:28 +0100641/*
642 * PMU platform driver and devicetree bindings.
643 */
644static struct of_device_id armpmu_of_device_ids[] = {
645 {.compatible = "arm,cortex-a9-pmu"},
646 {.compatible = "arm,cortex-a8-pmu"},
647 {.compatible = "arm,arm1136-pmu"},
648 {.compatible = "arm,arm1176-pmu"},
649 {},
650};
651
652static struct platform_device_id armpmu_plat_device_ids[] = {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700653 {.name = "cpu-arm-pmu"},
Will Deaconb0e89592011-07-26 22:10:28 +0100654 {},
655};
656
657static int __devinit armpmu_device_probe(struct platform_device *pdev)
658{
Will Deacon6bd05402011-12-02 18:16:01 +0100659 if (!cpu_pmu)
660 return -ENODEV;
661
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100662 cpu_pmu->plat_device = pdev;
Will Deaconb0e89592011-07-26 22:10:28 +0100663 return 0;
664}
665
666static struct platform_driver armpmu_driver = {
667 .driver = {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700668 .name = "cpu-arm-pmu",
Will Deaconb0e89592011-07-26 22:10:28 +0100669 .of_match_table = armpmu_of_device_ids,
670 },
671 .probe = armpmu_device_probe,
672 .id_table = armpmu_plat_device_ids,
673};
674
675static int __init register_pmu_driver(void)
676{
677 return platform_driver_register(&armpmu_driver);
678}
679device_initcall(register_pmu_driver);
680
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100681static struct pmu_hw_events *armpmu_get_cpu_events(void)
Mark Rutland92f701e2011-05-04 09:23:51 +0100682{
683 return &__get_cpu_var(cpu_hw_events);
684}
685
686static void __init cpu_pmu_init(struct arm_pmu *armpmu)
687{
Mark Rutland0f78d2d2011-04-28 10:17:04 +0100688 int cpu;
689 for_each_possible_cpu(cpu) {
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100690 struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
Mark Rutland3fc2c832011-06-24 11:30:59 +0100691 events->events = per_cpu(hw_events, cpu);
692 events->used_mask = per_cpu(used_mask, cpu);
Mark Rutland0f78d2d2011-04-28 10:17:04 +0100693 raw_spin_lock_init(&events->pmu_lock);
694 }
Mark Rutland92f701e2011-05-04 09:23:51 +0100695 armpmu->get_hw_events = armpmu_get_cpu_events;
Mark Rutland7ae18a52011-06-06 10:37:50 +0100696 armpmu->type = ARM_PMU_DEVICE_CPU;
Mark Rutland92f701e2011-05-04 09:23:51 +0100697}
698
Will Deaconb0e89592011-07-26 22:10:28 +0100699/*
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100700 * PMU hardware loses all context when a CPU goes offline.
701 * When a CPU is hotplugged back in, since some hardware registers are
702 * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
703 * junk values out of them.
704 */
705static int __cpuinit pmu_cpu_notify(struct notifier_block *b,
706 unsigned long action, void *hcpu)
707{
708 if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
709 return NOTIFY_DONE;
710
711 if (cpu_pmu && cpu_pmu->reset)
712 cpu_pmu->reset(NULL);
713
714 return NOTIFY_OK;
715}
716
Ashwin Chaugulef53fe442012-06-07 13:41:37 -0400717static void armpmu_update_counters(void)
718{
719 struct pmu_hw_events *hw_events;
720 int idx;
721
722 if (!cpu_pmu)
723 return;
724
725 hw_events = cpu_pmu->get_hw_events();
726
727 for (idx = 0; idx <= cpu_pmu->num_events; ++idx) {
728 struct perf_event *event = hw_events->events[idx];
729
730 if (!event)
731 continue;
732
733 armpmu_read(event);
734 }
735}
736
737static int cpu_has_active_perf(void)
738{
739 struct pmu_hw_events *hw_events;
740 int enabled;
741
742 if (!cpu_pmu)
743 return 0;
744
745 hw_events = cpu_pmu->get_hw_events();
746 enabled = bitmap_weight(hw_events->used_mask, cpu_pmu->num_events);
747
748 if (enabled)
749 /*Even one event's existence is good enough.*/
750 return 1;
751
752 return 0;
753}
754
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100755static struct notifier_block __cpuinitdata pmu_cpu_notifier = {
756 .notifier_call = pmu_cpu_notify,
757};
758
Ashwin Chaugulef53fe442012-06-07 13:41:37 -0400759/*TODO: Unify with pending patch from ARM */
760static int perf_cpu_pm_notifier(struct notifier_block *self, unsigned long cmd,
761 void *v)
762{
763 switch (cmd) {
764 case CPU_PM_ENTER:
765 if (cpu_has_active_perf()) {
766 armpmu_update_counters();
767 perf_pmu_disable(&cpu_pmu->pmu);
768 }
769 break;
770
771 case CPU_PM_ENTER_FAILED:
772 case CPU_PM_EXIT:
773 if (cpu_has_active_perf() && cpu_pmu->reset) {
774 cpu_pmu->reset(NULL);
775 perf_pmu_enable(&cpu_pmu->pmu);
776 }
777 break;
778 }
779
780 return NOTIFY_OK;
781}
782
783static struct notifier_block perf_cpu_pm_notifier_block = {
784 .notifier_call = perf_cpu_pm_notifier,
785};
786
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100787/*
Will Deaconb0e89592011-07-26 22:10:28 +0100788 * CPU PMU identification and registration.
789 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100790static int __init
791init_hw_perf_events(void)
792{
793 unsigned long cpuid = read_cpuid_id();
794 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
795 unsigned long part_number = (cpuid & 0xFFF0);
796
Will Deacon49e6a322010-04-30 11:33:33 +0100797 /* ARM Ltd CPUs. */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100798 if (0x41 == implementor) {
799 switch (part_number) {
800 case 0xB360: /* ARM1136 */
801 case 0xB560: /* ARM1156 */
802 case 0xB760: /* ARM1176 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100803 cpu_pmu = armv6pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100804 break;
805 case 0xB020: /* ARM11mpcore */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100806 cpu_pmu = armv6mpcore_pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100807 break;
Jean PIHET796d1292010-01-26 18:51:05 +0100808 case 0xC080: /* Cortex-A8 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100809 cpu_pmu = armv7_a8_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100810 break;
811 case 0xC090: /* Cortex-A9 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100812 cpu_pmu = armv7_a9_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100813 break;
Will Deacon0c205cb2011-06-03 17:40:15 +0100814 case 0xC050: /* Cortex-A5 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100815 cpu_pmu = armv7_a5_pmu_init();
Will Deacon0c205cb2011-06-03 17:40:15 +0100816 break;
Will Deacon14abd032011-01-19 14:24:38 +0000817 case 0xC0F0: /* Cortex-A15 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100818 cpu_pmu = armv7_a15_pmu_init();
Will Deacon14abd032011-01-19 14:24:38 +0000819 break;
Will Deacond33c88c2012-02-03 14:46:01 +0100820 case 0xC070: /* Cortex-A7 */
821 cpu_pmu = armv7_a7_pmu_init();
822 break;
Will Deacon49e6a322010-04-30 11:33:33 +0100823 }
824 /* Intel CPUs [xscale]. */
825 } else if (0x69 == implementor) {
826 part_number = (cpuid >> 13) & 0x7;
827 switch (part_number) {
828 case 1:
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100829 cpu_pmu = xscale1pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100830 break;
831 case 2:
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100832 cpu_pmu = xscale2pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100833 break;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100834 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700835 /* Qualcomm CPUs */
836 } else if (0x51 == implementor) {
837 switch (part_number) {
838 case 0x00F0: /* 8x50 & 7x30*/
Ashwin Chaugule7cd836e2012-06-11 16:26:47 -0400839 cpu_pmu = armv7_scorpion_pmu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700840 break;
841 case 0x02D0: /* 8x60 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700842// fabricmon_pmu_init();
Ashwin Chaugule7cd836e2012-06-11 16:26:47 -0400843 cpu_pmu = armv7_scorpionmp_pmu_init();
Steve Mucklef132c6c2012-06-06 18:30:57 -0700844// scorpionmp_l2_pmu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845 break;
846 case 0x0490: /* 8960 sim */
847 case 0x04D0: /* 8960 */
Neil Leedered415112012-02-09 13:34:09 -0500848 case 0x06F0: /* 8064 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700849// fabricmon_pmu_init();
850 cpu_pmu = armv7_krait_pmu_init();
851// krait_l2_pmu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700852 break;
853 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100854 }
855
Steve Mucklef132c6c2012-06-06 18:30:57 -0700856
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100857 if (cpu_pmu) {
Jean PIHET796d1292010-01-26 18:51:05 +0100858 pr_info("enabled with %s PMU driver, %d counters available\n",
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100859 cpu_pmu->name, cpu_pmu->num_events);
860 cpu_pmu_init(cpu_pmu);
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100861 register_cpu_notifier(&pmu_cpu_notifier);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100862 armpmu_register(cpu_pmu, "cpu", PERF_TYPE_RAW);
Ashwin Chaugulef53fe442012-06-07 13:41:37 -0400863 cpu_pm_register_notifier(&perf_cpu_pm_notifier_block);
Will Deacon49e6a322010-04-30 11:33:33 +0100864 } else {
865 pr_info("no hardware support available\n");
Will Deacon49e6a322010-04-30 11:33:33 +0100866 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100867
868 return 0;
869}
Peter Zijlstra004417a2010-11-25 18:38:29 +0100870early_initcall(init_hw_perf_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100871
872/*
873 * Callchain handling code.
874 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100875
876/*
877 * The registers we're interested in are at the end of the variable
878 * length saved register structure. The fp points at the end of this
879 * structure so the address of this struct is:
880 * (struct frame_tail *)(xxx->fp)-1
881 *
882 * This code has been adapted from the ARM OProfile support.
883 */
884struct frame_tail {
Will Deacon4d6b7a72010-11-30 18:15:53 +0100885 struct frame_tail __user *fp;
886 unsigned long sp;
887 unsigned long lr;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100888} __attribute__((packed));
889
890/*
891 * Get the return address for a single stackframe and return a pointer to the
892 * next frame tail.
893 */
Will Deacon4d6b7a72010-11-30 18:15:53 +0100894static struct frame_tail __user *
895user_backtrace(struct frame_tail __user *tail,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100896 struct perf_callchain_entry *entry)
897{
898 struct frame_tail buftail;
899
900 /* Also check accessibility of one struct frame_tail beyond */
901 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
902 return NULL;
903 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
904 return NULL;
905
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200906 perf_callchain_store(entry, buftail.lr);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100907
908 /*
909 * Frame pointers should strictly progress back up the stack
910 * (towards higher addresses).
911 */
Rabin Vincentcb061992011-02-09 11:35:12 +0100912 if (tail + 1 >= buftail.fp)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100913 return NULL;
914
915 return buftail.fp - 1;
916}
917
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200918void
919perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100920{
Will Deacon4d6b7a72010-11-30 18:15:53 +0100921 struct frame_tail __user *tail;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100922
Jamie Iles1b8873a2010-02-02 20:25:44 +0100923
Will Deacon4d6b7a72010-11-30 18:15:53 +0100924 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100925
Sonny Rao860ad782011-04-18 22:12:59 +0100926 while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
927 tail && !((unsigned long)tail & 0x3))
Jamie Iles1b8873a2010-02-02 20:25:44 +0100928 tail = user_backtrace(tail, entry);
929}
930
931/*
932 * Gets called by walk_stackframe() for every stackframe. This will be called
933 * whist unwinding the stackframe and is like a subroutine return so we use
934 * the PC.
935 */
936static int
937callchain_trace(struct stackframe *fr,
938 void *data)
939{
940 struct perf_callchain_entry *entry = data;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200941 perf_callchain_store(entry, fr->pc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100942 return 0;
943}
944
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200945void
946perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100947{
948 struct stackframe fr;
949
Jamie Iles1b8873a2010-02-02 20:25:44 +0100950 fr.fp = regs->ARM_fp;
951 fr.sp = regs->ARM_sp;
952 fr.lr = regs->ARM_lr;
953 fr.pc = regs->ARM_pc;
954 walk_stackframe(&fr, callchain_trace, entry);
955}