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,
1244 * ARMv6mpcore is almost identical to single core ARMv6 with the exception
1245 * that some of the events have different enumerations and that there is no
1246 * *hack* to stop the programmable counters. To stop the counters we simply
1247 * disable the interrupt reporting and update the event. When unthrottling we
1248 * reset the period and enable the interrupt reporting.
1250 static const struct arm_pmu armv6mpcore_pmu = {
1251 .id = ARM_PERF_PMU_ID_V6MP,
1252 .handle_irq = armv6pmu_handle_irq,
1253 .enable = armv6pmu_enable_event,
1254 .disable = armv6mpcore_pmu_disable_event,
1255 .read_counter = armv6pmu_read_counter,
1256 .write_counter = armv6pmu_write_counter,
1257 .get_event_idx = armv6pmu_get_event_idx,
1258 .start = armv6pmu_start,
1259 .stop = armv6pmu_stop,
1260 .cache_map = &armv6mpcore_perf_cache_map,
1261 .event_map = &armv6mpcore_perf_map,
1262 .raw_event_mask = 0xFF,
1264 .max_period = (1LLU << 32) - 1,
1268 * ARMv7 Cortex-A8 and Cortex-A9 Performance Events handling code.
1270 * Copied from ARMv6 code, with the low level code inspired
1271 * by the ARMv7 Oprofile code.
1273 * Cortex-A8 has up to 4 configurable performance counters and
1274 * a single cycle counter.
1275 * Cortex-A9 has up to 31 configurable performance counters and
1276 * a single cycle counter.
1278 * All counters can be enabled/disabled and IRQ masked separately. The cycle
1279 * counter and all 4 performance counters together can be reset separately.
1282 /* Common ARMv7 event types */
1283 enum armv7_perf_types {
1284 ARMV7_PERFCTR_PMNC_SW_INCR = 0x00,
1285 ARMV7_PERFCTR_IFETCH_MISS = 0x01,
1286 ARMV7_PERFCTR_ITLB_MISS = 0x02,
1287 ARMV7_PERFCTR_DCACHE_REFILL = 0x03,
1288 ARMV7_PERFCTR_DCACHE_ACCESS = 0x04,
1289 ARMV7_PERFCTR_DTLB_REFILL = 0x05,
1290 ARMV7_PERFCTR_DREAD = 0x06,
1291 ARMV7_PERFCTR_DWRITE = 0x07,
1293 ARMV7_PERFCTR_EXC_TAKEN = 0x09,
1294 ARMV7_PERFCTR_EXC_EXECUTED = 0x0A,
1295 ARMV7_PERFCTR_CID_WRITE = 0x0B,
1296 /* ARMV7_PERFCTR_PC_WRITE is equivalent to HW_BRANCH_INSTRUCTIONS.
1298 * - all branch instructions,
1299 * - instructions that explicitly write the PC,
1300 * - exception generating instructions.
1302 ARMV7_PERFCTR_PC_WRITE = 0x0C,
1303 ARMV7_PERFCTR_PC_IMM_BRANCH = 0x0D,
1304 ARMV7_PERFCTR_UNALIGNED_ACCESS = 0x0F,
1305 ARMV7_PERFCTR_PC_BRANCH_MIS_PRED = 0x10,
1306 ARMV7_PERFCTR_CLOCK_CYCLES = 0x11,
1308 ARMV7_PERFCTR_PC_BRANCH_MIS_USED = 0x12,
1310 ARMV7_PERFCTR_CPU_CYCLES = 0xFF
1313 /* ARMv7 Cortex-A8 specific event types */
1314 enum armv7_a8_perf_types {
1315 ARMV7_PERFCTR_INSTR_EXECUTED = 0x08,
1317 ARMV7_PERFCTR_PC_PROC_RETURN = 0x0E,
1319 ARMV7_PERFCTR_WRITE_BUFFER_FULL = 0x40,
1320 ARMV7_PERFCTR_L2_STORE_MERGED = 0x41,
1321 ARMV7_PERFCTR_L2_STORE_BUFF = 0x42,
1322 ARMV7_PERFCTR_L2_ACCESS = 0x43,
1323 ARMV7_PERFCTR_L2_CACH_MISS = 0x44,
1324 ARMV7_PERFCTR_AXI_READ_CYCLES = 0x45,
1325 ARMV7_PERFCTR_AXI_WRITE_CYCLES = 0x46,
1326 ARMV7_PERFCTR_MEMORY_REPLAY = 0x47,
1327 ARMV7_PERFCTR_UNALIGNED_ACCESS_REPLAY = 0x48,
1328 ARMV7_PERFCTR_L1_DATA_MISS = 0x49,
1329 ARMV7_PERFCTR_L1_INST_MISS = 0x4A,
1330 ARMV7_PERFCTR_L1_DATA_COLORING = 0x4B,
1331 ARMV7_PERFCTR_L1_NEON_DATA = 0x4C,
1332 ARMV7_PERFCTR_L1_NEON_CACH_DATA = 0x4D,
1333 ARMV7_PERFCTR_L2_NEON = 0x4E,
1334 ARMV7_PERFCTR_L2_NEON_HIT = 0x4F,
1335 ARMV7_PERFCTR_L1_INST = 0x50,
1336 ARMV7_PERFCTR_PC_RETURN_MIS_PRED = 0x51,
1337 ARMV7_PERFCTR_PC_BRANCH_FAILED = 0x52,
1338 ARMV7_PERFCTR_PC_BRANCH_TAKEN = 0x53,
1339 ARMV7_PERFCTR_PC_BRANCH_EXECUTED = 0x54,
1340 ARMV7_PERFCTR_OP_EXECUTED = 0x55,
1341 ARMV7_PERFCTR_CYCLES_INST_STALL = 0x56,
1342 ARMV7_PERFCTR_CYCLES_INST = 0x57,
1343 ARMV7_PERFCTR_CYCLES_NEON_DATA_STALL = 0x58,
1344 ARMV7_PERFCTR_CYCLES_NEON_INST_STALL = 0x59,
1345 ARMV7_PERFCTR_NEON_CYCLES = 0x5A,
1347 ARMV7_PERFCTR_PMU0_EVENTS = 0x70,
1348 ARMV7_PERFCTR_PMU1_EVENTS = 0x71,
1349 ARMV7_PERFCTR_PMU_EVENTS = 0x72,
1352 /* ARMv7 Cortex-A9 specific event types */
1353 enum armv7_a9_perf_types {
1354 ARMV7_PERFCTR_JAVA_HW_BYTECODE_EXEC = 0x40,
1355 ARMV7_PERFCTR_JAVA_SW_BYTECODE_EXEC = 0x41,
1356 ARMV7_PERFCTR_JAZELLE_BRANCH_EXEC = 0x42,
1358 ARMV7_PERFCTR_COHERENT_LINE_MISS = 0x50,
1359 ARMV7_PERFCTR_COHERENT_LINE_HIT = 0x51,
1361 ARMV7_PERFCTR_ICACHE_DEP_STALL_CYCLES = 0x60,
1362 ARMV7_PERFCTR_DCACHE_DEP_STALL_CYCLES = 0x61,
1363 ARMV7_PERFCTR_TLB_MISS_DEP_STALL_CYCLES = 0x62,
1364 ARMV7_PERFCTR_STREX_EXECUTED_PASSED = 0x63,
1365 ARMV7_PERFCTR_STREX_EXECUTED_FAILED = 0x64,
1366 ARMV7_PERFCTR_DATA_EVICTION = 0x65,
1367 ARMV7_PERFCTR_ISSUE_STAGE_NO_INST = 0x66,
1368 ARMV7_PERFCTR_ISSUE_STAGE_EMPTY = 0x67,
1369 ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE = 0x68,
1371 ARMV7_PERFCTR_PREDICTABLE_FUNCT_RETURNS = 0x6E,
1373 ARMV7_PERFCTR_MAIN_UNIT_EXECUTED_INST = 0x70,
1374 ARMV7_PERFCTR_SECOND_UNIT_EXECUTED_INST = 0x71,
1375 ARMV7_PERFCTR_LD_ST_UNIT_EXECUTED_INST = 0x72,
1376 ARMV7_PERFCTR_FP_EXECUTED_INST = 0x73,
1377 ARMV7_PERFCTR_NEON_EXECUTED_INST = 0x74,
1379 ARMV7_PERFCTR_PLD_FULL_DEP_STALL_CYCLES = 0x80,
1380 ARMV7_PERFCTR_DATA_WR_DEP_STALL_CYCLES = 0x81,
1381 ARMV7_PERFCTR_ITLB_MISS_DEP_STALL_CYCLES = 0x82,
1382 ARMV7_PERFCTR_DTLB_MISS_DEP_STALL_CYCLES = 0x83,
1383 ARMV7_PERFCTR_MICRO_ITLB_MISS_DEP_STALL_CYCLES = 0x84,
1384 ARMV7_PERFCTR_MICRO_DTLB_MISS_DEP_STALL_CYCLES = 0x85,
1385 ARMV7_PERFCTR_DMB_DEP_STALL_CYCLES = 0x86,
1387 ARMV7_PERFCTR_INTGR_CLK_ENABLED_CYCLES = 0x8A,
1388 ARMV7_PERFCTR_DATA_ENGINE_CLK_EN_CYCLES = 0x8B,
1390 ARMV7_PERFCTR_ISB_INST = 0x90,
1391 ARMV7_PERFCTR_DSB_INST = 0x91,
1392 ARMV7_PERFCTR_DMB_INST = 0x92,
1393 ARMV7_PERFCTR_EXT_INTERRUPTS = 0x93,
1395 ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_COMPLETED = 0xA0,
1396 ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_SKIPPED = 0xA1,
1397 ARMV7_PERFCTR_PLE_FIFO_FLUSH = 0xA2,
1398 ARMV7_PERFCTR_PLE_RQST_COMPLETED = 0xA3,
1399 ARMV7_PERFCTR_PLE_FIFO_OVERFLOW = 0xA4,
1400 ARMV7_PERFCTR_PLE_RQST_PROG = 0xA5
1404 * Cortex-A8 HW events mapping
1406 * The hardware events that we support. We do support cache operations but
1407 * we have harvard caches and no way to combine instruction and data
1408 * accesses/misses in hardware.
1410 static const unsigned armv7_a8_perf_map[PERF_COUNT_HW_MAX] = {
1411 [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
1412 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
1413 [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
1414 [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
1415 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
1416 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1417 [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
1420 static const unsigned armv7_a8_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
1421 [PERF_COUNT_HW_CACHE_OP_MAX]
1422 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
1425 * The performance counters don't differentiate between read
1426 * and write accesses/misses so this isn't strictly correct,
1427 * but it's the best we can do. Writes and reads get
1431 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
1432 [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
1435 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
1436 [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
1438 [C(OP_PREFETCH)] = {
1439 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1440 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1445 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
1446 [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
1449 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
1450 [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
1452 [C(OP_PREFETCH)] = {
1453 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1454 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1459 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
1460 [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
1463 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
1464 [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
1466 [C(OP_PREFETCH)] = {
1467 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1468 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1473 * Only ITLB misses and DTLB refills are supported.
1474 * If users want the DTLB refills misses a raw counter
1478 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1479 [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
1482 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1483 [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
1485 [C(OP_PREFETCH)] = {
1486 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1487 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1492 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1493 [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
1496 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1497 [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
1499 [C(OP_PREFETCH)] = {
1500 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1501 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1506 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
1508 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1511 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
1513 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1515 [C(OP_PREFETCH)] = {
1516 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1517 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1523 * Cortex-A9 HW events mapping
1525 static const unsigned armv7_a9_perf_map[PERF_COUNT_HW_MAX] = {
1526 [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
1527 [PERF_COUNT_HW_INSTRUCTIONS] =
1528 ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE,
1529 [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV7_PERFCTR_COHERENT_LINE_HIT,
1530 [PERF_COUNT_HW_CACHE_MISSES] = ARMV7_PERFCTR_COHERENT_LINE_MISS,
1531 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
1532 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1533 [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
1536 static const unsigned armv7_a9_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
1537 [PERF_COUNT_HW_CACHE_OP_MAX]
1538 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
1541 * The performance counters don't differentiate between read
1542 * and write accesses/misses so this isn't strictly correct,
1543 * but it's the best we can do. Writes and reads get
1547 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
1548 [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
1551 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
1552 [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
1554 [C(OP_PREFETCH)] = {
1555 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1556 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1561 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1562 [C(RESULT_MISS)] = ARMV7_PERFCTR_IFETCH_MISS,
1565 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1566 [C(RESULT_MISS)] = ARMV7_PERFCTR_IFETCH_MISS,
1568 [C(OP_PREFETCH)] = {
1569 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1570 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1575 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1576 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1579 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1580 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1582 [C(OP_PREFETCH)] = {
1583 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1584 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1589 * Only ITLB misses and DTLB refills are supported.
1590 * If users want the DTLB refills misses a raw counter
1594 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1595 [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
1598 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1599 [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
1601 [C(OP_PREFETCH)] = {
1602 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1603 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1608 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1609 [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
1612 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1613 [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
1615 [C(OP_PREFETCH)] = {
1616 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1617 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1622 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
1624 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1627 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
1629 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1631 [C(OP_PREFETCH)] = {
1632 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1633 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1639 * Perf Events counters
1641 enum armv7_counters {
1642 ARMV7_CYCLE_COUNTER = 1, /* Cycle counter */
1643 ARMV7_COUNTER0 = 2, /* First event counter */
1647 * The cycle counter is ARMV7_CYCLE_COUNTER.
1648 * The first event counter is ARMV7_COUNTER0.
1649 * The last event counter is (ARMV7_COUNTER0 + armpmu->num_events - 1).
1651 #define ARMV7_COUNTER_LAST (ARMV7_COUNTER0 + armpmu->num_events - 1)
1654 * ARMv7 low level PMNC access
1658 * Per-CPU PMNC: config reg
1660 #define ARMV7_PMNC_E (1 << 0) /* Enable all counters */
1661 #define ARMV7_PMNC_P (1 << 1) /* Reset all counters */
1662 #define ARMV7_PMNC_C (1 << 2) /* Cycle counter reset */
1663 #define ARMV7_PMNC_D (1 << 3) /* CCNT counts every 64th cpu cycle */
1664 #define ARMV7_PMNC_X (1 << 4) /* Export to ETM */
1665 #define ARMV7_PMNC_DP (1 << 5) /* Disable CCNT if non-invasive debug*/
1666 #define ARMV7_PMNC_N_SHIFT 11 /* Number of counters supported */
1667 #define ARMV7_PMNC_N_MASK 0x1f
1668 #define ARMV7_PMNC_MASK 0x3f /* Mask for writable bits */
1671 * Available counters
1673 #define ARMV7_CNT0 0 /* First event counter */
1674 #define ARMV7_CCNT 31 /* Cycle counter */
1676 /* Perf Event to low level counters mapping */
1677 #define ARMV7_EVENT_CNT_TO_CNTx (ARMV7_COUNTER0 - ARMV7_CNT0)
1680 * CNTENS: counters enable reg
1682 #define ARMV7_CNTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1683 #define ARMV7_CNTENS_C (1 << ARMV7_CCNT)
1686 * CNTENC: counters disable reg
1688 #define ARMV7_CNTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1689 #define ARMV7_CNTENC_C (1 << ARMV7_CCNT)
1692 * INTENS: counters overflow interrupt enable reg
1694 #define ARMV7_INTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1695 #define ARMV7_INTENS_C (1 << ARMV7_CCNT)
1698 * INTENC: counters overflow interrupt disable reg
1700 #define ARMV7_INTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1701 #define ARMV7_INTENC_C (1 << ARMV7_CCNT)
1704 * EVTSEL: Event selection reg
1706 #define ARMV7_EVTSEL_MASK 0xff /* Mask for writable bits */
1709 * SELECT: Counter selection reg
1711 #define ARMV7_SELECT_MASK 0x1f /* Mask for writable bits */
1714 * FLAG: counters overflow flag status reg
1716 #define ARMV7_FLAG_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1717 #define ARMV7_FLAG_C (1 << ARMV7_CCNT)
1718 #define ARMV7_FLAG_MASK 0xffffffff /* Mask for writable bits */
1719 #define ARMV7_OVERFLOWED_MASK ARMV7_FLAG_MASK
1721 static inline unsigned long armv7_pmnc_read(void)
1724 asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r"(val));
1728 static inline void armv7_pmnc_write(unsigned long val)
1730 val &= ARMV7_PMNC_MASK;
1731 asm volatile("mcr p15, 0, %0, c9, c12, 0" : : "r"(val));
1734 static inline int armv7_pmnc_has_overflowed(unsigned long pmnc)
1736 return pmnc & ARMV7_OVERFLOWED_MASK;
1739 static inline int armv7_pmnc_counter_has_overflowed(unsigned long pmnc,
1740 enum armv7_counters counter)
1744 if (counter == ARMV7_CYCLE_COUNTER)
1745 ret = pmnc & ARMV7_FLAG_C;
1746 else if ((counter >= ARMV7_COUNTER0) && (counter <= ARMV7_COUNTER_LAST))
1747 ret = pmnc & ARMV7_FLAG_P(counter);
1749 pr_err("CPU%u checking wrong counter %d overflow status\n",
1750 smp_processor_id(), counter);
1755 static inline int armv7_pmnc_select_counter(unsigned int idx)
1759 if ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST)) {
1760 pr_err("CPU%u selecting wrong PMNC counter"
1761 " %d\n", smp_processor_id(), idx);
1765 val = (idx - ARMV7_EVENT_CNT_TO_CNTx) & ARMV7_SELECT_MASK;
1766 asm volatile("mcr p15, 0, %0, c9, c12, 5" : : "r" (val));
1771 static inline u32 armv7pmu_read_counter(int idx)
1773 unsigned long value = 0;
1775 if (idx == ARMV7_CYCLE_COUNTER)
1776 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (value));
1777 else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) {
1778 if (armv7_pmnc_select_counter(idx) == idx)
1779 asm volatile("mrc p15, 0, %0, c9, c13, 2"
1782 pr_err("CPU%u reading wrong counter %d\n",
1783 smp_processor_id(), idx);
1788 static inline void armv7pmu_write_counter(int idx, u32 value)
1790 if (idx == ARMV7_CYCLE_COUNTER)
1791 asm volatile("mcr p15, 0, %0, c9, c13, 0" : : "r" (value));
1792 else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) {
1793 if (armv7_pmnc_select_counter(idx) == idx)
1794 asm volatile("mcr p15, 0, %0, c9, c13, 2"
1797 pr_err("CPU%u writing wrong counter %d\n",
1798 smp_processor_id(), idx);
1801 static inline void armv7_pmnc_write_evtsel(unsigned int idx, u32 val)
1803 if (armv7_pmnc_select_counter(idx) == idx) {
1804 val &= ARMV7_EVTSEL_MASK;
1805 asm volatile("mcr p15, 0, %0, c9, c13, 1" : : "r" (val));
1809 static inline u32 armv7_pmnc_enable_counter(unsigned int idx)
1813 if ((idx != ARMV7_CYCLE_COUNTER) &&
1814 ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
1815 pr_err("CPU%u enabling wrong PMNC counter"
1816 " %d\n", smp_processor_id(), idx);
1820 if (idx == ARMV7_CYCLE_COUNTER)
1821 val = ARMV7_CNTENS_C;
1823 val = ARMV7_CNTENS_P(idx);
1825 asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r" (val));
1830 static inline u32 armv7_pmnc_disable_counter(unsigned int idx)
1835 if ((idx != ARMV7_CYCLE_COUNTER) &&
1836 ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
1837 pr_err("CPU%u disabling wrong PMNC counter"
1838 " %d\n", smp_processor_id(), idx);
1842 if (idx == ARMV7_CYCLE_COUNTER)
1843 val = ARMV7_CNTENC_C;
1845 val = ARMV7_CNTENC_P(idx);
1847 asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r" (val));
1852 static inline u32 armv7_pmnc_enable_intens(unsigned int idx)
1856 if ((idx != ARMV7_CYCLE_COUNTER) &&
1857 ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
1858 pr_err("CPU%u enabling wrong PMNC counter"
1859 " interrupt enable %d\n", smp_processor_id(), idx);
1863 if (idx == ARMV7_CYCLE_COUNTER)
1864 val = ARMV7_INTENS_C;
1866 val = ARMV7_INTENS_P(idx);
1868 asm volatile("mcr p15, 0, %0, c9, c14, 1" : : "r" (val));
1873 static inline u32 armv7_pmnc_disable_intens(unsigned int idx)
1877 if ((idx != ARMV7_CYCLE_COUNTER) &&
1878 ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
1879 pr_err("CPU%u disabling wrong PMNC counter"
1880 " interrupt enable %d\n", smp_processor_id(), idx);
1884 if (idx == ARMV7_CYCLE_COUNTER)
1885 val = ARMV7_INTENC_C;
1887 val = ARMV7_INTENC_P(idx);
1889 asm volatile("mcr p15, 0, %0, c9, c14, 2" : : "r" (val));
1894 static inline u32 armv7_pmnc_getreset_flags(void)
1899 asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
1901 /* Write to clear flags */
1902 val &= ARMV7_FLAG_MASK;
1903 asm volatile("mcr p15, 0, %0, c9, c12, 3" : : "r" (val));
1909 static void armv7_pmnc_dump_regs(void)
1914 printk(KERN_INFO "PMNC registers dump:\n");
1916 asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r" (val));
1917 printk(KERN_INFO "PMNC =0x%08x\n", val);
1919 asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r" (val));
1920 printk(KERN_INFO "CNTENS=0x%08x\n", val);
1922 asm volatile("mrc p15, 0, %0, c9, c14, 1" : "=r" (val));
1923 printk(KERN_INFO "INTENS=0x%08x\n", val);
1925 asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
1926 printk(KERN_INFO "FLAGS =0x%08x\n", val);
1928 asm volatile("mrc p15, 0, %0, c9, c12, 5" : "=r" (val));
1929 printk(KERN_INFO "SELECT=0x%08x\n", val);
1931 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (val));
1932 printk(KERN_INFO "CCNT =0x%08x\n", val);
1934 for (cnt = ARMV7_COUNTER0; cnt < ARMV7_COUNTER_LAST; cnt++) {
1935 armv7_pmnc_select_counter(cnt);
1936 asm volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (val));
1937 printk(KERN_INFO "CNT[%d] count =0x%08x\n",
1938 cnt-ARMV7_EVENT_CNT_TO_CNTx, val);
1939 asm volatile("mrc p15, 0, %0, c9, c13, 1" : "=r" (val));
1940 printk(KERN_INFO "CNT[%d] evtsel=0x%08x\n",
1941 cnt-ARMV7_EVENT_CNT_TO_CNTx, val);
1946 void armv7pmu_enable_event(struct hw_perf_event *hwc, int idx)
1948 unsigned long flags;
1951 * Enable counter and interrupt, and set the counter to count
1952 * the event that we're interested in.
1954 spin_lock_irqsave(&pmu_lock, flags);
1959 armv7_pmnc_disable_counter(idx);
1962 * Set event (if destined for PMNx counters)
1963 * We don't need to set the event if it's a cycle count
1965 if (idx != ARMV7_CYCLE_COUNTER)
1966 armv7_pmnc_write_evtsel(idx, hwc->config_base);
1969 * Enable interrupt for this counter
1971 armv7_pmnc_enable_intens(idx);
1976 armv7_pmnc_enable_counter(idx);
1978 spin_unlock_irqrestore(&pmu_lock, flags);
1981 static void armv7pmu_disable_event(struct hw_perf_event *hwc, int idx)
1983 unsigned long flags;
1986 * Disable counter and interrupt
1988 spin_lock_irqsave(&pmu_lock, flags);
1993 armv7_pmnc_disable_counter(idx);
1996 * Disable interrupt for this counter
1998 armv7_pmnc_disable_intens(idx);
2000 spin_unlock_irqrestore(&pmu_lock, flags);
2003 static irqreturn_t armv7pmu_handle_irq(int irq_num, void *dev)
2006 struct perf_sample_data data;
2007 struct cpu_hw_events *cpuc;
2008 struct pt_regs *regs;
2012 * Get and reset the IRQ flags
2014 pmnc = armv7_pmnc_getreset_flags();
2017 * Did an overflow occur?
2019 if (!armv7_pmnc_has_overflowed(pmnc))
2023 * Handle the counter(s) overflow(s)
2025 regs = get_irq_regs();
2027 perf_sample_data_init(&data, 0);
2029 cpuc = &__get_cpu_var(cpu_hw_events);
2030 for (idx = 0; idx <= armpmu->num_events; ++idx) {
2031 struct perf_event *event = cpuc->events[idx];
2032 struct hw_perf_event *hwc;
2034 if (!test_bit(idx, cpuc->active_mask))
2038 * We have a single interrupt for all counters. Check that
2039 * each counter has overflowed before we process it.
2041 if (!armv7_pmnc_counter_has_overflowed(pmnc, idx))
2045 armpmu_event_update(event, hwc, idx);
2046 data.period = event->hw.last_period;
2047 if (!armpmu_event_set_period(event, hwc, idx))
2050 if (perf_event_overflow(event, 0, &data, regs))
2051 armpmu->disable(hwc, idx);
2055 * Handle the pending perf events.
2057 * Note: this call *must* be run with interrupts disabled. For
2058 * platforms that can have the PMU interrupts raised as an NMI, this
2066 static void armv7pmu_start(void)
2068 unsigned long flags;
2070 spin_lock_irqsave(&pmu_lock, flags);
2071 /* Enable all counters */
2072 armv7_pmnc_write(armv7_pmnc_read() | ARMV7_PMNC_E);
2073 spin_unlock_irqrestore(&pmu_lock, flags);
2076 static void armv7pmu_stop(void)
2078 unsigned long flags;
2080 spin_lock_irqsave(&pmu_lock, flags);
2081 /* Disable all counters */
2082 armv7_pmnc_write(armv7_pmnc_read() & ~ARMV7_PMNC_E);
2083 spin_unlock_irqrestore(&pmu_lock, flags);
2086 static int armv7pmu_get_event_idx(struct cpu_hw_events *cpuc,
2087 struct hw_perf_event *event)
2091 /* Always place a cycle counter into the cycle counter. */
2092 if (event->config_base == ARMV7_PERFCTR_CPU_CYCLES) {
2093 if (test_and_set_bit(ARMV7_CYCLE_COUNTER, cpuc->used_mask))
2096 return ARMV7_CYCLE_COUNTER;
2099 * For anything other than a cycle counter, try and use
2100 * the events counters
2102 for (idx = ARMV7_COUNTER0; idx <= armpmu->num_events; ++idx) {
2103 if (!test_and_set_bit(idx, cpuc->used_mask))
2107 /* The counters are all in use. */
2112 static struct arm_pmu armv7pmu = {
2113 .handle_irq = armv7pmu_handle_irq,
2114 .enable = armv7pmu_enable_event,
2115 .disable = armv7pmu_disable_event,
2116 .read_counter = armv7pmu_read_counter,
2117 .write_counter = armv7pmu_write_counter,
2118 .get_event_idx = armv7pmu_get_event_idx,
2119 .start = armv7pmu_start,
2120 .stop = armv7pmu_stop,
2121 .raw_event_mask = 0xFF,
2122 .max_period = (1LLU << 32) - 1,
2125 static u32 __init armv7_reset_read_pmnc(void)
2129 /* Initialize & Reset PMNC: C and P bits */
2130 armv7_pmnc_write(ARMV7_PMNC_P | ARMV7_PMNC_C);
2132 /* Read the nb of CNTx counters supported from PMNC */
2133 nb_cnt = (armv7_pmnc_read() >> ARMV7_PMNC_N_SHIFT) & ARMV7_PMNC_N_MASK;
2135 /* Add the CPU cycles counter and return */
2140 * ARMv5 [xscale] Performance counter handling code.
2142 * Based on xscale OProfile code.
2144 * There are two variants of the xscale PMU that we support:
2145 * - xscale1pmu: 2 event counters and a cycle counter
2146 * - xscale2pmu: 4 event counters and a cycle counter
2147 * The two variants share event definitions, but have different
2151 enum xscale_perf_types {
2152 XSCALE_PERFCTR_ICACHE_MISS = 0x00,
2153 XSCALE_PERFCTR_ICACHE_NO_DELIVER = 0x01,
2154 XSCALE_PERFCTR_DATA_STALL = 0x02,
2155 XSCALE_PERFCTR_ITLB_MISS = 0x03,
2156 XSCALE_PERFCTR_DTLB_MISS = 0x04,
2157 XSCALE_PERFCTR_BRANCH = 0x05,
2158 XSCALE_PERFCTR_BRANCH_MISS = 0x06,
2159 XSCALE_PERFCTR_INSTRUCTION = 0x07,
2160 XSCALE_PERFCTR_DCACHE_FULL_STALL = 0x08,
2161 XSCALE_PERFCTR_DCACHE_FULL_STALL_CONTIG = 0x09,
2162 XSCALE_PERFCTR_DCACHE_ACCESS = 0x0A,
2163 XSCALE_PERFCTR_DCACHE_MISS = 0x0B,
2164 XSCALE_PERFCTR_DCACHE_WRITE_BACK = 0x0C,
2165 XSCALE_PERFCTR_PC_CHANGED = 0x0D,
2166 XSCALE_PERFCTR_BCU_REQUEST = 0x10,
2167 XSCALE_PERFCTR_BCU_FULL = 0x11,
2168 XSCALE_PERFCTR_BCU_DRAIN = 0x12,
2169 XSCALE_PERFCTR_BCU_ECC_NO_ELOG = 0x14,
2170 XSCALE_PERFCTR_BCU_1_BIT_ERR = 0x15,
2171 XSCALE_PERFCTR_RMW = 0x16,
2172 /* XSCALE_PERFCTR_CCNT is not hardware defined */
2173 XSCALE_PERFCTR_CCNT = 0xFE,
2174 XSCALE_PERFCTR_UNUSED = 0xFF,
2177 enum xscale_counters {
2178 XSCALE_CYCLE_COUNTER = 1,
2185 static const unsigned xscale_perf_map[PERF_COUNT_HW_MAX] = {
2186 [PERF_COUNT_HW_CPU_CYCLES] = XSCALE_PERFCTR_CCNT,
2187 [PERF_COUNT_HW_INSTRUCTIONS] = XSCALE_PERFCTR_INSTRUCTION,
2188 [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
2189 [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
2190 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = XSCALE_PERFCTR_BRANCH,
2191 [PERF_COUNT_HW_BRANCH_MISSES] = XSCALE_PERFCTR_BRANCH_MISS,
2192 [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
2195 static const unsigned xscale_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
2196 [PERF_COUNT_HW_CACHE_OP_MAX]
2197 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
2200 [C(RESULT_ACCESS)] = XSCALE_PERFCTR_DCACHE_ACCESS,
2201 [C(RESULT_MISS)] = XSCALE_PERFCTR_DCACHE_MISS,
2204 [C(RESULT_ACCESS)] = XSCALE_PERFCTR_DCACHE_ACCESS,
2205 [C(RESULT_MISS)] = XSCALE_PERFCTR_DCACHE_MISS,
2207 [C(OP_PREFETCH)] = {
2208 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2209 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2214 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2215 [C(RESULT_MISS)] = XSCALE_PERFCTR_ICACHE_MISS,
2218 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2219 [C(RESULT_MISS)] = XSCALE_PERFCTR_ICACHE_MISS,
2221 [C(OP_PREFETCH)] = {
2222 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2223 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2228 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2229 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2232 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2233 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2235 [C(OP_PREFETCH)] = {
2236 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2237 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2242 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2243 [C(RESULT_MISS)] = XSCALE_PERFCTR_DTLB_MISS,
2246 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2247 [C(RESULT_MISS)] = XSCALE_PERFCTR_DTLB_MISS,
2249 [C(OP_PREFETCH)] = {
2250 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2251 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2256 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2257 [C(RESULT_MISS)] = XSCALE_PERFCTR_ITLB_MISS,
2260 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2261 [C(RESULT_MISS)] = XSCALE_PERFCTR_ITLB_MISS,
2263 [C(OP_PREFETCH)] = {
2264 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2265 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2270 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2271 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2274 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2275 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2277 [C(OP_PREFETCH)] = {
2278 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2279 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2284 #define XSCALE_PMU_ENABLE 0x001
2285 #define XSCALE_PMN_RESET 0x002
2286 #define XSCALE_CCNT_RESET 0x004
2287 #define XSCALE_PMU_RESET (CCNT_RESET | PMN_RESET)
2288 #define XSCALE_PMU_CNT64 0x008
2290 #define XSCALE1_OVERFLOWED_MASK 0x700
2291 #define XSCALE1_CCOUNT_OVERFLOW 0x400
2292 #define XSCALE1_COUNT0_OVERFLOW 0x100
2293 #define XSCALE1_COUNT1_OVERFLOW 0x200
2294 #define XSCALE1_CCOUNT_INT_EN 0x040
2295 #define XSCALE1_COUNT0_INT_EN 0x010
2296 #define XSCALE1_COUNT1_INT_EN 0x020
2297 #define XSCALE1_COUNT0_EVT_SHFT 12
2298 #define XSCALE1_COUNT0_EVT_MASK (0xff << XSCALE1_COUNT0_EVT_SHFT)
2299 #define XSCALE1_COUNT1_EVT_SHFT 20
2300 #define XSCALE1_COUNT1_EVT_MASK (0xff << XSCALE1_COUNT1_EVT_SHFT)
2303 xscale1pmu_read_pmnc(void)
2306 asm volatile("mrc p14, 0, %0, c0, c0, 0" : "=r" (val));
2311 xscale1pmu_write_pmnc(u32 val)
2313 /* upper 4bits and 7, 11 are write-as-0 */
2315 asm volatile("mcr p14, 0, %0, c0, c0, 0" : : "r" (val));
2319 xscale1_pmnc_counter_has_overflowed(unsigned long pmnc,
2320 enum xscale_counters counter)
2325 case XSCALE_CYCLE_COUNTER:
2326 ret = pmnc & XSCALE1_CCOUNT_OVERFLOW;
2328 case XSCALE_COUNTER0:
2329 ret = pmnc & XSCALE1_COUNT0_OVERFLOW;
2331 case XSCALE_COUNTER1:
2332 ret = pmnc & XSCALE1_COUNT1_OVERFLOW;
2335 WARN_ONCE(1, "invalid counter number (%d)\n", counter);
2342 xscale1pmu_handle_irq(int irq_num, void *dev)
2345 struct perf_sample_data data;
2346 struct cpu_hw_events *cpuc;
2347 struct pt_regs *regs;
2351 * NOTE: there's an A stepping erratum that states if an overflow
2352 * bit already exists and another occurs, the previous
2353 * Overflow bit gets cleared. There's no workaround.
2354 * Fixed in B stepping or later.
2356 pmnc = xscale1pmu_read_pmnc();
2359 * Write the value back to clear the overflow flags. Overflow
2360 * flags remain in pmnc for use below. We also disable the PMU
2361 * while we process the interrupt.
2363 xscale1pmu_write_pmnc(pmnc & ~XSCALE_PMU_ENABLE);
2365 if (!(pmnc & XSCALE1_OVERFLOWED_MASK))
2368 regs = get_irq_regs();
2370 perf_sample_data_init(&data, 0);
2372 cpuc = &__get_cpu_var(cpu_hw_events);
2373 for (idx = 0; idx <= armpmu->num_events; ++idx) {
2374 struct perf_event *event = cpuc->events[idx];
2375 struct hw_perf_event *hwc;
2377 if (!test_bit(idx, cpuc->active_mask))
2380 if (!xscale1_pmnc_counter_has_overflowed(pmnc, idx))
2384 armpmu_event_update(event, hwc, idx);
2385 data.period = event->hw.last_period;
2386 if (!armpmu_event_set_period(event, hwc, idx))
2389 if (perf_event_overflow(event, 0, &data, regs))
2390 armpmu->disable(hwc, idx);
2396 * Re-enable the PMU.
2398 pmnc = xscale1pmu_read_pmnc() | XSCALE_PMU_ENABLE;
2399 xscale1pmu_write_pmnc(pmnc);
2405 xscale1pmu_enable_event(struct hw_perf_event *hwc, int idx)
2407 unsigned long val, mask, evt, flags;
2410 case XSCALE_CYCLE_COUNTER:
2412 evt = XSCALE1_CCOUNT_INT_EN;
2414 case XSCALE_COUNTER0:
2415 mask = XSCALE1_COUNT0_EVT_MASK;
2416 evt = (hwc->config_base << XSCALE1_COUNT0_EVT_SHFT) |
2417 XSCALE1_COUNT0_INT_EN;
2419 case XSCALE_COUNTER1:
2420 mask = XSCALE1_COUNT1_EVT_MASK;
2421 evt = (hwc->config_base << XSCALE1_COUNT1_EVT_SHFT) |
2422 XSCALE1_COUNT1_INT_EN;
2425 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
2429 spin_lock_irqsave(&pmu_lock, flags);
2430 val = xscale1pmu_read_pmnc();
2433 xscale1pmu_write_pmnc(val);
2434 spin_unlock_irqrestore(&pmu_lock, flags);
2438 xscale1pmu_disable_event(struct hw_perf_event *hwc, int idx)
2440 unsigned long val, mask, evt, flags;
2443 case XSCALE_CYCLE_COUNTER:
2444 mask = XSCALE1_CCOUNT_INT_EN;
2447 case XSCALE_COUNTER0:
2448 mask = XSCALE1_COUNT0_INT_EN | XSCALE1_COUNT0_EVT_MASK;
2449 evt = XSCALE_PERFCTR_UNUSED << XSCALE1_COUNT0_EVT_SHFT;
2451 case XSCALE_COUNTER1:
2452 mask = XSCALE1_COUNT1_INT_EN | XSCALE1_COUNT1_EVT_MASK;
2453 evt = XSCALE_PERFCTR_UNUSED << XSCALE1_COUNT1_EVT_SHFT;
2456 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
2460 spin_lock_irqsave(&pmu_lock, flags);
2461 val = xscale1pmu_read_pmnc();
2464 xscale1pmu_write_pmnc(val);
2465 spin_unlock_irqrestore(&pmu_lock, flags);
2469 xscale1pmu_get_event_idx(struct cpu_hw_events *cpuc,
2470 struct hw_perf_event *event)
2472 if (XSCALE_PERFCTR_CCNT == event->config_base) {
2473 if (test_and_set_bit(XSCALE_CYCLE_COUNTER, cpuc->used_mask))
2476 return XSCALE_CYCLE_COUNTER;
2478 if (!test_and_set_bit(XSCALE_COUNTER1, cpuc->used_mask)) {
2479 return XSCALE_COUNTER1;
2482 if (!test_and_set_bit(XSCALE_COUNTER0, cpuc->used_mask)) {
2483 return XSCALE_COUNTER0;
2491 xscale1pmu_start(void)
2493 unsigned long flags, val;
2495 spin_lock_irqsave(&pmu_lock, flags);
2496 val = xscale1pmu_read_pmnc();
2497 val |= XSCALE_PMU_ENABLE;
2498 xscale1pmu_write_pmnc(val);
2499 spin_unlock_irqrestore(&pmu_lock, flags);
2503 xscale1pmu_stop(void)
2505 unsigned long flags, val;
2507 spin_lock_irqsave(&pmu_lock, flags);
2508 val = xscale1pmu_read_pmnc();
2509 val &= ~XSCALE_PMU_ENABLE;
2510 xscale1pmu_write_pmnc(val);
2511 spin_unlock_irqrestore(&pmu_lock, flags);
2515 xscale1pmu_read_counter(int counter)
2520 case XSCALE_CYCLE_COUNTER:
2521 asm volatile("mrc p14, 0, %0, c1, c0, 0" : "=r" (val));
2523 case XSCALE_COUNTER0:
2524 asm volatile("mrc p14, 0, %0, c2, c0, 0" : "=r" (val));
2526 case XSCALE_COUNTER1:
2527 asm volatile("mrc p14, 0, %0, c3, c0, 0" : "=r" (val));
2535 xscale1pmu_write_counter(int counter, u32 val)
2538 case XSCALE_CYCLE_COUNTER:
2539 asm volatile("mcr p14, 0, %0, c1, c0, 0" : : "r" (val));
2541 case XSCALE_COUNTER0:
2542 asm volatile("mcr p14, 0, %0, c2, c0, 0" : : "r" (val));
2544 case XSCALE_COUNTER1:
2545 asm volatile("mcr p14, 0, %0, c3, c0, 0" : : "r" (val));
2550 static const struct arm_pmu xscale1pmu = {
2551 .id = ARM_PERF_PMU_ID_XSCALE1,
2552 .handle_irq = xscale1pmu_handle_irq,
2553 .enable = xscale1pmu_enable_event,
2554 .disable = xscale1pmu_disable_event,
2555 .read_counter = xscale1pmu_read_counter,
2556 .write_counter = xscale1pmu_write_counter,
2557 .get_event_idx = xscale1pmu_get_event_idx,
2558 .start = xscale1pmu_start,
2559 .stop = xscale1pmu_stop,
2560 .cache_map = &xscale_perf_cache_map,
2561 .event_map = &xscale_perf_map,
2562 .raw_event_mask = 0xFF,
2564 .max_period = (1LLU << 32) - 1,
2567 #define XSCALE2_OVERFLOWED_MASK 0x01f
2568 #define XSCALE2_CCOUNT_OVERFLOW 0x001
2569 #define XSCALE2_COUNT0_OVERFLOW 0x002
2570 #define XSCALE2_COUNT1_OVERFLOW 0x004
2571 #define XSCALE2_COUNT2_OVERFLOW 0x008
2572 #define XSCALE2_COUNT3_OVERFLOW 0x010
2573 #define XSCALE2_CCOUNT_INT_EN 0x001
2574 #define XSCALE2_COUNT0_INT_EN 0x002
2575 #define XSCALE2_COUNT1_INT_EN 0x004
2576 #define XSCALE2_COUNT2_INT_EN 0x008
2577 #define XSCALE2_COUNT3_INT_EN 0x010
2578 #define XSCALE2_COUNT0_EVT_SHFT 0
2579 #define XSCALE2_COUNT0_EVT_MASK (0xff << XSCALE2_COUNT0_EVT_SHFT)
2580 #define XSCALE2_COUNT1_EVT_SHFT 8
2581 #define XSCALE2_COUNT1_EVT_MASK (0xff << XSCALE2_COUNT1_EVT_SHFT)
2582 #define XSCALE2_COUNT2_EVT_SHFT 16
2583 #define XSCALE2_COUNT2_EVT_MASK (0xff << XSCALE2_COUNT2_EVT_SHFT)
2584 #define XSCALE2_COUNT3_EVT_SHFT 24
2585 #define XSCALE2_COUNT3_EVT_MASK (0xff << XSCALE2_COUNT3_EVT_SHFT)
2588 xscale2pmu_read_pmnc(void)
2591 asm volatile("mrc p14, 0, %0, c0, c1, 0" : "=r" (val));
2592 /* bits 1-2 and 4-23 are read-unpredictable */
2593 return val & 0xff000009;
2597 xscale2pmu_write_pmnc(u32 val)
2599 /* bits 4-23 are write-as-0, 24-31 are write ignored */
2601 asm volatile("mcr p14, 0, %0, c0, c1, 0" : : "r" (val));
2605 xscale2pmu_read_overflow_flags(void)
2608 asm volatile("mrc p14, 0, %0, c5, c1, 0" : "=r" (val));
2613 xscale2pmu_write_overflow_flags(u32 val)
2615 asm volatile("mcr p14, 0, %0, c5, c1, 0" : : "r" (val));
2619 xscale2pmu_read_event_select(void)
2622 asm volatile("mrc p14, 0, %0, c8, c1, 0" : "=r" (val));
2627 xscale2pmu_write_event_select(u32 val)
2629 asm volatile("mcr p14, 0, %0, c8, c1, 0" : : "r"(val));
2633 xscale2pmu_read_int_enable(void)
2636 asm volatile("mrc p14, 0, %0, c4, c1, 0" : "=r" (val));
2641 xscale2pmu_write_int_enable(u32 val)
2643 asm volatile("mcr p14, 0, %0, c4, c1, 0" : : "r" (val));
2647 xscale2_pmnc_counter_has_overflowed(unsigned long of_flags,
2648 enum xscale_counters counter)
2653 case XSCALE_CYCLE_COUNTER:
2654 ret = of_flags & XSCALE2_CCOUNT_OVERFLOW;
2656 case XSCALE_COUNTER0:
2657 ret = of_flags & XSCALE2_COUNT0_OVERFLOW;
2659 case XSCALE_COUNTER1:
2660 ret = of_flags & XSCALE2_COUNT1_OVERFLOW;
2662 case XSCALE_COUNTER2:
2663 ret = of_flags & XSCALE2_COUNT2_OVERFLOW;
2665 case XSCALE_COUNTER3:
2666 ret = of_flags & XSCALE2_COUNT3_OVERFLOW;
2669 WARN_ONCE(1, "invalid counter number (%d)\n", counter);
2676 xscale2pmu_handle_irq(int irq_num, void *dev)
2678 unsigned long pmnc, of_flags;
2679 struct perf_sample_data data;
2680 struct cpu_hw_events *cpuc;
2681 struct pt_regs *regs;
2684 /* Disable the PMU. */
2685 pmnc = xscale2pmu_read_pmnc();
2686 xscale2pmu_write_pmnc(pmnc & ~XSCALE_PMU_ENABLE);
2688 /* Check the overflow flag register. */
2689 of_flags = xscale2pmu_read_overflow_flags();
2690 if (!(of_flags & XSCALE2_OVERFLOWED_MASK))
2693 /* Clear the overflow bits. */
2694 xscale2pmu_write_overflow_flags(of_flags);
2696 regs = get_irq_regs();
2698 perf_sample_data_init(&data, 0);
2700 cpuc = &__get_cpu_var(cpu_hw_events);
2701 for (idx = 0; idx <= armpmu->num_events; ++idx) {
2702 struct perf_event *event = cpuc->events[idx];
2703 struct hw_perf_event *hwc;
2705 if (!test_bit(idx, cpuc->active_mask))
2708 if (!xscale2_pmnc_counter_has_overflowed(pmnc, idx))
2712 armpmu_event_update(event, hwc, idx);
2713 data.period = event->hw.last_period;
2714 if (!armpmu_event_set_period(event, hwc, idx))
2717 if (perf_event_overflow(event, 0, &data, regs))
2718 armpmu->disable(hwc, idx);
2724 * Re-enable the PMU.
2726 pmnc = xscale2pmu_read_pmnc() | XSCALE_PMU_ENABLE;
2727 xscale2pmu_write_pmnc(pmnc);
2733 xscale2pmu_enable_event(struct hw_perf_event *hwc, int idx)
2735 unsigned long flags, ien, evtsel;
2737 ien = xscale2pmu_read_int_enable();
2738 evtsel = xscale2pmu_read_event_select();
2741 case XSCALE_CYCLE_COUNTER:
2742 ien |= XSCALE2_CCOUNT_INT_EN;
2744 case XSCALE_COUNTER0:
2745 ien |= XSCALE2_COUNT0_INT_EN;
2746 evtsel &= ~XSCALE2_COUNT0_EVT_MASK;
2747 evtsel |= hwc->config_base << XSCALE2_COUNT0_EVT_SHFT;
2749 case XSCALE_COUNTER1:
2750 ien |= XSCALE2_COUNT1_INT_EN;
2751 evtsel &= ~XSCALE2_COUNT1_EVT_MASK;
2752 evtsel |= hwc->config_base << XSCALE2_COUNT1_EVT_SHFT;
2754 case XSCALE_COUNTER2:
2755 ien |= XSCALE2_COUNT2_INT_EN;
2756 evtsel &= ~XSCALE2_COUNT2_EVT_MASK;
2757 evtsel |= hwc->config_base << XSCALE2_COUNT2_EVT_SHFT;
2759 case XSCALE_COUNTER3:
2760 ien |= XSCALE2_COUNT3_INT_EN;
2761 evtsel &= ~XSCALE2_COUNT3_EVT_MASK;
2762 evtsel |= hwc->config_base << XSCALE2_COUNT3_EVT_SHFT;
2765 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
2769 spin_lock_irqsave(&pmu_lock, flags);
2770 xscale2pmu_write_event_select(evtsel);
2771 xscale2pmu_write_int_enable(ien);
2772 spin_unlock_irqrestore(&pmu_lock, flags);
2776 xscale2pmu_disable_event(struct hw_perf_event *hwc, int idx)
2778 unsigned long flags, ien, evtsel;
2780 ien = xscale2pmu_read_int_enable();
2781 evtsel = xscale2pmu_read_event_select();
2784 case XSCALE_CYCLE_COUNTER:
2785 ien &= ~XSCALE2_CCOUNT_INT_EN;
2787 case XSCALE_COUNTER0:
2788 ien &= ~XSCALE2_COUNT0_INT_EN;
2789 evtsel &= ~XSCALE2_COUNT0_EVT_MASK;
2790 evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT0_EVT_SHFT;
2792 case XSCALE_COUNTER1:
2793 ien &= ~XSCALE2_COUNT1_INT_EN;
2794 evtsel &= ~XSCALE2_COUNT1_EVT_MASK;
2795 evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT1_EVT_SHFT;
2797 case XSCALE_COUNTER2:
2798 ien &= ~XSCALE2_COUNT2_INT_EN;
2799 evtsel &= ~XSCALE2_COUNT2_EVT_MASK;
2800 evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT2_EVT_SHFT;
2802 case XSCALE_COUNTER3:
2803 ien &= ~XSCALE2_COUNT3_INT_EN;
2804 evtsel &= ~XSCALE2_COUNT3_EVT_MASK;
2805 evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT3_EVT_SHFT;
2808 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
2812 spin_lock_irqsave(&pmu_lock, flags);
2813 xscale2pmu_write_event_select(evtsel);
2814 xscale2pmu_write_int_enable(ien);
2815 spin_unlock_irqrestore(&pmu_lock, flags);
2819 xscale2pmu_get_event_idx(struct cpu_hw_events *cpuc,
2820 struct hw_perf_event *event)
2822 int idx = xscale1pmu_get_event_idx(cpuc, event);
2826 if (!test_and_set_bit(XSCALE_COUNTER3, cpuc->used_mask))
2827 idx = XSCALE_COUNTER3;
2828 else if (!test_and_set_bit(XSCALE_COUNTER2, cpuc->used_mask))
2829 idx = XSCALE_COUNTER2;
2835 xscale2pmu_start(void)
2837 unsigned long flags, val;
2839 spin_lock_irqsave(&pmu_lock, flags);
2840 val = xscale2pmu_read_pmnc() & ~XSCALE_PMU_CNT64;
2841 val |= XSCALE_PMU_ENABLE;
2842 xscale2pmu_write_pmnc(val);
2843 spin_unlock_irqrestore(&pmu_lock, flags);
2847 xscale2pmu_stop(void)
2849 unsigned long flags, val;
2851 spin_lock_irqsave(&pmu_lock, flags);
2852 val = xscale2pmu_read_pmnc();
2853 val &= ~XSCALE_PMU_ENABLE;
2854 xscale2pmu_write_pmnc(val);
2855 spin_unlock_irqrestore(&pmu_lock, flags);
2859 xscale2pmu_read_counter(int counter)
2864 case XSCALE_CYCLE_COUNTER:
2865 asm volatile("mrc p14, 0, %0, c1, c1, 0" : "=r" (val));
2867 case XSCALE_COUNTER0:
2868 asm volatile("mrc p14, 0, %0, c0, c2, 0" : "=r" (val));
2870 case XSCALE_COUNTER1:
2871 asm volatile("mrc p14, 0, %0, c1, c2, 0" : "=r" (val));
2873 case XSCALE_COUNTER2:
2874 asm volatile("mrc p14, 0, %0, c2, c2, 0" : "=r" (val));
2876 case XSCALE_COUNTER3:
2877 asm volatile("mrc p14, 0, %0, c3, c2, 0" : "=r" (val));
2885 xscale2pmu_write_counter(int counter, u32 val)
2888 case XSCALE_CYCLE_COUNTER:
2889 asm volatile("mcr p14, 0, %0, c1, c1, 0" : : "r" (val));
2891 case XSCALE_COUNTER0:
2892 asm volatile("mcr p14, 0, %0, c0, c2, 0" : : "r" (val));
2894 case XSCALE_COUNTER1:
2895 asm volatile("mcr p14, 0, %0, c1, c2, 0" : : "r" (val));
2897 case XSCALE_COUNTER2:
2898 asm volatile("mcr p14, 0, %0, c2, c2, 0" : : "r" (val));
2900 case XSCALE_COUNTER3:
2901 asm volatile("mcr p14, 0, %0, c3, c2, 0" : : "r" (val));
2906 static const struct arm_pmu xscale2pmu = {
2907 .id = ARM_PERF_PMU_ID_XSCALE2,
2908 .handle_irq = xscale2pmu_handle_irq,
2909 .enable = xscale2pmu_enable_event,
2910 .disable = xscale2pmu_disable_event,
2911 .read_counter = xscale2pmu_read_counter,
2912 .write_counter = xscale2pmu_write_counter,
2913 .get_event_idx = xscale2pmu_get_event_idx,
2914 .start = xscale2pmu_start,
2915 .stop = xscale2pmu_stop,
2916 .cache_map = &xscale_perf_cache_map,
2917 .event_map = &xscale_perf_map,
2918 .raw_event_mask = 0xFF,
2920 .max_period = (1LLU << 32) - 1,
2924 init_hw_perf_events(void)
2926 unsigned long cpuid = read_cpuid_id();
2927 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
2928 unsigned long part_number = (cpuid & 0xFFF0);
2931 if (0x41 == implementor) {
2932 switch (part_number) {
2933 case 0xB360: /* ARM1136 */
2934 case 0xB560: /* ARM1156 */
2935 case 0xB760: /* ARM1176 */
2938 case 0xB020: /* ARM11mpcore */
2939 armpmu = &armv6mpcore_pmu;
2941 case 0xC080: /* Cortex-A8 */
2942 armv7pmu.id = ARM_PERF_PMU_ID_CA8;
2943 armv7pmu.cache_map = &armv7_a8_perf_cache_map;
2944 armv7pmu.event_map = &armv7_a8_perf_map;
2947 /* Reset PMNC and read the nb of CNTx counters
2949 armv7pmu.num_events = armv7_reset_read_pmnc();
2951 case 0xC090: /* Cortex-A9 */
2952 armv7pmu.id = ARM_PERF_PMU_ID_CA9;
2953 armv7pmu.cache_map = &armv7_a9_perf_cache_map;
2954 armv7pmu.event_map = &armv7_a9_perf_map;
2957 /* Reset PMNC and read the nb of CNTx counters
2959 armv7pmu.num_events = armv7_reset_read_pmnc();
2962 /* Intel CPUs [xscale]. */
2963 } else if (0x69 == implementor) {
2964 part_number = (cpuid >> 13) & 0x7;
2965 switch (part_number) {
2967 armpmu = &xscale1pmu;
2970 armpmu = &xscale2pmu;
2976 pr_info("enabled with %s PMU driver, %d counters available\n",
2977 arm_pmu_names[armpmu->id], armpmu->num_events);
2979 pr_info("no hardware support available\n");
2982 perf_pmu_register(&pmu);
2986 arch_initcall(init_hw_perf_events);
2989 * Callchain handling code.
2993 * The registers we're interested in are at the end of the variable
2994 * length saved register structure. The fp points at the end of this
2995 * structure so the address of this struct is:
2996 * (struct frame_tail *)(xxx->fp)-1
2998 * This code has been adapted from the ARM OProfile support.
3001 struct frame_tail *fp;
3004 } __attribute__((packed));
3007 * Get the return address for a single stackframe and return a pointer to the
3010 static struct frame_tail *
3011 user_backtrace(struct frame_tail *tail,
3012 struct perf_callchain_entry *entry)
3014 struct frame_tail buftail;
3016 /* Also check accessibility of one struct frame_tail beyond */
3017 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
3019 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
3022 perf_callchain_store(entry, buftail.lr);
3025 * Frame pointers should strictly progress back up the stack
3026 * (towards higher addresses).
3028 if (tail >= buftail.fp)
3031 return buftail.fp - 1;
3035 perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
3037 struct frame_tail *tail;
3040 tail = (struct frame_tail *)regs->ARM_fp - 1;
3042 while (tail && !((unsigned long)tail & 0x3))
3043 tail = user_backtrace(tail, entry);
3047 * Gets called by walk_stackframe() for every stackframe. This will be called
3048 * whist unwinding the stackframe and is like a subroutine return so we use
3052 callchain_trace(struct stackframe *fr,
3055 struct perf_callchain_entry *entry = data;
3056 perf_callchain_store(entry, fr->pc);
3061 perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
3063 struct stackframe fr;
3065 fr.fp = regs->ARM_fp;
3066 fr.sp = regs->ARM_sp;
3067 fr.lr = regs->ARM_lr;
3068 fr.pc = regs->ARM_pc;
3069 walk_stackframe(&fr, callchain_trace, entry);