2021-10-26 17:01:35 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Western Digital Corporation or its affiliates.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Atish Patra <atish.patra@wdc.com>
|
|
|
|
* Anup Patel <anup.patel@wdc.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/kvm_host.h>
|
|
|
|
#include <linux/uaccess.h>
|
2022-04-09 03:46:00 +00:00
|
|
|
#include <asm/hwcap.h>
|
2021-10-26 17:01:35 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_FPU
|
|
|
|
void kvm_riscv_vcpu_fp_reset(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
struct kvm_cpu_context *cntx = &vcpu->arch.guest_context;
|
|
|
|
|
|
|
|
cntx->sstatus &= ~SR_FS;
|
2022-07-29 11:44:11 +00:00
|
|
|
if (riscv_isa_extension_available(vcpu->arch.isa, f) ||
|
|
|
|
riscv_isa_extension_available(vcpu->arch.isa, d))
|
2021-10-26 17:01:35 +00:00
|
|
|
cntx->sstatus |= SR_FS_INITIAL;
|
|
|
|
else
|
|
|
|
cntx->sstatus |= SR_FS_OFF;
|
|
|
|
}
|
|
|
|
|
2021-11-28 16:07:39 +00:00
|
|
|
static void kvm_riscv_vcpu_fp_clean(struct kvm_cpu_context *cntx)
|
2021-10-26 17:01:35 +00:00
|
|
|
{
|
|
|
|
cntx->sstatus &= ~SR_FS;
|
|
|
|
cntx->sstatus |= SR_FS_CLEAN;
|
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_riscv_vcpu_guest_fp_save(struct kvm_cpu_context *cntx,
|
2022-07-29 11:44:11 +00:00
|
|
|
const unsigned long *isa)
|
2021-10-26 17:01:35 +00:00
|
|
|
{
|
|
|
|
if ((cntx->sstatus & SR_FS) == SR_FS_DIRTY) {
|
2022-07-29 11:44:11 +00:00
|
|
|
if (riscv_isa_extension_available(isa, d))
|
2021-10-26 17:01:35 +00:00
|
|
|
__kvm_riscv_fp_d_save(cntx);
|
2022-07-29 11:44:11 +00:00
|
|
|
else if (riscv_isa_extension_available(isa, f))
|
2021-10-26 17:01:35 +00:00
|
|
|
__kvm_riscv_fp_f_save(cntx);
|
|
|
|
kvm_riscv_vcpu_fp_clean(cntx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_riscv_vcpu_guest_fp_restore(struct kvm_cpu_context *cntx,
|
2022-07-29 11:44:11 +00:00
|
|
|
const unsigned long *isa)
|
2021-10-26 17:01:35 +00:00
|
|
|
{
|
|
|
|
if ((cntx->sstatus & SR_FS) != SR_FS_OFF) {
|
2022-07-29 11:44:11 +00:00
|
|
|
if (riscv_isa_extension_available(isa, d))
|
2021-10-26 17:01:35 +00:00
|
|
|
__kvm_riscv_fp_d_restore(cntx);
|
2022-07-29 11:44:11 +00:00
|
|
|
else if (riscv_isa_extension_available(isa, f))
|
2021-10-26 17:01:35 +00:00
|
|
|
__kvm_riscv_fp_f_restore(cntx);
|
|
|
|
kvm_riscv_vcpu_fp_clean(cntx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_riscv_vcpu_host_fp_save(struct kvm_cpu_context *cntx)
|
|
|
|
{
|
|
|
|
/* No need to check host sstatus as it can be modified outside */
|
|
|
|
if (riscv_isa_extension_available(NULL, d))
|
|
|
|
__kvm_riscv_fp_d_save(cntx);
|
|
|
|
else if (riscv_isa_extension_available(NULL, f))
|
|
|
|
__kvm_riscv_fp_f_save(cntx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_riscv_vcpu_host_fp_restore(struct kvm_cpu_context *cntx)
|
|
|
|
{
|
|
|
|
if (riscv_isa_extension_available(NULL, d))
|
|
|
|
__kvm_riscv_fp_d_restore(cntx);
|
|
|
|
else if (riscv_isa_extension_available(NULL, f))
|
|
|
|
__kvm_riscv_fp_f_restore(cntx);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int kvm_riscv_vcpu_get_reg_fp(struct kvm_vcpu *vcpu,
|
|
|
|
const struct kvm_one_reg *reg,
|
|
|
|
unsigned long rtype)
|
|
|
|
{
|
|
|
|
struct kvm_cpu_context *cntx = &vcpu->arch.guest_context;
|
|
|
|
unsigned long __user *uaddr =
|
|
|
|
(unsigned long __user *)(unsigned long)reg->addr;
|
|
|
|
unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK |
|
|
|
|
KVM_REG_SIZE_MASK |
|
|
|
|
rtype);
|
|
|
|
void *reg_val;
|
|
|
|
|
|
|
|
if ((rtype == KVM_REG_RISCV_FP_F) &&
|
2022-07-29 11:44:11 +00:00
|
|
|
riscv_isa_extension_available(vcpu->arch.isa, f)) {
|
2021-10-26 17:01:35 +00:00
|
|
|
if (KVM_REG_SIZE(reg->id) != sizeof(u32))
|
|
|
|
return -EINVAL;
|
|
|
|
if (reg_num == KVM_REG_RISCV_FP_F_REG(fcsr))
|
|
|
|
reg_val = &cntx->fp.f.fcsr;
|
|
|
|
else if ((KVM_REG_RISCV_FP_F_REG(f[0]) <= reg_num) &&
|
|
|
|
reg_num <= KVM_REG_RISCV_FP_F_REG(f[31]))
|
|
|
|
reg_val = &cntx->fp.f.f[reg_num];
|
|
|
|
else
|
|
|
|
return -EINVAL;
|
|
|
|
} else if ((rtype == KVM_REG_RISCV_FP_D) &&
|
2022-07-29 11:44:11 +00:00
|
|
|
riscv_isa_extension_available(vcpu->arch.isa, d)) {
|
2021-10-26 17:01:35 +00:00
|
|
|
if (reg_num == KVM_REG_RISCV_FP_D_REG(fcsr)) {
|
|
|
|
if (KVM_REG_SIZE(reg->id) != sizeof(u32))
|
|
|
|
return -EINVAL;
|
|
|
|
reg_val = &cntx->fp.d.fcsr;
|
|
|
|
} else if ((KVM_REG_RISCV_FP_D_REG(f[0]) <= reg_num) &&
|
|
|
|
reg_num <= KVM_REG_RISCV_FP_D_REG(f[31])) {
|
|
|
|
if (KVM_REG_SIZE(reg->id) != sizeof(u64))
|
|
|
|
return -EINVAL;
|
|
|
|
reg_val = &cntx->fp.d.f[reg_num];
|
|
|
|
} else
|
|
|
|
return -EINVAL;
|
|
|
|
} else
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (copy_to_user(uaddr, reg_val, KVM_REG_SIZE(reg->id)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int kvm_riscv_vcpu_set_reg_fp(struct kvm_vcpu *vcpu,
|
|
|
|
const struct kvm_one_reg *reg,
|
|
|
|
unsigned long rtype)
|
|
|
|
{
|
|
|
|
struct kvm_cpu_context *cntx = &vcpu->arch.guest_context;
|
|
|
|
unsigned long __user *uaddr =
|
|
|
|
(unsigned long __user *)(unsigned long)reg->addr;
|
|
|
|
unsigned long reg_num = reg->id & ~(KVM_REG_ARCH_MASK |
|
|
|
|
KVM_REG_SIZE_MASK |
|
|
|
|
rtype);
|
|
|
|
void *reg_val;
|
|
|
|
|
|
|
|
if ((rtype == KVM_REG_RISCV_FP_F) &&
|
2022-07-29 11:44:11 +00:00
|
|
|
riscv_isa_extension_available(vcpu->arch.isa, f)) {
|
2021-10-26 17:01:35 +00:00
|
|
|
if (KVM_REG_SIZE(reg->id) != sizeof(u32))
|
|
|
|
return -EINVAL;
|
|
|
|
if (reg_num == KVM_REG_RISCV_FP_F_REG(fcsr))
|
|
|
|
reg_val = &cntx->fp.f.fcsr;
|
|
|
|
else if ((KVM_REG_RISCV_FP_F_REG(f[0]) <= reg_num) &&
|
|
|
|
reg_num <= KVM_REG_RISCV_FP_F_REG(f[31]))
|
|
|
|
reg_val = &cntx->fp.f.f[reg_num];
|
|
|
|
else
|
|
|
|
return -EINVAL;
|
|
|
|
} else if ((rtype == KVM_REG_RISCV_FP_D) &&
|
2022-07-29 11:44:11 +00:00
|
|
|
riscv_isa_extension_available(vcpu->arch.isa, d)) {
|
2021-10-26 17:01:35 +00:00
|
|
|
if (reg_num == KVM_REG_RISCV_FP_D_REG(fcsr)) {
|
|
|
|
if (KVM_REG_SIZE(reg->id) != sizeof(u32))
|
|
|
|
return -EINVAL;
|
|
|
|
reg_val = &cntx->fp.d.fcsr;
|
|
|
|
} else if ((KVM_REG_RISCV_FP_D_REG(f[0]) <= reg_num) &&
|
|
|
|
reg_num <= KVM_REG_RISCV_FP_D_REG(f[31])) {
|
|
|
|
if (KVM_REG_SIZE(reg->id) != sizeof(u64))
|
|
|
|
return -EINVAL;
|
|
|
|
reg_val = &cntx->fp.d.f[reg_num];
|
|
|
|
} else
|
|
|
|
return -EINVAL;
|
|
|
|
} else
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (copy_from_user(reg_val, uaddr, KVM_REG_SIZE(reg->id)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|