1 /* 2 * Copyright 2008 Advanced Micro Devices, Inc. 3 * Copyright 2008 Red Hat Inc. 4 * Copyright 2009 Jerome Glisse. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: Dave Airlie 25 * Alex Deucher 26 * Jerome Glisse 27 */ 28 #include <drm/drmP.h> 29 #include <drm/amdgpu_drm.h> 30 #include "amdgpu.h" 31 #include "amdgpu_trace.h" 32 33 /* 34 * GPUVM 35 * GPUVM is similar to the legacy gart on older asics, however 36 * rather than there being a single global gart table 37 * for the entire GPU, there are multiple VM page tables active 38 * at any given time. The VM page tables can contain a mix 39 * vram pages and system memory pages and system memory pages 40 * can be mapped as snooped (cached system pages) or unsnooped 41 * (uncached system pages). 42 * Each VM has an ID associated with it and there is a page table 43 * associated with each VMID. When execting a command buffer, 44 * the kernel tells the the ring what VMID to use for that command 45 * buffer. VMIDs are allocated dynamically as commands are submitted. 46 * The userspace drivers maintain their own address space and the kernel 47 * sets up their pages tables accordingly when they submit their 48 * command buffers and a VMID is assigned. 49 * Cayman/Trinity support up to 8 active VMs at any given time; 50 * SI supports 16. 51 */ 52 53 /** 54 * amdgpu_vm_num_pde - return the number of page directory entries 55 * 56 * @adev: amdgpu_device pointer 57 * 58 * Calculate the number of page directory entries (cayman+). 59 */ 60 static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev) 61 { 62 return adev->vm_manager.max_pfn >> amdgpu_vm_block_size; 63 } 64 65 /** 66 * amdgpu_vm_directory_size - returns the size of the page directory in bytes 67 * 68 * @adev: amdgpu_device pointer 69 * 70 * Calculate the size of the page directory in bytes (cayman+). 71 */ 72 static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev) 73 { 74 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8); 75 } 76 77 /** 78 * amdgpu_vm_get_bos - add the vm BOs to a validation list 79 * 80 * @vm: vm providing the BOs 81 * @head: head of validation list 82 * 83 * Add the page directory to the list of BOs to 84 * validate for command submission (cayman+). 85 */ 86 struct amdgpu_bo_list_entry *amdgpu_vm_get_bos(struct amdgpu_device *adev, 87 struct amdgpu_vm *vm, 88 struct list_head *head) 89 { 90 struct amdgpu_bo_list_entry *list; 91 unsigned i, idx; 92 93 list = drm_malloc_ab(vm->max_pde_used + 2, 94 sizeof(struct amdgpu_bo_list_entry)); 95 if (!list) { 96 return NULL; 97 } 98 99 /* add the vm page table to the list */ 100 list[0].robj = vm->page_directory; 101 list[0].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM; 102 list[0].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM; 103 list[0].priority = 0; 104 list[0].tv.bo = &vm->page_directory->tbo; 105 list[0].tv.shared = true; 106 list_add(&list[0].tv.head, head); 107 108 for (i = 0, idx = 1; i <= vm->max_pde_used; i++) { 109 if (!vm->page_tables[i].bo) 110 continue; 111 112 list[idx].robj = vm->page_tables[i].bo; 113 list[idx].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM; 114 list[idx].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM; 115 list[idx].priority = 0; 116 list[idx].tv.bo = &list[idx].robj->tbo; 117 list[idx].tv.shared = true; 118 list_add(&list[idx++].tv.head, head); 119 } 120 121 return list; 122 } 123 124 /** 125 * amdgpu_vm_grab_id - allocate the next free VMID 126 * 127 * @vm: vm to allocate id for 128 * @ring: ring we want to submit job to 129 * @sync: sync object where we add dependencies 130 * 131 * Allocate an id for the vm, adding fences to the sync obj as necessary. 132 * 133 * Global mutex must be locked! 134 */ 135 int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring, 136 struct amdgpu_sync *sync) 137 { 138 struct amdgpu_fence *best[AMDGPU_MAX_RINGS] = {}; 139 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx]; 140 struct amdgpu_device *adev = ring->adev; 141 142 unsigned choices[2] = {}; 143 unsigned i; 144 145 /* check if the id is still valid */ 146 if (vm_id->id && vm_id->last_id_use && 147 vm_id->last_id_use == adev->vm_manager.active[vm_id->id]) { 148 trace_amdgpu_vm_grab_id(vm_id->id, ring->idx); 149 return 0; 150 } 151 152 /* we definately need to flush */ 153 vm_id->pd_gpu_addr = ~0ll; 154 155 /* skip over VMID 0, since it is the system VM */ 156 for (i = 1; i < adev->vm_manager.nvm; ++i) { 157 struct amdgpu_fence *fence = adev->vm_manager.active[i]; 158 159 if (fence == NULL) { 160 /* found a free one */ 161 vm_id->id = i; 162 trace_amdgpu_vm_grab_id(i, ring->idx); 163 return 0; 164 } 165 166 if (amdgpu_fence_is_earlier(fence, best[fence->ring->idx])) { 167 best[fence->ring->idx] = fence; 168 choices[fence->ring == ring ? 0 : 1] = i; 169 } 170 } 171 172 for (i = 0; i < 2; ++i) { 173 if (choices[i]) { 174 struct amdgpu_fence *fence; 175 176 fence = adev->vm_manager.active[choices[i]]; 177 vm_id->id = choices[i]; 178 179 trace_amdgpu_vm_grab_id(choices[i], ring->idx); 180 return amdgpu_sync_fence(ring->adev, sync, &fence->base); 181 } 182 } 183 184 /* should never happen */ 185 BUG(); 186 return -EINVAL; 187 } 188 189 /** 190 * amdgpu_vm_flush - hardware flush the vm 191 * 192 * @ring: ring to use for flush 193 * @vm: vm we want to flush 194 * @updates: last vm update that we waited for 195 * 196 * Flush the vm (cayman+). 197 * 198 * Global and local mutex must be locked! 199 */ 200 void amdgpu_vm_flush(struct amdgpu_ring *ring, 201 struct amdgpu_vm *vm, 202 struct fence *updates) 203 { 204 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory); 205 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx]; 206 struct fence *flushed_updates = vm_id->flushed_updates; 207 bool is_earlier = false; 208 209 if (flushed_updates && updates) { 210 BUG_ON(flushed_updates->context != updates->context); 211 is_earlier = (updates->seqno - flushed_updates->seqno <= 212 INT_MAX) ? true : false; 213 } 214 215 if (pd_addr != vm_id->pd_gpu_addr || !flushed_updates || 216 is_earlier) { 217 218 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id->id); 219 if (is_earlier) { 220 vm_id->flushed_updates = fence_get(updates); 221 fence_put(flushed_updates); 222 } 223 if (!flushed_updates) 224 vm_id->flushed_updates = fence_get(updates); 225 vm_id->pd_gpu_addr = pd_addr; 226 amdgpu_ring_emit_vm_flush(ring, vm_id->id, vm_id->pd_gpu_addr); 227 } 228 } 229 230 /** 231 * amdgpu_vm_fence - remember fence for vm 232 * 233 * @adev: amdgpu_device pointer 234 * @vm: vm we want to fence 235 * @fence: fence to remember 236 * 237 * Fence the vm (cayman+). 238 * Set the fence used to protect page table and id. 239 * 240 * Global and local mutex must be locked! 241 */ 242 void amdgpu_vm_fence(struct amdgpu_device *adev, 243 struct amdgpu_vm *vm, 244 struct amdgpu_fence *fence) 245 { 246 unsigned ridx = fence->ring->idx; 247 unsigned vm_id = vm->ids[ridx].id; 248 249 amdgpu_fence_unref(&adev->vm_manager.active[vm_id]); 250 adev->vm_manager.active[vm_id] = amdgpu_fence_ref(fence); 251 252 amdgpu_fence_unref(&vm->ids[ridx].last_id_use); 253 vm->ids[ridx].last_id_use = amdgpu_fence_ref(fence); 254 } 255 256 /** 257 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo 258 * 259 * @vm: requested vm 260 * @bo: requested buffer object 261 * 262 * Find @bo inside the requested vm (cayman+). 263 * Search inside the @bos vm list for the requested vm 264 * Returns the found bo_va or NULL if none is found 265 * 266 * Object has to be reserved! 267 */ 268 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm, 269 struct amdgpu_bo *bo) 270 { 271 struct amdgpu_bo_va *bo_va; 272 273 list_for_each_entry(bo_va, &bo->va, bo_list) { 274 if (bo_va->vm == vm) { 275 return bo_va; 276 } 277 } 278 return NULL; 279 } 280 281 /** 282 * amdgpu_vm_update_pages - helper to call the right asic function 283 * 284 * @adev: amdgpu_device pointer 285 * @ib: indirect buffer to fill with commands 286 * @pe: addr of the page entry 287 * @addr: dst addr to write into pe 288 * @count: number of page entries to update 289 * @incr: increase next addr by incr bytes 290 * @flags: hw access flags 291 * @gtt_flags: GTT hw access flags 292 * 293 * Traces the parameters and calls the right asic functions 294 * to setup the page table using the DMA. 295 */ 296 static void amdgpu_vm_update_pages(struct amdgpu_device *adev, 297 struct amdgpu_ib *ib, 298 uint64_t pe, uint64_t addr, 299 unsigned count, uint32_t incr, 300 uint32_t flags, uint32_t gtt_flags) 301 { 302 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags); 303 304 if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) { 305 uint64_t src = adev->gart.table_addr + (addr >> 12) * 8; 306 amdgpu_vm_copy_pte(adev, ib, pe, src, count); 307 308 } else if ((flags & AMDGPU_PTE_SYSTEM) || (count < 3)) { 309 amdgpu_vm_write_pte(adev, ib, pe, addr, 310 count, incr, flags); 311 312 } else { 313 amdgpu_vm_set_pte_pde(adev, ib, pe, addr, 314 count, incr, flags); 315 } 316 } 317 318 int amdgpu_vm_free_job(struct amdgpu_job *job) 319 { 320 int i; 321 for (i = 0; i < job->num_ibs; i++) 322 amdgpu_ib_free(job->adev, &job->ibs[i]); 323 kfree(job->ibs); 324 return 0; 325 } 326 327 /** 328 * amdgpu_vm_clear_bo - initially clear the page dir/table 329 * 330 * @adev: amdgpu_device pointer 331 * @bo: bo to clear 332 */ 333 static int amdgpu_vm_clear_bo(struct amdgpu_device *adev, 334 struct amdgpu_bo *bo) 335 { 336 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring; 337 struct fence *fence = NULL; 338 struct amdgpu_ib *ib; 339 unsigned entries; 340 uint64_t addr; 341 int r; 342 343 r = amdgpu_bo_reserve(bo, false); 344 if (r) 345 return r; 346 347 r = reservation_object_reserve_shared(bo->tbo.resv); 348 if (r) 349 return r; 350 351 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false); 352 if (r) 353 goto error_unreserve; 354 355 addr = amdgpu_bo_gpu_offset(bo); 356 entries = amdgpu_bo_size(bo) / 8; 357 358 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL); 359 if (!ib) 360 goto error_unreserve; 361 362 r = amdgpu_ib_get(ring, NULL, entries * 2 + 64, ib); 363 if (r) 364 goto error_free; 365 366 ib->length_dw = 0; 367 368 amdgpu_vm_update_pages(adev, ib, addr, 0, entries, 0, 0, 0); 369 amdgpu_vm_pad_ib(adev, ib); 370 WARN_ON(ib->length_dw > 64); 371 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1, 372 &amdgpu_vm_free_job, 373 AMDGPU_FENCE_OWNER_VM, 374 &fence); 375 if (!r) 376 amdgpu_bo_fence(bo, fence, true); 377 fence_put(fence); 378 if (amdgpu_enable_scheduler) { 379 amdgpu_bo_unreserve(bo); 380 return 0; 381 } 382 error_free: 383 amdgpu_ib_free(adev, ib); 384 kfree(ib); 385 386 error_unreserve: 387 amdgpu_bo_unreserve(bo); 388 return r; 389 } 390 391 /** 392 * amdgpu_vm_map_gart - get the physical address of a gart page 393 * 394 * @adev: amdgpu_device pointer 395 * @addr: the unmapped addr 396 * 397 * Look up the physical address of the page that the pte resolves 398 * to (cayman+). 399 * Returns the physical address of the page. 400 */ 401 uint64_t amdgpu_vm_map_gart(struct amdgpu_device *adev, uint64_t addr) 402 { 403 uint64_t result; 404 405 /* page table offset */ 406 result = adev->gart.pages_addr[addr >> PAGE_SHIFT]; 407 408 /* in case cpu page size != gpu page size*/ 409 result |= addr & (~PAGE_MASK); 410 411 return result; 412 } 413 414 /** 415 * amdgpu_vm_update_pdes - make sure that page directory is valid 416 * 417 * @adev: amdgpu_device pointer 418 * @vm: requested vm 419 * @start: start of GPU address range 420 * @end: end of GPU address range 421 * 422 * Allocates new page tables if necessary 423 * and updates the page directory (cayman+). 424 * Returns 0 for success, error for failure. 425 * 426 * Global and local mutex must be locked! 427 */ 428 int amdgpu_vm_update_page_directory(struct amdgpu_device *adev, 429 struct amdgpu_vm *vm) 430 { 431 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring; 432 struct amdgpu_bo *pd = vm->page_directory; 433 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd); 434 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8; 435 uint64_t last_pde = ~0, last_pt = ~0; 436 unsigned count = 0, pt_idx, ndw; 437 struct amdgpu_ib *ib; 438 struct fence *fence = NULL; 439 440 int r; 441 442 /* padding, etc. */ 443 ndw = 64; 444 445 /* assume the worst case */ 446 ndw += vm->max_pde_used * 6; 447 448 /* update too big for an IB */ 449 if (ndw > 0xfffff) 450 return -ENOMEM; 451 452 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL); 453 if (!ib) 454 return -ENOMEM; 455 456 r = amdgpu_ib_get(ring, NULL, ndw * 4, ib); 457 if (r) { 458 kfree(ib); 459 return r; 460 } 461 ib->length_dw = 0; 462 463 /* walk over the address space and update the page directory */ 464 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) { 465 struct amdgpu_bo *bo = vm->page_tables[pt_idx].bo; 466 uint64_t pde, pt; 467 468 if (bo == NULL) 469 continue; 470 471 pt = amdgpu_bo_gpu_offset(bo); 472 if (vm->page_tables[pt_idx].addr == pt) 473 continue; 474 vm->page_tables[pt_idx].addr = pt; 475 476 pde = pd_addr + pt_idx * 8; 477 if (((last_pde + 8 * count) != pde) || 478 ((last_pt + incr * count) != pt)) { 479 480 if (count) { 481 amdgpu_vm_update_pages(adev, ib, last_pde, 482 last_pt, count, incr, 483 AMDGPU_PTE_VALID, 0); 484 } 485 486 count = 1; 487 last_pde = pde; 488 last_pt = pt; 489 } else { 490 ++count; 491 } 492 } 493 494 if (count) 495 amdgpu_vm_update_pages(adev, ib, last_pde, last_pt, count, 496 incr, AMDGPU_PTE_VALID, 0); 497 498 if (ib->length_dw != 0) { 499 amdgpu_vm_pad_ib(adev, ib); 500 amdgpu_sync_resv(adev, &ib->sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM); 501 WARN_ON(ib->length_dw > ndw); 502 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1, 503 &amdgpu_vm_free_job, 504 AMDGPU_FENCE_OWNER_VM, 505 &fence); 506 if (r) 507 goto error_free; 508 509 amdgpu_bo_fence(pd, fence, true); 510 fence_put(vm->page_directory_fence); 511 vm->page_directory_fence = fence_get(fence); 512 fence_put(fence); 513 } 514 515 if (!amdgpu_enable_scheduler || ib->length_dw == 0) { 516 amdgpu_ib_free(adev, ib); 517 kfree(ib); 518 } 519 520 return 0; 521 522 error_free: 523 amdgpu_ib_free(adev, ib); 524 kfree(ib); 525 return r; 526 } 527 528 /** 529 * amdgpu_vm_frag_ptes - add fragment information to PTEs 530 * 531 * @adev: amdgpu_device pointer 532 * @ib: IB for the update 533 * @pe_start: first PTE to handle 534 * @pe_end: last PTE to handle 535 * @addr: addr those PTEs should point to 536 * @flags: hw mapping flags 537 * @gtt_flags: GTT hw mapping flags 538 * 539 * Global and local mutex must be locked! 540 */ 541 static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev, 542 struct amdgpu_ib *ib, 543 uint64_t pe_start, uint64_t pe_end, 544 uint64_t addr, uint32_t flags, 545 uint32_t gtt_flags) 546 { 547 /** 548 * The MC L1 TLB supports variable sized pages, based on a fragment 549 * field in the PTE. When this field is set to a non-zero value, page 550 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE 551 * flags are considered valid for all PTEs within the fragment range 552 * and corresponding mappings are assumed to be physically contiguous. 553 * 554 * The L1 TLB can store a single PTE for the whole fragment, 555 * significantly increasing the space available for translation 556 * caching. This leads to large improvements in throughput when the 557 * TLB is under pressure. 558 * 559 * The L2 TLB distributes small and large fragments into two 560 * asymmetric partitions. The large fragment cache is significantly 561 * larger. Thus, we try to use large fragments wherever possible. 562 * Userspace can support this by aligning virtual base address and 563 * allocation size to the fragment size. 564 */ 565 566 /* SI and newer are optimized for 64KB */ 567 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB; 568 uint64_t frag_align = 0x80; 569 570 uint64_t frag_start = ALIGN(pe_start, frag_align); 571 uint64_t frag_end = pe_end & ~(frag_align - 1); 572 573 unsigned count; 574 575 /* system pages are non continuously */ 576 if ((flags & AMDGPU_PTE_SYSTEM) || !(flags & AMDGPU_PTE_VALID) || 577 (frag_start >= frag_end)) { 578 579 count = (pe_end - pe_start) / 8; 580 amdgpu_vm_update_pages(adev, ib, pe_start, addr, count, 581 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags); 582 return; 583 } 584 585 /* handle the 4K area at the beginning */ 586 if (pe_start != frag_start) { 587 count = (frag_start - pe_start) / 8; 588 amdgpu_vm_update_pages(adev, ib, pe_start, addr, count, 589 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags); 590 addr += AMDGPU_GPU_PAGE_SIZE * count; 591 } 592 593 /* handle the area in the middle */ 594 count = (frag_end - frag_start) / 8; 595 amdgpu_vm_update_pages(adev, ib, frag_start, addr, count, 596 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags, 597 gtt_flags); 598 599 /* handle the 4K area at the end */ 600 if (frag_end != pe_end) { 601 addr += AMDGPU_GPU_PAGE_SIZE * count; 602 count = (pe_end - frag_end) / 8; 603 amdgpu_vm_update_pages(adev, ib, frag_end, addr, count, 604 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags); 605 } 606 } 607 608 /** 609 * amdgpu_vm_update_ptes - make sure that page tables are valid 610 * 611 * @adev: amdgpu_device pointer 612 * @vm: requested vm 613 * @start: start of GPU address range 614 * @end: end of GPU address range 615 * @dst: destination address to map to 616 * @flags: mapping flags 617 * 618 * Update the page tables in the range @start - @end (cayman+). 619 * 620 * Global and local mutex must be locked! 621 */ 622 static int amdgpu_vm_update_ptes(struct amdgpu_device *adev, 623 struct amdgpu_vm *vm, 624 struct amdgpu_ib *ib, 625 uint64_t start, uint64_t end, 626 uint64_t dst, uint32_t flags, 627 uint32_t gtt_flags) 628 { 629 uint64_t mask = AMDGPU_VM_PTE_COUNT - 1; 630 uint64_t last_pte = ~0, last_dst = ~0; 631 void *owner = AMDGPU_FENCE_OWNER_VM; 632 unsigned count = 0; 633 uint64_t addr; 634 635 /* sync to everything on unmapping */ 636 if (!(flags & AMDGPU_PTE_VALID)) 637 owner = AMDGPU_FENCE_OWNER_UNDEFINED; 638 639 /* walk over the address space and update the page tables */ 640 for (addr = start; addr < end; ) { 641 uint64_t pt_idx = addr >> amdgpu_vm_block_size; 642 struct amdgpu_bo *pt = vm->page_tables[pt_idx].bo; 643 unsigned nptes; 644 uint64_t pte; 645 int r; 646 647 amdgpu_sync_resv(adev, &ib->sync, pt->tbo.resv, owner); 648 r = reservation_object_reserve_shared(pt->tbo.resv); 649 if (r) 650 return r; 651 652 if ((addr & ~mask) == (end & ~mask)) 653 nptes = end - addr; 654 else 655 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask); 656 657 pte = amdgpu_bo_gpu_offset(pt); 658 pte += (addr & mask) * 8; 659 660 if ((last_pte + 8 * count) != pte) { 661 662 if (count) { 663 amdgpu_vm_frag_ptes(adev, ib, last_pte, 664 last_pte + 8 * count, 665 last_dst, flags, 666 gtt_flags); 667 } 668 669 count = nptes; 670 last_pte = pte; 671 last_dst = dst; 672 } else { 673 count += nptes; 674 } 675 676 addr += nptes; 677 dst += nptes * AMDGPU_GPU_PAGE_SIZE; 678 } 679 680 if (count) { 681 amdgpu_vm_frag_ptes(adev, ib, last_pte, 682 last_pte + 8 * count, 683 last_dst, flags, gtt_flags); 684 } 685 686 return 0; 687 } 688 689 /** 690 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table 691 * 692 * @adev: amdgpu_device pointer 693 * @vm: requested vm 694 * @mapping: mapped range and flags to use for the update 695 * @addr: addr to set the area to 696 * @gtt_flags: flags as they are used for GTT 697 * @fence: optional resulting fence 698 * 699 * Fill in the page table entries for @mapping. 700 * Returns 0 for success, -EINVAL for failure. 701 * 702 * Object have to be reserved and mutex must be locked! 703 */ 704 static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev, 705 struct amdgpu_vm *vm, 706 struct amdgpu_bo_va_mapping *mapping, 707 uint64_t addr, uint32_t gtt_flags, 708 struct fence **fence) 709 { 710 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring; 711 unsigned nptes, ncmds, ndw; 712 uint32_t flags = gtt_flags; 713 struct amdgpu_ib *ib; 714 struct fence *f = NULL; 715 int r; 716 717 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here 718 * but in case of something, we filter the flags in first place 719 */ 720 if (!(mapping->flags & AMDGPU_PTE_READABLE)) 721 flags &= ~AMDGPU_PTE_READABLE; 722 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE)) 723 flags &= ~AMDGPU_PTE_WRITEABLE; 724 725 trace_amdgpu_vm_bo_update(mapping); 726 727 nptes = mapping->it.last - mapping->it.start + 1; 728 729 /* 730 * reserve space for one command every (1 << BLOCK_SIZE) 731 * entries or 2k dwords (whatever is smaller) 732 */ 733 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1; 734 735 /* padding, etc. */ 736 ndw = 64; 737 738 if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) { 739 /* only copy commands needed */ 740 ndw += ncmds * 7; 741 742 } else if (flags & AMDGPU_PTE_SYSTEM) { 743 /* header for write data commands */ 744 ndw += ncmds * 4; 745 746 /* body of write data command */ 747 ndw += nptes * 2; 748 749 } else { 750 /* set page commands needed */ 751 ndw += ncmds * 10; 752 753 /* two extra commands for begin/end of fragment */ 754 ndw += 2 * 10; 755 } 756 757 /* update too big for an IB */ 758 if (ndw > 0xfffff) 759 return -ENOMEM; 760 761 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL); 762 if (!ib) 763 return -ENOMEM; 764 765 r = amdgpu_ib_get(ring, NULL, ndw * 4, ib); 766 if (r) { 767 kfree(ib); 768 return r; 769 } 770 771 ib->length_dw = 0; 772 773 r = amdgpu_vm_update_ptes(adev, vm, ib, mapping->it.start, 774 mapping->it.last + 1, addr + mapping->offset, 775 flags, gtt_flags); 776 777 if (r) { 778 amdgpu_ib_free(adev, ib); 779 kfree(ib); 780 return r; 781 } 782 783 amdgpu_vm_pad_ib(adev, ib); 784 WARN_ON(ib->length_dw > ndw); 785 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1, 786 &amdgpu_vm_free_job, 787 AMDGPU_FENCE_OWNER_VM, 788 &f); 789 if (r) 790 goto error_free; 791 792 amdgpu_bo_fence(vm->page_directory, f, true); 793 if (fence) { 794 fence_put(*fence); 795 *fence = fence_get(f); 796 } 797 fence_put(f); 798 if (!amdgpu_enable_scheduler) { 799 amdgpu_ib_free(adev, ib); 800 kfree(ib); 801 } 802 return 0; 803 804 error_free: 805 amdgpu_ib_free(adev, ib); 806 kfree(ib); 807 return r; 808 } 809 810 /** 811 * amdgpu_vm_bo_update - update all BO mappings in the vm page table 812 * 813 * @adev: amdgpu_device pointer 814 * @bo_va: requested BO and VM object 815 * @mem: ttm mem 816 * 817 * Fill in the page table entries for @bo_va. 818 * Returns 0 for success, -EINVAL for failure. 819 * 820 * Object have to be reserved and mutex must be locked! 821 */ 822 int amdgpu_vm_bo_update(struct amdgpu_device *adev, 823 struct amdgpu_bo_va *bo_va, 824 struct ttm_mem_reg *mem) 825 { 826 struct amdgpu_vm *vm = bo_va->vm; 827 struct amdgpu_bo_va_mapping *mapping; 828 uint32_t flags; 829 uint64_t addr; 830 int r; 831 832 if (mem) { 833 addr = (u64)mem->start << PAGE_SHIFT; 834 if (mem->mem_type != TTM_PL_TT) 835 addr += adev->vm_manager.vram_base_offset; 836 } else { 837 addr = 0; 838 } 839 840 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem); 841 842 spin_lock(&vm->status_lock); 843 if (!list_empty(&bo_va->vm_status)) 844 list_splice_init(&bo_va->valids, &bo_va->invalids); 845 spin_unlock(&vm->status_lock); 846 847 list_for_each_entry(mapping, &bo_va->invalids, list) { 848 r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, addr, 849 flags, &bo_va->last_pt_update); 850 if (r) 851 return r; 852 } 853 854 if (trace_amdgpu_vm_bo_mapping_enabled()) { 855 list_for_each_entry(mapping, &bo_va->valids, list) 856 trace_amdgpu_vm_bo_mapping(mapping); 857 858 list_for_each_entry(mapping, &bo_va->invalids, list) 859 trace_amdgpu_vm_bo_mapping(mapping); 860 } 861 862 spin_lock(&vm->status_lock); 863 list_splice_init(&bo_va->invalids, &bo_va->valids); 864 list_del_init(&bo_va->vm_status); 865 if (!mem) 866 list_add(&bo_va->vm_status, &vm->cleared); 867 spin_unlock(&vm->status_lock); 868 869 return 0; 870 } 871 872 /** 873 * amdgpu_vm_clear_freed - clear freed BOs in the PT 874 * 875 * @adev: amdgpu_device pointer 876 * @vm: requested vm 877 * 878 * Make sure all freed BOs are cleared in the PT. 879 * Returns 0 for success. 880 * 881 * PTs have to be reserved and mutex must be locked! 882 */ 883 int amdgpu_vm_clear_freed(struct amdgpu_device *adev, 884 struct amdgpu_vm *vm) 885 { 886 struct amdgpu_bo_va_mapping *mapping; 887 int r; 888 889 while (!list_empty(&vm->freed)) { 890 mapping = list_first_entry(&vm->freed, 891 struct amdgpu_bo_va_mapping, list); 892 list_del(&mapping->list); 893 894 r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, 0, 0, NULL); 895 kfree(mapping); 896 if (r) 897 return r; 898 899 } 900 return 0; 901 902 } 903 904 /** 905 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT 906 * 907 * @adev: amdgpu_device pointer 908 * @vm: requested vm 909 * 910 * Make sure all invalidated BOs are cleared in the PT. 911 * Returns 0 for success. 912 * 913 * PTs have to be reserved and mutex must be locked! 914 */ 915 int amdgpu_vm_clear_invalids(struct amdgpu_device *adev, 916 struct amdgpu_vm *vm, struct amdgpu_sync *sync) 917 { 918 struct amdgpu_bo_va *bo_va = NULL; 919 int r = 0; 920 921 spin_lock(&vm->status_lock); 922 while (!list_empty(&vm->invalidated)) { 923 bo_va = list_first_entry(&vm->invalidated, 924 struct amdgpu_bo_va, vm_status); 925 spin_unlock(&vm->status_lock); 926 927 r = amdgpu_vm_bo_update(adev, bo_va, NULL); 928 if (r) 929 return r; 930 931 spin_lock(&vm->status_lock); 932 } 933 spin_unlock(&vm->status_lock); 934 935 if (bo_va) 936 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update); 937 938 return r; 939 } 940 941 /** 942 * amdgpu_vm_bo_add - add a bo to a specific vm 943 * 944 * @adev: amdgpu_device pointer 945 * @vm: requested vm 946 * @bo: amdgpu buffer object 947 * 948 * Add @bo into the requested vm (cayman+). 949 * Add @bo to the list of bos associated with the vm 950 * Returns newly added bo_va or NULL for failure 951 * 952 * Object has to be reserved! 953 */ 954 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, 955 struct amdgpu_vm *vm, 956 struct amdgpu_bo *bo) 957 { 958 struct amdgpu_bo_va *bo_va; 959 960 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL); 961 if (bo_va == NULL) { 962 return NULL; 963 } 964 bo_va->vm = vm; 965 bo_va->bo = bo; 966 bo_va->ref_count = 1; 967 INIT_LIST_HEAD(&bo_va->bo_list); 968 INIT_LIST_HEAD(&bo_va->valids); 969 INIT_LIST_HEAD(&bo_va->invalids); 970 INIT_LIST_HEAD(&bo_va->vm_status); 971 972 list_add_tail(&bo_va->bo_list, &bo->va); 973 974 return bo_va; 975 } 976 977 /** 978 * amdgpu_vm_bo_map - map bo inside a vm 979 * 980 * @adev: amdgpu_device pointer 981 * @bo_va: bo_va to store the address 982 * @saddr: where to map the BO 983 * @offset: requested offset in the BO 984 * @flags: attributes of pages (read/write/valid/etc.) 985 * 986 * Add a mapping of the BO at the specefied addr into the VM. 987 * Returns 0 for success, error for failure. 988 * 989 * Object has to be reserved and gets unreserved by this function! 990 */ 991 int amdgpu_vm_bo_map(struct amdgpu_device *adev, 992 struct amdgpu_bo_va *bo_va, 993 uint64_t saddr, uint64_t offset, 994 uint64_t size, uint32_t flags) 995 { 996 struct amdgpu_bo_va_mapping *mapping; 997 struct amdgpu_vm *vm = bo_va->vm; 998 struct interval_tree_node *it; 999 unsigned last_pfn, pt_idx; 1000 uint64_t eaddr; 1001 int r; 1002 1003 /* validate the parameters */ 1004 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK || 1005 size == 0 || size & AMDGPU_GPU_PAGE_MASK) { 1006 amdgpu_bo_unreserve(bo_va->bo); 1007 return -EINVAL; 1008 } 1009 1010 /* make sure object fit at this offset */ 1011 eaddr = saddr + size; 1012 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo))) { 1013 amdgpu_bo_unreserve(bo_va->bo); 1014 return -EINVAL; 1015 } 1016 1017 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE; 1018 if (last_pfn > adev->vm_manager.max_pfn) { 1019 dev_err(adev->dev, "va above limit (0x%08X > 0x%08X)\n", 1020 last_pfn, adev->vm_manager.max_pfn); 1021 amdgpu_bo_unreserve(bo_va->bo); 1022 return -EINVAL; 1023 } 1024 1025 saddr /= AMDGPU_GPU_PAGE_SIZE; 1026 eaddr /= AMDGPU_GPU_PAGE_SIZE; 1027 1028 it = interval_tree_iter_first(&vm->va, saddr, eaddr - 1); 1029 if (it) { 1030 struct amdgpu_bo_va_mapping *tmp; 1031 tmp = container_of(it, struct amdgpu_bo_va_mapping, it); 1032 /* bo and tmp overlap, invalid addr */ 1033 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with " 1034 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr, 1035 tmp->it.start, tmp->it.last + 1); 1036 amdgpu_bo_unreserve(bo_va->bo); 1037 r = -EINVAL; 1038 goto error; 1039 } 1040 1041 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); 1042 if (!mapping) { 1043 amdgpu_bo_unreserve(bo_va->bo); 1044 r = -ENOMEM; 1045 goto error; 1046 } 1047 1048 INIT_LIST_HEAD(&mapping->list); 1049 mapping->it.start = saddr; 1050 mapping->it.last = eaddr - 1; 1051 mapping->offset = offset; 1052 mapping->flags = flags; 1053 1054 list_add(&mapping->list, &bo_va->invalids); 1055 interval_tree_insert(&mapping->it, &vm->va); 1056 trace_amdgpu_vm_bo_map(bo_va, mapping); 1057 1058 /* Make sure the page tables are allocated */ 1059 saddr >>= amdgpu_vm_block_size; 1060 eaddr >>= amdgpu_vm_block_size; 1061 1062 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev)); 1063 1064 if (eaddr > vm->max_pde_used) 1065 vm->max_pde_used = eaddr; 1066 1067 amdgpu_bo_unreserve(bo_va->bo); 1068 1069 /* walk over the address space and allocate the page tables */ 1070 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) { 1071 struct reservation_object *resv = vm->page_directory->tbo.resv; 1072 struct amdgpu_bo *pt; 1073 1074 if (vm->page_tables[pt_idx].bo) 1075 continue; 1076 1077 ww_mutex_lock(&resv->lock, NULL); 1078 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8, 1079 AMDGPU_GPU_PAGE_SIZE, true, 1080 AMDGPU_GEM_DOMAIN_VRAM, 1081 AMDGPU_GEM_CREATE_NO_CPU_ACCESS, 1082 NULL, resv, &pt); 1083 ww_mutex_unlock(&resv->lock); 1084 if (r) 1085 goto error_free; 1086 1087 r = amdgpu_vm_clear_bo(adev, pt); 1088 if (r) { 1089 amdgpu_bo_unref(&pt); 1090 goto error_free; 1091 } 1092 1093 vm->page_tables[pt_idx].addr = 0; 1094 vm->page_tables[pt_idx].bo = pt; 1095 } 1096 1097 return 0; 1098 1099 error_free: 1100 list_del(&mapping->list); 1101 interval_tree_remove(&mapping->it, &vm->va); 1102 trace_amdgpu_vm_bo_unmap(bo_va, mapping); 1103 kfree(mapping); 1104 1105 error: 1106 return r; 1107 } 1108 1109 /** 1110 * amdgpu_vm_bo_unmap - remove bo mapping from vm 1111 * 1112 * @adev: amdgpu_device pointer 1113 * @bo_va: bo_va to remove the address from 1114 * @saddr: where to the BO is mapped 1115 * 1116 * Remove a mapping of the BO at the specefied addr from the VM. 1117 * Returns 0 for success, error for failure. 1118 * 1119 * Object has to be reserved and gets unreserved by this function! 1120 */ 1121 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, 1122 struct amdgpu_bo_va *bo_va, 1123 uint64_t saddr) 1124 { 1125 struct amdgpu_bo_va_mapping *mapping; 1126 struct amdgpu_vm *vm = bo_va->vm; 1127 bool valid = true; 1128 1129 saddr /= AMDGPU_GPU_PAGE_SIZE; 1130 1131 list_for_each_entry(mapping, &bo_va->valids, list) { 1132 if (mapping->it.start == saddr) 1133 break; 1134 } 1135 1136 if (&mapping->list == &bo_va->valids) { 1137 valid = false; 1138 1139 list_for_each_entry(mapping, &bo_va->invalids, list) { 1140 if (mapping->it.start == saddr) 1141 break; 1142 } 1143 1144 if (&mapping->list == &bo_va->invalids) { 1145 amdgpu_bo_unreserve(bo_va->bo); 1146 return -ENOENT; 1147 } 1148 } 1149 1150 list_del(&mapping->list); 1151 interval_tree_remove(&mapping->it, &vm->va); 1152 trace_amdgpu_vm_bo_unmap(bo_va, mapping); 1153 1154 if (valid) 1155 list_add(&mapping->list, &vm->freed); 1156 else 1157 kfree(mapping); 1158 amdgpu_bo_unreserve(bo_va->bo); 1159 1160 return 0; 1161 } 1162 1163 /** 1164 * amdgpu_vm_bo_rmv - remove a bo to a specific vm 1165 * 1166 * @adev: amdgpu_device pointer 1167 * @bo_va: requested bo_va 1168 * 1169 * Remove @bo_va->bo from the requested vm (cayman+). 1170 * 1171 * Object have to be reserved! 1172 */ 1173 void amdgpu_vm_bo_rmv(struct amdgpu_device *adev, 1174 struct amdgpu_bo_va *bo_va) 1175 { 1176 struct amdgpu_bo_va_mapping *mapping, *next; 1177 struct amdgpu_vm *vm = bo_va->vm; 1178 1179 list_del(&bo_va->bo_list); 1180 1181 spin_lock(&vm->status_lock); 1182 list_del(&bo_va->vm_status); 1183 spin_unlock(&vm->status_lock); 1184 1185 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) { 1186 list_del(&mapping->list); 1187 interval_tree_remove(&mapping->it, &vm->va); 1188 trace_amdgpu_vm_bo_unmap(bo_va, mapping); 1189 list_add(&mapping->list, &vm->freed); 1190 } 1191 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) { 1192 list_del(&mapping->list); 1193 interval_tree_remove(&mapping->it, &vm->va); 1194 kfree(mapping); 1195 } 1196 1197 fence_put(bo_va->last_pt_update); 1198 kfree(bo_va); 1199 } 1200 1201 /** 1202 * amdgpu_vm_bo_invalidate - mark the bo as invalid 1203 * 1204 * @adev: amdgpu_device pointer 1205 * @vm: requested vm 1206 * @bo: amdgpu buffer object 1207 * 1208 * Mark @bo as invalid (cayman+). 1209 */ 1210 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, 1211 struct amdgpu_bo *bo) 1212 { 1213 struct amdgpu_bo_va *bo_va; 1214 1215 list_for_each_entry(bo_va, &bo->va, bo_list) { 1216 spin_lock(&bo_va->vm->status_lock); 1217 if (list_empty(&bo_va->vm_status)) 1218 list_add(&bo_va->vm_status, &bo_va->vm->invalidated); 1219 spin_unlock(&bo_va->vm->status_lock); 1220 } 1221 } 1222 1223 /** 1224 * amdgpu_vm_init - initialize a vm instance 1225 * 1226 * @adev: amdgpu_device pointer 1227 * @vm: requested vm 1228 * 1229 * Init @vm fields (cayman+). 1230 */ 1231 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm) 1232 { 1233 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE, 1234 AMDGPU_VM_PTE_COUNT * 8); 1235 unsigned pd_size, pd_entries, pts_size; 1236 int i, r; 1237 1238 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 1239 vm->ids[i].id = 0; 1240 vm->ids[i].flushed_updates = NULL; 1241 vm->ids[i].last_id_use = NULL; 1242 } 1243 mutex_init(&vm->mutex); 1244 vm->va = RB_ROOT; 1245 spin_lock_init(&vm->status_lock); 1246 INIT_LIST_HEAD(&vm->invalidated); 1247 INIT_LIST_HEAD(&vm->cleared); 1248 INIT_LIST_HEAD(&vm->freed); 1249 1250 pd_size = amdgpu_vm_directory_size(adev); 1251 pd_entries = amdgpu_vm_num_pdes(adev); 1252 1253 /* allocate page table array */ 1254 pts_size = pd_entries * sizeof(struct amdgpu_vm_pt); 1255 vm->page_tables = kzalloc(pts_size, GFP_KERNEL); 1256 if (vm->page_tables == NULL) { 1257 DRM_ERROR("Cannot allocate memory for page table array\n"); 1258 return -ENOMEM; 1259 } 1260 1261 vm->page_directory_fence = NULL; 1262 1263 r = amdgpu_bo_create(adev, pd_size, align, true, 1264 AMDGPU_GEM_DOMAIN_VRAM, 1265 AMDGPU_GEM_CREATE_NO_CPU_ACCESS, 1266 NULL, NULL, &vm->page_directory); 1267 if (r) 1268 return r; 1269 1270 r = amdgpu_vm_clear_bo(adev, vm->page_directory); 1271 if (r) { 1272 amdgpu_bo_unref(&vm->page_directory); 1273 vm->page_directory = NULL; 1274 return r; 1275 } 1276 1277 return 0; 1278 } 1279 1280 /** 1281 * amdgpu_vm_fini - tear down a vm instance 1282 * 1283 * @adev: amdgpu_device pointer 1284 * @vm: requested vm 1285 * 1286 * Tear down @vm (cayman+). 1287 * Unbind the VM and remove all bos from the vm bo list 1288 */ 1289 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) 1290 { 1291 struct amdgpu_bo_va_mapping *mapping, *tmp; 1292 int i; 1293 1294 if (!RB_EMPTY_ROOT(&vm->va)) { 1295 dev_err(adev->dev, "still active bo inside vm\n"); 1296 } 1297 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) { 1298 list_del(&mapping->list); 1299 interval_tree_remove(&mapping->it, &vm->va); 1300 kfree(mapping); 1301 } 1302 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { 1303 list_del(&mapping->list); 1304 kfree(mapping); 1305 } 1306 1307 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++) 1308 amdgpu_bo_unref(&vm->page_tables[i].bo); 1309 kfree(vm->page_tables); 1310 1311 amdgpu_bo_unref(&vm->page_directory); 1312 fence_put(vm->page_directory_fence); 1313 1314 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 1315 fence_put(vm->ids[i].flushed_updates); 1316 amdgpu_fence_unref(&vm->ids[i].last_id_use); 1317 } 1318 1319 mutex_destroy(&vm->mutex); 1320 } 1321