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.
25 * Jerome Glisse <glisse@freedesktop.org>
28 #include <linux/file.h>
29 #include <linux/pagemap.h>
30 #include <linux/sync_file.h>
31 #include <linux/dma-buf.h>
33 #include <drm/amdgpu_drm.h>
34 #include <drm/drm_syncobj.h>
35 #include <drm/ttm/ttm_tt.h>
37 #include "amdgpu_cs.h"
39 #include "amdgpu_trace.h"
40 #include "amdgpu_gmc.h"
41 #include "amdgpu_gem.h"
42 #include "amdgpu_ras.h"
44 static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p,
45 struct amdgpu_device *adev,
46 struct drm_file *filp,
47 union drm_amdgpu_cs *cs)
49 struct amdgpu_fpriv *fpriv = filp->driver_priv;
51 if (cs->in.num_chunks == 0)
54 memset(p, 0, sizeof(*p));
58 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
62 if (atomic_read(&p->ctx->guilty)) {
63 amdgpu_ctx_put(p->ctx);
67 amdgpu_sync_create(&p->sync);
71 static int amdgpu_cs_job_idx(struct amdgpu_cs_parser *p,
72 struct drm_amdgpu_cs_chunk_ib *chunk_ib)
74 struct drm_sched_entity *entity;
78 r = amdgpu_ctx_get_entity(p->ctx, chunk_ib->ip_type,
79 chunk_ib->ip_instance,
80 chunk_ib->ring, &entity);
85 * Abort if there is no run queue associated with this entity.
86 * Possibly because of disabled HW IP.
88 if (entity->rq == NULL)
91 /* Check if we can add this IB to some existing job */
92 for (i = 0; i < p->gang_size; ++i)
93 if (p->entities[i] == entity)
96 /* If not increase the gang size if possible */
97 if (i == AMDGPU_CS_GANG_SIZE)
100 p->entities[i] = entity;
101 p->gang_size = i + 1;
105 static int amdgpu_cs_p1_ib(struct amdgpu_cs_parser *p,
106 struct drm_amdgpu_cs_chunk_ib *chunk_ib,
107 unsigned int *num_ibs)
111 r = amdgpu_cs_job_idx(p, chunk_ib);
116 p->gang_leader_idx = r;
120 static int amdgpu_cs_p1_user_fence(struct amdgpu_cs_parser *p,
121 struct drm_amdgpu_cs_chunk_fence *data,
124 struct drm_gem_object *gobj;
125 struct amdgpu_bo *bo;
129 gobj = drm_gem_object_lookup(p->filp, data->handle);
133 bo = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
134 p->uf_entry.priority = 0;
135 p->uf_entry.tv.bo = &bo->tbo;
136 /* One for TTM and two for the CS job */
137 p->uf_entry.tv.num_shared = 3;
139 drm_gem_object_put(gobj);
141 size = amdgpu_bo_size(bo);
142 if (size != PAGE_SIZE || (data->offset + 8) > size) {
147 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) {
152 *offset = data->offset;
157 amdgpu_bo_unref(&bo);
161 static int amdgpu_cs_p1_bo_handles(struct amdgpu_cs_parser *p,
162 struct drm_amdgpu_bo_list_in *data)
164 struct drm_amdgpu_bo_list_entry *info;
167 r = amdgpu_bo_create_list_entry_array(data, &info);
171 r = amdgpu_bo_list_create(p->adev, p->filp, info, data->bo_number,
185 /* Copy the data from userspace and go over it the first time */
186 static int amdgpu_cs_pass1(struct amdgpu_cs_parser *p,
187 union drm_amdgpu_cs *cs)
189 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
190 unsigned int num_ibs[AMDGPU_CS_GANG_SIZE] = { };
191 struct amdgpu_vm *vm = &fpriv->vm;
192 uint64_t *chunk_array_user;
193 uint64_t *chunk_array;
194 uint32_t uf_offset = 0;
199 chunk_array = kvmalloc_array(cs->in.num_chunks, sizeof(uint64_t),
205 chunk_array_user = u64_to_user_ptr(cs->in.chunks);
206 if (copy_from_user(chunk_array, chunk_array_user,
207 sizeof(uint64_t)*cs->in.num_chunks)) {
212 p->nchunks = cs->in.num_chunks;
213 p->chunks = kvmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
220 for (i = 0; i < p->nchunks; i++) {
221 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
222 struct drm_amdgpu_cs_chunk user_chunk;
223 uint32_t __user *cdata;
225 chunk_ptr = u64_to_user_ptr(chunk_array[i]);
226 if (copy_from_user(&user_chunk, chunk_ptr,
227 sizeof(struct drm_amdgpu_cs_chunk))) {
230 goto free_partial_kdata;
232 p->chunks[i].chunk_id = user_chunk.chunk_id;
233 p->chunks[i].length_dw = user_chunk.length_dw;
235 size = p->chunks[i].length_dw;
236 cdata = u64_to_user_ptr(user_chunk.chunk_data);
238 p->chunks[i].kdata = kvmalloc_array(size, sizeof(uint32_t),
240 if (p->chunks[i].kdata == NULL) {
243 goto free_partial_kdata;
245 size *= sizeof(uint32_t);
246 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
248 goto free_partial_kdata;
251 /* Assume the worst on the following checks */
253 switch (p->chunks[i].chunk_id) {
254 case AMDGPU_CHUNK_ID_IB:
255 if (size < sizeof(struct drm_amdgpu_cs_chunk_ib))
256 goto free_partial_kdata;
258 ret = amdgpu_cs_p1_ib(p, p->chunks[i].kdata, num_ibs);
260 goto free_partial_kdata;
263 case AMDGPU_CHUNK_ID_FENCE:
264 if (size < sizeof(struct drm_amdgpu_cs_chunk_fence))
265 goto free_partial_kdata;
267 ret = amdgpu_cs_p1_user_fence(p, p->chunks[i].kdata,
270 goto free_partial_kdata;
273 case AMDGPU_CHUNK_ID_BO_HANDLES:
274 if (size < sizeof(struct drm_amdgpu_bo_list_in))
275 goto free_partial_kdata;
277 ret = amdgpu_cs_p1_bo_handles(p, p->chunks[i].kdata);
279 goto free_partial_kdata;
282 case AMDGPU_CHUNK_ID_DEPENDENCIES:
283 case AMDGPU_CHUNK_ID_SYNCOBJ_IN:
284 case AMDGPU_CHUNK_ID_SYNCOBJ_OUT:
285 case AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES:
286 case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_WAIT:
287 case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_SIGNAL:
291 goto free_partial_kdata;
297 goto free_partial_kdata;
300 for (i = 0; i < p->gang_size; ++i) {
301 ret = amdgpu_job_alloc(p->adev, vm, p->entities[i], vm,
302 num_ibs[i], &p->jobs[i]);
306 p->gang_leader = p->jobs[p->gang_leader_idx];
308 if (p->ctx->vram_lost_counter != p->gang_leader->vram_lost_counter) {
313 if (p->uf_entry.tv.bo)
314 p->gang_leader->uf_addr = uf_offset;
317 /* Use this opportunity to fill in task info for the vm */
318 amdgpu_vm_set_task_info(vm);
326 kvfree(p->chunks[i].kdata);
336 static int amdgpu_cs_p2_ib(struct amdgpu_cs_parser *p,
337 struct amdgpu_cs_chunk *chunk,
338 unsigned int *ce_preempt,
339 unsigned int *de_preempt)
341 struct drm_amdgpu_cs_chunk_ib *chunk_ib = chunk->kdata;
342 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
343 struct amdgpu_vm *vm = &fpriv->vm;
344 struct amdgpu_ring *ring;
345 struct amdgpu_job *job;
346 struct amdgpu_ib *ib;
349 r = amdgpu_cs_job_idx(p, chunk_ib);
354 ring = amdgpu_job_ring(job);
355 ib = &job->ibs[job->num_ibs++];
357 /* MM engine doesn't support user fences */
358 if (p->uf_entry.tv.bo && ring->funcs->no_user_fence)
361 if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX &&
362 chunk_ib->flags & AMDGPU_IB_FLAG_PREEMPT) {
363 if (chunk_ib->flags & AMDGPU_IB_FLAG_CE)
368 /* Each GFX command submit allows only 1 IB max
369 * preemptible for CE & DE */
370 if (*ce_preempt > 1 || *de_preempt > 1)
374 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE)
375 job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT;
377 r = amdgpu_ib_get(p->adev, vm, ring->funcs->parse_cs ?
378 chunk_ib->ib_bytes : 0,
379 AMDGPU_IB_POOL_DELAYED, ib);
381 DRM_ERROR("Failed to get ib !\n");
385 ib->gpu_addr = chunk_ib->va_start;
386 ib->length_dw = chunk_ib->ib_bytes / 4;
387 ib->flags = chunk_ib->flags;
391 static int amdgpu_cs_p2_dependencies(struct amdgpu_cs_parser *p,
392 struct amdgpu_cs_chunk *chunk)
394 struct drm_amdgpu_cs_chunk_dep *deps = chunk->kdata;
395 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
399 num_deps = chunk->length_dw * 4 /
400 sizeof(struct drm_amdgpu_cs_chunk_dep);
402 for (i = 0; i < num_deps; ++i) {
403 struct amdgpu_ctx *ctx;
404 struct drm_sched_entity *entity;
405 struct dma_fence *fence;
407 ctx = amdgpu_ctx_get(fpriv, deps[i].ctx_id);
411 r = amdgpu_ctx_get_entity(ctx, deps[i].ip_type,
413 deps[i].ring, &entity);
419 fence = amdgpu_ctx_get_fence(ctx, entity, deps[i].handle);
423 return PTR_ERR(fence);
427 if (chunk->chunk_id == AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES) {
428 struct drm_sched_fence *s_fence;
429 struct dma_fence *old = fence;
431 s_fence = to_drm_sched_fence(fence);
432 fence = dma_fence_get(&s_fence->scheduled);
436 r = amdgpu_sync_fence(&p->sync, fence);
437 dma_fence_put(fence);
444 static int amdgpu_syncobj_lookup_and_add(struct amdgpu_cs_parser *p,
445 uint32_t handle, u64 point,
448 struct dma_fence *fence;
451 r = drm_syncobj_find_fence(p->filp, handle, point, flags, &fence);
453 DRM_ERROR("syncobj %u failed to find fence @ %llu (%d)!\n",
458 r = amdgpu_sync_fence(&p->sync, fence);
459 dma_fence_put(fence);
463 static int amdgpu_cs_p2_syncobj_in(struct amdgpu_cs_parser *p,
464 struct amdgpu_cs_chunk *chunk)
466 struct drm_amdgpu_cs_chunk_sem *deps = chunk->kdata;
470 num_deps = chunk->length_dw * 4 /
471 sizeof(struct drm_amdgpu_cs_chunk_sem);
472 for (i = 0; i < num_deps; ++i) {
473 r = amdgpu_syncobj_lookup_and_add(p, deps[i].handle, 0, 0);
481 static int amdgpu_cs_p2_syncobj_timeline_wait(struct amdgpu_cs_parser *p,
482 struct amdgpu_cs_chunk *chunk)
484 struct drm_amdgpu_cs_chunk_syncobj *syncobj_deps = chunk->kdata;
488 num_deps = chunk->length_dw * 4 /
489 sizeof(struct drm_amdgpu_cs_chunk_syncobj);
490 for (i = 0; i < num_deps; ++i) {
491 r = amdgpu_syncobj_lookup_and_add(p, syncobj_deps[i].handle,
492 syncobj_deps[i].point,
493 syncobj_deps[i].flags);
501 static int amdgpu_cs_p2_syncobj_out(struct amdgpu_cs_parser *p,
502 struct amdgpu_cs_chunk *chunk)
504 struct drm_amdgpu_cs_chunk_sem *deps = chunk->kdata;
508 num_deps = chunk->length_dw * 4 /
509 sizeof(struct drm_amdgpu_cs_chunk_sem);
514 p->post_deps = kmalloc_array(num_deps, sizeof(*p->post_deps),
516 p->num_post_deps = 0;
522 for (i = 0; i < num_deps; ++i) {
523 p->post_deps[i].syncobj =
524 drm_syncobj_find(p->filp, deps[i].handle);
525 if (!p->post_deps[i].syncobj)
527 p->post_deps[i].chain = NULL;
528 p->post_deps[i].point = 0;
535 static int amdgpu_cs_p2_syncobj_timeline_signal(struct amdgpu_cs_parser *p,
536 struct amdgpu_cs_chunk *chunk)
538 struct drm_amdgpu_cs_chunk_syncobj *syncobj_deps = chunk->kdata;
542 num_deps = chunk->length_dw * 4 /
543 sizeof(struct drm_amdgpu_cs_chunk_syncobj);
548 p->post_deps = kmalloc_array(num_deps, sizeof(*p->post_deps),
550 p->num_post_deps = 0;
555 for (i = 0; i < num_deps; ++i) {
556 struct amdgpu_cs_post_dep *dep = &p->post_deps[i];
559 if (syncobj_deps[i].point) {
560 dep->chain = dma_fence_chain_alloc();
565 dep->syncobj = drm_syncobj_find(p->filp,
566 syncobj_deps[i].handle);
568 dma_fence_chain_free(dep->chain);
571 dep->point = syncobj_deps[i].point;
578 static int amdgpu_cs_pass2(struct amdgpu_cs_parser *p)
580 unsigned int ce_preempt = 0, de_preempt = 0;
583 for (i = 0; i < p->nchunks; ++i) {
584 struct amdgpu_cs_chunk *chunk;
586 chunk = &p->chunks[i];
588 switch (chunk->chunk_id) {
589 case AMDGPU_CHUNK_ID_IB:
590 r = amdgpu_cs_p2_ib(p, chunk, &ce_preempt, &de_preempt);
594 case AMDGPU_CHUNK_ID_DEPENDENCIES:
595 case AMDGPU_CHUNK_ID_SCHEDULED_DEPENDENCIES:
596 r = amdgpu_cs_p2_dependencies(p, chunk);
600 case AMDGPU_CHUNK_ID_SYNCOBJ_IN:
601 r = amdgpu_cs_p2_syncobj_in(p, chunk);
605 case AMDGPU_CHUNK_ID_SYNCOBJ_OUT:
606 r = amdgpu_cs_p2_syncobj_out(p, chunk);
610 case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_WAIT:
611 r = amdgpu_cs_p2_syncobj_timeline_wait(p, chunk);
615 case AMDGPU_CHUNK_ID_SYNCOBJ_TIMELINE_SIGNAL:
616 r = amdgpu_cs_p2_syncobj_timeline_signal(p, chunk);
626 /* Convert microseconds to bytes. */
627 static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
629 if (us <= 0 || !adev->mm_stats.log2_max_MBps)
632 /* Since accum_us is incremented by a million per second, just
633 * multiply it by the number of MB/s to get the number of bytes.
635 return us << adev->mm_stats.log2_max_MBps;
638 static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
640 if (!adev->mm_stats.log2_max_MBps)
643 return bytes >> adev->mm_stats.log2_max_MBps;
646 /* Returns how many bytes TTM can move right now. If no bytes can be moved,
647 * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
648 * which means it can go over the threshold once. If that happens, the driver
649 * will be in debt and no other buffer migrations can be done until that debt
652 * This approach allows moving a buffer of any size (it's important to allow
655 * The currency is simply time in microseconds and it increases as the clock
656 * ticks. The accumulated microseconds (us) are converted to bytes and
659 static void amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev,
663 s64 time_us, increment_us;
664 u64 free_vram, total_vram, used_vram;
665 /* Allow a maximum of 200 accumulated ms. This is basically per-IB
668 * It means that in order to get full max MBps, at least 5 IBs per
669 * second must be submitted and not more than 200ms apart from each
672 const s64 us_upper_bound = 200000;
674 if (!adev->mm_stats.log2_max_MBps) {
680 total_vram = adev->gmc.real_vram_size - atomic64_read(&adev->vram_pin_size);
681 used_vram = ttm_resource_manager_usage(&adev->mman.vram_mgr.manager);
682 free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
684 spin_lock(&adev->mm_stats.lock);
686 /* Increase the amount of accumulated us. */
687 time_us = ktime_to_us(ktime_get());
688 increment_us = time_us - adev->mm_stats.last_update_us;
689 adev->mm_stats.last_update_us = time_us;
690 adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
693 /* This prevents the short period of low performance when the VRAM
694 * usage is low and the driver is in debt or doesn't have enough
695 * accumulated us to fill VRAM quickly.
697 * The situation can occur in these cases:
698 * - a lot of VRAM is freed by userspace
699 * - the presence of a big buffer causes a lot of evictions
700 * (solution: split buffers into smaller ones)
702 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
703 * accum_us to a positive number.
705 if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
708 /* Be more aggressive on dGPUs. Try to fill a portion of free
711 if (!(adev->flags & AMD_IS_APU))
712 min_us = bytes_to_us(adev, free_vram / 4);
714 min_us = 0; /* Reset accum_us on APUs. */
716 adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
719 /* This is set to 0 if the driver is in debt to disallow (optional)
722 *max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
724 /* Do the same for visible VRAM if half of it is free */
725 if (!amdgpu_gmc_vram_full_visible(&adev->gmc)) {
726 u64 total_vis_vram = adev->gmc.visible_vram_size;
728 amdgpu_vram_mgr_vis_usage(&adev->mman.vram_mgr);
730 if (used_vis_vram < total_vis_vram) {
731 u64 free_vis_vram = total_vis_vram - used_vis_vram;
732 adev->mm_stats.accum_us_vis = min(adev->mm_stats.accum_us_vis +
733 increment_us, us_upper_bound);
735 if (free_vis_vram >= total_vis_vram / 2)
736 adev->mm_stats.accum_us_vis =
737 max(bytes_to_us(adev, free_vis_vram / 2),
738 adev->mm_stats.accum_us_vis);
741 *max_vis_bytes = us_to_bytes(adev, adev->mm_stats.accum_us_vis);
746 spin_unlock(&adev->mm_stats.lock);
749 /* Report how many bytes have really been moved for the last command
750 * submission. This can result in a debt that can stop buffer migrations
753 void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes,
756 spin_lock(&adev->mm_stats.lock);
757 adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
758 adev->mm_stats.accum_us_vis -= bytes_to_us(adev, num_vis_bytes);
759 spin_unlock(&adev->mm_stats.lock);
762 static int amdgpu_cs_bo_validate(void *param, struct amdgpu_bo *bo)
764 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
765 struct amdgpu_cs_parser *p = param;
766 struct ttm_operation_ctx ctx = {
767 .interruptible = true,
768 .no_wait_gpu = false,
769 .resv = bo->tbo.base.resv
774 if (bo->tbo.pin_count)
777 /* Don't move this buffer if we have depleted our allowance
778 * to move it. Don't move anything if the threshold is zero.
780 if (p->bytes_moved < p->bytes_moved_threshold &&
781 (!bo->tbo.base.dma_buf ||
782 list_empty(&bo->tbo.base.dma_buf->attachments))) {
783 if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
784 (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)) {
785 /* And don't move a CPU_ACCESS_REQUIRED BO to limited
786 * visible VRAM if we've depleted our allowance to do
789 if (p->bytes_moved_vis < p->bytes_moved_vis_threshold)
790 domain = bo->preferred_domains;
792 domain = bo->allowed_domains;
794 domain = bo->preferred_domains;
797 domain = bo->allowed_domains;
801 amdgpu_bo_placement_from_domain(bo, domain);
802 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
804 p->bytes_moved += ctx.bytes_moved;
805 if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
806 amdgpu_bo_in_cpu_visible_vram(bo))
807 p->bytes_moved_vis += ctx.bytes_moved;
809 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
810 domain = bo->allowed_domains;
817 static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
818 struct list_head *validated)
820 struct ttm_operation_ctx ctx = { true, false };
821 struct amdgpu_bo_list_entry *lobj;
824 list_for_each_entry(lobj, validated, tv.head) {
825 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(lobj->tv.bo);
826 struct mm_struct *usermm;
828 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
829 if (usermm && usermm != current->mm)
832 if (amdgpu_ttm_tt_is_userptr(bo->tbo.ttm) &&
833 lobj->user_invalidated && lobj->user_pages) {
834 amdgpu_bo_placement_from_domain(bo,
835 AMDGPU_GEM_DOMAIN_CPU);
836 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
840 amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm,
844 r = amdgpu_cs_bo_validate(p, bo);
848 kvfree(lobj->user_pages);
849 lobj->user_pages = NULL;
854 static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
855 union drm_amdgpu_cs *cs)
857 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
858 struct amdgpu_vm *vm = &fpriv->vm;
859 struct amdgpu_bo_list_entry *e;
860 struct list_head duplicates;
864 INIT_LIST_HEAD(&p->validated);
866 /* p->bo_list could already be assigned if AMDGPU_CHUNK_ID_BO_HANDLES is present */
867 if (cs->in.bo_list_handle) {
871 r = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle,
875 } else if (!p->bo_list) {
876 /* Create a empty bo_list when no handle is provided */
877 r = amdgpu_bo_list_create(p->adev, p->filp, NULL, 0,
883 mutex_lock(&p->bo_list->bo_list_mutex);
885 /* One for TTM and one for the CS job */
886 amdgpu_bo_list_for_each_entry(e, p->bo_list)
887 e->tv.num_shared = 2;
889 amdgpu_bo_list_get_list(p->bo_list, &p->validated);
891 INIT_LIST_HEAD(&duplicates);
892 amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
894 if (p->uf_entry.tv.bo && !ttm_to_amdgpu_bo(p->uf_entry.tv.bo)->parent)
895 list_add(&p->uf_entry.tv.head, &p->validated);
897 /* Get userptr backing pages. If pages are updated after registered
898 * in amdgpu_gem_userptr_ioctl(), amdgpu_cs_list_validate() will do
899 * amdgpu_ttm_backend_bind() to flush and invalidate new pages
901 amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
902 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
903 bool userpage_invalidated = false;
906 e->user_pages = kvmalloc_array(bo->tbo.ttm->num_pages,
907 sizeof(struct page *),
908 GFP_KERNEL | __GFP_ZERO);
909 if (!e->user_pages) {
910 DRM_ERROR("kvmalloc_array failure\n");
912 goto out_free_user_pages;
915 r = amdgpu_ttm_tt_get_user_pages(bo, e->user_pages, &e->range);
917 kvfree(e->user_pages);
918 e->user_pages = NULL;
919 goto out_free_user_pages;
922 for (i = 0; i < bo->tbo.ttm->num_pages; i++) {
923 if (bo->tbo.ttm->pages[i] != e->user_pages[i]) {
924 userpage_invalidated = true;
928 e->user_invalidated = userpage_invalidated;
931 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
933 if (unlikely(r != 0)) {
934 if (r != -ERESTARTSYS)
935 DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
936 goto out_free_user_pages;
939 amdgpu_bo_list_for_each_entry(e, p->bo_list) {
940 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
942 e->bo_va = amdgpu_vm_bo_find(vm, bo);
945 amdgpu_cs_get_threshold_for_moves(p->adev, &p->bytes_moved_threshold,
946 &p->bytes_moved_vis_threshold);
948 p->bytes_moved_vis = 0;
950 r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
951 amdgpu_cs_bo_validate, p);
953 DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
957 r = amdgpu_cs_list_validate(p, &duplicates);
961 r = amdgpu_cs_list_validate(p, &p->validated);
965 if (p->uf_entry.tv.bo) {
966 struct amdgpu_bo *uf = ttm_to_amdgpu_bo(p->uf_entry.tv.bo);
968 r = amdgpu_ttm_alloc_gart(&uf->tbo);
972 p->gang_leader->uf_addr += amdgpu_bo_gpu_offset(uf);
975 amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved,
978 for (i = 0; i < p->gang_size; ++i)
979 amdgpu_job_set_resources(p->jobs[i], p->bo_list->gds_obj,
985 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
988 amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
989 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
993 amdgpu_ttm_tt_get_user_pages_done(bo->tbo.ttm, e->range);
994 kvfree(e->user_pages);
995 e->user_pages = NULL;
998 mutex_unlock(&p->bo_list->bo_list_mutex);
1002 static void trace_amdgpu_cs_ibs(struct amdgpu_cs_parser *p)
1006 if (!trace_amdgpu_cs_enabled())
1009 for (i = 0; i < p->gang_size; ++i) {
1010 struct amdgpu_job *job = p->jobs[i];
1012 for (j = 0; j < job->num_ibs; ++j)
1013 trace_amdgpu_cs(p, job, &job->ibs[j]);
1017 static int amdgpu_cs_patch_ibs(struct amdgpu_cs_parser *p,
1018 struct amdgpu_job *job)
1020 struct amdgpu_ring *ring = amdgpu_job_ring(job);
1024 /* Only for UVD/VCE VM emulation */
1025 if (!ring->funcs->parse_cs && !ring->funcs->patch_cs_in_place)
1028 for (i = 0; i < job->num_ibs; ++i) {
1029 struct amdgpu_ib *ib = &job->ibs[i];
1030 struct amdgpu_bo_va_mapping *m;
1031 struct amdgpu_bo *aobj;
1035 va_start = ib->gpu_addr & AMDGPU_GMC_HOLE_MASK;
1036 r = amdgpu_cs_find_mapping(p, va_start, &aobj, &m);
1038 DRM_ERROR("IB va_start is invalid\n");
1042 if ((va_start + ib->length_dw * 4) >
1043 (m->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
1044 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
1048 /* the IB should be reserved at this point */
1049 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
1054 kptr += va_start - (m->start * AMDGPU_GPU_PAGE_SIZE);
1056 if (ring->funcs->parse_cs) {
1057 memcpy(ib->ptr, kptr, ib->length_dw * 4);
1058 amdgpu_bo_kunmap(aobj);
1060 r = amdgpu_ring_parse_cs(ring, p, job, ib);
1064 ib->ptr = (uint32_t *)kptr;
1065 r = amdgpu_ring_patch_cs_in_place(ring, p, job, ib);
1066 amdgpu_bo_kunmap(aobj);
1075 static int amdgpu_cs_patch_jobs(struct amdgpu_cs_parser *p)
1080 for (i = 0; i < p->gang_size; ++i) {
1081 r = amdgpu_cs_patch_ibs(p, p->jobs[i]);
1088 static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
1090 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
1091 struct amdgpu_job *job = p->gang_leader;
1092 struct amdgpu_device *adev = p->adev;
1093 struct amdgpu_vm *vm = &fpriv->vm;
1094 struct amdgpu_bo_list_entry *e;
1095 struct amdgpu_bo_va *bo_va;
1096 struct amdgpu_bo *bo;
1100 r = amdgpu_vm_clear_freed(adev, vm, NULL);
1104 r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
1108 r = amdgpu_sync_fence(&p->sync, fpriv->prt_va->last_pt_update);
1112 if (fpriv->csa_va) {
1113 bo_va = fpriv->csa_va;
1115 r = amdgpu_vm_bo_update(adev, bo_va, false);
1119 r = amdgpu_sync_fence(&p->sync, bo_va->last_pt_update);
1124 amdgpu_bo_list_for_each_entry(e, p->bo_list) {
1125 /* ignore duplicates */
1126 bo = ttm_to_amdgpu_bo(e->tv.bo);
1134 r = amdgpu_vm_bo_update(adev, bo_va, false);
1138 r = amdgpu_sync_fence(&p->sync, bo_va->last_pt_update);
1143 r = amdgpu_vm_handle_moved(adev, vm);
1147 r = amdgpu_vm_update_pdes(adev, vm, false);
1151 r = amdgpu_sync_fence(&p->sync, vm->last_update);
1155 for (i = 0; i < p->gang_size; ++i) {
1161 job->vm_pd_addr = amdgpu_gmc_pd_addr(vm->root.bo);
1164 if (amdgpu_vm_debug) {
1165 /* Invalidate all BOs to test for userspace bugs */
1166 amdgpu_bo_list_for_each_entry(e, p->bo_list) {
1167 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
1169 /* ignore duplicates */
1173 amdgpu_vm_bo_invalidate(adev, bo, false);
1180 static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
1182 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
1183 struct drm_gpu_scheduler *sched;
1184 struct amdgpu_bo_list_entry *e;
1185 struct dma_fence *fence;
1189 r = amdgpu_ctx_wait_prev_fence(p->ctx, p->entities[p->gang_leader_idx]);
1191 if (r != -ERESTARTSYS)
1192 DRM_ERROR("amdgpu_ctx_wait_prev_fence failed.\n");
1196 list_for_each_entry(e, &p->validated, tv.head) {
1197 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
1198 struct dma_resv *resv = bo->tbo.base.resv;
1199 enum amdgpu_sync_mode sync_mode;
1201 sync_mode = amdgpu_bo_explicit_sync(bo) ?
1202 AMDGPU_SYNC_EXPLICIT : AMDGPU_SYNC_NE_OWNER;
1203 r = amdgpu_sync_resv(p->adev, &p->sync, resv, sync_mode,
1209 for (i = 0; i < p->gang_size; ++i) {
1210 r = amdgpu_sync_push_to_job(&p->sync, p->jobs[i]);
1215 sched = p->gang_leader->base.entity->rq->sched;
1216 while ((fence = amdgpu_sync_get_fence(&p->sync))) {
1217 struct drm_sched_fence *s_fence = to_drm_sched_fence(fence);
1220 * When we have an dependency it might be necessary to insert a
1221 * pipeline sync to make sure that all caches etc are flushed and the
1222 * next job actually sees the results from the previous one
1223 * before we start executing on the same scheduler ring.
1225 if (!s_fence || s_fence->sched != sched)
1228 r = amdgpu_sync_fence(&p->gang_leader->explicit_sync, fence);
1235 static void amdgpu_cs_post_dependencies(struct amdgpu_cs_parser *p)
1239 for (i = 0; i < p->num_post_deps; ++i) {
1240 if (p->post_deps[i].chain && p->post_deps[i].point) {
1241 drm_syncobj_add_point(p->post_deps[i].syncobj,
1242 p->post_deps[i].chain,
1243 p->fence, p->post_deps[i].point);
1244 p->post_deps[i].chain = NULL;
1246 drm_syncobj_replace_fence(p->post_deps[i].syncobj,
1252 static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
1253 union drm_amdgpu_cs *cs)
1255 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
1256 struct amdgpu_job *leader = p->gang_leader;
1257 struct amdgpu_bo_list_entry *e;
1262 for (i = 0; i < p->gang_size; ++i)
1263 drm_sched_job_arm(&p->jobs[i]->base);
1265 for (i = 0; i < p->gang_size; ++i) {
1266 struct dma_fence *fence;
1268 if (p->jobs[i] == leader)
1271 fence = &p->jobs[i]->base.s_fence->scheduled;
1272 dma_fence_get(fence);
1273 r = drm_sched_job_add_dependency(&leader->base, fence);
1275 dma_fence_put(fence);
1280 if (p->gang_size > 1) {
1281 for (i = 0; i < p->gang_size; ++i)
1282 amdgpu_job_set_gang_leader(p->jobs[i], leader);
1285 /* No memory allocation is allowed while holding the notifier lock.
1286 * The lock is held until amdgpu_cs_submit is finished and fence is
1289 mutex_lock(&p->adev->notifier_lock);
1291 /* If userptr are invalidated after amdgpu_cs_parser_bos(), return
1292 * -EAGAIN, drmIoctl in libdrm will restart the amdgpu_cs_ioctl.
1295 amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
1296 struct amdgpu_bo *bo = ttm_to_amdgpu_bo(e->tv.bo);
1298 r |= !amdgpu_ttm_tt_get_user_pages_done(bo->tbo.ttm, e->range);
1306 p->fence = dma_fence_get(&leader->base.s_fence->finished);
1307 list_for_each_entry(e, &p->validated, tv.head) {
1309 /* Everybody except for the gang leader uses READ */
1310 for (i = 0; i < p->gang_size; ++i) {
1311 if (p->jobs[i] == leader)
1314 dma_resv_add_fence(e->tv.bo->base.resv,
1315 &p->jobs[i]->base.s_fence->finished,
1316 DMA_RESV_USAGE_READ);
1319 /* The gang leader is remembered as writer */
1320 e->tv.num_shared = 0;
1323 seq = amdgpu_ctx_add_fence(p->ctx, p->entities[p->gang_leader_idx],
1325 amdgpu_cs_post_dependencies(p);
1327 if ((leader->preamble_status & AMDGPU_PREAMBLE_IB_PRESENT) &&
1328 !p->ctx->preamble_presented) {
1329 leader->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
1330 p->ctx->preamble_presented = true;
1333 cs->out.handle = seq;
1334 leader->uf_sequence = seq;
1336 amdgpu_vm_bo_trace_cs(&fpriv->vm, &p->ticket);
1337 for (i = 0; i < p->gang_size; ++i) {
1338 amdgpu_job_free_resources(p->jobs[i]);
1339 trace_amdgpu_cs_ioctl(p->jobs[i]);
1340 drm_sched_entity_push_job(&p->jobs[i]->base);
1344 amdgpu_vm_move_to_lru_tail(p->adev, &fpriv->vm);
1345 ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
1347 mutex_unlock(&p->adev->notifier_lock);
1348 mutex_unlock(&p->bo_list->bo_list_mutex);
1352 mutex_unlock(&p->adev->notifier_lock);
1355 for (i = 0; i < p->gang_size; ++i)
1356 drm_sched_job_cleanup(&p->jobs[i]->base);
1360 /* Cleanup the parser structure */
1361 static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser)
1365 amdgpu_sync_free(&parser->sync);
1366 for (i = 0; i < parser->num_post_deps; i++) {
1367 drm_syncobj_put(parser->post_deps[i].syncobj);
1368 kfree(parser->post_deps[i].chain);
1370 kfree(parser->post_deps);
1372 dma_fence_put(parser->fence);
1375 amdgpu_ctx_put(parser->ctx);
1376 if (parser->bo_list)
1377 amdgpu_bo_list_put(parser->bo_list);
1379 for (i = 0; i < parser->nchunks; i++)
1380 kvfree(parser->chunks[i].kdata);
1381 kvfree(parser->chunks);
1382 for (i = 0; i < parser->gang_size; ++i) {
1383 if (parser->jobs[i])
1384 amdgpu_job_free(parser->jobs[i]);
1386 if (parser->uf_entry.tv.bo) {
1387 struct amdgpu_bo *uf = ttm_to_amdgpu_bo(parser->uf_entry.tv.bo);
1389 amdgpu_bo_unref(&uf);
1393 int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1395 struct amdgpu_device *adev = drm_to_adev(dev);
1396 struct amdgpu_cs_parser parser;
1399 if (amdgpu_ras_intr_triggered())
1402 if (!adev->accel_working)
1405 r = amdgpu_cs_parser_init(&parser, adev, filp, data);
1407 if (printk_ratelimit())
1408 DRM_ERROR("Failed to initialize parser %d!\n", r);
1412 r = amdgpu_cs_pass1(&parser, data);
1416 r = amdgpu_cs_pass2(&parser);
1420 r = amdgpu_cs_parser_bos(&parser, data);
1423 DRM_ERROR("Not enough memory for command submission!\n");
1424 else if (r != -ERESTARTSYS && r != -EAGAIN)
1425 DRM_ERROR("Failed to process the buffer list %d!\n", r);
1429 r = amdgpu_cs_patch_jobs(&parser);
1433 r = amdgpu_cs_vm_handling(&parser);
1437 r = amdgpu_cs_sync_rings(&parser);
1441 trace_amdgpu_cs_ibs(&parser);
1443 r = amdgpu_cs_submit(&parser, data);
1447 amdgpu_cs_parser_fini(&parser);
1451 ttm_eu_backoff_reservation(&parser.ticket, &parser.validated);
1452 mutex_unlock(&parser.bo_list->bo_list_mutex);
1455 amdgpu_cs_parser_fini(&parser);
1460 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1463 * @data: data from userspace
1464 * @filp: file private
1466 * Wait for the command submission identified by handle to finish.
1468 int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
1469 struct drm_file *filp)
1471 union drm_amdgpu_wait_cs *wait = data;
1472 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
1473 struct drm_sched_entity *entity;
1474 struct amdgpu_ctx *ctx;
1475 struct dma_fence *fence;
1478 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
1482 r = amdgpu_ctx_get_entity(ctx, wait->in.ip_type, wait->in.ip_instance,
1483 wait->in.ring, &entity);
1485 amdgpu_ctx_put(ctx);
1489 fence = amdgpu_ctx_get_fence(ctx, entity, wait->in.handle);
1493 r = dma_fence_wait_timeout(fence, true, timeout);
1494 if (r > 0 && fence->error)
1496 dma_fence_put(fence);
1500 amdgpu_ctx_put(ctx);
1504 memset(wait, 0, sizeof(*wait));
1505 wait->out.status = (r == 0);
1511 * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
1513 * @adev: amdgpu device
1514 * @filp: file private
1515 * @user: drm_amdgpu_fence copied from user space
1517 static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
1518 struct drm_file *filp,
1519 struct drm_amdgpu_fence *user)
1521 struct drm_sched_entity *entity;
1522 struct amdgpu_ctx *ctx;
1523 struct dma_fence *fence;
1526 ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
1528 return ERR_PTR(-EINVAL);
1530 r = amdgpu_ctx_get_entity(ctx, user->ip_type, user->ip_instance,
1531 user->ring, &entity);
1533 amdgpu_ctx_put(ctx);
1537 fence = amdgpu_ctx_get_fence(ctx, entity, user->seq_no);
1538 amdgpu_ctx_put(ctx);
1543 int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,
1544 struct drm_file *filp)
1546 struct amdgpu_device *adev = drm_to_adev(dev);
1547 union drm_amdgpu_fence_to_handle *info = data;
1548 struct dma_fence *fence;
1549 struct drm_syncobj *syncobj;
1550 struct sync_file *sync_file;
1553 fence = amdgpu_cs_get_fence(adev, filp, &info->in.fence);
1555 return PTR_ERR(fence);
1558 fence = dma_fence_get_stub();
1560 switch (info->in.what) {
1561 case AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ:
1562 r = drm_syncobj_create(&syncobj, 0, fence);
1563 dma_fence_put(fence);
1566 r = drm_syncobj_get_handle(filp, syncobj, &info->out.handle);
1567 drm_syncobj_put(syncobj);
1570 case AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ_FD:
1571 r = drm_syncobj_create(&syncobj, 0, fence);
1572 dma_fence_put(fence);
1575 r = drm_syncobj_get_fd(syncobj, (int *)&info->out.handle);
1576 drm_syncobj_put(syncobj);
1579 case AMDGPU_FENCE_TO_HANDLE_GET_SYNC_FILE_FD:
1580 fd = get_unused_fd_flags(O_CLOEXEC);
1582 dma_fence_put(fence);
1586 sync_file = sync_file_create(fence);
1587 dma_fence_put(fence);
1593 fd_install(fd, sync_file->file);
1594 info->out.handle = fd;
1598 dma_fence_put(fence);
1604 * amdgpu_cs_wait_all_fences - wait on all fences to signal
1606 * @adev: amdgpu device
1607 * @filp: file private
1608 * @wait: wait parameters
1609 * @fences: array of drm_amdgpu_fence
1611 static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
1612 struct drm_file *filp,
1613 union drm_amdgpu_wait_fences *wait,
1614 struct drm_amdgpu_fence *fences)
1616 uint32_t fence_count = wait->in.fence_count;
1620 for (i = 0; i < fence_count; i++) {
1621 struct dma_fence *fence;
1622 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1624 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1626 return PTR_ERR(fence);
1630 r = dma_fence_wait_timeout(fence, true, timeout);
1631 dma_fence_put(fence);
1639 return fence->error;
1642 memset(wait, 0, sizeof(*wait));
1643 wait->out.status = (r > 0);
1649 * amdgpu_cs_wait_any_fence - wait on any fence to signal
1651 * @adev: amdgpu device
1652 * @filp: file private
1653 * @wait: wait parameters
1654 * @fences: array of drm_amdgpu_fence
1656 static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
1657 struct drm_file *filp,
1658 union drm_amdgpu_wait_fences *wait,
1659 struct drm_amdgpu_fence *fences)
1661 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1662 uint32_t fence_count = wait->in.fence_count;
1663 uint32_t first = ~0;
1664 struct dma_fence **array;
1668 /* Prepare the fence array */
1669 array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
1674 for (i = 0; i < fence_count; i++) {
1675 struct dma_fence *fence;
1677 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1678 if (IS_ERR(fence)) {
1680 goto err_free_fence_array;
1683 } else { /* NULL, the fence has been already signaled */
1690 r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
1693 goto err_free_fence_array;
1696 memset(wait, 0, sizeof(*wait));
1697 wait->out.status = (r > 0);
1698 wait->out.first_signaled = first;
1700 if (first < fence_count && array[first])
1701 r = array[first]->error;
1705 err_free_fence_array:
1706 for (i = 0; i < fence_count; i++)
1707 dma_fence_put(array[i]);
1714 * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
1717 * @data: data from userspace
1718 * @filp: file private
1720 int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
1721 struct drm_file *filp)
1723 struct amdgpu_device *adev = drm_to_adev(dev);
1724 union drm_amdgpu_wait_fences *wait = data;
1725 uint32_t fence_count = wait->in.fence_count;
1726 struct drm_amdgpu_fence *fences_user;
1727 struct drm_amdgpu_fence *fences;
1730 /* Get the fences from userspace */
1731 fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
1736 fences_user = u64_to_user_ptr(wait->in.fences);
1737 if (copy_from_user(fences, fences_user,
1738 sizeof(struct drm_amdgpu_fence) * fence_count)) {
1740 goto err_free_fences;
1743 if (wait->in.wait_all)
1744 r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
1746 r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
1755 * amdgpu_cs_find_mapping - find bo_va for VM address
1757 * @parser: command submission parser context
1759 * @bo: resulting BO of the mapping found
1760 * @map: Placeholder to return found BO mapping
1762 * Search the buffer objects in the command submission context for a certain
1763 * virtual memory address. Returns allocation structure when found, NULL
1766 int amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1767 uint64_t addr, struct amdgpu_bo **bo,
1768 struct amdgpu_bo_va_mapping **map)
1770 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
1771 struct ttm_operation_ctx ctx = { false, false };
1772 struct amdgpu_vm *vm = &fpriv->vm;
1773 struct amdgpu_bo_va_mapping *mapping;
1776 addr /= AMDGPU_GPU_PAGE_SIZE;
1778 mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
1779 if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
1782 *bo = mapping->bo_va->base.bo;
1785 /* Double check that the BO is reserved by this CS */
1786 if (dma_resv_locking_ctx((*bo)->tbo.base.resv) != &parser->ticket)
1789 if (!((*bo)->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)) {
1790 (*bo)->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1791 amdgpu_bo_placement_from_domain(*bo, (*bo)->allowed_domains);
1792 r = ttm_bo_validate(&(*bo)->tbo, &(*bo)->placement, &ctx);
1797 return amdgpu_ttm_alloc_gart(&(*bo)->tbo);