i915_vgpu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 <drm/drm_print.h>
  24. #include "i915_drv.h"
  25. #include "i915_pvinfo.h"
  26. #include "i915_vgpu.h"
  27. /**
  28. * DOC: Intel GVT-g guest support
  29. *
  30. * Intel GVT-g is a graphics virtualization technology which shares the
  31. * GPU among multiple virtual machines on a time-sharing basis. Each
  32. * virtual machine is presented a virtual GPU (vGPU), which has equivalent
  33. * features as the underlying physical GPU (pGPU), so i915 driver can run
  34. * seamlessly in a virtual machine. This file provides vGPU specific
  35. * optimizations when running in a virtual machine, to reduce the complexity
  36. * of vGPU emulation and to improve the overall performance.
  37. *
  38. * A primary function introduced here is so-called "address space ballooning"
  39. * technique. Intel GVT-g partitions global graphics memory among multiple VMs,
  40. * so each VM can directly access a portion of the memory without hypervisor's
  41. * intervention, e.g. filling textures or queuing commands. However with the
  42. * partitioning an unmodified i915 driver would assume a smaller graphics
  43. * memory starting from address ZERO, then requires vGPU emulation module to
  44. * translate the graphics address between 'guest view' and 'host view', for
  45. * all registers and command opcodes which contain a graphics memory address.
  46. * To reduce the complexity, Intel GVT-g introduces "address space ballooning",
  47. * by telling the exact partitioning knowledge to each guest i915 driver, which
  48. * then reserves and prevents non-allocated portions from allocation. Thus vGPU
  49. * emulation module only needs to scan and validate graphics addresses without
  50. * complexity of address translation.
  51. *
  52. */
  53. /**
  54. * intel_vgpu_detect - detect virtual GPU
  55. * @dev_priv: i915 device private
  56. *
  57. * This function is called at the initialization stage, to detect whether
  58. * running on a vGPU.
  59. */
  60. void intel_vgpu_detect(struct drm_i915_private *dev_priv)
  61. {
  62. struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
  63. u64 magic;
  64. u16 version_major;
  65. void __iomem *shared_area;
  66. BUILD_BUG_ON(sizeof(struct vgt_if) != VGT_PVINFO_SIZE);
  67. /*
  68. * This is called before we setup the main MMIO BAR mappings used via
  69. * the uncore structure, so we need to access the BAR directly. Since
  70. * we do not support VGT on older gens, return early so we don't have
  71. * to consider differently numbered or sized MMIO bars
  72. */
  73. if (GRAPHICS_VER(dev_priv) < 6)
  74. return;
  75. shared_area = pci_iomap_range(pdev, 0, VGT_PVINFO_PAGE, VGT_PVINFO_SIZE);
  76. if (!shared_area) {
  77. drm_err(&dev_priv->drm,
  78. "failed to map MMIO bar to check for VGT\n");
  79. return;
  80. }
  81. magic = readq(shared_area + vgtif_offset(magic));
  82. if (magic != VGT_MAGIC)
  83. goto out;
  84. version_major = readw(shared_area + vgtif_offset(version_major));
  85. if (version_major < VGT_VERSION_MAJOR) {
  86. drm_info(&dev_priv->drm, "VGT interface version mismatch!\n");
  87. goto out;
  88. }
  89. dev_priv->vgpu.caps = readl(shared_area + vgtif_offset(vgt_caps));
  90. dev_priv->vgpu.active = true;
  91. mutex_init(&dev_priv->vgpu.lock);
  92. drm_info(&dev_priv->drm, "Virtual GPU for Intel GVT-g detected.\n");
  93. out:
  94. pci_iounmap(pdev, shared_area);
  95. }
  96. void intel_vgpu_register(struct drm_i915_private *i915)
  97. {
  98. /*
  99. * Notify a valid surface after modesetting, when running inside a VM.
  100. */
  101. if (intel_vgpu_active(i915))
  102. intel_uncore_write(&i915->uncore, vgtif_reg(display_ready),
  103. VGT_DRV_DISPLAY_READY);
  104. }
  105. bool intel_vgpu_active(struct drm_i915_private *dev_priv)
  106. {
  107. return dev_priv->vgpu.active;
  108. }
  109. bool intel_vgpu_has_full_ppgtt(struct drm_i915_private *dev_priv)
  110. {
  111. return dev_priv->vgpu.caps & VGT_CAPS_FULL_PPGTT;
  112. }
  113. bool intel_vgpu_has_hwsp_emulation(struct drm_i915_private *dev_priv)
  114. {
  115. return dev_priv->vgpu.caps & VGT_CAPS_HWSP_EMULATION;
  116. }
  117. bool intel_vgpu_has_huge_gtt(struct drm_i915_private *dev_priv)
  118. {
  119. return dev_priv->vgpu.caps & VGT_CAPS_HUGE_GTT;
  120. }
  121. struct _balloon_info_ {
  122. /*
  123. * There are up to 2 regions per mappable/unmappable graphic
  124. * memory that might be ballooned. Here, index 0/1 is for mappable
  125. * graphic memory, 2/3 for unmappable graphic memory.
  126. */
  127. struct drm_mm_node space[4];
  128. };
  129. static struct _balloon_info_ bl_info;
  130. static void vgt_deballoon_space(struct i915_ggtt *ggtt,
  131. struct drm_mm_node *node)
  132. {
  133. struct drm_i915_private *dev_priv = ggtt->vm.i915;
  134. if (!drm_mm_node_allocated(node))
  135. return;
  136. drm_dbg(&dev_priv->drm,
  137. "deballoon space: range [0x%llx - 0x%llx] %llu KiB.\n",
  138. node->start,
  139. node->start + node->size,
  140. node->size / 1024);
  141. ggtt->vm.reserved -= node->size;
  142. drm_mm_remove_node(node);
  143. }
  144. /**
  145. * intel_vgt_deballoon - deballoon reserved graphics address trunks
  146. * @ggtt: the global GGTT from which we reserved earlier
  147. *
  148. * This function is called to deallocate the ballooned-out graphic memory, when
  149. * driver is unloaded or when ballooning fails.
  150. */
  151. void intel_vgt_deballoon(struct i915_ggtt *ggtt)
  152. {
  153. struct drm_i915_private *dev_priv = ggtt->vm.i915;
  154. int i;
  155. if (!intel_vgpu_active(ggtt->vm.i915))
  156. return;
  157. drm_dbg(&dev_priv->drm, "VGT deballoon.\n");
  158. for (i = 0; i < 4; i++)
  159. vgt_deballoon_space(ggtt, &bl_info.space[i]);
  160. }
  161. static int vgt_balloon_space(struct i915_ggtt *ggtt,
  162. struct drm_mm_node *node,
  163. unsigned long start, unsigned long end)
  164. {
  165. struct drm_i915_private *dev_priv = ggtt->vm.i915;
  166. unsigned long size = end - start;
  167. int ret;
  168. if (start >= end)
  169. return -EINVAL;
  170. drm_info(&dev_priv->drm,
  171. "balloon space: range [ 0x%lx - 0x%lx ] %lu KiB.\n",
  172. start, end, size / 1024);
  173. ret = i915_gem_gtt_reserve(&ggtt->vm, NULL, node,
  174. size, start, I915_COLOR_UNEVICTABLE,
  175. 0);
  176. if (!ret)
  177. ggtt->vm.reserved += size;
  178. return ret;
  179. }
  180. /**
  181. * intel_vgt_balloon - balloon out reserved graphics address trunks
  182. * @ggtt: the global GGTT from which to reserve
  183. *
  184. * This function is called at the initialization stage, to balloon out the
  185. * graphic address space allocated to other vGPUs, by marking these spaces as
  186. * reserved. The ballooning related knowledge(starting address and size of
  187. * the mappable/unmappable graphic memory) is described in the vgt_if structure
  188. * in a reserved mmio range.
  189. *
  190. * To give an example, the drawing below depicts one typical scenario after
  191. * ballooning. Here the vGPU1 has 2 pieces of graphic address spaces ballooned
  192. * out each for the mappable and the non-mappable part. From the vGPU1 point of
  193. * view, the total size is the same as the physical one, with the start address
  194. * of its graphic space being zero. Yet there are some portions ballooned out(
  195. * the shadow part, which are marked as reserved by drm allocator). From the
  196. * host point of view, the graphic address space is partitioned by multiple
  197. * vGPUs in different VMs. ::
  198. *
  199. * vGPU1 view Host view
  200. * 0 ------> +-----------+ +-----------+
  201. * ^ |###########| | vGPU3 |
  202. * | |###########| +-----------+
  203. * | |###########| | vGPU2 |
  204. * | +-----------+ +-----------+
  205. * mappable GM | available | ==> | vGPU1 |
  206. * | +-----------+ +-----------+
  207. * | |###########| | |
  208. * v |###########| | Host |
  209. * +=======+===========+ +===========+
  210. * ^ |###########| | vGPU3 |
  211. * | |###########| +-----------+
  212. * | |###########| | vGPU2 |
  213. * | +-----------+ +-----------+
  214. * unmappable GM | available | ==> | vGPU1 |
  215. * | +-----------+ +-----------+
  216. * | |###########| | |
  217. * | |###########| | Host |
  218. * v |###########| | |
  219. * total GM size ------> +-----------+ +-----------+
  220. *
  221. * Returns:
  222. * zero on success, non-zero if configuration invalid or ballooning failed
  223. */
  224. int intel_vgt_balloon(struct i915_ggtt *ggtt)
  225. {
  226. struct drm_i915_private *dev_priv = ggtt->vm.i915;
  227. struct intel_uncore *uncore = &dev_priv->uncore;
  228. unsigned long ggtt_end = ggtt->vm.total;
  229. unsigned long mappable_base, mappable_size, mappable_end;
  230. unsigned long unmappable_base, unmappable_size, unmappable_end;
  231. int ret;
  232. if (!intel_vgpu_active(ggtt->vm.i915))
  233. return 0;
  234. mappable_base =
  235. intel_uncore_read(uncore, vgtif_reg(avail_rs.mappable_gmadr.base));
  236. mappable_size =
  237. intel_uncore_read(uncore, vgtif_reg(avail_rs.mappable_gmadr.size));
  238. unmappable_base =
  239. intel_uncore_read(uncore, vgtif_reg(avail_rs.nonmappable_gmadr.base));
  240. unmappable_size =
  241. intel_uncore_read(uncore, vgtif_reg(avail_rs.nonmappable_gmadr.size));
  242. mappable_end = mappable_base + mappable_size;
  243. unmappable_end = unmappable_base + unmappable_size;
  244. drm_info(&dev_priv->drm, "VGT ballooning configuration:\n");
  245. drm_info(&dev_priv->drm,
  246. "Mappable graphic memory: base 0x%lx size %ldKiB\n",
  247. mappable_base, mappable_size / 1024);
  248. drm_info(&dev_priv->drm,
  249. "Unmappable graphic memory: base 0x%lx size %ldKiB\n",
  250. unmappable_base, unmappable_size / 1024);
  251. if (mappable_end > ggtt->mappable_end ||
  252. unmappable_base < ggtt->mappable_end ||
  253. unmappable_end > ggtt_end) {
  254. drm_err(&dev_priv->drm, "Invalid ballooning configuration!\n");
  255. return -EINVAL;
  256. }
  257. /* Unmappable graphic memory ballooning */
  258. if (unmappable_base > ggtt->mappable_end) {
  259. ret = vgt_balloon_space(ggtt, &bl_info.space[2],
  260. ggtt->mappable_end, unmappable_base);
  261. if (ret)
  262. goto err;
  263. }
  264. if (unmappable_end < ggtt_end) {
  265. ret = vgt_balloon_space(ggtt, &bl_info.space[3],
  266. unmappable_end, ggtt_end);
  267. if (ret)
  268. goto err_upon_mappable;
  269. }
  270. /* Mappable graphic memory ballooning */
  271. if (mappable_base) {
  272. ret = vgt_balloon_space(ggtt, &bl_info.space[0],
  273. 0, mappable_base);
  274. if (ret)
  275. goto err_upon_unmappable;
  276. }
  277. if (mappable_end < ggtt->mappable_end) {
  278. ret = vgt_balloon_space(ggtt, &bl_info.space[1],
  279. mappable_end, ggtt->mappable_end);
  280. if (ret)
  281. goto err_below_mappable;
  282. }
  283. drm_info(&dev_priv->drm, "VGT balloon successfully\n");
  284. return 0;
  285. err_below_mappable:
  286. vgt_deballoon_space(ggtt, &bl_info.space[0]);
  287. err_upon_unmappable:
  288. vgt_deballoon_space(ggtt, &bl_info.space[3]);
  289. err_upon_mappable:
  290. vgt_deballoon_space(ggtt, &bl_info.space[2]);
  291. err:
  292. drm_err(&dev_priv->drm, "VGT balloon fail\n");
  293. return ret;
  294. }