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