1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2022 Intel Corporation 4 */ 5 6 #include "xe_pt.h" 7 8 #include "regs/xe_gtt_defs.h" 9 #include "xe_bo.h" 10 #include "xe_device.h" 11 #include "xe_drm_client.h" 12 #include "xe_exec_queue.h" 13 #include "xe_gt.h" 14 #include "xe_gt_stats.h" 15 #include "xe_migrate.h" 16 #include "xe_page_reclaim.h" 17 #include "xe_pat.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_svm.h" 23 #include "xe_sync.h" 24 #include "xe_tlb_inval_job.h" 25 #include "xe_trace.h" 26 #include "xe_ttm_stolen_mgr.h" 27 #include "xe_userptr.h" 28 #include "xe_vm.h" 29 30 struct xe_pt_dir { 31 struct xe_pt pt; 32 /** @children: Array of page-table child nodes */ 33 struct xe_ptw *children[XE_PDES]; 34 /** @staging: Array of page-table staging nodes */ 35 struct xe_ptw *staging[XE_PDES]; 36 }; 37 38 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_VM) 39 #define xe_pt_set_addr(__xe_pt, __addr) ((__xe_pt)->addr = (__addr)) 40 #define xe_pt_addr(__xe_pt) ((__xe_pt)->addr) 41 #else 42 #define xe_pt_set_addr(__xe_pt, __addr) 43 #define xe_pt_addr(__xe_pt) 0ull 44 #endif 45 46 static const u64 xe_normal_pt_shifts[] = {12, 21, 30, 39, 48}; 47 static const u64 xe_compact_pt_shifts[] = {16, 21, 30, 39, 48}; 48 49 #define XE_PT_HIGHEST_LEVEL (ARRAY_SIZE(xe_normal_pt_shifts) - 1) 50 51 static struct xe_pt_dir *as_xe_pt_dir(struct xe_pt *pt) 52 { 53 return container_of(pt, struct xe_pt_dir, pt); 54 } 55 56 static struct xe_pt * 57 xe_pt_entry_staging(struct xe_pt_dir *pt_dir, unsigned int index) 58 { 59 return container_of(pt_dir->staging[index], struct xe_pt, base); 60 } 61 62 static u64 __xe_pt_empty_pte(struct xe_tile *tile, struct xe_vm *vm, 63 unsigned int level) 64 { 65 struct xe_device *xe = tile_to_xe(tile); 66 u16 pat_index = xe_cache_pat_idx(xe, XE_CACHE_WB); 67 u8 id = tile->id; 68 69 if (!xe_vm_has_scratch(vm)) 70 return 0; 71 72 if (level > MAX_HUGEPTE_LEVEL) 73 return vm->pt_ops->pde_encode_bo(vm->scratch_pt[id][level - 1]->bo, 74 0); 75 76 return vm->pt_ops->pte_encode_addr(xe, 0, pat_index, level, IS_DGFX(xe), 0) | 77 XE_PTE_NULL; 78 } 79 80 static void xe_pt_free(struct xe_pt *pt) 81 { 82 if (pt->level) 83 kfree(as_xe_pt_dir(pt)); 84 else 85 kfree(pt); 86 } 87 88 /** 89 * xe_pt_create() - Create a page-table. 90 * @vm: The vm to create for. 91 * @tile: The tile to create for. 92 * @level: The page-table level. 93 * @exec: The drm_exec object used to lock the vm. 94 * 95 * Allocate and initialize a single struct xe_pt metadata structure. Also 96 * create the corresponding page-table bo, but don't initialize it. If the 97 * level is grater than zero, then it's assumed to be a directory page- 98 * table and the directory structure is also allocated and initialized to 99 * NULL pointers. 100 * 101 * Return: A valid struct xe_pt pointer on success, Pointer error code on 102 * error. 103 */ 104 struct xe_pt *xe_pt_create(struct xe_vm *vm, struct xe_tile *tile, 105 unsigned int level, struct drm_exec *exec) 106 { 107 struct xe_pt *pt; 108 struct xe_bo *bo; 109 u32 bo_flags; 110 int err; 111 112 if (level) { 113 struct xe_pt_dir *dir = kzalloc_obj(*dir); 114 115 pt = (dir) ? &dir->pt : NULL; 116 } else { 117 pt = kzalloc_obj(*pt); 118 } 119 if (!pt) 120 return ERR_PTR(-ENOMEM); 121 122 bo_flags = XE_BO_FLAG_VRAM_IF_DGFX(tile) | 123 XE_BO_FLAG_IGNORE_MIN_PAGE_SIZE | 124 XE_BO_FLAG_NO_RESV_EVICT | XE_BO_FLAG_PAGETABLE; 125 if (vm->xef) /* userspace */ 126 bo_flags |= XE_BO_FLAG_PINNED_LATE_RESTORE | XE_BO_FLAG_FORCE_USER_VRAM; 127 128 pt->level = level; 129 130 drm_WARN_ON(&vm->xe->drm, IS_ERR_OR_NULL(exec)); 131 bo = xe_bo_create_pin_map(vm->xe, tile, vm, SZ_4K, 132 ttm_bo_type_kernel, 133 bo_flags, exec); 134 if (IS_ERR(bo)) { 135 err = PTR_ERR(bo); 136 goto err_kfree; 137 } 138 pt->bo = bo; 139 pt->base.children = level ? as_xe_pt_dir(pt)->children : NULL; 140 pt->base.staging = level ? as_xe_pt_dir(pt)->staging : NULL; 141 142 if (vm->xef) 143 xe_drm_client_add_bo(vm->xef->client, pt->bo); 144 xe_tile_assert(tile, level <= XE_VM_MAX_LEVEL); 145 146 return pt; 147 148 err_kfree: 149 xe_pt_free(pt); 150 return ERR_PTR(err); 151 } 152 ALLOW_ERROR_INJECTION(xe_pt_create, ERRNO); 153 154 /** 155 * xe_pt_populate_empty() - Populate a page-table bo with scratch- or zero 156 * entries. 157 * @tile: The tile the scratch pagetable of which to use. 158 * @vm: The vm we populate for. 159 * @pt: The pagetable the bo of which to initialize. 160 * 161 * Populate the page-table bo of @pt with entries pointing into the tile's 162 * scratch page-table tree if any. Otherwise populate with zeros. 163 */ 164 void xe_pt_populate_empty(struct xe_tile *tile, struct xe_vm *vm, 165 struct xe_pt *pt) 166 { 167 struct iosys_map *map = &pt->bo->vmap; 168 u64 empty; 169 int i; 170 171 if (!xe_vm_has_scratch(vm)) { 172 /* 173 * FIXME: Some memory is allocated already allocated to zero? 174 * Find out which memory that is and avoid this memset... 175 */ 176 xe_map_memset(vm->xe, map, 0, 0, SZ_4K); 177 } else { 178 empty = __xe_pt_empty_pte(tile, vm, pt->level); 179 for (i = 0; i < XE_PDES; i++) 180 xe_pt_write(vm->xe, map, i, empty); 181 } 182 } 183 184 /** 185 * xe_pt_shift() - Return the ilog2 value of the size of the address range of 186 * a page-table at a certain level. 187 * @level: The level. 188 * 189 * Return: The ilog2 value of the size of the address range of a page-table 190 * at level @level. 191 */ 192 unsigned int xe_pt_shift(unsigned int level) 193 { 194 return XE_PTE_SHIFT + XE_PDE_SHIFT * level; 195 } 196 197 /** 198 * xe_pt_destroy() - Destroy a page-table tree. 199 * @pt: The root of the page-table tree to destroy. 200 * @flags: vm flags. Currently unused. 201 * @deferred: List head of lockless list for deferred putting. NULL for 202 * immediate putting. 203 * 204 * Puts the page-table bo, recursively calls xe_pt_destroy on all children 205 * and finally frees @pt. TODO: Can we remove the @flags argument? 206 */ 207 void xe_pt_destroy(struct xe_pt *pt, u32 flags, struct llist_head *deferred) 208 { 209 int i; 210 211 if (!pt) 212 return; 213 214 XE_WARN_ON(!list_empty(&pt->bo->ttm.base.gpuva.list)); 215 xe_bo_unpin(pt->bo); 216 xe_bo_put_deferred(pt->bo, deferred); 217 218 if (pt->level > 0 && pt->num_live) { 219 struct xe_pt_dir *pt_dir = as_xe_pt_dir(pt); 220 221 for (i = 0; i < XE_PDES; i++) { 222 if (xe_pt_entry_staging(pt_dir, i)) 223 xe_pt_destroy(xe_pt_entry_staging(pt_dir, i), flags, 224 deferred); 225 } 226 } 227 xe_pt_free(pt); 228 } 229 230 /** 231 * xe_pt_clear() - Clear a page-table. 232 * @xe: xe device. 233 * @pt: The page-table. 234 * 235 * Clears page-table by setting to zero. 236 */ 237 void xe_pt_clear(struct xe_device *xe, struct xe_pt *pt) 238 { 239 struct xe_bo *bo = pt->bo; 240 241 xe_bo_assert_held(bo); 242 if (!iosys_map_is_null(&bo->vmap)) 243 xe_map_memset(xe, &bo->vmap, 0, 0, SZ_4K); 244 } 245 246 /** 247 * DOC: Pagetable building 248 * 249 * Below we use the term "page-table" for both page-directories, containing 250 * pointers to lower level page-directories or page-tables, and level 0 251 * page-tables that contain only page-table-entries pointing to memory pages. 252 * 253 * When inserting an address range in an already existing page-table tree 254 * there will typically be a set of page-tables that are shared with other 255 * address ranges, and a set that are private to this address range. 256 * The set of shared page-tables can be at most two per level, 257 * and those can't be updated immediately because the entries of those 258 * page-tables may still be in use by the gpu for other mappings. Therefore 259 * when inserting entries into those, we instead stage those insertions by 260 * adding insertion data into struct xe_vm_pgtable_update structures. This 261 * data, (subtrees for the cpu and page-table-entries for the gpu) is then 262 * added in a separate commit step. CPU-data is committed while still under the 263 * vm lock, the object lock and for userptr, the notifier lock in read mode. 264 * The GPU async data is committed either by the GPU or CPU after fulfilling 265 * relevant dependencies. 266 * For non-shared page-tables (and, in fact, for shared ones that aren't 267 * existing at the time of staging), we add the data in-place without the 268 * special update structures. This private part of the page-table tree will 269 * remain disconnected from the vm page-table tree until data is committed to 270 * the shared page tables of the vm tree in the commit phase. 271 */ 272 273 struct xe_pt_update { 274 /** @update: The update structure we're building for this parent. */ 275 struct xe_vm_pgtable_update *update; 276 /** @parent: The parent. Used to detect a parent change. */ 277 struct xe_pt *parent; 278 /** @preexisting: Whether the parent was pre-existing or allocated */ 279 bool preexisting; 280 }; 281 282 /** 283 * struct xe_pt_stage_bind_walk - Walk state for the stage_bind walk. 284 */ 285 struct xe_pt_stage_bind_walk { 286 /** @base: The base class. */ 287 struct xe_pt_walk base; 288 289 /* Input parameters for the walk */ 290 /** @vm: The vm we're building for. */ 291 struct xe_vm *vm; 292 /** @tile: The tile we're building for. */ 293 struct xe_tile *tile; 294 /** @default_vram_pte: PTE flag only template for VRAM. No address is associated */ 295 u64 default_vram_pte; 296 /** @default_system_pte: PTE flag only template for System. No address is associated */ 297 u64 default_system_pte; 298 /** @dma_offset: DMA offset to add to the PTE. */ 299 u64 dma_offset; 300 /** 301 * @needs_64K: This address range enforces 64K alignment and 302 * granularity on VRAM. 303 */ 304 bool needs_64K; 305 /** @clear_pt: clear page table entries during the bind walk */ 306 bool clear_pt; 307 /** 308 * @target_leaf_level: Page-table level at which to emit leaf PTEs 309 * 0 for normal 4K/64K mappings, 1 for 2M huge pages, and 2 for 1G huge 310 * pages. The walk still traverses from the root down; this field tells 311 * xe_pt_stage_bind_entry() to treat the selected level as a leaf instead 312 * of descending further. 313 */ 314 u32 target_leaf_level; 315 /** 316 * @vma: VMA being mapped 317 */ 318 struct xe_vma *vma; 319 320 /* Also input, but is updated during the walk*/ 321 /** @curs: The DMA address cursor. */ 322 struct xe_res_cursor *curs; 323 /** @va_curs_start: The Virtual address corresponding to @curs->start */ 324 u64 va_curs_start; 325 326 /* Output */ 327 /** @wupd: Walk output data for page-table updates. */ 328 struct xe_walk_update { 329 /** @wupd.entries: Caller provided storage. */ 330 struct xe_vm_pgtable_update *entries; 331 /** @wupd.num_used_entries: Number of update @entries used. */ 332 unsigned int num_used_entries; 333 /** @wupd.updates: Tracks the update entry at a given level */ 334 struct xe_pt_update updates[XE_VM_MAX_LEVEL + 1]; 335 } wupd; 336 337 /* Walk state */ 338 /** 339 * @l0_end_addr: The end address of the current l0 leaf. Used for 340 * 64K granularity detection. 341 */ 342 u64 l0_end_addr; 343 /** @addr_64K: The start address of the current 64K chunk. */ 344 u64 addr_64K; 345 /** @found_64K: Whether @add_64K actually points to a 64K chunk. */ 346 bool found_64K; 347 }; 348 349 static int 350 xe_pt_new_shared(struct xe_walk_update *wupd, struct xe_pt *parent, 351 pgoff_t offset, bool alloc_entries) 352 { 353 struct xe_pt_update *upd = &wupd->updates[parent->level]; 354 struct xe_vm_pgtable_update *entry; 355 356 /* 357 * For *each level*, we could only have one active 358 * struct xt_pt_update at any one time. Once we move on to a 359 * new parent and page-directory, the old one is complete, and 360 * updates are either already stored in the build tree or in 361 * @wupd->entries 362 */ 363 if (likely(upd->parent == parent)) 364 return 0; 365 366 upd->parent = parent; 367 upd->preexisting = true; 368 369 if (wupd->num_used_entries == XE_VM_MAX_LEVEL * 2 + 1) 370 return -EINVAL; 371 372 entry = wupd->entries + wupd->num_used_entries++; 373 upd->update = entry; 374 entry->ofs = offset; 375 entry->pt_bo = parent->bo; 376 entry->pt = parent; 377 entry->flags = 0; 378 entry->qwords = 0; 379 entry->pt_bo->update_index = -1; 380 381 if (alloc_entries) { 382 entry->pt_entries = kmalloc_objs(*entry->pt_entries, XE_PDES); 383 if (!entry->pt_entries) 384 return -ENOMEM; 385 } 386 387 return 0; 388 } 389 390 /* 391 * NOTE: This is a very frequently called function so we allow ourselves 392 * to annotate (using branch prediction hints) the fastpath of updating a 393 * non-pre-existing pagetable with leaf ptes. 394 */ 395 static int 396 xe_pt_insert_entry(struct xe_pt_stage_bind_walk *xe_walk, struct xe_pt *parent, 397 pgoff_t offset, struct xe_pt *xe_child, u64 pte) 398 { 399 struct xe_pt_update *upd = &xe_walk->wupd.updates[parent->level]; 400 struct xe_pt_update *child_upd = xe_child ? 401 &xe_walk->wupd.updates[xe_child->level] : NULL; 402 int ret; 403 404 ret = xe_pt_new_shared(&xe_walk->wupd, parent, offset, true); 405 if (unlikely(ret)) 406 return ret; 407 408 /* 409 * Register this new pagetable so that it won't be recognized as 410 * a shared pagetable by a subsequent insertion. 411 */ 412 if (unlikely(child_upd)) { 413 child_upd->update = NULL; 414 child_upd->parent = xe_child; 415 child_upd->preexisting = false; 416 } 417 418 if (likely(!upd->preexisting)) { 419 /* Continue building a non-connected subtree. */ 420 struct iosys_map *map = &parent->bo->vmap; 421 422 if (unlikely(xe_child)) { 423 parent->base.children[offset] = &xe_child->base; 424 parent->base.staging[offset] = &xe_child->base; 425 } 426 427 xe_pt_write(xe_walk->vm->xe, map, offset, pte); 428 parent->num_live++; 429 } else { 430 /* Shared pt. Stage update. */ 431 unsigned int idx; 432 struct xe_vm_pgtable_update *entry = upd->update; 433 434 idx = offset - entry->ofs; 435 entry->pt_entries[idx].pt = xe_child; 436 entry->pt_entries[idx].pte = pte; 437 entry->qwords++; 438 } 439 440 return 0; 441 } 442 443 static bool xe_pt_hugepte_possible(u64 addr, u64 next, unsigned int level, 444 struct xe_pt_stage_bind_walk *xe_walk) 445 { 446 struct xe_bo *bo = xe_vma_bo(xe_walk->vma); 447 u64 size, dma; 448 449 if (level > MAX_HUGEPTE_LEVEL) 450 return false; 451 452 /* Does the virtual range requested cover a huge pte? */ 453 if (!xe_pt_covers(addr, next, level, &xe_walk->base)) 454 return false; 455 456 /* null VMA's and purged BO's do not have dma addresses */ 457 if (xe_vma_is_null(xe_walk->vma) || (bo && xe_bo_is_purged(bo))) 458 return true; 459 460 /* if we are clearing page table, no dma addresses*/ 461 if (xe_walk->clear_pt) 462 return true; 463 464 /* Does the DMA segment cover the whole pte? */ 465 if (next - xe_walk->va_curs_start > xe_walk->curs->size) 466 return false; 467 468 /* Is the DMA address huge PTE size aligned? */ 469 size = next - addr; 470 dma = addr - xe_walk->va_curs_start + xe_res_dma(xe_walk->curs); 471 472 return IS_ALIGNED(dma, size); 473 } 474 475 /* 476 * Scan the requested mapping to check whether it can be done entirely 477 * with 64K PTEs. 478 */ 479 static bool 480 xe_pt_scan_64K(u64 addr, u64 next, struct xe_pt_stage_bind_walk *xe_walk) 481 { 482 struct xe_bo *bo = xe_vma_bo(xe_walk->vma); 483 struct xe_res_cursor curs = *xe_walk->curs; 484 485 if (!IS_ALIGNED(addr, SZ_64K)) 486 return false; 487 488 if (next > xe_walk->l0_end_addr) 489 return false; 490 491 /* null VMA's and purged BO's do not have dma addresses */ 492 if (xe_vma_is_null(xe_walk->vma) || (bo && xe_bo_is_purged(bo))) 493 return true; 494 495 xe_res_next(&curs, addr - xe_walk->va_curs_start); 496 for (; addr < next; addr += SZ_64K) { 497 if (!IS_ALIGNED(xe_res_dma(&curs), SZ_64K) || curs.size < SZ_64K) 498 return false; 499 500 xe_res_next(&curs, SZ_64K); 501 } 502 503 return addr == next; 504 } 505 506 /* 507 * For non-compact "normal" 4K level-0 pagetables, we want to try to group 508 * addresses together in 64K-contigous regions to add a 64K TLB hint for the 509 * device to the PTE. 510 * This function determines whether the address is part of such a 511 * segment. For VRAM in normal pagetables, this is strictly necessary on 512 * some devices. 513 */ 514 static bool 515 xe_pt_is_pte_ps64K(u64 addr, u64 next, struct xe_pt_stage_bind_walk *xe_walk) 516 { 517 /* Address is within an already found 64k region */ 518 if (xe_walk->found_64K && addr - xe_walk->addr_64K < SZ_64K) 519 return true; 520 521 xe_walk->found_64K = xe_pt_scan_64K(addr, addr + SZ_64K, xe_walk); 522 xe_walk->addr_64K = addr; 523 524 return xe_walk->found_64K; 525 } 526 527 static bool xe_pt_huge_leaf_allowed(u64 addr, u64 next, unsigned int level, 528 struct xe_pt_stage_bind_walk *xe_walk) 529 { 530 if (xe_walk->clear_pt) 531 return xe_pt_hugepte_possible(addr, next, level, xe_walk); 532 533 if (!xe_debug_page_size_supported(xe_walk->vm->xe)) 534 return xe_pt_hugepte_possible(addr, next, level, xe_walk); 535 536 if (!xe_walk->target_leaf_level) 537 return xe_pt_hugepte_possible(addr, next, level, xe_walk); 538 539 if (level == xe_walk->target_leaf_level) 540 return xe_pt_hugepte_possible(addr, next, level, xe_walk); 541 542 return false; 543 } 544 545 static bool xe_pt_exact_leaf_required_but_invalid(u64 addr, u64 next, 546 unsigned int level, 547 struct xe_pt_stage_bind_walk *xe_walk) 548 { 549 struct xe_device *xe = xe_walk->vm->xe; 550 551 if (!xe_debug_page_size_mode_not_none(xe)) 552 return false; 553 554 return !xe_walk->clear_pt && 555 xe_walk->target_leaf_level && 556 level == xe_walk->target_leaf_level && 557 !xe_pt_hugepte_possible(addr, next, level, xe_walk); 558 } 559 560 static int 561 xe_pt_stage_bind_entry(struct xe_ptw *parent, pgoff_t offset, 562 unsigned int level, u64 addr, u64 next, 563 struct xe_ptw **child, 564 enum page_walk_action *action, 565 struct xe_pt_walk *walk) 566 { 567 struct xe_pt_stage_bind_walk *xe_walk = 568 container_of(walk, typeof(*xe_walk), base); 569 u16 pat_index = xe_walk->vma->attr.pat_index; 570 struct xe_pt *xe_parent = container_of(parent, typeof(*xe_parent), base); 571 struct xe_vm *vm = xe_walk->vm; 572 struct xe_pt *xe_child; 573 bool covers; 574 int ret = 0; 575 u64 pte; 576 577 if (xe_pt_exact_leaf_required_but_invalid(addr, next, level, xe_walk)) 578 return -EINVAL; 579 580 /* 581 * Is this a leaf entry? 582 * Always create a 4K leaf at level 0. For huge pages (level > 0), 583 * validate alignment and size with xe_pt_hugepte_possible(). 584 * When target_leaf_level is non-zero, only that huge-page level is 585 * accepted for normal bind walks. Clear walks remain unconstrained so 586 * existing huge leaves can be cleared without descending further. 587 */ 588 if (level == 0 || xe_pt_huge_leaf_allowed(addr, next, level, xe_walk)) { 589 struct xe_res_cursor *curs = xe_walk->curs; 590 struct xe_bo *bo = xe_vma_bo(xe_walk->vma); 591 bool is_null_or_purged = xe_vma_is_null(xe_walk->vma) || 592 (bo && xe_bo_is_purged(bo)); 593 bool is_vram = is_null_or_purged ? false : xe_res_is_vram(curs); 594 595 XE_WARN_ON(xe_walk->va_curs_start != addr); 596 597 if (xe_walk->clear_pt) { 598 pte = 0; 599 } else { 600 /* 601 * For purged BOs, treat like null VMAs - pass address 0. 602 * The pte_encode_vma will set XE_PTE_NULL flag for scratch mapping. 603 */ 604 pte = vm->pt_ops->pte_encode_vma(is_null_or_purged ? 0 : 605 xe_res_dma(curs) + 606 xe_walk->dma_offset, 607 xe_walk->vma, 608 pat_index, level); 609 if (!is_null_or_purged) 610 pte |= is_vram ? xe_walk->default_vram_pte : 611 xe_walk->default_system_pte; 612 613 /* 614 * Set the XE_PTE_PS64 hint if possible, otherwise if 615 * this device *requires* 64K PTE size for VRAM, fail. 616 */ 617 if (level == 0 && !xe_parent->is_compact) { 618 if (xe_pt_is_pte_ps64K(addr, next, xe_walk)) { 619 xe_walk->vma->gpuva.flags |= 620 XE_VMA_PTE_64K; 621 pte |= XE_PTE_PS64; 622 } else if (XE_WARN_ON(xe_walk->needs_64K && 623 is_vram)) { 624 return -EINVAL; 625 } 626 } 627 } 628 629 ret = xe_pt_insert_entry(xe_walk, xe_parent, offset, NULL, pte); 630 if (unlikely(ret)) 631 return ret; 632 633 if (!is_null_or_purged && !xe_walk->clear_pt) 634 xe_res_next(curs, next - addr); 635 xe_walk->va_curs_start = next; 636 xe_walk->vma->gpuva.flags |= (XE_VMA_PTE_4K << level); 637 *action = ACTION_CONTINUE; 638 639 return ret; 640 } 641 642 /* 643 * Descending to lower level. Determine if we need to allocate a 644 * new page table or -directory, which we do if there is no 645 * previous one or there is one we can completely replace. 646 */ 647 if (level == 1) { 648 walk->shifts = xe_normal_pt_shifts; 649 xe_walk->l0_end_addr = next; 650 } 651 652 covers = xe_pt_covers(addr, next, level, &xe_walk->base); 653 if (covers || !*child) { 654 u64 flags = 0; 655 656 xe_child = xe_pt_create(xe_walk->vm, xe_walk->tile, level - 1, 657 xe_vm_validation_exec(vm)); 658 if (IS_ERR(xe_child)) 659 return PTR_ERR(xe_child); 660 661 xe_pt_set_addr(xe_child, 662 round_down(addr, 1ull << walk->shifts[level])); 663 664 if (!covers) 665 xe_pt_populate_empty(xe_walk->tile, xe_walk->vm, xe_child); 666 667 *child = &xe_child->base; 668 669 /* 670 * Prefer the compact pagetable layout for L0 if possible. Only 671 * possible if VMA covers entire 2MB region as compact 64k and 672 * 4k pages cannot be mixed within a 2MB region. 673 * TODO: Suballocate the pt bo to avoid wasting a lot of 674 * memory. 675 */ 676 if (GRAPHICS_VERx100(tile_to_xe(xe_walk->tile)) >= 1250 && level == 1 && 677 covers && xe_pt_scan_64K(addr, next, xe_walk)) { 678 walk->shifts = xe_compact_pt_shifts; 679 xe_walk->vma->gpuva.flags |= XE_VMA_PTE_COMPACT; 680 flags |= XE_PDE_64K; 681 xe_child->is_compact = true; 682 } 683 684 pte = vm->pt_ops->pde_encode_bo(xe_child->bo, 0) | flags; 685 ret = xe_pt_insert_entry(xe_walk, xe_parent, offset, xe_child, 686 pte); 687 } 688 689 *action = ACTION_SUBTREE; 690 return ret; 691 } 692 693 static const struct xe_pt_walk_ops xe_pt_stage_bind_ops = { 694 .pt_entry = xe_pt_stage_bind_entry, 695 }; 696 697 /* 698 * Default atomic expectations for different allocation scenarios are as follows: 699 * 700 * 1. Traditional API: When the VM is not in LR mode: 701 * - Device atomics are expected to function with all allocations. 702 * 703 * 2. Compute/SVM API: When the VM is in LR mode: 704 * - Device atomics are the default behavior when the bo is placed in a single region. 705 * - In all other cases device atomics will be disabled with AE=0 until an application 706 * request differently using a ioctl like madvise. 707 */ 708 static bool xe_atomic_for_vram(struct xe_vm *vm, struct xe_vma *vma) 709 { 710 if (vma->attr.atomic_access == DRM_XE_ATOMIC_CPU) 711 return false; 712 713 return true; 714 } 715 716 static bool xe_atomic_for_system(struct xe_vm *vm, struct xe_vma *vma) 717 { 718 struct xe_device *xe = vm->xe; 719 struct xe_bo *bo = xe_vma_bo(vma); 720 721 if (!xe->info.has_device_atomics_on_smem || 722 vma->attr.atomic_access == DRM_XE_ATOMIC_CPU) 723 return false; 724 725 if (vma->attr.atomic_access == DRM_XE_ATOMIC_DEVICE) 726 return true; 727 728 /* 729 * If a SMEM+LMEM allocation is backed by SMEM, a device 730 * atomics will cause a gpu page fault and which then 731 * gets migrated to LMEM, bind such allocations with 732 * device atomics enabled. 733 */ 734 return (!IS_DGFX(xe) || (!xe_vm_in_lr_mode(vm) || 735 (bo && xe_bo_has_single_placement(bo)))); 736 } 737 738 static u32 xe_pt_target_leaf_level_from_bo(struct xe_device *xe, 739 struct xe_vma *vma) 740 { 741 struct xe_bo *bo = xe_vma_bo(vma); 742 743 if (!xe_debug_page_size_mode_not_none(xe)) 744 return 0; 745 746 if (!bo || !xe_bo_is_vram(bo) || !(bo->flags & XE_BO_FLAG_USER)) 747 return 0; 748 749 if (bo->flags & XE_BO_FLAG_NEEDS_1G) 750 return 2; 751 752 if (bo->flags & XE_BO_FLAG_NEEDS_2M) 753 return 1; 754 755 return 0; 756 } 757 758 /** 759 * xe_pt_stage_bind() - Build a disconnected page-table tree for a given address 760 * range. 761 * @tile: The tile we're building for. 762 * @vma: The vma indicating the address range. 763 * @range: The range indicating the address range. 764 * @entries: Storage for the update entries used for connecting the tree to 765 * the main tree at commit time. 766 * @num_entries: On output contains the number of @entries used. 767 * @clear_pt: Clear the page table entries. 768 * 769 * This function builds a disconnected page-table tree for a given address 770 * range. The tree is connected to the main vm tree for the gpu using 771 * xe_migrate_update_pgtables() and for the cpu using xe_pt_commit_bind(). 772 * The function builds xe_vm_pgtable_update structures for already existing 773 * shared page-tables, and non-existing shared and non-shared page-tables 774 * are built and populated directly. 775 * 776 * Return 0 on success, negative error code on error. 777 */ 778 static int 779 xe_pt_stage_bind(struct xe_tile *tile, struct xe_vma *vma, 780 struct xe_svm_range *range, 781 struct xe_vm_pgtable_update *entries, 782 u32 *num_entries, bool clear_pt) 783 { 784 struct xe_device *xe = tile_to_xe(tile); 785 struct xe_bo *bo = xe_vma_bo(vma); 786 struct xe_res_cursor curs = {}; 787 struct xe_vm *vm = xe_vma_vm(vma); 788 struct xe_pt_stage_bind_walk xe_walk = { 789 .base = { 790 .ops = &xe_pt_stage_bind_ops, 791 .shifts = xe_normal_pt_shifts, 792 .max_level = XE_PT_HIGHEST_LEVEL, 793 .staging = true, 794 }, 795 .vm = vm, 796 .tile = tile, 797 .curs = &curs, 798 .va_curs_start = range ? xe_svm_range_start(range) : 799 xe_vma_start(vma), 800 .vma = vma, 801 .wupd.entries = entries, 802 .clear_pt = clear_pt, 803 }; 804 struct xe_pt *pt = vm->pt_root[tile->id]; 805 int ret; 806 bool is_purged = false; 807 808 /* 809 * Check if BO is purged: 810 * - Scratch VMs: Use scratch PTEs (XE_PTE_NULL) for safe zero reads 811 * - Non-scratch VMs: Clear PTEs to zero (non-present) to avoid mapping to phys addr 0 812 * 813 * For non-scratch VMs, we force clear_pt=true so leaf PTEs become completely 814 * zero instead of creating a PRESENT mapping to physical address 0. 815 */ 816 if (bo && xe_bo_is_purged(bo)) { 817 is_purged = true; 818 819 /* 820 * For non-scratch VMs, a NULL rebind should use zero PTEs 821 * (non-present), not a present PTE to phys 0. 822 */ 823 if (!xe_vm_has_scratch(vm)) 824 xe_walk.clear_pt = true; 825 } 826 827 if (range) { 828 /* Move this entire thing to xe_svm.c? */ 829 xe_svm_notifier_lock(vm); 830 if (!xe_svm_range_pages_valid(range)) { 831 xe_svm_range_debug(range, "BIND PREPARE - RETRY"); 832 xe_svm_notifier_unlock(vm); 833 return -EAGAIN; 834 } 835 if (xe_svm_range_has_dma_mapping(range)) { 836 const struct drm_pagemap_addr *addr; 837 bool contiguous; 838 839 addr = xe_svm_range_first_dma(range, &contiguous); 840 xe_res_first_dma(addr, 0, xe_svm_range_size(range), 841 contiguous, &curs); 842 xe_svm_range_debug(range, "BIND PREPARE - MIXED"); 843 } else { 844 xe_assert(xe, false); 845 } 846 /* 847 * Note, when unlocking the resource cursor dma addresses may become 848 * stale, but the bind will be aborted anyway at commit time. 849 */ 850 xe_svm_notifier_unlock(vm); 851 } 852 853 xe_walk.target_leaf_level = xe_pt_target_leaf_level_from_bo(xe, vma); 854 xe_walk.needs_64K = (vm->flags & XE_VM_FLAG_64K); 855 if (clear_pt) { 856 xe_assert(xe, !range); 857 curs.size = xe_vma_size(vma); 858 goto walk_pt; 859 } 860 861 if (vma->gpuva.flags & XE_VMA_ATOMIC_PTE_BIT) { 862 xe_walk.default_vram_pte = xe_atomic_for_vram(vm, vma) ? XE_USM_PPGTT_PTE_AE : 0; 863 xe_walk.default_system_pte = xe_atomic_for_system(vm, vma) ? 864 XE_USM_PPGTT_PTE_AE : 0; 865 } 866 867 xe_walk.default_vram_pte |= XE_PPGTT_PTE_DM; 868 xe_walk.dma_offset = (bo && !is_purged) ? vram_region_gpu_offset(bo->ttm.resource) : 0; 869 if (!range) 870 xe_bo_assert_held(bo); 871 872 if (!xe_vma_is_null(vma) && !range && !is_purged) { 873 if (xe_vma_is_userptr(vma)) { 874 const struct drm_pagemap_addr *addr; 875 bool contiguous; 876 877 addr = drm_gpusvm_pages_first_dma(&to_userptr_vma(vma)->userptr.pages, 878 &contiguous); 879 xe_res_first_dma(addr, 0, xe_vma_size(vma), contiguous, 880 &curs); 881 } else if (xe_bo_is_vram(bo) || xe_bo_is_stolen(bo)) 882 xe_res_first(bo->ttm.resource, xe_vma_bo_offset(vma), 883 xe_vma_size(vma), &curs); 884 else 885 xe_res_first_sg(xe_bo_sg(bo), xe_vma_bo_offset(vma), 886 xe_vma_size(vma), &curs); 887 } else if (!range) { 888 curs.size = xe_vma_size(vma); 889 } 890 891 walk_pt: 892 ret = xe_pt_walk_range(&pt->base, pt->level, 893 range ? xe_svm_range_start(range) : xe_vma_start(vma), 894 range ? xe_svm_range_end(range) : xe_vma_end(vma), 895 &xe_walk.base); 896 897 *num_entries = xe_walk.wupd.num_used_entries; 898 return ret; 899 } 900 901 /** 902 * xe_pt_nonshared_offsets() - Determine the non-shared entry offsets of a 903 * shared pagetable. 904 * @addr: The start address within the non-shared pagetable. 905 * @end: The end address within the non-shared pagetable. 906 * @level: The level of the non-shared pagetable. 907 * @walk: Walk info. The function adjusts the walk action. 908 * @action: next action to perform (see enum page_walk_action) 909 * @offset: Ignored on input, First non-shared entry on output. 910 * @end_offset: Ignored on input, Last non-shared entry + 1 on output. 911 * 912 * A non-shared page-table has some entries that belong to the address range 913 * and others that don't. This function determines the entries that belong 914 * fully to the address range. Depending on level, some entries may 915 * partially belong to the address range (that can't happen at level 0). 916 * The function detects that and adjust those offsets to not include those 917 * partial entries. Iff it does detect partial entries, we know that there must 918 * be shared page tables also at lower levels, so it adjusts the walk action 919 * accordingly. 920 * 921 * Return: true if there were non-shared entries, false otherwise. 922 */ 923 static bool xe_pt_nonshared_offsets(u64 addr, u64 end, unsigned int level, 924 struct xe_pt_walk *walk, 925 enum page_walk_action *action, 926 pgoff_t *offset, pgoff_t *end_offset) 927 { 928 u64 size = 1ull << walk->shifts[level]; 929 930 *offset = xe_pt_offset(addr, level, walk); 931 *end_offset = xe_pt_num_entries(addr, end, level, walk) + *offset; 932 933 if (!level) 934 return true; 935 936 /* 937 * If addr or next are not size aligned, there are shared pts at lower 938 * level, so in that case traverse down the subtree 939 */ 940 *action = ACTION_CONTINUE; 941 if (!IS_ALIGNED(addr, size)) { 942 *action = ACTION_SUBTREE; 943 (*offset)++; 944 } 945 946 if (!IS_ALIGNED(end, size)) { 947 *action = ACTION_SUBTREE; 948 (*end_offset)--; 949 } 950 951 return *end_offset > *offset; 952 } 953 954 struct xe_pt_zap_ptes_walk { 955 /** @base: The walk base-class */ 956 struct xe_pt_walk base; 957 958 /* Input parameters for the walk */ 959 /** @tile: The tile we're building for */ 960 struct xe_tile *tile; 961 962 /* Output */ 963 /** @needs_invalidate: Whether we need to invalidate TLB*/ 964 bool needs_invalidate; 965 }; 966 967 static int xe_pt_zap_ptes_entry(struct xe_ptw *parent, pgoff_t offset, 968 unsigned int level, u64 addr, u64 next, 969 struct xe_ptw **child, 970 enum page_walk_action *action, 971 struct xe_pt_walk *walk) 972 { 973 struct xe_pt_zap_ptes_walk *xe_walk = 974 container_of(walk, typeof(*xe_walk), base); 975 struct xe_pt *xe_child; 976 pgoff_t end_offset; 977 978 XE_WARN_ON(!level); 979 980 /* 981 * Below would be unexpected behavior that needs to be root caused 982 * but better warn and bail than crash the driver. 983 */ 984 if (XE_WARN_ON(!*child)) 985 return 0; 986 987 xe_child = container_of(*child, typeof(*xe_child), base); 988 989 /* 990 * Note that we're called from an entry callback, and we're dealing 991 * with the child of that entry rather than the parent, so need to 992 * adjust level down. 993 */ 994 if (xe_pt_nonshared_offsets(addr, next, --level, walk, action, &offset, 995 &end_offset)) { 996 xe_map_memset(tile_to_xe(xe_walk->tile), &xe_child->bo->vmap, 997 offset * sizeof(u64), 0, 998 (end_offset - offset) * sizeof(u64)); 999 xe_walk->needs_invalidate = true; 1000 } 1001 1002 return 0; 1003 } 1004 1005 static const struct xe_pt_walk_ops xe_pt_zap_ptes_ops = { 1006 .pt_entry = xe_pt_zap_ptes_entry, 1007 }; 1008 1009 /** 1010 * xe_pt_zap_ptes() - Zap (zero) gpu ptes of an address range 1011 * @tile: The tile we're zapping for. 1012 * @vma: GPU VMA detailing address range. 1013 * 1014 * Eviction and Userptr invalidation needs to be able to zap the 1015 * gpu ptes of a given address range in pagefaulting mode. 1016 * In order to be able to do that, that function needs access to the shared 1017 * page-table entrieaso it can either clear the leaf PTEs or 1018 * clear the pointers to lower-level page-tables. The caller is required 1019 * to hold the necessary locks to ensure neither the page-table connectivity 1020 * nor the page-table entries of the range is updated from under us. 1021 * 1022 * Return: Whether ptes were actually updated and a TLB invalidation is 1023 * required. 1024 */ 1025 bool xe_pt_zap_ptes(struct xe_tile *tile, struct xe_vma *vma) 1026 { 1027 struct xe_pt_zap_ptes_walk xe_walk = { 1028 .base = { 1029 .ops = &xe_pt_zap_ptes_ops, 1030 .shifts = xe_normal_pt_shifts, 1031 .max_level = XE_PT_HIGHEST_LEVEL, 1032 }, 1033 .tile = tile, 1034 }; 1035 struct xe_pt *pt = xe_vma_vm(vma)->pt_root[tile->id]; 1036 u8 pt_mask = (vma->tile_present & ~vma->tile_invalidated); 1037 1038 if (xe_vma_bo(vma)) 1039 xe_bo_assert_held(xe_vma_bo(vma)); 1040 else if (xe_vma_is_userptr(vma)) 1041 lockdep_assert_held(&xe_vma_vm(vma)->svm.gpusvm.notifier_lock); 1042 1043 if (!(pt_mask & BIT(tile->id))) 1044 return false; 1045 1046 (void)xe_pt_walk_shared(&pt->base, pt->level, xe_vma_start(vma), 1047 xe_vma_end(vma), &xe_walk.base); 1048 1049 return xe_walk.needs_invalidate; 1050 } 1051 1052 /** 1053 * xe_pt_zap_ptes_range() - Zap (zero) gpu ptes of a SVM range 1054 * @tile: The tile we're zapping for. 1055 * @vm: The VM we're zapping for. 1056 * @range: The SVM range we're zapping for. 1057 * 1058 * SVM invalidation needs to be able to zap the gpu ptes of a given address 1059 * range. In order to be able to do that, that function needs access to the 1060 * shared page-table entries so it can either clear the leaf PTEs or 1061 * clear the pointers to lower-level page-tables. The caller is required 1062 * to hold the SVM notifier lock. 1063 * 1064 * Return: Whether ptes were actually updated and a TLB invalidation is 1065 * required. 1066 */ 1067 bool xe_pt_zap_ptes_range(struct xe_tile *tile, struct xe_vm *vm, 1068 struct xe_svm_range *range) 1069 { 1070 struct xe_pt_zap_ptes_walk xe_walk = { 1071 .base = { 1072 .ops = &xe_pt_zap_ptes_ops, 1073 .shifts = xe_normal_pt_shifts, 1074 .max_level = XE_PT_HIGHEST_LEVEL, 1075 }, 1076 .tile = tile, 1077 }; 1078 struct xe_pt *pt = vm->pt_root[tile->id]; 1079 u8 pt_mask = (range->tile_present & ~range->tile_invalidated); 1080 1081 /* 1082 * Locking rules: 1083 * 1084 * - notifier_lock (write): full protection against page table changes 1085 * and MMU notifier invalidations. 1086 * 1087 * - notifier_lock (read) + vm_lock (write): combined protection against 1088 * invalidations and concurrent page table modifications. (e.g., madvise) 1089 * 1090 */ 1091 lockdep_assert(lockdep_is_held_type(&vm->svm.gpusvm.notifier_lock, 0) || 1092 (lockdep_is_held_type(&vm->svm.gpusvm.notifier_lock, 1) && 1093 lockdep_is_held_type(&vm->lock, 0))); 1094 1095 if (!(pt_mask & BIT(tile->id))) 1096 return false; 1097 1098 (void)xe_pt_walk_shared(&pt->base, pt->level, xe_svm_range_start(range), 1099 xe_svm_range_end(range), &xe_walk.base); 1100 1101 return xe_walk.needs_invalidate; 1102 } 1103 1104 static void 1105 xe_vm_populate_pgtable(struct xe_migrate_pt_update *pt_update, struct xe_tile *tile, 1106 struct iosys_map *map, void *data, 1107 u32 qword_ofs, u32 num_qwords, 1108 const struct xe_vm_pgtable_update *update) 1109 { 1110 struct xe_pt_entry *ptes = update->pt_entries; 1111 u64 *ptr = data; 1112 u32 i; 1113 1114 /* 1115 * @qword_ofs is the absolute entry offset within the page table, while 1116 * @ptes is indexed relative to @update->ofs (its first entry). The GPU 1117 * path (write_pgtable) splits a single update into MAX_PTE_PER_SDI-sized 1118 * chunks, calling this with an advancing @qword_ofs but a fresh @data 1119 * pointer per chunk, so translate back into a @ptes index rather than 1120 * assuming the chunk starts at ptes[0]. 1121 */ 1122 for (i = 0; i < num_qwords; i++) { 1123 u32 idx = qword_ofs - update->ofs + i; 1124 1125 if (map) 1126 xe_map_wr(tile_to_xe(tile), map, (qword_ofs + i) * 1127 sizeof(u64), u64, ptes[idx].pte); 1128 else 1129 ptr[i] = ptes[idx].pte; 1130 } 1131 } 1132 1133 static void xe_pt_cancel_bind(struct xe_vma *vma, 1134 struct xe_vm_pgtable_update *entries, 1135 u32 num_entries) 1136 { 1137 u32 i, j; 1138 1139 for (i = 0; i < num_entries; i++) { 1140 struct xe_pt *pt = entries[i].pt; 1141 1142 if (!pt) 1143 continue; 1144 1145 if (pt->level) { 1146 for (j = 0; j < entries[i].qwords; j++) 1147 xe_pt_destroy(entries[i].pt_entries[j].pt, 1148 xe_vma_vm(vma)->flags, NULL); 1149 } 1150 1151 kfree(entries[i].pt_entries); 1152 entries[i].pt_entries = NULL; 1153 entries[i].qwords = 0; 1154 } 1155 } 1156 1157 #define XE_INVALID_VMA ((struct xe_vma *)(0xdeaddeadull)) 1158 1159 static void xe_pt_commit_prepare_locks_assert(struct xe_vma *vma) 1160 { 1161 struct xe_vm *vm; 1162 1163 if (vma == XE_INVALID_VMA) 1164 return; 1165 1166 vm = xe_vma_vm(vma); 1167 lockdep_assert_held(&vm->lock); 1168 1169 if (!xe_vma_has_no_bo(vma)) 1170 dma_resv_assert_held(xe_vma_bo(vma)->ttm.base.resv); 1171 1172 xe_vm_assert_held(vm); 1173 } 1174 1175 static void xe_pt_commit_locks_assert(struct xe_vma *vma) 1176 { 1177 struct xe_vm *vm; 1178 1179 if (vma == XE_INVALID_VMA) 1180 return; 1181 1182 vm = xe_vma_vm(vma); 1183 xe_pt_commit_prepare_locks_assert(vma); 1184 1185 if (xe_vma_is_userptr(vma)) 1186 xe_svm_assert_held_read_or_inject_write(vm); 1187 } 1188 1189 static void xe_pt_commit(struct xe_vma *vma, 1190 struct xe_vm_pgtable_update *entries, 1191 u32 num_entries, struct llist_head *deferred) 1192 { 1193 u32 i, j; 1194 1195 xe_pt_commit_locks_assert(vma); 1196 1197 for (i = 0; i < num_entries; i++) { 1198 struct xe_pt *pt = entries[i].pt; 1199 struct xe_pt_dir *pt_dir; 1200 1201 if (!pt->level) 1202 continue; 1203 1204 pt_dir = as_xe_pt_dir(pt); 1205 for (j = 0; j < entries[i].qwords; j++) { 1206 struct xe_pt *oldpte = entries[i].pt_entries[j].pt; 1207 int j_ = j + entries[i].ofs; 1208 1209 pt_dir->children[j_] = pt_dir->staging[j_]; 1210 xe_pt_destroy(oldpte, (vma == XE_INVALID_VMA) ? 0 : 1211 xe_vma_vm(vma)->flags, deferred); 1212 } 1213 } 1214 } 1215 1216 static void xe_pt_abort_bind(struct xe_vma *vma, 1217 struct xe_vm_pgtable_update *entries, 1218 u32 num_entries, bool rebind) 1219 { 1220 int i, j; 1221 1222 xe_pt_commit_prepare_locks_assert(vma); 1223 1224 for (i = num_entries - 1; i >= 0; --i) { 1225 struct xe_pt *pt = entries[i].pt; 1226 struct xe_pt_dir *pt_dir; 1227 1228 if (!rebind) 1229 pt->num_live -= entries[i].qwords; 1230 1231 if (!pt->level) 1232 continue; 1233 1234 pt_dir = as_xe_pt_dir(pt); 1235 for (j = 0; j < entries[i].qwords; j++) { 1236 u32 j_ = j + entries[i].ofs; 1237 struct xe_pt *newpte = xe_pt_entry_staging(pt_dir, j_); 1238 struct xe_pt *oldpte = entries[i].pt_entries[j].pt; 1239 1240 pt_dir->staging[j_] = oldpte ? &oldpte->base : 0; 1241 xe_pt_destroy(newpte, xe_vma_vm(vma)->flags, NULL); 1242 } 1243 } 1244 } 1245 1246 static void xe_pt_commit_prepare_bind(struct xe_vma *vma, 1247 struct xe_vm_pgtable_update *entries, 1248 u32 num_entries, bool rebind) 1249 { 1250 u32 i, j; 1251 1252 xe_pt_commit_prepare_locks_assert(vma); 1253 1254 for (i = 0; i < num_entries; i++) { 1255 struct xe_pt *pt = entries[i].pt; 1256 struct xe_pt_dir *pt_dir; 1257 1258 if (!rebind) 1259 pt->num_live += entries[i].qwords; 1260 1261 if (!pt->level) 1262 continue; 1263 1264 pt_dir = as_xe_pt_dir(pt); 1265 for (j = 0; j < entries[i].qwords; j++) { 1266 u32 j_ = j + entries[i].ofs; 1267 struct xe_pt *newpte = entries[i].pt_entries[j].pt; 1268 struct xe_pt *oldpte = NULL; 1269 1270 if (xe_pt_entry_staging(pt_dir, j_)) 1271 oldpte = xe_pt_entry_staging(pt_dir, j_); 1272 1273 pt_dir->staging[j_] = &newpte->base; 1274 entries[i].pt_entries[j].pt = oldpte; 1275 } 1276 } 1277 } 1278 1279 static void xe_pt_free_bind(struct xe_vm_pgtable_update *entries, 1280 u32 num_entries) 1281 { 1282 u32 i; 1283 1284 for (i = 0; i < num_entries; i++) 1285 kfree(entries[i].pt_entries); 1286 } 1287 1288 static int 1289 xe_pt_prepare_bind(struct xe_tile *tile, struct xe_vma *vma, 1290 struct xe_svm_range *range, 1291 struct xe_vm_pgtable_update *entries, 1292 u32 *num_entries, bool invalidate_on_bind) 1293 { 1294 int err; 1295 1296 *num_entries = 0; 1297 err = xe_pt_stage_bind(tile, vma, range, entries, num_entries, 1298 invalidate_on_bind); 1299 if (!err) 1300 xe_tile_assert(tile, *num_entries); 1301 1302 return err; 1303 } 1304 1305 static void xe_vm_dbg_print_entries(struct xe_device *xe, 1306 const struct xe_vm_pgtable_update *entries, 1307 unsigned int num_entries, bool bind) 1308 #if (IS_ENABLED(CONFIG_DRM_XE_DEBUG_VM)) 1309 { 1310 unsigned int i; 1311 1312 vm_dbg(&xe->drm, "%s: %u entries to update\n", bind ? "bind" : "unbind", 1313 num_entries); 1314 for (i = 0; i < num_entries; i++) { 1315 const struct xe_vm_pgtable_update *entry = &entries[i]; 1316 struct xe_pt *xe_pt = entry->pt; 1317 u64 page_size = 1ull << xe_pt_shift(xe_pt->level); 1318 u64 end; 1319 u64 start; 1320 1321 xe_assert(xe, !entry->pt->is_compact); 1322 start = entry->ofs * page_size; 1323 end = start + page_size * entry->qwords; 1324 vm_dbg(&xe->drm, 1325 "\t%u: Update level %u at (%u + %u) [%llx...%llx) f:%x\n", 1326 i, xe_pt->level, entry->ofs, entry->qwords, 1327 xe_pt_addr(xe_pt) + start, xe_pt_addr(xe_pt) + end, 0); 1328 } 1329 } 1330 #else 1331 {} 1332 #endif 1333 1334 static bool no_in_syncs(struct xe_sync_entry *syncs, u32 num_syncs) 1335 { 1336 int i; 1337 1338 for (i = 0; i < num_syncs; i++) { 1339 struct dma_fence *fence = syncs[i].fence; 1340 1341 if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, 1342 &fence->flags)) 1343 return false; 1344 } 1345 1346 return true; 1347 } 1348 1349 static int job_test_add_deps(struct xe_sched_job *job, 1350 struct dma_resv *resv, 1351 enum dma_resv_usage usage) 1352 { 1353 if (!job) { 1354 if (!dma_resv_test_signaled(resv, usage)) 1355 return -ETIME; 1356 1357 return 0; 1358 } 1359 1360 return xe_sched_job_add_deps(job, resv, usage); 1361 } 1362 1363 static int vma_add_deps(struct xe_vma *vma, struct xe_sched_job *job) 1364 { 1365 struct xe_bo *bo = xe_vma_bo(vma); 1366 1367 xe_bo_assert_held(bo); 1368 1369 if (bo && !bo->vm) 1370 return job_test_add_deps(job, bo->ttm.base.resv, 1371 DMA_RESV_USAGE_KERNEL); 1372 1373 return 0; 1374 } 1375 1376 static int op_add_deps(struct xe_vm *vm, struct xe_vma_op *op, 1377 struct xe_sched_job *job) 1378 { 1379 int err = 0; 1380 1381 /* 1382 * No need to check for is_cpu_addr_mirror here as vma_add_deps is a 1383 * NOP if VMA is_cpu_addr_mirror 1384 */ 1385 1386 switch (op->base.op) { 1387 case DRM_GPUVA_OP_MAP: 1388 if (!op->map.immediate && xe_vm_in_fault_mode(vm)) 1389 break; 1390 1391 err = vma_add_deps(op->map.vma, job); 1392 break; 1393 case DRM_GPUVA_OP_REMAP: 1394 if (op->remap.prev) 1395 err = vma_add_deps(op->remap.prev, job); 1396 if (!err && op->remap.next) 1397 err = vma_add_deps(op->remap.next, job); 1398 break; 1399 case DRM_GPUVA_OP_UNMAP: 1400 break; 1401 case DRM_GPUVA_OP_PREFETCH: 1402 err = vma_add_deps(gpuva_to_vma(op->base.prefetch.va), job); 1403 break; 1404 case DRM_GPUVA_OP_DRIVER: 1405 break; 1406 default: 1407 drm_warn(&vm->xe->drm, "NOT POSSIBLE"); 1408 } 1409 1410 return err; 1411 } 1412 1413 static int xe_pt_vm_dependencies(struct xe_sched_job *job, 1414 struct xe_tlb_inval_job *ijob, 1415 struct xe_tlb_inval_job *mjob, 1416 struct xe_vm *vm, 1417 struct xe_vma_ops *vops, 1418 struct xe_vm_pgtable_update_ops *pt_update_ops, 1419 struct xe_range_fence_tree *rftree) 1420 { 1421 struct xe_range_fence *rtfence; 1422 struct dma_fence *fence; 1423 struct xe_vma_op *op; 1424 int err = 0, i; 1425 1426 xe_vm_assert_held(vm); 1427 1428 if (!job && !no_in_syncs(vops->syncs, vops->num_syncs)) 1429 return -ETIME; 1430 1431 if (!job && !xe_exec_queue_is_idle(pt_update_ops->q)) 1432 return -ETIME; 1433 1434 if (pt_update_ops->wait_vm_bookkeep || pt_update_ops->wait_vm_kernel) { 1435 err = job_test_add_deps(job, xe_vm_resv(vm), 1436 pt_update_ops->wait_vm_bookkeep ? 1437 DMA_RESV_USAGE_BOOKKEEP : 1438 DMA_RESV_USAGE_KERNEL); 1439 if (err) 1440 return err; 1441 } 1442 1443 rtfence = xe_range_fence_tree_first(rftree, pt_update_ops->start, 1444 pt_update_ops->last); 1445 while (rtfence) { 1446 fence = rtfence->fence; 1447 1448 if (!dma_fence_is_signaled(fence)) { 1449 /* 1450 * Is this a CPU update? GPU is busy updating, so return 1451 * an error 1452 */ 1453 if (!job) 1454 return -ETIME; 1455 1456 dma_fence_get(fence); 1457 err = drm_sched_job_add_dependency(&job->drm, fence); 1458 if (err) 1459 return err; 1460 } 1461 1462 rtfence = xe_range_fence_tree_next(rtfence, 1463 pt_update_ops->start, 1464 pt_update_ops->last); 1465 } 1466 1467 list_for_each_entry(op, &vops->list, link) { 1468 err = op_add_deps(vm, op, job); 1469 if (err) 1470 return err; 1471 } 1472 1473 for (i = 0; job && !err && i < vops->num_syncs; i++) 1474 err = xe_sync_entry_add_deps(&vops->syncs[i], job); 1475 1476 if (job) { 1477 if (ijob) { 1478 err = xe_tlb_inval_job_alloc_dep(ijob); 1479 if (err) 1480 return err; 1481 } 1482 1483 if (mjob) { 1484 err = xe_tlb_inval_job_alloc_dep(mjob); 1485 if (err) 1486 return err; 1487 } 1488 } 1489 1490 return err; 1491 } 1492 1493 static int xe_pt_pre_commit(struct xe_migrate_pt_update *pt_update) 1494 { 1495 struct xe_vma_ops *vops = pt_update->vops; 1496 struct xe_vm *vm = vops->vm; 1497 struct xe_range_fence_tree *rftree = &vm->rftree[pt_update->tile_id]; 1498 struct xe_vm_pgtable_update_ops *pt_update_ops = 1499 &vops->pt_update_ops[pt_update->tile_id]; 1500 1501 return xe_pt_vm_dependencies(pt_update->job, pt_update->ijob, 1502 pt_update->mjob, vm, pt_update->vops, 1503 pt_update_ops, rftree); 1504 } 1505 1506 #if IS_ENABLED(CONFIG_DRM_GPUSVM) 1507 /* 1508 * Acquire/release the svm notifier_lock around xe_pt_svm_userptr_pre_commit() 1509 * and the matching late release in xe_pt_update_ops_run(). Read mode by 1510 * default; write mode when CONFIG_DRM_XE_USERPTR_INVAL_INJECT is on, 1511 * because a userptr op in this critical section may invoke the injected 1512 * xe_vma_userptr_force_invalidate() path that calls 1513 * drm_gpusvm_unmap_pages() with ctx->in_notifier=true, which requires the 1514 * lock held for write. 1515 */ 1516 static void xe_pt_svm_userptr_notifier_lock(struct xe_vm *vm) 1517 { 1518 #if IS_ENABLED(CONFIG_DRM_XE_USERPTR_INVAL_INJECT) 1519 down_write(&vm->svm.gpusvm.notifier_lock); 1520 #else 1521 xe_svm_notifier_lock(vm); 1522 #endif 1523 } 1524 1525 static void xe_pt_svm_userptr_notifier_unlock(struct xe_vm *vm) 1526 { 1527 #if IS_ENABLED(CONFIG_DRM_XE_USERPTR_INVAL_INJECT) 1528 up_write(&vm->svm.gpusvm.notifier_lock); 1529 #else 1530 xe_svm_notifier_unlock(vm); 1531 #endif 1532 } 1533 #else 1534 static inline void xe_pt_svm_userptr_notifier_lock(struct xe_vm *vm) { } 1535 static inline void xe_pt_svm_userptr_notifier_unlock(struct xe_vm *vm) { } 1536 #endif 1537 1538 #if IS_ENABLED(CONFIG_DRM_GPUSVM) 1539 #ifdef CONFIG_DRM_XE_USERPTR_INVAL_INJECT 1540 1541 static bool xe_pt_userptr_inject_eagain(struct xe_userptr_vma *uvma) 1542 { 1543 u32 divisor = uvma->userptr.divisor ? uvma->userptr.divisor : 2; 1544 static u32 count; 1545 1546 if (count++ % divisor == divisor - 1) { 1547 uvma->userptr.divisor = divisor << 1; 1548 return true; 1549 } 1550 1551 return false; 1552 } 1553 1554 #else 1555 1556 static bool xe_pt_userptr_inject_eagain(struct xe_userptr_vma *uvma) 1557 { 1558 return false; 1559 } 1560 1561 #endif 1562 1563 static int vma_check_userptr(struct xe_vm *vm, struct xe_vma *vma, 1564 struct xe_vm_pgtable_update_ops *pt_update) 1565 { 1566 struct xe_userptr_vma *uvma; 1567 unsigned long notifier_seq; 1568 1569 xe_svm_assert_held_read_or_inject_write(vm); 1570 1571 if (!xe_vma_is_userptr(vma)) 1572 return 0; 1573 1574 uvma = to_userptr_vma(vma); 1575 if (xe_pt_userptr_inject_eagain(uvma)) 1576 xe_vma_userptr_force_invalidate(uvma); 1577 1578 notifier_seq = uvma->userptr.pages.notifier_seq; 1579 1580 if (!mmu_interval_read_retry(&uvma->userptr.notifier, 1581 notifier_seq)) 1582 return 0; 1583 1584 if (xe_vm_in_fault_mode(vm)) 1585 return -EAGAIN; 1586 1587 /* 1588 * Just continue the operation since exec or rebind worker 1589 * will take care of rebinding. 1590 */ 1591 return 0; 1592 } 1593 1594 static int op_check_svm_userptr(struct xe_vm *vm, struct xe_vma_op *op, 1595 struct xe_vm_pgtable_update_ops *pt_update) 1596 { 1597 int err = 0; 1598 1599 xe_svm_assert_held_read_or_inject_write(vm); 1600 1601 switch (op->base.op) { 1602 case DRM_GPUVA_OP_MAP: 1603 if (!op->map.immediate && xe_vm_in_fault_mode(vm)) 1604 break; 1605 1606 err = vma_check_userptr(vm, op->map.vma, pt_update); 1607 break; 1608 case DRM_GPUVA_OP_REMAP: 1609 if (op->remap.prev && !op->remap.skip_prev) 1610 err = vma_check_userptr(vm, op->remap.prev, pt_update); 1611 if (!err && op->remap.next && !op->remap.skip_next) 1612 err = vma_check_userptr(vm, op->remap.next, pt_update); 1613 break; 1614 case DRM_GPUVA_OP_UNMAP: 1615 break; 1616 case DRM_GPUVA_OP_PREFETCH: 1617 if (xe_vma_is_cpu_addr_mirror(gpuva_to_vma(op->base.prefetch.va))) { 1618 struct xe_svm_range *range = op->map_range.range; 1619 unsigned long i; 1620 1621 xe_assert(vm->xe, 1622 xe_vma_is_cpu_addr_mirror(gpuva_to_vma(op->base.prefetch.va))); 1623 xa_for_each(&op->prefetch_range.range, i, range) { 1624 xe_svm_range_debug(range, "PRE-COMMIT"); 1625 1626 if (!xe_svm_range_pages_valid(range)) { 1627 xe_svm_range_debug(range, "PRE-COMMIT - RETRY"); 1628 return -ENODATA; 1629 } 1630 } 1631 } else { 1632 err = vma_check_userptr(vm, gpuva_to_vma(op->base.prefetch.va), pt_update); 1633 } 1634 break; 1635 #if IS_ENABLED(CONFIG_DRM_XE_GPUSVM) 1636 case DRM_GPUVA_OP_DRIVER: 1637 if (op->subop == XE_VMA_SUBOP_MAP_RANGE) { 1638 struct xe_svm_range *range = op->map_range.range; 1639 1640 xe_assert(vm->xe, xe_vma_is_cpu_addr_mirror(op->map_range.vma)); 1641 1642 xe_svm_range_debug(range, "PRE-COMMIT"); 1643 1644 if (!xe_svm_range_pages_valid(range)) { 1645 xe_svm_range_debug(range, "PRE-COMMIT - RETRY"); 1646 return -EAGAIN; 1647 } 1648 } 1649 break; 1650 #endif 1651 default: 1652 drm_warn(&vm->xe->drm, "NOT POSSIBLE"); 1653 } 1654 1655 return err; 1656 } 1657 1658 static int xe_pt_svm_userptr_pre_commit(struct xe_migrate_pt_update *pt_update) 1659 { 1660 struct xe_vm *vm = pt_update->vops->vm; 1661 struct xe_vma_ops *vops = pt_update->vops; 1662 struct xe_vm_pgtable_update_ops *pt_update_ops = 1663 &vops->pt_update_ops[pt_update->tile_id]; 1664 struct xe_vma_op *op; 1665 int err; 1666 1667 err = xe_pt_pre_commit(pt_update); 1668 if (err) 1669 return err; 1670 1671 xe_pt_svm_userptr_notifier_lock(vm); 1672 1673 list_for_each_entry(op, &vops->list, link) { 1674 err = op_check_svm_userptr(vm, op, pt_update_ops); 1675 if (err) { 1676 xe_pt_svm_userptr_notifier_unlock(vm); 1677 break; 1678 } 1679 } 1680 1681 return err; 1682 } 1683 #endif 1684 1685 struct xe_pt_stage_unbind_walk { 1686 /** @base: The pagewalk base-class. */ 1687 struct xe_pt_walk base; 1688 1689 /* Input parameters for the walk */ 1690 /** @tile: The tile we're unbinding from. */ 1691 struct xe_tile *tile; 1692 1693 /** 1694 * @modified_start: Walk range start, modified to include any 1695 * shared pagetables that we're the only user of and can thus 1696 * treat as private. 1697 */ 1698 u64 modified_start; 1699 /** @modified_end: Walk range start, modified like @modified_start. */ 1700 u64 modified_end; 1701 1702 /** @prl: Backing pointer to page reclaim list in pt_update_ops */ 1703 struct xe_page_reclaim_list *prl; 1704 1705 /* Output */ 1706 /* @wupd: Structure to track the page-table updates we're building */ 1707 struct xe_walk_update wupd; 1708 }; 1709 1710 /* 1711 * Check whether this range is the only one populating this pagetable, 1712 * and in that case, update the walk range checks so that higher levels don't 1713 * view us as a shared pagetable. 1714 */ 1715 static bool xe_pt_check_kill(u64 addr, u64 next, unsigned int level, 1716 const struct xe_pt *child, 1717 enum page_walk_action *action, 1718 struct xe_pt_walk *walk) 1719 { 1720 struct xe_pt_stage_unbind_walk *xe_walk = 1721 container_of(walk, typeof(*xe_walk), base); 1722 unsigned int shift = walk->shifts[level]; 1723 u64 size = 1ull << shift; 1724 1725 if (IS_ALIGNED(addr, size) && IS_ALIGNED(next, size) && 1726 ((next - addr) >> shift) == child->num_live) { 1727 u64 size = 1ull << walk->shifts[level + 1]; 1728 1729 *action = ACTION_CONTINUE; 1730 1731 if (xe_walk->modified_start >= addr) 1732 xe_walk->modified_start = round_down(addr, size); 1733 if (xe_walk->modified_end <= next) 1734 xe_walk->modified_end = round_up(next, size); 1735 1736 return true; 1737 } 1738 1739 return false; 1740 } 1741 1742 static int generate_reclaim_entry(struct xe_tile *tile, 1743 struct xe_page_reclaim_list *prl, 1744 u64 pte, struct xe_pt *xe_child) 1745 { 1746 struct xe_gt *gt = tile->primary_gt; 1747 struct xe_guc_page_reclaim_entry *reclaim_entries = prl->entries; 1748 bool is_2m = xe_child->level == 1 && (pte & XE_PDE_PS_2M); 1749 bool is_64k = xe_child->level == 0 && ((pte & XE_PTE_PS64) || xe_child->is_compact); 1750 u32 page_shift = is_2m ? ilog2(SZ_2M) : is_64k ? ilog2(SZ_64K) : ilog2(SZ_4K); 1751 /* Physical address bits start at page shift: 2M->[51:21], 64K->[51:16], 4K->[51:12] */ 1752 u64 phys_addr = pte & XE_PAGE_ADDR_MASK(page_shift); 1753 /* Page address is relative to 4K page regardless of entry level */ 1754 u64 phys_page = phys_addr >> XE_PTE_SHIFT; 1755 int num_entries = prl->num_entries; 1756 u32 reclamation_size = page_shift - XE_PTE_SHIFT; 1757 1758 xe_tile_assert(tile, xe_child->level <= MAX_HUGEPTE_LEVEL); 1759 xe_tile_assert(tile, reclaim_entries); 1760 xe_tile_assert(tile, num_entries < XE_PAGE_RECLAIM_MAX_ENTRIES - 1); 1761 1762 if (!xe_page_reclaim_list_valid(prl)) 1763 return -EINVAL; 1764 1765 /** 1766 * reclamation_size indicates the size of the page to be 1767 * invalidated and flushed from non-coherent cache. 1768 * Page size is computed as 2^(reclamation_size + XE_PTE_SHIFT) bytes. 1769 * Only 4K, 64K (level 0), and 2M pages are supported by hardware for page reclaim 1770 */ 1771 if (is_2m) { 1772 xe_gt_stats_incr(gt, XE_GT_STATS_ID_PRL_2M_ENTRY_COUNT, 1); 1773 } else if (is_64k) { 1774 xe_gt_stats_incr(gt, XE_GT_STATS_ID_PRL_64K_ENTRY_COUNT, 1); 1775 } else if (xe_child->level == 0) { 1776 xe_gt_stats_incr(gt, XE_GT_STATS_ID_PRL_4K_ENTRY_COUNT, 1); 1777 } else { 1778 xe_page_reclaim_list_abort(tile->primary_gt, prl, 1779 "unsupported PTE level=%u pte=%#llx", 1780 xe_child->level, pte); 1781 return -EINVAL; 1782 } 1783 1784 reclaim_entries[num_entries].qw = 1785 FIELD_PREP(XE_PAGE_RECLAIM_VALID, 1) | 1786 FIELD_PREP(XE_PAGE_RECLAIM_SIZE, reclamation_size) | 1787 FIELD_PREP(XE_PAGE_RECLAIM_ADDR_LO, phys_page) | 1788 FIELD_PREP(XE_PAGE_RECLAIM_ADDR_HI, phys_page >> 20); 1789 prl->num_entries++; 1790 vm_dbg(&tile_to_xe(tile)->drm, 1791 "PRL add entry: level=%u pte=%#llx reclamation_size=%u prl_idx=%d\n", 1792 xe_child->level, pte, reclamation_size, num_entries); 1793 1794 return 0; 1795 } 1796 1797 static int add_pte_to_prl(struct xe_tile *tile, struct xe_page_reclaim_list *prl, 1798 struct xe_pt *xe_child, u64 pte, u64 addr) 1799 { 1800 /* 1801 * In rare scenarios, pte may not be written yet due to racy conditions. 1802 * In such cases, invalidate the PRL and fallback to full PPC invalidation. 1803 */ 1804 if (!pte) { 1805 xe_page_reclaim_list_abort(tile->primary_gt, prl, 1806 "found zero pte at addr=%#llx", addr); 1807 return -EINVAL; 1808 } 1809 1810 /* Ensure it is a defined page */ 1811 xe_tile_assert(tile, xe_child->level == 0 || 1812 (pte & (XE_PDE_PS_2M | XE_PDPE_PS_1G))); 1813 1814 /* Account for NULL terminated entry on end (-1) */ 1815 if (prl->num_entries >= XE_PAGE_RECLAIM_MAX_ENTRIES - 1) { 1816 xe_page_reclaim_list_abort(tile->primary_gt, prl, 1817 "overflow while adding pte=%#llx", pte); 1818 return -ENOSPC; 1819 } 1820 1821 return generate_reclaim_entry(tile, prl, pte, xe_child); 1822 } 1823 1824 static bool add_compact_pt_prl(struct xe_tile *tile, struct xe_page_reclaim_list *prl, 1825 struct xe_device *xe, struct xe_pt *compact_pt, u64 addr) 1826 { 1827 struct iosys_map *map = &compact_pt->bo->vmap; 1828 1829 for (pgoff_t i = 0; i < SZ_2M / SZ_64K && xe_page_reclaim_list_valid(prl); i++) { 1830 u64 pte = xe_map_rd(xe, map, i * sizeof(u64), u64); 1831 1832 if (add_pte_to_prl(tile, prl, compact_pt, pte, addr + i * SZ_64K)) 1833 break; 1834 } 1835 1836 return xe_page_reclaim_list_valid(prl); 1837 } 1838 1839 static int xe_pt_stage_unbind_entry(struct xe_ptw *parent, pgoff_t offset, 1840 unsigned int level, u64 addr, u64 next, 1841 struct xe_ptw **child, 1842 enum page_walk_action *action, 1843 struct xe_pt_walk *walk) 1844 { 1845 struct xe_pt *xe_child = container_of(*child, typeof(*xe_child), base); 1846 struct xe_pt_stage_unbind_walk *xe_walk = 1847 container_of(walk, typeof(*xe_walk), base); 1848 struct xe_page_reclaim_list *prl = xe_walk->prl; 1849 struct xe_tile *tile = xe_walk->tile; 1850 struct xe_device *xe = tile_to_xe(tile); 1851 pgoff_t first = xe_pt_offset(addr, xe_child->level, walk); 1852 bool killed; 1853 1854 XE_WARN_ON(!*child); 1855 XE_WARN_ON(!level); 1856 /* Check for leaf node */ 1857 if (prl && xe_page_reclaim_list_valid(prl) && 1858 xe_child->level <= MAX_HUGEPTE_LEVEL) { 1859 struct iosys_map *leaf_map = &xe_child->bo->vmap; 1860 pgoff_t count = xe_pt_num_entries(addr, next, xe_child->level, walk); 1861 1862 for (pgoff_t i = 0; i < count; i++) { 1863 u64 pte; 1864 1865 /* 1866 * If not a leaf pt, skip unless non-leaf pt is interleaved between 1867 * leaf ptes which causes the page walk to skip over the child leaves 1868 */ 1869 if (xe_child->base.children && xe_child->base.children[first + i]) { 1870 u64 pt_size = 1ULL << walk->shifts[xe_child->level]; 1871 bool edge_pt = (i == 0 && !IS_ALIGNED(addr, pt_size)) || 1872 (i == count - 1 && !IS_ALIGNED(next, pt_size)); 1873 struct xe_pt *child_pt = 1874 container_of(xe_child->base.children[first + i], 1875 struct xe_pt, base); 1876 1877 /* Compact PTs always fill a full 2M-aligned slot, never an edge. */ 1878 XE_WARN_ON(child_pt->is_compact && edge_pt); 1879 if (edge_pt) 1880 continue; 1881 1882 /* Walker never descends into compact PTs, descend now */ 1883 if (child_pt->is_compact) { 1884 if (!add_compact_pt_prl(tile, prl, xe, child_pt, 1885 addr + (u64)i * pt_size)) 1886 break; 1887 } else { 1888 xe_page_reclaim_list_abort(tile->primary_gt, 1889 prl, 1890 "PT is skipped by walk at level=%u offset=%lu", 1891 xe_child->level, first + i); 1892 break; 1893 } 1894 continue; 1895 } 1896 1897 pte = xe_map_rd(xe, leaf_map, (first + i) * sizeof(u64), u64); 1898 1899 if (add_pte_to_prl(tile, prl, xe_child, pte, addr)) 1900 break; 1901 1902 /* An entry should be added for 64KB but contigious 4K have XE_PTE_PS64 */ 1903 if (pte & XE_PTE_PS64) 1904 i += 15; /* Skip other 15 consecutive 4K pages in the 64K page */ 1905 } 1906 } 1907 1908 killed = xe_pt_check_kill(addr, next, level - 1, xe_child, action, walk); 1909 1910 /* 1911 * Verify if any PTE are potentially dropped at non-leaf levels, either from being 1912 * killed or the page walk covers the region. 1913 */ 1914 if (prl && xe_page_reclaim_list_valid(prl) && 1915 xe_child->level > MAX_HUGEPTE_LEVEL && xe_child->num_live) { 1916 bool covered = xe_pt_covers(addr, next, xe_child->level, &xe_walk->base); 1917 1918 /* 1919 * If aborting page walk early (kill) or page walk completes the full range 1920 * we need to invalidate the PRL. 1921 */ 1922 if (killed || covered) 1923 xe_page_reclaim_list_abort(tile->primary_gt, prl, 1924 "kill at level=%u addr=%#llx next=%#llx num_live=%u", 1925 level, addr, next, xe_child->num_live); 1926 } 1927 1928 return 0; 1929 } 1930 1931 static int 1932 xe_pt_stage_unbind_post_descend(struct xe_ptw *parent, pgoff_t offset, 1933 unsigned int level, u64 addr, u64 next, 1934 struct xe_ptw **child, 1935 enum page_walk_action *action, 1936 struct xe_pt_walk *walk) 1937 { 1938 struct xe_pt_stage_unbind_walk *xe_walk = 1939 container_of(walk, typeof(*xe_walk), base); 1940 struct xe_pt *xe_child = container_of(*child, typeof(*xe_child), base); 1941 pgoff_t end_offset; 1942 u64 size = 1ull << walk->shifts[--level]; 1943 int err; 1944 1945 if (!IS_ALIGNED(addr, size)) 1946 addr = xe_walk->modified_start; 1947 if (!IS_ALIGNED(next, size)) 1948 next = xe_walk->modified_end; 1949 1950 /* Parent == *child is the root pt. Don't kill it. */ 1951 if (parent != *child && 1952 xe_pt_check_kill(addr, next, level, xe_child, action, walk)) 1953 return 0; 1954 1955 if (!xe_pt_nonshared_offsets(addr, next, level, walk, action, &offset, 1956 &end_offset)) 1957 return 0; 1958 1959 err = xe_pt_new_shared(&xe_walk->wupd, xe_child, offset, true); 1960 if (err) 1961 return err; 1962 1963 xe_walk->wupd.updates[level].update->qwords = end_offset - offset; 1964 1965 return 0; 1966 } 1967 1968 static const struct xe_pt_walk_ops xe_pt_stage_unbind_ops = { 1969 .pt_entry = xe_pt_stage_unbind_entry, 1970 .pt_post_descend = xe_pt_stage_unbind_post_descend, 1971 }; 1972 1973 /** 1974 * xe_pt_stage_unbind() - Build page-table update structures for an unbind 1975 * operation 1976 * @tile: The tile we're unbinding for. 1977 * @vm: The vm 1978 * @vma: The vma we're unbinding. 1979 * @range: The range we're unbinding. 1980 * @entries: Caller-provided storage for the update structures. 1981 * 1982 * Builds page-table update structures for an unbind operation. The function 1983 * will attempt to remove all page-tables that we're the only user 1984 * of, and for that to work, the unbind operation must be committed in the 1985 * same critical section that blocks racing binds to the same page-table tree. 1986 * 1987 * Return: The number of entries used. 1988 */ 1989 static unsigned int xe_pt_stage_unbind(struct xe_tile *tile, 1990 struct xe_vm *vm, 1991 struct xe_vma *vma, 1992 struct xe_svm_range *range, 1993 struct xe_vm_pgtable_update *entries) 1994 { 1995 u64 start = range ? xe_svm_range_start(range) : xe_vma_start(vma); 1996 u64 end = range ? xe_svm_range_end(range) : xe_vma_end(vma); 1997 struct xe_vm_pgtable_update_op *pt_update_op = 1998 container_of(entries, struct xe_vm_pgtable_update_op, entries[0]); 1999 struct xe_pt_stage_unbind_walk xe_walk = { 2000 .base = { 2001 .ops = &xe_pt_stage_unbind_ops, 2002 .shifts = xe_normal_pt_shifts, 2003 .max_level = XE_PT_HIGHEST_LEVEL, 2004 .staging = true, 2005 }, 2006 .tile = tile, 2007 .modified_start = start, 2008 .modified_end = end, 2009 .wupd.entries = entries, 2010 .prl = pt_update_op->prl, 2011 }; 2012 struct xe_pt *pt = vm->pt_root[tile->id]; 2013 2014 (void)xe_pt_walk_shared(&pt->base, pt->level, start, end, 2015 &xe_walk.base); 2016 2017 return xe_walk.wupd.num_used_entries; 2018 } 2019 2020 static void 2021 xe_migrate_clear_pgtable_callback(struct xe_migrate_pt_update *pt_update, 2022 struct xe_tile *tile, struct iosys_map *map, 2023 void *ptr, u32 qword_ofs, u32 num_qwords, 2024 const struct xe_vm_pgtable_update *update) 2025 { 2026 struct xe_vm *vm = pt_update->vops->vm; 2027 u64 empty = __xe_pt_empty_pte(tile, vm, update->pt->level); 2028 int i; 2029 2030 if (map && map->is_iomem) 2031 for (i = 0; i < num_qwords; ++i) 2032 xe_map_wr(tile_to_xe(tile), map, (qword_ofs + i) * 2033 sizeof(u64), u64, empty); 2034 else if (map) 2035 memset64(map->vaddr + qword_ofs * sizeof(u64), empty, 2036 num_qwords); 2037 else 2038 memset64(ptr, empty, num_qwords); 2039 } 2040 2041 static void xe_pt_abort_unbind(struct xe_vma *vma, 2042 struct xe_vm_pgtable_update *entries, 2043 u32 num_entries) 2044 { 2045 int i, j; 2046 2047 xe_pt_commit_prepare_locks_assert(vma); 2048 2049 for (i = num_entries - 1; i >= 0; --i) { 2050 struct xe_vm_pgtable_update *entry = &entries[i]; 2051 struct xe_pt *pt = entry->pt; 2052 struct xe_pt_dir *pt_dir = as_xe_pt_dir(pt); 2053 2054 pt->num_live += entry->qwords; 2055 2056 if (!pt->level) 2057 continue; 2058 2059 for (j = entry->ofs; j < entry->ofs + entry->qwords; j++) 2060 pt_dir->staging[j] = 2061 entries[i].pt_entries[j - entry->ofs].pt ? 2062 &entries[i].pt_entries[j - entry->ofs].pt->base : NULL; 2063 } 2064 } 2065 2066 static void 2067 xe_pt_commit_prepare_unbind(struct xe_vma *vma, 2068 struct xe_vm_pgtable_update *entries, 2069 u32 num_entries) 2070 { 2071 int i, j; 2072 2073 xe_pt_commit_prepare_locks_assert(vma); 2074 2075 for (i = 0; i < num_entries; ++i) { 2076 struct xe_vm_pgtable_update *entry = &entries[i]; 2077 struct xe_pt *pt = entry->pt; 2078 struct xe_pt_dir *pt_dir; 2079 2080 pt->num_live -= entry->qwords; 2081 if (!pt->level) 2082 continue; 2083 2084 pt_dir = as_xe_pt_dir(pt); 2085 for (j = entry->ofs; j < entry->ofs + entry->qwords; j++) { 2086 entry->pt_entries[j - entry->ofs].pt = 2087 xe_pt_entry_staging(pt_dir, j); 2088 pt_dir->staging[j] = NULL; 2089 } 2090 } 2091 } 2092 2093 static void 2094 xe_pt_update_ops_rfence_interval(struct xe_vm_pgtable_update_ops *pt_update_ops, 2095 u64 start, u64 end) 2096 { 2097 u64 last; 2098 u32 current_op = pt_update_ops->current_op; 2099 struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[current_op]; 2100 int i, level = 0; 2101 2102 for (i = 0; i < pt_op->num_entries; i++) { 2103 const struct xe_vm_pgtable_update *entry = &pt_op->entries[i]; 2104 2105 if (entry->pt->level > level) 2106 level = entry->pt->level; 2107 } 2108 2109 /* Greedy (non-optimal) calculation but simple */ 2110 start = ALIGN_DOWN(start, 0x1ull << xe_pt_shift(level)); 2111 last = ALIGN(end, 0x1ull << xe_pt_shift(level)) - 1; 2112 2113 if (start < pt_update_ops->start) 2114 pt_update_ops->start = start; 2115 if (last > pt_update_ops->last) 2116 pt_update_ops->last = last; 2117 } 2118 2119 static int vma_reserve_fences(struct xe_device *xe, struct xe_vma *vma) 2120 { 2121 int shift = xe_device_get_root_tile(xe)->media_gt ? 1 : 0; 2122 2123 if (!xe_vma_has_no_bo(vma) && !xe_vma_bo(vma)->vm) 2124 return dma_resv_reserve_fences(xe_vma_bo(vma)->ttm.base.resv, 2125 xe->info.tile_count << shift); 2126 2127 return 0; 2128 } 2129 2130 static int bind_op_prepare(struct xe_vm *vm, struct xe_tile *tile, 2131 struct xe_vm_pgtable_update_ops *pt_update_ops, 2132 struct xe_vma *vma, bool invalidate_on_bind) 2133 { 2134 u32 current_op = pt_update_ops->current_op; 2135 struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[current_op]; 2136 int err; 2137 2138 xe_tile_assert(tile, !xe_vma_is_cpu_addr_mirror(vma)); 2139 xe_bo_assert_held(xe_vma_bo(vma)); 2140 2141 vm_dbg(&xe_vma_vm(vma)->xe->drm, 2142 "Preparing bind, with range [%llx...%llx)\n", 2143 xe_vma_start(vma), xe_vma_end(vma) - 1); 2144 2145 pt_op->vma = NULL; 2146 pt_op->bind = true; 2147 pt_op->rebind = BIT(tile->id) & vma->tile_present; 2148 2149 err = vma_reserve_fences(tile_to_xe(tile), vma); 2150 if (err) 2151 return err; 2152 2153 err = xe_pt_prepare_bind(tile, vma, NULL, pt_op->entries, 2154 &pt_op->num_entries, invalidate_on_bind); 2155 if (!err) { 2156 xe_tile_assert(tile, pt_op->num_entries <= 2157 ARRAY_SIZE(pt_op->entries)); 2158 xe_vm_dbg_print_entries(tile_to_xe(tile), pt_op->entries, 2159 pt_op->num_entries, true); 2160 2161 xe_pt_update_ops_rfence_interval(pt_update_ops, 2162 xe_vma_start(vma), 2163 xe_vma_end(vma)); 2164 ++pt_update_ops->current_op; 2165 pt_update_ops->needs_svm_lock |= xe_vma_is_userptr(vma); 2166 2167 /* 2168 * If rebind, we have to invalidate TLB on !LR vms to invalidate 2169 * cached PTEs point to freed memory. On LR vms this is done 2170 * automatically when the context is re-enabled by the rebind worker, 2171 * or in fault mode it was invalidated on PTE zapping. 2172 * 2173 * If rebind, we have to invalidate TLB on context based TLB invalidation 2174 * LR vms, as they cannot be relied on context re-enable. 2175 * 2176 * If !rebind, and scratch enabled VMs, there is a chance the scratch 2177 * PTE is already cached in the TLB so it needs to be invalidated. 2178 * On !LR VMs this is done in the ring ops preceding a batch, but on 2179 * LR, in particular on user-space batch buffer chaining, it needs to 2180 * be done here. 2181 */ 2182 if ((!pt_op->rebind && xe_vm_has_scratch(vm) && 2183 xe_vm_in_lr_mode(vm))) 2184 pt_update_ops->needs_invalidation = true; 2185 else if (pt_op->rebind && xe_vm_in_preempt_fence_mode(vm) && 2186 vm->xe->info.has_ctx_tlb_inval) 2187 pt_update_ops->needs_invalidation = true; 2188 else if (pt_op->rebind && !xe_vm_in_lr_mode(vm)) 2189 /* We bump also if batch_invalidate_tlb is true */ 2190 vm->tlb_flush_seqno++; 2191 2192 vma->tile_staged |= BIT(tile->id); 2193 pt_op->vma = vma; 2194 xe_pt_commit_prepare_bind(vma, pt_op->entries, 2195 pt_op->num_entries, pt_op->rebind); 2196 } else { 2197 xe_pt_cancel_bind(vma, pt_op->entries, pt_op->num_entries); 2198 } 2199 2200 return err; 2201 } 2202 2203 static int bind_range_prepare(struct xe_vm *vm, struct xe_tile *tile, 2204 struct xe_vm_pgtable_update_ops *pt_update_ops, 2205 struct xe_vma *vma, struct xe_svm_range *range) 2206 { 2207 u32 current_op = pt_update_ops->current_op; 2208 struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[current_op]; 2209 int err; 2210 2211 xe_tile_assert(tile, xe_vma_is_cpu_addr_mirror(vma)); 2212 2213 vm_dbg(&xe_vma_vm(vma)->xe->drm, 2214 "Preparing bind, with range [%lx...%lx)\n", 2215 xe_svm_range_start(range), xe_svm_range_end(range) - 1); 2216 2217 pt_op->vma = NULL; 2218 pt_op->bind = true; 2219 pt_op->rebind = BIT(tile->id) & range->tile_present; 2220 2221 err = xe_pt_prepare_bind(tile, vma, range, pt_op->entries, 2222 &pt_op->num_entries, false); 2223 if (!err) { 2224 xe_tile_assert(tile, pt_op->num_entries <= 2225 ARRAY_SIZE(pt_op->entries)); 2226 xe_vm_dbg_print_entries(tile_to_xe(tile), pt_op->entries, 2227 pt_op->num_entries, true); 2228 2229 xe_pt_update_ops_rfence_interval(pt_update_ops, 2230 xe_svm_range_start(range), 2231 xe_svm_range_end(range)); 2232 ++pt_update_ops->current_op; 2233 pt_update_ops->needs_svm_lock = true; 2234 2235 pt_op->vma = vma; 2236 xe_pt_commit_prepare_bind(vma, pt_op->entries, 2237 pt_op->num_entries, pt_op->rebind); 2238 } else { 2239 xe_pt_cancel_bind(vma, pt_op->entries, pt_op->num_entries); 2240 } 2241 2242 return err; 2243 } 2244 2245 static int unbind_op_prepare(struct xe_tile *tile, 2246 struct xe_vm_pgtable_update_ops *pt_update_ops, 2247 struct xe_vma *vma) 2248 { 2249 struct xe_device *xe = tile_to_xe(tile); 2250 u32 current_op = pt_update_ops->current_op; 2251 struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[current_op]; 2252 int err; 2253 2254 if (!((vma->tile_present | vma->tile_staged) & BIT(tile->id))) 2255 return 0; 2256 2257 xe_tile_assert(tile, !xe_vma_is_cpu_addr_mirror(vma)); 2258 xe_bo_assert_held(xe_vma_bo(vma)); 2259 2260 vm_dbg(&xe_vma_vm(vma)->xe->drm, 2261 "Preparing unbind, with range [%llx...%llx)\n", 2262 xe_vma_start(vma), xe_vma_end(vma) - 1); 2263 2264 pt_op->vma = vma; 2265 pt_op->bind = false; 2266 pt_op->rebind = false; 2267 /* 2268 * Maintain one PRL located in pt_update_ops that all others in unbind op reference. 2269 * Ensure that PRL is allocated only once, and if invalidated, remains an invalidated PRL. 2270 */ 2271 if (xe->info.has_page_reclaim_hw_assist && 2272 xe_page_reclaim_list_is_new(&pt_update_ops->prl)) 2273 xe_page_reclaim_list_alloc_entries(&pt_update_ops->prl); 2274 2275 /* Page reclaim may not be needed due to other features, so skip the corresponding VMA */ 2276 pt_op->prl = (xe_page_reclaim_list_valid(&pt_update_ops->prl) && 2277 !xe_page_reclaim_skip(tile, vma)) ? &pt_update_ops->prl : NULL; 2278 2279 err = vma_reserve_fences(tile_to_xe(tile), vma); 2280 if (err) 2281 return err; 2282 2283 pt_op->num_entries = xe_pt_stage_unbind(tile, xe_vma_vm(vma), 2284 vma, NULL, pt_op->entries); 2285 2286 xe_vm_dbg_print_entries(tile_to_xe(tile), pt_op->entries, 2287 pt_op->num_entries, false); 2288 xe_pt_update_ops_rfence_interval(pt_update_ops, xe_vma_start(vma), 2289 xe_vma_end(vma)); 2290 ++pt_update_ops->current_op; 2291 pt_update_ops->needs_svm_lock |= xe_vma_is_userptr(vma); 2292 pt_update_ops->needs_invalidation = true; 2293 2294 xe_pt_commit_prepare_unbind(vma, pt_op->entries, pt_op->num_entries); 2295 2296 return 0; 2297 } 2298 2299 static bool 2300 xe_pt_op_check_range_skip_invalidation(struct xe_vm_pgtable_update_op *pt_op, 2301 struct xe_svm_range *range) 2302 { 2303 struct xe_vm_pgtable_update *update = pt_op->entries; 2304 2305 XE_WARN_ON(!pt_op->num_entries); 2306 2307 /* 2308 * We can't skip the invalidation if we are removing PTEs that span more 2309 * than the range, do some checks to ensure we are removing PTEs that 2310 * are invalid. 2311 */ 2312 2313 if (pt_op->num_entries > 1) 2314 return false; 2315 2316 if (update->pt->level == 0) 2317 return true; 2318 2319 if (update->pt->level == 1) 2320 return xe_svm_range_size(range) >= SZ_2M; 2321 2322 return false; 2323 } 2324 2325 static int unbind_range_prepare(struct xe_vm *vm, 2326 struct xe_tile *tile, 2327 struct xe_vm_pgtable_update_ops *pt_update_ops, 2328 struct xe_svm_range *range) 2329 { 2330 u32 current_op = pt_update_ops->current_op; 2331 struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[current_op]; 2332 2333 if (!(range->tile_present & BIT(tile->id))) 2334 return 0; 2335 2336 vm_dbg(&vm->xe->drm, 2337 "Preparing unbind, with range [%lx...%lx)\n", 2338 xe_svm_range_start(range), xe_svm_range_end(range) - 1); 2339 2340 pt_op->vma = XE_INVALID_VMA; 2341 pt_op->bind = false; 2342 pt_op->rebind = false; 2343 pt_op->prl = NULL; 2344 2345 pt_op->num_entries = xe_pt_stage_unbind(tile, vm, NULL, range, 2346 pt_op->entries); 2347 2348 xe_vm_dbg_print_entries(tile_to_xe(tile), pt_op->entries, 2349 pt_op->num_entries, false); 2350 xe_pt_update_ops_rfence_interval(pt_update_ops, xe_svm_range_start(range), 2351 xe_svm_range_end(range)); 2352 ++pt_update_ops->current_op; 2353 pt_update_ops->needs_svm_lock = true; 2354 pt_update_ops->needs_invalidation |= xe_vm_has_scratch(vm) || 2355 xe_vm_has_valid_gpu_mapping(tile, range->tile_present, 2356 range->tile_invalidated) || 2357 !xe_pt_op_check_range_skip_invalidation(pt_op, range); 2358 2359 xe_pt_commit_prepare_unbind(XE_INVALID_VMA, pt_op->entries, 2360 pt_op->num_entries); 2361 2362 return 0; 2363 } 2364 2365 static int op_prepare(struct xe_vm *vm, 2366 struct xe_tile *tile, 2367 struct xe_vm_pgtable_update_ops *pt_update_ops, 2368 struct xe_vma_op *op) 2369 { 2370 int err = 0; 2371 2372 xe_vm_assert_held(vm); 2373 2374 switch (op->base.op) { 2375 case DRM_GPUVA_OP_MAP: 2376 if ((!op->map.immediate && xe_vm_in_fault_mode(vm) && 2377 !op->map.invalidate_on_bind) || 2378 (op->map.vma_flags & XE_VMA_SYSTEM_ALLOCATOR)) 2379 break; 2380 2381 err = bind_op_prepare(vm, tile, pt_update_ops, op->map.vma, 2382 op->map.invalidate_on_bind); 2383 pt_update_ops->wait_vm_kernel = true; 2384 break; 2385 case DRM_GPUVA_OP_REMAP: 2386 { 2387 struct xe_vma *old = gpuva_to_vma(op->base.remap.unmap->va); 2388 2389 if (xe_vma_is_cpu_addr_mirror(old)) 2390 break; 2391 2392 err = unbind_op_prepare(tile, pt_update_ops, old); 2393 2394 if (!err && op->remap.prev && !op->remap.skip_prev) { 2395 err = bind_op_prepare(vm, tile, pt_update_ops, 2396 op->remap.prev, false); 2397 pt_update_ops->wait_vm_bookkeep = true; 2398 } 2399 if (!err && op->remap.next && !op->remap.skip_next) { 2400 err = bind_op_prepare(vm, tile, pt_update_ops, 2401 op->remap.next, false); 2402 pt_update_ops->wait_vm_bookkeep = true; 2403 } 2404 break; 2405 } 2406 case DRM_GPUVA_OP_UNMAP: 2407 { 2408 struct xe_vma *vma = gpuva_to_vma(op->base.unmap.va); 2409 2410 if (xe_vma_is_cpu_addr_mirror(vma)) 2411 break; 2412 2413 err = unbind_op_prepare(tile, pt_update_ops, vma); 2414 break; 2415 } 2416 case DRM_GPUVA_OP_PREFETCH: 2417 { 2418 struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va); 2419 2420 if (xe_vma_is_cpu_addr_mirror(vma)) { 2421 struct xe_svm_range *range; 2422 unsigned long i; 2423 2424 xa_for_each(&op->prefetch_range.range, i, range) { 2425 err = bind_range_prepare(vm, tile, pt_update_ops, 2426 vma, range); 2427 /* 2428 * Don't tell user space to retry, rather let 2429 * page faults fixup the pages. 2430 */ 2431 if (err == -EAGAIN) 2432 err = -ENODATA; 2433 if (err) 2434 return err; 2435 } 2436 } else { 2437 err = bind_op_prepare(vm, tile, pt_update_ops, vma, false); 2438 pt_update_ops->wait_vm_kernel = true; 2439 } 2440 break; 2441 } 2442 case DRM_GPUVA_OP_DRIVER: 2443 if (op->subop == XE_VMA_SUBOP_MAP_RANGE) { 2444 xe_assert(vm->xe, xe_vma_is_cpu_addr_mirror(op->map_range.vma)); 2445 2446 err = bind_range_prepare(vm, tile, pt_update_ops, 2447 op->map_range.vma, 2448 op->map_range.range); 2449 } else if (op->subop == XE_VMA_SUBOP_UNMAP_RANGE) { 2450 err = unbind_range_prepare(vm, tile, pt_update_ops, 2451 op->unmap_range.range); 2452 } 2453 break; 2454 default: 2455 drm_warn(&vm->xe->drm, "NOT POSSIBLE"); 2456 } 2457 2458 return err; 2459 } 2460 2461 static void 2462 xe_pt_update_ops_init(struct xe_vm_pgtable_update_ops *pt_update_ops) 2463 { 2464 init_llist_head(&pt_update_ops->deferred); 2465 pt_update_ops->current_op = 0; 2466 pt_update_ops->start = ~0x0ull; 2467 pt_update_ops->last = 0x0ull; 2468 pt_update_ops->needs_svm_lock = false; 2469 pt_update_ops->needs_invalidation = false; 2470 xe_page_reclaim_list_init(&pt_update_ops->prl); 2471 } 2472 2473 /** 2474 * xe_pt_update_ops_prepare() - Prepare PT update operations 2475 * @tile: Tile of PT update operations 2476 * @vops: VMA operationa 2477 * 2478 * Prepare PT update operations which includes updating internal PT state, 2479 * allocate memory for page tables, populate page table being pruned in, and 2480 * create PT update operations for leaf insertion / removal. 2481 * 2482 * Return: 0 on success, negative error code on error. 2483 */ 2484 int xe_pt_update_ops_prepare(struct xe_tile *tile, struct xe_vma_ops *vops) 2485 { 2486 struct xe_vm_pgtable_update_ops *pt_update_ops = 2487 &vops->pt_update_ops[tile->id]; 2488 struct xe_vma_op *op; 2489 int shift = tile->media_gt ? 1 : 0; 2490 int err; 2491 2492 lockdep_assert_held(&vops->vm->lock); 2493 xe_vm_assert_held(vops->vm); 2494 2495 xe_pt_update_ops_init(pt_update_ops); 2496 2497 err = dma_resv_reserve_fences(xe_vm_resv(vops->vm), 2498 tile_to_xe(tile)->info.tile_count << shift); 2499 if (err) 2500 return err; 2501 2502 list_for_each_entry(op, &vops->list, link) { 2503 err = op_prepare(vops->vm, tile, pt_update_ops, op); 2504 2505 if (err) 2506 return err; 2507 } 2508 2509 xe_tile_assert(tile, pt_update_ops->current_op <= 2510 pt_update_ops->num_ops); 2511 2512 #ifdef TEST_VM_OPS_ERROR 2513 if (vops->inject_error && 2514 vops->vm->xe->vm_inject_error_position == FORCE_OP_ERROR_PREPARE) 2515 return -ENOSPC; 2516 #endif 2517 2518 return 0; 2519 } 2520 ALLOW_ERROR_INJECTION(xe_pt_update_ops_prepare, ERRNO); 2521 2522 static void bind_op_commit(struct xe_vm *vm, struct xe_tile *tile, 2523 struct xe_vm_pgtable_update_ops *pt_update_ops, 2524 struct xe_vma *vma, struct dma_fence *fence, 2525 struct dma_fence *fence2, bool invalidate_on_bind) 2526 { 2527 xe_tile_assert(tile, !xe_vma_is_cpu_addr_mirror(vma)); 2528 2529 if (!xe_vma_has_no_bo(vma) && !xe_vma_bo(vma)->vm) { 2530 dma_resv_add_fence(xe_vma_bo(vma)->ttm.base.resv, fence, 2531 pt_update_ops->wait_vm_bookkeep ? 2532 DMA_RESV_USAGE_KERNEL : 2533 DMA_RESV_USAGE_BOOKKEEP); 2534 if (fence2) 2535 dma_resv_add_fence(xe_vma_bo(vma)->ttm.base.resv, fence2, 2536 pt_update_ops->wait_vm_bookkeep ? 2537 DMA_RESV_USAGE_KERNEL : 2538 DMA_RESV_USAGE_BOOKKEEP); 2539 } 2540 /* All WRITE_ONCE pair with READ_ONCE in xe_vm_has_valid_gpu_mapping() */ 2541 WRITE_ONCE(vma->tile_present, vma->tile_present | BIT(tile->id)); 2542 if (invalidate_on_bind) 2543 WRITE_ONCE(vma->tile_invalidated, 2544 vma->tile_invalidated | BIT(tile->id)); 2545 else 2546 WRITE_ONCE(vma->tile_invalidated, 2547 vma->tile_invalidated & ~BIT(tile->id)); 2548 vma->tile_staged &= ~BIT(tile->id); 2549 if (xe_vma_is_userptr(vma)) { 2550 xe_svm_assert_held_read_or_inject_write(vm); 2551 to_userptr_vma(vma)->userptr.initial_bind = true; 2552 } 2553 2554 /* 2555 * Kick rebind worker if this bind triggers preempt fences and not in 2556 * the rebind worker 2557 */ 2558 if (pt_update_ops->wait_vm_bookkeep && 2559 xe_vm_in_preempt_fence_mode(vm) && 2560 !current->mm) 2561 xe_vm_queue_rebind_worker(vm); 2562 } 2563 2564 static void unbind_op_commit(struct xe_vm *vm, struct xe_tile *tile, 2565 struct xe_vm_pgtable_update_ops *pt_update_ops, 2566 struct xe_vma *vma, struct dma_fence *fence, 2567 struct dma_fence *fence2) 2568 { 2569 xe_tile_assert(tile, !xe_vma_is_cpu_addr_mirror(vma)); 2570 2571 if (!xe_vma_has_no_bo(vma) && !xe_vma_bo(vma)->vm) { 2572 dma_resv_add_fence(xe_vma_bo(vma)->ttm.base.resv, fence, 2573 pt_update_ops->wait_vm_bookkeep ? 2574 DMA_RESV_USAGE_KERNEL : 2575 DMA_RESV_USAGE_BOOKKEEP); 2576 if (fence2) 2577 dma_resv_add_fence(xe_vma_bo(vma)->ttm.base.resv, fence2, 2578 pt_update_ops->wait_vm_bookkeep ? 2579 DMA_RESV_USAGE_KERNEL : 2580 DMA_RESV_USAGE_BOOKKEEP); 2581 } 2582 vma->tile_present &= ~BIT(tile->id); 2583 if (!vma->tile_present) { 2584 list_del_init(&vma->combined_links.rebind); 2585 if (xe_vma_is_userptr(vma)) { 2586 xe_svm_assert_held_read_or_inject_write(vm); 2587 2588 spin_lock(&vm->userptr.invalidated_lock); 2589 list_del_init(&to_userptr_vma(vma)->userptr.invalidate_link); 2590 spin_unlock(&vm->userptr.invalidated_lock); 2591 } 2592 } 2593 } 2594 2595 static void range_present_and_invalidated_tile(struct xe_vm *vm, 2596 struct xe_svm_range *range, 2597 u8 tile_id) 2598 { 2599 /* All WRITE_ONCE pair with READ_ONCE in xe_vm_has_valid_gpu_mapping() */ 2600 2601 lockdep_assert_held(&vm->svm.gpusvm.notifier_lock); 2602 2603 WRITE_ONCE(range->tile_present, range->tile_present | BIT(tile_id)); 2604 WRITE_ONCE(range->tile_invalidated, range->tile_invalidated & ~BIT(tile_id)); 2605 } 2606 2607 static void op_commit(struct xe_vm *vm, 2608 struct xe_tile *tile, 2609 struct xe_vm_pgtable_update_ops *pt_update_ops, 2610 struct xe_vma_op *op, struct dma_fence *fence, 2611 struct dma_fence *fence2) 2612 { 2613 xe_vm_assert_held(vm); 2614 2615 switch (op->base.op) { 2616 case DRM_GPUVA_OP_MAP: 2617 if ((!op->map.immediate && xe_vm_in_fault_mode(vm)) || 2618 (op->map.vma_flags & XE_VMA_SYSTEM_ALLOCATOR)) 2619 break; 2620 2621 bind_op_commit(vm, tile, pt_update_ops, op->map.vma, fence, 2622 fence2, op->map.invalidate_on_bind); 2623 break; 2624 case DRM_GPUVA_OP_REMAP: 2625 { 2626 struct xe_vma *old = gpuva_to_vma(op->base.remap.unmap->va); 2627 2628 if (xe_vma_is_cpu_addr_mirror(old)) 2629 break; 2630 2631 unbind_op_commit(vm, tile, pt_update_ops, old, fence, fence2); 2632 2633 if (op->remap.prev && !op->remap.skip_prev) 2634 bind_op_commit(vm, tile, pt_update_ops, op->remap.prev, 2635 fence, fence2, false); 2636 if (op->remap.next && !op->remap.skip_next) 2637 bind_op_commit(vm, tile, pt_update_ops, op->remap.next, 2638 fence, fence2, false); 2639 break; 2640 } 2641 case DRM_GPUVA_OP_UNMAP: 2642 { 2643 struct xe_vma *vma = gpuva_to_vma(op->base.unmap.va); 2644 2645 if (!xe_vma_is_cpu_addr_mirror(vma)) 2646 unbind_op_commit(vm, tile, pt_update_ops, vma, fence, 2647 fence2); 2648 break; 2649 } 2650 case DRM_GPUVA_OP_PREFETCH: 2651 { 2652 struct xe_vma *vma = gpuva_to_vma(op->base.prefetch.va); 2653 2654 if (xe_vma_is_cpu_addr_mirror(vma)) { 2655 struct xe_svm_range *range = NULL; 2656 unsigned long i; 2657 2658 xa_for_each(&op->prefetch_range.range, i, range) 2659 range_present_and_invalidated_tile(vm, range, tile->id); 2660 } else { 2661 bind_op_commit(vm, tile, pt_update_ops, vma, fence, 2662 fence2, false); 2663 } 2664 break; 2665 } 2666 case DRM_GPUVA_OP_DRIVER: 2667 { 2668 /* WRITE_ONCE pairs with READ_ONCE in xe_vm_has_valid_gpu_mapping() */ 2669 if (op->subop == XE_VMA_SUBOP_MAP_RANGE) 2670 range_present_and_invalidated_tile(vm, op->map_range.range, tile->id); 2671 else if (op->subop == XE_VMA_SUBOP_UNMAP_RANGE) 2672 WRITE_ONCE(op->unmap_range.range->tile_present, 2673 op->unmap_range.range->tile_present & 2674 ~BIT(tile->id)); 2675 2676 break; 2677 } 2678 default: 2679 drm_warn(&vm->xe->drm, "NOT POSSIBLE"); 2680 } 2681 } 2682 2683 static const struct xe_migrate_pt_update_ops migrate_ops = { 2684 .populate = xe_vm_populate_pgtable, 2685 .clear = xe_migrate_clear_pgtable_callback, 2686 .pre_commit = xe_pt_pre_commit, 2687 }; 2688 2689 #if IS_ENABLED(CONFIG_DRM_GPUSVM) 2690 static const struct xe_migrate_pt_update_ops svm_userptr_migrate_ops = { 2691 .populate = xe_vm_populate_pgtable, 2692 .clear = xe_migrate_clear_pgtable_callback, 2693 .pre_commit = xe_pt_svm_userptr_pre_commit, 2694 }; 2695 #else 2696 static const struct xe_migrate_pt_update_ops svm_userptr_migrate_ops; 2697 #endif 2698 2699 static struct xe_dep_scheduler *to_dep_scheduler(struct xe_exec_queue *q, 2700 struct xe_gt *gt) 2701 { 2702 if (xe_gt_is_media_type(gt)) 2703 return q->tlb_inval[XE_EXEC_QUEUE_TLB_INVAL_MEDIA_GT].dep_scheduler; 2704 2705 return q->tlb_inval[XE_EXEC_QUEUE_TLB_INVAL_PRIMARY_GT].dep_scheduler; 2706 } 2707 2708 /** 2709 * xe_pt_update_ops_run() - Run PT update operations 2710 * @tile: Tile of PT update operations 2711 * @vops: VMA operationa 2712 * 2713 * Run PT update operations which includes committing internal PT state changes, 2714 * creating job for PT update operations for leaf insertion / removal, and 2715 * installing job fence in various places. 2716 * 2717 * Return: fence on success, negative ERR_PTR on error. 2718 */ 2719 struct dma_fence * 2720 xe_pt_update_ops_run(struct xe_tile *tile, struct xe_vma_ops *vops) 2721 { 2722 struct xe_vm *vm = vops->vm; 2723 struct xe_vm_pgtable_update_ops *pt_update_ops = 2724 &vops->pt_update_ops[tile->id]; 2725 struct xe_exec_queue *q = pt_update_ops->q; 2726 struct dma_fence *fence, *ifence = NULL, *mfence = NULL; 2727 struct xe_tlb_inval_job *ijob = NULL, *mjob = NULL; 2728 struct xe_range_fence *rfence; 2729 struct xe_vma_op *op; 2730 int err = 0, i; 2731 struct xe_migrate_pt_update update = { 2732 .ops = pt_update_ops->needs_svm_lock ? 2733 &svm_userptr_migrate_ops : 2734 &migrate_ops, 2735 .vops = vops, 2736 .tile_id = tile->id, 2737 }; 2738 2739 lockdep_assert_held(&vm->lock); 2740 xe_vm_assert_held(vm); 2741 2742 if (!pt_update_ops->current_op) { 2743 xe_tile_assert(tile, xe_vm_in_fault_mode(vm)); 2744 2745 return dma_fence_get_stub(); 2746 } 2747 2748 #ifdef TEST_VM_OPS_ERROR 2749 if (vops->inject_error && 2750 vm->xe->vm_inject_error_position == FORCE_OP_ERROR_RUN) 2751 return ERR_PTR(-ENOSPC); 2752 #endif 2753 2754 if (pt_update_ops->needs_invalidation) { 2755 struct xe_dep_scheduler *dep_scheduler = 2756 to_dep_scheduler(q, tile->primary_gt); 2757 2758 ijob = xe_tlb_inval_job_create(q, &tile->primary_gt->tlb_inval, 2759 dep_scheduler, vm, 2760 pt_update_ops->start, 2761 pt_update_ops->last, 2762 XE_EXEC_QUEUE_TLB_INVAL_PRIMARY_GT); 2763 if (IS_ERR(ijob)) { 2764 err = PTR_ERR(ijob); 2765 goto kill_vm_tile1; 2766 } 2767 update.ijob = ijob; 2768 /* 2769 * Only add page reclaim for the primary GT. Media GT does not have 2770 * any PPC to flush, so enabling the PPC flush bit for media is 2771 * effectively a NOP and provides no performance benefit nor 2772 * interfere with primary GT. 2773 */ 2774 if (xe_page_reclaim_list_valid(&pt_update_ops->prl)) { 2775 xe_tlb_inval_job_add_page_reclaim(ijob, &pt_update_ops->prl); 2776 /* Release ref from alloc, job will now handle it */ 2777 xe_page_reclaim_list_invalidate(&pt_update_ops->prl); 2778 } 2779 2780 if (tile->media_gt) { 2781 dep_scheduler = to_dep_scheduler(q, tile->media_gt); 2782 2783 mjob = xe_tlb_inval_job_create(q, 2784 &tile->media_gt->tlb_inval, 2785 dep_scheduler, vm, 2786 pt_update_ops->start, 2787 pt_update_ops->last, 2788 XE_EXEC_QUEUE_TLB_INVAL_MEDIA_GT); 2789 if (IS_ERR(mjob)) { 2790 err = PTR_ERR(mjob); 2791 goto free_ijob; 2792 } 2793 update.mjob = mjob; 2794 } 2795 } 2796 2797 rfence = kzalloc_obj(*rfence); 2798 if (!rfence) { 2799 err = -ENOMEM; 2800 goto free_ijob; 2801 } 2802 2803 fence = xe_migrate_update_pgtables(tile->migrate, &update); 2804 if (IS_ERR(fence)) { 2805 err = PTR_ERR(fence); 2806 goto free_rfence; 2807 } 2808 2809 /* Point of no return - VM killed if failure after this */ 2810 for (i = 0; i < pt_update_ops->current_op; ++i) { 2811 struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[i]; 2812 2813 xe_pt_commit(pt_op->vma, pt_op->entries, 2814 pt_op->num_entries, &pt_update_ops->deferred); 2815 pt_op->vma = NULL; /* skip in xe_pt_update_ops_abort */ 2816 } 2817 2818 if (xe_range_fence_insert(&vm->rftree[tile->id], rfence, 2819 &xe_range_fence_kfree_ops, 2820 pt_update_ops->start, 2821 pt_update_ops->last, fence)) 2822 dma_fence_wait(fence, false); 2823 2824 if (ijob) 2825 ifence = xe_tlb_inval_job_push(ijob, tile->migrate, fence); 2826 if (mjob) 2827 mfence = xe_tlb_inval_job_push(mjob, tile->migrate, fence); 2828 2829 if (!mjob && !ijob) { 2830 dma_resv_add_fence(xe_vm_resv(vm), fence, 2831 pt_update_ops->wait_vm_bookkeep ? 2832 DMA_RESV_USAGE_KERNEL : 2833 DMA_RESV_USAGE_BOOKKEEP); 2834 2835 list_for_each_entry(op, &vops->list, link) 2836 op_commit(vops->vm, tile, pt_update_ops, op, fence, NULL); 2837 } else if (ijob && !mjob) { 2838 dma_resv_add_fence(xe_vm_resv(vm), ifence, 2839 pt_update_ops->wait_vm_bookkeep ? 2840 DMA_RESV_USAGE_KERNEL : 2841 DMA_RESV_USAGE_BOOKKEEP); 2842 2843 list_for_each_entry(op, &vops->list, link) 2844 op_commit(vops->vm, tile, pt_update_ops, op, ifence, NULL); 2845 } else { 2846 dma_resv_add_fence(xe_vm_resv(vm), ifence, 2847 pt_update_ops->wait_vm_bookkeep ? 2848 DMA_RESV_USAGE_KERNEL : 2849 DMA_RESV_USAGE_BOOKKEEP); 2850 2851 dma_resv_add_fence(xe_vm_resv(vm), mfence, 2852 pt_update_ops->wait_vm_bookkeep ? 2853 DMA_RESV_USAGE_KERNEL : 2854 DMA_RESV_USAGE_BOOKKEEP); 2855 2856 list_for_each_entry(op, &vops->list, link) 2857 op_commit(vops->vm, tile, pt_update_ops, op, ifence, 2858 mfence); 2859 } 2860 2861 if (pt_update_ops->needs_svm_lock) 2862 xe_pt_svm_userptr_notifier_unlock(vm); 2863 2864 /* 2865 * The last fence is only used for zero bind queue idling; migrate 2866 * queues are not exposed to user space. 2867 */ 2868 if (!(q->flags & EXEC_QUEUE_FLAG_MIGRATE)) 2869 xe_exec_queue_last_fence_set(q, vm, fence); 2870 2871 xe_tlb_inval_job_put(mjob); 2872 xe_tlb_inval_job_put(ijob); 2873 dma_fence_put(ifence); 2874 dma_fence_put(mfence); 2875 2876 return fence; 2877 2878 free_rfence: 2879 kfree(rfence); 2880 free_ijob: 2881 xe_tlb_inval_job_put(mjob); 2882 xe_tlb_inval_job_put(ijob); 2883 kill_vm_tile1: 2884 if (err != -EAGAIN && err != -ENODATA && tile->id) 2885 xe_vm_kill(vops->vm, false); 2886 2887 return ERR_PTR(err); 2888 } 2889 ALLOW_ERROR_INJECTION(xe_pt_update_ops_run, ERRNO); 2890 2891 /** 2892 * xe_pt_update_ops_fini() - Finish PT update operations 2893 * @tile: Tile of PT update operations 2894 * @vops: VMA operations 2895 * 2896 * Finish PT update operations by committing to destroy page table memory 2897 */ 2898 void xe_pt_update_ops_fini(struct xe_tile *tile, struct xe_vma_ops *vops) 2899 { 2900 struct xe_vm_pgtable_update_ops *pt_update_ops = 2901 &vops->pt_update_ops[tile->id]; 2902 int i; 2903 2904 xe_page_reclaim_entries_put(pt_update_ops->prl.entries); 2905 2906 lockdep_assert_held(&vops->vm->lock); 2907 xe_vm_assert_held(vops->vm); 2908 2909 for (i = 0; i < pt_update_ops->current_op; ++i) { 2910 struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[i]; 2911 2912 xe_pt_free_bind(pt_op->entries, pt_op->num_entries); 2913 } 2914 xe_bo_put_commit(&vops->pt_update_ops[tile->id].deferred); 2915 } 2916 2917 /** 2918 * xe_pt_update_ops_abort() - Abort PT update operations 2919 * @tile: Tile of PT update operations 2920 * @vops: VMA operationa 2921 * 2922 * Abort PT update operations by unwinding internal PT state 2923 */ 2924 void xe_pt_update_ops_abort(struct xe_tile *tile, struct xe_vma_ops *vops) 2925 { 2926 struct xe_vm_pgtable_update_ops *pt_update_ops = 2927 &vops->pt_update_ops[tile->id]; 2928 int i; 2929 2930 lockdep_assert_held(&vops->vm->lock); 2931 xe_vm_assert_held(vops->vm); 2932 2933 for (i = pt_update_ops->num_ops - 1; i >= 0; --i) { 2934 struct xe_vm_pgtable_update_op *pt_op = 2935 &pt_update_ops->ops[i]; 2936 2937 if (!pt_op->vma || i >= pt_update_ops->current_op) 2938 continue; 2939 2940 if (pt_op->bind) 2941 xe_pt_abort_bind(pt_op->vma, pt_op->entries, 2942 pt_op->num_entries, 2943 pt_op->rebind); 2944 else 2945 xe_pt_abort_unbind(pt_op->vma, pt_op->entries, 2946 pt_op->num_entries); 2947 } 2948 2949 xe_pt_update_ops_fini(tile, vops); 2950 } 2951