aperture_gm.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. list_add_tail(&reg->link,
  151. &dev_priv->mm.fence_list);
  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. struct list_head *pos, *q;
  163. intel_runtime_pm_get(dev_priv);
  164. /* Request fences from host */
  165. mutex_lock(&dev_priv->drm.struct_mutex);
  166. i = 0;
  167. list_for_each_safe(pos, q, &dev_priv->mm.fence_list) {
  168. reg = list_entry(pos, struct drm_i915_fence_reg, link);
  169. if (reg->pin_count || reg->vma)
  170. continue;
  171. list_del(pos);
  172. vgpu->fence.regs[i] = reg;
  173. if (++i == vgpu_fence_sz(vgpu))
  174. break;
  175. }
  176. if (i != vgpu_fence_sz(vgpu))
  177. goto out_free_fence;
  178. _clear_vgpu_fence(vgpu);
  179. mutex_unlock(&dev_priv->drm.struct_mutex);
  180. intel_runtime_pm_put(dev_priv);
  181. return 0;
  182. out_free_fence:
  183. /* Return fences to host, if fail */
  184. for (i = 0; i < vgpu_fence_sz(vgpu); i++) {
  185. reg = vgpu->fence.regs[i];
  186. if (!reg)
  187. continue;
  188. list_add_tail(&reg->link,
  189. &dev_priv->mm.fence_list);
  190. }
  191. mutex_unlock(&dev_priv->drm.struct_mutex);
  192. intel_runtime_pm_put(dev_priv);
  193. return -ENOSPC;
  194. }
  195. static void free_resource(struct intel_vgpu *vgpu)
  196. {
  197. struct intel_gvt *gvt = vgpu->gvt;
  198. gvt->gm.vgpu_allocated_low_gm_size -= vgpu_aperture_sz(vgpu);
  199. gvt->gm.vgpu_allocated_high_gm_size -= vgpu_hidden_sz(vgpu);
  200. gvt->fence.vgpu_allocated_fence_num -= vgpu_fence_sz(vgpu);
  201. }
  202. static int alloc_resource(struct intel_vgpu *vgpu,
  203. struct intel_vgpu_creation_params *param)
  204. {
  205. struct intel_gvt *gvt = vgpu->gvt;
  206. unsigned long request, avail, max, taken;
  207. const char *item;
  208. if (!param->low_gm_sz || !param->high_gm_sz || !param->fence_sz) {
  209. gvt_vgpu_err("Invalid vGPU creation params\n");
  210. return -EINVAL;
  211. }
  212. item = "low GM space";
  213. max = gvt_aperture_sz(gvt) - HOST_LOW_GM_SIZE;
  214. taken = gvt->gm.vgpu_allocated_low_gm_size;
  215. avail = max - taken;
  216. request = MB_TO_BYTES(param->low_gm_sz);
  217. if (request > avail)
  218. goto no_enough_resource;
  219. vgpu_aperture_sz(vgpu) = ALIGN(request, I915_GTT_PAGE_SIZE);
  220. item = "high GM space";
  221. max = gvt_hidden_sz(gvt) - HOST_HIGH_GM_SIZE;
  222. taken = gvt->gm.vgpu_allocated_high_gm_size;
  223. avail = max - taken;
  224. request = MB_TO_BYTES(param->high_gm_sz);
  225. if (request > avail)
  226. goto no_enough_resource;
  227. vgpu_hidden_sz(vgpu) = ALIGN(request, I915_GTT_PAGE_SIZE);
  228. item = "fence";
  229. max = gvt_fence_sz(gvt) - HOST_FENCE;
  230. taken = gvt->fence.vgpu_allocated_fence_num;
  231. avail = max - taken;
  232. request = param->fence_sz;
  233. if (request > avail)
  234. goto no_enough_resource;
  235. vgpu_fence_sz(vgpu) = request;
  236. gvt->gm.vgpu_allocated_low_gm_size += MB_TO_BYTES(param->low_gm_sz);
  237. gvt->gm.vgpu_allocated_high_gm_size += MB_TO_BYTES(param->high_gm_sz);
  238. gvt->fence.vgpu_allocated_fence_num += param->fence_sz;
  239. return 0;
  240. no_enough_resource:
  241. gvt_vgpu_err("fail to allocate resource %s\n", item);
  242. gvt_vgpu_err("request %luMB avail %luMB max %luMB taken %luMB\n",
  243. BYTES_TO_MB(request), BYTES_TO_MB(avail),
  244. BYTES_TO_MB(max), BYTES_TO_MB(taken));
  245. return -ENOSPC;
  246. }
  247. /**
  248. * inte_gvt_free_vgpu_resource - free HW resource owned by a vGPU
  249. * @vgpu: a vGPU
  250. *
  251. * This function is used to free the HW resource owned by a vGPU.
  252. *
  253. */
  254. void intel_vgpu_free_resource(struct intel_vgpu *vgpu)
  255. {
  256. free_vgpu_gm(vgpu);
  257. free_vgpu_fence(vgpu);
  258. free_resource(vgpu);
  259. }
  260. /**
  261. * intel_vgpu_reset_resource - reset resource state owned by a vGPU
  262. * @vgpu: a vGPU
  263. *
  264. * This function is used to reset resource state owned by a vGPU.
  265. *
  266. */
  267. void intel_vgpu_reset_resource(struct intel_vgpu *vgpu)
  268. {
  269. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  270. intel_runtime_pm_get(dev_priv);
  271. _clear_vgpu_fence(vgpu);
  272. intel_runtime_pm_put(dev_priv);
  273. }
  274. /**
  275. * intel_alloc_vgpu_resource - allocate HW resource for a vGPU
  276. * @vgpu: vGPU
  277. * @param: vGPU creation params
  278. *
  279. * This function is used to allocate HW resource for a vGPU. User specifies
  280. * the resource configuration through the creation params.
  281. *
  282. * Returns:
  283. * zero on success, negative error code if failed.
  284. *
  285. */
  286. int intel_vgpu_alloc_resource(struct intel_vgpu *vgpu,
  287. struct intel_vgpu_creation_params *param)
  288. {
  289. int ret;
  290. ret = alloc_resource(vgpu, param);
  291. if (ret)
  292. return ret;
  293. ret = alloc_vgpu_gm(vgpu);
  294. if (ret)
  295. goto out_free_resource;
  296. ret = alloc_vgpu_fence(vgpu);
  297. if (ret)
  298. goto out_free_vgpu_gm;
  299. return 0;
  300. out_free_vgpu_gm:
  301. free_vgpu_gm(vgpu);
  302. out_free_resource:
  303. free_resource(vgpu);
  304. return ret;
  305. }