xref: /linux/drivers/perf/riscv_pmu_sbi.c (revision 8581ae1ea0d203a71851b21455c2d5167ba00e50)
1e9991434SAtish Patra // SPDX-License-Identifier: GPL-2.0
2e9991434SAtish Patra /*
3e9991434SAtish Patra  * RISC-V performance counter support.
4e9991434SAtish Patra  *
5e9991434SAtish Patra  * Copyright (C) 2021 Western Digital Corporation or its affiliates.
6e9991434SAtish Patra  *
7e9991434SAtish Patra  * This code is based on ARM perf event code which is in turn based on
8e9991434SAtish Patra  * sparc64 and x86 code.
9e9991434SAtish Patra  */
10e9991434SAtish Patra 
11e9991434SAtish Patra #define pr_fmt(fmt) "riscv-pmu-sbi: " fmt
12e9991434SAtish Patra 
13e9991434SAtish Patra #include <linux/mod_devicetable.h>
14e9991434SAtish Patra #include <linux/perf/riscv_pmu.h>
15e9991434SAtish Patra #include <linux/platform_device.h>
164905ec2fSAtish Patra #include <linux/irq.h>
174905ec2fSAtish Patra #include <linux/irqdomain.h>
184905ec2fSAtish Patra #include <linux/of_irq.h>
194905ec2fSAtish Patra #include <linux/of.h>
20e9a023f2SEric Lin #include <linux/cpu_pm.h>
21096b52fdSSergey Matyukevich #include <linux/sched/clock.h>
22bc969d6cSYu Chien Peter Lin #include <linux/soc/andes/irq.h>
2316d3b1afSSamuel Holland #include <linux/workqueue.h>
24e9991434SAtish Patra 
2565e9fb08SHeiko Stuebner #include <asm/errata_list.h>
26e9991434SAtish Patra #include <asm/sbi.h>
27e72c4333SXiao Wang #include <asm/cpufeature.h>
2823c996fcSCharlie Jenkins #include <asm/vendor_extensions.h>
2923c996fcSCharlie Jenkins #include <asm/vendor_extensions/andes.h>
30e9991434SAtish Patra 
31bc969d6cSYu Chien Peter Lin #define ALT_SBI_PMU_OVERFLOW(__ovl)					\
32bc969d6cSYu Chien Peter Lin asm volatile(ALTERNATIVE_2(						\
33d1927f64SAtish Patra 	"csrr %0, " __stringify(CSR_SCOUNTOVF),				\
34bc969d6cSYu Chien Peter Lin 	"csrr %0, " __stringify(THEAD_C9XX_CSR_SCOUNTEROF),		\
35bc969d6cSYu Chien Peter Lin 		THEAD_VENDOR_ID, ERRATA_THEAD_PMU,			\
36bc969d6cSYu Chien Peter Lin 		CONFIG_ERRATA_THEAD_PMU,				\
37bc969d6cSYu Chien Peter Lin 	"csrr %0, " __stringify(ANDES_CSR_SCOUNTEROF),			\
3823c996fcSCharlie Jenkins 		ANDES_VENDOR_ID,					\
3923c996fcSCharlie Jenkins 		RISCV_ISA_VENDOR_EXT_XANDESPMU + RISCV_VENDOR_EXT_ALTERNATIVES_BASE, \
40bc969d6cSYu Chien Peter Lin 		CONFIG_ANDES_CUSTOM_PMU)				\
41bc969d6cSYu Chien Peter Lin 	: "=r" (__ovl) :						\
42bc969d6cSYu Chien Peter Lin 	: "memory")
43bc969d6cSYu Chien Peter Lin 
44bc969d6cSYu Chien Peter Lin #define ALT_SBI_PMU_OVF_CLEAR_PENDING(__irq_mask)			\
45bc969d6cSYu Chien Peter Lin asm volatile(ALTERNATIVE(						\
46bc969d6cSYu Chien Peter Lin 	"csrc " __stringify(CSR_IP) ", %0\n\t",				\
47bc969d6cSYu Chien Peter Lin 	"csrc " __stringify(ANDES_CSR_SLIP) ", %0\n\t",			\
4823c996fcSCharlie Jenkins 		ANDES_VENDOR_ID,					\
4923c996fcSCharlie Jenkins 		RISCV_ISA_VENDOR_EXT_XANDESPMU + RISCV_VENDOR_EXT_ALTERNATIVES_BASE, \
50bc969d6cSYu Chien Peter Lin 		CONFIG_ANDES_CUSTOM_PMU)				\
51bc969d6cSYu Chien Peter Lin 	: : "r"(__irq_mask)						\
52bc969d6cSYu Chien Peter Lin 	: "memory")
53bc969d6cSYu Chien Peter Lin 
54cc4c07c8SAlexandre Ghiti #define SYSCTL_NO_USER_ACCESS	0
55cc4c07c8SAlexandre Ghiti #define SYSCTL_USER_ACCESS	1
56cc4c07c8SAlexandre Ghiti #define SYSCTL_LEGACY		2
57cc4c07c8SAlexandre Ghiti 
58cc4c07c8SAlexandre Ghiti #define PERF_EVENT_FLAG_NO_USER_ACCESS	BIT(SYSCTL_NO_USER_ACCESS)
59cc4c07c8SAlexandre Ghiti #define PERF_EVENT_FLAG_USER_ACCESS	BIT(SYSCTL_USER_ACCESS)
60cc4c07c8SAlexandre Ghiti #define PERF_EVENT_FLAG_LEGACY		BIT(SYSCTL_LEGACY)
61cc4c07c8SAlexandre Ghiti 
6226fabd6dSNikita Shubin PMU_FORMAT_ATTR(event, "config:0-47");
6326fabd6dSNikita Shubin PMU_FORMAT_ATTR(firmware, "config:63");
6426fabd6dSNikita Shubin 
657dda24baSAtish Patra static bool sbi_v2_available;
66a8625217SAtish Patra static DEFINE_STATIC_KEY_FALSE(sbi_pmu_snapshot_available);
67a8625217SAtish Patra #define sbi_pmu_snapshot_available() \
68a8625217SAtish Patra 	static_branch_unlikely(&sbi_pmu_snapshot_available)
697dda24baSAtish Patra 
7026fabd6dSNikita Shubin static struct attribute *riscv_arch_formats_attr[] = {
7126fabd6dSNikita Shubin 	&format_attr_event.attr,
7226fabd6dSNikita Shubin 	&format_attr_firmware.attr,
7326fabd6dSNikita Shubin 	NULL,
7426fabd6dSNikita Shubin };
7526fabd6dSNikita Shubin 
7626fabd6dSNikita Shubin static struct attribute_group riscv_pmu_format_group = {
7726fabd6dSNikita Shubin 	.name = "format",
7826fabd6dSNikita Shubin 	.attrs = riscv_arch_formats_attr,
7926fabd6dSNikita Shubin };
8026fabd6dSNikita Shubin 
8126fabd6dSNikita Shubin static const struct attribute_group *riscv_pmu_attr_groups[] = {
8226fabd6dSNikita Shubin 	&riscv_pmu_format_group,
8326fabd6dSNikita Shubin 	NULL,
8426fabd6dSNikita Shubin };
8526fabd6dSNikita Shubin 
86cc4c07c8SAlexandre Ghiti /* Allow user mode access by default */
87cc4c07c8SAlexandre Ghiti static int sysctl_perf_user_access __read_mostly = SYSCTL_USER_ACCESS;
88cc4c07c8SAlexandre Ghiti 
89c7a9dceaSPalmer Dabbelt /*
90585e351fSAtish Patra  * RISC-V doesn't have heterogeneous harts yet. This need to be part of
91e9991434SAtish Patra  * per_cpu in case of harts with different pmu counters
92e9991434SAtish Patra  */
93e9991434SAtish Patra static union sbi_pmu_ctr_info *pmu_ctr_list;
9465e9fb08SHeiko Stuebner static bool riscv_pmu_use_irq;
9565e9fb08SHeiko Stuebner static unsigned int riscv_pmu_irq_num;
96bc969d6cSYu Chien Peter Lin static unsigned int riscv_pmu_irq_mask;
974905ec2fSAtish Patra static unsigned int riscv_pmu_irq;
98e9991434SAtish Patra 
99585e351fSAtish Patra /* Cache the available counters in a bitmask */
100585e351fSAtish Patra static unsigned long cmask;
101585e351fSAtish Patra 
102e9991434SAtish Patra struct sbi_pmu_event_data {
103e9991434SAtish Patra 	union {
104e9991434SAtish Patra 		union {
105e9991434SAtish Patra 			struct hw_gen_event {
106e9991434SAtish Patra 				uint32_t event_code:16;
107e9991434SAtish Patra 				uint32_t event_type:4;
108e9991434SAtish Patra 				uint32_t reserved:12;
109e9991434SAtish Patra 			} hw_gen_event;
110e9991434SAtish Patra 			struct hw_cache_event {
111e9991434SAtish Patra 				uint32_t result_id:1;
112e9991434SAtish Patra 				uint32_t op_id:2;
113e9991434SAtish Patra 				uint32_t cache_id:13;
114e9991434SAtish Patra 				uint32_t event_type:4;
115e9991434SAtish Patra 				uint32_t reserved:12;
116e9991434SAtish Patra 			} hw_cache_event;
117e9991434SAtish Patra 		};
118e9991434SAtish Patra 		uint32_t event_idx;
119e9991434SAtish Patra 	};
120e9991434SAtish Patra };
121e9991434SAtish Patra 
12216d3b1afSSamuel Holland static struct sbi_pmu_event_data pmu_hw_event_map[] = {
123e9991434SAtish Patra 	[PERF_COUNT_HW_CPU_CYCLES]		= {.hw_gen_event = {
124e9991434SAtish Patra 							SBI_PMU_HW_CPU_CYCLES,
125e9991434SAtish Patra 							SBI_PMU_EVENT_TYPE_HW, 0}},
126e9991434SAtish Patra 	[PERF_COUNT_HW_INSTRUCTIONS]		= {.hw_gen_event = {
127e9991434SAtish Patra 							SBI_PMU_HW_INSTRUCTIONS,
128e9991434SAtish Patra 							SBI_PMU_EVENT_TYPE_HW, 0}},
129e9991434SAtish Patra 	[PERF_COUNT_HW_CACHE_REFERENCES]	= {.hw_gen_event = {
130e9991434SAtish Patra 							SBI_PMU_HW_CACHE_REFERENCES,
131e9991434SAtish Patra 							SBI_PMU_EVENT_TYPE_HW, 0}},
132e9991434SAtish Patra 	[PERF_COUNT_HW_CACHE_MISSES]		= {.hw_gen_event = {
133e9991434SAtish Patra 							SBI_PMU_HW_CACHE_MISSES,
134e9991434SAtish Patra 							SBI_PMU_EVENT_TYPE_HW, 0}},
135e9991434SAtish Patra 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS]	= {.hw_gen_event = {
136e9991434SAtish Patra 							SBI_PMU_HW_BRANCH_INSTRUCTIONS,
137e9991434SAtish Patra 							SBI_PMU_EVENT_TYPE_HW, 0}},
138e9991434SAtish Patra 	[PERF_COUNT_HW_BRANCH_MISSES]		= {.hw_gen_event = {
139e9991434SAtish Patra 							SBI_PMU_HW_BRANCH_MISSES,
140e9991434SAtish Patra 							SBI_PMU_EVENT_TYPE_HW, 0}},
141e9991434SAtish Patra 	[PERF_COUNT_HW_BUS_CYCLES]		= {.hw_gen_event = {
142e9991434SAtish Patra 							SBI_PMU_HW_BUS_CYCLES,
143e9991434SAtish Patra 							SBI_PMU_EVENT_TYPE_HW, 0}},
144e9991434SAtish Patra 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND]	= {.hw_gen_event = {
145e9991434SAtish Patra 							SBI_PMU_HW_STALLED_CYCLES_FRONTEND,
146e9991434SAtish Patra 							SBI_PMU_EVENT_TYPE_HW, 0}},
147e9991434SAtish Patra 	[PERF_COUNT_HW_STALLED_CYCLES_BACKEND]	= {.hw_gen_event = {
148e9991434SAtish Patra 							SBI_PMU_HW_STALLED_CYCLES_BACKEND,
149e9991434SAtish Patra 							SBI_PMU_EVENT_TYPE_HW, 0}},
150e9991434SAtish Patra 	[PERF_COUNT_HW_REF_CPU_CYCLES]		= {.hw_gen_event = {
151e9991434SAtish Patra 							SBI_PMU_HW_REF_CPU_CYCLES,
152e9991434SAtish Patra 							SBI_PMU_EVENT_TYPE_HW, 0}},
153e9991434SAtish Patra };
154e9991434SAtish Patra 
155e9991434SAtish Patra #define C(x) PERF_COUNT_HW_CACHE_##x
15616d3b1afSSamuel Holland static struct sbi_pmu_event_data pmu_cache_event_map[PERF_COUNT_HW_CACHE_MAX]
157e9991434SAtish Patra [PERF_COUNT_HW_CACHE_OP_MAX]
158e9991434SAtish Patra [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
159e9991434SAtish Patra 	[C(L1D)] = {
160e9991434SAtish Patra 		[C(OP_READ)] = {
161e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
162e9991434SAtish Patra 					C(OP_READ), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
163e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
164e9991434SAtish Patra 					C(OP_READ), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
165e9991434SAtish Patra 		},
166e9991434SAtish Patra 		[C(OP_WRITE)] = {
167e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
168e9991434SAtish Patra 					C(OP_WRITE), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
169e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
170e9991434SAtish Patra 					C(OP_WRITE), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
171e9991434SAtish Patra 		},
172e9991434SAtish Patra 		[C(OP_PREFETCH)] = {
173e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
174e9991434SAtish Patra 					C(OP_PREFETCH), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
175e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
176e9991434SAtish Patra 					C(OP_PREFETCH), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
177e9991434SAtish Patra 		},
178e9991434SAtish Patra 	},
179e9991434SAtish Patra 	[C(L1I)] = {
180e9991434SAtish Patra 		[C(OP_READ)] = {
181e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event =	{C(RESULT_ACCESS),
182e9991434SAtish Patra 					C(OP_READ), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
183e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS), C(OP_READ),
184e9991434SAtish Patra 					C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
185e9991434SAtish Patra 		},
186e9991434SAtish Patra 		[C(OP_WRITE)] = {
187e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
188e9991434SAtish Patra 					C(OP_WRITE), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
189e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
190e9991434SAtish Patra 					C(OP_WRITE), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
191e9991434SAtish Patra 		},
192e9991434SAtish Patra 		[C(OP_PREFETCH)] = {
193e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
194e9991434SAtish Patra 					C(OP_PREFETCH), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
195e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
196e9991434SAtish Patra 					C(OP_PREFETCH), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
197e9991434SAtish Patra 		},
198e9991434SAtish Patra 	},
199e9991434SAtish Patra 	[C(LL)] = {
200e9991434SAtish Patra 		[C(OP_READ)] = {
201e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
202e9991434SAtish Patra 					C(OP_READ), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
203e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
204e9991434SAtish Patra 					C(OP_READ), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
205e9991434SAtish Patra 		},
206e9991434SAtish Patra 		[C(OP_WRITE)] = {
207e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
208e9991434SAtish Patra 					C(OP_WRITE), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
209e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
210e9991434SAtish Patra 					C(OP_WRITE), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
211e9991434SAtish Patra 		},
212e9991434SAtish Patra 		[C(OP_PREFETCH)] = {
213e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
214e9991434SAtish Patra 					C(OP_PREFETCH), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
215e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
216e9991434SAtish Patra 					C(OP_PREFETCH), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
217e9991434SAtish Patra 		},
218e9991434SAtish Patra 	},
219e9991434SAtish Patra 	[C(DTLB)] = {
220e9991434SAtish Patra 		[C(OP_READ)] = {
221e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
222e9991434SAtish Patra 					C(OP_READ), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
223e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
224e9991434SAtish Patra 					C(OP_READ), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
225e9991434SAtish Patra 		},
226e9991434SAtish Patra 		[C(OP_WRITE)] = {
227e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
228e9991434SAtish Patra 					C(OP_WRITE), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
229e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
230e9991434SAtish Patra 					C(OP_WRITE), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
231e9991434SAtish Patra 		},
232e9991434SAtish Patra 		[C(OP_PREFETCH)] = {
233e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
234e9991434SAtish Patra 					C(OP_PREFETCH), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
235e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
236e9991434SAtish Patra 					C(OP_PREFETCH), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
237e9991434SAtish Patra 		},
238e9991434SAtish Patra 	},
239e9991434SAtish Patra 	[C(ITLB)] = {
240e9991434SAtish Patra 		[C(OP_READ)] = {
241e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
242e9991434SAtish Patra 					C(OP_READ), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
243e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
244e9991434SAtish Patra 					C(OP_READ), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
245e9991434SAtish Patra 		},
246e9991434SAtish Patra 		[C(OP_WRITE)] = {
247e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
248e9991434SAtish Patra 					C(OP_WRITE), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
249e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
250e9991434SAtish Patra 					C(OP_WRITE), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
251e9991434SAtish Patra 		},
252e9991434SAtish Patra 		[C(OP_PREFETCH)] = {
253e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
254e9991434SAtish Patra 					C(OP_PREFETCH), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
255e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
256e9991434SAtish Patra 					C(OP_PREFETCH), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
257e9991434SAtish Patra 		},
258e9991434SAtish Patra 	},
259e9991434SAtish Patra 	[C(BPU)] = {
260e9991434SAtish Patra 		[C(OP_READ)] = {
261e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
262e9991434SAtish Patra 					C(OP_READ), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
263e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
264e9991434SAtish Patra 					C(OP_READ), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
265e9991434SAtish Patra 		},
266e9991434SAtish Patra 		[C(OP_WRITE)] = {
267e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
268e9991434SAtish Patra 					C(OP_WRITE), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
269e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
270e9991434SAtish Patra 					C(OP_WRITE), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
271e9991434SAtish Patra 		},
272e9991434SAtish Patra 		[C(OP_PREFETCH)] = {
273e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
274e9991434SAtish Patra 					C(OP_PREFETCH), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
275e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
276e9991434SAtish Patra 					C(OP_PREFETCH), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
277e9991434SAtish Patra 		},
278e9991434SAtish Patra 	},
279e9991434SAtish Patra 	[C(NODE)] = {
280e9991434SAtish Patra 		[C(OP_READ)] = {
281e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
282e9991434SAtish Patra 					C(OP_READ), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
283e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
284e9991434SAtish Patra 					C(OP_READ), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
285e9991434SAtish Patra 		},
286e9991434SAtish Patra 		[C(OP_WRITE)] = {
287e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
288e9991434SAtish Patra 					C(OP_WRITE), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
289e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
290e9991434SAtish Patra 					C(OP_WRITE), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
291e9991434SAtish Patra 		},
292e9991434SAtish Patra 		[C(OP_PREFETCH)] = {
293e9991434SAtish Patra 			[C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
294e9991434SAtish Patra 					C(OP_PREFETCH), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
295e9991434SAtish Patra 			[C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
296e9991434SAtish Patra 					C(OP_PREFETCH), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
297e9991434SAtish Patra 		},
298e9991434SAtish Patra 	},
299e9991434SAtish Patra };
300e9991434SAtish Patra 
pmu_sbi_check_event(struct sbi_pmu_event_data * edata)30116d3b1afSSamuel Holland static void pmu_sbi_check_event(struct sbi_pmu_event_data *edata)
30216d3b1afSSamuel Holland {
30316d3b1afSSamuel Holland 	struct sbiret ret;
30416d3b1afSSamuel Holland 
30516d3b1afSSamuel Holland 	ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH,
30616d3b1afSSamuel Holland 			0, cmask, 0, edata->event_idx, 0, 0);
30716d3b1afSSamuel Holland 	if (!ret.error) {
30816d3b1afSSamuel Holland 		sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP,
30916d3b1afSSamuel Holland 			  ret.value, 0x1, SBI_PMU_STOP_FLAG_RESET, 0, 0, 0);
31016d3b1afSSamuel Holland 	} else if (ret.error == SBI_ERR_NOT_SUPPORTED) {
31116d3b1afSSamuel Holland 		/* This event cannot be monitored by any counter */
31216d3b1afSSamuel Holland 		edata->event_idx = -EINVAL;
31316d3b1afSSamuel Holland 	}
31416d3b1afSSamuel Holland }
31516d3b1afSSamuel Holland 
pmu_sbi_check_std_events(struct work_struct * work)31616d3b1afSSamuel Holland static void pmu_sbi_check_std_events(struct work_struct *work)
31716d3b1afSSamuel Holland {
31816d3b1afSSamuel Holland 	for (int i = 0; i < ARRAY_SIZE(pmu_hw_event_map); i++)
31916d3b1afSSamuel Holland 		pmu_sbi_check_event(&pmu_hw_event_map[i]);
32016d3b1afSSamuel Holland 
32116d3b1afSSamuel Holland 	for (int i = 0; i < ARRAY_SIZE(pmu_cache_event_map); i++)
32216d3b1afSSamuel Holland 		for (int j = 0; j < ARRAY_SIZE(pmu_cache_event_map[i]); j++)
32316d3b1afSSamuel Holland 			for (int k = 0; k < ARRAY_SIZE(pmu_cache_event_map[i][j]); k++)
32416d3b1afSSamuel Holland 				pmu_sbi_check_event(&pmu_cache_event_map[i][j][k]);
32516d3b1afSSamuel Holland }
32616d3b1afSSamuel Holland 
32716d3b1afSSamuel Holland static DECLARE_WORK(check_std_events_work, pmu_sbi_check_std_events);
32816d3b1afSSamuel Holland 
pmu_sbi_ctr_get_width(int idx)329e9991434SAtish Patra static int pmu_sbi_ctr_get_width(int idx)
330e9991434SAtish Patra {
331e9991434SAtish Patra 	return pmu_ctr_list[idx].width;
332e9991434SAtish Patra }
333e9991434SAtish Patra 
pmu_sbi_ctr_is_fw(int cidx)334e9991434SAtish Patra static bool pmu_sbi_ctr_is_fw(int cidx)
335e9991434SAtish Patra {
336e9991434SAtish Patra 	union sbi_pmu_ctr_info *info;
337e9991434SAtish Patra 
338e9991434SAtish Patra 	info = &pmu_ctr_list[cidx];
339e9991434SAtish Patra 	if (!info)
340e9991434SAtish Patra 		return false;
341e9991434SAtish Patra 
342e9991434SAtish Patra 	return (info->type == SBI_PMU_CTR_TYPE_FW) ? true : false;
343e9991434SAtish Patra }
344e9991434SAtish Patra 
345585e351fSAtish Patra /*
346585e351fSAtish Patra  * Returns the counter width of a programmable counter and number of hardware
347585e351fSAtish Patra  * counters. As we don't support heterogeneous CPUs yet, it is okay to just
348585e351fSAtish Patra  * return the counter width of the first programmable counter.
349585e351fSAtish Patra  */
riscv_pmu_get_hpm_info(u32 * hw_ctr_width,u32 * num_hw_ctr)350585e351fSAtish Patra int riscv_pmu_get_hpm_info(u32 *hw_ctr_width, u32 *num_hw_ctr)
351585e351fSAtish Patra {
352585e351fSAtish Patra 	int i;
353585e351fSAtish Patra 	union sbi_pmu_ctr_info *info;
354585e351fSAtish Patra 	u32 hpm_width = 0, hpm_count = 0;
355585e351fSAtish Patra 
356585e351fSAtish Patra 	if (!cmask)
357585e351fSAtish Patra 		return -EINVAL;
358585e351fSAtish Patra 
359585e351fSAtish Patra 	for_each_set_bit(i, &cmask, RISCV_MAX_COUNTERS) {
360585e351fSAtish Patra 		info = &pmu_ctr_list[i];
361585e351fSAtish Patra 		if (!info)
362585e351fSAtish Patra 			continue;
363585e351fSAtish Patra 		if (!hpm_width && info->csr != CSR_CYCLE && info->csr != CSR_INSTRET)
364585e351fSAtish Patra 			hpm_width = info->width;
365585e351fSAtish Patra 		if (info->type == SBI_PMU_CTR_TYPE_HW)
366585e351fSAtish Patra 			hpm_count++;
367585e351fSAtish Patra 	}
368585e351fSAtish Patra 
369585e351fSAtish Patra 	*hw_ctr_width = hpm_width;
370585e351fSAtish Patra 	*num_hw_ctr = hpm_count;
371585e351fSAtish Patra 
372585e351fSAtish Patra 	return 0;
373585e351fSAtish Patra }
374585e351fSAtish Patra EXPORT_SYMBOL_GPL(riscv_pmu_get_hpm_info);
375585e351fSAtish Patra 
pmu_sbi_csr_index(struct perf_event * event)376cc4c07c8SAlexandre Ghiti static uint8_t pmu_sbi_csr_index(struct perf_event *event)
377cc4c07c8SAlexandre Ghiti {
378cc4c07c8SAlexandre Ghiti 	return pmu_ctr_list[event->hw.idx].csr - CSR_CYCLE;
379cc4c07c8SAlexandre Ghiti }
380cc4c07c8SAlexandre Ghiti 
pmu_sbi_get_filter_flags(struct perf_event * event)3818929283aSAtish Patra static unsigned long pmu_sbi_get_filter_flags(struct perf_event *event)
3828929283aSAtish Patra {
3838929283aSAtish Patra 	unsigned long cflags = 0;
3848929283aSAtish Patra 	bool guest_events = false;
3858929283aSAtish Patra 
3868929283aSAtish Patra 	if (event->attr.config1 & RISCV_PMU_CONFIG1_GUEST_EVENTS)
3878929283aSAtish Patra 		guest_events = true;
3888929283aSAtish Patra 	if (event->attr.exclude_kernel)
3898929283aSAtish Patra 		cflags |= guest_events ? SBI_PMU_CFG_FLAG_SET_VSINH : SBI_PMU_CFG_FLAG_SET_SINH;
3908929283aSAtish Patra 	if (event->attr.exclude_user)
3918929283aSAtish Patra 		cflags |= guest_events ? SBI_PMU_CFG_FLAG_SET_VUINH : SBI_PMU_CFG_FLAG_SET_UINH;
3928929283aSAtish Patra 	if (guest_events && event->attr.exclude_hv)
3938929283aSAtish Patra 		cflags |= SBI_PMU_CFG_FLAG_SET_SINH;
3948929283aSAtish Patra 	if (event->attr.exclude_host)
3958929283aSAtish Patra 		cflags |= SBI_PMU_CFG_FLAG_SET_UINH | SBI_PMU_CFG_FLAG_SET_SINH;
3968929283aSAtish Patra 	if (event->attr.exclude_guest)
3978929283aSAtish Patra 		cflags |= SBI_PMU_CFG_FLAG_SET_VSINH | SBI_PMU_CFG_FLAG_SET_VUINH;
3988929283aSAtish Patra 
3998929283aSAtish Patra 	return cflags;
4008929283aSAtish Patra }
4018929283aSAtish Patra 
pmu_sbi_ctr_get_idx(struct perf_event * event)402e9991434SAtish Patra static int pmu_sbi_ctr_get_idx(struct perf_event *event)
403e9991434SAtish Patra {
404e9991434SAtish Patra 	struct hw_perf_event *hwc = &event->hw;
405e9991434SAtish Patra 	struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
406e9991434SAtish Patra 	struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
407e9991434SAtish Patra 	struct sbiret ret;
408e9991434SAtish Patra 	int idx;
409cc4c07c8SAlexandre Ghiti 	uint64_t cbase = 0, cmask = rvpmu->cmask;
410e9991434SAtish Patra 	unsigned long cflags = 0;
411e9991434SAtish Patra 
4128929283aSAtish Patra 	cflags = pmu_sbi_get_filter_flags(event);
413cc4c07c8SAlexandre Ghiti 
414cc4c07c8SAlexandre Ghiti 	/*
415cc4c07c8SAlexandre Ghiti 	 * In legacy mode, we have to force the fixed counters for those events
416cc4c07c8SAlexandre Ghiti 	 * but not in the user access mode as we want to use the other counters
417cc4c07c8SAlexandre Ghiti 	 * that support sampling/filtering.
418cc4c07c8SAlexandre Ghiti 	 */
419941a8e9bSShifrin Dmitry 	if ((hwc->flags & PERF_EVENT_FLAG_LEGACY) && (event->attr.type == PERF_TYPE_HARDWARE)) {
420cc4c07c8SAlexandre Ghiti 		if (event->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
421cc4c07c8SAlexandre Ghiti 			cflags |= SBI_PMU_CFG_FLAG_SKIP_MATCH;
422cc4c07c8SAlexandre Ghiti 			cmask = 1;
423cc4c07c8SAlexandre Ghiti 		} else if (event->attr.config == PERF_COUNT_HW_INSTRUCTIONS) {
424cc4c07c8SAlexandre Ghiti 			cflags |= SBI_PMU_CFG_FLAG_SKIP_MATCH;
425c69f9cb0SAtish Patra 			cmask = BIT(CSR_INSTRET - CSR_CYCLE);
426cc4c07c8SAlexandre Ghiti 		}
427cc4c07c8SAlexandre Ghiti 	}
428cc4c07c8SAlexandre Ghiti 
429e9991434SAtish Patra 	/* retrieve the available counter index */
4300209b583SAtish Patra #if defined(CONFIG_32BIT)
4311537bf26SSergey Matyukevich 	ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase,
432cc4c07c8SAlexandre Ghiti 			cmask, cflags, hwc->event_base, hwc->config,
4331537bf26SSergey Matyukevich 			hwc->config >> 32);
4340209b583SAtish Patra #else
4351537bf26SSergey Matyukevich 	ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase,
436cc4c07c8SAlexandre Ghiti 			cmask, cflags, hwc->event_base, hwc->config, 0);
4370209b583SAtish Patra #endif
438e9991434SAtish Patra 	if (ret.error) {
439e9991434SAtish Patra 		pr_debug("Not able to find a counter for event %lx config %llx\n",
440e9991434SAtish Patra 			hwc->event_base, hwc->config);
441e9991434SAtish Patra 		return sbi_err_map_linux_errno(ret.error);
442e9991434SAtish Patra 	}
443e9991434SAtish Patra 
444e9991434SAtish Patra 	idx = ret.value;
4451537bf26SSergey Matyukevich 	if (!test_bit(idx, &rvpmu->cmask) || !pmu_ctr_list[idx].value)
446e9991434SAtish Patra 		return -ENOENT;
447e9991434SAtish Patra 
448e9991434SAtish Patra 	/* Additional sanity check for the counter id */
449e9991434SAtish Patra 	if (pmu_sbi_ctr_is_fw(idx)) {
450e9991434SAtish Patra 		if (!test_and_set_bit(idx, cpuc->used_fw_ctrs))
451e9991434SAtish Patra 			return idx;
452e9991434SAtish Patra 	} else {
453e9991434SAtish Patra 		if (!test_and_set_bit(idx, cpuc->used_hw_ctrs))
454e9991434SAtish Patra 			return idx;
455e9991434SAtish Patra 	}
456e9991434SAtish Patra 
457e9991434SAtish Patra 	return -ENOENT;
458e9991434SAtish Patra }
459e9991434SAtish Patra 
pmu_sbi_ctr_clear_idx(struct perf_event * event)460e9991434SAtish Patra static void pmu_sbi_ctr_clear_idx(struct perf_event *event)
461e9991434SAtish Patra {
462e9991434SAtish Patra 
463e9991434SAtish Patra 	struct hw_perf_event *hwc = &event->hw;
464e9991434SAtish Patra 	struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
465e9991434SAtish Patra 	struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
466e9991434SAtish Patra 	int idx = hwc->idx;
467e9991434SAtish Patra 
468e9991434SAtish Patra 	if (pmu_sbi_ctr_is_fw(idx))
469e9991434SAtish Patra 		clear_bit(idx, cpuc->used_fw_ctrs);
470e9991434SAtish Patra 	else
471e9991434SAtish Patra 		clear_bit(idx, cpuc->used_hw_ctrs);
472e9991434SAtish Patra }
473e9991434SAtish Patra 
pmu_event_find_cache(u64 config)474e9991434SAtish Patra static int pmu_event_find_cache(u64 config)
475e9991434SAtish Patra {
476e9991434SAtish Patra 	unsigned int cache_type, cache_op, cache_result, ret;
477e9991434SAtish Patra 
478e9991434SAtish Patra 	cache_type = (config >>  0) & 0xff;
479e9991434SAtish Patra 	if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
480e9991434SAtish Patra 		return -EINVAL;
481e9991434SAtish Patra 
482e9991434SAtish Patra 	cache_op = (config >>  8) & 0xff;
483e9991434SAtish Patra 	if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
484e9991434SAtish Patra 		return -EINVAL;
485e9991434SAtish Patra 
486e9991434SAtish Patra 	cache_result = (config >> 16) & 0xff;
487e9991434SAtish Patra 	if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
488e9991434SAtish Patra 		return -EINVAL;
489e9991434SAtish Patra 
490e9991434SAtish Patra 	ret = pmu_cache_event_map[cache_type][cache_op][cache_result].event_idx;
491e9991434SAtish Patra 
492e9991434SAtish Patra 	return ret;
493e9991434SAtish Patra }
494e9991434SAtish Patra 
pmu_sbi_is_fw_event(struct perf_event * event)495e9991434SAtish Patra static bool pmu_sbi_is_fw_event(struct perf_event *event)
496e9991434SAtish Patra {
497e9991434SAtish Patra 	u32 type = event->attr.type;
498e9991434SAtish Patra 	u64 config = event->attr.config;
499e9991434SAtish Patra 
500e9991434SAtish Patra 	if ((type == PERF_TYPE_RAW) && ((config >> 63) == 1))
501e9991434SAtish Patra 		return true;
502e9991434SAtish Patra 	else
503e9991434SAtish Patra 		return false;
504e9991434SAtish Patra }
505e9991434SAtish Patra 
pmu_sbi_event_map(struct perf_event * event,u64 * econfig)506e9991434SAtish Patra static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
507e9991434SAtish Patra {
508e9991434SAtish Patra 	u32 type = event->attr.type;
509e9991434SAtish Patra 	u64 config = event->attr.config;
510e9991434SAtish Patra 	int bSoftware;
511e9991434SAtish Patra 	u64 raw_config_val;
512e9991434SAtish Patra 	int ret;
513e9991434SAtish Patra 
51416d3b1afSSamuel Holland 	/*
51516d3b1afSSamuel Holland 	 * Ensure we are finished checking standard hardware events for
51616d3b1afSSamuel Holland 	 * validity before allowing userspace to configure any events.
51716d3b1afSSamuel Holland 	 */
51816d3b1afSSamuel Holland 	flush_work(&check_std_events_work);
51916d3b1afSSamuel Holland 
520e9991434SAtish Patra 	switch (type) {
521e9991434SAtish Patra 	case PERF_TYPE_HARDWARE:
522e9991434SAtish Patra 		if (config >= PERF_COUNT_HW_MAX)
523e9991434SAtish Patra 			return -EINVAL;
524e9991434SAtish Patra 		ret = pmu_hw_event_map[event->attr.config].event_idx;
525e9991434SAtish Patra 		break;
526e9991434SAtish Patra 	case PERF_TYPE_HW_CACHE:
527e9991434SAtish Patra 		ret = pmu_event_find_cache(config);
528e9991434SAtish Patra 		break;
529e9991434SAtish Patra 	case PERF_TYPE_RAW:
530e9991434SAtish Patra 		/*
531e9991434SAtish Patra 		 * As per SBI specification, the upper 16 bits must be unused for
532e9991434SAtish Patra 		 * a raw event. Use the MSB (63b) to distinguish between hardware
533e9991434SAtish Patra 		 * raw event and firmware events.
534e9991434SAtish Patra 		 */
535e9991434SAtish Patra 		bSoftware = config >> 63;
536e9991434SAtish Patra 		raw_config_val = config & RISCV_PMU_RAW_EVENT_MASK;
537e9991434SAtish Patra 		if (bSoftware) {
538e9991434SAtish Patra 			ret = (raw_config_val & 0xFFFF) |
539e9991434SAtish Patra 				(SBI_PMU_EVENT_TYPE_FW << 16);
540e9991434SAtish Patra 		} else {
541e9991434SAtish Patra 			ret = RISCV_PMU_RAW_EVENT_IDX;
542e9991434SAtish Patra 			*econfig = raw_config_val;
543e9991434SAtish Patra 		}
544e9991434SAtish Patra 		break;
545e9991434SAtish Patra 	default:
546e9991434SAtish Patra 		ret = -EINVAL;
547e9991434SAtish Patra 		break;
548e9991434SAtish Patra 	}
549e9991434SAtish Patra 
550e9991434SAtish Patra 	return ret;
551e9991434SAtish Patra }
552e9991434SAtish Patra 
pmu_sbi_snapshot_free(struct riscv_pmu * pmu)553a8625217SAtish Patra static void pmu_sbi_snapshot_free(struct riscv_pmu *pmu)
554a8625217SAtish Patra {
555a8625217SAtish Patra 	int cpu;
556a8625217SAtish Patra 
557a8625217SAtish Patra 	for_each_possible_cpu(cpu) {
558a8625217SAtish Patra 		struct cpu_hw_events *cpu_hw_evt = per_cpu_ptr(pmu->hw_events, cpu);
559a8625217SAtish Patra 
560a8625217SAtish Patra 		if (!cpu_hw_evt->snapshot_addr)
561a8625217SAtish Patra 			continue;
562a8625217SAtish Patra 
563a8625217SAtish Patra 		free_page((unsigned long)cpu_hw_evt->snapshot_addr);
564a8625217SAtish Patra 		cpu_hw_evt->snapshot_addr = NULL;
565a8625217SAtish Patra 		cpu_hw_evt->snapshot_addr_phys = 0;
566a8625217SAtish Patra 	}
567a8625217SAtish Patra }
568a8625217SAtish Patra 
pmu_sbi_snapshot_alloc(struct riscv_pmu * pmu)569a8625217SAtish Patra static int pmu_sbi_snapshot_alloc(struct riscv_pmu *pmu)
570a8625217SAtish Patra {
571a8625217SAtish Patra 	int cpu;
572a8625217SAtish Patra 	struct page *snapshot_page;
573a8625217SAtish Patra 
574a8625217SAtish Patra 	for_each_possible_cpu(cpu) {
575a8625217SAtish Patra 		struct cpu_hw_events *cpu_hw_evt = per_cpu_ptr(pmu->hw_events, cpu);
576a8625217SAtish Patra 
577a8625217SAtish Patra 		snapshot_page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
578a8625217SAtish Patra 		if (!snapshot_page) {
579a8625217SAtish Patra 			pmu_sbi_snapshot_free(pmu);
580a8625217SAtish Patra 			return -ENOMEM;
581a8625217SAtish Patra 		}
582a8625217SAtish Patra 		cpu_hw_evt->snapshot_addr = page_to_virt(snapshot_page);
583a8625217SAtish Patra 		cpu_hw_evt->snapshot_addr_phys = page_to_phys(snapshot_page);
584a8625217SAtish Patra 	}
585a8625217SAtish Patra 
586a8625217SAtish Patra 	return 0;
587a8625217SAtish Patra }
588a8625217SAtish Patra 
pmu_sbi_snapshot_disable(void)589a8625217SAtish Patra static int pmu_sbi_snapshot_disable(void)
590a8625217SAtish Patra {
591a8625217SAtish Patra 	struct sbiret ret;
592a8625217SAtish Patra 
593a8625217SAtish Patra 	ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_SNAPSHOT_SET_SHMEM, SBI_SHMEM_DISABLE,
594a8625217SAtish Patra 			SBI_SHMEM_DISABLE, 0, 0, 0, 0);
595a8625217SAtish Patra 	if (ret.error) {
596a8625217SAtish Patra 		pr_warn("failed to disable snapshot shared memory\n");
597a8625217SAtish Patra 		return sbi_err_map_linux_errno(ret.error);
598a8625217SAtish Patra 	}
599a8625217SAtish Patra 
600a8625217SAtish Patra 	return 0;
601a8625217SAtish Patra }
602a8625217SAtish Patra 
pmu_sbi_snapshot_setup(struct riscv_pmu * pmu,int cpu)603a8625217SAtish Patra static int pmu_sbi_snapshot_setup(struct riscv_pmu *pmu, int cpu)
604a8625217SAtish Patra {
605a8625217SAtish Patra 	struct cpu_hw_events *cpu_hw_evt;
606a8625217SAtish Patra 	struct sbiret ret = {0};
607a8625217SAtish Patra 
608a8625217SAtish Patra 	cpu_hw_evt = per_cpu_ptr(pmu->hw_events, cpu);
609a8625217SAtish Patra 	if (!cpu_hw_evt->snapshot_addr_phys)
610a8625217SAtish Patra 		return -EINVAL;
611a8625217SAtish Patra 
612a8625217SAtish Patra 	if (cpu_hw_evt->snapshot_set_done)
613a8625217SAtish Patra 		return 0;
614a8625217SAtish Patra 
615a8625217SAtish Patra 	if (IS_ENABLED(CONFIG_32BIT))
616a8625217SAtish Patra 		ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_SNAPSHOT_SET_SHMEM,
617a8625217SAtish Patra 				cpu_hw_evt->snapshot_addr_phys,
618a8625217SAtish Patra 				(u64)(cpu_hw_evt->snapshot_addr_phys) >> 32, 0, 0, 0, 0);
619a8625217SAtish Patra 	else
620a8625217SAtish Patra 		ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_SNAPSHOT_SET_SHMEM,
621a8625217SAtish Patra 				cpu_hw_evt->snapshot_addr_phys, 0, 0, 0, 0, 0);
622a8625217SAtish Patra 
623a8625217SAtish Patra 	/* Free up the snapshot area memory and fall back to SBI PMU calls without snapshot */
624a8625217SAtish Patra 	if (ret.error) {
625a8625217SAtish Patra 		if (ret.error != SBI_ERR_NOT_SUPPORTED)
626a8625217SAtish Patra 			pr_warn("pmu snapshot setup failed with error %ld\n", ret.error);
627a8625217SAtish Patra 		return sbi_err_map_linux_errno(ret.error);
628a8625217SAtish Patra 	}
629a8625217SAtish Patra 
630a8625217SAtish Patra 	memset(cpu_hw_evt->snapshot_cval_shcopy, 0, sizeof(u64) * RISCV_MAX_COUNTERS);
631a8625217SAtish Patra 	cpu_hw_evt->snapshot_set_done = true;
632a8625217SAtish Patra 
633a8625217SAtish Patra 	return 0;
634a8625217SAtish Patra }
635a8625217SAtish Patra 
pmu_sbi_ctr_read(struct perf_event * event)636e9991434SAtish Patra static u64 pmu_sbi_ctr_read(struct perf_event *event)
637e9991434SAtish Patra {
638e9991434SAtish Patra 	struct hw_perf_event *hwc = &event->hw;
639e9991434SAtish Patra 	int idx = hwc->idx;
640e9991434SAtish Patra 	struct sbiret ret;
641e9991434SAtish Patra 	u64 val = 0;
642a8625217SAtish Patra 	struct riscv_pmu *pmu = to_riscv_pmu(event->pmu);
643a8625217SAtish Patra 	struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
644a8625217SAtish Patra 	struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
6457dda24baSAtish Patra 	union sbi_pmu_ctr_info info = pmu_ctr_list[idx];
646e9991434SAtish Patra 
647a8625217SAtish Patra 	/* Read the value from the shared memory directly only if counter is stopped */
648a8625217SAtish Patra 	if (sbi_pmu_snapshot_available() && (hwc->state & PERF_HES_STOPPED)) {
649a8625217SAtish Patra 		val = sdata->ctr_values[idx];
650a8625217SAtish Patra 		return val;
651a8625217SAtish Patra 	}
652e9991434SAtish Patra 
653e9991434SAtish Patra 	if (pmu_sbi_is_fw_event(event)) {
654e9991434SAtish Patra 		ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_FW_READ,
655e9991434SAtish Patra 				hwc->idx, 0, 0, 0, 0, 0);
6567dda24baSAtish Patra 		if (ret.error)
6577dda24baSAtish Patra 			return 0;
6587dda24baSAtish Patra 
659e9991434SAtish Patra 		val = ret.value;
6607dda24baSAtish Patra 		if (IS_ENABLED(CONFIG_32BIT) && sbi_v2_available && info.width >= 32) {
6617dda24baSAtish Patra 			ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_FW_READ_HI,
6627dda24baSAtish Patra 					hwc->idx, 0, 0, 0, 0, 0);
6637dda24baSAtish Patra 			if (!ret.error)
6647dda24baSAtish Patra 				val |= ((u64)ret.value << 32);
6657dda24baSAtish Patra 			else
6667dda24baSAtish Patra 				WARN_ONCE(1, "Unable to read upper 32 bits of firmware counter error: %ld\n",
6677dda24baSAtish Patra 					  ret.error);
6687dda24baSAtish Patra 		}
669e9991434SAtish Patra 	} else {
670e9991434SAtish Patra 		val = riscv_pmu_ctr_read_csr(info.csr);
671e9991434SAtish Patra 		if (IS_ENABLED(CONFIG_32BIT))
6727dda24baSAtish Patra 			val |= ((u64)riscv_pmu_ctr_read_csr(info.csr + 0x80)) << 32;
673e9991434SAtish Patra 	}
674e9991434SAtish Patra 
675e9991434SAtish Patra 	return val;
676e9991434SAtish Patra }
677e9991434SAtish Patra 
pmu_sbi_set_scounteren(void * arg)678cc4c07c8SAlexandre Ghiti static void pmu_sbi_set_scounteren(void *arg)
679cc4c07c8SAlexandre Ghiti {
680cc4c07c8SAlexandre Ghiti 	struct perf_event *event = (struct perf_event *)arg;
681cc4c07c8SAlexandre Ghiti 
6823fec3233SAlexandre Ghiti 	if (event->hw.idx != -1)
683cc4c07c8SAlexandre Ghiti 		csr_write(CSR_SCOUNTEREN,
68434b56786SFei Wu 			  csr_read(CSR_SCOUNTEREN) | BIT(pmu_sbi_csr_index(event)));
685cc4c07c8SAlexandre Ghiti }
686cc4c07c8SAlexandre Ghiti 
pmu_sbi_reset_scounteren(void * arg)687cc4c07c8SAlexandre Ghiti static void pmu_sbi_reset_scounteren(void *arg)
688cc4c07c8SAlexandre Ghiti {
689cc4c07c8SAlexandre Ghiti 	struct perf_event *event = (struct perf_event *)arg;
690cc4c07c8SAlexandre Ghiti 
6913fec3233SAlexandre Ghiti 	if (event->hw.idx != -1)
692cc4c07c8SAlexandre Ghiti 		csr_write(CSR_SCOUNTEREN,
69334b56786SFei Wu 			  csr_read(CSR_SCOUNTEREN) & ~BIT(pmu_sbi_csr_index(event)));
694cc4c07c8SAlexandre Ghiti }
695cc4c07c8SAlexandre Ghiti 
pmu_sbi_ctr_start(struct perf_event * event,u64 ival)696e9991434SAtish Patra static void pmu_sbi_ctr_start(struct perf_event *event, u64 ival)
697e9991434SAtish Patra {
698e9991434SAtish Patra 	struct sbiret ret;
699e9991434SAtish Patra 	struct hw_perf_event *hwc = &event->hw;
700e9991434SAtish Patra 	unsigned long flag = SBI_PMU_START_FLAG_SET_INIT_VALUE;
701e9991434SAtish Patra 
702a8625217SAtish Patra 	/* There is no benefit setting SNAPSHOT FLAG for a single counter */
7030209b583SAtish Patra #if defined(CONFIG_32BIT)
704e9991434SAtish Patra 	ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
705e9991434SAtish Patra 			1, flag, ival, ival >> 32, 0);
7060209b583SAtish Patra #else
7070209b583SAtish Patra 	ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
7080209b583SAtish Patra 			1, flag, ival, 0, 0);
7090209b583SAtish Patra #endif
710e9991434SAtish Patra 	if (ret.error && (ret.error != SBI_ERR_ALREADY_STARTED))
711e9991434SAtish Patra 		pr_err("Starting counter idx %d failed with error %d\n",
712e9991434SAtish Patra 			hwc->idx, sbi_err_map_linux_errno(ret.error));
713cc4c07c8SAlexandre Ghiti 
714cc4c07c8SAlexandre Ghiti 	if ((hwc->flags & PERF_EVENT_FLAG_USER_ACCESS) &&
715cc4c07c8SAlexandre Ghiti 	    (hwc->flags & PERF_EVENT_FLAG_USER_READ_CNT))
716cc4c07c8SAlexandre Ghiti 		pmu_sbi_set_scounteren((void *)event);
717e9991434SAtish Patra }
718e9991434SAtish Patra 
pmu_sbi_ctr_stop(struct perf_event * event,unsigned long flag)719e9991434SAtish Patra static void pmu_sbi_ctr_stop(struct perf_event *event, unsigned long flag)
720e9991434SAtish Patra {
721e9991434SAtish Patra 	struct sbiret ret;
722e9991434SAtish Patra 	struct hw_perf_event *hwc = &event->hw;
723a8625217SAtish Patra 	struct riscv_pmu *pmu = to_riscv_pmu(event->pmu);
724a8625217SAtish Patra 	struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
725a8625217SAtish Patra 	struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
726e9991434SAtish Patra 
727cc4c07c8SAlexandre Ghiti 	if ((hwc->flags & PERF_EVENT_FLAG_USER_ACCESS) &&
728cc4c07c8SAlexandre Ghiti 	    (hwc->flags & PERF_EVENT_FLAG_USER_READ_CNT))
729cc4c07c8SAlexandre Ghiti 		pmu_sbi_reset_scounteren((void *)event);
730cc4c07c8SAlexandre Ghiti 
731a8625217SAtish Patra 	if (sbi_pmu_snapshot_available())
732a8625217SAtish Patra 		flag |= SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT;
733a8625217SAtish Patra 
734e9991434SAtish Patra 	ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, hwc->idx, 1, flag, 0, 0, 0);
735a8625217SAtish Patra 	if (!ret.error && sbi_pmu_snapshot_available()) {
736a8625217SAtish Patra 		/*
737a8625217SAtish Patra 		 * The counter snapshot is based on the index base specified by hwc->idx.
738a8625217SAtish Patra 		 * The actual counter value is updated in shared memory at index 0 when counter
739a8625217SAtish Patra 		 * mask is 0x01. To ensure accurate counter values, it's necessary to transfer
740a8625217SAtish Patra 		 * the counter value to shared memory. However, if hwc->idx is zero, the counter
741a8625217SAtish Patra 		 * value is already correctly updated in shared memory, requiring no further
742a8625217SAtish Patra 		 * adjustment.
743a8625217SAtish Patra 		 */
744a8625217SAtish Patra 		if (hwc->idx > 0) {
745a8625217SAtish Patra 			sdata->ctr_values[hwc->idx] = sdata->ctr_values[0];
746a8625217SAtish Patra 			sdata->ctr_values[0] = 0;
747a8625217SAtish Patra 		}
748a8625217SAtish Patra 	} else if (ret.error && (ret.error != SBI_ERR_ALREADY_STOPPED) &&
749a8625217SAtish Patra 		flag != SBI_PMU_STOP_FLAG_RESET) {
750e9991434SAtish Patra 		pr_err("Stopping counter idx %d failed with error %d\n",
751e9991434SAtish Patra 			hwc->idx, sbi_err_map_linux_errno(ret.error));
752e9991434SAtish Patra 	}
753a8625217SAtish Patra }
754e9991434SAtish Patra 
pmu_sbi_find_num_ctrs(void)755e9991434SAtish Patra static int pmu_sbi_find_num_ctrs(void)
756e9991434SAtish Patra {
757e9991434SAtish Patra 	struct sbiret ret;
758e9991434SAtish Patra 
759e9991434SAtish Patra 	ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_NUM_COUNTERS, 0, 0, 0, 0, 0, 0);
760e9991434SAtish Patra 	if (!ret.error)
761e9991434SAtish Patra 		return ret.value;
762e9991434SAtish Patra 	else
763e9991434SAtish Patra 		return sbi_err_map_linux_errno(ret.error);
764e9991434SAtish Patra }
765e9991434SAtish Patra 
pmu_sbi_get_ctrinfo(int nctr,unsigned long * mask)7661537bf26SSergey Matyukevich static int pmu_sbi_get_ctrinfo(int nctr, unsigned long *mask)
767e9991434SAtish Patra {
768e9991434SAtish Patra 	struct sbiret ret;
769e9991434SAtish Patra 	int i, num_hw_ctr = 0, num_fw_ctr = 0;
770e9991434SAtish Patra 	union sbi_pmu_ctr_info cinfo;
771e9991434SAtish Patra 
772e9991434SAtish Patra 	pmu_ctr_list = kcalloc(nctr, sizeof(*pmu_ctr_list), GFP_KERNEL);
773e9991434SAtish Patra 	if (!pmu_ctr_list)
774e9991434SAtish Patra 		return -ENOMEM;
775e9991434SAtish Patra 
77620e0fbabSSergey Matyukevich 	for (i = 0; i < nctr; i++) {
777e9991434SAtish Patra 		ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_GET_INFO, i, 0, 0, 0, 0, 0);
778e9991434SAtish Patra 		if (ret.error)
779e9991434SAtish Patra 			/* The logical counter ids are not expected to be contiguous */
780e9991434SAtish Patra 			continue;
7811537bf26SSergey Matyukevich 
7821537bf26SSergey Matyukevich 		*mask |= BIT(i);
7831537bf26SSergey Matyukevich 
784e9991434SAtish Patra 		cinfo.value = ret.value;
785e9991434SAtish Patra 		if (cinfo.type == SBI_PMU_CTR_TYPE_FW)
786e9991434SAtish Patra 			num_fw_ctr++;
787e9991434SAtish Patra 		else
788e9991434SAtish Patra 			num_hw_ctr++;
789e9991434SAtish Patra 		pmu_ctr_list[i].value = cinfo.value;
790e9991434SAtish Patra 	}
791e9991434SAtish Patra 
792e9991434SAtish Patra 	pr_info("%d firmware and %d hardware counters\n", num_fw_ctr, num_hw_ctr);
793e9991434SAtish Patra 
794e9991434SAtish Patra 	return 0;
795e9991434SAtish Patra }
796e9991434SAtish Patra 
pmu_sbi_stop_all(struct riscv_pmu * pmu)7974905ec2fSAtish Patra static inline void pmu_sbi_stop_all(struct riscv_pmu *pmu)
7984905ec2fSAtish Patra {
799c7a9dceaSPalmer Dabbelt 	/*
8004905ec2fSAtish Patra 	 * No need to check the error because we are disabling all the counters
8014905ec2fSAtish Patra 	 * which may include counters that are not enabled yet.
8024905ec2fSAtish Patra 	 */
8034905ec2fSAtish Patra 	sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP,
8047dd646cfSSamuel Holland 		  0, pmu->cmask, SBI_PMU_STOP_FLAG_RESET, 0, 0, 0);
8054905ec2fSAtish Patra }
8064905ec2fSAtish Patra 
pmu_sbi_stop_hw_ctrs(struct riscv_pmu * pmu)8074905ec2fSAtish Patra static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu)
8084905ec2fSAtish Patra {
8094905ec2fSAtish Patra 	struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
810a8625217SAtish Patra 	struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
811a8625217SAtish Patra 	unsigned long flag = 0;
812a8625217SAtish Patra 	int i, idx;
813a8625217SAtish Patra 	struct sbiret ret;
814a8625217SAtish Patra 	u64 temp_ctr_overflow_mask = 0;
8154905ec2fSAtish Patra 
816a8625217SAtish Patra 	if (sbi_pmu_snapshot_available())
817a8625217SAtish Patra 		flag = SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT;
818a8625217SAtish Patra 
819a8625217SAtish Patra 	/* Reset the shadow copy to avoid save/restore any value from previous overflow */
820a8625217SAtish Patra 	memset(cpu_hw_evt->snapshot_cval_shcopy, 0, sizeof(u64) * RISCV_MAX_COUNTERS);
821a8625217SAtish Patra 
822a8625217SAtish Patra 	for (i = 0; i < BITS_TO_LONGS(RISCV_MAX_COUNTERS); i++) {
8234905ec2fSAtish Patra 		/* No need to check the error here as we can't do anything about the error */
824a8625217SAtish Patra 		ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, i * BITS_PER_LONG,
825a8625217SAtish Patra 				cpu_hw_evt->used_hw_ctrs[i], flag, 0, 0, 0);
826a8625217SAtish Patra 		if (!ret.error && sbi_pmu_snapshot_available()) {
827a8625217SAtish Patra 			/* Save the counter values to avoid clobbering */
828a8625217SAtish Patra 			for_each_set_bit(idx, &cpu_hw_evt->used_hw_ctrs[i], BITS_PER_LONG)
829a8625217SAtish Patra 				cpu_hw_evt->snapshot_cval_shcopy[i * BITS_PER_LONG + idx] =
830a8625217SAtish Patra 							sdata->ctr_values[idx];
831a8625217SAtish Patra 			/* Save the overflow mask to avoid clobbering */
832a8625217SAtish Patra 			temp_ctr_overflow_mask |= sdata->ctr_overflow_mask << (i * BITS_PER_LONG);
833a8625217SAtish Patra 		}
834a8625217SAtish Patra 	}
835a8625217SAtish Patra 
836a8625217SAtish Patra 	/* Restore the counter values to the shared memory for used hw counters */
837a8625217SAtish Patra 	if (sbi_pmu_snapshot_available()) {
838a8625217SAtish Patra 		for_each_set_bit(idx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS)
839a8625217SAtish Patra 			sdata->ctr_values[idx] = cpu_hw_evt->snapshot_cval_shcopy[idx];
840a8625217SAtish Patra 		if (temp_ctr_overflow_mask)
841a8625217SAtish Patra 			sdata->ctr_overflow_mask = temp_ctr_overflow_mask;
842a8625217SAtish Patra 	}
8434905ec2fSAtish Patra }
8444905ec2fSAtish Patra 
845c7a9dceaSPalmer Dabbelt /*
8464905ec2fSAtish Patra  * This function starts all the used counters in two step approach.
8474905ec2fSAtish Patra  * Any counter that did not overflow can be start in a single step
8484905ec2fSAtish Patra  * while the overflowed counters need to be started with updated initialization
8494905ec2fSAtish Patra  * value.
8504905ec2fSAtish Patra  */
pmu_sbi_start_ovf_ctrs_sbi(struct cpu_hw_events * cpu_hw_evt,u64 ctr_ovf_mask)851a8625217SAtish Patra static inline void pmu_sbi_start_ovf_ctrs_sbi(struct cpu_hw_events *cpu_hw_evt,
852a8625217SAtish Patra 					      u64 ctr_ovf_mask)
8534905ec2fSAtish Patra {
854b994cdfcSAtish Patra 	int idx = 0, i;
8554905ec2fSAtish Patra 	struct perf_event *event;
8564905ec2fSAtish Patra 	unsigned long flag = SBI_PMU_START_FLAG_SET_INIT_VALUE;
8574905ec2fSAtish Patra 	unsigned long ctr_start_mask = 0;
8584905ec2fSAtish Patra 	uint64_t max_period;
8594905ec2fSAtish Patra 	struct hw_perf_event *hwc;
8604905ec2fSAtish Patra 	u64 init_val = 0;
8614905ec2fSAtish Patra 
862b994cdfcSAtish Patra 	for (i = 0; i < BITS_TO_LONGS(RISCV_MAX_COUNTERS); i++) {
863b994cdfcSAtish Patra 		ctr_start_mask = cpu_hw_evt->used_hw_ctrs[i] & ~ctr_ovf_mask;
8644905ec2fSAtish Patra 		/* Start all the counters that did not overflow in a single shot */
865b994cdfcSAtish Patra 		sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, i * BITS_PER_LONG, ctr_start_mask,
8664905ec2fSAtish Patra 			0, 0, 0, 0);
867b994cdfcSAtish Patra 	}
8684905ec2fSAtish Patra 
8694905ec2fSAtish Patra 	/* Reinitialize and start all the counter that overflowed */
8704905ec2fSAtish Patra 	while (ctr_ovf_mask) {
8714905ec2fSAtish Patra 		if (ctr_ovf_mask & 0x01) {
8724905ec2fSAtish Patra 			event = cpu_hw_evt->events[idx];
8734905ec2fSAtish Patra 			hwc = &event->hw;
8744905ec2fSAtish Patra 			max_period = riscv_pmu_ctr_get_width_mask(event);
8754905ec2fSAtish Patra 			init_val = local64_read(&hwc->prev_count) & max_period;
876acc1b919SAtish Patra #if defined(CONFIG_32BIT)
877acc1b919SAtish Patra 			sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
878acc1b919SAtish Patra 				  flag, init_val, init_val >> 32, 0);
879acc1b919SAtish Patra #else
8804905ec2fSAtish Patra 			sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
8814905ec2fSAtish Patra 				  flag, init_val, 0, 0);
882acc1b919SAtish Patra #endif
883133a6d1fSAtish Patra 			perf_event_update_userpage(event);
8844905ec2fSAtish Patra 		}
8854905ec2fSAtish Patra 		ctr_ovf_mask = ctr_ovf_mask >> 1;
8864905ec2fSAtish Patra 		idx++;
8874905ec2fSAtish Patra 	}
8884905ec2fSAtish Patra }
8894905ec2fSAtish Patra 
pmu_sbi_start_ovf_ctrs_snapshot(struct cpu_hw_events * cpu_hw_evt,u64 ctr_ovf_mask)890a8625217SAtish Patra static inline void pmu_sbi_start_ovf_ctrs_snapshot(struct cpu_hw_events *cpu_hw_evt,
891a8625217SAtish Patra 						   u64 ctr_ovf_mask)
892a8625217SAtish Patra {
893a8625217SAtish Patra 	int i, idx = 0;
894a8625217SAtish Patra 	struct perf_event *event;
895a8625217SAtish Patra 	unsigned long flag = SBI_PMU_START_FLAG_INIT_SNAPSHOT;
896a8625217SAtish Patra 	u64 max_period, init_val = 0;
897a8625217SAtish Patra 	struct hw_perf_event *hwc;
898a8625217SAtish Patra 	struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
899a8625217SAtish Patra 
900a8625217SAtish Patra 	for_each_set_bit(idx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS) {
901a8625217SAtish Patra 		if (ctr_ovf_mask & BIT(idx)) {
902a8625217SAtish Patra 			event = cpu_hw_evt->events[idx];
903a8625217SAtish Patra 			hwc = &event->hw;
904a8625217SAtish Patra 			max_period = riscv_pmu_ctr_get_width_mask(event);
905a8625217SAtish Patra 			init_val = local64_read(&hwc->prev_count) & max_period;
906a8625217SAtish Patra 			cpu_hw_evt->snapshot_cval_shcopy[idx] = init_val;
907a8625217SAtish Patra 		}
908a8625217SAtish Patra 		/*
909a8625217SAtish Patra 		 * We do not need to update the non-overflow counters the previous
910a8625217SAtish Patra 		 * value should have been there already.
911a8625217SAtish Patra 		 */
912a8625217SAtish Patra 	}
913a8625217SAtish Patra 
914a8625217SAtish Patra 	for (i = 0; i < BITS_TO_LONGS(RISCV_MAX_COUNTERS); i++) {
915a8625217SAtish Patra 		/* Restore the counter values to relative indices for used hw counters */
916a8625217SAtish Patra 		for_each_set_bit(idx, &cpu_hw_evt->used_hw_ctrs[i], BITS_PER_LONG)
917a8625217SAtish Patra 			sdata->ctr_values[idx] =
918a8625217SAtish Patra 					cpu_hw_evt->snapshot_cval_shcopy[idx + i * BITS_PER_LONG];
919a8625217SAtish Patra 		/* Start all the counters in a single shot */
920a8625217SAtish Patra 		sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx * BITS_PER_LONG,
921a8625217SAtish Patra 			  cpu_hw_evt->used_hw_ctrs[i], flag, 0, 0, 0);
922a8625217SAtish Patra 	}
923a8625217SAtish Patra }
924a8625217SAtish Patra 
pmu_sbi_start_overflow_mask(struct riscv_pmu * pmu,u64 ctr_ovf_mask)925a8625217SAtish Patra static void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
926a8625217SAtish Patra 					u64 ctr_ovf_mask)
927a8625217SAtish Patra {
928a8625217SAtish Patra 	struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
929a8625217SAtish Patra 
930a8625217SAtish Patra 	if (sbi_pmu_snapshot_available())
931a8625217SAtish Patra 		pmu_sbi_start_ovf_ctrs_snapshot(cpu_hw_evt, ctr_ovf_mask);
932a8625217SAtish Patra 	else
933a8625217SAtish Patra 		pmu_sbi_start_ovf_ctrs_sbi(cpu_hw_evt, ctr_ovf_mask);
934a8625217SAtish Patra }
935a8625217SAtish Patra 
pmu_sbi_ovf_handler(int irq,void * dev)9364905ec2fSAtish Patra static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
9374905ec2fSAtish Patra {
9384905ec2fSAtish Patra 	struct perf_sample_data data;
9394905ec2fSAtish Patra 	struct pt_regs *regs;
9404905ec2fSAtish Patra 	struct hw_perf_event *hw_evt;
9414905ec2fSAtish Patra 	union sbi_pmu_ctr_info *info;
9424905ec2fSAtish Patra 	int lidx, hidx, fidx;
9434905ec2fSAtish Patra 	struct riscv_pmu *pmu;
9444905ec2fSAtish Patra 	struct perf_event *event;
945a8625217SAtish Patra 	u64 overflow;
946a8625217SAtish Patra 	u64 overflowed_ctrs = 0;
9474905ec2fSAtish Patra 	struct cpu_hw_events *cpu_hw_evt = dev;
948096b52fdSSergey Matyukevich 	u64 start_clock = sched_clock();
949a8625217SAtish Patra 	struct riscv_pmu_snapshot_data *sdata = cpu_hw_evt->snapshot_addr;
9504905ec2fSAtish Patra 
9514905ec2fSAtish Patra 	if (WARN_ON_ONCE(!cpu_hw_evt))
9524905ec2fSAtish Patra 		return IRQ_NONE;
9534905ec2fSAtish Patra 
9544905ec2fSAtish Patra 	/* Firmware counter don't support overflow yet */
9554905ec2fSAtish Patra 	fidx = find_first_bit(cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS);
956c6e316acSAlexandre Ghiti 	if (fidx == RISCV_MAX_COUNTERS) {
957c6e316acSAlexandre Ghiti 		csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));
958c6e316acSAlexandre Ghiti 		return IRQ_NONE;
959c6e316acSAlexandre Ghiti 	}
960c6e316acSAlexandre Ghiti 
9614905ec2fSAtish Patra 	event = cpu_hw_evt->events[fidx];
9624905ec2fSAtish Patra 	if (!event) {
963bc969d6cSYu Chien Peter Lin 		ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
9644905ec2fSAtish Patra 		return IRQ_NONE;
9654905ec2fSAtish Patra 	}
9664905ec2fSAtish Patra 
9674905ec2fSAtish Patra 	pmu = to_riscv_pmu(event->pmu);
9684905ec2fSAtish Patra 	pmu_sbi_stop_hw_ctrs(pmu);
9694905ec2fSAtish Patra 
9704905ec2fSAtish Patra 	/* Overflow status register should only be read after counter are stopped */
971a8625217SAtish Patra 	if (sbi_pmu_snapshot_available())
972a8625217SAtish Patra 		overflow = sdata->ctr_overflow_mask;
973a8625217SAtish Patra 	else
97465e9fb08SHeiko Stuebner 		ALT_SBI_PMU_OVERFLOW(overflow);
9754905ec2fSAtish Patra 
976c7a9dceaSPalmer Dabbelt 	/*
9774905ec2fSAtish Patra 	 * Overflow interrupt pending bit should only be cleared after stopping
9784905ec2fSAtish Patra 	 * all the counters to avoid any race condition.
9794905ec2fSAtish Patra 	 */
980bc969d6cSYu Chien Peter Lin 	ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
9814905ec2fSAtish Patra 
9824905ec2fSAtish Patra 	/* No overflow bit is set */
9834905ec2fSAtish Patra 	if (!overflow)
9844905ec2fSAtish Patra 		return IRQ_NONE;
9854905ec2fSAtish Patra 
9864905ec2fSAtish Patra 	regs = get_irq_regs();
9874905ec2fSAtish Patra 
9884905ec2fSAtish Patra 	for_each_set_bit(lidx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS) {
9894905ec2fSAtish Patra 		struct perf_event *event = cpu_hw_evt->events[lidx];
9904905ec2fSAtish Patra 
9914905ec2fSAtish Patra 		/* Skip if invalid event or user did not request a sampling */
9924905ec2fSAtish Patra 		if (!event || !is_sampling_event(event))
9934905ec2fSAtish Patra 			continue;
9944905ec2fSAtish Patra 
9954905ec2fSAtish Patra 		info = &pmu_ctr_list[lidx];
9964905ec2fSAtish Patra 		/* Do a sanity check */
9974905ec2fSAtish Patra 		if (!info || info->type != SBI_PMU_CTR_TYPE_HW)
9984905ec2fSAtish Patra 			continue;
9994905ec2fSAtish Patra 
1000a8625217SAtish Patra 		if (sbi_pmu_snapshot_available())
1001a8625217SAtish Patra 			/* SBI implementation already updated the logical indicies */
1002a8625217SAtish Patra 			hidx = lidx;
1003a8625217SAtish Patra 		else
10044905ec2fSAtish Patra 			/* compute hardware counter index */
10054905ec2fSAtish Patra 			hidx = info->csr - CSR_CYCLE;
1006a8625217SAtish Patra 
1007a8625217SAtish Patra 		/* check if the corresponding bit is set in sscountovf or overflow mask in shmem */
100834b56786SFei Wu 		if (!(overflow & BIT(hidx)))
10094905ec2fSAtish Patra 			continue;
10104905ec2fSAtish Patra 
10114905ec2fSAtish Patra 		/*
10124905ec2fSAtish Patra 		 * Keep a track of overflowed counters so that they can be started
10134905ec2fSAtish Patra 		 * with updated initial value.
10144905ec2fSAtish Patra 		 */
101534b56786SFei Wu 		overflowed_ctrs |= BIT(lidx);
10164905ec2fSAtish Patra 		hw_evt = &event->hw;
1017a8625217SAtish Patra 		/* Update the event states here so that we know the state while reading */
1018a8625217SAtish Patra 		hw_evt->state |= PERF_HES_STOPPED;
10194905ec2fSAtish Patra 		riscv_pmu_event_update(event);
1020a8625217SAtish Patra 		hw_evt->state |= PERF_HES_UPTODATE;
10214905ec2fSAtish Patra 		perf_sample_data_init(&data, 0, hw_evt->last_period);
10224905ec2fSAtish Patra 		if (riscv_pmu_event_set_period(event)) {
10234905ec2fSAtish Patra 			/*
10244905ec2fSAtish Patra 			 * Unlike other ISAs, RISC-V don't have to disable interrupts
10254905ec2fSAtish Patra 			 * to avoid throttling here. As per the specification, the
10264905ec2fSAtish Patra 			 * interrupt remains disabled until the OF bit is set.
10274905ec2fSAtish Patra 			 * Interrupts are enabled again only during the start.
10284905ec2fSAtish Patra 			 * TODO: We will need to stop the guest counters once
10294905ec2fSAtish Patra 			 * virtualization support is added.
10304905ec2fSAtish Patra 			 */
10314905ec2fSAtish Patra 			perf_event_overflow(event, &data, regs);
10324905ec2fSAtish Patra 		}
1033a8625217SAtish Patra 		/* Reset the state as we are going to start the counter after the loop */
1034a8625217SAtish Patra 		hw_evt->state = 0;
10354905ec2fSAtish Patra 	}
1036096b52fdSSergey Matyukevich 
10374905ec2fSAtish Patra 	pmu_sbi_start_overflow_mask(pmu, overflowed_ctrs);
1038096b52fdSSergey Matyukevich 	perf_sample_event_took(sched_clock() - start_clock);
10394905ec2fSAtish Patra 
10404905ec2fSAtish Patra 	return IRQ_HANDLED;
10414905ec2fSAtish Patra }
10424905ec2fSAtish Patra 
pmu_sbi_starting_cpu(unsigned int cpu,struct hlist_node * node)1043e9991434SAtish Patra static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
1044e9991434SAtish Patra {
1045e9991434SAtish Patra 	struct riscv_pmu *pmu = hlist_entry_safe(node, struct riscv_pmu, node);
10464905ec2fSAtish Patra 	struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
1047e9991434SAtish Patra 
10485a5294fbSPalmer Dabbelt 	/*
1049cc4c07c8SAlexandre Ghiti 	 * We keep enabling userspace access to CYCLE, TIME and INSTRET via the
1050cc4c07c8SAlexandre Ghiti 	 * legacy option but that will be removed in the future.
10515a5294fbSPalmer Dabbelt 	 */
1052cc4c07c8SAlexandre Ghiti 	if (sysctl_perf_user_access == SYSCTL_LEGACY)
10535a5294fbSPalmer Dabbelt 		csr_write(CSR_SCOUNTEREN, 0x7);
1054cc4c07c8SAlexandre Ghiti 	else
1055cc4c07c8SAlexandre Ghiti 		csr_write(CSR_SCOUNTEREN, 0x2);
1056e9991434SAtish Patra 
1057e9991434SAtish Patra 	/* Stop all the counters so that they can be enabled from perf */
10584905ec2fSAtish Patra 	pmu_sbi_stop_all(pmu);
10594905ec2fSAtish Patra 
106065e9fb08SHeiko Stuebner 	if (riscv_pmu_use_irq) {
10614905ec2fSAtish Patra 		cpu_hw_evt->irq = riscv_pmu_irq;
1062bc969d6cSYu Chien Peter Lin 		ALT_SBI_PMU_OVF_CLEAR_PENDING(riscv_pmu_irq_mask);
10634905ec2fSAtish Patra 		enable_percpu_irq(riscv_pmu_irq, IRQ_TYPE_NONE);
10644905ec2fSAtish Patra 	}
1065e9991434SAtish Patra 
1066a8625217SAtish Patra 	if (sbi_pmu_snapshot_available())
1067a8625217SAtish Patra 		return pmu_sbi_snapshot_setup(pmu, cpu);
1068a8625217SAtish Patra 
1069e9991434SAtish Patra 	return 0;
1070e9991434SAtish Patra }
1071e9991434SAtish Patra 
pmu_sbi_dying_cpu(unsigned int cpu,struct hlist_node * node)1072e9991434SAtish Patra static int pmu_sbi_dying_cpu(unsigned int cpu, struct hlist_node *node)
1073e9991434SAtish Patra {
107465e9fb08SHeiko Stuebner 	if (riscv_pmu_use_irq) {
10754905ec2fSAtish Patra 		disable_percpu_irq(riscv_pmu_irq);
10764905ec2fSAtish Patra 	}
10774905ec2fSAtish Patra 
1078e9991434SAtish Patra 	/* Disable all counters access for user mode now */
1079e9991434SAtish Patra 	csr_write(CSR_SCOUNTEREN, 0x0);
1080e9991434SAtish Patra 
1081a8625217SAtish Patra 	if (sbi_pmu_snapshot_available())
1082a8625217SAtish Patra 		return pmu_sbi_snapshot_disable();
1083a8625217SAtish Patra 
1084e9991434SAtish Patra 	return 0;
1085e9991434SAtish Patra }
1086e9991434SAtish Patra 
pmu_sbi_setup_irqs(struct riscv_pmu * pmu,struct platform_device * pdev)10874905ec2fSAtish Patra static int pmu_sbi_setup_irqs(struct riscv_pmu *pmu, struct platform_device *pdev)
10884905ec2fSAtish Patra {
10894905ec2fSAtish Patra 	int ret;
10904905ec2fSAtish Patra 	struct cpu_hw_events __percpu *hw_events = pmu->hw_events;
10914905ec2fSAtish Patra 	struct irq_domain *domain = NULL;
10924905ec2fSAtish Patra 
109365e9fb08SHeiko Stuebner 	if (riscv_isa_extension_available(NULL, SSCOFPMF)) {
109465e9fb08SHeiko Stuebner 		riscv_pmu_irq_num = RV_IRQ_PMU;
109565e9fb08SHeiko Stuebner 		riscv_pmu_use_irq = true;
109665e9fb08SHeiko Stuebner 	} else if (IS_ENABLED(CONFIG_ERRATA_THEAD_PMU) &&
109765e9fb08SHeiko Stuebner 		   riscv_cached_mvendorid(0) == THEAD_VENDOR_ID &&
109865e9fb08SHeiko Stuebner 		   riscv_cached_marchid(0) == 0 &&
109965e9fb08SHeiko Stuebner 		   riscv_cached_mimpid(0) == 0) {
110065e9fb08SHeiko Stuebner 		riscv_pmu_irq_num = THEAD_C9XX_RV_IRQ_PMU;
110165e9fb08SHeiko Stuebner 		riscv_pmu_use_irq = true;
11020f242541SCharlie Jenkins 	} else if (riscv_has_vendor_extension_unlikely(ANDES_VENDOR_ID,
11030f242541SCharlie Jenkins 						       RISCV_ISA_VENDOR_EXT_XANDESPMU) &&
1104bc969d6cSYu Chien Peter Lin 		   IS_ENABLED(CONFIG_ANDES_CUSTOM_PMU)) {
1105bc969d6cSYu Chien Peter Lin 		riscv_pmu_irq_num = ANDES_SLI_CAUSE_BASE + ANDES_RV_IRQ_PMOVI;
1106bc969d6cSYu Chien Peter Lin 		riscv_pmu_use_irq = true;
110765e9fb08SHeiko Stuebner 	}
110865e9fb08SHeiko Stuebner 
1109bc969d6cSYu Chien Peter Lin 	riscv_pmu_irq_mask = BIT(riscv_pmu_irq_num % BITS_PER_LONG);
1110bc969d6cSYu Chien Peter Lin 
111165e9fb08SHeiko Stuebner 	if (!riscv_pmu_use_irq)
11124905ec2fSAtish Patra 		return -EOPNOTSUPP;
11134905ec2fSAtish Patra 
1114ca7473cbSSunil V L 	domain = irq_find_matching_fwnode(riscv_get_intc_hwnode(),
1115ca7473cbSSunil V L 					  DOMAIN_BUS_ANY);
11164905ec2fSAtish Patra 	if (!domain) {
11174905ec2fSAtish Patra 		pr_err("Failed to find INTC IRQ root domain\n");
11184905ec2fSAtish Patra 		return -ENODEV;
11194905ec2fSAtish Patra 	}
11204905ec2fSAtish Patra 
112165e9fb08SHeiko Stuebner 	riscv_pmu_irq = irq_create_mapping(domain, riscv_pmu_irq_num);
11224905ec2fSAtish Patra 	if (!riscv_pmu_irq) {
11234905ec2fSAtish Patra 		pr_err("Failed to map PMU interrupt for node\n");
11244905ec2fSAtish Patra 		return -ENODEV;
11254905ec2fSAtish Patra 	}
11264905ec2fSAtish Patra 
11274905ec2fSAtish Patra 	ret = request_percpu_irq(riscv_pmu_irq, pmu_sbi_ovf_handler, "riscv-pmu", hw_events);
11284905ec2fSAtish Patra 	if (ret) {
11294905ec2fSAtish Patra 		pr_err("registering percpu irq failed [%d]\n", ret);
11304905ec2fSAtish Patra 		return ret;
11314905ec2fSAtish Patra 	}
11324905ec2fSAtish Patra 
11334905ec2fSAtish Patra 	return 0;
11344905ec2fSAtish Patra }
11354905ec2fSAtish Patra 
1136e9a023f2SEric Lin #ifdef CONFIG_CPU_PM
riscv_pm_pmu_notify(struct notifier_block * b,unsigned long cmd,void * v)1137e9a023f2SEric Lin static int riscv_pm_pmu_notify(struct notifier_block *b, unsigned long cmd,
1138e9a023f2SEric Lin 				void *v)
1139e9a023f2SEric Lin {
1140e9a023f2SEric Lin 	struct riscv_pmu *rvpmu = container_of(b, struct riscv_pmu, riscv_pm_nb);
1141e9a023f2SEric Lin 	struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
1142e9a023f2SEric Lin 	int enabled = bitmap_weight(cpuc->used_hw_ctrs, RISCV_MAX_COUNTERS);
1143e9a023f2SEric Lin 	struct perf_event *event;
1144e9a023f2SEric Lin 	int idx;
1145e9a023f2SEric Lin 
1146e9a023f2SEric Lin 	if (!enabled)
1147e9a023f2SEric Lin 		return NOTIFY_OK;
1148e9a023f2SEric Lin 
1149e9a023f2SEric Lin 	for (idx = 0; idx < RISCV_MAX_COUNTERS; idx++) {
1150e9a023f2SEric Lin 		event = cpuc->events[idx];
1151e9a023f2SEric Lin 		if (!event)
1152e9a023f2SEric Lin 			continue;
1153e9a023f2SEric Lin 
1154e9a023f2SEric Lin 		switch (cmd) {
1155e9a023f2SEric Lin 		case CPU_PM_ENTER:
1156e9a023f2SEric Lin 			/*
1157e9a023f2SEric Lin 			 * Stop and update the counter
1158e9a023f2SEric Lin 			 */
1159e9a023f2SEric Lin 			riscv_pmu_stop(event, PERF_EF_UPDATE);
1160e9a023f2SEric Lin 			break;
1161e9a023f2SEric Lin 		case CPU_PM_EXIT:
1162e9a023f2SEric Lin 		case CPU_PM_ENTER_FAILED:
1163e9a023f2SEric Lin 			/*
1164e9a023f2SEric Lin 			 * Restore and enable the counter.
1165e9a023f2SEric Lin 			 */
11661c38b061SPeter Zijlstra 			riscv_pmu_start(event, PERF_EF_RELOAD);
1167e9a023f2SEric Lin 			break;
1168e9a023f2SEric Lin 		default:
1169e9a023f2SEric Lin 			break;
1170e9a023f2SEric Lin 		}
1171e9a023f2SEric Lin 	}
1172e9a023f2SEric Lin 
1173e9a023f2SEric Lin 	return NOTIFY_OK;
1174e9a023f2SEric Lin }
1175e9a023f2SEric Lin 
riscv_pm_pmu_register(struct riscv_pmu * pmu)1176e9a023f2SEric Lin static int riscv_pm_pmu_register(struct riscv_pmu *pmu)
1177e9a023f2SEric Lin {
1178e9a023f2SEric Lin 	pmu->riscv_pm_nb.notifier_call = riscv_pm_pmu_notify;
1179e9a023f2SEric Lin 	return cpu_pm_register_notifier(&pmu->riscv_pm_nb);
1180e9a023f2SEric Lin }
1181e9a023f2SEric Lin 
riscv_pm_pmu_unregister(struct riscv_pmu * pmu)1182e9a023f2SEric Lin static void riscv_pm_pmu_unregister(struct riscv_pmu *pmu)
1183e9a023f2SEric Lin {
1184e9a023f2SEric Lin 	cpu_pm_unregister_notifier(&pmu->riscv_pm_nb);
1185e9a023f2SEric Lin }
1186e9a023f2SEric Lin #else
riscv_pm_pmu_register(struct riscv_pmu * pmu)1187e9a023f2SEric Lin static inline int riscv_pm_pmu_register(struct riscv_pmu *pmu) { return 0; }
riscv_pm_pmu_unregister(struct riscv_pmu * pmu)1188e9a023f2SEric Lin static inline void riscv_pm_pmu_unregister(struct riscv_pmu *pmu) { }
1189e9a023f2SEric Lin #endif
1190e9a023f2SEric Lin 
riscv_pmu_destroy(struct riscv_pmu * pmu)1191e9a023f2SEric Lin static void riscv_pmu_destroy(struct riscv_pmu *pmu)
1192e9a023f2SEric Lin {
1193a8625217SAtish Patra 	if (sbi_v2_available) {
1194a8625217SAtish Patra 		if (sbi_pmu_snapshot_available()) {
1195a8625217SAtish Patra 			pmu_sbi_snapshot_disable();
1196a8625217SAtish Patra 			pmu_sbi_snapshot_free(pmu);
1197a8625217SAtish Patra 		}
1198a8625217SAtish Patra 	}
1199e9a023f2SEric Lin 	riscv_pm_pmu_unregister(pmu);
1200e9a023f2SEric Lin 	cpuhp_state_remove_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
1201e9a023f2SEric Lin }
1202e9a023f2SEric Lin 
pmu_sbi_event_init(struct perf_event * event)1203cc4c07c8SAlexandre Ghiti static void pmu_sbi_event_init(struct perf_event *event)
1204cc4c07c8SAlexandre Ghiti {
1205cc4c07c8SAlexandre Ghiti 	/*
1206cc4c07c8SAlexandre Ghiti 	 * The permissions are set at event_init so that we do not depend
1207cc4c07c8SAlexandre Ghiti 	 * on the sysctl value that can change.
1208cc4c07c8SAlexandre Ghiti 	 */
1209cc4c07c8SAlexandre Ghiti 	if (sysctl_perf_user_access == SYSCTL_NO_USER_ACCESS)
1210cc4c07c8SAlexandre Ghiti 		event->hw.flags |= PERF_EVENT_FLAG_NO_USER_ACCESS;
1211cc4c07c8SAlexandre Ghiti 	else if (sysctl_perf_user_access == SYSCTL_USER_ACCESS)
1212cc4c07c8SAlexandre Ghiti 		event->hw.flags |= PERF_EVENT_FLAG_USER_ACCESS;
1213cc4c07c8SAlexandre Ghiti 	else
1214cc4c07c8SAlexandre Ghiti 		event->hw.flags |= PERF_EVENT_FLAG_LEGACY;
1215cc4c07c8SAlexandre Ghiti }
1216cc4c07c8SAlexandre Ghiti 
pmu_sbi_event_mapped(struct perf_event * event,struct mm_struct * mm)1217cc4c07c8SAlexandre Ghiti static void pmu_sbi_event_mapped(struct perf_event *event, struct mm_struct *mm)
1218cc4c07c8SAlexandre Ghiti {
1219cc4c07c8SAlexandre Ghiti 	if (event->hw.flags & PERF_EVENT_FLAG_NO_USER_ACCESS)
1220cc4c07c8SAlexandre Ghiti 		return;
1221cc4c07c8SAlexandre Ghiti 
1222cc4c07c8SAlexandre Ghiti 	if (event->hw.flags & PERF_EVENT_FLAG_LEGACY) {
1223cc4c07c8SAlexandre Ghiti 		if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES &&
1224cc4c07c8SAlexandre Ghiti 		    event->attr.config != PERF_COUNT_HW_INSTRUCTIONS) {
1225cc4c07c8SAlexandre Ghiti 			return;
1226cc4c07c8SAlexandre Ghiti 		}
1227cc4c07c8SAlexandre Ghiti 	}
1228cc4c07c8SAlexandre Ghiti 
1229cc4c07c8SAlexandre Ghiti 	/*
1230cc4c07c8SAlexandre Ghiti 	 * The user mmapped the event to directly access it: this is where
1231cc4c07c8SAlexandre Ghiti 	 * we determine based on sysctl_perf_user_access if we grant userspace
1232cc4c07c8SAlexandre Ghiti 	 * the direct access to this event. That means that within the same
1233cc4c07c8SAlexandre Ghiti 	 * task, some events may be directly accessible and some other may not,
1234cc4c07c8SAlexandre Ghiti 	 * if the user changes the value of sysctl_perf_user_accesss in the
1235cc4c07c8SAlexandre Ghiti 	 * meantime.
1236cc4c07c8SAlexandre Ghiti 	 */
1237cc4c07c8SAlexandre Ghiti 
1238cc4c07c8SAlexandre Ghiti 	event->hw.flags |= PERF_EVENT_FLAG_USER_READ_CNT;
1239cc4c07c8SAlexandre Ghiti 
1240cc4c07c8SAlexandre Ghiti 	/*
1241cc4c07c8SAlexandre Ghiti 	 * We must enable userspace access *before* advertising in the user page
1242cc4c07c8SAlexandre Ghiti 	 * that it is possible to do so to avoid any race.
1243cc4c07c8SAlexandre Ghiti 	 * And we must notify all cpus here because threads that currently run
1244cc4c07c8SAlexandre Ghiti 	 * on other cpus will try to directly access the counter too without
1245cc4c07c8SAlexandre Ghiti 	 * calling pmu_sbi_ctr_start.
1246cc4c07c8SAlexandre Ghiti 	 */
1247cc4c07c8SAlexandre Ghiti 	if (event->hw.flags & PERF_EVENT_FLAG_USER_ACCESS)
1248cc4c07c8SAlexandre Ghiti 		on_each_cpu_mask(mm_cpumask(mm),
1249cc4c07c8SAlexandre Ghiti 				 pmu_sbi_set_scounteren, (void *)event, 1);
1250cc4c07c8SAlexandre Ghiti }
1251cc4c07c8SAlexandre Ghiti 
pmu_sbi_event_unmapped(struct perf_event * event,struct mm_struct * mm)1252cc4c07c8SAlexandre Ghiti static void pmu_sbi_event_unmapped(struct perf_event *event, struct mm_struct *mm)
1253cc4c07c8SAlexandre Ghiti {
1254cc4c07c8SAlexandre Ghiti 	if (event->hw.flags & PERF_EVENT_FLAG_NO_USER_ACCESS)
1255cc4c07c8SAlexandre Ghiti 		return;
1256cc4c07c8SAlexandre Ghiti 
1257cc4c07c8SAlexandre Ghiti 	if (event->hw.flags & PERF_EVENT_FLAG_LEGACY) {
1258cc4c07c8SAlexandre Ghiti 		if (event->attr.config != PERF_COUNT_HW_CPU_CYCLES &&
1259cc4c07c8SAlexandre Ghiti 		    event->attr.config != PERF_COUNT_HW_INSTRUCTIONS) {
1260cc4c07c8SAlexandre Ghiti 			return;
1261cc4c07c8SAlexandre Ghiti 		}
1262cc4c07c8SAlexandre Ghiti 	}
1263cc4c07c8SAlexandre Ghiti 
1264cc4c07c8SAlexandre Ghiti 	/*
1265cc4c07c8SAlexandre Ghiti 	 * Here we can directly remove user access since the user does not have
1266cc4c07c8SAlexandre Ghiti 	 * access to the user page anymore so we avoid the racy window where the
1267cc4c07c8SAlexandre Ghiti 	 * user could have read cap_user_rdpmc to true right before we disable
1268cc4c07c8SAlexandre Ghiti 	 * it.
1269cc4c07c8SAlexandre Ghiti 	 */
1270cc4c07c8SAlexandre Ghiti 	event->hw.flags &= ~PERF_EVENT_FLAG_USER_READ_CNT;
1271cc4c07c8SAlexandre Ghiti 
1272cc4c07c8SAlexandre Ghiti 	if (event->hw.flags & PERF_EVENT_FLAG_USER_ACCESS)
1273cc4c07c8SAlexandre Ghiti 		on_each_cpu_mask(mm_cpumask(mm),
1274cc4c07c8SAlexandre Ghiti 				 pmu_sbi_reset_scounteren, (void *)event, 1);
1275cc4c07c8SAlexandre Ghiti }
1276cc4c07c8SAlexandre Ghiti 
riscv_pmu_update_counter_access(void * info)1277cc4c07c8SAlexandre Ghiti static void riscv_pmu_update_counter_access(void *info)
1278cc4c07c8SAlexandre Ghiti {
1279cc4c07c8SAlexandre Ghiti 	if (sysctl_perf_user_access == SYSCTL_LEGACY)
1280cc4c07c8SAlexandre Ghiti 		csr_write(CSR_SCOUNTEREN, 0x7);
1281cc4c07c8SAlexandre Ghiti 	else
1282cc4c07c8SAlexandre Ghiti 		csr_write(CSR_SCOUNTEREN, 0x2);
1283cc4c07c8SAlexandre Ghiti }
1284cc4c07c8SAlexandre Ghiti 
riscv_pmu_proc_user_access_handler(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)128578eb4ea2SJoel Granados static int riscv_pmu_proc_user_access_handler(const struct ctl_table *table,
1286cc4c07c8SAlexandre Ghiti 					      int write, void *buffer,
1287cc4c07c8SAlexandre Ghiti 					      size_t *lenp, loff_t *ppos)
1288cc4c07c8SAlexandre Ghiti {
1289cc4c07c8SAlexandre Ghiti 	int prev = sysctl_perf_user_access;
1290cc4c07c8SAlexandre Ghiti 	int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
1291cc4c07c8SAlexandre Ghiti 
1292cc4c07c8SAlexandre Ghiti 	/*
1293cc4c07c8SAlexandre Ghiti 	 * Test against the previous value since we clear SCOUNTEREN when
1294cc4c07c8SAlexandre Ghiti 	 * sysctl_perf_user_access is set to SYSCTL_USER_ACCESS, but we should
1295cc4c07c8SAlexandre Ghiti 	 * not do that if that was already the case.
1296cc4c07c8SAlexandre Ghiti 	 */
1297cc4c07c8SAlexandre Ghiti 	if (ret || !write || prev == sysctl_perf_user_access)
1298cc4c07c8SAlexandre Ghiti 		return ret;
1299cc4c07c8SAlexandre Ghiti 
1300cc4c07c8SAlexandre Ghiti 	on_each_cpu(riscv_pmu_update_counter_access, NULL, 1);
1301cc4c07c8SAlexandre Ghiti 
1302cc4c07c8SAlexandre Ghiti 	return 0;
1303cc4c07c8SAlexandre Ghiti }
1304cc4c07c8SAlexandre Ghiti 
1305cc4c07c8SAlexandre Ghiti static struct ctl_table sbi_pmu_sysctl_table[] = {
1306cc4c07c8SAlexandre Ghiti 	{
1307cc4c07c8SAlexandre Ghiti 		.procname       = "perf_user_access",
1308cc4c07c8SAlexandre Ghiti 		.data		= &sysctl_perf_user_access,
1309cc4c07c8SAlexandre Ghiti 		.maxlen		= sizeof(unsigned int),
1310cc4c07c8SAlexandre Ghiti 		.mode           = 0644,
1311cc4c07c8SAlexandre Ghiti 		.proc_handler	= riscv_pmu_proc_user_access_handler,
1312cc4c07c8SAlexandre Ghiti 		.extra1		= SYSCTL_ZERO,
1313cc4c07c8SAlexandre Ghiti 		.extra2		= SYSCTL_TWO,
1314cc4c07c8SAlexandre Ghiti 	},
1315cc4c07c8SAlexandre Ghiti };
1316cc4c07c8SAlexandre Ghiti 
pmu_sbi_device_probe(struct platform_device * pdev)1317e9991434SAtish Patra static int pmu_sbi_device_probe(struct platform_device *pdev)
1318e9991434SAtish Patra {
1319e9991434SAtish Patra 	struct riscv_pmu *pmu = NULL;
13204905ec2fSAtish Patra 	int ret = -ENODEV;
13211537bf26SSergey Matyukevich 	int num_counters;
1322e9991434SAtish Patra 
1323e9991434SAtish Patra 	pr_info("SBI PMU extension is available\n");
1324e9991434SAtish Patra 	pmu = riscv_pmu_alloc();
1325e9991434SAtish Patra 	if (!pmu)
1326e9991434SAtish Patra 		return -ENOMEM;
1327e9991434SAtish Patra 
1328e9991434SAtish Patra 	num_counters = pmu_sbi_find_num_ctrs();
1329e9991434SAtish Patra 	if (num_counters < 0) {
1330e9991434SAtish Patra 		pr_err("SBI PMU extension doesn't provide any counters\n");
13314905ec2fSAtish Patra 		goto out_free;
1332e9991434SAtish Patra 	}
1333e9991434SAtish Patra 
1334ee95b88dSViacheslav Mitrofanov 	/* It is possible to get from SBI more than max number of counters */
1335ee95b88dSViacheslav Mitrofanov 	if (num_counters > RISCV_MAX_COUNTERS) {
1336ee95b88dSViacheslav Mitrofanov 		num_counters = RISCV_MAX_COUNTERS;
1337ee95b88dSViacheslav Mitrofanov 		pr_info("SBI returned more than maximum number of counters. Limiting the number of counters to %d\n", num_counters);
1338ee95b88dSViacheslav Mitrofanov 	}
1339ee95b88dSViacheslav Mitrofanov 
1340e9991434SAtish Patra 	/* cache all the information about counters now */
13411537bf26SSergey Matyukevich 	if (pmu_sbi_get_ctrinfo(num_counters, &cmask))
13424905ec2fSAtish Patra 		goto out_free;
1343e9991434SAtish Patra 
13444905ec2fSAtish Patra 	ret = pmu_sbi_setup_irqs(pmu, pdev);
13454905ec2fSAtish Patra 	if (ret < 0) {
13464905ec2fSAtish Patra 		pr_info("Perf sampling/filtering is not supported as sscof extension is not available\n");
13474905ec2fSAtish Patra 		pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
13484905ec2fSAtish Patra 		pmu->pmu.capabilities |= PERF_PMU_CAP_NO_EXCLUDE;
13494905ec2fSAtish Patra 	}
13501537bf26SSergey Matyukevich 
135126fabd6dSNikita Shubin 	pmu->pmu.attr_groups = riscv_pmu_attr_groups;
135250650e5fSJonathan Cameron 	pmu->pmu.parent = &pdev->dev;
13531537bf26SSergey Matyukevich 	pmu->cmask = cmask;
1354e9991434SAtish Patra 	pmu->ctr_start = pmu_sbi_ctr_start;
1355e9991434SAtish Patra 	pmu->ctr_stop = pmu_sbi_ctr_stop;
1356e9991434SAtish Patra 	pmu->event_map = pmu_sbi_event_map;
1357e9991434SAtish Patra 	pmu->ctr_get_idx = pmu_sbi_ctr_get_idx;
1358e9991434SAtish Patra 	pmu->ctr_get_width = pmu_sbi_ctr_get_width;
1359e9991434SAtish Patra 	pmu->ctr_clear_idx = pmu_sbi_ctr_clear_idx;
1360e9991434SAtish Patra 	pmu->ctr_read = pmu_sbi_ctr_read;
1361cc4c07c8SAlexandre Ghiti 	pmu->event_init = pmu_sbi_event_init;
1362cc4c07c8SAlexandre Ghiti 	pmu->event_mapped = pmu_sbi_event_mapped;
1363cc4c07c8SAlexandre Ghiti 	pmu->event_unmapped = pmu_sbi_event_unmapped;
1364cc4c07c8SAlexandre Ghiti 	pmu->csr_index = pmu_sbi_csr_index;
1365e9991434SAtish Patra 
1366e9a023f2SEric Lin 	ret = riscv_pm_pmu_register(pmu);
1367e9a023f2SEric Lin 	if (ret)
1368e9a023f2SEric Lin 		goto out_unregister;
1369e9a023f2SEric Lin 
1370e9991434SAtish Patra 	ret = perf_pmu_register(&pmu->pmu, "cpu", PERF_TYPE_RAW);
1371e9a023f2SEric Lin 	if (ret)
1372e9a023f2SEric Lin 		goto out_unregister;
1373e9991434SAtish Patra 
1374a8625217SAtish Patra 	/* SBI PMU Snapsphot is only available in SBI v2.0 */
1375a8625217SAtish Patra 	if (sbi_v2_available) {
1376*2840dadfSAlexandre Ghiti 		int cpu;
1377*2840dadfSAlexandre Ghiti 
1378a8625217SAtish Patra 		ret = pmu_sbi_snapshot_alloc(pmu);
1379a8625217SAtish Patra 		if (ret)
1380a8625217SAtish Patra 			goto out_unregister;
1381a8625217SAtish Patra 
1382*2840dadfSAlexandre Ghiti 		cpu = get_cpu();
1383*2840dadfSAlexandre Ghiti 
1384*2840dadfSAlexandre Ghiti 		ret = pmu_sbi_snapshot_setup(pmu, cpu);
1385a8625217SAtish Patra 		if (ret) {
1386a8625217SAtish Patra 			/* Snapshot is an optional feature. Continue if not available */
1387a8625217SAtish Patra 			pmu_sbi_snapshot_free(pmu);
1388a8625217SAtish Patra 		} else {
1389a8625217SAtish Patra 			pr_info("SBI PMU snapshot detected\n");
1390a8625217SAtish Patra 			/*
1391a8625217SAtish Patra 			 * We enable it once here for the boot cpu. If snapshot shmem setup
1392a8625217SAtish Patra 			 * fails during cpu hotplug process, it will fail to start the cpu
1393a8625217SAtish Patra 			 * as we can not handle hetergenous PMUs with different snapshot
1394a8625217SAtish Patra 			 * capability.
1395a8625217SAtish Patra 			 */
1396a8625217SAtish Patra 			static_branch_enable(&sbi_pmu_snapshot_available);
1397a8625217SAtish Patra 		}
1398*2840dadfSAlexandre Ghiti 		put_cpu();
1399a8625217SAtish Patra 	}
1400a8625217SAtish Patra 
1401cc4c07c8SAlexandre Ghiti 	register_sysctl("kernel", sbi_pmu_sysctl_table);
1402cc4c07c8SAlexandre Ghiti 
1403a8625217SAtish Patra 	ret = cpuhp_state_add_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
1404a8625217SAtish Patra 	if (ret)
1405a8625217SAtish Patra 		goto out_unregister;
1406a8625217SAtish Patra 
140716d3b1afSSamuel Holland 	/* Asynchronously check which standard events are available */
140816d3b1afSSamuel Holland 	schedule_work(&check_std_events_work);
140916d3b1afSSamuel Holland 
1410e9991434SAtish Patra 	return 0;
14114905ec2fSAtish Patra 
1412e9a023f2SEric Lin out_unregister:
1413e9a023f2SEric Lin 	riscv_pmu_destroy(pmu);
1414e9a023f2SEric Lin 
14154905ec2fSAtish Patra out_free:
14164905ec2fSAtish Patra 	kfree(pmu);
14174905ec2fSAtish Patra 	return ret;
1418e9991434SAtish Patra }
1419e9991434SAtish Patra 
1420e9991434SAtish Patra static struct platform_driver pmu_sbi_driver = {
1421e9991434SAtish Patra 	.probe		= pmu_sbi_device_probe,
1422e9991434SAtish Patra 	.driver		= {
1423d5ac062dSAlexandre Ghiti 		.name	= RISCV_PMU_SBI_PDEV_NAME,
1424e9991434SAtish Patra 	},
1425e9991434SAtish Patra };
1426e9991434SAtish Patra 
pmu_sbi_devinit(void)1427e9991434SAtish Patra static int __init pmu_sbi_devinit(void)
1428e9991434SAtish Patra {
1429e9991434SAtish Patra 	int ret;
1430e9991434SAtish Patra 	struct platform_device *pdev;
1431e9991434SAtish Patra 
1432e9991434SAtish Patra 	if (sbi_spec_version < sbi_mk_version(0, 3) ||
143341cad828SAndrew Jones 	    !sbi_probe_extension(SBI_EXT_PMU)) {
1434e9991434SAtish Patra 		return 0;
1435e9991434SAtish Patra 	}
1436e9991434SAtish Patra 
14377dda24baSAtish Patra 	if (sbi_spec_version >= sbi_mk_version(2, 0))
14387dda24baSAtish Patra 		sbi_v2_available = true;
14397dda24baSAtish Patra 
1440e9991434SAtish Patra 	ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_RISCV_STARTING,
1441e9991434SAtish Patra 				      "perf/riscv/pmu:starting",
1442e9991434SAtish Patra 				      pmu_sbi_starting_cpu, pmu_sbi_dying_cpu);
1443e9991434SAtish Patra 	if (ret) {
1444e9991434SAtish Patra 		pr_err("CPU hotplug notifier could not be registered: %d\n",
1445e9991434SAtish Patra 		       ret);
1446e9991434SAtish Patra 		return ret;
1447e9991434SAtish Patra 	}
1448e9991434SAtish Patra 
1449e9991434SAtish Patra 	ret = platform_driver_register(&pmu_sbi_driver);
1450e9991434SAtish Patra 	if (ret)
1451e9991434SAtish Patra 		return ret;
1452e9991434SAtish Patra 
1453d5ac062dSAlexandre Ghiti 	pdev = platform_device_register_simple(RISCV_PMU_SBI_PDEV_NAME, -1, NULL, 0);
1454e9991434SAtish Patra 	if (IS_ERR(pdev)) {
1455e9991434SAtish Patra 		platform_driver_unregister(&pmu_sbi_driver);
1456e9991434SAtish Patra 		return PTR_ERR(pdev);
1457e9991434SAtish Patra 	}
1458e9991434SAtish Patra 
1459e9991434SAtish Patra 	/* Notify legacy implementation that SBI pmu is available*/
1460e9991434SAtish Patra 	riscv_pmu_legacy_skip_init();
1461e9991434SAtish Patra 
1462e9991434SAtish Patra 	return ret;
1463e9991434SAtish Patra }
1464e9991434SAtish Patra device_initcall(pmu_sbi_devinit)
1465