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