blob: 8fb93d07de0e1795f9642f8c7721918468b9cac7 [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
Ashwin Chaugule5343d0c2012-09-06 17:49:31 -040044static DEFINE_PER_CPU(u32, from_idle);
Mark Rutland3fc2c832011-06-24 11:30:59 +010045static DEFINE_PER_CPU(struct perf_event * [ARMPMU_MAX_HWEVENTS], hw_events);
46static DEFINE_PER_CPU(unsigned long [BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)], used_mask);
Mark Rutland8be3f9a2011-05-17 11:20:11 +010047static DEFINE_PER_CPU(struct pmu_hw_events, cpu_hw_events);
Will Deacon181193f2010-04-30 11:32:44 +010048
Mark Rutland8a16b342011-04-28 16:27:54 +010049#define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu))
50
Jamie Iles1b8873a2010-02-02 20:25:44 +010051/* Set at runtime when we know what CPU type we are. */
Mark Rutland8be3f9a2011-05-17 11:20:11 +010052static struct arm_pmu *cpu_pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +010053
Will Deacon181193f2010-04-30 11:32:44 +010054enum arm_perf_pmu_ids
55armpmu_get_pmu_id(void)
56{
57 int id = -ENODEV;
58
Mark Rutland8be3f9a2011-05-17 11:20:11 +010059 if (cpu_pmu != NULL)
60 id = cpu_pmu->id;
Will Deacon181193f2010-04-30 11:32:44 +010061
62 return id;
63}
64EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
65
Will Deaconfeb45d02011-11-14 10:33:05 +000066int perf_num_counters(void)
Will Deacon929f5192010-04-30 11:34:26 +010067{
68 int max_events = 0;
69
Mark Rutland8be3f9a2011-05-17 11:20:11 +010070 if (cpu_pmu != NULL)
71 max_events = cpu_pmu->num_events;
Will Deacon929f5192010-04-30 11:34:26 +010072
73 return max_events;
74}
Matt Fleming3bf101b2010-09-27 20:22:24 +010075EXPORT_SYMBOL_GPL(perf_num_counters);
76
Jamie Iles1b8873a2010-02-02 20:25:44 +010077#define HW_OP_UNSUPPORTED 0xFFFF
78
79#define C(_x) \
80 PERF_COUNT_HW_CACHE_##_x
81
82#define CACHE_OP_UNSUPPORTED 0xFFFF
83
Jamie Iles1b8873a2010-02-02 20:25:44 +010084static int
Steve Mucklef132c6c2012-06-06 18:30:57 -070085armpmu_map_cache_event(unsigned (*cache_map)
Mark Rutlande1f431b2011-04-28 15:47:10 +010086 [PERF_COUNT_HW_CACHE_MAX]
87 [PERF_COUNT_HW_CACHE_OP_MAX]
88 [PERF_COUNT_HW_CACHE_RESULT_MAX],
89 u64 config)
Jamie Iles1b8873a2010-02-02 20:25:44 +010090{
91 unsigned int cache_type, cache_op, cache_result, ret;
92
93 cache_type = (config >> 0) & 0xff;
94 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
95 return -EINVAL;
96
97 cache_op = (config >> 8) & 0xff;
98 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
99 return -EINVAL;
100
101 cache_result = (config >> 16) & 0xff;
102 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
103 return -EINVAL;
104
Mark Rutlande1f431b2011-04-28 15:47:10 +0100105 ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
Jamie Iles1b8873a2010-02-02 20:25:44 +0100106
107 if (ret == CACHE_OP_UNSUPPORTED)
108 return -ENOENT;
109
110 return ret;
111}
112
113static int
Mark Rutlande1f431b2011-04-28 15:47:10 +0100114armpmu_map_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
Will Deacon84fee972010-11-13 17:13:56 +0000115{
Mark Rutlande1f431b2011-04-28 15:47:10 +0100116 int mapping = (*event_map)[config];
117 return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
Will Deacon84fee972010-11-13 17:13:56 +0000118}
119
120static int
Mark Rutlande1f431b2011-04-28 15:47:10 +0100121armpmu_map_raw_event(u32 raw_event_mask, u64 config)
Will Deacon84fee972010-11-13 17:13:56 +0000122{
Mark Rutlande1f431b2011-04-28 15:47:10 +0100123 return (int)(config & raw_event_mask);
124}
125
126static int map_cpu_event(struct perf_event *event,
127 const unsigned (*event_map)[PERF_COUNT_HW_MAX],
Steve Mucklef132c6c2012-06-06 18:30:57 -0700128 unsigned (*cache_map)
Mark Rutlande1f431b2011-04-28 15:47:10 +0100129 [PERF_COUNT_HW_CACHE_MAX]
130 [PERF_COUNT_HW_CACHE_OP_MAX]
131 [PERF_COUNT_HW_CACHE_RESULT_MAX],
132 u32 raw_event_mask)
133{
134 u64 config = event->attr.config;
135
136 switch (event->attr.type) {
137 case PERF_TYPE_HARDWARE:
138 return armpmu_map_event(event_map, config);
139 case PERF_TYPE_HW_CACHE:
140 return armpmu_map_cache_event(cache_map, config);
141 case PERF_TYPE_RAW:
142 return armpmu_map_raw_event(raw_event_mask, config);
143 }
144
145 return -ENOENT;
Will Deacon84fee972010-11-13 17:13:56 +0000146}
147
Mark Rutland0ce47082011-05-19 10:07:57 +0100148int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100149armpmu_event_set_period(struct perf_event *event,
150 struct hw_perf_event *hwc,
151 int idx)
152{
Mark Rutland8a16b342011-04-28 16:27:54 +0100153 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Peter Zijlstrae7850592010-05-21 14:43:08 +0200154 s64 left = local64_read(&hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100155 s64 period = hwc->sample_period;
156 int ret = 0;
157
158 if (unlikely(left <= -period)) {
159 left = period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200160 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100161 hwc->last_period = period;
162 ret = 1;
163 }
164
165 if (unlikely(left <= 0)) {
166 left += period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200167 local64_set(&hwc->period_left, left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100168 hwc->last_period = period;
169 ret = 1;
170 }
171
172 if (left > (s64)armpmu->max_period)
173 left = armpmu->max_period;
174
Peter Zijlstrae7850592010-05-21 14:43:08 +0200175 local64_set(&hwc->prev_count, (u64)-left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100176
177 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
178
179 perf_event_update_userpage(event);
180
181 return ret;
182}
183
Mark Rutland0ce47082011-05-19 10:07:57 +0100184u64
Jamie Iles1b8873a2010-02-02 20:25:44 +0100185armpmu_event_update(struct perf_event *event,
186 struct hw_perf_event *hwc,
Will Deacon57273472012-03-06 17:33:17 +0100187 int idx)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100188{
Mark Rutland8a16b342011-04-28 16:27:54 +0100189 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Will Deacona7378232011-03-25 17:12:37 +0100190 u64 delta, prev_raw_count, new_raw_count;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100191
192again:
Peter Zijlstrae7850592010-05-21 14:43:08 +0200193 prev_raw_count = local64_read(&hwc->prev_count);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100194 new_raw_count = armpmu->read_counter(idx);
195
Peter Zijlstrae7850592010-05-21 14:43:08 +0200196 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100197 new_raw_count) != prev_raw_count)
198 goto again;
199
Will Deacon57273472012-03-06 17:33:17 +0100200 delta = (new_raw_count - prev_raw_count) & armpmu->max_period;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100201
Peter Zijlstrae7850592010-05-21 14:43:08 +0200202 local64_add(delta, &event->count);
203 local64_sub(delta, &hwc->period_left);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100204
205 return new_raw_count;
206}
207
208static void
Jamie Iles1b8873a2010-02-02 20:25:44 +0100209armpmu_read(struct perf_event *event)
210{
211 struct hw_perf_event *hwc = &event->hw;
212
213 /* Don't read disabled counters! */
214 if (hwc->idx < 0)
215 return;
216
Will Deacon57273472012-03-06 17:33:17 +0100217 armpmu_event_update(event, hwc, hwc->idx);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100218}
219
220static void
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200221armpmu_stop(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100222{
Mark Rutland8a16b342011-04-28 16:27:54 +0100223 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100224 struct hw_perf_event *hwc = &event->hw;
225
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200226 /*
227 * ARM pmu always has to update the counter, so ignore
228 * PERF_EF_UPDATE, see comments in armpmu_start().
229 */
230 if (!(hwc->state & PERF_HES_STOPPED)) {
231 armpmu->disable(hwc, hwc->idx);
232 barrier(); /* why? */
Will Deacon57273472012-03-06 17:33:17 +0100233 armpmu_event_update(event, hwc, hwc->idx);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200234 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
235 }
236}
237
238static void
239armpmu_start(struct perf_event *event, int flags)
240{
Mark Rutland8a16b342011-04-28 16:27:54 +0100241 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200242 struct hw_perf_event *hwc = &event->hw;
243
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200244 /*
245 * ARM pmu always has to reprogram the period, so ignore
246 * PERF_EF_RELOAD, see the comment below.
247 */
248 if (flags & PERF_EF_RELOAD)
249 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
250
251 hwc->state = 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100252 /*
253 * Set the period again. Some counters can't be stopped, so when we
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200254 * were stopped we simply disabled the IRQ source and the counter
Jamie Iles1b8873a2010-02-02 20:25:44 +0100255 * may have been left counting. If we don't do this step then we may
256 * get an interrupt too soon or *way* too late if the overflow has
257 * happened since disabling.
258 */
259 armpmu_event_set_period(event, hwc, hwc->idx);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700260 armpmu->enable(hwc, hwc->idx, event->cpu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100261}
262
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200263static void
264armpmu_del(struct perf_event *event, int flags)
265{
Mark Rutland8a16b342011-04-28 16:27:54 +0100266 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100267 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200268 struct hw_perf_event *hwc = &event->hw;
269 int idx = hwc->idx;
270
271 WARN_ON(idx < 0);
272
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200273 armpmu_stop(event, PERF_EF_UPDATE);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100274 hw_events->events[idx] = NULL;
275 clear_bit(idx, hw_events->used_mask);
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200276
Ashwin Chaugule66a8a862012-06-13 14:58:04 -0400277 /* Clear event constraints. */
278 if (armpmu->clear_event_constraints)
279 armpmu->clear_event_constraints(event);
280
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200281 perf_event_update_userpage(event);
282}
283
Jamie Iles1b8873a2010-02-02 20:25:44 +0100284static int
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200285armpmu_add(struct perf_event *event, int flags)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100286{
Mark Rutland8a16b342011-04-28 16:27:54 +0100287 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100288 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100289 struct hw_perf_event *hwc = &event->hw;
290 int idx;
291 int err = 0;
292
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200293 perf_pmu_disable(event->pmu);
Ashwin Chaugule66a8a862012-06-13 14:58:04 -0400294 /*
295 * Tests if event is constrained. If not sets it so that next
296 * collision can be detected.
297 */
298 if (armpmu->test_set_event_constraints)
299 if (armpmu->test_set_event_constraints(event) < 0) {
300 pr_err("Event: %llx failed constraint check.\n",
301 event->attr.config);
302 event->state = PERF_EVENT_STATE_OFF;
303 goto out;
304 }
Peter Zijlstra24cd7f52010-06-11 17:32:03 +0200305
Jamie Iles1b8873a2010-02-02 20:25:44 +0100306 /* If we don't have a space for the counter then finish early. */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100307 idx = armpmu->get_event_idx(hw_events, hwc);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100308 if (idx < 0) {
309 err = idx;
310 goto out;
311 }
312
313 /*
314 * If there is an event in the counter we are going to use then make
315 * sure it is disabled.
316 */
317 event->hw.idx = idx;
318 armpmu->disable(hwc, idx);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100319 hw_events->events[idx] = event;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100320
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200321 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
322 if (flags & PERF_EF_START)
323 armpmu_start(event, PERF_EF_RELOAD);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100324
325 /* Propagate our changes to the userspace mapping. */
326 perf_event_update_userpage(event);
327
328out:
Peter Zijlstra33696fc2010-06-14 08:49:00 +0200329 perf_pmu_enable(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100330 return err;
331}
332
Jamie Iles1b8873a2010-02-02 20:25:44 +0100333static int
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100334validate_event(struct pmu_hw_events *hw_events,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100335 struct perf_event *event)
336{
Mark Rutland8a16b342011-04-28 16:27:54 +0100337 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100338 struct hw_perf_event fake_event = event->hw;
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100339 struct pmu *leader_pmu = event->group_leader->pmu;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100340
Mark Rutland7b9f72c2011-04-27 16:22:21 +0100341 if (event->pmu != leader_pmu || event->state <= PERF_EVENT_STATE_OFF)
Will Deacon65b47112010-09-02 09:32:08 +0100342 return 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100343
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100344 return armpmu->get_event_idx(hw_events, &fake_event) >= 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100345}
346
347static int
348validate_group(struct perf_event *event)
349{
350 struct perf_event *sibling, *leader = event->group_leader;
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100351 struct pmu_hw_events fake_pmu;
Will Deaconbce34d12011-11-17 15:05:14 +0000352 DECLARE_BITMAP(fake_used_mask, ARMPMU_MAX_HWEVENTS);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100353
Will Deaconbce34d12011-11-17 15:05:14 +0000354 /*
355 * Initialise the fake PMU. We only need to populate the
356 * used_mask for the purposes of validation.
357 */
358 memset(fake_used_mask, 0, sizeof(fake_used_mask));
359 fake_pmu.used_mask = fake_used_mask;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100360
361 if (!validate_event(&fake_pmu, leader))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100362 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100363
364 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
365 if (!validate_event(&fake_pmu, sibling))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100366 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100367 }
368
369 if (!validate_event(&fake_pmu, event))
Peter Zijlstraaa2bc1a2011-11-09 17:56:37 +0100370 return -EINVAL;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100371
372 return 0;
373}
374
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530375static irqreturn_t armpmu_platform_irq(int irq, void *dev)
376{
Mark Rutland8a16b342011-04-28 16:27:54 +0100377 struct arm_pmu *armpmu = (struct arm_pmu *) dev;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100378 struct platform_device *plat_device = armpmu->plat_device;
379 struct arm_pmu_platdata *plat = dev_get_platdata(&plat_device->dev);
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530380
381 return plat->handle_irq(irq, dev, armpmu->handle_irq);
382}
383
Steve Mucklef132c6c2012-06-06 18:30:57 -0700384int
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500385armpmu_generic_request_irq(int irq, irq_handler_t *handle_irq)
386{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700387 return request_irq(irq, *handle_irq,
388 IRQF_DISABLED | IRQF_NOBALANCING,
389 "armpmu", NULL);
390}
391
392void
393armpmu_generic_free_irq(int irq)
394{
395 if (irq >= 0)
396 free_irq(irq, NULL);
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500397}
398
Will Deacon0b390e22011-07-27 15:18:59 +0100399static void
Mark Rutland8a16b342011-04-28 16:27:54 +0100400armpmu_release_hardware(struct arm_pmu *armpmu)
Will Deacon0b390e22011-07-27 15:18:59 +0100401{
402 int i, irq, irqs;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100403 struct platform_device *pmu_device = armpmu->plat_device;
Ashwin Chaugulef53fe442012-06-07 13:41:37 -0400404
Will Deacon0b390e22011-07-27 15:18:59 +0100405 irqs = min(pmu_device->num_resources, num_possible_cpus());
406
407 for (i = 0; i < irqs; ++i) {
408 if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs))
409 continue;
410 irq = platform_get_irq(pmu_device, i);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700411 armpmu->free_pmu_irq(irq);
Will Deacon0b390e22011-07-27 15:18:59 +0100412 }
413
Mark Rutland7ae18a52011-06-06 10:37:50 +0100414 release_pmu(armpmu->type);
Will Deacon0b390e22011-07-27 15:18:59 +0100415}
416
Jamie Iles1b8873a2010-02-02 20:25:44 +0100417static int
Mark Rutland8a16b342011-04-28 16:27:54 +0100418armpmu_reserve_hardware(struct arm_pmu *armpmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100419{
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530420 struct arm_pmu_platdata *plat;
421 irq_handler_t handle_irq;
Will Deaconb0e89592011-07-26 22:10:28 +0100422 int i, err, irq, irqs;
Mark Rutlanda9356a02011-05-04 09:23:15 +0100423 struct platform_device *pmu_device = armpmu->plat_device;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100424
Will Deacone5a21322011-11-22 18:01:46 +0000425 if (!pmu_device)
426 return -ENODEV;
427
Mark Rutland7ae18a52011-06-06 10:37:50 +0100428 err = reserve_pmu(armpmu->type);
Will Deaconb0e89592011-07-26 22:10:28 +0100429 if (err) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100430 pr_warning("unable to reserve pmu\n");
Will Deaconb0e89592011-07-26 22:10:28 +0100431 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100432 }
433
Rabin Vincent0e25a5c2011-02-08 09:24:36 +0530434 plat = dev_get_platdata(&pmu_device->dev);
435 if (plat && plat->handle_irq)
436 handle_irq = armpmu_platform_irq;
437 else
438 handle_irq = armpmu->handle_irq;
439
Ashwin Chaugule6c755b22012-10-29 16:30:05 -0400440 if (plat && plat->request_pmu_irq)
441 armpmu->request_pmu_irq = plat->request_pmu_irq;
Ashwin Chaugule1f7e7ba2012-11-27 14:49:58 -0500442 else if (!armpmu->request_pmu_irq)
Ashwin Chaugule6c755b22012-10-29 16:30:05 -0400443 armpmu->request_pmu_irq = armpmu_generic_request_irq;
444
445 if (plat && plat->free_pmu_irq)
446 armpmu->free_pmu_irq = plat->free_pmu_irq;
Ashwin Chaugule2f71d2b2012-12-06 09:56:15 -0500447 else if (!armpmu->free_pmu_irq)
Ashwin Chaugule6c755b22012-10-29 16:30:05 -0400448 armpmu->free_pmu_irq = armpmu_generic_free_irq;
449
Will Deacon0b390e22011-07-27 15:18:59 +0100450 irqs = min(pmu_device->num_resources, num_possible_cpus());
Will Deaconb0e89592011-07-26 22:10:28 +0100451 if (irqs < 1) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100452 pr_err("no irqs for PMUs defined\n");
453 return -ENODEV;
454 }
455
Will Deaconb0e89592011-07-26 22:10:28 +0100456 for (i = 0; i < irqs; ++i) {
Will Deacon0b390e22011-07-27 15:18:59 +0100457 err = 0;
Will Deacon49c006b2010-04-29 17:13:24 +0100458 irq = platform_get_irq(pmu_device, i);
459 if (irq < 0)
460 continue;
461
Will Deaconb0e89592011-07-26 22:10:28 +0100462 /*
463 * If we have a single PMU interrupt that we can't shift,
464 * assume that we're running on a uniprocessor machine and
Will Deacon0b390e22011-07-27 15:18:59 +0100465 * continue. Otherwise, continue without this interrupt.
Will Deaconb0e89592011-07-26 22:10:28 +0100466 */
Will Deacon0b390e22011-07-27 15:18:59 +0100467 if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
468 pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
469 irq, i);
470 continue;
Will Deaconb0e89592011-07-26 22:10:28 +0100471 }
472
Ashwin Chaugule4afdedc2012-01-17 13:23:50 -0500473 err = armpmu->request_pmu_irq(irq, &handle_irq);
474
Steve Mucklef132c6c2012-06-06 18:30:57 -0700475 if (err) {
476 pr_warning("unable to request IRQ%d for %s perf "
477 "counters\n", irq, armpmu->name);
478
479 armpmu_release_hardware(cpu_pmu);
480 return err;
481 }
482
Will Deacon0b390e22011-07-27 15:18:59 +0100483 cpumask_set_cpu(i, &armpmu->active_irqs);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100484 }
485
Will Deacon0b390e22011-07-27 15:18:59 +0100486 return 0;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100487}
488
Jamie Iles1b8873a2010-02-02 20:25:44 +0100489static void
490hw_perf_event_destroy(struct perf_event *event)
491{
Mark Rutland8a16b342011-04-28 16:27:54 +0100492 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Mark Rutland03b78982011-04-27 11:20:11 +0100493 atomic_t *active_events = &armpmu->active_events;
494 struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
495
496 if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
Mark Rutland8a16b342011-04-28 16:27:54 +0100497 armpmu_release_hardware(armpmu);
Mark Rutland03b78982011-04-27 11:20:11 +0100498 mutex_unlock(pmu_reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100499 }
500}
501
502static int
Will Deacon05d22fd2011-07-19 11:57:30 +0100503event_requires_mode_exclusion(struct perf_event_attr *attr)
504{
505 return attr->exclude_idle || attr->exclude_user ||
506 attr->exclude_kernel || attr->exclude_hv;
507}
508
509static int
Jamie Iles1b8873a2010-02-02 20:25:44 +0100510__hw_perf_event_init(struct perf_event *event)
511{
Mark Rutland8a16b342011-04-28 16:27:54 +0100512 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100513 struct hw_perf_event *hwc = &event->hw;
514 int mapping, err;
515
Mark Rutlande1f431b2011-04-28 15:47:10 +0100516 mapping = armpmu->map_event(event);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100517
518 if (mapping < 0) {
519 pr_debug("event %x:%llx not supported\n", event->attr.type,
520 event->attr.config);
521 return mapping;
522 }
523
524 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100525 * We don't assign an index until we actually place the event onto
526 * hardware. Use -1 to signify that we haven't decided where to put it
527 * yet. For SMP systems, each core has it's own PMU so we can't do any
528 * clever allocation or constraints checking at this point.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100529 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100530 hwc->idx = -1;
531 hwc->config_base = 0;
532 hwc->config = 0;
533 hwc->event_base = 0;
534
535 /*
536 * Check whether we need to exclude the counter from certain modes.
537 */
538 if ((!armpmu->set_event_filter ||
539 armpmu->set_event_filter(hwc, &event->attr)) &&
540 event_requires_mode_exclusion(&event->attr)) {
Jamie Iles1b8873a2010-02-02 20:25:44 +0100541 pr_debug("ARM performance counters do not support "
542 "mode exclusion\n");
543 return -EPERM;
544 }
545
Ashwin Chaugule66a8a862012-06-13 14:58:04 -0400546
Jamie Iles1b8873a2010-02-02 20:25:44 +0100547 /*
Will Deacon05d22fd2011-07-19 11:57:30 +0100548 * Store the event encoding into the config_base field.
Jamie Iles1b8873a2010-02-02 20:25:44 +0100549 */
Will Deacon05d22fd2011-07-19 11:57:30 +0100550 hwc->config_base |= (unsigned long)mapping;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100551
552 if (!hwc->sample_period) {
Will Deacon57273472012-03-06 17:33:17 +0100553 /*
554 * For non-sampling runs, limit the sample_period to half
555 * of the counter width. That way, the new counter value
556 * is far less likely to overtake the previous one unless
557 * you have some serious IRQ latency issues.
558 */
559 hwc->sample_period = armpmu->max_period >> 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100560 hwc->last_period = hwc->sample_period;
Peter Zijlstrae7850592010-05-21 14:43:08 +0200561 local64_set(&hwc->period_left, hwc->sample_period);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100562 }
563
564 err = 0;
565 if (event->group_leader != event) {
566 err = validate_group(event);
567 if (err)
568 return -EINVAL;
569 }
570
571 return err;
572}
573
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200574static int armpmu_event_init(struct perf_event *event)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100575{
Mark Rutland8a16b342011-04-28 16:27:54 +0100576 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100577 int err = 0;
Mark Rutland03b78982011-04-27 11:20:11 +0100578 atomic_t *active_events = &armpmu->active_events;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100579
Stephane Eranian2481c5f2012-02-09 23:20:59 +0100580 /* does not support taken branch sampling */
581 if (has_branch_stack(event))
582 return -EOPNOTSUPP;
583
Mark Rutlande1f431b2011-04-28 15:47:10 +0100584 if (armpmu->map_event(event) == -ENOENT)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200585 return -ENOENT;
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200586
Jamie Iles1b8873a2010-02-02 20:25:44 +0100587 event->destroy = hw_perf_event_destroy;
588
Mark Rutland03b78982011-04-27 11:20:11 +0100589 if (!atomic_inc_not_zero(active_events)) {
590 mutex_lock(&armpmu->reserve_mutex);
591 if (atomic_read(active_events) == 0)
Mark Rutland8a16b342011-04-28 16:27:54 +0100592 err = armpmu_reserve_hardware(armpmu);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100593
594 if (!err)
Mark Rutland03b78982011-04-27 11:20:11 +0100595 atomic_inc(active_events);
596 mutex_unlock(&armpmu->reserve_mutex);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100597 }
598
599 if (err)
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200600 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100601
602 err = __hw_perf_event_init(event);
603 if (err)
604 hw_perf_event_destroy(event);
605
Peter Zijlstrab0a873e2010-06-11 13:35:08 +0200606 return err;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100607}
608
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200609static void armpmu_enable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100610{
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100611 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100612 struct pmu_hw_events *hw_events = armpmu->get_hw_events();
Mark Rutland7325eae2011-08-23 11:59:49 +0100613 int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
Ashwin Chauguleb2c31d42012-08-03 16:30:08 -0400614 int idx;
615
Ashwin Chaugule5343d0c2012-09-06 17:49:31 -0400616 if (__get_cpu_var(from_idle)) {
Ashwin Chauguleb2c31d42012-08-03 16:30:08 -0400617 for (idx = 0; idx <= cpu_pmu->num_events; ++idx) {
618 struct perf_event *event = hw_events->events[idx];
619
620 if (!event)
621 continue;
622
623 armpmu->enable(&event->hw, idx, event->cpu);
624 }
625
626 /* Reset bit so we don't needlessly re-enable counters.*/
Ashwin Chaugule5343d0c2012-09-06 17:49:31 -0400627 __get_cpu_var(from_idle) = 0;
Ashwin Chauguleb2c31d42012-08-03 16:30:08 -0400628 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100629
Ashwin Chaugule5343d0c2012-09-06 17:49:31 -0400630 /* So we don't start the PMU before enabling counters after idle. */
631 barrier();
632
Will Deaconf4f38432011-07-01 14:38:12 +0100633 if (enabled)
634 armpmu->start();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100635}
636
Peter Zijlstraa4eaf7f2010-06-16 14:37:10 +0200637static void armpmu_disable(struct pmu *pmu)
Jamie Iles1b8873a2010-02-02 20:25:44 +0100638{
Mark Rutland8a16b342011-04-28 16:27:54 +0100639 struct arm_pmu *armpmu = to_arm_pmu(pmu);
Mark Rutland48957152011-04-27 10:31:51 +0100640 armpmu->stop();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100641}
642
Ashwin Chaugule4a81cb82012-06-07 13:40:54 -0400643static void armpmu_init(struct arm_pmu *armpmu)
Mark Rutland03b78982011-04-27 11:20:11 +0100644{
645 atomic_set(&armpmu->active_events, 0);
646 mutex_init(&armpmu->reserve_mutex);
Mark Rutland8a16b342011-04-28 16:27:54 +0100647
Ashwin Chaugule795bf7b2012-06-20 16:23:08 -0400648 armpmu->pmu.pmu_enable = armpmu_enable;
649 armpmu->pmu.pmu_disable = armpmu_disable;
650 armpmu->pmu.event_init = armpmu_event_init;
651 armpmu->pmu.add = armpmu_add;
652 armpmu->pmu.del = armpmu_del;
653 armpmu->pmu.start = armpmu_start;
654 armpmu->pmu.stop = armpmu_stop;
655 armpmu->pmu.read = armpmu_read;
Mark Rutland8a16b342011-04-28 16:27:54 +0100656}
657
Ashwin Chaugule4a81cb82012-06-07 13:40:54 -0400658int armpmu_register(struct arm_pmu *armpmu, char *name, int type)
Mark Rutland8a16b342011-04-28 16:27:54 +0100659{
660 armpmu_init(armpmu);
661 return perf_pmu_register(&armpmu->pmu, name, type);
Mark Rutland03b78982011-04-27 11:20:11 +0100662}
663
Will Deacon43eab872010-11-13 19:04:32 +0000664/* Include the PMU-specific implementations. */
665#include "perf_event_xscale.c"
666#include "perf_event_v6.c"
667#include "perf_event_v7.c"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700668#include "perf_event_msm_krait.c"
Ashwin Chaugule7cd836e2012-06-11 16:26:47 -0400669#include "perf_event_msm.c"
Will Deacon49e6a322010-04-30 11:33:33 +0100670
Will Deacon574b69c2011-03-25 13:13:34 +0100671/*
672 * Ensure the PMU has sane values out of reset.
673 * This requires SMP to be available, so exists as a separate initcall.
674 */
675static int __init
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100676cpu_pmu_reset(void)
Will Deacon574b69c2011-03-25 13:13:34 +0100677{
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100678 if (cpu_pmu && cpu_pmu->reset)
679 return on_each_cpu(cpu_pmu->reset, NULL, 1);
Will Deacon574b69c2011-03-25 13:13:34 +0100680 return 0;
681}
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100682arch_initcall(cpu_pmu_reset);
Will Deacon574b69c2011-03-25 13:13:34 +0100683
Will Deaconb0e89592011-07-26 22:10:28 +0100684/*
685 * PMU platform driver and devicetree bindings.
686 */
687static struct of_device_id armpmu_of_device_ids[] = {
688 {.compatible = "arm,cortex-a9-pmu"},
689 {.compatible = "arm,cortex-a8-pmu"},
690 {.compatible = "arm,arm1136-pmu"},
691 {.compatible = "arm,arm1176-pmu"},
692 {},
693};
694
695static struct platform_device_id armpmu_plat_device_ids[] = {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700696 {.name = "cpu-arm-pmu"},
Will Deaconb0e89592011-07-26 22:10:28 +0100697 {},
698};
699
700static int __devinit armpmu_device_probe(struct platform_device *pdev)
701{
Will Deacon6bd05402011-12-02 18:16:01 +0100702 if (!cpu_pmu)
703 return -ENODEV;
704
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100705 cpu_pmu->plat_device = pdev;
Will Deaconb0e89592011-07-26 22:10:28 +0100706 return 0;
707}
708
709static struct platform_driver armpmu_driver = {
710 .driver = {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700711 .name = "cpu-arm-pmu",
Will Deaconb0e89592011-07-26 22:10:28 +0100712 .of_match_table = armpmu_of_device_ids,
713 },
714 .probe = armpmu_device_probe,
715 .id_table = armpmu_plat_device_ids,
716};
717
718static int __init register_pmu_driver(void)
719{
720 return platform_driver_register(&armpmu_driver);
721}
722device_initcall(register_pmu_driver);
723
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100724static struct pmu_hw_events *armpmu_get_cpu_events(void)
Mark Rutland92f701e2011-05-04 09:23:51 +0100725{
726 return &__get_cpu_var(cpu_hw_events);
727}
728
729static void __init cpu_pmu_init(struct arm_pmu *armpmu)
730{
Mark Rutland0f78d2d2011-04-28 10:17:04 +0100731 int cpu;
732 for_each_possible_cpu(cpu) {
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100733 struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
Mark Rutland3fc2c832011-06-24 11:30:59 +0100734 events->events = per_cpu(hw_events, cpu);
735 events->used_mask = per_cpu(used_mask, cpu);
Mark Rutland0f78d2d2011-04-28 10:17:04 +0100736 raw_spin_lock_init(&events->pmu_lock);
737 }
Mark Rutland92f701e2011-05-04 09:23:51 +0100738 armpmu->get_hw_events = armpmu_get_cpu_events;
Mark Rutland7ae18a52011-06-06 10:37:50 +0100739 armpmu->type = ARM_PMU_DEVICE_CPU;
Mark Rutland92f701e2011-05-04 09:23:51 +0100740}
741
Ashwin Chaugule4cdf85a2013-01-16 11:22:08 -0500742static int cpu_has_active_perf(void)
743{
744 struct pmu_hw_events *hw_events;
745 int enabled;
746
747 if (!cpu_pmu)
748 return 0;
749
750 hw_events = cpu_pmu->get_hw_events();
751 enabled = bitmap_weight(hw_events->used_mask, cpu_pmu->num_events);
752
753 if (enabled)
754 /*Even one event's existence is good enough.*/
755 return 1;
756
757 return 0;
758}
759
760void enable_irq_callback(void *info)
761{
762 int irq = *(unsigned int *)info;
763 enable_percpu_irq(irq, IRQ_TYPE_EDGE_RISING);
764}
765
766void disable_irq_callback(void *info)
767{
768 int irq = *(unsigned int *)info;
769 disable_percpu_irq(irq);
770}
771
Will Deaconb0e89592011-07-26 22:10:28 +0100772/*
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100773 * PMU hardware loses all context when a CPU goes offline.
774 * When a CPU is hotplugged back in, since some hardware registers are
775 * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
776 * junk values out of them.
777 */
778static int __cpuinit pmu_cpu_notify(struct notifier_block *b,
779 unsigned long action, void *hcpu)
780{
Ashwin Chaugule4cdf85a2013-01-16 11:22:08 -0500781 int irq;
782
783 if (cpu_has_active_perf()) {
784 switch ((action & ~CPU_TASKS_FROZEN)) {
785
786 case CPU_DOWN_PREPARE:
787 /*
788 * If this is on a multicore CPU, we need
789 * to disarm the PMU IRQ before disappearing.
790 */
791 if (cpu_pmu &&
792 cpu_pmu->plat_device->dev.platform_data) {
Neil Leeder05cfbed2013-02-19 16:10:10 -0500793 irq = platform_get_irq(cpu_pmu->plat_device, 0);
Ashwin Chaugule4cdf85a2013-01-16 11:22:08 -0500794 smp_call_function_single((int)hcpu,
795 disable_irq_callback, &irq, 1);
796 }
797 return NOTIFY_DONE;
798
799 case CPU_UP_PREPARE:
800 /*
801 * If this is on a multicore CPU, we need
802 * to arm the PMU IRQ before appearing.
803 */
804 if (cpu_pmu &&
805 cpu_pmu->plat_device->dev.platform_data) {
Neil Leeder05cfbed2013-02-19 16:10:10 -0500806 irq = platform_get_irq(cpu_pmu->plat_device, 0);
Ashwin Chaugule4cdf85a2013-01-16 11:22:08 -0500807 smp_call_function_single((int)hcpu,
808 enable_irq_callback, &irq, 1);
809 }
810 return NOTIFY_DONE;
811
812 case CPU_STARTING:
813 if (cpu_pmu && cpu_pmu->reset) {
814 cpu_pmu->reset(NULL);
815 return NOTIFY_OK;
816 }
817 default:
818 return NOTIFY_DONE;
819 }
820 }
821
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100822 if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
823 return NOTIFY_DONE;
824
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100825 return NOTIFY_OK;
826}
827
Ashwin Chaugulef53fe442012-06-07 13:41:37 -0400828static void armpmu_update_counters(void)
829{
830 struct pmu_hw_events *hw_events;
831 int idx;
832
833 if (!cpu_pmu)
834 return;
835
836 hw_events = cpu_pmu->get_hw_events();
837
838 for (idx = 0; idx <= cpu_pmu->num_events; ++idx) {
839 struct perf_event *event = hw_events->events[idx];
840
841 if (!event)
842 continue;
843
844 armpmu_read(event);
845 }
846}
847
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100848static struct notifier_block __cpuinitdata pmu_cpu_notifier = {
849 .notifier_call = pmu_cpu_notify,
850};
851
Ashwin Chaugulef53fe442012-06-07 13:41:37 -0400852/*TODO: Unify with pending patch from ARM */
853static int perf_cpu_pm_notifier(struct notifier_block *self, unsigned long cmd,
854 void *v)
855{
856 switch (cmd) {
857 case CPU_PM_ENTER:
858 if (cpu_has_active_perf()) {
859 armpmu_update_counters();
860 perf_pmu_disable(&cpu_pmu->pmu);
861 }
862 break;
863
864 case CPU_PM_ENTER_FAILED:
865 case CPU_PM_EXIT:
866 if (cpu_has_active_perf() && cpu_pmu->reset) {
Ashwin Chauguleb2c31d42012-08-03 16:30:08 -0400867 /*
868 * Flip this bit so armpmu_enable knows it needs
869 * to re-enable active counters.
870 */
Ashwin Chaugule5343d0c2012-09-06 17:49:31 -0400871 __get_cpu_var(from_idle) = 1;
Ashwin Chaugulef53fe442012-06-07 13:41:37 -0400872 cpu_pmu->reset(NULL);
873 perf_pmu_enable(&cpu_pmu->pmu);
874 }
875 break;
876 }
877
878 return NOTIFY_OK;
879}
880
881static struct notifier_block perf_cpu_pm_notifier_block = {
882 .notifier_call = perf_cpu_pm_notifier,
883};
884
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100885/*
Will Deaconb0e89592011-07-26 22:10:28 +0100886 * CPU PMU identification and registration.
887 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100888static int __init
889init_hw_perf_events(void)
890{
891 unsigned long cpuid = read_cpuid_id();
892 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
893 unsigned long part_number = (cpuid & 0xFFF0);
894
Will Deacon49e6a322010-04-30 11:33:33 +0100895 /* ARM Ltd CPUs. */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100896 if (0x41 == implementor) {
897 switch (part_number) {
898 case 0xB360: /* ARM1136 */
899 case 0xB560: /* ARM1156 */
900 case 0xB760: /* ARM1176 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100901 cpu_pmu = armv6pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100902 break;
903 case 0xB020: /* ARM11mpcore */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100904 cpu_pmu = armv6mpcore_pmu_init();
Jamie Iles1b8873a2010-02-02 20:25:44 +0100905 break;
Jean PIHET796d1292010-01-26 18:51:05 +0100906 case 0xC080: /* Cortex-A8 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100907 cpu_pmu = armv7_a8_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100908 break;
909 case 0xC090: /* Cortex-A9 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100910 cpu_pmu = armv7_a9_pmu_init();
Jean PIHET796d1292010-01-26 18:51:05 +0100911 break;
Will Deacon0c205cb2011-06-03 17:40:15 +0100912 case 0xC050: /* Cortex-A5 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100913 cpu_pmu = armv7_a5_pmu_init();
Will Deacon0c205cb2011-06-03 17:40:15 +0100914 break;
Will Deacon14abd032011-01-19 14:24:38 +0000915 case 0xC0F0: /* Cortex-A15 */
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100916 cpu_pmu = armv7_a15_pmu_init();
Will Deacon14abd032011-01-19 14:24:38 +0000917 break;
Will Deacond33c88c2012-02-03 14:46:01 +0100918 case 0xC070: /* Cortex-A7 */
919 cpu_pmu = armv7_a7_pmu_init();
920 break;
Will Deacon49e6a322010-04-30 11:33:33 +0100921 }
922 /* Intel CPUs [xscale]. */
923 } else if (0x69 == implementor) {
924 part_number = (cpuid >> 13) & 0x7;
925 switch (part_number) {
926 case 1:
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100927 cpu_pmu = xscale1pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100928 break;
929 case 2:
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100930 cpu_pmu = xscale2pmu_init();
Will Deacon49e6a322010-04-30 11:33:33 +0100931 break;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100932 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933 /* Qualcomm CPUs */
934 } else if (0x51 == implementor) {
935 switch (part_number) {
936 case 0x00F0: /* 8x50 & 7x30*/
Ashwin Chaugule7cd836e2012-06-11 16:26:47 -0400937 cpu_pmu = armv7_scorpion_pmu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700938 break;
939 case 0x02D0: /* 8x60 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700940// fabricmon_pmu_init();
Ashwin Chaugule7cd836e2012-06-11 16:26:47 -0400941 cpu_pmu = armv7_scorpionmp_pmu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700942 break;
943 case 0x0490: /* 8960 sim */
944 case 0x04D0: /* 8960 */
Neil Leedered415112012-02-09 13:34:09 -0500945 case 0x06F0: /* 8064 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700946// fabricmon_pmu_init();
947 cpu_pmu = armv7_krait_pmu_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948 break;
949 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100950 }
951
Steve Mucklef132c6c2012-06-06 18:30:57 -0700952
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100953 if (cpu_pmu) {
Jean PIHET796d1292010-01-26 18:51:05 +0100954 pr_info("enabled with %s PMU driver, %d counters available\n",
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100955 cpu_pmu->name, cpu_pmu->num_events);
956 cpu_pmu_init(cpu_pmu);
Lorenzo Pieralisia0feb6d2012-03-06 17:37:45 +0100957 register_cpu_notifier(&pmu_cpu_notifier);
Mark Rutland8be3f9a2011-05-17 11:20:11 +0100958 armpmu_register(cpu_pmu, "cpu", PERF_TYPE_RAW);
Ashwin Chaugulef53fe442012-06-07 13:41:37 -0400959 cpu_pm_register_notifier(&perf_cpu_pm_notifier_block);
Will Deacon49e6a322010-04-30 11:33:33 +0100960 } else {
961 pr_info("no hardware support available\n");
Will Deacon49e6a322010-04-30 11:33:33 +0100962 }
Jamie Iles1b8873a2010-02-02 20:25:44 +0100963
964 return 0;
965}
Peter Zijlstra004417a2010-11-25 18:38:29 +0100966early_initcall(init_hw_perf_events);
Jamie Iles1b8873a2010-02-02 20:25:44 +0100967
968/*
969 * Callchain handling code.
970 */
Jamie Iles1b8873a2010-02-02 20:25:44 +0100971
972/*
973 * The registers we're interested in are at the end of the variable
974 * length saved register structure. The fp points at the end of this
975 * structure so the address of this struct is:
976 * (struct frame_tail *)(xxx->fp)-1
977 *
978 * This code has been adapted from the ARM OProfile support.
979 */
980struct frame_tail {
Will Deacon4d6b7a72010-11-30 18:15:53 +0100981 struct frame_tail __user *fp;
982 unsigned long sp;
983 unsigned long lr;
Jamie Iles1b8873a2010-02-02 20:25:44 +0100984} __attribute__((packed));
985
986/*
987 * Get the return address for a single stackframe and return a pointer to the
988 * next frame tail.
989 */
Will Deacon4d6b7a72010-11-30 18:15:53 +0100990static struct frame_tail __user *
991user_backtrace(struct frame_tail __user *tail,
Jamie Iles1b8873a2010-02-02 20:25:44 +0100992 struct perf_callchain_entry *entry)
993{
994 struct frame_tail buftail;
995
996 /* Also check accessibility of one struct frame_tail beyond */
997 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
998 return NULL;
999 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
1000 return NULL;
1001
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001002 perf_callchain_store(entry, buftail.lr);
Jamie Iles1b8873a2010-02-02 20:25:44 +01001003
1004 /*
1005 * Frame pointers should strictly progress back up the stack
1006 * (towards higher addresses).
1007 */
Rabin Vincentcb061992011-02-09 11:35:12 +01001008 if (tail + 1 >= buftail.fp)
Jamie Iles1b8873a2010-02-02 20:25:44 +01001009 return NULL;
1010
1011 return buftail.fp - 1;
1012}
1013
Frederic Weisbecker56962b42010-06-30 23:03:51 +02001014void
1015perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +01001016{
Will Deacon4d6b7a72010-11-30 18:15:53 +01001017 struct frame_tail __user *tail;
Jamie Iles1b8873a2010-02-02 20:25:44 +01001018
Jamie Iles1b8873a2010-02-02 20:25:44 +01001019
Will Deacon4d6b7a72010-11-30 18:15:53 +01001020 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
Jamie Iles1b8873a2010-02-02 20:25:44 +01001021
Sonny Rao860ad782011-04-18 22:12:59 +01001022 while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
1023 tail && !((unsigned long)tail & 0x3))
Jamie Iles1b8873a2010-02-02 20:25:44 +01001024 tail = user_backtrace(tail, entry);
1025}
1026
1027/*
1028 * Gets called by walk_stackframe() for every stackframe. This will be called
1029 * whist unwinding the stackframe and is like a subroutine return so we use
1030 * the PC.
1031 */
1032static int
1033callchain_trace(struct stackframe *fr,
1034 void *data)
1035{
1036 struct perf_callchain_entry *entry = data;
Frederic Weisbecker70791ce2010-06-29 19:34:05 +02001037 perf_callchain_store(entry, fr->pc);
Jamie Iles1b8873a2010-02-02 20:25:44 +01001038 return 0;
1039}
1040
Frederic Weisbecker56962b42010-06-30 23:03:51 +02001041void
1042perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
Jamie Iles1b8873a2010-02-02 20:25:44 +01001043{
1044 struct stackframe fr;
1045
Jamie Iles1b8873a2010-02-02 20:25:44 +01001046 fr.fp = regs->ARM_fp;
1047 fr.sp = regs->ARM_sp;
1048 fr.lr = regs->ARM_lr;
1049 fr.pc = regs->ARM_pc;
1050 walk_stackframe(&fr, callchain_trace, entry);
1051}