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 <drm/xe_drm.h> 14 15 #include "generated/xe_wa_oob.h" 16 #include "instructions/xe_mi_commands.h" 17 #include "regs/xe_gpu_commands.h" 18 #include "tests/xe_test.h" 19 #include "xe_assert.h" 20 #include "xe_bb.h" 21 #include "xe_bo.h" 22 #include "xe_exec_queue.h" 23 #include "xe_ggtt.h" 24 #include "xe_gt.h" 25 #include "xe_hw_engine.h" 26 #include "xe_lrc.h" 27 #include "xe_map.h" 28 #include "xe_mocs.h" 29 #include "xe_pt.h" 30 #include "xe_res_cursor.h" 31 #include "xe_sched_job.h" 32 #include "xe_sync.h" 33 #include "xe_trace.h" 34 #include "xe_vm.h" 35 #include "xe_wa.h" 36 37 /** 38 * struct xe_migrate - migrate context. 39 */ 40 struct xe_migrate { 41 /** @q: Default exec queue used for migration */ 42 struct xe_exec_queue *q; 43 /** @tile: Backpointer to the tile this struct xe_migrate belongs to. */ 44 struct xe_tile *tile; 45 /** @job_mutex: Timeline mutex for @eng. */ 46 struct mutex job_mutex; 47 /** @pt_bo: Page-table buffer object. */ 48 struct xe_bo *pt_bo; 49 /** @batch_base_ofs: VM offset of the migration batch buffer */ 50 u64 batch_base_ofs; 51 /** @usm_batch_base_ofs: VM offset of the usm batch buffer */ 52 u64 usm_batch_base_ofs; 53 /** @cleared_mem_ofs: VM offset of @cleared_bo. */ 54 u64 cleared_mem_ofs; 55 /** 56 * @fence: dma-fence representing the last migration job batch. 57 * Protected by @job_mutex. 58 */ 59 struct dma_fence *fence; 60 /** 61 * @vm_update_sa: For integrated, used to suballocate page-tables 62 * out of the pt_bo. 63 */ 64 struct drm_suballoc_manager vm_update_sa; 65 }; 66 67 #define MAX_PREEMPTDISABLE_TRANSFER SZ_8M /* Around 1ms. */ 68 #define MAX_CCS_LIMITED_TRANSFER SZ_4M /* XE_PAGE_SIZE * (FIELD_MAX(XE2_CCS_SIZE_MASK) + 1) */ 69 #define NUM_KERNEL_PDE 17 70 #define NUM_PT_SLOTS 32 71 #define LEVEL0_PAGE_TABLE_ENCODE_SIZE SZ_2M 72 73 /** 74 * xe_tile_migrate_engine() - Get this tile's migrate engine. 75 * @tile: The tile. 76 * 77 * Returns the default migrate engine of this tile. 78 * TODO: Perhaps this function is slightly misplaced, and even unneeded? 79 * 80 * Return: The default migrate engine 81 */ 82 struct xe_exec_queue *xe_tile_migrate_engine(struct xe_tile *tile) 83 { 84 return tile->migrate->q; 85 } 86 87 static void xe_migrate_fini(struct drm_device *dev, void *arg) 88 { 89 struct xe_migrate *m = arg; 90 91 xe_vm_lock(m->q->vm, false); 92 xe_bo_unpin(m->pt_bo); 93 xe_vm_unlock(m->q->vm); 94 95 dma_fence_put(m->fence); 96 xe_bo_put(m->pt_bo); 97 drm_suballoc_manager_fini(&m->vm_update_sa); 98 mutex_destroy(&m->job_mutex); 99 xe_vm_close_and_put(m->q->vm); 100 xe_exec_queue_put(m->q); 101 } 102 103 static u64 xe_migrate_vm_addr(u64 slot, u32 level) 104 { 105 XE_WARN_ON(slot >= NUM_PT_SLOTS); 106 107 /* First slot is reserved for mapping of PT bo and bb, start from 1 */ 108 return (slot + 1ULL) << xe_pt_shift(level + 1); 109 } 110 111 static u64 xe_migrate_vram_ofs(struct xe_device *xe, u64 addr) 112 { 113 /* 114 * Remove the DPA to get a correct offset into identity table for the 115 * migrate offset 116 */ 117 addr -= xe->mem.vram.dpa_base; 118 return addr + (256ULL << xe_pt_shift(2)); 119 } 120 121 static int xe_migrate_prepare_vm(struct xe_tile *tile, struct xe_migrate *m, 122 struct xe_vm *vm) 123 { 124 struct xe_device *xe = tile_to_xe(tile); 125 u16 pat_index = xe->pat.idx[XE_CACHE_WB]; 126 u8 id = tile->id; 127 u32 num_entries = NUM_PT_SLOTS, num_level = vm->pt_root[id]->level; 128 u32 map_ofs, level, i; 129 struct xe_bo *bo, *batch = tile->mem.kernel_bb_pool->bo; 130 u64 entry; 131 132 /* Can't bump NUM_PT_SLOTS too high */ 133 BUILD_BUG_ON(NUM_PT_SLOTS > SZ_2M/XE_PAGE_SIZE); 134 /* Must be a multiple of 64K to support all platforms */ 135 BUILD_BUG_ON(NUM_PT_SLOTS * XE_PAGE_SIZE % SZ_64K); 136 /* And one slot reserved for the 4KiB page table updates */ 137 BUILD_BUG_ON(!(NUM_KERNEL_PDE & 1)); 138 139 /* Need to be sure everything fits in the first PT, or create more */ 140 xe_tile_assert(tile, m->batch_base_ofs + batch->size < SZ_2M); 141 142 bo = xe_bo_create_pin_map(vm->xe, tile, vm, 143 num_entries * XE_PAGE_SIZE, 144 ttm_bo_type_kernel, 145 XE_BO_CREATE_VRAM_IF_DGFX(tile) | 146 XE_BO_CREATE_PINNED_BIT); 147 if (IS_ERR(bo)) 148 return PTR_ERR(bo); 149 150 entry = vm->pt_ops->pde_encode_bo(bo, bo->size - XE_PAGE_SIZE, pat_index); 151 xe_pt_write(xe, &vm->pt_root[id]->bo->vmap, 0, entry); 152 153 map_ofs = (num_entries - num_level) * XE_PAGE_SIZE; 154 155 /* Map the entire BO in our level 0 pt */ 156 for (i = 0, level = 0; i < num_entries; level++) { 157 entry = vm->pt_ops->pte_encode_bo(bo, i * XE_PAGE_SIZE, 158 pat_index, 0); 159 160 xe_map_wr(xe, &bo->vmap, map_ofs + level * 8, u64, entry); 161 162 if (vm->flags & XE_VM_FLAG_64K) 163 i += 16; 164 else 165 i += 1; 166 } 167 168 if (!IS_DGFX(xe)) { 169 /* Write out batch too */ 170 m->batch_base_ofs = NUM_PT_SLOTS * XE_PAGE_SIZE; 171 if (xe->info.has_usm) { 172 batch = tile->primary_gt->usm.bb_pool->bo; 173 m->usm_batch_base_ofs = m->batch_base_ofs; 174 } 175 176 for (i = 0; i < batch->size; 177 i += vm->flags & XE_VM_FLAG_64K ? XE_64K_PAGE_SIZE : 178 XE_PAGE_SIZE) { 179 entry = vm->pt_ops->pte_encode_bo(batch, i, 180 pat_index, 0); 181 182 xe_map_wr(xe, &bo->vmap, map_ofs + level * 8, u64, 183 entry); 184 level++; 185 } 186 } else { 187 u64 batch_addr = xe_bo_addr(batch, 0, XE_PAGE_SIZE); 188 189 m->batch_base_ofs = xe_migrate_vram_ofs(xe, batch_addr); 190 191 if (xe->info.has_usm) { 192 batch = tile->primary_gt->usm.bb_pool->bo; 193 batch_addr = xe_bo_addr(batch, 0, XE_PAGE_SIZE); 194 m->usm_batch_base_ofs = xe_migrate_vram_ofs(xe, batch_addr); 195 } 196 } 197 198 for (level = 1; level < num_level; level++) { 199 u32 flags = 0; 200 201 if (vm->flags & XE_VM_FLAG_64K && level == 1) 202 flags = XE_PDE_64K; 203 204 entry = vm->pt_ops->pde_encode_bo(bo, map_ofs + (level - 1) * 205 XE_PAGE_SIZE, pat_index); 206 xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE * level, u64, 207 entry | flags); 208 } 209 210 /* Write PDE's that point to our BO. */ 211 for (i = 0; i < num_entries - num_level; i++) { 212 entry = vm->pt_ops->pde_encode_bo(bo, i * XE_PAGE_SIZE, 213 pat_index); 214 215 xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE + 216 (i + 1) * 8, u64, entry); 217 } 218 219 /* Set up a 1GiB NULL mapping at 255GiB offset. */ 220 level = 2; 221 xe_map_wr(xe, &bo->vmap, map_ofs + XE_PAGE_SIZE * level + 255 * 8, u64, 222 vm->pt_ops->pte_encode_addr(xe, 0, pat_index, level, IS_DGFX(xe), 0) 223 | XE_PTE_NULL); 224 m->cleared_mem_ofs = (255ULL << xe_pt_shift(level)); 225 226 /* Identity map the entire vram at 256GiB offset */ 227 if (IS_DGFX(xe)) { 228 u64 pos, ofs, flags; 229 230 level = 2; 231 ofs = map_ofs + XE_PAGE_SIZE * level + 256 * 8; 232 flags = vm->pt_ops->pte_encode_addr(xe, 0, pat_index, level, 233 true, 0); 234 235 /* 236 * Use 1GB pages, it shouldn't matter the physical amount of 237 * vram is less, when we don't access it. 238 */ 239 for (pos = xe->mem.vram.dpa_base; 240 pos < xe->mem.vram.actual_physical_size + xe->mem.vram.dpa_base; 241 pos += SZ_1G, ofs += 8) 242 xe_map_wr(xe, &bo->vmap, ofs, u64, pos | flags); 243 } 244 245 /* 246 * Example layout created above, with root level = 3: 247 * [PT0...PT7]: kernel PT's for copy/clear; 64 or 4KiB PTE's 248 * [PT8]: Kernel PT for VM_BIND, 4 KiB PTE's 249 * [PT9...PT28]: Userspace PT's for VM_BIND, 4 KiB PTE's 250 * [PT29 = PDE 0] [PT30 = PDE 1] [PT31 = PDE 2] 251 * 252 * This makes the lowest part of the VM point to the pagetables. 253 * Hence the lowest 2M in the vm should point to itself, with a few writes 254 * and flushes, other parts of the VM can be used either for copying and 255 * clearing. 256 * 257 * For performance, the kernel reserves PDE's, so about 20 are left 258 * for async VM updates. 259 * 260 * To make it easier to work, each scratch PT is put in slot (1 + PT #) 261 * everywhere, this allows lockless updates to scratch pages by using 262 * the different addresses in VM. 263 */ 264 #define NUM_VMUSA_UNIT_PER_PAGE 32 265 #define VM_SA_UPDATE_UNIT_SIZE (XE_PAGE_SIZE / NUM_VMUSA_UNIT_PER_PAGE) 266 #define NUM_VMUSA_WRITES_PER_UNIT (VM_SA_UPDATE_UNIT_SIZE / sizeof(u64)) 267 drm_suballoc_manager_init(&m->vm_update_sa, 268 (map_ofs / XE_PAGE_SIZE - NUM_KERNEL_PDE) * 269 NUM_VMUSA_UNIT_PER_PAGE, 0); 270 271 m->pt_bo = bo; 272 return 0; 273 } 274 275 /* 276 * Due to workaround 16017236439, odd instance hardware copy engines are 277 * faster than even instance ones. 278 * This function returns the mask involving all fast copy engines and the 279 * reserved copy engine to be used as logical mask for migrate engine. 280 * Including the reserved copy engine is required to avoid deadlocks due to 281 * migrate jobs servicing the faults gets stuck behind the job that faulted. 282 */ 283 static u32 xe_migrate_usm_logical_mask(struct xe_gt *gt) 284 { 285 u32 logical_mask = 0; 286 struct xe_hw_engine *hwe; 287 enum xe_hw_engine_id id; 288 289 for_each_hw_engine(hwe, gt, id) { 290 if (hwe->class != XE_ENGINE_CLASS_COPY) 291 continue; 292 293 if (!XE_WA(gt, 16017236439) || 294 xe_gt_is_usm_hwe(gt, hwe) || hwe->instance & 1) 295 logical_mask |= BIT(hwe->logical_instance); 296 } 297 298 return logical_mask; 299 } 300 301 /** 302 * xe_migrate_init() - Initialize a migrate context 303 * @tile: Back-pointer to the tile we're initializing for. 304 * 305 * Return: Pointer to a migrate context on success. Error pointer on error. 306 */ 307 struct xe_migrate *xe_migrate_init(struct xe_tile *tile) 308 { 309 struct xe_device *xe = tile_to_xe(tile); 310 struct xe_gt *primary_gt = tile->primary_gt; 311 struct xe_migrate *m; 312 struct xe_vm *vm; 313 int err; 314 315 m = drmm_kzalloc(&xe->drm, sizeof(*m), GFP_KERNEL); 316 if (!m) 317 return ERR_PTR(-ENOMEM); 318 319 m->tile = tile; 320 321 /* Special layout, prepared below.. */ 322 vm = xe_vm_create(xe, XE_VM_FLAG_MIGRATION | 323 XE_VM_FLAG_SET_TILE_ID(tile)); 324 if (IS_ERR(vm)) 325 return ERR_CAST(vm); 326 327 xe_vm_lock(vm, false); 328 err = xe_migrate_prepare_vm(tile, m, vm); 329 xe_vm_unlock(vm); 330 if (err) { 331 xe_vm_close_and_put(vm); 332 return ERR_PTR(err); 333 } 334 335 if (xe->info.has_usm) { 336 struct xe_hw_engine *hwe = xe_gt_hw_engine(primary_gt, 337 XE_ENGINE_CLASS_COPY, 338 primary_gt->usm.reserved_bcs_instance, 339 false); 340 u32 logical_mask = xe_migrate_usm_logical_mask(primary_gt); 341 342 if (!hwe || !logical_mask) 343 return ERR_PTR(-EINVAL); 344 345 m->q = xe_exec_queue_create(xe, vm, logical_mask, 1, hwe, 346 EXEC_QUEUE_FLAG_KERNEL | 347 EXEC_QUEUE_FLAG_PERMANENT); 348 } else { 349 m->q = xe_exec_queue_create_class(xe, primary_gt, vm, 350 XE_ENGINE_CLASS_COPY, 351 EXEC_QUEUE_FLAG_KERNEL | 352 EXEC_QUEUE_FLAG_PERMANENT); 353 } 354 if (IS_ERR(m->q)) { 355 xe_vm_close_and_put(vm); 356 return ERR_CAST(m->q); 357 } 358 if (xe->info.has_usm) 359 m->q->priority = XE_EXEC_QUEUE_PRIORITY_KERNEL; 360 361 mutex_init(&m->job_mutex); 362 363 err = drmm_add_action_or_reset(&xe->drm, xe_migrate_fini, m); 364 if (err) 365 return ERR_PTR(err); 366 367 return m; 368 } 369 370 static u64 max_mem_transfer_per_pass(struct xe_device *xe) 371 { 372 if (!IS_DGFX(xe) && xe_device_has_flat_ccs(xe)) 373 return MAX_CCS_LIMITED_TRANSFER; 374 375 return MAX_PREEMPTDISABLE_TRANSFER; 376 } 377 378 static u64 xe_migrate_res_sizes(struct xe_device *xe, struct xe_res_cursor *cur) 379 { 380 /* 381 * For VRAM we use identity mapped pages so we are limited to current 382 * cursor size. For system we program the pages ourselves so we have no 383 * such limitation. 384 */ 385 return min_t(u64, max_mem_transfer_per_pass(xe), 386 mem_type_is_vram(cur->mem_type) ? cur->size : 387 cur->remaining); 388 } 389 390 static u32 pte_update_size(struct xe_migrate *m, 391 bool is_vram, 392 struct ttm_resource *res, 393 struct xe_res_cursor *cur, 394 u64 *L0, u64 *L0_ofs, u32 *L0_pt, 395 u32 cmd_size, u32 pt_ofs, u32 avail_pts) 396 { 397 u32 cmds = 0; 398 399 *L0_pt = pt_ofs; 400 if (!is_vram) { 401 /* Clip L0 to available size */ 402 u64 size = min(*L0, (u64)avail_pts * SZ_2M); 403 u64 num_4k_pages = DIV_ROUND_UP(size, XE_PAGE_SIZE); 404 405 *L0 = size; 406 *L0_ofs = xe_migrate_vm_addr(pt_ofs, 0); 407 408 /* MI_STORE_DATA_IMM */ 409 cmds += 3 * DIV_ROUND_UP(num_4k_pages, 0x1ff); 410 411 /* PDE qwords */ 412 cmds += num_4k_pages * 2; 413 414 /* Each chunk has a single blit command */ 415 cmds += cmd_size; 416 } else { 417 /* Offset into identity map. */ 418 *L0_ofs = xe_migrate_vram_ofs(tile_to_xe(m->tile), 419 cur->start + vram_region_gpu_offset(res)); 420 cmds += cmd_size; 421 } 422 423 return cmds; 424 } 425 426 static void emit_pte(struct xe_migrate *m, 427 struct xe_bb *bb, u32 at_pt, 428 bool is_vram, bool is_comp_pte, 429 struct xe_res_cursor *cur, 430 u32 size, struct xe_bo *bo) 431 { 432 struct xe_device *xe = tile_to_xe(m->tile); 433 434 u16 pat_index; 435 u32 ptes; 436 u64 ofs = at_pt * XE_PAGE_SIZE; 437 u64 cur_ofs; 438 439 /* Indirect access needs compression enabled uncached PAT index */ 440 if (GRAPHICS_VERx100(xe) >= 2000) 441 pat_index = is_comp_pte ? xe->pat.idx[XE_CACHE_NONE_COMPRESSION] : 442 xe->pat.idx[XE_CACHE_NONE]; 443 else 444 pat_index = xe->pat.idx[XE_CACHE_WB]; 445 446 /* 447 * FIXME: Emitting VRAM PTEs to L0 PTs is forbidden. Currently 448 * we're only emitting VRAM PTEs during sanity tests, so when 449 * that's moved to a Kunit test, we should condition VRAM PTEs 450 * on running tests. 451 */ 452 453 ptes = DIV_ROUND_UP(size, XE_PAGE_SIZE); 454 455 while (ptes) { 456 u32 chunk = min(0x1ffU, ptes); 457 458 bb->cs[bb->len++] = MI_STORE_DATA_IMM | MI_SDI_NUM_QW(chunk); 459 bb->cs[bb->len++] = ofs; 460 bb->cs[bb->len++] = 0; 461 462 cur_ofs = ofs; 463 ofs += chunk * 8; 464 ptes -= chunk; 465 466 while (chunk--) { 467 u64 addr, flags = 0; 468 bool devmem = false; 469 470 addr = xe_res_dma(cur) & PAGE_MASK; 471 if (is_vram) { 472 /* Is this a 64K PTE entry? */ 473 if ((m->q->vm->flags & XE_VM_FLAG_64K) && 474 !(cur_ofs & (16 * 8 - 1))) { 475 xe_tile_assert(m->tile, IS_ALIGNED(addr, SZ_64K)); 476 flags |= XE_PTE_PS64; 477 } 478 479 addr += vram_region_gpu_offset(bo->ttm.resource); 480 devmem = true; 481 } 482 483 addr = m->q->vm->pt_ops->pte_encode_addr(m->tile->xe, 484 addr, pat_index, 485 0, devmem, flags); 486 bb->cs[bb->len++] = lower_32_bits(addr); 487 bb->cs[bb->len++] = upper_32_bits(addr); 488 489 xe_res_next(cur, min_t(u32, size, PAGE_SIZE)); 490 cur_ofs += 8; 491 } 492 } 493 } 494 495 #define EMIT_COPY_CCS_DW 5 496 static void emit_copy_ccs(struct xe_gt *gt, struct xe_bb *bb, 497 u64 dst_ofs, bool dst_is_indirect, 498 u64 src_ofs, bool src_is_indirect, 499 u32 size) 500 { 501 struct xe_device *xe = gt_to_xe(gt); 502 u32 *cs = bb->cs + bb->len; 503 u32 num_ccs_blks; 504 u32 num_pages; 505 u32 ccs_copy_size; 506 u32 mocs; 507 508 if (GRAPHICS_VERx100(xe) >= 2000) { 509 num_pages = DIV_ROUND_UP(size, XE_PAGE_SIZE); 510 xe_gt_assert(gt, FIELD_FIT(XE2_CCS_SIZE_MASK, num_pages - 1)); 511 512 ccs_copy_size = REG_FIELD_PREP(XE2_CCS_SIZE_MASK, num_pages - 1); 513 mocs = FIELD_PREP(XE2_XY_CTRL_SURF_MOCS_INDEX_MASK, gt->mocs.uc_index); 514 515 } else { 516 num_ccs_blks = DIV_ROUND_UP(xe_device_ccs_bytes(gt_to_xe(gt), size), 517 NUM_CCS_BYTES_PER_BLOCK); 518 xe_gt_assert(gt, FIELD_FIT(CCS_SIZE_MASK, num_ccs_blks - 1)); 519 520 ccs_copy_size = REG_FIELD_PREP(CCS_SIZE_MASK, num_ccs_blks - 1); 521 mocs = FIELD_PREP(XY_CTRL_SURF_MOCS_MASK, gt->mocs.uc_index); 522 } 523 524 *cs++ = XY_CTRL_SURF_COPY_BLT | 525 (src_is_indirect ? 0x0 : 0x1) << SRC_ACCESS_TYPE_SHIFT | 526 (dst_is_indirect ? 0x0 : 0x1) << DST_ACCESS_TYPE_SHIFT | 527 ccs_copy_size; 528 *cs++ = lower_32_bits(src_ofs); 529 *cs++ = upper_32_bits(src_ofs) | mocs; 530 *cs++ = lower_32_bits(dst_ofs); 531 *cs++ = upper_32_bits(dst_ofs) | mocs; 532 533 bb->len = cs - bb->cs; 534 } 535 536 #define EMIT_COPY_DW 10 537 static void emit_copy(struct xe_gt *gt, struct xe_bb *bb, 538 u64 src_ofs, u64 dst_ofs, unsigned int size, 539 unsigned int pitch) 540 { 541 struct xe_device *xe = gt_to_xe(gt); 542 u32 mocs = 0; 543 u32 tile_y = 0; 544 545 xe_gt_assert(gt, size / pitch <= S16_MAX); 546 xe_gt_assert(gt, pitch / 4 <= S16_MAX); 547 xe_gt_assert(gt, pitch <= U16_MAX); 548 549 if (GRAPHICS_VER(xe) >= 20) 550 mocs = FIELD_PREP(XE2_XY_FAST_COPY_BLT_MOCS_INDEX_MASK, gt->mocs.uc_index); 551 552 if (GRAPHICS_VERx100(xe) >= 1250) 553 tile_y = XY_FAST_COPY_BLT_D1_SRC_TILE4 | XY_FAST_COPY_BLT_D1_DST_TILE4; 554 555 bb->cs[bb->len++] = XY_FAST_COPY_BLT_CMD | (10 - 2); 556 bb->cs[bb->len++] = XY_FAST_COPY_BLT_DEPTH_32 | pitch | tile_y | mocs; 557 bb->cs[bb->len++] = 0; 558 bb->cs[bb->len++] = (size / pitch) << 16 | pitch / 4; 559 bb->cs[bb->len++] = lower_32_bits(dst_ofs); 560 bb->cs[bb->len++] = upper_32_bits(dst_ofs); 561 bb->cs[bb->len++] = 0; 562 bb->cs[bb->len++] = pitch | mocs; 563 bb->cs[bb->len++] = lower_32_bits(src_ofs); 564 bb->cs[bb->len++] = upper_32_bits(src_ofs); 565 } 566 567 static int job_add_deps(struct xe_sched_job *job, struct dma_resv *resv, 568 enum dma_resv_usage usage) 569 { 570 return drm_sched_job_add_resv_dependencies(&job->drm, resv, usage); 571 } 572 573 static u64 xe_migrate_batch_base(struct xe_migrate *m, bool usm) 574 { 575 return usm ? m->usm_batch_base_ofs : m->batch_base_ofs; 576 } 577 578 static u32 xe_migrate_ccs_copy(struct xe_migrate *m, 579 struct xe_bb *bb, 580 u64 src_ofs, bool src_is_indirect, 581 u64 dst_ofs, bool dst_is_indirect, u32 dst_size, 582 u64 ccs_ofs, bool copy_ccs) 583 { 584 struct xe_gt *gt = m->tile->primary_gt; 585 u32 flush_flags = 0; 586 587 if (xe_device_has_flat_ccs(gt_to_xe(gt)) && !copy_ccs && dst_is_indirect) { 588 /* 589 * If the src is already in vram, then it should already 590 * have been cleared by us, or has been populated by the 591 * user. Make sure we copy the CCS aux state as-is. 592 * 593 * Otherwise if the bo doesn't have any CCS metadata attached, 594 * we still need to clear it for security reasons. 595 */ 596 u64 ccs_src_ofs = src_is_indirect ? src_ofs : m->cleared_mem_ofs; 597 598 emit_copy_ccs(gt, bb, 599 dst_ofs, true, 600 ccs_src_ofs, src_is_indirect, dst_size); 601 602 flush_flags = MI_FLUSH_DW_CCS; 603 } else if (copy_ccs) { 604 if (!src_is_indirect) 605 src_ofs = ccs_ofs; 606 else if (!dst_is_indirect) 607 dst_ofs = ccs_ofs; 608 609 xe_gt_assert(gt, src_is_indirect || dst_is_indirect); 610 611 emit_copy_ccs(gt, bb, dst_ofs, dst_is_indirect, src_ofs, 612 src_is_indirect, dst_size); 613 if (dst_is_indirect) 614 flush_flags = MI_FLUSH_DW_CCS; 615 } 616 617 return flush_flags; 618 } 619 620 /** 621 * xe_migrate_copy() - Copy content of TTM resources. 622 * @m: The migration context. 623 * @src_bo: The buffer object @src is currently bound to. 624 * @dst_bo: If copying between resources created for the same bo, set this to 625 * the same value as @src_bo. If copying between buffer objects, set it to 626 * the buffer object @dst is currently bound to. 627 * @src: The source TTM resource. 628 * @dst: The dst TTM resource. 629 * @copy_only_ccs: If true copy only CCS metadata 630 * 631 * Copies the contents of @src to @dst: On flat CCS devices, 632 * the CCS metadata is copied as well if needed, or if not present, 633 * the CCS metadata of @dst is cleared for security reasons. 634 * 635 * Return: Pointer to a dma_fence representing the last copy batch, or 636 * an error pointer on failure. If there is a failure, any copy operation 637 * started by the function call has been synced. 638 */ 639 struct dma_fence *xe_migrate_copy(struct xe_migrate *m, 640 struct xe_bo *src_bo, 641 struct xe_bo *dst_bo, 642 struct ttm_resource *src, 643 struct ttm_resource *dst, 644 bool copy_only_ccs) 645 { 646 struct xe_gt *gt = m->tile->primary_gt; 647 struct xe_device *xe = gt_to_xe(gt); 648 struct dma_fence *fence = NULL; 649 u64 size = src_bo->size; 650 struct xe_res_cursor src_it, dst_it, ccs_it; 651 u64 src_L0_ofs, dst_L0_ofs; 652 u32 src_L0_pt, dst_L0_pt; 653 u64 src_L0, dst_L0; 654 int pass = 0; 655 int err; 656 bool src_is_pltt = src->mem_type == XE_PL_TT; 657 bool dst_is_pltt = dst->mem_type == XE_PL_TT; 658 bool src_is_vram = mem_type_is_vram(src->mem_type); 659 bool dst_is_vram = mem_type_is_vram(dst->mem_type); 660 bool copy_ccs = xe_device_has_flat_ccs(xe) && 661 xe_bo_needs_ccs_pages(src_bo) && xe_bo_needs_ccs_pages(dst_bo); 662 bool copy_system_ccs = copy_ccs && (!src_is_vram || !dst_is_vram); 663 664 /* Copying CCS between two different BOs is not supported yet. */ 665 if (XE_WARN_ON(copy_ccs && src_bo != dst_bo)) 666 return ERR_PTR(-EINVAL); 667 668 if (src_bo != dst_bo && XE_WARN_ON(src_bo->size != dst_bo->size)) 669 return ERR_PTR(-EINVAL); 670 671 if (!src_is_vram) 672 xe_res_first_sg(xe_bo_sg(src_bo), 0, size, &src_it); 673 else 674 xe_res_first(src, 0, size, &src_it); 675 if (!dst_is_vram) 676 xe_res_first_sg(xe_bo_sg(dst_bo), 0, size, &dst_it); 677 else 678 xe_res_first(dst, 0, size, &dst_it); 679 680 if (copy_system_ccs) 681 xe_res_first_sg(xe_bo_sg(src_bo), xe_bo_ccs_pages_start(src_bo), 682 PAGE_ALIGN(xe_device_ccs_bytes(xe, size)), 683 &ccs_it); 684 685 while (size) { 686 u32 batch_size = 2; /* arb_clear() + MI_BATCH_BUFFER_END */ 687 struct xe_sched_job *job; 688 struct xe_bb *bb; 689 u32 flush_flags; 690 u32 update_idx; 691 u64 ccs_ofs, ccs_size; 692 u32 ccs_pt; 693 694 bool usm = xe->info.has_usm; 695 u32 avail_pts = max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENCODE_SIZE; 696 697 src_L0 = xe_migrate_res_sizes(xe, &src_it); 698 dst_L0 = xe_migrate_res_sizes(xe, &dst_it); 699 700 drm_dbg(&xe->drm, "Pass %u, sizes: %llu & %llu\n", 701 pass++, src_L0, dst_L0); 702 703 src_L0 = min(src_L0, dst_L0); 704 705 batch_size += pte_update_size(m, src_is_vram, src, &src_it, &src_L0, 706 &src_L0_ofs, &src_L0_pt, 0, 0, 707 avail_pts); 708 709 batch_size += pte_update_size(m, dst_is_vram, dst, &dst_it, &src_L0, 710 &dst_L0_ofs, &dst_L0_pt, 0, 711 avail_pts, avail_pts); 712 713 if (copy_system_ccs) { 714 ccs_size = xe_device_ccs_bytes(xe, src_L0); 715 batch_size += pte_update_size(m, false, NULL, &ccs_it, &ccs_size, 716 &ccs_ofs, &ccs_pt, 0, 717 2 * avail_pts, 718 avail_pts); 719 } 720 721 /* Add copy commands size here */ 722 batch_size += ((copy_only_ccs) ? 0 : EMIT_COPY_DW) + 723 ((xe_device_has_flat_ccs(xe) ? EMIT_COPY_CCS_DW : 0)); 724 725 bb = xe_bb_new(gt, batch_size, usm); 726 if (IS_ERR(bb)) { 727 err = PTR_ERR(bb); 728 goto err_sync; 729 } 730 731 if (!src_is_vram) 732 emit_pte(m, bb, src_L0_pt, src_is_vram, true, &src_it, src_L0, 733 src_bo); 734 else 735 xe_res_next(&src_it, src_L0); 736 737 if (!dst_is_vram) 738 emit_pte(m, bb, dst_L0_pt, dst_is_vram, true, &dst_it, src_L0, 739 dst_bo); 740 else 741 xe_res_next(&dst_it, src_L0); 742 743 if (copy_system_ccs) 744 emit_pte(m, bb, ccs_pt, false, false, &ccs_it, ccs_size, src_bo); 745 746 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 747 update_idx = bb->len; 748 749 if (!copy_only_ccs) 750 emit_copy(gt, bb, src_L0_ofs, dst_L0_ofs, src_L0, XE_PAGE_SIZE); 751 752 flush_flags = xe_migrate_ccs_copy(m, bb, src_L0_ofs, 753 IS_DGFX(xe) ? src_is_vram : src_is_pltt, 754 dst_L0_ofs, 755 IS_DGFX(xe) ? dst_is_vram : dst_is_pltt, 756 src_L0, ccs_ofs, copy_ccs); 757 758 mutex_lock(&m->job_mutex); 759 job = xe_bb_create_migration_job(m->q, bb, 760 xe_migrate_batch_base(m, usm), 761 update_idx); 762 if (IS_ERR(job)) { 763 err = PTR_ERR(job); 764 goto err; 765 } 766 767 xe_sched_job_add_migrate_flush(job, flush_flags); 768 if (!fence) { 769 err = job_add_deps(job, src_bo->ttm.base.resv, 770 DMA_RESV_USAGE_BOOKKEEP); 771 if (!err && src_bo != dst_bo) 772 err = job_add_deps(job, dst_bo->ttm.base.resv, 773 DMA_RESV_USAGE_BOOKKEEP); 774 if (err) 775 goto err_job; 776 } 777 778 xe_sched_job_arm(job); 779 dma_fence_put(fence); 780 fence = dma_fence_get(&job->drm.s_fence->finished); 781 xe_sched_job_push(job); 782 783 dma_fence_put(m->fence); 784 m->fence = dma_fence_get(fence); 785 786 mutex_unlock(&m->job_mutex); 787 788 xe_bb_free(bb, fence); 789 size -= src_L0; 790 continue; 791 792 err_job: 793 xe_sched_job_put(job); 794 err: 795 mutex_unlock(&m->job_mutex); 796 xe_bb_free(bb, NULL); 797 798 err_sync: 799 /* Sync partial copy if any. FIXME: under job_mutex? */ 800 if (fence) { 801 dma_fence_wait(fence, false); 802 dma_fence_put(fence); 803 } 804 805 return ERR_PTR(err); 806 } 807 808 return fence; 809 } 810 811 static void emit_clear_link_copy(struct xe_gt *gt, struct xe_bb *bb, u64 src_ofs, 812 u32 size, u32 pitch) 813 { 814 struct xe_device *xe = gt_to_xe(gt); 815 u32 *cs = bb->cs + bb->len; 816 u32 len = PVC_MEM_SET_CMD_LEN_DW; 817 818 *cs++ = PVC_MEM_SET_CMD | PVC_MEM_SET_MATRIX | (len - 2); 819 *cs++ = pitch - 1; 820 *cs++ = (size / pitch) - 1; 821 *cs++ = pitch - 1; 822 *cs++ = lower_32_bits(src_ofs); 823 *cs++ = upper_32_bits(src_ofs); 824 if (GRAPHICS_VERx100(xe) >= 2000) 825 *cs++ = FIELD_PREP(XE2_MEM_SET_MOCS_INDEX_MASK, gt->mocs.uc_index); 826 else 827 *cs++ = FIELD_PREP(PVC_MEM_SET_MOCS_INDEX_MASK, gt->mocs.uc_index); 828 829 xe_gt_assert(gt, cs - bb->cs == len + bb->len); 830 831 bb->len += len; 832 } 833 834 static void emit_clear_main_copy(struct xe_gt *gt, struct xe_bb *bb, 835 u64 src_ofs, u32 size, u32 pitch, bool is_vram) 836 { 837 struct xe_device *xe = gt_to_xe(gt); 838 u32 *cs = bb->cs + bb->len; 839 u32 len = XY_FAST_COLOR_BLT_DW; 840 841 if (GRAPHICS_VERx100(xe) < 1250) 842 len = 11; 843 844 *cs++ = XY_FAST_COLOR_BLT_CMD | XY_FAST_COLOR_BLT_DEPTH_32 | 845 (len - 2); 846 if (GRAPHICS_VERx100(xe) >= 2000) 847 *cs++ = FIELD_PREP(XE2_XY_FAST_COLOR_BLT_MOCS_INDEX_MASK, gt->mocs.uc_index) | 848 (pitch - 1); 849 else 850 *cs++ = FIELD_PREP(XY_FAST_COLOR_BLT_MOCS_MASK, gt->mocs.uc_index) | 851 (pitch - 1); 852 *cs++ = 0; 853 *cs++ = (size / pitch) << 16 | pitch / 4; 854 *cs++ = lower_32_bits(src_ofs); 855 *cs++ = upper_32_bits(src_ofs); 856 *cs++ = (is_vram ? 0x0 : 0x1) << XY_FAST_COLOR_BLT_MEM_TYPE_SHIFT; 857 *cs++ = 0; 858 *cs++ = 0; 859 *cs++ = 0; 860 *cs++ = 0; 861 862 if (len > 11) { 863 *cs++ = 0; 864 *cs++ = 0; 865 *cs++ = 0; 866 *cs++ = 0; 867 *cs++ = 0; 868 } 869 870 xe_gt_assert(gt, cs - bb->cs == len + bb->len); 871 872 bb->len += len; 873 } 874 875 static bool has_service_copy_support(struct xe_gt *gt) 876 { 877 /* 878 * What we care about is whether the architecture was designed with 879 * service copy functionality (specifically the new MEM_SET / MEM_COPY 880 * instructions) so check the architectural engine list rather than the 881 * actual list since these instructions are usable on BCS0 even if 882 * all of the actual service copy engines (BCS1-BCS8) have been fused 883 * off. 884 */ 885 return gt->info.__engine_mask & GENMASK(XE_HW_ENGINE_BCS8, 886 XE_HW_ENGINE_BCS1); 887 } 888 889 static u32 emit_clear_cmd_len(struct xe_gt *gt) 890 { 891 if (has_service_copy_support(gt)) 892 return PVC_MEM_SET_CMD_LEN_DW; 893 else 894 return XY_FAST_COLOR_BLT_DW; 895 } 896 897 static void emit_clear(struct xe_gt *gt, struct xe_bb *bb, u64 src_ofs, 898 u32 size, u32 pitch, bool is_vram) 899 { 900 if (has_service_copy_support(gt)) 901 emit_clear_link_copy(gt, bb, src_ofs, size, pitch); 902 else 903 emit_clear_main_copy(gt, bb, src_ofs, size, pitch, 904 is_vram); 905 } 906 907 /** 908 * xe_migrate_clear() - Copy content of TTM resources. 909 * @m: The migration context. 910 * @bo: The buffer object @dst is currently bound to. 911 * @dst: The dst TTM resource to be cleared. 912 * 913 * Clear the contents of @dst to zero. On flat CCS devices, 914 * the CCS metadata is cleared to zero as well on VRAM destinations. 915 * TODO: Eliminate the @bo argument. 916 * 917 * Return: Pointer to a dma_fence representing the last clear batch, or 918 * an error pointer on failure. If there is a failure, any clear operation 919 * started by the function call has been synced. 920 */ 921 struct dma_fence *xe_migrate_clear(struct xe_migrate *m, 922 struct xe_bo *bo, 923 struct ttm_resource *dst) 924 { 925 bool clear_vram = mem_type_is_vram(dst->mem_type); 926 struct xe_gt *gt = m->tile->primary_gt; 927 struct xe_device *xe = gt_to_xe(gt); 928 bool clear_system_ccs = (xe_bo_needs_ccs_pages(bo) && !IS_DGFX(xe)) ? true : false; 929 struct dma_fence *fence = NULL; 930 u64 size = bo->size; 931 struct xe_res_cursor src_it; 932 struct ttm_resource *src = dst; 933 int err; 934 int pass = 0; 935 936 if (!clear_vram) 937 xe_res_first_sg(xe_bo_sg(bo), 0, bo->size, &src_it); 938 else 939 xe_res_first(src, 0, bo->size, &src_it); 940 941 while (size) { 942 u64 clear_L0_ofs; 943 u32 clear_L0_pt; 944 u32 flush_flags = 0; 945 u64 clear_L0; 946 struct xe_sched_job *job; 947 struct xe_bb *bb; 948 u32 batch_size, update_idx; 949 950 bool usm = xe->info.has_usm; 951 u32 avail_pts = max_mem_transfer_per_pass(xe) / LEVEL0_PAGE_TABLE_ENCODE_SIZE; 952 953 clear_L0 = xe_migrate_res_sizes(xe, &src_it); 954 955 drm_dbg(&xe->drm, "Pass %u, size: %llu\n", pass++, clear_L0); 956 957 /* Calculate final sizes and batch size.. */ 958 batch_size = 2 + 959 pte_update_size(m, clear_vram, src, &src_it, 960 &clear_L0, &clear_L0_ofs, &clear_L0_pt, 961 clear_system_ccs ? 0 : emit_clear_cmd_len(gt), 0, 962 avail_pts); 963 964 if (xe_device_has_flat_ccs(xe)) 965 batch_size += EMIT_COPY_CCS_DW; 966 967 /* Clear commands */ 968 969 if (WARN_ON_ONCE(!clear_L0)) 970 break; 971 972 bb = xe_bb_new(gt, batch_size, usm); 973 if (IS_ERR(bb)) { 974 err = PTR_ERR(bb); 975 goto err_sync; 976 } 977 978 size -= clear_L0; 979 /* Preemption is enabled again by the ring ops. */ 980 if (!clear_vram) { 981 emit_pte(m, bb, clear_L0_pt, clear_vram, true, &src_it, clear_L0, 982 bo); 983 } else { 984 xe_res_next(&src_it, clear_L0); 985 } 986 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 987 update_idx = bb->len; 988 989 if (!clear_system_ccs) 990 emit_clear(gt, bb, clear_L0_ofs, clear_L0, XE_PAGE_SIZE, clear_vram); 991 992 if (xe_device_has_flat_ccs(xe)) { 993 emit_copy_ccs(gt, bb, clear_L0_ofs, true, 994 m->cleared_mem_ofs, false, clear_L0); 995 flush_flags = MI_FLUSH_DW_CCS; 996 } 997 998 mutex_lock(&m->job_mutex); 999 job = xe_bb_create_migration_job(m->q, bb, 1000 xe_migrate_batch_base(m, usm), 1001 update_idx); 1002 if (IS_ERR(job)) { 1003 err = PTR_ERR(job); 1004 goto err; 1005 } 1006 1007 xe_sched_job_add_migrate_flush(job, flush_flags); 1008 if (!fence) { 1009 /* 1010 * There can't be anything userspace related at this 1011 * point, so we just need to respect any potential move 1012 * fences, which are always tracked as 1013 * DMA_RESV_USAGE_KERNEL. 1014 */ 1015 err = job_add_deps(job, bo->ttm.base.resv, 1016 DMA_RESV_USAGE_KERNEL); 1017 if (err) 1018 goto err_job; 1019 } 1020 1021 xe_sched_job_arm(job); 1022 dma_fence_put(fence); 1023 fence = dma_fence_get(&job->drm.s_fence->finished); 1024 xe_sched_job_push(job); 1025 1026 dma_fence_put(m->fence); 1027 m->fence = dma_fence_get(fence); 1028 1029 mutex_unlock(&m->job_mutex); 1030 1031 xe_bb_free(bb, fence); 1032 continue; 1033 1034 err_job: 1035 xe_sched_job_put(job); 1036 err: 1037 mutex_unlock(&m->job_mutex); 1038 xe_bb_free(bb, NULL); 1039 err_sync: 1040 /* Sync partial copies if any. FIXME: job_mutex? */ 1041 if (fence) { 1042 dma_fence_wait(m->fence, false); 1043 dma_fence_put(fence); 1044 } 1045 1046 return ERR_PTR(err); 1047 } 1048 1049 if (clear_system_ccs) 1050 bo->ccs_cleared = true; 1051 1052 return fence; 1053 } 1054 1055 static void write_pgtable(struct xe_tile *tile, struct xe_bb *bb, u64 ppgtt_ofs, 1056 const struct xe_vm_pgtable_update *update, 1057 struct xe_migrate_pt_update *pt_update) 1058 { 1059 const struct xe_migrate_pt_update_ops *ops = pt_update->ops; 1060 u32 chunk; 1061 u32 ofs = update->ofs, size = update->qwords; 1062 1063 /* 1064 * If we have 512 entries (max), we would populate it ourselves, 1065 * and update the PDE above it to the new pointer. 1066 * The only time this can only happen if we have to update the top 1067 * PDE. This requires a BO that is almost vm->size big. 1068 * 1069 * This shouldn't be possible in practice.. might change when 16K 1070 * pages are used. Hence the assert. 1071 */ 1072 xe_tile_assert(tile, update->qwords <= 0x1ff); 1073 if (!ppgtt_ofs) 1074 ppgtt_ofs = xe_migrate_vram_ofs(tile_to_xe(tile), 1075 xe_bo_addr(update->pt_bo, 0, 1076 XE_PAGE_SIZE)); 1077 1078 do { 1079 u64 addr = ppgtt_ofs + ofs * 8; 1080 1081 chunk = min(update->qwords, 0x1ffU); 1082 1083 /* Ensure populatefn can do memset64 by aligning bb->cs */ 1084 if (!(bb->len & 1)) 1085 bb->cs[bb->len++] = MI_NOOP; 1086 1087 bb->cs[bb->len++] = MI_STORE_DATA_IMM | MI_SDI_NUM_QW(chunk); 1088 bb->cs[bb->len++] = lower_32_bits(addr); 1089 bb->cs[bb->len++] = upper_32_bits(addr); 1090 ops->populate(pt_update, tile, NULL, bb->cs + bb->len, ofs, chunk, 1091 update); 1092 1093 bb->len += chunk * 2; 1094 ofs += chunk; 1095 size -= chunk; 1096 } while (size); 1097 } 1098 1099 struct xe_vm *xe_migrate_get_vm(struct xe_migrate *m) 1100 { 1101 return xe_vm_get(m->q->vm); 1102 } 1103 1104 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST) 1105 struct migrate_test_params { 1106 struct xe_test_priv base; 1107 bool force_gpu; 1108 }; 1109 1110 #define to_migrate_test_params(_priv) \ 1111 container_of(_priv, struct migrate_test_params, base) 1112 #endif 1113 1114 static struct dma_fence * 1115 xe_migrate_update_pgtables_cpu(struct xe_migrate *m, 1116 struct xe_vm *vm, struct xe_bo *bo, 1117 const struct xe_vm_pgtable_update *updates, 1118 u32 num_updates, bool wait_vm, 1119 struct xe_migrate_pt_update *pt_update) 1120 { 1121 XE_TEST_DECLARE(struct migrate_test_params *test = 1122 to_migrate_test_params 1123 (xe_cur_kunit_priv(XE_TEST_LIVE_MIGRATE));) 1124 const struct xe_migrate_pt_update_ops *ops = pt_update->ops; 1125 struct dma_fence *fence; 1126 int err; 1127 u32 i; 1128 1129 if (XE_TEST_ONLY(test && test->force_gpu)) 1130 return ERR_PTR(-ETIME); 1131 1132 if (bo && !dma_resv_test_signaled(bo->ttm.base.resv, 1133 DMA_RESV_USAGE_KERNEL)) 1134 return ERR_PTR(-ETIME); 1135 1136 if (wait_vm && !dma_resv_test_signaled(xe_vm_resv(vm), 1137 DMA_RESV_USAGE_BOOKKEEP)) 1138 return ERR_PTR(-ETIME); 1139 1140 if (ops->pre_commit) { 1141 pt_update->job = NULL; 1142 err = ops->pre_commit(pt_update); 1143 if (err) 1144 return ERR_PTR(err); 1145 } 1146 for (i = 0; i < num_updates; i++) { 1147 const struct xe_vm_pgtable_update *update = &updates[i]; 1148 1149 ops->populate(pt_update, m->tile, &update->pt_bo->vmap, NULL, 1150 update->ofs, update->qwords, update); 1151 } 1152 1153 if (vm) { 1154 trace_xe_vm_cpu_bind(vm); 1155 xe_device_wmb(vm->xe); 1156 } 1157 1158 fence = dma_fence_get_stub(); 1159 1160 return fence; 1161 } 1162 1163 static bool no_in_syncs(struct xe_vm *vm, struct xe_exec_queue *q, 1164 struct xe_sync_entry *syncs, u32 num_syncs) 1165 { 1166 struct dma_fence *fence; 1167 int i; 1168 1169 for (i = 0; i < num_syncs; i++) { 1170 fence = syncs[i].fence; 1171 1172 if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, 1173 &fence->flags)) 1174 return false; 1175 } 1176 if (q) { 1177 fence = xe_exec_queue_last_fence_get(q, vm); 1178 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) 1179 return false; 1180 } 1181 1182 return true; 1183 } 1184 1185 /** 1186 * xe_migrate_update_pgtables() - Pipelined page-table update 1187 * @m: The migrate context. 1188 * @vm: The vm we'll be updating. 1189 * @bo: The bo whose dma-resv we will await before updating, or NULL if userptr. 1190 * @q: The exec queue to be used for the update or NULL if the default 1191 * migration engine is to be used. 1192 * @updates: An array of update descriptors. 1193 * @num_updates: Number of descriptors in @updates. 1194 * @syncs: Array of xe_sync_entry to await before updating. Note that waits 1195 * will block the engine timeline. 1196 * @num_syncs: Number of entries in @syncs. 1197 * @pt_update: Pointer to a struct xe_migrate_pt_update, which contains 1198 * pointers to callback functions and, if subclassed, private arguments to 1199 * those. 1200 * 1201 * Perform a pipelined page-table update. The update descriptors are typically 1202 * built under the same lock critical section as a call to this function. If 1203 * using the default engine for the updates, they will be performed in the 1204 * order they grab the job_mutex. If different engines are used, external 1205 * synchronization is needed for overlapping updates to maintain page-table 1206 * consistency. Note that the meaing of "overlapping" is that the updates 1207 * touch the same page-table, which might be a higher-level page-directory. 1208 * If no pipelining is needed, then updates may be performed by the cpu. 1209 * 1210 * Return: A dma_fence that, when signaled, indicates the update completion. 1211 */ 1212 struct dma_fence * 1213 xe_migrate_update_pgtables(struct xe_migrate *m, 1214 struct xe_vm *vm, 1215 struct xe_bo *bo, 1216 struct xe_exec_queue *q, 1217 const struct xe_vm_pgtable_update *updates, 1218 u32 num_updates, 1219 struct xe_sync_entry *syncs, u32 num_syncs, 1220 struct xe_migrate_pt_update *pt_update) 1221 { 1222 const struct xe_migrate_pt_update_ops *ops = pt_update->ops; 1223 struct xe_tile *tile = m->tile; 1224 struct xe_gt *gt = tile->primary_gt; 1225 struct xe_device *xe = tile_to_xe(tile); 1226 struct xe_sched_job *job; 1227 struct dma_fence *fence; 1228 struct drm_suballoc *sa_bo = NULL; 1229 struct xe_vma *vma = pt_update->vma; 1230 struct xe_bb *bb; 1231 u32 i, batch_size, ppgtt_ofs, update_idx, page_ofs = 0; 1232 u64 addr; 1233 int err = 0; 1234 bool usm = !q && xe->info.has_usm; 1235 bool first_munmap_rebind = vma && 1236 vma->gpuva.flags & XE_VMA_FIRST_REBIND; 1237 struct xe_exec_queue *q_override = !q ? m->q : q; 1238 u16 pat_index = xe->pat.idx[XE_CACHE_WB]; 1239 1240 /* Use the CPU if no in syncs and engine is idle */ 1241 if (no_in_syncs(vm, q, syncs, num_syncs) && xe_exec_queue_is_idle(q_override)) { 1242 fence = xe_migrate_update_pgtables_cpu(m, vm, bo, updates, 1243 num_updates, 1244 first_munmap_rebind, 1245 pt_update); 1246 if (!IS_ERR(fence) || fence == ERR_PTR(-EAGAIN)) 1247 return fence; 1248 } 1249 1250 /* fixed + PTE entries */ 1251 if (IS_DGFX(xe)) 1252 batch_size = 2; 1253 else 1254 batch_size = 6 + num_updates * 2; 1255 1256 for (i = 0; i < num_updates; i++) { 1257 u32 num_cmds = DIV_ROUND_UP(updates[i].qwords, 0x1ff); 1258 1259 /* align noop + MI_STORE_DATA_IMM cmd prefix */ 1260 batch_size += 4 * num_cmds + updates[i].qwords * 2; 1261 } 1262 1263 /* 1264 * XXX: Create temp bo to copy from, if batch_size becomes too big? 1265 * 1266 * Worst case: Sum(2 * (each lower level page size) + (top level page size)) 1267 * Should be reasonably bound.. 1268 */ 1269 xe_tile_assert(tile, batch_size < SZ_128K); 1270 1271 bb = xe_bb_new(gt, batch_size, !q && xe->info.has_usm); 1272 if (IS_ERR(bb)) 1273 return ERR_CAST(bb); 1274 1275 /* For sysmem PTE's, need to map them in our hole.. */ 1276 if (!IS_DGFX(xe)) { 1277 ppgtt_ofs = NUM_KERNEL_PDE - 1; 1278 if (q) { 1279 xe_tile_assert(tile, num_updates <= NUM_VMUSA_WRITES_PER_UNIT); 1280 1281 sa_bo = drm_suballoc_new(&m->vm_update_sa, 1, 1282 GFP_KERNEL, true, 0); 1283 if (IS_ERR(sa_bo)) { 1284 err = PTR_ERR(sa_bo); 1285 goto err; 1286 } 1287 1288 ppgtt_ofs = NUM_KERNEL_PDE + 1289 (drm_suballoc_soffset(sa_bo) / 1290 NUM_VMUSA_UNIT_PER_PAGE); 1291 page_ofs = (drm_suballoc_soffset(sa_bo) % 1292 NUM_VMUSA_UNIT_PER_PAGE) * 1293 VM_SA_UPDATE_UNIT_SIZE; 1294 } 1295 1296 /* Map our PT's to gtt */ 1297 bb->cs[bb->len++] = MI_STORE_DATA_IMM | MI_SDI_NUM_QW(num_updates); 1298 bb->cs[bb->len++] = ppgtt_ofs * XE_PAGE_SIZE + page_ofs; 1299 bb->cs[bb->len++] = 0; /* upper_32_bits */ 1300 1301 for (i = 0; i < num_updates; i++) { 1302 struct xe_bo *pt_bo = updates[i].pt_bo; 1303 1304 xe_tile_assert(tile, pt_bo->size == SZ_4K); 1305 1306 addr = vm->pt_ops->pte_encode_bo(pt_bo, 0, pat_index, 0); 1307 bb->cs[bb->len++] = lower_32_bits(addr); 1308 bb->cs[bb->len++] = upper_32_bits(addr); 1309 } 1310 1311 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 1312 update_idx = bb->len; 1313 1314 addr = xe_migrate_vm_addr(ppgtt_ofs, 0) + 1315 (page_ofs / sizeof(u64)) * XE_PAGE_SIZE; 1316 for (i = 0; i < num_updates; i++) 1317 write_pgtable(tile, bb, addr + i * XE_PAGE_SIZE, 1318 &updates[i], pt_update); 1319 } else { 1320 /* phys pages, no preamble required */ 1321 bb->cs[bb->len++] = MI_BATCH_BUFFER_END; 1322 update_idx = bb->len; 1323 1324 for (i = 0; i < num_updates; i++) 1325 write_pgtable(tile, bb, 0, &updates[i], pt_update); 1326 } 1327 1328 if (!q) 1329 mutex_lock(&m->job_mutex); 1330 1331 job = xe_bb_create_migration_job(q ?: m->q, bb, 1332 xe_migrate_batch_base(m, usm), 1333 update_idx); 1334 if (IS_ERR(job)) { 1335 err = PTR_ERR(job); 1336 goto err_bb; 1337 } 1338 1339 /* Wait on BO move */ 1340 if (bo) { 1341 err = job_add_deps(job, bo->ttm.base.resv, 1342 DMA_RESV_USAGE_KERNEL); 1343 if (err) 1344 goto err_job; 1345 } 1346 1347 /* 1348 * Munmap style VM unbind, need to wait for all jobs to be complete / 1349 * trigger preempts before moving forward 1350 */ 1351 if (first_munmap_rebind) { 1352 err = job_add_deps(job, xe_vm_resv(vm), 1353 DMA_RESV_USAGE_BOOKKEEP); 1354 if (err) 1355 goto err_job; 1356 } 1357 1358 err = xe_sched_job_last_fence_add_dep(job, vm); 1359 for (i = 0; !err && i < num_syncs; i++) 1360 err = xe_sync_entry_add_deps(&syncs[i], job); 1361 1362 if (err) 1363 goto err_job; 1364 1365 if (ops->pre_commit) { 1366 pt_update->job = job; 1367 err = ops->pre_commit(pt_update); 1368 if (err) 1369 goto err_job; 1370 } 1371 xe_sched_job_arm(job); 1372 fence = dma_fence_get(&job->drm.s_fence->finished); 1373 xe_sched_job_push(job); 1374 1375 if (!q) 1376 mutex_unlock(&m->job_mutex); 1377 1378 xe_bb_free(bb, fence); 1379 drm_suballoc_free(sa_bo, fence); 1380 1381 return fence; 1382 1383 err_job: 1384 xe_sched_job_put(job); 1385 err_bb: 1386 if (!q) 1387 mutex_unlock(&m->job_mutex); 1388 xe_bb_free(bb, NULL); 1389 err: 1390 drm_suballoc_free(sa_bo, NULL); 1391 return ERR_PTR(err); 1392 } 1393 1394 /** 1395 * xe_migrate_wait() - Complete all operations using the xe_migrate context 1396 * @m: Migrate context to wait for. 1397 * 1398 * Waits until the GPU no longer uses the migrate context's default engine 1399 * or its page-table objects. FIXME: What about separate page-table update 1400 * engines? 1401 */ 1402 void xe_migrate_wait(struct xe_migrate *m) 1403 { 1404 if (m->fence) 1405 dma_fence_wait(m->fence, false); 1406 } 1407 1408 #if IS_ENABLED(CONFIG_DRM_XE_KUNIT_TEST) 1409 #include "tests/xe_migrate.c" 1410 #endif 1411