aperture_gm.c 9.6 KB

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