xref: /linux/drivers/gpu/drm/xe/tests/xe_migrate.c (revision 99676aed1fec109d62822e21a06760eb098dc5f4)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2020-2022 Intel Corporation
4  */
5 
6 #include <kunit/test.h>
7 #include <kunit/visibility.h>
8 
9 #include "tests/xe_kunit_helpers.h"
10 #include "tests/xe_pci_test.h"
11 
12 #include "xe_pat.h"
13 #include "xe_pci.h"
14 #include "xe_pm.h"
15 
16 static bool sanity_fence_failed(struct xe_device *xe, struct dma_fence *fence,
17 				const char *str, struct kunit *test)
18 {
19 	long ret;
20 
21 	if (IS_ERR(fence)) {
22 		KUNIT_FAIL(test, "Failed to create fence for %s: %li\n", str,
23 			   PTR_ERR(fence));
24 		return true;
25 	}
26 	if (!fence)
27 		return true;
28 
29 	ret = dma_fence_wait_timeout(fence, false, 5 * HZ);
30 	if (ret <= 0) {
31 		KUNIT_FAIL(test, "Fence timed out for %s: %li\n", str, ret);
32 		return true;
33 	}
34 
35 	return false;
36 }
37 
38 static int run_sanity_job(struct xe_migrate *m, struct xe_device *xe,
39 			  struct xe_bb *bb, u32 second_idx, const char *str,
40 			  struct kunit *test)
41 {
42 	u64 batch_base = xe_migrate_batch_base(m, xe->info.has_usm);
43 	struct xe_sched_job *job = xe_bb_create_migration_job(m->q, bb,
44 							      batch_base,
45 							      second_idx);
46 	struct dma_fence *fence;
47 
48 	if (IS_ERR(job)) {
49 		KUNIT_FAIL(test, "Failed to allocate fake pt: %li\n",
50 			   PTR_ERR(job));
51 		return PTR_ERR(job);
52 	}
53 
54 	xe_sched_job_arm(job);
55 	fence = dma_fence_get(&job->drm.s_fence->finished);
56 	xe_sched_job_push(job);
57 
58 	if (sanity_fence_failed(xe, fence, str, test))
59 		return -ETIMEDOUT;
60 
61 	dma_fence_put(fence);
62 	kunit_info(test, "%s: Job completed\n", str);
63 	return 0;
64 }
65 
66 #define check(_retval, _expected, str, _test)				\
67 	do { if ((_retval) != (_expected)) {				\
68 			KUNIT_FAIL(_test, "Sanity check failed: " str	\
69 				   " expected %llx, got %llx\n",	\
70 				   (u64)(_expected), (u64)(_retval));	\
71 		} } while (0)
72 
73 static void test_copy(struct xe_migrate *m, struct xe_bo *bo,
74 		      struct kunit *test, u32 region, struct drm_exec *exec)
75 {
76 	struct xe_device *xe = tile_to_xe(m->tile);
77 	u64 retval, expected = 0;
78 	bool big = xe_bo_size(bo) >= SZ_2M;
79 	struct dma_fence *fence;
80 	const char *str = big ? "Copying big bo" : "Copying small bo";
81 	int err;
82 
83 	struct xe_bo *remote = xe_bo_create_locked(xe, m->tile, NULL,
84 						   xe_bo_size(bo),
85 						   ttm_bo_type_kernel,
86 						   region |
87 						   XE_BO_FLAG_NEEDS_CPU_ACCESS |
88 						   XE_BO_FLAG_PINNED,
89 						   exec);
90 	if (IS_ERR(remote)) {
91 		KUNIT_FAIL(test, "Failed to allocate remote bo for %s: %pe\n",
92 			   str, remote);
93 		return;
94 	}
95 
96 	err = xe_bo_validate(remote, NULL, false, exec);
97 	if (err) {
98 		KUNIT_FAIL(test, "Failed to validate system bo for %s: %i\n",
99 			   str, err);
100 		goto out_unlock;
101 	}
102 
103 	err = xe_bo_vmap(remote);
104 	if (err) {
105 		KUNIT_FAIL(test, "Failed to vmap system bo for %s: %i\n",
106 			   str, err);
107 		goto out_unlock;
108 	}
109 
110 	xe_map_memset(xe, &remote->vmap, 0, 0xd0, xe_bo_size(remote));
111 	fence = xe_migrate_clear(m, remote, remote->ttm.resource,
112 				 XE_MIGRATE_CLEAR_FLAG_FULL);
113 	if (!sanity_fence_failed(xe, fence, big ? "Clearing remote big bo" :
114 				 "Clearing remote small bo", test)) {
115 		retval = xe_map_rd(xe, &remote->vmap, 0, u64);
116 		check(retval, expected, "remote first offset should be cleared",
117 		      test);
118 		retval = xe_map_rd(xe, &remote->vmap, xe_bo_size(remote) - 8, u64);
119 		check(retval, expected, "remote last offset should be cleared",
120 		      test);
121 	}
122 	dma_fence_put(fence);
123 
124 	/* Try to copy 0xc0 from remote to vram with 2MB or 64KiB/4KiB pages */
125 	xe_map_memset(xe, &remote->vmap, 0, 0xc0, xe_bo_size(remote));
126 	xe_map_memset(xe, &bo->vmap, 0, 0xd0, xe_bo_size(bo));
127 
128 	expected = 0xc0c0c0c0c0c0c0c0;
129 	fence = xe_migrate_copy(m, remote, bo, remote->ttm.resource,
130 				bo->ttm.resource, false);
131 	if (!sanity_fence_failed(xe, fence, big ? "Copying big bo remote -> vram" :
132 				 "Copying small bo remote -> vram", test)) {
133 		retval = xe_map_rd(xe, &bo->vmap, 0, u64);
134 		check(retval, expected,
135 		      "remote -> vram bo first offset should be copied", test);
136 		retval = xe_map_rd(xe, &bo->vmap, xe_bo_size(bo) - 8, u64);
137 		check(retval, expected,
138 		      "remote -> vram bo offset should be copied", test);
139 	}
140 	dma_fence_put(fence);
141 
142 	/* And other way around.. slightly hacky.. */
143 	xe_map_memset(xe, &remote->vmap, 0, 0xd0, xe_bo_size(remote));
144 	xe_map_memset(xe, &bo->vmap, 0, 0xc0, xe_bo_size(bo));
145 
146 	fence = xe_migrate_copy(m, bo, remote, bo->ttm.resource,
147 				remote->ttm.resource, false);
148 	if (!sanity_fence_failed(xe, fence, big ? "Copying big bo vram -> remote" :
149 				 "Copying small bo vram -> remote", test)) {
150 		retval = xe_map_rd(xe, &remote->vmap, 0, u64);
151 		check(retval, expected,
152 		      "vram -> remote bo first offset should be copied", test);
153 		retval = xe_map_rd(xe, &remote->vmap, xe_bo_size(bo) - 8, u64);
154 		check(retval, expected,
155 		      "vram -> remote bo last offset should be copied", test);
156 	}
157 	dma_fence_put(fence);
158 
159 	xe_bo_vunmap(remote);
160 out_unlock:
161 	xe_bo_unlock(remote);
162 	xe_bo_put(remote);
163 }
164 
165 static void test_copy_sysmem(struct xe_migrate *m, struct xe_bo *bo,
166 			     struct drm_exec *exec, struct kunit *test)
167 {
168 	test_copy(m, bo, test, XE_BO_FLAG_SYSTEM, exec);
169 }
170 
171 static void test_copy_vram(struct xe_migrate *m, struct xe_bo *bo,
172 			   struct drm_exec *exec, struct kunit *test)
173 {
174 	u32 region;
175 
176 	if (bo->ttm.resource->mem_type == XE_PL_SYSTEM)
177 		return;
178 
179 	if (bo->ttm.resource->mem_type == XE_PL_VRAM0)
180 		region = XE_BO_FLAG_VRAM1;
181 	else
182 		region = XE_BO_FLAG_VRAM0;
183 	test_copy(m, bo, test, region, exec);
184 }
185 
186 static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test,
187 				   struct drm_exec *exec)
188 {
189 	struct xe_tile *tile = m->tile;
190 	struct xe_device *xe = tile_to_xe(tile);
191 	struct xe_bo *pt, *bo = m->pt_bo, *big, *tiny;
192 	struct xe_res_cursor src_it;
193 	struct dma_fence *fence;
194 	u64 retval, expected;
195 	struct xe_bb *bb;
196 	int err;
197 	u8 id = tile->id;
198 
199 	err = xe_bo_vmap(bo);
200 	if (err) {
201 		KUNIT_FAIL(test, "Failed to vmap our pagetables: %li\n",
202 			   PTR_ERR(bo));
203 		return;
204 	}
205 
206 	big = xe_bo_create_pin_map(xe, tile, m->q->vm, SZ_4M,
207 				   ttm_bo_type_kernel,
208 				   XE_BO_FLAG_VRAM_IF_DGFX(tile),
209 				   exec);
210 	if (IS_ERR(big)) {
211 		KUNIT_FAIL(test, "Failed to allocate bo: %li\n", PTR_ERR(big));
212 		goto vunmap;
213 	}
214 
215 	pt = xe_bo_create_pin_map(xe, tile, m->q->vm, XE_PAGE_SIZE,
216 				  ttm_bo_type_kernel,
217 				  XE_BO_FLAG_VRAM_IF_DGFX(tile),
218 				  exec);
219 	if (IS_ERR(pt)) {
220 		KUNIT_FAIL(test, "Failed to allocate fake pt: %li\n",
221 			   PTR_ERR(pt));
222 		goto free_big;
223 	}
224 
225 	tiny = xe_bo_create_pin_map(xe, tile, m->q->vm,
226 				    2 * SZ_4K,
227 				    ttm_bo_type_kernel,
228 				    XE_BO_FLAG_VRAM_IF_DGFX(tile),
229 				    exec);
230 	if (IS_ERR(tiny)) {
231 		KUNIT_FAIL(test, "Failed to allocate tiny fake pt: %li\n",
232 			   PTR_ERR(tiny));
233 		goto free_pt;
234 	}
235 
236 	bb = xe_bb_new(tile->primary_gt, 32, xe->info.has_usm);
237 	if (IS_ERR(bb)) {
238 		KUNIT_FAIL(test, "Failed to create batchbuffer: %li\n",
239 			   PTR_ERR(bb));
240 		goto free_tiny;
241 	}
242 
243 	kunit_info(test, "Starting tests, top level PT addr: %lx, special pagetable base addr: %lx\n",
244 		   (unsigned long)xe_bo_main_addr(m->q->vm->pt_root[id]->bo, XE_PAGE_SIZE),
245 		   (unsigned long)xe_bo_main_addr(m->pt_bo, XE_PAGE_SIZE));
246 
247 	/* First part of the test, are we updating our pagetable bo with a new entry? */
248 	xe_map_wr(xe, &bo->vmap, XE_PAGE_SIZE * (NUM_KERNEL_PDE - 1), u64,
249 		  0xdeaddeadbeefbeef);
250 	expected = m->q->vm->pt_ops->pte_encode_bo(pt, 0, xe_cache_pat_idx(xe, XE_CACHE_WB), 0);
251 	if (m->q->vm->flags & XE_VM_FLAG_64K)
252 		expected |= XE_PTE_PS64;
253 	if (xe_bo_is_vram(pt))
254 		xe_res_first(pt->ttm.resource, 0, xe_bo_size(pt), &src_it);
255 	else
256 		xe_res_first_sg(xe_bo_sg(pt), 0, xe_bo_size(pt), &src_it);
257 
258 	emit_pte(m, bb, NUM_KERNEL_PDE - 1, xe_bo_is_vram(pt), false,
259 		 &src_it, XE_PAGE_SIZE, pt->ttm.resource);
260 
261 	run_sanity_job(m, xe, bb, bb->len, "Writing PTE for our fake PT", test);
262 
263 	retval = xe_map_rd(xe, &bo->vmap, XE_PAGE_SIZE * (NUM_KERNEL_PDE - 1),
264 			   u64);
265 	check(retval, expected, "PTE entry write", test);
266 
267 	/* Now try to write data to our newly mapped 'pagetable', see if it succeeds */
268 	bb->len = 0;
269 	bb->cs[bb->len++] = MI_BATCH_BUFFER_END;
270 	xe_map_wr(xe, &pt->vmap, 0, u32, 0xdeaddead);
271 	expected = 0;
272 
273 	emit_clear(tile->primary_gt, bb, xe_migrate_vm_addr(NUM_KERNEL_PDE - 1, 0), 4, 4,
274 		   IS_DGFX(xe));
275 	run_sanity_job(m, xe, bb, 1, "Writing to our newly mapped pagetable",
276 		       test);
277 
278 	retval = xe_map_rd(xe, &pt->vmap, 0, u32);
279 	check(retval, expected, "Write to PT after adding PTE", test);
280 
281 	/* Sanity checks passed, try the full ones! */
282 
283 	/* Clear a small bo */
284 	kunit_info(test, "Clearing small buffer object\n");
285 	xe_map_memset(xe, &tiny->vmap, 0, 0x22, xe_bo_size(tiny));
286 	expected = 0;
287 	fence = xe_migrate_clear(m, tiny, tiny->ttm.resource,
288 				 XE_MIGRATE_CLEAR_FLAG_FULL);
289 	if (sanity_fence_failed(xe, fence, "Clearing small bo", test))
290 		goto out;
291 
292 	dma_fence_put(fence);
293 	retval = xe_map_rd(xe, &tiny->vmap, 0, u32);
294 	check(retval, expected, "Command clear small first value", test);
295 	retval = xe_map_rd(xe, &tiny->vmap, xe_bo_size(tiny) - 4, u32);
296 	check(retval, expected, "Command clear small last value", test);
297 
298 	kunit_info(test, "Copying small buffer object to system\n");
299 	test_copy_sysmem(m, tiny, exec, test);
300 	if (xe->info.tile_count > 1) {
301 		kunit_info(test, "Copying small buffer object to other vram\n");
302 		test_copy_vram(m, tiny, exec, test);
303 	}
304 
305 	/* Clear a big bo */
306 	kunit_info(test, "Clearing big buffer object\n");
307 	xe_map_memset(xe, &big->vmap, 0, 0x11, xe_bo_size(big));
308 	expected = 0;
309 	fence = xe_migrate_clear(m, big, big->ttm.resource,
310 				 XE_MIGRATE_CLEAR_FLAG_FULL);
311 	if (sanity_fence_failed(xe, fence, "Clearing big bo", test))
312 		goto out;
313 
314 	dma_fence_put(fence);
315 	retval = xe_map_rd(xe, &big->vmap, 0, u32);
316 	check(retval, expected, "Command clear big first value", test);
317 	retval = xe_map_rd(xe, &big->vmap, xe_bo_size(big) - 4, u32);
318 	check(retval, expected, "Command clear big last value", test);
319 
320 	kunit_info(test, "Copying big buffer object to system\n");
321 	test_copy_sysmem(m, big, exec, test);
322 	if (xe->info.tile_count > 1) {
323 		kunit_info(test, "Copying big buffer object to other vram\n");
324 		test_copy_vram(m, big, exec, test);
325 	}
326 
327 out:
328 	xe_bb_free(bb, NULL);
329 free_tiny:
330 	xe_bo_unpin(tiny);
331 	xe_bo_put(tiny);
332 free_pt:
333 	xe_bo_unpin(pt);
334 	xe_bo_put(pt);
335 free_big:
336 	xe_bo_unpin(big);
337 	xe_bo_put(big);
338 vunmap:
339 	xe_bo_vunmap(m->pt_bo);
340 }
341 
342 static int migrate_test_run_device(struct xe_device *xe)
343 {
344 	struct kunit *test = kunit_get_current_test();
345 	struct xe_tile *tile;
346 	int id;
347 
348 	guard(xe_pm_runtime)(xe);
349 	for_each_tile(tile, xe, id) {
350 		struct xe_migrate *m = tile->migrate;
351 		struct drm_exec *exec = XE_VALIDATION_OPT_OUT;
352 
353 		kunit_info(test, "Testing tile id %d.\n", id);
354 		xe_vm_lock(m->q->vm, false);
355 		xe_migrate_sanity_test(m, test, exec);
356 		xe_vm_unlock(m->q->vm);
357 	}
358 
359 	return 0;
360 }
361 
362 static void xe_migrate_sanity_kunit(struct kunit *test)
363 {
364 	struct xe_device *xe = test->priv;
365 
366 	migrate_test_run_device(xe);
367 }
368 
369 static struct dma_fence *blt_copy(struct xe_tile *tile,
370 				  struct xe_bo *src_bo, struct xe_bo *dst_bo,
371 				  bool copy_only_ccs, const char *str, struct kunit *test)
372 {
373 	struct xe_gt *gt = tile->primary_gt;
374 	struct xe_migrate *m = tile->migrate;
375 	struct xe_device *xe = gt_to_xe(gt);
376 	struct dma_fence *fence = NULL;
377 	u64 size = xe_bo_size(src_bo);
378 	struct xe_res_cursor src_it, dst_it;
379 	struct ttm_resource *src = src_bo->ttm.resource, *dst = dst_bo->ttm.resource;
380 	u64 src_L0_ofs, dst_L0_ofs;
381 	u32 src_L0_pt, dst_L0_pt;
382 	u64 src_L0, dst_L0;
383 	int err;
384 	bool src_is_vram = mem_type_is_vram(src->mem_type);
385 	bool dst_is_vram = mem_type_is_vram(dst->mem_type);
386 
387 	if (!src_is_vram)
388 		xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it);
389 	else
390 		xe_res_first(src, 0, size, &src_it);
391 
392 	if (!dst_is_vram)
393 		xe_res_first_sg(xe_bo_sg(dst_bo), 0, size, &dst_it);
394 	else
395 		xe_res_first(dst, 0, size, &dst_it);
396 
397 	while (size) {
398 		u32 batch_size = 2; /* arb_clear() + MI_BATCH_BUFFER_END */
399 		struct xe_sched_job *job;
400 		struct xe_bb *bb;
401 		u32 flush_flags = 0;
402 		u32 update_idx;
403 		u32 avail_pts = max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENCODE_SIZE;
404 		u32 pte_flags;
405 
406 		src_L0 = xe_migrate_res_sizes(m, &src_it);
407 		dst_L0 = xe_migrate_res_sizes(m, &dst_it);
408 
409 		src_L0 = min(src_L0, dst_L0);
410 
411 		pte_flags = src_is_vram ? (PTE_UPDATE_FLAG_IS_VRAM |
412 					   PTE_UPDATE_FLAG_IS_COMP_PTE) : 0;
413 		batch_size += pte_update_size(m, pte_flags, src, &src_it, &src_L0,
414 					      &src_L0_ofs, &src_L0_pt, 0, 0,
415 					      avail_pts);
416 
417 		pte_flags = dst_is_vram ? (PTE_UPDATE_FLAG_IS_VRAM |
418 					   PTE_UPDATE_FLAG_IS_COMP_PTE) : 0;
419 		batch_size += pte_update_size(m, pte_flags, dst, &dst_it, &src_L0,
420 					      &dst_L0_ofs, &dst_L0_pt, 0,
421 					      avail_pts, avail_pts);
422 
423 		/* Add copy commands size here */
424 		batch_size += ((copy_only_ccs) ? 0 : emit_copy_cmd_len(xe)) +
425 			((xe_device_has_flat_ccs(xe) && copy_only_ccs) ? EMIT_COPY_CCS_DW : 0);
426 
427 		bb = xe_bb_new(gt, batch_size, xe->info.has_usm);
428 		if (IS_ERR(bb)) {
429 			err = PTR_ERR(bb);
430 			goto err_sync;
431 		}
432 
433 		if (src_is_vram)
434 			xe_res_next(&src_it, src_L0);
435 		else
436 			emit_pte(m, bb, src_L0_pt, src_is_vram, false,
437 				 &src_it, src_L0, src);
438 
439 		if (dst_is_vram)
440 			xe_res_next(&dst_it, src_L0);
441 		else
442 			emit_pte(m, bb, dst_L0_pt, dst_is_vram, false,
443 				 &dst_it, src_L0, dst);
444 
445 		bb->cs[bb->len++] = MI_BATCH_BUFFER_END;
446 		update_idx = bb->len;
447 		if (!copy_only_ccs)
448 			emit_copy(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0, XE_PAGE_SIZE);
449 
450 		if (copy_only_ccs)
451 			flush_flags = xe_migrate_ccs_copy(m, bb, src_L0_ofs,
452 							  src_is_vram, dst_L0_ofs,
453 							  dst_is_vram, src_L0, dst_L0_ofs,
454 							  copy_only_ccs);
455 
456 		job = xe_bb_create_migration_job(m->q, bb,
457 						 xe_migrate_batch_base(m, xe->info.has_usm),
458 						 update_idx);
459 		if (IS_ERR(job)) {
460 			err = PTR_ERR(job);
461 			goto err;
462 		}
463 
464 		xe_sched_job_add_migrate_flush(job, flush_flags);
465 
466 		mutex_lock(&m->job_mutex);
467 		xe_sched_job_arm(job);
468 		dma_fence_put(fence);
469 		fence = dma_fence_get(&job->drm.s_fence->finished);
470 		xe_sched_job_push(job);
471 
472 		dma_fence_put(m->fence);
473 		m->fence = dma_fence_get(fence);
474 
475 		mutex_unlock(&m->job_mutex);
476 
477 		xe_bb_free(bb, fence);
478 		size -= src_L0;
479 		continue;
480 
481 err:
482 		xe_bb_free(bb, NULL);
483 
484 err_sync:
485 		if (fence) {
486 			dma_fence_wait(fence, false);
487 			dma_fence_put(fence);
488 		}
489 		return ERR_PTR(err);
490 	}
491 
492 	return fence;
493 }
494 
495 static void test_migrate(struct xe_device *xe, struct xe_tile *tile,
496 			 struct xe_bo *sys_bo, struct xe_bo *vram_bo, struct xe_bo *ccs_bo,
497 			 struct drm_exec *exec, struct kunit *test)
498 {
499 	struct dma_fence *fence;
500 	u64 expected, retval;
501 	long timeout;
502 	long ret;
503 
504 	expected = 0xd0d0d0d0d0d0d0d0;
505 	xe_map_memset(xe, &sys_bo->vmap, 0, 0xd0, xe_bo_size(sys_bo));
506 
507 	fence = blt_copy(tile, sys_bo, vram_bo, false, "Blit copy from sysmem to vram", test);
508 	if (!sanity_fence_failed(xe, fence, "Blit copy from sysmem to vram", test)) {
509 		retval = xe_map_rd(xe, &vram_bo->vmap, 0, u64);
510 		if (retval == expected)
511 			KUNIT_FAIL(test, "Sanity check failed: VRAM must have compressed value\n");
512 	}
513 	dma_fence_put(fence);
514 
515 	kunit_info(test, "Evict vram buffer object\n");
516 	ret = xe_bo_evict(vram_bo, exec);
517 	if (ret) {
518 		KUNIT_FAIL(test, "Failed to evict bo.\n");
519 		return;
520 	}
521 
522 	ret = xe_bo_vmap(vram_bo);
523 	if (ret) {
524 		KUNIT_FAIL(test, "Failed to vmap vram bo: %li\n", ret);
525 		return;
526 	}
527 
528 	retval = xe_map_rd(xe, &vram_bo->vmap, 0, u64);
529 	check(retval, expected, "Clear evicted vram data first value", test);
530 	retval = xe_map_rd(xe, &vram_bo->vmap, xe_bo_size(vram_bo) - 8, u64);
531 	check(retval, expected, "Clear evicted vram data last value", test);
532 
533 	fence = blt_copy(tile, vram_bo, ccs_bo,
534 			 true, "Blit surf copy from vram to sysmem", test);
535 	if (!sanity_fence_failed(xe, fence, "Clear ccs buffer data", test)) {
536 		retval = xe_map_rd(xe, &ccs_bo->vmap, 0, u64);
537 		check(retval, 0, "Clear ccs data first value", test);
538 
539 		retval = xe_map_rd(xe, &ccs_bo->vmap, xe_bo_size(ccs_bo) - 8, u64);
540 		check(retval, 0, "Clear ccs data last value", test);
541 	}
542 	dma_fence_put(fence);
543 
544 	kunit_info(test, "Restore vram buffer object\n");
545 	ret = xe_bo_validate(vram_bo, NULL, false, exec);
546 	if (ret) {
547 		KUNIT_FAIL(test, "Failed to validate vram bo for: %li\n", ret);
548 		return;
549 	}
550 
551 	/* Sync all migration blits */
552 	timeout = dma_resv_wait_timeout(vram_bo->ttm.base.resv,
553 					DMA_RESV_USAGE_KERNEL,
554 					true,
555 					5 * HZ);
556 	if (timeout <= 0) {
557 		KUNIT_FAIL(test, "Failed to sync bo eviction.\n");
558 		return;
559 	}
560 
561 	ret = xe_bo_vmap(vram_bo);
562 	if (ret) {
563 		KUNIT_FAIL(test, "Failed to vmap vram bo: %li\n", ret);
564 		return;
565 	}
566 
567 	retval = xe_map_rd(xe, &vram_bo->vmap, 0, u64);
568 	check(retval, expected, "Restored value must be equal to initial value", test);
569 	retval = xe_map_rd(xe, &vram_bo->vmap, xe_bo_size(vram_bo) - 8, u64);
570 	check(retval, expected, "Restored value must be equal to initial value", test);
571 
572 	fence = blt_copy(tile, vram_bo, ccs_bo,
573 			 true, "Blit surf copy from vram to sysmem", test);
574 	if (!sanity_fence_failed(xe, fence, "Clear ccs buffer data", test)) {
575 		retval = xe_map_rd(xe, &ccs_bo->vmap, 0, u64);
576 		check(retval, 0, "Clear ccs data first value", test);
577 		retval = xe_map_rd(xe, &ccs_bo->vmap, xe_bo_size(ccs_bo) - 8, u64);
578 		check(retval, 0, "Clear ccs data last value", test);
579 	}
580 	dma_fence_put(fence);
581 }
582 
583 static void test_clear(struct xe_device *xe, struct xe_tile *tile,
584 		       struct xe_bo *sys_bo, struct xe_bo *vram_bo, struct kunit *test)
585 {
586 	struct dma_fence *fence;
587 	u64 expected, retval;
588 
589 	expected = 0xd0d0d0d0d0d0d0d0;
590 	xe_map_memset(xe, &sys_bo->vmap, 0, 0xd0, xe_bo_size(sys_bo));
591 
592 	fence = blt_copy(tile, sys_bo, vram_bo, false, "Blit copy from sysmem to vram", test);
593 	if (!sanity_fence_failed(xe, fence, "Blit copy from sysmem to vram", test)) {
594 		retval = xe_map_rd(xe, &vram_bo->vmap, 0, u64);
595 		if (retval == expected)
596 			KUNIT_FAIL(test, "Sanity check failed: VRAM must have compressed value\n");
597 	}
598 	dma_fence_put(fence);
599 
600 	fence = blt_copy(tile, vram_bo, sys_bo, false, "Blit copy from vram to sysmem", test);
601 	if (!sanity_fence_failed(xe, fence, "Blit copy from vram to sysmem", test)) {
602 		retval = xe_map_rd(xe, &sys_bo->vmap, 0, u64);
603 		check(retval, expected, "Decompressed value must be equal to initial value", test);
604 		retval = xe_map_rd(xe, &sys_bo->vmap, xe_bo_size(sys_bo) - 8, u64);
605 		check(retval, expected, "Decompressed value must be equal to initial value", test);
606 	}
607 	dma_fence_put(fence);
608 
609 	kunit_info(test, "Clear vram buffer object\n");
610 	expected = 0x0000000000000000;
611 	fence = xe_migrate_clear(tile->migrate, vram_bo, vram_bo->ttm.resource,
612 				 XE_MIGRATE_CLEAR_FLAG_FULL);
613 	if (sanity_fence_failed(xe, fence, "Clear vram_bo", test))
614 		return;
615 	dma_fence_put(fence);
616 
617 	fence = blt_copy(tile, vram_bo, sys_bo,
618 			 false, "Blit copy from vram to sysmem", test);
619 	if (!sanity_fence_failed(xe, fence, "Clear main buffer data", test)) {
620 		retval = xe_map_rd(xe, &sys_bo->vmap, 0, u64);
621 		check(retval, expected, "Clear main buffer first value", test);
622 		retval = xe_map_rd(xe, &sys_bo->vmap, xe_bo_size(sys_bo) - 8, u64);
623 		check(retval, expected, "Clear main buffer last value", test);
624 	}
625 	dma_fence_put(fence);
626 
627 	fence = blt_copy(tile, vram_bo, sys_bo,
628 			 true, "Blit surf copy from vram to sysmem", test);
629 	if (!sanity_fence_failed(xe, fence, "Clear ccs buffer data", test)) {
630 		retval = xe_map_rd(xe, &sys_bo->vmap, 0, u64);
631 		check(retval, expected, "Clear ccs data first value", test);
632 		retval = xe_map_rd(xe, &sys_bo->vmap, xe_bo_size(sys_bo) - 8, u64);
633 		check(retval, expected, "Clear ccs data last value", test);
634 	}
635 	dma_fence_put(fence);
636 }
637 
638 static void validate_ccs_test_run_tile(struct xe_device *xe, struct xe_tile *tile,
639 				       struct kunit *test)
640 {
641 	struct xe_bo *sys_bo, *vram_bo = NULL, *ccs_bo = NULL;
642 	unsigned int bo_flags = XE_BO_FLAG_VRAM_IF_DGFX(tile);
643 	struct drm_exec *exec;
644 	long ret;
645 
646 	sys_bo = xe_bo_create_user(xe, NULL, SZ_4M,
647 				   DRM_XE_GEM_CPU_CACHING_WC,
648 				   XE_BO_FLAG_SYSTEM |
649 				   XE_BO_FLAG_NEEDS_CPU_ACCESS |
650 				   XE_BO_FLAG_PINNED, NULL);
651 
652 	if (IS_ERR(sys_bo)) {
653 		KUNIT_FAIL(test, "xe_bo_create() failed with err=%ld\n",
654 			   PTR_ERR(sys_bo));
655 		return;
656 	}
657 
658 	exec = XE_VALIDATION_OPT_OUT;
659 	xe_bo_lock(sys_bo, false);
660 	ret = xe_bo_validate(sys_bo, NULL, false, exec);
661 	if (ret) {
662 		KUNIT_FAIL(test, "Failed to validate system bo for: %li\n", ret);
663 		goto free_sysbo;
664 	}
665 
666 	ret = xe_bo_vmap(sys_bo);
667 	if (ret) {
668 		KUNIT_FAIL(test, "Failed to vmap system bo: %li\n", ret);
669 		goto free_sysbo;
670 	}
671 	xe_bo_unlock(sys_bo);
672 
673 	ccs_bo = xe_bo_create_user(xe, NULL, SZ_4M,
674 				   DRM_XE_GEM_CPU_CACHING_WC,
675 				   bo_flags | XE_BO_FLAG_NEEDS_CPU_ACCESS |
676 				   XE_BO_FLAG_PINNED, NULL);
677 
678 	if (IS_ERR(ccs_bo)) {
679 		KUNIT_FAIL(test, "xe_bo_create() failed with err=%ld\n",
680 			   PTR_ERR(ccs_bo));
681 		return;
682 	}
683 
684 	xe_bo_lock(ccs_bo, false);
685 	ret = xe_bo_validate(ccs_bo, NULL, false, exec);
686 	if (ret) {
687 		KUNIT_FAIL(test, "Failed to validate system bo for: %li\n", ret);
688 		goto free_ccsbo;
689 	}
690 
691 	ret = xe_bo_vmap(ccs_bo);
692 	if (ret) {
693 		KUNIT_FAIL(test, "Failed to vmap system bo: %li\n", ret);
694 		goto free_ccsbo;
695 	}
696 	xe_bo_unlock(ccs_bo);
697 
698 	vram_bo = xe_bo_create_user(xe, NULL, SZ_4M,
699 				    DRM_XE_GEM_CPU_CACHING_WC,
700 				    bo_flags | XE_BO_FLAG_NEEDS_CPU_ACCESS |
701 				    XE_BO_FLAG_PINNED, NULL);
702 	if (IS_ERR(vram_bo)) {
703 		KUNIT_FAIL(test, "xe_bo_create() failed with err=%ld\n",
704 			   PTR_ERR(vram_bo));
705 		return;
706 	}
707 
708 	xe_bo_lock(vram_bo, false);
709 	ret = xe_bo_validate(vram_bo, NULL, false, exec);
710 	if (ret) {
711 		KUNIT_FAIL(test, "Failed to validate vram bo for: %li\n", ret);
712 		goto free_vrambo;
713 	}
714 
715 	ret = xe_bo_vmap(vram_bo);
716 	if (ret) {
717 		KUNIT_FAIL(test, "Failed to vmap vram bo: %li\n", ret);
718 		goto free_vrambo;
719 	}
720 
721 	test_clear(xe, tile, sys_bo, vram_bo, test);
722 	test_migrate(xe, tile, sys_bo, vram_bo, ccs_bo, exec, test);
723 	xe_bo_unlock(vram_bo);
724 
725 	xe_bo_lock(vram_bo, false);
726 	xe_bo_vunmap(vram_bo);
727 	xe_bo_unlock(vram_bo);
728 
729 	xe_bo_lock(ccs_bo, false);
730 	xe_bo_vunmap(ccs_bo);
731 	xe_bo_unlock(ccs_bo);
732 
733 	xe_bo_lock(sys_bo, false);
734 	xe_bo_vunmap(sys_bo);
735 	xe_bo_unlock(sys_bo);
736 free_vrambo:
737 	xe_bo_put(vram_bo);
738 free_ccsbo:
739 	xe_bo_put(ccs_bo);
740 free_sysbo:
741 	xe_bo_put(sys_bo);
742 }
743 
744 static int validate_ccs_test_run_device(struct xe_device *xe)
745 {
746 	struct kunit *test = kunit_get_current_test();
747 	struct xe_tile *tile;
748 	int id;
749 
750 	if (!xe_device_has_flat_ccs(xe)) {
751 		kunit_skip(test, "non-flat-ccs device\n");
752 		return 0;
753 	}
754 
755 	if (!(GRAPHICS_VER(xe) >= 20 && IS_DGFX(xe))) {
756 		kunit_skip(test, "non-xe2 discrete device\n");
757 		return 0;
758 	}
759 
760 	guard(xe_pm_runtime)(xe);
761 	for_each_tile(tile, xe, id)
762 		validate_ccs_test_run_tile(xe, tile, test);
763 
764 	return 0;
765 }
766 
767 static void xe_validate_ccs_kunit(struct kunit *test)
768 {
769 	struct xe_device *xe = test->priv;
770 
771 	validate_ccs_test_run_device(xe);
772 }
773 
774 static struct kunit_case xe_migrate_tests[] = {
775 	KUNIT_CASE_PARAM(xe_migrate_sanity_kunit, xe_pci_live_device_gen_param),
776 	KUNIT_CASE_PARAM(xe_validate_ccs_kunit, xe_pci_live_device_gen_param),
777 	{}
778 };
779 
780 VISIBLE_IF_KUNIT
781 struct kunit_suite xe_migrate_test_suite = {
782 	.name = "xe_migrate",
783 	.test_cases = xe_migrate_tests,
784 	.init = xe_kunit_helper_xe_device_live_test_init,
785 };
786 EXPORT_SYMBOL_IF_KUNIT(xe_migrate_test_suite);
787