xref: /openbmc/linux/arch/x86/include/asm/mshyperv.h (revision d6e2d652443751e290b2edb70173ec3c22f78fbe)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2e08cae41SH. Peter Anvin #ifndef _ASM_X86_MSHYPER_H
3e08cae41SH. Peter Anvin #define _ASM_X86_MSHYPER_H
4a2a47c6cSKy Srinivasan 
5e08cae41SH. Peter Anvin #include <linux/types.h>
6806c8927SVitaly Kuznetsov #include <linux/nmi.h>
71cf106d9SBoqun Feng #include <linux/msi.h>
8fc53662fSVitaly Kuznetsov #include <asm/io.h>
95a485803SVitaly Kuznetsov #include <asm/hyperv-tlfs.h>
10e70e5892SDavid Woodhouse #include <asm/nospec-branch.h>
11b9d8cf2eSMichael Kelley #include <asm/paravirt.h>
12753ed9c9SJoseph Salisbury #include <asm/mshyperv.h>
13e08cae41SH. Peter Anvin 
14812b0597SMichael Kelley /*
15812b0597SMichael Kelley  * Hyper-V always provides a single IO-APIC at this MMIO address.
16812b0597SMichael Kelley  * Ideally, the value should be looked up in ACPI tables, but it
17812b0597SMichael Kelley  * is needed for mapping the IO-APIC early in boot on Confidential
18812b0597SMichael Kelley  * VMs, before ACPI functions can be used.
19812b0597SMichael Kelley  */
20812b0597SMichael Kelley #define HV_IOAPIC_BASE_ADDRESS 0xfec00000
21812b0597SMichael Kelley 
223be1bc2fSSaurabh Sengar #define HV_VTL_NORMAL 0x0
233be1bc2fSSaurabh Sengar #define HV_VTL_SECURE 0x1
243be1bc2fSSaurabh Sengar #define HV_VTL_MGMT   0x2
253be1bc2fSSaurabh Sengar 
26faff4406STianyu Lan union hv_ghcb;
27faff4406STianyu Lan 
280cc4f6d9STianyu Lan DECLARE_STATIC_KEY_FALSE(isolation_type_snp);
29*d6e2d652STianyu Lan DECLARE_STATIC_KEY_FALSE(isolation_type_en_snp);
300cc4f6d9STianyu Lan 
31cc4edae4SLan Tianyu typedef int (*hyperv_fill_flush_list_func)(
32cc4edae4SLan Tianyu 		struct hv_guest_mapping_flush_list *flush,
33cc4edae4SLan Tianyu 		void *data);
34cc4edae4SLan Tianyu 
35bc2b0331SK. Y. Srinivasan void hyperv_vector_handler(struct pt_regs *regs);
368730046cSK. Y. Srinivasan 
370a7a0058SSaurabh Sengar static inline unsigned char hv_get_nmi_reason(void)
380a7a0058SSaurabh Sengar {
390a7a0058SSaurabh Sengar 	return 0;
400a7a0058SSaurabh Sengar }
410a7a0058SSaurabh Sengar 
428730046cSK. Y. Srinivasan #if IS_ENABLED(CONFIG_HYPERV)
43dfe94d40SDexuan Cui extern int hyperv_init_cpuhp;
44dfe94d40SDexuan Cui 
45fc53662fSVitaly Kuznetsov extern void *hv_hypercall_pg;
46fc53662fSVitaly Kuznetsov 
4799a0f46aSWei Liu extern u64 hv_current_partition_id;
4899a0f46aSWei Liu 
49e1878402SMichael Kelley extern union hv_ghcb * __percpu *hv_ghcb_pg;
500cc4f6d9STianyu Lan 
5186b5ec35SWei Liu int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
5286b5ec35SWei Liu int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
5386b5ec35SWei Liu int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
5486b5ec35SWei Liu 
55fc53662fSVitaly Kuznetsov static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
56fc53662fSVitaly Kuznetsov {
57fc53662fSVitaly Kuznetsov 	u64 input_address = input ? virt_to_phys(input) : 0;
58fc53662fSVitaly Kuznetsov 	u64 output_address = output ? virt_to_phys(output) : 0;
59fc53662fSVitaly Kuznetsov 	u64 hv_status;
60fc53662fSVitaly Kuznetsov 
61fc53662fSVitaly Kuznetsov #ifdef CONFIG_X86_64
62fc53662fSVitaly Kuznetsov 	if (!hv_hypercall_pg)
63fc53662fSVitaly Kuznetsov 		return U64_MAX;
64fc53662fSVitaly Kuznetsov 
65fc53662fSVitaly Kuznetsov 	__asm__ __volatile__("mov %4, %%r8\n"
66e70e5892SDavid Woodhouse 			     CALL_NOSPEC
67f5caf621SJosh Poimboeuf 			     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
68fc53662fSVitaly Kuznetsov 			       "+c" (control), "+d" (input_address)
69e70e5892SDavid Woodhouse 			     :  "r" (output_address),
70e70e5892SDavid Woodhouse 				THUNK_TARGET(hv_hypercall_pg)
71fc53662fSVitaly Kuznetsov 			     : "cc", "memory", "r8", "r9", "r10", "r11");
72fc53662fSVitaly Kuznetsov #else
73fc53662fSVitaly Kuznetsov 	u32 input_address_hi = upper_32_bits(input_address);
74fc53662fSVitaly Kuznetsov 	u32 input_address_lo = lower_32_bits(input_address);
75fc53662fSVitaly Kuznetsov 	u32 output_address_hi = upper_32_bits(output_address);
76fc53662fSVitaly Kuznetsov 	u32 output_address_lo = lower_32_bits(output_address);
77fc53662fSVitaly Kuznetsov 
78fc53662fSVitaly Kuznetsov 	if (!hv_hypercall_pg)
79fc53662fSVitaly Kuznetsov 		return U64_MAX;
80fc53662fSVitaly Kuznetsov 
81e70e5892SDavid Woodhouse 	__asm__ __volatile__(CALL_NOSPEC
82fc53662fSVitaly Kuznetsov 			     : "=A" (hv_status),
83f5caf621SJosh Poimboeuf 			       "+c" (input_address_lo), ASM_CALL_CONSTRAINT
84fc53662fSVitaly Kuznetsov 			     : "A" (control),
85fc53662fSVitaly Kuznetsov 			       "b" (input_address_hi),
86fc53662fSVitaly Kuznetsov 			       "D"(output_address_hi), "S"(output_address_lo),
87e70e5892SDavid Woodhouse 			       THUNK_TARGET(hv_hypercall_pg)
88fc53662fSVitaly Kuznetsov 			     : "cc", "memory");
89fc53662fSVitaly Kuznetsov #endif /* !x86_64 */
90fc53662fSVitaly Kuznetsov 	return hv_status;
91fc53662fSVitaly Kuznetsov }
92dee863b5SVitaly Kuznetsov 
93f0d2f5c2SJinank Jain /* Hypercall to the L0 hypervisor */
94f0d2f5c2SJinank Jain static inline u64 hv_do_nested_hypercall(u64 control, void *input, void *output)
956a8edbd0SVitaly Kuznetsov {
96f0d2f5c2SJinank Jain 	return hv_do_hypercall(control | HV_HYPERCALL_NESTED, input, output);
97f0d2f5c2SJinank Jain }
98f0d2f5c2SJinank Jain 
99f0d2f5c2SJinank Jain /* Fast hypercall with 8 bytes of input and no output */
100f0d2f5c2SJinank Jain static inline u64 _hv_do_fast_hypercall8(u64 control, u64 input1)
101f0d2f5c2SJinank Jain {
102f0d2f5c2SJinank Jain 	u64 hv_status;
1036a8edbd0SVitaly Kuznetsov 
1046a8edbd0SVitaly Kuznetsov #ifdef CONFIG_X86_64
1056a8edbd0SVitaly Kuznetsov 	{
106e70e5892SDavid Woodhouse 		__asm__ __volatile__(CALL_NOSPEC
107f5caf621SJosh Poimboeuf 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
1086a8edbd0SVitaly Kuznetsov 				       "+c" (control), "+d" (input1)
109e70e5892SDavid Woodhouse 				     : THUNK_TARGET(hv_hypercall_pg)
1106a8edbd0SVitaly Kuznetsov 				     : "cc", "r8", "r9", "r10", "r11");
1116a8edbd0SVitaly Kuznetsov 	}
1126a8edbd0SVitaly Kuznetsov #else
1136a8edbd0SVitaly Kuznetsov 	{
1146a8edbd0SVitaly Kuznetsov 		u32 input1_hi = upper_32_bits(input1);
1156a8edbd0SVitaly Kuznetsov 		u32 input1_lo = lower_32_bits(input1);
1166a8edbd0SVitaly Kuznetsov 
117e70e5892SDavid Woodhouse 		__asm__ __volatile__ (CALL_NOSPEC
1186a8edbd0SVitaly Kuznetsov 				      : "=A"(hv_status),
1196a8edbd0SVitaly Kuznetsov 					"+c"(input1_lo),
120f5caf621SJosh Poimboeuf 					ASM_CALL_CONSTRAINT
1216a8edbd0SVitaly Kuznetsov 				      :	"A" (control),
1226a8edbd0SVitaly Kuznetsov 					"b" (input1_hi),
123e70e5892SDavid Woodhouse 					THUNK_TARGET(hv_hypercall_pg)
1246a8edbd0SVitaly Kuznetsov 				      : "cc", "edi", "esi");
1256a8edbd0SVitaly Kuznetsov 	}
1266a8edbd0SVitaly Kuznetsov #endif
1276a8edbd0SVitaly Kuznetsov 		return hv_status;
1286a8edbd0SVitaly Kuznetsov }
1296a8edbd0SVitaly Kuznetsov 
130f0d2f5c2SJinank Jain static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
13153e52966SVitaly Kuznetsov {
132f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
133f0d2f5c2SJinank Jain 
134f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall8(control, input1);
135f0d2f5c2SJinank Jain }
136f0d2f5c2SJinank Jain 
137f0d2f5c2SJinank Jain static inline u64 hv_do_fast_nested_hypercall8(u16 code, u64 input1)
138f0d2f5c2SJinank Jain {
139f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
140f0d2f5c2SJinank Jain 
141f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall8(control, input1);
142f0d2f5c2SJinank Jain }
143f0d2f5c2SJinank Jain 
144f0d2f5c2SJinank Jain /* Fast hypercall with 16 bytes of input */
145f0d2f5c2SJinank Jain static inline u64 _hv_do_fast_hypercall16(u64 control, u64 input1, u64 input2)
146f0d2f5c2SJinank Jain {
147f0d2f5c2SJinank Jain 	u64 hv_status;
14853e52966SVitaly Kuznetsov 
14953e52966SVitaly Kuznetsov #ifdef CONFIG_X86_64
15053e52966SVitaly Kuznetsov 	{
15153e52966SVitaly Kuznetsov 		__asm__ __volatile__("mov %4, %%r8\n"
15253e52966SVitaly Kuznetsov 				     CALL_NOSPEC
15353e52966SVitaly Kuznetsov 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
15453e52966SVitaly Kuznetsov 				       "+c" (control), "+d" (input1)
15553e52966SVitaly Kuznetsov 				     : "r" (input2),
15653e52966SVitaly Kuznetsov 				       THUNK_TARGET(hv_hypercall_pg)
15753e52966SVitaly Kuznetsov 				     : "cc", "r8", "r9", "r10", "r11");
15853e52966SVitaly Kuznetsov 	}
15953e52966SVitaly Kuznetsov #else
16053e52966SVitaly Kuznetsov 	{
16153e52966SVitaly Kuznetsov 		u32 input1_hi = upper_32_bits(input1);
16253e52966SVitaly Kuznetsov 		u32 input1_lo = lower_32_bits(input1);
16353e52966SVitaly Kuznetsov 		u32 input2_hi = upper_32_bits(input2);
16453e52966SVitaly Kuznetsov 		u32 input2_lo = lower_32_bits(input2);
16553e52966SVitaly Kuznetsov 
16653e52966SVitaly Kuznetsov 		__asm__ __volatile__ (CALL_NOSPEC
16753e52966SVitaly Kuznetsov 				      : "=A"(hv_status),
16853e52966SVitaly Kuznetsov 					"+c"(input1_lo), ASM_CALL_CONSTRAINT
16953e52966SVitaly Kuznetsov 				      :	"A" (control), "b" (input1_hi),
17053e52966SVitaly Kuznetsov 					"D"(input2_hi), "S"(input2_lo),
17153e52966SVitaly Kuznetsov 					THUNK_TARGET(hv_hypercall_pg)
17253e52966SVitaly Kuznetsov 				      : "cc");
17353e52966SVitaly Kuznetsov 	}
17453e52966SVitaly Kuznetsov #endif
17553e52966SVitaly Kuznetsov 	return hv_status;
17653e52966SVitaly Kuznetsov }
17753e52966SVitaly Kuznetsov 
178f0d2f5c2SJinank Jain static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
179f0d2f5c2SJinank Jain {
180f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
181f0d2f5c2SJinank Jain 
182f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall16(control, input1, input2);
183f0d2f5c2SJinank Jain }
184f0d2f5c2SJinank Jain 
185f0d2f5c2SJinank Jain static inline u64 hv_do_fast_nested_hypercall16(u16 code, u64 input1, u64 input2)
186f0d2f5c2SJinank Jain {
187f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
188f0d2f5c2SJinank Jain 
189f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall16(control, input1, input2);
190f0d2f5c2SJinank Jain }
191f0d2f5c2SJinank Jain 
192a46d15ccSVitaly Kuznetsov extern struct hv_vp_assist_page **hv_vp_assist_page;
193a46d15ccSVitaly Kuznetsov 
194a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
195a46d15ccSVitaly Kuznetsov {
196a46d15ccSVitaly Kuznetsov 	if (!hv_vp_assist_page)
197a46d15ccSVitaly Kuznetsov 		return NULL;
198a46d15ccSVitaly Kuznetsov 
199a46d15ccSVitaly Kuznetsov 	return hv_vp_assist_page[cpu];
200a46d15ccSVitaly Kuznetsov }
2017415aea6SVitaly Kuznetsov 
2026b48cb5fSK. Y. Srinivasan void __init hyperv_init(void);
2032ffd9e33SVitaly Kuznetsov void hyperv_setup_mmu_ops(void);
20493286261SVitaly Kuznetsov void set_hv_tscchange_cb(void (*cb)(void));
20593286261SVitaly Kuznetsov void clear_hv_tscchange_cb(void);
20693286261SVitaly Kuznetsov void hyperv_stop_tsc_emulation(void);
207eb914cfeSTianyu Lan int hyperv_flush_guest_mapping(u64 as);
208cc4edae4SLan Tianyu int hyperv_flush_guest_mapping_range(u64 as,
209cc4edae4SLan Tianyu 		hyperv_fill_flush_list_func fill_func, void *data);
210cc4edae4SLan Tianyu int hyperv_fill_flush_guest_mapping_list(
211cc4edae4SLan Tianyu 		struct hv_guest_mapping_flush_list *flush,
212cc4edae4SLan Tianyu 		u64 start_gfn, u64 end_gfn);
2132d2ccf24SThomas Gleixner 
2142d2ccf24SThomas Gleixner #ifdef CONFIG_X86_64
2156b48cb5fSK. Y. Srinivasan void hv_apic_init(void);
2163a025de6SYi Sun void __init hv_init_spinlocks(void);
2173a025de6SYi Sun bool hv_vcpu_is_preempted(int vcpu);
2182d2ccf24SThomas Gleixner #else
2192d2ccf24SThomas Gleixner static inline void hv_apic_init(void) {}
2202d2ccf24SThomas Gleixner #endif
2212d2ccf24SThomas Gleixner 
222e39397d1SWei Liu struct irq_domain *hv_create_pci_msi_domain(void);
223e39397d1SWei Liu 
224fb5ef351SWei Liu int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
225fb5ef351SWei Liu 		struct hv_interrupt_entry *entry);
226fb5ef351SWei Liu int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
227faff4406STianyu Lan 
228faff4406STianyu Lan #ifdef CONFIG_AMD_MEM_ENCRYPT
229faff4406STianyu Lan void hv_ghcb_msr_write(u64 msr, u64 value);
230faff4406STianyu Lan void hv_ghcb_msr_read(u64 msr, u64 *value);
23149d6a3c0STianyu Lan bool hv_ghcb_negotiate_protocol(void);
232611d4c71SGuilherme G. Piccoli void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason);
233812b0597SMichael Kelley void hv_vtom_init(void);
234faff4406STianyu Lan #else
235faff4406STianyu Lan static inline void hv_ghcb_msr_write(u64 msr, u64 value) {}
236faff4406STianyu Lan static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {}
23749d6a3c0STianyu Lan static inline bool hv_ghcb_negotiate_protocol(void) { return false; }
23849d6a3c0STianyu Lan static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {}
239812b0597SMichael Kelley static inline void hv_vtom_init(void) {}
240faff4406STianyu Lan #endif
241faff4406STianyu Lan 
242faff4406STianyu Lan extern bool hv_isolation_type_snp(void);
243*d6e2d652STianyu Lan extern bool hv_isolation_type_en_snp(void);
244faff4406STianyu Lan 
245faff4406STianyu Lan static inline bool hv_is_synic_reg(unsigned int reg)
246faff4406STianyu Lan {
247b14033a3SNuno Das Neves 	return (reg >= HV_REGISTER_SCONTROL) &&
248b14033a3SNuno Das Neves 	       (reg <= HV_REGISTER_SINT15);
249b14033a3SNuno Das Neves }
250b14033a3SNuno Das Neves 
251b14033a3SNuno Das Neves static inline bool hv_is_sint_reg(unsigned int reg)
252b14033a3SNuno Das Neves {
253b14033a3SNuno Das Neves 	return (reg >= HV_REGISTER_SINT0) &&
254b14033a3SNuno Das Neves 	       (reg <= HV_REGISTER_SINT15);
255faff4406STianyu Lan }
256faff4406STianyu Lan 
2577fec185aSJinank Jain u64 hv_get_register(unsigned int reg);
2587fec185aSJinank Jain void hv_set_register(unsigned int reg, u64 value);
2597fec185aSJinank Jain u64 hv_get_non_nested_register(unsigned int reg);
2607fec185aSJinank Jain void hv_set_non_nested_register(unsigned int reg, u64 value);
261faff4406STianyu Lan 
262e39acc37SPeter Zijlstra static __always_inline u64 hv_raw_get_register(unsigned int reg)
263e39acc37SPeter Zijlstra {
264e39acc37SPeter Zijlstra 	return __rdmsr(reg);
265e39acc37SPeter Zijlstra }
266e39acc37SPeter Zijlstra 
26779cadff2SVitaly Kuznetsov #else /* CONFIG_HYPERV */
26879cadff2SVitaly Kuznetsov static inline void hyperv_init(void) {}
2692ffd9e33SVitaly Kuznetsov static inline void hyperv_setup_mmu_ops(void) {}
27093286261SVitaly Kuznetsov static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
27193286261SVitaly Kuznetsov static inline void clear_hv_tscchange_cb(void) {}
27293286261SVitaly Kuznetsov static inline void hyperv_stop_tsc_emulation(void) {};
273a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
274a46d15ccSVitaly Kuznetsov {
275a46d15ccSVitaly Kuznetsov 	return NULL;
276a46d15ccSVitaly Kuznetsov }
277eb914cfeSTianyu Lan static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
278cc4edae4SLan Tianyu static inline int hyperv_flush_guest_mapping_range(u64 as,
279cc4edae4SLan Tianyu 		hyperv_fill_flush_list_func fill_func, void *data)
280cc4edae4SLan Tianyu {
281cc4edae4SLan Tianyu 	return -1;
282cc4edae4SLan Tianyu }
283faff4406STianyu Lan static inline void hv_set_register(unsigned int reg, u64 value) { }
284faff4406STianyu Lan static inline u64 hv_get_register(unsigned int reg) { return 0; }
2857fec185aSJinank Jain static inline void hv_set_non_nested_register(unsigned int reg, u64 value) { }
2867fec185aSJinank Jain static inline u64 hv_get_non_nested_register(unsigned int reg) { return 0; }
28779cadff2SVitaly Kuznetsov #endif /* CONFIG_HYPERV */
28879cadff2SVitaly Kuznetsov 
289765e33f5SMichael Kelley 
2903be1bc2fSSaurabh Sengar #ifdef CONFIG_HYPERV_VTL_MODE
2913be1bc2fSSaurabh Sengar void __init hv_vtl_init_platform(void);
2923be1bc2fSSaurabh Sengar #else
2933be1bc2fSSaurabh Sengar static inline void __init hv_vtl_init_platform(void) {}
2943be1bc2fSSaurabh Sengar #endif
2953be1bc2fSSaurabh Sengar 
296765e33f5SMichael Kelley #include <asm-generic/mshyperv.h>
297765e33f5SMichael Kelley 
298a2a47c6cSKy Srinivasan #endif
299