blob: 778128b52f8c512c7362026881822ad64f80998b [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
Ashwin Chaugule66a8a862012-06-13 14:58:04 -0400276 /* Clear event constraints. */
277 if (armpmu->clear_event_constraints)
278 armpmu->clear_event_constraints(event);
279
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200280 perf_event_update_userpage(event);
281}
282
Jamie Iles1b8873a2010-02-02 20:25:44 +0100283static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200284armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100285{
Mark Rutland8a16b342011-04-28 16:27:54 +0100286 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100287 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100288 struct hw_perf_event *hwc = &event->hw;
289 int idx;
290 int err = 0;
291
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200292 perf_pmu_disable(event->pmu);
Ashwin Chaugule66a8a862012-06-13 14:58:04 -0400293 /*
294 * Tests if event is constrained. If not sets it so that next
295 * collision can be detected.
296 */
297 if (armpmu->test_set_event_constraints)
298 if (armpmu->test_set_event_constraints(event) < 0) {
299 pr_err("Event: %llx failed constraint check.\n",
300 event->attr.config);
301 event->state = PERF_EVENT_STATE_OFF;
302 goto out;
303 }
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200304
Jamie Iles1b8873a2010-02-02 20:25:44 +0100305 /* If we don't have a space for the counter then finish early. */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100306 idx = armpmu->get_event_idx(hw_events, hwc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100307 if (idx < 0) {
308 err = idx;
309 goto out;
310 }
311
312 /*
313 * If there is an event in the counter we are going to use then make
314 * sure it is disabled.
315 */
316 event->hw.idx = idx;
317 armpmu->disable(hwc, idx);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100318 hw_events->events[idx] = event;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100319
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200320 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
321 if (flags & PERF_EF_START)
322 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100323
324 /* Propagate our changes to the userspace mapping. */
325 perf_event_update_userpage(event);
326
327out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200328 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100329 return err;
330}
331
Jamie Iles1b8873a2010-02-02 20:25:44 +0100332static int
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100333validate_event(struct pmu_hw_events *hw_events,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100334 struct perf_event *event)
335{
Mark Rutland8a16b342011-04-28 16:27:54 +0100336 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100337 struct hw_perf_event fake_event = event->hw;
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100338 struct pmu *leader_pmu = event->group_leader->pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100339
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100340 if (event->pmu != leader_pmu || event->state <= PERF_EVENT_STATE_OFF)
Will Deacon65b47112010-09-02 09:32:08 +0100341 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100342
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100343 return armpmu->get_event_idx(hw_events, &fake_event) >= 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100344}
345
346static int
347validate_group(struct perf_event *event)
348{
349 struct perf_event *sibling, *leader = event->group_leader;
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100350 struct pmu_hw_events fake_pmu;
Will Deaconbce34d12011-11-17 15:05:14 +0000351 DECLARE_BITMAP(fake_used_mask, ARMPMU_MAX_HWEVENTS);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100352
Will Deaconbce34d12011-11-17 15:05:14 +0000353 /*
354 * Initialise the fake PMU. We only need to populate the
355 * used_mask for the purposes of validation.
356 */
357 memset(fake_used_mask, 0, sizeof(fake_used_mask));
358 fake_pmu.used_mask = fake_used_mask;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100359
360 if (!validate_event(&fake_pmu, leader))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100361 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100362
363 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
364 if (!validate_event(&fake_pmu, sibling))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100365 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100366 }
367
368 if (!validate_event(&fake_pmu, event))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100369 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100370
371 return 0;
372}
373
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530374static irqreturn_t armpmu_platform_irq(int irq, void *dev)
375{
Mark Rutland8a16b342011-04-28 16:27:54 +0100376 struct arm_pmu *armpmu = (struct arm_pmu *) dev;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100377 struct platform_device *plat_device = armpmu->plat_device;
378 struct arm_pmu_platdata *plat = dev_get_platdata(&plat_device->dev);
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530379
380 return plat->handle_irq(irq, dev, armpmu->handle_irq);
381}
382
Steve Mucklef132c6c2012-06-06 18:30:57 -0700383int
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500384armpmu_generic_request_irq(int irq, irq_handler_t *handle_irq)
385{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700386 return request_irq(irq, *handle_irq,
387 IRQF_DISABLED | IRQF_NOBALANCING,
388 "armpmu", NULL);
389}
390
391void
392armpmu_generic_free_irq(int irq)
393{
394 if (irq >= 0)
395 free_irq(irq, NULL);
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500396}
397
Will Deacon0b390e22011-07-27 15:18:59 +0100398static void
Mark Rutland8a16b342011-04-28 16:27:54 +0100399armpmu_release_hardware(struct arm_pmu *armpmu)
Will Deacon0b390e22011-07-27 15:18:59 +0100400{
401 int i, irq, irqs;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100402 struct platform_device *pmu_device = armpmu->plat_device;
Ashwin Chaugulef53fe442012-06-07 13:41:37 -0400403
Will Deacon0b390e22011-07-27 15:18:59 +0100404 irqs = min(pmu_device->num_resources, num_possible_cpus());
405
406 for (i = 0; i < irqs; ++i) {
407 if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs))
408 continue;
409 irq = platform_get_irq(pmu_device, i);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700410 armpmu->free_pmu_irq(irq);
Will Deacon0b390e22011-07-27 15:18:59 +0100411 }
412
Mark Rutland7ae18a52011-06-06 10:37:50 +0100413 release_pmu(armpmu->type);
Will Deacon0b390e22011-07-27 15:18:59 +0100414}
415
Jamie Iles1b8873a2010-02-02 20:25:44 +0100416static int
Mark Rutland8a16b342011-04-28 16:27:54 +0100417armpmu_reserve_hardware(struct arm_pmu *armpmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100418{
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530419 struct arm_pmu_platdata *plat;
420 irq_handler_t handle_irq;
Will Deaconb0e89592011-07-26 22:10:28 +0100421 int i, err, irq, irqs;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100422 struct platform_device *pmu_device = armpmu->plat_device;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100423
Will Deacone5a21322011-11-22 18:01:46 +0000424 if (!pmu_device)
425 return -ENODEV;
426
Mark Rutland7ae18a52011-06-06 10:37:50 +0100427 err = reserve_pmu(armpmu->type);
Will Deaconb0e89592011-07-26 22:10:28 +0100428 if (err) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100429 pr_warning("unable to reserve pmu\n");
Will Deaconb0e89592011-07-26 22:10:28 +0100430 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100431 }
432
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530433 plat = dev_get_platdata(&pmu_device->dev);
434 if (plat && plat->handle_irq)
435 handle_irq = armpmu_platform_irq;
436 else
437 handle_irq = armpmu->handle_irq;
438
Will Deacon0b390e22011-07-27 15:18:59 +0100439 irqs = min(pmu_device->num_resources, num_possible_cpus());
Will Deaconb0e89592011-07-26 22:10:28 +0100440 if (irqs < 1) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100441 pr_err("no irqs for PMUs defined\n");
442 return -ENODEV;
443 }
444
Will Deaconb0e89592011-07-26 22:10:28 +0100445 for (i = 0; i < irqs; ++i) {
Will Deacon0b390e22011-07-27 15:18:59 +0100446 err = 0;
Will Deacon49c006b2010-04-29 17:13:24 +0100447 irq = platform_get_irq(pmu_device, i);
448 if (irq < 0)
449 continue;
450
Will Deaconb0e89592011-07-26 22:10:28 +0100451 /*
452 * If we have a single PMU interrupt that we can't shift,
453 * assume that we're running on a uniprocessor machine and
Will Deacon0b390e22011-07-27 15:18:59 +0100454 * continue. Otherwise, continue without this interrupt.
Will Deaconb0e89592011-07-26 22:10:28 +0100455 */
Will Deacon0b390e22011-07-27 15:18:59 +0100456 if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
457 pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
458 irq, i);
459 continue;
Will Deaconb0e89592011-07-26 22:10:28 +0100460 }
461
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500462 err = armpmu->request_pmu_irq(irq, &handle_irq);
463
Steve Mucklef132c6c2012-06-06 18:30:57 -0700464 if (err) {
465 pr_warning("unable to request IRQ%d for %s perf "
466 "counters\n", irq, armpmu->name);
467
468 armpmu_release_hardware(cpu_pmu);
469 return err;
470 }
471
Will Deacon0b390e22011-07-27 15:18:59 +0100472 cpumask_set_cpu(i, &armpmu->active_irqs);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100473 }
474
Will Deacon0b390e22011-07-27 15:18:59 +0100475 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100476}
477
Jamie Iles1b8873a2010-02-02 20:25:44 +0100478static void
479hw_perf_event_destroy(struct perf_event *event)
480{
Mark Rutland8a16b342011-04-28 16:27:54 +0100481 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland03b78982011-04-27 11:20:11 +0100482 atomic_t *active_events = &armpmu->active_events;
483 struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
484
485 if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
Mark Rutland8a16b342011-04-28 16:27:54 +0100486 armpmu_release_hardware(armpmu);
Mark Rutland03b78982011-04-27 11:20:11 +0100487 mutex_unlock(pmu_reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100488 }
489}
490
491static int
Will Deacon05d22fd2011-07-19 11:57:30 +0100492event_requires_mode_exclusion(struct perf_event_attr *attr)
493{
494 return attr->exclude_idle || attr->exclude_user ||
495 attr->exclude_kernel || attr->exclude_hv;
496}
497
498static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100499__hw_perf_event_init(struct perf_event *event)
500{
Mark Rutland8a16b342011-04-28 16:27:54 +0100501 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100502 struct hw_perf_event *hwc = &event->hw;
503 int mapping, err;
504
Mark Rutlande1f431b2011-04-28 15:47:10 +0100505 mapping = armpmu->map_event(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100506
507 if (mapping < 0) {
508 pr_debug("event %x:%llx not supported\n", event->attr.type,
509 event->attr.config);
510 return mapping;
511 }
512
513 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100514 * We don't assign an index until we actually place the event onto
515 * hardware. Use -1 to signify that we haven't decided where to put it
516 * yet. For SMP systems, each core has it's own PMU so we can't do any
517 * clever allocation or constraints checking at this point.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100518 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100519 hwc->idx = -1;
520 hwc->config_base = 0;
521 hwc->config = 0;
522 hwc->event_base = 0;
523
524 /*
525 * Check whether we need to exclude the counter from certain modes.
526 */
527 if ((!armpmu->set_event_filter ||
528 armpmu->set_event_filter(hwc, &event->attr)) &&
529 event_requires_mode_exclusion(&event->attr)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100530 pr_debug("ARM performance counters do not support "
531 "mode exclusion\n");
532 return -EPERM;
533 }
534
Ashwin Chaugule66a8a862012-06-13 14:58:04 -0400535
Jamie Iles1b8873a2010-02-02 20:25:44 +0100536 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100537 * Store the event encoding into the config_base field.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100538 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100539 hwc->config_base |= (unsigned long)mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100540
541 if (!hwc->sample_period) {
Will Deacon57273472012-03-06 17:33:17 +0100542 /*
543 * For non-sampling runs, limit the sample_period to half
544 * of the counter width. That way, the new counter value
545 * is far less likely to overtake the previous one unless
546 * you have some serious IRQ latency issues.
547 */
548 hwc->sample_period = armpmu->max_period >> 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100549 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200550 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100551 }
552
553 err = 0;
554 if (event->group_leader != event) {
555 err = validate_group(event);
556 if (err)
557 return -EINVAL;
558 }
559
560 return err;
561}
562
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200563static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100564{
Mark Rutland8a16b342011-04-28 16:27:54 +0100565 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100566 int err = 0;
Mark Rutland03b78982011-04-27 11:20:11 +0100567 atomic_t *active_events = &armpmu->active_events;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100568
Stephane Eranian2481c5f2012-02-09 23:20:59 +0100569 /* does not support taken branch sampling */
570 if (has_branch_stack(event))
571 return -EOPNOTSUPP;
572
Mark Rutlande1f431b2011-04-28 15:47:10 +0100573 if (armpmu->map_event(event) == -ENOENT)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200574 return -ENOENT;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200575
Jamie Iles1b8873a2010-02-02 20:25:44 +0100576 event->destroy = hw_perf_event_destroy;
577
Mark Rutland03b78982011-04-27 11:20:11 +0100578 if (!atomic_inc_not_zero(active_events)) {
579 mutex_lock(&armpmu->reserve_mutex);
580 if (atomic_read(active_events) == 0)
Mark Rutland8a16b342011-04-28 16:27:54 +0100581 err = armpmu_reserve_hardware(armpmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100582
583 if (!err)
Mark Rutland03b78982011-04-27 11:20:11 +0100584 atomic_inc(active_events);
585 mutex_unlock(&armpmu->reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100586 }
587
588 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200589 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100590
591 err = __hw_perf_event_init(event);
592 if (err)
593 hw_perf_event_destroy(event);
594
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200595 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100596}
597
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200598static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100599{
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100600 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100601 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
Mark Rutland7325eae2011-08-23 11:59:49 +0100602 int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100603
Will Deaconf4f38432011-07-01 14:38:12 +0100604 if (enabled)
605 armpmu->start();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100606}
607
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200608static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100609{
Mark Rutland8a16b342011-04-28 16:27:54 +0100610 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Mark Rutland48957152011-04-27 10:31:51 +0100611 armpmu->stop();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100612}
613
Ashwin Chaugule4a81cb82012-06-07 13:40:54 -0400614static void armpmu_init(struct arm_pmu *armpmu)
Mark Rutland03b78982011-04-27 11:20:11 +0100615{
616 atomic_set(&armpmu->active_events, 0);
617 mutex_init(&armpmu->reserve_mutex);
Mark Rutland8a16b342011-04-28 16:27:54 +0100618
Ashwin Chaugule795bf7b2012-06-20 16:23:08 -0400619 armpmu->pmu.pmu_enable = armpmu_enable;
620 armpmu->pmu.pmu_disable = armpmu_disable;
621 armpmu->pmu.event_init = armpmu_event_init;
622 armpmu->pmu.add = armpmu_add;
623 armpmu->pmu.del = armpmu_del;
624 armpmu->pmu.start = armpmu_start;
625 armpmu->pmu.stop = armpmu_stop;
626 armpmu->pmu.read = armpmu_read;
Mark Rutland8a16b342011-04-28 16:27:54 +0100627}
628
Ashwin Chaugule4a81cb82012-06-07 13:40:54 -0400629int armpmu_register(struct arm_pmu *armpmu, char *name, int type)
Mark Rutland8a16b342011-04-28 16:27:54 +0100630{
631 armpmu_init(armpmu);
632 return perf_pmu_register(&armpmu->pmu, name, type);
Mark Rutland03b78982011-04-27 11:20:11 +0100633}
634
Will Deacon43eab872010-11-13 19:04:32 +0000635/* Include the PMU-specific implementations. */
636#include "perf_event_xscale.c"
637#include "perf_event_v6.c"
638#include "perf_event_v7.c"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639#include "perf_event_msm_krait.c"
Ashwin Chaugule7cd836e2012-06-11 16:26:47 -0400640#include "perf_event_msm.c"
Will Deacon49e6a322010-04-30 11:33:33 +0100641
Will Deacon574b69c2011-03-25 13:13:34 +0100642/*
643 * Ensure the PMU has sane values out of reset.
644 * This requires SMP to be available, so exists as a separate initcall.
645 */
646static int __init
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100647cpu_pmu_reset(void)
Will Deacon574b69c2011-03-25 13:13:34 +0100648{
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100649 if (cpu_pmu && cpu_pmu->reset)
650 return on_each_cpu(cpu_pmu->reset, NULL, 1);
Will Deacon574b69c2011-03-25 13:13:34 +0100651 return 0;
652}
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100653arch_initcall(cpu_pmu_reset);
Will Deacon574b69c2011-03-25 13:13:34 +0100654
Will Deaconb0e89592011-07-26 22:10:28 +0100655/*
656 * PMU platform driver and devicetree bindings.
657 */
658static struct of_device_id armpmu_of_device_ids[] = {
659 {.compatible = "arm,cortex-a9-pmu"},
660 {.compatible = "arm,cortex-a8-pmu"},
661 {.compatible = "arm,arm1136-pmu"},
662 {.compatible = "arm,arm1176-pmu"},
663 {},
664};
665
666static struct platform_device_id armpmu_plat_device_ids[] = {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700667 {.name = "cpu-arm-pmu"},
Will Deaconb0e89592011-07-26 22:10:28 +0100668 {},
669};
670
671static int __devinit armpmu_device_probe(struct platform_device *pdev)
672{
Will Deacon6bd05402011-12-02 18:16:01 +0100673 if (!cpu_pmu)
674 return -ENODEV;
675
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100676 cpu_pmu->plat_device = pdev;
Will Deaconb0e89592011-07-26 22:10:28 +0100677 return 0;
678}
679
680static struct platform_driver armpmu_driver = {
681 .driver = {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700682 .name = "cpu-arm-pmu",
Will Deaconb0e89592011-07-26 22:10:28 +0100683 .of_match_table = armpmu_of_device_ids,
684 },
685 .probe = armpmu_device_probe,
686 .id_table = armpmu_plat_device_ids,
687};
688
689static int __init register_pmu_driver(void)
690{
691 return platform_driver_register(&armpmu_driver);
692}
693device_initcall(register_pmu_driver);
694
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100695static struct pmu_hw_events *armpmu_get_cpu_events(void)
Mark Rutland92f701e2011-05-04 09:23:51 +0100696{
697 return &__get_cpu_var(cpu_hw_events);
698}
699
700static void __init cpu_pmu_init(struct arm_pmu *armpmu)
701{
Mark Rutland0f78d2d2011-04-28 10:17:04 +0100702 int cpu;
703 for_each_possible_cpu(cpu) {
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100704 struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
Mark Rutland3fc2c832011-06-24 11:30:59 +0100705 events->events = per_cpu(hw_events, cpu);
706 events->used_mask = per_cpu(used_mask, cpu);
Mark Rutland0f78d2d2011-04-28 10:17:04 +0100707 raw_spin_lock_init(&events->pmu_lock);
708 }
Mark Rutland92f701e2011-05-04 09:23:51 +0100709 armpmu->get_hw_events = armpmu_get_cpu_events;
Mark Rutland7ae18a52011-06-06 10:37:50 +0100710 armpmu->type = ARM_PMU_DEVICE_CPU;
Mark Rutland92f701e2011-05-04 09:23:51 +0100711}
712
Will Deaconb0e89592011-07-26 22:10:28 +0100713/*
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100714 * PMU hardware loses all context when a CPU goes offline.
715 * When a CPU is hotplugged back in, since some hardware registers are
716 * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
717 * junk values out of them.
718 */
719static int __cpuinit pmu_cpu_notify(struct notifier_block *b,
720 unsigned long action, void *hcpu)
721{
722 if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
723 return NOTIFY_DONE;
724
725 if (cpu_pmu && cpu_pmu->reset)
726 cpu_pmu->reset(NULL);
727
728 return NOTIFY_OK;
729}
730
Ashwin Chaugulef53fe442012-06-07 13:41:37 -0400731static void armpmu_update_counters(void)
732{
733 struct pmu_hw_events *hw_events;
734 int idx;
735
736 if (!cpu_pmu)
737 return;
738
739 hw_events = cpu_pmu->get_hw_events();
740
741 for (idx = 0; idx <= cpu_pmu->num_events; ++idx) {
742 struct perf_event *event = hw_events->events[idx];
743
744 if (!event)
745 continue;
746
747 armpmu_read(event);
748 }
749}
750
751static int cpu_has_active_perf(void)
752{
753 struct pmu_hw_events *hw_events;
754 int enabled;
755
756 if (!cpu_pmu)
757 return 0;
758
759 hw_events = cpu_pmu->get_hw_events();
760 enabled = bitmap_weight(hw_events->used_mask, cpu_pmu->num_events);
761
762 if (enabled)
763 /*Even one event's existence is good enough.*/
764 return 1;
765
766 return 0;
767}
768
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100769static struct notifier_block __cpuinitdata pmu_cpu_notifier = {
770 .notifier_call = pmu_cpu_notify,
771};
772
Ashwin Chaugulef53fe442012-06-07 13:41:37 -0400773/*TODO: Unify with pending patch from ARM */
774static int perf_cpu_pm_notifier(struct notifier_block *self, unsigned long cmd,
775 void *v)
776{
777 switch (cmd) {
778 case CPU_PM_ENTER:
779 if (cpu_has_active_perf()) {
780 armpmu_update_counters();
781 perf_pmu_disable(&cpu_pmu->pmu);
782 }
783 break;
784
785 case CPU_PM_ENTER_FAILED:
786 case CPU_PM_EXIT:
787 if (cpu_has_active_perf() && cpu_pmu->reset) {
788 cpu_pmu->reset(NULL);
789 perf_pmu_enable(&cpu_pmu->pmu);
790 }
791 break;
792 }
793
794 return NOTIFY_OK;
795}
796
797static struct notifier_block perf_cpu_pm_notifier_block = {
798 .notifier_call = perf_cpu_pm_notifier,
799};
800
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100801/*
Will Deaconb0e89592011-07-26 22:10:28 +0100802 * CPU PMU identification and registration.
803 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100804static int __init
805init_hw_perf_events(void)
806{
807 unsigned long cpuid = read_cpuid_id();
808 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
809 unsigned long part_number = (cpuid & 0xFFF0);
810
Will Deacon49e6a322010-04-30 11:33:33 +0100811 /* ARM Ltd CPUs. */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100812 if (0x41 == implementor) {
813 switch (part_number) {
814 case 0xB360: /* ARM1136 */
815 case 0xB560: /* ARM1156 */
816 case 0xB760: /* ARM1176 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100817 cpu_pmu = armv6pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100818 break;
819 case 0xB020: /* ARM11mpcore */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100820 cpu_pmu = armv6mpcore_pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100821 break;
Jean PIHET796d1292010-01-26 18:51:05 +0100822 case 0xC080: /* Cortex-A8 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100823 cpu_pmu = armv7_a8_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100824 break;
825 case 0xC090: /* Cortex-A9 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100826 cpu_pmu = armv7_a9_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100827 break;
Will Deacon0c205cb2011-06-03 17:40:15 +0100828 case 0xC050: /* Cortex-A5 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100829 cpu_pmu = armv7_a5_pmu_init();
Will Deacon0c205cb2011-06-03 17:40:15 +0100830 break;
Will Deacon14abd032011-01-19 14:24:38 +0000831 case 0xC0F0: /* Cortex-A15 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100832 cpu_pmu = armv7_a15_pmu_init();
Will Deacon14abd032011-01-19 14:24:38 +0000833 break;
Will Deacond33c88c2012-02-03 14:46:01 +0100834 case 0xC070: /* Cortex-A7 */
835 cpu_pmu = armv7_a7_pmu_init();
836 break;
Will Deacon49e6a322010-04-30 11:33:33 +0100837 }
838 /* Intel CPUs [xscale]. */
839 } else if (0x69 == implementor) {
840 part_number = (cpuid >> 13) & 0x7;
841 switch (part_number) {
842 case 1:
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100843 cpu_pmu = xscale1pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100844 break;
845 case 2:
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100846 cpu_pmu = xscale2pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100847 break;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100848 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849 /* Qualcomm CPUs */
850 } else if (0x51 == implementor) {
851 switch (part_number) {
852 case 0x00F0: /* 8x50 & 7x30*/
Ashwin Chaugule7cd836e2012-06-11 16:26:47 -0400853 cpu_pmu = armv7_scorpion_pmu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 break;
855 case 0x02D0: /* 8x60 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700856// fabricmon_pmu_init();
Ashwin Chaugule7cd836e2012-06-11 16:26:47 -0400857 cpu_pmu = armv7_scorpionmp_pmu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858 break;
859 case 0x0490: /* 8960 sim */
860 case 0x04D0: /* 8960 */
Neil Leedered415112012-02-09 13:34:09 -0500861 case 0x06F0: /* 8064 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700862// fabricmon_pmu_init();
863 cpu_pmu = armv7_krait_pmu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700864 break;
865 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100866 }
867
Steve Mucklef132c6c2012-06-06 18:30:57 -0700868
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100869 if (cpu_pmu) {
Jean PIHET796d1292010-01-26 18:51:05 +0100870 pr_info("enabled with %s PMU driver, %d counters available\n",
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100871 cpu_pmu->name, cpu_pmu->num_events);
872 cpu_pmu_init(cpu_pmu);
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100873 register_cpu_notifier(&pmu_cpu_notifier);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100874 armpmu_register(cpu_pmu, "cpu", PERF_TYPE_RAW);
Ashwin Chaugulef53fe442012-06-07 13:41:37 -0400875 cpu_pm_register_notifier(&perf_cpu_pm_notifier_block);
Will Deacon49e6a322010-04-30 11:33:33 +0100876 } else {
877 pr_info("no hardware support available\n");
Will Deacon49e6a322010-04-30 11:33:33 +0100878 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100879
880 return 0;
881}
Peter Zijlstra004417a2010-11-25 18:38:29 +0100882early_initcall(init_hw_perf_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100883
884/*
885 * Callchain handling code.
886 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100887
888/*
889 * The registers we're interested in are at the end of the variable
890 * length saved register structure. The fp points at the end of this
891 * structure so the address of this struct is:
892 * (struct frame_tail *)(xxx->fp)-1
893 *
894 * This code has been adapted from the ARM OProfile support.
895 */
896struct frame_tail {
Will Deacon4d6b7a72010-11-30 18:15:53 +0100897 struct frame_tail __user *fp;
898 unsigned long sp;
899 unsigned long lr;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100900} __attribute__((packed));
901
902/*
903 * Get the return address for a single stackframe and return a pointer to the
904 * next frame tail.
905 */
Will Deacon4d6b7a72010-11-30 18:15:53 +0100906static struct frame_tail __user *
907user_backtrace(struct frame_tail __user *tail,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100908 struct perf_callchain_entry *entry)
909{
910 struct frame_tail buftail;
911
912 /* Also check accessibility of one struct frame_tail beyond */
913 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
914 return NULL;
915 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
916 return NULL;
917
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200918 perf_callchain_store(entry, buftail.lr);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100919
920 /*
921 * Frame pointers should strictly progress back up the stack
922 * (towards higher addresses).
923 */
Rabin Vincentcb061992011-02-09 11:35:12 +0100924 if (tail + 1 >= buftail.fp)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100925 return NULL;
926
927 return buftail.fp - 1;
928}
929
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200930void
931perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100932{
Will Deacon4d6b7a72010-11-30 18:15:53 +0100933 struct frame_tail __user *tail;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100934
Jamie Iles1b8873a2010-02-02 20:25:44 +0100935
Will Deacon4d6b7a72010-11-30 18:15:53 +0100936 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100937
Sonny Rao860ad782011-04-18 22:12:59 +0100938 while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
939 tail && !((unsigned long)tail & 0x3))
Jamie Iles1b8873a2010-02-02 20:25:44 +0100940 tail = user_backtrace(tail, entry);
941}
942
943/*
944 * Gets called by walk_stackframe() for every stackframe. This will be called
945 * whist unwinding the stackframe and is like a subroutine return so we use
946 * the PC.
947 */
948static int
949callchain_trace(struct stackframe *fr,
950 void *data)
951{
952 struct perf_callchain_entry *entry = data;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200953 perf_callchain_store(entry, fr->pc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100954 return 0;
955}
956
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200957void
958perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100959{
960 struct stackframe fr;
961
Jamie Iles1b8873a2010-02-02 20:25:44 +0100962 fr.fp = regs->ARM_fp;
963 fr.sp = regs->ARM_sp;
964 fr.lr = regs->ARM_lr;
965 fr.pc = regs->ARM_pc;
966 walk_stackframe(&fr, callchain_trace, entry);
967}