1 /* 2 * Copyright 2008 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 "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * 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 * PRECISION INSIGHT AND/OR ITS SUPPLIERS 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 OTHER 22 * DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: 25 * Jerome Glisse <glisse@freedesktop.org> 26 */ 27 #include <linux/pagemap.h> 28 #include <drm/drmP.h> 29 #include <drm/amdgpu_drm.h> 30 #include "amdgpu.h" 31 #include "amdgpu_trace.h" 32 33 int amdgpu_cs_get_ring(struct amdgpu_device *adev, u32 ip_type, 34 u32 ip_instance, u32 ring, 35 struct amdgpu_ring **out_ring) 36 { 37 /* Right now all IPs have only one instance - multiple rings. */ 38 if (ip_instance != 0) { 39 DRM_ERROR("invalid ip instance: %d\n", ip_instance); 40 return -EINVAL; 41 } 42 43 switch (ip_type) { 44 default: 45 DRM_ERROR("unknown ip type: %d\n", ip_type); 46 return -EINVAL; 47 case AMDGPU_HW_IP_GFX: 48 if (ring < adev->gfx.num_gfx_rings) { 49 *out_ring = &adev->gfx.gfx_ring[ring]; 50 } else { 51 DRM_ERROR("only %d gfx rings are supported now\n", 52 adev->gfx.num_gfx_rings); 53 return -EINVAL; 54 } 55 break; 56 case AMDGPU_HW_IP_COMPUTE: 57 if (ring < adev->gfx.num_compute_rings) { 58 *out_ring = &adev->gfx.compute_ring[ring]; 59 } else { 60 DRM_ERROR("only %d compute rings are supported now\n", 61 adev->gfx.num_compute_rings); 62 return -EINVAL; 63 } 64 break; 65 case AMDGPU_HW_IP_DMA: 66 if (ring < adev->sdma.num_instances) { 67 *out_ring = &adev->sdma.instance[ring].ring; 68 } else { 69 DRM_ERROR("only %d SDMA rings are supported\n", 70 adev->sdma.num_instances); 71 return -EINVAL; 72 } 73 break; 74 case AMDGPU_HW_IP_UVD: 75 *out_ring = &adev->uvd.ring; 76 break; 77 case AMDGPU_HW_IP_VCE: 78 if (ring < adev->vce.num_rings){ 79 *out_ring = &adev->vce.ring[ring]; 80 } else { 81 DRM_ERROR("only %d VCE rings are supported\n", adev->vce.num_rings); 82 return -EINVAL; 83 } 84 break; 85 case AMDGPU_HW_IP_UVD_ENC: 86 if (ring < adev->uvd.num_enc_rings){ 87 *out_ring = &adev->uvd.ring_enc[ring]; 88 } else { 89 DRM_ERROR("only %d UVD ENC rings are supported\n", 90 adev->uvd.num_enc_rings); 91 return -EINVAL; 92 } 93 break; 94 case AMDGPU_HW_IP_VCN_DEC: 95 *out_ring = &adev->vcn.ring_dec; 96 break; 97 case AMDGPU_HW_IP_VCN_ENC: 98 if (ring < adev->vcn.num_enc_rings){ 99 *out_ring = &adev->vcn.ring_enc[ring]; 100 } else { 101 DRM_ERROR("only %d VCN ENC rings are supported\n", 102 adev->vcn.num_enc_rings); 103 return -EINVAL; 104 } 105 break; 106 } 107 108 if (!(*out_ring && (*out_ring)->adev)) { 109 DRM_ERROR("Ring %d is not initialized on IP %d\n", 110 ring, ip_type); 111 return -EINVAL; 112 } 113 114 return 0; 115 } 116 117 static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p, 118 struct drm_amdgpu_cs_chunk_fence *data, 119 uint32_t *offset) 120 { 121 struct drm_gem_object *gobj; 122 unsigned long size; 123 124 gobj = drm_gem_object_lookup(p->filp, data->handle); 125 if (gobj == NULL) 126 return -EINVAL; 127 128 p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj)); 129 p->uf_entry.priority = 0; 130 p->uf_entry.tv.bo = &p->uf_entry.robj->tbo; 131 p->uf_entry.tv.shared = true; 132 p->uf_entry.user_pages = NULL; 133 134 size = amdgpu_bo_size(p->uf_entry.robj); 135 if (size != PAGE_SIZE || (data->offset + 8) > size) 136 return -EINVAL; 137 138 *offset = data->offset; 139 140 drm_gem_object_unreference_unlocked(gobj); 141 142 if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) { 143 amdgpu_bo_unref(&p->uf_entry.robj); 144 return -EINVAL; 145 } 146 147 return 0; 148 } 149 150 int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, void *data) 151 { 152 struct amdgpu_fpriv *fpriv = p->filp->driver_priv; 153 struct amdgpu_vm *vm = &fpriv->vm; 154 union drm_amdgpu_cs *cs = data; 155 uint64_t *chunk_array_user; 156 uint64_t *chunk_array; 157 unsigned size, num_ibs = 0; 158 uint32_t uf_offset = 0; 159 int i; 160 int ret; 161 162 if (cs->in.num_chunks == 0) 163 return 0; 164 165 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL); 166 if (!chunk_array) 167 return -ENOMEM; 168 169 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id); 170 if (!p->ctx) { 171 ret = -EINVAL; 172 goto free_chunk; 173 } 174 175 /* get chunks */ 176 chunk_array_user = (uint64_t __user *)(uintptr_t)(cs->in.chunks); 177 if (copy_from_user(chunk_array, chunk_array_user, 178 sizeof(uint64_t)*cs->in.num_chunks)) { 179 ret = -EFAULT; 180 goto put_ctx; 181 } 182 183 p->nchunks = cs->in.num_chunks; 184 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk), 185 GFP_KERNEL); 186 if (!p->chunks) { 187 ret = -ENOMEM; 188 goto put_ctx; 189 } 190 191 for (i = 0; i < p->nchunks; i++) { 192 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL; 193 struct drm_amdgpu_cs_chunk user_chunk; 194 uint32_t __user *cdata; 195 196 chunk_ptr = (void __user *)(uintptr_t)chunk_array[i]; 197 if (copy_from_user(&user_chunk, chunk_ptr, 198 sizeof(struct drm_amdgpu_cs_chunk))) { 199 ret = -EFAULT; 200 i--; 201 goto free_partial_kdata; 202 } 203 p->chunks[i].chunk_id = user_chunk.chunk_id; 204 p->chunks[i].length_dw = user_chunk.length_dw; 205 206 size = p->chunks[i].length_dw; 207 cdata = (void __user *)(uintptr_t)user_chunk.chunk_data; 208 209 p->chunks[i].kdata = drm_malloc_ab(size, sizeof(uint32_t)); 210 if (p->chunks[i].kdata == NULL) { 211 ret = -ENOMEM; 212 i--; 213 goto free_partial_kdata; 214 } 215 size *= sizeof(uint32_t); 216 if (copy_from_user(p->chunks[i].kdata, cdata, size)) { 217 ret = -EFAULT; 218 goto free_partial_kdata; 219 } 220 221 switch (p->chunks[i].chunk_id) { 222 case AMDGPU_CHUNK_ID_IB: 223 ++num_ibs; 224 break; 225 226 case AMDGPU_CHUNK_ID_FENCE: 227 size = sizeof(struct drm_amdgpu_cs_chunk_fence); 228 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) { 229 ret = -EINVAL; 230 goto free_partial_kdata; 231 } 232 233 ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata, 234 &uf_offset); 235 if (ret) 236 goto free_partial_kdata; 237 238 break; 239 240 case AMDGPU_CHUNK_ID_DEPENDENCIES: 241 break; 242 243 default: 244 ret = -EINVAL; 245 goto free_partial_kdata; 246 } 247 } 248 249 ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm); 250 if (ret) 251 goto free_all_kdata; 252 253 if (p->uf_entry.robj) 254 p->job->uf_addr = uf_offset; 255 kfree(chunk_array); 256 return 0; 257 258 free_all_kdata: 259 i = p->nchunks - 1; 260 free_partial_kdata: 261 for (; i >= 0; i--) 262 drm_free_large(p->chunks[i].kdata); 263 kfree(p->chunks); 264 p->chunks = NULL; 265 p->nchunks = 0; 266 put_ctx: 267 amdgpu_ctx_put(p->ctx); 268 free_chunk: 269 kfree(chunk_array); 270 271 return ret; 272 } 273 274 /* Convert microseconds to bytes. */ 275 static u64 us_to_bytes(struct amdgpu_device *adev, s64 us) 276 { 277 if (us <= 0 || !adev->mm_stats.log2_max_MBps) 278 return 0; 279 280 /* Since accum_us is incremented by a million per second, just 281 * multiply it by the number of MB/s to get the number of bytes. 282 */ 283 return us << adev->mm_stats.log2_max_MBps; 284 } 285 286 static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes) 287 { 288 if (!adev->mm_stats.log2_max_MBps) 289 return 0; 290 291 return bytes >> adev->mm_stats.log2_max_MBps; 292 } 293 294 /* Returns how many bytes TTM can move right now. If no bytes can be moved, 295 * it returns 0. If it returns non-zero, it's OK to move at least one buffer, 296 * which means it can go over the threshold once. If that happens, the driver 297 * will be in debt and no other buffer migrations can be done until that debt 298 * is repaid. 299 * 300 * This approach allows moving a buffer of any size (it's important to allow 301 * that). 302 * 303 * The currency is simply time in microseconds and it increases as the clock 304 * ticks. The accumulated microseconds (us) are converted to bytes and 305 * returned. 306 */ 307 static u64 amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev) 308 { 309 s64 time_us, increment_us; 310 u64 max_bytes; 311 u64 free_vram, total_vram, used_vram; 312 313 /* Allow a maximum of 200 accumulated ms. This is basically per-IB 314 * throttling. 315 * 316 * It means that in order to get full max MBps, at least 5 IBs per 317 * second must be submitted and not more than 200ms apart from each 318 * other. 319 */ 320 const s64 us_upper_bound = 200000; 321 322 if (!adev->mm_stats.log2_max_MBps) 323 return 0; 324 325 total_vram = adev->mc.real_vram_size - adev->vram_pin_size; 326 used_vram = atomic64_read(&adev->vram_usage); 327 free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram; 328 329 spin_lock(&adev->mm_stats.lock); 330 331 /* Increase the amount of accumulated us. */ 332 time_us = ktime_to_us(ktime_get()); 333 increment_us = time_us - adev->mm_stats.last_update_us; 334 adev->mm_stats.last_update_us = time_us; 335 adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us, 336 us_upper_bound); 337 338 /* This prevents the short period of low performance when the VRAM 339 * usage is low and the driver is in debt or doesn't have enough 340 * accumulated us to fill VRAM quickly. 341 * 342 * The situation can occur in these cases: 343 * - a lot of VRAM is freed by userspace 344 * - the presence of a big buffer causes a lot of evictions 345 * (solution: split buffers into smaller ones) 346 * 347 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting 348 * accum_us to a positive number. 349 */ 350 if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) { 351 s64 min_us; 352 353 /* Be more aggresive on dGPUs. Try to fill a portion of free 354 * VRAM now. 355 */ 356 if (!(adev->flags & AMD_IS_APU)) 357 min_us = bytes_to_us(adev, free_vram / 4); 358 else 359 min_us = 0; /* Reset accum_us on APUs. */ 360 361 adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us); 362 } 363 364 /* This returns 0 if the driver is in debt to disallow (optional) 365 * buffer moves. 366 */ 367 max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us); 368 369 spin_unlock(&adev->mm_stats.lock); 370 return max_bytes; 371 } 372 373 /* Report how many bytes have really been moved for the last command 374 * submission. This can result in a debt that can stop buffer migrations 375 * temporarily. 376 */ 377 void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes) 378 { 379 spin_lock(&adev->mm_stats.lock); 380 adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes); 381 spin_unlock(&adev->mm_stats.lock); 382 } 383 384 static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p, 385 struct amdgpu_bo *bo) 386 { 387 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 388 u64 initial_bytes_moved; 389 uint32_t domain; 390 int r; 391 392 if (bo->pin_count) 393 return 0; 394 395 /* Don't move this buffer if we have depleted our allowance 396 * to move it. Don't move anything if the threshold is zero. 397 */ 398 if (p->bytes_moved < p->bytes_moved_threshold) 399 domain = bo->prefered_domains; 400 else 401 domain = bo->allowed_domains; 402 403 retry: 404 amdgpu_ttm_placement_from_domain(bo, domain); 405 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved); 406 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false); 407 p->bytes_moved += atomic64_read(&adev->num_bytes_moved) - 408 initial_bytes_moved; 409 410 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) { 411 domain = bo->allowed_domains; 412 goto retry; 413 } 414 415 return r; 416 } 417 418 /* Last resort, try to evict something from the current working set */ 419 static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p, 420 struct amdgpu_bo *validated) 421 { 422 uint32_t domain = validated->allowed_domains; 423 int r; 424 425 if (!p->evictable) 426 return false; 427 428 for (;&p->evictable->tv.head != &p->validated; 429 p->evictable = list_prev_entry(p->evictable, tv.head)) { 430 431 struct amdgpu_bo_list_entry *candidate = p->evictable; 432 struct amdgpu_bo *bo = candidate->robj; 433 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); 434 u64 initial_bytes_moved; 435 uint32_t other; 436 437 /* If we reached our current BO we can forget it */ 438 if (candidate->robj == validated) 439 break; 440 441 other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type); 442 443 /* Check if this BO is in one of the domains we need space for */ 444 if (!(other & domain)) 445 continue; 446 447 /* Check if we can move this BO somewhere else */ 448 other = bo->allowed_domains & ~domain; 449 if (!other) 450 continue; 451 452 /* Good we can try to move this BO somewhere else */ 453 amdgpu_ttm_placement_from_domain(bo, other); 454 initial_bytes_moved = atomic64_read(&adev->num_bytes_moved); 455 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false); 456 p->bytes_moved += atomic64_read(&adev->num_bytes_moved) - 457 initial_bytes_moved; 458 459 if (unlikely(r)) 460 break; 461 462 p->evictable = list_prev_entry(p->evictable, tv.head); 463 list_move(&candidate->tv.head, &p->validated); 464 465 return true; 466 } 467 468 return false; 469 } 470 471 static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo) 472 { 473 struct amdgpu_cs_parser *p = param; 474 int r; 475 476 do { 477 r = amdgpu_cs_bo_validate(p, bo); 478 } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo)); 479 if (r) 480 return r; 481 482 if (bo->shadow) 483 r = amdgpu_cs_bo_validate(p, bo->shadow); 484 485 return r; 486 } 487 488 static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p, 489 struct list_head *validated) 490 { 491 struct amdgpu_bo_list_entry *lobj; 492 int r; 493 494 list_for_each_entry(lobj, validated, tv.head) { 495 struct amdgpu_bo *bo = lobj->robj; 496 bool binding_userptr = false; 497 struct mm_struct *usermm; 498 499 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm); 500 if (usermm && usermm != current->mm) 501 return -EPERM; 502 503 /* Check if we have user pages and nobody bound the BO already */ 504 if (lobj->user_pages && bo->tbo.ttm->state != tt_bound) { 505 size_t size = sizeof(struct page *); 506 507 size *= bo->tbo.ttm->num_pages; 508 memcpy(bo->tbo.ttm->pages, lobj->user_pages, size); 509 binding_userptr = true; 510 } 511 512 if (p->evictable == lobj) 513 p->evictable = NULL; 514 515 r = amdgpu_cs_validate(p, bo); 516 if (r) 517 return r; 518 519 if (binding_userptr) { 520 drm_free_large(lobj->user_pages); 521 lobj->user_pages = NULL; 522 } 523 } 524 return 0; 525 } 526 527 static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p, 528 union drm_amdgpu_cs *cs) 529 { 530 struct amdgpu_fpriv *fpriv = p->filp->driver_priv; 531 struct amdgpu_bo_list_entry *e; 532 struct list_head duplicates; 533 bool need_mmap_lock = false; 534 unsigned i, tries = 10; 535 int r; 536 537 INIT_LIST_HEAD(&p->validated); 538 539 p->bo_list = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle); 540 if (p->bo_list) { 541 need_mmap_lock = p->bo_list->first_userptr != 542 p->bo_list->num_entries; 543 amdgpu_bo_list_get_list(p->bo_list, &p->validated); 544 } 545 546 INIT_LIST_HEAD(&duplicates); 547 amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd); 548 549 if (p->uf_entry.robj) 550 list_add(&p->uf_entry.tv.head, &p->validated); 551 552 if (need_mmap_lock) 553 down_read(¤t->mm->mmap_sem); 554 555 while (1) { 556 struct list_head need_pages; 557 unsigned i; 558 559 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true, 560 &duplicates); 561 if (unlikely(r != 0)) { 562 if (r != -ERESTARTSYS) 563 DRM_ERROR("ttm_eu_reserve_buffers failed.\n"); 564 goto error_free_pages; 565 } 566 567 /* Without a BO list we don't have userptr BOs */ 568 if (!p->bo_list) 569 break; 570 571 INIT_LIST_HEAD(&need_pages); 572 for (i = p->bo_list->first_userptr; 573 i < p->bo_list->num_entries; ++i) { 574 575 e = &p->bo_list->array[i]; 576 577 if (amdgpu_ttm_tt_userptr_invalidated(e->robj->tbo.ttm, 578 &e->user_invalidated) && e->user_pages) { 579 580 /* We acquired a page array, but somebody 581 * invalidated it. Free it an try again 582 */ 583 release_pages(e->user_pages, 584 e->robj->tbo.ttm->num_pages, 585 false); 586 drm_free_large(e->user_pages); 587 e->user_pages = NULL; 588 } 589 590 if (e->robj->tbo.ttm->state != tt_bound && 591 !e->user_pages) { 592 list_del(&e->tv.head); 593 list_add(&e->tv.head, &need_pages); 594 595 amdgpu_bo_unreserve(e->robj); 596 } 597 } 598 599 if (list_empty(&need_pages)) 600 break; 601 602 /* Unreserve everything again. */ 603 ttm_eu_backoff_reservation(&p->ticket, &p->validated); 604 605 /* We tried too many times, just abort */ 606 if (!--tries) { 607 r = -EDEADLK; 608 DRM_ERROR("deadlock in %s\n", __func__); 609 goto error_free_pages; 610 } 611 612 /* Fill the page arrays for all useptrs. */ 613 list_for_each_entry(e, &need_pages, tv.head) { 614 struct ttm_tt *ttm = e->robj->tbo.ttm; 615 616 e->user_pages = drm_calloc_large(ttm->num_pages, 617 sizeof(struct page*)); 618 if (!e->user_pages) { 619 r = -ENOMEM; 620 DRM_ERROR("calloc failure in %s\n", __func__); 621 goto error_free_pages; 622 } 623 624 r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages); 625 if (r) { 626 DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n"); 627 drm_free_large(e->user_pages); 628 e->user_pages = NULL; 629 goto error_free_pages; 630 } 631 } 632 633 /* And try again. */ 634 list_splice(&need_pages, &p->validated); 635 } 636 637 p->bytes_moved_threshold = amdgpu_cs_get_threshold_for_moves(p->adev); 638 p->bytes_moved = 0; 639 p->evictable = list_last_entry(&p->validated, 640 struct amdgpu_bo_list_entry, 641 tv.head); 642 643 r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm, 644 amdgpu_cs_validate, p); 645 if (r) { 646 DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n"); 647 goto error_validate; 648 } 649 650 r = amdgpu_cs_list_validate(p, &duplicates); 651 if (r) { 652 DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n"); 653 goto error_validate; 654 } 655 656 r = amdgpu_cs_list_validate(p, &p->validated); 657 if (r) { 658 DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n"); 659 goto error_validate; 660 } 661 662 amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved); 663 664 fpriv->vm.last_eviction_counter = 665 atomic64_read(&p->adev->num_evictions); 666 667 if (p->bo_list) { 668 struct amdgpu_bo *gds = p->bo_list->gds_obj; 669 struct amdgpu_bo *gws = p->bo_list->gws_obj; 670 struct amdgpu_bo *oa = p->bo_list->oa_obj; 671 struct amdgpu_vm *vm = &fpriv->vm; 672 unsigned i; 673 674 for (i = 0; i < p->bo_list->num_entries; i++) { 675 struct amdgpu_bo *bo = p->bo_list->array[i].robj; 676 677 p->bo_list->array[i].bo_va = amdgpu_vm_bo_find(vm, bo); 678 } 679 680 if (gds) { 681 p->job->gds_base = amdgpu_bo_gpu_offset(gds); 682 p->job->gds_size = amdgpu_bo_size(gds); 683 } 684 if (gws) { 685 p->job->gws_base = amdgpu_bo_gpu_offset(gws); 686 p->job->gws_size = amdgpu_bo_size(gws); 687 } 688 if (oa) { 689 p->job->oa_base = amdgpu_bo_gpu_offset(oa); 690 p->job->oa_size = amdgpu_bo_size(oa); 691 } 692 } 693 694 if (!r && p->uf_entry.robj) { 695 struct amdgpu_bo *uf = p->uf_entry.robj; 696 697 r = amdgpu_ttm_bind(&uf->tbo, &uf->tbo.mem); 698 p->job->uf_addr += amdgpu_bo_gpu_offset(uf); 699 } 700 701 error_validate: 702 if (r) { 703 amdgpu_vm_move_pt_bos_in_lru(p->adev, &fpriv->vm); 704 ttm_eu_backoff_reservation(&p->ticket, &p->validated); 705 } 706 707 error_free_pages: 708 709 if (need_mmap_lock) 710 up_read(¤t->mm->mmap_sem); 711 712 if (p->bo_list) { 713 for (i = p->bo_list->first_userptr; 714 i < p->bo_list->num_entries; ++i) { 715 e = &p->bo_list->array[i]; 716 717 if (!e->user_pages) 718 continue; 719 720 release_pages(e->user_pages, 721 e->robj->tbo.ttm->num_pages, 722 false); 723 drm_free_large(e->user_pages); 724 } 725 } 726 727 return r; 728 } 729 730 static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p) 731 { 732 struct amdgpu_bo_list_entry *e; 733 int r; 734 735 list_for_each_entry(e, &p->validated, tv.head) { 736 struct reservation_object *resv = e->robj->tbo.resv; 737 r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp); 738 739 if (r) 740 return r; 741 } 742 return 0; 743 } 744 745 /** 746 * cs_parser_fini() - clean parser states 747 * @parser: parser structure holding parsing context. 748 * @error: error number 749 * 750 * If error is set than unvalidate buffer, otherwise just free memory 751 * used by parsing context. 752 **/ 753 static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error, bool backoff) 754 { 755 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv; 756 unsigned i; 757 758 if (!error) { 759 amdgpu_vm_move_pt_bos_in_lru(parser->adev, &fpriv->vm); 760 761 ttm_eu_fence_buffer_objects(&parser->ticket, 762 &parser->validated, 763 parser->fence); 764 } else if (backoff) { 765 ttm_eu_backoff_reservation(&parser->ticket, 766 &parser->validated); 767 } 768 dma_fence_put(parser->fence); 769 770 if (parser->ctx) 771 amdgpu_ctx_put(parser->ctx); 772 if (parser->bo_list) 773 amdgpu_bo_list_put(parser->bo_list); 774 775 for (i = 0; i < parser->nchunks; i++) 776 drm_free_large(parser->chunks[i].kdata); 777 kfree(parser->chunks); 778 if (parser->job) 779 amdgpu_job_free(parser->job); 780 amdgpu_bo_unref(&parser->uf_entry.robj); 781 } 782 783 static int amdgpu_bo_vm_update_pte(struct amdgpu_cs_parser *p) 784 { 785 struct amdgpu_device *adev = p->adev; 786 struct amdgpu_fpriv *fpriv = p->filp->driver_priv; 787 struct amdgpu_vm *vm = &fpriv->vm; 788 struct amdgpu_bo_va *bo_va; 789 struct amdgpu_bo *bo; 790 int i, r; 791 792 r = amdgpu_vm_update_directories(adev, vm); 793 if (r) 794 return r; 795 796 r = amdgpu_sync_fence(adev, &p->job->sync, vm->last_dir_update); 797 if (r) 798 return r; 799 800 r = amdgpu_vm_clear_freed(adev, vm, NULL); 801 if (r) 802 return r; 803 804 r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false); 805 if (r) 806 return r; 807 808 r = amdgpu_sync_fence(adev, &p->job->sync, 809 fpriv->prt_va->last_pt_update); 810 if (r) 811 return r; 812 813 if (amdgpu_sriov_vf(adev)) { 814 struct dma_fence *f; 815 bo_va = vm->csa_bo_va; 816 BUG_ON(!bo_va); 817 r = amdgpu_vm_bo_update(adev, bo_va, false); 818 if (r) 819 return r; 820 821 f = bo_va->last_pt_update; 822 r = amdgpu_sync_fence(adev, &p->job->sync, f); 823 if (r) 824 return r; 825 } 826 827 if (p->bo_list) { 828 for (i = 0; i < p->bo_list->num_entries; i++) { 829 struct dma_fence *f; 830 831 /* ignore duplicates */ 832 bo = p->bo_list->array[i].robj; 833 if (!bo) 834 continue; 835 836 bo_va = p->bo_list->array[i].bo_va; 837 if (bo_va == NULL) 838 continue; 839 840 r = amdgpu_vm_bo_update(adev, bo_va, false); 841 if (r) 842 return r; 843 844 f = bo_va->last_pt_update; 845 r = amdgpu_sync_fence(adev, &p->job->sync, f); 846 if (r) 847 return r; 848 } 849 850 } 851 852 r = amdgpu_vm_clear_invalids(adev, vm, &p->job->sync); 853 854 if (amdgpu_vm_debug && p->bo_list) { 855 /* Invalidate all BOs to test for userspace bugs */ 856 for (i = 0; i < p->bo_list->num_entries; i++) { 857 /* ignore duplicates */ 858 bo = p->bo_list->array[i].robj; 859 if (!bo) 860 continue; 861 862 amdgpu_vm_bo_invalidate(adev, bo); 863 } 864 } 865 866 return r; 867 } 868 869 static int amdgpu_cs_ib_vm_chunk(struct amdgpu_device *adev, 870 struct amdgpu_cs_parser *p) 871 { 872 struct amdgpu_fpriv *fpriv = p->filp->driver_priv; 873 struct amdgpu_vm *vm = &fpriv->vm; 874 struct amdgpu_ring *ring = p->job->ring; 875 int i, r; 876 877 /* Only for UVD/VCE VM emulation */ 878 if (ring->funcs->parse_cs) { 879 for (i = 0; i < p->job->num_ibs; i++) { 880 r = amdgpu_ring_parse_cs(ring, p, i); 881 if (r) 882 return r; 883 } 884 } 885 886 if (p->job->vm) { 887 p->job->vm_pd_addr = amdgpu_bo_gpu_offset(vm->root.bo); 888 889 r = amdgpu_bo_vm_update_pte(p); 890 if (r) 891 return r; 892 } 893 894 return amdgpu_cs_sync_rings(p); 895 } 896 897 static int amdgpu_cs_ib_fill(struct amdgpu_device *adev, 898 struct amdgpu_cs_parser *parser) 899 { 900 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv; 901 struct amdgpu_vm *vm = &fpriv->vm; 902 int i, j; 903 int r, ce_preempt = 0, de_preempt = 0; 904 905 for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) { 906 struct amdgpu_cs_chunk *chunk; 907 struct amdgpu_ib *ib; 908 struct drm_amdgpu_cs_chunk_ib *chunk_ib; 909 struct amdgpu_ring *ring; 910 911 chunk = &parser->chunks[i]; 912 ib = &parser->job->ibs[j]; 913 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata; 914 915 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB) 916 continue; 917 918 if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX && amdgpu_sriov_vf(adev)) { 919 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREEMPT) { 920 if (chunk_ib->flags & AMDGPU_IB_FLAG_CE) 921 ce_preempt++; 922 else 923 de_preempt++; 924 } 925 926 /* each GFX command submit allows 0 or 1 IB preemptible for CE & DE */ 927 if (ce_preempt > 1 || de_preempt > 1) 928 return -EINVAL; 929 } 930 931 r = amdgpu_cs_get_ring(adev, chunk_ib->ip_type, 932 chunk_ib->ip_instance, chunk_ib->ring, 933 &ring); 934 if (r) 935 return r; 936 937 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE) { 938 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT; 939 if (!parser->ctx->preamble_presented) { 940 parser->job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST; 941 parser->ctx->preamble_presented = true; 942 } 943 } 944 945 if (parser->job->ring && parser->job->ring != ring) 946 return -EINVAL; 947 948 parser->job->ring = ring; 949 950 if (ring->funcs->parse_cs) { 951 struct amdgpu_bo_va_mapping *m; 952 struct amdgpu_bo *aobj = NULL; 953 uint64_t offset; 954 uint8_t *kptr; 955 956 m = amdgpu_cs_find_mapping(parser, chunk_ib->va_start, 957 &aobj); 958 if (!aobj) { 959 DRM_ERROR("IB va_start is invalid\n"); 960 return -EINVAL; 961 } 962 963 if ((chunk_ib->va_start + chunk_ib->ib_bytes) > 964 (m->last + 1) * AMDGPU_GPU_PAGE_SIZE) { 965 DRM_ERROR("IB va_start+ib_bytes is invalid\n"); 966 return -EINVAL; 967 } 968 969 /* the IB should be reserved at this point */ 970 r = amdgpu_bo_kmap(aobj, (void **)&kptr); 971 if (r) { 972 return r; 973 } 974 975 offset = m->start * AMDGPU_GPU_PAGE_SIZE; 976 kptr += chunk_ib->va_start - offset; 977 978 r = amdgpu_ib_get(adev, vm, chunk_ib->ib_bytes, ib); 979 if (r) { 980 DRM_ERROR("Failed to get ib !\n"); 981 return r; 982 } 983 984 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes); 985 amdgpu_bo_kunmap(aobj); 986 } else { 987 r = amdgpu_ib_get(adev, vm, 0, ib); 988 if (r) { 989 DRM_ERROR("Failed to get ib !\n"); 990 return r; 991 } 992 993 } 994 995 ib->gpu_addr = chunk_ib->va_start; 996 ib->length_dw = chunk_ib->ib_bytes / 4; 997 ib->flags = chunk_ib->flags; 998 j++; 999 } 1000 1001 /* UVD & VCE fw doesn't support user fences */ 1002 if (parser->job->uf_addr && ( 1003 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_UVD || 1004 parser->job->ring->funcs->type == AMDGPU_RING_TYPE_VCE)) 1005 return -EINVAL; 1006 1007 return 0; 1008 } 1009 1010 static int amdgpu_cs_dependencies(struct amdgpu_device *adev, 1011 struct amdgpu_cs_parser *p) 1012 { 1013 struct amdgpu_fpriv *fpriv = p->filp->driver_priv; 1014 int i, j, r; 1015 1016 for (i = 0; i < p->nchunks; ++i) { 1017 struct drm_amdgpu_cs_chunk_dep *deps; 1018 struct amdgpu_cs_chunk *chunk; 1019 unsigned num_deps; 1020 1021 chunk = &p->chunks[i]; 1022 1023 if (chunk->chunk_id != AMDGPU_CHUNK_ID_DEPENDENCIES) 1024 continue; 1025 1026 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata; 1027 num_deps = chunk->length_dw * 4 / 1028 sizeof(struct drm_amdgpu_cs_chunk_dep); 1029 1030 for (j = 0; j < num_deps; ++j) { 1031 struct amdgpu_ring *ring; 1032 struct amdgpu_ctx *ctx; 1033 struct dma_fence *fence; 1034 1035 r = amdgpu_cs_get_ring(adev, deps[j].ip_type, 1036 deps[j].ip_instance, 1037 deps[j].ring, &ring); 1038 if (r) 1039 return r; 1040 1041 ctx = amdgpu_ctx_get(fpriv, deps[j].ctx_id); 1042 if (ctx == NULL) 1043 return -EINVAL; 1044 1045 fence = amdgpu_ctx_get_fence(ctx, ring, 1046 deps[j].handle); 1047 if (IS_ERR(fence)) { 1048 r = PTR_ERR(fence); 1049 amdgpu_ctx_put(ctx); 1050 return r; 1051 1052 } else if (fence) { 1053 r = amdgpu_sync_fence(adev, &p->job->sync, 1054 fence); 1055 dma_fence_put(fence); 1056 amdgpu_ctx_put(ctx); 1057 if (r) 1058 return r; 1059 } 1060 } 1061 } 1062 1063 return 0; 1064 } 1065 1066 static int amdgpu_cs_submit(struct amdgpu_cs_parser *p, 1067 union drm_amdgpu_cs *cs) 1068 { 1069 struct amdgpu_ring *ring = p->job->ring; 1070 struct amd_sched_entity *entity = &p->ctx->rings[ring->idx].entity; 1071 struct amdgpu_job *job; 1072 int r; 1073 1074 job = p->job; 1075 p->job = NULL; 1076 1077 r = amd_sched_job_init(&job->base, &ring->sched, entity, p->filp); 1078 if (r) { 1079 amdgpu_job_free(job); 1080 return r; 1081 } 1082 1083 job->owner = p->filp; 1084 job->fence_ctx = entity->fence_context; 1085 p->fence = dma_fence_get(&job->base.s_fence->finished); 1086 cs->out.handle = amdgpu_ctx_add_fence(p->ctx, ring, p->fence); 1087 job->uf_sequence = cs->out.handle; 1088 amdgpu_job_free_resources(job); 1089 amdgpu_cs_parser_fini(p, 0, true); 1090 1091 trace_amdgpu_cs_ioctl(job); 1092 amd_sched_entity_push_job(&job->base); 1093 1094 return 0; 1095 } 1096 1097 int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) 1098 { 1099 struct amdgpu_device *adev = dev->dev_private; 1100 union drm_amdgpu_cs *cs = data; 1101 struct amdgpu_cs_parser parser = {}; 1102 bool reserved_buffers = false; 1103 int i, r; 1104 1105 if (!adev->accel_working) 1106 return -EBUSY; 1107 1108 parser.adev = adev; 1109 parser.filp = filp; 1110 1111 r = amdgpu_cs_parser_init(&parser, data); 1112 if (r) { 1113 DRM_ERROR("Failed to initialize parser !\n"); 1114 goto out; 1115 } 1116 1117 r = amdgpu_cs_parser_bos(&parser, data); 1118 if (r) { 1119 if (r == -ENOMEM) 1120 DRM_ERROR("Not enough memory for command submission!\n"); 1121 else if (r != -ERESTARTSYS) 1122 DRM_ERROR("Failed to process the buffer list %d!\n", r); 1123 goto out; 1124 } 1125 1126 reserved_buffers = true; 1127 r = amdgpu_cs_ib_fill(adev, &parser); 1128 if (r) 1129 goto out; 1130 1131 r = amdgpu_cs_dependencies(adev, &parser); 1132 if (r) { 1133 DRM_ERROR("Failed in the dependencies handling %d!\n", r); 1134 goto out; 1135 } 1136 1137 for (i = 0; i < parser.job->num_ibs; i++) 1138 trace_amdgpu_cs(&parser, i); 1139 1140 r = amdgpu_cs_ib_vm_chunk(adev, &parser); 1141 if (r) 1142 goto out; 1143 1144 r = amdgpu_cs_submit(&parser, cs); 1145 if (r) 1146 goto out; 1147 1148 return 0; 1149 out: 1150 amdgpu_cs_parser_fini(&parser, r, reserved_buffers); 1151 return r; 1152 } 1153 1154 /** 1155 * amdgpu_cs_wait_ioctl - wait for a command submission to finish 1156 * 1157 * @dev: drm device 1158 * @data: data from userspace 1159 * @filp: file private 1160 * 1161 * Wait for the command submission identified by handle to finish. 1162 */ 1163 int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data, 1164 struct drm_file *filp) 1165 { 1166 union drm_amdgpu_wait_cs *wait = data; 1167 struct amdgpu_device *adev = dev->dev_private; 1168 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout); 1169 struct amdgpu_ring *ring = NULL; 1170 struct amdgpu_ctx *ctx; 1171 struct dma_fence *fence; 1172 long r; 1173 1174 r = amdgpu_cs_get_ring(adev, wait->in.ip_type, wait->in.ip_instance, 1175 wait->in.ring, &ring); 1176 if (r) 1177 return r; 1178 1179 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id); 1180 if (ctx == NULL) 1181 return -EINVAL; 1182 1183 fence = amdgpu_ctx_get_fence(ctx, ring, wait->in.handle); 1184 if (IS_ERR(fence)) 1185 r = PTR_ERR(fence); 1186 else if (fence) { 1187 r = dma_fence_wait_timeout(fence, true, timeout); 1188 dma_fence_put(fence); 1189 } else 1190 r = 1; 1191 1192 amdgpu_ctx_put(ctx); 1193 if (r < 0) 1194 return r; 1195 1196 memset(wait, 0, sizeof(*wait)); 1197 wait->out.status = (r == 0); 1198 1199 return 0; 1200 } 1201 1202 /** 1203 * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence 1204 * 1205 * @adev: amdgpu device 1206 * @filp: file private 1207 * @user: drm_amdgpu_fence copied from user space 1208 */ 1209 static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev, 1210 struct drm_file *filp, 1211 struct drm_amdgpu_fence *user) 1212 { 1213 struct amdgpu_ring *ring; 1214 struct amdgpu_ctx *ctx; 1215 struct dma_fence *fence; 1216 int r; 1217 1218 r = amdgpu_cs_get_ring(adev, user->ip_type, user->ip_instance, 1219 user->ring, &ring); 1220 if (r) 1221 return ERR_PTR(r); 1222 1223 ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id); 1224 if (ctx == NULL) 1225 return ERR_PTR(-EINVAL); 1226 1227 fence = amdgpu_ctx_get_fence(ctx, ring, user->seq_no); 1228 amdgpu_ctx_put(ctx); 1229 1230 return fence; 1231 } 1232 1233 /** 1234 * amdgpu_cs_wait_all_fence - wait on all fences to signal 1235 * 1236 * @adev: amdgpu device 1237 * @filp: file private 1238 * @wait: wait parameters 1239 * @fences: array of drm_amdgpu_fence 1240 */ 1241 static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev, 1242 struct drm_file *filp, 1243 union drm_amdgpu_wait_fences *wait, 1244 struct drm_amdgpu_fence *fences) 1245 { 1246 uint32_t fence_count = wait->in.fence_count; 1247 unsigned int i; 1248 long r = 1; 1249 1250 for (i = 0; i < fence_count; i++) { 1251 struct dma_fence *fence; 1252 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns); 1253 1254 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]); 1255 if (IS_ERR(fence)) 1256 return PTR_ERR(fence); 1257 else if (!fence) 1258 continue; 1259 1260 r = dma_fence_wait_timeout(fence, true, timeout); 1261 dma_fence_put(fence); 1262 if (r < 0) 1263 return r; 1264 1265 if (r == 0) 1266 break; 1267 } 1268 1269 memset(wait, 0, sizeof(*wait)); 1270 wait->out.status = (r > 0); 1271 1272 return 0; 1273 } 1274 1275 /** 1276 * amdgpu_cs_wait_any_fence - wait on any fence to signal 1277 * 1278 * @adev: amdgpu device 1279 * @filp: file private 1280 * @wait: wait parameters 1281 * @fences: array of drm_amdgpu_fence 1282 */ 1283 static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev, 1284 struct drm_file *filp, 1285 union drm_amdgpu_wait_fences *wait, 1286 struct drm_amdgpu_fence *fences) 1287 { 1288 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns); 1289 uint32_t fence_count = wait->in.fence_count; 1290 uint32_t first = ~0; 1291 struct dma_fence **array; 1292 unsigned int i; 1293 long r; 1294 1295 /* Prepare the fence array */ 1296 array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL); 1297 1298 if (array == NULL) 1299 return -ENOMEM; 1300 1301 for (i = 0; i < fence_count; i++) { 1302 struct dma_fence *fence; 1303 1304 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]); 1305 if (IS_ERR(fence)) { 1306 r = PTR_ERR(fence); 1307 goto err_free_fence_array; 1308 } else if (fence) { 1309 array[i] = fence; 1310 } else { /* NULL, the fence has been already signaled */ 1311 r = 1; 1312 goto out; 1313 } 1314 } 1315 1316 r = dma_fence_wait_any_timeout(array, fence_count, true, timeout, 1317 &first); 1318 if (r < 0) 1319 goto err_free_fence_array; 1320 1321 out: 1322 memset(wait, 0, sizeof(*wait)); 1323 wait->out.status = (r > 0); 1324 wait->out.first_signaled = first; 1325 /* set return value 0 to indicate success */ 1326 r = 0; 1327 1328 err_free_fence_array: 1329 for (i = 0; i < fence_count; i++) 1330 dma_fence_put(array[i]); 1331 kfree(array); 1332 1333 return r; 1334 } 1335 1336 /** 1337 * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish 1338 * 1339 * @dev: drm device 1340 * @data: data from userspace 1341 * @filp: file private 1342 */ 1343 int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data, 1344 struct drm_file *filp) 1345 { 1346 struct amdgpu_device *adev = dev->dev_private; 1347 union drm_amdgpu_wait_fences *wait = data; 1348 uint32_t fence_count = wait->in.fence_count; 1349 struct drm_amdgpu_fence *fences_user; 1350 struct drm_amdgpu_fence *fences; 1351 int r; 1352 1353 /* Get the fences from userspace */ 1354 fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence), 1355 GFP_KERNEL); 1356 if (fences == NULL) 1357 return -ENOMEM; 1358 1359 fences_user = (void __user *)(uintptr_t)(wait->in.fences); 1360 if (copy_from_user(fences, fences_user, 1361 sizeof(struct drm_amdgpu_fence) * fence_count)) { 1362 r = -EFAULT; 1363 goto err_free_fences; 1364 } 1365 1366 if (wait->in.wait_all) 1367 r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences); 1368 else 1369 r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences); 1370 1371 err_free_fences: 1372 kfree(fences); 1373 1374 return r; 1375 } 1376 1377 /** 1378 * amdgpu_cs_find_bo_va - find bo_va for VM address 1379 * 1380 * @parser: command submission parser context 1381 * @addr: VM address 1382 * @bo: resulting BO of the mapping found 1383 * 1384 * Search the buffer objects in the command submission context for a certain 1385 * virtual memory address. Returns allocation structure when found, NULL 1386 * otherwise. 1387 */ 1388 struct amdgpu_bo_va_mapping * 1389 amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser, 1390 uint64_t addr, struct amdgpu_bo **bo) 1391 { 1392 struct amdgpu_bo_va_mapping *mapping; 1393 unsigned i; 1394 1395 if (!parser->bo_list) 1396 return NULL; 1397 1398 addr /= AMDGPU_GPU_PAGE_SIZE; 1399 1400 for (i = 0; i < parser->bo_list->num_entries; i++) { 1401 struct amdgpu_bo_list_entry *lobj; 1402 1403 lobj = &parser->bo_list->array[i]; 1404 if (!lobj->bo_va) 1405 continue; 1406 1407 list_for_each_entry(mapping, &lobj->bo_va->valids, list) { 1408 if (mapping->start > addr || 1409 addr > mapping->last) 1410 continue; 1411 1412 *bo = lobj->bo_va->bo; 1413 return mapping; 1414 } 1415 1416 list_for_each_entry(mapping, &lobj->bo_va->invalids, list) { 1417 if (mapping->start > addr || 1418 addr > mapping->last) 1419 continue; 1420 1421 *bo = lobj->bo_va->bo; 1422 return mapping; 1423 } 1424 } 1425 1426 return NULL; 1427 } 1428 1429 /** 1430 * amdgpu_cs_sysvm_access_required - make BOs accessible by the system VM 1431 * 1432 * @parser: command submission parser context 1433 * 1434 * Helper for UVD/VCE VM emulation, make sure BOs are accessible by the system VM. 1435 */ 1436 int amdgpu_cs_sysvm_access_required(struct amdgpu_cs_parser *parser) 1437 { 1438 unsigned i; 1439 int r; 1440 1441 if (!parser->bo_list) 1442 return 0; 1443 1444 for (i = 0; i < parser->bo_list->num_entries; i++) { 1445 struct amdgpu_bo *bo = parser->bo_list->array[i].robj; 1446 1447 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem); 1448 if (unlikely(r)) 1449 return r; 1450 1451 if (bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS) 1452 continue; 1453 1454 bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS; 1455 amdgpu_ttm_placement_from_domain(bo, bo->allowed_domains); 1456 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false); 1457 if (unlikely(r)) 1458 return r; 1459 } 1460 1461 return 0; 1462 } 1463