1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _ASM_X86_MSHYPER_H 3 #define _ASM_X86_MSHYPER_H 4 5 #include <linux/types.h> 6 #include <linux/nmi.h> 7 #include <linux/msi.h> 8 #include <asm/io.h> 9 #include <asm/hyperv-tlfs.h> 10 #include <asm/nospec-branch.h> 11 12 typedef int (*hyperv_fill_flush_list_func)( 13 struct hv_guest_mapping_flush_list *flush, 14 void *data); 15 16 #define hv_init_timer(timer, tick) \ 17 wrmsrl(HV_X64_MSR_STIMER0_COUNT + (2*timer), tick) 18 #define hv_init_timer_config(timer, val) \ 19 wrmsrl(HV_X64_MSR_STIMER0_CONFIG + (2*timer), val) 20 21 #define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val) 22 #define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val) 23 24 #define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val) 25 #define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val) 26 27 #define hv_get_synic_state(val) rdmsrl(HV_X64_MSR_SCONTROL, val) 28 #define hv_set_synic_state(val) wrmsrl(HV_X64_MSR_SCONTROL, val) 29 30 #define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index) 31 32 #define hv_signal_eom() wrmsrl(HV_X64_MSR_EOM, 0) 33 34 #define hv_get_synint_state(int_num, val) \ 35 rdmsrl(HV_X64_MSR_SINT0 + int_num, val) 36 #define hv_set_synint_state(int_num, val) \ 37 wrmsrl(HV_X64_MSR_SINT0 + int_num, val) 38 #define hv_recommend_using_aeoi() \ 39 (!(ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED)) 40 41 #define hv_get_crash_ctl(val) \ 42 rdmsrl(HV_X64_MSR_CRASH_CTL, val) 43 44 #define hv_get_time_ref_count(val) \ 45 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, val) 46 47 #define hv_get_reference_tsc(val) \ 48 rdmsrl(HV_X64_MSR_REFERENCE_TSC, val) 49 #define hv_set_reference_tsc(val) \ 50 wrmsrl(HV_X64_MSR_REFERENCE_TSC, val) 51 #define hv_set_clocksource_vdso(val) \ 52 ((val).vdso_clock_mode = VDSO_CLOCKMODE_HVCLOCK) 53 #define hv_enable_vdso_clocksource() \ 54 vclocks_set_used(VDSO_CLOCKMODE_HVCLOCK); 55 #define hv_get_raw_timer() rdtsc_ordered() 56 57 void hyperv_vector_handler(struct pt_regs *regs); 58 59 static inline void hv_enable_stimer0_percpu_irq(int irq) {} 60 static inline void hv_disable_stimer0_percpu_irq(int irq) {} 61 62 63 #if IS_ENABLED(CONFIG_HYPERV) 64 extern void *hv_hypercall_pg; 65 extern void __percpu **hyperv_pcpu_input_arg; 66 67 static inline u64 hv_do_hypercall(u64 control, void *input, void *output) 68 { 69 u64 input_address = input ? virt_to_phys(input) : 0; 70 u64 output_address = output ? virt_to_phys(output) : 0; 71 u64 hv_status; 72 73 #ifdef CONFIG_X86_64 74 if (!hv_hypercall_pg) 75 return U64_MAX; 76 77 __asm__ __volatile__("mov %4, %%r8\n" 78 CALL_NOSPEC 79 : "=a" (hv_status), ASM_CALL_CONSTRAINT, 80 "+c" (control), "+d" (input_address) 81 : "r" (output_address), 82 THUNK_TARGET(hv_hypercall_pg) 83 : "cc", "memory", "r8", "r9", "r10", "r11"); 84 #else 85 u32 input_address_hi = upper_32_bits(input_address); 86 u32 input_address_lo = lower_32_bits(input_address); 87 u32 output_address_hi = upper_32_bits(output_address); 88 u32 output_address_lo = lower_32_bits(output_address); 89 90 if (!hv_hypercall_pg) 91 return U64_MAX; 92 93 __asm__ __volatile__(CALL_NOSPEC 94 : "=A" (hv_status), 95 "+c" (input_address_lo), ASM_CALL_CONSTRAINT 96 : "A" (control), 97 "b" (input_address_hi), 98 "D"(output_address_hi), "S"(output_address_lo), 99 THUNK_TARGET(hv_hypercall_pg) 100 : "cc", "memory"); 101 #endif /* !x86_64 */ 102 return hv_status; 103 } 104 105 /* Fast hypercall with 8 bytes of input and no output */ 106 static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1) 107 { 108 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; 109 110 #ifdef CONFIG_X86_64 111 { 112 __asm__ __volatile__(CALL_NOSPEC 113 : "=a" (hv_status), ASM_CALL_CONSTRAINT, 114 "+c" (control), "+d" (input1) 115 : THUNK_TARGET(hv_hypercall_pg) 116 : "cc", "r8", "r9", "r10", "r11"); 117 } 118 #else 119 { 120 u32 input1_hi = upper_32_bits(input1); 121 u32 input1_lo = lower_32_bits(input1); 122 123 __asm__ __volatile__ (CALL_NOSPEC 124 : "=A"(hv_status), 125 "+c"(input1_lo), 126 ASM_CALL_CONSTRAINT 127 : "A" (control), 128 "b" (input1_hi), 129 THUNK_TARGET(hv_hypercall_pg) 130 : "cc", "edi", "esi"); 131 } 132 #endif 133 return hv_status; 134 } 135 136 /* Fast hypercall with 16 bytes of input */ 137 static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2) 138 { 139 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT; 140 141 #ifdef CONFIG_X86_64 142 { 143 __asm__ __volatile__("mov %4, %%r8\n" 144 CALL_NOSPEC 145 : "=a" (hv_status), ASM_CALL_CONSTRAINT, 146 "+c" (control), "+d" (input1) 147 : "r" (input2), 148 THUNK_TARGET(hv_hypercall_pg) 149 : "cc", "r8", "r9", "r10", "r11"); 150 } 151 #else 152 { 153 u32 input1_hi = upper_32_bits(input1); 154 u32 input1_lo = lower_32_bits(input1); 155 u32 input2_hi = upper_32_bits(input2); 156 u32 input2_lo = lower_32_bits(input2); 157 158 __asm__ __volatile__ (CALL_NOSPEC 159 : "=A"(hv_status), 160 "+c"(input1_lo), ASM_CALL_CONSTRAINT 161 : "A" (control), "b" (input1_hi), 162 "D"(input2_hi), "S"(input2_lo), 163 THUNK_TARGET(hv_hypercall_pg) 164 : "cc"); 165 } 166 #endif 167 return hv_status; 168 } 169 170 /* 171 * Rep hypercalls. Callers of this functions are supposed to ensure that 172 * rep_count and varhead_size comply with Hyper-V hypercall definition. 173 */ 174 static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size, 175 void *input, void *output) 176 { 177 u64 control = code; 178 u64 status; 179 u16 rep_comp; 180 181 control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET; 182 control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET; 183 184 do { 185 status = hv_do_hypercall(control, input, output); 186 if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS) 187 return status; 188 189 /* Bits 32-43 of status have 'Reps completed' data. */ 190 rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >> 191 HV_HYPERCALL_REP_COMP_OFFSET; 192 193 control &= ~HV_HYPERCALL_REP_START_MASK; 194 control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET; 195 196 touch_nmi_watchdog(); 197 } while (rep_comp < rep_count); 198 199 return status; 200 } 201 202 extern struct hv_vp_assist_page **hv_vp_assist_page; 203 204 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) 205 { 206 if (!hv_vp_assist_page) 207 return NULL; 208 209 return hv_vp_assist_page[cpu]; 210 } 211 212 void __init hyperv_init(void); 213 void hyperv_setup_mmu_ops(void); 214 void *hv_alloc_hyperv_page(void); 215 void *hv_alloc_hyperv_zeroed_page(void); 216 void hv_free_hyperv_page(unsigned long addr); 217 void set_hv_tscchange_cb(void (*cb)(void)); 218 void clear_hv_tscchange_cb(void); 219 void hyperv_stop_tsc_emulation(void); 220 int hyperv_flush_guest_mapping(u64 as); 221 int hyperv_flush_guest_mapping_range(u64 as, 222 hyperv_fill_flush_list_func fill_func, void *data); 223 int hyperv_fill_flush_guest_mapping_list( 224 struct hv_guest_mapping_flush_list *flush, 225 u64 start_gfn, u64 end_gfn); 226 227 #ifdef CONFIG_X86_64 228 void hv_apic_init(void); 229 void __init hv_init_spinlocks(void); 230 bool hv_vcpu_is_preempted(int vcpu); 231 #else 232 static inline void hv_apic_init(void) {} 233 #endif 234 235 static inline void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry, 236 struct msi_desc *msi_desc) 237 { 238 msi_entry->address = msi_desc->msg.address_lo; 239 msi_entry->data = msi_desc->msg.data; 240 } 241 242 #else /* CONFIG_HYPERV */ 243 static inline void hyperv_init(void) {} 244 static inline void hyperv_setup_mmu_ops(void) {} 245 static inline void *hv_alloc_hyperv_page(void) { return NULL; } 246 static inline void hv_free_hyperv_page(unsigned long addr) {} 247 static inline void set_hv_tscchange_cb(void (*cb)(void)) {} 248 static inline void clear_hv_tscchange_cb(void) {} 249 static inline void hyperv_stop_tsc_emulation(void) {}; 250 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu) 251 { 252 return NULL; 253 } 254 static inline int hyperv_flush_guest_mapping(u64 as) { return -1; } 255 static inline int hyperv_flush_guest_mapping_range(u64 as, 256 hyperv_fill_flush_list_func fill_func, void *data) 257 { 258 return -1; 259 } 260 #endif /* CONFIG_HYPERV */ 261 262 263 #include <asm-generic/mshyperv.h> 264 265 #endif 266