vcpu_sbi_base.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2021 Western Digital Corporation or its affiliates.
  4. *
  5. * Authors:
  6. * Atish Patra <[email protected]>
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/err.h>
  10. #include <linux/kvm_host.h>
  11. #include <linux/version.h>
  12. #include <asm/csr.h>
  13. #include <asm/sbi.h>
  14. #include <asm/kvm_vcpu_timer.h>
  15. #include <asm/kvm_vcpu_sbi.h>
  16. static int kvm_sbi_ext_base_handler(struct kvm_vcpu *vcpu, struct kvm_run *run,
  17. unsigned long *out_val,
  18. struct kvm_cpu_trap *trap, bool *exit)
  19. {
  20. int ret = 0;
  21. struct kvm_cpu_context *cp = &vcpu->arch.guest_context;
  22. struct sbiret ecall_ret;
  23. switch (cp->a6) {
  24. case SBI_EXT_BASE_GET_SPEC_VERSION:
  25. *out_val = (KVM_SBI_VERSION_MAJOR <<
  26. SBI_SPEC_VERSION_MAJOR_SHIFT) |
  27. KVM_SBI_VERSION_MINOR;
  28. break;
  29. case SBI_EXT_BASE_GET_IMP_ID:
  30. *out_val = KVM_SBI_IMPID;
  31. break;
  32. case SBI_EXT_BASE_GET_IMP_VERSION:
  33. *out_val = LINUX_VERSION_CODE;
  34. break;
  35. case SBI_EXT_BASE_PROBE_EXT:
  36. if ((cp->a0 >= SBI_EXT_EXPERIMENTAL_START &&
  37. cp->a0 <= SBI_EXT_EXPERIMENTAL_END) ||
  38. (cp->a0 >= SBI_EXT_VENDOR_START &&
  39. cp->a0 <= SBI_EXT_VENDOR_END)) {
  40. /*
  41. * For experimental/vendor extensions
  42. * forward it to the userspace
  43. */
  44. kvm_riscv_vcpu_sbi_forward(vcpu, run);
  45. *exit = true;
  46. } else
  47. *out_val = kvm_vcpu_sbi_find_ext(cp->a0) ? 1 : 0;
  48. break;
  49. case SBI_EXT_BASE_GET_MVENDORID:
  50. case SBI_EXT_BASE_GET_MARCHID:
  51. case SBI_EXT_BASE_GET_MIMPID:
  52. ecall_ret = sbi_ecall(SBI_EXT_BASE, cp->a6, 0, 0, 0, 0, 0, 0);
  53. if (!ecall_ret.error)
  54. *out_val = ecall_ret.value;
  55. /*TODO: We are unnecessarily converting the error twice */
  56. ret = sbi_err_map_linux_errno(ecall_ret.error);
  57. break;
  58. default:
  59. ret = -EOPNOTSUPP;
  60. break;
  61. }
  62. return ret;
  63. }
  64. const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_base = {
  65. .extid_start = SBI_EXT_BASE,
  66. .extid_end = SBI_EXT_BASE,
  67. .handler = kvm_sbi_ext_base_handler,
  68. };
  69. static int kvm_sbi_ext_forward_handler(struct kvm_vcpu *vcpu,
  70. struct kvm_run *run,
  71. unsigned long *out_val,
  72. struct kvm_cpu_trap *utrap,
  73. bool *exit)
  74. {
  75. /*
  76. * Both SBI experimental and vendor extensions are
  77. * unconditionally forwarded to userspace.
  78. */
  79. kvm_riscv_vcpu_sbi_forward(vcpu, run);
  80. *exit = true;
  81. return 0;
  82. }
  83. const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_experimental = {
  84. .extid_start = SBI_EXT_EXPERIMENTAL_START,
  85. .extid_end = SBI_EXT_EXPERIMENTAL_END,
  86. .handler = kvm_sbi_ext_forward_handler,
  87. };
  88. const struct kvm_vcpu_sbi_extension vcpu_sbi_ext_vendor = {
  89. .extid_start = SBI_EXT_VENDOR_START,
  90. .extid_end = SBI_EXT_VENDOR_END,
  91. .handler = kvm_sbi_ext_forward_handler,
  92. };