xref: /openbmc/linux/arch/x86/include/asm/mshyperv.h (revision 44676bb9)
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 #include <asm/mshyperv.h>
13 
14 /*
15  * Hyper-V always provides a single IO-APIC at this MMIO address.
16  * Ideally, the value should be looked up in ACPI tables, but it
17  * is needed for mapping the IO-APIC early in boot on Confidential
18  * VMs, before ACPI functions can be used.
19  */
20 #define HV_IOAPIC_BASE_ADDRESS 0xfec00000
21 
22 #define HV_VTL_NORMAL 0x0
23 #define HV_VTL_SECURE 0x1
24 #define HV_VTL_MGMT   0x2
25 
26 union hv_ghcb;
27 
28 DECLARE_STATIC_KEY_FALSE(isolation_type_snp);
29 DECLARE_STATIC_KEY_FALSE(isolation_type_en_snp);
30 
31 typedef int (*hyperv_fill_flush_list_func)(
32 		struct hv_guest_mapping_flush_list *flush,
33 		void *data);
34 
35 void hyperv_vector_handler(struct pt_regs *regs);
36 
37 static inline unsigned char hv_get_nmi_reason(void)
38 {
39 	return 0;
40 }
41 
42 #if IS_ENABLED(CONFIG_HYPERV)
43 extern int hyperv_init_cpuhp;
44 
45 extern void *hv_hypercall_pg;
46 
47 extern u64 hv_current_partition_id;
48 
49 extern union hv_ghcb * __percpu *hv_ghcb_pg;
50 
51 extern bool hv_isolation_type_en_snp(void);
52 /*
53  * DEFAULT INIT GPAT and SEGMENT LIMIT value in struct VMSA
54  * to start AP in enlightened SEV guest.
55  */
56 #define HV_AP_INIT_GPAT_DEFAULT		0x0007040600070406ULL
57 #define HV_AP_SEGMENT_LIMIT		0xffffffff
58 
59 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
60 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
61 int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
62 
63 static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
64 {
65 	u64 input_address = input ? virt_to_phys(input) : 0;
66 	u64 output_address = output ? virt_to_phys(output) : 0;
67 	u64 hv_status;
68 
69 #ifdef CONFIG_X86_64
70 	if (hv_isolation_type_en_snp()) {
71 		__asm__ __volatile__("mov %4, %%r8\n"
72 				     "vmmcall"
73 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
74 				       "+c" (control), "+d" (input_address)
75 				     :  "r" (output_address)
76 				     : "cc", "memory", "r8", "r9", "r10", "r11");
77 		return hv_status;
78 	}
79 
80 	if (!hv_hypercall_pg)
81 		return U64_MAX;
82 
83 	__asm__ __volatile__("mov %4, %%r8\n"
84 			     CALL_NOSPEC
85 			     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
86 			       "+c" (control), "+d" (input_address)
87 			     :  "r" (output_address),
88 				THUNK_TARGET(hv_hypercall_pg)
89 			     : "cc", "memory", "r8", "r9", "r10", "r11");
90 #else
91 	u32 input_address_hi = upper_32_bits(input_address);
92 	u32 input_address_lo = lower_32_bits(input_address);
93 	u32 output_address_hi = upper_32_bits(output_address);
94 	u32 output_address_lo = lower_32_bits(output_address);
95 
96 	if (!hv_hypercall_pg)
97 		return U64_MAX;
98 
99 	__asm__ __volatile__(CALL_NOSPEC
100 			     : "=A" (hv_status),
101 			       "+c" (input_address_lo), ASM_CALL_CONSTRAINT
102 			     : "A" (control),
103 			       "b" (input_address_hi),
104 			       "D"(output_address_hi), "S"(output_address_lo),
105 			       THUNK_TARGET(hv_hypercall_pg)
106 			     : "cc", "memory");
107 #endif /* !x86_64 */
108 	return hv_status;
109 }
110 
111 /* Hypercall to the L0 hypervisor */
112 static inline u64 hv_do_nested_hypercall(u64 control, void *input, void *output)
113 {
114 	return hv_do_hypercall(control | HV_HYPERCALL_NESTED, input, output);
115 }
116 
117 /* Fast hypercall with 8 bytes of input and no output */
118 static inline u64 _hv_do_fast_hypercall8(u64 control, u64 input1)
119 {
120 	u64 hv_status;
121 
122 #ifdef CONFIG_X86_64
123 	if (hv_isolation_type_en_snp()) {
124 		__asm__ __volatile__(
125 				"vmmcall"
126 				: "=a" (hv_status), ASM_CALL_CONSTRAINT,
127 				"+c" (control), "+d" (input1)
128 				:: "cc", "r8", "r9", "r10", "r11");
129 	} else {
130 		__asm__ __volatile__(CALL_NOSPEC
131 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
132 				       "+c" (control), "+d" (input1)
133 				     : THUNK_TARGET(hv_hypercall_pg)
134 				     : "cc", "r8", "r9", "r10", "r11");
135 	}
136 #else
137 	{
138 		u32 input1_hi = upper_32_bits(input1);
139 		u32 input1_lo = lower_32_bits(input1);
140 
141 		__asm__ __volatile__ (CALL_NOSPEC
142 				      : "=A"(hv_status),
143 					"+c"(input1_lo),
144 					ASM_CALL_CONSTRAINT
145 				      :	"A" (control),
146 					"b" (input1_hi),
147 					THUNK_TARGET(hv_hypercall_pg)
148 				      : "cc", "edi", "esi");
149 	}
150 #endif
151 		return hv_status;
152 }
153 
154 static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
155 {
156 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
157 
158 	return _hv_do_fast_hypercall8(control, input1);
159 }
160 
161 static inline u64 hv_do_fast_nested_hypercall8(u16 code, u64 input1)
162 {
163 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
164 
165 	return _hv_do_fast_hypercall8(control, input1);
166 }
167 
168 /* Fast hypercall with 16 bytes of input */
169 static inline u64 _hv_do_fast_hypercall16(u64 control, u64 input1, u64 input2)
170 {
171 	u64 hv_status;
172 
173 #ifdef CONFIG_X86_64
174 	if (hv_isolation_type_en_snp()) {
175 		__asm__ __volatile__("mov %4, %%r8\n"
176 				     "vmmcall"
177 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
178 				       "+c" (control), "+d" (input1)
179 				     : "r" (input2)
180 				     : "cc", "r8", "r9", "r10", "r11");
181 	} else {
182 		__asm__ __volatile__("mov %4, %%r8\n"
183 				     CALL_NOSPEC
184 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
185 				       "+c" (control), "+d" (input1)
186 				     : "r" (input2),
187 				       THUNK_TARGET(hv_hypercall_pg)
188 				     : "cc", "r8", "r9", "r10", "r11");
189 	}
190 #else
191 	{
192 		u32 input1_hi = upper_32_bits(input1);
193 		u32 input1_lo = lower_32_bits(input1);
194 		u32 input2_hi = upper_32_bits(input2);
195 		u32 input2_lo = lower_32_bits(input2);
196 
197 		__asm__ __volatile__ (CALL_NOSPEC
198 				      : "=A"(hv_status),
199 					"+c"(input1_lo), ASM_CALL_CONSTRAINT
200 				      :	"A" (control), "b" (input1_hi),
201 					"D"(input2_hi), "S"(input2_lo),
202 					THUNK_TARGET(hv_hypercall_pg)
203 				      : "cc");
204 	}
205 #endif
206 	return hv_status;
207 }
208 
209 static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
210 {
211 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
212 
213 	return _hv_do_fast_hypercall16(control, input1, input2);
214 }
215 
216 static inline u64 hv_do_fast_nested_hypercall16(u16 code, u64 input1, u64 input2)
217 {
218 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
219 
220 	return _hv_do_fast_hypercall16(control, input1, input2);
221 }
222 
223 extern struct hv_vp_assist_page **hv_vp_assist_page;
224 
225 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
226 {
227 	if (!hv_vp_assist_page)
228 		return NULL;
229 
230 	return hv_vp_assist_page[cpu];
231 }
232 
233 void __init hyperv_init(void);
234 void hyperv_setup_mmu_ops(void);
235 void set_hv_tscchange_cb(void (*cb)(void));
236 void clear_hv_tscchange_cb(void);
237 void hyperv_stop_tsc_emulation(void);
238 int hyperv_flush_guest_mapping(u64 as);
239 int hyperv_flush_guest_mapping_range(u64 as,
240 		hyperv_fill_flush_list_func fill_func, void *data);
241 int hyperv_fill_flush_guest_mapping_list(
242 		struct hv_guest_mapping_flush_list *flush,
243 		u64 start_gfn, u64 end_gfn);
244 
245 #ifdef CONFIG_X86_64
246 void hv_apic_init(void);
247 void __init hv_init_spinlocks(void);
248 bool hv_vcpu_is_preempted(int vcpu);
249 #else
250 static inline void hv_apic_init(void) {}
251 #endif
252 
253 struct irq_domain *hv_create_pci_msi_domain(void);
254 
255 int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
256 		struct hv_interrupt_entry *entry);
257 int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
258 
259 #ifdef CONFIG_AMD_MEM_ENCRYPT
260 void hv_ghcb_msr_write(u64 msr, u64 value);
261 void hv_ghcb_msr_read(u64 msr, u64 *value);
262 bool hv_ghcb_negotiate_protocol(void);
263 void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason);
264 void hv_vtom_init(void);
265 int hv_snp_boot_ap(int cpu, unsigned long start_ip);
266 #else
267 static inline void hv_ghcb_msr_write(u64 msr, u64 value) {}
268 static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {}
269 static inline bool hv_ghcb_negotiate_protocol(void) { return false; }
270 static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {}
271 static inline void hv_vtom_init(void) {}
272 static int hv_snp_boot_ap(int cpu, unsigned long start_ip) { return 0; }
273 #endif
274 
275 extern bool hv_isolation_type_snp(void);
276 
277 static inline bool hv_is_synic_reg(unsigned int reg)
278 {
279 	return (reg >= HV_REGISTER_SCONTROL) &&
280 	       (reg <= HV_REGISTER_SINT15);
281 }
282 
283 static inline bool hv_is_sint_reg(unsigned int reg)
284 {
285 	return (reg >= HV_REGISTER_SINT0) &&
286 	       (reg <= HV_REGISTER_SINT15);
287 }
288 
289 u64 hv_get_register(unsigned int reg);
290 void hv_set_register(unsigned int reg, u64 value);
291 u64 hv_get_non_nested_register(unsigned int reg);
292 void hv_set_non_nested_register(unsigned int reg, u64 value);
293 
294 static __always_inline u64 hv_raw_get_register(unsigned int reg)
295 {
296 	return __rdmsr(reg);
297 }
298 
299 #else /* CONFIG_HYPERV */
300 static inline void hyperv_init(void) {}
301 static inline void hyperv_setup_mmu_ops(void) {}
302 static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
303 static inline void clear_hv_tscchange_cb(void) {}
304 static inline void hyperv_stop_tsc_emulation(void) {};
305 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
306 {
307 	return NULL;
308 }
309 static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
310 static inline int hyperv_flush_guest_mapping_range(u64 as,
311 		hyperv_fill_flush_list_func fill_func, void *data)
312 {
313 	return -1;
314 }
315 static inline void hv_set_register(unsigned int reg, u64 value) { }
316 static inline u64 hv_get_register(unsigned int reg) { return 0; }
317 static inline void hv_set_non_nested_register(unsigned int reg, u64 value) { }
318 static inline u64 hv_get_non_nested_register(unsigned int reg) { return 0; }
319 #endif /* CONFIG_HYPERV */
320 
321 
322 #ifdef CONFIG_HYPERV_VTL_MODE
323 void __init hv_vtl_init_platform(void);
324 #else
325 static inline void __init hv_vtl_init_platform(void) {}
326 #endif
327 
328 #include <asm-generic/mshyperv.h>
329 
330 #endif
331