2 * Copyright 2008 Jerome Glisse.
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:
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
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.
27 #include <linux/pagemap.h>
28 #include <linux/sync_file.h>
30 #include <drm/amdgpu_drm.h>
31 #include <drm/drm_syncobj.h>
33 #include "amdgpu_trace.h"
34 #include "amdgpu_gmc.h"
35 #include "amdgpu_gem.h"
37 static int amdgpu_cs_user_fence_chunk(struct amdgpu_cs_parser *p,
38 struct drm_amdgpu_cs_chunk_fence *data,
41 struct drm_gem_object *gobj;
46 gobj = drm_gem_object_lookup(p->filp, data->handle);
50 bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
51 p->uf_entry.priority = 0;
52 p->uf_entry.tv.bo = &bo->tbo;
53 /* One for TTM and one for the CS job */
54 p->uf_entry.tv.num_shared = 2;
55 p->uf_entry.user_pages = NULL;
57 drm_gem_object_put_unlocked(gobj);
59 size = amdgpu_bo_size(bo);
60 if (size != PAGE_SIZE || (data->offset + 8) > size) {
65 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) {
70 *offset = data->offset;
79 static int amdgpu_cs_bo_handles_chunk(struct amdgpu_cs_parser *p,
80 struct drm_amdgpu_bo_list_in *data)
83 struct drm_amdgpu_bo_list_entry *info = NULL;
85 r = amdgpu_bo_create_list_entry_array(data, &info);
89 r = amdgpu_bo_list_create(p->adev, p->filp, info, data->bo_number,
104 static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, union drm_amdgpu_cs *cs)
106 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
107 struct amdgpu_vm *vm = &fpriv->vm;
108 uint64_t *chunk_array_user;
109 uint64_t *chunk_array;
110 unsigned size, num_ibs = 0;
111 uint32_t uf_offset = 0;
115 if (cs->in.num_chunks == 0)
118 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
122 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
128 mutex_lock(&p->ctx->lock);
130 /* skip guilty context job */
131 if (atomic_read(&p->ctx->guilty) == 1) {
137 chunk_array_user = u64_to_user_ptr(cs->in.chunks);
138 if (copy_from_user(chunk_array, chunk_array_user,
139 sizeof(uint64_t)*cs->in.num_chunks)) {
144 p->nchunks = cs->in.num_chunks;
145 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
152 for (i = 0; i < p->nchunks; i++) {
153 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
154 struct drm_amdgpu_cs_chunk user_chunk;
155 uint32_t __user *cdata;
157 chunk_ptr = u64_to_user_ptr(chunk_array[i]);
158 if (copy_from_user(&user_chunk, chunk_ptr,
159 sizeof(struct drm_amdgpu_cs_chunk))) {
162 goto free_partial_kdata;
164 p->chunks[i].chunk_id = user_chunk.chunk_id;
165 p->chunks[i].length_dw = user_chunk.length_dw;
167 size = p->chunks[i].length_dw;
168 cdata = u64_to_user_ptr(user_chunk.chunk_data);
170 p->chunks[i].kdata = kvmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
171 if (p->chunks[i].kdata == NULL) {
174 goto free_partial_kdata;
176 size *= sizeof(uint32_t);
177 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
179 goto free_partial_kdata;
182 switch (p->chunks[i].chunk_id) {
183 case AMDGPU_CHUNK_ID_IB:
187 case AMDGPU_CHUNK_ID_FENCE:
188 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
189 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
191 goto free_partial_kdata;
194 ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
197 goto free_partial_kdata;
201 case AMDGPU_CHUNK_ID_BO_HANDLES:
202 size = sizeof(struct drm_amdgpu_bo_list_in);
203 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
205 goto free_partial_kdata;
208 ret = amdgpu_cs_bo_handles_chunk(p, p->chunks[i].kdata);
210 goto free_partial_kdata;
214 case AMDGPU_CHUNK_ID_DEPENDENCIES:
215 case AMDGPU_CHUNK_ID_SYNCOBJ_IN:
216 case AMDGPU_CHUNK_ID_SYNCOBJ_OUT:
217 case AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES:
218 case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_WAIT:
219 case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_SIGNAL:
224 goto free_partial_kdata;
228 ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
232 if (p->ctx->vram_lost_counter != p->job->vram_lost_counter) {
237 if (p->uf_entry.tv.bo)
238 p->job->uf_addr = uf_offset;
241 /* Use this opportunity to fill in task info for the vm */
242 amdgpu_vm_set_task_info(vm);
250 kvfree(p->chunks[i].kdata);
260 /* Convert microseconds to bytes. */
261 static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
263 if (us <= 0 || !adev->mm_stats.log2_max_MBps)
266 /* Since accum_us is incremented by a million per second, just
267 * multiply it by the number of MB/s to get the number of bytes.
269 return us << adev->mm_stats.log2_max_MBps;
272 static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
274 if (!adev->mm_stats.log2_max_MBps)
277 return bytes >> adev->mm_stats.log2_max_MBps;
280 /* Returns how many bytes TTM can move right now. If no bytes can be moved,
281 * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
282 * which means it can go over the threshold once. If that happens, the driver
283 * will be in debt and no other buffer migrations can be done until that debt
286 * This approach allows moving a buffer of any size (it's important to allow
289 * The currency is simply time in microseconds and it increases as the clock
290 * ticks. The accumulated microseconds (us) are converted to bytes and
293 static void amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev,
297 s64 time_us, increment_us;
298 u64 free_vram, total_vram, used_vram;
300 /* Allow a maximum of 200 accumulated ms. This is basically per-IB
303 * It means that in order to get full max MBps, at least 5 IBs per
304 * second must be submitted and not more than 200ms apart from each
307 const s64 us_upper_bound = 200000;
309 if (!adev->mm_stats.log2_max_MBps) {
315 total_vram = adev->gmc.real_vram_size - atomic64_read(&adev->vram_pin_size);
316 used_vram = amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
317 free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
319 spin_lock(&adev->mm_stats.lock);
321 /* Increase the amount of accumulated us. */
322 time_us = ktime_to_us(ktime_get());
323 increment_us = time_us - adev->mm_stats.last_update_us;
324 adev->mm_stats.last_update_us = time_us;
325 adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
328 /* This prevents the short period of low performance when the VRAM
329 * usage is low and the driver is in debt or doesn't have enough
330 * accumulated us to fill VRAM quickly.
332 * The situation can occur in these cases:
333 * - a lot of VRAM is freed by userspace
334 * - the presence of a big buffer causes a lot of evictions
335 * (solution: split buffers into smaller ones)
337 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
338 * accum_us to a positive number.
340 if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
343 /* Be more aggresive on dGPUs. Try to fill a portion of free
346 if (!(adev->flags & AMD_IS_APU))
347 min_us = bytes_to_us(adev, free_vram / 4);
349 min_us = 0; /* Reset accum_us on APUs. */
351 adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
354 /* This is set to 0 if the driver is in debt to disallow (optional)
357 *max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
359 /* Do the same for visible VRAM if half of it is free */
360 if (!amdgpu_gmc_vram_full_visible(&adev->gmc)) {
361 u64 total_vis_vram = adev->gmc.visible_vram_size;
363 amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
365 if (used_vis_vram < total_vis_vram) {
366 u64 free_vis_vram = total_vis_vram - used_vis_vram;
367 adev->mm_stats.accum_us_vis = min(adev->mm_stats.accum_us_vis +
368 increment_us, us_upper_bound);
370 if (free_vis_vram >= total_vis_vram / 2)
371 adev->mm_stats.accum_us_vis =
372 max(bytes_to_us(adev, free_vis_vram / 2),
373 adev->mm_stats.accum_us_vis);
376 *max_vis_bytes = us_to_bytes(adev, adev->mm_stats.accum_us_vis);
381 spin_unlock(&adev->mm_stats.lock);
384 /* Report how many bytes have really been moved for the last command
385 * submission. This can result in a debt that can stop buffer migrations
388 void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes,
391 spin_lock(&adev->mm_stats.lock);
392 adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
393 adev->mm_stats.accum_us_vis -= bytes_to_us(adev, num_vis_bytes);
394 spin_unlock(&adev->mm_stats.lock);
397 static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
398 struct amdgpu_bo *bo)
400 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
401 struct ttm_operation_ctx ctx = {
402 .interruptible = true,
403 .no_wait_gpu = false,
404 .resv = bo->tbo.resv,
413 /* Don't move this buffer if we have depleted our allowance
414 * to move it. Don't move anything if the threshold is zero.
416 if (p->bytes_moved < p->bytes_moved_threshold) {
417 if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
418 (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)) {
419 /* And don't move a CPU_ACCESS_REQUIRED BO to limited
420 * visible VRAM if we've depleted our allowance to do
423 if (p->bytes_moved_vis < p->bytes_moved_vis_threshold)
424 domain = bo->preferred_domains;
426 domain = bo->allowed_domains;
428 domain = bo->preferred_domains;
431 domain = bo->allowed_domains;
435 amdgpu_bo_placement_from_domain(bo, domain);
436 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
438 p->bytes_moved += ctx.bytes_moved;
439 if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
440 amdgpu_bo_in_cpu_visible_vram(bo))
441 p->bytes_moved_vis += ctx.bytes_moved;
443 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
444 domain = bo->allowed_domains;
451 /* Last resort, try to evict something from the current working set */
452 static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
453 struct amdgpu_bo *validated)
455 uint32_t domain = validated->allowed_domains;
456 struct ttm_operation_ctx ctx = { true, false };
462 for (;&p->evictable->tv.head != &p->validated;
463 p->evictable = list_prev_entry(p->evictable, tv.head)) {
465 struct amdgpu_bo_list_entry *candidate = p->evictable;
466 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(candidate->tv.bo);
467 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
468 bool update_bytes_moved_vis;
471 /* If we reached our current BO we can forget it */
475 /* We can't move pinned BOs here */
479 other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
481 /* Check if this BO is in one of the domains we need space for */
482 if (!(other & domain))
485 /* Check if we can move this BO somewhere else */
486 other = bo->allowed_domains & ~domain;
490 /* Good we can try to move this BO somewhere else */
491 update_bytes_moved_vis =
492 !amdgpu_gmc_vram_full_visible(&adev->gmc) &&
493 amdgpu_bo_in_cpu_visible_vram(bo);
494 amdgpu_bo_placement_from_domain(bo, other);
495 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
496 p->bytes_moved += ctx.bytes_moved;
497 if (update_bytes_moved_vis)
498 p->bytes_moved_vis += ctx.bytes_moved;
503 p->evictable = list_prev_entry(p->evictable, tv.head);
504 list_move(&candidate->tv.head, &p->validated);
512 static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo)
514 struct amdgpu_cs_parser *p = param;
518 r = amdgpu_cs_bo_validate(p, bo);
519 } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo));
524 r = amdgpu_cs_bo_validate(p, bo->shadow);
529 static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
530 struct list_head *validated)
532 struct ttm_operation_ctx ctx = { true, false };
533 struct amdgpu_bo_list_entry *lobj;
536 list_for_each_entry(lobj, validated, tv.head) {
537 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(lobj->tv.bo);
538 bool binding_userptr = false;
539 struct mm_struct *usermm;
541 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
542 if (usermm && usermm != current->mm)
545 /* Check if we have user pages and nobody bound the BO already */
546 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
548 amdgpu_bo_placement_from_domain(bo,
549 AMDGPU_GEM_DOMAIN_CPU);
550 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
553 amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm,
555 binding_userptr = true;
558 if (p->evictable == lobj)
561 r = amdgpu_cs_validate(p, bo);
565 if (binding_userptr) {
566 kvfree(lobj->user_pages);
567 lobj->user_pages = NULL;
573 static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
574 union drm_amdgpu_cs *cs)
576 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
577 struct amdgpu_vm *vm = &fpriv->vm;
578 struct amdgpu_bo_list_entry *e;
579 struct list_head duplicates;
580 struct amdgpu_bo *gds;
581 struct amdgpu_bo *gws;
582 struct amdgpu_bo *oa;
586 INIT_LIST_HEAD(&p->validated);
588 /* p->bo_list could already be assigned if AMDGPU_CHUNK_ID_BO_HANDLES is present */
589 if (cs->in.bo_list_handle) {
593 r = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle,
597 } else if (!p->bo_list) {
598 /* Create a empty bo_list when no handle is provided */
599 r = amdgpu_bo_list_create(p->adev, p->filp, NULL, 0,
605 /* One for TTM and one for the CS job */
606 amdgpu_bo_list_for_each_entry(e, p->bo_list)
607 e->tv.num_shared = 2;
609 amdgpu_bo_list_get_list(p->bo_list, &p->validated);
610 if (p->bo_list->first_userptr != p->bo_list->num_entries)
611 p->mn = amdgpu_mn_get(p->adev, AMDGPU_MN_TYPE_GFX);
613 INIT_LIST_HEAD(&duplicates);
614 amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
616 if (p->uf_entry.tv.bo && !ttm_to_amdgpu_bo(p->uf_entry.tv.bo)->parent)
617 list_add(&p->uf_entry.tv.head, &p->validated);
620 struct list_head need_pages;
622 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
624 if (unlikely(r != 0)) {
625 if (r != -ERESTARTSYS)
626 DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
627 goto error_free_pages;
630 INIT_LIST_HEAD(&need_pages);
631 amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
632 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
634 if (amdgpu_ttm_tt_userptr_invalidated(bo->tbo.ttm,
635 &e->user_invalidated) && e->user_pages) {
637 /* We acquired a page array, but somebody
638 * invalidated it. Free it and try again
640 release_pages(e->user_pages,
641 bo->tbo.ttm->num_pages);
642 kvfree(e->user_pages);
643 e->user_pages = NULL;
646 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
648 list_del(&e->tv.head);
649 list_add(&e->tv.head, &need_pages);
651 amdgpu_bo_unreserve(bo);
655 if (list_empty(&need_pages))
658 /* Unreserve everything again. */
659 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
661 /* We tried too many times, just abort */
664 DRM_ERROR("deadlock in %s\n", __func__);
665 goto error_free_pages;
668 /* Fill the page arrays for all userptrs. */
669 list_for_each_entry(e, &need_pages, tv.head) {
670 struct ttm_tt *ttm = e->tv.bo->ttm;
672 e->user_pages = kvmalloc_array(ttm->num_pages,
673 sizeof(struct page*),
674 GFP_KERNEL | __GFP_ZERO);
675 if (!e->user_pages) {
677 DRM_ERROR("calloc failure in %s\n", __func__);
678 goto error_free_pages;
681 r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
683 DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
684 kvfree(e->user_pages);
685 e->user_pages = NULL;
686 goto error_free_pages;
691 list_splice(&need_pages, &p->validated);
694 amdgpu_cs_get_threshold_for_moves(p->adev, &p->bytes_moved_threshold,
695 &p->bytes_moved_vis_threshold);
697 p->bytes_moved_vis = 0;
698 p->evictable = list_last_entry(&p->validated,
699 struct amdgpu_bo_list_entry,
702 r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
703 amdgpu_cs_validate, p);
705 DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
709 r = amdgpu_cs_list_validate(p, &duplicates);
711 DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
715 r = amdgpu_cs_list_validate(p, &p->validated);
717 DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
721 amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved,
724 gds = p->bo_list->gds_obj;
725 gws = p->bo_list->gws_obj;
726 oa = p->bo_list->oa_obj;
728 amdgpu_bo_list_for_each_entry(e, p->bo_list) {
729 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
731 /* Make sure we use the exclusive slot for shared BOs */
732 if (bo->prime_shared_count)
733 e->tv.num_shared = 0;
734 e->bo_va = amdgpu_vm_bo_find(vm, bo);
738 p->job->gds_base = amdgpu_bo_gpu_offset(gds) >> PAGE_SHIFT;
739 p->job->gds_size = amdgpu_bo_size(gds) >> PAGE_SHIFT;
742 p->job->gws_base = amdgpu_bo_gpu_offset(gws) >> PAGE_SHIFT;
743 p->job->gws_size = amdgpu_bo_size(gws) >> PAGE_SHIFT;
746 p->job->oa_base = amdgpu_bo_gpu_offset(oa) >> PAGE_SHIFT;
747 p->job->oa_size = amdgpu_bo_size(oa) >> PAGE_SHIFT;
750 if (!r && p->uf_entry.tv.bo) {
751 struct amdgpu_bo *uf = ttm_to_amdgpu_bo(p->uf_entry.tv.bo);
753 r = amdgpu_ttm_alloc_gart(&uf->tbo);
754 p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
759 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
763 amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
767 release_pages(e->user_pages, e->tv.bo->ttm->num_pages);
768 kvfree(e->user_pages);
774 static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
776 struct amdgpu_bo_list_entry *e;
779 list_for_each_entry(e, &p->validated, tv.head) {
780 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
781 struct reservation_object *resv = bo->tbo.resv;
783 r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp,
784 amdgpu_bo_explicit_sync(bo));
793 * cs_parser_fini() - clean parser states
794 * @parser: parser structure holding parsing context.
795 * @error: error number
797 * If error is set than unvalidate buffer, otherwise just free memory
798 * used by parsing context.
800 static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error,
805 if (error && backoff)
806 ttm_eu_backoff_reservation(&parser->ticket,
809 for (i = 0; i < parser->num_post_deps; i++) {
810 drm_syncobj_put(parser->post_deps[i].syncobj);
811 kfree(parser->post_deps[i].chain);
813 kfree(parser->post_deps);
815 dma_fence_put(parser->fence);
818 mutex_unlock(&parser->ctx->lock);
819 amdgpu_ctx_put(parser->ctx);
822 amdgpu_bo_list_put(parser->bo_list);
824 for (i = 0; i < parser->nchunks; i++)
825 kvfree(parser->chunks[i].kdata);
826 kfree(parser->chunks);
828 amdgpu_job_free(parser->job);
829 if (parser->uf_entry.tv.bo) {
830 struct amdgpu_bo *uf = ttm_to_amdgpu_bo(parser->uf_entry.tv.bo);
832 amdgpu_bo_unref(&uf);
836 static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
838 struct amdgpu_ring *ring = to_amdgpu_ring(p->entity->rq->sched);
839 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
840 struct amdgpu_device *adev = p->adev;
841 struct amdgpu_vm *vm = &fpriv->vm;
842 struct amdgpu_bo_list_entry *e;
843 struct amdgpu_bo_va *bo_va;
844 struct amdgpu_bo *bo;
847 /* Only for UVD/VCE VM emulation */
848 if (ring->funcs->parse_cs || ring->funcs->patch_cs_in_place) {
851 for (i = 0, j = 0; i < p->nchunks && j < p->job->num_ibs; i++) {
852 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
853 struct amdgpu_bo_va_mapping *m;
854 struct amdgpu_bo *aobj = NULL;
855 struct amdgpu_cs_chunk *chunk;
856 uint64_t offset, va_start;
857 struct amdgpu_ib *ib;
860 chunk = &p->chunks[i];
861 ib = &p->job->ibs[j];
862 chunk_ib = chunk->kdata;
864 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
867 va_start = chunk_ib->va_start & AMDGPU_GMC_HOLE_MASK;
868 r = amdgpu_cs_find_mapping(p, va_start, &aobj, &m);
870 DRM_ERROR("IB va_start is invalid\n");
874 if ((va_start + chunk_ib->ib_bytes) >
875 (m->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
876 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
880 /* the IB should be reserved at this point */
881 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
886 offset = m->start * AMDGPU_GPU_PAGE_SIZE;
887 kptr += va_start - offset;
889 if (ring->funcs->parse_cs) {
890 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
891 amdgpu_bo_kunmap(aobj);
893 r = amdgpu_ring_parse_cs(ring, p, j);
897 ib->ptr = (uint32_t *)kptr;
898 r = amdgpu_ring_patch_cs_in_place(ring, p, j);
899 amdgpu_bo_kunmap(aobj);
909 return amdgpu_cs_sync_rings(p);
912 r = amdgpu_vm_clear_freed(adev, vm, NULL);
916 r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
920 r = amdgpu_sync_fence(adev, &p->job->sync,
921 fpriv->prt_va->last_pt_update, false);
925 if (amdgpu_sriov_vf(adev)) {
928 bo_va = fpriv->csa_va;
930 r = amdgpu_vm_bo_update(adev, bo_va, false);
934 f = bo_va->last_pt_update;
935 r = amdgpu_sync_fence(adev, &p->job->sync, f, false);
940 amdgpu_bo_list_for_each_entry(e, p->bo_list) {
943 /* ignore duplicates */
944 bo = ttm_to_amdgpu_bo(e->tv.bo);
952 r = amdgpu_vm_bo_update(adev, bo_va, false);
956 f = bo_va->last_pt_update;
957 r = amdgpu_sync_fence(adev, &p->job->sync, f, false);
962 r = amdgpu_vm_handle_moved(adev, vm);
966 r = amdgpu_vm_update_directories(adev, vm);
970 r = amdgpu_sync_fence(adev, &p->job->sync, vm->last_update, false);
974 p->job->vm_pd_addr = amdgpu_gmc_pd_addr(vm->root.base.bo);
976 if (amdgpu_vm_debug) {
977 /* Invalidate all BOs to test for userspace bugs */
978 amdgpu_bo_list_for_each_entry(e, p->bo_list) {
979 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
981 /* ignore duplicates */
985 amdgpu_vm_bo_invalidate(adev, bo, false);
989 return amdgpu_cs_sync_rings(p);
992 static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
993 struct amdgpu_cs_parser *parser)
995 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
996 struct amdgpu_vm *vm = &fpriv->vm;
997 int r, ce_preempt = 0, de_preempt = 0;
998 struct amdgpu_ring *ring;
1001 for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
1002 struct amdgpu_cs_chunk *chunk;
1003 struct amdgpu_ib *ib;
1004 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
1005 struct drm_sched_entity *entity;
1007 chunk = &parser->chunks[i];
1008 ib = &parser->job->ibs[j];
1009 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
1011 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
1014 if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX && amdgpu_sriov_vf(adev)) {
1015 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREEMPT) {
1016 if (chunk_ib->flags & AMDGPU_IB_FLAG_CE)
1022 /* each GFX command submit allows 0 or 1 IB preemptible for CE & DE */
1023 if (ce_preempt > 1 || de_preempt > 1)
1027 r = amdgpu_ctx_get_entity(parser->ctx, chunk_ib->ip_type,
1028 chunk_ib->ip_instance, chunk_ib->ring,
1033 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE)
1034 parser->job->preamble_status |=
1035 AMDGPU_PREAMBLE_IB_PRESENT;
1037 if (parser->entity && parser->entity != entity)
1040 parser->entity = entity;
1042 ring = to_amdgpu_ring(entity->rq->sched);
1043 r = amdgpu_ib_get(adev, vm, ring->funcs->parse_cs ?
1044 chunk_ib->ib_bytes : 0, ib);
1046 DRM_ERROR("Failed to get ib !\n");
1050 ib->gpu_addr = chunk_ib->va_start;
1051 ib->length_dw = chunk_ib->ib_bytes / 4;
1052 ib->flags = chunk_ib->flags;
1057 /* UVD & VCE fw doesn't support user fences */
1058 ring = to_amdgpu_ring(parser->entity->rq->sched);
1059 if (parser->job->uf_addr && (
1060 ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
1061 ring->funcs->type == AMDGPU_RING_TYPE_VCE))
1064 return amdgpu_ctx_wait_prev_fence(parser->ctx, parser->entity);
1067 static int amdgpu_cs_process_fence_dep(struct amdgpu_cs_parser *p,
1068 struct amdgpu_cs_chunk *chunk)
1070 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
1073 struct drm_amdgpu_cs_chunk_dep *deps;
1075 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
1076 num_deps = chunk->length_dw * 4 /
1077 sizeof(struct drm_amdgpu_cs_chunk_dep);
1079 for (i = 0; i < num_deps; ++i) {
1080 struct amdgpu_ctx *ctx;
1081 struct drm_sched_entity *entity;
1082 struct dma_fence *fence;
1084 ctx = amdgpu_ctx_get(fpriv, deps[i].ctx_id);
1088 r = amdgpu_ctx_get_entity(ctx, deps[i].ip_type,
1089 deps[i].ip_instance,
1090 deps[i].ring, &entity);
1092 amdgpu_ctx_put(ctx);
1096 fence = amdgpu_ctx_get_fence(ctx, entity,
1099 if (chunk->chunk_id == AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES) {
1100 struct drm_sched_fence *s_fence = to_drm_sched_fence(fence);
1101 struct dma_fence *old = fence;
1103 fence = dma_fence_get(&s_fence->scheduled);
1107 if (IS_ERR(fence)) {
1109 amdgpu_ctx_put(ctx);
1112 r = amdgpu_sync_fence(p->adev, &p->job->sync, fence,
1114 dma_fence_put(fence);
1115 amdgpu_ctx_put(ctx);
1123 static int amdgpu_syncobj_lookup_and_add_to_sync(struct amdgpu_cs_parser *p,
1124 uint32_t handle, u64 point,
1127 struct dma_fence *fence;
1130 r = drm_syncobj_find_fence(p->filp, handle, point, flags, &fence);
1132 DRM_ERROR("syncobj %u failed to find fence @ %llu (%d)!\n",
1137 r = amdgpu_sync_fence(p->adev, &p->job->sync, fence, true);
1138 dma_fence_put(fence);
1143 static int amdgpu_cs_process_syncobj_in_dep(struct amdgpu_cs_parser *p,
1144 struct amdgpu_cs_chunk *chunk)
1146 struct drm_amdgpu_cs_chunk_sem *deps;
1150 deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
1151 num_deps = chunk->length_dw * 4 /
1152 sizeof(struct drm_amdgpu_cs_chunk_sem);
1153 for (i = 0; i < num_deps; ++i) {
1154 r = amdgpu_syncobj_lookup_and_add_to_sync(p, deps[i].handle,
1164 static int amdgpu_cs_process_syncobj_timeline_in_dep(struct amdgpu_cs_parser *p,
1165 struct amdgpu_cs_chunk *chunk)
1167 struct drm_amdgpu_cs_chunk_syncobj *syncobj_deps;
1171 syncobj_deps = (struct drm_amdgpu_cs_chunk_syncobj *)chunk->kdata;
1172 num_deps = chunk->length_dw * 4 /
1173 sizeof(struct drm_amdgpu_cs_chunk_syncobj);
1174 for (i = 0; i < num_deps; ++i) {
1175 r = amdgpu_syncobj_lookup_and_add_to_sync(p,
1176 syncobj_deps[i].handle,
1177 syncobj_deps[i].point,
1178 syncobj_deps[i].flags);
1186 static int amdgpu_cs_process_syncobj_out_dep(struct amdgpu_cs_parser *p,
1187 struct amdgpu_cs_chunk *chunk)
1189 struct drm_amdgpu_cs_chunk_sem *deps;
1193 deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
1194 num_deps = chunk->length_dw * 4 /
1195 sizeof(struct drm_amdgpu_cs_chunk_sem);
1197 p->post_deps = kmalloc_array(num_deps, sizeof(*p->post_deps),
1199 p->num_post_deps = 0;
1205 for (i = 0; i < num_deps; ++i) {
1206 p->post_deps[i].syncobj =
1207 drm_syncobj_find(p->filp, deps[i].handle);
1208 if (!p->post_deps[i].syncobj)
1210 p->post_deps[i].chain = NULL;
1211 p->post_deps[i].point = 0;
1219 static int amdgpu_cs_process_syncobj_timeline_out_dep(struct amdgpu_cs_parser *p,
1220 struct amdgpu_cs_chunk
1223 struct drm_amdgpu_cs_chunk_syncobj *syncobj_deps;
1227 syncobj_deps = (struct drm_amdgpu_cs_chunk_syncobj *)chunk->kdata;
1228 num_deps = chunk->length_dw * 4 /
1229 sizeof(struct drm_amdgpu_cs_chunk_syncobj);
1231 p->post_deps = kmalloc_array(num_deps, sizeof(*p->post_deps),
1233 p->num_post_deps = 0;
1238 for (i = 0; i < num_deps; ++i) {
1239 struct amdgpu_cs_post_dep *dep = &p->post_deps[i];
1242 if (syncobj_deps[i].point) {
1243 dep->chain = kmalloc(sizeof(*dep->chain), GFP_KERNEL);
1248 dep->syncobj = drm_syncobj_find(p->filp,
1249 syncobj_deps[i].handle);
1250 if (!dep->syncobj) {
1254 dep->point = syncobj_deps[i].point;
1261 static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
1262 struct amdgpu_cs_parser *p)
1266 for (i = 0; i < p->nchunks; ++i) {
1267 struct amdgpu_cs_chunk *chunk;
1269 chunk = &p->chunks[i];
1271 switch (chunk->chunk_id) {
1272 case AMDGPU_CHUNK_ID_DEPENDENCIES:
1273 case AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES:
1274 r = amdgpu_cs_process_fence_dep(p, chunk);
1278 case AMDGPU_CHUNK_ID_SYNCOBJ_IN:
1279 r = amdgpu_cs_process_syncobj_in_dep(p, chunk);
1283 case AMDGPU_CHUNK_ID_SYNCOBJ_OUT:
1284 r = amdgpu_cs_process_syncobj_out_dep(p, chunk);
1288 case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_WAIT:
1289 r = amdgpu_cs_process_syncobj_timeline_in_dep(p, chunk);
1293 case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_SIGNAL:
1294 r = amdgpu_cs_process_syncobj_timeline_out_dep(p, chunk);
1304 static void amdgpu_cs_post_dependencies(struct amdgpu_cs_parser *p)
1308 for (i = 0; i < p->num_post_deps; ++i) {
1309 if (p->post_deps[i].chain && p->post_deps[i].point) {
1310 drm_syncobj_add_point(p->post_deps[i].syncobj,
1311 p->post_deps[i].chain,
1312 p->fence, p->post_deps[i].point);
1313 p->post_deps[i].chain = NULL;
1315 drm_syncobj_replace_fence(p->post_deps[i].syncobj,
1321 static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
1322 union drm_amdgpu_cs *cs)
1324 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
1325 struct drm_sched_entity *entity = p->entity;
1326 enum drm_sched_priority priority;
1327 struct amdgpu_ring *ring;
1328 struct amdgpu_bo_list_entry *e;
1329 struct amdgpu_job *job;
1337 r = drm_sched_job_init(&job->base, entity, p->filp);
1341 /* No memory allocation is allowed while holding the mn lock */
1342 amdgpu_mn_lock(p->mn);
1343 amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
1344 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
1346 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
1352 job->owner = p->filp;
1353 p->fence = dma_fence_get(&job->base.s_fence->finished);
1355 amdgpu_ctx_add_fence(p->ctx, entity, p->fence, &seq);
1356 amdgpu_cs_post_dependencies(p);
1358 if ((job->preamble_status & AMDGPU_PREAMBLE_IB_PRESENT) &&
1359 !p->ctx->preamble_presented) {
1360 job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
1361 p->ctx->preamble_presented = true;
1364 cs->out.handle = seq;
1365 job->uf_sequence = seq;
1367 amdgpu_job_free_resources(job);
1369 trace_amdgpu_cs_ioctl(job);
1370 amdgpu_vm_bo_trace_cs(&fpriv->vm, &p->ticket);
1371 priority = job->base.s_priority;
1372 drm_sched_entity_push_job(&job->base, entity);
1374 ring = to_amdgpu_ring(entity->rq->sched);
1375 amdgpu_ring_priority_get(ring, priority);
1377 amdgpu_vm_move_to_lru_tail(p->adev, &fpriv->vm);
1379 ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
1380 amdgpu_mn_unlock(p->mn);
1385 drm_sched_job_cleanup(&job->base);
1386 amdgpu_mn_unlock(p->mn);
1389 amdgpu_job_free(job);
1393 int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1395 struct amdgpu_device *adev = dev->dev_private;
1396 union drm_amdgpu_cs *cs = data;
1397 struct amdgpu_cs_parser parser = {};
1398 bool reserved_buffers = false;
1401 if (!adev->accel_working)
1407 r = amdgpu_cs_parser_init(&parser, data);
1409 DRM_ERROR("Failed to initialize parser %d!\n", r);
1413 r = amdgpu_cs_ib_fill(adev, &parser);
1417 r = amdgpu_cs_dependencies(adev, &parser);
1419 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
1423 r = amdgpu_cs_parser_bos(&parser, data);
1426 DRM_ERROR("Not enough memory for command submission!\n");
1427 else if (r != -ERESTARTSYS)
1428 DRM_ERROR("Failed to process the buffer list %d!\n", r);
1432 reserved_buffers = true;
1434 for (i = 0; i < parser.job->num_ibs; i++)
1435 trace_amdgpu_cs(&parser, i);
1437 r = amdgpu_cs_vm_handling(&parser);
1441 r = amdgpu_cs_submit(&parser, cs);
1444 amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
1449 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1452 * @data: data from userspace
1453 * @filp: file private
1455 * Wait for the command submission identified by handle to finish.
1457 int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
1458 struct drm_file *filp)
1460 union drm_amdgpu_wait_cs *wait = data;
1461 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
1462 struct drm_sched_entity *entity;
1463 struct amdgpu_ctx *ctx;
1464 struct dma_fence *fence;
1467 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
1471 r = amdgpu_ctx_get_entity(ctx, wait->in.ip_type, wait->in.ip_instance,
1472 wait->in.ring, &entity);
1474 amdgpu_ctx_put(ctx);
1478 fence = amdgpu_ctx_get_fence(ctx, entity, wait->in.handle);
1482 r = dma_fence_wait_timeout(fence, true, timeout);
1483 if (r > 0 && fence->error)
1485 dma_fence_put(fence);
1489 amdgpu_ctx_put(ctx);
1493 memset(wait, 0, sizeof(*wait));
1494 wait->out.status = (r == 0);
1500 * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
1502 * @adev: amdgpu device
1503 * @filp: file private
1504 * @user: drm_amdgpu_fence copied from user space
1506 static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
1507 struct drm_file *filp,
1508 struct drm_amdgpu_fence *user)
1510 struct drm_sched_entity *entity;
1511 struct amdgpu_ctx *ctx;
1512 struct dma_fence *fence;
1515 ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
1517 return ERR_PTR(-EINVAL);
1519 r = amdgpu_ctx_get_entity(ctx, user->ip_type, user->ip_instance,
1520 user->ring, &entity);
1522 amdgpu_ctx_put(ctx);
1526 fence = amdgpu_ctx_get_fence(ctx, entity, user->seq_no);
1527 amdgpu_ctx_put(ctx);
1532 int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,
1533 struct drm_file *filp)
1535 struct amdgpu_device *adev = dev->dev_private;
1536 union drm_amdgpu_fence_to_handle *info = data;
1537 struct dma_fence *fence;
1538 struct drm_syncobj *syncobj;
1539 struct sync_file *sync_file;
1542 fence = amdgpu_cs_get_fence(adev, filp, &info->in.fence);
1544 return PTR_ERR(fence);
1547 fence = dma_fence_get_stub();
1549 switch (info->in.what) {
1550 case AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ:
1551 r = drm_syncobj_create(&syncobj, 0, fence);
1552 dma_fence_put(fence);
1555 r = drm_syncobj_get_handle(filp, syncobj, &info->out.handle);
1556 drm_syncobj_put(syncobj);
1559 case AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ_FD:
1560 r = drm_syncobj_create(&syncobj, 0, fence);
1561 dma_fence_put(fence);
1564 r = drm_syncobj_get_fd(syncobj, (int*)&info->out.handle);
1565 drm_syncobj_put(syncobj);
1568 case AMDGPU_FENCE_TO_HANDLE_GET_SYNC_FILE_FD:
1569 fd = get_unused_fd_flags(O_CLOEXEC);
1571 dma_fence_put(fence);
1575 sync_file = sync_file_create(fence);
1576 dma_fence_put(fence);
1582 fd_install(fd, sync_file->file);
1583 info->out.handle = fd;
1592 * amdgpu_cs_wait_all_fence - wait on all fences to signal
1594 * @adev: amdgpu device
1595 * @filp: file private
1596 * @wait: wait parameters
1597 * @fences: array of drm_amdgpu_fence
1599 static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
1600 struct drm_file *filp,
1601 union drm_amdgpu_wait_fences *wait,
1602 struct drm_amdgpu_fence *fences)
1604 uint32_t fence_count = wait->in.fence_count;
1608 for (i = 0; i < fence_count; i++) {
1609 struct dma_fence *fence;
1610 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1612 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1614 return PTR_ERR(fence);
1618 r = dma_fence_wait_timeout(fence, true, timeout);
1619 dma_fence_put(fence);
1627 return fence->error;
1630 memset(wait, 0, sizeof(*wait));
1631 wait->out.status = (r > 0);
1637 * amdgpu_cs_wait_any_fence - wait on any fence to signal
1639 * @adev: amdgpu device
1640 * @filp: file private
1641 * @wait: wait parameters
1642 * @fences: array of drm_amdgpu_fence
1644 static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
1645 struct drm_file *filp,
1646 union drm_amdgpu_wait_fences *wait,
1647 struct drm_amdgpu_fence *fences)
1649 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1650 uint32_t fence_count = wait->in.fence_count;
1651 uint32_t first = ~0;
1652 struct dma_fence **array;
1656 /* Prepare the fence array */
1657 array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
1662 for (i = 0; i < fence_count; i++) {
1663 struct dma_fence *fence;
1665 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1666 if (IS_ERR(fence)) {
1668 goto err_free_fence_array;
1671 } else { /* NULL, the fence has been already signaled */
1678 r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
1681 goto err_free_fence_array;
1684 memset(wait, 0, sizeof(*wait));
1685 wait->out.status = (r > 0);
1686 wait->out.first_signaled = first;
1688 if (first < fence_count && array[first])
1689 r = array[first]->error;
1693 err_free_fence_array:
1694 for (i = 0; i < fence_count; i++)
1695 dma_fence_put(array[i]);
1702 * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
1705 * @data: data from userspace
1706 * @filp: file private
1708 int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
1709 struct drm_file *filp)
1711 struct amdgpu_device *adev = dev->dev_private;
1712 union drm_amdgpu_wait_fences *wait = data;
1713 uint32_t fence_count = wait->in.fence_count;
1714 struct drm_amdgpu_fence *fences_user;
1715 struct drm_amdgpu_fence *fences;
1718 /* Get the fences from userspace */
1719 fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
1724 fences_user = u64_to_user_ptr(wait->in.fences);
1725 if (copy_from_user(fences, fences_user,
1726 sizeof(struct drm_amdgpu_fence) * fence_count)) {
1728 goto err_free_fences;
1731 if (wait->in.wait_all)
1732 r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
1734 r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
1743 * amdgpu_cs_find_bo_va - find bo_va for VM address
1745 * @parser: command submission parser context
1747 * @bo: resulting BO of the mapping found
1749 * Search the buffer objects in the command submission context for a certain
1750 * virtual memory address. Returns allocation structure when found, NULL
1753 int amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1754 uint64_t addr, struct amdgpu_bo **bo,
1755 struct amdgpu_bo_va_mapping **map)
1757 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
1758 struct ttm_operation_ctx ctx = { false, false };
1759 struct amdgpu_vm *vm = &fpriv->vm;
1760 struct amdgpu_bo_va_mapping *mapping;
1763 addr /= AMDGPU_GPU_PAGE_SIZE;
1765 mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
1766 if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
1769 *bo = mapping->bo_va->base.bo;
1772 /* Double check that the BO is reserved by this CS */
1773 if (READ_ONCE((*bo)->tbo.resv->lock.ctx) != &parser->ticket)
1776 if (!((*bo)->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)) {
1777 (*bo)->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1778 amdgpu_bo_placement_from_domain(*bo, (*bo)->allowed_domains);
1779 r = ttm_bo_validate(&(*bo)->tbo, &(*bo)->placement, &ctx);
1784 return amdgpu_ttm_alloc_gart(&(*bo)->tbo);