blob: a0b4f1bca4917e0fea04beac112e4814de1980ec [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Will Deacon03089682012-03-05 11:49:32 +00002/*
Will Deacon4b47e572018-10-05 13:31:10 +01003 * ARMv8 PMUv3 Performance Events handling code.
Will Deacon03089682012-03-05 11:49:32 +00004 *
5 * Copyright (C) 2012 ARM Limited
6 * Author: Will Deacon <will.deacon@arm.com>
7 *
8 * This code is based heavily on the ARMv7 perf event code.
Will Deacon03089682012-03-05 11:49:32 +00009 */
Will Deacon03089682012-03-05 11:49:32 +000010
Will Deacon03089682012-03-05 11:49:32 +000011#include <asm/irq_regs.h>
Shannon Zhaob8cfadf2016-03-24 16:01:16 +000012#include <asm/perf_event.h>
Ashok Kumarbf2d4782016-04-21 05:58:43 -070013#include <asm/sysreg.h>
Marc Zyngierd98ecda2016-01-25 17:31:13 +000014#include <asm/virt.h>
Will Deacon03089682012-03-05 11:49:32 +000015
Mark Salterdbee3a72016-09-14 17:32:29 -050016#include <linux/acpi.h>
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -070017#include <linux/clocksource.h>
Andrew Murrayd1947bc2019-04-09 20:22:13 +010018#include <linux/kvm_host.h>
Mark Rutland6475b2d2015-10-02 10:55:03 +010019#include <linux/of.h>
20#include <linux/perf/arm_pmu.h>
21#include <linux/platform_device.h>
Raphael Gaultd91cc2f2019-08-20 16:57:45 +010022#include <linux/smp.h>
Will Deacon03089682012-03-05 11:49:32 +000023
Mark Rutlandac82d122015-10-02 10:55:04 +010024/* ARMv8 Cortex-A53 specific event types. */
Ashok Kumar03598fd2016-04-21 05:58:41 -070025#define ARMV8_A53_PERFCTR_PREF_LINEFILL 0xC2
Mark Rutlandac82d122015-10-02 10:55:04 +010026
Jan Glauberd0aa2bf2016-02-18 17:50:11 +010027/* ARMv8 Cavium ThunderX specific event types. */
Ashok Kumar03598fd2016-04-21 05:58:41 -070028#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST 0xE9
29#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS 0xEA
30#define ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS 0xEB
31#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS 0xEC
32#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS 0xED
Mark Rutland62a4dda2015-10-02 10:55:05 +010033
Jeremy Linton236b9b912016-09-14 17:32:30 -050034/*
35 * ARMv8 Architectural defined events, not all of these may
Will Deacon342e53b2018-10-05 13:28:07 +010036 * be supported on any given implementation. Unsupported events will
37 * be disabled at run-time based on the PMCEID registers.
Jeremy Linton236b9b912016-09-14 17:32:30 -050038 */
Will Deacon03089682012-03-05 11:49:32 +000039static const unsigned armv8_pmuv3_perf_map[PERF_COUNT_HW_MAX] = {
Mark Rutlandae2fb7e2015-07-21 11:36:39 +010040 PERF_MAP_ALL_UNSUPPORTED,
Ashok Kumar03598fd2016-04-21 05:58:41 -070041 [PERF_COUNT_HW_CPU_CYCLES] = ARMV8_PMUV3_PERFCTR_CPU_CYCLES,
42 [PERF_COUNT_HW_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_INST_RETIRED,
43 [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
44 [PERF_COUNT_HW_CACHE_MISSES] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
Jeremy Linton236b9b912016-09-14 17:32:30 -050045 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED,
Ashok Kumar03598fd2016-04-21 05:58:41 -070046 [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Jeremy Linton236b9b912016-09-14 17:32:30 -050047 [PERF_COUNT_HW_BUS_CYCLES] = ARMV8_PMUV3_PERFCTR_BUS_CYCLES,
48 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV8_PMUV3_PERFCTR_STALL_FRONTEND,
49 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV8_PMUV3_PERFCTR_STALL_BACKEND,
Will Deacon03089682012-03-05 11:49:32 +000050};
51
52static const unsigned armv8_pmuv3_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
53 [PERF_COUNT_HW_CACHE_OP_MAX]
54 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
Mark Rutlandae2fb7e2015-07-21 11:36:39 +010055 PERF_CACHE_MAP_ALL_UNSUPPORTED,
56
Ashok Kumar03598fd2016-04-21 05:58:41 -070057 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE,
58 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL,
Mark Rutlandae2fb7e2015-07-21 11:36:39 +010059
Jeremy Linton236b9b912016-09-14 17:32:30 -050060 [C(L1I)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE,
61 [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL,
62
63 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL,
64 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1D_TLB,
65
66 [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL,
67 [C(ITLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1I_TLB,
68
Ashok Kumar03598fd2016-04-21 05:58:41 -070069 [C(BPU)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_BR_PRED,
70 [C(BPU)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_BR_MIS_PRED,
Will Deacon03089682012-03-05 11:49:32 +000071};
72
Mark Rutlandac82d122015-10-02 10:55:04 +010073static const unsigned armv8_a53_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
74 [PERF_COUNT_HW_CACHE_OP_MAX]
75 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
76 PERF_CACHE_MAP_ALL_UNSUPPORTED,
77
Ashok Kumar03598fd2016-04-21 05:58:41 -070078 [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_A53_PERFCTR_PREF_LINEFILL,
Mark Rutlandac82d122015-10-02 10:55:04 +010079
Julien Thierry5cf7fb22017-07-25 17:27:36 +010080 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
81 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
Mark Rutlandac82d122015-10-02 10:55:04 +010082};
83
Mark Rutland62a4dda2015-10-02 10:55:05 +010084static const unsigned armv8_a57_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
85 [PERF_COUNT_HW_CACHE_OP_MAX]
86 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
87 PERF_CACHE_MAP_ALL_UNSUPPORTED,
88
Ashok Kumar03598fd2016-04-21 05:58:41 -070089 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
90 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
91 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
92 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +010093
Ashok Kumar03598fd2016-04-21 05:58:41 -070094 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
95 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +010096
Julien Thierry5cf7fb22017-07-25 17:27:36 +010097 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
98 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +010099};
100
Julien Thierry5561b6c2017-08-09 17:46:38 +0100101static const unsigned armv8_a73_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
102 [PERF_COUNT_HW_CACHE_OP_MAX]
103 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
104 PERF_CACHE_MAP_ALL_UNSUPPORTED,
105
106 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
107 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
Julien Thierry5561b6c2017-08-09 17:46:38 +0100108};
109
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100110static const unsigned armv8_thunder_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
111 [PERF_COUNT_HW_CACHE_OP_MAX]
112 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
113 PERF_CACHE_MAP_ALL_UNSUPPORTED,
114
Ashok Kumar03598fd2016-04-21 05:58:41 -0700115 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
116 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
117 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
118 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_MISS_ST,
119 [C(L1D)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_ACCESS,
120 [C(L1D)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1D_CACHE_PREF_MISS,
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100121
Ashok Kumar03598fd2016-04-21 05:58:41 -0700122 [C(L1I)][C(OP_PREFETCH)][C(RESULT_ACCESS)] = ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS,
123 [C(L1I)][C(OP_PREFETCH)][C(RESULT_MISS)] = ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS,
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100124
Ashok Kumar03598fd2016-04-21 05:58:41 -0700125 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
126 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
127 [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
128 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
Mark Rutland62a4dda2015-10-02 10:55:05 +0100129};
130
Ashok Kumar201a72b2016-04-21 05:58:45 -0700131static const unsigned armv8_vulcan_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
132 [PERF_COUNT_HW_CACHE_OP_MAX]
133 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
134 PERF_CACHE_MAP_ALL_UNSUPPORTED,
135
136 [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_RD,
137 [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_RD,
138 [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_WR,
139 [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_CACHE_REFILL_WR,
140
Ashok Kumar201a72b2016-04-21 05:58:45 -0700141 [C(DTLB)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_RD,
142 [C(DTLB)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_WR,
143 [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_RD,
144 [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV8_IMPDEF_PERFCTR_L1D_TLB_REFILL_WR,
145
Ashok Kumar201a72b2016-04-21 05:58:45 -0700146 [C(NODE)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_RD,
147 [C(NODE)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV8_IMPDEF_PERFCTR_BUS_ACCESS_WR,
148};
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700149
150static ssize_t
151armv8pmu_events_sysfs_show(struct device *dev,
152 struct device_attribute *attr, char *page)
153{
154 struct perf_pmu_events_attr *pmu_attr;
155
156 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
157
158 return sprintf(page, "event=0x%03llx\n", pmu_attr->id);
159}
160
Drew Richardson9e9caa62015-10-22 07:07:32 -0700161#define ARMV8_EVENT_ATTR(name, config) \
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700162 PMU_EVENT_ATTR(name, armv8_event_attr_##name, \
163 config, armv8pmu_events_sysfs_show)
Drew Richardson9e9caa62015-10-22 07:07:32 -0700164
Ashok Kumar03598fd2016-04-21 05:58:41 -0700165ARMV8_EVENT_ATTR(sw_incr, ARMV8_PMUV3_PERFCTR_SW_INCR);
166ARMV8_EVENT_ATTR(l1i_cache_refill, ARMV8_PMUV3_PERFCTR_L1I_CACHE_REFILL);
167ARMV8_EVENT_ATTR(l1i_tlb_refill, ARMV8_PMUV3_PERFCTR_L1I_TLB_REFILL);
168ARMV8_EVENT_ATTR(l1d_cache_refill, ARMV8_PMUV3_PERFCTR_L1D_CACHE_REFILL);
169ARMV8_EVENT_ATTR(l1d_cache, ARMV8_PMUV3_PERFCTR_L1D_CACHE);
170ARMV8_EVENT_ATTR(l1d_tlb_refill, ARMV8_PMUV3_PERFCTR_L1D_TLB_REFILL);
171ARMV8_EVENT_ATTR(ld_retired, ARMV8_PMUV3_PERFCTR_LD_RETIRED);
172ARMV8_EVENT_ATTR(st_retired, ARMV8_PMUV3_PERFCTR_ST_RETIRED);
173ARMV8_EVENT_ATTR(inst_retired, ARMV8_PMUV3_PERFCTR_INST_RETIRED);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700174ARMV8_EVENT_ATTR(exc_taken, ARMV8_PMUV3_PERFCTR_EXC_TAKEN);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700175ARMV8_EVENT_ATTR(exc_return, ARMV8_PMUV3_PERFCTR_EXC_RETURN);
176ARMV8_EVENT_ATTR(cid_write_retired, ARMV8_PMUV3_PERFCTR_CID_WRITE_RETIRED);
177ARMV8_EVENT_ATTR(pc_write_retired, ARMV8_PMUV3_PERFCTR_PC_WRITE_RETIRED);
178ARMV8_EVENT_ATTR(br_immed_retired, ARMV8_PMUV3_PERFCTR_BR_IMMED_RETIRED);
179ARMV8_EVENT_ATTR(br_return_retired, ARMV8_PMUV3_PERFCTR_BR_RETURN_RETIRED);
180ARMV8_EVENT_ATTR(unaligned_ldst_retired, ARMV8_PMUV3_PERFCTR_UNALIGNED_LDST_RETIRED);
181ARMV8_EVENT_ATTR(br_mis_pred, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED);
182ARMV8_EVENT_ATTR(cpu_cycles, ARMV8_PMUV3_PERFCTR_CPU_CYCLES);
183ARMV8_EVENT_ATTR(br_pred, ARMV8_PMUV3_PERFCTR_BR_PRED);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700184ARMV8_EVENT_ATTR(mem_access, ARMV8_PMUV3_PERFCTR_MEM_ACCESS);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700185ARMV8_EVENT_ATTR(l1i_cache, ARMV8_PMUV3_PERFCTR_L1I_CACHE);
186ARMV8_EVENT_ATTR(l1d_cache_wb, ARMV8_PMUV3_PERFCTR_L1D_CACHE_WB);
187ARMV8_EVENT_ATTR(l2d_cache, ARMV8_PMUV3_PERFCTR_L2D_CACHE);
188ARMV8_EVENT_ATTR(l2d_cache_refill, ARMV8_PMUV3_PERFCTR_L2D_CACHE_REFILL);
189ARMV8_EVENT_ATTR(l2d_cache_wb, ARMV8_PMUV3_PERFCTR_L2D_CACHE_WB);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700190ARMV8_EVENT_ATTR(bus_access, ARMV8_PMUV3_PERFCTR_BUS_ACCESS);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700191ARMV8_EVENT_ATTR(memory_error, ARMV8_PMUV3_PERFCTR_MEMORY_ERROR);
192ARMV8_EVENT_ATTR(inst_spec, ARMV8_PMUV3_PERFCTR_INST_SPEC);
193ARMV8_EVENT_ATTR(ttbr_write_retired, ARMV8_PMUV3_PERFCTR_TTBR_WRITE_RETIRED);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700194ARMV8_EVENT_ATTR(bus_cycles, ARMV8_PMUV3_PERFCTR_BUS_CYCLES);
Will Deacon4ba25782016-04-25 15:05:24 +0100195/* Don't expose the chain event in /sys, since it's useless in isolation */
Drew Richardson9e9caa62015-10-22 07:07:32 -0700196ARMV8_EVENT_ATTR(l1d_cache_allocate, ARMV8_PMUV3_PERFCTR_L1D_CACHE_ALLOCATE);
197ARMV8_EVENT_ATTR(l2d_cache_allocate, ARMV8_PMUV3_PERFCTR_L2D_CACHE_ALLOCATE);
198ARMV8_EVENT_ATTR(br_retired, ARMV8_PMUV3_PERFCTR_BR_RETIRED);
199ARMV8_EVENT_ATTR(br_mis_pred_retired, ARMV8_PMUV3_PERFCTR_BR_MIS_PRED_RETIRED);
200ARMV8_EVENT_ATTR(stall_frontend, ARMV8_PMUV3_PERFCTR_STALL_FRONTEND);
201ARMV8_EVENT_ATTR(stall_backend, ARMV8_PMUV3_PERFCTR_STALL_BACKEND);
202ARMV8_EVENT_ATTR(l1d_tlb, ARMV8_PMUV3_PERFCTR_L1D_TLB);
203ARMV8_EVENT_ATTR(l1i_tlb, ARMV8_PMUV3_PERFCTR_L1I_TLB);
204ARMV8_EVENT_ATTR(l2i_cache, ARMV8_PMUV3_PERFCTR_L2I_CACHE);
205ARMV8_EVENT_ATTR(l2i_cache_refill, ARMV8_PMUV3_PERFCTR_L2I_CACHE_REFILL);
206ARMV8_EVENT_ATTR(l3d_cache_allocate, ARMV8_PMUV3_PERFCTR_L3D_CACHE_ALLOCATE);
207ARMV8_EVENT_ATTR(l3d_cache_refill, ARMV8_PMUV3_PERFCTR_L3D_CACHE_REFILL);
208ARMV8_EVENT_ATTR(l3d_cache, ARMV8_PMUV3_PERFCTR_L3D_CACHE);
209ARMV8_EVENT_ATTR(l3d_cache_wb, ARMV8_PMUV3_PERFCTR_L3D_CACHE_WB);
210ARMV8_EVENT_ATTR(l2d_tlb_refill, ARMV8_PMUV3_PERFCTR_L2D_TLB_REFILL);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700211ARMV8_EVENT_ATTR(l2i_tlb_refill, ARMV8_PMUV3_PERFCTR_L2I_TLB_REFILL);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700212ARMV8_EVENT_ATTR(l2d_tlb, ARMV8_PMUV3_PERFCTR_L2D_TLB);
Ashok Kumar03598fd2016-04-21 05:58:41 -0700213ARMV8_EVENT_ATTR(l2i_tlb, ARMV8_PMUV3_PERFCTR_L2I_TLB);
Will Deacon2ddd5e582018-10-05 13:33:02 +0100214ARMV8_EVENT_ATTR(remote_access, ARMV8_PMUV3_PERFCTR_REMOTE_ACCESS);
215ARMV8_EVENT_ATTR(ll_cache, ARMV8_PMUV3_PERFCTR_LL_CACHE);
216ARMV8_EVENT_ATTR(ll_cache_miss, ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS);
217ARMV8_EVENT_ATTR(dtlb_walk, ARMV8_PMUV3_PERFCTR_DTLB_WALK);
218ARMV8_EVENT_ATTR(itlb_walk, ARMV8_PMUV3_PERFCTR_ITLB_WALK);
219ARMV8_EVENT_ATTR(ll_cache_rd, ARMV8_PMUV3_PERFCTR_LL_CACHE_RD);
220ARMV8_EVENT_ATTR(ll_cache_miss_rd, ARMV8_PMUV3_PERFCTR_LL_CACHE_MISS_RD);
221ARMV8_EVENT_ATTR(remote_access_rd, ARMV8_PMUV3_PERFCTR_REMOTE_ACCESS_RD);
222ARMV8_EVENT_ATTR(sample_pop, ARMV8_SPE_PERFCTR_SAMPLE_POP);
223ARMV8_EVENT_ATTR(sample_feed, ARMV8_SPE_PERFCTR_SAMPLE_FEED);
224ARMV8_EVENT_ATTR(sample_filtrate, ARMV8_SPE_PERFCTR_SAMPLE_FILTRATE);
225ARMV8_EVENT_ATTR(sample_collision, ARMV8_SPE_PERFCTR_SAMPLE_COLLISION);
Drew Richardson9e9caa62015-10-22 07:07:32 -0700226
227static struct attribute *armv8_pmuv3_event_attrs[] = {
228 &armv8_event_attr_sw_incr.attr.attr,
229 &armv8_event_attr_l1i_cache_refill.attr.attr,
230 &armv8_event_attr_l1i_tlb_refill.attr.attr,
231 &armv8_event_attr_l1d_cache_refill.attr.attr,
232 &armv8_event_attr_l1d_cache.attr.attr,
233 &armv8_event_attr_l1d_tlb_refill.attr.attr,
234 &armv8_event_attr_ld_retired.attr.attr,
235 &armv8_event_attr_st_retired.attr.attr,
236 &armv8_event_attr_inst_retired.attr.attr,
237 &armv8_event_attr_exc_taken.attr.attr,
238 &armv8_event_attr_exc_return.attr.attr,
239 &armv8_event_attr_cid_write_retired.attr.attr,
240 &armv8_event_attr_pc_write_retired.attr.attr,
241 &armv8_event_attr_br_immed_retired.attr.attr,
242 &armv8_event_attr_br_return_retired.attr.attr,
243 &armv8_event_attr_unaligned_ldst_retired.attr.attr,
244 &armv8_event_attr_br_mis_pred.attr.attr,
245 &armv8_event_attr_cpu_cycles.attr.attr,
246 &armv8_event_attr_br_pred.attr.attr,
247 &armv8_event_attr_mem_access.attr.attr,
248 &armv8_event_attr_l1i_cache.attr.attr,
249 &armv8_event_attr_l1d_cache_wb.attr.attr,
250 &armv8_event_attr_l2d_cache.attr.attr,
251 &armv8_event_attr_l2d_cache_refill.attr.attr,
252 &armv8_event_attr_l2d_cache_wb.attr.attr,
253 &armv8_event_attr_bus_access.attr.attr,
254 &armv8_event_attr_memory_error.attr.attr,
255 &armv8_event_attr_inst_spec.attr.attr,
256 &armv8_event_attr_ttbr_write_retired.attr.attr,
257 &armv8_event_attr_bus_cycles.attr.attr,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700258 &armv8_event_attr_l1d_cache_allocate.attr.attr,
259 &armv8_event_attr_l2d_cache_allocate.attr.attr,
260 &armv8_event_attr_br_retired.attr.attr,
261 &armv8_event_attr_br_mis_pred_retired.attr.attr,
262 &armv8_event_attr_stall_frontend.attr.attr,
263 &armv8_event_attr_stall_backend.attr.attr,
264 &armv8_event_attr_l1d_tlb.attr.attr,
265 &armv8_event_attr_l1i_tlb.attr.attr,
266 &armv8_event_attr_l2i_cache.attr.attr,
267 &armv8_event_attr_l2i_cache_refill.attr.attr,
268 &armv8_event_attr_l3d_cache_allocate.attr.attr,
269 &armv8_event_attr_l3d_cache_refill.attr.attr,
270 &armv8_event_attr_l3d_cache.attr.attr,
271 &armv8_event_attr_l3d_cache_wb.attr.attr,
272 &armv8_event_attr_l2d_tlb_refill.attr.attr,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700273 &armv8_event_attr_l2i_tlb_refill.attr.attr,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700274 &armv8_event_attr_l2d_tlb.attr.attr,
Ashok Kumar03598fd2016-04-21 05:58:41 -0700275 &armv8_event_attr_l2i_tlb.attr.attr,
Will Deacon2ddd5e582018-10-05 13:33:02 +0100276 &armv8_event_attr_remote_access.attr.attr,
277 &armv8_event_attr_ll_cache.attr.attr,
278 &armv8_event_attr_ll_cache_miss.attr.attr,
279 &armv8_event_attr_dtlb_walk.attr.attr,
280 &armv8_event_attr_itlb_walk.attr.attr,
281 &armv8_event_attr_ll_cache_rd.attr.attr,
282 &armv8_event_attr_ll_cache_miss_rd.attr.attr,
283 &armv8_event_attr_remote_access_rd.attr.attr,
284 &armv8_event_attr_sample_pop.attr.attr,
285 &armv8_event_attr_sample_feed.attr.attr,
286 &armv8_event_attr_sample_filtrate.attr.attr,
287 &armv8_event_attr_sample_collision.attr.attr,
Will Deacon57d74122015-12-22 14:42:57 +0000288 NULL,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700289};
290
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700291static umode_t
292armv8pmu_event_attr_is_visible(struct kobject *kobj,
293 struct attribute *attr, int unused)
294{
295 struct device *dev = kobj_to_dev(kobj);
296 struct pmu *pmu = dev_get_drvdata(dev);
297 struct arm_pmu *cpu_pmu = container_of(pmu, struct arm_pmu, pmu);
298 struct perf_pmu_events_attr *pmu_attr;
299
300 pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr.attr);
301
Will Deacon342e53b2018-10-05 13:28:07 +0100302 if (pmu_attr->id < ARMV8_PMUV3_MAX_COMMON_EVENTS &&
303 test_bit(pmu_attr->id, cpu_pmu->pmceid_bitmap))
304 return attr->mode;
305
306 pmu_attr->id -= ARMV8_PMUV3_EXT_COMMON_EVENT_BASE;
307 if (pmu_attr->id < ARMV8_PMUV3_MAX_COMMON_EVENTS &&
308 test_bit(pmu_attr->id, cpu_pmu->pmceid_ext_bitmap))
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700309 return attr->mode;
310
311 return 0;
312}
313
Drew Richardson9e9caa62015-10-22 07:07:32 -0700314static struct attribute_group armv8_pmuv3_events_attr_group = {
315 .name = "events",
316 .attrs = armv8_pmuv3_event_attrs,
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700317 .is_visible = armv8pmu_event_attr_is_visible,
Drew Richardson9e9caa62015-10-22 07:07:32 -0700318};
319
Shaokun Zhangfe7296e2017-05-24 15:43:18 +0800320PMU_FORMAT_ATTR(event, "config:0-15");
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100321PMU_FORMAT_ATTR(long, "config1:0");
322
323static inline bool armv8pmu_event_is_64bit(struct perf_event *event)
324{
325 return event->attr.config1 & 0x1;
326}
Will Deacon57d74122015-12-22 14:42:57 +0000327
328static struct attribute *armv8_pmuv3_format_attrs[] = {
329 &format_attr_event.attr,
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100330 &format_attr_long.attr,
Will Deacon57d74122015-12-22 14:42:57 +0000331 NULL,
332};
333
334static struct attribute_group armv8_pmuv3_format_attr_group = {
335 .name = "format",
336 .attrs = armv8_pmuv3_format_attrs,
337};
338
Will Deacon03089682012-03-05 11:49:32 +0000339/*
340 * Perf Events' indices
341 */
342#define ARMV8_IDX_CYCLE_COUNTER 0
343#define ARMV8_IDX_COUNTER0 1
Mark Rutland6475b2d2015-10-02 10:55:03 +0100344#define ARMV8_IDX_COUNTER_LAST(cpu_pmu) \
345 (ARMV8_IDX_CYCLE_COUNTER + cpu_pmu->num_events - 1)
Will Deacon03089682012-03-05 11:49:32 +0000346
Will Deacon03089682012-03-05 11:49:32 +0000347/*
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100348 * We must chain two programmable counters for 64 bit events,
349 * except when we have allocated the 64bit cycle counter (for CPU
350 * cycles event). This must be called only when the event has
351 * a counter allocated.
352 */
353static inline bool armv8pmu_event_is_chained(struct perf_event *event)
354{
355 int idx = event->hw.idx;
356
357 return !WARN_ON(idx < 0) &&
358 armv8pmu_event_is_64bit(event) &&
359 (idx != ARMV8_IDX_CYCLE_COUNTER);
360}
361
362/*
Will Deacon03089682012-03-05 11:49:32 +0000363 * ARMv8 low level PMU access
364 */
365
366/*
367 * Perf Event to low level counters mapping
368 */
369#define ARMV8_IDX_TO_COUNTER(x) \
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000370 (((x) - ARMV8_IDX_COUNTER0) & ARMV8_PMU_COUNTER_MASK)
Will Deacon03089682012-03-05 11:49:32 +0000371
372static inline u32 armv8pmu_pmcr_read(void)
373{
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700374 return read_sysreg(pmcr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000375}
376
377static inline void armv8pmu_pmcr_write(u32 val)
378{
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000379 val &= ARMV8_PMU_PMCR_MASK;
Will Deacon03089682012-03-05 11:49:32 +0000380 isb();
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700381 write_sysreg(val, pmcr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000382}
383
384static inline int armv8pmu_has_overflowed(u32 pmovsr)
385{
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000386 return pmovsr & ARMV8_PMU_OVERFLOWED_MASK;
Will Deacon03089682012-03-05 11:49:32 +0000387}
388
Mark Rutland6475b2d2015-10-02 10:55:03 +0100389static inline int armv8pmu_counter_valid(struct arm_pmu *cpu_pmu, int idx)
Will Deacon03089682012-03-05 11:49:32 +0000390{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100391 return idx >= ARMV8_IDX_CYCLE_COUNTER &&
392 idx <= ARMV8_IDX_COUNTER_LAST(cpu_pmu);
Will Deacon03089682012-03-05 11:49:32 +0000393}
394
395static inline int armv8pmu_counter_has_overflowed(u32 pmnc, int idx)
396{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100397 return pmnc & BIT(ARMV8_IDX_TO_COUNTER(idx));
Will Deacon03089682012-03-05 11:49:32 +0000398}
399
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100400static inline void armv8pmu_select_counter(int idx)
Will Deacon03089682012-03-05 11:49:32 +0000401{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100402 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700403 write_sysreg(counter, pmselr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000404 isb();
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100405}
Will Deacon03089682012-03-05 11:49:32 +0000406
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100407static inline u32 armv8pmu_read_evcntr(int idx)
408{
409 armv8pmu_select_counter(idx);
410 return read_sysreg(pmxevcntr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000411}
412
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100413static inline u64 armv8pmu_read_hw_counter(struct perf_event *event)
414{
415 int idx = event->hw.idx;
416 u64 val = 0;
417
418 val = armv8pmu_read_evcntr(idx);
419 if (armv8pmu_event_is_chained(event))
420 val = (val << 32) | armv8pmu_read_evcntr(idx - 1);
421 return val;
422}
423
Raphael Gault3d659e72019-04-11 17:16:46 +0100424static u64 armv8pmu_read_counter(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000425{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100426 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
427 struct hw_perf_event *hwc = &event->hw;
428 int idx = hwc->idx;
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100429 u64 value = 0;
Will Deacon03089682012-03-05 11:49:32 +0000430
Mark Rutland6475b2d2015-10-02 10:55:03 +0100431 if (!armv8pmu_counter_valid(cpu_pmu, idx))
Will Deacon03089682012-03-05 11:49:32 +0000432 pr_err("CPU%u reading wrong counter %d\n",
433 smp_processor_id(), idx);
434 else if (idx == ARMV8_IDX_CYCLE_COUNTER)
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700435 value = read_sysreg(pmccntr_el0);
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100436 else
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100437 value = armv8pmu_read_hw_counter(event);
Will Deacon03089682012-03-05 11:49:32 +0000438
439 return value;
440}
441
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100442static inline void armv8pmu_write_evcntr(int idx, u32 value)
443{
444 armv8pmu_select_counter(idx);
445 write_sysreg(value, pmxevcntr_el0);
446}
447
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100448static inline void armv8pmu_write_hw_counter(struct perf_event *event,
449 u64 value)
450{
451 int idx = event->hw.idx;
452
453 if (armv8pmu_event_is_chained(event)) {
454 armv8pmu_write_evcntr(idx, upper_32_bits(value));
455 armv8pmu_write_evcntr(idx - 1, lower_32_bits(value));
456 } else {
457 armv8pmu_write_evcntr(idx, value);
458 }
459}
460
Raphael Gault3d659e72019-04-11 17:16:46 +0100461static void armv8pmu_write_counter(struct perf_event *event, u64 value)
Will Deacon03089682012-03-05 11:49:32 +0000462{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100463 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
464 struct hw_perf_event *hwc = &event->hw;
465 int idx = hwc->idx;
466
467 if (!armv8pmu_counter_valid(cpu_pmu, idx))
Will Deacon03089682012-03-05 11:49:32 +0000468 pr_err("CPU%u writing wrong counter %d\n",
469 smp_processor_id(), idx);
Jan Glauber7175f052016-02-18 17:50:13 +0100470 else if (idx == ARMV8_IDX_CYCLE_COUNTER) {
471 /*
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100472 * The cycles counter is really a 64-bit counter.
473 * When treating it as a 32-bit counter, we only count
474 * the lower 32 bits, and set the upper 32-bits so that
475 * we get an interrupt upon 32-bit overflow.
Jan Glauber7175f052016-02-18 17:50:13 +0100476 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100477 if (!armv8pmu_event_is_64bit(event))
478 value |= 0xffffffff00000000ULL;
Suzuki K Poulose3a952002018-07-10 09:57:59 +0100479 write_sysreg(value, pmccntr_el0);
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100480 } else
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100481 armv8pmu_write_hw_counter(event, value);
Will Deacon03089682012-03-05 11:49:32 +0000482}
483
484static inline void armv8pmu_write_evtype(int idx, u32 val)
485{
Suzuki K Poulose0c55d192018-07-10 09:58:02 +0100486 armv8pmu_select_counter(idx);
487 val &= ARMV8_PMU_EVTYPE_MASK;
488 write_sysreg(val, pmxevtyper_el0);
Will Deacon03089682012-03-05 11:49:32 +0000489}
490
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100491static inline void armv8pmu_write_event_type(struct perf_event *event)
492{
493 struct hw_perf_event *hwc = &event->hw;
494 int idx = hwc->idx;
495
496 /*
497 * For chained events, the low counter is programmed to count
498 * the event of interest and the high counter is programmed
499 * with CHAIN event code with filters set to count at all ELs.
500 */
501 if (armv8pmu_event_is_chained(event)) {
502 u32 chain_evt = ARMV8_PMUV3_PERFCTR_CHAIN |
503 ARMV8_PMU_INCLUDE_EL2;
504
505 armv8pmu_write_evtype(idx - 1, hwc->config_base);
506 armv8pmu_write_evtype(idx, chain_evt);
507 } else {
508 armv8pmu_write_evtype(idx, hwc->config_base);
509 }
510}
511
Will Deacon03089682012-03-05 11:49:32 +0000512static inline int armv8pmu_enable_counter(int idx)
513{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100514 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700515 write_sysreg(BIT(counter), pmcntenset_el0);
Will Deacon03089682012-03-05 11:49:32 +0000516 return idx;
517}
518
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100519static inline void armv8pmu_enable_event_counter(struct perf_event *event)
520{
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100521 struct perf_event_attr *attr = &event->attr;
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100522 int idx = event->hw.idx;
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100523 u32 counter_bits = BIT(ARMV8_IDX_TO_COUNTER(idx));
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100524
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100525 if (armv8pmu_event_is_chained(event))
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100526 counter_bits |= BIT(ARMV8_IDX_TO_COUNTER(idx - 1));
527
528 kvm_set_pmu_events(counter_bits, attr);
529
530 /* We rely on the hypervisor switch code to enable guest counters */
531 if (!kvm_pmu_counter_deferred(attr)) {
532 armv8pmu_enable_counter(idx);
533 if (armv8pmu_event_is_chained(event))
534 armv8pmu_enable_counter(idx - 1);
535 }
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100536}
537
Will Deacon03089682012-03-05 11:49:32 +0000538static inline int armv8pmu_disable_counter(int idx)
539{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100540 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700541 write_sysreg(BIT(counter), pmcntenclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000542 return idx;
543}
544
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100545static inline void armv8pmu_disable_event_counter(struct perf_event *event)
546{
547 struct hw_perf_event *hwc = &event->hw;
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100548 struct perf_event_attr *attr = &event->attr;
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100549 int idx = hwc->idx;
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100550 u32 counter_bits = BIT(ARMV8_IDX_TO_COUNTER(idx));
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100551
552 if (armv8pmu_event_is_chained(event))
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100553 counter_bits |= BIT(ARMV8_IDX_TO_COUNTER(idx - 1));
554
555 kvm_clr_pmu_events(counter_bits);
556
557 /* We rely on the hypervisor switch code to disable guest counters */
558 if (!kvm_pmu_counter_deferred(attr)) {
559 if (armv8pmu_event_is_chained(event))
560 armv8pmu_disable_counter(idx - 1);
561 armv8pmu_disable_counter(idx);
562 }
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100563}
564
Will Deacon03089682012-03-05 11:49:32 +0000565static inline int armv8pmu_enable_intens(int idx)
566{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100567 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700568 write_sysreg(BIT(counter), pmintenset_el1);
Will Deacon03089682012-03-05 11:49:32 +0000569 return idx;
570}
571
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100572static inline int armv8pmu_enable_event_irq(struct perf_event *event)
573{
574 return armv8pmu_enable_intens(event->hw.idx);
575}
576
Will Deacon03089682012-03-05 11:49:32 +0000577static inline int armv8pmu_disable_intens(int idx)
578{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100579 u32 counter = ARMV8_IDX_TO_COUNTER(idx);
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700580 write_sysreg(BIT(counter), pmintenclr_el1);
Will Deacon03089682012-03-05 11:49:32 +0000581 isb();
582 /* Clear the overflow flag in case an interrupt is pending. */
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700583 write_sysreg(BIT(counter), pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000584 isb();
Mark Rutland6475b2d2015-10-02 10:55:03 +0100585
Will Deacon03089682012-03-05 11:49:32 +0000586 return idx;
587}
588
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100589static inline int armv8pmu_disable_event_irq(struct perf_event *event)
590{
591 return armv8pmu_disable_intens(event->hw.idx);
592}
593
Will Deacon03089682012-03-05 11:49:32 +0000594static inline u32 armv8pmu_getreset_flags(void)
595{
596 u32 value;
597
598 /* Read */
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700599 value = read_sysreg(pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000600
601 /* Write to clear flags */
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000602 value &= ARMV8_PMU_OVSR_MASK;
Ashok Kumarbf2d4782016-04-21 05:58:43 -0700603 write_sysreg(value, pmovsclr_el0);
Will Deacon03089682012-03-05 11:49:32 +0000604
605 return value;
606}
607
Mark Rutland6475b2d2015-10-02 10:55:03 +0100608static void armv8pmu_enable_event(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000609{
610 unsigned long flags;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100611 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
612 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
Will Deacon03089682012-03-05 11:49:32 +0000613
614 /*
615 * Enable counter and interrupt, and set the counter to count
616 * the event that we're interested in.
617 */
618 raw_spin_lock_irqsave(&events->pmu_lock, flags);
619
620 /*
621 * Disable counter
622 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100623 armv8pmu_disable_event_counter(event);
Will Deacon03089682012-03-05 11:49:32 +0000624
625 /*
626 * Set event (if destined for PMNx counters).
627 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100628 armv8pmu_write_event_type(event);
Will Deacon03089682012-03-05 11:49:32 +0000629
630 /*
631 * Enable interrupt for this counter
632 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100633 armv8pmu_enable_event_irq(event);
Will Deacon03089682012-03-05 11:49:32 +0000634
635 /*
636 * Enable counter
637 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100638 armv8pmu_enable_event_counter(event);
Will Deacon03089682012-03-05 11:49:32 +0000639
640 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
641}
642
Mark Rutland6475b2d2015-10-02 10:55:03 +0100643static void armv8pmu_disable_event(struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000644{
645 unsigned long flags;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100646 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
647 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
Will Deacon03089682012-03-05 11:49:32 +0000648
649 /*
650 * Disable counter and interrupt
651 */
652 raw_spin_lock_irqsave(&events->pmu_lock, flags);
653
654 /*
655 * Disable counter
656 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100657 armv8pmu_disable_event_counter(event);
Will Deacon03089682012-03-05 11:49:32 +0000658
659 /*
660 * Disable interrupt for this counter
661 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100662 armv8pmu_disable_event_irq(event);
Will Deacon03089682012-03-05 11:49:32 +0000663
664 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
665}
666
Suzuki K Poulose3cce50d2018-07-10 09:58:03 +0100667static void armv8pmu_start(struct arm_pmu *cpu_pmu)
668{
669 unsigned long flags;
670 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
671
672 raw_spin_lock_irqsave(&events->pmu_lock, flags);
673 /* Enable all counters */
674 armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMU_PMCR_E);
675 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
676}
677
678static void armv8pmu_stop(struct arm_pmu *cpu_pmu)
679{
680 unsigned long flags;
681 struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
682
683 raw_spin_lock_irqsave(&events->pmu_lock, flags);
684 /* Disable all counters */
685 armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMU_PMCR_E);
686 raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
687}
688
Mark Rutland0788f1e2018-05-10 11:35:15 +0100689static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +0000690{
691 u32 pmovsr;
692 struct perf_sample_data data;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100693 struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
Will Deacon03089682012-03-05 11:49:32 +0000694 struct pt_regs *regs;
695 int idx;
696
697 /*
698 * Get and reset the IRQ flags
699 */
700 pmovsr = armv8pmu_getreset_flags();
701
702 /*
703 * Did an overflow occur?
704 */
705 if (!armv8pmu_has_overflowed(pmovsr))
706 return IRQ_NONE;
707
708 /*
709 * Handle the counter(s) overflow(s)
710 */
711 regs = get_irq_regs();
712
Suzuki K Poulose3cce50d2018-07-10 09:58:03 +0100713 /*
714 * Stop the PMU while processing the counter overflows
715 * to prevent skews in group events.
716 */
717 armv8pmu_stop(cpu_pmu);
Will Deacon03089682012-03-05 11:49:32 +0000718 for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
719 struct perf_event *event = cpuc->events[idx];
720 struct hw_perf_event *hwc;
721
722 /* Ignore if we don't have an event. */
723 if (!event)
724 continue;
725
726 /*
727 * We have a single interrupt for all counters. Check that
728 * each counter has overflowed before we process it.
729 */
730 if (!armv8pmu_counter_has_overflowed(pmovsr, idx))
731 continue;
732
733 hwc = &event->hw;
Mark Rutland6475b2d2015-10-02 10:55:03 +0100734 armpmu_event_update(event);
Will Deacon03089682012-03-05 11:49:32 +0000735 perf_sample_data_init(&data, 0, hwc->last_period);
Mark Rutland6475b2d2015-10-02 10:55:03 +0100736 if (!armpmu_event_set_period(event))
Will Deacon03089682012-03-05 11:49:32 +0000737 continue;
738
739 if (perf_event_overflow(event, &data, regs))
Mark Rutland6475b2d2015-10-02 10:55:03 +0100740 cpu_pmu->disable(event);
Will Deacon03089682012-03-05 11:49:32 +0000741 }
Suzuki K Poulose3cce50d2018-07-10 09:58:03 +0100742 armv8pmu_start(cpu_pmu);
Will Deacon03089682012-03-05 11:49:32 +0000743
744 /*
745 * Handle the pending perf events.
746 *
747 * Note: this call *must* be run with interrupts disabled. For
748 * platforms that can have the PMU interrupts raised as an NMI, this
749 * will not work.
750 */
751 irq_work_run();
752
753 return IRQ_HANDLED;
754}
755
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100756static int armv8pmu_get_single_idx(struct pmu_hw_events *cpuc,
757 struct arm_pmu *cpu_pmu)
758{
759 int idx;
760
761 for (idx = ARMV8_IDX_COUNTER0; idx < cpu_pmu->num_events; idx ++) {
762 if (!test_and_set_bit(idx, cpuc->used_mask))
763 return idx;
764 }
765 return -EAGAIN;
766}
767
768static int armv8pmu_get_chain_idx(struct pmu_hw_events *cpuc,
769 struct arm_pmu *cpu_pmu)
770{
771 int idx;
772
773 /*
774 * Chaining requires two consecutive event counters, where
775 * the lower idx must be even.
776 */
777 for (idx = ARMV8_IDX_COUNTER0 + 1; idx < cpu_pmu->num_events; idx += 2) {
778 if (!test_and_set_bit(idx, cpuc->used_mask)) {
779 /* Check if the preceding even counter is available */
780 if (!test_and_set_bit(idx - 1, cpuc->used_mask))
781 return idx;
782 /* Release the Odd counter */
783 clear_bit(idx, cpuc->used_mask);
784 }
785 }
786 return -EAGAIN;
787}
788
Will Deacon03089682012-03-05 11:49:32 +0000789static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
Mark Rutland6475b2d2015-10-02 10:55:03 +0100790 struct perf_event *event)
Will Deacon03089682012-03-05 11:49:32 +0000791{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100792 struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
793 struct hw_perf_event *hwc = &event->hw;
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000794 unsigned long evtype = hwc->config_base & ARMV8_PMU_EVTYPE_EVENT;
Will Deacon03089682012-03-05 11:49:32 +0000795
Pratyush Anand1031a152017-07-01 12:03:35 +0530796 /* Always prefer to place a cycle counter into the cycle counter. */
Ashok Kumar03598fd2016-04-21 05:58:41 -0700797 if (evtype == ARMV8_PMUV3_PERFCTR_CPU_CYCLES) {
Pratyush Anand1031a152017-07-01 12:03:35 +0530798 if (!test_and_set_bit(ARMV8_IDX_CYCLE_COUNTER, cpuc->used_mask))
799 return ARMV8_IDX_CYCLE_COUNTER;
Will Deacon03089682012-03-05 11:49:32 +0000800 }
801
802 /*
Pratyush Anand1031a152017-07-01 12:03:35 +0530803 * Otherwise use events counters
Will Deacon03089682012-03-05 11:49:32 +0000804 */
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100805 if (armv8pmu_event_is_64bit(event))
806 return armv8pmu_get_chain_idx(cpuc, cpu_pmu);
807 else
808 return armv8pmu_get_single_idx(cpuc, cpu_pmu);
Will Deacon03089682012-03-05 11:49:32 +0000809}
810
Suzuki K Poulose7dfc8db2018-07-10 09:58:01 +0100811static void armv8pmu_clear_event_idx(struct pmu_hw_events *cpuc,
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100812 struct perf_event *event)
Suzuki K Poulose7dfc8db2018-07-10 09:58:01 +0100813{
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100814 int idx = event->hw.idx;
815
816 clear_bit(idx, cpuc->used_mask);
817 if (armv8pmu_event_is_chained(event))
818 clear_bit(idx - 1, cpuc->used_mask);
Suzuki K Poulose7dfc8db2018-07-10 09:58:01 +0100819}
820
Will Deacon03089682012-03-05 11:49:32 +0000821/*
Andrew Murrayb3650672019-01-18 14:02:27 +0000822 * Add an event filter to a given event.
Will Deacon03089682012-03-05 11:49:32 +0000823 */
824static int armv8pmu_set_event_filter(struct hw_perf_event *event,
825 struct perf_event_attr *attr)
826{
827 unsigned long config_base = 0;
828
829 if (attr->exclude_idle)
830 return -EPERM;
Ganapatrao Kulkarni78a19cf2017-05-02 21:59:34 +0530831
832 /*
833 * If we're running in hyp mode, then we *are* the hypervisor.
834 * Therefore we ignore exclude_hv in this configuration, since
835 * there's no hypervisor to sample anyway. This is consistent
836 * with other architectures (x86 and Power).
837 */
838 if (is_kernel_in_hyp_mode()) {
Andrew Murray435e53f2019-04-09 20:22:15 +0100839 if (!attr->exclude_kernel && !attr->exclude_host)
Ganapatrao Kulkarni78a19cf2017-05-02 21:59:34 +0530840 config_base |= ARMV8_PMU_INCLUDE_EL2;
Andrew Murray435e53f2019-04-09 20:22:15 +0100841 if (attr->exclude_guest)
Ganapatrao Kulkarni78a19cf2017-05-02 21:59:34 +0530842 config_base |= ARMV8_PMU_EXCLUDE_EL1;
Andrew Murray435e53f2019-04-09 20:22:15 +0100843 if (attr->exclude_host)
844 config_base |= ARMV8_PMU_EXCLUDE_EL0;
Ganapatrao Kulkarni78a19cf2017-05-02 21:59:34 +0530845 } else {
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100846 if (!attr->exclude_hv && !attr->exclude_host)
Ganapatrao Kulkarni78a19cf2017-05-02 21:59:34 +0530847 config_base |= ARMV8_PMU_INCLUDE_EL2;
848 }
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100849
850 /*
851 * Filter out !VHE kernels and guest kernels
852 */
853 if (attr->exclude_kernel)
854 config_base |= ARMV8_PMU_EXCLUDE_EL1;
855
Will Deacon03089682012-03-05 11:49:32 +0000856 if (attr->exclude_user)
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000857 config_base |= ARMV8_PMU_EXCLUDE_EL0;
Will Deacon03089682012-03-05 11:49:32 +0000858
859 /*
860 * Install the filter into config_base as this is used to
861 * construct the event type.
862 */
863 event->config_base = config_base;
864
865 return 0;
866}
867
Will Deaconca2b4972018-10-05 13:24:36 +0100868static int armv8pmu_filter_match(struct perf_event *event)
869{
870 unsigned long evtype = event->hw.config_base & ARMV8_PMU_EVTYPE_EVENT;
871 return evtype != ARMV8_PMUV3_PERFCTR_CHAIN;
872}
873
Will Deacon03089682012-03-05 11:49:32 +0000874static void armv8pmu_reset(void *info)
875{
Mark Rutland6475b2d2015-10-02 10:55:03 +0100876 struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
Will Deacon03089682012-03-05 11:49:32 +0000877 u32 idx, nb_cnt = cpu_pmu->num_events;
878
879 /* The counter and interrupt enable registers are unknown at reset. */
Mark Rutland6475b2d2015-10-02 10:55:03 +0100880 for (idx = ARMV8_IDX_CYCLE_COUNTER; idx < nb_cnt; ++idx) {
881 armv8pmu_disable_counter(idx);
882 armv8pmu_disable_intens(idx);
883 }
Will Deacon03089682012-03-05 11:49:32 +0000884
Andrew Murrayd1947bc2019-04-09 20:22:13 +0100885 /* Clear the counters we flip at guest entry/exit */
886 kvm_clr_pmu_events(U32_MAX);
887
Jan Glauber7175f052016-02-18 17:50:13 +0100888 /*
889 * Initialize & Reset PMNC. Request overflow interrupt for
890 * 64 bit cycle counter but cheat in armv8pmu_write_counter().
891 */
Shannon Zhaob8cfadf2016-03-24 16:01:16 +0000892 armv8pmu_pmcr_write(ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C |
893 ARMV8_PMU_PMCR_LC);
Will Deacon03089682012-03-05 11:49:32 +0000894}
895
Will Deacon6c833bb2017-08-08 16:58:33 +0100896static int __armv8_pmuv3_map_event(struct perf_event *event,
897 const unsigned (*extra_event_map)
898 [PERF_COUNT_HW_MAX],
899 const unsigned (*extra_cache_map)
900 [PERF_COUNT_HW_CACHE_MAX]
901 [PERF_COUNT_HW_CACHE_OP_MAX]
902 [PERF_COUNT_HW_CACHE_RESULT_MAX])
Will Deacon03089682012-03-05 11:49:32 +0000903{
Jeremy Linton236b9b912016-09-14 17:32:30 -0500904 int hw_event_id;
905 struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
906
907 hw_event_id = armpmu_map_event(event, &armv8_pmuv3_perf_map,
908 &armv8_pmuv3_perf_cache_map,
909 ARMV8_PMU_EVTYPE_EVENT);
Jeremy Linton236b9b912016-09-14 17:32:30 -0500910
Suzuki K Poulosec1320792018-07-10 09:58:04 +0100911 if (armv8pmu_event_is_64bit(event))
912 event->hw.flags |= ARMPMU_EVT_64BIT;
913
Shaokun Zhange2b5c5c2018-10-06 15:57:38 +0800914 /* Only expose micro/arch events supported by this PMU */
Will Deacon6c833bb2017-08-08 16:58:33 +0100915 if ((hw_event_id > 0) && (hw_event_id < ARMV8_PMUV3_MAX_COMMON_EVENTS)
916 && test_bit(hw_event_id, armpmu->pmceid_bitmap)) {
917 return hw_event_id;
Jeremy Linton236b9b912016-09-14 17:32:30 -0500918 }
919
Will Deacon6c833bb2017-08-08 16:58:33 +0100920 return armpmu_map_event(event, extra_event_map, extra_cache_map,
921 ARMV8_PMU_EVTYPE_EVENT);
922}
923
924static int armv8_pmuv3_map_event(struct perf_event *event)
925{
926 return __armv8_pmuv3_map_event(event, NULL, NULL);
Will Deacon03089682012-03-05 11:49:32 +0000927}
928
Mark Rutlandac82d122015-10-02 10:55:04 +0100929static int armv8_a53_map_event(struct perf_event *event)
930{
Will Deacond0d09d42017-08-08 17:11:27 +0100931 return __armv8_pmuv3_map_event(event, NULL, &armv8_a53_perf_cache_map);
Mark Rutlandac82d122015-10-02 10:55:04 +0100932}
933
Mark Rutland62a4dda2015-10-02 10:55:05 +0100934static int armv8_a57_map_event(struct perf_event *event)
935{
Will Deacond0d09d42017-08-08 17:11:27 +0100936 return __armv8_pmuv3_map_event(event, NULL, &armv8_a57_perf_cache_map);
Mark Rutland62a4dda2015-10-02 10:55:05 +0100937}
938
Julien Thierry5561b6c2017-08-09 17:46:38 +0100939static int armv8_a73_map_event(struct perf_event *event)
940{
941 return __armv8_pmuv3_map_event(event, NULL, &armv8_a73_perf_cache_map);
942}
943
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100944static int armv8_thunder_map_event(struct perf_event *event)
945{
Will Deacond0d09d42017-08-08 17:11:27 +0100946 return __armv8_pmuv3_map_event(event, NULL,
Will Deacon6c833bb2017-08-08 16:58:33 +0100947 &armv8_thunder_perf_cache_map);
Jan Glauberd0aa2bf2016-02-18 17:50:11 +0100948}
949
Ashok Kumar201a72b2016-04-21 05:58:45 -0700950static int armv8_vulcan_map_event(struct perf_event *event)
951{
Will Deacond0d09d42017-08-08 17:11:27 +0100952 return __armv8_pmuv3_map_event(event, NULL,
Will Deacon6c833bb2017-08-08 16:58:33 +0100953 &armv8_vulcan_perf_cache_map);
Ashok Kumar201a72b2016-04-21 05:58:45 -0700954}
955
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100956struct armv8pmu_probe_info {
957 struct arm_pmu *pmu;
958 bool present;
959};
960
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700961static void __armv8pmu_probe_pmu(void *info)
Will Deacon03089682012-03-05 11:49:32 +0000962{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100963 struct armv8pmu_probe_info *probe = info;
964 struct arm_pmu *cpu_pmu = probe->pmu;
Mark Rutlandfaa9a082017-04-25 12:08:50 +0100965 u64 dfr0;
Will Deacon342e53b2018-10-05 13:28:07 +0100966 u64 pmceid_raw[2];
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700967 u32 pmceid[2];
Mark Rutlandfaa9a082017-04-25 12:08:50 +0100968 int pmuver;
Will Deacon03089682012-03-05 11:49:32 +0000969
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100970 dfr0 = read_sysreg(id_aa64dfr0_el1);
Mark Rutland03313652018-02-14 17:21:57 +0000971 pmuver = cpuid_feature_extract_unsigned_field(dfr0,
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100972 ID_AA64DFR0_PMUVER_SHIFT);
Mark Rutland03313652018-02-14 17:21:57 +0000973 if (pmuver == 0xf || pmuver == 0)
Mark Rutlandf1b36dc2017-04-11 09:39:56 +0100974 return;
975
976 probe->present = true;
977
Will Deacon03089682012-03-05 11:49:32 +0000978 /* Read the nb of CNTx counters supported from PMNC */
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700979 cpu_pmu->num_events = (armv8pmu_pmcr_read() >> ARMV8_PMU_PMCR_N_SHIFT)
980 & ARMV8_PMU_PMCR_N_MASK;
Will Deacon03089682012-03-05 11:49:32 +0000981
Mark Rutland6475b2d2015-10-02 10:55:03 +0100982 /* Add the CPU cycles counter */
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700983 cpu_pmu->num_events += 1;
984
Will Deacon342e53b2018-10-05 13:28:07 +0100985 pmceid[0] = pmceid_raw[0] = read_sysreg(pmceid0_el0);
986 pmceid[1] = pmceid_raw[1] = read_sysreg(pmceid1_el0);
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700987
Yury Norov3aa56882018-02-06 15:38:06 -0800988 bitmap_from_arr32(cpu_pmu->pmceid_bitmap,
989 pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
Will Deacon342e53b2018-10-05 13:28:07 +0100990
991 pmceid[0] = pmceid_raw[0] >> 32;
992 pmceid[1] = pmceid_raw[1] >> 32;
993
994 bitmap_from_arr32(cpu_pmu->pmceid_ext_bitmap,
995 pmceid, ARMV8_PMUV3_MAX_COMMON_EVENTS);
Will Deacon03089682012-03-05 11:49:32 +0000996}
997
Ashok Kumar4b1a9e62016-04-21 05:58:44 -0700998static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +0000999{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001000 struct armv8pmu_probe_info probe = {
1001 .pmu = cpu_pmu,
1002 .present = false,
1003 };
1004 int ret;
1005
1006 ret = smp_call_function_any(&cpu_pmu->supported_cpus,
Ashok Kumar4b1a9e62016-04-21 05:58:44 -07001007 __armv8pmu_probe_pmu,
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001008 &probe, 1);
1009 if (ret)
1010 return ret;
1011
1012 return probe.present ? 0 : -ENODEV;
Will Deacon03089682012-03-05 11:49:32 +00001013}
1014
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001015static int armv8_pmu_init(struct arm_pmu *cpu_pmu)
Will Deacon03089682012-03-05 11:49:32 +00001016{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001017 int ret = armv8pmu_probe_pmu(cpu_pmu);
1018 if (ret)
1019 return ret;
1020
Will Deacond3adeed2018-10-05 13:26:21 +01001021 cpu_pmu->handle_irq = armv8pmu_handle_irq;
1022 cpu_pmu->enable = armv8pmu_enable_event;
1023 cpu_pmu->disable = armv8pmu_disable_event;
1024 cpu_pmu->read_counter = armv8pmu_read_counter;
1025 cpu_pmu->write_counter = armv8pmu_write_counter;
1026 cpu_pmu->get_event_idx = armv8pmu_get_event_idx;
1027 cpu_pmu->clear_event_idx = armv8pmu_clear_event_idx;
1028 cpu_pmu->start = armv8pmu_start;
1029 cpu_pmu->stop = armv8pmu_stop;
1030 cpu_pmu->reset = armv8pmu_reset;
Mark Rutlandac82d122015-10-02 10:55:04 +01001031 cpu_pmu->set_event_filter = armv8pmu_set_event_filter;
Will Deaconca2b4972018-10-05 13:24:36 +01001032 cpu_pmu->filter_match = armv8pmu_filter_match;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001033
1034 return 0;
Mark Rutlandac82d122015-10-02 10:55:04 +01001035}
1036
1037static int armv8_pmuv3_init(struct arm_pmu *cpu_pmu)
1038{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001039 int ret = armv8_pmu_init(cpu_pmu);
1040 if (ret)
1041 return ret;
1042
Mark Rutland6475b2d2015-10-02 10:55:03 +01001043 cpu_pmu->name = "armv8_pmuv3";
1044 cpu_pmu->map_event = armv8_pmuv3_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001045 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1046 &armv8_pmuv3_events_attr_group;
1047 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1048 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001049
1050 return 0;
Mark Rutlandac82d122015-10-02 10:55:04 +01001051}
1052
Julien Thierrye884f802017-08-09 17:46:39 +01001053static int armv8_a35_pmu_init(struct arm_pmu *cpu_pmu)
1054{
1055 int ret = armv8_pmu_init(cpu_pmu);
1056 if (ret)
1057 return ret;
1058
1059 cpu_pmu->name = "armv8_cortex_a35";
1060 cpu_pmu->map_event = armv8_a53_map_event;
1061 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1062 &armv8_pmuv3_events_attr_group;
1063 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1064 &armv8_pmuv3_format_attr_group;
1065
1066 return 0;
1067}
1068
Mark Rutlandac82d122015-10-02 10:55:04 +01001069static int armv8_a53_pmu_init(struct arm_pmu *cpu_pmu)
1070{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001071 int ret = armv8_pmu_init(cpu_pmu);
1072 if (ret)
1073 return ret;
1074
Mark Rutlandac82d122015-10-02 10:55:04 +01001075 cpu_pmu->name = "armv8_cortex_a53";
1076 cpu_pmu->map_event = armv8_a53_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001077 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1078 &armv8_pmuv3_events_attr_group;
1079 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1080 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001081
1082 return 0;
Will Deacon03089682012-03-05 11:49:32 +00001083}
Will Deacon03089682012-03-05 11:49:32 +00001084
Mark Rutland62a4dda2015-10-02 10:55:05 +01001085static int armv8_a57_pmu_init(struct arm_pmu *cpu_pmu)
1086{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001087 int ret = armv8_pmu_init(cpu_pmu);
1088 if (ret)
1089 return ret;
1090
Mark Rutland62a4dda2015-10-02 10:55:05 +01001091 cpu_pmu->name = "armv8_cortex_a57";
1092 cpu_pmu->map_event = armv8_a57_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001093 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1094 &armv8_pmuv3_events_attr_group;
1095 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1096 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001097
1098 return 0;
Mark Rutland62a4dda2015-10-02 10:55:05 +01001099}
1100
Will Deacon5d7ee872015-12-22 14:45:35 +00001101static int armv8_a72_pmu_init(struct arm_pmu *cpu_pmu)
1102{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001103 int ret = armv8_pmu_init(cpu_pmu);
1104 if (ret)
1105 return ret;
1106
Will Deacon5d7ee872015-12-22 14:45:35 +00001107 cpu_pmu->name = "armv8_cortex_a72";
1108 cpu_pmu->map_event = armv8_a57_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001109 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1110 &armv8_pmuv3_events_attr_group;
1111 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1112 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001113
1114 return 0;
Will Deacon5d7ee872015-12-22 14:45:35 +00001115}
1116
Julien Thierry5561b6c2017-08-09 17:46:38 +01001117static int armv8_a73_pmu_init(struct arm_pmu *cpu_pmu)
1118{
1119 int ret = armv8_pmu_init(cpu_pmu);
1120 if (ret)
1121 return ret;
1122
1123 cpu_pmu->name = "armv8_cortex_a73";
1124 cpu_pmu->map_event = armv8_a73_map_event;
1125 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1126 &armv8_pmuv3_events_attr_group;
1127 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1128 &armv8_pmuv3_format_attr_group;
1129
1130 return 0;
1131}
1132
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001133static int armv8_thunder_pmu_init(struct arm_pmu *cpu_pmu)
1134{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001135 int ret = armv8_pmu_init(cpu_pmu);
1136 if (ret)
1137 return ret;
1138
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001139 cpu_pmu->name = "armv8_cavium_thunder";
1140 cpu_pmu->map_event = armv8_thunder_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001141 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1142 &armv8_pmuv3_events_attr_group;
1143 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1144 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001145
1146 return 0;
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001147}
1148
Ashok Kumar201a72b2016-04-21 05:58:45 -07001149static int armv8_vulcan_pmu_init(struct arm_pmu *cpu_pmu)
1150{
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001151 int ret = armv8_pmu_init(cpu_pmu);
1152 if (ret)
1153 return ret;
1154
Ashok Kumar201a72b2016-04-21 05:58:45 -07001155 cpu_pmu->name = "armv8_brcm_vulcan";
1156 cpu_pmu->map_event = armv8_vulcan_map_event;
Mark Rutland569de9022016-09-09 14:08:27 +01001157 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] =
1158 &armv8_pmuv3_events_attr_group;
1159 cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] =
1160 &armv8_pmuv3_format_attr_group;
Mark Rutlandf1b36dc2017-04-11 09:39:56 +01001161
1162 return 0;
Ashok Kumar201a72b2016-04-21 05:58:45 -07001163}
1164
Mark Rutland6475b2d2015-10-02 10:55:03 +01001165static const struct of_device_id armv8_pmu_of_device_ids[] = {
1166 {.compatible = "arm,armv8-pmuv3", .data = armv8_pmuv3_init},
Julien Thierrye884f802017-08-09 17:46:39 +01001167 {.compatible = "arm,cortex-a35-pmu", .data = armv8_a35_pmu_init},
Mark Rutlandac82d122015-10-02 10:55:04 +01001168 {.compatible = "arm,cortex-a53-pmu", .data = armv8_a53_pmu_init},
Mark Rutland62a4dda2015-10-02 10:55:05 +01001169 {.compatible = "arm,cortex-a57-pmu", .data = armv8_a57_pmu_init},
Will Deacon5d7ee872015-12-22 14:45:35 +00001170 {.compatible = "arm,cortex-a72-pmu", .data = armv8_a72_pmu_init},
Julien Thierry5561b6c2017-08-09 17:46:38 +01001171 {.compatible = "arm,cortex-a73-pmu", .data = armv8_a73_pmu_init},
Jan Glauberd0aa2bf2016-02-18 17:50:11 +01001172 {.compatible = "cavium,thunder-pmu", .data = armv8_thunder_pmu_init},
Ashok Kumar201a72b2016-04-21 05:58:45 -07001173 {.compatible = "brcm,vulcan-pmu", .data = armv8_vulcan_pmu_init},
Will Deacon03089682012-03-05 11:49:32 +00001174 {},
1175};
1176
Mark Rutland6475b2d2015-10-02 10:55:03 +01001177static int armv8_pmu_device_probe(struct platform_device *pdev)
Will Deacon03089682012-03-05 11:49:32 +00001178{
Mark Rutlandf00fa5f2017-04-11 09:39:57 +01001179 return arm_pmu_device_probe(pdev, armv8_pmu_of_device_ids, NULL);
Will Deacon03089682012-03-05 11:49:32 +00001180}
1181
Mark Rutland6475b2d2015-10-02 10:55:03 +01001182static struct platform_driver armv8_pmu_driver = {
Will Deacon03089682012-03-05 11:49:32 +00001183 .driver = {
Jeremy Linton85023b22016-09-14 17:32:31 -05001184 .name = ARMV8_PMU_PDEV_NAME,
Mark Rutland6475b2d2015-10-02 10:55:03 +01001185 .of_match_table = armv8_pmu_of_device_ids,
Anders Roxell81e9fa82018-10-17 17:26:22 +02001186 .suppress_bind_attrs = true,
Will Deacon03089682012-03-05 11:49:32 +00001187 },
Mark Rutland6475b2d2015-10-02 10:55:03 +01001188 .probe = armv8_pmu_device_probe,
Will Deacon03089682012-03-05 11:49:32 +00001189};
1190
Mark Rutlandf00fa5f2017-04-11 09:39:57 +01001191static int __init armv8_pmu_driver_init(void)
1192{
1193 if (acpi_disabled)
1194 return platform_driver_register(&armv8_pmu_driver);
1195 else
1196 return arm_pmu_acpi_probe(armv8_pmuv3_init);
1197}
1198device_initcall(armv8_pmu_driver_init)
Michael O'Farrell9d2dcc8f2018-07-30 13:14:34 -07001199
1200void arch_perf_update_userpage(struct perf_event *event,
1201 struct perf_event_mmap_page *userpg, u64 now)
1202{
1203 u32 freq;
1204 u32 shift;
1205
1206 /*
1207 * Internal timekeeping for enabled/running/stopped times
1208 * is always computed with the sched_clock.
1209 */
1210 freq = arch_timer_get_rate();
1211 userpg->cap_user_time = 1;
1212
1213 clocks_calc_mult_shift(&userpg->time_mult, &shift, freq,
1214 NSEC_PER_SEC, 0);
1215 /*
1216 * time_shift is not expected to be greater than 31 due to
1217 * the original published conversion algorithm shifting a
1218 * 32-bit value (now specifies a 64-bit value) - refer
1219 * perf_event_mmap_page documentation in perf_event.h.
1220 */
1221 if (shift == 32) {
1222 shift = 31;
1223 userpg->time_mult >>= 1;
1224 }
1225 userpg->time_shift = (u16)shift;
1226 userpg->time_offset = -now;
1227}