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 code is based on ARM perf event code which is in turn based on
8 * sparc64 and x86 code.
11 #define pr_fmt(fmt) "riscv-pmu-sbi: " fmt
13 #include <linux/mod_devicetable.h>
14 #include <linux/perf/riscv_pmu.h>
15 #include <linux/platform_device.h>
16 #include <linux/irq.h>
17 #include <linux/irqdomain.h>
18 #include <linux/of_irq.h>
20 #include <linux/cpu_pm.h>
21 #include <linux/sched/clock.h>
24 #include <asm/hwcap.h>
26 PMU_FORMAT_ATTR(event, "config:0-47");
27 PMU_FORMAT_ATTR(firmware, "config:63");
29 static struct attribute *riscv_arch_formats_attr[] = {
30 &format_attr_event.attr,
31 &format_attr_firmware.attr,
35 static struct attribute_group riscv_pmu_format_group = {
37 .attrs = riscv_arch_formats_attr,
40 static const struct attribute_group *riscv_pmu_attr_groups[] = {
41 &riscv_pmu_format_group,
46 * RISC-V doesn't have hetergenous harts yet. This need to be part of
47 * per_cpu in case of harts with different pmu counters
49 static union sbi_pmu_ctr_info *pmu_ctr_list;
50 static unsigned int riscv_pmu_irq;
52 struct sbi_pmu_event_data {
56 uint32_t event_code:16;
57 uint32_t event_type:4;
60 struct hw_cache_event {
64 uint32_t event_type:4;
72 static const struct sbi_pmu_event_data pmu_hw_event_map[] = {
73 [PERF_COUNT_HW_CPU_CYCLES] = {.hw_gen_event = {
74 SBI_PMU_HW_CPU_CYCLES,
75 SBI_PMU_EVENT_TYPE_HW, 0}},
76 [PERF_COUNT_HW_INSTRUCTIONS] = {.hw_gen_event = {
77 SBI_PMU_HW_INSTRUCTIONS,
78 SBI_PMU_EVENT_TYPE_HW, 0}},
79 [PERF_COUNT_HW_CACHE_REFERENCES] = {.hw_gen_event = {
80 SBI_PMU_HW_CACHE_REFERENCES,
81 SBI_PMU_EVENT_TYPE_HW, 0}},
82 [PERF_COUNT_HW_CACHE_MISSES] = {.hw_gen_event = {
83 SBI_PMU_HW_CACHE_MISSES,
84 SBI_PMU_EVENT_TYPE_HW, 0}},
85 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {.hw_gen_event = {
86 SBI_PMU_HW_BRANCH_INSTRUCTIONS,
87 SBI_PMU_EVENT_TYPE_HW, 0}},
88 [PERF_COUNT_HW_BRANCH_MISSES] = {.hw_gen_event = {
89 SBI_PMU_HW_BRANCH_MISSES,
90 SBI_PMU_EVENT_TYPE_HW, 0}},
91 [PERF_COUNT_HW_BUS_CYCLES] = {.hw_gen_event = {
92 SBI_PMU_HW_BUS_CYCLES,
93 SBI_PMU_EVENT_TYPE_HW, 0}},
94 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {.hw_gen_event = {
95 SBI_PMU_HW_STALLED_CYCLES_FRONTEND,
96 SBI_PMU_EVENT_TYPE_HW, 0}},
97 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {.hw_gen_event = {
98 SBI_PMU_HW_STALLED_CYCLES_BACKEND,
99 SBI_PMU_EVENT_TYPE_HW, 0}},
100 [PERF_COUNT_HW_REF_CPU_CYCLES] = {.hw_gen_event = {
101 SBI_PMU_HW_REF_CPU_CYCLES,
102 SBI_PMU_EVENT_TYPE_HW, 0}},
105 #define C(x) PERF_COUNT_HW_CACHE_##x
106 static const struct sbi_pmu_event_data pmu_cache_event_map[PERF_COUNT_HW_CACHE_MAX]
107 [PERF_COUNT_HW_CACHE_OP_MAX]
108 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
111 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
112 C(OP_READ), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
113 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
114 C(OP_READ), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
117 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
118 C(OP_WRITE), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
119 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
120 C(OP_WRITE), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
123 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
124 C(OP_PREFETCH), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
125 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
126 C(OP_PREFETCH), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
131 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
132 C(OP_READ), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
133 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS), C(OP_READ),
134 C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
137 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
138 C(OP_WRITE), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
139 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
140 C(OP_WRITE), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
143 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
144 C(OP_PREFETCH), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
145 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
146 C(OP_PREFETCH), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
151 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
152 C(OP_READ), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
153 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
154 C(OP_READ), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
157 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
158 C(OP_WRITE), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
159 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
160 C(OP_WRITE), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
163 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
164 C(OP_PREFETCH), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
165 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
166 C(OP_PREFETCH), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
171 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
172 C(OP_READ), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
173 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
174 C(OP_READ), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
177 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
178 C(OP_WRITE), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
179 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
180 C(OP_WRITE), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
183 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
184 C(OP_PREFETCH), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
185 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
186 C(OP_PREFETCH), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
191 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
192 C(OP_READ), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
193 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
194 C(OP_READ), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
197 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
198 C(OP_WRITE), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
199 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
200 C(OP_WRITE), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
203 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
204 C(OP_PREFETCH), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
205 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
206 C(OP_PREFETCH), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
211 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
212 C(OP_READ), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
213 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
214 C(OP_READ), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
217 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
218 C(OP_WRITE), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
219 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
220 C(OP_WRITE), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
223 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
224 C(OP_PREFETCH), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
225 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
226 C(OP_PREFETCH), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
231 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
232 C(OP_READ), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
233 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
234 C(OP_READ), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
237 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
238 C(OP_WRITE), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
239 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
240 C(OP_WRITE), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
243 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
244 C(OP_PREFETCH), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
245 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
246 C(OP_PREFETCH), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
251 static int pmu_sbi_ctr_get_width(int idx)
253 return pmu_ctr_list[idx].width;
256 static bool pmu_sbi_ctr_is_fw(int cidx)
258 union sbi_pmu_ctr_info *info;
260 info = &pmu_ctr_list[cidx];
264 return (info->type == SBI_PMU_CTR_TYPE_FW) ? true : false;
267 static int pmu_sbi_ctr_get_idx(struct perf_event *event)
269 struct hw_perf_event *hwc = &event->hw;
270 struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
271 struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
275 unsigned long cflags = 0;
277 if (event->attr.exclude_kernel)
278 cflags |= SBI_PMU_CFG_FLAG_SET_SINH;
279 if (event->attr.exclude_user)
280 cflags |= SBI_PMU_CFG_FLAG_SET_UINH;
282 /* retrieve the available counter index */
283 #if defined(CONFIG_32BIT)
284 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase,
285 rvpmu->cmask, cflags, hwc->event_base, hwc->config,
288 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase,
289 rvpmu->cmask, cflags, hwc->event_base, hwc->config, 0);
292 pr_debug("Not able to find a counter for event %lx config %llx\n",
293 hwc->event_base, hwc->config);
294 return sbi_err_map_linux_errno(ret.error);
298 if (!test_bit(idx, &rvpmu->cmask) || !pmu_ctr_list[idx].value)
301 /* Additional sanity check for the counter id */
302 if (pmu_sbi_ctr_is_fw(idx)) {
303 if (!test_and_set_bit(idx, cpuc->used_fw_ctrs))
306 if (!test_and_set_bit(idx, cpuc->used_hw_ctrs))
313 static void pmu_sbi_ctr_clear_idx(struct perf_event *event)
316 struct hw_perf_event *hwc = &event->hw;
317 struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
318 struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
321 if (pmu_sbi_ctr_is_fw(idx))
322 clear_bit(idx, cpuc->used_fw_ctrs);
324 clear_bit(idx, cpuc->used_hw_ctrs);
327 static int pmu_event_find_cache(u64 config)
329 unsigned int cache_type, cache_op, cache_result, ret;
331 cache_type = (config >> 0) & 0xff;
332 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
335 cache_op = (config >> 8) & 0xff;
336 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
339 cache_result = (config >> 16) & 0xff;
340 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
343 ret = pmu_cache_event_map[cache_type][cache_op][cache_result].event_idx;
348 static bool pmu_sbi_is_fw_event(struct perf_event *event)
350 u32 type = event->attr.type;
351 u64 config = event->attr.config;
353 if ((type == PERF_TYPE_RAW) && ((config >> 63) == 1))
359 static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
361 u32 type = event->attr.type;
362 u64 config = event->attr.config;
368 case PERF_TYPE_HARDWARE:
369 if (config >= PERF_COUNT_HW_MAX)
371 ret = pmu_hw_event_map[event->attr.config].event_idx;
373 case PERF_TYPE_HW_CACHE:
374 ret = pmu_event_find_cache(config);
378 * As per SBI specification, the upper 16 bits must be unused for
379 * a raw event. Use the MSB (63b) to distinguish between hardware
380 * raw event and firmware events.
382 bSoftware = config >> 63;
383 raw_config_val = config & RISCV_PMU_RAW_EVENT_MASK;
385 if (raw_config_val < SBI_PMU_FW_MAX)
386 ret = (raw_config_val & 0xFFFF) |
387 (SBI_PMU_EVENT_TYPE_FW << 16);
391 ret = RISCV_PMU_RAW_EVENT_IDX;
392 *econfig = raw_config_val;
403 static u64 pmu_sbi_ctr_read(struct perf_event *event)
405 struct hw_perf_event *hwc = &event->hw;
408 union sbi_pmu_ctr_info info;
411 if (pmu_sbi_is_fw_event(event)) {
412 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_FW_READ,
413 hwc->idx, 0, 0, 0, 0, 0);
417 info = pmu_ctr_list[idx];
418 val = riscv_pmu_ctr_read_csr(info.csr);
419 if (IS_ENABLED(CONFIG_32BIT))
420 val = ((u64)riscv_pmu_ctr_read_csr(info.csr + 0x80)) << 31 | val;
426 static void pmu_sbi_ctr_start(struct perf_event *event, u64 ival)
429 struct hw_perf_event *hwc = &event->hw;
430 unsigned long flag = SBI_PMU_START_FLAG_SET_INIT_VALUE;
432 #if defined(CONFIG_32BIT)
433 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
434 1, flag, ival, ival >> 32, 0);
436 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
437 1, flag, ival, 0, 0);
439 if (ret.error && (ret.error != SBI_ERR_ALREADY_STARTED))
440 pr_err("Starting counter idx %d failed with error %d\n",
441 hwc->idx, sbi_err_map_linux_errno(ret.error));
444 static void pmu_sbi_ctr_stop(struct perf_event *event, unsigned long flag)
447 struct hw_perf_event *hwc = &event->hw;
449 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, hwc->idx, 1, flag, 0, 0, 0);
450 if (ret.error && (ret.error != SBI_ERR_ALREADY_STOPPED) &&
451 flag != SBI_PMU_STOP_FLAG_RESET)
452 pr_err("Stopping counter idx %d failed with error %d\n",
453 hwc->idx, sbi_err_map_linux_errno(ret.error));
456 static int pmu_sbi_find_num_ctrs(void)
460 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_NUM_COUNTERS, 0, 0, 0, 0, 0, 0);
464 return sbi_err_map_linux_errno(ret.error);
467 static int pmu_sbi_get_ctrinfo(int nctr, unsigned long *mask)
470 int i, num_hw_ctr = 0, num_fw_ctr = 0;
471 union sbi_pmu_ctr_info cinfo;
473 pmu_ctr_list = kcalloc(nctr, sizeof(*pmu_ctr_list), GFP_KERNEL);
477 for (i = 0; i < nctr; i++) {
478 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_GET_INFO, i, 0, 0, 0, 0, 0);
480 /* The logical counter ids are not expected to be contiguous */
485 cinfo.value = ret.value;
486 if (cinfo.type == SBI_PMU_CTR_TYPE_FW)
490 pmu_ctr_list[i].value = cinfo.value;
493 pr_info("%d firmware and %d hardware counters\n", num_fw_ctr, num_hw_ctr);
498 static inline void pmu_sbi_stop_all(struct riscv_pmu *pmu)
501 * No need to check the error because we are disabling all the counters
502 * which may include counters that are not enabled yet.
504 sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP,
505 0, pmu->cmask, 0, 0, 0, 0);
508 static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu)
510 struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
512 /* No need to check the error here as we can't do anything about the error */
513 sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, 0,
514 cpu_hw_evt->used_hw_ctrs[0], 0, 0, 0, 0);
518 * This function starts all the used counters in two step approach.
519 * Any counter that did not overflow can be start in a single step
520 * while the overflowed counters need to be started with updated initialization
523 static inline void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
524 unsigned long ctr_ovf_mask)
527 struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
528 struct perf_event *event;
529 unsigned long flag = SBI_PMU_START_FLAG_SET_INIT_VALUE;
530 unsigned long ctr_start_mask = 0;
532 struct hw_perf_event *hwc;
535 ctr_start_mask = cpu_hw_evt->used_hw_ctrs[0] & ~ctr_ovf_mask;
537 /* Start all the counters that did not overflow in a single shot */
538 sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, 0, ctr_start_mask,
541 /* Reinitialize and start all the counter that overflowed */
542 while (ctr_ovf_mask) {
543 if (ctr_ovf_mask & 0x01) {
544 event = cpu_hw_evt->events[idx];
546 max_period = riscv_pmu_ctr_get_width_mask(event);
547 init_val = local64_read(&hwc->prev_count) & max_period;
548 #if defined(CONFIG_32BIT)
549 sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
550 flag, init_val, init_val >> 32, 0);
552 sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
553 flag, init_val, 0, 0);
555 perf_event_update_userpage(event);
557 ctr_ovf_mask = ctr_ovf_mask >> 1;
562 static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
564 struct perf_sample_data data;
565 struct pt_regs *regs;
566 struct hw_perf_event *hw_evt;
567 union sbi_pmu_ctr_info *info;
568 int lidx, hidx, fidx;
569 struct riscv_pmu *pmu;
570 struct perf_event *event;
571 unsigned long overflow;
572 unsigned long overflowed_ctrs = 0;
573 struct cpu_hw_events *cpu_hw_evt = dev;
574 u64 start_clock = sched_clock();
576 if (WARN_ON_ONCE(!cpu_hw_evt))
579 /* Firmware counter don't support overflow yet */
580 fidx = find_first_bit(cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS);
581 event = cpu_hw_evt->events[fidx];
583 csr_clear(CSR_SIP, SIP_LCOFIP);
587 pmu = to_riscv_pmu(event->pmu);
588 pmu_sbi_stop_hw_ctrs(pmu);
590 /* Overflow status register should only be read after counter are stopped */
591 overflow = csr_read(CSR_SSCOUNTOVF);
594 * Overflow interrupt pending bit should only be cleared after stopping
595 * all the counters to avoid any race condition.
597 csr_clear(CSR_SIP, SIP_LCOFIP);
599 /* No overflow bit is set */
603 regs = get_irq_regs();
605 for_each_set_bit(lidx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS) {
606 struct perf_event *event = cpu_hw_evt->events[lidx];
608 /* Skip if invalid event or user did not request a sampling */
609 if (!event || !is_sampling_event(event))
612 info = &pmu_ctr_list[lidx];
613 /* Do a sanity check */
614 if (!info || info->type != SBI_PMU_CTR_TYPE_HW)
617 /* compute hardware counter index */
618 hidx = info->csr - CSR_CYCLE;
619 /* check if the corresponding bit is set in sscountovf */
620 if (!(overflow & (1 << hidx)))
624 * Keep a track of overflowed counters so that they can be started
625 * with updated initial value.
627 overflowed_ctrs |= 1 << lidx;
629 riscv_pmu_event_update(event);
630 perf_sample_data_init(&data, 0, hw_evt->last_period);
631 if (riscv_pmu_event_set_period(event)) {
633 * Unlike other ISAs, RISC-V don't have to disable interrupts
634 * to avoid throttling here. As per the specification, the
635 * interrupt remains disabled until the OF bit is set.
636 * Interrupts are enabled again only during the start.
637 * TODO: We will need to stop the guest counters once
638 * virtualization support is added.
640 perf_event_overflow(event, &data, regs);
644 pmu_sbi_start_overflow_mask(pmu, overflowed_ctrs);
645 perf_sample_event_took(sched_clock() - start_clock);
650 static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
652 struct riscv_pmu *pmu = hlist_entry_safe(node, struct riscv_pmu, node);
653 struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
655 /* Enable the access for TIME csr only from the user mode now */
656 csr_write(CSR_SCOUNTEREN, 0x2);
658 /* Stop all the counters so that they can be enabled from perf */
659 pmu_sbi_stop_all(pmu);
661 if (riscv_isa_extension_available(NULL, SSCOFPMF)) {
662 cpu_hw_evt->irq = riscv_pmu_irq;
663 csr_clear(CSR_IP, BIT(RV_IRQ_PMU));
664 csr_set(CSR_IE, BIT(RV_IRQ_PMU));
665 enable_percpu_irq(riscv_pmu_irq, IRQ_TYPE_NONE);
671 static int pmu_sbi_dying_cpu(unsigned int cpu, struct hlist_node *node)
673 if (riscv_isa_extension_available(NULL, SSCOFPMF)) {
674 disable_percpu_irq(riscv_pmu_irq);
675 csr_clear(CSR_IE, BIT(RV_IRQ_PMU));
678 /* Disable all counters access for user mode now */
679 csr_write(CSR_SCOUNTEREN, 0x0);
684 static int pmu_sbi_setup_irqs(struct riscv_pmu *pmu, struct platform_device *pdev)
687 struct cpu_hw_events __percpu *hw_events = pmu->hw_events;
688 struct device_node *cpu, *child;
689 struct irq_domain *domain = NULL;
691 if (!riscv_isa_extension_available(NULL, SSCOFPMF))
694 for_each_of_cpu_node(cpu) {
695 child = of_get_compatible_child(cpu, "riscv,cpu-intc");
697 pr_err("Failed to find INTC node\n");
701 domain = irq_find_host(child);
709 pr_err("Failed to find INTC IRQ root domain\n");
713 riscv_pmu_irq = irq_create_mapping(domain, RV_IRQ_PMU);
714 if (!riscv_pmu_irq) {
715 pr_err("Failed to map PMU interrupt for node\n");
719 ret = request_percpu_irq(riscv_pmu_irq, pmu_sbi_ovf_handler, "riscv-pmu", hw_events);
721 pr_err("registering percpu irq failed [%d]\n", ret);
729 static int riscv_pm_pmu_notify(struct notifier_block *b, unsigned long cmd,
732 struct riscv_pmu *rvpmu = container_of(b, struct riscv_pmu, riscv_pm_nb);
733 struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
734 int enabled = bitmap_weight(cpuc->used_hw_ctrs, RISCV_MAX_COUNTERS);
735 struct perf_event *event;
741 for (idx = 0; idx < RISCV_MAX_COUNTERS; idx++) {
742 event = cpuc->events[idx];
749 * Stop and update the counter
751 riscv_pmu_stop(event, PERF_EF_UPDATE);
754 case CPU_PM_ENTER_FAILED:
756 * Restore and enable the counter.
758 * Requires RCU read locking to be functional,
759 * wrap the call within RCU_NONIDLE to make the
760 * RCU subsystem aware this cpu is not idle from
761 * an RCU perspective for the riscv_pmu_start() call
764 RCU_NONIDLE(riscv_pmu_start(event, PERF_EF_RELOAD));
774 static int riscv_pm_pmu_register(struct riscv_pmu *pmu)
776 pmu->riscv_pm_nb.notifier_call = riscv_pm_pmu_notify;
777 return cpu_pm_register_notifier(&pmu->riscv_pm_nb);
780 static void riscv_pm_pmu_unregister(struct riscv_pmu *pmu)
782 cpu_pm_unregister_notifier(&pmu->riscv_pm_nb);
785 static inline int riscv_pm_pmu_register(struct riscv_pmu *pmu) { return 0; }
786 static inline void riscv_pm_pmu_unregister(struct riscv_pmu *pmu) { }
789 static void riscv_pmu_destroy(struct riscv_pmu *pmu)
791 riscv_pm_pmu_unregister(pmu);
792 cpuhp_state_remove_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
795 static int pmu_sbi_device_probe(struct platform_device *pdev)
797 struct riscv_pmu *pmu = NULL;
798 unsigned long cmask = 0;
802 pr_info("SBI PMU extension is available\n");
803 pmu = riscv_pmu_alloc();
807 num_counters = pmu_sbi_find_num_ctrs();
808 if (num_counters < 0) {
809 pr_err("SBI PMU extension doesn't provide any counters\n");
813 /* cache all the information about counters now */
814 if (pmu_sbi_get_ctrinfo(num_counters, &cmask))
817 ret = pmu_sbi_setup_irqs(pmu, pdev);
819 pr_info("Perf sampling/filtering is not supported as sscof extension is not available\n");
820 pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
821 pmu->pmu.capabilities |= PERF_PMU_CAP_NO_EXCLUDE;
824 pmu->pmu.attr_groups = riscv_pmu_attr_groups;
826 pmu->ctr_start = pmu_sbi_ctr_start;
827 pmu->ctr_stop = pmu_sbi_ctr_stop;
828 pmu->event_map = pmu_sbi_event_map;
829 pmu->ctr_get_idx = pmu_sbi_ctr_get_idx;
830 pmu->ctr_get_width = pmu_sbi_ctr_get_width;
831 pmu->ctr_clear_idx = pmu_sbi_ctr_clear_idx;
832 pmu->ctr_read = pmu_sbi_ctr_read;
834 ret = cpuhp_state_add_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
838 ret = riscv_pm_pmu_register(pmu);
842 ret = perf_pmu_register(&pmu->pmu, "cpu", PERF_TYPE_RAW);
849 riscv_pmu_destroy(pmu);
856 static struct platform_driver pmu_sbi_driver = {
857 .probe = pmu_sbi_device_probe,
859 .name = RISCV_PMU_PDEV_NAME,
863 static int __init pmu_sbi_devinit(void)
866 struct platform_device *pdev;
868 if (sbi_spec_version < sbi_mk_version(0, 3) ||
869 sbi_probe_extension(SBI_EXT_PMU) <= 0) {
873 ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_RISCV_STARTING,
874 "perf/riscv/pmu:starting",
875 pmu_sbi_starting_cpu, pmu_sbi_dying_cpu);
877 pr_err("CPU hotplug notifier could not be registered: %d\n",
882 ret = platform_driver_register(&pmu_sbi_driver);
886 pdev = platform_device_register_simple(RISCV_PMU_PDEV_NAME, -1, NULL, 0);
888 platform_driver_unregister(&pmu_sbi_driver);
889 return PTR_ERR(pdev);
892 /* Notify legacy implementation that SBI pmu is available*/
893 riscv_pmu_legacy_skip_init();
897 device_initcall(pmu_sbi_devinit)