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 "gem/i915_gem_internal.h"
26 #include "gem/i915_gem_pm.h"
27 #include "gem/selftests/igt_gem_utils.h"
28 #include "gem/selftests/mock_context.h"
29 #include "gt/intel_gt.h"
30 #include "gt/intel_gt_print.h"
32 #include "i915_selftest.h"
34 #include "igt_flush_test.h"
35 #include "lib_sw_fence.h"
37 #include "mock_gem_device.h"
39 static void quirk_add(struct drm_i915_gem_object *obj,
40 struct list_head *objects)
42 /* quirk is only for live tiled objects, use it to declare ownership */
43 GEM_BUG_ON(i915_gem_object_has_tiling_quirk(obj));
44 i915_gem_object_set_tiling_quirk(obj);
45 list_add(&obj->st_link, objects);
48 static int populate_ggtt(struct i915_ggtt *ggtt, struct list_head *objects)
50 struct drm_i915_gem_object *obj;
57 obj = i915_gem_object_create_internal(ggtt->vm.i915,
62 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
64 i915_gem_object_put(obj);
65 if (vma == ERR_PTR(-ENOSPC))
71 quirk_add(obj, objects);
74 pr_debug("Filled GGTT with %lu pages [%llu total]\n",
75 count, ggtt->vm.total / PAGE_SIZE);
77 if (list_empty(&ggtt->vm.bound_list)) {
78 pr_err("No objects on the GGTT inactive list!\n");
85 static void unpin_ggtt(struct i915_ggtt *ggtt)
89 list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
90 if (i915_gem_object_has_tiling_quirk(vma->obj))
94 static void cleanup_objects(struct i915_ggtt *ggtt, struct list_head *list)
96 struct drm_i915_gem_object *obj, *on;
98 list_for_each_entry_safe(obj, on, list, st_link) {
99 GEM_BUG_ON(!i915_gem_object_has_tiling_quirk(obj));
100 i915_gem_object_set_tiling_quirk(obj);
101 i915_gem_object_put(obj);
104 i915_gem_drain_freed_objects(ggtt->vm.i915);
107 static int igt_evict_something(void *arg)
109 struct intel_gt *gt = arg;
110 struct i915_ggtt *ggtt = gt->ggtt;
114 /* Fill the GGTT with pinned objects and try to evict one. */
116 err = populate_ggtt(ggtt, &objects);
120 /* Everything is pinned, nothing should happen */
121 mutex_lock(&ggtt->vm.mutex);
122 err = i915_gem_evict_something(&ggtt->vm, NULL,
123 I915_GTT_PAGE_SIZE, 0, 0,
126 mutex_unlock(&ggtt->vm.mutex);
127 if (err != -ENOSPC) {
128 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
135 /* Everything is unpinned, we should be able to evict something */
136 mutex_lock(&ggtt->vm.mutex);
137 err = i915_gem_evict_something(&ggtt->vm, NULL,
138 I915_GTT_PAGE_SIZE, 0, 0,
141 mutex_unlock(&ggtt->vm.mutex);
143 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
149 cleanup_objects(ggtt, &objects);
153 static int igt_overcommit(void *arg)
155 struct intel_gt *gt = arg;
156 struct i915_ggtt *ggtt = gt->ggtt;
157 struct drm_i915_gem_object *obj;
158 struct i915_vma *vma;
162 /* Fill the GGTT with pinned objects and then try to pin one more.
163 * We expect it to fail.
166 err = populate_ggtt(ggtt, &objects);
170 obj = i915_gem_object_create_internal(gt->i915, I915_GTT_PAGE_SIZE);
176 quirk_add(obj, &objects);
178 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
179 if (vma != ERR_PTR(-ENOSPC)) {
180 pr_err("Failed to evict+insert, i915_gem_object_ggtt_pin returned err=%d\n", (int)PTR_ERR_OR_ZERO(vma));
186 cleanup_objects(ggtt, &objects);
190 static int igt_evict_for_vma(void *arg)
192 struct intel_gt *gt = arg;
193 struct i915_ggtt *ggtt = gt->ggtt;
194 struct drm_mm_node target = {
201 /* Fill the GGTT with pinned objects and try to evict a range. */
203 err = populate_ggtt(ggtt, &objects);
207 /* Everything is pinned, nothing should happen */
208 mutex_lock(&ggtt->vm.mutex);
209 err = i915_gem_evict_for_node(&ggtt->vm, NULL, &target, 0);
210 mutex_unlock(&ggtt->vm.mutex);
211 if (err != -ENOSPC) {
212 pr_err("i915_gem_evict_for_node on a full GGTT returned err=%d\n",
219 /* Everything is unpinned, we should be able to evict the node */
220 mutex_lock(&ggtt->vm.mutex);
221 err = i915_gem_evict_for_node(&ggtt->vm, NULL, &target, 0);
222 mutex_unlock(&ggtt->vm.mutex);
224 pr_err("i915_gem_evict_for_node returned err=%d\n",
230 cleanup_objects(ggtt, &objects);
234 static void mock_color_adjust(const struct drm_mm_node *node,
241 static int igt_evict_for_cache_color(void *arg)
243 struct intel_gt *gt = arg;
244 struct i915_ggtt *ggtt = gt->ggtt;
245 const unsigned long flags = PIN_OFFSET_FIXED;
246 struct drm_mm_node target = {
247 .start = I915_GTT_PAGE_SIZE * 2,
248 .size = I915_GTT_PAGE_SIZE,
249 .color = i915_gem_get_pat_index(gt->i915, I915_CACHE_LLC),
251 struct drm_i915_gem_object *obj;
252 struct i915_vma *vma;
257 * Currently the use of color_adjust for the GGTT is limited to cache
258 * coloring and guard pages, and so the presence of mm.color_adjust for
259 * the GGTT is assumed to be i915_ggtt_color_adjust, hence using a mock
260 * color adjust will work just fine for our purposes.
262 ggtt->vm.mm.color_adjust = mock_color_adjust;
263 GEM_BUG_ON(!i915_vm_has_cache_coloring(&ggtt->vm));
265 obj = i915_gem_object_create_internal(gt->i915, I915_GTT_PAGE_SIZE);
270 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
271 quirk_add(obj, &objects);
273 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
274 I915_GTT_PAGE_SIZE | flags);
276 pr_err("[0]i915_gem_object_ggtt_pin failed\n");
281 obj = i915_gem_object_create_internal(gt->i915, I915_GTT_PAGE_SIZE);
286 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
287 quirk_add(obj, &objects);
289 /* Neighbouring; same colour - should fit */
290 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
291 (I915_GTT_PAGE_SIZE * 2) | flags);
293 pr_err("[1]i915_gem_object_ggtt_pin failed\n");
300 /* Remove just the second vma */
301 mutex_lock(&ggtt->vm.mutex);
302 err = i915_gem_evict_for_node(&ggtt->vm, NULL, &target, 0);
303 mutex_unlock(&ggtt->vm.mutex);
305 pr_err("[0]i915_gem_evict_for_node returned err=%d\n", err);
309 /* Attempt to remove the first *pinned* vma, by removing the (empty)
310 * neighbour -- this should fail.
312 target.color = i915_gem_get_pat_index(gt->i915, I915_CACHE_L3_LLC);
314 mutex_lock(&ggtt->vm.mutex);
315 err = i915_gem_evict_for_node(&ggtt->vm, NULL, &target, 0);
316 mutex_unlock(&ggtt->vm.mutex);
318 pr_err("[1]i915_gem_evict_for_node returned err=%d\n", err);
327 cleanup_objects(ggtt, &objects);
328 ggtt->vm.mm.color_adjust = NULL;
332 static int igt_evict_vm(void *arg)
334 struct intel_gt *gt = arg;
335 struct i915_ggtt *ggtt = gt->ggtt;
336 struct i915_gem_ww_ctx ww;
340 /* Fill the GGTT with pinned objects and try to evict everything. */
342 err = populate_ggtt(ggtt, &objects);
346 /* Everything is pinned, nothing should happen */
347 mutex_lock(&ggtt->vm.mutex);
348 err = i915_gem_evict_vm(&ggtt->vm, NULL, NULL);
349 mutex_unlock(&ggtt->vm.mutex);
351 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
358 for_i915_gem_ww(&ww, err, false) {
359 mutex_lock(&ggtt->vm.mutex);
360 err = i915_gem_evict_vm(&ggtt->vm, &ww, NULL);
361 mutex_unlock(&ggtt->vm.mutex);
365 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
371 cleanup_objects(ggtt, &objects);
375 static int igt_evict_contexts(void *arg)
377 const u64 PRETEND_GGTT_SIZE = 16ull << 20;
378 struct intel_gt *gt = arg;
379 struct i915_ggtt *ggtt = gt->ggtt;
380 struct drm_i915_private *i915 = gt->i915;
381 struct intel_engine_cs *engine;
382 enum intel_engine_id id;
384 struct drm_mm_node node;
385 struct reserved *next;
387 intel_wakeref_t wakeref;
388 struct drm_mm_node hole;
393 * The purpose of this test is to verify that we will trigger an
394 * eviction in the GGTT when constructing a request that requires
395 * additional space in the GGTT for pinning the context. This space
396 * is not directly tied to the request so reclaiming it requires
399 * As such this test is only meaningful for full-ppgtt environments
400 * where the GTT space of the request is separate from the GGTT
401 * allocation required to build the request.
403 if (!HAS_FULL_PPGTT(i915))
406 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
408 /* Reserve a block so that we know we have enough to fit a few rq */
409 memset(&hole, 0, sizeof(hole));
410 mutex_lock(&ggtt->vm.mutex);
411 err = i915_gem_gtt_insert(&ggtt->vm, NULL, &hole,
412 PRETEND_GGTT_SIZE, 0, I915_COLOR_UNEVICTABLE,
418 /* Make the GGTT appear small by filling it with unevictable nodes */
423 mutex_unlock(&ggtt->vm.mutex);
424 r = kcalloc(1, sizeof(*r), GFP_KERNEL);
425 mutex_lock(&ggtt->vm.mutex);
431 if (i915_gem_gtt_insert(&ggtt->vm, NULL, &r->node,
432 1ul << 20, 0, I915_COLOR_UNEVICTABLE,
444 drm_mm_remove_node(&hole);
445 mutex_unlock(&ggtt->vm.mutex);
446 pr_info("Filled GGTT with %lu 1MiB nodes\n", count);
448 /* Overfill the GGTT with context objects and so try to evict one. */
449 for_each_engine(engine, gt, id) {
450 struct i915_sw_fence fence;
451 struct i915_request *last = NULL;
454 onstack_fence_init(&fence);
456 struct intel_context *ce;
457 struct i915_request *rq;
459 ce = intel_context_create(engine);
463 /* We will need some GGTT space for the rq's context */
464 igt_evict_ctl.fail_if_busy = true;
465 rq = intel_context_create_request(ce);
466 igt_evict_ctl.fail_if_busy = false;
467 intel_context_put(ce);
470 /* When full, fail_if_busy will trigger EBUSY */
471 if (PTR_ERR(rq) != -EBUSY) {
472 pr_err("Unexpected error from request alloc (on %s): %d\n",
480 /* Keep every request/ctx pinned until we are full */
481 err = i915_sw_fence_await_sw_fence_gfp(&rq->submit,
487 i915_request_add(rq);
490 i915_request_put(last);
491 last = i915_request_get(rq);
494 onstack_fence_fini(&fence);
495 pr_info("Submitted %lu contexts/requests on %s\n",
496 count, engine->name);
500 if (i915_request_wait(last, 0, HZ) < 0) {
502 i915_request_put(last);
503 pr_err("Failed waiting for last request (on %s)",
507 i915_request_put(last);
509 err = intel_gt_wait_for_idle(engine->gt, HZ * 3);
511 gt_err(engine->gt, "Failed to idle GT (on %s)",
517 mutex_lock(&ggtt->vm.mutex);
519 if (igt_flush_test(i915))
522 struct reserved *next = reserved->next;
524 drm_mm_remove_node(&reserved->node);
529 if (drm_mm_node_allocated(&hole))
530 drm_mm_remove_node(&hole);
531 mutex_unlock(&ggtt->vm.mutex);
532 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
537 int i915_gem_evict_mock_selftests(void)
539 static const struct i915_subtest tests[] = {
540 SUBTEST(igt_evict_something),
541 SUBTEST(igt_evict_for_vma),
542 SUBTEST(igt_evict_for_cache_color),
543 SUBTEST(igt_evict_vm),
544 SUBTEST(igt_overcommit),
546 struct drm_i915_private *i915;
547 intel_wakeref_t wakeref;
550 i915 = mock_gem_device();
554 with_intel_runtime_pm(&i915->runtime_pm, wakeref)
555 err = i915_subtests(tests, to_gt(i915));
557 mock_destroy_device(i915);
561 int i915_gem_evict_live_selftests(struct drm_i915_private *i915)
563 static const struct i915_subtest tests[] = {
564 SUBTEST(igt_evict_contexts),
567 if (intel_gt_is_wedged(to_gt(i915)))
570 return intel_gt_live_subtests(tests, to_gt(i915));