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/ttm/ttm_tt.h> 13 #include <uapi/drm/xe_drm.h> 14 15 #include <generated/xe_wa_oob.h> 16 17 #include "instructions/xe_gpu_commands.h" 18 #include "instructions/xe_mi_commands.h" 19 #include "regs/xe_gtt_defs.h" 20 #include "tests/xe_test.h" 21 #include "xe_assert.h" 22 #include "xe_bb.h" 23 #include "xe_bo.h" 24 #include "xe_exec_queue.h" 25 #include "xe_ggtt.h" 26 #include "xe_gt.h" 27 #include "xe_hw_engine.h" 28 #include "xe_lrc.h" 29 #include "xe_map.h" 30 #include "xe_mocs.h" 31 #include "xe_pt.h" 32 #include "xe_res_cursor.h" 33 #include "xe_sched_job.h" 34 #include "xe_sync.h" 35 #include "xe_trace_bo.h" 36 #include "xe_vm.h" 37 38 /** 39 * struct xe_migrate - migrate context. 40 */ 41 struct xe_migrate { 42 /** @q: Default exec queue used for migration */ 43 struct xe_exec_queue *q; 44 /** @tile: Backpointer to the tile this struct xe_migrate belongs to. */ 45 struct xe_tile *tile; 46 /** @job_mutex: Timeline mutex for @eng. */ 47 struct mutex job_mutex; 48 /** @pt_bo: Page-table buffer object. */ 49 struct xe_bo *pt_bo; 50 /** @batch_base_ofs: VM offset of the migration batch buffer */ 51 u64 batch_base_ofs; 52 /** @usm_batch_base_ofs: VM offset of the usm batch buffer */ 53 u64 usm_batch_base_ofs; 54 /** @cleared_mem_ofs: VM offset of @cleared_bo. */ 55 u64 cleared_mem_ofs; 56 /** 57 * @fence: dma-fence representing the last migration job batch. 58 * Protected by @job_mutex. 59 */ 60 struct dma_fence *fence; 61 /** 62 * @vm_update_sa: For integrated, used to suballocate page-tables 63 * out of the pt_bo. 64 */ 65 struct drm_suballoc_manager vm_update_sa; 66 /** @min_chunk_size: For dgfx, Minimum chunk size */ 67 u64 min_chunk_size; 68 }; 69 70 #define MAX_PREEMPTDISABLE_TRANSFER SZ_8M /* Around 1ms. */ 71 #define MAX_CCS_LIMITED_TRANSFER SZ_4M /* XE_PAGE_SIZE * (FIELD_MAX(XE2_CCS_SIZE_MASK) + 1) */ 72 #define NUM_KERNEL_PDE 15 73 #define NUM_PT_SLOTS 32 74 #define LEVEL0_PAGE_TABLE_ENCODE_SIZE SZ_2M 75 #define MAX_NUM_PTE 512 76 #define IDENTITY_OFFSET 256ULL 77 78 /* 79 * Although MI_STORE_DATA_IMM's "length" field is 10-bits, 0x3FE is the largest 80 * legal value accepted. Since that instruction field is always stored in 81 * (val-2) format, this translates to 0x400 dwords for the true maximum length 82 * of the instruction. Subtracting the instruction header (1 dword) and 83 * address (2 dwords), that leaves 0x3FD dwords (0x1FE qwords) for PTE values. 84 */ 85 #define MAX_PTE_PER_SDI 0x1FE 86 87 /** 88 * xe_tile_migrate_exec_queue() - Get this tile's migrate exec queue. 89 * @tile: The tile. 90 * 91 * Returns the default migrate exec queue of this tile. 92 * 93 * Return: The default migrate exec queue 94 */ 95 struct xe_exec_queue *xe_tile_migrate_exec_queue(struct xe_tile *tile) 96 { 97 return tile->migrate->q; 98 } 99 100 static void xe_migrate_fini(void *arg) 101 { 102 struct xe_migrate *m = arg; 103 104 xe_vm_lock(m->q->vm, false); 105 xe_bo_unpin(m->pt_bo); 106 xe_vm_unlock(m->q->vm); 107 108 dma_fence_put(m->fence); 109 xe_bo_put(m->pt_bo); 110 drm_suballoc_manager_fini(&m->vm_update_sa); 111 mutex_destroy(&m->job_mutex); 112 xe_vm_close_and_put(m->q->vm); 113 xe_exec_queue_put(m->q); 114 } 115 116 static u64 xe_migrate_vm_addr(u64 slot, u32 level) 117 { 118 XE_WARN_ON(slot >= NUM_PT_SLOTS); 119 120 /* First slot is reserved for mapping of PT bo and bb, start from 1 */ 121 return (slot + 1ULL) << xe_pt_shift(level + 1); 122 } 123 124 static u64 xe_migrate_vram_ofs(struct xe_device *xe, u64 addr, bool is_comp_pte) 125 { 126 /* 127 * Remove the DPA to get a correct offset into identity table for the 128 * migrate offset 129 */ 130 u64 identity_offset = IDENTITY_OFFSET; 131 132 if (GRAPHICS_VER(xe) >= 20 && is_comp_pte) 133 identity_offset += DIV_ROUND_UP_ULL(xe->mem.vram.actual_physical_size, SZ_1G); 134 135 addr -= xe->mem.vram.dpa_base; 136 return addr + (identity_offset << xe_pt_shift(2)); 137 } 138 139 static void xe_migrate_program_identity(struct xe_device *xe, struct xe_vm *vm, struct xe_bo *bo, 140 u64 map_ofs, u64 vram_offset, u16 pat_index, u64 pt_2m_ofs) 141 { 142 u64 pos, ofs, flags; 143 u64 entry; 144 /* XXX: Unclear if this should be usable_size? */ 145 u64 vram_limit = xe->mem.vram.actual_physical_size + 146 xe->mem.vram.dpa_base; 147 u32 level = 2; 148 149 ofs = map_ofs + XE_PAGE_SIZE * level + vram_offset * 8; 150 flags = vm->pt_ops->pte_encode_addr(xe, 0, pat_index, level, 151 true, 0); 152 153 xe_assert(xe, IS_ALIGNED(xe->mem.vram.usable_size, SZ_2M)); 154 155 /* 156 * Use 1GB pages when possible, last chunk always use 2M 157 * pages as mixing reserved memory (stolen, WOCPM) with a single 158 * mapping is not allowed on certain platforms. 159 */ 160 for (pos = xe->mem.vram.dpa_base; pos < vram_limit; 161 pos += SZ_1G, ofs += 8) { 162 if (pos + SZ_1G >= vram_limit) { 163 entry = vm->pt_ops->pde_encode_bo(bo, pt_2m_ofs, 164 pat_index); 165 xe_map_wr(xe, &bo->vmap, ofs, u64, entry); 166 167 flags = vm->pt_ops->pte_encode_addr(xe, 0, 168 pat_index, 169 level - 1, 170 true, 0); 171 172 for (ofs = pt_2m_ofs; pos < vram_limit; 173 pos += SZ_2M, ofs += 8) 174 xe_map_wr(xe, &bo->vmap, ofs, u64, pos | flags); 175 break; /* Ensure pos == vram_limit assert correct */ 176 } 177 178 xe_map_wr(xe, &bo->vmap, ofs, u64, pos | flags); 179 } 180 181 xe_assert(xe, pos == vram_limit); 182 } 183 184 static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m, 185 struct xe_vm *vm) 186 { 187 struct xe_device *xe = tile_to_xe(tile); 188 u16 pat_index = xe->pat.idx[XE_CACHE_WB]; 189 u8 id = tile->id; 190 u32 num_entries = NUM_PT_SLOTS, num_level = vm->pt_root[id]->level; 191 #define VRAM_IDENTITY_MAP_COUNT 2 192 u32 num_setup = num_level + VRAM_IDENTITY_MAP_COUNT; 193 #undef VRAM_IDENTITY_MAP_COUNT 194 u32 map_ofs, level, i; 195 struct xe_bo *bo, *batch = tile->mem.kernel_bb_pool->bo; 196 u64 entry, pt29_ofs; 197 198 /* Can't bump NUM_PT_SLOTS too high */ 199 BUILD_BUG_ON(NUM_PT_SLOTS > SZ_2M/XE_PAGE_SIZE); 200 /* Must be a multiple of 64K to support all platforms */ 201 BUILD_BUG_ON(NUM_PT_SLOTS * XE_PAGE_SIZE % SZ_64K); 202 /* And one slot reserved for the 4KiB page table updates */ 203 BUILD_BUG_ON(!(NUM_KERNEL_PDE & 1)); 204 205 /* Need to be sure everything fits in the first PT, or create more */ 206 xe_tile_assert(tile, m->batch_base_ofs + batch->size < SZ_2M); 207 208 bo = xe_bo_create_pin_map(vm->xe, tile, vm, 209 num_entries * XE_PAGE_SIZE, 210 ttm_bo_type_kernel, 211 XE_BO_FLAG_VRAM_IF_DGFX(tile) | 212 XE_BO_FLAG_PAGETABLE); 213 if (IS_ERR(bo)) 214 return PTR_ERR(bo); 215 216 /* PT30 & PT31 reserved for 2M identity map */ 217 pt29_ofs = bo->size - 3 * XE_PAGE_SIZE; 218 entry = vm->pt_ops->pde_encode_bo(bo, pt29_ofs, pat_index); 219 xe_pt_write(xe, &vm->pt_root[id]->bo->vmap, 0, entry); 220 221 map_ofs = (num_entries - num_setup) * XE_PAGE_SIZE; 222 223 /* Map the entire BO in our level 0 pt */ 224 for (i = 0, level = 0; i < num_entries; level++) { 225 entry = vm->pt_ops->pte_encode_bo(bo, i * XE_PAGE_SIZE, 226 pat_index, 0); 227 228 xe_map_wr(xe, &bo->vmap, map_ofs + level * 8, u64, entry); 229 230 if (vm->flags & XE_VM_FLAG_64K) 231 i += 16; 232 else 233 i += 1; 234 } 235 236 if (!IS_DGFX(xe)) { 237 /* Write out batch too */ 238 m->batch_base_ofs = NUM_PT_SLOTS * XE_PAGE_SIZE; 239 for (i = 0; i < batch->size; 240 i += vm->flags & XE_VM_FLAG_64K ? XE_64K_PAGE_SIZE : 241 XE_PAGE_SIZE) { 242 entry = vm->pt_ops->pte_encode_bo(batch, i, 243 pat_index, 0); 244 245 xe_map_wr(xe, &bo->vmap, map_ofs + level * 8, u64, 246 entry); 247 level++; 248 } 249 if (xe->info.has_usm) { 250 xe_tile_assert(tile, batch->size == SZ_1M); 251 252 batch = tile->primary_gt->usm.bb_pool->bo; 253 m->usm_batch_base_ofs = m->batch_base_ofs + SZ_1M; 254 xe_tile_assert(tile, batch->size == SZ_512K); 255 256 for (i = 0; i < batch->size; 257 i += vm->flags & XE_VM_FLAG_64K ? XE_64K_PAGE_SIZE : 258 XE_PAGE_SIZE) { 259 entry = vm->pt_ops->pte_encode_bo(batch, i, 260 pat_index, 0); 261 262 xe_map_wr(xe, &bo->vmap, map_ofs + level * 8, u64, 263 entry); 264 level++; 265 } 266 } 267 } else { 268 u64 batch_addr = xe_bo_addr(batch, 0, XE_PAGE_SIZE); 269 270 m->batch_base_ofs = xe_migrate_vram_ofs(xe, batch_addr, false); 271 272 if (xe->info.has_usm) { 273 batch = tile->primary_gt->usm.bb_pool->bo; 274 batch_addr = xe_bo_addr(batch, 0, XE_PAGE_SIZE); 275 m->usm_batch_base_ofs = xe_migrate_vram_ofs(xe, batch_addr, false); 276 } 277 } 278 279 for (level = 1; level < num_level; level++) { 280 u32 flags = 0; 281 282 if (vm->flags & XE_VM_FLAG_64K && level == 1) 283 flags = XE_PDE_64K; 284 285 entry = vm->pt_ops->pde_encode_bo(bo, map_ofs + (u64)(level - 1) * 286 XE_PAGE_SIZE, pat_index); 287 xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE * level, u64, 288 entry | flags); 289 } 290 291 /* Write PDE's that point to our BO. */ 292 for (i = 0; i < map_ofs / PAGE_SIZE; i++) { 293 entry = vm->pt_ops->pde_encode_bo(bo, (u64)i * XE_PAGE_SIZE, 294 pat_index); 295 296 xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE + 297 (i + 1) * 8, u64, entry); 298 } 299 300 /* Set up a 1GiB NULL mapping at 255GiB offset. */ 301 level = 2; 302 xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE * level + 255 * 8, u64, 303 vm->pt_ops->pte_encode_addr(xe, 0, pat_index, level, IS_DGFX(xe), 0) 304 | XE_PTE_NULL); 305 m->cleared_mem_ofs = (255ULL << xe_pt_shift(level)); 306 307 /* Identity map the entire vram at 256GiB offset */ 308 if (IS_DGFX(xe)) { 309 u64 pt30_ofs = bo->size - 2 * XE_PAGE_SIZE; 310 311 xe_migrate_program_identity(xe, vm, bo, map_ofs, IDENTITY_OFFSET, 312 pat_index, pt30_ofs); 313 xe_assert(xe, xe->mem.vram.actual_physical_size <= 314 (MAX_NUM_PTE - IDENTITY_OFFSET) * SZ_1G); 315 316 /* 317 * Identity map the entire vram for compressed pat_index for xe2+ 318 * if flat ccs is enabled. 319 */ 320 if (GRAPHICS_VER(xe) >= 20 && xe_device_has_flat_ccs(xe)) { 321 u16 comp_pat_index = xe->pat.idx[XE_CACHE_NONE_COMPRESSION]; 322 u64 vram_offset = IDENTITY_OFFSET + 323 DIV_ROUND_UP_ULL(xe->mem.vram.actual_physical_size, SZ_1G); 324 u64 pt31_ofs = bo->size - XE_PAGE_SIZE; 325 326 xe_assert(xe, xe->mem.vram.actual_physical_size <= (MAX_NUM_PTE - 327 IDENTITY_OFFSET - IDENTITY_OFFSET / 2) * SZ_1G); 328 xe_migrate_program_identity(xe, vm, bo, map_ofs, vram_offset, 329 comp_pat_index, pt31_ofs); 330 } 331 } 332 333 /* 334 * Example layout created above, with root level = 3: 335 * [PT0...PT7]: kernel PT's for copy/clear; 64 or 4KiB PTE's 336 * [PT8]: Kernel PT for VM_BIND, 4 KiB PTE's 337 * [PT9...PT26]: Userspace PT's for VM_BIND, 4 KiB PTE's 338 * [PT27 = PDE 0] [PT28 = PDE 1] [PT29 = PDE 2] [PT30 & PT31 = 2M vram identity map] 339 * 340 * This makes the lowest part of the VM point to the pagetables. 341 * Hence the lowest 2M in the vm should point to itself, with a few writes 342 * and flushes, other parts of the VM can be used either for copying and 343 * clearing. 344 * 345 * For performance, the kernel reserves PDE's, so about 20 are left 346 * for async VM updates. 347 * 348 * To make it easier to work, each scratch PT is put in slot (1 + PT #) 349 * everywhere, this allows lockless updates to scratch pages by using 350 * the different addresses in VM. 351 */ 352 #define NUM_VMUSA_UNIT_PER_PAGE 32 353 #define VM_SA_UPDATE_UNIT_SIZE (XE_PAGE_SIZE / NUM_VMUSA_UNIT_PER_PAGE) 354 #define NUM_VMUSA_WRITES_PER_UNIT (VM_SA_UPDATE_UNIT_SIZE / sizeof(u64)) 355 drm_suballoc_manager_init(&m->vm_update_sa, 356 (size_t)(map_ofs / XE_PAGE_SIZE - NUM_KERNEL_PDE) * 357 NUM_VMUSA_UNIT_PER_PAGE, 0); 358 359 m->pt_bo = bo; 360 return 0; 361 } 362 363 /* 364 * Including the reserved copy engine is required to avoid deadlocks due to 365 * migrate jobs servicing the faults gets stuck behind the job that faulted. 366 */ 367 static u32 xe_migrate_usm_logical_mask(struct xe_gt *gt) 368 { 369 u32 logical_mask = 0; 370 struct xe_hw_engine *hwe; 371 enum xe_hw_engine_id id; 372 373 for_each_hw_engine(hwe, gt, id) { 374 if (hwe->class != XE_ENGINE_CLASS_COPY) 375 continue; 376 377 if (xe_gt_is_usm_hwe(gt, hwe)) 378 logical_mask |= BIT(hwe->logical_instance); 379 } 380 381 return logical_mask; 382 } 383 384 static bool xe_migrate_needs_ccs_emit(struct xe_device *xe) 385 { 386 return xe_device_has_flat_ccs(xe) && !(GRAPHICS_VER(xe) >= 20 && IS_DGFX(xe)); 387 } 388 389 /** 390 * xe_migrate_init() - Initialize a migrate context 391 * @tile: Back-pointer to the tile we're initializing for. 392 * 393 * Return: Pointer to a migrate context on success. Error pointer on error. 394 */ 395 struct xe_migrate *xe_migrate_init(struct xe_tile *tile) 396 { 397 struct xe_device *xe = tile_to_xe(tile); 398 struct xe_gt *primary_gt = tile->primary_gt; 399 struct xe_migrate *m; 400 struct xe_vm *vm; 401 int err; 402 403 m = devm_kzalloc(xe->drm.dev, sizeof(*m), GFP_KERNEL); 404 if (!m) 405 return ERR_PTR(-ENOMEM); 406 407 m->tile = tile; 408 409 /* Special layout, prepared below.. */ 410 vm = xe_vm_create(xe, XE_VM_FLAG_MIGRATION | 411 XE_VM_FLAG_SET_TILE_ID(tile)); 412 if (IS_ERR(vm)) 413 return ERR_CAST(vm); 414 415 xe_vm_lock(vm, false); 416 err = xe_migrate_prepare_vm(tile, m, vm); 417 xe_vm_unlock(vm); 418 if (err) { 419 xe_vm_close_and_put(vm); 420 return ERR_PTR(err); 421 } 422 423 if (xe->info.has_usm) { 424 struct xe_hw_engine *hwe = xe_gt_hw_engine(primary_gt, 425 XE_ENGINE_CLASS_COPY, 426 primary_gt->usm.reserved_bcs_instance, 427 false); 428 u32 logical_mask = xe_migrate_usm_logical_mask(primary_gt); 429 430 if (!hwe || !logical_mask) 431 return ERR_PTR(-EINVAL); 432 433 /* 434 * XXX: Currently only reserving 1 (likely slow) BCS instance on 435 * PVC, may want to revisit if performance is needed. 436 */ 437 m->q = xe_exec_queue_create(xe, vm, logical_mask, 1, hwe, 438 EXEC_QUEUE_FLAG_KERNEL | 439 EXEC_QUEUE_FLAG_PERMANENT | 440 EXEC_QUEUE_FLAG_HIGH_PRIORITY, 0); 441 } else { 442 m->q = xe_exec_queue_create_class(xe, primary_gt, vm, 443 XE_ENGINE_CLASS_COPY, 444 EXEC_QUEUE_FLAG_KERNEL | 445 EXEC_QUEUE_FLAG_PERMANENT, 0); 446 } 447 if (IS_ERR(m->q)) { 448 xe_vm_close_and_put(vm); 449 return ERR_CAST(m->q); 450 } 451 452 mutex_init(&m->job_mutex); 453 fs_reclaim_acquire(GFP_KERNEL); 454 might_lock(&m->job_mutex); 455 fs_reclaim_release(GFP_KERNEL); 456 457 err = devm_add_action_or_reset(xe->drm.dev, xe_migrate_fini, m); 458 if (err) 459 return ERR_PTR(err); 460 461 if (IS_DGFX(xe)) { 462 if (xe_migrate_needs_ccs_emit(xe)) 463 /* min chunk size corresponds to 4K of CCS Metadata */ 464 m->min_chunk_size = SZ_4K * SZ_64K / 465 xe_device_ccs_bytes(xe, SZ_64K); 466 else 467 /* Somewhat arbitrary to avoid a huge amount of blits */ 468 m->min_chunk_size = SZ_64K; 469 m->min_chunk_size = roundup_pow_of_two(m->min_chunk_size); 470 drm_dbg(&xe->drm, "Migrate min chunk size is 0x%08llx\n", 471 (unsigned long long)m->min_chunk_size); 472 } 473 474 return m; 475 } 476 477 static u64 max_mem_transfer_per_pass(struct xe_device *xe) 478 { 479 if (!IS_DGFX(xe) && xe_device_has_flat_ccs(xe)) 480 return MAX_CCS_LIMITED_TRANSFER; 481 482 return MAX_PREEMPTDISABLE_TRANSFER; 483 } 484 485 static u64 xe_migrate_res_sizes(struct xe_migrate *m, struct xe_res_cursor *cur) 486 { 487 struct xe_device *xe = tile_to_xe(m->tile); 488 u64 size = min_t(u64, max_mem_transfer_per_pass(xe), cur->remaining); 489 490 if (mem_type_is_vram(cur->mem_type)) { 491 /* 492 * VRAM we want to blit in chunks with sizes aligned to 493 * min_chunk_size in order for the offset to CCS metadata to be 494 * page-aligned. If it's the last chunk it may be smaller. 495 * 496 * Another constraint is that we need to limit the blit to 497 * the VRAM block size, unless size is smaller than 498 * min_chunk_size. 499 */ 500 u64 chunk = max_t(u64, cur->size, m->min_chunk_size); 501 502 size = min_t(u64, size, chunk); 503 if (size > m->min_chunk_size) 504 size = round_down(size, m->min_chunk_size); 505 } 506 507 return size; 508 } 509 510 static bool xe_migrate_allow_identity(u64 size, const struct xe_res_cursor *cur) 511 { 512 /* If the chunk is not fragmented, allow identity map. */ 513 return cur->size >= size; 514 } 515 516 #define PTE_UPDATE_FLAG_IS_VRAM BIT(0) 517 #define PTE_UPDATE_FLAG_IS_COMP_PTE BIT(1) 518 519 static u32 pte_update_size(struct xe_migrate *m, 520 u32 flags, 521 struct ttm_resource *res, 522 struct xe_res_cursor *cur, 523 u64 *L0, u64 *L0_ofs, u32 *L0_pt, 524 u32 cmd_size, u32 pt_ofs, u32 avail_pts) 525 { 526 u32 cmds = 0; 527 bool is_vram = PTE_UPDATE_FLAG_IS_VRAM & flags; 528 bool is_comp_pte = PTE_UPDATE_FLAG_IS_COMP_PTE & flags; 529 530 *L0_pt = pt_ofs; 531 if (is_vram && xe_migrate_allow_identity(*L0, cur)) { 532 /* Offset into identity map. */ 533 *L0_ofs = xe_migrate_vram_ofs(tile_to_xe(m->tile), 534 cur->start + vram_region_gpu_offset(res), 535 is_comp_pte); 536 cmds += cmd_size; 537 } else { 538 /* Clip L0 to available size */ 539 u64 size = min(*L0, (u64)avail_pts * SZ_2M); 540 u32 num_4k_pages = (size + XE_PAGE_SIZE - 1) >> XE_PTE_SHIFT; 541 542 *L0 = size; 543 *L0_ofs = xe_migrate_vm_addr(pt_ofs, 0); 544 545 /* MI_STORE_DATA_IMM */ 546 cmds += 3 * DIV_ROUND_UP(num_4k_pages, MAX_PTE_PER_SDI); 547 548 /* PDE qwords */ 549 cmds += num_4k_pages * 2; 550 551 /* Each chunk has a single blit command */ 552 cmds += cmd_size; 553 } 554 555 return cmds; 556 } 557 558 static void emit_pte(struct xe_migrate *m, 559 struct xe_bb *bb, u32 at_pt, 560 bool is_vram, bool is_comp_pte, 561 struct xe_res_cursor *cur, 562 u32 size, struct ttm_resource *res) 563 { 564 struct xe_device *xe = tile_to_xe(m->tile); 565 struct xe_vm *vm = m->q->vm; 566 u16 pat_index; 567 u32 ptes; 568 u64 ofs = (u64)at_pt * XE_PAGE_SIZE; 569 u64 cur_ofs; 570 571 /* Indirect access needs compression enabled uncached PAT index */ 572 if (GRAPHICS_VERx100(xe) >= 2000) 573 pat_index = is_comp_pte ? xe->pat.idx[XE_CACHE_NONE_COMPRESSION] : 574 xe->pat.idx[XE_CACHE_WB]; 575 else 576 pat_index = xe->pat.idx[XE_CACHE_WB]; 577 578 ptes = DIV_ROUND_UP(size, XE_PAGE_SIZE); 579 580 while (ptes) { 581 u32 chunk = min(MAX_PTE_PER_SDI, ptes); 582 583 bb->cs[bb->len++] = MI_STORE_DATA_IMM | MI_SDI_NUM_QW(chunk); 584 bb->cs[bb->len++] = ofs; 585 bb->cs[bb->len++] = 0; 586 587 cur_ofs = ofs; 588 ofs += chunk * 8; 589 ptes -= chunk; 590 591 while (chunk--) { 592 u64 addr, flags = 0; 593 bool devmem = false; 594 595 addr = xe_res_dma(cur) & PAGE_MASK; 596 if (is_vram) { 597 if (vm->flags & XE_VM_FLAG_64K) { 598 u64 va = cur_ofs * XE_PAGE_SIZE / 8; 599 600 xe_assert(xe, (va & (SZ_64K - 1)) == 601 (addr & (SZ_64K - 1))); 602 603 flags |= XE_PTE_PS64; 604 } 605 606 addr += vram_region_gpu_offset(res); 607 devmem = true; 608 } 609 610 addr = vm->pt_ops->pte_encode_addr(m->tile->xe, 611 addr, pat_index, 612 0, devmem, flags); 613 bb->cs[bb->len++] = lower_32_bits(addr); 614 bb->cs[bb->len++] = upper_32_bits(addr); 615 616 xe_res_next(cur, min_t(u32, size, PAGE_SIZE)); 617 cur_ofs += 8; 618 } 619 } 620 } 621 622 #define EMIT_COPY_CCS_DW 5 623 static void emit_copy_ccs(struct xe_gt *gt, struct xe_bb *bb, 624 u64 dst_ofs, bool dst_is_indirect, 625 u64 src_ofs, bool src_is_indirect, 626 u32 size) 627 { 628 struct xe_device *xe = gt_to_xe(gt); 629 u32 *cs = bb->cs + bb->len; 630 u32 num_ccs_blks; 631 u32 num_pages; 632 u32 ccs_copy_size; 633 u32 mocs; 634 635 if (GRAPHICS_VERx100(xe) >= 2000) { 636 num_pages = DIV_ROUND_UP(size, XE_PAGE_SIZE); 637 xe_gt_assert(gt, FIELD_FIT(XE2_CCS_SIZE_MASK, num_pages - 1)); 638 639 ccs_copy_size = REG_FIELD_PREP(XE2_CCS_SIZE_MASK, num_pages - 1); 640 mocs = FIELD_PREP(XE2_XY_CTRL_SURF_MOCS_INDEX_MASK, gt->mocs.uc_index); 641 642 } else { 643 num_ccs_blks = DIV_ROUND_UP(xe_device_ccs_bytes(gt_to_xe(gt), size), 644 NUM_CCS_BYTES_PER_BLOCK); 645 xe_gt_assert(gt, FIELD_FIT(CCS_SIZE_MASK, num_ccs_blks - 1)); 646 647 ccs_copy_size = REG_FIELD_PREP(CCS_SIZE_MASK, num_ccs_blks - 1); 648 mocs = FIELD_PREP(XY_CTRL_SURF_MOCS_MASK, gt->mocs.uc_index); 649 } 650 651 *cs++ = XY_CTRL_SURF_COPY_BLT | 652 (src_is_indirect ? 0x0 : 0x1) << SRC_ACCESS_TYPE_SHIFT | 653 (dst_is_indirect ? 0x0 : 0x1) << DST_ACCESS_TYPE_SHIFT | 654 ccs_copy_size; 655 *cs++ = lower_32_bits(src_ofs); 656 *cs++ = upper_32_bits(src_ofs) | mocs; 657 *cs++ = lower_32_bits(dst_ofs); 658 *cs++ = upper_32_bits(dst_ofs) | mocs; 659 660 bb->len = cs - bb->cs; 661 } 662 663 #define EMIT_COPY_DW 10 664 static void emit_copy(struct xe_gt *gt, struct xe_bb *bb, 665 u64 src_ofs, u64 dst_ofs, unsigned int size, 666 unsigned int pitch) 667 { 668 struct xe_device *xe = gt_to_xe(gt); 669 u32 mocs = 0; 670 u32 tile_y = 0; 671 672 xe_gt_assert(gt, size / pitch <= S16_MAX); 673 xe_gt_assert(gt, pitch / 4 <= S16_MAX); 674 xe_gt_assert(gt, pitch <= U16_MAX); 675 676 if (GRAPHICS_VER(xe) >= 20) 677 mocs = FIELD_PREP(XE2_XY_FAST_COPY_BLT_MOCS_INDEX_MASK, gt->mocs.uc_index); 678 679 if (GRAPHICS_VERx100(xe) >= 1250) 680 tile_y = XY_FAST_COPY_BLT_D1_SRC_TILE4 | XY_FAST_COPY_BLT_D1_DST_TILE4; 681 682 bb->cs[bb->len++] = XY_FAST_COPY_BLT_CMD | (10 - 2); 683 bb->cs[bb->len++] = XY_FAST_COPY_BLT_DEPTH_32 | pitch | tile_y | mocs; 684 bb->cs[bb->len++] = 0; 685 bb->cs[bb->len++] = (size / pitch) << 16 | pitch / 4; 686 bb->cs[bb->len++] = lower_32_bits(dst_ofs); 687 bb->cs[bb->len++] = upper_32_bits(dst_ofs); 688 bb->cs[bb->len++] = 0; 689 bb->cs[bb->len++] = pitch | mocs; 690 bb->cs[bb->len++] = lower_32_bits(src_ofs); 691 bb->cs[bb->len++] = upper_32_bits(src_ofs); 692 } 693 694 static u64 xe_migrate_batch_base(struct xe_migrate *m, bool usm) 695 { 696 return usm ? m->usm_batch_base_ofs : m->batch_base_ofs; 697 } 698 699 static u32 xe_migrate_ccs_copy(struct xe_migrate *m, 700 struct xe_bb *bb, 701 u64 src_ofs, bool src_is_indirect, 702 u64 dst_ofs, bool dst_is_indirect, u32 dst_size, 703 u64 ccs_ofs, bool copy_ccs) 704 { 705 struct xe_gt *gt = m->tile->primary_gt; 706 u32 flush_flags = 0; 707 708 if (!copy_ccs && dst_is_indirect) { 709 /* 710 * If the src is already in vram, then it should already 711 * have been cleared by us, or has been populated by the 712 * user. Make sure we copy the CCS aux state as-is. 713 * 714 * Otherwise if the bo doesn't have any CCS metadata attached, 715 * we still need to clear it for security reasons. 716 */ 717 u64 ccs_src_ofs = src_is_indirect ? src_ofs : m->cleared_mem_ofs; 718 719 emit_copy_ccs(gt, bb, 720 dst_ofs, true, 721 ccs_src_ofs, src_is_indirect, dst_size); 722 723 flush_flags = MI_FLUSH_DW_CCS; 724 } else if (copy_ccs) { 725 if (!src_is_indirect) 726 src_ofs = ccs_ofs; 727 else if (!dst_is_indirect) 728 dst_ofs = ccs_ofs; 729 730 xe_gt_assert(gt, src_is_indirect || dst_is_indirect); 731 732 emit_copy_ccs(gt, bb, dst_ofs, dst_is_indirect, src_ofs, 733 src_is_indirect, dst_size); 734 if (dst_is_indirect) 735 flush_flags = MI_FLUSH_DW_CCS; 736 } 737 738 return flush_flags; 739 } 740 741 /** 742 * xe_migrate_copy() - Copy content of TTM resources. 743 * @m: The migration context. 744 * @src_bo: The buffer object @src is currently bound to. 745 * @dst_bo: If copying between resources created for the same bo, set this to 746 * the same value as @src_bo. If copying between buffer objects, set it to 747 * the buffer object @dst is currently bound to. 748 * @src: The source TTM resource. 749 * @dst: The dst TTM resource. 750 * @copy_only_ccs: If true copy only CCS metadata 751 * 752 * Copies the contents of @src to @dst: On flat CCS devices, 753 * the CCS metadata is copied as well if needed, or if not present, 754 * the CCS metadata of @dst is cleared for security reasons. 755 * 756 * Return: Pointer to a dma_fence representing the last copy batch, or 757 * an error pointer on failure. If there is a failure, any copy operation 758 * started by the function call has been synced. 759 */ 760 struct dma_fence *xe_migrate_copy(struct xe_migrate *m, 761 struct xe_bo *src_bo, 762 struct xe_bo *dst_bo, 763 struct ttm_resource *src, 764 struct ttm_resource *dst, 765 bool copy_only_ccs) 766 { 767 struct xe_gt *gt = m->tile->primary_gt; 768 struct xe_device *xe = gt_to_xe(gt); 769 struct dma_fence *fence = NULL; 770 u64 size = src_bo->size; 771 struct xe_res_cursor src_it, dst_it, ccs_it; 772 u64 src_L0_ofs, dst_L0_ofs; 773 u32 src_L0_pt, dst_L0_pt; 774 u64 src_L0, dst_L0; 775 int pass = 0; 776 int err; 777 bool src_is_pltt = src->mem_type == XE_PL_TT; 778 bool dst_is_pltt = dst->mem_type == XE_PL_TT; 779 bool src_is_vram = mem_type_is_vram(src->mem_type); 780 bool dst_is_vram = mem_type_is_vram(dst->mem_type); 781 bool type_device = src_bo->ttm.type == ttm_bo_type_device; 782 bool needs_ccs_emit = type_device && xe_migrate_needs_ccs_emit(xe); 783 bool copy_ccs = xe_device_has_flat_ccs(xe) && 784 xe_bo_needs_ccs_pages(src_bo) && xe_bo_needs_ccs_pages(dst_bo); 785 bool copy_system_ccs = copy_ccs && (!src_is_vram || !dst_is_vram); 786 bool use_comp_pat = type_device && xe_device_has_flat_ccs(xe) && 787 GRAPHICS_VER(xe) >= 20 && src_is_vram && !dst_is_vram; 788 789 /* Copying CCS between two different BOs is not supported yet. */ 790 if (XE_WARN_ON(copy_ccs && src_bo != dst_bo)) 791 return ERR_PTR(-EINVAL); 792 793 if (src_bo != dst_bo && XE_WARN_ON(src_bo->size != dst_bo->size)) 794 return ERR_PTR(-EINVAL); 795 796 if (!src_is_vram) 797 xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it); 798 else 799 xe_res_first(src, 0, size, &src_it); 800 if (!dst_is_vram) 801 xe_res_first_sg(xe_bo_sg(dst_bo), 0, size, &dst_it); 802 else 803 xe_res_first(dst, 0, size, &dst_it); 804 805 if (copy_system_ccs) 806 xe_res_first_sg(xe_bo_sg(src_bo), xe_bo_ccs_pages_start(src_bo), 807 PAGE_ALIGN(xe_device_ccs_bytes(xe, size)), 808 &ccs_it); 809 810 while (size) { 811 u32 batch_size = 2; /* arb_clear() + MI_BATCH_BUFFER_END */ 812 struct xe_sched_job *job; 813 struct xe_bb *bb; 814 u32 flush_flags = 0; 815 u32 update_idx; 816 u64 ccs_ofs, ccs_size; 817 u32 ccs_pt; 818 u32 pte_flags; 819 820 bool usm = xe->info.has_usm; 821 u32 avail_pts = max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENCODE_SIZE; 822 823 src_L0 = xe_migrate_res_sizes(m, &src_it); 824 dst_L0 = xe_migrate_res_sizes(m, &dst_it); 825 826 drm_dbg(&xe->drm, "Pass %u, sizes: %llu & %llu\n", 827 pass++, src_L0, dst_L0); 828 829 src_L0 = min(src_L0, dst_L0); 830 831 pte_flags = src_is_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0; 832 pte_flags |= use_comp_pat ? PTE_UPDATE_FLAG_IS_COMP_PTE : 0; 833 batch_size += pte_update_size(m, pte_flags, src, &src_it, &src_L0, 834 &src_L0_ofs, &src_L0_pt, 0, 0, 835 avail_pts); 836 837 pte_flags = dst_is_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0; 838 batch_size += pte_update_size(m, pte_flags, dst, &dst_it, &src_L0, 839 &dst_L0_ofs, &dst_L0_pt, 0, 840 avail_pts, avail_pts); 841 842 if (copy_system_ccs) { 843 xe_assert(xe, type_device); 844 ccs_size = xe_device_ccs_bytes(xe, src_L0); 845 batch_size += pte_update_size(m, 0, NULL, &ccs_it, &ccs_size, 846 &ccs_ofs, &ccs_pt, 0, 847 2 * avail_pts, 848 avail_pts); 849 xe_assert(xe, IS_ALIGNED(ccs_it.start, PAGE_SIZE)); 850 } 851 852 /* Add copy commands size here */ 853 batch_size += ((copy_only_ccs) ? 0 : EMIT_COPY_DW) + 854 ((needs_ccs_emit ? EMIT_COPY_CCS_DW : 0)); 855 856 bb = xe_bb_new(gt, batch_size, usm); 857 if (IS_ERR(bb)) { 858 err = PTR_ERR(bb); 859 goto err_sync; 860 } 861 862 if (src_is_vram && xe_migrate_allow_identity(src_L0, &src_it)) 863 xe_res_next(&src_it, src_L0); 864 else 865 emit_pte(m, bb, src_L0_pt, src_is_vram, copy_system_ccs, 866 &src_it, src_L0, src); 867 868 if (dst_is_vram && xe_migrate_allow_identity(src_L0, &dst_it)) 869 xe_res_next(&dst_it, src_L0); 870 else 871 emit_pte(m, bb, dst_L0_pt, dst_is_vram, copy_system_ccs, 872 &dst_it, src_L0, dst); 873 874 if (copy_system_ccs) 875 emit_pte(m, bb, ccs_pt, false, false, &ccs_it, ccs_size, src); 876 877 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 878 update_idx = bb->len; 879 880 if (!copy_only_ccs) 881 emit_copy(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0, XE_PAGE_SIZE); 882 883 if (needs_ccs_emit) 884 flush_flags = xe_migrate_ccs_copy(m, bb, src_L0_ofs, 885 IS_DGFX(xe) ? src_is_vram : src_is_pltt, 886 dst_L0_ofs, 887 IS_DGFX(xe) ? dst_is_vram : dst_is_pltt, 888 src_L0, ccs_ofs, copy_ccs); 889 890 job = xe_bb_create_migration_job(m->q, bb, 891 xe_migrate_batch_base(m, usm), 892 update_idx); 893 if (IS_ERR(job)) { 894 err = PTR_ERR(job); 895 goto err; 896 } 897 898 xe_sched_job_add_migrate_flush(job, flush_flags); 899 if (!fence) { 900 err = xe_sched_job_add_deps(job, src_bo->ttm.base.resv, 901 DMA_RESV_USAGE_BOOKKEEP); 902 if (!err && src_bo != dst_bo) 903 err = xe_sched_job_add_deps(job, dst_bo->ttm.base.resv, 904 DMA_RESV_USAGE_BOOKKEEP); 905 if (err) 906 goto err_job; 907 } 908 909 mutex_lock(&m->job_mutex); 910 xe_sched_job_arm(job); 911 dma_fence_put(fence); 912 fence = dma_fence_get(&job->drm.s_fence->finished); 913 xe_sched_job_push(job); 914 915 dma_fence_put(m->fence); 916 m->fence = dma_fence_get(fence); 917 918 mutex_unlock(&m->job_mutex); 919 920 xe_bb_free(bb, fence); 921 size -= src_L0; 922 continue; 923 924 err_job: 925 xe_sched_job_put(job); 926 err: 927 xe_bb_free(bb, NULL); 928 929 err_sync: 930 /* Sync partial copy if any. FIXME: under job_mutex? */ 931 if (fence) { 932 dma_fence_wait(fence, false); 933 dma_fence_put(fence); 934 } 935 936 return ERR_PTR(err); 937 } 938 939 return fence; 940 } 941 942 static void emit_clear_link_copy(struct xe_gt *gt, struct xe_bb *bb, u64 src_ofs, 943 u32 size, u32 pitch) 944 { 945 struct xe_device *xe = gt_to_xe(gt); 946 u32 *cs = bb->cs + bb->len; 947 u32 len = PVC_MEM_SET_CMD_LEN_DW; 948 949 *cs++ = PVC_MEM_SET_CMD | PVC_MEM_SET_MATRIX | (len - 2); 950 *cs++ = pitch - 1; 951 *cs++ = (size / pitch) - 1; 952 *cs++ = pitch - 1; 953 *cs++ = lower_32_bits(src_ofs); 954 *cs++ = upper_32_bits(src_ofs); 955 if (GRAPHICS_VERx100(xe) >= 2000) 956 *cs++ = FIELD_PREP(XE2_MEM_SET_MOCS_INDEX_MASK, gt->mocs.uc_index); 957 else 958 *cs++ = FIELD_PREP(PVC_MEM_SET_MOCS_INDEX_MASK, gt->mocs.uc_index); 959 960 xe_gt_assert(gt, cs - bb->cs == len + bb->len); 961 962 bb->len += len; 963 } 964 965 static void emit_clear_main_copy(struct xe_gt *gt, struct xe_bb *bb, 966 u64 src_ofs, u32 size, u32 pitch, bool is_vram) 967 { 968 struct xe_device *xe = gt_to_xe(gt); 969 u32 *cs = bb->cs + bb->len; 970 u32 len = XY_FAST_COLOR_BLT_DW; 971 972 if (GRAPHICS_VERx100(xe) < 1250) 973 len = 11; 974 975 *cs++ = XY_FAST_COLOR_BLT_CMD | XY_FAST_COLOR_BLT_DEPTH_32 | 976 (len - 2); 977 if (GRAPHICS_VERx100(xe) >= 2000) 978 *cs++ = FIELD_PREP(XE2_XY_FAST_COLOR_BLT_MOCS_INDEX_MASK, gt->mocs.uc_index) | 979 (pitch - 1); 980 else 981 *cs++ = FIELD_PREP(XY_FAST_COLOR_BLT_MOCS_MASK, gt->mocs.uc_index) | 982 (pitch - 1); 983 *cs++ = 0; 984 *cs++ = (size / pitch) << 16 | pitch / 4; 985 *cs++ = lower_32_bits(src_ofs); 986 *cs++ = upper_32_bits(src_ofs); 987 *cs++ = (is_vram ? 0x0 : 0x1) << XY_FAST_COLOR_BLT_MEM_TYPE_SHIFT; 988 *cs++ = 0; 989 *cs++ = 0; 990 *cs++ = 0; 991 *cs++ = 0; 992 993 if (len > 11) { 994 *cs++ = 0; 995 *cs++ = 0; 996 *cs++ = 0; 997 *cs++ = 0; 998 *cs++ = 0; 999 } 1000 1001 xe_gt_assert(gt, cs - bb->cs == len + bb->len); 1002 1003 bb->len += len; 1004 } 1005 1006 static bool has_service_copy_support(struct xe_gt *gt) 1007 { 1008 /* 1009 * What we care about is whether the architecture was designed with 1010 * service copy functionality (specifically the new MEM_SET / MEM_COPY 1011 * instructions) so check the architectural engine list rather than the 1012 * actual list since these instructions are usable on BCS0 even if 1013 * all of the actual service copy engines (BCS1-BCS8) have been fused 1014 * off. 1015 */ 1016 return gt->info.engine_mask & GENMASK(XE_HW_ENGINE_BCS8, 1017 XE_HW_ENGINE_BCS1); 1018 } 1019 1020 static u32 emit_clear_cmd_len(struct xe_gt *gt) 1021 { 1022 if (has_service_copy_support(gt)) 1023 return PVC_MEM_SET_CMD_LEN_DW; 1024 else 1025 return XY_FAST_COLOR_BLT_DW; 1026 } 1027 1028 static void emit_clear(struct xe_gt *gt, struct xe_bb *bb, u64 src_ofs, 1029 u32 size, u32 pitch, bool is_vram) 1030 { 1031 if (has_service_copy_support(gt)) 1032 emit_clear_link_copy(gt, bb, src_ofs, size, pitch); 1033 else 1034 emit_clear_main_copy(gt, bb, src_ofs, size, pitch, 1035 is_vram); 1036 } 1037 1038 /** 1039 * xe_migrate_clear() - Copy content of TTM resources. 1040 * @m: The migration context. 1041 * @bo: The buffer object @dst is currently bound to. 1042 * @dst: The dst TTM resource to be cleared. 1043 * @clear_flags: flags to specify which data to clear: CCS, BO, or both. 1044 * 1045 * Clear the contents of @dst to zero when XE_MIGRATE_CLEAR_FLAG_BO_DATA is set. 1046 * On flat CCS devices, the CCS metadata is cleared to zero with XE_MIGRATE_CLEAR_FLAG_CCS_DATA. 1047 * Set XE_MIGRATE_CLEAR_FLAG_FULL to clear bo as well as CCS metadata. 1048 * TODO: Eliminate the @bo argument. 1049 * 1050 * Return: Pointer to a dma_fence representing the last clear batch, or 1051 * an error pointer on failure. If there is a failure, any clear operation 1052 * started by the function call has been synced. 1053 */ 1054 struct dma_fence *xe_migrate_clear(struct xe_migrate *m, 1055 struct xe_bo *bo, 1056 struct ttm_resource *dst, 1057 u32 clear_flags) 1058 { 1059 bool clear_vram = mem_type_is_vram(dst->mem_type); 1060 bool clear_bo_data = XE_MIGRATE_CLEAR_FLAG_BO_DATA & clear_flags; 1061 bool clear_ccs = XE_MIGRATE_CLEAR_FLAG_CCS_DATA & clear_flags; 1062 struct xe_gt *gt = m->tile->primary_gt; 1063 struct xe_device *xe = gt_to_xe(gt); 1064 bool clear_only_system_ccs = false; 1065 struct dma_fence *fence = NULL; 1066 u64 size = bo->size; 1067 struct xe_res_cursor src_it; 1068 struct ttm_resource *src = dst; 1069 int err; 1070 1071 if (WARN_ON(!clear_bo_data && !clear_ccs)) 1072 return NULL; 1073 1074 if (!clear_bo_data && clear_ccs && !IS_DGFX(xe)) 1075 clear_only_system_ccs = true; 1076 1077 if (!clear_vram) 1078 xe_res_first_sg(xe_bo_sg(bo), 0, bo->size, &src_it); 1079 else 1080 xe_res_first(src, 0, bo->size, &src_it); 1081 1082 while (size) { 1083 u64 clear_L0_ofs; 1084 u32 clear_L0_pt; 1085 u32 flush_flags = 0; 1086 u64 clear_L0; 1087 struct xe_sched_job *job; 1088 struct xe_bb *bb; 1089 u32 batch_size, update_idx; 1090 u32 pte_flags; 1091 1092 bool usm = xe->info.has_usm; 1093 u32 avail_pts = max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENCODE_SIZE; 1094 1095 clear_L0 = xe_migrate_res_sizes(m, &src_it); 1096 1097 /* Calculate final sizes and batch size.. */ 1098 pte_flags = clear_vram ? PTE_UPDATE_FLAG_IS_VRAM : 0; 1099 batch_size = 2 + 1100 pte_update_size(m, pte_flags, src, &src_it, 1101 &clear_L0, &clear_L0_ofs, &clear_L0_pt, 1102 clear_bo_data ? emit_clear_cmd_len(gt) : 0, 0, 1103 avail_pts); 1104 1105 if (xe_migrate_needs_ccs_emit(xe)) 1106 batch_size += EMIT_COPY_CCS_DW; 1107 1108 /* Clear commands */ 1109 1110 if (WARN_ON_ONCE(!clear_L0)) 1111 break; 1112 1113 bb = xe_bb_new(gt, batch_size, usm); 1114 if (IS_ERR(bb)) { 1115 err = PTR_ERR(bb); 1116 goto err_sync; 1117 } 1118 1119 size -= clear_L0; 1120 /* Preemption is enabled again by the ring ops. */ 1121 if (clear_vram && xe_migrate_allow_identity(clear_L0, &src_it)) 1122 xe_res_next(&src_it, clear_L0); 1123 else 1124 emit_pte(m, bb, clear_L0_pt, clear_vram, clear_only_system_ccs, 1125 &src_it, clear_L0, dst); 1126 1127 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 1128 update_idx = bb->len; 1129 1130 if (clear_bo_data) 1131 emit_clear(gt, bb, clear_L0_ofs, clear_L0, XE_PAGE_SIZE, clear_vram); 1132 1133 if (xe_migrate_needs_ccs_emit(xe)) { 1134 emit_copy_ccs(gt, bb, clear_L0_ofs, true, 1135 m->cleared_mem_ofs, false, clear_L0); 1136 flush_flags = MI_FLUSH_DW_CCS; 1137 } 1138 1139 job = xe_bb_create_migration_job(m->q, bb, 1140 xe_migrate_batch_base(m, usm), 1141 update_idx); 1142 if (IS_ERR(job)) { 1143 err = PTR_ERR(job); 1144 goto err; 1145 } 1146 1147 xe_sched_job_add_migrate_flush(job, flush_flags); 1148 if (!fence) { 1149 /* 1150 * There can't be anything userspace related at this 1151 * point, so we just need to respect any potential move 1152 * fences, which are always tracked as 1153 * DMA_RESV_USAGE_KERNEL. 1154 */ 1155 err = xe_sched_job_add_deps(job, bo->ttm.base.resv, 1156 DMA_RESV_USAGE_KERNEL); 1157 if (err) 1158 goto err_job; 1159 } 1160 1161 mutex_lock(&m->job_mutex); 1162 xe_sched_job_arm(job); 1163 dma_fence_put(fence); 1164 fence = dma_fence_get(&job->drm.s_fence->finished); 1165 xe_sched_job_push(job); 1166 1167 dma_fence_put(m->fence); 1168 m->fence = dma_fence_get(fence); 1169 1170 mutex_unlock(&m->job_mutex); 1171 1172 xe_bb_free(bb, fence); 1173 continue; 1174 1175 err_job: 1176 xe_sched_job_put(job); 1177 err: 1178 xe_bb_free(bb, NULL); 1179 err_sync: 1180 /* Sync partial copies if any. FIXME: job_mutex? */ 1181 if (fence) { 1182 dma_fence_wait(fence, false); 1183 dma_fence_put(fence); 1184 } 1185 1186 return ERR_PTR(err); 1187 } 1188 1189 if (clear_ccs) 1190 bo->ccs_cleared = true; 1191 1192 return fence; 1193 } 1194 1195 static void write_pgtable(struct xe_tile *tile, struct xe_bb *bb, u64 ppgtt_ofs, 1196 const struct xe_vm_pgtable_update_op *pt_op, 1197 const struct xe_vm_pgtable_update *update, 1198 struct xe_migrate_pt_update *pt_update) 1199 { 1200 const struct xe_migrate_pt_update_ops *ops = pt_update->ops; 1201 u32 chunk; 1202 u32 ofs = update->ofs, size = update->qwords; 1203 1204 /* 1205 * If we have 512 entries (max), we would populate it ourselves, 1206 * and update the PDE above it to the new pointer. 1207 * The only time this can only happen if we have to update the top 1208 * PDE. This requires a BO that is almost vm->size big. 1209 * 1210 * This shouldn't be possible in practice.. might change when 16K 1211 * pages are used. Hence the assert. 1212 */ 1213 xe_tile_assert(tile, update->qwords < MAX_NUM_PTE); 1214 if (!ppgtt_ofs) 1215 ppgtt_ofs = xe_migrate_vram_ofs(tile_to_xe(tile), 1216 xe_bo_addr(update->pt_bo, 0, 1217 XE_PAGE_SIZE), false); 1218 1219 do { 1220 u64 addr = ppgtt_ofs + ofs * 8; 1221 1222 chunk = min(size, MAX_PTE_PER_SDI); 1223 1224 /* Ensure populatefn can do memset64 by aligning bb->cs */ 1225 if (!(bb->len & 1)) 1226 bb->cs[bb->len++] = MI_NOOP; 1227 1228 bb->cs[bb->len++] = MI_STORE_DATA_IMM | MI_SDI_NUM_QW(chunk); 1229 bb->cs[bb->len++] = lower_32_bits(addr); 1230 bb->cs[bb->len++] = upper_32_bits(addr); 1231 if (pt_op->bind) 1232 ops->populate(pt_update, tile, NULL, bb->cs + bb->len, 1233 ofs, chunk, update); 1234 else 1235 ops->clear(pt_update, tile, NULL, bb->cs + bb->len, 1236 ofs, chunk, update); 1237 1238 bb->len += chunk * 2; 1239 ofs += chunk; 1240 size -= chunk; 1241 } while (size); 1242 } 1243 1244 struct xe_vm *xe_migrate_get_vm(struct xe_migrate *m) 1245 { 1246 return xe_vm_get(m->q->vm); 1247 } 1248 1249 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST) 1250 struct migrate_test_params { 1251 struct xe_test_priv base; 1252 bool force_gpu; 1253 }; 1254 1255 #define to_migrate_test_params(_priv) \ 1256 container_of(_priv, struct migrate_test_params, base) 1257 #endif 1258 1259 static struct dma_fence * 1260 xe_migrate_update_pgtables_cpu(struct xe_migrate *m, 1261 struct xe_migrate_pt_update *pt_update) 1262 { 1263 XE_TEST_DECLARE(struct migrate_test_params *test = 1264 to_migrate_test_params 1265 (xe_cur_kunit_priv(XE_TEST_LIVE_MIGRATE));) 1266 const struct xe_migrate_pt_update_ops *ops = pt_update->ops; 1267 struct xe_vm *vm = pt_update->vops->vm; 1268 struct xe_vm_pgtable_update_ops *pt_update_ops = 1269 &pt_update->vops->pt_update_ops[pt_update->tile_id]; 1270 int err; 1271 u32 i, j; 1272 1273 if (XE_TEST_ONLY(test && test->force_gpu)) 1274 return ERR_PTR(-ETIME); 1275 1276 if (ops->pre_commit) { 1277 pt_update->job = NULL; 1278 err = ops->pre_commit(pt_update); 1279 if (err) 1280 return ERR_PTR(err); 1281 } 1282 1283 for (i = 0; i < pt_update_ops->num_ops; ++i) { 1284 const struct xe_vm_pgtable_update_op *pt_op = 1285 &pt_update_ops->ops[i]; 1286 1287 for (j = 0; j < pt_op->num_entries; j++) { 1288 const struct xe_vm_pgtable_update *update = 1289 &pt_op->entries[j]; 1290 1291 if (pt_op->bind) 1292 ops->populate(pt_update, m->tile, 1293 &update->pt_bo->vmap, NULL, 1294 update->ofs, update->qwords, 1295 update); 1296 else 1297 ops->clear(pt_update, m->tile, 1298 &update->pt_bo->vmap, NULL, 1299 update->ofs, update->qwords, update); 1300 } 1301 } 1302 1303 trace_xe_vm_cpu_bind(vm); 1304 xe_device_wmb(vm->xe); 1305 1306 return dma_fence_get_stub(); 1307 } 1308 1309 static struct dma_fence * 1310 __xe_migrate_update_pgtables(struct xe_migrate *m, 1311 struct xe_migrate_pt_update *pt_update, 1312 struct xe_vm_pgtable_update_ops *pt_update_ops) 1313 { 1314 const struct xe_migrate_pt_update_ops *ops = pt_update->ops; 1315 struct xe_tile *tile = m->tile; 1316 struct xe_gt *gt = tile->primary_gt; 1317 struct xe_device *xe = tile_to_xe(tile); 1318 struct xe_sched_job *job; 1319 struct dma_fence *fence; 1320 struct drm_suballoc *sa_bo = NULL; 1321 struct xe_bb *bb; 1322 u32 i, j, batch_size = 0, ppgtt_ofs, update_idx, page_ofs = 0; 1323 u32 num_updates = 0, current_update = 0; 1324 u64 addr; 1325 int err = 0; 1326 bool is_migrate = pt_update_ops->q == m->q; 1327 bool usm = is_migrate && xe->info.has_usm; 1328 1329 for (i = 0; i < pt_update_ops->num_ops; ++i) { 1330 struct xe_vm_pgtable_update_op *pt_op = &pt_update_ops->ops[i]; 1331 struct xe_vm_pgtable_update *updates = pt_op->entries; 1332 1333 num_updates += pt_op->num_entries; 1334 for (j = 0; j < pt_op->num_entries; ++j) { 1335 u32 num_cmds = DIV_ROUND_UP(updates[j].qwords, 1336 MAX_PTE_PER_SDI); 1337 1338 /* align noop + MI_STORE_DATA_IMM cmd prefix */ 1339 batch_size += 4 * num_cmds + updates[j].qwords * 2; 1340 } 1341 } 1342 1343 /* fixed + PTE entries */ 1344 if (IS_DGFX(xe)) 1345 batch_size += 2; 1346 else 1347 batch_size += 6 * (num_updates / MAX_PTE_PER_SDI + 1) + 1348 num_updates * 2; 1349 1350 bb = xe_bb_new(gt, batch_size, usm); 1351 if (IS_ERR(bb)) 1352 return ERR_CAST(bb); 1353 1354 /* For sysmem PTE's, need to map them in our hole.. */ 1355 if (!IS_DGFX(xe)) { 1356 u16 pat_index = xe->pat.idx[XE_CACHE_WB]; 1357 u32 ptes, ofs; 1358 1359 ppgtt_ofs = NUM_KERNEL_PDE - 1; 1360 if (!is_migrate) { 1361 u32 num_units = DIV_ROUND_UP(num_updates, 1362 NUM_VMUSA_WRITES_PER_UNIT); 1363 1364 if (num_units > m->vm_update_sa.size) { 1365 err = -ENOBUFS; 1366 goto err_bb; 1367 } 1368 sa_bo = drm_suballoc_new(&m->vm_update_sa, num_units, 1369 GFP_KERNEL, true, 0); 1370 if (IS_ERR(sa_bo)) { 1371 err = PTR_ERR(sa_bo); 1372 goto err_bb; 1373 } 1374 1375 ppgtt_ofs = NUM_KERNEL_PDE + 1376 (drm_suballoc_soffset(sa_bo) / 1377 NUM_VMUSA_UNIT_PER_PAGE); 1378 page_ofs = (drm_suballoc_soffset(sa_bo) % 1379 NUM_VMUSA_UNIT_PER_PAGE) * 1380 VM_SA_UPDATE_UNIT_SIZE; 1381 } 1382 1383 /* Map our PT's to gtt */ 1384 i = 0; 1385 j = 0; 1386 ptes = num_updates; 1387 ofs = ppgtt_ofs * XE_PAGE_SIZE + page_ofs; 1388 while (ptes) { 1389 u32 chunk = min(MAX_PTE_PER_SDI, ptes); 1390 u32 idx = 0; 1391 1392 bb->cs[bb->len++] = MI_STORE_DATA_IMM | 1393 MI_SDI_NUM_QW(chunk); 1394 bb->cs[bb->len++] = ofs; 1395 bb->cs[bb->len++] = 0; /* upper_32_bits */ 1396 1397 for (; i < pt_update_ops->num_ops; ++i) { 1398 struct xe_vm_pgtable_update_op *pt_op = 1399 &pt_update_ops->ops[i]; 1400 struct xe_vm_pgtable_update *updates = pt_op->entries; 1401 1402 for (; j < pt_op->num_entries; ++j, ++current_update, ++idx) { 1403 struct xe_vm *vm = pt_update->vops->vm; 1404 struct xe_bo *pt_bo = updates[j].pt_bo; 1405 1406 if (idx == chunk) 1407 goto next_cmd; 1408 1409 xe_tile_assert(tile, pt_bo->size == SZ_4K); 1410 1411 /* Map a PT at most once */ 1412 if (pt_bo->update_index < 0) 1413 pt_bo->update_index = current_update; 1414 1415 addr = vm->pt_ops->pte_encode_bo(pt_bo, 0, 1416 pat_index, 0); 1417 bb->cs[bb->len++] = lower_32_bits(addr); 1418 bb->cs[bb->len++] = upper_32_bits(addr); 1419 } 1420 1421 j = 0; 1422 } 1423 1424 next_cmd: 1425 ptes -= chunk; 1426 ofs += chunk * sizeof(u64); 1427 } 1428 1429 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 1430 update_idx = bb->len; 1431 1432 addr = xe_migrate_vm_addr(ppgtt_ofs, 0) + 1433 (page_ofs / sizeof(u64)) * XE_PAGE_SIZE; 1434 for (i = 0; i < pt_update_ops->num_ops; ++i) { 1435 struct xe_vm_pgtable_update_op *pt_op = 1436 &pt_update_ops->ops[i]; 1437 struct xe_vm_pgtable_update *updates = pt_op->entries; 1438 1439 for (j = 0; j < pt_op->num_entries; ++j) { 1440 struct xe_bo *pt_bo = updates[j].pt_bo; 1441 1442 write_pgtable(tile, bb, addr + 1443 pt_bo->update_index * XE_PAGE_SIZE, 1444 pt_op, &updates[j], pt_update); 1445 } 1446 } 1447 } else { 1448 /* phys pages, no preamble required */ 1449 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 1450 update_idx = bb->len; 1451 1452 for (i = 0; i < pt_update_ops->num_ops; ++i) { 1453 struct xe_vm_pgtable_update_op *pt_op = 1454 &pt_update_ops->ops[i]; 1455 struct xe_vm_pgtable_update *updates = pt_op->entries; 1456 1457 for (j = 0; j < pt_op->num_entries; ++j) 1458 write_pgtable(tile, bb, 0, pt_op, &updates[j], 1459 pt_update); 1460 } 1461 } 1462 1463 job = xe_bb_create_migration_job(pt_update_ops->q, bb, 1464 xe_migrate_batch_base(m, usm), 1465 update_idx); 1466 if (IS_ERR(job)) { 1467 err = PTR_ERR(job); 1468 goto err_sa; 1469 } 1470 1471 if (ops->pre_commit) { 1472 pt_update->job = job; 1473 err = ops->pre_commit(pt_update); 1474 if (err) 1475 goto err_job; 1476 } 1477 if (is_migrate) 1478 mutex_lock(&m->job_mutex); 1479 1480 xe_sched_job_arm(job); 1481 fence = dma_fence_get(&job->drm.s_fence->finished); 1482 xe_sched_job_push(job); 1483 1484 if (is_migrate) 1485 mutex_unlock(&m->job_mutex); 1486 1487 xe_bb_free(bb, fence); 1488 drm_suballoc_free(sa_bo, fence); 1489 1490 return fence; 1491 1492 err_job: 1493 xe_sched_job_put(job); 1494 err_sa: 1495 drm_suballoc_free(sa_bo, NULL); 1496 err_bb: 1497 xe_bb_free(bb, NULL); 1498 return ERR_PTR(err); 1499 } 1500 1501 /** 1502 * xe_migrate_update_pgtables() - Pipelined page-table update 1503 * @m: The migrate context. 1504 * @pt_update: PT update arguments 1505 * 1506 * Perform a pipelined page-table update. The update descriptors are typically 1507 * built under the same lock critical section as a call to this function. If 1508 * using the default engine for the updates, they will be performed in the 1509 * order they grab the job_mutex. If different engines are used, external 1510 * synchronization is needed for overlapping updates to maintain page-table 1511 * consistency. Note that the meaning of "overlapping" is that the updates 1512 * touch the same page-table, which might be a higher-level page-directory. 1513 * If no pipelining is needed, then updates may be performed by the cpu. 1514 * 1515 * Return: A dma_fence that, when signaled, indicates the update completion. 1516 */ 1517 struct dma_fence * 1518 xe_migrate_update_pgtables(struct xe_migrate *m, 1519 struct xe_migrate_pt_update *pt_update) 1520 1521 { 1522 struct xe_vm_pgtable_update_ops *pt_update_ops = 1523 &pt_update->vops->pt_update_ops[pt_update->tile_id]; 1524 struct dma_fence *fence; 1525 1526 fence = xe_migrate_update_pgtables_cpu(m, pt_update); 1527 1528 /* -ETIME indicates a job is needed, anything else is legit error */ 1529 if (!IS_ERR(fence) || PTR_ERR(fence) != -ETIME) 1530 return fence; 1531 1532 return __xe_migrate_update_pgtables(m, pt_update, pt_update_ops); 1533 } 1534 1535 /** 1536 * xe_migrate_wait() - Complete all operations using the xe_migrate context 1537 * @m: Migrate context to wait for. 1538 * 1539 * Waits until the GPU no longer uses the migrate context's default engine 1540 * or its page-table objects. FIXME: What about separate page-table update 1541 * engines? 1542 */ 1543 void xe_migrate_wait(struct xe_migrate *m) 1544 { 1545 if (m->fence) 1546 dma_fence_wait(m->fence, false); 1547 } 1548 1549 #if IS_ENABLED(CONFIG_DRM_XE_DEVMEM_MIRROR) 1550 static u32 pte_update_cmd_size(u64 size) 1551 { 1552 u32 num_dword; 1553 u64 entries = DIV_U64_ROUND_UP(size, XE_PAGE_SIZE); 1554 1555 XE_WARN_ON(size > MAX_PREEMPTDISABLE_TRANSFER); 1556 /* 1557 * MI_STORE_DATA_IMM command is used to update page table. Each 1558 * instruction can update maximumly 0x1ff pte entries. To update 1559 * n (n <= 0x1ff) pte entries, we need: 1560 * 1 dword for the MI_STORE_DATA_IMM command header (opcode etc) 1561 * 2 dword for the page table's physical location 1562 * 2*n dword for value of pte to fill (each pte entry is 2 dwords) 1563 */ 1564 num_dword = (1 + 2) * DIV_U64_ROUND_UP(entries, 0x1ff); 1565 num_dword += entries * 2; 1566 1567 return num_dword; 1568 } 1569 1570 static void build_pt_update_batch_sram(struct xe_migrate *m, 1571 struct xe_bb *bb, u32 pt_offset, 1572 dma_addr_t *sram_addr, u32 size) 1573 { 1574 u16 pat_index = tile_to_xe(m->tile)->pat.idx[XE_CACHE_WB]; 1575 u32 ptes; 1576 int i = 0; 1577 1578 ptes = DIV_ROUND_UP(size, XE_PAGE_SIZE); 1579 while (ptes) { 1580 u32 chunk = min(0x1ffU, ptes); 1581 1582 bb->cs[bb->len++] = MI_STORE_DATA_IMM | MI_SDI_NUM_QW(chunk); 1583 bb->cs[bb->len++] = pt_offset; 1584 bb->cs[bb->len++] = 0; 1585 1586 pt_offset += chunk * 8; 1587 ptes -= chunk; 1588 1589 while (chunk--) { 1590 u64 addr = sram_addr[i++] & PAGE_MASK; 1591 1592 xe_tile_assert(m->tile, addr); 1593 addr = m->q->vm->pt_ops->pte_encode_addr(m->tile->xe, 1594 addr, pat_index, 1595 0, false, 0); 1596 bb->cs[bb->len++] = lower_32_bits(addr); 1597 bb->cs[bb->len++] = upper_32_bits(addr); 1598 } 1599 } 1600 } 1601 1602 enum xe_migrate_copy_dir { 1603 XE_MIGRATE_COPY_TO_VRAM, 1604 XE_MIGRATE_COPY_TO_SRAM, 1605 }; 1606 1607 static struct dma_fence *xe_migrate_vram(struct xe_migrate *m, 1608 unsigned long npages, 1609 dma_addr_t *sram_addr, u64 vram_addr, 1610 const enum xe_migrate_copy_dir dir) 1611 { 1612 struct xe_gt *gt = m->tile->primary_gt; 1613 struct xe_device *xe = gt_to_xe(gt); 1614 bool use_usm_batch = xe->info.has_usm; 1615 struct dma_fence *fence = NULL; 1616 u32 batch_size = 2; 1617 u64 src_L0_ofs, dst_L0_ofs; 1618 u64 round_update_size; 1619 struct xe_sched_job *job; 1620 struct xe_bb *bb; 1621 u32 update_idx, pt_slot = 0; 1622 int err; 1623 1624 if (npages * PAGE_SIZE > MAX_PREEMPTDISABLE_TRANSFER) 1625 return ERR_PTR(-EINVAL); 1626 1627 round_update_size = npages * PAGE_SIZE; 1628 batch_size += pte_update_cmd_size(round_update_size); 1629 batch_size += EMIT_COPY_DW; 1630 1631 bb = xe_bb_new(gt, batch_size, use_usm_batch); 1632 if (IS_ERR(bb)) { 1633 err = PTR_ERR(bb); 1634 return ERR_PTR(err); 1635 } 1636 1637 build_pt_update_batch_sram(m, bb, pt_slot * XE_PAGE_SIZE, 1638 sram_addr, round_update_size); 1639 1640 if (dir == XE_MIGRATE_COPY_TO_VRAM) { 1641 src_L0_ofs = xe_migrate_vm_addr(pt_slot, 0); 1642 dst_L0_ofs = xe_migrate_vram_ofs(xe, vram_addr, false); 1643 1644 } else { 1645 src_L0_ofs = xe_migrate_vram_ofs(xe, vram_addr, false); 1646 dst_L0_ofs = xe_migrate_vm_addr(pt_slot, 0); 1647 } 1648 1649 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 1650 update_idx = bb->len; 1651 1652 emit_copy(gt, bb, src_L0_ofs, dst_L0_ofs, round_update_size, 1653 XE_PAGE_SIZE); 1654 1655 job = xe_bb_create_migration_job(m->q, bb, 1656 xe_migrate_batch_base(m, use_usm_batch), 1657 update_idx); 1658 if (IS_ERR(job)) { 1659 err = PTR_ERR(job); 1660 goto err; 1661 } 1662 1663 xe_sched_job_add_migrate_flush(job, 0); 1664 1665 mutex_lock(&m->job_mutex); 1666 xe_sched_job_arm(job); 1667 fence = dma_fence_get(&job->drm.s_fence->finished); 1668 xe_sched_job_push(job); 1669 1670 dma_fence_put(m->fence); 1671 m->fence = dma_fence_get(fence); 1672 mutex_unlock(&m->job_mutex); 1673 1674 xe_bb_free(bb, fence); 1675 1676 return fence; 1677 1678 err: 1679 xe_bb_free(bb, NULL); 1680 1681 return ERR_PTR(err); 1682 } 1683 1684 /** 1685 * xe_migrate_to_vram() - Migrate to VRAM 1686 * @m: The migration context. 1687 * @npages: Number of pages to migrate. 1688 * @src_addr: Array of dma addresses (source of migrate) 1689 * @dst_addr: Device physical address of VRAM (destination of migrate) 1690 * 1691 * Copy from an array dma addresses to a VRAM device physical address 1692 * 1693 * Return: dma fence for migrate to signal completion on succees, ERR_PTR on 1694 * failure 1695 */ 1696 struct dma_fence *xe_migrate_to_vram(struct xe_migrate *m, 1697 unsigned long npages, 1698 dma_addr_t *src_addr, 1699 u64 dst_addr) 1700 { 1701 return xe_migrate_vram(m, npages, src_addr, dst_addr, 1702 XE_MIGRATE_COPY_TO_VRAM); 1703 } 1704 1705 /** 1706 * xe_migrate_from_vram() - Migrate from VRAM 1707 * @m: The migration context. 1708 * @npages: Number of pages to migrate. 1709 * @src_addr: Device physical address of VRAM (source of migrate) 1710 * @dst_addr: Array of dma addresses (destination of migrate) 1711 * 1712 * Copy from a VRAM device physical address to an array dma addresses 1713 * 1714 * Return: dma fence for migrate to signal completion on succees, ERR_PTR on 1715 * failure 1716 */ 1717 struct dma_fence *xe_migrate_from_vram(struct xe_migrate *m, 1718 unsigned long npages, 1719 u64 src_addr, 1720 dma_addr_t *dst_addr) 1721 { 1722 return xe_migrate_vram(m, npages, dst_addr, src_addr, 1723 XE_MIGRATE_COPY_TO_SRAM); 1724 } 1725 1726 #endif 1727 1728 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST) 1729 #include "tests/xe_migrate.c" 1730 #endif 1731