blob: bc014af59fb39090397dfaaca6213d36aada7144 [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>
Mark Rutland6475b2d2015-10-02 10:55:03 +010028#include <linux/of.h>
29#include <linux/perf/arm_pmu.h>
30#include <linux/platform_device.h>
Will Deacon03089682012-03-05 11:49:32 +000031
32/*
33 * ARMv8 PMUv3 Performance Events handling code.
Wei Huangb112c842016-11-16 11:09:20 -060034 * Common event types (some are defined in asm/perf_event.h).
Will Deacon03089682012-03-05 11:49:32 +000035 */
Will Deacon03089682012-03-05 11:49:32 +000036
Drew Richardson90381cb2015-10-22 07:07:01 -070037/* At least one of the following is required. */
Ashok Kumar03598fd2016-04-21 05:58:41 -070038#define ARMV8_PMUV3_PERFCTR_INST_RETIRED 0x08
39#define ARMV8_PMUV3_PERFCTR_INST_SPEC 0x1B
Will Deacon03089682012-03-05 11:49:32 +000040
Drew Richardson90381cb2015-10-22 07:07:01 -070041/* Common architectural events. */
Ashok Kumar03598fd2016-04-21 05:58:41 -070042#define ARMV8_PMUV3_PERFCTR_LD_RETIRED 0x06
43#define ARMV8_PMUV3_PERFCTR_ST_RETIRED 0x07
Drew Richardson90381cb2015-10-22 07:07:01 -070044#define ARMV8_PMUV3_PERFCTR_EXC_TAKEN 0x09
Ashok Kumar03598fd2016-04-21 05:58:41 -070045#define ARMV8_PMUV3_PERFCTR_EXC_RETURN 0x0A
46#define ARMV8_PMUV3_PERFCTR_CID_WRITE_RETIRED 0x0B
47#define ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED 0x0C
48#define ARMV8_PMUV3_PERFCTR_BR_IMMED_RETIRED 0x0D
49#define ARMV8_PMUV3_PERFCTR_BR_RETURN_RETIRED 0x0E
50#define ARMV8_PMUV3_PERFCTR_UNALIGNED_LDST_RETIRED 0x0F
51#define ARMV8_PMUV3_PERFCTR_TTBR_WRITE_RETIRED 0x1C
Drew Richardson9e9caa62015-10-22 07:07:32 -070052#define ARMV8_PMUV3_PERFCTR_CHAIN 0x1E
53#define ARMV8_PMUV3_PERFCTR_BR_RETIRED 0x21
Drew Richardson90381cb2015-10-22 07:07:01 -070054
55/* Common microarchitectural events. */
Ashok Kumar03598fd2016-04-21 05:58:41 -070056#define ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL 0x01
57#define ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL 0x02
58#define ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL 0x05
Drew Richardson90381cb2015-10-22 07:07:01 -070059#define ARMV8_PMUV3_PERFCTR_MEM_ACCESS 0x13
Ashok Kumar03598fd2016-04-21 05:58:41 -070060#define ARMV8_PMUV3_PERFCTR_L1I_CACHE 0x14
61#define ARMV8_PMUV3_PERFCTR_L1D_CACHE_WB 0x15
62#define ARMV8_PMUV3_PERFCTR_L2D_CACHE 0x16
63#define ARMV8_PMUV3_PERFCTR_L2D_CACHE_REFILL 0x17
64#define ARMV8_PMUV3_PERFCTR_L2D_CACHE_WB 0x18
Drew Richardson90381cb2015-10-22 07:07:01 -070065#define ARMV8_PMUV3_PERFCTR_BUS_ACCESS 0x19
Ashok Kumar03598fd2016-04-21 05:58:41 -070066#define ARMV8_PMUV3_PERFCTR_MEMORY_ERROR 0x1A
Drew Richardson90381cb2015-10-22 07:07:01 -070067#define ARMV8_PMUV3_PERFCTR_BUS_CYCLES 0x1D
Drew Richardson9e9caa62015-10-22 07:07:32 -070068#define ARMV8_PMUV3_PERFCTR_L1D_CACHE_ALLOCATE 0x1F
69#define ARMV8_PMUV3_PERFCTR_L2D_CACHE_ALLOCATE 0x20
70#define ARMV8_PMUV3_PERFCTR_BR_MIS_PRED_RETIRED 0x22
71#define ARMV8_PMUV3_PERFCTR_STALL_FRONTEND 0x23
72#define ARMV8_PMUV3_PERFCTR_STALL_BACKEND 0x24
73#define ARMV8_PMUV3_PERFCTR_L1D_TLB 0x25
74#define ARMV8_PMUV3_PERFCTR_L1I_TLB 0x26
75#define ARMV8_PMUV3_PERFCTR_L2I_CACHE 0x27
76#define ARMV8_PMUV3_PERFCTR_L2I_CACHE_REFILL 0x28
77#define ARMV8_PMUV3_PERFCTR_L3D_CACHE_ALLOCATE 0x29
78#define ARMV8_PMUV3_PERFCTR_L3D_CACHE_REFILL 0x2A
79#define ARMV8_PMUV3_PERFCTR_L3D_CACHE 0x2B
80#define ARMV8_PMUV3_PERFCTR_L3D_CACHE_WB 0x2C
81#define ARMV8_PMUV3_PERFCTR_L2D_TLB_REFILL 0x2D
Ashok Kumar03598fd2016-04-21 05:58:41 -070082#define ARMV8_PMUV3_PERFCTR_L2I_TLB_REFILL 0x2E
Drew Richardson9e9caa62015-10-22 07:07:32 -070083#define ARMV8_PMUV3_PERFCTR_L2D_TLB 0x2F
Ashok Kumar03598fd2016-04-21 05:58:41 -070084#define ARMV8_PMUV3_PERFCTR_L2I_TLB 0x30
Will Deacon03089682012-03-05 11:49:32 +000085
Ashok Kumar03598fd2016-04-21 05:58:41 -070086/* ARMv8 recommended implementation defined event types */
87#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD 0x40
88#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR 0x41
89#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD 0x42
90#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR 0x43
Ashok Kumar0893f742016-04-21 05:58:42 -070091#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_INNER 0x44
92#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_OUTER 0x45
93#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WB_VICTIM 0x46
94#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WB_CLEAN 0x47
95#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_INVAL 0x48
96
Ashok Kumar03598fd2016-04-21 05:58:41 -070097#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD 0x4C
98#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR 0x4D
99#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD 0x4E
100#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR 0x4F
Ashok Kumar0893f742016-04-21 05:58:42 -0700101#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_RD 0x50
102#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_WR 0x51
103#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_REFILL_RD 0x52
104#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_REFILL_WR 0x53
105
106#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_WB_VICTIM 0x56
107#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_WB_CLEAN 0x57
108#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_INVAL 0x58
109
110#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_REFILL_RD 0x5C
111#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_REFILL_WR 0x5D
112#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_RD 0x5E
113#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_WR 0x5F
114
115#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD 0x60
116#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR 0x61
117#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_SHARED 0x62
118#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_NOT_SHARED 0x63
119#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_NORMAL 0x64
120#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_PERIPH 0x65
121
122#define ARMV8_IMPDEF_PERFCTR_MEM_ACCESS_RD 0x66
123#define ARMV8_IMPDEF_PERFCTR_MEM_ACCESS_WR 0x67
124#define ARMV8_IMPDEF_PERFCTR_UNALIGNED_LD_SPEC 0x68
125#define ARMV8_IMPDEF_PERFCTR_UNALIGNED_ST_SPEC 0x69
126#define ARMV8_IMPDEF_PERFCTR_UNALIGNED_LDST_SPEC 0x6A
127
128#define ARMV8_IMPDEF_PERFCTR_LDREX_SPEC 0x6C
129#define ARMV8_IMPDEF_PERFCTR_STREX_PASS_SPEC 0x6D
130#define ARMV8_IMPDEF_PERFCTR_STREX_FAIL_SPEC 0x6E
131#define ARMV8_IMPDEF_PERFCTR_STREX_SPEC 0x6F
132#define ARMV8_IMPDEF_PERFCTR_LD_SPEC 0x70
133#define ARMV8_IMPDEF_PERFCTR_ST_SPEC 0x71
134#define ARMV8_IMPDEF_PERFCTR_LDST_SPEC 0x72
135#define ARMV8_IMPDEF_PERFCTR_DP_SPEC 0x73
136#define ARMV8_IMPDEF_PERFCTR_ASE_SPEC 0x74
137#define ARMV8_IMPDEF_PERFCTR_VFP_SPEC 0x75
138#define ARMV8_IMPDEF_PERFCTR_PC_WRITE_SPEC 0x76
139#define ARMV8_IMPDEF_PERFCTR_CRYPTO_SPEC 0x77
140#define ARMV8_IMPDEF_PERFCTR_BR_IMMED_SPEC 0x78
141#define ARMV8_IMPDEF_PERFCTR_BR_RETURN_SPEC 0x79
142#define ARMV8_IMPDEF_PERFCTR_BR_INDIRECT_SPEC 0x7A
143
144#define ARMV8_IMPDEF_PERFCTR_ISB_SPEC 0x7C
145#define ARMV8_IMPDEF_PERFCTR_DSB_SPEC 0x7D
146#define ARMV8_IMPDEF_PERFCTR_DMB_SPEC 0x7E
147
148#define ARMV8_IMPDEF_PERFCTR_EXC_UNDEF 0x81
149#define ARMV8_IMPDEF_PERFCTR_EXC_SVC 0x82
150#define ARMV8_IMPDEF_PERFCTR_EXC_PABORT 0x83
151#define ARMV8_IMPDEF_PERFCTR_EXC_DABORT 0x84
152
153#define ARMV8_IMPDEF_PERFCTR_EXC_IRQ 0x86
154#define ARMV8_IMPDEF_PERFCTR_EXC_FIQ 0x87
155#define ARMV8_IMPDEF_PERFCTR_EXC_SMC 0x88
156
157#define ARMV8_IMPDEF_PERFCTR_EXC_HVC 0x8A
158#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_PABORT 0x8B
159#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_DABORT 0x8C
160#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_OTHER 0x8D
161#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_IRQ 0x8E
162#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_FIQ 0x8F
163#define ARMV8_IMPDEF_PERFCTR_RC_LD_SPEC 0x90
164#define ARMV8_IMPDEF_PERFCTR_RC_ST_SPEC 0x91
165
166#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_RD 0xA0
167#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_WR 0xA1
168#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_REFILL_RD 0xA2
169#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_REFILL_WR 0xA3
170
171#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_WB_VICTIM 0xA6
172#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_WB_CLEAN 0xA7
173#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_INVAL 0xA8
Jan Glauber5f140cc2016-02-18 17:50:10 +0100174
Mark Rutlandac82d122015-10-02 10:55:04 +0100175/* ARMv8 Cortex-A53 specific event types. */
Ashok Kumar03598fd2016-04-21 05:58:41 -0700176#define ARMV8_A53_PERFCTR_PREF_LINEFILL 0xC2
Mark Rutlandac82d122015-10-02 10:55:04 +0100177
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100178/* ARMv8 Cavium ThunderX specific event types. */
Ashok Kumar03598fd2016-04-21 05:58:41 -0700179#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST 0xE9
180#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS 0xEA
181#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS 0xEB
182#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS 0xEC
183#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS 0xED
Mark Rutland62a4dda2015-10-02 10:55:05 +0100184
Will Deacon03089682012-03-05 11:49:32 +0000185/* PMUv3 HW events mapping. */
Jeremy Linton236b9b912016-09-14 17:32:30 -0500186
187/*
188 * ARMv8 Architectural defined events, not all of these may
189 * be supported on any given implementation. Undefined events will
190 * be disabled at run-time.
191 */
Will Deacon03089682012-03-05 11:49:32 +0000192static const unsigned armv8_pmuv3_perf_map[PERF_COUNT_HW_MAX] = {
Mark Rutlandae2fb7e2015-07-21 11:36:39 +0100193 PERF_MAP_ALL_UNSUPPORTED,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700194 [PERF_COUNT_HW_CPU_CYCLES] = ARMV8_PMUV3_PERFCTR_CPU_CYCLES,
195 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_INST_RETIRED,
196 [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
197 [PERF_COUNT_HW_CACHE_MISSES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
Jeremy Linton236b9b912016-09-14 17:32:30 -0500198 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700199 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Jeremy Linton236b9b912016-09-14 17:32:30 -0500200 [PERF_COUNT_HW_BUS_CYCLES] = ARMV8_PMUV3_PERFCTR_BUS_CYCLES,
201 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV8_PMUV3_PERFCTR_STALL_FRONTEND,
202 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV8_PMUV3_PERFCTR_STALL_BACKEND,
Will Deacon03089682012-03-05 11:49:32 +0000203};
204
205static const unsigned armv8_pmuv3_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
206 [PERF_COUNT_HW_CACHE_OP_MAX]
207 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
Mark Rutlandae2fb7e2015-07-21 11:36:39 +0100208 PERF_CACHE_MAP_ALL_UNSUPPORTED,
209
Ashok Kumar03598fd2016-04-21 05:58:41 -0700210 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
211 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
212 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
213 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
Mark Rutlandae2fb7e2015-07-21 11:36:39 +0100214
Jeremy Linton236b9b912016-09-14 17:32:30 -0500215 [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE,
216 [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL,
217
218 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL,
219 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_TLB,
220
221 [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL,
222 [C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB,
223
Ashok Kumar03598fd2016-04-21 05:58:41 -0700224 [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
225 [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
226 [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
227 [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Will Deacon03089682012-03-05 11:49:32 +0000228};
229
Mark Rutlandac82d122015-10-02 10:55:04 +0100230static const unsigned armv8_a53_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
231 [PERF_COUNT_HW_CACHE_OP_MAX]
232 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
233 PERF_CACHE_MAP_ALL_UNSUPPORTED,
234
Ashok Kumar03598fd2016-04-21 05:58:41 -0700235 [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_A53_PERFCTR_PREF_LINEFILL,
Mark Rutlandac82d122015-10-02 10:55:04 +0100236
Julien Thierry5cf7fb22017-07-25 17:27:36 +0100237 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
238 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
Mark Rutlandac82d122015-10-02 10:55:04 +0100239};
240
Mark Rutland62a4dda2015-10-02 10:55:05 +0100241static const unsigned armv8_a57_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
242 [PERF_COUNT_HW_CACHE_OP_MAX]
243 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
244 PERF_CACHE_MAP_ALL_UNSUPPORTED,
245
Ashok Kumar03598fd2016-04-21 05:58:41 -0700246 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
247 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
248 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
249 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100250
Ashok Kumar03598fd2016-04-21 05:58:41 -0700251 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
252 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100253
Julien Thierry5cf7fb22017-07-25 17:27:36 +0100254 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
255 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100256};
257
Julien Thierry5561b6c2017-08-09 17:46:38 +0100258static const unsigned armv8_a73_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
259 [PERF_COUNT_HW_CACHE_OP_MAX]
260 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
261 PERF_CACHE_MAP_ALL_UNSUPPORTED,
262
263 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
264 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
Julien Thierry5561b6c2017-08-09 17:46:38 +0100265};
266
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100267static const unsigned armv8_thunder_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
268 [PERF_COUNT_HW_CACHE_OP_MAX]
269 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
270 PERF_CACHE_MAP_ALL_UNSUPPORTED,
271
Ashok Kumar03598fd2016-04-21 05:58:41 -0700272 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
273 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
274 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
275 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST,
276 [C(L1D)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS,
277 [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS,
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100278
Ashok Kumar03598fd2016-04-21 05:58:41 -0700279 [C(L1I)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS,
280 [C(L1I)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS,
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100281
Ashok Kumar03598fd2016-04-21 05:58:41 -0700282 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
283 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
284 [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
285 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100286};
287
Ashok Kumar201a72b2016-04-21 05:58:45 -0700288static const unsigned armv8_vulcan_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
289 [PERF_COUNT_HW_CACHE_OP_MAX]
290 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
291 PERF_CACHE_MAP_ALL_UNSUPPORTED,
292
293 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
294 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
295 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
296 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR,
297
Ashok Kumar201a72b2016-04-21 05:58:45 -0700298 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
299 [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
300 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
301 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
302
Ashok Kumar201a72b2016-04-21 05:58:45 -0700303 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
304 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
305};
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700306
307static ssize_t
308armv8pmu_events_sysfs_show(struct device *dev,
309 struct device_attribute *attr, char *page)
310{
311 struct perf_pmu_events_attr *pmu_attr;
312
313 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
314
315 return sprintf(page, "event=0x%03llx\n", pmu_attr->id);
316}
317
Drew Richardson9e9caa62015-10-22 07:07:32 -0700318#define ARMV8_EVENT_ATTR_RESOLVE(m) #m
319#define ARMV8_EVENT_ATTR(name, config) \
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700320 PMU_EVENT_ATTR(name, armv8_event_attr_##name, \
321 config, armv8pmu_events_sysfs_show)
Drew Richardson9e9caa62015-10-22 07:07:32 -0700322
Ashok Kumar03598fd2016-04-21 05:58:41 -0700323ARMV8_EVENT_ATTR(sw_incr, ARMV8_PMUV3_PERFCTR_SW_INCR);
324ARMV8_EVENT_ATTR(l1i_cache_refill, ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL);
325ARMV8_EVENT_ATTR(l1i_tlb_refill, ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL);
326ARMV8_EVENT_ATTR(l1d_cache_refill, ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL);
327ARMV8_EVENT_ATTR(l1d_cache, ARMV8_PMUV3_PERFCTR_L1D_CACHE);
328ARMV8_EVENT_ATTR(l1d_tlb_refill, ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL);
329ARMV8_EVENT_ATTR(ld_retired, ARMV8_PMUV3_PERFCTR_LD_RETIRED);
330ARMV8_EVENT_ATTR(st_retired, ARMV8_PMUV3_PERFCTR_ST_RETIRED);
331ARMV8_EVENT_ATTR(inst_retired, ARMV8_PMUV3_PERFCTR_INST_RETIRED);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700332ARMV8_EVENT_ATTR(exc_taken, ARMV8_PMUV3_PERFCTR_EXC_TAKEN);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700333ARMV8_EVENT_ATTR(exc_return, ARMV8_PMUV3_PERFCTR_EXC_RETURN);
334ARMV8_EVENT_ATTR(cid_write_retired, ARMV8_PMUV3_PERFCTR_CID_WRITE_RETIRED);
335ARMV8_EVENT_ATTR(pc_write_retired, ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED);
336ARMV8_EVENT_ATTR(br_immed_retired, ARMV8_PMUV3_PERFCTR_BR_IMMED_RETIRED);
337ARMV8_EVENT_ATTR(br_return_retired, ARMV8_PMUV3_PERFCTR_BR_RETURN_RETIRED);
338ARMV8_EVENT_ATTR(unaligned_ldst_retired, ARMV8_PMUV3_PERFCTR_UNALIGNED_LDST_RETIRED);
339ARMV8_EVENT_ATTR(br_mis_pred, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED);
340ARMV8_EVENT_ATTR(cpu_cycles, ARMV8_PMUV3_PERFCTR_CPU_CYCLES);
341ARMV8_EVENT_ATTR(br_pred, ARMV8_PMUV3_PERFCTR_BR_PRED);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700342ARMV8_EVENT_ATTR(mem_access, ARMV8_PMUV3_PERFCTR_MEM_ACCESS);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700343ARMV8_EVENT_ATTR(l1i_cache, ARMV8_PMUV3_PERFCTR_L1I_CACHE);
344ARMV8_EVENT_ATTR(l1d_cache_wb, ARMV8_PMUV3_PERFCTR_L1D_CACHE_WB);
345ARMV8_EVENT_ATTR(l2d_cache, ARMV8_PMUV3_PERFCTR_L2D_CACHE);
346ARMV8_EVENT_ATTR(l2d_cache_refill, ARMV8_PMUV3_PERFCTR_L2D_CACHE_REFILL);
347ARMV8_EVENT_ATTR(l2d_cache_wb, ARMV8_PMUV3_PERFCTR_L2D_CACHE_WB);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700348ARMV8_EVENT_ATTR(bus_access, ARMV8_PMUV3_PERFCTR_BUS_ACCESS);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700349ARMV8_EVENT_ATTR(memory_error, ARMV8_PMUV3_PERFCTR_MEMORY_ERROR);
350ARMV8_EVENT_ATTR(inst_spec, ARMV8_PMUV3_PERFCTR_INST_SPEC);
351ARMV8_EVENT_ATTR(ttbr_write_retired, ARMV8_PMUV3_PERFCTR_TTBR_WRITE_RETIRED);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700352ARMV8_EVENT_ATTR(bus_cycles, ARMV8_PMUV3_PERFCTR_BUS_CYCLES);
Will Deacon4ba25782016-04-25 15:05:24 +0100353/* Don't expose the chain event in /sys, since it's useless in isolation */
Drew Richardson9e9caa62015-10-22 07:07:32 -0700354ARMV8_EVENT_ATTR(l1d_cache_allocate, ARMV8_PMUV3_PERFCTR_L1D_CACHE_ALLOCATE);
355ARMV8_EVENT_ATTR(l2d_cache_allocate, ARMV8_PMUV3_PERFCTR_L2D_CACHE_ALLOCATE);
356ARMV8_EVENT_ATTR(br_retired, ARMV8_PMUV3_PERFCTR_BR_RETIRED);
357ARMV8_EVENT_ATTR(br_mis_pred_retired, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED_RETIRED);
358ARMV8_EVENT_ATTR(stall_frontend, ARMV8_PMUV3_PERFCTR_STALL_FRONTEND);
359ARMV8_EVENT_ATTR(stall_backend, ARMV8_PMUV3_PERFCTR_STALL_BACKEND);
360ARMV8_EVENT_ATTR(l1d_tlb, ARMV8_PMUV3_PERFCTR_L1D_TLB);
361ARMV8_EVENT_ATTR(l1i_tlb, ARMV8_PMUV3_PERFCTR_L1I_TLB);
362ARMV8_EVENT_ATTR(l2i_cache, ARMV8_PMUV3_PERFCTR_L2I_CACHE);
363ARMV8_EVENT_ATTR(l2i_cache_refill, ARMV8_PMUV3_PERFCTR_L2I_CACHE_REFILL);
364ARMV8_EVENT_ATTR(l3d_cache_allocate, ARMV8_PMUV3_PERFCTR_L3D_CACHE_ALLOCATE);
365ARMV8_EVENT_ATTR(l3d_cache_refill, ARMV8_PMUV3_PERFCTR_L3D_CACHE_REFILL);
366ARMV8_EVENT_ATTR(l3d_cache, ARMV8_PMUV3_PERFCTR_L3D_CACHE);
367ARMV8_EVENT_ATTR(l3d_cache_wb, ARMV8_PMUV3_PERFCTR_L3D_CACHE_WB);
368ARMV8_EVENT_ATTR(l2d_tlb_refill, ARMV8_PMUV3_PERFCTR_L2D_TLB_REFILL);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700369ARMV8_EVENT_ATTR(l2i_tlb_refill, ARMV8_PMUV3_PERFCTR_L2I_TLB_REFILL);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700370ARMV8_EVENT_ATTR(l2d_tlb, ARMV8_PMUV3_PERFCTR_L2D_TLB);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700371ARMV8_EVENT_ATTR(l2i_tlb, ARMV8_PMUV3_PERFCTR_L2I_TLB);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700372
373static struct attribute *armv8_pmuv3_event_attrs[] = {
374 &armv8_event_attr_sw_incr.attr.attr,
375 &armv8_event_attr_l1i_cache_refill.attr.attr,
376 &armv8_event_attr_l1i_tlb_refill.attr.attr,
377 &armv8_event_attr_l1d_cache_refill.attr.attr,
378 &armv8_event_attr_l1d_cache.attr.attr,
379 &armv8_event_attr_l1d_tlb_refill.attr.attr,
380 &armv8_event_attr_ld_retired.attr.attr,
381 &armv8_event_attr_st_retired.attr.attr,
382 &armv8_event_attr_inst_retired.attr.attr,
383 &armv8_event_attr_exc_taken.attr.attr,
384 &armv8_event_attr_exc_return.attr.attr,
385 &armv8_event_attr_cid_write_retired.attr.attr,
386 &armv8_event_attr_pc_write_retired.attr.attr,
387 &armv8_event_attr_br_immed_retired.attr.attr,
388 &armv8_event_attr_br_return_retired.attr.attr,
389 &armv8_event_attr_unaligned_ldst_retired.attr.attr,
390 &armv8_event_attr_br_mis_pred.attr.attr,
391 &armv8_event_attr_cpu_cycles.attr.attr,
392 &armv8_event_attr_br_pred.attr.attr,
393 &armv8_event_attr_mem_access.attr.attr,
394 &armv8_event_attr_l1i_cache.attr.attr,
395 &armv8_event_attr_l1d_cache_wb.attr.attr,
396 &armv8_event_attr_l2d_cache.attr.attr,
397 &armv8_event_attr_l2d_cache_refill.attr.attr,
398 &armv8_event_attr_l2d_cache_wb.attr.attr,
399 &armv8_event_attr_bus_access.attr.attr,
400 &armv8_event_attr_memory_error.attr.attr,
401 &armv8_event_attr_inst_spec.attr.attr,
402 &armv8_event_attr_ttbr_write_retired.attr.attr,
403 &armv8_event_attr_bus_cycles.attr.attr,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700404 &armv8_event_attr_l1d_cache_allocate.attr.attr,
405 &armv8_event_attr_l2d_cache_allocate.attr.attr,
406 &armv8_event_attr_br_retired.attr.attr,
407 &armv8_event_attr_br_mis_pred_retired.attr.attr,
408 &armv8_event_attr_stall_frontend.attr.attr,
409 &armv8_event_attr_stall_backend.attr.attr,
410 &armv8_event_attr_l1d_tlb.attr.attr,
411 &armv8_event_attr_l1i_tlb.attr.attr,
412 &armv8_event_attr_l2i_cache.attr.attr,
413 &armv8_event_attr_l2i_cache_refill.attr.attr,
414 &armv8_event_attr_l3d_cache_allocate.attr.attr,
415 &armv8_event_attr_l3d_cache_refill.attr.attr,
416 &armv8_event_attr_l3d_cache.attr.attr,
417 &armv8_event_attr_l3d_cache_wb.attr.attr,
418 &armv8_event_attr_l2d_tlb_refill.attr.attr,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700419 &armv8_event_attr_l2i_tlb_refill.attr.attr,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700420 &armv8_event_attr_l2d_tlb.attr.attr,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700421 &armv8_event_attr_l2i_tlb.attr.attr,
Will Deacon57d74122015-12-22 14:42:57 +0000422 NULL,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700423};
424
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700425static umode_t
426armv8pmu_event_attr_is_visible(struct kobject *kobj,
427 struct attribute *attr, int unused)
428{
429 struct device *dev = kobj_to_dev(kobj);
430 struct pmu *pmu = dev_get_drvdata(dev);
431 struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
432 struct perf_pmu_events_attr *pmu_attr;
433
434 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
435
436 if (test_bit(pmu_attr->id, cpu_pmu->pmceid_bitmap))
437 return attr->mode;
438
439 return 0;
440}
441
Drew Richardson9e9caa62015-10-22 07:07:32 -0700442static struct attribute_group armv8_pmuv3_events_attr_group = {
443 .name = "events",
444 .attrs = armv8_pmuv3_event_attrs,
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700445 .is_visible = armv8pmu_event_attr_is_visible,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700446};
447
Shaokun Zhangfe7296e2017-05-24 15:43:18 +0800448PMU_FORMAT_ATTR(event, "config:0-15");
Will Deacon57d74122015-12-22 14:42:57 +0000449
450static struct attribute *armv8_pmuv3_format_attrs[] = {
451 &format_attr_event.attr,
452 NULL,
453};
454
455static struct attribute_group armv8_pmuv3_format_attr_group = {
456 .name = "format",
457 .attrs = armv8_pmuv3_format_attrs,
458};
459
Will Deacon03089682012-03-05 11:49:32 +0000460/*
461 * Perf Events' indices
462 */
463#define ARMV8_IDX_CYCLE_COUNTER 0
464#define ARMV8_IDX_COUNTER0 1
Mark Rutland6475b2d2015-10-02 10:55:03 +0100465#define ARMV8_IDX_COUNTER_LAST(cpu_pmu) \
466 (ARMV8_IDX_CYCLE_COUNTER + cpu_pmu->num_events - 1)
Will Deacon03089682012-03-05 11:49:32 +0000467
Will Deacon03089682012-03-05 11:49:32 +0000468/*
469 * ARMv8 low level PMU access
470 */
471
472/*
473 * Perf Event to low level counters mapping
474 */
475#define ARMV8_IDX_TO_COUNTER(x) \
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000476 (((x) - ARMV8_IDX_COUNTER0) & ARMV8_PMU_COUNTER_MASK)
Will Deacon03089682012-03-05 11:49:32 +0000477
478static inline u32 armv8pmu_pmcr_read(void)
479{
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700480 return read_sysreg(pmcr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000481}
482
483static inline void armv8pmu_pmcr_write(u32 val)
484{
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000485 val &= ARMV8_PMU_PMCR_MASK;
Will Deacon03089682012-03-05 11:49:32 +0000486 isb();
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700487 write_sysreg(val, pmcr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000488}
489
490static inline int armv8pmu_has_overflowed(u32 pmovsr)
491{
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000492 return pmovsr & ARMV8_PMU_OVERFLOWED_MASK;
Will Deacon03089682012-03-05 11:49:32 +0000493}
494
Mark Rutland6475b2d2015-10-02 10:55:03 +0100495static inline int armv8pmu_counter_valid(struct arm_pmu *cpu_pmu, int idx)
Will Deacon03089682012-03-05 11:49:32 +0000496{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100497 return idx >= ARMV8_IDX_CYCLE_COUNTER &&
498 idx <= ARMV8_IDX_COUNTER_LAST(cpu_pmu);
Will Deacon03089682012-03-05 11:49:32 +0000499}
500
501static inline int armv8pmu_counter_has_overflowed(u32 pmnc, int idx)
502{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100503 return pmnc & BIT(ARMV8_IDX_TO_COUNTER(idx));
Will Deacon03089682012-03-05 11:49:32 +0000504}
505
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100506static inline void armv8pmu_select_counter(int idx)
Will Deacon03089682012-03-05 11:49:32 +0000507{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100508 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700509 write_sysreg(counter, pmselr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000510 isb();
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100511}
Will Deacon03089682012-03-05 11:49:32 +0000512
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100513static inline u32 armv8pmu_read_evcntr(int idx)
514{
515 armv8pmu_select_counter(idx);
516 return read_sysreg(pmxevcntr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000517}
518
Suzuki K Poulose3a952002018-07-10 09:57:59 +0100519static inline u64 armv8pmu_read_counter(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000520{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100521 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
522 struct hw_perf_event *hwc = &event->hw;
523 int idx = hwc->idx;
Will Deacon03089682012-03-05 11:49:32 +0000524 u32 value = 0;
525
Mark Rutland6475b2d2015-10-02 10:55:03 +0100526 if (!armv8pmu_counter_valid(cpu_pmu, idx))
Will Deacon03089682012-03-05 11:49:32 +0000527 pr_err("CPU%u reading wrong counter %d\n",
528 smp_processor_id(), idx);
529 else if (idx == ARMV8_IDX_CYCLE_COUNTER)
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700530 value = read_sysreg(pmccntr_el0);
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100531 else
532 value = armv8pmu_read_evcntr(idx);
Will Deacon03089682012-03-05 11:49:32 +0000533
534 return value;
535}
536
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100537static inline void armv8pmu_write_evcntr(int idx, u32 value)
538{
539 armv8pmu_select_counter(idx);
540 write_sysreg(value, pmxevcntr_el0);
541}
542
Suzuki K Poulose3a952002018-07-10 09:57:59 +0100543static inline void armv8pmu_write_counter(struct perf_event *event, u64 value)
Will Deacon03089682012-03-05 11:49:32 +0000544{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100545 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
546 struct hw_perf_event *hwc = &event->hw;
547 int idx = hwc->idx;
548
549 if (!armv8pmu_counter_valid(cpu_pmu, idx))
Will Deacon03089682012-03-05 11:49:32 +0000550 pr_err("CPU%u writing wrong counter %d\n",
551 smp_processor_id(), idx);
Jan Glauber7175f052016-02-18 17:50:13 +0100552 else if (idx == ARMV8_IDX_CYCLE_COUNTER) {
553 /*
554 * Set the upper 32bits as this is a 64bit counter but we only
555 * count using the lower 32bits and we want an interrupt when
556 * it overflows.
557 */
Suzuki K Poulose3a952002018-07-10 09:57:59 +0100558 value |= 0xffffffff00000000ULL;
559 write_sysreg(value, pmccntr_el0);
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100560 } else
561 armv8pmu_write_evcntr(idx, value);
Will Deacon03089682012-03-05 11:49:32 +0000562}
563
564static inline void armv8pmu_write_evtype(int idx, u32 val)
565{
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100566 armv8pmu_select_counter(idx);
567 val &= ARMV8_PMU_EVTYPE_MASK;
568 write_sysreg(val, pmxevtyper_el0);
Will Deacon03089682012-03-05 11:49:32 +0000569}
570
571static inline int armv8pmu_enable_counter(int idx)
572{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100573 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700574 write_sysreg(BIT(counter), pmcntenset_el0);
Will Deacon03089682012-03-05 11:49:32 +0000575 return idx;
576}
577
578static inline int armv8pmu_disable_counter(int idx)
579{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100580 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700581 write_sysreg(BIT(counter), pmcntenclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000582 return idx;
583}
584
585static inline int armv8pmu_enable_intens(int idx)
586{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100587 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700588 write_sysreg(BIT(counter), pmintenset_el1);
Will Deacon03089682012-03-05 11:49:32 +0000589 return idx;
590}
591
592static inline int armv8pmu_disable_intens(int idx)
593{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100594 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700595 write_sysreg(BIT(counter), pmintenclr_el1);
Will Deacon03089682012-03-05 11:49:32 +0000596 isb();
597 /* Clear the overflow flag in case an interrupt is pending. */
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700598 write_sysreg(BIT(counter), pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000599 isb();
Mark Rutland6475b2d2015-10-02 10:55:03 +0100600
Will Deacon03089682012-03-05 11:49:32 +0000601 return idx;
602}
603
604static inline u32 armv8pmu_getreset_flags(void)
605{
606 u32 value;
607
608 /* Read */
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700609 value = read_sysreg(pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000610
611 /* Write to clear flags */
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000612 value &= ARMV8_PMU_OVSR_MASK;
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700613 write_sysreg(value, pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000614
615 return value;
616}
617
Mark Rutland6475b2d2015-10-02 10:55:03 +0100618static void armv8pmu_enable_event(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000619{
620 unsigned long flags;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100621 struct hw_perf_event *hwc = &event->hw;
622 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
623 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
624 int idx = hwc->idx;
Will Deacon03089682012-03-05 11:49:32 +0000625
626 /*
627 * Enable counter and interrupt, and set the counter to count
628 * the event that we're interested in.
629 */
630 raw_spin_lock_irqsave(&events->pmu_lock, flags);
631
632 /*
633 * Disable counter
634 */
635 armv8pmu_disable_counter(idx);
636
637 /*
638 * Set event (if destined for PMNx counters).
639 */
640 armv8pmu_write_evtype(idx, hwc->config_base);
641
642 /*
643 * Enable interrupt for this counter
644 */
645 armv8pmu_enable_intens(idx);
646
647 /*
648 * Enable counter
649 */
650 armv8pmu_enable_counter(idx);
651
652 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
653}
654
Mark Rutland6475b2d2015-10-02 10:55:03 +0100655static void armv8pmu_disable_event(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000656{
657 unsigned long flags;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100658 struct hw_perf_event *hwc = &event->hw;
659 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
660 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
661 int idx = hwc->idx;
Will Deacon03089682012-03-05 11:49:32 +0000662
663 /*
664 * Disable counter and interrupt
665 */
666 raw_spin_lock_irqsave(&events->pmu_lock, flags);
667
668 /*
669 * Disable counter
670 */
671 armv8pmu_disable_counter(idx);
672
673 /*
674 * Disable interrupt for this counter
675 */
676 armv8pmu_disable_intens(idx);
677
678 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
679}
680
Mark Rutland0788f1e2018-05-10 11:35:15 +0100681static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +0000682{
683 u32 pmovsr;
684 struct perf_sample_data data;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100685 struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
Will Deacon03089682012-03-05 11:49:32 +0000686 struct pt_regs *regs;
687 int idx;
688
689 /*
690 * Get and reset the IRQ flags
691 */
692 pmovsr = armv8pmu_getreset_flags();
693
694 /*
695 * Did an overflow occur?
696 */
697 if (!armv8pmu_has_overflowed(pmovsr))
698 return IRQ_NONE;
699
700 /*
701 * Handle the counter(s) overflow(s)
702 */
703 regs = get_irq_regs();
704
Will Deacon03089682012-03-05 11:49:32 +0000705 for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
706 struct perf_event *event = cpuc->events[idx];
707 struct hw_perf_event *hwc;
708
709 /* Ignore if we don't have an event. */
710 if (!event)
711 continue;
712
713 /*
714 * We have a single interrupt for all counters. Check that
715 * each counter has overflowed before we process it.
716 */
717 if (!armv8pmu_counter_has_overflowed(pmovsr, idx))
718 continue;
719
720 hwc = &event->hw;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100721 armpmu_event_update(event);
Will Deacon03089682012-03-05 11:49:32 +0000722 perf_sample_data_init(&data, 0, hwc->last_period);
Mark Rutland6475b2d2015-10-02 10:55:03 +0100723 if (!armpmu_event_set_period(event))
Will Deacon03089682012-03-05 11:49:32 +0000724 continue;
725
726 if (perf_event_overflow(event, &data, regs))
Mark Rutland6475b2d2015-10-02 10:55:03 +0100727 cpu_pmu->disable(event);
Will Deacon03089682012-03-05 11:49:32 +0000728 }
729
730 /*
731 * Handle the pending perf events.
732 *
733 * Note: this call *must* be run with interrupts disabled. For
734 * platforms that can have the PMU interrupts raised as an NMI, this
735 * will not work.
736 */
737 irq_work_run();
738
739 return IRQ_HANDLED;
740}
741
Mark Rutland6475b2d2015-10-02 10:55:03 +0100742static void armv8pmu_start(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +0000743{
744 unsigned long flags;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100745 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
Will Deacon03089682012-03-05 11:49:32 +0000746
747 raw_spin_lock_irqsave(&events->pmu_lock, flags);
748 /* Enable all counters */
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000749 armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMU_PMCR_E);
Will Deacon03089682012-03-05 11:49:32 +0000750 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
751}
752
Mark Rutland6475b2d2015-10-02 10:55:03 +0100753static void armv8pmu_stop(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +0000754{
755 unsigned long flags;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100756 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
Will Deacon03089682012-03-05 11:49:32 +0000757
758 raw_spin_lock_irqsave(&events->pmu_lock, flags);
759 /* Disable all counters */
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000760 armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMU_PMCR_E);
Will Deacon03089682012-03-05 11:49:32 +0000761 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
762}
763
764static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
Mark Rutland6475b2d2015-10-02 10:55:03 +0100765 struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000766{
767 int idx;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100768 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
769 struct hw_perf_event *hwc = &event->hw;
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000770 unsigned long evtype = hwc->config_base & ARMV8_PMU_EVTYPE_EVENT;
Will Deacon03089682012-03-05 11:49:32 +0000771
Pratyush Anand1031a152017-07-01 12:03:35 +0530772 /* Always prefer to place a cycle counter into the cycle counter. */
Ashok Kumar03598fd2016-04-21 05:58:41 -0700773 if (evtype == ARMV8_PMUV3_PERFCTR_CPU_CYCLES) {
Pratyush Anand1031a152017-07-01 12:03:35 +0530774 if (!test_and_set_bit(ARMV8_IDX_CYCLE_COUNTER, cpuc->used_mask))
775 return ARMV8_IDX_CYCLE_COUNTER;
Will Deacon03089682012-03-05 11:49:32 +0000776 }
777
778 /*
Pratyush Anand1031a152017-07-01 12:03:35 +0530779 * Otherwise use events counters
Will Deacon03089682012-03-05 11:49:32 +0000780 */
781 for (idx = ARMV8_IDX_COUNTER0; idx < cpu_pmu->num_events; ++idx) {
782 if (!test_and_set_bit(idx, cpuc->used_mask))
783 return idx;
784 }
785
786 /* The counters are all in use. */
787 return -EAGAIN;
788}
789
Suzuki K Poulose7dfc8db2018-07-10 09:58:01 +0100790static void armv8pmu_clear_event_idx(struct pmu_hw_events *cpuc,
791 struct perf_event *event)
792{
793 clear_bit(event->hw.idx, cpuc->used_mask);
794}
795
Will Deacon03089682012-03-05 11:49:32 +0000796/*
797 * Add an event filter to a given event. This will only work for PMUv2 PMUs.
798 */
799static int armv8pmu_set_event_filter(struct hw_perf_event *event,
800 struct perf_event_attr *attr)
801{
802 unsigned long config_base = 0;
803
804 if (attr->exclude_idle)
805 return -EPERM;
Ganapatrao Kulkarni78a19cf2017-05-02 21:59:34 +0530806
807 /*
808 * If we're running in hyp mode, then we *are* the hypervisor.
809 * Therefore we ignore exclude_hv in this configuration, since
810 * there's no hypervisor to sample anyway. This is consistent
811 * with other architectures (x86 and Power).
812 */
813 if (is_kernel_in_hyp_mode()) {
814 if (!attr->exclude_kernel)
815 config_base |= ARMV8_PMU_INCLUDE_EL2;
816 } else {
817 if (attr->exclude_kernel)
818 config_base |= ARMV8_PMU_EXCLUDE_EL1;
819 if (!attr->exclude_hv)
820 config_base |= ARMV8_PMU_INCLUDE_EL2;
821 }
Will Deacon03089682012-03-05 11:49:32 +0000822 if (attr->exclude_user)
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000823 config_base |= ARMV8_PMU_EXCLUDE_EL0;
Will Deacon03089682012-03-05 11:49:32 +0000824
825 /*
826 * Install the filter into config_base as this is used to
827 * construct the event type.
828 */
829 event->config_base = config_base;
830
831 return 0;
832}
833
834static void armv8pmu_reset(void *info)
835{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100836 struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
Will Deacon03089682012-03-05 11:49:32 +0000837 u32 idx, nb_cnt = cpu_pmu->num_events;
838
839 /* The counter and interrupt enable registers are unknown at reset. */
Mark Rutland6475b2d2015-10-02 10:55:03 +0100840 for (idx = ARMV8_IDX_CYCLE_COUNTER; idx < nb_cnt; ++idx) {
841 armv8pmu_disable_counter(idx);
842 armv8pmu_disable_intens(idx);
843 }
Will Deacon03089682012-03-05 11:49:32 +0000844
Jan Glauber7175f052016-02-18 17:50:13 +0100845 /*
846 * Initialize & Reset PMNC. Request overflow interrupt for
847 * 64 bit cycle counter but cheat in armv8pmu_write_counter().
848 */
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000849 armv8pmu_pmcr_write(ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C |
850 ARMV8_PMU_PMCR_LC);
Will Deacon03089682012-03-05 11:49:32 +0000851}
852
Will Deacon6c833bb2017-08-08 16:58:33 +0100853static int __armv8_pmuv3_map_event(struct perf_event *event,
854 const unsigned (*extra_event_map)
855 [PERF_COUNT_HW_MAX],
856 const unsigned (*extra_cache_map)
857 [PERF_COUNT_HW_CACHE_MAX]
858 [PERF_COUNT_HW_CACHE_OP_MAX]
859 [PERF_COUNT_HW_CACHE_RESULT_MAX])
Will Deacon03089682012-03-05 11:49:32 +0000860{
Jeremy Linton236b9b912016-09-14 17:32:30 -0500861 int hw_event_id;
862 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
863
864 hw_event_id = armpmu_map_event(event, &armv8_pmuv3_perf_map,
865 &armv8_pmuv3_perf_cache_map,
866 ARMV8_PMU_EVTYPE_EVENT);
Jeremy Linton236b9b912016-09-14 17:32:30 -0500867
Will Deacon6c833bb2017-08-08 16:58:33 +0100868 /* Onl expose micro/arch events supported by this PMU */
869 if ((hw_event_id > 0) && (hw_event_id < ARMV8_PMUV3_MAX_COMMON_EVENTS)
870 && test_bit(hw_event_id, armpmu->pmceid_bitmap)) {
871 return hw_event_id;
Jeremy Linton236b9b912016-09-14 17:32:30 -0500872 }
873
Will Deacon6c833bb2017-08-08 16:58:33 +0100874 return armpmu_map_event(event, extra_event_map, extra_cache_map,
875 ARMV8_PMU_EVTYPE_EVENT);
876}
877
878static int armv8_pmuv3_map_event(struct perf_event *event)
879{
880 return __armv8_pmuv3_map_event(event, NULL, NULL);
Will Deacon03089682012-03-05 11:49:32 +0000881}
882
Mark Rutlandac82d122015-10-02 10:55:04 +0100883static int armv8_a53_map_event(struct perf_event *event)
884{
Will Deacond0d09d42017-08-08 17:11:27 +0100885 return __armv8_pmuv3_map_event(event, NULL, &armv8_a53_perf_cache_map);
Mark Rutlandac82d122015-10-02 10:55:04 +0100886}
887
Mark Rutland62a4dda2015-10-02 10:55:05 +0100888static int armv8_a57_map_event(struct perf_event *event)
889{
Will Deacond0d09d42017-08-08 17:11:27 +0100890 return __armv8_pmuv3_map_event(event, NULL, &armv8_a57_perf_cache_map);
Mark Rutland62a4dda2015-10-02 10:55:05 +0100891}
892
Julien Thierry5561b6c2017-08-09 17:46:38 +0100893static int armv8_a73_map_event(struct perf_event *event)
894{
895 return __armv8_pmuv3_map_event(event, NULL, &armv8_a73_perf_cache_map);
896}
897
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100898static int armv8_thunder_map_event(struct perf_event *event)
899{
Will Deacond0d09d42017-08-08 17:11:27 +0100900 return __armv8_pmuv3_map_event(event, NULL,
Will Deacon6c833bb2017-08-08 16:58:33 +0100901 &armv8_thunder_perf_cache_map);
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100902}
903
Ashok Kumar201a72b2016-04-21 05:58:45 -0700904static int armv8_vulcan_map_event(struct perf_event *event)
905{
Will Deacond0d09d42017-08-08 17:11:27 +0100906 return __armv8_pmuv3_map_event(event, NULL,
Will Deacon6c833bb2017-08-08 16:58:33 +0100907 &armv8_vulcan_perf_cache_map);
Ashok Kumar201a72b2016-04-21 05:58:45 -0700908}
909
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100910struct armv8pmu_probe_info {
911 struct arm_pmu *pmu;
912 bool present;
913};
914
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700915static void __armv8pmu_probe_pmu(void *info)
Will Deacon03089682012-03-05 11:49:32 +0000916{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100917 struct armv8pmu_probe_info *probe = info;
918 struct arm_pmu *cpu_pmu = probe->pmu;
Mark Rutlandfaa9a082017-04-25 12:08:50 +0100919 u64 dfr0;
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700920 u32 pmceid[2];
Mark Rutlandfaa9a082017-04-25 12:08:50 +0100921 int pmuver;
Will Deacon03089682012-03-05 11:49:32 +0000922
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100923 dfr0 = read_sysreg(id_aa64dfr0_el1);
Mark Rutland03313652018-02-14 17:21:57 +0000924 pmuver = cpuid_feature_extract_unsigned_field(dfr0,
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100925 ID_AA64DFR0_PMUVER_SHIFT);
Mark Rutland03313652018-02-14 17:21:57 +0000926 if (pmuver == 0xf || pmuver == 0)
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100927 return;
928
929 probe->present = true;
930
Will Deacon03089682012-03-05 11:49:32 +0000931 /* Read the nb of CNTx counters supported from PMNC */
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700932 cpu_pmu->num_events = (armv8pmu_pmcr_read() >> ARMV8_PMU_PMCR_N_SHIFT)
933 & ARMV8_PMU_PMCR_N_MASK;
Will Deacon03089682012-03-05 11:49:32 +0000934
Mark Rutland6475b2d2015-10-02 10:55:03 +0100935 /* Add the CPU cycles counter */
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700936 cpu_pmu->num_events += 1;
937
938 pmceid[0] = read_sysreg(pmceid0_el0);
939 pmceid[1] = read_sysreg(pmceid1_el0);
940
Yury Norov3aa56882018-02-06 15:38:06 -0800941 bitmap_from_arr32(cpu_pmu->pmceid_bitmap,
942 pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
Will Deacon03089682012-03-05 11:49:32 +0000943}
944
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700945static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +0000946{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100947 struct armv8pmu_probe_info probe = {
948 .pmu = cpu_pmu,
949 .present = false,
950 };
951 int ret;
952
953 ret = smp_call_function_any(&cpu_pmu->supported_cpus,
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700954 __armv8pmu_probe_pmu,
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100955 &probe, 1);
956 if (ret)
957 return ret;
958
959 return probe.present ? 0 : -ENODEV;
Will Deacon03089682012-03-05 11:49:32 +0000960}
961
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100962static int armv8_pmu_init(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +0000963{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100964 int ret = armv8pmu_probe_pmu(cpu_pmu);
965 if (ret)
966 return ret;
967
Mark Rutland6475b2d2015-10-02 10:55:03 +0100968 cpu_pmu->handle_irq = armv8pmu_handle_irq,
969 cpu_pmu->enable = armv8pmu_enable_event,
970 cpu_pmu->disable = armv8pmu_disable_event,
971 cpu_pmu->read_counter = armv8pmu_read_counter,
972 cpu_pmu->write_counter = armv8pmu_write_counter,
973 cpu_pmu->get_event_idx = armv8pmu_get_event_idx,
Suzuki K Poulose7dfc8db2018-07-10 09:58:01 +0100974 cpu_pmu->clear_event_idx = armv8pmu_clear_event_idx,
Mark Rutland6475b2d2015-10-02 10:55:03 +0100975 cpu_pmu->start = armv8pmu_start,
976 cpu_pmu->stop = armv8pmu_stop,
977 cpu_pmu->reset = armv8pmu_reset,
Mark Rutlandac82d122015-10-02 10:55:04 +0100978 cpu_pmu->set_event_filter = armv8pmu_set_event_filter;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100979
980 return 0;
Mark Rutlandac82d122015-10-02 10:55:04 +0100981}
982
983static int armv8_pmuv3_init(struct arm_pmu *cpu_pmu)
984{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100985 int ret = armv8_pmu_init(cpu_pmu);
986 if (ret)
987 return ret;
988
Mark Rutland6475b2d2015-10-02 10:55:03 +0100989 cpu_pmu->name = "armv8_pmuv3";
990 cpu_pmu->map_event = armv8_pmuv3_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +0100991 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
992 &armv8_pmuv3_events_attr_group;
993 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
994 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100995
996 return 0;
Mark Rutlandac82d122015-10-02 10:55:04 +0100997}
998
Julien Thierrye884f802017-08-09 17:46:39 +0100999static int armv8_a35_pmu_init(struct arm_pmu *cpu_pmu)
1000{
1001 int ret = armv8_pmu_init(cpu_pmu);
1002 if (ret)
1003 return ret;
1004
1005 cpu_pmu->name = "armv8_cortex_a35";
1006 cpu_pmu->map_event = armv8_a53_map_event;
1007 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1008 &armv8_pmuv3_events_attr_group;
1009 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1010 &armv8_pmuv3_format_attr_group;
1011
1012 return 0;
1013}
1014
Mark Rutlandac82d122015-10-02 10:55:04 +01001015static int armv8_a53_pmu_init(struct arm_pmu *cpu_pmu)
1016{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001017 int ret = armv8_pmu_init(cpu_pmu);
1018 if (ret)
1019 return ret;
1020
Mark Rutlandac82d122015-10-02 10:55:04 +01001021 cpu_pmu->name = "armv8_cortex_a53";
1022 cpu_pmu->map_event = armv8_a53_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001023 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1024 &armv8_pmuv3_events_attr_group;
1025 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1026 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001027
1028 return 0;
Will Deacon03089682012-03-05 11:49:32 +00001029}
Will Deacon03089682012-03-05 11:49:32 +00001030
Mark Rutland62a4dda2015-10-02 10:55:05 +01001031static int armv8_a57_pmu_init(struct arm_pmu *cpu_pmu)
1032{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001033 int ret = armv8_pmu_init(cpu_pmu);
1034 if (ret)
1035 return ret;
1036
Mark Rutland62a4dda2015-10-02 10:55:05 +01001037 cpu_pmu->name = "armv8_cortex_a57";
1038 cpu_pmu->map_event = armv8_a57_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001039 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1040 &armv8_pmuv3_events_attr_group;
1041 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1042 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001043
1044 return 0;
Mark Rutland62a4dda2015-10-02 10:55:05 +01001045}
1046
Will Deacon5d7ee872015-12-22 14:45:35 +00001047static int armv8_a72_pmu_init(struct arm_pmu *cpu_pmu)
1048{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001049 int ret = armv8_pmu_init(cpu_pmu);
1050 if (ret)
1051 return ret;
1052
Will Deacon5d7ee872015-12-22 14:45:35 +00001053 cpu_pmu->name = "armv8_cortex_a72";
1054 cpu_pmu->map_event = armv8_a57_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001055 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1056 &armv8_pmuv3_events_attr_group;
1057 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1058 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001059
1060 return 0;
Will Deacon5d7ee872015-12-22 14:45:35 +00001061}
1062
Julien Thierry5561b6c2017-08-09 17:46:38 +01001063static int armv8_a73_pmu_init(struct arm_pmu *cpu_pmu)
1064{
1065 int ret = armv8_pmu_init(cpu_pmu);
1066 if (ret)
1067 return ret;
1068
1069 cpu_pmu->name = "armv8_cortex_a73";
1070 cpu_pmu->map_event = armv8_a73_map_event;
1071 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1072 &armv8_pmuv3_events_attr_group;
1073 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1074 &armv8_pmuv3_format_attr_group;
1075
1076 return 0;
1077}
1078
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001079static int armv8_thunder_pmu_init(struct arm_pmu *cpu_pmu)
1080{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001081 int ret = armv8_pmu_init(cpu_pmu);
1082 if (ret)
1083 return ret;
1084
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001085 cpu_pmu->name = "armv8_cavium_thunder";
1086 cpu_pmu->map_event = armv8_thunder_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001087 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1088 &armv8_pmuv3_events_attr_group;
1089 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1090 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001091
1092 return 0;
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001093}
1094
Ashok Kumar201a72b2016-04-21 05:58:45 -07001095static int armv8_vulcan_pmu_init(struct arm_pmu *cpu_pmu)
1096{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001097 int ret = armv8_pmu_init(cpu_pmu);
1098 if (ret)
1099 return ret;
1100
Ashok Kumar201a72b2016-04-21 05:58:45 -07001101 cpu_pmu->name = "armv8_brcm_vulcan";
1102 cpu_pmu->map_event = armv8_vulcan_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001103 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1104 &armv8_pmuv3_events_attr_group;
1105 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1106 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001107
1108 return 0;
Ashok Kumar201a72b2016-04-21 05:58:45 -07001109}
1110
Mark Rutland6475b2d2015-10-02 10:55:03 +01001111static const struct of_device_id armv8_pmu_of_device_ids[] = {
1112 {.compatible = "arm,armv8-pmuv3", .data = armv8_pmuv3_init},
Julien Thierrye884f802017-08-09 17:46:39 +01001113 {.compatible = "arm,cortex-a35-pmu", .data = armv8_a35_pmu_init},
Mark Rutlandac82d122015-10-02 10:55:04 +01001114 {.compatible = "arm,cortex-a53-pmu", .data = armv8_a53_pmu_init},
Mark Rutland62a4dda2015-10-02 10:55:05 +01001115 {.compatible = "arm,cortex-a57-pmu", .data = armv8_a57_pmu_init},
Will Deacon5d7ee872015-12-22 14:45:35 +00001116 {.compatible = "arm,cortex-a72-pmu", .data = armv8_a72_pmu_init},
Julien Thierry5561b6c2017-08-09 17:46:38 +01001117 {.compatible = "arm,cortex-a73-pmu", .data = armv8_a73_pmu_init},
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001118 {.compatible = "cavium,thunder-pmu", .data = armv8_thunder_pmu_init},
Ashok Kumar201a72b2016-04-21 05:58:45 -07001119 {.compatible = "brcm,vulcan-pmu", .data = armv8_vulcan_pmu_init},
Will Deacon03089682012-03-05 11:49:32 +00001120 {},
1121};
1122
Mark Rutland6475b2d2015-10-02 10:55:03 +01001123static int armv8_pmu_device_probe(struct platform_device *pdev)
Will Deacon03089682012-03-05 11:49:32 +00001124{
Mark Rutlandf00fa5f2017-04-11 09:39:57 +01001125 return arm_pmu_device_probe(pdev, armv8_pmu_of_device_ids, NULL);
Will Deacon03089682012-03-05 11:49:32 +00001126}
1127
Mark Rutland6475b2d2015-10-02 10:55:03 +01001128static struct platform_driver armv8_pmu_driver = {
Will Deacon03089682012-03-05 11:49:32 +00001129 .driver = {
Jeremy Linton85023b22016-09-14 17:32:31 -05001130 .name = ARMV8_PMU_PDEV_NAME,
Mark Rutland6475b2d2015-10-02 10:55:03 +01001131 .of_match_table = armv8_pmu_of_device_ids,
Will Deacon03089682012-03-05 11:49:32 +00001132 },
Mark Rutland6475b2d2015-10-02 10:55:03 +01001133 .probe = armv8_pmu_device_probe,
Will Deacon03089682012-03-05 11:49:32 +00001134};
1135
Mark Rutlandf00fa5f2017-04-11 09:39:57 +01001136static int __init armv8_pmu_driver_init(void)
1137{
1138 if (acpi_disabled)
1139 return platform_driver_register(&armv8_pmu_driver);
1140 else
1141 return arm_pmu_acpi_probe(armv8_pmuv3_init);
1142}
1143device_initcall(armv8_pmu_driver_init)