i915_gem_evict.c 12 KB

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