gvt.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #include <linux/types.h>
  24. #include <xen/xen.h>
  25. #include "i915_drv.h"
  26. struct intel_gvt_host intel_gvt_host;
  27. static const char * const supported_hypervisors[] = {
  28. [INTEL_GVT_HYPERVISOR_XEN] = "XEN",
  29. [INTEL_GVT_HYPERVISOR_KVM] = "KVM",
  30. };
  31. /**
  32. * intel_gvt_init_host - Load MPT modules and detect if we're running in host
  33. * @gvt: intel gvt device
  34. *
  35. * This function is called at the driver loading stage. If failed to find a
  36. * loadable MPT module or detect currently we're running in a VM, then GVT-g
  37. * will be disabled
  38. *
  39. * Returns:
  40. * Zero on success, negative error code if failed.
  41. *
  42. */
  43. int intel_gvt_init_host(void)
  44. {
  45. if (intel_gvt_host.initialized)
  46. return 0;
  47. /* Xen DOM U */
  48. if (xen_domain() && !xen_initial_domain())
  49. return -ENODEV;
  50. /* Try to load MPT modules for hypervisors */
  51. if (xen_initial_domain()) {
  52. /* In Xen dom0 */
  53. intel_gvt_host.mpt = try_then_request_module(
  54. symbol_get(xengt_mpt), "xengt");
  55. intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_XEN;
  56. } else {
  57. /* not in Xen. Try KVMGT */
  58. intel_gvt_host.mpt = try_then_request_module(
  59. symbol_get(kvmgt_mpt), "kvm");
  60. intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_KVM;
  61. }
  62. /* Fail to load MPT modules - bail out */
  63. if (!intel_gvt_host.mpt)
  64. return -EINVAL;
  65. /* Try to detect if we're running in host instead of VM. */
  66. if (!intel_gvt_hypervisor_detect_host())
  67. return -ENODEV;
  68. gvt_dbg_core("Running with hypervisor %s in host mode\n",
  69. supported_hypervisors[intel_gvt_host.hypervisor_type]);
  70. intel_gvt_host.initialized = true;
  71. return 0;
  72. }
  73. static void init_device_info(struct intel_gvt *gvt)
  74. {
  75. if (IS_BROADWELL(gvt->dev_priv))
  76. gvt->device_info.max_support_vgpus = 8;
  77. /* This function will grow large in GVT device model patches. */
  78. }
  79. /**
  80. * intel_gvt_clean_device - clean a GVT device
  81. * @gvt: intel gvt device
  82. *
  83. * This function is called at the driver unloading stage, to free the
  84. * resources owned by a GVT device.
  85. *
  86. */
  87. void intel_gvt_clean_device(struct drm_i915_private *dev_priv)
  88. {
  89. struct intel_gvt *gvt = &dev_priv->gvt;
  90. if (WARN_ON(!gvt->initialized))
  91. return;
  92. /* Other de-initialization of GVT components will be introduced. */
  93. gvt->initialized = false;
  94. }
  95. /**
  96. * intel_gvt_init_device - initialize a GVT device
  97. * @dev_priv: drm i915 private data
  98. *
  99. * This function is called at the initialization stage, to initialize
  100. * necessary GVT components.
  101. *
  102. * Returns:
  103. * Zero on success, negative error code if failed.
  104. *
  105. */
  106. int intel_gvt_init_device(struct drm_i915_private *dev_priv)
  107. {
  108. struct intel_gvt *gvt = &dev_priv->gvt;
  109. /*
  110. * Cannot initialize GVT device without intel_gvt_host gets
  111. * initialized first.
  112. */
  113. if (WARN_ON(!intel_gvt_host.initialized))
  114. return -EINVAL;
  115. if (WARN_ON(gvt->initialized))
  116. return -EEXIST;
  117. gvt_dbg_core("init gvt device\n");
  118. init_device_info(gvt);
  119. /*
  120. * Other initialization of GVT components will be introduce here.
  121. */
  122. gvt_dbg_core("gvt device creation is done\n");
  123. gvt->initialized = true;
  124. return 0;
  125. }