1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 /*
4  * This file contains definitions from Hyper-V Hypervisor Top-Level Functional
5  * Specification (TLFS):
6  * https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/reference/tlfs
7  */
8 
9 #ifndef _ASM_GENERIC_HYPERV_TLFS_H
10 #define _ASM_GENERIC_HYPERV_TLFS_H
11 
12 #include <linux/types.h>
13 #include <linux/bits.h>
14 #include <linux/time64.h>
15 
16 /*
17  * While not explicitly listed in the TLFS, Hyper-V always runs with a page size
18  * of 4096. These definitions are used when communicating with Hyper-V using
19  * guest physical pages and guest physical page addresses, since the guest page
20  * size may not be 4096 on all architectures.
21  */
22 #define HV_HYP_PAGE_SHIFT      12
23 #define HV_HYP_PAGE_SIZE       BIT(HV_HYP_PAGE_SHIFT)
24 #define HV_HYP_PAGE_MASK       (~(HV_HYP_PAGE_SIZE - 1))
25 
26 /*
27  * Hyper-V provides two categories of flags relevant to guest VMs.  The
28  * "Features" category indicates specific functionality that is available
29  * to guests on this particular instance of Hyper-V. The "Features"
30  * are presented in four groups, each of which is 32 bits. The group A
31  * and B definitions are common across architectures and are listed here.
32  * However, not all flags are relevant on all architectures.
33  *
34  * Groups C and D vary across architectures and are listed in the
35  * architecture specific portion of hyperv-tlfs.h. Some of these flags exist
36  * on multiple architectures, but the bit positions are different so they
37  * cannot appear in the generic portion of hyperv-tlfs.h.
38  *
39  * The "Enlightenments" category provides recommendations on whether to use
40  * specific enlightenments that are available. The Enlighenments are a single
41  * group of 32 bits, but they vary across architectures and are listed in
42  * the architecture specific portion of hyperv-tlfs.h.
43  */
44 
45 /*
46  * Group A Features.
47  */
48 
49 /* VP Runtime register available */
50 #define HV_MSR_VP_RUNTIME_AVAILABLE		BIT(0)
51 /* Partition Reference Counter available*/
52 #define HV_MSR_TIME_REF_COUNT_AVAILABLE		BIT(1)
53 /* Basic SynIC register available */
54 #define HV_MSR_SYNIC_AVAILABLE			BIT(2)
55 /* Synthetic Timer registers available */
56 #define HV_MSR_SYNTIMER_AVAILABLE		BIT(3)
57 /* Virtual APIC assist and VP assist page registers available */
58 #define HV_MSR_APIC_ACCESS_AVAILABLE		BIT(4)
59 /* Hypercall and Guest OS ID registers available*/
60 #define HV_MSR_HYPERCALL_AVAILABLE		BIT(5)
61 /* Access virtual processor index register available*/
62 #define HV_MSR_VP_INDEX_AVAILABLE		BIT(6)
63 /* Virtual system reset register available*/
64 #define HV_MSR_RESET_AVAILABLE			BIT(7)
65 /* Access statistics page registers available */
66 #define HV_MSR_STAT_PAGES_AVAILABLE		BIT(8)
67 /* Partition reference TSC register is available */
68 #define HV_MSR_REFERENCE_TSC_AVAILABLE		BIT(9)
69 /* Partition Guest IDLE register is available */
70 #define HV_MSR_GUEST_IDLE_AVAILABLE		BIT(10)
71 /* Partition local APIC and TSC frequency registers available */
72 #define HV_ACCESS_FREQUENCY_MSRS		BIT(11)
73 /* AccessReenlightenmentControls privilege */
74 #define HV_ACCESS_REENLIGHTENMENT		BIT(13)
75 /* AccessTscInvariantControls privilege */
76 #define HV_ACCESS_TSC_INVARIANT			BIT(15)
77 
78 /*
79  * Group B features.
80  */
81 #define HV_CREATE_PARTITIONS			BIT(0)
82 #define HV_ACCESS_PARTITION_ID			BIT(1)
83 #define HV_ACCESS_MEMORY_POOL			BIT(2)
84 #define HV_ADJUST_MESSAGE_BUFFERS		BIT(3)
85 #define HV_POST_MESSAGES			BIT(4)
86 #define HV_SIGNAL_EVENTS			BIT(5)
87 #define HV_CREATE_PORT				BIT(6)
88 #define HV_CONNECT_PORT				BIT(7)
89 #define HV_ACCESS_STATS				BIT(8)
90 #define HV_DEBUGGING				BIT(11)
91 #define HV_CPU_MANAGEMENT			BIT(12)
92 #define HV_ISOLATION				BIT(22)
93 
94 
95 /*
96  * TSC page layout.
97  */
98 struct ms_hyperv_tsc_page {
99 	volatile u32 tsc_sequence;
100 	u32 reserved1;
101 	volatile u64 tsc_scale;
102 	volatile s64 tsc_offset;
103 } __packed;
104 
105 /*
106  * The guest OS needs to register the guest ID with the hypervisor.
107  * The guest ID is a 64 bit entity and the structure of this ID is
108  * specified in the Hyper-V specification:
109  *
110  * msdn.microsoft.com/en-us/library/windows/hardware/ff542653%28v=vs.85%29.aspx
111  *
112  * While the current guideline does not specify how Linux guest ID(s)
113  * need to be generated, our plan is to publish the guidelines for
114  * Linux and other guest operating systems that currently are hosted
115  * on Hyper-V. The implementation here conforms to this yet
116  * unpublished guidelines.
117  *
118  *
119  * Bit(s)
120  * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
121  * 62:56 - Os Type; Linux is 0x100
122  * 55:48 - Distro specific identification
123  * 47:16 - Linux kernel version number
124  * 15:0  - Distro specific identification
125  *
126  *
127  */
128 
129 #define HV_LINUX_VENDOR_ID              0x8100
130 
131 /*
132  * Crash notification flags.
133  */
134 #define HV_CRASH_CTL_CRASH_NOTIFY_MSG		BIT_ULL(62)
135 #define HV_CRASH_CTL_CRASH_NOTIFY		BIT_ULL(63)
136 
137 /* Declare the various hypercall operations. */
138 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE	0x0002
139 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST	0x0003
140 #define HVCALL_NOTIFY_LONG_SPIN_WAIT		0x0008
141 #define HVCALL_SEND_IPI				0x000b
142 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX	0x0013
143 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX	0x0014
144 #define HVCALL_SEND_IPI_EX			0x0015
145 #define HVCALL_GET_VP_REGISTERS			0x0050
146 #define HVCALL_SET_VP_REGISTERS			0x0051
147 #define HVCALL_POST_MESSAGE			0x005c
148 #define HVCALL_SIGNAL_EVENT			0x005d
149 #define HVCALL_POST_DEBUG_DATA			0x0069
150 #define HVCALL_RETRIEVE_DEBUG_DATA		0x006a
151 #define HVCALL_RESET_DEBUG_SESSION		0x006b
152 #define HVCALL_RETARGET_INTERRUPT		0x007e
153 #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af
154 #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST 0x00b0
155 
156 #define HV_FLUSH_ALL_PROCESSORS			BIT(0)
157 #define HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES	BIT(1)
158 #define HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY	BIT(2)
159 #define HV_FLUSH_USE_EXTENDED_RANGE_FORMAT	BIT(3)
160 
161 enum HV_GENERIC_SET_FORMAT {
162 	HV_GENERIC_SET_SPARSE_4K,
163 	HV_GENERIC_SET_ALL,
164 };
165 
166 #define HV_PARTITION_ID_SELF		((u64)-1)
167 #define HV_VP_INDEX_SELF		((u32)-2)
168 
169 #define HV_HYPERCALL_RESULT_MASK	GENMASK_ULL(15, 0)
170 #define HV_HYPERCALL_FAST_BIT		BIT(16)
171 #define HV_HYPERCALL_VARHEAD_OFFSET	17
172 #define HV_HYPERCALL_REP_COMP_OFFSET	32
173 #define HV_HYPERCALL_REP_COMP_1		BIT_ULL(32)
174 #define HV_HYPERCALL_REP_COMP_MASK	GENMASK_ULL(43, 32)
175 #define HV_HYPERCALL_REP_START_OFFSET	48
176 #define HV_HYPERCALL_REP_START_MASK	GENMASK_ULL(59, 48)
177 
178 /* hypercall status code */
179 #define HV_STATUS_SUCCESS			0
180 #define HV_STATUS_INVALID_HYPERCALL_CODE	2
181 #define HV_STATUS_INVALID_HYPERCALL_INPUT	3
182 #define HV_STATUS_INVALID_ALIGNMENT		4
183 #define HV_STATUS_INVALID_PARAMETER		5
184 #define HV_STATUS_OPERATION_DENIED		8
185 #define HV_STATUS_INSUFFICIENT_MEMORY		11
186 #define HV_STATUS_INVALID_PORT_ID		17
187 #define HV_STATUS_INVALID_CONNECTION_ID		18
188 #define HV_STATUS_INSUFFICIENT_BUFFERS		19
189 
190 /*
191  * The Hyper-V TimeRefCount register and the TSC
192  * page provide a guest VM clock with 100ns tick rate
193  */
194 #define HV_CLOCK_HZ (NSEC_PER_SEC/100)
195 
196 /* Define the number of synthetic interrupt sources. */
197 #define HV_SYNIC_SINT_COUNT		(16)
198 /* Define the expected SynIC version. */
199 #define HV_SYNIC_VERSION_1		(0x1)
200 /* Valid SynIC vectors are 16-255. */
201 #define HV_SYNIC_FIRST_VALID_VECTOR	(16)
202 
203 #define HV_SYNIC_CONTROL_ENABLE		(1ULL << 0)
204 #define HV_SYNIC_SIMP_ENABLE		(1ULL << 0)
205 #define HV_SYNIC_SIEFP_ENABLE		(1ULL << 0)
206 #define HV_SYNIC_SINT_MASKED		(1ULL << 16)
207 #define HV_SYNIC_SINT_AUTO_EOI		(1ULL << 17)
208 #define HV_SYNIC_SINT_VECTOR_MASK	(0xFF)
209 
210 #define HV_SYNIC_STIMER_COUNT		(4)
211 
212 /* Define synthetic interrupt controller message constants. */
213 #define HV_MESSAGE_SIZE			(256)
214 #define HV_MESSAGE_PAYLOAD_BYTE_COUNT	(240)
215 #define HV_MESSAGE_PAYLOAD_QWORD_COUNT	(30)
216 
217 /* Define synthetic interrupt controller message flags. */
218 union hv_message_flags {
219 	__u8 asu8;
220 	struct {
221 		__u8 msg_pending:1;
222 		__u8 reserved:7;
223 	} __packed;
224 };
225 
226 /* Define port identifier type. */
227 union hv_port_id {
228 	__u32 asu32;
229 	struct {
230 		__u32 id:24;
231 		__u32 reserved:8;
232 	} __packed u;
233 };
234 
235 /* Define synthetic interrupt controller message header. */
236 struct hv_message_header {
237 	__u32 message_type;
238 	__u8 payload_size;
239 	union hv_message_flags message_flags;
240 	__u8 reserved[2];
241 	union {
242 		__u64 sender;
243 		union hv_port_id port;
244 	};
245 } __packed;
246 
247 /* Define synthetic interrupt controller message format. */
248 struct hv_message {
249 	struct hv_message_header header;
250 	union {
251 		__u64 payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
252 	} u;
253 } __packed;
254 
255 /* Define the synthetic interrupt message page layout. */
256 struct hv_message_page {
257 	struct hv_message sint_message[HV_SYNIC_SINT_COUNT];
258 } __packed;
259 
260 /* Define timer message payload structure. */
261 struct hv_timer_message_payload {
262 	__u32 timer_index;
263 	__u32 reserved;
264 	__u64 expiration_time;	/* When the timer expired */
265 	__u64 delivery_time;	/* When the message was delivered */
266 } __packed;
267 
268 
269 /* Define synthetic interrupt controller flag constants. */
270 #define HV_EVENT_FLAGS_COUNT		(256 * 8)
271 #define HV_EVENT_FLAGS_LONG_COUNT	(256 / sizeof(unsigned long))
272 
273 /*
274  * Synthetic timer configuration.
275  */
276 union hv_stimer_config {
277 	u64 as_uint64;
278 	struct {
279 		u64 enable:1;
280 		u64 periodic:1;
281 		u64 lazy:1;
282 		u64 auto_enable:1;
283 		u64 apic_vector:8;
284 		u64 direct_mode:1;
285 		u64 reserved_z0:3;
286 		u64 sintx:4;
287 		u64 reserved_z1:44;
288 	} __packed;
289 };
290 
291 
292 /* Define the synthetic interrupt controller event flags format. */
293 union hv_synic_event_flags {
294 	unsigned long flags[HV_EVENT_FLAGS_LONG_COUNT];
295 };
296 
297 /* Define SynIC control register. */
298 union hv_synic_scontrol {
299 	u64 as_uint64;
300 	struct {
301 		u64 enable:1;
302 		u64 reserved:63;
303 	} __packed;
304 };
305 
306 /* Define synthetic interrupt source. */
307 union hv_synic_sint {
308 	u64 as_uint64;
309 	struct {
310 		u64 vector:8;
311 		u64 reserved1:8;
312 		u64 masked:1;
313 		u64 auto_eoi:1;
314 		u64 polling:1;
315 		u64 reserved2:45;
316 	} __packed;
317 };
318 
319 /* Define the format of the SIMP register */
320 union hv_synic_simp {
321 	u64 as_uint64;
322 	struct {
323 		u64 simp_enabled:1;
324 		u64 preserved:11;
325 		u64 base_simp_gpa:52;
326 	} __packed;
327 };
328 
329 /* Define the format of the SIEFP register */
330 union hv_synic_siefp {
331 	u64 as_uint64;
332 	struct {
333 		u64 siefp_enabled:1;
334 		u64 preserved:11;
335 		u64 base_siefp_gpa:52;
336 	} __packed;
337 };
338 
339 struct hv_vpset {
340 	u64 format;
341 	u64 valid_bank_mask;
342 	u64 bank_contents[];
343 } __packed;
344 
345 /* HvCallSendSyntheticClusterIpi hypercall */
346 struct hv_send_ipi {
347 	u32 vector;
348 	u32 reserved;
349 	u64 cpu_mask;
350 } __packed;
351 
352 /* HvCallSendSyntheticClusterIpiEx hypercall */
353 struct hv_send_ipi_ex {
354 	u32 vector;
355 	u32 reserved;
356 	struct hv_vpset vp_set;
357 } __packed;
358 
359 /* HvFlushGuestPhysicalAddressSpace hypercalls */
360 struct hv_guest_mapping_flush {
361 	u64 address_space;
362 	u64 flags;
363 } __packed;
364 
365 /*
366  *  HV_MAX_FLUSH_PAGES = "additional_pages" + 1. It's limited
367  *  by the bitwidth of "additional_pages" in union hv_gpa_page_range.
368  */
369 #define HV_MAX_FLUSH_PAGES (2048)
370 
371 /* HvFlushGuestPhysicalAddressList hypercall */
372 union hv_gpa_page_range {
373 	u64 address_space;
374 	struct {
375 		u64 additional_pages:11;
376 		u64 largepage:1;
377 		u64 basepfn:52;
378 	} page;
379 };
380 
381 /*
382  * All input flush parameters should be in single page. The max flush
383  * count is equal with how many entries of union hv_gpa_page_range can
384  * be populated into the input parameter page.
385  */
386 #define HV_MAX_FLUSH_REP_COUNT ((HV_HYP_PAGE_SIZE - 2 * sizeof(u64)) /	\
387 				sizeof(union hv_gpa_page_range))
388 
389 struct hv_guest_mapping_flush_list {
390 	u64 address_space;
391 	u64 flags;
392 	union hv_gpa_page_range gpa_list[HV_MAX_FLUSH_REP_COUNT];
393 };
394 
395 /* HvFlushVirtualAddressSpace, HvFlushVirtualAddressList hypercalls */
396 struct hv_tlb_flush {
397 	u64 address_space;
398 	u64 flags;
399 	u64 processor_mask;
400 	u64 gva_list[];
401 } __packed;
402 
403 /* HvFlushVirtualAddressSpaceEx, HvFlushVirtualAddressListEx hypercalls */
404 struct hv_tlb_flush_ex {
405 	u64 address_space;
406 	u64 flags;
407 	struct hv_vpset hv_vp_set;
408 	u64 gva_list[];
409 } __packed;
410 
411 /* HvRetargetDeviceInterrupt hypercall */
412 union hv_msi_entry {
413 	u64 as_uint64;
414 	struct {
415 		u32 address;
416 		u32 data;
417 	} __packed;
418 };
419 
420 struct hv_interrupt_entry {
421 	u32 source;			/* 1 for MSI(-X) */
422 	u32 reserved1;
423 	union hv_msi_entry msi_entry;
424 } __packed;
425 
426 /*
427  * flags for hv_device_interrupt_target.flags
428  */
429 #define HV_DEVICE_INTERRUPT_TARGET_MULTICAST		1
430 #define HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET	2
431 
432 struct hv_device_interrupt_target {
433 	u32 vector;
434 	u32 flags;
435 	union {
436 		u64 vp_mask;
437 		struct hv_vpset vp_set;
438 	};
439 } __packed;
440 
441 struct hv_retarget_device_interrupt {
442 	u64 partition_id;		/* use "self" */
443 	u64 device_id;
444 	struct hv_interrupt_entry int_entry;
445 	u64 reserved2;
446 	struct hv_device_interrupt_target int_target;
447 } __packed __aligned(8);
448 
449 
450 /* HvGetVpRegisters hypercall input with variable size reg name list*/
451 struct hv_get_vp_registers_input {
452 	struct {
453 		u64 partitionid;
454 		u32 vpindex;
455 		u8  inputvtl;
456 		u8  padding[3];
457 	} header;
458 	struct input {
459 		u32 name0;
460 		u32 name1;
461 	} element[];
462 } __packed;
463 
464 
465 /* HvGetVpRegisters returns an array of these output elements */
466 struct hv_get_vp_registers_output {
467 	union {
468 		struct {
469 			u32 a;
470 			u32 b;
471 			u32 c;
472 			u32 d;
473 		} as32 __packed;
474 		struct {
475 			u64 low;
476 			u64 high;
477 		} as64 __packed;
478 	};
479 };
480 
481 /* HvSetVpRegisters hypercall with variable size reg name/value list*/
482 struct hv_set_vp_registers_input {
483 	struct {
484 		u64 partitionid;
485 		u32 vpindex;
486 		u8  inputvtl;
487 		u8  padding[3];
488 	} header;
489 	struct {
490 		u32 name;
491 		u32 padding1;
492 		u64 padding2;
493 		u64 valuelow;
494 		u64 valuehigh;
495 	} element[];
496 } __packed;
497 
498 #endif
499