Linux 4.18.10
[linux/fpc-iii.git] / arch / arm64 / kernel / perf_event.c
blobdd5b4fab114f8f2be390751aebe3e33916a68ed4
1 /*
2 * PMU support
4 * Copyright (C) 2012 ARM Limited
5 * Author: Will Deacon <will.deacon@arm.com>
7 * This code is based heavily on the ARMv7 perf event code.
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.
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.
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/>.
22 #include <asm/irq_regs.h>
23 #include <asm/perf_event.h>
24 #include <asm/sysreg.h>
25 #include <asm/virt.h>
27 #include <linux/acpi.h>
28 #include <linux/of.h>
29 #include <linux/perf/arm_pmu.h>
30 #include <linux/platform_device.h>
33 * ARMv8 PMUv3 Performance Events handling code.
34 * Common event types (some are defined in asm/perf_event.h).
37 /* At least one of the following is required. */
38 #define ARMV8_PMUV3_PERFCTR_INST_RETIRED 0x08
39 #define ARMV8_PMUV3_PERFCTR_INST_SPEC 0x1B
41 /* Common architectural events. */
42 #define ARMV8_PMUV3_PERFCTR_LD_RETIRED 0x06
43 #define ARMV8_PMUV3_PERFCTR_ST_RETIRED 0x07
44 #define ARMV8_PMUV3_PERFCTR_EXC_TAKEN 0x09
45 #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
52 #define ARMV8_PMUV3_PERFCTR_CHAIN 0x1E
53 #define ARMV8_PMUV3_PERFCTR_BR_RETIRED 0x21
55 /* Common microarchitectural events. */
56 #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
59 #define ARMV8_PMUV3_PERFCTR_MEM_ACCESS 0x13
60 #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
65 #define ARMV8_PMUV3_PERFCTR_BUS_ACCESS 0x19
66 #define ARMV8_PMUV3_PERFCTR_MEMORY_ERROR 0x1A
67 #define ARMV8_PMUV3_PERFCTR_BUS_CYCLES 0x1D
68 #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
82 #define ARMV8_PMUV3_PERFCTR_L2I_TLB_REFILL 0x2E
83 #define ARMV8_PMUV3_PERFCTR_L2D_TLB 0x2F
84 #define ARMV8_PMUV3_PERFCTR_L2I_TLB 0x30
86 /* 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
91 #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
97 #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
101 #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
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
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
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
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
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
144 #define ARMV8_IMPDEF_PERFCTR_ISB_SPEC 0x7C
145 #define ARMV8_IMPDEF_PERFCTR_DSB_SPEC 0x7D
146 #define ARMV8_IMPDEF_PERFCTR_DMB_SPEC 0x7E
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
153 #define ARMV8_IMPDEF_PERFCTR_EXC_IRQ 0x86
154 #define ARMV8_IMPDEF_PERFCTR_EXC_FIQ 0x87
155 #define ARMV8_IMPDEF_PERFCTR_EXC_SMC 0x88
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
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
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
175 /* ARMv8 Cortex-A53 specific event types. */
176 #define ARMV8_A53_PERFCTR_PREF_LINEFILL 0xC2
178 /* ARMv8 Cavium ThunderX specific event types. */
179 #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
185 /* PMUv3 HW events mapping. */
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.
192 static const unsigned armv8_pmuv3_perf_map[PERF_COUNT_HW_MAX] = {
193 PERF_MAP_ALL_UNSUPPORTED,
194 [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,
198 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED,
199 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
200 [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,
205 static 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] = {
208 PERF_CACHE_MAP_ALL_UNSUPPORTED,
210 [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,
215 [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,
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,
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,
224 [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,
230 static 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,
235 [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_A53_PERFCTR_PREF_LINEFILL,
237 [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,
241 static 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,
246 [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,
251 [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,
254 [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,
258 static 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,
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,
267 static 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,
272 [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,
279 [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,
282 [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,
288 static 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,
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,
298 [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,
303 [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,
307 static ssize_t
308 armv8pmu_events_sysfs_show(struct device *dev,
309 struct device_attribute *attr, char *page)
311 struct perf_pmu_events_attr *pmu_attr;
313 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
315 return sprintf(page, "event=0x%03llx\n", pmu_attr->id);
318 #define ARMV8_EVENT_ATTR_RESOLVE(m) #m
319 #define ARMV8_EVENT_ATTR(name, config) \
320 PMU_EVENT_ATTR(name, armv8_event_attr_##name, \
321 config, armv8pmu_events_sysfs_show)
323 ARMV8_EVENT_ATTR(sw_incr, ARMV8_PMUV3_PERFCTR_SW_INCR);
324 ARMV8_EVENT_ATTR(l1i_cache_refill, ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL);
325 ARMV8_EVENT_ATTR(l1i_tlb_refill, ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL);
326 ARMV8_EVENT_ATTR(l1d_cache_refill, ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL);
327 ARMV8_EVENT_ATTR(l1d_cache, ARMV8_PMUV3_PERFCTR_L1D_CACHE);
328 ARMV8_EVENT_ATTR(l1d_tlb_refill, ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL);
329 ARMV8_EVENT_ATTR(ld_retired, ARMV8_PMUV3_PERFCTR_LD_RETIRED);
330 ARMV8_EVENT_ATTR(st_retired, ARMV8_PMUV3_PERFCTR_ST_RETIRED);
331 ARMV8_EVENT_ATTR(inst_retired, ARMV8_PMUV3_PERFCTR_INST_RETIRED);
332 ARMV8_EVENT_ATTR(exc_taken, ARMV8_PMUV3_PERFCTR_EXC_TAKEN);
333 ARMV8_EVENT_ATTR(exc_return, ARMV8_PMUV3_PERFCTR_EXC_RETURN);
334 ARMV8_EVENT_ATTR(cid_write_retired, ARMV8_PMUV3_PERFCTR_CID_WRITE_RETIRED);
335 ARMV8_EVENT_ATTR(pc_write_retired, ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED);
336 ARMV8_EVENT_ATTR(br_immed_retired, ARMV8_PMUV3_PERFCTR_BR_IMMED_RETIRED);
337 ARMV8_EVENT_ATTR(br_return_retired, ARMV8_PMUV3_PERFCTR_BR_RETURN_RETIRED);
338 ARMV8_EVENT_ATTR(unaligned_ldst_retired, ARMV8_PMUV3_PERFCTR_UNALIGNED_LDST_RETIRED);
339 ARMV8_EVENT_ATTR(br_mis_pred, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED);
340 ARMV8_EVENT_ATTR(cpu_cycles, ARMV8_PMUV3_PERFCTR_CPU_CYCLES);
341 ARMV8_EVENT_ATTR(br_pred, ARMV8_PMUV3_PERFCTR_BR_PRED);
342 ARMV8_EVENT_ATTR(mem_access, ARMV8_PMUV3_PERFCTR_MEM_ACCESS);
343 ARMV8_EVENT_ATTR(l1i_cache, ARMV8_PMUV3_PERFCTR_L1I_CACHE);
344 ARMV8_EVENT_ATTR(l1d_cache_wb, ARMV8_PMUV3_PERFCTR_L1D_CACHE_WB);
345 ARMV8_EVENT_ATTR(l2d_cache, ARMV8_PMUV3_PERFCTR_L2D_CACHE);
346 ARMV8_EVENT_ATTR(l2d_cache_refill, ARMV8_PMUV3_PERFCTR_L2D_CACHE_REFILL);
347 ARMV8_EVENT_ATTR(l2d_cache_wb, ARMV8_PMUV3_PERFCTR_L2D_CACHE_WB);
348 ARMV8_EVENT_ATTR(bus_access, ARMV8_PMUV3_PERFCTR_BUS_ACCESS);
349 ARMV8_EVENT_ATTR(memory_error, ARMV8_PMUV3_PERFCTR_MEMORY_ERROR);
350 ARMV8_EVENT_ATTR(inst_spec, ARMV8_PMUV3_PERFCTR_INST_SPEC);
351 ARMV8_EVENT_ATTR(ttbr_write_retired, ARMV8_PMUV3_PERFCTR_TTBR_WRITE_RETIRED);
352 ARMV8_EVENT_ATTR(bus_cycles, ARMV8_PMUV3_PERFCTR_BUS_CYCLES);
353 /* Don't expose the chain event in /sys, since it's useless in isolation */
354 ARMV8_EVENT_ATTR(l1d_cache_allocate, ARMV8_PMUV3_PERFCTR_L1D_CACHE_ALLOCATE);
355 ARMV8_EVENT_ATTR(l2d_cache_allocate, ARMV8_PMUV3_PERFCTR_L2D_CACHE_ALLOCATE);
356 ARMV8_EVENT_ATTR(br_retired, ARMV8_PMUV3_PERFCTR_BR_RETIRED);
357 ARMV8_EVENT_ATTR(br_mis_pred_retired, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED_RETIRED);
358 ARMV8_EVENT_ATTR(stall_frontend, ARMV8_PMUV3_PERFCTR_STALL_FRONTEND);
359 ARMV8_EVENT_ATTR(stall_backend, ARMV8_PMUV3_PERFCTR_STALL_BACKEND);
360 ARMV8_EVENT_ATTR(l1d_tlb, ARMV8_PMUV3_PERFCTR_L1D_TLB);
361 ARMV8_EVENT_ATTR(l1i_tlb, ARMV8_PMUV3_PERFCTR_L1I_TLB);
362 ARMV8_EVENT_ATTR(l2i_cache, ARMV8_PMUV3_PERFCTR_L2I_CACHE);
363 ARMV8_EVENT_ATTR(l2i_cache_refill, ARMV8_PMUV3_PERFCTR_L2I_CACHE_REFILL);
364 ARMV8_EVENT_ATTR(l3d_cache_allocate, ARMV8_PMUV3_PERFCTR_L3D_CACHE_ALLOCATE);
365 ARMV8_EVENT_ATTR(l3d_cache_refill, ARMV8_PMUV3_PERFCTR_L3D_CACHE_REFILL);
366 ARMV8_EVENT_ATTR(l3d_cache, ARMV8_PMUV3_PERFCTR_L3D_CACHE);
367 ARMV8_EVENT_ATTR(l3d_cache_wb, ARMV8_PMUV3_PERFCTR_L3D_CACHE_WB);
368 ARMV8_EVENT_ATTR(l2d_tlb_refill, ARMV8_PMUV3_PERFCTR_L2D_TLB_REFILL);
369 ARMV8_EVENT_ATTR(l2i_tlb_refill, ARMV8_PMUV3_PERFCTR_L2I_TLB_REFILL);
370 ARMV8_EVENT_ATTR(l2d_tlb, ARMV8_PMUV3_PERFCTR_L2D_TLB);
371 ARMV8_EVENT_ATTR(l2i_tlb, ARMV8_PMUV3_PERFCTR_L2I_TLB);
373 static 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,
404 &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,
419 &armv8_event_attr_l2i_tlb_refill.attr.attr,
420 &armv8_event_attr_l2d_tlb.attr.attr,
421 &armv8_event_attr_l2i_tlb.attr.attr,
422 NULL,
425 static umode_t
426 armv8pmu_event_attr_is_visible(struct kobject *kobj,
427 struct attribute *attr, int unused)
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;
434 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
436 if (test_bit(pmu_attr->id, cpu_pmu->pmceid_bitmap))
437 return attr->mode;
439 return 0;
442 static struct attribute_group armv8_pmuv3_events_attr_group = {
443 .name = "events",
444 .attrs = armv8_pmuv3_event_attrs,
445 .is_visible = armv8pmu_event_attr_is_visible,
448 PMU_FORMAT_ATTR(event, "config:0-15");
450 static struct attribute *armv8_pmuv3_format_attrs[] = {
451 &format_attr_event.attr,
452 NULL,
455 static struct attribute_group armv8_pmuv3_format_attr_group = {
456 .name = "format",
457 .attrs = armv8_pmuv3_format_attrs,
461 * Perf Events' indices
463 #define ARMV8_IDX_CYCLE_COUNTER 0
464 #define ARMV8_IDX_COUNTER0 1
465 #define ARMV8_IDX_COUNTER_LAST(cpu_pmu) \
466 (ARMV8_IDX_CYCLE_COUNTER + cpu_pmu->num_events - 1)
469 * ARMv8 low level PMU access
473 * Perf Event to low level counters mapping
475 #define ARMV8_IDX_TO_COUNTER(x) \
476 (((x) - ARMV8_IDX_COUNTER0) & ARMV8_PMU_COUNTER_MASK)
478 static inline u32 armv8pmu_pmcr_read(void)
480 return read_sysreg(pmcr_el0);
483 static inline void armv8pmu_pmcr_write(u32 val)
485 val &= ARMV8_PMU_PMCR_MASK;
486 isb();
487 write_sysreg(val, pmcr_el0);
490 static inline int armv8pmu_has_overflowed(u32 pmovsr)
492 return pmovsr & ARMV8_PMU_OVERFLOWED_MASK;
495 static inline int armv8pmu_counter_valid(struct arm_pmu *cpu_pmu, int idx)
497 return idx >= ARMV8_IDX_CYCLE_COUNTER &&
498 idx <= ARMV8_IDX_COUNTER_LAST(cpu_pmu);
501 static inline int armv8pmu_counter_has_overflowed(u32 pmnc, int idx)
503 return pmnc & BIT(ARMV8_IDX_TO_COUNTER(idx));
506 static inline int armv8pmu_select_counter(int idx)
508 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
509 write_sysreg(counter, pmselr_el0);
510 isb();
512 return idx;
515 static inline u32 armv8pmu_read_counter(struct perf_event *event)
517 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
518 struct hw_perf_event *hwc = &event->hw;
519 int idx = hwc->idx;
520 u32 value = 0;
522 if (!armv8pmu_counter_valid(cpu_pmu, idx))
523 pr_err("CPU%u reading wrong counter %d\n",
524 smp_processor_id(), idx);
525 else if (idx == ARMV8_IDX_CYCLE_COUNTER)
526 value = read_sysreg(pmccntr_el0);
527 else if (armv8pmu_select_counter(idx) == idx)
528 value = read_sysreg(pmxevcntr_el0);
530 return value;
533 static inline void armv8pmu_write_counter(struct perf_event *event, u32 value)
535 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
536 struct hw_perf_event *hwc = &event->hw;
537 int idx = hwc->idx;
539 if (!armv8pmu_counter_valid(cpu_pmu, idx))
540 pr_err("CPU%u writing wrong counter %d\n",
541 smp_processor_id(), idx);
542 else if (idx == ARMV8_IDX_CYCLE_COUNTER) {
544 * Set the upper 32bits as this is a 64bit counter but we only
545 * count using the lower 32bits and we want an interrupt when
546 * it overflows.
548 u64 value64 = 0xffffffff00000000ULL | value;
550 write_sysreg(value64, pmccntr_el0);
551 } else if (armv8pmu_select_counter(idx) == idx)
552 write_sysreg(value, pmxevcntr_el0);
555 static inline void armv8pmu_write_evtype(int idx, u32 val)
557 if (armv8pmu_select_counter(idx) == idx) {
558 val &= ARMV8_PMU_EVTYPE_MASK;
559 write_sysreg(val, pmxevtyper_el0);
563 static inline int armv8pmu_enable_counter(int idx)
565 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
566 write_sysreg(BIT(counter), pmcntenset_el0);
567 return idx;
570 static inline int armv8pmu_disable_counter(int idx)
572 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
573 write_sysreg(BIT(counter), pmcntenclr_el0);
574 return idx;
577 static inline int armv8pmu_enable_intens(int idx)
579 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
580 write_sysreg(BIT(counter), pmintenset_el1);
581 return idx;
584 static inline int armv8pmu_disable_intens(int idx)
586 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
587 write_sysreg(BIT(counter), pmintenclr_el1);
588 isb();
589 /* Clear the overflow flag in case an interrupt is pending. */
590 write_sysreg(BIT(counter), pmovsclr_el0);
591 isb();
593 return idx;
596 static inline u32 armv8pmu_getreset_flags(void)
598 u32 value;
600 /* Read */
601 value = read_sysreg(pmovsclr_el0);
603 /* Write to clear flags */
604 value &= ARMV8_PMU_OVSR_MASK;
605 write_sysreg(value, pmovsclr_el0);
607 return value;
610 static void armv8pmu_enable_event(struct perf_event *event)
612 unsigned long flags;
613 struct hw_perf_event *hwc = &event->hw;
614 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
615 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
616 int idx = hwc->idx;
619 * Enable counter and interrupt, and set the counter to count
620 * the event that we're interested in.
622 raw_spin_lock_irqsave(&events->pmu_lock, flags);
625 * Disable counter
627 armv8pmu_disable_counter(idx);
630 * Set event (if destined for PMNx counters).
632 armv8pmu_write_evtype(idx, hwc->config_base);
635 * Enable interrupt for this counter
637 armv8pmu_enable_intens(idx);
640 * Enable counter
642 armv8pmu_enable_counter(idx);
644 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
647 static void armv8pmu_disable_event(struct perf_event *event)
649 unsigned long flags;
650 struct hw_perf_event *hwc = &event->hw;
651 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
652 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
653 int idx = hwc->idx;
656 * Disable counter and interrupt
658 raw_spin_lock_irqsave(&events->pmu_lock, flags);
661 * Disable counter
663 armv8pmu_disable_counter(idx);
666 * Disable interrupt for this counter
668 armv8pmu_disable_intens(idx);
670 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
673 static void armv8pmu_start(struct arm_pmu *cpu_pmu)
675 unsigned long flags;
676 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
678 raw_spin_lock_irqsave(&events->pmu_lock, flags);
679 /* Enable all counters */
680 armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMU_PMCR_E);
681 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
684 static void armv8pmu_stop(struct arm_pmu *cpu_pmu)
686 unsigned long flags;
687 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
689 raw_spin_lock_irqsave(&events->pmu_lock, flags);
690 /* Disable all counters */
691 armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMU_PMCR_E);
692 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
695 static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
697 u32 pmovsr;
698 struct perf_sample_data data;
699 struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
700 struct pt_regs *regs;
701 int idx;
704 * Get and reset the IRQ flags
706 pmovsr = armv8pmu_getreset_flags();
709 * Did an overflow occur?
711 if (!armv8pmu_has_overflowed(pmovsr))
712 return IRQ_NONE;
715 * Handle the counter(s) overflow(s)
717 regs = get_irq_regs();
720 * Stop the PMU while processing the counter overflows
721 * to prevent skews in group events.
723 armv8pmu_stop(cpu_pmu);
724 for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
725 struct perf_event *event = cpuc->events[idx];
726 struct hw_perf_event *hwc;
728 /* Ignore if we don't have an event. */
729 if (!event)
730 continue;
733 * We have a single interrupt for all counters. Check that
734 * each counter has overflowed before we process it.
736 if (!armv8pmu_counter_has_overflowed(pmovsr, idx))
737 continue;
739 hwc = &event->hw;
740 armpmu_event_update(event);
741 perf_sample_data_init(&data, 0, hwc->last_period);
742 if (!armpmu_event_set_period(event))
743 continue;
745 if (perf_event_overflow(event, &data, regs))
746 cpu_pmu->disable(event);
748 armv8pmu_start(cpu_pmu);
751 * Handle the pending perf events.
753 * Note: this call *must* be run with interrupts disabled. For
754 * platforms that can have the PMU interrupts raised as an NMI, this
755 * will not work.
757 irq_work_run();
759 return IRQ_HANDLED;
762 static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
763 struct perf_event *event)
765 int idx;
766 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
767 struct hw_perf_event *hwc = &event->hw;
768 unsigned long evtype = hwc->config_base & ARMV8_PMU_EVTYPE_EVENT;
770 /* Always prefer to place a cycle counter into the cycle counter. */
771 if (evtype == ARMV8_PMUV3_PERFCTR_CPU_CYCLES) {
772 if (!test_and_set_bit(ARMV8_IDX_CYCLE_COUNTER, cpuc->used_mask))
773 return ARMV8_IDX_CYCLE_COUNTER;
777 * Otherwise use events counters
779 for (idx = ARMV8_IDX_COUNTER0; idx < cpu_pmu->num_events; ++idx) {
780 if (!test_and_set_bit(idx, cpuc->used_mask))
781 return idx;
784 /* The counters are all in use. */
785 return -EAGAIN;
789 * Add an event filter to a given event. This will only work for PMUv2 PMUs.
791 static int armv8pmu_set_event_filter(struct hw_perf_event *event,
792 struct perf_event_attr *attr)
794 unsigned long config_base = 0;
796 if (attr->exclude_idle)
797 return -EPERM;
800 * If we're running in hyp mode, then we *are* the hypervisor.
801 * Therefore we ignore exclude_hv in this configuration, since
802 * there's no hypervisor to sample anyway. This is consistent
803 * with other architectures (x86 and Power).
805 if (is_kernel_in_hyp_mode()) {
806 if (!attr->exclude_kernel)
807 config_base |= ARMV8_PMU_INCLUDE_EL2;
808 } else {
809 if (attr->exclude_kernel)
810 config_base |= ARMV8_PMU_EXCLUDE_EL1;
811 if (!attr->exclude_hv)
812 config_base |= ARMV8_PMU_INCLUDE_EL2;
814 if (attr->exclude_user)
815 config_base |= ARMV8_PMU_EXCLUDE_EL0;
818 * Install the filter into config_base as this is used to
819 * construct the event type.
821 event->config_base = config_base;
823 return 0;
826 static void armv8pmu_reset(void *info)
828 struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
829 u32 idx, nb_cnt = cpu_pmu->num_events;
831 /* The counter and interrupt enable registers are unknown at reset. */
832 for (idx = ARMV8_IDX_CYCLE_COUNTER; idx < nb_cnt; ++idx) {
833 armv8pmu_disable_counter(idx);
834 armv8pmu_disable_intens(idx);
838 * Initialize & Reset PMNC. Request overflow interrupt for
839 * 64 bit cycle counter but cheat in armv8pmu_write_counter().
841 armv8pmu_pmcr_write(ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C |
842 ARMV8_PMU_PMCR_LC);
845 static int __armv8_pmuv3_map_event(struct perf_event *event,
846 const unsigned (*extra_event_map)
847 [PERF_COUNT_HW_MAX],
848 const unsigned (*extra_cache_map)
849 [PERF_COUNT_HW_CACHE_MAX]
850 [PERF_COUNT_HW_CACHE_OP_MAX]
851 [PERF_COUNT_HW_CACHE_RESULT_MAX])
853 int hw_event_id;
854 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
856 hw_event_id = armpmu_map_event(event, &armv8_pmuv3_perf_map,
857 &armv8_pmuv3_perf_cache_map,
858 ARMV8_PMU_EVTYPE_EVENT);
860 /* Onl expose micro/arch events supported by this PMU */
861 if ((hw_event_id > 0) && (hw_event_id < ARMV8_PMUV3_MAX_COMMON_EVENTS)
862 && test_bit(hw_event_id, armpmu->pmceid_bitmap)) {
863 return hw_event_id;
866 return armpmu_map_event(event, extra_event_map, extra_cache_map,
867 ARMV8_PMU_EVTYPE_EVENT);
870 static int armv8_pmuv3_map_event(struct perf_event *event)
872 return __armv8_pmuv3_map_event(event, NULL, NULL);
875 static int armv8_a53_map_event(struct perf_event *event)
877 return __armv8_pmuv3_map_event(event, NULL, &armv8_a53_perf_cache_map);
880 static int armv8_a57_map_event(struct perf_event *event)
882 return __armv8_pmuv3_map_event(event, NULL, &armv8_a57_perf_cache_map);
885 static int armv8_a73_map_event(struct perf_event *event)
887 return __armv8_pmuv3_map_event(event, NULL, &armv8_a73_perf_cache_map);
890 static int armv8_thunder_map_event(struct perf_event *event)
892 return __armv8_pmuv3_map_event(event, NULL,
893 &armv8_thunder_perf_cache_map);
896 static int armv8_vulcan_map_event(struct perf_event *event)
898 return __armv8_pmuv3_map_event(event, NULL,
899 &armv8_vulcan_perf_cache_map);
902 struct armv8pmu_probe_info {
903 struct arm_pmu *pmu;
904 bool present;
907 static void __armv8pmu_probe_pmu(void *info)
909 struct armv8pmu_probe_info *probe = info;
910 struct arm_pmu *cpu_pmu = probe->pmu;
911 u64 dfr0;
912 u32 pmceid[2];
913 int pmuver;
915 dfr0 = read_sysreg(id_aa64dfr0_el1);
916 pmuver = cpuid_feature_extract_unsigned_field(dfr0,
917 ID_AA64DFR0_PMUVER_SHIFT);
918 if (pmuver == 0xf || pmuver == 0)
919 return;
921 probe->present = true;
923 /* Read the nb of CNTx counters supported from PMNC */
924 cpu_pmu->num_events = (armv8pmu_pmcr_read() >> ARMV8_PMU_PMCR_N_SHIFT)
925 & ARMV8_PMU_PMCR_N_MASK;
927 /* Add the CPU cycles counter */
928 cpu_pmu->num_events += 1;
930 pmceid[0] = read_sysreg(pmceid0_el0);
931 pmceid[1] = read_sysreg(pmceid1_el0);
933 bitmap_from_arr32(cpu_pmu->pmceid_bitmap,
934 pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
937 static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
939 struct armv8pmu_probe_info probe = {
940 .pmu = cpu_pmu,
941 .present = false,
943 int ret;
945 ret = smp_call_function_any(&cpu_pmu->supported_cpus,
946 __armv8pmu_probe_pmu,
947 &probe, 1);
948 if (ret)
949 return ret;
951 return probe.present ? 0 : -ENODEV;
954 static int armv8_pmu_init(struct arm_pmu *cpu_pmu)
956 int ret = armv8pmu_probe_pmu(cpu_pmu);
957 if (ret)
958 return ret;
960 cpu_pmu->handle_irq = armv8pmu_handle_irq,
961 cpu_pmu->enable = armv8pmu_enable_event,
962 cpu_pmu->disable = armv8pmu_disable_event,
963 cpu_pmu->read_counter = armv8pmu_read_counter,
964 cpu_pmu->write_counter = armv8pmu_write_counter,
965 cpu_pmu->get_event_idx = armv8pmu_get_event_idx,
966 cpu_pmu->start = armv8pmu_start,
967 cpu_pmu->stop = armv8pmu_stop,
968 cpu_pmu->reset = armv8pmu_reset,
969 cpu_pmu->max_period = (1LLU << 32) - 1,
970 cpu_pmu->set_event_filter = armv8pmu_set_event_filter;
972 return 0;
975 static int armv8_pmuv3_init(struct arm_pmu *cpu_pmu)
977 int ret = armv8_pmu_init(cpu_pmu);
978 if (ret)
979 return ret;
981 cpu_pmu->name = "armv8_pmuv3";
982 cpu_pmu->map_event = armv8_pmuv3_map_event;
983 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
984 &armv8_pmuv3_events_attr_group;
985 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
986 &armv8_pmuv3_format_attr_group;
988 return 0;
991 static int armv8_a35_pmu_init(struct arm_pmu *cpu_pmu)
993 int ret = armv8_pmu_init(cpu_pmu);
994 if (ret)
995 return ret;
997 cpu_pmu->name = "armv8_cortex_a35";
998 cpu_pmu->map_event = armv8_a53_map_event;
999 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1000 &armv8_pmuv3_events_attr_group;
1001 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1002 &armv8_pmuv3_format_attr_group;
1004 return 0;
1007 static int armv8_a53_pmu_init(struct arm_pmu *cpu_pmu)
1009 int ret = armv8_pmu_init(cpu_pmu);
1010 if (ret)
1011 return ret;
1013 cpu_pmu->name = "armv8_cortex_a53";
1014 cpu_pmu->map_event = armv8_a53_map_event;
1015 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1016 &armv8_pmuv3_events_attr_group;
1017 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1018 &armv8_pmuv3_format_attr_group;
1020 return 0;
1023 static int armv8_a57_pmu_init(struct arm_pmu *cpu_pmu)
1025 int ret = armv8_pmu_init(cpu_pmu);
1026 if (ret)
1027 return ret;
1029 cpu_pmu->name = "armv8_cortex_a57";
1030 cpu_pmu->map_event = armv8_a57_map_event;
1031 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1032 &armv8_pmuv3_events_attr_group;
1033 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1034 &armv8_pmuv3_format_attr_group;
1036 return 0;
1039 static int armv8_a72_pmu_init(struct arm_pmu *cpu_pmu)
1041 int ret = armv8_pmu_init(cpu_pmu);
1042 if (ret)
1043 return ret;
1045 cpu_pmu->name = "armv8_cortex_a72";
1046 cpu_pmu->map_event = armv8_a57_map_event;
1047 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1048 &armv8_pmuv3_events_attr_group;
1049 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1050 &armv8_pmuv3_format_attr_group;
1052 return 0;
1055 static int armv8_a73_pmu_init(struct arm_pmu *cpu_pmu)
1057 int ret = armv8_pmu_init(cpu_pmu);
1058 if (ret)
1059 return ret;
1061 cpu_pmu->name = "armv8_cortex_a73";
1062 cpu_pmu->map_event = armv8_a73_map_event;
1063 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1064 &armv8_pmuv3_events_attr_group;
1065 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1066 &armv8_pmuv3_format_attr_group;
1068 return 0;
1071 static int armv8_thunder_pmu_init(struct arm_pmu *cpu_pmu)
1073 int ret = armv8_pmu_init(cpu_pmu);
1074 if (ret)
1075 return ret;
1077 cpu_pmu->name = "armv8_cavium_thunder";
1078 cpu_pmu->map_event = armv8_thunder_map_event;
1079 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1080 &armv8_pmuv3_events_attr_group;
1081 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1082 &armv8_pmuv3_format_attr_group;
1084 return 0;
1087 static int armv8_vulcan_pmu_init(struct arm_pmu *cpu_pmu)
1089 int ret = armv8_pmu_init(cpu_pmu);
1090 if (ret)
1091 return ret;
1093 cpu_pmu->name = "armv8_brcm_vulcan";
1094 cpu_pmu->map_event = armv8_vulcan_map_event;
1095 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1096 &armv8_pmuv3_events_attr_group;
1097 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1098 &armv8_pmuv3_format_attr_group;
1100 return 0;
1103 static const struct of_device_id armv8_pmu_of_device_ids[] = {
1104 {.compatible = "arm,armv8-pmuv3", .data = armv8_pmuv3_init},
1105 {.compatible = "arm,cortex-a35-pmu", .data = armv8_a35_pmu_init},
1106 {.compatible = "arm,cortex-a53-pmu", .data = armv8_a53_pmu_init},
1107 {.compatible = "arm,cortex-a57-pmu", .data = armv8_a57_pmu_init},
1108 {.compatible = "arm,cortex-a72-pmu", .data = armv8_a72_pmu_init},
1109 {.compatible = "arm,cortex-a73-pmu", .data = armv8_a73_pmu_init},
1110 {.compatible = "cavium,thunder-pmu", .data = armv8_thunder_pmu_init},
1111 {.compatible = "brcm,vulcan-pmu", .data = armv8_vulcan_pmu_init},
1115 static int armv8_pmu_device_probe(struct platform_device *pdev)
1117 return arm_pmu_device_probe(pdev, armv8_pmu_of_device_ids, NULL);
1120 static struct platform_driver armv8_pmu_driver = {
1121 .driver = {
1122 .name = ARMV8_PMU_PDEV_NAME,
1123 .of_match_table = armv8_pmu_of_device_ids,
1125 .probe = armv8_pmu_device_probe,
1128 static int __init armv8_pmu_driver_init(void)
1130 if (acpi_disabled)
1131 return platform_driver_register(&armv8_pmu_driver);
1132 else
1133 return arm_pmu_acpi_probe(armv8_pmuv3_init);
1135 device_initcall(armv8_pmu_driver_init)