xref: /openbmc/linux/arch/x86/hyperv/hv_init.c (revision 769218209d8f00a4003dc9ee351198a473043eaf)
143aa3132SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28730046cSK. Y. Srinivasan /*
38730046cSK. Y. Srinivasan  * X86 specific Hyper-V initialization code.
48730046cSK. Y. Srinivasan  *
58730046cSK. Y. Srinivasan  * Copyright (C) 2016, Microsoft, Inc.
68730046cSK. Y. Srinivasan  *
78730046cSK. Y. Srinivasan  * Author : K. Y. Srinivasan <kys@microsoft.com>
88730046cSK. Y. Srinivasan  */
98730046cSK. Y. Srinivasan 
10203a521bSSaurabh Sengar #define pr_fmt(fmt)  "Hyper-V: " fmt
11203a521bSSaurabh Sengar 
122f285f46SDexuan Cui #include <linux/efi.h>
138730046cSK. Y. Srinivasan #include <linux/types.h>
14a6c76bb0SAndrea Parri (Microsoft) #include <linux/bitfield.h>
156dc77fa5SMichael Kelley #include <linux/io.h>
1693286261SVitaly Kuznetsov #include <asm/apic.h>
1793286261SVitaly Kuznetsov #include <asm/desc.h>
1854d8c1d3SSaurabh Sengar #include <asm/e820/api.h>
1949d6a3c0STianyu Lan #include <asm/sev.h>
20d5ace2a7SMichael Kelley #include <asm/ibt.h>
218730046cSK. Y. Srinivasan #include <asm/hypervisor.h>
225a485803SVitaly Kuznetsov #include <asm/hyperv-tlfs.h>
238730046cSK. Y. Srinivasan #include <asm/mshyperv.h>
24a16be368SThomas Gleixner #include <asm/idtentry.h>
25b1310355STianyu Lan #include <asm/set_memory.h>
26dfe94d40SDexuan Cui #include <linux/kexec.h>
278730046cSK. Y. Srinivasan #include <linux/version.h>
288730046cSK. Y. Srinivasan #include <linux/vmalloc.h>
298730046cSK. Y. Srinivasan #include <linux/mm.h>
3067071816SStephen Hemminger #include <linux/hyperv.h>
317415aea6SVitaly Kuznetsov #include <linux/slab.h>
32f3a99e76STianyu Lan #include <linux/kernel.h>
337415aea6SVitaly Kuznetsov #include <linux/cpuhotplug.h>
3405bd330aSDexuan Cui #include <linux/syscore_ops.h>
35dd2cb348SMichael Kelley #include <clocksource/hyperv_timer.h>
3680f73c9fSWei Liu #include <linux/highmem.h>
378730046cSK. Y. Srinivasan 
3899a0f46aSWei Liu u64 hv_current_partition_id = ~0ull;
3999a0f46aSWei Liu EXPORT_SYMBOL_GPL(hv_current_partition_id);
40dfe94d40SDexuan Cui 
41fc53662fSVitaly Kuznetsov void *hv_hypercall_pg;
42fc53662fSVitaly Kuznetsov EXPORT_SYMBOL_GPL(hv_hypercall_pg);
43dee863b5SVitaly Kuznetsov 
44e1878402SMichael Kelley union hv_ghcb * __percpu *hv_ghcb_pg;
450cc4f6d9STianyu Lan 
4605bd330aSDexuan Cui /* Storage to save the hypercall page temporarily for hibernation */
4705bd330aSDexuan Cui static void *hv_hypercall_pg_saved;
4805bd330aSDexuan Cui 
49a46d15ccSVitaly Kuznetsov struct hv_vp_assist_page **hv_vp_assist_page;
50a46d15ccSVitaly Kuznetsov EXPORT_SYMBOL_GPL(hv_vp_assist_page);
51a46d15ccSVitaly Kuznetsov 
hyperv_init_ghcb(void)520cc4f6d9STianyu Lan static int hyperv_init_ghcb(void)
530cc4f6d9STianyu Lan {
540cc4f6d9STianyu Lan 	u64 ghcb_gpa;
550cc4f6d9STianyu Lan 	void *ghcb_va;
560cc4f6d9STianyu Lan 	void **ghcb_base;
570cc4f6d9STianyu Lan 
58e3131f1cSDexuan Cui 	if (!ms_hyperv.paravisor_present || !hv_isolation_type_snp())
590cc4f6d9STianyu Lan 		return 0;
600cc4f6d9STianyu Lan 
610cc4f6d9STianyu Lan 	if (!hv_ghcb_pg)
620cc4f6d9STianyu Lan 		return -EINVAL;
630cc4f6d9STianyu Lan 
640cc4f6d9STianyu Lan 	/*
650cc4f6d9STianyu Lan 	 * GHCB page is allocated by paravisor. The address
660cc4f6d9STianyu Lan 	 * returned by MSR_AMD64_SEV_ES_GHCB is above shared
670cc4f6d9STianyu Lan 	 * memory boundary and map it here.
680cc4f6d9STianyu Lan 	 */
690cc4f6d9STianyu Lan 	rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa);
706afd9dc1SMichael Kelley 
716afd9dc1SMichael Kelley 	/* Mask out vTOM bit. ioremap_cache() maps decrypted */
726afd9dc1SMichael Kelley 	ghcb_gpa &= ~ms_hyperv.shared_gpa_boundary;
736afd9dc1SMichael Kelley 	ghcb_va = (void *)ioremap_cache(ghcb_gpa, HV_HYP_PAGE_SIZE);
740cc4f6d9STianyu Lan 	if (!ghcb_va)
750cc4f6d9STianyu Lan 		return -ENOMEM;
760cc4f6d9STianyu Lan 
770cc4f6d9STianyu Lan 	ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
780cc4f6d9STianyu Lan 	*ghcb_base = ghcb_va;
790cc4f6d9STianyu Lan 
800cc4f6d9STianyu Lan 	return 0;
810cc4f6d9STianyu Lan }
820cc4f6d9STianyu Lan 
hv_cpu_init(unsigned int cpu)837415aea6SVitaly Kuznetsov static int hv_cpu_init(unsigned int cpu)
847415aea6SVitaly Kuznetsov {
85e5d9b714SPraveen Kumar 	union hv_vp_assist_msr_contents msr = { 0 };
8668f2f2bcSDexuan Cui 	struct hv_vp_assist_page **hvp;
87afca4d95SMichael Kelley 	int ret;
8868bb7bfbSK. Y. Srinivasan 
89afca4d95SMichael Kelley 	ret = hv_common_cpu_init(cpu);
90afca4d95SMichael Kelley 	if (ret)
91afca4d95SMichael Kelley 		return ret;
92a3b74243SVitaly Kuznetsov 
93a46d15ccSVitaly Kuznetsov 	if (!hv_vp_assist_page)
94a46d15ccSVitaly Kuznetsov 		return 0;
95a46d15ccSVitaly Kuznetsov 
9668f2f2bcSDexuan Cui 	hvp = &hv_vp_assist_page[cpu];
97e5d9b714SPraveen Kumar 	if (hv_root_partition) {
98e5d9b714SPraveen Kumar 		/*
99e5d9b714SPraveen Kumar 		 * For root partition we get the hypervisor provided VP assist
100e5d9b714SPraveen Kumar 		 * page, instead of allocating a new page.
101e5d9b714SPraveen Kumar 		 */
102e5d9b714SPraveen Kumar 		rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
103ee681541SVitaly Kuznetsov 		*hvp = memremap(msr.pfn << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT,
104e5d9b714SPraveen Kumar 				PAGE_SIZE, MEMREMAP_WB);
105e5d9b714SPraveen Kumar 	} else {
106e5d9b714SPraveen Kumar 		/*
107e5d9b714SPraveen Kumar 		 * The VP assist page is an "overlay" page (see Hyper-V TLFS's
108e5d9b714SPraveen Kumar 		 * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed
109e5d9b714SPraveen Kumar 		 * out to make sure we always write the EOI MSR in
110e5d9b714SPraveen Kumar 		 * hv_apic_eoi_write() *after* the EOI optimization is disabled
111e5d9b714SPraveen Kumar 		 * in hv_cpu_die(), otherwise a CPU may not be stopped in the
112e5d9b714SPraveen Kumar 		 * case of CPU offlining and the VM will hang.
113e5d9b714SPraveen Kumar 		 */
114b1310355STianyu Lan 		if (!*hvp) {
11588dca4caSChristoph Hellwig 			*hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
116b1310355STianyu Lan 
117b1310355STianyu Lan 			/*
118b1310355STianyu Lan 			 * Hyper-V should never specify a VM that is a Confidential
119b1310355STianyu Lan 			 * VM and also running in the root partition. Root partition
120b1310355STianyu Lan 			 * is blocked to run in Confidential VM. So only decrypt assist
121b1310355STianyu Lan 			 * page in non-root partition here.
122b1310355STianyu Lan 			 */
123e3131f1cSDexuan Cui 			if (*hvp && !ms_hyperv.paravisor_present && hv_isolation_type_snp()) {
124b1310355STianyu Lan 				WARN_ON_ONCE(set_memory_decrypted((unsigned long)(*hvp), 1));
125b1310355STianyu Lan 				memset(*hvp, 0, PAGE_SIZE);
126b1310355STianyu Lan 			}
127b1310355STianyu Lan 		}
128b1310355STianyu Lan 
129e5d9b714SPraveen Kumar 		if (*hvp)
130e5d9b714SPraveen Kumar 			msr.pfn = vmalloc_to_pfn(*hvp);
131ee681541SVitaly Kuznetsov 
132e320ab3cSDexuan Cui 	}
133ee681541SVitaly Kuznetsov 	if (!WARN_ON(!(*hvp))) {
134e5d9b714SPraveen Kumar 		msr.enable = 1;
135e5d9b714SPraveen Kumar 		wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
136e5d9b714SPraveen Kumar 	}
137a46d15ccSVitaly Kuznetsov 
1380cc4f6d9STianyu Lan 	return hyperv_init_ghcb();
1397415aea6SVitaly Kuznetsov }
1407415aea6SVitaly Kuznetsov 
14193286261SVitaly Kuznetsov static void (*hv_reenlightenment_cb)(void);
14293286261SVitaly Kuznetsov 
hv_reenlightenment_notify(struct work_struct * dummy)14393286261SVitaly Kuznetsov static void hv_reenlightenment_notify(struct work_struct *dummy)
14493286261SVitaly Kuznetsov {
14593286261SVitaly Kuznetsov 	struct hv_tsc_emulation_status emu_status;
14693286261SVitaly Kuznetsov 
14793286261SVitaly Kuznetsov 	rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
14893286261SVitaly Kuznetsov 
14993286261SVitaly Kuznetsov 	/* Don't issue the callback if TSC accesses are not emulated */
15093286261SVitaly Kuznetsov 	if (hv_reenlightenment_cb && emu_status.inprogress)
15193286261SVitaly Kuznetsov 		hv_reenlightenment_cb();
15293286261SVitaly Kuznetsov }
15393286261SVitaly Kuznetsov static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
15493286261SVitaly Kuznetsov 
hyperv_stop_tsc_emulation(void)15593286261SVitaly Kuznetsov void hyperv_stop_tsc_emulation(void)
15693286261SVitaly Kuznetsov {
15793286261SVitaly Kuznetsov 	u64 freq;
15893286261SVitaly Kuznetsov 	struct hv_tsc_emulation_status emu_status;
15993286261SVitaly Kuznetsov 
16093286261SVitaly Kuznetsov 	rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
16193286261SVitaly Kuznetsov 	emu_status.inprogress = 0;
16293286261SVitaly Kuznetsov 	wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
16393286261SVitaly Kuznetsov 
16493286261SVitaly Kuznetsov 	rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
16593286261SVitaly Kuznetsov 	tsc_khz = div64_u64(freq, 1000);
16693286261SVitaly Kuznetsov }
16793286261SVitaly Kuznetsov EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
16893286261SVitaly Kuznetsov 
hv_reenlightenment_available(void)16993286261SVitaly Kuznetsov static inline bool hv_reenlightenment_available(void)
17093286261SVitaly Kuznetsov {
17193286261SVitaly Kuznetsov 	/*
172d9f6e12fSIngo Molnar 	 * Check for required features and privileges to make TSC frequency
17393286261SVitaly Kuznetsov 	 * change notifications work.
17493286261SVitaly Kuznetsov 	 */
175dfc53baaSJoseph Salisbury 	return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
17693286261SVitaly Kuznetsov 		ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
177dfc53baaSJoseph Salisbury 		ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
17893286261SVitaly Kuznetsov }
17993286261SVitaly Kuznetsov 
DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)180a16be368SThomas Gleixner DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
18193286261SVitaly Kuznetsov {
182670c04adSDave Hansen 	apic_eoi();
18351d4e5daSVitaly Kuznetsov 	inc_irq_stat(irq_hv_reenlightenment_count);
18493286261SVitaly Kuznetsov 	schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
18593286261SVitaly Kuznetsov }
18693286261SVitaly Kuznetsov 
set_hv_tscchange_cb(void (* cb)(void))18793286261SVitaly Kuznetsov void set_hv_tscchange_cb(void (*cb)(void))
18893286261SVitaly Kuznetsov {
18993286261SVitaly Kuznetsov 	struct hv_reenlightenment_control re_ctrl = {
19093286261SVitaly Kuznetsov 		.vector = HYPERV_REENLIGHTENMENT_VECTOR,
19193286261SVitaly Kuznetsov 		.enabled = 1,
19293286261SVitaly Kuznetsov 	};
19393286261SVitaly Kuznetsov 	struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
19493286261SVitaly Kuznetsov 
19593286261SVitaly Kuznetsov 	if (!hv_reenlightenment_available()) {
196203a521bSSaurabh Sengar 		pr_warn("reenlightenment support is unavailable\n");
19793286261SVitaly Kuznetsov 		return;
19893286261SVitaly Kuznetsov 	}
19993286261SVitaly Kuznetsov 
200daf97211SSean Christopherson 	if (!hv_vp_index)
201daf97211SSean Christopherson 		return;
202daf97211SSean Christopherson 
20393286261SVitaly Kuznetsov 	hv_reenlightenment_cb = cb;
20493286261SVitaly Kuznetsov 
20593286261SVitaly Kuznetsov 	/* Make sure callback is registered before we write to MSRs */
20693286261SVitaly Kuznetsov 	wmb();
20793286261SVitaly Kuznetsov 
208285f68afSVitaly Kuznetsov 	re_ctrl.target_vp = hv_vp_index[get_cpu()];
209285f68afSVitaly Kuznetsov 
21093286261SVitaly Kuznetsov 	wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
21193286261SVitaly Kuznetsov 	wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
212285f68afSVitaly Kuznetsov 
213285f68afSVitaly Kuznetsov 	put_cpu();
21493286261SVitaly Kuznetsov }
21593286261SVitaly Kuznetsov EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
21693286261SVitaly Kuznetsov 
clear_hv_tscchange_cb(void)21793286261SVitaly Kuznetsov void clear_hv_tscchange_cb(void)
21893286261SVitaly Kuznetsov {
21993286261SVitaly Kuznetsov 	struct hv_reenlightenment_control re_ctrl;
22093286261SVitaly Kuznetsov 
22193286261SVitaly Kuznetsov 	if (!hv_reenlightenment_available())
22293286261SVitaly Kuznetsov 		return;
22393286261SVitaly Kuznetsov 
22493286261SVitaly Kuznetsov 	rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
22593286261SVitaly Kuznetsov 	re_ctrl.enabled = 0;
22693286261SVitaly Kuznetsov 	wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
22793286261SVitaly Kuznetsov 
22893286261SVitaly Kuznetsov 	hv_reenlightenment_cb = NULL;
22993286261SVitaly Kuznetsov }
23093286261SVitaly Kuznetsov EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
23193286261SVitaly Kuznetsov 
hv_cpu_die(unsigned int cpu)232e7c4e36cSVitaly Kuznetsov static int hv_cpu_die(unsigned int cpu)
233e7c4e36cSVitaly Kuznetsov {
234e7c4e36cSVitaly Kuznetsov 	struct hv_reenlightenment_control re_ctrl;
235e7c4e36cSVitaly Kuznetsov 	unsigned int new_cpu;
2360cc4f6d9STianyu Lan 	void **ghcb_va;
2370cc4f6d9STianyu Lan 
2380cc4f6d9STianyu Lan 	if (hv_ghcb_pg) {
2390cc4f6d9STianyu Lan 		ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg);
2400cc4f6d9STianyu Lan 		if (*ghcb_va)
2416afd9dc1SMichael Kelley 			iounmap(*ghcb_va);
2420cc4f6d9STianyu Lan 		*ghcb_va = NULL;
2430cc4f6d9STianyu Lan 	}
24468bb7bfbSK. Y. Srinivasan 
245afca4d95SMichael Kelley 	hv_common_cpu_die(cpu);
246e7c4e36cSVitaly Kuznetsov 
247e5d9b714SPraveen Kumar 	if (hv_vp_assist_page && hv_vp_assist_page[cpu]) {
248e5d9b714SPraveen Kumar 		union hv_vp_assist_msr_contents msr = { 0 };
249e5d9b714SPraveen Kumar 		if (hv_root_partition) {
250e5d9b714SPraveen Kumar 			/*
251e5d9b714SPraveen Kumar 			 * For root partition the VP assist page is mapped to
252e5d9b714SPraveen Kumar 			 * hypervisor provided page, and thus we unmap the
253e5d9b714SPraveen Kumar 			 * page here and nullify it, so that in future we have
254e5d9b714SPraveen Kumar 			 * correct page address mapped in hv_cpu_init.
255e5d9b714SPraveen Kumar 			 */
256e5d9b714SPraveen Kumar 			memunmap(hv_vp_assist_page[cpu]);
257e5d9b714SPraveen Kumar 			hv_vp_assist_page[cpu] = NULL;
258e5d9b714SPraveen Kumar 			rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
259e5d9b714SPraveen Kumar 			msr.enable = 0;
260e5d9b714SPraveen Kumar 		}
261e5d9b714SPraveen Kumar 		wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
262e5d9b714SPraveen Kumar 	}
263a46d15ccSVitaly Kuznetsov 
264e7c4e36cSVitaly Kuznetsov 	if (hv_reenlightenment_cb == NULL)
265e7c4e36cSVitaly Kuznetsov 		return 0;
266e7c4e36cSVitaly Kuznetsov 
267e7c4e36cSVitaly Kuznetsov 	rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
268e7c4e36cSVitaly Kuznetsov 	if (re_ctrl.target_vp == hv_vp_index[cpu]) {
26938dce419SVitaly Kuznetsov 		/*
27038dce419SVitaly Kuznetsov 		 * Reassign reenlightenment notifications to some other online
27138dce419SVitaly Kuznetsov 		 * CPU or just disable the feature if there are no online CPUs
27238dce419SVitaly Kuznetsov 		 * left (happens on hibernation).
27338dce419SVitaly Kuznetsov 		 */
274e7c4e36cSVitaly Kuznetsov 		new_cpu = cpumask_any_but(cpu_online_mask, cpu);
275e7c4e36cSVitaly Kuznetsov 
27638dce419SVitaly Kuznetsov 		if (new_cpu < nr_cpu_ids)
277e7c4e36cSVitaly Kuznetsov 			re_ctrl.target_vp = hv_vp_index[new_cpu];
27838dce419SVitaly Kuznetsov 		else
27938dce419SVitaly Kuznetsov 			re_ctrl.enabled = 0;
28038dce419SVitaly Kuznetsov 
281e7c4e36cSVitaly Kuznetsov 		wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
282e7c4e36cSVitaly Kuznetsov 	}
283e7c4e36cSVitaly Kuznetsov 
284e7c4e36cSVitaly Kuznetsov 	return 0;
285e7c4e36cSVitaly Kuznetsov }
286e7c4e36cSVitaly Kuznetsov 
hv_pci_init(void)2872f285f46SDexuan Cui static int __init hv_pci_init(void)
2882f285f46SDexuan Cui {
28954d8c1d3SSaurabh Sengar 	bool gen2vm = efi_enabled(EFI_BOOT);
2902f285f46SDexuan Cui 
2912f285f46SDexuan Cui 	/*
29254d8c1d3SSaurabh Sengar 	 * A Generation-2 VM doesn't support legacy PCI/PCIe, so both
29354d8c1d3SSaurabh Sengar 	 * raw_pci_ops and raw_pci_ext_ops are NULL, and pci_subsys_init() ->
29454d8c1d3SSaurabh Sengar 	 * pcibios_init() doesn't call pcibios_resource_survey() ->
29554d8c1d3SSaurabh Sengar 	 * e820__reserve_resources_late(); as a result, any emulated persistent
29654d8c1d3SSaurabh Sengar 	 * memory of E820_TYPE_PRAM (12) via the kernel parameter
29754d8c1d3SSaurabh Sengar 	 * memmap=nn[KMG]!ss is not added into iomem_resource and hence can't be
29854d8c1d3SSaurabh Sengar 	 * detected by register_e820_pmem(). Fix this by directly calling
29954d8c1d3SSaurabh Sengar 	 * e820__reserve_resources_late() here: e820__reserve_resources_late()
30054d8c1d3SSaurabh Sengar 	 * depends on e820__reserve_resources(), which has been called earlier
30154d8c1d3SSaurabh Sengar 	 * from setup_arch(). Note: e820__reserve_resources_late() also adds
30254d8c1d3SSaurabh Sengar 	 * any memory of E820_TYPE_PMEM (7) into iomem_resource, and
30354d8c1d3SSaurabh Sengar 	 * acpi_nfit_register_region() -> acpi_nfit_insert_resource() ->
30454d8c1d3SSaurabh Sengar 	 * region_intersects() returns REGION_INTERSECTS, so the memory of
30554d8c1d3SSaurabh Sengar 	 * E820_TYPE_PMEM won't get added twice.
30654d8c1d3SSaurabh Sengar 	 *
30754d8c1d3SSaurabh Sengar 	 * We return 0 here so that pci_arch_init() won't print the warning:
3082f285f46SDexuan Cui 	 * "PCI: Fatal: No config space access function found"
3092f285f46SDexuan Cui 	 */
31054d8c1d3SSaurabh Sengar 	if (gen2vm) {
31154d8c1d3SSaurabh Sengar 		e820__reserve_resources_late();
3122f285f46SDexuan Cui 		return 0;
31354d8c1d3SSaurabh Sengar 	}
3142f285f46SDexuan Cui 
3152f285f46SDexuan Cui 	/* For Generation-1 VM, we'll proceed in pci_arch_init().  */
3162f285f46SDexuan Cui 	return 1;
3172f285f46SDexuan Cui }
3182f285f46SDexuan Cui 
hv_suspend(void)31905bd330aSDexuan Cui static int hv_suspend(void)
32005bd330aSDexuan Cui {
32105bd330aSDexuan Cui 	union hv_x64_msr_hypercall_contents hypercall_msr;
322421f090cSDexuan Cui 	int ret;
32305bd330aSDexuan Cui 
32480f73c9fSWei Liu 	if (hv_root_partition)
32580f73c9fSWei Liu 		return -EPERM;
32680f73c9fSWei Liu 
32705bd330aSDexuan Cui 	/*
32805bd330aSDexuan Cui 	 * Reset the hypercall page as it is going to be invalidated
329d9f6e12fSIngo Molnar 	 * across hibernation. Setting hv_hypercall_pg to NULL ensures
33005bd330aSDexuan Cui 	 * that any subsequent hypercall operation fails safely instead of
33105bd330aSDexuan Cui 	 * crashing due to an access of an invalid page. The hypercall page
33205bd330aSDexuan Cui 	 * pointer is restored on resume.
33305bd330aSDexuan Cui 	 */
33405bd330aSDexuan Cui 	hv_hypercall_pg_saved = hv_hypercall_pg;
33505bd330aSDexuan Cui 	hv_hypercall_pg = NULL;
33605bd330aSDexuan Cui 
33705bd330aSDexuan Cui 	/* Disable the hypercall page in the hypervisor */
33805bd330aSDexuan Cui 	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
33905bd330aSDexuan Cui 	hypercall_msr.enable = 0;
34005bd330aSDexuan Cui 	wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
34105bd330aSDexuan Cui 
342421f090cSDexuan Cui 	ret = hv_cpu_die(0);
343421f090cSDexuan Cui 	return ret;
34405bd330aSDexuan Cui }
34505bd330aSDexuan Cui 
hv_resume(void)34605bd330aSDexuan Cui static void hv_resume(void)
34705bd330aSDexuan Cui {
34805bd330aSDexuan Cui 	union hv_x64_msr_hypercall_contents hypercall_msr;
349421f090cSDexuan Cui 	int ret;
350421f090cSDexuan Cui 
351421f090cSDexuan Cui 	ret = hv_cpu_init(0);
352421f090cSDexuan Cui 	WARN_ON(ret);
35305bd330aSDexuan Cui 
35405bd330aSDexuan Cui 	/* Re-enable the hypercall page */
35505bd330aSDexuan Cui 	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
35605bd330aSDexuan Cui 	hypercall_msr.enable = 1;
35705bd330aSDexuan Cui 	hypercall_msr.guest_physical_address =
35805bd330aSDexuan Cui 		vmalloc_to_pfn(hv_hypercall_pg_saved);
35905bd330aSDexuan Cui 	wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
36005bd330aSDexuan Cui 
36105bd330aSDexuan Cui 	hv_hypercall_pg = hv_hypercall_pg_saved;
36205bd330aSDexuan Cui 	hv_hypercall_pg_saved = NULL;
36338dce419SVitaly Kuznetsov 
36438dce419SVitaly Kuznetsov 	/*
36538dce419SVitaly Kuznetsov 	 * Reenlightenment notifications are disabled by hv_cpu_die(0),
36638dce419SVitaly Kuznetsov 	 * reenable them here if hv_reenlightenment_cb was previously set.
36738dce419SVitaly Kuznetsov 	 */
36838dce419SVitaly Kuznetsov 	if (hv_reenlightenment_cb)
36938dce419SVitaly Kuznetsov 		set_hv_tscchange_cb(hv_reenlightenment_cb);
37005bd330aSDexuan Cui }
37105bd330aSDexuan Cui 
372421f090cSDexuan Cui /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
37305bd330aSDexuan Cui static struct syscore_ops hv_syscore_ops = {
37405bd330aSDexuan Cui 	.suspend	= hv_suspend,
37505bd330aSDexuan Cui 	.resume		= hv_resume,
37605bd330aSDexuan Cui };
37705bd330aSDexuan Cui 
378fff7b5e6SDexuan Cui static void (* __initdata old_setup_percpu_clockev)(void);
379fff7b5e6SDexuan Cui 
hv_stimer_setup_percpu_clockev(void)380fff7b5e6SDexuan Cui static void __init hv_stimer_setup_percpu_clockev(void)
381fff7b5e6SDexuan Cui {
382fff7b5e6SDexuan Cui 	/*
383fff7b5e6SDexuan Cui 	 * Ignore any errors in setting up stimer clockevents
384fff7b5e6SDexuan Cui 	 * as we can run with the LAPIC timer as a fallback.
385fff7b5e6SDexuan Cui 	 */
386ec866be6SMichael Kelley 	(void)hv_stimer_alloc(false);
387fff7b5e6SDexuan Cui 
388fff7b5e6SDexuan Cui 	/*
389fff7b5e6SDexuan Cui 	 * Still register the LAPIC timer, because the direct-mode STIMER is
390fff7b5e6SDexuan Cui 	 * not supported by old versions of Hyper-V. This also allows users
391fff7b5e6SDexuan Cui 	 * to switch to LAPIC timer via /sys, if they want to.
392fff7b5e6SDexuan Cui 	 */
393fff7b5e6SDexuan Cui 	if (old_setup_percpu_clockev)
394fff7b5e6SDexuan Cui 		old_setup_percpu_clockev();
395fff7b5e6SDexuan Cui }
396fff7b5e6SDexuan Cui 
hv_get_partition_id(void)39799a0f46aSWei Liu static void __init hv_get_partition_id(void)
39899a0f46aSWei Liu {
39999a0f46aSWei Liu 	struct hv_get_partition_id *output_page;
40099a0f46aSWei Liu 	u64 status;
40199a0f46aSWei Liu 	unsigned long flags;
40299a0f46aSWei Liu 
40399a0f46aSWei Liu 	local_irq_save(flags);
40499a0f46aSWei Liu 	output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
40599a0f46aSWei Liu 	status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
406753ed9c9SJoseph Salisbury 	if (!hv_result_success(status)) {
40799a0f46aSWei Liu 		/* No point in proceeding if this failed */
40899a0f46aSWei Liu 		pr_err("Failed to get partition ID: %lld\n", status);
40999a0f46aSWei Liu 		BUG();
41099a0f46aSWei Liu 	}
41199a0f46aSWei Liu 	hv_current_partition_id = output_page->partition_id;
41299a0f46aSWei Liu 	local_irq_restore(flags);
41399a0f46aSWei Liu }
41499a0f46aSWei Liu 
415f2a55d08SSaurabh Sengar #if IS_ENABLED(CONFIG_HYPERV_VTL_MODE)
get_vtl(void)4168387ce06STianyu Lan static u8 __init get_vtl(void)
4178387ce06STianyu Lan {
4188387ce06STianyu Lan 	u64 control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_REGISTERS;
4198387ce06STianyu Lan 	struct hv_get_vp_registers_input *input;
4208387ce06STianyu Lan 	struct hv_get_vp_registers_output *output;
4218387ce06STianyu Lan 	unsigned long flags;
4228387ce06STianyu Lan 	u64 ret;
4238387ce06STianyu Lan 
4248387ce06STianyu Lan 	local_irq_save(flags);
4258387ce06STianyu Lan 	input = *this_cpu_ptr(hyperv_pcpu_input_arg);
4268387ce06STianyu Lan 	output = (struct hv_get_vp_registers_output *)input;
4278387ce06STianyu Lan 
4288387ce06STianyu Lan 	memset(input, 0, struct_size(input, element, 1));
4298387ce06STianyu Lan 	input->header.partitionid = HV_PARTITION_ID_SELF;
4308387ce06STianyu Lan 	input->header.vpindex = HV_VP_INDEX_SELF;
4318387ce06STianyu Lan 	input->header.inputvtl = 0;
4328387ce06STianyu Lan 	input->element[0].name0 = HV_X64_REGISTER_VSM_VP_STATUS;
4338387ce06STianyu Lan 
4348387ce06STianyu Lan 	ret = hv_do_hypercall(control, input, output);
4358387ce06STianyu Lan 	if (hv_result_success(ret)) {
4368387ce06STianyu Lan 		ret = output->as64.low & HV_X64_VTL_MASK;
4378387ce06STianyu Lan 	} else {
438f2a55d08SSaurabh Sengar 		pr_err("Failed to get VTL(error: %lld) exiting...\n", ret);
439f2a55d08SSaurabh Sengar 		BUG();
4408387ce06STianyu Lan 	}
4418387ce06STianyu Lan 
4428387ce06STianyu Lan 	local_irq_restore(flags);
4438387ce06STianyu Lan 	return ret;
4448387ce06STianyu Lan }
445f2a55d08SSaurabh Sengar #else
get_vtl(void)446f2a55d08SSaurabh Sengar static inline u8 get_vtl(void) { return 0; }
447f2a55d08SSaurabh Sengar #endif
4488387ce06STianyu Lan 
4498730046cSK. Y. Srinivasan /*
4508730046cSK. Y. Srinivasan  * This function is to be invoked early in the boot sequence after the
4518730046cSK. Y. Srinivasan  * hypervisor has been detected.
4528730046cSK. Y. Srinivasan  *
4538730046cSK. Y. Srinivasan  * 1. Setup the hypercall page.
45463ed4e0cSK. Y. Srinivasan  * 2. Register Hyper-V specific clocksource.
4556b48cb5fSK. Y. Srinivasan  * 3. Setup Hyper-V specific APIC entry points.
4568730046cSK. Y. Srinivasan  */
hyperv_init(void)4576b48cb5fSK. Y. Srinivasan void __init hyperv_init(void)
4588730046cSK. Y. Srinivasan {
459f3e613e7SSean Christopherson 	u64 guest_id;
4608730046cSK. Y. Srinivasan 	union hv_x64_msr_hypercall_contents hypercall_msr;
461afca4d95SMichael Kelley 	int cpuhp;
4628730046cSK. Y. Srinivasan 
46303b2a320SJuergen Gross 	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
4648730046cSK. Y. Srinivasan 		return;
4658730046cSK. Y. Srinivasan 
466afca4d95SMichael Kelley 	if (hv_common_init())
4677415aea6SVitaly Kuznetsov 		return;
4687415aea6SVitaly Kuznetsov 
46968f2f2bcSDexuan Cui 	/*
47068f2f2bcSDexuan Cui 	 * The VP assist page is useless to a TDX guest: the only use we
47168f2f2bcSDexuan Cui 	 * would have for it is lazy EOI, which can not be used with TDX.
47268f2f2bcSDexuan Cui 	 */
47368f2f2bcSDexuan Cui 	if (hv_isolation_type_tdx())
47468f2f2bcSDexuan Cui 		hv_vp_assist_page = NULL;
47568f2f2bcSDexuan Cui 	else
476a46d15ccSVitaly Kuznetsov 		hv_vp_assist_page = kcalloc(num_possible_cpus(),
47768f2f2bcSDexuan Cui 					    sizeof(*hv_vp_assist_page),
47868f2f2bcSDexuan Cui 					    GFP_KERNEL);
479a46d15ccSVitaly Kuznetsov 	if (!hv_vp_assist_page) {
480a46d15ccSVitaly Kuznetsov 		ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
48168f2f2bcSDexuan Cui 
48268f2f2bcSDexuan Cui 		if (!hv_isolation_type_tdx())
483afca4d95SMichael Kelley 			goto common_free;
484a46d15ccSVitaly Kuznetsov 	}
485a46d15ccSVitaly Kuznetsov 
486e3131f1cSDexuan Cui 	if (ms_hyperv.paravisor_present && hv_isolation_type_snp()) {
48749d6a3c0STianyu Lan 		/* Negotiate GHCB Version. */
48849d6a3c0STianyu Lan 		if (!hv_ghcb_negotiate_protocol())
48949d6a3c0STianyu Lan 			hv_ghcb_terminate(SEV_TERM_SET_GEN,
49049d6a3c0STianyu Lan 					  GHCB_SEV_ES_PROT_UNSUPPORTED);
49149d6a3c0STianyu Lan 
492faff4406STianyu Lan 		hv_ghcb_pg = alloc_percpu(union hv_ghcb *);
4930cc4f6d9STianyu Lan 		if (!hv_ghcb_pg)
4940cc4f6d9STianyu Lan 			goto free_vp_assist_page;
4950cc4f6d9STianyu Lan 	}
4960cc4f6d9STianyu Lan 
4979636be85SMichael Kelley 	cpuhp = cpuhp_setup_state(CPUHP_AP_HYPERV_ONLINE, "x86/hyperv_init:online",
498a46d15ccSVitaly Kuznetsov 				  hv_cpu_init, hv_cpu_die);
499a46d15ccSVitaly Kuznetsov 	if (cpuhp < 0)
5000cc4f6d9STianyu Lan 		goto free_ghcb_page;
5017415aea6SVitaly Kuznetsov 
5028730046cSK. Y. Srinivasan 	/*
5038730046cSK. Y. Srinivasan 	 * Setup the hypercall page and enable hypercalls.
5048730046cSK. Y. Srinivasan 	 * 1. Register the guest ID
5058730046cSK. Y. Srinivasan 	 * 2. Enable the hypercall and register the hypercall page
50623378295SDexuan Cui 	 *
50723378295SDexuan Cui 	 * A TDX VM with no paravisor only uses TDX GHCI rather than hv_hypercall_pg:
50823378295SDexuan Cui 	 * when the hypercall input is a page, such a VM must pass a decrypted
50923378295SDexuan Cui 	 * page to Hyper-V, e.g. hv_post_message() uses the per-CPU page
51023378295SDexuan Cui 	 * hyperv_pcpu_input_arg, which is decrypted if no paravisor is present.
51123378295SDexuan Cui 	 *
51223378295SDexuan Cui 	 * A TDX VM with the paravisor uses hv_hypercall_pg for most hypercalls,
51323378295SDexuan Cui 	 * which are handled by the paravisor and the VM must use an encrypted
51423378295SDexuan Cui 	 * input page: in such a VM, the hyperv_pcpu_input_arg is encrypted and
51523378295SDexuan Cui 	 * used in the hypercalls, e.g. see hv_mark_gpa_visibility() and
51623378295SDexuan Cui 	 * hv_arch_irq_unmask(). Such a VM uses TDX GHCI for two hypercalls:
51723378295SDexuan Cui 	 * 1. HVCALL_SIGNAL_EVENT: see vmbus_set_event() and _hv_do_fast_hypercall8().
51823378295SDexuan Cui 	 * 2. HVCALL_POST_MESSAGE: the input page must be a decrypted page, i.e.
51923378295SDexuan Cui 	 * hv_post_message() in such a VM can't use the encrypted hyperv_pcpu_input_arg;
52023378295SDexuan Cui 	 * instead, hv_post_message() uses the post_msg_page, which is decrypted
52123378295SDexuan Cui 	 * in such a VM and is only used in such a VM.
5228730046cSK. Y. Srinivasan 	 */
523d5ebde1eSLi kunyu 	guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
5248730046cSK. Y. Srinivasan 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
5258730046cSK. Y. Srinivasan 
526b9b4fe3aSDexuan Cui 	/* With the paravisor, the VM must also write the ID via GHCB/GHCI */
527b9b4fe3aSDexuan Cui 	hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
528faff4406STianyu Lan 
52923378295SDexuan Cui 	/* A TDX VM with no paravisor only uses TDX GHCI rather than hv_hypercall_pg */
53023378295SDexuan Cui 	if (hv_isolation_type_tdx() && !ms_hyperv.paravisor_present)
531d6e0228dSDexuan Cui 		goto skip_hypercall_pg_init;
5328730046cSK. Y. Srinivasan 
533800e26b8SChristoph Hellwig 	hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
534800e26b8SChristoph Hellwig 			VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
535a3a66c38SChristoph Hellwig 			VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
536a3a66c38SChristoph Hellwig 			__builtin_return_address(0));
5370cc4f6d9STianyu Lan 	if (hv_hypercall_pg == NULL)
5380cc4f6d9STianyu Lan 		goto clean_guest_os_id;
5398730046cSK. Y. Srinivasan 
5408730046cSK. Y. Srinivasan 	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
5418730046cSK. Y. Srinivasan 	hypercall_msr.enable = 1;
54280f73c9fSWei Liu 
54380f73c9fSWei Liu 	if (hv_root_partition) {
54480f73c9fSWei Liu 		struct page *pg;
54503b9a6e1SZhao Liu 		void *src;
54680f73c9fSWei Liu 
54780f73c9fSWei Liu 		/*
54880f73c9fSWei Liu 		 * For the root partition, the hypervisor will set up its
54980f73c9fSWei Liu 		 * hypercall page. The hypervisor guarantees it will not show
55080f73c9fSWei Liu 		 * up in the root's address space. The root can't change the
55180f73c9fSWei Liu 		 * location of the hypercall page.
55280f73c9fSWei Liu 		 *
55380f73c9fSWei Liu 		 * Order is important here. We must enable the hypercall page
55480f73c9fSWei Liu 		 * so it is populated with code, then copy the code to an
55580f73c9fSWei Liu 		 * executable page.
55680f73c9fSWei Liu 		 */
55780f73c9fSWei Liu 		wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
55880f73c9fSWei Liu 
55980f73c9fSWei Liu 		pg = vmalloc_to_page(hv_hypercall_pg);
56080f73c9fSWei Liu 		src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
56180f73c9fSWei Liu 				MEMREMAP_WB);
56203b9a6e1SZhao Liu 		BUG_ON(!src);
56303b9a6e1SZhao Liu 		memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE);
56480f73c9fSWei Liu 		memunmap(src);
5650408f16bSStanislav Kinsburskiy 
5660408f16bSStanislav Kinsburskiy 		hv_remap_tsc_clocksource();
56780f73c9fSWei Liu 	} else {
568fc53662fSVitaly Kuznetsov 		hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
5698730046cSK. Y. Srinivasan 		wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
57080f73c9fSWei Liu 	}
57163ed4e0cSK. Y. Srinivasan 
572d6e0228dSDexuan Cui skip_hypercall_pg_init:
5734df4cb9eSMichael Kelley 	/*
574d5ace2a7SMichael Kelley 	 * Some versions of Hyper-V that provide IBT in guest VMs have a bug
575d5ace2a7SMichael Kelley 	 * in that there's no ENDBR64 instruction at the entry to the
576d5ace2a7SMichael Kelley 	 * hypercall page. Because hypercalls are invoked via an indirect call
577d5ace2a7SMichael Kelley 	 * to the hypercall page, all hypercall attempts fail when IBT is
578d5ace2a7SMichael Kelley 	 * enabled, and Linux panics. For such buggy versions, disable IBT.
579d5ace2a7SMichael Kelley 	 *
580d5ace2a7SMichael Kelley 	 * Fixed versions of Hyper-V always provide ENDBR64 on the hypercall
581d5ace2a7SMichael Kelley 	 * page, so if future Linux kernel versions enable IBT for 32-bit
582d5ace2a7SMichael Kelley 	 * builds, additional hypercall page hackery will be required here
583d5ace2a7SMichael Kelley 	 * to provide an ENDBR32.
584d5ace2a7SMichael Kelley 	 */
585d5ace2a7SMichael Kelley #ifdef CONFIG_X86_KERNEL_IBT
586d5ace2a7SMichael Kelley 	if (cpu_feature_enabled(X86_FEATURE_IBT) &&
587d5ace2a7SMichael Kelley 	    *(u32 *)hv_hypercall_pg != gen_endbr()) {
588d5ace2a7SMichael Kelley 		setup_clear_cpu_cap(X86_FEATURE_IBT);
589203a521bSSaurabh Sengar 		pr_warn("Disabling IBT because of Hyper-V bug\n");
590d5ace2a7SMichael Kelley 	}
591d5ace2a7SMichael Kelley #endif
592d5ace2a7SMichael Kelley 
593d5ace2a7SMichael Kelley 	/*
594fff7b5e6SDexuan Cui 	 * hyperv_init() is called before LAPIC is initialized: see
595fff7b5e6SDexuan Cui 	 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
596fff7b5e6SDexuan Cui 	 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
597fff7b5e6SDexuan Cui 	 * depends on LAPIC, so hv_stimer_alloc() should be called from
598fff7b5e6SDexuan Cui 	 * x86_init.timers.setup_percpu_clockev.
5994df4cb9eSMichael Kelley 	 */
600fff7b5e6SDexuan Cui 	old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
601fff7b5e6SDexuan Cui 	x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
6024df4cb9eSMichael Kelley 
6036b48cb5fSK. Y. Srinivasan 	hv_apic_init();
6046b48cb5fSK. Y. Srinivasan 
6052f285f46SDexuan Cui 	x86_init.pci.arch_init = hv_pci_init;
6062f285f46SDexuan Cui 
60705bd330aSDexuan Cui 	register_syscore_ops(&hv_syscore_ops);
60805bd330aSDexuan Cui 
60999a0f46aSWei Liu 	if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
61099a0f46aSWei Liu 		hv_get_partition_id();
61199a0f46aSWei Liu 
61299a0f46aSWei Liu 	BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
61399a0f46aSWei Liu 
614e39397d1SWei Liu #ifdef CONFIG_PCI_MSI
615e39397d1SWei Liu 	/*
616e39397d1SWei Liu 	 * If we're running as root, we want to create our own PCI MSI domain.
617e39397d1SWei Liu 	 * We can't set this in hv_pci_init because that would be too late.
618e39397d1SWei Liu 	 */
619e39397d1SWei Liu 	if (hv_root_partition)
620e39397d1SWei Liu 		x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
621e39397d1SWei Liu #endif
622e39397d1SWei Liu 
6236dc2a774SSunil Muthuswamy 	/* Query the VMs extended capability once, so that it can be cached. */
6246dc2a774SSunil Muthuswamy 	hv_query_ext_cap(0);
625062a5c42STianyu Lan 
6268387ce06STianyu Lan 	/* Find the VTL */
6278387ce06STianyu Lan 	ms_hyperv.vtl = get_vtl();
6288387ce06STianyu Lan 
62914058f72SSaurabh Sengar 	if (ms_hyperv.vtl > 0) /* non default VTL */
63014058f72SSaurabh Sengar 		hv_vtl_early_init();
63114058f72SSaurabh Sengar 
6327415aea6SVitaly Kuznetsov 	return;
6337415aea6SVitaly Kuznetsov 
6340cc4f6d9STianyu Lan clean_guest_os_id:
6350cc4f6d9STianyu Lan 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
636b9b4fe3aSDexuan Cui 	hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
637*2ae1beb3SAnirudh Rayabharam (Microsoft) 	cpuhp_remove_state(CPUHP_AP_HYPERV_ONLINE);
6380cc4f6d9STianyu Lan free_ghcb_page:
6390cc4f6d9STianyu Lan 	free_percpu(hv_ghcb_pg);
640a46d15ccSVitaly Kuznetsov free_vp_assist_page:
641a46d15ccSVitaly Kuznetsov 	kfree(hv_vp_assist_page);
642a46d15ccSVitaly Kuznetsov 	hv_vp_assist_page = NULL;
643afca4d95SMichael Kelley common_free:
644afca4d95SMichael Kelley 	hv_common_free();
6458730046cSK. Y. Srinivasan }
6466ab42a66SK. Y. Srinivasan 
6476ab42a66SK. Y. Srinivasan /*
648d6f3609dSVitaly Kuznetsov  * This routine is called before kexec/kdump, it does the required cleanup.
649d6f3609dSVitaly Kuznetsov  */
hyperv_cleanup(void)650d6f3609dSVitaly Kuznetsov void hyperv_cleanup(void)
651d6f3609dSVitaly Kuznetsov {
652d6f3609dSVitaly Kuznetsov 	union hv_x64_msr_hypercall_contents hypercall_msr;
6532982635aSAnirudh Rayabharam 	union hv_reference_tsc_msr tsc_msr;
654d6f3609dSVitaly Kuznetsov 
655d6f3609dSVitaly Kuznetsov 	/* Reset our OS id */
656d6f3609dSVitaly Kuznetsov 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
657b9b4fe3aSDexuan Cui 	hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
658d6f3609dSVitaly Kuznetsov 
659179fb36aSKairui Song 	/*
660179fb36aSKairui Song 	 * Reset hypercall page reference before reset the page,
661179fb36aSKairui Song 	 * let hypercall operations fail safely rather than
662179fb36aSKairui Song 	 * panic the kernel for using invalid hypercall page
663179fb36aSKairui Song 	 */
664179fb36aSKairui Song 	hv_hypercall_pg = NULL;
665179fb36aSKairui Song 
666d6f3609dSVitaly Kuznetsov 	/* Reset the hypercall page */
6672982635aSAnirudh Rayabharam 	hypercall_msr.as_uint64 = hv_get_register(HV_X64_MSR_HYPERCALL);
6682982635aSAnirudh Rayabharam 	hypercall_msr.enable = 0;
6692982635aSAnirudh Rayabharam 	hv_set_register(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
6705647dbf8SVitaly Kuznetsov 
6715647dbf8SVitaly Kuznetsov 	/* Reset the TSC page */
6722982635aSAnirudh Rayabharam 	tsc_msr.as_uint64 = hv_get_register(HV_X64_MSR_REFERENCE_TSC);
6732982635aSAnirudh Rayabharam 	tsc_msr.enable = 0;
6742982635aSAnirudh Rayabharam 	hv_set_register(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
675d6f3609dSVitaly Kuznetsov }
676d6f3609dSVitaly Kuznetsov 
hyperv_report_panic(struct pt_regs * regs,long err,bool in_die)677f3a99e76STianyu Lan void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
678d058fa7eSK. Y. Srinivasan {
679d058fa7eSK. Y. Srinivasan 	static bool panic_reported;
6807ed4325aSK. Y. Srinivasan 	u64 guest_id;
681d058fa7eSK. Y. Srinivasan 
682f3a99e76STianyu Lan 	if (in_die && !panic_on_oops)
683f3a99e76STianyu Lan 		return;
684f3a99e76STianyu Lan 
685d058fa7eSK. Y. Srinivasan 	/*
686d058fa7eSK. Y. Srinivasan 	 * We prefer to report panic on 'die' chain as we have proper
687d058fa7eSK. Y. Srinivasan 	 * registers to report, but if we miss it (e.g. on BUG()) we need
688d058fa7eSK. Y. Srinivasan 	 * to report it on 'panic'.
689d058fa7eSK. Y. Srinivasan 	 */
690d058fa7eSK. Y. Srinivasan 	if (panic_reported)
691d058fa7eSK. Y. Srinivasan 		return;
692d058fa7eSK. Y. Srinivasan 	panic_reported = true;
693d058fa7eSK. Y. Srinivasan 
6947ed4325aSK. Y. Srinivasan 	rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
6957ed4325aSK. Y. Srinivasan 
6967ed4325aSK. Y. Srinivasan 	wrmsrl(HV_X64_MSR_CRASH_P0, err);
6977ed4325aSK. Y. Srinivasan 	wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
6987ed4325aSK. Y. Srinivasan 	wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
6997ed4325aSK. Y. Srinivasan 	wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
7007ed4325aSK. Y. Srinivasan 	wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
701d058fa7eSK. Y. Srinivasan 
702d058fa7eSK. Y. Srinivasan 	/*
703d058fa7eSK. Y. Srinivasan 	 * Let Hyper-V know there is crash data available
704d058fa7eSK. Y. Srinivasan 	 */
705d058fa7eSK. Y. Srinivasan 	wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
706d058fa7eSK. Y. Srinivasan }
707d058fa7eSK. Y. Srinivasan EXPORT_SYMBOL_GPL(hyperv_report_panic);
70873638cddSK. Y. Srinivasan 
hv_is_hyperv_initialized(void)7094a5f3cdeSMichael Kelley bool hv_is_hyperv_initialized(void)
71073638cddSK. Y. Srinivasan {
71173638cddSK. Y. Srinivasan 	union hv_x64_msr_hypercall_contents hypercall_msr;
71273638cddSK. Y. Srinivasan 
7134a5f3cdeSMichael Kelley 	/*
7144a5f3cdeSMichael Kelley 	 * Ensure that we're really on Hyper-V, and not a KVM or Xen
7154a5f3cdeSMichael Kelley 	 * emulation of Hyper-V
7164a5f3cdeSMichael Kelley 	 */
7174a5f3cdeSMichael Kelley 	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
7184a5f3cdeSMichael Kelley 		return false;
7194a5f3cdeSMichael Kelley 
720d3a9d7e4SDexuan Cui 	/* A TDX VM with no paravisor uses TDX GHCI call rather than hv_hypercall_pg */
721d3a9d7e4SDexuan Cui 	if (hv_isolation_type_tdx() && !ms_hyperv.paravisor_present)
722d6e0228dSDexuan Cui 		return true;
7234a5f3cdeSMichael Kelley 	/*
7244a5f3cdeSMichael Kelley 	 * Verify that earlier initialization succeeded by checking
7254a5f3cdeSMichael Kelley 	 * that the hypercall page is setup
7264a5f3cdeSMichael Kelley 	 */
72773638cddSK. Y. Srinivasan 	hypercall_msr.as_uint64 = 0;
72873638cddSK. Y. Srinivasan 	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
72973638cddSK. Y. Srinivasan 
7304a5f3cdeSMichael Kelley 	return hypercall_msr.enable;
73173638cddSK. Y. Srinivasan }
7324a5f3cdeSMichael Kelley EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
733