intel_ring.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright © 2019 Intel Corporation
  4. */
  5. #include "gem/i915_gem_internal.h"
  6. #include "gem/i915_gem_lmem.h"
  7. #include "gem/i915_gem_object.h"
  8. #include "i915_drv.h"
  9. #include "i915_vma.h"
  10. #include "intel_engine.h"
  11. #include "intel_engine_regs.h"
  12. #include "intel_gpu_commands.h"
  13. #include "intel_ring.h"
  14. #include "intel_gt.h"
  15. #include "intel_timeline.h"
  16. unsigned int intel_ring_update_space(struct intel_ring *ring)
  17. {
  18. unsigned int space;
  19. space = __intel_ring_space(ring->head, ring->emit, ring->size);
  20. ring->space = space;
  21. return space;
  22. }
  23. void __intel_ring_pin(struct intel_ring *ring)
  24. {
  25. GEM_BUG_ON(!atomic_read(&ring->pin_count));
  26. atomic_inc(&ring->pin_count);
  27. }
  28. int intel_ring_pin(struct intel_ring *ring, struct i915_gem_ww_ctx *ww)
  29. {
  30. struct i915_vma *vma = ring->vma;
  31. unsigned int flags;
  32. void *addr;
  33. int ret;
  34. if (atomic_fetch_inc(&ring->pin_count))
  35. return 0;
  36. /* Ring wraparound at offset 0 sometimes hangs. No idea why. */
  37. flags = PIN_OFFSET_BIAS | i915_ggtt_pin_bias(vma);
  38. if (i915_gem_object_is_stolen(vma->obj))
  39. flags |= PIN_MAPPABLE;
  40. else
  41. flags |= PIN_HIGH;
  42. ret = i915_ggtt_pin(vma, ww, 0, flags);
  43. if (unlikely(ret))
  44. goto err_unpin;
  45. if (i915_vma_is_map_and_fenceable(vma) && !HAS_LLC(vma->vm->i915)) {
  46. addr = (void __force *)i915_vma_pin_iomap(vma);
  47. } else {
  48. int type = intel_gt_coherent_map_type(vma->vm->gt, vma->obj, false);
  49. addr = i915_gem_object_pin_map(vma->obj, type);
  50. }
  51. if (IS_ERR(addr)) {
  52. ret = PTR_ERR(addr);
  53. goto err_ring;
  54. }
  55. i915_vma_make_unshrinkable(vma);
  56. /* Discard any unused bytes beyond that submitted to hw. */
  57. intel_ring_reset(ring, ring->emit);
  58. ring->vaddr = addr;
  59. return 0;
  60. err_ring:
  61. i915_vma_unpin(vma);
  62. err_unpin:
  63. atomic_dec(&ring->pin_count);
  64. return ret;
  65. }
  66. void intel_ring_reset(struct intel_ring *ring, u32 tail)
  67. {
  68. tail = intel_ring_wrap(ring, tail);
  69. ring->tail = tail;
  70. ring->head = tail;
  71. ring->emit = tail;
  72. intel_ring_update_space(ring);
  73. }
  74. void intel_ring_unpin(struct intel_ring *ring)
  75. {
  76. struct i915_vma *vma = ring->vma;
  77. if (!atomic_dec_and_test(&ring->pin_count))
  78. return;
  79. i915_vma_unset_ggtt_write(vma);
  80. if (i915_vma_is_map_and_fenceable(vma) && !HAS_LLC(vma->vm->i915))
  81. i915_vma_unpin_iomap(vma);
  82. else
  83. i915_gem_object_unpin_map(vma->obj);
  84. i915_vma_make_purgeable(vma);
  85. i915_vma_unpin(vma);
  86. }
  87. static struct i915_vma *create_ring_vma(struct i915_ggtt *ggtt, int size)
  88. {
  89. struct i915_address_space *vm = &ggtt->vm;
  90. struct drm_i915_private *i915 = vm->i915;
  91. struct drm_i915_gem_object *obj;
  92. struct i915_vma *vma;
  93. obj = i915_gem_object_create_lmem(i915, size, I915_BO_ALLOC_VOLATILE |
  94. I915_BO_ALLOC_PM_VOLATILE);
  95. if (IS_ERR(obj) && i915_ggtt_has_aperture(ggtt) && !HAS_LLC(i915))
  96. obj = i915_gem_object_create_stolen(i915, size);
  97. if (IS_ERR(obj))
  98. obj = i915_gem_object_create_internal(i915, size);
  99. if (IS_ERR(obj))
  100. return ERR_CAST(obj);
  101. /*
  102. * Mark ring buffers as read-only from GPU side (so no stray overwrites)
  103. * if supported by the platform's GGTT.
  104. */
  105. if (vm->has_read_only)
  106. i915_gem_object_set_readonly(obj);
  107. vma = i915_vma_instance(obj, vm, NULL);
  108. if (IS_ERR(vma))
  109. goto err;
  110. return vma;
  111. err:
  112. i915_gem_object_put(obj);
  113. return vma;
  114. }
  115. struct intel_ring *
  116. intel_engine_create_ring(struct intel_engine_cs *engine, int size)
  117. {
  118. struct drm_i915_private *i915 = engine->i915;
  119. struct intel_ring *ring;
  120. struct i915_vma *vma;
  121. GEM_BUG_ON(!is_power_of_2(size));
  122. GEM_BUG_ON(RING_CTL_SIZE(size) & ~RING_NR_PAGES);
  123. ring = kzalloc_obj(*ring);
  124. if (!ring)
  125. return ERR_PTR(-ENOMEM);
  126. kref_init(&ring->ref);
  127. ring->size = size;
  128. ring->wrap = BITS_PER_TYPE(ring->size) - ilog2(size);
  129. /*
  130. * Workaround an erratum on the i830 which causes a hang if
  131. * the TAIL pointer points to within the last 2 cachelines
  132. * of the buffer.
  133. */
  134. ring->effective_size = size;
  135. if (IS_I830(i915) || IS_I845G(i915))
  136. ring->effective_size -= 2 * CACHELINE_BYTES;
  137. intel_ring_update_space(ring);
  138. vma = create_ring_vma(engine->gt->ggtt, size);
  139. if (IS_ERR(vma)) {
  140. kfree(ring);
  141. return ERR_CAST(vma);
  142. }
  143. ring->vma = vma;
  144. return ring;
  145. }
  146. void intel_ring_free(struct kref *ref)
  147. {
  148. struct intel_ring *ring = container_of(ref, typeof(*ring), ref);
  149. i915_vma_put(ring->vma);
  150. kfree(ring);
  151. }
  152. static noinline int
  153. wait_for_space(struct intel_ring *ring,
  154. struct intel_timeline *tl,
  155. unsigned int bytes)
  156. {
  157. struct i915_request *target;
  158. long timeout;
  159. if (intel_ring_update_space(ring) >= bytes)
  160. return 0;
  161. GEM_BUG_ON(list_empty(&tl->requests));
  162. list_for_each_entry(target, &tl->requests, link) {
  163. if (target->ring != ring)
  164. continue;
  165. /* Would completion of this request free enough space? */
  166. if (bytes <= __intel_ring_space(target->postfix,
  167. ring->emit, ring->size))
  168. break;
  169. }
  170. if (GEM_WARN_ON(&target->link == &tl->requests))
  171. return -ENOSPC;
  172. timeout = i915_request_wait(target,
  173. I915_WAIT_INTERRUPTIBLE,
  174. MAX_SCHEDULE_TIMEOUT);
  175. if (timeout < 0)
  176. return timeout;
  177. i915_request_retire_upto(target);
  178. intel_ring_update_space(ring);
  179. GEM_BUG_ON(ring->space < bytes);
  180. return 0;
  181. }
  182. u32 *intel_ring_begin(struct i915_request *rq, unsigned int num_dwords)
  183. {
  184. struct intel_ring *ring = rq->ring;
  185. const unsigned int remain_usable = ring->effective_size - ring->emit;
  186. const unsigned int bytes = num_dwords * sizeof(u32);
  187. unsigned int need_wrap = 0;
  188. unsigned int total_bytes;
  189. u32 *cs;
  190. /* Packets must be qword aligned. */
  191. GEM_BUG_ON(num_dwords & 1);
  192. total_bytes = bytes + rq->reserved_space;
  193. GEM_BUG_ON(total_bytes > ring->effective_size);
  194. if (unlikely(total_bytes > remain_usable)) {
  195. const int remain_actual = ring->size - ring->emit;
  196. if (bytes > remain_usable) {
  197. /*
  198. * Not enough space for the basic request. So need to
  199. * flush out the remainder and then wait for
  200. * base + reserved.
  201. */
  202. total_bytes += remain_actual;
  203. need_wrap = remain_actual | 1;
  204. } else {
  205. /*
  206. * The base request will fit but the reserved space
  207. * falls off the end. So we don't need an immediate
  208. * wrap and only need to effectively wait for the
  209. * reserved size from the start of ringbuffer.
  210. */
  211. total_bytes = rq->reserved_space + remain_actual;
  212. }
  213. }
  214. if (unlikely(total_bytes > ring->space)) {
  215. int ret;
  216. /*
  217. * Space is reserved in the ringbuffer for finalising the
  218. * request, as that cannot be allowed to fail. During request
  219. * finalisation, reserved_space is set to 0 to stop the
  220. * overallocation and the assumption is that then we never need
  221. * to wait (which has the risk of failing with EINTR).
  222. *
  223. * See also i915_request_alloc() and i915_request_add().
  224. */
  225. GEM_BUG_ON(!rq->reserved_space);
  226. ret = wait_for_space(ring,
  227. i915_request_timeline(rq),
  228. total_bytes);
  229. if (unlikely(ret))
  230. return ERR_PTR(ret);
  231. }
  232. if (unlikely(need_wrap)) {
  233. need_wrap &= ~1;
  234. GEM_BUG_ON(need_wrap > ring->space);
  235. GEM_BUG_ON(ring->emit + need_wrap > ring->size);
  236. GEM_BUG_ON(!IS_ALIGNED(need_wrap, sizeof(u64)));
  237. /* Fill the tail with MI_NOOP */
  238. memset64(ring->vaddr + ring->emit, 0, need_wrap / sizeof(u64));
  239. ring->space -= need_wrap;
  240. ring->emit = 0;
  241. }
  242. GEM_BUG_ON(ring->emit > ring->size - bytes);
  243. GEM_BUG_ON(ring->space < bytes);
  244. cs = ring->vaddr + ring->emit;
  245. if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
  246. memset32(cs, POISON_INUSE, bytes / sizeof(*cs));
  247. ring->emit += bytes;
  248. ring->space -= bytes;
  249. return cs;
  250. }
  251. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  252. #include "selftest_ring.c"
  253. #endif