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