1 /* 2 * Copyright 2009 Jerome Glisse. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sub license, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 19 * USE OR OTHER DEALINGS IN THE SOFTWARE. 20 * 21 * The above copyright notice and this permission notice (including the 22 * next paragraph) shall be included in all copies or substantial portions 23 * of the Software. 24 * 25 */ 26 /* 27 * Authors: 28 * Jerome Glisse <glisse@freedesktop.org> 29 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com> 30 * Dave Airlie 31 */ 32 33 #include <linux/dma-mapping.h> 34 #include <linux/iommu.h> 35 #include <linux/pagemap.h> 36 #include <linux/sched/task.h> 37 #include <linux/sched/mm.h> 38 #include <linux/seq_file.h> 39 #include <linux/slab.h> 40 #include <linux/swap.h> 41 #include <linux/dma-buf.h> 42 #include <linux/sizes.h> 43 #include <linux/module.h> 44 45 #include <drm/drm_drv.h> 46 #include <drm/ttm/ttm_bo.h> 47 #include <drm/ttm/ttm_placement.h> 48 #include <drm/ttm/ttm_range_manager.h> 49 #include <drm/ttm/ttm_tt.h> 50 51 #include <drm/amdgpu_drm.h> 52 53 #include "amdgpu.h" 54 #include "amdgpu_object.h" 55 #include "amdgpu_trace.h" 56 #include "amdgpu_amdkfd.h" 57 #include "amdgpu_sdma.h" 58 #include "amdgpu_ras.h" 59 #include "amdgpu_hmm.h" 60 #include "amdgpu_atomfirmware.h" 61 #include "amdgpu_res_cursor.h" 62 #include "bif/bif_4_1_d.h" 63 64 MODULE_IMPORT_NS("DMA_BUF"); 65 66 #define AMDGPU_TTM_VRAM_MAX_DW_READ ((size_t)128) 67 68 static int amdgpu_ttm_backend_bind(struct ttm_device *bdev, 69 struct ttm_tt *ttm, 70 struct ttm_resource *bo_mem); 71 static void amdgpu_ttm_backend_unbind(struct ttm_device *bdev, 72 struct ttm_tt *ttm); 73 74 static int amdgpu_ttm_init_on_chip(struct amdgpu_device *adev, 75 unsigned int type, 76 uint64_t size_in_page) 77 { 78 return ttm_range_man_init(&adev->mman.bdev, type, 79 false, size_in_page); 80 } 81 82 /** 83 * amdgpu_evict_flags - Compute placement flags 84 * 85 * @bo: The buffer object to evict 86 * @placement: Possible destination(s) for evicted BO 87 * 88 * Fill in placement data when ttm_bo_evict() is called 89 */ 90 static void amdgpu_evict_flags(struct ttm_buffer_object *bo, 91 struct ttm_placement *placement) 92 { 93 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev); 94 struct amdgpu_bo *abo; 95 static const struct ttm_place placements = { 96 .fpfn = 0, 97 .lpfn = 0, 98 .mem_type = TTM_PL_SYSTEM, 99 .flags = 0 100 }; 101 102 /* Don't handle scatter gather BOs */ 103 if (bo->type == ttm_bo_type_sg) { 104 placement->num_placement = 0; 105 return; 106 } 107 108 /* Object isn't an AMDGPU object so ignore */ 109 if (!amdgpu_bo_is_amdgpu_bo(bo)) { 110 placement->placement = &placements; 111 placement->num_placement = 1; 112 return; 113 } 114 115 abo = ttm_to_amdgpu_bo(bo); 116 if (abo->flags & AMDGPU_GEM_CREATE_DISCARDABLE) { 117 placement->num_placement = 0; 118 return; 119 } 120 121 switch (bo->resource->mem_type) { 122 case AMDGPU_PL_GDS: 123 case AMDGPU_PL_GWS: 124 case AMDGPU_PL_OA: 125 case AMDGPU_PL_DOORBELL: 126 case AMDGPU_PL_MMIO_REMAP: 127 placement->num_placement = 0; 128 return; 129 130 case TTM_PL_VRAM: 131 if (!adev->mman.buffer_funcs_enabled) { 132 /* Move to system memory */ 133 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU); 134 135 } else if (!amdgpu_gmc_vram_full_visible(&adev->gmc) && 136 !(abo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) && 137 amdgpu_res_cpu_visible(adev, bo->resource)) { 138 139 /* Try evicting to the CPU inaccessible part of VRAM 140 * first, but only set GTT as busy placement, so this 141 * BO will be evicted to GTT rather than causing other 142 * BOs to be evicted from VRAM 143 */ 144 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM | 145 AMDGPU_GEM_DOMAIN_GTT | 146 AMDGPU_GEM_DOMAIN_CPU); 147 abo->placements[0].fpfn = adev->gmc.visible_vram_size >> PAGE_SHIFT; 148 abo->placements[0].lpfn = 0; 149 abo->placements[0].flags |= TTM_PL_FLAG_DESIRED; 150 } else { 151 /* Move to GTT memory */ 152 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT | 153 AMDGPU_GEM_DOMAIN_CPU); 154 } 155 break; 156 case TTM_PL_TT: 157 case AMDGPU_PL_PREEMPT: 158 default: 159 amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU); 160 break; 161 } 162 *placement = abo->placement; 163 } 164 165 static struct dma_fence * 166 amdgpu_ttm_job_submit(struct amdgpu_device *adev, struct amdgpu_job *job, u32 num_dw) 167 { 168 struct amdgpu_ring *ring; 169 170 ring = adev->mman.buffer_funcs_ring; 171 amdgpu_ring_pad_ib(ring, &job->ibs[0]); 172 WARN_ON(job->ibs[0].length_dw > num_dw); 173 174 return amdgpu_job_submit(job); 175 } 176 177 /** 178 * amdgpu_ttm_map_buffer - Map memory into the GART windows 179 * @entity: entity to run the window setup job 180 * @bo: buffer object to map 181 * @mem: memory object to map 182 * @mm_cur: range to map 183 * @window: which GART window to use 184 * @tmz: if we should setup a TMZ enabled mapping 185 * @size: in number of bytes to map, out number of bytes mapped 186 * @addr: resulting address inside the MC address space 187 * 188 * Setup one of the GART windows to access a specific piece of memory or return 189 * the physical address for local memory. 190 */ 191 static int amdgpu_ttm_map_buffer(struct amdgpu_ttm_buffer_entity *entity, 192 struct ttm_buffer_object *bo, 193 struct ttm_resource *mem, 194 struct amdgpu_res_cursor *mm_cur, 195 unsigned int window, 196 bool tmz, uint64_t *size, uint64_t *addr) 197 { 198 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev); 199 unsigned int offset, num_pages, num_dw, num_bytes; 200 uint64_t src_addr, dst_addr; 201 struct amdgpu_job *job; 202 void *cpu_addr; 203 uint64_t flags; 204 int r; 205 206 BUG_ON(adev->mman.buffer_funcs->copy_max_bytes < 207 AMDGPU_GTT_MAX_TRANSFER_SIZE * 8); 208 209 if (WARN_ON(mem->mem_type == AMDGPU_PL_PREEMPT)) 210 return -EINVAL; 211 212 /* Map only what can't be accessed directly */ 213 if (!tmz && mem->start != AMDGPU_BO_INVALID_OFFSET) { 214 *addr = amdgpu_ttm_domain_start(adev, mem->mem_type) + 215 mm_cur->start; 216 return 0; 217 } 218 219 220 /* 221 * If start begins at an offset inside the page, then adjust the size 222 * and addr accordingly 223 */ 224 offset = mm_cur->start & ~PAGE_MASK; 225 226 num_pages = PFN_UP(*size + offset); 227 num_pages = min_t(uint32_t, num_pages, AMDGPU_GTT_MAX_TRANSFER_SIZE); 228 229 *size = min(*size, (uint64_t)num_pages * PAGE_SIZE - offset); 230 231 *addr = adev->gmc.gart_start; 232 *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 233 AMDGPU_GPU_PAGE_SIZE; 234 *addr += offset; 235 236 num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8); 237 num_bytes = num_pages * 8 * AMDGPU_GPU_PAGES_IN_CPU_PAGE; 238 239 r = amdgpu_job_alloc_with_ib(adev, &entity->base, 240 AMDGPU_FENCE_OWNER_UNDEFINED, 241 num_dw * 4 + num_bytes, 242 AMDGPU_IB_POOL_DELAYED, &job, 243 AMDGPU_KERNEL_JOB_ID_TTM_MAP_BUFFER); 244 if (r) 245 return r; 246 247 src_addr = num_dw * 4; 248 src_addr += job->ibs[0].gpu_addr; 249 250 dst_addr = amdgpu_bo_gpu_offset(adev->gart.bo); 251 dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8; 252 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, 253 dst_addr, num_bytes, 0); 254 255 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, mem); 256 if (tmz) 257 flags |= AMDGPU_PTE_TMZ; 258 259 cpu_addr = &job->ibs[0].ptr[num_dw]; 260 261 if (mem->mem_type == TTM_PL_TT) { 262 dma_addr_t *dma_addr; 263 264 dma_addr = &bo->ttm->dma_address[mm_cur->start >> PAGE_SHIFT]; 265 amdgpu_gart_map(adev, 0, num_pages, dma_addr, flags, cpu_addr); 266 } else { 267 u64 pa = mm_cur->start + adev->vm_manager.vram_base_offset; 268 269 amdgpu_gart_map_vram_range(adev, pa, 0, num_pages, flags, cpu_addr); 270 } 271 272 dma_fence_put(amdgpu_ttm_job_submit(adev, job, num_dw)); 273 return 0; 274 } 275 276 /** 277 * amdgpu_ttm_copy_mem_to_mem - Helper function for copy 278 * @adev: amdgpu device 279 * @entity: entity to run the jobs 280 * @src: buffer/address where to read from 281 * @dst: buffer/address where to write to 282 * @size: number of bytes to copy 283 * @tmz: if a secure copy should be used 284 * @resv: resv object to sync to 285 * @f: Returns the last fence if multiple jobs are submitted. 286 * 287 * The function copies @size bytes from {src->mem + src->offset} to 288 * {dst->mem + dst->offset}. src->bo and dst->bo could be same BO for a 289 * move and different for a BO to BO copy. 290 * 291 */ 292 __attribute__((nonnull)) 293 static int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev, 294 struct amdgpu_ttm_buffer_entity *entity, 295 const struct amdgpu_copy_mem *src, 296 const struct amdgpu_copy_mem *dst, 297 uint64_t size, bool tmz, 298 struct dma_resv *resv, 299 struct dma_fence **f) 300 { 301 struct amdgpu_res_cursor src_mm, dst_mm; 302 struct dma_fence *fence = NULL; 303 int r = 0; 304 uint32_t copy_flags = 0; 305 struct amdgpu_bo *abo_src, *abo_dst; 306 307 if (!adev->mman.buffer_funcs_enabled) { 308 dev_err(adev->dev, 309 "Trying to move memory with ring turned off.\n"); 310 return -EINVAL; 311 } 312 313 amdgpu_res_first(src->mem, src->offset, size, &src_mm); 314 amdgpu_res_first(dst->mem, dst->offset, size, &dst_mm); 315 316 mutex_lock(&adev->mman.gtt_window_lock); 317 while (src_mm.remaining) { 318 uint64_t from, to, cur_size, tiling_flags; 319 uint32_t num_type, data_format, max_com, write_compress_disable; 320 struct dma_fence *next; 321 322 /* Never copy more than 256MiB at once to avoid a timeout */ 323 cur_size = min3(src_mm.size, dst_mm.size, 256ULL << 20); 324 325 /* Map src to window 0 and dst to window 1. */ 326 r = amdgpu_ttm_map_buffer(entity, src->bo, src->mem, &src_mm, 327 0, tmz, &cur_size, &from); 328 if (r) 329 goto error; 330 331 r = amdgpu_ttm_map_buffer(entity, dst->bo, dst->mem, &dst_mm, 332 1, tmz, &cur_size, &to); 333 if (r) 334 goto error; 335 336 abo_src = ttm_to_amdgpu_bo(src->bo); 337 abo_dst = ttm_to_amdgpu_bo(dst->bo); 338 if (tmz) 339 copy_flags |= AMDGPU_COPY_FLAGS_TMZ; 340 if ((abo_src->flags & AMDGPU_GEM_CREATE_GFX12_DCC) && 341 (abo_src->tbo.resource->mem_type == TTM_PL_VRAM)) 342 copy_flags |= AMDGPU_COPY_FLAGS_READ_DECOMPRESSED; 343 if ((abo_dst->flags & AMDGPU_GEM_CREATE_GFX12_DCC) && 344 (dst->mem->mem_type == TTM_PL_VRAM)) { 345 copy_flags |= AMDGPU_COPY_FLAGS_WRITE_COMPRESSED; 346 amdgpu_bo_get_tiling_flags(abo_dst, &tiling_flags); 347 max_com = AMDGPU_TILING_GET(tiling_flags, GFX12_DCC_MAX_COMPRESSED_BLOCK); 348 num_type = AMDGPU_TILING_GET(tiling_flags, GFX12_DCC_NUMBER_TYPE); 349 data_format = AMDGPU_TILING_GET(tiling_flags, GFX12_DCC_DATA_FORMAT); 350 write_compress_disable = 351 AMDGPU_TILING_GET(tiling_flags, GFX12_DCC_WRITE_COMPRESS_DISABLE); 352 copy_flags |= (AMDGPU_COPY_FLAGS_SET(MAX_COMPRESSED, max_com) | 353 AMDGPU_COPY_FLAGS_SET(NUMBER_TYPE, num_type) | 354 AMDGPU_COPY_FLAGS_SET(DATA_FORMAT, data_format) | 355 AMDGPU_COPY_FLAGS_SET(WRITE_COMPRESS_DISABLE, 356 write_compress_disable)); 357 } 358 359 r = amdgpu_copy_buffer(adev, entity, from, to, cur_size, resv, 360 &next, true, copy_flags); 361 if (r) 362 goto error; 363 364 dma_fence_put(fence); 365 fence = next; 366 367 amdgpu_res_next(&src_mm, cur_size); 368 amdgpu_res_next(&dst_mm, cur_size); 369 } 370 error: 371 mutex_unlock(&adev->mman.gtt_window_lock); 372 *f = fence; 373 return r; 374 } 375 376 /* 377 * amdgpu_move_blit - Copy an entire buffer to another buffer 378 * 379 * This is a helper called by amdgpu_bo_move() and amdgpu_move_vram_ram() to 380 * help move buffers to and from VRAM. 381 */ 382 static int amdgpu_move_blit(struct ttm_buffer_object *bo, 383 bool evict, 384 struct ttm_resource *new_mem, 385 struct ttm_resource *old_mem) 386 { 387 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev); 388 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo); 389 struct amdgpu_copy_mem src, dst; 390 struct dma_fence *fence = NULL; 391 int r; 392 393 src.bo = bo; 394 dst.bo = bo; 395 src.mem = old_mem; 396 dst.mem = new_mem; 397 src.offset = 0; 398 dst.offset = 0; 399 400 r = amdgpu_ttm_copy_mem_to_mem(adev, 401 &adev->mman.move_entity, 402 &src, &dst, 403 new_mem->size, 404 amdgpu_bo_encrypted(abo), 405 bo->base.resv, &fence); 406 if (r) 407 goto error; 408 409 /* clear the space being freed */ 410 if (old_mem->mem_type == TTM_PL_VRAM && 411 (abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE)) { 412 struct dma_fence *wipe_fence = NULL; 413 414 r = amdgpu_fill_buffer(&adev->mman.move_entity, 415 abo, 0, NULL, &wipe_fence, 416 AMDGPU_KERNEL_JOB_ID_MOVE_BLIT); 417 if (r) { 418 goto error; 419 } else if (wipe_fence) { 420 amdgpu_vram_mgr_set_cleared(bo->resource); 421 dma_fence_put(fence); 422 fence = wipe_fence; 423 } 424 } 425 426 /* Always block for VM page tables before committing the new location */ 427 if (bo->type == ttm_bo_type_kernel) 428 r = ttm_bo_move_accel_cleanup(bo, fence, true, false, new_mem); 429 else 430 r = ttm_bo_move_accel_cleanup(bo, fence, evict, true, new_mem); 431 dma_fence_put(fence); 432 return r; 433 434 error: 435 if (fence) 436 dma_fence_wait(fence, false); 437 dma_fence_put(fence); 438 return r; 439 } 440 441 /** 442 * amdgpu_res_cpu_visible - Check that resource can be accessed by CPU 443 * @adev: amdgpu device 444 * @res: the resource to check 445 * 446 * Returns: true if the full resource is CPU visible, false otherwise. 447 */ 448 bool amdgpu_res_cpu_visible(struct amdgpu_device *adev, 449 struct ttm_resource *res) 450 { 451 struct amdgpu_res_cursor cursor; 452 453 if (!res) 454 return false; 455 456 if (res->mem_type == TTM_PL_SYSTEM || res->mem_type == TTM_PL_TT || 457 res->mem_type == AMDGPU_PL_PREEMPT || res->mem_type == AMDGPU_PL_DOORBELL || 458 res->mem_type == AMDGPU_PL_MMIO_REMAP) 459 return true; 460 461 if (res->mem_type != TTM_PL_VRAM) 462 return false; 463 464 amdgpu_res_first(res, 0, res->size, &cursor); 465 while (cursor.remaining) { 466 if ((cursor.start + cursor.size) > adev->gmc.visible_vram_size) 467 return false; 468 amdgpu_res_next(&cursor, cursor.size); 469 } 470 471 return true; 472 } 473 474 /* 475 * amdgpu_res_copyable - Check that memory can be accessed by ttm_bo_move_memcpy 476 * 477 * Called by amdgpu_bo_move() 478 */ 479 static bool amdgpu_res_copyable(struct amdgpu_device *adev, 480 struct ttm_resource *mem) 481 { 482 if (!amdgpu_res_cpu_visible(adev, mem)) 483 return false; 484 485 /* ttm_resource_ioremap only supports contiguous memory */ 486 if (mem->mem_type == TTM_PL_VRAM && 487 !(mem->placement & TTM_PL_FLAG_CONTIGUOUS)) 488 return false; 489 490 return true; 491 } 492 493 /* 494 * amdgpu_bo_move - Move a buffer object to a new memory location 495 * 496 * Called by ttm_bo_handle_move_mem() 497 */ 498 static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict, 499 struct ttm_operation_ctx *ctx, 500 struct ttm_resource *new_mem, 501 struct ttm_place *hop) 502 { 503 struct amdgpu_device *adev; 504 struct amdgpu_bo *abo; 505 struct ttm_resource *old_mem = bo->resource; 506 int r; 507 508 if (new_mem->mem_type == TTM_PL_TT || 509 new_mem->mem_type == AMDGPU_PL_PREEMPT) { 510 r = amdgpu_ttm_backend_bind(bo->bdev, bo->ttm, new_mem); 511 if (r) 512 return r; 513 } 514 515 abo = ttm_to_amdgpu_bo(bo); 516 adev = amdgpu_ttm_adev(bo->bdev); 517 518 if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM && 519 bo->ttm == NULL)) { 520 amdgpu_bo_move_notify(bo, evict, new_mem); 521 ttm_bo_move_null(bo, new_mem); 522 return 0; 523 } 524 if (old_mem->mem_type == TTM_PL_SYSTEM && 525 (new_mem->mem_type == TTM_PL_TT || 526 new_mem->mem_type == AMDGPU_PL_PREEMPT)) { 527 amdgpu_bo_move_notify(bo, evict, new_mem); 528 ttm_bo_move_null(bo, new_mem); 529 return 0; 530 } 531 if ((old_mem->mem_type == TTM_PL_TT || 532 old_mem->mem_type == AMDGPU_PL_PREEMPT) && 533 new_mem->mem_type == TTM_PL_SYSTEM) { 534 r = ttm_bo_wait_ctx(bo, ctx); 535 if (r) 536 return r; 537 538 amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm); 539 amdgpu_bo_move_notify(bo, evict, new_mem); 540 ttm_resource_free(bo, &bo->resource); 541 ttm_bo_assign_mem(bo, new_mem); 542 return 0; 543 } 544 545 if (old_mem->mem_type == AMDGPU_PL_GDS || 546 old_mem->mem_type == AMDGPU_PL_GWS || 547 old_mem->mem_type == AMDGPU_PL_OA || 548 old_mem->mem_type == AMDGPU_PL_DOORBELL || 549 old_mem->mem_type == AMDGPU_PL_MMIO_REMAP || 550 new_mem->mem_type == AMDGPU_PL_GDS || 551 new_mem->mem_type == AMDGPU_PL_GWS || 552 new_mem->mem_type == AMDGPU_PL_OA || 553 new_mem->mem_type == AMDGPU_PL_DOORBELL || 554 new_mem->mem_type == AMDGPU_PL_MMIO_REMAP) { 555 /* Nothing to save here */ 556 amdgpu_bo_move_notify(bo, evict, new_mem); 557 ttm_bo_move_null(bo, new_mem); 558 return 0; 559 } 560 561 if (bo->type == ttm_bo_type_device && 562 new_mem->mem_type == TTM_PL_VRAM && 563 old_mem->mem_type != TTM_PL_VRAM) { 564 /* amdgpu_bo_fault_reserve_notify will re-set this if the CPU 565 * accesses the BO after it's moved. 566 */ 567 abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; 568 } 569 570 if (adev->mman.buffer_funcs_enabled && 571 ((old_mem->mem_type == TTM_PL_SYSTEM && 572 new_mem->mem_type == TTM_PL_VRAM) || 573 (old_mem->mem_type == TTM_PL_VRAM && 574 new_mem->mem_type == TTM_PL_SYSTEM))) { 575 hop->fpfn = 0; 576 hop->lpfn = 0; 577 hop->mem_type = TTM_PL_TT; 578 hop->flags = TTM_PL_FLAG_TEMPORARY; 579 return -EMULTIHOP; 580 } 581 582 amdgpu_bo_move_notify(bo, evict, new_mem); 583 if (adev->mman.buffer_funcs_enabled) 584 r = amdgpu_move_blit(bo, evict, new_mem, old_mem); 585 else 586 r = -ENODEV; 587 588 if (r) { 589 /* Check that all memory is CPU accessible */ 590 if (!amdgpu_res_copyable(adev, old_mem) || 591 !amdgpu_res_copyable(adev, new_mem)) { 592 pr_err("Move buffer fallback to memcpy unavailable\n"); 593 return r; 594 } 595 596 r = ttm_bo_move_memcpy(bo, ctx, new_mem); 597 if (r) 598 return r; 599 } 600 601 /* update statistics after the move */ 602 if (evict) 603 atomic64_inc(&adev->num_evictions); 604 atomic64_add(bo->base.size, &adev->num_bytes_moved); 605 return 0; 606 } 607 608 /* 609 * amdgpu_ttm_io_mem_reserve - Reserve a block of memory during a fault 610 * 611 * Called by ttm_mem_io_reserve() ultimately via ttm_bo_vm_fault() 612 */ 613 static int amdgpu_ttm_io_mem_reserve(struct ttm_device *bdev, 614 struct ttm_resource *mem) 615 { 616 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev); 617 618 switch (mem->mem_type) { 619 case TTM_PL_SYSTEM: 620 /* system memory */ 621 return 0; 622 case TTM_PL_TT: 623 case AMDGPU_PL_PREEMPT: 624 break; 625 case TTM_PL_VRAM: 626 mem->bus.offset = mem->start << PAGE_SHIFT; 627 628 if (adev->mman.aper_base_kaddr && 629 mem->placement & TTM_PL_FLAG_CONTIGUOUS) 630 mem->bus.addr = (u8 *)adev->mman.aper_base_kaddr + 631 mem->bus.offset; 632 633 mem->bus.offset += adev->gmc.aper_base; 634 mem->bus.is_iomem = true; 635 break; 636 case AMDGPU_PL_DOORBELL: 637 mem->bus.offset = mem->start << PAGE_SHIFT; 638 mem->bus.offset += adev->doorbell.base; 639 mem->bus.is_iomem = true; 640 mem->bus.caching = ttm_uncached; 641 break; 642 case AMDGPU_PL_MMIO_REMAP: 643 mem->bus.offset = mem->start << PAGE_SHIFT; 644 mem->bus.offset += adev->rmmio_remap.bus_addr; 645 mem->bus.is_iomem = true; 646 mem->bus.caching = ttm_uncached; 647 break; 648 default: 649 return -EINVAL; 650 } 651 return 0; 652 } 653 654 static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo, 655 unsigned long page_offset) 656 { 657 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev); 658 struct amdgpu_res_cursor cursor; 659 660 amdgpu_res_first(bo->resource, (u64)page_offset << PAGE_SHIFT, 0, 661 &cursor); 662 663 if (bo->resource->mem_type == AMDGPU_PL_DOORBELL) 664 return ((uint64_t)(adev->doorbell.base + cursor.start)) >> PAGE_SHIFT; 665 else if (bo->resource->mem_type == AMDGPU_PL_MMIO_REMAP) 666 return ((uint64_t)(adev->rmmio_remap.bus_addr + cursor.start)) >> PAGE_SHIFT; 667 668 return (adev->gmc.aper_base + cursor.start) >> PAGE_SHIFT; 669 } 670 671 /** 672 * amdgpu_ttm_domain_start - Returns GPU start address 673 * @adev: amdgpu device object 674 * @type: type of the memory 675 * 676 * Returns: 677 * GPU start address of a memory domain 678 */ 679 680 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type) 681 { 682 switch (type) { 683 case TTM_PL_TT: 684 return adev->gmc.gart_start; 685 case TTM_PL_VRAM: 686 return adev->gmc.vram_start; 687 } 688 689 return 0; 690 } 691 692 /* 693 * TTM backend functions. 694 */ 695 struct amdgpu_ttm_tt { 696 struct ttm_tt ttm; 697 struct drm_gem_object *gobj; 698 u64 offset; 699 uint64_t userptr; 700 struct task_struct *usertask; 701 uint32_t userflags; 702 bool bound; 703 int32_t pool_id; 704 }; 705 706 #define ttm_to_amdgpu_ttm_tt(ptr) container_of(ptr, struct amdgpu_ttm_tt, ttm) 707 708 #ifdef CONFIG_DRM_AMDGPU_USERPTR 709 /* 710 * amdgpu_ttm_tt_get_user_pages - get device accessible pages that back user 711 * memory and start HMM tracking CPU page table update 712 * 713 * Calling function must call amdgpu_ttm_tt_userptr_range_done() once and only 714 * once afterwards to stop HMM tracking. Its the caller responsibility to ensure 715 * that range is a valid memory and it is freed too. 716 */ 717 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, 718 struct amdgpu_hmm_range *range) 719 { 720 struct ttm_tt *ttm = bo->tbo.ttm; 721 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm); 722 unsigned long start = gtt->userptr; 723 struct vm_area_struct *vma; 724 struct mm_struct *mm; 725 bool readonly; 726 int r = 0; 727 728 mm = bo->notifier.mm; 729 if (unlikely(!mm)) { 730 DRM_DEBUG_DRIVER("BO is not registered?\n"); 731 return -EFAULT; 732 } 733 734 if (!mmget_not_zero(mm)) /* Happens during process shutdown */ 735 return -ESRCH; 736 737 mmap_read_lock(mm); 738 vma = vma_lookup(mm, start); 739 if (unlikely(!vma)) { 740 r = -EFAULT; 741 goto out_unlock; 742 } 743 if (unlikely((gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) && 744 vma->vm_file)) { 745 r = -EPERM; 746 goto out_unlock; 747 } 748 749 readonly = amdgpu_ttm_tt_is_readonly(ttm); 750 r = amdgpu_hmm_range_get_pages(&bo->notifier, start, ttm->num_pages, 751 readonly, NULL, range); 752 out_unlock: 753 mmap_read_unlock(mm); 754 if (r) 755 pr_debug("failed %d to get user pages 0x%lx\n", r, start); 756 757 mmput(mm); 758 759 return r; 760 } 761 762 #endif 763 764 /* 765 * amdgpu_ttm_tt_set_user_pages - Copy pages in, putting old pages as necessary. 766 * 767 * Called by amdgpu_cs_list_validate(). This creates the page list 768 * that backs user memory and will ultimately be mapped into the device 769 * address space. 770 */ 771 void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct amdgpu_hmm_range *range) 772 { 773 unsigned long i; 774 775 for (i = 0; i < ttm->num_pages; ++i) 776 ttm->pages[i] = range ? hmm_pfn_to_page(range->hmm_range.hmm_pfns[i]) : NULL; 777 } 778 779 /* 780 * amdgpu_ttm_tt_pin_userptr - prepare the sg table with the user pages 781 * 782 * Called by amdgpu_ttm_backend_bind() 783 **/ 784 static int amdgpu_ttm_tt_pin_userptr(struct ttm_device *bdev, 785 struct ttm_tt *ttm) 786 { 787 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev); 788 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm); 789 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY); 790 enum dma_data_direction direction = write ? 791 DMA_BIDIRECTIONAL : DMA_TO_DEVICE; 792 int r; 793 794 /* Allocate an SG array and squash pages into it */ 795 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0, 796 (u64)ttm->num_pages << PAGE_SHIFT, 797 GFP_KERNEL); 798 if (r) 799 goto release_sg; 800 801 /* Map SG to device */ 802 r = dma_map_sgtable(adev->dev, ttm->sg, direction, 0); 803 if (r) 804 goto release_sg_table; 805 806 /* convert SG to linear array of pages and dma addresses */ 807 drm_prime_sg_to_dma_addr_array(ttm->sg, gtt->ttm.dma_address, 808 ttm->num_pages); 809 810 return 0; 811 812 release_sg_table: 813 sg_free_table(ttm->sg); 814 release_sg: 815 kfree(ttm->sg); 816 ttm->sg = NULL; 817 return r; 818 } 819 820 /* 821 * amdgpu_ttm_tt_unpin_userptr - Unpin and unmap userptr pages 822 */ 823 static void amdgpu_ttm_tt_unpin_userptr(struct ttm_device *bdev, 824 struct ttm_tt *ttm) 825 { 826 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev); 827 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm); 828 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY); 829 enum dma_data_direction direction = write ? 830 DMA_BIDIRECTIONAL : DMA_TO_DEVICE; 831 832 /* double check that we don't free the table twice */ 833 if (!ttm->sg || !ttm->sg->sgl) 834 return; 835 836 /* unmap the pages mapped to the device */ 837 dma_unmap_sgtable(adev->dev, ttm->sg, direction, 0); 838 sg_free_table(ttm->sg); 839 } 840 841 /* 842 * total_pages is constructed as MQD0+CtrlStack0 + MQD1+CtrlStack1 + ... 843 * MQDn+CtrlStackn where n is the number of XCCs per partition. 844 * pages_per_xcc is the size of one MQD+CtrlStack. The first page is MQD 845 * and uses memory type default, UC. The rest of pages_per_xcc are 846 * Ctrl stack and modify their memory type to NC. 847 */ 848 static void amdgpu_ttm_gart_bind_gfx9_mqd(struct amdgpu_device *adev, 849 struct ttm_tt *ttm, uint64_t flags) 850 { 851 struct amdgpu_ttm_tt *gtt = (void *)ttm; 852 uint64_t total_pages = ttm->num_pages; 853 int num_xcc = max(1U, adev->gfx.num_xcc_per_xcp); 854 uint64_t page_idx, pages_per_xcc; 855 int i; 856 uint64_t ctrl_flags = AMDGPU_PTE_MTYPE_VG10(flags, AMDGPU_MTYPE_NC); 857 858 pages_per_xcc = total_pages; 859 do_div(pages_per_xcc, num_xcc); 860 861 for (i = 0, page_idx = 0; i < num_xcc; i++, page_idx += pages_per_xcc) { 862 /* MQD page: use default flags */ 863 amdgpu_gart_bind(adev, 864 gtt->offset + (page_idx << PAGE_SHIFT), 865 1, >t->ttm.dma_address[page_idx], flags); 866 /* 867 * Ctrl pages - modify the memory type to NC (ctrl_flags) from 868 * the second page of the BO onward. 869 */ 870 amdgpu_gart_bind(adev, 871 gtt->offset + ((page_idx + 1) << PAGE_SHIFT), 872 pages_per_xcc - 1, 873 >t->ttm.dma_address[page_idx + 1], 874 ctrl_flags); 875 } 876 } 877 878 static void amdgpu_ttm_gart_bind(struct amdgpu_device *adev, 879 struct ttm_buffer_object *tbo, 880 uint64_t flags) 881 { 882 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(tbo); 883 struct ttm_tt *ttm = tbo->ttm; 884 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm); 885 886 if (amdgpu_bo_encrypted(abo)) 887 flags |= AMDGPU_PTE_TMZ; 888 889 if (abo->flags & AMDGPU_GEM_CREATE_CP_MQD_GFX9) { 890 amdgpu_ttm_gart_bind_gfx9_mqd(adev, ttm, flags); 891 } else { 892 amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages, 893 gtt->ttm.dma_address, flags); 894 } 895 gtt->bound = true; 896 } 897 898 /* 899 * amdgpu_ttm_backend_bind - Bind GTT memory 900 * 901 * Called by ttm_tt_bind() on behalf of ttm_bo_handle_move_mem(). 902 * This handles binding GTT memory to the device address space. 903 */ 904 static int amdgpu_ttm_backend_bind(struct ttm_device *bdev, 905 struct ttm_tt *ttm, 906 struct ttm_resource *bo_mem) 907 { 908 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev); 909 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm); 910 uint64_t flags; 911 int r; 912 913 if (!bo_mem) 914 return -EINVAL; 915 916 if (gtt->bound) 917 return 0; 918 919 if (gtt->userptr) { 920 r = amdgpu_ttm_tt_pin_userptr(bdev, ttm); 921 if (r) { 922 dev_err(adev->dev, "failed to pin userptr\n"); 923 return r; 924 } 925 } else if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL) { 926 if (!ttm->sg) { 927 struct dma_buf_attachment *attach; 928 struct sg_table *sgt; 929 930 attach = gtt->gobj->import_attach; 931 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); 932 if (IS_ERR(sgt)) 933 return PTR_ERR(sgt); 934 935 ttm->sg = sgt; 936 } 937 938 drm_prime_sg_to_dma_addr_array(ttm->sg, gtt->ttm.dma_address, 939 ttm->num_pages); 940 } 941 942 if (!ttm->num_pages) { 943 WARN(1, "nothing to bind %u pages for mreg %p back %p!\n", 944 ttm->num_pages, bo_mem, ttm); 945 } 946 947 if (bo_mem->mem_type != TTM_PL_TT || 948 !amdgpu_gtt_mgr_has_gart_addr(bo_mem)) { 949 gtt->offset = AMDGPU_BO_INVALID_OFFSET; 950 return 0; 951 } 952 953 /* compute PTE flags relevant to this BO memory */ 954 flags = amdgpu_ttm_tt_pte_flags(adev, ttm, bo_mem); 955 956 /* bind pages into GART page tables */ 957 gtt->offset = (u64)bo_mem->start << PAGE_SHIFT; 958 amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages, 959 gtt->ttm.dma_address, flags); 960 gtt->bound = true; 961 return 0; 962 } 963 964 /* 965 * amdgpu_ttm_alloc_gart - Make sure buffer object is accessible either 966 * through AGP or GART aperture. 967 * 968 * If bo is accessible through AGP aperture, then use AGP aperture 969 * to access bo; otherwise allocate logical space in GART aperture 970 * and map bo to GART aperture. 971 */ 972 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo) 973 { 974 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev); 975 struct ttm_operation_ctx ctx = { false, false }; 976 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(bo->ttm); 977 struct ttm_placement placement; 978 struct ttm_place placements; 979 struct ttm_resource *tmp; 980 uint64_t addr, flags; 981 int r; 982 983 if (bo->resource->start != AMDGPU_BO_INVALID_OFFSET) 984 return 0; 985 986 addr = amdgpu_gmc_agp_addr(bo); 987 if (addr != AMDGPU_BO_INVALID_OFFSET) 988 return 0; 989 990 /* allocate GART space */ 991 placement.num_placement = 1; 992 placement.placement = &placements; 993 placements.fpfn = 0; 994 placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT; 995 placements.mem_type = TTM_PL_TT; 996 placements.flags = bo->resource->placement; 997 998 r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx); 999 if (unlikely(r)) 1000 return r; 1001 1002 /* compute PTE flags for this buffer object */ 1003 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, tmp); 1004 1005 /* Bind pages */ 1006 gtt->offset = (u64)tmp->start << PAGE_SHIFT; 1007 amdgpu_ttm_gart_bind(adev, bo, flags); 1008 amdgpu_gart_invalidate_tlb(adev); 1009 ttm_resource_free(bo, &bo->resource); 1010 ttm_bo_assign_mem(bo, tmp); 1011 1012 return 0; 1013 } 1014 1015 /* 1016 * amdgpu_ttm_recover_gart - Rebind GTT pages 1017 * 1018 * Called by amdgpu_gtt_mgr_recover() from amdgpu_device_reset() to 1019 * rebind GTT pages during a GPU reset. 1020 */ 1021 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo) 1022 { 1023 struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev); 1024 uint64_t flags; 1025 1026 if (!tbo->ttm) 1027 return; 1028 1029 flags = amdgpu_ttm_tt_pte_flags(adev, tbo->ttm, tbo->resource); 1030 amdgpu_ttm_gart_bind(adev, tbo, flags); 1031 } 1032 1033 /* 1034 * amdgpu_ttm_backend_unbind - Unbind GTT mapped pages 1035 * 1036 * Called by ttm_tt_unbind() on behalf of ttm_bo_move_ttm() and 1037 * ttm_tt_destroy(). 1038 */ 1039 static void amdgpu_ttm_backend_unbind(struct ttm_device *bdev, 1040 struct ttm_tt *ttm) 1041 { 1042 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev); 1043 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm); 1044 1045 /* if the pages have userptr pinning then clear that first */ 1046 if (gtt->userptr) { 1047 amdgpu_ttm_tt_unpin_userptr(bdev, ttm); 1048 } else if (ttm->sg && drm_gem_is_imported(gtt->gobj)) { 1049 struct dma_buf_attachment *attach; 1050 1051 attach = gtt->gobj->import_attach; 1052 dma_buf_unmap_attachment(attach, ttm->sg, DMA_BIDIRECTIONAL); 1053 ttm->sg = NULL; 1054 } 1055 1056 if (!gtt->bound) 1057 return; 1058 1059 if (gtt->offset == AMDGPU_BO_INVALID_OFFSET) 1060 return; 1061 1062 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */ 1063 amdgpu_gart_unbind(adev, gtt->offset, ttm->num_pages); 1064 gtt->bound = false; 1065 } 1066 1067 static void amdgpu_ttm_backend_destroy(struct ttm_device *bdev, 1068 struct ttm_tt *ttm) 1069 { 1070 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm); 1071 1072 if (gtt->usertask) 1073 put_task_struct(gtt->usertask); 1074 1075 ttm_tt_fini(>t->ttm); 1076 kfree(gtt); 1077 } 1078 1079 /** 1080 * amdgpu_ttm_mmio_remap_alloc_sgt - build an sg_table for MMIO_REMAP I/O aperture 1081 * @adev: amdgpu device providing the remap BAR base (adev->rmmio_remap.bus_addr) 1082 * @res: TTM resource of the BO to export; expected to live in AMDGPU_PL_MMIO_REMAP 1083 * @dev: importing device to map for (typically @attach->dev in dma-buf paths) 1084 * @dir: DMA data direction for the importer (passed to dma_map_resource()) 1085 * @sgt: output; on success, set to a newly allocated sg_table describing the I/O span 1086 * 1087 * The HDP flush page (AMDGPU_PL_MMIO_REMAP) is a fixed hardware I/O window in a PCI 1088 * BAR—there are no struct pages to back it. Importers still need a DMA address list, 1089 * so we synthesize a minimal sg_table and populate it from dma_map_resource(), not 1090 * from pages. Using the common amdgpu_res_cursor walker keeps the offset/size math 1091 * consistent with other TTM/manager users. 1092 * 1093 * - @res is assumed to be a small, contiguous I/O region (typically a single 4 KiB 1094 * page) in AMDGPU_PL_MMIO_REMAP. Callers should validate placement before calling. 1095 * - The sg entry is created with sg_set_page(sg, NULL, …) to reflect I/O space. 1096 * - The mapping uses DMA_ATTR_SKIP_CPU_SYNC because this is MMIO, not cacheable RAM. 1097 * - Peer reachability / p2pdma policy checks must be done by the caller. 1098 * 1099 * Return: 1100 * * 0 on success, with *@sgt set to a valid table that must be freed via 1101 * amdgpu_ttm_mmio_remap_free_sgt(). 1102 * * -ENOMEM if allocation of the sg_table fails. 1103 * * -EIO if dma_map_resource() fails. 1104 * 1105 */ 1106 int amdgpu_ttm_mmio_remap_alloc_sgt(struct amdgpu_device *adev, 1107 struct ttm_resource *res, 1108 struct device *dev, 1109 enum dma_data_direction dir, 1110 struct sg_table **sgt) 1111 { 1112 struct amdgpu_res_cursor cur; 1113 dma_addr_t dma; 1114 resource_size_t phys; 1115 struct scatterlist *sg; 1116 int r; 1117 1118 /* Walk the resource once; MMIO_REMAP is expected to be contiguous+small. */ 1119 amdgpu_res_first(res, 0, res->size, &cur); 1120 1121 /* Translate byte offset in the remap window into a host physical BAR address. */ 1122 phys = adev->rmmio_remap.bus_addr + cur.start; 1123 1124 /* Build a single-entry sg_table mapped as I/O (no struct page backing). */ 1125 *sgt = kzalloc(sizeof(**sgt), GFP_KERNEL); 1126 if (!*sgt) 1127 return -ENOMEM; 1128 r = sg_alloc_table(*sgt, 1, GFP_KERNEL); 1129 if (r) { 1130 kfree(*sgt); 1131 return r; 1132 } 1133 sg = (*sgt)->sgl; 1134 sg_set_page(sg, NULL, cur.size, 0); /* WHY: I/O space → no pages */ 1135 1136 dma = dma_map_resource(dev, phys, cur.size, dir, DMA_ATTR_SKIP_CPU_SYNC); 1137 if (dma_mapping_error(dev, dma)) { 1138 sg_free_table(*sgt); 1139 kfree(*sgt); 1140 return -EIO; 1141 } 1142 sg_dma_address(sg) = dma; 1143 sg_dma_len(sg) = cur.size; 1144 return 0; 1145 } 1146 1147 void amdgpu_ttm_mmio_remap_free_sgt(struct device *dev, 1148 enum dma_data_direction dir, 1149 struct sg_table *sgt) 1150 { 1151 struct scatterlist *sg = sgt->sgl; 1152 1153 dma_unmap_resource(dev, sg_dma_address(sg), sg_dma_len(sg), 1154 dir, DMA_ATTR_SKIP_CPU_SYNC); 1155 sg_free_table(sgt); 1156 kfree(sgt); 1157 } 1158 1159 /** 1160 * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO 1161 * 1162 * @bo: The buffer object to create a GTT ttm_tt object around 1163 * @page_flags: Page flags to be added to the ttm_tt object 1164 * 1165 * Called by ttm_tt_create(). 1166 */ 1167 static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo, 1168 uint32_t page_flags) 1169 { 1170 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev); 1171 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo); 1172 struct amdgpu_ttm_tt *gtt; 1173 enum ttm_caching caching; 1174 1175 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL); 1176 if (!gtt) 1177 return NULL; 1178 1179 gtt->gobj = &bo->base; 1180 if (adev->gmc.mem_partitions && abo->xcp_id >= 0) 1181 gtt->pool_id = KFD_XCP_MEM_ID(adev, abo->xcp_id); 1182 else 1183 gtt->pool_id = abo->xcp_id; 1184 1185 if (abo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) 1186 caching = ttm_write_combined; 1187 else 1188 caching = ttm_cached; 1189 1190 /* allocate space for the uninitialized page entries */ 1191 if (ttm_sg_tt_init(>t->ttm, bo, page_flags, caching)) { 1192 kfree(gtt); 1193 return NULL; 1194 } 1195 return >t->ttm; 1196 } 1197 1198 /* 1199 * amdgpu_ttm_tt_populate - Map GTT pages visible to the device 1200 * 1201 * Map the pages of a ttm_tt object to an address space visible 1202 * to the underlying device. 1203 */ 1204 static int amdgpu_ttm_tt_populate(struct ttm_device *bdev, 1205 struct ttm_tt *ttm, 1206 struct ttm_operation_ctx *ctx) 1207 { 1208 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev); 1209 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm); 1210 struct ttm_pool *pool; 1211 pgoff_t i; 1212 int ret; 1213 1214 /* user pages are bound by amdgpu_ttm_tt_pin_userptr() */ 1215 if (gtt->userptr) { 1216 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL); 1217 if (!ttm->sg) 1218 return -ENOMEM; 1219 return 0; 1220 } 1221 1222 if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL) 1223 return 0; 1224 1225 if (adev->mman.ttm_pools && gtt->pool_id >= 0) 1226 pool = &adev->mman.ttm_pools[gtt->pool_id]; 1227 else 1228 pool = &adev->mman.bdev.pool; 1229 ret = ttm_pool_alloc(pool, ttm, ctx); 1230 if (ret) 1231 return ret; 1232 1233 for (i = 0; i < ttm->num_pages; ++i) 1234 ttm->pages[i]->mapping = bdev->dev_mapping; 1235 1236 return 0; 1237 } 1238 1239 /* 1240 * amdgpu_ttm_tt_unpopulate - unmap GTT pages and unpopulate page arrays 1241 * 1242 * Unmaps pages of a ttm_tt object from the device address space and 1243 * unpopulates the page array backing it. 1244 */ 1245 static void amdgpu_ttm_tt_unpopulate(struct ttm_device *bdev, 1246 struct ttm_tt *ttm) 1247 { 1248 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm); 1249 struct amdgpu_device *adev; 1250 struct ttm_pool *pool; 1251 pgoff_t i; 1252 1253 amdgpu_ttm_backend_unbind(bdev, ttm); 1254 1255 if (gtt->userptr) { 1256 amdgpu_ttm_tt_set_user_pages(ttm, NULL); 1257 kfree(ttm->sg); 1258 ttm->sg = NULL; 1259 return; 1260 } 1261 1262 if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL) 1263 return; 1264 1265 for (i = 0; i < ttm->num_pages; ++i) 1266 ttm->pages[i]->mapping = NULL; 1267 1268 adev = amdgpu_ttm_adev(bdev); 1269 1270 if (adev->mman.ttm_pools && gtt->pool_id >= 0) 1271 pool = &adev->mman.ttm_pools[gtt->pool_id]; 1272 else 1273 pool = &adev->mman.bdev.pool; 1274 1275 return ttm_pool_free(pool, ttm); 1276 } 1277 1278 /** 1279 * amdgpu_ttm_tt_get_userptr - Return the userptr GTT ttm_tt for the current 1280 * task 1281 * 1282 * @tbo: The ttm_buffer_object that contains the userptr 1283 * @user_addr: The returned value 1284 */ 1285 int amdgpu_ttm_tt_get_userptr(const struct ttm_buffer_object *tbo, 1286 uint64_t *user_addr) 1287 { 1288 struct amdgpu_ttm_tt *gtt; 1289 1290 if (!tbo->ttm) 1291 return -EINVAL; 1292 1293 gtt = (void *)tbo->ttm; 1294 *user_addr = gtt->userptr; 1295 return 0; 1296 } 1297 1298 /** 1299 * amdgpu_ttm_tt_set_userptr - Initialize userptr GTT ttm_tt for the current 1300 * task 1301 * 1302 * @bo: The ttm_buffer_object to bind this userptr to 1303 * @addr: The address in the current tasks VM space to use 1304 * @flags: Requirements of userptr object. 1305 * 1306 * Called by amdgpu_gem_userptr_ioctl() and kfd_ioctl_alloc_memory_of_gpu() to 1307 * bind userptr pages to current task and by kfd_ioctl_acquire_vm() to 1308 * initialize GPU VM for a KFD process. 1309 */ 1310 int amdgpu_ttm_tt_set_userptr(struct ttm_buffer_object *bo, 1311 uint64_t addr, uint32_t flags) 1312 { 1313 struct amdgpu_ttm_tt *gtt; 1314 1315 if (!bo->ttm) { 1316 /* TODO: We want a separate TTM object type for userptrs */ 1317 bo->ttm = amdgpu_ttm_tt_create(bo, 0); 1318 if (bo->ttm == NULL) 1319 return -ENOMEM; 1320 } 1321 1322 /* Set TTM_TT_FLAG_EXTERNAL before populate but after create. */ 1323 bo->ttm->page_flags |= TTM_TT_FLAG_EXTERNAL; 1324 1325 gtt = ttm_to_amdgpu_ttm_tt(bo->ttm); 1326 gtt->userptr = addr; 1327 gtt->userflags = flags; 1328 1329 if (gtt->usertask) 1330 put_task_struct(gtt->usertask); 1331 gtt->usertask = current->group_leader; 1332 get_task_struct(gtt->usertask); 1333 1334 return 0; 1335 } 1336 1337 /* 1338 * amdgpu_ttm_tt_get_usermm - Return memory manager for ttm_tt object 1339 */ 1340 struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm) 1341 { 1342 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm); 1343 1344 if (gtt == NULL) 1345 return NULL; 1346 1347 if (gtt->usertask == NULL) 1348 return NULL; 1349 1350 return gtt->usertask->mm; 1351 } 1352 1353 /* 1354 * amdgpu_ttm_tt_affect_userptr - Determine if a ttm_tt object lays inside an 1355 * address range for the current task. 1356 * 1357 */ 1358 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start, 1359 unsigned long end, unsigned long *userptr) 1360 { 1361 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm); 1362 unsigned long size; 1363 1364 if (gtt == NULL || !gtt->userptr) 1365 return false; 1366 1367 /* Return false if no part of the ttm_tt object lies within 1368 * the range 1369 */ 1370 size = (unsigned long)gtt->ttm.num_pages * PAGE_SIZE; 1371 if (gtt->userptr > end || gtt->userptr + size <= start) 1372 return false; 1373 1374 if (userptr) 1375 *userptr = gtt->userptr; 1376 return true; 1377 } 1378 1379 /* 1380 * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr? 1381 */ 1382 bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm) 1383 { 1384 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm); 1385 1386 if (gtt == NULL || !gtt->userptr) 1387 return false; 1388 1389 return true; 1390 } 1391 1392 /* 1393 * amdgpu_ttm_tt_is_readonly - Is the ttm_tt object read only? 1394 */ 1395 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm) 1396 { 1397 struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm); 1398 1399 if (gtt == NULL) 1400 return false; 1401 1402 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY); 1403 } 1404 1405 /** 1406 * amdgpu_ttm_tt_pde_flags - Compute PDE flags for ttm_tt object 1407 * 1408 * @ttm: The ttm_tt object to compute the flags for 1409 * @mem: The memory registry backing this ttm_tt object 1410 * 1411 * Figure out the flags to use for a VM PDE (Page Directory Entry). 1412 */ 1413 uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_resource *mem) 1414 { 1415 uint64_t flags = 0; 1416 1417 if (mem && mem->mem_type != TTM_PL_SYSTEM) 1418 flags |= AMDGPU_PTE_VALID; 1419 1420 if (mem && (mem->mem_type == TTM_PL_TT || 1421 mem->mem_type == AMDGPU_PL_DOORBELL || 1422 mem->mem_type == AMDGPU_PL_PREEMPT || 1423 mem->mem_type == AMDGPU_PL_MMIO_REMAP)) { 1424 flags |= AMDGPU_PTE_SYSTEM; 1425 1426 if (ttm && ttm->caching == ttm_cached) 1427 flags |= AMDGPU_PTE_SNOOPED; 1428 } 1429 1430 if (mem && mem->mem_type == TTM_PL_VRAM && 1431 mem->bus.caching == ttm_cached) 1432 flags |= AMDGPU_PTE_SNOOPED; 1433 1434 return flags; 1435 } 1436 1437 /** 1438 * amdgpu_ttm_tt_pte_flags - Compute PTE flags for ttm_tt object 1439 * 1440 * @adev: amdgpu_device pointer 1441 * @ttm: The ttm_tt object to compute the flags for 1442 * @mem: The memory registry backing this ttm_tt object 1443 * 1444 * Figure out the flags to use for a VM PTE (Page Table Entry). 1445 */ 1446 uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm, 1447 struct ttm_resource *mem) 1448 { 1449 uint64_t flags = amdgpu_ttm_tt_pde_flags(ttm, mem); 1450 1451 flags |= adev->gart.gart_pte_flags; 1452 flags |= AMDGPU_PTE_READABLE; 1453 1454 if (!amdgpu_ttm_tt_is_readonly(ttm)) 1455 flags |= AMDGPU_PTE_WRITEABLE; 1456 1457 return flags; 1458 } 1459 1460 /* 1461 * amdgpu_ttm_bo_eviction_valuable - Check to see if we can evict a buffer 1462 * object. 1463 * 1464 * Return true if eviction is sensible. Called by ttm_mem_evict_first() on 1465 * behalf of ttm_bo_mem_force_space() which tries to evict buffer objects until 1466 * it can find space for a new object and by ttm_bo_force_list_clean() which is 1467 * used to clean out a memory space. 1468 */ 1469 static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, 1470 const struct ttm_place *place) 1471 { 1472 struct dma_resv_iter resv_cursor; 1473 struct dma_fence *f; 1474 1475 if (!amdgpu_bo_is_amdgpu_bo(bo)) 1476 return ttm_bo_eviction_valuable(bo, place); 1477 1478 /* Swapout? */ 1479 if (bo->resource->mem_type == TTM_PL_SYSTEM) 1480 return true; 1481 1482 if (bo->type == ttm_bo_type_kernel && 1483 !amdgpu_vm_evictable(ttm_to_amdgpu_bo(bo))) 1484 return false; 1485 1486 /* If bo is a KFD BO, check if the bo belongs to the current process. 1487 * If true, then return false as any KFD process needs all its BOs to 1488 * be resident to run successfully 1489 */ 1490 dma_resv_for_each_fence(&resv_cursor, bo->base.resv, 1491 DMA_RESV_USAGE_BOOKKEEP, f) { 1492 if (amdkfd_fence_check_mm(f, current->mm) && 1493 !(place->flags & TTM_PL_FLAG_CONTIGUOUS)) 1494 return false; 1495 } 1496 1497 /* Preemptible BOs don't own system resources managed by the 1498 * driver (pages, VRAM, GART space). They point to resources 1499 * owned by someone else (e.g. pageable memory in user mode 1500 * or a DMABuf). They are used in a preemptible context so we 1501 * can guarantee no deadlocks and good QoS in case of MMU 1502 * notifiers or DMABuf move notifiers from the resource owner. 1503 */ 1504 if (bo->resource->mem_type == AMDGPU_PL_PREEMPT) 1505 return false; 1506 1507 if (bo->resource->mem_type == TTM_PL_TT && 1508 amdgpu_bo_encrypted(ttm_to_amdgpu_bo(bo))) 1509 return false; 1510 1511 return ttm_bo_eviction_valuable(bo, place); 1512 } 1513 1514 static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos, 1515 void *buf, size_t size, bool write) 1516 { 1517 while (size) { 1518 uint64_t aligned_pos = ALIGN_DOWN(pos, 4); 1519 uint64_t bytes = 4 - (pos & 0x3); 1520 uint32_t shift = (pos & 0x3) * 8; 1521 uint32_t mask = 0xffffffff << shift; 1522 uint32_t value = 0; 1523 1524 if (size < bytes) { 1525 mask &= 0xffffffff >> (bytes - size) * 8; 1526 bytes = size; 1527 } 1528 1529 if (mask != 0xffffffff) { 1530 amdgpu_device_mm_access(adev, aligned_pos, &value, 4, false); 1531 if (write) { 1532 value &= ~mask; 1533 value |= (*(uint32_t *)buf << shift) & mask; 1534 amdgpu_device_mm_access(adev, aligned_pos, &value, 4, true); 1535 } else { 1536 value = (value & mask) >> shift; 1537 memcpy(buf, &value, bytes); 1538 } 1539 } else { 1540 amdgpu_device_mm_access(adev, aligned_pos, buf, 4, write); 1541 } 1542 1543 pos += bytes; 1544 buf += bytes; 1545 size -= bytes; 1546 } 1547 } 1548 1549 static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo, 1550 unsigned long offset, void *buf, 1551 int len, int write) 1552 { 1553 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo); 1554 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev); 1555 struct amdgpu_res_cursor src_mm; 1556 struct amdgpu_job *job; 1557 struct dma_fence *fence; 1558 uint64_t src_addr, dst_addr; 1559 unsigned int num_dw; 1560 int r, idx; 1561 1562 if (len != PAGE_SIZE) 1563 return -EINVAL; 1564 1565 if (!adev->mman.sdma_access_ptr) 1566 return -EACCES; 1567 1568 if (!drm_dev_enter(adev_to_drm(adev), &idx)) 1569 return -ENODEV; 1570 1571 if (write) 1572 memcpy(adev->mman.sdma_access_ptr, buf, len); 1573 1574 num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8); 1575 r = amdgpu_job_alloc_with_ib(adev, &adev->mman.default_entity.base, 1576 AMDGPU_FENCE_OWNER_UNDEFINED, 1577 num_dw * 4, AMDGPU_IB_POOL_DELAYED, 1578 &job, 1579 AMDGPU_KERNEL_JOB_ID_TTM_ACCESS_MEMORY_SDMA); 1580 if (r) 1581 goto out; 1582 1583 mutex_lock(&adev->mman.gtt_window_lock); 1584 amdgpu_res_first(abo->tbo.resource, offset, len, &src_mm); 1585 src_addr = amdgpu_ttm_domain_start(adev, bo->resource->mem_type) + 1586 src_mm.start; 1587 dst_addr = amdgpu_bo_gpu_offset(adev->mman.sdma_access_bo); 1588 if (write) 1589 swap(src_addr, dst_addr); 1590 1591 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr, 1592 PAGE_SIZE, 0); 1593 1594 fence = amdgpu_ttm_job_submit(adev, job, num_dw); 1595 mutex_unlock(&adev->mman.gtt_window_lock); 1596 1597 if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout)) 1598 r = -ETIMEDOUT; 1599 dma_fence_put(fence); 1600 1601 if (!(r || write)) 1602 memcpy(buf, adev->mman.sdma_access_ptr, len); 1603 out: 1604 drm_dev_exit(idx); 1605 return r; 1606 } 1607 1608 /** 1609 * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object. 1610 * 1611 * @bo: The buffer object to read/write 1612 * @offset: Offset into buffer object 1613 * @buf: Secondary buffer to write/read from 1614 * @len: Length in bytes of access 1615 * @write: true if writing 1616 * 1617 * This is used to access VRAM that backs a buffer object via MMIO 1618 * access for debugging purposes. 1619 */ 1620 static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo, 1621 unsigned long offset, void *buf, int len, 1622 int write) 1623 { 1624 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo); 1625 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev); 1626 struct amdgpu_res_cursor cursor; 1627 int ret = 0; 1628 1629 if (bo->resource->mem_type != TTM_PL_VRAM) 1630 return -EIO; 1631 1632 if (amdgpu_device_has_timeouts_enabled(adev) && 1633 !amdgpu_ttm_access_memory_sdma(bo, offset, buf, len, write)) 1634 return len; 1635 1636 amdgpu_res_first(bo->resource, offset, len, &cursor); 1637 while (cursor.remaining) { 1638 size_t count, size = cursor.size; 1639 loff_t pos = cursor.start; 1640 1641 count = amdgpu_device_aper_access(adev, pos, buf, size, write); 1642 size -= count; 1643 if (size) { 1644 /* using MM to access rest vram and handle un-aligned address */ 1645 pos += count; 1646 buf += count; 1647 amdgpu_ttm_vram_mm_access(adev, pos, buf, size, write); 1648 } 1649 1650 ret += cursor.size; 1651 buf += cursor.size; 1652 amdgpu_res_next(&cursor, cursor.size); 1653 } 1654 1655 return ret; 1656 } 1657 1658 static void 1659 amdgpu_bo_delete_mem_notify(struct ttm_buffer_object *bo) 1660 { 1661 amdgpu_bo_move_notify(bo, false, NULL); 1662 } 1663 1664 static struct ttm_device_funcs amdgpu_bo_driver = { 1665 .ttm_tt_create = &amdgpu_ttm_tt_create, 1666 .ttm_tt_populate = &amdgpu_ttm_tt_populate, 1667 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate, 1668 .ttm_tt_destroy = &amdgpu_ttm_backend_destroy, 1669 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable, 1670 .evict_flags = &amdgpu_evict_flags, 1671 .move = &amdgpu_bo_move, 1672 .delete_mem_notify = &amdgpu_bo_delete_mem_notify, 1673 .release_notify = &amdgpu_bo_release_notify, 1674 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve, 1675 .io_mem_pfn = amdgpu_ttm_io_mem_pfn, 1676 .access_memory = &amdgpu_ttm_access_memory, 1677 }; 1678 1679 /* 1680 * Firmware Reservation functions 1681 */ 1682 /** 1683 * amdgpu_ttm_fw_reserve_vram_fini - free fw reserved vram 1684 * 1685 * @adev: amdgpu_device pointer 1686 * 1687 * free fw reserved vram if it has been reserved. 1688 */ 1689 static void amdgpu_ttm_fw_reserve_vram_fini(struct amdgpu_device *adev) 1690 { 1691 amdgpu_bo_free_kernel(&adev->mman.fw_vram_usage_reserved_bo, 1692 NULL, &adev->mman.fw_vram_usage_va); 1693 } 1694 1695 /* 1696 * Driver Reservation functions 1697 */ 1698 /** 1699 * amdgpu_ttm_drv_reserve_vram_fini - free drv reserved vram 1700 * 1701 * @adev: amdgpu_device pointer 1702 * 1703 * free drv reserved vram if it has been reserved. 1704 */ 1705 static void amdgpu_ttm_drv_reserve_vram_fini(struct amdgpu_device *adev) 1706 { 1707 amdgpu_bo_free_kernel(&adev->mman.drv_vram_usage_reserved_bo, 1708 NULL, 1709 &adev->mman.drv_vram_usage_va); 1710 } 1711 1712 /** 1713 * amdgpu_ttm_fw_reserve_vram_init - create bo vram reservation from fw 1714 * 1715 * @adev: amdgpu_device pointer 1716 * 1717 * create bo vram reservation from fw. 1718 */ 1719 static int amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev) 1720 { 1721 uint64_t vram_size = adev->gmc.visible_vram_size; 1722 1723 adev->mman.fw_vram_usage_va = NULL; 1724 adev->mman.fw_vram_usage_reserved_bo = NULL; 1725 1726 if (adev->mman.fw_vram_usage_size == 0 || 1727 adev->mman.fw_vram_usage_size > vram_size) 1728 return 0; 1729 1730 return amdgpu_bo_create_kernel_at(adev, 1731 adev->mman.fw_vram_usage_start_offset, 1732 adev->mman.fw_vram_usage_size, 1733 &adev->mman.fw_vram_usage_reserved_bo, 1734 &adev->mman.fw_vram_usage_va); 1735 } 1736 1737 /** 1738 * amdgpu_ttm_drv_reserve_vram_init - create bo vram reservation from driver 1739 * 1740 * @adev: amdgpu_device pointer 1741 * 1742 * create bo vram reservation from drv. 1743 */ 1744 static int amdgpu_ttm_drv_reserve_vram_init(struct amdgpu_device *adev) 1745 { 1746 u64 vram_size = adev->gmc.visible_vram_size; 1747 1748 adev->mman.drv_vram_usage_va = NULL; 1749 adev->mman.drv_vram_usage_reserved_bo = NULL; 1750 1751 if (adev->mman.drv_vram_usage_size == 0 || 1752 adev->mman.drv_vram_usage_size > vram_size) 1753 return 0; 1754 1755 return amdgpu_bo_create_kernel_at(adev, 1756 adev->mman.drv_vram_usage_start_offset, 1757 adev->mman.drv_vram_usage_size, 1758 &adev->mman.drv_vram_usage_reserved_bo, 1759 &adev->mman.drv_vram_usage_va); 1760 } 1761 1762 /* 1763 * Memoy training reservation functions 1764 */ 1765 1766 /** 1767 * amdgpu_ttm_training_reserve_vram_fini - free memory training reserved vram 1768 * 1769 * @adev: amdgpu_device pointer 1770 * 1771 * free memory training reserved vram if it has been reserved. 1772 */ 1773 static int amdgpu_ttm_training_reserve_vram_fini(struct amdgpu_device *adev) 1774 { 1775 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx; 1776 1777 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT; 1778 amdgpu_bo_free_kernel(&ctx->c2p_bo, NULL, NULL); 1779 ctx->c2p_bo = NULL; 1780 1781 return 0; 1782 } 1783 1784 static void amdgpu_ttm_training_data_block_init(struct amdgpu_device *adev, 1785 uint32_t reserve_size) 1786 { 1787 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx; 1788 1789 memset(ctx, 0, sizeof(*ctx)); 1790 1791 ctx->c2p_train_data_offset = 1792 ALIGN((adev->gmc.mc_vram_size - reserve_size - SZ_1M), SZ_1M); 1793 ctx->p2c_train_data_offset = 1794 (adev->gmc.mc_vram_size - GDDR6_MEM_TRAINING_OFFSET); 1795 ctx->train_data_size = 1796 GDDR6_MEM_TRAINING_DATA_SIZE_IN_BYTES; 1797 1798 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n", 1799 ctx->train_data_size, 1800 ctx->p2c_train_data_offset, 1801 ctx->c2p_train_data_offset); 1802 } 1803 1804 /* 1805 * reserve TMR memory at the top of VRAM which holds 1806 * IP Discovery data and is protected by PSP. 1807 */ 1808 static int amdgpu_ttm_reserve_tmr(struct amdgpu_device *adev) 1809 { 1810 struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx; 1811 bool mem_train_support = false; 1812 uint32_t reserve_size = 0; 1813 int ret; 1814 1815 if (adev->bios && !amdgpu_sriov_vf(adev)) { 1816 if (amdgpu_atomfirmware_mem_training_supported(adev)) 1817 mem_train_support = true; 1818 else 1819 DRM_DEBUG("memory training does not support!\n"); 1820 } 1821 1822 /* 1823 * Query reserved tmr size through atom firmwareinfo for Sienna_Cichlid and onwards for all 1824 * the use cases (IP discovery/G6 memory training/profiling/diagnostic data.etc) 1825 * 1826 * Otherwise, fallback to legacy approach to check and reserve tmr block for ip 1827 * discovery data and G6 memory training data respectively 1828 */ 1829 if (adev->bios) 1830 reserve_size = 1831 amdgpu_atomfirmware_get_fw_reserved_fb_size(adev); 1832 1833 if (!adev->bios && 1834 (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 3) || 1835 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 4) || 1836 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 5, 0))) 1837 reserve_size = max(reserve_size, (uint32_t)280 << 20); 1838 else if (!adev->bios && 1839 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(12, 1, 0)) { 1840 if (hweight32(adev->aid_mask) == 1) 1841 reserve_size = max(reserve_size, (uint32_t)128 << 20); 1842 else 1843 reserve_size = max(reserve_size, (uint32_t)144 << 20); 1844 } else if (!reserve_size) 1845 reserve_size = DISCOVERY_TMR_OFFSET; 1846 1847 if (mem_train_support) { 1848 /* reserve vram for mem train according to TMR location */ 1849 amdgpu_ttm_training_data_block_init(adev, reserve_size); 1850 ret = amdgpu_bo_create_kernel_at(adev, 1851 ctx->c2p_train_data_offset, 1852 ctx->train_data_size, 1853 &ctx->c2p_bo, 1854 NULL); 1855 if (ret) { 1856 dev_err(adev->dev, "alloc c2p_bo failed(%d)!\n", ret); 1857 amdgpu_ttm_training_reserve_vram_fini(adev); 1858 return ret; 1859 } 1860 ctx->init = PSP_MEM_TRAIN_RESERVE_SUCCESS; 1861 } 1862 1863 ret = amdgpu_bo_create_kernel_at( 1864 adev, adev->gmc.real_vram_size - reserve_size, reserve_size, 1865 &adev->mman.fw_reserved_memory, NULL); 1866 if (ret) { 1867 dev_err(adev->dev, "alloc tmr failed(%d)!\n", ret); 1868 amdgpu_bo_free_kernel(&adev->mman.fw_reserved_memory, NULL, 1869 NULL); 1870 return ret; 1871 } 1872 1873 return 0; 1874 } 1875 1876 static int amdgpu_ttm_pools_init(struct amdgpu_device *adev) 1877 { 1878 int i; 1879 1880 if (!adev->gmc.is_app_apu || !adev->gmc.num_mem_partitions) 1881 return 0; 1882 1883 adev->mman.ttm_pools = kcalloc(adev->gmc.num_mem_partitions, 1884 sizeof(*adev->mman.ttm_pools), 1885 GFP_KERNEL); 1886 if (!adev->mman.ttm_pools) 1887 return -ENOMEM; 1888 1889 for (i = 0; i < adev->gmc.num_mem_partitions; i++) { 1890 ttm_pool_init(&adev->mman.ttm_pools[i], adev->dev, 1891 adev->gmc.mem_partitions[i].numa.node, 1892 TTM_ALLOCATION_POOL_BENEFICIAL_ORDER(get_order(SZ_2M))); 1893 } 1894 return 0; 1895 } 1896 1897 static void amdgpu_ttm_pools_fini(struct amdgpu_device *adev) 1898 { 1899 int i; 1900 1901 if (!adev->gmc.is_app_apu || !adev->mman.ttm_pools) 1902 return; 1903 1904 for (i = 0; i < adev->gmc.num_mem_partitions; i++) 1905 ttm_pool_fini(&adev->mman.ttm_pools[i]); 1906 1907 kfree(adev->mman.ttm_pools); 1908 adev->mman.ttm_pools = NULL; 1909 } 1910 1911 /** 1912 * amdgpu_ttm_mmio_remap_bo_init - Allocate the singleton 4K MMIO_REMAP BO 1913 * @adev: amdgpu device 1914 * 1915 * Allocates a one-page (4K) GEM BO in AMDGPU_GEM_DOMAIN_MMIO_REMAP when the 1916 * hardware exposes a remap base (adev->rmmio_remap.bus_addr) and the host 1917 * PAGE_SIZE is <= AMDGPU_GPU_PAGE_SIZE (4K). The BO is created as a regular 1918 * GEM object (amdgpu_bo_create). 1919 * 1920 * The BO is created as a normal GEM object via amdgpu_bo_create(), then 1921 * reserved and pinned at the TTM level (ttm_bo_pin()) so it can never be 1922 * migrated or evicted. No CPU mapping is established here. 1923 * 1924 * Return: 1925 * * 0 on success or intentional skip (feature not present/unsupported) 1926 * * negative errno on allocation failure 1927 */ 1928 static int amdgpu_ttm_mmio_remap_bo_init(struct amdgpu_device *adev) 1929 { 1930 struct amdgpu_bo_param bp; 1931 int r; 1932 1933 /* Skip if HW doesn't expose remap, or if PAGE_SIZE > AMDGPU_GPU_PAGE_SIZE (4K). */ 1934 if (!adev->rmmio_remap.bus_addr || PAGE_SIZE > AMDGPU_GPU_PAGE_SIZE) 1935 return 0; 1936 1937 memset(&bp, 0, sizeof(bp)); 1938 1939 /* Create exactly one GEM BO in the MMIO_REMAP domain. */ 1940 bp.type = ttm_bo_type_device; /* userspace-mappable GEM */ 1941 bp.size = AMDGPU_GPU_PAGE_SIZE; /* 4K */ 1942 bp.byte_align = AMDGPU_GPU_PAGE_SIZE; 1943 bp.domain = AMDGPU_GEM_DOMAIN_MMIO_REMAP; 1944 bp.flags = 0; 1945 bp.resv = NULL; 1946 bp.bo_ptr_size = sizeof(struct amdgpu_bo); 1947 1948 r = amdgpu_bo_create(adev, &bp, &adev->rmmio_remap.bo); 1949 if (r) 1950 return r; 1951 1952 r = amdgpu_bo_reserve(adev->rmmio_remap.bo, true); 1953 if (r) 1954 goto err_unref; 1955 1956 /* 1957 * MMIO_REMAP is a fixed I/O placement (AMDGPU_PL_MMIO_REMAP). 1958 * Use TTM-level pin so the BO cannot be evicted/migrated, 1959 * independent of GEM domains. This 1960 * enforces the “fixed I/O window” 1961 */ 1962 ttm_bo_pin(&adev->rmmio_remap.bo->tbo); 1963 1964 amdgpu_bo_unreserve(adev->rmmio_remap.bo); 1965 return 0; 1966 1967 err_unref: 1968 if (adev->rmmio_remap.bo) 1969 amdgpu_bo_unref(&adev->rmmio_remap.bo); 1970 adev->rmmio_remap.bo = NULL; 1971 return r; 1972 } 1973 1974 /** 1975 * amdgpu_ttm_mmio_remap_bo_fini - Free the singleton MMIO_REMAP BO 1976 * @adev: amdgpu device 1977 * 1978 * Frees the kernel-owned MMIO_REMAP BO if it was allocated by 1979 * amdgpu_ttm_mmio_remap_bo_init(). 1980 */ 1981 static void amdgpu_ttm_mmio_remap_bo_fini(struct amdgpu_device *adev) 1982 { 1983 struct amdgpu_bo *bo = adev->rmmio_remap.bo; 1984 1985 if (!bo) 1986 return; /* <-- safest early exit */ 1987 1988 if (!amdgpu_bo_reserve(adev->rmmio_remap.bo, true)) { 1989 ttm_bo_unpin(&adev->rmmio_remap.bo->tbo); 1990 amdgpu_bo_unreserve(adev->rmmio_remap.bo); 1991 } 1992 amdgpu_bo_unref(&adev->rmmio_remap.bo); 1993 adev->rmmio_remap.bo = NULL; 1994 } 1995 1996 /* 1997 * amdgpu_ttm_init - Init the memory management (ttm) as well as various 1998 * gtt/vram related fields. 1999 * 2000 * This initializes all of the memory space pools that the TTM layer 2001 * will need such as the GTT space (system memory mapped to the device), 2002 * VRAM (on-board memory), and on-chip memories (GDS, GWS, OA) which 2003 * can be mapped per VMID. 2004 */ 2005 int amdgpu_ttm_init(struct amdgpu_device *adev) 2006 { 2007 uint64_t gtt_size; 2008 int r; 2009 2010 mutex_init(&adev->mman.gtt_window_lock); 2011 2012 dma_set_max_seg_size(adev->dev, UINT_MAX); 2013 /* No others user of address space so set it to 0 */ 2014 r = ttm_device_init(&adev->mman.bdev, &amdgpu_bo_driver, adev->dev, 2015 adev_to_drm(adev)->anon_inode->i_mapping, 2016 adev_to_drm(adev)->vma_offset_manager, 2017 (adev->need_swiotlb ? 2018 TTM_ALLOCATION_POOL_USE_DMA_ALLOC : 0) | 2019 (dma_addressing_limited(adev->dev) ? 2020 TTM_ALLOCATION_POOL_USE_DMA32 : 0) | 2021 TTM_ALLOCATION_POOL_BENEFICIAL_ORDER(get_order(SZ_2M))); 2022 if (r) { 2023 dev_err(adev->dev, 2024 "failed initializing buffer object driver(%d).\n", r); 2025 return r; 2026 } 2027 2028 r = amdgpu_ttm_pools_init(adev); 2029 if (r) { 2030 dev_err(adev->dev, "failed to init ttm pools(%d).\n", r); 2031 return r; 2032 } 2033 adev->mman.initialized = true; 2034 2035 if (!adev->gmc.is_app_apu) { 2036 /* Initialize VRAM pool with all of VRAM divided into pages */ 2037 r = amdgpu_vram_mgr_init(adev); 2038 if (r) { 2039 dev_err(adev->dev, "Failed initializing VRAM heap.\n"); 2040 return r; 2041 } 2042 } 2043 2044 /* Change the size here instead of the init above so only lpfn is affected */ 2045 amdgpu_ttm_set_buffer_funcs_status(adev, false); 2046 #ifdef CONFIG_64BIT 2047 #ifdef CONFIG_X86 2048 if (adev->gmc.xgmi.connected_to_cpu) 2049 adev->mman.aper_base_kaddr = ioremap_cache(adev->gmc.aper_base, 2050 adev->gmc.visible_vram_size); 2051 2052 else if (adev->gmc.is_app_apu) 2053 DRM_DEBUG_DRIVER( 2054 "No need to ioremap when real vram size is 0\n"); 2055 else 2056 #endif 2057 adev->mman.aper_base_kaddr = ioremap_wc(adev->gmc.aper_base, 2058 adev->gmc.visible_vram_size); 2059 #endif 2060 2061 /* 2062 *The reserved vram for firmware must be pinned to the specified 2063 *place on the VRAM, so reserve it early. 2064 */ 2065 r = amdgpu_ttm_fw_reserve_vram_init(adev); 2066 if (r) 2067 return r; 2068 2069 /* 2070 * The reserved VRAM for the driver must be pinned to a specific 2071 * location in VRAM, so reserve it early. 2072 */ 2073 r = amdgpu_ttm_drv_reserve_vram_init(adev); 2074 if (r) 2075 return r; 2076 2077 /* 2078 * only NAVI10 and later ASICs support IP discovery. 2079 * If IP discovery is enabled, a block of memory should be 2080 * reserved for it. 2081 */ 2082 if (adev->discovery.reserve_tmr) { 2083 r = amdgpu_ttm_reserve_tmr(adev); 2084 if (r) 2085 return r; 2086 } 2087 2088 /* allocate memory as required for VGA 2089 * This is used for VGA emulation and pre-OS scanout buffers to 2090 * avoid display artifacts while transitioning between pre-OS 2091 * and driver. 2092 */ 2093 if (!adev->gmc.is_app_apu) { 2094 r = amdgpu_bo_create_kernel_at(adev, 0, 2095 adev->mman.stolen_vga_size, 2096 &adev->mman.stolen_vga_memory, 2097 NULL); 2098 if (r) 2099 return r; 2100 2101 r = amdgpu_bo_create_kernel_at(adev, adev->mman.stolen_vga_size, 2102 adev->mman.stolen_extended_size, 2103 &adev->mman.stolen_extended_memory, 2104 NULL); 2105 2106 if (r) 2107 return r; 2108 2109 r = amdgpu_bo_create_kernel_at(adev, 2110 adev->mman.stolen_reserved_offset, 2111 adev->mman.stolen_reserved_size, 2112 &adev->mman.stolen_reserved_memory, 2113 NULL); 2114 if (r) 2115 return r; 2116 } else { 2117 DRM_DEBUG_DRIVER("Skipped stolen memory reservation\n"); 2118 } 2119 2120 dev_info(adev->dev, " %uM of VRAM memory ready\n", 2121 (unsigned int)(adev->gmc.real_vram_size / (1024 * 1024))); 2122 2123 /* Compute GTT size, either based on TTM limit 2124 * or whatever the user passed on module init. 2125 */ 2126 gtt_size = ttm_tt_pages_limit() << PAGE_SHIFT; 2127 if (amdgpu_gtt_size != -1) { 2128 uint64_t configured_size = (uint64_t)amdgpu_gtt_size << 20; 2129 2130 drm_warn(&adev->ddev, 2131 "Configuring gttsize via module parameter is deprecated, please use ttm.pages_limit\n"); 2132 if (gtt_size != configured_size) 2133 drm_warn(&adev->ddev, 2134 "GTT size has been set as %llu but TTM size has been set as %llu, this is unusual\n", 2135 configured_size, gtt_size); 2136 2137 gtt_size = configured_size; 2138 } 2139 2140 /* Initialize GTT memory pool */ 2141 r = amdgpu_gtt_mgr_init(adev, gtt_size); 2142 if (r) { 2143 dev_err(adev->dev, "Failed initializing GTT heap.\n"); 2144 return r; 2145 } 2146 dev_info(adev->dev, " %uM of GTT memory ready.\n", 2147 (unsigned int)(gtt_size / (1024 * 1024))); 2148 2149 if (adev->flags & AMD_IS_APU) { 2150 if (adev->gmc.real_vram_size < gtt_size) 2151 adev->apu_prefer_gtt = true; 2152 } 2153 2154 /* Initialize doorbell pool on PCI BAR */ 2155 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_DOORBELL, adev->doorbell.size / PAGE_SIZE); 2156 if (r) { 2157 dev_err(adev->dev, "Failed initializing doorbell heap.\n"); 2158 return r; 2159 } 2160 2161 /* Create a boorbell page for kernel usages */ 2162 r = amdgpu_doorbell_create_kernel_doorbells(adev); 2163 if (r) { 2164 dev_err(adev->dev, "Failed to initialize kernel doorbells.\n"); 2165 return r; 2166 } 2167 2168 /* Initialize MMIO-remap pool (single page 4K) */ 2169 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_MMIO_REMAP, 1); 2170 if (r) { 2171 dev_err(adev->dev, "Failed initializing MMIO-remap heap.\n"); 2172 return r; 2173 } 2174 2175 /* Allocate the singleton MMIO_REMAP BO (4K) if supported */ 2176 r = amdgpu_ttm_mmio_remap_bo_init(adev); 2177 if (r) 2178 return r; 2179 2180 /* Initialize preemptible memory pool */ 2181 r = amdgpu_preempt_mgr_init(adev); 2182 if (r) { 2183 dev_err(adev->dev, "Failed initializing PREEMPT heap.\n"); 2184 return r; 2185 } 2186 2187 /* Initialize various on-chip memory pools */ 2188 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GDS, adev->gds.gds_size); 2189 if (r) { 2190 dev_err(adev->dev, "Failed initializing GDS heap.\n"); 2191 return r; 2192 } 2193 2194 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GWS, adev->gds.gws_size); 2195 if (r) { 2196 dev_err(adev->dev, "Failed initializing gws heap.\n"); 2197 return r; 2198 } 2199 2200 r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_OA, adev->gds.oa_size); 2201 if (r) { 2202 dev_err(adev->dev, "Failed initializing oa heap.\n"); 2203 return r; 2204 } 2205 if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE, 2206 AMDGPU_GEM_DOMAIN_GTT, 2207 &adev->mman.sdma_access_bo, NULL, 2208 &adev->mman.sdma_access_ptr)) 2209 drm_warn(adev_to_drm(adev), 2210 "Debug VRAM access will use slowpath MM access\n"); 2211 2212 return 0; 2213 } 2214 2215 /* 2216 * amdgpu_ttm_fini - De-initialize the TTM memory pools 2217 */ 2218 void amdgpu_ttm_fini(struct amdgpu_device *adev) 2219 { 2220 int idx; 2221 2222 if (!adev->mman.initialized) 2223 return; 2224 2225 amdgpu_ttm_pools_fini(adev); 2226 2227 amdgpu_ttm_training_reserve_vram_fini(adev); 2228 /* return the stolen vga memory back to VRAM */ 2229 if (!adev->gmc.is_app_apu) { 2230 amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL); 2231 amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL); 2232 /* return the FW reserved memory back to VRAM */ 2233 amdgpu_bo_free_kernel(&adev->mman.fw_reserved_memory, NULL, 2234 NULL); 2235 amdgpu_bo_free_kernel(&adev->mman.fw_reserved_memory_extend, NULL, 2236 NULL); 2237 if (adev->mman.stolen_reserved_size) 2238 amdgpu_bo_free_kernel(&adev->mman.stolen_reserved_memory, 2239 NULL, NULL); 2240 } 2241 amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo, NULL, 2242 &adev->mman.sdma_access_ptr); 2243 2244 amdgpu_ttm_mmio_remap_bo_fini(adev); 2245 amdgpu_ttm_fw_reserve_vram_fini(adev); 2246 amdgpu_ttm_drv_reserve_vram_fini(adev); 2247 2248 if (drm_dev_enter(adev_to_drm(adev), &idx)) { 2249 2250 if (adev->mman.aper_base_kaddr) 2251 iounmap(adev->mman.aper_base_kaddr); 2252 adev->mman.aper_base_kaddr = NULL; 2253 2254 drm_dev_exit(idx); 2255 } 2256 2257 if (!adev->gmc.is_app_apu) 2258 amdgpu_vram_mgr_fini(adev); 2259 amdgpu_gtt_mgr_fini(adev); 2260 amdgpu_preempt_mgr_fini(adev); 2261 amdgpu_doorbell_fini(adev); 2262 2263 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_GDS); 2264 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_GWS); 2265 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA); 2266 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_DOORBELL); 2267 ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_MMIO_REMAP); 2268 ttm_device_fini(&adev->mman.bdev); 2269 adev->mman.initialized = false; 2270 dev_info(adev->dev, " ttm finalized\n"); 2271 } 2272 2273 /** 2274 * amdgpu_ttm_set_buffer_funcs_status - enable/disable use of buffer functions 2275 * 2276 * @adev: amdgpu_device pointer 2277 * @enable: true when we can use buffer functions. 2278 * 2279 * Enable/disable use of buffer functions during suspend/resume. This should 2280 * only be called at bootup or when userspace isn't running. 2281 */ 2282 void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev, bool enable) 2283 { 2284 struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM); 2285 uint64_t size; 2286 int r; 2287 2288 if (!adev->mman.initialized || amdgpu_in_reset(adev) || 2289 adev->mman.buffer_funcs_enabled == enable || adev->gmc.is_app_apu) 2290 return; 2291 2292 if (enable) { 2293 struct amdgpu_ring *ring; 2294 struct drm_gpu_scheduler *sched; 2295 2296 ring = adev->mman.buffer_funcs_ring; 2297 sched = &ring->sched; 2298 r = drm_sched_entity_init(&adev->mman.default_entity.base, 2299 DRM_SCHED_PRIORITY_KERNEL, &sched, 2300 1, NULL); 2301 if (r) { 2302 dev_err(adev->dev, 2303 "Failed setting up TTM BO move entity (%d)\n", 2304 r); 2305 return; 2306 } 2307 2308 r = drm_sched_entity_init(&adev->mman.clear_entity.base, 2309 DRM_SCHED_PRIORITY_NORMAL, &sched, 2310 1, NULL); 2311 if (r) { 2312 dev_err(adev->dev, 2313 "Failed setting up TTM BO clear entity (%d)\n", 2314 r); 2315 goto error_free_entity; 2316 } 2317 2318 r = drm_sched_entity_init(&adev->mman.move_entity.base, 2319 DRM_SCHED_PRIORITY_NORMAL, &sched, 2320 1, NULL); 2321 if (r) { 2322 dev_err(adev->dev, 2323 "Failed setting up TTM BO move entity (%d)\n", 2324 r); 2325 drm_sched_entity_destroy(&adev->mman.clear_entity.base); 2326 goto error_free_entity; 2327 } 2328 } else { 2329 drm_sched_entity_destroy(&adev->mman.default_entity.base); 2330 drm_sched_entity_destroy(&adev->mman.clear_entity.base); 2331 drm_sched_entity_destroy(&adev->mman.move_entity.base); 2332 /* Drop all the old fences since re-creating the scheduler entities 2333 * will allocate new contexts. 2334 */ 2335 ttm_resource_manager_cleanup(man); 2336 } 2337 2338 /* this just adjusts TTM size idea, which sets lpfn to the correct value */ 2339 if (enable) 2340 size = adev->gmc.real_vram_size; 2341 else 2342 size = adev->gmc.visible_vram_size; 2343 man->size = size; 2344 adev->mman.buffer_funcs_enabled = enable; 2345 2346 return; 2347 2348 error_free_entity: 2349 drm_sched_entity_destroy(&adev->mman.default_entity.base); 2350 } 2351 2352 static int amdgpu_ttm_prepare_job(struct amdgpu_device *adev, 2353 struct amdgpu_ttm_buffer_entity *entity, 2354 unsigned int num_dw, 2355 struct dma_resv *resv, 2356 bool vm_needs_flush, 2357 struct amdgpu_job **job, 2358 u64 k_job_id) 2359 { 2360 enum amdgpu_ib_pool_type pool = AMDGPU_IB_POOL_DELAYED; 2361 int r; 2362 r = amdgpu_job_alloc_with_ib(adev, &entity->base, 2363 AMDGPU_FENCE_OWNER_UNDEFINED, 2364 num_dw * 4, pool, job, k_job_id); 2365 if (r) 2366 return r; 2367 2368 if (vm_needs_flush) { 2369 (*job)->vm_pd_addr = amdgpu_gmc_pd_addr(adev->gmc.pdb0_bo ? 2370 adev->gmc.pdb0_bo : 2371 adev->gart.bo); 2372 (*job)->vm_needs_flush = true; 2373 } 2374 if (!resv) 2375 return 0; 2376 2377 return drm_sched_job_add_resv_dependencies(&(*job)->base, resv, 2378 DMA_RESV_USAGE_BOOKKEEP); 2379 } 2380 2381 int amdgpu_copy_buffer(struct amdgpu_device *adev, 2382 struct amdgpu_ttm_buffer_entity *entity, 2383 uint64_t src_offset, 2384 uint64_t dst_offset, uint32_t byte_count, 2385 struct dma_resv *resv, 2386 struct dma_fence **fence, 2387 bool vm_needs_flush, uint32_t copy_flags) 2388 { 2389 unsigned int num_loops, num_dw; 2390 struct amdgpu_ring *ring; 2391 struct amdgpu_job *job; 2392 uint32_t max_bytes; 2393 unsigned int i; 2394 int r; 2395 2396 ring = adev->mman.buffer_funcs_ring; 2397 2398 if (!ring->sched.ready) { 2399 dev_err(adev->dev, 2400 "Trying to move memory with ring turned off.\n"); 2401 return -EINVAL; 2402 } 2403 2404 max_bytes = adev->mman.buffer_funcs->copy_max_bytes; 2405 num_loops = DIV_ROUND_UP(byte_count, max_bytes); 2406 num_dw = ALIGN(num_loops * adev->mman.buffer_funcs->copy_num_dw, 8); 2407 r = amdgpu_ttm_prepare_job(adev, entity, num_dw, 2408 resv, vm_needs_flush, &job, 2409 AMDGPU_KERNEL_JOB_ID_TTM_COPY_BUFFER); 2410 if (r) 2411 goto error_free; 2412 2413 for (i = 0; i < num_loops; i++) { 2414 uint32_t cur_size_in_bytes = min(byte_count, max_bytes); 2415 2416 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset, 2417 dst_offset, cur_size_in_bytes, copy_flags); 2418 src_offset += cur_size_in_bytes; 2419 dst_offset += cur_size_in_bytes; 2420 byte_count -= cur_size_in_bytes; 2421 } 2422 2423 *fence = amdgpu_ttm_job_submit(adev, job, num_dw); 2424 2425 return 0; 2426 2427 error_free: 2428 amdgpu_job_free(job); 2429 dev_err(adev->dev, "Error scheduling IBs (%d)\n", r); 2430 return r; 2431 } 2432 2433 static int amdgpu_ttm_fill_mem(struct amdgpu_device *adev, 2434 struct amdgpu_ttm_buffer_entity *entity, 2435 uint32_t src_data, 2436 uint64_t dst_addr, uint32_t byte_count, 2437 struct dma_resv *resv, 2438 struct dma_fence **fence, 2439 bool vm_needs_flush, 2440 u64 k_job_id) 2441 { 2442 unsigned int num_loops, num_dw; 2443 struct amdgpu_job *job; 2444 uint32_t max_bytes; 2445 unsigned int i; 2446 int r; 2447 2448 max_bytes = adev->mman.buffer_funcs->fill_max_bytes; 2449 num_loops = DIV_ROUND_UP_ULL(byte_count, max_bytes); 2450 num_dw = ALIGN(num_loops * adev->mman.buffer_funcs->fill_num_dw, 8); 2451 r = amdgpu_ttm_prepare_job(adev, entity, num_dw, resv, 2452 vm_needs_flush, &job, k_job_id); 2453 if (r) 2454 return r; 2455 2456 for (i = 0; i < num_loops; i++) { 2457 uint32_t cur_size = min(byte_count, max_bytes); 2458 2459 amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data, dst_addr, 2460 cur_size); 2461 2462 dst_addr += cur_size; 2463 byte_count -= cur_size; 2464 } 2465 2466 *fence = amdgpu_ttm_job_submit(adev, job, num_dw); 2467 return 0; 2468 } 2469 2470 /** 2471 * amdgpu_ttm_clear_buffer - clear memory buffers 2472 * @bo: amdgpu buffer object 2473 * @resv: reservation object 2474 * @fence: dma_fence associated with the operation 2475 * 2476 * Clear the memory buffer resource. 2477 * 2478 * Returns: 2479 * 0 for success or a negative error code on failure. 2480 */ 2481 int amdgpu_ttm_clear_buffer(struct amdgpu_bo *bo, 2482 struct dma_resv *resv, 2483 struct dma_fence **fence) 2484 { 2485 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 2486 struct amdgpu_res_cursor cursor; 2487 u64 addr; 2488 int r = 0; 2489 2490 if (!adev->mman.buffer_funcs_enabled) 2491 return -EINVAL; 2492 2493 if (!fence) 2494 return -EINVAL; 2495 2496 *fence = dma_fence_get_stub(); 2497 2498 amdgpu_res_first(bo->tbo.resource, 0, amdgpu_bo_size(bo), &cursor); 2499 2500 mutex_lock(&adev->mman.gtt_window_lock); 2501 while (cursor.remaining) { 2502 struct dma_fence *next = NULL; 2503 u64 size; 2504 2505 if (amdgpu_res_cleared(&cursor)) { 2506 amdgpu_res_next(&cursor, cursor.size); 2507 continue; 2508 } 2509 2510 /* Never clear more than 256MiB at once to avoid timeouts */ 2511 size = min(cursor.size, 256ULL << 20); 2512 2513 r = amdgpu_ttm_map_buffer(&adev->mman.clear_entity, 2514 &bo->tbo, bo->tbo.resource, &cursor, 2515 1, false, &size, &addr); 2516 if (r) 2517 goto err; 2518 2519 r = amdgpu_ttm_fill_mem(adev, &adev->mman.clear_entity, 0, addr, size, resv, 2520 &next, true, 2521 AMDGPU_KERNEL_JOB_ID_TTM_CLEAR_BUFFER); 2522 if (r) 2523 goto err; 2524 2525 dma_fence_put(*fence); 2526 *fence = next; 2527 2528 amdgpu_res_next(&cursor, size); 2529 } 2530 err: 2531 mutex_unlock(&adev->mman.gtt_window_lock); 2532 2533 return r; 2534 } 2535 2536 int amdgpu_fill_buffer(struct amdgpu_ttm_buffer_entity *entity, 2537 struct amdgpu_bo *bo, 2538 uint32_t src_data, 2539 struct dma_resv *resv, 2540 struct dma_fence **f, 2541 u64 k_job_id) 2542 { 2543 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 2544 struct dma_fence *fence = NULL; 2545 struct amdgpu_res_cursor dst; 2546 int r; 2547 2548 if (!adev->mman.buffer_funcs_enabled) { 2549 dev_err(adev->dev, 2550 "Trying to clear memory with ring turned off.\n"); 2551 return -EINVAL; 2552 } 2553 2554 amdgpu_res_first(bo->tbo.resource, 0, amdgpu_bo_size(bo), &dst); 2555 2556 mutex_lock(&adev->mman.gtt_window_lock); 2557 while (dst.remaining) { 2558 struct dma_fence *next; 2559 uint64_t cur_size, to; 2560 2561 /* Never fill more than 256MiB at once to avoid timeouts */ 2562 cur_size = min(dst.size, 256ULL << 20); 2563 2564 r = amdgpu_ttm_map_buffer(entity, &bo->tbo, bo->tbo.resource, &dst, 2565 1, false, &cur_size, &to); 2566 if (r) 2567 goto error; 2568 2569 r = amdgpu_ttm_fill_mem(adev, entity, 2570 src_data, to, cur_size, resv, 2571 &next, true, k_job_id); 2572 if (r) 2573 goto error; 2574 2575 dma_fence_put(fence); 2576 fence = next; 2577 2578 amdgpu_res_next(&dst, cur_size); 2579 } 2580 error: 2581 mutex_unlock(&adev->mman.gtt_window_lock); 2582 if (f) 2583 *f = dma_fence_get(fence); 2584 dma_fence_put(fence); 2585 return r; 2586 } 2587 2588 /** 2589 * amdgpu_ttm_evict_resources - evict memory buffers 2590 * @adev: amdgpu device object 2591 * @mem_type: evicted BO's memory type 2592 * 2593 * Evicts all @mem_type buffers on the lru list of the memory type. 2594 * 2595 * Returns: 2596 * 0 for success or a negative error code on failure. 2597 */ 2598 int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int mem_type) 2599 { 2600 struct ttm_resource_manager *man; 2601 2602 switch (mem_type) { 2603 case TTM_PL_VRAM: 2604 case TTM_PL_TT: 2605 case AMDGPU_PL_GWS: 2606 case AMDGPU_PL_GDS: 2607 case AMDGPU_PL_OA: 2608 man = ttm_manager_type(&adev->mman.bdev, mem_type); 2609 break; 2610 default: 2611 dev_err(adev->dev, "Trying to evict invalid memory type\n"); 2612 return -EINVAL; 2613 } 2614 2615 return ttm_resource_manager_evict_all(&adev->mman.bdev, man); 2616 } 2617 2618 #if defined(CONFIG_DEBUG_FS) 2619 2620 static int amdgpu_ttm_page_pool_show(struct seq_file *m, void *unused) 2621 { 2622 struct amdgpu_device *adev = m->private; 2623 2624 return ttm_pool_debugfs(&adev->mman.bdev.pool, m); 2625 } 2626 2627 DEFINE_SHOW_ATTRIBUTE(amdgpu_ttm_page_pool); 2628 2629 /* 2630 * amdgpu_ttm_vram_read - Linear read access to VRAM 2631 * 2632 * Accesses VRAM via MMIO for debugging purposes. 2633 */ 2634 static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf, 2635 size_t size, loff_t *pos) 2636 { 2637 struct amdgpu_device *adev = file_inode(f)->i_private; 2638 ssize_t result = 0; 2639 2640 if (size & 0x3 || *pos & 0x3) 2641 return -EINVAL; 2642 2643 if (*pos >= adev->gmc.mc_vram_size) 2644 return -ENXIO; 2645 2646 size = min(size, (size_t)(adev->gmc.mc_vram_size - *pos)); 2647 while (size) { 2648 size_t bytes = min(size, AMDGPU_TTM_VRAM_MAX_DW_READ * 4); 2649 uint32_t value[AMDGPU_TTM_VRAM_MAX_DW_READ]; 2650 2651 amdgpu_device_vram_access(adev, *pos, value, bytes, false); 2652 if (copy_to_user(buf, value, bytes)) 2653 return -EFAULT; 2654 2655 result += bytes; 2656 buf += bytes; 2657 *pos += bytes; 2658 size -= bytes; 2659 } 2660 2661 return result; 2662 } 2663 2664 /* 2665 * amdgpu_ttm_vram_write - Linear write access to VRAM 2666 * 2667 * Accesses VRAM via MMIO for debugging purposes. 2668 */ 2669 static ssize_t amdgpu_ttm_vram_write(struct file *f, const char __user *buf, 2670 size_t size, loff_t *pos) 2671 { 2672 struct amdgpu_device *adev = file_inode(f)->i_private; 2673 ssize_t result = 0; 2674 int r; 2675 2676 if (size & 0x3 || *pos & 0x3) 2677 return -EINVAL; 2678 2679 if (*pos >= adev->gmc.mc_vram_size) 2680 return -ENXIO; 2681 2682 while (size) { 2683 uint32_t value; 2684 2685 if (*pos >= adev->gmc.mc_vram_size) 2686 return result; 2687 2688 r = get_user(value, (uint32_t *)buf); 2689 if (r) 2690 return r; 2691 2692 amdgpu_device_mm_access(adev, *pos, &value, 4, true); 2693 2694 result += 4; 2695 buf += 4; 2696 *pos += 4; 2697 size -= 4; 2698 } 2699 2700 return result; 2701 } 2702 2703 static const struct file_operations amdgpu_ttm_vram_fops = { 2704 .owner = THIS_MODULE, 2705 .read = amdgpu_ttm_vram_read, 2706 .write = amdgpu_ttm_vram_write, 2707 .llseek = default_llseek, 2708 }; 2709 2710 /* 2711 * amdgpu_iomem_read - Virtual read access to GPU mapped memory 2712 * 2713 * This function is used to read memory that has been mapped to the 2714 * GPU and the known addresses are not physical addresses but instead 2715 * bus addresses (e.g., what you'd put in an IB or ring buffer). 2716 */ 2717 static ssize_t amdgpu_iomem_read(struct file *f, char __user *buf, 2718 size_t size, loff_t *pos) 2719 { 2720 struct amdgpu_device *adev = file_inode(f)->i_private; 2721 struct iommu_domain *dom; 2722 ssize_t result = 0; 2723 int r; 2724 2725 /* retrieve the IOMMU domain if any for this device */ 2726 dom = iommu_get_domain_for_dev(adev->dev); 2727 2728 while (size) { 2729 phys_addr_t addr = *pos & PAGE_MASK; 2730 loff_t off = *pos & ~PAGE_MASK; 2731 size_t bytes = PAGE_SIZE - off; 2732 unsigned long pfn; 2733 struct page *p; 2734 void *ptr; 2735 2736 bytes = min(bytes, size); 2737 2738 /* Translate the bus address to a physical address. If 2739 * the domain is NULL it means there is no IOMMU active 2740 * and the address translation is the identity 2741 */ 2742 addr = dom ? iommu_iova_to_phys(dom, addr) : addr; 2743 2744 pfn = addr >> PAGE_SHIFT; 2745 if (!pfn_valid(pfn)) 2746 return -EPERM; 2747 2748 p = pfn_to_page(pfn); 2749 if (p->mapping != adev->mman.bdev.dev_mapping) 2750 return -EPERM; 2751 2752 ptr = kmap_local_page(p); 2753 r = copy_to_user(buf, ptr + off, bytes); 2754 kunmap_local(ptr); 2755 if (r) 2756 return -EFAULT; 2757 2758 size -= bytes; 2759 *pos += bytes; 2760 result += bytes; 2761 } 2762 2763 return result; 2764 } 2765 2766 /* 2767 * amdgpu_iomem_write - Virtual write access to GPU mapped memory 2768 * 2769 * This function is used to write memory that has been mapped to the 2770 * GPU and the known addresses are not physical addresses but instead 2771 * bus addresses (e.g., what you'd put in an IB or ring buffer). 2772 */ 2773 static ssize_t amdgpu_iomem_write(struct file *f, const char __user *buf, 2774 size_t size, loff_t *pos) 2775 { 2776 struct amdgpu_device *adev = file_inode(f)->i_private; 2777 struct iommu_domain *dom; 2778 ssize_t result = 0; 2779 int r; 2780 2781 dom = iommu_get_domain_for_dev(adev->dev); 2782 2783 while (size) { 2784 phys_addr_t addr = *pos & PAGE_MASK; 2785 loff_t off = *pos & ~PAGE_MASK; 2786 size_t bytes = PAGE_SIZE - off; 2787 unsigned long pfn; 2788 struct page *p; 2789 void *ptr; 2790 2791 bytes = min(bytes, size); 2792 2793 addr = dom ? iommu_iova_to_phys(dom, addr) : addr; 2794 2795 pfn = addr >> PAGE_SHIFT; 2796 if (!pfn_valid(pfn)) 2797 return -EPERM; 2798 2799 p = pfn_to_page(pfn); 2800 if (p->mapping != adev->mman.bdev.dev_mapping) 2801 return -EPERM; 2802 2803 ptr = kmap_local_page(p); 2804 r = copy_from_user(ptr + off, buf, bytes); 2805 kunmap_local(ptr); 2806 if (r) 2807 return -EFAULT; 2808 2809 size -= bytes; 2810 *pos += bytes; 2811 result += bytes; 2812 } 2813 2814 return result; 2815 } 2816 2817 static const struct file_operations amdgpu_ttm_iomem_fops = { 2818 .owner = THIS_MODULE, 2819 .read = amdgpu_iomem_read, 2820 .write = amdgpu_iomem_write, 2821 .llseek = default_llseek 2822 }; 2823 2824 #endif 2825 2826 void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev) 2827 { 2828 #if defined(CONFIG_DEBUG_FS) 2829 struct drm_minor *minor = adev_to_drm(adev)->primary; 2830 struct dentry *root = minor->debugfs_root; 2831 2832 debugfs_create_file_size("amdgpu_vram", 0444, root, adev, 2833 &amdgpu_ttm_vram_fops, adev->gmc.mc_vram_size); 2834 debugfs_create_file("amdgpu_iomem", 0444, root, adev, 2835 &amdgpu_ttm_iomem_fops); 2836 debugfs_create_file("ttm_page_pool", 0444, root, adev, 2837 &amdgpu_ttm_page_pool_fops); 2838 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev, 2839 TTM_PL_VRAM), 2840 root, "amdgpu_vram_mm"); 2841 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev, 2842 TTM_PL_TT), 2843 root, "amdgpu_gtt_mm"); 2844 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev, 2845 AMDGPU_PL_GDS), 2846 root, "amdgpu_gds_mm"); 2847 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev, 2848 AMDGPU_PL_GWS), 2849 root, "amdgpu_gws_mm"); 2850 ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev, 2851 AMDGPU_PL_OA), 2852 root, "amdgpu_oa_mm"); 2853 2854 #endif 2855 } 2856