Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 /*
0004  * Core routines for interacting with Microsoft's Hyper-V hypervisor,
0005  * including hypervisor initialization.
0006  *
0007  * Copyright (C) 2021, Microsoft, Inc.
0008  *
0009  * Author : Michael Kelley <mikelley@microsoft.com>
0010  */
0011 
0012 #include <linux/types.h>
0013 #include <linux/acpi.h>
0014 #include <linux/export.h>
0015 #include <linux/errno.h>
0016 #include <linux/version.h>
0017 #include <linux/cpuhotplug.h>
0018 #include <asm/mshyperv.h>
0019 
0020 static bool hyperv_initialized;
0021 
0022 static int __init hyperv_init(void)
0023 {
0024     struct hv_get_vp_registers_output   result;
0025     u32 a, b, c, d;
0026     u64 guest_id;
0027     int ret;
0028 
0029     /*
0030      * Allow for a kernel built with CONFIG_HYPERV to be running in
0031      * a non-Hyper-V environment, including on DT instead of ACPI.
0032      * In such cases, do nothing and return success.
0033      */
0034     if (acpi_disabled)
0035         return 0;
0036 
0037     if (strncmp((char *)&acpi_gbl_FADT.hypervisor_id, "MsHyperV", 8))
0038         return 0;
0039 
0040     /* Setup the guest ID */
0041     guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
0042     hv_set_vpreg(HV_REGISTER_GUEST_OSID, guest_id);
0043 
0044     /* Get the features and hints from Hyper-V */
0045     hv_get_vpreg_128(HV_REGISTER_FEATURES, &result);
0046     ms_hyperv.features = result.as32.a;
0047     ms_hyperv.priv_high = result.as32.b;
0048     ms_hyperv.misc_features = result.as32.c;
0049 
0050     hv_get_vpreg_128(HV_REGISTER_ENLIGHTENMENTS, &result);
0051     ms_hyperv.hints = result.as32.a;
0052 
0053     pr_info("Hyper-V: privilege flags low 0x%x, high 0x%x, hints 0x%x, misc 0x%x\n",
0054         ms_hyperv.features, ms_hyperv.priv_high, ms_hyperv.hints,
0055         ms_hyperv.misc_features);
0056 
0057     /* Get information about the Hyper-V host version */
0058     hv_get_vpreg_128(HV_REGISTER_HYPERVISOR_VERSION, &result);
0059     a = result.as32.a;
0060     b = result.as32.b;
0061     c = result.as32.c;
0062     d = result.as32.d;
0063     pr_info("Hyper-V: Host Build %d.%d.%d.%d-%d-%d\n",
0064         b >> 16, b & 0xFFFF, a, d & 0xFFFFFF, c, d >> 24);
0065 
0066     ret = hv_common_init();
0067     if (ret)
0068         return ret;
0069 
0070     ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "arm64/hyperv_init:online",
0071                 hv_common_cpu_init, hv_common_cpu_die);
0072     if (ret < 0) {
0073         hv_common_free();
0074         return ret;
0075     }
0076 
0077     hyperv_initialized = true;
0078     return 0;
0079 }
0080 
0081 early_initcall(hyperv_init);
0082 
0083 bool hv_is_hyperv_initialized(void)
0084 {
0085     return hyperv_initialized;
0086 }
0087 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);