xref: /openbmc/linux/arch/x86/include/asm/mshyperv.h (revision 31e67366)
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 #include <asm/paravirt.h>
12 
13 typedef int (*hyperv_fill_flush_list_func)(
14 		struct hv_guest_mapping_flush_list *flush,
15 		void *data);
16 
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)
21 
22 #define hv_get_simp(val) rdmsrl(HV_X64_MSR_SIMP, val)
23 #define hv_set_simp(val) wrmsrl(HV_X64_MSR_SIMP, val)
24 
25 #define hv_get_siefp(val) rdmsrl(HV_X64_MSR_SIEFP, val)
26 #define hv_set_siefp(val) wrmsrl(HV_X64_MSR_SIEFP, val)
27 
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)
30 
31 #define hv_get_vp_index(index) rdmsrl(HV_X64_MSR_VP_INDEX, index)
32 
33 #define hv_signal_eom() wrmsrl(HV_X64_MSR_EOM, 0)
34 
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))
41 
42 #define hv_get_crash_ctl(val) \
43 	rdmsrl(HV_X64_MSR_CRASH_CTL, val)
44 
45 #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) \
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
58 
59 /*
60  * Reference to pv_ops must be inline so objtool
61  * detection of noinstr violations can work correctly.
62  */
63 static __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 
70 void hyperv_vector_handler(struct pt_regs *regs);
71 
72 static inline void hv_enable_stimer0_percpu_irq(int irq) {}
73 static inline void hv_disable_stimer0_percpu_irq(int irq) {}
74 
75 
76 #if IS_ENABLED(CONFIG_HYPERV)
77 extern int hyperv_init_cpuhp;
78 
79 extern void *hv_hypercall_pg;
80 extern void  __percpu  **hyperv_pcpu_input_arg;
81 extern void  __percpu  **hyperv_pcpu_output_arg;
82 
83 extern u64 hv_current_partition_id;
84 
85 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
86 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
87 int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
88 
89 static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
90 {
91 	u64 input_address = input ? virt_to_phys(input) : 0;
92 	u64 output_address = output ? virt_to_phys(output) : 0;
93 	u64 hv_status;
94 
95 #ifdef CONFIG_X86_64
96 	if (!hv_hypercall_pg)
97 		return U64_MAX;
98 
99 	__asm__ __volatile__("mov %4, %%r8\n"
100 			     CALL_NOSPEC
101 			     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
102 			       "+c" (control), "+d" (input_address)
103 			     :  "r" (output_address),
104 				THUNK_TARGET(hv_hypercall_pg)
105 			     : "cc", "memory", "r8", "r9", "r10", "r11");
106 #else
107 	u32 input_address_hi = upper_32_bits(input_address);
108 	u32 input_address_lo = lower_32_bits(input_address);
109 	u32 output_address_hi = upper_32_bits(output_address);
110 	u32 output_address_lo = lower_32_bits(output_address);
111 
112 	if (!hv_hypercall_pg)
113 		return U64_MAX;
114 
115 	__asm__ __volatile__(CALL_NOSPEC
116 			     : "=A" (hv_status),
117 			       "+c" (input_address_lo), ASM_CALL_CONSTRAINT
118 			     : "A" (control),
119 			       "b" (input_address_hi),
120 			       "D"(output_address_hi), "S"(output_address_lo),
121 			       THUNK_TARGET(hv_hypercall_pg)
122 			     : "cc", "memory");
123 #endif /* !x86_64 */
124 	return hv_status;
125 }
126 
127 /* Fast hypercall with 8 bytes of input and no output */
128 static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
129 {
130 	u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
131 
132 #ifdef CONFIG_X86_64
133 	{
134 		__asm__ __volatile__(CALL_NOSPEC
135 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
136 				       "+c" (control), "+d" (input1)
137 				     : THUNK_TARGET(hv_hypercall_pg)
138 				     : "cc", "r8", "r9", "r10", "r11");
139 	}
140 #else
141 	{
142 		u32 input1_hi = upper_32_bits(input1);
143 		u32 input1_lo = lower_32_bits(input1);
144 
145 		__asm__ __volatile__ (CALL_NOSPEC
146 				      : "=A"(hv_status),
147 					"+c"(input1_lo),
148 					ASM_CALL_CONSTRAINT
149 				      :	"A" (control),
150 					"b" (input1_hi),
151 					THUNK_TARGET(hv_hypercall_pg)
152 				      : "cc", "edi", "esi");
153 	}
154 #endif
155 		return hv_status;
156 }
157 
158 /* Fast hypercall with 16 bytes of input */
159 static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
160 {
161 	u64 hv_status, control = (u64)code | HV_HYPERCALL_FAST_BIT;
162 
163 #ifdef CONFIG_X86_64
164 	{
165 		__asm__ __volatile__("mov %4, %%r8\n"
166 				     CALL_NOSPEC
167 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
168 				       "+c" (control), "+d" (input1)
169 				     : "r" (input2),
170 				       THUNK_TARGET(hv_hypercall_pg)
171 				     : "cc", "r8", "r9", "r10", "r11");
172 	}
173 #else
174 	{
175 		u32 input1_hi = upper_32_bits(input1);
176 		u32 input1_lo = lower_32_bits(input1);
177 		u32 input2_hi = upper_32_bits(input2);
178 		u32 input2_lo = lower_32_bits(input2);
179 
180 		__asm__ __volatile__ (CALL_NOSPEC
181 				      : "=A"(hv_status),
182 					"+c"(input1_lo), ASM_CALL_CONSTRAINT
183 				      :	"A" (control), "b" (input1_hi),
184 					"D"(input2_hi), "S"(input2_lo),
185 					THUNK_TARGET(hv_hypercall_pg)
186 				      : "cc");
187 	}
188 #endif
189 	return hv_status;
190 }
191 
192 /*
193  * Rep hypercalls. Callers of this functions are supposed to ensure that
194  * rep_count and varhead_size comply with Hyper-V hypercall definition.
195  */
196 static inline u64 hv_do_rep_hypercall(u16 code, u16 rep_count, u16 varhead_size,
197 				      void *input, void *output)
198 {
199 	u64 control = code;
200 	u64 status;
201 	u16 rep_comp;
202 
203 	control |= (u64)varhead_size << HV_HYPERCALL_VARHEAD_OFFSET;
204 	control |= (u64)rep_count << HV_HYPERCALL_REP_COMP_OFFSET;
205 
206 	do {
207 		status = hv_do_hypercall(control, input, output);
208 		if ((status & HV_HYPERCALL_RESULT_MASK) != HV_STATUS_SUCCESS)
209 			return status;
210 
211 		/* Bits 32-43 of status have 'Reps completed' data. */
212 		rep_comp = (status & HV_HYPERCALL_REP_COMP_MASK) >>
213 			HV_HYPERCALL_REP_COMP_OFFSET;
214 
215 		control &= ~HV_HYPERCALL_REP_START_MASK;
216 		control |= (u64)rep_comp << HV_HYPERCALL_REP_START_OFFSET;
217 
218 		touch_nmi_watchdog();
219 	} while (rep_comp < rep_count);
220 
221 	return status;
222 }
223 
224 extern struct hv_vp_assist_page **hv_vp_assist_page;
225 
226 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
227 {
228 	if (!hv_vp_assist_page)
229 		return NULL;
230 
231 	return hv_vp_assist_page[cpu];
232 }
233 
234 void __init hyperv_init(void);
235 void hyperv_setup_mmu_ops(void);
236 void *hv_alloc_hyperv_page(void);
237 void *hv_alloc_hyperv_zeroed_page(void);
238 void hv_free_hyperv_page(unsigned long addr);
239 void set_hv_tscchange_cb(void (*cb)(void));
240 void clear_hv_tscchange_cb(void);
241 void hyperv_stop_tsc_emulation(void);
242 int hyperv_flush_guest_mapping(u64 as);
243 int hyperv_flush_guest_mapping_range(u64 as,
244 		hyperv_fill_flush_list_func fill_func, void *data);
245 int hyperv_fill_flush_guest_mapping_list(
246 		struct hv_guest_mapping_flush_list *flush,
247 		u64 start_gfn, u64 end_gfn);
248 
249 extern bool hv_root_partition;
250 
251 #ifdef CONFIG_X86_64
252 void hv_apic_init(void);
253 void __init hv_init_spinlocks(void);
254 bool hv_vcpu_is_preempted(int vcpu);
255 #else
256 static inline void hv_apic_init(void) {}
257 #endif
258 
259 static inline void hv_set_msi_entry_from_desc(union hv_msi_entry *msi_entry,
260 					      struct msi_desc *msi_desc)
261 {
262 	msi_entry->address.as_uint32 = msi_desc->msg.address_lo;
263 	msi_entry->data.as_uint32 = msi_desc->msg.data;
264 }
265 
266 struct irq_domain *hv_create_pci_msi_domain(void);
267 
268 int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
269 		struct hv_interrupt_entry *entry);
270 int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
271 
272 #else /* CONFIG_HYPERV */
273 static inline void hyperv_init(void) {}
274 static inline void hyperv_setup_mmu_ops(void) {}
275 static inline void *hv_alloc_hyperv_page(void) { return NULL; }
276 static inline void hv_free_hyperv_page(unsigned long addr) {}
277 static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
278 static inline void clear_hv_tscchange_cb(void) {}
279 static inline void hyperv_stop_tsc_emulation(void) {};
280 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
281 {
282 	return NULL;
283 }
284 static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
285 static inline int hyperv_flush_guest_mapping_range(u64 as,
286 		hyperv_fill_flush_list_func fill_func, void *data)
287 {
288 	return -1;
289 }
290 #endif /* CONFIG_HYPERV */
291 
292 
293 #include <asm-generic/mshyperv.h>
294 
295 #endif
296