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