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