1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2023 Rivos Inc
6 * Atish Patra <atishp@rivosinc.com>
9 #include <linux/errno.h>
10 #include <linux/err.h>
11 #include <linux/kvm_host.h>
14 #include <asm/kvm_vcpu_sbi.h>
16 static int kvm_sbi_ext_pmu_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
17 struct kvm_vcpu_sbi_return *retdata)
20 struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
21 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
22 unsigned long funcid = cp->a6;
25 if (!kvpmu->init_done) {
26 retdata->err_val = SBI_ERR_NOT_SUPPORTED;
31 case SBI_EXT_PMU_NUM_COUNTERS:
32 ret = kvm_riscv_vcpu_pmu_num_ctrs(vcpu, retdata);
34 case SBI_EXT_PMU_COUNTER_GET_INFO:
35 ret = kvm_riscv_vcpu_pmu_ctr_info(vcpu, cp->a0, retdata);
37 case SBI_EXT_PMU_COUNTER_CFG_MATCH:
38 #if defined(CONFIG_32BIT)
39 temp = ((uint64_t)cp->a5 << 32) | cp->a4;
44 * This can fail if perf core framework fails to create an event.
45 * Forward the error to userspace because it's an error which
46 * happened within the host kernel. The other option would be
47 * to convert to an SBI error and forward to the guest.
49 ret = kvm_riscv_vcpu_pmu_ctr_cfg_match(vcpu, cp->a0, cp->a1,
50 cp->a2, cp->a3, temp, retdata);
52 case SBI_EXT_PMU_COUNTER_START:
53 #if defined(CONFIG_32BIT)
54 temp = ((uint64_t)cp->a4 << 32) | cp->a3;
58 ret = kvm_riscv_vcpu_pmu_ctr_start(vcpu, cp->a0, cp->a1, cp->a2,
61 case SBI_EXT_PMU_COUNTER_STOP:
62 ret = kvm_riscv_vcpu_pmu_ctr_stop(vcpu, cp->a0, cp->a1, cp->a2, retdata);
64 case SBI_EXT_PMU_COUNTER_FW_READ:
65 ret = kvm_riscv_vcpu_pmu_ctr_read(vcpu, cp->a0, retdata);
68 retdata->err_val = SBI_ERR_NOT_SUPPORTED;
74 static unsigned long kvm_sbi_ext_pmu_probe(struct kvm_vcpu *vcpu)
76 struct kvm_pmu *kvpmu = vcpu_to_pmu(vcpu);
78 return kvpmu->init_done;
81 const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_pmu = {
82 .extid_start = SBI_EXT_PMU,
83 .extid_end = SBI_EXT_PMU,
84 .handler = kvm_sbi_ext_pmu_handler,
85 .probe = kvm_sbi_ext_pmu_probe,