2021-11-18 01:39:12 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Western Digital Corporation or its affiliates.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Atish Patra <atish.patra@wdc.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/kvm_host.h>
|
|
|
|
#include <asm/sbi.h>
|
|
|
|
#include <asm/kvm_vcpu_sbi.h>
|
|
|
|
|
|
|
|
static int kvm_sbi_hsm_vcpu_start(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
struct kvm_cpu_context *reset_cntx;
|
|
|
|
struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
|
|
|
|
struct kvm_vcpu *target_vcpu;
|
|
|
|
unsigned long target_vcpuid = cp->a0;
|
2024-04-17 00:45:25 -07:00
|
|
|
int ret = 0;
|
2021-11-18 01:39:12 -07:00
|
|
|
|
|
|
|
target_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, target_vcpuid);
|
|
|
|
if (!target_vcpu)
|
2023-02-04 18:15:07 -07:00
|
|
|
return SBI_ERR_INVALID_PARAM;
|
2024-04-17 00:45:25 -07:00
|
|
|
|
|
|
|
spin_lock(&target_vcpu->arch.mp_state_lock);
|
|
|
|
|
|
|
|
if (!kvm_riscv_vcpu_stopped(target_vcpu)) {
|
|
|
|
ret = SBI_ERR_ALREADY_AVAILABLE;
|
|
|
|
goto out;
|
|
|
|
}
|
2021-11-18 01:39:12 -07:00
|
|
|
|
2024-04-17 00:45:26 -07:00
|
|
|
spin_lock(&target_vcpu->arch.reset_cntx_lock);
|
2021-11-18 01:39:12 -07:00
|
|
|
reset_cntx = &target_vcpu->arch.guest_reset_context;
|
|
|
|
/* start address */
|
|
|
|
reset_cntx->sepc = cp->a1;
|
|
|
|
/* target vcpu id to start */
|
|
|
|
reset_cntx->a0 = target_vcpuid;
|
|
|
|
/* private data passed from kernel */
|
|
|
|
reset_cntx->a1 = cp->a2;
|
2024-04-17 00:45:26 -07:00
|
|
|
spin_unlock(&target_vcpu->arch.reset_cntx_lock);
|
|
|
|
|
2021-11-18 01:39:12 -07:00
|
|
|
kvm_make_request(KVM_REQ_VCPU_RESET, target_vcpu);
|
|
|
|
|
2024-04-17 00:45:25 -07:00
|
|
|
__kvm_riscv_vcpu_power_on(target_vcpu);
|
2021-11-18 01:39:12 -07:00
|
|
|
|
2024-04-17 00:45:25 -07:00
|
|
|
out:
|
|
|
|
spin_unlock(&target_vcpu->arch.mp_state_lock);
|
|
|
|
|
|
|
|
return ret;
|
2021-11-18 01:39:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int kvm_sbi_hsm_vcpu_stop(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
2024-04-17 00:45:25 -07:00
|
|
|
int ret = 0;
|
2021-11-18 01:39:12 -07:00
|
|
|
|
2024-04-17 00:45:25 -07:00
|
|
|
spin_lock(&vcpu->arch.mp_state_lock);
|
2021-11-18 01:39:12 -07:00
|
|
|
|
2024-04-17 00:45:25 -07:00
|
|
|
if (kvm_riscv_vcpu_stopped(vcpu)) {
|
|
|
|
ret = SBI_ERR_FAILURE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
__kvm_riscv_vcpu_power_off(vcpu);
|
|
|
|
|
|
|
|
out:
|
|
|
|
spin_unlock(&vcpu->arch.mp_state_lock);
|
|
|
|
|
|
|
|
return ret;
|
2021-11-18 01:39:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int kvm_sbi_hsm_vcpu_get_status(struct kvm_vcpu *vcpu)
|
|
|
|
{
|
|
|
|
struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
|
|
|
|
unsigned long target_vcpuid = cp->a0;
|
|
|
|
struct kvm_vcpu *target_vcpu;
|
|
|
|
|
|
|
|
target_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, target_vcpuid);
|
|
|
|
if (!target_vcpu)
|
2023-02-04 18:15:07 -07:00
|
|
|
return SBI_ERR_INVALID_PARAM;
|
2024-04-17 00:45:25 -07:00
|
|
|
if (!kvm_riscv_vcpu_stopped(target_vcpu))
|
2021-02-14 22:13:39 -07:00
|
|
|
return SBI_HSM_STATE_STARTED;
|
2022-01-31 00:01:36 -07:00
|
|
|
else if (vcpu->stat.generic.blocking)
|
|
|
|
return SBI_HSM_STATE_SUSPENDED;
|
2021-11-18 01:39:12 -07:00
|
|
|
else
|
2021-02-14 22:13:39 -07:00
|
|
|
return SBI_HSM_STATE_STOPPED;
|
2021-11-18 01:39:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static int kvm_sbi_ext_hsm_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
|
2023-02-04 18:15:07 -07:00
|
|
|
struct kvm_vcpu_sbi_return *retdata)
|
2021-11-18 01:39:12 -07:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
|
|
|
|
unsigned long funcid = cp->a6;
|
|
|
|
|
|
|
|
switch (funcid) {
|
|
|
|
case SBI_EXT_HSM_HART_START:
|
|
|
|
ret = kvm_sbi_hsm_vcpu_start(vcpu);
|
|
|
|
break;
|
|
|
|
case SBI_EXT_HSM_HART_STOP:
|
|
|
|
ret = kvm_sbi_hsm_vcpu_stop(vcpu);
|
|
|
|
break;
|
|
|
|
case SBI_EXT_HSM_HART_STATUS:
|
|
|
|
ret = kvm_sbi_hsm_vcpu_get_status(vcpu);
|
|
|
|
if (ret >= 0) {
|
2023-02-04 18:15:07 -07:00
|
|
|
retdata->out_val = ret;
|
|
|
|
retdata->err_val = 0;
|
2021-11-18 01:39:12 -07:00
|
|
|
}
|
2023-02-04 18:15:07 -07:00
|
|
|
return 0;
|
2022-01-31 00:01:36 -07:00
|
|
|
case SBI_EXT_HSM_HART_SUSPEND:
|
|
|
|
switch (cp->a0) {
|
|
|
|
case SBI_HSM_SUSPEND_RET_DEFAULT:
|
|
|
|
kvm_riscv_vcpu_wfi(vcpu);
|
|
|
|
break;
|
|
|
|
case SBI_HSM_SUSPEND_NON_RET_DEFAULT:
|
2023-02-04 18:15:07 -07:00
|
|
|
ret = SBI_ERR_NOT_SUPPORTED;
|
2022-01-31 00:01:36 -07:00
|
|
|
break;
|
|
|
|
default:
|
2023-02-04 18:15:07 -07:00
|
|
|
ret = SBI_ERR_INVALID_PARAM;
|
2022-01-31 00:01:36 -07:00
|
|
|
}
|
|
|
|
break;
|
2021-11-18 01:39:12 -07:00
|
|
|
default:
|
2023-02-04 18:15:07 -07:00
|
|
|
ret = SBI_ERR_NOT_SUPPORTED;
|
2021-11-18 01:39:12 -07:00
|
|
|
}
|
|
|
|
|
2023-02-04 18:15:07 -07:00
|
|
|
retdata->err_val = ret;
|
|
|
|
|
|
|
|
return 0;
|
2021-11-18 01:39:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_hsm = {
|
|
|
|
.extid_start = SBI_EXT_HSM,
|
|
|
|
.extid_end = SBI_EXT_HSM,
|
|
|
|
.handler = kvm_sbi_ext_hsm_handler,
|
|
|
|
};
|