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 = {
310 .enable = armpmu_enable,
311 .disable = armpmu_disable,
312 .unthrottle = armpmu_unthrottle,
317 validate_event(struct cpu_hw_events *cpuc,
318 struct perf_event *event)
320 struct hw_perf_event fake_event = event->hw;
322 if (event->pmu && event->pmu != &pmu)
325 return armpmu->get_event_idx(cpuc, &fake_event) >= 0;
329 validate_group(struct perf_event *event)
331 struct perf_event *sibling, *leader = event->group_leader;
332 struct cpu_hw_events fake_pmu;
334 memset(&fake_pmu, 0, sizeof(fake_pmu));
336 if (!validate_event(&fake_pmu, leader))
339 list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
340 if (!validate_event(&fake_pmu, sibling))
344 if (!validate_event(&fake_pmu, event))
351 armpmu_reserve_hardware(void)
353 int i, err = -ENODEV, irq;
355 pmu_device = reserve_pmu(ARM_PMU_DEVICE_CPU);
356 if (IS_ERR(pmu_device)) {
357 pr_warning("unable to reserve pmu\n");
358 return PTR_ERR(pmu_device);
361 init_pmu(ARM_PMU_DEVICE_CPU);
363 if (pmu_device->num_resources < 1) {
364 pr_err("no irqs for PMUs defined\n");
368 for (i = 0; i < pmu_device->num_resources; ++i) {
369 irq = platform_get_irq(pmu_device, i);
373 err = request_irq(irq, armpmu->handle_irq,
374 IRQF_DISABLED | IRQF_NOBALANCING,
377 pr_warning("unable to request IRQ%d for ARM perf "
384 for (i = i - 1; i >= 0; --i) {
385 irq = platform_get_irq(pmu_device, i);
389 release_pmu(pmu_device);
397 armpmu_release_hardware(void)
401 for (i = pmu_device->num_resources - 1; i >= 0; --i) {
402 irq = platform_get_irq(pmu_device, i);
408 release_pmu(pmu_device);
412 static atomic_t active_events = ATOMIC_INIT(0);
413 static DEFINE_MUTEX(pmu_reserve_mutex);
416 hw_perf_event_destroy(struct perf_event *event)
418 if (atomic_dec_and_mutex_lock(&active_events, &pmu_reserve_mutex)) {
419 armpmu_release_hardware();
420 mutex_unlock(&pmu_reserve_mutex);
425 __hw_perf_event_init(struct perf_event *event)
427 struct hw_perf_event *hwc = &event->hw;
430 /* Decode the generic type into an ARM event identifier. */
431 if (PERF_TYPE_HARDWARE == event->attr.type) {
432 mapping = armpmu->event_map(event->attr.config);
433 } else if (PERF_TYPE_HW_CACHE == event->attr.type) {
434 mapping = armpmu_map_cache_event(event->attr.config);
435 } else if (PERF_TYPE_RAW == event->attr.type) {
436 mapping = armpmu->raw_event(event->attr.config);
438 pr_debug("event type %x not supported\n", event->attr.type);
443 pr_debug("event %x:%llx not supported\n", event->attr.type,
449 * Check whether we need to exclude the counter from certain modes.
450 * The ARM performance counters are on all of the time so if someone
451 * has asked us for some excludes then we have to fail.
453 if (event->attr.exclude_kernel || event->attr.exclude_user ||
454 event->attr.exclude_hv || event->attr.exclude_idle) {
455 pr_debug("ARM performance counters do not support "
461 * We don't assign an index until we actually place the event onto
462 * hardware. Use -1 to signify that we haven't decided where to put it
463 * yet. For SMP systems, each core has it's own PMU so we can't do any
464 * clever allocation or constraints checking at this point.
469 * Store the event encoding into the config_base field. config and
470 * event_base are unused as the only 2 things we need to know are
471 * the event mapping and the counter to use. The counter to use is
472 * also the indx and the config_base is the event type.
474 hwc->config_base = (unsigned long)mapping;
478 if (!hwc->sample_period) {
479 hwc->sample_period = armpmu->max_period;
480 hwc->last_period = hwc->sample_period;
481 local64_set(&hwc->period_left, hwc->sample_period);
485 if (event->group_leader != event) {
486 err = validate_group(event);
495 hw_perf_event_init(struct perf_event *event)
500 return ERR_PTR(-ENODEV);
502 event->destroy = hw_perf_event_destroy;
504 if (!atomic_inc_not_zero(&active_events)) {
505 if (atomic_read(&active_events) > perf_max_events) {
506 atomic_dec(&active_events);
507 return ERR_PTR(-ENOSPC);
510 mutex_lock(&pmu_reserve_mutex);
511 if (atomic_read(&active_events) == 0) {
512 err = armpmu_reserve_hardware();
516 atomic_inc(&active_events);
517 mutex_unlock(&pmu_reserve_mutex);
523 err = __hw_perf_event_init(event);
525 hw_perf_event_destroy(event);
527 return err ? ERR_PTR(err) : &pmu;
533 /* Enable all of the perf events on hardware. */
535 struct cpu_hw_events *cpuc = &__get_cpu_var(cpu_hw_events);
540 for (idx = 0; idx <= armpmu->num_events; ++idx) {
541 struct perf_event *event = cpuc->events[idx];
546 armpmu->enable(&event->hw, idx);
553 hw_perf_disable(void)
560 * ARMv6 Performance counter handling code.
562 * ARMv6 has 2 configurable performance counters and a single cycle counter.
563 * They all share a single reset bit but can be written to zero so we can use
566 * The counters can't be individually enabled or disabled so when we remove
567 * one event and replace it with another we could get spurious counts from the
568 * wrong event. However, we can take advantage of the fact that the
569 * performance counters can export events to the event bus, and the event bus
570 * itself can be monitored. This requires that we *don't* export the events to
571 * the event bus. The procedure for disabling a configurable counter is:
572 * - change the counter to count the ETMEXTOUT[0] signal (0x20). This
573 * effectively stops the counter from counting.
574 * - disable the counter's interrupt generation (each counter has it's
575 * own interrupt enable bit).
576 * Once stopped, the counter value can be written as 0 to reset.
578 * To enable a counter:
579 * - enable the counter's interrupt generation.
580 * - set the new event type.
582 * Note: the dedicated cycle counter only counts cycles and can't be
583 * enabled/disabled independently of the others. When we want to disable the
584 * cycle counter, we have to just disable the interrupt reporting and start
585 * ignoring that counter. When re-enabling, we have to reset the value and
586 * enable the interrupt.
589 enum armv6_perf_types {
590 ARMV6_PERFCTR_ICACHE_MISS = 0x0,
591 ARMV6_PERFCTR_IBUF_STALL = 0x1,
592 ARMV6_PERFCTR_DDEP_STALL = 0x2,
593 ARMV6_PERFCTR_ITLB_MISS = 0x3,
594 ARMV6_PERFCTR_DTLB_MISS = 0x4,
595 ARMV6_PERFCTR_BR_EXEC = 0x5,
596 ARMV6_PERFCTR_BR_MISPREDICT = 0x6,
597 ARMV6_PERFCTR_INSTR_EXEC = 0x7,
598 ARMV6_PERFCTR_DCACHE_HIT = 0x9,
599 ARMV6_PERFCTR_DCACHE_ACCESS = 0xA,
600 ARMV6_PERFCTR_DCACHE_MISS = 0xB,
601 ARMV6_PERFCTR_DCACHE_WBACK = 0xC,
602 ARMV6_PERFCTR_SW_PC_CHANGE = 0xD,
603 ARMV6_PERFCTR_MAIN_TLB_MISS = 0xF,
604 ARMV6_PERFCTR_EXPL_D_ACCESS = 0x10,
605 ARMV6_PERFCTR_LSU_FULL_STALL = 0x11,
606 ARMV6_PERFCTR_WBUF_DRAINED = 0x12,
607 ARMV6_PERFCTR_CPU_CYCLES = 0xFF,
608 ARMV6_PERFCTR_NOP = 0x20,
611 enum armv6_counters {
612 ARMV6_CYCLE_COUNTER = 1,
618 * The hardware events that we support. We do support cache operations but
619 * we have harvard caches and no way to combine instruction and data
620 * accesses/misses in hardware.
622 static const unsigned armv6_perf_map[PERF_COUNT_HW_MAX] = {
623 [PERF_COUNT_HW_CPU_CYCLES] = ARMV6_PERFCTR_CPU_CYCLES,
624 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6_PERFCTR_INSTR_EXEC,
625 [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
626 [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
627 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6_PERFCTR_BR_EXEC,
628 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6_PERFCTR_BR_MISPREDICT,
629 [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
632 static const unsigned armv6_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
633 [PERF_COUNT_HW_CACHE_OP_MAX]
634 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
637 * The performance counters don't differentiate between read
638 * and write accesses/misses so this isn't strictly correct,
639 * but it's the best we can do. Writes and reads get
643 [C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
644 [C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
647 [C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
648 [C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
651 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
652 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
657 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
658 [C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
661 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
662 [C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
665 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
666 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
671 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
672 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
675 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
676 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
679 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
680 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
685 * The ARM performance counters can count micro DTLB misses,
686 * micro ITLB misses and main TLB misses. There isn't an event
687 * for TLB misses, so use the micro misses here and if users
688 * want the main TLB misses they can use a raw counter.
691 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
692 [C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
695 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
696 [C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
699 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
700 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
705 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
706 [C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
709 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
710 [C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
713 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
714 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
719 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
720 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
723 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
724 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
727 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
728 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
733 enum armv6mpcore_perf_types {
734 ARMV6MPCORE_PERFCTR_ICACHE_MISS = 0x0,
735 ARMV6MPCORE_PERFCTR_IBUF_STALL = 0x1,
736 ARMV6MPCORE_PERFCTR_DDEP_STALL = 0x2,
737 ARMV6MPCORE_PERFCTR_ITLB_MISS = 0x3,
738 ARMV6MPCORE_PERFCTR_DTLB_MISS = 0x4,
739 ARMV6MPCORE_PERFCTR_BR_EXEC = 0x5,
740 ARMV6MPCORE_PERFCTR_BR_NOTPREDICT = 0x6,
741 ARMV6MPCORE_PERFCTR_BR_MISPREDICT = 0x7,
742 ARMV6MPCORE_PERFCTR_INSTR_EXEC = 0x8,
743 ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS = 0xA,
744 ARMV6MPCORE_PERFCTR_DCACHE_RDMISS = 0xB,
745 ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS = 0xC,
746 ARMV6MPCORE_PERFCTR_DCACHE_WRMISS = 0xD,
747 ARMV6MPCORE_PERFCTR_DCACHE_EVICTION = 0xE,
748 ARMV6MPCORE_PERFCTR_SW_PC_CHANGE = 0xF,
749 ARMV6MPCORE_PERFCTR_MAIN_TLB_MISS = 0x10,
750 ARMV6MPCORE_PERFCTR_EXPL_MEM_ACCESS = 0x11,
751 ARMV6MPCORE_PERFCTR_LSU_FULL_STALL = 0x12,
752 ARMV6MPCORE_PERFCTR_WBUF_DRAINED = 0x13,
753 ARMV6MPCORE_PERFCTR_CPU_CYCLES = 0xFF,
757 * The hardware events that we support. We do support cache operations but
758 * we have harvard caches and no way to combine instruction and data
759 * accesses/misses in hardware.
761 static const unsigned armv6mpcore_perf_map[PERF_COUNT_HW_MAX] = {
762 [PERF_COUNT_HW_CPU_CYCLES] = ARMV6MPCORE_PERFCTR_CPU_CYCLES,
763 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_INSTR_EXEC,
764 [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
765 [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
766 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_BR_EXEC,
767 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6MPCORE_PERFCTR_BR_MISPREDICT,
768 [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
771 static const unsigned armv6mpcore_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
772 [PERF_COUNT_HW_CACHE_OP_MAX]
773 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
777 ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS,
779 ARMV6MPCORE_PERFCTR_DCACHE_RDMISS,
783 ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS,
785 ARMV6MPCORE_PERFCTR_DCACHE_WRMISS,
788 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
789 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
794 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
795 [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
798 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
799 [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
802 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
803 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
808 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
809 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
812 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
813 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
816 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
817 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
822 * The ARM performance counters can count micro DTLB misses,
823 * micro ITLB misses and main TLB misses. There isn't an event
824 * for TLB misses, so use the micro misses here and if users
825 * want the main TLB misses they can use a raw counter.
828 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
829 [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
832 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
833 [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
836 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
837 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
842 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
843 [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
846 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
847 [C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
850 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
851 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
856 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
857 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
860 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
861 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
864 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
865 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
870 static inline unsigned long
871 armv6_pmcr_read(void)
874 asm volatile("mrc p15, 0, %0, c15, c12, 0" : "=r"(val));
879 armv6_pmcr_write(unsigned long val)
881 asm volatile("mcr p15, 0, %0, c15, c12, 0" : : "r"(val));
884 #define ARMV6_PMCR_ENABLE (1 << 0)
885 #define ARMV6_PMCR_CTR01_RESET (1 << 1)
886 #define ARMV6_PMCR_CCOUNT_RESET (1 << 2)
887 #define ARMV6_PMCR_CCOUNT_DIV (1 << 3)
888 #define ARMV6_PMCR_COUNT0_IEN (1 << 4)
889 #define ARMV6_PMCR_COUNT1_IEN (1 << 5)
890 #define ARMV6_PMCR_CCOUNT_IEN (1 << 6)
891 #define ARMV6_PMCR_COUNT0_OVERFLOW (1 << 8)
892 #define ARMV6_PMCR_COUNT1_OVERFLOW (1 << 9)
893 #define ARMV6_PMCR_CCOUNT_OVERFLOW (1 << 10)
894 #define ARMV6_PMCR_EVT_COUNT0_SHIFT 20
895 #define ARMV6_PMCR_EVT_COUNT0_MASK (0xFF << ARMV6_PMCR_EVT_COUNT0_SHIFT)
896 #define ARMV6_PMCR_EVT_COUNT1_SHIFT 12
897 #define ARMV6_PMCR_EVT_COUNT1_MASK (0xFF << ARMV6_PMCR_EVT_COUNT1_SHIFT)
899 #define ARMV6_PMCR_OVERFLOWED_MASK \
900 (ARMV6_PMCR_COUNT0_OVERFLOW | ARMV6_PMCR_COUNT1_OVERFLOW | \
901 ARMV6_PMCR_CCOUNT_OVERFLOW)
904 armv6_pmcr_has_overflowed(unsigned long pmcr)
906 return (pmcr & ARMV6_PMCR_OVERFLOWED_MASK);
910 armv6_pmcr_counter_has_overflowed(unsigned long pmcr,
911 enum armv6_counters counter)
915 if (ARMV6_CYCLE_COUNTER == counter)
916 ret = pmcr & ARMV6_PMCR_CCOUNT_OVERFLOW;
917 else if (ARMV6_COUNTER0 == counter)
918 ret = pmcr & ARMV6_PMCR_COUNT0_OVERFLOW;
919 else if (ARMV6_COUNTER1 == counter)
920 ret = pmcr & ARMV6_PMCR_COUNT1_OVERFLOW;
922 WARN_ONCE(1, "invalid counter number (%d)\n", counter);
928 armv6pmu_read_counter(int counter)
930 unsigned long value = 0;
932 if (ARMV6_CYCLE_COUNTER == counter)
933 asm volatile("mrc p15, 0, %0, c15, c12, 1" : "=r"(value));
934 else if (ARMV6_COUNTER0 == counter)
935 asm volatile("mrc p15, 0, %0, c15, c12, 2" : "=r"(value));
936 else if (ARMV6_COUNTER1 == counter)
937 asm volatile("mrc p15, 0, %0, c15, c12, 3" : "=r"(value));
939 WARN_ONCE(1, "invalid counter number (%d)\n", counter);
945 armv6pmu_write_counter(int counter,
948 if (ARMV6_CYCLE_COUNTER == counter)
949 asm volatile("mcr p15, 0, %0, c15, c12, 1" : : "r"(value));
950 else if (ARMV6_COUNTER0 == counter)
951 asm volatile("mcr p15, 0, %0, c15, c12, 2" : : "r"(value));
952 else if (ARMV6_COUNTER1 == counter)
953 asm volatile("mcr p15, 0, %0, c15, c12, 3" : : "r"(value));
955 WARN_ONCE(1, "invalid counter number (%d)\n", counter);
959 armv6pmu_enable_event(struct hw_perf_event *hwc,
962 unsigned long val, mask, evt, flags;
964 if (ARMV6_CYCLE_COUNTER == idx) {
966 evt = ARMV6_PMCR_CCOUNT_IEN;
967 } else if (ARMV6_COUNTER0 == idx) {
968 mask = ARMV6_PMCR_EVT_COUNT0_MASK;
969 evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT0_SHIFT) |
970 ARMV6_PMCR_COUNT0_IEN;
971 } else if (ARMV6_COUNTER1 == idx) {
972 mask = ARMV6_PMCR_EVT_COUNT1_MASK;
973 evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT1_SHIFT) |
974 ARMV6_PMCR_COUNT1_IEN;
976 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
981 * Mask out the current event and set the counter to count the event
982 * that we're interested in.
984 spin_lock_irqsave(&pmu_lock, flags);
985 val = armv6_pmcr_read();
988 armv6_pmcr_write(val);
989 spin_unlock_irqrestore(&pmu_lock, flags);
993 armv6pmu_handle_irq(int irq_num,
996 unsigned long pmcr = armv6_pmcr_read();
997 struct perf_sample_data data;
998 struct cpu_hw_events *cpuc;
999 struct pt_regs *regs;
1002 if (!armv6_pmcr_has_overflowed(pmcr))
1005 regs = get_irq_regs();
1008 * The interrupts are cleared by writing the overflow flags back to
1009 * the control register. All of the other bits don't have any effect
1010 * if they are rewritten, so write the whole value back.
1012 armv6_pmcr_write(pmcr);
1014 perf_sample_data_init(&data, 0);
1016 cpuc = &__get_cpu_var(cpu_hw_events);
1017 for (idx = 0; idx <= armpmu->num_events; ++idx) {
1018 struct perf_event *event = cpuc->events[idx];
1019 struct hw_perf_event *hwc;
1021 if (!test_bit(idx, cpuc->active_mask))
1025 * We have a single interrupt for all counters. Check that
1026 * each counter has overflowed before we process it.
1028 if (!armv6_pmcr_counter_has_overflowed(pmcr, idx))
1032 armpmu_event_update(event, hwc, idx);
1033 data.period = event->hw.last_period;
1034 if (!armpmu_event_set_period(event, hwc, idx))
1037 if (perf_event_overflow(event, 0, &data, regs))
1038 armpmu->disable(hwc, idx);
1042 * Handle the pending perf events.
1044 * Note: this call *must* be run with interrupts enabled. For
1045 * platforms that can have the PMU interrupts raised as a PMI, this
1048 perf_event_do_pending();
1054 armv6pmu_start(void)
1056 unsigned long flags, val;
1058 spin_lock_irqsave(&pmu_lock, flags);
1059 val = armv6_pmcr_read();
1060 val |= ARMV6_PMCR_ENABLE;
1061 armv6_pmcr_write(val);
1062 spin_unlock_irqrestore(&pmu_lock, flags);
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);
1078 armv6pmu_event_map(int config)
1080 int mapping = armv6_perf_map[config];
1081 if (HW_OP_UNSUPPORTED == mapping)
1082 mapping = -EOPNOTSUPP;
1087 armv6mpcore_pmu_event_map(int config)
1089 int mapping = armv6mpcore_perf_map[config];
1090 if (HW_OP_UNSUPPORTED == mapping)
1091 mapping = -EOPNOTSUPP;
1096 armv6pmu_raw_event(u64 config)
1098 return config & 0xff;
1102 armv6pmu_get_event_idx(struct cpu_hw_events *cpuc,
1103 struct hw_perf_event *event)
1105 /* Always place a cycle counter into the cycle counter. */
1106 if (ARMV6_PERFCTR_CPU_CYCLES == event->config_base) {
1107 if (test_and_set_bit(ARMV6_CYCLE_COUNTER, cpuc->used_mask))
1110 return ARMV6_CYCLE_COUNTER;
1113 * For anything other than a cycle counter, try and use
1114 * counter0 and counter1.
1116 if (!test_and_set_bit(ARMV6_COUNTER1, cpuc->used_mask)) {
1117 return ARMV6_COUNTER1;
1120 if (!test_and_set_bit(ARMV6_COUNTER0, cpuc->used_mask)) {
1121 return ARMV6_COUNTER0;
1124 /* The counters are all in use. */
1130 armv6pmu_disable_event(struct hw_perf_event *hwc,
1133 unsigned long val, mask, evt, flags;
1135 if (ARMV6_CYCLE_COUNTER == idx) {
1136 mask = ARMV6_PMCR_CCOUNT_IEN;
1138 } else if (ARMV6_COUNTER0 == idx) {
1139 mask = ARMV6_PMCR_COUNT0_IEN | ARMV6_PMCR_EVT_COUNT0_MASK;
1140 evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT0_SHIFT;
1141 } else if (ARMV6_COUNTER1 == idx) {
1142 mask = ARMV6_PMCR_COUNT1_IEN | ARMV6_PMCR_EVT_COUNT1_MASK;
1143 evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT1_SHIFT;
1145 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
1150 * Mask out the current event and set the counter to count the number
1151 * of ETM bus signal assertion cycles. The external reporting should
1152 * be disabled and so this should never increment.
1154 spin_lock_irqsave(&pmu_lock, flags);
1155 val = armv6_pmcr_read();
1158 armv6_pmcr_write(val);
1159 spin_unlock_irqrestore(&pmu_lock, flags);
1163 armv6mpcore_pmu_disable_event(struct hw_perf_event *hwc,
1166 unsigned long val, mask, flags, evt = 0;
1168 if (ARMV6_CYCLE_COUNTER == idx) {
1169 mask = ARMV6_PMCR_CCOUNT_IEN;
1170 } else if (ARMV6_COUNTER0 == idx) {
1171 mask = ARMV6_PMCR_COUNT0_IEN;
1172 } else if (ARMV6_COUNTER1 == idx) {
1173 mask = ARMV6_PMCR_COUNT1_IEN;
1175 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
1180 * Unlike UP ARMv6, we don't have a way of stopping the counters. We
1181 * simply disable the interrupt reporting.
1183 spin_lock_irqsave(&pmu_lock, flags);
1184 val = armv6_pmcr_read();
1187 armv6_pmcr_write(val);
1188 spin_unlock_irqrestore(&pmu_lock, flags);
1191 static const struct arm_pmu armv6pmu = {
1192 .id = ARM_PERF_PMU_ID_V6,
1193 .handle_irq = armv6pmu_handle_irq,
1194 .enable = armv6pmu_enable_event,
1195 .disable = armv6pmu_disable_event,
1196 .event_map = armv6pmu_event_map,
1197 .raw_event = armv6pmu_raw_event,
1198 .read_counter = armv6pmu_read_counter,
1199 .write_counter = armv6pmu_write_counter,
1200 .get_event_idx = armv6pmu_get_event_idx,
1201 .start = armv6pmu_start,
1202 .stop = armv6pmu_stop,
1204 .max_period = (1LLU << 32) - 1,
1208 * ARMv6mpcore is almost identical to single core ARMv6 with the exception
1209 * that some of the events have different enumerations and that there is no
1210 * *hack* to stop the programmable counters. To stop the counters we simply
1211 * disable the interrupt reporting and update the event. When unthrottling we
1212 * reset the period and enable the interrupt reporting.
1214 static const struct arm_pmu armv6mpcore_pmu = {
1215 .id = ARM_PERF_PMU_ID_V6MP,
1216 .handle_irq = armv6pmu_handle_irq,
1217 .enable = armv6pmu_enable_event,
1218 .disable = armv6mpcore_pmu_disable_event,
1219 .event_map = armv6mpcore_pmu_event_map,
1220 .raw_event = armv6pmu_raw_event,
1221 .read_counter = armv6pmu_read_counter,
1222 .write_counter = armv6pmu_write_counter,
1223 .get_event_idx = armv6pmu_get_event_idx,
1224 .start = armv6pmu_start,
1225 .stop = armv6pmu_stop,
1227 .max_period = (1LLU << 32) - 1,
1231 * ARMv7 Cortex-A8 and Cortex-A9 Performance Events handling code.
1233 * Copied from ARMv6 code, with the low level code inspired
1234 * by the ARMv7 Oprofile code.
1236 * Cortex-A8 has up to 4 configurable performance counters and
1237 * a single cycle counter.
1238 * Cortex-A9 has up to 31 configurable performance counters and
1239 * a single cycle counter.
1241 * All counters can be enabled/disabled and IRQ masked separately. The cycle
1242 * counter and all 4 performance counters together can be reset separately.
1245 /* Common ARMv7 event types */
1246 enum armv7_perf_types {
1247 ARMV7_PERFCTR_PMNC_SW_INCR = 0x00,
1248 ARMV7_PERFCTR_IFETCH_MISS = 0x01,
1249 ARMV7_PERFCTR_ITLB_MISS = 0x02,
1250 ARMV7_PERFCTR_DCACHE_REFILL = 0x03,
1251 ARMV7_PERFCTR_DCACHE_ACCESS = 0x04,
1252 ARMV7_PERFCTR_DTLB_REFILL = 0x05,
1253 ARMV7_PERFCTR_DREAD = 0x06,
1254 ARMV7_PERFCTR_DWRITE = 0x07,
1256 ARMV7_PERFCTR_EXC_TAKEN = 0x09,
1257 ARMV7_PERFCTR_EXC_EXECUTED = 0x0A,
1258 ARMV7_PERFCTR_CID_WRITE = 0x0B,
1259 /* ARMV7_PERFCTR_PC_WRITE is equivalent to HW_BRANCH_INSTRUCTIONS.
1261 * - all branch instructions,
1262 * - instructions that explicitly write the PC,
1263 * - exception generating instructions.
1265 ARMV7_PERFCTR_PC_WRITE = 0x0C,
1266 ARMV7_PERFCTR_PC_IMM_BRANCH = 0x0D,
1267 ARMV7_PERFCTR_UNALIGNED_ACCESS = 0x0F,
1268 ARMV7_PERFCTR_PC_BRANCH_MIS_PRED = 0x10,
1269 ARMV7_PERFCTR_CLOCK_CYCLES = 0x11,
1271 ARMV7_PERFCTR_PC_BRANCH_MIS_USED = 0x12,
1273 ARMV7_PERFCTR_CPU_CYCLES = 0xFF
1276 /* ARMv7 Cortex-A8 specific event types */
1277 enum armv7_a8_perf_types {
1278 ARMV7_PERFCTR_INSTR_EXECUTED = 0x08,
1280 ARMV7_PERFCTR_PC_PROC_RETURN = 0x0E,
1282 ARMV7_PERFCTR_WRITE_BUFFER_FULL = 0x40,
1283 ARMV7_PERFCTR_L2_STORE_MERGED = 0x41,
1284 ARMV7_PERFCTR_L2_STORE_BUFF = 0x42,
1285 ARMV7_PERFCTR_L2_ACCESS = 0x43,
1286 ARMV7_PERFCTR_L2_CACH_MISS = 0x44,
1287 ARMV7_PERFCTR_AXI_READ_CYCLES = 0x45,
1288 ARMV7_PERFCTR_AXI_WRITE_CYCLES = 0x46,
1289 ARMV7_PERFCTR_MEMORY_REPLAY = 0x47,
1290 ARMV7_PERFCTR_UNALIGNED_ACCESS_REPLAY = 0x48,
1291 ARMV7_PERFCTR_L1_DATA_MISS = 0x49,
1292 ARMV7_PERFCTR_L1_INST_MISS = 0x4A,
1293 ARMV7_PERFCTR_L1_DATA_COLORING = 0x4B,
1294 ARMV7_PERFCTR_L1_NEON_DATA = 0x4C,
1295 ARMV7_PERFCTR_L1_NEON_CACH_DATA = 0x4D,
1296 ARMV7_PERFCTR_L2_NEON = 0x4E,
1297 ARMV7_PERFCTR_L2_NEON_HIT = 0x4F,
1298 ARMV7_PERFCTR_L1_INST = 0x50,
1299 ARMV7_PERFCTR_PC_RETURN_MIS_PRED = 0x51,
1300 ARMV7_PERFCTR_PC_BRANCH_FAILED = 0x52,
1301 ARMV7_PERFCTR_PC_BRANCH_TAKEN = 0x53,
1302 ARMV7_PERFCTR_PC_BRANCH_EXECUTED = 0x54,
1303 ARMV7_PERFCTR_OP_EXECUTED = 0x55,
1304 ARMV7_PERFCTR_CYCLES_INST_STALL = 0x56,
1305 ARMV7_PERFCTR_CYCLES_INST = 0x57,
1306 ARMV7_PERFCTR_CYCLES_NEON_DATA_STALL = 0x58,
1307 ARMV7_PERFCTR_CYCLES_NEON_INST_STALL = 0x59,
1308 ARMV7_PERFCTR_NEON_CYCLES = 0x5A,
1310 ARMV7_PERFCTR_PMU0_EVENTS = 0x70,
1311 ARMV7_PERFCTR_PMU1_EVENTS = 0x71,
1312 ARMV7_PERFCTR_PMU_EVENTS = 0x72,
1315 /* ARMv7 Cortex-A9 specific event types */
1316 enum armv7_a9_perf_types {
1317 ARMV7_PERFCTR_JAVA_HW_BYTECODE_EXEC = 0x40,
1318 ARMV7_PERFCTR_JAVA_SW_BYTECODE_EXEC = 0x41,
1319 ARMV7_PERFCTR_JAZELLE_BRANCH_EXEC = 0x42,
1321 ARMV7_PERFCTR_COHERENT_LINE_MISS = 0x50,
1322 ARMV7_PERFCTR_COHERENT_LINE_HIT = 0x51,
1324 ARMV7_PERFCTR_ICACHE_DEP_STALL_CYCLES = 0x60,
1325 ARMV7_PERFCTR_DCACHE_DEP_STALL_CYCLES = 0x61,
1326 ARMV7_PERFCTR_TLB_MISS_DEP_STALL_CYCLES = 0x62,
1327 ARMV7_PERFCTR_STREX_EXECUTED_PASSED = 0x63,
1328 ARMV7_PERFCTR_STREX_EXECUTED_FAILED = 0x64,
1329 ARMV7_PERFCTR_DATA_EVICTION = 0x65,
1330 ARMV7_PERFCTR_ISSUE_STAGE_NO_INST = 0x66,
1331 ARMV7_PERFCTR_ISSUE_STAGE_EMPTY = 0x67,
1332 ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE = 0x68,
1334 ARMV7_PERFCTR_PREDICTABLE_FUNCT_RETURNS = 0x6E,
1336 ARMV7_PERFCTR_MAIN_UNIT_EXECUTED_INST = 0x70,
1337 ARMV7_PERFCTR_SECOND_UNIT_EXECUTED_INST = 0x71,
1338 ARMV7_PERFCTR_LD_ST_UNIT_EXECUTED_INST = 0x72,
1339 ARMV7_PERFCTR_FP_EXECUTED_INST = 0x73,
1340 ARMV7_PERFCTR_NEON_EXECUTED_INST = 0x74,
1342 ARMV7_PERFCTR_PLD_FULL_DEP_STALL_CYCLES = 0x80,
1343 ARMV7_PERFCTR_DATA_WR_DEP_STALL_CYCLES = 0x81,
1344 ARMV7_PERFCTR_ITLB_MISS_DEP_STALL_CYCLES = 0x82,
1345 ARMV7_PERFCTR_DTLB_MISS_DEP_STALL_CYCLES = 0x83,
1346 ARMV7_PERFCTR_MICRO_ITLB_MISS_DEP_STALL_CYCLES = 0x84,
1347 ARMV7_PERFCTR_MICRO_DTLB_MISS_DEP_STALL_CYCLES = 0x85,
1348 ARMV7_PERFCTR_DMB_DEP_STALL_CYCLES = 0x86,
1350 ARMV7_PERFCTR_INTGR_CLK_ENABLED_CYCLES = 0x8A,
1351 ARMV7_PERFCTR_DATA_ENGINE_CLK_EN_CYCLES = 0x8B,
1353 ARMV7_PERFCTR_ISB_INST = 0x90,
1354 ARMV7_PERFCTR_DSB_INST = 0x91,
1355 ARMV7_PERFCTR_DMB_INST = 0x92,
1356 ARMV7_PERFCTR_EXT_INTERRUPTS = 0x93,
1358 ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_COMPLETED = 0xA0,
1359 ARMV7_PERFCTR_PLE_CACHE_LINE_RQST_SKIPPED = 0xA1,
1360 ARMV7_PERFCTR_PLE_FIFO_FLUSH = 0xA2,
1361 ARMV7_PERFCTR_PLE_RQST_COMPLETED = 0xA3,
1362 ARMV7_PERFCTR_PLE_FIFO_OVERFLOW = 0xA4,
1363 ARMV7_PERFCTR_PLE_RQST_PROG = 0xA5
1367 * Cortex-A8 HW events mapping
1369 * The hardware events that we support. We do support cache operations but
1370 * we have harvard caches and no way to combine instruction and data
1371 * accesses/misses in hardware.
1373 static const unsigned armv7_a8_perf_map[PERF_COUNT_HW_MAX] = {
1374 [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
1375 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV7_PERFCTR_INSTR_EXECUTED,
1376 [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
1377 [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
1378 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
1379 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1380 [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
1383 static const unsigned armv7_a8_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
1384 [PERF_COUNT_HW_CACHE_OP_MAX]
1385 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
1388 * The performance counters don't differentiate between read
1389 * and write accesses/misses so this isn't strictly correct,
1390 * but it's the best we can do. Writes and reads get
1394 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
1395 [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
1398 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
1399 [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
1401 [C(OP_PREFETCH)] = {
1402 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1403 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1408 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
1409 [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
1412 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L1_INST,
1413 [C(RESULT_MISS)] = ARMV7_PERFCTR_L1_INST_MISS,
1415 [C(OP_PREFETCH)] = {
1416 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1417 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1422 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
1423 [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
1426 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_L2_ACCESS,
1427 [C(RESULT_MISS)] = ARMV7_PERFCTR_L2_CACH_MISS,
1429 [C(OP_PREFETCH)] = {
1430 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1431 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1436 * Only ITLB misses and DTLB refills are supported.
1437 * If users want the DTLB refills misses a raw counter
1441 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1442 [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
1445 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1446 [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
1448 [C(OP_PREFETCH)] = {
1449 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1450 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1455 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1456 [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
1459 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1460 [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
1462 [C(OP_PREFETCH)] = {
1463 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1464 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1469 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
1471 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1474 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
1476 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1478 [C(OP_PREFETCH)] = {
1479 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1480 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1486 * Cortex-A9 HW events mapping
1488 static const unsigned armv7_a9_perf_map[PERF_COUNT_HW_MAX] = {
1489 [PERF_COUNT_HW_CPU_CYCLES] = ARMV7_PERFCTR_CPU_CYCLES,
1490 [PERF_COUNT_HW_INSTRUCTIONS] =
1491 ARMV7_PERFCTR_INST_OUT_OF_RENAME_STAGE,
1492 [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV7_PERFCTR_COHERENT_LINE_HIT,
1493 [PERF_COUNT_HW_CACHE_MISSES] = ARMV7_PERFCTR_COHERENT_LINE_MISS,
1494 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV7_PERFCTR_PC_WRITE,
1495 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1496 [PERF_COUNT_HW_BUS_CYCLES] = ARMV7_PERFCTR_CLOCK_CYCLES,
1499 static const unsigned armv7_a9_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
1500 [PERF_COUNT_HW_CACHE_OP_MAX]
1501 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
1504 * The performance counters don't differentiate between read
1505 * and write accesses/misses so this isn't strictly correct,
1506 * but it's the best we can do. Writes and reads get
1510 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
1511 [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
1514 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_DCACHE_ACCESS,
1515 [C(RESULT_MISS)] = ARMV7_PERFCTR_DCACHE_REFILL,
1517 [C(OP_PREFETCH)] = {
1518 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1519 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1524 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1525 [C(RESULT_MISS)] = ARMV7_PERFCTR_IFETCH_MISS,
1528 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1529 [C(RESULT_MISS)] = ARMV7_PERFCTR_IFETCH_MISS,
1531 [C(OP_PREFETCH)] = {
1532 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1533 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1538 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1539 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1542 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1543 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1545 [C(OP_PREFETCH)] = {
1546 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1547 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1552 * Only ITLB misses and DTLB refills are supported.
1553 * If users want the DTLB refills misses a raw counter
1557 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1558 [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
1561 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1562 [C(RESULT_MISS)] = ARMV7_PERFCTR_DTLB_REFILL,
1564 [C(OP_PREFETCH)] = {
1565 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1566 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1571 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1572 [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
1575 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1576 [C(RESULT_MISS)] = ARMV7_PERFCTR_ITLB_MISS,
1578 [C(OP_PREFETCH)] = {
1579 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1580 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1585 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
1587 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1590 [C(RESULT_ACCESS)] = ARMV7_PERFCTR_PC_WRITE,
1592 = ARMV7_PERFCTR_PC_BRANCH_MIS_PRED,
1594 [C(OP_PREFETCH)] = {
1595 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
1596 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
1602 * Perf Events counters
1604 enum armv7_counters {
1605 ARMV7_CYCLE_COUNTER = 1, /* Cycle counter */
1606 ARMV7_COUNTER0 = 2, /* First event counter */
1610 * The cycle counter is ARMV7_CYCLE_COUNTER.
1611 * The first event counter is ARMV7_COUNTER0.
1612 * The last event counter is (ARMV7_COUNTER0 + armpmu->num_events - 1).
1614 #define ARMV7_COUNTER_LAST (ARMV7_COUNTER0 + armpmu->num_events - 1)
1617 * ARMv7 low level PMNC access
1621 * Per-CPU PMNC: config reg
1623 #define ARMV7_PMNC_E (1 << 0) /* Enable all counters */
1624 #define ARMV7_PMNC_P (1 << 1) /* Reset all counters */
1625 #define ARMV7_PMNC_C (1 << 2) /* Cycle counter reset */
1626 #define ARMV7_PMNC_D (1 << 3) /* CCNT counts every 64th cpu cycle */
1627 #define ARMV7_PMNC_X (1 << 4) /* Export to ETM */
1628 #define ARMV7_PMNC_DP (1 << 5) /* Disable CCNT if non-invasive debug*/
1629 #define ARMV7_PMNC_N_SHIFT 11 /* Number of counters supported */
1630 #define ARMV7_PMNC_N_MASK 0x1f
1631 #define ARMV7_PMNC_MASK 0x3f /* Mask for writable bits */
1634 * Available counters
1636 #define ARMV7_CNT0 0 /* First event counter */
1637 #define ARMV7_CCNT 31 /* Cycle counter */
1639 /* Perf Event to low level counters mapping */
1640 #define ARMV7_EVENT_CNT_TO_CNTx (ARMV7_COUNTER0 - ARMV7_CNT0)
1643 * CNTENS: counters enable reg
1645 #define ARMV7_CNTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1646 #define ARMV7_CNTENS_C (1 << ARMV7_CCNT)
1649 * CNTENC: counters disable reg
1651 #define ARMV7_CNTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1652 #define ARMV7_CNTENC_C (1 << ARMV7_CCNT)
1655 * INTENS: counters overflow interrupt enable reg
1657 #define ARMV7_INTENS_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1658 #define ARMV7_INTENS_C (1 << ARMV7_CCNT)
1661 * INTENC: counters overflow interrupt disable reg
1663 #define ARMV7_INTENC_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1664 #define ARMV7_INTENC_C (1 << ARMV7_CCNT)
1667 * EVTSEL: Event selection reg
1669 #define ARMV7_EVTSEL_MASK 0xff /* Mask for writable bits */
1672 * SELECT: Counter selection reg
1674 #define ARMV7_SELECT_MASK 0x1f /* Mask for writable bits */
1677 * FLAG: counters overflow flag status reg
1679 #define ARMV7_FLAG_P(idx) (1 << (idx - ARMV7_EVENT_CNT_TO_CNTx))
1680 #define ARMV7_FLAG_C (1 << ARMV7_CCNT)
1681 #define ARMV7_FLAG_MASK 0xffffffff /* Mask for writable bits */
1682 #define ARMV7_OVERFLOWED_MASK ARMV7_FLAG_MASK
1684 static inline unsigned long armv7_pmnc_read(void)
1687 asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r"(val));
1691 static inline void armv7_pmnc_write(unsigned long val)
1693 val &= ARMV7_PMNC_MASK;
1694 asm volatile("mcr p15, 0, %0, c9, c12, 0" : : "r"(val));
1697 static inline int armv7_pmnc_has_overflowed(unsigned long pmnc)
1699 return pmnc & ARMV7_OVERFLOWED_MASK;
1702 static inline int armv7_pmnc_counter_has_overflowed(unsigned long pmnc,
1703 enum armv7_counters counter)
1707 if (counter == ARMV7_CYCLE_COUNTER)
1708 ret = pmnc & ARMV7_FLAG_C;
1709 else if ((counter >= ARMV7_COUNTER0) && (counter <= ARMV7_COUNTER_LAST))
1710 ret = pmnc & ARMV7_FLAG_P(counter);
1712 pr_err("CPU%u checking wrong counter %d overflow status\n",
1713 smp_processor_id(), counter);
1718 static inline int armv7_pmnc_select_counter(unsigned int idx)
1722 if ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST)) {
1723 pr_err("CPU%u selecting wrong PMNC counter"
1724 " %d\n", smp_processor_id(), idx);
1728 val = (idx - ARMV7_EVENT_CNT_TO_CNTx) & ARMV7_SELECT_MASK;
1729 asm volatile("mcr p15, 0, %0, c9, c12, 5" : : "r" (val));
1734 static inline u32 armv7pmu_read_counter(int idx)
1736 unsigned long value = 0;
1738 if (idx == ARMV7_CYCLE_COUNTER)
1739 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (value));
1740 else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) {
1741 if (armv7_pmnc_select_counter(idx) == idx)
1742 asm volatile("mrc p15, 0, %0, c9, c13, 2"
1745 pr_err("CPU%u reading wrong counter %d\n",
1746 smp_processor_id(), idx);
1751 static inline void armv7pmu_write_counter(int idx, u32 value)
1753 if (idx == ARMV7_CYCLE_COUNTER)
1754 asm volatile("mcr p15, 0, %0, c9, c13, 0" : : "r" (value));
1755 else if ((idx >= ARMV7_COUNTER0) && (idx <= ARMV7_COUNTER_LAST)) {
1756 if (armv7_pmnc_select_counter(idx) == idx)
1757 asm volatile("mcr p15, 0, %0, c9, c13, 2"
1760 pr_err("CPU%u writing wrong counter %d\n",
1761 smp_processor_id(), idx);
1764 static inline void armv7_pmnc_write_evtsel(unsigned int idx, u32 val)
1766 if (armv7_pmnc_select_counter(idx) == idx) {
1767 val &= ARMV7_EVTSEL_MASK;
1768 asm volatile("mcr p15, 0, %0, c9, c13, 1" : : "r" (val));
1772 static inline u32 armv7_pmnc_enable_counter(unsigned int idx)
1776 if ((idx != ARMV7_CYCLE_COUNTER) &&
1777 ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
1778 pr_err("CPU%u enabling wrong PMNC counter"
1779 " %d\n", smp_processor_id(), idx);
1783 if (idx == ARMV7_CYCLE_COUNTER)
1784 val = ARMV7_CNTENS_C;
1786 val = ARMV7_CNTENS_P(idx);
1788 asm volatile("mcr p15, 0, %0, c9, c12, 1" : : "r" (val));
1793 static inline u32 armv7_pmnc_disable_counter(unsigned int idx)
1798 if ((idx != ARMV7_CYCLE_COUNTER) &&
1799 ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
1800 pr_err("CPU%u disabling wrong PMNC counter"
1801 " %d\n", smp_processor_id(), idx);
1805 if (idx == ARMV7_CYCLE_COUNTER)
1806 val = ARMV7_CNTENC_C;
1808 val = ARMV7_CNTENC_P(idx);
1810 asm volatile("mcr p15, 0, %0, c9, c12, 2" : : "r" (val));
1815 static inline u32 armv7_pmnc_enable_intens(unsigned int idx)
1819 if ((idx != ARMV7_CYCLE_COUNTER) &&
1820 ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
1821 pr_err("CPU%u enabling wrong PMNC counter"
1822 " interrupt enable %d\n", smp_processor_id(), idx);
1826 if (idx == ARMV7_CYCLE_COUNTER)
1827 val = ARMV7_INTENS_C;
1829 val = ARMV7_INTENS_P(idx);
1831 asm volatile("mcr p15, 0, %0, c9, c14, 1" : : "r" (val));
1836 static inline u32 armv7_pmnc_disable_intens(unsigned int idx)
1840 if ((idx != ARMV7_CYCLE_COUNTER) &&
1841 ((idx < ARMV7_COUNTER0) || (idx > ARMV7_COUNTER_LAST))) {
1842 pr_err("CPU%u disabling wrong PMNC counter"
1843 " interrupt enable %d\n", smp_processor_id(), idx);
1847 if (idx == ARMV7_CYCLE_COUNTER)
1848 val = ARMV7_INTENC_C;
1850 val = ARMV7_INTENC_P(idx);
1852 asm volatile("mcr p15, 0, %0, c9, c14, 2" : : "r" (val));
1857 static inline u32 armv7_pmnc_getreset_flags(void)
1862 asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
1864 /* Write to clear flags */
1865 val &= ARMV7_FLAG_MASK;
1866 asm volatile("mcr p15, 0, %0, c9, c12, 3" : : "r" (val));
1872 static void armv7_pmnc_dump_regs(void)
1877 printk(KERN_INFO "PMNC registers dump:\n");
1879 asm volatile("mrc p15, 0, %0, c9, c12, 0" : "=r" (val));
1880 printk(KERN_INFO "PMNC =0x%08x\n", val);
1882 asm volatile("mrc p15, 0, %0, c9, c12, 1" : "=r" (val));
1883 printk(KERN_INFO "CNTENS=0x%08x\n", val);
1885 asm volatile("mrc p15, 0, %0, c9, c14, 1" : "=r" (val));
1886 printk(KERN_INFO "INTENS=0x%08x\n", val);
1888 asm volatile("mrc p15, 0, %0, c9, c12, 3" : "=r" (val));
1889 printk(KERN_INFO "FLAGS =0x%08x\n", val);
1891 asm volatile("mrc p15, 0, %0, c9, c12, 5" : "=r" (val));
1892 printk(KERN_INFO "SELECT=0x%08x\n", val);
1894 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r" (val));
1895 printk(KERN_INFO "CCNT =0x%08x\n", val);
1897 for (cnt = ARMV7_COUNTER0; cnt < ARMV7_COUNTER_LAST; cnt++) {
1898 armv7_pmnc_select_counter(cnt);
1899 asm volatile("mrc p15, 0, %0, c9, c13, 2" : "=r" (val));
1900 printk(KERN_INFO "CNT[%d] count =0x%08x\n",
1901 cnt-ARMV7_EVENT_CNT_TO_CNTx, val);
1902 asm volatile("mrc p15, 0, %0, c9, c13, 1" : "=r" (val));
1903 printk(KERN_INFO "CNT[%d] evtsel=0x%08x\n",
1904 cnt-ARMV7_EVENT_CNT_TO_CNTx, val);
1909 void armv7pmu_enable_event(struct hw_perf_event *hwc, int idx)
1911 unsigned long flags;
1914 * Enable counter and interrupt, and set the counter to count
1915 * the event that we're interested in.
1917 spin_lock_irqsave(&pmu_lock, flags);
1922 armv7_pmnc_disable_counter(idx);
1925 * Set event (if destined for PMNx counters)
1926 * We don't need to set the event if it's a cycle count
1928 if (idx != ARMV7_CYCLE_COUNTER)
1929 armv7_pmnc_write_evtsel(idx, hwc->config_base);
1932 * Enable interrupt for this counter
1934 armv7_pmnc_enable_intens(idx);
1939 armv7_pmnc_enable_counter(idx);
1941 spin_unlock_irqrestore(&pmu_lock, flags);
1944 static void armv7pmu_disable_event(struct hw_perf_event *hwc, int idx)
1946 unsigned long flags;
1949 * Disable counter and interrupt
1951 spin_lock_irqsave(&pmu_lock, flags);
1956 armv7_pmnc_disable_counter(idx);
1959 * Disable interrupt for this counter
1961 armv7_pmnc_disable_intens(idx);
1963 spin_unlock_irqrestore(&pmu_lock, flags);
1966 static irqreturn_t armv7pmu_handle_irq(int irq_num, void *dev)
1969 struct perf_sample_data data;
1970 struct cpu_hw_events *cpuc;
1971 struct pt_regs *regs;
1975 * Get and reset the IRQ flags
1977 pmnc = armv7_pmnc_getreset_flags();
1980 * Did an overflow occur?
1982 if (!armv7_pmnc_has_overflowed(pmnc))
1986 * Handle the counter(s) overflow(s)
1988 regs = get_irq_regs();
1990 perf_sample_data_init(&data, 0);
1992 cpuc = &__get_cpu_var(cpu_hw_events);
1993 for (idx = 0; idx <= armpmu->num_events; ++idx) {
1994 struct perf_event *event = cpuc->events[idx];
1995 struct hw_perf_event *hwc;
1997 if (!test_bit(idx, cpuc->active_mask))
2001 * We have a single interrupt for all counters. Check that
2002 * each counter has overflowed before we process it.
2004 if (!armv7_pmnc_counter_has_overflowed(pmnc, idx))
2008 armpmu_event_update(event, hwc, idx);
2009 data.period = event->hw.last_period;
2010 if (!armpmu_event_set_period(event, hwc, idx))
2013 if (perf_event_overflow(event, 0, &data, regs))
2014 armpmu->disable(hwc, idx);
2018 * Handle the pending perf events.
2020 * Note: this call *must* be run with interrupts enabled. For
2021 * platforms that can have the PMU interrupts raised as a PMI, this
2024 perf_event_do_pending();
2029 static void armv7pmu_start(void)
2031 unsigned long flags;
2033 spin_lock_irqsave(&pmu_lock, flags);
2034 /* Enable all counters */
2035 armv7_pmnc_write(armv7_pmnc_read() | ARMV7_PMNC_E);
2036 spin_unlock_irqrestore(&pmu_lock, flags);
2039 static void armv7pmu_stop(void)
2041 unsigned long flags;
2043 spin_lock_irqsave(&pmu_lock, flags);
2044 /* Disable all counters */
2045 armv7_pmnc_write(armv7_pmnc_read() & ~ARMV7_PMNC_E);
2046 spin_unlock_irqrestore(&pmu_lock, flags);
2049 static inline int armv7_a8_pmu_event_map(int config)
2051 int mapping = armv7_a8_perf_map[config];
2052 if (HW_OP_UNSUPPORTED == mapping)
2053 mapping = -EOPNOTSUPP;
2057 static inline int armv7_a9_pmu_event_map(int config)
2059 int mapping = armv7_a9_perf_map[config];
2060 if (HW_OP_UNSUPPORTED == mapping)
2061 mapping = -EOPNOTSUPP;
2065 static u64 armv7pmu_raw_event(u64 config)
2067 return config & 0xff;
2070 static int armv7pmu_get_event_idx(struct cpu_hw_events *cpuc,
2071 struct hw_perf_event *event)
2075 /* Always place a cycle counter into the cycle counter. */
2076 if (event->config_base == ARMV7_PERFCTR_CPU_CYCLES) {
2077 if (test_and_set_bit(ARMV7_CYCLE_COUNTER, cpuc->used_mask))
2080 return ARMV7_CYCLE_COUNTER;
2083 * For anything other than a cycle counter, try and use
2084 * the events counters
2086 for (idx = ARMV7_COUNTER0; idx <= armpmu->num_events; ++idx) {
2087 if (!test_and_set_bit(idx, cpuc->used_mask))
2091 /* The counters are all in use. */
2096 static struct arm_pmu armv7pmu = {
2097 .handle_irq = armv7pmu_handle_irq,
2098 .enable = armv7pmu_enable_event,
2099 .disable = armv7pmu_disable_event,
2100 .raw_event = armv7pmu_raw_event,
2101 .read_counter = armv7pmu_read_counter,
2102 .write_counter = armv7pmu_write_counter,
2103 .get_event_idx = armv7pmu_get_event_idx,
2104 .start = armv7pmu_start,
2105 .stop = armv7pmu_stop,
2106 .max_period = (1LLU << 32) - 1,
2109 static u32 __init armv7_reset_read_pmnc(void)
2113 /* Initialize & Reset PMNC: C and P bits */
2114 armv7_pmnc_write(ARMV7_PMNC_P | ARMV7_PMNC_C);
2116 /* Read the nb of CNTx counters supported from PMNC */
2117 nb_cnt = (armv7_pmnc_read() >> ARMV7_PMNC_N_SHIFT) & ARMV7_PMNC_N_MASK;
2119 /* Add the CPU cycles counter and return */
2124 * ARMv5 [xscale] Performance counter handling code.
2126 * Based on xscale OProfile code.
2128 * There are two variants of the xscale PMU that we support:
2129 * - xscale1pmu: 2 event counters and a cycle counter
2130 * - xscale2pmu: 4 event counters and a cycle counter
2131 * The two variants share event definitions, but have different
2135 enum xscale_perf_types {
2136 XSCALE_PERFCTR_ICACHE_MISS = 0x00,
2137 XSCALE_PERFCTR_ICACHE_NO_DELIVER = 0x01,
2138 XSCALE_PERFCTR_DATA_STALL = 0x02,
2139 XSCALE_PERFCTR_ITLB_MISS = 0x03,
2140 XSCALE_PERFCTR_DTLB_MISS = 0x04,
2141 XSCALE_PERFCTR_BRANCH = 0x05,
2142 XSCALE_PERFCTR_BRANCH_MISS = 0x06,
2143 XSCALE_PERFCTR_INSTRUCTION = 0x07,
2144 XSCALE_PERFCTR_DCACHE_FULL_STALL = 0x08,
2145 XSCALE_PERFCTR_DCACHE_FULL_STALL_CONTIG = 0x09,
2146 XSCALE_PERFCTR_DCACHE_ACCESS = 0x0A,
2147 XSCALE_PERFCTR_DCACHE_MISS = 0x0B,
2148 XSCALE_PERFCTR_DCACHE_WRITE_BACK = 0x0C,
2149 XSCALE_PERFCTR_PC_CHANGED = 0x0D,
2150 XSCALE_PERFCTR_BCU_REQUEST = 0x10,
2151 XSCALE_PERFCTR_BCU_FULL = 0x11,
2152 XSCALE_PERFCTR_BCU_DRAIN = 0x12,
2153 XSCALE_PERFCTR_BCU_ECC_NO_ELOG = 0x14,
2154 XSCALE_PERFCTR_BCU_1_BIT_ERR = 0x15,
2155 XSCALE_PERFCTR_RMW = 0x16,
2156 /* XSCALE_PERFCTR_CCNT is not hardware defined */
2157 XSCALE_PERFCTR_CCNT = 0xFE,
2158 XSCALE_PERFCTR_UNUSED = 0xFF,
2161 enum xscale_counters {
2162 XSCALE_CYCLE_COUNTER = 1,
2169 static const unsigned xscale_perf_map[PERF_COUNT_HW_MAX] = {
2170 [PERF_COUNT_HW_CPU_CYCLES] = XSCALE_PERFCTR_CCNT,
2171 [PERF_COUNT_HW_INSTRUCTIONS] = XSCALE_PERFCTR_INSTRUCTION,
2172 [PERF_COUNT_HW_CACHE_REFERENCES] = HW_OP_UNSUPPORTED,
2173 [PERF_COUNT_HW_CACHE_MISSES] = HW_OP_UNSUPPORTED,
2174 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = XSCALE_PERFCTR_BRANCH,
2175 [PERF_COUNT_HW_BRANCH_MISSES] = XSCALE_PERFCTR_BRANCH_MISS,
2176 [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
2179 static const unsigned xscale_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
2180 [PERF_COUNT_HW_CACHE_OP_MAX]
2181 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
2184 [C(RESULT_ACCESS)] = XSCALE_PERFCTR_DCACHE_ACCESS,
2185 [C(RESULT_MISS)] = XSCALE_PERFCTR_DCACHE_MISS,
2188 [C(RESULT_ACCESS)] = XSCALE_PERFCTR_DCACHE_ACCESS,
2189 [C(RESULT_MISS)] = XSCALE_PERFCTR_DCACHE_MISS,
2191 [C(OP_PREFETCH)] = {
2192 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2193 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2198 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2199 [C(RESULT_MISS)] = XSCALE_PERFCTR_ICACHE_MISS,
2202 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2203 [C(RESULT_MISS)] = XSCALE_PERFCTR_ICACHE_MISS,
2205 [C(OP_PREFETCH)] = {
2206 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2207 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2212 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2213 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2216 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2217 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2219 [C(OP_PREFETCH)] = {
2220 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2221 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2226 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2227 [C(RESULT_MISS)] = XSCALE_PERFCTR_DTLB_MISS,
2230 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2231 [C(RESULT_MISS)] = XSCALE_PERFCTR_DTLB_MISS,
2233 [C(OP_PREFETCH)] = {
2234 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2235 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2240 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2241 [C(RESULT_MISS)] = XSCALE_PERFCTR_ITLB_MISS,
2244 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2245 [C(RESULT_MISS)] = XSCALE_PERFCTR_ITLB_MISS,
2247 [C(OP_PREFETCH)] = {
2248 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2249 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2254 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2255 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2258 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2259 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2261 [C(OP_PREFETCH)] = {
2262 [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
2263 [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
2268 #define XSCALE_PMU_ENABLE 0x001
2269 #define XSCALE_PMN_RESET 0x002
2270 #define XSCALE_CCNT_RESET 0x004
2271 #define XSCALE_PMU_RESET (CCNT_RESET | PMN_RESET)
2272 #define XSCALE_PMU_CNT64 0x008
2275 xscalepmu_event_map(int config)
2277 int mapping = xscale_perf_map[config];
2278 if (HW_OP_UNSUPPORTED == mapping)
2279 mapping = -EOPNOTSUPP;
2284 xscalepmu_raw_event(u64 config)
2286 return config & 0xff;
2289 #define XSCALE1_OVERFLOWED_MASK 0x700
2290 #define XSCALE1_CCOUNT_OVERFLOW 0x400
2291 #define XSCALE1_COUNT0_OVERFLOW 0x100
2292 #define XSCALE1_COUNT1_OVERFLOW 0x200
2293 #define XSCALE1_CCOUNT_INT_EN 0x040
2294 #define XSCALE1_COUNT0_INT_EN 0x010
2295 #define XSCALE1_COUNT1_INT_EN 0x020
2296 #define XSCALE1_COUNT0_EVT_SHFT 12
2297 #define XSCALE1_COUNT0_EVT_MASK (0xff << XSCALE1_COUNT0_EVT_SHFT)
2298 #define XSCALE1_COUNT1_EVT_SHFT 20
2299 #define XSCALE1_COUNT1_EVT_MASK (0xff << XSCALE1_COUNT1_EVT_SHFT)
2302 xscale1pmu_read_pmnc(void)
2305 asm volatile("mrc p14, 0, %0, c0, c0, 0" : "=r" (val));
2310 xscale1pmu_write_pmnc(u32 val)
2312 /* upper 4bits and 7, 11 are write-as-0 */
2314 asm volatile("mcr p14, 0, %0, c0, c0, 0" : : "r" (val));
2318 xscale1_pmnc_counter_has_overflowed(unsigned long pmnc,
2319 enum xscale_counters counter)
2324 case XSCALE_CYCLE_COUNTER:
2325 ret = pmnc & XSCALE1_CCOUNT_OVERFLOW;
2327 case XSCALE_COUNTER0:
2328 ret = pmnc & XSCALE1_COUNT0_OVERFLOW;
2330 case XSCALE_COUNTER1:
2331 ret = pmnc & XSCALE1_COUNT1_OVERFLOW;
2334 WARN_ONCE(1, "invalid counter number (%d)\n", counter);
2341 xscale1pmu_handle_irq(int irq_num, void *dev)
2344 struct perf_sample_data data;
2345 struct cpu_hw_events *cpuc;
2346 struct pt_regs *regs;
2350 * NOTE: there's an A stepping erratum that states if an overflow
2351 * bit already exists and another occurs, the previous
2352 * Overflow bit gets cleared. There's no workaround.
2353 * Fixed in B stepping or later.
2355 pmnc = xscale1pmu_read_pmnc();
2358 * Write the value back to clear the overflow flags. Overflow
2359 * flags remain in pmnc for use below. We also disable the PMU
2360 * while we process the interrupt.
2362 xscale1pmu_write_pmnc(pmnc & ~XSCALE_PMU_ENABLE);
2364 if (!(pmnc & XSCALE1_OVERFLOWED_MASK))
2367 regs = get_irq_regs();
2369 perf_sample_data_init(&data, 0);
2371 cpuc = &__get_cpu_var(cpu_hw_events);
2372 for (idx = 0; idx <= armpmu->num_events; ++idx) {
2373 struct perf_event *event = cpuc->events[idx];
2374 struct hw_perf_event *hwc;
2376 if (!test_bit(idx, cpuc->active_mask))
2379 if (!xscale1_pmnc_counter_has_overflowed(pmnc, idx))
2383 armpmu_event_update(event, hwc, idx);
2384 data.period = event->hw.last_period;
2385 if (!armpmu_event_set_period(event, hwc, idx))
2388 if (perf_event_overflow(event, 0, &data, regs))
2389 armpmu->disable(hwc, idx);
2392 perf_event_do_pending();
2395 * Re-enable the PMU.
2397 pmnc = xscale1pmu_read_pmnc() | XSCALE_PMU_ENABLE;
2398 xscale1pmu_write_pmnc(pmnc);
2404 xscale1pmu_enable_event(struct hw_perf_event *hwc, int idx)
2406 unsigned long val, mask, evt, flags;
2409 case XSCALE_CYCLE_COUNTER:
2411 evt = XSCALE1_CCOUNT_INT_EN;
2413 case XSCALE_COUNTER0:
2414 mask = XSCALE1_COUNT0_EVT_MASK;
2415 evt = (hwc->config_base << XSCALE1_COUNT0_EVT_SHFT) |
2416 XSCALE1_COUNT0_INT_EN;
2418 case XSCALE_COUNTER1:
2419 mask = XSCALE1_COUNT1_EVT_MASK;
2420 evt = (hwc->config_base << XSCALE1_COUNT1_EVT_SHFT) |
2421 XSCALE1_COUNT1_INT_EN;
2424 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
2428 spin_lock_irqsave(&pmu_lock, flags);
2429 val = xscale1pmu_read_pmnc();
2432 xscale1pmu_write_pmnc(val);
2433 spin_unlock_irqrestore(&pmu_lock, flags);
2437 xscale1pmu_disable_event(struct hw_perf_event *hwc, int idx)
2439 unsigned long val, mask, evt, flags;
2442 case XSCALE_CYCLE_COUNTER:
2443 mask = XSCALE1_CCOUNT_INT_EN;
2446 case XSCALE_COUNTER0:
2447 mask = XSCALE1_COUNT0_INT_EN | XSCALE1_COUNT0_EVT_MASK;
2448 evt = XSCALE_PERFCTR_UNUSED << XSCALE1_COUNT0_EVT_SHFT;
2450 case XSCALE_COUNTER1:
2451 mask = XSCALE1_COUNT1_INT_EN | XSCALE1_COUNT1_EVT_MASK;
2452 evt = XSCALE_PERFCTR_UNUSED << XSCALE1_COUNT1_EVT_SHFT;
2455 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
2459 spin_lock_irqsave(&pmu_lock, flags);
2460 val = xscale1pmu_read_pmnc();
2463 xscale1pmu_write_pmnc(val);
2464 spin_unlock_irqrestore(&pmu_lock, flags);
2468 xscale1pmu_get_event_idx(struct cpu_hw_events *cpuc,
2469 struct hw_perf_event *event)
2471 if (XSCALE_PERFCTR_CCNT == event->config_base) {
2472 if (test_and_set_bit(XSCALE_CYCLE_COUNTER, cpuc->used_mask))
2475 return XSCALE_CYCLE_COUNTER;
2477 if (!test_and_set_bit(XSCALE_COUNTER1, cpuc->used_mask)) {
2478 return XSCALE_COUNTER1;
2481 if (!test_and_set_bit(XSCALE_COUNTER0, cpuc->used_mask)) {
2482 return XSCALE_COUNTER0;
2490 xscale1pmu_start(void)
2492 unsigned long flags, val;
2494 spin_lock_irqsave(&pmu_lock, flags);
2495 val = xscale1pmu_read_pmnc();
2496 val |= XSCALE_PMU_ENABLE;
2497 xscale1pmu_write_pmnc(val);
2498 spin_unlock_irqrestore(&pmu_lock, flags);
2502 xscale1pmu_stop(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_read_counter(int counter)
2519 case XSCALE_CYCLE_COUNTER:
2520 asm volatile("mrc p14, 0, %0, c1, c0, 0" : "=r" (val));
2522 case XSCALE_COUNTER0:
2523 asm volatile("mrc p14, 0, %0, c2, c0, 0" : "=r" (val));
2525 case XSCALE_COUNTER1:
2526 asm volatile("mrc p14, 0, %0, c3, c0, 0" : "=r" (val));
2534 xscale1pmu_write_counter(int counter, u32 val)
2537 case XSCALE_CYCLE_COUNTER:
2538 asm volatile("mcr p14, 0, %0, c1, c0, 0" : : "r" (val));
2540 case XSCALE_COUNTER0:
2541 asm volatile("mcr p14, 0, %0, c2, c0, 0" : : "r" (val));
2543 case XSCALE_COUNTER1:
2544 asm volatile("mcr p14, 0, %0, c3, c0, 0" : : "r" (val));
2549 static const struct arm_pmu xscale1pmu = {
2550 .id = ARM_PERF_PMU_ID_XSCALE1,
2551 .handle_irq = xscale1pmu_handle_irq,
2552 .enable = xscale1pmu_enable_event,
2553 .disable = xscale1pmu_disable_event,
2554 .event_map = xscalepmu_event_map,
2555 .raw_event = xscalepmu_raw_event,
2556 .read_counter = xscale1pmu_read_counter,
2557 .write_counter = xscale1pmu_write_counter,
2558 .get_event_idx = xscale1pmu_get_event_idx,
2559 .start = xscale1pmu_start,
2560 .stop = xscale1pmu_stop,
2562 .max_period = (1LLU << 32) - 1,
2565 #define XSCALE2_OVERFLOWED_MASK 0x01f
2566 #define XSCALE2_CCOUNT_OVERFLOW 0x001
2567 #define XSCALE2_COUNT0_OVERFLOW 0x002
2568 #define XSCALE2_COUNT1_OVERFLOW 0x004
2569 #define XSCALE2_COUNT2_OVERFLOW 0x008
2570 #define XSCALE2_COUNT3_OVERFLOW 0x010
2571 #define XSCALE2_CCOUNT_INT_EN 0x001
2572 #define XSCALE2_COUNT0_INT_EN 0x002
2573 #define XSCALE2_COUNT1_INT_EN 0x004
2574 #define XSCALE2_COUNT2_INT_EN 0x008
2575 #define XSCALE2_COUNT3_INT_EN 0x010
2576 #define XSCALE2_COUNT0_EVT_SHFT 0
2577 #define XSCALE2_COUNT0_EVT_MASK (0xff << XSCALE2_COUNT0_EVT_SHFT)
2578 #define XSCALE2_COUNT1_EVT_SHFT 8
2579 #define XSCALE2_COUNT1_EVT_MASK (0xff << XSCALE2_COUNT1_EVT_SHFT)
2580 #define XSCALE2_COUNT2_EVT_SHFT 16
2581 #define XSCALE2_COUNT2_EVT_MASK (0xff << XSCALE2_COUNT2_EVT_SHFT)
2582 #define XSCALE2_COUNT3_EVT_SHFT 24
2583 #define XSCALE2_COUNT3_EVT_MASK (0xff << XSCALE2_COUNT3_EVT_SHFT)
2586 xscale2pmu_read_pmnc(void)
2589 asm volatile("mrc p14, 0, %0, c0, c1, 0" : "=r" (val));
2590 /* bits 1-2 and 4-23 are read-unpredictable */
2591 return val & 0xff000009;
2595 xscale2pmu_write_pmnc(u32 val)
2597 /* bits 4-23 are write-as-0, 24-31 are write ignored */
2599 asm volatile("mcr p14, 0, %0, c0, c1, 0" : : "r" (val));
2603 xscale2pmu_read_overflow_flags(void)
2606 asm volatile("mrc p14, 0, %0, c5, c1, 0" : "=r" (val));
2611 xscale2pmu_write_overflow_flags(u32 val)
2613 asm volatile("mcr p14, 0, %0, c5, c1, 0" : : "r" (val));
2617 xscale2pmu_read_event_select(void)
2620 asm volatile("mrc p14, 0, %0, c8, c1, 0" : "=r" (val));
2625 xscale2pmu_write_event_select(u32 val)
2627 asm volatile("mcr p14, 0, %0, c8, c1, 0" : : "r"(val));
2631 xscale2pmu_read_int_enable(void)
2634 asm volatile("mrc p14, 0, %0, c4, c1, 0" : "=r" (val));
2639 xscale2pmu_write_int_enable(u32 val)
2641 asm volatile("mcr p14, 0, %0, c4, c1, 0" : : "r" (val));
2645 xscale2_pmnc_counter_has_overflowed(unsigned long of_flags,
2646 enum xscale_counters counter)
2651 case XSCALE_CYCLE_COUNTER:
2652 ret = of_flags & XSCALE2_CCOUNT_OVERFLOW;
2654 case XSCALE_COUNTER0:
2655 ret = of_flags & XSCALE2_COUNT0_OVERFLOW;
2657 case XSCALE_COUNTER1:
2658 ret = of_flags & XSCALE2_COUNT1_OVERFLOW;
2660 case XSCALE_COUNTER2:
2661 ret = of_flags & XSCALE2_COUNT2_OVERFLOW;
2663 case XSCALE_COUNTER3:
2664 ret = of_flags & XSCALE2_COUNT3_OVERFLOW;
2667 WARN_ONCE(1, "invalid counter number (%d)\n", counter);
2674 xscale2pmu_handle_irq(int irq_num, void *dev)
2676 unsigned long pmnc, of_flags;
2677 struct perf_sample_data data;
2678 struct cpu_hw_events *cpuc;
2679 struct pt_regs *regs;
2682 /* Disable the PMU. */
2683 pmnc = xscale2pmu_read_pmnc();
2684 xscale2pmu_write_pmnc(pmnc & ~XSCALE_PMU_ENABLE);
2686 /* Check the overflow flag register. */
2687 of_flags = xscale2pmu_read_overflow_flags();
2688 if (!(of_flags & XSCALE2_OVERFLOWED_MASK))
2691 /* Clear the overflow bits. */
2692 xscale2pmu_write_overflow_flags(of_flags);
2694 regs = get_irq_regs();
2696 perf_sample_data_init(&data, 0);
2698 cpuc = &__get_cpu_var(cpu_hw_events);
2699 for (idx = 0; idx <= armpmu->num_events; ++idx) {
2700 struct perf_event *event = cpuc->events[idx];
2701 struct hw_perf_event *hwc;
2703 if (!test_bit(idx, cpuc->active_mask))
2706 if (!xscale2_pmnc_counter_has_overflowed(pmnc, idx))
2710 armpmu_event_update(event, hwc, idx);
2711 data.period = event->hw.last_period;
2712 if (!armpmu_event_set_period(event, hwc, idx))
2715 if (perf_event_overflow(event, 0, &data, regs))
2716 armpmu->disable(hwc, idx);
2719 perf_event_do_pending();
2722 * Re-enable the PMU.
2724 pmnc = xscale2pmu_read_pmnc() | XSCALE_PMU_ENABLE;
2725 xscale2pmu_write_pmnc(pmnc);
2731 xscale2pmu_enable_event(struct hw_perf_event *hwc, int idx)
2733 unsigned long flags, ien, evtsel;
2735 ien = xscale2pmu_read_int_enable();
2736 evtsel = xscale2pmu_read_event_select();
2739 case XSCALE_CYCLE_COUNTER:
2740 ien |= XSCALE2_CCOUNT_INT_EN;
2742 case XSCALE_COUNTER0:
2743 ien |= XSCALE2_COUNT0_INT_EN;
2744 evtsel &= ~XSCALE2_COUNT0_EVT_MASK;
2745 evtsel |= hwc->config_base << XSCALE2_COUNT0_EVT_SHFT;
2747 case XSCALE_COUNTER1:
2748 ien |= XSCALE2_COUNT1_INT_EN;
2749 evtsel &= ~XSCALE2_COUNT1_EVT_MASK;
2750 evtsel |= hwc->config_base << XSCALE2_COUNT1_EVT_SHFT;
2752 case XSCALE_COUNTER2:
2753 ien |= XSCALE2_COUNT2_INT_EN;
2754 evtsel &= ~XSCALE2_COUNT2_EVT_MASK;
2755 evtsel |= hwc->config_base << XSCALE2_COUNT2_EVT_SHFT;
2757 case XSCALE_COUNTER3:
2758 ien |= XSCALE2_COUNT3_INT_EN;
2759 evtsel &= ~XSCALE2_COUNT3_EVT_MASK;
2760 evtsel |= hwc->config_base << XSCALE2_COUNT3_EVT_SHFT;
2763 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
2767 spin_lock_irqsave(&pmu_lock, flags);
2768 xscale2pmu_write_event_select(evtsel);
2769 xscale2pmu_write_int_enable(ien);
2770 spin_unlock_irqrestore(&pmu_lock, flags);
2774 xscale2pmu_disable_event(struct hw_perf_event *hwc, int idx)
2776 unsigned long flags, ien, evtsel;
2778 ien = xscale2pmu_read_int_enable();
2779 evtsel = xscale2pmu_read_event_select();
2782 case XSCALE_CYCLE_COUNTER:
2783 ien &= ~XSCALE2_CCOUNT_INT_EN;
2785 case XSCALE_COUNTER0:
2786 ien &= ~XSCALE2_COUNT0_INT_EN;
2787 evtsel &= ~XSCALE2_COUNT0_EVT_MASK;
2788 evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT0_EVT_SHFT;
2790 case XSCALE_COUNTER1:
2791 ien &= ~XSCALE2_COUNT1_INT_EN;
2792 evtsel &= ~XSCALE2_COUNT1_EVT_MASK;
2793 evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT1_EVT_SHFT;
2795 case XSCALE_COUNTER2:
2796 ien &= ~XSCALE2_COUNT2_INT_EN;
2797 evtsel &= ~XSCALE2_COUNT2_EVT_MASK;
2798 evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT2_EVT_SHFT;
2800 case XSCALE_COUNTER3:
2801 ien &= ~XSCALE2_COUNT3_INT_EN;
2802 evtsel &= ~XSCALE2_COUNT3_EVT_MASK;
2803 evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT3_EVT_SHFT;
2806 WARN_ONCE(1, "invalid counter number (%d)\n", idx);
2810 spin_lock_irqsave(&pmu_lock, flags);
2811 xscale2pmu_write_event_select(evtsel);
2812 xscale2pmu_write_int_enable(ien);
2813 spin_unlock_irqrestore(&pmu_lock, flags);
2817 xscale2pmu_get_event_idx(struct cpu_hw_events *cpuc,
2818 struct hw_perf_event *event)
2820 int idx = xscale1pmu_get_event_idx(cpuc, event);
2824 if (!test_and_set_bit(XSCALE_COUNTER3, cpuc->used_mask))
2825 idx = XSCALE_COUNTER3;
2826 else if (!test_and_set_bit(XSCALE_COUNTER2, cpuc->used_mask))
2827 idx = XSCALE_COUNTER2;
2833 xscale2pmu_start(void)
2835 unsigned long flags, val;
2837 spin_lock_irqsave(&pmu_lock, flags);
2838 val = xscale2pmu_read_pmnc() & ~XSCALE_PMU_CNT64;
2839 val |= XSCALE_PMU_ENABLE;
2840 xscale2pmu_write_pmnc(val);
2841 spin_unlock_irqrestore(&pmu_lock, flags);
2845 xscale2pmu_stop(void)
2847 unsigned long flags, val;
2849 spin_lock_irqsave(&pmu_lock, flags);
2850 val = xscale2pmu_read_pmnc();
2851 val &= ~XSCALE_PMU_ENABLE;
2852 xscale2pmu_write_pmnc(val);
2853 spin_unlock_irqrestore(&pmu_lock, flags);
2857 xscale2pmu_read_counter(int counter)
2862 case XSCALE_CYCLE_COUNTER:
2863 asm volatile("mrc p14, 0, %0, c1, c1, 0" : "=r" (val));
2865 case XSCALE_COUNTER0:
2866 asm volatile("mrc p14, 0, %0, c0, c2, 0" : "=r" (val));
2868 case XSCALE_COUNTER1:
2869 asm volatile("mrc p14, 0, %0, c1, c2, 0" : "=r" (val));
2871 case XSCALE_COUNTER2:
2872 asm volatile("mrc p14, 0, %0, c2, c2, 0" : "=r" (val));
2874 case XSCALE_COUNTER3:
2875 asm volatile("mrc p14, 0, %0, c3, c2, 0" : "=r" (val));
2883 xscale2pmu_write_counter(int counter, u32 val)
2886 case XSCALE_CYCLE_COUNTER:
2887 asm volatile("mcr p14, 0, %0, c1, c1, 0" : : "r" (val));
2889 case XSCALE_COUNTER0:
2890 asm volatile("mcr p14, 0, %0, c0, c2, 0" : : "r" (val));
2892 case XSCALE_COUNTER1:
2893 asm volatile("mcr p14, 0, %0, c1, c2, 0" : : "r" (val));
2895 case XSCALE_COUNTER2:
2896 asm volatile("mcr p14, 0, %0, c2, c2, 0" : : "r" (val));
2898 case XSCALE_COUNTER3:
2899 asm volatile("mcr p14, 0, %0, c3, c2, 0" : : "r" (val));
2904 static const struct arm_pmu xscale2pmu = {
2905 .id = ARM_PERF_PMU_ID_XSCALE2,
2906 .handle_irq = xscale2pmu_handle_irq,
2907 .enable = xscale2pmu_enable_event,
2908 .disable = xscale2pmu_disable_event,
2909 .event_map = xscalepmu_event_map,
2910 .raw_event = xscalepmu_raw_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,
2917 .max_period = (1LLU << 32) - 1,
2921 init_hw_perf_events(void)
2923 unsigned long cpuid = read_cpuid_id();
2924 unsigned long implementor = (cpuid & 0xFF000000) >> 24;
2925 unsigned long part_number = (cpuid & 0xFFF0);
2928 if (0x41 == implementor) {
2929 switch (part_number) {
2930 case 0xB360: /* ARM1136 */
2931 case 0xB560: /* ARM1156 */
2932 case 0xB760: /* ARM1176 */
2934 memcpy(armpmu_perf_cache_map, armv6_perf_cache_map,
2935 sizeof(armv6_perf_cache_map));
2936 perf_max_events = armv6pmu.num_events;
2938 case 0xB020: /* ARM11mpcore */
2939 armpmu = &armv6mpcore_pmu;
2940 memcpy(armpmu_perf_cache_map,
2941 armv6mpcore_perf_cache_map,
2942 sizeof(armv6mpcore_perf_cache_map));
2943 perf_max_events = armv6mpcore_pmu.num_events;
2945 case 0xC080: /* Cortex-A8 */
2946 armv7pmu.id = ARM_PERF_PMU_ID_CA8;
2947 memcpy(armpmu_perf_cache_map, armv7_a8_perf_cache_map,
2948 sizeof(armv7_a8_perf_cache_map));
2949 armv7pmu.event_map = armv7_a8_pmu_event_map;
2952 /* Reset PMNC and read the nb of CNTx counters
2954 armv7pmu.num_events = armv7_reset_read_pmnc();
2955 perf_max_events = armv7pmu.num_events;
2957 case 0xC090: /* Cortex-A9 */
2958 armv7pmu.id = ARM_PERF_PMU_ID_CA9;
2959 memcpy(armpmu_perf_cache_map, armv7_a9_perf_cache_map,
2960 sizeof(armv7_a9_perf_cache_map));
2961 armv7pmu.event_map = armv7_a9_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;
2970 /* Intel CPUs [xscale]. */
2971 } else if (0x69 == implementor) {
2972 part_number = (cpuid >> 13) & 0x7;
2973 switch (part_number) {
2975 armpmu = &xscale1pmu;
2976 memcpy(armpmu_perf_cache_map, xscale_perf_cache_map,
2977 sizeof(xscale_perf_cache_map));
2978 perf_max_events = xscale1pmu.num_events;
2981 armpmu = &xscale2pmu;
2982 memcpy(armpmu_perf_cache_map, xscale_perf_cache_map,
2983 sizeof(xscale_perf_cache_map));
2984 perf_max_events = xscale2pmu.num_events;
2990 pr_info("enabled with %s PMU driver, %d counters available\n",
2991 arm_pmu_names[armpmu->id], armpmu->num_events);
2993 pr_info("no hardware support available\n");
2994 perf_max_events = -1;
2999 arch_initcall(init_hw_perf_events);
3002 * Callchain handling code.
3006 * The registers we're interested in are at the end of the variable
3007 * length saved register structure. The fp points at the end of this
3008 * structure so the address of this struct is:
3009 * (struct frame_tail *)(xxx->fp)-1
3011 * This code has been adapted from the ARM OProfile support.
3014 struct frame_tail *fp;
3017 } __attribute__((packed));
3020 * Get the return address for a single stackframe and return a pointer to the
3023 static struct frame_tail *
3024 user_backtrace(struct frame_tail *tail,
3025 struct perf_callchain_entry *entry)
3027 struct frame_tail buftail;
3029 /* Also check accessibility of one struct frame_tail beyond */
3030 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
3032 if (__copy_from_user_inatomic(&buftail, tail, sizeof(buftail)))
3035 perf_callchain_store(entry, buftail.lr);
3038 * Frame pointers should strictly progress back up the stack
3039 * (towards higher addresses).
3041 if (tail >= buftail.fp)
3044 return buftail.fp - 1;
3048 perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
3050 struct frame_tail *tail;
3053 tail = (struct frame_tail *)regs->ARM_fp - 1;
3055 while (tail && !((unsigned long)tail & 0x3))
3056 tail = user_backtrace(tail, entry);
3060 * Gets called by walk_stackframe() for every stackframe. This will be called
3061 * whist unwinding the stackframe and is like a subroutine return so we use
3065 callchain_trace(struct stackframe *fr,
3068 struct perf_callchain_entry *entry = data;
3069 perf_callchain_store(entry, fr->pc);
3074 perf_callchain_kernel(struct perf_callchain_entry *entry, struct pt_regs *regs)
3076 struct stackframe fr;
3078 fr.fp = regs->ARM_fp;
3079 fr.sp = regs->ARM_sp;
3080 fr.lr = regs->ARM_lr;
3081 fr.pc = regs->ARM_pc;
3082 walk_stackframe(&fr, callchain_trace, entry);