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