1 #ifndef _ASM_X86_MSHYPER_H 2 #define _ASM_X86_MSHYPER_H 3 4 #include <linux/types.h> 5 #include <linux/atomic.h> 6 #include <asm/hyperv.h> 7 8 /* 9 * The below CPUID leaves are present if VersionAndFeatures.HypervisorPresent 10 * is set by CPUID(HVCPUID_VERSION_FEATURES). 11 */ 12 enum 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 27 struct ms_hyperv_info { 28 u32 features; 29 u32 misc_features; 30 u32 hints; 31 u32 max_vp_index; 32 u32 max_lp_index; 33 }; 34 35 extern struct ms_hyperv_info ms_hyperv; 36 37 /* 38 * Declare the MSR used to setup pages used to communicate with the hypervisor. 39 */ 40 union hv_x64_msr_hypercall_contents { 41 u64 as_uint64; 42 struct { 43 u64 enable:1; 44 u64 reserved:11; 45 u64 guest_physical_address:52; 46 }; 47 }; 48 49 /* 50 * TSC page layout. 51 */ 52 53 struct ms_hyperv_tsc_page { 54 volatile u32 tsc_sequence; 55 u32 reserved1; 56 volatile u64 tsc_scale; 57 volatile s64 tsc_offset; 58 u64 reserved2[509]; 59 }; 60 61 /* 62 * The guest OS needs to register the guest ID with the hypervisor. 63 * The guest ID is a 64 bit entity and the structure of this ID is 64 * specified in the Hyper-V specification: 65 * 66 * msdn.microsoft.com/en-us/library/windows/hardware/ff542653%28v=vs.85%29.aspx 67 * 68 * While the current guideline does not specify how Linux guest ID(s) 69 * need to be generated, our plan is to publish the guidelines for 70 * Linux and other guest operating systems that currently are hosted 71 * on Hyper-V. The implementation here conforms to this yet 72 * unpublished guidelines. 73 * 74 * 75 * Bit(s) 76 * 63 - Indicates if the OS is Open Source or not; 1 is Open Source 77 * 62:56 - Os Type; Linux is 0x100 78 * 55:48 - Distro specific identification 79 * 47:16 - Linux kernel version number 80 * 15:0 - Distro specific identification 81 * 82 * 83 */ 84 85 #define HV_LINUX_VENDOR_ID 0x8100 86 87 /* 88 * Generate the guest ID based on the guideline described above. 89 */ 90 91 static inline __u64 generate_guest_id(__u64 d_info1, __u64 kernel_version, 92 __u64 d_info2) 93 { 94 __u64 guest_id = 0; 95 96 guest_id = (((__u64)HV_LINUX_VENDOR_ID) << 48); 97 guest_id |= (d_info1 << 48); 98 guest_id |= (kernel_version << 16); 99 guest_id |= d_info2; 100 101 return guest_id; 102 } 103 104 105 /* Free the message slot and signal end-of-message if required */ 106 static inline void vmbus_signal_eom(struct hv_message *msg, u32 old_msg_type) 107 { 108 /* 109 * On crash we're reading some other CPU's message page and we need 110 * to be careful: this other CPU may already had cleared the header 111 * and the host may already had delivered some other message there. 112 * In case we blindly write msg->header.message_type we're going 113 * to lose it. We can still lose a message of the same type but 114 * we count on the fact that there can only be one 115 * CHANNELMSG_UNLOAD_RESPONSE and we don't care about other messages 116 * on crash. 117 */ 118 if (cmpxchg(&msg->header.message_type, old_msg_type, 119 HVMSG_NONE) != old_msg_type) 120 return; 121 122 /* 123 * Make sure the write to MessageType (ie set to 124 * HVMSG_NONE) happens before we read the 125 * MessagePending and EOMing. Otherwise, the EOMing 126 * will not deliver any more messages since there is 127 * no empty slot 128 */ 129 mb(); 130 131 if (msg->header.message_flags.msg_pending) { 132 /* 133 * This will cause message queue rescan to 134 * possibly deliver another msg from the 135 * hypervisor 136 */ 137 wrmsrl(HV_X64_MSR_EOM, 0); 138 } 139 } 140 141 #define hv_init_timer(timer, tick) wrmsrl(timer, tick) 142 #define hv_init_timer_config(config, val) wrmsrl(config, val) 143 144 #define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val) 145 #define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val) 146 147 #define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val) 148 #define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val) 149 150 #define hv_get_synic_state(val) rdmsrl(HV_X64_MSR_SCONTROL, val) 151 #define hv_set_synic_state(val) wrmsrl(HV_X64_MSR_SCONTROL, val) 152 153 #define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index) 154 155 #define hv_get_synint_state(int_num, val) rdmsrl(int_num, val) 156 #define hv_set_synint_state(int_num, val) wrmsrl(int_num, val) 157 158 void hyperv_callback_vector(void); 159 #ifdef CONFIG_TRACING 160 #define trace_hyperv_callback_vector hyperv_callback_vector 161 #endif 162 void hyperv_vector_handler(struct pt_regs *regs); 163 void hv_setup_vmbus_irq(void (*handler)(void)); 164 void hv_remove_vmbus_irq(void); 165 166 void hv_setup_kexec_handler(void (*handler)(void)); 167 void hv_remove_kexec_handler(void); 168 void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs)); 169 void hv_remove_crash_handler(void); 170 171 #if IS_ENABLED(CONFIG_HYPERV) 172 extern struct clocksource *hyperv_cs; 173 174 void hyperv_init(void); 175 void hyperv_report_panic(struct pt_regs *regs); 176 bool hv_is_hypercall_page_setup(void); 177 void hyperv_cleanup(void); 178 #else /* CONFIG_HYPERV */ 179 static inline void hyperv_init(void) {} 180 static inline bool hv_is_hypercall_page_setup(void) { return false; } 181 static inline hyperv_cleanup(void) {} 182 #endif /* CONFIG_HYPERV */ 183 184 #ifdef CONFIG_HYPERV_TSCPAGE 185 struct ms_hyperv_tsc_page *hv_get_tsc_page(void); 186 static inline u64 hv_read_tsc_page(const struct ms_hyperv_tsc_page *tsc_pg) 187 { 188 u64 scale, offset, cur_tsc; 189 u32 sequence; 190 191 /* 192 * The protocol for reading Hyper-V TSC page is specified in Hypervisor 193 * Top-Level Functional Specification ver. 3.0 and above. To get the 194 * reference time we must do the following: 195 * - READ ReferenceTscSequence 196 * A special '0' value indicates the time source is unreliable and we 197 * need to use something else. The currently published specification 198 * versions (up to 4.0b) contain a mistake and wrongly claim '-1' 199 * instead of '0' as the special value, see commit c35b82ef0294. 200 * - ReferenceTime = 201 * ((RDTSC() * ReferenceTscScale) >> 64) + ReferenceTscOffset 202 * - READ ReferenceTscSequence again. In case its value has changed 203 * since our first reading we need to discard ReferenceTime and repeat 204 * the whole sequence as the hypervisor was updating the page in 205 * between. 206 */ 207 do { 208 sequence = READ_ONCE(tsc_pg->tsc_sequence); 209 if (!sequence) 210 return U64_MAX; 211 /* 212 * Make sure we read sequence before we read other values from 213 * TSC page. 214 */ 215 smp_rmb(); 216 217 scale = READ_ONCE(tsc_pg->tsc_scale); 218 offset = READ_ONCE(tsc_pg->tsc_offset); 219 cur_tsc = rdtsc_ordered(); 220 221 /* 222 * Make sure we read sequence after we read all other values 223 * from TSC page. 224 */ 225 smp_rmb(); 226 227 } while (READ_ONCE(tsc_pg->tsc_sequence) != sequence); 228 229 return mul_u64_u64_shr(cur_tsc, scale, 64) + offset; 230 } 231 232 #else 233 static inline struct ms_hyperv_tsc_page *hv_get_tsc_page(void) 234 { 235 return NULL; 236 } 237 #endif 238 #endif 239