Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2020-2021 Intel Corporation
 */

#include "gt/intel_migrate.h"
#include "gt/intel_gpu_commands.h"
#include "gem/i915_gem_ttm_move.h"

#include "i915_deps.h"

#include "selftests/igt_reset.h"
#include "selftests/igt_spinner.h"

static int igt_fill_check_buffer(struct drm_i915_gem_object *obj,
				 bool fill)
{
	struct drm_i915_private *i915 = to_i915(obj->base.dev);
	unsigned int i, count = obj->base.size / sizeof(u32);
	enum i915_map_type map_type =
		i915_coherent_map_type(i915, obj, false);
	u32 *cur;
	int err = 0;

	assert_object_held(obj);
	cur = i915_gem_object_pin_map(obj, map_type);
	if (IS_ERR(cur))
		return PTR_ERR(cur);

	if (fill)
		for (i = 0; i < count; ++i)
			*cur++ = i;
	else
		for (i = 0; i < count; ++i)
			if (*cur++ != i) {
				pr_err("Object content mismatch at location %d of %d\n", i, count);
				err = -EINVAL;
				break;
			}

	i915_gem_object_unpin_map(obj);

	return err;
}

static int igt_create_migrate(struct intel_gt *gt, enum intel_region_id src,
			      enum intel_region_id dst)
{
	struct drm_i915_private *i915 = gt->i915;
	struct intel_memory_region *src_mr = i915->mm.regions[src];
	struct intel_memory_region *dst_mr = i915->mm.regions[dst];
	struct drm_i915_gem_object *obj;
	struct i915_gem_ww_ctx ww;
	int err = 0;

	GEM_BUG_ON(!src_mr);
	GEM_BUG_ON(!dst_mr);

	/* Switch object backing-store on create */
	obj = i915_gem_object_create_region(src_mr, dst_mr->min_page_size, 0, 0);
	if (IS_ERR(obj))
		return PTR_ERR(obj);

	for_i915_gem_ww(&ww, err, true) {
		err = i915_gem_object_lock(obj, &ww);
		if (err)
			continue;

		err = igt_fill_check_buffer(obj, true);
		if (err)
			continue;

		err = i915_gem_object_migrate(obj, &ww, dst);
		if (err)
			continue;

		err = i915_gem_object_pin_pages(obj);
		if (err)
			continue;

		if (i915_gem_object_can_migrate(obj, src))
			err = -EINVAL;

		i915_gem_object_unpin_pages(obj);
		err = i915_gem_object_wait_migration(obj, true);
		if (err)
			continue;

		err = igt_fill_check_buffer(obj, false);
	}
	i915_gem_object_put(obj);

	return err;
}

static int igt_smem_create_migrate(void *arg)
{
	return igt_create_migrate(arg, INTEL_REGION_LMEM_0, INTEL_REGION_SMEM);
}

static int igt_lmem_create_migrate(void *arg)
{
	return igt_create_migrate(arg, INTEL_REGION_SMEM, INTEL_REGION_LMEM_0);
}

static int igt_same_create_migrate(void *arg)
{
	return igt_create_migrate(arg, INTEL_REGION_LMEM_0, INTEL_REGION_LMEM_0);
}

static int lmem_pages_migrate_one(struct i915_gem_ww_ctx *ww,
				  struct drm_i915_gem_object *obj,
				  struct i915_vma *vma,
				  bool silent_migrate)
{
	int err;

	err = i915_gem_object_lock(obj, ww);
	if (err)
		return err;

	if (vma) {
		err = i915_vma_pin_ww(vma, ww, obj->base.size, 0,
				      0UL | PIN_OFFSET_FIXED |
				      PIN_USER);
		if (err) {
			if (err != -EINTR && err != ERESTARTSYS &&
			    err != -EDEADLK)
				pr_err("Failed to pin vma.\n");
			return err;
		}

		i915_vma_unpin(vma);
	}

	/*
	 * Migration will implicitly unbind (asynchronously) any bound
	 * vmas.
	 */
	if (i915_gem_object_is_lmem(obj)) {
		err = i915_gem_object_migrate(obj, ww, INTEL_REGION_SMEM);
		if (err) {
			if (!silent_migrate)
				pr_err("Object failed migration to smem\n");
			if (err)
				return err;
		}

		if (i915_gem_object_is_lmem(obj)) {
			pr_err("object still backed by lmem\n");
			err = -EINVAL;
		}

		if (!i915_gem_object_has_struct_page(obj)) {
			pr_err("object not backed by struct page\n");
			err = -EINVAL;
		}

	} else {
		err = i915_gem_object_migrate(obj, ww, INTEL_REGION_LMEM_0);
		if (err) {
			if (!silent_migrate)
				pr_err("Object failed migration to lmem\n");
			if (err)
				return err;
		}

		if (i915_gem_object_has_struct_page(obj)) {
			pr_err("object still backed by struct page\n");
			err = -EINVAL;
		}

		if (!i915_gem_object_is_lmem(obj)) {
			pr_err("object not backed by lmem\n");
			err = -EINVAL;
		}
	}

	return err;
}

static int __igt_lmem_pages_migrate(struct intel_gt *gt,
				    struct i915_address_space *vm,
				    struct i915_deps *deps,
				    struct igt_spinner *spin,
				    struct dma_fence *spin_fence,
				    bool borked_migrate)
{
	struct drm_i915_private *i915 = gt->i915;
	struct drm_i915_gem_object *obj;
	struct i915_vma *vma = NULL;
	struct i915_gem_ww_ctx ww;
	struct i915_request *rq;
	int err;
	int i;

	/* From LMEM to shmem and back again */

	obj = i915_gem_object_create_lmem(i915, SZ_2M, 0);
	if (IS_ERR(obj))
		return PTR_ERR(obj);

	if (vm) {
		vma = i915_vma_instance(obj, vm, NULL);
		if (IS_ERR(vma)) {
			err = PTR_ERR(vma);
			goto out_put;
		}
	}

	/* Initial GPU fill, sync, CPU initialization. */
	for_i915_gem_ww(&ww, err, true) {
		err = i915_gem_object_lock(obj, &ww);
		if (err)
			continue;

		err = ____i915_gem_object_get_pages(obj);
		if (err)
			continue;

		err = intel_migrate_clear(&gt->migrate, &ww, deps,
					  obj->mm.pages->sgl, obj->cache_level,
					  i915_gem_object_is_lmem(obj),
					  0xdeadbeaf, &rq);
		if (rq) {
			err = dma_resv_reserve_fences(obj->base.resv, 1);
			if (!err)
				dma_resv_add_fence(obj->base.resv, &rq->fence,
						   DMA_RESV_USAGE_KERNEL);
			i915_request_put(rq);
		}
		if (err)
			continue;

		if (!vma) {
			err = igt_fill_check_buffer(obj, true);
			if (err)
				continue;
		}
	}
	if (err)
		goto out_put;

	/*
	 * Migrate to and from smem without explicitly syncing.
	 * Finalize with data in smem for fast readout.
	 */
	for (i = 1; i <= 5; ++i) {
		for_i915_gem_ww(&ww, err, true)
			err = lmem_pages_migrate_one(&ww, obj, vma,
						     borked_migrate);
		if (err)
			goto out_put;
	}

	err = i915_gem_object_lock_interruptible(obj, NULL);
	if (err)
		goto out_put;

	if (spin) {
		if (dma_fence_is_signaled(spin_fence)) {
			pr_err("Spinner was terminated by hangcheck.\n");
			err = -EBUSY;
			goto out_unlock;
		}
		igt_spinner_end(spin);
	}

	/* Finally sync migration and check content. */
	err = i915_gem_object_wait_migration(obj, true);
	if (err)
		goto out_unlock;

	if (vma) {
		err = i915_vma_wait_for_bind(vma);
		if (err)
			goto out_unlock;
	} else {
		err = igt_fill_check_buffer(obj, false);
	}

out_unlock:
	i915_gem_object_unlock(obj);
out_put:
	i915_gem_object_put(obj);

	return err;
}

static int igt_lmem_pages_failsafe_migrate(void *arg)
{
	int fail_gpu, fail_alloc, ban_memcpy, ret;
	struct intel_gt *gt = arg;

	for (fail_gpu = 0; fail_gpu < 2; ++fail_gpu) {
		for (fail_alloc = 0; fail_alloc < 2; ++fail_alloc) {
			for (ban_memcpy = 0; ban_memcpy < 2; ++ban_memcpy) {
				pr_info("Simulated failure modes: gpu: %d, alloc:%d, ban_memcpy: %d\n",
					fail_gpu, fail_alloc, ban_memcpy);
				i915_ttm_migrate_set_ban_memcpy(ban_memcpy);
				i915_ttm_migrate_set_failure_modes(fail_gpu,
								   fail_alloc);
				ret = __igt_lmem_pages_migrate(gt, NULL, NULL,
							       NULL, NULL,
							       ban_memcpy &&
							       fail_gpu);

				if (ban_memcpy && fail_gpu) {
					struct intel_gt *__gt;
					unsigned int id;

					if (ret != -EIO) {
						pr_err("expected -EIO, got (%d)\n", ret);
						ret = -EINVAL;
					} else {
						ret = 0;
					}

					for_each_gt(__gt, gt->i915, id) {
						intel_wakeref_t wakeref;
						bool wedged;

						mutex_lock(&__gt->reset.mutex);
						wedged = test_bit(I915_WEDGED, &__gt->reset.flags);
						mutex_unlock(&__gt->reset.mutex);

						if (fail_gpu && !fail_alloc) {
							if (!wedged) {
								pr_err("gt(%u) not wedged\n", id);
								ret = -EINVAL;
								continue;
							}
						} else if (wedged) {
							pr_err("gt(%u) incorrectly wedged\n", id);
							ret = -EINVAL;
						} else {
							continue;
						}

						wakeref = intel_runtime_pm_get(__gt->uncore->rpm);
						igt_global_reset_lock(__gt);
						intel_gt_reset(__gt, ALL_ENGINES, NULL);
						igt_global_reset_unlock(__gt);
						intel_runtime_pm_put(__gt->uncore->rpm, wakeref);
					}
					if (ret)
						goto out_err;
				}
			}
		}
	}

out_err:
	i915_ttm_migrate_set_failure_modes(false, false);
	i915_ttm_migrate_set_ban_memcpy(false);
	return ret;
}

/*
 * This subtest tests that unbinding at migration is indeed performed
 * async. We launch a spinner and a number of migrations depending on
 * that spinner to have terminated. Before each migration we bind a
 * vma, which should then be async unbound by the migration operation.
 * If we are able to schedule migrations without blocking while the
 * spinner is still running, those unbinds are indeed async and non-
 * blocking.
 *
 * Note that each async bind operation is awaiting the previous migration
 * due to the moving fence resulting from the migration.
 */
static int igt_async_migrate(struct intel_gt *gt)
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	struct i915_ppgtt *ppgtt;
	struct igt_spinner spin;
	int err;

	ppgtt = i915_ppgtt_create(gt, 0);
	if (IS_ERR(ppgtt))
		return PTR_ERR(ppgtt);

	if (igt_spinner_init(&spin, gt)) {
		err = -ENOMEM;
		goto out_spin;
	}

	for_each_engine(engine, gt, id) {
		struct ttm_operation_ctx ctx = {
			.interruptible = true
		};
		struct dma_fence *spin_fence;
		struct intel_context *ce;
		struct i915_request *rq;
		struct i915_deps deps;

		ce = intel_context_create(engine);
		if (IS_ERR(ce)) {
			err = PTR_ERR(ce);
			goto out_ce;
		}

		/*
		 * Use MI_NOOP, making the spinner non-preemptible. If there
		 * is a code path where we fail async operation due to the
		 * running spinner, we will block and fail to end the
		 * spinner resulting in a deadlock. But with a non-
		 * preemptible spinner, hangcheck will terminate the spinner
		 * for us, and we will later detect that and fail the test.
		 */
		rq = igt_spinner_create_request(&spin, ce, MI_NOOP);
		intel_context_put(ce);
		if (IS_ERR(rq)) {
			err = PTR_ERR(rq);
			goto out_ce;
		}

		i915_deps_init(&deps, GFP_KERNEL);
		err = i915_deps_add_dependency(&deps, &rq->fence, &ctx);
		spin_fence = dma_fence_get(&rq->fence);
		i915_request_add(rq);
		if (err)
			goto out_ce;

		err = __igt_lmem_pages_migrate(gt, &ppgtt->vm, &deps, &spin,
					       spin_fence, false);
		i915_deps_fini(&deps);
		dma_fence_put(spin_fence);
		if (err)
			goto out_ce;
	}

out_ce:
	igt_spinner_fini(&spin);
out_spin:
	i915_vm_put(&ppgtt->vm);

	return err;
}

/*
 * Setting ASYNC_FAIL_ALLOC to 2 will simulate memory allocation failure while
 * arming the migration error check and block async migration. This
 * will cause us to deadlock and hangcheck will terminate the spinner
 * causing the test to fail.
 */
#define ASYNC_FAIL_ALLOC 1
static int igt_lmem_async_migrate(void *arg)
{
	int fail_gpu, fail_alloc, ban_memcpy, ret;
	struct intel_gt *gt = arg;

	for (fail_gpu = 0; fail_gpu < 2; ++fail_gpu) {
		for (fail_alloc = 0; fail_alloc < ASYNC_FAIL_ALLOC; ++fail_alloc) {
			for (ban_memcpy = 0; ban_memcpy < 2; ++ban_memcpy) {
				pr_info("Simulated failure modes: gpu: %d, alloc: %d, ban_memcpy: %d\n",
					fail_gpu, fail_alloc, ban_memcpy);
				i915_ttm_migrate_set_ban_memcpy(ban_memcpy);
				i915_ttm_migrate_set_failure_modes(fail_gpu,
								   fail_alloc);
				ret = igt_async_migrate(gt);

				if (fail_gpu && ban_memcpy) {
					struct intel_gt *__gt;
					unsigned int id;

					if (ret != -EIO) {
						pr_err("expected -EIO, got (%d)\n", ret);
						ret = -EINVAL;
					} else {
						ret = 0;
					}

					for_each_gt(__gt, gt->i915, id) {
						intel_wakeref_t wakeref;
						bool wedged;

						mutex_lock(&__gt->reset.mutex);
						wedged = test_bit(I915_WEDGED, &__gt->reset.flags);
						mutex_unlock(&__gt->reset.mutex);

						if (fail_gpu && !fail_alloc) {
							if (!wedged) {
								pr_err("gt(%u) not wedged\n", id);
								ret = -EINVAL;
								continue;
							}
						} else if (wedged) {
							pr_err("gt(%u) incorrectly wedged\n", id);
							ret = -EINVAL;
						} else {
							continue;
						}

						wakeref = intel_runtime_pm_get(__gt->uncore->rpm);
						igt_global_reset_lock(__gt);
						intel_gt_reset(__gt, ALL_ENGINES, NULL);
						igt_global_reset_unlock(__gt);
						intel_runtime_pm_put(__gt->uncore->rpm, wakeref);
					}
				}
				if (ret)
					goto out_err;
			}
		}
	}

out_err:
	i915_ttm_migrate_set_failure_modes(false, false);
	i915_ttm_migrate_set_ban_memcpy(false);
	return ret;
}

int i915_gem_migrate_live_selftests(struct drm_i915_private *i915)
{
	static const struct i915_subtest tests[] = {
		SUBTEST(igt_smem_create_migrate),
		SUBTEST(igt_lmem_create_migrate),
		SUBTEST(igt_same_create_migrate),
		SUBTEST(igt_lmem_pages_failsafe_migrate),
		SUBTEST(igt_lmem_async_migrate),
	};

	if (!HAS_LMEM(i915))
		return 0;

	return intel_gt_live_subtests(tests, to_gt(i915));
}