i915_gem_evict.c 8.4 KB

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