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>
33235783Skib#include <dev/drm2/drm.h>
34235783Skib#include <dev/drm2/i915/i915_drm.h>
35235783Skib#include <dev/drm2/i915/i915_drv.h>
36235783Skib
37235783Skibstatic bool
38235783Skibmark_free(struct drm_i915_gem_object *obj, struct list_head *unwind)
39235783Skib{
40235783Skib	list_add(&obj->exec_list, unwind);
41235783Skib	return drm_mm_scan_add_block(obj->gtt_space);
42235783Skib}
43235783Skib
44235783Skibint
45235783Skibi915_gem_evict_something(struct drm_device *dev, int min_size,
46235783Skib			 unsigned alignment, bool mappable)
47235783Skib{
48235783Skib	drm_i915_private_t *dev_priv = dev->dev_private;
49235783Skib	struct list_head eviction_list, unwind_list;
50235783Skib	struct drm_i915_gem_object *obj;
51235783Skib	int ret = 0;
52235783Skib
53235783Skib	CTR4(KTR_DRM, "evict_something %p %d %u %d", dev, min_size,
54235783Skib	    alignment, mappable);
55235783Skib
56235783Skib	/*
57235783Skib	 * The goal is to evict objects and amalgamate space in LRU order.
58235783Skib	 * The oldest idle objects reside on the inactive list, which is in
59235783Skib	 * retirement order. The next objects to retire are those on the (per
60235783Skib	 * ring) active list that do not have an outstanding flush. Once the
61235783Skib	 * hardware reports completion (the seqno is updated after the
62235783Skib	 * batchbuffer has been finished) the clean buffer objects would
63235783Skib	 * be retired to the inactive list. Any dirty objects would be added
64235783Skib	 * to the tail of the flushing list. So after processing the clean
65235783Skib	 * active objects we need to emit a MI_FLUSH to retire the flushing
66235783Skib	 * list, hence the retirement order of the flushing list is in
67235783Skib	 * advance of the dirty objects on the active lists.
68235783Skib	 *
69235783Skib	 * The retirement sequence is thus:
70235783Skib	 *   1. Inactive objects (already retired)
71235783Skib	 *   2. Clean active objects
72235783Skib	 *   3. Flushing list
73235783Skib	 *   4. Dirty active objects.
74235783Skib	 *
75235783Skib	 * On each list, the oldest objects lie at the HEAD with the freshest
76235783Skib	 * object on the TAIL.
77235783Skib	 */
78235783Skib
79235783Skib	INIT_LIST_HEAD(&unwind_list);
80235783Skib	if (mappable)
81235783Skib		drm_mm_init_scan_with_range(&dev_priv->mm.gtt_space, min_size,
82235783Skib					    alignment, 0,
83235783Skib					    dev_priv->mm.gtt_mappable_end);
84235783Skib	else
85235783Skib		drm_mm_init_scan(&dev_priv->mm.gtt_space, min_size, alignment);
86235783Skib
87235783Skib	/* First see if there is a large enough contiguous idle region... */
88235783Skib	list_for_each_entry(obj, &dev_priv->mm.inactive_list, mm_list) {
89235783Skib		if (mark_free(obj, &unwind_list))
90235783Skib			goto found;
91235783Skib	}
92235783Skib
93235783Skib	/* Now merge in the soon-to-be-expired objects... */
94235783Skib	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
95235783Skib		/* Does the object require an outstanding flush? */
96235783Skib		if (obj->base.write_domain || obj->pin_count)
97235783Skib			continue;
98235783Skib
99235783Skib		if (mark_free(obj, &unwind_list))
100235783Skib			goto found;
101235783Skib	}
102235783Skib
103235783Skib	/* Finally add anything with a pending flush (in order of retirement) */
104235783Skib	list_for_each_entry(obj, &dev_priv->mm.flushing_list, mm_list) {
105235783Skib		if (obj->pin_count)
106235783Skib			continue;
107235783Skib
108235783Skib		if (mark_free(obj, &unwind_list))
109235783Skib			goto found;
110235783Skib	}
111235783Skib	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
112235783Skib		if (!obj->base.write_domain || obj->pin_count)
113235783Skib			continue;
114235783Skib
115235783Skib		if (mark_free(obj, &unwind_list))
116235783Skib			goto found;
117235783Skib	}
118235783Skib
119235783Skib	/* Nothing found, clean up and bail out! */
120235783Skib	while (!list_empty(&unwind_list)) {
121235783Skib		obj = list_first_entry(&unwind_list,
122235783Skib				       struct drm_i915_gem_object,
123235783Skib				       exec_list);
124235783Skib
125235783Skib		ret = drm_mm_scan_remove_block(obj->gtt_space);
126235783Skib		KASSERT(ret == 0, ("drm_mm_scan_remove_block failed %d", ret));
127235783Skib
128235783Skib		list_del_init(&obj->exec_list);
129235783Skib	}
130235783Skib
131235783Skib	/* We expect the caller to unpin, evict all and try again, or give up.
132235783Skib	 * So calling i915_gem_evict_everything() is unnecessary.
133235783Skib	 */
134235783Skib	return -ENOSPC;
135235783Skib
136235783Skibfound:
137235783Skib	/* drm_mm doesn't allow any other other operations while
138235783Skib	 * scanning, therefore store to be evicted objects on a
139235783Skib	 * temporary list. */
140235783Skib	INIT_LIST_HEAD(&eviction_list);
141235783Skib	while (!list_empty(&unwind_list)) {
142235783Skib		obj = list_first_entry(&unwind_list,
143235783Skib				       struct drm_i915_gem_object,
144235783Skib				       exec_list);
145235783Skib		if (drm_mm_scan_remove_block(obj->gtt_space)) {
146235783Skib			list_move(&obj->exec_list, &eviction_list);
147235783Skib			drm_gem_object_reference(&obj->base);
148235783Skib			continue;
149235783Skib		}
150235783Skib		list_del_init(&obj->exec_list);
151235783Skib	}
152235783Skib
153235783Skib	/* Unbinding will emit any required flushes */
154235783Skib	while (!list_empty(&eviction_list)) {
155235783Skib		obj = list_first_entry(&eviction_list,
156235783Skib				       struct drm_i915_gem_object,
157235783Skib				       exec_list);
158235783Skib		if (ret == 0)
159235783Skib			ret = i915_gem_object_unbind(obj);
160235783Skib
161235783Skib		list_del_init(&obj->exec_list);
162235783Skib		drm_gem_object_unreference(&obj->base);
163235783Skib	}
164235783Skib
165235783Skib	return ret;
166235783Skib}
167235783Skib
168235783Skibint
169235783Skibi915_gem_evict_everything(struct drm_device *dev, bool purgeable_only)
170235783Skib{
171235783Skib	drm_i915_private_t *dev_priv = dev->dev_private;
172235783Skib	int ret;
173235783Skib	bool lists_empty;
174235783Skib
175235783Skib	lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
176235783Skib		       list_empty(&dev_priv->mm.flushing_list) &&
177235783Skib		       list_empty(&dev_priv->mm.active_list));
178235783Skib	if (lists_empty)
179235783Skib		return -ENOSPC;
180235783Skib
181235783Skib	CTR2(KTR_DRM, "evict_everything %p %d", dev, purgeable_only);
182235783Skib
183235783Skib	/* Flush everything (on to the inactive lists) and evict */
184235783Skib	ret = i915_gpu_idle(dev, true);
185235783Skib	if (ret)
186235783Skib		return ret;
187235783Skib
188235783Skib	KASSERT(list_empty(&dev_priv->mm.flushing_list),
189235783Skib	    ("flush list not empty"));
190235783Skib
191235783Skib	return i915_gem_evict_inactive(dev, purgeable_only);
192235783Skib}
193235783Skib
194235783Skib/** Unbinds all inactive objects. */
195235783Skibint
196235783Skibi915_gem_evict_inactive(struct drm_device *dev, bool purgeable_only)
197235783Skib{
198235783Skib	drm_i915_private_t *dev_priv = dev->dev_private;
199235783Skib	struct drm_i915_gem_object *obj, *next;
200235783Skib
201235783Skib	CTR2(KTR_DRM, "evict_inactive %p %d", dev, purgeable_only);
202235783Skib
203235783Skib	list_for_each_entry_safe(obj, next,
204235783Skib				 &dev_priv->mm.inactive_list, mm_list) {
205235783Skib		if (!purgeable_only || obj->madv != I915_MADV_WILLNEED) {
206235783Skib			int ret = i915_gem_object_unbind(obj);
207235783Skib			if (ret)
208235783Skib				return ret;
209235783Skib		}
210235783Skib	}
211235783Skib
212235783Skib	return 0;
213235783Skib}
214