19bbb8888SMichael Kelley // SPDX-License-Identifier: GPL-2.0
29bbb8888SMichael Kelley
39bbb8888SMichael Kelley /*
49bbb8888SMichael Kelley * Core routines for interacting with Microsoft's Hyper-V hypervisor,
59bbb8888SMichael Kelley * including hypervisor initialization.
69bbb8888SMichael Kelley *
79bbb8888SMichael Kelley * Copyright (C) 2021, Microsoft, Inc.
89bbb8888SMichael Kelley *
99bbb8888SMichael Kelley * Author : Michael Kelley <mikelley@microsoft.com>
109bbb8888SMichael Kelley */
119bbb8888SMichael Kelley
129bbb8888SMichael Kelley #include <linux/types.h>
139bbb8888SMichael Kelley #include <linux/acpi.h>
149bbb8888SMichael Kelley #include <linux/export.h>
159bbb8888SMichael Kelley #include <linux/errno.h>
169bbb8888SMichael Kelley #include <linux/version.h>
179bbb8888SMichael Kelley #include <linux/cpuhotplug.h>
189bbb8888SMichael Kelley #include <asm/mshyperv.h>
199bbb8888SMichael Kelley
209bbb8888SMichael Kelley static bool hyperv_initialized;
219bbb8888SMichael Kelley
hyperv_init(void)229bbb8888SMichael Kelley static int __init hyperv_init(void)
239bbb8888SMichael Kelley {
249bbb8888SMichael Kelley struct hv_get_vp_registers_output result;
259bbb8888SMichael Kelley u32 a, b, c, d;
269bbb8888SMichael Kelley u64 guest_id;
279bbb8888SMichael Kelley int ret;
289bbb8888SMichael Kelley
299bbb8888SMichael Kelley /*
309bbb8888SMichael Kelley * Allow for a kernel built with CONFIG_HYPERV to be running in
319bbb8888SMichael Kelley * a non-Hyper-V environment, including on DT instead of ACPI.
329bbb8888SMichael Kelley * In such cases, do nothing and return success.
339bbb8888SMichael Kelley */
349bbb8888SMichael Kelley if (acpi_disabled)
359bbb8888SMichael Kelley return 0;
369bbb8888SMichael Kelley
379bbb8888SMichael Kelley if (strncmp((char *)&acpi_gbl_FADT.hypervisor_id, "MsHyperV", 8))
389bbb8888SMichael Kelley return 0;
399bbb8888SMichael Kelley
409bbb8888SMichael Kelley /* Setup the guest ID */
41d5ebde1eSLi kunyu guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
429bbb8888SMichael Kelley hv_set_vpreg(HV_REGISTER_GUEST_OSID, guest_id);
439bbb8888SMichael Kelley
449bbb8888SMichael Kelley /* Get the features and hints from Hyper-V */
459bbb8888SMichael Kelley hv_get_vpreg_128(HV_REGISTER_FEATURES, &result);
469bbb8888SMichael Kelley ms_hyperv.features = result.as32.a;
479bbb8888SMichael Kelley ms_hyperv.priv_high = result.as32.b;
489bbb8888SMichael Kelley ms_hyperv.misc_features = result.as32.c;
499bbb8888SMichael Kelley
509bbb8888SMichael Kelley hv_get_vpreg_128(HV_REGISTER_ENLIGHTENMENTS, &result);
519bbb8888SMichael Kelley ms_hyperv.hints = result.as32.a;
529bbb8888SMichael Kelley
539bbb8888SMichael Kelley pr_info("Hyper-V: privilege flags low 0x%x, high 0x%x, hints 0x%x, misc 0x%x\n",
549bbb8888SMichael Kelley ms_hyperv.features, ms_hyperv.priv_high, ms_hyperv.hints,
559bbb8888SMichael Kelley ms_hyperv.misc_features);
569bbb8888SMichael Kelley
579bbb8888SMichael Kelley /* Get information about the Hyper-V host version */
589bbb8888SMichael Kelley hv_get_vpreg_128(HV_REGISTER_HYPERVISOR_VERSION, &result);
599bbb8888SMichael Kelley a = result.as32.a;
609bbb8888SMichael Kelley b = result.as32.b;
619bbb8888SMichael Kelley c = result.as32.c;
629bbb8888SMichael Kelley d = result.as32.d;
639bbb8888SMichael Kelley pr_info("Hyper-V: Host Build %d.%d.%d.%d-%d-%d\n",
649bbb8888SMichael Kelley b >> 16, b & 0xFFFF, a, d & 0xFFFFFF, c, d >> 24);
659bbb8888SMichael Kelley
669bbb8888SMichael Kelley ret = hv_common_init();
679bbb8888SMichael Kelley if (ret)
689bbb8888SMichael Kelley return ret;
699bbb8888SMichael Kelley
70*52ae076cSMichael Kelley ret = cpuhp_setup_state(CPUHP_AP_HYPERV_ONLINE, "arm64/hyperv_init:online",
719bbb8888SMichael Kelley hv_common_cpu_init, hv_common_cpu_die);
729bbb8888SMichael Kelley if (ret < 0) {
739bbb8888SMichael Kelley hv_common_free();
749bbb8888SMichael Kelley return ret;
759bbb8888SMichael Kelley }
769bbb8888SMichael Kelley
779bbb8888SMichael Kelley hyperv_initialized = true;
789bbb8888SMichael Kelley return 0;
799bbb8888SMichael Kelley }
809bbb8888SMichael Kelley
819bbb8888SMichael Kelley early_initcall(hyperv_init);
829bbb8888SMichael Kelley
hv_is_hyperv_initialized(void)839bbb8888SMichael Kelley bool hv_is_hyperv_initialized(void)
849bbb8888SMichael Kelley {
859bbb8888SMichael Kelley return hyperv_initialized;
869bbb8888SMichael Kelley }
879bbb8888SMichael Kelley EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
88