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>
23 #include <asm/errata_list.h>
25 #include <asm/hwcap.h>
27 PMU_FORMAT_ATTR(event, "config:0-47");
28 PMU_FORMAT_ATTR(firmware, "config:63");
30 static struct attribute *riscv_arch_formats_attr[] = {
31 &format_attr_event.attr,
32 &format_attr_firmware.attr,
36 static struct attribute_group riscv_pmu_format_group = {
38 .attrs = riscv_arch_formats_attr,
41 static const struct attribute_group *riscv_pmu_attr_groups[] = {
42 &riscv_pmu_format_group,
47 * RISC-V doesn't have hetergenous harts yet. This need to be part of
48 * per_cpu in case of harts with different pmu counters
50 static union sbi_pmu_ctr_info *pmu_ctr_list;
51 static bool riscv_pmu_use_irq;
52 static unsigned int riscv_pmu_irq_num;
53 static unsigned int riscv_pmu_irq;
55 struct sbi_pmu_event_data {
59 uint32_t event_code:16;
60 uint32_t event_type:4;
63 struct hw_cache_event {
67 uint32_t event_type:4;
75 static const struct sbi_pmu_event_data pmu_hw_event_map[] = {
76 [PERF_COUNT_HW_CPU_CYCLES] = {.hw_gen_event = {
77 SBI_PMU_HW_CPU_CYCLES,
78 SBI_PMU_EVENT_TYPE_HW, 0}},
79 [PERF_COUNT_HW_INSTRUCTIONS] = {.hw_gen_event = {
80 SBI_PMU_HW_INSTRUCTIONS,
81 SBI_PMU_EVENT_TYPE_HW, 0}},
82 [PERF_COUNT_HW_CACHE_REFERENCES] = {.hw_gen_event = {
83 SBI_PMU_HW_CACHE_REFERENCES,
84 SBI_PMU_EVENT_TYPE_HW, 0}},
85 [PERF_COUNT_HW_CACHE_MISSES] = {.hw_gen_event = {
86 SBI_PMU_HW_CACHE_MISSES,
87 SBI_PMU_EVENT_TYPE_HW, 0}},
88 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = {.hw_gen_event = {
89 SBI_PMU_HW_BRANCH_INSTRUCTIONS,
90 SBI_PMU_EVENT_TYPE_HW, 0}},
91 [PERF_COUNT_HW_BRANCH_MISSES] = {.hw_gen_event = {
92 SBI_PMU_HW_BRANCH_MISSES,
93 SBI_PMU_EVENT_TYPE_HW, 0}},
94 [PERF_COUNT_HW_BUS_CYCLES] = {.hw_gen_event = {
95 SBI_PMU_HW_BUS_CYCLES,
96 SBI_PMU_EVENT_TYPE_HW, 0}},
97 [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = {.hw_gen_event = {
98 SBI_PMU_HW_STALLED_CYCLES_FRONTEND,
99 SBI_PMU_EVENT_TYPE_HW, 0}},
100 [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = {.hw_gen_event = {
101 SBI_PMU_HW_STALLED_CYCLES_BACKEND,
102 SBI_PMU_EVENT_TYPE_HW, 0}},
103 [PERF_COUNT_HW_REF_CPU_CYCLES] = {.hw_gen_event = {
104 SBI_PMU_HW_REF_CPU_CYCLES,
105 SBI_PMU_EVENT_TYPE_HW, 0}},
108 #define C(x) PERF_COUNT_HW_CACHE_##x
109 static const struct sbi_pmu_event_data pmu_cache_event_map[PERF_COUNT_HW_CACHE_MAX]
110 [PERF_COUNT_HW_CACHE_OP_MAX]
111 [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
114 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
115 C(OP_READ), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
116 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
117 C(OP_READ), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
120 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
121 C(OP_WRITE), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
122 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
123 C(OP_WRITE), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
126 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
127 C(OP_PREFETCH), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
128 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
129 C(OP_PREFETCH), C(L1D), SBI_PMU_EVENT_TYPE_CACHE, 0}},
134 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
135 C(OP_READ), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
136 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS), C(OP_READ),
137 C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
140 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
141 C(OP_WRITE), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
142 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
143 C(OP_WRITE), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
146 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
147 C(OP_PREFETCH), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
148 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
149 C(OP_PREFETCH), C(L1I), SBI_PMU_EVENT_TYPE_CACHE, 0}},
154 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
155 C(OP_READ), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
156 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
157 C(OP_READ), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
160 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
161 C(OP_WRITE), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
162 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
163 C(OP_WRITE), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
166 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
167 C(OP_PREFETCH), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
168 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
169 C(OP_PREFETCH), C(LL), SBI_PMU_EVENT_TYPE_CACHE, 0}},
174 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
175 C(OP_READ), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
176 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
177 C(OP_READ), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
180 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
181 C(OP_WRITE), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
182 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
183 C(OP_WRITE), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
186 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
187 C(OP_PREFETCH), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
188 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
189 C(OP_PREFETCH), C(DTLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
194 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
195 C(OP_READ), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
196 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
197 C(OP_READ), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
200 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
201 C(OP_WRITE), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
202 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
203 C(OP_WRITE), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
206 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
207 C(OP_PREFETCH), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
208 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
209 C(OP_PREFETCH), C(ITLB), SBI_PMU_EVENT_TYPE_CACHE, 0}},
214 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
215 C(OP_READ), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
216 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
217 C(OP_READ), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
220 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
221 C(OP_WRITE), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
222 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
223 C(OP_WRITE), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
226 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
227 C(OP_PREFETCH), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
228 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
229 C(OP_PREFETCH), C(BPU), SBI_PMU_EVENT_TYPE_CACHE, 0}},
234 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
235 C(OP_READ), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
236 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
237 C(OP_READ), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
240 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
241 C(OP_WRITE), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
242 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
243 C(OP_WRITE), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
246 [C(RESULT_ACCESS)] = {.hw_cache_event = {C(RESULT_ACCESS),
247 C(OP_PREFETCH), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
248 [C(RESULT_MISS)] = {.hw_cache_event = {C(RESULT_MISS),
249 C(OP_PREFETCH), C(NODE), SBI_PMU_EVENT_TYPE_CACHE, 0}},
254 static int pmu_sbi_ctr_get_width(int idx)
256 return pmu_ctr_list[idx].width;
259 static bool pmu_sbi_ctr_is_fw(int cidx)
261 union sbi_pmu_ctr_info *info;
263 info = &pmu_ctr_list[cidx];
267 return (info->type == SBI_PMU_CTR_TYPE_FW) ? true : false;
270 static int pmu_sbi_ctr_get_idx(struct perf_event *event)
272 struct hw_perf_event *hwc = &event->hw;
273 struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
274 struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
278 unsigned long cflags = 0;
280 if (event->attr.exclude_kernel)
281 cflags |= SBI_PMU_CFG_FLAG_SET_SINH;
282 if (event->attr.exclude_user)
283 cflags |= SBI_PMU_CFG_FLAG_SET_UINH;
285 /* retrieve the available counter index */
286 #if defined(CONFIG_32BIT)
287 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase,
288 rvpmu->cmask, cflags, hwc->event_base, hwc->config,
291 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_CFG_MATCH, cbase,
292 rvpmu->cmask, cflags, hwc->event_base, hwc->config, 0);
295 pr_debug("Not able to find a counter for event %lx config %llx\n",
296 hwc->event_base, hwc->config);
297 return sbi_err_map_linux_errno(ret.error);
301 if (!test_bit(idx, &rvpmu->cmask) || !pmu_ctr_list[idx].value)
304 /* Additional sanity check for the counter id */
305 if (pmu_sbi_ctr_is_fw(idx)) {
306 if (!test_and_set_bit(idx, cpuc->used_fw_ctrs))
309 if (!test_and_set_bit(idx, cpuc->used_hw_ctrs))
316 static void pmu_sbi_ctr_clear_idx(struct perf_event *event)
319 struct hw_perf_event *hwc = &event->hw;
320 struct riscv_pmu *rvpmu = to_riscv_pmu(event->pmu);
321 struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
324 if (pmu_sbi_ctr_is_fw(idx))
325 clear_bit(idx, cpuc->used_fw_ctrs);
327 clear_bit(idx, cpuc->used_hw_ctrs);
330 static int pmu_event_find_cache(u64 config)
332 unsigned int cache_type, cache_op, cache_result, ret;
334 cache_type = (config >> 0) & 0xff;
335 if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
338 cache_op = (config >> 8) & 0xff;
339 if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
342 cache_result = (config >> 16) & 0xff;
343 if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
346 ret = pmu_cache_event_map[cache_type][cache_op][cache_result].event_idx;
351 static bool pmu_sbi_is_fw_event(struct perf_event *event)
353 u32 type = event->attr.type;
354 u64 config = event->attr.config;
356 if ((type == PERF_TYPE_RAW) && ((config >> 63) == 1))
362 static int pmu_sbi_event_map(struct perf_event *event, u64 *econfig)
364 u32 type = event->attr.type;
365 u64 config = event->attr.config;
371 case PERF_TYPE_HARDWARE:
372 if (config >= PERF_COUNT_HW_MAX)
374 ret = pmu_hw_event_map[event->attr.config].event_idx;
376 case PERF_TYPE_HW_CACHE:
377 ret = pmu_event_find_cache(config);
381 * As per SBI specification, the upper 16 bits must be unused for
382 * a raw event. Use the MSB (63b) to distinguish between hardware
383 * raw event and firmware events.
385 bSoftware = config >> 63;
386 raw_config_val = config & RISCV_PMU_RAW_EVENT_MASK;
388 if (raw_config_val < SBI_PMU_FW_MAX)
389 ret = (raw_config_val & 0xFFFF) |
390 (SBI_PMU_EVENT_TYPE_FW << 16);
394 ret = RISCV_PMU_RAW_EVENT_IDX;
395 *econfig = raw_config_val;
406 static u64 pmu_sbi_ctr_read(struct perf_event *event)
408 struct hw_perf_event *hwc = &event->hw;
411 union sbi_pmu_ctr_info info;
414 if (pmu_sbi_is_fw_event(event)) {
415 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_FW_READ,
416 hwc->idx, 0, 0, 0, 0, 0);
420 info = pmu_ctr_list[idx];
421 val = riscv_pmu_ctr_read_csr(info.csr);
422 if (IS_ENABLED(CONFIG_32BIT))
423 val = ((u64)riscv_pmu_ctr_read_csr(info.csr + 0x80)) << 31 | val;
429 static void pmu_sbi_ctr_start(struct perf_event *event, u64 ival)
432 struct hw_perf_event *hwc = &event->hw;
433 unsigned long flag = SBI_PMU_START_FLAG_SET_INIT_VALUE;
435 #if defined(CONFIG_32BIT)
436 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
437 1, flag, ival, ival >> 32, 0);
439 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, hwc->idx,
440 1, flag, ival, 0, 0);
442 if (ret.error && (ret.error != SBI_ERR_ALREADY_STARTED))
443 pr_err("Starting counter idx %d failed with error %d\n",
444 hwc->idx, sbi_err_map_linux_errno(ret.error));
447 static void pmu_sbi_ctr_stop(struct perf_event *event, unsigned long flag)
450 struct hw_perf_event *hwc = &event->hw;
452 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, hwc->idx, 1, flag, 0, 0, 0);
453 if (ret.error && (ret.error != SBI_ERR_ALREADY_STOPPED) &&
454 flag != SBI_PMU_STOP_FLAG_RESET)
455 pr_err("Stopping counter idx %d failed with error %d\n",
456 hwc->idx, sbi_err_map_linux_errno(ret.error));
459 static int pmu_sbi_find_num_ctrs(void)
463 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_NUM_COUNTERS, 0, 0, 0, 0, 0, 0);
467 return sbi_err_map_linux_errno(ret.error);
470 static int pmu_sbi_get_ctrinfo(int nctr, unsigned long *mask)
473 int i, num_hw_ctr = 0, num_fw_ctr = 0;
474 union sbi_pmu_ctr_info cinfo;
476 pmu_ctr_list = kcalloc(nctr, sizeof(*pmu_ctr_list), GFP_KERNEL);
480 for (i = 0; i < nctr; i++) {
481 ret = sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_GET_INFO, i, 0, 0, 0, 0, 0);
483 /* The logical counter ids are not expected to be contiguous */
488 cinfo.value = ret.value;
489 if (cinfo.type == SBI_PMU_CTR_TYPE_FW)
493 pmu_ctr_list[i].value = cinfo.value;
496 pr_info("%d firmware and %d hardware counters\n", num_fw_ctr, num_hw_ctr);
501 static inline void pmu_sbi_stop_all(struct riscv_pmu *pmu)
504 * No need to check the error because we are disabling all the counters
505 * which may include counters that are not enabled yet.
507 sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP,
508 0, pmu->cmask, 0, 0, 0, 0);
511 static inline void pmu_sbi_stop_hw_ctrs(struct riscv_pmu *pmu)
513 struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
515 /* No need to check the error here as we can't do anything about the error */
516 sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_STOP, 0,
517 cpu_hw_evt->used_hw_ctrs[0], 0, 0, 0, 0);
521 * This function starts all the used counters in two step approach.
522 * Any counter that did not overflow can be start in a single step
523 * while the overflowed counters need to be started with updated initialization
526 static inline void pmu_sbi_start_overflow_mask(struct riscv_pmu *pmu,
527 unsigned long ctr_ovf_mask)
530 struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
531 struct perf_event *event;
532 unsigned long flag = SBI_PMU_START_FLAG_SET_INIT_VALUE;
533 unsigned long ctr_start_mask = 0;
535 struct hw_perf_event *hwc;
538 ctr_start_mask = cpu_hw_evt->used_hw_ctrs[0] & ~ctr_ovf_mask;
540 /* Start all the counters that did not overflow in a single shot */
541 sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, 0, ctr_start_mask,
544 /* Reinitialize and start all the counter that overflowed */
545 while (ctr_ovf_mask) {
546 if (ctr_ovf_mask & 0x01) {
547 event = cpu_hw_evt->events[idx];
549 max_period = riscv_pmu_ctr_get_width_mask(event);
550 init_val = local64_read(&hwc->prev_count) & max_period;
551 #if defined(CONFIG_32BIT)
552 sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
553 flag, init_val, init_val >> 32, 0);
555 sbi_ecall(SBI_EXT_PMU, SBI_EXT_PMU_COUNTER_START, idx, 1,
556 flag, init_val, 0, 0);
558 perf_event_update_userpage(event);
560 ctr_ovf_mask = ctr_ovf_mask >> 1;
565 static irqreturn_t pmu_sbi_ovf_handler(int irq, void *dev)
567 struct perf_sample_data data;
568 struct pt_regs *regs;
569 struct hw_perf_event *hw_evt;
570 union sbi_pmu_ctr_info *info;
571 int lidx, hidx, fidx;
572 struct riscv_pmu *pmu;
573 struct perf_event *event;
574 unsigned long overflow;
575 unsigned long overflowed_ctrs = 0;
576 struct cpu_hw_events *cpu_hw_evt = dev;
577 u64 start_clock = sched_clock();
579 if (WARN_ON_ONCE(!cpu_hw_evt))
582 /* Firmware counter don't support overflow yet */
583 fidx = find_first_bit(cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS);
584 event = cpu_hw_evt->events[fidx];
586 csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));
590 pmu = to_riscv_pmu(event->pmu);
591 pmu_sbi_stop_hw_ctrs(pmu);
593 /* Overflow status register should only be read after counter are stopped */
594 ALT_SBI_PMU_OVERFLOW(overflow);
597 * Overflow interrupt pending bit should only be cleared after stopping
598 * all the counters to avoid any race condition.
600 csr_clear(CSR_SIP, BIT(riscv_pmu_irq_num));
602 /* No overflow bit is set */
606 regs = get_irq_regs();
608 for_each_set_bit(lidx, cpu_hw_evt->used_hw_ctrs, RISCV_MAX_COUNTERS) {
609 struct perf_event *event = cpu_hw_evt->events[lidx];
611 /* Skip if invalid event or user did not request a sampling */
612 if (!event || !is_sampling_event(event))
615 info = &pmu_ctr_list[lidx];
616 /* Do a sanity check */
617 if (!info || info->type != SBI_PMU_CTR_TYPE_HW)
620 /* compute hardware counter index */
621 hidx = info->csr - CSR_CYCLE;
622 /* check if the corresponding bit is set in sscountovf */
623 if (!(overflow & (1 << hidx)))
627 * Keep a track of overflowed counters so that they can be started
628 * with updated initial value.
630 overflowed_ctrs |= 1 << lidx;
632 riscv_pmu_event_update(event);
633 perf_sample_data_init(&data, 0, hw_evt->last_period);
634 if (riscv_pmu_event_set_period(event)) {
636 * Unlike other ISAs, RISC-V don't have to disable interrupts
637 * to avoid throttling here. As per the specification, the
638 * interrupt remains disabled until the OF bit is set.
639 * Interrupts are enabled again only during the start.
640 * TODO: We will need to stop the guest counters once
641 * virtualization support is added.
643 perf_event_overflow(event, &data, regs);
647 pmu_sbi_start_overflow_mask(pmu, overflowed_ctrs);
648 perf_sample_event_took(sched_clock() - start_clock);
653 static int pmu_sbi_starting_cpu(unsigned int cpu, struct hlist_node *node)
655 struct riscv_pmu *pmu = hlist_entry_safe(node, struct riscv_pmu, node);
656 struct cpu_hw_events *cpu_hw_evt = this_cpu_ptr(pmu->hw_events);
659 * Enable the access for CYCLE, TIME, and INSTRET CSRs from userspace,
660 * as is necessary to maintain uABI compatibility.
662 csr_write(CSR_SCOUNTEREN, 0x7);
664 /* Stop all the counters so that they can be enabled from perf */
665 pmu_sbi_stop_all(pmu);
667 if (riscv_pmu_use_irq) {
668 cpu_hw_evt->irq = riscv_pmu_irq;
669 csr_clear(CSR_IP, BIT(riscv_pmu_irq_num));
670 csr_set(CSR_IE, BIT(riscv_pmu_irq_num));
671 enable_percpu_irq(riscv_pmu_irq, IRQ_TYPE_NONE);
677 static int pmu_sbi_dying_cpu(unsigned int cpu, struct hlist_node *node)
679 if (riscv_pmu_use_irq) {
680 disable_percpu_irq(riscv_pmu_irq);
681 csr_clear(CSR_IE, BIT(riscv_pmu_irq_num));
684 /* Disable all counters access for user mode now */
685 csr_write(CSR_SCOUNTEREN, 0x0);
690 static int pmu_sbi_setup_irqs(struct riscv_pmu *pmu, struct platform_device *pdev)
693 struct cpu_hw_events __percpu *hw_events = pmu->hw_events;
694 struct device_node *cpu, *child;
695 struct irq_domain *domain = NULL;
697 if (riscv_isa_extension_available(NULL, SSCOFPMF)) {
698 riscv_pmu_irq_num = RV_IRQ_PMU;
699 riscv_pmu_use_irq = true;
700 } else if (IS_ENABLED(CONFIG_ERRATA_THEAD_PMU) &&
701 riscv_cached_mvendorid(0) == THEAD_VENDOR_ID &&
702 riscv_cached_marchid(0) == 0 &&
703 riscv_cached_mimpid(0) == 0) {
704 riscv_pmu_irq_num = THEAD_C9XX_RV_IRQ_PMU;
705 riscv_pmu_use_irq = true;
708 if (!riscv_pmu_use_irq)
711 for_each_of_cpu_node(cpu) {
712 child = of_get_compatible_child(cpu, "riscv,cpu-intc");
714 pr_err("Failed to find INTC node\n");
718 domain = irq_find_host(child);
726 pr_err("Failed to find INTC IRQ root domain\n");
730 riscv_pmu_irq = irq_create_mapping(domain, riscv_pmu_irq_num);
731 if (!riscv_pmu_irq) {
732 pr_err("Failed to map PMU interrupt for node\n");
736 ret = request_percpu_irq(riscv_pmu_irq, pmu_sbi_ovf_handler, "riscv-pmu", hw_events);
738 pr_err("registering percpu irq failed [%d]\n", ret);
746 static int riscv_pm_pmu_notify(struct notifier_block *b, unsigned long cmd,
749 struct riscv_pmu *rvpmu = container_of(b, struct riscv_pmu, riscv_pm_nb);
750 struct cpu_hw_events *cpuc = this_cpu_ptr(rvpmu->hw_events);
751 int enabled = bitmap_weight(cpuc->used_hw_ctrs, RISCV_MAX_COUNTERS);
752 struct perf_event *event;
758 for (idx = 0; idx < RISCV_MAX_COUNTERS; idx++) {
759 event = cpuc->events[idx];
766 * Stop and update the counter
768 riscv_pmu_stop(event, PERF_EF_UPDATE);
771 case CPU_PM_ENTER_FAILED:
773 * Restore and enable the counter.
775 riscv_pmu_start(event, PERF_EF_RELOAD);
785 static int riscv_pm_pmu_register(struct riscv_pmu *pmu)
787 pmu->riscv_pm_nb.notifier_call = riscv_pm_pmu_notify;
788 return cpu_pm_register_notifier(&pmu->riscv_pm_nb);
791 static void riscv_pm_pmu_unregister(struct riscv_pmu *pmu)
793 cpu_pm_unregister_notifier(&pmu->riscv_pm_nb);
796 static inline int riscv_pm_pmu_register(struct riscv_pmu *pmu) { return 0; }
797 static inline void riscv_pm_pmu_unregister(struct riscv_pmu *pmu) { }
800 static void riscv_pmu_destroy(struct riscv_pmu *pmu)
802 riscv_pm_pmu_unregister(pmu);
803 cpuhp_state_remove_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
806 static int pmu_sbi_device_probe(struct platform_device *pdev)
808 struct riscv_pmu *pmu = NULL;
809 unsigned long cmask = 0;
813 pr_info("SBI PMU extension is available\n");
814 pmu = riscv_pmu_alloc();
818 num_counters = pmu_sbi_find_num_ctrs();
819 if (num_counters < 0) {
820 pr_err("SBI PMU extension doesn't provide any counters\n");
824 /* cache all the information about counters now */
825 if (pmu_sbi_get_ctrinfo(num_counters, &cmask))
828 ret = pmu_sbi_setup_irqs(pmu, pdev);
830 pr_info("Perf sampling/filtering is not supported as sscof extension is not available\n");
831 pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
832 pmu->pmu.capabilities |= PERF_PMU_CAP_NO_EXCLUDE;
835 pmu->pmu.attr_groups = riscv_pmu_attr_groups;
837 pmu->ctr_start = pmu_sbi_ctr_start;
838 pmu->ctr_stop = pmu_sbi_ctr_stop;
839 pmu->event_map = pmu_sbi_event_map;
840 pmu->ctr_get_idx = pmu_sbi_ctr_get_idx;
841 pmu->ctr_get_width = pmu_sbi_ctr_get_width;
842 pmu->ctr_clear_idx = pmu_sbi_ctr_clear_idx;
843 pmu->ctr_read = pmu_sbi_ctr_read;
845 ret = cpuhp_state_add_instance(CPUHP_AP_PERF_RISCV_STARTING, &pmu->node);
849 ret = riscv_pm_pmu_register(pmu);
853 ret = perf_pmu_register(&pmu->pmu, "cpu", PERF_TYPE_RAW);
860 riscv_pmu_destroy(pmu);
867 static struct platform_driver pmu_sbi_driver = {
868 .probe = pmu_sbi_device_probe,
870 .name = RISCV_PMU_PDEV_NAME,
874 static int __init pmu_sbi_devinit(void)
877 struct platform_device *pdev;
879 if (sbi_spec_version < sbi_mk_version(0, 3) ||
880 sbi_probe_extension(SBI_EXT_PMU) <= 0) {
884 ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_RISCV_STARTING,
885 "perf/riscv/pmu:starting",
886 pmu_sbi_starting_cpu, pmu_sbi_dying_cpu);
888 pr_err("CPU hotplug notifier could not be registered: %d\n",
893 ret = platform_driver_register(&pmu_sbi_driver);
897 pdev = platform_device_register_simple(RISCV_PMU_PDEV_NAME, -1, NULL, 0);
899 platform_driver_unregister(&pmu_sbi_driver);
900 return PTR_ERR(pdev);
903 /* Notify legacy implementation that SBI pmu is available*/
904 riscv_pmu_legacy_skip_init();
908 device_initcall(pmu_sbi_devinit)