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 int populate_ggtt(struct drm_i915_private *i915)
34 struct drm_i915_gem_object *obj;
38 size + I915_GTT_PAGE_SIZE <= i915->ggtt.base.total;
39 size += I915_GTT_PAGE_SIZE) {
42 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
46 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
51 if (!list_empty(&i915->mm.unbound_list)) {
53 list_for_each_entry(obj, &i915->mm.unbound_list, mm.link)
56 pr_err("Found %lld objects unbound!\n", size);
60 if (list_empty(&i915->ggtt.base.inactive_list)) {
61 pr_err("No objects on the GGTT inactive list!\n");
68 static void unpin_ggtt(struct drm_i915_private *i915)
72 list_for_each_entry(vma, &i915->ggtt.base.inactive_list, vm_link)
76 static void cleanup_objects(struct drm_i915_private *i915)
78 struct drm_i915_gem_object *obj, *on;
80 list_for_each_entry_safe(obj, on, &i915->mm.unbound_list, mm.link)
81 i915_gem_object_put(obj);
83 list_for_each_entry_safe(obj, on, &i915->mm.bound_list, mm.link)
84 i915_gem_object_put(obj);
86 mutex_unlock(&i915->drm.struct_mutex);
88 i915_gem_drain_freed_objects(i915);
90 mutex_lock(&i915->drm.struct_mutex);
93 static int igt_evict_something(void *arg)
95 struct drm_i915_private *i915 = arg;
96 struct i915_ggtt *ggtt = &i915->ggtt;
99 /* Fill the GGTT with pinned objects and try to evict one. */
101 err = populate_ggtt(i915);
105 /* Everything is pinned, nothing should happen */
106 err = i915_gem_evict_something(&ggtt->base,
107 I915_GTT_PAGE_SIZE, 0, 0,
110 if (err != -ENOSPC) {
111 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
118 /* Everything is unpinned, we should be able to evict something */
119 err = i915_gem_evict_something(&ggtt->base,
120 I915_GTT_PAGE_SIZE, 0, 0,
124 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
130 cleanup_objects(i915);
134 static int igt_overcommit(void *arg)
136 struct drm_i915_private *i915 = arg;
137 struct drm_i915_gem_object *obj;
138 struct i915_vma *vma;
141 /* Fill the GGTT with pinned objects and then try to pin one more.
142 * We expect it to fail.
145 err = populate_ggtt(i915);
149 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
155 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
156 if (!IS_ERR(vma) || PTR_ERR(vma) != -ENOSPC) {
157 pr_err("Failed to evict+insert, i915_gem_object_ggtt_pin returned err=%d\n", (int)PTR_ERR(vma));
163 cleanup_objects(i915);
167 static int igt_evict_for_vma(void *arg)
169 struct drm_i915_private *i915 = arg;
170 struct i915_ggtt *ggtt = &i915->ggtt;
171 struct drm_mm_node target = {
177 /* Fill the GGTT with pinned objects and try to evict a range. */
179 err = populate_ggtt(i915);
183 /* Everything is pinned, nothing should happen */
184 err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
185 if (err != -ENOSPC) {
186 pr_err("i915_gem_evict_for_node on a full GGTT returned err=%d\n",
193 /* Everything is unpinned, we should be able to evict the node */
194 err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
196 pr_err("i915_gem_evict_for_node returned err=%d\n",
202 cleanup_objects(i915);
206 static void mock_color_adjust(const struct drm_mm_node *node,
213 static int igt_evict_for_cache_color(void *arg)
215 struct drm_i915_private *i915 = arg;
216 struct i915_ggtt *ggtt = &i915->ggtt;
217 const unsigned long flags = PIN_OFFSET_FIXED;
218 struct drm_mm_node target = {
219 .start = I915_GTT_PAGE_SIZE * 2,
220 .size = I915_GTT_PAGE_SIZE,
221 .color = I915_CACHE_LLC,
223 struct drm_i915_gem_object *obj;
224 struct i915_vma *vma;
227 /* Currently the use of color_adjust is limited to cache domains within
228 * the ggtt, and so the presence of mm.color_adjust is assumed to be
229 * i915_gtt_color_adjust throughout our driver, so using a mock color
230 * adjust will work just fine for our purposes.
232 ggtt->base.mm.color_adjust = mock_color_adjust;
234 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
239 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
241 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
242 I915_GTT_PAGE_SIZE | flags);
244 pr_err("[0]i915_gem_object_ggtt_pin failed\n");
249 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
254 i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
256 /* Neighbouring; same colour - should fit */
257 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
258 (I915_GTT_PAGE_SIZE * 2) | flags);
260 pr_err("[1]i915_gem_object_ggtt_pin failed\n");
267 /* Remove just the second vma */
268 err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
270 pr_err("[0]i915_gem_evict_for_node returned err=%d\n", err);
274 /* Attempt to remove the first *pinned* vma, by removing the (empty)
275 * neighbour -- this should fail.
277 target.color = I915_CACHE_L3_LLC;
279 err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
281 pr_err("[1]i915_gem_evict_for_node returned err=%d\n", err);
290 cleanup_objects(i915);
291 ggtt->base.mm.color_adjust = NULL;
295 static int igt_evict_vm(void *arg)
297 struct drm_i915_private *i915 = arg;
298 struct i915_ggtt *ggtt = &i915->ggtt;
301 /* Fill the GGTT with pinned objects and try to evict everything. */
303 err = populate_ggtt(i915);
307 /* Everything is pinned, nothing should happen */
308 err = i915_gem_evict_vm(&ggtt->base);
310 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
317 err = i915_gem_evict_vm(&ggtt->base);
319 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
325 cleanup_objects(i915);
329 static int igt_evict_contexts(void *arg)
331 const u64 PRETEND_GGTT_SIZE = 16ull << 20;
332 struct drm_i915_private *i915 = arg;
333 struct intel_engine_cs *engine;
334 enum intel_engine_id id;
336 struct drm_mm_node node;
337 struct reserved *next;
339 struct drm_mm_node hole;
344 * The purpose of this test is to verify that we will trigger an
345 * eviction in the GGTT when constructing a request that requires
346 * additional space in the GGTT for pinning the context. This space
347 * is not directly tied to the request so reclaiming it requires
350 * As such this test is only meaningful for full-ppgtt environments
351 * where the GTT space of the request is separate from the GGTT
352 * allocation required to build the request.
354 if (!USES_FULL_PPGTT(i915))
357 mutex_lock(&i915->drm.struct_mutex);
358 intel_runtime_pm_get(i915);
360 /* Reserve a block so that we know we have enough to fit a few rq */
361 memset(&hole, 0, sizeof(hole));
362 err = i915_gem_gtt_insert(&i915->ggtt.base, &hole,
363 PRETEND_GGTT_SIZE, 0, I915_COLOR_UNEVICTABLE,
364 0, i915->ggtt.base.total,
369 /* Make the GGTT appear small by filling it with unevictable nodes */
374 r = kcalloc(1, sizeof(*r), GFP_KERNEL);
380 if (i915_gem_gtt_insert(&i915->ggtt.base, &r->node,
381 1ul << 20, 0, I915_COLOR_UNEVICTABLE,
382 0, i915->ggtt.base.total,
393 drm_mm_remove_node(&hole);
394 mutex_unlock(&i915->drm.struct_mutex);
395 pr_info("Filled GGTT with %lu 1MiB nodes\n", count);
397 /* Overfill the GGTT with context objects and so try to evict one. */
398 for_each_engine(engine, i915, id) {
399 struct i915_sw_fence fence;
400 struct drm_file *file;
402 file = mock_file(i915);
404 return PTR_ERR(file);
407 mutex_lock(&i915->drm.struct_mutex);
408 onstack_fence_init(&fence);
410 struct i915_request *rq;
411 struct i915_gem_context *ctx;
413 ctx = live_context(i915, file);
417 /* We will need some GGTT space for the rq's context */
418 igt_evict_ctl.fail_if_busy = true;
419 rq = i915_request_alloc(engine, ctx);
420 igt_evict_ctl.fail_if_busy = false;
423 /* When full, fail_if_busy will trigger EBUSY */
424 if (PTR_ERR(rq) != -EBUSY) {
425 pr_err("Unexpected error from request alloc (ctx hw id %u, on %s): %d\n",
426 ctx->hw_id, engine->name,
433 /* Keep every request/ctx pinned until we are full */
434 err = i915_sw_fence_await_sw_fence_gfp(&rq->submit,
440 i915_request_add(rq);
444 mutex_unlock(&i915->drm.struct_mutex);
446 onstack_fence_fini(&fence);
447 pr_info("Submitted %lu contexts/requests on %s\n",
448 count, engine->name);
450 mock_file_free(i915, file);
455 mutex_lock(&i915->drm.struct_mutex);
458 struct reserved *next = reserved->next;
460 drm_mm_remove_node(&reserved->node);
465 if (drm_mm_node_allocated(&hole))
466 drm_mm_remove_node(&hole);
467 intel_runtime_pm_put(i915);
468 mutex_unlock(&i915->drm.struct_mutex);
473 int i915_gem_evict_mock_selftests(void)
475 static const struct i915_subtest tests[] = {
476 SUBTEST(igt_evict_something),
477 SUBTEST(igt_evict_for_vma),
478 SUBTEST(igt_evict_for_cache_color),
479 SUBTEST(igt_evict_vm),
480 SUBTEST(igt_overcommit),
482 struct drm_i915_private *i915;
485 i915 = mock_gem_device();
489 mutex_lock(&i915->drm.struct_mutex);
490 err = i915_subtests(tests, i915);
491 mutex_unlock(&i915->drm.struct_mutex);
493 drm_dev_unref(&i915->drm);
497 int i915_gem_evict_live_selftests(struct drm_i915_private *i915)
499 static const struct i915_subtest tests[] = {
500 SUBTEST(igt_evict_contexts),
503 return i915_subtests(tests, i915);