blob: 5cc0c0f30e75b3d44c27d13a98c2207b7e31d76d [file] [log] [blame]
Thomas Gleixner43aa3132019-05-29 07:17:54 -07001// SPDX-License-Identifier: GPL-2.0-only
K. Y. Srinivasan87300462017-01-18 16:45:02 -07002/*
3 * X86 specific Hyper-V initialization code.
4 *
5 * Copyright (C) 2016, Microsoft, Inc.
6 *
7 * Author : K. Y. Srinivasan <kys@microsoft.com>
K. Y. Srinivasan87300462017-01-18 16:45:02 -07008 */
9
Dexuan Cuib96f8652019-11-19 23:16:04 -080010#include <linux/acpi.h>
Dexuan Cui2f285f42018-09-18 22:29:50 +000011#include <linux/efi.h>
K. Y. Srinivasan87300462017-01-18 16:45:02 -070012#include <linux/types.h>
Andrea Parri (Microsoft)a6c76bb02021-02-01 15:48:11 +010013#include <linux/bitfield.h>
Vitaly Kuznetsov93286262018-01-24 14:23:33 +010014#include <asm/apic.h>
15#include <asm/desc.h>
K. Y. Srinivasan87300462017-01-18 16:45:02 -070016#include <asm/hypervisor.h>
Vitaly Kuznetsov5a485802018-03-20 15:02:05 +010017#include <asm/hyperv-tlfs.h>
K. Y. Srinivasan87300462017-01-18 16:45:02 -070018#include <asm/mshyperv.h>
Thomas Gleixnera16be362020-05-21 22:05:43 +020019#include <asm/idtentry.h>
Dexuan Cuidfe94d42020-12-21 22:55:41 -080020#include <linux/kexec.h>
K. Y. Srinivasan87300462017-01-18 16:45:02 -070021#include <linux/version.h>
22#include <linux/vmalloc.h>
23#include <linux/mm.h>
Stephen Hemminger67071812017-03-04 18:27:11 -070024#include <linux/hyperv.h>
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +020025#include <linux/slab.h>
Tianyu Lanf3a99e72020-04-06 08:53:31 -070026#include <linux/kernel.h>
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +020027#include <linux/cpuhotplug.h>
Dexuan Cui05bd3302020-01-06 14:42:39 -080028#include <linux/syscore_ops.h>
Michael Kelleydd2cb342019-07-01 04:26:06 +000029#include <clocksource/hyperv_timer.h>
Wei Liu80f73c92021-02-03 15:04:26 +000030#include <linux/highmem.h>
K. Y. Srinivasan87300462017-01-18 16:45:02 -070031
Dexuan Cuidfe94d42020-12-21 22:55:41 -080032int hyperv_init_cpuhp;
Wei Liu99a0f462021-02-03 15:04:25 +000033u64 hv_current_partition_id = ~0ull;
34EXPORT_SYMBOL_GPL(hv_current_partition_id);
Dexuan Cuidfe94d42020-12-21 22:55:41 -080035
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020036void *hv_hypercall_pg;
37EXPORT_SYMBOL_GPL(hv_hypercall_pg);
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -070038
Dexuan Cui05bd3302020-01-06 14:42:39 -080039/* Storage to save the hypercall page temporarily for hibernation */
40static void *hv_hypercall_pg_saved;
41
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +010042struct hv_vp_assist_page **hv_vp_assist_page;
43EXPORT_SYMBOL_GPL(hv_vp_assist_page);
44
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +020045static int hv_cpu_init(unsigned int cpu)
46{
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +010047 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
Michael Kelleyafca4d92021-07-14 11:34:45 -070048 int ret;
K. Y. Srinivasan68bb7bf2018-05-16 14:53:31 -070049
Michael Kelleyafca4d92021-07-14 11:34:45 -070050 ret = hv_common_cpu_init(cpu);
51 if (ret)
52 return ret;
Vitaly Kuznetsova3b74242017-10-06 17:48:54 +020053
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +010054 if (!hv_vp_assist_page)
55 return 0;
56
Dexuan Cuie320ab32019-07-19 03:22:35 +000057 /*
58 * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section
59 * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure
60 * we always write the EOI MSR in hv_apic_eoi_write() *after* the
61 * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may
62 * not be stopped in the case of CPU offlining and the VM will hang.
63 */
64 if (!*hvp) {
Christoph Hellwig88dca4c2020-06-01 21:51:40 -070065 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
Dexuan Cuie320ab32019-07-19 03:22:35 +000066 }
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +010067
68 if (*hvp) {
69 u64 val;
70
71 val = vmalloc_to_pfn(*hvp);
72 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
73 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
74
75 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
76 }
77
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +020078 return 0;
79}
80
Vitaly Kuznetsov93286262018-01-24 14:23:33 +010081static void (*hv_reenlightenment_cb)(void);
82
83static void hv_reenlightenment_notify(struct work_struct *dummy)
84{
85 struct hv_tsc_emulation_status emu_status;
86
87 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
88
89 /* Don't issue the callback if TSC accesses are not emulated */
90 if (hv_reenlightenment_cb && emu_status.inprogress)
91 hv_reenlightenment_cb();
92}
93static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
94
95void hyperv_stop_tsc_emulation(void)
96{
97 u64 freq;
98 struct hv_tsc_emulation_status emu_status;
99
100 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
101 emu_status.inprogress = 0;
102 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
103
104 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
105 tsc_khz = div64_u64(freq, 1000);
106}
107EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
108
109static inline bool hv_reenlightenment_available(void)
110{
111 /*
Ingo Molnard9f6e122021-03-18 15:28:01 +0100112 * Check for required features and privileges to make TSC frequency
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100113 * change notifications work.
114 */
Joseph Salisburydfc53ba2020-09-26 07:26:26 -0700115 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100116 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
Joseph Salisburydfc53ba2020-09-26 07:26:26 -0700117 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100118}
119
Thomas Gleixnera16be362020-05-21 22:05:43 +0200120DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100121{
Thomas Gleixnera16be362020-05-21 22:05:43 +0200122 ack_APIC_irq();
Vitaly Kuznetsov51d4e5d2018-01-24 14:23:35 +0100123 inc_irq_stat(irq_hv_reenlightenment_count);
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100124 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100125}
126
127void set_hv_tscchange_cb(void (*cb)(void))
128{
129 struct hv_reenlightenment_control re_ctrl = {
130 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
131 .enabled = 1,
132 .target_vp = hv_vp_index[smp_processor_id()]
133 };
134 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
135
136 if (!hv_reenlightenment_available()) {
137 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
138 return;
139 }
140
141 hv_reenlightenment_cb = cb;
142
143 /* Make sure callback is registered before we write to MSRs */
144 wmb();
145
146 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
147 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
148}
149EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
150
151void clear_hv_tscchange_cb(void)
152{
153 struct hv_reenlightenment_control re_ctrl;
154
155 if (!hv_reenlightenment_available())
156 return;
157
158 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
159 re_ctrl.enabled = 0;
160 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
161
162 hv_reenlightenment_cb = NULL;
163}
164EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
165
Vitaly Kuznetsove7c4e362018-01-24 14:23:34 +0100166static int hv_cpu_die(unsigned int cpu)
167{
168 struct hv_reenlightenment_control re_ctrl;
169 unsigned int new_cpu;
K. Y. Srinivasan68bb7bf2018-05-16 14:53:31 -0700170
Michael Kelleyafca4d92021-07-14 11:34:45 -0700171 hv_common_cpu_die(cpu);
Vitaly Kuznetsove7c4e362018-01-24 14:23:34 +0100172
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100173 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
174 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
175
Vitaly Kuznetsove7c4e362018-01-24 14:23:34 +0100176 if (hv_reenlightenment_cb == NULL)
177 return 0;
178
179 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
180 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
Vitaly Kuznetsov38dce412020-05-12 18:01:53 +0200181 /*
182 * Reassign reenlightenment notifications to some other online
183 * CPU or just disable the feature if there are no online CPUs
184 * left (happens on hibernation).
185 */
Vitaly Kuznetsove7c4e362018-01-24 14:23:34 +0100186 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
187
Vitaly Kuznetsov38dce412020-05-12 18:01:53 +0200188 if (new_cpu < nr_cpu_ids)
189 re_ctrl.target_vp = hv_vp_index[new_cpu];
190 else
191 re_ctrl.enabled = 0;
192
Vitaly Kuznetsove7c4e362018-01-24 14:23:34 +0100193 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
194 }
195
196 return 0;
197}
198
Dexuan Cui2f285f42018-09-18 22:29:50 +0000199static int __init hv_pci_init(void)
200{
201 int gen2vm = efi_enabled(EFI_BOOT);
202
203 /*
204 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
205 * The purpose is to suppress the harmless warning:
206 * "PCI: Fatal: No config space access function found"
207 */
208 if (gen2vm)
209 return 0;
210
211 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
212 return 1;
213}
214
Dexuan Cui05bd3302020-01-06 14:42:39 -0800215static int hv_suspend(void)
216{
217 union hv_x64_msr_hypercall_contents hypercall_msr;
Dexuan Cui421f0902020-04-20 19:46:11 -0700218 int ret;
Dexuan Cui05bd3302020-01-06 14:42:39 -0800219
Wei Liu80f73c92021-02-03 15:04:26 +0000220 if (hv_root_partition)
221 return -EPERM;
222
Dexuan Cui05bd3302020-01-06 14:42:39 -0800223 /*
224 * Reset the hypercall page as it is going to be invalidated
Ingo Molnard9f6e122021-03-18 15:28:01 +0100225 * across hibernation. Setting hv_hypercall_pg to NULL ensures
Dexuan Cui05bd3302020-01-06 14:42:39 -0800226 * that any subsequent hypercall operation fails safely instead of
227 * crashing due to an access of an invalid page. The hypercall page
228 * pointer is restored on resume.
229 */
230 hv_hypercall_pg_saved = hv_hypercall_pg;
231 hv_hypercall_pg = NULL;
232
233 /* Disable the hypercall page in the hypervisor */
234 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
235 hypercall_msr.enable = 0;
236 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
237
Dexuan Cui421f0902020-04-20 19:46:11 -0700238 ret = hv_cpu_die(0);
239 return ret;
Dexuan Cui05bd3302020-01-06 14:42:39 -0800240}
241
242static void hv_resume(void)
243{
244 union hv_x64_msr_hypercall_contents hypercall_msr;
Dexuan Cui421f0902020-04-20 19:46:11 -0700245 int ret;
246
247 ret = hv_cpu_init(0);
248 WARN_ON(ret);
Dexuan Cui05bd3302020-01-06 14:42:39 -0800249
250 /* Re-enable the hypercall page */
251 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
252 hypercall_msr.enable = 1;
253 hypercall_msr.guest_physical_address =
254 vmalloc_to_pfn(hv_hypercall_pg_saved);
255 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
256
257 hv_hypercall_pg = hv_hypercall_pg_saved;
258 hv_hypercall_pg_saved = NULL;
Vitaly Kuznetsov38dce412020-05-12 18:01:53 +0200259
260 /*
261 * Reenlightenment notifications are disabled by hv_cpu_die(0),
262 * reenable them here if hv_reenlightenment_cb was previously set.
263 */
264 if (hv_reenlightenment_cb)
265 set_hv_tscchange_cb(hv_reenlightenment_cb);
Dexuan Cui05bd3302020-01-06 14:42:39 -0800266}
267
Dexuan Cui421f0902020-04-20 19:46:11 -0700268/* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
Dexuan Cui05bd3302020-01-06 14:42:39 -0800269static struct syscore_ops hv_syscore_ops = {
270 .suspend = hv_suspend,
271 .resume = hv_resume,
272};
273
Dexuan Cuifff7b5e2021-01-16 14:31:36 -0800274static void (* __initdata old_setup_percpu_clockev)(void);
275
276static void __init hv_stimer_setup_percpu_clockev(void)
277{
278 /*
279 * Ignore any errors in setting up stimer clockevents
280 * as we can run with the LAPIC timer as a fallback.
281 */
Michael Kelleyec866be62021-03-02 13:38:22 -0800282 (void)hv_stimer_alloc(false);
Dexuan Cuifff7b5e2021-01-16 14:31:36 -0800283
284 /*
285 * Still register the LAPIC timer, because the direct-mode STIMER is
286 * not supported by old versions of Hyper-V. This also allows users
287 * to switch to LAPIC timer via /sys, if they want to.
288 */
289 if (old_setup_percpu_clockev)
290 old_setup_percpu_clockev();
291}
292
Wei Liu99a0f462021-02-03 15:04:25 +0000293static void __init hv_get_partition_id(void)
294{
295 struct hv_get_partition_id *output_page;
296 u64 status;
297 unsigned long flags;
298
299 local_irq_save(flags);
300 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
301 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
Joseph Salisbury753ed9c2021-04-16 17:43:03 -0700302 if (!hv_result_success(status)) {
Wei Liu99a0f462021-02-03 15:04:25 +0000303 /* No point in proceeding if this failed */
304 pr_err("Failed to get partition ID: %lld\n", status);
305 BUG();
306 }
307 hv_current_partition_id = output_page->partition_id;
308 local_irq_restore(flags);
309}
310
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700311/*
312 * This function is to be invoked early in the boot sequence after the
313 * hypervisor has been detected.
314 *
315 * 1. Setup the hypercall page.
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700316 * 2. Register Hyper-V specific clocksource.
K. Y. Srinivasan6b48cb52018-05-16 14:53:30 -0700317 * 3. Setup Hyper-V specific APIC entry points.
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700318 */
K. Y. Srinivasan6b48cb52018-05-16 14:53:30 -0700319void __init hyperv_init(void)
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700320{
Vitaly Kuznetsov89a8f6d2018-01-24 14:23:31 +0100321 u64 guest_id, required_msrs;
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700322 union hv_x64_msr_hypercall_contents hypercall_msr;
Michael Kelleyafca4d92021-07-14 11:34:45 -0700323 int cpuhp;
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700324
Juergen Gross03b2a322017-11-09 14:27:36 +0100325 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700326 return;
327
Vitaly Kuznetsov89a8f6d2018-01-24 14:23:31 +0100328 /* Absolutely required MSRs */
Joseph Salisburydfc53ba2020-09-26 07:26:26 -0700329 required_msrs = HV_MSR_HYPERCALL_AVAILABLE |
330 HV_MSR_VP_INDEX_AVAILABLE;
Vitaly Kuznetsov89a8f6d2018-01-24 14:23:31 +0100331
332 if ((ms_hyperv.features & required_msrs) != required_msrs)
333 return;
334
Michael Kelleyafca4d92021-07-14 11:34:45 -0700335 if (hv_common_init())
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200336 return;
337
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100338 hv_vp_assist_page = kcalloc(num_possible_cpus(),
339 sizeof(*hv_vp_assist_page), GFP_KERNEL);
340 if (!hv_vp_assist_page) {
341 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
Michael Kelleyafca4d92021-07-14 11:34:45 -0700342 goto common_free;
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100343 }
344
345 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
346 hv_cpu_init, hv_cpu_die);
347 if (cpuhp < 0)
348 goto free_vp_assist_page;
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200349
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700350 /*
351 * Setup the hypercall page and enable hypercalls.
352 * 1. Register the guest ID
353 * 2. Enable the hypercall and register the hypercall page
354 */
355 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
356 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
357
Christoph Hellwig800e26b2020-06-25 20:30:40 -0700358 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
359 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
Christoph Hellwiga3a66c32020-07-03 15:15:27 -0700360 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
361 __builtin_return_address(0));
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200362 if (hv_hypercall_pg == NULL) {
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700363 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100364 goto remove_cpuhp_state;
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700365 }
366
367 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
368 hypercall_msr.enable = 1;
Wei Liu80f73c92021-02-03 15:04:26 +0000369
370 if (hv_root_partition) {
371 struct page *pg;
372 void *src, *dst;
373
374 /*
375 * For the root partition, the hypervisor will set up its
376 * hypercall page. The hypervisor guarantees it will not show
377 * up in the root's address space. The root can't change the
378 * location of the hypercall page.
379 *
380 * Order is important here. We must enable the hypercall page
381 * so it is populated with code, then copy the code to an
382 * executable page.
383 */
384 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
385
386 pg = vmalloc_to_page(hv_hypercall_pg);
387 dst = kmap(pg);
388 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
389 MEMREMAP_WB);
390 BUG_ON(!(src && dst));
391 memcpy(dst, src, HV_HYP_PAGE_SIZE);
392 memunmap(src);
393 kunmap(pg);
394 } else {
395 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
396 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
397 }
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700398
Michael Kelley4df4cb9e92019-11-13 01:11:49 +0000399 /*
Dexuan Cuifff7b5e2021-01-16 14:31:36 -0800400 * hyperv_init() is called before LAPIC is initialized: see
401 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
402 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
403 * depends on LAPIC, so hv_stimer_alloc() should be called from
404 * x86_init.timers.setup_percpu_clockev.
Michael Kelley4df4cb9e92019-11-13 01:11:49 +0000405 */
Dexuan Cuifff7b5e2021-01-16 14:31:36 -0800406 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
407 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
Michael Kelley4df4cb9e92019-11-13 01:11:49 +0000408
K. Y. Srinivasan6b48cb52018-05-16 14:53:30 -0700409 hv_apic_init();
410
Dexuan Cui2f285f42018-09-18 22:29:50 +0000411 x86_init.pci.arch_init = hv_pci_init;
412
Dexuan Cui05bd3302020-01-06 14:42:39 -0800413 register_syscore_ops(&hv_syscore_ops);
414
Dexuan Cuidfe94d42020-12-21 22:55:41 -0800415 hyperv_init_cpuhp = cpuhp;
Wei Liu99a0f462021-02-03 15:04:25 +0000416
417 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
418 hv_get_partition_id();
419
420 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
421
Wei Liue39397d2021-02-03 15:04:34 +0000422#ifdef CONFIG_PCI_MSI
423 /*
424 * If we're running as root, we want to create our own PCI MSI domain.
425 * We can't set this in hv_pci_init because that would be too late.
426 */
427 if (hv_root_partition)
428 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
429#endif
430
Sunil Muthuswamy6dc2a772021-03-23 18:47:16 +0000431 /* Query the VMs extended capability once, so that it can be cached. */
432 hv_query_ext_cap(0);
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200433 return;
434
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100435remove_cpuhp_state:
436 cpuhp_remove_state(cpuhp);
437free_vp_assist_page:
438 kfree(hv_vp_assist_page);
439 hv_vp_assist_page = NULL;
Michael Kelleyafca4d92021-07-14 11:34:45 -0700440common_free:
441 hv_common_free();
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700442}
K. Y. Srinivasan6ab42a62017-01-18 16:45:03 -0700443
444/*
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700445 * This routine is called before kexec/kdump, it does the required cleanup.
446 */
447void hyperv_cleanup(void)
448{
449 union hv_x64_msr_hypercall_contents hypercall_msr;
450
Dexuan Cui05bd3302020-01-06 14:42:39 -0800451 unregister_syscore_ops(&hv_syscore_ops);
452
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700453 /* Reset our OS id */
454 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
455
Kairui Song179fb362019-03-06 19:18:27 +0800456 /*
457 * Reset hypercall page reference before reset the page,
458 * let hypercall operations fail safely rather than
459 * panic the kernel for using invalid hypercall page
460 */
461 hv_hypercall_pg = NULL;
462
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700463 /* Reset the hypercall page */
464 hypercall_msr.as_uint64 = 0;
465 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Vitaly Kuznetsov5647dbf2017-01-28 12:37:15 -0700466
467 /* Reset the TSC page */
468 hypercall_msr.as_uint64 = 0;
469 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700470}
471EXPORT_SYMBOL_GPL(hyperv_cleanup);
472
Tianyu Lanf3a99e72020-04-06 08:53:31 -0700473void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700474{
475 static bool panic_reported;
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -0700476 u64 guest_id;
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700477
Tianyu Lanf3a99e72020-04-06 08:53:31 -0700478 if (in_die && !panic_on_oops)
479 return;
480
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700481 /*
482 * We prefer to report panic on 'die' chain as we have proper
483 * registers to report, but if we miss it (e.g. on BUG()) we need
484 * to report it on 'panic'.
485 */
486 if (panic_reported)
487 return;
488 panic_reported = true;
489
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -0700490 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
491
492 wrmsrl(HV_X64_MSR_CRASH_P0, err);
493 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
494 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
495 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
496 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700497
498 /*
499 * Let Hyper-V know there is crash data available
500 */
501 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
502}
503EXPORT_SYMBOL_GPL(hyperv_report_panic);
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700504
Michael Kelley4a5f3cd2017-12-22 11:19:02 -0700505bool hv_is_hyperv_initialized(void)
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700506{
507 union hv_x64_msr_hypercall_contents hypercall_msr;
508
Michael Kelley4a5f3cd2017-12-22 11:19:02 -0700509 /*
510 * Ensure that we're really on Hyper-V, and not a KVM or Xen
511 * emulation of Hyper-V
512 */
513 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
514 return false;
515
516 /*
517 * Verify that earlier initialization succeeded by checking
518 * that the hypercall page is setup
519 */
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700520 hypercall_msr.as_uint64 = 0;
521 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
522
Michael Kelley4a5f3cd2017-12-22 11:19:02 -0700523 return hypercall_msr.enable;
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700524}
Michael Kelley4a5f3cd2017-12-22 11:19:02 -0700525EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
Dexuan Cuib96f8652019-11-19 23:16:04 -0800526
527bool hv_is_hibernation_supported(void)
528{
Wei Liu80f73c92021-02-03 15:04:26 +0000529 return !hv_root_partition && acpi_sleep_state_supported(ACPI_STATE_S4);
Dexuan Cuib96f8652019-11-19 23:16:04 -0800530}
531EXPORT_SYMBOL_GPL(hv_is_hibernation_supported);
Andrea Parri (Microsoft)a6c76bb02021-02-01 15:48:11 +0100532
533enum hv_isolation_type hv_get_isolation_type(void)
534{
Sunil Muthuswamy6dc2a772021-03-23 18:47:16 +0000535 if (!(ms_hyperv.priv_high & HV_ISOLATION))
Andrea Parri (Microsoft)a6c76bb02021-02-01 15:48:11 +0100536 return HV_ISOLATION_TYPE_NONE;
537 return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b);
538}
539EXPORT_SYMBOL_GPL(hv_get_isolation_type);
540
541bool hv_is_isolation_supported(void)
542{
543 return hv_get_isolation_type() != HV_ISOLATION_TYPE_NONE;
544}
545EXPORT_SYMBOL_GPL(hv_is_isolation_supported);