xref: /openbmc/linux/arch/x86/include/asm/mshyperv.h (revision 0a7a00580a4fad9a6cd28c2d825e0e5ae917e59e)
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 
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 
32*0a7a0058SSaurabh Sengar static inline unsigned char hv_get_nmi_reason(void)
33*0a7a0058SSaurabh Sengar {
34*0a7a0058SSaurabh Sengar 	return 0;
35*0a7a0058SSaurabh Sengar }
36*0a7a0058SSaurabh Sengar 
378730046cSK. Y. Srinivasan #if IS_ENABLED(CONFIG_HYPERV)
38dfe94d40SDexuan Cui extern int hyperv_init_cpuhp;
39dfe94d40SDexuan Cui 
40fc53662fSVitaly Kuznetsov extern void *hv_hypercall_pg;
41fc53662fSVitaly Kuznetsov 
4299a0f46aSWei Liu extern u64 hv_current_partition_id;
4399a0f46aSWei Liu 
44e1878402SMichael Kelley extern union hv_ghcb * __percpu *hv_ghcb_pg;
450cc4f6d9STianyu Lan 
4686b5ec35SWei Liu int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages);
4786b5ec35SWei Liu int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id);
4886b5ec35SWei Liu int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags);
4986b5ec35SWei Liu 
50fc53662fSVitaly Kuznetsov static inline u64 hv_do_hypercall(u64 control, void *input, void *output)
51fc53662fSVitaly Kuznetsov {
52fc53662fSVitaly Kuznetsov 	u64 input_address = input ? virt_to_phys(input) : 0;
53fc53662fSVitaly Kuznetsov 	u64 output_address = output ? virt_to_phys(output) : 0;
54fc53662fSVitaly Kuznetsov 	u64 hv_status;
55fc53662fSVitaly Kuznetsov 
56fc53662fSVitaly Kuznetsov #ifdef CONFIG_X86_64
57fc53662fSVitaly Kuznetsov 	if (!hv_hypercall_pg)
58fc53662fSVitaly Kuznetsov 		return U64_MAX;
59fc53662fSVitaly Kuznetsov 
60fc53662fSVitaly Kuznetsov 	__asm__ __volatile__("mov %4, %%r8\n"
61e70e5892SDavid Woodhouse 			     CALL_NOSPEC
62f5caf621SJosh Poimboeuf 			     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
63fc53662fSVitaly Kuznetsov 			       "+c" (control), "+d" (input_address)
64e70e5892SDavid Woodhouse 			     :  "r" (output_address),
65e70e5892SDavid Woodhouse 				THUNK_TARGET(hv_hypercall_pg)
66fc53662fSVitaly Kuznetsov 			     : "cc", "memory", "r8", "r9", "r10", "r11");
67fc53662fSVitaly Kuznetsov #else
68fc53662fSVitaly Kuznetsov 	u32 input_address_hi = upper_32_bits(input_address);
69fc53662fSVitaly Kuznetsov 	u32 input_address_lo = lower_32_bits(input_address);
70fc53662fSVitaly Kuznetsov 	u32 output_address_hi = upper_32_bits(output_address);
71fc53662fSVitaly Kuznetsov 	u32 output_address_lo = lower_32_bits(output_address);
72fc53662fSVitaly Kuznetsov 
73fc53662fSVitaly Kuznetsov 	if (!hv_hypercall_pg)
74fc53662fSVitaly Kuznetsov 		return U64_MAX;
75fc53662fSVitaly Kuznetsov 
76e70e5892SDavid Woodhouse 	__asm__ __volatile__(CALL_NOSPEC
77fc53662fSVitaly Kuznetsov 			     : "=A" (hv_status),
78f5caf621SJosh Poimboeuf 			       "+c" (input_address_lo), ASM_CALL_CONSTRAINT
79fc53662fSVitaly Kuznetsov 			     : "A" (control),
80fc53662fSVitaly Kuznetsov 			       "b" (input_address_hi),
81fc53662fSVitaly Kuznetsov 			       "D"(output_address_hi), "S"(output_address_lo),
82e70e5892SDavid Woodhouse 			       THUNK_TARGET(hv_hypercall_pg)
83fc53662fSVitaly Kuznetsov 			     : "cc", "memory");
84fc53662fSVitaly Kuznetsov #endif /* !x86_64 */
85fc53662fSVitaly Kuznetsov 	return hv_status;
86fc53662fSVitaly Kuznetsov }
87dee863b5SVitaly Kuznetsov 
88f0d2f5c2SJinank Jain /* Hypercall to the L0 hypervisor */
89f0d2f5c2SJinank Jain static inline u64 hv_do_nested_hypercall(u64 control, void *input, void *output)
906a8edbd0SVitaly Kuznetsov {
91f0d2f5c2SJinank Jain 	return hv_do_hypercall(control | HV_HYPERCALL_NESTED, input, output);
92f0d2f5c2SJinank Jain }
93f0d2f5c2SJinank Jain 
94f0d2f5c2SJinank Jain /* Fast hypercall with 8 bytes of input and no output */
95f0d2f5c2SJinank Jain static inline u64 _hv_do_fast_hypercall8(u64 control, u64 input1)
96f0d2f5c2SJinank Jain {
97f0d2f5c2SJinank Jain 	u64 hv_status;
986a8edbd0SVitaly Kuznetsov 
996a8edbd0SVitaly Kuznetsov #ifdef CONFIG_X86_64
1006a8edbd0SVitaly Kuznetsov 	{
101e70e5892SDavid Woodhouse 		__asm__ __volatile__(CALL_NOSPEC
102f5caf621SJosh Poimboeuf 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
1036a8edbd0SVitaly Kuznetsov 				       "+c" (control), "+d" (input1)
104e70e5892SDavid Woodhouse 				     : THUNK_TARGET(hv_hypercall_pg)
1056a8edbd0SVitaly Kuznetsov 				     : "cc", "r8", "r9", "r10", "r11");
1066a8edbd0SVitaly Kuznetsov 	}
1076a8edbd0SVitaly Kuznetsov #else
1086a8edbd0SVitaly Kuznetsov 	{
1096a8edbd0SVitaly Kuznetsov 		u32 input1_hi = upper_32_bits(input1);
1106a8edbd0SVitaly Kuznetsov 		u32 input1_lo = lower_32_bits(input1);
1116a8edbd0SVitaly Kuznetsov 
112e70e5892SDavid Woodhouse 		__asm__ __volatile__ (CALL_NOSPEC
1136a8edbd0SVitaly Kuznetsov 				      : "=A"(hv_status),
1146a8edbd0SVitaly Kuznetsov 					"+c"(input1_lo),
115f5caf621SJosh Poimboeuf 					ASM_CALL_CONSTRAINT
1166a8edbd0SVitaly Kuznetsov 				      :	"A" (control),
1176a8edbd0SVitaly Kuznetsov 					"b" (input1_hi),
118e70e5892SDavid Woodhouse 					THUNK_TARGET(hv_hypercall_pg)
1196a8edbd0SVitaly Kuznetsov 				      : "cc", "edi", "esi");
1206a8edbd0SVitaly Kuznetsov 	}
1216a8edbd0SVitaly Kuznetsov #endif
1226a8edbd0SVitaly Kuznetsov 		return hv_status;
1236a8edbd0SVitaly Kuznetsov }
1246a8edbd0SVitaly Kuznetsov 
125f0d2f5c2SJinank Jain static inline u64 hv_do_fast_hypercall8(u16 code, u64 input1)
12653e52966SVitaly Kuznetsov {
127f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
128f0d2f5c2SJinank Jain 
129f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall8(control, input1);
130f0d2f5c2SJinank Jain }
131f0d2f5c2SJinank Jain 
132f0d2f5c2SJinank Jain static inline u64 hv_do_fast_nested_hypercall8(u16 code, u64 input1)
133f0d2f5c2SJinank Jain {
134f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
135f0d2f5c2SJinank Jain 
136f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall8(control, input1);
137f0d2f5c2SJinank Jain }
138f0d2f5c2SJinank Jain 
139f0d2f5c2SJinank Jain /* Fast hypercall with 16 bytes of input */
140f0d2f5c2SJinank Jain static inline u64 _hv_do_fast_hypercall16(u64 control, u64 input1, u64 input2)
141f0d2f5c2SJinank Jain {
142f0d2f5c2SJinank Jain 	u64 hv_status;
14353e52966SVitaly Kuznetsov 
14453e52966SVitaly Kuznetsov #ifdef CONFIG_X86_64
14553e52966SVitaly Kuznetsov 	{
14653e52966SVitaly Kuznetsov 		__asm__ __volatile__("mov %4, %%r8\n"
14753e52966SVitaly Kuznetsov 				     CALL_NOSPEC
14853e52966SVitaly Kuznetsov 				     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
14953e52966SVitaly Kuznetsov 				       "+c" (control), "+d" (input1)
15053e52966SVitaly Kuznetsov 				     : "r" (input2),
15153e52966SVitaly Kuznetsov 				       THUNK_TARGET(hv_hypercall_pg)
15253e52966SVitaly Kuznetsov 				     : "cc", "r8", "r9", "r10", "r11");
15353e52966SVitaly Kuznetsov 	}
15453e52966SVitaly Kuznetsov #else
15553e52966SVitaly Kuznetsov 	{
15653e52966SVitaly Kuznetsov 		u32 input1_hi = upper_32_bits(input1);
15753e52966SVitaly Kuznetsov 		u32 input1_lo = lower_32_bits(input1);
15853e52966SVitaly Kuznetsov 		u32 input2_hi = upper_32_bits(input2);
15953e52966SVitaly Kuznetsov 		u32 input2_lo = lower_32_bits(input2);
16053e52966SVitaly Kuznetsov 
16153e52966SVitaly Kuznetsov 		__asm__ __volatile__ (CALL_NOSPEC
16253e52966SVitaly Kuznetsov 				      : "=A"(hv_status),
16353e52966SVitaly Kuznetsov 					"+c"(input1_lo), ASM_CALL_CONSTRAINT
16453e52966SVitaly Kuznetsov 				      :	"A" (control), "b" (input1_hi),
16553e52966SVitaly Kuznetsov 					"D"(input2_hi), "S"(input2_lo),
16653e52966SVitaly Kuznetsov 					THUNK_TARGET(hv_hypercall_pg)
16753e52966SVitaly Kuznetsov 				      : "cc");
16853e52966SVitaly Kuznetsov 	}
16953e52966SVitaly Kuznetsov #endif
17053e52966SVitaly Kuznetsov 	return hv_status;
17153e52966SVitaly Kuznetsov }
17253e52966SVitaly Kuznetsov 
173f0d2f5c2SJinank Jain static inline u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
174f0d2f5c2SJinank Jain {
175f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT;
176f0d2f5c2SJinank Jain 
177f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall16(control, input1, input2);
178f0d2f5c2SJinank Jain }
179f0d2f5c2SJinank Jain 
180f0d2f5c2SJinank Jain static inline u64 hv_do_fast_nested_hypercall16(u16 code, u64 input1, u64 input2)
181f0d2f5c2SJinank Jain {
182f0d2f5c2SJinank Jain 	u64 control = (u64)code | HV_HYPERCALL_FAST_BIT | HV_HYPERCALL_NESTED;
183f0d2f5c2SJinank Jain 
184f0d2f5c2SJinank Jain 	return _hv_do_fast_hypercall16(control, input1, input2);
185f0d2f5c2SJinank Jain }
186f0d2f5c2SJinank Jain 
187a46d15ccSVitaly Kuznetsov extern struct hv_vp_assist_page **hv_vp_assist_page;
188a46d15ccSVitaly Kuznetsov 
189a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
190a46d15ccSVitaly Kuznetsov {
191a46d15ccSVitaly Kuznetsov 	if (!hv_vp_assist_page)
192a46d15ccSVitaly Kuznetsov 		return NULL;
193a46d15ccSVitaly Kuznetsov 
194a46d15ccSVitaly Kuznetsov 	return hv_vp_assist_page[cpu];
195a46d15ccSVitaly Kuznetsov }
1967415aea6SVitaly Kuznetsov 
1976b48cb5fSK. Y. Srinivasan void __init hyperv_init(void);
1982ffd9e33SVitaly Kuznetsov void hyperv_setup_mmu_ops(void);
19993286261SVitaly Kuznetsov void set_hv_tscchange_cb(void (*cb)(void));
20093286261SVitaly Kuznetsov void clear_hv_tscchange_cb(void);
20193286261SVitaly Kuznetsov void hyperv_stop_tsc_emulation(void);
202eb914cfeSTianyu Lan int hyperv_flush_guest_mapping(u64 as);
203cc4edae4SLan Tianyu int hyperv_flush_guest_mapping_range(u64 as,
204cc4edae4SLan Tianyu 		hyperv_fill_flush_list_func fill_func, void *data);
205cc4edae4SLan Tianyu int hyperv_fill_flush_guest_mapping_list(
206cc4edae4SLan Tianyu 		struct hv_guest_mapping_flush_list *flush,
207cc4edae4SLan Tianyu 		u64 start_gfn, u64 end_gfn);
2082d2ccf24SThomas Gleixner 
2092d2ccf24SThomas Gleixner #ifdef CONFIG_X86_64
2106b48cb5fSK. Y. Srinivasan void hv_apic_init(void);
2113a025de6SYi Sun void __init hv_init_spinlocks(void);
2123a025de6SYi Sun bool hv_vcpu_is_preempted(int vcpu);
2132d2ccf24SThomas Gleixner #else
2142d2ccf24SThomas Gleixner static inline void hv_apic_init(void) {}
2152d2ccf24SThomas Gleixner #endif
2162d2ccf24SThomas Gleixner 
217e39397d1SWei Liu struct irq_domain *hv_create_pci_msi_domain(void);
218e39397d1SWei Liu 
219fb5ef351SWei Liu int hv_map_ioapic_interrupt(int ioapic_id, bool level, int vcpu, int vector,
220fb5ef351SWei Liu 		struct hv_interrupt_entry *entry);
221fb5ef351SWei Liu int hv_unmap_ioapic_interrupt(int ioapic_id, struct hv_interrupt_entry *entry);
222faff4406STianyu Lan 
223faff4406STianyu Lan #ifdef CONFIG_AMD_MEM_ENCRYPT
224faff4406STianyu Lan void hv_ghcb_msr_write(u64 msr, u64 value);
225faff4406STianyu Lan void hv_ghcb_msr_read(u64 msr, u64 *value);
22649d6a3c0STianyu Lan bool hv_ghcb_negotiate_protocol(void);
22749d6a3c0STianyu Lan void hv_ghcb_terminate(unsigned int set, unsigned int reason);
228812b0597SMichael Kelley void hv_vtom_init(void);
229faff4406STianyu Lan #else
230faff4406STianyu Lan static inline void hv_ghcb_msr_write(u64 msr, u64 value) {}
231faff4406STianyu Lan static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {}
23249d6a3c0STianyu Lan static inline bool hv_ghcb_negotiate_protocol(void) { return false; }
23349d6a3c0STianyu Lan static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {}
234812b0597SMichael Kelley static inline void hv_vtom_init(void) {}
235faff4406STianyu Lan #endif
236faff4406STianyu Lan 
237faff4406STianyu Lan extern bool hv_isolation_type_snp(void);
238faff4406STianyu Lan 
239faff4406STianyu Lan static inline bool hv_is_synic_reg(unsigned int reg)
240faff4406STianyu Lan {
241b14033a3SNuno Das Neves 	return (reg >= HV_REGISTER_SCONTROL) &&
242b14033a3SNuno Das Neves 	       (reg <= HV_REGISTER_SINT15);
243b14033a3SNuno Das Neves }
244b14033a3SNuno Das Neves 
245b14033a3SNuno Das Neves static inline bool hv_is_sint_reg(unsigned int reg)
246b14033a3SNuno Das Neves {
247b14033a3SNuno Das Neves 	return (reg >= HV_REGISTER_SINT0) &&
248b14033a3SNuno Das Neves 	       (reg <= HV_REGISTER_SINT15);
249faff4406STianyu Lan }
250faff4406STianyu Lan 
2517fec185aSJinank Jain u64 hv_get_register(unsigned int reg);
2527fec185aSJinank Jain void hv_set_register(unsigned int reg, u64 value);
2537fec185aSJinank Jain u64 hv_get_non_nested_register(unsigned int reg);
2547fec185aSJinank Jain void hv_set_non_nested_register(unsigned int reg, u64 value);
255faff4406STianyu Lan 
25679cadff2SVitaly Kuznetsov #else /* CONFIG_HYPERV */
25779cadff2SVitaly Kuznetsov static inline void hyperv_init(void) {}
2582ffd9e33SVitaly Kuznetsov static inline void hyperv_setup_mmu_ops(void) {}
25993286261SVitaly Kuznetsov static inline void set_hv_tscchange_cb(void (*cb)(void)) {}
26093286261SVitaly Kuznetsov static inline void clear_hv_tscchange_cb(void) {}
26193286261SVitaly Kuznetsov static inline void hyperv_stop_tsc_emulation(void) {};
262a46d15ccSVitaly Kuznetsov static inline struct hv_vp_assist_page *hv_get_vp_assist_page(unsigned int cpu)
263a46d15ccSVitaly Kuznetsov {
264a46d15ccSVitaly Kuznetsov 	return NULL;
265a46d15ccSVitaly Kuznetsov }
266eb914cfeSTianyu Lan static inline int hyperv_flush_guest_mapping(u64 as) { return -1; }
267cc4edae4SLan Tianyu static inline int hyperv_flush_guest_mapping_range(u64 as,
268cc4edae4SLan Tianyu 		hyperv_fill_flush_list_func fill_func, void *data)
269cc4edae4SLan Tianyu {
270cc4edae4SLan Tianyu 	return -1;
271cc4edae4SLan Tianyu }
272faff4406STianyu Lan static inline void hv_set_register(unsigned int reg, u64 value) { }
273faff4406STianyu Lan static inline u64 hv_get_register(unsigned int reg) { return 0; }
2747fec185aSJinank Jain static inline void hv_set_non_nested_register(unsigned int reg, u64 value) { }
2757fec185aSJinank Jain static inline u64 hv_get_non_nested_register(unsigned int reg) { return 0; }
27679cadff2SVitaly Kuznetsov #endif /* CONFIG_HYPERV */
27779cadff2SVitaly Kuznetsov 
278765e33f5SMichael Kelley 
279765e33f5SMichael Kelley #include <asm-generic/mshyperv.h>
280765e33f5SMichael Kelley 
281a2a47c6cSKy Srinivasan #endif
282