2 * Copyright © 2016 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include "../i915_selftest.h"
27 #include "mock_gem_device.h"
29 static int populate_ggtt(struct drm_i915_private *i915)
31 struct drm_i915_gem_object *obj;
35 size + I915_GTT_PAGE_SIZE <= i915->ggtt.base.total;
36 size += I915_GTT_PAGE_SIZE) {
39 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
43 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
48 if (!list_empty(&i915->mm.unbound_list)) {
50 list_for_each_entry(obj, &i915->mm.unbound_list, global_link)
53 pr_err("Found %lld objects unbound!\n", size);
57 if (list_empty(&i915->ggtt.base.inactive_list)) {
58 pr_err("No objects on the GGTT inactive list!\n");
65 static void unpin_ggtt(struct drm_i915_private *i915)
69 list_for_each_entry(vma, &i915->ggtt.base.inactive_list, vm_link)
73 static void cleanup_objects(struct drm_i915_private *i915)
75 struct drm_i915_gem_object *obj, *on;
77 list_for_each_entry_safe(obj, on, &i915->mm.unbound_list, global_link)
78 i915_gem_object_put(obj);
80 list_for_each_entry_safe(obj, on, &i915->mm.bound_list, global_link)
81 i915_gem_object_put(obj);
83 mutex_unlock(&i915->drm.struct_mutex);
85 i915_gem_drain_freed_objects(i915);
87 mutex_lock(&i915->drm.struct_mutex);
90 static int igt_evict_something(void *arg)
92 struct drm_i915_private *i915 = arg;
93 struct i915_ggtt *ggtt = &i915->ggtt;
96 /* Fill the GGTT with pinned objects and try to evict one. */
98 err = populate_ggtt(i915);
102 /* Everything is pinned, nothing should happen */
103 err = i915_gem_evict_something(&ggtt->base,
104 I915_GTT_PAGE_SIZE, 0, 0,
107 if (err != -ENOSPC) {
108 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
115 /* Everything is unpinned, we should be able to evict something */
116 err = i915_gem_evict_something(&ggtt->base,
117 I915_GTT_PAGE_SIZE, 0, 0,
121 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
127 cleanup_objects(i915);
131 static int igt_overcommit(void *arg)
133 struct drm_i915_private *i915 = arg;
134 struct drm_i915_gem_object *obj;
135 struct i915_vma *vma;
138 /* Fill the GGTT with pinned objects and then try to pin one more.
139 * We expect it to fail.
142 err = populate_ggtt(i915);
146 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
152 list_move(&obj->global_link, &i915->mm.unbound_list);
154 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
155 if (!IS_ERR(vma) || PTR_ERR(vma) != -ENOSPC) {
156 pr_err("Failed to evict+insert, i915_gem_object_ggtt_pin returned err=%d\n", (int)PTR_ERR(vma));
162 cleanup_objects(i915);
166 static int igt_evict_for_vma(void *arg)
168 struct drm_i915_private *i915 = arg;
169 struct i915_ggtt *ggtt = &i915->ggtt;
170 struct drm_mm_node target = {
176 /* Fill the GGTT with pinned objects and try to evict a range. */
178 err = populate_ggtt(i915);
182 /* Everything is pinned, nothing should happen */
183 err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
184 if (err != -ENOSPC) {
185 pr_err("i915_gem_evict_for_node on a full GGTT returned err=%d\n",
192 /* Everything is unpinned, we should be able to evict the node */
193 err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
195 pr_err("i915_gem_evict_for_node returned err=%d\n",
201 cleanup_objects(i915);
205 static void mock_color_adjust(const struct drm_mm_node *node,
212 static int igt_evict_for_cache_color(void *arg)
214 struct drm_i915_private *i915 = arg;
215 struct i915_ggtt *ggtt = &i915->ggtt;
216 const unsigned long flags = PIN_OFFSET_FIXED;
217 struct drm_mm_node target = {
218 .start = I915_GTT_PAGE_SIZE * 2,
219 .size = I915_GTT_PAGE_SIZE,
220 .color = I915_CACHE_LLC,
222 struct drm_i915_gem_object *obj;
223 struct i915_vma *vma;
226 /* Currently the use of color_adjust is limited to cache domains within
227 * the ggtt, and so the presence of mm.color_adjust is assumed to be
228 * i915_gtt_color_adjust throughout our driver, so using a mock color
229 * adjust will work just fine for our purposes.
231 ggtt->base.mm.color_adjust = mock_color_adjust;
233 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
238 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
240 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
241 I915_GTT_PAGE_SIZE | flags);
243 pr_err("[0]i915_gem_object_ggtt_pin failed\n");
248 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
253 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
255 /* Neighbouring; same colour - should fit */
256 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
257 (I915_GTT_PAGE_SIZE * 2) | flags);
259 pr_err("[1]i915_gem_object_ggtt_pin failed\n");
266 /* Remove just the second vma */
267 err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
269 pr_err("[0]i915_gem_evict_for_node returned err=%d\n", err);
273 /* Attempt to remove the first *pinned* vma, by removing the (empty)
274 * neighbour -- this should fail.
276 target.color = I915_CACHE_L3_LLC;
278 err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
280 pr_err("[1]i915_gem_evict_for_node returned err=%d\n", err);
289 cleanup_objects(i915);
290 ggtt->base.mm.color_adjust = NULL;
294 static int igt_evict_vm(void *arg)
296 struct drm_i915_private *i915 = arg;
297 struct i915_ggtt *ggtt = &i915->ggtt;
300 /* Fill the GGTT with pinned objects and try to evict everything. */
302 err = populate_ggtt(i915);
306 /* Everything is pinned, nothing should happen */
307 err = i915_gem_evict_vm(&ggtt->base, false);
309 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
316 err = i915_gem_evict_vm(&ggtt->base, false);
318 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
324 cleanup_objects(i915);
328 int i915_gem_evict_mock_selftests(void)
330 static const struct i915_subtest tests[] = {
331 SUBTEST(igt_evict_something),
332 SUBTEST(igt_evict_for_vma),
333 SUBTEST(igt_evict_for_cache_color),
334 SUBTEST(igt_evict_vm),
335 SUBTEST(igt_overcommit),
337 struct drm_i915_private *i915;
340 i915 = mock_gem_device();
344 mutex_lock(&i915->drm.struct_mutex);
345 err = i915_subtests(tests, i915);
346 mutex_unlock(&i915->drm.struct_mutex);
348 drm_dev_unref(&i915->drm);