xref: /openbmc/linux/arch/x86/include/asm/mshyperv.h (revision 812b0597fb4043240724e4c7bed7ba1fe15c0e3f)
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 
14*812b0597SMichael Kelley /*
15*812b0597SMichael Kelley  * Hyper-V always provides a single IO-APIC at this MMIO address.
16*812b0597SMichael Kelley  * Ideally, the value should be looked up in ACPI tables, but it
17*812b0597SMichael Kelley  * is needed for mapping the IO-APIC early in boot on Confidential
18*812b0597SMichael Kelley  * VMs, before ACPI functions can be used.
19*812b0597SMichael Kelley  */
20*812b0597SMichael Kelley #define HV_IOAPIC_BASE_ADDRESS 0xfec00000
21*812b0597SMichael Kelley 
22faff4406STianyu Lan union hv_ghcb;
23faff4406STianyu Lan 
240cc4f6d9STianyu Lan DECLARE_STATIC_KEY_FALSE(isolation_type_snp);
250cc4f6d9STianyu Lan 
26cc4edae4SLan Tianyu typedef int (*hyperv_fill_flush_list_func)(
27cc4edae4SLan Tianyu 		struct hv_guest_mapping_flush_list *flush,
28cc4edae4SLan Tianyu 		void *data);
29cc4edae4SLan Tianyu 
30bc2b0331SK. Y. Srinivasan void hyperv_vector_handler(struct pt_regs *regs);
318730046cSK. Y. Srinivasan 
328730046cSK. Y. Srinivasan #if IS_ENABLED(CONFIG_HYPERV)
33dfe94d40SDexuan Cui extern int hyperv_init_cpuhp;
34dfe94d40SDexuan Cui 
35fc53662fSVitaly Kuznetsov extern void *hv_hypercall_pg;
36fc53662fSVitaly Kuznetsov 
3799a0f46aSWei Liu extern u64 hv_current_partition_id;
3899a0f46aSWei Liu 
39e1878402SMichael Kelley extern union hv_ghcb * __percpu *hv_ghcb_pg;
400cc4f6d9STianyu Lan 
4186b5ec35SWei Liu int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
4286b5ec35SWei Liu int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
4386b5ec35SWei Liu int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
4486b5ec35SWei Liu 
45fc53662fSVitaly Kuznetsov static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
46fc53662fSVitaly Kuznetsov {
47fc53662fSVitaly Kuznetsov 	u64 input_address = input ? virt_to_phys(input) : 0;
48fc53662fSVitaly Kuznetsov 	u64 output_address = output ? virt_to_phys(output) : 0;
49fc53662fSVitaly Kuznetsov 	u64 hv_status;
50fc53662fSVitaly Kuznetsov 
51fc53662fSVitaly Kuznetsov #ifdef CONFIG_X86_64
52fc53662fSVitaly Kuznetsov 	if (!hv_hypercall_pg)
53fc53662fSVitaly Kuznetsov 		return U64_MAX;
54fc53662fSVitaly Kuznetsov 
55fc53662fSVitaly Kuznetsov 	__asm__ __volatile__("mov %4, %%r8\n"
56e70e5892SDavid Woodhouse 			     CALL_NOSPEC
57f5caf621SJosh Poimboeuf 			     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
58fc53662fSVitaly Kuznetsov 			       "+c" (control), "+d" (input_address)
59e70e5892SDavid Woodhouse 			     :  "r" (output_address),
60e70e5892SDavid Woodhouse 				THUNK_TARGET(hv_hypercall_pg)
61fc53662fSVitaly Kuznetsov 			     : "cc", "memory", "r8", "r9", "r10", "r11");
62fc53662fSVitaly Kuznetsov #else
63fc53662fSVitaly Kuznetsov 	u32 input_address_hi = upper_32_bits(input_address);
64fc53662fSVitaly Kuznetsov 	u32 input_address_lo = lower_32_bits(input_address);
65fc53662fSVitaly Kuznetsov 	u32 output_address_hi = upper_32_bits(output_address);
66fc53662fSVitaly Kuznetsov 	u32 output_address_lo = lower_32_bits(output_address);
67fc53662fSVitaly Kuznetsov 
68fc53662fSVitaly Kuznetsov 	if (!hv_hypercall_pg)
69fc53662fSVitaly Kuznetsov 		return U64_MAX;
70fc53662fSVitaly Kuznetsov 
71e70e5892SDavid Woodhouse 	__asm__ __volatile__(CALL_NOSPEC
72fc53662fSVitaly Kuznetsov 			     : "=A" (hv_status),
73f5caf621SJosh Poimboeuf 			       "+c" (input_address_lo), ASM_CALL_CONSTRAINT
74fc53662fSVitaly Kuznetsov 			     : "A" (control),
75fc53662fSVitaly Kuznetsov 			       "b" (input_address_hi),
76fc53662fSVitaly Kuznetsov 			       "D"(output_address_hi), "S"(output_address_lo),
77e70e5892SDavid Woodhouse 			       THUNK_TARGET(hv_hypercall_pg)
78fc53662fSVitaly Kuznetsov 			     : "cc", "memory");
79fc53662fSVitaly Kuznetsov #endif /* !x86_64 */
80fc53662fSVitaly Kuznetsov 	return hv_status;
81fc53662fSVitaly Kuznetsov }
82dee863b5SVitaly Kuznetsov 
83f0d2f5c2SJinank Jain /* Hypercall to the L0 hypervisor */
84f0d2f5c2SJinank Jain static inline u64 hv_do_nested_hypercall(u64 control, void *input, void *output)
856a8edbd0SVitaly Kuznetsov {
86f0d2f5c2SJinank Jain 	return hv_do_hypercall(control | HV_HYPERCALL_NESTED, input, output);
87f0d2f5c2SJinank Jain }
88f0d2f5c2SJinank Jain 
89f0d2f5c2SJinank Jain /* Fast hypercall with 8 bytes of input and no output */
90f0d2f5c2SJinank Jain static inline u64 _hv_do_fast_hypercall8(u64 control, u64 input1)
91f0d2f5c2SJinank Jain {
92f0d2f5c2SJinank Jain 	u64 hv_status;
936a8edbd0SVitaly Kuznetsov 
946a8edbd0SVitaly Kuznetsov #ifdef CONFIG_X86_64
956a8edbd0SVitaly Kuznetsov 	{
96e70e5892SDavid Woodhouse 		__asm__ __volatile__(CALL_NOSPEC
97f5caf621SJosh Poimboeuf 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
986a8edbd0SVitaly Kuznetsov 				       "+c" (control), "+d" (input1)
99e70e5892SDavid Woodhouse 				     : THUNK_TARGET(hv_hypercall_pg)
1006a8edbd0SVitaly Kuznetsov 				     : "cc", "r8", "r9", "r10", "r11");
1016a8edbd0SVitaly Kuznetsov 	}
1026a8edbd0SVitaly Kuznetsov #else
1036a8edbd0SVitaly Kuznetsov 	{
1046a8edbd0SVitaly Kuznetsov 		u32 input1_hi = upper_32_bits(input1);
1056a8edbd0SVitaly Kuznetsov 		u32 input1_lo = lower_32_bits(input1);
1066a8edbd0SVitaly Kuznetsov 
107e70e5892SDavid Woodhouse 		__asm__ __volatile__ (CALL_NOSPEC
1086a8edbd0SVitaly Kuznetsov 				      : "=A"(hv_status),
1096a8edbd0SVitaly Kuznetsov 					"+c"(input1_lo),
110f5caf621SJosh Poimboeuf 					ASM_CALL_CONSTRAINT
1116a8edbd0SVitaly Kuznetsov 				      :	"A" (control),
1126a8edbd0SVitaly Kuznetsov 					"b" (input1_hi),
113e70e5892SDavid Woodhouse 					THUNK_TARGET(hv_hypercall_pg)
1146a8edbd0SVitaly Kuznetsov 				      : "cc", "edi", "esi");
1156a8edbd0SVitaly Kuznetsov 	}
1166a8edbd0SVitaly Kuznetsov #endif
1176a8edbd0SVitaly Kuznetsov 		return hv_status;
1186a8edbd0SVitaly Kuznetsov }
1196a8edbd0SVitaly Kuznetsov 
120f0d2f5c2SJinank Jain static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
12153e52966SVitaly Kuznetsov {
122f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
123f0d2f5c2SJinank Jain 
124f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall8(control, input1);
125f0d2f5c2SJinank Jain }
126f0d2f5c2SJinank Jain 
127f0d2f5c2SJinank Jain static inline u64 hv_do_fast_nested_hypercall8(u16 code, u64 input1)
128f0d2f5c2SJinank Jain {
129f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
130f0d2f5c2SJinank Jain 
131f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall8(control, input1);
132f0d2f5c2SJinank Jain }
133f0d2f5c2SJinank Jain 
134f0d2f5c2SJinank Jain /* Fast hypercall with 16 bytes of input */
135f0d2f5c2SJinank Jain static inline u64 _hv_do_fast_hypercall16(u64 control, u64 input1, u64 input2)
136f0d2f5c2SJinank Jain {
137f0d2f5c2SJinank Jain 	u64 hv_status;
13853e52966SVitaly Kuznetsov 
13953e52966SVitaly Kuznetsov #ifdef CONFIG_X86_64
14053e52966SVitaly Kuznetsov 	{
14153e52966SVitaly Kuznetsov 		__asm__ __volatile__("mov %4, %%r8\n"
14253e52966SVitaly Kuznetsov 				     CALL_NOSPEC
14353e52966SVitaly Kuznetsov 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
14453e52966SVitaly Kuznetsov 				       "+c" (control), "+d" (input1)
14553e52966SVitaly Kuznetsov 				     : "r" (input2),
14653e52966SVitaly Kuznetsov 				       THUNK_TARGET(hv_hypercall_pg)
14753e52966SVitaly Kuznetsov 				     : "cc", "r8", "r9", "r10", "r11");
14853e52966SVitaly Kuznetsov 	}
14953e52966SVitaly Kuznetsov #else
15053e52966SVitaly Kuznetsov 	{
15153e52966SVitaly Kuznetsov 		u32 input1_hi = upper_32_bits(input1);
15253e52966SVitaly Kuznetsov 		u32 input1_lo = lower_32_bits(input1);
15353e52966SVitaly Kuznetsov 		u32 input2_hi = upper_32_bits(input2);
15453e52966SVitaly Kuznetsov 		u32 input2_lo = lower_32_bits(input2);
15553e52966SVitaly Kuznetsov 
15653e52966SVitaly Kuznetsov 		__asm__ __volatile__ (CALL_NOSPEC
15753e52966SVitaly Kuznetsov 				      : "=A"(hv_status),
15853e52966SVitaly Kuznetsov 					"+c"(input1_lo), ASM_CALL_CONSTRAINT
15953e52966SVitaly Kuznetsov 				      :	"A" (control), "b" (input1_hi),
16053e52966SVitaly Kuznetsov 					"D"(input2_hi), "S"(input2_lo),
16153e52966SVitaly Kuznetsov 					THUNK_TARGET(hv_hypercall_pg)
16253e52966SVitaly Kuznetsov 				      : "cc");
16353e52966SVitaly Kuznetsov 	}
16453e52966SVitaly Kuznetsov #endif
16553e52966SVitaly Kuznetsov 	return hv_status;
16653e52966SVitaly Kuznetsov }
16753e52966SVitaly Kuznetsov 
168f0d2f5c2SJinank Jain static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
169f0d2f5c2SJinank Jain {
170f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
171f0d2f5c2SJinank Jain 
172f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall16(control, input1, input2);
173f0d2f5c2SJinank Jain }
174f0d2f5c2SJinank Jain 
175f0d2f5c2SJinank Jain static inline u64 hv_do_fast_nested_hypercall16(u16 code, u64 input1, u64 input2)
176f0d2f5c2SJinank Jain {
177f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
178f0d2f5c2SJinank Jain 
179f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall16(control, input1, input2);
180f0d2f5c2SJinank Jain }
181f0d2f5c2SJinank Jain 
182a46d15ccSVitaly Kuznetsov extern struct hv_vp_assist_page **hv_vp_assist_page;
183a46d15ccSVitaly Kuznetsov 
184a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
185a46d15ccSVitaly Kuznetsov {
186a46d15ccSVitaly Kuznetsov 	if (!hv_vp_assist_page)
187a46d15ccSVitaly Kuznetsov 		return NULL;
188a46d15ccSVitaly Kuznetsov 
189a46d15ccSVitaly Kuznetsov 	return hv_vp_assist_page[cpu];
190a46d15ccSVitaly Kuznetsov }
1917415aea6SVitaly Kuznetsov 
1926b48cb5fSK. Y. Srinivasan void __init hyperv_init(void);
1932ffd9e33SVitaly Kuznetsov void hyperv_setup_mmu_ops(void);
19493286261SVitaly Kuznetsov void set_hv_tscchange_cb(void (*cb)(void));
19593286261SVitaly Kuznetsov void clear_hv_tscchange_cb(void);
19693286261SVitaly Kuznetsov void hyperv_stop_tsc_emulation(void);
197eb914cfeSTianyu Lan int hyperv_flush_guest_mapping(u64 as);
198cc4edae4SLan Tianyu int hyperv_flush_guest_mapping_range(u64 as,
199cc4edae4SLan Tianyu 		hyperv_fill_flush_list_func fill_func, void *data);
200cc4edae4SLan Tianyu int hyperv_fill_flush_guest_mapping_list(
201cc4edae4SLan Tianyu 		struct hv_guest_mapping_flush_list *flush,
202cc4edae4SLan Tianyu 		u64 start_gfn, u64 end_gfn);
2032d2ccf24SThomas Gleixner 
2042d2ccf24SThomas Gleixner #ifdef CONFIG_X86_64
2056b48cb5fSK. Y. Srinivasan void hv_apic_init(void);
2063a025de6SYi Sun void __init hv_init_spinlocks(void);
2073a025de6SYi Sun bool hv_vcpu_is_preempted(int vcpu);
2082d2ccf24SThomas Gleixner #else
2092d2ccf24SThomas Gleixner static inline void hv_apic_init(void) {}
2102d2ccf24SThomas Gleixner #endif
2112d2ccf24SThomas Gleixner 
212e39397d1SWei Liu struct irq_domain *hv_create_pci_msi_domain(void);
213e39397d1SWei Liu 
214fb5ef351SWei Liu int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
215fb5ef351SWei Liu 		struct hv_interrupt_entry *entry);
216fb5ef351SWei Liu int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
217faff4406STianyu Lan 
218faff4406STianyu Lan #ifdef CONFIG_AMD_MEM_ENCRYPT
219faff4406STianyu Lan void hv_ghcb_msr_write(u64 msr, u64 value);
220faff4406STianyu Lan void hv_ghcb_msr_read(u64 msr, u64 *value);
22149d6a3c0STianyu Lan bool hv_ghcb_negotiate_protocol(void);
22249d6a3c0STianyu Lan void hv_ghcb_terminate(unsigned int set, unsigned int reason);
223*812b0597SMichael Kelley void hv_vtom_init(void);
224faff4406STianyu Lan #else
225faff4406STianyu Lan static inline void hv_ghcb_msr_write(u64 msr, u64 value) {}
226faff4406STianyu Lan static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {}
22749d6a3c0STianyu Lan static inline bool hv_ghcb_negotiate_protocol(void) { return false; }
22849d6a3c0STianyu Lan static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {}
229*812b0597SMichael Kelley static inline void hv_vtom_init(void) {}
230faff4406STianyu Lan #endif
231faff4406STianyu Lan 
232faff4406STianyu Lan extern bool hv_isolation_type_snp(void);
233faff4406STianyu Lan 
234faff4406STianyu Lan static inline bool hv_is_synic_reg(unsigned int reg)
235faff4406STianyu Lan {
236b14033a3SNuno Das Neves 	return (reg >= HV_REGISTER_SCONTROL) &&
237b14033a3SNuno Das Neves 	       (reg <= HV_REGISTER_SINT15);
238b14033a3SNuno Das Neves }
239b14033a3SNuno Das Neves 
240b14033a3SNuno Das Neves static inline bool hv_is_sint_reg(unsigned int reg)
241b14033a3SNuno Das Neves {
242b14033a3SNuno Das Neves 	return (reg >= HV_REGISTER_SINT0) &&
243b14033a3SNuno Das Neves 	       (reg <= HV_REGISTER_SINT15);
244faff4406STianyu Lan }
245faff4406STianyu Lan 
2467fec185aSJinank Jain u64 hv_get_register(unsigned int reg);
2477fec185aSJinank Jain void hv_set_register(unsigned int reg, u64 value);
2487fec185aSJinank Jain u64 hv_get_non_nested_register(unsigned int reg);
2497fec185aSJinank Jain void hv_set_non_nested_register(unsigned int reg, u64 value);
250faff4406STianyu Lan 
25179cadff2SVitaly Kuznetsov #else /* CONFIG_HYPERV */
25279cadff2SVitaly Kuznetsov static inline void hyperv_init(void) {}
2532ffd9e33SVitaly Kuznetsov static inline void hyperv_setup_mmu_ops(void) {}
25493286261SVitaly Kuznetsov static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
25593286261SVitaly Kuznetsov static inline void clear_hv_tscchange_cb(void) {}
25693286261SVitaly Kuznetsov static inline void hyperv_stop_tsc_emulation(void) {};
257a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
258a46d15ccSVitaly Kuznetsov {
259a46d15ccSVitaly Kuznetsov 	return NULL;
260a46d15ccSVitaly Kuznetsov }
261eb914cfeSTianyu Lan static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
262cc4edae4SLan Tianyu static inline int hyperv_flush_guest_mapping_range(u64 as,
263cc4edae4SLan Tianyu 		hyperv_fill_flush_list_func fill_func, void *data)
264cc4edae4SLan Tianyu {
265cc4edae4SLan Tianyu 	return -1;
266cc4edae4SLan Tianyu }
267faff4406STianyu Lan static inline void hv_set_register(unsigned int reg, u64 value) { }
268faff4406STianyu Lan static inline u64 hv_get_register(unsigned int reg) { return 0; }
2697fec185aSJinank Jain static inline void hv_set_non_nested_register(unsigned int reg, u64 value) { }
2707fec185aSJinank Jain static inline u64 hv_get_non_nested_register(unsigned int reg) { return 0; }
27179cadff2SVitaly Kuznetsov #endif /* CONFIG_HYPERV */
27279cadff2SVitaly Kuznetsov 
273765e33f5SMichael Kelley 
274765e33f5SMichael Kelley #include <asm-generic/mshyperv.h>
275765e33f5SMichael Kelley 
276a2a47c6cSKy Srinivasan #endif
277