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