]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
RISC-V: KVM: Write hgatp register with valid mode bits
authorFangyu Yu <fangyu.yu@linux.alibaba.com>
Thu, 21 Aug 2025 14:25:40 +0000 (10:25 -0400)
committerAnup Patel <anup@brainfault.org>
Tue, 16 Sep 2025 05:24:01 +0000 (10:54 +0530)
According to the RISC-V Privileged Architecture Spec, when MODE=Bare
is selected,software must write zero to the remaining fields of hgatp.

We have detected the valid mode supported by the HW before, So using a
valid mode to detect how many vmid bits are supported.

Fixes: fd7bb4a251df ("RISC-V: KVM: Implement VMID allocator")
Reviewed-by: Nutty Liu <nutty.liu@hotmail.com>
Reviewed-by: Troy Mitchell <troy.mitchell@linux.spacemit.com>
Reviewed-by: Guo Ren (Alibaba DAMO Academy) <guoren@kernel.org>
Signed-off-by: Fangyu Yu <fangyu.yu@linux.alibaba.com>
Signed-off-by: Guo Ren (Alibaba DAMO Academy) <guoren@kernel.org>
Link: https://lore.kernel.org/r/20250821142542.2472079-2-guoren@kernel.org
Signed-off-by: Anup Patel <anup@brainfault.org>
arch/riscv/kvm/vmid.c

index 3b426c800480c8b17241d83bbf41b12002ff9bb4..5f33625f40706535f50fb3e814932845f937e748 100644 (file)
@@ -14,6 +14,7 @@
 #include <linux/smp.h>
 #include <linux/kvm_host.h>
 #include <asm/csr.h>
+#include <asm/kvm_mmu.h>
 #include <asm/kvm_tlb.h>
 #include <asm/kvm_vmid.h>
 
@@ -28,7 +29,7 @@ void __init kvm_riscv_gstage_vmid_detect(void)
 
        /* Figure-out number of VMID bits in HW */
        old = csr_read(CSR_HGATP);
-       csr_write(CSR_HGATP, old | HGATP_VMID);
+       csr_write(CSR_HGATP, (kvm_riscv_gstage_mode << HGATP_MODE_SHIFT) | HGATP_VMID);
        vmid_bits = csr_read(CSR_HGATP);
        vmid_bits = (vmid_bits & HGATP_VMID) >> HGATP_VMID_SHIFT;
        vmid_bits = fls_long(vmid_bits);