kvm_vcpu.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2020-2023 Loongson Technology Corporation Limited
  4. */
  5. #ifndef __ASM_LOONGARCH_KVM_VCPU_H__
  6. #define __ASM_LOONGARCH_KVM_VCPU_H__
  7. #include <linux/kvm_host.h>
  8. #include <asm/loongarch.h>
  9. /* Controlled by 0x5 guest estat */
  10. #define CPU_SIP0 (_ULCAST_(1))
  11. #define CPU_SIP1 (_ULCAST_(1) << 1)
  12. #define CPU_PMU (_ULCAST_(1) << 10)
  13. #define CPU_TIMER (_ULCAST_(1) << 11)
  14. #define CPU_IPI (_ULCAST_(1) << 12)
  15. #define CPU_AVEC (_ULCAST_(1) << 14)
  16. /* Controlled by 0x52 guest exception VIP aligned to estat bit 5~12 */
  17. #define CPU_IP0 (_ULCAST_(1))
  18. #define CPU_IP1 (_ULCAST_(1) << 1)
  19. #define CPU_IP2 (_ULCAST_(1) << 2)
  20. #define CPU_IP3 (_ULCAST_(1) << 3)
  21. #define CPU_IP4 (_ULCAST_(1) << 4)
  22. #define CPU_IP5 (_ULCAST_(1) << 5)
  23. #define CPU_IP6 (_ULCAST_(1) << 6)
  24. #define CPU_IP7 (_ULCAST_(1) << 7)
  25. #define MNSEC_PER_SEC (NSEC_PER_SEC >> 20)
  26. /* KVM_IRQ_LINE irq field index values */
  27. #define KVM_LOONGSON_IRQ_TYPE_SHIFT 24
  28. #define KVM_LOONGSON_IRQ_TYPE_MASK 0xff
  29. #define KVM_LOONGSON_IRQ_VCPU_SHIFT 16
  30. #define KVM_LOONGSON_IRQ_VCPU_MASK 0xff
  31. #define KVM_LOONGSON_IRQ_NUM_SHIFT 0
  32. #define KVM_LOONGSON_IRQ_NUM_MASK 0xffff
  33. typedef union loongarch_instruction larch_inst;
  34. typedef int (*exit_handle_fn)(struct kvm_vcpu *, int);
  35. int kvm_emu_mmio_read(struct kvm_vcpu *vcpu, larch_inst inst);
  36. int kvm_emu_mmio_write(struct kvm_vcpu *vcpu, larch_inst inst);
  37. int kvm_complete_mmio_read(struct kvm_vcpu *vcpu, struct kvm_run *run);
  38. int kvm_complete_iocsr_read(struct kvm_vcpu *vcpu, struct kvm_run *run);
  39. int kvm_complete_user_service(struct kvm_vcpu *vcpu, struct kvm_run *run);
  40. int kvm_emu_idle(struct kvm_vcpu *vcpu);
  41. int kvm_pending_timer(struct kvm_vcpu *vcpu);
  42. int kvm_handle_fault(struct kvm_vcpu *vcpu, int fault);
  43. void kvm_deliver_intr(struct kvm_vcpu *vcpu);
  44. void kvm_deliver_exception(struct kvm_vcpu *vcpu);
  45. void kvm_own_fpu(struct kvm_vcpu *vcpu);
  46. void kvm_lose_fpu(struct kvm_vcpu *vcpu);
  47. void kvm_save_fpu(struct loongarch_fpu *fpu);
  48. void kvm_restore_fpu(struct loongarch_fpu *fpu);
  49. void kvm_restore_fcsr(struct loongarch_fpu *fpu);
  50. #ifdef CONFIG_CPU_HAS_LSX
  51. int kvm_own_lsx(struct kvm_vcpu *vcpu);
  52. void kvm_save_lsx(struct loongarch_fpu *fpu);
  53. void kvm_restore_lsx(struct loongarch_fpu *fpu);
  54. #else
  55. static inline int kvm_own_lsx(struct kvm_vcpu *vcpu) { return -EINVAL; }
  56. static inline void kvm_save_lsx(struct loongarch_fpu *fpu) { }
  57. static inline void kvm_restore_lsx(struct loongarch_fpu *fpu) { }
  58. #endif
  59. #ifdef CONFIG_CPU_HAS_LASX
  60. int kvm_own_lasx(struct kvm_vcpu *vcpu);
  61. void kvm_save_lasx(struct loongarch_fpu *fpu);
  62. void kvm_restore_lasx(struct loongarch_fpu *fpu);
  63. #else
  64. static inline int kvm_own_lasx(struct kvm_vcpu *vcpu) { return -EINVAL; }
  65. static inline void kvm_save_lasx(struct loongarch_fpu *fpu) { }
  66. static inline void kvm_restore_lasx(struct loongarch_fpu *fpu) { }
  67. #endif
  68. #ifdef CONFIG_CPU_HAS_LBT
  69. int kvm_own_lbt(struct kvm_vcpu *vcpu);
  70. #else
  71. static inline int kvm_own_lbt(struct kvm_vcpu *vcpu) { return -EINVAL; }
  72. #endif
  73. void kvm_init_timer(struct kvm_vcpu *vcpu, unsigned long hz);
  74. void kvm_save_timer(struct kvm_vcpu *vcpu);
  75. void kvm_restore_timer(struct kvm_vcpu *vcpu);
  76. int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_interrupt *irq);
  77. struct kvm_vcpu *kvm_get_vcpu_by_cpuid(struct kvm *kvm, int cpuid);
  78. /*
  79. * Loongarch KVM guest interrupt handling
  80. */
  81. static inline void kvm_queue_irq(struct kvm_vcpu *vcpu, unsigned int irq)
  82. {
  83. set_bit(irq, &vcpu->arch.irq_pending);
  84. clear_bit(irq, &vcpu->arch.irq_clear);
  85. }
  86. static inline void kvm_dequeue_irq(struct kvm_vcpu *vcpu, unsigned int irq)
  87. {
  88. clear_bit(irq, &vcpu->arch.irq_pending);
  89. set_bit(irq, &vcpu->arch.irq_clear);
  90. }
  91. static inline int kvm_queue_exception(struct kvm_vcpu *vcpu,
  92. unsigned int code, unsigned int subcode)
  93. {
  94. /* only one exception can be injected */
  95. if (!vcpu->arch.exception_pending) {
  96. set_bit(code, &vcpu->arch.exception_pending);
  97. vcpu->arch.esubcode = subcode;
  98. return 0;
  99. } else
  100. return -1;
  101. }
  102. static inline unsigned long kvm_read_reg(struct kvm_vcpu *vcpu, int num)
  103. {
  104. return vcpu->arch.gprs[num];
  105. }
  106. static inline void kvm_write_reg(struct kvm_vcpu *vcpu, int num, unsigned long val)
  107. {
  108. vcpu->arch.gprs[num] = val;
  109. }
  110. static inline bool kvm_pvtime_supported(void)
  111. {
  112. return !!sched_info_on();
  113. }
  114. static inline bool kvm_guest_has_pv_feature(struct kvm_vcpu *vcpu, unsigned int feature)
  115. {
  116. return vcpu->kvm->arch.pv_features & BIT(feature);
  117. }
  118. #endif /* __ASM_LOONGARCH_KVM_VCPU_H__ */