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{
40280369Skib	if (obj->pin_count)
41280369Skib		return false;
42280369Skib
43235783Skib	list_add(&obj->exec_list, unwind);
44235783Skib	return drm_mm_scan_add_block(obj->gtt_space);
45235783Skib}
46235783Skib
47235783Skibint
48235783Skibi915_gem_evict_something(struct drm_device *dev, int min_size,
49235783Skib			 unsigned alignment, bool mappable)
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)
84235783Skib		drm_mm_init_scan_with_range(&dev_priv->mm.gtt_space, min_size,
85282199Sdumbbell					    alignment, 0, 0,
86235783Skib					    dev_priv->mm.gtt_mappable_end);
87235783Skib	else
88282199Sdumbbell		drm_mm_init_scan(&dev_priv->mm.gtt_space, min_size, alignment, 0);
89235783Skib
90235783Skib	/* First see if there is a large enough contiguous idle region... */
91235783Skib	list_for_each_entry(obj, &dev_priv->mm.inactive_list, mm_list) {
92235783Skib		if (mark_free(obj, &unwind_list))
93235783Skib			goto found;
94235783Skib	}
95235783Skib
96235783Skib	/* Now merge in the soon-to-be-expired objects... */
97235783Skib	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
98235783Skib		/* Does the object require an outstanding flush? */
99280369Skib		if (obj->base.write_domain)
100235783Skib			continue;
101235783Skib
102235783Skib		if (mark_free(obj, &unwind_list))
103235783Skib			goto found;
104235783Skib	}
105235783Skib
106235783Skib	/* Finally add anything with a pending flush (in order of retirement) */
107235783Skib	list_for_each_entry(obj, &dev_priv->mm.flushing_list, mm_list) {
108235783Skib		if (mark_free(obj, &unwind_list))
109235783Skib			goto found;
110235783Skib	}
111235783Skib	list_for_each_entry(obj, &dev_priv->mm.active_list, mm_list) {
112280369Skib		if (!obj->base.write_domain)
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;
172280369Skib	struct drm_i915_gem_object *obj, *next;
173280369Skib	bool lists_empty;
174235783Skib	int ret;
175235783Skib
176235783Skib	lists_empty = (list_empty(&dev_priv->mm.inactive_list) &&
177235783Skib		       list_empty(&dev_priv->mm.flushing_list) &&
178235783Skib		       list_empty(&dev_priv->mm.active_list));
179235783Skib	if (lists_empty)
180235783Skib		return -ENOSPC;
181235783Skib
182235783Skib	CTR2(KTR_DRM, "evict_everything %p %d", dev, purgeable_only);
183235783Skib
184280369Skib	/* The gpu_idle will flush everything in the write domain to the
185280369Skib	 * active list. Then we must move everything off the active list
186280369Skib	 * with retire requests.
187280369Skib	 */
188280369Skib	ret = i915_gpu_idle(dev);
189235783Skib	if (ret)
190235783Skib		return ret;
191235783Skib
192280369Skib	i915_gem_retire_requests(dev);
193280369Skib
194235783Skib	KASSERT(list_empty(&dev_priv->mm.flushing_list),
195235783Skib	    ("flush list not empty"));
196235783Skib
197280369Skib	/* Having flushed everything, unbind() should never raise an error */
198235783Skib	list_for_each_entry_safe(obj, next,
199235783Skib				 &dev_priv->mm.inactive_list, mm_list) {
200235783Skib		if (!purgeable_only || obj->madv != I915_MADV_WILLNEED) {
201280369Skib			if (obj->pin_count == 0)
202280369Skib				i915_gem_object_unbind(obj);
203235783Skib		}
204235783Skib	}
205235783Skib
206235783Skib	return 0;
207235783Skib}
208