i915_gem_evict.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. mark_free(struct i915_vma *vma, struct list_head *unwind)
  35. {
  36. if (vma->pin_count)
  37. return false;
  38. if (WARN_ON(!list_empty(&vma->exec_list)))
  39. return false;
  40. list_add(&vma->exec_list, unwind);
  41. return drm_mm_scan_add_block(&vma->node);
  42. }
  43. /**
  44. * i915_gem_evict_something - Evict vmas to make room for binding a new one
  45. * @dev: drm_device
  46. * @vm: address space to evict from
  47. * @min_size: size of the desired free space
  48. * @alignment: alignment constraint of the desired free space
  49. * @cache_level: cache_level for the desired space
  50. * @start: start (inclusive) of the range from which to evict objects
  51. * @end: end (exclusive) of the range from which to evict objects
  52. * @flags: additional flags to control the eviction algorithm
  53. *
  54. * This function will try to evict vmas until a free space satisfying the
  55. * requirements is found. Callers must check first whether any such hole exists
  56. * already before calling this function.
  57. *
  58. * This function is used by the object/vma binding code.
  59. *
  60. * Since this function is only used to free up virtual address space it only
  61. * ignores pinned vmas, and not object where the backing storage itself is
  62. * pinned. Hence obj->pages_pin_count does not protect against eviction.
  63. *
  64. * To clarify: This is for freeing up virtual address space, not for freeing
  65. * memory in e.g. the shrinker.
  66. */
  67. int
  68. i915_gem_evict_something(struct drm_device *dev, struct i915_address_space *vm,
  69. int min_size, unsigned alignment, unsigned cache_level,
  70. unsigned long start, unsigned long end,
  71. unsigned flags)
  72. {
  73. struct list_head eviction_list, unwind_list;
  74. struct i915_vma *vma;
  75. int ret = 0;
  76. int pass = 0;
  77. trace_i915_gem_evict(dev, min_size, alignment, flags);
  78. /*
  79. * The goal is to evict objects and amalgamate space in LRU order.
  80. * The oldest idle objects reside on the inactive list, which is in
  81. * retirement order. The next objects to retire are those on the (per
  82. * ring) active list that do not have an outstanding flush. Once the
  83. * hardware reports completion (the seqno is updated after the
  84. * batchbuffer has been finished) the clean buffer objects would
  85. * be retired to the inactive list. Any dirty objects would be added
  86. * to the tail of the flushing list. So after processing the clean
  87. * active objects we need to emit a MI_FLUSH to retire the flushing
  88. * list, hence the retirement order of the flushing list is in
  89. * advance of the dirty objects on the active lists.
  90. *
  91. * The retirement sequence is thus:
  92. * 1. Inactive objects (already retired)
  93. * 2. Clean active objects
  94. * 3. Flushing list
  95. * 4. Dirty active objects.
  96. *
  97. * On each list, the oldest objects lie at the HEAD with the freshest
  98. * object on the TAIL.
  99. */
  100. INIT_LIST_HEAD(&unwind_list);
  101. if (start != 0 || end != vm->total) {
  102. drm_mm_init_scan_with_range(&vm->mm, min_size,
  103. alignment, cache_level,
  104. start, end);
  105. } else
  106. drm_mm_init_scan(&vm->mm, min_size, alignment, cache_level);
  107. search_again:
  108. /* First see if there is a large enough contiguous idle region... */
  109. list_for_each_entry(vma, &vm->inactive_list, vm_link) {
  110. if (mark_free(vma, &unwind_list))
  111. goto found;
  112. }
  113. if (flags & PIN_NONBLOCK)
  114. goto none;
  115. /* Now merge in the soon-to-be-expired objects... */
  116. list_for_each_entry(vma, &vm->active_list, vm_link) {
  117. if (mark_free(vma, &unwind_list))
  118. goto found;
  119. }
  120. none:
  121. /* Nothing found, clean up and bail out! */
  122. while (!list_empty(&unwind_list)) {
  123. vma = list_first_entry(&unwind_list,
  124. struct i915_vma,
  125. exec_list);
  126. ret = drm_mm_scan_remove_block(&vma->node);
  127. BUG_ON(ret);
  128. list_del_init(&vma->exec_list);
  129. }
  130. /* Can we unpin some objects such as idle hw contents,
  131. * or pending flips?
  132. */
  133. if (flags & PIN_NONBLOCK)
  134. return -ENOSPC;
  135. /* Only idle the GPU and repeat the search once */
  136. if (pass++ == 0) {
  137. ret = i915_gpu_idle(dev);
  138. if (ret)
  139. return ret;
  140. i915_gem_retire_requests(dev);
  141. goto search_again;
  142. }
  143. /* If we still have pending pageflip completions, drop
  144. * back to userspace to give our workqueues time to
  145. * acquire our locks and unpin the old scanouts.
  146. */
  147. return intel_has_pending_fb_unpin(dev) ? -EAGAIN : -ENOSPC;
  148. found:
  149. /* drm_mm doesn't allow any other other operations while
  150. * scanning, therefore store to be evicted objects on a
  151. * temporary list. */
  152. INIT_LIST_HEAD(&eviction_list);
  153. while (!list_empty(&unwind_list)) {
  154. vma = list_first_entry(&unwind_list,
  155. struct i915_vma,
  156. exec_list);
  157. if (drm_mm_scan_remove_block(&vma->node)) {
  158. list_move(&vma->exec_list, &eviction_list);
  159. drm_gem_object_reference(&vma->obj->base);
  160. continue;
  161. }
  162. list_del_init(&vma->exec_list);
  163. }
  164. /* Unbinding will emit any required flushes */
  165. while (!list_empty(&eviction_list)) {
  166. struct drm_gem_object *obj;
  167. vma = list_first_entry(&eviction_list,
  168. struct i915_vma,
  169. exec_list);
  170. obj = &vma->obj->base;
  171. list_del_init(&vma->exec_list);
  172. if (ret == 0)
  173. ret = i915_vma_unbind(vma);
  174. drm_gem_object_unreference(obj);
  175. }
  176. return ret;
  177. }
  178. int
  179. i915_gem_evict_for_vma(struct i915_vma *target)
  180. {
  181. struct drm_mm_node *node, *next;
  182. list_for_each_entry_safe(node, next,
  183. &target->vm->mm.head_node.node_list,
  184. node_list) {
  185. struct i915_vma *vma;
  186. int ret;
  187. if (node->start + node->size <= target->node.start)
  188. continue;
  189. if (node->start >= target->node.start + target->node.size)
  190. break;
  191. vma = container_of(node, typeof(*vma), node);
  192. if (vma->pin_count) {
  193. if (!vma->exec_entry || (vma->pin_count > 1))
  194. /* Object is pinned for some other use */
  195. return -EBUSY;
  196. /* We need to evict a buffer in the same batch */
  197. if (vma->exec_entry->flags & EXEC_OBJECT_PINNED)
  198. /* Overlapping fixed objects in the same batch */
  199. return -EINVAL;
  200. return -ENOSPC;
  201. }
  202. ret = i915_vma_unbind(vma);
  203. if (ret)
  204. return ret;
  205. }
  206. return 0;
  207. }
  208. /**
  209. * i915_gem_evict_vm - Evict all idle vmas from a vm
  210. * @vm: Address space to cleanse
  211. * @do_idle: Boolean directing whether to idle first.
  212. *
  213. * This function evicts all idles vmas from a vm. If all unpinned vmas should be
  214. * evicted the @do_idle needs to be set to true.
  215. *
  216. * This is used by the execbuf code as a last-ditch effort to defragment the
  217. * address space.
  218. *
  219. * To clarify: This is for freeing up virtual address space, not for freeing
  220. * memory in e.g. the shrinker.
  221. */
  222. int i915_gem_evict_vm(struct i915_address_space *vm, bool do_idle)
  223. {
  224. struct i915_vma *vma, *next;
  225. int ret;
  226. WARN_ON(!mutex_is_locked(&vm->dev->struct_mutex));
  227. trace_i915_gem_evict_vm(vm);
  228. if (do_idle) {
  229. ret = i915_gpu_idle(vm->dev);
  230. if (ret)
  231. return ret;
  232. i915_gem_retire_requests(vm->dev);
  233. WARN_ON(!list_empty(&vm->active_list));
  234. }
  235. list_for_each_entry_safe(vma, next, &vm->inactive_list, vm_link)
  236. if (vma->pin_count == 0)
  237. WARN_ON(i915_vma_unbind(vma));
  238. return 0;
  239. }