a3bace16282ee80da4c3b9f39ec0a52f73c3ace9
[linux-2.6-microblaze.git] / drivers / gpu / drm / xe / tests / xe_migrate.c
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2020-2022 Intel Corporation
4  */
5
6 #include <kunit/test.h>
7
8 #include "tests/xe_migrate_test.h"
9
10 #include "xe_pci.h"
11
12 static bool sanity_fence_failed(struct xe_device *xe, struct dma_fence *fence,
13                                 const char *str, struct kunit *test)
14 {
15         long ret;
16
17         if (IS_ERR(fence)) {
18                 KUNIT_FAIL(test, "Failed to create fence for %s: %li\n", str,
19                            PTR_ERR(fence));
20                 return true;
21         }
22         if (!fence)
23                 return true;
24
25         ret = dma_fence_wait_timeout(fence, false, 5 * HZ);
26         if (ret <= 0) {
27                 KUNIT_FAIL(test, "Fence timed out for %s: %li\n", str, ret);
28                 return true;
29         }
30
31         return false;
32 }
33
34 static int run_sanity_job(struct xe_migrate *m, struct xe_device *xe,
35                           struct xe_bb *bb, u32 second_idx, const char *str,
36                           struct kunit *test)
37 {
38         struct xe_sched_job *job = xe_bb_create_migration_job(m->eng, bb,
39                                                               m->batch_base_ofs,
40                                                               second_idx);
41         struct dma_fence *fence;
42
43         if (IS_ERR(job)) {
44                 KUNIT_FAIL(test, "Failed to allocate fake pt: %li\n",
45                            PTR_ERR(job));
46                 return PTR_ERR(job);
47         }
48
49         xe_sched_job_arm(job);
50         fence = dma_fence_get(&job->drm.s_fence->finished);
51         xe_sched_job_push(job);
52
53         if (sanity_fence_failed(xe, fence, str, test))
54                 return -ETIMEDOUT;
55
56         dma_fence_put(fence);
57         kunit_info(test, "%s: Job completed\n", str);
58         return 0;
59 }
60
61 static void
62 sanity_populate_cb(struct xe_migrate_pt_update *pt_update,
63                    struct xe_gt *gt, struct iosys_map *map, void *dst,
64                    u32 qword_ofs, u32 num_qwords,
65                    const struct xe_vm_pgtable_update *update)
66 {
67         int i;
68         u64 *ptr = dst;
69         u64 value;
70
71         for (i = 0; i < num_qwords; i++) {
72                 value = (qword_ofs + i - update->ofs) * 0x1111111111111111ULL;
73                 if (map)
74                         xe_map_wr(gt_to_xe(gt), map, (qword_ofs + i) *
75                                   sizeof(u64), u64, value);
76                 else
77                         ptr[i] = value;
78         }
79 }
80
81 static const struct xe_migrate_pt_update_ops sanity_ops = {
82         .populate = sanity_populate_cb,
83 };
84
85 #define check(_retval, _expected, str, _test)                           \
86         do { if ((_retval) != (_expected)) {                            \
87                         KUNIT_FAIL(_test, "Sanity check failed: " str   \
88                                    " expected %llx, got %llx\n",        \
89                                    (u64)(_expected), (u64)(_retval));   \
90                 } } while (0)
91
92 static void test_copy(struct xe_migrate *m, struct xe_bo *bo,
93                       struct kunit *test)
94 {
95         struct xe_device *xe = gt_to_xe(m->gt);
96         u64 retval, expected = 0xc0c0c0c0c0c0c0c0ULL;
97         bool big = bo->size >= SZ_2M;
98         struct dma_fence *fence;
99         const char *str = big ? "Copying big bo" : "Copying small bo";
100         int err;
101
102         struct xe_bo *sysmem = xe_bo_create_locked(xe, m->gt, NULL,
103                                                    bo->size,
104                                                    ttm_bo_type_kernel,
105                                                    XE_BO_CREATE_SYSTEM_BIT);
106         if (IS_ERR(sysmem)) {
107                 KUNIT_FAIL(test, "Failed to allocate sysmem bo for %s: %li\n",
108                            str, PTR_ERR(sysmem));
109                 return;
110         }
111
112         err = xe_bo_validate(sysmem, NULL, false);
113         if (err) {
114                 KUNIT_FAIL(test, "Failed to validate system bo for %s: %li\n",
115                            str, err);
116                 goto out_unlock;
117         }
118
119         err = xe_bo_vmap(sysmem);
120         if (err) {
121                 KUNIT_FAIL(test, "Failed to vmap system bo for %s: %li\n",
122                            str, err);
123                 goto out_unlock;
124         }
125
126         xe_map_memset(xe, &sysmem->vmap, 0, 0xd0, sysmem->size);
127         fence = xe_migrate_clear(m, sysmem, sysmem->ttm.resource, 0xc0c0c0c0);
128         if (!sanity_fence_failed(xe, fence, big ? "Clearing sysmem big bo" :
129                                  "Clearing sysmem small bo", test)) {
130                 retval = xe_map_rd(xe, &sysmem->vmap, 0, u64);
131                 check(retval, expected, "sysmem first offset should be cleared",
132                       test);
133                 retval = xe_map_rd(xe, &sysmem->vmap, sysmem->size - 8, u64);
134                 check(retval, expected, "sysmem last offset should be cleared",
135                       test);
136         }
137         dma_fence_put(fence);
138
139         /* Try to copy 0xc0 from sysmem to vram with 2MB or 64KiB/4KiB pages */
140         xe_map_memset(xe, &sysmem->vmap, 0, 0xc0, sysmem->size);
141         xe_map_memset(xe, &bo->vmap, 0, 0xd0, bo->size);
142
143         fence = xe_migrate_copy(m, sysmem, sysmem->ttm.resource,
144                                 bo->ttm.resource);
145         if (!sanity_fence_failed(xe, fence, big ? "Copying big bo sysmem -> vram" :
146                                  "Copying small bo sysmem -> vram", test)) {
147                 retval = xe_map_rd(xe, &bo->vmap, 0, u64);
148                 check(retval, expected,
149                       "sysmem -> vram bo first offset should be copied", test);
150                 retval = xe_map_rd(xe, &bo->vmap, bo->size - 8, u64);
151                 check(retval, expected,
152                       "sysmem -> vram bo offset should be copied", test);
153         }
154         dma_fence_put(fence);
155
156         /* And other way around.. slightly hacky.. */
157         xe_map_memset(xe, &sysmem->vmap, 0, 0xd0, sysmem->size);
158         xe_map_memset(xe, &bo->vmap, 0, 0xc0, bo->size);
159
160         fence = xe_migrate_copy(m, sysmem, bo->ttm.resource,
161                                 sysmem->ttm.resource);
162         if (!sanity_fence_failed(xe, fence, big ? "Copying big bo vram -> sysmem" :
163                                  "Copying small bo vram -> sysmem", test)) {
164                 retval = xe_map_rd(xe, &sysmem->vmap, 0, u64);
165                 check(retval, expected,
166                       "vram -> sysmem bo first offset should be copied", test);
167                 retval = xe_map_rd(xe, &sysmem->vmap, bo->size - 8, u64);
168                 check(retval, expected,
169                       "vram -> sysmem bo last offset should be copied", test);
170         }
171         dma_fence_put(fence);
172
173         xe_bo_vunmap(sysmem);
174 out_unlock:
175         xe_bo_unlock_no_vm(sysmem);
176         xe_bo_put(sysmem);
177 }
178
179 static void test_pt_update(struct xe_migrate *m, struct xe_bo *pt,
180                            struct kunit *test)
181 {
182         struct xe_device *xe = gt_to_xe(m->gt);
183         struct dma_fence *fence;
184         u64 retval, expected;
185         int i;
186
187         struct xe_vm_pgtable_update update = {
188                 .ofs = 1,
189                 .qwords = 0x10,
190                 .pt_bo = pt,
191         };
192         struct xe_migrate_pt_update pt_update = {
193                 .ops = &sanity_ops,
194         };
195
196         /* Test xe_migrate_update_pgtables() updates the pagetable as expected */
197         expected = 0xf0f0f0f0f0f0f0f0ULL;
198         xe_map_memset(xe, &pt->vmap, 0, (u8)expected, pt->size);
199
200         fence = xe_migrate_update_pgtables(m, NULL, NULL, m->eng, &update, 1,
201                                            NULL, 0, &pt_update);
202         if (sanity_fence_failed(xe, fence, "Migration pagetable update", test))
203                 return;
204
205         dma_fence_put(fence);
206         retval = xe_map_rd(xe, &pt->vmap, 0, u64);
207         check(retval, expected, "PTE[0] must stay untouched", test);
208
209         for (i = 0; i < update.qwords; i++) {
210                 retval = xe_map_rd(xe, &pt->vmap, (update.ofs + i) * 8, u64);
211                 check(retval, i * 0x1111111111111111ULL, "PTE update", test);
212         }
213
214         retval = xe_map_rd(xe, &pt->vmap, 8 * (update.ofs + update.qwords),
215                            u64);
216         check(retval, expected, "PTE[0x11] must stay untouched", test);
217 }
218
219 static void xe_migrate_sanity_test(struct xe_migrate *m, struct kunit *test)
220 {
221         struct xe_gt *gt = m->gt;
222         struct xe_device *xe = gt_to_xe(gt);
223         struct xe_bo *pt, *bo = m->pt_bo, *big, *tiny;
224         struct xe_res_cursor src_it;
225         struct dma_fence *fence;
226         u64 retval, expected;
227         struct xe_bb *bb;
228         int err;
229         u8 id = gt->info.id;
230
231         err = xe_bo_vmap(bo);
232         if (err) {
233                 KUNIT_FAIL(test, "Failed to vmap our pagetables: %li\n",
234                            PTR_ERR(bo));
235                 return;
236         }
237
238         big = xe_bo_create_pin_map(xe, m->gt, m->eng->vm, SZ_4M,
239                                    ttm_bo_type_kernel,
240                                    XE_BO_CREATE_VRAM_IF_DGFX(m->gt) |
241                                    XE_BO_CREATE_PINNED_BIT);
242         if (IS_ERR(big)) {
243                 KUNIT_FAIL(test, "Failed to allocate bo: %li\n", PTR_ERR(big));
244                 goto vunmap;
245         }
246
247         pt = xe_bo_create_pin_map(xe, m->gt, m->eng->vm, GEN8_PAGE_SIZE,
248                                   ttm_bo_type_kernel,
249                                   XE_BO_CREATE_VRAM_IF_DGFX(m->gt) |
250                                   XE_BO_CREATE_PINNED_BIT);
251         if (IS_ERR(pt)) {
252                 KUNIT_FAIL(test, "Failed to allocate fake pt: %li\n",
253                            PTR_ERR(pt));
254                 goto free_big;
255         }
256
257         tiny = xe_bo_create_pin_map(xe, m->gt, m->eng->vm,
258                                     2 * SZ_4K,
259                                     ttm_bo_type_kernel,
260                                     XE_BO_CREATE_VRAM_IF_DGFX(m->gt) |
261                                     XE_BO_CREATE_PINNED_BIT);
262         if (IS_ERR(tiny)) {
263                 KUNIT_FAIL(test, "Failed to allocate fake pt: %li\n",
264                            PTR_ERR(pt));
265                 goto free_pt;
266         }
267
268         bb = xe_bb_new(m->gt, 32, xe->info.supports_usm);
269         if (IS_ERR(bb)) {
270                 KUNIT_FAIL(test, "Failed to create batchbuffer: %li\n",
271                            PTR_ERR(bb));
272                 goto free_tiny;
273         }
274
275         kunit_info(test, "Starting tests, top level PT addr: %lx, special pagetable base addr: %lx\n",
276                    (unsigned long)xe_bo_main_addr(m->eng->vm->pt_root[id]->bo, GEN8_PAGE_SIZE),
277                    (unsigned long)xe_bo_main_addr(m->pt_bo, GEN8_PAGE_SIZE));
278
279         /* First part of the test, are we updating our pagetable bo with a new entry? */
280         xe_map_wr(xe, &bo->vmap, GEN8_PAGE_SIZE * (NUM_KERNEL_PDE - 1), u64, 0xdeaddeadbeefbeef);
281         expected = gen8_pte_encode(NULL, pt, 0, XE_CACHE_WB, 0, 0);
282         if (m->eng->vm->flags & XE_VM_FLAGS_64K)
283                 expected |= GEN12_PTE_PS64;
284         xe_res_first(pt->ttm.resource, 0, pt->size, &src_it);
285         emit_pte(m, bb, NUM_KERNEL_PDE - 1, xe_bo_is_vram(pt),
286                  &src_it, GEN8_PAGE_SIZE, pt);
287         run_sanity_job(m, xe, bb, bb->len, "Writing PTE for our fake PT", test);
288
289         retval = xe_map_rd(xe, &bo->vmap, GEN8_PAGE_SIZE * (NUM_KERNEL_PDE - 1),
290                            u64);
291         check(retval, expected, "PTE entry write", test);
292
293         /* Now try to write data to our newly mapped 'pagetable', see if it succeeds */
294         bb->len = 0;
295         bb->cs[bb->len++] = MI_BATCH_BUFFER_END;
296         xe_map_wr(xe, &pt->vmap, 0, u32, 0xdeaddead);
297         expected = 0x12345678U;
298
299         emit_clear(m->gt, bb, xe_migrate_vm_addr(NUM_KERNEL_PDE - 1, 0), 4, 4,
300                    expected, IS_DGFX(xe));
301         run_sanity_job(m, xe, bb, 1, "Writing to our newly mapped pagetable",
302                        test);
303
304         retval = xe_map_rd(xe, &pt->vmap, 0, u32);
305         check(retval, expected, "Write to PT after adding PTE", test);
306
307         /* Sanity checks passed, try the full ones! */
308
309         /* Clear a small bo */
310         kunit_info(test, "Clearing small buffer object\n");
311         xe_map_memset(xe, &tiny->vmap, 0, 0x22, tiny->size);
312         expected = 0x224488ff;
313         fence = xe_migrate_clear(m, tiny, tiny->ttm.resource, expected);
314         if (sanity_fence_failed(xe, fence, "Clearing small bo", test))
315                 goto out;
316
317         dma_fence_put(fence);
318         retval = xe_map_rd(xe, &tiny->vmap, 0, u32);
319         check(retval, expected, "Command clear small first value", test);
320         retval = xe_map_rd(xe, &tiny->vmap, tiny->size - 4, u32);
321         check(retval, expected, "Command clear small last value", test);
322
323         if (IS_DGFX(xe)) {
324                 kunit_info(test, "Copying small buffer object to system\n");
325                 test_copy(m, tiny, test);
326         }
327
328         /* Clear a big bo with a fixed value */
329         kunit_info(test, "Clearing big buffer object\n");
330         xe_map_memset(xe, &big->vmap, 0, 0x11, big->size);
331         expected = 0x11223344U;
332         fence = xe_migrate_clear(m, big, big->ttm.resource, expected);
333         if (sanity_fence_failed(xe, fence, "Clearing big bo", test))
334                 goto out;
335
336         dma_fence_put(fence);
337         retval = xe_map_rd(xe, &big->vmap, 0, u32);
338         check(retval, expected, "Command clear big first value", test);
339         retval = xe_map_rd(xe, &big->vmap, big->size - 4, u32);
340         check(retval, expected, "Command clear big last value", test);
341
342         if (IS_DGFX(xe)) {
343                 kunit_info(test, "Copying big buffer object to system\n");
344                 test_copy(m, big, test);
345         }
346
347         test_pt_update(m, pt, test);
348
349 out:
350         xe_bb_free(bb, NULL);
351 free_tiny:
352         xe_bo_unpin(tiny);
353         xe_bo_put(tiny);
354 free_pt:
355         xe_bo_unpin(pt);
356         xe_bo_put(pt);
357 free_big:
358         xe_bo_unpin(big);
359         xe_bo_put(big);
360 vunmap:
361         xe_bo_vunmap(m->pt_bo);
362 }
363
364 static int migrate_test_run_device(struct xe_device *xe)
365 {
366         struct kunit *test = xe_cur_kunit();
367         struct xe_gt *gt;
368         int id;
369
370         for_each_gt(gt, xe, id) {
371                 struct xe_migrate *m = gt->migrate;
372                 struct ww_acquire_ctx ww;
373
374                 kunit_info(test, "Testing gt id %d.\n", id);
375                 xe_vm_lock(m->eng->vm, &ww, 0, true);
376                 xe_device_mem_access_get(xe);
377                 xe_migrate_sanity_test(m, test);
378                 xe_device_mem_access_put(xe);
379                 xe_vm_unlock(m->eng->vm, &ww);
380         }
381
382         return 0;
383 }
384
385 void xe_migrate_sanity_kunit(struct kunit *test)
386 {
387         xe_call_for_each_device(migrate_test_run_device);
388 }
389 EXPORT_SYMBOL(xe_migrate_sanity_kunit);