1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2020 Intel Corporation 4 */ 5 6 #include "xe_migrate.h" 7 8 #include <linux/bitfield.h> 9 #include <linux/sizes.h> 10 11 #include <drm/drm_managed.h> 12 #include <drm/drm_pagemap.h> 13 #include <drm/ttm/ttm_tt.h> 14 #include <uapi/drm/xe_drm.h> 15 16 #include <generated/xe_wa_oob.h> 17 18 #include "instructions/xe_gpu_commands.h" 19 #include "instructions/xe_mi_commands.h" 20 #include "regs/xe_gtt_defs.h" 21 #include "tests/xe_test.h" 22 #include "xe_assert.h" 23 #include "xe_bb.h" 24 #include "xe_bo.h" 25 #include "xe_exec_queue.h" 26 #include "xe_ggtt.h" 27 #include "xe_gt.h" 28 #include "xe_gt_printk.h" 29 #include "xe_hw_engine.h" 30 #include "xe_lrc.h" 31 #include "xe_map.h" 32 #include "xe_mem_pool.h" 33 #include "xe_mocs.h" 34 #include "xe_pat.h" 35 #include "xe_printk.h" 36 #include "xe_pt.h" 37 #include "xe_res_cursor.h" 38 #include "xe_sa.h" 39 #include "xe_sched_job.h" 40 #include "xe_sriov_vf_ccs.h" 41 #include "xe_svm.h" 42 #include "xe_sync.h" 43 #include "xe_trace_bo.h" 44 #include "xe_validation.h" 45 #include "xe_vm.h" 46 #include "xe_vram.h" 47 48 /** 49 * struct xe_migrate - migrate context. 50 */ 51 struct xe_migrate { 52 /** @q: Default exec queue used for migration */ 53 struct xe_exec_queue *q; 54 /** @tile: Backpointer to the tile this struct xe_migrate belongs to. */ 55 struct xe_tile *tile; 56 /** @job_mutex: Timeline mutex for @eng. */ 57 struct mutex job_mutex; 58 /** @pt_bo: Page-table buffer object. */ 59 struct xe_bo *pt_bo; 60 /** @batch_base_ofs: VM offset of the migration batch buffer */ 61 u64 batch_base_ofs; 62 /** @usm_batch_base_ofs: VM offset of the usm batch buffer */ 63 u64 usm_batch_base_ofs; 64 /** @cleared_mem_ofs: VM offset of @cleared_bo. */ 65 u64 cleared_mem_ofs; 66 /** @large_page_copy_ofs: VM offset of 2M pages used for large copies */ 67 u64 large_page_copy_ofs; 68 /** 69 * @large_page_copy_pdes: BO offset to writeout 2M pages (PDEs) used for 70 * large copies 71 */ 72 u64 large_page_copy_pdes; 73 /** 74 * @fence: dma-fence representing the last migration job batch. 75 * Protected by @job_mutex. 76 */ 77 struct dma_fence *fence; 78 /** 79 * @vm_update_sa: For integrated, used to suballocate page-tables 80 * out of the pt_bo. 81 */ 82 struct drm_suballoc_manager vm_update_sa; 83 /** @min_chunk_size: For dgfx, Minimum chunk size */ 84 u64 min_chunk_size; 85 }; 86 87 #define MAX_PREEMPTDISABLE_TRANSFER SZ_8M /* Around 1ms. */ 88 #define MAX_CCS_LIMITED_TRANSFER SZ_4M /* XE_PAGE_SIZE * (FIELD_MAX(XE2_CCS_SIZE_MASK) + 1) */ 89 #define NUM_KERNEL_PDE 15 90 #define NUM_PT_SLOTS 48 91 #define LEVEL0_PAGE_TABLE_ENCODE_SIZE SZ_2M 92 #define MAX_NUM_PTE 512 93 #define IDENTITY_OFFSET 256ULL 94 95 /* 96 * Although MI_STORE_DATA_IMM's "length" field is 10-bits, 0x3FE is the largest 97 * legal value accepted. Since that instruction field is always stored in 98 * (val-2) format, this translates to 0x400 dwords for the true maximum length 99 * of the instruction. Subtracting the instruction header (1 dword) and 100 * address (2 dwords), that leaves 0x3FD dwords (0x1FE qwords) for PTE values. 101 */ 102 #define MAX_PTE_PER_SDI 0x1FEU 103 104 static void xe_migrate_fini(void *arg) 105 { 106 struct xe_migrate *m = arg; 107 108 xe_vm_lock(m->q->vm, false); 109 xe_bo_unpin(m->pt_bo); 110 xe_vm_unlock(m->q->vm); 111 112 dma_fence_put(m->fence); 113 xe_bo_put(m->pt_bo); 114 drm_suballoc_manager_fini(&m->vm_update_sa); 115 mutex_destroy(&m->job_mutex); 116 xe_vm_close_and_put(m->q->vm); 117 xe_exec_queue_put(m->q); 118 } 119 120 static inline u16 xe_migrate_pat_index(struct xe_device *xe, 121 enum ttm_caching caching, 122 bool is_comp_pte) 123 { 124 enum xe_cache_level cache_level; 125 126 /* 127 * Select the appropriate PAT index for buffer object PTEs programmed 128 * by emit_pte(). We choose not to mess with xe_migrate_prepare_vm() 129 * yet, for simplicity. 130 */ 131 if (is_comp_pte && GRAPHICS_VERx100(xe) >= 2000) 132 cache_level = XE_CACHE_NONE_COMPRESSION; 133 else if (caching == ttm_cached) 134 cache_level = XE_CACHE_WB; 135 else 136 cache_level = XE_CACHE_NONE; 137 138 return xe_cache_pat_idx(xe, cache_level); 139 } 140 141 static u64 xe_migrate_vm_addr(u64 slot, u32 level) 142 { 143 XE_WARN_ON(slot >= NUM_PT_SLOTS); 144 145 /* First slot is reserved for mapping of PT bo and bb, start from 1 */ 146 return (slot + 1ULL) << xe_pt_shift(level + 1); 147 } 148 149 static u64 xe_migrate_vram_ofs(struct xe_device *xe, u64 addr, bool is_comp_pte) 150 { 151 /* 152 * Remove the DPA to get a correct offset into identity table for the 153 * migrate offset 154 */ 155 u64 identity_offset = IDENTITY_OFFSET; 156 157 if (GRAPHICS_VER(xe) >= 20 && is_comp_pte) 158 identity_offset += DIV_ROUND_UP_ULL(xe_vram_region_actual_physical_size 159 (xe->mem.vram), SZ_1G); 160 161 addr -= xe_vram_region_dpa_base(xe->mem.vram); 162 return addr + (identity_offset << xe_pt_shift(2)); 163 } 164 165 static void xe_migrate_program_identity(struct xe_device *xe, struct xe_vm *vm, struct xe_bo *bo, 166 u64 map_ofs, u64 vram_offset, u16 pat_index, u64 pt_2m_ofs, 167 u64 pt_4k_ofs) 168 { 169 struct xe_vram_region *vram = xe->mem.vram; 170 resource_size_t dpa_base = xe_vram_region_dpa_base(vram); 171 u64 pos, ofs, flags; 172 u64 entry; 173 u64 vram_limit = xe_vram_region_usable_size(vram) + dpa_base; 174 u32 level = 2; 175 176 ofs = map_ofs + XE_PAGE_SIZE * level + vram_offset * 8; 177 flags = vm->pt_ops->pte_encode_addr(xe, 0, pat_index, level, 178 true, 0); 179 180 /* 181 * Use 1GB pages when possible, last chunk always use 2M 182 * pages as mixing reserved memory (stolen, WOCPM) with a single 183 * mapping is not allowed on certain platforms. 184 */ 185 for (pos = dpa_base; pos < vram_limit; 186 pos += SZ_1G, ofs += 8) { 187 if (pos + SZ_1G >= vram_limit) { 188 entry = vm->pt_ops->pde_encode_bo(bo, pt_2m_ofs); 189 xe_map_wr(xe, &bo->vmap, ofs, u64, entry); 190 191 flags = vm->pt_ops->pte_encode_addr(xe, 0, 192 pat_index, 193 level - 1, 194 true, 0); 195 196 for (ofs = pt_2m_ofs; pos < vram_limit; 197 pos += SZ_2M, ofs += 8) { 198 if (pos + SZ_2M > vram_limit) { 199 entry = vm->pt_ops->pde_encode_bo(bo, pt_4k_ofs); 200 xe_map_wr(xe, &bo->vmap, ofs, u64, entry); 201 202 flags = vm->pt_ops->pte_encode_addr(xe, 0, 203 pat_index, 204 level - 2, 205 true, 0); 206 207 for (ofs = pt_4k_ofs; pos < vram_limit; 208 pos += SZ_4K, ofs += 8) 209 xe_map_wr(xe, &bo->vmap, ofs, u64, pos | flags); 210 break; 211 } 212 213 xe_map_wr(xe, &bo->vmap, ofs, u64, pos | flags); 214 } 215 break; /* Ensure pos == vram_limit assert correct */ 216 } 217 218 xe_map_wr(xe, &bo->vmap, ofs, u64, pos | flags); 219 } 220 221 xe_assert(xe, pos == vram_limit); 222 } 223 224 static int xe_migrate_pt_bo_alloc(struct xe_tile *tile, struct xe_migrate *m, 225 struct xe_vm *vm, struct drm_exec *exec) 226 { 227 struct xe_bo *bo, *batch = tile->mem.kernel_bb_pool->bo; 228 u32 num_entries = NUM_PT_SLOTS; 229 230 /* Can't bump NUM_PT_SLOTS too high */ 231 BUILD_BUG_ON(NUM_PT_SLOTS > SZ_2M/XE_PAGE_SIZE); 232 /* Must be a multiple of 64K to support all platforms */ 233 BUILD_BUG_ON(NUM_PT_SLOTS * XE_PAGE_SIZE % SZ_64K); 234 /* And one slot reserved for the 4KiB page table updates */ 235 BUILD_BUG_ON(!(NUM_KERNEL_PDE & 1)); 236 237 /* Need to be sure everything fits in the first PT, or create more */ 238 xe_tile_assert(tile, m->batch_base_ofs + xe_bo_size(batch) < SZ_2M); 239 240 bo = xe_bo_create_pin_map(vm->xe, tile, vm, 241 num_entries * XE_PAGE_SIZE, 242 ttm_bo_type_kernel, 243 XE_BO_FLAG_VRAM_IF_DGFX(tile) | 244 XE_BO_FLAG_PAGETABLE, exec); 245 if (IS_ERR(bo)) 246 return PTR_ERR(bo); 247 248 m->pt_bo = bo; 249 return 0; 250 } 251 252 static void xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m, 253 struct xe_vm *vm, u32 *ofs) 254 { 255 struct xe_device *xe = tile_to_xe(tile); 256 u16 pat_index = xe_cache_pat_idx(xe, XE_CACHE_WB); 257 u8 id = tile->id; 258 u32 num_entries = NUM_PT_SLOTS, num_level = vm->pt_root[id]->level; 259 #define VRAM_IDENTITY_MAP_PT_COUNT 4 260 u32 num_setup = num_level + VRAM_IDENTITY_MAP_PT_COUNT; 261 #undef VRAM_IDENTITY_MAP_PT_COUNT 262 u32 map_ofs, level, i; 263 struct xe_bo *bo = m->pt_bo, *batch = tile->mem.kernel_bb_pool->bo; 264 u64 entry; 265 266 /* PT44..PT47 reserved for 4K and 2M identity map */ 267 u64 l1_pt_ofs = xe_bo_size(bo) - 5 * XE_PAGE_SIZE; 268 269 entry = vm->pt_ops->pde_encode_bo(bo, l1_pt_ofs); 270 xe_pt_write(xe, &vm->pt_root[id]->bo->vmap, 0, entry); 271 272 map_ofs = (num_entries - num_setup) * XE_PAGE_SIZE; 273 274 /* Map the entire BO in our level 0 pt */ 275 for (i = 0, level = 0; i < num_entries; level++) { 276 entry = vm->pt_ops->pte_encode_bo(bo, i * XE_PAGE_SIZE, 277 pat_index, 0); 278 279 xe_map_wr(xe, &bo->vmap, map_ofs + level * 8, u64, entry); 280 281 if (vm->flags & XE_VM_FLAG_64K) 282 i += 16; 283 else 284 i += 1; 285 } 286 287 if (!IS_DGFX(xe)) { 288 /* Write out batch too */ 289 m->batch_base_ofs = NUM_PT_SLOTS * XE_PAGE_SIZE; 290 for (i = 0; i < xe_bo_size(batch); 291 i += vm->flags & XE_VM_FLAG_64K ? XE_64K_PAGE_SIZE : 292 XE_PAGE_SIZE) { 293 entry = vm->pt_ops->pte_encode_bo(batch, i, 294 pat_index, 0); 295 296 xe_map_wr(xe, &bo->vmap, map_ofs + level * 8, u64, 297 entry); 298 level++; 299 } 300 if (xe->info.has_usm) { 301 xe_tile_assert(tile, xe_bo_size(batch) == SZ_1M); 302 303 batch = tile->primary_gt->usm.bb_pool->bo; 304 m->usm_batch_base_ofs = m->batch_base_ofs + SZ_1M; 305 xe_tile_assert(tile, xe_bo_size(batch) == SZ_512K); 306 307 for (i = 0; i < xe_bo_size(batch); 308 i += vm->flags & XE_VM_FLAG_64K ? XE_64K_PAGE_SIZE : 309 XE_PAGE_SIZE) { 310 entry = vm->pt_ops->pte_encode_bo(batch, i, 311 pat_index, 0); 312 313 xe_map_wr(xe, &bo->vmap, map_ofs + level * 8, u64, 314 entry); 315 level++; 316 } 317 } 318 } else { 319 u64 batch_addr = xe_bo_addr(batch, 0, XE_PAGE_SIZE); 320 321 m->batch_base_ofs = xe_migrate_vram_ofs(xe, batch_addr, false); 322 323 if (xe->info.has_usm) { 324 batch = tile->primary_gt->usm.bb_pool->bo; 325 batch_addr = xe_bo_addr(batch, 0, XE_PAGE_SIZE); 326 m->usm_batch_base_ofs = xe_migrate_vram_ofs(xe, batch_addr, false); 327 } 328 } 329 330 for (level = 1; level < num_level; level++) { 331 u32 flags = 0; 332 333 if (vm->flags & XE_VM_FLAG_64K && level == 1) 334 flags = XE_PDE_64K; 335 336 entry = vm->pt_ops->pde_encode_bo(bo, map_ofs + (u64)(level - 1) * 337 XE_PAGE_SIZE); 338 xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE * level, u64, 339 entry | flags); 340 } 341 342 /* Write PDE's that point to our BO. */ 343 for (i = 0; i < map_ofs / XE_PAGE_SIZE; i++) { 344 entry = vm->pt_ops->pde_encode_bo(bo, (u64)i * XE_PAGE_SIZE); 345 346 xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE + 347 (i + 1) * 8, u64, entry); 348 } 349 350 /* Reserve 2M PDEs */ 351 level = 1; 352 m->large_page_copy_ofs = NUM_PT_SLOTS << xe_pt_shift(level); 353 m->large_page_copy_pdes = map_ofs + XE_PAGE_SIZE * level + 354 NUM_PT_SLOTS * 8; 355 356 /* Set up a 1GiB NULL mapping at 255GiB offset. */ 357 level = 2; 358 xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE * level + 255 * 8, u64, 359 vm->pt_ops->pte_encode_addr(xe, 0, pat_index, level, IS_DGFX(xe), 0) 360 | XE_PTE_NULL); 361 m->cleared_mem_ofs = (255ULL << xe_pt_shift(level)); 362 363 /* Identity map the entire vram at 256GiB offset */ 364 if (IS_DGFX(xe)) { 365 u64 pt46_ofs = xe_bo_size(bo) - 2 * XE_PAGE_SIZE; 366 resource_size_t actual_phy_size = xe_vram_region_actual_physical_size(xe->mem.vram); 367 368 u64 pt44_ofs = xe_bo_size(bo) - 4 * XE_PAGE_SIZE; 369 xe_migrate_program_identity(xe, vm, bo, map_ofs, IDENTITY_OFFSET, 370 pat_index, pt46_ofs, pt44_ofs); 371 xe_assert(xe, actual_phy_size <= (MAX_NUM_PTE - IDENTITY_OFFSET) * SZ_1G); 372 373 /* 374 * Identity map the entire vram for compressed pat_index for xe2+ 375 * if flat ccs is enabled. 376 */ 377 if (GRAPHICS_VER(xe) >= 20 && xe_device_has_flat_ccs(xe)) { 378 u16 comp_pat_index = xe_cache_pat_idx(xe, XE_CACHE_NONE_COMPRESSION); 379 u64 vram_offset = IDENTITY_OFFSET + 380 DIV_ROUND_UP_ULL(actual_phy_size, SZ_1G); 381 u64 pt47_ofs = xe_bo_size(bo) - XE_PAGE_SIZE; 382 383 xe_assert(xe, actual_phy_size <= (MAX_NUM_PTE - IDENTITY_OFFSET - 384 IDENTITY_OFFSET / 2) * SZ_1G); 385 u64 pt45_ofs = xe_bo_size(bo) - 3 * XE_PAGE_SIZE; 386 xe_migrate_program_identity(xe, vm, bo, map_ofs, vram_offset, 387 comp_pat_index, pt47_ofs, pt45_ofs); 388 } 389 } 390 391 if (ofs) 392 *ofs = map_ofs; 393 } 394 395 static void xe_migrate_suballoc_manager_init(struct xe_migrate *m, u32 map_ofs) 396 { 397 /* 398 * Example layout created above, with root level = 3: 399 * [PT0...PT7]: kernel PT's for copy/clear; 64 or 4KiB PTE's 400 * [PT8]: Kernel PT for VM_BIND, 4 KiB PTE's 401 * [PT9...PT40]: Userspace PT's for VM_BIND, 4 KiB PTE's 402 * [PT41 = PDE 0] [PT44...PT47 = 4K and 2M vram identity maps] 403 * 404 * This makes the lowest part of the VM point to the pagetables. 405 * Hence the lowest 2M in the vm should point to itself, with a few writes 406 * and flushes, other parts of the VM can be used either for copying and 407 * clearing. 408 * 409 * For performance, the kernel reserves PDE's, so about 20 are left 410 * for async VM updates. 411 * 412 * To make it easier to work, each scratch PT is put in slot (1 + PT #) 413 * everywhere, this allows lockless updates to scratch pages by using 414 * the different addresses in VM. 415 */ 416 #define NUM_VMUSA_UNIT_PER_PAGE 32 417 #define VM_SA_UPDATE_UNIT_SIZE (XE_PAGE_SIZE / NUM_VMUSA_UNIT_PER_PAGE) 418 #define NUM_VMUSA_WRITES_PER_UNIT (VM_SA_UPDATE_UNIT_SIZE / sizeof(u64)) 419 drm_suballoc_manager_init(&m->vm_update_sa, 420 (size_t)(map_ofs / XE_PAGE_SIZE - NUM_KERNEL_PDE) * 421 NUM_VMUSA_UNIT_PER_PAGE, 0); 422 } 423 424 static bool xe_migrate_needs_ccs_emit(struct xe_device *xe) 425 { 426 return xe_device_has_flat_ccs(xe) && !(GRAPHICS_VER(xe) >= 20 && IS_DGFX(xe)); 427 } 428 429 /** 430 * xe_migrate_alloc - Allocate a migrate struct for a given &xe_tile 431 * @tile: &xe_tile 432 * 433 * Allocates a &xe_migrate for a given tile. 434 * 435 * Return: &xe_migrate on success, or NULL when out of memory. 436 */ 437 struct xe_migrate *xe_migrate_alloc(struct xe_tile *tile) 438 { 439 struct xe_migrate *m = drmm_kzalloc(&tile_to_xe(tile)->drm, sizeof(*m), GFP_KERNEL); 440 441 if (m) 442 m->tile = tile; 443 return m; 444 } 445 446 static int xe_migrate_lock_prepare_vm(struct xe_tile *tile, struct xe_migrate *m, struct xe_vm *vm) 447 { 448 struct xe_device *xe = tile_to_xe(tile); 449 struct xe_validation_ctx ctx; 450 struct drm_exec exec; 451 u32 map_ofs; 452 int err = 0; 453 454 xe_validation_guard(&ctx, &xe->val, &exec, (struct xe_val_flags) {}, err) { 455 err = xe_vm_drm_exec_lock(vm, &exec); 456 if (err) 457 return err; 458 459 drm_exec_retry_on_contention(&exec); 460 461 err = xe_migrate_pt_bo_alloc(tile, m, vm, &exec); 462 if (err) 463 return err; 464 465 xe_migrate_prepare_vm(tile, m, vm, &map_ofs); 466 xe_migrate_suballoc_manager_init(m, map_ofs); 467 drm_exec_retry_on_contention(&exec); 468 xe_validation_retry_on_oom(&ctx, &err); 469 } 470 471 return err; 472 } 473 474 /** 475 * xe_migrate_init() - Initialize a migrate context 476 * @m: The migration context 477 * 478 * Return: 0 if successful, negative error code on failure 479 */ 480 int xe_migrate_init(struct xe_migrate *m) 481 { 482 struct xe_tile *tile = m->tile; 483 struct xe_gt *primary_gt = tile->primary_gt; 484 struct xe_device *xe = tile_to_xe(tile); 485 struct xe_vm *vm; 486 int err; 487 488 /* Special layout, prepared below.. */ 489 vm = xe_vm_create(xe, XE_VM_FLAG_MIGRATION | 490 XE_VM_FLAG_SET_TILE_ID(tile), NULL); 491 if (IS_ERR(vm)) 492 return PTR_ERR(vm); 493 494 err = xe_migrate_lock_prepare_vm(tile, m, vm); 495 if (err) 496 goto err_out; 497 498 if (xe->info.has_usm) { 499 struct xe_hw_engine *hwe0 = primary_gt->usm.paging_hwe0; 500 u32 logical_mask = primary_gt->usm.paging_logical_mask; 501 502 if (!hwe0 || !logical_mask) { 503 err = -EINVAL; 504 goto err_out; 505 } 506 507 /* 508 * XXX: Currently only reserving 1 (likely slow) BCS instance on 509 * PVC, may want to revisit if performance is needed. 510 */ 511 m->q = xe_exec_queue_create(xe, vm, logical_mask, 1, hwe0, 512 EXEC_QUEUE_FLAG_KERNEL | 513 EXEC_QUEUE_FLAG_HIGH_PRIORITY | 514 EXEC_QUEUE_FLAG_MIGRATE | 515 EXEC_QUEUE_FLAG_LOW_LATENCY, 0); 516 } else { 517 m->q = xe_exec_queue_create_class(xe, primary_gt, vm, 518 XE_ENGINE_CLASS_COPY, 519 EXEC_QUEUE_FLAG_KERNEL | 520 EXEC_QUEUE_FLAG_MIGRATE, 0); 521 } 522 if (IS_ERR(m->q)) { 523 err = PTR_ERR(m->q); 524 goto err_out; 525 } 526 527 mutex_init(&m->job_mutex); 528 fs_reclaim_acquire(GFP_KERNEL); 529 might_lock(&m->job_mutex); 530 fs_reclaim_release(GFP_KERNEL); 531 532 err = devm_add_action_or_reset(xe->drm.dev, xe_migrate_fini, m); 533 if (err) 534 return err; 535 536 if (IS_DGFX(xe)) { 537 if (xe_migrate_needs_ccs_emit(xe)) 538 /* min chunk size corresponds to 4K of CCS Metadata */ 539 m->min_chunk_size = SZ_4K * SZ_64K / 540 xe_device_ccs_bytes(xe, SZ_64K); 541 else 542 /* Somewhat arbitrary to avoid a huge amount of blits */ 543 m->min_chunk_size = SZ_64K; 544 m->min_chunk_size = roundup_pow_of_two(m->min_chunk_size); 545 drm_dbg(&xe->drm, "Migrate min chunk size is 0x%08llx\n", 546 (unsigned long long)m->min_chunk_size); 547 } 548 549 return err; 550 551 err_out: 552 xe_vm_close_and_put(vm); 553 return err; 554 555 } 556 557 static u64 max_mem_transfer_per_pass(struct xe_device *xe) 558 { 559 if (!IS_DGFX(xe) && xe_device_has_flat_ccs(xe)) 560 return MAX_CCS_LIMITED_TRANSFER; 561 562 return MAX_PREEMPTDISABLE_TRANSFER; 563 } 564 565 static u64 xe_migrate_res_sizes(struct xe_migrate *m, struct xe_res_cursor *cur) 566 { 567 struct xe_device *xe = tile_to_xe(m->tile); 568 u64 size = min_t(u64, max_mem_transfer_per_pass(xe), cur->remaining); 569 570 if (mem_type_is_vram(cur->mem_type)) { 571 /* 572 * VRAM we want to blit in chunks with sizes aligned to 573 * min_chunk_size in order for the offset to CCS metadata to be 574 * page-aligned. If it's the last chunk it may be smaller. 575 * 576 * Another constraint is that we need to limit the blit to 577 * the VRAM block size, unless size is smaller than 578 * min_chunk_size. 579 */ 580 u64 chunk = max_t(u64, cur->size, m->min_chunk_size); 581 582 size = min_t(u64, size, chunk); 583 if (size > m->min_chunk_size) 584 size = round_down(size, m->min_chunk_size); 585 } 586 587 return size; 588 } 589 590 static bool xe_migrate_allow_identity(u64 size, const struct xe_res_cursor *cur) 591 { 592 /* If the chunk is not fragmented, allow identity map. */ 593 return cur->size >= size; 594 } 595 596 #define PTE_UPDATE_FLAG_IS_VRAM BIT(0) 597 #define PTE_UPDATE_FLAG_IS_COMP_PTE BIT(1) 598 599 static u32 pte_update_size(struct xe_migrate *m, 600 u32 flags, 601 struct ttm_resource *res, 602 struct xe_res_cursor *cur, 603 u64 *L0, u64 *L0_ofs, u32 *L0_pt, 604 u32 cmd_size, u32 pt_ofs, u32 avail_pts) 605 { 606 u32 cmds = 0; 607 bool is_vram = PTE_UPDATE_FLAG_IS_VRAM & flags; 608 bool is_comp_pte = PTE_UPDATE_FLAG_IS_COMP_PTE & flags; 609 610 *L0_pt = pt_ofs; 611 if (is_vram && xe_migrate_allow_identity(*L0, cur)) { 612 /* Offset into identity map. */ 613 *L0_ofs = xe_migrate_vram_ofs(tile_to_xe(m->tile), 614 cur->start + vram_region_gpu_offset(res), 615 is_comp_pte); 616 cmds += cmd_size; 617 } else { 618 /* Clip L0 to available size */ 619 u64 size = min(*L0, (u64)avail_pts * SZ_2M); 620 u32 num_4k_pages = (size + XE_PAGE_SIZE - 1) >> XE_PTE_SHIFT; 621 622 *L0 = size; 623 *L0_ofs = xe_migrate_vm_addr(pt_ofs, 0); 624 625 /* MI_STORE_DATA_IMM */ 626 cmds += 3 * DIV_ROUND_UP(num_4k_pages, MAX_PTE_PER_SDI); 627 628 /* PDE qwords */ 629 cmds += num_4k_pages * 2; 630 631 /* Each chunk has a single blit command */ 632 cmds += cmd_size; 633 } 634 635 return cmds; 636 } 637 638 static void emit_pte(struct xe_migrate *m, 639 struct xe_bb *bb, u32 at_pt, 640 bool is_vram, bool is_comp_pte, 641 struct xe_res_cursor *cur, 642 u32 size, struct ttm_resource *res) 643 { 644 struct xe_device *xe = tile_to_xe(m->tile); 645 struct xe_vm *vm = m->q->vm; 646 struct xe_bo *bo = ttm_to_xe_bo(res->bo); 647 enum ttm_caching caching = ttm_cached; 648 u16 pat_index; 649 u32 ptes; 650 u64 ofs = (u64)at_pt * XE_PAGE_SIZE; 651 u64 cur_ofs; 652 653 if (!is_vram && bo->ttm.ttm) 654 caching = bo->ttm.ttm->caching; 655 656 pat_index = xe_migrate_pat_index(xe, caching, is_comp_pte); 657 658 ptes = DIV_ROUND_UP(size, XE_PAGE_SIZE); 659 660 while (ptes) { 661 u32 chunk = min(MAX_PTE_PER_SDI, ptes); 662 663 bb->cs[bb->len++] = MI_STORE_DATA_IMM | MI_SDI_NUM_QW(chunk); 664 bb->cs[bb->len++] = ofs; 665 bb->cs[bb->len++] = 0; 666 667 cur_ofs = ofs; 668 ofs += chunk * 8; 669 ptes -= chunk; 670 671 while (chunk--) { 672 u64 addr, flags = 0; 673 bool devmem = false; 674 675 addr = xe_res_dma(cur) & PAGE_MASK; 676 if (is_vram) { 677 if (vm->flags & XE_VM_FLAG_64K) { 678 u64 va = cur_ofs * XE_PAGE_SIZE / 8; 679 680 xe_assert(xe, (va & (SZ_64K - 1)) == 681 (addr & (SZ_64K - 1))); 682 683 flags |= XE_PTE_PS64; 684 } 685 686 addr += vram_region_gpu_offset(res); 687 devmem = true; 688 } 689 690 addr = vm->pt_ops->pte_encode_addr(m->tile->xe, 691 addr, pat_index, 692 0, devmem, flags); 693 bb->cs[bb->len++] = lower_32_bits(addr); 694 bb->cs[bb->len++] = upper_32_bits(addr); 695 696 xe_res_next(cur, min_t(u32, size, PAGE_SIZE)); 697 cur_ofs += 8; 698 } 699 } 700 } 701 702 #define EMIT_COPY_CCS_DW 5 703 static void emit_copy_ccs(struct xe_gt *gt, struct xe_bb *bb, 704 u64 dst_ofs, bool dst_is_indirect, 705 u64 src_ofs, bool src_is_indirect, 706 u32 size) 707 { 708 struct xe_device *xe = gt_to_xe(gt); 709 u32 *cs = bb->cs + bb->len; 710 u32 num_ccs_blks; 711 u32 num_pages; 712 u32 ccs_copy_size; 713 u32 mocs; 714 715 if (GRAPHICS_VERx100(xe) >= 2000) { 716 num_pages = DIV_ROUND_UP(size, XE_PAGE_SIZE); 717 xe_gt_assert(gt, FIELD_FIT(XE2_CCS_SIZE_MASK, num_pages - 1)); 718 719 ccs_copy_size = REG_FIELD_PREP(XE2_CCS_SIZE_MASK, num_pages - 1); 720 mocs = FIELD_PREP(XE2_XY_CTRL_SURF_MOCS_INDEX_MASK, gt->mocs.uc_index); 721 722 } else { 723 num_ccs_blks = DIV_ROUND_UP(xe_device_ccs_bytes(gt_to_xe(gt), size), 724 NUM_CCS_BYTES_PER_BLOCK); 725 xe_gt_assert(gt, FIELD_FIT(CCS_SIZE_MASK, num_ccs_blks - 1)); 726 727 ccs_copy_size = REG_FIELD_PREP(CCS_SIZE_MASK, num_ccs_blks - 1); 728 mocs = FIELD_PREP(XY_CTRL_SURF_MOCS_MASK, gt->mocs.uc_index); 729 } 730 731 *cs++ = XY_CTRL_SURF_COPY_BLT | 732 (src_is_indirect ? 0x0 : 0x1) << SRC_ACCESS_TYPE_SHIFT | 733 (dst_is_indirect ? 0x0 : 0x1) << DST_ACCESS_TYPE_SHIFT | 734 ccs_copy_size; 735 *cs++ = lower_32_bits(src_ofs); 736 *cs++ = upper_32_bits(src_ofs) | mocs; 737 *cs++ = lower_32_bits(dst_ofs); 738 *cs++ = upper_32_bits(dst_ofs) | mocs; 739 740 bb->len = cs - bb->cs; 741 } 742 743 static u32 blt_fast_copy_cmd_len(struct xe_device *xe) 744 { 745 return 10; 746 } 747 748 static u32 blt_mem_copy_cmd_len(struct xe_device *xe) 749 { 750 return 10; 751 } 752 753 static u32 emit_copy_cmd_len(struct xe_device *xe) 754 { 755 return (xe->info.has_mem_copy_instr) ? blt_mem_copy_cmd_len(xe) : 756 blt_fast_copy_cmd_len(xe); 757 } 758 759 static void emit_xy_fast_copy(struct xe_gt *gt, struct xe_bb *bb, u64 src_ofs, 760 u64 dst_ofs, unsigned int size, 761 unsigned int pitch) 762 { 763 struct xe_device *xe = gt_to_xe(gt); 764 u32 mocs = 0; 765 u32 tile_y = 0; 766 u32 len; 767 768 xe_gt_assert(gt, !(pitch & 3)); 769 xe_gt_assert(gt, size / pitch <= S16_MAX); 770 xe_gt_assert(gt, pitch / 4 <= S16_MAX); 771 xe_gt_assert(gt, pitch <= U16_MAX); 772 773 if (GRAPHICS_VER(xe) >= 20) 774 mocs = FIELD_PREP(XE2_XY_FAST_COPY_BLT_MOCS_INDEX_MASK, gt->mocs.uc_index); 775 776 if (GRAPHICS_VERx100(xe) >= 1250) 777 tile_y = XY_FAST_COPY_BLT_D1_SRC_TILE4 | XY_FAST_COPY_BLT_D1_DST_TILE4; 778 779 len = blt_fast_copy_cmd_len(xe); 780 bb->cs[bb->len++] = XY_FAST_COPY_BLT_CMD | (len - 2); 781 bb->cs[bb->len++] = XY_FAST_COPY_BLT_DEPTH_32 | pitch | tile_y | mocs; 782 bb->cs[bb->len++] = 0; 783 bb->cs[bb->len++] = (size / pitch) << 16 | pitch / 4; 784 bb->cs[bb->len++] = lower_32_bits(dst_ofs); 785 bb->cs[bb->len++] = upper_32_bits(dst_ofs); 786 bb->cs[bb->len++] = 0; 787 bb->cs[bb->len++] = pitch | mocs; 788 bb->cs[bb->len++] = lower_32_bits(src_ofs); 789 bb->cs[bb->len++] = upper_32_bits(src_ofs); 790 } 791 792 #define PAGE_COPY_MODE_PS SZ_256 /* hw uses 256 bytes as the page-size */ 793 static void emit_mem_copy(struct xe_gt *gt, struct xe_bb *bb, u64 src_ofs, 794 u64 dst_ofs, unsigned int size, unsigned int pitch) 795 { 796 u32 mode, copy_type, width; 797 u32 len; 798 799 xe_gt_assert(gt, IS_ALIGNED(size, pitch)); 800 xe_gt_assert(gt, pitch <= U16_MAX); 801 xe_gt_assert(gt, pitch); 802 xe_gt_assert(gt, size); 803 804 if (IS_ALIGNED(size, PAGE_COPY_MODE_PS) && 805 IS_ALIGNED(lower_32_bits(src_ofs), PAGE_COPY_MODE_PS) && 806 IS_ALIGNED(lower_32_bits(dst_ofs), PAGE_COPY_MODE_PS)) { 807 mode = MEM_COPY_PAGE_COPY_MODE; 808 copy_type = 0; /* linear copy */ 809 width = size / PAGE_COPY_MODE_PS; 810 } else if (pitch > 1) { 811 xe_gt_assert(gt, size / pitch <= U16_MAX); 812 mode = 0; /* BYTE_COPY */ 813 copy_type = MEM_COPY_MATRIX_COPY; 814 width = pitch; 815 } else { 816 mode = 0; /* BYTE_COPY */ 817 copy_type = 0; /* linear copy */ 818 width = size; 819 } 820 821 xe_gt_assert(gt, width <= U16_MAX); 822 823 len = blt_mem_copy_cmd_len(gt_to_xe(gt)); 824 825 bb->cs[bb->len++] = MEM_COPY_CMD | mode | copy_type | (len - 2); 826 bb->cs[bb->len++] = width - 1; 827 bb->cs[bb->len++] = size / pitch - 1; /* ignored by hw for page-copy/linear above */ 828 bb->cs[bb->len++] = pitch - 1; 829 bb->cs[bb->len++] = pitch - 1; 830 bb->cs[bb->len++] = lower_32_bits(src_ofs); 831 bb->cs[bb->len++] = upper_32_bits(src_ofs); 832 bb->cs[bb->len++] = lower_32_bits(dst_ofs); 833 bb->cs[bb->len++] = upper_32_bits(dst_ofs); 834 bb->cs[bb->len++] = FIELD_PREP(MEM_COPY_SRC_MOCS_INDEX_MASK, gt->mocs.uc_index) | 835 FIELD_PREP(MEM_COPY_DST_MOCS_INDEX_MASK, gt->mocs.uc_index); 836 } 837 838 static void emit_copy(struct xe_gt *gt, struct xe_bb *bb, 839 u64 src_ofs, u64 dst_ofs, unsigned int size, 840 unsigned int pitch) 841 { 842 struct xe_device *xe = gt_to_xe(gt); 843 844 if (xe->info.has_mem_copy_instr) 845 emit_mem_copy(gt, bb, src_ofs, dst_ofs, size, pitch); 846 else 847 emit_xy_fast_copy(gt, bb, src_ofs, dst_ofs, size, pitch); 848 } 849 850 static u64 xe_migrate_batch_base(struct xe_migrate *m, bool usm) 851 { 852 return usm ? m->usm_batch_base_ofs : m->batch_base_ofs; 853 } 854 855 static u32 xe_migrate_ccs_copy(struct xe_migrate *m, 856 struct xe_bb *bb, 857 u64 src_ofs, bool src_is_indirect, 858 u64 dst_ofs, bool dst_is_indirect, u32 dst_size, 859 u64 ccs_ofs, bool copy_ccs) 860 { 861 struct xe_gt *gt = m->tile->primary_gt; 862 u32 flush_flags = 0; 863 864 if (!copy_ccs && dst_is_indirect) { 865 /* 866 * If the src is already in vram, then it should already 867 * have been cleared by us, or has been populated by the 868 * user. Make sure we copy the CCS aux state as-is. 869 * 870 * Otherwise if the bo doesn't have any CCS metadata attached, 871 * we still need to clear it for security reasons. 872 */ 873 u64 ccs_src_ofs = src_is_indirect ? src_ofs : m->cleared_mem_ofs; 874 875 emit_copy_ccs(gt, bb, 876 dst_ofs, true, 877 ccs_src_ofs, src_is_indirect, dst_size); 878 879 flush_flags = MI_FLUSH_DW_CCS; 880 } else if (copy_ccs) { 881 if (!src_is_indirect) 882 src_ofs = ccs_ofs; 883 else if (!dst_is_indirect) 884 dst_ofs = ccs_ofs; 885 886 xe_gt_assert(gt, src_is_indirect || dst_is_indirect); 887 888 emit_copy_ccs(gt, bb, dst_ofs, dst_is_indirect, src_ofs, 889 src_is_indirect, dst_size); 890 if (dst_is_indirect) 891 flush_flags = MI_FLUSH_DW_CCS; 892 } 893 894 return flush_flags; 895 } 896 897 static struct dma_fence *__xe_migrate_copy(struct xe_migrate *m, 898 struct xe_bo *src_bo, 899 struct xe_bo *dst_bo, 900 struct ttm_resource *src, 901 struct ttm_resource *dst, 902 bool copy_only_ccs, 903 bool is_vram_resolve) 904 { 905 struct xe_gt *gt = m->tile->primary_gt; 906 struct xe_device *xe = gt_to_xe(gt); 907 struct dma_fence *fence = NULL; 908 u64 size = xe_bo_size(src_bo); 909 struct xe_res_cursor src_it, dst_it, ccs_it; 910 u64 src_L0_ofs, dst_L0_ofs; 911 u32 src_L0_pt, dst_L0_pt; 912 u64 src_L0, dst_L0; 913 int pass = 0; 914 int err; 915 bool src_is_pltt = src->mem_type == XE_PL_TT; 916 bool dst_is_pltt = dst->mem_type == XE_PL_TT; 917 bool src_is_vram = mem_type_is_vram(src->mem_type); 918 bool dst_is_vram = mem_type_is_vram(dst->mem_type); 919 bool type_device = src_bo->ttm.type == ttm_bo_type_device; 920 bool needs_ccs_emit = type_device && xe_migrate_needs_ccs_emit(xe); 921 bool copy_ccs = xe_device_has_flat_ccs(xe) && 922 xe_bo_needs_ccs_pages(src_bo) && xe_bo_needs_ccs_pages(dst_bo); 923 bool copy_system_ccs = copy_ccs && (!src_is_vram || !dst_is_vram); 924 925 /* 926 * For decompression operation, always use the compression PAT index. 927 * Otherwise, only use the compression PAT index for device memory 928 * when copying from VRAM to system memory. 929 */ 930 bool use_comp_pat = is_vram_resolve || (type_device && 931 xe_device_has_flat_ccs(xe) && 932 GRAPHICS_VER(xe) >= 20 && src_is_vram && !dst_is_vram); 933 934 /* Copying CCS between two different BOs is not supported yet. */ 935 if (XE_WARN_ON(copy_ccs && src_bo != dst_bo)) 936 return ERR_PTR(-EINVAL); 937 938 if (src_bo != dst_bo && XE_WARN_ON(xe_bo_size(src_bo) != xe_bo_size(dst_bo))) 939 return ERR_PTR(-EINVAL); 940 941 if (!src_is_vram) 942 xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it); 943 else 944 xe_res_first(src, 0, size, &src_it); 945 if (!dst_is_vram) 946 xe_res_first_sg(xe_bo_sg(dst_bo), 0, size, &dst_it); 947 else 948 xe_res_first(dst, 0, size, &dst_it); 949 950 if (copy_system_ccs) 951 xe_res_first_sg(xe_bo_sg(src_bo), xe_bo_ccs_pages_start(src_bo), 952 PAGE_ALIGN(xe_device_ccs_bytes(xe, size)), 953 &ccs_it); 954 955 while (size) { 956 u32 batch_size = 1; /* MI_BATCH_BUFFER_END */ 957 struct xe_sched_job *job; 958 struct xe_bb *bb; 959 u32 flush_flags = 0; 960 u32 update_idx; 961 u64 ccs_ofs, ccs_size; 962 u32 ccs_pt; 963 u32 pte_flags; 964 965 bool usm = xe->info.has_usm; 966 u32 avail_pts = max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENCODE_SIZE; 967 968 src_L0 = xe_migrate_res_sizes(m, &src_it); 969 dst_L0 = xe_migrate_res_sizes(m, &dst_it); 970 971 drm_dbg(&xe->drm, "Pass %u, sizes: %llu & %llu\n", 972 pass++, src_L0, dst_L0); 973 974 src_L0 = min(src_L0, dst_L0); 975 976 pte_flags = src_is_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0; 977 pte_flags |= use_comp_pat ? PTE_UPDATE_FLAG_IS_COMP_PTE : 0; 978 batch_size += pte_update_size(m, pte_flags, src, &src_it, &src_L0, 979 &src_L0_ofs, &src_L0_pt, 0, 0, 980 avail_pts); 981 if (copy_only_ccs) { 982 dst_L0_ofs = src_L0_ofs; 983 } else { 984 pte_flags = dst_is_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0; 985 batch_size += pte_update_size(m, pte_flags, dst, 986 &dst_it, &src_L0, 987 &dst_L0_ofs, &dst_L0_pt, 988 0, avail_pts, avail_pts); 989 } 990 991 if (copy_system_ccs) { 992 xe_assert(xe, type_device); 993 ccs_size = xe_device_ccs_bytes(xe, src_L0); 994 batch_size += pte_update_size(m, 0, NULL, &ccs_it, &ccs_size, 995 &ccs_ofs, &ccs_pt, 0, 996 2 * avail_pts, 997 avail_pts); 998 xe_assert(xe, IS_ALIGNED(ccs_it.start, PAGE_SIZE)); 999 } 1000 1001 /* Add copy commands size here */ 1002 batch_size += ((copy_only_ccs) ? 0 : emit_copy_cmd_len(xe)) + 1003 ((needs_ccs_emit ? EMIT_COPY_CCS_DW : 0)); 1004 1005 bb = xe_bb_new(gt, batch_size, usm); 1006 if (IS_ERR(bb)) { 1007 err = PTR_ERR(bb); 1008 goto err_sync; 1009 } 1010 1011 if (src_is_vram && xe_migrate_allow_identity(src_L0, &src_it)) 1012 xe_res_next(&src_it, src_L0); 1013 else 1014 emit_pte(m, bb, src_L0_pt, src_is_vram, copy_system_ccs || use_comp_pat, 1015 &src_it, src_L0, src); 1016 1017 if (dst_is_vram && xe_migrate_allow_identity(src_L0, &dst_it)) 1018 xe_res_next(&dst_it, src_L0); 1019 else if (!copy_only_ccs) 1020 emit_pte(m, bb, dst_L0_pt, dst_is_vram, copy_system_ccs, 1021 &dst_it, src_L0, dst); 1022 1023 if (copy_system_ccs) 1024 emit_pte(m, bb, ccs_pt, false, false, &ccs_it, ccs_size, src); 1025 1026 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 1027 update_idx = bb->len; 1028 1029 if (!copy_only_ccs) 1030 emit_copy(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0, XE_PAGE_SIZE); 1031 1032 if (needs_ccs_emit) 1033 flush_flags = xe_migrate_ccs_copy(m, bb, src_L0_ofs, 1034 IS_DGFX(xe) ? src_is_vram : src_is_pltt, 1035 dst_L0_ofs, 1036 IS_DGFX(xe) ? dst_is_vram : dst_is_pltt, 1037 src_L0, ccs_ofs, copy_ccs); 1038 1039 job = xe_bb_create_migration_job(m->q, bb, 1040 xe_migrate_batch_base(m, usm), 1041 update_idx); 1042 if (IS_ERR(job)) { 1043 err = PTR_ERR(job); 1044 goto err; 1045 } 1046 1047 xe_sched_job_add_migrate_flush(job, flush_flags | MI_INVALIDATE_TLB); 1048 if (!fence) { 1049 err = xe_sched_job_add_deps(job, src_bo->ttm.base.resv, 1050 DMA_RESV_USAGE_BOOKKEEP); 1051 if (!err && src_bo->ttm.base.resv != dst_bo->ttm.base.resv) 1052 err = xe_sched_job_add_deps(job, dst_bo->ttm.base.resv, 1053 DMA_RESV_USAGE_BOOKKEEP); 1054 if (err) 1055 goto err_job; 1056 } 1057 1058 mutex_lock(&m->job_mutex); 1059 xe_sched_job_arm(job); 1060 dma_fence_put(fence); 1061 fence = dma_fence_get(&job->drm.s_fence->finished); 1062 xe_sched_job_push(job); 1063 1064 dma_fence_put(m->fence); 1065 m->fence = dma_fence_get(fence); 1066 1067 mutex_unlock(&m->job_mutex); 1068 1069 xe_bb_free(bb, fence); 1070 size -= src_L0; 1071 continue; 1072 1073 err_job: 1074 xe_sched_job_put(job); 1075 err: 1076 xe_bb_free(bb, NULL); 1077 1078 err_sync: 1079 /* Sync partial copy if any. FIXME: under job_mutex? */ 1080 if (fence) { 1081 dma_fence_wait(fence, false); 1082 dma_fence_put(fence); 1083 } 1084 1085 return ERR_PTR(err); 1086 } 1087 1088 return fence; 1089 } 1090 1091 /** 1092 * xe_migrate_copy() - Copy content of TTM resources. 1093 * @m: The migration context. 1094 * @src_bo: The buffer object @src is currently bound to. 1095 * @dst_bo: If copying between resources created for the same bo, set this to 1096 * the same value as @src_bo. If copying between buffer objects, set it to 1097 * the buffer object @dst is currently bound to. 1098 * @src: The source TTM resource. 1099 * @dst: The dst TTM resource. 1100 * @copy_only_ccs: If true copy only CCS metadata 1101 * 1102 * Copies the contents of @src to @dst: On flat CCS devices, 1103 * the CCS metadata is copied as well if needed, or if not present, 1104 * the CCS metadata of @dst is cleared for security reasons. 1105 * 1106 * Return: Pointer to a dma_fence representing the last copy batch, or 1107 * an error pointer on failure. If there is a failure, any copy operation 1108 * started by the function call has been synced. 1109 */ 1110 struct dma_fence *xe_migrate_copy(struct xe_migrate *m, 1111 struct xe_bo *src_bo, 1112 struct xe_bo *dst_bo, 1113 struct ttm_resource *src, 1114 struct ttm_resource *dst, 1115 bool copy_only_ccs) 1116 { 1117 return __xe_migrate_copy(m, src_bo, dst_bo, src, dst, copy_only_ccs, false); 1118 } 1119 1120 /** 1121 * xe_migrate_resolve() - Resolve and decompress a buffer object if required. 1122 * @m: The migrate context 1123 * @bo: The buffer object to resolve 1124 * @res: The reservation object 1125 * 1126 * Wrapper around __xe_migrate_copy() with is_vram_resolve set to true 1127 * to trigger decompression if needed. 1128 * 1129 * Return: A dma_fence that signals on completion, or an ERR_PTR on failure. 1130 */ 1131 struct dma_fence *xe_migrate_resolve(struct xe_migrate *m, 1132 struct xe_bo *bo, 1133 struct ttm_resource *res) 1134 { 1135 return __xe_migrate_copy(m, bo, bo, res, res, false, true); 1136 } 1137 1138 /** 1139 * xe_migrate_lrc() - Get the LRC from migrate context. 1140 * @migrate: Migrate context. 1141 * 1142 * Return: Pointer to LRC on success, error on failure 1143 */ 1144 struct xe_lrc *xe_migrate_lrc(struct xe_migrate *migrate) 1145 { 1146 return migrate->q->lrc[0]; 1147 } 1148 1149 static u64 migrate_vm_ppgtt_addr_tlb_inval(void) 1150 { 1151 /* 1152 * The migrate VM is self-referential so it can modify its own PTEs (see 1153 * pte_update_size() or emit_pte() functions). We reserve NUM_KERNEL_PDE 1154 * entries for kernel operations (copies, clears, CCS migrate), and 1155 * suballocate the rest to user operations (binds/unbinds). With 1156 * NUM_KERNEL_PDE = 15, NUM_KERNEL_PDE - 1 is already used for PTE updates, 1157 * so assign NUM_KERNEL_PDE - 2 for TLB invalidation. 1158 */ 1159 return (NUM_KERNEL_PDE - 2) * XE_PAGE_SIZE; 1160 } 1161 1162 static int emit_flush_invalidate(u32 *dw, int i, u32 flags) 1163 { 1164 u64 addr = migrate_vm_ppgtt_addr_tlb_inval(); 1165 1166 dw[i++] = MI_FLUSH_DW | MI_INVALIDATE_TLB | MI_FLUSH_DW_OP_STOREDW | 1167 MI_FLUSH_IMM_DW | flags; 1168 dw[i++] = lower_32_bits(addr); 1169 dw[i++] = upper_32_bits(addr); 1170 dw[i++] = MI_NOOP; 1171 dw[i++] = MI_NOOP; 1172 1173 return i; 1174 } 1175 1176 /** 1177 * xe_migrate_ccs_rw_copy() - Copy content of TTM resources. 1178 * @tile: Tile whose migration context to be used. 1179 * @q : Execution to be used along with migration context. 1180 * @src_bo: The buffer object @src is currently bound to. 1181 * @new_mem: The (not yet committed) destination resource @src_bo is being 1182 * moved into; src_bo->ttm.resource is still the old resource. 1183 * @read_write : Creates BB commands for CCS read/write. 1184 * 1185 * Creates batch buffer instructions to copy CCS metadata from CCS pool to 1186 * memory and vice versa. 1187 * 1188 * This function should only be called for IGPU. 1189 * 1190 * Return: 0 if successful, negative error code on failure. 1191 */ 1192 int xe_migrate_ccs_rw_copy(struct xe_tile *tile, struct xe_exec_queue *q, 1193 struct xe_bo *src_bo, 1194 struct ttm_resource *new_mem, 1195 enum xe_sriov_vf_ccs_rw_ctxs read_write) 1196 1197 { 1198 bool src_is_pltt = read_write == XE_SRIOV_VF_CCS_READ_CTX; 1199 bool dst_is_pltt = read_write == XE_SRIOV_VF_CCS_WRITE_CTX; 1200 struct ttm_resource *src = new_mem; 1201 struct xe_migrate *m = tile->migrate; 1202 struct xe_gt *gt = tile->primary_gt; 1203 u32 batch_size, batch_size_allocated; 1204 struct xe_device *xe = gt_to_xe(gt); 1205 struct xe_res_cursor src_it, ccs_it; 1206 struct xe_mem_pool *bb_pool; 1207 struct xe_sriov_vf_ccs_ctx *ctx; 1208 u64 size = xe_bo_size(src_bo); 1209 struct xe_mem_pool_node *bb; 1210 u64 src_L0, src_L0_ofs; 1211 struct xe_bb xe_bb_tmp; 1212 u32 src_L0_pt; 1213 int err; 1214 1215 ctx = &xe->sriov.vf.ccs.contexts[read_write]; 1216 1217 xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it); 1218 1219 xe_res_first_sg(xe_bo_sg(src_bo), xe_bo_ccs_pages_start(src_bo), 1220 PAGE_ALIGN(xe_device_ccs_bytes(xe, size)), 1221 &ccs_it); 1222 1223 /* Calculate Batch buffer size */ 1224 batch_size = 0; 1225 while (size) { 1226 batch_size += 10; /* Flush + ggtt addr + 2 NOP */ 1227 u64 ccs_ofs, ccs_size; 1228 u32 ccs_pt; 1229 1230 u32 avail_pts = max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENCODE_SIZE; 1231 1232 src_L0 = min_t(u64, max_mem_transfer_per_pass(xe), size); 1233 1234 batch_size += pte_update_size(m, false, src, &src_it, &src_L0, 1235 &src_L0_ofs, &src_L0_pt, 0, 0, 1236 avail_pts); 1237 1238 ccs_size = xe_device_ccs_bytes(xe, src_L0); 1239 batch_size += pte_update_size(m, 0, NULL, &ccs_it, &ccs_size, &ccs_ofs, 1240 &ccs_pt, 0, avail_pts, avail_pts); 1241 xe_assert(xe, IS_ALIGNED(ccs_it.start, PAGE_SIZE)); 1242 1243 /* Add copy commands size here */ 1244 batch_size += EMIT_COPY_CCS_DW; 1245 1246 size -= src_L0; 1247 } 1248 1249 bb = xe_mem_pool_alloc_node(); 1250 if (IS_ERR(bb)) 1251 return PTR_ERR(bb); 1252 1253 bb_pool = ctx->mem.ccs_bb_pool; 1254 scoped_guard(mutex, xe_mem_pool_bo_swap_guard(bb_pool)) { 1255 xe_mem_pool_swap_shadow_locked(bb_pool); 1256 1257 err = xe_mem_pool_insert_node(bb_pool, bb, batch_size * sizeof(u32)); 1258 if (err) { 1259 xe_gt_err(gt, "BB allocation failed.\n"); 1260 kfree(bb); 1261 return err; 1262 } 1263 1264 batch_size_allocated = batch_size; 1265 size = xe_bo_size(src_bo); 1266 batch_size = 0; 1267 1268 xe_bb_tmp = (struct xe_bb){ .cs = xe_mem_pool_node_cpu_addr(bb), .len = 0 }; 1269 /* 1270 * Emit PTE and copy commands here. 1271 * The CCS copy command can only support limited size. If the size to be 1272 * copied is more than the limit, divide copy into chunks. So, calculate 1273 * sizes here again before copy command is emitted. 1274 */ 1275 1276 while (size) { 1277 batch_size += 10; /* Flush + ggtt addr + 2 NOP */ 1278 u32 flush_flags = 0; 1279 u64 ccs_ofs, ccs_size; 1280 u32 ccs_pt; 1281 1282 u32 avail_pts = max_mem_transfer_per_pass(xe) / 1283 LEVEL0_PAGE_TABLE_ENCODE_SIZE; 1284 1285 src_L0 = xe_migrate_res_sizes(m, &src_it); 1286 1287 batch_size += pte_update_size(m, false, src, &src_it, &src_L0, 1288 &src_L0_ofs, &src_L0_pt, 0, 0, 1289 avail_pts); 1290 1291 ccs_size = xe_device_ccs_bytes(xe, src_L0); 1292 batch_size += pte_update_size(m, 0, NULL, &ccs_it, &ccs_size, &ccs_ofs, 1293 &ccs_pt, 0, avail_pts, avail_pts); 1294 xe_assert(xe, IS_ALIGNED(ccs_it.start, PAGE_SIZE)); 1295 batch_size += EMIT_COPY_CCS_DW; 1296 1297 emit_pte(m, &xe_bb_tmp, src_L0_pt, false, true, &src_it, src_L0, src); 1298 1299 emit_pte(m, &xe_bb_tmp, ccs_pt, false, false, &ccs_it, ccs_size, src); 1300 1301 xe_bb_tmp.len = emit_flush_invalidate(xe_bb_tmp.cs, xe_bb_tmp.len, 1302 flush_flags); 1303 flush_flags = xe_migrate_ccs_copy(m, &xe_bb_tmp, src_L0_ofs, src_is_pltt, 1304 src_L0_ofs, dst_is_pltt, 1305 src_L0, ccs_ofs, true); 1306 xe_bb_tmp.len = emit_flush_invalidate(xe_bb_tmp.cs, xe_bb_tmp.len, 1307 flush_flags); 1308 1309 size -= src_L0; 1310 } 1311 1312 xe_assert(xe, (batch_size_allocated == xe_bb_tmp.len)); 1313 xe_assert(xe, bb->sa_node.size == xe_bb_tmp.len * sizeof(u32)); 1314 src_bo->bb_ccs[read_write] = bb; 1315 1316 xe_sriov_vf_ccs_rw_update_bb_addr(ctx); 1317 xe_mem_pool_sync_shadow_locked(bb); 1318 } 1319 1320 return 0; 1321 } 1322 1323 /** 1324 * xe_migrate_ccs_rw_copy_clear() - Clear the CCS read/write batch buffer 1325 * content. 1326 * @src_bo: The buffer object @src is currently bound to. 1327 * @read_write : Creates BB commands for CCS read/write. 1328 * @bound: Device is bound 1329 * 1330 * Directly clearing the BB lacks atomicity and can lead to undefined 1331 * behavior if the vCPU is halted mid-operation during the clearing 1332 * process. To avoid this issue, we use a shadow buffer object approach. 1333 * 1334 * First swap the SA BO address with the shadow BO, perform the clearing 1335 * operation on the BB, update the shadow BO in the ring buffer, then 1336 * sync the shadow and the actual buffer to maintain consistency. 1337 * 1338 * Returns: None. 1339 */ 1340 void xe_migrate_ccs_rw_copy_clear(struct xe_bo *src_bo, 1341 enum xe_sriov_vf_ccs_rw_ctxs read_write, 1342 bool bound) 1343 { 1344 struct xe_mem_pool_node *bb = src_bo->bb_ccs[read_write]; 1345 struct xe_device *xe = xe_bo_device(src_bo); 1346 struct xe_mem_pool *bb_pool; 1347 struct xe_sriov_vf_ccs_ctx *ctx; 1348 u32 *cs; 1349 1350 xe_assert(xe, IS_SRIOV_VF(xe)); 1351 1352 ctx = &xe->sriov.vf.ccs.contexts[read_write]; 1353 bb_pool = ctx->mem.ccs_bb_pool; 1354 1355 scoped_guard(mutex, xe_mem_pool_bo_swap_guard(bb_pool)) { 1356 if (bound) { 1357 xe_mem_pool_swap_shadow_locked(bb_pool); 1358 1359 cs = xe_mem_pool_node_cpu_addr(bb); 1360 memset(cs, MI_NOOP, bb->sa_node.size); 1361 xe_sriov_vf_ccs_rw_update_bb_addr(ctx); 1362 1363 xe_mem_pool_sync_shadow_locked(bb); 1364 } 1365 xe_mem_pool_free_node(bb); 1366 src_bo->bb_ccs[read_write] = NULL; 1367 } 1368 } 1369 1370 /** 1371 * xe_migrate_exec_queue() - Get the execution queue from migrate context. 1372 * @migrate: Migrate context. 1373 * 1374 * Return: Pointer to execution queue on success, error on failure 1375 */ 1376 struct xe_exec_queue *xe_migrate_exec_queue(struct xe_migrate *migrate) 1377 { 1378 return migrate->q; 1379 } 1380 1381 /** 1382 * xe_migrate_vram_copy_chunk() - Copy a chunk of a VRAM buffer object. 1383 * @vram_bo: The VRAM buffer object. 1384 * @vram_offset: The VRAM offset. 1385 * @sysmem_bo: The sysmem buffer object. 1386 * @sysmem_offset: The sysmem offset. 1387 * @size: The size of VRAM chunk to copy. 1388 * @dir: The direction of the copy operation. 1389 * 1390 * Copies a portion of a buffer object between VRAM and system memory. 1391 * On Xe2 platforms that support flat CCS, VRAM data is decompressed when 1392 * copying to system memory. 1393 * 1394 * Return: Pointer to a dma_fence representing the last copy batch, or 1395 * an error pointer on failure. If there is a failure, any copy operation 1396 * started by the function call has been synced. 1397 */ 1398 struct dma_fence *xe_migrate_vram_copy_chunk(struct xe_bo *vram_bo, u64 vram_offset, 1399 struct xe_bo *sysmem_bo, u64 sysmem_offset, 1400 u64 size, enum xe_migrate_copy_dir dir) 1401 { 1402 struct xe_device *xe = xe_bo_device(vram_bo); 1403 struct xe_tile *tile = vram_bo->tile; 1404 struct xe_gt *gt = tile->primary_gt; 1405 struct xe_migrate *m = tile->migrate; 1406 struct dma_fence *fence = NULL; 1407 struct ttm_resource *vram = vram_bo->ttm.resource; 1408 struct ttm_resource *sysmem = sysmem_bo->ttm.resource; 1409 struct xe_res_cursor vram_it, sysmem_it; 1410 u64 vram_L0_ofs, sysmem_L0_ofs; 1411 u32 vram_L0_pt, sysmem_L0_pt; 1412 u64 vram_L0, sysmem_L0; 1413 bool to_sysmem = (dir == XE_MIGRATE_COPY_TO_SRAM); 1414 bool use_comp_pat = to_sysmem && 1415 GRAPHICS_VER(xe) >= 20 && xe_device_has_flat_ccs(xe); 1416 int pass = 0; 1417 int err; 1418 1419 xe_assert(xe, IS_ALIGNED(vram_offset | sysmem_offset | size, PAGE_SIZE)); 1420 xe_assert(xe, xe_bo_is_vram(vram_bo)); 1421 xe_assert(xe, !xe_bo_is_vram(sysmem_bo)); 1422 xe_assert(xe, !range_overflows(vram_offset, size, (u64)vram_bo->ttm.base.size)); 1423 xe_assert(xe, !range_overflows(sysmem_offset, size, (u64)sysmem_bo->ttm.base.size)); 1424 1425 xe_res_first(vram, vram_offset, size, &vram_it); 1426 xe_res_first_sg(xe_bo_sg(sysmem_bo), sysmem_offset, size, &sysmem_it); 1427 1428 while (size) { 1429 u32 pte_flags = PTE_UPDATE_FLAG_IS_VRAM; 1430 u32 batch_size = 2; /* arb_clear() + MI_BATCH_BUFFER_END */ 1431 struct xe_sched_job *job; 1432 struct xe_bb *bb; 1433 u32 update_idx; 1434 bool usm = xe->info.has_usm; 1435 u32 avail_pts = max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENCODE_SIZE; 1436 1437 sysmem_L0 = xe_migrate_res_sizes(m, &sysmem_it); 1438 vram_L0 = min(xe_migrate_res_sizes(m, &vram_it), sysmem_L0); 1439 1440 xe_dbg(xe, "Pass %u, size: %llu\n", pass++, vram_L0); 1441 1442 pte_flags |= use_comp_pat ? PTE_UPDATE_FLAG_IS_COMP_PTE : 0; 1443 batch_size += pte_update_size(m, pte_flags, vram, &vram_it, &vram_L0, 1444 &vram_L0_ofs, &vram_L0_pt, 0, 0, avail_pts); 1445 1446 batch_size += pte_update_size(m, 0, sysmem, &sysmem_it, &vram_L0, &sysmem_L0_ofs, 1447 &sysmem_L0_pt, 0, avail_pts, avail_pts); 1448 batch_size += emit_copy_cmd_len(xe); 1449 1450 bb = xe_bb_new(gt, batch_size, usm); 1451 if (IS_ERR(bb)) { 1452 err = PTR_ERR(bb); 1453 return ERR_PTR(err); 1454 } 1455 1456 if (xe_migrate_allow_identity(vram_L0, &vram_it)) 1457 xe_res_next(&vram_it, vram_L0); 1458 else 1459 emit_pte(m, bb, vram_L0_pt, true, use_comp_pat, &vram_it, vram_L0, vram); 1460 1461 emit_pte(m, bb, sysmem_L0_pt, false, false, &sysmem_it, vram_L0, sysmem); 1462 1463 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 1464 update_idx = bb->len; 1465 1466 if (to_sysmem) 1467 emit_copy(gt, bb, vram_L0_ofs, sysmem_L0_ofs, vram_L0, XE_PAGE_SIZE); 1468 else 1469 emit_copy(gt, bb, sysmem_L0_ofs, vram_L0_ofs, vram_L0, XE_PAGE_SIZE); 1470 1471 job = xe_bb_create_migration_job(m->q, bb, xe_migrate_batch_base(m, usm), 1472 update_idx); 1473 if (IS_ERR(job)) { 1474 xe_bb_free(bb, NULL); 1475 err = PTR_ERR(job); 1476 return ERR_PTR(err); 1477 } 1478 1479 xe_sched_job_add_migrate_flush(job, MI_INVALIDATE_TLB); 1480 1481 xe_assert(xe, dma_resv_test_signaled(vram_bo->ttm.base.resv, 1482 DMA_RESV_USAGE_BOOKKEEP)); 1483 xe_assert(xe, dma_resv_test_signaled(sysmem_bo->ttm.base.resv, 1484 DMA_RESV_USAGE_BOOKKEEP)); 1485 1486 scoped_guard(mutex, &m->job_mutex) { 1487 xe_sched_job_arm(job); 1488 dma_fence_put(fence); 1489 fence = dma_fence_get(&job->drm.s_fence->finished); 1490 xe_sched_job_push(job); 1491 1492 dma_fence_put(m->fence); 1493 m->fence = dma_fence_get(fence); 1494 } 1495 1496 xe_bb_free(bb, fence); 1497 size -= vram_L0; 1498 } 1499 1500 return fence; 1501 } 1502 1503 static u32 blt_mem_set_cmd_len(struct xe_device *xe) 1504 { 1505 return 7; 1506 } 1507 1508 static void emit_clear_link_copy(struct xe_gt *gt, struct xe_bb *bb, u64 src_ofs, 1509 u32 size, u32 pitch) 1510 { 1511 struct xe_device *xe = gt_to_xe(gt); 1512 u32 *cs = bb->cs + bb->len; 1513 u32 len = blt_mem_set_cmd_len(xe); 1514 1515 *cs++ = PVC_MEM_SET_CMD | PVC_MEM_SET_MATRIX | (len - 2); 1516 *cs++ = pitch - 1; 1517 *cs++ = (size / pitch) - 1; 1518 *cs++ = pitch - 1; 1519 *cs++ = lower_32_bits(src_ofs); 1520 *cs++ = upper_32_bits(src_ofs); 1521 if (GRAPHICS_VERx100(xe) >= 2000) 1522 *cs++ = FIELD_PREP(XE2_MEM_SET_MOCS_INDEX_MASK, gt->mocs.uc_index); 1523 else 1524 *cs++ = FIELD_PREP(PVC_MEM_SET_MOCS_INDEX_MASK, gt->mocs.uc_index); 1525 1526 xe_gt_assert(gt, cs - bb->cs == len + bb->len); 1527 1528 bb->len += len; 1529 } 1530 1531 static u32 blt_fast_color_cmd_len(struct xe_device *xe) 1532 { 1533 if (GRAPHICS_VERx100(xe) >= 1250) 1534 return 16; 1535 else 1536 return 11; 1537 } 1538 1539 static void emit_clear_main_copy(struct xe_gt *gt, struct xe_bb *bb, 1540 u64 src_ofs, u32 size, u32 pitch, bool is_vram) 1541 { 1542 struct xe_device *xe = gt_to_xe(gt); 1543 u32 *cs = bb->cs + bb->len; 1544 u32 len = blt_fast_color_cmd_len(xe); 1545 1546 1547 *cs++ = XY_FAST_COLOR_BLT_CMD | XY_FAST_COLOR_BLT_DEPTH_32 | 1548 (len - 2); 1549 if (GRAPHICS_VERx100(xe) >= 2000) 1550 *cs++ = FIELD_PREP(XE2_XY_FAST_COLOR_BLT_MOCS_INDEX_MASK, gt->mocs.uc_index) | 1551 (pitch - 1); 1552 else 1553 *cs++ = FIELD_PREP(XY_FAST_COLOR_BLT_MOCS_MASK, gt->mocs.uc_index) | 1554 (pitch - 1); 1555 *cs++ = 0; 1556 *cs++ = (size / pitch) << 16 | pitch / 4; 1557 *cs++ = lower_32_bits(src_ofs); 1558 *cs++ = upper_32_bits(src_ofs); 1559 *cs++ = (is_vram ? 0x0 : 0x1) << XY_FAST_COLOR_BLT_MEM_TYPE_SHIFT; 1560 *cs++ = 0; 1561 *cs++ = 0; 1562 *cs++ = 0; 1563 *cs++ = 0; 1564 1565 if (len > 11) { 1566 *cs++ = 0; 1567 *cs++ = 0; 1568 *cs++ = 0; 1569 *cs++ = 0; 1570 *cs++ = 0; 1571 } 1572 1573 xe_gt_assert(gt, cs - bb->cs == len + bb->len); 1574 1575 bb->len += len; 1576 } 1577 1578 static u32 emit_clear_cmd_len(struct xe_gt *gt) 1579 { 1580 struct xe_device *xe = gt_to_xe(gt); 1581 1582 if (gt->info.has_xe2_blt_instructions) 1583 return blt_mem_set_cmd_len(xe); 1584 else 1585 return blt_fast_color_cmd_len(xe); 1586 } 1587 1588 static void emit_clear(struct xe_gt *gt, struct xe_bb *bb, u64 src_ofs, 1589 u32 size, u32 pitch, bool is_vram) 1590 { 1591 if (gt->info.has_xe2_blt_instructions) 1592 emit_clear_link_copy(gt, bb, src_ofs, size, pitch); 1593 else 1594 emit_clear_main_copy(gt, bb, src_ofs, size, pitch, 1595 is_vram); 1596 } 1597 1598 /** 1599 * xe_migrate_clear() - Copy content of TTM resources. 1600 * @m: The migration context. 1601 * @bo: The buffer object @dst is currently bound to. 1602 * @dst: The dst TTM resource to be cleared. 1603 * @clear_flags: flags to specify which data to clear: CCS, BO, or both. 1604 * 1605 * Clear the contents of @dst to zero when XE_MIGRATE_CLEAR_FLAG_BO_DATA is set. 1606 * On flat CCS devices, the CCS metadata is cleared to zero with XE_MIGRATE_CLEAR_FLAG_CCS_DATA. 1607 * Set XE_MIGRATE_CLEAR_FLAG_FULL to clear bo as well as CCS metadata. 1608 * TODO: Eliminate the @bo argument. 1609 * 1610 * Return: Pointer to a dma_fence representing the last clear batch, or 1611 * an error pointer on failure. If there is a failure, any clear operation 1612 * started by the function call has been synced. 1613 */ 1614 struct dma_fence *xe_migrate_clear(struct xe_migrate *m, 1615 struct xe_bo *bo, 1616 struct ttm_resource *dst, 1617 u32 clear_flags) 1618 { 1619 bool clear_vram = mem_type_is_vram(dst->mem_type); 1620 bool clear_bo_data = XE_MIGRATE_CLEAR_FLAG_BO_DATA & clear_flags; 1621 bool clear_ccs = XE_MIGRATE_CLEAR_FLAG_CCS_DATA & clear_flags; 1622 struct xe_gt *gt = m->tile->primary_gt; 1623 struct xe_device *xe = gt_to_xe(gt); 1624 bool clear_only_system_ccs = false; 1625 struct dma_fence *fence = NULL; 1626 u64 size = xe_bo_size(bo); 1627 struct xe_res_cursor src_it; 1628 struct ttm_resource *src = dst; 1629 int err; 1630 1631 if (WARN_ON(!clear_bo_data && !clear_ccs)) 1632 return NULL; 1633 1634 if (!clear_bo_data && clear_ccs && !IS_DGFX(xe)) 1635 clear_only_system_ccs = true; 1636 1637 if (!clear_vram) 1638 xe_res_first_sg(xe_bo_sg(bo), 0, xe_bo_size(bo), &src_it); 1639 else 1640 xe_res_first(src, 0, xe_bo_size(bo), &src_it); 1641 1642 while (size) { 1643 u64 clear_L0_ofs; 1644 u32 clear_L0_pt; 1645 u32 flush_flags = 0; 1646 u64 clear_L0; 1647 struct xe_sched_job *job; 1648 struct xe_bb *bb; 1649 u32 batch_size, update_idx; 1650 u32 pte_flags; 1651 1652 bool usm = xe->info.has_usm; 1653 u32 avail_pts = max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENCODE_SIZE; 1654 1655 clear_L0 = xe_migrate_res_sizes(m, &src_it); 1656 1657 /* Calculate final sizes and batch size.. */ 1658 pte_flags = clear_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0; 1659 batch_size = 1 + 1660 pte_update_size(m, pte_flags, src, &src_it, 1661 &clear_L0, &clear_L0_ofs, &clear_L0_pt, 1662 clear_bo_data ? emit_clear_cmd_len(gt) : 0, 0, 1663 avail_pts); 1664 1665 if (xe_migrate_needs_ccs_emit(xe)) 1666 batch_size += EMIT_COPY_CCS_DW; 1667 1668 /* Clear commands */ 1669 1670 if (WARN_ON_ONCE(!clear_L0)) 1671 break; 1672 1673 bb = xe_bb_new(gt, batch_size, usm); 1674 if (IS_ERR(bb)) { 1675 err = PTR_ERR(bb); 1676 goto err_sync; 1677 } 1678 1679 size -= clear_L0; 1680 /* Preemption is enabled again by the ring ops. */ 1681 if (clear_vram && xe_migrate_allow_identity(clear_L0, &src_it)) { 1682 xe_res_next(&src_it, clear_L0); 1683 } else { 1684 emit_pte(m, bb, clear_L0_pt, clear_vram, 1685 clear_only_system_ccs, &src_it, clear_L0, dst); 1686 flush_flags |= MI_INVALIDATE_TLB; 1687 } 1688 1689 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 1690 update_idx = bb->len; 1691 1692 if (clear_bo_data) 1693 emit_clear(gt, bb, clear_L0_ofs, clear_L0, XE_PAGE_SIZE, clear_vram); 1694 1695 if (xe_migrate_needs_ccs_emit(xe)) { 1696 emit_copy_ccs(gt, bb, clear_L0_ofs, true, 1697 m->cleared_mem_ofs, false, clear_L0); 1698 flush_flags |= MI_FLUSH_DW_CCS; 1699 } 1700 1701 job = xe_bb_create_migration_job(m->q, bb, 1702 xe_migrate_batch_base(m, usm), 1703 update_idx); 1704 if (IS_ERR(job)) { 1705 err = PTR_ERR(job); 1706 goto err; 1707 } 1708 1709 xe_sched_job_add_migrate_flush(job, flush_flags); 1710 if (!fence) { 1711 /* 1712 * There can't be anything userspace related at this 1713 * point, so we just need to respect any potential move 1714 * fences, which are always tracked as 1715 * DMA_RESV_USAGE_KERNEL. 1716 */ 1717 err = xe_sched_job_add_deps(job, bo->ttm.base.resv, 1718 DMA_RESV_USAGE_KERNEL); 1719 if (err) 1720 goto err_job; 1721 } 1722 1723 mutex_lock(&m->job_mutex); 1724 xe_sched_job_arm(job); 1725 dma_fence_put(fence); 1726 fence = dma_fence_get(&job->drm.s_fence->finished); 1727 xe_sched_job_push(job); 1728 1729 dma_fence_put(m->fence); 1730 m->fence = dma_fence_get(fence); 1731 1732 mutex_unlock(&m->job_mutex); 1733 1734 xe_bb_free(bb, fence); 1735 continue; 1736 1737 err_job: 1738 xe_sched_job_put(job); 1739 err: 1740 xe_bb_free(bb, NULL); 1741 err_sync: 1742 /* Sync partial copies if any. FIXME: job_mutex? */ 1743 if (fence) { 1744 dma_fence_wait(fence, false); 1745 dma_fence_put(fence); 1746 } 1747 1748 return ERR_PTR(err); 1749 } 1750 1751 if (clear_ccs) 1752 bo->ccs_cleared = true; 1753 1754 return fence; 1755 } 1756 1757 static void write_pgtable(struct xe_tile *tile, struct xe_bb *bb, u64 ppgtt_ofs, 1758 const struct xe_vm_pgtable_update_op *pt_op, 1759 const struct xe_vm_pgtable_update *update, 1760 struct xe_migrate_pt_update *pt_update) 1761 { 1762 const struct xe_migrate_pt_update_ops *ops = pt_update->ops; 1763 u32 chunk; 1764 u32 ofs = update->ofs, size = update->qwords; 1765 1766 /* 1767 * If we have 512 entries (max), we would populate it ourselves, 1768 * and update the PDE above it to the new pointer. 1769 * The only time this can only happen if we have to update the top 1770 * PDE. This requires a BO that is almost vm->size big. 1771 * 1772 * This shouldn't be possible in practice.. might change when 16K 1773 * pages are used. Hence the assert. 1774 */ 1775 xe_tile_assert(tile, update->qwords < MAX_NUM_PTE); 1776 if (!ppgtt_ofs) 1777 ppgtt_ofs = xe_migrate_vram_ofs(tile_to_xe(tile), 1778 xe_bo_addr(update->pt_bo, 0, 1779 XE_PAGE_SIZE), false); 1780 1781 do { 1782 u64 addr = ppgtt_ofs + ofs * 8; 1783 1784 chunk = min(size, MAX_PTE_PER_SDI); 1785 1786 /* Ensure populatefn can do memset64 by aligning bb->cs */ 1787 if (!(bb->len & 1)) 1788 bb->cs[bb->len++] = MI_NOOP; 1789 1790 bb->cs[bb->len++] = MI_STORE_DATA_IMM | MI_SDI_NUM_QW(chunk); 1791 bb->cs[bb->len++] = lower_32_bits(addr); 1792 bb->cs[bb->len++] = upper_32_bits(addr); 1793 if (pt_op->bind) 1794 ops->populate(pt_update, tile, NULL, bb->cs + bb->len, 1795 ofs, chunk, update); 1796 else 1797 ops->clear(pt_update, tile, NULL, bb->cs + bb->len, 1798 ofs, chunk, update); 1799 1800 bb->len += chunk * 2; 1801 ofs += chunk; 1802 size -= chunk; 1803 } while (size); 1804 } 1805 1806 struct xe_vm *xe_migrate_get_vm(struct xe_migrate *m) 1807 { 1808 return xe_vm_get(m->q->vm); 1809 } 1810 1811 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST) 1812 struct migrate_test_params { 1813 struct xe_test_priv base; 1814 bool force_gpu; 1815 }; 1816 1817 #define to_migrate_test_params(_priv) \ 1818 container_of(_priv, struct migrate_test_params, base) 1819 #endif 1820 1821 static struct dma_fence * 1822 xe_migrate_update_pgtables_cpu(struct xe_migrate *m, 1823 struct xe_migrate_pt_update *pt_update) 1824 { 1825 XE_TEST_DECLARE(struct migrate_test_params *test = 1826 to_migrate_test_params 1827 (xe_cur_kunit_priv(XE_TEST_LIVE_MIGRATE));) 1828 const struct xe_migrate_pt_update_ops *ops = pt_update->ops; 1829 struct xe_vm *vm = pt_update->vops->vm; 1830 struct xe_vm_pgtable_update_ops *pt_update_ops = 1831 &pt_update->vops->pt_update_ops[pt_update->tile_id]; 1832 int err; 1833 u32 i, j; 1834 1835 if (XE_TEST_ONLY(test && test->force_gpu)) 1836 return ERR_PTR(-ETIME); 1837 1838 if (ops->pre_commit) { 1839 pt_update->job = NULL; 1840 err = ops->pre_commit(pt_update); 1841 if (err) 1842 return ERR_PTR(err); 1843 } 1844 1845 for (i = 0; i < pt_update_ops->num_ops; ++i) { 1846 const struct xe_vm_pgtable_update_op *pt_op = 1847 &pt_update_ops->ops[i]; 1848 1849 for (j = 0; j < pt_op->num_entries; j++) { 1850 const struct xe_vm_pgtable_update *update = 1851 &pt_op->entries[j]; 1852 1853 if (pt_op->bind) 1854 ops->populate(pt_update, m->tile, 1855 &update->pt_bo->vmap, NULL, 1856 update->ofs, update->qwords, 1857 update); 1858 else 1859 ops->clear(pt_update, m->tile, 1860 &update->pt_bo->vmap, NULL, 1861 update->ofs, update->qwords, update); 1862 } 1863 } 1864 1865 trace_xe_vm_cpu_bind(vm); 1866 xe_device_wmb(vm->xe); 1867 1868 return dma_fence_get_stub(); 1869 } 1870 1871 static struct dma_fence * 1872 __xe_migrate_update_pgtables(struct xe_migrate *m, 1873 struct xe_migrate_pt_update *pt_update, 1874 struct xe_vm_pgtable_update_ops *pt_update_ops) 1875 { 1876 const struct xe_migrate_pt_update_ops *ops = pt_update->ops; 1877 struct xe_tile *tile = m->tile; 1878 struct xe_gt *gt = tile->primary_gt; 1879 struct xe_device *xe = tile_to_xe(tile); 1880 struct xe_sched_job *job; 1881 struct dma_fence *fence; 1882 struct drm_suballoc *sa_bo = NULL; 1883 struct xe_bb *bb; 1884 u32 i, j, batch_size = 0, ppgtt_ofs, update_idx, page_ofs = 0; 1885 u32 num_updates = 0, current_update = 0; 1886 u64 addr; 1887 int err = 0; 1888 bool is_migrate = pt_update_ops->q == m->q; 1889 bool usm = is_migrate && xe->info.has_usm; 1890 1891 for (i = 0; i < pt_update_ops->num_ops; ++i) { 1892 struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[i]; 1893 struct xe_vm_pgtable_update *updates = pt_op->entries; 1894 1895 num_updates += pt_op->num_entries; 1896 for (j = 0; j < pt_op->num_entries; ++j) { 1897 u32 num_cmds = DIV_ROUND_UP(updates[j].qwords, 1898 MAX_PTE_PER_SDI); 1899 1900 /* align noop + MI_STORE_DATA_IMM cmd prefix */ 1901 batch_size += 4 * num_cmds + updates[j].qwords * 2; 1902 } 1903 } 1904 1905 /* fixed + PTE entries */ 1906 if (IS_DGFX(xe)) 1907 batch_size += 2; 1908 else 1909 batch_size += 6 * (num_updates / MAX_PTE_PER_SDI + 1) + 1910 num_updates * 2; 1911 1912 bb = xe_bb_new(gt, batch_size, usm); 1913 if (IS_ERR(bb)) 1914 return ERR_CAST(bb); 1915 1916 /* For sysmem PTE's, need to map them in our hole.. */ 1917 if (!IS_DGFX(xe)) { 1918 u16 pat_index = xe_cache_pat_idx(xe, XE_CACHE_WB); 1919 u32 ptes, ofs; 1920 1921 ppgtt_ofs = NUM_KERNEL_PDE - 1; 1922 if (!is_migrate) { 1923 u32 num_units = DIV_ROUND_UP(num_updates, 1924 NUM_VMUSA_WRITES_PER_UNIT); 1925 1926 if (num_units > m->vm_update_sa.size) { 1927 err = -ENOBUFS; 1928 goto err_bb; 1929 } 1930 sa_bo = drm_suballoc_new(&m->vm_update_sa, num_units, 1931 GFP_KERNEL, true, 0); 1932 if (IS_ERR(sa_bo)) { 1933 err = PTR_ERR(sa_bo); 1934 goto err_bb; 1935 } 1936 1937 ppgtt_ofs = NUM_KERNEL_PDE + 1938 (drm_suballoc_soffset(sa_bo) / 1939 NUM_VMUSA_UNIT_PER_PAGE); 1940 page_ofs = (drm_suballoc_soffset(sa_bo) % 1941 NUM_VMUSA_UNIT_PER_PAGE) * 1942 VM_SA_UPDATE_UNIT_SIZE; 1943 } 1944 1945 /* Map our PT's to gtt */ 1946 i = 0; 1947 j = 0; 1948 ptes = num_updates; 1949 ofs = ppgtt_ofs * XE_PAGE_SIZE + page_ofs; 1950 while (ptes) { 1951 u32 chunk = min(MAX_PTE_PER_SDI, ptes); 1952 u32 idx = 0; 1953 1954 bb->cs[bb->len++] = MI_STORE_DATA_IMM | 1955 MI_SDI_NUM_QW(chunk); 1956 bb->cs[bb->len++] = ofs; 1957 bb->cs[bb->len++] = 0; /* upper_32_bits */ 1958 1959 for (; i < pt_update_ops->num_ops; ++i) { 1960 struct xe_vm_pgtable_update_op *pt_op = 1961 &pt_update_ops->ops[i]; 1962 struct xe_vm_pgtable_update *updates = pt_op->entries; 1963 1964 for (; j < pt_op->num_entries; ++j, ++current_update, ++idx) { 1965 struct xe_vm *vm = pt_update->vops->vm; 1966 struct xe_bo *pt_bo = updates[j].pt_bo; 1967 1968 if (idx == chunk) 1969 goto next_cmd; 1970 1971 xe_tile_assert(tile, xe_bo_size(pt_bo) == SZ_4K); 1972 1973 /* Map a PT at most once */ 1974 if (pt_bo->update_index < 0) 1975 pt_bo->update_index = current_update; 1976 1977 addr = vm->pt_ops->pte_encode_bo(pt_bo, 0, 1978 pat_index, 0); 1979 bb->cs[bb->len++] = lower_32_bits(addr); 1980 bb->cs[bb->len++] = upper_32_bits(addr); 1981 } 1982 1983 j = 0; 1984 } 1985 1986 next_cmd: 1987 ptes -= chunk; 1988 ofs += chunk * sizeof(u64); 1989 } 1990 1991 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 1992 update_idx = bb->len; 1993 1994 addr = xe_migrate_vm_addr(ppgtt_ofs, 0) + 1995 (page_ofs / sizeof(u64)) * XE_PAGE_SIZE; 1996 for (i = 0; i < pt_update_ops->num_ops; ++i) { 1997 struct xe_vm_pgtable_update_op *pt_op = 1998 &pt_update_ops->ops[i]; 1999 struct xe_vm_pgtable_update *updates = pt_op->entries; 2000 2001 for (j = 0; j < pt_op->num_entries; ++j) { 2002 struct xe_bo *pt_bo = updates[j].pt_bo; 2003 2004 write_pgtable(tile, bb, addr + 2005 pt_bo->update_index * XE_PAGE_SIZE, 2006 pt_op, &updates[j], pt_update); 2007 } 2008 } 2009 } else { 2010 /* phys pages, no preamble required */ 2011 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 2012 update_idx = bb->len; 2013 2014 for (i = 0; i < pt_update_ops->num_ops; ++i) { 2015 struct xe_vm_pgtable_update_op *pt_op = 2016 &pt_update_ops->ops[i]; 2017 struct xe_vm_pgtable_update *updates = pt_op->entries; 2018 2019 for (j = 0; j < pt_op->num_entries; ++j) 2020 write_pgtable(tile, bb, 0, pt_op, &updates[j], 2021 pt_update); 2022 } 2023 } 2024 2025 job = xe_bb_create_migration_job(pt_update_ops->q, bb, 2026 xe_migrate_batch_base(m, usm), 2027 update_idx); 2028 if (IS_ERR(job)) { 2029 err = PTR_ERR(job); 2030 goto err_sa; 2031 } 2032 2033 xe_sched_job_add_migrate_flush(job, MI_INVALIDATE_TLB); 2034 2035 if (ops->pre_commit) { 2036 pt_update->job = job; 2037 err = ops->pre_commit(pt_update); 2038 if (err) 2039 goto err_job; 2040 } 2041 if (is_migrate) 2042 mutex_lock(&m->job_mutex); 2043 2044 xe_sched_job_arm(job); 2045 fence = dma_fence_get(&job->drm.s_fence->finished); 2046 xe_sched_job_push(job); 2047 2048 if (is_migrate) 2049 mutex_unlock(&m->job_mutex); 2050 2051 xe_bb_free(bb, fence); 2052 drm_suballoc_free(sa_bo, fence); 2053 2054 return fence; 2055 2056 err_job: 2057 xe_sched_job_put(job); 2058 err_sa: 2059 drm_suballoc_free(sa_bo, NULL); 2060 err_bb: 2061 xe_bb_free(bb, NULL); 2062 return ERR_PTR(err); 2063 } 2064 2065 /** 2066 * xe_migrate_update_pgtables() - Pipelined page-table update 2067 * @m: The migrate context. 2068 * @pt_update: PT update arguments 2069 * 2070 * Perform a pipelined page-table update. The update descriptors are typically 2071 * built under the same lock critical section as a call to this function. If 2072 * using the default engine for the updates, they will be performed in the 2073 * order they grab the job_mutex. If different engines are used, external 2074 * synchronization is needed for overlapping updates to maintain page-table 2075 * consistency. Note that the meaning of "overlapping" is that the updates 2076 * touch the same page-table, which might be a higher-level page-directory. 2077 * If no pipelining is needed, then updates may be performed by the cpu. 2078 * 2079 * Return: A dma_fence that, when signaled, indicates the update completion. 2080 */ 2081 struct dma_fence * 2082 xe_migrate_update_pgtables(struct xe_migrate *m, 2083 struct xe_migrate_pt_update *pt_update) 2084 2085 { 2086 struct xe_vm_pgtable_update_ops *pt_update_ops = 2087 &pt_update->vops->pt_update_ops[pt_update->tile_id]; 2088 struct dma_fence *fence; 2089 2090 fence = xe_migrate_update_pgtables_cpu(m, pt_update); 2091 2092 /* -ETIME indicates a job is needed, anything else is legit error */ 2093 if (!IS_ERR(fence) || PTR_ERR(fence) != -ETIME) 2094 return fence; 2095 2096 return __xe_migrate_update_pgtables(m, pt_update, pt_update_ops); 2097 } 2098 2099 /** 2100 * xe_migrate_wait() - Complete all operations using the xe_migrate context 2101 * @m: Migrate context to wait for. 2102 * 2103 * Waits until the GPU no longer uses the migrate context's default engine 2104 * or its page-table objects. FIXME: What about separate page-table update 2105 * engines? 2106 */ 2107 void xe_migrate_wait(struct xe_migrate *m) 2108 { 2109 if (m->fence) 2110 dma_fence_wait(m->fence, false); 2111 } 2112 2113 static u32 pte_update_cmd_size(u64 size) 2114 { 2115 u32 num_dword; 2116 u64 entries = DIV_U64_ROUND_UP(size, XE_PAGE_SIZE); 2117 2118 XE_WARN_ON(size > MAX_PREEMPTDISABLE_TRANSFER); 2119 2120 /* 2121 * MI_STORE_DATA_IMM command is used to update page table. Each 2122 * instruction can update maximumly MAX_PTE_PER_SDI pte entries. To 2123 * update n (n <= MAX_PTE_PER_SDI) pte entries, we need: 2124 * 2125 * - 1 dword for the MI_STORE_DATA_IMM command header (opcode etc) 2126 * - 2 dword for the page table's physical location 2127 * - 2*n dword for value of pte to fill (each pte entry is 2 dwords) 2128 */ 2129 num_dword = (1 + 2) * DIV_U64_ROUND_UP(entries, MAX_PTE_PER_SDI); 2130 num_dword += entries * 2; 2131 2132 return num_dword; 2133 } 2134 2135 static void build_pt_update_batch_sram(struct xe_migrate *m, 2136 struct xe_bb *bb, u32 pt_offset, 2137 struct drm_pagemap_addr *sram_addr, 2138 u32 size, int level) 2139 { 2140 u16 pat_index = xe_cache_pat_idx(tile_to_xe(m->tile), XE_CACHE_WB); 2141 u64 gpu_page_size = 0x1ull << xe_pt_shift(level); 2142 u32 ptes; 2143 int i = 0; 2144 2145 xe_tile_assert(m->tile, PAGE_ALIGNED(size)); 2146 2147 ptes = DIV_ROUND_UP(size, gpu_page_size); 2148 while (ptes) { 2149 u32 chunk = min(MAX_PTE_PER_SDI, ptes); 2150 2151 if (!level) 2152 chunk = ALIGN_DOWN(chunk, PAGE_SIZE / XE_PAGE_SIZE); 2153 2154 bb->cs[bb->len++] = MI_STORE_DATA_IMM | MI_SDI_NUM_QW(chunk); 2155 bb->cs[bb->len++] = pt_offset; 2156 bb->cs[bb->len++] = 0; 2157 2158 pt_offset += chunk * 8; 2159 ptes -= chunk; 2160 2161 while (chunk--) { 2162 u64 addr = sram_addr[i].addr; 2163 u64 pte; 2164 2165 xe_tile_assert(m->tile, sram_addr[i].proto == 2166 DRM_INTERCONNECT_SYSTEM || 2167 sram_addr[i].proto == XE_INTERCONNECT_P2P); 2168 xe_tile_assert(m->tile, addr); 2169 xe_tile_assert(m->tile, PAGE_ALIGNED(addr)); 2170 2171 again: 2172 pte = m->q->vm->pt_ops->pte_encode_addr(m->tile->xe, 2173 addr, pat_index, 2174 level, false, 0); 2175 bb->cs[bb->len++] = lower_32_bits(pte); 2176 bb->cs[bb->len++] = upper_32_bits(pte); 2177 2178 if (gpu_page_size < PAGE_SIZE) { 2179 addr += XE_PAGE_SIZE; 2180 if (!PAGE_ALIGNED(addr)) { 2181 chunk--; 2182 goto again; 2183 } 2184 i++; 2185 } else { 2186 i += gpu_page_size / PAGE_SIZE; 2187 } 2188 } 2189 } 2190 } 2191 2192 static bool xe_migrate_vram_use_pde(struct drm_pagemap_addr *sram_addr, 2193 unsigned long size) 2194 { 2195 u32 large_size = (0x1 << xe_pt_shift(1)); 2196 unsigned long i, incr = large_size / PAGE_SIZE; 2197 2198 for (i = 0; i < DIV_ROUND_UP(size, PAGE_SIZE); i += incr) 2199 if (PAGE_SIZE << sram_addr[i].order != large_size) 2200 return false; 2201 2202 return true; 2203 } 2204 2205 #define XE_CACHELINE_BYTES 64ull 2206 #define XE_CACHELINE_MASK (XE_CACHELINE_BYTES - 1) 2207 2208 static u32 xe_migrate_copy_pitch(struct xe_device *xe, u32 len) 2209 { 2210 u32 pitch; 2211 2212 if (IS_ALIGNED(len, PAGE_SIZE)) 2213 pitch = PAGE_SIZE; 2214 else if (IS_ALIGNED(len, SZ_4K)) 2215 pitch = SZ_4K; 2216 else if (IS_ALIGNED(len, SZ_256)) 2217 pitch = SZ_256; 2218 else if (IS_ALIGNED(len, 4)) 2219 pitch = 4; 2220 else 2221 pitch = 1; 2222 2223 xe_assert(xe, pitch > 1 || xe->info.has_mem_copy_instr); 2224 return pitch; 2225 } 2226 2227 static struct dma_fence *xe_migrate_vram(struct xe_migrate *m, 2228 unsigned long len, 2229 unsigned long sram_offset, 2230 struct drm_pagemap_addr *sram_addr, 2231 u64 vram_addr, 2232 struct dma_fence *deps, 2233 const enum xe_migrate_copy_dir dir) 2234 { 2235 struct xe_gt *gt = m->tile->primary_gt; 2236 struct xe_device *xe = gt_to_xe(gt); 2237 bool use_usm_batch = xe->info.has_usm; 2238 struct dma_fence *fence = NULL; 2239 u32 batch_size = 1; 2240 u64 src_L0_ofs, dst_L0_ofs; 2241 struct xe_sched_job *job; 2242 struct xe_bb *bb; 2243 u32 update_idx, pt_slot = 0; 2244 unsigned long npages = DIV_ROUND_UP(len + sram_offset, PAGE_SIZE); 2245 unsigned int pitch = xe_migrate_copy_pitch(xe, len); 2246 int err; 2247 unsigned long i, j; 2248 bool use_pde = xe_migrate_vram_use_pde(sram_addr, len + sram_offset); 2249 2250 if (!xe->info.has_mem_copy_instr && 2251 drm_WARN_ON(&xe->drm, 2252 (!IS_ALIGNED(len, pitch)) || (sram_offset | vram_addr) & XE_CACHELINE_MASK)) 2253 return ERR_PTR(-EOPNOTSUPP); 2254 2255 xe_assert(xe, npages * PAGE_SIZE <= MAX_PREEMPTDISABLE_TRANSFER); 2256 2257 batch_size += pte_update_cmd_size(npages << PAGE_SHIFT); 2258 batch_size += emit_copy_cmd_len(xe); 2259 2260 bb = xe_bb_new(gt, batch_size, use_usm_batch); 2261 if (IS_ERR(bb)) { 2262 err = PTR_ERR(bb); 2263 return ERR_PTR(err); 2264 } 2265 2266 /* 2267 * If the order of a struct drm_pagemap_addr entry is greater than 0, 2268 * the entry is populated by GPU pagemap but subsequent entries within 2269 * the range of that order are not populated. 2270 * build_pt_update_batch_sram() expects a fully populated array of 2271 * struct drm_pagemap_addr. Ensure this is the case even with higher 2272 * orders. 2273 */ 2274 for (i = 0; !use_pde && i < npages;) { 2275 unsigned int order = sram_addr[i].order; 2276 2277 for (j = 1; j < NR_PAGES(order) && i + j < npages; j++) 2278 if (!sram_addr[i + j].addr) 2279 sram_addr[i + j].addr = sram_addr[i].addr + j * PAGE_SIZE; 2280 2281 i += NR_PAGES(order); 2282 } 2283 2284 if (use_pde) 2285 build_pt_update_batch_sram(m, bb, m->large_page_copy_pdes, 2286 sram_addr, npages << PAGE_SHIFT, 1); 2287 else 2288 build_pt_update_batch_sram(m, bb, pt_slot * XE_PAGE_SIZE, 2289 sram_addr, npages << PAGE_SHIFT, 0); 2290 2291 if (dir == XE_MIGRATE_COPY_TO_VRAM) { 2292 if (use_pde) 2293 src_L0_ofs = m->large_page_copy_ofs + sram_offset; 2294 else 2295 src_L0_ofs = xe_migrate_vm_addr(pt_slot, 0) + sram_offset; 2296 dst_L0_ofs = xe_migrate_vram_ofs(xe, vram_addr, false); 2297 2298 } else { 2299 src_L0_ofs = xe_migrate_vram_ofs(xe, vram_addr, false); 2300 if (use_pde) 2301 dst_L0_ofs = m->large_page_copy_ofs + sram_offset; 2302 else 2303 dst_L0_ofs = xe_migrate_vm_addr(pt_slot, 0) + sram_offset; 2304 } 2305 2306 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 2307 update_idx = bb->len; 2308 2309 emit_copy(gt, bb, src_L0_ofs, dst_L0_ofs, len, pitch); 2310 2311 job = xe_bb_create_migration_job(m->q, bb, 2312 xe_migrate_batch_base(m, use_usm_batch), 2313 update_idx); 2314 if (IS_ERR(job)) { 2315 err = PTR_ERR(job); 2316 goto err; 2317 } 2318 2319 xe_sched_job_add_migrate_flush(job, MI_INVALIDATE_TLB); 2320 2321 if (deps && !dma_fence_is_signaled(deps)) { 2322 dma_fence_get(deps); 2323 err = drm_sched_job_add_dependency(&job->drm, deps); 2324 if (err) 2325 dma_fence_wait(deps, false); 2326 err = 0; 2327 } 2328 2329 mutex_lock(&m->job_mutex); 2330 xe_sched_job_arm(job); 2331 fence = dma_fence_get(&job->drm.s_fence->finished); 2332 xe_sched_job_push(job); 2333 2334 dma_fence_put(m->fence); 2335 m->fence = dma_fence_get(fence); 2336 mutex_unlock(&m->job_mutex); 2337 2338 xe_bb_free(bb, fence); 2339 2340 return fence; 2341 2342 err: 2343 xe_bb_free(bb, NULL); 2344 2345 return ERR_PTR(err); 2346 } 2347 2348 /** 2349 * xe_migrate_to_vram() - Migrate to VRAM 2350 * @m: The migration context. 2351 * @npages: Number of pages to migrate. 2352 * @src_addr: Array of DMA information (source of migrate) 2353 * @dst_addr: Device physical address of VRAM (destination of migrate) 2354 * @deps: struct dma_fence representing the dependencies that need 2355 * to be signaled before migration. 2356 * 2357 * Copy from an array dma addresses to a VRAM device physical address 2358 * 2359 * Return: dma fence for migrate to signal completion on success, ERR_PTR on 2360 * failure 2361 */ 2362 struct dma_fence *xe_migrate_to_vram(struct xe_migrate *m, 2363 unsigned long npages, 2364 struct drm_pagemap_addr *src_addr, 2365 u64 dst_addr, 2366 struct dma_fence *deps) 2367 { 2368 return xe_migrate_vram(m, npages * PAGE_SIZE, 0, src_addr, dst_addr, 2369 deps, XE_MIGRATE_COPY_TO_VRAM); 2370 } 2371 2372 /** 2373 * xe_migrate_from_vram() - Migrate from VRAM 2374 * @m: The migration context. 2375 * @npages: Number of pages to migrate. 2376 * @src_addr: Device physical address of VRAM (source of migrate) 2377 * @dst_addr: Array of DMA information (destination of migrate) 2378 * @deps: struct dma_fence representing the dependencies that need 2379 * to be signaled before migration. 2380 * 2381 * Copy from a VRAM device physical address to an array dma addresses 2382 * 2383 * Return: dma fence for migrate to signal completion on success, ERR_PTR on 2384 * failure 2385 */ 2386 struct dma_fence *xe_migrate_from_vram(struct xe_migrate *m, 2387 unsigned long npages, 2388 u64 src_addr, 2389 struct drm_pagemap_addr *dst_addr, 2390 struct dma_fence *deps) 2391 { 2392 return xe_migrate_vram(m, npages * PAGE_SIZE, 0, dst_addr, src_addr, 2393 deps, XE_MIGRATE_COPY_TO_SRAM); 2394 } 2395 2396 static void xe_migrate_dma_unmap(struct xe_device *xe, 2397 struct drm_pagemap_addr *pagemap_addr, 2398 int len, int write) 2399 { 2400 unsigned long i, npages = DIV_ROUND_UP(len, PAGE_SIZE); 2401 2402 for (i = 0; i < npages; ++i) { 2403 if (!pagemap_addr[i].addr) 2404 break; 2405 2406 dma_unmap_page(xe->drm.dev, pagemap_addr[i].addr, PAGE_SIZE, 2407 write ? DMA_TO_DEVICE : DMA_FROM_DEVICE); 2408 } 2409 kfree(pagemap_addr); 2410 } 2411 2412 static struct drm_pagemap_addr *xe_migrate_dma_map(struct xe_device *xe, 2413 void *buf, int len, 2414 int write) 2415 { 2416 struct drm_pagemap_addr *pagemap_addr; 2417 unsigned long i, npages = DIV_ROUND_UP(len, PAGE_SIZE); 2418 2419 pagemap_addr = kzalloc_objs(*pagemap_addr, npages); 2420 if (!pagemap_addr) 2421 return ERR_PTR(-ENOMEM); 2422 2423 for (i = 0; i < npages; ++i) { 2424 dma_addr_t addr; 2425 struct page *page; 2426 enum dma_data_direction dir = write ? DMA_TO_DEVICE : 2427 DMA_FROM_DEVICE; 2428 2429 if (is_vmalloc_addr(buf)) 2430 page = vmalloc_to_page(buf); 2431 else 2432 page = virt_to_page(buf); 2433 2434 addr = dma_map_page(xe->drm.dev, page, 0, PAGE_SIZE, dir); 2435 if (dma_mapping_error(xe->drm.dev, addr)) 2436 goto err_fault; 2437 2438 pagemap_addr[i] = 2439 drm_pagemap_addr_encode(addr, 2440 DRM_INTERCONNECT_SYSTEM, 2441 0, dir); 2442 buf += PAGE_SIZE; 2443 } 2444 2445 return pagemap_addr; 2446 2447 err_fault: 2448 xe_migrate_dma_unmap(xe, pagemap_addr, len, write); 2449 return ERR_PTR(-EFAULT); 2450 } 2451 2452 /** 2453 * xe_migrate_access_memory - Access memory of a BO via GPU 2454 * 2455 * @m: The migration context. 2456 * @bo: buffer object 2457 * @offset: access offset into buffer object 2458 * @buf: pointer to caller memory to read into or write from 2459 * @len: length of access 2460 * @write: write access 2461 * 2462 * Access memory of a BO via GPU either reading in or writing from a passed in 2463 * pointer. Pointer is dma mapped for GPU access and GPU commands are issued to 2464 * read to or write from pointer. 2465 * 2466 * Returns: 2467 * 0 if successful, negative error code on failure. 2468 */ 2469 int xe_migrate_access_memory(struct xe_migrate *m, struct xe_bo *bo, 2470 unsigned long offset, void *buf, int len, 2471 int write) 2472 { 2473 struct xe_tile *tile = m->tile; 2474 struct xe_device *xe = tile_to_xe(tile); 2475 struct xe_res_cursor cursor; 2476 struct dma_fence *fence = NULL; 2477 struct drm_pagemap_addr *pagemap_addr; 2478 unsigned long page_offset = (unsigned long)buf & ~PAGE_MASK; 2479 int bytes_left = len, current_page = 0; 2480 void *orig_buf = buf; 2481 2482 xe_bo_assert_held(bo); 2483 2484 /* Use bounce buffer for small access and unaligned access */ 2485 if (!xe->info.has_mem_copy_instr && 2486 (!IS_ALIGNED(len, 4) || 2487 !IS_ALIGNED(page_offset, XE_CACHELINE_BYTES) || 2488 !IS_ALIGNED(offset, XE_CACHELINE_BYTES))) { 2489 int buf_offset = 0; 2490 void *bounce; 2491 int err; 2492 2493 BUILD_BUG_ON(!is_power_of_2(XE_CACHELINE_BYTES)); 2494 bounce = kmalloc(XE_CACHELINE_BYTES, GFP_KERNEL); 2495 if (!bounce) 2496 return -ENOMEM; 2497 2498 /* 2499 * Less than ideal for large unaligned access but this should be 2500 * fairly rare, can fixup if this becomes common. 2501 */ 2502 do { 2503 int copy_bytes = min_t(int, bytes_left, 2504 XE_CACHELINE_BYTES - 2505 (offset & XE_CACHELINE_MASK)); 2506 int ptr_offset = offset & XE_CACHELINE_MASK; 2507 2508 err = xe_migrate_access_memory(m, bo, 2509 offset & 2510 ~XE_CACHELINE_MASK, 2511 bounce, 2512 XE_CACHELINE_BYTES, 0); 2513 if (err) 2514 break; 2515 2516 if (write) { 2517 memcpy(bounce + ptr_offset, buf + buf_offset, copy_bytes); 2518 2519 err = xe_migrate_access_memory(m, bo, 2520 offset & ~XE_CACHELINE_MASK, 2521 bounce, 2522 XE_CACHELINE_BYTES, write); 2523 if (err) 2524 break; 2525 } else { 2526 memcpy(buf + buf_offset, bounce + ptr_offset, 2527 copy_bytes); 2528 } 2529 2530 bytes_left -= copy_bytes; 2531 buf_offset += copy_bytes; 2532 offset += copy_bytes; 2533 } while (bytes_left); 2534 2535 kfree(bounce); 2536 return err; 2537 } 2538 2539 pagemap_addr = xe_migrate_dma_map(xe, buf, len + page_offset, write); 2540 if (IS_ERR(pagemap_addr)) 2541 return PTR_ERR(pagemap_addr); 2542 2543 xe_res_first(bo->ttm.resource, offset, xe_bo_size(bo) - offset, &cursor); 2544 2545 do { 2546 struct dma_fence *__fence; 2547 u64 vram_addr = vram_region_gpu_offset(bo->ttm.resource) + 2548 cursor.start; 2549 int current_bytes; 2550 u32 pitch; 2551 2552 if (cursor.size > MAX_PREEMPTDISABLE_TRANSFER) 2553 current_bytes = min_t(int, bytes_left, 2554 MAX_PREEMPTDISABLE_TRANSFER); 2555 else 2556 current_bytes = min_t(int, bytes_left, cursor.size); 2557 2558 pitch = xe_migrate_copy_pitch(xe, current_bytes); 2559 if (xe->info.has_mem_copy_instr) 2560 current_bytes = min_t(int, current_bytes, U16_MAX * pitch); 2561 else 2562 current_bytes = min_t(int, current_bytes, 2563 round_down(S16_MAX * pitch, 2564 XE_CACHELINE_BYTES)); 2565 2566 __fence = xe_migrate_vram(m, current_bytes, 2567 (unsigned long)buf & ~PAGE_MASK, 2568 &pagemap_addr[current_page], 2569 vram_addr, NULL, write ? 2570 XE_MIGRATE_COPY_TO_VRAM : 2571 XE_MIGRATE_COPY_TO_SRAM); 2572 if (IS_ERR(__fence)) { 2573 if (fence) { 2574 dma_fence_wait(fence, false); 2575 dma_fence_put(fence); 2576 } 2577 fence = __fence; 2578 goto out_err; 2579 } 2580 2581 dma_fence_put(fence); 2582 fence = __fence; 2583 2584 buf += current_bytes; 2585 offset += current_bytes; 2586 current_page = (int)(buf - orig_buf) / PAGE_SIZE; 2587 bytes_left -= current_bytes; 2588 if (bytes_left) 2589 xe_res_next(&cursor, current_bytes); 2590 } while (bytes_left); 2591 2592 dma_fence_wait(fence, false); 2593 dma_fence_put(fence); 2594 2595 out_err: 2596 xe_migrate_dma_unmap(xe, pagemap_addr, len + page_offset, write); 2597 return IS_ERR(fence) ? PTR_ERR(fence) : 0; 2598 } 2599 2600 /** 2601 * xe_migrate_job_lock() - Lock migrate job lock 2602 * @m: The migration context. 2603 * @q: Queue associated with the operation which requires a lock 2604 * 2605 * Lock the migrate job lock if the queue is a migration queue, otherwise 2606 * assert the VM's dma-resv is held (user queue's have own locking). 2607 */ 2608 void xe_migrate_job_lock(struct xe_migrate *m, struct xe_exec_queue *q) 2609 { 2610 bool is_migrate = q == m->q; 2611 2612 if (is_migrate) 2613 mutex_lock(&m->job_mutex); 2614 else 2615 xe_vm_assert_held(q->user_vm); /* User queues VM's should be locked */ 2616 } 2617 2618 /** 2619 * xe_migrate_job_unlock() - Unlock migrate job lock 2620 * @m: The migration context. 2621 * @q: Queue associated with the operation which requires a lock 2622 * 2623 * Unlock the migrate job lock if the queue is a migration queue, otherwise 2624 * assert the VM's dma-resv is held (user queue's have own locking). 2625 */ 2626 void xe_migrate_job_unlock(struct xe_migrate *m, struct xe_exec_queue *q) 2627 { 2628 bool is_migrate = q == m->q; 2629 2630 if (is_migrate) 2631 mutex_unlock(&m->job_mutex); 2632 else 2633 xe_vm_assert_held(q->user_vm); /* User queues VM's should be locked */ 2634 } 2635 2636 #if IS_ENABLED(CONFIG_PROVE_LOCKING) 2637 /** 2638 * xe_migrate_job_lock_assert() - Assert migrate job lock held of queue 2639 * @q: Migrate queue 2640 */ 2641 void xe_migrate_job_lock_assert(struct xe_exec_queue *q) 2642 { 2643 struct xe_migrate *m = gt_to_tile(q->gt)->migrate; 2644 2645 xe_gt_assert(q->gt, q == m->q); 2646 lockdep_assert_held(&m->job_mutex); 2647 } 2648 #endif 2649 2650 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST) 2651 #include "tests/xe_migrate.c" 2652 #endif 2653 2654 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG_MEM) 2655 int xe_migrate_debug_ccs_overlap(struct xe_migrate *m, 2656 struct xe_bo *scratch_bo, 2657 bool write_to_ccs) 2658 { 2659 struct xe_device *xe = tile_to_xe(m->tile); 2660 struct xe_gt *gt = m->tile->primary_gt; 2661 struct dma_fence *fence; 2662 struct xe_bb *bb; 2663 struct xe_sched_job *job; 2664 u64 first_page_dpa, clear_L0_ofs, scratch_dpa, scratch_L0_ofs; 2665 2666 if (!xe_device_has_flat_ccs(xe)) 2667 return -EINVAL; 2668 2669 first_page_dpa = xe_vram_region_dpa_base(m->tile->mem.vram); 2670 clear_L0_ofs = xe_migrate_vram_ofs(xe, first_page_dpa, true); 2671 2672 scratch_dpa = xe_bo_addr(scratch_bo, 0, XE_PAGE_SIZE); 2673 scratch_L0_ofs = xe_migrate_vram_ofs(xe, scratch_dpa, false); 2674 2675 bb = xe_bb_new(gt, EMIT_COPY_CCS_DW + 1, xe->info.has_usm); 2676 if (IS_ERR(bb)) { 2677 drm_warn(&xe->drm, "Failed to create bb for VRAM overlap check\n"); 2678 return PTR_ERR(bb); 2679 } 2680 2681 /* 4MB payload = 8KB CCS metadata */ 2682 if (write_to_ccs) { 2683 emit_copy_ccs(gt, bb, clear_L0_ofs, true, 2684 scratch_L0_ofs, false, SZ_4M); 2685 } else { 2686 emit_copy_ccs(gt, bb, scratch_L0_ofs, false, 2687 clear_L0_ofs, true, SZ_4M); 2688 } 2689 2690 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 2691 2692 job = xe_bb_create_migration_job(m->q, bb, 2693 xe_migrate_batch_base(m, xe->info.has_usm), 2694 0); 2695 if (!IS_ERR(job)) { 2696 xe_sched_job_add_migrate_flush(job, MI_FLUSH_DW_CCS); 2697 2698 mutex_lock(&m->job_mutex); 2699 xe_sched_job_arm(job); 2700 2701 fence = dma_fence_get(&job->drm.s_fence->finished); 2702 xe_sched_job_push(job); 2703 mutex_unlock(&m->job_mutex); 2704 2705 dma_fence_wait(fence, false); 2706 dma_fence_put(fence); 2707 } else { 2708 drm_warn(&xe->drm, "Failed to create job for VRAM overlap check\n"); 2709 xe_bb_free(bb, NULL); 2710 return PTR_ERR(job); 2711 } 2712 2713 xe_bb_free(bb, NULL); 2714 return 0; 2715 } 2716 #endif 2717