Debian Codegraph @

arch/riscv/kvm/vcpu_pmu.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2023 Rivos Inc
4 *
5 * Authors:
6 * Atish Patra <[email protected]>
7 */
8
9 #define pr_fmt(fmt) "riscv-kvm-pmu: " fmt
10 #include <linux/errno.h>
11 #include <linux/err.h>
12 #include <linux/kvm_host.h>
13 #include <linux/nospec.h>
14 #include <linux/perf/riscv_pmu.h>
15 #include <asm/csr.h>
16 #include <asm/kvm_vcpu_sbi.h>
17 #include <asm/kvm_vcpu_pmu.h>
18 #include <asm/sbi.h>
19 #include <linux/bitops.h>
20
21 #define kvm_pmu_num_counters(pmu) ((pmu)->num_hw_ctrs + (pmu)->num_fw_ctrs)
22 #define get_event_type(x) (((x) & SBI_PMU_EVENT_IDX_TYPE_MASK) >> 16)
23 #define get_event_code(x) ((x) & SBI_PMU_EVENT_IDX_CODE_MASK)
24
25 static enum perf_hw_id hw_event_perf_map[SBI_PMU_HW_GENERAL_MAX] = {
26 [SBI_PMU_HW_CPU_CYCLES] = PERF_COUNT_HW_CPU_CYCLES,
27 [SBI_PMU_HW_INSTRUCTIONS] = PERF_COUNT_HW_INSTRUCTIONS,
28 [SBI_PMU_HW_CACHE_REFERENCES] = PERF_COUNT_HW_CACHE_REFERENCES,
29 [SBI_PMU_HW_CACHE_MISSES] = PERF_COUNT_HW_CACHE_MISSES,
30 [SBI_PMU_HW_BRANCH_INSTRUCTIONS] = PERF_COUNT_HW_BRANCH_INSTRUCTIONS,
31 [SBI_PMU_HW_BRANCH_MISSES] = PERF_COUNT_HW_BRANCH_MISSES,
32 [SBI_PMU_HW_BUS_CYCLES] = PERF_COUNT_HW_BUS_CYCLES,
33 [SBI_PMU_HW_STALLED_CYCLES_FRONTEND] = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND,
34 [SBI_PMU_HW_STALLED_CYCLES_BACKEND] = PERF_COUNT_HW_STALLED_CYCLES_BACKEND,
35 [SBI_PMU_HW_REF_CPU_CYCLES] = PERF_COUNT_HW_REF_CPU_CYCLES,
36 };
37
38 static u64 kvm_pmu_get_sample_period(struct kvm_pmc *pmc)
39 {
40 u64 counter_val_mask = GENMASK(pmc->cinfo.width, 0);
41 u64 sample_period;
42
43 if (!pmc->counter_val)
44 sample_period = counter_val_mask;
45 else
46 sample_period = (-pmc->counter_val) & counter_val_mask;
47
48 return sample_period;
49 }
50
51 static u32 kvm_pmu_get_perf_event_type(unsigned long eidx)
52 {
53 enum sbi_pmu_event_type etype = get_event_type(eidx);
54 u32 type = PERF_TYPE_MAX;
55
56 switch (etype) {
57 case SBI_PMU_EVENT_TYPE_HW:
58 type = PERF_TYPE_HARDWARE;
59 break;
60 case SBI_PMU_EVENT_TYPE_CACHE:
61 type = PERF_TYPE_HW_CACHE;
62 break;
63 case SBI_PMU_EVENT_TYPE_RAW:
64 case SBI_PMU_EVENT_TYPE_RAW_V2:
65 case SBI_PMU_EVENT_TYPE_FW:
66 type = PERF_TYPE_RAW;
67 break;
68 default:
69 break;
70 }
71
72 return type;
73 }
74
75 static bool kvm_pmu_is_fw_event(unsigned long eidx)
76 {
77 return get_event_type(eidx) == SBI_PMU_EVENT_TYPE_FW;
78 }
79
80 static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc)
81 {
82 if (pmc->perf_event) {
83 perf_event_disable(pmc->perf_event);
84 perf_event_release_kernel(pmc->perf_event);
85 pmc->perf_event = NULL;
86 }
87 }
88
89 static u64 kvm_pmu_get_perf_event_hw_config(u32 sbi_event_code)
90 {
91 return hw_event_perf_map[array_index_nospec(sbi_event_code,
92 SBI_PMU_HW_GENERAL_MAX)];
93 }
94
95 static u64 kvm_pmu_get_perf_event_cache_config(u32 sbi_event_code)
96 {
97 u64 config = U64_MAX;
98 unsigned int cache_type, cache_op, cache_result;
99
100 /* All the cache event masks lie within 0xFF. No separate masking is necessary */
101 cache_type = (sbi_event_code & SBI_PMU_EVENT_CACHE_ID_CODE_MASK) >>
102 SBI_PMU_EVENT_CACHE_ID_SHIFT;
103 cache_op = (sbi_event_code & SBI_PMU_EVENT_CACHE_OP_ID_CODE_MASK) >>
104 SBI_PMU_EVENT_CACHE_OP_SHIFT;
105 cache_result = sbi_event_code & SBI_PMU_EVENT_CACHE_RESULT_ID_CODE_MASK;
106
107 if (cache_type >= PERF_COUNT_HW_CACHE_MAX ||
108 cache_op >= PERF_COUNT_HW_CACHE_OP_MAX ||
109 cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
110 return config;
111
112 config = cache_type | (cache_op << 8) | (cache_result << 16);
113
114 return config;
115 }
116
117 static u64 kvm_pmu_get_perf_event_config(unsigned long eidx, uint64_t evt_data)
118 {
119 enum sbi_pmu_event_type etype = get_event_type(eidx);
120 u32 ecode = get_event_code(eidx);
121 u64 config = U64_MAX;
122
123 switch (etype) {
124 case SBI_PMU_EVENT_TYPE_HW:
125 if (ecode < SBI_PMU_HW_GENERAL_MAX)
126 config = kvm_pmu_get_perf_event_hw_config(ecode);
127 break;
128 case SBI_PMU_EVENT_TYPE_CACHE:
129 config = kvm_pmu_get_perf_event_cache_config(ecode);
130 break;
131 case SBI_PMU_EVENT_TYPE_RAW:
132 config = evt_data & RISCV_PMU_RAW_EVENT_MASK;
133 break;
134 case SBI_PMU_EVENT_TYPE_RAW_V2:
135 config = evt_data & RISCV_PMU_RAW_EVENT_V2_MASK;
136 break;
137 case SBI_PMU_EVENT_TYPE_FW:
138 if (ecode < SBI_PMU_FW_MAX)
139 config = (1ULL << 63) | ecode;
140 break;
141 default:
142 break;
143 }
144
145 return config;
146 }
147
148 static int kvm_pmu_get_fixed_pmc_index(unsigned long eidx)
149 {
150 u32 etype = kvm_pmu_get_perf_event_type(eidx);
151 u32 ecode = get_event_code(eidx);
152
153 if (etype != SBI_PMU_EVENT_TYPE_HW)
154 return -EINVAL;
155
156 if (ecode == SBI_PMU_HW_CPU_CYCLES)
157 return 0;
158 else if (ecode == SBI_PMU_HW_INSTRUCTIONS)
159 return 2;
160 else
161 return -EINVAL;
162 }
163
164 static int kvm_pmu_get_programmable_pmc_index(struct kvm_pmu *kvpmu, unsigned long eidx,
165 unsigned long cbase, unsigned long cmask)
166 {
167 int ctr_idx = -1;
168 int i, pmc_idx;
169 int min, max;
170
171 if (kvm_pmu_is_fw_event(eidx)) {
172 /* Firmware counters are mapped 1:1 starting from num_hw_ctrs for simplicity */
173 min = kvpmu->num_hw_ctrs;
174 max = min + kvpmu->num_fw_ctrs;
175 } else {
176 /* First 3 counters are reserved for fixed counters */
177 min = 3;
178 max = kvpmu->num_hw_ctrs;
179 }
180
181 for_each_set_bit(i, &cmask, BITS_PER_LONG) {
182 pmc_idx = i + cbase;
183 if ((pmc_idx >= min && pmc_idx < max) &&
184 !test_bit(pmc_idx, kvpmu->pmc_in_use)) {
185 ctr_idx = pmc_idx;
186 break;
187 }
188 }
189
190 return ctr_idx;
191 }
192
193 static int pmu_get_pmc_index(struct kvm_pmu *pmu, unsigned long eidx,
194 unsigned long cbase, unsigned long cmask)
195 {
196 int ret;
197
198 /* Fixed counters need to be have fixed mapping as they have different width */
199 ret = kvm_pmu_get_fixed_pmc_index(eidx);
200 if (ret >= 0)
201 return ret;
202
203 return kvm_pmu_get_programmable_pmc_index(pmu, eidx, cbase, cmask);
204 }
205
206 static int pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long cidx,
207 unsigned long *out_val)
208 {
209 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
210 struct kvm_pmc *pmc;
211 int fevent_code;
212
213 if (!IS_ENABLED(CONFIG_32BIT)) {
214 pr_warn("%s: should be invoked for only RV32\n", __func__);
215 return -EINVAL;
216 }
217
218 if (cidx >= kvm_pmu_num_counters(kvpmu) || cidx == 1) {
219 pr_warn("Invalid counter id [%ld]during read\n", cidx);
220 return -EINVAL;
221 }
222
223 cidx = array_index_nospec(cidx, RISCV_KVM_MAX_COUNTERS);
224 pmc = &kvpmu->pmc[cidx];
225
226 if (pmc->cinfo.type != SBI_PMU_CTR_TYPE_FW)
227 return -EINVAL;
228
229 fevent_code = get_event_code(pmc->event_idx);
230 pmc->counter_val = kvpmu->fw_event[fevent_code].value;
231
232 *out_val = pmc->counter_val >> 32;
233
234 return 0;
235 }
236
237 static int pmu_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx,
238 unsigned long *out_val)
239 {
240 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
241 struct kvm_pmc *pmc;
242 u64 enabled, running;
243 int fevent_code;
244
245 if (cidx >= kvm_pmu_num_counters(kvpmu) || cidx == 1) {
246 pr_warn("Invalid counter id [%ld] during read\n", cidx);
247 return -EINVAL;
248 }
249
250 cidx = array_index_nospec(cidx, RISCV_KVM_MAX_COUNTERS);
251 pmc = &kvpmu->pmc[cidx];
252
253 if (pmc->cinfo.type == SBI_PMU_CTR_TYPE_FW) {
254 fevent_code = get_event_code(pmc->event_idx);
255 pmc->counter_val = kvpmu->fw_event[fevent_code].value;
256 } else if (pmc->perf_event) {
257 pmc->counter_val += perf_event_read_value(pmc->perf_event, &enabled, &running);
258 } else {
259 return -EINVAL;
260 }
261 *out_val = pmc->counter_val;
262
263 return 0;
264 }
265
266 static int kvm_pmu_validate_counter_mask(struct kvm_pmu *kvpmu, unsigned long ctr_base,
267 unsigned long ctr_mask)
268 {
269 /* Make sure the we have a valid counter mask requested from the caller */
270 if (!ctr_mask || (ctr_base + __fls(ctr_mask) >= kvm_pmu_num_counters(kvpmu)))
271 return -EINVAL;
272
273 return 0;
274 }
275
276 static void kvm_riscv_pmu_overflow(struct perf_event *perf_event,
277 struct perf_sample_data *data,
278 struct pt_regs *regs)
279 {
280 struct kvm_pmc *pmc = perf_event->overflow_handler_context;
281 struct kvm_vcpu *vcpu = pmc->vcpu;
282 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
283 struct riscv_pmu *rpmu = to_riscv_pmu(perf_event->pmu);
284 u64 period;
285
286 /*
287 * Stop the event counting by directly accessing the perf_event.
288 * Otherwise, this needs to deferred via a workqueue.
289 * That will introduce skew in the counter value because the actual
290 * physical counter would start after returning from this function.
291 * It will be stopped again once the workqueue is scheduled
292 */
293 rpmu->pmu.stop(perf_event, PERF_EF_UPDATE);
294
295 /*
296 * The hw counter would start automatically when this function returns.
297 * Thus, the host may continue to interrupt and inject it to the guest
298 * even without the guest configuring the next event. Depending on the hardware
299 * the host may have some sluggishness only if privilege mode filtering is not
300 * available. In an ideal world, where qemu is not the only capable hardware,
301 * this can be removed.
302 * FYI: ARM64 does this way while x86 doesn't do anything as such.
303 * TODO: Should we keep it for RISC-V ?
304 */
305 period = -(local64_read(&perf_event->count));
306
307 local64_set(&perf_event->hw.period_left, 0);
308 perf_event->attr.sample_period = period;
309 perf_event->hw.sample_period = period;
310
311 set_bit(pmc->idx, kvpmu->pmc_overflown);
312 kvm_riscv_vcpu_set_interrupt(vcpu, IRQ_PMU_OVF);
313
314 rpmu->pmu.start(perf_event, PERF_EF_RELOAD);
315 }
316
317 static long kvm_pmu_create_perf_event(struct kvm_pmc *pmc, struct perf_event_attr *attr,
318 unsigned long flags, unsigned long eidx,
319 unsigned long evtdata)
320 {
321 struct perf_event *event;
322
323 kvm_pmu_release_perf_event(pmc);
324 attr->config = kvm_pmu_get_perf_event_config(eidx, evtdata);
325 if (flags & SBI_PMU_CFG_FLAG_CLEAR_VALUE) {
326 //TODO: Do we really want to clear the value in hardware counter
327 pmc->counter_val = 0;
328 }
329
330 /*
331 * Set the default sample_period for now. The guest specified value
332 * will be updated in the start call.
333 */
334 attr->sample_period = kvm_pmu_get_sample_period(pmc);
335
336 event = perf_event_create_kernel_counter(attr, -1, current, kvm_riscv_pmu_overflow, pmc);
337 if (IS_ERR(event)) {
338 pr_debug("kvm pmu event creation failed for eidx %lx: %ld\n", eidx, PTR_ERR(event));
339 return PTR_ERR(event);
340 }
341
342 pmc->perf_event = event;
343 if (flags & SBI_PMU_CFG_FLAG_AUTO_START)
344 perf_event_enable(pmc->perf_event);
345
346 return 0;
347 }
348
349 int kvm_riscv_vcpu_pmu_incr_fw(struct kvm_vcpu *vcpu, unsigned long fid)
350 {
351 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
352 struct kvm_fw_event *fevent;
353
354 if (!kvpmu || fid >= SBI_PMU_FW_MAX)
355 return -EINVAL;
356
357 fevent = &kvpmu->fw_event[fid];
358 if (fevent->started)
359 fevent->value++;
360
361 return 0;
362 }
363
364 int kvm_riscv_vcpu_pmu_read_hpm(struct kvm_vcpu *vcpu, unsigned int csr_num,
365 unsigned long *val, unsigned long new_val,
366 unsigned long wr_mask)
367 {
368 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
369 int cidx, ret = KVM_INSN_CONTINUE_NEXT_SEPC;
370
371 if (!kvpmu || !kvpmu->init_done) {
372 /*
373 * In absence of sscofpmf in the platform, the guest OS may use
374 * the legacy PMU driver to read cycle/instret. In that case,
375 * just return 0 to avoid any illegal trap. However, any other
376 * hpmcounter access should result in illegal trap as they must
377 * be access through SBI PMU only.
378 */
379 if (csr_num == CSR_CYCLE || csr_num == CSR_INSTRET) {
380 *val = 0;
381 return ret;
382 } else {
383 return KVM_INSN_ILLEGAL_TRAP;
384 }
385 }
386
387 /* The counter CSR are read only. Thus, any write should result in illegal traps */
388 if (wr_mask)
389 return KVM_INSN_ILLEGAL_TRAP;
390
391 cidx = csr_num - CSR_CYCLE;
392
393 if (pmu_ctr_read(vcpu, cidx, val) < 0)
394 return KVM_INSN_ILLEGAL_TRAP;
395
396 return ret;
397 }
398
399 static void kvm_pmu_clear_snapshot_area(struct kvm_vcpu *vcpu)
400 {
401 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
402
403 kfree(kvpmu->sdata);
404 kvpmu->sdata = NULL;
405 kvpmu->snapshot_addr = INVALID_GPA;
406 }
407
408 int kvm_riscv_vcpu_pmu_snapshot_set_shmem(struct kvm_vcpu *vcpu, unsigned long saddr_low,
409 unsigned long saddr_high, unsigned long flags,
410 struct kvm_vcpu_sbi_return *retdata)
411 {
412 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
413 int snapshot_area_size = sizeof(struct riscv_pmu_snapshot_data);
414 int sbiret = 0;
415 gpa_t saddr;
416
417 if (!kvpmu || flags) {
418 sbiret = SBI_ERR_INVALID_PARAM;
419 goto out;
420 }
421
422 if (saddr_low == SBI_SHMEM_DISABLE && saddr_high == SBI_SHMEM_DISABLE) {
423 kvm_pmu_clear_snapshot_area(vcpu);
424 return 0;
425 }
426
427 saddr = saddr_low;
428
429 if (saddr_high != 0) {
430 if (IS_ENABLED(CONFIG_32BIT))
431 saddr |= ((gpa_t)saddr_high << 32);
432 else
433 sbiret = SBI_ERR_INVALID_ADDRESS;
434 goto out;
435 }
436
437 kvpmu->sdata = kzalloc(snapshot_area_size, GFP_ATOMIC);
438 if (!kvpmu->sdata) {
439 sbiret = SBI_ERR_FAILURE;
440 goto out;
441 }
442
443 /* No need to check writable slot explicitly as kvm_vcpu_write_guest does it internally */
444 if (kvm_vcpu_write_guest(vcpu, saddr, kvpmu->sdata, snapshot_area_size)) {
445 kfree(kvpmu->sdata);
446 sbiret = SBI_ERR_INVALID_ADDRESS;
447 goto out;
448 }
449
450 kvpmu->snapshot_addr = saddr;
451
452 out:
453 retdata->err_val = sbiret;
454
455 return 0;
456 }
457
458 int kvm_riscv_vcpu_pmu_event_info(struct kvm_vcpu *vcpu, unsigned long saddr_low,
459 unsigned long saddr_high, unsigned long num_events,
460 unsigned long flags, struct kvm_vcpu_sbi_return *retdata)
461 {
462 struct riscv_pmu_event_info *einfo = NULL;
463 int shmem_size = num_events * sizeof(*einfo);
464 gpa_t shmem;
465 u32 eidx, etype;
466 u64 econfig;
467 int ret;
468
469 if (flags != 0 || (saddr_low & (SZ_16 - 1) || num_events == 0)) {
470 ret = SBI_ERR_INVALID_PARAM;
471 goto out;
472 }
473
474 shmem = saddr_low;
475 if (saddr_high != 0) {
476 if (IS_ENABLED(CONFIG_32BIT)) {
477 shmem |= ((gpa_t)saddr_high << 32);
478 } else {
479 ret = SBI_ERR_INVALID_ADDRESS;
480 goto out;
481 }
482 }
483
484 einfo = kzalloc(shmem_size, GFP_KERNEL);
485 if (!einfo) {
486 ret = SBI_ERR_FAILURE;
487 goto out;
488 }
489
490 ret = kvm_vcpu_read_guest(vcpu, shmem, einfo, shmem_size);
491 if (ret) {
492 ret = SBI_ERR_FAILURE;
493 goto free_mem;
494 }
495
496 for (int i = 0; i < num_events; i++) {
497 eidx = einfo[i].event_idx;
498 etype = kvm_pmu_get_perf_event_type(eidx);
499 econfig = kvm_pmu_get_perf_event_config(eidx, einfo[i].event_data);
500 ret = riscv_pmu_get_event_info(etype, econfig, NULL);
501 einfo[i].output = (ret > 0) ? 1 : 0;
502 }
503
504 ret = kvm_vcpu_write_guest(vcpu, shmem, einfo, shmem_size);
505 if (ret)
506 ret = SBI_ERR_INVALID_ADDRESS;
507
508 free_mem:
509 kfree(einfo);
510 out:
511 retdata->err_val = ret;
512
513 return 0;
514 }
515
516 int kvm_riscv_vcpu_pmu_num_ctrs(struct kvm_vcpu *vcpu,
517 struct kvm_vcpu_sbi_return *retdata)
518 {
519 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
520
521 retdata->out_val = kvm_pmu_num_counters(kvpmu);
522
523 return 0;
524 }
525
526 int kvm_riscv_vcpu_pmu_ctr_info(struct kvm_vcpu *vcpu, unsigned long cidx,
527 struct kvm_vcpu_sbi_return *retdata)
528 {
529 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
530
531 if (cidx >= RISCV_KVM_MAX_COUNTERS || cidx == 1) {
532 retdata->err_val = SBI_ERR_INVALID_PARAM;
533 return 0;
534 }
535
536 cidx = array_index_nospec(cidx, RISCV_KVM_MAX_COUNTERS);
537 retdata->out_val = kvpmu->pmc[cidx].cinfo.value;
538
539 return 0;
540 }
541
542 int kvm_riscv_vcpu_pmu_ctr_start(struct kvm_vcpu *vcpu, unsigned long ctr_base,
543 unsigned long ctr_mask, unsigned long flags, u64 ival,
544 struct kvm_vcpu_sbi_return *retdata)
545 {
546 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
547 int i, pmc_index, sbiret = 0;
548 struct kvm_pmc *pmc;
549 int fevent_code;
550 bool snap_flag_set = flags & SBI_PMU_START_FLAG_INIT_SNAPSHOT;
551
552 if (kvm_pmu_validate_counter_mask(kvpmu, ctr_base, ctr_mask) < 0) {
553 sbiret = SBI_ERR_INVALID_PARAM;
554 goto out;
555 }
556
557 if (snap_flag_set) {
558 if (kvpmu->snapshot_addr == INVALID_GPA) {
559 sbiret = SBI_ERR_NO_SHMEM;
560 goto out;
561 }
562 if (kvm_vcpu_read_guest(vcpu, kvpmu->snapshot_addr, kvpmu->sdata,
563 sizeof(struct riscv_pmu_snapshot_data))) {
564 pr_warn("Unable to read snapshot shared memory while starting counters\n");
565 sbiret = SBI_ERR_FAILURE;
566 goto out;
567 }
568 }
569 /* Start the counters that have been configured and requested by the guest */
570 for_each_set_bit(i, &ctr_mask, RISCV_MAX_COUNTERS) {
571 pmc_index = array_index_nospec(i + ctr_base,
572 RISCV_KVM_MAX_COUNTERS);
573 if (!test_bit(pmc_index, kvpmu->pmc_in_use))
574 continue;
575 /* The guest started the counter again. Reset the overflow status */
576 clear_bit(pmc_index, kvpmu->pmc_overflown);
577 pmc = &kvpmu->pmc[pmc_index];
578 if (flags & SBI_PMU_START_FLAG_SET_INIT_VALUE) {
579 pmc->counter_val = ival;
580 } else if (snap_flag_set) {
581 /* The counter index in the snapshot are relative to the counter base */
582 pmc->counter_val = kvpmu->sdata->ctr_values[i];
583 }
584
585 if (pmc->cinfo.type == SBI_PMU_CTR_TYPE_FW) {
586 fevent_code = get_event_code(pmc->event_idx);
587 if (fevent_code >= SBI_PMU_FW_MAX) {
588 sbiret = SBI_ERR_INVALID_PARAM;
589 goto out;
590 }
591
592 /* Check if the counter was already started for some reason */
593 if (kvpmu->fw_event[fevent_code].started) {
594 sbiret = SBI_ERR_ALREADY_STARTED;
595 continue;
596 }
597
598 kvpmu->fw_event[fevent_code].started = true;
599 kvpmu->fw_event[fevent_code].value = pmc->counter_val;
600 } else if (pmc->perf_event) {
601 if (unlikely(pmc->started)) {
602 sbiret = SBI_ERR_ALREADY_STARTED;
603 continue;
604 }
605 perf_event_period(pmc->perf_event, kvm_pmu_get_sample_period(pmc));
606 perf_event_enable(pmc->perf_event);
607 pmc->started = true;
608 } else {
609 sbiret = SBI_ERR_INVALID_PARAM;
610 }
611 }
612
613 out:
614 retdata->err_val = sbiret;
615
616 return 0;
617 }
618
619 int kvm_riscv_vcpu_pmu_ctr_stop(struct kvm_vcpu *vcpu, unsigned long ctr_base,
620 unsigned long ctr_mask, unsigned long flags,
621 struct kvm_vcpu_sbi_return *retdata)
622 {
623 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
624 int i, pmc_index, sbiret = 0;
625 u64 enabled, running;
626 struct kvm_pmc *pmc;
627 int fevent_code;
628 bool snap_flag_set = flags & SBI_PMU_STOP_FLAG_TAKE_SNAPSHOT;
629 bool shmem_needs_update = false;
630
631 if (kvm_pmu_validate_counter_mask(kvpmu, ctr_base, ctr_mask) < 0) {
632 sbiret = SBI_ERR_INVALID_PARAM;
633 goto out;
634 }
635
636 if (snap_flag_set && kvpmu->snapshot_addr == INVALID_GPA) {
637 sbiret = SBI_ERR_NO_SHMEM;
638 goto out;
639 }
640
641 /* Stop the counters that have been configured and requested by the guest */
642 for_each_set_bit(i, &ctr_mask, RISCV_MAX_COUNTERS) {
643 pmc_index = array_index_nospec(i + ctr_base,
644 RISCV_KVM_MAX_COUNTERS);
645 if (!test_bit(pmc_index, kvpmu->pmc_in_use))
646 continue;
647 pmc = &kvpmu->pmc[pmc_index];
648 if (pmc->cinfo.type == SBI_PMU_CTR_TYPE_FW) {
649 fevent_code = get_event_code(pmc->event_idx);
650 if (fevent_code >= SBI_PMU_FW_MAX) {
651 sbiret = SBI_ERR_INVALID_PARAM;
652 goto out;
653 }
654
655 if (!kvpmu->fw_event[fevent_code].started)
656 sbiret = SBI_ERR_ALREADY_STOPPED;
657
658 kvpmu->fw_event[fevent_code].started = false;
659 } else if (pmc->perf_event) {
660 if (pmc->started) {
661 /* Stop counting the counter */
662 perf_event_disable(pmc->perf_event);
663 pmc->started = false;
664 } else {
665 sbiret = SBI_ERR_ALREADY_STOPPED;
666 }
667
668 if (flags & SBI_PMU_STOP_FLAG_RESET)
669 /* Release the counter if this is a reset request */
670 kvm_pmu_release_perf_event(pmc);
671 } else {
672 sbiret = SBI_ERR_INVALID_PARAM;
673 }
674
675 if (snap_flag_set && !sbiret) {
676 if (pmc->cinfo.type == SBI_PMU_CTR_TYPE_FW)
677 pmc->counter_val = kvpmu->fw_event[fevent_code].value;
678 else if (pmc->perf_event)
679 pmc->counter_val += perf_event_read_value(pmc->perf_event,
680 &enabled, &running);
681 /*
682 * The counter and overflow indicies in the snapshot region are w.r.to
683 * cbase. Modify the set bit in the counter mask instead of the pmc_index
684 * which indicates the absolute counter index.
685 */
686 if (test_bit(pmc_index, kvpmu->pmc_overflown))
687 kvpmu->sdata->ctr_overflow_mask |= BIT(i);
688 kvpmu->sdata->ctr_values[i] = pmc->counter_val;
689 shmem_needs_update = true;
690 }
691
692 if (flags & SBI_PMU_STOP_FLAG_RESET) {
693 pmc->event_idx = SBI_PMU_EVENT_IDX_INVALID;
694 clear_bit(pmc_index, kvpmu->pmc_in_use);
695 clear_bit(pmc_index, kvpmu->pmc_overflown);
696 if (snap_flag_set) {
697 /*
698 * Only clear the given counter as the caller is responsible to
699 * validate both the overflow mask and configured counters.
700 */
701 kvpmu->sdata->ctr_overflow_mask &= ~BIT(i);
702 shmem_needs_update = true;
703 }
704 }
705 }
706
707 if (shmem_needs_update)
708 kvm_vcpu_write_guest(vcpu, kvpmu->snapshot_addr, kvpmu->sdata,
709 sizeof(struct riscv_pmu_snapshot_data));
710
711 out:
712 retdata->err_val = sbiret;
713
714 return 0;
715 }
716
717 int kvm_riscv_vcpu_pmu_ctr_cfg_match(struct kvm_vcpu *vcpu, unsigned long ctr_base,
718 unsigned long ctr_mask, unsigned long flags,
719 unsigned long eidx, u64 evtdata,
720 struct kvm_vcpu_sbi_return *retdata)
721 {
722 int ctr_idx, sbiret = 0;
723 long ret;
724 bool is_fevent;
725 unsigned long event_code;
726 u32 etype = kvm_pmu_get_perf_event_type(eidx);
727 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
728 struct kvm_pmc *pmc = NULL;
729 struct perf_event_attr attr = {
730 .type = etype,
731 .size = sizeof(struct perf_event_attr),
732 .pinned = true,
733 .disabled = true,
734 /*
735 * It should never reach here if the platform doesn't support the sscofpmf
736 * extension as mode filtering won't work without it.
737 */
738 .exclude_host = true,
739 .exclude_hv = true,
740 .exclude_user = !!(flags & SBI_PMU_CFG_FLAG_SET_UINH),
741 .exclude_kernel = !!(flags & SBI_PMU_CFG_FLAG_SET_SINH),
742 .config1 = RISCV_PMU_CONFIG1_GUEST_EVENTS,
743 };
744
745 if (kvm_pmu_validate_counter_mask(kvpmu, ctr_base, ctr_mask) < 0) {
746 sbiret = SBI_ERR_INVALID_PARAM;
747 goto out;
748 }
749
750 event_code = get_event_code(eidx);
751 is_fevent = kvm_pmu_is_fw_event(eidx);
752 if (is_fevent && event_code >= SBI_PMU_FW_MAX) {
753 sbiret = SBI_ERR_NOT_SUPPORTED;
754 goto out;
755 }
756
757 /*
758 * SKIP_MATCH flag indicates the caller is aware of the assigned counter
759 * for this event. Just do a sanity check if it already marked used.
760 */
761 if (flags & SBI_PMU_CFG_FLAG_SKIP_MATCH) {
762 if (!test_bit(ctr_base + __ffs(ctr_mask), kvpmu->pmc_in_use)) {
763 sbiret = SBI_ERR_FAILURE;
764 goto out;
765 }
766 ctr_idx = ctr_base + __ffs(ctr_mask);
767 } else {
768 ctr_idx = pmu_get_pmc_index(kvpmu, eidx, ctr_base, ctr_mask);
769 if (ctr_idx < 0) {
770 sbiret = SBI_ERR_NOT_SUPPORTED;
771 goto out;
772 }
773 }
774
775 ctr_idx = array_index_nospec(ctr_idx, RISCV_KVM_MAX_COUNTERS);
776 pmc = &kvpmu->pmc[ctr_idx];
777 pmc->idx = ctr_idx;
778
779 if (is_fevent) {
780 if (flags & SBI_PMU_CFG_FLAG_AUTO_START)
781 kvpmu->fw_event[event_code].started = true;
782 } else {
783 ret = kvm_pmu_create_perf_event(pmc, &attr, flags, eidx, evtdata);
784 if (ret) {
785 sbiret = SBI_ERR_NOT_SUPPORTED;
786 goto out;
787 }
788 }
789
790 set_bit(ctr_idx, kvpmu->pmc_in_use);
791 pmc->event_idx = eidx;
792 retdata->out_val = ctr_idx;
793 out:
794 retdata->err_val = sbiret;
795
796 return 0;
797 }
798
799 int kvm_riscv_vcpu_pmu_fw_ctr_read_hi(struct kvm_vcpu *vcpu, unsigned long cidx,
800 struct kvm_vcpu_sbi_return *retdata)
801 {
802 int ret;
803
804 ret = pmu_fw_ctr_read_hi(vcpu, cidx, &retdata->out_val);
805 if (ret == -EINVAL)
806 retdata->err_val = SBI_ERR_INVALID_PARAM;
807
808 return 0;
809 }
810
811 int kvm_riscv_vcpu_pmu_fw_ctr_read(struct kvm_vcpu *vcpu, unsigned long cidx,
812 struct kvm_vcpu_sbi_return *retdata)
813 {
814 int ret;
815
816 ret = pmu_ctr_read(vcpu, cidx, &retdata->out_val);
817 if (ret == -EINVAL)
818 retdata->err_val = SBI_ERR_INVALID_PARAM;
819
820 return 0;
821 }
822
823 void kvm_riscv_vcpu_pmu_init(struct kvm_vcpu *vcpu)
824 {
825 int i = 0, ret, num_hw_ctrs = 0, hpm_width = 0;
826 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
827 struct kvm_pmc *pmc;
828
829 /*
830 * PMU functionality should be only available to guests if privilege mode
831 * filtering is available in the host. Otherwise, guest will always count
832 * events while the execution is in hypervisor mode.
833 */
834 if (!riscv_isa_extension_available(NULL, SSCOFPMF))
835 return;
836
837 ret = riscv_pmu_get_hpm_info(&hpm_width, &num_hw_ctrs);
838 if (ret < 0 || !hpm_width || !num_hw_ctrs)
839 return;
840
841 /*
842 * Increase the number of hardware counters to offset the time counter.
843 */
844 kvpmu->num_hw_ctrs = num_hw_ctrs + 1;
845 kvpmu->num_fw_ctrs = SBI_PMU_FW_MAX;
846 memset(&kvpmu->fw_event, 0, SBI_PMU_FW_MAX * sizeof(struct kvm_fw_event));
847 kvpmu->snapshot_addr = INVALID_GPA;
848
849 if (kvpmu->num_hw_ctrs > RISCV_KVM_MAX_HW_CTRS) {
850 pr_warn_once("Limiting the hardware counters to 32 as specified by the ISA");
851 kvpmu->num_hw_ctrs = RISCV_KVM_MAX_HW_CTRS;
852 }
853
854 /*
855 * There is no correlation between the logical hardware counter and virtual counters.
856 * However, we need to encode a hpmcounter CSR in the counter info field so that
857 * KVM can trap n emulate the read. This works well in the migration use case as
858 * KVM doesn't care if the actual hpmcounter is available in the hardware or not.
859 */
860 for (i = 0; i < kvm_pmu_num_counters(kvpmu); i++) {
861 /* TIME CSR shouldn't be read from perf interface */
862 if (i == 1)
863 continue;
864 pmc = &kvpmu->pmc[i];
865 pmc->idx = i;
866 pmc->event_idx = SBI_PMU_EVENT_IDX_INVALID;
867 pmc->vcpu = vcpu;
868 if (i < kvpmu->num_hw_ctrs) {
869 pmc->cinfo.type = SBI_PMU_CTR_TYPE_HW;
870 if (i < 3)
871 /* CY, IR counters */
872 pmc->cinfo.width = 63;
873 else
874 pmc->cinfo.width = hpm_width;
875 /*
876 * The CSR number doesn't have any relation with the logical
877 * hardware counters. The CSR numbers are encoded sequentially
878 * to avoid maintaining a map between the virtual counter
879 * and CSR number.
880 */
881 pmc->cinfo.csr = CSR_CYCLE + i;
882 } else {
883 pmc->cinfo.type = SBI_PMU_CTR_TYPE_FW;
884 pmc->cinfo.width = 63;
885 }
886 }
887
888 kvpmu->init_done = true;
889 }
890
891 void kvm_riscv_vcpu_pmu_deinit(struct kvm_vcpu *vcpu)
892 {
893 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
894 struct kvm_pmc *pmc;
895 int i;
896
897 if (!kvpmu)
898 return;
899
900 for_each_set_bit(i, kvpmu->pmc_in_use, RISCV_KVM_MAX_COUNTERS) {
901 pmc = &kvpmu->pmc[i];
902 pmc->counter_val = 0;
903 kvm_pmu_release_perf_event(pmc);
904 pmc->event_idx = SBI_PMU_EVENT_IDX_INVALID;
905 }
906 bitmap_zero(kvpmu->pmc_in_use, RISCV_KVM_MAX_COUNTERS);
907 bitmap_zero(kvpmu->pmc_overflown, RISCV_KVM_MAX_COUNTERS);
908 memset(&kvpmu->fw_event, 0, SBI_PMU_FW_MAX * sizeof(struct kvm_fw_event));
909 kvm_pmu_clear_snapshot_area(vcpu);
910 }
911
912 void kvm_riscv_vcpu_pmu_reset(struct kvm_vcpu *vcpu)
913 {
914 kvm_riscv_vcpu_pmu_deinit(vcpu);
915 }