1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright © 2008-2010 Intel Corporation
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun * Software.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18*4882a593Smuzhiyun * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*4882a593Smuzhiyun * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21*4882a593Smuzhiyun * IN THE SOFTWARE.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Authors:
24*4882a593Smuzhiyun * Eric Anholt <eric@anholt.net>
25*4882a593Smuzhiyun * Chris Wilson <chris@chris-wilson.co.uuk>
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include "gem/i915_gem_context.h"
30*4882a593Smuzhiyun #include "gt/intel_gt_requests.h"
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #include "i915_drv.h"
33*4882a593Smuzhiyun #include "i915_trace.h"
34*4882a593Smuzhiyun
I915_SELFTEST_DECLARE(static struct igt_evict_ctl{ bool fail_if_busy:1; } igt_evict_ctl;)35*4882a593Smuzhiyun I915_SELFTEST_DECLARE(static struct igt_evict_ctl {
36*4882a593Smuzhiyun bool fail_if_busy:1;
37*4882a593Smuzhiyun } igt_evict_ctl;)
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun static int ggtt_flush(struct intel_gt *gt)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun * Not everything in the GGTT is tracked via vma (otherwise we
43*4882a593Smuzhiyun * could evict as required with minimal stalling) so we are forced
44*4882a593Smuzhiyun * to idle the GPU and explicitly retire outstanding requests in
45*4882a593Smuzhiyun * the hopes that we can then remove contexts and the like only
46*4882a593Smuzhiyun * bound by their active reference.
47*4882a593Smuzhiyun */
48*4882a593Smuzhiyun return intel_gt_wait_for_idle(gt, MAX_SCHEDULE_TIMEOUT);
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun static bool
mark_free(struct drm_mm_scan * scan,struct i915_vma * vma,unsigned int flags,struct list_head * unwind)52*4882a593Smuzhiyun mark_free(struct drm_mm_scan *scan,
53*4882a593Smuzhiyun struct i915_vma *vma,
54*4882a593Smuzhiyun unsigned int flags,
55*4882a593Smuzhiyun struct list_head *unwind)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun if (i915_vma_is_pinned(vma))
58*4882a593Smuzhiyun return false;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun list_add(&vma->evict_link, unwind);
61*4882a593Smuzhiyun return drm_mm_scan_add_block(scan, &vma->node);
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /**
65*4882a593Smuzhiyun * i915_gem_evict_something - Evict vmas to make room for binding a new one
66*4882a593Smuzhiyun * @vm: address space to evict from
67*4882a593Smuzhiyun * @min_size: size of the desired free space
68*4882a593Smuzhiyun * @alignment: alignment constraint of the desired free space
69*4882a593Smuzhiyun * @color: color for the desired space
70*4882a593Smuzhiyun * @start: start (inclusive) of the range from which to evict objects
71*4882a593Smuzhiyun * @end: end (exclusive) of the range from which to evict objects
72*4882a593Smuzhiyun * @flags: additional flags to control the eviction algorithm
73*4882a593Smuzhiyun *
74*4882a593Smuzhiyun * This function will try to evict vmas until a free space satisfying the
75*4882a593Smuzhiyun * requirements is found. Callers must check first whether any such hole exists
76*4882a593Smuzhiyun * already before calling this function.
77*4882a593Smuzhiyun *
78*4882a593Smuzhiyun * This function is used by the object/vma binding code.
79*4882a593Smuzhiyun *
80*4882a593Smuzhiyun * Since this function is only used to free up virtual address space it only
81*4882a593Smuzhiyun * ignores pinned vmas, and not object where the backing storage itself is
82*4882a593Smuzhiyun * pinned. Hence obj->pages_pin_count does not protect against eviction.
83*4882a593Smuzhiyun *
84*4882a593Smuzhiyun * To clarify: This is for freeing up virtual address space, not for freeing
85*4882a593Smuzhiyun * memory in e.g. the shrinker.
86*4882a593Smuzhiyun */
87*4882a593Smuzhiyun int
i915_gem_evict_something(struct i915_address_space * vm,u64 min_size,u64 alignment,unsigned long color,u64 start,u64 end,unsigned flags)88*4882a593Smuzhiyun i915_gem_evict_something(struct i915_address_space *vm,
89*4882a593Smuzhiyun u64 min_size, u64 alignment,
90*4882a593Smuzhiyun unsigned long color,
91*4882a593Smuzhiyun u64 start, u64 end,
92*4882a593Smuzhiyun unsigned flags)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun struct drm_mm_scan scan;
95*4882a593Smuzhiyun struct list_head eviction_list;
96*4882a593Smuzhiyun struct i915_vma *vma, *next;
97*4882a593Smuzhiyun struct drm_mm_node *node;
98*4882a593Smuzhiyun enum drm_mm_insert_mode mode;
99*4882a593Smuzhiyun struct i915_vma *active;
100*4882a593Smuzhiyun int ret;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun lockdep_assert_held(&vm->mutex);
103*4882a593Smuzhiyun trace_i915_gem_evict(vm, min_size, alignment, flags);
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun * The goal is to evict objects and amalgamate space in rough LRU order.
107*4882a593Smuzhiyun * Since both active and inactive objects reside on the same list,
108*4882a593Smuzhiyun * in a mix of creation and last scanned order, as we process the list
109*4882a593Smuzhiyun * we sort it into inactive/active, which keeps the active portion
110*4882a593Smuzhiyun * in a rough MRU order.
111*4882a593Smuzhiyun *
112*4882a593Smuzhiyun * The retirement sequence is thus:
113*4882a593Smuzhiyun * 1. Inactive objects (already retired, random order)
114*4882a593Smuzhiyun * 2. Active objects (will stall on unbinding, oldest scanned first)
115*4882a593Smuzhiyun */
116*4882a593Smuzhiyun mode = DRM_MM_INSERT_BEST;
117*4882a593Smuzhiyun if (flags & PIN_HIGH)
118*4882a593Smuzhiyun mode = DRM_MM_INSERT_HIGH;
119*4882a593Smuzhiyun if (flags & PIN_MAPPABLE)
120*4882a593Smuzhiyun mode = DRM_MM_INSERT_LOW;
121*4882a593Smuzhiyun drm_mm_scan_init_with_range(&scan, &vm->mm,
122*4882a593Smuzhiyun min_size, alignment, color,
123*4882a593Smuzhiyun start, end, mode);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun intel_gt_retire_requests(vm->gt);
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun search_again:
128*4882a593Smuzhiyun active = NULL;
129*4882a593Smuzhiyun INIT_LIST_HEAD(&eviction_list);
130*4882a593Smuzhiyun list_for_each_entry_safe(vma, next, &vm->bound_list, vm_link) {
131*4882a593Smuzhiyun if (vma == active) { /* now seen this vma twice */
132*4882a593Smuzhiyun if (flags & PIN_NONBLOCK)
133*4882a593Smuzhiyun break;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun active = ERR_PTR(-EAGAIN);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /*
139*4882a593Smuzhiyun * We keep this list in a rough least-recently scanned order
140*4882a593Smuzhiyun * of active elements (inactive elements are cheap to reap).
141*4882a593Smuzhiyun * New entries are added to the end, and we move anything we
142*4882a593Smuzhiyun * scan to the end. The assumption is that the working set
143*4882a593Smuzhiyun * of applications is either steady state (and thanks to the
144*4882a593Smuzhiyun * userspace bo cache it almost always is) or volatile and
145*4882a593Smuzhiyun * frequently replaced after a frame, which are self-evicting!
146*4882a593Smuzhiyun * Given that assumption, the MRU order of the scan list is
147*4882a593Smuzhiyun * fairly static, and keeping it in least-recently scan order
148*4882a593Smuzhiyun * is suitable.
149*4882a593Smuzhiyun *
150*4882a593Smuzhiyun * To notice when we complete one full cycle, we record the
151*4882a593Smuzhiyun * first active element seen, before moving it to the tail.
152*4882a593Smuzhiyun */
153*4882a593Smuzhiyun if (active != ERR_PTR(-EAGAIN) && i915_vma_is_active(vma)) {
154*4882a593Smuzhiyun if (!active)
155*4882a593Smuzhiyun active = vma;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun list_move_tail(&vma->vm_link, &vm->bound_list);
158*4882a593Smuzhiyun continue;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun if (mark_free(&scan, vma, flags, &eviction_list))
162*4882a593Smuzhiyun goto found;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /* Nothing found, clean up and bail out! */
166*4882a593Smuzhiyun list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
167*4882a593Smuzhiyun ret = drm_mm_scan_remove_block(&scan, &vma->node);
168*4882a593Smuzhiyun BUG_ON(ret);
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun /*
172*4882a593Smuzhiyun * Can we unpin some objects such as idle hw contents,
173*4882a593Smuzhiyun * or pending flips? But since only the GGTT has global entries
174*4882a593Smuzhiyun * such as scanouts, rinbuffers and contexts, we can skip the
175*4882a593Smuzhiyun * purge when inspecting per-process local address spaces.
176*4882a593Smuzhiyun */
177*4882a593Smuzhiyun if (!i915_is_ggtt(vm) || flags & PIN_NONBLOCK)
178*4882a593Smuzhiyun return -ENOSPC;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /*
181*4882a593Smuzhiyun * Not everything in the GGTT is tracked via VMA using
182*4882a593Smuzhiyun * i915_vma_move_to_active(), otherwise we could evict as required
183*4882a593Smuzhiyun * with minimal stalling. Instead we are forced to idle the GPU and
184*4882a593Smuzhiyun * explicitly retire outstanding requests which will then remove
185*4882a593Smuzhiyun * the pinning for active objects such as contexts and ring,
186*4882a593Smuzhiyun * enabling us to evict them on the next iteration.
187*4882a593Smuzhiyun *
188*4882a593Smuzhiyun * To ensure that all user contexts are evictable, we perform
189*4882a593Smuzhiyun * a switch to the perma-pinned kernel context. This all also gives
190*4882a593Smuzhiyun * us a termination condition, when the last retired context is
191*4882a593Smuzhiyun * the kernel's there is no more we can evict.
192*4882a593Smuzhiyun */
193*4882a593Smuzhiyun if (I915_SELFTEST_ONLY(igt_evict_ctl.fail_if_busy))
194*4882a593Smuzhiyun return -EBUSY;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun ret = ggtt_flush(vm->gt);
197*4882a593Smuzhiyun if (ret)
198*4882a593Smuzhiyun return ret;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun cond_resched();
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun flags |= PIN_NONBLOCK;
203*4882a593Smuzhiyun goto search_again;
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun found:
206*4882a593Smuzhiyun /* drm_mm doesn't allow any other other operations while
207*4882a593Smuzhiyun * scanning, therefore store to-be-evicted objects on a
208*4882a593Smuzhiyun * temporary list and take a reference for all before
209*4882a593Smuzhiyun * calling unbind (which may remove the active reference
210*4882a593Smuzhiyun * of any of our objects, thus corrupting the list).
211*4882a593Smuzhiyun */
212*4882a593Smuzhiyun list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
213*4882a593Smuzhiyun if (drm_mm_scan_remove_block(&scan, &vma->node))
214*4882a593Smuzhiyun __i915_vma_pin(vma);
215*4882a593Smuzhiyun else
216*4882a593Smuzhiyun list_del(&vma->evict_link);
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun /* Unbinding will emit any required flushes */
220*4882a593Smuzhiyun ret = 0;
221*4882a593Smuzhiyun list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
222*4882a593Smuzhiyun __i915_vma_unpin(vma);
223*4882a593Smuzhiyun if (ret == 0)
224*4882a593Smuzhiyun ret = __i915_vma_unbind(vma);
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun while (ret == 0 && (node = drm_mm_scan_color_evict(&scan))) {
228*4882a593Smuzhiyun vma = container_of(node, struct i915_vma, node);
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /* If we find any non-objects (!vma), we cannot evict them */
231*4882a593Smuzhiyun if (vma->node.color != I915_COLOR_UNEVICTABLE)
232*4882a593Smuzhiyun ret = __i915_vma_unbind(vma);
233*4882a593Smuzhiyun else
234*4882a593Smuzhiyun ret = -ENOSPC; /* XXX search failed, try again? */
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun return ret;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun /**
241*4882a593Smuzhiyun * i915_gem_evict_for_vma - Evict vmas to make room for binding a new one
242*4882a593Smuzhiyun * @vm: address space to evict from
243*4882a593Smuzhiyun * @target: range (and color) to evict for
244*4882a593Smuzhiyun * @flags: additional flags to control the eviction algorithm
245*4882a593Smuzhiyun *
246*4882a593Smuzhiyun * This function will try to evict vmas that overlap the target node.
247*4882a593Smuzhiyun *
248*4882a593Smuzhiyun * To clarify: This is for freeing up virtual address space, not for freeing
249*4882a593Smuzhiyun * memory in e.g. the shrinker.
250*4882a593Smuzhiyun */
i915_gem_evict_for_node(struct i915_address_space * vm,struct drm_mm_node * target,unsigned int flags)251*4882a593Smuzhiyun int i915_gem_evict_for_node(struct i915_address_space *vm,
252*4882a593Smuzhiyun struct drm_mm_node *target,
253*4882a593Smuzhiyun unsigned int flags)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun LIST_HEAD(eviction_list);
256*4882a593Smuzhiyun struct drm_mm_node *node;
257*4882a593Smuzhiyun u64 start = target->start;
258*4882a593Smuzhiyun u64 end = start + target->size;
259*4882a593Smuzhiyun struct i915_vma *vma, *next;
260*4882a593Smuzhiyun int ret = 0;
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun lockdep_assert_held(&vm->mutex);
263*4882a593Smuzhiyun GEM_BUG_ON(!IS_ALIGNED(start, I915_GTT_PAGE_SIZE));
264*4882a593Smuzhiyun GEM_BUG_ON(!IS_ALIGNED(end, I915_GTT_PAGE_SIZE));
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun trace_i915_gem_evict_node(vm, target, flags);
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun /*
269*4882a593Smuzhiyun * Retire before we search the active list. Although we have
270*4882a593Smuzhiyun * reasonable accuracy in our retirement lists, we may have
271*4882a593Smuzhiyun * a stray pin (preventing eviction) that can only be resolved by
272*4882a593Smuzhiyun * retiring.
273*4882a593Smuzhiyun */
274*4882a593Smuzhiyun intel_gt_retire_requests(vm->gt);
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun if (i915_vm_has_cache_coloring(vm)) {
277*4882a593Smuzhiyun /* Expand search to cover neighbouring guard pages (or lack!) */
278*4882a593Smuzhiyun if (start)
279*4882a593Smuzhiyun start -= I915_GTT_PAGE_SIZE;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /* Always look at the page afterwards to avoid the end-of-GTT */
282*4882a593Smuzhiyun end += I915_GTT_PAGE_SIZE;
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun GEM_BUG_ON(start >= end);
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun drm_mm_for_each_node_in_range(node, &vm->mm, start, end) {
287*4882a593Smuzhiyun /* If we find any non-objects (!vma), we cannot evict them */
288*4882a593Smuzhiyun if (node->color == I915_COLOR_UNEVICTABLE) {
289*4882a593Smuzhiyun ret = -ENOSPC;
290*4882a593Smuzhiyun break;
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun GEM_BUG_ON(!drm_mm_node_allocated(node));
294*4882a593Smuzhiyun vma = container_of(node, typeof(*vma), node);
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun /*
297*4882a593Smuzhiyun * If we are using coloring to insert guard pages between
298*4882a593Smuzhiyun * different cache domains within the address space, we have
299*4882a593Smuzhiyun * to check whether the objects on either side of our range
300*4882a593Smuzhiyun * abutt and conflict. If they are in conflict, then we evict
301*4882a593Smuzhiyun * those as well to make room for our guard pages.
302*4882a593Smuzhiyun */
303*4882a593Smuzhiyun if (i915_vm_has_cache_coloring(vm)) {
304*4882a593Smuzhiyun if (node->start + node->size == target->start) {
305*4882a593Smuzhiyun if (node->color == target->color)
306*4882a593Smuzhiyun continue;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun if (node->start == target->start + target->size) {
309*4882a593Smuzhiyun if (node->color == target->color)
310*4882a593Smuzhiyun continue;
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun if (i915_vma_is_pinned(vma)) {
315*4882a593Smuzhiyun ret = -ENOSPC;
316*4882a593Smuzhiyun break;
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun if (flags & PIN_NONBLOCK && i915_vma_is_active(vma)) {
320*4882a593Smuzhiyun ret = -ENOSPC;
321*4882a593Smuzhiyun break;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun /*
325*4882a593Smuzhiyun * Never show fear in the face of dragons!
326*4882a593Smuzhiyun *
327*4882a593Smuzhiyun * We cannot directly remove this node from within this
328*4882a593Smuzhiyun * iterator and as with i915_gem_evict_something() we employ
329*4882a593Smuzhiyun * the vma pin_count in order to prevent the action of
330*4882a593Smuzhiyun * unbinding one vma from freeing (by dropping its active
331*4882a593Smuzhiyun * reference) another in our eviction list.
332*4882a593Smuzhiyun */
333*4882a593Smuzhiyun __i915_vma_pin(vma);
334*4882a593Smuzhiyun list_add(&vma->evict_link, &eviction_list);
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun list_for_each_entry_safe(vma, next, &eviction_list, evict_link) {
338*4882a593Smuzhiyun __i915_vma_unpin(vma);
339*4882a593Smuzhiyun if (ret == 0)
340*4882a593Smuzhiyun ret = __i915_vma_unbind(vma);
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun return ret;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun /**
347*4882a593Smuzhiyun * i915_gem_evict_vm - Evict all idle vmas from a vm
348*4882a593Smuzhiyun * @vm: Address space to cleanse
349*4882a593Smuzhiyun *
350*4882a593Smuzhiyun * This function evicts all vmas from a vm.
351*4882a593Smuzhiyun *
352*4882a593Smuzhiyun * This is used by the execbuf code as a last-ditch effort to defragment the
353*4882a593Smuzhiyun * address space.
354*4882a593Smuzhiyun *
355*4882a593Smuzhiyun * To clarify: This is for freeing up virtual address space, not for freeing
356*4882a593Smuzhiyun * memory in e.g. the shrinker.
357*4882a593Smuzhiyun */
i915_gem_evict_vm(struct i915_address_space * vm)358*4882a593Smuzhiyun int i915_gem_evict_vm(struct i915_address_space *vm)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun int ret = 0;
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun lockdep_assert_held(&vm->mutex);
363*4882a593Smuzhiyun trace_i915_gem_evict_vm(vm);
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun /* Switch back to the default context in order to unpin
366*4882a593Smuzhiyun * the existing context objects. However, such objects only
367*4882a593Smuzhiyun * pin themselves inside the global GTT and performing the
368*4882a593Smuzhiyun * switch otherwise is ineffective.
369*4882a593Smuzhiyun */
370*4882a593Smuzhiyun if (i915_is_ggtt(vm)) {
371*4882a593Smuzhiyun ret = ggtt_flush(vm->gt);
372*4882a593Smuzhiyun if (ret)
373*4882a593Smuzhiyun return ret;
374*4882a593Smuzhiyun }
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun do {
377*4882a593Smuzhiyun struct i915_vma *vma, *vn;
378*4882a593Smuzhiyun LIST_HEAD(eviction_list);
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun list_for_each_entry(vma, &vm->bound_list, vm_link) {
381*4882a593Smuzhiyun if (i915_vma_is_pinned(vma))
382*4882a593Smuzhiyun continue;
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun __i915_vma_pin(vma);
385*4882a593Smuzhiyun list_add(&vma->evict_link, &eviction_list);
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun if (list_empty(&eviction_list))
388*4882a593Smuzhiyun break;
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun ret = 0;
391*4882a593Smuzhiyun list_for_each_entry_safe(vma, vn, &eviction_list, evict_link) {
392*4882a593Smuzhiyun __i915_vma_unpin(vma);
393*4882a593Smuzhiyun if (ret == 0)
394*4882a593Smuzhiyun ret = __i915_vma_unbind(vma);
395*4882a593Smuzhiyun if (ret != -EINTR) /* "Get me out of here!" */
396*4882a593Smuzhiyun ret = 0;
397*4882a593Smuzhiyun }
398*4882a593Smuzhiyun } while (ret == 0);
399*4882a593Smuzhiyun
400*4882a593Smuzhiyun return ret;
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
404*4882a593Smuzhiyun #include "selftests/i915_gem_evict.c"
405*4882a593Smuzhiyun #endif
406