aperture_gm.c 9.3 KB

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