1 /*
2  * Copyright © 2016 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  *
23  */
24 
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"
29 
30 #include "i915_selftest.h"
31 
32 #include "igt_flush_test.h"
33 #include "lib_sw_fence.h"
34 #include "mock_drm.h"
35 #include "mock_gem_device.h"
36 
37 static void quirk_add(struct drm_i915_gem_object *obj,
38 		      struct list_head *objects)
39 {
40 	/* quirk is only for live tiled objects, use it to declare ownership */
41 	GEM_BUG_ON(obj->mm.quirked);
42 	obj->mm.quirked = true;
43 	list_add(&obj->st_link, objects);
44 }
45 
46 static int populate_ggtt(struct drm_i915_private *i915,
47 			 struct list_head *objects)
48 {
49 	unsigned long unbound, bound, count;
50 	struct drm_i915_gem_object *obj;
51 	u64 size;
52 
53 	count = 0;
54 	for (size = 0;
55 	     size + I915_GTT_PAGE_SIZE <= i915->ggtt.vm.total;
56 	     size += I915_GTT_PAGE_SIZE) {
57 		struct i915_vma *vma;
58 
59 		obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
60 		if (IS_ERR(obj))
61 			return PTR_ERR(obj);
62 
63 		quirk_add(obj, objects);
64 
65 		vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
66 		if (IS_ERR(vma))
67 			return PTR_ERR(vma);
68 
69 		count++;
70 	}
71 
72 	bound = 0;
73 	unbound = 0;
74 	list_for_each_entry(obj, objects, st_link) {
75 		GEM_BUG_ON(!obj->mm.quirked);
76 
77 		if (atomic_read(&obj->bind_count))
78 			bound++;
79 		else
80 			unbound++;
81 	}
82 	GEM_BUG_ON(bound + unbound != count);
83 
84 	if (unbound) {
85 		pr_err("%s: Found %lu objects unbound, expected %u!\n",
86 		       __func__, unbound, 0);
87 		return -EINVAL;
88 	}
89 
90 	if (bound != count) {
91 		pr_err("%s: Found %lu objects bound, expected %lu!\n",
92 		       __func__, bound, count);
93 		return -EINVAL;
94 	}
95 
96 	if (list_empty(&i915->ggtt.vm.bound_list)) {
97 		pr_err("No objects on the GGTT inactive list!\n");
98 		return -EINVAL;
99 	}
100 
101 	return 0;
102 }
103 
104 static void unpin_ggtt(struct drm_i915_private *i915)
105 {
106 	struct i915_ggtt *ggtt = &i915->ggtt;
107 	struct i915_vma *vma;
108 
109 	mutex_lock(&ggtt->vm.mutex);
110 	list_for_each_entry(vma, &i915->ggtt.vm.bound_list, vm_link)
111 		if (vma->obj->mm.quirked)
112 			i915_vma_unpin(vma);
113 	mutex_unlock(&ggtt->vm.mutex);
114 }
115 
116 static void cleanup_objects(struct drm_i915_private *i915,
117 			    struct list_head *list)
118 {
119 	struct drm_i915_gem_object *obj, *on;
120 
121 	list_for_each_entry_safe(obj, on, list, st_link) {
122 		GEM_BUG_ON(!obj->mm.quirked);
123 		obj->mm.quirked = false;
124 		i915_gem_object_put(obj);
125 	}
126 
127 	mutex_unlock(&i915->drm.struct_mutex);
128 
129 	i915_gem_drain_freed_objects(i915);
130 
131 	mutex_lock(&i915->drm.struct_mutex);
132 }
133 
134 static int igt_evict_something(void *arg)
135 {
136 	struct drm_i915_private *i915 = arg;
137 	struct i915_ggtt *ggtt = &i915->ggtt;
138 	LIST_HEAD(objects);
139 	int err;
140 
141 	/* Fill the GGTT with pinned objects and try to evict one. */
142 
143 	err = populate_ggtt(i915, &objects);
144 	if (err)
145 		goto cleanup;
146 
147 	/* Everything is pinned, nothing should happen */
148 	err = i915_gem_evict_something(&ggtt->vm,
149 				       I915_GTT_PAGE_SIZE, 0, 0,
150 				       0, U64_MAX,
151 				       0);
152 	if (err != -ENOSPC) {
153 		pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
154 		       err);
155 		goto cleanup;
156 	}
157 
158 	unpin_ggtt(i915);
159 
160 	/* Everything is unpinned, we should be able to evict something */
161 	err = i915_gem_evict_something(&ggtt->vm,
162 				       I915_GTT_PAGE_SIZE, 0, 0,
163 				       0, U64_MAX,
164 				       0);
165 	if (err) {
166 		pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
167 		       err);
168 		goto cleanup;
169 	}
170 
171 cleanup:
172 	cleanup_objects(i915, &objects);
173 	return err;
174 }
175 
176 static int igt_overcommit(void *arg)
177 {
178 	struct drm_i915_private *i915 = arg;
179 	struct drm_i915_gem_object *obj;
180 	struct i915_vma *vma;
181 	LIST_HEAD(objects);
182 	int err;
183 
184 	/* Fill the GGTT with pinned objects and then try to pin one more.
185 	 * We expect it to fail.
186 	 */
187 
188 	err = populate_ggtt(i915, &objects);
189 	if (err)
190 		goto cleanup;
191 
192 	obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
193 	if (IS_ERR(obj)) {
194 		err = PTR_ERR(obj);
195 		goto cleanup;
196 	}
197 
198 	quirk_add(obj, &objects);
199 
200 	vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
201 	if (!IS_ERR(vma) || PTR_ERR(vma) != -ENOSPC) {
202 		pr_err("Failed to evict+insert, i915_gem_object_ggtt_pin returned err=%d\n", (int)PTR_ERR(vma));
203 		err = -EINVAL;
204 		goto cleanup;
205 	}
206 
207 cleanup:
208 	cleanup_objects(i915, &objects);
209 	return err;
210 }
211 
212 static int igt_evict_for_vma(void *arg)
213 {
214 	struct drm_i915_private *i915 = arg;
215 	struct i915_ggtt *ggtt = &i915->ggtt;
216 	struct drm_mm_node target = {
217 		.start = 0,
218 		.size = 4096,
219 	};
220 	LIST_HEAD(objects);
221 	int err;
222 
223 	/* Fill the GGTT with pinned objects and try to evict a range. */
224 
225 	err = populate_ggtt(i915, &objects);
226 	if (err)
227 		goto cleanup;
228 
229 	/* Everything is pinned, nothing should happen */
230 	err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
231 	if (err != -ENOSPC) {
232 		pr_err("i915_gem_evict_for_node on a full GGTT returned err=%d\n",
233 		       err);
234 		goto cleanup;
235 	}
236 
237 	unpin_ggtt(i915);
238 
239 	/* Everything is unpinned, we should be able to evict the node */
240 	err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
241 	if (err) {
242 		pr_err("i915_gem_evict_for_node returned err=%d\n",
243 		       err);
244 		goto cleanup;
245 	}
246 
247 cleanup:
248 	cleanup_objects(i915, &objects);
249 	return err;
250 }
251 
252 static void mock_color_adjust(const struct drm_mm_node *node,
253 			      unsigned long color,
254 			      u64 *start,
255 			      u64 *end)
256 {
257 }
258 
259 static int igt_evict_for_cache_color(void *arg)
260 {
261 	struct drm_i915_private *i915 = arg;
262 	struct i915_ggtt *ggtt = &i915->ggtt;
263 	const unsigned long flags = PIN_OFFSET_FIXED;
264 	struct drm_mm_node target = {
265 		.start = I915_GTT_PAGE_SIZE * 2,
266 		.size = I915_GTT_PAGE_SIZE,
267 		.color = I915_CACHE_LLC,
268 	};
269 	struct drm_i915_gem_object *obj;
270 	struct i915_vma *vma;
271 	LIST_HEAD(objects);
272 	int err;
273 
274 	/* Currently the use of color_adjust is limited to cache domains within
275 	 * the ggtt, and so the presence of mm.color_adjust is assumed to be
276 	 * i915_gtt_color_adjust throughout our driver, so using a mock color
277 	 * adjust will work just fine for our purposes.
278 	 */
279 	ggtt->vm.mm.color_adjust = mock_color_adjust;
280 
281 	obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
282 	if (IS_ERR(obj)) {
283 		err = PTR_ERR(obj);
284 		goto cleanup;
285 	}
286 	i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
287 	quirk_add(obj, &objects);
288 
289 	vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
290 				       I915_GTT_PAGE_SIZE | flags);
291 	if (IS_ERR(vma)) {
292 		pr_err("[0]i915_gem_object_ggtt_pin failed\n");
293 		err = PTR_ERR(vma);
294 		goto cleanup;
295 	}
296 
297 	obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
298 	if (IS_ERR(obj)) {
299 		err = PTR_ERR(obj);
300 		goto cleanup;
301 	}
302 	i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
303 	quirk_add(obj, &objects);
304 
305 	/* Neighbouring; same colour - should fit */
306 	vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
307 				       (I915_GTT_PAGE_SIZE * 2) | flags);
308 	if (IS_ERR(vma)) {
309 		pr_err("[1]i915_gem_object_ggtt_pin failed\n");
310 		err = PTR_ERR(vma);
311 		goto cleanup;
312 	}
313 
314 	i915_vma_unpin(vma);
315 
316 	/* Remove just the second vma */
317 	err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
318 	if (err) {
319 		pr_err("[0]i915_gem_evict_for_node returned err=%d\n", err);
320 		goto cleanup;
321 	}
322 
323 	/* Attempt to remove the first *pinned* vma, by removing the (empty)
324 	 * neighbour -- this should fail.
325 	 */
326 	target.color = I915_CACHE_L3_LLC;
327 
328 	err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
329 	if (!err) {
330 		pr_err("[1]i915_gem_evict_for_node returned err=%d\n", err);
331 		err = -EINVAL;
332 		goto cleanup;
333 	}
334 
335 	err = 0;
336 
337 cleanup:
338 	unpin_ggtt(i915);
339 	cleanup_objects(i915, &objects);
340 	ggtt->vm.mm.color_adjust = NULL;
341 	return err;
342 }
343 
344 static int igt_evict_vm(void *arg)
345 {
346 	struct drm_i915_private *i915 = arg;
347 	struct i915_ggtt *ggtt = &i915->ggtt;
348 	LIST_HEAD(objects);
349 	int err;
350 
351 	/* Fill the GGTT with pinned objects and try to evict everything. */
352 
353 	err = populate_ggtt(i915, &objects);
354 	if (err)
355 		goto cleanup;
356 
357 	/* Everything is pinned, nothing should happen */
358 	err = i915_gem_evict_vm(&ggtt->vm);
359 	if (err) {
360 		pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
361 		       err);
362 		goto cleanup;
363 	}
364 
365 	unpin_ggtt(i915);
366 
367 	err = i915_gem_evict_vm(&ggtt->vm);
368 	if (err) {
369 		pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
370 		       err);
371 		goto cleanup;
372 	}
373 
374 cleanup:
375 	cleanup_objects(i915, &objects);
376 	return err;
377 }
378 
379 static int igt_evict_contexts(void *arg)
380 {
381 	const u64 PRETEND_GGTT_SIZE = 16ull << 20;
382 	struct drm_i915_private *i915 = arg;
383 	struct intel_engine_cs *engine;
384 	enum intel_engine_id id;
385 	struct reserved {
386 		struct drm_mm_node node;
387 		struct reserved *next;
388 	} *reserved = NULL;
389 	intel_wakeref_t wakeref;
390 	struct drm_mm_node hole;
391 	unsigned long count;
392 	int err;
393 
394 	/*
395 	 * The purpose of this test is to verify that we will trigger an
396 	 * eviction in the GGTT when constructing a request that requires
397 	 * additional space in the GGTT for pinning the context. This space
398 	 * is not directly tied to the request so reclaiming it requires
399 	 * extra work.
400 	 *
401 	 * As such this test is only meaningful for full-ppgtt environments
402 	 * where the GTT space of the request is separate from the GGTT
403 	 * allocation required to build the request.
404 	 */
405 	if (!HAS_FULL_PPGTT(i915))
406 		return 0;
407 
408 	mutex_lock(&i915->drm.struct_mutex);
409 	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
410 
411 	/* Reserve a block so that we know we have enough to fit a few rq */
412 	memset(&hole, 0, sizeof(hole));
413 	err = i915_gem_gtt_insert(&i915->ggtt.vm, &hole,
414 				  PRETEND_GGTT_SIZE, 0, I915_COLOR_UNEVICTABLE,
415 				  0, i915->ggtt.vm.total,
416 				  PIN_NOEVICT);
417 	if (err)
418 		goto out_locked;
419 
420 	/* Make the GGTT appear small by filling it with unevictable nodes */
421 	count = 0;
422 	do {
423 		struct reserved *r;
424 
425 		r = kcalloc(1, sizeof(*r), GFP_KERNEL);
426 		if (!r) {
427 			err = -ENOMEM;
428 			goto out_locked;
429 		}
430 
431 		if (i915_gem_gtt_insert(&i915->ggtt.vm, &r->node,
432 					1ul << 20, 0, I915_COLOR_UNEVICTABLE,
433 					0, i915->ggtt.vm.total,
434 					PIN_NOEVICT)) {
435 			kfree(r);
436 			break;
437 		}
438 
439 		r->next = reserved;
440 		reserved = r;
441 
442 		count++;
443 	} while (1);
444 	drm_mm_remove_node(&hole);
445 	mutex_unlock(&i915->drm.struct_mutex);
446 	pr_info("Filled GGTT with %lu 1MiB nodes\n", count);
447 
448 	/* Overfill the GGTT with context objects and so try to evict one. */
449 	for_each_engine(engine, i915, id) {
450 		struct i915_sw_fence fence;
451 		struct drm_file *file;
452 
453 		file = mock_file(i915);
454 		if (IS_ERR(file)) {
455 			err = PTR_ERR(file);
456 			break;
457 		}
458 
459 		count = 0;
460 		mutex_lock(&i915->drm.struct_mutex);
461 		onstack_fence_init(&fence);
462 		do {
463 			struct i915_request *rq;
464 			struct i915_gem_context *ctx;
465 
466 			ctx = live_context(i915, file);
467 			if (IS_ERR(ctx))
468 				break;
469 
470 			/* We will need some GGTT space for the rq's context */
471 			igt_evict_ctl.fail_if_busy = true;
472 			rq = igt_request_alloc(ctx, engine);
473 			igt_evict_ctl.fail_if_busy = false;
474 
475 			if (IS_ERR(rq)) {
476 				/* When full, fail_if_busy will trigger EBUSY */
477 				if (PTR_ERR(rq) != -EBUSY) {
478 					pr_err("Unexpected error from request alloc (ctx hw id %u, on %s): %d\n",
479 					       ctx->hw_id, engine->name,
480 					       (int)PTR_ERR(rq));
481 					err = PTR_ERR(rq);
482 				}
483 				break;
484 			}
485 
486 			/* Keep every request/ctx pinned until we are full */
487 			err = i915_sw_fence_await_sw_fence_gfp(&rq->submit,
488 							       &fence,
489 							       GFP_KERNEL);
490 			if (err < 0)
491 				break;
492 
493 			i915_request_add(rq);
494 			count++;
495 			err = 0;
496 		} while(1);
497 		mutex_unlock(&i915->drm.struct_mutex);
498 
499 		onstack_fence_fini(&fence);
500 		pr_info("Submitted %lu contexts/requests on %s\n",
501 			count, engine->name);
502 
503 		mock_file_free(i915, file);
504 		if (err)
505 			break;
506 	}
507 
508 	mutex_lock(&i915->drm.struct_mutex);
509 out_locked:
510 	if (igt_flush_test(i915, I915_WAIT_LOCKED))
511 		err = -EIO;
512 	while (reserved) {
513 		struct reserved *next = reserved->next;
514 
515 		drm_mm_remove_node(&reserved->node);
516 		kfree(reserved);
517 
518 		reserved = next;
519 	}
520 	if (drm_mm_node_allocated(&hole))
521 		drm_mm_remove_node(&hole);
522 	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
523 	mutex_unlock(&i915->drm.struct_mutex);
524 
525 	return err;
526 }
527 
528 int i915_gem_evict_mock_selftests(void)
529 {
530 	static const struct i915_subtest tests[] = {
531 		SUBTEST(igt_evict_something),
532 		SUBTEST(igt_evict_for_vma),
533 		SUBTEST(igt_evict_for_cache_color),
534 		SUBTEST(igt_evict_vm),
535 		SUBTEST(igt_overcommit),
536 	};
537 	struct drm_i915_private *i915;
538 	intel_wakeref_t wakeref;
539 	int err = 0;
540 
541 	i915 = mock_gem_device();
542 	if (!i915)
543 		return -ENOMEM;
544 
545 	mutex_lock(&i915->drm.struct_mutex);
546 	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
547 		err = i915_subtests(tests, i915);
548 
549 	mutex_unlock(&i915->drm.struct_mutex);
550 
551 	drm_dev_put(&i915->drm);
552 	return err;
553 }
554 
555 int i915_gem_evict_live_selftests(struct drm_i915_private *i915)
556 {
557 	static const struct i915_subtest tests[] = {
558 		SUBTEST(igt_evict_contexts),
559 	};
560 
561 	if (intel_gt_is_wedged(&i915->gt))
562 		return 0;
563 
564 	return i915_subtests(tests, i915);
565 }
566