i915_gem_evict.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright © 2008-2010 Intel Corporation
  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
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. * Chris Wilson <chris@chris-wilson.co.uuk>
  26. *
  27. */
  28. #include <drm/drmP.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "intel_drv.h"
  32. #include "i915_trace.h"
  33. static bool ggtt_is_idle(struct drm_i915_private *dev_priv)
  34. {
  35. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  36. struct intel_engine_cs *engine;
  37. enum intel_engine_id id;
  38. for_each_engine(engine, dev_priv, id) {
  39. struct intel_timeline *tl;
  40. tl = &ggtt->base.timeline.engine[engine->id];
  41. if (i915_gem_active_isset(&tl->last_request))
  42. return false;
  43. }
  44. return true;
  45. }
  46. static bool
  47. mark_free(struct i915_vma *vma, unsigned int flags, struct list_head *unwind)
  48. {
  49. if (i915_vma_is_pinned(vma))
  50. return false;
  51. if (WARN_ON(!list_empty(&vma->exec_list)))
  52. return false;
  53. if (flags & PIN_NONFAULT && !list_empty(&vma->obj->userfault_link))
  54. return false;
  55. list_add(&vma->exec_list, unwind);
  56. return drm_mm_scan_add_block(&vma->node);
  57. }
  58. /**
  59. * i915_gem_evict_something - Evict vmas to make room for binding a new one
  60. * @vm: address space to evict from
  61. * @min_size: size of the desired free space
  62. * @alignment: alignment constraint of the desired free space
  63. * @cache_level: cache_level for the desired space
  64. * @start: start (inclusive) of the range from which to evict objects
  65. * @end: end (exclusive) of the range from which to evict objects
  66. * @flags: additional flags to control the eviction algorithm
  67. *
  68. * This function will try to evict vmas until a free space satisfying the
  69. * requirements is found. Callers must check first whether any such hole exists
  70. * already before calling this function.
  71. *
  72. * This function is used by the object/vma binding code.
  73. *
  74. * Since this function is only used to free up virtual address space it only
  75. * ignores pinned vmas, and not object where the backing storage itself is
  76. * pinned. Hence obj->pages_pin_count does not protect against eviction.
  77. *
  78. * To clarify: This is for freeing up virtual address space, not for freeing
  79. * memory in e.g. the shrinker.
  80. */
  81. int
  82. i915_gem_evict_something(struct i915_address_space *vm,
  83. u64 min_size, u64 alignment,
  84. unsigned cache_level,
  85. u64 start, u64 end,
  86. unsigned flags)
  87. {
  88. struct drm_i915_private *dev_priv = vm->i915;
  89. struct list_head eviction_list;
  90. struct list_head *phases[] = {
  91. &vm->inactive_list,
  92. &vm->active_list,
  93. NULL,
  94. }, **phase;
  95. struct i915_vma *vma, *next;
  96. int ret;
  97. lockdep_assert_held(&vm->i915->drm.struct_mutex);
  98. trace_i915_gem_evict(vm, min_size, alignment, flags);
  99. /*
  100. * The goal is to evict objects and amalgamate space in LRU order.
  101. * The oldest idle objects reside on the inactive list, which is in
  102. * retirement order. The next objects to retire are those in flight,
  103. * on the active list, again in retirement order.
  104. *
  105. * The retirement sequence is thus:
  106. * 1. Inactive objects (already retired)
  107. * 2. Active objects (will stall on unbinding)
  108. *
  109. * On each list, the oldest objects lie at the HEAD with the freshest
  110. * object on the TAIL.
  111. */
  112. if (start != 0 || end != vm->total) {
  113. drm_mm_init_scan_with_range(&vm->mm, min_size,
  114. alignment, cache_level,
  115. start, end);
  116. } else
  117. drm_mm_init_scan(&vm->mm, min_size, alignment, cache_level);
  118. if (flags & PIN_NONBLOCK)
  119. phases[1] = NULL;
  120. search_again:
  121. INIT_LIST_HEAD(&eviction_list);
  122. phase = phases;
  123. do {
  124. list_for_each_entry(vma, *phase, vm_link)
  125. if (mark_free(vma, flags, &eviction_list))
  126. goto found;
  127. } while (*++phase);
  128. /* Nothing found, clean up and bail out! */
  129. list_for_each_entry_safe(vma, next, &eviction_list, exec_list) {
  130. ret = drm_mm_scan_remove_block(&vma->node);
  131. BUG_ON(ret);
  132. INIT_LIST_HEAD(&vma->exec_list);
  133. }
  134. /* Can we unpin some objects such as idle hw contents,
  135. * or pending flips? But since only the GGTT has global entries
  136. * such as scanouts, rinbuffers and contexts, we can skip the
  137. * purge when inspecting per-process local address spaces.
  138. */
  139. if (!i915_is_ggtt(vm) || flags & PIN_NONBLOCK)
  140. return -ENOSPC;
  141. if (ggtt_is_idle(dev_priv)) {
  142. /* If we still have pending pageflip completions, drop
  143. * back to userspace to give our workqueues time to
  144. * acquire our locks and unpin the old scanouts.
  145. */
  146. return intel_has_pending_fb_unpin(dev_priv) ? -EAGAIN : -ENOSPC;
  147. }
  148. /* Not everything in the GGTT is tracked via vma (otherwise we
  149. * could evict as required with minimal stalling) so we are forced
  150. * to idle the GPU and explicitly retire outstanding requests in
  151. * the hopes that we can then remove contexts and the like only
  152. * bound by their active reference.
  153. */
  154. ret = i915_gem_switch_to_kernel_context(dev_priv);
  155. if (ret)
  156. return ret;
  157. ret = i915_gem_wait_for_idle(dev_priv,
  158. I915_WAIT_INTERRUPTIBLE |
  159. I915_WAIT_LOCKED);
  160. if (ret)
  161. return ret;
  162. i915_gem_retire_requests(dev_priv);
  163. goto search_again;
  164. found:
  165. /* drm_mm doesn't allow any other other operations while
  166. * scanning, therefore store to-be-evicted objects on a
  167. * temporary list and take a reference for all before
  168. * calling unbind (which may remove the active reference
  169. * of any of our objects, thus corrupting the list).
  170. */
  171. list_for_each_entry_safe(vma, next, &eviction_list, exec_list) {
  172. if (drm_mm_scan_remove_block(&vma->node))
  173. __i915_vma_pin(vma);
  174. else
  175. list_del_init(&vma->exec_list);
  176. }
  177. /* Unbinding will emit any required flushes */
  178. while (!list_empty(&eviction_list)) {
  179. vma = list_first_entry(&eviction_list,
  180. struct i915_vma,
  181. exec_list);
  182. list_del_init(&vma->exec_list);
  183. __i915_vma_unpin(vma);
  184. if (ret == 0)
  185. ret = i915_vma_unbind(vma);
  186. }
  187. return ret;
  188. }
  189. int
  190. i915_gem_evict_for_vma(struct i915_vma *target)
  191. {
  192. struct drm_mm_node *node, *next;
  193. lockdep_assert_held(&target->vm->i915->drm.struct_mutex);
  194. list_for_each_entry_safe(node, next,
  195. &target->vm->mm.head_node.node_list,
  196. node_list) {
  197. struct i915_vma *vma;
  198. int ret;
  199. if (node->start + node->size <= target->node.start)
  200. continue;
  201. if (node->start >= target->node.start + target->node.size)
  202. break;
  203. vma = container_of(node, typeof(*vma), node);
  204. if (i915_vma_is_pinned(vma)) {
  205. if (!vma->exec_entry || i915_vma_pin_count(vma) > 1)
  206. /* Object is pinned for some other use */
  207. return -EBUSY;
  208. /* We need to evict a buffer in the same batch */
  209. if (vma->exec_entry->flags & EXEC_OBJECT_PINNED)
  210. /* Overlapping fixed objects in the same batch */
  211. return -EINVAL;
  212. return -ENOSPC;
  213. }
  214. ret = i915_vma_unbind(vma);
  215. if (ret)
  216. return ret;
  217. }
  218. return 0;
  219. }
  220. /**
  221. * i915_gem_evict_vm - Evict all idle vmas from a vm
  222. * @vm: Address space to cleanse
  223. * @do_idle: Boolean directing whether to idle first.
  224. *
  225. * This function evicts all idles vmas from a vm. If all unpinned vmas should be
  226. * evicted the @do_idle needs to be set to true.
  227. *
  228. * This is used by the execbuf code as a last-ditch effort to defragment the
  229. * address space.
  230. *
  231. * To clarify: This is for freeing up virtual address space, not for freeing
  232. * memory in e.g. the shrinker.
  233. */
  234. int i915_gem_evict_vm(struct i915_address_space *vm, bool do_idle)
  235. {
  236. struct i915_vma *vma, *next;
  237. int ret;
  238. lockdep_assert_held(&vm->i915->drm.struct_mutex);
  239. trace_i915_gem_evict_vm(vm);
  240. if (do_idle) {
  241. struct drm_i915_private *dev_priv = vm->i915;
  242. if (i915_is_ggtt(vm)) {
  243. ret = i915_gem_switch_to_kernel_context(dev_priv);
  244. if (ret)
  245. return ret;
  246. }
  247. ret = i915_gem_wait_for_idle(dev_priv,
  248. I915_WAIT_INTERRUPTIBLE |
  249. I915_WAIT_LOCKED);
  250. if (ret)
  251. return ret;
  252. i915_gem_retire_requests(dev_priv);
  253. WARN_ON(!list_empty(&vm->active_list));
  254. }
  255. list_for_each_entry_safe(vma, next, &vm->inactive_list, vm_link)
  256. if (!i915_vma_is_pinned(vma))
  257. WARN_ON(i915_vma_unbind(vma));
  258. return 0;
  259. }