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