acrn_drv.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ACRN_HSM_DRV_H
  3. #define __ACRN_HSM_DRV_H
  4. #include <linux/acrn.h>
  5. #include <linux/dev_printk.h>
  6. #include <linux/miscdevice.h>
  7. #include <linux/types.h>
  8. #include "hypercall.h"
  9. extern struct miscdevice acrn_dev;
  10. #define ACRN_NAME_LEN 16
  11. #define ACRN_MEM_MAPPING_MAX 256
  12. #define ACRN_MEM_REGION_ADD 0
  13. #define ACRN_MEM_REGION_DEL 2
  14. struct acrn_vm;
  15. struct acrn_ioreq_client;
  16. /**
  17. * struct vm_memory_region_op - Hypervisor memory operation
  18. * @type: Operation type (ACRN_MEM_REGION_*)
  19. * @attr: Memory attribute (ACRN_MEM_TYPE_* | ACRN_MEM_ACCESS_*)
  20. * @user_vm_pa: Physical address of User VM to be mapped.
  21. * @service_vm_pa: Physical address of Service VM to be mapped.
  22. * @size: Size of this region.
  23. *
  24. * Structure containing needed information that is provided to ACRN Hypervisor
  25. * to manage the EPT mappings of a single memory region of the User VM. Several
  26. * &struct vm_memory_region_op can be batched to ACRN Hypervisor, see &struct
  27. * vm_memory_region_batch.
  28. */
  29. struct vm_memory_region_op {
  30. u32 type;
  31. u32 attr;
  32. u64 user_vm_pa;
  33. u64 service_vm_pa;
  34. u64 size;
  35. };
  36. /**
  37. * struct vm_memory_region_batch - A batch of vm_memory_region_op.
  38. * @vmid: A User VM ID.
  39. * @reserved: Reserved.
  40. * @regions_num: The number of vm_memory_region_op.
  41. * @regions_gpa: Physical address of a vm_memory_region_op array.
  42. * @regions_op: Flexible array of vm_memory_region_op.
  43. *
  44. * HC_VM_SET_MEMORY_REGIONS uses this structure to manage EPT mappings of
  45. * multiple memory regions of a User VM. A &struct vm_memory_region_batch
  46. * contains multiple &struct vm_memory_region_op for batch processing in the
  47. * ACRN Hypervisor.
  48. */
  49. struct vm_memory_region_batch {
  50. u16 vmid;
  51. u16 reserved[3];
  52. u32 regions_num;
  53. u64 regions_gpa;
  54. struct vm_memory_region_op regions_op[] __counted_by(regions_num);
  55. };
  56. /**
  57. * struct vm_memory_mapping - Memory map between a User VM and the Service VM
  58. * @pages: Pages in Service VM kernel.
  59. * @npages: Number of pages.
  60. * @service_vm_va: Virtual address in Service VM kernel.
  61. * @user_vm_pa: Physical address in User VM.
  62. * @size: Size of this memory region.
  63. *
  64. * HSM maintains memory mappings between a User VM GPA and the Service VM
  65. * kernel VA for accelerating the User VM GPA translation.
  66. */
  67. struct vm_memory_mapping {
  68. struct page **pages;
  69. int npages;
  70. void *service_vm_va;
  71. u64 user_vm_pa;
  72. size_t size;
  73. };
  74. /**
  75. * struct acrn_ioreq_buffer - Data for setting the ioreq buffer of User VM
  76. * @ioreq_buf: The GPA of the IO request shared buffer of a VM
  77. *
  78. * The parameter for the HC_SET_IOREQ_BUFFER hypercall used to set up
  79. * the shared I/O request buffer between Service VM and ACRN hypervisor.
  80. */
  81. struct acrn_ioreq_buffer {
  82. u64 ioreq_buf;
  83. };
  84. struct acrn_ioreq_range {
  85. struct list_head list;
  86. u32 type;
  87. u64 start;
  88. u64 end;
  89. };
  90. #define ACRN_IOREQ_CLIENT_DESTROYING 0U
  91. typedef int (*ioreq_handler_t)(struct acrn_ioreq_client *client,
  92. struct acrn_io_request *req);
  93. /**
  94. * struct acrn_ioreq_client - Structure of I/O client.
  95. * @name: Client name
  96. * @vm: The VM that the client belongs to
  97. * @list: List node for this acrn_ioreq_client
  98. * @is_default: If this client is the default one
  99. * @flags: Flags (ACRN_IOREQ_CLIENT_*)
  100. * @range_list: I/O ranges
  101. * @range_lock: Lock to protect range_list
  102. * @ioreqs_map: The pending I/O requests bitmap.
  103. * @handler: I/O requests handler of this client
  104. * @thread: The thread which executes the handler
  105. * @wq: The wait queue for the handler thread parking
  106. * @priv: Data for the thread
  107. */
  108. struct acrn_ioreq_client {
  109. char name[ACRN_NAME_LEN];
  110. struct acrn_vm *vm;
  111. struct list_head list;
  112. bool is_default;
  113. unsigned long flags;
  114. struct list_head range_list;
  115. rwlock_t range_lock;
  116. DECLARE_BITMAP(ioreqs_map, ACRN_IO_REQUEST_MAX);
  117. ioreq_handler_t handler;
  118. struct task_struct *thread;
  119. wait_queue_head_t wq;
  120. void *priv;
  121. };
  122. #define ACRN_INVALID_VMID (0xffffU)
  123. #define ACRN_VM_FLAG_DESTROYED 0U
  124. #define ACRN_VM_FLAG_CLEARING_IOREQ 1U
  125. extern struct list_head acrn_vm_list;
  126. extern rwlock_t acrn_vm_list_lock;
  127. /**
  128. * struct acrn_vm - Properties of ACRN User VM.
  129. * @list: Entry within global list of all VMs.
  130. * @vmid: User VM ID.
  131. * @vcpu_num: Number of virtual CPUs in the VM.
  132. * @flags: Flags (ACRN_VM_FLAG_*) of the VM. This is VM
  133. * flag management in HSM which is different
  134. * from the &acrn_vm_creation.vm_flag.
  135. * @regions_mapping_lock: Lock to protect &acrn_vm.regions_mapping and
  136. * &acrn_vm.regions_mapping_count.
  137. * @regions_mapping: Memory mappings of this VM.
  138. * @regions_mapping_count: Number of memory mapping of this VM.
  139. * @ioreq_clients_lock: Lock to protect ioreq_clients and default_client
  140. * @ioreq_clients: The I/O request clients list of this VM
  141. * @default_client: The default I/O request client
  142. * @ioreq_buf: I/O request shared buffer
  143. * @ioreq_page: The page of the I/O request shared buffer
  144. * @pci_conf_addr: Address of a PCI configuration access emulation
  145. * @monitor_page: Page of interrupt statistics of User VM
  146. * @ioeventfds_lock: Lock to protect ioeventfds list
  147. * @ioeventfds: List to link all hsm_ioeventfd
  148. * @ioeventfd_client: I/O client for ioeventfds of the VM
  149. * @irqfds_lock: Lock to protect irqfds list
  150. * @irqfds: List to link all hsm_irqfd
  151. * @irqfd_wq: Workqueue for irqfd async shutdown
  152. */
  153. struct acrn_vm {
  154. struct list_head list;
  155. u16 vmid;
  156. int vcpu_num;
  157. unsigned long flags;
  158. struct mutex regions_mapping_lock;
  159. struct vm_memory_mapping regions_mapping[ACRN_MEM_MAPPING_MAX];
  160. int regions_mapping_count;
  161. spinlock_t ioreq_clients_lock;
  162. struct list_head ioreq_clients;
  163. struct acrn_ioreq_client *default_client;
  164. struct acrn_io_request_buffer *ioreq_buf;
  165. struct page *ioreq_page;
  166. u32 pci_conf_addr;
  167. struct page *monitor_page;
  168. struct mutex ioeventfds_lock;
  169. struct list_head ioeventfds;
  170. struct acrn_ioreq_client *ioeventfd_client;
  171. struct mutex irqfds_lock;
  172. struct list_head irqfds;
  173. struct workqueue_struct *irqfd_wq;
  174. };
  175. struct acrn_vm *acrn_vm_create(struct acrn_vm *vm,
  176. struct acrn_vm_creation *vm_param);
  177. int acrn_vm_destroy(struct acrn_vm *vm);
  178. int acrn_mm_region_add(struct acrn_vm *vm, u64 user_gpa, u64 service_gpa,
  179. u64 size, u32 mem_type, u32 mem_access_right);
  180. int acrn_mm_region_del(struct acrn_vm *vm, u64 user_gpa, u64 size);
  181. int acrn_vm_memseg_map(struct acrn_vm *vm, struct acrn_vm_memmap *memmap);
  182. int acrn_vm_memseg_unmap(struct acrn_vm *vm, struct acrn_vm_memmap *memmap);
  183. int acrn_vm_ram_map(struct acrn_vm *vm, struct acrn_vm_memmap *memmap);
  184. void acrn_vm_all_ram_unmap(struct acrn_vm *vm);
  185. int acrn_ioreq_init(struct acrn_vm *vm, u64 buf_vma);
  186. void acrn_ioreq_deinit(struct acrn_vm *vm);
  187. int acrn_ioreq_intr_setup(void);
  188. void acrn_ioreq_intr_remove(void);
  189. void acrn_ioreq_request_clear(struct acrn_vm *vm);
  190. int acrn_ioreq_client_wait(struct acrn_ioreq_client *client);
  191. int acrn_ioreq_request_default_complete(struct acrn_vm *vm, u16 vcpu);
  192. struct acrn_ioreq_client *acrn_ioreq_client_create(struct acrn_vm *vm,
  193. ioreq_handler_t handler,
  194. void *data, bool is_default,
  195. const char *name);
  196. void acrn_ioreq_client_destroy(struct acrn_ioreq_client *client);
  197. int acrn_ioreq_range_add(struct acrn_ioreq_client *client,
  198. u32 type, u64 start, u64 end);
  199. void acrn_ioreq_range_del(struct acrn_ioreq_client *client,
  200. u32 type, u64 start, u64 end);
  201. int acrn_msi_inject(struct acrn_vm *vm, u64 msi_addr, u64 msi_data);
  202. int acrn_ioeventfd_init(struct acrn_vm *vm);
  203. int acrn_ioeventfd_config(struct acrn_vm *vm, struct acrn_ioeventfd *args);
  204. void acrn_ioeventfd_deinit(struct acrn_vm *vm);
  205. int acrn_irqfd_init(struct acrn_vm *vm);
  206. int acrn_irqfd_config(struct acrn_vm *vm, struct acrn_irqfd *args);
  207. void acrn_irqfd_deinit(struct acrn_vm *vm);
  208. #endif /* __ACRN_HSM_DRV_H */