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