vmid.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2019 Western Digital Corporation or its affiliates.
  4. *
  5. * Authors:
  6. * Anup Patel <[email protected]>
  7. */
  8. #include <linux/bitops.h>
  9. #include <linux/cpumask.h>
  10. #include <linux/errno.h>
  11. #include <linux/err.h>
  12. #include <linux/module.h>
  13. #include <linux/smp.h>
  14. #include <linux/kvm_host.h>
  15. #include <asm/csr.h>
  16. static unsigned long vmid_version = 1;
  17. static unsigned long vmid_next;
  18. static unsigned long vmid_bits;
  19. static DEFINE_SPINLOCK(vmid_lock);
  20. void kvm_riscv_gstage_vmid_detect(void)
  21. {
  22. unsigned long old;
  23. /* Figure-out number of VMID bits in HW */
  24. old = csr_read(CSR_HGATP);
  25. csr_write(CSR_HGATP, old | HGATP_VMID_MASK);
  26. vmid_bits = csr_read(CSR_HGATP);
  27. vmid_bits = (vmid_bits & HGATP_VMID_MASK) >> HGATP_VMID_SHIFT;
  28. vmid_bits = fls_long(vmid_bits);
  29. csr_write(CSR_HGATP, old);
  30. /* We polluted local TLB so flush all guest TLB */
  31. kvm_riscv_local_hfence_gvma_all();
  32. /* We don't use VMID bits if they are not sufficient */
  33. if ((1UL << vmid_bits) < num_possible_cpus())
  34. vmid_bits = 0;
  35. }
  36. unsigned long kvm_riscv_gstage_vmid_bits(void)
  37. {
  38. return vmid_bits;
  39. }
  40. int kvm_riscv_gstage_vmid_init(struct kvm *kvm)
  41. {
  42. /* Mark the initial VMID and VMID version invalid */
  43. kvm->arch.vmid.vmid_version = 0;
  44. kvm->arch.vmid.vmid = 0;
  45. return 0;
  46. }
  47. bool kvm_riscv_gstage_vmid_ver_changed(struct kvm_vmid *vmid)
  48. {
  49. if (!vmid_bits)
  50. return false;
  51. return unlikely(READ_ONCE(vmid->vmid_version) !=
  52. READ_ONCE(vmid_version));
  53. }
  54. static void __local_hfence_gvma_all(void *info)
  55. {
  56. kvm_riscv_local_hfence_gvma_all();
  57. }
  58. void kvm_riscv_gstage_vmid_update(struct kvm_vcpu *vcpu)
  59. {
  60. unsigned long i;
  61. struct kvm_vcpu *v;
  62. struct kvm_vmid *vmid = &vcpu->kvm->arch.vmid;
  63. if (!kvm_riscv_gstage_vmid_ver_changed(vmid))
  64. return;
  65. spin_lock(&vmid_lock);
  66. /*
  67. * We need to re-check the vmid_version here to ensure that if
  68. * another vcpu already allocated a valid vmid for this vm.
  69. */
  70. if (!kvm_riscv_gstage_vmid_ver_changed(vmid)) {
  71. spin_unlock(&vmid_lock);
  72. return;
  73. }
  74. /* First user of a new VMID version? */
  75. if (unlikely(vmid_next == 0)) {
  76. WRITE_ONCE(vmid_version, READ_ONCE(vmid_version) + 1);
  77. vmid_next = 1;
  78. /*
  79. * We ran out of VMIDs so we increment vmid_version and
  80. * start assigning VMIDs from 1.
  81. *
  82. * This also means existing VMIDs assignment to all Guest
  83. * instances is invalid and we have force VMID re-assignement
  84. * for all Guest instances. The Guest instances that were not
  85. * running will automatically pick-up new VMIDs because will
  86. * call kvm_riscv_gstage_vmid_update() whenever they enter
  87. * in-kernel run loop. For Guest instances that are already
  88. * running, we force VM exits on all host CPUs using IPI and
  89. * flush all Guest TLBs.
  90. */
  91. on_each_cpu_mask(cpu_online_mask, __local_hfence_gvma_all,
  92. NULL, 1);
  93. }
  94. vmid->vmid = vmid_next;
  95. vmid_next++;
  96. vmid_next &= (1 << vmid_bits) - 1;
  97. WRITE_ONCE(vmid->vmid_version, READ_ONCE(vmid_version));
  98. spin_unlock(&vmid_lock);
  99. /* Request G-stage page table update for all VCPUs */
  100. kvm_for_each_vcpu(i, v, vcpu->kvm)
  101. kvm_make_request(KVM_REQ_UPDATE_HGATP, v);
  102. }