1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
243eab878SWill Deacon /*
343eab878SWill Deacon * ARMv6 Performance counter handling code.
443eab878SWill Deacon *
543eab878SWill Deacon * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
643eab878SWill Deacon *
743eab878SWill Deacon * ARMv6 has 2 configurable performance counters and a single cycle counter.
843eab878SWill Deacon * They all share a single reset bit but can be written to zero so we can use
943eab878SWill Deacon * that for a reset.
1043eab878SWill Deacon *
1143eab878SWill Deacon * The counters can't be individually enabled or disabled so when we remove
1243eab878SWill Deacon * one event and replace it with another we could get spurious counts from the
1343eab878SWill Deacon * wrong event. However, we can take advantage of the fact that the
1443eab878SWill Deacon * performance counters can export events to the event bus, and the event bus
1543eab878SWill Deacon * itself can be monitored. This requires that we *don't* export the events to
1643eab878SWill Deacon * the event bus. The procedure for disabling a configurable counter is:
1743eab878SWill Deacon * - change the counter to count the ETMEXTOUT[0] signal (0x20). This
1843eab878SWill Deacon * effectively stops the counter from counting.
1943eab878SWill Deacon * - disable the counter's interrupt generation (each counter has it's
2043eab878SWill Deacon * own interrupt enable bit).
2143eab878SWill Deacon * Once stopped, the counter value can be written as 0 to reset.
2243eab878SWill Deacon *
2343eab878SWill Deacon * To enable a counter:
2443eab878SWill Deacon * - enable the counter's interrupt generation.
2543eab878SWill Deacon * - set the new event type.
2643eab878SWill Deacon *
2743eab878SWill Deacon * Note: the dedicated cycle counter only counts cycles and can't be
2843eab878SWill Deacon * enabled/disabled independently of the others. When we want to disable the
2943eab878SWill Deacon * cycle counter, we have to just disable the interrupt reporting and start
3043eab878SWill Deacon * ignoring that counter. When re-enabling, we have to reset the value and
3143eab878SWill Deacon * enable the interrupt.
3243eab878SWill Deacon */
3343eab878SWill Deacon
34e399b1a4SRussell King #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K)
351fe115b3SMark Rutland
361fe115b3SMark Rutland #include <asm/cputype.h>
371fe115b3SMark Rutland #include <asm/irq_regs.h>
381fe115b3SMark Rutland
391fe115b3SMark Rutland #include <linux/of.h>
40fa8ad788SMark Rutland #include <linux/perf/arm_pmu.h>
411fe115b3SMark Rutland #include <linux/platform_device.h>
421fe115b3SMark Rutland
4343eab878SWill Deacon enum armv6_perf_types {
4443eab878SWill Deacon ARMV6_PERFCTR_ICACHE_MISS = 0x0,
4543eab878SWill Deacon ARMV6_PERFCTR_IBUF_STALL = 0x1,
4643eab878SWill Deacon ARMV6_PERFCTR_DDEP_STALL = 0x2,
4743eab878SWill Deacon ARMV6_PERFCTR_ITLB_MISS = 0x3,
4843eab878SWill Deacon ARMV6_PERFCTR_DTLB_MISS = 0x4,
4943eab878SWill Deacon ARMV6_PERFCTR_BR_EXEC = 0x5,
5043eab878SWill Deacon ARMV6_PERFCTR_BR_MISPREDICT = 0x6,
5143eab878SWill Deacon ARMV6_PERFCTR_INSTR_EXEC = 0x7,
5243eab878SWill Deacon ARMV6_PERFCTR_DCACHE_HIT = 0x9,
5343eab878SWill Deacon ARMV6_PERFCTR_DCACHE_ACCESS = 0xA,
5443eab878SWill Deacon ARMV6_PERFCTR_DCACHE_MISS = 0xB,
5543eab878SWill Deacon ARMV6_PERFCTR_DCACHE_WBACK = 0xC,
5643eab878SWill Deacon ARMV6_PERFCTR_SW_PC_CHANGE = 0xD,
5743eab878SWill Deacon ARMV6_PERFCTR_MAIN_TLB_MISS = 0xF,
5843eab878SWill Deacon ARMV6_PERFCTR_EXPL_D_ACCESS = 0x10,
5943eab878SWill Deacon ARMV6_PERFCTR_LSU_FULL_STALL = 0x11,
6043eab878SWill Deacon ARMV6_PERFCTR_WBUF_DRAINED = 0x12,
6143eab878SWill Deacon ARMV6_PERFCTR_CPU_CYCLES = 0xFF,
6243eab878SWill Deacon ARMV6_PERFCTR_NOP = 0x20,
6343eab878SWill Deacon };
6443eab878SWill Deacon
6543eab878SWill Deacon enum armv6_counters {
66d2b41f74SWill Deacon ARMV6_CYCLE_COUNTER = 0,
6743eab878SWill Deacon ARMV6_COUNTER0,
6843eab878SWill Deacon ARMV6_COUNTER1,
6943eab878SWill Deacon };
7043eab878SWill Deacon
7143eab878SWill Deacon /*
7243eab878SWill Deacon * The hardware events that we support. We do support cache operations but
7343eab878SWill Deacon * we have harvard caches and no way to combine instruction and data
7443eab878SWill Deacon * accesses/misses in hardware.
7543eab878SWill Deacon */
7643eab878SWill Deacon static const unsigned armv6_perf_map[PERF_COUNT_HW_MAX] = {
77cf20ae8cSMark Rutland PERF_MAP_ALL_UNSUPPORTED,
7843eab878SWill Deacon [PERF_COUNT_HW_CPU_CYCLES] = ARMV6_PERFCTR_CPU_CYCLES,
7943eab878SWill Deacon [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6_PERFCTR_INSTR_EXEC,
8043eab878SWill Deacon [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6_PERFCTR_BR_EXEC,
8143eab878SWill Deacon [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6_PERFCTR_BR_MISPREDICT,
820445e7a5SWill Deacon [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV6_PERFCTR_IBUF_STALL,
830445e7a5SWill Deacon [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV6_PERFCTR_LSU_FULL_STALL,
8443eab878SWill Deacon };
8543eab878SWill Deacon
8643eab878SWill Deacon static const unsigned armv6_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
8743eab878SWill Deacon [PERF_COUNT_HW_CACHE_OP_MAX]
8843eab878SWill Deacon [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
89cf20ae8cSMark Rutland PERF_CACHE_MAP_ALL_UNSUPPORTED,
90cf20ae8cSMark Rutland
9143eab878SWill Deacon /*
92cf20ae8cSMark Rutland * The performance counters don't differentiate between read and write
93cf20ae8cSMark Rutland * accesses/misses so this isn't strictly correct, but it's the best we
94cf20ae8cSMark Rutland * can do. Writes and reads get combined.
9543eab878SWill Deacon */
96cf20ae8cSMark Rutland [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
97cf20ae8cSMark Rutland [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
98cf20ae8cSMark Rutland [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
99cf20ae8cSMark Rutland [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
100cf20ae8cSMark Rutland
101cf20ae8cSMark Rutland [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
102cf20ae8cSMark Rutland
10343eab878SWill Deacon /*
104cf20ae8cSMark Rutland * The ARM performance counters can count micro DTLB misses, micro ITLB
105cf20ae8cSMark Rutland * misses and main TLB misses. There isn't an event for TLB misses, so
106cf20ae8cSMark Rutland * use the micro misses here and if users want the main TLB misses they
107cf20ae8cSMark Rutland * can use a raw counter.
10843eab878SWill Deacon */
109cf20ae8cSMark Rutland [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
110cf20ae8cSMark Rutland [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
111cf20ae8cSMark Rutland
112cf20ae8cSMark Rutland [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
113cf20ae8cSMark Rutland [C(ITLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
11443eab878SWill Deacon };
11543eab878SWill Deacon
11643eab878SWill Deacon enum armv6mpcore_perf_types {
11743eab878SWill Deacon ARMV6MPCORE_PERFCTR_ICACHE_MISS = 0x0,
11843eab878SWill Deacon ARMV6MPCORE_PERFCTR_IBUF_STALL = 0x1,
11943eab878SWill Deacon ARMV6MPCORE_PERFCTR_DDEP_STALL = 0x2,
12043eab878SWill Deacon ARMV6MPCORE_PERFCTR_ITLB_MISS = 0x3,
12143eab878SWill Deacon ARMV6MPCORE_PERFCTR_DTLB_MISS = 0x4,
12243eab878SWill Deacon ARMV6MPCORE_PERFCTR_BR_EXEC = 0x5,
12343eab878SWill Deacon ARMV6MPCORE_PERFCTR_BR_NOTPREDICT = 0x6,
12443eab878SWill Deacon ARMV6MPCORE_PERFCTR_BR_MISPREDICT = 0x7,
12543eab878SWill Deacon ARMV6MPCORE_PERFCTR_INSTR_EXEC = 0x8,
12643eab878SWill Deacon ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS = 0xA,
12743eab878SWill Deacon ARMV6MPCORE_PERFCTR_DCACHE_RDMISS = 0xB,
12843eab878SWill Deacon ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS = 0xC,
12943eab878SWill Deacon ARMV6MPCORE_PERFCTR_DCACHE_WRMISS = 0xD,
13043eab878SWill Deacon ARMV6MPCORE_PERFCTR_DCACHE_EVICTION = 0xE,
13143eab878SWill Deacon ARMV6MPCORE_PERFCTR_SW_PC_CHANGE = 0xF,
13243eab878SWill Deacon ARMV6MPCORE_PERFCTR_MAIN_TLB_MISS = 0x10,
13343eab878SWill Deacon ARMV6MPCORE_PERFCTR_EXPL_MEM_ACCESS = 0x11,
13443eab878SWill Deacon ARMV6MPCORE_PERFCTR_LSU_FULL_STALL = 0x12,
13543eab878SWill Deacon ARMV6MPCORE_PERFCTR_WBUF_DRAINED = 0x13,
13643eab878SWill Deacon ARMV6MPCORE_PERFCTR_CPU_CYCLES = 0xFF,
13743eab878SWill Deacon };
13843eab878SWill Deacon
13943eab878SWill Deacon /*
14043eab878SWill Deacon * The hardware events that we support. We do support cache operations but
14143eab878SWill Deacon * we have harvard caches and no way to combine instruction and data
14243eab878SWill Deacon * accesses/misses in hardware.
14343eab878SWill Deacon */
14443eab878SWill Deacon static const unsigned armv6mpcore_perf_map[PERF_COUNT_HW_MAX] = {
145cf20ae8cSMark Rutland PERF_MAP_ALL_UNSUPPORTED,
14643eab878SWill Deacon [PERF_COUNT_HW_CPU_CYCLES] = ARMV6MPCORE_PERFCTR_CPU_CYCLES,
14743eab878SWill Deacon [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_INSTR_EXEC,
14843eab878SWill Deacon [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_BR_EXEC,
14943eab878SWill Deacon [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6MPCORE_PERFCTR_BR_MISPREDICT,
1500445e7a5SWill Deacon [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV6MPCORE_PERFCTR_IBUF_STALL,
1510445e7a5SWill Deacon [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV6MPCORE_PERFCTR_LSU_FULL_STALL,
15243eab878SWill Deacon };
15343eab878SWill Deacon
15443eab878SWill Deacon static const unsigned armv6mpcore_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
15543eab878SWill Deacon [PERF_COUNT_HW_CACHE_OP_MAX]
15643eab878SWill Deacon [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
157cf20ae8cSMark Rutland PERF_CACHE_MAP_ALL_UNSUPPORTED,
158cf20ae8cSMark Rutland
159cf20ae8cSMark Rutland [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS,
160cf20ae8cSMark Rutland [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DCACHE_RDMISS,
161cf20ae8cSMark Rutland [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS,
162cf20ae8cSMark Rutland [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DCACHE_WRMISS,
163cf20ae8cSMark Rutland
164cf20ae8cSMark Rutland [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
165cf20ae8cSMark Rutland
16643eab878SWill Deacon /*
167cf20ae8cSMark Rutland * The ARM performance counters can count micro DTLB misses, micro ITLB
168cf20ae8cSMark Rutland * misses and main TLB misses. There isn't an event for TLB misses, so
169cf20ae8cSMark Rutland * use the micro misses here and if users want the main TLB misses they
170cf20ae8cSMark Rutland * can use a raw counter.
17143eab878SWill Deacon */
172cf20ae8cSMark Rutland [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
173cf20ae8cSMark Rutland [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
174cf20ae8cSMark Rutland
175cf20ae8cSMark Rutland [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
176cf20ae8cSMark Rutland [C(ITLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
17743eab878SWill Deacon };
17843eab878SWill Deacon
17943eab878SWill Deacon static inline unsigned long
armv6_pmcr_read(void)18043eab878SWill Deacon armv6_pmcr_read(void)
18143eab878SWill Deacon {
18243eab878SWill Deacon u32 val;
18343eab878SWill Deacon asm volatile("mrc p15, 0, %0, c15, c12, 0" : "=r"(val));
18443eab878SWill Deacon return val;
18543eab878SWill Deacon }
18643eab878SWill Deacon
18743eab878SWill Deacon static inline void
armv6_pmcr_write(unsigned long val)18843eab878SWill Deacon armv6_pmcr_write(unsigned long val)
18943eab878SWill Deacon {
19043eab878SWill Deacon asm volatile("mcr p15, 0, %0, c15, c12, 0" : : "r"(val));
19143eab878SWill Deacon }
19243eab878SWill Deacon
19343eab878SWill Deacon #define ARMV6_PMCR_ENABLE (1 << 0)
19443eab878SWill Deacon #define ARMV6_PMCR_CTR01_RESET (1 << 1)
19543eab878SWill Deacon #define ARMV6_PMCR_CCOUNT_RESET (1 << 2)
19643eab878SWill Deacon #define ARMV6_PMCR_CCOUNT_DIV (1 << 3)
19743eab878SWill Deacon #define ARMV6_PMCR_COUNT0_IEN (1 << 4)
19843eab878SWill Deacon #define ARMV6_PMCR_COUNT1_IEN (1 << 5)
19943eab878SWill Deacon #define ARMV6_PMCR_CCOUNT_IEN (1 << 6)
20043eab878SWill Deacon #define ARMV6_PMCR_COUNT0_OVERFLOW (1 << 8)
20143eab878SWill Deacon #define ARMV6_PMCR_COUNT1_OVERFLOW (1 << 9)
20243eab878SWill Deacon #define ARMV6_PMCR_CCOUNT_OVERFLOW (1 << 10)
20343eab878SWill Deacon #define ARMV6_PMCR_EVT_COUNT0_SHIFT 20
20443eab878SWill Deacon #define ARMV6_PMCR_EVT_COUNT0_MASK (0xFF << ARMV6_PMCR_EVT_COUNT0_SHIFT)
20543eab878SWill Deacon #define ARMV6_PMCR_EVT_COUNT1_SHIFT 12
20643eab878SWill Deacon #define ARMV6_PMCR_EVT_COUNT1_MASK (0xFF << ARMV6_PMCR_EVT_COUNT1_SHIFT)
20743eab878SWill Deacon
20843eab878SWill Deacon #define ARMV6_PMCR_OVERFLOWED_MASK \
20943eab878SWill Deacon (ARMV6_PMCR_COUNT0_OVERFLOW | ARMV6_PMCR_COUNT1_OVERFLOW | \
21043eab878SWill Deacon ARMV6_PMCR_CCOUNT_OVERFLOW)
21143eab878SWill Deacon
21243eab878SWill Deacon static inline int
armv6_pmcr_has_overflowed(unsigned long pmcr)21343eab878SWill Deacon armv6_pmcr_has_overflowed(unsigned long pmcr)
21443eab878SWill Deacon {
21543eab878SWill Deacon return pmcr & ARMV6_PMCR_OVERFLOWED_MASK;
21643eab878SWill Deacon }
21743eab878SWill Deacon
21843eab878SWill Deacon static inline int
armv6_pmcr_counter_has_overflowed(unsigned long pmcr,enum armv6_counters counter)21943eab878SWill Deacon armv6_pmcr_counter_has_overflowed(unsigned long pmcr,
22043eab878SWill Deacon enum armv6_counters counter)
22143eab878SWill Deacon {
22243eab878SWill Deacon int ret = 0;
22343eab878SWill Deacon
22443eab878SWill Deacon if (ARMV6_CYCLE_COUNTER == counter)
22543eab878SWill Deacon ret = pmcr & ARMV6_PMCR_CCOUNT_OVERFLOW;
22643eab878SWill Deacon else if (ARMV6_COUNTER0 == counter)
22743eab878SWill Deacon ret = pmcr & ARMV6_PMCR_COUNT0_OVERFLOW;
22843eab878SWill Deacon else if (ARMV6_COUNTER1 == counter)
22943eab878SWill Deacon ret = pmcr & ARMV6_PMCR_COUNT1_OVERFLOW;
23043eab878SWill Deacon else
23143eab878SWill Deacon WARN_ONCE(1, "invalid counter number (%d)\n", counter);
23243eab878SWill Deacon
23343eab878SWill Deacon return ret;
23443eab878SWill Deacon }
23543eab878SWill Deacon
armv6pmu_read_counter(struct perf_event * event)2363a95200dSSuzuki K Poulose static inline u64 armv6pmu_read_counter(struct perf_event *event)
23743eab878SWill Deacon {
238ed6f2a52SSudeep KarkadaNagesha struct hw_perf_event *hwc = &event->hw;
239ed6f2a52SSudeep KarkadaNagesha int counter = hwc->idx;
24043eab878SWill Deacon unsigned long value = 0;
24143eab878SWill Deacon
24243eab878SWill Deacon if (ARMV6_CYCLE_COUNTER == counter)
24343eab878SWill Deacon asm volatile("mrc p15, 0, %0, c15, c12, 1" : "=r"(value));
24443eab878SWill Deacon else if (ARMV6_COUNTER0 == counter)
24543eab878SWill Deacon asm volatile("mrc p15, 0, %0, c15, c12, 2" : "=r"(value));
24643eab878SWill Deacon else if (ARMV6_COUNTER1 == counter)
24743eab878SWill Deacon asm volatile("mrc p15, 0, %0, c15, c12, 3" : "=r"(value));
24843eab878SWill Deacon else
24943eab878SWill Deacon WARN_ONCE(1, "invalid counter number (%d)\n", counter);
25043eab878SWill Deacon
25143eab878SWill Deacon return value;
25243eab878SWill Deacon }
25343eab878SWill Deacon
armv6pmu_write_counter(struct perf_event * event,u64 value)2543a95200dSSuzuki K Poulose static inline void armv6pmu_write_counter(struct perf_event *event, u64 value)
25543eab878SWill Deacon {
256ed6f2a52SSudeep KarkadaNagesha struct hw_perf_event *hwc = &event->hw;
257ed6f2a52SSudeep KarkadaNagesha int counter = hwc->idx;
258ed6f2a52SSudeep KarkadaNagesha
25943eab878SWill Deacon if (ARMV6_CYCLE_COUNTER == counter)
26043eab878SWill Deacon asm volatile("mcr p15, 0, %0, c15, c12, 1" : : "r"(value));
26143eab878SWill Deacon else if (ARMV6_COUNTER0 == counter)
26243eab878SWill Deacon asm volatile("mcr p15, 0, %0, c15, c12, 2" : : "r"(value));
26343eab878SWill Deacon else if (ARMV6_COUNTER1 == counter)
26443eab878SWill Deacon asm volatile("mcr p15, 0, %0, c15, c12, 3" : : "r"(value));
26543eab878SWill Deacon else
26643eab878SWill Deacon WARN_ONCE(1, "invalid counter number (%d)\n", counter);
26743eab878SWill Deacon }
26843eab878SWill Deacon
armv6pmu_enable_event(struct perf_event * event)269ed6f2a52SSudeep KarkadaNagesha static void armv6pmu_enable_event(struct perf_event *event)
27043eab878SWill Deacon {
27143eab878SWill Deacon unsigned long val, mask, evt, flags;
272ed6f2a52SSudeep KarkadaNagesha struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
273ed6f2a52SSudeep KarkadaNagesha struct hw_perf_event *hwc = &event->hw;
27411679250SMark Rutland struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
275ed6f2a52SSudeep KarkadaNagesha int idx = hwc->idx;
27643eab878SWill Deacon
27743eab878SWill Deacon if (ARMV6_CYCLE_COUNTER == idx) {
27843eab878SWill Deacon mask = 0;
27943eab878SWill Deacon evt = ARMV6_PMCR_CCOUNT_IEN;
28043eab878SWill Deacon } else if (ARMV6_COUNTER0 == idx) {
28143eab878SWill Deacon mask = ARMV6_PMCR_EVT_COUNT0_MASK;
28243eab878SWill Deacon evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT0_SHIFT) |
28343eab878SWill Deacon ARMV6_PMCR_COUNT0_IEN;
28443eab878SWill Deacon } else if (ARMV6_COUNTER1 == idx) {
28543eab878SWill Deacon mask = ARMV6_PMCR_EVT_COUNT1_MASK;
28643eab878SWill Deacon evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT1_SHIFT) |
28743eab878SWill Deacon ARMV6_PMCR_COUNT1_IEN;
28843eab878SWill Deacon } else {
28943eab878SWill Deacon WARN_ONCE(1, "invalid counter number (%d)\n", idx);
29043eab878SWill Deacon return;
29143eab878SWill Deacon }
29243eab878SWill Deacon
29343eab878SWill Deacon /*
29443eab878SWill Deacon * Mask out the current event and set the counter to count the event
29543eab878SWill Deacon * that we're interested in.
29643eab878SWill Deacon */
2970f78d2d5SMark Rutland raw_spin_lock_irqsave(&events->pmu_lock, flags);
29843eab878SWill Deacon val = armv6_pmcr_read();
29943eab878SWill Deacon val &= ~mask;
30043eab878SWill Deacon val |= evt;
30143eab878SWill Deacon armv6_pmcr_write(val);
3020f78d2d5SMark Rutland raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
30343eab878SWill Deacon }
30443eab878SWill Deacon
30543eab878SWill Deacon static irqreturn_t
armv6pmu_handle_irq(struct arm_pmu * cpu_pmu)3060788f1e9SMark Rutland armv6pmu_handle_irq(struct arm_pmu *cpu_pmu)
30743eab878SWill Deacon {
30843eab878SWill Deacon unsigned long pmcr = armv6_pmcr_read();
30943eab878SWill Deacon struct perf_sample_data data;
31011679250SMark Rutland struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
31143eab878SWill Deacon struct pt_regs *regs;
31243eab878SWill Deacon int idx;
31343eab878SWill Deacon
31443eab878SWill Deacon if (!armv6_pmcr_has_overflowed(pmcr))
31543eab878SWill Deacon return IRQ_NONE;
31643eab878SWill Deacon
31743eab878SWill Deacon regs = get_irq_regs();
31843eab878SWill Deacon
31943eab878SWill Deacon /*
32043eab878SWill Deacon * The interrupts are cleared by writing the overflow flags back to
32143eab878SWill Deacon * the control register. All of the other bits don't have any effect
32243eab878SWill Deacon * if they are rewritten, so write the whole value back.
32343eab878SWill Deacon */
32443eab878SWill Deacon armv6_pmcr_write(pmcr);
32543eab878SWill Deacon
3268be3f9a2SMark Rutland for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
32743eab878SWill Deacon struct perf_event *event = cpuc->events[idx];
32843eab878SWill Deacon struct hw_perf_event *hwc;
32943eab878SWill Deacon
330f6f5a30cSWill Deacon /* Ignore if we don't have an event. */
331f6f5a30cSWill Deacon if (!event)
33243eab878SWill Deacon continue;
33343eab878SWill Deacon
33443eab878SWill Deacon /*
33543eab878SWill Deacon * We have a single interrupt for all counters. Check that
33643eab878SWill Deacon * each counter has overflowed before we process it.
33743eab878SWill Deacon */
33843eab878SWill Deacon if (!armv6_pmcr_counter_has_overflowed(pmcr, idx))
33943eab878SWill Deacon continue;
34043eab878SWill Deacon
34143eab878SWill Deacon hwc = &event->hw;
342ed6f2a52SSudeep KarkadaNagesha armpmu_event_update(event);
343fd0d000bSRobert Richter perf_sample_data_init(&data, 0, hwc->last_period);
344ed6f2a52SSudeep KarkadaNagesha if (!armpmu_event_set_period(event))
34543eab878SWill Deacon continue;
34643eab878SWill Deacon
347a8b0ca17SPeter Zijlstra if (perf_event_overflow(event, &data, regs))
348ed6f2a52SSudeep KarkadaNagesha cpu_pmu->disable(event);
34943eab878SWill Deacon }
35043eab878SWill Deacon
35143eab878SWill Deacon /*
35243eab878SWill Deacon * Handle the pending perf events.
35343eab878SWill Deacon *
35443eab878SWill Deacon * Note: this call *must* be run with interrupts disabled. For
35543eab878SWill Deacon * platforms that can have the PMU interrupts raised as an NMI, this
35643eab878SWill Deacon * will not work.
35743eab878SWill Deacon */
35843eab878SWill Deacon irq_work_run();
35943eab878SWill Deacon
36043eab878SWill Deacon return IRQ_HANDLED;
36143eab878SWill Deacon }
36243eab878SWill Deacon
armv6pmu_start(struct arm_pmu * cpu_pmu)363ed6f2a52SSudeep KarkadaNagesha static void armv6pmu_start(struct arm_pmu *cpu_pmu)
36443eab878SWill Deacon {
36543eab878SWill Deacon unsigned long flags, val;
36611679250SMark Rutland struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
36743eab878SWill Deacon
3680f78d2d5SMark Rutland raw_spin_lock_irqsave(&events->pmu_lock, flags);
36943eab878SWill Deacon val = armv6_pmcr_read();
37043eab878SWill Deacon val |= ARMV6_PMCR_ENABLE;
37143eab878SWill Deacon armv6_pmcr_write(val);
3720f78d2d5SMark Rutland raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
37343eab878SWill Deacon }
37443eab878SWill Deacon
armv6pmu_stop(struct arm_pmu * cpu_pmu)375ed6f2a52SSudeep KarkadaNagesha static void armv6pmu_stop(struct arm_pmu *cpu_pmu)
37643eab878SWill Deacon {
37743eab878SWill Deacon unsigned long flags, val;
37811679250SMark Rutland struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
37943eab878SWill Deacon
3800f78d2d5SMark Rutland raw_spin_lock_irqsave(&events->pmu_lock, flags);
38143eab878SWill Deacon val = armv6_pmcr_read();
38243eab878SWill Deacon val &= ~ARMV6_PMCR_ENABLE;
38343eab878SWill Deacon armv6_pmcr_write(val);
3840f78d2d5SMark Rutland raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
38543eab878SWill Deacon }
38643eab878SWill Deacon
38743eab878SWill Deacon static int
armv6pmu_get_event_idx(struct pmu_hw_events * cpuc,struct perf_event * event)3888be3f9a2SMark Rutland armv6pmu_get_event_idx(struct pmu_hw_events *cpuc,
389ed6f2a52SSudeep KarkadaNagesha struct perf_event *event)
39043eab878SWill Deacon {
391ed6f2a52SSudeep KarkadaNagesha struct hw_perf_event *hwc = &event->hw;
39243eab878SWill Deacon /* Always place a cycle counter into the cycle counter. */
393ed6f2a52SSudeep KarkadaNagesha if (ARMV6_PERFCTR_CPU_CYCLES == hwc->config_base) {
39443eab878SWill Deacon if (test_and_set_bit(ARMV6_CYCLE_COUNTER, cpuc->used_mask))
39543eab878SWill Deacon return -EAGAIN;
39643eab878SWill Deacon
39743eab878SWill Deacon return ARMV6_CYCLE_COUNTER;
39843eab878SWill Deacon } else {
39943eab878SWill Deacon /*
40043eab878SWill Deacon * For anything other than a cycle counter, try and use
40143eab878SWill Deacon * counter0 and counter1.
40243eab878SWill Deacon */
40343eab878SWill Deacon if (!test_and_set_bit(ARMV6_COUNTER1, cpuc->used_mask))
40443eab878SWill Deacon return ARMV6_COUNTER1;
40543eab878SWill Deacon
40643eab878SWill Deacon if (!test_and_set_bit(ARMV6_COUNTER0, cpuc->used_mask))
40743eab878SWill Deacon return ARMV6_COUNTER0;
40843eab878SWill Deacon
40943eab878SWill Deacon /* The counters are all in use. */
41043eab878SWill Deacon return -EAGAIN;
41143eab878SWill Deacon }
41243eab878SWill Deacon }
41343eab878SWill Deacon
armv6pmu_clear_event_idx(struct pmu_hw_events * cpuc,struct perf_event * event)414*7dfc8db1SSuzuki K Poulose static void armv6pmu_clear_event_idx(struct pmu_hw_events *cpuc,
415*7dfc8db1SSuzuki K Poulose struct perf_event *event)
416*7dfc8db1SSuzuki K Poulose {
417*7dfc8db1SSuzuki K Poulose clear_bit(event->hw.idx, cpuc->used_mask);
418*7dfc8db1SSuzuki K Poulose }
419*7dfc8db1SSuzuki K Poulose
armv6pmu_disable_event(struct perf_event * event)420ed6f2a52SSudeep KarkadaNagesha static void armv6pmu_disable_event(struct perf_event *event)
42143eab878SWill Deacon {
42243eab878SWill Deacon unsigned long val, mask, evt, flags;
423ed6f2a52SSudeep KarkadaNagesha struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
424ed6f2a52SSudeep KarkadaNagesha struct hw_perf_event *hwc = &event->hw;
42511679250SMark Rutland struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
426ed6f2a52SSudeep KarkadaNagesha int idx = hwc->idx;
42743eab878SWill Deacon
42843eab878SWill Deacon if (ARMV6_CYCLE_COUNTER == idx) {
42943eab878SWill Deacon mask = ARMV6_PMCR_CCOUNT_IEN;
43043eab878SWill Deacon evt = 0;
43143eab878SWill Deacon } else if (ARMV6_COUNTER0 == idx) {
43243eab878SWill Deacon mask = ARMV6_PMCR_COUNT0_IEN | ARMV6_PMCR_EVT_COUNT0_MASK;
43343eab878SWill Deacon evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT0_SHIFT;
43443eab878SWill Deacon } else if (ARMV6_COUNTER1 == idx) {
43543eab878SWill Deacon mask = ARMV6_PMCR_COUNT1_IEN | ARMV6_PMCR_EVT_COUNT1_MASK;
43643eab878SWill Deacon evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT1_SHIFT;
43743eab878SWill Deacon } else {
43843eab878SWill Deacon WARN_ONCE(1, "invalid counter number (%d)\n", idx);
43943eab878SWill Deacon return;
44043eab878SWill Deacon }
44143eab878SWill Deacon
44243eab878SWill Deacon /*
44343eab878SWill Deacon * Mask out the current event and set the counter to count the number
44443eab878SWill Deacon * of ETM bus signal assertion cycles. The external reporting should
44543eab878SWill Deacon * be disabled and so this should never increment.
44643eab878SWill Deacon */
4470f78d2d5SMark Rutland raw_spin_lock_irqsave(&events->pmu_lock, flags);
44843eab878SWill Deacon val = armv6_pmcr_read();
44943eab878SWill Deacon val &= ~mask;
45043eab878SWill Deacon val |= evt;
45143eab878SWill Deacon armv6_pmcr_write(val);
4520f78d2d5SMark Rutland raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
45343eab878SWill Deacon }
45443eab878SWill Deacon
armv6mpcore_pmu_disable_event(struct perf_event * event)455ed6f2a52SSudeep KarkadaNagesha static void armv6mpcore_pmu_disable_event(struct perf_event *event)
45643eab878SWill Deacon {
45743eab878SWill Deacon unsigned long val, mask, flags, evt = 0;
458ed6f2a52SSudeep KarkadaNagesha struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
459ed6f2a52SSudeep KarkadaNagesha struct hw_perf_event *hwc = &event->hw;
46011679250SMark Rutland struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
461ed6f2a52SSudeep KarkadaNagesha int idx = hwc->idx;
46243eab878SWill Deacon
46343eab878SWill Deacon if (ARMV6_CYCLE_COUNTER == idx) {
46443eab878SWill Deacon mask = ARMV6_PMCR_CCOUNT_IEN;
46543eab878SWill Deacon } else if (ARMV6_COUNTER0 == idx) {
46643eab878SWill Deacon mask = ARMV6_PMCR_COUNT0_IEN;
46743eab878SWill Deacon } else if (ARMV6_COUNTER1 == idx) {
46843eab878SWill Deacon mask = ARMV6_PMCR_COUNT1_IEN;
46943eab878SWill Deacon } else {
47043eab878SWill Deacon WARN_ONCE(1, "invalid counter number (%d)\n", idx);
47143eab878SWill Deacon return;
47243eab878SWill Deacon }
47343eab878SWill Deacon
47443eab878SWill Deacon /*
47543eab878SWill Deacon * Unlike UP ARMv6, we don't have a way of stopping the counters. We
47643eab878SWill Deacon * simply disable the interrupt reporting.
47743eab878SWill Deacon */
4780f78d2d5SMark Rutland raw_spin_lock_irqsave(&events->pmu_lock, flags);
47943eab878SWill Deacon val = armv6_pmcr_read();
48043eab878SWill Deacon val &= ~mask;
48143eab878SWill Deacon val |= evt;
48243eab878SWill Deacon armv6_pmcr_write(val);
4830f78d2d5SMark Rutland raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
48443eab878SWill Deacon }
48543eab878SWill Deacon
armv6_map_event(struct perf_event * event)486e1f431b5SMark Rutland static int armv6_map_event(struct perf_event *event)
487e1f431b5SMark Rutland {
4886dbc0029SWill Deacon return armpmu_map_event(event, &armv6_perf_map,
489e1f431b5SMark Rutland &armv6_perf_cache_map, 0xFF);
490e1f431b5SMark Rutland }
491e1f431b5SMark Rutland
armv6pmu_init(struct arm_pmu * cpu_pmu)4923d1ff755SMark Rutland static void armv6pmu_init(struct arm_pmu *cpu_pmu)
49343eab878SWill Deacon {
494513c99ceSSudeep KarkadaNagesha cpu_pmu->handle_irq = armv6pmu_handle_irq;
495513c99ceSSudeep KarkadaNagesha cpu_pmu->enable = armv6pmu_enable_event;
496513c99ceSSudeep KarkadaNagesha cpu_pmu->disable = armv6pmu_disable_event;
497513c99ceSSudeep KarkadaNagesha cpu_pmu->read_counter = armv6pmu_read_counter;
498513c99ceSSudeep KarkadaNagesha cpu_pmu->write_counter = armv6pmu_write_counter;
499513c99ceSSudeep KarkadaNagesha cpu_pmu->get_event_idx = armv6pmu_get_event_idx;
500*7dfc8db1SSuzuki K Poulose cpu_pmu->clear_event_idx = armv6pmu_clear_event_idx;
501513c99ceSSudeep KarkadaNagesha cpu_pmu->start = armv6pmu_start;
502513c99ceSSudeep KarkadaNagesha cpu_pmu->stop = armv6pmu_stop;
503513c99ceSSudeep KarkadaNagesha cpu_pmu->map_event = armv6_map_event;
504513c99ceSSudeep KarkadaNagesha cpu_pmu->num_events = 3;
5053d1ff755SMark Rutland }
506513c99ceSSudeep KarkadaNagesha
armv6_1136_pmu_init(struct arm_pmu * cpu_pmu)5073d1ff755SMark Rutland static int armv6_1136_pmu_init(struct arm_pmu *cpu_pmu)
5083d1ff755SMark Rutland {
5093d1ff755SMark Rutland armv6pmu_init(cpu_pmu);
5103d1ff755SMark Rutland cpu_pmu->name = "armv6_1136";
5113d1ff755SMark Rutland return 0;
5123d1ff755SMark Rutland }
5133d1ff755SMark Rutland
armv6_1156_pmu_init(struct arm_pmu * cpu_pmu)5143d1ff755SMark Rutland static int armv6_1156_pmu_init(struct arm_pmu *cpu_pmu)
5153d1ff755SMark Rutland {
5163d1ff755SMark Rutland armv6pmu_init(cpu_pmu);
5173d1ff755SMark Rutland cpu_pmu->name = "armv6_1156";
5183d1ff755SMark Rutland return 0;
5193d1ff755SMark Rutland }
5203d1ff755SMark Rutland
armv6_1176_pmu_init(struct arm_pmu * cpu_pmu)5213d1ff755SMark Rutland static int armv6_1176_pmu_init(struct arm_pmu *cpu_pmu)
5223d1ff755SMark Rutland {
5233d1ff755SMark Rutland armv6pmu_init(cpu_pmu);
5243d1ff755SMark Rutland cpu_pmu->name = "armv6_1176";
525513c99ceSSudeep KarkadaNagesha return 0;
52643eab878SWill Deacon }
52743eab878SWill Deacon
52843eab878SWill Deacon /*
52943eab878SWill Deacon * ARMv6mpcore is almost identical to single core ARMv6 with the exception
53043eab878SWill Deacon * that some of the events have different enumerations and that there is no
53143eab878SWill Deacon * *hack* to stop the programmable counters. To stop the counters we simply
53243eab878SWill Deacon * disable the interrupt reporting and update the event. When unthrottling we
53343eab878SWill Deacon * reset the period and enable the interrupt reporting.
53443eab878SWill Deacon */
535e1f431b5SMark Rutland
armv6mpcore_map_event(struct perf_event * event)536e1f431b5SMark Rutland static int armv6mpcore_map_event(struct perf_event *event)
537e1f431b5SMark Rutland {
5386dbc0029SWill Deacon return armpmu_map_event(event, &armv6mpcore_perf_map,
539e1f431b5SMark Rutland &armv6mpcore_perf_cache_map, 0xFF);
540e1f431b5SMark Rutland }
541e1f431b5SMark Rutland
armv6mpcore_pmu_init(struct arm_pmu * cpu_pmu)542351a102dSGreg Kroah-Hartman static int armv6mpcore_pmu_init(struct arm_pmu *cpu_pmu)
54343eab878SWill Deacon {
5443d1ff755SMark Rutland cpu_pmu->name = "armv6_11mpcore";
545513c99ceSSudeep KarkadaNagesha cpu_pmu->handle_irq = armv6pmu_handle_irq;
546513c99ceSSudeep KarkadaNagesha cpu_pmu->enable = armv6pmu_enable_event;
547513c99ceSSudeep KarkadaNagesha cpu_pmu->disable = armv6mpcore_pmu_disable_event;
548513c99ceSSudeep KarkadaNagesha cpu_pmu->read_counter = armv6pmu_read_counter;
549513c99ceSSudeep KarkadaNagesha cpu_pmu->write_counter = armv6pmu_write_counter;
550513c99ceSSudeep KarkadaNagesha cpu_pmu->get_event_idx = armv6pmu_get_event_idx;
551*7dfc8db1SSuzuki K Poulose cpu_pmu->clear_event_idx = armv6pmu_clear_event_idx;
552513c99ceSSudeep KarkadaNagesha cpu_pmu->start = armv6pmu_start;
553513c99ceSSudeep KarkadaNagesha cpu_pmu->stop = armv6pmu_stop;
554513c99ceSSudeep KarkadaNagesha cpu_pmu->map_event = armv6mpcore_map_event;
555513c99ceSSudeep KarkadaNagesha cpu_pmu->num_events = 3;
556513c99ceSSudeep KarkadaNagesha
557513c99ceSSudeep KarkadaNagesha return 0;
55843eab878SWill Deacon }
5591fe115b3SMark Rutland
560d5f7b828SArvind Yadav static const struct of_device_id armv6_pmu_of_device_ids[] = {
5611fe115b3SMark Rutland {.compatible = "arm,arm11mpcore-pmu", .data = armv6mpcore_pmu_init},
5621fe115b3SMark Rutland {.compatible = "arm,arm1176-pmu", .data = armv6_1176_pmu_init},
5631fe115b3SMark Rutland {.compatible = "arm,arm1136-pmu", .data = armv6_1136_pmu_init},
5641fe115b3SMark Rutland { /* sentinel value */ }
5651fe115b3SMark Rutland };
5661fe115b3SMark Rutland
5671fe115b3SMark Rutland static const struct pmu_probe_info armv6_pmu_probe_table[] = {
5681fe115b3SMark Rutland ARM_PMU_PROBE(ARM_CPU_PART_ARM1136, armv6_1136_pmu_init),
5691fe115b3SMark Rutland ARM_PMU_PROBE(ARM_CPU_PART_ARM1156, armv6_1156_pmu_init),
5701fe115b3SMark Rutland ARM_PMU_PROBE(ARM_CPU_PART_ARM1176, armv6_1176_pmu_init),
5711fe115b3SMark Rutland ARM_PMU_PROBE(ARM_CPU_PART_ARM11MPCORE, armv6mpcore_pmu_init),
5721fe115b3SMark Rutland { /* sentinel value */ }
5731fe115b3SMark Rutland };
5741fe115b3SMark Rutland
armv6_pmu_device_probe(struct platform_device * pdev)5751fe115b3SMark Rutland static int armv6_pmu_device_probe(struct platform_device *pdev)
5763d1ff755SMark Rutland {
5771fe115b3SMark Rutland return arm_pmu_device_probe(pdev, armv6_pmu_of_device_ids,
5781fe115b3SMark Rutland armv6_pmu_probe_table);
5793d1ff755SMark Rutland }
5803d1ff755SMark Rutland
5811fe115b3SMark Rutland static struct platform_driver armv6_pmu_driver = {
5821fe115b3SMark Rutland .driver = {
5831fe115b3SMark Rutland .name = "armv6-pmu",
5841fe115b3SMark Rutland .of_match_table = armv6_pmu_of_device_ids,
5851fe115b3SMark Rutland },
5861fe115b3SMark Rutland .probe = armv6_pmu_device_probe,
5871fe115b3SMark Rutland };
5883d1ff755SMark Rutland
589b128cb55SGeliang Tang builtin_platform_driver(armv6_pmu_driver);
590e399b1a4SRussell King #endif /* CONFIG_CPU_V6 || CONFIG_CPU_V6K */
591