xref: /openbmc/linux/arch/x86/include/asm/mshyperv.h (revision d6e2d652)
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 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
52 int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
53 int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
54 
55 static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
56 {
57 	u64 input_address = input ? virt_to_phys(input) : 0;
58 	u64 output_address = output ? virt_to_phys(output) : 0;
59 	u64 hv_status;
60 
61 #ifdef CONFIG_X86_64
62 	if (!hv_hypercall_pg)
63 		return U64_MAX;
64 
65 	__asm__ __volatile__("mov %4, %%r8\n"
66 			     CALL_NOSPEC
67 			     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
68 			       "+c" (control), "+d" (input_address)
69 			     :  "r" (output_address),
70 				THUNK_TARGET(hv_hypercall_pg)
71 			     : "cc", "memory", "r8", "r9", "r10", "r11");
72 #else
73 	u32 input_address_hi = upper_32_bits(input_address);
74 	u32 input_address_lo = lower_32_bits(input_address);
75 	u32 output_address_hi = upper_32_bits(output_address);
76 	u32 output_address_lo = lower_32_bits(output_address);
77 
78 	if (!hv_hypercall_pg)
79 		return U64_MAX;
80 
81 	__asm__ __volatile__(CALL_NOSPEC
82 			     : "=A" (hv_status),
83 			       "+c" (input_address_lo), ASM_CALL_CONSTRAINT
84 			     : "A" (control),
85 			       "b" (input_address_hi),
86 			       "D"(output_address_hi), "S"(output_address_lo),
87 			       THUNK_TARGET(hv_hypercall_pg)
88 			     : "cc", "memory");
89 #endif /* !x86_64 */
90 	return hv_status;
91 }
92 
93 /* Hypercall to the L0 hypervisor */
94 static inline u64 hv_do_nested_hypercall(u64 control, void *input, void *output)
95 {
96 	return hv_do_hypercall(control | HV_HYPERCALL_NESTED, input, output);
97 }
98 
99 /* Fast hypercall with 8 bytes of input and no output */
100 static inline u64 _hv_do_fast_hypercall8(u64 control, u64 input1)
101 {
102 	u64 hv_status;
103 
104 #ifdef CONFIG_X86_64
105 	{
106 		__asm__ __volatile__(CALL_NOSPEC
107 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
108 				       "+c" (control), "+d" (input1)
109 				     : THUNK_TARGET(hv_hypercall_pg)
110 				     : "cc", "r8", "r9", "r10", "r11");
111 	}
112 #else
113 	{
114 		u32 input1_hi = upper_32_bits(input1);
115 		u32 input1_lo = lower_32_bits(input1);
116 
117 		__asm__ __volatile__ (CALL_NOSPEC
118 				      : "=A"(hv_status),
119 					"+c"(input1_lo),
120 					ASM_CALL_CONSTRAINT
121 				      :	"A" (control),
122 					"b" (input1_hi),
123 					THUNK_TARGET(hv_hypercall_pg)
124 				      : "cc", "edi", "esi");
125 	}
126 #endif
127 		return hv_status;
128 }
129 
130 static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
131 {
132 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
133 
134 	return _hv_do_fast_hypercall8(control, input1);
135 }
136 
137 static inline u64 hv_do_fast_nested_hypercall8(u16 code, u64 input1)
138 {
139 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
140 
141 	return _hv_do_fast_hypercall8(control, input1);
142 }
143 
144 /* Fast hypercall with 16 bytes of input */
145 static inline u64 _hv_do_fast_hypercall16(u64 control, u64 input1, u64 input2)
146 {
147 	u64 hv_status;
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
175 	return hv_status;
176 }
177 
178 static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
179 {
180 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
181 
182 	return _hv_do_fast_hypercall16(control, input1, input2);
183 }
184 
185 static inline u64 hv_do_fast_nested_hypercall16(u16 code, u64 input1, u64 input2)
186 {
187 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
188 
189 	return _hv_do_fast_hypercall16(control, input1, input2);
190 }
191 
192 extern struct hv_vp_assist_page **hv_vp_assist_page;
193 
194 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
195 {
196 	if (!hv_vp_assist_page)
197 		return NULL;
198 
199 	return hv_vp_assist_page[cpu];
200 }
201 
202 void __init hyperv_init(void);
203 void hyperv_setup_mmu_ops(void);
204 void set_hv_tscchange_cb(void (*cb)(void));
205 void clear_hv_tscchange_cb(void);
206 void hyperv_stop_tsc_emulation(void);
207 int hyperv_flush_guest_mapping(u64 as);
208 int hyperv_flush_guest_mapping_range(u64 as,
209 		hyperv_fill_flush_list_func fill_func, void *data);
210 int hyperv_fill_flush_guest_mapping_list(
211 		struct hv_guest_mapping_flush_list *flush,
212 		u64 start_gfn, u64 end_gfn);
213 
214 #ifdef CONFIG_X86_64
215 void hv_apic_init(void);
216 void __init hv_init_spinlocks(void);
217 bool hv_vcpu_is_preempted(int vcpu);
218 #else
219 static inline void hv_apic_init(void) {}
220 #endif
221 
222 struct irq_domain *hv_create_pci_msi_domain(void);
223 
224 int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
225 		struct hv_interrupt_entry *entry);
226 int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
227 
228 #ifdef CONFIG_AMD_MEM_ENCRYPT
229 void hv_ghcb_msr_write(u64 msr, u64 value);
230 void hv_ghcb_msr_read(u64 msr, u64 *value);
231 bool hv_ghcb_negotiate_protocol(void);
232 void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason);
233 void hv_vtom_init(void);
234 #else
235 static inline void hv_ghcb_msr_write(u64 msr, u64 value) {}
236 static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {}
237 static inline bool hv_ghcb_negotiate_protocol(void) { return false; }
238 static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {}
239 static inline void hv_vtom_init(void) {}
240 #endif
241 
242 extern bool hv_isolation_type_snp(void);
243 extern bool hv_isolation_type_en_snp(void);
244 
245 static inline bool hv_is_synic_reg(unsigned int reg)
246 {
247 	return (reg >= HV_REGISTER_SCONTROL) &&
248 	       (reg <= HV_REGISTER_SINT15);
249 }
250 
251 static inline bool hv_is_sint_reg(unsigned int reg)
252 {
253 	return (reg >= HV_REGISTER_SINT0) &&
254 	       (reg <= HV_REGISTER_SINT15);
255 }
256 
257 u64 hv_get_register(unsigned int reg);
258 void hv_set_register(unsigned int reg, u64 value);
259 u64 hv_get_non_nested_register(unsigned int reg);
260 void hv_set_non_nested_register(unsigned int reg, u64 value);
261 
262 static __always_inline u64 hv_raw_get_register(unsigned int reg)
263 {
264 	return __rdmsr(reg);
265 }
266 
267 #else /* CONFIG_HYPERV */
268 static inline void hyperv_init(void) {}
269 static inline void hyperv_setup_mmu_ops(void) {}
270 static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
271 static inline void clear_hv_tscchange_cb(void) {}
272 static inline void hyperv_stop_tsc_emulation(void) {};
273 static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
274 {
275 	return NULL;
276 }
277 static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
278 static inline int hyperv_flush_guest_mapping_range(u64 as,
279 		hyperv_fill_flush_list_func fill_func, void *data)
280 {
281 	return -1;
282 }
283 static inline void hv_set_register(unsigned int reg, u64 value) { }
284 static inline u64 hv_get_register(unsigned int reg) { return 0; }
285 static inline void hv_set_non_nested_register(unsigned int reg, u64 value) { }
286 static inline u64 hv_get_non_nested_register(unsigned int reg) { return 0; }
287 #endif /* CONFIG_HYPERV */
288 
289 
290 #ifdef CONFIG_HYPERV_VTL_MODE
291 void __init hv_vtl_init_platform(void);
292 #else
293 static inline void __init hv_vtl_init_platform(void) {}
294 #endif
295 
296 #include <asm-generic/mshyperv.h>
297 
298 #endif
299