• 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/sysreg.h>
14 #include <asm/virt.h>
15 
16 #include <clocksource/arm_arch_timer.h>
17 
18 #include <linux/acpi.h>
19 #include <linux/clocksource.h>
20 #include <linux/kvm_host.h>
21 #include <linux/of.h>
22 #include <linux/perf/arm_pmu.h>
23 #include <linux/platform_device.h>
24 #include <linux/sched_clock.h>
25 #include <linux/smp.h>
26 
27 /* ARMv8 Cortex-A53 specific event types. */
28 #define ARMV8_A53_PERFCTR_PREF_LINEFILL				0xC2
29 
30 /* ARMv8 Cavium ThunderX specific event types. */
31 #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
36 
37 /*
38  * ARMv8 Architectural defined events, not all of these may
39  * be supported on any given implementation. Unsupported events will
40  * be disabled at run-time based on the PMCEID registers.
41  */
42 static const unsigned armv8_pmuv3_perf_map[PERF_COUNT_HW_MAX] = {
43 	PERF_MAP_ALL_UNSUPPORTED,
44 	[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,
48 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS]	= ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED,
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(ldst_align_lat, ARMV8_PMUV3_PERFCTR_LDST_ALIGN_LAT),
250 	ARMV8_EVENT_ATTR(ld_align_lat, ARMV8_PMUV3_PERFCTR_LD_ALIGN_LAT),
251 	ARMV8_EVENT_ATTR(st_align_lat, ARMV8_PMUV3_PERFCTR_ST_ALIGN_LAT),
252 	ARMV8_EVENT_ATTR(mem_access_checked, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED),
253 	ARMV8_EVENT_ATTR(mem_access_checked_rd, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED_RD),
254 	ARMV8_EVENT_ATTR(mem_access_checked_wr, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED_WR),
255 	NULL,
256 };
257 
258 static umode_t
armv8pmu_event_attr_is_visible(struct kobject * kobj,struct attribute * attr,int unused)259 armv8pmu_event_attr_is_visible(struct kobject *kobj,
260 			       struct attribute *attr, int unused)
261 {
262 	struct device *dev = kobj_to_dev(kobj);
263 	struct pmu *pmu = dev_get_drvdata(dev);
264 	struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
265 	struct perf_pmu_events_attr *pmu_attr;
266 
267 	pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
268 
269 	if (pmu_attr->id < ARMV8_PMUV3_MAX_COMMON_EVENTS &&
270 	    test_bit(pmu_attr->id, cpu_pmu->pmceid_bitmap))
271 		return attr->mode;
272 
273 	if (pmu_attr->id >= ARMV8_PMUV3_EXT_COMMON_EVENT_BASE) {
274 		u64 id = pmu_attr->id - ARMV8_PMUV3_EXT_COMMON_EVENT_BASE;
275 
276 		if (id < ARMV8_PMUV3_MAX_COMMON_EVENTS &&
277 		    test_bit(id, cpu_pmu->pmceid_ext_bitmap))
278 			return attr->mode;
279 	}
280 
281 	return 0;
282 }
283 
284 static const struct attribute_group armv8_pmuv3_events_attr_group = {
285 	.name = "events",
286 	.attrs = armv8_pmuv3_event_attrs,
287 	.is_visible = armv8pmu_event_attr_is_visible,
288 };
289 
290 PMU_FORMAT_ATTR(event, "config:0-15");
291 PMU_FORMAT_ATTR(long, "config1:0");
292 
293 static int sysctl_perf_user_access __read_mostly;
294 static int sysctl_export_pmu_events __read_mostly;
295 
armv8pmu_event_is_64bit(struct perf_event * event)296 static inline bool armv8pmu_event_is_64bit(struct perf_event *event)
297 {
298 	return event->attr.config1 & 0x1;
299 }
300 
301 static struct attribute *armv8_pmuv3_format_attrs[] = {
302 	&format_attr_event.attr,
303 	&format_attr_long.attr,
304 	NULL,
305 };
306 
307 static const struct attribute_group armv8_pmuv3_format_attr_group = {
308 	.name = "format",
309 	.attrs = armv8_pmuv3_format_attrs,
310 };
311 
slots_show(struct device * dev,struct device_attribute * attr,char * page)312 static ssize_t slots_show(struct device *dev, struct device_attribute *attr,
313 			  char *page)
314 {
315 	struct pmu *pmu = dev_get_drvdata(dev);
316 	struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
317 	u32 slots = cpu_pmu->reg_pmmir & ARMV8_PMU_SLOTS_MASK;
318 
319 	return sysfs_emit(page, "0x%08x\n", slots);
320 }
321 
322 static DEVICE_ATTR_RO(slots);
323 
bus_slots_show(struct device * dev,struct device_attribute * attr,char * page)324 static ssize_t bus_slots_show(struct device *dev, struct device_attribute *attr,
325 			      char *page)
326 {
327 	struct pmu *pmu = dev_get_drvdata(dev);
328 	struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
329 	u32 bus_slots = (cpu_pmu->reg_pmmir >> ARMV8_PMU_BUS_SLOTS_SHIFT)
330 			& ARMV8_PMU_BUS_SLOTS_MASK;
331 
332 	return sysfs_emit(page, "0x%08x\n", bus_slots);
333 }
334 
335 static DEVICE_ATTR_RO(bus_slots);
336 
bus_width_show(struct device * dev,struct device_attribute * attr,char * page)337 static ssize_t bus_width_show(struct device *dev, struct device_attribute *attr,
338 			      char *page)
339 {
340 	struct pmu *pmu = dev_get_drvdata(dev);
341 	struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
342 	u32 bus_width = (cpu_pmu->reg_pmmir >> ARMV8_PMU_BUS_WIDTH_SHIFT)
343 			& ARMV8_PMU_BUS_WIDTH_MASK;
344 	u32 val = 0;
345 
346 	/* Encoded as Log2(number of bytes), plus one */
347 	if (bus_width > 2 && bus_width < 13)
348 		val = 1 << (bus_width - 1);
349 
350 	return sysfs_emit(page, "0x%08x\n", val);
351 }
352 
353 static DEVICE_ATTR_RO(bus_width);
354 
355 static struct attribute *armv8_pmuv3_caps_attrs[] = {
356 	&dev_attr_slots.attr,
357 	&dev_attr_bus_slots.attr,
358 	&dev_attr_bus_width.attr,
359 	NULL,
360 };
361 
362 static const struct attribute_group armv8_pmuv3_caps_attr_group = {
363 	.name = "caps",
364 	.attrs = armv8_pmuv3_caps_attrs,
365 };
366 
367 /*
368  * Perf Events' indices
369  */
370 #define	ARMV8_IDX_CYCLE_COUNTER	0
371 #define	ARMV8_IDX_COUNTER0	1
372 
373 
374 /*
375  * We unconditionally enable ARMv8.5-PMU long event counter support
376  * (64-bit events) where supported. Indicate if this arm_pmu has long
377  * event counter support.
378  */
armv8pmu_has_long_event(struct arm_pmu * cpu_pmu)379 static bool armv8pmu_has_long_event(struct arm_pmu *cpu_pmu)
380 {
381 	return (cpu_pmu->pmuver >= ID_AA64DFR0_EL1_PMUVer_V3P5);
382 }
383 
384 /*
385  * We must chain two programmable counters for 64 bit events,
386  * except when we have allocated the 64bit cycle counter (for CPU
387  * cycles event). This must be called only when the event has
388  * a counter allocated.
389  */
armv8pmu_event_is_chained(struct perf_event * event)390 static inline bool armv8pmu_event_is_chained(struct perf_event *event)
391 {
392 	int idx = event->hw.idx;
393 	struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
394 
395 	return !WARN_ON(idx < 0) &&
396 	       armv8pmu_event_is_64bit(event) &&
397 	       !armv8pmu_has_long_event(cpu_pmu) &&
398 	       (idx != ARMV8_IDX_CYCLE_COUNTER);
399 }
400 
401 /*
402  * ARMv8 low level PMU access
403  */
404 
405 /*
406  * Perf Event to low level counters mapping
407  */
408 #define	ARMV8_IDX_TO_COUNTER(x)	\
409 	(((x) - ARMV8_IDX_COUNTER0) & ARMV8_PMU_COUNTER_MASK)
410 
411 /*
412  * This code is really good
413  */
414 
415 #define PMEVN_CASE(n, case_macro) \
416 	case n: case_macro(n); break
417 
418 #define PMEVN_SWITCH(x, case_macro)				\
419 	do {							\
420 		switch (x) {					\
421 		PMEVN_CASE(0,  case_macro);			\
422 		PMEVN_CASE(1,  case_macro);			\
423 		PMEVN_CASE(2,  case_macro);			\
424 		PMEVN_CASE(3,  case_macro);			\
425 		PMEVN_CASE(4,  case_macro);			\
426 		PMEVN_CASE(5,  case_macro);			\
427 		PMEVN_CASE(6,  case_macro);			\
428 		PMEVN_CASE(7,  case_macro);			\
429 		PMEVN_CASE(8,  case_macro);			\
430 		PMEVN_CASE(9,  case_macro);			\
431 		PMEVN_CASE(10, case_macro);			\
432 		PMEVN_CASE(11, case_macro);			\
433 		PMEVN_CASE(12, case_macro);			\
434 		PMEVN_CASE(13, case_macro);			\
435 		PMEVN_CASE(14, case_macro);			\
436 		PMEVN_CASE(15, case_macro);			\
437 		PMEVN_CASE(16, case_macro);			\
438 		PMEVN_CASE(17, case_macro);			\
439 		PMEVN_CASE(18, case_macro);			\
440 		PMEVN_CASE(19, case_macro);			\
441 		PMEVN_CASE(20, case_macro);			\
442 		PMEVN_CASE(21, case_macro);			\
443 		PMEVN_CASE(22, case_macro);			\
444 		PMEVN_CASE(23, case_macro);			\
445 		PMEVN_CASE(24, case_macro);			\
446 		PMEVN_CASE(25, case_macro);			\
447 		PMEVN_CASE(26, case_macro);			\
448 		PMEVN_CASE(27, case_macro);			\
449 		PMEVN_CASE(28, case_macro);			\
450 		PMEVN_CASE(29, case_macro);			\
451 		PMEVN_CASE(30, case_macro);			\
452 		default: WARN(1, "Invalid PMEV* index\n");	\
453 		}						\
454 	} while (0)
455 
456 #define RETURN_READ_PMEVCNTRN(n) \
457 	return read_sysreg(pmevcntr##n##_el0)
read_pmevcntrn(int n)458 static unsigned long read_pmevcntrn(int n)
459 {
460 	PMEVN_SWITCH(n, RETURN_READ_PMEVCNTRN);
461 	return 0;
462 }
463 
464 #define WRITE_PMEVCNTRN(n) \
465 	write_sysreg(val, pmevcntr##n##_el0)
write_pmevcntrn(int n,unsigned long val)466 static void write_pmevcntrn(int n, unsigned long val)
467 {
468 	PMEVN_SWITCH(n, WRITE_PMEVCNTRN);
469 }
470 
471 #define WRITE_PMEVTYPERN(n) \
472 	write_sysreg(val, pmevtyper##n##_el0)
write_pmevtypern(int n,unsigned long val)473 static void write_pmevtypern(int n, unsigned long val)
474 {
475 	PMEVN_SWITCH(n, WRITE_PMEVTYPERN);
476 }
477 
armv8pmu_pmcr_read(void)478 static inline u32 armv8pmu_pmcr_read(void)
479 {
480 	return read_sysreg(pmcr_el0);
481 }
482 
armv8pmu_pmcr_write(u32 val)483 static inline void armv8pmu_pmcr_write(u32 val)
484 {
485 	val &= ARMV8_PMU_PMCR_MASK;
486 	isb();
487 	write_sysreg(val, pmcr_el0);
488 }
489 
armv8pmu_has_overflowed(u32 pmovsr)490 static inline int armv8pmu_has_overflowed(u32 pmovsr)
491 {
492 	return pmovsr & ARMV8_PMU_OVERFLOWED_MASK;
493 }
494 
armv8pmu_counter_has_overflowed(u32 pmnc,int idx)495 static inline int armv8pmu_counter_has_overflowed(u32 pmnc, int idx)
496 {
497 	return pmnc & BIT(ARMV8_IDX_TO_COUNTER(idx));
498 }
499 
armv8pmu_read_evcntr(int idx)500 static inline u64 armv8pmu_read_evcntr(int idx)
501 {
502 	u32 counter = ARMV8_IDX_TO_COUNTER(idx);
503 
504 	return read_pmevcntrn(counter);
505 }
506 
armv8pmu_read_hw_counter(struct perf_event * event)507 static inline u64 armv8pmu_read_hw_counter(struct perf_event *event)
508 {
509 	int idx = event->hw.idx;
510 	u64 val = armv8pmu_read_evcntr(idx);
511 
512 	if (armv8pmu_event_is_chained(event))
513 		val = (val << 32) | armv8pmu_read_evcntr(idx - 1);
514 	return val;
515 }
516 
517 /*
518  * The cycle counter is always a 64-bit counter. When ARMV8_PMU_PMCR_LP
519  * is set the event counters also become 64-bit counters. Unless the
520  * user has requested a long counter (attr.config1) then we want to
521  * interrupt upon 32-bit overflow - we achieve this by applying a bias.
522  */
armv8pmu_event_needs_bias(struct perf_event * event)523 static bool armv8pmu_event_needs_bias(struct perf_event *event)
524 {
525 	struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
526 	struct hw_perf_event *hwc = &event->hw;
527 	int idx = hwc->idx;
528 
529 	if (armv8pmu_event_is_64bit(event))
530 		return false;
531 
532 	if (armv8pmu_has_long_event(cpu_pmu) ||
533 	    idx == ARMV8_IDX_CYCLE_COUNTER)
534 		return true;
535 
536 	return false;
537 }
538 
armv8pmu_bias_long_counter(struct perf_event * event,u64 value)539 static u64 armv8pmu_bias_long_counter(struct perf_event *event, u64 value)
540 {
541 	if (armv8pmu_event_needs_bias(event))
542 		value |= GENMASK(63, 32);
543 
544 	return value;
545 }
546 
armv8pmu_unbias_long_counter(struct perf_event * event,u64 value)547 static u64 armv8pmu_unbias_long_counter(struct perf_event *event, u64 value)
548 {
549 	if (armv8pmu_event_needs_bias(event))
550 		value &= ~GENMASK(63, 32);
551 
552 	return value;
553 }
554 
armv8pmu_read_counter(struct perf_event * event)555 static u64 armv8pmu_read_counter(struct perf_event *event)
556 {
557 	struct hw_perf_event *hwc = &event->hw;
558 	int idx = hwc->idx;
559 	u64 value;
560 
561 	if (idx == ARMV8_IDX_CYCLE_COUNTER)
562 		value = read_sysreg(pmccntr_el0);
563 	else
564 		value = armv8pmu_read_hw_counter(event);
565 
566 	return  armv8pmu_unbias_long_counter(event, value);
567 }
568 
armv8pmu_write_evcntr(int idx,u64 value)569 static inline void armv8pmu_write_evcntr(int idx, u64 value)
570 {
571 	u32 counter = ARMV8_IDX_TO_COUNTER(idx);
572 
573 	write_pmevcntrn(counter, value);
574 }
575 
armv8pmu_write_hw_counter(struct perf_event * event,u64 value)576 static inline void armv8pmu_write_hw_counter(struct perf_event *event,
577 					     u64 value)
578 {
579 	int idx = event->hw.idx;
580 
581 	if (armv8pmu_event_is_chained(event)) {
582 		armv8pmu_write_evcntr(idx, upper_32_bits(value));
583 		armv8pmu_write_evcntr(idx - 1, lower_32_bits(value));
584 	} else {
585 		armv8pmu_write_evcntr(idx, value);
586 	}
587 }
588 
armv8pmu_write_counter(struct perf_event * event,u64 value)589 static void armv8pmu_write_counter(struct perf_event *event, u64 value)
590 {
591 	struct hw_perf_event *hwc = &event->hw;
592 	int idx = hwc->idx;
593 
594 	value = armv8pmu_bias_long_counter(event, value);
595 
596 	if (idx == ARMV8_IDX_CYCLE_COUNTER)
597 		write_sysreg(value, pmccntr_el0);
598 	else
599 		armv8pmu_write_hw_counter(event, value);
600 }
601 
armv8pmu_write_evtype(int idx,u32 val)602 static inline void armv8pmu_write_evtype(int idx, u32 val)
603 {
604 	u32 counter = ARMV8_IDX_TO_COUNTER(idx);
605 
606 	val &= ARMV8_PMU_EVTYPE_MASK;
607 	write_pmevtypern(counter, val);
608 }
609 
armv8pmu_write_event_type(struct perf_event * event)610 static inline void armv8pmu_write_event_type(struct perf_event *event)
611 {
612 	struct hw_perf_event *hwc = &event->hw;
613 	int idx = hwc->idx;
614 
615 	/*
616 	 * For chained events, the low counter is programmed to count
617 	 * the event of interest and the high counter is programmed
618 	 * with CHAIN event code with filters set to count at all ELs.
619 	 */
620 	if (armv8pmu_event_is_chained(event)) {
621 		u32 chain_evt = ARMV8_PMUV3_PERFCTR_CHAIN |
622 				ARMV8_PMU_INCLUDE_EL2;
623 
624 		armv8pmu_write_evtype(idx - 1, hwc->config_base);
625 		armv8pmu_write_evtype(idx, chain_evt);
626 	} else {
627 		if (idx == ARMV8_IDX_CYCLE_COUNTER)
628 			write_sysreg(hwc->config_base, pmccfiltr_el0);
629 		else
630 			armv8pmu_write_evtype(idx, hwc->config_base);
631 	}
632 }
633 
armv8pmu_event_cnten_mask(struct perf_event * event)634 static u32 armv8pmu_event_cnten_mask(struct perf_event *event)
635 {
636 	int counter = ARMV8_IDX_TO_COUNTER(event->hw.idx);
637 	u32 mask = BIT(counter);
638 
639 	if (armv8pmu_event_is_chained(event))
640 		mask |= BIT(counter - 1);
641 	return mask;
642 }
643 
armv8pmu_enable_counter(u32 mask)644 static inline void armv8pmu_enable_counter(u32 mask)
645 {
646 	/*
647 	 * Make sure event configuration register writes are visible before we
648 	 * enable the counter.
649 	 * */
650 	isb();
651 	write_sysreg(mask, pmcntenset_el0);
652 }
653 
armv8pmu_enable_event_counter(struct perf_event * event)654 static inline void armv8pmu_enable_event_counter(struct perf_event *event)
655 {
656 	struct perf_event_attr *attr = &event->attr;
657 	u32 mask = armv8pmu_event_cnten_mask(event);
658 
659 	kvm_set_pmu_events(mask, attr);
660 
661 	/* We rely on the hypervisor switch code to enable guest counters */
662 	if (!kvm_pmu_counter_deferred(attr))
663 		armv8pmu_enable_counter(mask);
664 }
665 
armv8pmu_disable_counter(u32 mask)666 static inline void armv8pmu_disable_counter(u32 mask)
667 {
668 	write_sysreg(mask, pmcntenclr_el0);
669 	/*
670 	 * Make sure the effects of disabling the counter are visible before we
671 	 * start configuring the event.
672 	 */
673 	isb();
674 }
675 
armv8pmu_disable_event_counter(struct perf_event * event)676 static inline void armv8pmu_disable_event_counter(struct perf_event *event)
677 {
678 	struct perf_event_attr *attr = &event->attr;
679 	u32 mask = armv8pmu_event_cnten_mask(event);
680 
681 	kvm_clr_pmu_events(mask);
682 
683 	/* We rely on the hypervisor switch code to disable guest counters */
684 	if (!kvm_pmu_counter_deferred(attr))
685 		armv8pmu_disable_counter(mask);
686 }
687 
armv8pmu_enable_intens(u32 mask)688 static inline void armv8pmu_enable_intens(u32 mask)
689 {
690 	write_sysreg(mask, pmintenset_el1);
691 }
692 
armv8pmu_enable_event_irq(struct perf_event * event)693 static inline void armv8pmu_enable_event_irq(struct perf_event *event)
694 {
695 	u32 counter = ARMV8_IDX_TO_COUNTER(event->hw.idx);
696 	armv8pmu_enable_intens(BIT(counter));
697 }
698 
armv8pmu_disable_intens(u32 mask)699 static inline void armv8pmu_disable_intens(u32 mask)
700 {
701 	write_sysreg(mask, pmintenclr_el1);
702 	isb();
703 	/* Clear the overflow flag in case an interrupt is pending. */
704 	write_sysreg(mask, pmovsclr_el0);
705 	isb();
706 }
707 
armv8pmu_disable_event_irq(struct perf_event * event)708 static inline void armv8pmu_disable_event_irq(struct perf_event *event)
709 {
710 	u32 counter = ARMV8_IDX_TO_COUNTER(event->hw.idx);
711 	armv8pmu_disable_intens(BIT(counter));
712 }
713 
armv8pmu_getreset_flags(void)714 static inline u32 armv8pmu_getreset_flags(void)
715 {
716 	u32 value;
717 
718 	/* Read */
719 	value = read_sysreg(pmovsclr_el0);
720 
721 	/* Write to clear flags */
722 	value &= ARMV8_PMU_OVSR_MASK;
723 	write_sysreg(value, pmovsclr_el0);
724 
725 	return value;
726 }
727 
armv8pmu_enable_event(struct perf_event * event)728 static void armv8pmu_enable_event(struct perf_event *event)
729 {
730 	/*
731 	 * Enable counter and interrupt, and set the counter to count
732 	 * the event that we're interested in.
733 	 */
734 
735 	/*
736 	 * Disable counter
737 	 */
738 	armv8pmu_disable_event_counter(event);
739 
740 	/*
741 	 * Set event.
742 	 */
743 	armv8pmu_write_event_type(event);
744 
745 	/*
746 	 * Enable interrupt for this counter
747 	 */
748 	armv8pmu_enable_event_irq(event);
749 
750 	/*
751 	 * Enable counter
752 	 */
753 	armv8pmu_enable_event_counter(event);
754 }
755 
armv8pmu_disable_event(struct perf_event * event)756 static void armv8pmu_disable_event(struct perf_event *event)
757 {
758 	/*
759 	 * Disable counter
760 	 */
761 	armv8pmu_disable_event_counter(event);
762 
763 	/*
764 	 * Disable interrupt for this counter
765 	 */
766 	armv8pmu_disable_event_irq(event);
767 }
768 
armv8pmu_start(struct arm_pmu * cpu_pmu)769 static void armv8pmu_start(struct arm_pmu *cpu_pmu)
770 {
771 	/* Enable all counters */
772 	armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMU_PMCR_E);
773 }
774 
armv8pmu_stop(struct arm_pmu * cpu_pmu)775 static void armv8pmu_stop(struct arm_pmu *cpu_pmu)
776 {
777 	/* Disable all counters */
778 	armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMU_PMCR_E);
779 }
780 
armv8pmu_handle_irq(struct arm_pmu * cpu_pmu)781 static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
782 {
783 	u32 pmovsr;
784 	struct perf_sample_data data;
785 	struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
786 	struct pt_regs *regs;
787 	int idx;
788 
789 	/*
790 	 * Get and reset the IRQ flags
791 	 */
792 	pmovsr = armv8pmu_getreset_flags();
793 
794 	/*
795 	 * Did an overflow occur?
796 	 */
797 	if (!armv8pmu_has_overflowed(pmovsr))
798 		return IRQ_NONE;
799 
800 	/*
801 	 * Handle the counter(s) overflow(s)
802 	 */
803 	regs = get_irq_regs();
804 
805 	/*
806 	 * Stop the PMU while processing the counter overflows
807 	 * to prevent skews in group events.
808 	 */
809 	armv8pmu_stop(cpu_pmu);
810 	for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
811 		struct perf_event *event = cpuc->events[idx];
812 		struct hw_perf_event *hwc;
813 
814 		/* Ignore if we don't have an event. */
815 		if (!event)
816 			continue;
817 
818 		/*
819 		 * We have a single interrupt for all counters. Check that
820 		 * each counter has overflowed before we process it.
821 		 */
822 		if (!armv8pmu_counter_has_overflowed(pmovsr, idx))
823 			continue;
824 
825 		hwc = &event->hw;
826 		armpmu_event_update(event);
827 		perf_sample_data_init(&data, 0, hwc->last_period);
828 		if (!armpmu_event_set_period(event))
829 			continue;
830 
831 		/*
832 		 * Perf event overflow will queue the processing of the event as
833 		 * an irq_work which will be taken care of in the handling of
834 		 * IPI_IRQ_WORK.
835 		 */
836 		if (perf_event_overflow(event, &data, regs))
837 			cpu_pmu->disable(event);
838 	}
839 	armv8pmu_start(cpu_pmu);
840 
841 	return IRQ_HANDLED;
842 }
843 
armv8pmu_get_single_idx(struct pmu_hw_events * cpuc,struct arm_pmu * cpu_pmu)844 static int armv8pmu_get_single_idx(struct pmu_hw_events *cpuc,
845 				    struct arm_pmu *cpu_pmu)
846 {
847 	int idx;
848 
849 	for (idx = ARMV8_IDX_COUNTER0; idx < cpu_pmu->num_events; idx++) {
850 		if (!test_and_set_bit(idx, cpuc->used_mask))
851 			return idx;
852 	}
853 	return -EAGAIN;
854 }
855 
armv8pmu_get_chain_idx(struct pmu_hw_events * cpuc,struct arm_pmu * cpu_pmu)856 static int armv8pmu_get_chain_idx(struct pmu_hw_events *cpuc,
857 				   struct arm_pmu *cpu_pmu)
858 {
859 	int idx;
860 
861 	/*
862 	 * Chaining requires two consecutive event counters, where
863 	 * the lower idx must be even.
864 	 */
865 	for (idx = ARMV8_IDX_COUNTER0 + 1; idx < cpu_pmu->num_events; idx += 2) {
866 		if (!test_and_set_bit(idx, cpuc->used_mask)) {
867 			/* Check if the preceding even counter is available */
868 			if (!test_and_set_bit(idx - 1, cpuc->used_mask))
869 				return idx;
870 			/* Release the Odd counter */
871 			clear_bit(idx, cpuc->used_mask);
872 		}
873 	}
874 	return -EAGAIN;
875 }
876 
armv8pmu_get_event_idx(struct pmu_hw_events * cpuc,struct perf_event * event)877 static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
878 				  struct perf_event *event)
879 {
880 	struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
881 	struct hw_perf_event *hwc = &event->hw;
882 	unsigned long evtype = hwc->config_base & ARMV8_PMU_EVTYPE_EVENT;
883 
884 	/* Always prefer to place a cycle counter into the cycle counter. */
885 	if (evtype == ARMV8_PMUV3_PERFCTR_CPU_CYCLES) {
886 		if (!test_and_set_bit(ARMV8_IDX_CYCLE_COUNTER, cpuc->used_mask))
887 			return ARMV8_IDX_CYCLE_COUNTER;
888 	}
889 
890 	/*
891 	 * Otherwise use events counters
892 	 */
893 	if (armv8pmu_event_is_64bit(event) &&
894 	    !armv8pmu_has_long_event(cpu_pmu))
895 		return	armv8pmu_get_chain_idx(cpuc, cpu_pmu);
896 	else
897 		return armv8pmu_get_single_idx(cpuc, cpu_pmu);
898 }
899 
armv8pmu_clear_event_idx(struct pmu_hw_events * cpuc,struct perf_event * event)900 static void armv8pmu_clear_event_idx(struct pmu_hw_events *cpuc,
901 				     struct perf_event *event)
902 {
903 	int idx = event->hw.idx;
904 
905 	clear_bit(idx, cpuc->used_mask);
906 	if (armv8pmu_event_is_chained(event))
907 		clear_bit(idx - 1, cpuc->used_mask);
908 }
909 
910 /*
911  * Add an event filter to a given event.
912  */
armv8pmu_set_event_filter(struct hw_perf_event * event,struct perf_event_attr * attr)913 static int armv8pmu_set_event_filter(struct hw_perf_event *event,
914 				     struct perf_event_attr *attr)
915 {
916 	unsigned long config_base = 0;
917 
918 	if (attr->exclude_idle)
919 		return -EPERM;
920 
921 	/*
922 	 * If we're running in hyp mode, then we *are* the hypervisor.
923 	 * Therefore we ignore exclude_hv in this configuration, since
924 	 * there's no hypervisor to sample anyway. This is consistent
925 	 * with other architectures (x86 and Power).
926 	 */
927 	if (is_kernel_in_hyp_mode()) {
928 		if (!attr->exclude_kernel && !attr->exclude_host)
929 			config_base |= ARMV8_PMU_INCLUDE_EL2;
930 		if (attr->exclude_guest)
931 			config_base |= ARMV8_PMU_EXCLUDE_EL1;
932 		if (attr->exclude_host)
933 			config_base |= ARMV8_PMU_EXCLUDE_EL0;
934 	} else {
935 		if (!attr->exclude_hv && !attr->exclude_host)
936 			config_base |= ARMV8_PMU_INCLUDE_EL2;
937 	}
938 
939 	/*
940 	 * Filter out !VHE kernels and guest kernels
941 	 */
942 	if (attr->exclude_kernel)
943 		config_base |= ARMV8_PMU_EXCLUDE_EL1;
944 
945 	if (attr->exclude_user)
946 		config_base |= ARMV8_PMU_EXCLUDE_EL0;
947 
948 	/*
949 	 * Install the filter into config_base as this is used to
950 	 * construct the event type.
951 	 */
952 	event->config_base = config_base;
953 
954 	return 0;
955 }
956 
armv8pmu_filter_match(struct perf_event * event)957 static int armv8pmu_filter_match(struct perf_event *event)
958 {
959 	unsigned long evtype = event->hw.config_base & ARMV8_PMU_EVTYPE_EVENT;
960 	return evtype != ARMV8_PMUV3_PERFCTR_CHAIN;
961 }
962 
export_pmu_events(char * str)963 static int __init export_pmu_events(char *str)
964 {
965 	/* Enable exporting of pmu events at early bootup with kernel
966 	 * arguments.
967 	 */
968 	sysctl_export_pmu_events = 1;
969 	return 0;
970 }
971 
972 early_param("export_pmu_events", export_pmu_events);
973 
armv8pmu_reset(void * info)974 static void armv8pmu_reset(void *info)
975 {
976 	struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
977 	u32 pmcr;
978 
979 	/* The counter and interrupt enable registers are unknown at reset. */
980 	armv8pmu_disable_counter(U32_MAX);
981 	armv8pmu_disable_intens(U32_MAX);
982 
983 	/* Clear the counters we flip at guest entry/exit */
984 	kvm_clr_pmu_events(U32_MAX);
985 
986 	/*
987 	 * Initialize & Reset PMNC. Request overflow interrupt for
988 	 * 64 bit cycle counter but cheat in armv8pmu_write_counter().
989 	 */
990 	pmcr = ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C | ARMV8_PMU_PMCR_LC;
991 
992 	/* Enable long event counter support where available */
993 	if (armv8pmu_has_long_event(cpu_pmu))
994 		pmcr |= ARMV8_PMU_PMCR_LP;
995 
996 	if (sysctl_export_pmu_events)
997 		pmcr |= ARMV8_PMU_PMCR_X;
998 
999 	armv8pmu_pmcr_write(pmcr);
1000 }
1001 
__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])1002 static int __armv8_pmuv3_map_event(struct perf_event *event,
1003 				   const unsigned (*extra_event_map)
1004 						  [PERF_COUNT_HW_MAX],
1005 				   const unsigned (*extra_cache_map)
1006 						  [PERF_COUNT_HW_CACHE_MAX]
1007 						  [PERF_COUNT_HW_CACHE_OP_MAX]
1008 						  [PERF_COUNT_HW_CACHE_RESULT_MAX])
1009 {
1010 	int hw_event_id;
1011 	struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
1012 
1013 	hw_event_id = armpmu_map_event(event, &armv8_pmuv3_perf_map,
1014 				       &armv8_pmuv3_perf_cache_map,
1015 				       ARMV8_PMU_EVTYPE_EVENT);
1016 
1017 	if (armv8pmu_event_is_64bit(event))
1018 		event->hw.flags |= ARMPMU_EVT_64BIT;
1019 
1020 	/* Only expose micro/arch events supported by this PMU */
1021 	if ((hw_event_id > 0) && (hw_event_id < ARMV8_PMUV3_MAX_COMMON_EVENTS)
1022 	    && test_bit(hw_event_id, armpmu->pmceid_bitmap)) {
1023 		return hw_event_id;
1024 	}
1025 
1026 	return armpmu_map_event(event, extra_event_map, extra_cache_map,
1027 				ARMV8_PMU_EVTYPE_EVENT);
1028 }
1029 
armv8_pmuv3_map_event(struct perf_event * event)1030 static int armv8_pmuv3_map_event(struct perf_event *event)
1031 {
1032 	return __armv8_pmuv3_map_event(event, NULL, NULL);
1033 }
1034 
armv8_a53_map_event(struct perf_event * event)1035 static int armv8_a53_map_event(struct perf_event *event)
1036 {
1037 	return __armv8_pmuv3_map_event(event, NULL, &armv8_a53_perf_cache_map);
1038 }
1039 
armv8_a57_map_event(struct perf_event * event)1040 static int armv8_a57_map_event(struct perf_event *event)
1041 {
1042 	return __armv8_pmuv3_map_event(event, NULL, &armv8_a57_perf_cache_map);
1043 }
1044 
armv8_a73_map_event(struct perf_event * event)1045 static int armv8_a73_map_event(struct perf_event *event)
1046 {
1047 	return __armv8_pmuv3_map_event(event, NULL, &armv8_a73_perf_cache_map);
1048 }
1049 
armv8_thunder_map_event(struct perf_event * event)1050 static int armv8_thunder_map_event(struct perf_event *event)
1051 {
1052 	return __armv8_pmuv3_map_event(event, NULL,
1053 				       &armv8_thunder_perf_cache_map);
1054 }
1055 
armv8_vulcan_map_event(struct perf_event * event)1056 static int armv8_vulcan_map_event(struct perf_event *event)
1057 {
1058 	return __armv8_pmuv3_map_event(event, NULL,
1059 				       &armv8_vulcan_perf_cache_map);
1060 }
1061 
1062 struct armv8pmu_probe_info {
1063 	struct arm_pmu *pmu;
1064 	bool present;
1065 };
1066 
__armv8pmu_probe_pmu(void * info)1067 static void __armv8pmu_probe_pmu(void *info)
1068 {
1069 	struct armv8pmu_probe_info *probe = info;
1070 	struct arm_pmu *cpu_pmu = probe->pmu;
1071 	u64 dfr0;
1072 	u64 pmceid_raw[2];
1073 	u32 pmceid[2];
1074 	int pmuver;
1075 
1076 	dfr0 = read_sysreg(id_aa64dfr0_el1);
1077 	pmuver = cpuid_feature_extract_unsigned_field(dfr0,
1078 			ID_AA64DFR0_EL1_PMUVer_SHIFT);
1079 	if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF || pmuver == 0)
1080 		return;
1081 
1082 	cpu_pmu->pmuver = pmuver;
1083 	probe->present = true;
1084 
1085 	/* Read the nb of CNTx counters supported from PMNC */
1086 	cpu_pmu->num_events = (armv8pmu_pmcr_read() >> ARMV8_PMU_PMCR_N_SHIFT)
1087 		& ARMV8_PMU_PMCR_N_MASK;
1088 
1089 	/* Add the CPU cycles counter */
1090 	cpu_pmu->num_events += 1;
1091 
1092 	pmceid[0] = pmceid_raw[0] = read_sysreg(pmceid0_el0);
1093 	pmceid[1] = pmceid_raw[1] = read_sysreg(pmceid1_el0);
1094 
1095 	bitmap_from_arr32(cpu_pmu->pmceid_bitmap,
1096 			     pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
1097 
1098 	pmceid[0] = pmceid_raw[0] >> 32;
1099 	pmceid[1] = pmceid_raw[1] >> 32;
1100 
1101 	bitmap_from_arr32(cpu_pmu->pmceid_ext_bitmap,
1102 			     pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
1103 
1104 	/* store PMMIR_EL1 register for sysfs */
1105 	if (pmuver >= ID_AA64DFR0_EL1_PMUVer_V3P4 && (pmceid_raw[1] & BIT(31)))
1106 		cpu_pmu->reg_pmmir = read_cpuid(PMMIR_EL1);
1107 	else
1108 		cpu_pmu->reg_pmmir = 0;
1109 }
1110 
armv8pmu_probe_pmu(struct arm_pmu * cpu_pmu)1111 static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
1112 {
1113 	struct armv8pmu_probe_info probe = {
1114 		.pmu = cpu_pmu,
1115 		.present = false,
1116 	};
1117 	int ret;
1118 
1119 	ret = smp_call_function_any(&cpu_pmu->supported_cpus,
1120 				    __armv8pmu_probe_pmu,
1121 				    &probe, 1);
1122 	if (ret)
1123 		return ret;
1124 
1125 	return probe.present ? 0 : -ENODEV;
1126 }
1127 
1128 static struct ctl_table armv8_pmu_sysctl_table[] = {
1129 	{
1130 		.procname       = "perf_user_access",
1131 		.data		= &sysctl_perf_user_access,
1132 		.maxlen		= sizeof(unsigned int),
1133 		.mode           = 0644,
1134 		.proc_handler	= proc_dointvec_minmax,
1135 		.extra1		= SYSCTL_ZERO,
1136 		.extra2		= SYSCTL_ONE,
1137 	},
1138 	{
1139 		.procname       = "export_pmu_events",
1140 		.data           = &sysctl_export_pmu_events,
1141 		.maxlen         = sizeof(unsigned int),
1142 		.mode           = 0644,
1143 		.proc_handler   = proc_dointvec_minmax,
1144 		.extra1         = SYSCTL_ZERO,
1145 		.extra2         = SYSCTL_ONE,
1146 	},
1147 	{ }
1148 };
1149 
armv8_pmu_register_sysctl_table(void)1150 static void armv8_pmu_register_sysctl_table(void)
1151 {
1152 	static u32 tbl_registered = 0;
1153 
1154 	if (!cmpxchg_relaxed(&tbl_registered, 0, 1))
1155 		register_sysctl("kernel", armv8_pmu_sysctl_table);
1156 }
1157 
armv8_pmu_init(struct arm_pmu * cpu_pmu,char * name,int (* map_event)(struct perf_event * event),const struct attribute_group * events,const struct attribute_group * format,const struct attribute_group * caps)1158 static int armv8_pmu_init(struct arm_pmu *cpu_pmu, char *name,
1159 			  int (*map_event)(struct perf_event *event),
1160 			  const struct attribute_group *events,
1161 			  const struct attribute_group *format,
1162 			  const struct attribute_group *caps)
1163 {
1164 	int ret = armv8pmu_probe_pmu(cpu_pmu);
1165 	if (ret)
1166 		return ret;
1167 
1168 	cpu_pmu->handle_irq		= armv8pmu_handle_irq;
1169 	cpu_pmu->enable			= armv8pmu_enable_event;
1170 	cpu_pmu->disable		= armv8pmu_disable_event;
1171 	cpu_pmu->read_counter		= armv8pmu_read_counter;
1172 	cpu_pmu->write_counter		= armv8pmu_write_counter;
1173 	cpu_pmu->get_event_idx		= armv8pmu_get_event_idx;
1174 	cpu_pmu->clear_event_idx	= armv8pmu_clear_event_idx;
1175 	cpu_pmu->start			= armv8pmu_start;
1176 	cpu_pmu->stop			= armv8pmu_stop;
1177 	cpu_pmu->reset			= armv8pmu_reset;
1178 	cpu_pmu->set_event_filter	= armv8pmu_set_event_filter;
1179 	cpu_pmu->filter_match		= armv8pmu_filter_match;
1180 
1181 	cpu_pmu->name			= name;
1182 	cpu_pmu->map_event		= map_event;
1183 	cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] = events ?
1184 			events : &armv8_pmuv3_events_attr_group;
1185 	cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] = format ?
1186 			format : &armv8_pmuv3_format_attr_group;
1187 	cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_CAPS] = caps ?
1188 			caps : &armv8_pmuv3_caps_attr_group;
1189 
1190 	armv8_pmu_register_sysctl_table();
1191 	return 0;
1192 }
1193 
armv8_pmu_init_nogroups(struct arm_pmu * cpu_pmu,char * name,int (* map_event)(struct perf_event * event))1194 static int armv8_pmu_init_nogroups(struct arm_pmu *cpu_pmu, char *name,
1195 				   int (*map_event)(struct perf_event *event))
1196 {
1197 	return armv8_pmu_init(cpu_pmu, name, map_event, NULL, NULL, NULL);
1198 }
1199 
1200 #define PMUV3_INIT_SIMPLE(name)						\
1201 static int name##_pmu_init(struct arm_pmu *cpu_pmu)			\
1202 {									\
1203 	return armv8_pmu_init_nogroups(cpu_pmu, #name, armv8_pmuv3_map_event);\
1204 }
1205 
1206 PMUV3_INIT_SIMPLE(armv8_pmuv3)
1207 
PMUV3_INIT_SIMPLE(armv8_cortex_a34)1208 PMUV3_INIT_SIMPLE(armv8_cortex_a34)
1209 PMUV3_INIT_SIMPLE(armv8_cortex_a55)
1210 PMUV3_INIT_SIMPLE(armv8_cortex_a65)
1211 PMUV3_INIT_SIMPLE(armv8_cortex_a75)
1212 PMUV3_INIT_SIMPLE(armv8_cortex_a76)
1213 PMUV3_INIT_SIMPLE(armv8_cortex_a77)
1214 PMUV3_INIT_SIMPLE(armv8_cortex_a78)
1215 PMUV3_INIT_SIMPLE(armv9_cortex_a510)
1216 PMUV3_INIT_SIMPLE(armv9_cortex_a710)
1217 PMUV3_INIT_SIMPLE(armv8_cortex_x1)
1218 PMUV3_INIT_SIMPLE(armv9_cortex_x2)
1219 PMUV3_INIT_SIMPLE(armv8_neoverse_e1)
1220 PMUV3_INIT_SIMPLE(armv8_neoverse_n1)
1221 PMUV3_INIT_SIMPLE(armv9_neoverse_n2)
1222 PMUV3_INIT_SIMPLE(armv8_neoverse_v1)
1223 
1224 PMUV3_INIT_SIMPLE(armv8_nvidia_carmel)
1225 PMUV3_INIT_SIMPLE(armv8_nvidia_denver)
1226 
1227 static int armv8_a35_pmu_init(struct arm_pmu *cpu_pmu)
1228 {
1229 	return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a35",
1230 				       armv8_a53_map_event);
1231 }
1232 
armv8_a53_pmu_init(struct arm_pmu * cpu_pmu)1233 static int armv8_a53_pmu_init(struct arm_pmu *cpu_pmu)
1234 {
1235 	return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a53",
1236 				       armv8_a53_map_event);
1237 }
1238 
armv8_a57_pmu_init(struct arm_pmu * cpu_pmu)1239 static int armv8_a57_pmu_init(struct arm_pmu *cpu_pmu)
1240 {
1241 	return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a57",
1242 				       armv8_a57_map_event);
1243 }
1244 
armv8_a72_pmu_init(struct arm_pmu * cpu_pmu)1245 static int armv8_a72_pmu_init(struct arm_pmu *cpu_pmu)
1246 {
1247 	return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a72",
1248 				       armv8_a57_map_event);
1249 }
1250 
armv8_a73_pmu_init(struct arm_pmu * cpu_pmu)1251 static int armv8_a73_pmu_init(struct arm_pmu *cpu_pmu)
1252 {
1253 	return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a73",
1254 				       armv8_a73_map_event);
1255 }
1256 
armv8_thunder_pmu_init(struct arm_pmu * cpu_pmu)1257 static int armv8_thunder_pmu_init(struct arm_pmu *cpu_pmu)
1258 {
1259 	return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cavium_thunder",
1260 				       armv8_thunder_map_event);
1261 }
1262 
armv8_vulcan_pmu_init(struct arm_pmu * cpu_pmu)1263 static int armv8_vulcan_pmu_init(struct arm_pmu *cpu_pmu)
1264 {
1265 	return armv8_pmu_init_nogroups(cpu_pmu, "armv8_brcm_vulcan",
1266 				       armv8_vulcan_map_event);
1267 }
1268 
1269 static const struct of_device_id armv8_pmu_of_device_ids[] = {
1270 	{.compatible = "arm,armv8-pmuv3",	.data = armv8_pmuv3_pmu_init},
1271 	{.compatible = "arm,cortex-a34-pmu",	.data = armv8_cortex_a34_pmu_init},
1272 	{.compatible = "arm,cortex-a35-pmu",	.data = armv8_a35_pmu_init},
1273 	{.compatible = "arm,cortex-a53-pmu",	.data = armv8_a53_pmu_init},
1274 	{.compatible = "arm,cortex-a55-pmu",	.data = armv8_cortex_a55_pmu_init},
1275 	{.compatible = "arm,cortex-a57-pmu",	.data = armv8_a57_pmu_init},
1276 	{.compatible = "arm,cortex-a65-pmu",	.data = armv8_cortex_a65_pmu_init},
1277 	{.compatible = "arm,cortex-a72-pmu",	.data = armv8_a72_pmu_init},
1278 	{.compatible = "arm,cortex-a73-pmu",	.data = armv8_a73_pmu_init},
1279 	{.compatible = "arm,cortex-a75-pmu",	.data = armv8_cortex_a75_pmu_init},
1280 	{.compatible = "arm,cortex-a76-pmu",	.data = armv8_cortex_a76_pmu_init},
1281 	{.compatible = "arm,cortex-a77-pmu",	.data = armv8_cortex_a77_pmu_init},
1282 	{.compatible = "arm,cortex-a78-pmu",	.data = armv8_cortex_a78_pmu_init},
1283 	{.compatible = "arm,cortex-a510-pmu",	.data = armv9_cortex_a510_pmu_init},
1284 	{.compatible = "arm,cortex-a710-pmu",	.data = armv9_cortex_a710_pmu_init},
1285 	{.compatible = "arm,cortex-x1-pmu",	.data = armv8_cortex_x1_pmu_init},
1286 	{.compatible = "arm,cortex-x2-pmu",	.data = armv9_cortex_x2_pmu_init},
1287 	{.compatible = "arm,neoverse-e1-pmu",	.data = armv8_neoverse_e1_pmu_init},
1288 	{.compatible = "arm,neoverse-n1-pmu",	.data = armv8_neoverse_n1_pmu_init},
1289 	{.compatible = "arm,neoverse-n2-pmu",	.data = armv9_neoverse_n2_pmu_init},
1290 	{.compatible = "arm,neoverse-v1-pmu",	.data = armv8_neoverse_v1_pmu_init},
1291 	{.compatible = "cavium,thunder-pmu",	.data = armv8_thunder_pmu_init},
1292 	{.compatible = "brcm,vulcan-pmu",	.data = armv8_vulcan_pmu_init},
1293 	{.compatible = "nvidia,carmel-pmu",	.data = armv8_nvidia_carmel_pmu_init},
1294 	{.compatible = "nvidia,denver-pmu",	.data = armv8_nvidia_denver_pmu_init},
1295 	{},
1296 };
1297 
armv8_pmu_device_probe(struct platform_device * pdev)1298 static int armv8_pmu_device_probe(struct platform_device *pdev)
1299 {
1300 	return arm_pmu_device_probe(pdev, armv8_pmu_of_device_ids, NULL);
1301 }
1302 
1303 static struct platform_driver armv8_pmu_driver = {
1304 	.driver		= {
1305 		.name	= ARMV8_PMU_PDEV_NAME,
1306 		.of_match_table = armv8_pmu_of_device_ids,
1307 		.suppress_bind_attrs = true,
1308 	},
1309 	.probe		= armv8_pmu_device_probe,
1310 };
1311 
armv8_pmu_driver_init(void)1312 static int __init armv8_pmu_driver_init(void)
1313 {
1314 	if (acpi_disabled)
1315 		return platform_driver_register(&armv8_pmu_driver);
1316 	else
1317 		return arm_pmu_acpi_probe(armv8_pmuv3_pmu_init);
1318 }
device_initcall(armv8_pmu_driver_init)1319 device_initcall(armv8_pmu_driver_init)
1320 
1321 void arch_perf_update_userpage(struct perf_event *event,
1322 			       struct perf_event_mmap_page *userpg, u64 now)
1323 {
1324 	struct clock_read_data *rd;
1325 	unsigned int seq;
1326 	u64 ns;
1327 
1328 	userpg->cap_user_time = 0;
1329 	userpg->cap_user_time_zero = 0;
1330 	userpg->cap_user_time_short = 0;
1331 
1332 	do {
1333 		rd = sched_clock_read_begin(&seq);
1334 
1335 		if (rd->read_sched_clock != arch_timer_read_counter)
1336 			return;
1337 
1338 		userpg->time_mult = rd->mult;
1339 		userpg->time_shift = rd->shift;
1340 		userpg->time_zero = rd->epoch_ns;
1341 		userpg->time_cycles = rd->epoch_cyc;
1342 		userpg->time_mask = rd->sched_clock_mask;
1343 
1344 		/*
1345 		 * Subtract the cycle base, such that software that
1346 		 * doesn't know about cap_user_time_short still 'works'
1347 		 * assuming no wraps.
1348 		 */
1349 		ns = mul_u64_u32_shr(rd->epoch_cyc, rd->mult, rd->shift);
1350 		userpg->time_zero -= ns;
1351 
1352 	} while (sched_clock_read_retry(seq));
1353 
1354 	userpg->time_offset = userpg->time_zero - now;
1355 
1356 	/*
1357 	 * time_shift is not expected to be greater than 31 due to
1358 	 * the original published conversion algorithm shifting a
1359 	 * 32-bit value (now specifies a 64-bit value) - refer
1360 	 * perf_event_mmap_page documentation in perf_event.h.
1361 	 */
1362 	if (userpg->time_shift == 32) {
1363 		userpg->time_shift = 31;
1364 		userpg->time_mult >>= 1;
1365 	}
1366 
1367 	/*
1368 	 * Internal timekeeping for enabled/running/stopped times
1369 	 * is always computed with the sched_clock.
1370 	 */
1371 	userpg->cap_user_time = 1;
1372 	userpg->cap_user_time_zero = 1;
1373 	userpg->cap_user_time_short = 1;
1374 }
1375