blob: af6e7e6e0ecd0a70ed4ea08ac20d945dcbe46bb5 [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
Jamie Iles1b8873a2010-02-02 20:25:44 +010031/*
Will Deaconecf5a892011-07-19 22:43:28 +010032 * ARMv6 supports a maximum of 3 events, starting from index 0. If we add
Jamie Iles1b8873a2010-02-02 20:25:44 +010033 * another platform that supports more, we need to increase this to be the
34 * largest of all platforms.
Jean PIHET796d1292010-01-26 18:51:05 +010035 *
36 * ARMv7 supports up to 32 events:
37 * cycle counter CCNT + 31 events counters CNT0..30.
38 * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
Jamie Iles1b8873a2010-02-02 20:25:44 +010039 */
Will Deaconecf5a892011-07-19 22:43:28 +010040#define ARMPMU_MAX_HWEVENTS 32
Jamie Iles1b8873a2010-02-02 20:25:44 +010041
Mark Rutland3fc2c832011-06-24 11:30:59 +010042static DEFINE_PER_CPU(struct perf_event * [ARMPMU_MAX_HWEVENTS], hw_events);
43static DEFINE_PER_CPU(unsigned long [BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)], used_mask);
Mark Rutland8be3f9a2011-05-17 11:20:11 +010044static DEFINE_PER_CPU(struct pmu_hw_events, cpu_hw_events);
Will Deacon181193f2010-04-30 11:32:44 +010045
Mark Rutland8a16b342011-04-28 16:27:54 +010046#define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu))
47
Jamie Iles1b8873a2010-02-02 20:25:44 +010048/* Set at runtime when we know what CPU type we are. */
Mark Rutland8be3f9a2011-05-17 11:20:11 +010049static struct arm_pmu *cpu_pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +010050
Will Deacon181193f2010-04-30 11:32:44 +010051enum arm_perf_pmu_ids
52armpmu_get_pmu_id(void)
53{
54 int id = -ENODEV;
55
Mark Rutland8be3f9a2011-05-17 11:20:11 +010056 if (cpu_pmu != NULL)
57 id = cpu_pmu->id;
Will Deacon181193f2010-04-30 11:32:44 +010058
59 return id;
60}
61EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
62
Will Deaconfeb45d02011-11-14 10:33:05 +000063int perf_num_counters(void)
Will Deacon929f5192010-04-30 11:34:26 +010064{
65 int max_events = 0;
66
Mark Rutland8be3f9a2011-05-17 11:20:11 +010067 if (cpu_pmu != NULL)
68 max_events = cpu_pmu->num_events;
Will Deacon929f5192010-04-30 11:34:26 +010069
70 return max_events;
71}
Matt Fleming3bf101b2010-09-27 20:22:24 +010072EXPORT_SYMBOL_GPL(perf_num_counters);
73
Jamie Iles1b8873a2010-02-02 20:25:44 +010074#define HW_OP_UNSUPPORTED 0xFFFF
75
76#define C(_x) \
77 PERF_COUNT_HW_CACHE_##_x
78
79#define CACHE_OP_UNSUPPORTED 0xFFFF
80
Jamie Iles1b8873a2010-02-02 20:25:44 +010081static int
Steve Mucklef132c6c2012-06-06 18:30:57 -070082armpmu_map_cache_event(unsigned (*cache_map)
Mark Rutlande1f431b2011-04-28 15:47:10 +010083 [PERF_COUNT_HW_CACHE_MAX]
84 [PERF_COUNT_HW_CACHE_OP_MAX]
85 [PERF_COUNT_HW_CACHE_RESULT_MAX],
86 u64 config)
Jamie Iles1b8873a2010-02-02 20:25:44 +010087{
88 unsigned int cache_type, cache_op, cache_result, ret;
89
90 cache_type = (config >> 0) & 0xff;
91 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
92 return -EINVAL;
93
94 cache_op = (config >> 8) & 0xff;
95 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
96 return -EINVAL;
97
98 cache_result = (config >> 16) & 0xff;
99 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
100 return -EINVAL;
101
Mark Rutlande1f431b2011-04-28 15:47:10 +0100102 ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100103
104 if (ret == CACHE_OP_UNSUPPORTED)
105 return -ENOENT;
106
107 return ret;
108}
109
110static int
Mark Rutlande1f431b2011-04-28 15:47:10 +0100111armpmu_map_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
Will Deacon84fee972010-11-13 17:13:56 +0000112{
Mark Rutlande1f431b2011-04-28 15:47:10 +0100113 int mapping = (*event_map)[config];
114 return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
Will Deacon84fee972010-11-13 17:13:56 +0000115}
116
117static int
Mark Rutlande1f431b2011-04-28 15:47:10 +0100118armpmu_map_raw_event(u32 raw_event_mask, u64 config)
Will Deacon84fee972010-11-13 17:13:56 +0000119{
Mark Rutlande1f431b2011-04-28 15:47:10 +0100120 return (int)(config & raw_event_mask);
121}
122
123static int map_cpu_event(struct perf_event *event,
124 const unsigned (*event_map)[PERF_COUNT_HW_MAX],
Steve Mucklef132c6c2012-06-06 18:30:57 -0700125 unsigned (*cache_map)
Mark Rutlande1f431b2011-04-28 15:47:10 +0100126 [PERF_COUNT_HW_CACHE_MAX]
127 [PERF_COUNT_HW_CACHE_OP_MAX]
128 [PERF_COUNT_HW_CACHE_RESULT_MAX],
129 u32 raw_event_mask)
130{
131 u64 config = event->attr.config;
132
133 switch (event->attr.type) {
134 case PERF_TYPE_HARDWARE:
135 return armpmu_map_event(event_map, config);
136 case PERF_TYPE_HW_CACHE:
137 return armpmu_map_cache_event(cache_map, config);
138 case PERF_TYPE_RAW:
139 return armpmu_map_raw_event(raw_event_mask, config);
140 }
141
142 return -ENOENT;
Will Deacon84fee972010-11-13 17:13:56 +0000143}
144
Mark Rutland0ce47082011-05-19 10:07:57 +0100145int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100146armpmu_event_set_period(struct perf_event *event,
147 struct hw_perf_event *hwc,
148 int idx)
149{
Mark Rutland8a16b342011-04-28 16:27:54 +0100150 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Peter Zijlstrae7850592010-05-21 14:43:08 +0200151 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100152 s64 period = hwc->sample_period;
153 int ret = 0;
154
155 if (unlikely(left <= -period)) {
156 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200157 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100158 hwc->last_period = period;
159 ret = 1;
160 }
161
162 if (unlikely(left <= 0)) {
163 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200164 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100165 hwc->last_period = period;
166 ret = 1;
167 }
168
169 if (left > (s64)armpmu->max_period)
170 left = armpmu->max_period;
171
Peter Zijlstrae7850592010-05-21 14:43:08 +0200172 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100173
174 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
175
176 perf_event_update_userpage(event);
177
178 return ret;
179}
180
Mark Rutland0ce47082011-05-19 10:07:57 +0100181u64
Jamie Iles1b8873a2010-02-02 20:25:44 +0100182armpmu_event_update(struct perf_event *event,
183 struct hw_perf_event *hwc,
Will Deacon57273472012-03-06 17:33:17 +0100184 int idx)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100185{
Mark Rutland8a16b342011-04-28 16:27:54 +0100186 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Will Deacona7378232011-03-25 17:12:37 +0100187 u64 delta, prev_raw_count, new_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100188
189again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200190 prev_raw_count = local64_read(&hwc->prev_count);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100191 new_raw_count = armpmu->read_counter(idx);
192
Peter Zijlstrae7850592010-05-21 14:43:08 +0200193 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100194 new_raw_count) != prev_raw_count)
195 goto again;
196
Will Deacon57273472012-03-06 17:33:17 +0100197 delta = (new_raw_count - prev_raw_count) & armpmu->max_period;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100198
Peter Zijlstrae7850592010-05-21 14:43:08 +0200199 local64_add(delta, &event->count);
200 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100201
202 return new_raw_count;
203}
204
205static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100206armpmu_read(struct perf_event *event)
207{
208 struct hw_perf_event *hwc = &event->hw;
209
210 /* Don't read disabled counters! */
211 if (hwc->idx < 0)
212 return;
213
Will Deacon57273472012-03-06 17:33:17 +0100214 armpmu_event_update(event, hwc, hwc->idx);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100215}
216
217static void
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200218armpmu_stop(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100219{
Mark Rutland8a16b342011-04-28 16:27:54 +0100220 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100221 struct hw_perf_event *hwc = &event->hw;
222
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200223 /*
224 * ARM pmu always has to update the counter, so ignore
225 * PERF_EF_UPDATE, see comments in armpmu_start().
226 */
227 if (!(hwc->state & PERF_HES_STOPPED)) {
228 armpmu->disable(hwc, hwc->idx);
229 barrier(); /* why? */
Will Deacon57273472012-03-06 17:33:17 +0100230 armpmu_event_update(event, hwc, hwc->idx);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200231 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
232 }
233}
234
235static void
236armpmu_start(struct perf_event *event, int flags)
237{
Mark Rutland8a16b342011-04-28 16:27:54 +0100238 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200239 struct hw_perf_event *hwc = &event->hw;
240
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200241 /*
242 * ARM pmu always has to reprogram the period, so ignore
243 * PERF_EF_RELOAD, see the comment below.
244 */
245 if (flags & PERF_EF_RELOAD)
246 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
247
248 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100249 /*
250 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200251 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100252 * may have been left counting. If we don't do this step then we may
253 * get an interrupt too soon or *way* too late if the overflow has
254 * happened since disabling.
255 */
256 armpmu_event_set_period(event, hwc, hwc->idx);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700257 armpmu->enable(hwc, hwc->idx, event->cpu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100258}
259
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200260static void
261armpmu_del(struct perf_event *event, int flags)
262{
Mark Rutland8a16b342011-04-28 16:27:54 +0100263 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100264 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200265 struct hw_perf_event *hwc = &event->hw;
266 int idx = hwc->idx;
267
268 WARN_ON(idx < 0);
269
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200270 armpmu_stop(event, PERF_EF_UPDATE);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100271 hw_events->events[idx] = NULL;
272 clear_bit(idx, hw_events->used_mask);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200273
274 perf_event_update_userpage(event);
275}
276
Jamie Iles1b8873a2010-02-02 20:25:44 +0100277static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200278armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100279{
Mark Rutland8a16b342011-04-28 16:27:54 +0100280 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100281 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100282 struct hw_perf_event *hwc = &event->hw;
283 int idx;
284 int err = 0;
285
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200286 perf_pmu_disable(event->pmu);
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200287
Jamie Iles1b8873a2010-02-02 20:25:44 +0100288 /* If we don't have a space for the counter then finish early. */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100289 idx = armpmu->get_event_idx(hw_events, hwc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100290 if (idx < 0) {
291 err = idx;
292 goto out;
293 }
294
295 /*
296 * If there is an event in the counter we are going to use then make
297 * sure it is disabled.
298 */
299 event->hw.idx = idx;
300 armpmu->disable(hwc, idx);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100301 hw_events->events[idx] = event;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100302
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200303 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
304 if (flags & PERF_EF_START)
305 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100306
307 /* Propagate our changes to the userspace mapping. */
308 perf_event_update_userpage(event);
309
310out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200311 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100312 return err;
313}
314
Jamie Iles1b8873a2010-02-02 20:25:44 +0100315static int
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100316validate_event(struct pmu_hw_events *hw_events,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100317 struct perf_event *event)
318{
Mark Rutland8a16b342011-04-28 16:27:54 +0100319 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100320 struct hw_perf_event fake_event = event->hw;
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100321 struct pmu *leader_pmu = event->group_leader->pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100322
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100323 if (event->pmu != leader_pmu || event->state <= PERF_EVENT_STATE_OFF)
Will Deacon65b47112010-09-02 09:32:08 +0100324 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100325
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100326 return armpmu->get_event_idx(hw_events, &fake_event) >= 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100327}
328
329static int
330validate_group(struct perf_event *event)
331{
332 struct perf_event *sibling, *leader = event->group_leader;
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100333 struct pmu_hw_events fake_pmu;
Will Deaconbce34d12011-11-17 15:05:14 +0000334 DECLARE_BITMAP(fake_used_mask, ARMPMU_MAX_HWEVENTS);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100335
Will Deaconbce34d12011-11-17 15:05:14 +0000336 /*
337 * Initialise the fake PMU. We only need to populate the
338 * used_mask for the purposes of validation.
339 */
340 memset(fake_used_mask, 0, sizeof(fake_used_mask));
341 fake_pmu.used_mask = fake_used_mask;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100342
343 if (!validate_event(&fake_pmu, leader))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100344 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100345
346 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
347 if (!validate_event(&fake_pmu, sibling))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100348 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100349 }
350
351 if (!validate_event(&fake_pmu, event))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100352 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100353
354 return 0;
355}
356
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530357static irqreturn_t armpmu_platform_irq(int irq, void *dev)
358{
Mark Rutland8a16b342011-04-28 16:27:54 +0100359 struct arm_pmu *armpmu = (struct arm_pmu *) dev;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100360 struct platform_device *plat_device = armpmu->plat_device;
361 struct arm_pmu_platdata *plat = dev_get_platdata(&plat_device->dev);
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530362
363 return plat->handle_irq(irq, dev, armpmu->handle_irq);
364}
365
Steve Mucklef132c6c2012-06-06 18:30:57 -0700366int
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500367armpmu_generic_request_irq(int irq, irq_handler_t *handle_irq)
368{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700369 return request_irq(irq, *handle_irq,
370 IRQF_DISABLED | IRQF_NOBALANCING,
371 "armpmu", NULL);
372}
373
374void
375armpmu_generic_free_irq(int irq)
376{
377 if (irq >= 0)
378 free_irq(irq, NULL);
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500379}
380
Will Deacon0b390e22011-07-27 15:18:59 +0100381static void
Mark Rutland8a16b342011-04-28 16:27:54 +0100382armpmu_release_hardware(struct arm_pmu *armpmu)
Will Deacon0b390e22011-07-27 15:18:59 +0100383{
384 int i, irq, irqs;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100385 struct platform_device *pmu_device = armpmu->plat_device;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700386#if 0
Ming Leie0516a62011-03-02 15:00:08 +0800387 struct arm_pmu_platdata *plat =
388 dev_get_platdata(&pmu_device->dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700389#endif
Will Deacon0b390e22011-07-27 15:18:59 +0100390 irqs = min(pmu_device->num_resources, num_possible_cpus());
391
392 for (i = 0; i < irqs; ++i) {
393 if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs))
394 continue;
395 irq = platform_get_irq(pmu_device, i);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700396 armpmu->free_pmu_irq(irq);
397#if 0
Ming Leie0516a62011-03-02 15:00:08 +0800398 if (irq >= 0) {
399 if (plat && plat->disable_irq)
400 plat->disable_irq(irq);
Mark Rutland8a16b342011-04-28 16:27:54 +0100401 free_irq(irq, armpmu);
Ming Leie0516a62011-03-02 15:00:08 +0800402 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700403#endif
Will Deacon0b390e22011-07-27 15:18:59 +0100404 }
405
Mark Rutland7ae18a52011-06-06 10:37:50 +0100406 release_pmu(armpmu->type);
Will Deacon0b390e22011-07-27 15:18:59 +0100407}
408
Jamie Iles1b8873a2010-02-02 20:25:44 +0100409static int
Mark Rutland8a16b342011-04-28 16:27:54 +0100410armpmu_reserve_hardware(struct arm_pmu *armpmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100411{
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530412 struct arm_pmu_platdata *plat;
413 irq_handler_t handle_irq;
Will Deaconb0e89592011-07-26 22:10:28 +0100414 int i, err, irq, irqs;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100415 struct platform_device *pmu_device = armpmu->plat_device;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100416
Will Deacone5a21322011-11-22 18:01:46 +0000417 if (!pmu_device)
418 return -ENODEV;
419
Mark Rutland7ae18a52011-06-06 10:37:50 +0100420 err = reserve_pmu(armpmu->type);
Will Deaconb0e89592011-07-26 22:10:28 +0100421 if (err) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100422 pr_warning("unable to reserve pmu\n");
Will Deaconb0e89592011-07-26 22:10:28 +0100423 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100424 }
425
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530426 plat = dev_get_platdata(&pmu_device->dev);
427 if (plat && plat->handle_irq)
428 handle_irq = armpmu_platform_irq;
429 else
430 handle_irq = armpmu->handle_irq;
431
Will Deacon0b390e22011-07-27 15:18:59 +0100432 irqs = min(pmu_device->num_resources, num_possible_cpus());
Will Deaconb0e89592011-07-26 22:10:28 +0100433 if (irqs < 1) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100434 pr_err("no irqs for PMUs defined\n");
435 return -ENODEV;
436 }
437
Will Deaconb0e89592011-07-26 22:10:28 +0100438 for (i = 0; i < irqs; ++i) {
Will Deacon0b390e22011-07-27 15:18:59 +0100439 err = 0;
Will Deacon49c006b2010-04-29 17:13:24 +0100440 irq = platform_get_irq(pmu_device, i);
441 if (irq < 0)
442 continue;
443
Will Deaconb0e89592011-07-26 22:10:28 +0100444 /*
445 * If we have a single PMU interrupt that we can't shift,
446 * assume that we're running on a uniprocessor machine and
Will Deacon0b390e22011-07-27 15:18:59 +0100447 * continue. Otherwise, continue without this interrupt.
Will Deaconb0e89592011-07-26 22:10:28 +0100448 */
Will Deacon0b390e22011-07-27 15:18:59 +0100449 if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
450 pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
451 irq, i);
452 continue;
Will Deaconb0e89592011-07-26 22:10:28 +0100453 }
454
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500455 err = armpmu->request_pmu_irq(irq, &handle_irq);
456
Steve Mucklef132c6c2012-06-06 18:30:57 -0700457 if (err) {
458 pr_warning("unable to request IRQ%d for %s perf "
459 "counters\n", irq, armpmu->name);
460
461 armpmu_release_hardware(cpu_pmu);
462 return err;
463 }
464
465#if 0
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530466 err = request_irq(irq, handle_irq,
Will Deaconddee87f2010-02-25 15:04:14 +0100467 IRQF_DISABLED | IRQF_NOBALANCING,
Mark Rutland8a16b342011-04-28 16:27:54 +0100468 "arm-pmu", armpmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100469 if (err) {
Will Deaconb0e89592011-07-26 22:10:28 +0100470 pr_err("unable to request IRQ%d for ARM PMU counters\n",
471 irq);
Mark Rutland8a16b342011-04-28 16:27:54 +0100472 armpmu_release_hardware(armpmu);
Will Deacon0b390e22011-07-27 15:18:59 +0100473 return err;
Ming Leie0516a62011-03-02 15:00:08 +0800474 } else if (plat && plat->enable_irq)
475 plat->enable_irq(irq);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700476#endif
Will Deacon0b390e22011-07-27 15:18:59 +0100477
478 cpumask_set_cpu(i, &armpmu->active_irqs);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100479 }
480
Will Deacon0b390e22011-07-27 15:18:59 +0100481 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100482}
483
Jamie Iles1b8873a2010-02-02 20:25:44 +0100484static void
485hw_perf_event_destroy(struct perf_event *event)
486{
Mark Rutland8a16b342011-04-28 16:27:54 +0100487 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland03b78982011-04-27 11:20:11 +0100488 atomic_t *active_events = &armpmu->active_events;
489 struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
490
491 if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
Mark Rutland8a16b342011-04-28 16:27:54 +0100492 armpmu_release_hardware(armpmu);
Mark Rutland03b78982011-04-27 11:20:11 +0100493 mutex_unlock(pmu_reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100494 }
495}
496
497static int
Will Deacon05d22fd2011-07-19 11:57:30 +0100498event_requires_mode_exclusion(struct perf_event_attr *attr)
499{
500 return attr->exclude_idle || attr->exclude_user ||
501 attr->exclude_kernel || attr->exclude_hv;
502}
503
504static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100505__hw_perf_event_init(struct perf_event *event)
506{
Mark Rutland8a16b342011-04-28 16:27:54 +0100507 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100508 struct hw_perf_event *hwc = &event->hw;
509 int mapping, err;
510
Mark Rutlande1f431b2011-04-28 15:47:10 +0100511 mapping = armpmu->map_event(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100512
513 if (mapping < 0) {
514 pr_debug("event %x:%llx not supported\n", event->attr.type,
515 event->attr.config);
516 return mapping;
517 }
518
519 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100520 * We don't assign an index until we actually place the event onto
521 * hardware. Use -1 to signify that we haven't decided where to put it
522 * yet. For SMP systems, each core has it's own PMU so we can't do any
523 * clever allocation or constraints checking at this point.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100524 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100525 hwc->idx = -1;
526 hwc->config_base = 0;
527 hwc->config = 0;
528 hwc->event_base = 0;
529
530 /*
531 * Check whether we need to exclude the counter from certain modes.
532 */
533 if ((!armpmu->set_event_filter ||
534 armpmu->set_event_filter(hwc, &event->attr)) &&
535 event_requires_mode_exclusion(&event->attr)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100536 pr_debug("ARM performance counters do not support "
537 "mode exclusion\n");
538 return -EPERM;
539 }
540
541 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100542 * Store the event encoding into the config_base field.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100543 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100544 hwc->config_base |= (unsigned long)mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100545
546 if (!hwc->sample_period) {
Will Deacon57273472012-03-06 17:33:17 +0100547 /*
548 * For non-sampling runs, limit the sample_period to half
549 * of the counter width. That way, the new counter value
550 * is far less likely to overtake the previous one unless
551 * you have some serious IRQ latency issues.
552 */
553 hwc->sample_period = armpmu->max_period >> 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100554 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200555 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100556 }
557
558 err = 0;
559 if (event->group_leader != event) {
560 err = validate_group(event);
561 if (err)
562 return -EINVAL;
563 }
564
565 return err;
566}
567
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200568static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100569{
Mark Rutland8a16b342011-04-28 16:27:54 +0100570 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100571 int err = 0;
Mark Rutland03b78982011-04-27 11:20:11 +0100572 atomic_t *active_events = &armpmu->active_events;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100573
Stephane Eranian2481c5f2012-02-09 23:20:59 +0100574 /* does not support taken branch sampling */
575 if (has_branch_stack(event))
576 return -EOPNOTSUPP;
577
Mark Rutlande1f431b2011-04-28 15:47:10 +0100578 if (armpmu->map_event(event) == -ENOENT)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200579 return -ENOENT;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200580
Jamie Iles1b8873a2010-02-02 20:25:44 +0100581 event->destroy = hw_perf_event_destroy;
582
Mark Rutland03b78982011-04-27 11:20:11 +0100583 if (!atomic_inc_not_zero(active_events)) {
584 mutex_lock(&armpmu->reserve_mutex);
585 if (atomic_read(active_events) == 0)
Mark Rutland8a16b342011-04-28 16:27:54 +0100586 err = armpmu_reserve_hardware(armpmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100587
588 if (!err)
Mark Rutland03b78982011-04-27 11:20:11 +0100589 atomic_inc(active_events);
590 mutex_unlock(&armpmu->reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100591 }
592
593 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200594 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100595
596 err = __hw_perf_event_init(event);
597 if (err)
598 hw_perf_event_destroy(event);
599
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200600 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100601}
602
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200603static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100604{
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100605 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100606 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
Mark Rutland7325eae2011-08-23 11:59:49 +0100607 int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100608
Will Deaconf4f38432011-07-01 14:38:12 +0100609 if (enabled)
610 armpmu->start();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100611}
612
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200613static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100614{
Mark Rutland8a16b342011-04-28 16:27:54 +0100615 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Mark Rutland48957152011-04-27 10:31:51 +0100616 armpmu->stop();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100617}
618
Mark Rutland03b78982011-04-27 11:20:11 +0100619static void __init armpmu_init(struct arm_pmu *armpmu)
620{
621 atomic_set(&armpmu->active_events, 0);
622 mutex_init(&armpmu->reserve_mutex);
Mark Rutland8a16b342011-04-28 16:27:54 +0100623
624 armpmu->pmu = (struct pmu) {
625 .pmu_enable = armpmu_enable,
626 .pmu_disable = armpmu_disable,
627 .event_init = armpmu_event_init,
628 .add = armpmu_add,
629 .del = armpmu_del,
630 .start = armpmu_start,
631 .stop = armpmu_stop,
632 .read = armpmu_read,
633 };
634}
635
Mark Rutland0ce47082011-05-19 10:07:57 +0100636int __init armpmu_register(struct arm_pmu *armpmu, char *name, int type)
Mark Rutland8a16b342011-04-28 16:27:54 +0100637{
638 armpmu_init(armpmu);
639 return perf_pmu_register(&armpmu->pmu, name, type);
Mark Rutland03b78982011-04-27 11:20:11 +0100640}
641
Will Deacon43eab872010-11-13 19:04:32 +0000642/* Include the PMU-specific implementations. */
643#include "perf_event_xscale.c"
644#include "perf_event_v6.c"
645#include "perf_event_v7.c"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646#include "perf_event_msm_krait.c"
Will Deacon49e6a322010-04-30 11:33:33 +0100647
Will Deacon574b69c2011-03-25 13:13:34 +0100648/*
649 * Ensure the PMU has sane values out of reset.
650 * This requires SMP to be available, so exists as a separate initcall.
651 */
652static int __init
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100653cpu_pmu_reset(void)
Will Deacon574b69c2011-03-25 13:13:34 +0100654{
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100655 if (cpu_pmu && cpu_pmu->reset)
656 return on_each_cpu(cpu_pmu->reset, NULL, 1);
Will Deacon574b69c2011-03-25 13:13:34 +0100657 return 0;
658}
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100659arch_initcall(cpu_pmu_reset);
Will Deacon574b69c2011-03-25 13:13:34 +0100660
Will Deaconb0e89592011-07-26 22:10:28 +0100661/*
662 * PMU platform driver and devicetree bindings.
663 */
664static struct of_device_id armpmu_of_device_ids[] = {
665 {.compatible = "arm,cortex-a9-pmu"},
666 {.compatible = "arm,cortex-a8-pmu"},
667 {.compatible = "arm,arm1136-pmu"},
668 {.compatible = "arm,arm1176-pmu"},
669 {},
670};
671
672static struct platform_device_id armpmu_plat_device_ids[] = {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700673 {.name = "cpu-arm-pmu"},
Will Deaconb0e89592011-07-26 22:10:28 +0100674 {},
675};
676
677static int __devinit armpmu_device_probe(struct platform_device *pdev)
678{
Will Deacon6bd05402011-12-02 18:16:01 +0100679 if (!cpu_pmu)
680 return -ENODEV;
681
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100682 cpu_pmu->plat_device = pdev;
Will Deaconb0e89592011-07-26 22:10:28 +0100683 return 0;
684}
685
686static struct platform_driver armpmu_driver = {
687 .driver = {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700688 .name = "cpu-arm-pmu",
Will Deaconb0e89592011-07-26 22:10:28 +0100689 .of_match_table = armpmu_of_device_ids,
690 },
691 .probe = armpmu_device_probe,
692 .id_table = armpmu_plat_device_ids,
693};
694
695static int __init register_pmu_driver(void)
696{
697 return platform_driver_register(&armpmu_driver);
698}
699device_initcall(register_pmu_driver);
700
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100701static struct pmu_hw_events *armpmu_get_cpu_events(void)
Mark Rutland92f701e2011-05-04 09:23:51 +0100702{
703 return &__get_cpu_var(cpu_hw_events);
704}
705
706static void __init cpu_pmu_init(struct arm_pmu *armpmu)
707{
Mark Rutland0f78d2d2011-04-28 10:17:04 +0100708 int cpu;
709 for_each_possible_cpu(cpu) {
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100710 struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
Mark Rutland3fc2c832011-06-24 11:30:59 +0100711 events->events = per_cpu(hw_events, cpu);
712 events->used_mask = per_cpu(used_mask, cpu);
Mark Rutland0f78d2d2011-04-28 10:17:04 +0100713 raw_spin_lock_init(&events->pmu_lock);
714 }
Mark Rutland92f701e2011-05-04 09:23:51 +0100715 armpmu->get_hw_events = armpmu_get_cpu_events;
Mark Rutland7ae18a52011-06-06 10:37:50 +0100716 armpmu->type = ARM_PMU_DEVICE_CPU;
Mark Rutland92f701e2011-05-04 09:23:51 +0100717}
718
Will Deaconb0e89592011-07-26 22:10:28 +0100719/*
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100720 * PMU hardware loses all context when a CPU goes offline.
721 * When a CPU is hotplugged back in, since some hardware registers are
722 * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
723 * junk values out of them.
724 */
725static int __cpuinit pmu_cpu_notify(struct notifier_block *b,
726 unsigned long action, void *hcpu)
727{
728 if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
729 return NOTIFY_DONE;
730
731 if (cpu_pmu && cpu_pmu->reset)
732 cpu_pmu->reset(NULL);
733
734 return NOTIFY_OK;
735}
736
737static struct notifier_block __cpuinitdata pmu_cpu_notifier = {
738 .notifier_call = pmu_cpu_notify,
739};
740
741/*
Will Deaconb0e89592011-07-26 22:10:28 +0100742 * CPU PMU identification and registration.
743 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100744static int __init
745init_hw_perf_events(void)
746{
747 unsigned long cpuid = read_cpuid_id();
748 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
749 unsigned long part_number = (cpuid & 0xFFF0);
750
Will Deacon49e6a322010-04-30 11:33:33 +0100751 /* ARM Ltd CPUs. */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100752 if (0x41 == implementor) {
753 switch (part_number) {
754 case 0xB360: /* ARM1136 */
755 case 0xB560: /* ARM1156 */
756 case 0xB760: /* ARM1176 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100757 cpu_pmu = armv6pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100758 break;
759 case 0xB020: /* ARM11mpcore */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100760 cpu_pmu = armv6mpcore_pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100761 break;
Jean PIHET796d1292010-01-26 18:51:05 +0100762 case 0xC080: /* Cortex-A8 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100763 cpu_pmu = armv7_a8_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100764 break;
765 case 0xC090: /* Cortex-A9 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100766 cpu_pmu = armv7_a9_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100767 break;
Will Deacon0c205cb2011-06-03 17:40:15 +0100768 case 0xC050: /* Cortex-A5 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100769 cpu_pmu = armv7_a5_pmu_init();
Will Deacon0c205cb2011-06-03 17:40:15 +0100770 break;
Will Deacon14abd032011-01-19 14:24:38 +0000771 case 0xC0F0: /* Cortex-A15 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100772 cpu_pmu = armv7_a15_pmu_init();
Will Deacon14abd032011-01-19 14:24:38 +0000773 break;
Will Deacond33c88c2012-02-03 14:46:01 +0100774 case 0xC070: /* Cortex-A7 */
775 cpu_pmu = armv7_a7_pmu_init();
776 break;
Will Deacon49e6a322010-04-30 11:33:33 +0100777 }
778 /* Intel CPUs [xscale]. */
779 } else if (0x69 == implementor) {
780 part_number = (cpuid >> 13) & 0x7;
781 switch (part_number) {
782 case 1:
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100783 cpu_pmu = xscale1pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100784 break;
785 case 2:
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100786 cpu_pmu = xscale2pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100787 break;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100788 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700789 /* Qualcomm CPUs */
790 } else if (0x51 == implementor) {
791 switch (part_number) {
792 case 0x00F0: /* 8x50 & 7x30*/
Steve Mucklef132c6c2012-06-06 18:30:57 -0700793// cpu_pmu = armv7_scorpion_pmu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794 break;
795 case 0x02D0: /* 8x60 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700796// fabricmon_pmu_init();
797// cpu_pmu = armv7_scorpionmp_pmu_init();
798// scorpionmp_l2_pmu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700799 break;
800 case 0x0490: /* 8960 sim */
801 case 0x04D0: /* 8960 */
Neil Leedered415112012-02-09 13:34:09 -0500802 case 0x06F0: /* 8064 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700803// fabricmon_pmu_init();
804 cpu_pmu = armv7_krait_pmu_init();
805// krait_l2_pmu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806 break;
807 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100808 }
809
Steve Mucklef132c6c2012-06-06 18:30:57 -0700810
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100811 if (cpu_pmu) {
Jean PIHET796d1292010-01-26 18:51:05 +0100812 pr_info("enabled with %s PMU driver, %d counters available\n",
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100813 cpu_pmu->name, cpu_pmu->num_events);
814 cpu_pmu_init(cpu_pmu);
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100815 register_cpu_notifier(&pmu_cpu_notifier);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100816 armpmu_register(cpu_pmu, "cpu", PERF_TYPE_RAW);
Will Deacon49e6a322010-04-30 11:33:33 +0100817 } else {
818 pr_info("no hardware support available\n");
Will Deacon49e6a322010-04-30 11:33:33 +0100819 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100820
821 return 0;
822}
Peter Zijlstra004417a2010-11-25 18:38:29 +0100823early_initcall(init_hw_perf_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100824
825/*
826 * Callchain handling code.
827 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100828
829/*
830 * The registers we're interested in are at the end of the variable
831 * length saved register structure. The fp points at the end of this
832 * structure so the address of this struct is:
833 * (struct frame_tail *)(xxx->fp)-1
834 *
835 * This code has been adapted from the ARM OProfile support.
836 */
837struct frame_tail {
Will Deacon4d6b7a72010-11-30 18:15:53 +0100838 struct frame_tail __user *fp;
839 unsigned long sp;
840 unsigned long lr;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100841} __attribute__((packed));
842
843/*
844 * Get the return address for a single stackframe and return a pointer to the
845 * next frame tail.
846 */
Will Deacon4d6b7a72010-11-30 18:15:53 +0100847static struct frame_tail __user *
848user_backtrace(struct frame_tail __user *tail,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100849 struct perf_callchain_entry *entry)
850{
851 struct frame_tail buftail;
852
853 /* Also check accessibility of one struct frame_tail beyond */
854 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
855 return NULL;
856 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
857 return NULL;
858
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200859 perf_callchain_store(entry, buftail.lr);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100860
861 /*
862 * Frame pointers should strictly progress back up the stack
863 * (towards higher addresses).
864 */
Rabin Vincentcb061992011-02-09 11:35:12 +0100865 if (tail + 1 >= buftail.fp)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100866 return NULL;
867
868 return buftail.fp - 1;
869}
870
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200871void
872perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100873{
Will Deacon4d6b7a72010-11-30 18:15:53 +0100874 struct frame_tail __user *tail;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100875
Jamie Iles1b8873a2010-02-02 20:25:44 +0100876
Will Deacon4d6b7a72010-11-30 18:15:53 +0100877 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100878
Sonny Rao860ad782011-04-18 22:12:59 +0100879 while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
880 tail && !((unsigned long)tail & 0x3))
Jamie Iles1b8873a2010-02-02 20:25:44 +0100881 tail = user_backtrace(tail, entry);
882}
883
884/*
885 * Gets called by walk_stackframe() for every stackframe. This will be called
886 * whist unwinding the stackframe and is like a subroutine return so we use
887 * the PC.
888 */
889static int
890callchain_trace(struct stackframe *fr,
891 void *data)
892{
893 struct perf_callchain_entry *entry = data;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +0200894 perf_callchain_store(entry, fr->pc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100895 return 0;
896}
897
Frederic Weisbecker56962b42010-06-30 23:03:51 +0200898void
899perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100900{
901 struct stackframe fr;
902
Jamie Iles1b8873a2010-02-02 20:25:44 +0100903 fr.fp = regs->ARM_fp;
904 fr.sp = regs->ARM_sp;
905 fr.lr = regs->ARM_lr;
906 fr.pc = regs->ARM_pc;
907 walk_stackframe(&fr, callchain_trace, entry);
908}