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