• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  * Copyright (c) 2023 MediaTek Inc.
4  */
5 
6 /**
7  * DOC: UAPI of GenieZone Hypervisor
8  *
9  * This file declares common data structure shared among user space,
10  * kernel space, and GenieZone hypervisor.
11  */
12 #ifndef __GZVM_H__
13 #define __GZVM_H__
14 
15 #include <linux/const.h>
16 #include <linux/types.h>
17 #include <linux/ioctl.h>
18 
19 #define GZVM_CAP_VM_GPA_SIZE	0xa5
20 #define GZVM_CAP_PROTECTED_VM	0xffbadab1
21 /* query hypervisor supported block-based demand page */
22 #define GZVM_CAP_BLOCK_BASED_DEMAND_PAGING	0x9201
23 #define GZVM_CAP_ENABLE_DEMAND_PAGING	0x9202
24 #define GZVM_CAP_ENABLE_IDLE		0x9203
25 #define GZVM_CAP_QUERY_HYP_BATCH_PAGES	0x9204
26 #define GZVM_CAP_QUERY_DESTROY_BATCH_PAGES	0x9205
27 
28 /* sub-commands put in args[0] for GZVM_CAP_PROTECTED_VM */
29 #define GZVM_CAP_PVM_SET_PVMFW_GPA		0
30 #define GZVM_CAP_PVM_GET_PVMFW_SIZE		1
31 /* GZVM_CAP_PVM_SET_PROTECTED_VM only sets protected but not load pvmfw */
32 #define GZVM_CAP_PVM_SET_PROTECTED_VM		2
33 
34 /*
35  * Architecture specific registers are to be defined and ORed with
36  * the arch identifier.
37  */
38 #define GZVM_REG_ARCH_ARM64	FIELD_PREP(GENMASK_ULL(63, 56), 0x60)
39 #define GZVM_REG_ARCH_MASK	FIELD_PREP(GENMASK_ULL(63, 56), 0xff)
40 /*
41  * Reg size = BIT((reg.id & GZVM_REG_SIZE_MASK) >> GZVM_REG_SIZE_SHIFT) bytes
42  */
43 #define GZVM_REG_SIZE_SHIFT	52
44 #define GZVM_REG_SIZE_MASK	FIELD_PREP(GENMASK_ULL(63, 48), 0x00f0)
45 
46 #define GZVM_REG_SIZE_U8	FIELD_PREP(GENMASK_ULL(63, 48), 0x0000)
47 #define GZVM_REG_SIZE_U16	FIELD_PREP(GENMASK_ULL(63, 48), 0x0010)
48 #define GZVM_REG_SIZE_U32	FIELD_PREP(GENMASK_ULL(63, 48), 0x0020)
49 #define GZVM_REG_SIZE_U64	FIELD_PREP(GENMASK_ULL(63, 48), 0x0030)
50 #define GZVM_REG_SIZE_U128	FIELD_PREP(GENMASK_ULL(63, 48), 0x0040)
51 #define GZVM_REG_SIZE_U256	FIELD_PREP(GENMASK_ULL(63, 48), 0x0050)
52 #define GZVM_REG_SIZE_U512	FIELD_PREP(GENMASK_ULL(63, 48), 0x0060)
53 #define GZVM_REG_SIZE_U1024	FIELD_PREP(GENMASK_ULL(63, 48), 0x0070)
54 #define GZVM_REG_SIZE_U2048	FIELD_PREP(GENMASK_ULL(63, 48), 0x0080)
55 
56 #define GZVM_REG_TYPE_GENERAL2	FIELD_PREP(GENMASK(23, 16), 0x10)
57 
58 /* GZVM ioctls */
59 #define GZVM_IOC_MAGIC			0x92	/* gz */
60 
61 /* ioctls for /dev/gzvm fds */
62 #define GZVM_CREATE_VM             _IO(GZVM_IOC_MAGIC,   0x01) /* Returns a Geniezone VM fd */
63 
64 /*
65  * Check if the given capability is supported or not.
66  * The argument is capability. Ex. GZVM_CAP_PROTECTED_VM or GZVM_CAP_VM_GPA_SIZE
67  * return is 0 (supported, no error)
68  * return is -EOPNOTSUPP (unsupported)
69  * return is -EFAULT (failed to get the argument from userspace)
70  */
71 #define GZVM_CHECK_EXTENSION       _IO(GZVM_IOC_MAGIC,   0x03)
72 
73 /* ioctls for VM fds */
74 /* for GZVM_SET_MEMORY_REGION */
75 struct gzvm_memory_region {
76 	__u32 slot;
77 	__u32 flags;
78 	__u64 guest_phys_addr;
79 	__u64 memory_size; /* bytes */
80 };
81 
82 #define GZVM_SET_MEMORY_REGION     _IOW(GZVM_IOC_MAGIC,  0x40, \
83 					struct gzvm_memory_region)
84 /*
85  * GZVM_CREATE_VCPU receives as a parameter the vcpu slot,
86  * and returns a vcpu fd.
87  */
88 #define GZVM_CREATE_VCPU           _IO(GZVM_IOC_MAGIC,   0x41)
89 
90 /**
91  * struct gzvm_userspace_memory_region: gzvm userspace memory region descriptor
92  * @slot: memory slot
93  * @flags: describe the usage of userspace memory region
94  * @guest_phys_addr: guest vm's physical address
95  * @memory_size: memory size in bytes
96  * @userspace_addr: start of the userspace allocated memory
97  */
98 struct gzvm_userspace_memory_region {
99 	__u32 slot;
100 	__u32 flags;
101 	__u64 guest_phys_addr;
102 	__u64 memory_size;
103 	__u64 userspace_addr;
104 };
105 
106 #define GZVM_SET_USER_MEMORY_REGION _IOW(GZVM_IOC_MAGIC, 0x46, \
107 					 struct gzvm_userspace_memory_region)
108 
109 /* for GZVM_IRQ_LINE, irq field index values */
110 #define GZVM_IRQ_VCPU_MASK		0xff
111 #define GZVM_IRQ_LINE_TYPE		GENMASK(27, 24)
112 #define GZVM_IRQ_LINE_VCPU		GENMASK(23, 16)
113 #define GZVM_IRQ_LINE_VCPU2		GENMASK(31, 28)
114 #define GZVM_IRQ_LINE_NUM		GENMASK(15, 0)
115 
116 /* irq_type field */
117 #define GZVM_IRQ_TYPE_CPU		0
118 #define GZVM_IRQ_TYPE_SPI		1
119 #define GZVM_IRQ_TYPE_PPI		2
120 
121 /* out-of-kernel GIC cpu interrupt injection irq_number field */
122 #define GZVM_IRQ_CPU_IRQ		0
123 #define GZVM_IRQ_CPU_FIQ		1
124 
125 struct gzvm_irq_level {
126 	union {
127 		__u32 irq;
128 		__s32 status;
129 	};
130 	__u32 level;
131 };
132 
133 #define GZVM_IRQ_LINE              _IOW(GZVM_IOC_MAGIC,  0x61, \
134 					struct gzvm_irq_level)
135 
136 enum gzvm_device_type {
137 	GZVM_DEV_TYPE_ARM_VGIC_V3_DIST = 0,
138 	GZVM_DEV_TYPE_ARM_VGIC_V3_REDIST = 1,
139 	GZVM_DEV_TYPE_MAX,
140 };
141 
142 /**
143  * struct gzvm_create_device: For GZVM_CREATE_DEVICE.
144  * @dev_type: Device type.
145  * @id: Device id.
146  * @flags: Bypass to hypervisor to handle them and these are flags of virtual
147  *         devices.
148  * @dev_addr: Device ipa address in VM's view.
149  * @dev_reg_size: Device register range size.
150  * @attr_addr: If user -> kernel, this is user virtual address of device
151  *             specific attributes (if needed). If kernel->hypervisor,
152  *             this is ipa.
153  * @attr_size: This attr_size is the buffer size in bytes of each attribute
154  *             needed from various devices. The attribute here refers to the
155  *             additional data passed from VMM(e.g. Crosvm) to GenieZone
156  *             hypervisor when virtual devices were to be created. Thus,
157  *             we need attr_addr and attr_size in the gzvm_create_device
158  *             structure to keep track of the attribute mentioned.
159  *
160  * Store information needed to create device.
161  */
162 struct gzvm_create_device {
163 	__u32 dev_type;
164 	__u32 id;
165 	__u64 flags;
166 	__u64 dev_addr;
167 	__u64 dev_reg_size;
168 	__u64 attr_addr;
169 	__u64 attr_size;
170 };
171 
172 #define GZVM_CREATE_DEVICE	   _IOWR(GZVM_IOC_MAGIC,  0xe0, \
173 					struct gzvm_create_device)
174 
175 /*
176  * ioctls for vcpu fds
177  */
178 #define GZVM_RUN                   _IO(GZVM_IOC_MAGIC,   0x80)
179 
180 /* VM exit reason */
181 enum {
182 	GZVM_EXIT_UNKNOWN = 0x92920000,
183 	GZVM_EXIT_MMIO = 0x92920001,
184 	GZVM_EXIT_HYPERCALL = 0x92920002,
185 	GZVM_EXIT_IRQ = 0x92920003,
186 	GZVM_EXIT_EXCEPTION = 0x92920004,
187 	GZVM_EXIT_DEBUG = 0x92920005,
188 	GZVM_EXIT_FAIL_ENTRY = 0x92920006,
189 	GZVM_EXIT_INTERNAL_ERROR = 0x92920007,
190 	GZVM_EXIT_SYSTEM_EVENT = 0x92920008,
191 	GZVM_EXIT_SHUTDOWN = 0x92920009,
192 	GZVM_EXIT_GZ = 0x9292000a,
193 	GZVM_EXIT_IDLE = 0x9292000b,
194 	GZVM_EXIT_IPI = 0x9292000d,
195 };
196 
197 /* exception definitions of GZVM_EXIT_EXCEPTION */
198 enum {
199 	GZVM_EXCEPTION_UNKNOWN = 0x0,
200 	GZVM_EXCEPTION_PAGE_FAULT = 0x1,
201 };
202 
203 /* hypercall definitions of GZVM_EXIT_HYPERCALL */
204 enum {
205 	GZVM_HVC_PTP = 0x86000001,
206 	GZVM_HVC_MEM_RELINQUISH = 0xc6000009,
207 };
208 
209 /**
210  * struct gzvm_vcpu_run: Same purpose as kvm_run, this struct is
211  *			 shared between userspace, kernel and
212  *			 GenieZone hypervisor
213  * @exit_reason: The reason why gzvm_vcpu_run has stopped running the vCPU
214  * @immediate_exit: Polled when the vcpu is scheduled.
215  *                  If set, immediately returns -EINTR
216  * @padding1: Reserved for future-proof and must be zero filled
217  * @mmio: The nested struct in anonymous union. Handle mmio in host side
218  * @fail_entry: The nested struct in anonymous union.
219  *              Handle invalid entry address at the first run
220  * @exception: The nested struct in anonymous union.
221  *             Handle exception occurred in VM
222  * @hypercall: The nested struct in anonymous union.
223  *             Some hypercalls issued from VM must be handled
224  * @internal: The nested struct in anonymous union. The errors from hypervisor
225  * @system_event: The nested struct in anonymous union.
226  *                VM's PSCI must be handled by host
227  * @padding: Fix it to a reasonable size future-proof for keeping the same
228  *           struct size when adding new variables in the union is needed
229  *
230  * Keep identical layout between the 3 modules
231  */
232 struct gzvm_vcpu_run {
233 	/* to userspace */
234 	__u32 exit_reason;
235 	__u8 immediate_exit;
236 	__u8 padding1[3];
237 	/* union structure of collection of guest exit reason */
238 	union {
239 		/* GZVM_EXIT_MMIO */
240 		struct {
241 			/* From FAR_EL2 */
242 			/* The address guest tries to access */
243 			__u64 phys_addr;
244 			/* The value to be written (is_write is 1) or
245 			 * be filled by user for reads (is_write is 0)
246 			 */
247 			__u8 data[8];
248 			/* From ESR_EL2 as */
249 			/* The size of written data.
250 			 * Only the first `size` bytes of `data` are handled
251 			 */
252 			__u64 size;
253 			/* From ESR_EL2 */
254 			/* The register number where the data is stored */
255 			__u32 reg_nr;
256 			/* From ESR_EL2 */
257 			/* 1 for VM to perform a write or 0 for VM to perform a read */
258 			__u8 is_write;
259 		} mmio;
260 		/* GZVM_EXIT_FAIL_ENTRY */
261 		struct {
262 			/* The reason codes about hardware entry failure */
263 			__u64 hardware_entry_failure_reason;
264 			/* The current processor number via smp_processor_id() */
265 			__u32 cpu;
266 		} fail_entry;
267 		/* GZVM_EXIT_EXCEPTION */
268 		struct {
269 			/* Which exception vector */
270 			__u32 exception;
271 			/* Exception error codes */
272 			__u32 error_code;
273 			/* Fault GPA (guest physical address or IPA in ARM) */
274 			__u64 fault_gpa;
275 			/* Future-proof reservation and reset to zero in hypervisor.
276 			 * Fill up to the union size, 256 bytes.
277 			 */
278 			__u64 reserved[30];
279 		} exception;
280 		/* GZVM_EXIT_HYPERCALL */
281 		struct {
282 			/* The hypercall's arguments */
283 			__u64 args[8];	/* in-out */
284 		} hypercall;
285 		/* GZVM_EXIT_INTERNAL_ERROR */
286 		struct {
287 			/* The errors codes about GZVM_EXIT_INTERNAL_ERROR */
288 			__u32 suberror;
289 			/* The number of elements used in data[] */
290 			__u32 ndata;
291 			/* Keep the detailed information about GZVM_EXIT_SYSTEM_EVENT */
292 			__u64 data[16];
293 		} internal;
294 		/* GZVM_EXIT_SYSTEM_EVENT */
295 		struct {
296 #define GZVM_SYSTEM_EVENT_SHUTDOWN       1
297 #define GZVM_SYSTEM_EVENT_RESET          2
298 #define GZVM_SYSTEM_EVENT_CRASH          3
299 #define GZVM_SYSTEM_EVENT_WAKEUP         4
300 #define GZVM_SYSTEM_EVENT_SUSPEND        5
301 #define GZVM_SYSTEM_EVENT_SEV_TERM       6
302 #define GZVM_SYSTEM_EVENT_S2IDLE         7
303 			/* System event type.
304 			 * Ex. GZVM_SYSTEM_EVENT_SHUTDOWN or GZVM_SYSTEM_EVENT_RESET...etc.
305 			 */
306 			__u32 type;
307 			/* The number of elements used in data[] */
308 			__u32 ndata;
309 			/* Keep the detailed information about GZVM_EXIT_SYSTEM_EVENT */
310 			__u64 data[16];
311 		} system_event;
312 		char padding[256];
313 	};
314 };
315 
316 /**
317  * struct gzvm_enable_cap: The `capability support` on GenieZone hypervisor
318  * @cap: `GZVM_CAP_ARM_PROTECTED_VM` or `GZVM_CAP_ARM_VM_IPA_SIZE`
319  * @args: x3-x7 registers can be used for additional args
320  */
321 struct gzvm_enable_cap {
322 	__u64 cap;
323 	__u64 args[5];
324 };
325 
326 #define GZVM_ENABLE_CAP            _IOW(GZVM_IOC_MAGIC,  0xa3, \
327 					struct gzvm_enable_cap)
328 
329 /* for GZVM_GET/SET_ONE_REG */
330 struct gzvm_one_reg {
331 	__u64 id;
332 	__u64 addr;
333 };
334 
335 #define GZVM_GET_ONE_REG	   _IOW(GZVM_IOC_MAGIC,  0xab, \
336 					struct gzvm_one_reg)
337 #define GZVM_SET_ONE_REG	   _IOW(GZVM_IOC_MAGIC,  0xac, \
338 					struct gzvm_one_reg)
339 
340 #define GZVM_REG_GENERIC	   0x0000000000000000ULL
341 
342 #define GZVM_IRQFD_FLAG_DEASSIGN	BIT(0)
343 /*
344  * GZVM_IRQFD_FLAG_RESAMPLE indicates resamplefd is valid and specifies
345  * the irqfd to operate in resampling mode for level triggered interrupt
346  * emulation.
347  */
348 #define GZVM_IRQFD_FLAG_RESAMPLE	BIT(1)
349 
350 /**
351  * struct gzvm_irqfd: gzvm irqfd descriptor
352  * @fd: File descriptor.
353  * @gsi: Used for level IRQ fast-path.
354  * @flags: FLAG_DEASSIGN or FLAG_RESAMPLE.
355  * @resamplefd: The file descriptor of the resampler.
356  * @pad: Reserved for future-proof.
357  */
358 struct gzvm_irqfd {
359 	__u32 fd;
360 	__u32 gsi;
361 	__u32 flags;
362 	__u32 resamplefd;
363 	__u8  pad[16];
364 };
365 
366 #define GZVM_IRQFD	_IOW(GZVM_IOC_MAGIC, 0x76, struct gzvm_irqfd)
367 
368 enum {
369 	gzvm_ioeventfd_flag_nr_datamatch = 0,
370 	gzvm_ioeventfd_flag_nr_pio = 1,
371 	gzvm_ioeventfd_flag_nr_deassign = 2,
372 	gzvm_ioeventfd_flag_nr_max,
373 };
374 
375 #define GZVM_IOEVENTFD_FLAG_DATAMATCH	(1 << gzvm_ioeventfd_flag_nr_datamatch)
376 #define GZVM_IOEVENTFD_FLAG_PIO		(1 << gzvm_ioeventfd_flag_nr_pio)
377 #define GZVM_IOEVENTFD_FLAG_DEASSIGN	(1 << gzvm_ioeventfd_flag_nr_deassign)
378 #define GZVM_IOEVENTFD_VALID_FLAG_MASK	((1 << gzvm_ioeventfd_flag_nr_max) - 1)
379 
380 struct gzvm_ioeventfd {
381 	__u64 datamatch;
382 	/* private: legal pio/mmio address */
383 	__u64 addr;
384 	/* private: 1, 2, 4, or 8 bytes; or 0 to ignore length */
385 	__u32 len;
386 	__s32 fd;
387 	__u32 flags;
388 	__u8  pad[36];
389 };
390 
391 #define GZVM_IOEVENTFD	_IOW(GZVM_IOC_MAGIC, 0x79, struct gzvm_ioeventfd)
392 
393 /**
394  * struct gzvm_dtb_config: store address and size of dtb passed from userspace
395  *
396  * @dtb_addr: dtb address set by VMM (guset memory)
397  * @dtb_size: dtb size
398  */
399 struct gzvm_dtb_config {
400 	__u64 dtb_addr;
401 	__u64 dtb_size;
402 };
403 
404 #define GZVM_SET_DTB_CONFIG       _IOW(GZVM_IOC_MAGIC, 0xff, \
405 				       struct gzvm_dtb_config)
406 
407 #endif /* __GZVM_H__ */
408