ARM: 6834/1: perf: reset counters on all CPUs during initialisation
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / arm / kernel / perf_event.c
CommitLineData
1b8873a0
JI
1#undef DEBUG
2
3/*
4 * ARM performance counter support.
5 *
6 * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
43eab878 7 * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
796d1295 8 *
1b8873a0
JI
9 * This code is based on the sparc64 perf event code, which is in turn based
10 * on the x86 code. Callchain code is based on the ARM OProfile backtrace
11 * code.
12 */
13#define pr_fmt(fmt) "hw perfevents: " fmt
14
15#include <linux/interrupt.h>
16#include <linux/kernel.h>
181193f3 17#include <linux/module.h>
1b8873a0 18#include <linux/perf_event.h>
49c006b9 19#include <linux/platform_device.h>
1b8873a0
JI
20#include <linux/spinlock.h>
21#include <linux/uaccess.h>
22
23#include <asm/cputype.h>
24#include <asm/irq.h>
25#include <asm/irq_regs.h>
26#include <asm/pmu.h>
27#include <asm/stacktrace.h>
28
49c006b9 29static struct platform_device *pmu_device;
1b8873a0
JI
30
31/*
32 * Hardware lock to serialize accesses to PMU registers. Needed for the
33 * read/modify/write sequences.
34 */
961ec6da 35static DEFINE_RAW_SPINLOCK(pmu_lock);
1b8873a0
JI
36
37/*
38 * ARMv6 supports a maximum of 3 events, starting from index 1. If we add
39 * another platform that supports more, we need to increase this to be the
40 * largest of all platforms.
796d1295
JP
41 *
42 * ARMv7 supports up to 32 events:
43 * cycle counter CCNT + 31 events counters CNT0..30.
44 * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
1b8873a0 45 */
796d1295 46#define ARMPMU_MAX_HWEVENTS 33
1b8873a0
JI
47
48/* The events for a given CPU. */
49struct cpu_hw_events {
50 /*
51 * The events that are active on the CPU for the given index. Index 0
52 * is reserved.
53 */
54 struct perf_event *events[ARMPMU_MAX_HWEVENTS];
55
56 /*
57 * A 1 bit for an index indicates that the counter is being used for
58 * an event. A 0 means that the counter can be used.
59 */
60 unsigned long used_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
61
62 /*
63 * A 1 bit for an index indicates that the counter is actively being
64 * used.
65 */
66 unsigned long active_mask[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)];
67};
4d6b7a77 68static DEFINE_PER_CPU(struct cpu_hw_events, cpu_hw_events);
181193f3 69
1b8873a0 70struct arm_pmu {
181193f3 71 enum arm_perf_pmu_ids id;
62994831 72 const char *name;
1b8873a0
JI
73 irqreturn_t (*handle_irq)(int irq_num, void *dev);
74 void (*enable)(struct hw_perf_event *evt, int idx);
75 void (*disable)(struct hw_perf_event *evt, int idx);
1b8873a0
JI
76 int (*get_event_idx)(struct cpu_hw_events *cpuc,
77 struct hw_perf_event *hwc);
78 u32 (*read_counter)(int idx);
79 void (*write_counter)(int idx, u32 val);
80 void (*start)(void);
81 void (*stop)(void);
574b69cb 82 void (*reset)(void *);
84fee97a
WD
83 const unsigned (*cache_map)[PERF_COUNT_HW_CACHE_MAX]
84 [PERF_COUNT_HW_CACHE_OP_MAX]
85 [PERF_COUNT_HW_CACHE_RESULT_MAX];
86 const unsigned (*event_map)[PERF_COUNT_HW_MAX];
87 u32 raw_event_mask;
1b8873a0
JI
88 int num_events;
89 u64 max_period;
90};
91
92/* Set at runtime when we know what CPU type we are. */
93static const struct arm_pmu *armpmu;
94
181193f3
WD
95enum arm_perf_pmu_ids
96armpmu_get_pmu_id(void)
97{
98 int id = -ENODEV;
99
100 if (armpmu != NULL)
101 id = armpmu->id;
102
103 return id;
104}
105EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
106
929f5199
WD
107int
108armpmu_get_max_events(void)
109{
110 int max_events = 0;
111
112 if (armpmu != NULL)
113 max_events = armpmu->num_events;
114
115 return max_events;
116}
117EXPORT_SYMBOL_GPL(armpmu_get_max_events);
118
3bf101ba
MF
119int perf_num_counters(void)
120{
121 return armpmu_get_max_events();
122}
123EXPORT_SYMBOL_GPL(perf_num_counters);
124
1b8873a0
JI
125#define HW_OP_UNSUPPORTED 0xFFFF
126
127#define C(_x) \
128 PERF_COUNT_HW_CACHE_##_x
129
130#define CACHE_OP_UNSUPPORTED 0xFFFF
131
1b8873a0
JI
132static int
133armpmu_map_cache_event(u64 config)
134{
135 unsigned int cache_type, cache_op, cache_result, ret;
136
137 cache_type = (config >> 0) & 0xff;
138 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
139 return -EINVAL;
140
141 cache_op = (config >> 8) & 0xff;
142 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
143 return -EINVAL;
144
145 cache_result = (config >> 16) & 0xff;
146 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
147 return -EINVAL;
148
84fee97a 149 ret = (int)(*armpmu->cache_map)[cache_type][cache_op][cache_result];
1b8873a0
JI
150
151 if (ret == CACHE_OP_UNSUPPORTED)
152 return -ENOENT;
153
154 return ret;
155}
156
84fee97a
WD
157static int
158armpmu_map_event(u64 config)
159{
160 int mapping = (*armpmu->event_map)[config];
161 return mapping == HW_OP_UNSUPPORTED ? -EOPNOTSUPP : mapping;
162}
163
164static int
165armpmu_map_raw_event(u64 config)
166{
167 return (int)(config & armpmu->raw_event_mask);
168}
169
1b8873a0
JI
170static int
171armpmu_event_set_period(struct perf_event *event,
172 struct hw_perf_event *hwc,
173 int idx)
174{
e7850595 175 s64 left = local64_read(&hwc->period_left);
1b8873a0
JI
176 s64 period = hwc->sample_period;
177 int ret = 0;
178
179 if (unlikely(left <= -period)) {
180 left = period;
e7850595 181 local64_set(&hwc->period_left, left);
1b8873a0
JI
182 hwc->last_period = period;
183 ret = 1;
184 }
185
186 if (unlikely(left <= 0)) {
187 left += period;
e7850595 188 local64_set(&hwc->period_left, left);
1b8873a0
JI
189 hwc->last_period = period;
190 ret = 1;
191 }
192
193 if (left > (s64)armpmu->max_period)
194 left = armpmu->max_period;
195
e7850595 196 local64_set(&hwc->prev_count, (u64)-left);
1b8873a0
JI
197
198 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
199
200 perf_event_update_userpage(event);
201
202 return ret;
203}
204
205static u64
206armpmu_event_update(struct perf_event *event,
207 struct hw_perf_event *hwc,
208 int idx)
209{
210 int shift = 64 - 32;
211 s64 prev_raw_count, new_raw_count;
446a5a8b 212 u64 delta;
1b8873a0
JI
213
214again:
e7850595 215 prev_raw_count = local64_read(&hwc->prev_count);
1b8873a0
JI
216 new_raw_count = armpmu->read_counter(idx);
217
e7850595 218 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
1b8873a0
JI
219 new_raw_count) != prev_raw_count)
220 goto again;
221
222 delta = (new_raw_count << shift) - (prev_raw_count << shift);
223 delta >>= shift;
224
e7850595
PZ
225 local64_add(delta, &event->count);
226 local64_sub(delta, &hwc->period_left);
1b8873a0
JI
227
228 return new_raw_count;
229}
230
231static void
a4eaf7f1 232armpmu_read(struct perf_event *event)
1b8873a0 233{
1b8873a0 234 struct hw_perf_event *hwc = &event->hw;
1b8873a0 235
a4eaf7f1
PZ
236 /* Don't read disabled counters! */
237 if (hwc->idx < 0)
238 return;
1b8873a0 239
a4eaf7f1 240 armpmu_event_update(event, hwc, hwc->idx);
1b8873a0
JI
241}
242
243static void
a4eaf7f1 244armpmu_stop(struct perf_event *event, int flags)
1b8873a0
JI
245{
246 struct hw_perf_event *hwc = &event->hw;
247
a4eaf7f1 248 if (!armpmu)
1b8873a0
JI
249 return;
250
a4eaf7f1
PZ
251 /*
252 * ARM pmu always has to update the counter, so ignore
253 * PERF_EF_UPDATE, see comments in armpmu_start().
254 */
255 if (!(hwc->state & PERF_HES_STOPPED)) {
256 armpmu->disable(hwc, hwc->idx);
257 barrier(); /* why? */
258 armpmu_event_update(event, hwc, hwc->idx);
259 hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
260 }
1b8873a0
JI
261}
262
263static void
a4eaf7f1 264armpmu_start(struct perf_event *event, int flags)
1b8873a0
JI
265{
266 struct hw_perf_event *hwc = &event->hw;
267
a4eaf7f1
PZ
268 if (!armpmu)
269 return;
270
271 /*
272 * ARM pmu always has to reprogram the period, so ignore
273 * PERF_EF_RELOAD, see the comment below.
274 */
275 if (flags & PERF_EF_RELOAD)
276 WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
277
278 hwc->state = 0;
1b8873a0
JI
279 /*
280 * Set the period again. Some counters can't be stopped, so when we
a4eaf7f1 281 * were stopped we simply disabled the IRQ source and the counter
1b8873a0
JI
282 * may have been left counting. If we don't do this step then we may
283 * get an interrupt too soon or *way* too late if the overflow has
284 * happened since disabling.
285 */
286 armpmu_event_set_period(event, hwc, hwc->idx);
287 armpmu->enable(hwc, hwc->idx);
288}
289
a4eaf7f1
PZ
290static void
291armpmu_del(struct perf_event *event, int flags)
292{
293 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
294 struct hw_perf_event *hwc = &event->hw;
295 int idx = hwc->idx;
296
297 WARN_ON(idx < 0);
298
299 clear_bit(idx, cpuc->active_mask);
300 armpmu_stop(event, PERF_EF_UPDATE);
301 cpuc->events[idx] = NULL;
302 clear_bit(idx, cpuc->used_mask);
303
304 perf_event_update_userpage(event);
305}
306
1b8873a0 307static int
a4eaf7f1 308armpmu_add(struct perf_event *event, int flags)
1b8873a0
JI
309{
310 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
311 struct hw_perf_event *hwc = &event->hw;
312 int idx;
313 int err = 0;
314
33696fc0 315 perf_pmu_disable(event->pmu);
24cd7f54 316
1b8873a0
JI
317 /* If we don't have a space for the counter then finish early. */
318 idx = armpmu->get_event_idx(cpuc, hwc);
319 if (idx < 0) {
320 err = idx;
321 goto out;
322 }
323
324 /*
325 * If there is an event in the counter we are going to use then make
326 * sure it is disabled.
327 */
328 event->hw.idx = idx;
329 armpmu->disable(hwc, idx);
330 cpuc->events[idx] = event;
331 set_bit(idx, cpuc->active_mask);
332
a4eaf7f1
PZ
333 hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
334 if (flags & PERF_EF_START)
335 armpmu_start(event, PERF_EF_RELOAD);
1b8873a0
JI
336
337 /* Propagate our changes to the userspace mapping. */
338 perf_event_update_userpage(event);
339
340out:
33696fc0 341 perf_pmu_enable(event->pmu);
1b8873a0
JI
342 return err;
343}
344
b0a873eb 345static struct pmu pmu;
1b8873a0
JI
346
347static int
348validate_event(struct cpu_hw_events *cpuc,
349 struct perf_event *event)
350{
351 struct hw_perf_event fake_event = event->hw;
352
65b4711f
WD
353 if (event->pmu != &pmu || event->state <= PERF_EVENT_STATE_OFF)
354 return 1;
1b8873a0
JI
355
356 return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
357}
358
359static int
360validate_group(struct perf_event *event)
361{
362 struct perf_event *sibling, *leader = event->group_leader;
363 struct cpu_hw_events fake_pmu;
364
365 memset(&fake_pmu, 0, sizeof(fake_pmu));
366
367 if (!validate_event(&fake_pmu, leader))
368 return -ENOSPC;
369
370 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
371 if (!validate_event(&fake_pmu, sibling))
372 return -ENOSPC;
373 }
374
375 if (!validate_event(&fake_pmu, event))
376 return -ENOSPC;
377
378 return 0;
379}
380
0e25a5c9
RV
381static irqreturn_t armpmu_platform_irq(int irq, void *dev)
382{
383 struct arm_pmu_platdata *plat = dev_get_platdata(&pmu_device->dev);
384
385 return plat->handle_irq(irq, dev, armpmu->handle_irq);
386}
387
1b8873a0
JI
388static int
389armpmu_reserve_hardware(void)
390{
0e25a5c9
RV
391 struct arm_pmu_platdata *plat;
392 irq_handler_t handle_irq;
49c006b9 393 int i, err = -ENODEV, irq;
1b8873a0 394
49c006b9
WD
395 pmu_device = reserve_pmu(ARM_PMU_DEVICE_CPU);
396 if (IS_ERR(pmu_device)) {
1b8873a0 397 pr_warning("unable to reserve pmu\n");
49c006b9 398 return PTR_ERR(pmu_device);
1b8873a0
JI
399 }
400
49c006b9 401 init_pmu(ARM_PMU_DEVICE_CPU);
1b8873a0 402
0e25a5c9
RV
403 plat = dev_get_platdata(&pmu_device->dev);
404 if (plat && plat->handle_irq)
405 handle_irq = armpmu_platform_irq;
406 else
407 handle_irq = armpmu->handle_irq;
408
49c006b9 409 if (pmu_device->num_resources < 1) {
1b8873a0
JI
410 pr_err("no irqs for PMUs defined\n");
411 return -ENODEV;
412 }
413
49c006b9
WD
414 for (i = 0; i < pmu_device->num_resources; ++i) {
415 irq = platform_get_irq(pmu_device, i);
416 if (irq < 0)
417 continue;
418
0e25a5c9 419 err = request_irq(irq, handle_irq,
ddee87f2
WD
420 IRQF_DISABLED | IRQF_NOBALANCING,
421 "armpmu", NULL);
1b8873a0 422 if (err) {
49c006b9
WD
423 pr_warning("unable to request IRQ%d for ARM perf "
424 "counters\n", irq);
1b8873a0
JI
425 break;
426 }
427 }
428
429 if (err) {
49c006b9
WD
430 for (i = i - 1; i >= 0; --i) {
431 irq = platform_get_irq(pmu_device, i);
432 if (irq >= 0)
433 free_irq(irq, NULL);
434 }
435 release_pmu(pmu_device);
436 pmu_device = NULL;
1b8873a0
JI
437 }
438
439 return err;
440}
441
442static void
443armpmu_release_hardware(void)
444{
49c006b9 445 int i, irq;
1b8873a0 446
49c006b9
WD
447 for (i = pmu_device->num_resources - 1; i >= 0; --i) {
448 irq = platform_get_irq(pmu_device, i);
449 if (irq >= 0)
450 free_irq(irq, NULL);
451 }
1b8873a0
JI
452 armpmu->stop();
453
49c006b9
WD
454 release_pmu(pmu_device);
455 pmu_device = NULL;
1b8873a0
JI
456}
457
458static atomic_t active_events = ATOMIC_INIT(0);
459static DEFINE_MUTEX(pmu_reserve_mutex);
460
461static void
462hw_perf_event_destroy(struct perf_event *event)
463{
464 if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
465 armpmu_release_hardware();
466 mutex_unlock(&pmu_reserve_mutex);
467 }
468}
469
470static int
471__hw_perf_event_init(struct perf_event *event)
472{
473 struct hw_perf_event *hwc = &event->hw;
474 int mapping, err;
475
476 /* Decode the generic type into an ARM event identifier. */
477 if (PERF_TYPE_HARDWARE == event->attr.type) {
84fee97a 478 mapping = armpmu_map_event(event->attr.config);
1b8873a0
JI
479 } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
480 mapping = armpmu_map_cache_event(event->attr.config);
481 } else if (PERF_TYPE_RAW == event->attr.type) {
84fee97a 482 mapping = armpmu_map_raw_event(event->attr.config);
1b8873a0
JI
483 } else {
484 pr_debug("event type %x not supported\n", event->attr.type);
485 return -EOPNOTSUPP;
486 }
487
488 if (mapping < 0) {
489 pr_debug("event %x:%llx not supported\n", event->attr.type,
490 event->attr.config);
491 return mapping;
492 }
493
494 /*
495 * Check whether we need to exclude the counter from certain modes.
496 * The ARM performance counters are on all of the time so if someone
497 * has asked us for some excludes then we have to fail.
498 */
499 if (event->attr.exclude_kernel || event->attr.exclude_user ||
500 event->attr.exclude_hv || event->attr.exclude_idle) {
501 pr_debug("ARM performance counters do not support "
502 "mode exclusion\n");
503 return -EPERM;
504 }
505
506 /*
507 * We don't assign an index until we actually place the event onto
508 * hardware. Use -1 to signify that we haven't decided where to put it
509 * yet. For SMP systems, each core has it's own PMU so we can't do any
510 * clever allocation or constraints checking at this point.
511 */
512 hwc->idx = -1;
513
514 /*
515 * Store the event encoding into the config_base field. config and
516 * event_base are unused as the only 2 things we need to know are
517 * the event mapping and the counter to use. The counter to use is
518 * also the indx and the config_base is the event type.
519 */
520 hwc->config_base = (unsigned long)mapping;
521 hwc->config = 0;
522 hwc->event_base = 0;
523
524 if (!hwc->sample_period) {
525 hwc->sample_period = armpmu->max_period;
526 hwc->last_period = hwc->sample_period;
e7850595 527 local64_set(&hwc->period_left, hwc->sample_period);
1b8873a0
JI
528 }
529
530 err = 0;
531 if (event->group_leader != event) {
532 err = validate_group(event);
533 if (err)
534 return -EINVAL;
535 }
536
537 return err;
538}
539
b0a873eb 540static int armpmu_event_init(struct perf_event *event)
1b8873a0
JI
541{
542 int err = 0;
543
b0a873eb
PZ
544 switch (event->attr.type) {
545 case PERF_TYPE_RAW:
546 case PERF_TYPE_HARDWARE:
547 case PERF_TYPE_HW_CACHE:
548 break;
549
550 default:
551 return -ENOENT;
552 }
553
1b8873a0 554 if (!armpmu)
b0a873eb 555 return -ENODEV;
1b8873a0
JI
556
557 event->destroy = hw_perf_event_destroy;
558
559 if (!atomic_inc_not_zero(&active_events)) {
1efeb08d 560 if (atomic_read(&active_events) > armpmu->num_events) {
1b8873a0 561 atomic_dec(&active_events);
b0a873eb 562 return -ENOSPC;
1b8873a0
JI
563 }
564
565 mutex_lock(&pmu_reserve_mutex);
566 if (atomic_read(&active_events) == 0) {
567 err = armpmu_reserve_hardware();
568 }
569
570 if (!err)
571 atomic_inc(&active_events);
572 mutex_unlock(&pmu_reserve_mutex);
573 }
574
575 if (err)
b0a873eb 576 return err;
1b8873a0
JI
577
578 err = __hw_perf_event_init(event);
579 if (err)
580 hw_perf_event_destroy(event);
581
b0a873eb 582 return err;
1b8873a0
JI
583}
584
a4eaf7f1 585static void armpmu_enable(struct pmu *pmu)
1b8873a0
JI
586{
587 /* Enable all of the perf events on hardware. */
588 int idx;
589 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
590
591 if (!armpmu)
592 return;
593
594 for (idx = 0; idx <= armpmu->num_events; ++idx) {
595 struct perf_event *event = cpuc->events[idx];
596
597 if (!event)
598 continue;
599
600 armpmu->enable(&event->hw, idx);
601 }
602
603 armpmu->start();
604}
605
a4eaf7f1 606static void armpmu_disable(struct pmu *pmu)
1b8873a0
JI
607{
608 if (armpmu)
609 armpmu->stop();
610}
611
33696fc0 612static struct pmu pmu = {
a4eaf7f1
PZ
613 .pmu_enable = armpmu_enable,
614 .pmu_disable = armpmu_disable,
615 .event_init = armpmu_event_init,
616 .add = armpmu_add,
617 .del = armpmu_del,
618 .start = armpmu_start,
619 .stop = armpmu_stop,
620 .read = armpmu_read,
33696fc0
PZ
621};
622
43eab878
WD
623/* Include the PMU-specific implementations. */
624#include "perf_event_xscale.c"
625#include "perf_event_v6.c"
626#include "perf_event_v7.c"
49e6a32f 627
574b69cb
WD
628/*
629 * Ensure the PMU has sane values out of reset.
630 * This requires SMP to be available, so exists as a separate initcall.
631 */
632static int __init
633armpmu_reset(void)
634{
635 if (armpmu && armpmu->reset)
636 return on_each_cpu(armpmu->reset, NULL, 1);
637 return 0;
638}
639arch_initcall(armpmu_reset);
640
1b8873a0
JI
641static int __init
642init_hw_perf_events(void)
643{
644 unsigned long cpuid = read_cpuid_id();
645 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
646 unsigned long part_number = (cpuid & 0xFFF0);
647
49e6a32f 648 /* ARM Ltd CPUs. */
1b8873a0
JI
649 if (0x41 == implementor) {
650 switch (part_number) {
651 case 0xB360: /* ARM1136 */
652 case 0xB560: /* ARM1156 */
653 case 0xB760: /* ARM1176 */
3cb314ba 654 armpmu = armv6pmu_init();
1b8873a0
JI
655 break;
656 case 0xB020: /* ARM11mpcore */
3cb314ba 657 armpmu = armv6mpcore_pmu_init();
1b8873a0 658 break;
796d1295 659 case 0xC080: /* Cortex-A8 */
3cb314ba 660 armpmu = armv7_a8_pmu_init();
796d1295
JP
661 break;
662 case 0xC090: /* Cortex-A9 */
3cb314ba 663 armpmu = armv7_a9_pmu_init();
796d1295 664 break;
49e6a32f
WD
665 }
666 /* Intel CPUs [xscale]. */
667 } else if (0x69 == implementor) {
668 part_number = (cpuid >> 13) & 0x7;
669 switch (part_number) {
670 case 1:
3cb314ba 671 armpmu = xscale1pmu_init();
49e6a32f
WD
672 break;
673 case 2:
3cb314ba 674 armpmu = xscale2pmu_init();
49e6a32f 675 break;
1b8873a0
JI
676 }
677 }
678
49e6a32f 679 if (armpmu) {
796d1295 680 pr_info("enabled with %s PMU driver, %d counters available\n",
62994831 681 armpmu->name, armpmu->num_events);
49e6a32f
WD
682 } else {
683 pr_info("no hardware support available\n");
49e6a32f 684 }
1b8873a0 685
2e80a82a 686 perf_pmu_register(&pmu, "cpu", PERF_TYPE_RAW);
b0a873eb 687
1b8873a0
JI
688 return 0;
689}
004417a6 690early_initcall(init_hw_perf_events);
1b8873a0
JI
691
692/*
693 * Callchain handling code.
694 */
1b8873a0
JI
695
696/*
697 * The registers we're interested in are at the end of the variable
698 * length saved register structure. The fp points at the end of this
699 * structure so the address of this struct is:
700 * (struct frame_tail *)(xxx->fp)-1
701 *
702 * This code has been adapted from the ARM OProfile support.
703 */
704struct frame_tail {
4d6b7a77
WD
705 struct frame_tail __user *fp;
706 unsigned long sp;
707 unsigned long lr;
1b8873a0
JI
708} __attribute__((packed));
709
710/*
711 * Get the return address for a single stackframe and return a pointer to the
712 * next frame tail.
713 */
4d6b7a77
WD
714static struct frame_tail __user *
715user_backtrace(struct frame_tail __user *tail,
1b8873a0
JI
716 struct perf_callchain_entry *entry)
717{
718 struct frame_tail buftail;
719
720 /* Also check accessibility of one struct frame_tail beyond */
721 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
722 return NULL;
723 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
724 return NULL;
725
70791ce9 726 perf_callchain_store(entry, buftail.lr);
1b8873a0
JI
727
728 /*
729 * Frame pointers should strictly progress back up the stack
730 * (towards higher addresses).
731 */
cb06199b 732 if (tail + 1 >= buftail.fp)
1b8873a0
JI
733 return NULL;
734
735 return buftail.fp - 1;
736}
737
56962b44
FW
738void
739perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
1b8873a0 740{
4d6b7a77 741 struct frame_tail __user *tail;
1b8873a0 742
1b8873a0 743
4d6b7a77 744 tail = (struct frame_tail __user *)regs->ARM_fp - 1;
1b8873a0
JI
745
746 while (tail && !((unsigned long)tail & 0x3))
747 tail = user_backtrace(tail, entry);
748}
749
750/*
751 * Gets called by walk_stackframe() for every stackframe. This will be called
752 * whist unwinding the stackframe and is like a subroutine return so we use
753 * the PC.
754 */
755static int
756callchain_trace(struct stackframe *fr,
757 void *data)
758{
759 struct perf_callchain_entry *entry = data;
70791ce9 760 perf_callchain_store(entry, fr->pc);
1b8873a0
JI
761 return 0;
762}
763
56962b44
FW
764void
765perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
1b8873a0
JI
766{
767 struct stackframe fr;
768
1b8873a0
JI
769 fr.fp = regs->ARM_fp;
770 fr.sp = regs->ARM_sp;
771 fr.lr = regs->ARM_lr;
772 fr.pc = regs->ARM_pc;
773 walk_stackframe(&fr, callchain_trace, entry);
774}