1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_MSHYPER_H
3 #define _ASM_X86_MSHYPER_H
5 #include <linux/types.h>
9 #include <asm/hyperv-tlfs.h>
10 #include <asm/nospec-branch.h>
11 #include <asm/paravirt.h>
13 typedef int (*hyperv_fill_flush_list_func)(
14 struct hv_guest_mapping_flush_list *flush,
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)
22 #define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val)
23 #define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val)
25 #define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val)
26 #define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val)
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)
31 #define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index)
33 #define hv_signal_eom() wrmsrl(HV_X64_MSR_EOM, 0)
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))
42 #define hv_get_crash_ctl(val) \
43 rdmsrl(HV_X64_MSR_CRASH_CTL, val)
45 #define hv_get_time_ref_count(val) \
46 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, val)
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 #define hv_get_vector() HYPERVISOR_CALLBACK_VECTOR
60 * Reference to pv_ops must be inline so objtool
61 * detection of noinstr violations can work correctly.
63 static __always_inline void hv_setup_sched_clock(void *sched_clock)
65 #ifdef CONFIG_PARAVIRT
66 pv_ops.time.sched_clock = sched_clock;
70 void hyperv_vector_handler(struct pt_regs *regs);
72 static inline void hv_enable_stimer0_percpu_irq(int irq) {}
73 static inline void hv_disable_stimer0_percpu_irq(int irq) {}
76 #if IS_ENABLED(CONFIG_HYPERV)
77 extern void *hv_hypercall_pg;
78 extern void __percpu **hyperv_pcpu_input_arg;
80 static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
82 u64 input_address = input ? virt_to_phys(input) : 0;
83 u64 output_address = output ? virt_to_phys(output) : 0;
90 __asm__ __volatile__("mov %4, %%r8\n"
92 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
93 "+c" (control), "+d" (input_address)
94 : "r" (output_address),
95 THUNK_TARGET(hv_hypercall_pg)
96 : "cc", "memory", "r8", "r9", "r10", "r11");
98 u32 input_address_hi = upper_32_bits(input_address);
99 u32 input_address_lo = lower_32_bits(input_address);
100 u32 output_address_hi = upper_32_bits(output_address);
101 u32 output_address_lo = lower_32_bits(output_address);
103 if (!hv_hypercall_pg)
106 __asm__ __volatile__(CALL_NOSPEC
108 "+c" (input_address_lo), ASM_CALL_CONSTRAINT
110 "b" (input_address_hi),
111 "D"(output_address_hi), "S"(output_address_lo),
112 THUNK_TARGET(hv_hypercall_pg)
118 /* Fast hypercall with 8 bytes of input and no output */
119 static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
121 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
125 __asm__ __volatile__(CALL_NOSPEC
126 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
127 "+c" (control), "+d" (input1)
128 : THUNK_TARGET(hv_hypercall_pg)
129 : "cc", "r8", "r9", "r10", "r11");
133 u32 input1_hi = upper_32_bits(input1);
134 u32 input1_lo = lower_32_bits(input1);
136 __asm__ __volatile__ (CALL_NOSPEC
142 THUNK_TARGET(hv_hypercall_pg)
143 : "cc", "edi", "esi");
149 /* Fast hypercall with 16 bytes of input */
150 static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
152 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
156 __asm__ __volatile__("mov %4, %%r8\n"
158 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
159 "+c" (control), "+d" (input1)
161 THUNK_TARGET(hv_hypercall_pg)
162 : "cc", "r8", "r9", "r10", "r11");
166 u32 input1_hi = upper_32_bits(input1);
167 u32 input1_lo = lower_32_bits(input1);
168 u32 input2_hi = upper_32_bits(input2);
169 u32 input2_lo = lower_32_bits(input2);
171 __asm__ __volatile__ (CALL_NOSPEC
173 "+c"(input1_lo), ASM_CALL_CONSTRAINT
174 : "A" (control), "b" (input1_hi),
175 "D"(input2_hi), "S"(input2_lo),
176 THUNK_TARGET(hv_hypercall_pg)
184 * Rep hypercalls. Callers of this functions are supposed to ensure that
185 * rep_count and varhead_size comply with Hyper-V hypercall definition.
187 static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
188 void *input, void *output)
194 control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
195 control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
198 status = hv_do_hypercall(control, input, output);
199 if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS)
202 /* Bits 32-43 of status have 'Reps completed' data. */
203 rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >>
204 HV_HYPERCALL_REP_COMP_OFFSET;
206 control &= ~HV_HYPERCALL_REP_START_MASK;
207 control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
209 touch_nmi_watchdog();
210 } while (rep_comp < rep_count);
215 extern struct hv_vp_assist_page **hv_vp_assist_page;
217 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
219 if (!hv_vp_assist_page)
222 return hv_vp_assist_page[cpu];
225 void __init hyperv_init(void);
226 void hyperv_setup_mmu_ops(void);
227 void *hv_alloc_hyperv_page(void);
228 void *hv_alloc_hyperv_zeroed_page(void);
229 void hv_free_hyperv_page(unsigned long addr);
230 void set_hv_tscchange_cb(void (*cb)(void));
231 void clear_hv_tscchange_cb(void);
232 void hyperv_stop_tsc_emulation(void);
233 int hyperv_flush_guest_mapping(u64 as);
234 int hyperv_flush_guest_mapping_range(u64 as,
235 hyperv_fill_flush_list_func fill_func, void *data);
236 int hyperv_fill_flush_guest_mapping_list(
237 struct hv_guest_mapping_flush_list *flush,
238 u64 start_gfn, u64 end_gfn);
241 void hv_apic_init(void);
242 void __init hv_init_spinlocks(void);
243 bool hv_vcpu_is_preempted(int vcpu);
245 static inline void hv_apic_init(void) {}
248 static inline void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry,
249 struct msi_desc *msi_desc)
251 msi_entry->address = msi_desc->msg.address_lo;
252 msi_entry->data = msi_desc->msg.data;
255 #else /* CONFIG_HYPERV */
256 static inline void hyperv_init(void) {}
257 static inline void hyperv_setup_mmu_ops(void) {}
258 static inline void *hv_alloc_hyperv_page(void) { return NULL; }
259 static inline void hv_free_hyperv_page(unsigned long addr) {}
260 static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
261 static inline void clear_hv_tscchange_cb(void) {}
262 static inline void hyperv_stop_tsc_emulation(void) {};
263 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
267 static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
268 static inline int hyperv_flush_guest_mapping_range(u64 as,
269 hyperv_fill_flush_list_func fill_func, void *data)
273 #endif /* CONFIG_HYPERV */
276 #include <asm-generic/mshyperv.h>