gzvm_arch_common.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2023 MediaTek Inc.
  4. */
  5. #ifndef __GZVM_ARCH_COMMON_H__
  6. #define __GZVM_ARCH_COMMON_H__
  7. #include <linux/arm-smccc.h>
  8. enum {
  9. GZVM_FUNC_CREATE_VM = 0,
  10. GZVM_FUNC_DESTROY_VM = 1,
  11. GZVM_FUNC_CREATE_VCPU = 2,
  12. GZVM_FUNC_DESTROY_VCPU = 3,
  13. GZVM_FUNC_SET_MEMREGION = 4,
  14. GZVM_FUNC_RUN = 5,
  15. GZVM_FUNC_GET_ONE_REG = 8,
  16. GZVM_FUNC_SET_ONE_REG = 9,
  17. GZVM_FUNC_IRQ_LINE = 10,
  18. GZVM_FUNC_CREATE_DEVICE = 11,
  19. GZVM_FUNC_PROBE = 12,
  20. GZVM_FUNC_ENABLE_CAP = 13,
  21. GZVM_FUNC_INFORM_EXIT = 14,
  22. GZVM_FUNC_MEMREGION_PURPOSE = 15,
  23. GZVM_FUNC_SET_DTB_CONFIG = 16,
  24. GZVM_FUNC_MAP_GUEST = 17,
  25. GZVM_FUNC_MAP_GUEST_BLOCK = 18,
  26. NR_GZVM_FUNC,
  27. };
  28. #define SMC_ENTITY_MTK 59
  29. #define GZVM_FUNCID_START (0x1000)
  30. #define GZVM_HCALL_ID(func) \
  31. ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_32, \
  32. SMC_ENTITY_MTK, (GZVM_FUNCID_START + (func)))
  33. #define MT_HVC_GZVM_CREATE_VM GZVM_HCALL_ID(GZVM_FUNC_CREATE_VM)
  34. #define MT_HVC_GZVM_DESTROY_VM GZVM_HCALL_ID(GZVM_FUNC_DESTROY_VM)
  35. #define MT_HVC_GZVM_CREATE_VCPU GZVM_HCALL_ID(GZVM_FUNC_CREATE_VCPU)
  36. #define MT_HVC_GZVM_DESTROY_VCPU GZVM_HCALL_ID(GZVM_FUNC_DESTROY_VCPU)
  37. #define MT_HVC_GZVM_SET_MEMREGION GZVM_HCALL_ID(GZVM_FUNC_SET_MEMREGION)
  38. #define MT_HVC_GZVM_RUN GZVM_HCALL_ID(GZVM_FUNC_RUN)
  39. #define MT_HVC_GZVM_GET_ONE_REG GZVM_HCALL_ID(GZVM_FUNC_GET_ONE_REG)
  40. #define MT_HVC_GZVM_SET_ONE_REG GZVM_HCALL_ID(GZVM_FUNC_SET_ONE_REG)
  41. #define MT_HVC_GZVM_IRQ_LINE GZVM_HCALL_ID(GZVM_FUNC_IRQ_LINE)
  42. #define MT_HVC_GZVM_CREATE_DEVICE GZVM_HCALL_ID(GZVM_FUNC_CREATE_DEVICE)
  43. #define MT_HVC_GZVM_PROBE GZVM_HCALL_ID(GZVM_FUNC_PROBE)
  44. #define MT_HVC_GZVM_ENABLE_CAP GZVM_HCALL_ID(GZVM_FUNC_ENABLE_CAP)
  45. #define MT_HVC_GZVM_INFORM_EXIT GZVM_HCALL_ID(GZVM_FUNC_INFORM_EXIT)
  46. #define MT_HVC_GZVM_MEMREGION_PURPOSE GZVM_HCALL_ID(GZVM_FUNC_MEMREGION_PURPOSE)
  47. #define MT_HVC_GZVM_SET_DTB_CONFIG GZVM_HCALL_ID(GZVM_FUNC_SET_DTB_CONFIG)
  48. #define MT_HVC_GZVM_MAP_GUEST GZVM_HCALL_ID(GZVM_FUNC_MAP_GUEST)
  49. #define MT_HVC_GZVM_MAP_GUEST_BLOCK GZVM_HCALL_ID(GZVM_FUNC_MAP_GUEST_BLOCK)
  50. #define GIC_V3_NR_LRS 16
  51. /**
  52. * gzvm_hypcall_wrapper() - the wrapper for hvc calls
  53. * @a0-a7: arguments passed in registers 0 to 7
  54. * @res: result values from registers 0 to 3
  55. *
  56. * Return: The wrapper helps caller to convert geniezone errno to Linux errno.
  57. */
  58. static inline int gzvm_hypcall_wrapper(unsigned long a0, unsigned long a1,
  59. unsigned long a2, unsigned long a3,
  60. unsigned long a4, unsigned long a5,
  61. unsigned long a6, unsigned long a7,
  62. struct arm_smccc_res *res)
  63. {
  64. arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
  65. return gzvm_err_to_errno(res->a0);
  66. }
  67. static inline u16 get_vmid_from_tuple(unsigned int tuple)
  68. {
  69. return (u16)(tuple >> 16);
  70. }
  71. static inline u16 get_vcpuid_from_tuple(unsigned int tuple)
  72. {
  73. return (u16)(tuple & 0xffff);
  74. }
  75. /**
  76. * struct gzvm_vcpu_hwstate: Sync architecture state back to host for handling
  77. * @nr_lrs: The available LRs(list registers) in Soc.
  78. * @__pad: add an explicit '__u32 __pad;' in the middle to make it clear
  79. * what the actual layout is.
  80. * @lr: The array of LRs(list registers).
  81. *
  82. * - Keep the same layout of hypervisor data struct.
  83. * - Sync list registers back for acking virtual device interrupt status.
  84. */
  85. struct gzvm_vcpu_hwstate {
  86. __le32 nr_lrs;
  87. __le32 __pad;
  88. __le64 lr[GIC_V3_NR_LRS];
  89. };
  90. static inline unsigned int
  91. assemble_vm_vcpu_tuple(u16 vmid, u16 vcpuid)
  92. {
  93. return ((unsigned int)vmid << 16 | vcpuid);
  94. }
  95. static inline void
  96. disassemble_vm_vcpu_tuple(unsigned int tuple, u16 *vmid, u16 *vcpuid)
  97. {
  98. *vmid = get_vmid_from_tuple(tuple);
  99. *vcpuid = get_vcpuid_from_tuple(tuple);
  100. }
  101. #endif /* __GZVM_ARCH_COMMON_H__ */