1 // SPDX-License-Identifier: MIT
3 * Copyright © 2020-2022 Intel Corporation
6 #include <kunit/test.h>
7 #include <kunit/visibility.h>
9 #include "tests/xe_migrate_test.h"
10 #include "tests/xe_pci_test.h"
15 static bool sanity_fence_failed(struct xe_device *xe, struct dma_fence *fence,
16 const char *str, struct kunit *test)
21 KUNIT_FAIL(test, "Failed to create fence for %s: %li\n", str,
28 ret = dma_fence_wait_timeout(fence, false, 5 * HZ);
30 KUNIT_FAIL(test, "Fence timed out for %s: %li\n", str, ret);
37 static int run_sanity_job(struct xe_migrate *m, struct xe_device *xe,
38 struct xe_bb *bb, u32 second_idx, const char *str,
41 u64 batch_base = xe_migrate_batch_base(m, xe->info.has_usm);
42 struct xe_sched_job *job = xe_bb_create_migration_job(m->q, bb,
45 struct dma_fence *fence;
48 KUNIT_FAIL(test, "Failed to allocate fake pt: %li\n",
53 xe_sched_job_arm(job);
54 fence = dma_fence_get(&job->drm.s_fence->finished);
55 xe_sched_job_push(job);
57 if (sanity_fence_failed(xe, fence, str, test))
61 kunit_info(test, "%s: Job completed\n", str);
66 sanity_populate_cb(struct xe_migrate_pt_update *pt_update,
67 struct xe_tile *tile, struct iosys_map *map, void *dst,
68 u32 qword_ofs, u32 num_qwords,
69 const struct xe_vm_pgtable_update *update)
71 struct migrate_test_params *p =
72 to_migrate_test_params(xe_cur_kunit_priv(XE_TEST_LIVE_MIGRATE));
77 for (i = 0; i < num_qwords; i++) {
78 value = (qword_ofs + i - update->ofs) * 0x1111111111111111ULL;
80 xe_map_wr(tile_to_xe(tile), map, (qword_ofs + i) *
81 sizeof(u64), u64, value);
86 kunit_info(xe_cur_kunit(), "Used %s.\n", map ? "CPU" : "GPU");
87 if (p->force_gpu && map)
88 KUNIT_FAIL(xe_cur_kunit(), "GPU pagetable update used CPU.\n");
91 static const struct xe_migrate_pt_update_ops sanity_ops = {
92 .populate = sanity_populate_cb,
95 #define check(_retval, _expected, str, _test) \
96 do { if ((_retval) != (_expected)) { \
97 KUNIT_FAIL(_test, "Sanity check failed: " str \
98 " expected %llx, got %llx\n", \
99 (u64)(_expected), (u64)(_retval)); \
102 static void test_copy(struct xe_migrate *m, struct xe_bo *bo,
103 struct kunit *test, u32 region)
105 struct xe_device *xe = tile_to_xe(m->tile);
106 u64 retval, expected = 0;
107 bool big = bo->size >= SZ_2M;
108 struct dma_fence *fence;
109 const char *str = big ? "Copying big bo" : "Copying small bo";
112 struct xe_bo *remote = xe_bo_create_locked(xe, m->tile, NULL,
116 XE_BO_FLAG_NEEDS_CPU_ACCESS);
117 if (IS_ERR(remote)) {
118 KUNIT_FAIL(test, "Failed to allocate remote bo for %s: %pe\n",
123 err = xe_bo_validate(remote, NULL, false);
125 KUNIT_FAIL(test, "Failed to validate system bo for %s: %i\n",
130 err = xe_bo_vmap(remote);
132 KUNIT_FAIL(test, "Failed to vmap system bo for %s: %i\n",
137 xe_map_memset(xe, &remote->vmap, 0, 0xd0, remote->size);
138 fence = xe_migrate_clear(m, remote, remote->ttm.resource);
139 if (!sanity_fence_failed(xe, fence, big ? "Clearing remote big bo" :
140 "Clearing remote small bo", test)) {
141 retval = xe_map_rd(xe, &remote->vmap, 0, u64);
142 check(retval, expected, "remote first offset should be cleared",
144 retval = xe_map_rd(xe, &remote->vmap, remote->size - 8, u64);
145 check(retval, expected, "remote last offset should be cleared",
148 dma_fence_put(fence);
150 /* Try to copy 0xc0 from remote to vram with 2MB or 64KiB/4KiB pages */
151 xe_map_memset(xe, &remote->vmap, 0, 0xc0, remote->size);
152 xe_map_memset(xe, &bo->vmap, 0, 0xd0, bo->size);
154 expected = 0xc0c0c0c0c0c0c0c0;
155 fence = xe_migrate_copy(m, remote, bo, remote->ttm.resource,
156 bo->ttm.resource, false);
157 if (!sanity_fence_failed(xe, fence, big ? "Copying big bo remote -> vram" :
158 "Copying small bo remote -> vram", test)) {
159 retval = xe_map_rd(xe, &bo->vmap, 0, u64);
160 check(retval, expected,
161 "remote -> vram bo first offset should be copied", test);
162 retval = xe_map_rd(xe, &bo->vmap, bo->size - 8, u64);
163 check(retval, expected,
164 "remote -> vram bo offset should be copied", test);
166 dma_fence_put(fence);
168 /* And other way around.. slightly hacky.. */
169 xe_map_memset(xe, &remote->vmap, 0, 0xd0, remote->size);
170 xe_map_memset(xe, &bo->vmap, 0, 0xc0, bo->size);
172 fence = xe_migrate_copy(m, bo, remote, bo->ttm.resource,
173 remote->ttm.resource, false);
174 if (!sanity_fence_failed(xe, fence, big ? "Copying big bo vram -> remote" :
175 "Copying small bo vram -> remote", test)) {
176 retval = xe_map_rd(xe, &remote->vmap, 0, u64);
177 check(retval, expected,
178 "vram -> remote bo first offset should be copied", test);
179 retval = xe_map_rd(xe, &remote->vmap, bo->size - 8, u64);
180 check(retval, expected,
181 "vram -> remote bo last offset should be copied", test);
183 dma_fence_put(fence);
185 xe_bo_vunmap(remote);
187 xe_bo_unlock(remote);
191 static void test_copy_sysmem(struct xe_migrate *m, struct xe_bo *bo,
194 test_copy(m, bo, test, XE_BO_FLAG_SYSTEM);
197 static void test_copy_vram(struct xe_migrate *m, struct xe_bo *bo,
202 if (bo->ttm.resource->mem_type == XE_PL_SYSTEM)
205 if (bo->ttm.resource->mem_type == XE_PL_VRAM0)
206 region = XE_BO_FLAG_VRAM1;
208 region = XE_BO_FLAG_VRAM0;
209 test_copy(m, bo, test, region);
212 static void test_pt_update(struct xe_migrate *m, struct xe_bo *pt,
213 struct kunit *test, bool force_gpu)
215 struct xe_device *xe = tile_to_xe(m->tile);
216 struct dma_fence *fence;
217 u64 retval, expected;
221 struct xe_vm_pgtable_update update = {
226 struct xe_migrate_pt_update pt_update = {
229 struct migrate_test_params p = {
230 .base.id = XE_TEST_LIVE_MIGRATE,
231 .force_gpu = force_gpu,
235 /* Test xe_migrate_update_pgtables() updates the pagetable as expected */
236 expected = 0xf0f0f0f0f0f0f0f0ULL;
237 xe_map_memset(xe, &pt->vmap, 0, (u8)expected, pt->size);
240 fence = xe_migrate_update_pgtables(m, m->q->vm, NULL, m->q, &update, 1,
241 NULL, 0, &pt_update);
243 if (sanity_fence_failed(xe, fence, "Migration pagetable update", test))
246 kunit_info(test, "Updating without syncing took %llu us,\n",
247 (unsigned long long)ktime_to_us(ktime_sub(now, then)));
249 dma_fence_put(fence);
250 retval = xe_map_rd(xe, &pt->vmap, 0, u64);
251 check(retval, expected, "PTE[0] must stay untouched", test);
253 for (i = 0; i < update.qwords; i++) {
254 retval = xe_map_rd(xe, &pt->vmap, (update.ofs + i) * 8, u64);
255 check(retval, i * 0x1111111111111111ULL, "PTE update", test);
258 retval = xe_map_rd(xe, &pt->vmap, 8 * (update.ofs + update.qwords),
260 check(retval, expected, "PTE[0x11] must stay untouched", test);
263 static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test)
265 struct xe_tile *tile = m->tile;
266 struct xe_device *xe = tile_to_xe(tile);
267 struct xe_bo *pt, *bo = m->pt_bo, *big, *tiny;
268 struct xe_res_cursor src_it;
269 struct dma_fence *fence;
270 u64 retval, expected;
275 err = xe_bo_vmap(bo);
277 KUNIT_FAIL(test, "Failed to vmap our pagetables: %li\n",
282 big = xe_bo_create_pin_map(xe, tile, m->q->vm, SZ_4M,
284 XE_BO_FLAG_VRAM_IF_DGFX(tile) |
287 KUNIT_FAIL(test, "Failed to allocate bo: %li\n", PTR_ERR(big));
291 pt = xe_bo_create_pin_map(xe, tile, m->q->vm, XE_PAGE_SIZE,
293 XE_BO_FLAG_VRAM_IF_DGFX(tile) |
296 KUNIT_FAIL(test, "Failed to allocate fake pt: %li\n",
301 tiny = xe_bo_create_pin_map(xe, tile, m->q->vm,
304 XE_BO_FLAG_VRAM_IF_DGFX(tile) |
307 KUNIT_FAIL(test, "Failed to allocate fake pt: %li\n",
312 bb = xe_bb_new(tile->primary_gt, 32, xe->info.has_usm);
314 KUNIT_FAIL(test, "Failed to create batchbuffer: %li\n",
319 kunit_info(test, "Starting tests, top level PT addr: %lx, special pagetable base addr: %lx\n",
320 (unsigned long)xe_bo_main_addr(m->q->vm->pt_root[id]->bo, XE_PAGE_SIZE),
321 (unsigned long)xe_bo_main_addr(m->pt_bo, XE_PAGE_SIZE));
323 /* First part of the test, are we updating our pagetable bo with a new entry? */
324 xe_map_wr(xe, &bo->vmap, XE_PAGE_SIZE * (NUM_KERNEL_PDE - 1), u64,
326 expected = m->q->vm->pt_ops->pte_encode_bo(pt, 0, xe->pat.idx[XE_CACHE_WB], 0);
327 if (m->q->vm->flags & XE_VM_FLAG_64K)
328 expected |= XE_PTE_PS64;
329 if (xe_bo_is_vram(pt))
330 xe_res_first(pt->ttm.resource, 0, pt->size, &src_it);
332 xe_res_first_sg(xe_bo_sg(pt), 0, pt->size, &src_it);
334 emit_pte(m, bb, NUM_KERNEL_PDE - 1, xe_bo_is_vram(pt), false,
335 &src_it, XE_PAGE_SIZE, pt->ttm.resource);
337 run_sanity_job(m, xe, bb, bb->len, "Writing PTE for our fake PT", test);
339 retval = xe_map_rd(xe, &bo->vmap, XE_PAGE_SIZE * (NUM_KERNEL_PDE - 1),
341 check(retval, expected, "PTE entry write", test);
343 /* Now try to write data to our newly mapped 'pagetable', see if it succeeds */
345 bb->cs[bb->len++] = MI_BATCH_BUFFER_END;
346 xe_map_wr(xe, &pt->vmap, 0, u32, 0xdeaddead);
349 emit_clear(tile->primary_gt, bb, xe_migrate_vm_addr(NUM_KERNEL_PDE - 1, 0), 4, 4,
351 run_sanity_job(m, xe, bb, 1, "Writing to our newly mapped pagetable",
354 retval = xe_map_rd(xe, &pt->vmap, 0, u32);
355 check(retval, expected, "Write to PT after adding PTE", test);
357 /* Sanity checks passed, try the full ones! */
359 /* Clear a small bo */
360 kunit_info(test, "Clearing small buffer object\n");
361 xe_map_memset(xe, &tiny->vmap, 0, 0x22, tiny->size);
363 fence = xe_migrate_clear(m, tiny, tiny->ttm.resource);
364 if (sanity_fence_failed(xe, fence, "Clearing small bo", test))
367 dma_fence_put(fence);
368 retval = xe_map_rd(xe, &tiny->vmap, 0, u32);
369 check(retval, expected, "Command clear small first value", test);
370 retval = xe_map_rd(xe, &tiny->vmap, tiny->size - 4, u32);
371 check(retval, expected, "Command clear small last value", test);
373 kunit_info(test, "Copying small buffer object to system\n");
374 test_copy_sysmem(m, tiny, test);
375 if (xe->info.tile_count > 1) {
376 kunit_info(test, "Copying small buffer object to other vram\n");
377 test_copy_vram(m, tiny, test);
381 kunit_info(test, "Clearing big buffer object\n");
382 xe_map_memset(xe, &big->vmap, 0, 0x11, big->size);
384 fence = xe_migrate_clear(m, big, big->ttm.resource);
385 if (sanity_fence_failed(xe, fence, "Clearing big bo", test))
388 dma_fence_put(fence);
389 retval = xe_map_rd(xe, &big->vmap, 0, u32);
390 check(retval, expected, "Command clear big first value", test);
391 retval = xe_map_rd(xe, &big->vmap, big->size - 4, u32);
392 check(retval, expected, "Command clear big last value", test);
394 kunit_info(test, "Copying big buffer object to system\n");
395 test_copy_sysmem(m, big, test);
396 if (xe->info.tile_count > 1) {
397 kunit_info(test, "Copying big buffer object to other vram\n");
398 test_copy_vram(m, big, test);
401 kunit_info(test, "Testing page table update using CPU if GPU idle.\n");
402 test_pt_update(m, pt, test, false);
403 kunit_info(test, "Testing page table update using GPU\n");
404 test_pt_update(m, pt, test, true);
407 xe_bb_free(bb, NULL);
418 xe_bo_vunmap(m->pt_bo);
421 static int migrate_test_run_device(struct xe_device *xe)
423 struct kunit *test = xe_cur_kunit();
424 struct xe_tile *tile;
427 xe_pm_runtime_get(xe);
429 for_each_tile(tile, xe, id) {
430 struct xe_migrate *m = tile->migrate;
432 kunit_info(test, "Testing tile id %d.\n", id);
433 xe_vm_lock(m->q->vm, true);
434 xe_migrate_sanity_test(m, test);
435 xe_vm_unlock(m->q->vm);
438 xe_pm_runtime_put(xe);
443 void xe_migrate_sanity_kunit(struct kunit *test)
445 xe_call_for_each_device(migrate_test_run_device);
447 EXPORT_SYMBOL_IF_KUNIT(xe_migrate_sanity_kunit);