blob: 81fc3024012229e9fc1b315dc85fe006069ef515 [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>
H. Peter Anvine08cae42010-05-07 16:57:28 -070011
Lan Tianyucc4edae42018-12-06 21:21:05 +080012typedef int (*hyperv_fill_flush_list_func)(
13 struct hv_guest_mapping_flush_list *flush,
14 void *data);
15
Michael Kelley619a4c82018-06-05 13:37:53 -070016#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)
K. Y. Srinivasand5116b42017-01-19 11:51:51 -070020
K. Y. Srinivasan155e4a22017-01-19 11:51:54 -070021#define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val)
22#define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val)
23
K. Y. Srinivasan8e307bf2017-01-19 11:51:55 -070024#define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val)
25#define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val)
26
K. Y. Srinivasan06d1d982017-01-19 11:51:56 -070027#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
K. Y. Srinivasan7297ff02017-01-19 11:51:57 -070030#define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index)
31
Michael Kelley765e33f2019-05-30 00:14:00 +000032#define hv_signal_eom() wrmsrl(HV_X64_MSR_EOM, 0)
33
Michael Kelley619a4c82018-06-05 13:37:53 -070034#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)
K. Y. Srinivasan37e11d52017-01-19 11:51:58 -070038
Sunil Muthuswamy9d9c9652018-07-28 21:58:47 +000039#define hv_get_crash_ctl(val) \
40 rdmsrl(HV_X64_MSR_CRASH_CTL, val)
Seiji Aguchicf910e82013-06-20 11:46:53 -040041
Michael Kelleydd2cb342019-07-01 04:26:06 +000042#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).archdata.vclock_mode = VCLOCK_HVCLOCK)
51#define hv_get_raw_timer() rdtsc_ordered()
52
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -080053void hyperv_callback_vector(void);
Vitaly Kuznetsov93286262018-01-24 14:23:33 +010054void hyperv_reenlightenment_vector(void);
Seiji Aguchicf910e82013-06-20 11:46:53 -040055#ifdef CONFIG_TRACING
56#define trace_hyperv_callback_vector hyperv_callback_vector
57#endif
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -080058void hyperv_vector_handler(struct pt_regs *regs);
K. Y. Srinivasan87300462017-01-18 16:45:02 -070059
Michael Kelley248e7422018-03-04 22:17:18 -070060/*
61 * Routines for stimer0 Direct Mode handling.
62 * On x86/x64, there are no percpu actions to take.
63 */
64void hv_stimer0_vector_handler(struct pt_regs *regs);
65void hv_stimer0_callback_vector(void);
Michael Kelley248e7422018-03-04 22:17:18 -070066
67static inline void hv_enable_stimer0_percpu_irq(int irq) {}
68static inline void hv_disable_stimer0_percpu_irq(int irq) {}
69
70
K. Y. Srinivasan87300462017-01-18 16:45:02 -070071#if IS_ENABLED(CONFIG_HYPERV)
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020072extern void *hv_hypercall_pg;
K. Y. Srinivasan68bb7bf2018-05-16 14:53:31 -070073extern void __percpu **hyperv_pcpu_input_arg;
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020074
75static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
76{
77 u64 input_address = input ? virt_to_phys(input) : 0;
78 u64 output_address = output ? virt_to_phys(output) : 0;
79 u64 hv_status;
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020080
81#ifdef CONFIG_X86_64
82 if (!hv_hypercall_pg)
83 return U64_MAX;
84
85 __asm__ __volatile__("mov %4, %%r8\n"
David Woodhousee70e5892018-01-11 21:46:30 +000086 CALL_NOSPEC
Josh Poimboeuff5caf622017-09-20 16:24:33 -050087 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020088 "+c" (control), "+d" (input_address)
David Woodhousee70e5892018-01-11 21:46:30 +000089 : "r" (output_address),
90 THUNK_TARGET(hv_hypercall_pg)
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020091 : "cc", "memory", "r8", "r9", "r10", "r11");
92#else
93 u32 input_address_hi = upper_32_bits(input_address);
94 u32 input_address_lo = lower_32_bits(input_address);
95 u32 output_address_hi = upper_32_bits(output_address);
96 u32 output_address_lo = lower_32_bits(output_address);
97
98 if (!hv_hypercall_pg)
99 return U64_MAX;
100
David Woodhousee70e5892018-01-11 21:46:30 +0000101 __asm__ __volatile__(CALL_NOSPEC
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200102 : "=A" (hv_status),
Josh Poimboeuff5caf622017-09-20 16:24:33 -0500103 "+c" (input_address_lo), ASM_CALL_CONSTRAINT
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200104 : "A" (control),
105 "b" (input_address_hi),
106 "D"(output_address_hi), "S"(output_address_lo),
David Woodhousee70e5892018-01-11 21:46:30 +0000107 THUNK_TARGET(hv_hypercall_pg)
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200108 : "cc", "memory");
109#endif /* !x86_64 */
110 return hv_status;
111}
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -0700112
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200113/* Fast hypercall with 8 bytes of input and no output */
114static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
115{
116 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200117
118#ifdef CONFIG_X86_64
119 {
David Woodhousee70e5892018-01-11 21:46:30 +0000120 __asm__ __volatile__(CALL_NOSPEC
Josh Poimboeuff5caf622017-09-20 16:24:33 -0500121 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200122 "+c" (control), "+d" (input1)
David Woodhousee70e5892018-01-11 21:46:30 +0000123 : THUNK_TARGET(hv_hypercall_pg)
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200124 : "cc", "r8", "r9", "r10", "r11");
125 }
126#else
127 {
128 u32 input1_hi = upper_32_bits(input1);
129 u32 input1_lo = lower_32_bits(input1);
130
David Woodhousee70e5892018-01-11 21:46:30 +0000131 __asm__ __volatile__ (CALL_NOSPEC
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200132 : "=A"(hv_status),
133 "+c"(input1_lo),
Josh Poimboeuff5caf622017-09-20 16:24:33 -0500134 ASM_CALL_CONSTRAINT
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200135 : "A" (control),
136 "b" (input1_hi),
David Woodhousee70e5892018-01-11 21:46:30 +0000137 THUNK_TARGET(hv_hypercall_pg)
Vitaly Kuznetsov6a8edbd2017-08-02 18:09:15 +0200138 : "cc", "edi", "esi");
139 }
140#endif
141 return hv_status;
142}
143
Vitaly Kuznetsov53e52962018-06-22 19:06:22 +0200144/* Fast hypercall with 16 bytes of input */
145static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
146{
147 u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
148
149#ifdef CONFIG_X86_64
150 {
151 __asm__ __volatile__("mov %4, %%r8\n"
152 CALL_NOSPEC
153 : "=a" (hv_status), ASM_CALL_CONSTRAINT,
154 "+c" (control), "+d" (input1)
155 : "r" (input2),
156 THUNK_TARGET(hv_hypercall_pg)
157 : "cc", "r8", "r9", "r10", "r11");
158 }
159#else
160 {
161 u32 input1_hi = upper_32_bits(input1);
162 u32 input1_lo = lower_32_bits(input1);
163 u32 input2_hi = upper_32_bits(input2);
164 u32 input2_lo = lower_32_bits(input2);
165
166 __asm__ __volatile__ (CALL_NOSPEC
167 : "=A"(hv_status),
168 "+c"(input1_lo), ASM_CALL_CONSTRAINT
169 : "A" (control), "b" (input1_hi),
170 "D"(input2_hi), "S"(input2_lo),
171 THUNK_TARGET(hv_hypercall_pg)
172 : "cc");
173 }
174#endif
Yi Wangb42967d2018-10-29 15:17:31 +0800175 return hv_status;
Vitaly Kuznetsov53e52962018-06-22 19:06:22 +0200176}
177
Vitaly Kuznetsov806c8922017-08-02 18:09:17 +0200178/*
179 * Rep hypercalls. Callers of this functions are supposed to ensure that
180 * rep_count and varhead_size comply with Hyper-V hypercall definition.
181 */
182static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
183 void *input, void *output)
184{
185 u64 control = code;
186 u64 status;
187 u16 rep_comp;
188
189 control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
190 control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
191
192 do {
193 status = hv_do_hypercall(control, input, output);
194 if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS)
195 return status;
196
197 /* Bits 32-43 of status have 'Reps completed' data. */
198 rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >>
199 HV_HYPERCALL_REP_COMP_OFFSET;
200
201 control &= ~HV_HYPERCALL_REP_START_MASK;
202 control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
203
204 touch_nmi_watchdog();
205 } while (rep_comp < rep_count);
206
207 return status;
208}
209
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100210extern struct hv_vp_assist_page **hv_vp_assist_page;
211
212static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
213{
214 if (!hv_vp_assist_page)
215 return NULL;
216
217 return hv_vp_assist_page[cpu];
218}
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200219
K. Y. Srinivasan6b48cb52018-05-16 14:53:30 -0700220void __init hyperv_init(void);
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200221void hyperv_setup_mmu_ops(void);
Maya Nakamura8c3e44b2019-07-12 08:21:25 +0000222void *hv_alloc_hyperv_page(void);
Himadri Pandyafa36dcd2019-07-30 09:49:43 +0000223void *hv_alloc_hyperv_zeroed_page(void);
Maya Nakamura8c3e44b2019-07-12 08:21:25 +0000224void hv_free_hyperv_page(unsigned long addr);
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100225void hyperv_reenlightenment_intr(struct pt_regs *regs);
226void set_hv_tscchange_cb(void (*cb)(void));
227void clear_hv_tscchange_cb(void);
228void hyperv_stop_tsc_emulation(void);
Tianyu Laneb914cf2018-07-19 08:40:06 +0000229int hyperv_flush_guest_mapping(u64 as);
Lan Tianyucc4edae42018-12-06 21:21:05 +0800230int hyperv_flush_guest_mapping_range(u64 as,
231 hyperv_fill_flush_list_func fill_func, void *data);
232int hyperv_fill_flush_guest_mapping_list(
233 struct hv_guest_mapping_flush_list *flush,
234 u64 start_gfn, u64 end_gfn);
Thomas Gleixner2d2ccf242018-05-19 21:22:48 +0200235
236#ifdef CONFIG_X86_64
K. Y. Srinivasan6b48cb52018-05-16 14:53:30 -0700237void hv_apic_init(void);
Yi Sun3a025de2018-10-08 16:29:34 +0800238void __init hv_init_spinlocks(void);
239bool hv_vcpu_is_preempted(int vcpu);
Thomas Gleixner2d2ccf242018-05-19 21:22:48 +0200240#else
241static inline void hv_apic_init(void) {}
242#endif
243
Boqun Feng1cf106d2020-02-10 11:39:53 +0800244static inline void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry,
245 struct msi_desc *msi_desc)
246{
247 msi_entry->address = msi_desc->msg.address_lo;
248 msi_entry->data = msi_desc->msg.data;
249}
250
Vitaly Kuznetsov79cadff2017-08-02 18:09:13 +0200251#else /* CONFIG_HYPERV */
252static inline void hyperv_init(void) {}
Vitaly Kuznetsov2ffd9e32017-08-02 18:09:19 +0200253static inline void hyperv_setup_mmu_ops(void) {}
Maya Nakamura8c3e44b2019-07-12 08:21:25 +0000254static inline void *hv_alloc_hyperv_page(void) { return NULL; }
255static inline void hv_free_hyperv_page(unsigned long addr) {}
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100256static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
257static inline void clear_hv_tscchange_cb(void) {}
258static inline void hyperv_stop_tsc_emulation(void) {};
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100259static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
260{
261 return NULL;
262}
Tianyu Laneb914cf2018-07-19 08:40:06 +0000263static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
Lan Tianyucc4edae42018-12-06 21:21:05 +0800264static inline int hyperv_flush_guest_mapping_range(u64 as,
265 hyperv_fill_flush_list_func fill_func, void *data)
266{
267 return -1;
268}
Vitaly Kuznetsov79cadff2017-08-02 18:09:13 +0200269#endif /* CONFIG_HYPERV */
270
Michael Kelley765e33f2019-05-30 00:14:00 +0000271
272#include <asm-generic/mshyperv.h>
273
Ky Srinivasana2a47c62010-05-06 12:08:41 -0700274#endif