blob: 3605f77ad4df1796c52b01a2bb959569d5ea1a83 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Will Deacon03089682012-03-05 11:49:32 +00002/*
Will Deacon4b47e572018-10-05 13:31:10 +01003 * ARMv8 PMUv3 Performance Events handling code.
Will Deacon03089682012-03-05 11:49:32 +00004 *
5 * Copyright (C) 2012 ARM Limited
6 * Author: Will Deacon <will.deacon@arm.com>
7 *
8 * This code is based heavily on the ARMv7 perf event code.
Will Deacon03089682012-03-05 11:49:32 +00009 */
Will Deacon03089682012-03-05 11:49:32 +000010
Will Deacon03089682012-03-05 11:49:32 +000011#include <asm/irq_regs.h>
Shannon Zhaob8cfadf2016-03-24 16:01:16 +000012#include <asm/perf_event.h>
Ashok Kumarbf2d4782016-04-21 05:58:43 -070013#include <asm/sysreg.h>
Marc Zyngierd98ecda2016-01-25 17:31:13 +000014#include <asm/virt.h>
Will Deacon03089682012-03-05 11:49:32 +000015
Peter Zijlstra279a8112020-07-16 13:11:27 +080016#include <clocksource/arm_arch_timer.h>
17
Mark Salterdbee3a72016-09-14 17:32:29 -050018#include <linux/acpi.h>
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -070019#include <linux/clocksource.h>
Andrew Murrayd1947bc2019-04-09 20:22:13 +010020#include <linux/kvm_host.h>
Mark Rutland6475b2d2015-10-02 10:55:03 +010021#include <linux/of.h>
22#include <linux/perf/arm_pmu.h>
23#include <linux/platform_device.h>
Peter Zijlstra950b74dd2020-07-16 13:11:26 +080024#include <linux/sched_clock.h>
Raphael Gaultd91cc2f2019-08-20 16:57:45 +010025#include <linux/smp.h>
Will Deacon03089682012-03-05 11:49:32 +000026
Mark Rutlandac82d122015-10-02 10:55:04 +010027/* ARMv8 Cortex-A53 specific event types. */
Ashok Kumar03598fd2016-04-21 05:58:41 -070028#define ARMV8_A53_PERFCTR_PREF_LINEFILL 0xC2
Mark Rutlandac82d122015-10-02 10:55:04 +010029
Jan Glauberd0aa2bf2016-02-18 17:50:11 +010030/* ARMv8 Cavium ThunderX specific event types. */
Ashok Kumar03598fd2016-04-21 05:58:41 -070031#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST 0xE9
32#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS 0xEA
33#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS 0xEB
34#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS 0xEC
35#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS 0xED
Mark Rutland62a4dda2015-10-02 10:55:05 +010036
Jeremy Linton236b9b912016-09-14 17:32:30 -050037/*
38 * ARMv8 Architectural defined events, not all of these may
Will Deacon342e53b2018-10-05 13:28:07 +010039 * be supported on any given implementation. Unsupported events will
40 * be disabled at run-time based on the PMCEID registers.
Jeremy Linton236b9b912016-09-14 17:32:30 -050041 */
Will Deacon03089682012-03-05 11:49:32 +000042static const unsigned armv8_pmuv3_perf_map[PERF_COUNT_HW_MAX] = {
Mark Rutlandae2fb7e2015-07-21 11:36:39 +010043 PERF_MAP_ALL_UNSUPPORTED,
Ashok Kumar03598fd2016-04-21 05:58:41 -070044 [PERF_COUNT_HW_CPU_CYCLES] = ARMV8_PMUV3_PERFCTR_CPU_CYCLES,
45 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_INST_RETIRED,
46 [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
47 [PERF_COUNT_HW_CACHE_MISSES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
Jeremy Linton236b9b912016-09-14 17:32:30 -050048 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED,
Ashok Kumar03598fd2016-04-21 05:58:41 -070049 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Jeremy Linton236b9b912016-09-14 17:32:30 -050050 [PERF_COUNT_HW_BUS_CYCLES] = ARMV8_PMUV3_PERFCTR_BUS_CYCLES,
51 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV8_PMUV3_PERFCTR_STALL_FRONTEND,
52 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV8_PMUV3_PERFCTR_STALL_BACKEND,
Will Deacon03089682012-03-05 11:49:32 +000053};
54
55static const unsigned armv8_pmuv3_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
56 [PERF_COUNT_HW_CACHE_OP_MAX]
57 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
Mark Rutlandae2fb7e2015-07-21 11:36:39 +010058 PERF_CACHE_MAP_ALL_UNSUPPORTED,
59
Ashok Kumar03598fd2016-04-21 05:58:41 -070060 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
61 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
Mark Rutlandae2fb7e2015-07-21 11:36:39 +010062
Jeremy Linton236b9b912016-09-14 17:32:30 -050063 [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE,
64 [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL,
65
66 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL,
67 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_TLB,
68
69 [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL,
70 [C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB,
71
Leo Yanffdbd3d2020-08-11 13:35:05 +080072 [C(LL)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS_RD,
73 [C(LL)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_LL_CACHE_RD,
74
Ashok Kumar03598fd2016-04-21 05:58:41 -070075 [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
76 [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Will Deacon03089682012-03-05 11:49:32 +000077};
78
Mark Rutlandac82d122015-10-02 10:55:04 +010079static const unsigned armv8_a53_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
80 [PERF_COUNT_HW_CACHE_OP_MAX]
81 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
82 PERF_CACHE_MAP_ALL_UNSUPPORTED,
83
Ashok Kumar03598fd2016-04-21 05:58:41 -070084 [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_A53_PERFCTR_PREF_LINEFILL,
Mark Rutlandac82d122015-10-02 10:55:04 +010085
Julien Thierry5cf7fb22017-07-25 17:27:36 +010086 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
87 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
Mark Rutlandac82d122015-10-02 10:55:04 +010088};
89
Mark Rutland62a4dda2015-10-02 10:55:05 +010090static const unsigned armv8_a57_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
91 [PERF_COUNT_HW_CACHE_OP_MAX]
92 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
93 PERF_CACHE_MAP_ALL_UNSUPPORTED,
94
Ashok Kumar03598fd2016-04-21 05:58:41 -070095 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
96 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
97 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
98 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +010099
Ashok Kumar03598fd2016-04-21 05:58:41 -0700100 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
101 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100102
Julien Thierry5cf7fb22017-07-25 17:27:36 +0100103 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
104 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100105};
106
Julien Thierry5561b6c2017-08-09 17:46:38 +0100107static const unsigned armv8_a73_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
108 [PERF_COUNT_HW_CACHE_OP_MAX]
109 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
110 PERF_CACHE_MAP_ALL_UNSUPPORTED,
111
112 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
113 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
Julien Thierry5561b6c2017-08-09 17:46:38 +0100114};
115
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100116static const unsigned armv8_thunder_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
117 [PERF_COUNT_HW_CACHE_OP_MAX]
118 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
119 PERF_CACHE_MAP_ALL_UNSUPPORTED,
120
Ashok Kumar03598fd2016-04-21 05:58:41 -0700121 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
122 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
123 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
124 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST,
125 [C(L1D)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS,
126 [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS,
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100127
Ashok Kumar03598fd2016-04-21 05:58:41 -0700128 [C(L1I)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS,
129 [C(L1I)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS,
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100130
Ashok Kumar03598fd2016-04-21 05:58:41 -0700131 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
132 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
133 [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
134 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100135};
136
Ashok Kumar201a72b2016-04-21 05:58:45 -0700137static const unsigned armv8_vulcan_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
138 [PERF_COUNT_HW_CACHE_OP_MAX]
139 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
140 PERF_CACHE_MAP_ALL_UNSUPPORTED,
141
142 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
143 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
144 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
145 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR,
146
Ashok Kumar201a72b2016-04-21 05:58:45 -0700147 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
148 [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
149 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
150 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
151
Ashok Kumar201a72b2016-04-21 05:58:45 -0700152 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
153 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
154};
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700155
156static ssize_t
157armv8pmu_events_sysfs_show(struct device *dev,
158 struct device_attribute *attr, char *page)
159{
160 struct perf_pmu_events_attr *pmu_attr;
161
162 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
163
Shaokun Zhang539707c2020-06-18 21:35:44 +0800164 return sprintf(page, "event=0x%04llx\n", pmu_attr->id);
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700165}
166
Shaokun Zhang9ef85672019-10-30 11:46:17 +0800167#define ARMV8_EVENT_ATTR(name, config) \
168 (&((struct perf_pmu_events_attr) { \
169 .attr = __ATTR(name, 0444, armv8pmu_events_sysfs_show, NULL), \
170 .id = config, \
171 }).attr.attr)
Drew Richardson9e9caa62015-10-22 07:07:32 -0700172
173static struct attribute *armv8_pmuv3_event_attrs[] = {
Shaokun Zhang9ef85672019-10-30 11:46:17 +0800174 ARMV8_EVENT_ATTR(sw_incr, ARMV8_PMUV3_PERFCTR_SW_INCR),
175 ARMV8_EVENT_ATTR(l1i_cache_refill, ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL),
176 ARMV8_EVENT_ATTR(l1i_tlb_refill, ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL),
177 ARMV8_EVENT_ATTR(l1d_cache_refill, ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL),
178 ARMV8_EVENT_ATTR(l1d_cache, ARMV8_PMUV3_PERFCTR_L1D_CACHE),
179 ARMV8_EVENT_ATTR(l1d_tlb_refill, ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL),
180 ARMV8_EVENT_ATTR(ld_retired, ARMV8_PMUV3_PERFCTR_LD_RETIRED),
181 ARMV8_EVENT_ATTR(st_retired, ARMV8_PMUV3_PERFCTR_ST_RETIRED),
182 ARMV8_EVENT_ATTR(inst_retired, ARMV8_PMUV3_PERFCTR_INST_RETIRED),
183 ARMV8_EVENT_ATTR(exc_taken, ARMV8_PMUV3_PERFCTR_EXC_TAKEN),
184 ARMV8_EVENT_ATTR(exc_return, ARMV8_PMUV3_PERFCTR_EXC_RETURN),
185 ARMV8_EVENT_ATTR(cid_write_retired, ARMV8_PMUV3_PERFCTR_CID_WRITE_RETIRED),
186 ARMV8_EVENT_ATTR(pc_write_retired, ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED),
187 ARMV8_EVENT_ATTR(br_immed_retired, ARMV8_PMUV3_PERFCTR_BR_IMMED_RETIRED),
188 ARMV8_EVENT_ATTR(br_return_retired, ARMV8_PMUV3_PERFCTR_BR_RETURN_RETIRED),
189 ARMV8_EVENT_ATTR(unaligned_ldst_retired, ARMV8_PMUV3_PERFCTR_UNALIGNED_LDST_RETIRED),
190 ARMV8_EVENT_ATTR(br_mis_pred, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED),
191 ARMV8_EVENT_ATTR(cpu_cycles, ARMV8_PMUV3_PERFCTR_CPU_CYCLES),
192 ARMV8_EVENT_ATTR(br_pred, ARMV8_PMUV3_PERFCTR_BR_PRED),
193 ARMV8_EVENT_ATTR(mem_access, ARMV8_PMUV3_PERFCTR_MEM_ACCESS),
194 ARMV8_EVENT_ATTR(l1i_cache, ARMV8_PMUV3_PERFCTR_L1I_CACHE),
195 ARMV8_EVENT_ATTR(l1d_cache_wb, ARMV8_PMUV3_PERFCTR_L1D_CACHE_WB),
196 ARMV8_EVENT_ATTR(l2d_cache, ARMV8_PMUV3_PERFCTR_L2D_CACHE),
197 ARMV8_EVENT_ATTR(l2d_cache_refill, ARMV8_PMUV3_PERFCTR_L2D_CACHE_REFILL),
198 ARMV8_EVENT_ATTR(l2d_cache_wb, ARMV8_PMUV3_PERFCTR_L2D_CACHE_WB),
199 ARMV8_EVENT_ATTR(bus_access, ARMV8_PMUV3_PERFCTR_BUS_ACCESS),
200 ARMV8_EVENT_ATTR(memory_error, ARMV8_PMUV3_PERFCTR_MEMORY_ERROR),
201 ARMV8_EVENT_ATTR(inst_spec, ARMV8_PMUV3_PERFCTR_INST_SPEC),
202 ARMV8_EVENT_ATTR(ttbr_write_retired, ARMV8_PMUV3_PERFCTR_TTBR_WRITE_RETIRED),
203 ARMV8_EVENT_ATTR(bus_cycles, ARMV8_PMUV3_PERFCTR_BUS_CYCLES),
204 /* Don't expose the chain event in /sys, since it's useless in isolation */
205 ARMV8_EVENT_ATTR(l1d_cache_allocate, ARMV8_PMUV3_PERFCTR_L1D_CACHE_ALLOCATE),
206 ARMV8_EVENT_ATTR(l2d_cache_allocate, ARMV8_PMUV3_PERFCTR_L2D_CACHE_ALLOCATE),
207 ARMV8_EVENT_ATTR(br_retired, ARMV8_PMUV3_PERFCTR_BR_RETIRED),
208 ARMV8_EVENT_ATTR(br_mis_pred_retired, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED_RETIRED),
209 ARMV8_EVENT_ATTR(stall_frontend, ARMV8_PMUV3_PERFCTR_STALL_FRONTEND),
210 ARMV8_EVENT_ATTR(stall_backend, ARMV8_PMUV3_PERFCTR_STALL_BACKEND),
211 ARMV8_EVENT_ATTR(l1d_tlb, ARMV8_PMUV3_PERFCTR_L1D_TLB),
212 ARMV8_EVENT_ATTR(l1i_tlb, ARMV8_PMUV3_PERFCTR_L1I_TLB),
213 ARMV8_EVENT_ATTR(l2i_cache, ARMV8_PMUV3_PERFCTR_L2I_CACHE),
214 ARMV8_EVENT_ATTR(l2i_cache_refill, ARMV8_PMUV3_PERFCTR_L2I_CACHE_REFILL),
215 ARMV8_EVENT_ATTR(l3d_cache_allocate, ARMV8_PMUV3_PERFCTR_L3D_CACHE_ALLOCATE),
216 ARMV8_EVENT_ATTR(l3d_cache_refill, ARMV8_PMUV3_PERFCTR_L3D_CACHE_REFILL),
217 ARMV8_EVENT_ATTR(l3d_cache, ARMV8_PMUV3_PERFCTR_L3D_CACHE),
218 ARMV8_EVENT_ATTR(l3d_cache_wb, ARMV8_PMUV3_PERFCTR_L3D_CACHE_WB),
219 ARMV8_EVENT_ATTR(l2d_tlb_refill, ARMV8_PMUV3_PERFCTR_L2D_TLB_REFILL),
220 ARMV8_EVENT_ATTR(l2i_tlb_refill, ARMV8_PMUV3_PERFCTR_L2I_TLB_REFILL),
221 ARMV8_EVENT_ATTR(l2d_tlb, ARMV8_PMUV3_PERFCTR_L2D_TLB),
222 ARMV8_EVENT_ATTR(l2i_tlb, ARMV8_PMUV3_PERFCTR_L2I_TLB),
223 ARMV8_EVENT_ATTR(remote_access, ARMV8_PMUV3_PERFCTR_REMOTE_ACCESS),
224 ARMV8_EVENT_ATTR(ll_cache, ARMV8_PMUV3_PERFCTR_LL_CACHE),
225 ARMV8_EVENT_ATTR(ll_cache_miss, ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS),
226 ARMV8_EVENT_ATTR(dtlb_walk, ARMV8_PMUV3_PERFCTR_DTLB_WALK),
227 ARMV8_EVENT_ATTR(itlb_walk, ARMV8_PMUV3_PERFCTR_ITLB_WALK),
228 ARMV8_EVENT_ATTR(ll_cache_rd, ARMV8_PMUV3_PERFCTR_LL_CACHE_RD),
229 ARMV8_EVENT_ATTR(ll_cache_miss_rd, ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS_RD),
230 ARMV8_EVENT_ATTR(remote_access_rd, ARMV8_PMUV3_PERFCTR_REMOTE_ACCESS_RD),
Shaokun Zhang55fdc1f2020-07-21 18:49:33 +0800231 ARMV8_EVENT_ATTR(l1d_cache_lmiss_rd, ARMV8_PMUV3_PERFCTR_L1D_CACHE_LMISS_RD),
232 ARMV8_EVENT_ATTR(op_retired, ARMV8_PMUV3_PERFCTR_OP_RETIRED),
233 ARMV8_EVENT_ATTR(op_spec, ARMV8_PMUV3_PERFCTR_OP_SPEC),
234 ARMV8_EVENT_ATTR(stall, ARMV8_PMUV3_PERFCTR_STALL),
235 ARMV8_EVENT_ATTR(stall_slot_backend, ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND),
236 ARMV8_EVENT_ATTR(stall_slot_frontend, ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND),
237 ARMV8_EVENT_ATTR(stall_slot, ARMV8_PMUV3_PERFCTR_STALL_SLOT),
Shaokun Zhang9ef85672019-10-30 11:46:17 +0800238 ARMV8_EVENT_ATTR(sample_pop, ARMV8_SPE_PERFCTR_SAMPLE_POP),
239 ARMV8_EVENT_ATTR(sample_feed, ARMV8_SPE_PERFCTR_SAMPLE_FEED),
240 ARMV8_EVENT_ATTR(sample_filtrate, ARMV8_SPE_PERFCTR_SAMPLE_FILTRATE),
241 ARMV8_EVENT_ATTR(sample_collision, ARMV8_SPE_PERFCTR_SAMPLE_COLLISION),
Shaokun Zhang55fdc1f2020-07-21 18:49:33 +0800242 ARMV8_EVENT_ATTR(cnt_cycles, ARMV8_AMU_PERFCTR_CNT_CYCLES),
243 ARMV8_EVENT_ATTR(stall_backend_mem, ARMV8_AMU_PERFCTR_STALL_BACKEND_MEM),
244 ARMV8_EVENT_ATTR(l1i_cache_lmiss, ARMV8_PMUV3_PERFCTR_L1I_CACHE_LMISS),
245 ARMV8_EVENT_ATTR(l2d_cache_lmiss_rd, ARMV8_PMUV3_PERFCTR_L2D_CACHE_LMISS_RD),
246 ARMV8_EVENT_ATTR(l2i_cache_lmiss, ARMV8_PMUV3_PERFCTR_L2I_CACHE_LMISS),
247 ARMV8_EVENT_ATTR(l3d_cache_lmiss_rd, ARMV8_PMUV3_PERFCTR_L3D_CACHE_LMISS_RD),
248 ARMV8_EVENT_ATTR(ldst_align_lat, ARMV8_PMUV3_PERFCTR_LDST_ALIGN_LAT),
249 ARMV8_EVENT_ATTR(ld_align_lat, ARMV8_PMUV3_PERFCTR_LD_ALIGN_LAT),
250 ARMV8_EVENT_ATTR(st_align_lat, ARMV8_PMUV3_PERFCTR_ST_ALIGN_LAT),
251 ARMV8_EVENT_ATTR(mem_access_checked, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED),
252 ARMV8_EVENT_ATTR(mem_access_checked_rd, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED_RD),
253 ARMV8_EVENT_ATTR(mem_access_checked_wr, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED_WR),
Will Deacon57d74122015-12-22 14:42:57 +0000254 NULL,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700255};
256
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700257static umode_t
258armv8pmu_event_attr_is_visible(struct kobject *kobj,
259 struct attribute *attr, int unused)
260{
261 struct device *dev = kobj_to_dev(kobj);
262 struct pmu *pmu = dev_get_drvdata(dev);
263 struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
264 struct perf_pmu_events_attr *pmu_attr;
265
266 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
267
Will Deacon342e53b2018-10-05 13:28:07 +0100268 if (pmu_attr->id < ARMV8_PMUV3_MAX_COMMON_EVENTS &&
269 test_bit(pmu_attr->id, cpu_pmu->pmceid_bitmap))
270 return attr->mode;
271
Shaokun Zhang539707c2020-06-18 21:35:44 +0800272 if (pmu_attr->id >= ARMV8_PMUV3_EXT_COMMON_EVENT_BASE) {
273 u64 id = pmu_attr->id - ARMV8_PMUV3_EXT_COMMON_EVENT_BASE;
274
275 if (id < ARMV8_PMUV3_MAX_COMMON_EVENTS &&
276 test_bit(id, cpu_pmu->pmceid_ext_bitmap))
277 return attr->mode;
278 }
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700279
280 return 0;
281}
282
Drew Richardson9e9caa62015-10-22 07:07:32 -0700283static struct attribute_group armv8_pmuv3_events_attr_group = {
284 .name = "events",
285 .attrs = armv8_pmuv3_event_attrs,
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700286 .is_visible = armv8pmu_event_attr_is_visible,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700287};
288
Shaokun Zhangfe7296e2017-05-24 15:43:18 +0800289PMU_FORMAT_ATTR(event, "config:0-15");
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100290PMU_FORMAT_ATTR(long, "config1:0");
291
292static inline bool armv8pmu_event_is_64bit(struct perf_event *event)
293{
294 return event->attr.config1 & 0x1;
295}
Will Deacon57d74122015-12-22 14:42:57 +0000296
297static struct attribute *armv8_pmuv3_format_attrs[] = {
298 &format_attr_event.attr,
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100299 &format_attr_long.attr,
Will Deacon57d74122015-12-22 14:42:57 +0000300 NULL,
301};
302
303static struct attribute_group armv8_pmuv3_format_attr_group = {
304 .name = "format",
305 .attrs = armv8_pmuv3_format_attrs,
306};
307
Shaokun Zhangf5be3a62020-09-22 13:53:45 +0800308static ssize_t slots_show(struct device *dev, struct device_attribute *attr,
309 char *page)
310{
311 struct pmu *pmu = dev_get_drvdata(dev);
312 struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
313 u32 slots = cpu_pmu->reg_pmmir & ARMV8_PMU_SLOTS_MASK;
314
315 return snprintf(page, PAGE_SIZE, "0x%08x\n", slots);
316}
317
318static DEVICE_ATTR_RO(slots);
319
320static struct attribute *armv8_pmuv3_caps_attrs[] = {
321 &dev_attr_slots.attr,
322 NULL,
323};
324
325static struct attribute_group armv8_pmuv3_caps_attr_group = {
326 .name = "caps",
327 .attrs = armv8_pmuv3_caps_attrs,
328};
329
Will Deacon03089682012-03-05 11:49:32 +0000330/*
331 * Perf Events' indices
332 */
333#define ARMV8_IDX_CYCLE_COUNTER 0
334#define ARMV8_IDX_COUNTER0 1
Will Deacon03089682012-03-05 11:49:32 +0000335
Andrew Murray8673e022020-03-02 18:17:52 +0000336
337/*
338 * We unconditionally enable ARMv8.5-PMU long event counter support
339 * (64-bit events) where supported. Indicate if this arm_pmu has long
340 * event counter support.
341 */
342static bool armv8pmu_has_long_event(struct arm_pmu *cpu_pmu)
343{
344 return (cpu_pmu->pmuver >= ID_AA64DFR0_PMUVER_8_5);
345}
346
Will Deacon03089682012-03-05 11:49:32 +0000347/*
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100348 * We must chain two programmable counters for 64 bit events,
349 * except when we have allocated the 64bit cycle counter (for CPU
350 * cycles event). This must be called only when the event has
351 * a counter allocated.
352 */
353static inline bool armv8pmu_event_is_chained(struct perf_event *event)
354{
355 int idx = event->hw.idx;
Andrew Murray8673e022020-03-02 18:17:52 +0000356 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100357
358 return !WARN_ON(idx < 0) &&
359 armv8pmu_event_is_64bit(event) &&
Andrew Murray8673e022020-03-02 18:17:52 +0000360 !armv8pmu_has_long_event(cpu_pmu) &&
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100361 (idx != ARMV8_IDX_CYCLE_COUNTER);
362}
363
364/*
Will Deacon03089682012-03-05 11:49:32 +0000365 * ARMv8 low level PMU access
366 */
367
368/*
369 * Perf Event to low level counters mapping
370 */
371#define ARMV8_IDX_TO_COUNTER(x) \
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000372 (((x) - ARMV8_IDX_COUNTER0) & ARMV8_PMU_COUNTER_MASK)
Will Deacon03089682012-03-05 11:49:32 +0000373
Mark Rutland0fdf1bb2020-09-24 12:07:01 +0100374/*
375 * This code is really good
376 */
377
378#define PMEVN_CASE(n, case_macro) \
379 case n: case_macro(n); break
380
381#define PMEVN_SWITCH(x, case_macro) \
382 do { \
383 switch (x) { \
384 PMEVN_CASE(0, case_macro); \
385 PMEVN_CASE(1, case_macro); \
386 PMEVN_CASE(2, case_macro); \
387 PMEVN_CASE(3, case_macro); \
388 PMEVN_CASE(4, case_macro); \
389 PMEVN_CASE(5, case_macro); \
390 PMEVN_CASE(6, case_macro); \
391 PMEVN_CASE(7, case_macro); \
392 PMEVN_CASE(8, case_macro); \
393 PMEVN_CASE(9, case_macro); \
394 PMEVN_CASE(10, case_macro); \
395 PMEVN_CASE(11, case_macro); \
396 PMEVN_CASE(12, case_macro); \
397 PMEVN_CASE(13, case_macro); \
398 PMEVN_CASE(14, case_macro); \
399 PMEVN_CASE(15, case_macro); \
400 PMEVN_CASE(16, case_macro); \
401 PMEVN_CASE(17, case_macro); \
402 PMEVN_CASE(18, case_macro); \
403 PMEVN_CASE(19, case_macro); \
404 PMEVN_CASE(20, case_macro); \
405 PMEVN_CASE(21, case_macro); \
406 PMEVN_CASE(22, case_macro); \
407 PMEVN_CASE(23, case_macro); \
408 PMEVN_CASE(24, case_macro); \
409 PMEVN_CASE(25, case_macro); \
410 PMEVN_CASE(26, case_macro); \
411 PMEVN_CASE(27, case_macro); \
412 PMEVN_CASE(28, case_macro); \
413 PMEVN_CASE(29, case_macro); \
414 PMEVN_CASE(30, case_macro); \
415 default: WARN(1, "Invalid PMEV* index\n"); \
416 } \
417 } while (0)
418
419#define RETURN_READ_PMEVCNTRN(n) \
420 return read_sysreg(pmevcntr##n##_el0)
421static unsigned long read_pmevcntrn(int n)
422{
423 PMEVN_SWITCH(n, RETURN_READ_PMEVCNTRN);
424 return 0;
425}
426
427#define WRITE_PMEVCNTRN(n) \
428 write_sysreg(val, pmevcntr##n##_el0)
429static void write_pmevcntrn(int n, unsigned long val)
430{
431 PMEVN_SWITCH(n, WRITE_PMEVCNTRN);
432}
433
434#define WRITE_PMEVTYPERN(n) \
435 write_sysreg(val, pmevtyper##n##_el0)
436static void write_pmevtypern(int n, unsigned long val)
437{
438 PMEVN_SWITCH(n, WRITE_PMEVTYPERN);
439}
440
Will Deacon03089682012-03-05 11:49:32 +0000441static inline u32 armv8pmu_pmcr_read(void)
442{
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700443 return read_sysreg(pmcr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000444}
445
446static inline void armv8pmu_pmcr_write(u32 val)
447{
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000448 val &= ARMV8_PMU_PMCR_MASK;
Will Deacon03089682012-03-05 11:49:32 +0000449 isb();
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700450 write_sysreg(val, pmcr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000451}
452
453static inline int armv8pmu_has_overflowed(u32 pmovsr)
454{
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000455 return pmovsr & ARMV8_PMU_OVERFLOWED_MASK;
Will Deacon03089682012-03-05 11:49:32 +0000456}
457
Will Deacon03089682012-03-05 11:49:32 +0000458static inline int armv8pmu_counter_has_overflowed(u32 pmnc, int idx)
459{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100460 return pmnc & BIT(ARMV8_IDX_TO_COUNTER(idx));
Will Deacon03089682012-03-05 11:49:32 +0000461}
462
Mark Rutland0fdf1bb2020-09-24 12:07:01 +0100463static inline u32 armv8pmu_read_evcntr(int idx)
Will Deacon03089682012-03-05 11:49:32 +0000464{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100465 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Will Deacon03089682012-03-05 11:49:32 +0000466
Mark Rutland0fdf1bb2020-09-24 12:07:01 +0100467 return read_pmevcntrn(counter);
Will Deacon03089682012-03-05 11:49:32 +0000468}
469
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100470static inline u64 armv8pmu_read_hw_counter(struct perf_event *event)
471{
472 int idx = event->hw.idx;
473 u64 val = 0;
474
475 val = armv8pmu_read_evcntr(idx);
476 if (armv8pmu_event_is_chained(event))
477 val = (val << 32) | armv8pmu_read_evcntr(idx - 1);
478 return val;
479}
480
Andrew Murray8673e022020-03-02 18:17:52 +0000481/*
482 * The cycle counter is always a 64-bit counter. When ARMV8_PMU_PMCR_LP
483 * is set the event counters also become 64-bit counters. Unless the
484 * user has requested a long counter (attr.config1) then we want to
485 * interrupt upon 32-bit overflow - we achieve this by applying a bias.
486 */
487static bool armv8pmu_event_needs_bias(struct perf_event *event)
488{
489 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
490 struct hw_perf_event *hwc = &event->hw;
491 int idx = hwc->idx;
492
493 if (armv8pmu_event_is_64bit(event))
494 return false;
495
496 if (armv8pmu_has_long_event(cpu_pmu) ||
497 idx == ARMV8_IDX_CYCLE_COUNTER)
498 return true;
499
500 return false;
501}
502
503static u64 armv8pmu_bias_long_counter(struct perf_event *event, u64 value)
504{
505 if (armv8pmu_event_needs_bias(event))
506 value |= GENMASK(63, 32);
507
508 return value;
509}
510
511static u64 armv8pmu_unbias_long_counter(struct perf_event *event, u64 value)
512{
513 if (armv8pmu_event_needs_bias(event))
514 value &= ~GENMASK(63, 32);
515
516 return value;
517}
518
Raphael Gault3d659e72019-04-11 17:16:46 +0100519static u64 armv8pmu_read_counter(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000520{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100521 struct hw_perf_event *hwc = &event->hw;
522 int idx = hwc->idx;
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100523 u64 value = 0;
Will Deacon03089682012-03-05 11:49:32 +0000524
Qi Liu44fdf4e2020-09-04 17:57:38 +0800525 if (idx == ARMV8_IDX_CYCLE_COUNTER)
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700526 value = read_sysreg(pmccntr_el0);
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100527 else
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100528 value = armv8pmu_read_hw_counter(event);
Will Deacon03089682012-03-05 11:49:32 +0000529
Andrew Murray8673e022020-03-02 18:17:52 +0000530 return armv8pmu_unbias_long_counter(event, value);
Will Deacon03089682012-03-05 11:49:32 +0000531}
532
Andrew Murray8673e022020-03-02 18:17:52 +0000533static inline void armv8pmu_write_evcntr(int idx, u64 value)
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100534{
Mark Rutland0fdf1bb2020-09-24 12:07:01 +0100535 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
536
537 write_pmevcntrn(counter, value);
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100538}
539
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100540static inline void armv8pmu_write_hw_counter(struct perf_event *event,
541 u64 value)
542{
543 int idx = event->hw.idx;
544
545 if (armv8pmu_event_is_chained(event)) {
546 armv8pmu_write_evcntr(idx, upper_32_bits(value));
547 armv8pmu_write_evcntr(idx - 1, lower_32_bits(value));
548 } else {
549 armv8pmu_write_evcntr(idx, value);
550 }
551}
552
Raphael Gault3d659e72019-04-11 17:16:46 +0100553static void armv8pmu_write_counter(struct perf_event *event, u64 value)
Will Deacon03089682012-03-05 11:49:32 +0000554{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100555 struct hw_perf_event *hwc = &event->hw;
556 int idx = hwc->idx;
557
Andrew Murray8673e022020-03-02 18:17:52 +0000558 value = armv8pmu_bias_long_counter(event, value);
559
Qi Liu44fdf4e2020-09-04 17:57:38 +0800560 if (idx == ARMV8_IDX_CYCLE_COUNTER)
Suzuki K Poulose3a952002018-07-10 09:57:59 +0100561 write_sysreg(value, pmccntr_el0);
Andrew Murray8673e022020-03-02 18:17:52 +0000562 else
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100563 armv8pmu_write_hw_counter(event, value);
Will Deacon03089682012-03-05 11:49:32 +0000564}
565
566static inline void armv8pmu_write_evtype(int idx, u32 val)
567{
Mark Rutland0fdf1bb2020-09-24 12:07:01 +0100568 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
569
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100570 val &= ARMV8_PMU_EVTYPE_MASK;
Mark Rutland0fdf1bb2020-09-24 12:07:01 +0100571 write_pmevtypern(counter, val);
Will Deacon03089682012-03-05 11:49:32 +0000572}
573
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100574static inline void armv8pmu_write_event_type(struct perf_event *event)
575{
576 struct hw_perf_event *hwc = &event->hw;
577 int idx = hwc->idx;
578
579 /*
580 * For chained events, the low counter is programmed to count
581 * the event of interest and the high counter is programmed
582 * with CHAIN event code with filters set to count at all ELs.
583 */
584 if (armv8pmu_event_is_chained(event)) {
585 u32 chain_evt = ARMV8_PMUV3_PERFCTR_CHAIN |
586 ARMV8_PMU_INCLUDE_EL2;
587
588 armv8pmu_write_evtype(idx - 1, hwc->config_base);
589 armv8pmu_write_evtype(idx, chain_evt);
590 } else {
Mark Rutland0fdf1bb2020-09-24 12:07:01 +0100591 if (idx == ARMV8_IDX_CYCLE_COUNTER)
592 write_sysreg(hwc->config_base, pmccfiltr_el0);
593 else
594 armv8pmu_write_evtype(idx, hwc->config_base);
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100595 }
596}
597
Robin Murphy29227d62020-03-17 18:22:54 +0000598static u32 armv8pmu_event_cnten_mask(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000599{
Robin Murphy29227d62020-03-17 18:22:54 +0000600 int counter = ARMV8_IDX_TO_COUNTER(event->hw.idx);
601 u32 mask = BIT(counter);
602
603 if (armv8pmu_event_is_chained(event))
604 mask |= BIT(counter - 1);
605 return mask;
606}
607
608static inline void armv8pmu_enable_counter(u32 mask)
609{
Alexandru Elisei490d7b72020-09-24 12:07:00 +0100610 /*
611 * Make sure event configuration register writes are visible before we
612 * enable the counter.
613 * */
614 isb();
Robin Murphy29227d62020-03-17 18:22:54 +0000615 write_sysreg(mask, pmcntenset_el0);
Will Deacon03089682012-03-05 11:49:32 +0000616}
617
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100618static inline void armv8pmu_enable_event_counter(struct perf_event *event)
619{
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100620 struct perf_event_attr *attr = &event->attr;
Robin Murphy29227d62020-03-17 18:22:54 +0000621 u32 mask = armv8pmu_event_cnten_mask(event);
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100622
Robin Murphy29227d62020-03-17 18:22:54 +0000623 kvm_set_pmu_events(mask, attr);
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100624
625 /* We rely on the hypervisor switch code to enable guest counters */
Robin Murphy29227d62020-03-17 18:22:54 +0000626 if (!kvm_pmu_counter_deferred(attr))
627 armv8pmu_enable_counter(mask);
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100628}
629
Robin Murphy29227d62020-03-17 18:22:54 +0000630static inline void armv8pmu_disable_counter(u32 mask)
Will Deacon03089682012-03-05 11:49:32 +0000631{
Robin Murphy29227d62020-03-17 18:22:54 +0000632 write_sysreg(mask, pmcntenclr_el0);
Mark Rutland0fdf1bb2020-09-24 12:07:01 +0100633 /*
634 * Make sure the effects of disabling the counter are visible before we
635 * start configuring the event.
636 */
637 isb();
Will Deacon03089682012-03-05 11:49:32 +0000638}
639
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100640static inline void armv8pmu_disable_event_counter(struct perf_event *event)
641{
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100642 struct perf_event_attr *attr = &event->attr;
Robin Murphy29227d62020-03-17 18:22:54 +0000643 u32 mask = armv8pmu_event_cnten_mask(event);
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100644
Robin Murphy29227d62020-03-17 18:22:54 +0000645 kvm_clr_pmu_events(mask);
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100646
647 /* We rely on the hypervisor switch code to disable guest counters */
Robin Murphy29227d62020-03-17 18:22:54 +0000648 if (!kvm_pmu_counter_deferred(attr))
649 armv8pmu_disable_counter(mask);
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100650}
651
Robin Murphy29227d62020-03-17 18:22:54 +0000652static inline void armv8pmu_enable_intens(u32 mask)
Will Deacon03089682012-03-05 11:49:32 +0000653{
Robin Murphy29227d62020-03-17 18:22:54 +0000654 write_sysreg(mask, pmintenset_el1);
Will Deacon03089682012-03-05 11:49:32 +0000655}
656
Robin Murphy29227d62020-03-17 18:22:54 +0000657static inline void armv8pmu_enable_event_irq(struct perf_event *event)
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100658{
Robin Murphy29227d62020-03-17 18:22:54 +0000659 u32 counter = ARMV8_IDX_TO_COUNTER(event->hw.idx);
660 armv8pmu_enable_intens(BIT(counter));
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100661}
662
Robin Murphy29227d62020-03-17 18:22:54 +0000663static inline void armv8pmu_disable_intens(u32 mask)
Will Deacon03089682012-03-05 11:49:32 +0000664{
Robin Murphy29227d62020-03-17 18:22:54 +0000665 write_sysreg(mask, pmintenclr_el1);
Will Deacon03089682012-03-05 11:49:32 +0000666 isb();
667 /* Clear the overflow flag in case an interrupt is pending. */
Robin Murphy29227d62020-03-17 18:22:54 +0000668 write_sysreg(mask, pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000669 isb();
Will Deacon03089682012-03-05 11:49:32 +0000670}
671
Robin Murphy29227d62020-03-17 18:22:54 +0000672static inline void armv8pmu_disable_event_irq(struct perf_event *event)
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100673{
Robin Murphy29227d62020-03-17 18:22:54 +0000674 u32 counter = ARMV8_IDX_TO_COUNTER(event->hw.idx);
675 armv8pmu_disable_intens(BIT(counter));
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100676}
677
Will Deacon03089682012-03-05 11:49:32 +0000678static inline u32 armv8pmu_getreset_flags(void)
679{
680 u32 value;
681
682 /* Read */
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700683 value = read_sysreg(pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000684
685 /* Write to clear flags */
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000686 value &= ARMV8_PMU_OVSR_MASK;
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700687 write_sysreg(value, pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000688
689 return value;
690}
691
Mark Rutland6475b2d2015-10-02 10:55:03 +0100692static void armv8pmu_enable_event(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000693{
Will Deacon03089682012-03-05 11:49:32 +0000694 /*
695 * Enable counter and interrupt, and set the counter to count
696 * the event that we're interested in.
697 */
Will Deacon03089682012-03-05 11:49:32 +0000698
699 /*
700 * Disable counter
701 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100702 armv8pmu_disable_event_counter(event);
Will Deacon03089682012-03-05 11:49:32 +0000703
704 /*
Mark Rutland0fdf1bb2020-09-24 12:07:01 +0100705 * Set event.
Will Deacon03089682012-03-05 11:49:32 +0000706 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100707 armv8pmu_write_event_type(event);
Will Deacon03089682012-03-05 11:49:32 +0000708
709 /*
710 * Enable interrupt for this counter
711 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100712 armv8pmu_enable_event_irq(event);
Will Deacon03089682012-03-05 11:49:32 +0000713
714 /*
715 * Enable counter
716 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100717 armv8pmu_enable_event_counter(event);
Will Deacon03089682012-03-05 11:49:32 +0000718}
719
Mark Rutland6475b2d2015-10-02 10:55:03 +0100720static void armv8pmu_disable_event(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000721{
Will Deacon03089682012-03-05 11:49:32 +0000722 /*
723 * Disable counter
724 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100725 armv8pmu_disable_event_counter(event);
Will Deacon03089682012-03-05 11:49:32 +0000726
727 /*
728 * Disable interrupt for this counter
729 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100730 armv8pmu_disable_event_irq(event);
Will Deacon03089682012-03-05 11:49:32 +0000731}
732
Suzuki K Poulose3cce50d2018-07-10 09:58:03 +0100733static void armv8pmu_start(struct arm_pmu *cpu_pmu)
734{
Suzuki K Poulose3cce50d2018-07-10 09:58:03 +0100735 /* Enable all counters */
736 armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMU_PMCR_E);
Suzuki K Poulose3cce50d2018-07-10 09:58:03 +0100737}
738
739static void armv8pmu_stop(struct arm_pmu *cpu_pmu)
740{
Suzuki K Poulose3cce50d2018-07-10 09:58:03 +0100741 /* Disable all counters */
742 armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMU_PMCR_E);
Suzuki K Poulose3cce50d2018-07-10 09:58:03 +0100743}
744
Mark Rutland0788f1e2018-05-10 11:35:15 +0100745static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +0000746{
747 u32 pmovsr;
748 struct perf_sample_data data;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100749 struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
Will Deacon03089682012-03-05 11:49:32 +0000750 struct pt_regs *regs;
751 int idx;
752
753 /*
754 * Get and reset the IRQ flags
755 */
756 pmovsr = armv8pmu_getreset_flags();
757
758 /*
759 * Did an overflow occur?
760 */
761 if (!armv8pmu_has_overflowed(pmovsr))
762 return IRQ_NONE;
763
764 /*
765 * Handle the counter(s) overflow(s)
766 */
767 regs = get_irq_regs();
768
Suzuki K Poulose3cce50d2018-07-10 09:58:03 +0100769 /*
770 * Stop the PMU while processing the counter overflows
771 * to prevent skews in group events.
772 */
773 armv8pmu_stop(cpu_pmu);
Will Deacon03089682012-03-05 11:49:32 +0000774 for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
775 struct perf_event *event = cpuc->events[idx];
776 struct hw_perf_event *hwc;
777
778 /* Ignore if we don't have an event. */
779 if (!event)
780 continue;
781
782 /*
783 * We have a single interrupt for all counters. Check that
784 * each counter has overflowed before we process it.
785 */
786 if (!armv8pmu_counter_has_overflowed(pmovsr, idx))
787 continue;
788
789 hwc = &event->hw;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100790 armpmu_event_update(event);
Will Deacon03089682012-03-05 11:49:32 +0000791 perf_sample_data_init(&data, 0, hwc->last_period);
Mark Rutland6475b2d2015-10-02 10:55:03 +0100792 if (!armpmu_event_set_period(event))
Will Deacon03089682012-03-05 11:49:32 +0000793 continue;
794
Julien Thierry05ab7282020-09-24 12:07:03 +0100795 /*
796 * Perf event overflow will queue the processing of the event as
797 * an irq_work which will be taken care of in the handling of
798 * IPI_IRQ_WORK.
799 */
Will Deacon03089682012-03-05 11:49:32 +0000800 if (perf_event_overflow(event, &data, regs))
Mark Rutland6475b2d2015-10-02 10:55:03 +0100801 cpu_pmu->disable(event);
Will Deacon03089682012-03-05 11:49:32 +0000802 }
Suzuki K Poulose3cce50d2018-07-10 09:58:03 +0100803 armv8pmu_start(cpu_pmu);
Will Deacon03089682012-03-05 11:49:32 +0000804
Will Deacon03089682012-03-05 11:49:32 +0000805 return IRQ_HANDLED;
806}
807
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100808static int armv8pmu_get_single_idx(struct pmu_hw_events *cpuc,
809 struct arm_pmu *cpu_pmu)
810{
811 int idx;
812
813 for (idx = ARMV8_IDX_COUNTER0; idx < cpu_pmu->num_events; idx ++) {
814 if (!test_and_set_bit(idx, cpuc->used_mask))
815 return idx;
816 }
817 return -EAGAIN;
818}
819
820static int armv8pmu_get_chain_idx(struct pmu_hw_events *cpuc,
821 struct arm_pmu *cpu_pmu)
822{
823 int idx;
824
825 /*
826 * Chaining requires two consecutive event counters, where
827 * the lower idx must be even.
828 */
829 for (idx = ARMV8_IDX_COUNTER0 + 1; idx < cpu_pmu->num_events; idx += 2) {
830 if (!test_and_set_bit(idx, cpuc->used_mask)) {
831 /* Check if the preceding even counter is available */
832 if (!test_and_set_bit(idx - 1, cpuc->used_mask))
833 return idx;
834 /* Release the Odd counter */
835 clear_bit(idx, cpuc->used_mask);
836 }
837 }
838 return -EAGAIN;
839}
840
Will Deacon03089682012-03-05 11:49:32 +0000841static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
Mark Rutland6475b2d2015-10-02 10:55:03 +0100842 struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000843{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100844 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
845 struct hw_perf_event *hwc = &event->hw;
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000846 unsigned long evtype = hwc->config_base & ARMV8_PMU_EVTYPE_EVENT;
Will Deacon03089682012-03-05 11:49:32 +0000847
Pratyush Anand1031a152017-07-01 12:03:35 +0530848 /* Always prefer to place a cycle counter into the cycle counter. */
Ashok Kumar03598fd2016-04-21 05:58:41 -0700849 if (evtype == ARMV8_PMUV3_PERFCTR_CPU_CYCLES) {
Pratyush Anand1031a152017-07-01 12:03:35 +0530850 if (!test_and_set_bit(ARMV8_IDX_CYCLE_COUNTER, cpuc->used_mask))
851 return ARMV8_IDX_CYCLE_COUNTER;
Will Deacon03089682012-03-05 11:49:32 +0000852 }
853
854 /*
Pratyush Anand1031a152017-07-01 12:03:35 +0530855 * Otherwise use events counters
Will Deacon03089682012-03-05 11:49:32 +0000856 */
Andrew Murray8673e022020-03-02 18:17:52 +0000857 if (armv8pmu_event_is_64bit(event) &&
858 !armv8pmu_has_long_event(cpu_pmu))
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100859 return armv8pmu_get_chain_idx(cpuc, cpu_pmu);
860 else
861 return armv8pmu_get_single_idx(cpuc, cpu_pmu);
Will Deacon03089682012-03-05 11:49:32 +0000862}
863
Suzuki K Poulose7dfc8db2018-07-10 09:58:01 +0100864static void armv8pmu_clear_event_idx(struct pmu_hw_events *cpuc,
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100865 struct perf_event *event)
Suzuki K Poulose7dfc8db2018-07-10 09:58:01 +0100866{
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100867 int idx = event->hw.idx;
868
869 clear_bit(idx, cpuc->used_mask);
870 if (armv8pmu_event_is_chained(event))
871 clear_bit(idx - 1, cpuc->used_mask);
Suzuki K Poulose7dfc8db2018-07-10 09:58:01 +0100872}
873
Will Deacon03089682012-03-05 11:49:32 +0000874/*
Andrew Murrayb3650672019-01-18 14:02:27 +0000875 * Add an event filter to a given event.
Will Deacon03089682012-03-05 11:49:32 +0000876 */
877static int armv8pmu_set_event_filter(struct hw_perf_event *event,
878 struct perf_event_attr *attr)
879{
880 unsigned long config_base = 0;
881
882 if (attr->exclude_idle)
883 return -EPERM;
Ganapatrao Kulkarni78a19cf2017-05-02 21:59:34 +0530884
885 /*
886 * If we're running in hyp mode, then we *are* the hypervisor.
887 * Therefore we ignore exclude_hv in this configuration, since
888 * there's no hypervisor to sample anyway. This is consistent
889 * with other architectures (x86 and Power).
890 */
891 if (is_kernel_in_hyp_mode()) {
Andrew Murray435e53f2019-04-09 20:22:15 +0100892 if (!attr->exclude_kernel && !attr->exclude_host)
Ganapatrao Kulkarni78a19cf2017-05-02 21:59:34 +0530893 config_base |= ARMV8_PMU_INCLUDE_EL2;
Andrew Murray435e53f2019-04-09 20:22:15 +0100894 if (attr->exclude_guest)
Ganapatrao Kulkarni78a19cf2017-05-02 21:59:34 +0530895 config_base |= ARMV8_PMU_EXCLUDE_EL1;
Andrew Murray435e53f2019-04-09 20:22:15 +0100896 if (attr->exclude_host)
897 config_base |= ARMV8_PMU_EXCLUDE_EL0;
Ganapatrao Kulkarni78a19cf2017-05-02 21:59:34 +0530898 } else {
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100899 if (!attr->exclude_hv && !attr->exclude_host)
Ganapatrao Kulkarni78a19cf2017-05-02 21:59:34 +0530900 config_base |= ARMV8_PMU_INCLUDE_EL2;
901 }
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100902
903 /*
904 * Filter out !VHE kernels and guest kernels
905 */
906 if (attr->exclude_kernel)
907 config_base |= ARMV8_PMU_EXCLUDE_EL1;
908
Will Deacon03089682012-03-05 11:49:32 +0000909 if (attr->exclude_user)
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000910 config_base |= ARMV8_PMU_EXCLUDE_EL0;
Will Deacon03089682012-03-05 11:49:32 +0000911
912 /*
913 * Install the filter into config_base as this is used to
914 * construct the event type.
915 */
916 event->config_base = config_base;
917
918 return 0;
919}
920
Will Deaconca2b4972018-10-05 13:24:36 +0100921static int armv8pmu_filter_match(struct perf_event *event)
922{
923 unsigned long evtype = event->hw.config_base & ARMV8_PMU_EVTYPE_EVENT;
924 return evtype != ARMV8_PMUV3_PERFCTR_CHAIN;
925}
926
Will Deacon03089682012-03-05 11:49:32 +0000927static void armv8pmu_reset(void *info)
928{
Andrew Murray8673e022020-03-02 18:17:52 +0000929 struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
930 u32 pmcr;
931
Will Deacon03089682012-03-05 11:49:32 +0000932 /* The counter and interrupt enable registers are unknown at reset. */
Robin Murphy29227d62020-03-17 18:22:54 +0000933 armv8pmu_disable_counter(U32_MAX);
934 armv8pmu_disable_intens(U32_MAX);
Will Deacon03089682012-03-05 11:49:32 +0000935
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100936 /* Clear the counters we flip at guest entry/exit */
937 kvm_clr_pmu_events(U32_MAX);
938
Jan Glauber7175f052016-02-18 17:50:13 +0100939 /*
940 * Initialize & Reset PMNC. Request overflow interrupt for
941 * 64 bit cycle counter but cheat in armv8pmu_write_counter().
942 */
Andrew Murray8673e022020-03-02 18:17:52 +0000943 pmcr = ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C | ARMV8_PMU_PMCR_LC;
944
945 /* Enable long event counter support where available */
946 if (armv8pmu_has_long_event(cpu_pmu))
947 pmcr |= ARMV8_PMU_PMCR_LP;
948
949 armv8pmu_pmcr_write(pmcr);
Will Deacon03089682012-03-05 11:49:32 +0000950}
951
Will Deacon6c833bb2017-08-08 16:58:33 +0100952static int __armv8_pmuv3_map_event(struct perf_event *event,
953 const unsigned (*extra_event_map)
954 [PERF_COUNT_HW_MAX],
955 const unsigned (*extra_cache_map)
956 [PERF_COUNT_HW_CACHE_MAX]
957 [PERF_COUNT_HW_CACHE_OP_MAX]
958 [PERF_COUNT_HW_CACHE_RESULT_MAX])
Will Deacon03089682012-03-05 11:49:32 +0000959{
Jeremy Linton236b9b912016-09-14 17:32:30 -0500960 int hw_event_id;
961 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
962
963 hw_event_id = armpmu_map_event(event, &armv8_pmuv3_perf_map,
964 &armv8_pmuv3_perf_cache_map,
965 ARMV8_PMU_EVTYPE_EVENT);
Jeremy Linton236b9b912016-09-14 17:32:30 -0500966
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100967 if (armv8pmu_event_is_64bit(event))
968 event->hw.flags |= ARMPMU_EVT_64BIT;
969
Shaokun Zhange2b5c5c2018-10-06 15:57:38 +0800970 /* Only expose micro/arch events supported by this PMU */
Will Deacon6c833bb2017-08-08 16:58:33 +0100971 if ((hw_event_id > 0) && (hw_event_id < ARMV8_PMUV3_MAX_COMMON_EVENTS)
972 && test_bit(hw_event_id, armpmu->pmceid_bitmap)) {
973 return hw_event_id;
Jeremy Linton236b9b912016-09-14 17:32:30 -0500974 }
975
Will Deacon6c833bb2017-08-08 16:58:33 +0100976 return armpmu_map_event(event, extra_event_map, extra_cache_map,
977 ARMV8_PMU_EVTYPE_EVENT);
978}
979
980static int armv8_pmuv3_map_event(struct perf_event *event)
981{
982 return __armv8_pmuv3_map_event(event, NULL, NULL);
Will Deacon03089682012-03-05 11:49:32 +0000983}
984
Mark Rutlandac82d122015-10-02 10:55:04 +0100985static int armv8_a53_map_event(struct perf_event *event)
986{
Will Deacond0d09d42017-08-08 17:11:27 +0100987 return __armv8_pmuv3_map_event(event, NULL, &armv8_a53_perf_cache_map);
Mark Rutlandac82d122015-10-02 10:55:04 +0100988}
989
Mark Rutland62a4dda2015-10-02 10:55:05 +0100990static int armv8_a57_map_event(struct perf_event *event)
991{
Will Deacond0d09d42017-08-08 17:11:27 +0100992 return __armv8_pmuv3_map_event(event, NULL, &armv8_a57_perf_cache_map);
Mark Rutland62a4dda2015-10-02 10:55:05 +0100993}
994
Julien Thierry5561b6c2017-08-09 17:46:38 +0100995static int armv8_a73_map_event(struct perf_event *event)
996{
997 return __armv8_pmuv3_map_event(event, NULL, &armv8_a73_perf_cache_map);
998}
999
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001000static int armv8_thunder_map_event(struct perf_event *event)
1001{
Will Deacond0d09d42017-08-08 17:11:27 +01001002 return __armv8_pmuv3_map_event(event, NULL,
Will Deacon6c833bb2017-08-08 16:58:33 +01001003 &armv8_thunder_perf_cache_map);
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001004}
1005
Ashok Kumar201a72b2016-04-21 05:58:45 -07001006static int armv8_vulcan_map_event(struct perf_event *event)
1007{
Will Deacond0d09d42017-08-08 17:11:27 +01001008 return __armv8_pmuv3_map_event(event, NULL,
Will Deacon6c833bb2017-08-08 16:58:33 +01001009 &armv8_vulcan_perf_cache_map);
Ashok Kumar201a72b2016-04-21 05:58:45 -07001010}
1011
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001012struct armv8pmu_probe_info {
1013 struct arm_pmu *pmu;
1014 bool present;
1015};
1016
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001017static void __armv8pmu_probe_pmu(void *info)
Will Deacon03089682012-03-05 11:49:32 +00001018{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001019 struct armv8pmu_probe_info *probe = info;
1020 struct arm_pmu *cpu_pmu = probe->pmu;
Mark Rutlandfaa9a082017-04-25 12:08:50 +01001021 u64 dfr0;
Will Deacon342e53b2018-10-05 13:28:07 +01001022 u64 pmceid_raw[2];
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001023 u32 pmceid[2];
Mark Rutlandfaa9a082017-04-25 12:08:50 +01001024 int pmuver;
Will Deacon03089682012-03-05 11:49:32 +00001025
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001026 dfr0 = read_sysreg(id_aa64dfr0_el1);
Mark Rutland03313652018-02-14 17:21:57 +00001027 pmuver = cpuid_feature_extract_unsigned_field(dfr0,
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001028 ID_AA64DFR0_PMUVER_SHIFT);
Mark Rutland03313652018-02-14 17:21:57 +00001029 if (pmuver == 0xf || pmuver == 0)
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001030 return;
1031
Andrew Murray8673e022020-03-02 18:17:52 +00001032 cpu_pmu->pmuver = pmuver;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001033 probe->present = true;
1034
Will Deacon03089682012-03-05 11:49:32 +00001035 /* Read the nb of CNTx counters supported from PMNC */
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001036 cpu_pmu->num_events = (armv8pmu_pmcr_read() >> ARMV8_PMU_PMCR_N_SHIFT)
1037 & ARMV8_PMU_PMCR_N_MASK;
Will Deacon03089682012-03-05 11:49:32 +00001038
Mark Rutland6475b2d2015-10-02 10:55:03 +01001039 /* Add the CPU cycles counter */
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001040 cpu_pmu->num_events += 1;
1041
Will Deacon342e53b2018-10-05 13:28:07 +01001042 pmceid[0] = pmceid_raw[0] = read_sysreg(pmceid0_el0);
1043 pmceid[1] = pmceid_raw[1] = read_sysreg(pmceid1_el0);
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001044
Yury Norov3aa56882018-02-06 15:38:06 -08001045 bitmap_from_arr32(cpu_pmu->pmceid_bitmap,
1046 pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
Will Deacon342e53b2018-10-05 13:28:07 +01001047
1048 pmceid[0] = pmceid_raw[0] >> 32;
1049 pmceid[1] = pmceid_raw[1] >> 32;
1050
1051 bitmap_from_arr32(cpu_pmu->pmceid_ext_bitmap,
1052 pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001053
1054 /* store PMMIR_EL1 register for sysfs */
1055 if (pmuver >= ID_AA64DFR0_PMUVER_8_4 && (pmceid_raw[1] & BIT(31)))
1056 cpu_pmu->reg_pmmir = read_cpuid(PMMIR_EL1);
1057 else
1058 cpu_pmu->reg_pmmir = 0;
Will Deacon03089682012-03-05 11:49:32 +00001059}
1060
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001061static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +00001062{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001063 struct armv8pmu_probe_info probe = {
1064 .pmu = cpu_pmu,
1065 .present = false,
1066 };
1067 int ret;
1068
1069 ret = smp_call_function_any(&cpu_pmu->supported_cpus,
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001070 __armv8pmu_probe_pmu,
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001071 &probe, 1);
1072 if (ret)
1073 return ret;
1074
1075 return probe.present ? 0 : -ENODEV;
Will Deacon03089682012-03-05 11:49:32 +00001076}
1077
Robin Murphye424b172020-02-21 19:35:31 +00001078static int armv8_pmu_init(struct arm_pmu *cpu_pmu, char *name,
1079 int (*map_event)(struct perf_event *event),
1080 const struct attribute_group *events,
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001081 const struct attribute_group *format,
1082 const struct attribute_group *caps)
Will Deacon03089682012-03-05 11:49:32 +00001083{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001084 int ret = armv8pmu_probe_pmu(cpu_pmu);
1085 if (ret)
1086 return ret;
1087
Will Deacond3adeed2018-10-05 13:26:21 +01001088 cpu_pmu->handle_irq = armv8pmu_handle_irq;
1089 cpu_pmu->enable = armv8pmu_enable_event;
1090 cpu_pmu->disable = armv8pmu_disable_event;
1091 cpu_pmu->read_counter = armv8pmu_read_counter;
1092 cpu_pmu->write_counter = armv8pmu_write_counter;
1093 cpu_pmu->get_event_idx = armv8pmu_get_event_idx;
1094 cpu_pmu->clear_event_idx = armv8pmu_clear_event_idx;
1095 cpu_pmu->start = armv8pmu_start;
1096 cpu_pmu->stop = armv8pmu_stop;
1097 cpu_pmu->reset = armv8pmu_reset;
Mark Rutlandac82d122015-10-02 10:55:04 +01001098 cpu_pmu->set_event_filter = armv8pmu_set_event_filter;
Will Deaconca2b4972018-10-05 13:24:36 +01001099 cpu_pmu->filter_match = armv8pmu_filter_match;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001100
Robin Murphye424b172020-02-21 19:35:31 +00001101 cpu_pmu->name = name;
1102 cpu_pmu->map_event = map_event;
1103 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] = events ?
1104 events : &armv8_pmuv3_events_attr_group;
1105 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] = format ?
1106 format : &armv8_pmuv3_format_attr_group;
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001107 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_CAPS] = caps ?
1108 caps : &armv8_pmuv3_caps_attr_group;
Robin Murphye424b172020-02-21 19:35:31 +00001109
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001110 return 0;
Mark Rutlandac82d122015-10-02 10:55:04 +01001111}
1112
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001113static int armv8_pmu_init_nogroups(struct arm_pmu *cpu_pmu, char *name,
1114 int (*map_event)(struct perf_event *event))
1115{
1116 return armv8_pmu_init(cpu_pmu, name, map_event, NULL, NULL, NULL);
1117}
1118
Mark Rutlandac82d122015-10-02 10:55:04 +01001119static int armv8_pmuv3_init(struct arm_pmu *cpu_pmu)
1120{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001121 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_pmuv3",
1122 armv8_pmuv3_map_event);
Mark Rutlandac82d122015-10-02 10:55:04 +01001123}
1124
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001125static int armv8_a34_pmu_init(struct arm_pmu *cpu_pmu)
1126{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001127 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a34",
1128 armv8_pmuv3_map_event);
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001129}
1130
Julien Thierrye884f802017-08-09 17:46:39 +01001131static int armv8_a35_pmu_init(struct arm_pmu *cpu_pmu)
1132{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001133 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a35",
1134 armv8_a53_map_event);
Julien Thierrye884f802017-08-09 17:46:39 +01001135}
1136
Mark Rutlandac82d122015-10-02 10:55:04 +01001137static int armv8_a53_pmu_init(struct arm_pmu *cpu_pmu)
1138{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001139 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a53",
1140 armv8_a53_map_event);
Will Deacon03089682012-03-05 11:49:32 +00001141}
Will Deacon03089682012-03-05 11:49:32 +00001142
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001143static int armv8_a55_pmu_init(struct arm_pmu *cpu_pmu)
1144{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001145 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a55",
1146 armv8_pmuv3_map_event);
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001147}
1148
Mark Rutland62a4dda2015-10-02 10:55:05 +01001149static int armv8_a57_pmu_init(struct arm_pmu *cpu_pmu)
1150{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001151 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a57",
1152 armv8_a57_map_event);
Mark Rutland62a4dda2015-10-02 10:55:05 +01001153}
1154
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001155static int armv8_a65_pmu_init(struct arm_pmu *cpu_pmu)
1156{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001157 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a65",
1158 armv8_pmuv3_map_event);
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001159}
1160
Will Deacon5d7ee872015-12-22 14:45:35 +00001161static int armv8_a72_pmu_init(struct arm_pmu *cpu_pmu)
1162{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001163 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a72",
1164 armv8_a57_map_event);
Will Deacon5d7ee872015-12-22 14:45:35 +00001165}
1166
Julien Thierry5561b6c2017-08-09 17:46:38 +01001167static int armv8_a73_pmu_init(struct arm_pmu *cpu_pmu)
1168{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001169 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a73",
1170 armv8_a73_map_event);
Julien Thierry5561b6c2017-08-09 17:46:38 +01001171}
1172
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001173static int armv8_a75_pmu_init(struct arm_pmu *cpu_pmu)
1174{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001175 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a75",
1176 armv8_pmuv3_map_event);
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001177}
1178
1179static int armv8_a76_pmu_init(struct arm_pmu *cpu_pmu)
1180{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001181 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a76",
1182 armv8_pmuv3_map_event);
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001183}
1184
1185static int armv8_a77_pmu_init(struct arm_pmu *cpu_pmu)
1186{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001187 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a77",
1188 armv8_pmuv3_map_event);
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001189}
1190
1191static int armv8_e1_pmu_init(struct arm_pmu *cpu_pmu)
1192{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001193 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_neoverse_e1",
1194 armv8_pmuv3_map_event);
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001195}
1196
1197static int armv8_n1_pmu_init(struct arm_pmu *cpu_pmu)
1198{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001199 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_neoverse_n1",
1200 armv8_pmuv3_map_event);
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001201}
1202
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001203static int armv8_thunder_pmu_init(struct arm_pmu *cpu_pmu)
1204{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001205 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cavium_thunder",
1206 armv8_thunder_map_event);
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001207}
1208
Ashok Kumar201a72b2016-04-21 05:58:45 -07001209static int armv8_vulcan_pmu_init(struct arm_pmu *cpu_pmu)
1210{
Shaokun Zhangf5be3a62020-09-22 13:53:45 +08001211 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_brcm_vulcan",
1212 armv8_vulcan_map_event);
Ashok Kumar201a72b2016-04-21 05:58:45 -07001213}
1214
Mark Rutland6475b2d2015-10-02 10:55:03 +01001215static const struct of_device_id armv8_pmu_of_device_ids[] = {
1216 {.compatible = "arm,armv8-pmuv3", .data = armv8_pmuv3_init},
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001217 {.compatible = "arm,cortex-a34-pmu", .data = armv8_a34_pmu_init},
Julien Thierrye884f802017-08-09 17:46:39 +01001218 {.compatible = "arm,cortex-a35-pmu", .data = armv8_a35_pmu_init},
Mark Rutlandac82d122015-10-02 10:55:04 +01001219 {.compatible = "arm,cortex-a53-pmu", .data = armv8_a53_pmu_init},
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001220 {.compatible = "arm,cortex-a55-pmu", .data = armv8_a55_pmu_init},
Mark Rutland62a4dda2015-10-02 10:55:05 +01001221 {.compatible = "arm,cortex-a57-pmu", .data = armv8_a57_pmu_init},
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001222 {.compatible = "arm,cortex-a65-pmu", .data = armv8_a65_pmu_init},
Will Deacon5d7ee872015-12-22 14:45:35 +00001223 {.compatible = "arm,cortex-a72-pmu", .data = armv8_a72_pmu_init},
Julien Thierry5561b6c2017-08-09 17:46:38 +01001224 {.compatible = "arm,cortex-a73-pmu", .data = armv8_a73_pmu_init},
Robin Murphy29cc4ce2020-02-21 19:35:32 +00001225 {.compatible = "arm,cortex-a75-pmu", .data = armv8_a75_pmu_init},
1226 {.compatible = "arm,cortex-a76-pmu", .data = armv8_a76_pmu_init},
1227 {.compatible = "arm,cortex-a77-pmu", .data = armv8_a77_pmu_init},
1228 {.compatible = "arm,neoverse-e1-pmu", .data = armv8_e1_pmu_init},
1229 {.compatible = "arm,neoverse-n1-pmu", .data = armv8_n1_pmu_init},
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001230 {.compatible = "cavium,thunder-pmu", .data = armv8_thunder_pmu_init},
Ashok Kumar201a72b2016-04-21 05:58:45 -07001231 {.compatible = "brcm,vulcan-pmu", .data = armv8_vulcan_pmu_init},
Will Deacon03089682012-03-05 11:49:32 +00001232 {},
1233};
1234
Mark Rutland6475b2d2015-10-02 10:55:03 +01001235static int armv8_pmu_device_probe(struct platform_device *pdev)
Will Deacon03089682012-03-05 11:49:32 +00001236{
Mark Rutlandf00fa5f2017-04-11 09:39:57 +01001237 return arm_pmu_device_probe(pdev, armv8_pmu_of_device_ids, NULL);
Will Deacon03089682012-03-05 11:49:32 +00001238}
1239
Mark Rutland6475b2d2015-10-02 10:55:03 +01001240static struct platform_driver armv8_pmu_driver = {
Will Deacon03089682012-03-05 11:49:32 +00001241 .driver = {
Jeremy Linton85023b22016-09-14 17:32:31 -05001242 .name = ARMV8_PMU_PDEV_NAME,
Mark Rutland6475b2d2015-10-02 10:55:03 +01001243 .of_match_table = armv8_pmu_of_device_ids,
Anders Roxell81e9fa82018-10-17 17:26:22 +02001244 .suppress_bind_attrs = true,
Will Deacon03089682012-03-05 11:49:32 +00001245 },
Mark Rutland6475b2d2015-10-02 10:55:03 +01001246 .probe = armv8_pmu_device_probe,
Will Deacon03089682012-03-05 11:49:32 +00001247};
1248
Mark Rutlandf00fa5f2017-04-11 09:39:57 +01001249static int __init armv8_pmu_driver_init(void)
1250{
1251 if (acpi_disabled)
1252 return platform_driver_register(&armv8_pmu_driver);
1253 else
1254 return arm_pmu_acpi_probe(armv8_pmuv3_init);
1255}
1256device_initcall(armv8_pmu_driver_init)
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -07001257
1258void arch_perf_update_userpage(struct perf_event *event,
1259 struct perf_event_mmap_page *userpg, u64 now)
1260{
Peter Zijlstra950b74dd2020-07-16 13:11:26 +08001261 struct clock_read_data *rd;
1262 unsigned int seq;
1263 u64 ns;
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -07001264
Peter Zijlstra279a8112020-07-16 13:11:27 +08001265 userpg->cap_user_time = 0;
1266 userpg->cap_user_time_zero = 0;
Peter Zijlstrac8f9eb02020-07-16 13:11:29 +08001267 userpg->cap_user_time_short = 0;
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -07001268
Peter Zijlstra950b74dd2020-07-16 13:11:26 +08001269 do {
1270 rd = sched_clock_read_begin(&seq);
1271
Peter Zijlstra279a8112020-07-16 13:11:27 +08001272 if (rd->read_sched_clock != arch_timer_read_counter)
1273 return;
1274
Peter Zijlstra950b74dd2020-07-16 13:11:26 +08001275 userpg->time_mult = rd->mult;
1276 userpg->time_shift = rd->shift;
1277 userpg->time_zero = rd->epoch_ns;
Peter Zijlstrac8f9eb02020-07-16 13:11:29 +08001278 userpg->time_cycles = rd->epoch_cyc;
1279 userpg->time_mask = rd->sched_clock_mask;
Peter Zijlstra950b74dd2020-07-16 13:11:26 +08001280
1281 /*
Peter Zijlstrac8f9eb02020-07-16 13:11:29 +08001282 * Subtract the cycle base, such that software that
1283 * doesn't know about cap_user_time_short still 'works'
1284 * assuming no wraps.
Peter Zijlstra950b74dd2020-07-16 13:11:26 +08001285 */
1286 ns = mul_u64_u32_shr(rd->epoch_cyc, rd->mult, rd->shift);
1287 userpg->time_zero -= ns;
1288
1289 } while (sched_clock_read_retry(seq));
1290
1291 userpg->time_offset = userpg->time_zero - now;
1292
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -07001293 /*
1294 * time_shift is not expected to be greater than 31 due to
1295 * the original published conversion algorithm shifting a
1296 * 32-bit value (now specifies a 64-bit value) - refer
1297 * perf_event_mmap_page documentation in perf_event.h.
1298 */
Peter Zijlstra950b74dd2020-07-16 13:11:26 +08001299 if (userpg->time_shift == 32) {
1300 userpg->time_shift = 31;
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -07001301 userpg->time_mult >>= 1;
1302 }
Peter Zijlstra950b74dd2020-07-16 13:11:26 +08001303
Peter Zijlstra279a8112020-07-16 13:11:27 +08001304 /*
1305 * Internal timekeeping for enabled/running/stopped times
1306 * is always computed with the sched_clock.
1307 */
1308 userpg->cap_user_time = 1;
1309 userpg->cap_user_time_zero = 1;
Peter Zijlstrac8f9eb02020-07-16 13:11:29 +08001310 userpg->cap_user_time_short = 1;
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -07001311}