blob: ac1c5c41501d3aebc4617871a8c182b558886189 [file] [log] [blame]
Will Deacon03089682012-03-05 11:49:32 +00001/*
2 * PMU support
3 *
4 * Copyright (C) 2012 ARM Limited
5 * Author: Will Deacon <will.deacon@arm.com>
6 *
7 * This code is based heavily on the ARMv7 perf event code.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
Will Deacon03089682012-03-05 11:49:32 +000021
Will Deacon03089682012-03-05 11:49:32 +000022#include <asm/irq_regs.h>
Shannon Zhaob8cfadf2016-03-24 16:01:16 +000023#include <asm/perf_event.h>
Ashok Kumarbf2d4782016-04-21 05:58:43 -070024#include <asm/sysreg.h>
Marc Zyngierd98ecda2016-01-25 17:31:13 +000025#include <asm/virt.h>
Will Deacon03089682012-03-05 11:49:32 +000026
Mark Salterdbee3a72016-09-14 17:32:29 -050027#include <linux/acpi.h>
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -070028#include <linux/clocksource.h>
Mark Rutland6475b2d2015-10-02 10:55:03 +010029#include <linux/of.h>
30#include <linux/perf/arm_pmu.h>
31#include <linux/platform_device.h>
Will Deacon03089682012-03-05 11:49:32 +000032
33/*
34 * ARMv8 PMUv3 Performance Events handling code.
Wei Huangb112c842016-11-16 11:09:20 -060035 * Common event types (some are defined in asm/perf_event.h).
Will Deacon03089682012-03-05 11:49:32 +000036 */
Will Deacon03089682012-03-05 11:49:32 +000037
Drew Richardson90381cb2015-10-22 07:07:01 -070038/* At least one of the following is required. */
Ashok Kumar03598fd2016-04-21 05:58:41 -070039#define ARMV8_PMUV3_PERFCTR_INST_RETIRED 0x08
40#define ARMV8_PMUV3_PERFCTR_INST_SPEC 0x1B
Will Deacon03089682012-03-05 11:49:32 +000041
Drew Richardson90381cb2015-10-22 07:07:01 -070042/* Common architectural events. */
Ashok Kumar03598fd2016-04-21 05:58:41 -070043#define ARMV8_PMUV3_PERFCTR_LD_RETIRED 0x06
44#define ARMV8_PMUV3_PERFCTR_ST_RETIRED 0x07
Drew Richardson90381cb2015-10-22 07:07:01 -070045#define ARMV8_PMUV3_PERFCTR_EXC_TAKEN 0x09
Ashok Kumar03598fd2016-04-21 05:58:41 -070046#define ARMV8_PMUV3_PERFCTR_EXC_RETURN 0x0A
47#define ARMV8_PMUV3_PERFCTR_CID_WRITE_RETIRED 0x0B
48#define ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED 0x0C
49#define ARMV8_PMUV3_PERFCTR_BR_IMMED_RETIRED 0x0D
50#define ARMV8_PMUV3_PERFCTR_BR_RETURN_RETIRED 0x0E
51#define ARMV8_PMUV3_PERFCTR_UNALIGNED_LDST_RETIRED 0x0F
52#define ARMV8_PMUV3_PERFCTR_TTBR_WRITE_RETIRED 0x1C
Drew Richardson9e9caa62015-10-22 07:07:32 -070053#define ARMV8_PMUV3_PERFCTR_CHAIN 0x1E
54#define ARMV8_PMUV3_PERFCTR_BR_RETIRED 0x21
Drew Richardson90381cb2015-10-22 07:07:01 -070055
56/* Common microarchitectural events. */
Ashok Kumar03598fd2016-04-21 05:58:41 -070057#define ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL 0x01
58#define ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL 0x02
59#define ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL 0x05
Drew Richardson90381cb2015-10-22 07:07:01 -070060#define ARMV8_PMUV3_PERFCTR_MEM_ACCESS 0x13
Ashok Kumar03598fd2016-04-21 05:58:41 -070061#define ARMV8_PMUV3_PERFCTR_L1I_CACHE 0x14
62#define ARMV8_PMUV3_PERFCTR_L1D_CACHE_WB 0x15
63#define ARMV8_PMUV3_PERFCTR_L2D_CACHE 0x16
64#define ARMV8_PMUV3_PERFCTR_L2D_CACHE_REFILL 0x17
65#define ARMV8_PMUV3_PERFCTR_L2D_CACHE_WB 0x18
Drew Richardson90381cb2015-10-22 07:07:01 -070066#define ARMV8_PMUV3_PERFCTR_BUS_ACCESS 0x19
Ashok Kumar03598fd2016-04-21 05:58:41 -070067#define ARMV8_PMUV3_PERFCTR_MEMORY_ERROR 0x1A
Drew Richardson90381cb2015-10-22 07:07:01 -070068#define ARMV8_PMUV3_PERFCTR_BUS_CYCLES 0x1D
Drew Richardson9e9caa62015-10-22 07:07:32 -070069#define ARMV8_PMUV3_PERFCTR_L1D_CACHE_ALLOCATE 0x1F
70#define ARMV8_PMUV3_PERFCTR_L2D_CACHE_ALLOCATE 0x20
71#define ARMV8_PMUV3_PERFCTR_BR_MIS_PRED_RETIRED 0x22
72#define ARMV8_PMUV3_PERFCTR_STALL_FRONTEND 0x23
73#define ARMV8_PMUV3_PERFCTR_STALL_BACKEND 0x24
74#define ARMV8_PMUV3_PERFCTR_L1D_TLB 0x25
75#define ARMV8_PMUV3_PERFCTR_L1I_TLB 0x26
76#define ARMV8_PMUV3_PERFCTR_L2I_CACHE 0x27
77#define ARMV8_PMUV3_PERFCTR_L2I_CACHE_REFILL 0x28
78#define ARMV8_PMUV3_PERFCTR_L3D_CACHE_ALLOCATE 0x29
79#define ARMV8_PMUV3_PERFCTR_L3D_CACHE_REFILL 0x2A
80#define ARMV8_PMUV3_PERFCTR_L3D_CACHE 0x2B
81#define ARMV8_PMUV3_PERFCTR_L3D_CACHE_WB 0x2C
82#define ARMV8_PMUV3_PERFCTR_L2D_TLB_REFILL 0x2D
Ashok Kumar03598fd2016-04-21 05:58:41 -070083#define ARMV8_PMUV3_PERFCTR_L2I_TLB_REFILL 0x2E
Drew Richardson9e9caa62015-10-22 07:07:32 -070084#define ARMV8_PMUV3_PERFCTR_L2D_TLB 0x2F
Ashok Kumar03598fd2016-04-21 05:58:41 -070085#define ARMV8_PMUV3_PERFCTR_L2I_TLB 0x30
Will Deacon03089682012-03-05 11:49:32 +000086
Ashok Kumar03598fd2016-04-21 05:58:41 -070087/* ARMv8 recommended implementation defined event types */
88#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD 0x40
89#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR 0x41
90#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD 0x42
91#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR 0x43
Ashok Kumar0893f742016-04-21 05:58:42 -070092#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_INNER 0x44
93#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_OUTER 0x45
94#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WB_VICTIM 0x46
95#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WB_CLEAN 0x47
96#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_INVAL 0x48
97
Ashok Kumar03598fd2016-04-21 05:58:41 -070098#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD 0x4C
99#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR 0x4D
100#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD 0x4E
101#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR 0x4F
Ashok Kumar0893f742016-04-21 05:58:42 -0700102#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_RD 0x50
103#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_WR 0x51
104#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_REFILL_RD 0x52
105#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_REFILL_WR 0x53
106
107#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_WB_VICTIM 0x56
108#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_WB_CLEAN 0x57
109#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_INVAL 0x58
110
111#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_REFILL_RD 0x5C
112#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_REFILL_WR 0x5D
113#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_RD 0x5E
114#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_WR 0x5F
115
116#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD 0x60
117#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR 0x61
118#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_SHARED 0x62
119#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_NOT_SHARED 0x63
120#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_NORMAL 0x64
121#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_PERIPH 0x65
122
123#define ARMV8_IMPDEF_PERFCTR_MEM_ACCESS_RD 0x66
124#define ARMV8_IMPDEF_PERFCTR_MEM_ACCESS_WR 0x67
125#define ARMV8_IMPDEF_PERFCTR_UNALIGNED_LD_SPEC 0x68
126#define ARMV8_IMPDEF_PERFCTR_UNALIGNED_ST_SPEC 0x69
127#define ARMV8_IMPDEF_PERFCTR_UNALIGNED_LDST_SPEC 0x6A
128
129#define ARMV8_IMPDEF_PERFCTR_LDREX_SPEC 0x6C
130#define ARMV8_IMPDEF_PERFCTR_STREX_PASS_SPEC 0x6D
131#define ARMV8_IMPDEF_PERFCTR_STREX_FAIL_SPEC 0x6E
132#define ARMV8_IMPDEF_PERFCTR_STREX_SPEC 0x6F
133#define ARMV8_IMPDEF_PERFCTR_LD_SPEC 0x70
134#define ARMV8_IMPDEF_PERFCTR_ST_SPEC 0x71
135#define ARMV8_IMPDEF_PERFCTR_LDST_SPEC 0x72
136#define ARMV8_IMPDEF_PERFCTR_DP_SPEC 0x73
137#define ARMV8_IMPDEF_PERFCTR_ASE_SPEC 0x74
138#define ARMV8_IMPDEF_PERFCTR_VFP_SPEC 0x75
139#define ARMV8_IMPDEF_PERFCTR_PC_WRITE_SPEC 0x76
140#define ARMV8_IMPDEF_PERFCTR_CRYPTO_SPEC 0x77
141#define ARMV8_IMPDEF_PERFCTR_BR_IMMED_SPEC 0x78
142#define ARMV8_IMPDEF_PERFCTR_BR_RETURN_SPEC 0x79
143#define ARMV8_IMPDEF_PERFCTR_BR_INDIRECT_SPEC 0x7A
144
145#define ARMV8_IMPDEF_PERFCTR_ISB_SPEC 0x7C
146#define ARMV8_IMPDEF_PERFCTR_DSB_SPEC 0x7D
147#define ARMV8_IMPDEF_PERFCTR_DMB_SPEC 0x7E
148
149#define ARMV8_IMPDEF_PERFCTR_EXC_UNDEF 0x81
150#define ARMV8_IMPDEF_PERFCTR_EXC_SVC 0x82
151#define ARMV8_IMPDEF_PERFCTR_EXC_PABORT 0x83
152#define ARMV8_IMPDEF_PERFCTR_EXC_DABORT 0x84
153
154#define ARMV8_IMPDEF_PERFCTR_EXC_IRQ 0x86
155#define ARMV8_IMPDEF_PERFCTR_EXC_FIQ 0x87
156#define ARMV8_IMPDEF_PERFCTR_EXC_SMC 0x88
157
158#define ARMV8_IMPDEF_PERFCTR_EXC_HVC 0x8A
159#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_PABORT 0x8B
160#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_DABORT 0x8C
161#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_OTHER 0x8D
162#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_IRQ 0x8E
163#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_FIQ 0x8F
164#define ARMV8_IMPDEF_PERFCTR_RC_LD_SPEC 0x90
165#define ARMV8_IMPDEF_PERFCTR_RC_ST_SPEC 0x91
166
167#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_RD 0xA0
168#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_WR 0xA1
169#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_REFILL_RD 0xA2
170#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_REFILL_WR 0xA3
171
172#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_WB_VICTIM 0xA6
173#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_WB_CLEAN 0xA7
174#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_INVAL 0xA8
Jan Glauber5f140cc2016-02-18 17:50:10 +0100175
Mark Rutlandac82d122015-10-02 10:55:04 +0100176/* ARMv8 Cortex-A53 specific event types. */
Ashok Kumar03598fd2016-04-21 05:58:41 -0700177#define ARMV8_A53_PERFCTR_PREF_LINEFILL 0xC2
Mark Rutlandac82d122015-10-02 10:55:04 +0100178
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100179/* ARMv8 Cavium ThunderX specific event types. */
Ashok Kumar03598fd2016-04-21 05:58:41 -0700180#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST 0xE9
181#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS 0xEA
182#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS 0xEB
183#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS 0xEC
184#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS 0xED
Mark Rutland62a4dda2015-10-02 10:55:05 +0100185
Will Deacon03089682012-03-05 11:49:32 +0000186/* PMUv3 HW events mapping. */
Jeremy Linton236b9b912016-09-14 17:32:30 -0500187
188/*
189 * ARMv8 Architectural defined events, not all of these may
190 * be supported on any given implementation. Undefined events will
191 * be disabled at run-time.
192 */
Will Deacon03089682012-03-05 11:49:32 +0000193static const unsigned armv8_pmuv3_perf_map[PERF_COUNT_HW_MAX] = {
Mark Rutlandae2fb7e2015-07-21 11:36:39 +0100194 PERF_MAP_ALL_UNSUPPORTED,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700195 [PERF_COUNT_HW_CPU_CYCLES] = ARMV8_PMUV3_PERFCTR_CPU_CYCLES,
196 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_INST_RETIRED,
197 [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
198 [PERF_COUNT_HW_CACHE_MISSES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
Jeremy Linton236b9b912016-09-14 17:32:30 -0500199 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700200 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Jeremy Linton236b9b912016-09-14 17:32:30 -0500201 [PERF_COUNT_HW_BUS_CYCLES] = ARMV8_PMUV3_PERFCTR_BUS_CYCLES,
202 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV8_PMUV3_PERFCTR_STALL_FRONTEND,
203 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV8_PMUV3_PERFCTR_STALL_BACKEND,
Will Deacon03089682012-03-05 11:49:32 +0000204};
205
206static const unsigned armv8_pmuv3_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
207 [PERF_COUNT_HW_CACHE_OP_MAX]
208 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
Mark Rutlandae2fb7e2015-07-21 11:36:39 +0100209 PERF_CACHE_MAP_ALL_UNSUPPORTED,
210
Ashok Kumar03598fd2016-04-21 05:58:41 -0700211 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
212 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
213 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
214 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
Mark Rutlandae2fb7e2015-07-21 11:36:39 +0100215
Jeremy Linton236b9b912016-09-14 17:32:30 -0500216 [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE,
217 [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL,
218
219 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL,
220 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_TLB,
221
222 [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL,
223 [C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB,
224
Ashok Kumar03598fd2016-04-21 05:58:41 -0700225 [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
226 [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
227 [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
228 [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Will Deacon03089682012-03-05 11:49:32 +0000229};
230
Mark Rutlandac82d122015-10-02 10:55:04 +0100231static const unsigned armv8_a53_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
232 [PERF_COUNT_HW_CACHE_OP_MAX]
233 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
234 PERF_CACHE_MAP_ALL_UNSUPPORTED,
235
Ashok Kumar03598fd2016-04-21 05:58:41 -0700236 [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_A53_PERFCTR_PREF_LINEFILL,
Mark Rutlandac82d122015-10-02 10:55:04 +0100237
Julien Thierry5cf7fb22017-07-25 17:27:36 +0100238 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
239 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
Mark Rutlandac82d122015-10-02 10:55:04 +0100240};
241
Mark Rutland62a4dda2015-10-02 10:55:05 +0100242static const unsigned armv8_a57_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
243 [PERF_COUNT_HW_CACHE_OP_MAX]
244 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
245 PERF_CACHE_MAP_ALL_UNSUPPORTED,
246
Ashok Kumar03598fd2016-04-21 05:58:41 -0700247 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
248 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
249 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
250 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100251
Ashok Kumar03598fd2016-04-21 05:58:41 -0700252 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
253 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100254
Julien Thierry5cf7fb22017-07-25 17:27:36 +0100255 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
256 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100257};
258
Julien Thierry5561b6c2017-08-09 17:46:38 +0100259static const unsigned armv8_a73_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
260 [PERF_COUNT_HW_CACHE_OP_MAX]
261 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
262 PERF_CACHE_MAP_ALL_UNSUPPORTED,
263
264 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
265 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
Julien Thierry5561b6c2017-08-09 17:46:38 +0100266};
267
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100268static const unsigned armv8_thunder_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
269 [PERF_COUNT_HW_CACHE_OP_MAX]
270 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
271 PERF_CACHE_MAP_ALL_UNSUPPORTED,
272
Ashok Kumar03598fd2016-04-21 05:58:41 -0700273 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
274 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
275 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
276 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST,
277 [C(L1D)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS,
278 [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS,
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100279
Ashok Kumar03598fd2016-04-21 05:58:41 -0700280 [C(L1I)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS,
281 [C(L1I)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS,
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100282
Ashok Kumar03598fd2016-04-21 05:58:41 -0700283 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
284 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
285 [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
286 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100287};
288
Ashok Kumar201a72b2016-04-21 05:58:45 -0700289static const unsigned armv8_vulcan_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
290 [PERF_COUNT_HW_CACHE_OP_MAX]
291 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
292 PERF_CACHE_MAP_ALL_UNSUPPORTED,
293
294 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
295 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
296 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
297 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR,
298
Ashok Kumar201a72b2016-04-21 05:58:45 -0700299 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
300 [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
301 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
302 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
303
Ashok Kumar201a72b2016-04-21 05:58:45 -0700304 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
305 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
306};
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700307
308static ssize_t
309armv8pmu_events_sysfs_show(struct device *dev,
310 struct device_attribute *attr, char *page)
311{
312 struct perf_pmu_events_attr *pmu_attr;
313
314 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
315
316 return sprintf(page, "event=0x%03llx\n", pmu_attr->id);
317}
318
Drew Richardson9e9caa62015-10-22 07:07:32 -0700319#define ARMV8_EVENT_ATTR_RESOLVE(m) #m
320#define ARMV8_EVENT_ATTR(name, config) \
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700321 PMU_EVENT_ATTR(name, armv8_event_attr_##name, \
322 config, armv8pmu_events_sysfs_show)
Drew Richardson9e9caa62015-10-22 07:07:32 -0700323
Ashok Kumar03598fd2016-04-21 05:58:41 -0700324ARMV8_EVENT_ATTR(sw_incr, ARMV8_PMUV3_PERFCTR_SW_INCR);
325ARMV8_EVENT_ATTR(l1i_cache_refill, ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL);
326ARMV8_EVENT_ATTR(l1i_tlb_refill, ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL);
327ARMV8_EVENT_ATTR(l1d_cache_refill, ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL);
328ARMV8_EVENT_ATTR(l1d_cache, ARMV8_PMUV3_PERFCTR_L1D_CACHE);
329ARMV8_EVENT_ATTR(l1d_tlb_refill, ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL);
330ARMV8_EVENT_ATTR(ld_retired, ARMV8_PMUV3_PERFCTR_LD_RETIRED);
331ARMV8_EVENT_ATTR(st_retired, ARMV8_PMUV3_PERFCTR_ST_RETIRED);
332ARMV8_EVENT_ATTR(inst_retired, ARMV8_PMUV3_PERFCTR_INST_RETIRED);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700333ARMV8_EVENT_ATTR(exc_taken, ARMV8_PMUV3_PERFCTR_EXC_TAKEN);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700334ARMV8_EVENT_ATTR(exc_return, ARMV8_PMUV3_PERFCTR_EXC_RETURN);
335ARMV8_EVENT_ATTR(cid_write_retired, ARMV8_PMUV3_PERFCTR_CID_WRITE_RETIRED);
336ARMV8_EVENT_ATTR(pc_write_retired, ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED);
337ARMV8_EVENT_ATTR(br_immed_retired, ARMV8_PMUV3_PERFCTR_BR_IMMED_RETIRED);
338ARMV8_EVENT_ATTR(br_return_retired, ARMV8_PMUV3_PERFCTR_BR_RETURN_RETIRED);
339ARMV8_EVENT_ATTR(unaligned_ldst_retired, ARMV8_PMUV3_PERFCTR_UNALIGNED_LDST_RETIRED);
340ARMV8_EVENT_ATTR(br_mis_pred, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED);
341ARMV8_EVENT_ATTR(cpu_cycles, ARMV8_PMUV3_PERFCTR_CPU_CYCLES);
342ARMV8_EVENT_ATTR(br_pred, ARMV8_PMUV3_PERFCTR_BR_PRED);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700343ARMV8_EVENT_ATTR(mem_access, ARMV8_PMUV3_PERFCTR_MEM_ACCESS);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700344ARMV8_EVENT_ATTR(l1i_cache, ARMV8_PMUV3_PERFCTR_L1I_CACHE);
345ARMV8_EVENT_ATTR(l1d_cache_wb, ARMV8_PMUV3_PERFCTR_L1D_CACHE_WB);
346ARMV8_EVENT_ATTR(l2d_cache, ARMV8_PMUV3_PERFCTR_L2D_CACHE);
347ARMV8_EVENT_ATTR(l2d_cache_refill, ARMV8_PMUV3_PERFCTR_L2D_CACHE_REFILL);
348ARMV8_EVENT_ATTR(l2d_cache_wb, ARMV8_PMUV3_PERFCTR_L2D_CACHE_WB);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700349ARMV8_EVENT_ATTR(bus_access, ARMV8_PMUV3_PERFCTR_BUS_ACCESS);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700350ARMV8_EVENT_ATTR(memory_error, ARMV8_PMUV3_PERFCTR_MEMORY_ERROR);
351ARMV8_EVENT_ATTR(inst_spec, ARMV8_PMUV3_PERFCTR_INST_SPEC);
352ARMV8_EVENT_ATTR(ttbr_write_retired, ARMV8_PMUV3_PERFCTR_TTBR_WRITE_RETIRED);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700353ARMV8_EVENT_ATTR(bus_cycles, ARMV8_PMUV3_PERFCTR_BUS_CYCLES);
Will Deacon4ba25782016-04-25 15:05:24 +0100354/* Don't expose the chain event in /sys, since it's useless in isolation */
Drew Richardson9e9caa62015-10-22 07:07:32 -0700355ARMV8_EVENT_ATTR(l1d_cache_allocate, ARMV8_PMUV3_PERFCTR_L1D_CACHE_ALLOCATE);
356ARMV8_EVENT_ATTR(l2d_cache_allocate, ARMV8_PMUV3_PERFCTR_L2D_CACHE_ALLOCATE);
357ARMV8_EVENT_ATTR(br_retired, ARMV8_PMUV3_PERFCTR_BR_RETIRED);
358ARMV8_EVENT_ATTR(br_mis_pred_retired, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED_RETIRED);
359ARMV8_EVENT_ATTR(stall_frontend, ARMV8_PMUV3_PERFCTR_STALL_FRONTEND);
360ARMV8_EVENT_ATTR(stall_backend, ARMV8_PMUV3_PERFCTR_STALL_BACKEND);
361ARMV8_EVENT_ATTR(l1d_tlb, ARMV8_PMUV3_PERFCTR_L1D_TLB);
362ARMV8_EVENT_ATTR(l1i_tlb, ARMV8_PMUV3_PERFCTR_L1I_TLB);
363ARMV8_EVENT_ATTR(l2i_cache, ARMV8_PMUV3_PERFCTR_L2I_CACHE);
364ARMV8_EVENT_ATTR(l2i_cache_refill, ARMV8_PMUV3_PERFCTR_L2I_CACHE_REFILL);
365ARMV8_EVENT_ATTR(l3d_cache_allocate, ARMV8_PMUV3_PERFCTR_L3D_CACHE_ALLOCATE);
366ARMV8_EVENT_ATTR(l3d_cache_refill, ARMV8_PMUV3_PERFCTR_L3D_CACHE_REFILL);
367ARMV8_EVENT_ATTR(l3d_cache, ARMV8_PMUV3_PERFCTR_L3D_CACHE);
368ARMV8_EVENT_ATTR(l3d_cache_wb, ARMV8_PMUV3_PERFCTR_L3D_CACHE_WB);
369ARMV8_EVENT_ATTR(l2d_tlb_refill, ARMV8_PMUV3_PERFCTR_L2D_TLB_REFILL);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700370ARMV8_EVENT_ATTR(l2i_tlb_refill, ARMV8_PMUV3_PERFCTR_L2I_TLB_REFILL);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700371ARMV8_EVENT_ATTR(l2d_tlb, ARMV8_PMUV3_PERFCTR_L2D_TLB);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700372ARMV8_EVENT_ATTR(l2i_tlb, ARMV8_PMUV3_PERFCTR_L2I_TLB);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700373
374static struct attribute *armv8_pmuv3_event_attrs[] = {
375 &armv8_event_attr_sw_incr.attr.attr,
376 &armv8_event_attr_l1i_cache_refill.attr.attr,
377 &armv8_event_attr_l1i_tlb_refill.attr.attr,
378 &armv8_event_attr_l1d_cache_refill.attr.attr,
379 &armv8_event_attr_l1d_cache.attr.attr,
380 &armv8_event_attr_l1d_tlb_refill.attr.attr,
381 &armv8_event_attr_ld_retired.attr.attr,
382 &armv8_event_attr_st_retired.attr.attr,
383 &armv8_event_attr_inst_retired.attr.attr,
384 &armv8_event_attr_exc_taken.attr.attr,
385 &armv8_event_attr_exc_return.attr.attr,
386 &armv8_event_attr_cid_write_retired.attr.attr,
387 &armv8_event_attr_pc_write_retired.attr.attr,
388 &armv8_event_attr_br_immed_retired.attr.attr,
389 &armv8_event_attr_br_return_retired.attr.attr,
390 &armv8_event_attr_unaligned_ldst_retired.attr.attr,
391 &armv8_event_attr_br_mis_pred.attr.attr,
392 &armv8_event_attr_cpu_cycles.attr.attr,
393 &armv8_event_attr_br_pred.attr.attr,
394 &armv8_event_attr_mem_access.attr.attr,
395 &armv8_event_attr_l1i_cache.attr.attr,
396 &armv8_event_attr_l1d_cache_wb.attr.attr,
397 &armv8_event_attr_l2d_cache.attr.attr,
398 &armv8_event_attr_l2d_cache_refill.attr.attr,
399 &armv8_event_attr_l2d_cache_wb.attr.attr,
400 &armv8_event_attr_bus_access.attr.attr,
401 &armv8_event_attr_memory_error.attr.attr,
402 &armv8_event_attr_inst_spec.attr.attr,
403 &armv8_event_attr_ttbr_write_retired.attr.attr,
404 &armv8_event_attr_bus_cycles.attr.attr,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700405 &armv8_event_attr_l1d_cache_allocate.attr.attr,
406 &armv8_event_attr_l2d_cache_allocate.attr.attr,
407 &armv8_event_attr_br_retired.attr.attr,
408 &armv8_event_attr_br_mis_pred_retired.attr.attr,
409 &armv8_event_attr_stall_frontend.attr.attr,
410 &armv8_event_attr_stall_backend.attr.attr,
411 &armv8_event_attr_l1d_tlb.attr.attr,
412 &armv8_event_attr_l1i_tlb.attr.attr,
413 &armv8_event_attr_l2i_cache.attr.attr,
414 &armv8_event_attr_l2i_cache_refill.attr.attr,
415 &armv8_event_attr_l3d_cache_allocate.attr.attr,
416 &armv8_event_attr_l3d_cache_refill.attr.attr,
417 &armv8_event_attr_l3d_cache.attr.attr,
418 &armv8_event_attr_l3d_cache_wb.attr.attr,
419 &armv8_event_attr_l2d_tlb_refill.attr.attr,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700420 &armv8_event_attr_l2i_tlb_refill.attr.attr,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700421 &armv8_event_attr_l2d_tlb.attr.attr,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700422 &armv8_event_attr_l2i_tlb.attr.attr,
Will Deacon57d74122015-12-22 14:42:57 +0000423 NULL,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700424};
425
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700426static umode_t
427armv8pmu_event_attr_is_visible(struct kobject *kobj,
428 struct attribute *attr, int unused)
429{
430 struct device *dev = kobj_to_dev(kobj);
431 struct pmu *pmu = dev_get_drvdata(dev);
432 struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
433 struct perf_pmu_events_attr *pmu_attr;
434
435 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
436
437 if (test_bit(pmu_attr->id, cpu_pmu->pmceid_bitmap))
438 return attr->mode;
439
440 return 0;
441}
442
Drew Richardson9e9caa62015-10-22 07:07:32 -0700443static struct attribute_group armv8_pmuv3_events_attr_group = {
444 .name = "events",
445 .attrs = armv8_pmuv3_event_attrs,
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700446 .is_visible = armv8pmu_event_attr_is_visible,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700447};
448
Shaokun Zhangfe7296e2017-05-24 15:43:18 +0800449PMU_FORMAT_ATTR(event, "config:0-15");
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100450PMU_FORMAT_ATTR(long, "config1:0");
451
452static inline bool armv8pmu_event_is_64bit(struct perf_event *event)
453{
454 return event->attr.config1 & 0x1;
455}
Will Deacon57d74122015-12-22 14:42:57 +0000456
457static struct attribute *armv8_pmuv3_format_attrs[] = {
458 &format_attr_event.attr,
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100459 &format_attr_long.attr,
Will Deacon57d74122015-12-22 14:42:57 +0000460 NULL,
461};
462
463static struct attribute_group armv8_pmuv3_format_attr_group = {
464 .name = "format",
465 .attrs = armv8_pmuv3_format_attrs,
466};
467
Will Deacon03089682012-03-05 11:49:32 +0000468/*
469 * Perf Events' indices
470 */
471#define ARMV8_IDX_CYCLE_COUNTER 0
472#define ARMV8_IDX_COUNTER0 1
Mark Rutland6475b2d2015-10-02 10:55:03 +0100473#define ARMV8_IDX_COUNTER_LAST(cpu_pmu) \
474 (ARMV8_IDX_CYCLE_COUNTER + cpu_pmu->num_events - 1)
Will Deacon03089682012-03-05 11:49:32 +0000475
Will Deacon03089682012-03-05 11:49:32 +0000476/*
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100477 * We must chain two programmable counters for 64 bit events,
478 * except when we have allocated the 64bit cycle counter (for CPU
479 * cycles event). This must be called only when the event has
480 * a counter allocated.
481 */
482static inline bool armv8pmu_event_is_chained(struct perf_event *event)
483{
484 int idx = event->hw.idx;
485
486 return !WARN_ON(idx < 0) &&
487 armv8pmu_event_is_64bit(event) &&
488 (idx != ARMV8_IDX_CYCLE_COUNTER);
489}
490
491/*
Will Deacon03089682012-03-05 11:49:32 +0000492 * ARMv8 low level PMU access
493 */
494
495/*
496 * Perf Event to low level counters mapping
497 */
498#define ARMV8_IDX_TO_COUNTER(x) \
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000499 (((x) - ARMV8_IDX_COUNTER0) & ARMV8_PMU_COUNTER_MASK)
Will Deacon03089682012-03-05 11:49:32 +0000500
501static inline u32 armv8pmu_pmcr_read(void)
502{
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700503 return read_sysreg(pmcr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000504}
505
506static inline void armv8pmu_pmcr_write(u32 val)
507{
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000508 val &= ARMV8_PMU_PMCR_MASK;
Will Deacon03089682012-03-05 11:49:32 +0000509 isb();
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700510 write_sysreg(val, pmcr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000511}
512
513static inline int armv8pmu_has_overflowed(u32 pmovsr)
514{
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000515 return pmovsr & ARMV8_PMU_OVERFLOWED_MASK;
Will Deacon03089682012-03-05 11:49:32 +0000516}
517
Mark Rutland6475b2d2015-10-02 10:55:03 +0100518static inline int armv8pmu_counter_valid(struct arm_pmu *cpu_pmu, int idx)
Will Deacon03089682012-03-05 11:49:32 +0000519{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100520 return idx >= ARMV8_IDX_CYCLE_COUNTER &&
521 idx <= ARMV8_IDX_COUNTER_LAST(cpu_pmu);
Will Deacon03089682012-03-05 11:49:32 +0000522}
523
524static inline int armv8pmu_counter_has_overflowed(u32 pmnc, int idx)
525{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100526 return pmnc & BIT(ARMV8_IDX_TO_COUNTER(idx));
Will Deacon03089682012-03-05 11:49:32 +0000527}
528
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100529static inline void armv8pmu_select_counter(int idx)
Will Deacon03089682012-03-05 11:49:32 +0000530{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100531 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700532 write_sysreg(counter, pmselr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000533 isb();
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100534}
Will Deacon03089682012-03-05 11:49:32 +0000535
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100536static inline u32 armv8pmu_read_evcntr(int idx)
537{
538 armv8pmu_select_counter(idx);
539 return read_sysreg(pmxevcntr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000540}
541
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100542static inline u64 armv8pmu_read_hw_counter(struct perf_event *event)
543{
544 int idx = event->hw.idx;
545 u64 val = 0;
546
547 val = armv8pmu_read_evcntr(idx);
548 if (armv8pmu_event_is_chained(event))
549 val = (val << 32) | armv8pmu_read_evcntr(idx - 1);
550 return val;
551}
552
Suzuki K Poulose3a952002018-07-10 09:57:59 +0100553static inline u64 armv8pmu_read_counter(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000554{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100555 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
556 struct hw_perf_event *hwc = &event->hw;
557 int idx = hwc->idx;
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100558 u64 value = 0;
Will Deacon03089682012-03-05 11:49:32 +0000559
Mark Rutland6475b2d2015-10-02 10:55:03 +0100560 if (!armv8pmu_counter_valid(cpu_pmu, idx))
Will Deacon03089682012-03-05 11:49:32 +0000561 pr_err("CPU%u reading wrong counter %d\n",
562 smp_processor_id(), idx);
563 else if (idx == ARMV8_IDX_CYCLE_COUNTER)
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700564 value = read_sysreg(pmccntr_el0);
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100565 else
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100566 value = armv8pmu_read_hw_counter(event);
Will Deacon03089682012-03-05 11:49:32 +0000567
568 return value;
569}
570
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100571static inline void armv8pmu_write_evcntr(int idx, u32 value)
572{
573 armv8pmu_select_counter(idx);
574 write_sysreg(value, pmxevcntr_el0);
575}
576
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100577static inline void armv8pmu_write_hw_counter(struct perf_event *event,
578 u64 value)
579{
580 int idx = event->hw.idx;
581
582 if (armv8pmu_event_is_chained(event)) {
583 armv8pmu_write_evcntr(idx, upper_32_bits(value));
584 armv8pmu_write_evcntr(idx - 1, lower_32_bits(value));
585 } else {
586 armv8pmu_write_evcntr(idx, value);
587 }
588}
589
Suzuki K Poulose3a952002018-07-10 09:57:59 +0100590static inline void armv8pmu_write_counter(struct perf_event *event, u64 value)
Will Deacon03089682012-03-05 11:49:32 +0000591{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100592 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
593 struct hw_perf_event *hwc = &event->hw;
594 int idx = hwc->idx;
595
596 if (!armv8pmu_counter_valid(cpu_pmu, idx))
Will Deacon03089682012-03-05 11:49:32 +0000597 pr_err("CPU%u writing wrong counter %d\n",
598 smp_processor_id(), idx);
Jan Glauber7175f052016-02-18 17:50:13 +0100599 else if (idx == ARMV8_IDX_CYCLE_COUNTER) {
600 /*
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100601 * The cycles counter is really a 64-bit counter.
602 * When treating it as a 32-bit counter, we only count
603 * the lower 32 bits, and set the upper 32-bits so that
604 * we get an interrupt upon 32-bit overflow.
Jan Glauber7175f052016-02-18 17:50:13 +0100605 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100606 if (!armv8pmu_event_is_64bit(event))
607 value |= 0xffffffff00000000ULL;
Suzuki K Poulose3a952002018-07-10 09:57:59 +0100608 write_sysreg(value, pmccntr_el0);
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100609 } else
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100610 armv8pmu_write_hw_counter(event, value);
Will Deacon03089682012-03-05 11:49:32 +0000611}
612
613static inline void armv8pmu_write_evtype(int idx, u32 val)
614{
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100615 armv8pmu_select_counter(idx);
616 val &= ARMV8_PMU_EVTYPE_MASK;
617 write_sysreg(val, pmxevtyper_el0);
Will Deacon03089682012-03-05 11:49:32 +0000618}
619
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100620static inline void armv8pmu_write_event_type(struct perf_event *event)
621{
622 struct hw_perf_event *hwc = &event->hw;
623 int idx = hwc->idx;
624
625 /*
626 * For chained events, the low counter is programmed to count
627 * the event of interest and the high counter is programmed
628 * with CHAIN event code with filters set to count at all ELs.
629 */
630 if (armv8pmu_event_is_chained(event)) {
631 u32 chain_evt = ARMV8_PMUV3_PERFCTR_CHAIN |
632 ARMV8_PMU_INCLUDE_EL2;
633
634 armv8pmu_write_evtype(idx - 1, hwc->config_base);
635 armv8pmu_write_evtype(idx, chain_evt);
636 } else {
637 armv8pmu_write_evtype(idx, hwc->config_base);
638 }
639}
640
Will Deacon03089682012-03-05 11:49:32 +0000641static inline int armv8pmu_enable_counter(int idx)
642{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100643 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700644 write_sysreg(BIT(counter), pmcntenset_el0);
Will Deacon03089682012-03-05 11:49:32 +0000645 return idx;
646}
647
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100648static inline void armv8pmu_enable_event_counter(struct perf_event *event)
649{
650 int idx = event->hw.idx;
651
652 armv8pmu_enable_counter(idx);
653 if (armv8pmu_event_is_chained(event))
654 armv8pmu_enable_counter(idx - 1);
655 isb();
656}
657
Will Deacon03089682012-03-05 11:49:32 +0000658static inline int armv8pmu_disable_counter(int idx)
659{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100660 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700661 write_sysreg(BIT(counter), pmcntenclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000662 return idx;
663}
664
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100665static inline void armv8pmu_disable_event_counter(struct perf_event *event)
666{
667 struct hw_perf_event *hwc = &event->hw;
668 int idx = hwc->idx;
669
670 if (armv8pmu_event_is_chained(event))
671 armv8pmu_disable_counter(idx - 1);
672 armv8pmu_disable_counter(idx);
673}
674
Will Deacon03089682012-03-05 11:49:32 +0000675static inline int armv8pmu_enable_intens(int idx)
676{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100677 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700678 write_sysreg(BIT(counter), pmintenset_el1);
Will Deacon03089682012-03-05 11:49:32 +0000679 return idx;
680}
681
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100682static inline int armv8pmu_enable_event_irq(struct perf_event *event)
683{
684 return armv8pmu_enable_intens(event->hw.idx);
685}
686
Will Deacon03089682012-03-05 11:49:32 +0000687static inline int armv8pmu_disable_intens(int idx)
688{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100689 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700690 write_sysreg(BIT(counter), pmintenclr_el1);
Will Deacon03089682012-03-05 11:49:32 +0000691 isb();
692 /* Clear the overflow flag in case an interrupt is pending. */
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700693 write_sysreg(BIT(counter), pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000694 isb();
Mark Rutland6475b2d2015-10-02 10:55:03 +0100695
Will Deacon03089682012-03-05 11:49:32 +0000696 return idx;
697}
698
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100699static inline int armv8pmu_disable_event_irq(struct perf_event *event)
700{
701 return armv8pmu_disable_intens(event->hw.idx);
702}
703
Will Deacon03089682012-03-05 11:49:32 +0000704static inline u32 armv8pmu_getreset_flags(void)
705{
706 u32 value;
707
708 /* Read */
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700709 value = read_sysreg(pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000710
711 /* Write to clear flags */
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000712 value &= ARMV8_PMU_OVSR_MASK;
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700713 write_sysreg(value, pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000714
715 return value;
716}
717
Mark Rutland6475b2d2015-10-02 10:55:03 +0100718static void armv8pmu_enable_event(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000719{
720 unsigned long flags;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100721 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
722 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
Will Deacon03089682012-03-05 11:49:32 +0000723
724 /*
725 * Enable counter and interrupt, and set the counter to count
726 * the event that we're interested in.
727 */
728 raw_spin_lock_irqsave(&events->pmu_lock, flags);
729
730 /*
731 * Disable counter
732 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100733 armv8pmu_disable_event_counter(event);
Will Deacon03089682012-03-05 11:49:32 +0000734
735 /*
736 * Set event (if destined for PMNx counters).
737 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100738 armv8pmu_write_event_type(event);
Will Deacon03089682012-03-05 11:49:32 +0000739
740 /*
741 * Enable interrupt for this counter
742 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100743 armv8pmu_enable_event_irq(event);
Will Deacon03089682012-03-05 11:49:32 +0000744
745 /*
746 * Enable counter
747 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100748 armv8pmu_enable_event_counter(event);
Will Deacon03089682012-03-05 11:49:32 +0000749
750 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
751}
752
Mark Rutland6475b2d2015-10-02 10:55:03 +0100753static void armv8pmu_disable_event(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000754{
755 unsigned long flags;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100756 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
757 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
Will Deacon03089682012-03-05 11:49:32 +0000758
759 /*
760 * Disable counter and interrupt
761 */
762 raw_spin_lock_irqsave(&events->pmu_lock, flags);
763
764 /*
765 * Disable counter
766 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100767 armv8pmu_disable_event_counter(event);
Will Deacon03089682012-03-05 11:49:32 +0000768
769 /*
770 * Disable interrupt for this counter
771 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100772 armv8pmu_disable_event_irq(event);
Will Deacon03089682012-03-05 11:49:32 +0000773
774 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
775}
776
Suzuki K Poulose3cce50d2018-07-10 09:58:03 +0100777static void armv8pmu_start(struct arm_pmu *cpu_pmu)
778{
779 unsigned long flags;
780 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
781
782 raw_spin_lock_irqsave(&events->pmu_lock, flags);
783 /* Enable all counters */
784 armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMU_PMCR_E);
785 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
786}
787
788static void armv8pmu_stop(struct arm_pmu *cpu_pmu)
789{
790 unsigned long flags;
791 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
792
793 raw_spin_lock_irqsave(&events->pmu_lock, flags);
794 /* Disable all counters */
795 armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMU_PMCR_E);
796 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
797}
798
Mark Rutland0788f1e2018-05-10 11:35:15 +0100799static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +0000800{
801 u32 pmovsr;
802 struct perf_sample_data data;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100803 struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
Will Deacon03089682012-03-05 11:49:32 +0000804 struct pt_regs *regs;
805 int idx;
806
807 /*
808 * Get and reset the IRQ flags
809 */
810 pmovsr = armv8pmu_getreset_flags();
811
812 /*
813 * Did an overflow occur?
814 */
815 if (!armv8pmu_has_overflowed(pmovsr))
816 return IRQ_NONE;
817
818 /*
819 * Handle the counter(s) overflow(s)
820 */
821 regs = get_irq_regs();
822
Suzuki K Poulose3cce50d2018-07-10 09:58:03 +0100823 /*
824 * Stop the PMU while processing the counter overflows
825 * to prevent skews in group events.
826 */
827 armv8pmu_stop(cpu_pmu);
Will Deacon03089682012-03-05 11:49:32 +0000828 for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
829 struct perf_event *event = cpuc->events[idx];
830 struct hw_perf_event *hwc;
831
832 /* Ignore if we don't have an event. */
833 if (!event)
834 continue;
835
836 /*
837 * We have a single interrupt for all counters. Check that
838 * each counter has overflowed before we process it.
839 */
840 if (!armv8pmu_counter_has_overflowed(pmovsr, idx))
841 continue;
842
843 hwc = &event->hw;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100844 armpmu_event_update(event);
Will Deacon03089682012-03-05 11:49:32 +0000845 perf_sample_data_init(&data, 0, hwc->last_period);
Mark Rutland6475b2d2015-10-02 10:55:03 +0100846 if (!armpmu_event_set_period(event))
Will Deacon03089682012-03-05 11:49:32 +0000847 continue;
848
849 if (perf_event_overflow(event, &data, regs))
Mark Rutland6475b2d2015-10-02 10:55:03 +0100850 cpu_pmu->disable(event);
Will Deacon03089682012-03-05 11:49:32 +0000851 }
Suzuki K Poulose3cce50d2018-07-10 09:58:03 +0100852 armv8pmu_start(cpu_pmu);
Will Deacon03089682012-03-05 11:49:32 +0000853
854 /*
855 * Handle the pending perf events.
856 *
857 * Note: this call *must* be run with interrupts disabled. For
858 * platforms that can have the PMU interrupts raised as an NMI, this
859 * will not work.
860 */
861 irq_work_run();
862
863 return IRQ_HANDLED;
864}
865
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100866static int armv8pmu_get_single_idx(struct pmu_hw_events *cpuc,
867 struct arm_pmu *cpu_pmu)
868{
869 int idx;
870
871 for (idx = ARMV8_IDX_COUNTER0; idx < cpu_pmu->num_events; idx ++) {
872 if (!test_and_set_bit(idx, cpuc->used_mask))
873 return idx;
874 }
875 return -EAGAIN;
876}
877
878static int armv8pmu_get_chain_idx(struct pmu_hw_events *cpuc,
879 struct arm_pmu *cpu_pmu)
880{
881 int idx;
882
883 /*
884 * Chaining requires two consecutive event counters, where
885 * the lower idx must be even.
886 */
887 for (idx = ARMV8_IDX_COUNTER0 + 1; idx < cpu_pmu->num_events; idx += 2) {
888 if (!test_and_set_bit(idx, cpuc->used_mask)) {
889 /* Check if the preceding even counter is available */
890 if (!test_and_set_bit(idx - 1, cpuc->used_mask))
891 return idx;
892 /* Release the Odd counter */
893 clear_bit(idx, cpuc->used_mask);
894 }
895 }
896 return -EAGAIN;
897}
898
Will Deacon03089682012-03-05 11:49:32 +0000899static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
Mark Rutland6475b2d2015-10-02 10:55:03 +0100900 struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000901{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100902 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
903 struct hw_perf_event *hwc = &event->hw;
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000904 unsigned long evtype = hwc->config_base & ARMV8_PMU_EVTYPE_EVENT;
Will Deacon03089682012-03-05 11:49:32 +0000905
Pratyush Anand1031a152017-07-01 12:03:35 +0530906 /* Always prefer to place a cycle counter into the cycle counter. */
Ashok Kumar03598fd2016-04-21 05:58:41 -0700907 if (evtype == ARMV8_PMUV3_PERFCTR_CPU_CYCLES) {
Pratyush Anand1031a152017-07-01 12:03:35 +0530908 if (!test_and_set_bit(ARMV8_IDX_CYCLE_COUNTER, cpuc->used_mask))
909 return ARMV8_IDX_CYCLE_COUNTER;
Will Deacon03089682012-03-05 11:49:32 +0000910 }
911
912 /*
Pratyush Anand1031a152017-07-01 12:03:35 +0530913 * Otherwise use events counters
Will Deacon03089682012-03-05 11:49:32 +0000914 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100915 if (armv8pmu_event_is_64bit(event))
916 return armv8pmu_get_chain_idx(cpuc, cpu_pmu);
917 else
918 return armv8pmu_get_single_idx(cpuc, cpu_pmu);
Will Deacon03089682012-03-05 11:49:32 +0000919}
920
Suzuki K Poulose7dfc8db2018-07-10 09:58:01 +0100921static void armv8pmu_clear_event_idx(struct pmu_hw_events *cpuc,
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100922 struct perf_event *event)
Suzuki K Poulose7dfc8db2018-07-10 09:58:01 +0100923{
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100924 int idx = event->hw.idx;
925
926 clear_bit(idx, cpuc->used_mask);
927 if (armv8pmu_event_is_chained(event))
928 clear_bit(idx - 1, cpuc->used_mask);
Suzuki K Poulose7dfc8db2018-07-10 09:58:01 +0100929}
930
Will Deacon03089682012-03-05 11:49:32 +0000931/*
932 * Add an event filter to a given event. This will only work for PMUv2 PMUs.
933 */
934static int armv8pmu_set_event_filter(struct hw_perf_event *event,
935 struct perf_event_attr *attr)
936{
937 unsigned long config_base = 0;
938
939 if (attr->exclude_idle)
940 return -EPERM;
Ganapatrao Kulkarni78a19cf2017-05-02 21:59:34 +0530941
942 /*
943 * If we're running in hyp mode, then we *are* the hypervisor.
944 * Therefore we ignore exclude_hv in this configuration, since
945 * there's no hypervisor to sample anyway. This is consistent
946 * with other architectures (x86 and Power).
947 */
948 if (is_kernel_in_hyp_mode()) {
949 if (!attr->exclude_kernel)
950 config_base |= ARMV8_PMU_INCLUDE_EL2;
951 } else {
952 if (attr->exclude_kernel)
953 config_base |= ARMV8_PMU_EXCLUDE_EL1;
954 if (!attr->exclude_hv)
955 config_base |= ARMV8_PMU_INCLUDE_EL2;
956 }
Will Deacon03089682012-03-05 11:49:32 +0000957 if (attr->exclude_user)
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000958 config_base |= ARMV8_PMU_EXCLUDE_EL0;
Will Deacon03089682012-03-05 11:49:32 +0000959
960 /*
961 * Install the filter into config_base as this is used to
962 * construct the event type.
963 */
964 event->config_base = config_base;
965
966 return 0;
967}
968
Will Deaconca2b4972018-10-05 13:24:36 +0100969static int armv8pmu_filter_match(struct perf_event *event)
970{
971 unsigned long evtype = event->hw.config_base & ARMV8_PMU_EVTYPE_EVENT;
972 return evtype != ARMV8_PMUV3_PERFCTR_CHAIN;
973}
974
Will Deacon03089682012-03-05 11:49:32 +0000975static void armv8pmu_reset(void *info)
976{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100977 struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
Will Deacon03089682012-03-05 11:49:32 +0000978 u32 idx, nb_cnt = cpu_pmu->num_events;
979
980 /* The counter and interrupt enable registers are unknown at reset. */
Mark Rutland6475b2d2015-10-02 10:55:03 +0100981 for (idx = ARMV8_IDX_CYCLE_COUNTER; idx < nb_cnt; ++idx) {
982 armv8pmu_disable_counter(idx);
983 armv8pmu_disable_intens(idx);
984 }
Will Deacon03089682012-03-05 11:49:32 +0000985
Jan Glauber7175f052016-02-18 17:50:13 +0100986 /*
987 * Initialize & Reset PMNC. Request overflow interrupt for
988 * 64 bit cycle counter but cheat in armv8pmu_write_counter().
989 */
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000990 armv8pmu_pmcr_write(ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C |
991 ARMV8_PMU_PMCR_LC);
Will Deacon03089682012-03-05 11:49:32 +0000992}
993
Will Deacon6c833bb2017-08-08 16:58:33 +0100994static int __armv8_pmuv3_map_event(struct perf_event *event,
995 const unsigned (*extra_event_map)
996 [PERF_COUNT_HW_MAX],
997 const unsigned (*extra_cache_map)
998 [PERF_COUNT_HW_CACHE_MAX]
999 [PERF_COUNT_HW_CACHE_OP_MAX]
1000 [PERF_COUNT_HW_CACHE_RESULT_MAX])
Will Deacon03089682012-03-05 11:49:32 +00001001{
Jeremy Linton236b9b912016-09-14 17:32:30 -05001002 int hw_event_id;
1003 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
1004
1005 hw_event_id = armpmu_map_event(event, &armv8_pmuv3_perf_map,
1006 &armv8_pmuv3_perf_cache_map,
1007 ARMV8_PMU_EVTYPE_EVENT);
Jeremy Linton236b9b912016-09-14 17:32:30 -05001008
Suzuki K Poulosec1320792018-07-10 09:58:04 +01001009 if (armv8pmu_event_is_64bit(event))
1010 event->hw.flags |= ARMPMU_EVT_64BIT;
1011
Will Deacon6c833bb2017-08-08 16:58:33 +01001012 /* Onl expose micro/arch events supported by this PMU */
1013 if ((hw_event_id > 0) && (hw_event_id < ARMV8_PMUV3_MAX_COMMON_EVENTS)
1014 && test_bit(hw_event_id, armpmu->pmceid_bitmap)) {
1015 return hw_event_id;
Jeremy Linton236b9b912016-09-14 17:32:30 -05001016 }
1017
Will Deacon6c833bb2017-08-08 16:58:33 +01001018 return armpmu_map_event(event, extra_event_map, extra_cache_map,
1019 ARMV8_PMU_EVTYPE_EVENT);
1020}
1021
1022static int armv8_pmuv3_map_event(struct perf_event *event)
1023{
1024 return __armv8_pmuv3_map_event(event, NULL, NULL);
Will Deacon03089682012-03-05 11:49:32 +00001025}
1026
Mark Rutlandac82d122015-10-02 10:55:04 +01001027static int armv8_a53_map_event(struct perf_event *event)
1028{
Will Deacond0d09d42017-08-08 17:11:27 +01001029 return __armv8_pmuv3_map_event(event, NULL, &armv8_a53_perf_cache_map);
Mark Rutlandac82d122015-10-02 10:55:04 +01001030}
1031
Mark Rutland62a4dda2015-10-02 10:55:05 +01001032static int armv8_a57_map_event(struct perf_event *event)
1033{
Will Deacond0d09d42017-08-08 17:11:27 +01001034 return __armv8_pmuv3_map_event(event, NULL, &armv8_a57_perf_cache_map);
Mark Rutland62a4dda2015-10-02 10:55:05 +01001035}
1036
Julien Thierry5561b6c2017-08-09 17:46:38 +01001037static int armv8_a73_map_event(struct perf_event *event)
1038{
1039 return __armv8_pmuv3_map_event(event, NULL, &armv8_a73_perf_cache_map);
1040}
1041
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001042static int armv8_thunder_map_event(struct perf_event *event)
1043{
Will Deacond0d09d42017-08-08 17:11:27 +01001044 return __armv8_pmuv3_map_event(event, NULL,
Will Deacon6c833bb2017-08-08 16:58:33 +01001045 &armv8_thunder_perf_cache_map);
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001046}
1047
Ashok Kumar201a72b2016-04-21 05:58:45 -07001048static int armv8_vulcan_map_event(struct perf_event *event)
1049{
Will Deacond0d09d42017-08-08 17:11:27 +01001050 return __armv8_pmuv3_map_event(event, NULL,
Will Deacon6c833bb2017-08-08 16:58:33 +01001051 &armv8_vulcan_perf_cache_map);
Ashok Kumar201a72b2016-04-21 05:58:45 -07001052}
1053
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001054struct armv8pmu_probe_info {
1055 struct arm_pmu *pmu;
1056 bool present;
1057};
1058
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001059static void __armv8pmu_probe_pmu(void *info)
Will Deacon03089682012-03-05 11:49:32 +00001060{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001061 struct armv8pmu_probe_info *probe = info;
1062 struct arm_pmu *cpu_pmu = probe->pmu;
Mark Rutlandfaa9a082017-04-25 12:08:50 +01001063 u64 dfr0;
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001064 u32 pmceid[2];
Mark Rutlandfaa9a082017-04-25 12:08:50 +01001065 int pmuver;
Will Deacon03089682012-03-05 11:49:32 +00001066
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001067 dfr0 = read_sysreg(id_aa64dfr0_el1);
Mark Rutland03313652018-02-14 17:21:57 +00001068 pmuver = cpuid_feature_extract_unsigned_field(dfr0,
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001069 ID_AA64DFR0_PMUVER_SHIFT);
Mark Rutland03313652018-02-14 17:21:57 +00001070 if (pmuver == 0xf || pmuver == 0)
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001071 return;
1072
1073 probe->present = true;
1074
Will Deacon03089682012-03-05 11:49:32 +00001075 /* Read the nb of CNTx counters supported from PMNC */
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001076 cpu_pmu->num_events = (armv8pmu_pmcr_read() >> ARMV8_PMU_PMCR_N_SHIFT)
1077 & ARMV8_PMU_PMCR_N_MASK;
Will Deacon03089682012-03-05 11:49:32 +00001078
Mark Rutland6475b2d2015-10-02 10:55:03 +01001079 /* Add the CPU cycles counter */
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001080 cpu_pmu->num_events += 1;
1081
1082 pmceid[0] = read_sysreg(pmceid0_el0);
1083 pmceid[1] = read_sysreg(pmceid1_el0);
1084
Yury Norov3aa56882018-02-06 15:38:06 -08001085 bitmap_from_arr32(cpu_pmu->pmceid_bitmap,
1086 pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
Will Deacon03089682012-03-05 11:49:32 +00001087}
1088
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001089static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +00001090{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001091 struct armv8pmu_probe_info probe = {
1092 .pmu = cpu_pmu,
1093 .present = false,
1094 };
1095 int ret;
1096
1097 ret = smp_call_function_any(&cpu_pmu->supported_cpus,
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001098 __armv8pmu_probe_pmu,
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001099 &probe, 1);
1100 if (ret)
1101 return ret;
1102
1103 return probe.present ? 0 : -ENODEV;
Will Deacon03089682012-03-05 11:49:32 +00001104}
1105
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001106static int armv8_pmu_init(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +00001107{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001108 int ret = armv8pmu_probe_pmu(cpu_pmu);
1109 if (ret)
1110 return ret;
1111
Will Deacond3adeed2018-10-05 13:26:21 +01001112 cpu_pmu->handle_irq = armv8pmu_handle_irq;
1113 cpu_pmu->enable = armv8pmu_enable_event;
1114 cpu_pmu->disable = armv8pmu_disable_event;
1115 cpu_pmu->read_counter = armv8pmu_read_counter;
1116 cpu_pmu->write_counter = armv8pmu_write_counter;
1117 cpu_pmu->get_event_idx = armv8pmu_get_event_idx;
1118 cpu_pmu->clear_event_idx = armv8pmu_clear_event_idx;
1119 cpu_pmu->start = armv8pmu_start;
1120 cpu_pmu->stop = armv8pmu_stop;
1121 cpu_pmu->reset = armv8pmu_reset;
Mark Rutlandac82d122015-10-02 10:55:04 +01001122 cpu_pmu->set_event_filter = armv8pmu_set_event_filter;
Will Deaconca2b4972018-10-05 13:24:36 +01001123 cpu_pmu->filter_match = armv8pmu_filter_match;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001124
1125 return 0;
Mark Rutlandac82d122015-10-02 10:55:04 +01001126}
1127
1128static int armv8_pmuv3_init(struct arm_pmu *cpu_pmu)
1129{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001130 int ret = armv8_pmu_init(cpu_pmu);
1131 if (ret)
1132 return ret;
1133
Mark Rutland6475b2d2015-10-02 10:55:03 +01001134 cpu_pmu->name = "armv8_pmuv3";
1135 cpu_pmu->map_event = armv8_pmuv3_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001136 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1137 &armv8_pmuv3_events_attr_group;
1138 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1139 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001140
1141 return 0;
Mark Rutlandac82d122015-10-02 10:55:04 +01001142}
1143
Julien Thierrye884f802017-08-09 17:46:39 +01001144static int armv8_a35_pmu_init(struct arm_pmu *cpu_pmu)
1145{
1146 int ret = armv8_pmu_init(cpu_pmu);
1147 if (ret)
1148 return ret;
1149
1150 cpu_pmu->name = "armv8_cortex_a35";
1151 cpu_pmu->map_event = armv8_a53_map_event;
1152 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1153 &armv8_pmuv3_events_attr_group;
1154 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1155 &armv8_pmuv3_format_attr_group;
1156
1157 return 0;
1158}
1159
Mark Rutlandac82d122015-10-02 10:55:04 +01001160static int armv8_a53_pmu_init(struct arm_pmu *cpu_pmu)
1161{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001162 int ret = armv8_pmu_init(cpu_pmu);
1163 if (ret)
1164 return ret;
1165
Mark Rutlandac82d122015-10-02 10:55:04 +01001166 cpu_pmu->name = "armv8_cortex_a53";
1167 cpu_pmu->map_event = armv8_a53_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001168 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1169 &armv8_pmuv3_events_attr_group;
1170 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1171 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001172
1173 return 0;
Will Deacon03089682012-03-05 11:49:32 +00001174}
Will Deacon03089682012-03-05 11:49:32 +00001175
Mark Rutland62a4dda2015-10-02 10:55:05 +01001176static int armv8_a57_pmu_init(struct arm_pmu *cpu_pmu)
1177{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001178 int ret = armv8_pmu_init(cpu_pmu);
1179 if (ret)
1180 return ret;
1181
Mark Rutland62a4dda2015-10-02 10:55:05 +01001182 cpu_pmu->name = "armv8_cortex_a57";
1183 cpu_pmu->map_event = armv8_a57_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001184 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1185 &armv8_pmuv3_events_attr_group;
1186 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1187 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001188
1189 return 0;
Mark Rutland62a4dda2015-10-02 10:55:05 +01001190}
1191
Will Deacon5d7ee872015-12-22 14:45:35 +00001192static int armv8_a72_pmu_init(struct arm_pmu *cpu_pmu)
1193{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001194 int ret = armv8_pmu_init(cpu_pmu);
1195 if (ret)
1196 return ret;
1197
Will Deacon5d7ee872015-12-22 14:45:35 +00001198 cpu_pmu->name = "armv8_cortex_a72";
1199 cpu_pmu->map_event = armv8_a57_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001200 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1201 &armv8_pmuv3_events_attr_group;
1202 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1203 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001204
1205 return 0;
Will Deacon5d7ee872015-12-22 14:45:35 +00001206}
1207
Julien Thierry5561b6c2017-08-09 17:46:38 +01001208static int armv8_a73_pmu_init(struct arm_pmu *cpu_pmu)
1209{
1210 int ret = armv8_pmu_init(cpu_pmu);
1211 if (ret)
1212 return ret;
1213
1214 cpu_pmu->name = "armv8_cortex_a73";
1215 cpu_pmu->map_event = armv8_a73_map_event;
1216 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1217 &armv8_pmuv3_events_attr_group;
1218 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1219 &armv8_pmuv3_format_attr_group;
1220
1221 return 0;
1222}
1223
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001224static int armv8_thunder_pmu_init(struct arm_pmu *cpu_pmu)
1225{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001226 int ret = armv8_pmu_init(cpu_pmu);
1227 if (ret)
1228 return ret;
1229
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001230 cpu_pmu->name = "armv8_cavium_thunder";
1231 cpu_pmu->map_event = armv8_thunder_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001232 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1233 &armv8_pmuv3_events_attr_group;
1234 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1235 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001236
1237 return 0;
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001238}
1239
Ashok Kumar201a72b2016-04-21 05:58:45 -07001240static int armv8_vulcan_pmu_init(struct arm_pmu *cpu_pmu)
1241{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001242 int ret = armv8_pmu_init(cpu_pmu);
1243 if (ret)
1244 return ret;
1245
Ashok Kumar201a72b2016-04-21 05:58:45 -07001246 cpu_pmu->name = "armv8_brcm_vulcan";
1247 cpu_pmu->map_event = armv8_vulcan_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001248 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1249 &armv8_pmuv3_events_attr_group;
1250 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1251 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001252
1253 return 0;
Ashok Kumar201a72b2016-04-21 05:58:45 -07001254}
1255
Mark Rutland6475b2d2015-10-02 10:55:03 +01001256static const struct of_device_id armv8_pmu_of_device_ids[] = {
1257 {.compatible = "arm,armv8-pmuv3", .data = armv8_pmuv3_init},
Julien Thierrye884f802017-08-09 17:46:39 +01001258 {.compatible = "arm,cortex-a35-pmu", .data = armv8_a35_pmu_init},
Mark Rutlandac82d122015-10-02 10:55:04 +01001259 {.compatible = "arm,cortex-a53-pmu", .data = armv8_a53_pmu_init},
Mark Rutland62a4dda2015-10-02 10:55:05 +01001260 {.compatible = "arm,cortex-a57-pmu", .data = armv8_a57_pmu_init},
Will Deacon5d7ee872015-12-22 14:45:35 +00001261 {.compatible = "arm,cortex-a72-pmu", .data = armv8_a72_pmu_init},
Julien Thierry5561b6c2017-08-09 17:46:38 +01001262 {.compatible = "arm,cortex-a73-pmu", .data = armv8_a73_pmu_init},
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001263 {.compatible = "cavium,thunder-pmu", .data = armv8_thunder_pmu_init},
Ashok Kumar201a72b2016-04-21 05:58:45 -07001264 {.compatible = "brcm,vulcan-pmu", .data = armv8_vulcan_pmu_init},
Will Deacon03089682012-03-05 11:49:32 +00001265 {},
1266};
1267
Mark Rutland6475b2d2015-10-02 10:55:03 +01001268static int armv8_pmu_device_probe(struct platform_device *pdev)
Will Deacon03089682012-03-05 11:49:32 +00001269{
Mark Rutlandf00fa5f2017-04-11 09:39:57 +01001270 return arm_pmu_device_probe(pdev, armv8_pmu_of_device_ids, NULL);
Will Deacon03089682012-03-05 11:49:32 +00001271}
1272
Mark Rutland6475b2d2015-10-02 10:55:03 +01001273static struct platform_driver armv8_pmu_driver = {
Will Deacon03089682012-03-05 11:49:32 +00001274 .driver = {
Jeremy Linton85023b22016-09-14 17:32:31 -05001275 .name = ARMV8_PMU_PDEV_NAME,
Mark Rutland6475b2d2015-10-02 10:55:03 +01001276 .of_match_table = armv8_pmu_of_device_ids,
Will Deacon03089682012-03-05 11:49:32 +00001277 },
Mark Rutland6475b2d2015-10-02 10:55:03 +01001278 .probe = armv8_pmu_device_probe,
Will Deacon03089682012-03-05 11:49:32 +00001279};
1280
Mark Rutlandf00fa5f2017-04-11 09:39:57 +01001281static int __init armv8_pmu_driver_init(void)
1282{
1283 if (acpi_disabled)
1284 return platform_driver_register(&armv8_pmu_driver);
1285 else
1286 return arm_pmu_acpi_probe(armv8_pmuv3_init);
1287}
1288device_initcall(armv8_pmu_driver_init)
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -07001289
1290void arch_perf_update_userpage(struct perf_event *event,
1291 struct perf_event_mmap_page *userpg, u64 now)
1292{
1293 u32 freq;
1294 u32 shift;
1295
1296 /*
1297 * Internal timekeeping for enabled/running/stopped times
1298 * is always computed with the sched_clock.
1299 */
1300 freq = arch_timer_get_rate();
1301 userpg->cap_user_time = 1;
1302
1303 clocks_calc_mult_shift(&userpg->time_mult, &shift, freq,
1304 NSEC_PER_SEC, 0);
1305 /*
1306 * time_shift is not expected to be greater than 31 due to
1307 * the original published conversion algorithm shifting a
1308 * 32-bit value (now specifies a 64-bit value) - refer
1309 * perf_event_mmap_page documentation in perf_event.h.
1310 */
1311 if (shift == 32) {
1312 shift = 31;
1313 userpg->time_mult >>= 1;
1314 }
1315 userpg->time_shift = (u16)shift;
1316 userpg->time_offset = -now;
1317}