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"
29 #include "i915_selftest.h"
31 #include "lib_sw_fence.h"
33 #include "mock_gem_device.h"
35 static void quirk_add(struct drm_i915_gem_object *obj,
36 struct list_head *objects)
38 /* quirk is only for live tiled objects, use it to declare ownership */
39 GEM_BUG_ON(obj->mm.quirked);
40 obj->mm.quirked = true;
41 list_add(&obj->st_link, objects);
44 static int populate_ggtt(struct drm_i915_private *i915,
45 struct list_head *objects)
47 unsigned long unbound, bound, count;
48 struct drm_i915_gem_object *obj;
53 size + I915_GTT_PAGE_SIZE <= i915->ggtt.vm.total;
54 size += I915_GTT_PAGE_SIZE) {
57 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
61 quirk_add(obj, objects);
63 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
72 list_for_each_entry(obj, objects, st_link) {
73 GEM_BUG_ON(!obj->mm.quirked);
75 if (atomic_read(&obj->bind_count))
80 GEM_BUG_ON(bound + unbound != count);
83 pr_err("%s: Found %lu objects unbound, expected %u!\n",
84 __func__, unbound, 0);
89 pr_err("%s: Found %lu objects bound, expected %lu!\n",
90 __func__, bound, count);
94 if (list_empty(&i915->ggtt.vm.bound_list)) {
95 pr_err("No objects on the GGTT inactive list!\n");
102 static void unpin_ggtt(struct drm_i915_private *i915)
104 struct i915_ggtt *ggtt = &i915->ggtt;
105 struct i915_vma *vma;
107 mutex_lock(&ggtt->vm.mutex);
108 list_for_each_entry(vma, &i915->ggtt.vm.bound_list, vm_link)
109 if (vma->obj->mm.quirked)
111 mutex_unlock(&ggtt->vm.mutex);
114 static void cleanup_objects(struct drm_i915_private *i915,
115 struct list_head *list)
117 struct drm_i915_gem_object *obj, *on;
119 list_for_each_entry_safe(obj, on, list, st_link) {
120 GEM_BUG_ON(!obj->mm.quirked);
121 obj->mm.quirked = false;
122 i915_gem_object_put(obj);
125 mutex_unlock(&i915->drm.struct_mutex);
127 i915_gem_drain_freed_objects(i915);
129 mutex_lock(&i915->drm.struct_mutex);
132 static int igt_evict_something(void *arg)
134 struct drm_i915_private *i915 = arg;
135 struct i915_ggtt *ggtt = &i915->ggtt;
139 /* Fill the GGTT with pinned objects and try to evict one. */
141 err = populate_ggtt(i915, &objects);
145 /* Everything is pinned, nothing should happen */
146 err = i915_gem_evict_something(&ggtt->vm,
147 I915_GTT_PAGE_SIZE, 0, 0,
150 if (err != -ENOSPC) {
151 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
158 /* Everything is unpinned, we should be able to evict something */
159 err = i915_gem_evict_something(&ggtt->vm,
160 I915_GTT_PAGE_SIZE, 0, 0,
164 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
170 cleanup_objects(i915, &objects);
174 static int igt_overcommit(void *arg)
176 struct drm_i915_private *i915 = arg;
177 struct drm_i915_gem_object *obj;
178 struct i915_vma *vma;
182 /* Fill the GGTT with pinned objects and then try to pin one more.
183 * We expect it to fail.
186 err = populate_ggtt(i915, &objects);
190 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
196 quirk_add(obj, &objects);
198 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
199 if (!IS_ERR(vma) || PTR_ERR(vma) != -ENOSPC) {
200 pr_err("Failed to evict+insert, i915_gem_object_ggtt_pin returned err=%d\n", (int)PTR_ERR(vma));
206 cleanup_objects(i915, &objects);
210 static int igt_evict_for_vma(void *arg)
212 struct drm_i915_private *i915 = arg;
213 struct i915_ggtt *ggtt = &i915->ggtt;
214 struct drm_mm_node target = {
221 /* Fill the GGTT with pinned objects and try to evict a range. */
223 err = populate_ggtt(i915, &objects);
227 /* Everything is pinned, nothing should happen */
228 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
229 if (err != -ENOSPC) {
230 pr_err("i915_gem_evict_for_node on a full GGTT returned err=%d\n",
237 /* Everything is unpinned, we should be able to evict the node */
238 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
240 pr_err("i915_gem_evict_for_node returned err=%d\n",
246 cleanup_objects(i915, &objects);
250 static void mock_color_adjust(const struct drm_mm_node *node,
257 static int igt_evict_for_cache_color(void *arg)
259 struct drm_i915_private *i915 = arg;
260 struct i915_ggtt *ggtt = &i915->ggtt;
261 const unsigned long flags = PIN_OFFSET_FIXED;
262 struct drm_mm_node target = {
263 .start = I915_GTT_PAGE_SIZE * 2,
264 .size = I915_GTT_PAGE_SIZE,
265 .color = I915_CACHE_LLC,
267 struct drm_i915_gem_object *obj;
268 struct i915_vma *vma;
272 /* Currently the use of color_adjust is limited to cache domains within
273 * the ggtt, and so the presence of mm.color_adjust is assumed to be
274 * i915_gtt_color_adjust throughout our driver, so using a mock color
275 * adjust will work just fine for our purposes.
277 ggtt->vm.mm.color_adjust = mock_color_adjust;
279 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
284 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
285 quirk_add(obj, &objects);
287 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
288 I915_GTT_PAGE_SIZE | flags);
290 pr_err("[0]i915_gem_object_ggtt_pin failed\n");
295 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
300 i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
301 quirk_add(obj, &objects);
303 /* Neighbouring; same colour - should fit */
304 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
305 (I915_GTT_PAGE_SIZE * 2) | flags);
307 pr_err("[1]i915_gem_object_ggtt_pin failed\n");
314 /* Remove just the second vma */
315 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
317 pr_err("[0]i915_gem_evict_for_node returned err=%d\n", err);
321 /* Attempt to remove the first *pinned* vma, by removing the (empty)
322 * neighbour -- this should fail.
324 target.color = I915_CACHE_L3_LLC;
326 err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
328 pr_err("[1]i915_gem_evict_for_node returned err=%d\n", err);
337 cleanup_objects(i915, &objects);
338 ggtt->vm.mm.color_adjust = NULL;
342 static int igt_evict_vm(void *arg)
344 struct drm_i915_private *i915 = arg;
345 struct i915_ggtt *ggtt = &i915->ggtt;
349 /* Fill the GGTT with pinned objects and try to evict everything. */
351 err = populate_ggtt(i915, &objects);
355 /* Everything is pinned, nothing should happen */
356 err = i915_gem_evict_vm(&ggtt->vm);
358 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
365 err = i915_gem_evict_vm(&ggtt->vm);
367 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
373 cleanup_objects(i915, &objects);
377 static int igt_evict_contexts(void *arg)
379 const u64 PRETEND_GGTT_SIZE = 16ull << 20;
380 struct drm_i915_private *i915 = arg;
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 mutex_lock(&i915->drm.struct_mutex);
407 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
409 /* Reserve a block so that we know we have enough to fit a few rq */
410 memset(&hole, 0, sizeof(hole));
411 err = i915_gem_gtt_insert(&i915->ggtt.vm, &hole,
412 PRETEND_GGTT_SIZE, 0, I915_COLOR_UNEVICTABLE,
413 0, i915->ggtt.vm.total,
418 /* Make the GGTT appear small by filling it with unevictable nodes */
423 r = kcalloc(1, sizeof(*r), GFP_KERNEL);
429 if (i915_gem_gtt_insert(&i915->ggtt.vm, &r->node,
430 1ul << 20, 0, I915_COLOR_UNEVICTABLE,
431 0, i915->ggtt.vm.total,
442 drm_mm_remove_node(&hole);
443 mutex_unlock(&i915->drm.struct_mutex);
444 pr_info("Filled GGTT with %lu 1MiB nodes\n", count);
446 /* Overfill the GGTT with context objects and so try to evict one. */
447 for_each_engine(engine, i915, id) {
448 struct i915_sw_fence fence;
449 struct drm_file *file;
451 file = mock_file(i915);
458 mutex_lock(&i915->drm.struct_mutex);
459 onstack_fence_init(&fence);
461 struct i915_request *rq;
462 struct i915_gem_context *ctx;
464 ctx = live_context(i915, file);
468 /* We will need some GGTT space for the rq's context */
469 igt_evict_ctl.fail_if_busy = true;
470 rq = igt_request_alloc(ctx, engine);
471 igt_evict_ctl.fail_if_busy = false;
474 /* When full, fail_if_busy will trigger EBUSY */
475 if (PTR_ERR(rq) != -EBUSY) {
476 pr_err("Unexpected error from request alloc (ctx hw id %u, on %s): %d\n",
477 ctx->hw_id, engine->name,
484 /* Keep every request/ctx pinned until we are full */
485 err = i915_sw_fence_await_sw_fence_gfp(&rq->submit,
491 i915_request_add(rq);
495 mutex_unlock(&i915->drm.struct_mutex);
497 onstack_fence_fini(&fence);
498 pr_info("Submitted %lu contexts/requests on %s\n",
499 count, engine->name);
501 mock_file_free(i915, file);
506 mutex_lock(&i915->drm.struct_mutex);
509 struct reserved *next = reserved->next;
511 drm_mm_remove_node(&reserved->node);
516 if (drm_mm_node_allocated(&hole))
517 drm_mm_remove_node(&hole);
518 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
519 mutex_unlock(&i915->drm.struct_mutex);
524 int i915_gem_evict_mock_selftests(void)
526 static const struct i915_subtest tests[] = {
527 SUBTEST(igt_evict_something),
528 SUBTEST(igt_evict_for_vma),
529 SUBTEST(igt_evict_for_cache_color),
530 SUBTEST(igt_evict_vm),
531 SUBTEST(igt_overcommit),
533 struct drm_i915_private *i915;
534 intel_wakeref_t wakeref;
537 i915 = mock_gem_device();
541 mutex_lock(&i915->drm.struct_mutex);
542 with_intel_runtime_pm(&i915->runtime_pm, wakeref)
543 err = i915_subtests(tests, i915);
545 mutex_unlock(&i915->drm.struct_mutex);
547 drm_dev_put(&i915->drm);
551 int i915_gem_evict_live_selftests(struct drm_i915_private *i915)
553 static const struct i915_subtest tests[] = {
554 SUBTEST(igt_evict_contexts),
557 if (i915_terminally_wedged(i915))
560 return i915_subtests(tests, i915);