1 // SPDX-License-Identifier: GPL-2.0
3 * RISC-V performance counter support.
5 * Copyright (C) 2021 Western Digital Corporation or its affiliates.
7 * This implementation is based on old RISC-V perf and ARM perf event code
8 * which are in turn based on sparc64 and x86 code.
11 #include <linux/cpumask.h>
12 #include <linux/irq.h>
13 #include <linux/irqdesc.h>
14 #include <linux/perf/riscv_pmu.h>
15 #include <linux/printk.h>
16 #include <linux/smp.h>
17 #include <linux/sched_clock.h>
21 static bool riscv_perf_user_access(struct perf_event *event)
23 return ((event->attr.type == PERF_TYPE_HARDWARE) ||
24 (event->attr.type == PERF_TYPE_HW_CACHE) ||
25 (event->attr.type == PERF_TYPE_RAW)) &&
26 !!(event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT) &&
27 (event->hw.idx != -1);
30 void arch_perf_update_userpage(struct perf_event *event,
31 struct perf_event_mmap_page *userpg, u64 now)
33 struct clock_read_data *rd;
37 userpg->cap_user_time = 0;
38 userpg->cap_user_time_zero = 0;
39 userpg->cap_user_time_short = 0;
40 userpg->cap_user_rdpmc = riscv_perf_user_access(event);
42 #ifdef CONFIG_RISCV_PMU
44 * The counters are 64-bit but the priv spec doesn't mandate all the
45 * bits to be implemented: that's why, counter width can vary based on
48 if (userpg->cap_user_rdpmc)
49 userpg->pmc_width = to_riscv_pmu(event->pmu)->ctr_get_width(event->hw.idx) + 1;
53 rd = sched_clock_read_begin(&seq);
55 userpg->time_mult = rd->mult;
56 userpg->time_shift = rd->shift;
57 userpg->time_zero = rd->epoch_ns;
58 userpg->time_cycles = rd->epoch_cyc;
59 userpg->time_mask = rd->sched_clock_mask;
62 * Subtract the cycle base, such that software that
63 * doesn't know about cap_user_time_short still 'works'
66 ns = mul_u64_u32_shr(rd->epoch_cyc, rd->mult, rd->shift);
67 userpg->time_zero -= ns;
69 } while (sched_clock_read_retry(seq));
71 userpg->time_offset = userpg->time_zero - now;
74 * time_shift is not expected to be greater than 31 due to
75 * the original published conversion algorithm shifting a
76 * 32-bit value (now specifies a 64-bit value) - refer
77 * perf_event_mmap_page documentation in perf_event.h.
79 if (userpg->time_shift == 32) {
80 userpg->time_shift = 31;
81 userpg->time_mult >>= 1;
85 * Internal timekeeping for enabled/running/stopped times
86 * is always computed with the sched_clock.
88 userpg->cap_user_time = 1;
89 userpg->cap_user_time_zero = 1;
90 userpg->cap_user_time_short = 1;
93 static unsigned long csr_read_num(int csr_num)
95 #define switchcase_csr_read(__csr_num, __val) {\
97 __val = csr_read(__csr_num); \
99 #define switchcase_csr_read_2(__csr_num, __val) {\
100 switchcase_csr_read(__csr_num + 0, __val) \
101 switchcase_csr_read(__csr_num + 1, __val)}
102 #define switchcase_csr_read_4(__csr_num, __val) {\
103 switchcase_csr_read_2(__csr_num + 0, __val) \
104 switchcase_csr_read_2(__csr_num + 2, __val)}
105 #define switchcase_csr_read_8(__csr_num, __val) {\
106 switchcase_csr_read_4(__csr_num + 0, __val) \
107 switchcase_csr_read_4(__csr_num + 4, __val)}
108 #define switchcase_csr_read_16(__csr_num, __val) {\
109 switchcase_csr_read_8(__csr_num + 0, __val) \
110 switchcase_csr_read_8(__csr_num + 8, __val)}
111 #define switchcase_csr_read_32(__csr_num, __val) {\
112 switchcase_csr_read_16(__csr_num + 0, __val) \
113 switchcase_csr_read_16(__csr_num + 16, __val)}
115 unsigned long ret = 0;
118 switchcase_csr_read_32(CSR_CYCLE, ret)
119 switchcase_csr_read_32(CSR_CYCLEH, ret)
125 #undef switchcase_csr_read_32
126 #undef switchcase_csr_read_16
127 #undef switchcase_csr_read_8
128 #undef switchcase_csr_read_4
129 #undef switchcase_csr_read_2
130 #undef switchcase_csr_read
134 * Read the CSR of a corresponding counter.
136 unsigned long riscv_pmu_ctr_read_csr(unsigned long csr)
138 if (csr < CSR_CYCLE || csr > CSR_HPMCOUNTER31H ||
139 (csr > CSR_HPMCOUNTER31 && csr < CSR_CYCLEH)) {
140 pr_err("Invalid performance counter csr %lx\n", csr);
144 return csr_read_num(csr);
147 u64 riscv_pmu_ctr_get_width_mask(struct perf_event *event)
150 struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
151 struct hw_perf_event *hwc = &event->hw;
153 if (!rvpmu->ctr_get_width)
155 * If the pmu driver doesn't support counter width, set it to default
156 * maximum allowed by the specification.
161 /* Handle init case where idx is not initialized yet */
162 cwidth = rvpmu->ctr_get_width(0);
164 cwidth = rvpmu->ctr_get_width(hwc->idx);
167 return GENMASK_ULL(cwidth, 0);
170 u64 riscv_pmu_event_update(struct perf_event *event)
172 struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
173 struct hw_perf_event *hwc = &event->hw;
174 u64 prev_raw_count, new_raw_count;
178 if (!rvpmu->ctr_read)
181 cmask = riscv_pmu_ctr_get_width_mask(event);
184 prev_raw_count = local64_read(&hwc->prev_count);
185 new_raw_count = rvpmu->ctr_read(event);
186 oldval = local64_cmpxchg(&hwc->prev_count, prev_raw_count,
188 } while (oldval != prev_raw_count);
190 delta = (new_raw_count - prev_raw_count) & cmask;
191 local64_add(delta, &event->count);
192 local64_sub(delta, &hwc->period_left);
197 void riscv_pmu_stop(struct perf_event *event, int flags)
199 struct hw_perf_event *hwc = &event->hw;
200 struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
202 WARN_ON_ONCE(hwc->state & PERF_HES_STOPPED);
204 if (!(hwc->state & PERF_HES_STOPPED)) {
205 if (rvpmu->ctr_stop) {
206 rvpmu->ctr_stop(event, 0);
207 hwc->state |= PERF_HES_STOPPED;
209 riscv_pmu_event_update(event);
210 hwc->state |= PERF_HES_UPTODATE;
214 int riscv_pmu_event_set_period(struct perf_event *event)
216 struct hw_perf_event *hwc = &event->hw;
217 s64 left = local64_read(&hwc->period_left);
218 s64 period = hwc->sample_period;
220 uint64_t max_period = riscv_pmu_ctr_get_width_mask(event);
222 if (unlikely(left <= -period)) {
224 local64_set(&hwc->period_left, left);
225 hwc->last_period = period;
229 if (unlikely(left <= 0)) {
231 local64_set(&hwc->period_left, left);
232 hwc->last_period = period;
237 * Limit the maximum period to prevent the counter value
238 * from overtaking the one we are about to program. In
239 * effect we are reducing max_period to account for
240 * interrupt latency (and we are being very conservative).
242 if (left > (max_period >> 1))
243 left = (max_period >> 1);
245 local64_set(&hwc->prev_count, (u64)-left);
247 perf_event_update_userpage(event);
252 void riscv_pmu_start(struct perf_event *event, int flags)
254 struct hw_perf_event *hwc = &event->hw;
255 struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
256 uint64_t max_period = riscv_pmu_ctr_get_width_mask(event);
259 if (flags & PERF_EF_RELOAD)
260 WARN_ON_ONCE(!(event->hw.state & PERF_HES_UPTODATE));
263 riscv_pmu_event_set_period(event);
264 init_val = local64_read(&hwc->prev_count) & max_period;
265 rvpmu->ctr_start(event, init_val);
266 perf_event_update_userpage(event);
269 static int riscv_pmu_add(struct perf_event *event, int flags)
271 struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
272 struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
273 struct hw_perf_event *hwc = &event->hw;
276 idx = rvpmu->ctr_get_idx(event);
281 cpuc->events[idx] = event;
283 hwc->state = PERF_HES_UPTODATE | PERF_HES_STOPPED;
284 if (flags & PERF_EF_START)
285 riscv_pmu_start(event, PERF_EF_RELOAD);
287 /* Propagate our changes to the userspace mapping. */
288 perf_event_update_userpage(event);
293 static void riscv_pmu_del(struct perf_event *event, int flags)
295 struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
296 struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
297 struct hw_perf_event *hwc = &event->hw;
299 riscv_pmu_stop(event, PERF_EF_UPDATE);
300 cpuc->events[hwc->idx] = NULL;
301 /* The firmware need to reset the counter mapping */
303 rvpmu->ctr_stop(event, RISCV_PMU_STOP_FLAG_RESET);
305 if (rvpmu->ctr_clear_idx)
306 rvpmu->ctr_clear_idx(event);
307 perf_event_update_userpage(event);
311 static void riscv_pmu_read(struct perf_event *event)
313 riscv_pmu_event_update(event);
316 static int riscv_pmu_event_init(struct perf_event *event)
318 struct hw_perf_event *hwc = &event->hw;
319 struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
321 u64 event_config = 0;
325 mapped_event = rvpmu->event_map(event, &event_config);
326 if (mapped_event < 0) {
327 pr_debug("event %x:%llx not supported\n", event->attr.type,
333 * idx is set to -1 because the index of a general event should not be
334 * decided until binding to some counter in pmu->add().
335 * config will contain the information about counter CSR
336 * the idx will contain the counter index
338 hwc->config = event_config;
340 hwc->event_base = mapped_event;
342 if (rvpmu->event_init)
343 rvpmu->event_init(event);
345 if (!is_sampling_event(event)) {
347 * For non-sampling runs, limit the sample_period to half
348 * of the counter width. That way, the new counter value
349 * is far less likely to overtake the previous one unless
350 * you have some serious IRQ latency issues.
352 cmask = riscv_pmu_ctr_get_width_mask(event);
353 hwc->sample_period = cmask >> 1;
354 hwc->last_period = hwc->sample_period;
355 local64_set(&hwc->period_left, hwc->sample_period);
361 static int riscv_pmu_event_idx(struct perf_event *event)
363 struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
365 if (!(event->hw.flags & PERF_EVENT_FLAG_USER_READ_CNT))
368 if (rvpmu->csr_index)
369 return rvpmu->csr_index(event) + 1;
374 static void riscv_pmu_event_mapped(struct perf_event *event, struct mm_struct *mm)
376 struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
378 if (rvpmu->event_mapped) {
379 rvpmu->event_mapped(event, mm);
380 perf_event_update_userpage(event);
384 static void riscv_pmu_event_unmapped(struct perf_event *event, struct mm_struct *mm)
386 struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
388 if (rvpmu->event_unmapped) {
389 rvpmu->event_unmapped(event, mm);
390 perf_event_update_userpage(event);
394 struct riscv_pmu *riscv_pmu_alloc(void)
396 struct riscv_pmu *pmu;
398 struct cpu_hw_events *cpuc;
400 pmu = kzalloc(sizeof(*pmu), GFP_KERNEL);
404 pmu->hw_events = alloc_percpu_gfp(struct cpu_hw_events, GFP_KERNEL);
405 if (!pmu->hw_events) {
406 pr_info("failed to allocate per-cpu PMU data.\n");
410 for_each_possible_cpu(cpuid) {
411 cpuc = per_cpu_ptr(pmu->hw_events, cpuid);
413 for (i = 0; i < RISCV_MAX_COUNTERS; i++)
414 cpuc->events[i] = NULL;
416 pmu->pmu = (struct pmu) {
417 .event_init = riscv_pmu_event_init,
418 .event_mapped = riscv_pmu_event_mapped,
419 .event_unmapped = riscv_pmu_event_unmapped,
420 .event_idx = riscv_pmu_event_idx,
421 .add = riscv_pmu_add,
422 .del = riscv_pmu_del,
423 .start = riscv_pmu_start,
424 .stop = riscv_pmu_stop,
425 .read = riscv_pmu_read,