xref: /openbmc/linux/arch/x86/include/asm/mshyperv.h (revision 611d4c716db0141cfc436994dc5aff1d69c924ad)
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 
14faff4406STianyu Lan union hv_ghcb;
15faff4406STianyu Lan 
160cc4f6d9STianyu Lan DECLARE_STATIC_KEY_FALSE(isolation_type_snp);
170cc4f6d9STianyu Lan 
18cc4edae4SLan Tianyu typedef int (*hyperv_fill_flush_list_func)(
19cc4edae4SLan Tianyu 		struct hv_guest_mapping_flush_list *flush,
20cc4edae4SLan Tianyu 		void *data);
21cc4edae4SLan Tianyu 
22bc2b0331SK. Y. Srinivasan void hyperv_vector_handler(struct pt_regs *regs);
238730046cSK. Y. Srinivasan 
248730046cSK. Y. Srinivasan #if IS_ENABLED(CONFIG_HYPERV)
25dfe94d40SDexuan Cui extern int hyperv_init_cpuhp;
26dfe94d40SDexuan Cui 
27fc53662fSVitaly Kuznetsov extern void *hv_hypercall_pg;
28fc53662fSVitaly Kuznetsov 
2999a0f46aSWei Liu extern u64 hv_current_partition_id;
3099a0f46aSWei Liu 
31e1878402SMichael Kelley extern union hv_ghcb * __percpu *hv_ghcb_pg;
320cc4f6d9STianyu Lan 
3386b5ec35SWei Liu int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
3486b5ec35SWei Liu int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
3586b5ec35SWei Liu int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
3686b5ec35SWei Liu 
37fc53662fSVitaly Kuznetsov static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
38fc53662fSVitaly Kuznetsov {
39fc53662fSVitaly Kuznetsov 	u64 input_address = input ? virt_to_phys(input) : 0;
40fc53662fSVitaly Kuznetsov 	u64 output_address = output ? virt_to_phys(output) : 0;
41fc53662fSVitaly Kuznetsov 	u64 hv_status;
42fc53662fSVitaly Kuznetsov 
43fc53662fSVitaly Kuznetsov #ifdef CONFIG_X86_64
44fc53662fSVitaly Kuznetsov 	if (!hv_hypercall_pg)
45fc53662fSVitaly Kuznetsov 		return U64_MAX;
46fc53662fSVitaly Kuznetsov 
47fc53662fSVitaly Kuznetsov 	__asm__ __volatile__("mov %4, %%r8\n"
48e70e5892SDavid Woodhouse 			     CALL_NOSPEC
49f5caf621SJosh Poimboeuf 			     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
50fc53662fSVitaly Kuznetsov 			       "+c" (control), "+d" (input_address)
51e70e5892SDavid Woodhouse 			     :  "r" (output_address),
52e70e5892SDavid Woodhouse 				THUNK_TARGET(hv_hypercall_pg)
53fc53662fSVitaly Kuznetsov 			     : "cc", "memory", "r8", "r9", "r10", "r11");
54fc53662fSVitaly Kuznetsov #else
55fc53662fSVitaly Kuznetsov 	u32 input_address_hi = upper_32_bits(input_address);
56fc53662fSVitaly Kuznetsov 	u32 input_address_lo = lower_32_bits(input_address);
57fc53662fSVitaly Kuznetsov 	u32 output_address_hi = upper_32_bits(output_address);
58fc53662fSVitaly Kuznetsov 	u32 output_address_lo = lower_32_bits(output_address);
59fc53662fSVitaly Kuznetsov 
60fc53662fSVitaly Kuznetsov 	if (!hv_hypercall_pg)
61fc53662fSVitaly Kuznetsov 		return U64_MAX;
62fc53662fSVitaly Kuznetsov 
63e70e5892SDavid Woodhouse 	__asm__ __volatile__(CALL_NOSPEC
64fc53662fSVitaly Kuznetsov 			     : "=A" (hv_status),
65f5caf621SJosh Poimboeuf 			       "+c" (input_address_lo), ASM_CALL_CONSTRAINT
66fc53662fSVitaly Kuznetsov 			     : "A" (control),
67fc53662fSVitaly Kuznetsov 			       "b" (input_address_hi),
68fc53662fSVitaly Kuznetsov 			       "D"(output_address_hi), "S"(output_address_lo),
69e70e5892SDavid Woodhouse 			       THUNK_TARGET(hv_hypercall_pg)
70fc53662fSVitaly Kuznetsov 			     : "cc", "memory");
71fc53662fSVitaly Kuznetsov #endif /* !x86_64 */
72fc53662fSVitaly Kuznetsov 	return hv_status;
73fc53662fSVitaly Kuznetsov }
74dee863b5SVitaly Kuznetsov 
75f0d2f5c2SJinank Jain /* Hypercall to the L0 hypervisor */
76f0d2f5c2SJinank Jain static inline u64 hv_do_nested_hypercall(u64 control, void *input, void *output)
776a8edbd0SVitaly Kuznetsov {
78f0d2f5c2SJinank Jain 	return hv_do_hypercall(control | HV_HYPERCALL_NESTED, input, output);
79f0d2f5c2SJinank Jain }
80f0d2f5c2SJinank Jain 
81f0d2f5c2SJinank Jain /* Fast hypercall with 8 bytes of input and no output */
82f0d2f5c2SJinank Jain static inline u64 _hv_do_fast_hypercall8(u64 control, u64 input1)
83f0d2f5c2SJinank Jain {
84f0d2f5c2SJinank Jain 	u64 hv_status;
856a8edbd0SVitaly Kuznetsov 
866a8edbd0SVitaly Kuznetsov #ifdef CONFIG_X86_64
876a8edbd0SVitaly Kuznetsov 	{
88e70e5892SDavid Woodhouse 		__asm__ __volatile__(CALL_NOSPEC
89f5caf621SJosh Poimboeuf 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
906a8edbd0SVitaly Kuznetsov 				       "+c" (control), "+d" (input1)
91e70e5892SDavid Woodhouse 				     : THUNK_TARGET(hv_hypercall_pg)
926a8edbd0SVitaly Kuznetsov 				     : "cc", "r8", "r9", "r10", "r11");
936a8edbd0SVitaly Kuznetsov 	}
946a8edbd0SVitaly Kuznetsov #else
956a8edbd0SVitaly Kuznetsov 	{
966a8edbd0SVitaly Kuznetsov 		u32 input1_hi = upper_32_bits(input1);
976a8edbd0SVitaly Kuznetsov 		u32 input1_lo = lower_32_bits(input1);
986a8edbd0SVitaly Kuznetsov 
99e70e5892SDavid Woodhouse 		__asm__ __volatile__ (CALL_NOSPEC
1006a8edbd0SVitaly Kuznetsov 				      : "=A"(hv_status),
1016a8edbd0SVitaly Kuznetsov 					"+c"(input1_lo),
102f5caf621SJosh Poimboeuf 					ASM_CALL_CONSTRAINT
1036a8edbd0SVitaly Kuznetsov 				      :	"A" (control),
1046a8edbd0SVitaly Kuznetsov 					"b" (input1_hi),
105e70e5892SDavid Woodhouse 					THUNK_TARGET(hv_hypercall_pg)
1066a8edbd0SVitaly Kuznetsov 				      : "cc", "edi", "esi");
1076a8edbd0SVitaly Kuznetsov 	}
1086a8edbd0SVitaly Kuznetsov #endif
1096a8edbd0SVitaly Kuznetsov 		return hv_status;
1106a8edbd0SVitaly Kuznetsov }
1116a8edbd0SVitaly Kuznetsov 
112f0d2f5c2SJinank Jain static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
11353e52966SVitaly Kuznetsov {
114f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
115f0d2f5c2SJinank Jain 
116f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall8(control, input1);
117f0d2f5c2SJinank Jain }
118f0d2f5c2SJinank Jain 
119f0d2f5c2SJinank Jain static inline u64 hv_do_fast_nested_hypercall8(u16 code, u64 input1)
120f0d2f5c2SJinank Jain {
121f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
122f0d2f5c2SJinank Jain 
123f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall8(control, input1);
124f0d2f5c2SJinank Jain }
125f0d2f5c2SJinank Jain 
126f0d2f5c2SJinank Jain /* Fast hypercall with 16 bytes of input */
127f0d2f5c2SJinank Jain static inline u64 _hv_do_fast_hypercall16(u64 control, u64 input1, u64 input2)
128f0d2f5c2SJinank Jain {
129f0d2f5c2SJinank Jain 	u64 hv_status;
13053e52966SVitaly Kuznetsov 
13153e52966SVitaly Kuznetsov #ifdef CONFIG_X86_64
13253e52966SVitaly Kuznetsov 	{
13353e52966SVitaly Kuznetsov 		__asm__ __volatile__("mov %4, %%r8\n"
13453e52966SVitaly Kuznetsov 				     CALL_NOSPEC
13553e52966SVitaly Kuznetsov 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
13653e52966SVitaly Kuznetsov 				       "+c" (control), "+d" (input1)
13753e52966SVitaly Kuznetsov 				     : "r" (input2),
13853e52966SVitaly Kuznetsov 				       THUNK_TARGET(hv_hypercall_pg)
13953e52966SVitaly Kuznetsov 				     : "cc", "r8", "r9", "r10", "r11");
14053e52966SVitaly Kuznetsov 	}
14153e52966SVitaly Kuznetsov #else
14253e52966SVitaly Kuznetsov 	{
14353e52966SVitaly Kuznetsov 		u32 input1_hi = upper_32_bits(input1);
14453e52966SVitaly Kuznetsov 		u32 input1_lo = lower_32_bits(input1);
14553e52966SVitaly Kuznetsov 		u32 input2_hi = upper_32_bits(input2);
14653e52966SVitaly Kuznetsov 		u32 input2_lo = lower_32_bits(input2);
14753e52966SVitaly Kuznetsov 
14853e52966SVitaly Kuznetsov 		__asm__ __volatile__ (CALL_NOSPEC
14953e52966SVitaly Kuznetsov 				      : "=A"(hv_status),
15053e52966SVitaly Kuznetsov 					"+c"(input1_lo), ASM_CALL_CONSTRAINT
15153e52966SVitaly Kuznetsov 				      :	"A" (control), "b" (input1_hi),
15253e52966SVitaly Kuznetsov 					"D"(input2_hi), "S"(input2_lo),
15353e52966SVitaly Kuznetsov 					THUNK_TARGET(hv_hypercall_pg)
15453e52966SVitaly Kuznetsov 				      : "cc");
15553e52966SVitaly Kuznetsov 	}
15653e52966SVitaly Kuznetsov #endif
15753e52966SVitaly Kuznetsov 	return hv_status;
15853e52966SVitaly Kuznetsov }
15953e52966SVitaly Kuznetsov 
160f0d2f5c2SJinank Jain static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
161f0d2f5c2SJinank Jain {
162f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
163f0d2f5c2SJinank Jain 
164f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall16(control, input1, input2);
165f0d2f5c2SJinank Jain }
166f0d2f5c2SJinank Jain 
167f0d2f5c2SJinank Jain static inline u64 hv_do_fast_nested_hypercall16(u16 code, u64 input1, u64 input2)
168f0d2f5c2SJinank Jain {
169f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
170f0d2f5c2SJinank Jain 
171f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall16(control, input1, input2);
172f0d2f5c2SJinank Jain }
173f0d2f5c2SJinank Jain 
174a46d15ccSVitaly Kuznetsov extern struct hv_vp_assist_page **hv_vp_assist_page;
175a46d15ccSVitaly Kuznetsov 
176a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
177a46d15ccSVitaly Kuznetsov {
178a46d15ccSVitaly Kuznetsov 	if (!hv_vp_assist_page)
179a46d15ccSVitaly Kuznetsov 		return NULL;
180a46d15ccSVitaly Kuznetsov 
181a46d15ccSVitaly Kuznetsov 	return hv_vp_assist_page[cpu];
182a46d15ccSVitaly Kuznetsov }
1837415aea6SVitaly Kuznetsov 
1846b48cb5fSK. Y. Srinivasan void __init hyperv_init(void);
1852ffd9e33SVitaly Kuznetsov void hyperv_setup_mmu_ops(void);
18693286261SVitaly Kuznetsov void set_hv_tscchange_cb(void (*cb)(void));
18793286261SVitaly Kuznetsov void clear_hv_tscchange_cb(void);
18893286261SVitaly Kuznetsov void hyperv_stop_tsc_emulation(void);
189eb914cfeSTianyu Lan int hyperv_flush_guest_mapping(u64 as);
190cc4edae4SLan Tianyu int hyperv_flush_guest_mapping_range(u64 as,
191cc4edae4SLan Tianyu 		hyperv_fill_flush_list_func fill_func, void *data);
192cc4edae4SLan Tianyu int hyperv_fill_flush_guest_mapping_list(
193cc4edae4SLan Tianyu 		struct hv_guest_mapping_flush_list *flush,
194cc4edae4SLan Tianyu 		u64 start_gfn, u64 end_gfn);
1952d2ccf24SThomas Gleixner 
1962d2ccf24SThomas Gleixner #ifdef CONFIG_X86_64
1976b48cb5fSK. Y. Srinivasan void hv_apic_init(void);
1983a025de6SYi Sun void __init hv_init_spinlocks(void);
1993a025de6SYi Sun bool hv_vcpu_is_preempted(int vcpu);
2002d2ccf24SThomas Gleixner #else
2012d2ccf24SThomas Gleixner static inline void hv_apic_init(void) {}
2022d2ccf24SThomas Gleixner #endif
2032d2ccf24SThomas Gleixner 
204e39397d1SWei Liu struct irq_domain *hv_create_pci_msi_domain(void);
205e39397d1SWei Liu 
206fb5ef351SWei Liu int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
207fb5ef351SWei Liu 		struct hv_interrupt_entry *entry);
208fb5ef351SWei Liu int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
209810a5212STianyu Lan int hv_set_mem_host_visibility(unsigned long addr, int numpages, bool visible);
210faff4406STianyu Lan 
211faff4406STianyu Lan #ifdef CONFIG_AMD_MEM_ENCRYPT
212faff4406STianyu Lan void hv_ghcb_msr_write(u64 msr, u64 value);
213faff4406STianyu Lan void hv_ghcb_msr_read(u64 msr, u64 *value);
21449d6a3c0STianyu Lan bool hv_ghcb_negotiate_protocol(void);
215*611d4c71SGuilherme G. Piccoli void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason);
216faff4406STianyu Lan #else
217faff4406STianyu Lan static inline void hv_ghcb_msr_write(u64 msr, u64 value) {}
218faff4406STianyu Lan static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {}
21949d6a3c0STianyu Lan static inline bool hv_ghcb_negotiate_protocol(void) { return false; }
22049d6a3c0STianyu Lan static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {}
221faff4406STianyu Lan #endif
222faff4406STianyu Lan 
223faff4406STianyu Lan extern bool hv_isolation_type_snp(void);
224faff4406STianyu Lan 
225faff4406STianyu Lan static inline bool hv_is_synic_reg(unsigned int reg)
226faff4406STianyu Lan {
227b14033a3SNuno Das Neves 	return (reg >= HV_REGISTER_SCONTROL) &&
228b14033a3SNuno Das Neves 	       (reg <= HV_REGISTER_SINT15);
229b14033a3SNuno Das Neves }
230b14033a3SNuno Das Neves 
231b14033a3SNuno Das Neves static inline bool hv_is_sint_reg(unsigned int reg)
232b14033a3SNuno Das Neves {
233b14033a3SNuno Das Neves 	return (reg >= HV_REGISTER_SINT0) &&
234b14033a3SNuno Das Neves 	       (reg <= HV_REGISTER_SINT15);
235faff4406STianyu Lan }
236faff4406STianyu Lan 
2377fec185aSJinank Jain u64 hv_get_register(unsigned int reg);
2387fec185aSJinank Jain void hv_set_register(unsigned int reg, u64 value);
2397fec185aSJinank Jain u64 hv_get_non_nested_register(unsigned int reg);
2407fec185aSJinank Jain void hv_set_non_nested_register(unsigned int reg, u64 value);
241faff4406STianyu Lan 
24279cadff2SVitaly Kuznetsov #else /* CONFIG_HYPERV */
24379cadff2SVitaly Kuznetsov static inline void hyperv_init(void) {}
2442ffd9e33SVitaly Kuznetsov static inline void hyperv_setup_mmu_ops(void) {}
24593286261SVitaly Kuznetsov static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
24693286261SVitaly Kuznetsov static inline void clear_hv_tscchange_cb(void) {}
24793286261SVitaly Kuznetsov static inline void hyperv_stop_tsc_emulation(void) {};
248a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
249a46d15ccSVitaly Kuznetsov {
250a46d15ccSVitaly Kuznetsov 	return NULL;
251a46d15ccSVitaly Kuznetsov }
252eb914cfeSTianyu Lan static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
253cc4edae4SLan Tianyu static inline int hyperv_flush_guest_mapping_range(u64 as,
254cc4edae4SLan Tianyu 		hyperv_fill_flush_list_func fill_func, void *data)
255cc4edae4SLan Tianyu {
256cc4edae4SLan Tianyu 	return -1;
257cc4edae4SLan Tianyu }
258faff4406STianyu Lan static inline void hv_set_register(unsigned int reg, u64 value) { }
259faff4406STianyu Lan static inline u64 hv_get_register(unsigned int reg) { return 0; }
2607fec185aSJinank Jain static inline void hv_set_non_nested_register(unsigned int reg, u64 value) { }
2617fec185aSJinank Jain static inline u64 hv_get_non_nested_register(unsigned int reg) { return 0; }
262810a5212STianyu Lan static inline int hv_set_mem_host_visibility(unsigned long addr, int numpages,
263810a5212STianyu Lan 					     bool visible)
264810a5212STianyu Lan {
265810a5212STianyu Lan 	return -1;
266810a5212STianyu Lan }
26779cadff2SVitaly Kuznetsov #endif /* CONFIG_HYPERV */
26879cadff2SVitaly Kuznetsov 
269765e33f5SMichael Kelley 
270765e33f5SMichael Kelley #include <asm-generic/mshyperv.h>
271765e33f5SMichael Kelley 
272a2a47c6cSKy Srinivasan #endif
273