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