xref: /linux/arch/x86/include/asm/mshyperv.h (revision 7415aea6072bab15969b6c3c5b2a193d88095326)
1e08cae41SH. Peter Anvin #ifndef _ASM_X86_MSHYPER_H
2e08cae41SH. Peter Anvin #define _ASM_X86_MSHYPER_H
3a2a47c6cSKy Srinivasan 
4e08cae41SH. Peter Anvin #include <linux/types.h>
526fcd952SThomas Gleixner #include <linux/atomic.h>
6806c8927SVitaly Kuznetsov #include <linux/nmi.h>
7fc53662fSVitaly Kuznetsov #include <asm/io.h>
8e08cae41SH. Peter Anvin #include <asm/hyperv.h>
9e08cae41SH. Peter Anvin 
108de8af7eSK. Y. Srinivasan /*
118de8af7eSK. Y. Srinivasan  * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
128de8af7eSK. Y. Srinivasan  * is set by CPUID(HVCPUID_VERSION_FEATURES).
138de8af7eSK. Y. Srinivasan  */
148de8af7eSK. Y. Srinivasan enum hv_cpuid_function {
158de8af7eSK. Y. Srinivasan 	HVCPUID_VERSION_FEATURES		= 0x00000001,
168de8af7eSK. Y. Srinivasan 	HVCPUID_VENDOR_MAXFUNCTION		= 0x40000000,
178de8af7eSK. Y. Srinivasan 	HVCPUID_INTERFACE			= 0x40000001,
188de8af7eSK. Y. Srinivasan 
198de8af7eSK. Y. Srinivasan 	/*
208de8af7eSK. Y. Srinivasan 	 * The remaining functions depend on the value of
218de8af7eSK. Y. Srinivasan 	 * HVCPUID_INTERFACE
228de8af7eSK. Y. Srinivasan 	 */
238de8af7eSK. Y. Srinivasan 	HVCPUID_VERSION				= 0x40000002,
248de8af7eSK. Y. Srinivasan 	HVCPUID_FEATURES			= 0x40000003,
258de8af7eSK. Y. Srinivasan 	HVCPUID_ENLIGHTENMENT_INFO		= 0x40000004,
268de8af7eSK. Y. Srinivasan 	HVCPUID_IMPLEMENTATION_LIMITS		= 0x40000005,
278de8af7eSK. Y. Srinivasan };
288de8af7eSK. Y. Srinivasan 
29e08cae41SH. Peter Anvin struct ms_hyperv_info {
30e08cae41SH. Peter Anvin 	u32 features;
31cc2dd402SDenis V. Lunev 	u32 misc_features;
32e08cae41SH. Peter Anvin 	u32 hints;
33e08cae41SH. Peter Anvin };
34e08cae41SH. Peter Anvin 
35e08cae41SH. Peter Anvin extern struct ms_hyperv_info ms_hyperv;
36a2a47c6cSKy Srinivasan 
373f646ed7SK. Y. Srinivasan /*
383f646ed7SK. Y. Srinivasan  * Declare the MSR used to setup pages used to communicate with the hypervisor.
393f646ed7SK. Y. Srinivasan  */
403f646ed7SK. Y. Srinivasan union hv_x64_msr_hypercall_contents {
413f646ed7SK. Y. Srinivasan 	u64 as_uint64;
423f646ed7SK. Y. Srinivasan 	struct {
433f646ed7SK. Y. Srinivasan 		u64 enable:1;
443f646ed7SK. Y. Srinivasan 		u64 reserved:11;
453f646ed7SK. Y. Srinivasan 		u64 guest_physical_address:52;
463f646ed7SK. Y. Srinivasan 	};
473f646ed7SK. Y. Srinivasan };
483f646ed7SK. Y. Srinivasan 
49352c9624SK. Y. Srinivasan /*
5063ed4e0cSK. Y. Srinivasan  * TSC page layout.
5163ed4e0cSK. Y. Srinivasan  */
5263ed4e0cSK. Y. Srinivasan 
5363ed4e0cSK. Y. Srinivasan struct ms_hyperv_tsc_page {
5463ed4e0cSK. Y. Srinivasan 	volatile u32 tsc_sequence;
5563ed4e0cSK. Y. Srinivasan 	u32 reserved1;
5663ed4e0cSK. Y. Srinivasan 	volatile u64 tsc_scale;
5763ed4e0cSK. Y. Srinivasan 	volatile s64 tsc_offset;
5863ed4e0cSK. Y. Srinivasan 	u64 reserved2[509];
5963ed4e0cSK. Y. Srinivasan };
6063ed4e0cSK. Y. Srinivasan 
6163ed4e0cSK. Y. Srinivasan /*
62352c9624SK. Y. Srinivasan  * The guest OS needs to register the guest ID with the hypervisor.
63352c9624SK. Y. Srinivasan  * The guest ID is a 64 bit entity and the structure of this ID is
64352c9624SK. Y. Srinivasan  * specified in the Hyper-V specification:
65352c9624SK. Y. Srinivasan  *
66352c9624SK. Y. Srinivasan  * msdn.microsoft.com/en-us/library/windows/hardware/ff542653%28v=vs.85%29.aspx
67352c9624SK. Y. Srinivasan  *
68352c9624SK. Y. Srinivasan  * While the current guideline does not specify how Linux guest ID(s)
69352c9624SK. Y. Srinivasan  * need to be generated, our plan is to publish the guidelines for
70352c9624SK. Y. Srinivasan  * Linux and other guest operating systems that currently are hosted
71352c9624SK. Y. Srinivasan  * on Hyper-V. The implementation here conforms to this yet
72352c9624SK. Y. Srinivasan  * unpublished guidelines.
73352c9624SK. Y. Srinivasan  *
74352c9624SK. Y. Srinivasan  *
75352c9624SK. Y. Srinivasan  * Bit(s)
76352c9624SK. Y. Srinivasan  * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
77352c9624SK. Y. Srinivasan  * 62:56 - Os Type; Linux is 0x100
78352c9624SK. Y. Srinivasan  * 55:48 - Distro specific identification
79352c9624SK. Y. Srinivasan  * 47:16 - Linux kernel version number
80352c9624SK. Y. Srinivasan  * 15:0  - Distro specific identification
81352c9624SK. Y. Srinivasan  *
82352c9624SK. Y. Srinivasan  *
83352c9624SK. Y. Srinivasan  */
84352c9624SK. Y. Srinivasan 
859b06e101SK. Y. Srinivasan #define HV_LINUX_VENDOR_ID              0x8100
86352c9624SK. Y. Srinivasan 
87352c9624SK. Y. Srinivasan /*
88352c9624SK. Y. Srinivasan  * Generate the guest ID based on the guideline described above.
89352c9624SK. Y. Srinivasan  */
90352c9624SK. Y. Srinivasan 
91352c9624SK. Y. Srinivasan static inline  __u64 generate_guest_id(__u64 d_info1, __u64 kernel_version,
92352c9624SK. Y. Srinivasan 				       __u64 d_info2)
93352c9624SK. Y. Srinivasan {
94352c9624SK. Y. Srinivasan 	__u64 guest_id = 0;
95352c9624SK. Y. Srinivasan 
969b06e101SK. Y. Srinivasan 	guest_id = (((__u64)HV_LINUX_VENDOR_ID) << 48);
97352c9624SK. Y. Srinivasan 	guest_id |= (d_info1 << 48);
98352c9624SK. Y. Srinivasan 	guest_id |= (kernel_version << 16);
99352c9624SK. Y. Srinivasan 	guest_id |= d_info2;
100352c9624SK. Y. Srinivasan 
101352c9624SK. Y. Srinivasan 	return guest_id;
102352c9624SK. Y. Srinivasan }
103352c9624SK. Y. Srinivasan 
104e810e48cSK. Y. Srinivasan 
105e810e48cSK. Y. Srinivasan /* Free the message slot and signal end-of-message if required */
106e810e48cSK. Y. Srinivasan static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
107e810e48cSK. Y. Srinivasan {
108e810e48cSK. Y. Srinivasan 	/*
109e810e48cSK. Y. Srinivasan 	 * On crash we're reading some other CPU's message page and we need
110e810e48cSK. Y. Srinivasan 	 * to be careful: this other CPU may already had cleared the header
111e810e48cSK. Y. Srinivasan 	 * and the host may already had delivered some other message there.
112e810e48cSK. Y. Srinivasan 	 * In case we blindly write msg->header.message_type we're going
113e810e48cSK. Y. Srinivasan 	 * to lose it. We can still lose a message of the same type but
114e810e48cSK. Y. Srinivasan 	 * we count on the fact that there can only be one
115e810e48cSK. Y. Srinivasan 	 * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages
116e810e48cSK. Y. Srinivasan 	 * on crash.
117e810e48cSK. Y. Srinivasan 	 */
118e810e48cSK. Y. Srinivasan 	if (cmpxchg(&msg->header.message_type, old_msg_type,
119e810e48cSK. Y. Srinivasan 		    HVMSG_NONE) != old_msg_type)
120e810e48cSK. Y. Srinivasan 		return;
121e810e48cSK. Y. Srinivasan 
122e810e48cSK. Y. Srinivasan 	/*
123e810e48cSK. Y. Srinivasan 	 * Make sure the write to MessageType (ie set to
124e810e48cSK. Y. Srinivasan 	 * HVMSG_NONE) happens before we read the
125e810e48cSK. Y. Srinivasan 	 * MessagePending and EOMing. Otherwise, the EOMing
126e810e48cSK. Y. Srinivasan 	 * will not deliver any more messages since there is
127e810e48cSK. Y. Srinivasan 	 * no empty slot
128e810e48cSK. Y. Srinivasan 	 */
129e810e48cSK. Y. Srinivasan 	mb();
130e810e48cSK. Y. Srinivasan 
131e810e48cSK. Y. Srinivasan 	if (msg->header.message_flags.msg_pending) {
132e810e48cSK. Y. Srinivasan 		/*
133e810e48cSK. Y. Srinivasan 		 * This will cause message queue rescan to
134e810e48cSK. Y. Srinivasan 		 * possibly deliver another msg from the
135e810e48cSK. Y. Srinivasan 		 * hypervisor
136e810e48cSK. Y. Srinivasan 		 */
137e810e48cSK. Y. Srinivasan 		wrmsrl(HV_X64_MSR_EOM, 0);
138e810e48cSK. Y. Srinivasan 	}
139e810e48cSK. Y. Srinivasan }
140e810e48cSK. Y. Srinivasan 
141d5116b40SK. Y. Srinivasan #define hv_init_timer(timer, tick) wrmsrl(timer, tick)
142d5116b40SK. Y. Srinivasan #define hv_init_timer_config(config, val) wrmsrl(config, val)
143d5116b40SK. Y. Srinivasan 
144155e4a2fSK. Y. Srinivasan #define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val)
145155e4a2fSK. Y. Srinivasan #define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val)
146155e4a2fSK. Y. Srinivasan 
1478e307bf8SK. Y. Srinivasan #define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val)
1488e307bf8SK. Y. Srinivasan #define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val)
1498e307bf8SK. Y. Srinivasan 
15006d1d98aSK. Y. Srinivasan #define hv_get_synic_state(val) rdmsrl(HV_X64_MSR_SCONTROL, val)
15106d1d98aSK. Y. Srinivasan #define hv_set_synic_state(val) wrmsrl(HV_X64_MSR_SCONTROL, val)
15206d1d98aSK. Y. Srinivasan 
1537297ff0cSK. Y. Srinivasan #define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index)
1547297ff0cSK. Y. Srinivasan 
15537e11d5cSK. Y. Srinivasan #define hv_get_synint_state(int_num, val) rdmsrl(int_num, val)
15637e11d5cSK. Y. Srinivasan #define hv_set_synint_state(int_num, val) wrmsrl(int_num, val)
15737e11d5cSK. Y. Srinivasan 
158bc2b0331SK. Y. Srinivasan void hyperv_callback_vector(void);
159cf910e83SSeiji Aguchi #ifdef CONFIG_TRACING
160cf910e83SSeiji Aguchi #define trace_hyperv_callback_vector hyperv_callback_vector
161cf910e83SSeiji Aguchi #endif
162bc2b0331SK. Y. Srinivasan void hyperv_vector_handler(struct pt_regs *regs);
16376d388cdSThomas Gleixner void hv_setup_vmbus_irq(void (*handler)(void));
16476d388cdSThomas Gleixner void hv_remove_vmbus_irq(void);
165bc2b0331SK. Y. Srinivasan 
1662517281dSVitaly Kuznetsov void hv_setup_kexec_handler(void (*handler)(void));
1672517281dSVitaly Kuznetsov void hv_remove_kexec_handler(void);
168b4370df2SVitaly Kuznetsov void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs));
169b4370df2SVitaly Kuznetsov void hv_remove_crash_handler(void);
1708730046cSK. Y. Srinivasan 
1718730046cSK. Y. Srinivasan #if IS_ENABLED(CONFIG_HYPERV)
172dee863b5SVitaly Kuznetsov extern struct clocksource *hyperv_cs;
173fc53662fSVitaly Kuznetsov extern void *hv_hypercall_pg;
174fc53662fSVitaly Kuznetsov 
175fc53662fSVitaly Kuznetsov static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
176fc53662fSVitaly Kuznetsov {
177fc53662fSVitaly Kuznetsov 	u64 input_address = input ? virt_to_phys(input) : 0;
178fc53662fSVitaly Kuznetsov 	u64 output_address = output ? virt_to_phys(output) : 0;
179fc53662fSVitaly Kuznetsov 	u64 hv_status;
180fc53662fSVitaly Kuznetsov 	register void *__sp asm(_ASM_SP);
181fc53662fSVitaly Kuznetsov 
182fc53662fSVitaly Kuznetsov #ifdef CONFIG_X86_64
183fc53662fSVitaly Kuznetsov 	if (!hv_hypercall_pg)
184fc53662fSVitaly Kuznetsov 		return U64_MAX;
185fc53662fSVitaly Kuznetsov 
186fc53662fSVitaly Kuznetsov 	__asm__ __volatile__("mov %4, %%r8\n"
187fc53662fSVitaly Kuznetsov 			     "call *%5"
188fc53662fSVitaly Kuznetsov 			     : "=a" (hv_status), "+r" (__sp),
189fc53662fSVitaly Kuznetsov 			       "+c" (control), "+d" (input_address)
190fc53662fSVitaly Kuznetsov 			     :  "r" (output_address), "m" (hv_hypercall_pg)
191fc53662fSVitaly Kuznetsov 			     : "cc", "memory", "r8", "r9", "r10", "r11");
192fc53662fSVitaly Kuznetsov #else
193fc53662fSVitaly Kuznetsov 	u32 input_address_hi = upper_32_bits(input_address);
194fc53662fSVitaly Kuznetsov 	u32 input_address_lo = lower_32_bits(input_address);
195fc53662fSVitaly Kuznetsov 	u32 output_address_hi = upper_32_bits(output_address);
196fc53662fSVitaly Kuznetsov 	u32 output_address_lo = lower_32_bits(output_address);
197fc53662fSVitaly Kuznetsov 
198fc53662fSVitaly Kuznetsov 	if (!hv_hypercall_pg)
199fc53662fSVitaly Kuznetsov 		return U64_MAX;
200fc53662fSVitaly Kuznetsov 
201fc53662fSVitaly Kuznetsov 	__asm__ __volatile__("call *%7"
202fc53662fSVitaly Kuznetsov 			     : "=A" (hv_status),
203fc53662fSVitaly Kuznetsov 			       "+c" (input_address_lo), "+r" (__sp)
204fc53662fSVitaly Kuznetsov 			     : "A" (control),
205fc53662fSVitaly Kuznetsov 			       "b" (input_address_hi),
206fc53662fSVitaly Kuznetsov 			       "D"(output_address_hi), "S"(output_address_lo),
207fc53662fSVitaly Kuznetsov 			       "m" (hv_hypercall_pg)
208fc53662fSVitaly Kuznetsov 			     : "cc", "memory");
209fc53662fSVitaly Kuznetsov #endif /* !x86_64 */
210fc53662fSVitaly Kuznetsov 	return hv_status;
211fc53662fSVitaly Kuznetsov }
212dee863b5SVitaly Kuznetsov 
213806c8927SVitaly Kuznetsov #define HV_HYPERCALL_RESULT_MASK	GENMASK_ULL(15, 0)
2146a8edbd0SVitaly Kuznetsov #define HV_HYPERCALL_FAST_BIT		BIT(16)
215806c8927SVitaly Kuznetsov #define HV_HYPERCALL_VARHEAD_OFFSET	17
216806c8927SVitaly Kuznetsov #define HV_HYPERCALL_REP_COMP_OFFSET	32
217806c8927SVitaly Kuznetsov #define HV_HYPERCALL_REP_COMP_MASK	GENMASK_ULL(43, 32)
218806c8927SVitaly Kuznetsov #define HV_HYPERCALL_REP_START_OFFSET	48
219806c8927SVitaly Kuznetsov #define HV_HYPERCALL_REP_START_MASK	GENMASK_ULL(59, 48)
2206a8edbd0SVitaly Kuznetsov 
2216a8edbd0SVitaly Kuznetsov /* Fast hypercall with 8 bytes of input and no output */
2226a8edbd0SVitaly Kuznetsov static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
2236a8edbd0SVitaly Kuznetsov {
2246a8edbd0SVitaly Kuznetsov 	u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
2256a8edbd0SVitaly Kuznetsov 	register void *__sp asm(_ASM_SP);
2266a8edbd0SVitaly Kuznetsov 
2276a8edbd0SVitaly Kuznetsov #ifdef CONFIG_X86_64
2286a8edbd0SVitaly Kuznetsov 	{
2296a8edbd0SVitaly Kuznetsov 		__asm__ __volatile__("call *%4"
2306a8edbd0SVitaly Kuznetsov 				     : "=a" (hv_status), "+r" (__sp),
2316a8edbd0SVitaly Kuznetsov 				       "+c" (control), "+d" (input1)
2326a8edbd0SVitaly Kuznetsov 				     : "m" (hv_hypercall_pg)
2336a8edbd0SVitaly Kuznetsov 				     : "cc", "r8", "r9", "r10", "r11");
2346a8edbd0SVitaly Kuznetsov 	}
2356a8edbd0SVitaly Kuznetsov #else
2366a8edbd0SVitaly Kuznetsov 	{
2376a8edbd0SVitaly Kuznetsov 		u32 input1_hi = upper_32_bits(input1);
2386a8edbd0SVitaly Kuznetsov 		u32 input1_lo = lower_32_bits(input1);
2396a8edbd0SVitaly Kuznetsov 
2406a8edbd0SVitaly Kuznetsov 		__asm__ __volatile__ ("call *%5"
2416a8edbd0SVitaly Kuznetsov 				      : "=A"(hv_status),
2426a8edbd0SVitaly Kuznetsov 					"+c"(input1_lo),
2436a8edbd0SVitaly Kuznetsov 					"+r"(__sp)
2446a8edbd0SVitaly Kuznetsov 				      :	"A" (control),
2456a8edbd0SVitaly Kuznetsov 					"b" (input1_hi),
2466a8edbd0SVitaly Kuznetsov 					"m" (hv_hypercall_pg)
2476a8edbd0SVitaly Kuznetsov 				      : "cc", "edi", "esi");
2486a8edbd0SVitaly Kuznetsov 	}
2496a8edbd0SVitaly Kuznetsov #endif
2506a8edbd0SVitaly Kuznetsov 		return hv_status;
2516a8edbd0SVitaly Kuznetsov }
2526a8edbd0SVitaly Kuznetsov 
253806c8927SVitaly Kuznetsov /*
254806c8927SVitaly Kuznetsov  * Rep hypercalls. Callers of this functions are supposed to ensure that
255806c8927SVitaly Kuznetsov  * rep_count and varhead_size comply with Hyper-V hypercall definition.
256806c8927SVitaly Kuznetsov  */
257806c8927SVitaly Kuznetsov static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
258806c8927SVitaly Kuznetsov 				      void *input, void *output)
259806c8927SVitaly Kuznetsov {
260806c8927SVitaly Kuznetsov 	u64 control = code;
261806c8927SVitaly Kuznetsov 	u64 status;
262806c8927SVitaly Kuznetsov 	u16 rep_comp;
263806c8927SVitaly Kuznetsov 
264806c8927SVitaly Kuznetsov 	control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
265806c8927SVitaly Kuznetsov 	control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
266806c8927SVitaly Kuznetsov 
267806c8927SVitaly Kuznetsov 	do {
268806c8927SVitaly Kuznetsov 		status = hv_do_hypercall(control, input, output);
269806c8927SVitaly Kuznetsov 		if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS)
270806c8927SVitaly Kuznetsov 			return status;
271806c8927SVitaly Kuznetsov 
272806c8927SVitaly Kuznetsov 		/* Bits 32-43 of status have 'Reps completed' data. */
273806c8927SVitaly Kuznetsov 		rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >>
274806c8927SVitaly Kuznetsov 			HV_HYPERCALL_REP_COMP_OFFSET;
275806c8927SVitaly Kuznetsov 
276806c8927SVitaly Kuznetsov 		control &= ~HV_HYPERCALL_REP_START_MASK;
277806c8927SVitaly Kuznetsov 		control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
278806c8927SVitaly Kuznetsov 
279806c8927SVitaly Kuznetsov 		touch_nmi_watchdog();
280806c8927SVitaly Kuznetsov 	} while (rep_comp < rep_count);
281806c8927SVitaly Kuznetsov 
282806c8927SVitaly Kuznetsov 	return status;
283806c8927SVitaly Kuznetsov }
284806c8927SVitaly Kuznetsov 
285*7415aea6SVitaly Kuznetsov /*
286*7415aea6SVitaly Kuznetsov  * Hypervisor's notion of virtual processor ID is different from
287*7415aea6SVitaly Kuznetsov  * Linux' notion of CPU ID. This information can only be retrieved
288*7415aea6SVitaly Kuznetsov  * in the context of the calling CPU. Setup a map for easy access
289*7415aea6SVitaly Kuznetsov  * to this information.
290*7415aea6SVitaly Kuznetsov  */
291*7415aea6SVitaly Kuznetsov extern u32 *hv_vp_index;
292*7415aea6SVitaly Kuznetsov 
293*7415aea6SVitaly Kuznetsov /**
294*7415aea6SVitaly Kuznetsov  * hv_cpu_number_to_vp_number() - Map CPU to VP.
295*7415aea6SVitaly Kuznetsov  * @cpu_number: CPU number in Linux terms
296*7415aea6SVitaly Kuznetsov  *
297*7415aea6SVitaly Kuznetsov  * This function returns the mapping between the Linux processor
298*7415aea6SVitaly Kuznetsov  * number and the hypervisor's virtual processor number, useful
299*7415aea6SVitaly Kuznetsov  * in making hypercalls and such that talk about specific
300*7415aea6SVitaly Kuznetsov  * processors.
301*7415aea6SVitaly Kuznetsov  *
302*7415aea6SVitaly Kuznetsov  * Return: Virtual processor number in Hyper-V terms
303*7415aea6SVitaly Kuznetsov  */
304*7415aea6SVitaly Kuznetsov static inline int hv_cpu_number_to_vp_number(int cpu_number)
305*7415aea6SVitaly Kuznetsov {
306*7415aea6SVitaly Kuznetsov 	return hv_vp_index[cpu_number];
307*7415aea6SVitaly Kuznetsov }
308*7415aea6SVitaly Kuznetsov 
3098730046cSK. Y. Srinivasan void hyperv_init(void);
310d058fa7eSK. Y. Srinivasan void hyperv_report_panic(struct pt_regs *regs);
31173638cddSK. Y. Srinivasan bool hv_is_hypercall_page_setup(void);
312d6f3609dSVitaly Kuznetsov void hyperv_cleanup(void);
31379cadff2SVitaly Kuznetsov #else /* CONFIG_HYPERV */
31479cadff2SVitaly Kuznetsov static inline void hyperv_init(void) {}
31579cadff2SVitaly Kuznetsov static inline bool hv_is_hypercall_page_setup(void) { return false; }
31679cadff2SVitaly Kuznetsov static inline void hyperv_cleanup(void) {}
31779cadff2SVitaly Kuznetsov #endif /* CONFIG_HYPERV */
31879cadff2SVitaly Kuznetsov 
319bd2a9adaSVitaly Kuznetsov #ifdef CONFIG_HYPERV_TSCPAGE
320bd2a9adaSVitaly Kuznetsov struct ms_hyperv_tsc_page *hv_get_tsc_page(void);
3210733379bSVitaly Kuznetsov static inline u64 hv_read_tsc_page(const struct ms_hyperv_tsc_page *tsc_pg)
3220733379bSVitaly Kuznetsov {
3230733379bSVitaly Kuznetsov 	u64 scale, offset, cur_tsc;
3240733379bSVitaly Kuznetsov 	u32 sequence;
3250733379bSVitaly Kuznetsov 
3260733379bSVitaly Kuznetsov 	/*
3270733379bSVitaly Kuznetsov 	 * The protocol for reading Hyper-V TSC page is specified in Hypervisor
3280733379bSVitaly Kuznetsov 	 * Top-Level Functional Specification ver. 3.0 and above. To get the
3290733379bSVitaly Kuznetsov 	 * reference time we must do the following:
3300733379bSVitaly Kuznetsov 	 * - READ ReferenceTscSequence
3310733379bSVitaly Kuznetsov 	 *   A special '0' value indicates the time source is unreliable and we
3320733379bSVitaly Kuznetsov 	 *   need to use something else. The currently published specification
3330733379bSVitaly Kuznetsov 	 *   versions (up to 4.0b) contain a mistake and wrongly claim '-1'
3340733379bSVitaly Kuznetsov 	 *   instead of '0' as the special value, see commit c35b82ef0294.
3350733379bSVitaly Kuznetsov 	 * - ReferenceTime =
3360733379bSVitaly Kuznetsov 	 *        ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset
3370733379bSVitaly Kuznetsov 	 * - READ ReferenceTscSequence again. In case its value has changed
3380733379bSVitaly Kuznetsov 	 *   since our first reading we need to discard ReferenceTime and repeat
3390733379bSVitaly Kuznetsov 	 *   the whole sequence as the hypervisor was updating the page in
3400733379bSVitaly Kuznetsov 	 *   between.
3410733379bSVitaly Kuznetsov 	 */
3420733379bSVitaly Kuznetsov 	do {
3430733379bSVitaly Kuznetsov 		sequence = READ_ONCE(tsc_pg->tsc_sequence);
3440733379bSVitaly Kuznetsov 		if (!sequence)
3450733379bSVitaly Kuznetsov 			return U64_MAX;
3460733379bSVitaly Kuznetsov 		/*
3470733379bSVitaly Kuznetsov 		 * Make sure we read sequence before we read other values from
3480733379bSVitaly Kuznetsov 		 * TSC page.
3490733379bSVitaly Kuznetsov 		 */
3500733379bSVitaly Kuznetsov 		smp_rmb();
3510733379bSVitaly Kuznetsov 
3520733379bSVitaly Kuznetsov 		scale = READ_ONCE(tsc_pg->tsc_scale);
3530733379bSVitaly Kuznetsov 		offset = READ_ONCE(tsc_pg->tsc_offset);
3540733379bSVitaly Kuznetsov 		cur_tsc = rdtsc_ordered();
3550733379bSVitaly Kuznetsov 
3560733379bSVitaly Kuznetsov 		/*
3570733379bSVitaly Kuznetsov 		 * Make sure we read sequence after we read all other values
3580733379bSVitaly Kuznetsov 		 * from TSC page.
3590733379bSVitaly Kuznetsov 		 */
3600733379bSVitaly Kuznetsov 		smp_rmb();
3610733379bSVitaly Kuznetsov 
3620733379bSVitaly Kuznetsov 	} while (READ_ONCE(tsc_pg->tsc_sequence) != sequence);
3630733379bSVitaly Kuznetsov 
3640733379bSVitaly Kuznetsov 	return mul_u64_u64_shr(cur_tsc, scale, 64) + offset;
3650733379bSVitaly Kuznetsov }
3660733379bSVitaly Kuznetsov 
367bd2a9adaSVitaly Kuznetsov #else
368bd2a9adaSVitaly Kuznetsov static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
369bd2a9adaSVitaly Kuznetsov {
370bd2a9adaSVitaly Kuznetsov 	return NULL;
371bd2a9adaSVitaly Kuznetsov }
372bd2a9adaSVitaly Kuznetsov #endif
373a2a47c6cSKy Srinivasan #endif
374