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