target/riscv: create finalize_features() for KVM

To turn cbom_blocksize and cboz_blocksize into class properties we need
KVM specific changes.

KVM is creating its own version of these options with a customized
setter() that prevents users from picking an invalid value during init()
time. This comes at the cost of duplicating each option that KVM
supports. This will keep happening for each new shared option KVM
implements in the future.

We can avoid that by using the same property TCG uses and adding
specific KVM handling during finalize() time, like TCG already does with
riscv_tcg_cpu_finalize_features(). To do that, the common CPU property
offers a way of knowing if an option was user set or not, sparing us
from doing unneeded syscalls.

riscv_kvm_cpu_finalize_features() is then created using the same
KVMScratch CPU we already use during init() time, since finalize() time
is still too early to use the official KVM CPU for it. cbom_blocksize
and cboz_blocksize are then handled during finalize() in the same way
they're handled by their KVM specific setter.

With this change we can proceed with the blocksize changes in the common
code without breaking the KVM driver.

Signed-off-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
tested-by tags added, rebased with Alistair's riscv-to-apply.next.
Message-ID: <20240112140201.127083-2-dbarboza@ventanamicro.com>
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
This commit is contained in:
Daniel Henrique Barboza 2024-01-12 11:01:54 -03:00 committed by Alistair Francis
parent 9d1173d20d
commit bbef914044
4 changed files with 72 additions and 5 deletions

View file

@ -1598,6 +1598,65 @@ static bool kvm_cpu_realize(CPUState *cs, Error **errp)
return true;
}
void riscv_kvm_cpu_finalize_features(RISCVCPU *cpu, Error **errp)
{
CPURISCVState *env = &cpu->env;
KVMScratchCPU kvmcpu;
struct kvm_one_reg reg;
uint64_t val;
int ret;
/* short-circuit without spinning the scratch CPU */
if (!cpu->cfg.ext_zicbom && !cpu->cfg.ext_zicboz) {
return;
}
if (!kvm_riscv_create_scratch_vcpu(&kvmcpu)) {
error_setg(errp, "Unable to create scratch KVM cpu");
return;
}
if (cpu->cfg.ext_zicbom &&
riscv_cpu_option_set(kvm_cbom_blocksize.name)) {
reg.id = kvm_riscv_reg_id_ulong(env, KVM_REG_RISCV_CONFIG,
kvm_cbom_blocksize.kvm_reg_id);
reg.addr = (uint64_t)&val;
ret = ioctl(kvmcpu.cpufd, KVM_GET_ONE_REG, &reg);
if (ret != 0) {
error_setg(errp, "Unable to read cbom_blocksize, error %d", errno);
return;
}
if (cpu->cfg.cbom_blocksize != val) {
error_setg(errp, "Unable to set cbom_blocksize to a different "
"value than the host (%lu)", val);
return;
}
}
if (cpu->cfg.ext_zicboz &&
riscv_cpu_option_set(kvm_cboz_blocksize.name)) {
reg.id = kvm_riscv_reg_id_ulong(env, KVM_REG_RISCV_CONFIG,
kvm_cboz_blocksize.kvm_reg_id);
reg.addr = (uint64_t)&val;
ret = ioctl(kvmcpu.cpufd, KVM_GET_ONE_REG, &reg);
if (ret != 0) {
error_setg(errp, "Unable to read cboz_blocksize, error %d", errno);
return;
}
if (cpu->cfg.cboz_blocksize != val) {
error_setg(errp, "Unable to set cboz_blocksize to a different "
"value than the host (%lu)", val);
return;
}
}
kvm_riscv_destroy_scratch_vcpu(&kvmcpu);
}
static void kvm_cpu_accel_class_init(ObjectClass *oc, void *data)
{
AccelCPUClass *acc = ACCEL_CPU_CLASS(oc);