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>
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;
44 gobj = drm_gem_object_lookup(p->filp, data->handle);
48 p->uf_entry.robj = amdgpu_bo_ref(gem_to_amdgpu_bo(gobj));
49 p->uf_entry.priority = 0;
50 p->uf_entry.tv.bo = &p->uf_entry.robj->tbo;
51 p->uf_entry.tv.shared = true;
52 p->uf_entry.user_pages = NULL;
54 size = amdgpu_bo_size(p->uf_entry.robj);
55 if (size != PAGE_SIZE || (data->offset + 8) > size)
58 *offset = data->offset;
60 drm_gem_object_put_unlocked(gobj);
62 if (amdgpu_ttm_tt_get_usermm(p->uf_entry.robj->tbo.ttm)) {
63 amdgpu_bo_unref(&p->uf_entry.robj);
70 static int amdgpu_cs_bo_handles_chunk(struct amdgpu_cs_parser *p,
71 struct drm_amdgpu_bo_list_in *data)
74 struct drm_amdgpu_bo_list_entry *info = NULL;
76 r = amdgpu_bo_create_list_entry_array(data, &info);
80 r = amdgpu_bo_list_create(p->adev, p->filp, info, data->bo_number,
95 static int amdgpu_cs_parser_init(struct amdgpu_cs_parser *p, union drm_amdgpu_cs *cs)
97 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
98 struct amdgpu_vm *vm = &fpriv->vm;
99 uint64_t *chunk_array_user;
100 uint64_t *chunk_array;
101 unsigned size, num_ibs = 0;
102 uint32_t uf_offset = 0;
106 if (cs->in.num_chunks == 0)
109 chunk_array = kmalloc_array(cs->in.num_chunks, sizeof(uint64_t), GFP_KERNEL);
113 p->ctx = amdgpu_ctx_get(fpriv, cs->in.ctx_id);
119 /* skip guilty context job */
120 if (atomic_read(&p->ctx->guilty) == 1) {
125 mutex_lock(&p->ctx->lock);
128 chunk_array_user = u64_to_user_ptr(cs->in.chunks);
129 if (copy_from_user(chunk_array, chunk_array_user,
130 sizeof(uint64_t)*cs->in.num_chunks)) {
135 p->nchunks = cs->in.num_chunks;
136 p->chunks = kmalloc_array(p->nchunks, sizeof(struct amdgpu_cs_chunk),
143 for (i = 0; i < p->nchunks; i++) {
144 struct drm_amdgpu_cs_chunk __user **chunk_ptr = NULL;
145 struct drm_amdgpu_cs_chunk user_chunk;
146 uint32_t __user *cdata;
148 chunk_ptr = u64_to_user_ptr(chunk_array[i]);
149 if (copy_from_user(&user_chunk, chunk_ptr,
150 sizeof(struct drm_amdgpu_cs_chunk))) {
153 goto free_partial_kdata;
155 p->chunks[i].chunk_id = user_chunk.chunk_id;
156 p->chunks[i].length_dw = user_chunk.length_dw;
158 size = p->chunks[i].length_dw;
159 cdata = u64_to_user_ptr(user_chunk.chunk_data);
161 p->chunks[i].kdata = kvmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
162 if (p->chunks[i].kdata == NULL) {
165 goto free_partial_kdata;
167 size *= sizeof(uint32_t);
168 if (copy_from_user(p->chunks[i].kdata, cdata, size)) {
170 goto free_partial_kdata;
173 switch (p->chunks[i].chunk_id) {
174 case AMDGPU_CHUNK_ID_IB:
178 case AMDGPU_CHUNK_ID_FENCE:
179 size = sizeof(struct drm_amdgpu_cs_chunk_fence);
180 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
182 goto free_partial_kdata;
185 ret = amdgpu_cs_user_fence_chunk(p, p->chunks[i].kdata,
188 goto free_partial_kdata;
192 case AMDGPU_CHUNK_ID_BO_HANDLES:
193 size = sizeof(struct drm_amdgpu_bo_list_in);
194 if (p->chunks[i].length_dw * sizeof(uint32_t) < size) {
196 goto free_partial_kdata;
199 ret = amdgpu_cs_bo_handles_chunk(p, p->chunks[i].kdata);
201 goto free_partial_kdata;
205 case AMDGPU_CHUNK_ID_DEPENDENCIES:
206 case AMDGPU_CHUNK_ID_SYNCOBJ_IN:
207 case AMDGPU_CHUNK_ID_SYNCOBJ_OUT:
212 goto free_partial_kdata;
216 ret = amdgpu_job_alloc(p->adev, num_ibs, &p->job, vm);
220 if (p->ctx->vram_lost_counter != p->job->vram_lost_counter) {
225 if (p->uf_entry.robj)
226 p->job->uf_addr = uf_offset;
229 /* Use this opportunity to fill in task info for the vm */
230 amdgpu_vm_set_task_info(vm);
238 kvfree(p->chunks[i].kdata);
248 /* Convert microseconds to bytes. */
249 static u64 us_to_bytes(struct amdgpu_device *adev, s64 us)
251 if (us <= 0 || !adev->mm_stats.log2_max_MBps)
254 /* Since accum_us is incremented by a million per second, just
255 * multiply it by the number of MB/s to get the number of bytes.
257 return us << adev->mm_stats.log2_max_MBps;
260 static s64 bytes_to_us(struct amdgpu_device *adev, u64 bytes)
262 if (!adev->mm_stats.log2_max_MBps)
265 return bytes >> adev->mm_stats.log2_max_MBps;
268 /* Returns how many bytes TTM can move right now. If no bytes can be moved,
269 * it returns 0. If it returns non-zero, it's OK to move at least one buffer,
270 * which means it can go over the threshold once. If that happens, the driver
271 * will be in debt and no other buffer migrations can be done until that debt
274 * This approach allows moving a buffer of any size (it's important to allow
277 * The currency is simply time in microseconds and it increases as the clock
278 * ticks. The accumulated microseconds (us) are converted to bytes and
281 static void amdgpu_cs_get_threshold_for_moves(struct amdgpu_device *adev,
285 s64 time_us, increment_us;
286 u64 free_vram, total_vram, used_vram;
288 /* Allow a maximum of 200 accumulated ms. This is basically per-IB
291 * It means that in order to get full max MBps, at least 5 IBs per
292 * second must be submitted and not more than 200ms apart from each
295 const s64 us_upper_bound = 200000;
297 if (!adev->mm_stats.log2_max_MBps) {
303 total_vram = adev->gmc.real_vram_size - atomic64_read(&adev->vram_pin_size);
304 used_vram = amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
305 free_vram = used_vram >= total_vram ? 0 : total_vram - used_vram;
307 spin_lock(&adev->mm_stats.lock);
309 /* Increase the amount of accumulated us. */
310 time_us = ktime_to_us(ktime_get());
311 increment_us = time_us - adev->mm_stats.last_update_us;
312 adev->mm_stats.last_update_us = time_us;
313 adev->mm_stats.accum_us = min(adev->mm_stats.accum_us + increment_us,
316 /* This prevents the short period of low performance when the VRAM
317 * usage is low and the driver is in debt or doesn't have enough
318 * accumulated us to fill VRAM quickly.
320 * The situation can occur in these cases:
321 * - a lot of VRAM is freed by userspace
322 * - the presence of a big buffer causes a lot of evictions
323 * (solution: split buffers into smaller ones)
325 * If 128 MB or 1/8th of VRAM is free, start filling it now by setting
326 * accum_us to a positive number.
328 if (free_vram >= 128 * 1024 * 1024 || free_vram >= total_vram / 8) {
331 /* Be more aggresive on dGPUs. Try to fill a portion of free
334 if (!(adev->flags & AMD_IS_APU))
335 min_us = bytes_to_us(adev, free_vram / 4);
337 min_us = 0; /* Reset accum_us on APUs. */
339 adev->mm_stats.accum_us = max(min_us, adev->mm_stats.accum_us);
342 /* This is set to 0 if the driver is in debt to disallow (optional)
345 *max_bytes = us_to_bytes(adev, adev->mm_stats.accum_us);
347 /* Do the same for visible VRAM if half of it is free */
348 if (!amdgpu_gmc_vram_full_visible(&adev->gmc)) {
349 u64 total_vis_vram = adev->gmc.visible_vram_size;
351 amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
353 if (used_vis_vram < total_vis_vram) {
354 u64 free_vis_vram = total_vis_vram - used_vis_vram;
355 adev->mm_stats.accum_us_vis = min(adev->mm_stats.accum_us_vis +
356 increment_us, us_upper_bound);
358 if (free_vis_vram >= total_vis_vram / 2)
359 adev->mm_stats.accum_us_vis =
360 max(bytes_to_us(adev, free_vis_vram / 2),
361 adev->mm_stats.accum_us_vis);
364 *max_vis_bytes = us_to_bytes(adev, adev->mm_stats.accum_us_vis);
369 spin_unlock(&adev->mm_stats.lock);
372 /* Report how many bytes have really been moved for the last command
373 * submission. This can result in a debt that can stop buffer migrations
376 void amdgpu_cs_report_moved_bytes(struct amdgpu_device *adev, u64 num_bytes,
379 spin_lock(&adev->mm_stats.lock);
380 adev->mm_stats.accum_us -= bytes_to_us(adev, num_bytes);
381 adev->mm_stats.accum_us_vis -= bytes_to_us(adev, num_vis_bytes);
382 spin_unlock(&adev->mm_stats.lock);
385 static int amdgpu_cs_bo_validate(struct amdgpu_cs_parser *p,
386 struct amdgpu_bo *bo)
388 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
389 struct ttm_operation_ctx ctx = {
390 .interruptible = true,
391 .no_wait_gpu = false,
392 .resv = bo->tbo.resv,
401 /* Don't move this buffer if we have depleted our allowance
402 * to move it. Don't move anything if the threshold is zero.
404 if (p->bytes_moved < p->bytes_moved_threshold) {
405 if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
406 (bo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)) {
407 /* And don't move a CPU_ACCESS_REQUIRED BO to limited
408 * visible VRAM if we've depleted our allowance to do
411 if (p->bytes_moved_vis < p->bytes_moved_vis_threshold)
412 domain = bo->preferred_domains;
414 domain = bo->allowed_domains;
416 domain = bo->preferred_domains;
419 domain = bo->allowed_domains;
423 amdgpu_bo_placement_from_domain(bo, domain);
424 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
426 p->bytes_moved += ctx.bytes_moved;
427 if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
428 amdgpu_bo_in_cpu_visible_vram(bo))
429 p->bytes_moved_vis += ctx.bytes_moved;
431 if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
432 domain = bo->allowed_domains;
439 /* Last resort, try to evict something from the current working set */
440 static bool amdgpu_cs_try_evict(struct amdgpu_cs_parser *p,
441 struct amdgpu_bo *validated)
443 uint32_t domain = validated->allowed_domains;
444 struct ttm_operation_ctx ctx = { true, false };
450 for (;&p->evictable->tv.head != &p->validated;
451 p->evictable = list_prev_entry(p->evictable, tv.head)) {
453 struct amdgpu_bo_list_entry *candidate = p->evictable;
454 struct amdgpu_bo *bo = candidate->robj;
455 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
456 bool update_bytes_moved_vis;
459 /* If we reached our current BO we can forget it */
460 if (candidate->robj == validated)
463 /* We can't move pinned BOs here */
467 other = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
469 /* Check if this BO is in one of the domains we need space for */
470 if (!(other & domain))
473 /* Check if we can move this BO somewhere else */
474 other = bo->allowed_domains & ~domain;
478 /* Good we can try to move this BO somewhere else */
479 update_bytes_moved_vis =
480 !amdgpu_gmc_vram_full_visible(&adev->gmc) &&
481 amdgpu_bo_in_cpu_visible_vram(bo);
482 amdgpu_bo_placement_from_domain(bo, other);
483 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
484 p->bytes_moved += ctx.bytes_moved;
485 if (update_bytes_moved_vis)
486 p->bytes_moved_vis += ctx.bytes_moved;
491 p->evictable = list_prev_entry(p->evictable, tv.head);
492 list_move(&candidate->tv.head, &p->validated);
500 static int amdgpu_cs_validate(void *param, struct amdgpu_bo *bo)
502 struct amdgpu_cs_parser *p = param;
506 r = amdgpu_cs_bo_validate(p, bo);
507 } while (r == -ENOMEM && amdgpu_cs_try_evict(p, bo));
512 r = amdgpu_cs_bo_validate(p, bo->shadow);
517 static int amdgpu_cs_list_validate(struct amdgpu_cs_parser *p,
518 struct list_head *validated)
520 struct ttm_operation_ctx ctx = { true, false };
521 struct amdgpu_bo_list_entry *lobj;
524 list_for_each_entry(lobj, validated, tv.head) {
525 struct amdgpu_bo *bo = lobj->robj;
526 bool binding_userptr = false;
527 struct mm_struct *usermm;
529 usermm = amdgpu_ttm_tt_get_usermm(bo->tbo.ttm);
530 if (usermm && usermm != current->mm)
533 /* Check if we have user pages and nobody bound the BO already */
534 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
536 amdgpu_bo_placement_from_domain(bo,
537 AMDGPU_GEM_DOMAIN_CPU);
538 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
541 amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm,
543 binding_userptr = true;
546 if (p->evictable == lobj)
549 r = amdgpu_cs_validate(p, bo);
553 if (binding_userptr) {
554 kvfree(lobj->user_pages);
555 lobj->user_pages = NULL;
561 static int amdgpu_cs_parser_bos(struct amdgpu_cs_parser *p,
562 union drm_amdgpu_cs *cs)
564 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
565 struct amdgpu_vm *vm = &fpriv->vm;
566 struct amdgpu_bo_list_entry *e;
567 struct list_head duplicates;
568 struct amdgpu_bo *gds;
569 struct amdgpu_bo *gws;
570 struct amdgpu_bo *oa;
574 INIT_LIST_HEAD(&p->validated);
576 /* p->bo_list could already be assigned if AMDGPU_CHUNK_ID_BO_HANDLES is present */
577 if (cs->in.bo_list_handle) {
581 r = amdgpu_bo_list_get(fpriv, cs->in.bo_list_handle,
585 } else if (!p->bo_list) {
586 /* Create a empty bo_list when no handle is provided */
587 r = amdgpu_bo_list_create(p->adev, p->filp, NULL, 0,
593 amdgpu_bo_list_get_list(p->bo_list, &p->validated);
594 if (p->bo_list->first_userptr != p->bo_list->num_entries)
595 p->mn = amdgpu_mn_get(p->adev, AMDGPU_MN_TYPE_GFX);
597 INIT_LIST_HEAD(&duplicates);
598 amdgpu_vm_get_pd_bo(&fpriv->vm, &p->validated, &p->vm_pd);
600 if (p->uf_entry.robj && !p->uf_entry.robj->parent)
601 list_add(&p->uf_entry.tv.head, &p->validated);
604 struct list_head need_pages;
606 r = ttm_eu_reserve_buffers(&p->ticket, &p->validated, true,
608 if (unlikely(r != 0)) {
609 if (r != -ERESTARTSYS)
610 DRM_ERROR("ttm_eu_reserve_buffers failed.\n");
611 goto error_free_pages;
614 INIT_LIST_HEAD(&need_pages);
615 amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
616 struct amdgpu_bo *bo = e->robj;
618 if (amdgpu_ttm_tt_userptr_invalidated(bo->tbo.ttm,
619 &e->user_invalidated) && e->user_pages) {
621 /* We acquired a page array, but somebody
622 * invalidated it. Free it and try again
624 release_pages(e->user_pages,
625 bo->tbo.ttm->num_pages);
626 kvfree(e->user_pages);
627 e->user_pages = NULL;
630 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm) &&
632 list_del(&e->tv.head);
633 list_add(&e->tv.head, &need_pages);
635 amdgpu_bo_unreserve(e->robj);
639 if (list_empty(&need_pages))
642 /* Unreserve everything again. */
643 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
645 /* We tried too many times, just abort */
648 DRM_ERROR("deadlock in %s\n", __func__);
649 goto error_free_pages;
652 /* Fill the page arrays for all userptrs. */
653 list_for_each_entry(e, &need_pages, tv.head) {
654 struct ttm_tt *ttm = e->robj->tbo.ttm;
656 e->user_pages = kvmalloc_array(ttm->num_pages,
657 sizeof(struct page*),
658 GFP_KERNEL | __GFP_ZERO);
659 if (!e->user_pages) {
661 DRM_ERROR("calloc failure in %s\n", __func__);
662 goto error_free_pages;
665 r = amdgpu_ttm_tt_get_user_pages(ttm, e->user_pages);
667 DRM_ERROR("amdgpu_ttm_tt_get_user_pages failed.\n");
668 kvfree(e->user_pages);
669 e->user_pages = NULL;
670 goto error_free_pages;
675 list_splice(&need_pages, &p->validated);
678 amdgpu_cs_get_threshold_for_moves(p->adev, &p->bytes_moved_threshold,
679 &p->bytes_moved_vis_threshold);
681 p->bytes_moved_vis = 0;
682 p->evictable = list_last_entry(&p->validated,
683 struct amdgpu_bo_list_entry,
686 r = amdgpu_vm_validate_pt_bos(p->adev, &fpriv->vm,
687 amdgpu_cs_validate, p);
689 DRM_ERROR("amdgpu_vm_validate_pt_bos() failed.\n");
693 r = amdgpu_cs_list_validate(p, &duplicates);
695 DRM_ERROR("amdgpu_cs_list_validate(duplicates) failed.\n");
699 r = amdgpu_cs_list_validate(p, &p->validated);
701 DRM_ERROR("amdgpu_cs_list_validate(validated) failed.\n");
705 amdgpu_cs_report_moved_bytes(p->adev, p->bytes_moved,
708 gds = p->bo_list->gds_obj;
709 gws = p->bo_list->gws_obj;
710 oa = p->bo_list->oa_obj;
712 amdgpu_bo_list_for_each_entry(e, p->bo_list)
713 e->bo_va = amdgpu_vm_bo_find(vm, e->robj);
716 p->job->gds_base = amdgpu_bo_gpu_offset(gds);
717 p->job->gds_size = amdgpu_bo_size(gds);
720 p->job->gws_base = amdgpu_bo_gpu_offset(gws);
721 p->job->gws_size = amdgpu_bo_size(gws);
724 p->job->oa_base = amdgpu_bo_gpu_offset(oa);
725 p->job->oa_size = amdgpu_bo_size(oa);
728 if (!r && p->uf_entry.robj) {
729 struct amdgpu_bo *uf = p->uf_entry.robj;
731 r = amdgpu_ttm_alloc_gart(&uf->tbo);
732 p->job->uf_addr += amdgpu_bo_gpu_offset(uf);
737 ttm_eu_backoff_reservation(&p->ticket, &p->validated);
741 amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
745 release_pages(e->user_pages,
746 e->robj->tbo.ttm->num_pages);
747 kvfree(e->user_pages);
753 static int amdgpu_cs_sync_rings(struct amdgpu_cs_parser *p)
755 struct amdgpu_bo_list_entry *e;
758 list_for_each_entry(e, &p->validated, tv.head) {
759 struct reservation_object *resv = e->robj->tbo.resv;
760 r = amdgpu_sync_resv(p->adev, &p->job->sync, resv, p->filp,
761 amdgpu_bo_explicit_sync(e->robj));
770 * cs_parser_fini() - clean parser states
771 * @parser: parser structure holding parsing context.
772 * @error: error number
774 * If error is set than unvalidate buffer, otherwise just free memory
775 * used by parsing context.
777 static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser, int error,
782 if (error && backoff)
783 ttm_eu_backoff_reservation(&parser->ticket,
786 for (i = 0; i < parser->num_post_dep_syncobjs; i++)
787 drm_syncobj_put(parser->post_dep_syncobjs[i]);
788 kfree(parser->post_dep_syncobjs);
790 dma_fence_put(parser->fence);
793 mutex_unlock(&parser->ctx->lock);
794 amdgpu_ctx_put(parser->ctx);
797 amdgpu_bo_list_put(parser->bo_list);
799 for (i = 0; i < parser->nchunks; i++)
800 kvfree(parser->chunks[i].kdata);
801 kfree(parser->chunks);
803 amdgpu_job_free(parser->job);
804 amdgpu_bo_unref(&parser->uf_entry.robj);
807 static int amdgpu_cs_vm_handling(struct amdgpu_cs_parser *p)
809 struct amdgpu_ring *ring = to_amdgpu_ring(p->entity->rq->sched);
810 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
811 struct amdgpu_device *adev = p->adev;
812 struct amdgpu_vm *vm = &fpriv->vm;
813 struct amdgpu_bo_list_entry *e;
814 struct amdgpu_bo_va *bo_va;
815 struct amdgpu_bo *bo;
818 /* Only for UVD/VCE VM emulation */
819 if (ring->funcs->parse_cs || ring->funcs->patch_cs_in_place) {
822 for (i = 0, j = 0; i < p->nchunks && j < p->job->num_ibs; i++) {
823 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
824 struct amdgpu_bo_va_mapping *m;
825 struct amdgpu_bo *aobj = NULL;
826 struct amdgpu_cs_chunk *chunk;
827 uint64_t offset, va_start;
828 struct amdgpu_ib *ib;
831 chunk = &p->chunks[i];
832 ib = &p->job->ibs[j];
833 chunk_ib = chunk->kdata;
835 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
838 va_start = chunk_ib->va_start & AMDGPU_VA_HOLE_MASK;
839 r = amdgpu_cs_find_mapping(p, va_start, &aobj, &m);
841 DRM_ERROR("IB va_start is invalid\n");
845 if ((va_start + chunk_ib->ib_bytes) >
846 (m->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
847 DRM_ERROR("IB va_start+ib_bytes is invalid\n");
851 /* the IB should be reserved at this point */
852 r = amdgpu_bo_kmap(aobj, (void **)&kptr);
857 offset = m->start * AMDGPU_GPU_PAGE_SIZE;
858 kptr += va_start - offset;
860 if (ring->funcs->parse_cs) {
861 memcpy(ib->ptr, kptr, chunk_ib->ib_bytes);
862 amdgpu_bo_kunmap(aobj);
864 r = amdgpu_ring_parse_cs(ring, p, j);
868 ib->ptr = (uint32_t *)kptr;
869 r = amdgpu_ring_patch_cs_in_place(ring, p, j);
870 amdgpu_bo_kunmap(aobj);
880 return amdgpu_cs_sync_rings(p);
883 r = amdgpu_vm_clear_freed(adev, vm, NULL);
887 r = amdgpu_vm_bo_update(adev, fpriv->prt_va, false);
891 r = amdgpu_sync_fence(adev, &p->job->sync,
892 fpriv->prt_va->last_pt_update, false);
896 if (amdgpu_sriov_vf(adev)) {
899 bo_va = fpriv->csa_va;
901 r = amdgpu_vm_bo_update(adev, bo_va, false);
905 f = bo_va->last_pt_update;
906 r = amdgpu_sync_fence(adev, &p->job->sync, f, false);
911 amdgpu_bo_list_for_each_entry(e, p->bo_list) {
914 /* ignore duplicates */
923 r = amdgpu_vm_bo_update(adev, bo_va, false);
927 f = bo_va->last_pt_update;
928 r = amdgpu_sync_fence(adev, &p->job->sync, f, false);
933 r = amdgpu_vm_handle_moved(adev, vm);
937 r = amdgpu_vm_update_directories(adev, vm);
941 r = amdgpu_sync_fence(adev, &p->job->sync, vm->last_update, false);
945 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
949 p->job->vm_pd_addr = amdgpu_gmc_pd_addr(vm->root.base.bo);
951 if (amdgpu_vm_debug) {
952 /* Invalidate all BOs to test for userspace bugs */
953 amdgpu_bo_list_for_each_entry(e, p->bo_list) {
954 /* ignore duplicates */
958 amdgpu_vm_bo_invalidate(adev, e->robj, false);
962 return amdgpu_cs_sync_rings(p);
965 static int amdgpu_cs_ib_fill(struct amdgpu_device *adev,
966 struct amdgpu_cs_parser *parser)
968 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
969 struct amdgpu_vm *vm = &fpriv->vm;
970 int r, ce_preempt = 0, de_preempt = 0;
971 struct amdgpu_ring *ring;
974 for (i = 0, j = 0; i < parser->nchunks && j < parser->job->num_ibs; i++) {
975 struct amdgpu_cs_chunk *chunk;
976 struct amdgpu_ib *ib;
977 struct drm_amdgpu_cs_chunk_ib *chunk_ib;
978 struct drm_sched_entity *entity;
980 chunk = &parser->chunks[i];
981 ib = &parser->job->ibs[j];
982 chunk_ib = (struct drm_amdgpu_cs_chunk_ib *)chunk->kdata;
984 if (chunk->chunk_id != AMDGPU_CHUNK_ID_IB)
987 if (chunk_ib->ip_type == AMDGPU_HW_IP_GFX && amdgpu_sriov_vf(adev)) {
988 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREEMPT) {
989 if (chunk_ib->flags & AMDGPU_IB_FLAG_CE)
995 /* each GFX command submit allows 0 or 1 IB preemptible for CE & DE */
996 if (ce_preempt > 1 || de_preempt > 1)
1000 r = amdgpu_ctx_get_entity(parser->ctx, chunk_ib->ip_type,
1001 chunk_ib->ip_instance, chunk_ib->ring,
1006 if (chunk_ib->flags & AMDGPU_IB_FLAG_PREAMBLE)
1007 parser->job->preamble_status |=
1008 AMDGPU_PREAMBLE_IB_PRESENT;
1010 if (parser->entity && parser->entity != entity)
1013 parser->entity = entity;
1015 ring = to_amdgpu_ring(entity->rq->sched);
1016 r = amdgpu_ib_get(adev, vm, ring->funcs->parse_cs ?
1017 chunk_ib->ib_bytes : 0, ib);
1019 DRM_ERROR("Failed to get ib !\n");
1023 ib->gpu_addr = chunk_ib->va_start;
1024 ib->length_dw = chunk_ib->ib_bytes / 4;
1025 ib->flags = chunk_ib->flags;
1030 /* UVD & VCE fw doesn't support user fences */
1031 ring = to_amdgpu_ring(parser->entity->rq->sched);
1032 if (parser->job->uf_addr && (
1033 ring->funcs->type == AMDGPU_RING_TYPE_UVD ||
1034 ring->funcs->type == AMDGPU_RING_TYPE_VCE))
1037 return amdgpu_ctx_wait_prev_fence(parser->ctx, parser->entity);
1040 static int amdgpu_cs_process_fence_dep(struct amdgpu_cs_parser *p,
1041 struct amdgpu_cs_chunk *chunk)
1043 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
1046 struct drm_amdgpu_cs_chunk_dep *deps;
1048 deps = (struct drm_amdgpu_cs_chunk_dep *)chunk->kdata;
1049 num_deps = chunk->length_dw * 4 /
1050 sizeof(struct drm_amdgpu_cs_chunk_dep);
1052 for (i = 0; i < num_deps; ++i) {
1053 struct amdgpu_ctx *ctx;
1054 struct drm_sched_entity *entity;
1055 struct dma_fence *fence;
1057 ctx = amdgpu_ctx_get(fpriv, deps[i].ctx_id);
1061 r = amdgpu_ctx_get_entity(ctx, deps[i].ip_type,
1062 deps[i].ip_instance,
1063 deps[i].ring, &entity);
1065 amdgpu_ctx_put(ctx);
1069 fence = amdgpu_ctx_get_fence(ctx, entity,
1071 if (IS_ERR(fence)) {
1073 amdgpu_ctx_put(ctx);
1076 r = amdgpu_sync_fence(p->adev, &p->job->sync, fence,
1078 dma_fence_put(fence);
1079 amdgpu_ctx_put(ctx);
1087 static int amdgpu_syncobj_lookup_and_add_to_sync(struct amdgpu_cs_parser *p,
1091 struct dma_fence *fence;
1092 r = drm_syncobj_find_fence(p->filp, handle, &fence);
1096 r = amdgpu_sync_fence(p->adev, &p->job->sync, fence, true);
1097 dma_fence_put(fence);
1102 static int amdgpu_cs_process_syncobj_in_dep(struct amdgpu_cs_parser *p,
1103 struct amdgpu_cs_chunk *chunk)
1107 struct drm_amdgpu_cs_chunk_sem *deps;
1109 deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
1110 num_deps = chunk->length_dw * 4 /
1111 sizeof(struct drm_amdgpu_cs_chunk_sem);
1113 for (i = 0; i < num_deps; ++i) {
1114 r = amdgpu_syncobj_lookup_and_add_to_sync(p, deps[i].handle);
1121 static int amdgpu_cs_process_syncobj_out_dep(struct amdgpu_cs_parser *p,
1122 struct amdgpu_cs_chunk *chunk)
1126 struct drm_amdgpu_cs_chunk_sem *deps;
1127 deps = (struct drm_amdgpu_cs_chunk_sem *)chunk->kdata;
1128 num_deps = chunk->length_dw * 4 /
1129 sizeof(struct drm_amdgpu_cs_chunk_sem);
1131 p->post_dep_syncobjs = kmalloc_array(num_deps,
1132 sizeof(struct drm_syncobj *),
1134 p->num_post_dep_syncobjs = 0;
1136 if (!p->post_dep_syncobjs)
1139 for (i = 0; i < num_deps; ++i) {
1140 p->post_dep_syncobjs[i] = drm_syncobj_find(p->filp, deps[i].handle);
1141 if (!p->post_dep_syncobjs[i])
1143 p->num_post_dep_syncobjs++;
1148 static int amdgpu_cs_dependencies(struct amdgpu_device *adev,
1149 struct amdgpu_cs_parser *p)
1153 for (i = 0; i < p->nchunks; ++i) {
1154 struct amdgpu_cs_chunk *chunk;
1156 chunk = &p->chunks[i];
1158 if (chunk->chunk_id == AMDGPU_CHUNK_ID_DEPENDENCIES) {
1159 r = amdgpu_cs_process_fence_dep(p, chunk);
1162 } else if (chunk->chunk_id == AMDGPU_CHUNK_ID_SYNCOBJ_IN) {
1163 r = amdgpu_cs_process_syncobj_in_dep(p, chunk);
1166 } else if (chunk->chunk_id == AMDGPU_CHUNK_ID_SYNCOBJ_OUT) {
1167 r = amdgpu_cs_process_syncobj_out_dep(p, chunk);
1176 static void amdgpu_cs_post_dependencies(struct amdgpu_cs_parser *p)
1180 for (i = 0; i < p->num_post_dep_syncobjs; ++i)
1181 drm_syncobj_replace_fence(p->post_dep_syncobjs[i], p->fence);
1184 static int amdgpu_cs_submit(struct amdgpu_cs_parser *p,
1185 union drm_amdgpu_cs *cs)
1187 struct amdgpu_fpriv *fpriv = p->filp->driver_priv;
1188 struct drm_sched_entity *entity = p->entity;
1189 enum drm_sched_priority priority;
1190 struct amdgpu_ring *ring;
1191 struct amdgpu_bo_list_entry *e;
1192 struct amdgpu_job *job;
1200 r = drm_sched_job_init(&job->base, entity, p->filp);
1204 /* No memory allocation is allowed while holding the mn lock */
1205 amdgpu_mn_lock(p->mn);
1206 amdgpu_bo_list_for_each_userptr_entry(e, p->bo_list) {
1207 struct amdgpu_bo *bo = e->robj;
1209 if (amdgpu_ttm_tt_userptr_needs_pages(bo->tbo.ttm)) {
1215 job->owner = p->filp;
1216 p->fence = dma_fence_get(&job->base.s_fence->finished);
1218 amdgpu_ctx_add_fence(p->ctx, entity, p->fence, &seq);
1219 amdgpu_cs_post_dependencies(p);
1221 if ((job->preamble_status & AMDGPU_PREAMBLE_IB_PRESENT) &&
1222 !p->ctx->preamble_presented) {
1223 job->preamble_status |= AMDGPU_PREAMBLE_IB_PRESENT_FIRST;
1224 p->ctx->preamble_presented = true;
1227 cs->out.handle = seq;
1228 job->uf_sequence = seq;
1230 amdgpu_job_free_resources(job);
1232 trace_amdgpu_cs_ioctl(job);
1233 amdgpu_vm_bo_trace_cs(&fpriv->vm, &p->ticket);
1234 priority = job->base.s_priority;
1235 drm_sched_entity_push_job(&job->base, entity);
1237 ring = to_amdgpu_ring(entity->rq->sched);
1238 amdgpu_ring_priority_get(ring, priority);
1240 amdgpu_vm_move_to_lru_tail(p->adev, &fpriv->vm);
1242 ttm_eu_fence_buffer_objects(&p->ticket, &p->validated, p->fence);
1243 amdgpu_mn_unlock(p->mn);
1248 dma_fence_put(&job->base.s_fence->finished);
1249 job->base.s_fence = NULL;
1250 amdgpu_mn_unlock(p->mn);
1253 amdgpu_job_free(job);
1257 int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1259 struct amdgpu_device *adev = dev->dev_private;
1260 union drm_amdgpu_cs *cs = data;
1261 struct amdgpu_cs_parser parser = {};
1262 bool reserved_buffers = false;
1265 if (!adev->accel_working)
1271 r = amdgpu_cs_parser_init(&parser, data);
1273 DRM_ERROR("Failed to initialize parser !\n");
1277 r = amdgpu_cs_ib_fill(adev, &parser);
1281 r = amdgpu_cs_parser_bos(&parser, data);
1284 DRM_ERROR("Not enough memory for command submission!\n");
1285 else if (r != -ERESTARTSYS)
1286 DRM_ERROR("Failed to process the buffer list %d!\n", r);
1290 reserved_buffers = true;
1292 r = amdgpu_cs_dependencies(adev, &parser);
1294 DRM_ERROR("Failed in the dependencies handling %d!\n", r);
1298 for (i = 0; i < parser.job->num_ibs; i++)
1299 trace_amdgpu_cs(&parser, i);
1301 r = amdgpu_cs_vm_handling(&parser);
1305 r = amdgpu_cs_submit(&parser, cs);
1308 amdgpu_cs_parser_fini(&parser, r, reserved_buffers);
1313 * amdgpu_cs_wait_ioctl - wait for a command submission to finish
1316 * @data: data from userspace
1317 * @filp: file private
1319 * Wait for the command submission identified by handle to finish.
1321 int amdgpu_cs_wait_ioctl(struct drm_device *dev, void *data,
1322 struct drm_file *filp)
1324 union drm_amdgpu_wait_cs *wait = data;
1325 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout);
1326 struct drm_sched_entity *entity;
1327 struct amdgpu_ctx *ctx;
1328 struct dma_fence *fence;
1331 ctx = amdgpu_ctx_get(filp->driver_priv, wait->in.ctx_id);
1335 r = amdgpu_ctx_get_entity(ctx, wait->in.ip_type, wait->in.ip_instance,
1336 wait->in.ring, &entity);
1338 amdgpu_ctx_put(ctx);
1342 fence = amdgpu_ctx_get_fence(ctx, entity, wait->in.handle);
1346 r = dma_fence_wait_timeout(fence, true, timeout);
1347 if (r > 0 && fence->error)
1349 dma_fence_put(fence);
1353 amdgpu_ctx_put(ctx);
1357 memset(wait, 0, sizeof(*wait));
1358 wait->out.status = (r == 0);
1364 * amdgpu_cs_get_fence - helper to get fence from drm_amdgpu_fence
1366 * @adev: amdgpu device
1367 * @filp: file private
1368 * @user: drm_amdgpu_fence copied from user space
1370 static struct dma_fence *amdgpu_cs_get_fence(struct amdgpu_device *adev,
1371 struct drm_file *filp,
1372 struct drm_amdgpu_fence *user)
1374 struct drm_sched_entity *entity;
1375 struct amdgpu_ctx *ctx;
1376 struct dma_fence *fence;
1379 ctx = amdgpu_ctx_get(filp->driver_priv, user->ctx_id);
1381 return ERR_PTR(-EINVAL);
1383 r = amdgpu_ctx_get_entity(ctx, user->ip_type, user->ip_instance,
1384 user->ring, &entity);
1386 amdgpu_ctx_put(ctx);
1390 fence = amdgpu_ctx_get_fence(ctx, entity, user->seq_no);
1391 amdgpu_ctx_put(ctx);
1396 int amdgpu_cs_fence_to_handle_ioctl(struct drm_device *dev, void *data,
1397 struct drm_file *filp)
1399 struct amdgpu_device *adev = dev->dev_private;
1400 union drm_amdgpu_fence_to_handle *info = data;
1401 struct dma_fence *fence;
1402 struct drm_syncobj *syncobj;
1403 struct sync_file *sync_file;
1406 fence = amdgpu_cs_get_fence(adev, filp, &info->in.fence);
1408 return PTR_ERR(fence);
1410 switch (info->in.what) {
1411 case AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ:
1412 r = drm_syncobj_create(&syncobj, 0, fence);
1413 dma_fence_put(fence);
1416 r = drm_syncobj_get_handle(filp, syncobj, &info->out.handle);
1417 drm_syncobj_put(syncobj);
1420 case AMDGPU_FENCE_TO_HANDLE_GET_SYNCOBJ_FD:
1421 r = drm_syncobj_create(&syncobj, 0, fence);
1422 dma_fence_put(fence);
1425 r = drm_syncobj_get_fd(syncobj, (int*)&info->out.handle);
1426 drm_syncobj_put(syncobj);
1429 case AMDGPU_FENCE_TO_HANDLE_GET_SYNC_FILE_FD:
1430 fd = get_unused_fd_flags(O_CLOEXEC);
1432 dma_fence_put(fence);
1436 sync_file = sync_file_create(fence);
1437 dma_fence_put(fence);
1443 fd_install(fd, sync_file->file);
1444 info->out.handle = fd;
1453 * amdgpu_cs_wait_all_fence - wait on all fences to signal
1455 * @adev: amdgpu device
1456 * @filp: file private
1457 * @wait: wait parameters
1458 * @fences: array of drm_amdgpu_fence
1460 static int amdgpu_cs_wait_all_fences(struct amdgpu_device *adev,
1461 struct drm_file *filp,
1462 union drm_amdgpu_wait_fences *wait,
1463 struct drm_amdgpu_fence *fences)
1465 uint32_t fence_count = wait->in.fence_count;
1469 for (i = 0; i < fence_count; i++) {
1470 struct dma_fence *fence;
1471 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1473 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1475 return PTR_ERR(fence);
1479 r = dma_fence_wait_timeout(fence, true, timeout);
1480 dma_fence_put(fence);
1488 return fence->error;
1491 memset(wait, 0, sizeof(*wait));
1492 wait->out.status = (r > 0);
1498 * amdgpu_cs_wait_any_fence - wait on any fence to signal
1500 * @adev: amdgpu device
1501 * @filp: file private
1502 * @wait: wait parameters
1503 * @fences: array of drm_amdgpu_fence
1505 static int amdgpu_cs_wait_any_fence(struct amdgpu_device *adev,
1506 struct drm_file *filp,
1507 union drm_amdgpu_wait_fences *wait,
1508 struct drm_amdgpu_fence *fences)
1510 unsigned long timeout = amdgpu_gem_timeout(wait->in.timeout_ns);
1511 uint32_t fence_count = wait->in.fence_count;
1512 uint32_t first = ~0;
1513 struct dma_fence **array;
1517 /* Prepare the fence array */
1518 array = kcalloc(fence_count, sizeof(struct dma_fence *), GFP_KERNEL);
1523 for (i = 0; i < fence_count; i++) {
1524 struct dma_fence *fence;
1526 fence = amdgpu_cs_get_fence(adev, filp, &fences[i]);
1527 if (IS_ERR(fence)) {
1529 goto err_free_fence_array;
1532 } else { /* NULL, the fence has been already signaled */
1539 r = dma_fence_wait_any_timeout(array, fence_count, true, timeout,
1542 goto err_free_fence_array;
1545 memset(wait, 0, sizeof(*wait));
1546 wait->out.status = (r > 0);
1547 wait->out.first_signaled = first;
1549 if (first < fence_count && array[first])
1550 r = array[first]->error;
1554 err_free_fence_array:
1555 for (i = 0; i < fence_count; i++)
1556 dma_fence_put(array[i]);
1563 * amdgpu_cs_wait_fences_ioctl - wait for multiple command submissions to finish
1566 * @data: data from userspace
1567 * @filp: file private
1569 int amdgpu_cs_wait_fences_ioctl(struct drm_device *dev, void *data,
1570 struct drm_file *filp)
1572 struct amdgpu_device *adev = dev->dev_private;
1573 union drm_amdgpu_wait_fences *wait = data;
1574 uint32_t fence_count = wait->in.fence_count;
1575 struct drm_amdgpu_fence *fences_user;
1576 struct drm_amdgpu_fence *fences;
1579 /* Get the fences from userspace */
1580 fences = kmalloc_array(fence_count, sizeof(struct drm_amdgpu_fence),
1585 fences_user = u64_to_user_ptr(wait->in.fences);
1586 if (copy_from_user(fences, fences_user,
1587 sizeof(struct drm_amdgpu_fence) * fence_count)) {
1589 goto err_free_fences;
1592 if (wait->in.wait_all)
1593 r = amdgpu_cs_wait_all_fences(adev, filp, wait, fences);
1595 r = amdgpu_cs_wait_any_fence(adev, filp, wait, fences);
1604 * amdgpu_cs_find_bo_va - find bo_va for VM address
1606 * @parser: command submission parser context
1608 * @bo: resulting BO of the mapping found
1610 * Search the buffer objects in the command submission context for a certain
1611 * virtual memory address. Returns allocation structure when found, NULL
1614 int amdgpu_cs_find_mapping(struct amdgpu_cs_parser *parser,
1615 uint64_t addr, struct amdgpu_bo **bo,
1616 struct amdgpu_bo_va_mapping **map)
1618 struct amdgpu_fpriv *fpriv = parser->filp->driver_priv;
1619 struct ttm_operation_ctx ctx = { false, false };
1620 struct amdgpu_vm *vm = &fpriv->vm;
1621 struct amdgpu_bo_va_mapping *mapping;
1624 addr /= AMDGPU_GPU_PAGE_SIZE;
1626 mapping = amdgpu_vm_bo_lookup_mapping(vm, addr);
1627 if (!mapping || !mapping->bo_va || !mapping->bo_va->base.bo)
1630 *bo = mapping->bo_va->base.bo;
1633 /* Double check that the BO is reserved by this CS */
1634 if (READ_ONCE((*bo)->tbo.resv->lock.ctx) != &parser->ticket)
1637 if (!((*bo)->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)) {
1638 (*bo)->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1639 amdgpu_bo_placement_from_domain(*bo, (*bo)->allowed_domains);
1640 r = ttm_bo_validate(&(*bo)->tbo, &(*bo)->placement, &ctx);
1645 return amdgpu_ttm_alloc_gart(&(*bo)->tbo);