blob: 530f448fddafff3297f0e97d697309f1a2cf15bf [file] [log] [blame]
H. Peter Anvine08cae42010-05-07 16:57:28 -07001#ifndef _ASM_X86_MSHYPER_H
2#define _ASM_X86_MSHYPER_H
Ky Srinivasana2a47c62010-05-06 12:08:41 -07003
H. Peter Anvine08cae42010-05-07 16:57:28 -07004#include <linux/types.h>
Thomas Gleixner26fcd952017-06-23 10:50:38 +02005#include <linux/atomic.h>
Vitaly Kuznetsov806c8922017-08-02 18:09:17 +02006#include <linux/nmi.h>
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +02007#include <asm/io.h>
H. Peter Anvine08cae42010-05-07 16:57:28 -07008#include <asm/hyperv.h>
9
K. Y. Srinivasan8de8af72017-01-19 11:51:47 -070010/*
11 * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
12 * is set by CPUID(HVCPUID_VERSION_FEATURES).
13 */
14enum hv_cpuid_function {
15 HVCPUID_VERSION_FEATURES = 0x00000001,
16 HVCPUID_VENDOR_MAXFUNCTION = 0x40000000,
17 HVCPUID_INTERFACE = 0x40000001,
18
19 /*
20 * The remaining functions depend on the value of
21 * HVCPUID_INTERFACE
22 */
23 HVCPUID_VERSION = 0x40000002,
24 HVCPUID_FEATURES = 0x40000003,
25 HVCPUID_ENLIGHTENMENT_INFO = 0x40000004,
26 HVCPUID_IMPLEMENTATION_LIMITS = 0x40000005,
27};
28
H. Peter Anvine08cae42010-05-07 16:57:28 -070029struct ms_hyperv_info {
30 u32 features;
Denis V. Lunevcc2dd402015-08-01 16:08:20 -070031 u32 misc_features;
H. Peter Anvine08cae42010-05-07 16:57:28 -070032 u32 hints;
Vitaly Kuznetsovdd018592017-06-25 10:06:41 -070033 u32 max_vp_index;
34 u32 max_lp_index;
H. Peter Anvine08cae42010-05-07 16:57:28 -070035};
36
37extern struct ms_hyperv_info ms_hyperv;
Ky Srinivasana2a47c62010-05-06 12:08:41 -070038
K. Y. Srinivasan3f646ed2017-01-18 16:45:00 -070039/*
40 * Declare the MSR used to setup pages used to communicate with the hypervisor.
41 */
42union hv_x64_msr_hypercall_contents {
43 u64 as_uint64;
44 struct {
45 u64 enable:1;
46 u64 reserved:11;
47 u64 guest_physical_address:52;
48 };
49};
50
K. Y. Srinivasan352c9622017-01-18 16:45:01 -070051/*
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070052 * TSC page layout.
53 */
54
55struct ms_hyperv_tsc_page {
56 volatile u32 tsc_sequence;
57 u32 reserved1;
58 volatile u64 tsc_scale;
59 volatile s64 tsc_offset;
60 u64 reserved2[509];
61};
62
63/*
K. Y. Srinivasan352c9622017-01-18 16:45:01 -070064 * The guest OS needs to register the guest ID with the hypervisor.
65 * The guest ID is a 64 bit entity and the structure of this ID is
66 * specified in the Hyper-V specification:
67 *
68 * msdn.microsoft.com/en-us/library/windows/hardware/ff542653%28v=vs.85%29.aspx
69 *
70 * While the current guideline does not specify how Linux guest ID(s)
71 * need to be generated, our plan is to publish the guidelines for
72 * Linux and other guest operating systems that currently are hosted
73 * on Hyper-V. The implementation here conforms to this yet
74 * unpublished guidelines.
75 *
76 *
77 * Bit(s)
78 * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
79 * 62:56 - Os Type; Linux is 0x100
80 * 55:48 - Distro specific identification
81 * 47:16 - Linux kernel version number
82 * 15:0 - Distro specific identification
83 *
84 *
85 */
86
K. Y. Srinivasan9b06e102017-02-04 08:46:23 -070087#define HV_LINUX_VENDOR_ID 0x8100
K. Y. Srinivasan352c9622017-01-18 16:45:01 -070088
89/*
90 * Generate the guest ID based on the guideline described above.
91 */
92
93static inline __u64 generate_guest_id(__u64 d_info1, __u64 kernel_version,
94 __u64 d_info2)
95{
96 __u64 guest_id = 0;
97
K. Y. Srinivasan9b06e102017-02-04 08:46:23 -070098 guest_id = (((__u64)HV_LINUX_VENDOR_ID) << 48);
K. Y. Srinivasan352c9622017-01-18 16:45:01 -070099 guest_id |= (d_info1 << 48);
100 guest_id |= (kernel_version << 16);
101 guest_id |= d_info2;
102
103 return guest_id;
104}
105
K. Y. Srinivasane810e482017-01-19 11:51:50 -0700106
107/* Free the message slot and signal end-of-message if required */
108static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
109{
110 /*
111 * On crash we're reading some other CPU's message page and we need
112 * to be careful: this other CPU may already had cleared the header
113 * and the host may already had delivered some other message there.
114 * In case we blindly write msg->header.message_type we're going
115 * to lose it. We can still lose a message of the same type but
116 * we count on the fact that there can only be one
117 * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages
118 * on crash.
119 */
120 if (cmpxchg(&msg->header.message_type, old_msg_type,
121 HVMSG_NONE) != old_msg_type)
122 return;
123
124 /*
125 * Make sure the write to MessageType (ie set to
126 * HVMSG_NONE) happens before we read the
127 * MessagePending and EOMing. Otherwise, the EOMing
128 * will not deliver any more messages since there is
129 * no empty slot
130 */
131 mb();
132
133 if (msg->header.message_flags.msg_pending) {
134 /*
135 * This will cause message queue rescan to
136 * possibly deliver another msg from the
137 * hypervisor
138 */
139 wrmsrl(HV_X64_MSR_EOM, 0);
140 }
141}
142
K. Y. Srinivasand5116b42017-01-19 11:51:51 -0700143#define hv_init_timer(timer, tick) wrmsrl(timer, tick)
144#define hv_init_timer_config(config, val) wrmsrl(config, val)
145
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700146#define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val)
147#define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val)
148
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700149#define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val)
150#define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val)
151
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700152#define hv_get_synic_state(val) rdmsrl(HV_X64_MSR_SCONTROL, val)
153#define hv_set_synic_state(val) wrmsrl(HV_X64_MSR_SCONTROL, val)
154
K. Y. Srinivasan7297ff02017-01-19 11:51:57 -0700155#define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index)
156
K. Y. Srinivasan37e11d52017-01-19 11:51:58 -0700157#define hv_get_synint_state(int_num, val) rdmsrl(int_num, val)
158#define hv_set_synint_state(int_num, val) wrmsrl(int_num, val)
159
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -0800160void hyperv_callback_vector(void);
Seiji Aguchicf910e82013-06-20 11:46:53 -0400161#ifdef CONFIG_TRACING
162#define trace_hyperv_callback_vector hyperv_callback_vector
163#endif
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -0800164void hyperv_vector_handler(struct pt_regs *regs);
Thomas Gleixner76d388c2014-03-05 13:42:14 +0100165void hv_setup_vmbus_irq(void (*handler)(void));
166void hv_remove_vmbus_irq(void);
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -0800167
Vitaly Kuznetsov25172812015-08-01 16:08:07 -0700168void hv_setup_kexec_handler(void (*handler)(void));
169void hv_remove_kexec_handler(void);
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -0700170void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs));
171void hv_remove_crash_handler(void);
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700172
173#if IS_ENABLED(CONFIG_HYPERV)
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -0700174extern struct clocksource *hyperv_cs;
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200175extern void *hv_hypercall_pg;
176
177static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
178{
179 u64 input_address = input ? virt_to_phys(input) : 0;
180 u64 output_address = output ? virt_to_phys(output) : 0;
181 u64 hv_status;
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200182
183#ifdef CONFIG_X86_64
184 if (!hv_hypercall_pg)
185 return U64_MAX;
186
187 __asm__ __volatile__("mov %4, %%r8\n"
188 "call *%5"
Josh Poimboeuff5caf622017-09-20 16:24:33 -0500189 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200190 "+c" (control), "+d" (input_address)
191 : "r" (output_address), "m" (hv_hypercall_pg)
192 : "cc", "memory", "r8", "r9", "r10", "r11");
193#else
194 u32 input_address_hi = upper_32_bits(input_address);
195 u32 input_address_lo = lower_32_bits(input_address);
196 u32 output_address_hi = upper_32_bits(output_address);
197 u32 output_address_lo = lower_32_bits(output_address);
198
199 if (!hv_hypercall_pg)
200 return U64_MAX;
201
202 __asm__ __volatile__("call *%7"
203 : "=A" (hv_status),
Josh Poimboeuff5caf622017-09-20 16:24:33 -0500204 "+c" (input_address_lo), ASM_CALL_CONSTRAINT
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200205 : "A" (control),
206 "b" (input_address_hi),
207 "D"(output_address_hi), "S"(output_address_lo),
208 "m" (hv_hypercall_pg)
209 : "cc", "memory");
210#endif /* !x86_64 */
211 return hv_status;
212}
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -0700213
Vitaly Kuznetsov806c8922017-08-02 18:09:17 +0200214#define HV_HYPERCALL_RESULT_MASK GENMASK_ULL(15, 0)
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200215#define HV_HYPERCALL_FAST_BIT BIT(16)
Vitaly Kuznetsov806c8922017-08-02 18:09:17 +0200216#define HV_HYPERCALL_VARHEAD_OFFSET 17
217#define HV_HYPERCALL_REP_COMP_OFFSET 32
218#define HV_HYPERCALL_REP_COMP_MASK GENMASK_ULL(43, 32)
219#define HV_HYPERCALL_REP_START_OFFSET 48
220#define HV_HYPERCALL_REP_START_MASK GENMASK_ULL(59, 48)
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200221
222/* Fast hypercall with 8 bytes of input and no output */
223static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
224{
225 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200226
227#ifdef CONFIG_X86_64
228 {
229 __asm__ __volatile__("call *%4"
Josh Poimboeuff5caf622017-09-20 16:24:33 -0500230 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200231 "+c" (control), "+d" (input1)
232 : "m" (hv_hypercall_pg)
233 : "cc", "r8", "r9", "r10", "r11");
234 }
235#else
236 {
237 u32 input1_hi = upper_32_bits(input1);
238 u32 input1_lo = lower_32_bits(input1);
239
240 __asm__ __volatile__ ("call *%5"
241 : "=A"(hv_status),
242 "+c"(input1_lo),
Josh Poimboeuff5caf622017-09-20 16:24:33 -0500243 ASM_CALL_CONSTRAINT
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200244 : "A" (control),
245 "b" (input1_hi),
246 "m" (hv_hypercall_pg)
247 : "cc", "edi", "esi");
248 }
249#endif
250 return hv_status;
251}
252
Vitaly Kuznetsov806c8922017-08-02 18:09:17 +0200253/*
254 * Rep hypercalls. Callers of this functions are supposed to ensure that
255 * rep_count and varhead_size comply with Hyper-V hypercall definition.
256 */
257static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
258 void *input, void *output)
259{
260 u64 control = code;
261 u64 status;
262 u16 rep_comp;
263
264 control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
265 control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
266
267 do {
268 status = hv_do_hypercall(control, input, output);
269 if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS)
270 return status;
271
272 /* Bits 32-43 of status have 'Reps completed' data. */
273 rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >>
274 HV_HYPERCALL_REP_COMP_OFFSET;
275
276 control &= ~HV_HYPERCALL_REP_START_MASK;
277 control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
278
279 touch_nmi_watchdog();
280 } while (rep_comp < rep_count);
281
282 return status;
283}
284
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200285/*
286 * Hypervisor's notion of virtual processor ID is different from
287 * Linux' notion of CPU ID. This information can only be retrieved
288 * in the context of the calling CPU. Setup a map for easy access
289 * to this information.
290 */
291extern u32 *hv_vp_index;
Vitaly Kuznetsova3b74242017-10-06 17:48:54 +0200292extern u32 hv_max_vp_index;
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200293
294/**
295 * hv_cpu_number_to_vp_number() - Map CPU to VP.
296 * @cpu_number: CPU number in Linux terms
297 *
298 * This function returns the mapping between the Linux processor
299 * number and the hypervisor's virtual processor number, useful
300 * in making hypercalls and such that talk about specific
301 * processors.
302 *
303 * Return: Virtual processor number in Hyper-V terms
304 */
305static inline int hv_cpu_number_to_vp_number(int cpu_number)
306{
307 return hv_vp_index[cpu_number];
308}
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700309
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700310void hyperv_init(void);
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200311void hyperv_setup_mmu_ops(void);
312void hyper_alloc_mmu(void);
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700313void hyperv_report_panic(struct pt_regs *regs);
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700314bool hv_is_hypercall_page_setup(void);
Ky Srinivasana2a47c62010-05-06 12:08:41 -0700315void hyperv_cleanup(void);
Vitaly Kuznetsov79cadff2017-08-02 18:09:13 +0200316#else /* CONFIG_HYPERV */
317static inline void hyperv_init(void) {}
318static inline bool hv_is_hypercall_page_setup(void) { return false; }
319static inline void hyperv_cleanup(void) {}
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200320static inline void hyperv_setup_mmu_ops(void) {}
Vitaly Kuznetsov79cadff2017-08-02 18:09:13 +0200321#endif /* CONFIG_HYPERV */
322
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +0100323#ifdef CONFIG_HYPERV_TSCPAGE
324struct ms_hyperv_tsc_page *hv_get_tsc_page(void);
Vitaly Kuznetsov07333792017-03-03 14:21:41 +0100325static inline u64 hv_read_tsc_page(const struct ms_hyperv_tsc_page *tsc_pg)
326{
327 u64 scale, offset, cur_tsc;
328 u32 sequence;
329
330 /*
331 * The protocol for reading Hyper-V TSC page is specified in Hypervisor
332 * Top-Level Functional Specification ver. 3.0 and above. To get the
333 * reference time we must do the following:
334 * - READ ReferenceTscSequence
335 * A special '0' value indicates the time source is unreliable and we
336 * need to use something else. The currently published specification
337 * versions (up to 4.0b) contain a mistake and wrongly claim '-1'
338 * instead of '0' as the special value, see commit c35b82ef0294.
339 * - ReferenceTime =
340 * ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset
341 * - READ ReferenceTscSequence again. In case its value has changed
342 * since our first reading we need to discard ReferenceTime and repeat
343 * the whole sequence as the hypervisor was updating the page in
344 * between.
345 */
346 do {
347 sequence = READ_ONCE(tsc_pg->tsc_sequence);
348 if (!sequence)
349 return U64_MAX;
350 /*
351 * Make sure we read sequence before we read other values from
352 * TSC page.
353 */
354 smp_rmb();
355
356 scale = READ_ONCE(tsc_pg->tsc_scale);
357 offset = READ_ONCE(tsc_pg->tsc_offset);
358 cur_tsc = rdtsc_ordered();
359
360 /*
361 * Make sure we read sequence after we read all other values
362 * from TSC page.
363 */
364 smp_rmb();
365
366 } while (READ_ONCE(tsc_pg->tsc_sequence) != sequence);
367
368 return mul_u64_u64_shr(cur_tsc, scale, 64) + offset;
369}
370
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +0100371#else
372static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
373{
374 return NULL;
375}
376#endif
Ky Srinivasana2a47c62010-05-06 12:08:41 -0700377#endif