1 /*
2  * Copyright (C) 2018-2022 Intel Corporation.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef VM_CONFIG_H_
8 #define VM_CONFIG_H_
9 
10 #include <types.h>
11 #include <pci.h>
12 #include <board_info.h>
13 #include <boot.h>
14 #include <acrn_common.h>
15 #include <vm_configurations.h>
16 #include <asm/sgx.h>
17 #include <acrn_hv_defs.h>
18 #include <schedule.h>
19 
20 #define AFFINITY_CPU(n)		(1UL << (n))
21 #define MAX_VCPUS_PER_VM	MAX_PCPU_NUM
22 #define MAX_VM_OS_NAME_LEN	32U
23 #define MAX_MOD_TAG_LEN		32U
24 
25 #if defined(CONFIG_SCHED_NOOP)
26 #define SERVICE_VM_IDLE		""
27 #elif defined(CONFIG_SCHED_PRIO)
28 #define SERVICE_VM_IDLE		""
29 #else
30 #define SERVICE_VM_IDLE		"idle=halt "
31 #endif
32 
33 #define PCI_DEV_TYPE_NONE		0U
34 #define PCI_DEV_TYPE_PTDEV		(1U << 0U)
35 #define PCI_DEV_TYPE_HVEMUL		(1U << 1U)
36 #define PCI_DEV_TYPE_SERVICE_VM_EMUL	(1U << 2U)
37 #define PCI_DEV_TYPE_DUMMY_MF_EMUL	(1U << 3U)
38 
39 #define MAX_MMIO_DEV_NUM	2U
40 
41 #define CONFIG_SERVICE_VM	.load_order = SERVICE_VM,	\
42 				.severity = SEVERITY_SERVICE_VM
43 
44 #define CONFIG_SAFETY_VM	.load_order = PRE_LAUNCHED_VM,	\
45 				.severity = SEVERITY_SAFETY_VM
46 
47 #define CONFIG_PRE_STD_VM	.load_order = PRE_LAUNCHED_VM,	\
48 				.severity = SEVERITY_STANDARD_VM
49 
50 #define CONFIG_PRE_RT_VM	.load_order = PRE_LAUNCHED_VM,	\
51 				.severity = SEVERITY_RTVM
52 
53 #define CONFIG_POST_STD_VM	.load_order = POST_LAUNCHED_VM,	\
54 				.severity = SEVERITY_STANDARD_VM
55 
56 #define CONFIG_POST_RT_VM	.load_order = POST_LAUNCHED_VM,	\
57 				.severity = SEVERITY_RTVM
58 
59 /* Bitmask of guest flags that can be programmed by device model. Other bits are set by hypervisor only. */
60 #if (SERVICE_VM_NUM == 0)
61 #define DM_OWNED_GUEST_FLAG_MASK	0UL
62 #elif defined(CONFIG_RELEASE)
63 #define DM_OWNED_GUEST_FLAG_MASK	(GUEST_FLAG_SECURE_WORLD_ENABLED | GUEST_FLAG_LAPIC_PASSTHROUGH \
64 					| GUEST_FLAG_RT | GUEST_FLAG_IO_COMPLETION_POLLING)
65 #else
66 #define DM_OWNED_GUEST_FLAG_MASK	(GUEST_FLAG_SECURE_WORLD_ENABLED | GUEST_FLAG_LAPIC_PASSTHROUGH \
67 					| GUEST_FLAG_RT | GUEST_FLAG_IO_COMPLETION_POLLING | GUEST_FLAG_PMU_PASSTHROUGH)
68 #endif
69 
70 /* ACRN guest severity */
71 enum acrn_vm_severity {
72 	SEVERITY_SAFETY_VM = 0x40U,
73 	SEVERITY_RTVM = RTVM_SEVERITY_LEVEL,
74 	SEVERITY_SERVICE_VM = 0x20U,
75 	SEVERITY_STANDARD_VM = 0x10U,
76 };
77 
78 struct vm_hpa_regions {
79 	uint64_t start_hpa;
80 	uint64_t size_hpa;
81 };
82 
83 struct acrn_vm_mem_config {
84 	uint64_t size;		/* VM memory size configuration */
85 	uint64_t region_num;
86 	struct vm_hpa_regions  *host_regions;
87 };
88 
89 struct target_vuart {
90 	uint8_t vm_id;		/* target VM id */
91 	uint8_t vuart_id;	/* target vuart index in a VM */
92 };
93 
94 enum vuart_type {
95 	VUART_LEGACY_PIO = 0,	/* legacy PIO vuart */
96 	VUART_PCI,		/* PCI vuart, may removed */
97 };
98 
99 union vuart_addr {
100 	uint16_t port_base;		/* addr for legacy type */
101 	struct {			/* addr for pci type */
102 		uint8_t f : 3;		/* BITs 0-2 */
103 		uint8_t d : 5;		/* BITs 3-7 */
104 		uint8_t b;		/* BITs 8-15 */
105 	} bdf;
106 };
107 
108 struct vuart_config {
109 	enum vuart_type type;		/* legacy PIO or PCI  */
110 	union vuart_addr addr;		/* port addr if in legacy type, or bdf addr if in pci type */
111 	uint16_t irq;
112 	struct target_vuart t_vuart;	/* target vuart */
113 } __aligned(8);
114 
115 enum os_kernel_type {
116 	KERNEL_BZIMAGE = 1,
117 	KERNEL_RAWIMAGE,
118 	KERNEL_ELF,
119 	KERNEL_UNKNOWN,
120 };
121 
122 struct acrn_vm_os_config {
123 	char name[MAX_VM_OS_NAME_LEN];			/* OS name, useful for debug */
124 	enum os_kernel_type kernel_type;		/* used for kernel specifc loading method */
125 	char kernel_mod_tag[MAX_MOD_TAG_LEN];		/* multiboot module tag for kernel */
126 	char ramdisk_mod_tag[MAX_MOD_TAG_LEN];		/* multiboot module tag for ramdisk */
127 	char bootargs[MAX_BOOTARGS_SIZE];		/* boot args/cmdline */
128 	uint64_t kernel_load_addr;
129 	uint64_t kernel_entry_addr;
130 	uint64_t kernel_ramdisk_addr;
131 } __aligned(8);
132 
133 struct acrn_vm_acpi_config {
134 	char acpi_mod_tag[MAX_MOD_TAG_LEN];		/* multiboot module tag for ACPI */
135 } __aligned(8);
136 
137 /* the vbdf is assgined by device model */
138 #define UNASSIGNED_VBDF        0xFFFFU
139 
140 struct acrn_vm_pci_dev_config {
141 	uint32_t emu_type;				/* the type how the device is emulated. */
142 	union pci_bdf vbdf;				/* virtual BDF of PCI device */
143 	union pci_bdf pbdf;				/* physical BDF of PCI device */
144 	char shm_region_name[32];			/* TODO: combine pbdf and shm_region_name into a union member */
145 	/* TODO: All device specific attributions need move to other place */
146 	struct target_vuart t_vuart;
147 	uint16_t vuart_idx;
148 	uint16_t vrp_sec_bus;			/* use virtual root port's secondary bus as unique identification */
149 	uint8_t vrp_max_payload;		/* vrp's dev cap's max payload */
150 	uint64_t vbar_base[PCI_BAR_COUNT];		/* vbar base address of PCI device, which is power-on default value */
151 	struct pci_pdev *pdev;				/* the physical PCI device if it's a PT device */
152 	const struct pci_vdev_ops *vdev_ops;		/* operations for PCI CFG read/write */
153 } __aligned(8);
154 
155 struct pt_intx_config {
156 	uint32_t phys_gsi;	/* physical IOAPIC gsi to be forwarded to the VM */
157 	uint32_t virt_gsi;	/* virtual IOAPIC gsi triggered on the vIOAPIC */
158 } __aligned(8);
159 
160 struct acrn_vm_config {
161 	enum acrn_vm_load_order load_order;		/* specify the load order of VM */
162 	char name[MAX_VM_NAME_LEN];				/* VM name identifier */
163 	uint8_t reserved[2];
164 	uint8_t severity;				/* severity of the VM */
165 	uint64_t cpu_affinity;				/* The set bits represent the pCPUs the vCPUs of
166 							 * the VM may run on.
167 							 */
168 	uint64_t guest_flags;				/* VM flags that we want to configure for guest
169 							 * Now we have two flags:
170 							 *	GUEST_FLAG_SECURE_WORLD_ENABLED
171 							 *	GUEST_FLAG_LAPIC_PASSTHROUGH
172 							 * We could add more guest flags in future;
173 							 */
174 	struct sched_params sched_params;		/* Scheduler params for vCPUs of this VM */
175 	uint16_t companion_vm_id;			/* The companion VM id for this VM */
176 	struct acrn_vm_mem_config memory;		/* memory configuration of VM */
177 	struct epc_section epc;				/* EPC memory configuration of VM */
178 	uint16_t pci_dev_num;				/* indicate how many PCI devices in VM */
179 	struct acrn_vm_pci_dev_config *pci_devs;	/* point to PCI devices BDF list */
180 	struct acrn_vm_os_config os_config;		/* OS information the VM */
181 	struct acrn_vm_acpi_config acpi_config;		/* ACPI config for the VM */
182 
183 	/*
184 	 * below are variable length members (per build).
185 	 * Service VM can get the vm_configs[] array through hypercall, but Service VM may not
186 	 * need to parse these members.
187 	 */
188 
189 	uint16_t num_pclosids; /* This defines the number of elements in the array pointed to by pclosids */
190 	/* pclosids: a pointer to an array of physical CLOSIDs (pCLOSIDs)) that is defined in vm_configurations.c
191 	 * by vmconfig,
192 	 * applicable only if CONFIG_RDT_ENABLED is defined on CAT capable platforms.
193 	 * The number of elements in the array must be equal to the value given by num_pclosids
194 	 */
195 	uint16_t *pclosids;
196 
197 	/* max_type_pcbm (type: l2 or l3) specifies the allocated portion of physical cache
198 	 * for the VM and is a contiguous capacity bitmask (CBM) starting at bit position low
199 	 * (the lowest assigned physical cache way) and ending at position high
200 	 * (the highest assigned physical cache way, inclusive).
201 	 * As CBM only allows contiguous '1' combinations, so max_type_pcbm essentially
202 	 * is a bitmask that selects/covers all the physical cache ways assigned to the VM.
203 	 */
204 	uint32_t max_l2_pcbm;
205 	uint32_t max_l3_pcbm;
206 
207 	struct vuart_config vuart[MAX_VUART_NUM_PER_VM];/* vuart configuration for VM */
208 
209 	bool pt_tpm2;
210 	struct acrn_mmiodev mmiodevs[MAX_MMIO_DEV_NUM];
211 
212 	bool pt_p2sb_bar; /* whether to passthru p2sb bridge to pre-launched VM or not */
213 
214 	uint16_t pt_intx_num; /* number of pt_intx_config entries pointed by pt_intx */
215 	struct pt_intx_config *pt_intx; /* stores the base address of struct pt_intx_config array */
216 } __aligned(8);
217 
218 struct acrn_vm_config *get_vm_config(uint16_t vm_id);
219 uint8_t get_vm_severity(uint16_t vm_id);
220 bool vm_has_matched_name(uint16_t vmid, const char *name);
221 
222 extern struct acrn_vm_config vm_configs[CONFIG_MAX_VM_NUM];
223 extern struct acrn_vm_config *const service_vm_config;
224 
225 #endif /* VM_CONFIG_H_ */
226