blob: a9310a69fffd4d453b760a15d16cc8e9cd1a90e8 [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.
34 * Common event types.
35 */
Will Deacon03089682012-03-05 11:49:32 +000036
Drew Richardson90381cb2015-10-22 07:07:01 -070037/* Required events. */
Ashok Kumar03598fd2016-04-21 05:58:41 -070038#define ARMV8_PMUV3_PERFCTR_SW_INCR 0x00
39#define ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL 0x03
40#define ARMV8_PMUV3_PERFCTR_L1D_CACHE 0x04
41#define ARMV8_PMUV3_PERFCTR_BR_MIS_PRED 0x10
42#define ARMV8_PMUV3_PERFCTR_CPU_CYCLES 0x11
43#define ARMV8_PMUV3_PERFCTR_BR_PRED 0x12
Will Deacon03089682012-03-05 11:49:32 +000044
Drew Richardson90381cb2015-10-22 07:07:01 -070045/* At least one of the following is required. */
Ashok Kumar03598fd2016-04-21 05:58:41 -070046#define ARMV8_PMUV3_PERFCTR_INST_RETIRED 0x08
47#define ARMV8_PMUV3_PERFCTR_INST_SPEC 0x1B
Will Deacon03089682012-03-05 11:49:32 +000048
Drew Richardson90381cb2015-10-22 07:07:01 -070049/* Common architectural events. */
Ashok Kumar03598fd2016-04-21 05:58:41 -070050#define ARMV8_PMUV3_PERFCTR_LD_RETIRED 0x06
51#define ARMV8_PMUV3_PERFCTR_ST_RETIRED 0x07
Drew Richardson90381cb2015-10-22 07:07:01 -070052#define ARMV8_PMUV3_PERFCTR_EXC_TAKEN 0x09
Ashok Kumar03598fd2016-04-21 05:58:41 -070053#define ARMV8_PMUV3_PERFCTR_EXC_RETURN 0x0A
54#define ARMV8_PMUV3_PERFCTR_CID_WRITE_RETIRED 0x0B
55#define ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED 0x0C
56#define ARMV8_PMUV3_PERFCTR_BR_IMMED_RETIRED 0x0D
57#define ARMV8_PMUV3_PERFCTR_BR_RETURN_RETIRED 0x0E
58#define ARMV8_PMUV3_PERFCTR_UNALIGNED_LDST_RETIRED 0x0F
59#define ARMV8_PMUV3_PERFCTR_TTBR_WRITE_RETIRED 0x1C
Drew Richardson9e9caa62015-10-22 07:07:32 -070060#define ARMV8_PMUV3_PERFCTR_CHAIN 0x1E
61#define ARMV8_PMUV3_PERFCTR_BR_RETIRED 0x21
Drew Richardson90381cb2015-10-22 07:07:01 -070062
63/* Common microarchitectural events. */
Ashok Kumar03598fd2016-04-21 05:58:41 -070064#define ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL 0x01
65#define ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL 0x02
66#define ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL 0x05
Drew Richardson90381cb2015-10-22 07:07:01 -070067#define ARMV8_PMUV3_PERFCTR_MEM_ACCESS 0x13
Ashok Kumar03598fd2016-04-21 05:58:41 -070068#define ARMV8_PMUV3_PERFCTR_L1I_CACHE 0x14
69#define ARMV8_PMUV3_PERFCTR_L1D_CACHE_WB 0x15
70#define ARMV8_PMUV3_PERFCTR_L2D_CACHE 0x16
71#define ARMV8_PMUV3_PERFCTR_L2D_CACHE_REFILL 0x17
72#define ARMV8_PMUV3_PERFCTR_L2D_CACHE_WB 0x18
Drew Richardson90381cb2015-10-22 07:07:01 -070073#define ARMV8_PMUV3_PERFCTR_BUS_ACCESS 0x19
Ashok Kumar03598fd2016-04-21 05:58:41 -070074#define ARMV8_PMUV3_PERFCTR_MEMORY_ERROR 0x1A
Drew Richardson90381cb2015-10-22 07:07:01 -070075#define ARMV8_PMUV3_PERFCTR_BUS_CYCLES 0x1D
Drew Richardson9e9caa62015-10-22 07:07:32 -070076#define ARMV8_PMUV3_PERFCTR_L1D_CACHE_ALLOCATE 0x1F
77#define ARMV8_PMUV3_PERFCTR_L2D_CACHE_ALLOCATE 0x20
78#define ARMV8_PMUV3_PERFCTR_BR_MIS_PRED_RETIRED 0x22
79#define ARMV8_PMUV3_PERFCTR_STALL_FRONTEND 0x23
80#define ARMV8_PMUV3_PERFCTR_STALL_BACKEND 0x24
81#define ARMV8_PMUV3_PERFCTR_L1D_TLB 0x25
82#define ARMV8_PMUV3_PERFCTR_L1I_TLB 0x26
83#define ARMV8_PMUV3_PERFCTR_L2I_CACHE 0x27
84#define ARMV8_PMUV3_PERFCTR_L2I_CACHE_REFILL 0x28
85#define ARMV8_PMUV3_PERFCTR_L3D_CACHE_ALLOCATE 0x29
86#define ARMV8_PMUV3_PERFCTR_L3D_CACHE_REFILL 0x2A
87#define ARMV8_PMUV3_PERFCTR_L3D_CACHE 0x2B
88#define ARMV8_PMUV3_PERFCTR_L3D_CACHE_WB 0x2C
89#define ARMV8_PMUV3_PERFCTR_L2D_TLB_REFILL 0x2D
Ashok Kumar03598fd2016-04-21 05:58:41 -070090#define ARMV8_PMUV3_PERFCTR_L2I_TLB_REFILL 0x2E
Drew Richardson9e9caa62015-10-22 07:07:32 -070091#define ARMV8_PMUV3_PERFCTR_L2D_TLB 0x2F
Ashok Kumar03598fd2016-04-21 05:58:41 -070092#define ARMV8_PMUV3_PERFCTR_L2I_TLB 0x30
Will Deacon03089682012-03-05 11:49:32 +000093
Ashok Kumar03598fd2016-04-21 05:58:41 -070094/* ARMv8 recommended implementation defined event types */
95#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD 0x40
96#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR 0x41
97#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD 0x42
98#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR 0x43
Ashok Kumar0893f742016-04-21 05:58:42 -070099#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_INNER 0x44
100#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_OUTER 0x45
101#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WB_VICTIM 0x46
102#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WB_CLEAN 0x47
103#define ARMV8_IMPDEF_PERFCTR_L1D_CACHE_INVAL 0x48
104
Ashok Kumar03598fd2016-04-21 05:58:41 -0700105#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD 0x4C
106#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR 0x4D
107#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD 0x4E
108#define ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR 0x4F
Ashok Kumar0893f742016-04-21 05:58:42 -0700109#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_RD 0x50
110#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_WR 0x51
111#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_REFILL_RD 0x52
112#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_REFILL_WR 0x53
113
114#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_WB_VICTIM 0x56
115#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_WB_CLEAN 0x57
116#define ARMV8_IMPDEF_PERFCTR_L2D_CACHE_INVAL 0x58
117
118#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_REFILL_RD 0x5C
119#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_REFILL_WR 0x5D
120#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_RD 0x5E
121#define ARMV8_IMPDEF_PERFCTR_L2D_TLB_WR 0x5F
122
123#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD 0x60
124#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR 0x61
125#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_SHARED 0x62
126#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_NOT_SHARED 0x63
127#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_NORMAL 0x64
128#define ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_PERIPH 0x65
129
130#define ARMV8_IMPDEF_PERFCTR_MEM_ACCESS_RD 0x66
131#define ARMV8_IMPDEF_PERFCTR_MEM_ACCESS_WR 0x67
132#define ARMV8_IMPDEF_PERFCTR_UNALIGNED_LD_SPEC 0x68
133#define ARMV8_IMPDEF_PERFCTR_UNALIGNED_ST_SPEC 0x69
134#define ARMV8_IMPDEF_PERFCTR_UNALIGNED_LDST_SPEC 0x6A
135
136#define ARMV8_IMPDEF_PERFCTR_LDREX_SPEC 0x6C
137#define ARMV8_IMPDEF_PERFCTR_STREX_PASS_SPEC 0x6D
138#define ARMV8_IMPDEF_PERFCTR_STREX_FAIL_SPEC 0x6E
139#define ARMV8_IMPDEF_PERFCTR_STREX_SPEC 0x6F
140#define ARMV8_IMPDEF_PERFCTR_LD_SPEC 0x70
141#define ARMV8_IMPDEF_PERFCTR_ST_SPEC 0x71
142#define ARMV8_IMPDEF_PERFCTR_LDST_SPEC 0x72
143#define ARMV8_IMPDEF_PERFCTR_DP_SPEC 0x73
144#define ARMV8_IMPDEF_PERFCTR_ASE_SPEC 0x74
145#define ARMV8_IMPDEF_PERFCTR_VFP_SPEC 0x75
146#define ARMV8_IMPDEF_PERFCTR_PC_WRITE_SPEC 0x76
147#define ARMV8_IMPDEF_PERFCTR_CRYPTO_SPEC 0x77
148#define ARMV8_IMPDEF_PERFCTR_BR_IMMED_SPEC 0x78
149#define ARMV8_IMPDEF_PERFCTR_BR_RETURN_SPEC 0x79
150#define ARMV8_IMPDEF_PERFCTR_BR_INDIRECT_SPEC 0x7A
151
152#define ARMV8_IMPDEF_PERFCTR_ISB_SPEC 0x7C
153#define ARMV8_IMPDEF_PERFCTR_DSB_SPEC 0x7D
154#define ARMV8_IMPDEF_PERFCTR_DMB_SPEC 0x7E
155
156#define ARMV8_IMPDEF_PERFCTR_EXC_UNDEF 0x81
157#define ARMV8_IMPDEF_PERFCTR_EXC_SVC 0x82
158#define ARMV8_IMPDEF_PERFCTR_EXC_PABORT 0x83
159#define ARMV8_IMPDEF_PERFCTR_EXC_DABORT 0x84
160
161#define ARMV8_IMPDEF_PERFCTR_EXC_IRQ 0x86
162#define ARMV8_IMPDEF_PERFCTR_EXC_FIQ 0x87
163#define ARMV8_IMPDEF_PERFCTR_EXC_SMC 0x88
164
165#define ARMV8_IMPDEF_PERFCTR_EXC_HVC 0x8A
166#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_PABORT 0x8B
167#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_DABORT 0x8C
168#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_OTHER 0x8D
169#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_IRQ 0x8E
170#define ARMV8_IMPDEF_PERFCTR_EXC_TRAP_FIQ 0x8F
171#define ARMV8_IMPDEF_PERFCTR_RC_LD_SPEC 0x90
172#define ARMV8_IMPDEF_PERFCTR_RC_ST_SPEC 0x91
173
174#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_RD 0xA0
175#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_WR 0xA1
176#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_REFILL_RD 0xA2
177#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_REFILL_WR 0xA3
178
179#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_WB_VICTIM 0xA6
180#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_WB_CLEAN 0xA7
181#define ARMV8_IMPDEF_PERFCTR_L3D_CACHE_INVAL 0xA8
Jan Glauber5f140cc2016-02-18 17:50:10 +0100182
Mark Rutlandac82d122015-10-02 10:55:04 +0100183/* ARMv8 Cortex-A53 specific event types. */
Ashok Kumar03598fd2016-04-21 05:58:41 -0700184#define ARMV8_A53_PERFCTR_PREF_LINEFILL 0xC2
Mark Rutlandac82d122015-10-02 10:55:04 +0100185
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100186/* ARMv8 Cavium ThunderX specific event types. */
Ashok Kumar03598fd2016-04-21 05:58:41 -0700187#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST 0xE9
188#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS 0xEA
189#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS 0xEB
190#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS 0xEC
191#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS 0xED
Mark Rutland62a4dda2015-10-02 10:55:05 +0100192
Will Deacon03089682012-03-05 11:49:32 +0000193/* PMUv3 HW events mapping. */
Jeremy Linton236b9b912016-09-14 17:32:30 -0500194
195/*
196 * ARMv8 Architectural defined events, not all of these may
197 * be supported on any given implementation. Undefined events will
198 * be disabled at run-time.
199 */
Will Deacon03089682012-03-05 11:49:32 +0000200static const unsigned armv8_pmuv3_perf_map[PERF_COUNT_HW_MAX] = {
Mark Rutlandae2fb7e2015-07-21 11:36:39 +0100201 PERF_MAP_ALL_UNSUPPORTED,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700202 [PERF_COUNT_HW_CPU_CYCLES] = ARMV8_PMUV3_PERFCTR_CPU_CYCLES,
203 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_INST_RETIRED,
204 [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
205 [PERF_COUNT_HW_CACHE_MISSES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
Jeremy Linton236b9b912016-09-14 17:32:30 -0500206 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700207 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Jeremy Linton236b9b912016-09-14 17:32:30 -0500208 [PERF_COUNT_HW_BUS_CYCLES] = ARMV8_PMUV3_PERFCTR_BUS_CYCLES,
209 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV8_PMUV3_PERFCTR_STALL_FRONTEND,
210 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV8_PMUV3_PERFCTR_STALL_BACKEND,
Will Deacon03089682012-03-05 11:49:32 +0000211};
212
Mark Rutlandac82d122015-10-02 10:55:04 +0100213/* ARM Cortex-A53 HW events mapping. */
214static const unsigned armv8_a53_perf_map[PERF_COUNT_HW_MAX] = {
215 PERF_MAP_ALL_UNSUPPORTED,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700216 [PERF_COUNT_HW_CPU_CYCLES] = ARMV8_PMUV3_PERFCTR_CPU_CYCLES,
217 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_INST_RETIRED,
218 [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
219 [PERF_COUNT_HW_CACHE_MISSES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
220 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED,
221 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Mark Rutlandac82d122015-10-02 10:55:04 +0100222 [PERF_COUNT_HW_BUS_CYCLES] = ARMV8_PMUV3_PERFCTR_BUS_CYCLES,
223};
224
Will Deacon5d7ee872015-12-22 14:45:35 +0000225/* ARM Cortex-A57 and Cortex-A72 events mapping. */
Mark Rutland62a4dda2015-10-02 10:55:05 +0100226static const unsigned armv8_a57_perf_map[PERF_COUNT_HW_MAX] = {
227 PERF_MAP_ALL_UNSUPPORTED,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700228 [PERF_COUNT_HW_CPU_CYCLES] = ARMV8_PMUV3_PERFCTR_CPU_CYCLES,
229 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_INST_RETIRED,
230 [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
231 [PERF_COUNT_HW_CACHE_MISSES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
232 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100233 [PERF_COUNT_HW_BUS_CYCLES] = ARMV8_PMUV3_PERFCTR_BUS_CYCLES,
234};
235
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100236static const unsigned armv8_thunder_perf_map[PERF_COUNT_HW_MAX] = {
237 PERF_MAP_ALL_UNSUPPORTED,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700238 [PERF_COUNT_HW_CPU_CYCLES] = ARMV8_PMUV3_PERFCTR_CPU_CYCLES,
239 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_INST_RETIRED,
240 [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
241 [PERF_COUNT_HW_CACHE_MISSES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
242 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED,
243 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100244 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV8_PMUV3_PERFCTR_STALL_FRONTEND,
245 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV8_PMUV3_PERFCTR_STALL_BACKEND,
246};
247
Ashok Kumar201a72b2016-04-21 05:58:45 -0700248/* Broadcom Vulcan events mapping */
249static const unsigned armv8_vulcan_perf_map[PERF_COUNT_HW_MAX] = {
250 PERF_MAP_ALL_UNSUPPORTED,
251 [PERF_COUNT_HW_CPU_CYCLES] = ARMV8_PMUV3_PERFCTR_CPU_CYCLES,
252 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_INST_RETIRED,
253 [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
254 [PERF_COUNT_HW_CACHE_MISSES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
255 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_BR_RETIRED,
256 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
257 [PERF_COUNT_HW_BUS_CYCLES] = ARMV8_PMUV3_PERFCTR_BUS_CYCLES,
258 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV8_PMUV3_PERFCTR_STALL_FRONTEND,
259 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV8_PMUV3_PERFCTR_STALL_BACKEND,
260};
261
Will Deacon03089682012-03-05 11:49:32 +0000262static const unsigned armv8_pmuv3_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
263 [PERF_COUNT_HW_CACHE_OP_MAX]
264 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
Mark Rutlandae2fb7e2015-07-21 11:36:39 +0100265 PERF_CACHE_MAP_ALL_UNSUPPORTED,
266
Ashok Kumar03598fd2016-04-21 05:58:41 -0700267 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
268 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
269 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
270 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
Mark Rutlandae2fb7e2015-07-21 11:36:39 +0100271
Jeremy Linton236b9b912016-09-14 17:32:30 -0500272 [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE,
273 [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL,
274
275 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL,
276 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_TLB,
277
278 [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL,
279 [C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB,
280
Ashok Kumar03598fd2016-04-21 05:58:41 -0700281 [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
282 [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
283 [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
284 [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Will Deacon03089682012-03-05 11:49:32 +0000285};
286
Mark Rutlandac82d122015-10-02 10:55:04 +0100287static const unsigned armv8_a53_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
288 [PERF_COUNT_HW_CACHE_OP_MAX]
289 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
290 PERF_CACHE_MAP_ALL_UNSUPPORTED,
291
Ashok Kumar03598fd2016-04-21 05:58:41 -0700292 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
293 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
294 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
295 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
296 [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_A53_PERFCTR_PREF_LINEFILL,
Mark Rutlandac82d122015-10-02 10:55:04 +0100297
Ashok Kumar03598fd2016-04-21 05:58:41 -0700298 [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE,
299 [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL,
Mark Rutlandac82d122015-10-02 10:55:04 +0100300
Ashok Kumar03598fd2016-04-21 05:58:41 -0700301 [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL,
Mark Rutlandac82d122015-10-02 10:55:04 +0100302
Ashok Kumar03598fd2016-04-21 05:58:41 -0700303 [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
304 [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
305 [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
306 [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Mark Rutlandac82d122015-10-02 10:55:04 +0100307};
308
Mark Rutland62a4dda2015-10-02 10:55:05 +0100309static const unsigned armv8_a57_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
310 [PERF_COUNT_HW_CACHE_OP_MAX]
311 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
312 PERF_CACHE_MAP_ALL_UNSUPPORTED,
313
Ashok Kumar03598fd2016-04-21 05:58:41 -0700314 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
315 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
316 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
317 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100318
Ashok Kumar03598fd2016-04-21 05:58:41 -0700319 [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE,
320 [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100321
Ashok Kumar03598fd2016-04-21 05:58:41 -0700322 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
323 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100324
Ashok Kumar03598fd2016-04-21 05:58:41 -0700325 [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100326
Ashok Kumar03598fd2016-04-21 05:58:41 -0700327 [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
328 [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
329 [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
330 [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100331};
332
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100333static const unsigned armv8_thunder_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
334 [PERF_COUNT_HW_CACHE_OP_MAX]
335 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
336 PERF_CACHE_MAP_ALL_UNSUPPORTED,
337
Ashok Kumar03598fd2016-04-21 05:58:41 -0700338 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
339 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
340 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
341 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST,
342 [C(L1D)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS,
343 [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS,
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100344
Ashok Kumar03598fd2016-04-21 05:58:41 -0700345 [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE,
346 [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL,
347 [C(L1I)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS,
348 [C(L1I)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS,
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100349
Ashok Kumar03598fd2016-04-21 05:58:41 -0700350 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
351 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
352 [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
353 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100354
Ashok Kumar03598fd2016-04-21 05:58:41 -0700355 [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100356
Ashok Kumar03598fd2016-04-21 05:58:41 -0700357 [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
358 [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
359 [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
360 [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100361};
362
Ashok Kumar201a72b2016-04-21 05:58:45 -0700363static const unsigned armv8_vulcan_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
364 [PERF_COUNT_HW_CACHE_OP_MAX]
365 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
366 PERF_CACHE_MAP_ALL_UNSUPPORTED,
367
368 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
369 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
370 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
371 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR,
372
373 [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE,
374 [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL,
375
376 [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL,
377 [C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB,
378
379 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
380 [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
381 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
382 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
383
384 [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
385 [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
386 [C(BPU)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
387 [C(BPU)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
388
389 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
390 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
391};
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700392
393static ssize_t
394armv8pmu_events_sysfs_show(struct device *dev,
395 struct device_attribute *attr, char *page)
396{
397 struct perf_pmu_events_attr *pmu_attr;
398
399 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
400
401 return sprintf(page, "event=0x%03llx\n", pmu_attr->id);
402}
403
Drew Richardson9e9caa62015-10-22 07:07:32 -0700404#define ARMV8_EVENT_ATTR_RESOLVE(m) #m
405#define ARMV8_EVENT_ATTR(name, config) \
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700406 PMU_EVENT_ATTR(name, armv8_event_attr_##name, \
407 config, armv8pmu_events_sysfs_show)
Drew Richardson9e9caa62015-10-22 07:07:32 -0700408
Ashok Kumar03598fd2016-04-21 05:58:41 -0700409ARMV8_EVENT_ATTR(sw_incr, ARMV8_PMUV3_PERFCTR_SW_INCR);
410ARMV8_EVENT_ATTR(l1i_cache_refill, ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL);
411ARMV8_EVENT_ATTR(l1i_tlb_refill, ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL);
412ARMV8_EVENT_ATTR(l1d_cache_refill, ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL);
413ARMV8_EVENT_ATTR(l1d_cache, ARMV8_PMUV3_PERFCTR_L1D_CACHE);
414ARMV8_EVENT_ATTR(l1d_tlb_refill, ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL);
415ARMV8_EVENT_ATTR(ld_retired, ARMV8_PMUV3_PERFCTR_LD_RETIRED);
416ARMV8_EVENT_ATTR(st_retired, ARMV8_PMUV3_PERFCTR_ST_RETIRED);
417ARMV8_EVENT_ATTR(inst_retired, ARMV8_PMUV3_PERFCTR_INST_RETIRED);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700418ARMV8_EVENT_ATTR(exc_taken, ARMV8_PMUV3_PERFCTR_EXC_TAKEN);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700419ARMV8_EVENT_ATTR(exc_return, ARMV8_PMUV3_PERFCTR_EXC_RETURN);
420ARMV8_EVENT_ATTR(cid_write_retired, ARMV8_PMUV3_PERFCTR_CID_WRITE_RETIRED);
421ARMV8_EVENT_ATTR(pc_write_retired, ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED);
422ARMV8_EVENT_ATTR(br_immed_retired, ARMV8_PMUV3_PERFCTR_BR_IMMED_RETIRED);
423ARMV8_EVENT_ATTR(br_return_retired, ARMV8_PMUV3_PERFCTR_BR_RETURN_RETIRED);
424ARMV8_EVENT_ATTR(unaligned_ldst_retired, ARMV8_PMUV3_PERFCTR_UNALIGNED_LDST_RETIRED);
425ARMV8_EVENT_ATTR(br_mis_pred, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED);
426ARMV8_EVENT_ATTR(cpu_cycles, ARMV8_PMUV3_PERFCTR_CPU_CYCLES);
427ARMV8_EVENT_ATTR(br_pred, ARMV8_PMUV3_PERFCTR_BR_PRED);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700428ARMV8_EVENT_ATTR(mem_access, ARMV8_PMUV3_PERFCTR_MEM_ACCESS);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700429ARMV8_EVENT_ATTR(l1i_cache, ARMV8_PMUV3_PERFCTR_L1I_CACHE);
430ARMV8_EVENT_ATTR(l1d_cache_wb, ARMV8_PMUV3_PERFCTR_L1D_CACHE_WB);
431ARMV8_EVENT_ATTR(l2d_cache, ARMV8_PMUV3_PERFCTR_L2D_CACHE);
432ARMV8_EVENT_ATTR(l2d_cache_refill, ARMV8_PMUV3_PERFCTR_L2D_CACHE_REFILL);
433ARMV8_EVENT_ATTR(l2d_cache_wb, ARMV8_PMUV3_PERFCTR_L2D_CACHE_WB);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700434ARMV8_EVENT_ATTR(bus_access, ARMV8_PMUV3_PERFCTR_BUS_ACCESS);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700435ARMV8_EVENT_ATTR(memory_error, ARMV8_PMUV3_PERFCTR_MEMORY_ERROR);
436ARMV8_EVENT_ATTR(inst_spec, ARMV8_PMUV3_PERFCTR_INST_SPEC);
437ARMV8_EVENT_ATTR(ttbr_write_retired, ARMV8_PMUV3_PERFCTR_TTBR_WRITE_RETIRED);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700438ARMV8_EVENT_ATTR(bus_cycles, ARMV8_PMUV3_PERFCTR_BUS_CYCLES);
Will Deacon4ba25782016-04-25 15:05:24 +0100439/* Don't expose the chain event in /sys, since it's useless in isolation */
Drew Richardson9e9caa62015-10-22 07:07:32 -0700440ARMV8_EVENT_ATTR(l1d_cache_allocate, ARMV8_PMUV3_PERFCTR_L1D_CACHE_ALLOCATE);
441ARMV8_EVENT_ATTR(l2d_cache_allocate, ARMV8_PMUV3_PERFCTR_L2D_CACHE_ALLOCATE);
442ARMV8_EVENT_ATTR(br_retired, ARMV8_PMUV3_PERFCTR_BR_RETIRED);
443ARMV8_EVENT_ATTR(br_mis_pred_retired, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED_RETIRED);
444ARMV8_EVENT_ATTR(stall_frontend, ARMV8_PMUV3_PERFCTR_STALL_FRONTEND);
445ARMV8_EVENT_ATTR(stall_backend, ARMV8_PMUV3_PERFCTR_STALL_BACKEND);
446ARMV8_EVENT_ATTR(l1d_tlb, ARMV8_PMUV3_PERFCTR_L1D_TLB);
447ARMV8_EVENT_ATTR(l1i_tlb, ARMV8_PMUV3_PERFCTR_L1I_TLB);
448ARMV8_EVENT_ATTR(l2i_cache, ARMV8_PMUV3_PERFCTR_L2I_CACHE);
449ARMV8_EVENT_ATTR(l2i_cache_refill, ARMV8_PMUV3_PERFCTR_L2I_CACHE_REFILL);
450ARMV8_EVENT_ATTR(l3d_cache_allocate, ARMV8_PMUV3_PERFCTR_L3D_CACHE_ALLOCATE);
451ARMV8_EVENT_ATTR(l3d_cache_refill, ARMV8_PMUV3_PERFCTR_L3D_CACHE_REFILL);
452ARMV8_EVENT_ATTR(l3d_cache, ARMV8_PMUV3_PERFCTR_L3D_CACHE);
453ARMV8_EVENT_ATTR(l3d_cache_wb, ARMV8_PMUV3_PERFCTR_L3D_CACHE_WB);
454ARMV8_EVENT_ATTR(l2d_tlb_refill, ARMV8_PMUV3_PERFCTR_L2D_TLB_REFILL);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700455ARMV8_EVENT_ATTR(l2i_tlb_refill, ARMV8_PMUV3_PERFCTR_L2I_TLB_REFILL);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700456ARMV8_EVENT_ATTR(l2d_tlb, ARMV8_PMUV3_PERFCTR_L2D_TLB);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700457ARMV8_EVENT_ATTR(l2i_tlb, ARMV8_PMUV3_PERFCTR_L2I_TLB);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700458
459static struct attribute *armv8_pmuv3_event_attrs[] = {
460 &armv8_event_attr_sw_incr.attr.attr,
461 &armv8_event_attr_l1i_cache_refill.attr.attr,
462 &armv8_event_attr_l1i_tlb_refill.attr.attr,
463 &armv8_event_attr_l1d_cache_refill.attr.attr,
464 &armv8_event_attr_l1d_cache.attr.attr,
465 &armv8_event_attr_l1d_tlb_refill.attr.attr,
466 &armv8_event_attr_ld_retired.attr.attr,
467 &armv8_event_attr_st_retired.attr.attr,
468 &armv8_event_attr_inst_retired.attr.attr,
469 &armv8_event_attr_exc_taken.attr.attr,
470 &armv8_event_attr_exc_return.attr.attr,
471 &armv8_event_attr_cid_write_retired.attr.attr,
472 &armv8_event_attr_pc_write_retired.attr.attr,
473 &armv8_event_attr_br_immed_retired.attr.attr,
474 &armv8_event_attr_br_return_retired.attr.attr,
475 &armv8_event_attr_unaligned_ldst_retired.attr.attr,
476 &armv8_event_attr_br_mis_pred.attr.attr,
477 &armv8_event_attr_cpu_cycles.attr.attr,
478 &armv8_event_attr_br_pred.attr.attr,
479 &armv8_event_attr_mem_access.attr.attr,
480 &armv8_event_attr_l1i_cache.attr.attr,
481 &armv8_event_attr_l1d_cache_wb.attr.attr,
482 &armv8_event_attr_l2d_cache.attr.attr,
483 &armv8_event_attr_l2d_cache_refill.attr.attr,
484 &armv8_event_attr_l2d_cache_wb.attr.attr,
485 &armv8_event_attr_bus_access.attr.attr,
486 &armv8_event_attr_memory_error.attr.attr,
487 &armv8_event_attr_inst_spec.attr.attr,
488 &armv8_event_attr_ttbr_write_retired.attr.attr,
489 &armv8_event_attr_bus_cycles.attr.attr,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700490 &armv8_event_attr_l1d_cache_allocate.attr.attr,
491 &armv8_event_attr_l2d_cache_allocate.attr.attr,
492 &armv8_event_attr_br_retired.attr.attr,
493 &armv8_event_attr_br_mis_pred_retired.attr.attr,
494 &armv8_event_attr_stall_frontend.attr.attr,
495 &armv8_event_attr_stall_backend.attr.attr,
496 &armv8_event_attr_l1d_tlb.attr.attr,
497 &armv8_event_attr_l1i_tlb.attr.attr,
498 &armv8_event_attr_l2i_cache.attr.attr,
499 &armv8_event_attr_l2i_cache_refill.attr.attr,
500 &armv8_event_attr_l3d_cache_allocate.attr.attr,
501 &armv8_event_attr_l3d_cache_refill.attr.attr,
502 &armv8_event_attr_l3d_cache.attr.attr,
503 &armv8_event_attr_l3d_cache_wb.attr.attr,
504 &armv8_event_attr_l2d_tlb_refill.attr.attr,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700505 &armv8_event_attr_l2i_tlb_refill.attr.attr,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700506 &armv8_event_attr_l2d_tlb.attr.attr,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700507 &armv8_event_attr_l2i_tlb.attr.attr,
Will Deacon57d74122015-12-22 14:42:57 +0000508 NULL,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700509};
510
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700511static umode_t
512armv8pmu_event_attr_is_visible(struct kobject *kobj,
513 struct attribute *attr, int unused)
514{
515 struct device *dev = kobj_to_dev(kobj);
516 struct pmu *pmu = dev_get_drvdata(dev);
517 struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
518 struct perf_pmu_events_attr *pmu_attr;
519
520 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
521
522 if (test_bit(pmu_attr->id, cpu_pmu->pmceid_bitmap))
523 return attr->mode;
524
525 return 0;
526}
527
Drew Richardson9e9caa62015-10-22 07:07:32 -0700528static struct attribute_group armv8_pmuv3_events_attr_group = {
529 .name = "events",
530 .attrs = armv8_pmuv3_event_attrs,
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700531 .is_visible = armv8pmu_event_attr_is_visible,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700532};
533
Will Deacon57d74122015-12-22 14:42:57 +0000534PMU_FORMAT_ATTR(event, "config:0-9");
535
536static struct attribute *armv8_pmuv3_format_attrs[] = {
537 &format_attr_event.attr,
538 NULL,
539};
540
541static struct attribute_group armv8_pmuv3_format_attr_group = {
542 .name = "format",
543 .attrs = armv8_pmuv3_format_attrs,
544};
545
Will Deacon03089682012-03-05 11:49:32 +0000546/*
547 * Perf Events' indices
548 */
549#define ARMV8_IDX_CYCLE_COUNTER 0
550#define ARMV8_IDX_COUNTER0 1
Mark Rutland6475b2d2015-10-02 10:55:03 +0100551#define ARMV8_IDX_COUNTER_LAST(cpu_pmu) \
552 (ARMV8_IDX_CYCLE_COUNTER + cpu_pmu->num_events - 1)
Will Deacon03089682012-03-05 11:49:32 +0000553
Will Deacon03089682012-03-05 11:49:32 +0000554/*
555 * ARMv8 low level PMU access
556 */
557
558/*
559 * Perf Event to low level counters mapping
560 */
561#define ARMV8_IDX_TO_COUNTER(x) \
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000562 (((x) - ARMV8_IDX_COUNTER0) & ARMV8_PMU_COUNTER_MASK)
Will Deacon03089682012-03-05 11:49:32 +0000563
564static inline u32 armv8pmu_pmcr_read(void)
565{
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700566 return read_sysreg(pmcr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000567}
568
569static inline void armv8pmu_pmcr_write(u32 val)
570{
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000571 val &= ARMV8_PMU_PMCR_MASK;
Will Deacon03089682012-03-05 11:49:32 +0000572 isb();
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700573 write_sysreg(val, pmcr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000574}
575
576static inline int armv8pmu_has_overflowed(u32 pmovsr)
577{
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000578 return pmovsr & ARMV8_PMU_OVERFLOWED_MASK;
Will Deacon03089682012-03-05 11:49:32 +0000579}
580
Mark Rutland6475b2d2015-10-02 10:55:03 +0100581static inline int armv8pmu_counter_valid(struct arm_pmu *cpu_pmu, int idx)
Will Deacon03089682012-03-05 11:49:32 +0000582{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100583 return idx >= ARMV8_IDX_CYCLE_COUNTER &&
584 idx <= ARMV8_IDX_COUNTER_LAST(cpu_pmu);
Will Deacon03089682012-03-05 11:49:32 +0000585}
586
587static inline int armv8pmu_counter_has_overflowed(u32 pmnc, int idx)
588{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100589 return pmnc & BIT(ARMV8_IDX_TO_COUNTER(idx));
Will Deacon03089682012-03-05 11:49:32 +0000590}
591
592static inline int armv8pmu_select_counter(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(counter, pmselr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000596 isb();
597
598 return idx;
599}
600
Mark Rutland6475b2d2015-10-02 10:55:03 +0100601static inline u32 armv8pmu_read_counter(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000602{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100603 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
604 struct hw_perf_event *hwc = &event->hw;
605 int idx = hwc->idx;
Will Deacon03089682012-03-05 11:49:32 +0000606 u32 value = 0;
607
Mark Rutland6475b2d2015-10-02 10:55:03 +0100608 if (!armv8pmu_counter_valid(cpu_pmu, idx))
Will Deacon03089682012-03-05 11:49:32 +0000609 pr_err("CPU%u reading wrong counter %d\n",
610 smp_processor_id(), idx);
611 else if (idx == ARMV8_IDX_CYCLE_COUNTER)
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700612 value = read_sysreg(pmccntr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000613 else if (armv8pmu_select_counter(idx) == idx)
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700614 value = read_sysreg(pmxevcntr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000615
616 return value;
617}
618
Mark Rutland6475b2d2015-10-02 10:55:03 +0100619static inline void armv8pmu_write_counter(struct perf_event *event, u32 value)
Will Deacon03089682012-03-05 11:49:32 +0000620{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100621 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
622 struct hw_perf_event *hwc = &event->hw;
623 int idx = hwc->idx;
624
625 if (!armv8pmu_counter_valid(cpu_pmu, idx))
Will Deacon03089682012-03-05 11:49:32 +0000626 pr_err("CPU%u writing wrong counter %d\n",
627 smp_processor_id(), idx);
Jan Glauber7175f052016-02-18 17:50:13 +0100628 else if (idx == ARMV8_IDX_CYCLE_COUNTER) {
629 /*
630 * Set the upper 32bits as this is a 64bit counter but we only
631 * count using the lower 32bits and we want an interrupt when
632 * it overflows.
633 */
634 u64 value64 = 0xffffffff00000000ULL | value;
635
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700636 write_sysreg(value64, pmccntr_el0);
Jan Glauber7175f052016-02-18 17:50:13 +0100637 } else if (armv8pmu_select_counter(idx) == idx)
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700638 write_sysreg(value, pmxevcntr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000639}
640
641static inline void armv8pmu_write_evtype(int idx, u32 val)
642{
643 if (armv8pmu_select_counter(idx) == idx) {
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000644 val &= ARMV8_PMU_EVTYPE_MASK;
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700645 write_sysreg(val, pmxevtyper_el0);
Will Deacon03089682012-03-05 11:49:32 +0000646 }
647}
648
649static inline int armv8pmu_enable_counter(int idx)
650{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100651 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700652 write_sysreg(BIT(counter), pmcntenset_el0);
Will Deacon03089682012-03-05 11:49:32 +0000653 return idx;
654}
655
656static inline int armv8pmu_disable_counter(int idx)
657{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100658 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700659 write_sysreg(BIT(counter), pmcntenclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000660 return idx;
661}
662
663static inline int armv8pmu_enable_intens(int idx)
664{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100665 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700666 write_sysreg(BIT(counter), pmintenset_el1);
Will Deacon03089682012-03-05 11:49:32 +0000667 return idx;
668}
669
670static inline int armv8pmu_disable_intens(int idx)
671{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100672 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700673 write_sysreg(BIT(counter), pmintenclr_el1);
Will Deacon03089682012-03-05 11:49:32 +0000674 isb();
675 /* Clear the overflow flag in case an interrupt is pending. */
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700676 write_sysreg(BIT(counter), pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000677 isb();
Mark Rutland6475b2d2015-10-02 10:55:03 +0100678
Will Deacon03089682012-03-05 11:49:32 +0000679 return idx;
680}
681
682static inline u32 armv8pmu_getreset_flags(void)
683{
684 u32 value;
685
686 /* Read */
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700687 value = read_sysreg(pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000688
689 /* Write to clear flags */
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000690 value &= ARMV8_PMU_OVSR_MASK;
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700691 write_sysreg(value, pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000692
693 return value;
694}
695
Mark Rutland6475b2d2015-10-02 10:55:03 +0100696static void armv8pmu_enable_event(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000697{
698 unsigned long flags;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100699 struct hw_perf_event *hwc = &event->hw;
700 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
701 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
702 int idx = hwc->idx;
Will Deacon03089682012-03-05 11:49:32 +0000703
704 /*
705 * Enable counter and interrupt, and set the counter to count
706 * the event that we're interested in.
707 */
708 raw_spin_lock_irqsave(&events->pmu_lock, flags);
709
710 /*
711 * Disable counter
712 */
713 armv8pmu_disable_counter(idx);
714
715 /*
716 * Set event (if destined for PMNx counters).
717 */
718 armv8pmu_write_evtype(idx, hwc->config_base);
719
720 /*
721 * Enable interrupt for this counter
722 */
723 armv8pmu_enable_intens(idx);
724
725 /*
726 * Enable counter
727 */
728 armv8pmu_enable_counter(idx);
729
730 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
731}
732
Mark Rutland6475b2d2015-10-02 10:55:03 +0100733static void armv8pmu_disable_event(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000734{
735 unsigned long flags;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100736 struct hw_perf_event *hwc = &event->hw;
737 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
738 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
739 int idx = hwc->idx;
Will Deacon03089682012-03-05 11:49:32 +0000740
741 /*
742 * Disable counter and interrupt
743 */
744 raw_spin_lock_irqsave(&events->pmu_lock, flags);
745
746 /*
747 * Disable counter
748 */
749 armv8pmu_disable_counter(idx);
750
751 /*
752 * Disable interrupt for this counter
753 */
754 armv8pmu_disable_intens(idx);
755
756 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
757}
758
759static irqreturn_t armv8pmu_handle_irq(int irq_num, void *dev)
760{
761 u32 pmovsr;
762 struct perf_sample_data data;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100763 struct arm_pmu *cpu_pmu = (struct arm_pmu *)dev;
764 struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
Will Deacon03089682012-03-05 11:49:32 +0000765 struct pt_regs *regs;
766 int idx;
767
768 /*
769 * Get and reset the IRQ flags
770 */
771 pmovsr = armv8pmu_getreset_flags();
772
773 /*
774 * Did an overflow occur?
775 */
776 if (!armv8pmu_has_overflowed(pmovsr))
777 return IRQ_NONE;
778
779 /*
780 * Handle the counter(s) overflow(s)
781 */
782 regs = get_irq_regs();
783
Will Deacon03089682012-03-05 11:49:32 +0000784 for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
785 struct perf_event *event = cpuc->events[idx];
786 struct hw_perf_event *hwc;
787
788 /* Ignore if we don't have an event. */
789 if (!event)
790 continue;
791
792 /*
793 * We have a single interrupt for all counters. Check that
794 * each counter has overflowed before we process it.
795 */
796 if (!armv8pmu_counter_has_overflowed(pmovsr, idx))
797 continue;
798
799 hwc = &event->hw;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100800 armpmu_event_update(event);
Will Deacon03089682012-03-05 11:49:32 +0000801 perf_sample_data_init(&data, 0, hwc->last_period);
Mark Rutland6475b2d2015-10-02 10:55:03 +0100802 if (!armpmu_event_set_period(event))
Will Deacon03089682012-03-05 11:49:32 +0000803 continue;
804
805 if (perf_event_overflow(event, &data, regs))
Mark Rutland6475b2d2015-10-02 10:55:03 +0100806 cpu_pmu->disable(event);
Will Deacon03089682012-03-05 11:49:32 +0000807 }
808
809 /*
810 * Handle the pending perf events.
811 *
812 * Note: this call *must* be run with interrupts disabled. For
813 * platforms that can have the PMU interrupts raised as an NMI, this
814 * will not work.
815 */
816 irq_work_run();
817
818 return IRQ_HANDLED;
819}
820
Mark Rutland6475b2d2015-10-02 10:55:03 +0100821static void armv8pmu_start(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +0000822{
823 unsigned long flags;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100824 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
Will Deacon03089682012-03-05 11:49:32 +0000825
826 raw_spin_lock_irqsave(&events->pmu_lock, flags);
827 /* Enable all counters */
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000828 armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMU_PMCR_E);
Will Deacon03089682012-03-05 11:49:32 +0000829 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
830}
831
Mark Rutland6475b2d2015-10-02 10:55:03 +0100832static void armv8pmu_stop(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +0000833{
834 unsigned long flags;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100835 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
Will Deacon03089682012-03-05 11:49:32 +0000836
837 raw_spin_lock_irqsave(&events->pmu_lock, flags);
838 /* Disable all counters */
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000839 armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMU_PMCR_E);
Will Deacon03089682012-03-05 11:49:32 +0000840 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
841}
842
843static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
Mark Rutland6475b2d2015-10-02 10:55:03 +0100844 struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000845{
846 int idx;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100847 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
848 struct hw_perf_event *hwc = &event->hw;
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000849 unsigned long evtype = hwc->config_base & ARMV8_PMU_EVTYPE_EVENT;
Will Deacon03089682012-03-05 11:49:32 +0000850
851 /* Always place a cycle counter into the cycle counter. */
Ashok Kumar03598fd2016-04-21 05:58:41 -0700852 if (evtype == ARMV8_PMUV3_PERFCTR_CPU_CYCLES) {
Will Deacon03089682012-03-05 11:49:32 +0000853 if (test_and_set_bit(ARMV8_IDX_CYCLE_COUNTER, cpuc->used_mask))
854 return -EAGAIN;
855
856 return ARMV8_IDX_CYCLE_COUNTER;
857 }
858
859 /*
860 * For anything other than a cycle counter, try and use
861 * the events counters
862 */
863 for (idx = ARMV8_IDX_COUNTER0; idx < cpu_pmu->num_events; ++idx) {
864 if (!test_and_set_bit(idx, cpuc->used_mask))
865 return idx;
866 }
867
868 /* The counters are all in use. */
869 return -EAGAIN;
870}
871
872/*
873 * Add an event filter to a given event. This will only work for PMUv2 PMUs.
874 */
875static int armv8pmu_set_event_filter(struct hw_perf_event *event,
876 struct perf_event_attr *attr)
877{
878 unsigned long config_base = 0;
879
880 if (attr->exclude_idle)
881 return -EPERM;
Marc Zyngierd98ecda2016-01-25 17:31:13 +0000882 if (is_kernel_in_hyp_mode() &&
883 attr->exclude_kernel != attr->exclude_hv)
884 return -EINVAL;
Will Deacon03089682012-03-05 11:49:32 +0000885 if (attr->exclude_user)
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000886 config_base |= ARMV8_PMU_EXCLUDE_EL0;
Marc Zyngierd98ecda2016-01-25 17:31:13 +0000887 if (!is_kernel_in_hyp_mode() && attr->exclude_kernel)
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000888 config_base |= ARMV8_PMU_EXCLUDE_EL1;
Will Deacon03089682012-03-05 11:49:32 +0000889 if (!attr->exclude_hv)
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000890 config_base |= ARMV8_PMU_INCLUDE_EL2;
Will Deacon03089682012-03-05 11:49:32 +0000891
892 /*
893 * Install the filter into config_base as this is used to
894 * construct the event type.
895 */
896 event->config_base = config_base;
897
898 return 0;
899}
900
901static void armv8pmu_reset(void *info)
902{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100903 struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
Will Deacon03089682012-03-05 11:49:32 +0000904 u32 idx, nb_cnt = cpu_pmu->num_events;
905
906 /* The counter and interrupt enable registers are unknown at reset. */
Mark Rutland6475b2d2015-10-02 10:55:03 +0100907 for (idx = ARMV8_IDX_CYCLE_COUNTER; idx < nb_cnt; ++idx) {
908 armv8pmu_disable_counter(idx);
909 armv8pmu_disable_intens(idx);
910 }
Will Deacon03089682012-03-05 11:49:32 +0000911
Jan Glauber7175f052016-02-18 17:50:13 +0100912 /*
913 * Initialize & Reset PMNC. Request overflow interrupt for
914 * 64 bit cycle counter but cheat in armv8pmu_write_counter().
915 */
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000916 armv8pmu_pmcr_write(ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C |
917 ARMV8_PMU_PMCR_LC);
Will Deacon03089682012-03-05 11:49:32 +0000918}
919
920static int armv8_pmuv3_map_event(struct perf_event *event)
921{
Jeremy Linton236b9b912016-09-14 17:32:30 -0500922 int hw_event_id;
923 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
924
925 hw_event_id = armpmu_map_event(event, &armv8_pmuv3_perf_map,
926 &armv8_pmuv3_perf_cache_map,
927 ARMV8_PMU_EVTYPE_EVENT);
928 if (hw_event_id < 0)
929 return hw_event_id;
930
931 /* disable micro/arch events not supported by this PMU */
932 if ((hw_event_id < ARMV8_PMUV3_MAX_COMMON_EVENTS) &&
933 !test_bit(hw_event_id, armpmu->pmceid_bitmap)) {
934 return -EOPNOTSUPP;
935 }
936
937 return hw_event_id;
Will Deacon03089682012-03-05 11:49:32 +0000938}
939
Mark Rutlandac82d122015-10-02 10:55:04 +0100940static int armv8_a53_map_event(struct perf_event *event)
941{
942 return armpmu_map_event(event, &armv8_a53_perf_map,
943 &armv8_a53_perf_cache_map,
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000944 ARMV8_PMU_EVTYPE_EVENT);
Mark Rutlandac82d122015-10-02 10:55:04 +0100945}
946
Mark Rutland62a4dda2015-10-02 10:55:05 +0100947static int armv8_a57_map_event(struct perf_event *event)
948{
949 return armpmu_map_event(event, &armv8_a57_perf_map,
950 &armv8_a57_perf_cache_map,
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000951 ARMV8_PMU_EVTYPE_EVENT);
Mark Rutland62a4dda2015-10-02 10:55:05 +0100952}
953
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100954static int armv8_thunder_map_event(struct perf_event *event)
955{
956 return armpmu_map_event(event, &armv8_thunder_perf_map,
957 &armv8_thunder_perf_cache_map,
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000958 ARMV8_PMU_EVTYPE_EVENT);
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100959}
960
Ashok Kumar201a72b2016-04-21 05:58:45 -0700961static int armv8_vulcan_map_event(struct perf_event *event)
962{
963 return armpmu_map_event(event, &armv8_vulcan_perf_map,
964 &armv8_vulcan_perf_cache_map,
965 ARMV8_PMU_EVTYPE_EVENT);
966}
967
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700968static void __armv8pmu_probe_pmu(void *info)
Will Deacon03089682012-03-05 11:49:32 +0000969{
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700970 struct arm_pmu *cpu_pmu = info;
971 u32 pmceid[2];
Will Deacon03089682012-03-05 11:49:32 +0000972
973 /* Read the nb of CNTx counters supported from PMNC */
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700974 cpu_pmu->num_events = (armv8pmu_pmcr_read() >> ARMV8_PMU_PMCR_N_SHIFT)
975 & ARMV8_PMU_PMCR_N_MASK;
Will Deacon03089682012-03-05 11:49:32 +0000976
Mark Rutland6475b2d2015-10-02 10:55:03 +0100977 /* Add the CPU cycles counter */
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700978 cpu_pmu->num_events += 1;
979
980 pmceid[0] = read_sysreg(pmceid0_el0);
981 pmceid[1] = read_sysreg(pmceid1_el0);
982
983 bitmap_from_u32array(cpu_pmu->pmceid_bitmap,
984 ARMV8_PMUV3_MAX_COMMON_EVENTS, pmceid,
985 ARRAY_SIZE(pmceid));
Will Deacon03089682012-03-05 11:49:32 +0000986}
987
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700988static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +0000989{
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700990 return smp_call_function_any(&cpu_pmu->supported_cpus,
991 __armv8pmu_probe_pmu,
992 cpu_pmu, 1);
Will Deacon03089682012-03-05 11:49:32 +0000993}
994
Mark Rutlandac82d122015-10-02 10:55:04 +0100995static void armv8_pmu_init(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +0000996{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100997 cpu_pmu->handle_irq = armv8pmu_handle_irq,
998 cpu_pmu->enable = armv8pmu_enable_event,
999 cpu_pmu->disable = armv8pmu_disable_event,
1000 cpu_pmu->read_counter = armv8pmu_read_counter,
1001 cpu_pmu->write_counter = armv8pmu_write_counter,
1002 cpu_pmu->get_event_idx = armv8pmu_get_event_idx,
1003 cpu_pmu->start = armv8pmu_start,
1004 cpu_pmu->stop = armv8pmu_stop,
1005 cpu_pmu->reset = armv8pmu_reset,
1006 cpu_pmu->max_period = (1LLU << 32) - 1,
Mark Rutlandac82d122015-10-02 10:55:04 +01001007 cpu_pmu->set_event_filter = armv8pmu_set_event_filter;
1008}
1009
1010static int armv8_pmuv3_init(struct arm_pmu *cpu_pmu)
1011{
1012 armv8_pmu_init(cpu_pmu);
Mark Rutland6475b2d2015-10-02 10:55:03 +01001013 cpu_pmu->name = "armv8_pmuv3";
1014 cpu_pmu->map_event = armv8_pmuv3_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001015 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;
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001019 return armv8pmu_probe_pmu(cpu_pmu);
Mark Rutlandac82d122015-10-02 10:55:04 +01001020}
1021
1022static int armv8_a53_pmu_init(struct arm_pmu *cpu_pmu)
1023{
1024 armv8_pmu_init(cpu_pmu);
1025 cpu_pmu->name = "armv8_cortex_a53";
1026 cpu_pmu->map_event = armv8_a53_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001027 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1028 &armv8_pmuv3_events_attr_group;
1029 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1030 &armv8_pmuv3_format_attr_group;
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001031 return armv8pmu_probe_pmu(cpu_pmu);
Will Deacon03089682012-03-05 11:49:32 +00001032}
Will Deacon03089682012-03-05 11:49:32 +00001033
Mark Rutland62a4dda2015-10-02 10:55:05 +01001034static int armv8_a57_pmu_init(struct arm_pmu *cpu_pmu)
1035{
1036 armv8_pmu_init(cpu_pmu);
1037 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;
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001043 return armv8pmu_probe_pmu(cpu_pmu);
Mark Rutland62a4dda2015-10-02 10:55:05 +01001044}
1045
Will Deacon5d7ee872015-12-22 14:45:35 +00001046static int armv8_a72_pmu_init(struct arm_pmu *cpu_pmu)
1047{
1048 armv8_pmu_init(cpu_pmu);
1049 cpu_pmu->name = "armv8_cortex_a72";
1050 cpu_pmu->map_event = armv8_a57_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001051 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1052 &armv8_pmuv3_events_attr_group;
1053 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1054 &armv8_pmuv3_format_attr_group;
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001055 return armv8pmu_probe_pmu(cpu_pmu);
Will Deacon5d7ee872015-12-22 14:45:35 +00001056}
1057
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001058static int armv8_thunder_pmu_init(struct arm_pmu *cpu_pmu)
1059{
1060 armv8_pmu_init(cpu_pmu);
1061 cpu_pmu->name = "armv8_cavium_thunder";
1062 cpu_pmu->map_event = armv8_thunder_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001063 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;
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001067 return armv8pmu_probe_pmu(cpu_pmu);
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001068}
1069
Ashok Kumar201a72b2016-04-21 05:58:45 -07001070static int armv8_vulcan_pmu_init(struct arm_pmu *cpu_pmu)
1071{
1072 armv8_pmu_init(cpu_pmu);
1073 cpu_pmu->name = "armv8_brcm_vulcan";
1074 cpu_pmu->map_event = armv8_vulcan_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001075 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1076 &armv8_pmuv3_events_attr_group;
1077 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1078 &armv8_pmuv3_format_attr_group;
Ashok Kumar201a72b2016-04-21 05:58:45 -07001079 return armv8pmu_probe_pmu(cpu_pmu);
1080}
1081
Mark Rutland6475b2d2015-10-02 10:55:03 +01001082static const struct of_device_id armv8_pmu_of_device_ids[] = {
1083 {.compatible = "arm,armv8-pmuv3", .data = armv8_pmuv3_init},
Mark Rutlandac82d122015-10-02 10:55:04 +01001084 {.compatible = "arm,cortex-a53-pmu", .data = armv8_a53_pmu_init},
Mark Rutland62a4dda2015-10-02 10:55:05 +01001085 {.compatible = "arm,cortex-a57-pmu", .data = armv8_a57_pmu_init},
Will Deacon5d7ee872015-12-22 14:45:35 +00001086 {.compatible = "arm,cortex-a72-pmu", .data = armv8_a72_pmu_init},
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001087 {.compatible = "cavium,thunder-pmu", .data = armv8_thunder_pmu_init},
Ashok Kumar201a72b2016-04-21 05:58:45 -07001088 {.compatible = "brcm,vulcan-pmu", .data = armv8_vulcan_pmu_init},
Will Deacon03089682012-03-05 11:49:32 +00001089 {},
1090};
1091
Jeremy Linton236b9b912016-09-14 17:32:30 -05001092/*
1093 * Non DT systems have their micro/arch events probed at run-time.
1094 * A fairly complete list of generic events are provided and ones that
1095 * aren't supported by the current PMU are disabled.
1096 */
Mark Salterdbee3a72016-09-14 17:32:29 -05001097static const struct pmu_probe_info armv8_pmu_probe_table[] = {
Jeremy Linton236b9b912016-09-14 17:32:30 -05001098 PMU_PROBE(0, 0, armv8_pmuv3_init), /* enable all defined counters */
Mark Salterdbee3a72016-09-14 17:32:29 -05001099 { /* sentinel value */ }
1100};
1101
Mark Rutland6475b2d2015-10-02 10:55:03 +01001102static int armv8_pmu_device_probe(struct platform_device *pdev)
Will Deacon03089682012-03-05 11:49:32 +00001103{
Mark Salterdbee3a72016-09-14 17:32:29 -05001104 if (acpi_disabled)
1105 return arm_pmu_device_probe(pdev, armv8_pmu_of_device_ids,
1106 NULL);
1107
1108 return arm_pmu_device_probe(pdev, armv8_pmu_of_device_ids,
1109 armv8_pmu_probe_table);
Will Deacon03089682012-03-05 11:49:32 +00001110}
1111
Mark Rutland6475b2d2015-10-02 10:55:03 +01001112static struct platform_driver armv8_pmu_driver = {
Will Deacon03089682012-03-05 11:49:32 +00001113 .driver = {
Jeremy Linton85023b22016-09-14 17:32:31 -05001114 .name = ARMV8_PMU_PDEV_NAME,
Mark Rutland6475b2d2015-10-02 10:55:03 +01001115 .of_match_table = armv8_pmu_of_device_ids,
Will Deacon03089682012-03-05 11:49:32 +00001116 },
Mark Rutland6475b2d2015-10-02 10:55:03 +01001117 .probe = armv8_pmu_device_probe,
Will Deacon03089682012-03-05 11:49:32 +00001118};
1119
Kefeng Wang826d0562016-08-10 20:59:15 +08001120builtin_platform_driver(armv8_pmu_driver);