i915_vgpu.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright(c) 2011-2015 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 "intel_drv.h"
  24. #include "i915_vgpu.h"
  25. /**
  26. * DOC: Intel GVT-g guest support
  27. *
  28. * Intel GVT-g is a graphics virtualization technology which shares the
  29. * GPU among multiple virtual machines on a time-sharing basis. Each
  30. * virtual machine is presented a virtual GPU (vGPU), which has equivalent
  31. * features as the underlying physical GPU (pGPU), so i915 driver can run
  32. * seamlessly in a virtual machine. This file provides vGPU specific
  33. * optimizations when running in a virtual machine, to reduce the complexity
  34. * of vGPU emulation and to improve the overall performance.
  35. *
  36. * A primary function introduced here is so-called "address space ballooning"
  37. * technique. Intel GVT-g partitions global graphics memory among multiple VMs,
  38. * so each VM can directly access a portion of the memory without hypervisor's
  39. * intervention, e.g. filling textures or queuing commands. However with the
  40. * partitioning an unmodified i915 driver would assume a smaller graphics
  41. * memory starting from address ZERO, then requires vGPU emulation module to
  42. * translate the graphics address between 'guest view' and 'host view', for
  43. * all registers and command opcodes which contain a graphics memory address.
  44. * To reduce the complexity, Intel GVT-g introduces "address space ballooning",
  45. * by telling the exact partitioning knowledge to each guest i915 driver, which
  46. * then reserves and prevents non-allocated portions from allocation. Thus vGPU
  47. * emulation module only needs to scan and validate graphics addresses without
  48. * complexity of address translation.
  49. *
  50. */
  51. /**
  52. * i915_check_vgpu - detect virtual GPU
  53. * @dev_priv: i915 device private
  54. *
  55. * This function is called at the initialization stage, to detect whether
  56. * running on a vGPU.
  57. */
  58. void i915_check_vgpu(struct drm_i915_private *dev_priv)
  59. {
  60. u64 magic;
  61. u16 version_major;
  62. BUILD_BUG_ON(sizeof(struct vgt_if) != VGT_PVINFO_SIZE);
  63. magic = __raw_i915_read64(dev_priv, vgtif_reg(magic));
  64. if (magic != VGT_MAGIC)
  65. return;
  66. version_major = __raw_i915_read16(dev_priv, vgtif_reg(version_major));
  67. if (version_major < VGT_VERSION_MAJOR) {
  68. DRM_INFO("VGT interface version mismatch!\n");
  69. return;
  70. }
  71. dev_priv->vgpu.caps = __raw_i915_read32(dev_priv, vgtif_reg(vgt_caps));
  72. dev_priv->vgpu.active = true;
  73. DRM_INFO("Virtual GPU for Intel GVT-g detected.\n");
  74. }
  75. bool intel_vgpu_has_full_48bit_ppgtt(struct drm_i915_private *dev_priv)
  76. {
  77. return dev_priv->vgpu.caps & VGT_CAPS_FULL_48BIT_PPGTT;
  78. }
  79. struct _balloon_info_ {
  80. /*
  81. * There are up to 2 regions per mappable/unmappable graphic
  82. * memory that might be ballooned. Here, index 0/1 is for mappable
  83. * graphic memory, 2/3 for unmappable graphic memory.
  84. */
  85. struct drm_mm_node space[4];
  86. };
  87. static struct _balloon_info_ bl_info;
  88. static void vgt_deballoon_space(struct i915_ggtt *ggtt,
  89. struct drm_mm_node *node)
  90. {
  91. DRM_DEBUG_DRIVER("deballoon space: range [0x%llx - 0x%llx] %llu KiB.\n",
  92. node->start,
  93. node->start + node->size,
  94. node->size / 1024);
  95. ggtt->base.reserved -= node->size;
  96. drm_mm_remove_node(node);
  97. }
  98. /**
  99. * intel_vgt_deballoon - deballoon reserved graphics address trunks
  100. * @dev_priv: i915 device private data
  101. *
  102. * This function is called to deallocate the ballooned-out graphic memory, when
  103. * driver is unloaded or when ballooning fails.
  104. */
  105. void intel_vgt_deballoon(struct drm_i915_private *dev_priv)
  106. {
  107. int i;
  108. if (!intel_vgpu_active(dev_priv))
  109. return;
  110. DRM_DEBUG("VGT deballoon.\n");
  111. for (i = 0; i < 4; i++)
  112. vgt_deballoon_space(&dev_priv->ggtt, &bl_info.space[i]);
  113. }
  114. static int vgt_balloon_space(struct i915_ggtt *ggtt,
  115. struct drm_mm_node *node,
  116. unsigned long start, unsigned long end)
  117. {
  118. unsigned long size = end - start;
  119. int ret;
  120. if (start >= end)
  121. return -EINVAL;
  122. DRM_INFO("balloon space: range [ 0x%lx - 0x%lx ] %lu KiB.\n",
  123. start, end, size / 1024);
  124. ret = i915_gem_gtt_reserve(&ggtt->base, node,
  125. size, start, I915_COLOR_UNEVICTABLE,
  126. 0);
  127. if (!ret)
  128. ggtt->base.reserved += size;
  129. return ret;
  130. }
  131. /**
  132. * intel_vgt_balloon - balloon out reserved graphics address trunks
  133. * @dev_priv: i915 device private data
  134. *
  135. * This function is called at the initialization stage, to balloon out the
  136. * graphic address space allocated to other vGPUs, by marking these spaces as
  137. * reserved. The ballooning related knowledge(starting address and size of
  138. * the mappable/unmappable graphic memory) is described in the vgt_if structure
  139. * in a reserved mmio range.
  140. *
  141. * To give an example, the drawing below depicts one typical scenario after
  142. * ballooning. Here the vGPU1 has 2 pieces of graphic address spaces ballooned
  143. * out each for the mappable and the non-mappable part. From the vGPU1 point of
  144. * view, the total size is the same as the physical one, with the start address
  145. * of its graphic space being zero. Yet there are some portions ballooned out(
  146. * the shadow part, which are marked as reserved by drm allocator). From the
  147. * host point of view, the graphic address space is partitioned by multiple
  148. * vGPUs in different VMs. ::
  149. *
  150. * vGPU1 view Host view
  151. * 0 ------> +-----------+ +-----------+
  152. * ^ |###########| | vGPU3 |
  153. * | |###########| +-----------+
  154. * | |###########| | vGPU2 |
  155. * | +-----------+ +-----------+
  156. * mappable GM | available | ==> | vGPU1 |
  157. * | +-----------+ +-----------+
  158. * | |###########| | |
  159. * v |###########| | Host |
  160. * +=======+===========+ +===========+
  161. * ^ |###########| | vGPU3 |
  162. * | |###########| +-----------+
  163. * | |###########| | vGPU2 |
  164. * | +-----------+ +-----------+
  165. * unmappable GM | available | ==> | vGPU1 |
  166. * | +-----------+ +-----------+
  167. * | |###########| | |
  168. * | |###########| | Host |
  169. * v |###########| | |
  170. * total GM size ------> +-----------+ +-----------+
  171. *
  172. * Returns:
  173. * zero on success, non-zero if configuration invalid or ballooning failed
  174. */
  175. int intel_vgt_balloon(struct drm_i915_private *dev_priv)
  176. {
  177. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  178. unsigned long ggtt_end = ggtt->base.total;
  179. unsigned long mappable_base, mappable_size, mappable_end;
  180. unsigned long unmappable_base, unmappable_size, unmappable_end;
  181. int ret;
  182. if (!intel_vgpu_active(dev_priv))
  183. return 0;
  184. mappable_base = I915_READ(vgtif_reg(avail_rs.mappable_gmadr.base));
  185. mappable_size = I915_READ(vgtif_reg(avail_rs.mappable_gmadr.size));
  186. unmappable_base = I915_READ(vgtif_reg(avail_rs.nonmappable_gmadr.base));
  187. unmappable_size = I915_READ(vgtif_reg(avail_rs.nonmappable_gmadr.size));
  188. mappable_end = mappable_base + mappable_size;
  189. unmappable_end = unmappable_base + unmappable_size;
  190. DRM_INFO("VGT ballooning configuration:\n");
  191. DRM_INFO("Mappable graphic memory: base 0x%lx size %ldKiB\n",
  192. mappable_base, mappable_size / 1024);
  193. DRM_INFO("Unmappable graphic memory: base 0x%lx size %ldKiB\n",
  194. unmappable_base, unmappable_size / 1024);
  195. if (mappable_end > ggtt->mappable_end ||
  196. unmappable_base < ggtt->mappable_end ||
  197. unmappable_end > ggtt_end) {
  198. DRM_ERROR("Invalid ballooning configuration!\n");
  199. return -EINVAL;
  200. }
  201. /* Unmappable graphic memory ballooning */
  202. if (unmappable_base > ggtt->mappable_end) {
  203. ret = vgt_balloon_space(ggtt, &bl_info.space[2],
  204. ggtt->mappable_end, unmappable_base);
  205. if (ret)
  206. goto err;
  207. }
  208. if (unmappable_end < ggtt_end) {
  209. ret = vgt_balloon_space(ggtt, &bl_info.space[3],
  210. unmappable_end, ggtt_end);
  211. if (ret)
  212. goto err_upon_mappable;
  213. }
  214. /* Mappable graphic memory ballooning */
  215. if (mappable_base) {
  216. ret = vgt_balloon_space(ggtt, &bl_info.space[0],
  217. 0, mappable_base);
  218. if (ret)
  219. goto err_upon_unmappable;
  220. }
  221. if (mappable_end < ggtt->mappable_end) {
  222. ret = vgt_balloon_space(ggtt, &bl_info.space[1],
  223. mappable_end, ggtt->mappable_end);
  224. if (ret)
  225. goto err_below_mappable;
  226. }
  227. DRM_INFO("VGT balloon successfully\n");
  228. return 0;
  229. err_below_mappable:
  230. vgt_deballoon_space(ggtt, &bl_info.space[0]);
  231. err_upon_unmappable:
  232. vgt_deballoon_space(ggtt, &bl_info.space[3]);
  233. err_upon_mappable:
  234. vgt_deballoon_space(ggtt, &bl_info.space[2]);
  235. err:
  236. DRM_ERROR("VGT balloon fail\n");
  237. return ret;
  238. }