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_POWER_MANAGEMENT			BIT(12)
92 
93 
94 /*
95  * TSC page layout.
96  */
97 struct ms_hyperv_tsc_page {
98 	volatile u32 tsc_sequence;
99 	u32 reserved1;
100 	volatile u64 tsc_scale;
101 	volatile s64 tsc_offset;
102 } __packed;
103 
104 /*
105  * The guest OS needs to register the guest ID with the hypervisor.
106  * The guest ID is a 64 bit entity and the structure of this ID is
107  * specified in the Hyper-V specification:
108  *
109  * msdn.microsoft.com/en-us/library/windows/hardware/ff542653%28v=vs.85%29.aspx
110  *
111  * While the current guideline does not specify how Linux guest ID(s)
112  * need to be generated, our plan is to publish the guidelines for
113  * Linux and other guest operating systems that currently are hosted
114  * on Hyper-V. The implementation here conforms to this yet
115  * unpublished guidelines.
116  *
117  *
118  * Bit(s)
119  * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
120  * 62:56 - Os Type; Linux is 0x100
121  * 55:48 - Distro specific identification
122  * 47:16 - Linux kernel version number
123  * 15:0  - Distro specific identification
124  *
125  *
126  */
127 
128 #define HV_LINUX_VENDOR_ID              0x8100
129 
130 /*
131  * Crash notification flags.
132  */
133 #define HV_CRASH_CTL_CRASH_NOTIFY_MSG		BIT_ULL(62)
134 #define HV_CRASH_CTL_CRASH_NOTIFY		BIT_ULL(63)
135 
136 /* Declare the various hypercall operations. */
137 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE	0x0002
138 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST	0x0003
139 #define HVCALL_NOTIFY_LONG_SPIN_WAIT		0x0008
140 #define HVCALL_SEND_IPI				0x000b
141 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE_EX	0x0013
142 #define HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST_EX	0x0014
143 #define HVCALL_SEND_IPI_EX			0x0015
144 #define HVCALL_GET_VP_REGISTERS			0x0050
145 #define HVCALL_SET_VP_REGISTERS			0x0051
146 #define HVCALL_POST_MESSAGE			0x005c
147 #define HVCALL_SIGNAL_EVENT			0x005d
148 #define HVCALL_POST_DEBUG_DATA			0x0069
149 #define HVCALL_RETRIEVE_DEBUG_DATA		0x006a
150 #define HVCALL_RESET_DEBUG_SESSION		0x006b
151 #define HVCALL_RETARGET_INTERRUPT		0x007e
152 #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af
153 #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST 0x00b0
154 
155 #define HV_FLUSH_ALL_PROCESSORS			BIT(0)
156 #define HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES	BIT(1)
157 #define HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY	BIT(2)
158 #define HV_FLUSH_USE_EXTENDED_RANGE_FORMAT	BIT(3)
159 
160 enum HV_GENERIC_SET_FORMAT {
161 	HV_GENERIC_SET_SPARSE_4K,
162 	HV_GENERIC_SET_ALL,
163 };
164 
165 #define HV_PARTITION_ID_SELF		((u64)-1)
166 #define HV_VP_INDEX_SELF		((u32)-2)
167 
168 #define HV_HYPERCALL_RESULT_MASK	GENMASK_ULL(15, 0)
169 #define HV_HYPERCALL_FAST_BIT		BIT(16)
170 #define HV_HYPERCALL_VARHEAD_OFFSET	17
171 #define HV_HYPERCALL_REP_COMP_OFFSET	32
172 #define HV_HYPERCALL_REP_COMP_1		BIT_ULL(32)
173 #define HV_HYPERCALL_REP_COMP_MASK	GENMASK_ULL(43, 32)
174 #define HV_HYPERCALL_REP_START_OFFSET	48
175 #define HV_HYPERCALL_REP_START_MASK	GENMASK_ULL(59, 48)
176 
177 /* hypercall status code */
178 #define HV_STATUS_SUCCESS			0
179 #define HV_STATUS_INVALID_HYPERCALL_CODE	2
180 #define HV_STATUS_INVALID_HYPERCALL_INPUT	3
181 #define HV_STATUS_INVALID_ALIGNMENT		4
182 #define HV_STATUS_INVALID_PARAMETER		5
183 #define HV_STATUS_OPERATION_DENIED		8
184 #define HV_STATUS_INSUFFICIENT_MEMORY		11
185 #define HV_STATUS_INVALID_PORT_ID		17
186 #define HV_STATUS_INVALID_CONNECTION_ID		18
187 #define HV_STATUS_INSUFFICIENT_BUFFERS		19
188 
189 /*
190  * The Hyper-V TimeRefCount register and the TSC
191  * page provide a guest VM clock with 100ns tick rate
192  */
193 #define HV_CLOCK_HZ (NSEC_PER_SEC/100)
194 
195 /* Define the number of synthetic interrupt sources. */
196 #define HV_SYNIC_SINT_COUNT		(16)
197 /* Define the expected SynIC version. */
198 #define HV_SYNIC_VERSION_1		(0x1)
199 /* Valid SynIC vectors are 16-255. */
200 #define HV_SYNIC_FIRST_VALID_VECTOR	(16)
201 
202 #define HV_SYNIC_CONTROL_ENABLE		(1ULL << 0)
203 #define HV_SYNIC_SIMP_ENABLE		(1ULL << 0)
204 #define HV_SYNIC_SIEFP_ENABLE		(1ULL << 0)
205 #define HV_SYNIC_SINT_MASKED		(1ULL << 16)
206 #define HV_SYNIC_SINT_AUTO_EOI		(1ULL << 17)
207 #define HV_SYNIC_SINT_VECTOR_MASK	(0xFF)
208 
209 #define HV_SYNIC_STIMER_COUNT		(4)
210 
211 /* Define synthetic interrupt controller message constants. */
212 #define HV_MESSAGE_SIZE			(256)
213 #define HV_MESSAGE_PAYLOAD_BYTE_COUNT	(240)
214 #define HV_MESSAGE_PAYLOAD_QWORD_COUNT	(30)
215 
216 /* Define synthetic interrupt controller message flags. */
217 union hv_message_flags {
218 	__u8 asu8;
219 	struct {
220 		__u8 msg_pending:1;
221 		__u8 reserved:7;
222 	} __packed;
223 };
224 
225 /* Define port identifier type. */
226 union hv_port_id {
227 	__u32 asu32;
228 	struct {
229 		__u32 id:24;
230 		__u32 reserved:8;
231 	} __packed u;
232 };
233 
234 /* Define synthetic interrupt controller message header. */
235 struct hv_message_header {
236 	__u32 message_type;
237 	__u8 payload_size;
238 	union hv_message_flags message_flags;
239 	__u8 reserved[2];
240 	union {
241 		__u64 sender;
242 		union hv_port_id port;
243 	};
244 } __packed;
245 
246 /* Define synthetic interrupt controller message format. */
247 struct hv_message {
248 	struct hv_message_header header;
249 	union {
250 		__u64 payload[HV_MESSAGE_PAYLOAD_QWORD_COUNT];
251 	} u;
252 } __packed;
253 
254 /* Define the synthetic interrupt message page layout. */
255 struct hv_message_page {
256 	struct hv_message sint_message[HV_SYNIC_SINT_COUNT];
257 } __packed;
258 
259 /* Define timer message payload structure. */
260 struct hv_timer_message_payload {
261 	__u32 timer_index;
262 	__u32 reserved;
263 	__u64 expiration_time;	/* When the timer expired */
264 	__u64 delivery_time;	/* When the message was delivered */
265 } __packed;
266 
267 
268 /* Define synthetic interrupt controller flag constants. */
269 #define HV_EVENT_FLAGS_COUNT		(256 * 8)
270 #define HV_EVENT_FLAGS_LONG_COUNT	(256 / sizeof(unsigned long))
271 
272 /*
273  * Synthetic timer configuration.
274  */
275 union hv_stimer_config {
276 	u64 as_uint64;
277 	struct {
278 		u64 enable:1;
279 		u64 periodic:1;
280 		u64 lazy:1;
281 		u64 auto_enable:1;
282 		u64 apic_vector:8;
283 		u64 direct_mode:1;
284 		u64 reserved_z0:3;
285 		u64 sintx:4;
286 		u64 reserved_z1:44;
287 	} __packed;
288 };
289 
290 
291 /* Define the synthetic interrupt controller event flags format. */
292 union hv_synic_event_flags {
293 	unsigned long flags[HV_EVENT_FLAGS_LONG_COUNT];
294 };
295 
296 /* Define SynIC control register. */
297 union hv_synic_scontrol {
298 	u64 as_uint64;
299 	struct {
300 		u64 enable:1;
301 		u64 reserved:63;
302 	} __packed;
303 };
304 
305 /* Define synthetic interrupt source. */
306 union hv_synic_sint {
307 	u64 as_uint64;
308 	struct {
309 		u64 vector:8;
310 		u64 reserved1:8;
311 		u64 masked:1;
312 		u64 auto_eoi:1;
313 		u64 polling:1;
314 		u64 reserved2:45;
315 	} __packed;
316 };
317 
318 /* Define the format of the SIMP register */
319 union hv_synic_simp {
320 	u64 as_uint64;
321 	struct {
322 		u64 simp_enabled:1;
323 		u64 preserved:11;
324 		u64 base_simp_gpa:52;
325 	} __packed;
326 };
327 
328 /* Define the format of the SIEFP register */
329 union hv_synic_siefp {
330 	u64 as_uint64;
331 	struct {
332 		u64 siefp_enabled:1;
333 		u64 preserved:11;
334 		u64 base_siefp_gpa:52;
335 	} __packed;
336 };
337 
338 struct hv_vpset {
339 	u64 format;
340 	u64 valid_bank_mask;
341 	u64 bank_contents[];
342 } __packed;
343 
344 /* HvCallSendSyntheticClusterIpi hypercall */
345 struct hv_send_ipi {
346 	u32 vector;
347 	u32 reserved;
348 	u64 cpu_mask;
349 } __packed;
350 
351 /* HvCallSendSyntheticClusterIpiEx hypercall */
352 struct hv_send_ipi_ex {
353 	u32 vector;
354 	u32 reserved;
355 	struct hv_vpset vp_set;
356 } __packed;
357 
358 /* HvFlushGuestPhysicalAddressSpace hypercalls */
359 struct hv_guest_mapping_flush {
360 	u64 address_space;
361 	u64 flags;
362 } __packed;
363 
364 /*
365  *  HV_MAX_FLUSH_PAGES = "additional_pages" + 1. It's limited
366  *  by the bitwidth of "additional_pages" in union hv_gpa_page_range.
367  */
368 #define HV_MAX_FLUSH_PAGES (2048)
369 
370 /* HvFlushGuestPhysicalAddressList hypercall */
371 union hv_gpa_page_range {
372 	u64 address_space;
373 	struct {
374 		u64 additional_pages:11;
375 		u64 largepage:1;
376 		u64 basepfn:52;
377 	} page;
378 };
379 
380 /*
381  * All input flush parameters should be in single page. The max flush
382  * count is equal with how many entries of union hv_gpa_page_range can
383  * be populated into the input parameter page.
384  */
385 #define HV_MAX_FLUSH_REP_COUNT ((HV_HYP_PAGE_SIZE - 2 * sizeof(u64)) /	\
386 				sizeof(union hv_gpa_page_range))
387 
388 struct hv_guest_mapping_flush_list {
389 	u64 address_space;
390 	u64 flags;
391 	union hv_gpa_page_range gpa_list[HV_MAX_FLUSH_REP_COUNT];
392 };
393 
394 /* HvFlushVirtualAddressSpace, HvFlushVirtualAddressList hypercalls */
395 struct hv_tlb_flush {
396 	u64 address_space;
397 	u64 flags;
398 	u64 processor_mask;
399 	u64 gva_list[];
400 } __packed;
401 
402 /* HvFlushVirtualAddressSpaceEx, HvFlushVirtualAddressListEx hypercalls */
403 struct hv_tlb_flush_ex {
404 	u64 address_space;
405 	u64 flags;
406 	struct hv_vpset hv_vp_set;
407 	u64 gva_list[];
408 } __packed;
409 
410 /* HvRetargetDeviceInterrupt hypercall */
411 union hv_msi_entry {
412 	u64 as_uint64;
413 	struct {
414 		u32 address;
415 		u32 data;
416 	} __packed;
417 };
418 
419 struct hv_interrupt_entry {
420 	u32 source;			/* 1 for MSI(-X) */
421 	u32 reserved1;
422 	union hv_msi_entry msi_entry;
423 } __packed;
424 
425 /*
426  * flags for hv_device_interrupt_target.flags
427  */
428 #define HV_DEVICE_INTERRUPT_TARGET_MULTICAST		1
429 #define HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET	2
430 
431 struct hv_device_interrupt_target {
432 	u32 vector;
433 	u32 flags;
434 	union {
435 		u64 vp_mask;
436 		struct hv_vpset vp_set;
437 	};
438 } __packed;
439 
440 struct hv_retarget_device_interrupt {
441 	u64 partition_id;		/* use "self" */
442 	u64 device_id;
443 	struct hv_interrupt_entry int_entry;
444 	u64 reserved2;
445 	struct hv_device_interrupt_target int_target;
446 } __packed __aligned(8);
447 
448 
449 /* HvGetVpRegisters hypercall input with variable size reg name list*/
450 struct hv_get_vp_registers_input {
451 	struct {
452 		u64 partitionid;
453 		u32 vpindex;
454 		u8  inputvtl;
455 		u8  padding[3];
456 	} header;
457 	struct input {
458 		u32 name0;
459 		u32 name1;
460 	} element[];
461 } __packed;
462 
463 
464 /* HvGetVpRegisters returns an array of these output elements */
465 struct hv_get_vp_registers_output {
466 	union {
467 		struct {
468 			u32 a;
469 			u32 b;
470 			u32 c;
471 			u32 d;
472 		} as32 __packed;
473 		struct {
474 			u64 low;
475 			u64 high;
476 		} as64 __packed;
477 	};
478 };
479 
480 /* HvSetVpRegisters hypercall with variable size reg name/value list*/
481 struct hv_set_vp_registers_input {
482 	struct {
483 		u64 partitionid;
484 		u32 vpindex;
485 		u8  inputvtl;
486 		u8  padding[3];
487 	} header;
488 	struct {
489 		u32 name;
490 		u32 padding1;
491 		u64 padding2;
492 		u64 valuelow;
493 		u64 valuehigh;
494 	} element[];
495 } __packed;
496 
497 #endif
498