i915_gem_evict.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 drm_mm_scan *scan,
  48. struct i915_vma *vma,
  49. unsigned int flags,
  50. struct list_head *unwind)
  51. {
  52. if (i915_vma_is_pinned(vma))
  53. return false;
  54. if (WARN_ON(!list_empty(&vma->exec_list)))
  55. return false;
  56. if (flags & PIN_NONFAULT && !list_empty(&vma->obj->userfault_link))
  57. return false;
  58. list_add(&vma->exec_list, unwind);
  59. return drm_mm_scan_add_block(scan, &vma->node);
  60. }
  61. /**
  62. * i915_gem_evict_something - Evict vmas to make room for binding a new one
  63. * @vm: address space to evict from
  64. * @min_size: size of the desired free space
  65. * @alignment: alignment constraint of the desired free space
  66. * @cache_level: cache_level for the desired space
  67. * @start: start (inclusive) of the range from which to evict objects
  68. * @end: end (exclusive) of the range from which to evict objects
  69. * @flags: additional flags to control the eviction algorithm
  70. *
  71. * This function will try to evict vmas until a free space satisfying the
  72. * requirements is found. Callers must check first whether any such hole exists
  73. * already before calling this function.
  74. *
  75. * This function is used by the object/vma binding code.
  76. *
  77. * Since this function is only used to free up virtual address space it only
  78. * ignores pinned vmas, and not object where the backing storage itself is
  79. * pinned. Hence obj->pages_pin_count does not protect against eviction.
  80. *
  81. * To clarify: This is for freeing up virtual address space, not for freeing
  82. * memory in e.g. the shrinker.
  83. */
  84. int
  85. i915_gem_evict_something(struct i915_address_space *vm,
  86. u64 min_size, u64 alignment,
  87. unsigned cache_level,
  88. u64 start, u64 end,
  89. unsigned flags)
  90. {
  91. struct drm_i915_private *dev_priv = vm->i915;
  92. struct drm_mm_scan scan;
  93. struct list_head eviction_list;
  94. struct list_head *phases[] = {
  95. &vm->inactive_list,
  96. &vm->active_list,
  97. NULL,
  98. }, **phase;
  99. struct i915_vma *vma, *next;
  100. struct drm_mm_node *node;
  101. enum drm_mm_insert_mode mode;
  102. int ret;
  103. lockdep_assert_held(&vm->i915->drm.struct_mutex);
  104. trace_i915_gem_evict(vm, min_size, alignment, flags);
  105. /*
  106. * The goal is to evict objects and amalgamate space in LRU order.
  107. * The oldest idle objects reside on the inactive list, which is in
  108. * retirement order. The next objects to retire are those in flight,
  109. * on the active list, again in retirement order.
  110. *
  111. * The retirement sequence is thus:
  112. * 1. Inactive objects (already retired)
  113. * 2. Active objects (will stall on unbinding)
  114. *
  115. * On each list, the oldest objects lie at the HEAD with the freshest
  116. * object on the TAIL.
  117. */
  118. mode = DRM_MM_INSERT_BEST;
  119. if (flags & PIN_HIGH)
  120. mode = DRM_MM_INSERT_HIGH;
  121. if (flags & PIN_MAPPABLE)
  122. mode = DRM_MM_INSERT_LOW;
  123. drm_mm_scan_init_with_range(&scan, &vm->mm,
  124. min_size, alignment, cache_level,
  125. start, end, mode);
  126. /* Retire before we search the active list. Although we have
  127. * reasonable accuracy in our retirement lists, we may have
  128. * a stray pin (preventing eviction) that can only be resolved by
  129. * retiring.
  130. */
  131. if (!(flags & PIN_NONBLOCK))
  132. i915_gem_retire_requests(dev_priv);
  133. else
  134. phases[1] = NULL;
  135. search_again:
  136. INIT_LIST_HEAD(&eviction_list);
  137. phase = phases;
  138. do {
  139. list_for_each_entry(vma, *phase, vm_link)
  140. if (mark_free(&scan, vma, flags, &eviction_list))
  141. goto found;
  142. } while (*++phase);
  143. /* Nothing found, clean up and bail out! */
  144. list_for_each_entry_safe(vma, next, &eviction_list, exec_list) {
  145. ret = drm_mm_scan_remove_block(&scan, &vma->node);
  146. BUG_ON(ret);
  147. INIT_LIST_HEAD(&vma->exec_list);
  148. }
  149. /* Can we unpin some objects such as idle hw contents,
  150. * or pending flips? But since only the GGTT has global entries
  151. * such as scanouts, rinbuffers and contexts, we can skip the
  152. * purge when inspecting per-process local address spaces.
  153. */
  154. if (!i915_is_ggtt(vm) || flags & PIN_NONBLOCK)
  155. return -ENOSPC;
  156. if (ggtt_is_idle(dev_priv)) {
  157. /* If we still have pending pageflip completions, drop
  158. * back to userspace to give our workqueues time to
  159. * acquire our locks and unpin the old scanouts.
  160. */
  161. return intel_has_pending_fb_unpin(dev_priv) ? -EAGAIN : -ENOSPC;
  162. }
  163. /* Not everything in the GGTT is tracked via vma (otherwise we
  164. * could evict as required with minimal stalling) so we are forced
  165. * to idle the GPU and explicitly retire outstanding requests in
  166. * the hopes that we can then remove contexts and the like only
  167. * bound by their active reference.
  168. */
  169. ret = i915_gem_switch_to_kernel_context(dev_priv);
  170. if (ret)
  171. return ret;
  172. ret = i915_gem_wait_for_idle(dev_priv,
  173. I915_WAIT_INTERRUPTIBLE |
  174. I915_WAIT_LOCKED);
  175. if (ret)
  176. return ret;
  177. goto search_again;
  178. found:
  179. /* drm_mm doesn't allow any other other operations while
  180. * scanning, therefore store to-be-evicted objects on a
  181. * temporary list and take a reference for all before
  182. * calling unbind (which may remove the active reference
  183. * of any of our objects, thus corrupting the list).
  184. */
  185. list_for_each_entry_safe(vma, next, &eviction_list, exec_list) {
  186. if (drm_mm_scan_remove_block(&scan, &vma->node))
  187. __i915_vma_pin(vma);
  188. else
  189. list_del_init(&vma->exec_list);
  190. }
  191. /* Unbinding will emit any required flushes */
  192. ret = 0;
  193. while (!list_empty(&eviction_list)) {
  194. vma = list_first_entry(&eviction_list,
  195. struct i915_vma,
  196. exec_list);
  197. list_del_init(&vma->exec_list);
  198. __i915_vma_unpin(vma);
  199. if (ret == 0)
  200. ret = i915_vma_unbind(vma);
  201. }
  202. while (ret == 0 && (node = drm_mm_scan_color_evict(&scan))) {
  203. vma = container_of(node, struct i915_vma, node);
  204. ret = i915_vma_unbind(vma);
  205. }
  206. return ret;
  207. }
  208. /**
  209. * i915_gem_evict_for_vma - Evict vmas to make room for binding a new one
  210. * @vm: address space to evict from
  211. * @target: range (and color) to evict for
  212. * @flags: additional flags to control the eviction algorithm
  213. *
  214. * This function will try to evict vmas that overlap the target node.
  215. *
  216. * To clarify: This is for freeing up virtual address space, not for freeing
  217. * memory in e.g. the shrinker.
  218. */
  219. int i915_gem_evict_for_node(struct i915_address_space *vm,
  220. struct drm_mm_node *target,
  221. unsigned int flags)
  222. {
  223. LIST_HEAD(eviction_list);
  224. struct drm_mm_node *node;
  225. u64 start = target->start;
  226. u64 end = start + target->size;
  227. struct i915_vma *vma, *next;
  228. bool check_color;
  229. int ret = 0;
  230. lockdep_assert_held(&vm->i915->drm.struct_mutex);
  231. GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
  232. GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
  233. trace_i915_gem_evict_node(vm, target, flags);
  234. /* Retire before we search the active list. Although we have
  235. * reasonable accuracy in our retirement lists, we may have
  236. * a stray pin (preventing eviction) that can only be resolved by
  237. * retiring.
  238. */
  239. if (!(flags & PIN_NONBLOCK))
  240. i915_gem_retire_requests(vm->i915);
  241. check_color = vm->mm.color_adjust;
  242. if (check_color) {
  243. /* Expand search to cover neighbouring guard pages (or lack!) */
  244. if (start)
  245. start -= I915_GTT_PAGE_SIZE;
  246. /* Always look at the page afterwards to avoid the end-of-GTT */
  247. end += I915_GTT_PAGE_SIZE;
  248. }
  249. GEM_BUG_ON(start >= end);
  250. drm_mm_for_each_node_in_range(node, &vm->mm, start, end) {
  251. /* If we find any non-objects (!vma), we cannot evict them */
  252. if (node->color == I915_COLOR_UNEVICTABLE) {
  253. ret = -ENOSPC;
  254. break;
  255. }
  256. GEM_BUG_ON(!node->allocated);
  257. vma = container_of(node, typeof(*vma), node);
  258. /* If we are using coloring to insert guard pages between
  259. * different cache domains within the address space, we have
  260. * to check whether the objects on either side of our range
  261. * abutt and conflict. If they are in conflict, then we evict
  262. * those as well to make room for our guard pages.
  263. */
  264. if (check_color) {
  265. if (node->start + node->size == target->start) {
  266. if (node->color == target->color)
  267. continue;
  268. }
  269. if (node->start == target->start + target->size) {
  270. if (node->color == target->color)
  271. continue;
  272. }
  273. }
  274. if (flags & PIN_NONBLOCK &&
  275. (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))) {
  276. ret = -ENOSPC;
  277. break;
  278. }
  279. /* Overlap of objects in the same batch? */
  280. if (i915_vma_is_pinned(vma) || !list_empty(&vma->exec_list)) {
  281. ret = -ENOSPC;
  282. if (vma->exec_entry &&
  283. vma->exec_entry->flags & EXEC_OBJECT_PINNED)
  284. ret = -EINVAL;
  285. break;
  286. }
  287. /* Never show fear in the face of dragons!
  288. *
  289. * We cannot directly remove this node from within this
  290. * iterator and as with i915_gem_evict_something() we employ
  291. * the vma pin_count in order to prevent the action of
  292. * unbinding one vma from freeing (by dropping its active
  293. * reference) another in our eviction list.
  294. */
  295. __i915_vma_pin(vma);
  296. list_add(&vma->exec_list, &eviction_list);
  297. }
  298. list_for_each_entry_safe(vma, next, &eviction_list, exec_list) {
  299. list_del_init(&vma->exec_list);
  300. __i915_vma_unpin(vma);
  301. if (ret == 0)
  302. ret = i915_vma_unbind(vma);
  303. }
  304. return ret;
  305. }
  306. /**
  307. * i915_gem_evict_vm - Evict all idle vmas from a vm
  308. * @vm: Address space to cleanse
  309. * @do_idle: Boolean directing whether to idle first.
  310. *
  311. * This function evicts all idles vmas from a vm. If all unpinned vmas should be
  312. * evicted the @do_idle needs to be set to true.
  313. *
  314. * This is used by the execbuf code as a last-ditch effort to defragment the
  315. * address space.
  316. *
  317. * To clarify: This is for freeing up virtual address space, not for freeing
  318. * memory in e.g. the shrinker.
  319. */
  320. int i915_gem_evict_vm(struct i915_address_space *vm, bool do_idle)
  321. {
  322. struct i915_vma *vma, *next;
  323. int ret;
  324. lockdep_assert_held(&vm->i915->drm.struct_mutex);
  325. trace_i915_gem_evict_vm(vm);
  326. if (do_idle) {
  327. struct drm_i915_private *dev_priv = vm->i915;
  328. if (i915_is_ggtt(vm)) {
  329. ret = i915_gem_switch_to_kernel_context(dev_priv);
  330. if (ret)
  331. return ret;
  332. }
  333. ret = i915_gem_wait_for_idle(dev_priv,
  334. I915_WAIT_INTERRUPTIBLE |
  335. I915_WAIT_LOCKED);
  336. if (ret)
  337. return ret;
  338. WARN_ON(!list_empty(&vm->active_list));
  339. }
  340. list_for_each_entry_safe(vma, next, &vm->inactive_list, vm_link)
  341. if (!i915_vma_is_pinned(vma))
  342. WARN_ON(i915_vma_unbind(vma));
  343. return 0;
  344. }
  345. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  346. #include "selftests/i915_gem_evict.c"
  347. #endif