blob: 2b58c8c1eeaafd9afca3a55b3486ec3a360adffa [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>
H. Peter Anvine08cae42010-05-07 16:57:28 -07006#include <asm/hyperv.h>
7
K. Y. Srinivasan8de8af72017-01-19 11:51:47 -07008/*
9 * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent
10 * is set by CPUID(HVCPUID_VERSION_FEATURES).
11 */
12enum hv_cpuid_function {
13 HVCPUID_VERSION_FEATURES = 0x00000001,
14 HVCPUID_VENDOR_MAXFUNCTION = 0x40000000,
15 HVCPUID_INTERFACE = 0x40000001,
16
17 /*
18 * The remaining functions depend on the value of
19 * HVCPUID_INTERFACE
20 */
21 HVCPUID_VERSION = 0x40000002,
22 HVCPUID_FEATURES = 0x40000003,
23 HVCPUID_ENLIGHTENMENT_INFO = 0x40000004,
24 HVCPUID_IMPLEMENTATION_LIMITS = 0x40000005,
25};
26
H. Peter Anvine08cae42010-05-07 16:57:28 -070027struct ms_hyperv_info {
28 u32 features;
Denis V. Lunevcc2dd402015-08-01 16:08:20 -070029 u32 misc_features;
H. Peter Anvine08cae42010-05-07 16:57:28 -070030 u32 hints;
31};
32
33extern struct ms_hyperv_info ms_hyperv;
Ky Srinivasana2a47c62010-05-06 12:08:41 -070034
K. Y. Srinivasan3f646ed2017-01-18 16:45:00 -070035/*
36 * Declare the MSR used to setup pages used to communicate with the hypervisor.
37 */
38union hv_x64_msr_hypercall_contents {
39 u64 as_uint64;
40 struct {
41 u64 enable:1;
42 u64 reserved:11;
43 u64 guest_physical_address:52;
44 };
45};
46
K. Y. Srinivasan352c9622017-01-18 16:45:01 -070047/*
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070048 * TSC page layout.
49 */
50
51struct ms_hyperv_tsc_page {
52 volatile u32 tsc_sequence;
53 u32 reserved1;
54 volatile u64 tsc_scale;
55 volatile s64 tsc_offset;
56 u64 reserved2[509];
57};
58
59/*
K. Y. Srinivasan352c9622017-01-18 16:45:01 -070060 * The guest OS needs to register the guest ID with the hypervisor.
61 * The guest ID is a 64 bit entity and the structure of this ID is
62 * specified in the Hyper-V specification:
63 *
64 * msdn.microsoft.com/en-us/library/windows/hardware/ff542653%28v=vs.85%29.aspx
65 *
66 * While the current guideline does not specify how Linux guest ID(s)
67 * need to be generated, our plan is to publish the guidelines for
68 * Linux and other guest operating systems that currently are hosted
69 * on Hyper-V. The implementation here conforms to this yet
70 * unpublished guidelines.
71 *
72 *
73 * Bit(s)
74 * 63 - Indicates if the OS is Open Source or not; 1 is Open Source
75 * 62:56 - Os Type; Linux is 0x100
76 * 55:48 - Distro specific identification
77 * 47:16 - Linux kernel version number
78 * 15:0 - Distro specific identification
79 *
80 *
81 */
82
K. Y. Srinivasan9b06e102017-02-04 08:46:23 -070083#define HV_LINUX_VENDOR_ID 0x8100
K. Y. Srinivasan352c9622017-01-18 16:45:01 -070084
85/*
86 * Generate the guest ID based on the guideline described above.
87 */
88
89static inline __u64 generate_guest_id(__u64 d_info1, __u64 kernel_version,
90 __u64 d_info2)
91{
92 __u64 guest_id = 0;
93
K. Y. Srinivasan9b06e102017-02-04 08:46:23 -070094 guest_id = (((__u64)HV_LINUX_VENDOR_ID) << 48);
K. Y. Srinivasan352c9622017-01-18 16:45:01 -070095 guest_id |= (d_info1 << 48);
96 guest_id |= (kernel_version << 16);
97 guest_id |= d_info2;
98
99 return guest_id;
100}
101
K. Y. Srinivasane810e482017-01-19 11:51:50 -0700102
103/* Free the message slot and signal end-of-message if required */
104static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type)
105{
106 /*
107 * On crash we're reading some other CPU's message page and we need
108 * to be careful: this other CPU may already had cleared the header
109 * and the host may already had delivered some other message there.
110 * In case we blindly write msg->header.message_type we're going
111 * to lose it. We can still lose a message of the same type but
112 * we count on the fact that there can only be one
113 * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages
114 * on crash.
115 */
116 if (cmpxchg(&msg->header.message_type, old_msg_type,
117 HVMSG_NONE) != old_msg_type)
118 return;
119
120 /*
121 * Make sure the write to MessageType (ie set to
122 * HVMSG_NONE) happens before we read the
123 * MessagePending and EOMing. Otherwise, the EOMing
124 * will not deliver any more messages since there is
125 * no empty slot
126 */
127 mb();
128
129 if (msg->header.message_flags.msg_pending) {
130 /*
131 * This will cause message queue rescan to
132 * possibly deliver another msg from the
133 * hypervisor
134 */
135 wrmsrl(HV_X64_MSR_EOM, 0);
136 }
137}
138
K. Y. Srinivasand5116b42017-01-19 11:51:51 -0700139#define hv_init_timer(timer, tick) wrmsrl(timer, tick)
140#define hv_init_timer_config(config, val) wrmsrl(config, val)
141
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -0700142#define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val)
143#define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val)
144
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -0700145#define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val)
146#define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val)
147
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -0700148#define hv_get_synic_state(val) rdmsrl(HV_X64_MSR_SCONTROL, val)
149#define hv_set_synic_state(val) wrmsrl(HV_X64_MSR_SCONTROL, val)
150
K. Y. Srinivasan7297ff02017-01-19 11:51:57 -0700151#define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index)
152
K. Y. Srinivasan37e11d52017-01-19 11:51:58 -0700153#define hv_get_synint_state(int_num, val) rdmsrl(int_num, val)
154#define hv_set_synint_state(int_num, val) wrmsrl(int_num, val)
155
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -0800156void hyperv_callback_vector(void);
Seiji Aguchicf910e82013-06-20 11:46:53 -0400157#ifdef CONFIG_TRACING
158#define trace_hyperv_callback_vector hyperv_callback_vector
159#endif
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -0800160void hyperv_vector_handler(struct pt_regs *regs);
Thomas Gleixner76d388c2014-03-05 13:42:14 +0100161void hv_setup_vmbus_irq(void (*handler)(void));
162void hv_remove_vmbus_irq(void);
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -0800163
Vitaly Kuznetsov25172812015-08-01 16:08:07 -0700164void hv_setup_kexec_handler(void (*handler)(void));
165void hv_remove_kexec_handler(void);
Vitaly Kuznetsovb4370df2015-08-01 16:08:09 -0700166void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs));
167void hv_remove_crash_handler(void);
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700168
169#if IS_ENABLED(CONFIG_HYPERV)
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -0700170extern struct clocksource *hyperv_cs;
171
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700172void hyperv_init(void);
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700173void hyperv_report_panic(struct pt_regs *regs);
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700174bool hv_is_hypercall_page_setup(void);
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700175void hyperv_cleanup(void);
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700176#endif
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +0100177#ifdef CONFIG_HYPERV_TSCPAGE
178struct ms_hyperv_tsc_page *hv_get_tsc_page(void);
Vitaly Kuznetsov07333792017-03-03 14:21:41 +0100179static inline u64 hv_read_tsc_page(const struct ms_hyperv_tsc_page *tsc_pg)
180{
181 u64 scale, offset, cur_tsc;
182 u32 sequence;
183
184 /*
185 * The protocol for reading Hyper-V TSC page is specified in Hypervisor
186 * Top-Level Functional Specification ver. 3.0 and above. To get the
187 * reference time we must do the following:
188 * - READ ReferenceTscSequence
189 * A special '0' value indicates the time source is unreliable and we
190 * need to use something else. The currently published specification
191 * versions (up to 4.0b) contain a mistake and wrongly claim '-1'
192 * instead of '0' as the special value, see commit c35b82ef0294.
193 * - ReferenceTime =
194 * ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset
195 * - READ ReferenceTscSequence again. In case its value has changed
196 * since our first reading we need to discard ReferenceTime and repeat
197 * the whole sequence as the hypervisor was updating the page in
198 * between.
199 */
200 do {
201 sequence = READ_ONCE(tsc_pg->tsc_sequence);
202 if (!sequence)
203 return U64_MAX;
204 /*
205 * Make sure we read sequence before we read other values from
206 * TSC page.
207 */
208 smp_rmb();
209
210 scale = READ_ONCE(tsc_pg->tsc_scale);
211 offset = READ_ONCE(tsc_pg->tsc_offset);
212 cur_tsc = rdtsc_ordered();
213
214 /*
215 * Make sure we read sequence after we read all other values
216 * from TSC page.
217 */
218 smp_rmb();
219
220 } while (READ_ONCE(tsc_pg->tsc_sequence) != sequence);
221
222 return mul_u64_u64_shr(cur_tsc, scale, 64) + offset;
223}
224
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +0100225#else
226static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
227{
228 return NULL;
229}
230#endif
Ky Srinivasana2a47c62010-05-06 12:08:41 -0700231#endif