blob: 30f76b96685799c7e95cfff039d265a1dc42dea0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
H. Peter Anvine08cae42010-05-07 16:57:28 -07002#ifndef _ASM_X86_MSHYPER_H
3#define _ASM_X86_MSHYPER_H
Ky Srinivasana2a47c62010-05-06 12:08:41 -07004
H. Peter Anvine08cae42010-05-07 16:57:28 -07005#include <linux/types.h>
Vitaly Kuznetsov806c8922017-08-02 18:09:17 +02006#include <linux/nmi.h>
Boqun Feng1cf106d2020-02-10 11:39:53 +08007#include <linux/msi.h>
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +02008#include <asm/io.h>
Vitaly Kuznetsov5a485802018-03-20 15:02:05 +01009#include <asm/hyperv-tlfs.h>
David Woodhousee70e5892018-01-11 21:46:30 +000010#include <asm/nospec-branch.h>
Michael Kelleyb9d8cf22020-08-09 18:29:51 -070011#include <asm/paravirt.h>
H. Peter Anvine08cae42010-05-07 16:57:28 -070012
Lan Tianyucc4edae42018-12-06 21:21:05 +080013typedef int (*hyperv_fill_flush_list_func)(
14 struct hv_guest_mapping_flush_list *flush,
15 void *data);
16
Michael Kelley619a4c82018-06-05 13:37:53 -070017#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)
K. Y. Srinivasand5116b42017-01-19 11:51:51 -070021
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -070022#define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val)
23#define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val)
24
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -070025#define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val)
26#define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val)
27
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -070028#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
K. Y. Srinivasan7297ff02017-01-19 11:51:57 -070031#define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index)
32
Michael Kelley765e33f2019-05-30 00:14:00 +000033#define hv_signal_eom() wrmsrl(HV_X64_MSR_EOM, 0)
34
Michael Kelley619a4c82018-06-05 13:37:53 -070035#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)
Michael Kelley2ddddd02020-04-20 09:49:26 -070039#define hv_recommend_using_aeoi() \
40 (!(ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED))
K. Y. Srinivasan37e11d52017-01-19 11:51:58 -070041
Sunil Muthuswamy9d9c9652018-07-28 21:58:47 +000042#define hv_get_crash_ctl(val) \
43 rdmsrl(HV_X64_MSR_CRASH_CTL, val)
Seiji Aguchicf910e82013-06-20 11:46:53 -040044
Michael Kelleydd2cb342019-07-01 04:26:06 +000045#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) \
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +010053 ((val).vdso_clock_mode = VDSO_CLOCKMODE_HVCLOCK)
Thomas Gleixnereec399d2020-02-07 13:38:54 +010054#define hv_enable_vdso_clocksource() \
Thomas Gleixnerb95a8a22020-02-07 13:38:56 +010055 vclocks_set_used(VDSO_CLOCKMODE_HVCLOCK);
Michael Kelleydd2cb342019-07-01 04:26:06 +000056#define hv_get_raw_timer() rdtsc_ordered()
Michael Kelley626b9012020-08-14 12:45:04 -070057#define hv_get_vector() HYPERVISOR_CALLBACK_VECTOR
Michael Kelleydd2cb342019-07-01 04:26:06 +000058
Michael Kelleyb9d8cf22020-08-09 18:29:51 -070059/*
60 * Reference to pv_ops must be inline so objtool
61 * detection of noinstr violations can work correctly.
62 */
63static __always_inline void hv_setup_sched_clock(void *sched_clock)
64{
65#ifdef CONFIG_PARAVIRT
66 pv_ops.time.sched_clock = sched_clock;
67#endif
68}
69
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -080070void hyperv_vector_handler(struct pt_regs *regs);
K. Y. Srinivasan87300462017-01-18 16:45:02 -070071
Michael Kelley248e7422018-03-04 22:17:18 -070072static inline void hv_enable_stimer0_percpu_irq(int irq) {}
73static inline void hv_disable_stimer0_percpu_irq(int irq) {}
74
75
K. Y. Srinivasan87300462017-01-18 16:45:02 -070076#if IS_ENABLED(CONFIG_HYPERV)
Dexuan Cuidfe94d42020-12-21 22:55:41 -080077extern int hyperv_init_cpuhp;
78
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020079extern void *hv_hypercall_pg;
K. Y. Srinivasan68bb7bf2018-05-16 14:53:31 -070080extern void __percpu **hyperv_pcpu_input_arg;
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020081
82static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
83{
84 u64 input_address = input ? virt_to_phys(input) : 0;
85 u64 output_address = output ? virt_to_phys(output) : 0;
86 u64 hv_status;
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020087
88#ifdef CONFIG_X86_64
89 if (!hv_hypercall_pg)
90 return U64_MAX;
91
92 __asm__ __volatile__("mov %4, %%r8\n"
David Woodhousee70e5892018-01-11 21:46:30 +000093 CALL_NOSPEC
Josh Poimboeuff5caf622017-09-20 16:24:33 -050094 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020095 "+c" (control), "+d" (input_address)
David Woodhousee70e5892018-01-11 21:46:30 +000096 : "r" (output_address),
97 THUNK_TARGET(hv_hypercall_pg)
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020098 : "cc", "memory", "r8", "r9", "r10", "r11");
99#else
100 u32 input_address_hi = upper_32_bits(input_address);
101 u32 input_address_lo = lower_32_bits(input_address);
102 u32 output_address_hi = upper_32_bits(output_address);
103 u32 output_address_lo = lower_32_bits(output_address);
104
105 if (!hv_hypercall_pg)
106 return U64_MAX;
107
David Woodhousee70e5892018-01-11 21:46:30 +0000108 __asm__ __volatile__(CALL_NOSPEC
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200109 : "=A" (hv_status),
Josh Poimboeuff5caf622017-09-20 16:24:33 -0500110 "+c" (input_address_lo), ASM_CALL_CONSTRAINT
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200111 : "A" (control),
112 "b" (input_address_hi),
113 "D"(output_address_hi), "S"(output_address_lo),
David Woodhousee70e5892018-01-11 21:46:30 +0000114 THUNK_TARGET(hv_hypercall_pg)
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200115 : "cc", "memory");
116#endif /* !x86_64 */
117 return hv_status;
118}
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -0700119
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200120/* Fast hypercall with 8 bytes of input and no output */
121static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
122{
123 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200124
125#ifdef CONFIG_X86_64
126 {
David Woodhousee70e5892018-01-11 21:46:30 +0000127 __asm__ __volatile__(CALL_NOSPEC
Josh Poimboeuff5caf622017-09-20 16:24:33 -0500128 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200129 "+c" (control), "+d" (input1)
David Woodhousee70e5892018-01-11 21:46:30 +0000130 : THUNK_TARGET(hv_hypercall_pg)
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200131 : "cc", "r8", "r9", "r10", "r11");
132 }
133#else
134 {
135 u32 input1_hi = upper_32_bits(input1);
136 u32 input1_lo = lower_32_bits(input1);
137
David Woodhousee70e5892018-01-11 21:46:30 +0000138 __asm__ __volatile__ (CALL_NOSPEC
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200139 : "=A"(hv_status),
140 "+c"(input1_lo),
Josh Poimboeuff5caf622017-09-20 16:24:33 -0500141 ASM_CALL_CONSTRAINT
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200142 : "A" (control),
143 "b" (input1_hi),
David Woodhousee70e5892018-01-11 21:46:30 +0000144 THUNK_TARGET(hv_hypercall_pg)
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200145 : "cc", "edi", "esi");
146 }
147#endif
148 return hv_status;
149}
150
Vitaly Kuznetsov53e52962018-06-22 19:06:22 +0200151/* Fast hypercall with 16 bytes of input */
152static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
153{
154 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
155
156#ifdef CONFIG_X86_64
157 {
158 __asm__ __volatile__("mov %4, %%r8\n"
159 CALL_NOSPEC
160 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
161 "+c" (control), "+d" (input1)
162 : "r" (input2),
163 THUNK_TARGET(hv_hypercall_pg)
164 : "cc", "r8", "r9", "r10", "r11");
165 }
166#else
167 {
168 u32 input1_hi = upper_32_bits(input1);
169 u32 input1_lo = lower_32_bits(input1);
170 u32 input2_hi = upper_32_bits(input2);
171 u32 input2_lo = lower_32_bits(input2);
172
173 __asm__ __volatile__ (CALL_NOSPEC
174 : "=A"(hv_status),
175 "+c"(input1_lo), ASM_CALL_CONSTRAINT
176 : "A" (control), "b" (input1_hi),
177 "D"(input2_hi), "S"(input2_lo),
178 THUNK_TARGET(hv_hypercall_pg)
179 : "cc");
180 }
181#endif
Yi Wangb42967d2018-10-29 15:17:31 +0800182 return hv_status;
Vitaly Kuznetsov53e52962018-06-22 19:06:22 +0200183}
184
Vitaly Kuznetsov806c8922017-08-02 18:09:17 +0200185/*
186 * Rep hypercalls. Callers of this functions are supposed to ensure that
187 * rep_count and varhead_size comply with Hyper-V hypercall definition.
188 */
189static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
190 void *input, void *output)
191{
192 u64 control = code;
193 u64 status;
194 u16 rep_comp;
195
196 control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
197 control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
198
199 do {
200 status = hv_do_hypercall(control, input, output);
201 if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS)
202 return status;
203
204 /* Bits 32-43 of status have 'Reps completed' data. */
205 rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >>
206 HV_HYPERCALL_REP_COMP_OFFSET;
207
208 control &= ~HV_HYPERCALL_REP_START_MASK;
209 control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
210
211 touch_nmi_watchdog();
212 } while (rep_comp < rep_count);
213
214 return status;
215}
216
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100217extern struct hv_vp_assist_page **hv_vp_assist_page;
218
219static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
220{
221 if (!hv_vp_assist_page)
222 return NULL;
223
224 return hv_vp_assist_page[cpu];
225}
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200226
K. Y. Srinivasan6b48cb52018-05-16 14:53:30 -0700227void __init hyperv_init(void);
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200228void hyperv_setup_mmu_ops(void);
Maya Nakamura8c3e44b2019-07-12 08:21:25 +0000229void *hv_alloc_hyperv_page(void);
Himadri Pandyafa36dcd2019-07-30 09:49:43 +0000230void *hv_alloc_hyperv_zeroed_page(void);
Maya Nakamura8c3e44b2019-07-12 08:21:25 +0000231void hv_free_hyperv_page(unsigned long addr);
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100232void set_hv_tscchange_cb(void (*cb)(void));
233void clear_hv_tscchange_cb(void);
234void hyperv_stop_tsc_emulation(void);
Tianyu Laneb914cf2018-07-19 08:40:06 +0000235int hyperv_flush_guest_mapping(u64 as);
Lan Tianyucc4edae42018-12-06 21:21:05 +0800236int hyperv_flush_guest_mapping_range(u64 as,
237 hyperv_fill_flush_list_func fill_func, void *data);
238int hyperv_fill_flush_guest_mapping_list(
239 struct hv_guest_mapping_flush_list *flush,
240 u64 start_gfn, u64 end_gfn);
Thomas Gleixner2d2ccf242018-05-19 21:22:48 +0200241
242#ifdef CONFIG_X86_64
K. Y. Srinivasan6b48cb52018-05-16 14:53:30 -0700243void hv_apic_init(void);
Yi Sun3a025de2018-10-08 16:29:34 +0800244void __init hv_init_spinlocks(void);
245bool hv_vcpu_is_preempted(int vcpu);
Thomas Gleixner2d2ccf242018-05-19 21:22:48 +0200246#else
247static inline void hv_apic_init(void) {}
248#endif
249
Boqun Feng1cf106d2020-02-10 11:39:53 +0800250static inline void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry,
251 struct msi_desc *msi_desc)
252{
253 msi_entry->address = msi_desc->msg.address_lo;
254 msi_entry->data = msi_desc->msg.data;
255}
256
Vitaly Kuznetsov79cadff2017-08-02 18:09:13 +0200257#else /* CONFIG_HYPERV */
258static inline void hyperv_init(void) {}
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200259static inline void hyperv_setup_mmu_ops(void) {}
Maya Nakamura8c3e44b2019-07-12 08:21:25 +0000260static inline void *hv_alloc_hyperv_page(void) { return NULL; }
261static inline void hv_free_hyperv_page(unsigned long addr) {}
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100262static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
263static inline void clear_hv_tscchange_cb(void) {}
264static inline void hyperv_stop_tsc_emulation(void) {};
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100265static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
266{
267 return NULL;
268}
Tianyu Laneb914cf2018-07-19 08:40:06 +0000269static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
Lan Tianyucc4edae42018-12-06 21:21:05 +0800270static inline int hyperv_flush_guest_mapping_range(u64 as,
271 hyperv_fill_flush_list_func fill_func, void *data)
272{
273 return -1;
274}
Vitaly Kuznetsov79cadff2017-08-02 18:09:13 +0200275#endif /* CONFIG_HYPERV */
276
Michael Kelley765e33f2019-05-30 00:14:00 +0000277
278#include <asm-generic/mshyperv.h>
279
Ky Srinivasana2a47c62010-05-06 12:08:41 -0700280#endif