xref: /openbmc/linux/arch/x86/include/asm/mshyperv.h (revision 060f2b979c4e0e894c381c76a4dcad24376feddd)
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>
8*060f2b97SZhiHu #include <linux/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);
290cc4f6d9STianyu Lan 
30cc4edae4SLan Tianyu typedef int (*hyperv_fill_flush_list_func)(
31cc4edae4SLan Tianyu 		struct hv_guest_mapping_flush_list *flush,
32cc4edae4SLan Tianyu 		void *data);
33cc4edae4SLan Tianyu 
34bc2b0331SK. Y. Srinivasan void hyperv_vector_handler(struct pt_regs *regs);
358730046cSK. Y. Srinivasan 
360a7a0058SSaurabh Sengar static inline unsigned char hv_get_nmi_reason(void)
370a7a0058SSaurabh Sengar {
380a7a0058SSaurabh Sengar 	return 0;
390a7a0058SSaurabh Sengar }
400a7a0058SSaurabh Sengar 
418730046cSK. Y. Srinivasan #if IS_ENABLED(CONFIG_HYPERV)
42dfe94d40SDexuan Cui extern int hyperv_init_cpuhp;
43dfe94d40SDexuan Cui 
44fc53662fSVitaly Kuznetsov extern void *hv_hypercall_pg;
45fc53662fSVitaly Kuznetsov 
4699a0f46aSWei Liu extern u64 hv_current_partition_id;
4799a0f46aSWei Liu 
48e1878402SMichael Kelley extern union hv_ghcb * __percpu *hv_ghcb_pg;
490cc4f6d9STianyu Lan 
5086b5ec35SWei Liu int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
5186b5ec35SWei Liu int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
5286b5ec35SWei Liu int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
5386b5ec35SWei Liu 
54fc53662fSVitaly Kuznetsov static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
55fc53662fSVitaly Kuznetsov {
56fc53662fSVitaly Kuznetsov 	u64 input_address = input ? virt_to_phys(input) : 0;
57fc53662fSVitaly Kuznetsov 	u64 output_address = output ? virt_to_phys(output) : 0;
58fc53662fSVitaly Kuznetsov 	u64 hv_status;
59fc53662fSVitaly Kuznetsov 
60fc53662fSVitaly Kuznetsov #ifdef CONFIG_X86_64
61fc53662fSVitaly Kuznetsov 	if (!hv_hypercall_pg)
62fc53662fSVitaly Kuznetsov 		return U64_MAX;
63fc53662fSVitaly Kuznetsov 
64fc53662fSVitaly Kuznetsov 	__asm__ __volatile__("mov %4, %%r8\n"
65e70e5892SDavid Woodhouse 			     CALL_NOSPEC
66f5caf621SJosh Poimboeuf 			     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
67fc53662fSVitaly Kuznetsov 			       "+c" (control), "+d" (input_address)
68e70e5892SDavid Woodhouse 			     :  "r" (output_address),
69e70e5892SDavid Woodhouse 				THUNK_TARGET(hv_hypercall_pg)
70fc53662fSVitaly Kuznetsov 			     : "cc", "memory", "r8", "r9", "r10", "r11");
71fc53662fSVitaly Kuznetsov #else
72fc53662fSVitaly Kuznetsov 	u32 input_address_hi = upper_32_bits(input_address);
73fc53662fSVitaly Kuznetsov 	u32 input_address_lo = lower_32_bits(input_address);
74fc53662fSVitaly Kuznetsov 	u32 output_address_hi = upper_32_bits(output_address);
75fc53662fSVitaly Kuznetsov 	u32 output_address_lo = lower_32_bits(output_address);
76fc53662fSVitaly Kuznetsov 
77fc53662fSVitaly Kuznetsov 	if (!hv_hypercall_pg)
78fc53662fSVitaly Kuznetsov 		return U64_MAX;
79fc53662fSVitaly Kuznetsov 
80e70e5892SDavid Woodhouse 	__asm__ __volatile__(CALL_NOSPEC
81fc53662fSVitaly Kuznetsov 			     : "=A" (hv_status),
82f5caf621SJosh Poimboeuf 			       "+c" (input_address_lo), ASM_CALL_CONSTRAINT
83fc53662fSVitaly Kuznetsov 			     : "A" (control),
84fc53662fSVitaly Kuznetsov 			       "b" (input_address_hi),
85fc53662fSVitaly Kuznetsov 			       "D"(output_address_hi), "S"(output_address_lo),
86e70e5892SDavid Woodhouse 			       THUNK_TARGET(hv_hypercall_pg)
87fc53662fSVitaly Kuznetsov 			     : "cc", "memory");
88fc53662fSVitaly Kuznetsov #endif /* !x86_64 */
89fc53662fSVitaly Kuznetsov 	return hv_status;
90fc53662fSVitaly Kuznetsov }
91dee863b5SVitaly Kuznetsov 
92f0d2f5c2SJinank Jain /* Hypercall to the L0 hypervisor */
93f0d2f5c2SJinank Jain static inline u64 hv_do_nested_hypercall(u64 control, void *input, void *output)
946a8edbd0SVitaly Kuznetsov {
95f0d2f5c2SJinank Jain 	return hv_do_hypercall(control | HV_HYPERCALL_NESTED, input, output);
96f0d2f5c2SJinank Jain }
97f0d2f5c2SJinank Jain 
98f0d2f5c2SJinank Jain /* Fast hypercall with 8 bytes of input and no output */
99f0d2f5c2SJinank Jain static inline u64 _hv_do_fast_hypercall8(u64 control, u64 input1)
100f0d2f5c2SJinank Jain {
101f0d2f5c2SJinank Jain 	u64 hv_status;
1026a8edbd0SVitaly Kuznetsov 
1036a8edbd0SVitaly Kuznetsov #ifdef CONFIG_X86_64
1046a8edbd0SVitaly Kuznetsov 	{
105e70e5892SDavid Woodhouse 		__asm__ __volatile__(CALL_NOSPEC
106f5caf621SJosh Poimboeuf 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
1076a8edbd0SVitaly Kuznetsov 				       "+c" (control), "+d" (input1)
108e70e5892SDavid Woodhouse 				     : THUNK_TARGET(hv_hypercall_pg)
1096a8edbd0SVitaly Kuznetsov 				     : "cc", "r8", "r9", "r10", "r11");
1106a8edbd0SVitaly Kuznetsov 	}
1116a8edbd0SVitaly Kuznetsov #else
1126a8edbd0SVitaly Kuznetsov 	{
1136a8edbd0SVitaly Kuznetsov 		u32 input1_hi = upper_32_bits(input1);
1146a8edbd0SVitaly Kuznetsov 		u32 input1_lo = lower_32_bits(input1);
1156a8edbd0SVitaly Kuznetsov 
116e70e5892SDavid Woodhouse 		__asm__ __volatile__ (CALL_NOSPEC
1176a8edbd0SVitaly Kuznetsov 				      : "=A"(hv_status),
1186a8edbd0SVitaly Kuznetsov 					"+c"(input1_lo),
119f5caf621SJosh Poimboeuf 					ASM_CALL_CONSTRAINT
1206a8edbd0SVitaly Kuznetsov 				      :	"A" (control),
1216a8edbd0SVitaly Kuznetsov 					"b" (input1_hi),
122e70e5892SDavid Woodhouse 					THUNK_TARGET(hv_hypercall_pg)
1236a8edbd0SVitaly Kuznetsov 				      : "cc", "edi", "esi");
1246a8edbd0SVitaly Kuznetsov 	}
1256a8edbd0SVitaly Kuznetsov #endif
1266a8edbd0SVitaly Kuznetsov 		return hv_status;
1276a8edbd0SVitaly Kuznetsov }
1286a8edbd0SVitaly Kuznetsov 
129f0d2f5c2SJinank Jain static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
13053e52966SVitaly Kuznetsov {
131f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
132f0d2f5c2SJinank Jain 
133f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall8(control, input1);
134f0d2f5c2SJinank Jain }
135f0d2f5c2SJinank Jain 
136f0d2f5c2SJinank Jain static inline u64 hv_do_fast_nested_hypercall8(u16 code, u64 input1)
137f0d2f5c2SJinank Jain {
138f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
139f0d2f5c2SJinank Jain 
140f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall8(control, input1);
141f0d2f5c2SJinank Jain }
142f0d2f5c2SJinank Jain 
143f0d2f5c2SJinank Jain /* Fast hypercall with 16 bytes of input */
144f0d2f5c2SJinank Jain static inline u64 _hv_do_fast_hypercall16(u64 control, u64 input1, u64 input2)
145f0d2f5c2SJinank Jain {
146f0d2f5c2SJinank Jain 	u64 hv_status;
14753e52966SVitaly Kuznetsov 
14853e52966SVitaly Kuznetsov #ifdef CONFIG_X86_64
14953e52966SVitaly Kuznetsov 	{
15053e52966SVitaly Kuznetsov 		__asm__ __volatile__("mov %4, %%r8\n"
15153e52966SVitaly Kuznetsov 				     CALL_NOSPEC
15253e52966SVitaly Kuznetsov 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
15353e52966SVitaly Kuznetsov 				       "+c" (control), "+d" (input1)
15453e52966SVitaly Kuznetsov 				     : "r" (input2),
15553e52966SVitaly Kuznetsov 				       THUNK_TARGET(hv_hypercall_pg)
15653e52966SVitaly Kuznetsov 				     : "cc", "r8", "r9", "r10", "r11");
15753e52966SVitaly Kuznetsov 	}
15853e52966SVitaly Kuznetsov #else
15953e52966SVitaly Kuznetsov 	{
16053e52966SVitaly Kuznetsov 		u32 input1_hi = upper_32_bits(input1);
16153e52966SVitaly Kuznetsov 		u32 input1_lo = lower_32_bits(input1);
16253e52966SVitaly Kuznetsov 		u32 input2_hi = upper_32_bits(input2);
16353e52966SVitaly Kuznetsov 		u32 input2_lo = lower_32_bits(input2);
16453e52966SVitaly Kuznetsov 
16553e52966SVitaly Kuznetsov 		__asm__ __volatile__ (CALL_NOSPEC
16653e52966SVitaly Kuznetsov 				      : "=A"(hv_status),
16753e52966SVitaly Kuznetsov 					"+c"(input1_lo), ASM_CALL_CONSTRAINT
16853e52966SVitaly Kuznetsov 				      :	"A" (control), "b" (input1_hi),
16953e52966SVitaly Kuznetsov 					"D"(input2_hi), "S"(input2_lo),
17053e52966SVitaly Kuznetsov 					THUNK_TARGET(hv_hypercall_pg)
17153e52966SVitaly Kuznetsov 				      : "cc");
17253e52966SVitaly Kuznetsov 	}
17353e52966SVitaly Kuznetsov #endif
17453e52966SVitaly Kuznetsov 	return hv_status;
17553e52966SVitaly Kuznetsov }
17653e52966SVitaly Kuznetsov 
177f0d2f5c2SJinank Jain static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
178f0d2f5c2SJinank Jain {
179f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
180f0d2f5c2SJinank Jain 
181f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall16(control, input1, input2);
182f0d2f5c2SJinank Jain }
183f0d2f5c2SJinank Jain 
184f0d2f5c2SJinank Jain static inline u64 hv_do_fast_nested_hypercall16(u16 code, u64 input1, u64 input2)
185f0d2f5c2SJinank Jain {
186f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
187f0d2f5c2SJinank Jain 
188f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall16(control, input1, input2);
189f0d2f5c2SJinank Jain }
190f0d2f5c2SJinank Jain 
191a46d15ccSVitaly Kuznetsov extern struct hv_vp_assist_page **hv_vp_assist_page;
192a46d15ccSVitaly Kuznetsov 
193a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
194a46d15ccSVitaly Kuznetsov {
195a46d15ccSVitaly Kuznetsov 	if (!hv_vp_assist_page)
196a46d15ccSVitaly Kuznetsov 		return NULL;
197a46d15ccSVitaly Kuznetsov 
198a46d15ccSVitaly Kuznetsov 	return hv_vp_assist_page[cpu];
199a46d15ccSVitaly Kuznetsov }
2007415aea6SVitaly Kuznetsov 
2016b48cb5fSK. Y. Srinivasan void __init hyperv_init(void);
2022ffd9e33SVitaly Kuznetsov void hyperv_setup_mmu_ops(void);
20393286261SVitaly Kuznetsov void set_hv_tscchange_cb(void (*cb)(void));
20493286261SVitaly Kuznetsov void clear_hv_tscchange_cb(void);
20593286261SVitaly Kuznetsov void hyperv_stop_tsc_emulation(void);
206eb914cfeSTianyu Lan int hyperv_flush_guest_mapping(u64 as);
207cc4edae4SLan Tianyu int hyperv_flush_guest_mapping_range(u64 as,
208cc4edae4SLan Tianyu 		hyperv_fill_flush_list_func fill_func, void *data);
209cc4edae4SLan Tianyu int hyperv_fill_flush_guest_mapping_list(
210cc4edae4SLan Tianyu 		struct hv_guest_mapping_flush_list *flush,
211cc4edae4SLan Tianyu 		u64 start_gfn, u64 end_gfn);
2122d2ccf24SThomas Gleixner 
2132d2ccf24SThomas Gleixner #ifdef CONFIG_X86_64
2146b48cb5fSK. Y. Srinivasan void hv_apic_init(void);
2153a025de6SYi Sun void __init hv_init_spinlocks(void);
2163a025de6SYi Sun bool hv_vcpu_is_preempted(int vcpu);
2172d2ccf24SThomas Gleixner #else
2182d2ccf24SThomas Gleixner static inline void hv_apic_init(void) {}
2192d2ccf24SThomas Gleixner #endif
2202d2ccf24SThomas Gleixner 
221e39397d1SWei Liu struct irq_domain *hv_create_pci_msi_domain(void);
222e39397d1SWei Liu 
223fb5ef351SWei Liu int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
224fb5ef351SWei Liu 		struct hv_interrupt_entry *entry);
225fb5ef351SWei Liu int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
226faff4406STianyu Lan 
227faff4406STianyu Lan #ifdef CONFIG_AMD_MEM_ENCRYPT
228faff4406STianyu Lan void hv_ghcb_msr_write(u64 msr, u64 value);
229faff4406STianyu Lan void hv_ghcb_msr_read(u64 msr, u64 *value);
23049d6a3c0STianyu Lan bool hv_ghcb_negotiate_protocol(void);
231611d4c71SGuilherme G. Piccoli void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason);
232812b0597SMichael Kelley void hv_vtom_init(void);
233faff4406STianyu Lan #else
234faff4406STianyu Lan static inline void hv_ghcb_msr_write(u64 msr, u64 value) {}
235faff4406STianyu Lan static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {}
23649d6a3c0STianyu Lan static inline bool hv_ghcb_negotiate_protocol(void) { return false; }
23749d6a3c0STianyu Lan static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {}
238812b0597SMichael Kelley static inline void hv_vtom_init(void) {}
239faff4406STianyu Lan #endif
240faff4406STianyu Lan 
241faff4406STianyu Lan extern bool hv_isolation_type_snp(void);
242faff4406STianyu Lan 
243faff4406STianyu Lan static inline bool hv_is_synic_reg(unsigned int reg)
244faff4406STianyu Lan {
245b14033a3SNuno Das Neves 	return (reg >= HV_REGISTER_SCONTROL) &&
246b14033a3SNuno Das Neves 	       (reg <= HV_REGISTER_SINT15);
247b14033a3SNuno Das Neves }
248b14033a3SNuno Das Neves 
249b14033a3SNuno Das Neves static inline bool hv_is_sint_reg(unsigned int reg)
250b14033a3SNuno Das Neves {
251b14033a3SNuno Das Neves 	return (reg >= HV_REGISTER_SINT0) &&
252b14033a3SNuno Das Neves 	       (reg <= HV_REGISTER_SINT15);
253faff4406STianyu Lan }
254faff4406STianyu Lan 
2557fec185aSJinank Jain u64 hv_get_register(unsigned int reg);
2567fec185aSJinank Jain void hv_set_register(unsigned int reg, u64 value);
2577fec185aSJinank Jain u64 hv_get_non_nested_register(unsigned int reg);
2587fec185aSJinank Jain void hv_set_non_nested_register(unsigned int reg, u64 value);
259faff4406STianyu Lan 
26079cadff2SVitaly Kuznetsov #else /* CONFIG_HYPERV */
26179cadff2SVitaly Kuznetsov static inline void hyperv_init(void) {}
2622ffd9e33SVitaly Kuznetsov static inline void hyperv_setup_mmu_ops(void) {}
26393286261SVitaly Kuznetsov static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
26493286261SVitaly Kuznetsov static inline void clear_hv_tscchange_cb(void) {}
26593286261SVitaly Kuznetsov static inline void hyperv_stop_tsc_emulation(void) {};
266a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
267a46d15ccSVitaly Kuznetsov {
268a46d15ccSVitaly Kuznetsov 	return NULL;
269a46d15ccSVitaly Kuznetsov }
270eb914cfeSTianyu Lan static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
271cc4edae4SLan Tianyu static inline int hyperv_flush_guest_mapping_range(u64 as,
272cc4edae4SLan Tianyu 		hyperv_fill_flush_list_func fill_func, void *data)
273cc4edae4SLan Tianyu {
274cc4edae4SLan Tianyu 	return -1;
275cc4edae4SLan Tianyu }
276faff4406STianyu Lan static inline void hv_set_register(unsigned int reg, u64 value) { }
277faff4406STianyu Lan static inline u64 hv_get_register(unsigned int reg) { return 0; }
2787fec185aSJinank Jain static inline void hv_set_non_nested_register(unsigned int reg, u64 value) { }
2797fec185aSJinank Jain static inline u64 hv_get_non_nested_register(unsigned int reg) { return 0; }
28079cadff2SVitaly Kuznetsov #endif /* CONFIG_HYPERV */
28179cadff2SVitaly Kuznetsov 
282765e33f5SMichael Kelley 
2833be1bc2fSSaurabh Sengar #ifdef CONFIG_HYPERV_VTL_MODE
2843be1bc2fSSaurabh Sengar void __init hv_vtl_init_platform(void);
2853be1bc2fSSaurabh Sengar #else
2863be1bc2fSSaurabh Sengar static inline void __init hv_vtl_init_platform(void) {}
2873be1bc2fSSaurabh Sengar #endif
2883be1bc2fSSaurabh Sengar 
289765e33f5SMichael Kelley #include <asm-generic/mshyperv.h>
290765e33f5SMichael Kelley 
291a2a47c6cSKy Srinivasan #endif
292