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