i915_gem_evict.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. * @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. * @mappable: whether the free space must be mappable
  51. * @nonblocking: whether evicting active objects is allowed or not
  52. *
  53. * This function will try to evict vmas until a free space satisfying the
  54. * requirements is found. Callers must check first whether any such hole exists
  55. * already before calling this function.
  56. *
  57. * This function is used by the object/vma binding code.
  58. *
  59. * To clarify: This is for freeing up virtual address space, not for freeing
  60. * memory in e.g. the shrinker.
  61. */
  62. int
  63. i915_gem_evict_something(struct drm_device *dev, struct i915_address_space *vm,
  64. int min_size, unsigned alignment, unsigned cache_level,
  65. unsigned long start, unsigned long end,
  66. unsigned flags)
  67. {
  68. struct list_head eviction_list, unwind_list;
  69. struct i915_vma *vma;
  70. int ret = 0;
  71. int pass = 0;
  72. trace_i915_gem_evict(dev, min_size, alignment, flags);
  73. /*
  74. * The goal is to evict objects and amalgamate space in LRU order.
  75. * The oldest idle objects reside on the inactive list, which is in
  76. * retirement order. The next objects to retire are those on the (per
  77. * ring) active list that do not have an outstanding flush. Once the
  78. * hardware reports completion (the seqno is updated after the
  79. * batchbuffer has been finished) the clean buffer objects would
  80. * be retired to the inactive list. Any dirty objects would be added
  81. * to the tail of the flushing list. So after processing the clean
  82. * active objects we need to emit a MI_FLUSH to retire the flushing
  83. * list, hence the retirement order of the flushing list is in
  84. * advance of the dirty objects on the active lists.
  85. *
  86. * The retirement sequence is thus:
  87. * 1. Inactive objects (already retired)
  88. * 2. Clean active objects
  89. * 3. Flushing list
  90. * 4. Dirty active objects.
  91. *
  92. * On each list, the oldest objects lie at the HEAD with the freshest
  93. * object on the TAIL.
  94. */
  95. INIT_LIST_HEAD(&unwind_list);
  96. if (start != 0 || end != vm->total) {
  97. drm_mm_init_scan_with_range(&vm->mm, min_size,
  98. alignment, cache_level,
  99. start, end);
  100. } else
  101. drm_mm_init_scan(&vm->mm, min_size, alignment, cache_level);
  102. search_again:
  103. /* First see if there is a large enough contiguous idle region... */
  104. list_for_each_entry(vma, &vm->inactive_list, mm_list) {
  105. if (mark_free(vma, &unwind_list))
  106. goto found;
  107. }
  108. if (flags & PIN_NONBLOCK)
  109. goto none;
  110. /* Now merge in the soon-to-be-expired objects... */
  111. list_for_each_entry(vma, &vm->active_list, mm_list) {
  112. if (mark_free(vma, &unwind_list))
  113. goto found;
  114. }
  115. none:
  116. /* Nothing found, clean up and bail out! */
  117. while (!list_empty(&unwind_list)) {
  118. vma = list_first_entry(&unwind_list,
  119. struct i915_vma,
  120. exec_list);
  121. ret = drm_mm_scan_remove_block(&vma->node);
  122. BUG_ON(ret);
  123. list_del_init(&vma->exec_list);
  124. }
  125. /* Can we unpin some objects such as idle hw contents,
  126. * or pending flips?
  127. */
  128. if (flags & PIN_NONBLOCK)
  129. return -ENOSPC;
  130. /* Only idle the GPU and repeat the search once */
  131. if (pass++ == 0) {
  132. ret = i915_gpu_idle(dev);
  133. if (ret)
  134. return ret;
  135. i915_gem_retire_requests(dev);
  136. goto search_again;
  137. }
  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(dev) ? -EAGAIN : -ENOSPC;
  143. found:
  144. /* drm_mm doesn't allow any other other operations while
  145. * scanning, therefore store to be evicted objects on a
  146. * temporary list. */
  147. INIT_LIST_HEAD(&eviction_list);
  148. while (!list_empty(&unwind_list)) {
  149. vma = list_first_entry(&unwind_list,
  150. struct i915_vma,
  151. exec_list);
  152. if (drm_mm_scan_remove_block(&vma->node)) {
  153. list_move(&vma->exec_list, &eviction_list);
  154. drm_gem_object_reference(&vma->obj->base);
  155. continue;
  156. }
  157. list_del_init(&vma->exec_list);
  158. }
  159. /* Unbinding will emit any required flushes */
  160. while (!list_empty(&eviction_list)) {
  161. struct drm_gem_object *obj;
  162. vma = list_first_entry(&eviction_list,
  163. struct i915_vma,
  164. exec_list);
  165. obj = &vma->obj->base;
  166. list_del_init(&vma->exec_list);
  167. if (ret == 0)
  168. ret = i915_vma_unbind(vma);
  169. drm_gem_object_unreference(obj);
  170. }
  171. return ret;
  172. }
  173. /**
  174. * i915_gem_evict_vm - Evict all idle vmas from a vm
  175. *
  176. * @vm: Address space to cleanse
  177. * @do_idle: Boolean directing whether to idle first.
  178. *
  179. * This function evicts all idles vmas from a vm. If all unpinned vmas should be
  180. * evicted the @do_idle needs to be set to true.
  181. *
  182. * This is used by the execbuf code as a last-ditch effort to defragment the
  183. * address space.
  184. *
  185. * To clarify: This is for freeing up virtual address space, not for freeing
  186. * memory in e.g. the shrinker.
  187. */
  188. int i915_gem_evict_vm(struct i915_address_space *vm, bool do_idle)
  189. {
  190. struct i915_vma *vma, *next;
  191. int ret;
  192. trace_i915_gem_evict_vm(vm);
  193. if (do_idle) {
  194. ret = i915_gpu_idle(vm->dev);
  195. if (ret)
  196. return ret;
  197. i915_gem_retire_requests(vm->dev);
  198. }
  199. list_for_each_entry_safe(vma, next, &vm->inactive_list, mm_list)
  200. if (vma->pin_count == 0)
  201. WARN_ON(i915_vma_unbind(vma));
  202. return 0;
  203. }
  204. /**
  205. * i915_gem_evict_everything - Try to evict all objects
  206. * @dev: Device to evict objects for
  207. *
  208. * This functions tries to evict all gem objects from all address spaces. Used
  209. * by the shrinker as a last-ditch effort and for suspend, before releasing the
  210. * backing storage of all unbound objects.
  211. */
  212. int
  213. i915_gem_evict_everything(struct drm_device *dev)
  214. {
  215. struct drm_i915_private *dev_priv = dev->dev_private;
  216. struct i915_address_space *vm;
  217. bool lists_empty = true;
  218. int ret;
  219. list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
  220. lists_empty = (list_empty(&vm->inactive_list) &&
  221. list_empty(&vm->active_list));
  222. if (!lists_empty)
  223. lists_empty = false;
  224. }
  225. if (lists_empty)
  226. return -ENOSPC;
  227. trace_i915_gem_evict_everything(dev);
  228. /* The gpu_idle will flush everything in the write domain to the
  229. * active list. Then we must move everything off the active list
  230. * with retire requests.
  231. */
  232. ret = i915_gpu_idle(dev);
  233. if (ret)
  234. return ret;
  235. i915_gem_retire_requests(dev);
  236. /* Having flushed everything, unbind() should never raise an error */
  237. list_for_each_entry(vm, &dev_priv->vm_list, global_link)
  238. WARN_ON(i915_gem_evict_vm(vm, false));
  239. return 0;
  240. }