4 * ARM performance counter support.
6 * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
8 * ARMv7 support: Jean Pihet <jpihet@mvista.com>
9 * 2010 (c) MontaVista Software, LLC.
11 * This code is based on the sparc64 perf event code, which is in turn based
12 * on the x86 code. Callchain code is based on the ARM OProfile backtrace
15 #define pr_fmt(fmt) "hw perfevents: " fmt
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/perf_event.h>
21 #include <linux/platform_device.h>
22 #include <linux/spinlock.h>
23 #include <linux/uaccess.h>
25 #include <asm/cputype.h>
27 #include <asm/irq_regs.h>
29 #include <asm/stacktrace.h>
31 static struct platform_device
*pmu_device
;
34 * Hardware lock to serialize accesses to PMU registers. Needed for the
35 * read/modify/write sequences.
37 DEFINE_SPINLOCK(pmu_lock
);
40 * ARMv6 supports a maximum of 3 events, starting from index 1. If we add
41 * another platform that supports more, we need to increase this to be the
42 * largest of all platforms.
44 * ARMv7 supports up to 32 events:
45 * cycle counter CCNT + 31 events counters CNT0..30.
46 * Cortex-A8 has 1+4 counters, Cortex-A9 has 1+6 counters.
48 #define ARMPMU_MAX_HWEVENTS 33
50 /* The events for a given CPU. */
51 struct cpu_hw_events
{
53 * The events that are active on the CPU for the given index. Index 0
56 struct perf_event
*events
[ARMPMU_MAX_HWEVENTS
];
59 * A 1 bit for an index indicates that the counter is being used for
60 * an event. A 0 means that the counter can be used.
62 unsigned long used_mask
[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS
)];
65 * A 1 bit for an index indicates that the counter is actively being
68 unsigned long active_mask
[BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS
)];
70 DEFINE_PER_CPU(struct cpu_hw_events
, cpu_hw_events
);
73 static const char *arm_pmu_names
[] = {
74 [ARM_PERF_PMU_ID_XSCALE1
] = "xscale1",
75 [ARM_PERF_PMU_ID_XSCALE2
] = "xscale2",
76 [ARM_PERF_PMU_ID_V6
] = "v6",
77 [ARM_PERF_PMU_ID_V6MP
] = "v6mpcore",
78 [ARM_PERF_PMU_ID_CA8
] = "ARMv7 Cortex-A8",
79 [ARM_PERF_PMU_ID_CA9
] = "ARMv7 Cortex-A9",
83 enum arm_perf_pmu_ids id
;
84 irqreturn_t (*handle_irq
)(int irq_num
, void *dev
);
85 void (*enable
)(struct hw_perf_event
*evt
, int idx
);
86 void (*disable
)(struct hw_perf_event
*evt
, int idx
);
87 int (*get_event_idx
)(struct cpu_hw_events
*cpuc
,
88 struct hw_perf_event
*hwc
);
89 u32 (*read_counter
)(int idx
);
90 void (*write_counter
)(int idx
, u32 val
);
93 const unsigned (*cache_map
)[PERF_COUNT_HW_CACHE_MAX
]
94 [PERF_COUNT_HW_CACHE_OP_MAX
]
95 [PERF_COUNT_HW_CACHE_RESULT_MAX
];
96 const unsigned (*event_map
)[PERF_COUNT_HW_MAX
];
102 /* Set at runtime when we know what CPU type we are. */
103 static const struct arm_pmu
*armpmu
;
105 enum arm_perf_pmu_ids
106 armpmu_get_pmu_id(void)
115 EXPORT_SYMBOL_GPL(armpmu_get_pmu_id
);
118 armpmu_get_max_events(void)
123 max_events
= armpmu
->num_events
;
127 EXPORT_SYMBOL_GPL(armpmu_get_max_events
);
129 int perf_num_counters(void)
131 return armpmu_get_max_events();
133 EXPORT_SYMBOL_GPL(perf_num_counters
);
135 #define HW_OP_UNSUPPORTED 0xFFFF
138 PERF_COUNT_HW_CACHE_##_x
140 #define CACHE_OP_UNSUPPORTED 0xFFFF
143 armpmu_map_cache_event(u64 config
)
145 unsigned int cache_type
, cache_op
, cache_result
, ret
;
147 cache_type
= (config
>> 0) & 0xff;
148 if (cache_type
>= PERF_COUNT_HW_CACHE_MAX
)
151 cache_op
= (config
>> 8) & 0xff;
152 if (cache_op
>= PERF_COUNT_HW_CACHE_OP_MAX
)
155 cache_result
= (config
>> 16) & 0xff;
156 if (cache_result
>= PERF_COUNT_HW_CACHE_RESULT_MAX
)
159 ret
= (int)(*armpmu
->cache_map
)[cache_type
][cache_op
][cache_result
];
161 if (ret
== CACHE_OP_UNSUPPORTED
)
168 armpmu_map_event(u64 config
)
170 int mapping
= (*armpmu
->event_map
)[config
];
171 return mapping
== HW_OP_UNSUPPORTED
? -EOPNOTSUPP
: mapping
;
175 armpmu_map_raw_event(u64 config
)
177 return (int)(config
& armpmu
->raw_event_mask
);
181 armpmu_event_set_period(struct perf_event
*event
,
182 struct hw_perf_event
*hwc
,
185 s64 left
= local64_read(&hwc
->period_left
);
186 s64 period
= hwc
->sample_period
;
189 if (unlikely(left
<= -period
)) {
191 local64_set(&hwc
->period_left
, left
);
192 hwc
->last_period
= period
;
196 if (unlikely(left
<= 0)) {
198 local64_set(&hwc
->period_left
, left
);
199 hwc
->last_period
= period
;
203 if (left
> (s64
)armpmu
->max_period
)
204 left
= armpmu
->max_period
;
206 local64_set(&hwc
->prev_count
, (u64
)-left
);
208 armpmu
->write_counter(idx
, (u64
)(-left
) & 0xffffffff);
210 perf_event_update_userpage(event
);
216 armpmu_event_update(struct perf_event
*event
,
217 struct hw_perf_event
*hwc
,
221 s64 prev_raw_count
, new_raw_count
;
225 prev_raw_count
= local64_read(&hwc
->prev_count
);
226 new_raw_count
= armpmu
->read_counter(idx
);
228 if (local64_cmpxchg(&hwc
->prev_count
, prev_raw_count
,
229 new_raw_count
) != prev_raw_count
)
232 delta
= (new_raw_count
<< shift
) - (prev_raw_count
<< shift
);
235 local64_add(delta
, &event
->count
);
236 local64_sub(delta
, &hwc
->period_left
);
238 return new_raw_count
;
242 armpmu_read(struct perf_event
*event
)
244 struct hw_perf_event
*hwc
= &event
->hw
;
246 /* Don't read disabled counters! */
250 armpmu_event_update(event
, hwc
, hwc
->idx
);
254 armpmu_stop(struct perf_event
*event
, int flags
)
256 struct hw_perf_event
*hwc
= &event
->hw
;
262 * ARM pmu always has to update the counter, so ignore
263 * PERF_EF_UPDATE, see comments in armpmu_start().
265 if (!(hwc
->state
& PERF_HES_STOPPED
)) {
266 armpmu
->disable(hwc
, hwc
->idx
);
267 barrier(); /* why? */
268 armpmu_event_update(event
, hwc
, hwc
->idx
);
269 hwc
->state
|= PERF_HES_STOPPED
| PERF_HES_UPTODATE
;
274 armpmu_start(struct perf_event
*event
, int flags
)
276 struct hw_perf_event
*hwc
= &event
->hw
;
282 * ARM pmu always has to reprogram the period, so ignore
283 * PERF_EF_RELOAD, see the comment below.
285 if (flags
& PERF_EF_RELOAD
)
286 WARN_ON_ONCE(!(hwc
->state
& PERF_HES_UPTODATE
));
290 * Set the period again. Some counters can't be stopped, so when we
291 * were stopped we simply disabled the IRQ source and the counter
292 * may have been left counting. If we don't do this step then we may
293 * get an interrupt too soon or *way* too late if the overflow has
294 * happened since disabling.
296 armpmu_event_set_period(event
, hwc
, hwc
->idx
);
297 armpmu
->enable(hwc
, hwc
->idx
);
301 armpmu_del(struct perf_event
*event
, int flags
)
303 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
304 struct hw_perf_event
*hwc
= &event
->hw
;
309 clear_bit(idx
, cpuc
->active_mask
);
310 armpmu_stop(event
, PERF_EF_UPDATE
);
311 cpuc
->events
[idx
] = NULL
;
312 clear_bit(idx
, cpuc
->used_mask
);
314 perf_event_update_userpage(event
);
318 armpmu_add(struct perf_event
*event
, int flags
)
320 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
321 struct hw_perf_event
*hwc
= &event
->hw
;
325 perf_pmu_disable(event
->pmu
);
327 /* If we don't have a space for the counter then finish early. */
328 idx
= armpmu
->get_event_idx(cpuc
, hwc
);
335 * If there is an event in the counter we are going to use then make
336 * sure it is disabled.
339 armpmu
->disable(hwc
, idx
);
340 cpuc
->events
[idx
] = event
;
341 set_bit(idx
, cpuc
->active_mask
);
343 hwc
->state
= PERF_HES_STOPPED
| PERF_HES_UPTODATE
;
344 if (flags
& PERF_EF_START
)
345 armpmu_start(event
, PERF_EF_RELOAD
);
347 /* Propagate our changes to the userspace mapping. */
348 perf_event_update_userpage(event
);
351 perf_pmu_enable(event
->pmu
);
355 static struct pmu pmu
;
358 validate_event(struct cpu_hw_events
*cpuc
,
359 struct perf_event
*event
)
361 struct hw_perf_event fake_event
= event
->hw
;
363 if (event
->pmu
!= &pmu
|| event
->state
<= PERF_EVENT_STATE_OFF
)
366 return armpmu
->get_event_idx(cpuc
, &fake_event
) >= 0;
370 validate_group(struct perf_event
*event
)
372 struct perf_event
*sibling
, *leader
= event
->group_leader
;
373 struct cpu_hw_events fake_pmu
;
375 memset(&fake_pmu
, 0, sizeof(fake_pmu
));
377 if (!validate_event(&fake_pmu
, leader
))
380 list_for_each_entry(sibling
, &leader
->sibling_list
, group_entry
) {
381 if (!validate_event(&fake_pmu
, sibling
))
385 if (!validate_event(&fake_pmu
, event
))
392 armpmu_reserve_hardware(void)
394 int i
, err
= -ENODEV
, irq
;
396 pmu_device
= reserve_pmu(ARM_PMU_DEVICE_CPU
);
397 if (IS_ERR(pmu_device
)) {
398 pr_warning("unable to reserve pmu\n");
399 return PTR_ERR(pmu_device
);
402 init_pmu(ARM_PMU_DEVICE_CPU
);
404 if (pmu_device
->num_resources
< 1) {
405 pr_err("no irqs for PMUs defined\n");
409 for (i
= 0; i
< pmu_device
->num_resources
; ++i
) {
410 irq
= platform_get_irq(pmu_device
, i
);
414 err
= request_irq(irq
, armpmu
->handle_irq
,
415 IRQF_DISABLED
| IRQF_NOBALANCING
,
418 pr_warning("unable to request IRQ%d for ARM perf "
425 for (i
= i
- 1; i
>= 0; --i
) {
426 irq
= platform_get_irq(pmu_device
, i
);
430 release_pmu(pmu_device
);
438 armpmu_release_hardware(void)
442 for (i
= pmu_device
->num_resources
- 1; i
>= 0; --i
) {
443 irq
= platform_get_irq(pmu_device
, i
);
449 release_pmu(pmu_device
);
453 static atomic_t active_events
= ATOMIC_INIT(0);
454 static DEFINE_MUTEX(pmu_reserve_mutex
);
457 hw_perf_event_destroy(struct perf_event
*event
)
459 if (atomic_dec_and_mutex_lock(&active_events
, &pmu_reserve_mutex
)) {
460 armpmu_release_hardware();
461 mutex_unlock(&pmu_reserve_mutex
);
466 __hw_perf_event_init(struct perf_event
*event
)
468 struct hw_perf_event
*hwc
= &event
->hw
;
471 /* Decode the generic type into an ARM event identifier. */
472 if (PERF_TYPE_HARDWARE
== event
->attr
.type
) {
473 mapping
= armpmu_map_event(event
->attr
.config
);
474 } else if (PERF_TYPE_HW_CACHE
== event
->attr
.type
) {
475 mapping
= armpmu_map_cache_event(event
->attr
.config
);
476 } else if (PERF_TYPE_RAW
== event
->attr
.type
) {
477 mapping
= armpmu_map_raw_event(event
->attr
.config
);
479 pr_debug("event type %x not supported\n", event
->attr
.type
);
484 pr_debug("event %x:%llx not supported\n", event
->attr
.type
,
490 * Check whether we need to exclude the counter from certain modes.
491 * The ARM performance counters are on all of the time so if someone
492 * has asked us for some excludes then we have to fail.
494 if (event
->attr
.exclude_kernel
|| event
->attr
.exclude_user
||
495 event
->attr
.exclude_hv
|| event
->attr
.exclude_idle
) {
496 pr_debug("ARM performance counters do not support "
502 * We don't assign an index until we actually place the event onto
503 * hardware. Use -1 to signify that we haven't decided where to put it
504 * yet. For SMP systems, each core has it's own PMU so we can't do any
505 * clever allocation or constraints checking at this point.
510 * Store the event encoding into the config_base field. config and
511 * event_base are unused as the only 2 things we need to know are
512 * the event mapping and the counter to use. The counter to use is
513 * also the indx and the config_base is the event type.
515 hwc
->config_base
= (unsigned long)mapping
;
519 if (!hwc
->sample_period
) {
520 hwc
->sample_period
= armpmu
->max_period
;
521 hwc
->last_period
= hwc
->sample_period
;
522 local64_set(&hwc
->period_left
, hwc
->sample_period
);
526 if (event
->group_leader
!= event
) {
527 err
= validate_group(event
);
535 static int armpmu_event_init(struct perf_event
*event
)
539 switch (event
->attr
.type
) {
541 case PERF_TYPE_HARDWARE
:
542 case PERF_TYPE_HW_CACHE
:
552 event
->destroy
= hw_perf_event_destroy
;
554 if (!atomic_inc_not_zero(&active_events
)) {
555 if (atomic_read(&active_events
) > armpmu
->num_events
) {
556 atomic_dec(&active_events
);
560 mutex_lock(&pmu_reserve_mutex
);
561 if (atomic_read(&active_events
) == 0) {
562 err
= armpmu_reserve_hardware();
566 atomic_inc(&active_events
);
567 mutex_unlock(&pmu_reserve_mutex
);
573 err
= __hw_perf_event_init(event
);
575 hw_perf_event_destroy(event
);
580 static void armpmu_enable(struct pmu
*pmu
)
582 /* Enable all of the perf events on hardware. */
584 struct cpu_hw_events
*cpuc
= &__get_cpu_var(cpu_hw_events
);
589 for (idx
= 0; idx
<= armpmu
->num_events
; ++idx
) {
590 struct perf_event
*event
= cpuc
->events
[idx
];
595 armpmu
->enable(&event
->hw
, idx
);
601 static void armpmu_disable(struct pmu
*pmu
)
607 static struct pmu pmu
= {
608 .pmu_enable
= armpmu_enable
,
609 .pmu_disable
= armpmu_disable
,
610 .event_init
= armpmu_event_init
,
613 .start
= armpmu_start
,
619 * ARMv6 Performance counter handling code.
621 * ARMv6 has 2 configurable performance counters and a single cycle counter.
622 * They all share a single reset bit but can be written to zero so we can use
625 * The counters can't be individually enabled or disabled so when we remove
626 * one event and replace it with another we could get spurious counts from the
627 * wrong event. However, we can take advantage of the fact that the
628 * performance counters can export events to the event bus, and the event bus
629 * itself can be monitored. This requires that we *don't* export the events to
630 * the event bus. The procedure for disabling a configurable counter is:
631 * - change the counter to count the ETMEXTOUT[0] signal (0x20). This
632 * effectively stops the counter from counting.
633 * - disable the counter's interrupt generation (each counter has it's
634 * own interrupt enable bit).
635 * Once stopped, the counter value can be written as 0 to reset.
637 * To enable a counter:
638 * - enable the counter's interrupt generation.
639 * - set the new event type.
641 * Note: the dedicated cycle counter only counts cycles and can't be
642 * enabled/disabled independently of the others. When we want to disable the
643 * cycle counter, we have to just disable the interrupt reporting and start
644 * ignoring that counter. When re-enabling, we have to reset the value and
645 * enable the interrupt.
648 enum armv6_perf_types
{
649 ARMV6_PERFCTR_ICACHE_MISS
= 0x0,
650 ARMV6_PERFCTR_IBUF_STALL
= 0x1,
651 ARMV6_PERFCTR_DDEP_STALL
= 0x2,
652 ARMV6_PERFCTR_ITLB_MISS
= 0x3,
653 ARMV6_PERFCTR_DTLB_MISS
= 0x4,
654 ARMV6_PERFCTR_BR_EXEC
= 0x5,
655 ARMV6_PERFCTR_BR_MISPREDICT
= 0x6,
656 ARMV6_PERFCTR_INSTR_EXEC
= 0x7,
657 ARMV6_PERFCTR_DCACHE_HIT
= 0x9,
658 ARMV6_PERFCTR_DCACHE_ACCESS
= 0xA,
659 ARMV6_PERFCTR_DCACHE_MISS
= 0xB,
660 ARMV6_PERFCTR_DCACHE_WBACK
= 0xC,
661 ARMV6_PERFCTR_SW_PC_CHANGE
= 0xD,
662 ARMV6_PERFCTR_MAIN_TLB_MISS
= 0xF,
663 ARMV6_PERFCTR_EXPL_D_ACCESS
= 0x10,
664 ARMV6_PERFCTR_LSU_FULL_STALL
= 0x11,
665 ARMV6_PERFCTR_WBUF_DRAINED
= 0x12,
666 ARMV6_PERFCTR_CPU_CYCLES
= 0xFF,
667 ARMV6_PERFCTR_NOP
= 0x20,
670 enum armv6_counters
{
671 ARMV6_CYCLE_COUNTER
= 1,
677 * The hardware events that we support. We do support cache operations but
678 * we have harvard caches and no way to combine instruction and data
679 * accesses/misses in hardware.
681 static const unsigned armv6_perf_map
[PERF_COUNT_HW_MAX
] = {
682 [PERF_COUNT_HW_CPU_CYCLES
] = ARMV6_PERFCTR_CPU_CYCLES
,
683 [PERF_COUNT_HW_INSTRUCTIONS
] = ARMV6_PERFCTR_INSTR_EXEC
,
684 [PERF_COUNT_HW_CACHE_REFERENCES
] = HW_OP_UNSUPPORTED
,
685 [PERF_COUNT_HW_CACHE_MISSES
] = HW_OP_UNSUPPORTED
,
686 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = ARMV6_PERFCTR_BR_EXEC
,
687 [PERF_COUNT_HW_BRANCH_MISSES
] = ARMV6_PERFCTR_BR_MISPREDICT
,
688 [PERF_COUNT_HW_BUS_CYCLES
] = HW_OP_UNSUPPORTED
,
691 static const unsigned armv6_perf_cache_map
[PERF_COUNT_HW_CACHE_MAX
]
692 [PERF_COUNT_HW_CACHE_OP_MAX
]
693 [PERF_COUNT_HW_CACHE_RESULT_MAX
] = {
696 * The performance counters don't differentiate between read
697 * and write accesses/misses so this isn't strictly correct,
698 * but it's the best we can do. Writes and reads get
702 [C(RESULT_ACCESS
)] = ARMV6_PERFCTR_DCACHE_ACCESS
,
703 [C(RESULT_MISS
)] = ARMV6_PERFCTR_DCACHE_MISS
,
706 [C(RESULT_ACCESS
)] = ARMV6_PERFCTR_DCACHE_ACCESS
,
707 [C(RESULT_MISS
)] = ARMV6_PERFCTR_DCACHE_MISS
,
710 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
711 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
716 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
717 [C(RESULT_MISS
)] = ARMV6_PERFCTR_ICACHE_MISS
,
720 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
721 [C(RESULT_MISS
)] = ARMV6_PERFCTR_ICACHE_MISS
,
724 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
725 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
730 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
731 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
734 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
735 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
738 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
739 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
744 * The ARM performance counters can count micro DTLB misses,
745 * micro ITLB misses and main TLB misses. There isn't an event
746 * for TLB misses, so use the micro misses here and if users
747 * want the main TLB misses they can use a raw counter.
750 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
751 [C(RESULT_MISS
)] = ARMV6_PERFCTR_DTLB_MISS
,
754 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
755 [C(RESULT_MISS
)] = ARMV6_PERFCTR_DTLB_MISS
,
758 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
759 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
764 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
765 [C(RESULT_MISS
)] = ARMV6_PERFCTR_ITLB_MISS
,
768 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
769 [C(RESULT_MISS
)] = ARMV6_PERFCTR_ITLB_MISS
,
772 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
773 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
778 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
779 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
782 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
783 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
786 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
787 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
792 enum armv6mpcore_perf_types
{
793 ARMV6MPCORE_PERFCTR_ICACHE_MISS
= 0x0,
794 ARMV6MPCORE_PERFCTR_IBUF_STALL
= 0x1,
795 ARMV6MPCORE_PERFCTR_DDEP_STALL
= 0x2,
796 ARMV6MPCORE_PERFCTR_ITLB_MISS
= 0x3,
797 ARMV6MPCORE_PERFCTR_DTLB_MISS
= 0x4,
798 ARMV6MPCORE_PERFCTR_BR_EXEC
= 0x5,
799 ARMV6MPCORE_PERFCTR_BR_NOTPREDICT
= 0x6,
800 ARMV6MPCORE_PERFCTR_BR_MISPREDICT
= 0x7,
801 ARMV6MPCORE_PERFCTR_INSTR_EXEC
= 0x8,
802 ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS
= 0xA,
803 ARMV6MPCORE_PERFCTR_DCACHE_RDMISS
= 0xB,
804 ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS
= 0xC,
805 ARMV6MPCORE_PERFCTR_DCACHE_WRMISS
= 0xD,
806 ARMV6MPCORE_PERFCTR_DCACHE_EVICTION
= 0xE,
807 ARMV6MPCORE_PERFCTR_SW_PC_CHANGE
= 0xF,
808 ARMV6MPCORE_PERFCTR_MAIN_TLB_MISS
= 0x10,
809 ARMV6MPCORE_PERFCTR_EXPL_MEM_ACCESS
= 0x11,
810 ARMV6MPCORE_PERFCTR_LSU_FULL_STALL
= 0x12,
811 ARMV6MPCORE_PERFCTR_WBUF_DRAINED
= 0x13,
812 ARMV6MPCORE_PERFCTR_CPU_CYCLES
= 0xFF,
816 * The hardware events that we support. We do support cache operations but
817 * we have harvard caches and no way to combine instruction and data
818 * accesses/misses in hardware.
820 static const unsigned armv6mpcore_perf_map
[PERF_COUNT_HW_MAX
] = {
821 [PERF_COUNT_HW_CPU_CYCLES
] = ARMV6MPCORE_PERFCTR_CPU_CYCLES
,
822 [PERF_COUNT_HW_INSTRUCTIONS
] = ARMV6MPCORE_PERFCTR_INSTR_EXEC
,
823 [PERF_COUNT_HW_CACHE_REFERENCES
] = HW_OP_UNSUPPORTED
,
824 [PERF_COUNT_HW_CACHE_MISSES
] = HW_OP_UNSUPPORTED
,
825 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = ARMV6MPCORE_PERFCTR_BR_EXEC
,
826 [PERF_COUNT_HW_BRANCH_MISSES
] = ARMV6MPCORE_PERFCTR_BR_MISPREDICT
,
827 [PERF_COUNT_HW_BUS_CYCLES
] = HW_OP_UNSUPPORTED
,
830 static const unsigned armv6mpcore_perf_cache_map
[PERF_COUNT_HW_CACHE_MAX
]
831 [PERF_COUNT_HW_CACHE_OP_MAX
]
832 [PERF_COUNT_HW_CACHE_RESULT_MAX
] = {
836 ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS
,
838 ARMV6MPCORE_PERFCTR_DCACHE_RDMISS
,
842 ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS
,
844 ARMV6MPCORE_PERFCTR_DCACHE_WRMISS
,
847 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
848 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
853 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
854 [C(RESULT_MISS
)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS
,
857 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
858 [C(RESULT_MISS
)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS
,
861 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
862 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
867 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
868 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
871 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
872 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
875 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
876 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
881 * The ARM performance counters can count micro DTLB misses,
882 * micro ITLB misses and main TLB misses. There isn't an event
883 * for TLB misses, so use the micro misses here and if users
884 * want the main TLB misses they can use a raw counter.
887 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
888 [C(RESULT_MISS
)] = ARMV6MPCORE_PERFCTR_DTLB_MISS
,
891 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
892 [C(RESULT_MISS
)] = ARMV6MPCORE_PERFCTR_DTLB_MISS
,
895 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
896 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
901 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
902 [C(RESULT_MISS
)] = ARMV6MPCORE_PERFCTR_ITLB_MISS
,
905 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
906 [C(RESULT_MISS
)] = ARMV6MPCORE_PERFCTR_ITLB_MISS
,
909 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
910 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
915 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
916 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
919 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
920 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
923 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
924 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
929 static inline unsigned long
930 armv6_pmcr_read(void)
933 asm volatile("mrc p15, 0, %0, c15, c12, 0" : "=r"(val
));
938 armv6_pmcr_write(unsigned long val
)
940 asm volatile("mcr p15, 0, %0, c15, c12, 0" : : "r"(val
));
943 #define ARMV6_PMCR_ENABLE (1 << 0)
944 #define ARMV6_PMCR_CTR01_RESET (1 << 1)
945 #define ARMV6_PMCR_CCOUNT_RESET (1 << 2)
946 #define ARMV6_PMCR_CCOUNT_DIV (1 << 3)
947 #define ARMV6_PMCR_COUNT0_IEN (1 << 4)
948 #define ARMV6_PMCR_COUNT1_IEN (1 << 5)
949 #define ARMV6_PMCR_CCOUNT_IEN (1 << 6)
950 #define ARMV6_PMCR_COUNT0_OVERFLOW (1 << 8)
951 #define ARMV6_PMCR_COUNT1_OVERFLOW (1 << 9)
952 #define ARMV6_PMCR_CCOUNT_OVERFLOW (1 << 10)
953 #define ARMV6_PMCR_EVT_COUNT0_SHIFT 20
954 #define ARMV6_PMCR_EVT_COUNT0_MASK (0xFF << ARMV6_PMCR_EVT_COUNT0_SHIFT)
955 #define ARMV6_PMCR_EVT_COUNT1_SHIFT 12
956 #define ARMV6_PMCR_EVT_COUNT1_MASK (0xFF << ARMV6_PMCR_EVT_COUNT1_SHIFT)
958 #define ARMV6_PMCR_OVERFLOWED_MASK \
959 (ARMV6_PMCR_COUNT0_OVERFLOW | ARMV6_PMCR_COUNT1_OVERFLOW | \
960 ARMV6_PMCR_CCOUNT_OVERFLOW)
963 armv6_pmcr_has_overflowed(unsigned long pmcr
)
965 return (pmcr
& ARMV6_PMCR_OVERFLOWED_MASK
);
969 armv6_pmcr_counter_has_overflowed(unsigned long pmcr
,
970 enum armv6_counters counter
)
974 if (ARMV6_CYCLE_COUNTER
== counter
)
975 ret
= pmcr
& ARMV6_PMCR_CCOUNT_OVERFLOW
;
976 else if (ARMV6_COUNTER0
== counter
)
977 ret
= pmcr
& ARMV6_PMCR_COUNT0_OVERFLOW
;
978 else if (ARMV6_COUNTER1
== counter
)
979 ret
= pmcr
& ARMV6_PMCR_COUNT1_OVERFLOW
;
981 WARN_ONCE(1, "invalid counter number (%d)\n", counter
);
987 armv6pmu_read_counter(int counter
)
989 unsigned long value
= 0;
991 if (ARMV6_CYCLE_COUNTER
== counter
)
992 asm volatile("mrc p15, 0, %0, c15, c12, 1" : "=r"(value
));
993 else if (ARMV6_COUNTER0
== counter
)
994 asm volatile("mrc p15, 0, %0, c15, c12, 2" : "=r"(value
));
995 else if (ARMV6_COUNTER1
== counter
)
996 asm volatile("mrc p15, 0, %0, c15, c12, 3" : "=r"(value
));
998 WARN_ONCE(1, "invalid counter number (%d)\n", counter
);
1004 armv6pmu_write_counter(int counter
,
1007 if (ARMV6_CYCLE_COUNTER
== counter
)
1008 asm volatile("mcr p15, 0, %0, c15, c12, 1" : : "r"(value
));
1009 else if (ARMV6_COUNTER0
== counter
)
1010 asm volatile("mcr p15, 0, %0, c15, c12, 2" : : "r"(value
));
1011 else if (ARMV6_COUNTER1
== counter
)
1012 asm volatile("mcr p15, 0, %0, c15, c12, 3" : : "r"(value
));
1014 WARN_ONCE(1, "invalid counter number (%d)\n", counter
);
1018 armv6pmu_enable_event(struct hw_perf_event
*hwc
,
1021 unsigned long val
, mask
, evt
, flags
;
1023 if (ARMV6_CYCLE_COUNTER
== idx
) {
1025 evt
= ARMV6_PMCR_CCOUNT_IEN
;
1026 } else if (ARMV6_COUNTER0
== idx
) {
1027 mask
= ARMV6_PMCR_EVT_COUNT0_MASK
;
1028 evt
= (hwc
->config_base
<< ARMV6_PMCR_EVT_COUNT0_SHIFT
) |
1029 ARMV6_PMCR_COUNT0_IEN
;
1030 } else if (ARMV6_COUNTER1
== idx
) {
1031 mask
= ARMV6_PMCR_EVT_COUNT1_MASK
;
1032 evt
= (hwc
->config_base
<< ARMV6_PMCR_EVT_COUNT1_SHIFT
) |
1033 ARMV6_PMCR_COUNT1_IEN
;
1035 WARN_ONCE(1, "invalid counter number (%d)\n", idx
);
1040 * Mask out the current event and set the counter to count the event
1041 * that we're interested in.
1043 spin_lock_irqsave(&pmu_lock
, flags
);
1044 val
= armv6_pmcr_read();
1047 armv6_pmcr_write(val
);
1048 spin_unlock_irqrestore(&pmu_lock
, flags
);
1052 armv6pmu_handle_irq(int irq_num
,
1055 unsigned long pmcr
= armv6_pmcr_read();
1056 struct perf_sample_data data
;
1057 struct cpu_hw_events
*cpuc
;
1058 struct pt_regs
*regs
;
1061 if (!armv6_pmcr_has_overflowed(pmcr
))
1064 regs
= get_irq_regs();
1067 * The interrupts are cleared by writing the overflow flags back to
1068 * the control register. All of the other bits don't have any effect
1069 * if they are rewritten, so write the whole value back.
1071 armv6_pmcr_write(pmcr
);
1073 perf_sample_data_init(&data
, 0);
1075 cpuc
= &__get_cpu_var(cpu_hw_events
);
1076 for (idx
= 0; idx
<= armpmu
->num_events
; ++idx
) {
1077 struct perf_event
*event
= cpuc
->events
[idx
];
1078 struct hw_perf_event
*hwc
;
1080 if (!test_bit(idx
, cpuc
->active_mask
))
1084 * We have a single interrupt for all counters. Check that
1085 * each counter has overflowed before we process it.
1087 if (!armv6_pmcr_counter_has_overflowed(pmcr
, idx
))
1091 armpmu_event_update(event
, hwc
, idx
);
1092 data
.period
= event
->hw
.last_period
;
1093 if (!armpmu_event_set_period(event
, hwc
, idx
))
1096 if (perf_event_overflow(event
, 0, &data
, regs
))
1097 armpmu
->disable(hwc
, idx
);
1101 * Handle the pending perf events.
1103 * Note: this call *must* be run with interrupts disabled. For
1104 * platforms that can have the PMU interrupts raised as an NMI, this
1113 armv6pmu_start(void)
1115 unsigned long flags
, val
;
1117 spin_lock_irqsave(&pmu_lock
, flags
);
1118 val
= armv6_pmcr_read();
1119 val
|= ARMV6_PMCR_ENABLE
;
1120 armv6_pmcr_write(val
);
1121 spin_unlock_irqrestore(&pmu_lock
, flags
);
1127 unsigned long flags
, val
;
1129 spin_lock_irqsave(&pmu_lock
, flags
);
1130 val
= armv6_pmcr_read();
1131 val
&= ~ARMV6_PMCR_ENABLE
;
1132 armv6_pmcr_write(val
);
1133 spin_unlock_irqrestore(&pmu_lock
, flags
);
1137 armv6pmu_get_event_idx(struct cpu_hw_events
*cpuc
,
1138 struct hw_perf_event
*event
)
1140 /* Always place a cycle counter into the cycle counter. */
1141 if (ARMV6_PERFCTR_CPU_CYCLES
== event
->config_base
) {
1142 if (test_and_set_bit(ARMV6_CYCLE_COUNTER
, cpuc
->used_mask
))
1145 return ARMV6_CYCLE_COUNTER
;
1148 * For anything other than a cycle counter, try and use
1149 * counter0 and counter1.
1151 if (!test_and_set_bit(ARMV6_COUNTER1
, cpuc
->used_mask
)) {
1152 return ARMV6_COUNTER1
;
1155 if (!test_and_set_bit(ARMV6_COUNTER0
, cpuc
->used_mask
)) {
1156 return ARMV6_COUNTER0
;
1159 /* The counters are all in use. */
1165 armv6pmu_disable_event(struct hw_perf_event
*hwc
,
1168 unsigned long val
, mask
, evt
, flags
;
1170 if (ARMV6_CYCLE_COUNTER
== idx
) {
1171 mask
= ARMV6_PMCR_CCOUNT_IEN
;
1173 } else if (ARMV6_COUNTER0
== idx
) {
1174 mask
= ARMV6_PMCR_COUNT0_IEN
| ARMV6_PMCR_EVT_COUNT0_MASK
;
1175 evt
= ARMV6_PERFCTR_NOP
<< ARMV6_PMCR_EVT_COUNT0_SHIFT
;
1176 } else if (ARMV6_COUNTER1
== idx
) {
1177 mask
= ARMV6_PMCR_COUNT1_IEN
| ARMV6_PMCR_EVT_COUNT1_MASK
;
1178 evt
= ARMV6_PERFCTR_NOP
<< ARMV6_PMCR_EVT_COUNT1_SHIFT
;
1180 WARN_ONCE(1, "invalid counter number (%d)\n", idx
);
1185 * Mask out the current event and set the counter to count the number
1186 * of ETM bus signal assertion cycles. The external reporting should
1187 * be disabled and so this should never increment.
1189 spin_lock_irqsave(&pmu_lock
, flags
);
1190 val
= armv6_pmcr_read();
1193 armv6_pmcr_write(val
);
1194 spin_unlock_irqrestore(&pmu_lock
, flags
);
1198 armv6mpcore_pmu_disable_event(struct hw_perf_event
*hwc
,
1201 unsigned long val
, mask
, flags
, evt
= 0;
1203 if (ARMV6_CYCLE_COUNTER
== idx
) {
1204 mask
= ARMV6_PMCR_CCOUNT_IEN
;
1205 } else if (ARMV6_COUNTER0
== idx
) {
1206 mask
= ARMV6_PMCR_COUNT0_IEN
;
1207 } else if (ARMV6_COUNTER1
== idx
) {
1208 mask
= ARMV6_PMCR_COUNT1_IEN
;
1210 WARN_ONCE(1, "invalid counter number (%d)\n", idx
);
1215 * Unlike UP ARMv6, we don't have a way of stopping the counters. We
1216 * simply disable the interrupt reporting.
1218 spin_lock_irqsave(&pmu_lock
, flags
);
1219 val
= armv6_pmcr_read();
1222 armv6_pmcr_write(val
);
1223 spin_unlock_irqrestore(&pmu_lock
, flags
);
1226 static const struct arm_pmu armv6pmu
= {
1227 .id
= ARM_PERF_PMU_ID_V6
,
1228 .handle_irq
= armv6pmu_handle_irq
,
1229 .enable
= armv6pmu_enable_event
,
1230 .disable
= armv6pmu_disable_event
,
1231 .read_counter
= armv6pmu_read_counter
,
1232 .write_counter
= armv6pmu_write_counter
,
1233 .get_event_idx
= armv6pmu_get_event_idx
,
1234 .start
= armv6pmu_start
,
1235 .stop
= armv6pmu_stop
,
1236 .cache_map
= &armv6_perf_cache_map
,
1237 .event_map
= &armv6_perf_map
,
1238 .raw_event_mask
= 0xFF,
1240 .max_period
= (1LLU << 32) - 1,
1243 const struct arm_pmu
*__init
armv6pmu_init(void)
1249 * ARMv6mpcore is almost identical to single core ARMv6 with the exception
1250 * that some of the events have different enumerations and that there is no
1251 * *hack* to stop the programmable counters. To stop the counters we simply
1252 * disable the interrupt reporting and update the event. When unthrottling we
1253 * reset the period and enable the interrupt reporting.
1255 static const struct arm_pmu armv6mpcore_pmu
= {
1256 .id
= ARM_PERF_PMU_ID_V6MP
,
1257 .handle_irq
= armv6pmu_handle_irq
,
1258 .enable
= armv6pmu_enable_event
,
1259 .disable
= armv6mpcore_pmu_disable_event
,
1260 .read_counter
= armv6pmu_read_counter
,
1261 .write_counter
= armv6pmu_write_counter
,
1262 .get_event_idx
= armv6pmu_get_event_idx
,
1263 .start
= armv6pmu_start
,
1264 .stop
= armv6pmu_stop
,
1265 .cache_map
= &armv6mpcore_perf_cache_map
,
1266 .event_map
= &armv6mpcore_perf_map
,
1267 .raw_event_mask
= 0xFF,
1269 .max_period
= (1LLU << 32) - 1,
1272 const struct arm_pmu
*__init
armv6mpcore_pmu_init(void)
1274 return &armv6mpcore_pmu
;
1278 * ARMv7 Cortex-A8 and Cortex-A9 Performance Events handling code.
1280 * Copied from ARMv6 code, with the low level code inspired
1281 * by the ARMv7 Oprofile code.
1283 * Cortex-A8 has up to 4 configurable performance counters and
1284 * a single cycle counter.
1285 * Cortex-A9 has up to 31 configurable performance counters and
1286 * a single cycle counter.
1288 * All counters can be enabled/disabled and IRQ masked separately. The cycle
1289 * counter and all 4 performance counters together can be reset separately.
1292 /* Common ARMv7 event types */
1293 enum armv7_perf_types
{
1294 ARMV7_PERFCTR_PMNC_SW_INCR
= 0x00,
1295 ARMV7_PERFCTR_IFETCH_MISS
= 0x01,
1296 ARMV7_PERFCTR_ITLB_MISS
= 0x02,
1297 ARMV7_PERFCTR_DCACHE_REFILL
= 0x03,
1298 ARMV7_PERFCTR_DCACHE_ACCESS
= 0x04,
1299 ARMV7_PERFCTR_DTLB_REFILL
= 0x05,
1300 ARMV7_PERFCTR_DREAD
= 0x06,
1301 ARMV7_PERFCTR_DWRITE
= 0x07,
1303 ARMV7_PERFCTR_EXC_TAKEN
= 0x09,
1304 ARMV7_PERFCTR_EXC_EXECUTED
= 0x0A,
1305 ARMV7_PERFCTR_CID_WRITE
= 0x0B,
1306 /* ARMV7_PERFCTR_PC_WRITE is equivalent to HW_BRANCH_INSTRUCTIONS.
1308 * - all branch instructions,
1309 * - instructions that explicitly write the PC,
1310 * - exception generating instructions.
1312 ARMV7_PERFCTR_PC_WRITE
= 0x0C,
1313 ARMV7_PERFCTR_PC_IMM_BRANCH
= 0x0D,
1314 ARMV7_PERFCTR_UNALIGNED_ACCESS
= 0x0F,
1315 ARMV7_PERFCTR_PC_BRANCH_MIS_PRED
= 0x10,
1316 ARMV7_PERFCTR_CLOCK_CYCLES
= 0x11,
1318 ARMV7_PERFCTR_PC_BRANCH_MIS_USED
= 0x12,
1320 ARMV7_PERFCTR_CPU_CYCLES
= 0xFF
1323 /* ARMv7 Cortex-A8 specific event types */
1324 enum armv7_a8_perf_types
{
1325 ARMV7_PERFCTR_INSTR_EXECUTED
= 0x08,
1327 ARMV7_PERFCTR_PC_PROC_RETURN
= 0x0E,
1329 ARMV7_PERFCTR_WRITE_BUFFER_FULL
= 0x40,
1330 ARMV7_PERFCTR_L2_STORE_MERGED
= 0x41,
1331 ARMV7_PERFCTR_L2_STORE_BUFF
= 0x42,
1332 ARMV7_PERFCTR_L2_ACCESS
= 0x43,
1333 ARMV7_PERFCTR_L2_CACH_MISS
= 0x44,
1334 ARMV7_PERFCTR_AXI_READ_CYCLES
= 0x45,
1335 ARMV7_PERFCTR_AXI_WRITE_CYCLES
= 0x46,
1336 ARMV7_PERFCTR_MEMORY_REPLAY
= 0x47,
1337 ARMV7_PERFCTR_UNALIGNED_ACCESS_REPLAY
= 0x48,
1338 ARMV7_PERFCTR_L1_DATA_MISS
= 0x49,
1339 ARMV7_PERFCTR_L1_INST_MISS
= 0x4A,
1340 ARMV7_PERFCTR_L1_DATA_COLORING
= 0x4B,
1341 ARMV7_PERFCTR_L1_NEON_DATA
= 0x4C,
1342 ARMV7_PERFCTR_L1_NEON_CACH_DATA
= 0x4D,
1343 ARMV7_PERFCTR_L2_NEON
= 0x4E,
1344 ARMV7_PERFCTR_L2_NEON_HIT
= 0x4F,
1345 ARMV7_PERFCTR_L1_INST
= 0x50,
1346 ARMV7_PERFCTR_PC_RETURN_MIS_PRED
= 0x51,
1347 ARMV7_PERFCTR_PC_BRANCH_FAILED
= 0x52,
1348 ARMV7_PERFCTR_PC_BRANCH_TAKEN
= 0x53,
1349 ARMV7_PERFCTR_PC_BRANCH_EXECUTED
= 0x54,
1350 ARMV7_PERFCTR_OP_EXECUTED
= 0x55,
1351 ARMV7_PERFCTR_CYCLES_INST_STALL
= 0x56,
1352 ARMV7_PERFCTR_CYCLES_INST
= 0x57,
1353 ARMV7_PERFCTR_CYCLES_NEON_DATA_STALL
= 0x58,
1354 ARMV7_PERFCTR_CYCLES_NEON_INST_STALL
= 0x59,
1355 ARMV7_PERFCTR_NEON_CYCLES
= 0x5A,
1357 ARMV7_PERFCTR_PMU0_EVENTS
= 0x70,
1358 ARMV7_PERFCTR_PMU1_EVENTS
= 0x71,
1359 ARMV7_PERFCTR_PMU_EVENTS
= 0x72,
1362 /* ARMv7 Cortex-A9 specific event types */
1363 enum armv7_a9_perf_types
{
1364 ARMV7_PERFCTR_JAVA_HW_BYTECODE_EXEC
= 0x40,
1365 ARMV7_PERFCTR_JAVA_SW_BYTECODE_EXEC
= 0x41,
1366 ARMV7_PERFCTR_JAZELLE_BRANCH_EXEC
= 0x42,
1368 ARMV7_PERFCTR_COHERENT_LINE_MISS
= 0x50,
1369 ARMV7_PERFCTR_COHERENT_LINE_HIT
= 0x51,
1371 ARMV7_PERFCTR_ICACHE_DEP_STALL_CYCLES
= 0x60,
1372 ARMV7_PERFCTR_DCACHE_DEP_STALL_CYCLES
= 0x61,
1373 ARMV7_PERFCTR_TLB_MISS_DEP_STALL_CYCLES
= 0x62,
1374 ARMV7_PERFCTR_STREX_EXECUTED_PASSED
= 0x63,
1375 ARMV7_PERFCTR_STREX_EXECUTED_FAILED
= 0x64,
1376 ARMV7_PERFCTR_DATA_EVICTION
= 0x65,
1377 ARMV7_PERFCTR_ISSUE_STAGE_NO_INST
= 0x66,
1378 ARMV7_PERFCTR_ISSUE_STAGE_EMPTY
= 0x67,
1379 ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE
= 0x68,
1381 ARMV7_PERFCTR_PREDICTABLE_FUNCT_RETURNS
= 0x6E,
1383 ARMV7_PERFCTR_MAIN_UNIT_EXECUTED_INST
= 0x70,
1384 ARMV7_PERFCTR_SECOND_UNIT_EXECUTED_INST
= 0x71,
1385 ARMV7_PERFCTR_LD_ST_UNIT_EXECUTED_INST
= 0x72,
1386 ARMV7_PERFCTR_FP_EXECUTED_INST
= 0x73,
1387 ARMV7_PERFCTR_NEON_EXECUTED_INST
= 0x74,
1389 ARMV7_PERFCTR_PLD_FULL_DEP_STALL_CYCLES
= 0x80,
1390 ARMV7_PERFCTR_DATA_WR_DEP_STALL_CYCLES
= 0x81,
1391 ARMV7_PERFCTR_ITLB_MISS_DEP_STALL_CYCLES
= 0x82,
1392 ARMV7_PERFCTR_DTLB_MISS_DEP_STALL_CYCLES
= 0x83,
1393 ARMV7_PERFCTR_MICRO_ITLB_MISS_DEP_STALL_CYCLES
= 0x84,
1394 ARMV7_PERFCTR_MICRO_DTLB_MISS_DEP_STALL_CYCLES
= 0x85,
1395 ARMV7_PERFCTR_DMB_DEP_STALL_CYCLES
= 0x86,
1397 ARMV7_PERFCTR_INTGR_CLK_ENABLED_CYCLES
= 0x8A,
1398 ARMV7_PERFCTR_DATA_ENGINE_CLK_EN_CYCLES
= 0x8B,
1400 ARMV7_PERFCTR_ISB_INST
= 0x90,
1401 ARMV7_PERFCTR_DSB_INST
= 0x91,
1402 ARMV7_PERFCTR_DMB_INST
= 0x92,
1403 ARMV7_PERFCTR_EXT_INTERRUPTS
= 0x93,
1405 ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_COMPLETED
= 0xA0,
1406 ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_SKIPPED
= 0xA1,
1407 ARMV7_PERFCTR_PLE_FIFO_FLUSH
= 0xA2,
1408 ARMV7_PERFCTR_PLE_RQST_COMPLETED
= 0xA3,
1409 ARMV7_PERFCTR_PLE_FIFO_OVERFLOW
= 0xA4,
1410 ARMV7_PERFCTR_PLE_RQST_PROG
= 0xA5
1414 * Cortex-A8 HW events mapping
1416 * The hardware events that we support. We do support cache operations but
1417 * we have harvard caches and no way to combine instruction and data
1418 * accesses/misses in hardware.
1420 static const unsigned armv7_a8_perf_map
[PERF_COUNT_HW_MAX
] = {
1421 [PERF_COUNT_HW_CPU_CYCLES
] = ARMV7_PERFCTR_CPU_CYCLES
,
1422 [PERF_COUNT_HW_INSTRUCTIONS
] = ARMV7_PERFCTR_INSTR_EXECUTED
,
1423 [PERF_COUNT_HW_CACHE_REFERENCES
] = HW_OP_UNSUPPORTED
,
1424 [PERF_COUNT_HW_CACHE_MISSES
] = HW_OP_UNSUPPORTED
,
1425 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = ARMV7_PERFCTR_PC_WRITE
,
1426 [PERF_COUNT_HW_BRANCH_MISSES
] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED
,
1427 [PERF_COUNT_HW_BUS_CYCLES
] = ARMV7_PERFCTR_CLOCK_CYCLES
,
1430 static const unsigned armv7_a8_perf_cache_map
[PERF_COUNT_HW_CACHE_MAX
]
1431 [PERF_COUNT_HW_CACHE_OP_MAX
]
1432 [PERF_COUNT_HW_CACHE_RESULT_MAX
] = {
1435 * The performance counters don't differentiate between read
1436 * and write accesses/misses so this isn't strictly correct,
1437 * but it's the best we can do. Writes and reads get
1441 [C(RESULT_ACCESS
)] = ARMV7_PERFCTR_DCACHE_ACCESS
,
1442 [C(RESULT_MISS
)] = ARMV7_PERFCTR_DCACHE_REFILL
,
1445 [C(RESULT_ACCESS
)] = ARMV7_PERFCTR_DCACHE_ACCESS
,
1446 [C(RESULT_MISS
)] = ARMV7_PERFCTR_DCACHE_REFILL
,
1448 [C(OP_PREFETCH
)] = {
1449 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1450 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
1455 [C(RESULT_ACCESS
)] = ARMV7_PERFCTR_L1_INST
,
1456 [C(RESULT_MISS
)] = ARMV7_PERFCTR_L1_INST_MISS
,
1459 [C(RESULT_ACCESS
)] = ARMV7_PERFCTR_L1_INST
,
1460 [C(RESULT_MISS
)] = ARMV7_PERFCTR_L1_INST_MISS
,
1462 [C(OP_PREFETCH
)] = {
1463 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1464 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
1469 [C(RESULT_ACCESS
)] = ARMV7_PERFCTR_L2_ACCESS
,
1470 [C(RESULT_MISS
)] = ARMV7_PERFCTR_L2_CACH_MISS
,
1473 [C(RESULT_ACCESS
)] = ARMV7_PERFCTR_L2_ACCESS
,
1474 [C(RESULT_MISS
)] = ARMV7_PERFCTR_L2_CACH_MISS
,
1476 [C(OP_PREFETCH
)] = {
1477 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1478 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
1483 * Only ITLB misses and DTLB refills are supported.
1484 * If users want the DTLB refills misses a raw counter
1488 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1489 [C(RESULT_MISS
)] = ARMV7_PERFCTR_DTLB_REFILL
,
1492 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1493 [C(RESULT_MISS
)] = ARMV7_PERFCTR_DTLB_REFILL
,
1495 [C(OP_PREFETCH
)] = {
1496 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1497 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
1502 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1503 [C(RESULT_MISS
)] = ARMV7_PERFCTR_ITLB_MISS
,
1506 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1507 [C(RESULT_MISS
)] = ARMV7_PERFCTR_ITLB_MISS
,
1509 [C(OP_PREFETCH
)] = {
1510 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1511 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
1516 [C(RESULT_ACCESS
)] = ARMV7_PERFCTR_PC_WRITE
,
1518 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED
,
1521 [C(RESULT_ACCESS
)] = ARMV7_PERFCTR_PC_WRITE
,
1523 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED
,
1525 [C(OP_PREFETCH
)] = {
1526 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1527 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
1533 * Cortex-A9 HW events mapping
1535 static const unsigned armv7_a9_perf_map
[PERF_COUNT_HW_MAX
] = {
1536 [PERF_COUNT_HW_CPU_CYCLES
] = ARMV7_PERFCTR_CPU_CYCLES
,
1537 [PERF_COUNT_HW_INSTRUCTIONS
] =
1538 ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE
,
1539 [PERF_COUNT_HW_CACHE_REFERENCES
] = ARMV7_PERFCTR_COHERENT_LINE_HIT
,
1540 [PERF_COUNT_HW_CACHE_MISSES
] = ARMV7_PERFCTR_COHERENT_LINE_MISS
,
1541 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = ARMV7_PERFCTR_PC_WRITE
,
1542 [PERF_COUNT_HW_BRANCH_MISSES
] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED
,
1543 [PERF_COUNT_HW_BUS_CYCLES
] = ARMV7_PERFCTR_CLOCK_CYCLES
,
1546 static const unsigned armv7_a9_perf_cache_map
[PERF_COUNT_HW_CACHE_MAX
]
1547 [PERF_COUNT_HW_CACHE_OP_MAX
]
1548 [PERF_COUNT_HW_CACHE_RESULT_MAX
] = {
1551 * The performance counters don't differentiate between read
1552 * and write accesses/misses so this isn't strictly correct,
1553 * but it's the best we can do. Writes and reads get
1557 [C(RESULT_ACCESS
)] = ARMV7_PERFCTR_DCACHE_ACCESS
,
1558 [C(RESULT_MISS
)] = ARMV7_PERFCTR_DCACHE_REFILL
,
1561 [C(RESULT_ACCESS
)] = ARMV7_PERFCTR_DCACHE_ACCESS
,
1562 [C(RESULT_MISS
)] = ARMV7_PERFCTR_DCACHE_REFILL
,
1564 [C(OP_PREFETCH
)] = {
1565 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1566 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
1571 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1572 [C(RESULT_MISS
)] = ARMV7_PERFCTR_IFETCH_MISS
,
1575 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1576 [C(RESULT_MISS
)] = ARMV7_PERFCTR_IFETCH_MISS
,
1578 [C(OP_PREFETCH
)] = {
1579 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1580 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
1585 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1586 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
1589 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1590 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
1592 [C(OP_PREFETCH
)] = {
1593 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1594 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
1599 * Only ITLB misses and DTLB refills are supported.
1600 * If users want the DTLB refills misses a raw counter
1604 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1605 [C(RESULT_MISS
)] = ARMV7_PERFCTR_DTLB_REFILL
,
1608 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1609 [C(RESULT_MISS
)] = ARMV7_PERFCTR_DTLB_REFILL
,
1611 [C(OP_PREFETCH
)] = {
1612 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1613 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
1618 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1619 [C(RESULT_MISS
)] = ARMV7_PERFCTR_ITLB_MISS
,
1622 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1623 [C(RESULT_MISS
)] = ARMV7_PERFCTR_ITLB_MISS
,
1625 [C(OP_PREFETCH
)] = {
1626 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1627 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
1632 [C(RESULT_ACCESS
)] = ARMV7_PERFCTR_PC_WRITE
,
1634 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED
,
1637 [C(RESULT_ACCESS
)] = ARMV7_PERFCTR_PC_WRITE
,
1639 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED
,
1641 [C(OP_PREFETCH
)] = {
1642 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
1643 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
1649 * Perf Events counters
1651 enum armv7_counters
{
1652 ARMV7_CYCLE_COUNTER
= 1, /* Cycle counter */
1653 ARMV7_COUNTER0
= 2, /* First event counter */
1657 * The cycle counter is ARMV7_CYCLE_COUNTER.
1658 * The first event counter is ARMV7_COUNTER0.
1659 * The last event counter is (ARMV7_COUNTER0 + armpmu->num_events - 1).
1661 #define ARMV7_COUNTER_LAST (ARMV7_COUNTER0 + armpmu->num_events - 1)
1664 * ARMv7 low level PMNC access
1668 * Per-CPU PMNC: config reg
1670 #define ARMV7_PMNC_E (1 << 0) /* Enable all counters */
1671 #define ARMV7_PMNC_P (1 << 1) /* Reset all counters */
1672 #define ARMV7_PMNC_C (1 << 2) /* Cycle counter reset */
1673 #define ARMV7_PMNC_D (1 << 3) /* CCNT counts every 64th cpu cycle */
1674 #define ARMV7_PMNC_X (1 << 4) /* Export to ETM */
1675 #define ARMV7_PMNC_DP (1 << 5) /* Disable CCNT if non-invasive debug*/
1676 #define ARMV7_PMNC_N_SHIFT 11 /* Number of counters supported */
1677 #define ARMV7_PMNC_N_MASK 0x1f
1678 #define ARMV7_PMNC_MASK 0x3f /* Mask for writable bits */
1681 * Available counters
1683 #define ARMV7_CNT0 0 /* First event counter */
1684 #define ARMV7_CCNT 31 /* Cycle counter */
1686 /* Perf Event to low level counters mapping */
1687 #define ARMV7_EVENT_CNT_TO_CNTx (ARMV7_COUNTER0 - ARMV7_CNT0)
1690 * CNTENS: counters enable reg
1692 #define ARMV7_CNTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1693 #define ARMV7_CNTENS_C (1 << ARMV7_CCNT)
1696 * CNTENC: counters disable reg
1698 #define ARMV7_CNTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1699 #define ARMV7_CNTENC_C (1 << ARMV7_CCNT)
1702 * INTENS: counters overflow interrupt enable reg
1704 #define ARMV7_INTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1705 #define ARMV7_INTENS_C (1 << ARMV7_CCNT)
1708 * INTENC: counters overflow interrupt disable reg
1710 #define ARMV7_INTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1711 #define ARMV7_INTENC_C (1 << ARMV7_CCNT)
1714 * EVTSEL: Event selection reg
1716 #define ARMV7_EVTSEL_MASK 0xff /* Mask for writable bits */
1719 * SELECT: Counter selection reg
1721 #define ARMV7_SELECT_MASK 0x1f /* Mask for writable bits */
1724 * FLAG: counters overflow flag status reg
1726 #define ARMV7_FLAG_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1727 #define ARMV7_FLAG_C (1 << ARMV7_CCNT)
1728 #define ARMV7_FLAG_MASK 0xffffffff /* Mask for writable bits */
1729 #define ARMV7_OVERFLOWED_MASK ARMV7_FLAG_MASK
1731 static inline unsigned long armv7_pmnc_read(void)
1734 asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r"(val
));
1738 static inline void armv7_pmnc_write(unsigned long val
)
1740 val
&= ARMV7_PMNC_MASK
;
1741 asm volatile("mcr p15, 0, %0, c9, c12, 0" : : "r"(val
));
1744 static inline int armv7_pmnc_has_overflowed(unsigned long pmnc
)
1746 return pmnc
& ARMV7_OVERFLOWED_MASK
;
1749 static inline int armv7_pmnc_counter_has_overflowed(unsigned long pmnc
,
1750 enum armv7_counters counter
)
1754 if (counter
== ARMV7_CYCLE_COUNTER
)
1755 ret
= pmnc
& ARMV7_FLAG_C
;
1756 else if ((counter
>= ARMV7_COUNTER0
) && (counter
<= ARMV7_COUNTER_LAST
))
1757 ret
= pmnc
& ARMV7_FLAG_P(counter
);
1759 pr_err("CPU%u checking wrong counter %d overflow status\n",
1760 smp_processor_id(), counter
);
1765 static inline int armv7_pmnc_select_counter(unsigned int idx
)
1769 if ((idx
< ARMV7_COUNTER0
) || (idx
> ARMV7_COUNTER_LAST
)) {
1770 pr_err("CPU%u selecting wrong PMNC counter"
1771 " %d\n", smp_processor_id(), idx
);
1775 val
= (idx
- ARMV7_EVENT_CNT_TO_CNTx
) & ARMV7_SELECT_MASK
;
1776 asm volatile("mcr p15, 0, %0, c9, c12, 5" : : "r" (val
));
1781 static inline u32
armv7pmu_read_counter(int idx
)
1783 unsigned long value
= 0;
1785 if (idx
== ARMV7_CYCLE_COUNTER
)
1786 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (value
));
1787 else if ((idx
>= ARMV7_COUNTER0
) && (idx
<= ARMV7_COUNTER_LAST
)) {
1788 if (armv7_pmnc_select_counter(idx
) == idx
)
1789 asm volatile("mrc p15, 0, %0, c9, c13, 2"
1792 pr_err("CPU%u reading wrong counter %d\n",
1793 smp_processor_id(), idx
);
1798 static inline void armv7pmu_write_counter(int idx
, u32 value
)
1800 if (idx
== ARMV7_CYCLE_COUNTER
)
1801 asm volatile("mcr p15, 0, %0, c9, c13, 0" : : "r" (value
));
1802 else if ((idx
>= ARMV7_COUNTER0
) && (idx
<= ARMV7_COUNTER_LAST
)) {
1803 if (armv7_pmnc_select_counter(idx
) == idx
)
1804 asm volatile("mcr p15, 0, %0, c9, c13, 2"
1807 pr_err("CPU%u writing wrong counter %d\n",
1808 smp_processor_id(), idx
);
1811 static inline void armv7_pmnc_write_evtsel(unsigned int idx
, u32 val
)
1813 if (armv7_pmnc_select_counter(idx
) == idx
) {
1814 val
&= ARMV7_EVTSEL_MASK
;
1815 asm volatile("mcr p15, 0, %0, c9, c13, 1" : : "r" (val
));
1819 static inline u32
armv7_pmnc_enable_counter(unsigned int idx
)
1823 if ((idx
!= ARMV7_CYCLE_COUNTER
) &&
1824 ((idx
< ARMV7_COUNTER0
) || (idx
> ARMV7_COUNTER_LAST
))) {
1825 pr_err("CPU%u enabling wrong PMNC counter"
1826 " %d\n", smp_processor_id(), idx
);
1830 if (idx
== ARMV7_CYCLE_COUNTER
)
1831 val
= ARMV7_CNTENS_C
;
1833 val
= ARMV7_CNTENS_P(idx
);
1835 asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r" (val
));
1840 static inline u32
armv7_pmnc_disable_counter(unsigned int idx
)
1845 if ((idx
!= ARMV7_CYCLE_COUNTER
) &&
1846 ((idx
< ARMV7_COUNTER0
) || (idx
> ARMV7_COUNTER_LAST
))) {
1847 pr_err("CPU%u disabling wrong PMNC counter"
1848 " %d\n", smp_processor_id(), idx
);
1852 if (idx
== ARMV7_CYCLE_COUNTER
)
1853 val
= ARMV7_CNTENC_C
;
1855 val
= ARMV7_CNTENC_P(idx
);
1857 asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r" (val
));
1862 static inline u32
armv7_pmnc_enable_intens(unsigned int idx
)
1866 if ((idx
!= ARMV7_CYCLE_COUNTER
) &&
1867 ((idx
< ARMV7_COUNTER0
) || (idx
> ARMV7_COUNTER_LAST
))) {
1868 pr_err("CPU%u enabling wrong PMNC counter"
1869 " interrupt enable %d\n", smp_processor_id(), idx
);
1873 if (idx
== ARMV7_CYCLE_COUNTER
)
1874 val
= ARMV7_INTENS_C
;
1876 val
= ARMV7_INTENS_P(idx
);
1878 asm volatile("mcr p15, 0, %0, c9, c14, 1" : : "r" (val
));
1883 static inline u32
armv7_pmnc_disable_intens(unsigned int idx
)
1887 if ((idx
!= ARMV7_CYCLE_COUNTER
) &&
1888 ((idx
< ARMV7_COUNTER0
) || (idx
> ARMV7_COUNTER_LAST
))) {
1889 pr_err("CPU%u disabling wrong PMNC counter"
1890 " interrupt enable %d\n", smp_processor_id(), idx
);
1894 if (idx
== ARMV7_CYCLE_COUNTER
)
1895 val
= ARMV7_INTENC_C
;
1897 val
= ARMV7_INTENC_P(idx
);
1899 asm volatile("mcr p15, 0, %0, c9, c14, 2" : : "r" (val
));
1904 static inline u32
armv7_pmnc_getreset_flags(void)
1909 asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val
));
1911 /* Write to clear flags */
1912 val
&= ARMV7_FLAG_MASK
;
1913 asm volatile("mcr p15, 0, %0, c9, c12, 3" : : "r" (val
));
1919 static void armv7_pmnc_dump_regs(void)
1924 printk(KERN_INFO
"PMNC registers dump:\n");
1926 asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r" (val
));
1927 printk(KERN_INFO
"PMNC =0x%08x\n", val
);
1929 asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r" (val
));
1930 printk(KERN_INFO
"CNTENS=0x%08x\n", val
);
1932 asm volatile("mrc p15, 0, %0, c9, c14, 1" : "=r" (val
));
1933 printk(KERN_INFO
"INTENS=0x%08x\n", val
);
1935 asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val
));
1936 printk(KERN_INFO
"FLAGS =0x%08x\n", val
);
1938 asm volatile("mrc p15, 0, %0, c9, c12, 5" : "=r" (val
));
1939 printk(KERN_INFO
"SELECT=0x%08x\n", val
);
1941 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (val
));
1942 printk(KERN_INFO
"CCNT =0x%08x\n", val
);
1944 for (cnt
= ARMV7_COUNTER0
; cnt
< ARMV7_COUNTER_LAST
; cnt
++) {
1945 armv7_pmnc_select_counter(cnt
);
1946 asm volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (val
));
1947 printk(KERN_INFO
"CNT[%d] count =0x%08x\n",
1948 cnt
-ARMV7_EVENT_CNT_TO_CNTx
, val
);
1949 asm volatile("mrc p15, 0, %0, c9, c13, 1" : "=r" (val
));
1950 printk(KERN_INFO
"CNT[%d] evtsel=0x%08x\n",
1951 cnt
-ARMV7_EVENT_CNT_TO_CNTx
, val
);
1956 void armv7pmu_enable_event(struct hw_perf_event
*hwc
, int idx
)
1958 unsigned long flags
;
1961 * Enable counter and interrupt, and set the counter to count
1962 * the event that we're interested in.
1964 spin_lock_irqsave(&pmu_lock
, flags
);
1969 armv7_pmnc_disable_counter(idx
);
1972 * Set event (if destined for PMNx counters)
1973 * We don't need to set the event if it's a cycle count
1975 if (idx
!= ARMV7_CYCLE_COUNTER
)
1976 armv7_pmnc_write_evtsel(idx
, hwc
->config_base
);
1979 * Enable interrupt for this counter
1981 armv7_pmnc_enable_intens(idx
);
1986 armv7_pmnc_enable_counter(idx
);
1988 spin_unlock_irqrestore(&pmu_lock
, flags
);
1991 static void armv7pmu_disable_event(struct hw_perf_event
*hwc
, int idx
)
1993 unsigned long flags
;
1996 * Disable counter and interrupt
1998 spin_lock_irqsave(&pmu_lock
, flags
);
2003 armv7_pmnc_disable_counter(idx
);
2006 * Disable interrupt for this counter
2008 armv7_pmnc_disable_intens(idx
);
2010 spin_unlock_irqrestore(&pmu_lock
, flags
);
2013 static irqreturn_t
armv7pmu_handle_irq(int irq_num
, void *dev
)
2016 struct perf_sample_data data
;
2017 struct cpu_hw_events
*cpuc
;
2018 struct pt_regs
*regs
;
2022 * Get and reset the IRQ flags
2024 pmnc
= armv7_pmnc_getreset_flags();
2027 * Did an overflow occur?
2029 if (!armv7_pmnc_has_overflowed(pmnc
))
2033 * Handle the counter(s) overflow(s)
2035 regs
= get_irq_regs();
2037 perf_sample_data_init(&data
, 0);
2039 cpuc
= &__get_cpu_var(cpu_hw_events
);
2040 for (idx
= 0; idx
<= armpmu
->num_events
; ++idx
) {
2041 struct perf_event
*event
= cpuc
->events
[idx
];
2042 struct hw_perf_event
*hwc
;
2044 if (!test_bit(idx
, cpuc
->active_mask
))
2048 * We have a single interrupt for all counters. Check that
2049 * each counter has overflowed before we process it.
2051 if (!armv7_pmnc_counter_has_overflowed(pmnc
, idx
))
2055 armpmu_event_update(event
, hwc
, idx
);
2056 data
.period
= event
->hw
.last_period
;
2057 if (!armpmu_event_set_period(event
, hwc
, idx
))
2060 if (perf_event_overflow(event
, 0, &data
, regs
))
2061 armpmu
->disable(hwc
, idx
);
2065 * Handle the pending perf events.
2067 * Note: this call *must* be run with interrupts disabled. For
2068 * platforms that can have the PMU interrupts raised as an NMI, this
2076 static void armv7pmu_start(void)
2078 unsigned long flags
;
2080 spin_lock_irqsave(&pmu_lock
, flags
);
2081 /* Enable all counters */
2082 armv7_pmnc_write(armv7_pmnc_read() | ARMV7_PMNC_E
);
2083 spin_unlock_irqrestore(&pmu_lock
, flags
);
2086 static void armv7pmu_stop(void)
2088 unsigned long flags
;
2090 spin_lock_irqsave(&pmu_lock
, flags
);
2091 /* Disable all counters */
2092 armv7_pmnc_write(armv7_pmnc_read() & ~ARMV7_PMNC_E
);
2093 spin_unlock_irqrestore(&pmu_lock
, flags
);
2096 static int armv7pmu_get_event_idx(struct cpu_hw_events
*cpuc
,
2097 struct hw_perf_event
*event
)
2101 /* Always place a cycle counter into the cycle counter. */
2102 if (event
->config_base
== ARMV7_PERFCTR_CPU_CYCLES
) {
2103 if (test_and_set_bit(ARMV7_CYCLE_COUNTER
, cpuc
->used_mask
))
2106 return ARMV7_CYCLE_COUNTER
;
2109 * For anything other than a cycle counter, try and use
2110 * the events counters
2112 for (idx
= ARMV7_COUNTER0
; idx
<= armpmu
->num_events
; ++idx
) {
2113 if (!test_and_set_bit(idx
, cpuc
->used_mask
))
2117 /* The counters are all in use. */
2122 static struct arm_pmu armv7pmu
= {
2123 .handle_irq
= armv7pmu_handle_irq
,
2124 .enable
= armv7pmu_enable_event
,
2125 .disable
= armv7pmu_disable_event
,
2126 .read_counter
= armv7pmu_read_counter
,
2127 .write_counter
= armv7pmu_write_counter
,
2128 .get_event_idx
= armv7pmu_get_event_idx
,
2129 .start
= armv7pmu_start
,
2130 .stop
= armv7pmu_stop
,
2131 .raw_event_mask
= 0xFF,
2132 .max_period
= (1LLU << 32) - 1,
2135 static u32 __init
armv7_reset_read_pmnc(void)
2139 /* Initialize & Reset PMNC: C and P bits */
2140 armv7_pmnc_write(ARMV7_PMNC_P
| ARMV7_PMNC_C
);
2142 /* Read the nb of CNTx counters supported from PMNC */
2143 nb_cnt
= (armv7_pmnc_read() >> ARMV7_PMNC_N_SHIFT
) & ARMV7_PMNC_N_MASK
;
2145 /* Add the CPU cycles counter and return */
2149 const struct arm_pmu
*__init
armv7_a8_pmu_init(void)
2151 armv7pmu
.id
= ARM_PERF_PMU_ID_CA8
;
2152 armv7pmu
.cache_map
= &armv7_a8_perf_cache_map
;
2153 armv7pmu
.event_map
= &armv7_a8_perf_map
;
2154 armv7pmu
.num_events
= armv7_reset_read_pmnc();
2158 const struct arm_pmu
*__init
armv7_a9_pmu_init(void)
2160 armv7pmu
.id
= ARM_PERF_PMU_ID_CA9
;
2161 armv7pmu
.cache_map
= &armv7_a9_perf_cache_map
;
2162 armv7pmu
.event_map
= &armv7_a9_perf_map
;
2163 armv7pmu
.num_events
= armv7_reset_read_pmnc();
2169 * ARMv5 [xscale] Performance counter handling code.
2171 * Based on xscale OProfile code.
2173 * There are two variants of the xscale PMU that we support:
2174 * - xscale1pmu: 2 event counters and a cycle counter
2175 * - xscale2pmu: 4 event counters and a cycle counter
2176 * The two variants share event definitions, but have different
2180 enum xscale_perf_types
{
2181 XSCALE_PERFCTR_ICACHE_MISS
= 0x00,
2182 XSCALE_PERFCTR_ICACHE_NO_DELIVER
= 0x01,
2183 XSCALE_PERFCTR_DATA_STALL
= 0x02,
2184 XSCALE_PERFCTR_ITLB_MISS
= 0x03,
2185 XSCALE_PERFCTR_DTLB_MISS
= 0x04,
2186 XSCALE_PERFCTR_BRANCH
= 0x05,
2187 XSCALE_PERFCTR_BRANCH_MISS
= 0x06,
2188 XSCALE_PERFCTR_INSTRUCTION
= 0x07,
2189 XSCALE_PERFCTR_DCACHE_FULL_STALL
= 0x08,
2190 XSCALE_PERFCTR_DCACHE_FULL_STALL_CONTIG
= 0x09,
2191 XSCALE_PERFCTR_DCACHE_ACCESS
= 0x0A,
2192 XSCALE_PERFCTR_DCACHE_MISS
= 0x0B,
2193 XSCALE_PERFCTR_DCACHE_WRITE_BACK
= 0x0C,
2194 XSCALE_PERFCTR_PC_CHANGED
= 0x0D,
2195 XSCALE_PERFCTR_BCU_REQUEST
= 0x10,
2196 XSCALE_PERFCTR_BCU_FULL
= 0x11,
2197 XSCALE_PERFCTR_BCU_DRAIN
= 0x12,
2198 XSCALE_PERFCTR_BCU_ECC_NO_ELOG
= 0x14,
2199 XSCALE_PERFCTR_BCU_1_BIT_ERR
= 0x15,
2200 XSCALE_PERFCTR_RMW
= 0x16,
2201 /* XSCALE_PERFCTR_CCNT is not hardware defined */
2202 XSCALE_PERFCTR_CCNT
= 0xFE,
2203 XSCALE_PERFCTR_UNUSED
= 0xFF,
2206 enum xscale_counters
{
2207 XSCALE_CYCLE_COUNTER
= 1,
2214 static const unsigned xscale_perf_map
[PERF_COUNT_HW_MAX
] = {
2215 [PERF_COUNT_HW_CPU_CYCLES
] = XSCALE_PERFCTR_CCNT
,
2216 [PERF_COUNT_HW_INSTRUCTIONS
] = XSCALE_PERFCTR_INSTRUCTION
,
2217 [PERF_COUNT_HW_CACHE_REFERENCES
] = HW_OP_UNSUPPORTED
,
2218 [PERF_COUNT_HW_CACHE_MISSES
] = HW_OP_UNSUPPORTED
,
2219 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS
] = XSCALE_PERFCTR_BRANCH
,
2220 [PERF_COUNT_HW_BRANCH_MISSES
] = XSCALE_PERFCTR_BRANCH_MISS
,
2221 [PERF_COUNT_HW_BUS_CYCLES
] = HW_OP_UNSUPPORTED
,
2224 static const unsigned xscale_perf_cache_map
[PERF_COUNT_HW_CACHE_MAX
]
2225 [PERF_COUNT_HW_CACHE_OP_MAX
]
2226 [PERF_COUNT_HW_CACHE_RESULT_MAX
] = {
2229 [C(RESULT_ACCESS
)] = XSCALE_PERFCTR_DCACHE_ACCESS
,
2230 [C(RESULT_MISS
)] = XSCALE_PERFCTR_DCACHE_MISS
,
2233 [C(RESULT_ACCESS
)] = XSCALE_PERFCTR_DCACHE_ACCESS
,
2234 [C(RESULT_MISS
)] = XSCALE_PERFCTR_DCACHE_MISS
,
2236 [C(OP_PREFETCH
)] = {
2237 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2238 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
2243 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2244 [C(RESULT_MISS
)] = XSCALE_PERFCTR_ICACHE_MISS
,
2247 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2248 [C(RESULT_MISS
)] = XSCALE_PERFCTR_ICACHE_MISS
,
2250 [C(OP_PREFETCH
)] = {
2251 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2252 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
2257 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2258 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
2261 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2262 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
2264 [C(OP_PREFETCH
)] = {
2265 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2266 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
2271 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2272 [C(RESULT_MISS
)] = XSCALE_PERFCTR_DTLB_MISS
,
2275 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2276 [C(RESULT_MISS
)] = XSCALE_PERFCTR_DTLB_MISS
,
2278 [C(OP_PREFETCH
)] = {
2279 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2280 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
2285 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2286 [C(RESULT_MISS
)] = XSCALE_PERFCTR_ITLB_MISS
,
2289 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2290 [C(RESULT_MISS
)] = XSCALE_PERFCTR_ITLB_MISS
,
2292 [C(OP_PREFETCH
)] = {
2293 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2294 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
2299 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2300 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
2303 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2304 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
2306 [C(OP_PREFETCH
)] = {
2307 [C(RESULT_ACCESS
)] = CACHE_OP_UNSUPPORTED
,
2308 [C(RESULT_MISS
)] = CACHE_OP_UNSUPPORTED
,
2313 #define XSCALE_PMU_ENABLE 0x001
2314 #define XSCALE_PMN_RESET 0x002
2315 #define XSCALE_CCNT_RESET 0x004
2316 #define XSCALE_PMU_RESET (CCNT_RESET | PMN_RESET)
2317 #define XSCALE_PMU_CNT64 0x008
2319 #define XSCALE1_OVERFLOWED_MASK 0x700
2320 #define XSCALE1_CCOUNT_OVERFLOW 0x400
2321 #define XSCALE1_COUNT0_OVERFLOW 0x100
2322 #define XSCALE1_COUNT1_OVERFLOW 0x200
2323 #define XSCALE1_CCOUNT_INT_EN 0x040
2324 #define XSCALE1_COUNT0_INT_EN 0x010
2325 #define XSCALE1_COUNT1_INT_EN 0x020
2326 #define XSCALE1_COUNT0_EVT_SHFT 12
2327 #define XSCALE1_COUNT0_EVT_MASK (0xff << XSCALE1_COUNT0_EVT_SHFT)
2328 #define XSCALE1_COUNT1_EVT_SHFT 20
2329 #define XSCALE1_COUNT1_EVT_MASK (0xff << XSCALE1_COUNT1_EVT_SHFT)
2332 xscale1pmu_read_pmnc(void)
2335 asm volatile("mrc p14, 0, %0, c0, c0, 0" : "=r" (val
));
2340 xscale1pmu_write_pmnc(u32 val
)
2342 /* upper 4bits and 7, 11 are write-as-0 */
2344 asm volatile("mcr p14, 0, %0, c0, c0, 0" : : "r" (val
));
2348 xscale1_pmnc_counter_has_overflowed(unsigned long pmnc
,
2349 enum xscale_counters counter
)
2354 case XSCALE_CYCLE_COUNTER
:
2355 ret
= pmnc
& XSCALE1_CCOUNT_OVERFLOW
;
2357 case XSCALE_COUNTER0
:
2358 ret
= pmnc
& XSCALE1_COUNT0_OVERFLOW
;
2360 case XSCALE_COUNTER1
:
2361 ret
= pmnc
& XSCALE1_COUNT1_OVERFLOW
;
2364 WARN_ONCE(1, "invalid counter number (%d)\n", counter
);
2371 xscale1pmu_handle_irq(int irq_num
, void *dev
)
2374 struct perf_sample_data data
;
2375 struct cpu_hw_events
*cpuc
;
2376 struct pt_regs
*regs
;
2380 * NOTE: there's an A stepping erratum that states if an overflow
2381 * bit already exists and another occurs, the previous
2382 * Overflow bit gets cleared. There's no workaround.
2383 * Fixed in B stepping or later.
2385 pmnc
= xscale1pmu_read_pmnc();
2388 * Write the value back to clear the overflow flags. Overflow
2389 * flags remain in pmnc for use below. We also disable the PMU
2390 * while we process the interrupt.
2392 xscale1pmu_write_pmnc(pmnc
& ~XSCALE_PMU_ENABLE
);
2394 if (!(pmnc
& XSCALE1_OVERFLOWED_MASK
))
2397 regs
= get_irq_regs();
2399 perf_sample_data_init(&data
, 0);
2401 cpuc
= &__get_cpu_var(cpu_hw_events
);
2402 for (idx
= 0; idx
<= armpmu
->num_events
; ++idx
) {
2403 struct perf_event
*event
= cpuc
->events
[idx
];
2404 struct hw_perf_event
*hwc
;
2406 if (!test_bit(idx
, cpuc
->active_mask
))
2409 if (!xscale1_pmnc_counter_has_overflowed(pmnc
, idx
))
2413 armpmu_event_update(event
, hwc
, idx
);
2414 data
.period
= event
->hw
.last_period
;
2415 if (!armpmu_event_set_period(event
, hwc
, idx
))
2418 if (perf_event_overflow(event
, 0, &data
, regs
))
2419 armpmu
->disable(hwc
, idx
);
2425 * Re-enable the PMU.
2427 pmnc
= xscale1pmu_read_pmnc() | XSCALE_PMU_ENABLE
;
2428 xscale1pmu_write_pmnc(pmnc
);
2434 xscale1pmu_enable_event(struct hw_perf_event
*hwc
, int idx
)
2436 unsigned long val
, mask
, evt
, flags
;
2439 case XSCALE_CYCLE_COUNTER
:
2441 evt
= XSCALE1_CCOUNT_INT_EN
;
2443 case XSCALE_COUNTER0
:
2444 mask
= XSCALE1_COUNT0_EVT_MASK
;
2445 evt
= (hwc
->config_base
<< XSCALE1_COUNT0_EVT_SHFT
) |
2446 XSCALE1_COUNT0_INT_EN
;
2448 case XSCALE_COUNTER1
:
2449 mask
= XSCALE1_COUNT1_EVT_MASK
;
2450 evt
= (hwc
->config_base
<< XSCALE1_COUNT1_EVT_SHFT
) |
2451 XSCALE1_COUNT1_INT_EN
;
2454 WARN_ONCE(1, "invalid counter number (%d)\n", idx
);
2458 spin_lock_irqsave(&pmu_lock
, flags
);
2459 val
= xscale1pmu_read_pmnc();
2462 xscale1pmu_write_pmnc(val
);
2463 spin_unlock_irqrestore(&pmu_lock
, flags
);
2467 xscale1pmu_disable_event(struct hw_perf_event
*hwc
, int idx
)
2469 unsigned long val
, mask
, evt
, flags
;
2472 case XSCALE_CYCLE_COUNTER
:
2473 mask
= XSCALE1_CCOUNT_INT_EN
;
2476 case XSCALE_COUNTER0
:
2477 mask
= XSCALE1_COUNT0_INT_EN
| XSCALE1_COUNT0_EVT_MASK
;
2478 evt
= XSCALE_PERFCTR_UNUSED
<< XSCALE1_COUNT0_EVT_SHFT
;
2480 case XSCALE_COUNTER1
:
2481 mask
= XSCALE1_COUNT1_INT_EN
| XSCALE1_COUNT1_EVT_MASK
;
2482 evt
= XSCALE_PERFCTR_UNUSED
<< XSCALE1_COUNT1_EVT_SHFT
;
2485 WARN_ONCE(1, "invalid counter number (%d)\n", idx
);
2489 spin_lock_irqsave(&pmu_lock
, flags
);
2490 val
= xscale1pmu_read_pmnc();
2493 xscale1pmu_write_pmnc(val
);
2494 spin_unlock_irqrestore(&pmu_lock
, flags
);
2498 xscale1pmu_get_event_idx(struct cpu_hw_events
*cpuc
,
2499 struct hw_perf_event
*event
)
2501 if (XSCALE_PERFCTR_CCNT
== event
->config_base
) {
2502 if (test_and_set_bit(XSCALE_CYCLE_COUNTER
, cpuc
->used_mask
))
2505 return XSCALE_CYCLE_COUNTER
;
2507 if (!test_and_set_bit(XSCALE_COUNTER1
, cpuc
->used_mask
)) {
2508 return XSCALE_COUNTER1
;
2511 if (!test_and_set_bit(XSCALE_COUNTER0
, cpuc
->used_mask
)) {
2512 return XSCALE_COUNTER0
;
2520 xscale1pmu_start(void)
2522 unsigned long flags
, val
;
2524 spin_lock_irqsave(&pmu_lock
, flags
);
2525 val
= xscale1pmu_read_pmnc();
2526 val
|= XSCALE_PMU_ENABLE
;
2527 xscale1pmu_write_pmnc(val
);
2528 spin_unlock_irqrestore(&pmu_lock
, flags
);
2532 xscale1pmu_stop(void)
2534 unsigned long flags
, val
;
2536 spin_lock_irqsave(&pmu_lock
, flags
);
2537 val
= xscale1pmu_read_pmnc();
2538 val
&= ~XSCALE_PMU_ENABLE
;
2539 xscale1pmu_write_pmnc(val
);
2540 spin_unlock_irqrestore(&pmu_lock
, flags
);
2544 xscale1pmu_read_counter(int counter
)
2549 case XSCALE_CYCLE_COUNTER
:
2550 asm volatile("mrc p14, 0, %0, c1, c0, 0" : "=r" (val
));
2552 case XSCALE_COUNTER0
:
2553 asm volatile("mrc p14, 0, %0, c2, c0, 0" : "=r" (val
));
2555 case XSCALE_COUNTER1
:
2556 asm volatile("mrc p14, 0, %0, c3, c0, 0" : "=r" (val
));
2564 xscale1pmu_write_counter(int counter
, u32 val
)
2567 case XSCALE_CYCLE_COUNTER
:
2568 asm volatile("mcr p14, 0, %0, c1, c0, 0" : : "r" (val
));
2570 case XSCALE_COUNTER0
:
2571 asm volatile("mcr p14, 0, %0, c2, c0, 0" : : "r" (val
));
2573 case XSCALE_COUNTER1
:
2574 asm volatile("mcr p14, 0, %0, c3, c0, 0" : : "r" (val
));
2579 static const struct arm_pmu xscale1pmu
= {
2580 .id
= ARM_PERF_PMU_ID_XSCALE1
,
2581 .handle_irq
= xscale1pmu_handle_irq
,
2582 .enable
= xscale1pmu_enable_event
,
2583 .disable
= xscale1pmu_disable_event
,
2584 .read_counter
= xscale1pmu_read_counter
,
2585 .write_counter
= xscale1pmu_write_counter
,
2586 .get_event_idx
= xscale1pmu_get_event_idx
,
2587 .start
= xscale1pmu_start
,
2588 .stop
= xscale1pmu_stop
,
2589 .cache_map
= &xscale_perf_cache_map
,
2590 .event_map
= &xscale_perf_map
,
2591 .raw_event_mask
= 0xFF,
2593 .max_period
= (1LLU << 32) - 1,
2596 const struct arm_pmu
*__init
xscale1pmu_init(void)
2601 #define XSCALE2_OVERFLOWED_MASK 0x01f
2602 #define XSCALE2_CCOUNT_OVERFLOW 0x001
2603 #define XSCALE2_COUNT0_OVERFLOW 0x002
2604 #define XSCALE2_COUNT1_OVERFLOW 0x004
2605 #define XSCALE2_COUNT2_OVERFLOW 0x008
2606 #define XSCALE2_COUNT3_OVERFLOW 0x010
2607 #define XSCALE2_CCOUNT_INT_EN 0x001
2608 #define XSCALE2_COUNT0_INT_EN 0x002
2609 #define XSCALE2_COUNT1_INT_EN 0x004
2610 #define XSCALE2_COUNT2_INT_EN 0x008
2611 #define XSCALE2_COUNT3_INT_EN 0x010
2612 #define XSCALE2_COUNT0_EVT_SHFT 0
2613 #define XSCALE2_COUNT0_EVT_MASK (0xff << XSCALE2_COUNT0_EVT_SHFT)
2614 #define XSCALE2_COUNT1_EVT_SHFT 8
2615 #define XSCALE2_COUNT1_EVT_MASK (0xff << XSCALE2_COUNT1_EVT_SHFT)
2616 #define XSCALE2_COUNT2_EVT_SHFT 16
2617 #define XSCALE2_COUNT2_EVT_MASK (0xff << XSCALE2_COUNT2_EVT_SHFT)
2618 #define XSCALE2_COUNT3_EVT_SHFT 24
2619 #define XSCALE2_COUNT3_EVT_MASK (0xff << XSCALE2_COUNT3_EVT_SHFT)
2622 xscale2pmu_read_pmnc(void)
2625 asm volatile("mrc p14, 0, %0, c0, c1, 0" : "=r" (val
));
2626 /* bits 1-2 and 4-23 are read-unpredictable */
2627 return val
& 0xff000009;
2631 xscale2pmu_write_pmnc(u32 val
)
2633 /* bits 4-23 are write-as-0, 24-31 are write ignored */
2635 asm volatile("mcr p14, 0, %0, c0, c1, 0" : : "r" (val
));
2639 xscale2pmu_read_overflow_flags(void)
2642 asm volatile("mrc p14, 0, %0, c5, c1, 0" : "=r" (val
));
2647 xscale2pmu_write_overflow_flags(u32 val
)
2649 asm volatile("mcr p14, 0, %0, c5, c1, 0" : : "r" (val
));
2653 xscale2pmu_read_event_select(void)
2656 asm volatile("mrc p14, 0, %0, c8, c1, 0" : "=r" (val
));
2661 xscale2pmu_write_event_select(u32 val
)
2663 asm volatile("mcr p14, 0, %0, c8, c1, 0" : : "r"(val
));
2667 xscale2pmu_read_int_enable(void)
2670 asm volatile("mrc p14, 0, %0, c4, c1, 0" : "=r" (val
));
2675 xscale2pmu_write_int_enable(u32 val
)
2677 asm volatile("mcr p14, 0, %0, c4, c1, 0" : : "r" (val
));
2681 xscale2_pmnc_counter_has_overflowed(unsigned long of_flags
,
2682 enum xscale_counters counter
)
2687 case XSCALE_CYCLE_COUNTER
:
2688 ret
= of_flags
& XSCALE2_CCOUNT_OVERFLOW
;
2690 case XSCALE_COUNTER0
:
2691 ret
= of_flags
& XSCALE2_COUNT0_OVERFLOW
;
2693 case XSCALE_COUNTER1
:
2694 ret
= of_flags
& XSCALE2_COUNT1_OVERFLOW
;
2696 case XSCALE_COUNTER2
:
2697 ret
= of_flags
& XSCALE2_COUNT2_OVERFLOW
;
2699 case XSCALE_COUNTER3
:
2700 ret
= of_flags
& XSCALE2_COUNT3_OVERFLOW
;
2703 WARN_ONCE(1, "invalid counter number (%d)\n", counter
);
2710 xscale2pmu_handle_irq(int irq_num
, void *dev
)
2712 unsigned long pmnc
, of_flags
;
2713 struct perf_sample_data data
;
2714 struct cpu_hw_events
*cpuc
;
2715 struct pt_regs
*regs
;
2718 /* Disable the PMU. */
2719 pmnc
= xscale2pmu_read_pmnc();
2720 xscale2pmu_write_pmnc(pmnc
& ~XSCALE_PMU_ENABLE
);
2722 /* Check the overflow flag register. */
2723 of_flags
= xscale2pmu_read_overflow_flags();
2724 if (!(of_flags
& XSCALE2_OVERFLOWED_MASK
))
2727 /* Clear the overflow bits. */
2728 xscale2pmu_write_overflow_flags(of_flags
);
2730 regs
= get_irq_regs();
2732 perf_sample_data_init(&data
, 0);
2734 cpuc
= &__get_cpu_var(cpu_hw_events
);
2735 for (idx
= 0; idx
<= armpmu
->num_events
; ++idx
) {
2736 struct perf_event
*event
= cpuc
->events
[idx
];
2737 struct hw_perf_event
*hwc
;
2739 if (!test_bit(idx
, cpuc
->active_mask
))
2742 if (!xscale2_pmnc_counter_has_overflowed(pmnc
, idx
))
2746 armpmu_event_update(event
, hwc
, idx
);
2747 data
.period
= event
->hw
.last_period
;
2748 if (!armpmu_event_set_period(event
, hwc
, idx
))
2751 if (perf_event_overflow(event
, 0, &data
, regs
))
2752 armpmu
->disable(hwc
, idx
);
2758 * Re-enable the PMU.
2760 pmnc
= xscale2pmu_read_pmnc() | XSCALE_PMU_ENABLE
;
2761 xscale2pmu_write_pmnc(pmnc
);
2767 xscale2pmu_enable_event(struct hw_perf_event
*hwc
, int idx
)
2769 unsigned long flags
, ien
, evtsel
;
2771 ien
= xscale2pmu_read_int_enable();
2772 evtsel
= xscale2pmu_read_event_select();
2775 case XSCALE_CYCLE_COUNTER
:
2776 ien
|= XSCALE2_CCOUNT_INT_EN
;
2778 case XSCALE_COUNTER0
:
2779 ien
|= XSCALE2_COUNT0_INT_EN
;
2780 evtsel
&= ~XSCALE2_COUNT0_EVT_MASK
;
2781 evtsel
|= hwc
->config_base
<< XSCALE2_COUNT0_EVT_SHFT
;
2783 case XSCALE_COUNTER1
:
2784 ien
|= XSCALE2_COUNT1_INT_EN
;
2785 evtsel
&= ~XSCALE2_COUNT1_EVT_MASK
;
2786 evtsel
|= hwc
->config_base
<< XSCALE2_COUNT1_EVT_SHFT
;
2788 case XSCALE_COUNTER2
:
2789 ien
|= XSCALE2_COUNT2_INT_EN
;
2790 evtsel
&= ~XSCALE2_COUNT2_EVT_MASK
;
2791 evtsel
|= hwc
->config_base
<< XSCALE2_COUNT2_EVT_SHFT
;
2793 case XSCALE_COUNTER3
:
2794 ien
|= XSCALE2_COUNT3_INT_EN
;
2795 evtsel
&= ~XSCALE2_COUNT3_EVT_MASK
;
2796 evtsel
|= hwc
->config_base
<< XSCALE2_COUNT3_EVT_SHFT
;
2799 WARN_ONCE(1, "invalid counter number (%d)\n", idx
);
2803 spin_lock_irqsave(&pmu_lock
, flags
);
2804 xscale2pmu_write_event_select(evtsel
);
2805 xscale2pmu_write_int_enable(ien
);
2806 spin_unlock_irqrestore(&pmu_lock
, flags
);
2810 xscale2pmu_disable_event(struct hw_perf_event
*hwc
, int idx
)
2812 unsigned long flags
, ien
, evtsel
;
2814 ien
= xscale2pmu_read_int_enable();
2815 evtsel
= xscale2pmu_read_event_select();
2818 case XSCALE_CYCLE_COUNTER
:
2819 ien
&= ~XSCALE2_CCOUNT_INT_EN
;
2821 case XSCALE_COUNTER0
:
2822 ien
&= ~XSCALE2_COUNT0_INT_EN
;
2823 evtsel
&= ~XSCALE2_COUNT0_EVT_MASK
;
2824 evtsel
|= XSCALE_PERFCTR_UNUSED
<< XSCALE2_COUNT0_EVT_SHFT
;
2826 case XSCALE_COUNTER1
:
2827 ien
&= ~XSCALE2_COUNT1_INT_EN
;
2828 evtsel
&= ~XSCALE2_COUNT1_EVT_MASK
;
2829 evtsel
|= XSCALE_PERFCTR_UNUSED
<< XSCALE2_COUNT1_EVT_SHFT
;
2831 case XSCALE_COUNTER2
:
2832 ien
&= ~XSCALE2_COUNT2_INT_EN
;
2833 evtsel
&= ~XSCALE2_COUNT2_EVT_MASK
;
2834 evtsel
|= XSCALE_PERFCTR_UNUSED
<< XSCALE2_COUNT2_EVT_SHFT
;
2836 case XSCALE_COUNTER3
:
2837 ien
&= ~XSCALE2_COUNT3_INT_EN
;
2838 evtsel
&= ~XSCALE2_COUNT3_EVT_MASK
;
2839 evtsel
|= XSCALE_PERFCTR_UNUSED
<< XSCALE2_COUNT3_EVT_SHFT
;
2842 WARN_ONCE(1, "invalid counter number (%d)\n", idx
);
2846 spin_lock_irqsave(&pmu_lock
, flags
);
2847 xscale2pmu_write_event_select(evtsel
);
2848 xscale2pmu_write_int_enable(ien
);
2849 spin_unlock_irqrestore(&pmu_lock
, flags
);
2853 xscale2pmu_get_event_idx(struct cpu_hw_events
*cpuc
,
2854 struct hw_perf_event
*event
)
2856 int idx
= xscale1pmu_get_event_idx(cpuc
, event
);
2860 if (!test_and_set_bit(XSCALE_COUNTER3
, cpuc
->used_mask
))
2861 idx
= XSCALE_COUNTER3
;
2862 else if (!test_and_set_bit(XSCALE_COUNTER2
, cpuc
->used_mask
))
2863 idx
= XSCALE_COUNTER2
;
2869 xscale2pmu_start(void)
2871 unsigned long flags
, val
;
2873 spin_lock_irqsave(&pmu_lock
, flags
);
2874 val
= xscale2pmu_read_pmnc() & ~XSCALE_PMU_CNT64
;
2875 val
|= XSCALE_PMU_ENABLE
;
2876 xscale2pmu_write_pmnc(val
);
2877 spin_unlock_irqrestore(&pmu_lock
, flags
);
2881 xscale2pmu_stop(void)
2883 unsigned long flags
, val
;
2885 spin_lock_irqsave(&pmu_lock
, flags
);
2886 val
= xscale2pmu_read_pmnc();
2887 val
&= ~XSCALE_PMU_ENABLE
;
2888 xscale2pmu_write_pmnc(val
);
2889 spin_unlock_irqrestore(&pmu_lock
, flags
);
2893 xscale2pmu_read_counter(int counter
)
2898 case XSCALE_CYCLE_COUNTER
:
2899 asm volatile("mrc p14, 0, %0, c1, c1, 0" : "=r" (val
));
2901 case XSCALE_COUNTER0
:
2902 asm volatile("mrc p14, 0, %0, c0, c2, 0" : "=r" (val
));
2904 case XSCALE_COUNTER1
:
2905 asm volatile("mrc p14, 0, %0, c1, c2, 0" : "=r" (val
));
2907 case XSCALE_COUNTER2
:
2908 asm volatile("mrc p14, 0, %0, c2, c2, 0" : "=r" (val
));
2910 case XSCALE_COUNTER3
:
2911 asm volatile("mrc p14, 0, %0, c3, c2, 0" : "=r" (val
));
2919 xscale2pmu_write_counter(int counter
, u32 val
)
2922 case XSCALE_CYCLE_COUNTER
:
2923 asm volatile("mcr p14, 0, %0, c1, c1, 0" : : "r" (val
));
2925 case XSCALE_COUNTER0
:
2926 asm volatile("mcr p14, 0, %0, c0, c2, 0" : : "r" (val
));
2928 case XSCALE_COUNTER1
:
2929 asm volatile("mcr p14, 0, %0, c1, c2, 0" : : "r" (val
));
2931 case XSCALE_COUNTER2
:
2932 asm volatile("mcr p14, 0, %0, c2, c2, 0" : : "r" (val
));
2934 case XSCALE_COUNTER3
:
2935 asm volatile("mcr p14, 0, %0, c3, c2, 0" : : "r" (val
));
2940 static const struct arm_pmu xscale2pmu
= {
2941 .id
= ARM_PERF_PMU_ID_XSCALE2
,
2942 .handle_irq
= xscale2pmu_handle_irq
,
2943 .enable
= xscale2pmu_enable_event
,
2944 .disable
= xscale2pmu_disable_event
,
2945 .read_counter
= xscale2pmu_read_counter
,
2946 .write_counter
= xscale2pmu_write_counter
,
2947 .get_event_idx
= xscale2pmu_get_event_idx
,
2948 .start
= xscale2pmu_start
,
2949 .stop
= xscale2pmu_stop
,
2950 .cache_map
= &xscale_perf_cache_map
,
2951 .event_map
= &xscale_perf_map
,
2952 .raw_event_mask
= 0xFF,
2954 .max_period
= (1LLU << 32) - 1,
2957 const struct arm_pmu
*__init
xscale2pmu_init(void)
2963 init_hw_perf_events(void)
2965 unsigned long cpuid
= read_cpuid_id();
2966 unsigned long implementor
= (cpuid
& 0xFF000000) >> 24;
2967 unsigned long part_number
= (cpuid
& 0xFFF0);
2970 if (0x41 == implementor
) {
2971 switch (part_number
) {
2972 case 0xB360: /* ARM1136 */
2973 case 0xB560: /* ARM1156 */
2974 case 0xB760: /* ARM1176 */
2975 armpmu
= armv6pmu_init();
2977 case 0xB020: /* ARM11mpcore */
2978 armpmu
= armv6mpcore_pmu_init();
2980 case 0xC080: /* Cortex-A8 */
2981 armpmu
= armv7_a8_pmu_init();
2983 case 0xC090: /* Cortex-A9 */
2984 armpmu
= armv7_a9_pmu_init();
2987 /* Intel CPUs [xscale]. */
2988 } else if (0x69 == implementor
) {
2989 part_number
= (cpuid
>> 13) & 0x7;
2990 switch (part_number
) {
2992 armpmu
= xscale1pmu_init();
2995 armpmu
= xscale2pmu_init();
3001 pr_info("enabled with %s PMU driver, %d counters available\n",
3002 arm_pmu_names
[armpmu
->id
], armpmu
->num_events
);
3004 pr_info("no hardware support available\n");
3007 perf_pmu_register(&pmu
);
3011 arch_initcall(init_hw_perf_events
);
3014 * Callchain handling code.
3018 * The registers we're interested in are at the end of the variable
3019 * length saved register structure. The fp points at the end of this
3020 * structure so the address of this struct is:
3021 * (struct frame_tail *)(xxx->fp)-1
3023 * This code has been adapted from the ARM OProfile support.
3026 struct frame_tail
*fp
;
3029 } __attribute__((packed
));
3032 * Get the return address for a single stackframe and return a pointer to the
3035 static struct frame_tail
*
3036 user_backtrace(struct frame_tail
*tail
,
3037 struct perf_callchain_entry
*entry
)
3039 struct frame_tail buftail
;
3041 /* Also check accessibility of one struct frame_tail beyond */
3042 if (!access_ok(VERIFY_READ
, tail
, sizeof(buftail
)))
3044 if (__copy_from_user_inatomic(&buftail
, tail
, sizeof(buftail
)))
3047 perf_callchain_store(entry
, buftail
.lr
);
3050 * Frame pointers should strictly progress back up the stack
3051 * (towards higher addresses).
3053 if (tail
>= buftail
.fp
)
3056 return buftail
.fp
- 1;
3060 perf_callchain_user(struct perf_callchain_entry
*entry
, struct pt_regs
*regs
)
3062 struct frame_tail
*tail
;
3065 tail
= (struct frame_tail
*)regs
->ARM_fp
- 1;
3067 while (tail
&& !((unsigned long)tail
& 0x3))
3068 tail
= user_backtrace(tail
, entry
);
3072 * Gets called by walk_stackframe() for every stackframe. This will be called
3073 * whist unwinding the stackframe and is like a subroutine return so we use
3077 callchain_trace(struct stackframe
*fr
,
3080 struct perf_callchain_entry
*entry
= data
;
3081 perf_callchain_store(entry
, fr
->pc
);
3086 perf_callchain_kernel(struct perf_callchain_entry
*entry
, struct pt_regs
*regs
)
3088 struct stackframe fr
;
3090 fr
.fp
= regs
->ARM_fp
;
3091 fr
.sp
= regs
->ARM_sp
;
3092 fr
.lr
= regs
->ARM_lr
;
3093 fr
.pc
= regs
->ARM_pc
;
3094 walk_stackframe(&fr
, callchain_trace
, entry
);