WIP FPC-III support
[linux/fpc-iii.git] / arch / arm64 / kernel / perf_event.c
blob38bb07eff8720d238e1d03759f59fb76e8f5ee05
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ARMv8 PMUv3 Performance Events handling code.
5 * Copyright (C) 2012 ARM Limited
6 * Author: Will Deacon <will.deacon@arm.com>
8 * This code is based heavily on the ARMv7 perf event code.
9 */
11 #include <asm/irq_regs.h>
12 #include <asm/perf_event.h>
13 #include <asm/sysreg.h>
14 #include <asm/virt.h>
16 #include <clocksource/arm_arch_timer.h>
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 #include <linux/nmi.h>
27 #include <linux/cpufreq.h>
29 /* ARMv8 Cortex-A53 specific event types. */
30 #define ARMV8_A53_PERFCTR_PREF_LINEFILL 0xC2
32 /* ARMv8 Cavium ThunderX specific event types. */
33 #define ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST 0xE9
34 #define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS 0xEA
35 #define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS 0xEB
36 #define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS 0xEC
37 #define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS 0xED
40 * ARMv8 Architectural defined events, not all of these may
41 * be supported on any given implementation. Unsupported events will
42 * be disabled at run-time based on the PMCEID registers.
44 static const unsigned armv8_pmuv3_perf_map[PERF_COUNT_HW_MAX] = {
45 PERF_MAP_ALL_UNSUPPORTED,
46 [PERF_COUNT_HW_CPU_CYCLES] = ARMV8_PMUV3_PERFCTR_CPU_CYCLES,
47 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_INST_RETIRED,
48 [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
49 [PERF_COUNT_HW_CACHE_MISSES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
50 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED,
51 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
52 [PERF_COUNT_HW_BUS_CYCLES] = ARMV8_PMUV3_PERFCTR_BUS_CYCLES,
53 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV8_PMUV3_PERFCTR_STALL_FRONTEND,
54 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV8_PMUV3_PERFCTR_STALL_BACKEND,
57 static const unsigned armv8_pmuv3_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
58 [PERF_COUNT_HW_CACHE_OP_MAX]
59 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
60 PERF_CACHE_MAP_ALL_UNSUPPORTED,
62 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
63 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
65 [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE,
66 [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL,
68 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL,
69 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_TLB,
71 [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL,
72 [C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB,
74 [C(LL)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS_RD,
75 [C(LL)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_LL_CACHE_RD,
77 [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
78 [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
81 static const unsigned armv8_a53_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
82 [PERF_COUNT_HW_CACHE_OP_MAX]
83 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
84 PERF_CACHE_MAP_ALL_UNSUPPORTED,
86 [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_A53_PERFCTR_PREF_LINEFILL,
88 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
89 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
92 static const unsigned armv8_a57_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
93 [PERF_COUNT_HW_CACHE_OP_MAX]
94 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
95 PERF_CACHE_MAP_ALL_UNSUPPORTED,
97 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
98 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
99 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
100 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR,
102 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
103 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
105 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
106 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
109 static const unsigned armv8_a73_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
110 [PERF_COUNT_HW_CACHE_OP_MAX]
111 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
112 PERF_CACHE_MAP_ALL_UNSUPPORTED,
114 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
115 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
118 static const unsigned armv8_thunder_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
119 [PERF_COUNT_HW_CACHE_OP_MAX]
120 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
121 PERF_CACHE_MAP_ALL_UNSUPPORTED,
123 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
124 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
125 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
126 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST,
127 [C(L1D)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS,
128 [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS,
130 [C(L1I)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS,
131 [C(L1I)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS,
133 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
134 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
135 [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
136 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
139 static const unsigned armv8_vulcan_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
140 [PERF_COUNT_HW_CACHE_OP_MAX]
141 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
142 PERF_CACHE_MAP_ALL_UNSUPPORTED,
144 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
145 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
146 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
147 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR,
149 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
150 [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
151 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
152 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
154 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
155 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
158 static ssize_t
159 armv8pmu_events_sysfs_show(struct device *dev,
160 struct device_attribute *attr, char *page)
162 struct perf_pmu_events_attr *pmu_attr;
164 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
166 return sprintf(page, "event=0x%04llx\n", pmu_attr->id);
169 #define ARMV8_EVENT_ATTR(name, config) \
170 (&((struct perf_pmu_events_attr) { \
171 .attr = __ATTR(name, 0444, armv8pmu_events_sysfs_show, NULL), \
172 .id = config, \
173 }).attr.attr)
175 static struct attribute *armv8_pmuv3_event_attrs[] = {
176 ARMV8_EVENT_ATTR(sw_incr, ARMV8_PMUV3_PERFCTR_SW_INCR),
177 ARMV8_EVENT_ATTR(l1i_cache_refill, ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL),
178 ARMV8_EVENT_ATTR(l1i_tlb_refill, ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL),
179 ARMV8_EVENT_ATTR(l1d_cache_refill, ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL),
180 ARMV8_EVENT_ATTR(l1d_cache, ARMV8_PMUV3_PERFCTR_L1D_CACHE),
181 ARMV8_EVENT_ATTR(l1d_tlb_refill, ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL),
182 ARMV8_EVENT_ATTR(ld_retired, ARMV8_PMUV3_PERFCTR_LD_RETIRED),
183 ARMV8_EVENT_ATTR(st_retired, ARMV8_PMUV3_PERFCTR_ST_RETIRED),
184 ARMV8_EVENT_ATTR(inst_retired, ARMV8_PMUV3_PERFCTR_INST_RETIRED),
185 ARMV8_EVENT_ATTR(exc_taken, ARMV8_PMUV3_PERFCTR_EXC_TAKEN),
186 ARMV8_EVENT_ATTR(exc_return, ARMV8_PMUV3_PERFCTR_EXC_RETURN),
187 ARMV8_EVENT_ATTR(cid_write_retired, ARMV8_PMUV3_PERFCTR_CID_WRITE_RETIRED),
188 ARMV8_EVENT_ATTR(pc_write_retired, ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED),
189 ARMV8_EVENT_ATTR(br_immed_retired, ARMV8_PMUV3_PERFCTR_BR_IMMED_RETIRED),
190 ARMV8_EVENT_ATTR(br_return_retired, ARMV8_PMUV3_PERFCTR_BR_RETURN_RETIRED),
191 ARMV8_EVENT_ATTR(unaligned_ldst_retired, ARMV8_PMUV3_PERFCTR_UNALIGNED_LDST_RETIRED),
192 ARMV8_EVENT_ATTR(br_mis_pred, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED),
193 ARMV8_EVENT_ATTR(cpu_cycles, ARMV8_PMUV3_PERFCTR_CPU_CYCLES),
194 ARMV8_EVENT_ATTR(br_pred, ARMV8_PMUV3_PERFCTR_BR_PRED),
195 ARMV8_EVENT_ATTR(mem_access, ARMV8_PMUV3_PERFCTR_MEM_ACCESS),
196 ARMV8_EVENT_ATTR(l1i_cache, ARMV8_PMUV3_PERFCTR_L1I_CACHE),
197 ARMV8_EVENT_ATTR(l1d_cache_wb, ARMV8_PMUV3_PERFCTR_L1D_CACHE_WB),
198 ARMV8_EVENT_ATTR(l2d_cache, ARMV8_PMUV3_PERFCTR_L2D_CACHE),
199 ARMV8_EVENT_ATTR(l2d_cache_refill, ARMV8_PMUV3_PERFCTR_L2D_CACHE_REFILL),
200 ARMV8_EVENT_ATTR(l2d_cache_wb, ARMV8_PMUV3_PERFCTR_L2D_CACHE_WB),
201 ARMV8_EVENT_ATTR(bus_access, ARMV8_PMUV3_PERFCTR_BUS_ACCESS),
202 ARMV8_EVENT_ATTR(memory_error, ARMV8_PMUV3_PERFCTR_MEMORY_ERROR),
203 ARMV8_EVENT_ATTR(inst_spec, ARMV8_PMUV3_PERFCTR_INST_SPEC),
204 ARMV8_EVENT_ATTR(ttbr_write_retired, ARMV8_PMUV3_PERFCTR_TTBR_WRITE_RETIRED),
205 ARMV8_EVENT_ATTR(bus_cycles, ARMV8_PMUV3_PERFCTR_BUS_CYCLES),
206 /* Don't expose the chain event in /sys, since it's useless in isolation */
207 ARMV8_EVENT_ATTR(l1d_cache_allocate, ARMV8_PMUV3_PERFCTR_L1D_CACHE_ALLOCATE),
208 ARMV8_EVENT_ATTR(l2d_cache_allocate, ARMV8_PMUV3_PERFCTR_L2D_CACHE_ALLOCATE),
209 ARMV8_EVENT_ATTR(br_retired, ARMV8_PMUV3_PERFCTR_BR_RETIRED),
210 ARMV8_EVENT_ATTR(br_mis_pred_retired, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED_RETIRED),
211 ARMV8_EVENT_ATTR(stall_frontend, ARMV8_PMUV3_PERFCTR_STALL_FRONTEND),
212 ARMV8_EVENT_ATTR(stall_backend, ARMV8_PMUV3_PERFCTR_STALL_BACKEND),
213 ARMV8_EVENT_ATTR(l1d_tlb, ARMV8_PMUV3_PERFCTR_L1D_TLB),
214 ARMV8_EVENT_ATTR(l1i_tlb, ARMV8_PMUV3_PERFCTR_L1I_TLB),
215 ARMV8_EVENT_ATTR(l2i_cache, ARMV8_PMUV3_PERFCTR_L2I_CACHE),
216 ARMV8_EVENT_ATTR(l2i_cache_refill, ARMV8_PMUV3_PERFCTR_L2I_CACHE_REFILL),
217 ARMV8_EVENT_ATTR(l3d_cache_allocate, ARMV8_PMUV3_PERFCTR_L3D_CACHE_ALLOCATE),
218 ARMV8_EVENT_ATTR(l3d_cache_refill, ARMV8_PMUV3_PERFCTR_L3D_CACHE_REFILL),
219 ARMV8_EVENT_ATTR(l3d_cache, ARMV8_PMUV3_PERFCTR_L3D_CACHE),
220 ARMV8_EVENT_ATTR(l3d_cache_wb, ARMV8_PMUV3_PERFCTR_L3D_CACHE_WB),
221 ARMV8_EVENT_ATTR(l2d_tlb_refill, ARMV8_PMUV3_PERFCTR_L2D_TLB_REFILL),
222 ARMV8_EVENT_ATTR(l2i_tlb_refill, ARMV8_PMUV3_PERFCTR_L2I_TLB_REFILL),
223 ARMV8_EVENT_ATTR(l2d_tlb, ARMV8_PMUV3_PERFCTR_L2D_TLB),
224 ARMV8_EVENT_ATTR(l2i_tlb, ARMV8_PMUV3_PERFCTR_L2I_TLB),
225 ARMV8_EVENT_ATTR(remote_access, ARMV8_PMUV3_PERFCTR_REMOTE_ACCESS),
226 ARMV8_EVENT_ATTR(ll_cache, ARMV8_PMUV3_PERFCTR_LL_CACHE),
227 ARMV8_EVENT_ATTR(ll_cache_miss, ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS),
228 ARMV8_EVENT_ATTR(dtlb_walk, ARMV8_PMUV3_PERFCTR_DTLB_WALK),
229 ARMV8_EVENT_ATTR(itlb_walk, ARMV8_PMUV3_PERFCTR_ITLB_WALK),
230 ARMV8_EVENT_ATTR(ll_cache_rd, ARMV8_PMUV3_PERFCTR_LL_CACHE_RD),
231 ARMV8_EVENT_ATTR(ll_cache_miss_rd, ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS_RD),
232 ARMV8_EVENT_ATTR(remote_access_rd, ARMV8_PMUV3_PERFCTR_REMOTE_ACCESS_RD),
233 ARMV8_EVENT_ATTR(l1d_cache_lmiss_rd, ARMV8_PMUV3_PERFCTR_L1D_CACHE_LMISS_RD),
234 ARMV8_EVENT_ATTR(op_retired, ARMV8_PMUV3_PERFCTR_OP_RETIRED),
235 ARMV8_EVENT_ATTR(op_spec, ARMV8_PMUV3_PERFCTR_OP_SPEC),
236 ARMV8_EVENT_ATTR(stall, ARMV8_PMUV3_PERFCTR_STALL),
237 ARMV8_EVENT_ATTR(stall_slot_backend, ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND),
238 ARMV8_EVENT_ATTR(stall_slot_frontend, ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND),
239 ARMV8_EVENT_ATTR(stall_slot, ARMV8_PMUV3_PERFCTR_STALL_SLOT),
240 ARMV8_EVENT_ATTR(sample_pop, ARMV8_SPE_PERFCTR_SAMPLE_POP),
241 ARMV8_EVENT_ATTR(sample_feed, ARMV8_SPE_PERFCTR_SAMPLE_FEED),
242 ARMV8_EVENT_ATTR(sample_filtrate, ARMV8_SPE_PERFCTR_SAMPLE_FILTRATE),
243 ARMV8_EVENT_ATTR(sample_collision, ARMV8_SPE_PERFCTR_SAMPLE_COLLISION),
244 ARMV8_EVENT_ATTR(cnt_cycles, ARMV8_AMU_PERFCTR_CNT_CYCLES),
245 ARMV8_EVENT_ATTR(stall_backend_mem, ARMV8_AMU_PERFCTR_STALL_BACKEND_MEM),
246 ARMV8_EVENT_ATTR(l1i_cache_lmiss, ARMV8_PMUV3_PERFCTR_L1I_CACHE_LMISS),
247 ARMV8_EVENT_ATTR(l2d_cache_lmiss_rd, ARMV8_PMUV3_PERFCTR_L2D_CACHE_LMISS_RD),
248 ARMV8_EVENT_ATTR(l2i_cache_lmiss, ARMV8_PMUV3_PERFCTR_L2I_CACHE_LMISS),
249 ARMV8_EVENT_ATTR(l3d_cache_lmiss_rd, ARMV8_PMUV3_PERFCTR_L3D_CACHE_LMISS_RD),
250 ARMV8_EVENT_ATTR(ldst_align_lat, ARMV8_PMUV3_PERFCTR_LDST_ALIGN_LAT),
251 ARMV8_EVENT_ATTR(ld_align_lat, ARMV8_PMUV3_PERFCTR_LD_ALIGN_LAT),
252 ARMV8_EVENT_ATTR(st_align_lat, ARMV8_PMUV3_PERFCTR_ST_ALIGN_LAT),
253 ARMV8_EVENT_ATTR(mem_access_checked, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED),
254 ARMV8_EVENT_ATTR(mem_access_checked_rd, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED_RD),
255 ARMV8_EVENT_ATTR(mem_access_checked_wr, ARMV8_MTE_PERFCTR_MEM_ACCESS_CHECKED_WR),
256 NULL,
259 static umode_t
260 armv8pmu_event_attr_is_visible(struct kobject *kobj,
261 struct attribute *attr, int unused)
263 struct device *dev = kobj_to_dev(kobj);
264 struct pmu *pmu = dev_get_drvdata(dev);
265 struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
266 struct perf_pmu_events_attr *pmu_attr;
268 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
270 if (pmu_attr->id < ARMV8_PMUV3_MAX_COMMON_EVENTS &&
271 test_bit(pmu_attr->id, cpu_pmu->pmceid_bitmap))
272 return attr->mode;
274 if (pmu_attr->id >= ARMV8_PMUV3_EXT_COMMON_EVENT_BASE) {
275 u64 id = pmu_attr->id - ARMV8_PMUV3_EXT_COMMON_EVENT_BASE;
277 if (id < ARMV8_PMUV3_MAX_COMMON_EVENTS &&
278 test_bit(id, cpu_pmu->pmceid_ext_bitmap))
279 return attr->mode;
282 return 0;
285 static struct attribute_group armv8_pmuv3_events_attr_group = {
286 .name = "events",
287 .attrs = armv8_pmuv3_event_attrs,
288 .is_visible = armv8pmu_event_attr_is_visible,
291 PMU_FORMAT_ATTR(event, "config:0-15");
292 PMU_FORMAT_ATTR(long, "config1:0");
294 static inline bool armv8pmu_event_is_64bit(struct perf_event *event)
296 return event->attr.config1 & 0x1;
299 static struct attribute *armv8_pmuv3_format_attrs[] = {
300 &format_attr_event.attr,
301 &format_attr_long.attr,
302 NULL,
305 static struct attribute_group armv8_pmuv3_format_attr_group = {
306 .name = "format",
307 .attrs = armv8_pmuv3_format_attrs,
310 static ssize_t slots_show(struct device *dev, struct device_attribute *attr,
311 char *page)
313 struct pmu *pmu = dev_get_drvdata(dev);
314 struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
315 u32 slots = cpu_pmu->reg_pmmir & ARMV8_PMU_SLOTS_MASK;
317 return snprintf(page, PAGE_SIZE, "0x%08x\n", slots);
320 static DEVICE_ATTR_RO(slots);
322 static struct attribute *armv8_pmuv3_caps_attrs[] = {
323 &dev_attr_slots.attr,
324 NULL,
327 static struct attribute_group armv8_pmuv3_caps_attr_group = {
328 .name = "caps",
329 .attrs = armv8_pmuv3_caps_attrs,
333 * Perf Events' indices
335 #define ARMV8_IDX_CYCLE_COUNTER 0
336 #define ARMV8_IDX_COUNTER0 1
340 * We unconditionally enable ARMv8.5-PMU long event counter support
341 * (64-bit events) where supported. Indicate if this arm_pmu has long
342 * event counter support.
344 static bool armv8pmu_has_long_event(struct arm_pmu *cpu_pmu)
346 return (cpu_pmu->pmuver >= ID_AA64DFR0_PMUVER_8_5);
350 * We must chain two programmable counters for 64 bit events,
351 * except when we have allocated the 64bit cycle counter (for CPU
352 * cycles event). This must be called only when the event has
353 * a counter allocated.
355 static inline bool armv8pmu_event_is_chained(struct perf_event *event)
357 int idx = event->hw.idx;
358 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
360 return !WARN_ON(idx < 0) &&
361 armv8pmu_event_is_64bit(event) &&
362 !armv8pmu_has_long_event(cpu_pmu) &&
363 (idx != ARMV8_IDX_CYCLE_COUNTER);
367 * ARMv8 low level PMU access
371 * Perf Event to low level counters mapping
373 #define ARMV8_IDX_TO_COUNTER(x) \
374 (((x) - ARMV8_IDX_COUNTER0) & ARMV8_PMU_COUNTER_MASK)
377 * This code is really good
380 #define PMEVN_CASE(n, case_macro) \
381 case n: case_macro(n); break
383 #define PMEVN_SWITCH(x, case_macro) \
384 do { \
385 switch (x) { \
386 PMEVN_CASE(0, case_macro); \
387 PMEVN_CASE(1, case_macro); \
388 PMEVN_CASE(2, case_macro); \
389 PMEVN_CASE(3, case_macro); \
390 PMEVN_CASE(4, case_macro); \
391 PMEVN_CASE(5, case_macro); \
392 PMEVN_CASE(6, case_macro); \
393 PMEVN_CASE(7, case_macro); \
394 PMEVN_CASE(8, case_macro); \
395 PMEVN_CASE(9, case_macro); \
396 PMEVN_CASE(10, case_macro); \
397 PMEVN_CASE(11, case_macro); \
398 PMEVN_CASE(12, case_macro); \
399 PMEVN_CASE(13, case_macro); \
400 PMEVN_CASE(14, case_macro); \
401 PMEVN_CASE(15, case_macro); \
402 PMEVN_CASE(16, case_macro); \
403 PMEVN_CASE(17, case_macro); \
404 PMEVN_CASE(18, case_macro); \
405 PMEVN_CASE(19, case_macro); \
406 PMEVN_CASE(20, case_macro); \
407 PMEVN_CASE(21, case_macro); \
408 PMEVN_CASE(22, case_macro); \
409 PMEVN_CASE(23, case_macro); \
410 PMEVN_CASE(24, case_macro); \
411 PMEVN_CASE(25, case_macro); \
412 PMEVN_CASE(26, case_macro); \
413 PMEVN_CASE(27, case_macro); \
414 PMEVN_CASE(28, case_macro); \
415 PMEVN_CASE(29, case_macro); \
416 PMEVN_CASE(30, case_macro); \
417 default: WARN(1, "Invalid PMEV* index\n"); \
419 } while (0)
421 #define RETURN_READ_PMEVCNTRN(n) \
422 return read_sysreg(pmevcntr##n##_el0)
423 static unsigned long read_pmevcntrn(int n)
425 PMEVN_SWITCH(n, RETURN_READ_PMEVCNTRN);
426 return 0;
429 #define WRITE_PMEVCNTRN(n) \
430 write_sysreg(val, pmevcntr##n##_el0)
431 static void write_pmevcntrn(int n, unsigned long val)
433 PMEVN_SWITCH(n, WRITE_PMEVCNTRN);
436 #define WRITE_PMEVTYPERN(n) \
437 write_sysreg(val, pmevtyper##n##_el0)
438 static void write_pmevtypern(int n, unsigned long val)
440 PMEVN_SWITCH(n, WRITE_PMEVTYPERN);
443 static inline u32 armv8pmu_pmcr_read(void)
445 return read_sysreg(pmcr_el0);
448 static inline void armv8pmu_pmcr_write(u32 val)
450 val &= ARMV8_PMU_PMCR_MASK;
451 isb();
452 write_sysreg(val, pmcr_el0);
455 static inline int armv8pmu_has_overflowed(u32 pmovsr)
457 return pmovsr & ARMV8_PMU_OVERFLOWED_MASK;
460 static inline int armv8pmu_counter_has_overflowed(u32 pmnc, int idx)
462 return pmnc & BIT(ARMV8_IDX_TO_COUNTER(idx));
465 static inline u32 armv8pmu_read_evcntr(int idx)
467 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
469 return read_pmevcntrn(counter);
472 static inline u64 armv8pmu_read_hw_counter(struct perf_event *event)
474 int idx = event->hw.idx;
475 u64 val = 0;
477 val = armv8pmu_read_evcntr(idx);
478 if (armv8pmu_event_is_chained(event))
479 val = (val << 32) | armv8pmu_read_evcntr(idx - 1);
480 return val;
484 * The cycle counter is always a 64-bit counter. When ARMV8_PMU_PMCR_LP
485 * is set the event counters also become 64-bit counters. Unless the
486 * user has requested a long counter (attr.config1) then we want to
487 * interrupt upon 32-bit overflow - we achieve this by applying a bias.
489 static bool armv8pmu_event_needs_bias(struct perf_event *event)
491 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
492 struct hw_perf_event *hwc = &event->hw;
493 int idx = hwc->idx;
495 if (armv8pmu_event_is_64bit(event))
496 return false;
498 if (armv8pmu_has_long_event(cpu_pmu) ||
499 idx == ARMV8_IDX_CYCLE_COUNTER)
500 return true;
502 return false;
505 static u64 armv8pmu_bias_long_counter(struct perf_event *event, u64 value)
507 if (armv8pmu_event_needs_bias(event))
508 value |= GENMASK(63, 32);
510 return value;
513 static u64 armv8pmu_unbias_long_counter(struct perf_event *event, u64 value)
515 if (armv8pmu_event_needs_bias(event))
516 value &= ~GENMASK(63, 32);
518 return value;
521 static u64 armv8pmu_read_counter(struct perf_event *event)
523 struct hw_perf_event *hwc = &event->hw;
524 int idx = hwc->idx;
525 u64 value = 0;
527 if (idx == ARMV8_IDX_CYCLE_COUNTER)
528 value = read_sysreg(pmccntr_el0);
529 else
530 value = armv8pmu_read_hw_counter(event);
532 return armv8pmu_unbias_long_counter(event, value);
535 static inline void armv8pmu_write_evcntr(int idx, u64 value)
537 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
539 write_pmevcntrn(counter, value);
542 static inline void armv8pmu_write_hw_counter(struct perf_event *event,
543 u64 value)
545 int idx = event->hw.idx;
547 if (armv8pmu_event_is_chained(event)) {
548 armv8pmu_write_evcntr(idx, upper_32_bits(value));
549 armv8pmu_write_evcntr(idx - 1, lower_32_bits(value));
550 } else {
551 armv8pmu_write_evcntr(idx, value);
555 static void armv8pmu_write_counter(struct perf_event *event, u64 value)
557 struct hw_perf_event *hwc = &event->hw;
558 int idx = hwc->idx;
560 value = armv8pmu_bias_long_counter(event, value);
562 if (idx == ARMV8_IDX_CYCLE_COUNTER)
563 write_sysreg(value, pmccntr_el0);
564 else
565 armv8pmu_write_hw_counter(event, value);
568 static inline void armv8pmu_write_evtype(int idx, u32 val)
570 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
572 val &= ARMV8_PMU_EVTYPE_MASK;
573 write_pmevtypern(counter, val);
576 static inline void armv8pmu_write_event_type(struct perf_event *event)
578 struct hw_perf_event *hwc = &event->hw;
579 int idx = hwc->idx;
582 * For chained events, the low counter is programmed to count
583 * the event of interest and the high counter is programmed
584 * with CHAIN event code with filters set to count at all ELs.
586 if (armv8pmu_event_is_chained(event)) {
587 u32 chain_evt = ARMV8_PMUV3_PERFCTR_CHAIN |
588 ARMV8_PMU_INCLUDE_EL2;
590 armv8pmu_write_evtype(idx - 1, hwc->config_base);
591 armv8pmu_write_evtype(idx, chain_evt);
592 } else {
593 if (idx == ARMV8_IDX_CYCLE_COUNTER)
594 write_sysreg(hwc->config_base, pmccfiltr_el0);
595 else
596 armv8pmu_write_evtype(idx, hwc->config_base);
600 static u32 armv8pmu_event_cnten_mask(struct perf_event *event)
602 int counter = ARMV8_IDX_TO_COUNTER(event->hw.idx);
603 u32 mask = BIT(counter);
605 if (armv8pmu_event_is_chained(event))
606 mask |= BIT(counter - 1);
607 return mask;
610 static inline void armv8pmu_enable_counter(u32 mask)
613 * Make sure event configuration register writes are visible before we
614 * enable the counter.
615 * */
616 isb();
617 write_sysreg(mask, pmcntenset_el0);
620 static inline void armv8pmu_enable_event_counter(struct perf_event *event)
622 struct perf_event_attr *attr = &event->attr;
623 u32 mask = armv8pmu_event_cnten_mask(event);
625 kvm_set_pmu_events(mask, attr);
627 /* We rely on the hypervisor switch code to enable guest counters */
628 if (!kvm_pmu_counter_deferred(attr))
629 armv8pmu_enable_counter(mask);
632 static inline void armv8pmu_disable_counter(u32 mask)
634 write_sysreg(mask, pmcntenclr_el0);
636 * Make sure the effects of disabling the counter are visible before we
637 * start configuring the event.
639 isb();
642 static inline void armv8pmu_disable_event_counter(struct perf_event *event)
644 struct perf_event_attr *attr = &event->attr;
645 u32 mask = armv8pmu_event_cnten_mask(event);
647 kvm_clr_pmu_events(mask);
649 /* We rely on the hypervisor switch code to disable guest counters */
650 if (!kvm_pmu_counter_deferred(attr))
651 armv8pmu_disable_counter(mask);
654 static inline void armv8pmu_enable_intens(u32 mask)
656 write_sysreg(mask, pmintenset_el1);
659 static inline void armv8pmu_enable_event_irq(struct perf_event *event)
661 u32 counter = ARMV8_IDX_TO_COUNTER(event->hw.idx);
662 armv8pmu_enable_intens(BIT(counter));
665 static inline void armv8pmu_disable_intens(u32 mask)
667 write_sysreg(mask, pmintenclr_el1);
668 isb();
669 /* Clear the overflow flag in case an interrupt is pending. */
670 write_sysreg(mask, pmovsclr_el0);
671 isb();
674 static inline void armv8pmu_disable_event_irq(struct perf_event *event)
676 u32 counter = ARMV8_IDX_TO_COUNTER(event->hw.idx);
677 armv8pmu_disable_intens(BIT(counter));
680 static inline u32 armv8pmu_getreset_flags(void)
682 u32 value;
684 /* Read */
685 value = read_sysreg(pmovsclr_el0);
687 /* Write to clear flags */
688 value &= ARMV8_PMU_OVSR_MASK;
689 write_sysreg(value, pmovsclr_el0);
691 return value;
694 static void armv8pmu_enable_event(struct perf_event *event)
697 * Enable counter and interrupt, and set the counter to count
698 * the event that we're interested in.
702 * Disable counter
704 armv8pmu_disable_event_counter(event);
707 * Set event.
709 armv8pmu_write_event_type(event);
712 * Enable interrupt for this counter
714 armv8pmu_enable_event_irq(event);
717 * Enable counter
719 armv8pmu_enable_event_counter(event);
722 static void armv8pmu_disable_event(struct perf_event *event)
725 * Disable counter
727 armv8pmu_disable_event_counter(event);
730 * Disable interrupt for this counter
732 armv8pmu_disable_event_irq(event);
735 static void armv8pmu_start(struct arm_pmu *cpu_pmu)
737 /* Enable all counters */
738 armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMU_PMCR_E);
741 static void armv8pmu_stop(struct arm_pmu *cpu_pmu)
743 /* Disable all counters */
744 armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMU_PMCR_E);
747 static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
749 u32 pmovsr;
750 struct perf_sample_data data;
751 struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
752 struct pt_regs *regs;
753 int idx;
756 * Get and reset the IRQ flags
758 pmovsr = armv8pmu_getreset_flags();
761 * Did an overflow occur?
763 if (!armv8pmu_has_overflowed(pmovsr))
764 return IRQ_NONE;
767 * Handle the counter(s) overflow(s)
769 regs = get_irq_regs();
772 * Stop the PMU while processing the counter overflows
773 * to prevent skews in group events.
775 armv8pmu_stop(cpu_pmu);
776 for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
777 struct perf_event *event = cpuc->events[idx];
778 struct hw_perf_event *hwc;
780 /* Ignore if we don't have an event. */
781 if (!event)
782 continue;
785 * We have a single interrupt for all counters. Check that
786 * each counter has overflowed before we process it.
788 if (!armv8pmu_counter_has_overflowed(pmovsr, idx))
789 continue;
791 hwc = &event->hw;
792 armpmu_event_update(event);
793 perf_sample_data_init(&data, 0, hwc->last_period);
794 if (!armpmu_event_set_period(event))
795 continue;
798 * Perf event overflow will queue the processing of the event as
799 * an irq_work which will be taken care of in the handling of
800 * IPI_IRQ_WORK.
802 if (perf_event_overflow(event, &data, regs))
803 cpu_pmu->disable(event);
805 armv8pmu_start(cpu_pmu);
807 return IRQ_HANDLED;
810 static int armv8pmu_get_single_idx(struct pmu_hw_events *cpuc,
811 struct arm_pmu *cpu_pmu)
813 int idx;
815 for (idx = ARMV8_IDX_COUNTER0; idx < cpu_pmu->num_events; idx ++) {
816 if (!test_and_set_bit(idx, cpuc->used_mask))
817 return idx;
819 return -EAGAIN;
822 static int armv8pmu_get_chain_idx(struct pmu_hw_events *cpuc,
823 struct arm_pmu *cpu_pmu)
825 int idx;
828 * Chaining requires two consecutive event counters, where
829 * the lower idx must be even.
831 for (idx = ARMV8_IDX_COUNTER0 + 1; idx < cpu_pmu->num_events; idx += 2) {
832 if (!test_and_set_bit(idx, cpuc->used_mask)) {
833 /* Check if the preceding even counter is available */
834 if (!test_and_set_bit(idx - 1, cpuc->used_mask))
835 return idx;
836 /* Release the Odd counter */
837 clear_bit(idx, cpuc->used_mask);
840 return -EAGAIN;
843 static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
844 struct perf_event *event)
846 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
847 struct hw_perf_event *hwc = &event->hw;
848 unsigned long evtype = hwc->config_base & ARMV8_PMU_EVTYPE_EVENT;
850 /* Always prefer to place a cycle counter into the cycle counter. */
851 if (evtype == ARMV8_PMUV3_PERFCTR_CPU_CYCLES) {
852 if (!test_and_set_bit(ARMV8_IDX_CYCLE_COUNTER, cpuc->used_mask))
853 return ARMV8_IDX_CYCLE_COUNTER;
857 * Otherwise use events counters
859 if (armv8pmu_event_is_64bit(event) &&
860 !armv8pmu_has_long_event(cpu_pmu))
861 return armv8pmu_get_chain_idx(cpuc, cpu_pmu);
862 else
863 return armv8pmu_get_single_idx(cpuc, cpu_pmu);
866 static void armv8pmu_clear_event_idx(struct pmu_hw_events *cpuc,
867 struct perf_event *event)
869 int idx = event->hw.idx;
871 clear_bit(idx, cpuc->used_mask);
872 if (armv8pmu_event_is_chained(event))
873 clear_bit(idx - 1, cpuc->used_mask);
877 * Add an event filter to a given event.
879 static int armv8pmu_set_event_filter(struct hw_perf_event *event,
880 struct perf_event_attr *attr)
882 unsigned long config_base = 0;
884 if (attr->exclude_idle)
885 return -EPERM;
888 * If we're running in hyp mode, then we *are* the hypervisor.
889 * Therefore we ignore exclude_hv in this configuration, since
890 * there's no hypervisor to sample anyway. This is consistent
891 * with other architectures (x86 and Power).
893 if (is_kernel_in_hyp_mode()) {
894 if (!attr->exclude_kernel && !attr->exclude_host)
895 config_base |= ARMV8_PMU_INCLUDE_EL2;
896 if (attr->exclude_guest)
897 config_base |= ARMV8_PMU_EXCLUDE_EL1;
898 if (attr->exclude_host)
899 config_base |= ARMV8_PMU_EXCLUDE_EL0;
900 } else {
901 if (!attr->exclude_hv && !attr->exclude_host)
902 config_base |= ARMV8_PMU_INCLUDE_EL2;
906 * Filter out !VHE kernels and guest kernels
908 if (attr->exclude_kernel)
909 config_base |= ARMV8_PMU_EXCLUDE_EL1;
911 if (attr->exclude_user)
912 config_base |= ARMV8_PMU_EXCLUDE_EL0;
915 * Install the filter into config_base as this is used to
916 * construct the event type.
918 event->config_base = config_base;
920 return 0;
923 static int armv8pmu_filter_match(struct perf_event *event)
925 unsigned long evtype = event->hw.config_base & ARMV8_PMU_EVTYPE_EVENT;
926 return evtype != ARMV8_PMUV3_PERFCTR_CHAIN;
929 static void armv8pmu_reset(void *info)
931 struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
932 u32 pmcr;
934 /* The counter and interrupt enable registers are unknown at reset. */
935 armv8pmu_disable_counter(U32_MAX);
936 armv8pmu_disable_intens(U32_MAX);
938 /* Clear the counters we flip at guest entry/exit */
939 kvm_clr_pmu_events(U32_MAX);
942 * Initialize & Reset PMNC. Request overflow interrupt for
943 * 64 bit cycle counter but cheat in armv8pmu_write_counter().
945 pmcr = ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C | ARMV8_PMU_PMCR_LC;
947 /* Enable long event counter support where available */
948 if (armv8pmu_has_long_event(cpu_pmu))
949 pmcr |= ARMV8_PMU_PMCR_LP;
951 armv8pmu_pmcr_write(pmcr);
954 static int __armv8_pmuv3_map_event(struct perf_event *event,
955 const unsigned (*extra_event_map)
956 [PERF_COUNT_HW_MAX],
957 const unsigned (*extra_cache_map)
958 [PERF_COUNT_HW_CACHE_MAX]
959 [PERF_COUNT_HW_CACHE_OP_MAX]
960 [PERF_COUNT_HW_CACHE_RESULT_MAX])
962 int hw_event_id;
963 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
965 hw_event_id = armpmu_map_event(event, &armv8_pmuv3_perf_map,
966 &armv8_pmuv3_perf_cache_map,
967 ARMV8_PMU_EVTYPE_EVENT);
969 if (armv8pmu_event_is_64bit(event))
970 event->hw.flags |= ARMPMU_EVT_64BIT;
972 /* Only expose micro/arch events supported by this PMU */
973 if ((hw_event_id > 0) && (hw_event_id < ARMV8_PMUV3_MAX_COMMON_EVENTS)
974 && test_bit(hw_event_id, armpmu->pmceid_bitmap)) {
975 return hw_event_id;
978 return armpmu_map_event(event, extra_event_map, extra_cache_map,
979 ARMV8_PMU_EVTYPE_EVENT);
982 static int armv8_pmuv3_map_event(struct perf_event *event)
984 return __armv8_pmuv3_map_event(event, NULL, NULL);
987 static int armv8_a53_map_event(struct perf_event *event)
989 return __armv8_pmuv3_map_event(event, NULL, &armv8_a53_perf_cache_map);
992 static int armv8_a57_map_event(struct perf_event *event)
994 return __armv8_pmuv3_map_event(event, NULL, &armv8_a57_perf_cache_map);
997 static int armv8_a73_map_event(struct perf_event *event)
999 return __armv8_pmuv3_map_event(event, NULL, &armv8_a73_perf_cache_map);
1002 static int armv8_thunder_map_event(struct perf_event *event)
1004 return __armv8_pmuv3_map_event(event, NULL,
1005 &armv8_thunder_perf_cache_map);
1008 static int armv8_vulcan_map_event(struct perf_event *event)
1010 return __armv8_pmuv3_map_event(event, NULL,
1011 &armv8_vulcan_perf_cache_map);
1014 struct armv8pmu_probe_info {
1015 struct arm_pmu *pmu;
1016 bool present;
1019 static void __armv8pmu_probe_pmu(void *info)
1021 struct armv8pmu_probe_info *probe = info;
1022 struct arm_pmu *cpu_pmu = probe->pmu;
1023 u64 dfr0;
1024 u64 pmceid_raw[2];
1025 u32 pmceid[2];
1026 int pmuver;
1028 dfr0 = read_sysreg(id_aa64dfr0_el1);
1029 pmuver = cpuid_feature_extract_unsigned_field(dfr0,
1030 ID_AA64DFR0_PMUVER_SHIFT);
1031 if (pmuver == 0xf || pmuver == 0)
1032 return;
1034 cpu_pmu->pmuver = pmuver;
1035 probe->present = true;
1037 /* Read the nb of CNTx counters supported from PMNC */
1038 cpu_pmu->num_events = (armv8pmu_pmcr_read() >> ARMV8_PMU_PMCR_N_SHIFT)
1039 & ARMV8_PMU_PMCR_N_MASK;
1041 /* Add the CPU cycles counter */
1042 cpu_pmu->num_events += 1;
1044 pmceid[0] = pmceid_raw[0] = read_sysreg(pmceid0_el0);
1045 pmceid[1] = pmceid_raw[1] = read_sysreg(pmceid1_el0);
1047 bitmap_from_arr32(cpu_pmu->pmceid_bitmap,
1048 pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
1050 pmceid[0] = pmceid_raw[0] >> 32;
1051 pmceid[1] = pmceid_raw[1] >> 32;
1053 bitmap_from_arr32(cpu_pmu->pmceid_ext_bitmap,
1054 pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
1056 /* store PMMIR_EL1 register for sysfs */
1057 if (pmuver >= ID_AA64DFR0_PMUVER_8_4 && (pmceid_raw[1] & BIT(31)))
1058 cpu_pmu->reg_pmmir = read_cpuid(PMMIR_EL1);
1059 else
1060 cpu_pmu->reg_pmmir = 0;
1063 static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
1065 struct armv8pmu_probe_info probe = {
1066 .pmu = cpu_pmu,
1067 .present = false,
1069 int ret;
1071 ret = smp_call_function_any(&cpu_pmu->supported_cpus,
1072 __armv8pmu_probe_pmu,
1073 &probe, 1);
1074 if (ret)
1075 return ret;
1077 return probe.present ? 0 : -ENODEV;
1080 static int armv8_pmu_init(struct arm_pmu *cpu_pmu, char *name,
1081 int (*map_event)(struct perf_event *event),
1082 const struct attribute_group *events,
1083 const struct attribute_group *format,
1084 const struct attribute_group *caps)
1086 int ret = armv8pmu_probe_pmu(cpu_pmu);
1087 if (ret)
1088 return ret;
1090 cpu_pmu->handle_irq = armv8pmu_handle_irq;
1091 cpu_pmu->enable = armv8pmu_enable_event;
1092 cpu_pmu->disable = armv8pmu_disable_event;
1093 cpu_pmu->read_counter = armv8pmu_read_counter;
1094 cpu_pmu->write_counter = armv8pmu_write_counter;
1095 cpu_pmu->get_event_idx = armv8pmu_get_event_idx;
1096 cpu_pmu->clear_event_idx = armv8pmu_clear_event_idx;
1097 cpu_pmu->start = armv8pmu_start;
1098 cpu_pmu->stop = armv8pmu_stop;
1099 cpu_pmu->reset = armv8pmu_reset;
1100 cpu_pmu->set_event_filter = armv8pmu_set_event_filter;
1101 cpu_pmu->filter_match = armv8pmu_filter_match;
1103 cpu_pmu->name = name;
1104 cpu_pmu->map_event = map_event;
1105 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] = events ?
1106 events : &armv8_pmuv3_events_attr_group;
1107 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] = format ?
1108 format : &armv8_pmuv3_format_attr_group;
1109 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_CAPS] = caps ?
1110 caps : &armv8_pmuv3_caps_attr_group;
1112 return 0;
1115 static int armv8_pmu_init_nogroups(struct arm_pmu *cpu_pmu, char *name,
1116 int (*map_event)(struct perf_event *event))
1118 return armv8_pmu_init(cpu_pmu, name, map_event, NULL, NULL, NULL);
1121 static int armv8_pmuv3_init(struct arm_pmu *cpu_pmu)
1123 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_pmuv3",
1124 armv8_pmuv3_map_event);
1127 static int armv8_a34_pmu_init(struct arm_pmu *cpu_pmu)
1129 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a34",
1130 armv8_pmuv3_map_event);
1133 static int armv8_a35_pmu_init(struct arm_pmu *cpu_pmu)
1135 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a35",
1136 armv8_a53_map_event);
1139 static int armv8_a53_pmu_init(struct arm_pmu *cpu_pmu)
1141 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a53",
1142 armv8_a53_map_event);
1145 static int armv8_a55_pmu_init(struct arm_pmu *cpu_pmu)
1147 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a55",
1148 armv8_pmuv3_map_event);
1151 static int armv8_a57_pmu_init(struct arm_pmu *cpu_pmu)
1153 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a57",
1154 armv8_a57_map_event);
1157 static int armv8_a65_pmu_init(struct arm_pmu *cpu_pmu)
1159 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a65",
1160 armv8_pmuv3_map_event);
1163 static int armv8_a72_pmu_init(struct arm_pmu *cpu_pmu)
1165 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a72",
1166 armv8_a57_map_event);
1169 static int armv8_a73_pmu_init(struct arm_pmu *cpu_pmu)
1171 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a73",
1172 armv8_a73_map_event);
1175 static int armv8_a75_pmu_init(struct arm_pmu *cpu_pmu)
1177 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a75",
1178 armv8_pmuv3_map_event);
1181 static int armv8_a76_pmu_init(struct arm_pmu *cpu_pmu)
1183 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a76",
1184 armv8_pmuv3_map_event);
1187 static int armv8_a77_pmu_init(struct arm_pmu *cpu_pmu)
1189 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cortex_a77",
1190 armv8_pmuv3_map_event);
1193 static int armv8_e1_pmu_init(struct arm_pmu *cpu_pmu)
1195 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_neoverse_e1",
1196 armv8_pmuv3_map_event);
1199 static int armv8_n1_pmu_init(struct arm_pmu *cpu_pmu)
1201 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_neoverse_n1",
1202 armv8_pmuv3_map_event);
1205 static int armv8_thunder_pmu_init(struct arm_pmu *cpu_pmu)
1207 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_cavium_thunder",
1208 armv8_thunder_map_event);
1211 static int armv8_vulcan_pmu_init(struct arm_pmu *cpu_pmu)
1213 return armv8_pmu_init_nogroups(cpu_pmu, "armv8_brcm_vulcan",
1214 armv8_vulcan_map_event);
1217 static const struct of_device_id armv8_pmu_of_device_ids[] = {
1218 {.compatible = "arm,armv8-pmuv3", .data = armv8_pmuv3_init},
1219 {.compatible = "arm,cortex-a34-pmu", .data = armv8_a34_pmu_init},
1220 {.compatible = "arm,cortex-a35-pmu", .data = armv8_a35_pmu_init},
1221 {.compatible = "arm,cortex-a53-pmu", .data = armv8_a53_pmu_init},
1222 {.compatible = "arm,cortex-a55-pmu", .data = armv8_a55_pmu_init},
1223 {.compatible = "arm,cortex-a57-pmu", .data = armv8_a57_pmu_init},
1224 {.compatible = "arm,cortex-a65-pmu", .data = armv8_a65_pmu_init},
1225 {.compatible = "arm,cortex-a72-pmu", .data = armv8_a72_pmu_init},
1226 {.compatible = "arm,cortex-a73-pmu", .data = armv8_a73_pmu_init},
1227 {.compatible = "arm,cortex-a75-pmu", .data = armv8_a75_pmu_init},
1228 {.compatible = "arm,cortex-a76-pmu", .data = armv8_a76_pmu_init},
1229 {.compatible = "arm,cortex-a77-pmu", .data = armv8_a77_pmu_init},
1230 {.compatible = "arm,neoverse-e1-pmu", .data = armv8_e1_pmu_init},
1231 {.compatible = "arm,neoverse-n1-pmu", .data = armv8_n1_pmu_init},
1232 {.compatible = "cavium,thunder-pmu", .data = armv8_thunder_pmu_init},
1233 {.compatible = "brcm,vulcan-pmu", .data = armv8_vulcan_pmu_init},
1237 static int armv8_pmu_device_probe(struct platform_device *pdev)
1239 return arm_pmu_device_probe(pdev, armv8_pmu_of_device_ids, NULL);
1242 static struct platform_driver armv8_pmu_driver = {
1243 .driver = {
1244 .name = ARMV8_PMU_PDEV_NAME,
1245 .of_match_table = armv8_pmu_of_device_ids,
1246 .suppress_bind_attrs = true,
1248 .probe = armv8_pmu_device_probe,
1251 static int __init armv8_pmu_driver_init(void)
1253 int ret;
1255 if (acpi_disabled)
1256 ret = platform_driver_register(&armv8_pmu_driver);
1257 else
1258 ret = arm_pmu_acpi_probe(armv8_pmuv3_init);
1261 * Try to re-initialize lockup detector after PMU init in
1262 * case PMU events are triggered via NMIs.
1264 if (ret == 0 && arm_pmu_irq_is_nmi())
1265 lockup_detector_init();
1267 return ret;
1269 device_initcall(armv8_pmu_driver_init)
1271 void arch_perf_update_userpage(struct perf_event *event,
1272 struct perf_event_mmap_page *userpg, u64 now)
1274 struct clock_read_data *rd;
1275 unsigned int seq;
1276 u64 ns;
1278 userpg->cap_user_time = 0;
1279 userpg->cap_user_time_zero = 0;
1280 userpg->cap_user_time_short = 0;
1282 do {
1283 rd = sched_clock_read_begin(&seq);
1285 if (rd->read_sched_clock != arch_timer_read_counter)
1286 return;
1288 userpg->time_mult = rd->mult;
1289 userpg->time_shift = rd->shift;
1290 userpg->time_zero = rd->epoch_ns;
1291 userpg->time_cycles = rd->epoch_cyc;
1292 userpg->time_mask = rd->sched_clock_mask;
1295 * Subtract the cycle base, such that software that
1296 * doesn't know about cap_user_time_short still 'works'
1297 * assuming no wraps.
1299 ns = mul_u64_u32_shr(rd->epoch_cyc, rd->mult, rd->shift);
1300 userpg->time_zero -= ns;
1302 } while (sched_clock_read_retry(seq));
1304 userpg->time_offset = userpg->time_zero - now;
1307 * time_shift is not expected to be greater than 31 due to
1308 * the original published conversion algorithm shifting a
1309 * 32-bit value (now specifies a 64-bit value) - refer
1310 * perf_event_mmap_page documentation in perf_event.h.
1312 if (userpg->time_shift == 32) {
1313 userpg->time_shift = 31;
1314 userpg->time_mult >>= 1;
1318 * Internal timekeeping for enabled/running/stopped times
1319 * is always computed with the sched_clock.
1321 userpg->cap_user_time = 1;
1322 userpg->cap_user_time_zero = 1;
1323 userpg->cap_user_time_short = 1;
1326 #ifdef CONFIG_HARDLOCKUP_DETECTOR_PERF
1328 * Safe maximum CPU frequency in case a particular platform doesn't implement
1329 * cpufreq driver. Although, architecture doesn't put any restrictions on
1330 * maximum frequency but 5 GHz seems to be safe maximum given the available
1331 * Arm CPUs in the market which are clocked much less than 5 GHz. On the other
1332 * hand, we can't make it much higher as it would lead to a large hard-lockup
1333 * detection timeout on parts which are running slower (eg. 1GHz on
1334 * Developerbox) and doesn't possess a cpufreq driver.
1336 #define SAFE_MAX_CPU_FREQ 5000000000UL // 5 GHz
1337 u64 hw_nmi_get_sample_period(int watchdog_thresh)
1339 unsigned int cpu = smp_processor_id();
1340 unsigned long max_cpu_freq;
1342 max_cpu_freq = cpufreq_get_hw_max_freq(cpu) * 1000UL;
1343 if (!max_cpu_freq)
1344 max_cpu_freq = SAFE_MAX_CPU_FREQ;
1346 return (u64)max_cpu_freq * watchdog_thresh;
1348 #endif