1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2022 Intel Corporation
4 */
5
6 #include <linux/dma-fence-array.h>
7
8 #include "xe_pt.h"
9
10 #include "regs/xe_gtt_defs.h"
11 #include "xe_bo.h"
12 #include "xe_device.h"
13 #include "xe_drm_client.h"
14 #include "xe_exec_queue.h"
15 #include "xe_gt.h"
16 #include "xe_gt_tlb_invalidation.h"
17 #include "xe_migrate.h"
18 #include "xe_pt_types.h"
19 #include "xe_pt_walk.h"
20 #include "xe_res_cursor.h"
21 #include "xe_sched_job.h"
22 #include "xe_sync.h"
23 #include "xe_trace.h"
24 #include "xe_ttm_stolen_mgr.h"
25 #include "xe_vm.h"
26
27 struct xe_pt_dir {
28 struct xe_pt pt;
29 /** @children: Array of page-table child nodes */
30 struct xe_ptw *children[XE_PDES];
31 /** @staging: Array of page-table staging nodes */
32 struct xe_ptw *staging[XE_PDES];
33 };
34
35 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_VM)
36 #define xe_pt_set_addr(__xe_pt, __addr) ((__xe_pt)->addr = (__addr))
37 #define xe_pt_addr(__xe_pt) ((__xe_pt)->addr)
38 #else
39 #define xe_pt_set_addr(__xe_pt, __addr)
40 #define xe_pt_addr(__xe_pt) 0ull
41 #endif
42
43 static const u64 xe_normal_pt_shifts[] = {12, 21, 30, 39, 48};
44 static const u64 xe_compact_pt_shifts[] = {16, 21, 30, 39, 48};
45
46 #define XE_PT_HIGHEST_LEVEL (ARRAY_SIZE(xe_normal_pt_shifts) - 1)
47
as_xe_pt_dir(struct xe_pt * pt)48 static struct xe_pt_dir *as_xe_pt_dir(struct xe_pt *pt)
49 {
50 return container_of(pt, struct xe_pt_dir, pt);
51 }
52
53 static struct xe_pt *
xe_pt_entry_staging(struct xe_pt_dir * pt_dir,unsigned int index)54 xe_pt_entry_staging(struct xe_pt_dir *pt_dir, unsigned int index)
55 {
56 return container_of(pt_dir->staging[index], struct xe_pt, base);
57 }
58
__xe_pt_empty_pte(struct xe_tile * tile,struct xe_vm * vm,unsigned int level)59 static u64 __xe_pt_empty_pte(struct xe_tile *tile, struct xe_vm *vm,
60 unsigned int level)
61 {
62 struct xe_device *xe = tile_to_xe(tile);
63 u16 pat_index = xe->pat.idx[XE_CACHE_WB];
64 u8 id = tile->id;
65
66 if (!xe_vm_has_scratch(vm))
67 return 0;
68
69 if (level > MAX_HUGEPTE_LEVEL)
70 return vm->pt_ops->pde_encode_bo(vm->scratch_pt[id][level - 1]->bo,
71 0, pat_index);
72
73 return vm->pt_ops->pte_encode_addr(xe, 0, pat_index, level, IS_DGFX(xe), 0) |
74 XE_PTE_NULL;
75 }
76
xe_pt_free(struct xe_pt * pt)77 static void xe_pt_free(struct xe_pt *pt)
78 {
79 if (pt->level)
80 kfree(as_xe_pt_dir(pt));
81 else
82 kfree(pt);
83 }
84
85 /**
86 * xe_pt_create() - Create a page-table.
87 * @vm: The vm to create for.
88 * @tile: The tile to create for.
89 * @level: The page-table level.
90 *
91 * Allocate and initialize a single struct xe_pt metadata structure. Also
92 * create the corresponding page-table bo, but don't initialize it. If the
93 * level is grater than zero, then it's assumed to be a directory page-
94 * table and the directory structure is also allocated and initialized to
95 * NULL pointers.
96 *
97 * Return: A valid struct xe_pt pointer on success, Pointer error code on
98 * error.
99 */
xe_pt_create(struct xe_vm * vm,struct xe_tile * tile,unsigned int level)100 struct xe_pt *xe_pt_create(struct xe_vm *vm, struct xe_tile *tile,
101 unsigned int level)
102 {
103 struct xe_pt *pt;
104 struct xe_bo *bo;
105 int err;
106
107 if (level) {
108 struct xe_pt_dir *dir = kzalloc(sizeof(*dir), GFP_KERNEL);
109
110 pt = (dir) ? &dir->pt : NULL;
111 } else {
112 pt = kzalloc(sizeof(*pt), GFP_KERNEL);
113 }
114 if (!pt)
115 return ERR_PTR(-ENOMEM);
116
117 pt->level = level;
118 bo = xe_bo_create_pin_map(vm->xe, tile, vm, SZ_4K,
119 ttm_bo_type_kernel,
120 XE_BO_FLAG_VRAM_IF_DGFX(tile) |
121 XE_BO_FLAG_IGNORE_MIN_PAGE_SIZE |
122 XE_BO_FLAG_PINNED |
123 XE_BO_FLAG_NO_RESV_EVICT |
124 XE_BO_FLAG_PAGETABLE);
125 if (IS_ERR(bo)) {
126 err = PTR_ERR(bo);
127 goto err_kfree;
128 }
129 pt->bo = bo;
130 pt->base.children = level ? as_xe_pt_dir(pt)->children : NULL;
131 pt->base.staging = level ? as_xe_pt_dir(pt)->staging : NULL;
132
133 if (vm->xef)
134 xe_drm_client_add_bo(vm->xef->client, pt->bo);
135 xe_tile_assert(tile, level <= XE_VM_MAX_LEVEL);
136
137 return pt;
138
139 err_kfree:
140 xe_pt_free(pt);
141 return ERR_PTR(err);
142 }
143
144 /**
145 * xe_pt_populate_empty() - Populate a page-table bo with scratch- or zero
146 * entries.
147 * @tile: The tile the scratch pagetable of which to use.
148 * @vm: The vm we populate for.
149 * @pt: The pagetable the bo of which to initialize.
150 *
151 * Populate the page-table bo of @pt with entries pointing into the tile's
152 * scratch page-table tree if any. Otherwise populate with zeros.
153 */
xe_pt_populate_empty(struct xe_tile * tile,struct xe_vm * vm,struct xe_pt * pt)154 void xe_pt_populate_empty(struct xe_tile *tile, struct xe_vm *vm,
155 struct xe_pt *pt)
156 {
157 struct iosys_map *map = &pt->bo->vmap;
158 u64 empty;
159 int i;
160
161 if (!xe_vm_has_scratch(vm)) {
162 /*
163 * FIXME: Some memory is allocated already allocated to zero?
164 * Find out which memory that is and avoid this memset...
165 */
166 xe_map_memset(vm->xe, map, 0, 0, SZ_4K);
167 } else {
168 empty = __xe_pt_empty_pte(tile, vm, pt->level);
169 for (i = 0; i < XE_PDES; i++)
170 xe_pt_write(vm->xe, map, i, empty);
171 }
172 }
173
174 /**
175 * xe_pt_shift() - Return the ilog2 value of the size of the address range of
176 * a page-table at a certain level.
177 * @level: The level.
178 *
179 * Return: The ilog2 value of the size of the address range of a page-table
180 * at level @level.
181 */
xe_pt_shift(unsigned int level)182 unsigned int xe_pt_shift(unsigned int level)
183 {
184 return XE_PTE_SHIFT + XE_PDE_SHIFT * level;
185 }
186
187 /**
188 * xe_pt_destroy() - Destroy a page-table tree.
189 * @pt: The root of the page-table tree to destroy.
190 * @flags: vm flags. Currently unused.
191 * @deferred: List head of lockless list for deferred putting. NULL for
192 * immediate putting.
193 *
194 * Puts the page-table bo, recursively calls xe_pt_destroy on all children
195 * and finally frees @pt. TODO: Can we remove the @flags argument?
196 */
xe_pt_destroy(struct xe_pt * pt,u32 flags,struct llist_head * deferred)197 void xe_pt_destroy(struct xe_pt *pt, u32 flags, struct llist_head *deferred)
198 {
199 int i;
200
201 if (!pt)
202 return;
203
204 XE_WARN_ON(!list_empty(&pt->bo->ttm.base.gpuva.list));
205 xe_bo_unpin(pt->bo);
206 xe_bo_put_deferred(pt->bo, deferred);
207
208 if (pt->level > 0 && pt->num_live) {
209 struct xe_pt_dir *pt_dir = as_xe_pt_dir(pt);
210
211 for (i = 0; i < XE_PDES; i++) {
212 if (xe_pt_entry_staging(pt_dir, i))
213 xe_pt_destroy(xe_pt_entry_staging(pt_dir, i), flags,
214 deferred);
215 }
216 }
217 xe_pt_free(pt);
218 }
219
220 /**
221 * xe_pt_clear() - Clear a page-table.
222 * @xe: xe device.
223 * @pt: The page-table.
224 *
225 * Clears page-table by setting to zero.
226 */
xe_pt_clear(struct xe_device * xe,struct xe_pt * pt)227 void xe_pt_clear(struct xe_device *xe, struct xe_pt *pt)
228 {
229 struct iosys_map *map = &pt->bo->vmap;
230
231 xe_map_memset(xe, map, 0, 0, SZ_4K);
232 }
233
234 /**
235 * DOC: Pagetable building
236 *
237 * Below we use the term "page-table" for both page-directories, containing
238 * pointers to lower level page-directories or page-tables, and level 0
239 * page-tables that contain only page-table-entries pointing to memory pages.
240 *
241 * When inserting an address range in an already existing page-table tree
242 * there will typically be a set of page-tables that are shared with other
243 * address ranges, and a set that are private to this address range.
244 * The set of shared page-tables can be at most two per level,
245 * and those can't be updated immediately because the entries of those
246 * page-tables may still be in use by the gpu for other mappings. Therefore
247 * when inserting entries into those, we instead stage those insertions by
248 * adding insertion data into struct xe_vm_pgtable_update structures. This
249 * data, (subtrees for the cpu and page-table-entries for the gpu) is then
250 * added in a separate commit step. CPU-data is committed while still under the
251 * vm lock, the object lock and for userptr, the notifier lock in read mode.
252 * The GPU async data is committed either by the GPU or CPU after fulfilling
253 * relevant dependencies.
254 * For non-shared page-tables (and, in fact, for shared ones that aren't
255 * existing at the time of staging), we add the data in-place without the
256 * special update structures. This private part of the page-table tree will
257 * remain disconnected from the vm page-table tree until data is committed to
258 * the shared page tables of the vm tree in the commit phase.
259 */
260
261 struct xe_pt_update {
262 /** @update: The update structure we're building for this parent. */
263 struct xe_vm_pgtable_update *update;
264 /** @parent: The parent. Used to detect a parent change. */
265 struct xe_pt *parent;
266 /** @preexisting: Whether the parent was pre-existing or allocated */
267 bool preexisting;
268 };
269
270 struct xe_pt_stage_bind_walk {
271 /** base: The base class. */
272 struct xe_pt_walk base;
273
274 /* Input parameters for the walk */
275 /** @vm: The vm we're building for. */
276 struct xe_vm *vm;
277 /** @tile: The tile we're building for. */
278 struct xe_tile *tile;
279 /** @default_pte: PTE flag only template. No address is associated */
280 u64 default_pte;
281 /** @dma_offset: DMA offset to add to the PTE. */
282 u64 dma_offset;
283 /**
284 * @needs_64k: This address range enforces 64K alignment and
285 * granularity.
286 */
287 bool needs_64K;
288 /**
289 * @vma: VMA being mapped
290 */
291 struct xe_vma *vma;
292
293 /* Also input, but is updated during the walk*/
294 /** @curs: The DMA address cursor. */
295 struct xe_res_cursor *curs;
296 /** @va_curs_start: The Virtual address coresponding to @curs->start */
297 u64 va_curs_start;
298
299 /* Output */
300 struct xe_walk_update {
301 /** @wupd.entries: Caller provided storage. */
302 struct xe_vm_pgtable_update *entries;
303 /** @wupd.num_used_entries: Number of update @entries used. */
304 unsigned int num_used_entries;
305 /** @wupd.updates: Tracks the update entry at a given level */
306 struct xe_pt_update updates[XE_VM_MAX_LEVEL + 1];
307 } wupd;
308
309 /* Walk state */
310 /**
311 * @l0_end_addr: The end address of the current l0 leaf. Used for
312 * 64K granularity detection.
313 */
314 u64 l0_end_addr;
315 /** @addr_64K: The start address of the current 64K chunk. */
316 u64 addr_64K;
317 /** @found_64: Whether @add_64K actually points to a 64K chunk. */
318 bool found_64K;
319 };
320
321 static int
xe_pt_new_shared(struct xe_walk_update * wupd,struct xe_pt * parent,pgoff_t offset,bool alloc_entries)322 xe_pt_new_shared(struct xe_walk_update *wupd, struct xe_pt *parent,
323 pgoff_t offset, bool alloc_entries)
324 {
325 struct xe_pt_update *upd = &wupd->updates[parent->level];
326 struct xe_vm_pgtable_update *entry;
327
328 /*
329 * For *each level*, we could only have one active
330 * struct xt_pt_update at any one time. Once we move on to a
331 * new parent and page-directory, the old one is complete, and
332 * updates are either already stored in the build tree or in
333 * @wupd->entries
334 */
335 if (likely(upd->parent == parent))
336 return 0;
337
338 upd->parent = parent;
339 upd->preexisting = true;
340
341 if (wupd->num_used_entries == XE_VM_MAX_LEVEL * 2 + 1)
342 return -EINVAL;
343
344 entry = wupd->entries + wupd->num_used_entries++;
345 upd->update = entry;
346 entry->ofs = offset;
347 entry->pt_bo = parent->bo;
348 entry->pt = parent;
349 entry->flags = 0;
350 entry->qwords = 0;
351 entry->pt_bo->update_index = -1;
352
353 if (alloc_entries) {
354 entry->pt_entries = kmalloc_array(XE_PDES,
355 sizeof(*entry->pt_entries),
356 GFP_KERNEL);
357 if (!entry->pt_entries)
358 return -ENOMEM;
359 }
360
361 return 0;
362 }
363
364 /*
365 * NOTE: This is a very frequently called function so we allow ourselves
366 * to annotate (using branch prediction hints) the fastpath of updating a
367 * non-pre-existing pagetable with leaf ptes.
368 */
369 static int
xe_pt_insert_entry(struct xe_pt_stage_bind_walk * xe_walk,struct xe_pt * parent,pgoff_t offset,struct xe_pt * xe_child,u64 pte)370 xe_pt_insert_entry(struct xe_pt_stage_bind_walk *xe_walk, struct xe_pt *parent,
371 pgoff_t offset, struct xe_pt *xe_child, u64 pte)
372 {
373 struct xe_pt_update *upd = &xe_walk->wupd.updates[parent->level];
374 struct xe_pt_update *child_upd = xe_child ?
375 &xe_walk->wupd.updates[xe_child->level] : NULL;
376 int ret;
377
378 ret = xe_pt_new_shared(&xe_walk->wupd, parent, offset, true);
379 if (unlikely(ret))
380 return ret;
381
382 /*
383 * Register this new pagetable so that it won't be recognized as
384 * a shared pagetable by a subsequent insertion.
385 */
386 if (unlikely(child_upd)) {
387 child_upd->update = NULL;
388 child_upd->parent = xe_child;
389 child_upd->preexisting = false;
390 }
391
392 if (likely(!upd->preexisting)) {
393 /* Continue building a non-connected subtree. */
394 struct iosys_map *map = &parent->bo->vmap;
395
396 if (unlikely(xe_child)) {
397 parent->base.children[offset] = &xe_child->base;
398 parent->base.staging[offset] = &xe_child->base;
399 }
400
401 xe_pt_write(xe_walk->vm->xe, map, offset, pte);
402 parent->num_live++;
403 } else {
404 /* Shared pt. Stage update. */
405 unsigned int idx;
406 struct xe_vm_pgtable_update *entry = upd->update;
407
408 idx = offset - entry->ofs;
409 entry->pt_entries[idx].pt = xe_child;
410 entry->pt_entries[idx].pte = pte;
411 entry->qwords++;
412 }
413
414 return 0;
415 }
416
xe_pt_hugepte_possible(u64 addr,u64 next,unsigned int level,struct xe_pt_stage_bind_walk * xe_walk)417 static bool xe_pt_hugepte_possible(u64 addr, u64 next, unsigned int level,
418 struct xe_pt_stage_bind_walk *xe_walk)
419 {
420 u64 size, dma;
421
422 if (level > MAX_HUGEPTE_LEVEL)
423 return false;
424
425 /* Does the virtual range requested cover a huge pte? */
426 if (!xe_pt_covers(addr, next, level, &xe_walk->base))
427 return false;
428
429 /* Does the DMA segment cover the whole pte? */
430 if (next - xe_walk->va_curs_start > xe_walk->curs->size)
431 return false;
432
433 /* null VMA's do not have dma addresses */
434 if (xe_vma_is_null(xe_walk->vma))
435 return true;
436
437 /* Is the DMA address huge PTE size aligned? */
438 size = next - addr;
439 dma = addr - xe_walk->va_curs_start + xe_res_dma(xe_walk->curs);
440
441 return IS_ALIGNED(dma, size);
442 }
443
444 /*
445 * Scan the requested mapping to check whether it can be done entirely
446 * with 64K PTEs.
447 */
448 static bool
xe_pt_scan_64K(u64 addr,u64 next,struct xe_pt_stage_bind_walk * xe_walk)449 xe_pt_scan_64K(u64 addr, u64 next, struct xe_pt_stage_bind_walk *xe_walk)
450 {
451 struct xe_res_cursor curs = *xe_walk->curs;
452
453 if (!IS_ALIGNED(addr, SZ_64K))
454 return false;
455
456 if (next > xe_walk->l0_end_addr)
457 return false;
458
459 /* null VMA's do not have dma addresses */
460 if (xe_vma_is_null(xe_walk->vma))
461 return true;
462
463 xe_res_next(&curs, addr - xe_walk->va_curs_start);
464 for (; addr < next; addr += SZ_64K) {
465 if (!IS_ALIGNED(xe_res_dma(&curs), SZ_64K) || curs.size < SZ_64K)
466 return false;
467
468 xe_res_next(&curs, SZ_64K);
469 }
470
471 return addr == next;
472 }
473
474 /*
475 * For non-compact "normal" 4K level-0 pagetables, we want to try to group
476 * addresses together in 64K-contigous regions to add a 64K TLB hint for the
477 * device to the PTE.
478 * This function determines whether the address is part of such a
479 * segment. For VRAM in normal pagetables, this is strictly necessary on
480 * some devices.
481 */
482 static bool
xe_pt_is_pte_ps64K(u64 addr,u64 next,struct xe_pt_stage_bind_walk * xe_walk)483 xe_pt_is_pte_ps64K(u64 addr, u64 next, struct xe_pt_stage_bind_walk *xe_walk)
484 {
485 /* Address is within an already found 64k region */
486 if (xe_walk->found_64K && addr - xe_walk->addr_64K < SZ_64K)
487 return true;
488
489 xe_walk->found_64K = xe_pt_scan_64K(addr, addr + SZ_64K, xe_walk);
490 xe_walk->addr_64K = addr;
491
492 return xe_walk->found_64K;
493 }
494
495 static int
xe_pt_stage_bind_entry(struct xe_ptw * parent,pgoff_t offset,unsigned int level,u64 addr,u64 next,struct xe_ptw ** child,enum page_walk_action * action,struct xe_pt_walk * walk)496 xe_pt_stage_bind_entry(struct xe_ptw *parent, pgoff_t offset,
497 unsigned int level, u64 addr, u64 next,
498 struct xe_ptw **child,
499 enum page_walk_action *action,
500 struct xe_pt_walk *walk)
501 {
502 struct xe_pt_stage_bind_walk *xe_walk =
503 container_of(walk, typeof(*xe_walk), base);
504 u16 pat_index = xe_walk->vma->pat_index;
505 struct xe_pt *xe_parent = container_of(parent, typeof(*xe_parent), base);
506 struct xe_vm *vm = xe_walk->vm;
507 struct xe_pt *xe_child;
508 bool covers;
509 int ret = 0;
510 u64 pte;
511
512 /* Is this a leaf entry ?*/
513 if (level == 0 || xe_pt_hugepte_possible(addr, next, level, xe_walk)) {
514 struct xe_res_cursor *curs = xe_walk->curs;
515 bool is_null = xe_vma_is_null(xe_walk->vma);
516
517 XE_WARN_ON(xe_walk->va_curs_start != addr);
518
519 pte = vm->pt_ops->pte_encode_vma(is_null ? 0 :
520 xe_res_dma(curs) + xe_walk->dma_offset,
521 xe_walk->vma, pat_index, level);
522 pte |= xe_walk->default_pte;
523
524 /*
525 * Set the XE_PTE_PS64 hint if possible, otherwise if
526 * this device *requires* 64K PTE size for VRAM, fail.
527 */
528 if (level == 0 && !xe_parent->is_compact) {
529 if (xe_pt_is_pte_ps64K(addr, next, xe_walk)) {
530 xe_walk->vma->gpuva.flags |= XE_VMA_PTE_64K;
531 pte |= XE_PTE_PS64;
532 } else if (XE_WARN_ON(xe_walk->needs_64K)) {
533 return -EINVAL;
534 }
535 }
536
537 ret = xe_pt_insert_entry(xe_walk, xe_parent, offset, NULL, pte);
538 if (unlikely(ret))
539 return ret;
540
541 if (!is_null)
542 xe_res_next(curs, next - addr);
543 xe_walk->va_curs_start = next;
544 xe_walk->vma->gpuva.flags |= (XE_VMA_PTE_4K << level);
545 *action = ACTION_CONTINUE;
546
547 return ret;
548 }
549
550 /*
551 * Descending to lower level. Determine if we need to allocate a
552 * new page table or -directory, which we do if there is no
553 * previous one or there is one we can completely replace.
554 */
555 if (level == 1) {
556 walk->shifts = xe_normal_pt_shifts;
557 xe_walk->l0_end_addr = next;
558 }
559
560 covers = xe_pt_covers(addr, next, level, &xe_walk->base);
561 if (covers || !*child) {
562 u64 flags = 0;
563
564 xe_child = xe_pt_create(xe_walk->vm, xe_walk->tile, level - 1);
565 if (IS_ERR(xe_child))
566 return PTR_ERR(xe_child);
567
568 xe_pt_set_addr(xe_child,
569 round_down(addr, 1ull << walk->shifts[level]));
570
571 if (!covers)
572 xe_pt_populate_empty(xe_walk->tile, xe_walk->vm, xe_child);
573
574 *child = &xe_child->base;
575
576 /*
577 * Prefer the compact pagetable layout for L0 if possible. Only
578 * possible if VMA covers entire 2MB region as compact 64k and
579 * 4k pages cannot be mixed within a 2MB region.
580 * TODO: Suballocate the pt bo to avoid wasting a lot of
581 * memory.
582 */
583 if (GRAPHICS_VERx100(tile_to_xe(xe_walk->tile)) >= 1250 && level == 1 &&
584 covers && xe_pt_scan_64K(addr, next, xe_walk)) {
585 walk->shifts = xe_compact_pt_shifts;
586 xe_walk->vma->gpuva.flags |= XE_VMA_PTE_COMPACT;
587 flags |= XE_PDE_64K;
588 xe_child->is_compact = true;
589 }
590
591 pte = vm->pt_ops->pde_encode_bo(xe_child->bo, 0, pat_index) | flags;
592 ret = xe_pt_insert_entry(xe_walk, xe_parent, offset, xe_child,
593 pte);
594 }
595
596 *action = ACTION_SUBTREE;
597 return ret;
598 }
599
600 static const struct xe_pt_walk_ops xe_pt_stage_bind_ops = {
601 .pt_entry = xe_pt_stage_bind_entry,
602 };
603
604 /**
605 * xe_pt_stage_bind() - Build a disconnected page-table tree for a given address
606 * range.
607 * @tile: The tile we're building for.
608 * @vma: The vma indicating the address range.
609 * @entries: Storage for the update entries used for connecting the tree to
610 * the main tree at commit time.
611 * @num_entries: On output contains the number of @entries used.
612 *
613 * This function builds a disconnected page-table tree for a given address
614 * range. The tree is connected to the main vm tree for the gpu using
615 * xe_migrate_update_pgtables() and for the cpu using xe_pt_commit_bind().
616 * The function builds xe_vm_pgtable_update structures for already existing
617 * shared page-tables, and non-existing shared and non-shared page-tables
618 * are built and populated directly.
619 *
620 * Return 0 on success, negative error code on error.
621 */
622 static int
xe_pt_stage_bind(struct xe_tile * tile,struct xe_vma * vma,struct xe_vm_pgtable_update * entries,u32 * num_entries)623 xe_pt_stage_bind(struct xe_tile *tile, struct xe_vma *vma,
624 struct xe_vm_pgtable_update *entries, u32 *num_entries)
625 {
626 struct xe_device *xe = tile_to_xe(tile);
627 struct xe_bo *bo = xe_vma_bo(vma);
628 bool is_devmem = !xe_vma_is_userptr(vma) && bo &&
629 (xe_bo_is_vram(bo) || xe_bo_is_stolen_devmem(bo));
630 struct xe_res_cursor curs;
631 struct xe_pt_stage_bind_walk xe_walk = {
632 .base = {
633 .ops = &xe_pt_stage_bind_ops,
634 .shifts = xe_normal_pt_shifts,
635 .max_level = XE_PT_HIGHEST_LEVEL,
636 .staging = true,
637 },
638 .vm = xe_vma_vm(vma),
639 .tile = tile,
640 .curs = &curs,
641 .va_curs_start = xe_vma_start(vma),
642 .vma = vma,
643 .wupd.entries = entries,
644 .needs_64K = (xe_vma_vm(vma)->flags & XE_VM_FLAG_64K) && is_devmem,
645 };
646 struct xe_pt *pt = xe_vma_vm(vma)->pt_root[tile->id];
647 int ret;
648
649 /**
650 * Default atomic expectations for different allocation scenarios are as follows:
651 *
652 * 1. Traditional API: When the VM is not in LR mode:
653 * - Device atomics are expected to function with all allocations.
654 *
655 * 2. Compute/SVM API: When the VM is in LR mode:
656 * - Device atomics are the default behavior when the bo is placed in a single region.
657 * - In all other cases device atomics will be disabled with AE=0 until an application
658 * request differently using a ioctl like madvise.
659 */
660 if (vma->gpuva.flags & XE_VMA_ATOMIC_PTE_BIT) {
661 if (xe_vm_in_lr_mode(xe_vma_vm(vma))) {
662 if (bo && xe_bo_has_single_placement(bo))
663 xe_walk.default_pte |= XE_USM_PPGTT_PTE_AE;
664 /**
665 * If a SMEM+LMEM allocation is backed by SMEM, a device
666 * atomics will cause a gpu page fault and which then
667 * gets migrated to LMEM, bind such allocations with
668 * device atomics enabled.
669 */
670 else if (is_devmem && !xe_bo_has_single_placement(bo))
671 xe_walk.default_pte |= XE_USM_PPGTT_PTE_AE;
672 } else {
673 xe_walk.default_pte |= XE_USM_PPGTT_PTE_AE;
674 }
675
676 /**
677 * Unset AE if the platform(PVC) doesn't support it on an
678 * allocation
679 */
680 if (!xe->info.has_device_atomics_on_smem && !is_devmem)
681 xe_walk.default_pte &= ~XE_USM_PPGTT_PTE_AE;
682 }
683
684 if (is_devmem) {
685 xe_walk.default_pte |= XE_PPGTT_PTE_DM;
686 xe_walk.dma_offset = vram_region_gpu_offset(bo->ttm.resource);
687 }
688
689 if (!xe_vma_has_no_bo(vma) && xe_bo_is_stolen(bo))
690 xe_walk.dma_offset = xe_ttm_stolen_gpu_offset(xe_bo_device(bo));
691
692 xe_bo_assert_held(bo);
693
694 if (!xe_vma_is_null(vma)) {
695 if (xe_vma_is_userptr(vma))
696 xe_res_first_sg(to_userptr_vma(vma)->userptr.sg, 0,
697 xe_vma_size(vma), &curs);
698 else if (xe_bo_is_vram(bo) || xe_bo_is_stolen(bo))
699 xe_res_first(bo->ttm.resource, xe_vma_bo_offset(vma),
700 xe_vma_size(vma), &curs);
701 else
702 xe_res_first_sg(xe_bo_sg(bo), xe_vma_bo_offset(vma),
703 xe_vma_size(vma), &curs);
704 } else {
705 curs.size = xe_vma_size(vma);
706 }
707
708 ret = xe_pt_walk_range(&pt->base, pt->level, xe_vma_start(vma),
709 xe_vma_end(vma), &xe_walk.base);
710
711 *num_entries = xe_walk.wupd.num_used_entries;
712 return ret;
713 }
714
715 /**
716 * xe_pt_nonshared_offsets() - Determine the non-shared entry offsets of a
717 * shared pagetable.
718 * @addr: The start address within the non-shared pagetable.
719 * @end: The end address within the non-shared pagetable.
720 * @level: The level of the non-shared pagetable.
721 * @walk: Walk info. The function adjusts the walk action.
722 * @action: next action to perform (see enum page_walk_action)
723 * @offset: Ignored on input, First non-shared entry on output.
724 * @end_offset: Ignored on input, Last non-shared entry + 1 on output.
725 *
726 * A non-shared page-table has some entries that belong to the address range
727 * and others that don't. This function determines the entries that belong
728 * fully to the address range. Depending on level, some entries may
729 * partially belong to the address range (that can't happen at level 0).
730 * The function detects that and adjust those offsets to not include those
731 * partial entries. Iff it does detect partial entries, we know that there must
732 * be shared page tables also at lower levels, so it adjusts the walk action
733 * accordingly.
734 *
735 * Return: true if there were non-shared entries, false otherwise.
736 */
xe_pt_nonshared_offsets(u64 addr,u64 end,unsigned int level,struct xe_pt_walk * walk,enum page_walk_action * action,pgoff_t * offset,pgoff_t * end_offset)737 static bool xe_pt_nonshared_offsets(u64 addr, u64 end, unsigned int level,
738 struct xe_pt_walk *walk,
739 enum page_walk_action *action,
740 pgoff_t *offset, pgoff_t *end_offset)
741 {
742 u64 size = 1ull << walk->shifts[level];
743
744 *offset = xe_pt_offset(addr, level, walk);
745 *end_offset = xe_pt_num_entries(addr, end, level, walk) + *offset;
746
747 if (!level)
748 return true;
749
750 /*
751 * If addr or next are not size aligned, there are shared pts at lower
752 * level, so in that case traverse down the subtree
753 */
754 *action = ACTION_CONTINUE;
755 if (!IS_ALIGNED(addr, size)) {
756 *action = ACTION_SUBTREE;
757 (*offset)++;
758 }
759
760 if (!IS_ALIGNED(end, size)) {
761 *action = ACTION_SUBTREE;
762 (*end_offset)--;
763 }
764
765 return *end_offset > *offset;
766 }
767
768 struct xe_pt_zap_ptes_walk {
769 /** @base: The walk base-class */
770 struct xe_pt_walk base;
771
772 /* Input parameters for the walk */
773 /** @tile: The tile we're building for */
774 struct xe_tile *tile;
775
776 /* Output */
777 /** @needs_invalidate: Whether we need to invalidate TLB*/
778 bool needs_invalidate;
779 };
780
xe_pt_zap_ptes_entry(struct xe_ptw * parent,pgoff_t offset,unsigned int level,u64 addr,u64 next,struct xe_ptw ** child,enum page_walk_action * action,struct xe_pt_walk * walk)781 static int xe_pt_zap_ptes_entry(struct xe_ptw *parent, pgoff_t offset,
782 unsigned int level, u64 addr, u64 next,
783 struct xe_ptw **child,
784 enum page_walk_action *action,
785 struct xe_pt_walk *walk)
786 {
787 struct xe_pt_zap_ptes_walk *xe_walk =
788 container_of(walk, typeof(*xe_walk), base);
789 struct xe_pt *xe_child = container_of(*child, typeof(*xe_child), base);
790 pgoff_t end_offset;
791
792 XE_WARN_ON(!*child);
793 XE_WARN_ON(!level);
794
795 /*
796 * Note that we're called from an entry callback, and we're dealing
797 * with the child of that entry rather than the parent, so need to
798 * adjust level down.
799 */
800 if (xe_pt_nonshared_offsets(addr, next, --level, walk, action, &offset,
801 &end_offset)) {
802 xe_map_memset(tile_to_xe(xe_walk->tile), &xe_child->bo->vmap,
803 offset * sizeof(u64), 0,
804 (end_offset - offset) * sizeof(u64));
805 xe_walk->needs_invalidate = true;
806 }
807
808 return 0;
809 }
810
811 static const struct xe_pt_walk_ops xe_pt_zap_ptes_ops = {
812 .pt_entry = xe_pt_zap_ptes_entry,
813 };
814
815 /**
816 * xe_pt_zap_ptes() - Zap (zero) gpu ptes of an address range
817 * @tile: The tile we're zapping for.
818 * @vma: GPU VMA detailing address range.
819 *
820 * Eviction and Userptr invalidation needs to be able to zap the
821 * gpu ptes of a given address range in pagefaulting mode.
822 * In order to be able to do that, that function needs access to the shared
823 * page-table entrieaso it can either clear the leaf PTEs or
824 * clear the pointers to lower-level page-tables. The caller is required
825 * to hold the necessary locks to ensure neither the page-table connectivity
826 * nor the page-table entries of the range is updated from under us.
827 *
828 * Return: Whether ptes were actually updated and a TLB invalidation is
829 * required.
830 */
xe_pt_zap_ptes(struct xe_tile * tile,struct xe_vma * vma)831 bool xe_pt_zap_ptes(struct xe_tile *tile, struct xe_vma *vma)
832 {
833 struct xe_pt_zap_ptes_walk xe_walk = {
834 .base = {
835 .ops = &xe_pt_zap_ptes_ops,
836 .shifts = xe_normal_pt_shifts,
837 .max_level = XE_PT_HIGHEST_LEVEL,
838 },
839 .tile = tile,
840 };
841 struct xe_pt *pt = xe_vma_vm(vma)->pt_root[tile->id];
842 u8 pt_mask = (vma->tile_present & ~vma->tile_invalidated);
843
844 if (!(pt_mask & BIT(tile->id)))
845 return false;
846
847 (void)xe_pt_walk_shared(&pt->base, pt->level, xe_vma_start(vma),
848 xe_vma_end(vma), &xe_walk.base);
849
850 return xe_walk.needs_invalidate;
851 }
852
853 static void
xe_vm_populate_pgtable(struct xe_migrate_pt_update * pt_update,struct xe_tile * tile,struct iosys_map * map,void * data,u32 qword_ofs,u32 num_qwords,const struct xe_vm_pgtable_update * update)854 xe_vm_populate_pgtable(struct xe_migrate_pt_update *pt_update, struct xe_tile *tile,
855 struct iosys_map *map, void *data,
856 u32 qword_ofs, u32 num_qwords,
857 const struct xe_vm_pgtable_update *update)
858 {
859 struct xe_pt_entry *ptes = update->pt_entries;
860 u64 *ptr = data;
861 u32 i;
862
863 for (i = 0; i < num_qwords; i++) {
864 if (map)
865 xe_map_wr(tile_to_xe(tile), map, (qword_ofs + i) *
866 sizeof(u64), u64, ptes[i].pte);
867 else
868 ptr[i] = ptes[i].pte;
869 }
870 }
871
xe_pt_cancel_bind(struct xe_vma * vma,struct xe_vm_pgtable_update * entries,u32 num_entries)872 static void xe_pt_cancel_bind(struct xe_vma *vma,
873 struct xe_vm_pgtable_update *entries,
874 u32 num_entries)
875 {
876 u32 i, j;
877
878 for (i = 0; i < num_entries; i++) {
879 struct xe_pt *pt = entries[i].pt;
880
881 if (!pt)
882 continue;
883
884 if (pt->level) {
885 for (j = 0; j < entries[i].qwords; j++)
886 xe_pt_destroy(entries[i].pt_entries[j].pt,
887 xe_vma_vm(vma)->flags, NULL);
888 }
889
890 kfree(entries[i].pt_entries);
891 entries[i].pt_entries = NULL;
892 entries[i].qwords = 0;
893 }
894 }
895
xe_pt_commit_prepare_locks_assert(struct xe_vma * vma)896 static void xe_pt_commit_prepare_locks_assert(struct xe_vma *vma)
897 {
898 struct xe_vm *vm = xe_vma_vm(vma);
899
900 lockdep_assert_held(&vm->lock);
901
902 if (!xe_vma_is_userptr(vma) && !xe_vma_is_null(vma))
903 dma_resv_assert_held(xe_vma_bo(vma)->ttm.base.resv);
904
905 xe_vm_assert_held(vm);
906 }
907
xe_pt_commit_locks_assert(struct xe_vma * vma)908 static void xe_pt_commit_locks_assert(struct xe_vma *vma)
909 {
910 struct xe_vm *vm = xe_vma_vm(vma);
911
912 xe_pt_commit_prepare_locks_assert(vma);
913
914 if (xe_vma_is_userptr(vma))
915 lockdep_assert_held_read(&vm->userptr.notifier_lock);
916 }
917
xe_pt_commit(struct xe_vma * vma,struct xe_vm_pgtable_update * entries,u32 num_entries,struct llist_head * deferred)918 static void xe_pt_commit(struct xe_vma *vma,
919 struct xe_vm_pgtable_update *entries,
920 u32 num_entries, struct llist_head *deferred)
921 {
922 u32 i, j;
923
924 xe_pt_commit_locks_assert(vma);
925
926 for (i = 0; i < num_entries; i++) {
927 struct xe_pt *pt = entries[i].pt;
928 struct xe_pt_dir *pt_dir;
929
930 if (!pt->level)
931 continue;
932
933 pt_dir = as_xe_pt_dir(pt);
934 for (j = 0; j < entries[i].qwords; j++) {
935 struct xe_pt *oldpte = entries[i].pt_entries[j].pt;
936 int j_ = j + entries[i].ofs;
937
938 pt_dir->children[j_] = pt_dir->staging[j_];
939 xe_pt_destroy(oldpte, xe_vma_vm(vma)->flags, deferred);
940 }
941 }
942 }
943
xe_pt_abort_bind(struct xe_vma * vma,struct xe_vm_pgtable_update * entries,u32 num_entries,bool rebind)944 static void xe_pt_abort_bind(struct xe_vma *vma,
945 struct xe_vm_pgtable_update *entries,
946 u32 num_entries, bool rebind)
947 {
948 int i, j;
949
950 xe_pt_commit_prepare_locks_assert(vma);
951
952 for (i = num_entries - 1; i >= 0; --i) {
953 struct xe_pt *pt = entries[i].pt;
954 struct xe_pt_dir *pt_dir;
955
956 if (!rebind)
957 pt->num_live -= entries[i].qwords;
958
959 if (!pt->level)
960 continue;
961
962 pt_dir = as_xe_pt_dir(pt);
963 for (j = 0; j < entries[i].qwords; j++) {
964 u32 j_ = j + entries[i].ofs;
965 struct xe_pt *newpte = xe_pt_entry_staging(pt_dir, j_);
966 struct xe_pt *oldpte = entries[i].pt_entries[j].pt;
967
968 pt_dir->staging[j_] = oldpte ? &oldpte->base : 0;
969 xe_pt_destroy(newpte, xe_vma_vm(vma)->flags, NULL);
970 }
971 }
972 }
973
xe_pt_commit_prepare_bind(struct xe_vma * vma,struct xe_vm_pgtable_update * entries,u32 num_entries,bool rebind)974 static void xe_pt_commit_prepare_bind(struct xe_vma *vma,
975 struct xe_vm_pgtable_update *entries,
976 u32 num_entries, bool rebind)
977 {
978 u32 i, j;
979
980 xe_pt_commit_prepare_locks_assert(vma);
981
982 for (i = 0; i < num_entries; i++) {
983 struct xe_pt *pt = entries[i].pt;
984 struct xe_pt_dir *pt_dir;
985
986 if (!rebind)
987 pt->num_live += entries[i].qwords;
988
989 if (!pt->level)
990 continue;
991
992 pt_dir = as_xe_pt_dir(pt);
993 for (j = 0; j < entries[i].qwords; j++) {
994 u32 j_ = j + entries[i].ofs;
995 struct xe_pt *newpte = entries[i].pt_entries[j].pt;
996 struct xe_pt *oldpte = NULL;
997
998 if (xe_pt_entry_staging(pt_dir, j_))
999 oldpte = xe_pt_entry_staging(pt_dir, j_);
1000
1001 pt_dir->staging[j_] = &newpte->base;
1002 entries[i].pt_entries[j].pt = oldpte;
1003 }
1004 }
1005 }
1006
xe_pt_free_bind(struct xe_vm_pgtable_update * entries,u32 num_entries)1007 static void xe_pt_free_bind(struct xe_vm_pgtable_update *entries,
1008 u32 num_entries)
1009 {
1010 u32 i;
1011
1012 for (i = 0; i < num_entries; i++)
1013 kfree(entries[i].pt_entries);
1014 }
1015
1016 static int
xe_pt_prepare_bind(struct xe_tile * tile,struct xe_vma * vma,struct xe_vm_pgtable_update * entries,u32 * num_entries)1017 xe_pt_prepare_bind(struct xe_tile *tile, struct xe_vma *vma,
1018 struct xe_vm_pgtable_update *entries, u32 *num_entries)
1019 {
1020 int err;
1021
1022 *num_entries = 0;
1023 err = xe_pt_stage_bind(tile, vma, entries, num_entries);
1024 if (!err)
1025 xe_tile_assert(tile, *num_entries);
1026
1027 return err;
1028 }
1029
xe_vm_dbg_print_entries(struct xe_device * xe,const struct xe_vm_pgtable_update * entries,unsigned int num_entries,bool bind)1030 static void xe_vm_dbg_print_entries(struct xe_device *xe,
1031 const struct xe_vm_pgtable_update *entries,
1032 unsigned int num_entries, bool bind)
1033 #if (IS_ENABLED(CONFIG_DRM_XE_DEBUG_VM))
1034 {
1035 unsigned int i;
1036
1037 vm_dbg(&xe->drm, "%s: %u entries to update\n", bind ? "bind" : "unbind",
1038 num_entries);
1039 for (i = 0; i < num_entries; i++) {
1040 const struct xe_vm_pgtable_update *entry = &entries[i];
1041 struct xe_pt *xe_pt = entry->pt;
1042 u64 page_size = 1ull << xe_pt_shift(xe_pt->level);
1043 u64 end;
1044 u64 start;
1045
1046 xe_assert(xe, !entry->pt->is_compact);
1047 start = entry->ofs * page_size;
1048 end = start + page_size * entry->qwords;
1049 vm_dbg(&xe->drm,
1050 "\t%u: Update level %u at (%u + %u) [%llx...%llx) f:%x\n",
1051 i, xe_pt->level, entry->ofs, entry->qwords,
1052 xe_pt_addr(xe_pt) + start, xe_pt_addr(xe_pt) + end, 0);
1053 }
1054 }
1055 #else
1056 {}
1057 #endif
1058
no_in_syncs(struct xe_sync_entry * syncs,u32 num_syncs)1059 static bool no_in_syncs(struct xe_sync_entry *syncs, u32 num_syncs)
1060 {
1061 int i;
1062
1063 for (i = 0; i < num_syncs; i++) {
1064 struct dma_fence *fence = syncs[i].fence;
1065
1066 if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
1067 &fence->flags))
1068 return false;
1069 }
1070
1071 return true;
1072 }
1073
job_test_add_deps(struct xe_sched_job * job,struct dma_resv * resv,enum dma_resv_usage usage)1074 static int job_test_add_deps(struct xe_sched_job *job,
1075 struct dma_resv *resv,
1076 enum dma_resv_usage usage)
1077 {
1078 if (!job) {
1079 if (!dma_resv_test_signaled(resv, usage))
1080 return -ETIME;
1081
1082 return 0;
1083 }
1084
1085 return xe_sched_job_add_deps(job, resv, usage);
1086 }
1087
vma_add_deps(struct xe_vma * vma,struct xe_sched_job * job)1088 static int vma_add_deps(struct xe_vma *vma, struct xe_sched_job *job)
1089 {
1090 struct xe_bo *bo = xe_vma_bo(vma);
1091
1092 xe_bo_assert_held(bo);
1093
1094 if (bo && !bo->vm)
1095 return job_test_add_deps(job, bo->ttm.base.resv,
1096 DMA_RESV_USAGE_KERNEL);
1097
1098 return 0;
1099 }
1100
op_add_deps(struct xe_vm * vm,struct xe_vma_op * op,struct xe_sched_job * job)1101 static int op_add_deps(struct xe_vm *vm, struct xe_vma_op *op,
1102 struct xe_sched_job *job)
1103 {
1104 int err = 0;
1105
1106 switch (op->base.op) {
1107 case DRM_GPUVA_OP_MAP:
1108 if (!op->map.immediate && xe_vm_in_fault_mode(vm))
1109 break;
1110
1111 err = vma_add_deps(op->map.vma, job);
1112 break;
1113 case DRM_GPUVA_OP_REMAP:
1114 if (op->remap.prev)
1115 err = vma_add_deps(op->remap.prev, job);
1116 if (!err && op->remap.next)
1117 err = vma_add_deps(op->remap.next, job);
1118 break;
1119 case DRM_GPUVA_OP_UNMAP:
1120 break;
1121 case DRM_GPUVA_OP_PREFETCH:
1122 err = vma_add_deps(gpuva_to_vma(op->base.prefetch.va), job);
1123 break;
1124 default:
1125 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
1126 }
1127
1128 return err;
1129 }
1130
xe_pt_vm_dependencies(struct xe_sched_job * job,struct xe_vm * vm,struct xe_vma_ops * vops,struct xe_vm_pgtable_update_ops * pt_update_ops,struct xe_range_fence_tree * rftree)1131 static int xe_pt_vm_dependencies(struct xe_sched_job *job,
1132 struct xe_vm *vm,
1133 struct xe_vma_ops *vops,
1134 struct xe_vm_pgtable_update_ops *pt_update_ops,
1135 struct xe_range_fence_tree *rftree)
1136 {
1137 struct xe_range_fence *rtfence;
1138 struct dma_fence *fence;
1139 struct xe_vma_op *op;
1140 int err = 0, i;
1141
1142 xe_vm_assert_held(vm);
1143
1144 if (!job && !no_in_syncs(vops->syncs, vops->num_syncs))
1145 return -ETIME;
1146
1147 if (!job && !xe_exec_queue_is_idle(pt_update_ops->q))
1148 return -ETIME;
1149
1150 if (pt_update_ops->wait_vm_bookkeep || pt_update_ops->wait_vm_kernel) {
1151 err = job_test_add_deps(job, xe_vm_resv(vm),
1152 pt_update_ops->wait_vm_bookkeep ?
1153 DMA_RESV_USAGE_BOOKKEEP :
1154 DMA_RESV_USAGE_KERNEL);
1155 if (err)
1156 return err;
1157 }
1158
1159 rtfence = xe_range_fence_tree_first(rftree, pt_update_ops->start,
1160 pt_update_ops->last);
1161 while (rtfence) {
1162 fence = rtfence->fence;
1163
1164 if (!dma_fence_is_signaled(fence)) {
1165 /*
1166 * Is this a CPU update? GPU is busy updating, so return
1167 * an error
1168 */
1169 if (!job)
1170 return -ETIME;
1171
1172 dma_fence_get(fence);
1173 err = drm_sched_job_add_dependency(&job->drm, fence);
1174 if (err)
1175 return err;
1176 }
1177
1178 rtfence = xe_range_fence_tree_next(rtfence,
1179 pt_update_ops->start,
1180 pt_update_ops->last);
1181 }
1182
1183 list_for_each_entry(op, &vops->list, link) {
1184 err = op_add_deps(vm, op, job);
1185 if (err)
1186 return err;
1187 }
1188
1189 if (!(pt_update_ops->q->flags & EXEC_QUEUE_FLAG_KERNEL)) {
1190 if (job)
1191 err = xe_sched_job_last_fence_add_dep(job, vm);
1192 else
1193 err = xe_exec_queue_last_fence_test_dep(pt_update_ops->q, vm);
1194 }
1195
1196 for (i = 0; job && !err && i < vops->num_syncs; i++)
1197 err = xe_sync_entry_add_deps(&vops->syncs[i], job);
1198
1199 return err;
1200 }
1201
xe_pt_pre_commit(struct xe_migrate_pt_update * pt_update)1202 static int xe_pt_pre_commit(struct xe_migrate_pt_update *pt_update)
1203 {
1204 struct xe_vma_ops *vops = pt_update->vops;
1205 struct xe_vm *vm = vops->vm;
1206 struct xe_range_fence_tree *rftree = &vm->rftree[pt_update->tile_id];
1207 struct xe_vm_pgtable_update_ops *pt_update_ops =
1208 &vops->pt_update_ops[pt_update->tile_id];
1209
1210 return xe_pt_vm_dependencies(pt_update->job, vm, pt_update->vops,
1211 pt_update_ops, rftree);
1212 }
1213
1214 #ifdef CONFIG_DRM_XE_USERPTR_INVAL_INJECT
1215
xe_pt_userptr_inject_eagain(struct xe_userptr_vma * uvma)1216 static bool xe_pt_userptr_inject_eagain(struct xe_userptr_vma *uvma)
1217 {
1218 u32 divisor = uvma->userptr.divisor ? uvma->userptr.divisor : 2;
1219 static u32 count;
1220
1221 if (count++ % divisor == divisor - 1) {
1222 uvma->userptr.divisor = divisor << 1;
1223 return true;
1224 }
1225
1226 return false;
1227 }
1228
1229 #else
1230
xe_pt_userptr_inject_eagain(struct xe_userptr_vma * uvma)1231 static bool xe_pt_userptr_inject_eagain(struct xe_userptr_vma *uvma)
1232 {
1233 return false;
1234 }
1235
1236 #endif
1237
vma_check_userptr(struct xe_vm * vm,struct xe_vma * vma,struct xe_vm_pgtable_update_ops * pt_update)1238 static int vma_check_userptr(struct xe_vm *vm, struct xe_vma *vma,
1239 struct xe_vm_pgtable_update_ops *pt_update)
1240 {
1241 struct xe_userptr_vma *uvma;
1242 unsigned long notifier_seq;
1243
1244 lockdep_assert_held_read(&vm->userptr.notifier_lock);
1245
1246 if (!xe_vma_is_userptr(vma))
1247 return 0;
1248
1249 uvma = to_userptr_vma(vma);
1250 if (xe_pt_userptr_inject_eagain(uvma))
1251 xe_vma_userptr_force_invalidate(uvma);
1252
1253 notifier_seq = uvma->userptr.notifier_seq;
1254
1255 if (!mmu_interval_read_retry(&uvma->userptr.notifier,
1256 notifier_seq))
1257 return 0;
1258
1259 if (xe_vm_in_fault_mode(vm))
1260 return -EAGAIN;
1261
1262 /*
1263 * Just continue the operation since exec or rebind worker
1264 * will take care of rebinding.
1265 */
1266 return 0;
1267 }
1268
op_check_userptr(struct xe_vm * vm,struct xe_vma_op * op,struct xe_vm_pgtable_update_ops * pt_update)1269 static int op_check_userptr(struct xe_vm *vm, struct xe_vma_op *op,
1270 struct xe_vm_pgtable_update_ops *pt_update)
1271 {
1272 int err = 0;
1273
1274 lockdep_assert_held_read(&vm->userptr.notifier_lock);
1275
1276 switch (op->base.op) {
1277 case DRM_GPUVA_OP_MAP:
1278 if (!op->map.immediate && xe_vm_in_fault_mode(vm))
1279 break;
1280
1281 err = vma_check_userptr(vm, op->map.vma, pt_update);
1282 break;
1283 case DRM_GPUVA_OP_REMAP:
1284 if (op->remap.prev)
1285 err = vma_check_userptr(vm, op->remap.prev, pt_update);
1286 if (!err && op->remap.next)
1287 err = vma_check_userptr(vm, op->remap.next, pt_update);
1288 break;
1289 case DRM_GPUVA_OP_UNMAP:
1290 break;
1291 case DRM_GPUVA_OP_PREFETCH:
1292 err = vma_check_userptr(vm, gpuva_to_vma(op->base.prefetch.va),
1293 pt_update);
1294 break;
1295 default:
1296 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
1297 }
1298
1299 return err;
1300 }
1301
xe_pt_userptr_pre_commit(struct xe_migrate_pt_update * pt_update)1302 static int xe_pt_userptr_pre_commit(struct xe_migrate_pt_update *pt_update)
1303 {
1304 struct xe_vm *vm = pt_update->vops->vm;
1305 struct xe_vma_ops *vops = pt_update->vops;
1306 struct xe_vm_pgtable_update_ops *pt_update_ops =
1307 &vops->pt_update_ops[pt_update->tile_id];
1308 struct xe_vma_op *op;
1309 int err;
1310
1311 err = xe_pt_pre_commit(pt_update);
1312 if (err)
1313 return err;
1314
1315 down_read(&vm->userptr.notifier_lock);
1316
1317 list_for_each_entry(op, &vops->list, link) {
1318 err = op_check_userptr(vm, op, pt_update_ops);
1319 if (err) {
1320 up_read(&vm->userptr.notifier_lock);
1321 break;
1322 }
1323 }
1324
1325 return err;
1326 }
1327
1328 struct invalidation_fence {
1329 struct xe_gt_tlb_invalidation_fence base;
1330 struct xe_gt *gt;
1331 struct dma_fence *fence;
1332 struct dma_fence_cb cb;
1333 struct work_struct work;
1334 u64 start;
1335 u64 end;
1336 u32 asid;
1337 };
1338
invalidation_fence_cb(struct dma_fence * fence,struct dma_fence_cb * cb)1339 static void invalidation_fence_cb(struct dma_fence *fence,
1340 struct dma_fence_cb *cb)
1341 {
1342 struct invalidation_fence *ifence =
1343 container_of(cb, struct invalidation_fence, cb);
1344 struct xe_device *xe = gt_to_xe(ifence->gt);
1345
1346 trace_xe_gt_tlb_invalidation_fence_cb(xe, &ifence->base);
1347 if (!ifence->fence->error) {
1348 queue_work(system_wq, &ifence->work);
1349 } else {
1350 ifence->base.base.error = ifence->fence->error;
1351 xe_gt_tlb_invalidation_fence_signal(&ifence->base);
1352 }
1353 dma_fence_put(ifence->fence);
1354 }
1355
invalidation_fence_work_func(struct work_struct * w)1356 static void invalidation_fence_work_func(struct work_struct *w)
1357 {
1358 struct invalidation_fence *ifence =
1359 container_of(w, struct invalidation_fence, work);
1360 struct xe_device *xe = gt_to_xe(ifence->gt);
1361
1362 trace_xe_gt_tlb_invalidation_fence_work_func(xe, &ifence->base);
1363 xe_gt_tlb_invalidation_range(ifence->gt, &ifence->base, ifence->start,
1364 ifence->end, ifence->asid);
1365 }
1366
invalidation_fence_init(struct xe_gt * gt,struct invalidation_fence * ifence,struct dma_fence * fence,u64 start,u64 end,u32 asid)1367 static void invalidation_fence_init(struct xe_gt *gt,
1368 struct invalidation_fence *ifence,
1369 struct dma_fence *fence,
1370 u64 start, u64 end, u32 asid)
1371 {
1372 int ret;
1373
1374 trace_xe_gt_tlb_invalidation_fence_create(gt_to_xe(gt), &ifence->base);
1375
1376 xe_gt_tlb_invalidation_fence_init(gt, &ifence->base, false);
1377
1378 ifence->fence = fence;
1379 ifence->gt = gt;
1380 ifence->start = start;
1381 ifence->end = end;
1382 ifence->asid = asid;
1383
1384 INIT_WORK(&ifence->work, invalidation_fence_work_func);
1385 ret = dma_fence_add_callback(fence, &ifence->cb, invalidation_fence_cb);
1386 if (ret == -ENOENT) {
1387 dma_fence_put(ifence->fence); /* Usually dropped in CB */
1388 invalidation_fence_work_func(&ifence->work);
1389 } else if (ret) {
1390 dma_fence_put(&ifence->base.base); /* Caller ref */
1391 dma_fence_put(&ifence->base.base); /* Creation ref */
1392 }
1393
1394 xe_gt_assert(gt, !ret || ret == -ENOENT);
1395 }
1396
1397 struct xe_pt_stage_unbind_walk {
1398 /** @base: The pagewalk base-class. */
1399 struct xe_pt_walk base;
1400
1401 /* Input parameters for the walk */
1402 /** @tile: The tile we're unbinding from. */
1403 struct xe_tile *tile;
1404
1405 /**
1406 * @modified_start: Walk range start, modified to include any
1407 * shared pagetables that we're the only user of and can thus
1408 * treat as private.
1409 */
1410 u64 modified_start;
1411 /** @modified_end: Walk range start, modified like @modified_start. */
1412 u64 modified_end;
1413
1414 /* Output */
1415 /* @wupd: Structure to track the page-table updates we're building */
1416 struct xe_walk_update wupd;
1417 };
1418
1419 /*
1420 * Check whether this range is the only one populating this pagetable,
1421 * and in that case, update the walk range checks so that higher levels don't
1422 * view us as a shared pagetable.
1423 */
xe_pt_check_kill(u64 addr,u64 next,unsigned int level,const struct xe_pt * child,enum page_walk_action * action,struct xe_pt_walk * walk)1424 static bool xe_pt_check_kill(u64 addr, u64 next, unsigned int level,
1425 const struct xe_pt *child,
1426 enum page_walk_action *action,
1427 struct xe_pt_walk *walk)
1428 {
1429 struct xe_pt_stage_unbind_walk *xe_walk =
1430 container_of(walk, typeof(*xe_walk), base);
1431 unsigned int shift = walk->shifts[level];
1432 u64 size = 1ull << shift;
1433
1434 if (IS_ALIGNED(addr, size) && IS_ALIGNED(next, size) &&
1435 ((next - addr) >> shift) == child->num_live) {
1436 u64 size = 1ull << walk->shifts[level + 1];
1437
1438 *action = ACTION_CONTINUE;
1439
1440 if (xe_walk->modified_start >= addr)
1441 xe_walk->modified_start = round_down(addr, size);
1442 if (xe_walk->modified_end <= next)
1443 xe_walk->modified_end = round_up(next, size);
1444
1445 return true;
1446 }
1447
1448 return false;
1449 }
1450
xe_pt_stage_unbind_entry(struct xe_ptw * parent,pgoff_t offset,unsigned int level,u64 addr,u64 next,struct xe_ptw ** child,enum page_walk_action * action,struct xe_pt_walk * walk)1451 static int xe_pt_stage_unbind_entry(struct xe_ptw *parent, pgoff_t offset,
1452 unsigned int level, u64 addr, u64 next,
1453 struct xe_ptw **child,
1454 enum page_walk_action *action,
1455 struct xe_pt_walk *walk)
1456 {
1457 struct xe_pt *xe_child = container_of(*child, typeof(*xe_child), base);
1458
1459 XE_WARN_ON(!*child);
1460 XE_WARN_ON(!level);
1461
1462 xe_pt_check_kill(addr, next, level - 1, xe_child, action, walk);
1463
1464 return 0;
1465 }
1466
1467 static int
xe_pt_stage_unbind_post_descend(struct xe_ptw * parent,pgoff_t offset,unsigned int level,u64 addr,u64 next,struct xe_ptw ** child,enum page_walk_action * action,struct xe_pt_walk * walk)1468 xe_pt_stage_unbind_post_descend(struct xe_ptw *parent, pgoff_t offset,
1469 unsigned int level, u64 addr, u64 next,
1470 struct xe_ptw **child,
1471 enum page_walk_action *action,
1472 struct xe_pt_walk *walk)
1473 {
1474 struct xe_pt_stage_unbind_walk *xe_walk =
1475 container_of(walk, typeof(*xe_walk), base);
1476 struct xe_pt *xe_child = container_of(*child, typeof(*xe_child), base);
1477 pgoff_t end_offset;
1478 u64 size = 1ull << walk->shifts[--level];
1479 int err;
1480
1481 if (!IS_ALIGNED(addr, size))
1482 addr = xe_walk->modified_start;
1483 if (!IS_ALIGNED(next, size))
1484 next = xe_walk->modified_end;
1485
1486 /* Parent == *child is the root pt. Don't kill it. */
1487 if (parent != *child &&
1488 xe_pt_check_kill(addr, next, level, xe_child, action, walk))
1489 return 0;
1490
1491 if (!xe_pt_nonshared_offsets(addr, next, level, walk, action, &offset,
1492 &end_offset))
1493 return 0;
1494
1495 err = xe_pt_new_shared(&xe_walk->wupd, xe_child, offset, true);
1496 if (err)
1497 return err;
1498
1499 xe_walk->wupd.updates[level].update->qwords = end_offset - offset;
1500
1501 return 0;
1502 }
1503
1504 static const struct xe_pt_walk_ops xe_pt_stage_unbind_ops = {
1505 .pt_entry = xe_pt_stage_unbind_entry,
1506 .pt_post_descend = xe_pt_stage_unbind_post_descend,
1507 };
1508
1509 /**
1510 * xe_pt_stage_unbind() - Build page-table update structures for an unbind
1511 * operation
1512 * @tile: The tile we're unbinding for.
1513 * @vma: The vma we're unbinding.
1514 * @entries: Caller-provided storage for the update structures.
1515 *
1516 * Builds page-table update structures for an unbind operation. The function
1517 * will attempt to remove all page-tables that we're the only user
1518 * of, and for that to work, the unbind operation must be committed in the
1519 * same critical section that blocks racing binds to the same page-table tree.
1520 *
1521 * Return: The number of entries used.
1522 */
xe_pt_stage_unbind(struct xe_tile * tile,struct xe_vma * vma,struct xe_vm_pgtable_update * entries)1523 static unsigned int xe_pt_stage_unbind(struct xe_tile *tile, struct xe_vma *vma,
1524 struct xe_vm_pgtable_update *entries)
1525 {
1526 struct xe_pt_stage_unbind_walk xe_walk = {
1527 .base = {
1528 .ops = &xe_pt_stage_unbind_ops,
1529 .shifts = xe_normal_pt_shifts,
1530 .max_level = XE_PT_HIGHEST_LEVEL,
1531 .staging = true,
1532 },
1533 .tile = tile,
1534 .modified_start = xe_vma_start(vma),
1535 .modified_end = xe_vma_end(vma),
1536 .wupd.entries = entries,
1537 };
1538 struct xe_pt *pt = xe_vma_vm(vma)->pt_root[tile->id];
1539
1540 (void)xe_pt_walk_shared(&pt->base, pt->level, xe_vma_start(vma),
1541 xe_vma_end(vma), &xe_walk.base);
1542
1543 return xe_walk.wupd.num_used_entries;
1544 }
1545
1546 static void
xe_migrate_clear_pgtable_callback(struct xe_migrate_pt_update * pt_update,struct xe_tile * tile,struct iosys_map * map,void * ptr,u32 qword_ofs,u32 num_qwords,const struct xe_vm_pgtable_update * update)1547 xe_migrate_clear_pgtable_callback(struct xe_migrate_pt_update *pt_update,
1548 struct xe_tile *tile, struct iosys_map *map,
1549 void *ptr, u32 qword_ofs, u32 num_qwords,
1550 const struct xe_vm_pgtable_update *update)
1551 {
1552 struct xe_vm *vm = pt_update->vops->vm;
1553 u64 empty = __xe_pt_empty_pte(tile, vm, update->pt->level);
1554 int i;
1555
1556 if (map && map->is_iomem)
1557 for (i = 0; i < num_qwords; ++i)
1558 xe_map_wr(tile_to_xe(tile), map, (qword_ofs + i) *
1559 sizeof(u64), u64, empty);
1560 else if (map)
1561 memset64(map->vaddr + qword_ofs * sizeof(u64), empty,
1562 num_qwords);
1563 else
1564 memset64(ptr, empty, num_qwords);
1565 }
1566
xe_pt_abort_unbind(struct xe_vma * vma,struct xe_vm_pgtable_update * entries,u32 num_entries)1567 static void xe_pt_abort_unbind(struct xe_vma *vma,
1568 struct xe_vm_pgtable_update *entries,
1569 u32 num_entries)
1570 {
1571 int i, j;
1572
1573 xe_pt_commit_prepare_locks_assert(vma);
1574
1575 for (i = num_entries - 1; i >= 0; --i) {
1576 struct xe_vm_pgtable_update *entry = &entries[i];
1577 struct xe_pt *pt = entry->pt;
1578 struct xe_pt_dir *pt_dir = as_xe_pt_dir(pt);
1579
1580 pt->num_live += entry->qwords;
1581
1582 if (!pt->level)
1583 continue;
1584
1585 for (j = entry->ofs; j < entry->ofs + entry->qwords; j++)
1586 pt_dir->staging[j] =
1587 entries[i].pt_entries[j - entry->ofs].pt ?
1588 &entries[i].pt_entries[j - entry->ofs].pt->base : NULL;
1589 }
1590 }
1591
1592 static void
xe_pt_commit_prepare_unbind(struct xe_vma * vma,struct xe_vm_pgtable_update * entries,u32 num_entries)1593 xe_pt_commit_prepare_unbind(struct xe_vma *vma,
1594 struct xe_vm_pgtable_update *entries,
1595 u32 num_entries)
1596 {
1597 int i, j;
1598
1599 xe_pt_commit_prepare_locks_assert(vma);
1600
1601 for (i = 0; i < num_entries; ++i) {
1602 struct xe_vm_pgtable_update *entry = &entries[i];
1603 struct xe_pt *pt = entry->pt;
1604 struct xe_pt_dir *pt_dir;
1605
1606 pt->num_live -= entry->qwords;
1607 if (!pt->level)
1608 continue;
1609
1610 pt_dir = as_xe_pt_dir(pt);
1611 for (j = entry->ofs; j < entry->ofs + entry->qwords; j++) {
1612 entry->pt_entries[j - entry->ofs].pt =
1613 xe_pt_entry_staging(pt_dir, j);
1614 pt_dir->staging[j] = NULL;
1615 }
1616 }
1617 }
1618
1619 static void
xe_pt_update_ops_rfence_interval(struct xe_vm_pgtable_update_ops * pt_update_ops,struct xe_vma * vma)1620 xe_pt_update_ops_rfence_interval(struct xe_vm_pgtable_update_ops *pt_update_ops,
1621 struct xe_vma *vma)
1622 {
1623 u32 current_op = pt_update_ops->current_op;
1624 struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[current_op];
1625 int i, level = 0;
1626 u64 start, last;
1627
1628 for (i = 0; i < pt_op->num_entries; i++) {
1629 const struct xe_vm_pgtable_update *entry = &pt_op->entries[i];
1630
1631 if (entry->pt->level > level)
1632 level = entry->pt->level;
1633 }
1634
1635 /* Greedy (non-optimal) calculation but simple */
1636 start = ALIGN_DOWN(xe_vma_start(vma), 0x1ull << xe_pt_shift(level));
1637 last = ALIGN(xe_vma_end(vma), 0x1ull << xe_pt_shift(level)) - 1;
1638
1639 if (start < pt_update_ops->start)
1640 pt_update_ops->start = start;
1641 if (last > pt_update_ops->last)
1642 pt_update_ops->last = last;
1643 }
1644
vma_reserve_fences(struct xe_device * xe,struct xe_vma * vma)1645 static int vma_reserve_fences(struct xe_device *xe, struct xe_vma *vma)
1646 {
1647 int shift = xe_device_get_root_tile(xe)->media_gt ? 1 : 0;
1648
1649 if (!xe_vma_has_no_bo(vma) && !xe_vma_bo(vma)->vm)
1650 return dma_resv_reserve_fences(xe_vma_bo(vma)->ttm.base.resv,
1651 xe->info.tile_count << shift);
1652
1653 return 0;
1654 }
1655
bind_op_prepare(struct xe_vm * vm,struct xe_tile * tile,struct xe_vm_pgtable_update_ops * pt_update_ops,struct xe_vma * vma)1656 static int bind_op_prepare(struct xe_vm *vm, struct xe_tile *tile,
1657 struct xe_vm_pgtable_update_ops *pt_update_ops,
1658 struct xe_vma *vma)
1659 {
1660 u32 current_op = pt_update_ops->current_op;
1661 struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[current_op];
1662 int err;
1663
1664 xe_bo_assert_held(xe_vma_bo(vma));
1665
1666 vm_dbg(&xe_vma_vm(vma)->xe->drm,
1667 "Preparing bind, with range [%llx...%llx)\n",
1668 xe_vma_start(vma), xe_vma_end(vma) - 1);
1669
1670 pt_op->vma = NULL;
1671 pt_op->bind = true;
1672 pt_op->rebind = BIT(tile->id) & vma->tile_present;
1673
1674 err = vma_reserve_fences(tile_to_xe(tile), vma);
1675 if (err)
1676 return err;
1677
1678 err = xe_pt_prepare_bind(tile, vma, pt_op->entries,
1679 &pt_op->num_entries);
1680 if (!err) {
1681 xe_tile_assert(tile, pt_op->num_entries <=
1682 ARRAY_SIZE(pt_op->entries));
1683 xe_vm_dbg_print_entries(tile_to_xe(tile), pt_op->entries,
1684 pt_op->num_entries, true);
1685
1686 xe_pt_update_ops_rfence_interval(pt_update_ops, vma);
1687 ++pt_update_ops->current_op;
1688 pt_update_ops->needs_userptr_lock |= xe_vma_is_userptr(vma);
1689
1690 /*
1691 * If rebind, we have to invalidate TLB on !LR vms to invalidate
1692 * cached PTEs point to freed memory. On LR vms this is done
1693 * automatically when the context is re-enabled by the rebind worker,
1694 * or in fault mode it was invalidated on PTE zapping.
1695 *
1696 * If !rebind, and scratch enabled VMs, there is a chance the scratch
1697 * PTE is already cached in the TLB so it needs to be invalidated.
1698 * On !LR VMs this is done in the ring ops preceding a batch, but on
1699 * non-faulting LR, in particular on user-space batch buffer chaining,
1700 * it needs to be done here.
1701 */
1702 if ((!pt_op->rebind && xe_vm_has_scratch(vm) &&
1703 xe_vm_in_preempt_fence_mode(vm)))
1704 pt_update_ops->needs_invalidation = true;
1705 else if (pt_op->rebind && !xe_vm_in_lr_mode(vm))
1706 /* We bump also if batch_invalidate_tlb is true */
1707 vm->tlb_flush_seqno++;
1708
1709 vma->tile_staged |= BIT(tile->id);
1710 pt_op->vma = vma;
1711 xe_pt_commit_prepare_bind(vma, pt_op->entries,
1712 pt_op->num_entries, pt_op->rebind);
1713 } else {
1714 xe_pt_cancel_bind(vma, pt_op->entries, pt_op->num_entries);
1715 }
1716
1717 return err;
1718 }
1719
unbind_op_prepare(struct xe_tile * tile,struct xe_vm_pgtable_update_ops * pt_update_ops,struct xe_vma * vma)1720 static int unbind_op_prepare(struct xe_tile *tile,
1721 struct xe_vm_pgtable_update_ops *pt_update_ops,
1722 struct xe_vma *vma)
1723 {
1724 u32 current_op = pt_update_ops->current_op;
1725 struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[current_op];
1726 int err;
1727
1728 if (!((vma->tile_present | vma->tile_staged) & BIT(tile->id)))
1729 return 0;
1730
1731 xe_bo_assert_held(xe_vma_bo(vma));
1732
1733 vm_dbg(&xe_vma_vm(vma)->xe->drm,
1734 "Preparing unbind, with range [%llx...%llx)\n",
1735 xe_vma_start(vma), xe_vma_end(vma) - 1);
1736
1737 /*
1738 * Wait for invalidation to complete. Can corrupt internal page table
1739 * state if an invalidation is running while preparing an unbind.
1740 */
1741 if (xe_vma_is_userptr(vma) && xe_vm_in_fault_mode(xe_vma_vm(vma)))
1742 mmu_interval_read_begin(&to_userptr_vma(vma)->userptr.notifier);
1743
1744 pt_op->vma = vma;
1745 pt_op->bind = false;
1746 pt_op->rebind = false;
1747
1748 err = vma_reserve_fences(tile_to_xe(tile), vma);
1749 if (err)
1750 return err;
1751
1752 pt_op->num_entries = xe_pt_stage_unbind(tile, vma, pt_op->entries);
1753
1754 xe_vm_dbg_print_entries(tile_to_xe(tile), pt_op->entries,
1755 pt_op->num_entries, false);
1756 xe_pt_update_ops_rfence_interval(pt_update_ops, vma);
1757 ++pt_update_ops->current_op;
1758 pt_update_ops->needs_userptr_lock |= xe_vma_is_userptr(vma);
1759 pt_update_ops->needs_invalidation = true;
1760
1761 xe_pt_commit_prepare_unbind(vma, pt_op->entries, pt_op->num_entries);
1762
1763 return 0;
1764 }
1765
op_prepare(struct xe_vm * vm,struct xe_tile * tile,struct xe_vm_pgtable_update_ops * pt_update_ops,struct xe_vma_op * op)1766 static int op_prepare(struct xe_vm *vm,
1767 struct xe_tile *tile,
1768 struct xe_vm_pgtable_update_ops *pt_update_ops,
1769 struct xe_vma_op *op)
1770 {
1771 int err = 0;
1772
1773 xe_vm_assert_held(vm);
1774
1775 switch (op->base.op) {
1776 case DRM_GPUVA_OP_MAP:
1777 if (!op->map.immediate && xe_vm_in_fault_mode(vm))
1778 break;
1779
1780 err = bind_op_prepare(vm, tile, pt_update_ops, op->map.vma);
1781 pt_update_ops->wait_vm_kernel = true;
1782 break;
1783 case DRM_GPUVA_OP_REMAP:
1784 err = unbind_op_prepare(tile, pt_update_ops,
1785 gpuva_to_vma(op->base.remap.unmap->va));
1786
1787 if (!err && op->remap.prev) {
1788 err = bind_op_prepare(vm, tile, pt_update_ops,
1789 op->remap.prev);
1790 pt_update_ops->wait_vm_bookkeep = true;
1791 }
1792 if (!err && op->remap.next) {
1793 err = bind_op_prepare(vm, tile, pt_update_ops,
1794 op->remap.next);
1795 pt_update_ops->wait_vm_bookkeep = true;
1796 }
1797 break;
1798 case DRM_GPUVA_OP_UNMAP:
1799 err = unbind_op_prepare(tile, pt_update_ops,
1800 gpuva_to_vma(op->base.unmap.va));
1801 break;
1802 case DRM_GPUVA_OP_PREFETCH:
1803 err = bind_op_prepare(vm, tile, pt_update_ops,
1804 gpuva_to_vma(op->base.prefetch.va));
1805 pt_update_ops->wait_vm_kernel = true;
1806 break;
1807 default:
1808 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
1809 }
1810
1811 return err;
1812 }
1813
1814 static void
xe_pt_update_ops_init(struct xe_vm_pgtable_update_ops * pt_update_ops)1815 xe_pt_update_ops_init(struct xe_vm_pgtable_update_ops *pt_update_ops)
1816 {
1817 init_llist_head(&pt_update_ops->deferred);
1818 pt_update_ops->start = ~0x0ull;
1819 pt_update_ops->last = 0x0ull;
1820 }
1821
1822 /**
1823 * xe_pt_update_ops_prepare() - Prepare PT update operations
1824 * @tile: Tile of PT update operations
1825 * @vops: VMA operationa
1826 *
1827 * Prepare PT update operations which includes updating internal PT state,
1828 * allocate memory for page tables, populate page table being pruned in, and
1829 * create PT update operations for leaf insertion / removal.
1830 *
1831 * Return: 0 on success, negative error code on error.
1832 */
xe_pt_update_ops_prepare(struct xe_tile * tile,struct xe_vma_ops * vops)1833 int xe_pt_update_ops_prepare(struct xe_tile *tile, struct xe_vma_ops *vops)
1834 {
1835 struct xe_vm_pgtable_update_ops *pt_update_ops =
1836 &vops->pt_update_ops[tile->id];
1837 struct xe_vma_op *op;
1838 int shift = tile->media_gt ? 1 : 0;
1839 int err;
1840
1841 lockdep_assert_held(&vops->vm->lock);
1842 xe_vm_assert_held(vops->vm);
1843
1844 xe_pt_update_ops_init(pt_update_ops);
1845
1846 err = dma_resv_reserve_fences(xe_vm_resv(vops->vm),
1847 tile_to_xe(tile)->info.tile_count << shift);
1848 if (err)
1849 return err;
1850
1851 list_for_each_entry(op, &vops->list, link) {
1852 err = op_prepare(vops->vm, tile, pt_update_ops, op);
1853
1854 if (err)
1855 return err;
1856 }
1857
1858 xe_tile_assert(tile, pt_update_ops->current_op <=
1859 pt_update_ops->num_ops);
1860
1861 #ifdef TEST_VM_OPS_ERROR
1862 if (vops->inject_error &&
1863 vops->vm->xe->vm_inject_error_position == FORCE_OP_ERROR_PREPARE)
1864 return -ENOSPC;
1865 #endif
1866
1867 return 0;
1868 }
1869
bind_op_commit(struct xe_vm * vm,struct xe_tile * tile,struct xe_vm_pgtable_update_ops * pt_update_ops,struct xe_vma * vma,struct dma_fence * fence,struct dma_fence * fence2)1870 static void bind_op_commit(struct xe_vm *vm, struct xe_tile *tile,
1871 struct xe_vm_pgtable_update_ops *pt_update_ops,
1872 struct xe_vma *vma, struct dma_fence *fence,
1873 struct dma_fence *fence2)
1874 {
1875 if (!xe_vma_has_no_bo(vma) && !xe_vma_bo(vma)->vm) {
1876 dma_resv_add_fence(xe_vma_bo(vma)->ttm.base.resv, fence,
1877 pt_update_ops->wait_vm_bookkeep ?
1878 DMA_RESV_USAGE_KERNEL :
1879 DMA_RESV_USAGE_BOOKKEEP);
1880 if (fence2)
1881 dma_resv_add_fence(xe_vma_bo(vma)->ttm.base.resv, fence2,
1882 pt_update_ops->wait_vm_bookkeep ?
1883 DMA_RESV_USAGE_KERNEL :
1884 DMA_RESV_USAGE_BOOKKEEP);
1885 }
1886 vma->tile_present |= BIT(tile->id);
1887 vma->tile_staged &= ~BIT(tile->id);
1888 if (xe_vma_is_userptr(vma)) {
1889 lockdep_assert_held_read(&vm->userptr.notifier_lock);
1890 to_userptr_vma(vma)->userptr.initial_bind = true;
1891 }
1892
1893 /*
1894 * Kick rebind worker if this bind triggers preempt fences and not in
1895 * the rebind worker
1896 */
1897 if (pt_update_ops->wait_vm_bookkeep &&
1898 xe_vm_in_preempt_fence_mode(vm) &&
1899 !current->mm)
1900 xe_vm_queue_rebind_worker(vm);
1901 }
1902
unbind_op_commit(struct xe_vm * vm,struct xe_tile * tile,struct xe_vm_pgtable_update_ops * pt_update_ops,struct xe_vma * vma,struct dma_fence * fence,struct dma_fence * fence2)1903 static void unbind_op_commit(struct xe_vm *vm, struct xe_tile *tile,
1904 struct xe_vm_pgtable_update_ops *pt_update_ops,
1905 struct xe_vma *vma, struct dma_fence *fence,
1906 struct dma_fence *fence2)
1907 {
1908 if (!xe_vma_has_no_bo(vma) && !xe_vma_bo(vma)->vm) {
1909 dma_resv_add_fence(xe_vma_bo(vma)->ttm.base.resv, fence,
1910 pt_update_ops->wait_vm_bookkeep ?
1911 DMA_RESV_USAGE_KERNEL :
1912 DMA_RESV_USAGE_BOOKKEEP);
1913 if (fence2)
1914 dma_resv_add_fence(xe_vma_bo(vma)->ttm.base.resv, fence2,
1915 pt_update_ops->wait_vm_bookkeep ?
1916 DMA_RESV_USAGE_KERNEL :
1917 DMA_RESV_USAGE_BOOKKEEP);
1918 }
1919 vma->tile_present &= ~BIT(tile->id);
1920 if (!vma->tile_present) {
1921 list_del_init(&vma->combined_links.rebind);
1922 if (xe_vma_is_userptr(vma)) {
1923 lockdep_assert_held_read(&vm->userptr.notifier_lock);
1924
1925 spin_lock(&vm->userptr.invalidated_lock);
1926 list_del_init(&to_userptr_vma(vma)->userptr.invalidate_link);
1927 spin_unlock(&vm->userptr.invalidated_lock);
1928 }
1929 }
1930 }
1931
op_commit(struct xe_vm * vm,struct xe_tile * tile,struct xe_vm_pgtable_update_ops * pt_update_ops,struct xe_vma_op * op,struct dma_fence * fence,struct dma_fence * fence2)1932 static void op_commit(struct xe_vm *vm,
1933 struct xe_tile *tile,
1934 struct xe_vm_pgtable_update_ops *pt_update_ops,
1935 struct xe_vma_op *op, struct dma_fence *fence,
1936 struct dma_fence *fence2)
1937 {
1938 xe_vm_assert_held(vm);
1939
1940 switch (op->base.op) {
1941 case DRM_GPUVA_OP_MAP:
1942 if (!op->map.immediate && xe_vm_in_fault_mode(vm))
1943 break;
1944
1945 bind_op_commit(vm, tile, pt_update_ops, op->map.vma, fence,
1946 fence2);
1947 break;
1948 case DRM_GPUVA_OP_REMAP:
1949 unbind_op_commit(vm, tile, pt_update_ops,
1950 gpuva_to_vma(op->base.remap.unmap->va), fence,
1951 fence2);
1952
1953 if (op->remap.prev)
1954 bind_op_commit(vm, tile, pt_update_ops, op->remap.prev,
1955 fence, fence2);
1956 if (op->remap.next)
1957 bind_op_commit(vm, tile, pt_update_ops, op->remap.next,
1958 fence, fence2);
1959 break;
1960 case DRM_GPUVA_OP_UNMAP:
1961 unbind_op_commit(vm, tile, pt_update_ops,
1962 gpuva_to_vma(op->base.unmap.va), fence, fence2);
1963 break;
1964 case DRM_GPUVA_OP_PREFETCH:
1965 bind_op_commit(vm, tile, pt_update_ops,
1966 gpuva_to_vma(op->base.prefetch.va), fence, fence2);
1967 break;
1968 default:
1969 drm_warn(&vm->xe->drm, "NOT POSSIBLE");
1970 }
1971 }
1972
1973 static const struct xe_migrate_pt_update_ops migrate_ops = {
1974 .populate = xe_vm_populate_pgtable,
1975 .clear = xe_migrate_clear_pgtable_callback,
1976 .pre_commit = xe_pt_pre_commit,
1977 };
1978
1979 static const struct xe_migrate_pt_update_ops userptr_migrate_ops = {
1980 .populate = xe_vm_populate_pgtable,
1981 .clear = xe_migrate_clear_pgtable_callback,
1982 .pre_commit = xe_pt_userptr_pre_commit,
1983 };
1984
1985 /**
1986 * xe_pt_update_ops_run() - Run PT update operations
1987 * @tile: Tile of PT update operations
1988 * @vops: VMA operationa
1989 *
1990 * Run PT update operations which includes committing internal PT state changes,
1991 * creating job for PT update operations for leaf insertion / removal, and
1992 * installing job fence in various places.
1993 *
1994 * Return: fence on success, negative ERR_PTR on error.
1995 */
1996 struct dma_fence *
xe_pt_update_ops_run(struct xe_tile * tile,struct xe_vma_ops * vops)1997 xe_pt_update_ops_run(struct xe_tile *tile, struct xe_vma_ops *vops)
1998 {
1999 struct xe_vm *vm = vops->vm;
2000 struct xe_vm_pgtable_update_ops *pt_update_ops =
2001 &vops->pt_update_ops[tile->id];
2002 struct dma_fence *fence;
2003 struct invalidation_fence *ifence = NULL, *mfence = NULL;
2004 struct dma_fence **fences = NULL;
2005 struct dma_fence_array *cf = NULL;
2006 struct xe_range_fence *rfence;
2007 struct xe_vma_op *op;
2008 int err = 0, i;
2009 struct xe_migrate_pt_update update = {
2010 .ops = pt_update_ops->needs_userptr_lock ?
2011 &userptr_migrate_ops :
2012 &migrate_ops,
2013 .vops = vops,
2014 .tile_id = tile->id,
2015 };
2016
2017 lockdep_assert_held(&vm->lock);
2018 xe_vm_assert_held(vm);
2019
2020 if (!pt_update_ops->current_op) {
2021 xe_tile_assert(tile, xe_vm_in_fault_mode(vm));
2022
2023 return dma_fence_get_stub();
2024 }
2025
2026 #ifdef TEST_VM_OPS_ERROR
2027 if (vops->inject_error &&
2028 vm->xe->vm_inject_error_position == FORCE_OP_ERROR_RUN)
2029 return ERR_PTR(-ENOSPC);
2030 #endif
2031
2032 if (pt_update_ops->needs_invalidation) {
2033 ifence = kzalloc(sizeof(*ifence), GFP_KERNEL);
2034 if (!ifence) {
2035 err = -ENOMEM;
2036 goto kill_vm_tile1;
2037 }
2038 if (tile->media_gt) {
2039 mfence = kzalloc(sizeof(*ifence), GFP_KERNEL);
2040 if (!mfence) {
2041 err = -ENOMEM;
2042 goto free_ifence;
2043 }
2044 fences = kmalloc_array(2, sizeof(*fences), GFP_KERNEL);
2045 if (!fences) {
2046 err = -ENOMEM;
2047 goto free_ifence;
2048 }
2049 cf = dma_fence_array_alloc(2);
2050 if (!cf) {
2051 err = -ENOMEM;
2052 goto free_ifence;
2053 }
2054 }
2055 }
2056
2057 rfence = kzalloc(sizeof(*rfence), GFP_KERNEL);
2058 if (!rfence) {
2059 err = -ENOMEM;
2060 goto free_ifence;
2061 }
2062
2063 fence = xe_migrate_update_pgtables(tile->migrate, &update);
2064 if (IS_ERR(fence)) {
2065 err = PTR_ERR(fence);
2066 goto free_rfence;
2067 }
2068
2069 /* Point of no return - VM killed if failure after this */
2070 for (i = 0; i < pt_update_ops->current_op; ++i) {
2071 struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[i];
2072
2073 xe_pt_commit(pt_op->vma, pt_op->entries,
2074 pt_op->num_entries, &pt_update_ops->deferred);
2075 pt_op->vma = NULL; /* skip in xe_pt_update_ops_abort */
2076 }
2077
2078 if (xe_range_fence_insert(&vm->rftree[tile->id], rfence,
2079 &xe_range_fence_kfree_ops,
2080 pt_update_ops->start,
2081 pt_update_ops->last, fence))
2082 dma_fence_wait(fence, false);
2083
2084 /* tlb invalidation must be done before signaling rebind */
2085 if (ifence) {
2086 if (mfence)
2087 dma_fence_get(fence);
2088 invalidation_fence_init(tile->primary_gt, ifence, fence,
2089 pt_update_ops->start,
2090 pt_update_ops->last, vm->usm.asid);
2091 if (mfence) {
2092 invalidation_fence_init(tile->media_gt, mfence, fence,
2093 pt_update_ops->start,
2094 pt_update_ops->last, vm->usm.asid);
2095 fences[0] = &ifence->base.base;
2096 fences[1] = &mfence->base.base;
2097 dma_fence_array_init(cf, 2, fences,
2098 vm->composite_fence_ctx,
2099 vm->composite_fence_seqno++,
2100 false);
2101 fence = &cf->base;
2102 } else {
2103 fence = &ifence->base.base;
2104 }
2105 }
2106
2107 if (!mfence) {
2108 dma_resv_add_fence(xe_vm_resv(vm), fence,
2109 pt_update_ops->wait_vm_bookkeep ?
2110 DMA_RESV_USAGE_KERNEL :
2111 DMA_RESV_USAGE_BOOKKEEP);
2112
2113 list_for_each_entry(op, &vops->list, link)
2114 op_commit(vops->vm, tile, pt_update_ops, op, fence, NULL);
2115 } else {
2116 dma_resv_add_fence(xe_vm_resv(vm), &ifence->base.base,
2117 pt_update_ops->wait_vm_bookkeep ?
2118 DMA_RESV_USAGE_KERNEL :
2119 DMA_RESV_USAGE_BOOKKEEP);
2120
2121 dma_resv_add_fence(xe_vm_resv(vm), &mfence->base.base,
2122 pt_update_ops->wait_vm_bookkeep ?
2123 DMA_RESV_USAGE_KERNEL :
2124 DMA_RESV_USAGE_BOOKKEEP);
2125
2126 list_for_each_entry(op, &vops->list, link)
2127 op_commit(vops->vm, tile, pt_update_ops, op,
2128 &ifence->base.base, &mfence->base.base);
2129 }
2130
2131 if (pt_update_ops->needs_userptr_lock)
2132 up_read(&vm->userptr.notifier_lock);
2133
2134 return fence;
2135
2136 free_rfence:
2137 kfree(rfence);
2138 free_ifence:
2139 kfree(cf);
2140 kfree(fences);
2141 kfree(mfence);
2142 kfree(ifence);
2143 kill_vm_tile1:
2144 if (err != -EAGAIN && tile->id)
2145 xe_vm_kill(vops->vm, false);
2146
2147 return ERR_PTR(err);
2148 }
2149
2150 /**
2151 * xe_pt_update_ops_fini() - Finish PT update operations
2152 * @tile: Tile of PT update operations
2153 * @vops: VMA operations
2154 *
2155 * Finish PT update operations by committing to destroy page table memory
2156 */
xe_pt_update_ops_fini(struct xe_tile * tile,struct xe_vma_ops * vops)2157 void xe_pt_update_ops_fini(struct xe_tile *tile, struct xe_vma_ops *vops)
2158 {
2159 struct xe_vm_pgtable_update_ops *pt_update_ops =
2160 &vops->pt_update_ops[tile->id];
2161 int i;
2162
2163 lockdep_assert_held(&vops->vm->lock);
2164 xe_vm_assert_held(vops->vm);
2165
2166 for (i = 0; i < pt_update_ops->current_op; ++i) {
2167 struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[i];
2168
2169 xe_pt_free_bind(pt_op->entries, pt_op->num_entries);
2170 }
2171 xe_bo_put_commit(&vops->pt_update_ops[tile->id].deferred);
2172 }
2173
2174 /**
2175 * xe_pt_update_ops_abort() - Abort PT update operations
2176 * @tile: Tile of PT update operations
2177 * @vops: VMA operationa
2178 *
2179 * Abort PT update operations by unwinding internal PT state
2180 */
xe_pt_update_ops_abort(struct xe_tile * tile,struct xe_vma_ops * vops)2181 void xe_pt_update_ops_abort(struct xe_tile *tile, struct xe_vma_ops *vops)
2182 {
2183 struct xe_vm_pgtable_update_ops *pt_update_ops =
2184 &vops->pt_update_ops[tile->id];
2185 int i;
2186
2187 lockdep_assert_held(&vops->vm->lock);
2188 xe_vm_assert_held(vops->vm);
2189
2190 for (i = pt_update_ops->num_ops - 1; i >= 0; --i) {
2191 struct xe_vm_pgtable_update_op *pt_op =
2192 &pt_update_ops->ops[i];
2193
2194 if (!pt_op->vma || i >= pt_update_ops->current_op)
2195 continue;
2196
2197 if (pt_op->bind)
2198 xe_pt_abort_bind(pt_op->vma, pt_op->entries,
2199 pt_op->num_entries,
2200 pt_op->rebind);
2201 else
2202 xe_pt_abort_unbind(pt_op->vma, pt_op->entries,
2203 pt_op->num_entries);
2204 }
2205
2206 xe_pt_update_ops_fini(tile, vops);
2207 }
2208