i915_gem_evict.c 9.0 KB

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