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"
14 static bool sanity_fence_failed(struct xe_device *xe, struct dma_fence *fence,
15 const char *str, struct kunit *test)
20 KUNIT_FAIL(test, "Failed to create fence for %s: %li\n", str,
27 ret = dma_fence_wait_timeout(fence, false, 5 * HZ);
29 KUNIT_FAIL(test, "Fence timed out for %s: %li\n", str, ret);
36 static int run_sanity_job(struct xe_migrate *m, struct xe_device *xe,
37 struct xe_bb *bb, u32 second_idx, const char *str,
40 u64 batch_base = xe_migrate_batch_base(m, xe->info.has_usm);
41 struct xe_sched_job *job = xe_bb_create_migration_job(m->q, bb,
44 struct dma_fence *fence;
47 KUNIT_FAIL(test, "Failed to allocate fake pt: %li\n",
52 xe_sched_job_arm(job);
53 fence = dma_fence_get(&job->drm.s_fence->finished);
54 xe_sched_job_push(job);
56 if (sanity_fence_failed(xe, fence, str, test))
60 kunit_info(test, "%s: Job completed\n", str);
65 sanity_populate_cb(struct xe_migrate_pt_update *pt_update,
66 struct xe_tile *tile, struct iosys_map *map, void *dst,
67 u32 qword_ofs, u32 num_qwords,
68 const struct xe_vm_pgtable_update *update)
70 struct migrate_test_params *p =
71 to_migrate_test_params(xe_cur_kunit_priv(XE_TEST_LIVE_MIGRATE));
76 for (i = 0; i < num_qwords; i++) {
77 value = (qword_ofs + i - update->ofs) * 0x1111111111111111ULL;
79 xe_map_wr(tile_to_xe(tile), map, (qword_ofs + i) *
80 sizeof(u64), u64, value);
85 kunit_info(xe_cur_kunit(), "Used %s.\n", map ? "CPU" : "GPU");
86 if (p->force_gpu && map)
87 KUNIT_FAIL(xe_cur_kunit(), "GPU pagetable update used CPU.\n");
90 static const struct xe_migrate_pt_update_ops sanity_ops = {
91 .populate = sanity_populate_cb,
94 #define check(_retval, _expected, str, _test) \
95 do { if ((_retval) != (_expected)) { \
96 KUNIT_FAIL(_test, "Sanity check failed: " str \
97 " expected %llx, got %llx\n", \
98 (u64)(_expected), (u64)(_retval)); \
101 static void test_copy(struct xe_migrate *m, struct xe_bo *bo,
102 struct kunit *test, u32 region)
104 struct xe_device *xe = tile_to_xe(m->tile);
105 u64 retval, expected = 0;
106 bool big = bo->size >= SZ_2M;
107 struct dma_fence *fence;
108 const char *str = big ? "Copying big bo" : "Copying small bo";
111 struct xe_bo *remote = xe_bo_create_locked(xe, m->tile, NULL,
115 XE_BO_NEEDS_CPU_ACCESS);
116 if (IS_ERR(remote)) {
117 KUNIT_FAIL(test, "Failed to allocate remote bo for %s: %pe\n",
122 err = xe_bo_validate(remote, NULL, false);
124 KUNIT_FAIL(test, "Failed to validate system bo for %s: %i\n",
129 err = xe_bo_vmap(remote);
131 KUNIT_FAIL(test, "Failed to vmap system bo for %s: %i\n",
136 xe_map_memset(xe, &remote->vmap, 0, 0xd0, remote->size);
137 fence = xe_migrate_clear(m, remote, remote->ttm.resource);
138 if (!sanity_fence_failed(xe, fence, big ? "Clearing remote big bo" :
139 "Clearing remote small bo", test)) {
140 retval = xe_map_rd(xe, &remote->vmap, 0, u64);
141 check(retval, expected, "remote first offset should be cleared",
143 retval = xe_map_rd(xe, &remote->vmap, remote->size - 8, u64);
144 check(retval, expected, "remote last offset should be cleared",
147 dma_fence_put(fence);
149 /* Try to copy 0xc0 from remote to vram with 2MB or 64KiB/4KiB pages */
150 xe_map_memset(xe, &remote->vmap, 0, 0xc0, remote->size);
151 xe_map_memset(xe, &bo->vmap, 0, 0xd0, bo->size);
153 expected = 0xc0c0c0c0c0c0c0c0;
154 fence = xe_migrate_copy(m, remote, bo, remote->ttm.resource,
155 bo->ttm.resource, false);
156 if (!sanity_fence_failed(xe, fence, big ? "Copying big bo remote -> vram" :
157 "Copying small bo remote -> vram", test)) {
158 retval = xe_map_rd(xe, &bo->vmap, 0, u64);
159 check(retval, expected,
160 "remote -> vram bo first offset should be copied", test);
161 retval = xe_map_rd(xe, &bo->vmap, bo->size - 8, u64);
162 check(retval, expected,
163 "remote -> vram bo offset should be copied", test);
165 dma_fence_put(fence);
167 /* And other way around.. slightly hacky.. */
168 xe_map_memset(xe, &remote->vmap, 0, 0xd0, remote->size);
169 xe_map_memset(xe, &bo->vmap, 0, 0xc0, bo->size);
171 fence = xe_migrate_copy(m, bo, remote, bo->ttm.resource,
172 remote->ttm.resource, false);
173 if (!sanity_fence_failed(xe, fence, big ? "Copying big bo vram -> remote" :
174 "Copying small bo vram -> remote", test)) {
175 retval = xe_map_rd(xe, &remote->vmap, 0, u64);
176 check(retval, expected,
177 "vram -> remote bo first offset should be copied", test);
178 retval = xe_map_rd(xe, &remote->vmap, bo->size - 8, u64);
179 check(retval, expected,
180 "vram -> remote bo last offset should be copied", test);
182 dma_fence_put(fence);
184 xe_bo_vunmap(remote);
186 xe_bo_unlock(remote);
190 static void test_copy_sysmem(struct xe_migrate *m, struct xe_bo *bo,
193 test_copy(m, bo, test, XE_BO_CREATE_SYSTEM_BIT);
196 static void test_copy_vram(struct xe_migrate *m, struct xe_bo *bo,
201 if (bo->ttm.resource->mem_type == XE_PL_SYSTEM)
204 if (bo->ttm.resource->mem_type == XE_PL_VRAM0)
205 region = XE_BO_CREATE_VRAM1_BIT;
207 region = XE_BO_CREATE_VRAM0_BIT;
208 test_copy(m, bo, test, region);
211 static void test_pt_update(struct xe_migrate *m, struct xe_bo *pt,
212 struct kunit *test, bool force_gpu)
214 struct xe_device *xe = tile_to_xe(m->tile);
215 struct dma_fence *fence;
216 u64 retval, expected;
220 struct xe_vm_pgtable_update update = {
225 struct xe_migrate_pt_update pt_update = {
228 struct migrate_test_params p = {
229 .base.id = XE_TEST_LIVE_MIGRATE,
230 .force_gpu = force_gpu,
234 /* Test xe_migrate_update_pgtables() updates the pagetable as expected */
235 expected = 0xf0f0f0f0f0f0f0f0ULL;
236 xe_map_memset(xe, &pt->vmap, 0, (u8)expected, pt->size);
239 fence = xe_migrate_update_pgtables(m, m->q->vm, NULL, m->q, &update, 1,
240 NULL, 0, &pt_update);
242 if (sanity_fence_failed(xe, fence, "Migration pagetable update", test))
245 kunit_info(test, "Updating without syncing took %llu us,\n",
246 (unsigned long long)ktime_to_us(ktime_sub(now, then)));
248 dma_fence_put(fence);
249 retval = xe_map_rd(xe, &pt->vmap, 0, u64);
250 check(retval, expected, "PTE[0] must stay untouched", test);
252 for (i = 0; i < update.qwords; i++) {
253 retval = xe_map_rd(xe, &pt->vmap, (update.ofs + i) * 8, u64);
254 check(retval, i * 0x1111111111111111ULL, "PTE update", test);
257 retval = xe_map_rd(xe, &pt->vmap, 8 * (update.ofs + update.qwords),
259 check(retval, expected, "PTE[0x11] must stay untouched", test);
262 static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test)
264 struct xe_tile *tile = m->tile;
265 struct xe_device *xe = tile_to_xe(tile);
266 struct xe_bo *pt, *bo = m->pt_bo, *big, *tiny;
267 struct xe_res_cursor src_it;
268 struct dma_fence *fence;
269 u64 retval, expected;
274 err = xe_bo_vmap(bo);
276 KUNIT_FAIL(test, "Failed to vmap our pagetables: %li\n",
281 big = xe_bo_create_pin_map(xe, tile, m->q->vm, SZ_4M,
283 XE_BO_CREATE_VRAM_IF_DGFX(tile) |
284 XE_BO_CREATE_PINNED_BIT);
286 KUNIT_FAIL(test, "Failed to allocate bo: %li\n", PTR_ERR(big));
290 pt = xe_bo_create_pin_map(xe, tile, m->q->vm, XE_PAGE_SIZE,
292 XE_BO_CREATE_VRAM_IF_DGFX(tile) |
293 XE_BO_CREATE_PINNED_BIT);
295 KUNIT_FAIL(test, "Failed to allocate fake pt: %li\n",
300 tiny = xe_bo_create_pin_map(xe, tile, m->q->vm,
303 XE_BO_CREATE_VRAM_IF_DGFX(tile) |
304 XE_BO_CREATE_PINNED_BIT);
306 KUNIT_FAIL(test, "Failed to allocate fake pt: %li\n",
311 bb = xe_bb_new(tile->primary_gt, 32, xe->info.has_usm);
313 KUNIT_FAIL(test, "Failed to create batchbuffer: %li\n",
318 kunit_info(test, "Starting tests, top level PT addr: %lx, special pagetable base addr: %lx\n",
319 (unsigned long)xe_bo_main_addr(m->q->vm->pt_root[id]->bo, XE_PAGE_SIZE),
320 (unsigned long)xe_bo_main_addr(m->pt_bo, XE_PAGE_SIZE));
322 /* First part of the test, are we updating our pagetable bo with a new entry? */
323 xe_map_wr(xe, &bo->vmap, XE_PAGE_SIZE * (NUM_KERNEL_PDE - 1), u64,
325 expected = m->q->vm->pt_ops->pte_encode_bo(pt, 0, xe->pat.idx[XE_CACHE_WB], 0);
326 if (m->q->vm->flags & XE_VM_FLAG_64K)
327 expected |= XE_PTE_PS64;
328 if (xe_bo_is_vram(pt))
329 xe_res_first(pt->ttm.resource, 0, pt->size, &src_it);
331 xe_res_first_sg(xe_bo_sg(pt), 0, pt->size, &src_it);
333 emit_pte(m, bb, NUM_KERNEL_PDE - 1, xe_bo_is_vram(pt), false,
334 &src_it, XE_PAGE_SIZE, pt->ttm.resource);
336 run_sanity_job(m, xe, bb, bb->len, "Writing PTE for our fake PT", test);
338 retval = xe_map_rd(xe, &bo->vmap, XE_PAGE_SIZE * (NUM_KERNEL_PDE - 1),
340 check(retval, expected, "PTE entry write", test);
342 /* Now try to write data to our newly mapped 'pagetable', see if it succeeds */
344 bb->cs[bb->len++] = MI_BATCH_BUFFER_END;
345 xe_map_wr(xe, &pt->vmap, 0, u32, 0xdeaddead);
348 emit_clear(tile->primary_gt, bb, xe_migrate_vm_addr(NUM_KERNEL_PDE - 1, 0), 4, 4,
350 run_sanity_job(m, xe, bb, 1, "Writing to our newly mapped pagetable",
353 retval = xe_map_rd(xe, &pt->vmap, 0, u32);
354 check(retval, expected, "Write to PT after adding PTE", test);
356 /* Sanity checks passed, try the full ones! */
358 /* Clear a small bo */
359 kunit_info(test, "Clearing small buffer object\n");
360 xe_map_memset(xe, &tiny->vmap, 0, 0x22, tiny->size);
362 fence = xe_migrate_clear(m, tiny, tiny->ttm.resource);
363 if (sanity_fence_failed(xe, fence, "Clearing small bo", test))
366 dma_fence_put(fence);
367 retval = xe_map_rd(xe, &tiny->vmap, 0, u32);
368 check(retval, expected, "Command clear small first value", test);
369 retval = xe_map_rd(xe, &tiny->vmap, tiny->size - 4, u32);
370 check(retval, expected, "Command clear small last value", test);
372 kunit_info(test, "Copying small buffer object to system\n");
373 test_copy_sysmem(m, tiny, test);
374 if (xe->info.tile_count > 1) {
375 kunit_info(test, "Copying small buffer object to other vram\n");
376 test_copy_vram(m, tiny, test);
380 kunit_info(test, "Clearing big buffer object\n");
381 xe_map_memset(xe, &big->vmap, 0, 0x11, big->size);
383 fence = xe_migrate_clear(m, big, big->ttm.resource);
384 if (sanity_fence_failed(xe, fence, "Clearing big bo", test))
387 dma_fence_put(fence);
388 retval = xe_map_rd(xe, &big->vmap, 0, u32);
389 check(retval, expected, "Command clear big first value", test);
390 retval = xe_map_rd(xe, &big->vmap, big->size - 4, u32);
391 check(retval, expected, "Command clear big last value", test);
393 kunit_info(test, "Copying big buffer object to system\n");
394 test_copy_sysmem(m, big, test);
395 if (xe->info.tile_count > 1) {
396 kunit_info(test, "Copying big buffer object to other vram\n");
397 test_copy_vram(m, big, test);
400 kunit_info(test, "Testing page table update using CPU if GPU idle.\n");
401 test_pt_update(m, pt, test, false);
402 kunit_info(test, "Testing page table update using GPU\n");
403 test_pt_update(m, pt, test, true);
406 xe_bb_free(bb, NULL);
417 xe_bo_vunmap(m->pt_bo);
420 static int migrate_test_run_device(struct xe_device *xe)
422 struct kunit *test = xe_cur_kunit();
423 struct xe_tile *tile;
426 for_each_tile(tile, xe, id) {
427 struct xe_migrate *m = tile->migrate;
429 kunit_info(test, "Testing tile id %d.\n", id);
430 xe_vm_lock(m->q->vm, true);
431 xe_device_mem_access_get(xe);
432 xe_migrate_sanity_test(m, test);
433 xe_device_mem_access_put(xe);
434 xe_vm_unlock(m->q->vm);
440 void xe_migrate_sanity_kunit(struct kunit *test)
442 xe_call_for_each_device(migrate_test_run_device);
444 EXPORT_SYMBOL_IF_KUNIT(xe_migrate_sanity_kunit);