2021-09-27 04:40:08 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 Western Digital Corporation or its affiliates.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anup Patel <anup.patel@wdc.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/cpumask.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/module.h>
|
2022-05-08 22:14:05 -07:00
|
|
|
#include <linux/smp.h>
|
2021-09-27 04:40:08 -07:00
|
|
|
#include <linux/kvm_host.h>
|
|
|
|
#include <asm/csr.h>
|
|
|
|
|
|
|
|
static unsigned long vmid_version = 1;
|
|
|
|
static unsigned long vmid_next;
|
2022-11-30 16:09:09 -07:00
|
|
|
static unsigned long vmid_bits __ro_after_init;
|
2021-09-27 04:40:08 -07:00
|
|
|
static DEFINE_SPINLOCK(vmid_lock);
|
|
|
|
|
2022-11-30 16:09:09 -07:00
|
|
|
void __init kvm_riscv_gstage_vmid_detect(void)
|
2021-09-27 04:40:08 -07:00
|
|
|
{
|
|
|
|
unsigned long old;
|
|
|
|
|
|
|
|
/* Figure-out number of VMID bits in HW */
|
|
|
|
old = csr_read(CSR_HGATP);
|
2023-01-08 23:25:55 -07:00
|
|
|
csr_write(CSR_HGATP, old | HGATP_VMID);
|
2021-09-27 04:40:08 -07:00
|
|
|
vmid_bits = csr_read(CSR_HGATP);
|
2023-01-08 23:25:55 -07:00
|
|
|
vmid_bits = (vmid_bits & HGATP_VMID) >> HGATP_VMID_SHIFT;
|
2021-09-27 04:40:08 -07:00
|
|
|
vmid_bits = fls_long(vmid_bits);
|
|
|
|
csr_write(CSR_HGATP, old);
|
|
|
|
|
|
|
|
/* We polluted local TLB so flush all guest TLB */
|
2022-05-08 22:13:52 -07:00
|
|
|
kvm_riscv_local_hfence_gvma_all();
|
2021-09-27 04:40:08 -07:00
|
|
|
|
|
|
|
/* We don't use VMID bits if they are not sufficient */
|
|
|
|
if ((1UL << vmid_bits) < num_possible_cpus())
|
|
|
|
vmid_bits = 0;
|
|
|
|
}
|
|
|
|
|
2022-05-08 22:13:30 -07:00
|
|
|
unsigned long kvm_riscv_gstage_vmid_bits(void)
|
2021-09-27 04:40:08 -07:00
|
|
|
{
|
|
|
|
return vmid_bits;
|
|
|
|
}
|
|
|
|
|
2022-05-08 22:13:30 -07:00
|
|
|
int kvm_riscv_gstage_vmid_init(struct kvm *kvm)
|
2021-09-27 04:40:08 -07:00
|
|
|
{
|
|
|
|
/* Mark the initial VMID and VMID version invalid */
|
|
|
|
kvm->arch.vmid.vmid_version = 0;
|
|
|
|
kvm->arch.vmid.vmid = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-08 22:13:30 -07:00
|
|
|
bool kvm_riscv_gstage_vmid_ver_changed(struct kvm_vmid *vmid)
|
2021-09-27 04:40:08 -07:00
|
|
|
{
|
|
|
|
if (!vmid_bits)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return unlikely(READ_ONCE(vmid->vmid_version) !=
|
|
|
|
READ_ONCE(vmid_version));
|
|
|
|
}
|
|
|
|
|
2022-05-08 22:14:05 -07:00
|
|
|
static void __local_hfence_gvma_all(void *info)
|
|
|
|
{
|
|
|
|
kvm_riscv_local_hfence_gvma_all();
|
|
|
|
}
|
|
|
|
|
2022-05-08 22:13:30 -07:00
|
|
|
void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu)
|
2021-09-27 04:40:08 -07:00
|
|
|
{
|
2021-11-16 09:04:02 -07:00
|
|
|
unsigned long i;
|
2021-09-27 04:40:08 -07:00
|
|
|
struct kvm_vcpu *v;
|
|
|
|
struct kvm_vmid *vmid = &vcpu->kvm->arch.vmid;
|
|
|
|
|
2022-05-08 22:13:30 -07:00
|
|
|
if (!kvm_riscv_gstage_vmid_ver_changed(vmid))
|
2021-09-27 04:40:08 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
spin_lock(&vmid_lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to re-check the vmid_version here to ensure that if
|
|
|
|
* another vcpu already allocated a valid vmid for this vm.
|
|
|
|
*/
|
2022-05-08 22:13:30 -07:00
|
|
|
if (!kvm_riscv_gstage_vmid_ver_changed(vmid)) {
|
2021-09-27 04:40:08 -07:00
|
|
|
spin_unlock(&vmid_lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First user of a new VMID version? */
|
|
|
|
if (unlikely(vmid_next == 0)) {
|
|
|
|
WRITE_ONCE(vmid_version, READ_ONCE(vmid_version) + 1);
|
|
|
|
vmid_next = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We ran out of VMIDs so we increment vmid_version and
|
|
|
|
* start assigning VMIDs from 1.
|
|
|
|
*
|
2022-06-08 20:48:15 -07:00
|
|
|
* This also means existing VMIDs assignment to all Guest
|
2021-09-27 04:40:08 -07:00
|
|
|
* instances is invalid and we have force VMID re-assignement
|
|
|
|
* for all Guest instances. The Guest instances that were not
|
|
|
|
* running will automatically pick-up new VMIDs because will
|
2022-05-08 22:13:30 -07:00
|
|
|
* call kvm_riscv_gstage_vmid_update() whenever they enter
|
2021-09-27 04:40:08 -07:00
|
|
|
* in-kernel run loop. For Guest instances that are already
|
|
|
|
* running, we force VM exits on all host CPUs using IPI and
|
|
|
|
* flush all Guest TLBs.
|
|
|
|
*/
|
2022-05-08 22:14:05 -07:00
|
|
|
on_each_cpu_mask(cpu_online_mask, __local_hfence_gvma_all,
|
|
|
|
NULL, 1);
|
2021-09-27 04:40:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
vmid->vmid = vmid_next;
|
|
|
|
vmid_next++;
|
|
|
|
vmid_next &= (1 << vmid_bits) - 1;
|
|
|
|
|
|
|
|
WRITE_ONCE(vmid->vmid_version, READ_ONCE(vmid_version));
|
|
|
|
|
|
|
|
spin_unlock(&vmid_lock);
|
|
|
|
|
2022-05-08 22:13:30 -07:00
|
|
|
/* Request G-stage page table update for all VCPUs */
|
2021-09-27 04:40:08 -07:00
|
|
|
kvm_for_each_vcpu(i, v, vcpu->kvm)
|
|
|
|
kvm_make_request(KVM_REQ_UPDATE_HGATP, v);
|
|
|
|
}
|