mshyperv.c 2.2 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 <[email protected]>
  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. static int __init hyperv_init(void)
  19. {
  20. struct hv_get_vp_registers_output result;
  21. u32 a, b, c, d;
  22. u64 guest_id;
  23. int ret;
  24. /*
  25. * Allow for a kernel built with CONFIG_HYPERV to be running in
  26. * a non-Hyper-V environment, including on DT instead of ACPI.
  27. * In such cases, do nothing and return success.
  28. */
  29. if (acpi_disabled)
  30. return 0;
  31. if (strncmp((char *)&acpi_gbl_FADT.hypervisor_id, "MsHyperV", 8))
  32. return 0;
  33. /* Setup the guest ID */
  34. guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
  35. hv_set_vpreg(HV_REGISTER_GUEST_OSID, guest_id);
  36. /* Get the features and hints from Hyper-V */
  37. hv_get_vpreg_128(HV_REGISTER_FEATURES, &result);
  38. ms_hyperv.features = result.as32.a;
  39. ms_hyperv.priv_high = result.as32.b;
  40. ms_hyperv.misc_features = result.as32.c;
  41. hv_get_vpreg_128(HV_REGISTER_ENLIGHTENMENTS, &result);
  42. ms_hyperv.hints = result.as32.a;
  43. pr_info("Hyper-V: privilege flags low 0x%x, high 0x%x, hints 0x%x, misc 0x%x\n",
  44. ms_hyperv.features, ms_hyperv.priv_high, ms_hyperv.hints,
  45. ms_hyperv.misc_features);
  46. /* Get information about the Hyper-V host version */
  47. hv_get_vpreg_128(HV_REGISTER_HYPERVISOR_VERSION, &result);
  48. a = result.as32.a;
  49. b = result.as32.b;
  50. c = result.as32.c;
  51. d = result.as32.d;
  52. pr_info("Hyper-V: Host Build %d.%d.%d.%d-%d-%d\n",
  53. b >> 16, b & 0xFFFF, a, d & 0xFFFFFF, c, d >> 24);
  54. ret = hv_common_init();
  55. if (ret)
  56. return ret;
  57. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "arm64/hyperv_init:online",
  58. hv_common_cpu_init, hv_common_cpu_die);
  59. if (ret < 0) {
  60. hv_common_free();
  61. return ret;
  62. }
  63. hyperv_initialized = true;
  64. return 0;
  65. }
  66. early_initcall(hyperv_init);
  67. bool hv_is_hyperv_initialized(void)
  68. {
  69. return hyperv_initialized;
  70. }
  71. EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);