1235783Skib/*
2235783Skib * Copyright �� 2008-2010 Intel Corporation
3235783Skib *
4235783Skib * Permission is hereby granted, free of charge, to any person obtaining a
5235783Skib * copy of this software and associated documentation files (the "Software"),
6235783Skib * to deal in the Software without restriction, including without limitation
7235783Skib * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8235783Skib * and/or sell copies of the Software, and to permit persons to whom the
9235783Skib * Software is furnished to do so, subject to the following conditions:
10235783Skib *
11235783Skib * The above copyright notice and this permission notice (including the next
12235783Skib * paragraph) shall be included in all copies or substantial portions of the
13235783Skib * Software.
14235783Skib *
15235783Skib * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16235783Skib * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17235783Skib * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18235783Skib * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19235783Skib * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20235783Skib * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21235783Skib * IN THE SOFTWARE.
22235783Skib *
23235783Skib * Authors:
24235783Skib *    Eric Anholt <eric@anholt.net>
25235783Skib *    Chris Wilson <chris@chris-wilson.co.uuk>
26235783Skib *
27235783Skib */
28235783Skib
29235783Skib#include <sys/cdefs.h>
30235783Skib__FBSDID("$FreeBSD$");
31235783Skib
32235783Skib#include <dev/drm2/drmP.h>
33296548Sdumbbell#include <dev/drm2/i915/i915_drv.h>
34235783Skib#include <dev/drm2/i915/i915_drm.h>
35235783Skib
36235783Skibstatic bool
37235783Skibmark_free(struct drm_i915_gem_object *obj, struct list_head *unwind)
38235783Skib{
39277487Skib	if (obj->pin_count)
40277487Skib		return false;
41277487Skib
42235783Skib	list_add(&obj->exec_list, unwind);
43235783Skib	return drm_mm_scan_add_block(obj->gtt_space);
44235783Skib}
45235783Skib
46235783Skibint
47235783Skibi915_gem_evict_something(struct drm_device *dev, int min_size,
48296548Sdumbbell			 unsigned alignment, unsigned cache_level,
49296548Sdumbbell			 bool mappable, bool nonblocking)
50235783Skib{
51235783Skib	drm_i915_private_t *dev_priv = dev->dev_private;
52235783Skib	struct list_head eviction_list, unwind_list;
53235783Skib	struct drm_i915_gem_object *obj;
54235783Skib	int ret = 0;
55235783Skib
56235783Skib	CTR4(KTR_DRM, "evict_something %p %d %u %d", dev, min_size,
57235783Skib	    alignment, mappable);
58235783Skib
59235783Skib	/*
60235783Skib	 * The goal is to evict objects and amalgamate space in LRU order.
61235783Skib	 * The oldest idle objects reside on the inactive list, which is in
62235783Skib	 * retirement order. The next objects to retire are those on the (per
63235783Skib	 * ring) active list that do not have an outstanding flush. Once the
64235783Skib	 * hardware reports completion (the seqno is updated after the
65235783Skib	 * batchbuffer has been finished) the clean buffer objects would
66235783Skib	 * be retired to the inactive list. Any dirty objects would be added
67235783Skib	 * to the tail of the flushing list. So after processing the clean
68235783Skib	 * active objects we need to emit a MI_FLUSH to retire the flushing
69235783Skib	 * list, hence the retirement order of the flushing list is in
70235783Skib	 * advance of the dirty objects on the active lists.
71235783Skib	 *
72235783Skib	 * The retirement sequence is thus:
73235783Skib	 *   1. Inactive objects (already retired)
74235783Skib	 *   2. Clean active objects
75235783Skib	 *   3. Flushing list
76235783Skib	 *   4. Dirty active objects.
77235783Skib	 *
78235783Skib	 * On each list, the oldest objects lie at the HEAD with the freshest
79235783Skib	 * object on the TAIL.
80235783Skib	 */
81235783Skib
82235783Skib	INIT_LIST_HEAD(&unwind_list);
83235783Skib	if (mappable)
84296548Sdumbbell		drm_mm_init_scan_with_range(&dev_priv->mm.gtt_space,
85296548Sdumbbell					    min_size, alignment, cache_level,
86296548Sdumbbell					    0, dev_priv->mm.gtt_mappable_end);
87235783Skib	else
88296548Sdumbbell		drm_mm_init_scan(&dev_priv->mm.gtt_space,
89296548Sdumbbell				 min_size, alignment, cache_level);
90235783Skib
91235783Skib	/* First see if there is a large enough contiguous idle region... */
92235783Skib	list_for_each_entry(obj, &dev_priv->mm.inactive_list, mm_list) {
93235783Skib		if (mark_free(obj, &unwind_list))
94235783Skib			goto found;
95235783Skib	}
96235783Skib
97296548Sdumbbell	if (nonblocking)
98296548Sdumbbell		goto none;
99296548Sdumbbell
100235783Skib	/* Now merge in the soon-to-be-expired objects... */
101235783Skib	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
102235783Skib		if (mark_free(obj, &unwind_list))
103235783Skib			goto found;
104235783Skib	}
105235783Skib
106296548Sdumbbellnone:
107235783Skib	/* Nothing found, clean up and bail out! */
108235783Skib	while (!list_empty(&unwind_list)) {
109235783Skib		obj = list_first_entry(&unwind_list,
110235783Skib				       struct drm_i915_gem_object,
111235783Skib				       exec_list);
112235783Skib
113235783Skib		ret = drm_mm_scan_remove_block(obj->gtt_space);
114296548Sdumbbell		BUG_ON(ret);
115235783Skib
116235783Skib		list_del_init(&obj->exec_list);
117235783Skib	}
118235783Skib
119235783Skib	/* We expect the caller to unpin, evict all and try again, or give up.
120235783Skib	 * So calling i915_gem_evict_everything() is unnecessary.
121235783Skib	 */
122235783Skib	return -ENOSPC;
123235783Skib
124235783Skibfound:
125235783Skib	/* drm_mm doesn't allow any other other operations while
126235783Skib	 * scanning, therefore store to be evicted objects on a
127235783Skib	 * temporary list. */
128235783Skib	INIT_LIST_HEAD(&eviction_list);
129235783Skib	while (!list_empty(&unwind_list)) {
130235783Skib		obj = list_first_entry(&unwind_list,
131235783Skib				       struct drm_i915_gem_object,
132235783Skib				       exec_list);
133235783Skib		if (drm_mm_scan_remove_block(obj->gtt_space)) {
134235783Skib			list_move(&obj->exec_list, &eviction_list);
135235783Skib			drm_gem_object_reference(&obj->base);
136235783Skib			continue;
137235783Skib		}
138235783Skib		list_del_init(&obj->exec_list);
139235783Skib	}
140235783Skib
141235783Skib	/* Unbinding will emit any required flushes */
142235783Skib	while (!list_empty(&eviction_list)) {
143235783Skib		obj = list_first_entry(&eviction_list,
144235783Skib				       struct drm_i915_gem_object,
145235783Skib				       exec_list);
146235783Skib		if (ret == 0)
147235783Skib			ret = i915_gem_object_unbind(obj);
148235783Skib
149235783Skib		list_del_init(&obj->exec_list);
150235783Skib		drm_gem_object_unreference(&obj->base);
151235783Skib	}
152235783Skib
153235783Skib	return ret;
154235783Skib}
155235783Skib
156235783Skibint
157296548Sdumbbelli915_gem_evict_everything(struct drm_device *dev)
158235783Skib{
159235783Skib	drm_i915_private_t *dev_priv = dev->dev_private;
160277487Skib	struct drm_i915_gem_object *obj, *next;
161277487Skib	bool lists_empty;
162235783Skib	int ret;
163235783Skib
164235783Skib	lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
165235783Skib		       list_empty(&dev_priv->mm.active_list));
166235783Skib	if (lists_empty)
167235783Skib		return -ENOSPC;
168235783Skib
169296548Sdumbbell	CTR1(KTR_DRM, "evict_everything %p", dev);
170235783Skib
171277487Skib	/* The gpu_idle will flush everything in the write domain to the
172277487Skib	 * active list. Then we must move everything off the active list
173277487Skib	 * with retire requests.
174277487Skib	 */
175277487Skib	ret = i915_gpu_idle(dev);
176235783Skib	if (ret)
177235783Skib		return ret;
178235783Skib
179277487Skib	i915_gem_retire_requests(dev);
180277487Skib
181277487Skib	/* Having flushed everything, unbind() should never raise an error */
182235783Skib	list_for_each_entry_safe(obj, next,
183296548Sdumbbell				 &dev_priv->mm.inactive_list, mm_list)
184296548Sdumbbell		if (obj->pin_count == 0)
185296548Sdumbbell			WARN_ON(i915_gem_object_unbind(obj));
186235783Skib
187235783Skib	return 0;
188235783Skib}
189