xref: /dragonfly/sys/dev/drm/i915/i915_gem_evict.c (revision 2c9916cd)
1e3adcf8fSFrançois Tigeot /*
2e3adcf8fSFrançois Tigeot  * Copyright © 2008-2010 Intel Corporation
3e3adcf8fSFrançois Tigeot  *
4e3adcf8fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5e3adcf8fSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6e3adcf8fSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7e3adcf8fSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e3adcf8fSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9e3adcf8fSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10e3adcf8fSFrançois Tigeot  *
11e3adcf8fSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
12e3adcf8fSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
13e3adcf8fSFrançois Tigeot  * Software.
14e3adcf8fSFrançois Tigeot  *
15e3adcf8fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16e3adcf8fSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17e3adcf8fSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18e3adcf8fSFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19e3adcf8fSFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20e3adcf8fSFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21e3adcf8fSFrançois Tigeot  * IN THE SOFTWARE.
22e3adcf8fSFrançois Tigeot  *
23e3adcf8fSFrançois Tigeot  * Authors:
24e3adcf8fSFrançois Tigeot  *    Eric Anholt <eric@anholt.net>
25e3adcf8fSFrançois Tigeot  *    Chris Wilson <chris@chris-wilson.co.uuk>
26e3adcf8fSFrançois Tigeot  *
27e3adcf8fSFrançois Tigeot  */
28e3adcf8fSFrançois Tigeot 
2918e26a6dSFrançois Tigeot #include <drm/drmP.h>
30a2fdbec6SFrançois Tigeot #include <drm/i915_drm.h>
319edbd4a0SFrançois Tigeot 
329edbd4a0SFrançois Tigeot #include "i915_drv.h"
339edbd4a0SFrançois Tigeot #include "intel_drv.h"
34a2fdbec6SFrançois Tigeot #include "i915_trace.h"
35e3adcf8fSFrançois Tigeot 
36e3adcf8fSFrançois Tigeot static bool
379edbd4a0SFrançois Tigeot mark_free(struct i915_vma *vma, struct list_head *unwind)
38e3adcf8fSFrançois Tigeot {
39ba55f2f5SFrançois Tigeot 	if (vma->pin_count)
40e9243325SFrançois Tigeot 		return false;
41e9243325SFrançois Tigeot 
429edbd4a0SFrançois Tigeot 	if (WARN_ON(!list_empty(&vma->exec_list)))
439edbd4a0SFrançois Tigeot 		return false;
449edbd4a0SFrançois Tigeot 
459edbd4a0SFrançois Tigeot 	list_add(&vma->exec_list, unwind);
469edbd4a0SFrançois Tigeot 	return drm_mm_scan_add_block(&vma->node);
47e3adcf8fSFrançois Tigeot }
48e3adcf8fSFrançois Tigeot 
49ba55f2f5SFrançois Tigeot /**
50ba55f2f5SFrançois Tigeot  * i915_gem_evict_something - Evict vmas to make room for binding a new one
51ba55f2f5SFrançois Tigeot  * @dev: drm_device
52ba55f2f5SFrançois Tigeot  * @vm: address space to evict from
53*2c9916cdSFrançois Tigeot  * @min_size: size of the desired free space
54ba55f2f5SFrançois Tigeot  * @alignment: alignment constraint of the desired free space
55ba55f2f5SFrançois Tigeot  * @cache_level: cache_level for the desired space
56*2c9916cdSFrançois Tigeot  * @start: start (inclusive) of the range from which to evict objects
57*2c9916cdSFrançois Tigeot  * @end: end (exclusive) of the range from which to evict objects
58*2c9916cdSFrançois Tigeot  * @flags: additional flags to control the eviction algorithm
59ba55f2f5SFrançois Tigeot  *
60ba55f2f5SFrançois Tigeot  * This function will try to evict vmas until a free space satisfying the
61ba55f2f5SFrançois Tigeot  * requirements is found. Callers must check first whether any such hole exists
62ba55f2f5SFrançois Tigeot  * already before calling this function.
63ba55f2f5SFrançois Tigeot  *
64ba55f2f5SFrançois Tigeot  * This function is used by the object/vma binding code.
65ba55f2f5SFrançois Tigeot  *
66ba55f2f5SFrançois Tigeot  * To clarify: This is for freeing up virtual address space, not for freeing
67ba55f2f5SFrançois Tigeot  * memory in e.g. the shrinker.
68ba55f2f5SFrançois Tigeot  */
69e3adcf8fSFrançois Tigeot int
709edbd4a0SFrançois Tigeot i915_gem_evict_something(struct drm_device *dev, struct i915_address_space *vm,
719edbd4a0SFrançois Tigeot 			 int min_size, unsigned alignment, unsigned cache_level,
72ba55f2f5SFrançois Tigeot 			 unsigned long start, unsigned long end,
73ba55f2f5SFrançois Tigeot 			 unsigned flags)
74e3adcf8fSFrançois Tigeot {
75e3adcf8fSFrançois Tigeot 	struct list_head eviction_list, unwind_list;
769edbd4a0SFrançois Tigeot 	struct i915_vma *vma;
77e3adcf8fSFrançois Tigeot 	int ret = 0;
789edbd4a0SFrançois Tigeot 	int pass = 0;
79e3adcf8fSFrançois Tigeot 
80ba55f2f5SFrançois Tigeot 	trace_i915_gem_evict(dev, min_size, alignment, flags);
81a2fdbec6SFrançois Tigeot 
82e3adcf8fSFrançois Tigeot 	/*
83e3adcf8fSFrançois Tigeot 	 * The goal is to evict objects and amalgamate space in LRU order.
84e3adcf8fSFrançois Tigeot 	 * The oldest idle objects reside on the inactive list, which is in
85e3adcf8fSFrançois Tigeot 	 * retirement order. The next objects to retire are those on the (per
86e3adcf8fSFrançois Tigeot 	 * ring) active list that do not have an outstanding flush. Once the
87e3adcf8fSFrançois Tigeot 	 * hardware reports completion (the seqno is updated after the
88e3adcf8fSFrançois Tigeot 	 * batchbuffer has been finished) the clean buffer objects would
89e3adcf8fSFrançois Tigeot 	 * be retired to the inactive list. Any dirty objects would be added
90e3adcf8fSFrançois Tigeot 	 * to the tail of the flushing list. So after processing the clean
91e3adcf8fSFrançois Tigeot 	 * active objects we need to emit a MI_FLUSH to retire the flushing
92e3adcf8fSFrançois Tigeot 	 * list, hence the retirement order of the flushing list is in
93e3adcf8fSFrançois Tigeot 	 * advance of the dirty objects on the active lists.
94e3adcf8fSFrançois Tigeot 	 *
95e3adcf8fSFrançois Tigeot 	 * The retirement sequence is thus:
96e3adcf8fSFrançois Tigeot 	 *   1. Inactive objects (already retired)
97e3adcf8fSFrançois Tigeot 	 *   2. Clean active objects
98e3adcf8fSFrançois Tigeot 	 *   3. Flushing list
99e3adcf8fSFrançois Tigeot 	 *   4. Dirty active objects.
100e3adcf8fSFrançois Tigeot 	 *
101e3adcf8fSFrançois Tigeot 	 * On each list, the oldest objects lie at the HEAD with the freshest
102e3adcf8fSFrançois Tigeot 	 * object on the TAIL.
103e3adcf8fSFrançois Tigeot 	 */
104e3adcf8fSFrançois Tigeot 
105e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&unwind_list);
106ba55f2f5SFrançois Tigeot 	if (start != 0 || end != vm->total) {
1079edbd4a0SFrançois Tigeot 		drm_mm_init_scan_with_range(&vm->mm, min_size,
108ba55f2f5SFrançois Tigeot 					    alignment, cache_level,
109ba55f2f5SFrançois Tigeot 					    start, end);
1109edbd4a0SFrançois Tigeot 	} else
1119edbd4a0SFrançois Tigeot 		drm_mm_init_scan(&vm->mm, min_size, alignment, cache_level);
112e3adcf8fSFrançois Tigeot 
1139edbd4a0SFrançois Tigeot search_again:
114e3adcf8fSFrançois Tigeot 	/* First see if there is a large enough contiguous idle region... */
1159edbd4a0SFrançois Tigeot 	list_for_each_entry(vma, &vm->inactive_list, mm_list) {
1169edbd4a0SFrançois Tigeot 		if (mark_free(vma, &unwind_list))
117e3adcf8fSFrançois Tigeot 			goto found;
118e3adcf8fSFrançois Tigeot 	}
119e3adcf8fSFrançois Tigeot 
120ba55f2f5SFrançois Tigeot 	if (flags & PIN_NONBLOCK)
1219f16360bSFrançois Tigeot 		goto none;
1229f16360bSFrançois Tigeot 
123e3adcf8fSFrançois Tigeot 	/* Now merge in the soon-to-be-expired objects... */
1249edbd4a0SFrançois Tigeot 	list_for_each_entry(vma, &vm->active_list, mm_list) {
1259edbd4a0SFrançois Tigeot 		if (mark_free(vma, &unwind_list))
126e3adcf8fSFrançois Tigeot 			goto found;
127e3adcf8fSFrançois Tigeot 	}
128e3adcf8fSFrançois Tigeot 
1299f16360bSFrançois Tigeot none:
130e3adcf8fSFrançois Tigeot 	/* Nothing found, clean up and bail out! */
131e3adcf8fSFrançois Tigeot 	while (!list_empty(&unwind_list)) {
1329edbd4a0SFrançois Tigeot 		vma = list_first_entry(&unwind_list,
1339edbd4a0SFrançois Tigeot 				       struct i915_vma,
134e3adcf8fSFrançois Tigeot 				       exec_list);
1359edbd4a0SFrançois Tigeot 		ret = drm_mm_scan_remove_block(&vma->node);
136f192107fSFrançois Tigeot 		BUG_ON(ret);
137e3adcf8fSFrançois Tigeot 
1389edbd4a0SFrançois Tigeot 		list_del_init(&vma->exec_list);
139e3adcf8fSFrançois Tigeot 	}
140e3adcf8fSFrançois Tigeot 
1419edbd4a0SFrançois Tigeot 	/* Can we unpin some objects such as idle hw contents,
1429edbd4a0SFrançois Tigeot 	 * or pending flips?
143e3adcf8fSFrançois Tigeot 	 */
144ba55f2f5SFrançois Tigeot 	if (flags & PIN_NONBLOCK)
145e3adcf8fSFrançois Tigeot 		return -ENOSPC;
146e3adcf8fSFrançois Tigeot 
1479edbd4a0SFrançois Tigeot 	/* Only idle the GPU and repeat the search once */
1489edbd4a0SFrançois Tigeot 	if (pass++ == 0) {
1499edbd4a0SFrançois Tigeot 		ret = i915_gpu_idle(dev);
1509edbd4a0SFrançois Tigeot 		if (ret)
1519edbd4a0SFrançois Tigeot 			return ret;
1529edbd4a0SFrançois Tigeot 
1539edbd4a0SFrançois Tigeot 		i915_gem_retire_requests(dev);
1549edbd4a0SFrançois Tigeot 		goto search_again;
1559edbd4a0SFrançois Tigeot 	}
1569edbd4a0SFrançois Tigeot 
1579edbd4a0SFrançois Tigeot 	/* If we still have pending pageflip completions, drop
1589edbd4a0SFrançois Tigeot 	 * back to userspace to give our workqueues time to
1599edbd4a0SFrançois Tigeot 	 * acquire our locks and unpin the old scanouts.
1609edbd4a0SFrançois Tigeot 	 */
1619edbd4a0SFrançois Tigeot 	return intel_has_pending_fb_unpin(dev) ? -EAGAIN : -ENOSPC;
1629edbd4a0SFrançois Tigeot 
163e3adcf8fSFrançois Tigeot found:
164e3adcf8fSFrançois Tigeot 	/* drm_mm doesn't allow any other other operations while
165e3adcf8fSFrançois Tigeot 	 * scanning, therefore store to be evicted objects on a
166e3adcf8fSFrançois Tigeot 	 * temporary list. */
167e3adcf8fSFrançois Tigeot 	INIT_LIST_HEAD(&eviction_list);
168e3adcf8fSFrançois Tigeot 	while (!list_empty(&unwind_list)) {
1699edbd4a0SFrançois Tigeot 		vma = list_first_entry(&unwind_list,
1709edbd4a0SFrançois Tigeot 				       struct i915_vma,
171e3adcf8fSFrançois Tigeot 				       exec_list);
1729edbd4a0SFrançois Tigeot 		if (drm_mm_scan_remove_block(&vma->node)) {
1739edbd4a0SFrançois Tigeot 			list_move(&vma->exec_list, &eviction_list);
1749edbd4a0SFrançois Tigeot 			drm_gem_object_reference(&vma->obj->base);
175e3adcf8fSFrançois Tigeot 			continue;
176e3adcf8fSFrançois Tigeot 		}
1779edbd4a0SFrançois Tigeot 		list_del_init(&vma->exec_list);
178e3adcf8fSFrançois Tigeot 	}
179e3adcf8fSFrançois Tigeot 
180e3adcf8fSFrançois Tigeot 	/* Unbinding will emit any required flushes */
181e3adcf8fSFrançois Tigeot 	while (!list_empty(&eviction_list)) {
1829edbd4a0SFrançois Tigeot 		struct drm_gem_object *obj;
1839edbd4a0SFrançois Tigeot 		vma = list_first_entry(&eviction_list,
1849edbd4a0SFrançois Tigeot 				       struct i915_vma,
185e3adcf8fSFrançois Tigeot 				       exec_list);
186e3adcf8fSFrançois Tigeot 
1879edbd4a0SFrançois Tigeot 		obj =  &vma->obj->base;
1889edbd4a0SFrançois Tigeot 		list_del_init(&vma->exec_list);
1899edbd4a0SFrançois Tigeot 		if (ret == 0)
1909edbd4a0SFrançois Tigeot 			ret = i915_vma_unbind(vma);
1919edbd4a0SFrançois Tigeot 
1929edbd4a0SFrançois Tigeot 		drm_gem_object_unreference(obj);
193e3adcf8fSFrançois Tigeot 	}
194e3adcf8fSFrançois Tigeot 
195e3adcf8fSFrançois Tigeot 	return ret;
196e3adcf8fSFrançois Tigeot }
197e3adcf8fSFrançois Tigeot 
1989edbd4a0SFrançois Tigeot /**
199ba55f2f5SFrançois Tigeot  * i915_gem_evict_vm - Evict all idle vmas from a vm
200ba55f2f5SFrançois Tigeot  * @vm: Address space to cleanse
2019edbd4a0SFrançois Tigeot  * @do_idle: Boolean directing whether to idle first.
2029edbd4a0SFrançois Tigeot  *
203ba55f2f5SFrançois Tigeot  * This function evicts all idles vmas from a vm. If all unpinned vmas should be
204ba55f2f5SFrançois Tigeot  * evicted the @do_idle needs to be set to true.
2059edbd4a0SFrançois Tigeot  *
206ba55f2f5SFrançois Tigeot  * This is used by the execbuf code as a last-ditch effort to defragment the
207ba55f2f5SFrançois Tigeot  * address space.
208ba55f2f5SFrançois Tigeot  *
209ba55f2f5SFrançois Tigeot  * To clarify: This is for freeing up virtual address space, not for freeing
210ba55f2f5SFrançois Tigeot  * memory in e.g. the shrinker.
2119edbd4a0SFrançois Tigeot  */
2129edbd4a0SFrançois Tigeot int i915_gem_evict_vm(struct i915_address_space *vm, bool do_idle)
2139edbd4a0SFrançois Tigeot {
2149edbd4a0SFrançois Tigeot 	struct i915_vma *vma, *next;
2159edbd4a0SFrançois Tigeot 	int ret;
2169edbd4a0SFrançois Tigeot 
217*2c9916cdSFrançois Tigeot 	WARN_ON(!mutex_is_locked(&vm->dev->struct_mutex));
2189edbd4a0SFrançois Tigeot 	trace_i915_gem_evict_vm(vm);
2199edbd4a0SFrançois Tigeot 
2209edbd4a0SFrançois Tigeot 	if (do_idle) {
2219edbd4a0SFrançois Tigeot 		ret = i915_gpu_idle(vm->dev);
2229edbd4a0SFrançois Tigeot 		if (ret)
2239edbd4a0SFrançois Tigeot 			return ret;
2249edbd4a0SFrançois Tigeot 
2259edbd4a0SFrançois Tigeot 		i915_gem_retire_requests(vm->dev);
226*2c9916cdSFrançois Tigeot 
227*2c9916cdSFrançois Tigeot 		WARN_ON(!list_empty(&vm->active_list));
2289edbd4a0SFrançois Tigeot 	}
2299edbd4a0SFrançois Tigeot 
2309edbd4a0SFrançois Tigeot 	list_for_each_entry_safe(vma, next, &vm->inactive_list, mm_list)
231ba55f2f5SFrançois Tigeot 		if (vma->pin_count == 0)
2329edbd4a0SFrançois Tigeot 			WARN_ON(i915_vma_unbind(vma));
2339edbd4a0SFrançois Tigeot 
2349edbd4a0SFrançois Tigeot 	return 0;
2359edbd4a0SFrançois Tigeot }
2369edbd4a0SFrançois Tigeot 
237ba55f2f5SFrançois Tigeot /**
238ba55f2f5SFrançois Tigeot  * i915_gem_evict_everything - Try to evict all objects
239ba55f2f5SFrançois Tigeot  * @dev: Device to evict objects for
240ba55f2f5SFrançois Tigeot  *
241ba55f2f5SFrançois Tigeot  * This functions tries to evict all gem objects from all address spaces. Used
242ba55f2f5SFrançois Tigeot  * by the shrinker as a last-ditch effort and for suspend, before releasing the
243ba55f2f5SFrançois Tigeot  * backing storage of all unbound objects.
244ba55f2f5SFrançois Tigeot  */
245e3adcf8fSFrançois Tigeot int
246686a02f1SFrançois Tigeot i915_gem_evict_everything(struct drm_device *dev)
247e3adcf8fSFrançois Tigeot {
248ba55f2f5SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2491b13d190SFrançois Tigeot 	struct i915_address_space *vm, *v;
2509edbd4a0SFrançois Tigeot 	bool lists_empty = true;
251686a02f1SFrançois Tigeot 	int ret;
252e3adcf8fSFrançois Tigeot 
2539edbd4a0SFrançois Tigeot 	list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
2549edbd4a0SFrançois Tigeot 		lists_empty = (list_empty(&vm->inactive_list) &&
2559edbd4a0SFrançois Tigeot 			       list_empty(&vm->active_list));
2569edbd4a0SFrançois Tigeot 		if (!lists_empty)
2579edbd4a0SFrançois Tigeot 			lists_empty = false;
2589edbd4a0SFrançois Tigeot 	}
2599edbd4a0SFrançois Tigeot 
260e3adcf8fSFrançois Tigeot 	if (lists_empty)
261e3adcf8fSFrançois Tigeot 		return -ENOSPC;
262e3adcf8fSFrançois Tigeot 
263a2fdbec6SFrançois Tigeot 	trace_i915_gem_evict_everything(dev);
264a2fdbec6SFrançois Tigeot 
265686a02f1SFrançois Tigeot 	/* The gpu_idle will flush everything in the write domain to the
266686a02f1SFrançois Tigeot 	 * active list. Then we must move everything off the active list
267686a02f1SFrançois Tigeot 	 * with retire requests.
268686a02f1SFrançois Tigeot 	 */
269b030f26bSFrançois Tigeot 	ret = i915_gpu_idle(dev);
270e3adcf8fSFrançois Tigeot 	if (ret)
271e3adcf8fSFrançois Tigeot 		return ret;
272e3adcf8fSFrançois Tigeot 
273686a02f1SFrançois Tigeot 	i915_gem_retire_requests(dev);
274e3adcf8fSFrançois Tigeot 
275686a02f1SFrançois Tigeot 	/* Having flushed everything, unbind() should never raise an error */
2761b13d190SFrançois Tigeot 	list_for_each_entry_safe(vm, v, &dev_priv->vm_list, global_link)
2779edbd4a0SFrançois Tigeot 		WARN_ON(i915_gem_evict_vm(vm, false));
278e3adcf8fSFrançois Tigeot 
279e3adcf8fSFrançois Tigeot 	return 0;
280e3adcf8fSFrançois Tigeot }
281