2 * Copyright © 2008-2010 Intel Corporation
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:
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
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
24 * Eric Anholt <eric@anholt.net>
25 * Chris Wilson <chris@chris-wilson.co.uuk>
29 #include <drm/i915_drm.h>
31 #include "gem/i915_gem_context.h"
34 #include "intel_drv.h"
35 #include "i915_trace.h"
37 I915_SELFTEST_DECLARE(static struct igt_evict_ctl {
41 static int ggtt_flush(struct drm_i915_private *i915)
44 * Not everything in the GGTT is tracked via vma (otherwise we
45 * could evict as required with minimal stalling) so we are forced
46 * to idle the GPU and explicitly retire outstanding requests in
47 * the hopes that we can then remove contexts and the like only
48 * bound by their active reference.
50 return i915_gem_wait_for_idle(i915,
51 I915_WAIT_INTERRUPTIBLE |
53 MAX_SCHEDULE_TIMEOUT);
57 mark_free(struct drm_mm_scan *scan,
60 struct list_head *unwind)
62 if (i915_vma_is_pinned(vma))
65 if (flags & PIN_NONFAULT && i915_vma_has_userfault(vma))
68 list_add(&vma->evict_link, unwind);
69 return drm_mm_scan_add_block(scan, &vma->node);
73 * i915_gem_evict_something - Evict vmas to make room for binding a new one
74 * @vm: address space to evict from
75 * @min_size: size of the desired free space
76 * @alignment: alignment constraint of the desired free space
77 * @cache_level: cache_level for the desired space
78 * @start: start (inclusive) of the range from which to evict objects
79 * @end: end (exclusive) of the range from which to evict objects
80 * @flags: additional flags to control the eviction algorithm
82 * This function will try to evict vmas until a free space satisfying the
83 * requirements is found. Callers must check first whether any such hole exists
84 * already before calling this function.
86 * This function is used by the object/vma binding code.
88 * Since this function is only used to free up virtual address space it only
89 * ignores pinned vmas, and not object where the backing storage itself is
90 * pinned. Hence obj->pages_pin_count does not protect against eviction.
92 * To clarify: This is for freeing up virtual address space, not for freeing
93 * memory in e.g. the shrinker.
96 i915_gem_evict_something(struct i915_address_space *vm,
97 u64 min_size, u64 alignment,
102 struct drm_i915_private *dev_priv = vm->i915;
103 struct drm_mm_scan scan;
104 struct list_head eviction_list;
105 struct i915_vma *vma, *next;
106 struct drm_mm_node *node;
107 enum drm_mm_insert_mode mode;
108 struct i915_vma *active;
111 lockdep_assert_held(&vm->i915->drm.struct_mutex);
112 trace_i915_gem_evict(vm, min_size, alignment, flags);
115 * The goal is to evict objects and amalgamate space in rough LRU order.
116 * Since both active and inactive objects reside on the same list,
117 * in a mix of creation and last scanned order, as we process the list
118 * we sort it into inactive/active, which keeps the active portion
119 * in a rough MRU order.
121 * The retirement sequence is thus:
122 * 1. Inactive objects (already retired, random order)
123 * 2. Active objects (will stall on unbinding, oldest scanned first)
125 mode = DRM_MM_INSERT_BEST;
126 if (flags & PIN_HIGH)
127 mode = DRM_MM_INSERT_HIGH;
128 if (flags & PIN_MAPPABLE)
129 mode = DRM_MM_INSERT_LOW;
130 drm_mm_scan_init_with_range(&scan, &vm->mm,
131 min_size, alignment, cache_level,
135 * Retire before we search the active list. Although we have
136 * reasonable accuracy in our retirement lists, we may have
137 * a stray pin (preventing eviction) that can only be resolved by
140 if (!(flags & PIN_NONBLOCK))
141 i915_retire_requests(dev_priv);
145 INIT_LIST_HEAD(&eviction_list);
146 list_for_each_entry_safe(vma, next, &vm->bound_list, vm_link) {
148 * We keep this list in a rough least-recently scanned order
149 * of active elements (inactive elements are cheap to reap).
150 * New entries are added to the end, and we move anything we
151 * scan to the end. The assumption is that the working set
152 * of applications is either steady state (and thanks to the
153 * userspace bo cache it almost always is) or volatile and
154 * frequently replaced after a frame, which are self-evicting!
155 * Given that assumption, the MRU order of the scan list is
156 * fairly static, and keeping it in least-recently scan order
159 * To notice when we complete one full cycle, we record the
160 * first active element seen, before moving it to the tail.
162 if (i915_vma_is_active(vma)) {
164 if (flags & PIN_NONBLOCK)
167 active = ERR_PTR(-EAGAIN);
170 if (active != ERR_PTR(-EAGAIN)) {
174 list_move_tail(&vma->vm_link, &vm->bound_list);
179 if (mark_free(&scan, vma, flags, &eviction_list))
183 /* Nothing found, clean up and bail out! */
184 list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
185 ret = drm_mm_scan_remove_block(&scan, &vma->node);
190 * Can we unpin some objects such as idle hw contents,
191 * or pending flips? But since only the GGTT has global entries
192 * such as scanouts, rinbuffers and contexts, we can skip the
193 * purge when inspecting per-process local address spaces.
195 if (!i915_is_ggtt(vm) || flags & PIN_NONBLOCK)
199 * Not everything in the GGTT is tracked via VMA using
200 * i915_vma_move_to_active(), otherwise we could evict as required
201 * with minimal stalling. Instead we are forced to idle the GPU and
202 * explicitly retire outstanding requests which will then remove
203 * the pinning for active objects such as contexts and ring,
204 * enabling us to evict them on the next iteration.
206 * To ensure that all user contexts are evictable, we perform
207 * a switch to the perma-pinned kernel context. This all also gives
208 * us a termination condition, when the last retired context is
209 * the kernel's there is no more we can evict.
211 if (I915_SELFTEST_ONLY(igt_evict_ctl.fail_if_busy))
214 ret = ggtt_flush(dev_priv);
220 flags |= PIN_NONBLOCK;
224 /* drm_mm doesn't allow any other other operations while
225 * scanning, therefore store to-be-evicted objects on a
226 * temporary list and take a reference for all before
227 * calling unbind (which may remove the active reference
228 * of any of our objects, thus corrupting the list).
230 list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
231 if (drm_mm_scan_remove_block(&scan, &vma->node))
234 list_del(&vma->evict_link);
237 /* Unbinding will emit any required flushes */
239 list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
240 __i915_vma_unpin(vma);
242 ret = i915_vma_unbind(vma);
245 while (ret == 0 && (node = drm_mm_scan_color_evict(&scan))) {
246 vma = container_of(node, struct i915_vma, node);
247 ret = i915_vma_unbind(vma);
254 * i915_gem_evict_for_vma - Evict vmas to make room for binding a new one
255 * @vm: address space to evict from
256 * @target: range (and color) to evict for
257 * @flags: additional flags to control the eviction algorithm
259 * This function will try to evict vmas that overlap the target node.
261 * To clarify: This is for freeing up virtual address space, not for freeing
262 * memory in e.g. the shrinker.
264 int i915_gem_evict_for_node(struct i915_address_space *vm,
265 struct drm_mm_node *target,
268 LIST_HEAD(eviction_list);
269 struct drm_mm_node *node;
270 u64 start = target->start;
271 u64 end = start + target->size;
272 struct i915_vma *vma, *next;
276 lockdep_assert_held(&vm->i915->drm.struct_mutex);
277 GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
278 GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
280 trace_i915_gem_evict_node(vm, target, flags);
282 /* Retire before we search the active list. Although we have
283 * reasonable accuracy in our retirement lists, we may have
284 * a stray pin (preventing eviction) that can only be resolved by
287 if (!(flags & PIN_NONBLOCK))
288 i915_retire_requests(vm->i915);
290 check_color = vm->mm.color_adjust;
292 /* Expand search to cover neighbouring guard pages (or lack!) */
294 start -= I915_GTT_PAGE_SIZE;
296 /* Always look at the page afterwards to avoid the end-of-GTT */
297 end += I915_GTT_PAGE_SIZE;
299 GEM_BUG_ON(start >= end);
301 drm_mm_for_each_node_in_range(node, &vm->mm, start, end) {
302 /* If we find any non-objects (!vma), we cannot evict them */
303 if (node->color == I915_COLOR_UNEVICTABLE) {
308 GEM_BUG_ON(!node->allocated);
309 vma = container_of(node, typeof(*vma), node);
311 /* If we are using coloring to insert guard pages between
312 * different cache domains within the address space, we have
313 * to check whether the objects on either side of our range
314 * abutt and conflict. If they are in conflict, then we evict
315 * those as well to make room for our guard pages.
318 if (node->start + node->size == target->start) {
319 if (node->color == target->color)
322 if (node->start == target->start + target->size) {
323 if (node->color == target->color)
328 if (flags & PIN_NONBLOCK &&
329 (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))) {
334 if (flags & PIN_NONFAULT && i915_vma_has_userfault(vma)) {
339 /* Overlap of objects in the same batch? */
340 if (i915_vma_is_pinned(vma)) {
342 if (vma->exec_flags &&
343 *vma->exec_flags & EXEC_OBJECT_PINNED)
348 /* Never show fear in the face of dragons!
350 * We cannot directly remove this node from within this
351 * iterator and as with i915_gem_evict_something() we employ
352 * the vma pin_count in order to prevent the action of
353 * unbinding one vma from freeing (by dropping its active
354 * reference) another in our eviction list.
357 list_add(&vma->evict_link, &eviction_list);
360 list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
361 __i915_vma_unpin(vma);
363 ret = i915_vma_unbind(vma);
370 * i915_gem_evict_vm - Evict all idle vmas from a vm
371 * @vm: Address space to cleanse
373 * This function evicts all vmas from a vm.
375 * This is used by the execbuf code as a last-ditch effort to defragment the
378 * To clarify: This is for freeing up virtual address space, not for freeing
379 * memory in e.g. the shrinker.
381 int i915_gem_evict_vm(struct i915_address_space *vm)
383 struct list_head eviction_list;
384 struct i915_vma *vma, *next;
387 lockdep_assert_held(&vm->i915->drm.struct_mutex);
388 trace_i915_gem_evict_vm(vm);
390 /* Switch back to the default context in order to unpin
391 * the existing context objects. However, such objects only
392 * pin themselves inside the global GTT and performing the
393 * switch otherwise is ineffective.
395 if (i915_is_ggtt(vm)) {
396 ret = ggtt_flush(vm->i915);
401 INIT_LIST_HEAD(&eviction_list);
402 mutex_lock(&vm->mutex);
403 list_for_each_entry(vma, &vm->bound_list, vm_link) {
404 if (i915_vma_is_pinned(vma))
408 list_add(&vma->evict_link, &eviction_list);
410 mutex_unlock(&vm->mutex);
413 list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
414 __i915_vma_unpin(vma);
416 ret = i915_vma_unbind(vma);
421 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
422 #include "selftests/i915_gem_evict.c"