aperture_gm.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. * Authors:
  24. * Kevin Tian <kevin.tian@intel.com>
  25. * Dexuan Cui
  26. *
  27. * Contributors:
  28. * Pei Zhang <pei.zhang@intel.com>
  29. * Min He <min.he@intel.com>
  30. * Niu Bing <bing.niu@intel.com>
  31. * Yulei Zhang <yulei.zhang@intel.com>
  32. * Zhenyu Wang <zhenyuw@linux.intel.com>
  33. * Zhi Wang <zhi.a.wang@intel.com>
  34. *
  35. */
  36. #include "i915_drv.h"
  37. #include "gvt.h"
  38. static int alloc_gm(struct intel_vgpu *vgpu, bool high_gm)
  39. {
  40. struct intel_gvt *gvt = vgpu->gvt;
  41. struct drm_i915_private *dev_priv = gvt->dev_priv;
  42. unsigned int flags;
  43. u64 start, end, size;
  44. struct drm_mm_node *node;
  45. int ret;
  46. if (high_gm) {
  47. node = &vgpu->gm.high_gm_node;
  48. size = vgpu_hidden_sz(vgpu);
  49. start = ALIGN(gvt_hidden_gmadr_base(gvt), I915_GTT_PAGE_SIZE);
  50. end = ALIGN(gvt_hidden_gmadr_end(gvt), I915_GTT_PAGE_SIZE);
  51. flags = PIN_HIGH;
  52. } else {
  53. node = &vgpu->gm.low_gm_node;
  54. size = vgpu_aperture_sz(vgpu);
  55. start = ALIGN(gvt_aperture_gmadr_base(gvt), I915_GTT_PAGE_SIZE);
  56. end = ALIGN(gvt_aperture_gmadr_end(gvt), I915_GTT_PAGE_SIZE);
  57. flags = PIN_MAPPABLE;
  58. }
  59. mutex_lock(&dev_priv->drm.struct_mutex);
  60. ret = i915_gem_gtt_insert(&dev_priv->ggtt.base, node,
  61. size, I915_GTT_PAGE_SIZE,
  62. I915_COLOR_UNEVICTABLE,
  63. start, end, flags);
  64. mutex_unlock(&dev_priv->drm.struct_mutex);
  65. if (ret)
  66. gvt_err("fail to alloc %s gm space from host\n",
  67. high_gm ? "high" : "low");
  68. return ret;
  69. }
  70. static int alloc_vgpu_gm(struct intel_vgpu *vgpu)
  71. {
  72. struct intel_gvt *gvt = vgpu->gvt;
  73. struct drm_i915_private *dev_priv = gvt->dev_priv;
  74. int ret;
  75. ret = alloc_gm(vgpu, false);
  76. if (ret)
  77. return ret;
  78. ret = alloc_gm(vgpu, true);
  79. if (ret)
  80. goto out_free_aperture;
  81. gvt_dbg_core("vgpu%d: alloc low GM start %llx size %llx\n", vgpu->id,
  82. vgpu_aperture_offset(vgpu), vgpu_aperture_sz(vgpu));
  83. gvt_dbg_core("vgpu%d: alloc high GM start %llx size %llx\n", vgpu->id,
  84. vgpu_hidden_offset(vgpu), vgpu_hidden_sz(vgpu));
  85. return 0;
  86. out_free_aperture:
  87. mutex_lock(&dev_priv->drm.struct_mutex);
  88. drm_mm_remove_node(&vgpu->gm.low_gm_node);
  89. mutex_unlock(&dev_priv->drm.struct_mutex);
  90. return ret;
  91. }
  92. static void free_vgpu_gm(struct intel_vgpu *vgpu)
  93. {
  94. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  95. mutex_lock(&dev_priv->drm.struct_mutex);
  96. drm_mm_remove_node(&vgpu->gm.low_gm_node);
  97. drm_mm_remove_node(&vgpu->gm.high_gm_node);
  98. mutex_unlock(&dev_priv->drm.struct_mutex);
  99. }
  100. /**
  101. * intel_vgpu_write_fence - write fence registers owned by a vGPU
  102. * @vgpu: vGPU instance
  103. * @fence: vGPU fence register number
  104. * @value: Fence register value to be written
  105. *
  106. * This function is used to write fence registers owned by a vGPU. The vGPU
  107. * fence register number will be translated into HW fence register number.
  108. *
  109. */
  110. void intel_vgpu_write_fence(struct intel_vgpu *vgpu,
  111. u32 fence, u64 value)
  112. {
  113. struct intel_gvt *gvt = vgpu->gvt;
  114. struct drm_i915_private *dev_priv = gvt->dev_priv;
  115. struct drm_i915_fence_reg *reg;
  116. i915_reg_t fence_reg_lo, fence_reg_hi;
  117. assert_rpm_wakelock_held(dev_priv);
  118. if (WARN_ON(fence > vgpu_fence_sz(vgpu)))
  119. return;
  120. reg = vgpu->fence.regs[fence];
  121. if (WARN_ON(!reg))
  122. return;
  123. fence_reg_lo = FENCE_REG_GEN6_LO(reg->id);
  124. fence_reg_hi = FENCE_REG_GEN6_HI(reg->id);
  125. I915_WRITE(fence_reg_lo, 0);
  126. POSTING_READ(fence_reg_lo);
  127. I915_WRITE(fence_reg_hi, upper_32_bits(value));
  128. I915_WRITE(fence_reg_lo, lower_32_bits(value));
  129. POSTING_READ(fence_reg_lo);
  130. }
  131. static void _clear_vgpu_fence(struct intel_vgpu *vgpu)
  132. {
  133. int i;
  134. for (i = 0; i < vgpu_fence_sz(vgpu); i++)
  135. intel_vgpu_write_fence(vgpu, i, 0);
  136. }
  137. static void free_vgpu_fence(struct intel_vgpu *vgpu)
  138. {
  139. struct intel_gvt *gvt = vgpu->gvt;
  140. struct drm_i915_private *dev_priv = gvt->dev_priv;
  141. struct drm_i915_fence_reg *reg;
  142. u32 i;
  143. if (WARN_ON(!vgpu_fence_sz(vgpu)))
  144. return;
  145. intel_runtime_pm_get(dev_priv);
  146. mutex_lock(&dev_priv->drm.struct_mutex);
  147. _clear_vgpu_fence(vgpu);
  148. for (i = 0; i < vgpu_fence_sz(vgpu); i++) {
  149. reg = vgpu->fence.regs[i];
  150. i915_unreserve_fence(reg);
  151. vgpu->fence.regs[i] = NULL;
  152. }
  153. mutex_unlock(&dev_priv->drm.struct_mutex);
  154. intel_runtime_pm_put(dev_priv);
  155. }
  156. static int alloc_vgpu_fence(struct intel_vgpu *vgpu)
  157. {
  158. struct intel_gvt *gvt = vgpu->gvt;
  159. struct drm_i915_private *dev_priv = gvt->dev_priv;
  160. struct drm_i915_fence_reg *reg;
  161. int i;
  162. intel_runtime_pm_get(dev_priv);
  163. /* Request fences from host */
  164. mutex_lock(&dev_priv->drm.struct_mutex);
  165. for (i = 0; i < vgpu_fence_sz(vgpu); i++) {
  166. reg = i915_reserve_fence(dev_priv);
  167. if (IS_ERR(reg))
  168. goto out_free_fence;
  169. vgpu->fence.regs[i] = reg;
  170. }
  171. _clear_vgpu_fence(vgpu);
  172. mutex_unlock(&dev_priv->drm.struct_mutex);
  173. intel_runtime_pm_put(dev_priv);
  174. return 0;
  175. out_free_fence:
  176. gvt_vgpu_err("Failed to alloc fences\n");
  177. /* Return fences to host, if fail */
  178. for (i = 0; i < vgpu_fence_sz(vgpu); i++) {
  179. reg = vgpu->fence.regs[i];
  180. if (!reg)
  181. continue;
  182. i915_unreserve_fence(reg);
  183. vgpu->fence.regs[i] = NULL;
  184. }
  185. mutex_unlock(&dev_priv->drm.struct_mutex);
  186. intel_runtime_pm_put(dev_priv);
  187. return -ENOSPC;
  188. }
  189. static void free_resource(struct intel_vgpu *vgpu)
  190. {
  191. struct intel_gvt *gvt = vgpu->gvt;
  192. gvt->gm.vgpu_allocated_low_gm_size -= vgpu_aperture_sz(vgpu);
  193. gvt->gm.vgpu_allocated_high_gm_size -= vgpu_hidden_sz(vgpu);
  194. gvt->fence.vgpu_allocated_fence_num -= vgpu_fence_sz(vgpu);
  195. }
  196. static int alloc_resource(struct intel_vgpu *vgpu,
  197. struct intel_vgpu_creation_params *param)
  198. {
  199. struct intel_gvt *gvt = vgpu->gvt;
  200. unsigned long request, avail, max, taken;
  201. const char *item;
  202. if (!param->low_gm_sz || !param->high_gm_sz || !param->fence_sz) {
  203. gvt_vgpu_err("Invalid vGPU creation params\n");
  204. return -EINVAL;
  205. }
  206. item = "low GM space";
  207. max = gvt_aperture_sz(gvt) - HOST_LOW_GM_SIZE;
  208. taken = gvt->gm.vgpu_allocated_low_gm_size;
  209. avail = max - taken;
  210. request = MB_TO_BYTES(param->low_gm_sz);
  211. if (request > avail)
  212. goto no_enough_resource;
  213. vgpu_aperture_sz(vgpu) = ALIGN(request, I915_GTT_PAGE_SIZE);
  214. item = "high GM space";
  215. max = gvt_hidden_sz(gvt) - HOST_HIGH_GM_SIZE;
  216. taken = gvt->gm.vgpu_allocated_high_gm_size;
  217. avail = max - taken;
  218. request = MB_TO_BYTES(param->high_gm_sz);
  219. if (request > avail)
  220. goto no_enough_resource;
  221. vgpu_hidden_sz(vgpu) = ALIGN(request, I915_GTT_PAGE_SIZE);
  222. item = "fence";
  223. max = gvt_fence_sz(gvt) - HOST_FENCE;
  224. taken = gvt->fence.vgpu_allocated_fence_num;
  225. avail = max - taken;
  226. request = param->fence_sz;
  227. if (request > avail)
  228. goto no_enough_resource;
  229. vgpu_fence_sz(vgpu) = request;
  230. gvt->gm.vgpu_allocated_low_gm_size += MB_TO_BYTES(param->low_gm_sz);
  231. gvt->gm.vgpu_allocated_high_gm_size += MB_TO_BYTES(param->high_gm_sz);
  232. gvt->fence.vgpu_allocated_fence_num += param->fence_sz;
  233. return 0;
  234. no_enough_resource:
  235. gvt_err("fail to allocate resource %s\n", item);
  236. gvt_err("request %luMB avail %luMB max %luMB taken %luMB\n",
  237. BYTES_TO_MB(request), BYTES_TO_MB(avail),
  238. BYTES_TO_MB(max), BYTES_TO_MB(taken));
  239. return -ENOSPC;
  240. }
  241. /**
  242. * inte_gvt_free_vgpu_resource - free HW resource owned by a vGPU
  243. * @vgpu: a vGPU
  244. *
  245. * This function is used to free the HW resource owned by a vGPU.
  246. *
  247. */
  248. void intel_vgpu_free_resource(struct intel_vgpu *vgpu)
  249. {
  250. free_vgpu_gm(vgpu);
  251. free_vgpu_fence(vgpu);
  252. free_resource(vgpu);
  253. }
  254. /**
  255. * intel_vgpu_reset_resource - reset resource state owned by a vGPU
  256. * @vgpu: a vGPU
  257. *
  258. * This function is used to reset resource state owned by a vGPU.
  259. *
  260. */
  261. void intel_vgpu_reset_resource(struct intel_vgpu *vgpu)
  262. {
  263. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  264. intel_runtime_pm_get(dev_priv);
  265. _clear_vgpu_fence(vgpu);
  266. intel_runtime_pm_put(dev_priv);
  267. }
  268. /**
  269. * intel_alloc_vgpu_resource - allocate HW resource for a vGPU
  270. * @vgpu: vGPU
  271. * @param: vGPU creation params
  272. *
  273. * This function is used to allocate HW resource for a vGPU. User specifies
  274. * the resource configuration through the creation params.
  275. *
  276. * Returns:
  277. * zero on success, negative error code if failed.
  278. *
  279. */
  280. int intel_vgpu_alloc_resource(struct intel_vgpu *vgpu,
  281. struct intel_vgpu_creation_params *param)
  282. {
  283. int ret;
  284. ret = alloc_resource(vgpu, param);
  285. if (ret)
  286. return ret;
  287. ret = alloc_vgpu_gm(vgpu);
  288. if (ret)
  289. goto out_free_resource;
  290. ret = alloc_vgpu_fence(vgpu);
  291. if (ret)
  292. goto out_free_vgpu_gm;
  293. return 0;
  294. out_free_vgpu_gm:
  295. free_vgpu_gm(vgpu);
  296. out_free_resource:
  297. free_resource(vgpu);
  298. return ret;
  299. }