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 (*event_map)(int evt);
88 u64 (*raw_event)(u64);
89 int (*get_event_idx)(struct cpu_hw_events *cpuc,
90 struct hw_perf_event *hwc);
91 u32 (*read_counter)(int idx);
92 void (*write_counter)(int idx, u32 val);
99 /* Set at runtime when we know what CPU type we are. */
100 static const struct arm_pmu *armpmu;
102 enum arm_perf_pmu_ids
103 armpmu_get_pmu_id(void)
112 EXPORT_SYMBOL_GPL(armpmu_get_pmu_id);
115 armpmu_get_max_events(void)
120 max_events = armpmu->num_events;
124 EXPORT_SYMBOL_GPL(armpmu_get_max_events);
126 #define HW_OP_UNSUPPORTED 0xFFFF
129 PERF_COUNT_HW_CACHE_##_x
131 #define CACHE_OP_UNSUPPORTED 0xFFFF
133 static unsigned armpmu_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
134 [PERF_COUNT_HW_CACHE_OP_MAX]
135 [PERF_COUNT_HW_CACHE_RESULT_MAX];
138 armpmu_map_cache_event(u64 config)
140 unsigned int cache_type, cache_op, cache_result, ret;
142 cache_type = (config >> 0) & 0xff;
143 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
146 cache_op = (config >> 8) & 0xff;
147 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
150 cache_result = (config >> 16) & 0xff;
151 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
154 ret = (int)armpmu_perf_cache_map[cache_type][cache_op][cache_result];
156 if (ret == CACHE_OP_UNSUPPORTED)
163 armpmu_event_set_period(struct perf_event *event,
164 struct hw_perf_event *hwc,
167 s64 left = local64_read(&hwc->period_left);
168 s64 period = hwc->sample_period;
171 if (unlikely(left <= -period)) {
173 local64_set(&hwc->period_left, left);
174 hwc->last_period = period;
178 if (unlikely(left <= 0)) {
180 local64_set(&hwc->period_left, left);
181 hwc->last_period = period;
185 if (left > (s64)armpmu->max_period)
186 left = armpmu->max_period;
188 local64_set(&hwc->prev_count, (u64)-left);
190 armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
192 perf_event_update_userpage(event);
198 armpmu_event_update(struct perf_event *event,
199 struct hw_perf_event *hwc,
203 s64 prev_raw_count, new_raw_count;
207 prev_raw_count = local64_read(&hwc->prev_count);
208 new_raw_count = armpmu->read_counter(idx);
210 if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
211 new_raw_count) != prev_raw_count)
214 delta = (new_raw_count << shift) - (prev_raw_count << shift);
217 local64_add(delta, &event->count);
218 local64_sub(delta, &hwc->period_left);
220 return new_raw_count;
224 armpmu_disable(struct perf_event *event)
226 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
227 struct hw_perf_event *hwc = &event->hw;
232 clear_bit(idx, cpuc->active_mask);
233 armpmu->disable(hwc, idx);
237 armpmu_event_update(event, hwc, idx);
238 cpuc->events[idx] = NULL;
239 clear_bit(idx, cpuc->used_mask);
241 perf_event_update_userpage(event);
245 armpmu_read(struct perf_event *event)
247 struct hw_perf_event *hwc = &event->hw;
249 /* Don't read disabled counters! */
253 armpmu_event_update(event, hwc, hwc->idx);
257 armpmu_unthrottle(struct perf_event *event)
259 struct hw_perf_event *hwc = &event->hw;
262 * Set the period again. Some counters can't be stopped, so when we
263 * were throttled we simply disabled the IRQ source and the counter
264 * may have been left counting. If we don't do this step then we may
265 * get an interrupt too soon or *way* too late if the overflow has
266 * happened since disabling.
268 armpmu_event_set_period(event, hwc, hwc->idx);
269 armpmu->enable(hwc, hwc->idx);
273 armpmu_enable(struct perf_event *event)
275 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
276 struct hw_perf_event *hwc = &event->hw;
280 /* If we don't have a space for the counter then finish early. */
281 idx = armpmu->get_event_idx(cpuc, hwc);
288 * If there is an event in the counter we are going to use then make
289 * sure it is disabled.
292 armpmu->disable(hwc, idx);
293 cpuc->events[idx] = event;
294 set_bit(idx, cpuc->active_mask);
296 /* Set the period for the event. */
297 armpmu_event_set_period(event, hwc, idx);
299 /* Enable the event. */
300 armpmu->enable(hwc, idx);
302 /* Propagate our changes to the userspace mapping. */
303 perf_event_update_userpage(event);
309 static struct pmu pmu;
312 validate_event(struct cpu_hw_events *cpuc,
313 struct perf_event *event)
315 struct hw_perf_event fake_event = event->hw;
317 if (event->pmu && event->pmu != &pmu)
320 return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
324 validate_group(struct perf_event *event)
326 struct perf_event *sibling, *leader = event->group_leader;
327 struct cpu_hw_events fake_pmu;
329 memset(&fake_pmu, 0, sizeof(fake_pmu));
331 if (!validate_event(&fake_pmu, leader))
334 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
335 if (!validate_event(&fake_pmu, sibling))
339 if (!validate_event(&fake_pmu, event))
346 armpmu_reserve_hardware(void)
348 int i, err = -ENODEV, irq;
350 pmu_device = reserve_pmu(ARM_PMU_DEVICE_CPU);
351 if (IS_ERR(pmu_device)) {
352 pr_warning("unable to reserve pmu\n");
353 return PTR_ERR(pmu_device);
356 init_pmu(ARM_PMU_DEVICE_CPU);
358 if (pmu_device->num_resources < 1) {
359 pr_err("no irqs for PMUs defined\n");
363 for (i = 0; i < pmu_device->num_resources; ++i) {
364 irq = platform_get_irq(pmu_device, i);
368 err = request_irq(irq, armpmu->handle_irq,
369 IRQF_DISABLED | IRQF_NOBALANCING,
372 pr_warning("unable to request IRQ%d for ARM perf "
379 for (i = i - 1; i >= 0; --i) {
380 irq = platform_get_irq(pmu_device, i);
384 release_pmu(pmu_device);
392 armpmu_release_hardware(void)
396 for (i = pmu_device->num_resources - 1; i >= 0; --i) {
397 irq = platform_get_irq(pmu_device, i);
403 release_pmu(pmu_device);
407 static atomic_t active_events = ATOMIC_INIT(0);
408 static DEFINE_MUTEX(pmu_reserve_mutex);
411 hw_perf_event_destroy(struct perf_event *event)
413 if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
414 armpmu_release_hardware();
415 mutex_unlock(&pmu_reserve_mutex);
420 __hw_perf_event_init(struct perf_event *event)
422 struct hw_perf_event *hwc = &event->hw;
425 /* Decode the generic type into an ARM event identifier. */
426 if (PERF_TYPE_HARDWARE == event->attr.type) {
427 mapping = armpmu->event_map(event->attr.config);
428 } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
429 mapping = armpmu_map_cache_event(event->attr.config);
430 } else if (PERF_TYPE_RAW == event->attr.type) {
431 mapping = armpmu->raw_event(event->attr.config);
433 pr_debug("event type %x not supported\n", event->attr.type);
438 pr_debug("event %x:%llx not supported\n", event->attr.type,
444 * Check whether we need to exclude the counter from certain modes.
445 * The ARM performance counters are on all of the time so if someone
446 * has asked us for some excludes then we have to fail.
448 if (event->attr.exclude_kernel || event->attr.exclude_user ||
449 event->attr.exclude_hv || event->attr.exclude_idle) {
450 pr_debug("ARM performance counters do not support "
456 * We don't assign an index until we actually place the event onto
457 * hardware. Use -1 to signify that we haven't decided where to put it
458 * yet. For SMP systems, each core has it's own PMU so we can't do any
459 * clever allocation or constraints checking at this point.
464 * Store the event encoding into the config_base field. config and
465 * event_base are unused as the only 2 things we need to know are
466 * the event mapping and the counter to use. The counter to use is
467 * also the indx and the config_base is the event type.
469 hwc->config_base = (unsigned long)mapping;
473 if (!hwc->sample_period) {
474 hwc->sample_period = armpmu->max_period;
475 hwc->last_period = hwc->sample_period;
476 local64_set(&hwc->period_left, hwc->sample_period);
480 if (event->group_leader != event) {
481 err = validate_group(event);
489 static int armpmu_event_init(struct perf_event *event)
493 switch (event->attr.type) {
495 case PERF_TYPE_HARDWARE:
496 case PERF_TYPE_HW_CACHE:
506 event->destroy = hw_perf_event_destroy;
508 if (!atomic_inc_not_zero(&active_events)) {
509 if (atomic_read(&active_events) > perf_max_events) {
510 atomic_dec(&active_events);
514 mutex_lock(&pmu_reserve_mutex);
515 if (atomic_read(&active_events) == 0) {
516 err = armpmu_reserve_hardware();
520 atomic_inc(&active_events);
521 mutex_unlock(&pmu_reserve_mutex);
527 err = __hw_perf_event_init(event);
529 hw_perf_event_destroy(event);
534 static struct pmu pmu = {
535 .event_init = armpmu_event_init,
536 .enable = armpmu_enable,
537 .disable = armpmu_disable,
538 .unthrottle = armpmu_unthrottle,
545 /* Enable all of the perf events on hardware. */
547 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
552 for (idx = 0; idx <= armpmu->num_events; ++idx) {
553 struct perf_event *event = cpuc->events[idx];
558 armpmu->enable(&event->hw, idx);
565 hw_perf_disable(void)
572 * ARMv6 Performance counter handling code.
574 * ARMv6 has 2 configurable performance counters and a single cycle counter.
575 * They all share a single reset bit but can be written to zero so we can use
578 * The counters can't be individually enabled or disabled so when we remove
579 * one event and replace it with another we could get spurious counts from the
580 * wrong event. However, we can take advantage of the fact that the
581 * performance counters can export events to the event bus, and the event bus
582 * itself can be monitored. This requires that we *don't* export the events to
583 * the event bus. The procedure for disabling a configurable counter is:
584 * - change the counter to count the ETMEXTOUT[0] signal (0x20). This
585 * effectively stops the counter from counting.
586 * - disable the counter's interrupt generation (each counter has it's
587 * own interrupt enable bit).
588 * Once stopped, the counter value can be written as 0 to reset.
590 * To enable a counter:
591 * - enable the counter's interrupt generation.
592 * - set the new event type.
594 * Note: the dedicated cycle counter only counts cycles and can't be
595 * enabled/disabled independently of the others. When we want to disable the
596 * cycle counter, we have to just disable the interrupt reporting and start
597 * ignoring that counter. When re-enabling, we have to reset the value and
598 * enable the interrupt.
601 enum armv6_perf_types {
602 ARMV6_PERFCTR_ICACHE_MISS = 0x0,
603 ARMV6_PERFCTR_IBUF_STALL = 0x1,
604 ARMV6_PERFCTR_DDEP_STALL = 0x2,
605 ARMV6_PERFCTR_ITLB_MISS = 0x3,
606 ARMV6_PERFCTR_DTLB_MISS = 0x4,
607 ARMV6_PERFCTR_BR_EXEC = 0x5,
608 ARMV6_PERFCTR_BR_MISPREDICT = 0x6,
609 ARMV6_PERFCTR_INSTR_EXEC = 0x7,
610 ARMV6_PERFCTR_DCACHE_HIT = 0x9,
611 ARMV6_PERFCTR_DCACHE_ACCESS = 0xA,
612 ARMV6_PERFCTR_DCACHE_MISS = 0xB,
613 ARMV6_PERFCTR_DCACHE_WBACK = 0xC,
614 ARMV6_PERFCTR_SW_PC_CHANGE = 0xD,
615 ARMV6_PERFCTR_MAIN_TLB_MISS = 0xF,
616 ARMV6_PERFCTR_EXPL_D_ACCESS = 0x10,
617 ARMV6_PERFCTR_LSU_FULL_STALL = 0x11,
618 ARMV6_PERFCTR_WBUF_DRAINED = 0x12,
619 ARMV6_PERFCTR_CPU_CYCLES = 0xFF,
620 ARMV6_PERFCTR_NOP = 0x20,
623 enum armv6_counters {
624 ARMV6_CYCLE_COUNTER = 1,
630 * The hardware events that we support. We do support cache operations but
631 * we have harvard caches and no way to combine instruction and data
632 * accesses/misses in hardware.
634 static const unsigned armv6_perf_map[PERF_COUNT_HW_MAX] = {
635 [PERF_COUNT_HW_CPU_CYCLES] = ARMV6_PERFCTR_CPU_CYCLES,
636 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6_PERFCTR_INSTR_EXEC,
637 [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
638 [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
639 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6_PERFCTR_BR_EXEC,
640 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6_PERFCTR_BR_MISPREDICT,
641 [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
644 static const unsigned armv6_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
645 [PERF_COUNT_HW_CACHE_OP_MAX]
646 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
649 * The performance counters don't differentiate between read
650 * and write accesses/misses so this isn't strictly correct,
651 * but it's the best we can do. Writes and reads get
655 [C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
656 [C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
659 [C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
660 [C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
663 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
664 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
669 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
670 [C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
673 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
674 [C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
677 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
678 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
683 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
684 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
687 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
688 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
691 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
692 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
697 * The ARM performance counters can count micro DTLB misses,
698 * micro ITLB misses and main TLB misses. There isn't an event
699 * for TLB misses, so use the micro misses here and if users
700 * want the main TLB misses they can use a raw counter.
703 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
704 [C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
707 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
708 [C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
711 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
712 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
717 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
718 [C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
721 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
722 [C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
725 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
726 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
731 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
732 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
735 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
736 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
739 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
740 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
745 enum armv6mpcore_perf_types {
746 ARMV6MPCORE_PERFCTR_ICACHE_MISS = 0x0,
747 ARMV6MPCORE_PERFCTR_IBUF_STALL = 0x1,
748 ARMV6MPCORE_PERFCTR_DDEP_STALL = 0x2,
749 ARMV6MPCORE_PERFCTR_ITLB_MISS = 0x3,
750 ARMV6MPCORE_PERFCTR_DTLB_MISS = 0x4,
751 ARMV6MPCORE_PERFCTR_BR_EXEC = 0x5,
752 ARMV6MPCORE_PERFCTR_BR_NOTPREDICT = 0x6,
753 ARMV6MPCORE_PERFCTR_BR_MISPREDICT = 0x7,
754 ARMV6MPCORE_PERFCTR_INSTR_EXEC = 0x8,
755 ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS = 0xA,
756 ARMV6MPCORE_PERFCTR_DCACHE_RDMISS = 0xB,
757 ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS = 0xC,
758 ARMV6MPCORE_PERFCTR_DCACHE_WRMISS = 0xD,
759 ARMV6MPCORE_PERFCTR_DCACHE_EVICTION = 0xE,
760 ARMV6MPCORE_PERFCTR_SW_PC_CHANGE = 0xF,
761 ARMV6MPCORE_PERFCTR_MAIN_TLB_MISS = 0x10,
762 ARMV6MPCORE_PERFCTR_EXPL_MEM_ACCESS = 0x11,
763 ARMV6MPCORE_PERFCTR_LSU_FULL_STALL = 0x12,
764 ARMV6MPCORE_PERFCTR_WBUF_DRAINED = 0x13,
765 ARMV6MPCORE_PERFCTR_CPU_CYCLES = 0xFF,
769 * The hardware events that we support. We do support cache operations but
770 * we have harvard caches and no way to combine instruction and data
771 * accesses/misses in hardware.
773 static const unsigned armv6mpcore_perf_map[PERF_COUNT_HW_MAX] = {
774 [PERF_COUNT_HW_CPU_CYCLES] = ARMV6MPCORE_PERFCTR_CPU_CYCLES,
775 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_INSTR_EXEC,
776 [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
777 [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
778 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_BR_EXEC,
779 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6MPCORE_PERFCTR_BR_MISPREDICT,
780 [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
783 static const unsigned armv6mpcore_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
784 [PERF_COUNT_HW_CACHE_OP_MAX]
785 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
789 ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS,
791 ARMV6MPCORE_PERFCTR_DCACHE_RDMISS,
795 ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS,
797 ARMV6MPCORE_PERFCTR_DCACHE_WRMISS,
800 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
801 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
806 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
807 [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
810 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
811 [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
814 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
815 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
820 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
821 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
824 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
825 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
828 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
829 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
834 * The ARM performance counters can count micro DTLB misses,
835 * micro ITLB misses and main TLB misses. There isn't an event
836 * for TLB misses, so use the micro misses here and if users
837 * want the main TLB misses they can use a raw counter.
840 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
841 [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
844 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
845 [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
848 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
849 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
854 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
855 [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
858 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
859 [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
862 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
863 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
868 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
869 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
872 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
873 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
876 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
877 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
882 static inline unsigned long
883 armv6_pmcr_read(void)
886 asm volatile("mrc p15, 0, %0, c15, c12, 0" : "=r"(val));
891 armv6_pmcr_write(unsigned long val)
893 asm volatile("mcr p15, 0, %0, c15, c12, 0" : : "r"(val));
896 #define ARMV6_PMCR_ENABLE (1 << 0)
897 #define ARMV6_PMCR_CTR01_RESET (1 << 1)
898 #define ARMV6_PMCR_CCOUNT_RESET (1 << 2)
899 #define ARMV6_PMCR_CCOUNT_DIV (1 << 3)
900 #define ARMV6_PMCR_COUNT0_IEN (1 << 4)
901 #define ARMV6_PMCR_COUNT1_IEN (1 << 5)
902 #define ARMV6_PMCR_CCOUNT_IEN (1 << 6)
903 #define ARMV6_PMCR_COUNT0_OVERFLOW (1 << 8)
904 #define ARMV6_PMCR_COUNT1_OVERFLOW (1 << 9)
905 #define ARMV6_PMCR_CCOUNT_OVERFLOW (1 << 10)
906 #define ARMV6_PMCR_EVT_COUNT0_SHIFT 20
907 #define ARMV6_PMCR_EVT_COUNT0_MASK (0xFF << ARMV6_PMCR_EVT_COUNT0_SHIFT)
908 #define ARMV6_PMCR_EVT_COUNT1_SHIFT 12
909 #define ARMV6_PMCR_EVT_COUNT1_MASK (0xFF << ARMV6_PMCR_EVT_COUNT1_SHIFT)
911 #define ARMV6_PMCR_OVERFLOWED_MASK \
912 (ARMV6_PMCR_COUNT0_OVERFLOW | ARMV6_PMCR_COUNT1_OVERFLOW | \
913 ARMV6_PMCR_CCOUNT_OVERFLOW)
916 armv6_pmcr_has_overflowed(unsigned long pmcr)
918 return (pmcr & ARMV6_PMCR_OVERFLOWED_MASK);
922 armv6_pmcr_counter_has_overflowed(unsigned long pmcr,
923 enum armv6_counters counter)
927 if (ARMV6_CYCLE_COUNTER == counter)
928 ret = pmcr & ARMV6_PMCR_CCOUNT_OVERFLOW;
929 else if (ARMV6_COUNTER0 == counter)
930 ret = pmcr & ARMV6_PMCR_COUNT0_OVERFLOW;
931 else if (ARMV6_COUNTER1 == counter)
932 ret = pmcr & ARMV6_PMCR_COUNT1_OVERFLOW;
934 WARN_ONCE(1, "invalid counter number (%d)\n", counter);
940 armv6pmu_read_counter(int counter)
942 unsigned long value = 0;
944 if (ARMV6_CYCLE_COUNTER == counter)
945 asm volatile("mrc p15, 0, %0, c15, c12, 1" : "=r"(value));
946 else if (ARMV6_COUNTER0 == counter)
947 asm volatile("mrc p15, 0, %0, c15, c12, 2" : "=r"(value));
948 else if (ARMV6_COUNTER1 == counter)
949 asm volatile("mrc p15, 0, %0, c15, c12, 3" : "=r"(value));
951 WARN_ONCE(1, "invalid counter number (%d)\n", counter);
957 armv6pmu_write_counter(int counter,
960 if (ARMV6_CYCLE_COUNTER == counter)
961 asm volatile("mcr p15, 0, %0, c15, c12, 1" : : "r"(value));
962 else if (ARMV6_COUNTER0 == counter)
963 asm volatile("mcr p15, 0, %0, c15, c12, 2" : : "r"(value));
964 else if (ARMV6_COUNTER1 == counter)
965 asm volatile("mcr p15, 0, %0, c15, c12, 3" : : "r"(value));
967 WARN_ONCE(1, "invalid counter number (%d)\n", counter);
971 armv6pmu_enable_event(struct hw_perf_event *hwc,
974 unsigned long val, mask, evt, flags;
976 if (ARMV6_CYCLE_COUNTER == idx) {
978 evt = ARMV6_PMCR_CCOUNT_IEN;
979 } else if (ARMV6_COUNTER0 == idx) {
980 mask = ARMV6_PMCR_EVT_COUNT0_MASK;
981 evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT0_SHIFT) |
982 ARMV6_PMCR_COUNT0_IEN;
983 } else if (ARMV6_COUNTER1 == idx) {
984 mask = ARMV6_PMCR_EVT_COUNT1_MASK;
985 evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT1_SHIFT) |
986 ARMV6_PMCR_COUNT1_IEN;
988 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
993 * Mask out the current event and set the counter to count the event
994 * that we're interested in.
996 spin_lock_irqsave(&pmu_lock, flags);
997 val = armv6_pmcr_read();
1000 armv6_pmcr_write(val);
1001 spin_unlock_irqrestore(&pmu_lock, flags);
1005 armv6pmu_handle_irq(int irq_num,
1008 unsigned long pmcr = armv6_pmcr_read();
1009 struct perf_sample_data data;
1010 struct cpu_hw_events *cpuc;
1011 struct pt_regs *regs;
1014 if (!armv6_pmcr_has_overflowed(pmcr))
1017 regs = get_irq_regs();
1020 * The interrupts are cleared by writing the overflow flags back to
1021 * the control register. All of the other bits don't have any effect
1022 * if they are rewritten, so write the whole value back.
1024 armv6_pmcr_write(pmcr);
1026 perf_sample_data_init(&data, 0);
1028 cpuc = &__get_cpu_var(cpu_hw_events);
1029 for (idx = 0; idx <= armpmu->num_events; ++idx) {
1030 struct perf_event *event = cpuc->events[idx];
1031 struct hw_perf_event *hwc;
1033 if (!test_bit(idx, cpuc->active_mask))
1037 * We have a single interrupt for all counters. Check that
1038 * each counter has overflowed before we process it.
1040 if (!armv6_pmcr_counter_has_overflowed(pmcr, idx))
1044 armpmu_event_update(event, hwc, idx);
1045 data.period = event->hw.last_period;
1046 if (!armpmu_event_set_period(event, hwc, idx))
1049 if (perf_event_overflow(event, 0, &data, regs))
1050 armpmu->disable(hwc, idx);
1054 * Handle the pending perf events.
1056 * Note: this call *must* be run with interrupts enabled. For
1057 * platforms that can have the PMU interrupts raised as a PMI, this
1060 perf_event_do_pending();
1066 armv6pmu_start(void)
1068 unsigned long flags, val;
1070 spin_lock_irqsave(&pmu_lock, flags);
1071 val = armv6_pmcr_read();
1072 val |= ARMV6_PMCR_ENABLE;
1073 armv6_pmcr_write(val);
1074 spin_unlock_irqrestore(&pmu_lock, flags);
1080 unsigned long flags, val;
1082 spin_lock_irqsave(&pmu_lock, flags);
1083 val = armv6_pmcr_read();
1084 val &= ~ARMV6_PMCR_ENABLE;
1085 armv6_pmcr_write(val);
1086 spin_unlock_irqrestore(&pmu_lock, flags);
1090 armv6pmu_event_map(int config)
1092 int mapping = armv6_perf_map[config];
1093 if (HW_OP_UNSUPPORTED == mapping)
1094 mapping = -EOPNOTSUPP;
1099 armv6mpcore_pmu_event_map(int config)
1101 int mapping = armv6mpcore_perf_map[config];
1102 if (HW_OP_UNSUPPORTED == mapping)
1103 mapping = -EOPNOTSUPP;
1108 armv6pmu_raw_event(u64 config)
1110 return config & 0xff;
1114 armv6pmu_get_event_idx(struct cpu_hw_events *cpuc,
1115 struct hw_perf_event *event)
1117 /* Always place a cycle counter into the cycle counter. */
1118 if (ARMV6_PERFCTR_CPU_CYCLES == event->config_base) {
1119 if (test_and_set_bit(ARMV6_CYCLE_COUNTER, cpuc->used_mask))
1122 return ARMV6_CYCLE_COUNTER;
1125 * For anything other than a cycle counter, try and use
1126 * counter0 and counter1.
1128 if (!test_and_set_bit(ARMV6_COUNTER1, cpuc->used_mask)) {
1129 return ARMV6_COUNTER1;
1132 if (!test_and_set_bit(ARMV6_COUNTER0, cpuc->used_mask)) {
1133 return ARMV6_COUNTER0;
1136 /* The counters are all in use. */
1142 armv6pmu_disable_event(struct hw_perf_event *hwc,
1145 unsigned long val, mask, evt, flags;
1147 if (ARMV6_CYCLE_COUNTER == idx) {
1148 mask = ARMV6_PMCR_CCOUNT_IEN;
1150 } else if (ARMV6_COUNTER0 == idx) {
1151 mask = ARMV6_PMCR_COUNT0_IEN | ARMV6_PMCR_EVT_COUNT0_MASK;
1152 evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT0_SHIFT;
1153 } else if (ARMV6_COUNTER1 == idx) {
1154 mask = ARMV6_PMCR_COUNT1_IEN | ARMV6_PMCR_EVT_COUNT1_MASK;
1155 evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT1_SHIFT;
1157 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
1162 * Mask out the current event and set the counter to count the number
1163 * of ETM bus signal assertion cycles. The external reporting should
1164 * be disabled and so this should never increment.
1166 spin_lock_irqsave(&pmu_lock, flags);
1167 val = armv6_pmcr_read();
1170 armv6_pmcr_write(val);
1171 spin_unlock_irqrestore(&pmu_lock, flags);
1175 armv6mpcore_pmu_disable_event(struct hw_perf_event *hwc,
1178 unsigned long val, mask, flags, evt = 0;
1180 if (ARMV6_CYCLE_COUNTER == idx) {
1181 mask = ARMV6_PMCR_CCOUNT_IEN;
1182 } else if (ARMV6_COUNTER0 == idx) {
1183 mask = ARMV6_PMCR_COUNT0_IEN;
1184 } else if (ARMV6_COUNTER1 == idx) {
1185 mask = ARMV6_PMCR_COUNT1_IEN;
1187 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
1192 * Unlike UP ARMv6, we don't have a way of stopping the counters. We
1193 * simply disable the interrupt reporting.
1195 spin_lock_irqsave(&pmu_lock, flags);
1196 val = armv6_pmcr_read();
1199 armv6_pmcr_write(val);
1200 spin_unlock_irqrestore(&pmu_lock, flags);
1203 static const struct arm_pmu armv6pmu = {
1204 .id = ARM_PERF_PMU_ID_V6,
1205 .handle_irq = armv6pmu_handle_irq,
1206 .enable = armv6pmu_enable_event,
1207 .disable = armv6pmu_disable_event,
1208 .event_map = armv6pmu_event_map,
1209 .raw_event = armv6pmu_raw_event,
1210 .read_counter = armv6pmu_read_counter,
1211 .write_counter = armv6pmu_write_counter,
1212 .get_event_idx = armv6pmu_get_event_idx,
1213 .start = armv6pmu_start,
1214 .stop = armv6pmu_stop,
1216 .max_period = (1LLU << 32) - 1,
1220 * ARMv6mpcore is almost identical to single core ARMv6 with the exception
1221 * that some of the events have different enumerations and that there is no
1222 * *hack* to stop the programmable counters. To stop the counters we simply
1223 * disable the interrupt reporting and update the event. When unthrottling we
1224 * reset the period and enable the interrupt reporting.
1226 static const struct arm_pmu armv6mpcore_pmu = {
1227 .id = ARM_PERF_PMU_ID_V6MP,
1228 .handle_irq = armv6pmu_handle_irq,
1229 .enable = armv6pmu_enable_event,
1230 .disable = armv6mpcore_pmu_disable_event,
1231 .event_map = armv6mpcore_pmu_event_map,
1232 .raw_event = armv6pmu_raw_event,
1233 .read_counter = armv6pmu_read_counter,
1234 .write_counter = armv6pmu_write_counter,
1235 .get_event_idx = armv6pmu_get_event_idx,
1236 .start = armv6pmu_start,
1237 .stop = armv6pmu_stop,
1239 .max_period = (1LLU << 32) - 1,
1243 * ARMv7 Cortex-A8 and Cortex-A9 Performance Events handling code.
1245 * Copied from ARMv6 code, with the low level code inspired
1246 * by the ARMv7 Oprofile code.
1248 * Cortex-A8 has up to 4 configurable performance counters and
1249 * a single cycle counter.
1250 * Cortex-A9 has up to 31 configurable performance counters and
1251 * a single cycle counter.
1253 * All counters can be enabled/disabled and IRQ masked separately. The cycle
1254 * counter and all 4 performance counters together can be reset separately.
1257 /* Common ARMv7 event types */
1258 enum armv7_perf_types {
1259 ARMV7_PERFCTR_PMNC_SW_INCR = 0x00,
1260 ARMV7_PERFCTR_IFETCH_MISS = 0x01,
1261 ARMV7_PERFCTR_ITLB_MISS = 0x02,
1262 ARMV7_PERFCTR_DCACHE_REFILL = 0x03,
1263 ARMV7_PERFCTR_DCACHE_ACCESS = 0x04,
1264 ARMV7_PERFCTR_DTLB_REFILL = 0x05,
1265 ARMV7_PERFCTR_DREAD = 0x06,
1266 ARMV7_PERFCTR_DWRITE = 0x07,
1268 ARMV7_PERFCTR_EXC_TAKEN = 0x09,
1269 ARMV7_PERFCTR_EXC_EXECUTED = 0x0A,
1270 ARMV7_PERFCTR_CID_WRITE = 0x0B,
1271 /* ARMV7_PERFCTR_PC_WRITE is equivalent to HW_BRANCH_INSTRUCTIONS.
1273 * - all branch instructions,
1274 * - instructions that explicitly write the PC,
1275 * - exception generating instructions.
1277 ARMV7_PERFCTR_PC_WRITE = 0x0C,
1278 ARMV7_PERFCTR_PC_IMM_BRANCH = 0x0D,
1279 ARMV7_PERFCTR_UNALIGNED_ACCESS = 0x0F,
1280 ARMV7_PERFCTR_PC_BRANCH_MIS_PRED = 0x10,
1281 ARMV7_PERFCTR_CLOCK_CYCLES = 0x11,
1283 ARMV7_PERFCTR_PC_BRANCH_MIS_USED = 0x12,
1285 ARMV7_PERFCTR_CPU_CYCLES = 0xFF
1288 /* ARMv7 Cortex-A8 specific event types */
1289 enum armv7_a8_perf_types {
1290 ARMV7_PERFCTR_INSTR_EXECUTED = 0x08,
1292 ARMV7_PERFCTR_PC_PROC_RETURN = 0x0E,
1294 ARMV7_PERFCTR_WRITE_BUFFER_FULL = 0x40,
1295 ARMV7_PERFCTR_L2_STORE_MERGED = 0x41,
1296 ARMV7_PERFCTR_L2_STORE_BUFF = 0x42,
1297 ARMV7_PERFCTR_L2_ACCESS = 0x43,
1298 ARMV7_PERFCTR_L2_CACH_MISS = 0x44,
1299 ARMV7_PERFCTR_AXI_READ_CYCLES = 0x45,
1300 ARMV7_PERFCTR_AXI_WRITE_CYCLES = 0x46,
1301 ARMV7_PERFCTR_MEMORY_REPLAY = 0x47,
1302 ARMV7_PERFCTR_UNALIGNED_ACCESS_REPLAY = 0x48,
1303 ARMV7_PERFCTR_L1_DATA_MISS = 0x49,
1304 ARMV7_PERFCTR_L1_INST_MISS = 0x4A,
1305 ARMV7_PERFCTR_L1_DATA_COLORING = 0x4B,
1306 ARMV7_PERFCTR_L1_NEON_DATA = 0x4C,
1307 ARMV7_PERFCTR_L1_NEON_CACH_DATA = 0x4D,
1308 ARMV7_PERFCTR_L2_NEON = 0x4E,
1309 ARMV7_PERFCTR_L2_NEON_HIT = 0x4F,
1310 ARMV7_PERFCTR_L1_INST = 0x50,
1311 ARMV7_PERFCTR_PC_RETURN_MIS_PRED = 0x51,
1312 ARMV7_PERFCTR_PC_BRANCH_FAILED = 0x52,
1313 ARMV7_PERFCTR_PC_BRANCH_TAKEN = 0x53,
1314 ARMV7_PERFCTR_PC_BRANCH_EXECUTED = 0x54,
1315 ARMV7_PERFCTR_OP_EXECUTED = 0x55,
1316 ARMV7_PERFCTR_CYCLES_INST_STALL = 0x56,
1317 ARMV7_PERFCTR_CYCLES_INST = 0x57,
1318 ARMV7_PERFCTR_CYCLES_NEON_DATA_STALL = 0x58,
1319 ARMV7_PERFCTR_CYCLES_NEON_INST_STALL = 0x59,
1320 ARMV7_PERFCTR_NEON_CYCLES = 0x5A,
1322 ARMV7_PERFCTR_PMU0_EVENTS = 0x70,
1323 ARMV7_PERFCTR_PMU1_EVENTS = 0x71,
1324 ARMV7_PERFCTR_PMU_EVENTS = 0x72,
1327 /* ARMv7 Cortex-A9 specific event types */
1328 enum armv7_a9_perf_types {
1329 ARMV7_PERFCTR_JAVA_HW_BYTECODE_EXEC = 0x40,
1330 ARMV7_PERFCTR_JAVA_SW_BYTECODE_EXEC = 0x41,
1331 ARMV7_PERFCTR_JAZELLE_BRANCH_EXEC = 0x42,
1333 ARMV7_PERFCTR_COHERENT_LINE_MISS = 0x50,
1334 ARMV7_PERFCTR_COHERENT_LINE_HIT = 0x51,
1336 ARMV7_PERFCTR_ICACHE_DEP_STALL_CYCLES = 0x60,
1337 ARMV7_PERFCTR_DCACHE_DEP_STALL_CYCLES = 0x61,
1338 ARMV7_PERFCTR_TLB_MISS_DEP_STALL_CYCLES = 0x62,
1339 ARMV7_PERFCTR_STREX_EXECUTED_PASSED = 0x63,
1340 ARMV7_PERFCTR_STREX_EXECUTED_FAILED = 0x64,
1341 ARMV7_PERFCTR_DATA_EVICTION = 0x65,
1342 ARMV7_PERFCTR_ISSUE_STAGE_NO_INST = 0x66,
1343 ARMV7_PERFCTR_ISSUE_STAGE_EMPTY = 0x67,
1344 ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE = 0x68,
1346 ARMV7_PERFCTR_PREDICTABLE_FUNCT_RETURNS = 0x6E,
1348 ARMV7_PERFCTR_MAIN_UNIT_EXECUTED_INST = 0x70,
1349 ARMV7_PERFCTR_SECOND_UNIT_EXECUTED_INST = 0x71,
1350 ARMV7_PERFCTR_LD_ST_UNIT_EXECUTED_INST = 0x72,
1351 ARMV7_PERFCTR_FP_EXECUTED_INST = 0x73,
1352 ARMV7_PERFCTR_NEON_EXECUTED_INST = 0x74,
1354 ARMV7_PERFCTR_PLD_FULL_DEP_STALL_CYCLES = 0x80,
1355 ARMV7_PERFCTR_DATA_WR_DEP_STALL_CYCLES = 0x81,
1356 ARMV7_PERFCTR_ITLB_MISS_DEP_STALL_CYCLES = 0x82,
1357 ARMV7_PERFCTR_DTLB_MISS_DEP_STALL_CYCLES = 0x83,
1358 ARMV7_PERFCTR_MICRO_ITLB_MISS_DEP_STALL_CYCLES = 0x84,
1359 ARMV7_PERFCTR_MICRO_DTLB_MISS_DEP_STALL_CYCLES = 0x85,
1360 ARMV7_PERFCTR_DMB_DEP_STALL_CYCLES = 0x86,
1362 ARMV7_PERFCTR_INTGR_CLK_ENABLED_CYCLES = 0x8A,
1363 ARMV7_PERFCTR_DATA_ENGINE_CLK_EN_CYCLES = 0x8B,
1365 ARMV7_PERFCTR_ISB_INST = 0x90,
1366 ARMV7_PERFCTR_DSB_INST = 0x91,
1367 ARMV7_PERFCTR_DMB_INST = 0x92,
1368 ARMV7_PERFCTR_EXT_INTERRUPTS = 0x93,
1370 ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_COMPLETED = 0xA0,
1371 ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_SKIPPED = 0xA1,
1372 ARMV7_PERFCTR_PLE_FIFO_FLUSH = 0xA2,
1373 ARMV7_PERFCTR_PLE_RQST_COMPLETED = 0xA3,
1374 ARMV7_PERFCTR_PLE_FIFO_OVERFLOW = 0xA4,
1375 ARMV7_PERFCTR_PLE_RQST_PROG = 0xA5
1379 * Cortex-A8 HW events mapping
1381 * The hardware events that we support. We do support cache operations but
1382 * we have harvard caches and no way to combine instruction and data
1383 * accesses/misses in hardware.
1385 static const unsigned armv7_a8_perf_map[PERF_COUNT_HW_MAX] = {
1386 [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
1387 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
1388 [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
1389 [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
1390 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
1391 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1392 [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
1395 static const unsigned armv7_a8_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
1396 [PERF_COUNT_HW_CACHE_OP_MAX]
1397 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
1400 * The performance counters don't differentiate between read
1401 * and write accesses/misses so this isn't strictly correct,
1402 * but it's the best we can do. Writes and reads get
1406 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
1407 [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
1410 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
1411 [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
1413 [C(OP_PREFETCH)] = {
1414 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1415 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1420 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
1421 [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
1424 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
1425 [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
1427 [C(OP_PREFETCH)] = {
1428 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1429 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1434 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
1435 [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
1438 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
1439 [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
1441 [C(OP_PREFETCH)] = {
1442 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1443 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1448 * Only ITLB misses and DTLB refills are supported.
1449 * If users want the DTLB refills misses a raw counter
1453 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1454 [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
1457 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1458 [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
1460 [C(OP_PREFETCH)] = {
1461 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1462 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1467 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1468 [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
1471 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1472 [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
1474 [C(OP_PREFETCH)] = {
1475 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1476 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1481 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
1483 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1486 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
1488 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1490 [C(OP_PREFETCH)] = {
1491 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1492 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1498 * Cortex-A9 HW events mapping
1500 static const unsigned armv7_a9_perf_map[PERF_COUNT_HW_MAX] = {
1501 [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
1502 [PERF_COUNT_HW_INSTRUCTIONS] =
1503 ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE,
1504 [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV7_PERFCTR_COHERENT_LINE_HIT,
1505 [PERF_COUNT_HW_CACHE_MISSES] = ARMV7_PERFCTR_COHERENT_LINE_MISS,
1506 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
1507 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1508 [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
1511 static const unsigned armv7_a9_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
1512 [PERF_COUNT_HW_CACHE_OP_MAX]
1513 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
1516 * The performance counters don't differentiate between read
1517 * and write accesses/misses so this isn't strictly correct,
1518 * but it's the best we can do. Writes and reads get
1522 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
1523 [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
1526 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
1527 [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
1529 [C(OP_PREFETCH)] = {
1530 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1531 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1536 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1537 [C(RESULT_MISS)] = ARMV7_PERFCTR_IFETCH_MISS,
1540 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1541 [C(RESULT_MISS)] = ARMV7_PERFCTR_IFETCH_MISS,
1543 [C(OP_PREFETCH)] = {
1544 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1545 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1550 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1551 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1554 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1555 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1557 [C(OP_PREFETCH)] = {
1558 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1559 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1564 * Only ITLB misses and DTLB refills are supported.
1565 * If users want the DTLB refills misses a raw counter
1569 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1570 [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
1573 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1574 [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
1576 [C(OP_PREFETCH)] = {
1577 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1578 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1583 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1584 [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
1587 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1588 [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
1590 [C(OP_PREFETCH)] = {
1591 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1592 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1597 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
1599 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1602 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
1604 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1606 [C(OP_PREFETCH)] = {
1607 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1608 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1614 * Perf Events counters
1616 enum armv7_counters {
1617 ARMV7_CYCLE_COUNTER = 1, /* Cycle counter */
1618 ARMV7_COUNTER0 = 2, /* First event counter */
1622 * The cycle counter is ARMV7_CYCLE_COUNTER.
1623 * The first event counter is ARMV7_COUNTER0.
1624 * The last event counter is (ARMV7_COUNTER0 + armpmu->num_events - 1).
1626 #define ARMV7_COUNTER_LAST (ARMV7_COUNTER0 + armpmu->num_events - 1)
1629 * ARMv7 low level PMNC access
1633 * Per-CPU PMNC: config reg
1635 #define ARMV7_PMNC_E (1 << 0) /* Enable all counters */
1636 #define ARMV7_PMNC_P (1 << 1) /* Reset all counters */
1637 #define ARMV7_PMNC_C (1 << 2) /* Cycle counter reset */
1638 #define ARMV7_PMNC_D (1 << 3) /* CCNT counts every 64th cpu cycle */
1639 #define ARMV7_PMNC_X (1 << 4) /* Export to ETM */
1640 #define ARMV7_PMNC_DP (1 << 5) /* Disable CCNT if non-invasive debug*/
1641 #define ARMV7_PMNC_N_SHIFT 11 /* Number of counters supported */
1642 #define ARMV7_PMNC_N_MASK 0x1f
1643 #define ARMV7_PMNC_MASK 0x3f /* Mask for writable bits */
1646 * Available counters
1648 #define ARMV7_CNT0 0 /* First event counter */
1649 #define ARMV7_CCNT 31 /* Cycle counter */
1651 /* Perf Event to low level counters mapping */
1652 #define ARMV7_EVENT_CNT_TO_CNTx (ARMV7_COUNTER0 - ARMV7_CNT0)
1655 * CNTENS: counters enable reg
1657 #define ARMV7_CNTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1658 #define ARMV7_CNTENS_C (1 << ARMV7_CCNT)
1661 * CNTENC: counters disable reg
1663 #define ARMV7_CNTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1664 #define ARMV7_CNTENC_C (1 << ARMV7_CCNT)
1667 * INTENS: counters overflow interrupt enable reg
1669 #define ARMV7_INTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1670 #define ARMV7_INTENS_C (1 << ARMV7_CCNT)
1673 * INTENC: counters overflow interrupt disable reg
1675 #define ARMV7_INTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1676 #define ARMV7_INTENC_C (1 << ARMV7_CCNT)
1679 * EVTSEL: Event selection reg
1681 #define ARMV7_EVTSEL_MASK 0xff /* Mask for writable bits */
1684 * SELECT: Counter selection reg
1686 #define ARMV7_SELECT_MASK 0x1f /* Mask for writable bits */
1689 * FLAG: counters overflow flag status reg
1691 #define ARMV7_FLAG_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1692 #define ARMV7_FLAG_C (1 << ARMV7_CCNT)
1693 #define ARMV7_FLAG_MASK 0xffffffff /* Mask for writable bits */
1694 #define ARMV7_OVERFLOWED_MASK ARMV7_FLAG_MASK
1696 static inline unsigned long armv7_pmnc_read(void)
1699 asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r"(val));
1703 static inline void armv7_pmnc_write(unsigned long val)
1705 val &= ARMV7_PMNC_MASK;
1706 asm volatile("mcr p15, 0, %0, c9, c12, 0" : : "r"(val));
1709 static inline int armv7_pmnc_has_overflowed(unsigned long pmnc)
1711 return pmnc & ARMV7_OVERFLOWED_MASK;
1714 static inline int armv7_pmnc_counter_has_overflowed(unsigned long pmnc,
1715 enum armv7_counters counter)
1719 if (counter == ARMV7_CYCLE_COUNTER)
1720 ret = pmnc & ARMV7_FLAG_C;
1721 else if ((counter >= ARMV7_COUNTER0) && (counter <= ARMV7_COUNTER_LAST))
1722 ret = pmnc & ARMV7_FLAG_P(counter);
1724 pr_err("CPU%u checking wrong counter %d overflow status\n",
1725 smp_processor_id(), counter);
1730 static inline int armv7_pmnc_select_counter(unsigned int idx)
1734 if ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST)) {
1735 pr_err("CPU%u selecting wrong PMNC counter"
1736 " %d\n", smp_processor_id(), idx);
1740 val = (idx - ARMV7_EVENT_CNT_TO_CNTx) & ARMV7_SELECT_MASK;
1741 asm volatile("mcr p15, 0, %0, c9, c12, 5" : : "r" (val));
1746 static inline u32 armv7pmu_read_counter(int idx)
1748 unsigned long value = 0;
1750 if (idx == ARMV7_CYCLE_COUNTER)
1751 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (value));
1752 else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) {
1753 if (armv7_pmnc_select_counter(idx) == idx)
1754 asm volatile("mrc p15, 0, %0, c9, c13, 2"
1757 pr_err("CPU%u reading wrong counter %d\n",
1758 smp_processor_id(), idx);
1763 static inline void armv7pmu_write_counter(int idx, u32 value)
1765 if (idx == ARMV7_CYCLE_COUNTER)
1766 asm volatile("mcr p15, 0, %0, c9, c13, 0" : : "r" (value));
1767 else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) {
1768 if (armv7_pmnc_select_counter(idx) == idx)
1769 asm volatile("mcr p15, 0, %0, c9, c13, 2"
1772 pr_err("CPU%u writing wrong counter %d\n",
1773 smp_processor_id(), idx);
1776 static inline void armv7_pmnc_write_evtsel(unsigned int idx, u32 val)
1778 if (armv7_pmnc_select_counter(idx) == idx) {
1779 val &= ARMV7_EVTSEL_MASK;
1780 asm volatile("mcr p15, 0, %0, c9, c13, 1" : : "r" (val));
1784 static inline u32 armv7_pmnc_enable_counter(unsigned int idx)
1788 if ((idx != ARMV7_CYCLE_COUNTER) &&
1789 ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
1790 pr_err("CPU%u enabling wrong PMNC counter"
1791 " %d\n", smp_processor_id(), idx);
1795 if (idx == ARMV7_CYCLE_COUNTER)
1796 val = ARMV7_CNTENS_C;
1798 val = ARMV7_CNTENS_P(idx);
1800 asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r" (val));
1805 static inline u32 armv7_pmnc_disable_counter(unsigned int idx)
1810 if ((idx != ARMV7_CYCLE_COUNTER) &&
1811 ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
1812 pr_err("CPU%u disabling wrong PMNC counter"
1813 " %d\n", smp_processor_id(), idx);
1817 if (idx == ARMV7_CYCLE_COUNTER)
1818 val = ARMV7_CNTENC_C;
1820 val = ARMV7_CNTENC_P(idx);
1822 asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r" (val));
1827 static inline u32 armv7_pmnc_enable_intens(unsigned int idx)
1831 if ((idx != ARMV7_CYCLE_COUNTER) &&
1832 ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
1833 pr_err("CPU%u enabling wrong PMNC counter"
1834 " interrupt enable %d\n", smp_processor_id(), idx);
1838 if (idx == ARMV7_CYCLE_COUNTER)
1839 val = ARMV7_INTENS_C;
1841 val = ARMV7_INTENS_P(idx);
1843 asm volatile("mcr p15, 0, %0, c9, c14, 1" : : "r" (val));
1848 static inline u32 armv7_pmnc_disable_intens(unsigned int idx)
1852 if ((idx != ARMV7_CYCLE_COUNTER) &&
1853 ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
1854 pr_err("CPU%u disabling wrong PMNC counter"
1855 " interrupt enable %d\n", smp_processor_id(), idx);
1859 if (idx == ARMV7_CYCLE_COUNTER)
1860 val = ARMV7_INTENC_C;
1862 val = ARMV7_INTENC_P(idx);
1864 asm volatile("mcr p15, 0, %0, c9, c14, 2" : : "r" (val));
1869 static inline u32 armv7_pmnc_getreset_flags(void)
1874 asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
1876 /* Write to clear flags */
1877 val &= ARMV7_FLAG_MASK;
1878 asm volatile("mcr p15, 0, %0, c9, c12, 3" : : "r" (val));
1884 static void armv7_pmnc_dump_regs(void)
1889 printk(KERN_INFO "PMNC registers dump:\n");
1891 asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r" (val));
1892 printk(KERN_INFO "PMNC =0x%08x\n", val);
1894 asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r" (val));
1895 printk(KERN_INFO "CNTENS=0x%08x\n", val);
1897 asm volatile("mrc p15, 0, %0, c9, c14, 1" : "=r" (val));
1898 printk(KERN_INFO "INTENS=0x%08x\n", val);
1900 asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
1901 printk(KERN_INFO "FLAGS =0x%08x\n", val);
1903 asm volatile("mrc p15, 0, %0, c9, c12, 5" : "=r" (val));
1904 printk(KERN_INFO "SELECT=0x%08x\n", val);
1906 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (val));
1907 printk(KERN_INFO "CCNT =0x%08x\n", val);
1909 for (cnt = ARMV7_COUNTER0; cnt < ARMV7_COUNTER_LAST; cnt++) {
1910 armv7_pmnc_select_counter(cnt);
1911 asm volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (val));
1912 printk(KERN_INFO "CNT[%d] count =0x%08x\n",
1913 cnt-ARMV7_EVENT_CNT_TO_CNTx, val);
1914 asm volatile("mrc p15, 0, %0, c9, c13, 1" : "=r" (val));
1915 printk(KERN_INFO "CNT[%d] evtsel=0x%08x\n",
1916 cnt-ARMV7_EVENT_CNT_TO_CNTx, val);
1921 void armv7pmu_enable_event(struct hw_perf_event *hwc, int idx)
1923 unsigned long flags;
1926 * Enable counter and interrupt, and set the counter to count
1927 * the event that we're interested in.
1929 spin_lock_irqsave(&pmu_lock, flags);
1934 armv7_pmnc_disable_counter(idx);
1937 * Set event (if destined for PMNx counters)
1938 * We don't need to set the event if it's a cycle count
1940 if (idx != ARMV7_CYCLE_COUNTER)
1941 armv7_pmnc_write_evtsel(idx, hwc->config_base);
1944 * Enable interrupt for this counter
1946 armv7_pmnc_enable_intens(idx);
1951 armv7_pmnc_enable_counter(idx);
1953 spin_unlock_irqrestore(&pmu_lock, flags);
1956 static void armv7pmu_disable_event(struct hw_perf_event *hwc, int idx)
1958 unsigned long flags;
1961 * Disable counter and interrupt
1963 spin_lock_irqsave(&pmu_lock, flags);
1968 armv7_pmnc_disable_counter(idx);
1971 * Disable interrupt for this counter
1973 armv7_pmnc_disable_intens(idx);
1975 spin_unlock_irqrestore(&pmu_lock, flags);
1978 static irqreturn_t armv7pmu_handle_irq(int irq_num, void *dev)
1981 struct perf_sample_data data;
1982 struct cpu_hw_events *cpuc;
1983 struct pt_regs *regs;
1987 * Get and reset the IRQ flags
1989 pmnc = armv7_pmnc_getreset_flags();
1992 * Did an overflow occur?
1994 if (!armv7_pmnc_has_overflowed(pmnc))
1998 * Handle the counter(s) overflow(s)
2000 regs = get_irq_regs();
2002 perf_sample_data_init(&data, 0);
2004 cpuc = &__get_cpu_var(cpu_hw_events);
2005 for (idx = 0; idx <= armpmu->num_events; ++idx) {
2006 struct perf_event *event = cpuc->events[idx];
2007 struct hw_perf_event *hwc;
2009 if (!test_bit(idx, cpuc->active_mask))
2013 * We have a single interrupt for all counters. Check that
2014 * each counter has overflowed before we process it.
2016 if (!armv7_pmnc_counter_has_overflowed(pmnc, idx))
2020 armpmu_event_update(event, hwc, idx);
2021 data.period = event->hw.last_period;
2022 if (!armpmu_event_set_period(event, hwc, idx))
2025 if (perf_event_overflow(event, 0, &data, regs))
2026 armpmu->disable(hwc, idx);
2030 * Handle the pending perf events.
2032 * Note: this call *must* be run with interrupts enabled. For
2033 * platforms that can have the PMU interrupts raised as a PMI, this
2036 perf_event_do_pending();
2041 static void armv7pmu_start(void)
2043 unsigned long flags;
2045 spin_lock_irqsave(&pmu_lock, flags);
2046 /* Enable all counters */
2047 armv7_pmnc_write(armv7_pmnc_read() | ARMV7_PMNC_E);
2048 spin_unlock_irqrestore(&pmu_lock, flags);
2051 static void armv7pmu_stop(void)
2053 unsigned long flags;
2055 spin_lock_irqsave(&pmu_lock, flags);
2056 /* Disable all counters */
2057 armv7_pmnc_write(armv7_pmnc_read() & ~ARMV7_PMNC_E);
2058 spin_unlock_irqrestore(&pmu_lock, flags);
2061 static inline int armv7_a8_pmu_event_map(int config)
2063 int mapping = armv7_a8_perf_map[config];
2064 if (HW_OP_UNSUPPORTED == mapping)
2065 mapping = -EOPNOTSUPP;
2069 static inline int armv7_a9_pmu_event_map(int config)
2071 int mapping = armv7_a9_perf_map[config];
2072 if (HW_OP_UNSUPPORTED == mapping)
2073 mapping = -EOPNOTSUPP;
2077 static u64 armv7pmu_raw_event(u64 config)
2079 return config & 0xff;
2082 static int armv7pmu_get_event_idx(struct cpu_hw_events *cpuc,
2083 struct hw_perf_event *event)
2087 /* Always place a cycle counter into the cycle counter. */
2088 if (event->config_base == ARMV7_PERFCTR_CPU_CYCLES) {
2089 if (test_and_set_bit(ARMV7_CYCLE_COUNTER, cpuc->used_mask))
2092 return ARMV7_CYCLE_COUNTER;
2095 * For anything other than a cycle counter, try and use
2096 * the events counters
2098 for (idx = ARMV7_COUNTER0; idx <= armpmu->num_events; ++idx) {
2099 if (!test_and_set_bit(idx, cpuc->used_mask))
2103 /* The counters are all in use. */
2108 static struct arm_pmu armv7pmu = {
2109 .handle_irq = armv7pmu_handle_irq,
2110 .enable = armv7pmu_enable_event,
2111 .disable = armv7pmu_disable_event,
2112 .raw_event = armv7pmu_raw_event,
2113 .read_counter = armv7pmu_read_counter,
2114 .write_counter = armv7pmu_write_counter,
2115 .get_event_idx = armv7pmu_get_event_idx,
2116 .start = armv7pmu_start,
2117 .stop = armv7pmu_stop,
2118 .max_period = (1LLU << 32) - 1,
2121 static u32 __init armv7_reset_read_pmnc(void)
2125 /* Initialize & Reset PMNC: C and P bits */
2126 armv7_pmnc_write(ARMV7_PMNC_P | ARMV7_PMNC_C);
2128 /* Read the nb of CNTx counters supported from PMNC */
2129 nb_cnt = (armv7_pmnc_read() >> ARMV7_PMNC_N_SHIFT) & ARMV7_PMNC_N_MASK;
2131 /* Add the CPU cycles counter and return */
2136 * ARMv5 [xscale] Performance counter handling code.
2138 * Based on xscale OProfile code.
2140 * There are two variants of the xscale PMU that we support:
2141 * - xscale1pmu: 2 event counters and a cycle counter
2142 * - xscale2pmu: 4 event counters and a cycle counter
2143 * The two variants share event definitions, but have different
2147 enum xscale_perf_types {
2148 XSCALE_PERFCTR_ICACHE_MISS = 0x00,
2149 XSCALE_PERFCTR_ICACHE_NO_DELIVER = 0x01,
2150 XSCALE_PERFCTR_DATA_STALL = 0x02,
2151 XSCALE_PERFCTR_ITLB_MISS = 0x03,
2152 XSCALE_PERFCTR_DTLB_MISS = 0x04,
2153 XSCALE_PERFCTR_BRANCH = 0x05,
2154 XSCALE_PERFCTR_BRANCH_MISS = 0x06,
2155 XSCALE_PERFCTR_INSTRUCTION = 0x07,
2156 XSCALE_PERFCTR_DCACHE_FULL_STALL = 0x08,
2157 XSCALE_PERFCTR_DCACHE_FULL_STALL_CONTIG = 0x09,
2158 XSCALE_PERFCTR_DCACHE_ACCESS = 0x0A,
2159 XSCALE_PERFCTR_DCACHE_MISS = 0x0B,
2160 XSCALE_PERFCTR_DCACHE_WRITE_BACK = 0x0C,
2161 XSCALE_PERFCTR_PC_CHANGED = 0x0D,
2162 XSCALE_PERFCTR_BCU_REQUEST = 0x10,
2163 XSCALE_PERFCTR_BCU_FULL = 0x11,
2164 XSCALE_PERFCTR_BCU_DRAIN = 0x12,
2165 XSCALE_PERFCTR_BCU_ECC_NO_ELOG = 0x14,
2166 XSCALE_PERFCTR_BCU_1_BIT_ERR = 0x15,
2167 XSCALE_PERFCTR_RMW = 0x16,
2168 /* XSCALE_PERFCTR_CCNT is not hardware defined */
2169 XSCALE_PERFCTR_CCNT = 0xFE,
2170 XSCALE_PERFCTR_UNUSED = 0xFF,
2173 enum xscale_counters {
2174 XSCALE_CYCLE_COUNTER = 1,
2181 static const unsigned xscale_perf_map[PERF_COUNT_HW_MAX] = {
2182 [PERF_COUNT_HW_CPU_CYCLES] = XSCALE_PERFCTR_CCNT,
2183 [PERF_COUNT_HW_INSTRUCTIONS] = XSCALE_PERFCTR_INSTRUCTION,
2184 [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
2185 [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
2186 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = XSCALE_PERFCTR_BRANCH,
2187 [PERF_COUNT_HW_BRANCH_MISSES] = XSCALE_PERFCTR_BRANCH_MISS,
2188 [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
2191 static const unsigned xscale_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
2192 [PERF_COUNT_HW_CACHE_OP_MAX]
2193 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
2196 [C(RESULT_ACCESS)] = XSCALE_PERFCTR_DCACHE_ACCESS,
2197 [C(RESULT_MISS)] = XSCALE_PERFCTR_DCACHE_MISS,
2200 [C(RESULT_ACCESS)] = XSCALE_PERFCTR_DCACHE_ACCESS,
2201 [C(RESULT_MISS)] = XSCALE_PERFCTR_DCACHE_MISS,
2203 [C(OP_PREFETCH)] = {
2204 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2205 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2210 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2211 [C(RESULT_MISS)] = XSCALE_PERFCTR_ICACHE_MISS,
2214 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2215 [C(RESULT_MISS)] = XSCALE_PERFCTR_ICACHE_MISS,
2217 [C(OP_PREFETCH)] = {
2218 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2219 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2224 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2225 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2228 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2229 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2231 [C(OP_PREFETCH)] = {
2232 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2233 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2238 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2239 [C(RESULT_MISS)] = XSCALE_PERFCTR_DTLB_MISS,
2242 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2243 [C(RESULT_MISS)] = XSCALE_PERFCTR_DTLB_MISS,
2245 [C(OP_PREFETCH)] = {
2246 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2247 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2252 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2253 [C(RESULT_MISS)] = XSCALE_PERFCTR_ITLB_MISS,
2256 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2257 [C(RESULT_MISS)] = XSCALE_PERFCTR_ITLB_MISS,
2259 [C(OP_PREFETCH)] = {
2260 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2261 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2266 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2267 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2270 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2271 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2273 [C(OP_PREFETCH)] = {
2274 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2275 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2280 #define XSCALE_PMU_ENABLE 0x001
2281 #define XSCALE_PMN_RESET 0x002
2282 #define XSCALE_CCNT_RESET 0x004
2283 #define XSCALE_PMU_RESET (CCNT_RESET | PMN_RESET)
2284 #define XSCALE_PMU_CNT64 0x008
2287 xscalepmu_event_map(int config)
2289 int mapping = xscale_perf_map[config];
2290 if (HW_OP_UNSUPPORTED == mapping)
2291 mapping = -EOPNOTSUPP;
2296 xscalepmu_raw_event(u64 config)
2298 return config & 0xff;
2301 #define XSCALE1_OVERFLOWED_MASK 0x700
2302 #define XSCALE1_CCOUNT_OVERFLOW 0x400
2303 #define XSCALE1_COUNT0_OVERFLOW 0x100
2304 #define XSCALE1_COUNT1_OVERFLOW 0x200
2305 #define XSCALE1_CCOUNT_INT_EN 0x040
2306 #define XSCALE1_COUNT0_INT_EN 0x010
2307 #define XSCALE1_COUNT1_INT_EN 0x020
2308 #define XSCALE1_COUNT0_EVT_SHFT 12
2309 #define XSCALE1_COUNT0_EVT_MASK (0xff << XSCALE1_COUNT0_EVT_SHFT)
2310 #define XSCALE1_COUNT1_EVT_SHFT 20
2311 #define XSCALE1_COUNT1_EVT_MASK (0xff << XSCALE1_COUNT1_EVT_SHFT)
2314 xscale1pmu_read_pmnc(void)
2317 asm volatile("mrc p14, 0, %0, c0, c0, 0" : "=r" (val));
2322 xscale1pmu_write_pmnc(u32 val)
2324 /* upper 4bits and 7, 11 are write-as-0 */
2326 asm volatile("mcr p14, 0, %0, c0, c0, 0" : : "r" (val));
2330 xscale1_pmnc_counter_has_overflowed(unsigned long pmnc,
2331 enum xscale_counters counter)
2336 case XSCALE_CYCLE_COUNTER:
2337 ret = pmnc & XSCALE1_CCOUNT_OVERFLOW;
2339 case XSCALE_COUNTER0:
2340 ret = pmnc & XSCALE1_COUNT0_OVERFLOW;
2342 case XSCALE_COUNTER1:
2343 ret = pmnc & XSCALE1_COUNT1_OVERFLOW;
2346 WARN_ONCE(1, "invalid counter number (%d)\n", counter);
2353 xscale1pmu_handle_irq(int irq_num, void *dev)
2356 struct perf_sample_data data;
2357 struct cpu_hw_events *cpuc;
2358 struct pt_regs *regs;
2362 * NOTE: there's an A stepping erratum that states if an overflow
2363 * bit already exists and another occurs, the previous
2364 * Overflow bit gets cleared. There's no workaround.
2365 * Fixed in B stepping or later.
2367 pmnc = xscale1pmu_read_pmnc();
2370 * Write the value back to clear the overflow flags. Overflow
2371 * flags remain in pmnc for use below. We also disable the PMU
2372 * while we process the interrupt.
2374 xscale1pmu_write_pmnc(pmnc & ~XSCALE_PMU_ENABLE);
2376 if (!(pmnc & XSCALE1_OVERFLOWED_MASK))
2379 regs = get_irq_regs();
2381 perf_sample_data_init(&data, 0);
2383 cpuc = &__get_cpu_var(cpu_hw_events);
2384 for (idx = 0; idx <= armpmu->num_events; ++idx) {
2385 struct perf_event *event = cpuc->events[idx];
2386 struct hw_perf_event *hwc;
2388 if (!test_bit(idx, cpuc->active_mask))
2391 if (!xscale1_pmnc_counter_has_overflowed(pmnc, idx))
2395 armpmu_event_update(event, hwc, idx);
2396 data.period = event->hw.last_period;
2397 if (!armpmu_event_set_period(event, hwc, idx))
2400 if (perf_event_overflow(event, 0, &data, regs))
2401 armpmu->disable(hwc, idx);
2404 perf_event_do_pending();
2407 * Re-enable the PMU.
2409 pmnc = xscale1pmu_read_pmnc() | XSCALE_PMU_ENABLE;
2410 xscale1pmu_write_pmnc(pmnc);
2416 xscale1pmu_enable_event(struct hw_perf_event *hwc, int idx)
2418 unsigned long val, mask, evt, flags;
2421 case XSCALE_CYCLE_COUNTER:
2423 evt = XSCALE1_CCOUNT_INT_EN;
2425 case XSCALE_COUNTER0:
2426 mask = XSCALE1_COUNT0_EVT_MASK;
2427 evt = (hwc->config_base << XSCALE1_COUNT0_EVT_SHFT) |
2428 XSCALE1_COUNT0_INT_EN;
2430 case XSCALE_COUNTER1:
2431 mask = XSCALE1_COUNT1_EVT_MASK;
2432 evt = (hwc->config_base << XSCALE1_COUNT1_EVT_SHFT) |
2433 XSCALE1_COUNT1_INT_EN;
2436 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
2440 spin_lock_irqsave(&pmu_lock, flags);
2441 val = xscale1pmu_read_pmnc();
2444 xscale1pmu_write_pmnc(val);
2445 spin_unlock_irqrestore(&pmu_lock, flags);
2449 xscale1pmu_disable_event(struct hw_perf_event *hwc, int idx)
2451 unsigned long val, mask, evt, flags;
2454 case XSCALE_CYCLE_COUNTER:
2455 mask = XSCALE1_CCOUNT_INT_EN;
2458 case XSCALE_COUNTER0:
2459 mask = XSCALE1_COUNT0_INT_EN | XSCALE1_COUNT0_EVT_MASK;
2460 evt = XSCALE_PERFCTR_UNUSED << XSCALE1_COUNT0_EVT_SHFT;
2462 case XSCALE_COUNTER1:
2463 mask = XSCALE1_COUNT1_INT_EN | XSCALE1_COUNT1_EVT_MASK;
2464 evt = XSCALE_PERFCTR_UNUSED << XSCALE1_COUNT1_EVT_SHFT;
2467 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
2471 spin_lock_irqsave(&pmu_lock, flags);
2472 val = xscale1pmu_read_pmnc();
2475 xscale1pmu_write_pmnc(val);
2476 spin_unlock_irqrestore(&pmu_lock, flags);
2480 xscale1pmu_get_event_idx(struct cpu_hw_events *cpuc,
2481 struct hw_perf_event *event)
2483 if (XSCALE_PERFCTR_CCNT == event->config_base) {
2484 if (test_and_set_bit(XSCALE_CYCLE_COUNTER, cpuc->used_mask))
2487 return XSCALE_CYCLE_COUNTER;
2489 if (!test_and_set_bit(XSCALE_COUNTER1, cpuc->used_mask)) {
2490 return XSCALE_COUNTER1;
2493 if (!test_and_set_bit(XSCALE_COUNTER0, cpuc->used_mask)) {
2494 return XSCALE_COUNTER0;
2502 xscale1pmu_start(void)
2504 unsigned long flags, val;
2506 spin_lock_irqsave(&pmu_lock, flags);
2507 val = xscale1pmu_read_pmnc();
2508 val |= XSCALE_PMU_ENABLE;
2509 xscale1pmu_write_pmnc(val);
2510 spin_unlock_irqrestore(&pmu_lock, flags);
2514 xscale1pmu_stop(void)
2516 unsigned long flags, val;
2518 spin_lock_irqsave(&pmu_lock, flags);
2519 val = xscale1pmu_read_pmnc();
2520 val &= ~XSCALE_PMU_ENABLE;
2521 xscale1pmu_write_pmnc(val);
2522 spin_unlock_irqrestore(&pmu_lock, flags);
2526 xscale1pmu_read_counter(int counter)
2531 case XSCALE_CYCLE_COUNTER:
2532 asm volatile("mrc p14, 0, %0, c1, c0, 0" : "=r" (val));
2534 case XSCALE_COUNTER0:
2535 asm volatile("mrc p14, 0, %0, c2, c0, 0" : "=r" (val));
2537 case XSCALE_COUNTER1:
2538 asm volatile("mrc p14, 0, %0, c3, c0, 0" : "=r" (val));
2546 xscale1pmu_write_counter(int counter, u32 val)
2549 case XSCALE_CYCLE_COUNTER:
2550 asm volatile("mcr p14, 0, %0, c1, c0, 0" : : "r" (val));
2552 case XSCALE_COUNTER0:
2553 asm volatile("mcr p14, 0, %0, c2, c0, 0" : : "r" (val));
2555 case XSCALE_COUNTER1:
2556 asm volatile("mcr p14, 0, %0, c3, c0, 0" : : "r" (val));
2561 static const struct arm_pmu xscale1pmu = {
2562 .id = ARM_PERF_PMU_ID_XSCALE1,
2563 .handle_irq = xscale1pmu_handle_irq,
2564 .enable = xscale1pmu_enable_event,
2565 .disable = xscale1pmu_disable_event,
2566 .event_map = xscalepmu_event_map,
2567 .raw_event = xscalepmu_raw_event,
2568 .read_counter = xscale1pmu_read_counter,
2569 .write_counter = xscale1pmu_write_counter,
2570 .get_event_idx = xscale1pmu_get_event_idx,
2571 .start = xscale1pmu_start,
2572 .stop = xscale1pmu_stop,
2574 .max_period = (1LLU << 32) - 1,
2577 #define XSCALE2_OVERFLOWED_MASK 0x01f
2578 #define XSCALE2_CCOUNT_OVERFLOW 0x001
2579 #define XSCALE2_COUNT0_OVERFLOW 0x002
2580 #define XSCALE2_COUNT1_OVERFLOW 0x004
2581 #define XSCALE2_COUNT2_OVERFLOW 0x008
2582 #define XSCALE2_COUNT3_OVERFLOW 0x010
2583 #define XSCALE2_CCOUNT_INT_EN 0x001
2584 #define XSCALE2_COUNT0_INT_EN 0x002
2585 #define XSCALE2_COUNT1_INT_EN 0x004
2586 #define XSCALE2_COUNT2_INT_EN 0x008
2587 #define XSCALE2_COUNT3_INT_EN 0x010
2588 #define XSCALE2_COUNT0_EVT_SHFT 0
2589 #define XSCALE2_COUNT0_EVT_MASK (0xff << XSCALE2_COUNT0_EVT_SHFT)
2590 #define XSCALE2_COUNT1_EVT_SHFT 8
2591 #define XSCALE2_COUNT1_EVT_MASK (0xff << XSCALE2_COUNT1_EVT_SHFT)
2592 #define XSCALE2_COUNT2_EVT_SHFT 16
2593 #define XSCALE2_COUNT2_EVT_MASK (0xff << XSCALE2_COUNT2_EVT_SHFT)
2594 #define XSCALE2_COUNT3_EVT_SHFT 24
2595 #define XSCALE2_COUNT3_EVT_MASK (0xff << XSCALE2_COUNT3_EVT_SHFT)
2598 xscale2pmu_read_pmnc(void)
2601 asm volatile("mrc p14, 0, %0, c0, c1, 0" : "=r" (val));
2602 /* bits 1-2 and 4-23 are read-unpredictable */
2603 return val & 0xff000009;
2607 xscale2pmu_write_pmnc(u32 val)
2609 /* bits 4-23 are write-as-0, 24-31 are write ignored */
2611 asm volatile("mcr p14, 0, %0, c0, c1, 0" : : "r" (val));
2615 xscale2pmu_read_overflow_flags(void)
2618 asm volatile("mrc p14, 0, %0, c5, c1, 0" : "=r" (val));
2623 xscale2pmu_write_overflow_flags(u32 val)
2625 asm volatile("mcr p14, 0, %0, c5, c1, 0" : : "r" (val));
2629 xscale2pmu_read_event_select(void)
2632 asm volatile("mrc p14, 0, %0, c8, c1, 0" : "=r" (val));
2637 xscale2pmu_write_event_select(u32 val)
2639 asm volatile("mcr p14, 0, %0, c8, c1, 0" : : "r"(val));
2643 xscale2pmu_read_int_enable(void)
2646 asm volatile("mrc p14, 0, %0, c4, c1, 0" : "=r" (val));
2651 xscale2pmu_write_int_enable(u32 val)
2653 asm volatile("mcr p14, 0, %0, c4, c1, 0" : : "r" (val));
2657 xscale2_pmnc_counter_has_overflowed(unsigned long of_flags,
2658 enum xscale_counters counter)
2663 case XSCALE_CYCLE_COUNTER:
2664 ret = of_flags & XSCALE2_CCOUNT_OVERFLOW;
2666 case XSCALE_COUNTER0:
2667 ret = of_flags & XSCALE2_COUNT0_OVERFLOW;
2669 case XSCALE_COUNTER1:
2670 ret = of_flags & XSCALE2_COUNT1_OVERFLOW;
2672 case XSCALE_COUNTER2:
2673 ret = of_flags & XSCALE2_COUNT2_OVERFLOW;
2675 case XSCALE_COUNTER3:
2676 ret = of_flags & XSCALE2_COUNT3_OVERFLOW;
2679 WARN_ONCE(1, "invalid counter number (%d)\n", counter);
2686 xscale2pmu_handle_irq(int irq_num, void *dev)
2688 unsigned long pmnc, of_flags;
2689 struct perf_sample_data data;
2690 struct cpu_hw_events *cpuc;
2691 struct pt_regs *regs;
2694 /* Disable the PMU. */
2695 pmnc = xscale2pmu_read_pmnc();
2696 xscale2pmu_write_pmnc(pmnc & ~XSCALE_PMU_ENABLE);
2698 /* Check the overflow flag register. */
2699 of_flags = xscale2pmu_read_overflow_flags();
2700 if (!(of_flags & XSCALE2_OVERFLOWED_MASK))
2703 /* Clear the overflow bits. */
2704 xscale2pmu_write_overflow_flags(of_flags);
2706 regs = get_irq_regs();
2708 perf_sample_data_init(&data, 0);
2710 cpuc = &__get_cpu_var(cpu_hw_events);
2711 for (idx = 0; idx <= armpmu->num_events; ++idx) {
2712 struct perf_event *event = cpuc->events[idx];
2713 struct hw_perf_event *hwc;
2715 if (!test_bit(idx, cpuc->active_mask))
2718 if (!xscale2_pmnc_counter_has_overflowed(pmnc, idx))
2722 armpmu_event_update(event, hwc, idx);
2723 data.period = event->hw.last_period;
2724 if (!armpmu_event_set_period(event, hwc, idx))
2727 if (perf_event_overflow(event, 0, &data, regs))
2728 armpmu->disable(hwc, idx);
2731 perf_event_do_pending();
2734 * Re-enable the PMU.
2736 pmnc = xscale2pmu_read_pmnc() | XSCALE_PMU_ENABLE;
2737 xscale2pmu_write_pmnc(pmnc);
2743 xscale2pmu_enable_event(struct hw_perf_event *hwc, int idx)
2745 unsigned long flags, ien, evtsel;
2747 ien = xscale2pmu_read_int_enable();
2748 evtsel = xscale2pmu_read_event_select();
2751 case XSCALE_CYCLE_COUNTER:
2752 ien |= XSCALE2_CCOUNT_INT_EN;
2754 case XSCALE_COUNTER0:
2755 ien |= XSCALE2_COUNT0_INT_EN;
2756 evtsel &= ~XSCALE2_COUNT0_EVT_MASK;
2757 evtsel |= hwc->config_base << XSCALE2_COUNT0_EVT_SHFT;
2759 case XSCALE_COUNTER1:
2760 ien |= XSCALE2_COUNT1_INT_EN;
2761 evtsel &= ~XSCALE2_COUNT1_EVT_MASK;
2762 evtsel |= hwc->config_base << XSCALE2_COUNT1_EVT_SHFT;
2764 case XSCALE_COUNTER2:
2765 ien |= XSCALE2_COUNT2_INT_EN;
2766 evtsel &= ~XSCALE2_COUNT2_EVT_MASK;
2767 evtsel |= hwc->config_base << XSCALE2_COUNT2_EVT_SHFT;
2769 case XSCALE_COUNTER3:
2770 ien |= XSCALE2_COUNT3_INT_EN;
2771 evtsel &= ~XSCALE2_COUNT3_EVT_MASK;
2772 evtsel |= hwc->config_base << XSCALE2_COUNT3_EVT_SHFT;
2775 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
2779 spin_lock_irqsave(&pmu_lock, flags);
2780 xscale2pmu_write_event_select(evtsel);
2781 xscale2pmu_write_int_enable(ien);
2782 spin_unlock_irqrestore(&pmu_lock, flags);
2786 xscale2pmu_disable_event(struct hw_perf_event *hwc, int idx)
2788 unsigned long flags, ien, evtsel;
2790 ien = xscale2pmu_read_int_enable();
2791 evtsel = xscale2pmu_read_event_select();
2794 case XSCALE_CYCLE_COUNTER:
2795 ien &= ~XSCALE2_CCOUNT_INT_EN;
2797 case XSCALE_COUNTER0:
2798 ien &= ~XSCALE2_COUNT0_INT_EN;
2799 evtsel &= ~XSCALE2_COUNT0_EVT_MASK;
2800 evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT0_EVT_SHFT;
2802 case XSCALE_COUNTER1:
2803 ien &= ~XSCALE2_COUNT1_INT_EN;
2804 evtsel &= ~XSCALE2_COUNT1_EVT_MASK;
2805 evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT1_EVT_SHFT;
2807 case XSCALE_COUNTER2:
2808 ien &= ~XSCALE2_COUNT2_INT_EN;
2809 evtsel &= ~XSCALE2_COUNT2_EVT_MASK;
2810 evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT2_EVT_SHFT;
2812 case XSCALE_COUNTER3:
2813 ien &= ~XSCALE2_COUNT3_INT_EN;
2814 evtsel &= ~XSCALE2_COUNT3_EVT_MASK;
2815 evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT3_EVT_SHFT;
2818 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
2822 spin_lock_irqsave(&pmu_lock, flags);
2823 xscale2pmu_write_event_select(evtsel);
2824 xscale2pmu_write_int_enable(ien);
2825 spin_unlock_irqrestore(&pmu_lock, flags);
2829 xscale2pmu_get_event_idx(struct cpu_hw_events *cpuc,
2830 struct hw_perf_event *event)
2832 int idx = xscale1pmu_get_event_idx(cpuc, event);
2836 if (!test_and_set_bit(XSCALE_COUNTER3, cpuc->used_mask))
2837 idx = XSCALE_COUNTER3;
2838 else if (!test_and_set_bit(XSCALE_COUNTER2, cpuc->used_mask))
2839 idx = XSCALE_COUNTER2;
2845 xscale2pmu_start(void)
2847 unsigned long flags, val;
2849 spin_lock_irqsave(&pmu_lock, flags);
2850 val = xscale2pmu_read_pmnc() & ~XSCALE_PMU_CNT64;
2851 val |= XSCALE_PMU_ENABLE;
2852 xscale2pmu_write_pmnc(val);
2853 spin_unlock_irqrestore(&pmu_lock, flags);
2857 xscale2pmu_stop(void)
2859 unsigned long flags, val;
2861 spin_lock_irqsave(&pmu_lock, flags);
2862 val = xscale2pmu_read_pmnc();
2863 val &= ~XSCALE_PMU_ENABLE;
2864 xscale2pmu_write_pmnc(val);
2865 spin_unlock_irqrestore(&pmu_lock, flags);
2869 xscale2pmu_read_counter(int counter)
2874 case XSCALE_CYCLE_COUNTER:
2875 asm volatile("mrc p14, 0, %0, c1, c1, 0" : "=r" (val));
2877 case XSCALE_COUNTER0:
2878 asm volatile("mrc p14, 0, %0, c0, c2, 0" : "=r" (val));
2880 case XSCALE_COUNTER1:
2881 asm volatile("mrc p14, 0, %0, c1, c2, 0" : "=r" (val));
2883 case XSCALE_COUNTER2:
2884 asm volatile("mrc p14, 0, %0, c2, c2, 0" : "=r" (val));
2886 case XSCALE_COUNTER3:
2887 asm volatile("mrc p14, 0, %0, c3, c2, 0" : "=r" (val));
2895 xscale2pmu_write_counter(int counter, u32 val)
2898 case XSCALE_CYCLE_COUNTER:
2899 asm volatile("mcr p14, 0, %0, c1, c1, 0" : : "r" (val));
2901 case XSCALE_COUNTER0:
2902 asm volatile("mcr p14, 0, %0, c0, c2, 0" : : "r" (val));
2904 case XSCALE_COUNTER1:
2905 asm volatile("mcr p14, 0, %0, c1, c2, 0" : : "r" (val));
2907 case XSCALE_COUNTER2:
2908 asm volatile("mcr p14, 0, %0, c2, c2, 0" : : "r" (val));
2910 case XSCALE_COUNTER3:
2911 asm volatile("mcr p14, 0, %0, c3, c2, 0" : : "r" (val));
2916 static const struct arm_pmu xscale2pmu = {
2917 .id = ARM_PERF_PMU_ID_XSCALE2,
2918 .handle_irq = xscale2pmu_handle_irq,
2919 .enable = xscale2pmu_enable_event,
2920 .disable = xscale2pmu_disable_event,
2921 .event_map = xscalepmu_event_map,
2922 .raw_event = xscalepmu_raw_event,
2923 .read_counter = xscale2pmu_read_counter,
2924 .write_counter = xscale2pmu_write_counter,
2925 .get_event_idx = xscale2pmu_get_event_idx,
2926 .start = xscale2pmu_start,
2927 .stop = xscale2pmu_stop,
2929 .max_period = (1LLU << 32) - 1,
2933 init_hw_perf_events(void)
2935 unsigned long cpuid = read_cpuid_id();
2936 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
2937 unsigned long part_number = (cpuid & 0xFFF0);
2940 if (0x41 == implementor) {
2941 switch (part_number) {
2942 case 0xB360: /* ARM1136 */
2943 case 0xB560: /* ARM1156 */
2944 case 0xB760: /* ARM1176 */
2946 memcpy(armpmu_perf_cache_map, armv6_perf_cache_map,
2947 sizeof(armv6_perf_cache_map));
2948 perf_max_events = armv6pmu.num_events;
2950 case 0xB020: /* ARM11mpcore */
2951 armpmu = &armv6mpcore_pmu;
2952 memcpy(armpmu_perf_cache_map,
2953 armv6mpcore_perf_cache_map,
2954 sizeof(armv6mpcore_perf_cache_map));
2955 perf_max_events = armv6mpcore_pmu.num_events;
2957 case 0xC080: /* Cortex-A8 */
2958 armv7pmu.id = ARM_PERF_PMU_ID_CA8;
2959 memcpy(armpmu_perf_cache_map, armv7_a8_perf_cache_map,
2960 sizeof(armv7_a8_perf_cache_map));
2961 armv7pmu.event_map = armv7_a8_pmu_event_map;
2964 /* Reset PMNC and read the nb of CNTx counters
2966 armv7pmu.num_events = armv7_reset_read_pmnc();
2967 perf_max_events = armv7pmu.num_events;
2969 case 0xC090: /* Cortex-A9 */
2970 armv7pmu.id = ARM_PERF_PMU_ID_CA9;
2971 memcpy(armpmu_perf_cache_map, armv7_a9_perf_cache_map,
2972 sizeof(armv7_a9_perf_cache_map));
2973 armv7pmu.event_map = armv7_a9_pmu_event_map;
2976 /* Reset PMNC and read the nb of CNTx counters
2978 armv7pmu.num_events = armv7_reset_read_pmnc();
2979 perf_max_events = armv7pmu.num_events;
2982 /* Intel CPUs [xscale]. */
2983 } else if (0x69 == implementor) {
2984 part_number = (cpuid >> 13) & 0x7;
2985 switch (part_number) {
2987 armpmu = &xscale1pmu;
2988 memcpy(armpmu_perf_cache_map, xscale_perf_cache_map,
2989 sizeof(xscale_perf_cache_map));
2990 perf_max_events = xscale1pmu.num_events;
2993 armpmu = &xscale2pmu;
2994 memcpy(armpmu_perf_cache_map, xscale_perf_cache_map,
2995 sizeof(xscale_perf_cache_map));
2996 perf_max_events = xscale2pmu.num_events;
3002 pr_info("enabled with %s PMU driver, %d counters available\n",
3003 arm_pmu_names[armpmu->id], armpmu->num_events);
3005 pr_info("no hardware support available\n");
3006 perf_max_events = -1;
3009 perf_pmu_register(&pmu);
3013 arch_initcall(init_hw_perf_events);
3016 * Callchain handling code.
3020 * The registers we're interested in are at the end of the variable
3021 * length saved register structure. The fp points at the end of this
3022 * structure so the address of this struct is:
3023 * (struct frame_tail *)(xxx->fp)-1
3025 * This code has been adapted from the ARM OProfile support.
3028 struct frame_tail *fp;
3031 } __attribute__((packed));
3034 * Get the return address for a single stackframe and return a pointer to the
3037 static struct frame_tail *
3038 user_backtrace(struct frame_tail *tail,
3039 struct perf_callchain_entry *entry)
3041 struct frame_tail buftail;
3043 /* Also check accessibility of one struct frame_tail beyond */
3044 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
3046 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
3049 perf_callchain_store(entry, buftail.lr);
3052 * Frame pointers should strictly progress back up the stack
3053 * (towards higher addresses).
3055 if (tail >= buftail.fp)
3058 return buftail.fp - 1;
3062 perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
3064 struct frame_tail *tail;
3067 tail = (struct frame_tail *)regs->ARM_fp - 1;
3069 while (tail && !((unsigned long)tail & 0x3))
3070 tail = user_backtrace(tail, entry);
3074 * Gets called by walk_stackframe() for every stackframe. This will be called
3075 * whist unwinding the stackframe and is like a subroutine return so we use
3079 callchain_trace(struct stackframe *fr,
3082 struct perf_callchain_entry *entry = data;
3083 perf_callchain_store(entry, fr->pc);
3088 perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
3090 struct stackframe fr;
3092 fr.fp = regs->ARM_fp;
3093 fr.sp = regs->ARM_sp;
3094 fr.lr = regs->ARM_lr;
3095 fr.pc = regs->ARM_pc;
3096 walk_stackframe(&fr, callchain_trace, entry);