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>
12 typedef int (*hyperv_fill_flush_list_func
)(
13 struct hv_guest_mapping_flush_list
*flush
,
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)
21 #define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val)
22 #define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val)
24 #define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val)
25 #define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val)
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)
30 #define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index)
32 #define hv_signal_eom() wrmsrl(HV_X64_MSR_EOM, 0)
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)
39 #define hv_get_crash_ctl(val) \
40 rdmsrl(HV_X64_MSR_CRASH_CTL, val)
42 #define hv_get_time_ref_count(val) \
43 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, val)
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()
55 void hyperv_callback_vector(void);
56 void hyperv_reenlightenment_vector(void);
58 #define trace_hyperv_callback_vector hyperv_callback_vector
60 void hyperv_vector_handler(struct pt_regs
*regs
);
63 * Routines for stimer0 Direct Mode handling.
64 * On x86/x64, there are no percpu actions to take.
66 void hv_stimer0_vector_handler(struct pt_regs
*regs
);
67 void hv_stimer0_callback_vector(void);
69 static inline void hv_enable_stimer0_percpu_irq(int irq
) {}
70 static inline void hv_disable_stimer0_percpu_irq(int irq
) {}
73 #if IS_ENABLED(CONFIG_HYPERV)
74 extern void *hv_hypercall_pg
;
75 extern void __percpu
**hyperv_pcpu_input_arg
;
77 static inline u64
hv_do_hypercall(u64 control
, void *input
, void *output
)
79 u64 input_address
= input
? virt_to_phys(input
) : 0;
80 u64 output_address
= output
? virt_to_phys(output
) : 0;
87 __asm__
__volatile__("mov %4, %%r8\n"
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");
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
);
100 if (!hv_hypercall_pg
)
103 __asm__
__volatile__(CALL_NOSPEC
105 "+c" (input_address_lo
), ASM_CALL_CONSTRAINT
107 "b" (input_address_hi
),
108 "D"(output_address_hi
), "S"(output_address_lo
),
109 THUNK_TARGET(hv_hypercall_pg
)
115 /* Fast hypercall with 8 bytes of input and no output */
116 static inline u64
hv_do_fast_hypercall8(u16 code
, u64 input1
)
118 u64 hv_status
, control
= (u64
)code
| HV_HYPERCALL_FAST_BIT
;
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");
130 u32 input1_hi
= upper_32_bits(input1
);
131 u32 input1_lo
= lower_32_bits(input1
);
133 __asm__
__volatile__ (CALL_NOSPEC
139 THUNK_TARGET(hv_hypercall_pg
)
140 : "cc", "edi", "esi");
146 /* Fast hypercall with 16 bytes of input */
147 static inline u64
hv_do_fast_hypercall16(u16 code
, u64 input1
, u64 input2
)
149 u64 hv_status
, control
= (u64
)code
| HV_HYPERCALL_FAST_BIT
;
153 __asm__
__volatile__("mov %4, %%r8\n"
155 : "=a" (hv_status
), ASM_CALL_CONSTRAINT
,
156 "+c" (control
), "+d" (input1
)
158 THUNK_TARGET(hv_hypercall_pg
)
159 : "cc", "r8", "r9", "r10", "r11");
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
);
168 __asm__
__volatile__ (CALL_NOSPEC
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
)
181 * Rep hypercalls. Callers of this functions are supposed to ensure that
182 * rep_count and varhead_size comply with Hyper-V hypercall definition.
184 static inline u64
hv_do_rep_hypercall(u16 code
, u16 rep_count
, u16 varhead_size
,
185 void *input
, void *output
)
191 control
|= (u64
)varhead_size
<< HV_HYPERCALL_VARHEAD_OFFSET
;
192 control
|= (u64
)rep_count
<< HV_HYPERCALL_REP_COMP_OFFSET
;
195 status
= hv_do_hypercall(control
, input
, output
);
196 if ((status
& HV_HYPERCALL_RESULT_MASK
) != HV_STATUS_SUCCESS
)
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
;
203 control
&= ~HV_HYPERCALL_REP_START_MASK
;
204 control
|= (u64
)rep_comp
<< HV_HYPERCALL_REP_START_OFFSET
;
206 touch_nmi_watchdog();
207 } while (rep_comp
< rep_count
);
212 extern struct hv_vp_assist_page
**hv_vp_assist_page
;
214 static inline struct hv_vp_assist_page
*hv_get_vp_assist_page(unsigned int cpu
)
216 if (!hv_vp_assist_page
)
219 return hv_vp_assist_page
[cpu
];
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
);
239 void hv_apic_init(void);
240 void __init
hv_init_spinlocks(void);
241 bool hv_vcpu_is_preempted(int vcpu
);
243 static inline void hv_apic_init(void) {}
246 static inline void hv_set_msi_entry_from_desc(union hv_msi_entry
*msi_entry
,
247 struct msi_desc
*msi_desc
)
249 msi_entry
->address
= msi_desc
->msg
.address_lo
;
250 msi_entry
->data
= msi_desc
->msg
.data
;
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
)
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
)
271 #endif /* CONFIG_HYPERV */
274 #include <asm-generic/mshyperv.h>