xref: /linux/drivers/gpu/drm/xe/tests/xe_bo.c (revision 75fd04f276de31cc59419fda169232d097fbf291)
1 // SPDX-License-Identifier: GPL-2.0 AND MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include <kunit/test.h>
7 #include <kunit/visibility.h>
8 
9 #include <linux/iosys-map.h>
10 #include <linux/math64.h>
11 #include <linux/prandom.h>
12 #include <linux/swap.h>
13 
14 #include <uapi/linux/sysinfo.h>
15 
16 #include "tests/xe_kunit_helpers.h"
17 #include "tests/xe_pci_test.h"
18 #include "tests/xe_test.h"
19 
20 #include "xe_bo_evict.h"
21 #include "xe_pci.h"
22 #include "xe_pm.h"
23 
24 static int ccs_test_migrate(struct xe_tile *tile, struct xe_bo *bo,
25 			    bool clear, u64 get_val, u64 assign_val,
26 			    struct kunit *test)
27 {
28 	struct dma_fence *fence;
29 	struct ttm_tt *ttm;
30 	struct page *page;
31 	pgoff_t ccs_page;
32 	long timeout;
33 	u64 *cpu_map;
34 	int ret;
35 	u32 offset;
36 
37 	/* Move bo to VRAM if not already there. */
38 	ret = xe_bo_validate(bo, NULL, false);
39 	if (ret) {
40 		KUNIT_FAIL(test, "Failed to validate bo.\n");
41 		return ret;
42 	}
43 
44 	/* Optionally clear bo *and* CCS data in VRAM. */
45 	if (clear) {
46 		fence = xe_migrate_clear(tile->migrate, bo, bo->ttm.resource,
47 					 XE_MIGRATE_CLEAR_FLAG_FULL);
48 		if (IS_ERR(fence)) {
49 			KUNIT_FAIL(test, "Failed to submit bo clear.\n");
50 			return PTR_ERR(fence);
51 		}
52 
53 		if (dma_fence_wait_timeout(fence, false, 5 * HZ) <= 0) {
54 			dma_fence_put(fence);
55 			KUNIT_FAIL(test, "Timeout while clearing bo.\n");
56 			return  -ETIME;
57 		}
58 
59 		dma_fence_put(fence);
60 	}
61 
62 	/* Evict to system. CCS data should be copied. */
63 	ret = xe_bo_evict(bo, true);
64 	if (ret) {
65 		KUNIT_FAIL(test, "Failed to evict bo.\n");
66 		return ret;
67 	}
68 
69 	/* Sync all migration blits */
70 	timeout = dma_resv_wait_timeout(bo->ttm.base.resv,
71 					DMA_RESV_USAGE_KERNEL,
72 					true,
73 					5 * HZ);
74 	if (timeout <= 0) {
75 		KUNIT_FAIL(test, "Failed to sync bo eviction.\n");
76 		return -ETIME;
77 	}
78 
79 	/*
80 	 * Bo with CCS data is now in system memory. Verify backing store
81 	 * and data integrity. Then assign for the next testing round while
82 	 * we still have a CPU map.
83 	 */
84 	ttm = bo->ttm.ttm;
85 	if (!ttm || !ttm_tt_is_populated(ttm)) {
86 		KUNIT_FAIL(test, "Bo was not in expected placement.\n");
87 		return -EINVAL;
88 	}
89 
90 	ccs_page = xe_bo_ccs_pages_start(bo) >> PAGE_SHIFT;
91 	if (ccs_page >= ttm->num_pages) {
92 		KUNIT_FAIL(test, "No TTM CCS pages present.\n");
93 		return -EINVAL;
94 	}
95 
96 	page = ttm->pages[ccs_page];
97 	cpu_map = kmap_local_page(page);
98 
99 	/* Check first CCS value */
100 	if (cpu_map[0] != get_val) {
101 		KUNIT_FAIL(test,
102 			   "Expected CCS readout 0x%016llx, got 0x%016llx.\n",
103 			   (unsigned long long)get_val,
104 			   (unsigned long long)cpu_map[0]);
105 		ret = -EINVAL;
106 	}
107 
108 	/* Check last CCS value, or at least last value in page. */
109 	offset = xe_device_ccs_bytes(tile_to_xe(tile), bo->size);
110 	offset = min_t(u32, offset, PAGE_SIZE) / sizeof(u64) - 1;
111 	if (cpu_map[offset] != get_val) {
112 		KUNIT_FAIL(test,
113 			   "Expected CCS readout 0x%016llx, got 0x%016llx.\n",
114 			   (unsigned long long)get_val,
115 			   (unsigned long long)cpu_map[offset]);
116 		ret = -EINVAL;
117 	}
118 
119 	cpu_map[0] = assign_val;
120 	cpu_map[offset] = assign_val;
121 	kunmap_local(cpu_map);
122 
123 	return ret;
124 }
125 
126 static void ccs_test_run_tile(struct xe_device *xe, struct xe_tile *tile,
127 			      struct kunit *test)
128 {
129 	struct xe_bo *bo;
130 
131 	int ret;
132 
133 	/* TODO: Sanity check */
134 	unsigned int bo_flags = XE_BO_FLAG_VRAM_IF_DGFX(tile);
135 
136 	if (IS_DGFX(xe))
137 		kunit_info(test, "Testing vram id %u\n", tile->id);
138 	else
139 		kunit_info(test, "Testing system memory\n");
140 
141 	bo = xe_bo_create_user(xe, NULL, NULL, SZ_1M, DRM_XE_GEM_CPU_CACHING_WC,
142 			       bo_flags);
143 	if (IS_ERR(bo)) {
144 		KUNIT_FAIL(test, "Failed to create bo.\n");
145 		return;
146 	}
147 
148 	xe_bo_lock(bo, false);
149 
150 	kunit_info(test, "Verifying that CCS data is cleared on creation.\n");
151 	ret = ccs_test_migrate(tile, bo, false, 0ULL, 0xdeadbeefdeadbeefULL,
152 			       test);
153 	if (ret)
154 		goto out_unlock;
155 
156 	kunit_info(test, "Verifying that CCS data survives migration.\n");
157 	ret = ccs_test_migrate(tile, bo, false, 0xdeadbeefdeadbeefULL,
158 			       0xdeadbeefdeadbeefULL, test);
159 	if (ret)
160 		goto out_unlock;
161 
162 	kunit_info(test, "Verifying that CCS data can be properly cleared.\n");
163 	ret = ccs_test_migrate(tile, bo, true, 0ULL, 0ULL, test);
164 
165 out_unlock:
166 	xe_bo_unlock(bo);
167 	xe_bo_put(bo);
168 }
169 
170 static int ccs_test_run_device(struct xe_device *xe)
171 {
172 	struct kunit *test = kunit_get_current_test();
173 	struct xe_tile *tile;
174 	int id;
175 
176 	if (!xe_device_has_flat_ccs(xe)) {
177 		kunit_skip(test, "non-flat-ccs device\n");
178 		return 0;
179 	}
180 
181 	/* For xe2+ dgfx, we don't handle ccs metadata */
182 	if (GRAPHICS_VER(xe) >= 20 && IS_DGFX(xe)) {
183 		kunit_skip(test, "xe2+ dgfx device\n");
184 		return 0;
185 	}
186 
187 	xe_pm_runtime_get(xe);
188 
189 	for_each_tile(tile, xe, id) {
190 		/* For igfx run only for primary tile */
191 		if (!IS_DGFX(xe) && id > 0)
192 			continue;
193 		ccs_test_run_tile(xe, tile, test);
194 	}
195 
196 	xe_pm_runtime_put(xe);
197 
198 	return 0;
199 }
200 
201 static void xe_ccs_migrate_kunit(struct kunit *test)
202 {
203 	struct xe_device *xe = test->priv;
204 
205 	ccs_test_run_device(xe);
206 }
207 
208 static int evict_test_run_tile(struct xe_device *xe, struct xe_tile *tile, struct kunit *test)
209 {
210 	struct xe_bo *bo, *external;
211 	unsigned int bo_flags = XE_BO_FLAG_VRAM_IF_DGFX(tile);
212 	struct xe_vm *vm = xe_migrate_get_vm(xe_device_get_root_tile(xe)->migrate);
213 	struct xe_gt *__gt;
214 	int err, i, id;
215 
216 	kunit_info(test, "Testing device %s vram id %u\n",
217 		   dev_name(xe->drm.dev), tile->id);
218 
219 	for (i = 0; i < 2; ++i) {
220 		xe_vm_lock(vm, false);
221 		bo = xe_bo_create_user(xe, NULL, vm, 0x10000,
222 				       DRM_XE_GEM_CPU_CACHING_WC,
223 				       bo_flags);
224 		xe_vm_unlock(vm);
225 		if (IS_ERR(bo)) {
226 			KUNIT_FAIL(test, "bo create err=%pe\n", bo);
227 			break;
228 		}
229 
230 		external = xe_bo_create_user(xe, NULL, NULL, 0x10000,
231 					     DRM_XE_GEM_CPU_CACHING_WC,
232 					     bo_flags);
233 		if (IS_ERR(external)) {
234 			KUNIT_FAIL(test, "external bo create err=%pe\n", external);
235 			goto cleanup_bo;
236 		}
237 
238 		xe_bo_lock(external, false);
239 		err = xe_bo_pin_external(external);
240 		xe_bo_unlock(external);
241 		if (err) {
242 			KUNIT_FAIL(test, "external bo pin err=%pe\n",
243 				   ERR_PTR(err));
244 			goto cleanup_external;
245 		}
246 
247 		err = xe_bo_evict_all(xe);
248 		if (err) {
249 			KUNIT_FAIL(test, "evict err=%pe\n", ERR_PTR(err));
250 			goto cleanup_all;
251 		}
252 
253 		for_each_gt(__gt, xe, id)
254 			xe_gt_sanitize(__gt);
255 		err = xe_bo_restore_kernel(xe);
256 		/*
257 		 * Snapshotting the CTB and copying back a potentially old
258 		 * version seems risky, depending on what might have been
259 		 * inflight. Also it seems snapshotting the ADS object and
260 		 * copying back results in serious breakage. Normally when
261 		 * calling xe_bo_restore_kernel() we always fully restart the
262 		 * GT, which re-intializes such things.  We could potentially
263 		 * skip saving and restoring such objects in xe_bo_evict_all()
264 		 * however seems quite fragile not to also restart the GT. Try
265 		 * to do that here by triggering a GT reset.
266 		 */
267 		for_each_gt(__gt, xe, id) {
268 			xe_gt_reset_async(__gt);
269 			flush_work(&__gt->reset.worker);
270 		}
271 		if (err) {
272 			KUNIT_FAIL(test, "restore kernel err=%pe\n",
273 				   ERR_PTR(err));
274 			goto cleanup_all;
275 		}
276 
277 		err = xe_bo_restore_user(xe);
278 		if (err) {
279 			KUNIT_FAIL(test, "restore user err=%pe\n", ERR_PTR(err));
280 			goto cleanup_all;
281 		}
282 
283 		if (!xe_bo_is_vram(external)) {
284 			KUNIT_FAIL(test, "external bo is not vram\n");
285 			err = -EPROTO;
286 			goto cleanup_all;
287 		}
288 
289 		if (xe_bo_is_vram(bo)) {
290 			KUNIT_FAIL(test, "bo is vram\n");
291 			err = -EPROTO;
292 			goto cleanup_all;
293 		}
294 
295 		if (i) {
296 			down_read(&vm->lock);
297 			xe_vm_lock(vm, false);
298 			err = xe_bo_validate(bo, bo->vm, false);
299 			xe_vm_unlock(vm);
300 			up_read(&vm->lock);
301 			if (err) {
302 				KUNIT_FAIL(test, "bo valid err=%pe\n",
303 					   ERR_PTR(err));
304 				goto cleanup_all;
305 			}
306 			xe_bo_lock(external, false);
307 			err = xe_bo_validate(external, NULL, false);
308 			xe_bo_unlock(external);
309 			if (err) {
310 				KUNIT_FAIL(test, "external bo valid err=%pe\n",
311 					   ERR_PTR(err));
312 				goto cleanup_all;
313 			}
314 		}
315 
316 		xe_bo_lock(external, false);
317 		xe_bo_unpin_external(external);
318 		xe_bo_unlock(external);
319 
320 		xe_bo_put(external);
321 
322 		xe_bo_lock(bo, false);
323 		__xe_bo_unset_bulk_move(bo);
324 		xe_bo_unlock(bo);
325 		xe_bo_put(bo);
326 		continue;
327 
328 cleanup_all:
329 		xe_bo_lock(external, false);
330 		xe_bo_unpin_external(external);
331 		xe_bo_unlock(external);
332 cleanup_external:
333 		xe_bo_put(external);
334 cleanup_bo:
335 		xe_bo_lock(bo, false);
336 		__xe_bo_unset_bulk_move(bo);
337 		xe_bo_unlock(bo);
338 		xe_bo_put(bo);
339 		break;
340 	}
341 
342 	xe_vm_put(vm);
343 
344 	return 0;
345 }
346 
347 static int evict_test_run_device(struct xe_device *xe)
348 {
349 	struct kunit *test = kunit_get_current_test();
350 	struct xe_tile *tile;
351 	int id;
352 
353 	if (!IS_DGFX(xe)) {
354 		kunit_skip(test, "non-discrete device\n");
355 		return 0;
356 	}
357 
358 	xe_pm_runtime_get(xe);
359 
360 	for_each_tile(tile, xe, id)
361 		evict_test_run_tile(xe, tile, test);
362 
363 	xe_pm_runtime_put(xe);
364 
365 	return 0;
366 }
367 
368 static void xe_bo_evict_kunit(struct kunit *test)
369 {
370 	struct xe_device *xe = test->priv;
371 
372 	evict_test_run_device(xe);
373 }
374 
375 struct xe_bo_link {
376 	struct list_head link;
377 	struct xe_bo *bo;
378 	u32 val;
379 };
380 
381 #define XE_BO_SHRINK_SIZE ((unsigned long)SZ_64M)
382 
383 static int shrink_test_fill_random(struct xe_bo *bo, struct rnd_state *state,
384 				   struct xe_bo_link *link)
385 {
386 	struct iosys_map map;
387 	int ret = ttm_bo_vmap(&bo->ttm, &map);
388 	size_t __maybe_unused i;
389 
390 	if (ret)
391 		return ret;
392 
393 	for (i = 0; i < bo->ttm.base.size; i += sizeof(u32)) {
394 		u32 val = prandom_u32_state(state);
395 
396 		iosys_map_wr(&map, i, u32, val);
397 		if (i == 0)
398 			link->val = val;
399 	}
400 
401 	ttm_bo_vunmap(&bo->ttm, &map);
402 	return 0;
403 }
404 
405 static bool shrink_test_verify(struct kunit *test, struct xe_bo *bo,
406 			       unsigned int bo_nr, struct rnd_state *state,
407 			       struct xe_bo_link *link)
408 {
409 	struct iosys_map map;
410 	int ret = ttm_bo_vmap(&bo->ttm, &map);
411 	size_t i;
412 	bool failed = false;
413 
414 	if (ret) {
415 		KUNIT_FAIL(test, "Error mapping bo %u for content check.\n", bo_nr);
416 		return true;
417 	}
418 
419 	for (i = 0; i < bo->ttm.base.size; i += sizeof(u32)) {
420 		u32 val = prandom_u32_state(state);
421 
422 		if (iosys_map_rd(&map, i, u32) != val) {
423 			KUNIT_FAIL(test, "Content not preserved, bo %u offset 0x%016llx",
424 				   bo_nr, (unsigned long long)i);
425 			kunit_info(test, "Failed value is 0x%08x, recorded 0x%08x\n",
426 				   (unsigned int)iosys_map_rd(&map, i, u32), val);
427 			if (i == 0 && val != link->val)
428 				kunit_info(test, "Looks like PRNG is out of sync.\n");
429 			failed = true;
430 			break;
431 		}
432 	}
433 
434 	ttm_bo_vunmap(&bo->ttm, &map);
435 
436 	return failed;
437 }
438 
439 /*
440  * Try to create system bos corresponding to twice the amount
441  * of available system memory to test shrinker functionality.
442  * If no swap space is available to accommodate the
443  * memory overcommit, mark bos purgeable.
444  */
445 static int shrink_test_run_device(struct xe_device *xe)
446 {
447 	struct kunit *test = kunit_get_current_test();
448 	LIST_HEAD(bos);
449 	struct xe_bo_link *link, *next;
450 	struct sysinfo si;
451 	u64 ram, ram_and_swap, purgeable = 0, alloced, to_alloc, limit;
452 	unsigned int interrupted = 0, successful = 0, count = 0;
453 	struct rnd_state prng;
454 	u64 rand_seed;
455 	bool failed = false;
456 
457 	rand_seed = get_random_u64();
458 	prandom_seed_state(&prng, rand_seed);
459 	kunit_info(test, "Random seed is 0x%016llx.\n",
460 		   (unsigned long long)rand_seed);
461 
462 	/* Skip if execution time is expected to be too long. */
463 
464 	limit = SZ_32G;
465 	/* IGFX with flat CCS needs to copy when swapping / shrinking */
466 	if (!IS_DGFX(xe) && xe_device_has_flat_ccs(xe))
467 		limit = SZ_16G;
468 
469 	si_meminfo(&si);
470 	ram = (size_t)si.freeram * si.mem_unit;
471 	if (ram > limit) {
472 		kunit_skip(test, "Too long expected execution time.\n");
473 		return 0;
474 	}
475 	to_alloc = ram * 2;
476 
477 	ram_and_swap = ram + get_nr_swap_pages() * PAGE_SIZE;
478 	if (to_alloc > ram_and_swap)
479 		purgeable = to_alloc - ram_and_swap;
480 	purgeable += div64_u64(purgeable, 5);
481 
482 	kunit_info(test, "Free ram is %lu bytes. Will allocate twice of that.\n",
483 		   (unsigned long)ram);
484 	for (alloced = 0; alloced < to_alloc; alloced += XE_BO_SHRINK_SIZE) {
485 		struct xe_bo *bo;
486 		unsigned int mem_type;
487 		struct xe_ttm_tt *xe_tt;
488 
489 		link = kzalloc(sizeof(*link), GFP_KERNEL);
490 		if (!link) {
491 			KUNIT_FAIL(test, "Unexpected link allocation failure\n");
492 			failed = true;
493 			break;
494 		}
495 
496 		INIT_LIST_HEAD(&link->link);
497 
498 		/* We can create bos using WC caching here. But it is slower. */
499 		bo = xe_bo_create_user(xe, NULL, NULL, XE_BO_SHRINK_SIZE,
500 				       DRM_XE_GEM_CPU_CACHING_WB,
501 				       XE_BO_FLAG_SYSTEM);
502 		if (IS_ERR(bo)) {
503 			if (bo != ERR_PTR(-ENOMEM) && bo != ERR_PTR(-ENOSPC) &&
504 			    bo != ERR_PTR(-EINTR) && bo != ERR_PTR(-ERESTARTSYS))
505 				KUNIT_FAIL(test, "Error creating bo: %pe\n", bo);
506 			kfree(link);
507 			failed = true;
508 			break;
509 		}
510 		xe_bo_lock(bo, false);
511 		xe_tt = container_of(bo->ttm.ttm, typeof(*xe_tt), ttm);
512 
513 		/*
514 		 * Allocate purgeable bos first, because if we do it the
515 		 * other way around, they may not be subject to swapping...
516 		 */
517 		if (alloced < purgeable) {
518 			xe_tt->purgeable = true;
519 			bo->ttm.priority = 0;
520 		} else {
521 			int ret = shrink_test_fill_random(bo, &prng, link);
522 
523 			if (ret) {
524 				xe_bo_unlock(bo);
525 				xe_bo_put(bo);
526 				KUNIT_FAIL(test, "Error filling bo with random data: %pe\n",
527 					   ERR_PTR(ret));
528 				kfree(link);
529 				failed = true;
530 				break;
531 			}
532 		}
533 
534 		mem_type = bo->ttm.resource->mem_type;
535 		xe_bo_unlock(bo);
536 		link->bo = bo;
537 		list_add_tail(&link->link, &bos);
538 
539 		if (mem_type != XE_PL_TT) {
540 			KUNIT_FAIL(test, "Bo in incorrect memory type: %u\n",
541 				   bo->ttm.resource->mem_type);
542 			failed = true;
543 		}
544 		cond_resched();
545 		if (signal_pending(current))
546 			break;
547 	}
548 
549 	/*
550 	 * Read back and destroy bos. Reset the pseudo-random seed to get an
551 	 * identical pseudo-random number sequence for readback.
552 	 */
553 	prandom_seed_state(&prng, rand_seed);
554 	list_for_each_entry_safe(link, next, &bos, link) {
555 		static struct ttm_operation_ctx ctx = {.interruptible = true};
556 		struct xe_bo *bo = link->bo;
557 		struct xe_ttm_tt *xe_tt;
558 		int ret;
559 
560 		count++;
561 		if (!signal_pending(current) && !failed) {
562 			bool purgeable, intr = false;
563 
564 			xe_bo_lock(bo, NULL);
565 
566 			/* xe_tt->purgeable is cleared on validate. */
567 			xe_tt = container_of(bo->ttm.ttm, typeof(*xe_tt), ttm);
568 			purgeable = xe_tt->purgeable;
569 			do {
570 				ret = ttm_bo_validate(&bo->ttm, &tt_placement, &ctx);
571 				if (ret == -EINTR)
572 					intr = true;
573 			} while (ret == -EINTR && !signal_pending(current));
574 
575 			if (!ret && !purgeable)
576 				failed = shrink_test_verify(test, bo, count, &prng, link);
577 
578 			xe_bo_unlock(bo);
579 			if (ret) {
580 				KUNIT_FAIL(test, "Validation failed: %pe\n",
581 					   ERR_PTR(ret));
582 				failed = true;
583 			} else if (intr) {
584 				interrupted++;
585 			} else {
586 				successful++;
587 			}
588 		}
589 		xe_bo_put(link->bo);
590 		list_del(&link->link);
591 		kfree(link);
592 	}
593 	kunit_info(test, "Readbacks interrupted: %u successful: %u\n",
594 		   interrupted, successful);
595 
596 	return 0;
597 }
598 
599 static void xe_bo_shrink_kunit(struct kunit *test)
600 {
601 	struct xe_device *xe = test->priv;
602 
603 	shrink_test_run_device(xe);
604 }
605 
606 static struct kunit_case xe_bo_tests[] = {
607 	KUNIT_CASE_PARAM(xe_ccs_migrate_kunit, xe_pci_live_device_gen_param),
608 	KUNIT_CASE_PARAM(xe_bo_evict_kunit, xe_pci_live_device_gen_param),
609 	{}
610 };
611 
612 VISIBLE_IF_KUNIT
613 struct kunit_suite xe_bo_test_suite = {
614 	.name = "xe_bo",
615 	.test_cases = xe_bo_tests,
616 	.init = xe_kunit_helper_xe_device_live_test_init,
617 };
618 EXPORT_SYMBOL_IF_KUNIT(xe_bo_test_suite);
619 
620 static struct kunit_case xe_bo_shrink_test[] = {
621 	KUNIT_CASE_PARAM_ATTR(xe_bo_shrink_kunit, xe_pci_live_device_gen_param,
622 			      {.speed = KUNIT_SPEED_SLOW}),
623 	{}
624 };
625 
626 VISIBLE_IF_KUNIT
627 struct kunit_suite xe_bo_shrink_test_suite = {
628 	.name = "xe_bo_shrink",
629 	.test_cases = xe_bo_shrink_test,
630 	.init = xe_kunit_helper_xe_device_live_test_init,
631 };
632 EXPORT_SYMBOL_IF_KUNIT(xe_bo_shrink_test_suite);
633