• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ARMv8 PMUv3 Performance Events handling code.
4  *
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.
9  */
10 
11 #include <asm/irq_regs.h>
12 #include <asm/perf_event.h>
13 #include <asm/virt.h>
14 
15 #include <clocksource/arm_arch_timer.h>
16 
17 #include <linux/acpi.h>
18 #include <linux/bitfield.h>
19 #include <linux/clocksource.h>
20 #include <linux/of.h>
21 #include <linux/perf/arm_pmu.h>
22 #include <linux/perf/arm_pmuv3.h>
23 #include <linux/platform_device.h>
24 #include <linux/sched_clock.h>
25 #include <linux/smp.h>
26 #include <linux/nmi.h>
27 
28 /* ARMv8 Cortex-A53 specific event types. */
29 #define ARMV8_A53_PERFCTR_PREF_LINEFILL				0xC2
30 
31 /* ARMv8 Cavium ThunderX specific event types. */
32 #define ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST			0xE9
33 #define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS		0xEA
34 #define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS		0xEB
35 #define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS		0xEC
36 #define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS		0xED
37 
38 /*
39  * ARMv8 Architectural defined events, not all of these may
40  * be supported on any given implementation. Unsupported events will
41  * be disabled at run-time based on the PMCEID registers.
42  */
43 static const unsigned armv8_pmuv3_perf_map[PERF_COUNT_HW_MAX] = {
44 	PERF_MAP_ALL_UNSUPPORTED,
45 	[PERF_COUNT_HW_CPU_CYCLES]		= ARMV8_PMUV3_PERFCTR_CPU_CYCLES,
46 	[PERF_COUNT_HW_INSTRUCTIONS]		= ARMV8_PMUV3_PERFCTR_INST_RETIRED,
47 	[PERF_COUNT_HW_CACHE_REFERENCES]	= ARMV8_PMUV3_PERFCTR_L1D_CACHE,
48 	[PERF_COUNT_HW_CACHE_MISSES]		= ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
49 	[PERF_COUNT_HW_BRANCH_MISSES]		= ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
50 	[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,
53 };
54 
55 static 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] = {
58 	PERF_CACHE_MAP_ALL_UNSUPPORTED,
59 
60 	[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,
62 
63 	[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 
72 	[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 
75 	[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,
77 };
78 
79 static 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 
84 	[C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_A53_PERFCTR_PREF_LINEFILL,
85 
86 	[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,
88 };
89 
90 static 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 
95 	[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,
99 
100 	[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,
102 
103 	[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,
105 };
106 
107 static 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,
114 };
115 
116 static 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 
121 	[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,
127 
128 	[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,
130 
131 	[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,
135 };
136 
137 static 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 
147 	[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 
152 	[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 };
155 
156 static ssize_t
armv8pmu_events_sysfs_show(struct device * dev,struct device_attribute * attr,char * page)157 armv8pmu_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 
164 	return sprintf(page, "event=0x%04llx\n", pmu_attr->id);
165 }
166 
167 #define ARMV8_EVENT_ATTR(name, config)						\
168 	PMU_EVENT_ATTR_ID(name, armv8pmu_events_sysfs_show, config)
169 
170 static struct attribute *armv8_pmuv3_event_attrs[] = {
171 	/*
172 	 * Don't expose the sw_incr event in /sys. It's not usable as writes to
173 	 * PMSWINC_EL0 will trap as PMUSERENR.{SW,EN}=={0,0} and event rotation
174 	 * means we don't have a fixed event<->counter relationship regardless.
175 	 */
176 	ARMV8_EVENT_ATTR(l1i_cache_refill, ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL),
177 	ARMV8_EVENT_ATTR(l1i_tlb_refill, ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL),
178 	ARMV8_EVENT_ATTR(l1d_cache_refill, ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL),
179 	ARMV8_EVENT_ATTR(l1d_cache, ARMV8_PMUV3_PERFCTR_L1D_CACHE),
180 	ARMV8_EVENT_ATTR(l1d_tlb_refill, ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL),
181 	ARMV8_EVENT_ATTR(ld_retired, ARMV8_PMUV3_PERFCTR_LD_RETIRED),
182 	ARMV8_EVENT_ATTR(st_retired, ARMV8_PMUV3_PERFCTR_ST_RETIRED),
183 	ARMV8_EVENT_ATTR(inst_retired, ARMV8_PMUV3_PERFCTR_INST_RETIRED),
184 	ARMV8_EVENT_ATTR(exc_taken, ARMV8_PMUV3_PERFCTR_EXC_TAKEN),
185 	ARMV8_EVENT_ATTR(exc_return, ARMV8_PMUV3_PERFCTR_EXC_RETURN),
186 	ARMV8_EVENT_ATTR(cid_write_retired, ARMV8_PMUV3_PERFCTR_CID_WRITE_RETIRED),
187 	ARMV8_EVENT_ATTR(pc_write_retired, ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED),
188 	ARMV8_EVENT_ATTR(br_immed_retired, ARMV8_PMUV3_PERFCTR_BR_IMMED_RETIRED),
189 	ARMV8_EVENT_ATTR(br_return_retired, ARMV8_PMUV3_PERFCTR_BR_RETURN_RETIRED),
190 	ARMV8_EVENT_ATTR(unaligned_ldst_retired, ARMV8_PMUV3_PERFCTR_UNALIGNED_LDST_RETIRED),
191 	ARMV8_EVENT_ATTR(br_mis_pred, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED),
192 	ARMV8_EVENT_ATTR(cpu_cycles, ARMV8_PMUV3_PERFCTR_CPU_CYCLES),
193 	ARMV8_EVENT_ATTR(br_pred, ARMV8_PMUV3_PERFCTR_BR_PRED),
194 	ARMV8_EVENT_ATTR(mem_access, ARMV8_PMUV3_PERFCTR_MEM_ACCESS),
195 	ARMV8_EVENT_ATTR(l1i_cache, ARMV8_PMUV3_PERFCTR_L1I_CACHE),
196 	ARMV8_EVENT_ATTR(l1d_cache_wb, ARMV8_PMUV3_PERFCTR_L1D_CACHE_WB),
197 	ARMV8_EVENT_ATTR(l2d_cache, ARMV8_PMUV3_PERFCTR_L2D_CACHE),
198 	ARMV8_EVENT_ATTR(l2d_cache_refill, ARMV8_PMUV3_PERFCTR_L2D_CACHE_REFILL),
199 	ARMV8_EVENT_ATTR(l2d_cache_wb, ARMV8_PMUV3_PERFCTR_L2D_CACHE_WB),
200 	ARMV8_EVENT_ATTR(bus_access, ARMV8_PMUV3_PERFCTR_BUS_ACCESS),
201 	ARMV8_EVENT_ATTR(memory_error, ARMV8_PMUV3_PERFCTR_MEMORY_ERROR),
202 	ARMV8_EVENT_ATTR(inst_spec, ARMV8_PMUV3_PERFCTR_INST_SPEC),
203 	ARMV8_EVENT_ATTR(ttbr_write_retired, ARMV8_PMUV3_PERFCTR_TTBR_WRITE_RETIRED),
204 	ARMV8_EVENT_ATTR(bus_cycles, ARMV8_PMUV3_PERFCTR_BUS_CYCLES),
205 	/* Don't expose the chain event in /sys, since it's useless in isolation */
206 	ARMV8_EVENT_ATTR(l1d_cache_allocate, ARMV8_PMUV3_PERFCTR_L1D_CACHE_ALLOCATE),
207 	ARMV8_EVENT_ATTR(l2d_cache_allocate, ARMV8_PMUV3_PERFCTR_L2D_CACHE_ALLOCATE),
208 	ARMV8_EVENT_ATTR(br_retired, ARMV8_PMUV3_PERFCTR_BR_RETIRED),
209 	ARMV8_EVENT_ATTR(br_mis_pred_retired, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED_RETIRED),
210 	ARMV8_EVENT_ATTR(stall_frontend, ARMV8_PMUV3_PERFCTR_STALL_FRONTEND),
211 	ARMV8_EVENT_ATTR(stall_backend, ARMV8_PMUV3_PERFCTR_STALL_BACKEND),
212 	ARMV8_EVENT_ATTR(l1d_tlb, ARMV8_PMUV3_PERFCTR_L1D_TLB),
213 	ARMV8_EVENT_ATTR(l1i_tlb, ARMV8_PMUV3_PERFCTR_L1I_TLB),
214 	ARMV8_EVENT_ATTR(l2i_cache, ARMV8_PMUV3_PERFCTR_L2I_CACHE),
215 	ARMV8_EVENT_ATTR(l2i_cache_refill, ARMV8_PMUV3_PERFCTR_L2I_CACHE_REFILL),
216 	ARMV8_EVENT_ATTR(l3d_cache_allocate, ARMV8_PMUV3_PERFCTR_L3D_CACHE_ALLOCATE),
217 	ARMV8_EVENT_ATTR(l3d_cache_refill, ARMV8_PMUV3_PERFCTR_L3D_CACHE_REFILL),
218 	ARMV8_EVENT_ATTR(l3d_cache, ARMV8_PMUV3_PERFCTR_L3D_CACHE),
219 	ARMV8_EVENT_ATTR(l3d_cache_wb, ARMV8_PMUV3_PERFCTR_L3D_CACHE_WB),
220 	ARMV8_EVENT_ATTR(l2d_tlb_refill, ARMV8_PMUV3_PERFCTR_L2D_TLB_REFILL),
221 	ARMV8_EVENT_ATTR(l2i_tlb_refill, ARMV8_PMUV3_PERFCTR_L2I_TLB_REFILL),
222 	ARMV8_EVENT_ATTR(l2d_tlb, ARMV8_PMUV3_PERFCTR_L2D_TLB),
223 	ARMV8_EVENT_ATTR(l2i_tlb, ARMV8_PMUV3_PERFCTR_L2I_TLB),
224 	ARMV8_EVENT_ATTR(remote_access, ARMV8_PMUV3_PERFCTR_REMOTE_ACCESS),
225 	ARMV8_EVENT_ATTR(ll_cache, ARMV8_PMUV3_PERFCTR_LL_CACHE),
226 	ARMV8_EVENT_ATTR(ll_cache_miss, ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS),
227 	ARMV8_EVENT_ATTR(dtlb_walk, ARMV8_PMUV3_PERFCTR_DTLB_WALK),
228 	ARMV8_EVENT_ATTR(itlb_walk, ARMV8_PMUV3_PERFCTR_ITLB_WALK),
229 	ARMV8_EVENT_ATTR(ll_cache_rd, ARMV8_PMUV3_PERFCTR_LL_CACHE_RD),
230 	ARMV8_EVENT_ATTR(ll_cache_miss_rd, ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS_RD),
231 	ARMV8_EVENT_ATTR(remote_access_rd, ARMV8_PMUV3_PERFCTR_REMOTE_ACCESS_RD),
232 	ARMV8_EVENT_ATTR(l1d_cache_lmiss_rd, ARMV8_PMUV3_PERFCTR_L1D_CACHE_LMISS_RD),
233 	ARMV8_EVENT_ATTR(op_retired, ARMV8_PMUV3_PERFCTR_OP_RETIRED),
234 	ARMV8_EVENT_ATTR(op_spec, ARMV8_PMUV3_PERFCTR_OP_SPEC),
235 	ARMV8_EVENT_ATTR(stall, ARMV8_PMUV3_PERFCTR_STALL),
236 	ARMV8_EVENT_ATTR(stall_slot_backend, ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND),
237 	ARMV8_EVENT_ATTR(stall_slot_frontend, ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND),
238 	ARMV8_EVENT_ATTR(stall_slot, ARMV8_PMUV3_PERFCTR_STALL_SLOT),
239 	ARMV8_EVENT_ATTR(sample_pop, ARMV8_SPE_PERFCTR_SAMPLE_POP),
240 	ARMV8_EVENT_ATTR(sample_feed, ARMV8_SPE_PERFCTR_SAMPLE_FEED),
241 	ARMV8_EVENT_ATTR(sample_filtrate, ARMV8_SPE_PERFCTR_SAMPLE_FILTRATE),
242 	ARMV8_EVENT_ATTR(sample_collision, ARMV8_SPE_PERFCTR_SAMPLE_COLLISION),
243 	ARMV8_EVENT_ATTR(cnt_cycles, ARMV8_AMU_PERFCTR_CNT_CYCLES),
244 	ARMV8_EVENT_ATTR(stall_backend_mem, ARMV8_AMU_PERFCTR_STALL_BACKEND_MEM),
245 	ARMV8_EVENT_ATTR(l1i_cache_lmiss, ARMV8_PMUV3_PERFCTR_L1I_CACHE_LMISS),
246 	ARMV8_EVENT_ATTR(l2d_cache_lmiss_rd, ARMV8_PMUV3_PERFCTR_L2D_CACHE_LMISS_RD),
247 	ARMV8_EVENT_ATTR(l2i_cache_lmiss, ARMV8_PMUV3_PERFCTR_L2I_CACHE_LMISS),
248 	ARMV8_EVENT_ATTR(l3d_cache_lmiss_rd, ARMV8_PMUV3_PERFCTR_L3D_CACHE_LMISS_RD),
249 	ARMV8_EVENT_ATTR(trb_wrap, ARMV8_PMUV3_PERFCTR_TRB_WRAP),
250 	ARMV8_EVENT_ATTR(trb_trig, ARMV8_PMUV3_PERFCTR_TRB_TRIG),
251 	ARMV8_EVENT_ATTR(trcextout0, ARMV8_PMUV3_PERFCTR_TRCEXTOUT0),
252 	ARMV8_EVENT_ATTR(trcextout1, ARMV8_PMUV3_PERFCTR_TRCEXTOUT1),
253 	ARMV8_EVENT_ATTR(trcextout2, ARMV8_PMUV3_PERFCTR_TRCEXTOUT2),
254 	ARMV8_EVENT_ATTR(trcextout3, ARMV8_PMUV3_PERFCTR_TRCEXTOUT3),
255 	ARMV8_EVENT_ATTR(cti_trigout4, ARMV8_PMUV3_PERFCTR_CTI_TRIGOUT4),
256 	ARMV8_EVENT_ATTR(cti_trigout5, ARMV8_PMUV3_PERFCTR_CTI_TRIGOUT5),
257 	ARMV8_EVENT_ATTR(cti_trigout6, ARMV8_PMUV3_PERFCTR_CTI_TRIGOUT6),
258 	ARMV8_EVENT_ATTR(cti_trigout7, ARMV8_PMUV3_PERFCTR_CTI_TRIGOUT7),
259 	ARMV8_EVENT_ATTR(ldst_align_lat, ARMV8_PMUV3_PERFCTR_LDST_ALIGN_LAT),
260 	ARMV8_EVENT_ATTR(ld_align_lat, ARMV8_PMUV3_PERFCTR_LD_ALIGN_LAT),
261 	ARMV8_EVENT_ATTR(st_align_lat, ARMV8_PMUV3_PERFCTR_ST_ALIGN_LAT),
262 	ARMV8_EVENT_ATTR(mem_access_checked, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED),
263 	ARMV8_EVENT_ATTR(mem_access_checked_rd, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED_RD),
264 	ARMV8_EVENT_ATTR(mem_access_checked_wr, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED_WR),
265 	NULL,
266 };
267 
268 static umode_t
armv8pmu_event_attr_is_visible(struct kobject * kobj,struct attribute * attr,int unused)269 armv8pmu_event_attr_is_visible(struct kobject *kobj,
270 			       struct attribute *attr, int unused)
271 {
272 	struct device *dev = kobj_to_dev(kobj);
273 	struct pmu *pmu = dev_get_drvdata(dev);
274 	struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
275 	struct perf_pmu_events_attr *pmu_attr;
276 
277 	pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
278 
279 	if (pmu_attr->id < ARMV8_PMUV3_MAX_COMMON_EVENTS &&
280 	    test_bit(pmu_attr->id, cpu_pmu->pmceid_bitmap))
281 		return attr->mode;
282 
283 	if (pmu_attr->id >= ARMV8_PMUV3_EXT_COMMON_EVENT_BASE) {
284 		u64 id = pmu_attr->id - ARMV8_PMUV3_EXT_COMMON_EVENT_BASE;
285 
286 		if (id < ARMV8_PMUV3_MAX_COMMON_EVENTS &&
287 		    test_bit(id, cpu_pmu->pmceid_ext_bitmap))
288 			return attr->mode;
289 	}
290 
291 	return 0;
292 }
293 
294 static const struct attribute_group armv8_pmuv3_events_attr_group = {
295 	.name = "events",
296 	.attrs = armv8_pmuv3_event_attrs,
297 	.is_visible = armv8pmu_event_attr_is_visible,
298 };
299 
300 /* User ABI */
301 #define ATTR_CFG_FLD_event_CFG		config
302 #define ATTR_CFG_FLD_event_LO		0
303 #define ATTR_CFG_FLD_event_HI		15
304 #define ATTR_CFG_FLD_long_CFG		config1
305 #define ATTR_CFG_FLD_long_LO		0
306 #define ATTR_CFG_FLD_long_HI		0
307 #define ATTR_CFG_FLD_rdpmc_CFG		config1
308 #define ATTR_CFG_FLD_rdpmc_LO		1
309 #define ATTR_CFG_FLD_rdpmc_HI		1
310 #define ATTR_CFG_FLD_threshold_count_CFG	config1 /* PMEVTYPER.TC[0] */
311 #define ATTR_CFG_FLD_threshold_count_LO		2
312 #define ATTR_CFG_FLD_threshold_count_HI		2
313 #define ATTR_CFG_FLD_threshold_compare_CFG	config1 /* PMEVTYPER.TC[2:1] */
314 #define ATTR_CFG_FLD_threshold_compare_LO	3
315 #define ATTR_CFG_FLD_threshold_compare_HI	4
316 #define ATTR_CFG_FLD_threshold_CFG		config1 /* PMEVTYPER.TH */
317 #define ATTR_CFG_FLD_threshold_LO		5
318 #define ATTR_CFG_FLD_threshold_HI		16
319 
320 GEN_PMU_FORMAT_ATTR(event);
321 GEN_PMU_FORMAT_ATTR(long);
322 GEN_PMU_FORMAT_ATTR(rdpmc);
323 GEN_PMU_FORMAT_ATTR(threshold_count);
324 GEN_PMU_FORMAT_ATTR(threshold_compare);
325 GEN_PMU_FORMAT_ATTR(threshold);
326 
327 static int sysctl_perf_user_access __read_mostly;
328 static int sysctl_export_pmu_events __read_mostly;
329 
armv8pmu_event_is_64bit(struct perf_event * event)330 static bool armv8pmu_event_is_64bit(struct perf_event *event)
331 {
332 	return ATTR_CFG_GET_FLD(&event->attr, long);
333 }
334 
armv8pmu_event_want_user_access(struct perf_event * event)335 static bool armv8pmu_event_want_user_access(struct perf_event *event)
336 {
337 	return ATTR_CFG_GET_FLD(&event->attr, rdpmc);
338 }
339 
armv8pmu_event_get_threshold(struct perf_event_attr * attr)340 static u32 armv8pmu_event_get_threshold(struct perf_event_attr *attr)
341 {
342 	return ATTR_CFG_GET_FLD(attr, threshold);
343 }
344 
armv8pmu_event_threshold_control(struct perf_event_attr * attr)345 static u8 armv8pmu_event_threshold_control(struct perf_event_attr *attr)
346 {
347 	u8 th_compare = ATTR_CFG_GET_FLD(attr, threshold_compare);
348 	u8 th_count = ATTR_CFG_GET_FLD(attr, threshold_count);
349 
350 	/*
351 	 * The count bit is always the bottom bit of the full control field, and
352 	 * the comparison is the upper two bits, but it's not explicitly
353 	 * labelled in the Arm ARM. For the Perf interface we split it into two
354 	 * fields, so reconstruct it here.
355 	 */
356 	return (th_compare << 1) | th_count;
357 }
358 
359 static struct attribute *armv8_pmuv3_format_attrs[] = {
360 	&format_attr_event.attr,
361 	&format_attr_long.attr,
362 	&format_attr_rdpmc.attr,
363 	&format_attr_threshold.attr,
364 	&format_attr_threshold_compare.attr,
365 	&format_attr_threshold_count.attr,
366 	NULL,
367 };
368 
369 static const struct attribute_group armv8_pmuv3_format_attr_group = {
370 	.name = "format",
371 	.attrs = armv8_pmuv3_format_attrs,
372 };
373 
slots_show(struct device * dev,struct device_attribute * attr,char * page)374 static ssize_t slots_show(struct device *dev, struct device_attribute *attr,
375 			  char *page)
376 {
377 	struct pmu *pmu = dev_get_drvdata(dev);
378 	struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
379 	u32 slots = FIELD_GET(ARMV8_PMU_SLOTS, cpu_pmu->reg_pmmir);
380 
381 	return sysfs_emit(page, "0x%08x\n", slots);
382 }
383 
384 static DEVICE_ATTR_RO(slots);
385 
bus_slots_show(struct device * dev,struct device_attribute * attr,char * page)386 static ssize_t bus_slots_show(struct device *dev, struct device_attribute *attr,
387 			      char *page)
388 {
389 	struct pmu *pmu = dev_get_drvdata(dev);
390 	struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
391 	u32 bus_slots = FIELD_GET(ARMV8_PMU_BUS_SLOTS, cpu_pmu->reg_pmmir);
392 
393 	return sysfs_emit(page, "0x%08x\n", bus_slots);
394 }
395 
396 static DEVICE_ATTR_RO(bus_slots);
397 
bus_width_show(struct device * dev,struct device_attribute * attr,char * page)398 static ssize_t bus_width_show(struct device *dev, struct device_attribute *attr,
399 			      char *page)
400 {
401 	struct pmu *pmu = dev_get_drvdata(dev);
402 	struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
403 	u32 bus_width = FIELD_GET(ARMV8_PMU_BUS_WIDTH, cpu_pmu->reg_pmmir);
404 	u32 val = 0;
405 
406 	/* Encoded as Log2(number of bytes), plus one */
407 	if (bus_width > 2 && bus_width < 13)
408 		val = 1 << (bus_width - 1);
409 
410 	return sysfs_emit(page, "0x%08x\n", val);
411 }
412 
413 static DEVICE_ATTR_RO(bus_width);
414 
threshold_max(struct arm_pmu * cpu_pmu)415 static u32 threshold_max(struct arm_pmu *cpu_pmu)
416 {
417 	/*
418 	 * PMMIR.THWIDTH is readable and non-zero on aarch32, but it would be
419 	 * impossible to write the threshold in the upper 32 bits of PMEVTYPER.
420 	 */
421 	if (IS_ENABLED(CONFIG_ARM))
422 		return 0;
423 
424 	/*
425 	 * The largest value that can be written to PMEVTYPER<n>_EL0.TH is
426 	 * (2 ^ PMMIR.THWIDTH) - 1.
427 	 */
428 	return (1 << FIELD_GET(ARMV8_PMU_THWIDTH, cpu_pmu->reg_pmmir)) - 1;
429 }
430 
threshold_max_show(struct device * dev,struct device_attribute * attr,char * page)431 static ssize_t threshold_max_show(struct device *dev,
432 				  struct device_attribute *attr, char *page)
433 {
434 	struct pmu *pmu = dev_get_drvdata(dev);
435 	struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
436 
437 	return sysfs_emit(page, "0x%08x\n", threshold_max(cpu_pmu));
438 }
439 
440 static DEVICE_ATTR_RO(threshold_max);
441 
442 static struct attribute *armv8_pmuv3_caps_attrs[] = {
443 	&dev_attr_slots.attr,
444 	&dev_attr_bus_slots.attr,
445 	&dev_attr_bus_width.attr,
446 	&dev_attr_threshold_max.attr,
447 	NULL,
448 };
449 
450 static const struct attribute_group armv8_pmuv3_caps_attr_group = {
451 	.name = "caps",
452 	.attrs = armv8_pmuv3_caps_attrs,
453 };
454 
455 /*
456  * We unconditionally enable ARMv8.5-PMU long event counter support
457  * (64-bit events) where supported. Indicate if this arm_pmu has long
458  * event counter support.
459  *
460  * On AArch32, long counters make no sense (you can't access the top
461  * bits), so we only enable this on AArch64.
462  */
armv8pmu_has_long_event(struct arm_pmu * cpu_pmu)463 static bool armv8pmu_has_long_event(struct arm_pmu *cpu_pmu)
464 {
465 	return (IS_ENABLED(CONFIG_ARM64) && is_pmuv3p5(cpu_pmu->pmuver));
466 }
467 
armv8pmu_event_has_user_read(struct perf_event * event)468 static bool armv8pmu_event_has_user_read(struct perf_event *event)
469 {
470 	return event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT;
471 }
472 
473 /*
474  * We must chain two programmable counters for 64 bit events,
475  * except when we have allocated the 64bit cycle counter (for CPU
476  * cycles event) or when user space counter access is enabled.
477  */
armv8pmu_event_is_chained(struct perf_event * event)478 static bool armv8pmu_event_is_chained(struct perf_event *event)
479 {
480 	int idx = event->hw.idx;
481 	struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
482 
483 	return !armv8pmu_event_has_user_read(event) &&
484 	       armv8pmu_event_is_64bit(event) &&
485 	       !armv8pmu_has_long_event(cpu_pmu) &&
486 	       (idx < ARMV8_PMU_MAX_GENERAL_COUNTERS);
487 }
488 
489 /*
490  * ARMv8 low level PMU access
491  */
armv8pmu_pmcr_read(void)492 static u64 armv8pmu_pmcr_read(void)
493 {
494 	return read_pmcr();
495 }
496 
armv8pmu_pmcr_write(u64 val)497 static void armv8pmu_pmcr_write(u64 val)
498 {
499 	val &= ARMV8_PMU_PMCR_MASK;
500 	isb();
501 	write_pmcr(val);
502 }
503 
armv8pmu_has_overflowed(u64 pmovsr)504 static int armv8pmu_has_overflowed(u64 pmovsr)
505 {
506 	return !!(pmovsr & ARMV8_PMU_OVERFLOWED_MASK);
507 }
508 
armv8pmu_counter_has_overflowed(u64 pmnc,int idx)509 static int armv8pmu_counter_has_overflowed(u64 pmnc, int idx)
510 {
511 	return !!(pmnc & BIT(idx));
512 }
513 
armv8pmu_read_evcntr(int idx)514 static u64 armv8pmu_read_evcntr(int idx)
515 {
516 	return read_pmevcntrn(idx);
517 }
518 
armv8pmu_read_hw_counter(struct perf_event * event)519 static u64 armv8pmu_read_hw_counter(struct perf_event *event)
520 {
521 	int idx = event->hw.idx;
522 	u64 val = armv8pmu_read_evcntr(idx);
523 
524 	if (armv8pmu_event_is_chained(event))
525 		val = (val << 32) | armv8pmu_read_evcntr(idx - 1);
526 	return val;
527 }
528 
529 /*
530  * The cycle counter is always a 64-bit counter. When ARMV8_PMU_PMCR_LP
531  * is set the event counters also become 64-bit counters. Unless the
532  * user has requested a long counter (attr.config1) then we want to
533  * interrupt upon 32-bit overflow - we achieve this by applying a bias.
534  */
armv8pmu_event_needs_bias(struct perf_event * event)535 static bool armv8pmu_event_needs_bias(struct perf_event *event)
536 {
537 	struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
538 	struct hw_perf_event *hwc = &event->hw;
539 	int idx = hwc->idx;
540 
541 	if (armv8pmu_event_is_64bit(event))
542 		return false;
543 
544 	if (armv8pmu_has_long_event(cpu_pmu) ||
545 	    idx >= ARMV8_PMU_MAX_GENERAL_COUNTERS)
546 		return true;
547 
548 	return false;
549 }
550 
armv8pmu_bias_long_counter(struct perf_event * event,u64 value)551 static u64 armv8pmu_bias_long_counter(struct perf_event *event, u64 value)
552 {
553 	if (armv8pmu_event_needs_bias(event))
554 		value |= GENMASK_ULL(63, 32);
555 
556 	return value;
557 }
558 
armv8pmu_unbias_long_counter(struct perf_event * event,u64 value)559 static u64 armv8pmu_unbias_long_counter(struct perf_event *event, u64 value)
560 {
561 	if (armv8pmu_event_needs_bias(event))
562 		value &= ~GENMASK_ULL(63, 32);
563 
564 	return value;
565 }
566 
armv8pmu_read_counter(struct perf_event * event)567 static u64 armv8pmu_read_counter(struct perf_event *event)
568 {
569 	struct hw_perf_event *hwc = &event->hw;
570 	int idx = hwc->idx;
571 	u64 value;
572 
573 	if (idx == ARMV8_PMU_CYCLE_IDX)
574 		value = read_pmccntr();
575 	else if (idx == ARMV8_PMU_INSTR_IDX)
576 		value = read_pmicntr();
577 	else
578 		value = armv8pmu_read_hw_counter(event);
579 
580 	return  armv8pmu_unbias_long_counter(event, value);
581 }
582 
armv8pmu_write_evcntr(int idx,u64 value)583 static void armv8pmu_write_evcntr(int idx, u64 value)
584 {
585 	write_pmevcntrn(idx, value);
586 }
587 
armv8pmu_write_hw_counter(struct perf_event * event,u64 value)588 static void armv8pmu_write_hw_counter(struct perf_event *event,
589 					     u64 value)
590 {
591 	int idx = event->hw.idx;
592 
593 	if (armv8pmu_event_is_chained(event)) {
594 		armv8pmu_write_evcntr(idx, upper_32_bits(value));
595 		armv8pmu_write_evcntr(idx - 1, lower_32_bits(value));
596 	} else {
597 		armv8pmu_write_evcntr(idx, value);
598 	}
599 }
600 
armv8pmu_write_counter(struct perf_event * event,u64 value)601 static void armv8pmu_write_counter(struct perf_event *event, u64 value)
602 {
603 	struct hw_perf_event *hwc = &event->hw;
604 	int idx = hwc->idx;
605 
606 	value = armv8pmu_bias_long_counter(event, value);
607 
608 	if (idx == ARMV8_PMU_CYCLE_IDX)
609 		write_pmccntr(value);
610 	else if (idx == ARMV8_PMU_INSTR_IDX)
611 		write_pmicntr(value);
612 	else
613 		armv8pmu_write_hw_counter(event, value);
614 }
615 
armv8pmu_write_evtype(int idx,unsigned long val)616 static void armv8pmu_write_evtype(int idx, unsigned long val)
617 {
618 	unsigned long mask = ARMV8_PMU_EVTYPE_EVENT |
619 			     ARMV8_PMU_INCLUDE_EL2 |
620 			     ARMV8_PMU_EXCLUDE_EL0 |
621 			     ARMV8_PMU_EXCLUDE_EL1;
622 
623 	if (IS_ENABLED(CONFIG_ARM64))
624 		mask |= ARMV8_PMU_EVTYPE_TC | ARMV8_PMU_EVTYPE_TH;
625 
626 	val &= mask;
627 	write_pmevtypern(idx, val);
628 }
629 
armv8pmu_write_event_type(struct perf_event * event)630 static void armv8pmu_write_event_type(struct perf_event *event)
631 {
632 	struct hw_perf_event *hwc = &event->hw;
633 	int idx = hwc->idx;
634 
635 	/*
636 	 * For chained events, the low counter is programmed to count
637 	 * the event of interest and the high counter is programmed
638 	 * with CHAIN event code with filters set to count at all ELs.
639 	 */
640 	if (armv8pmu_event_is_chained(event)) {
641 		u32 chain_evt = ARMV8_PMUV3_PERFCTR_CHAIN |
642 				ARMV8_PMU_INCLUDE_EL2;
643 
644 		armv8pmu_write_evtype(idx - 1, hwc->config_base);
645 		armv8pmu_write_evtype(idx, chain_evt);
646 	} else {
647 		if (idx == ARMV8_PMU_CYCLE_IDX)
648 			write_pmccfiltr(hwc->config_base);
649 		else if (idx == ARMV8_PMU_INSTR_IDX)
650 			write_pmicfiltr(hwc->config_base);
651 		else
652 			armv8pmu_write_evtype(idx, hwc->config_base);
653 	}
654 }
655 
armv8pmu_event_cnten_mask(struct perf_event * event)656 static u64 armv8pmu_event_cnten_mask(struct perf_event *event)
657 {
658 	int counter = event->hw.idx;
659 	u64 mask = BIT(counter);
660 
661 	if (armv8pmu_event_is_chained(event))
662 		mask |= BIT(counter - 1);
663 	return mask;
664 }
665 
armv8pmu_enable_counter(u64 mask)666 static void armv8pmu_enable_counter(u64 mask)
667 {
668 	/*
669 	 * Make sure event configuration register writes are visible before we
670 	 * enable the counter.
671 	 * */
672 	isb();
673 	write_pmcntenset(mask);
674 }
675 
armv8pmu_enable_event_counter(struct perf_event * event)676 static void armv8pmu_enable_event_counter(struct perf_event *event)
677 {
678 	struct perf_event_attr *attr = &event->attr;
679 	u64 mask = armv8pmu_event_cnten_mask(event);
680 
681 	kvm_set_pmu_events(mask, attr);
682 
683 	/* We rely on the hypervisor switch code to enable guest counters */
684 	if (!kvm_pmu_counter_deferred(attr))
685 		armv8pmu_enable_counter(mask);
686 }
687 
armv8pmu_disable_counter(u64 mask)688 static void armv8pmu_disable_counter(u64 mask)
689 {
690 	write_pmcntenclr(mask);
691 	/*
692 	 * Make sure the effects of disabling the counter are visible before we
693 	 * start configuring the event.
694 	 */
695 	isb();
696 }
697 
armv8pmu_disable_event_counter(struct perf_event * event)698 static void armv8pmu_disable_event_counter(struct perf_event *event)
699 {
700 	struct perf_event_attr *attr = &event->attr;
701 	u64 mask = armv8pmu_event_cnten_mask(event);
702 
703 	kvm_clr_pmu_events(mask);
704 
705 	/* We rely on the hypervisor switch code to disable guest counters */
706 	if (!kvm_pmu_counter_deferred(attr))
707 		armv8pmu_disable_counter(mask);
708 }
709 
armv8pmu_enable_intens(u64 mask)710 static void armv8pmu_enable_intens(u64 mask)
711 {
712 	write_pmintenset(mask);
713 }
714 
armv8pmu_enable_event_irq(struct perf_event * event)715 static void armv8pmu_enable_event_irq(struct perf_event *event)
716 {
717 	armv8pmu_enable_intens(BIT(event->hw.idx));
718 }
719 
armv8pmu_disable_intens(u64 mask)720 static void armv8pmu_disable_intens(u64 mask)
721 {
722 	write_pmintenclr(mask);
723 	isb();
724 	/* Clear the overflow flag in case an interrupt is pending. */
725 	write_pmovsclr(mask);
726 	isb();
727 }
728 
armv8pmu_disable_event_irq(struct perf_event * event)729 static void armv8pmu_disable_event_irq(struct perf_event *event)
730 {
731 	armv8pmu_disable_intens(BIT(event->hw.idx));
732 }
733 
armv8pmu_getreset_flags(void)734 static u64 armv8pmu_getreset_flags(void)
735 {
736 	u64 value;
737 
738 	/* Read */
739 	value = read_pmovsclr();
740 
741 	/* Write to clear flags */
742 	value &= ARMV8_PMU_OVERFLOWED_MASK;
743 	write_pmovsclr(value);
744 
745 	return value;
746 }
747 
update_pmuserenr(u64 val)748 static void update_pmuserenr(u64 val)
749 {
750 	lockdep_assert_irqs_disabled();
751 
752 	/*
753 	 * The current PMUSERENR_EL0 value might be the value for the guest.
754 	 * If that's the case, have KVM keep tracking of the register value
755 	 * for the host EL0 so that KVM can restore it before returning to
756 	 * the host EL0. Otherwise, update the register now.
757 	 */
758 	if (kvm_set_pmuserenr(val))
759 		return;
760 
761 	write_pmuserenr(val);
762 }
763 
armv8pmu_disable_user_access(void)764 static void armv8pmu_disable_user_access(void)
765 {
766 	update_pmuserenr(0);
767 }
768 
armv8pmu_enable_user_access(struct arm_pmu * cpu_pmu)769 static void armv8pmu_enable_user_access(struct arm_pmu *cpu_pmu)
770 {
771 	int i;
772 	struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
773 
774 	/* Clear any unused counters to avoid leaking their contents */
775 	for_each_andnot_bit(i, cpu_pmu->cntr_mask, cpuc->used_mask,
776 			    ARMPMU_MAX_HWEVENTS) {
777 		if (i == ARMV8_PMU_CYCLE_IDX)
778 			write_pmccntr(0);
779 		else if (i == ARMV8_PMU_INSTR_IDX)
780 			write_pmicntr(0);
781 		else
782 			armv8pmu_write_evcntr(i, 0);
783 	}
784 
785 	update_pmuserenr(ARMV8_PMU_USERENR_ER | ARMV8_PMU_USERENR_CR);
786 }
787 
armv8pmu_enable_event(struct perf_event * event)788 static void armv8pmu_enable_event(struct perf_event *event)
789 {
790 	/*
791 	 * Enable counter and interrupt, and set the counter to count
792 	 * the event that we're interested in.
793 	 */
794 	armv8pmu_disable_event_counter(event);
795 	armv8pmu_write_event_type(event);
796 	armv8pmu_enable_event_irq(event);
797 	armv8pmu_enable_event_counter(event);
798 }
799 
armv8pmu_disable_event(struct perf_event * event)800 static void armv8pmu_disable_event(struct perf_event *event)
801 {
802 	armv8pmu_disable_event_counter(event);
803 	armv8pmu_disable_event_irq(event);
804 }
805 
armv8pmu_start(struct arm_pmu * cpu_pmu)806 static void armv8pmu_start(struct arm_pmu *cpu_pmu)
807 {
808 	struct perf_event_context *ctx;
809 	int nr_user = 0;
810 
811 	ctx = perf_cpu_task_ctx();
812 	if (ctx)
813 		nr_user = ctx->nr_user;
814 
815 	if (sysctl_perf_user_access && nr_user)
816 		armv8pmu_enable_user_access(cpu_pmu);
817 	else
818 		armv8pmu_disable_user_access();
819 
820 	kvm_vcpu_pmu_resync_el0();
821 
822 	/* Enable all counters */
823 	armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMU_PMCR_E);
824 }
825 
armv8pmu_stop(struct arm_pmu * cpu_pmu)826 static void armv8pmu_stop(struct arm_pmu *cpu_pmu)
827 {
828 	/* Disable all counters */
829 	armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMU_PMCR_E);
830 }
831 
armv8pmu_handle_irq(struct arm_pmu * cpu_pmu)832 static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
833 {
834 	u64 pmovsr;
835 	struct perf_sample_data data;
836 	struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
837 	struct pt_regs *regs;
838 	int idx;
839 
840 	/*
841 	 * Get and reset the IRQ flags
842 	 */
843 	pmovsr = armv8pmu_getreset_flags();
844 
845 	/*
846 	 * Did an overflow occur?
847 	 */
848 	if (!armv8pmu_has_overflowed(pmovsr))
849 		return IRQ_NONE;
850 
851 	/*
852 	 * Handle the counter(s) overflow(s)
853 	 */
854 	regs = get_irq_regs();
855 
856 	/*
857 	 * Stop the PMU while processing the counter overflows
858 	 * to prevent skews in group events.
859 	 */
860 	armv8pmu_stop(cpu_pmu);
861 	for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMPMU_MAX_HWEVENTS) {
862 		struct perf_event *event = cpuc->events[idx];
863 		struct hw_perf_event *hwc;
864 
865 		/* Ignore if we don't have an event. */
866 		if (!event)
867 			continue;
868 
869 		/*
870 		 * We have a single interrupt for all counters. Check that
871 		 * each counter has overflowed before we process it.
872 		 */
873 		if (!armv8pmu_counter_has_overflowed(pmovsr, idx))
874 			continue;
875 
876 		hwc = &event->hw;
877 		armpmu_event_update(event);
878 		perf_sample_data_init(&data, 0, hwc->last_period);
879 		if (!armpmu_event_set_period(event))
880 			continue;
881 
882 		/*
883 		 * Perf event overflow will queue the processing of the event as
884 		 * an irq_work which will be taken care of in the handling of
885 		 * IPI_IRQ_WORK.
886 		 */
887 		if (perf_event_overflow(event, &data, regs))
888 			cpu_pmu->disable(event);
889 	}
890 	armv8pmu_start(cpu_pmu);
891 
892 	return IRQ_HANDLED;
893 }
894 
armv8pmu_get_single_idx(struct pmu_hw_events * cpuc,struct arm_pmu * cpu_pmu)895 static int armv8pmu_get_single_idx(struct pmu_hw_events *cpuc,
896 				    struct arm_pmu *cpu_pmu)
897 {
898 	int idx;
899 
900 	for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS) {
901 		if (!test_and_set_bit(idx, cpuc->used_mask))
902 			return idx;
903 	}
904 	return -EAGAIN;
905 }
906 
armv8pmu_get_chain_idx(struct pmu_hw_events * cpuc,struct arm_pmu * cpu_pmu)907 static int armv8pmu_get_chain_idx(struct pmu_hw_events *cpuc,
908 				   struct arm_pmu *cpu_pmu)
909 {
910 	int idx;
911 
912 	/*
913 	 * Chaining requires two consecutive event counters, where
914 	 * the lower idx must be even.
915 	 */
916 	for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS) {
917 		if (!(idx & 0x1))
918 			continue;
919 		if (!test_and_set_bit(idx, cpuc->used_mask)) {
920 			/* Check if the preceding even counter is available */
921 			if (!test_and_set_bit(idx - 1, cpuc->used_mask))
922 				return idx;
923 			/* Release the Odd counter */
924 			clear_bit(idx, cpuc->used_mask);
925 		}
926 	}
927 	return -EAGAIN;
928 }
929 
armv8pmu_get_event_idx(struct pmu_hw_events * cpuc,struct perf_event * event)930 static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
931 				  struct perf_event *event)
932 {
933 	struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
934 	struct hw_perf_event *hwc = &event->hw;
935 	unsigned long evtype = hwc->config_base & ARMV8_PMU_EVTYPE_EVENT;
936 
937 	/* Always prefer to place a cycle counter into the cycle counter. */
938 	if ((evtype == ARMV8_PMUV3_PERFCTR_CPU_CYCLES) &&
939 	    !armv8pmu_event_get_threshold(&event->attr)) {
940 		if (!test_and_set_bit(ARMV8_PMU_CYCLE_IDX, cpuc->used_mask))
941 			return ARMV8_PMU_CYCLE_IDX;
942 		else if (armv8pmu_event_is_64bit(event) &&
943 			   armv8pmu_event_want_user_access(event) &&
944 			   !armv8pmu_has_long_event(cpu_pmu))
945 				return -EAGAIN;
946 	}
947 
948 	/*
949 	 * Always prefer to place a instruction counter into the instruction counter,
950 	 * but don't expose the instruction counter to userspace access as userspace
951 	 * may not know how to handle it.
952 	 */
953 	if ((evtype == ARMV8_PMUV3_PERFCTR_INST_RETIRED) &&
954 	    !armv8pmu_event_get_threshold(&event->attr) &&
955 	    test_bit(ARMV8_PMU_INSTR_IDX, cpu_pmu->cntr_mask) &&
956 	    !armv8pmu_event_want_user_access(event)) {
957 		if (!test_and_set_bit(ARMV8_PMU_INSTR_IDX, cpuc->used_mask))
958 			return ARMV8_PMU_INSTR_IDX;
959 	}
960 
961 	/*
962 	 * Otherwise use events counters
963 	 */
964 	if (armv8pmu_event_is_chained(event))
965 		return	armv8pmu_get_chain_idx(cpuc, cpu_pmu);
966 	else
967 		return armv8pmu_get_single_idx(cpuc, cpu_pmu);
968 }
969 
armv8pmu_clear_event_idx(struct pmu_hw_events * cpuc,struct perf_event * event)970 static void armv8pmu_clear_event_idx(struct pmu_hw_events *cpuc,
971 				     struct perf_event *event)
972 {
973 	int idx = event->hw.idx;
974 
975 	clear_bit(idx, cpuc->used_mask);
976 	if (armv8pmu_event_is_chained(event))
977 		clear_bit(idx - 1, cpuc->used_mask);
978 }
979 
armv8pmu_user_event_idx(struct perf_event * event)980 static int armv8pmu_user_event_idx(struct perf_event *event)
981 {
982 	if (!sysctl_perf_user_access || !armv8pmu_event_has_user_read(event))
983 		return 0;
984 
985 	return event->hw.idx + 1;
986 }
987 
988 /*
989  * Add an event filter to a given event.
990  */
armv8pmu_set_event_filter(struct hw_perf_event * event,struct perf_event_attr * attr)991 static int armv8pmu_set_event_filter(struct hw_perf_event *event,
992 				     struct perf_event_attr *attr)
993 {
994 	unsigned long config_base = 0;
995 	struct perf_event *perf_event = container_of(attr, struct perf_event,
996 						     attr);
997 	struct arm_pmu *cpu_pmu = to_arm_pmu(perf_event->pmu);
998 	u32 th;
999 
1000 	if (attr->exclude_idle) {
1001 		pr_debug("ARM performance counters do not support mode exclusion\n");
1002 		return -EOPNOTSUPP;
1003 	}
1004 
1005 	/*
1006 	 * If we're running in hyp mode, then we *are* the hypervisor.
1007 	 * Therefore we ignore exclude_hv in this configuration, since
1008 	 * there's no hypervisor to sample anyway. This is consistent
1009 	 * with other architectures (x86 and Power).
1010 	 */
1011 	if (is_kernel_in_hyp_mode()) {
1012 		if (!attr->exclude_kernel && !attr->exclude_host)
1013 			config_base |= ARMV8_PMU_INCLUDE_EL2;
1014 		if (attr->exclude_guest)
1015 			config_base |= ARMV8_PMU_EXCLUDE_EL1;
1016 		if (attr->exclude_host)
1017 			config_base |= ARMV8_PMU_EXCLUDE_EL0;
1018 	} else {
1019 		if (!attr->exclude_hv && !attr->exclude_host)
1020 			config_base |= ARMV8_PMU_INCLUDE_EL2;
1021 	}
1022 
1023 	/*
1024 	 * Filter out !VHE kernels and guest kernels
1025 	 */
1026 	if (attr->exclude_kernel)
1027 		config_base |= ARMV8_PMU_EXCLUDE_EL1;
1028 
1029 	if (attr->exclude_user)
1030 		config_base |= ARMV8_PMU_EXCLUDE_EL0;
1031 
1032 	/*
1033 	 * If FEAT_PMUv3_TH isn't implemented, then THWIDTH (threshold_max) will
1034 	 * be 0 and will also trigger this check, preventing it from being used.
1035 	 */
1036 	th = armv8pmu_event_get_threshold(attr);
1037 	if (th > threshold_max(cpu_pmu)) {
1038 		pr_debug("PMU event threshold exceeds max value\n");
1039 		return -EINVAL;
1040 	}
1041 
1042 	if (th) {
1043 		config_base |= FIELD_PREP(ARMV8_PMU_EVTYPE_TH, th);
1044 		config_base |= FIELD_PREP(ARMV8_PMU_EVTYPE_TC,
1045 					  armv8pmu_event_threshold_control(attr));
1046 	}
1047 
1048 	/*
1049 	 * Install the filter into config_base as this is used to
1050 	 * construct the event type.
1051 	 */
1052 	event->config_base = config_base;
1053 
1054 	return 0;
1055 }
1056 
export_pmu_events(char * str)1057 static int __init export_pmu_events(char *str)
1058 {
1059 	/* Enable exporting of pmu events at early bootup with kernel
1060 	 * arguments.
1061 	 */
1062 	sysctl_export_pmu_events = 1;
1063 	return 0;
1064 }
1065 
1066 early_param("export_pmu_events", export_pmu_events);
1067 
armv8pmu_reset(void * info)1068 static void armv8pmu_reset(void *info)
1069 {
1070 	struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
1071 	u64 pmcr, mask;
1072 
1073 	bitmap_to_arr64(&mask, cpu_pmu->cntr_mask, ARMPMU_MAX_HWEVENTS);
1074 
1075 	/* The counter and interrupt enable registers are unknown at reset. */
1076 	armv8pmu_disable_counter(mask);
1077 	armv8pmu_disable_intens(mask);
1078 
1079 	/* Clear the counters we flip at guest entry/exit */
1080 	kvm_clr_pmu_events(mask);
1081 
1082 	/*
1083 	 * Initialize & Reset PMNC. Request overflow interrupt for
1084 	 * 64 bit cycle counter but cheat in armv8pmu_write_counter().
1085 	 */
1086 	pmcr = ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C | ARMV8_PMU_PMCR_LC;
1087 
1088 	/* Enable long event counter support where available */
1089 	if (armv8pmu_has_long_event(cpu_pmu))
1090 		pmcr |= ARMV8_PMU_PMCR_LP;
1091 
1092 	if (sysctl_export_pmu_events)
1093 		pmcr |= ARMV8_PMU_PMCR_X;
1094 
1095 	armv8pmu_pmcr_write(pmcr);
1096 }
1097 
__armv8_pmuv3_map_event_id(struct arm_pmu * armpmu,struct perf_event * event)1098 static int __armv8_pmuv3_map_event_id(struct arm_pmu *armpmu,
1099 				      struct perf_event *event)
1100 {
1101 	if (event->attr.type == PERF_TYPE_HARDWARE &&
1102 	    event->attr.config == PERF_COUNT_HW_BRANCH_INSTRUCTIONS) {
1103 
1104 		if (test_bit(ARMV8_PMUV3_PERFCTR_BR_RETIRED,
1105 			     armpmu->pmceid_bitmap))
1106 			return ARMV8_PMUV3_PERFCTR_BR_RETIRED;
1107 
1108 		if (test_bit(ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED,
1109 			     armpmu->pmceid_bitmap))
1110 			return ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED;
1111 
1112 		return HW_OP_UNSUPPORTED;
1113 	}
1114 
1115 	return armpmu_map_event(event, &armv8_pmuv3_perf_map,
1116 				&armv8_pmuv3_perf_cache_map,
1117 				ARMV8_PMU_EVTYPE_EVENT);
1118 }
1119 
__armv8_pmuv3_map_event(struct perf_event * event,const unsigned (* extra_event_map)[PERF_COUNT_HW_MAX],const unsigned (* extra_cache_map)[PERF_COUNT_HW_CACHE_MAX][PERF_COUNT_HW_CACHE_OP_MAX][PERF_COUNT_HW_CACHE_RESULT_MAX])1120 static int __armv8_pmuv3_map_event(struct perf_event *event,
1121 				   const unsigned (*extra_event_map)
1122 						  [PERF_COUNT_HW_MAX],
1123 				   const unsigned (*extra_cache_map)
1124 						  [PERF_COUNT_HW_CACHE_MAX]
1125 						  [PERF_COUNT_HW_CACHE_OP_MAX]
1126 						  [PERF_COUNT_HW_CACHE_RESULT_MAX])
1127 {
1128 	int hw_event_id;
1129 	struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
1130 
1131 	hw_event_id = __armv8_pmuv3_map_event_id(armpmu, event);
1132 
1133 	/*
1134 	 * CHAIN events only work when paired with an adjacent counter, and it
1135 	 * never makes sense for a user to open one in isolation, as they'll be
1136 	 * rotated arbitrarily.
1137 	 */
1138 	if (hw_event_id == ARMV8_PMUV3_PERFCTR_CHAIN)
1139 		return -EINVAL;
1140 
1141 	if (armv8pmu_event_is_64bit(event))
1142 		event->hw.flags |= ARMPMU_EVT_64BIT;
1143 
1144 	/*
1145 	 * User events must be allocated into a single counter, and so
1146 	 * must not be chained.
1147 	 *
1148 	 * Most 64-bit events require long counter support, but 64-bit
1149 	 * CPU_CYCLES events can be placed into the dedicated cycle
1150 	 * counter when this is free.
1151 	 */
1152 	if (armv8pmu_event_want_user_access(event)) {
1153 		if (!(event->attach_state & PERF_ATTACH_TASK))
1154 			return -EINVAL;
1155 		if (armv8pmu_event_is_64bit(event) &&
1156 		    (hw_event_id != ARMV8_PMUV3_PERFCTR_CPU_CYCLES) &&
1157 		    !armv8pmu_has_long_event(armpmu))
1158 			return -EOPNOTSUPP;
1159 
1160 		event->hw.flags |= PERF_EVENT_FLAG_USER_READ_CNT;
1161 	}
1162 
1163 	/* Only expose micro/arch events supported by this PMU */
1164 	if ((hw_event_id > 0) && (hw_event_id < ARMV8_PMUV3_MAX_COMMON_EVENTS)
1165 	    && test_bit(hw_event_id, armpmu->pmceid_bitmap)) {
1166 		return hw_event_id;
1167 	}
1168 
1169 	return armpmu_map_event(event, extra_event_map, extra_cache_map,
1170 				ARMV8_PMU_EVTYPE_EVENT);
1171 }
1172 
armv8_pmuv3_map_event(struct perf_event * event)1173 static int armv8_pmuv3_map_event(struct perf_event *event)
1174 {
1175 	return __armv8_pmuv3_map_event(event, NULL, NULL);
1176 }
1177 
armv8_a53_map_event(struct perf_event * event)1178 static int armv8_a53_map_event(struct perf_event *event)
1179 {
1180 	return __armv8_pmuv3_map_event(event, NULL, &armv8_a53_perf_cache_map);
1181 }
1182 
armv8_a57_map_event(struct perf_event * event)1183 static int armv8_a57_map_event(struct perf_event *event)
1184 {
1185 	return __armv8_pmuv3_map_event(event, NULL, &armv8_a57_perf_cache_map);
1186 }
1187 
armv8_a73_map_event(struct perf_event * event)1188 static int armv8_a73_map_event(struct perf_event *event)
1189 {
1190 	return __armv8_pmuv3_map_event(event, NULL, &armv8_a73_perf_cache_map);
1191 }
1192 
armv8_thunder_map_event(struct perf_event * event)1193 static int armv8_thunder_map_event(struct perf_event *event)
1194 {
1195 	return __armv8_pmuv3_map_event(event, NULL,
1196 				       &armv8_thunder_perf_cache_map);
1197 }
1198 
armv8_vulcan_map_event(struct perf_event * event)1199 static int armv8_vulcan_map_event(struct perf_event *event)
1200 {
1201 	return __armv8_pmuv3_map_event(event, NULL,
1202 				       &armv8_vulcan_perf_cache_map);
1203 }
1204 
1205 struct armv8pmu_probe_info {
1206 	struct arm_pmu *pmu;
1207 	bool present;
1208 };
1209 
__armv8pmu_probe_pmu(void * info)1210 static void __armv8pmu_probe_pmu(void *info)
1211 {
1212 	struct armv8pmu_probe_info *probe = info;
1213 	struct arm_pmu *cpu_pmu = probe->pmu;
1214 	u64 pmceid_raw[2];
1215 	u32 pmceid[2];
1216 	int pmuver;
1217 
1218 	pmuver = read_pmuver();
1219 	if (!pmuv3_implemented(pmuver))
1220 		return;
1221 
1222 	cpu_pmu->pmuver = pmuver;
1223 	probe->present = true;
1224 
1225 	/* Read the nb of CNTx counters supported from PMNC */
1226 	bitmap_set(cpu_pmu->cntr_mask,
1227 		   0, FIELD_GET(ARMV8_PMU_PMCR_N, armv8pmu_pmcr_read()));
1228 
1229 	/* Add the CPU cycles counter */
1230 	set_bit(ARMV8_PMU_CYCLE_IDX, cpu_pmu->cntr_mask);
1231 
1232 	/* Add the CPU instructions counter */
1233 	if (pmuv3_has_icntr())
1234 		set_bit(ARMV8_PMU_INSTR_IDX, cpu_pmu->cntr_mask);
1235 
1236 	pmceid[0] = pmceid_raw[0] = read_pmceid0();
1237 	pmceid[1] = pmceid_raw[1] = read_pmceid1();
1238 
1239 	bitmap_from_arr32(cpu_pmu->pmceid_bitmap,
1240 			     pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
1241 
1242 	pmceid[0] = pmceid_raw[0] >> 32;
1243 	pmceid[1] = pmceid_raw[1] >> 32;
1244 
1245 	bitmap_from_arr32(cpu_pmu->pmceid_ext_bitmap,
1246 			     pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
1247 
1248 	/* store PMMIR register for sysfs */
1249 	if (is_pmuv3p4(pmuver))
1250 		cpu_pmu->reg_pmmir = read_pmmir();
1251 	else
1252 		cpu_pmu->reg_pmmir = 0;
1253 }
1254 
armv8pmu_probe_pmu(struct arm_pmu * cpu_pmu)1255 static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
1256 {
1257 	struct armv8pmu_probe_info probe = {
1258 		.pmu = cpu_pmu,
1259 		.present = false,
1260 	};
1261 	int ret;
1262 
1263 	ret = smp_call_function_any(&cpu_pmu->supported_cpus,
1264 				    __armv8pmu_probe_pmu,
1265 				    &probe, 1);
1266 	if (ret)
1267 		return ret;
1268 
1269 	return probe.present ? 0 : -ENODEV;
1270 }
1271 
armv8pmu_disable_user_access_ipi(void * unused)1272 static void armv8pmu_disable_user_access_ipi(void *unused)
1273 {
1274 	armv8pmu_disable_user_access();
1275 }
1276 
armv8pmu_proc_user_access_handler(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1277 static int armv8pmu_proc_user_access_handler(const struct ctl_table *table, int write,
1278 		void *buffer, size_t *lenp, loff_t *ppos)
1279 {
1280 	int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
1281 	if (ret || !write || sysctl_perf_user_access)
1282 		return ret;
1283 
1284 	on_each_cpu(armv8pmu_disable_user_access_ipi, NULL, 1);
1285 	return 0;
1286 }
1287 
1288 static struct ctl_table armv8_pmu_sysctl_table[] = {
1289 	{
1290 		.procname       = "perf_user_access",
1291 		.data		= &sysctl_perf_user_access,
1292 		.maxlen		= sizeof(unsigned int),
1293 		.mode           = 0644,
1294 		.proc_handler	= armv8pmu_proc_user_access_handler,
1295 		.extra1		= SYSCTL_ZERO,
1296 		.extra2		= SYSCTL_ONE,
1297 	},
1298 	{
1299 		.procname       = "export_pmu_events",
1300 		.data           = &sysctl_export_pmu_events,
1301 		.maxlen         = sizeof(unsigned int),
1302 		.mode           = 0644,
1303 		.proc_handler   = proc_dointvec_minmax,
1304 		.extra1         = SYSCTL_ZERO,
1305 		.extra2         = SYSCTL_ONE,
1306 	},
1307 };
1308 
armv8_pmu_register_sysctl_table(void)1309 static void armv8_pmu_register_sysctl_table(void)
1310 {
1311 	static u32 tbl_registered = 0;
1312 
1313 	if (!cmpxchg_relaxed(&tbl_registered, 0, 1))
1314 		register_sysctl("kernel", armv8_pmu_sysctl_table);
1315 }
1316 
armv8_pmu_init(struct arm_pmu * cpu_pmu,char * name,int (* map_event)(struct perf_event * event))1317 static int armv8_pmu_init(struct arm_pmu *cpu_pmu, char *name,
1318 			  int (*map_event)(struct perf_event *event))
1319 {
1320 	int ret = armv8pmu_probe_pmu(cpu_pmu);
1321 	if (ret)
1322 		return ret;
1323 
1324 	cpu_pmu->handle_irq		= armv8pmu_handle_irq;
1325 	cpu_pmu->enable			= armv8pmu_enable_event;
1326 	cpu_pmu->disable		= armv8pmu_disable_event;
1327 	cpu_pmu->read_counter		= armv8pmu_read_counter;
1328 	cpu_pmu->write_counter		= armv8pmu_write_counter;
1329 	cpu_pmu->get_event_idx		= armv8pmu_get_event_idx;
1330 	cpu_pmu->clear_event_idx	= armv8pmu_clear_event_idx;
1331 	cpu_pmu->start			= armv8pmu_start;
1332 	cpu_pmu->stop			= armv8pmu_stop;
1333 	cpu_pmu->reset			= armv8pmu_reset;
1334 	cpu_pmu->set_event_filter	= armv8pmu_set_event_filter;
1335 
1336 	cpu_pmu->pmu.event_idx		= armv8pmu_user_event_idx;
1337 
1338 	cpu_pmu->name			= name;
1339 	cpu_pmu->map_event		= map_event;
1340 	cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] = &armv8_pmuv3_events_attr_group;
1341 	cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] = &armv8_pmuv3_format_attr_group;
1342 	cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_CAPS] = &armv8_pmuv3_caps_attr_group;
1343 	armv8_pmu_register_sysctl_table();
1344 	return 0;
1345 }
1346 
1347 #define PMUV3_INIT_SIMPLE(name)						\
1348 static int name##_pmu_init(struct arm_pmu *cpu_pmu)			\
1349 {									\
1350 	return armv8_pmu_init(cpu_pmu, #name, armv8_pmuv3_map_event);	\
1351 }
1352 
1353 #define PMUV3_INIT_MAP_EVENT(name, map_event)				\
1354 static int name##_pmu_init(struct arm_pmu *cpu_pmu)			\
1355 {									\
1356 	return armv8_pmu_init(cpu_pmu, #name, map_event);		\
1357 }
1358 
1359 PMUV3_INIT_SIMPLE(armv8_pmuv3)
1360 
1361 PMUV3_INIT_SIMPLE(armv8_cortex_a34)
1362 PMUV3_INIT_SIMPLE(armv8_cortex_a55)
1363 PMUV3_INIT_SIMPLE(armv8_cortex_a65)
1364 PMUV3_INIT_SIMPLE(armv8_cortex_a75)
1365 PMUV3_INIT_SIMPLE(armv8_cortex_a76)
1366 PMUV3_INIT_SIMPLE(armv8_cortex_a77)
1367 PMUV3_INIT_SIMPLE(armv8_cortex_a78)
1368 PMUV3_INIT_SIMPLE(armv9_cortex_a510)
1369 PMUV3_INIT_SIMPLE(armv9_cortex_a520)
1370 PMUV3_INIT_SIMPLE(armv9_cortex_a710)
1371 PMUV3_INIT_SIMPLE(armv9_cortex_a715)
1372 PMUV3_INIT_SIMPLE(armv9_cortex_a720)
1373 PMUV3_INIT_SIMPLE(armv9_cortex_a725)
1374 PMUV3_INIT_SIMPLE(armv8_cortex_x1)
1375 PMUV3_INIT_SIMPLE(armv9_cortex_x2)
1376 PMUV3_INIT_SIMPLE(armv9_cortex_x3)
1377 PMUV3_INIT_SIMPLE(armv9_cortex_x4)
1378 PMUV3_INIT_SIMPLE(armv9_cortex_x925)
1379 PMUV3_INIT_SIMPLE(armv8_neoverse_e1)
1380 PMUV3_INIT_SIMPLE(armv8_neoverse_n1)
1381 PMUV3_INIT_SIMPLE(armv9_neoverse_n2)
1382 PMUV3_INIT_SIMPLE(armv9_neoverse_n3)
1383 PMUV3_INIT_SIMPLE(armv8_neoverse_v1)
1384 PMUV3_INIT_SIMPLE(armv8_neoverse_v2)
1385 PMUV3_INIT_SIMPLE(armv8_neoverse_v3)
1386 PMUV3_INIT_SIMPLE(armv8_neoverse_v3ae)
1387 
1388 PMUV3_INIT_SIMPLE(armv8_nvidia_carmel)
1389 PMUV3_INIT_SIMPLE(armv8_nvidia_denver)
1390 
1391 PMUV3_INIT_MAP_EVENT(armv8_cortex_a35, armv8_a53_map_event)
1392 PMUV3_INIT_MAP_EVENT(armv8_cortex_a53, armv8_a53_map_event)
1393 PMUV3_INIT_MAP_EVENT(armv8_cortex_a57, armv8_a57_map_event)
1394 PMUV3_INIT_MAP_EVENT(armv8_cortex_a72, armv8_a57_map_event)
1395 PMUV3_INIT_MAP_EVENT(armv8_cortex_a73, armv8_a73_map_event)
1396 PMUV3_INIT_MAP_EVENT(armv8_cavium_thunder, armv8_thunder_map_event)
1397 PMUV3_INIT_MAP_EVENT(armv8_brcm_vulcan, armv8_vulcan_map_event)
1398 
1399 static const struct of_device_id armv8_pmu_of_device_ids[] = {
1400 	{.compatible = "arm,armv8-pmuv3",	.data = armv8_pmuv3_pmu_init},
1401 	{.compatible = "arm,cortex-a34-pmu",	.data = armv8_cortex_a34_pmu_init},
1402 	{.compatible = "arm,cortex-a35-pmu",	.data = armv8_cortex_a35_pmu_init},
1403 	{.compatible = "arm,cortex-a53-pmu",	.data = armv8_cortex_a53_pmu_init},
1404 	{.compatible = "arm,cortex-a55-pmu",	.data = armv8_cortex_a55_pmu_init},
1405 	{.compatible = "arm,cortex-a57-pmu",	.data = armv8_cortex_a57_pmu_init},
1406 	{.compatible = "arm,cortex-a65-pmu",	.data = armv8_cortex_a65_pmu_init},
1407 	{.compatible = "arm,cortex-a72-pmu",	.data = armv8_cortex_a72_pmu_init},
1408 	{.compatible = "arm,cortex-a73-pmu",	.data = armv8_cortex_a73_pmu_init},
1409 	{.compatible = "arm,cortex-a75-pmu",	.data = armv8_cortex_a75_pmu_init},
1410 	{.compatible = "arm,cortex-a76-pmu",	.data = armv8_cortex_a76_pmu_init},
1411 	{.compatible = "arm,cortex-a77-pmu",	.data = armv8_cortex_a77_pmu_init},
1412 	{.compatible = "arm,cortex-a78-pmu",	.data = armv8_cortex_a78_pmu_init},
1413 	{.compatible = "arm,cortex-a510-pmu",	.data = armv9_cortex_a510_pmu_init},
1414 	{.compatible = "arm,cortex-a520-pmu",	.data = armv9_cortex_a520_pmu_init},
1415 	{.compatible = "arm,cortex-a710-pmu",	.data = armv9_cortex_a710_pmu_init},
1416 	{.compatible = "arm,cortex-a715-pmu",	.data = armv9_cortex_a715_pmu_init},
1417 	{.compatible = "arm,cortex-a720-pmu",	.data = armv9_cortex_a720_pmu_init},
1418 	{.compatible = "arm,cortex-a725-pmu",	.data = armv9_cortex_a725_pmu_init},
1419 	{.compatible = "arm,cortex-x1-pmu",	.data = armv8_cortex_x1_pmu_init},
1420 	{.compatible = "arm,cortex-x2-pmu",	.data = armv9_cortex_x2_pmu_init},
1421 	{.compatible = "arm,cortex-x3-pmu",	.data = armv9_cortex_x3_pmu_init},
1422 	{.compatible = "arm,cortex-x4-pmu",	.data = armv9_cortex_x4_pmu_init},
1423 	{.compatible = "arm,cortex-x925-pmu",	.data = armv9_cortex_x925_pmu_init},
1424 	{.compatible = "arm,neoverse-e1-pmu",	.data = armv8_neoverse_e1_pmu_init},
1425 	{.compatible = "arm,neoverse-n1-pmu",	.data = armv8_neoverse_n1_pmu_init},
1426 	{.compatible = "arm,neoverse-n2-pmu",	.data = armv9_neoverse_n2_pmu_init},
1427 	{.compatible = "arm,neoverse-n3-pmu",	.data = armv9_neoverse_n3_pmu_init},
1428 	{.compatible = "arm,neoverse-v1-pmu",	.data = armv8_neoverse_v1_pmu_init},
1429 	{.compatible = "arm,neoverse-v2-pmu",	.data = armv8_neoverse_v2_pmu_init},
1430 	{.compatible = "arm,neoverse-v3-pmu",	.data = armv8_neoverse_v3_pmu_init},
1431 	{.compatible = "arm,neoverse-v3ae-pmu",	.data = armv8_neoverse_v3ae_pmu_init},
1432 	{.compatible = "cavium,thunder-pmu",	.data = armv8_cavium_thunder_pmu_init},
1433 	{.compatible = "brcm,vulcan-pmu",	.data = armv8_brcm_vulcan_pmu_init},
1434 	{.compatible = "nvidia,carmel-pmu",	.data = armv8_nvidia_carmel_pmu_init},
1435 	{.compatible = "nvidia,denver-pmu",	.data = armv8_nvidia_denver_pmu_init},
1436 	{},
1437 };
1438 
armv8_pmu_device_probe(struct platform_device * pdev)1439 static int armv8_pmu_device_probe(struct platform_device *pdev)
1440 {
1441 	return arm_pmu_device_probe(pdev, armv8_pmu_of_device_ids, NULL);
1442 }
1443 
1444 static struct platform_driver armv8_pmu_driver = {
1445 	.driver		= {
1446 		.name	= ARMV8_PMU_PDEV_NAME,
1447 		.of_match_table = armv8_pmu_of_device_ids,
1448 		.suppress_bind_attrs = true,
1449 	},
1450 	.probe		= armv8_pmu_device_probe,
1451 };
1452 
armv8_pmu_driver_init(void)1453 static int __init armv8_pmu_driver_init(void)
1454 {
1455 	int ret;
1456 
1457 	if (acpi_disabled)
1458 		ret = platform_driver_register(&armv8_pmu_driver);
1459 	else
1460 		ret = arm_pmu_acpi_probe(armv8_pmuv3_pmu_init);
1461 
1462 	if (!ret)
1463 		lockup_detector_retry_init();
1464 
1465 	return ret;
1466 }
device_initcall(armv8_pmu_driver_init)1467 device_initcall(armv8_pmu_driver_init)
1468 
1469 void arch_perf_update_userpage(struct perf_event *event,
1470 			       struct perf_event_mmap_page *userpg, u64 now)
1471 {
1472 	struct clock_read_data *rd;
1473 	unsigned int seq;
1474 	u64 ns;
1475 
1476 	userpg->cap_user_time = 0;
1477 	userpg->cap_user_time_zero = 0;
1478 	userpg->cap_user_time_short = 0;
1479 	userpg->cap_user_rdpmc = armv8pmu_event_has_user_read(event);
1480 
1481 	if (userpg->cap_user_rdpmc) {
1482 		if (event->hw.flags & ARMPMU_EVT_64BIT)
1483 			userpg->pmc_width = 64;
1484 		else
1485 			userpg->pmc_width = 32;
1486 	}
1487 
1488 	do {
1489 		rd = sched_clock_read_begin(&seq);
1490 
1491 		if (rd->read_sched_clock != arch_timer_read_counter)
1492 			return;
1493 
1494 		userpg->time_mult = rd->mult;
1495 		userpg->time_shift = rd->shift;
1496 		userpg->time_zero = rd->epoch_ns;
1497 		userpg->time_cycles = rd->epoch_cyc;
1498 		userpg->time_mask = rd->sched_clock_mask;
1499 
1500 		/*
1501 		 * Subtract the cycle base, such that software that
1502 		 * doesn't know about cap_user_time_short still 'works'
1503 		 * assuming no wraps.
1504 		 */
1505 		ns = mul_u64_u32_shr(rd->epoch_cyc, rd->mult, rd->shift);
1506 		userpg->time_zero -= ns;
1507 
1508 	} while (sched_clock_read_retry(seq));
1509 
1510 	userpg->time_offset = userpg->time_zero - now;
1511 
1512 	/*
1513 	 * time_shift is not expected to be greater than 31 due to
1514 	 * the original published conversion algorithm shifting a
1515 	 * 32-bit value (now specifies a 64-bit value) - refer
1516 	 * perf_event_mmap_page documentation in perf_event.h.
1517 	 */
1518 	if (userpg->time_shift == 32) {
1519 		userpg->time_shift = 31;
1520 		userpg->time_mult >>= 1;
1521 	}
1522 
1523 	/*
1524 	 * Internal timekeeping for enabled/running/stopped times
1525 	 * is always computed with the sched_clock.
1526 	 */
1527 	userpg->cap_user_time = 1;
1528 	userpg->cap_user_time_zero = 1;
1529 	userpg->cap_user_time_short = 1;
1530 }
1531