1 /*
2  * common definition
3  *
4  * Copyright (C) 2017-2022 Intel Corporation.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 /**
10  * @file acrn_common.h
11  *
12  * @brief acrn common data structure for hypercall or ioctl
13  */
14 
15 #ifndef ACRN_COMMON_H
16 #define ACRN_COMMON_H
17 
18 #include <types.h>
19 
20 /*
21  * Common structures for ACRN/HSM/DM
22  */
23 
24 /*
25  * IO request
26  */
27 
28 #define ACRN_IO_REQUEST_MAX		16U
29 #define ACRN_ASYNCIO_MAX		64U
30 
31 #define ACRN_IOREQ_STATE_PENDING	0U
32 #define ACRN_IOREQ_STATE_COMPLETE	1U
33 #define ACRN_IOREQ_STATE_PROCESSING	2U
34 #define ACRN_IOREQ_STATE_FREE		3U
35 
36 #define ACRN_IOREQ_TYPE_PORTIO		0U
37 #define ACRN_IOREQ_TYPE_MMIO		1U
38 #define ACRN_IOREQ_TYPE_PCICFG		2U
39 #define ACRN_IOREQ_TYPE_WP		3U
40 
41 #define ACRN_IOREQ_DIR_READ		0U
42 #define ACRN_IOREQ_DIR_WRITE		1U
43 
44 
45 
46 /* IOAPIC device model info */
47 #define VIOAPIC_RTE_NUM	48U  /* vioapic pins */
48 
49 #if VIOAPIC_RTE_NUM < 24U
50 #error "VIOAPIC_RTE_NUM must be larger than 23"
51 #endif
52 
53 /* Generic VM flags from guest OS */
54 #define GUEST_FLAG_SECURE_WORLD_ENABLED		(1UL << 0U)	/* Whether secure world is enabled */
55 #define GUEST_FLAG_LAPIC_PASSTHROUGH		(1UL << 1U)  	/* Whether LAPIC is passed through */
56 #define GUEST_FLAG_IO_COMPLETION_POLLING	(1UL << 2U)  	/* Whether need hypervisor poll IO completion */
57 #define GUEST_FLAG_HIDE_MTRR			(1UL << 3U)  	/* Whether hide MTRR from VM */
58 #define GUEST_FLAG_RT				(1UL << 4U)     /* Whether the vm is RT-VM */
59 #define GUEST_FLAG_NVMX_ENABLED			(1UL << 5U)	/* Whether this VM supports nested virtualization */
60 #define GUEST_FLAG_SECURITY_VM			(1UL << 6U)	/* Whether this VM needs to do security-vm related fixup (TPM2 and SMBIOS pt) */
61 #define GUEST_FLAG_VCAT_ENABLED			(1UL << 7U)	/* Whether this VM supports vCAT */
62 #define GUEST_FLAG_STATIC_VM       (1UL << 8U)  /* Whether this VM uses static VM configuration */
63 #define GUEST_FLAG_TEE				(1UL << 9U)	/* Whether the VM is TEE VM */
64 #define GUEST_FLAG_REE				(1UL << 10U)	/* Whether the VM is REE VM */
65 #define GUEST_FLAG_PMU_PASSTHROUGH	(1UL << 11U)    /* Whether PMU is passed through */
66 #define GUEST_FLAG_VHWP				(1UL << 12U)    /* Whether the VM supports vHWP */
67 #define GUEST_FLAG_VTM				(1UL << 13U)    /* Whether the VM supports virtual thermal monitor */
68 #define GUEST_FLAG_STATELESS			(1UL << 14U)	/* Whether the VM is stateless (can be forcefully shutdown with no data loss) */
69 
70 /* TODO: We may need to get this addr from guest ACPI instead of hardcode here */
71 #define VIRTUAL_SLEEP_CTL_ADDR		0x400U /* Pre-launched VM uses ACPI reduced HW mode and sleep control register */
72 #define VIRTUAL_PM1A_CNT_ADDR		0x404U
73 #define	VIRTUAL_PM1A_SCI_EN		0x0001
74 #define VIRTUAL_PM1A_SLP_TYP		0x1c00U
75 #define VIRTUAL_PM1A_SLP_EN		0x2000U
76 #define	VIRTUAL_PM1A_ALWAYS_ZERO	0xc003
77 
78 #define MAX_VM_NAME_LEN        (16U)
79 
80 /**
81  * @brief Hypercall
82  *
83  * @addtogroup acrn_hypercall ACRN Hypercall
84  * @{
85  */
86 
87 /**
88  * @brief Representation of a MMIO request
89  */
90 struct acrn_mmio_request {
91 	/**
92 	 * @brief Direction of the access
93 	 *
94 	 * Either \p ACRN_IOREQ_DIR_READ or \p ACRN_IOREQ_DIR_WRITE.
95 	 */
96 	uint32_t direction;
97 
98 	/**
99 	 * @brief reserved
100 	 */
101 	uint32_t reserved;
102 
103 	/**
104 	 * @brief Address of the I/O access
105 	 */
106 	uint64_t address;
107 
108 	/**
109 	 * @brief Width of the I/O access in byte
110 	 */
111 	uint64_t size;
112 
113 	/**
114 	 * @brief The value read for I/O reads or to be written for I/O writes
115 	 */
116 	uint64_t value;
117 };
118 
119 /**
120  * @brief Representation of a port I/O request
121  */
122 struct acrn_pio_request {
123 	/**
124 	 * @brief Direction of the access
125 	 *
126 	 * Either \p ACRN_IOREQ_DIR_READ or \p ACRN_IOREQ_DIR_WRITE.
127 	 */
128 	uint32_t direction;
129 
130 	/**
131 	 * @brief reserved
132 	 */
133 	uint32_t reserved;
134 
135 	/**
136 	 * @brief Port address of the I/O access
137 	 */
138 	uint64_t address;
139 
140 	/**
141 	 * @brief Width of the I/O access in byte
142 	 */
143 	uint64_t size;
144 
145 	/**
146 	 * @brief The value read for I/O reads or to be written for I/O writes
147 	 */
148 	uint32_t value;
149 };
150 
151 /**
152  * @brief Representation of a PCI configuration space access
153  */
154 struct acrn_pci_request {
155 	/**
156 	 * @brief Direction of the access
157 	 *
158 	 * Either \p ACRN_IOREQ_DIR_READ or \p ACRN_IOREQ_DIR_WRITE.
159 	 */
160 	uint32_t direction;
161 
162 	/**
163 	 * @brief Reserved
164 	 */
165 	uint32_t reserved[3];/* need keep same header fields with pio_request */
166 
167 	/**
168 	 * @brief Width of the I/O access in byte
169 	 */
170 	int64_t size;
171 
172 	/**
173 	 * @brief The value read for I/O reads or to be written for I/O writes
174 	 */
175 	int32_t value;
176 
177 	/**
178 	 * @brief The \p bus part of the BDF of the device
179 	 */
180 	int32_t bus;
181 
182 	/**
183 	 * @brief The \p device part of the BDF of the device
184 	 */
185 	int32_t dev;
186 
187 	/**
188 	 * @brief The \p function part of the BDF of the device
189 	 */
190 	int32_t func;
191 
192 	/**
193 	 * @brief The register to be accessed in the configuration space
194 	 */
195 	int32_t reg;
196 };
197 
198 /**
199  * @brief 256-byte I/O requests
200  *
201  * The state transitions of a I/O request are:
202  *
203  *    FREE -> PENDING -> PROCESSING -> COMPLETE -> FREE -> ...
204  *
205  * When a request is in COMPLETE or FREE state, the request is owned by the
206  * hypervisor. Service VM (HSM or DM) shall not read or write the internals of the
207  * request except the state.
208  *
209  * When a request is in PENDING or PROCESSING state, the request is owned by
210  * Service VM. The hypervisor shall not read or write the request other than the state.
211  *
212  * Based on the rules above, a typical I/O request lifecycle should looks like
213  * the following.
214  *
215  * @verbatim embed:rst:leading-asterisk
216  *
217  * +-----------------------+-------------------------+----------------------+
218  * | Service VM vCPU 0     | Service VM vCPU x       | User VM vCPU y       |
219  * +=======================+=========================+======================+
220  * |                       |                         | Hypervisor:          |
221  * |                       |                         |                      |
222  * |                       |                         | - Fill in type,      |
223  * |                       |                         |   addr, etc.         |
224  * |                       |                         | - Pause User VM vCPU |
225  * |                       |                         | - Set state to       |
226  * |                       |                         |   PENDING (a)        |
227  * |                       |                         | - Fire upcall to     |
228  * |                       |                         |   Service VM vCPU 0  |
229  * |                       |                         |                      |
230  * +-----------------------+-------------------------+----------------------+
231  * | HSM:                  |                         |                      |
232  * |                       |                         |                      |
233  * | - Scan for pending    |                         |                      |
234  * |   requests            |                         |                      |
235  * | - Set state to        |                         |                      |
236  * |   PROCESSING (b)      |                         |                      |
237  * | - Assign requests to  |                         |                      |
238  * |   clients (c)         |                         |                      |
239  * |                       |                         |                      |
240  * +-----------------------+-------------------------+----------------------+
241  * |                       | Client:                 |                      |
242  * |                       |                         |                      |
243  * |                       | - Scan for assigned     |                      |
244  * |                       |   requests              |                      |
245  * |                       | - Handle the            |                      |
246  * |                       |   requests (d)          |                      |
247  * |                       | - Set state to COMPLETE |                      |
248  * |                       | - Notify the hypervisor |                      |
249  * |                       |                         |                      |
250  * +-----------------------+-------------------------+----------------------+
251  * |                       | Hypervisor:             |                      |
252  * |                       |                         |                      |
253  * |                       | - resume User VM vCPU y |                      |
254  * |                       |   (e)                   |                      |
255  * |                       |                         |                      |
256  * +-----------------------+-------------------------+----------------------+
257  * |                       |                         | Hypervisor:          |
258  * |                       |                         |                      |
259  * |                       |                         | - Post-work (f)      |
260  * |                       |                         | - set state to FREE  |
261  * |                       |                         |                      |
262  * +-----------------------+-------------------------+----------------------+
263  *
264  * @endverbatim
265  *
266  * Note that the following shall hold.
267  *
268  *   1. (a) happens before (b)
269  *   2. (c) happens before (d)
270  *   3. (e) happens before (f)
271  *   4. One vCPU cannot trigger another I/O request before the previous one has
272  *      completed (i.e. the state switched to FREE)
273  *
274  * Accesses to the state of a acrn_io_request shall be atomic and proper barriers
275  * are needed to ensure that:
276  *
277  *   1. Setting state to PENDING is the last operation when issuing a request in
278  *      the hypervisor, as the hypervisor shall not access the request any more.
279  *
280  *   2. Due to similar reasons, setting state to COMPLETE is the last operation
281  *      of request handling in HSM or clients in Service VM.
282  */
283 struct acrn_io_request {
284 	/**
285 	 * @brief Type of this request.
286 	 *
287 	 * Byte offset: 0.
288 	 */
289 	uint32_t type;
290 
291 	/**
292 	 * @brief Hypervisor will poll completion if set.
293 	 *
294 	 * Byte offset: 4.
295 	 */
296 	uint32_t completion_polling;
297 
298 	/**
299 	 * @brief Reserved.
300 	 *
301 	 * Byte offset: 8.
302 	 */
303 	uint32_t reserved0[14];
304 
305 	/**
306 	 * @brief Details about this request.
307 	 *
308 	 * Byte offset: 64.
309 	 */
310 	union {
311 		struct acrn_pio_request		pio_request;
312 		struct acrn_pci_request		pci_request;
313 		struct acrn_mmio_request	mmio_request;
314 		uint64_t			data[8];
315 	} reqs;
316 
317 	/**
318 	 * @brief Reserved.
319 	 *
320 	 * Byte offset: 128.
321 	 */
322 	uint32_t reserved1;
323 
324 	/**
325 	 * @brief If this request has been handled by HSM driver.
326 	 *
327 	 * Byte offset: 132.
328 	 */
329 	int32_t kernel_handled;
330 
331 	/**
332 	 * @brief The status of this request.
333 	 *
334 	 * Taking ACRN_IOREQ_STATE_xxx as values.
335 	 *
336 	 * Byte offset: 136.
337 	 */
338 	uint32_t processed;
339 } __aligned(256);
340 
341 struct acrn_io_request_buffer {
342 	union {
343 		struct acrn_io_request	req_slot[ACRN_IO_REQUEST_MAX];
344 		int8_t			reserved[4096];
345 	};
346 };
347 
348 struct acrn_asyncio_info {
349 	uint32_t type;
350 	uint32_t match_data;
351 	uint64_t addr;
352 	uint64_t fd;
353 	uint64_t data;
354 };
355 
356 /**
357  * @brief Info to create a VM, the parameter for HC_CREATE_VM hypercall
358  */
359 struct acrn_vm_creation {
360 	/** created vmid return to HSM. Keep it first field */
361 	uint16_t vmid;
362 
363 	/** Reserved */
364 	uint16_t reserved0;
365 
366 	/** VCPU numbers this VM want to create */
367 	uint16_t vcpu_num;
368 
369 	/** Reserved */
370 	uint16_t reserved1;
371 
372 	/** the name of this VM */
373 	uint8_t	 name[MAX_VM_NAME_LEN];
374 
375 	/* VM flag bits from Guest OS, now used
376 	 *  GUEST_FLAG_SECURE_WORLD_ENABLED          (1UL<<0)
377 	 */
378 	uint64_t vm_flag;
379 
380 	uint64_t ioreq_buf;
381 
382 	/**
383 	 *   The least significant set bit is the PCPU # the VCPU 0 maps to;
384 	 *   second set least significant bit is the PCPU # the VCPU 1 maps to;
385 	 *   and so on...
386 	*/
387 	uint64_t cpu_affinity;
388 };
389 
390 /* General-purpose register layout aligned with the general-purpose register idx
391  * when vmexit, such as vmexit due to CR access, refer to SMD Vol.3C 27-6.
392  */
393 struct acrn_gp_regs {
394 	uint64_t rax;
395 	uint64_t rcx;
396 	uint64_t rdx;
397 	uint64_t rbx;
398 	uint64_t rsp;
399 	uint64_t rbp;
400 	uint64_t rsi;
401 	uint64_t rdi;
402 	uint64_t r8;
403 	uint64_t r9;
404 	uint64_t r10;
405 	uint64_t r11;
406 	uint64_t r12;
407 	uint64_t r13;
408 	uint64_t r14;
409 	uint64_t r15;
410 };
411 
412 /* struct to define how the descriptor stored in memory.
413  * Refer SDM Vol3 3.5.1 "Segment Descriptor Tables"
414  * Figure 3-11
415  */
416 struct acrn_descriptor_ptr {
417 	uint16_t limit;
418 	uint64_t base;
419 	uint16_t reserved[3];   /* align struct size to 64bit */
420 } __packed;
421 
422 /**
423  * @brief registers info for vcpu.
424  */
425 struct acrn_regs {
426 	struct acrn_gp_regs gprs;
427 	struct acrn_descriptor_ptr gdt;
428 	struct acrn_descriptor_ptr idt;
429 
430 	uint64_t        rip;
431 	uint64_t        cs_base;
432 	uint64_t        cr0;
433 	uint64_t        cr4;
434 	uint64_t        cr3;
435 	uint64_t        ia32_efer;
436 	uint64_t        rflags;
437 	uint64_t        reserved_64[4];
438 
439 	uint32_t        cs_ar;
440 	uint32_t        cs_limit;
441 	uint32_t        reserved_32[3];
442 
443 	/* don't change the order of following sel */
444 	uint16_t        cs_sel;
445 	uint16_t        ss_sel;
446 	uint16_t        ds_sel;
447 	uint16_t        es_sel;
448 	uint16_t        fs_sel;
449 	uint16_t        gs_sel;
450 	uint16_t        ldt_sel;
451 	uint16_t        tr_sel;
452 };
453 
454 /**
455  * @brief Info to set vcpu state
456  *
457  * the parameter for HC_SET_VCPU_STATE
458  */
459 struct acrn_vcpu_regs {
460 	/** the virtual CPU ID for the VCPU to set state */
461 	uint16_t vcpu_id;
462 
463 	/** reserved space to make cpu_state aligned to 8 bytes */
464 	uint16_t reserved[3];
465 
466 	/** the structure to hold vcpu state */
467 	struct acrn_regs vcpu_regs;
468 };
469 
470 /** Operation types for setting IRQ line */
471 #define GSI_SET_HIGH		0U
472 #define GSI_SET_LOW		1U
473 #define GSI_RAISING_PULSE	2U
474 #define GSI_FALLING_PULSE	3U
475 
476 /**
477  * @brief Info to Set/Clear/Pulse a virtual IRQ line for a VM
478  *
479  * the parameter for HC_SET_IRQLINE hypercall
480  */
481 struct acrn_irqline_ops {
482 	uint32_t gsi;
483 	uint32_t op;
484 };
485 
486 
487 /**
488  * @brief Info to inject a MSI interrupt to VM
489  *
490  * the parameter for HC_INJECT_MSI hypercall
491  */
492 struct acrn_msi_entry {
493 	/** MSI addr[19:12] with dest VCPU ID */
494 	uint64_t msi_addr;
495 
496 	/** MSI data[7:0] with vector */
497 	uint64_t msi_data;
498 };
499 
500 /**
501  * @brief Info The power state data of a VCPU.
502  *
503  */
504 
505 #define SPACE_SYSTEM_MEMORY     0U
506 #define SPACE_SYSTEM_IO         1U
507 #define SPACE_PCI_CONFIG        2U
508 #define SPACE_Embedded_Control  3U
509 #define SPACE_SMBUS             4U
510 #define SPACE_PLATFORM_COMM     10U
511 #define SPACE_FFixedHW          0x7FU
512 
513 struct acrn_acpi_generic_address {
514 	uint8_t 	space_id;
515 	uint8_t 	bit_width;
516 	uint8_t 	bit_offset;
517 	uint8_t 	access_size;
518 	uint64_t	address;
519 } __attribute__ ((__packed__));
520 
521 struct acrn_cstate_data {
522 	struct acrn_acpi_generic_address cx_reg;
523 	uint8_t 	type;
524 	uint32_t	latency;
525 	uint64_t	power;
526 };
527 
528 struct acrn_pstate_data {
529 	uint64_t core_frequency;	/* megahertz */
530 	uint64_t power;			/* milliWatts */
531 	uint64_t transition_latency;	/* microseconds */
532 	uint64_t bus_master_latency;	/* microseconds */
533 	uint64_t control;		/* control value */
534 	uint64_t status;		/* success indicator */
535 };
536 
537 enum acrn_cpufreq_policy_type {
538 	CPUFREQ_POLICY_PERFORMANCE,
539 	CPUFREQ_POLICY_NOMINAL,
540 };
541 
542 struct acrn_cpufreq_limits {
543 	/* Performance levels for HWP */
544 	uint8_t guaranteed_hwp_lvl;
545 	uint8_t highest_hwp_lvl;
546 	uint8_t lowest_hwp_lvl;
547 	/* Index for the p-state table _PSS */
548 	uint8_t nominal_pstate;
549 	uint8_t performance_pstate;
550 };
551 
552 struct acpi_sx_pkg {
553 	uint8_t		val_pm1a;
554 	uint8_t		val_pm1b;
555 	uint16_t	reserved;
556 } __aligned(8);
557 
558 struct pm_s_state_data {
559 	struct acrn_acpi_generic_address pm1a_evt;
560 	struct acrn_acpi_generic_address pm1b_evt;
561 	struct acrn_acpi_generic_address pm1a_cnt;
562 	struct acrn_acpi_generic_address pm1b_cnt;
563 	struct acpi_sx_pkg s3_pkg;
564 	struct acpi_sx_pkg s5_pkg;
565 	uint32_t *wake_vector_32;
566 	uint64_t *wake_vector_64;
567 } __aligned(8);
568 
569 /**
570  * @brief Info PM command from DM/HSM.
571  *
572  * The command would specify request type(e.g. get px count or data) for
573  * specific VM and specific VCPU with specific state number.
574  * For Px, PMCMD_STATE_NUM means Px number from 0 to (MAX_PSTATE - 1),
575  * For Cx, PMCMD_STATE_NUM means Cx entry index from 1 to MAX_CX_ENTRY.
576  */
577 #define PMCMD_VMID_MASK		0xff000000U
578 #define PMCMD_VCPUID_MASK	0x00ff0000U
579 #define PMCMD_STATE_NUM_MASK	0x0000ff00U
580 #define PMCMD_TYPE_MASK		0x000000ffU
581 
582 #define PMCMD_VMID_SHIFT	24U
583 #define PMCMD_VCPUID_SHIFT	16U
584 #define PMCMD_STATE_NUM_SHIFT	8U
585 
586 enum acrn_pm_cmd_type {
587 	ACRN_PMCMD_GET_PX_CNT,
588 	ACRN_PMCMD_GET_PX_DATA,
589 	ACRN_PMCMD_GET_CX_CNT,
590 	ACRN_PMCMD_GET_CX_DATA,
591 };
592 
593 /**
594  * @brief Info to get a VM interrupt count data
595  *
596  * the parameter for HC_VM_INTR_MONITOR hypercall
597  */
598 #define MAX_PTDEV_NUM 24U
599 struct acrn_intr_monitor {
600 	/** sub command for intr monitor */
601 	uint32_t cmd;
602 	/** the count of this buffer to save */
603 	uint32_t buf_cnt;
604 
605 	/** the buffer which save each interrupt count */
606 	uint64_t buffer[MAX_PTDEV_NUM * 2];
607 } __aligned(8);
608 
609 /** cmd for intr monitor **/
610 #define INTR_CMD_GET_DATA 0U
611 #define INTR_CMD_DELAY_INT 1U
612 
613 /*
614  * PRE_LAUNCHED_VM is launched by ACRN hypervisor, with LAPIC_PT;
615  * Service VM is launched by ACRN hypervisor, without LAPIC_PT;
616  * POST_LAUNCHED_VM is launched by ACRN devicemodel, with/without LAPIC_PT depends on usecases.
617  *
618  * Assumption: vm_configs array is completely initialized w.r.t. load_order member of
619  * 		acrn_vm_config for all the VMs.
620  */
621 enum acrn_vm_load_order {
622 	PRE_LAUNCHED_VM = 0,
623 	SERVICE_VM,
624 	POST_LAUNCHED_VM,	/* Launched by Devicemodel in Service VM */
625 	MAX_LOAD_ORDER
626 };
627 
628 struct acrn_vm_config_header {
629        enum acrn_vm_load_order load_order;
630        char name[MAX_VM_NAME_LEN];
631        uint8_t reserved[2];
632        uint8_t severity;
633        uint64_t cpu_affinity;
634        uint64_t guest_flags;
635        /*
636         * The following are hv-specific members and are thus opaque.
637         * vm_config_entry_size determines the real size of this structure.
638         */
639 } __aligned(8);
640 
641 /**
642  * @brief Info to configure virtual root port
643  *
644  * Configuration passed to hv when adding a virtual root port which
645  * is used as PTM root
646  */
647 struct vrp_config
648 {
649 	uint16_t phy_bdf;
650 	uint8_t max_payload; /* dev cap's max payload */
651 	uint8_t primary_bus;
652 	uint8_t secondary_bus;
653 	uint8_t subordinate_bus;
654 	uint8_t ptm_capable;
655 	uint32_t ptm_cap_offset;
656 };
657 
658 /* Type of PCI device assignment */
659 #define ACRN_PTDEV_QUIRK_ASSIGN	(1U << 0)
660 
661 #define ACRN_PCI_NUM_BARS	6U
662 /**
663  * @brief Info to assign or deassign PCI for a VM
664  *
665  */
666 struct acrn_pcidev {
667 	/** the type of the the pass-through PCI device */
668 	uint32_t type;
669 
670 	/** virtual BDF# of the pass-through PCI device */
671 	uint16_t virt_bdf;
672 
673 	/** physical BDF# of the pass-through PCI device */
674 	uint16_t phys_bdf;
675 
676 	/** the PCI Interrupt Line, initialized by ACRN-DM, which is RO and
677 	 *  ideally not used for pass-through MSI/MSI-x devices.
678 	 */
679 	uint8_t intr_line;
680 
681 	/** the PCI Interrupt Pin, initialized by ACRN-DM, which is RO and
682 	 *  ideally not used for pass-through MSI/MSI-x devices.
683 	 */
684 	uint8_t intr_pin;
685 
686 	/** the base address of the PCI BAR, initialized by ACRN-DM. */
687 	uint32_t bar[ACRN_PCI_NUM_BARS];
688 };
689 
690 #define MMIODEV_RES_NUM        3
691 
692 /**
693  * @brief Info to assign or deassign a MMIO device for a VM
694  */
695 struct acrn_mmiodev {
696 	char name[8];
697 	struct acrn_mmiores {
698 		/** the gpa of the MMIO region for the MMIO device */
699 		uint64_t user_vm_pa;
700 		/** the hpa of the MMIO region for the MMIO device: for post-launched VM
701 		  * it's pa in service vm; for pre-launched VM it's pa in HV.
702 		  */
703 		uint64_t host_pa;
704 		/** the size of the MMIO region for the MMIO resource */
705 		uint64_t size;
706 		/** the memory type of the MMIO region for the MMIO resource */
707 		uint64_t mem_type;
708 	} res[MMIODEV_RES_NUM];
709 };
710 
711 /**
712  * @brief Info to create or destroy a virtual PCI or legacy device for a VM
713  *
714  * the parameter for HC_CREATE_VDEV or HC_DESTROY_VDEV hypercall
715  */
716 struct acrn_vdev {
717 	/*
718 	 * the identifier of the device, the low 32 bits represent the vendor
719 	 * id and device id of PCI device and the high 32 bits represent the
720 	 * device number of the legacy device
721 	 */
722 	union {
723 		uint64_t value;
724 		struct {
725 			uint16_t vendor;
726 			uint16_t device;
727 			uint32_t legacy_id;
728 		} fields;
729 	} id;
730 
731 	/*
732 	 * the slot of the device, if the device is a PCI device, the slot
733 	 * represents BDF, otherwise it represents legacy device slot number
734 	 */
735 	uint64_t slot;
736 
737 	/** the IO resource address of the device, initialized by ACRN-DM. */
738 	uint32_t io_addr[ACRN_PCI_NUM_BARS];
739 
740 	/** the IO resource size of the device, initialized by ACRN-DM. */
741 	uint32_t io_size[ACRN_PCI_NUM_BARS];
742 
743 	/** the options for the virtual device, initialized by ACRN-DM. */
744 	uint8_t	args[128];
745 };
746 
747 #define ACRN_ASYNCIO_PIO	(0x01U)
748 #define ACRN_ASYNCIO_MMIO	(0x02U)
749 
750 #define SBUF_MAGIC	0x5aa57aa71aa13aa3UL
751 #define SBUF_MAX_SIZE	(1UL << 22U)
752 #define SBUF_HEAD_SIZE	64U
753 
754 /* sbuf flags */
755 #define OVERRUN_CNT_EN	(1U << 0U) /* whether overrun counting is enabled */
756 #define OVERWRITE_EN	(1U << 1U) /* whether overwrite is enabled */
757 
758 /**
759  * (sbuf) head + buf (store (ele_num - 1) elements at most)
760  * buffer empty: tail == head
761  * buffer full:  (tail + ele_size) % size == head
762  *
763  *             Base of memory for elements
764  *                |
765  *                |
766  * ----------------------------------------------------------------------
767  * | struct shared_buf | raw data (ele_size)| ... | raw data (ele_size) |
768  * ----------------------------------------------------------------------
769  * |
770  * |
771  * struct shared_buf *buf
772  */
773 
774 enum {
775 	ACRN_TRACE = 0U,
776 	ACRN_HVLOG,
777 	ACRN_SEP,
778 	ACRN_SOCWATCH,
779 	/* The sbuf with above ids are created each pcpu */
780 	ACRN_SBUF_PER_PCPU_ID_MAX,
781 	ACRN_ASYNCIO = 64,
782 	ACRN_VM_EVENT,
783 };
784 
785 /* Make sure sizeof(struct shared_buf) == SBUF_HEAD_SIZE */
786 struct shared_buf {
787 	uint64_t magic;
788 	uint32_t ele_num;	/* number of elements */
789 	uint32_t ele_size;	/* sizeof of elements */
790 	uint32_t head;		/* offset from base, to read */
791 	uint32_t tail;		/* offset from base, to write */
792 	uint32_t flags;
793 	uint32_t reserved;
794 	uint32_t overrun_cnt;	/* count of overrun */
795 	uint32_t size;		/* ele_num * ele_size */
796 	uint32_t padding[6];
797 };
798 
799 /**
800  * VM event architecture:
801  * +------------------------------------------------------+
802  * |    Service VM                                        |
803  * | +----------------------------+                       |
804  * | | DM  +--------------------+ |                       |
805  * | |     | [ event source ]   | |                       |
806  * | |     +-+------------+-----+ |                       |
807  * | |       | (eventfd)  |(sbuf) |                       |
808  * | |       v            v       |                       |
809  * | | +------------------------+ | (socket)  +---------+ |
810  * | | |  [event deliver logic] |-+---------->| Libvirt | |
811  * | | +------------------------+ |           +---------+ |
812  * | |       ^            ^       |                       |
813  * | +-------|------------|-------+                       |
814  * |         | (eventfd)  | (sbuf)                        |
815  * +---------|------------|-------------------------------+
816  * +---------|------------|-------------------------------+
817  * | kernel [HSM]         |                               |
818  * |         ^            |                               |
819  * +---------|------------|-------------------------------+
820  *           |upcall      |
821  * +---------+------------+-------------------------------+
822  * | HV     [ event source ]                              |
823  * +------------------------------------------------------+
824  *
825  * For event sources in HV
826  *  - HV puts the event in the shared ring sbuf.
827  *  - The hypervisor notifies the service VM via upcall.
828  *  - HSM in the service VM notifies device model via eventfd.
829  *  - The device model fetches and handles events from the shared ring sbuf.
830  * For event sources in DM
831  *  - DM puts the event in the DM event ring sbuf.
832  *  - DM notifies event delivery logic via eventfd.
833  *  - The event delivery logic fetches and handles events from the DM event ring sbuf.
834  */
835 #define VM_EVENT_RTC_CHG	0U
836 #define VM_EVENT_POWEROFF	1U
837 #define VM_EVENT_TRIPLE_FAULT	2U
838 
839 #define VM_EVENT_COUNT		3U
840 
841 #define VM_EVENT_DATA_LEN	28U
842 
843 struct vm_event {
844     uint32_t type;
845     uint8_t event_data[VM_EVENT_DATA_LEN];
846 };
847 
848 struct rtc_change_event_data {
849 	int64_t	last_time; /* time(in secs) of the RTC defore been set */
850 	int64_t delta_time; /* delta of time(in secs) the RTC has been changed */
851 };
852 
853 /* DM vrtc's reference time is besed on sys time, while the HV vrtc is based on pRTC.
854  * When the delta time is sent to users, they need to know what it is relative to.
855  */
856 #define RTC_CHG_RELATIVE_PHYSICAL_RTC		0
857 #define RTC_CHG_RELATIVE_SERVICE_VM_SYS_TIME	1
858 /**
859  * @}
860  */
861 #endif /* ACRN_COMMON_H */
862