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 "lib_sw_fence.h"
28 #include "mock_context.h"
30 #include "mock_gem_device.h"
32 static void quirk_add(struct drm_i915_gem_object *obj,
33 struct list_head *objects)
35 /* quirk is only for live tiled objects, use it to declare ownership */
36 GEM_BUG_ON(obj->mm.quirked);
37 obj->mm.quirked = true;
38 list_add(&obj->st_link, objects);
41 static int populate_ggtt(struct drm_i915_private *i915,
42 struct list_head *objects)
44 unsigned long unbound, bound, count;
45 struct drm_i915_gem_object *obj;
50 size + I915_GTT_PAGE_SIZE <= i915->ggtt.vm.total;
51 size += I915_GTT_PAGE_SIZE) {
54 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
58 quirk_add(obj, objects);
60 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
68 list_for_each_entry(obj, &i915->mm.unbound_list, mm.link)
72 pr_err("%s: Found %lu objects unbound, expected %u!\n",
73 __func__, unbound, 0);
78 list_for_each_entry(obj, &i915->mm.bound_list, mm.link)
82 pr_err("%s: Found %lu objects bound, expected %lu!\n",
83 __func__, bound, count);
87 if (list_empty(&i915->ggtt.vm.bound_list)) {
88 pr_err("No objects on the GGTT inactive list!\n");
95 static void unpin_ggtt(struct drm_i915_private *i915)
97 struct i915_ggtt *ggtt = &i915->ggtt;
100 mutex_lock(&ggtt->vm.mutex);
101 list_for_each_entry(vma, &i915->ggtt.vm.bound_list, vm_link)
102 if (vma->obj->mm.quirked)
104 mutex_unlock(&ggtt->vm.mutex);
107 static void cleanup_objects(struct drm_i915_private *i915,
108 struct list_head *list)
110 struct drm_i915_gem_object *obj, *on;
112 list_for_each_entry_safe(obj, on, list, st_link) {
113 GEM_BUG_ON(!obj->mm.quirked);
114 obj->mm.quirked = false;
115 i915_gem_object_put(obj);
118 mutex_unlock(&i915->drm.struct_mutex);
120 i915_gem_drain_freed_objects(i915);
122 mutex_lock(&i915->drm.struct_mutex);
125 static int igt_evict_something(void *arg)
127 struct drm_i915_private *i915 = arg;
128 struct i915_ggtt *ggtt = &i915->ggtt;
132 /* Fill the GGTT with pinned objects and try to evict one. */
134 err = populate_ggtt(i915, &objects);
138 /* Everything is pinned, nothing should happen */
139 err = i915_gem_evict_something(&ggtt->vm,
140 I915_GTT_PAGE_SIZE, 0, 0,
143 if (err != -ENOSPC) {
144 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
151 /* Everything is unpinned, we should be able to evict something */
152 err = i915_gem_evict_something(&ggtt->vm,
153 I915_GTT_PAGE_SIZE, 0, 0,
157 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
163 cleanup_objects(i915, &objects);
167 static int igt_overcommit(void *arg)
169 struct drm_i915_private *i915 = arg;
170 struct drm_i915_gem_object *obj;
171 struct i915_vma *vma;
175 /* Fill the GGTT with pinned objects and then try to pin one more.
176 * We expect it to fail.
179 err = populate_ggtt(i915, &objects);
183 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
189 quirk_add(obj, &objects);
191 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
192 if (!IS_ERR(vma) || PTR_ERR(vma) != -ENOSPC) {
193 pr_err("Failed to evict+insert, i915_gem_object_ggtt_pin returned err=%d\n", (int)PTR_ERR(vma));
199 cleanup_objects(i915, &objects);
203 static int igt_evict_for_vma(void *arg)
205 struct drm_i915_private *i915 = arg;
206 struct i915_ggtt *ggtt = &i915->ggtt;
207 struct drm_mm_node target = {
214 /* Fill the GGTT with pinned objects and try to evict a range. */
216 err = populate_ggtt(i915, &objects);
220 /* Everything is pinned, nothing should happen */
221 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
222 if (err != -ENOSPC) {
223 pr_err("i915_gem_evict_for_node on a full GGTT returned err=%d\n",
230 /* Everything is unpinned, we should be able to evict the node */
231 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
233 pr_err("i915_gem_evict_for_node returned err=%d\n",
239 cleanup_objects(i915, &objects);
243 static void mock_color_adjust(const struct drm_mm_node *node,
250 static int igt_evict_for_cache_color(void *arg)
252 struct drm_i915_private *i915 = arg;
253 struct i915_ggtt *ggtt = &i915->ggtt;
254 const unsigned long flags = PIN_OFFSET_FIXED;
255 struct drm_mm_node target = {
256 .start = I915_GTT_PAGE_SIZE * 2,
257 .size = I915_GTT_PAGE_SIZE,
258 .color = I915_CACHE_LLC,
260 struct drm_i915_gem_object *obj;
261 struct i915_vma *vma;
265 /* Currently the use of color_adjust is limited to cache domains within
266 * the ggtt, and so the presence of mm.color_adjust is assumed to be
267 * i915_gtt_color_adjust throughout our driver, so using a mock color
268 * adjust will work just fine for our purposes.
270 ggtt->vm.mm.color_adjust = mock_color_adjust;
272 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
277 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
278 quirk_add(obj, &objects);
280 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
281 I915_GTT_PAGE_SIZE | flags);
283 pr_err("[0]i915_gem_object_ggtt_pin failed\n");
288 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
293 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
294 quirk_add(obj, &objects);
296 /* Neighbouring; same colour - should fit */
297 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
298 (I915_GTT_PAGE_SIZE * 2) | flags);
300 pr_err("[1]i915_gem_object_ggtt_pin failed\n");
307 /* Remove just the second vma */
308 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
310 pr_err("[0]i915_gem_evict_for_node returned err=%d\n", err);
314 /* Attempt to remove the first *pinned* vma, by removing the (empty)
315 * neighbour -- this should fail.
317 target.color = I915_CACHE_L3_LLC;
319 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
321 pr_err("[1]i915_gem_evict_for_node returned err=%d\n", err);
330 cleanup_objects(i915, &objects);
331 ggtt->vm.mm.color_adjust = NULL;
335 static int igt_evict_vm(void *arg)
337 struct drm_i915_private *i915 = arg;
338 struct i915_ggtt *ggtt = &i915->ggtt;
342 /* Fill the GGTT with pinned objects and try to evict everything. */
344 err = populate_ggtt(i915, &objects);
348 /* Everything is pinned, nothing should happen */
349 err = i915_gem_evict_vm(&ggtt->vm);
351 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
358 err = i915_gem_evict_vm(&ggtt->vm);
360 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
366 cleanup_objects(i915, &objects);
370 static int igt_evict_contexts(void *arg)
372 const u64 PRETEND_GGTT_SIZE = 16ull << 20;
373 struct drm_i915_private *i915 = arg;
374 struct intel_engine_cs *engine;
375 enum intel_engine_id id;
377 struct drm_mm_node node;
378 struct reserved *next;
380 intel_wakeref_t wakeref;
381 struct drm_mm_node hole;
386 * The purpose of this test is to verify that we will trigger an
387 * eviction in the GGTT when constructing a request that requires
388 * additional space in the GGTT for pinning the context. This space
389 * is not directly tied to the request so reclaiming it requires
392 * As such this test is only meaningful for full-ppgtt environments
393 * where the GTT space of the request is separate from the GGTT
394 * allocation required to build the request.
396 if (!HAS_FULL_PPGTT(i915))
399 mutex_lock(&i915->drm.struct_mutex);
400 wakeref = intel_runtime_pm_get(i915);
402 /* Reserve a block so that we know we have enough to fit a few rq */
403 memset(&hole, 0, sizeof(hole));
404 err = i915_gem_gtt_insert(&i915->ggtt.vm, &hole,
405 PRETEND_GGTT_SIZE, 0, I915_COLOR_UNEVICTABLE,
406 0, i915->ggtt.vm.total,
411 /* Make the GGTT appear small by filling it with unevictable nodes */
416 r = kcalloc(1, sizeof(*r), GFP_KERNEL);
422 if (i915_gem_gtt_insert(&i915->ggtt.vm, &r->node,
423 1ul << 20, 0, I915_COLOR_UNEVICTABLE,
424 0, i915->ggtt.vm.total,
435 drm_mm_remove_node(&hole);
436 mutex_unlock(&i915->drm.struct_mutex);
437 pr_info("Filled GGTT with %lu 1MiB nodes\n", count);
439 /* Overfill the GGTT with context objects and so try to evict one. */
440 for_each_engine(engine, i915, id) {
441 struct i915_sw_fence fence;
442 struct drm_file *file;
444 file = mock_file(i915);
451 mutex_lock(&i915->drm.struct_mutex);
452 onstack_fence_init(&fence);
454 struct i915_request *rq;
455 struct i915_gem_context *ctx;
457 ctx = live_context(i915, file);
461 /* We will need some GGTT space for the rq's context */
462 igt_evict_ctl.fail_if_busy = true;
463 rq = i915_request_alloc(engine, ctx);
464 igt_evict_ctl.fail_if_busy = false;
467 /* When full, fail_if_busy will trigger EBUSY */
468 if (PTR_ERR(rq) != -EBUSY) {
469 pr_err("Unexpected error from request alloc (ctx hw id %u, on %s): %d\n",
470 ctx->hw_id, engine->name,
477 /* Keep every request/ctx pinned until we are full */
478 err = i915_sw_fence_await_sw_fence_gfp(&rq->submit,
484 i915_request_add(rq);
488 mutex_unlock(&i915->drm.struct_mutex);
490 onstack_fence_fini(&fence);
491 pr_info("Submitted %lu contexts/requests on %s\n",
492 count, engine->name);
494 mock_file_free(i915, file);
499 mutex_lock(&i915->drm.struct_mutex);
502 struct reserved *next = reserved->next;
504 drm_mm_remove_node(&reserved->node);
509 if (drm_mm_node_allocated(&hole))
510 drm_mm_remove_node(&hole);
511 intel_runtime_pm_put(i915, wakeref);
512 mutex_unlock(&i915->drm.struct_mutex);
517 int i915_gem_evict_mock_selftests(void)
519 static const struct i915_subtest tests[] = {
520 SUBTEST(igt_evict_something),
521 SUBTEST(igt_evict_for_vma),
522 SUBTEST(igt_evict_for_cache_color),
523 SUBTEST(igt_evict_vm),
524 SUBTEST(igt_overcommit),
526 struct drm_i915_private *i915;
527 intel_wakeref_t wakeref;
530 i915 = mock_gem_device();
534 mutex_lock(&i915->drm.struct_mutex);
535 with_intel_runtime_pm(i915, wakeref)
536 err = i915_subtests(tests, i915);
538 mutex_unlock(&i915->drm.struct_mutex);
540 drm_dev_put(&i915->drm);
544 int i915_gem_evict_live_selftests(struct drm_i915_private *i915)
546 static const struct i915_subtest tests[] = {
547 SUBTEST(igt_evict_contexts),
550 if (i915_terminally_wedged(i915))
553 return i915_subtests(tests, i915);