aperture_gm.c 9.3 KB

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