mshyperv.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Core routines for interacting with Microsoft's Hyper-V hypervisor,
  4. * including hypervisor initialization.
  5. *
  6. * Copyright (C) 2021, Microsoft, Inc.
  7. *
  8. * Author : Michael Kelley <mikelley@microsoft.com>
  9. */
  10. #include <linux/types.h>
  11. #include <linux/acpi.h>
  12. #include <linux/export.h>
  13. #include <linux/errno.h>
  14. #include <linux/version.h>
  15. #include <linux/cpuhotplug.h>
  16. #include <asm/mshyperv.h>
  17. static bool hyperv_initialized;
  18. int hv_get_hypervisor_version(union hv_hypervisor_version_info *info)
  19. {
  20. hv_get_vpreg_128(HV_REGISTER_HYPERVISOR_VERSION,
  21. (struct hv_get_vp_registers_output *)info);
  22. return 0;
  23. }
  24. static int __init hyperv_init(void)
  25. {
  26. struct hv_get_vp_registers_output result;
  27. u64 guest_id;
  28. int ret;
  29. /*
  30. * Allow for a kernel built with CONFIG_HYPERV to be running in
  31. * a non-Hyper-V environment, including on DT instead of ACPI.
  32. * In such cases, do nothing and return success.
  33. */
  34. if (acpi_disabled)
  35. return 0;
  36. if (strncmp((char *)&acpi_gbl_FADT.hypervisor_id, "MsHyperV", 8))
  37. return 0;
  38. /* Setup the guest ID */
  39. guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
  40. hv_set_vpreg(HV_REGISTER_GUEST_OS_ID, guest_id);
  41. /* Get the features and hints from Hyper-V */
  42. hv_get_vpreg_128(HV_REGISTER_FEATURES, &result);
  43. ms_hyperv.features = result.as32.a;
  44. ms_hyperv.priv_high = result.as32.b;
  45. ms_hyperv.misc_features = result.as32.c;
  46. hv_get_vpreg_128(HV_REGISTER_ENLIGHTENMENTS, &result);
  47. ms_hyperv.hints = result.as32.a;
  48. pr_info("Hyper-V: privilege flags low 0x%x, high 0x%x, hints 0x%x, misc 0x%x\n",
  49. ms_hyperv.features, ms_hyperv.priv_high, ms_hyperv.hints,
  50. ms_hyperv.misc_features);
  51. ret = hv_common_init();
  52. if (ret)
  53. return ret;
  54. ret = cpuhp_setup_state(CPUHP_AP_HYPERV_ONLINE, "arm64/hyperv_init:online",
  55. hv_common_cpu_init, hv_common_cpu_die);
  56. if (ret < 0) {
  57. hv_common_free();
  58. return ret;
  59. }
  60. ms_hyperv_late_init();
  61. hyperv_initialized = true;
  62. return 0;
  63. }
  64. early_initcall(hyperv_init);
  65. bool hv_is_hyperv_initialized(void)
  66. {
  67. return hyperv_initialized;
  68. }
  69. EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);