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_pm.h"
26 #include "gem/selftests/igt_gem_utils.h"
27 #include "gem/selftests/mock_context.h"
28 #include "gt/intel_gt.h"
30 #include "i915_selftest.h"
32 #include "igt_flush_test.h"
33 #include "lib_sw_fence.h"
35 #include "mock_gem_device.h"
37 static void quirk_add(struct drm_i915_gem_object *obj,
38 struct list_head *objects)
40 /* quirk is only for live tiled objects, use it to declare ownership */
41 GEM_BUG_ON(i915_gem_object_has_tiling_quirk(obj));
42 i915_gem_object_set_tiling_quirk(obj);
43 list_add(&obj->st_link, objects);
46 static int populate_ggtt(struct i915_ggtt *ggtt, struct list_head *objects)
48 struct drm_i915_gem_object *obj;
55 obj = i915_gem_object_create_internal(ggtt->vm.i915,
60 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
62 i915_gem_object_put(obj);
63 if (vma == ERR_PTR(-ENOSPC))
69 quirk_add(obj, objects);
72 pr_debug("Filled GGTT with %lu pages [%llu total]\n",
73 count, ggtt->vm.total / PAGE_SIZE);
75 if (list_empty(&ggtt->vm.bound_list)) {
76 pr_err("No objects on the GGTT inactive list!\n");
83 static void unpin_ggtt(struct i915_ggtt *ggtt)
87 list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
88 if (i915_gem_object_has_tiling_quirk(vma->obj))
92 static void cleanup_objects(struct i915_ggtt *ggtt, struct list_head *list)
94 struct drm_i915_gem_object *obj, *on;
96 list_for_each_entry_safe(obj, on, list, st_link) {
97 GEM_BUG_ON(!i915_gem_object_has_tiling_quirk(obj));
98 i915_gem_object_set_tiling_quirk(obj);
99 i915_gem_object_put(obj);
102 i915_gem_drain_freed_objects(ggtt->vm.i915);
105 static int igt_evict_something(void *arg)
107 struct intel_gt *gt = arg;
108 struct i915_ggtt *ggtt = gt->ggtt;
112 /* Fill the GGTT with pinned objects and try to evict one. */
114 err = populate_ggtt(ggtt, &objects);
118 /* Everything is pinned, nothing should happen */
119 mutex_lock(&ggtt->vm.mutex);
120 err = i915_gem_evict_something(&ggtt->vm,
121 I915_GTT_PAGE_SIZE, 0, 0,
124 mutex_unlock(&ggtt->vm.mutex);
125 if (err != -ENOSPC) {
126 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
133 /* Everything is unpinned, we should be able to evict something */
134 mutex_lock(&ggtt->vm.mutex);
135 err = i915_gem_evict_something(&ggtt->vm,
136 I915_GTT_PAGE_SIZE, 0, 0,
139 mutex_unlock(&ggtt->vm.mutex);
141 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
147 cleanup_objects(ggtt, &objects);
151 static int igt_overcommit(void *arg)
153 struct intel_gt *gt = arg;
154 struct i915_ggtt *ggtt = gt->ggtt;
155 struct drm_i915_gem_object *obj;
156 struct i915_vma *vma;
160 /* Fill the GGTT with pinned objects and then try to pin one more.
161 * We expect it to fail.
164 err = populate_ggtt(ggtt, &objects);
168 obj = i915_gem_object_create_internal(gt->i915, I915_GTT_PAGE_SIZE);
174 quirk_add(obj, &objects);
176 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
177 if (vma != ERR_PTR(-ENOSPC)) {
178 pr_err("Failed to evict+insert, i915_gem_object_ggtt_pin returned err=%d\n", (int)PTR_ERR_OR_ZERO(vma));
184 cleanup_objects(ggtt, &objects);
188 static int igt_evict_for_vma(void *arg)
190 struct intel_gt *gt = arg;
191 struct i915_ggtt *ggtt = gt->ggtt;
192 struct drm_mm_node target = {
199 /* Fill the GGTT with pinned objects and try to evict a range. */
201 err = populate_ggtt(ggtt, &objects);
205 /* Everything is pinned, nothing should happen */
206 mutex_lock(&ggtt->vm.mutex);
207 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
208 mutex_unlock(&ggtt->vm.mutex);
209 if (err != -ENOSPC) {
210 pr_err("i915_gem_evict_for_node on a full GGTT returned err=%d\n",
217 /* Everything is unpinned, we should be able to evict the node */
218 mutex_lock(&ggtt->vm.mutex);
219 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
220 mutex_unlock(&ggtt->vm.mutex);
222 pr_err("i915_gem_evict_for_node returned err=%d\n",
228 cleanup_objects(ggtt, &objects);
232 static void mock_color_adjust(const struct drm_mm_node *node,
239 static int igt_evict_for_cache_color(void *arg)
241 struct intel_gt *gt = arg;
242 struct i915_ggtt *ggtt = gt->ggtt;
243 const unsigned long flags = PIN_OFFSET_FIXED;
244 struct drm_mm_node target = {
245 .start = I915_GTT_PAGE_SIZE * 2,
246 .size = I915_GTT_PAGE_SIZE,
247 .color = I915_CACHE_LLC,
249 struct drm_i915_gem_object *obj;
250 struct i915_vma *vma;
255 * Currently the use of color_adjust for the GGTT is limited to cache
256 * coloring and guard pages, and so the presence of mm.color_adjust for
257 * the GGTT is assumed to be i915_ggtt_color_adjust, hence using a mock
258 * color adjust will work just fine for our purposes.
260 ggtt->vm.mm.color_adjust = mock_color_adjust;
261 GEM_BUG_ON(!i915_vm_has_cache_coloring(&ggtt->vm));
263 obj = i915_gem_object_create_internal(gt->i915, I915_GTT_PAGE_SIZE);
268 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
269 quirk_add(obj, &objects);
271 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
272 I915_GTT_PAGE_SIZE | flags);
274 pr_err("[0]i915_gem_object_ggtt_pin failed\n");
279 obj = i915_gem_object_create_internal(gt->i915, I915_GTT_PAGE_SIZE);
284 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
285 quirk_add(obj, &objects);
287 /* Neighbouring; same colour - should fit */
288 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
289 (I915_GTT_PAGE_SIZE * 2) | flags);
291 pr_err("[1]i915_gem_object_ggtt_pin failed\n");
298 /* Remove just the second vma */
299 mutex_lock(&ggtt->vm.mutex);
300 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
301 mutex_unlock(&ggtt->vm.mutex);
303 pr_err("[0]i915_gem_evict_for_node returned err=%d\n", err);
307 /* Attempt to remove the first *pinned* vma, by removing the (empty)
308 * neighbour -- this should fail.
310 target.color = I915_CACHE_L3_LLC;
312 mutex_lock(&ggtt->vm.mutex);
313 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
314 mutex_unlock(&ggtt->vm.mutex);
316 pr_err("[1]i915_gem_evict_for_node returned err=%d\n", err);
325 cleanup_objects(ggtt, &objects);
326 ggtt->vm.mm.color_adjust = NULL;
330 static int igt_evict_vm(void *arg)
332 struct intel_gt *gt = arg;
333 struct i915_ggtt *ggtt = gt->ggtt;
337 /* Fill the GGTT with pinned objects and try to evict everything. */
339 err = populate_ggtt(ggtt, &objects);
343 /* Everything is pinned, nothing should happen */
344 mutex_lock(&ggtt->vm.mutex);
345 err = i915_gem_evict_vm(&ggtt->vm);
346 mutex_unlock(&ggtt->vm.mutex);
348 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
355 mutex_lock(&ggtt->vm.mutex);
356 err = i915_gem_evict_vm(&ggtt->vm);
357 mutex_unlock(&ggtt->vm.mutex);
359 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
365 cleanup_objects(ggtt, &objects);
369 static int igt_evict_contexts(void *arg)
371 const u64 PRETEND_GGTT_SIZE = 16ull << 20;
372 struct intel_gt *gt = arg;
373 struct i915_ggtt *ggtt = gt->ggtt;
374 struct drm_i915_private *i915 = gt->i915;
375 struct intel_engine_cs *engine;
376 enum intel_engine_id id;
378 struct drm_mm_node node;
379 struct reserved *next;
381 intel_wakeref_t wakeref;
382 struct drm_mm_node hole;
387 * The purpose of this test is to verify that we will trigger an
388 * eviction in the GGTT when constructing a request that requires
389 * additional space in the GGTT for pinning the context. This space
390 * is not directly tied to the request so reclaiming it requires
393 * As such this test is only meaningful for full-ppgtt environments
394 * where the GTT space of the request is separate from the GGTT
395 * allocation required to build the request.
397 if (!HAS_FULL_PPGTT(i915))
400 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
402 /* Reserve a block so that we know we have enough to fit a few rq */
403 memset(&hole, 0, sizeof(hole));
404 mutex_lock(&ggtt->vm.mutex);
405 err = i915_gem_gtt_insert(&ggtt->vm, &hole,
406 PRETEND_GGTT_SIZE, 0, I915_COLOR_UNEVICTABLE,
412 /* Make the GGTT appear small by filling it with unevictable nodes */
417 mutex_unlock(&ggtt->vm.mutex);
418 r = kcalloc(1, sizeof(*r), GFP_KERNEL);
419 mutex_lock(&ggtt->vm.mutex);
425 if (i915_gem_gtt_insert(&ggtt->vm, &r->node,
426 1ul << 20, 0, I915_COLOR_UNEVICTABLE,
438 drm_mm_remove_node(&hole);
439 mutex_unlock(&ggtt->vm.mutex);
440 pr_info("Filled GGTT with %lu 1MiB nodes\n", count);
442 /* Overfill the GGTT with context objects and so try to evict one. */
443 for_each_engine(engine, gt, id) {
444 struct i915_sw_fence fence;
447 onstack_fence_init(&fence);
449 struct intel_context *ce;
450 struct i915_request *rq;
452 ce = intel_context_create(engine);
456 /* We will need some GGTT space for the rq's context */
457 igt_evict_ctl.fail_if_busy = true;
458 rq = intel_context_create_request(ce);
459 igt_evict_ctl.fail_if_busy = false;
460 intel_context_put(ce);
463 /* When full, fail_if_busy will trigger EBUSY */
464 if (PTR_ERR(rq) != -EBUSY) {
465 pr_err("Unexpected error from request alloc (on %s): %d\n",
473 /* Keep every request/ctx pinned until we are full */
474 err = i915_sw_fence_await_sw_fence_gfp(&rq->submit,
480 i915_request_add(rq);
484 onstack_fence_fini(&fence);
485 pr_info("Submitted %lu contexts/requests on %s\n",
486 count, engine->name);
491 mutex_lock(&ggtt->vm.mutex);
493 if (igt_flush_test(i915))
496 struct reserved *next = reserved->next;
498 drm_mm_remove_node(&reserved->node);
503 if (drm_mm_node_allocated(&hole))
504 drm_mm_remove_node(&hole);
505 mutex_unlock(&ggtt->vm.mutex);
506 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
511 int i915_gem_evict_mock_selftests(void)
513 static const struct i915_subtest tests[] = {
514 SUBTEST(igt_evict_something),
515 SUBTEST(igt_evict_for_vma),
516 SUBTEST(igt_evict_for_cache_color),
517 SUBTEST(igt_evict_vm),
518 SUBTEST(igt_overcommit),
520 struct drm_i915_private *i915;
521 intel_wakeref_t wakeref;
524 i915 = mock_gem_device();
528 with_intel_runtime_pm(&i915->runtime_pm, wakeref)
529 err = i915_subtests(tests, &i915->gt);
531 mock_destroy_device(i915);
535 int i915_gem_evict_live_selftests(struct drm_i915_private *i915)
537 static const struct i915_subtest tests[] = {
538 SUBTEST(igt_evict_contexts),
541 if (intel_gt_is_wedged(&i915->gt))
544 return intel_gt_live_subtests(tests, &i915->gt);